2 * Netlink interface for IEEE 802.15.4 stack
4 * Copyright 2007, 2008 Siemens AG
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 * Sergey Lapin <slapin@ossfans.org>
17 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
18 * Maxim Osipov <maxim.osipov@siemens.com>
21 #include <linux/gfp.h>
22 #include <linux/kernel.h>
23 #include <linux/if_arp.h>
24 #include <linux/netdevice.h>
25 #include <linux/ieee802154.h>
26 #include <net/netlink.h>
27 #include <net/genetlink.h>
29 #include <linux/nl802154.h>
30 #include <linux/export.h>
31 #include <net/af_ieee802154.h>
32 #include <net/ieee802154_netdev.h>
33 #include <net/cfg802154.h>
35 #include "ieee802154.h"
37 static int nla_put_hwaddr(struct sk_buff *msg, int type, __le64 hwaddr)
39 return nla_put_u64(msg, type, swab64((__force u64)hwaddr));
42 static __le64 nla_get_hwaddr(const struct nlattr *nla)
44 return ieee802154_devaddr_from_raw(nla_data(nla));
47 static int nla_put_shortaddr(struct sk_buff *msg, int type, __le16 addr)
49 return nla_put_u16(msg, type, le16_to_cpu(addr));
52 static __le16 nla_get_shortaddr(const struct nlattr *nla)
54 return cpu_to_le16(nla_get_u16(nla));
57 static int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
61 pr_debug("%s\n", __func__);
63 msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
67 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
68 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
69 nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
71 nla_put_u8(msg, IEEE802154_ATTR_STATUS, status))
73 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
80 static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid,
81 u32 seq, int flags, struct net_device *dev)
85 struct ieee802154_mlme_ops *ops;
86 __le16 short_addr, pan_id;
88 pr_debug("%s\n", __func__);
90 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
91 IEEE802154_LIST_IFACE);
95 ops = ieee802154_mlme_ops(dev);
96 phy = dev->ieee802154_ptr->wpan_phy;
98 get_device(&phy->dev);
101 short_addr = dev->ieee802154_ptr->short_addr;
102 pan_id = dev->ieee802154_ptr->pan_id;
105 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
106 nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
107 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
108 nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
110 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) ||
111 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, pan_id))
112 goto nla_put_failure;
114 if (ops->get_mac_params) {
115 struct ieee802154_mac_params params;
118 ops->get_mac_params(dev, ¶ms);
121 if (nla_put_s8(msg, IEEE802154_ATTR_TXPOWER,
122 params.transmit_power / 100) ||
123 nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, params.lbt) ||
124 nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE,
126 nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL,
127 params.cca_ed_level / 100) ||
128 nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES,
129 params.csma_retries) ||
130 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE,
132 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE,
134 nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES,
135 params.frame_retries))
136 goto nla_put_failure;
140 genlmsg_end(msg, hdr);
145 genlmsg_cancel(msg, hdr);
150 /* Requests from userspace */
151 static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
153 struct net_device *dev;
155 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
156 char name[IFNAMSIZ + 1];
158 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
160 dev = dev_get_by_name(&init_net, name);
161 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX]) {
162 dev = dev_get_by_index(&init_net,
163 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
171 if (dev->type != ARPHRD_IEEE802154) {
179 int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info)
181 struct net_device *dev;
182 struct ieee802154_addr addr;
184 int ret = -EOPNOTSUPP;
186 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
187 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
188 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
189 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
190 !info->attrs[IEEE802154_ATTR_CAPABILITY])
193 dev = ieee802154_nl_get_dev(info);
196 if (!ieee802154_mlme_ops(dev)->assoc_req)
199 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
200 addr.mode = IEEE802154_ADDR_LONG;
201 addr.extended_addr = nla_get_hwaddr(
202 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]);
204 addr.mode = IEEE802154_ADDR_SHORT;
205 addr.short_addr = nla_get_shortaddr(
206 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
208 addr.pan_id = nla_get_shortaddr(
209 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
211 if (info->attrs[IEEE802154_ATTR_PAGE])
212 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
216 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
217 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
219 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
226 int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info)
228 struct net_device *dev;
229 struct ieee802154_addr addr;
230 int ret = -EOPNOTSUPP;
232 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
233 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
234 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
237 dev = ieee802154_nl_get_dev(info);
240 if (!ieee802154_mlme_ops(dev)->assoc_resp)
243 addr.mode = IEEE802154_ADDR_LONG;
244 addr.extended_addr = nla_get_hwaddr(
245 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
247 addr.pan_id = dev->ieee802154_ptr->pan_id;
250 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
251 nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
252 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
259 int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info)
261 struct net_device *dev;
262 struct ieee802154_addr addr;
263 int ret = -EOPNOTSUPP;
265 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
266 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
267 !info->attrs[IEEE802154_ATTR_REASON])
270 dev = ieee802154_nl_get_dev(info);
273 if (!ieee802154_mlme_ops(dev)->disassoc_req)
276 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
277 addr.mode = IEEE802154_ADDR_LONG;
278 addr.extended_addr = nla_get_hwaddr(
279 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
281 addr.mode = IEEE802154_ADDR_SHORT;
282 addr.short_addr = nla_get_shortaddr(
283 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
286 addr.pan_id = dev->ieee802154_ptr->pan_id;
289 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
290 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
297 /* PANid, channel, beacon_order = 15, superframe_order = 15,
298 * PAN_coordinator, battery_life_extension = 0,
299 * coord_realignment = 0, security_enable = 0
301 int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
303 struct net_device *dev;
304 struct ieee802154_addr addr;
306 u8 channel, bcn_ord, sf_ord;
308 int pan_coord, blx, coord_realign;
311 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
312 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
313 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
314 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
315 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
316 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
317 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
318 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
322 dev = ieee802154_nl_get_dev(info);
326 if (netif_running(dev))
329 if (!ieee802154_mlme_ops(dev)->start_req) {
334 addr.mode = IEEE802154_ADDR_SHORT;
335 addr.short_addr = nla_get_shortaddr(
336 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
337 addr.pan_id = nla_get_shortaddr(
338 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
340 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
341 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
342 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
343 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
344 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
345 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
347 if (info->attrs[IEEE802154_ATTR_PAGE])
348 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
352 if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
353 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
359 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
360 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
363 /* FIXME: add validation for unused parameters to be sane
366 ieee802154_nl_start_confirm(dev, IEEE802154_SUCCESS);
373 int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
375 struct net_device *dev;
376 int ret = -EOPNOTSUPP;
382 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
383 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
384 !info->attrs[IEEE802154_ATTR_DURATION])
387 dev = ieee802154_nl_get_dev(info);
390 if (!ieee802154_mlme_ops(dev)->scan_req)
393 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
394 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
395 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
397 if (info->attrs[IEEE802154_ATTR_PAGE])
398 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
402 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
410 int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info)
412 /* Request for interface name, index, type, IEEE address,
413 * PAN Id, short address
416 struct net_device *dev = NULL;
419 pr_debug("%s\n", __func__);
421 dev = ieee802154_nl_get_dev(info);
425 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
429 rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq,
436 return genlmsg_reply(msg, info);
444 int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb)
446 struct net *net = sock_net(skb->sk);
447 struct net_device *dev;
449 int s_idx = cb->args[0];
451 pr_debug("%s\n", __func__);
454 for_each_netdev(net, dev) {
455 if (idx < s_idx || dev->type != ARPHRD_IEEE802154)
458 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid,
460 NLM_F_MULTI, dev) < 0)
470 int ieee802154_set_macparams(struct sk_buff *skb, struct genl_info *info)
472 struct net_device *dev = NULL;
473 struct ieee802154_mlme_ops *ops;
474 struct ieee802154_mac_params params;
475 struct wpan_phy *phy;
478 pr_debug("%s\n", __func__);
480 dev = ieee802154_nl_get_dev(info);
484 ops = ieee802154_mlme_ops(dev);
486 if (!ops->get_mac_params || !ops->set_mac_params) {
491 if (netif_running(dev)) {
496 if (!info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
497 !info->attrs[IEEE802154_ATTR_CCA_MODE] &&
498 !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] &&
499 !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] &&
500 !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] &&
501 !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] &&
502 !info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
505 phy = dev->ieee802154_ptr->wpan_phy;
506 get_device(&phy->dev);
509 ops->get_mac_params(dev, ¶ms);
511 if (info->attrs[IEEE802154_ATTR_TXPOWER])
512 params.transmit_power = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]) * 100;
514 if (info->attrs[IEEE802154_ATTR_LBT_ENABLED])
515 params.lbt = nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
517 if (info->attrs[IEEE802154_ATTR_CCA_MODE])
518 params.cca.mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
520 if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])
521 params.cca_ed_level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]) * 100;
523 if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES])
524 params.csma_retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]);
526 if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE])
527 params.min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]);
529 if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE])
530 params.max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]);
532 if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
533 params.frame_retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]);
535 rc = ops->set_mac_params(dev, ¶ms);
549 ieee802154_llsec_parse_key_id(struct genl_info *info,
550 struct ieee802154_llsec_key_id *desc)
552 memset(desc, 0, sizeof(*desc));
554 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE])
557 desc->mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]);
559 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
560 if (!info->attrs[IEEE802154_ATTR_PAN_ID] &&
561 !(info->attrs[IEEE802154_ATTR_SHORT_ADDR] ||
562 info->attrs[IEEE802154_ATTR_HW_ADDR]))
565 desc->device_addr.pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
567 if (info->attrs[IEEE802154_ATTR_SHORT_ADDR]) {
568 desc->device_addr.mode = IEEE802154_ADDR_SHORT;
569 desc->device_addr.short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
571 desc->device_addr.mode = IEEE802154_ADDR_LONG;
572 desc->device_addr.extended_addr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
576 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
577 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID])
580 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
581 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT])
584 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
585 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED])
588 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT)
589 desc->id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]);
591 switch (desc->mode) {
592 case IEEE802154_SCF_KEY_SHORT_INDEX:
594 u32 source = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]);
596 desc->short_source = cpu_to_le32(source);
599 case IEEE802154_SCF_KEY_HW_INDEX:
600 desc->extended_source = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]);
608 ieee802154_llsec_fill_key_id(struct sk_buff *msg,
609 const struct ieee802154_llsec_key_id *desc)
611 if (nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_MODE, desc->mode))
614 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
615 if (nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID,
616 desc->device_addr.pan_id))
619 if (desc->device_addr.mode == IEEE802154_ADDR_SHORT &&
620 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
621 desc->device_addr.short_addr))
624 if (desc->device_addr.mode == IEEE802154_ADDR_LONG &&
625 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR,
626 desc->device_addr.extended_addr))
630 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
631 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_ID, desc->id))
634 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
635 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT,
636 le32_to_cpu(desc->short_source)))
639 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
640 nla_put_hwaddr(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED,
641 desc->extended_source))
647 int ieee802154_llsec_getparams(struct sk_buff *skb, struct genl_info *info)
650 struct net_device *dev = NULL;
652 struct ieee802154_mlme_ops *ops;
654 struct ieee802154_llsec_params params;
656 pr_debug("%s\n", __func__);
658 dev = ieee802154_nl_get_dev(info);
662 ops = ieee802154_mlme_ops(dev);
668 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
672 hdr = genlmsg_put(msg, 0, info->snd_seq, &nl802154_family, 0,
673 IEEE802154_LLSEC_GETPARAMS);
677 rc = ops->llsec->get_params(dev, ¶ms);
681 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
682 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
683 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_ENABLED, params.enabled) ||
684 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVEL, params.out_level) ||
685 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
686 be32_to_cpu(params.frame_counter)) ||
687 ieee802154_llsec_fill_key_id(msg, ¶ms.out_key))
692 return ieee802154_nl_reply(msg, info);
700 int ieee802154_llsec_setparams(struct sk_buff *skb, struct genl_info *info)
702 struct net_device *dev = NULL;
704 struct ieee802154_mlme_ops *ops;
705 struct ieee802154_llsec_params params;
708 pr_debug("%s\n", __func__);
710 dev = ieee802154_nl_get_dev(info);
714 if (!info->attrs[IEEE802154_ATTR_LLSEC_ENABLED] &&
715 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE] &&
716 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL])
719 ops = ieee802154_mlme_ops(dev);
725 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL] &&
726 nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) > 7)
729 if (info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]) {
730 params.enabled = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]);
731 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
734 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) {
735 if (ieee802154_llsec_parse_key_id(info, ¶ms.out_key))
738 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
741 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) {
742 params.out_level = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]);
743 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
746 if (info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]) {
747 u32 fc = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
749 params.frame_counter = cpu_to_be32(fc);
750 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
753 rc = ops->llsec->set_params(dev, ¶ms, changed);
763 struct llsec_dump_data {
768 struct net_device *dev;
769 struct ieee802154_mlme_ops *ops;
770 struct ieee802154_llsec_table *table;
774 ieee802154_llsec_dump_table(struct sk_buff *skb, struct netlink_callback *cb,
775 int (*step)(struct llsec_dump_data *))
777 struct net *net = sock_net(skb->sk);
778 struct net_device *dev;
779 struct llsec_dump_data data;
781 int first_dev = cb->args[0];
784 for_each_netdev(net, dev) {
785 if (idx < first_dev || dev->type != ARPHRD_IEEE802154)
788 data.ops = ieee802154_mlme_ops(dev);
789 if (!data.ops->llsec)
793 data.s_idx = cb->args[1];
794 data.s_idx2 = cb->args[2];
796 data.portid = NETLINK_CB(cb->skb).portid;
797 data.nlmsg_seq = cb->nlh->nlmsg_seq;
799 data.ops->llsec->lock_table(dev);
800 data.ops->llsec->get_table(data.dev, &data.table);
802 data.ops->llsec->unlock_table(dev);
816 ieee802154_nl_llsec_change(struct sk_buff *skb, struct genl_info *info,
817 int (*fn)(struct net_device*, struct genl_info*))
819 struct net_device *dev = NULL;
822 dev = ieee802154_nl_get_dev(info);
826 if (!ieee802154_mlme_ops(dev)->llsec)
836 ieee802154_llsec_parse_key(struct genl_info *info,
837 struct ieee802154_llsec_key *key)
840 u32 commands[256 / 32];
842 memset(key, 0, sizeof(*key));
844 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES] ||
845 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES])
848 frames = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES]);
849 if ((frames & BIT(IEEE802154_FC_TYPE_MAC_CMD)) &&
850 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS])
853 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) {
855 info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS],
858 if (commands[0] || commands[1] || commands[2] || commands[3] ||
859 commands[4] || commands[5] || commands[6] ||
860 commands[7] >= BIT(IEEE802154_CMD_GTS_REQ + 1))
863 key->cmd_frame_ids = commands[7];
866 key->frame_types = frames;
868 nla_memcpy(key->key, info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES],
869 IEEE802154_LLSEC_KEY_SIZE);
874 static int llsec_add_key(struct net_device *dev, struct genl_info *info)
876 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
877 struct ieee802154_llsec_key key;
878 struct ieee802154_llsec_key_id id;
880 if (ieee802154_llsec_parse_key(info, &key) ||
881 ieee802154_llsec_parse_key_id(info, &id))
884 return ops->llsec->add_key(dev, &id, &key);
887 int ieee802154_llsec_add_key(struct sk_buff *skb, struct genl_info *info)
889 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
890 (NLM_F_CREATE | NLM_F_EXCL))
893 return ieee802154_nl_llsec_change(skb, info, llsec_add_key);
896 static int llsec_remove_key(struct net_device *dev, struct genl_info *info)
898 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
899 struct ieee802154_llsec_key_id id;
901 if (ieee802154_llsec_parse_key_id(info, &id))
904 return ops->llsec->del_key(dev, &id);
907 int ieee802154_llsec_del_key(struct sk_buff *skb, struct genl_info *info)
909 return ieee802154_nl_llsec_change(skb, info, llsec_remove_key);
913 ieee802154_nl_fill_key(struct sk_buff *msg, u32 portid, u32 seq,
914 const struct ieee802154_llsec_key_entry *key,
915 const struct net_device *dev)
918 u32 commands[256 / 32];
920 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
921 IEEE802154_LLSEC_LIST_KEY);
925 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
926 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
927 ieee802154_llsec_fill_key_id(msg, &key->id) ||
928 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES,
929 key->key->frame_types))
930 goto nla_put_failure;
932 if (key->key->frame_types & BIT(IEEE802154_FC_TYPE_MAC_CMD)) {
933 memset(commands, 0, sizeof(commands));
934 commands[7] = key->key->cmd_frame_ids;
935 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS,
936 sizeof(commands), commands))
937 goto nla_put_failure;
940 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_BYTES,
941 IEEE802154_LLSEC_KEY_SIZE, key->key->key))
942 goto nla_put_failure;
944 genlmsg_end(msg, hdr);
948 genlmsg_cancel(msg, hdr);
953 static int llsec_iter_keys(struct llsec_dump_data *data)
955 struct ieee802154_llsec_key_entry *pos;
958 list_for_each_entry(pos, &data->table->keys, list) {
959 if (idx++ < data->s_idx)
962 if (ieee802154_nl_fill_key(data->skb, data->portid,
963 data->nlmsg_seq, pos, data->dev)) {
974 int ieee802154_llsec_dump_keys(struct sk_buff *skb, struct netlink_callback *cb)
976 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_keys);
980 llsec_parse_dev(struct genl_info *info,
981 struct ieee802154_llsec_device *dev)
983 memset(dev, 0, sizeof(*dev));
985 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
986 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
987 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE] ||
988 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE] ||
989 (!!info->attrs[IEEE802154_ATTR_PAN_ID] !=
990 !!info->attrs[IEEE802154_ATTR_SHORT_ADDR]))
993 if (info->attrs[IEEE802154_ATTR_PAN_ID]) {
994 dev->pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
995 dev->short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
997 dev->short_addr = cpu_to_le16(IEEE802154_ADDR_UNDEF);
1000 dev->hwaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1001 dev->frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1002 dev->seclevel_exempt = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1003 dev->key_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE]);
1005 if (dev->key_mode >= __IEEE802154_LLSEC_DEVKEY_MAX)
1011 static int llsec_add_dev(struct net_device *dev, struct genl_info *info)
1013 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1014 struct ieee802154_llsec_device desc;
1016 if (llsec_parse_dev(info, &desc))
1019 return ops->llsec->add_dev(dev, &desc);
1022 int ieee802154_llsec_add_dev(struct sk_buff *skb, struct genl_info *info)
1024 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1025 (NLM_F_CREATE | NLM_F_EXCL))
1028 return ieee802154_nl_llsec_change(skb, info, llsec_add_dev);
1031 static int llsec_del_dev(struct net_device *dev, struct genl_info *info)
1033 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1036 if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
1039 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1041 return ops->llsec->del_dev(dev, devaddr);
1044 int ieee802154_llsec_del_dev(struct sk_buff *skb, struct genl_info *info)
1046 return ieee802154_nl_llsec_change(skb, info, llsec_del_dev);
1050 ieee802154_nl_fill_dev(struct sk_buff *msg, u32 portid, u32 seq,
1051 const struct ieee802154_llsec_device *desc,
1052 const struct net_device *dev)
1056 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1057 IEEE802154_LLSEC_LIST_DEV);
1061 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1062 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1063 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, desc->pan_id) ||
1064 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
1065 desc->short_addr) ||
1066 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, desc->hwaddr) ||
1067 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1068 desc->frame_counter) ||
1069 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1070 desc->seclevel_exempt) ||
1071 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE, desc->key_mode))
1072 goto nla_put_failure;
1074 genlmsg_end(msg, hdr);
1078 genlmsg_cancel(msg, hdr);
1083 static int llsec_iter_devs(struct llsec_dump_data *data)
1085 struct ieee802154_llsec_device *pos;
1086 int rc = 0, idx = 0;
1088 list_for_each_entry(pos, &data->table->devices, list) {
1089 if (idx++ < data->s_idx)
1092 if (ieee802154_nl_fill_dev(data->skb, data->portid,
1093 data->nlmsg_seq, pos, data->dev)) {
1104 int ieee802154_llsec_dump_devs(struct sk_buff *skb, struct netlink_callback *cb)
1106 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devs);
1109 static int llsec_add_devkey(struct net_device *dev, struct genl_info *info)
1111 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1112 struct ieee802154_llsec_device_key key;
1115 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
1116 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1117 ieee802154_llsec_parse_key_id(info, &key.key_id))
1120 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1121 key.frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1123 return ops->llsec->add_devkey(dev, devaddr, &key);
1126 int ieee802154_llsec_add_devkey(struct sk_buff *skb, struct genl_info *info)
1128 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1129 (NLM_F_CREATE | NLM_F_EXCL))
1132 return ieee802154_nl_llsec_change(skb, info, llsec_add_devkey);
1135 static int llsec_del_devkey(struct net_device *dev, struct genl_info *info)
1137 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1138 struct ieee802154_llsec_device_key key;
1141 if (!info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1142 ieee802154_llsec_parse_key_id(info, &key.key_id))
1145 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1147 return ops->llsec->del_devkey(dev, devaddr, &key);
1150 int ieee802154_llsec_del_devkey(struct sk_buff *skb, struct genl_info *info)
1152 return ieee802154_nl_llsec_change(skb, info, llsec_del_devkey);
1156 ieee802154_nl_fill_devkey(struct sk_buff *msg, u32 portid, u32 seq,
1158 const struct ieee802154_llsec_device_key *devkey,
1159 const struct net_device *dev)
1163 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1164 IEEE802154_LLSEC_LIST_DEVKEY);
1168 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1169 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1170 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, devaddr) ||
1171 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1172 devkey->frame_counter) ||
1173 ieee802154_llsec_fill_key_id(msg, &devkey->key_id))
1174 goto nla_put_failure;
1176 genlmsg_end(msg, hdr);
1180 genlmsg_cancel(msg, hdr);
1185 static int llsec_iter_devkeys(struct llsec_dump_data *data)
1187 struct ieee802154_llsec_device *dpos;
1188 struct ieee802154_llsec_device_key *kpos;
1189 int rc = 0, idx = 0, idx2;
1191 list_for_each_entry(dpos, &data->table->devices, list) {
1192 if (idx++ < data->s_idx)
1197 list_for_each_entry(kpos, &dpos->keys, list) {
1198 if (idx2++ < data->s_idx2)
1201 if (ieee802154_nl_fill_devkey(data->skb, data->portid,
1205 return rc = -EMSGSIZE;
1217 int ieee802154_llsec_dump_devkeys(struct sk_buff *skb,
1218 struct netlink_callback *cb)
1220 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devkeys);
1224 llsec_parse_seclevel(struct genl_info *info,
1225 struct ieee802154_llsec_seclevel *sl)
1227 memset(sl, 0, sizeof(*sl));
1229 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE] ||
1230 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS] ||
1231 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE])
1234 sl->frame_type = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE]);
1235 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD) {
1236 if (!info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID])
1239 sl->cmd_frame_id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]);
1242 sl->sec_levels = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS]);
1243 sl->device_override = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1248 static int llsec_add_seclevel(struct net_device *dev, struct genl_info *info)
1250 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1251 struct ieee802154_llsec_seclevel sl;
1253 if (llsec_parse_seclevel(info, &sl))
1256 return ops->llsec->add_seclevel(dev, &sl);
1259 int ieee802154_llsec_add_seclevel(struct sk_buff *skb, struct genl_info *info)
1261 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1262 (NLM_F_CREATE | NLM_F_EXCL))
1265 return ieee802154_nl_llsec_change(skb, info, llsec_add_seclevel);
1268 static int llsec_del_seclevel(struct net_device *dev, struct genl_info *info)
1270 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1271 struct ieee802154_llsec_seclevel sl;
1273 if (llsec_parse_seclevel(info, &sl))
1276 return ops->llsec->del_seclevel(dev, &sl);
1279 int ieee802154_llsec_del_seclevel(struct sk_buff *skb, struct genl_info *info)
1281 return ieee802154_nl_llsec_change(skb, info, llsec_del_seclevel);
1285 ieee802154_nl_fill_seclevel(struct sk_buff *msg, u32 portid, u32 seq,
1286 const struct ieee802154_llsec_seclevel *sl,
1287 const struct net_device *dev)
1291 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1292 IEEE802154_LLSEC_LIST_SECLEVEL);
1296 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1297 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1298 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_FRAME_TYPE, sl->frame_type) ||
1299 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVELS, sl->sec_levels) ||
1300 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1301 sl->device_override))
1302 goto nla_put_failure;
1304 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD &&
1305 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID,
1307 goto nla_put_failure;
1309 genlmsg_end(msg, hdr);
1313 genlmsg_cancel(msg, hdr);
1318 static int llsec_iter_seclevels(struct llsec_dump_data *data)
1320 struct ieee802154_llsec_seclevel *pos;
1321 int rc = 0, idx = 0;
1323 list_for_each_entry(pos, &data->table->security_levels, list) {
1324 if (idx++ < data->s_idx)
1327 if (ieee802154_nl_fill_seclevel(data->skb, data->portid,
1328 data->nlmsg_seq, pos,
1340 int ieee802154_llsec_dump_seclevels(struct sk_buff *skb,
1341 struct netlink_callback *cb)
1343 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_seclevels);