1 // SPDX-License-Identifier: GPL-2.0-only
5 * Alexander Aring <aar@pengutronix.de>
7 * Based on: net/wireless/nl80211.c
10 #include <linux/rtnetlink.h>
12 #include <net/cfg802154.h>
13 #include <net/genetlink.h>
14 #include <net/mac802154.h>
15 #include <net/netlink.h>
16 #include <net/nl802154.h>
23 /* the netlink family */
24 static struct genl_family nl802154_fam;
26 /* multicast groups */
27 enum nl802154_multicast_groups {
28 NL802154_MCGRP_CONFIG,
31 static const struct genl_multicast_group nl802154_mcgrps[] = {
32 [NL802154_MCGRP_CONFIG] = { .name = "config", },
35 /* returns ERR_PTR values */
36 static struct wpan_dev *
37 __cfg802154_wpan_dev_from_attrs(struct net *netns, struct nlattr **attrs)
39 struct cfg802154_registered_device *rdev;
40 struct wpan_dev *result = NULL;
41 bool have_ifidx = attrs[NL802154_ATTR_IFINDEX];
42 bool have_wpan_dev_id = attrs[NL802154_ATTR_WPAN_DEV];
44 int wpan_phy_idx = -1;
49 if (!have_ifidx && !have_wpan_dev_id)
50 return ERR_PTR(-EINVAL);
53 ifidx = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
54 if (have_wpan_dev_id) {
55 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
56 wpan_phy_idx = wpan_dev_id >> 32;
59 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
60 struct wpan_dev *wpan_dev;
62 if (wpan_phy_net(&rdev->wpan_phy) != netns)
65 if (have_wpan_dev_id && rdev->wpan_phy_idx != wpan_phy_idx)
68 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
69 if (have_ifidx && wpan_dev->netdev &&
70 wpan_dev->netdev->ifindex == ifidx) {
74 if (have_wpan_dev_id &&
75 wpan_dev->identifier == (u32)wpan_dev_id) {
88 return ERR_PTR(-ENODEV);
91 static struct cfg802154_registered_device *
92 __cfg802154_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
94 struct cfg802154_registered_device *rdev = NULL, *tmp;
95 struct net_device *netdev;
99 if (!attrs[NL802154_ATTR_WPAN_PHY] &&
100 !attrs[NL802154_ATTR_IFINDEX] &&
101 !attrs[NL802154_ATTR_WPAN_DEV])
102 return ERR_PTR(-EINVAL);
104 if (attrs[NL802154_ATTR_WPAN_PHY])
105 rdev = cfg802154_rdev_by_wpan_phy_idx(
106 nla_get_u32(attrs[NL802154_ATTR_WPAN_PHY]));
108 if (attrs[NL802154_ATTR_WPAN_DEV]) {
109 u64 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
110 struct wpan_dev *wpan_dev;
113 tmp = cfg802154_rdev_by_wpan_phy_idx(wpan_dev_id >> 32);
115 /* make sure wpan_dev exists */
116 list_for_each_entry(wpan_dev, &tmp->wpan_dev_list, list) {
117 if (wpan_dev->identifier != (u32)wpan_dev_id)
126 if (rdev && tmp != rdev)
127 return ERR_PTR(-EINVAL);
132 if (attrs[NL802154_ATTR_IFINDEX]) {
133 int ifindex = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
135 netdev = __dev_get_by_index(netns, ifindex);
137 if (netdev->ieee802154_ptr)
138 tmp = wpan_phy_to_rdev(
139 netdev->ieee802154_ptr->wpan_phy);
143 /* not wireless device -- return error */
145 return ERR_PTR(-EINVAL);
147 /* mismatch -- return error */
148 if (rdev && tmp != rdev)
149 return ERR_PTR(-EINVAL);
156 return ERR_PTR(-ENODEV);
158 if (netns != wpan_phy_net(&rdev->wpan_phy))
159 return ERR_PTR(-ENODEV);
164 /* This function returns a pointer to the driver
165 * that the genl_info item that is passed refers to.
167 * The result of this can be a PTR_ERR and hence must
168 * be checked with IS_ERR() for errors.
170 static struct cfg802154_registered_device *
171 cfg802154_get_dev_from_info(struct net *netns, struct genl_info *info)
173 return __cfg802154_rdev_from_attrs(netns, info->attrs);
176 /* policy for the attributes */
177 static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
178 [NL802154_ATTR_WPAN_PHY] = { .type = NLA_U32 },
179 [NL802154_ATTR_WPAN_PHY_NAME] = { .type = NLA_NUL_STRING,
182 [NL802154_ATTR_IFINDEX] = { .type = NLA_U32 },
183 [NL802154_ATTR_IFTYPE] = { .type = NLA_U32 },
184 [NL802154_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
186 [NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
188 [NL802154_ATTR_PAGE] = { .type = NLA_U8, },
189 [NL802154_ATTR_CHANNEL] = { .type = NLA_U8, },
191 [NL802154_ATTR_TX_POWER] = { .type = NLA_S32, },
193 [NL802154_ATTR_CCA_MODE] = { .type = NLA_U32, },
194 [NL802154_ATTR_CCA_OPT] = { .type = NLA_U32, },
195 [NL802154_ATTR_CCA_ED_LEVEL] = { .type = NLA_S32, },
197 [NL802154_ATTR_SUPPORTED_CHANNEL] = { .type = NLA_U32, },
199 [NL802154_ATTR_PAN_ID] = { .type = NLA_U16, },
200 [NL802154_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
201 [NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16, },
203 [NL802154_ATTR_MIN_BE] = { .type = NLA_U8, },
204 [NL802154_ATTR_MAX_BE] = { .type = NLA_U8, },
205 [NL802154_ATTR_MAX_CSMA_BACKOFFS] = { .type = NLA_U8, },
207 [NL802154_ATTR_MAX_FRAME_RETRIES] = { .type = NLA_S8, },
209 [NL802154_ATTR_LBT_MODE] = { .type = NLA_U8, },
211 [NL802154_ATTR_WPAN_PHY_CAPS] = { .type = NLA_NESTED },
213 [NL802154_ATTR_SUPPORTED_COMMANDS] = { .type = NLA_NESTED },
215 [NL802154_ATTR_ACKREQ_DEFAULT] = { .type = NLA_U8 },
217 [NL802154_ATTR_PID] = { .type = NLA_U32 },
218 [NL802154_ATTR_NETNS_FD] = { .type = NLA_U32 },
219 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
220 [NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
221 [NL802154_ATTR_SEC_OUT_LEVEL] = { .type = NLA_U32, },
222 [NL802154_ATTR_SEC_OUT_KEY_ID] = { .type = NLA_NESTED, },
223 [NL802154_ATTR_SEC_FRAME_COUNTER] = { .type = NLA_U32 },
225 [NL802154_ATTR_SEC_LEVEL] = { .type = NLA_NESTED },
226 [NL802154_ATTR_SEC_DEVICE] = { .type = NLA_NESTED },
227 [NL802154_ATTR_SEC_DEVKEY] = { .type = NLA_NESTED },
228 [NL802154_ATTR_SEC_KEY] = { .type = NLA_NESTED },
229 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
232 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
234 nl802154_prepare_wpan_dev_dump(struct sk_buff *skb,
235 struct netlink_callback *cb,
236 struct cfg802154_registered_device **rdev,
237 struct wpan_dev **wpan_dev)
239 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
245 *wpan_dev = __cfg802154_wpan_dev_from_attrs(sock_net(skb->sk),
247 if (IS_ERR(*wpan_dev)) {
248 err = PTR_ERR(*wpan_dev);
251 *rdev = wpan_phy_to_rdev((*wpan_dev)->wpan_phy);
252 /* 0 is the first index - add 1 to parse only once */
253 cb->args[0] = (*rdev)->wpan_phy_idx + 1;
254 cb->args[1] = (*wpan_dev)->identifier;
256 /* subtract the 1 again here */
257 struct wpan_phy *wpan_phy = wpan_phy_idx_to_wpan_phy(cb->args[0] - 1);
258 struct wpan_dev *tmp;
264 *rdev = wpan_phy_to_rdev(wpan_phy);
267 list_for_each_entry(tmp, &(*rdev)->wpan_dev_list, list) {
268 if (tmp->identifier == cb->args[1]) {
287 nl802154_finish_wpan_dev_dump(struct cfg802154_registered_device *rdev)
291 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
293 /* message building helper */
294 static inline void *nl802154hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
297 /* since there is no private header just add the generic one */
298 return genlmsg_put(skb, portid, seq, &nl802154_fam, flags, cmd);
302 nl802154_put_flags(struct sk_buff *msg, int attr, u32 mask)
304 struct nlattr *nl_flags = nla_nest_start_noflag(msg, attr);
312 if ((mask & 1) && nla_put_flag(msg, i))
319 nla_nest_end(msg, nl_flags);
324 nl802154_send_wpan_phy_channels(struct cfg802154_registered_device *rdev,
327 struct nlattr *nl_page;
330 nl_page = nla_nest_start_noflag(msg, NL802154_ATTR_CHANNELS_SUPPORTED);
334 for (page = 0; page <= IEEE802154_MAX_PAGE; page++) {
335 if (nla_put_u32(msg, NL802154_ATTR_SUPPORTED_CHANNEL,
336 rdev->wpan_phy.supported.channels[page]))
339 nla_nest_end(msg, nl_page);
345 nl802154_put_capabilities(struct sk_buff *msg,
346 struct cfg802154_registered_device *rdev)
348 const struct wpan_phy_supported *caps = &rdev->wpan_phy.supported;
349 struct nlattr *nl_caps, *nl_channels;
352 nl_caps = nla_nest_start_noflag(msg, NL802154_ATTR_WPAN_PHY_CAPS);
356 nl_channels = nla_nest_start_noflag(msg, NL802154_CAP_ATTR_CHANNELS);
360 for (i = 0; i <= IEEE802154_MAX_PAGE; i++) {
361 if (caps->channels[i]) {
362 if (nl802154_put_flags(msg, i, caps->channels[i]))
367 nla_nest_end(msg, nl_channels);
369 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
370 struct nlattr *nl_ed_lvls;
372 nl_ed_lvls = nla_nest_start_noflag(msg,
373 NL802154_CAP_ATTR_CCA_ED_LEVELS);
377 for (i = 0; i < caps->cca_ed_levels_size; i++) {
378 if (nla_put_s32(msg, i, caps->cca_ed_levels[i]))
382 nla_nest_end(msg, nl_ed_lvls);
385 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
386 struct nlattr *nl_tx_pwrs;
388 nl_tx_pwrs = nla_nest_start_noflag(msg,
389 NL802154_CAP_ATTR_TX_POWERS);
393 for (i = 0; i < caps->tx_powers_size; i++) {
394 if (nla_put_s32(msg, i, caps->tx_powers[i]))
398 nla_nest_end(msg, nl_tx_pwrs);
401 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
402 if (nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_MODES,
404 nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_OPTS,
409 if (nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MINBE, caps->min_minbe) ||
410 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MINBE, caps->max_minbe) ||
411 nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MAXBE, caps->min_maxbe) ||
412 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MAXBE, caps->max_maxbe) ||
413 nla_put_u8(msg, NL802154_CAP_ATTR_MIN_CSMA_BACKOFFS,
414 caps->min_csma_backoffs) ||
415 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_CSMA_BACKOFFS,
416 caps->max_csma_backoffs) ||
417 nla_put_s8(msg, NL802154_CAP_ATTR_MIN_FRAME_RETRIES,
418 caps->min_frame_retries) ||
419 nla_put_s8(msg, NL802154_CAP_ATTR_MAX_FRAME_RETRIES,
420 caps->max_frame_retries) ||
421 nl802154_put_flags(msg, NL802154_CAP_ATTR_IFTYPES,
423 nla_put_u32(msg, NL802154_CAP_ATTR_LBT, caps->lbt))
426 nla_nest_end(msg, nl_caps);
431 static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev,
432 enum nl802154_commands cmd,
433 struct sk_buff *msg, u32 portid, u32 seq,
436 struct nlattr *nl_cmds;
440 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
444 if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
445 nla_put_string(msg, NL802154_ATTR_WPAN_PHY_NAME,
446 wpan_phy_name(&rdev->wpan_phy)) ||
447 nla_put_u32(msg, NL802154_ATTR_GENERATION,
448 cfg802154_rdev_list_generation))
449 goto nla_put_failure;
451 if (cmd != NL802154_CMD_NEW_WPAN_PHY)
456 /* current channel settings */
457 if (nla_put_u8(msg, NL802154_ATTR_PAGE,
458 rdev->wpan_phy.current_page) ||
459 nla_put_u8(msg, NL802154_ATTR_CHANNEL,
460 rdev->wpan_phy.current_channel))
461 goto nla_put_failure;
463 /* TODO remove this behaviour, we still keep support it for a while
464 * so users can change the behaviour to the new one.
466 if (nl802154_send_wpan_phy_channels(rdev, msg))
467 goto nla_put_failure;
470 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
471 if (nla_put_u32(msg, NL802154_ATTR_CCA_MODE,
472 rdev->wpan_phy.cca.mode))
473 goto nla_put_failure;
475 if (rdev->wpan_phy.cca.mode == NL802154_CCA_ENERGY_CARRIER) {
476 if (nla_put_u32(msg, NL802154_ATTR_CCA_OPT,
477 rdev->wpan_phy.cca.opt))
478 goto nla_put_failure;
482 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
483 if (nla_put_s32(msg, NL802154_ATTR_TX_POWER,
484 rdev->wpan_phy.transmit_power))
485 goto nla_put_failure;
488 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
489 if (nla_put_s32(msg, NL802154_ATTR_CCA_ED_LEVEL,
490 rdev->wpan_phy.cca_ed_level))
491 goto nla_put_failure;
494 if (nl802154_put_capabilities(msg, rdev))
495 goto nla_put_failure;
497 nl_cmds = nla_nest_start_noflag(msg, NL802154_ATTR_SUPPORTED_COMMANDS);
499 goto nla_put_failure;
504 if (rdev->ops->op) { \
506 if (nla_put_u32(msg, i, NL802154_CMD_ ## n)) \
507 goto nla_put_failure; \
511 CMD(add_virtual_intf, NEW_INTERFACE);
512 CMD(del_virtual_intf, DEL_INTERFACE);
513 CMD(set_channel, SET_CHANNEL);
514 CMD(set_pan_id, SET_PAN_ID);
515 CMD(set_short_addr, SET_SHORT_ADDR);
516 CMD(set_backoff_exponent, SET_BACKOFF_EXPONENT);
517 CMD(set_max_csma_backoffs, SET_MAX_CSMA_BACKOFFS);
518 CMD(set_max_frame_retries, SET_MAX_FRAME_RETRIES);
519 CMD(set_lbt_mode, SET_LBT_MODE);
520 CMD(set_ackreq_default, SET_ACKREQ_DEFAULT);
522 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER)
523 CMD(set_tx_power, SET_TX_POWER);
525 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL)
526 CMD(set_cca_ed_level, SET_CCA_ED_LEVEL);
528 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE)
529 CMD(set_cca_mode, SET_CCA_MODE);
532 nla_nest_end(msg, nl_cmds);
535 genlmsg_end(msg, hdr);
539 genlmsg_cancel(msg, hdr);
543 struct nl802154_dump_wpan_phy_state {
549 static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb,
550 struct netlink_callback *cb,
551 struct nl802154_dump_wpan_phy_state *state)
553 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
554 struct nlattr **tb = info->attrs;
556 if (tb[NL802154_ATTR_WPAN_PHY])
557 state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]);
558 if (tb[NL802154_ATTR_WPAN_DEV])
559 state->filter_wpan_phy = nla_get_u64(tb[NL802154_ATTR_WPAN_DEV]) >> 32;
560 if (tb[NL802154_ATTR_IFINDEX]) {
561 struct net_device *netdev;
562 struct cfg802154_registered_device *rdev;
563 int ifidx = nla_get_u32(tb[NL802154_ATTR_IFINDEX]);
565 netdev = __dev_get_by_index(&init_net, ifidx);
568 if (netdev->ieee802154_ptr) {
569 rdev = wpan_phy_to_rdev(
570 netdev->ieee802154_ptr->wpan_phy);
571 state->filter_wpan_phy = rdev->wpan_phy_idx;
579 nl802154_dump_wpan_phy(struct sk_buff *skb, struct netlink_callback *cb)
582 struct nl802154_dump_wpan_phy_state *state = (void *)cb->args[0];
583 struct cfg802154_registered_device *rdev;
587 state = kzalloc(sizeof(*state), GFP_KERNEL);
592 state->filter_wpan_phy = -1;
593 ret = nl802154_dump_wpan_phy_parse(skb, cb, state);
599 cb->args[0] = (long)state;
602 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
603 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
605 if (++idx <= state->start)
607 if (state->filter_wpan_phy != -1 &&
608 state->filter_wpan_phy != rdev->wpan_phy_idx)
610 /* attempt to fit multiple wpan_phy data chunks into the skb */
611 ret = nl802154_send_wpan_phy(rdev,
612 NL802154_CMD_NEW_WPAN_PHY,
614 NETLINK_CB(cb->skb).portid,
615 cb->nlh->nlmsg_seq, NLM_F_MULTI);
617 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
618 !skb->len && cb->min_dump_alloc < 4096) {
619 cb->min_dump_alloc = 4096;
635 static int nl802154_dump_wpan_phy_done(struct netlink_callback *cb)
637 kfree((void *)cb->args[0]);
641 static int nl802154_get_wpan_phy(struct sk_buff *skb, struct genl_info *info)
644 struct cfg802154_registered_device *rdev = info->user_ptr[0];
646 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
650 if (nl802154_send_wpan_phy(rdev, NL802154_CMD_NEW_WPAN_PHY, msg,
651 info->snd_portid, info->snd_seq, 0) < 0) {
656 return genlmsg_reply(msg, info);
659 static inline u64 wpan_dev_id(struct wpan_dev *wpan_dev)
661 return (u64)wpan_dev->identifier |
662 ((u64)wpan_phy_to_rdev(wpan_dev->wpan_phy)->wpan_phy_idx << 32);
665 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
666 #include <net/ieee802154_netdev.h>
669 ieee802154_llsec_send_key_id(struct sk_buff *msg,
670 const struct ieee802154_llsec_key_id *desc)
672 struct nlattr *nl_dev_addr;
674 if (nla_put_u32(msg, NL802154_KEY_ID_ATTR_MODE, desc->mode))
677 switch (desc->mode) {
678 case NL802154_KEY_ID_MODE_IMPLICIT:
679 nl_dev_addr = nla_nest_start_noflag(msg,
680 NL802154_KEY_ID_ATTR_IMPLICIT);
684 if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_PAN_ID,
685 desc->device_addr.pan_id) ||
686 nla_put_u32(msg, NL802154_DEV_ADDR_ATTR_MODE,
687 desc->device_addr.mode))
690 switch (desc->device_addr.mode) {
691 case NL802154_DEV_ADDR_SHORT:
692 if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_SHORT,
693 desc->device_addr.short_addr))
696 case NL802154_DEV_ADDR_EXTENDED:
697 if (nla_put_le64(msg, NL802154_DEV_ADDR_ATTR_EXTENDED,
698 desc->device_addr.extended_addr,
699 NL802154_DEV_ADDR_ATTR_PAD))
703 /* userspace should handle unknown */
707 nla_nest_end(msg, nl_dev_addr);
709 case NL802154_KEY_ID_MODE_INDEX:
711 case NL802154_KEY_ID_MODE_INDEX_SHORT:
712 /* TODO renmae short_source? */
713 if (nla_put_le32(msg, NL802154_KEY_ID_ATTR_SOURCE_SHORT,
717 case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
718 if (nla_put_le64(msg, NL802154_KEY_ID_ATTR_SOURCE_EXTENDED,
719 desc->extended_source,
720 NL802154_KEY_ID_ATTR_PAD))
724 /* userspace should handle unknown */
728 /* TODO key_id to key_idx ? Check naming */
729 if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
730 if (nla_put_u8(msg, NL802154_KEY_ID_ATTR_INDEX, desc->id))
737 static int nl802154_get_llsec_params(struct sk_buff *msg,
738 struct cfg802154_registered_device *rdev,
739 struct wpan_dev *wpan_dev)
741 struct nlattr *nl_key_id;
742 struct ieee802154_llsec_params params;
745 ret = rdev_get_llsec_params(rdev, wpan_dev, ¶ms);
749 if (nla_put_u8(msg, NL802154_ATTR_SEC_ENABLED, params.enabled) ||
750 nla_put_u32(msg, NL802154_ATTR_SEC_OUT_LEVEL, params.out_level) ||
751 nla_put_be32(msg, NL802154_ATTR_SEC_FRAME_COUNTER,
752 params.frame_counter))
755 nl_key_id = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_OUT_KEY_ID);
759 ret = ieee802154_llsec_send_key_id(msg, ¶ms.out_key);
763 nla_nest_end(msg, nl_key_id);
767 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
770 nl802154_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
771 struct cfg802154_registered_device *rdev,
772 struct wpan_dev *wpan_dev)
774 struct net_device *dev = wpan_dev->netdev;
777 hdr = nl802154hdr_put(msg, portid, seq, flags,
778 NL802154_CMD_NEW_INTERFACE);
783 (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex) ||
784 nla_put_string(msg, NL802154_ATTR_IFNAME, dev->name)))
785 goto nla_put_failure;
787 if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
788 nla_put_u32(msg, NL802154_ATTR_IFTYPE, wpan_dev->iftype) ||
789 nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
790 wpan_dev_id(wpan_dev), NL802154_ATTR_PAD) ||
791 nla_put_u32(msg, NL802154_ATTR_GENERATION,
792 rdev->devlist_generation ^
793 (cfg802154_rdev_list_generation << 2)))
794 goto nla_put_failure;
796 /* address settings */
797 if (nla_put_le64(msg, NL802154_ATTR_EXTENDED_ADDR,
798 wpan_dev->extended_addr,
799 NL802154_ATTR_PAD) ||
800 nla_put_le16(msg, NL802154_ATTR_SHORT_ADDR,
801 wpan_dev->short_addr) ||
802 nla_put_le16(msg, NL802154_ATTR_PAN_ID, wpan_dev->pan_id))
803 goto nla_put_failure;
806 if (nla_put_s8(msg, NL802154_ATTR_MAX_FRAME_RETRIES,
807 wpan_dev->frame_retries) ||
808 nla_put_u8(msg, NL802154_ATTR_MAX_BE, wpan_dev->max_be) ||
809 nla_put_u8(msg, NL802154_ATTR_MAX_CSMA_BACKOFFS,
810 wpan_dev->csma_retries) ||
811 nla_put_u8(msg, NL802154_ATTR_MIN_BE, wpan_dev->min_be))
812 goto nla_put_failure;
814 /* listen before transmit */
815 if (nla_put_u8(msg, NL802154_ATTR_LBT_MODE, wpan_dev->lbt))
816 goto nla_put_failure;
818 /* ackreq default behaviour */
819 if (nla_put_u8(msg, NL802154_ATTR_ACKREQ_DEFAULT, wpan_dev->ackreq))
820 goto nla_put_failure;
822 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
823 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
826 if (nl802154_get_llsec_params(msg, rdev, wpan_dev) < 0)
827 goto nla_put_failure;
830 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
832 genlmsg_end(msg, hdr);
836 genlmsg_cancel(msg, hdr);
841 nl802154_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
845 int wp_start = cb->args[0];
846 int if_start = cb->args[1];
847 struct cfg802154_registered_device *rdev;
848 struct wpan_dev *wpan_dev;
851 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
852 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
854 if (wp_idx < wp_start) {
860 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
861 if (if_idx < if_start) {
865 if (nl802154_send_iface(skb, NETLINK_CB(cb->skb).portid,
866 cb->nlh->nlmsg_seq, NLM_F_MULTI,
867 rdev, wpan_dev) < 0) {
878 cb->args[0] = wp_idx;
879 cb->args[1] = if_idx;
884 static int nl802154_get_interface(struct sk_buff *skb, struct genl_info *info)
887 struct cfg802154_registered_device *rdev = info->user_ptr[0];
888 struct wpan_dev *wdev = info->user_ptr[1];
890 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
894 if (nl802154_send_iface(msg, info->snd_portid, info->snd_seq, 0,
900 return genlmsg_reply(msg, info);
903 static int nl802154_new_interface(struct sk_buff *skb, struct genl_info *info)
905 struct cfg802154_registered_device *rdev = info->user_ptr[0];
906 enum nl802154_iftype type = NL802154_IFTYPE_UNSPEC;
907 __le64 extended_addr = cpu_to_le64(0x0000000000000000ULL);
909 /* TODO avoid failing a new interface
910 * creation due to pending removal?
913 if (!info->attrs[NL802154_ATTR_IFNAME])
916 if (info->attrs[NL802154_ATTR_IFTYPE]) {
917 type = nla_get_u32(info->attrs[NL802154_ATTR_IFTYPE]);
918 if (type > NL802154_IFTYPE_MAX ||
919 !(rdev->wpan_phy.supported.iftypes & BIT(type)))
923 if (info->attrs[NL802154_ATTR_EXTENDED_ADDR])
924 extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
926 if (!rdev->ops->add_virtual_intf)
929 return rdev_add_virtual_intf(rdev,
930 nla_data(info->attrs[NL802154_ATTR_IFNAME]),
931 NET_NAME_USER, type, extended_addr);
934 static int nl802154_del_interface(struct sk_buff *skb, struct genl_info *info)
936 struct cfg802154_registered_device *rdev = info->user_ptr[0];
937 struct wpan_dev *wpan_dev = info->user_ptr[1];
939 if (!rdev->ops->del_virtual_intf)
942 /* If we remove a wpan device without a netdev then clear
943 * user_ptr[1] so that nl802154_post_doit won't dereference it
944 * to check if it needs to do dev_put(). Otherwise it crashes
945 * since the wpan_dev has been freed, unlike with a netdev where
946 * we need the dev_put() for the netdev to really be freed.
948 if (!wpan_dev->netdev)
949 info->user_ptr[1] = NULL;
951 return rdev_del_virtual_intf(rdev, wpan_dev);
954 static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info)
956 struct cfg802154_registered_device *rdev = info->user_ptr[0];
959 if (!info->attrs[NL802154_ATTR_PAGE] ||
960 !info->attrs[NL802154_ATTR_CHANNEL])
963 page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
964 channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]);
966 /* check 802.15.4 constraints */
967 if (page > IEEE802154_MAX_PAGE || channel > IEEE802154_MAX_CHANNEL ||
968 !(rdev->wpan_phy.supported.channels[page] & BIT(channel)))
971 return rdev_set_channel(rdev, page, channel);
974 static int nl802154_set_cca_mode(struct sk_buff *skb, struct genl_info *info)
976 struct cfg802154_registered_device *rdev = info->user_ptr[0];
977 struct wpan_phy_cca cca;
979 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE))
982 if (!info->attrs[NL802154_ATTR_CCA_MODE])
985 cca.mode = nla_get_u32(info->attrs[NL802154_ATTR_CCA_MODE]);
986 /* checking 802.15.4 constraints */
987 if (cca.mode < NL802154_CCA_ENERGY ||
988 cca.mode > NL802154_CCA_ATTR_MAX ||
989 !(rdev->wpan_phy.supported.cca_modes & BIT(cca.mode)))
992 if (cca.mode == NL802154_CCA_ENERGY_CARRIER) {
993 if (!info->attrs[NL802154_ATTR_CCA_OPT])
996 cca.opt = nla_get_u32(info->attrs[NL802154_ATTR_CCA_OPT]);
997 if (cca.opt > NL802154_CCA_OPT_ATTR_MAX ||
998 !(rdev->wpan_phy.supported.cca_opts & BIT(cca.opt)))
1002 return rdev_set_cca_mode(rdev, &cca);
1005 static int nl802154_set_cca_ed_level(struct sk_buff *skb, struct genl_info *info)
1007 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1011 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL))
1014 if (!info->attrs[NL802154_ATTR_CCA_ED_LEVEL])
1017 ed_level = nla_get_s32(info->attrs[NL802154_ATTR_CCA_ED_LEVEL]);
1019 for (i = 0; i < rdev->wpan_phy.supported.cca_ed_levels_size; i++) {
1020 if (ed_level == rdev->wpan_phy.supported.cca_ed_levels[i])
1021 return rdev_set_cca_ed_level(rdev, ed_level);
1027 static int nl802154_set_tx_power(struct sk_buff *skb, struct genl_info *info)
1029 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1033 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER))
1036 if (!info->attrs[NL802154_ATTR_TX_POWER])
1039 power = nla_get_s32(info->attrs[NL802154_ATTR_TX_POWER]);
1041 for (i = 0; i < rdev->wpan_phy.supported.tx_powers_size; i++) {
1042 if (power == rdev->wpan_phy.supported.tx_powers[i])
1043 return rdev_set_tx_power(rdev, power);
1049 static int nl802154_set_pan_id(struct sk_buff *skb, struct genl_info *info)
1051 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1052 struct net_device *dev = info->user_ptr[1];
1053 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1056 /* conflict here while tx/rx calls */
1057 if (netif_running(dev))
1060 if (wpan_dev->lowpan_dev) {
1061 if (netif_running(wpan_dev->lowpan_dev))
1065 /* don't change address fields on monitor */
1066 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1067 !info->attrs[NL802154_ATTR_PAN_ID])
1070 pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
1073 * I am not sure about to check here on broadcast pan_id.
1074 * Broadcast is a valid setting, comment from 802.15.4:
1075 * If this value is 0xffff, the device is not associated.
1077 * This could useful to simple deassociate an device.
1079 if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
1082 return rdev_set_pan_id(rdev, wpan_dev, pan_id);
1085 static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info)
1087 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1088 struct net_device *dev = info->user_ptr[1];
1089 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1092 /* conflict here while tx/rx calls */
1093 if (netif_running(dev))
1096 if (wpan_dev->lowpan_dev) {
1097 if (netif_running(wpan_dev->lowpan_dev))
1101 /* don't change address fields on monitor */
1102 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1103 !info->attrs[NL802154_ATTR_SHORT_ADDR])
1106 short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
1109 * I am not sure about to check here on broadcast short_addr.
1110 * Broadcast is a valid setting, comment from 802.15.4:
1111 * A value of 0xfffe indicates that the device has
1112 * associated but has not been allocated an address. A
1113 * value of 0xffff indicates that the device does not
1114 * have a short address.
1116 * I think we should allow to set these settings but
1117 * don't allow to allow socket communication with it.
1119 if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
1120 short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
1123 return rdev_set_short_addr(rdev, wpan_dev, short_addr);
1127 nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info)
1129 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1130 struct net_device *dev = info->user_ptr[1];
1131 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1134 /* should be set on netif open inside phy settings */
1135 if (netif_running(dev))
1138 if (!info->attrs[NL802154_ATTR_MIN_BE] ||
1139 !info->attrs[NL802154_ATTR_MAX_BE])
1142 min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]);
1143 max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]);
1145 /* check 802.15.4 constraints */
1146 if (min_be < rdev->wpan_phy.supported.min_minbe ||
1147 min_be > rdev->wpan_phy.supported.max_minbe ||
1148 max_be < rdev->wpan_phy.supported.min_maxbe ||
1149 max_be > rdev->wpan_phy.supported.max_maxbe ||
1153 return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
1157 nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info)
1159 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1160 struct net_device *dev = info->user_ptr[1];
1161 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1162 u8 max_csma_backoffs;
1164 /* conflict here while other running iface settings */
1165 if (netif_running(dev))
1168 if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS])
1171 max_csma_backoffs = nla_get_u8(
1172 info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]);
1174 /* check 802.15.4 constraints */
1175 if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs ||
1176 max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs)
1179 return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
1183 nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info)
1185 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1186 struct net_device *dev = info->user_ptr[1];
1187 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1188 s8 max_frame_retries;
1190 if (netif_running(dev))
1193 if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES])
1196 max_frame_retries = nla_get_s8(
1197 info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]);
1199 /* check 802.15.4 constraints */
1200 if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries ||
1201 max_frame_retries > rdev->wpan_phy.supported.max_frame_retries)
1204 return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
1207 static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info)
1209 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1210 struct net_device *dev = info->user_ptr[1];
1211 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1214 if (netif_running(dev))
1217 if (!info->attrs[NL802154_ATTR_LBT_MODE])
1220 mode = nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
1222 if (mode != 0 && mode != 1)
1225 if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt))
1228 return rdev_set_lbt_mode(rdev, wpan_dev, mode);
1232 nl802154_set_ackreq_default(struct sk_buff *skb, struct genl_info *info)
1234 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1235 struct net_device *dev = info->user_ptr[1];
1236 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1239 if (netif_running(dev))
1242 if (!info->attrs[NL802154_ATTR_ACKREQ_DEFAULT])
1245 ackreq = nla_get_u8(info->attrs[NL802154_ATTR_ACKREQ_DEFAULT]);
1247 if (ackreq != 0 && ackreq != 1)
1250 return rdev_set_ackreq_default(rdev, wpan_dev, ackreq);
1253 static int nl802154_wpan_phy_netns(struct sk_buff *skb, struct genl_info *info)
1255 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1259 if (info->attrs[NL802154_ATTR_PID]) {
1260 u32 pid = nla_get_u32(info->attrs[NL802154_ATTR_PID]);
1262 net = get_net_ns_by_pid(pid);
1263 } else if (info->attrs[NL802154_ATTR_NETNS_FD]) {
1264 u32 fd = nla_get_u32(info->attrs[NL802154_ATTR_NETNS_FD]);
1266 net = get_net_ns_by_fd(fd);
1272 return PTR_ERR(net);
1276 /* check if anything to do */
1277 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), net))
1278 err = cfg802154_switch_netns(rdev, net);
1284 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
1285 static const struct nla_policy nl802154_dev_addr_policy[NL802154_DEV_ADDR_ATTR_MAX + 1] = {
1286 [NL802154_DEV_ADDR_ATTR_PAN_ID] = { .type = NLA_U16 },
1287 [NL802154_DEV_ADDR_ATTR_MODE] = { .type = NLA_U32 },
1288 [NL802154_DEV_ADDR_ATTR_SHORT] = { .type = NLA_U16 },
1289 [NL802154_DEV_ADDR_ATTR_EXTENDED] = { .type = NLA_U64 },
1293 ieee802154_llsec_parse_dev_addr(struct nlattr *nla,
1294 struct ieee802154_addr *addr)
1296 struct nlattr *attrs[NL802154_DEV_ADDR_ATTR_MAX + 1];
1298 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ADDR_ATTR_MAX, nla, nl802154_dev_addr_policy, NULL))
1301 if (!attrs[NL802154_DEV_ADDR_ATTR_PAN_ID] ||
1302 !attrs[NL802154_DEV_ADDR_ATTR_MODE] ||
1303 !(attrs[NL802154_DEV_ADDR_ATTR_SHORT] ||
1304 attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]))
1307 addr->pan_id = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_PAN_ID]);
1308 addr->mode = nla_get_u32(attrs[NL802154_DEV_ADDR_ATTR_MODE]);
1309 switch (addr->mode) {
1310 case NL802154_DEV_ADDR_SHORT:
1311 addr->short_addr = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_SHORT]);
1313 case NL802154_DEV_ADDR_EXTENDED:
1314 addr->extended_addr = nla_get_le64(attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]);
1323 static const struct nla_policy nl802154_key_id_policy[NL802154_KEY_ID_ATTR_MAX + 1] = {
1324 [NL802154_KEY_ID_ATTR_MODE] = { .type = NLA_U32 },
1325 [NL802154_KEY_ID_ATTR_INDEX] = { .type = NLA_U8 },
1326 [NL802154_KEY_ID_ATTR_IMPLICIT] = { .type = NLA_NESTED },
1327 [NL802154_KEY_ID_ATTR_SOURCE_SHORT] = { .type = NLA_U32 },
1328 [NL802154_KEY_ID_ATTR_SOURCE_EXTENDED] = { .type = NLA_U64 },
1332 ieee802154_llsec_parse_key_id(struct nlattr *nla,
1333 struct ieee802154_llsec_key_id *desc)
1335 struct nlattr *attrs[NL802154_KEY_ID_ATTR_MAX + 1];
1337 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_KEY_ID_ATTR_MAX, nla, nl802154_key_id_policy, NULL))
1340 if (!attrs[NL802154_KEY_ID_ATTR_MODE])
1343 desc->mode = nla_get_u32(attrs[NL802154_KEY_ID_ATTR_MODE]);
1344 switch (desc->mode) {
1345 case NL802154_KEY_ID_MODE_IMPLICIT:
1346 if (!attrs[NL802154_KEY_ID_ATTR_IMPLICIT])
1349 if (ieee802154_llsec_parse_dev_addr(attrs[NL802154_KEY_ID_ATTR_IMPLICIT],
1350 &desc->device_addr) < 0)
1353 case NL802154_KEY_ID_MODE_INDEX:
1355 case NL802154_KEY_ID_MODE_INDEX_SHORT:
1356 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT])
1359 desc->short_source = nla_get_le32(attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT]);
1361 case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
1362 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED])
1365 desc->extended_source = nla_get_le64(attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED]);
1371 if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
1372 if (!attrs[NL802154_KEY_ID_ATTR_INDEX])
1375 /* TODO change id to idx */
1376 desc->id = nla_get_u8(attrs[NL802154_KEY_ID_ATTR_INDEX]);
1382 static int nl802154_set_llsec_params(struct sk_buff *skb,
1383 struct genl_info *info)
1385 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1386 struct net_device *dev = info->user_ptr[1];
1387 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1388 struct ieee802154_llsec_params params;
1392 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1395 if (info->attrs[NL802154_ATTR_SEC_ENABLED]) {
1398 enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1399 if (enabled != 0 && enabled != 1)
1402 params.enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1403 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
1406 if (info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID]) {
1407 ret = ieee802154_llsec_parse_key_id(info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID],
1412 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
1415 if (info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]) {
1416 params.out_level = nla_get_u32(info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]);
1417 if (params.out_level > NL802154_SECLEVEL_MAX)
1420 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
1423 if (info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]) {
1424 params.frame_counter = nla_get_be32(info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]);
1425 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
1428 return rdev_set_llsec_params(rdev, wpan_dev, ¶ms, changed);
1431 static int nl802154_send_key(struct sk_buff *msg, u32 cmd, u32 portid,
1433 struct cfg802154_registered_device *rdev,
1434 struct net_device *dev,
1435 const struct ieee802154_llsec_key_entry *key)
1438 u32 commands[NL802154_CMD_FRAME_NR_IDS / 32];
1439 struct nlattr *nl_key, *nl_key_id;
1441 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1445 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1446 goto nla_put_failure;
1448 nl_key = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_KEY);
1450 goto nla_put_failure;
1452 nl_key_id = nla_nest_start_noflag(msg, NL802154_KEY_ATTR_ID);
1454 goto nla_put_failure;
1456 if (ieee802154_llsec_send_key_id(msg, &key->id) < 0)
1457 goto nla_put_failure;
1459 nla_nest_end(msg, nl_key_id);
1461 if (nla_put_u8(msg, NL802154_KEY_ATTR_USAGE_FRAMES,
1462 key->key->frame_types))
1463 goto nla_put_failure;
1465 if (key->key->frame_types & BIT(NL802154_FRAME_CMD)) {
1466 /* TODO for each nested */
1467 memset(commands, 0, sizeof(commands));
1468 commands[7] = key->key->cmd_frame_ids;
1469 if (nla_put(msg, NL802154_KEY_ATTR_USAGE_CMDS,
1470 sizeof(commands), commands))
1471 goto nla_put_failure;
1474 if (nla_put(msg, NL802154_KEY_ATTR_BYTES, NL802154_KEY_SIZE,
1476 goto nla_put_failure;
1478 nla_nest_end(msg, nl_key);
1479 genlmsg_end(msg, hdr);
1484 genlmsg_cancel(msg, hdr);
1489 nl802154_dump_llsec_key(struct sk_buff *skb, struct netlink_callback *cb)
1491 struct cfg802154_registered_device *rdev = NULL;
1492 struct ieee802154_llsec_key_entry *key;
1493 struct ieee802154_llsec_table *table;
1494 struct wpan_dev *wpan_dev;
1497 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1501 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1506 if (!wpan_dev->netdev) {
1511 rdev_lock_llsec_table(rdev, wpan_dev);
1512 rdev_get_llsec_table(rdev, wpan_dev, &table);
1514 /* TODO make it like station dump */
1518 list_for_each_entry(key, &table->keys, list) {
1519 if (nl802154_send_key(skb, NL802154_CMD_NEW_SEC_KEY,
1520 NETLINK_CB(cb->skb).portid,
1521 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1522 rdev, wpan_dev->netdev, key) < 0) {
1525 rdev_unlock_llsec_table(rdev, wpan_dev);
1533 rdev_unlock_llsec_table(rdev, wpan_dev);
1536 nl802154_finish_wpan_dev_dump(rdev);
1541 static const struct nla_policy nl802154_key_policy[NL802154_KEY_ATTR_MAX + 1] = {
1542 [NL802154_KEY_ATTR_ID] = { NLA_NESTED },
1543 /* TODO handle it as for_each_nested and NLA_FLAG? */
1544 [NL802154_KEY_ATTR_USAGE_FRAMES] = { NLA_U8 },
1545 /* TODO handle it as for_each_nested, not static array? */
1546 [NL802154_KEY_ATTR_USAGE_CMDS] = { .len = NL802154_CMD_FRAME_NR_IDS / 8 },
1547 [NL802154_KEY_ATTR_BYTES] = { .len = NL802154_KEY_SIZE },
1550 static int nl802154_add_llsec_key(struct sk_buff *skb, struct genl_info *info)
1552 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1553 struct net_device *dev = info->user_ptr[1];
1554 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1555 struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1556 struct ieee802154_llsec_key key = { };
1557 struct ieee802154_llsec_key_id id = { };
1558 u32 commands[NL802154_CMD_FRAME_NR_IDS / 32] = { };
1560 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1563 if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
1564 nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
1567 if (!attrs[NL802154_KEY_ATTR_USAGE_FRAMES] ||
1568 !attrs[NL802154_KEY_ATTR_BYTES])
1571 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1574 key.frame_types = nla_get_u8(attrs[NL802154_KEY_ATTR_USAGE_FRAMES]);
1575 if (key.frame_types > BIT(NL802154_FRAME_MAX) ||
1576 ((key.frame_types & BIT(NL802154_FRAME_CMD)) &&
1577 !attrs[NL802154_KEY_ATTR_USAGE_CMDS]))
1580 if (attrs[NL802154_KEY_ATTR_USAGE_CMDS]) {
1581 /* TODO for each nested */
1582 nla_memcpy(commands, attrs[NL802154_KEY_ATTR_USAGE_CMDS],
1583 NL802154_CMD_FRAME_NR_IDS / 8);
1585 /* TODO understand the -EINVAL logic here? last condition */
1586 if (commands[0] || commands[1] || commands[2] || commands[3] ||
1587 commands[4] || commands[5] || commands[6] ||
1588 commands[7] > BIT(NL802154_CMD_FRAME_MAX))
1591 key.cmd_frame_ids = commands[7];
1593 key.cmd_frame_ids = 0;
1596 nla_memcpy(key.key, attrs[NL802154_KEY_ATTR_BYTES], NL802154_KEY_SIZE);
1598 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1601 return rdev_add_llsec_key(rdev, wpan_dev, &id, &key);
1604 static int nl802154_del_llsec_key(struct sk_buff *skb, struct genl_info *info)
1606 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1607 struct net_device *dev = info->user_ptr[1];
1608 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1609 struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1610 struct ieee802154_llsec_key_id id;
1612 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1615 if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
1616 nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
1619 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1622 return rdev_del_llsec_key(rdev, wpan_dev, &id);
1625 static int nl802154_send_device(struct sk_buff *msg, u32 cmd, u32 portid,
1627 struct cfg802154_registered_device *rdev,
1628 struct net_device *dev,
1629 const struct ieee802154_llsec_device *dev_desc)
1632 struct nlattr *nl_device;
1634 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1638 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1639 goto nla_put_failure;
1641 nl_device = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVICE);
1643 goto nla_put_failure;
1645 if (nla_put_u32(msg, NL802154_DEV_ATTR_FRAME_COUNTER,
1646 dev_desc->frame_counter) ||
1647 nla_put_le16(msg, NL802154_DEV_ATTR_PAN_ID, dev_desc->pan_id) ||
1648 nla_put_le16(msg, NL802154_DEV_ATTR_SHORT_ADDR,
1649 dev_desc->short_addr) ||
1650 nla_put_le64(msg, NL802154_DEV_ATTR_EXTENDED_ADDR,
1651 dev_desc->hwaddr, NL802154_DEV_ATTR_PAD) ||
1652 nla_put_u8(msg, NL802154_DEV_ATTR_SECLEVEL_EXEMPT,
1653 dev_desc->seclevel_exempt) ||
1654 nla_put_u32(msg, NL802154_DEV_ATTR_KEY_MODE, dev_desc->key_mode))
1655 goto nla_put_failure;
1657 nla_nest_end(msg, nl_device);
1658 genlmsg_end(msg, hdr);
1663 genlmsg_cancel(msg, hdr);
1668 nl802154_dump_llsec_dev(struct sk_buff *skb, struct netlink_callback *cb)
1670 struct cfg802154_registered_device *rdev = NULL;
1671 struct ieee802154_llsec_device *dev;
1672 struct ieee802154_llsec_table *table;
1673 struct wpan_dev *wpan_dev;
1676 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1680 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1685 if (!wpan_dev->netdev) {
1690 rdev_lock_llsec_table(rdev, wpan_dev);
1691 rdev_get_llsec_table(rdev, wpan_dev, &table);
1693 /* TODO make it like station dump */
1697 list_for_each_entry(dev, &table->devices, list) {
1698 if (nl802154_send_device(skb, NL802154_CMD_NEW_SEC_LEVEL,
1699 NETLINK_CB(cb->skb).portid,
1700 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1701 rdev, wpan_dev->netdev, dev) < 0) {
1704 rdev_unlock_llsec_table(rdev, wpan_dev);
1712 rdev_unlock_llsec_table(rdev, wpan_dev);
1715 nl802154_finish_wpan_dev_dump(rdev);
1720 static const struct nla_policy nl802154_dev_policy[NL802154_DEV_ATTR_MAX + 1] = {
1721 [NL802154_DEV_ATTR_FRAME_COUNTER] = { NLA_U32 },
1722 [NL802154_DEV_ATTR_PAN_ID] = { .type = NLA_U16 },
1723 [NL802154_DEV_ATTR_SHORT_ADDR] = { .type = NLA_U16 },
1724 [NL802154_DEV_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
1725 [NL802154_DEV_ATTR_SECLEVEL_EXEMPT] = { NLA_U8 },
1726 [NL802154_DEV_ATTR_KEY_MODE] = { NLA_U32 },
1730 ieee802154_llsec_parse_device(struct nlattr *nla,
1731 struct ieee802154_llsec_device *dev)
1733 struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
1735 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, nla, nl802154_dev_policy, NULL))
1738 memset(dev, 0, sizeof(*dev));
1740 if (!attrs[NL802154_DEV_ATTR_FRAME_COUNTER] ||
1741 !attrs[NL802154_DEV_ATTR_PAN_ID] ||
1742 !attrs[NL802154_DEV_ATTR_SHORT_ADDR] ||
1743 !attrs[NL802154_DEV_ATTR_EXTENDED_ADDR] ||
1744 !attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] ||
1745 !attrs[NL802154_DEV_ATTR_KEY_MODE])
1749 dev->frame_counter = nla_get_u32(attrs[NL802154_DEV_ATTR_FRAME_COUNTER]);
1750 dev->pan_id = nla_get_le16(attrs[NL802154_DEV_ATTR_PAN_ID]);
1751 dev->short_addr = nla_get_le16(attrs[NL802154_DEV_ATTR_SHORT_ADDR]);
1752 /* TODO rename hwaddr to extended_addr */
1753 dev->hwaddr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
1754 dev->seclevel_exempt = nla_get_u8(attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT]);
1755 dev->key_mode = nla_get_u32(attrs[NL802154_DEV_ATTR_KEY_MODE]);
1757 if (dev->key_mode > NL802154_DEVKEY_MAX ||
1758 (dev->seclevel_exempt != 0 && dev->seclevel_exempt != 1))
1764 static int nl802154_add_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1766 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1767 struct net_device *dev = info->user_ptr[1];
1768 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1769 struct ieee802154_llsec_device dev_desc;
1771 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1774 if (ieee802154_llsec_parse_device(info->attrs[NL802154_ATTR_SEC_DEVICE],
1778 return rdev_add_device(rdev, wpan_dev, &dev_desc);
1781 static int nl802154_del_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1783 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1784 struct net_device *dev = info->user_ptr[1];
1785 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1786 struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
1787 __le64 extended_addr;
1789 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1792 if (!info->attrs[NL802154_ATTR_SEC_DEVICE] ||
1793 nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVICE], nl802154_dev_policy, info->extack))
1796 if (!attrs[NL802154_DEV_ATTR_EXTENDED_ADDR])
1799 extended_addr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
1800 return rdev_del_device(rdev, wpan_dev, extended_addr);
1803 static int nl802154_send_devkey(struct sk_buff *msg, u32 cmd, u32 portid,
1805 struct cfg802154_registered_device *rdev,
1806 struct net_device *dev, __le64 extended_addr,
1807 const struct ieee802154_llsec_device_key *devkey)
1810 struct nlattr *nl_devkey, *nl_key_id;
1812 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1816 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1817 goto nla_put_failure;
1819 nl_devkey = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVKEY);
1821 goto nla_put_failure;
1823 if (nla_put_le64(msg, NL802154_DEVKEY_ATTR_EXTENDED_ADDR,
1824 extended_addr, NL802154_DEVKEY_ATTR_PAD) ||
1825 nla_put_u32(msg, NL802154_DEVKEY_ATTR_FRAME_COUNTER,
1826 devkey->frame_counter))
1827 goto nla_put_failure;
1829 nl_key_id = nla_nest_start_noflag(msg, NL802154_DEVKEY_ATTR_ID);
1831 goto nla_put_failure;
1833 if (ieee802154_llsec_send_key_id(msg, &devkey->key_id) < 0)
1834 goto nla_put_failure;
1836 nla_nest_end(msg, nl_key_id);
1837 nla_nest_end(msg, nl_devkey);
1838 genlmsg_end(msg, hdr);
1843 genlmsg_cancel(msg, hdr);
1848 nl802154_dump_llsec_devkey(struct sk_buff *skb, struct netlink_callback *cb)
1850 struct cfg802154_registered_device *rdev = NULL;
1851 struct ieee802154_llsec_device_key *kpos;
1852 struct ieee802154_llsec_device *dpos;
1853 struct ieee802154_llsec_table *table;
1854 struct wpan_dev *wpan_dev;
1857 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1861 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1866 if (!wpan_dev->netdev) {
1871 rdev_lock_llsec_table(rdev, wpan_dev);
1872 rdev_get_llsec_table(rdev, wpan_dev, &table);
1874 /* TODO make it like station dump */
1878 /* TODO look if remove devkey and do some nested attribute */
1879 list_for_each_entry(dpos, &table->devices, list) {
1880 list_for_each_entry(kpos, &dpos->keys, list) {
1881 if (nl802154_send_devkey(skb,
1882 NL802154_CMD_NEW_SEC_LEVEL,
1883 NETLINK_CB(cb->skb).portid,
1891 rdev_unlock_llsec_table(rdev, wpan_dev);
1900 rdev_unlock_llsec_table(rdev, wpan_dev);
1903 nl802154_finish_wpan_dev_dump(rdev);
1908 static const struct nla_policy nl802154_devkey_policy[NL802154_DEVKEY_ATTR_MAX + 1] = {
1909 [NL802154_DEVKEY_ATTR_FRAME_COUNTER] = { NLA_U32 },
1910 [NL802154_DEVKEY_ATTR_EXTENDED_ADDR] = { NLA_U64 },
1911 [NL802154_DEVKEY_ATTR_ID] = { NLA_NESTED },
1914 static int nl802154_add_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
1916 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1917 struct net_device *dev = info->user_ptr[1];
1918 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1919 struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
1920 struct ieee802154_llsec_device_key key;
1921 __le64 extended_addr;
1923 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1926 if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
1927 nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack) < 0)
1930 if (!attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER] ||
1931 !attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1934 /* TODO change key.id ? */
1935 if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1940 key.frame_counter = nla_get_u32(attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER]);
1941 /* TODO change naming hwaddr -> extended_addr
1942 * check unique identifier short+pan OR extended_addr
1944 extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1945 return rdev_add_devkey(rdev, wpan_dev, extended_addr, &key);
1948 static int nl802154_del_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
1950 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1951 struct net_device *dev = info->user_ptr[1];
1952 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1953 struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
1954 struct ieee802154_llsec_device_key key;
1955 __le64 extended_addr;
1957 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1960 if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
1961 nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack))
1964 if (!attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1967 /* TODO change key.id ? */
1968 if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1972 /* TODO change naming hwaddr -> extended_addr
1973 * check unique identifier short+pan OR extended_addr
1975 extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1976 return rdev_del_devkey(rdev, wpan_dev, extended_addr, &key);
1979 static int nl802154_send_seclevel(struct sk_buff *msg, u32 cmd, u32 portid,
1981 struct cfg802154_registered_device *rdev,
1982 struct net_device *dev,
1983 const struct ieee802154_llsec_seclevel *sl)
1986 struct nlattr *nl_seclevel;
1988 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1992 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1993 goto nla_put_failure;
1995 nl_seclevel = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_LEVEL);
1997 goto nla_put_failure;
1999 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_FRAME, sl->frame_type) ||
2000 nla_put_u32(msg, NL802154_SECLEVEL_ATTR_LEVELS, sl->sec_levels) ||
2001 nla_put_u8(msg, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE,
2002 sl->device_override))
2003 goto nla_put_failure;
2005 if (sl->frame_type == NL802154_FRAME_CMD) {
2006 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_CMD_FRAME,
2008 goto nla_put_failure;
2011 nla_nest_end(msg, nl_seclevel);
2012 genlmsg_end(msg, hdr);
2017 genlmsg_cancel(msg, hdr);
2022 nl802154_dump_llsec_seclevel(struct sk_buff *skb, struct netlink_callback *cb)
2024 struct cfg802154_registered_device *rdev = NULL;
2025 struct ieee802154_llsec_seclevel *sl;
2026 struct ieee802154_llsec_table *table;
2027 struct wpan_dev *wpan_dev;
2030 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2034 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2039 if (!wpan_dev->netdev) {
2044 rdev_lock_llsec_table(rdev, wpan_dev);
2045 rdev_get_llsec_table(rdev, wpan_dev, &table);
2047 /* TODO make it like station dump */
2051 list_for_each_entry(sl, &table->security_levels, list) {
2052 if (nl802154_send_seclevel(skb, NL802154_CMD_NEW_SEC_LEVEL,
2053 NETLINK_CB(cb->skb).portid,
2054 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2055 rdev, wpan_dev->netdev, sl) < 0) {
2058 rdev_unlock_llsec_table(rdev, wpan_dev);
2066 rdev_unlock_llsec_table(rdev, wpan_dev);
2069 nl802154_finish_wpan_dev_dump(rdev);
2074 static const struct nla_policy nl802154_seclevel_policy[NL802154_SECLEVEL_ATTR_MAX + 1] = {
2075 [NL802154_SECLEVEL_ATTR_LEVELS] = { .type = NLA_U8 },
2076 [NL802154_SECLEVEL_ATTR_FRAME] = { .type = NLA_U32 },
2077 [NL802154_SECLEVEL_ATTR_CMD_FRAME] = { .type = NLA_U32 },
2078 [NL802154_SECLEVEL_ATTR_DEV_OVERRIDE] = { .type = NLA_U8 },
2082 llsec_parse_seclevel(struct nlattr *nla, struct ieee802154_llsec_seclevel *sl)
2084 struct nlattr *attrs[NL802154_SECLEVEL_ATTR_MAX + 1];
2086 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_SECLEVEL_ATTR_MAX, nla, nl802154_seclevel_policy, NULL))
2089 memset(sl, 0, sizeof(*sl));
2091 if (!attrs[NL802154_SECLEVEL_ATTR_LEVELS] ||
2092 !attrs[NL802154_SECLEVEL_ATTR_FRAME] ||
2093 !attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE])
2096 sl->sec_levels = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_LEVELS]);
2097 sl->frame_type = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_FRAME]);
2098 sl->device_override = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE]);
2099 if (sl->frame_type > NL802154_FRAME_MAX ||
2100 (sl->device_override != 0 && sl->device_override != 1))
2103 if (sl->frame_type == NL802154_FRAME_CMD) {
2104 if (!attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME])
2107 sl->cmd_frame_id = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME]);
2108 if (sl->cmd_frame_id > NL802154_CMD_FRAME_MAX)
2115 static int nl802154_add_llsec_seclevel(struct sk_buff *skb,
2116 struct genl_info *info)
2118 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2119 struct net_device *dev = info->user_ptr[1];
2120 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2121 struct ieee802154_llsec_seclevel sl;
2123 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2126 if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2130 return rdev_add_seclevel(rdev, wpan_dev, &sl);
2133 static int nl802154_del_llsec_seclevel(struct sk_buff *skb,
2134 struct genl_info *info)
2136 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2137 struct net_device *dev = info->user_ptr[1];
2138 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2139 struct ieee802154_llsec_seclevel sl;
2141 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2144 if (!info->attrs[NL802154_ATTR_SEC_LEVEL] ||
2145 llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2149 return rdev_del_seclevel(rdev, wpan_dev, &sl);
2151 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2153 #define NL802154_FLAG_NEED_WPAN_PHY 0x01
2154 #define NL802154_FLAG_NEED_NETDEV 0x02
2155 #define NL802154_FLAG_NEED_RTNL 0x04
2156 #define NL802154_FLAG_CHECK_NETDEV_UP 0x08
2157 #define NL802154_FLAG_NEED_WPAN_DEV 0x10
2159 static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
2160 struct genl_info *info)
2162 struct cfg802154_registered_device *rdev;
2163 struct wpan_dev *wpan_dev;
2164 struct net_device *dev;
2165 bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
2170 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
2171 rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
2175 return PTR_ERR(rdev);
2177 info->user_ptr[0] = rdev;
2178 } else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
2179 ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2181 wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
2183 if (IS_ERR(wpan_dev)) {
2186 return PTR_ERR(wpan_dev);
2189 dev = wpan_dev->netdev;
2190 rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
2192 if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
2199 info->user_ptr[1] = dev;
2201 info->user_ptr[1] = wpan_dev;
2205 if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
2206 !netif_running(dev)) {
2215 info->user_ptr[0] = rdev;
2221 static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
2222 struct genl_info *info)
2224 if (info->user_ptr[1]) {
2225 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2226 struct wpan_dev *wpan_dev = info->user_ptr[1];
2228 if (wpan_dev->netdev)
2229 dev_put(wpan_dev->netdev);
2231 dev_put(info->user_ptr[1]);
2235 if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
2239 static const struct genl_ops nl802154_ops[] = {
2241 .cmd = NL802154_CMD_GET_WPAN_PHY,
2242 .validate = GENL_DONT_VALIDATE_STRICT |
2243 GENL_DONT_VALIDATE_DUMP_STRICT,
2244 .doit = nl802154_get_wpan_phy,
2245 .dumpit = nl802154_dump_wpan_phy,
2246 .done = nl802154_dump_wpan_phy_done,
2247 /* can be retrieved by unprivileged users */
2248 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2249 NL802154_FLAG_NEED_RTNL,
2252 .cmd = NL802154_CMD_GET_INTERFACE,
2253 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2254 .doit = nl802154_get_interface,
2255 .dumpit = nl802154_dump_interface,
2256 /* can be retrieved by unprivileged users */
2257 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2258 NL802154_FLAG_NEED_RTNL,
2261 .cmd = NL802154_CMD_NEW_INTERFACE,
2262 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2263 .doit = nl802154_new_interface,
2264 .flags = GENL_ADMIN_PERM,
2265 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2266 NL802154_FLAG_NEED_RTNL,
2269 .cmd = NL802154_CMD_DEL_INTERFACE,
2270 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2271 .doit = nl802154_del_interface,
2272 .flags = GENL_ADMIN_PERM,
2273 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2274 NL802154_FLAG_NEED_RTNL,
2277 .cmd = NL802154_CMD_SET_CHANNEL,
2278 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2279 .doit = nl802154_set_channel,
2280 .flags = GENL_ADMIN_PERM,
2281 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2282 NL802154_FLAG_NEED_RTNL,
2285 .cmd = NL802154_CMD_SET_CCA_MODE,
2286 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2287 .doit = nl802154_set_cca_mode,
2288 .flags = GENL_ADMIN_PERM,
2289 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2290 NL802154_FLAG_NEED_RTNL,
2293 .cmd = NL802154_CMD_SET_CCA_ED_LEVEL,
2294 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2295 .doit = nl802154_set_cca_ed_level,
2296 .flags = GENL_ADMIN_PERM,
2297 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2298 NL802154_FLAG_NEED_RTNL,
2301 .cmd = NL802154_CMD_SET_TX_POWER,
2302 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2303 .doit = nl802154_set_tx_power,
2304 .flags = GENL_ADMIN_PERM,
2305 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2306 NL802154_FLAG_NEED_RTNL,
2309 .cmd = NL802154_CMD_SET_WPAN_PHY_NETNS,
2310 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2311 .doit = nl802154_wpan_phy_netns,
2312 .flags = GENL_ADMIN_PERM,
2313 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2314 NL802154_FLAG_NEED_RTNL,
2317 .cmd = NL802154_CMD_SET_PAN_ID,
2318 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2319 .doit = nl802154_set_pan_id,
2320 .flags = GENL_ADMIN_PERM,
2321 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2322 NL802154_FLAG_NEED_RTNL,
2325 .cmd = NL802154_CMD_SET_SHORT_ADDR,
2326 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2327 .doit = nl802154_set_short_addr,
2328 .flags = GENL_ADMIN_PERM,
2329 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2330 NL802154_FLAG_NEED_RTNL,
2333 .cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
2334 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2335 .doit = nl802154_set_backoff_exponent,
2336 .flags = GENL_ADMIN_PERM,
2337 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2338 NL802154_FLAG_NEED_RTNL,
2341 .cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
2342 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2343 .doit = nl802154_set_max_csma_backoffs,
2344 .flags = GENL_ADMIN_PERM,
2345 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2346 NL802154_FLAG_NEED_RTNL,
2349 .cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
2350 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2351 .doit = nl802154_set_max_frame_retries,
2352 .flags = GENL_ADMIN_PERM,
2353 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2354 NL802154_FLAG_NEED_RTNL,
2357 .cmd = NL802154_CMD_SET_LBT_MODE,
2358 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2359 .doit = nl802154_set_lbt_mode,
2360 .flags = GENL_ADMIN_PERM,
2361 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2362 NL802154_FLAG_NEED_RTNL,
2365 .cmd = NL802154_CMD_SET_ACKREQ_DEFAULT,
2366 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2367 .doit = nl802154_set_ackreq_default,
2368 .flags = GENL_ADMIN_PERM,
2369 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2370 NL802154_FLAG_NEED_RTNL,
2372 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
2374 .cmd = NL802154_CMD_SET_SEC_PARAMS,
2375 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2376 .doit = nl802154_set_llsec_params,
2377 .flags = GENL_ADMIN_PERM,
2378 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2379 NL802154_FLAG_NEED_RTNL,
2382 .cmd = NL802154_CMD_GET_SEC_KEY,
2383 .validate = GENL_DONT_VALIDATE_STRICT |
2384 GENL_DONT_VALIDATE_DUMP_STRICT,
2385 /* TODO .doit by matching key id? */
2386 .dumpit = nl802154_dump_llsec_key,
2387 .flags = GENL_ADMIN_PERM,
2388 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2389 NL802154_FLAG_NEED_RTNL,
2392 .cmd = NL802154_CMD_NEW_SEC_KEY,
2393 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2394 .doit = nl802154_add_llsec_key,
2395 .flags = GENL_ADMIN_PERM,
2396 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2397 NL802154_FLAG_NEED_RTNL,
2400 .cmd = NL802154_CMD_DEL_SEC_KEY,
2401 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2402 .doit = nl802154_del_llsec_key,
2403 .flags = GENL_ADMIN_PERM,
2404 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2405 NL802154_FLAG_NEED_RTNL,
2407 /* TODO unique identifier must short+pan OR extended_addr */
2409 .cmd = NL802154_CMD_GET_SEC_DEV,
2410 .validate = GENL_DONT_VALIDATE_STRICT |
2411 GENL_DONT_VALIDATE_DUMP_STRICT,
2412 /* TODO .doit by matching extended_addr? */
2413 .dumpit = nl802154_dump_llsec_dev,
2414 .flags = GENL_ADMIN_PERM,
2415 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2416 NL802154_FLAG_NEED_RTNL,
2419 .cmd = NL802154_CMD_NEW_SEC_DEV,
2420 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2421 .doit = nl802154_add_llsec_dev,
2422 .flags = GENL_ADMIN_PERM,
2423 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2424 NL802154_FLAG_NEED_RTNL,
2427 .cmd = NL802154_CMD_DEL_SEC_DEV,
2428 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2429 .doit = nl802154_del_llsec_dev,
2430 .flags = GENL_ADMIN_PERM,
2431 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2432 NL802154_FLAG_NEED_RTNL,
2434 /* TODO remove complete devkey, put it as nested? */
2436 .cmd = NL802154_CMD_GET_SEC_DEVKEY,
2437 .validate = GENL_DONT_VALIDATE_STRICT |
2438 GENL_DONT_VALIDATE_DUMP_STRICT,
2439 /* TODO doit by matching ??? */
2440 .dumpit = nl802154_dump_llsec_devkey,
2441 .flags = GENL_ADMIN_PERM,
2442 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2443 NL802154_FLAG_NEED_RTNL,
2446 .cmd = NL802154_CMD_NEW_SEC_DEVKEY,
2447 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2448 .doit = nl802154_add_llsec_devkey,
2449 .flags = GENL_ADMIN_PERM,
2450 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2451 NL802154_FLAG_NEED_RTNL,
2454 .cmd = NL802154_CMD_DEL_SEC_DEVKEY,
2455 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2456 .doit = nl802154_del_llsec_devkey,
2457 .flags = GENL_ADMIN_PERM,
2458 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2459 NL802154_FLAG_NEED_RTNL,
2462 .cmd = NL802154_CMD_GET_SEC_LEVEL,
2463 .validate = GENL_DONT_VALIDATE_STRICT |
2464 GENL_DONT_VALIDATE_DUMP_STRICT,
2465 /* TODO .doit by matching frame_type? */
2466 .dumpit = nl802154_dump_llsec_seclevel,
2467 .flags = GENL_ADMIN_PERM,
2468 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2469 NL802154_FLAG_NEED_RTNL,
2472 .cmd = NL802154_CMD_NEW_SEC_LEVEL,
2473 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2474 .doit = nl802154_add_llsec_seclevel,
2475 .flags = GENL_ADMIN_PERM,
2476 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2477 NL802154_FLAG_NEED_RTNL,
2480 .cmd = NL802154_CMD_DEL_SEC_LEVEL,
2481 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2482 /* TODO match frame_type only? */
2483 .doit = nl802154_del_llsec_seclevel,
2484 .flags = GENL_ADMIN_PERM,
2485 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2486 NL802154_FLAG_NEED_RTNL,
2488 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2491 static struct genl_family nl802154_fam __ro_after_init = {
2492 .name = NL802154_GENL_NAME, /* have users key off the name instead */
2493 .hdrsize = 0, /* no private header */
2494 .version = 1, /* no particular meaning now */
2495 .maxattr = NL802154_ATTR_MAX,
2496 .policy = nl802154_policy,
2498 .pre_doit = nl802154_pre_doit,
2499 .post_doit = nl802154_post_doit,
2500 .module = THIS_MODULE,
2501 .ops = nl802154_ops,
2502 .n_ops = ARRAY_SIZE(nl802154_ops),
2503 .mcgrps = nl802154_mcgrps,
2504 .n_mcgrps = ARRAY_SIZE(nl802154_mcgrps),
2507 /* initialisation/exit functions */
2508 int __init nl802154_init(void)
2510 return genl_register_family(&nl802154_fam);
2513 void nl802154_exit(void)
2515 genl_unregister_family(&nl802154_fam);