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 (nl802154_get_llsec_params(msg, rdev, wpan_dev) < 0)
824 goto nla_put_failure;
825 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
827 genlmsg_end(msg, hdr);
831 genlmsg_cancel(msg, hdr);
836 nl802154_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
840 int wp_start = cb->args[0];
841 int if_start = cb->args[1];
842 struct cfg802154_registered_device *rdev;
843 struct wpan_dev *wpan_dev;
846 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
847 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
849 if (wp_idx < wp_start) {
855 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
856 if (if_idx < if_start) {
860 if (nl802154_send_iface(skb, NETLINK_CB(cb->skb).portid,
861 cb->nlh->nlmsg_seq, NLM_F_MULTI,
862 rdev, wpan_dev) < 0) {
873 cb->args[0] = wp_idx;
874 cb->args[1] = if_idx;
879 static int nl802154_get_interface(struct sk_buff *skb, struct genl_info *info)
882 struct cfg802154_registered_device *rdev = info->user_ptr[0];
883 struct wpan_dev *wdev = info->user_ptr[1];
885 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
889 if (nl802154_send_iface(msg, info->snd_portid, info->snd_seq, 0,
895 return genlmsg_reply(msg, info);
898 static int nl802154_new_interface(struct sk_buff *skb, struct genl_info *info)
900 struct cfg802154_registered_device *rdev = info->user_ptr[0];
901 enum nl802154_iftype type = NL802154_IFTYPE_UNSPEC;
902 __le64 extended_addr = cpu_to_le64(0x0000000000000000ULL);
904 /* TODO avoid failing a new interface
905 * creation due to pending removal?
908 if (!info->attrs[NL802154_ATTR_IFNAME])
911 if (info->attrs[NL802154_ATTR_IFTYPE]) {
912 type = nla_get_u32(info->attrs[NL802154_ATTR_IFTYPE]);
913 if (type > NL802154_IFTYPE_MAX ||
914 !(rdev->wpan_phy.supported.iftypes & BIT(type)))
918 if (info->attrs[NL802154_ATTR_EXTENDED_ADDR])
919 extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
921 if (!rdev->ops->add_virtual_intf)
924 return rdev_add_virtual_intf(rdev,
925 nla_data(info->attrs[NL802154_ATTR_IFNAME]),
926 NET_NAME_USER, type, extended_addr);
929 static int nl802154_del_interface(struct sk_buff *skb, struct genl_info *info)
931 struct cfg802154_registered_device *rdev = info->user_ptr[0];
932 struct wpan_dev *wpan_dev = info->user_ptr[1];
934 if (!rdev->ops->del_virtual_intf)
937 /* If we remove a wpan device without a netdev then clear
938 * user_ptr[1] so that nl802154_post_doit won't dereference it
939 * to check if it needs to do dev_put(). Otherwise it crashes
940 * since the wpan_dev has been freed, unlike with a netdev where
941 * we need the dev_put() for the netdev to really be freed.
943 if (!wpan_dev->netdev)
944 info->user_ptr[1] = NULL;
946 return rdev_del_virtual_intf(rdev, wpan_dev);
949 static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info)
951 struct cfg802154_registered_device *rdev = info->user_ptr[0];
954 if (!info->attrs[NL802154_ATTR_PAGE] ||
955 !info->attrs[NL802154_ATTR_CHANNEL])
958 page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
959 channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]);
961 /* check 802.15.4 constraints */
962 if (page > IEEE802154_MAX_PAGE || channel > IEEE802154_MAX_CHANNEL ||
963 !(rdev->wpan_phy.supported.channels[page] & BIT(channel)))
966 return rdev_set_channel(rdev, page, channel);
969 static int nl802154_set_cca_mode(struct sk_buff *skb, struct genl_info *info)
971 struct cfg802154_registered_device *rdev = info->user_ptr[0];
972 struct wpan_phy_cca cca;
974 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE))
977 if (!info->attrs[NL802154_ATTR_CCA_MODE])
980 cca.mode = nla_get_u32(info->attrs[NL802154_ATTR_CCA_MODE]);
981 /* checking 802.15.4 constraints */
982 if (cca.mode < NL802154_CCA_ENERGY ||
983 cca.mode > NL802154_CCA_ATTR_MAX ||
984 !(rdev->wpan_phy.supported.cca_modes & BIT(cca.mode)))
987 if (cca.mode == NL802154_CCA_ENERGY_CARRIER) {
988 if (!info->attrs[NL802154_ATTR_CCA_OPT])
991 cca.opt = nla_get_u32(info->attrs[NL802154_ATTR_CCA_OPT]);
992 if (cca.opt > NL802154_CCA_OPT_ATTR_MAX ||
993 !(rdev->wpan_phy.supported.cca_opts & BIT(cca.opt)))
997 return rdev_set_cca_mode(rdev, &cca);
1000 static int nl802154_set_cca_ed_level(struct sk_buff *skb, struct genl_info *info)
1002 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1006 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL))
1009 if (!info->attrs[NL802154_ATTR_CCA_ED_LEVEL])
1012 ed_level = nla_get_s32(info->attrs[NL802154_ATTR_CCA_ED_LEVEL]);
1014 for (i = 0; i < rdev->wpan_phy.supported.cca_ed_levels_size; i++) {
1015 if (ed_level == rdev->wpan_phy.supported.cca_ed_levels[i])
1016 return rdev_set_cca_ed_level(rdev, ed_level);
1022 static int nl802154_set_tx_power(struct sk_buff *skb, struct genl_info *info)
1024 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1028 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER))
1031 if (!info->attrs[NL802154_ATTR_TX_POWER])
1034 power = nla_get_s32(info->attrs[NL802154_ATTR_TX_POWER]);
1036 for (i = 0; i < rdev->wpan_phy.supported.tx_powers_size; i++) {
1037 if (power == rdev->wpan_phy.supported.tx_powers[i])
1038 return rdev_set_tx_power(rdev, power);
1044 static int nl802154_set_pan_id(struct sk_buff *skb, struct genl_info *info)
1046 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1047 struct net_device *dev = info->user_ptr[1];
1048 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1051 /* conflict here while tx/rx calls */
1052 if (netif_running(dev))
1055 if (wpan_dev->lowpan_dev) {
1056 if (netif_running(wpan_dev->lowpan_dev))
1060 /* don't change address fields on monitor */
1061 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1062 !info->attrs[NL802154_ATTR_PAN_ID])
1065 pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
1068 * I am not sure about to check here on broadcast pan_id.
1069 * Broadcast is a valid setting, comment from 802.15.4:
1070 * If this value is 0xffff, the device is not associated.
1072 * This could useful to simple deassociate an device.
1074 if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
1077 return rdev_set_pan_id(rdev, wpan_dev, pan_id);
1080 static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info)
1082 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1083 struct net_device *dev = info->user_ptr[1];
1084 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1087 /* conflict here while tx/rx calls */
1088 if (netif_running(dev))
1091 if (wpan_dev->lowpan_dev) {
1092 if (netif_running(wpan_dev->lowpan_dev))
1096 /* don't change address fields on monitor */
1097 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1098 !info->attrs[NL802154_ATTR_SHORT_ADDR])
1101 short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
1104 * I am not sure about to check here on broadcast short_addr.
1105 * Broadcast is a valid setting, comment from 802.15.4:
1106 * A value of 0xfffe indicates that the device has
1107 * associated but has not been allocated an address. A
1108 * value of 0xffff indicates that the device does not
1109 * have a short address.
1111 * I think we should allow to set these settings but
1112 * don't allow to allow socket communication with it.
1114 if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
1115 short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
1118 return rdev_set_short_addr(rdev, wpan_dev, short_addr);
1122 nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info)
1124 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1125 struct net_device *dev = info->user_ptr[1];
1126 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1129 /* should be set on netif open inside phy settings */
1130 if (netif_running(dev))
1133 if (!info->attrs[NL802154_ATTR_MIN_BE] ||
1134 !info->attrs[NL802154_ATTR_MAX_BE])
1137 min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]);
1138 max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]);
1140 /* check 802.15.4 constraints */
1141 if (min_be < rdev->wpan_phy.supported.min_minbe ||
1142 min_be > rdev->wpan_phy.supported.max_minbe ||
1143 max_be < rdev->wpan_phy.supported.min_maxbe ||
1144 max_be > rdev->wpan_phy.supported.max_maxbe ||
1148 return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
1152 nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info)
1154 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1155 struct net_device *dev = info->user_ptr[1];
1156 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1157 u8 max_csma_backoffs;
1159 /* conflict here while other running iface settings */
1160 if (netif_running(dev))
1163 if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS])
1166 max_csma_backoffs = nla_get_u8(
1167 info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]);
1169 /* check 802.15.4 constraints */
1170 if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs ||
1171 max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs)
1174 return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
1178 nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info)
1180 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1181 struct net_device *dev = info->user_ptr[1];
1182 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1183 s8 max_frame_retries;
1185 if (netif_running(dev))
1188 if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES])
1191 max_frame_retries = nla_get_s8(
1192 info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]);
1194 /* check 802.15.4 constraints */
1195 if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries ||
1196 max_frame_retries > rdev->wpan_phy.supported.max_frame_retries)
1199 return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
1202 static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info)
1204 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1205 struct net_device *dev = info->user_ptr[1];
1206 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1209 if (netif_running(dev))
1212 if (!info->attrs[NL802154_ATTR_LBT_MODE])
1215 mode = nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
1217 if (mode != 0 && mode != 1)
1220 if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt))
1223 return rdev_set_lbt_mode(rdev, wpan_dev, mode);
1227 nl802154_set_ackreq_default(struct sk_buff *skb, struct genl_info *info)
1229 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1230 struct net_device *dev = info->user_ptr[1];
1231 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1234 if (netif_running(dev))
1237 if (!info->attrs[NL802154_ATTR_ACKREQ_DEFAULT])
1240 ackreq = nla_get_u8(info->attrs[NL802154_ATTR_ACKREQ_DEFAULT]);
1242 if (ackreq != 0 && ackreq != 1)
1245 return rdev_set_ackreq_default(rdev, wpan_dev, ackreq);
1248 static int nl802154_wpan_phy_netns(struct sk_buff *skb, struct genl_info *info)
1250 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1254 if (info->attrs[NL802154_ATTR_PID]) {
1255 u32 pid = nla_get_u32(info->attrs[NL802154_ATTR_PID]);
1257 net = get_net_ns_by_pid(pid);
1258 } else if (info->attrs[NL802154_ATTR_NETNS_FD]) {
1259 u32 fd = nla_get_u32(info->attrs[NL802154_ATTR_NETNS_FD]);
1261 net = get_net_ns_by_fd(fd);
1267 return PTR_ERR(net);
1271 /* check if anything to do */
1272 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), net))
1273 err = cfg802154_switch_netns(rdev, net);
1279 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
1280 static const struct nla_policy nl802154_dev_addr_policy[NL802154_DEV_ADDR_ATTR_MAX + 1] = {
1281 [NL802154_DEV_ADDR_ATTR_PAN_ID] = { .type = NLA_U16 },
1282 [NL802154_DEV_ADDR_ATTR_MODE] = { .type = NLA_U32 },
1283 [NL802154_DEV_ADDR_ATTR_SHORT] = { .type = NLA_U16 },
1284 [NL802154_DEV_ADDR_ATTR_EXTENDED] = { .type = NLA_U64 },
1288 ieee802154_llsec_parse_dev_addr(struct nlattr *nla,
1289 struct ieee802154_addr *addr)
1291 struct nlattr *attrs[NL802154_DEV_ADDR_ATTR_MAX + 1];
1293 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ADDR_ATTR_MAX, nla, nl802154_dev_addr_policy, NULL))
1296 if (!attrs[NL802154_DEV_ADDR_ATTR_PAN_ID] ||
1297 !attrs[NL802154_DEV_ADDR_ATTR_MODE] ||
1298 !(attrs[NL802154_DEV_ADDR_ATTR_SHORT] ||
1299 attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]))
1302 addr->pan_id = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_PAN_ID]);
1303 addr->mode = nla_get_u32(attrs[NL802154_DEV_ADDR_ATTR_MODE]);
1304 switch (addr->mode) {
1305 case NL802154_DEV_ADDR_SHORT:
1306 addr->short_addr = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_SHORT]);
1308 case NL802154_DEV_ADDR_EXTENDED:
1309 addr->extended_addr = nla_get_le64(attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]);
1318 static const struct nla_policy nl802154_key_id_policy[NL802154_KEY_ID_ATTR_MAX + 1] = {
1319 [NL802154_KEY_ID_ATTR_MODE] = { .type = NLA_U32 },
1320 [NL802154_KEY_ID_ATTR_INDEX] = { .type = NLA_U8 },
1321 [NL802154_KEY_ID_ATTR_IMPLICIT] = { .type = NLA_NESTED },
1322 [NL802154_KEY_ID_ATTR_SOURCE_SHORT] = { .type = NLA_U32 },
1323 [NL802154_KEY_ID_ATTR_SOURCE_EXTENDED] = { .type = NLA_U64 },
1327 ieee802154_llsec_parse_key_id(struct nlattr *nla,
1328 struct ieee802154_llsec_key_id *desc)
1330 struct nlattr *attrs[NL802154_KEY_ID_ATTR_MAX + 1];
1332 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_KEY_ID_ATTR_MAX, nla, nl802154_key_id_policy, NULL))
1335 if (!attrs[NL802154_KEY_ID_ATTR_MODE])
1338 desc->mode = nla_get_u32(attrs[NL802154_KEY_ID_ATTR_MODE]);
1339 switch (desc->mode) {
1340 case NL802154_KEY_ID_MODE_IMPLICIT:
1341 if (!attrs[NL802154_KEY_ID_ATTR_IMPLICIT])
1344 if (ieee802154_llsec_parse_dev_addr(attrs[NL802154_KEY_ID_ATTR_IMPLICIT],
1345 &desc->device_addr) < 0)
1348 case NL802154_KEY_ID_MODE_INDEX:
1350 case NL802154_KEY_ID_MODE_INDEX_SHORT:
1351 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT])
1354 desc->short_source = nla_get_le32(attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT]);
1356 case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
1357 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED])
1360 desc->extended_source = nla_get_le64(attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED]);
1366 if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
1367 if (!attrs[NL802154_KEY_ID_ATTR_INDEX])
1370 /* TODO change id to idx */
1371 desc->id = nla_get_u8(attrs[NL802154_KEY_ID_ATTR_INDEX]);
1377 static int nl802154_set_llsec_params(struct sk_buff *skb,
1378 struct genl_info *info)
1380 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1381 struct net_device *dev = info->user_ptr[1];
1382 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1383 struct ieee802154_llsec_params params;
1387 if (info->attrs[NL802154_ATTR_SEC_ENABLED]) {
1390 enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1391 if (enabled != 0 && enabled != 1)
1394 params.enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1395 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
1398 if (info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID]) {
1399 ret = ieee802154_llsec_parse_key_id(info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID],
1404 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
1407 if (info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]) {
1408 params.out_level = nla_get_u32(info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]);
1409 if (params.out_level > NL802154_SECLEVEL_MAX)
1412 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
1415 if (info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]) {
1416 params.frame_counter = nla_get_be32(info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]);
1417 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
1420 return rdev_set_llsec_params(rdev, wpan_dev, ¶ms, changed);
1423 static int nl802154_send_key(struct sk_buff *msg, u32 cmd, u32 portid,
1425 struct cfg802154_registered_device *rdev,
1426 struct net_device *dev,
1427 const struct ieee802154_llsec_key_entry *key)
1430 u32 commands[NL802154_CMD_FRAME_NR_IDS / 32];
1431 struct nlattr *nl_key, *nl_key_id;
1433 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1437 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1438 goto nla_put_failure;
1440 nl_key = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_KEY);
1442 goto nla_put_failure;
1444 nl_key_id = nla_nest_start_noflag(msg, NL802154_KEY_ATTR_ID);
1446 goto nla_put_failure;
1448 if (ieee802154_llsec_send_key_id(msg, &key->id) < 0)
1449 goto nla_put_failure;
1451 nla_nest_end(msg, nl_key_id);
1453 if (nla_put_u8(msg, NL802154_KEY_ATTR_USAGE_FRAMES,
1454 key->key->frame_types))
1455 goto nla_put_failure;
1457 if (key->key->frame_types & BIT(NL802154_FRAME_CMD)) {
1458 /* TODO for each nested */
1459 memset(commands, 0, sizeof(commands));
1460 commands[7] = key->key->cmd_frame_ids;
1461 if (nla_put(msg, NL802154_KEY_ATTR_USAGE_CMDS,
1462 sizeof(commands), commands))
1463 goto nla_put_failure;
1466 if (nla_put(msg, NL802154_KEY_ATTR_BYTES, NL802154_KEY_SIZE,
1468 goto nla_put_failure;
1470 nla_nest_end(msg, nl_key);
1471 genlmsg_end(msg, hdr);
1476 genlmsg_cancel(msg, hdr);
1481 nl802154_dump_llsec_key(struct sk_buff *skb, struct netlink_callback *cb)
1483 struct cfg802154_registered_device *rdev = NULL;
1484 struct ieee802154_llsec_key_entry *key;
1485 struct ieee802154_llsec_table *table;
1486 struct wpan_dev *wpan_dev;
1489 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1493 if (!wpan_dev->netdev) {
1498 rdev_lock_llsec_table(rdev, wpan_dev);
1499 rdev_get_llsec_table(rdev, wpan_dev, &table);
1501 /* TODO make it like station dump */
1505 list_for_each_entry(key, &table->keys, list) {
1506 if (nl802154_send_key(skb, NL802154_CMD_NEW_SEC_KEY,
1507 NETLINK_CB(cb->skb).portid,
1508 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1509 rdev, wpan_dev->netdev, key) < 0) {
1512 rdev_unlock_llsec_table(rdev, wpan_dev);
1520 rdev_unlock_llsec_table(rdev, wpan_dev);
1523 nl802154_finish_wpan_dev_dump(rdev);
1528 static const struct nla_policy nl802154_key_policy[NL802154_KEY_ATTR_MAX + 1] = {
1529 [NL802154_KEY_ATTR_ID] = { NLA_NESTED },
1530 /* TODO handle it as for_each_nested and NLA_FLAG? */
1531 [NL802154_KEY_ATTR_USAGE_FRAMES] = { NLA_U8 },
1532 /* TODO handle it as for_each_nested, not static array? */
1533 [NL802154_KEY_ATTR_USAGE_CMDS] = { .len = NL802154_CMD_FRAME_NR_IDS / 8 },
1534 [NL802154_KEY_ATTR_BYTES] = { .len = NL802154_KEY_SIZE },
1537 static int nl802154_add_llsec_key(struct sk_buff *skb, struct genl_info *info)
1539 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1540 struct net_device *dev = info->user_ptr[1];
1541 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1542 struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1543 struct ieee802154_llsec_key key = { };
1544 struct ieee802154_llsec_key_id id = { };
1545 u32 commands[NL802154_CMD_FRAME_NR_IDS / 32] = { };
1547 if (nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
1550 if (!attrs[NL802154_KEY_ATTR_USAGE_FRAMES] ||
1551 !attrs[NL802154_KEY_ATTR_BYTES])
1554 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1557 key.frame_types = nla_get_u8(attrs[NL802154_KEY_ATTR_USAGE_FRAMES]);
1558 if (key.frame_types > BIT(NL802154_FRAME_MAX) ||
1559 ((key.frame_types & BIT(NL802154_FRAME_CMD)) &&
1560 !attrs[NL802154_KEY_ATTR_USAGE_CMDS]))
1563 if (attrs[NL802154_KEY_ATTR_USAGE_CMDS]) {
1564 /* TODO for each nested */
1565 nla_memcpy(commands, attrs[NL802154_KEY_ATTR_USAGE_CMDS],
1566 NL802154_CMD_FRAME_NR_IDS / 8);
1568 /* TODO understand the -EINVAL logic here? last condition */
1569 if (commands[0] || commands[1] || commands[2] || commands[3] ||
1570 commands[4] || commands[5] || commands[6] ||
1571 commands[7] > BIT(NL802154_CMD_FRAME_MAX))
1574 key.cmd_frame_ids = commands[7];
1576 key.cmd_frame_ids = 0;
1579 nla_memcpy(key.key, attrs[NL802154_KEY_ATTR_BYTES], NL802154_KEY_SIZE);
1581 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1584 return rdev_add_llsec_key(rdev, wpan_dev, &id, &key);
1587 static int nl802154_del_llsec_key(struct sk_buff *skb, struct genl_info *info)
1589 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1590 struct net_device *dev = info->user_ptr[1];
1591 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1592 struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1593 struct ieee802154_llsec_key_id id;
1595 if (nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
1598 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1601 return rdev_del_llsec_key(rdev, wpan_dev, &id);
1604 static int nl802154_send_device(struct sk_buff *msg, u32 cmd, u32 portid,
1606 struct cfg802154_registered_device *rdev,
1607 struct net_device *dev,
1608 const struct ieee802154_llsec_device *dev_desc)
1611 struct nlattr *nl_device;
1613 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1617 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1618 goto nla_put_failure;
1620 nl_device = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVICE);
1622 goto nla_put_failure;
1624 if (nla_put_u32(msg, NL802154_DEV_ATTR_FRAME_COUNTER,
1625 dev_desc->frame_counter) ||
1626 nla_put_le16(msg, NL802154_DEV_ATTR_PAN_ID, dev_desc->pan_id) ||
1627 nla_put_le16(msg, NL802154_DEV_ATTR_SHORT_ADDR,
1628 dev_desc->short_addr) ||
1629 nla_put_le64(msg, NL802154_DEV_ATTR_EXTENDED_ADDR,
1630 dev_desc->hwaddr, NL802154_DEV_ATTR_PAD) ||
1631 nla_put_u8(msg, NL802154_DEV_ATTR_SECLEVEL_EXEMPT,
1632 dev_desc->seclevel_exempt) ||
1633 nla_put_u32(msg, NL802154_DEV_ATTR_KEY_MODE, dev_desc->key_mode))
1634 goto nla_put_failure;
1636 nla_nest_end(msg, nl_device);
1637 genlmsg_end(msg, hdr);
1642 genlmsg_cancel(msg, hdr);
1647 nl802154_dump_llsec_dev(struct sk_buff *skb, struct netlink_callback *cb)
1649 struct cfg802154_registered_device *rdev = NULL;
1650 struct ieee802154_llsec_device *dev;
1651 struct ieee802154_llsec_table *table;
1652 struct wpan_dev *wpan_dev;
1655 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1659 if (!wpan_dev->netdev) {
1664 rdev_lock_llsec_table(rdev, wpan_dev);
1665 rdev_get_llsec_table(rdev, wpan_dev, &table);
1667 /* TODO make it like station dump */
1671 list_for_each_entry(dev, &table->devices, list) {
1672 if (nl802154_send_device(skb, NL802154_CMD_NEW_SEC_LEVEL,
1673 NETLINK_CB(cb->skb).portid,
1674 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1675 rdev, wpan_dev->netdev, dev) < 0) {
1678 rdev_unlock_llsec_table(rdev, wpan_dev);
1686 rdev_unlock_llsec_table(rdev, wpan_dev);
1689 nl802154_finish_wpan_dev_dump(rdev);
1694 static const struct nla_policy nl802154_dev_policy[NL802154_DEV_ATTR_MAX + 1] = {
1695 [NL802154_DEV_ATTR_FRAME_COUNTER] = { NLA_U32 },
1696 [NL802154_DEV_ATTR_PAN_ID] = { .type = NLA_U16 },
1697 [NL802154_DEV_ATTR_SHORT_ADDR] = { .type = NLA_U16 },
1698 [NL802154_DEV_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
1699 [NL802154_DEV_ATTR_SECLEVEL_EXEMPT] = { NLA_U8 },
1700 [NL802154_DEV_ATTR_KEY_MODE] = { NLA_U32 },
1704 ieee802154_llsec_parse_device(struct nlattr *nla,
1705 struct ieee802154_llsec_device *dev)
1707 struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
1709 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, nla, nl802154_dev_policy, NULL))
1712 memset(dev, 0, sizeof(*dev));
1714 if (!attrs[NL802154_DEV_ATTR_FRAME_COUNTER] ||
1715 !attrs[NL802154_DEV_ATTR_PAN_ID] ||
1716 !attrs[NL802154_DEV_ATTR_SHORT_ADDR] ||
1717 !attrs[NL802154_DEV_ATTR_EXTENDED_ADDR] ||
1718 !attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] ||
1719 !attrs[NL802154_DEV_ATTR_KEY_MODE])
1723 dev->frame_counter = nla_get_u32(attrs[NL802154_DEV_ATTR_FRAME_COUNTER]);
1724 dev->pan_id = nla_get_le16(attrs[NL802154_DEV_ATTR_PAN_ID]);
1725 dev->short_addr = nla_get_le16(attrs[NL802154_DEV_ATTR_SHORT_ADDR]);
1726 /* TODO rename hwaddr to extended_addr */
1727 dev->hwaddr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
1728 dev->seclevel_exempt = nla_get_u8(attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT]);
1729 dev->key_mode = nla_get_u32(attrs[NL802154_DEV_ATTR_KEY_MODE]);
1731 if (dev->key_mode > NL802154_DEVKEY_MAX ||
1732 (dev->seclevel_exempt != 0 && dev->seclevel_exempt != 1))
1738 static int nl802154_add_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1740 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1741 struct net_device *dev = info->user_ptr[1];
1742 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1743 struct ieee802154_llsec_device dev_desc;
1745 if (ieee802154_llsec_parse_device(info->attrs[NL802154_ATTR_SEC_DEVICE],
1749 return rdev_add_device(rdev, wpan_dev, &dev_desc);
1752 static int nl802154_del_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1754 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1755 struct net_device *dev = info->user_ptr[1];
1756 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1757 struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
1758 __le64 extended_addr;
1760 if (nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVICE], nl802154_dev_policy, info->extack))
1763 if (!attrs[NL802154_DEV_ATTR_EXTENDED_ADDR])
1766 extended_addr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
1767 return rdev_del_device(rdev, wpan_dev, extended_addr);
1770 static int nl802154_send_devkey(struct sk_buff *msg, u32 cmd, u32 portid,
1772 struct cfg802154_registered_device *rdev,
1773 struct net_device *dev, __le64 extended_addr,
1774 const struct ieee802154_llsec_device_key *devkey)
1777 struct nlattr *nl_devkey, *nl_key_id;
1779 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1783 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1784 goto nla_put_failure;
1786 nl_devkey = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVKEY);
1788 goto nla_put_failure;
1790 if (nla_put_le64(msg, NL802154_DEVKEY_ATTR_EXTENDED_ADDR,
1791 extended_addr, NL802154_DEVKEY_ATTR_PAD) ||
1792 nla_put_u32(msg, NL802154_DEVKEY_ATTR_FRAME_COUNTER,
1793 devkey->frame_counter))
1794 goto nla_put_failure;
1796 nl_key_id = nla_nest_start_noflag(msg, NL802154_DEVKEY_ATTR_ID);
1798 goto nla_put_failure;
1800 if (ieee802154_llsec_send_key_id(msg, &devkey->key_id) < 0)
1801 goto nla_put_failure;
1803 nla_nest_end(msg, nl_key_id);
1804 nla_nest_end(msg, nl_devkey);
1805 genlmsg_end(msg, hdr);
1810 genlmsg_cancel(msg, hdr);
1815 nl802154_dump_llsec_devkey(struct sk_buff *skb, struct netlink_callback *cb)
1817 struct cfg802154_registered_device *rdev = NULL;
1818 struct ieee802154_llsec_device_key *kpos;
1819 struct ieee802154_llsec_device *dpos;
1820 struct ieee802154_llsec_table *table;
1821 struct wpan_dev *wpan_dev;
1824 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1828 if (!wpan_dev->netdev) {
1833 rdev_lock_llsec_table(rdev, wpan_dev);
1834 rdev_get_llsec_table(rdev, wpan_dev, &table);
1836 /* TODO make it like station dump */
1840 /* TODO look if remove devkey and do some nested attribute */
1841 list_for_each_entry(dpos, &table->devices, list) {
1842 list_for_each_entry(kpos, &dpos->keys, list) {
1843 if (nl802154_send_devkey(skb,
1844 NL802154_CMD_NEW_SEC_LEVEL,
1845 NETLINK_CB(cb->skb).portid,
1853 rdev_unlock_llsec_table(rdev, wpan_dev);
1862 rdev_unlock_llsec_table(rdev, wpan_dev);
1865 nl802154_finish_wpan_dev_dump(rdev);
1870 static const struct nla_policy nl802154_devkey_policy[NL802154_DEVKEY_ATTR_MAX + 1] = {
1871 [NL802154_DEVKEY_ATTR_FRAME_COUNTER] = { NLA_U32 },
1872 [NL802154_DEVKEY_ATTR_EXTENDED_ADDR] = { NLA_U64 },
1873 [NL802154_DEVKEY_ATTR_ID] = { NLA_NESTED },
1876 static int nl802154_add_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
1878 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1879 struct net_device *dev = info->user_ptr[1];
1880 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1881 struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
1882 struct ieee802154_llsec_device_key key;
1883 __le64 extended_addr;
1885 if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
1886 nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack) < 0)
1889 if (!attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER] ||
1890 !attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1893 /* TODO change key.id ? */
1894 if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1899 key.frame_counter = nla_get_u32(attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER]);
1900 /* TODO change naming hwaddr -> extended_addr
1901 * check unique identifier short+pan OR extended_addr
1903 extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1904 return rdev_add_devkey(rdev, wpan_dev, extended_addr, &key);
1907 static int nl802154_del_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
1909 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1910 struct net_device *dev = info->user_ptr[1];
1911 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1912 struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
1913 struct ieee802154_llsec_device_key key;
1914 __le64 extended_addr;
1916 if (nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack))
1919 if (!attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1922 /* TODO change key.id ? */
1923 if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1927 /* TODO change naming hwaddr -> extended_addr
1928 * check unique identifier short+pan OR extended_addr
1930 extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1931 return rdev_del_devkey(rdev, wpan_dev, extended_addr, &key);
1934 static int nl802154_send_seclevel(struct sk_buff *msg, u32 cmd, u32 portid,
1936 struct cfg802154_registered_device *rdev,
1937 struct net_device *dev,
1938 const struct ieee802154_llsec_seclevel *sl)
1941 struct nlattr *nl_seclevel;
1943 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1947 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1948 goto nla_put_failure;
1950 nl_seclevel = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_LEVEL);
1952 goto nla_put_failure;
1954 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_FRAME, sl->frame_type) ||
1955 nla_put_u32(msg, NL802154_SECLEVEL_ATTR_LEVELS, sl->sec_levels) ||
1956 nla_put_u8(msg, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE,
1957 sl->device_override))
1958 goto nla_put_failure;
1960 if (sl->frame_type == NL802154_FRAME_CMD) {
1961 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_CMD_FRAME,
1963 goto nla_put_failure;
1966 nla_nest_end(msg, nl_seclevel);
1967 genlmsg_end(msg, hdr);
1972 genlmsg_cancel(msg, hdr);
1977 nl802154_dump_llsec_seclevel(struct sk_buff *skb, struct netlink_callback *cb)
1979 struct cfg802154_registered_device *rdev = NULL;
1980 struct ieee802154_llsec_seclevel *sl;
1981 struct ieee802154_llsec_table *table;
1982 struct wpan_dev *wpan_dev;
1985 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1989 if (!wpan_dev->netdev) {
1994 rdev_lock_llsec_table(rdev, wpan_dev);
1995 rdev_get_llsec_table(rdev, wpan_dev, &table);
1997 /* TODO make it like station dump */
2001 list_for_each_entry(sl, &table->security_levels, list) {
2002 if (nl802154_send_seclevel(skb, NL802154_CMD_NEW_SEC_LEVEL,
2003 NETLINK_CB(cb->skb).portid,
2004 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2005 rdev, wpan_dev->netdev, sl) < 0) {
2008 rdev_unlock_llsec_table(rdev, wpan_dev);
2016 rdev_unlock_llsec_table(rdev, wpan_dev);
2019 nl802154_finish_wpan_dev_dump(rdev);
2024 static const struct nla_policy nl802154_seclevel_policy[NL802154_SECLEVEL_ATTR_MAX + 1] = {
2025 [NL802154_SECLEVEL_ATTR_LEVELS] = { .type = NLA_U8 },
2026 [NL802154_SECLEVEL_ATTR_FRAME] = { .type = NLA_U32 },
2027 [NL802154_SECLEVEL_ATTR_CMD_FRAME] = { .type = NLA_U32 },
2028 [NL802154_SECLEVEL_ATTR_DEV_OVERRIDE] = { .type = NLA_U8 },
2032 llsec_parse_seclevel(struct nlattr *nla, struct ieee802154_llsec_seclevel *sl)
2034 struct nlattr *attrs[NL802154_SECLEVEL_ATTR_MAX + 1];
2036 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_SECLEVEL_ATTR_MAX, nla, nl802154_seclevel_policy, NULL))
2039 memset(sl, 0, sizeof(*sl));
2041 if (!attrs[NL802154_SECLEVEL_ATTR_LEVELS] ||
2042 !attrs[NL802154_SECLEVEL_ATTR_FRAME] ||
2043 !attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE])
2046 sl->sec_levels = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_LEVELS]);
2047 sl->frame_type = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_FRAME]);
2048 sl->device_override = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE]);
2049 if (sl->frame_type > NL802154_FRAME_MAX ||
2050 (sl->device_override != 0 && sl->device_override != 1))
2053 if (sl->frame_type == NL802154_FRAME_CMD) {
2054 if (!attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME])
2057 sl->cmd_frame_id = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME]);
2058 if (sl->cmd_frame_id > NL802154_CMD_FRAME_MAX)
2065 static int nl802154_add_llsec_seclevel(struct sk_buff *skb,
2066 struct genl_info *info)
2068 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2069 struct net_device *dev = info->user_ptr[1];
2070 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2071 struct ieee802154_llsec_seclevel sl;
2073 if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2077 return rdev_add_seclevel(rdev, wpan_dev, &sl);
2080 static int nl802154_del_llsec_seclevel(struct sk_buff *skb,
2081 struct genl_info *info)
2083 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2084 struct net_device *dev = info->user_ptr[1];
2085 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2086 struct ieee802154_llsec_seclevel sl;
2088 if (!info->attrs[NL802154_ATTR_SEC_LEVEL] ||
2089 llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2093 return rdev_del_seclevel(rdev, wpan_dev, &sl);
2095 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2097 #define NL802154_FLAG_NEED_WPAN_PHY 0x01
2098 #define NL802154_FLAG_NEED_NETDEV 0x02
2099 #define NL802154_FLAG_NEED_RTNL 0x04
2100 #define NL802154_FLAG_CHECK_NETDEV_UP 0x08
2101 #define NL802154_FLAG_NEED_NETDEV_UP (NL802154_FLAG_NEED_NETDEV |\
2102 NL802154_FLAG_CHECK_NETDEV_UP)
2103 #define NL802154_FLAG_NEED_WPAN_DEV 0x10
2104 #define NL802154_FLAG_NEED_WPAN_DEV_UP (NL802154_FLAG_NEED_WPAN_DEV |\
2105 NL802154_FLAG_CHECK_NETDEV_UP)
2107 static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
2108 struct genl_info *info)
2110 struct cfg802154_registered_device *rdev;
2111 struct wpan_dev *wpan_dev;
2112 struct net_device *dev;
2113 bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
2118 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
2119 rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
2123 return PTR_ERR(rdev);
2125 info->user_ptr[0] = rdev;
2126 } else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
2127 ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2129 wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
2131 if (IS_ERR(wpan_dev)) {
2134 return PTR_ERR(wpan_dev);
2137 dev = wpan_dev->netdev;
2138 rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
2140 if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
2147 info->user_ptr[1] = dev;
2149 info->user_ptr[1] = wpan_dev;
2153 if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
2154 !netif_running(dev)) {
2163 info->user_ptr[0] = rdev;
2169 static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
2170 struct genl_info *info)
2172 if (info->user_ptr[1]) {
2173 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2174 struct wpan_dev *wpan_dev = info->user_ptr[1];
2176 if (wpan_dev->netdev)
2177 dev_put(wpan_dev->netdev);
2179 dev_put(info->user_ptr[1]);
2183 if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
2187 static const struct genl_ops nl802154_ops[] = {
2189 .cmd = NL802154_CMD_GET_WPAN_PHY,
2190 .validate = GENL_DONT_VALIDATE_STRICT |
2191 GENL_DONT_VALIDATE_DUMP_STRICT,
2192 .doit = nl802154_get_wpan_phy,
2193 .dumpit = nl802154_dump_wpan_phy,
2194 .done = nl802154_dump_wpan_phy_done,
2195 /* can be retrieved by unprivileged users */
2196 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2197 NL802154_FLAG_NEED_RTNL,
2200 .cmd = NL802154_CMD_GET_INTERFACE,
2201 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2202 .doit = nl802154_get_interface,
2203 .dumpit = nl802154_dump_interface,
2204 /* can be retrieved by unprivileged users */
2205 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2206 NL802154_FLAG_NEED_RTNL,
2209 .cmd = NL802154_CMD_NEW_INTERFACE,
2210 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2211 .doit = nl802154_new_interface,
2212 .flags = GENL_ADMIN_PERM,
2213 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2214 NL802154_FLAG_NEED_RTNL,
2217 .cmd = NL802154_CMD_DEL_INTERFACE,
2218 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2219 .doit = nl802154_del_interface,
2220 .flags = GENL_ADMIN_PERM,
2221 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2222 NL802154_FLAG_NEED_RTNL,
2225 .cmd = NL802154_CMD_SET_CHANNEL,
2226 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2227 .doit = nl802154_set_channel,
2228 .flags = GENL_ADMIN_PERM,
2229 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2230 NL802154_FLAG_NEED_RTNL,
2233 .cmd = NL802154_CMD_SET_CCA_MODE,
2234 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2235 .doit = nl802154_set_cca_mode,
2236 .flags = GENL_ADMIN_PERM,
2237 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2238 NL802154_FLAG_NEED_RTNL,
2241 .cmd = NL802154_CMD_SET_CCA_ED_LEVEL,
2242 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2243 .doit = nl802154_set_cca_ed_level,
2244 .flags = GENL_ADMIN_PERM,
2245 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2246 NL802154_FLAG_NEED_RTNL,
2249 .cmd = NL802154_CMD_SET_TX_POWER,
2250 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2251 .doit = nl802154_set_tx_power,
2252 .flags = GENL_ADMIN_PERM,
2253 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2254 NL802154_FLAG_NEED_RTNL,
2257 .cmd = NL802154_CMD_SET_WPAN_PHY_NETNS,
2258 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2259 .doit = nl802154_wpan_phy_netns,
2260 .flags = GENL_ADMIN_PERM,
2261 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2262 NL802154_FLAG_NEED_RTNL,
2265 .cmd = NL802154_CMD_SET_PAN_ID,
2266 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2267 .doit = nl802154_set_pan_id,
2268 .flags = GENL_ADMIN_PERM,
2269 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2270 NL802154_FLAG_NEED_RTNL,
2273 .cmd = NL802154_CMD_SET_SHORT_ADDR,
2274 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2275 .doit = nl802154_set_short_addr,
2276 .flags = GENL_ADMIN_PERM,
2277 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2278 NL802154_FLAG_NEED_RTNL,
2281 .cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
2282 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2283 .doit = nl802154_set_backoff_exponent,
2284 .flags = GENL_ADMIN_PERM,
2285 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2286 NL802154_FLAG_NEED_RTNL,
2289 .cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
2290 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2291 .doit = nl802154_set_max_csma_backoffs,
2292 .flags = GENL_ADMIN_PERM,
2293 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2294 NL802154_FLAG_NEED_RTNL,
2297 .cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
2298 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2299 .doit = nl802154_set_max_frame_retries,
2300 .flags = GENL_ADMIN_PERM,
2301 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2302 NL802154_FLAG_NEED_RTNL,
2305 .cmd = NL802154_CMD_SET_LBT_MODE,
2306 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2307 .doit = nl802154_set_lbt_mode,
2308 .flags = GENL_ADMIN_PERM,
2309 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2310 NL802154_FLAG_NEED_RTNL,
2313 .cmd = NL802154_CMD_SET_ACKREQ_DEFAULT,
2314 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2315 .doit = nl802154_set_ackreq_default,
2316 .flags = GENL_ADMIN_PERM,
2317 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2318 NL802154_FLAG_NEED_RTNL,
2320 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
2322 .cmd = NL802154_CMD_SET_SEC_PARAMS,
2323 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2324 .doit = nl802154_set_llsec_params,
2325 .flags = GENL_ADMIN_PERM,
2326 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2327 NL802154_FLAG_NEED_RTNL,
2330 .cmd = NL802154_CMD_GET_SEC_KEY,
2331 .validate = GENL_DONT_VALIDATE_STRICT |
2332 GENL_DONT_VALIDATE_DUMP_STRICT,
2333 /* TODO .doit by matching key id? */
2334 .dumpit = nl802154_dump_llsec_key,
2335 .flags = GENL_ADMIN_PERM,
2336 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2337 NL802154_FLAG_NEED_RTNL,
2340 .cmd = NL802154_CMD_NEW_SEC_KEY,
2341 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2342 .doit = nl802154_add_llsec_key,
2343 .flags = GENL_ADMIN_PERM,
2344 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2345 NL802154_FLAG_NEED_RTNL,
2348 .cmd = NL802154_CMD_DEL_SEC_KEY,
2349 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2350 .doit = nl802154_del_llsec_key,
2351 .flags = GENL_ADMIN_PERM,
2352 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2353 NL802154_FLAG_NEED_RTNL,
2355 /* TODO unique identifier must short+pan OR extended_addr */
2357 .cmd = NL802154_CMD_GET_SEC_DEV,
2358 .validate = GENL_DONT_VALIDATE_STRICT |
2359 GENL_DONT_VALIDATE_DUMP_STRICT,
2360 /* TODO .doit by matching extended_addr? */
2361 .dumpit = nl802154_dump_llsec_dev,
2362 .flags = GENL_ADMIN_PERM,
2363 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2364 NL802154_FLAG_NEED_RTNL,
2367 .cmd = NL802154_CMD_NEW_SEC_DEV,
2368 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2369 .doit = nl802154_add_llsec_dev,
2370 .flags = GENL_ADMIN_PERM,
2371 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2372 NL802154_FLAG_NEED_RTNL,
2375 .cmd = NL802154_CMD_DEL_SEC_DEV,
2376 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2377 .doit = nl802154_del_llsec_dev,
2378 .flags = GENL_ADMIN_PERM,
2379 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2380 NL802154_FLAG_NEED_RTNL,
2382 /* TODO remove complete devkey, put it as nested? */
2384 .cmd = NL802154_CMD_GET_SEC_DEVKEY,
2385 .validate = GENL_DONT_VALIDATE_STRICT |
2386 GENL_DONT_VALIDATE_DUMP_STRICT,
2387 /* TODO doit by matching ??? */
2388 .dumpit = nl802154_dump_llsec_devkey,
2389 .flags = GENL_ADMIN_PERM,
2390 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2391 NL802154_FLAG_NEED_RTNL,
2394 .cmd = NL802154_CMD_NEW_SEC_DEVKEY,
2395 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2396 .doit = nl802154_add_llsec_devkey,
2397 .flags = GENL_ADMIN_PERM,
2398 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2399 NL802154_FLAG_NEED_RTNL,
2402 .cmd = NL802154_CMD_DEL_SEC_DEVKEY,
2403 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2404 .doit = nl802154_del_llsec_devkey,
2405 .flags = GENL_ADMIN_PERM,
2406 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2407 NL802154_FLAG_NEED_RTNL,
2410 .cmd = NL802154_CMD_GET_SEC_LEVEL,
2411 .validate = GENL_DONT_VALIDATE_STRICT |
2412 GENL_DONT_VALIDATE_DUMP_STRICT,
2413 /* TODO .doit by matching frame_type? */
2414 .dumpit = nl802154_dump_llsec_seclevel,
2415 .flags = GENL_ADMIN_PERM,
2416 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2417 NL802154_FLAG_NEED_RTNL,
2420 .cmd = NL802154_CMD_NEW_SEC_LEVEL,
2421 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2422 .doit = nl802154_add_llsec_seclevel,
2423 .flags = GENL_ADMIN_PERM,
2424 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2425 NL802154_FLAG_NEED_RTNL,
2428 .cmd = NL802154_CMD_DEL_SEC_LEVEL,
2429 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2430 /* TODO match frame_type only? */
2431 .doit = nl802154_del_llsec_seclevel,
2432 .flags = GENL_ADMIN_PERM,
2433 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2434 NL802154_FLAG_NEED_RTNL,
2436 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2439 static struct genl_family nl802154_fam __ro_after_init = {
2440 .name = NL802154_GENL_NAME, /* have users key off the name instead */
2441 .hdrsize = 0, /* no private header */
2442 .version = 1, /* no particular meaning now */
2443 .maxattr = NL802154_ATTR_MAX,
2444 .policy = nl802154_policy,
2446 .pre_doit = nl802154_pre_doit,
2447 .post_doit = nl802154_post_doit,
2448 .module = THIS_MODULE,
2449 .ops = nl802154_ops,
2450 .n_ops = ARRAY_SIZE(nl802154_ops),
2451 .mcgrps = nl802154_mcgrps,
2452 .n_mcgrps = ARRAY_SIZE(nl802154_mcgrps),
2455 /* initialisation/exit functions */
2456 int __init nl802154_init(void)
2458 return genl_register_family(&nl802154_fam);
2461 void nl802154_exit(void)
2463 genl_unregister_family(&nl802154_fam);