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);
100 short_addr = ops->get_short_addr(dev);
101 pan_id = ops->get_pan_id(dev);
103 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
104 nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
105 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
106 nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
108 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) ||
109 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, pan_id))
110 goto nla_put_failure;
112 if (ops->get_mac_params) {
113 struct ieee802154_mac_params params;
116 ops->get_mac_params(dev, ¶ms);
119 if (nla_put_s8(msg, IEEE802154_ATTR_TXPOWER,
120 params.transmit_power) ||
121 nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, params.lbt) ||
122 nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE,
124 nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL,
125 params.cca_ed_level) ||
126 nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES,
127 params.csma_retries) ||
128 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE,
130 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE,
132 nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES,
133 params.frame_retries))
134 goto nla_put_failure;
138 genlmsg_end(msg, hdr);
143 genlmsg_cancel(msg, hdr);
148 /* Requests from userspace */
149 static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
151 struct net_device *dev;
153 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
154 char name[IFNAMSIZ + 1];
156 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
158 dev = dev_get_by_name(&init_net, name);
159 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX]) {
160 dev = dev_get_by_index(&init_net,
161 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
169 /* Check on mtu is currently a hacked solution because lowpan
170 * and wpan have the same ARPHRD type.
172 if (dev->type != ARPHRD_IEEE802154 || dev->mtu != IEEE802154_MTU) {
180 int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info)
182 struct net_device *dev;
183 struct ieee802154_addr addr;
185 int ret = -EOPNOTSUPP;
187 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
188 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
189 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
190 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
191 !info->attrs[IEEE802154_ATTR_CAPABILITY])
194 dev = ieee802154_nl_get_dev(info);
197 if (!ieee802154_mlme_ops(dev)->assoc_req)
200 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
201 addr.mode = IEEE802154_ADDR_LONG;
202 addr.extended_addr = nla_get_hwaddr(
203 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]);
205 addr.mode = IEEE802154_ADDR_SHORT;
206 addr.short_addr = nla_get_shortaddr(
207 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
209 addr.pan_id = nla_get_shortaddr(
210 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
212 if (info->attrs[IEEE802154_ATTR_PAGE])
213 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
217 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
218 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
220 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
227 int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info)
229 struct net_device *dev;
230 struct ieee802154_addr addr;
231 int ret = -EOPNOTSUPP;
233 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
234 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
235 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
238 dev = ieee802154_nl_get_dev(info);
241 if (!ieee802154_mlme_ops(dev)->assoc_resp)
244 addr.mode = IEEE802154_ADDR_LONG;
245 addr.extended_addr = nla_get_hwaddr(
246 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
247 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
249 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
250 nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
251 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
258 int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info)
260 struct net_device *dev;
261 struct ieee802154_addr addr;
262 int ret = -EOPNOTSUPP;
264 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
265 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
266 !info->attrs[IEEE802154_ATTR_REASON])
269 dev = ieee802154_nl_get_dev(info);
272 if (!ieee802154_mlme_ops(dev)->disassoc_req)
275 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
276 addr.mode = IEEE802154_ADDR_LONG;
277 addr.extended_addr = nla_get_hwaddr(
278 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
280 addr.mode = IEEE802154_ADDR_SHORT;
281 addr.short_addr = nla_get_shortaddr(
282 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
284 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
286 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
287 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
294 /* PANid, channel, beacon_order = 15, superframe_order = 15,
295 * PAN_coordinator, battery_life_extension = 0,
296 * coord_realignment = 0, security_enable = 0
298 int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
300 struct net_device *dev;
301 struct ieee802154_addr addr;
303 u8 channel, bcn_ord, sf_ord;
305 int pan_coord, blx, coord_realign;
308 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
309 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
310 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
311 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
312 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
313 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
314 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
315 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
319 dev = ieee802154_nl_get_dev(info);
323 if (netif_running(dev))
326 if (!ieee802154_mlme_ops(dev)->start_req) {
331 addr.mode = IEEE802154_ADDR_SHORT;
332 addr.short_addr = nla_get_shortaddr(
333 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
334 addr.pan_id = nla_get_shortaddr(
335 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
337 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
338 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
339 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
340 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
341 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
342 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
344 if (info->attrs[IEEE802154_ATTR_PAGE])
345 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
349 if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
350 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
356 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
357 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
360 /* FIXME: add validation for unused parameters to be sane
363 ieee802154_nl_start_confirm(dev, IEEE802154_SUCCESS);
370 int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
372 struct net_device *dev;
373 int ret = -EOPNOTSUPP;
379 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
380 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
381 !info->attrs[IEEE802154_ATTR_DURATION])
384 dev = ieee802154_nl_get_dev(info);
387 if (!ieee802154_mlme_ops(dev)->scan_req)
390 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
391 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
392 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
394 if (info->attrs[IEEE802154_ATTR_PAGE])
395 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
399 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
407 int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info)
409 /* Request for interface name, index, type, IEEE address,
410 * PAN Id, short address
413 struct net_device *dev = NULL;
416 pr_debug("%s\n", __func__);
418 dev = ieee802154_nl_get_dev(info);
422 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
426 rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq,
433 return genlmsg_reply(msg, info);
441 int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb)
443 struct net *net = sock_net(skb->sk);
444 struct net_device *dev;
446 int s_idx = cb->args[0];
448 pr_debug("%s\n", __func__);
451 for_each_netdev(net, dev) {
452 /* Check on mtu is currently a hacked solution because lowpan
453 * and wpan have the same ARPHRD type.
455 if (idx < s_idx || dev->type != ARPHRD_IEEE802154 ||
456 dev->mtu != IEEE802154_MTU)
459 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid,
461 NLM_F_MULTI, dev) < 0)
471 int ieee802154_set_macparams(struct sk_buff *skb, struct genl_info *info)
473 struct net_device *dev = NULL;
474 struct ieee802154_mlme_ops *ops;
475 struct ieee802154_mac_params params;
476 struct wpan_phy *phy;
479 pr_debug("%s\n", __func__);
481 dev = ieee802154_nl_get_dev(info);
485 ops = ieee802154_mlme_ops(dev);
487 if (!ops->get_mac_params || !ops->set_mac_params) {
492 if (netif_running(dev)) {
497 if (!info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
498 !info->attrs[IEEE802154_ATTR_CCA_MODE] &&
499 !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] &&
500 !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] &&
501 !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] &&
502 !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] &&
503 !info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
506 phy = dev->ieee802154_ptr->wpan_phy;
507 get_device(&phy->dev);
510 ops->get_mac_params(dev, ¶ms);
512 if (info->attrs[IEEE802154_ATTR_TXPOWER])
513 params.transmit_power = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]);
515 if (info->attrs[IEEE802154_ATTR_LBT_ENABLED])
516 params.lbt = nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
518 if (info->attrs[IEEE802154_ATTR_CCA_MODE])
519 params.cca.mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
521 if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])
522 params.cca_ed_level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]);
524 if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES])
525 params.csma_retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]);
527 if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE])
528 params.min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]);
530 if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE])
531 params.max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]);
533 if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
534 params.frame_retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]);
536 rc = ops->set_mac_params(dev, ¶ms);
550 ieee802154_llsec_parse_key_id(struct genl_info *info,
551 struct ieee802154_llsec_key_id *desc)
553 memset(desc, 0, sizeof(*desc));
555 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE])
558 desc->mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]);
560 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
561 if (!info->attrs[IEEE802154_ATTR_PAN_ID] &&
562 !(info->attrs[IEEE802154_ATTR_SHORT_ADDR] ||
563 info->attrs[IEEE802154_ATTR_HW_ADDR]))
566 desc->device_addr.pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
568 if (info->attrs[IEEE802154_ATTR_SHORT_ADDR]) {
569 desc->device_addr.mode = IEEE802154_ADDR_SHORT;
570 desc->device_addr.short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
572 desc->device_addr.mode = IEEE802154_ADDR_LONG;
573 desc->device_addr.extended_addr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
577 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
578 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID])
581 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
582 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT])
585 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
586 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED])
589 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT)
590 desc->id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]);
592 switch (desc->mode) {
593 case IEEE802154_SCF_KEY_SHORT_INDEX:
595 u32 source = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]);
597 desc->short_source = cpu_to_le32(source);
600 case IEEE802154_SCF_KEY_HW_INDEX:
601 desc->extended_source = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]);
609 ieee802154_llsec_fill_key_id(struct sk_buff *msg,
610 const struct ieee802154_llsec_key_id *desc)
612 if (nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_MODE, desc->mode))
615 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
616 if (nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID,
617 desc->device_addr.pan_id))
620 if (desc->device_addr.mode == IEEE802154_ADDR_SHORT &&
621 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
622 desc->device_addr.short_addr))
625 if (desc->device_addr.mode == IEEE802154_ADDR_LONG &&
626 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR,
627 desc->device_addr.extended_addr))
631 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
632 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_ID, desc->id))
635 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
636 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT,
637 le32_to_cpu(desc->short_source)))
640 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
641 nla_put_hwaddr(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED,
642 desc->extended_source))
648 int ieee802154_llsec_getparams(struct sk_buff *skb, struct genl_info *info)
651 struct net_device *dev = NULL;
653 struct ieee802154_mlme_ops *ops;
655 struct ieee802154_llsec_params params;
657 pr_debug("%s\n", __func__);
659 dev = ieee802154_nl_get_dev(info);
663 ops = ieee802154_mlme_ops(dev);
669 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
673 hdr = genlmsg_put(msg, 0, info->snd_seq, &nl802154_family, 0,
674 IEEE802154_LLSEC_GETPARAMS);
678 rc = ops->llsec->get_params(dev, ¶ms);
682 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
683 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
684 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_ENABLED, params.enabled) ||
685 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVEL, params.out_level) ||
686 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
687 be32_to_cpu(params.frame_counter)) ||
688 ieee802154_llsec_fill_key_id(msg, ¶ms.out_key))
693 return ieee802154_nl_reply(msg, info);
701 int ieee802154_llsec_setparams(struct sk_buff *skb, struct genl_info *info)
703 struct net_device *dev = NULL;
705 struct ieee802154_mlme_ops *ops;
706 struct ieee802154_llsec_params params;
709 pr_debug("%s\n", __func__);
711 dev = ieee802154_nl_get_dev(info);
715 if (!info->attrs[IEEE802154_ATTR_LLSEC_ENABLED] &&
716 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE] &&
717 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL])
720 ops = ieee802154_mlme_ops(dev);
726 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL] &&
727 nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) > 7)
730 if (info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]) {
731 params.enabled = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]);
732 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
735 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) {
736 if (ieee802154_llsec_parse_key_id(info, ¶ms.out_key))
739 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
742 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) {
743 params.out_level = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]);
744 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
747 if (info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]) {
748 u32 fc = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
750 params.frame_counter = cpu_to_be32(fc);
751 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
754 rc = ops->llsec->set_params(dev, ¶ms, changed);
764 struct llsec_dump_data {
769 struct net_device *dev;
770 struct ieee802154_mlme_ops *ops;
771 struct ieee802154_llsec_table *table;
775 ieee802154_llsec_dump_table(struct sk_buff *skb, struct netlink_callback *cb,
776 int (*step)(struct llsec_dump_data *))
778 struct net *net = sock_net(skb->sk);
779 struct net_device *dev;
780 struct llsec_dump_data data;
782 int first_dev = cb->args[0];
785 for_each_netdev(net, dev) {
786 /* Check on mtu is currently a hacked solution because lowpan
787 * and wpan have the same ARPHRD type.
789 if (idx < first_dev || dev->type != ARPHRD_IEEE802154 ||
790 dev->mtu != IEEE802154_MTU)
793 data.ops = ieee802154_mlme_ops(dev);
794 if (!data.ops->llsec)
798 data.s_idx = cb->args[1];
799 data.s_idx2 = cb->args[2];
801 data.portid = NETLINK_CB(cb->skb).portid;
802 data.nlmsg_seq = cb->nlh->nlmsg_seq;
804 data.ops->llsec->lock_table(dev);
805 data.ops->llsec->get_table(data.dev, &data.table);
807 data.ops->llsec->unlock_table(dev);
821 ieee802154_nl_llsec_change(struct sk_buff *skb, struct genl_info *info,
822 int (*fn)(struct net_device*, struct genl_info*))
824 struct net_device *dev = NULL;
827 dev = ieee802154_nl_get_dev(info);
831 if (!ieee802154_mlme_ops(dev)->llsec)
841 ieee802154_llsec_parse_key(struct genl_info *info,
842 struct ieee802154_llsec_key *key)
845 u32 commands[256 / 32];
847 memset(key, 0, sizeof(*key));
849 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES] ||
850 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES])
853 frames = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES]);
854 if ((frames & BIT(IEEE802154_FC_TYPE_MAC_CMD)) &&
855 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS])
858 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) {
860 info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS],
863 if (commands[0] || commands[1] || commands[2] || commands[3] ||
864 commands[4] || commands[5] || commands[6] ||
865 commands[7] >= BIT(IEEE802154_CMD_GTS_REQ + 1))
868 key->cmd_frame_ids = commands[7];
871 key->frame_types = frames;
873 nla_memcpy(key->key, info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES],
874 IEEE802154_LLSEC_KEY_SIZE);
879 static int llsec_add_key(struct net_device *dev, struct genl_info *info)
881 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
882 struct ieee802154_llsec_key key;
883 struct ieee802154_llsec_key_id id;
885 if (ieee802154_llsec_parse_key(info, &key) ||
886 ieee802154_llsec_parse_key_id(info, &id))
889 return ops->llsec->add_key(dev, &id, &key);
892 int ieee802154_llsec_add_key(struct sk_buff *skb, struct genl_info *info)
894 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
895 (NLM_F_CREATE | NLM_F_EXCL))
898 return ieee802154_nl_llsec_change(skb, info, llsec_add_key);
901 static int llsec_remove_key(struct net_device *dev, struct genl_info *info)
903 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
904 struct ieee802154_llsec_key_id id;
906 if (ieee802154_llsec_parse_key_id(info, &id))
909 return ops->llsec->del_key(dev, &id);
912 int ieee802154_llsec_del_key(struct sk_buff *skb, struct genl_info *info)
914 return ieee802154_nl_llsec_change(skb, info, llsec_remove_key);
918 ieee802154_nl_fill_key(struct sk_buff *msg, u32 portid, u32 seq,
919 const struct ieee802154_llsec_key_entry *key,
920 const struct net_device *dev)
923 u32 commands[256 / 32];
925 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
926 IEEE802154_LLSEC_LIST_KEY);
930 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
931 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
932 ieee802154_llsec_fill_key_id(msg, &key->id) ||
933 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES,
934 key->key->frame_types))
935 goto nla_put_failure;
937 if (key->key->frame_types & BIT(IEEE802154_FC_TYPE_MAC_CMD)) {
938 memset(commands, 0, sizeof(commands));
939 commands[7] = key->key->cmd_frame_ids;
940 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS,
941 sizeof(commands), commands))
942 goto nla_put_failure;
945 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_BYTES,
946 IEEE802154_LLSEC_KEY_SIZE, key->key->key))
947 goto nla_put_failure;
949 genlmsg_end(msg, hdr);
953 genlmsg_cancel(msg, hdr);
958 static int llsec_iter_keys(struct llsec_dump_data *data)
960 struct ieee802154_llsec_key_entry *pos;
963 list_for_each_entry(pos, &data->table->keys, list) {
964 if (idx++ < data->s_idx)
967 if (ieee802154_nl_fill_key(data->skb, data->portid,
968 data->nlmsg_seq, pos, data->dev)) {
979 int ieee802154_llsec_dump_keys(struct sk_buff *skb, struct netlink_callback *cb)
981 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_keys);
985 llsec_parse_dev(struct genl_info *info,
986 struct ieee802154_llsec_device *dev)
988 memset(dev, 0, sizeof(*dev));
990 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
991 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
992 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE] ||
993 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE] ||
994 (!!info->attrs[IEEE802154_ATTR_PAN_ID] !=
995 !!info->attrs[IEEE802154_ATTR_SHORT_ADDR]))
998 if (info->attrs[IEEE802154_ATTR_PAN_ID]) {
999 dev->pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
1000 dev->short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
1002 dev->short_addr = cpu_to_le16(IEEE802154_ADDR_UNDEF);
1005 dev->hwaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1006 dev->frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1007 dev->seclevel_exempt = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1008 dev->key_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE]);
1010 if (dev->key_mode >= __IEEE802154_LLSEC_DEVKEY_MAX)
1016 static int llsec_add_dev(struct net_device *dev, struct genl_info *info)
1018 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1019 struct ieee802154_llsec_device desc;
1021 if (llsec_parse_dev(info, &desc))
1024 return ops->llsec->add_dev(dev, &desc);
1027 int ieee802154_llsec_add_dev(struct sk_buff *skb, struct genl_info *info)
1029 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1030 (NLM_F_CREATE | NLM_F_EXCL))
1033 return ieee802154_nl_llsec_change(skb, info, llsec_add_dev);
1036 static int llsec_del_dev(struct net_device *dev, struct genl_info *info)
1038 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1041 if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
1044 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1046 return ops->llsec->del_dev(dev, devaddr);
1049 int ieee802154_llsec_del_dev(struct sk_buff *skb, struct genl_info *info)
1051 return ieee802154_nl_llsec_change(skb, info, llsec_del_dev);
1055 ieee802154_nl_fill_dev(struct sk_buff *msg, u32 portid, u32 seq,
1056 const struct ieee802154_llsec_device *desc,
1057 const struct net_device *dev)
1061 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1062 IEEE802154_LLSEC_LIST_DEV);
1066 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1067 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1068 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, desc->pan_id) ||
1069 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
1070 desc->short_addr) ||
1071 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, desc->hwaddr) ||
1072 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1073 desc->frame_counter) ||
1074 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1075 desc->seclevel_exempt) ||
1076 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE, desc->key_mode))
1077 goto nla_put_failure;
1079 genlmsg_end(msg, hdr);
1083 genlmsg_cancel(msg, hdr);
1088 static int llsec_iter_devs(struct llsec_dump_data *data)
1090 struct ieee802154_llsec_device *pos;
1091 int rc = 0, idx = 0;
1093 list_for_each_entry(pos, &data->table->devices, list) {
1094 if (idx++ < data->s_idx)
1097 if (ieee802154_nl_fill_dev(data->skb, data->portid,
1098 data->nlmsg_seq, pos, data->dev)) {
1109 int ieee802154_llsec_dump_devs(struct sk_buff *skb, struct netlink_callback *cb)
1111 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devs);
1114 static int llsec_add_devkey(struct net_device *dev, struct genl_info *info)
1116 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1117 struct ieee802154_llsec_device_key key;
1120 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
1121 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1122 ieee802154_llsec_parse_key_id(info, &key.key_id))
1125 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1126 key.frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1128 return ops->llsec->add_devkey(dev, devaddr, &key);
1131 int ieee802154_llsec_add_devkey(struct sk_buff *skb, struct genl_info *info)
1133 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1134 (NLM_F_CREATE | NLM_F_EXCL))
1137 return ieee802154_nl_llsec_change(skb, info, llsec_add_devkey);
1140 static int llsec_del_devkey(struct net_device *dev, struct genl_info *info)
1142 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1143 struct ieee802154_llsec_device_key key;
1146 if (!info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1147 ieee802154_llsec_parse_key_id(info, &key.key_id))
1150 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1152 return ops->llsec->del_devkey(dev, devaddr, &key);
1155 int ieee802154_llsec_del_devkey(struct sk_buff *skb, struct genl_info *info)
1157 return ieee802154_nl_llsec_change(skb, info, llsec_del_devkey);
1161 ieee802154_nl_fill_devkey(struct sk_buff *msg, u32 portid, u32 seq,
1163 const struct ieee802154_llsec_device_key *devkey,
1164 const struct net_device *dev)
1168 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1169 IEEE802154_LLSEC_LIST_DEVKEY);
1173 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1174 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1175 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, devaddr) ||
1176 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1177 devkey->frame_counter) ||
1178 ieee802154_llsec_fill_key_id(msg, &devkey->key_id))
1179 goto nla_put_failure;
1181 genlmsg_end(msg, hdr);
1185 genlmsg_cancel(msg, hdr);
1190 static int llsec_iter_devkeys(struct llsec_dump_data *data)
1192 struct ieee802154_llsec_device *dpos;
1193 struct ieee802154_llsec_device_key *kpos;
1194 int rc = 0, idx = 0, idx2;
1196 list_for_each_entry(dpos, &data->table->devices, list) {
1197 if (idx++ < data->s_idx)
1202 list_for_each_entry(kpos, &dpos->keys, list) {
1203 if (idx2++ < data->s_idx2)
1206 if (ieee802154_nl_fill_devkey(data->skb, data->portid,
1210 return rc = -EMSGSIZE;
1222 int ieee802154_llsec_dump_devkeys(struct sk_buff *skb,
1223 struct netlink_callback *cb)
1225 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devkeys);
1229 llsec_parse_seclevel(struct genl_info *info,
1230 struct ieee802154_llsec_seclevel *sl)
1232 memset(sl, 0, sizeof(*sl));
1234 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE] ||
1235 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS] ||
1236 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE])
1239 sl->frame_type = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE]);
1240 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD) {
1241 if (!info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID])
1244 sl->cmd_frame_id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]);
1247 sl->sec_levels = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS]);
1248 sl->device_override = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1253 static int llsec_add_seclevel(struct net_device *dev, struct genl_info *info)
1255 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1256 struct ieee802154_llsec_seclevel sl;
1258 if (llsec_parse_seclevel(info, &sl))
1261 return ops->llsec->add_seclevel(dev, &sl);
1264 int ieee802154_llsec_add_seclevel(struct sk_buff *skb, struct genl_info *info)
1266 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1267 (NLM_F_CREATE | NLM_F_EXCL))
1270 return ieee802154_nl_llsec_change(skb, info, llsec_add_seclevel);
1273 static int llsec_del_seclevel(struct net_device *dev, struct genl_info *info)
1275 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1276 struct ieee802154_llsec_seclevel sl;
1278 if (llsec_parse_seclevel(info, &sl))
1281 return ops->llsec->del_seclevel(dev, &sl);
1284 int ieee802154_llsec_del_seclevel(struct sk_buff *skb, struct genl_info *info)
1286 return ieee802154_nl_llsec_change(skb, info, llsec_del_seclevel);
1290 ieee802154_nl_fill_seclevel(struct sk_buff *msg, u32 portid, u32 seq,
1291 const struct ieee802154_llsec_seclevel *sl,
1292 const struct net_device *dev)
1296 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1297 IEEE802154_LLSEC_LIST_SECLEVEL);
1301 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1302 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1303 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_FRAME_TYPE, sl->frame_type) ||
1304 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVELS, sl->sec_levels) ||
1305 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1306 sl->device_override))
1307 goto nla_put_failure;
1309 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD &&
1310 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID,
1312 goto nla_put_failure;
1314 genlmsg_end(msg, hdr);
1318 genlmsg_cancel(msg, hdr);
1323 static int llsec_iter_seclevels(struct llsec_dump_data *data)
1325 struct ieee802154_llsec_seclevel *pos;
1326 int rc = 0, idx = 0;
1328 list_for_each_entry(pos, &data->table->security_levels, list) {
1329 if (idx++ < data->s_idx)
1332 if (ieee802154_nl_fill_seclevel(data->skb, data->portid,
1333 data->nlmsg_seq, pos,
1345 int ieee802154_llsec_dump_seclevels(struct sk_buff *skb,
1346 struct netlink_callback *cb)
1348 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_seclevels);