1 // SPDX-License-Identifier: GPL-2.0-only
3 * Netlink interface for IEEE 802.15.4 stack
5 * Copyright 2007, 2008 Siemens AG
8 * Sergey Lapin <slapin@ossfans.org>
9 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
10 * Maxim Osipov <maxim.osipov@siemens.com>
13 #include <linux/gfp.h>
14 #include <linux/kernel.h>
15 #include <linux/if_arp.h>
16 #include <linux/netdevice.h>
17 #include <linux/ieee802154.h>
18 #include <net/netlink.h>
19 #include <net/genetlink.h>
21 #include <linux/nl802154.h>
22 #include <linux/export.h>
23 #include <net/af_ieee802154.h>
24 #include <net/ieee802154_netdev.h>
25 #include <net/cfg802154.h>
27 #include "ieee802154.h"
29 static int nla_put_hwaddr(struct sk_buff *msg, int type, __le64 hwaddr,
32 return nla_put_u64_64bit(msg, type, swab64((__force u64)hwaddr),
36 static __le64 nla_get_hwaddr(const struct nlattr *nla)
38 return ieee802154_devaddr_from_raw(nla_data(nla));
41 static int nla_put_shortaddr(struct sk_buff *msg, int type, __le16 addr)
43 return nla_put_u16(msg, type, le16_to_cpu(addr));
46 static __le16 nla_get_shortaddr(const struct nlattr *nla)
48 return cpu_to_le16(nla_get_u16(nla));
51 static int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
55 pr_debug("%s\n", __func__);
57 msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
61 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
62 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
63 nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
65 nla_put_u8(msg, IEEE802154_ATTR_STATUS, status))
67 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
74 static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid,
75 u32 seq, int flags, struct net_device *dev)
79 struct ieee802154_mlme_ops *ops;
80 __le16 short_addr, pan_id;
82 pr_debug("%s\n", __func__);
84 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
85 IEEE802154_LIST_IFACE);
89 ops = ieee802154_mlme_ops(dev);
90 phy = dev->ieee802154_ptr->wpan_phy;
92 get_device(&phy->dev);
95 short_addr = dev->ieee802154_ptr->short_addr;
96 pan_id = dev->ieee802154_ptr->pan_id;
99 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
100 nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
101 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
102 nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
104 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) ||
105 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, pan_id))
106 goto nla_put_failure;
108 if (ops->get_mac_params) {
109 struct ieee802154_mac_params params;
112 ops->get_mac_params(dev, ¶ms);
115 if (nla_put_s8(msg, IEEE802154_ATTR_TXPOWER,
116 params.transmit_power / 100) ||
117 nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, params.lbt) ||
118 nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE,
120 nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL,
121 params.cca_ed_level / 100) ||
122 nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES,
123 params.csma_retries) ||
124 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE,
126 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE,
128 nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES,
129 params.frame_retries))
130 goto nla_put_failure;
134 genlmsg_end(msg, hdr);
139 genlmsg_cancel(msg, hdr);
144 /* Requests from userspace */
145 static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
147 struct net_device *dev;
149 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
150 char name[IFNAMSIZ + 1];
152 nla_strscpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
154 dev = dev_get_by_name(&init_net, name);
155 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX]) {
156 dev = dev_get_by_index(&init_net,
157 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
165 if (dev->type != ARPHRD_IEEE802154) {
173 int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info)
175 struct net_device *dev;
176 struct ieee802154_addr addr;
178 int ret = -EOPNOTSUPP;
180 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
181 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
182 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
183 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
184 !info->attrs[IEEE802154_ATTR_CAPABILITY])
187 dev = ieee802154_nl_get_dev(info);
190 if (!ieee802154_mlme_ops(dev)->assoc_req)
193 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
194 addr.mode = IEEE802154_ADDR_LONG;
195 addr.extended_addr = nla_get_hwaddr(
196 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]);
198 addr.mode = IEEE802154_ADDR_SHORT;
199 addr.short_addr = nla_get_shortaddr(
200 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
202 addr.pan_id = nla_get_shortaddr(
203 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
205 if (info->attrs[IEEE802154_ATTR_PAGE])
206 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
210 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
211 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
213 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
220 int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info)
222 struct net_device *dev;
223 struct ieee802154_addr addr;
224 int ret = -EOPNOTSUPP;
226 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
227 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
228 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
231 dev = ieee802154_nl_get_dev(info);
234 if (!ieee802154_mlme_ops(dev)->assoc_resp)
237 addr.mode = IEEE802154_ADDR_LONG;
238 addr.extended_addr = nla_get_hwaddr(
239 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
241 addr.pan_id = dev->ieee802154_ptr->pan_id;
244 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
245 nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
246 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
253 int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info)
255 struct net_device *dev;
256 struct ieee802154_addr addr;
257 int ret = -EOPNOTSUPP;
259 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
260 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
261 !info->attrs[IEEE802154_ATTR_REASON])
264 dev = ieee802154_nl_get_dev(info);
267 if (!ieee802154_mlme_ops(dev)->disassoc_req)
270 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
271 addr.mode = IEEE802154_ADDR_LONG;
272 addr.extended_addr = nla_get_hwaddr(
273 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
275 addr.mode = IEEE802154_ADDR_SHORT;
276 addr.short_addr = nla_get_shortaddr(
277 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
280 addr.pan_id = dev->ieee802154_ptr->pan_id;
283 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
284 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
291 /* PANid, channel, beacon_order = 15, superframe_order = 15,
292 * PAN_coordinator, battery_life_extension = 0,
293 * coord_realignment = 0, security_enable = 0
295 int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
297 struct net_device *dev;
298 struct ieee802154_addr addr;
300 u8 channel, bcn_ord, sf_ord;
302 int pan_coord, blx, coord_realign;
305 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
306 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
307 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
308 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
309 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
310 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
311 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
312 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
316 dev = ieee802154_nl_get_dev(info);
320 if (netif_running(dev))
323 if (!ieee802154_mlme_ops(dev)->start_req) {
328 addr.mode = IEEE802154_ADDR_SHORT;
329 addr.short_addr = nla_get_shortaddr(
330 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
331 addr.pan_id = nla_get_shortaddr(
332 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
334 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
335 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
336 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
337 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
338 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
339 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
341 if (info->attrs[IEEE802154_ATTR_PAGE])
342 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
346 if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
347 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
353 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
354 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
357 /* FIXME: add validation for unused parameters to be sane
360 ieee802154_nl_start_confirm(dev, IEEE802154_SUCCESS);
367 int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
369 struct net_device *dev;
370 int ret = -EOPNOTSUPP;
376 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
377 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
378 !info->attrs[IEEE802154_ATTR_DURATION])
381 dev = ieee802154_nl_get_dev(info);
384 if (!ieee802154_mlme_ops(dev)->scan_req)
387 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
388 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
389 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
391 if (info->attrs[IEEE802154_ATTR_PAGE])
392 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
396 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
404 int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info)
406 /* Request for interface name, index, type, IEEE address,
407 * PAN Id, short address
410 struct net_device *dev = NULL;
413 pr_debug("%s\n", __func__);
415 dev = ieee802154_nl_get_dev(info);
419 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
423 rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq,
430 return genlmsg_reply(msg, info);
438 int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb)
440 struct net *net = sock_net(skb->sk);
441 struct net_device *dev;
443 int s_idx = cb->args[0];
445 pr_debug("%s\n", __func__);
448 for_each_netdev(net, dev) {
449 if (idx < s_idx || dev->type != ARPHRD_IEEE802154)
452 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid,
454 NLM_F_MULTI, dev) < 0)
464 int ieee802154_set_macparams(struct sk_buff *skb, struct genl_info *info)
466 struct net_device *dev = NULL;
467 struct ieee802154_mlme_ops *ops;
468 struct ieee802154_mac_params params;
469 struct wpan_phy *phy;
472 pr_debug("%s\n", __func__);
474 dev = ieee802154_nl_get_dev(info);
478 ops = ieee802154_mlme_ops(dev);
480 if (!ops->get_mac_params || !ops->set_mac_params) {
485 if (netif_running(dev)) {
490 if (!info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
491 !info->attrs[IEEE802154_ATTR_CCA_MODE] &&
492 !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] &&
493 !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] &&
494 !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] &&
495 !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] &&
496 !info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
499 phy = dev->ieee802154_ptr->wpan_phy;
500 get_device(&phy->dev);
503 ops->get_mac_params(dev, ¶ms);
505 if (info->attrs[IEEE802154_ATTR_TXPOWER])
506 params.transmit_power = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]) * 100;
508 if (info->attrs[IEEE802154_ATTR_LBT_ENABLED])
509 params.lbt = nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
511 if (info->attrs[IEEE802154_ATTR_CCA_MODE])
512 params.cca.mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
514 if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])
515 params.cca_ed_level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]) * 100;
517 if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES])
518 params.csma_retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]);
520 if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE])
521 params.min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]);
523 if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE])
524 params.max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]);
526 if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
527 params.frame_retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]);
529 rc = ops->set_mac_params(dev, ¶ms);
543 ieee802154_llsec_parse_key_id(struct genl_info *info,
544 struct ieee802154_llsec_key_id *desc)
546 memset(desc, 0, sizeof(*desc));
548 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE])
551 desc->mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]);
553 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
554 if (!info->attrs[IEEE802154_ATTR_PAN_ID])
557 desc->device_addr.pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
559 if (info->attrs[IEEE802154_ATTR_SHORT_ADDR]) {
560 desc->device_addr.mode = IEEE802154_ADDR_SHORT;
561 desc->device_addr.short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
563 if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
566 desc->device_addr.mode = IEEE802154_ADDR_LONG;
567 desc->device_addr.extended_addr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
571 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
572 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID])
575 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
576 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT])
579 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
580 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED])
583 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT)
584 desc->id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]);
586 switch (desc->mode) {
587 case IEEE802154_SCF_KEY_SHORT_INDEX:
589 u32 source = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]);
591 desc->short_source = cpu_to_le32(source);
594 case IEEE802154_SCF_KEY_HW_INDEX:
595 desc->extended_source = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]);
603 ieee802154_llsec_fill_key_id(struct sk_buff *msg,
604 const struct ieee802154_llsec_key_id *desc)
606 if (nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_MODE, desc->mode))
609 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
610 if (nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID,
611 desc->device_addr.pan_id))
614 if (desc->device_addr.mode == IEEE802154_ADDR_SHORT &&
615 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
616 desc->device_addr.short_addr))
619 if (desc->device_addr.mode == IEEE802154_ADDR_LONG &&
620 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR,
621 desc->device_addr.extended_addr,
622 IEEE802154_ATTR_PAD))
626 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
627 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_ID, desc->id))
630 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
631 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT,
632 le32_to_cpu(desc->short_source)))
635 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
636 nla_put_hwaddr(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED,
637 desc->extended_source, IEEE802154_ATTR_PAD))
643 int ieee802154_llsec_getparams(struct sk_buff *skb, struct genl_info *info)
646 struct net_device *dev = NULL;
648 struct ieee802154_mlme_ops *ops;
650 struct ieee802154_llsec_params params;
652 pr_debug("%s\n", __func__);
654 dev = ieee802154_nl_get_dev(info);
658 ops = ieee802154_mlme_ops(dev);
664 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
668 hdr = genlmsg_put(msg, 0, info->snd_seq, &nl802154_family, 0,
669 IEEE802154_LLSEC_GETPARAMS);
673 rc = ops->llsec->get_params(dev, ¶ms);
677 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
678 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
679 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_ENABLED, params.enabled) ||
680 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVEL, params.out_level) ||
681 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
682 be32_to_cpu(params.frame_counter)) ||
683 ieee802154_llsec_fill_key_id(msg, ¶ms.out_key))
688 return ieee802154_nl_reply(msg, info);
696 int ieee802154_llsec_setparams(struct sk_buff *skb, struct genl_info *info)
698 struct net_device *dev = NULL;
700 struct ieee802154_mlme_ops *ops;
701 struct ieee802154_llsec_params params;
704 pr_debug("%s\n", __func__);
706 dev = ieee802154_nl_get_dev(info);
710 if (!info->attrs[IEEE802154_ATTR_LLSEC_ENABLED] &&
711 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE] &&
712 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL])
715 ops = ieee802154_mlme_ops(dev);
721 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL] &&
722 nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) > 7)
725 if (info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]) {
726 params.enabled = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]);
727 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
730 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) {
731 if (ieee802154_llsec_parse_key_id(info, ¶ms.out_key))
734 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
737 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) {
738 params.out_level = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]);
739 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
742 if (info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]) {
743 u32 fc = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
745 params.frame_counter = cpu_to_be32(fc);
746 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
749 rc = ops->llsec->set_params(dev, ¶ms, changed);
759 struct llsec_dump_data {
764 struct net_device *dev;
765 struct ieee802154_mlme_ops *ops;
766 struct ieee802154_llsec_table *table;
770 ieee802154_llsec_dump_table(struct sk_buff *skb, struct netlink_callback *cb,
771 int (*step)(struct llsec_dump_data *))
773 struct net *net = sock_net(skb->sk);
774 struct net_device *dev;
775 struct llsec_dump_data data;
777 int first_dev = cb->args[0];
780 for_each_netdev(net, dev) {
781 if (idx < first_dev || dev->type != ARPHRD_IEEE802154)
784 data.ops = ieee802154_mlme_ops(dev);
785 if (!data.ops->llsec)
789 data.s_idx = cb->args[1];
790 data.s_idx2 = cb->args[2];
792 data.portid = NETLINK_CB(cb->skb).portid;
793 data.nlmsg_seq = cb->nlh->nlmsg_seq;
795 data.ops->llsec->lock_table(dev);
796 data.ops->llsec->get_table(data.dev, &data.table);
798 data.ops->llsec->unlock_table(dev);
812 ieee802154_nl_llsec_change(struct sk_buff *skb, struct genl_info *info,
813 int (*fn)(struct net_device*, struct genl_info*))
815 struct net_device *dev = NULL;
818 dev = ieee802154_nl_get_dev(info);
822 if (!ieee802154_mlme_ops(dev)->llsec)
832 ieee802154_llsec_parse_key(struct genl_info *info,
833 struct ieee802154_llsec_key *key)
836 u32 commands[256 / 32];
838 memset(key, 0, sizeof(*key));
840 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES] ||
841 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES])
844 frames = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES]);
845 if ((frames & BIT(IEEE802154_FC_TYPE_MAC_CMD)) &&
846 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS])
849 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) {
851 info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS],
854 if (commands[0] || commands[1] || commands[2] || commands[3] ||
855 commands[4] || commands[5] || commands[6] ||
856 commands[7] >= BIT(IEEE802154_CMD_GTS_REQ + 1))
859 key->cmd_frame_ids = commands[7];
862 key->frame_types = frames;
864 nla_memcpy(key->key, info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES],
865 IEEE802154_LLSEC_KEY_SIZE);
870 static int llsec_add_key(struct net_device *dev, struct genl_info *info)
872 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
873 struct ieee802154_llsec_key key;
874 struct ieee802154_llsec_key_id id;
876 if (ieee802154_llsec_parse_key(info, &key) ||
877 ieee802154_llsec_parse_key_id(info, &id))
880 return ops->llsec->add_key(dev, &id, &key);
883 int ieee802154_llsec_add_key(struct sk_buff *skb, struct genl_info *info)
885 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
886 (NLM_F_CREATE | NLM_F_EXCL))
889 return ieee802154_nl_llsec_change(skb, info, llsec_add_key);
892 static int llsec_remove_key(struct net_device *dev, struct genl_info *info)
894 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
895 struct ieee802154_llsec_key_id id;
897 if (ieee802154_llsec_parse_key_id(info, &id))
900 return ops->llsec->del_key(dev, &id);
903 int ieee802154_llsec_del_key(struct sk_buff *skb, struct genl_info *info)
905 return ieee802154_nl_llsec_change(skb, info, llsec_remove_key);
909 ieee802154_nl_fill_key(struct sk_buff *msg, u32 portid, u32 seq,
910 const struct ieee802154_llsec_key_entry *key,
911 const struct net_device *dev)
914 u32 commands[256 / 32];
916 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
917 IEEE802154_LLSEC_LIST_KEY);
921 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
922 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
923 ieee802154_llsec_fill_key_id(msg, &key->id) ||
924 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES,
925 key->key->frame_types))
926 goto nla_put_failure;
928 if (key->key->frame_types & BIT(IEEE802154_FC_TYPE_MAC_CMD)) {
929 memset(commands, 0, sizeof(commands));
930 commands[7] = key->key->cmd_frame_ids;
931 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS,
932 sizeof(commands), commands))
933 goto nla_put_failure;
936 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_BYTES,
937 IEEE802154_LLSEC_KEY_SIZE, key->key->key))
938 goto nla_put_failure;
940 genlmsg_end(msg, hdr);
944 genlmsg_cancel(msg, hdr);
949 static int llsec_iter_keys(struct llsec_dump_data *data)
951 struct ieee802154_llsec_key_entry *pos;
954 list_for_each_entry(pos, &data->table->keys, list) {
955 if (idx++ < data->s_idx)
958 if (ieee802154_nl_fill_key(data->skb, data->portid,
959 data->nlmsg_seq, pos, data->dev)) {
970 int ieee802154_llsec_dump_keys(struct sk_buff *skb, struct netlink_callback *cb)
972 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_keys);
976 llsec_parse_dev(struct genl_info *info,
977 struct ieee802154_llsec_device *dev)
979 memset(dev, 0, sizeof(*dev));
981 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
982 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
983 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE] ||
984 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE] ||
985 (!!info->attrs[IEEE802154_ATTR_PAN_ID] !=
986 !!info->attrs[IEEE802154_ATTR_SHORT_ADDR]))
989 if (info->attrs[IEEE802154_ATTR_PAN_ID]) {
990 dev->pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
991 dev->short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
993 dev->short_addr = cpu_to_le16(IEEE802154_ADDR_UNDEF);
996 dev->hwaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
997 dev->frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
998 dev->seclevel_exempt = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
999 dev->key_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE]);
1001 if (dev->key_mode >= __IEEE802154_LLSEC_DEVKEY_MAX)
1007 static int llsec_add_dev(struct net_device *dev, struct genl_info *info)
1009 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1010 struct ieee802154_llsec_device desc;
1012 if (llsec_parse_dev(info, &desc))
1015 return ops->llsec->add_dev(dev, &desc);
1018 int ieee802154_llsec_add_dev(struct sk_buff *skb, struct genl_info *info)
1020 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1021 (NLM_F_CREATE | NLM_F_EXCL))
1024 return ieee802154_nl_llsec_change(skb, info, llsec_add_dev);
1027 static int llsec_del_dev(struct net_device *dev, struct genl_info *info)
1029 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1032 if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
1035 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1037 return ops->llsec->del_dev(dev, devaddr);
1040 int ieee802154_llsec_del_dev(struct sk_buff *skb, struct genl_info *info)
1042 return ieee802154_nl_llsec_change(skb, info, llsec_del_dev);
1046 ieee802154_nl_fill_dev(struct sk_buff *msg, u32 portid, u32 seq,
1047 const struct ieee802154_llsec_device *desc,
1048 const struct net_device *dev)
1052 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1053 IEEE802154_LLSEC_LIST_DEV);
1057 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1058 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1059 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, desc->pan_id) ||
1060 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
1061 desc->short_addr) ||
1062 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, desc->hwaddr,
1063 IEEE802154_ATTR_PAD) ||
1064 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1065 desc->frame_counter) ||
1066 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1067 desc->seclevel_exempt) ||
1068 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE, desc->key_mode))
1069 goto nla_put_failure;
1071 genlmsg_end(msg, hdr);
1075 genlmsg_cancel(msg, hdr);
1080 static int llsec_iter_devs(struct llsec_dump_data *data)
1082 struct ieee802154_llsec_device *pos;
1083 int rc = 0, idx = 0;
1085 list_for_each_entry(pos, &data->table->devices, list) {
1086 if (idx++ < data->s_idx)
1089 if (ieee802154_nl_fill_dev(data->skb, data->portid,
1090 data->nlmsg_seq, pos, data->dev)) {
1101 int ieee802154_llsec_dump_devs(struct sk_buff *skb, struct netlink_callback *cb)
1103 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devs);
1106 static int llsec_add_devkey(struct net_device *dev, struct genl_info *info)
1108 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1109 struct ieee802154_llsec_device_key key;
1112 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
1113 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1114 ieee802154_llsec_parse_key_id(info, &key.key_id))
1117 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1118 key.frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1120 return ops->llsec->add_devkey(dev, devaddr, &key);
1123 int ieee802154_llsec_add_devkey(struct sk_buff *skb, struct genl_info *info)
1125 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1126 (NLM_F_CREATE | NLM_F_EXCL))
1129 return ieee802154_nl_llsec_change(skb, info, llsec_add_devkey);
1132 static int llsec_del_devkey(struct net_device *dev, struct genl_info *info)
1134 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1135 struct ieee802154_llsec_device_key key;
1138 if (!info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1139 ieee802154_llsec_parse_key_id(info, &key.key_id))
1142 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1144 return ops->llsec->del_devkey(dev, devaddr, &key);
1147 int ieee802154_llsec_del_devkey(struct sk_buff *skb, struct genl_info *info)
1149 return ieee802154_nl_llsec_change(skb, info, llsec_del_devkey);
1153 ieee802154_nl_fill_devkey(struct sk_buff *msg, u32 portid, u32 seq,
1155 const struct ieee802154_llsec_device_key *devkey,
1156 const struct net_device *dev)
1160 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1161 IEEE802154_LLSEC_LIST_DEVKEY);
1165 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1166 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1167 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, devaddr,
1168 IEEE802154_ATTR_PAD) ||
1169 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1170 devkey->frame_counter) ||
1171 ieee802154_llsec_fill_key_id(msg, &devkey->key_id))
1172 goto nla_put_failure;
1174 genlmsg_end(msg, hdr);
1178 genlmsg_cancel(msg, hdr);
1183 static int llsec_iter_devkeys(struct llsec_dump_data *data)
1185 struct ieee802154_llsec_device *dpos;
1186 struct ieee802154_llsec_device_key *kpos;
1187 int rc = 0, idx = 0, idx2;
1189 list_for_each_entry(dpos, &data->table->devices, list) {
1190 if (idx++ < data->s_idx)
1195 list_for_each_entry(kpos, &dpos->keys, list) {
1196 if (idx2++ < data->s_idx2)
1199 if (ieee802154_nl_fill_devkey(data->skb, data->portid,
1203 return rc = -EMSGSIZE;
1215 int ieee802154_llsec_dump_devkeys(struct sk_buff *skb,
1216 struct netlink_callback *cb)
1218 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devkeys);
1222 llsec_parse_seclevel(struct genl_info *info,
1223 struct ieee802154_llsec_seclevel *sl)
1225 memset(sl, 0, sizeof(*sl));
1227 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE] ||
1228 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS] ||
1229 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE])
1232 sl->frame_type = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE]);
1233 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD) {
1234 if (!info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID])
1237 sl->cmd_frame_id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]);
1240 sl->sec_levels = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS]);
1241 sl->device_override = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1246 static int llsec_add_seclevel(struct net_device *dev, struct genl_info *info)
1248 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1249 struct ieee802154_llsec_seclevel sl;
1251 if (llsec_parse_seclevel(info, &sl))
1254 return ops->llsec->add_seclevel(dev, &sl);
1257 int ieee802154_llsec_add_seclevel(struct sk_buff *skb, struct genl_info *info)
1259 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1260 (NLM_F_CREATE | NLM_F_EXCL))
1263 return ieee802154_nl_llsec_change(skb, info, llsec_add_seclevel);
1266 static int llsec_del_seclevel(struct net_device *dev, struct genl_info *info)
1268 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1269 struct ieee802154_llsec_seclevel sl;
1271 if (llsec_parse_seclevel(info, &sl))
1274 return ops->llsec->del_seclevel(dev, &sl);
1277 int ieee802154_llsec_del_seclevel(struct sk_buff *skb, struct genl_info *info)
1279 return ieee802154_nl_llsec_change(skb, info, llsec_del_seclevel);
1283 ieee802154_nl_fill_seclevel(struct sk_buff *msg, u32 portid, u32 seq,
1284 const struct ieee802154_llsec_seclevel *sl,
1285 const struct net_device *dev)
1289 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1290 IEEE802154_LLSEC_LIST_SECLEVEL);
1294 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1295 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1296 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_FRAME_TYPE, sl->frame_type) ||
1297 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVELS, sl->sec_levels) ||
1298 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1299 sl->device_override))
1300 goto nla_put_failure;
1302 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD &&
1303 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID,
1305 goto nla_put_failure;
1307 genlmsg_end(msg, hdr);
1311 genlmsg_cancel(msg, hdr);
1316 static int llsec_iter_seclevels(struct llsec_dump_data *data)
1318 struct ieee802154_llsec_seclevel *pos;
1319 int rc = 0, idx = 0;
1321 list_for_each_entry(pos, &data->table->security_levels, list) {
1322 if (idx++ < data->s_idx)
1325 if (ieee802154_nl_fill_seclevel(data->skb, data->portid,
1326 data->nlmsg_seq, pos,
1338 int ieee802154_llsec_dump_seclevels(struct sk_buff *skb,
1339 struct netlink_callback *cb)
1341 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_seclevels);