^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Netlink interface for IEEE 802.15.4 stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2007, 2008 Siemens AG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Written by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Sergey Lapin <slapin@ossfans.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Maxim Osipov <maxim.osipov@siemens.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <net/genetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/nl802154.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "ieee802154.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static unsigned int ieee802154_seq_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static DEFINE_SPINLOCK(ieee802154_seq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* Requests to userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct sk_buff *ieee802154_nl_create(int flags, u8 req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) void *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned long f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) spin_lock_irqsave(&ieee802154_seq_lock, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) &nl802154_family, flags, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) spin_unlock_irqrestore(&ieee802154_seq_lock, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (!hdr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) nlmsg_free(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct nlmsghdr *nlh = nlmsg_hdr(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void *hdr = genlmsg_data(nlmsg_data(nlh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) genlmsg_end(msg, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int flags, u8 req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) void *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) hdr = genlmsg_put_reply(msg, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) &nl802154_family, flags, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!hdr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) nlmsg_free(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct nlmsghdr *nlh = nlmsg_hdr(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) void *hdr = genlmsg_data(nlmsg_data(nlh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) genlmsg_end(msg, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return genlmsg_reply(msg, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static const struct genl_small_ops ieee802154_ops[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* see nl-phy.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) ieee802154_dump_phy),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* see nl-mac.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ieee802154_dump_iface),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) IEEE802154_OP(IEEE802154_SET_MACPARAMS, ieee802154_set_macparams),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) IEEE802154_OP(IEEE802154_LLSEC_GETPARAMS, ieee802154_llsec_getparams),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) IEEE802154_OP(IEEE802154_LLSEC_SETPARAMS, ieee802154_llsec_setparams),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) IEEE802154_DUMP(IEEE802154_LLSEC_LIST_KEY, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) ieee802154_llsec_dump_keys),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) IEEE802154_OP(IEEE802154_LLSEC_ADD_KEY, ieee802154_llsec_add_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) IEEE802154_OP(IEEE802154_LLSEC_DEL_KEY, ieee802154_llsec_del_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEV, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ieee802154_llsec_dump_devs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) IEEE802154_OP(IEEE802154_LLSEC_ADD_DEV, ieee802154_llsec_add_dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) IEEE802154_OP(IEEE802154_LLSEC_DEL_DEV, ieee802154_llsec_del_dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEVKEY, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ieee802154_llsec_dump_devkeys),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) IEEE802154_OP(IEEE802154_LLSEC_ADD_DEVKEY, ieee802154_llsec_add_devkey),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) IEEE802154_OP(IEEE802154_LLSEC_DEL_DEVKEY, ieee802154_llsec_del_devkey),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) IEEE802154_DUMP(IEEE802154_LLSEC_LIST_SECLEVEL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ieee802154_llsec_dump_seclevels),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) IEEE802154_OP(IEEE802154_LLSEC_ADD_SECLEVEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ieee802154_llsec_add_seclevel),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) IEEE802154_OP(IEEE802154_LLSEC_DEL_SECLEVEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ieee802154_llsec_del_seclevel),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static const struct genl_multicast_group ieee802154_mcgrps[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) [IEEE802154_COORD_MCGRP] = { .name = IEEE802154_MCAST_COORD_NAME, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) [IEEE802154_BEACON_MCGRP] = { .name = IEEE802154_MCAST_BEACON_NAME, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct genl_family nl802154_family __ro_after_init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) .hdrsize = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .name = IEEE802154_NL_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .version = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) .maxattr = IEEE802154_ATTR_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .policy = ieee802154_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .small_ops = ieee802154_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .n_small_ops = ARRAY_SIZE(ieee802154_ops),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .mcgrps = ieee802154_mcgrps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .n_mcgrps = ARRAY_SIZE(ieee802154_mcgrps),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int __init ieee802154_nl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return genl_register_family(&nl802154_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) void ieee802154_nl_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) genl_unregister_family(&nl802154_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }