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,
32 static const struct genl_multicast_group nl802154_mcgrps[] = {
33 [NL802154_MCGRP_CONFIG] = { .name = "config", },
34 [NL802154_MCGRP_SCAN] = { .name = "scan", },
37 /* returns ERR_PTR values */
38 static struct wpan_dev *
39 __cfg802154_wpan_dev_from_attrs(struct net *netns, struct nlattr **attrs)
41 struct cfg802154_registered_device *rdev;
42 struct wpan_dev *result = NULL;
43 bool have_ifidx = attrs[NL802154_ATTR_IFINDEX];
44 bool have_wpan_dev_id = attrs[NL802154_ATTR_WPAN_DEV];
46 int wpan_phy_idx = -1;
51 if (!have_ifidx && !have_wpan_dev_id)
52 return ERR_PTR(-EINVAL);
55 ifidx = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
56 if (have_wpan_dev_id) {
57 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
58 wpan_phy_idx = wpan_dev_id >> 32;
61 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
62 struct wpan_dev *wpan_dev;
64 if (wpan_phy_net(&rdev->wpan_phy) != netns)
67 if (have_wpan_dev_id && rdev->wpan_phy_idx != wpan_phy_idx)
70 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
71 if (have_ifidx && wpan_dev->netdev &&
72 wpan_dev->netdev->ifindex == ifidx) {
76 if (have_wpan_dev_id &&
77 wpan_dev->identifier == (u32)wpan_dev_id) {
90 return ERR_PTR(-ENODEV);
93 static struct cfg802154_registered_device *
94 __cfg802154_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
96 struct cfg802154_registered_device *rdev = NULL, *tmp;
97 struct net_device *netdev;
101 if (!attrs[NL802154_ATTR_WPAN_PHY] &&
102 !attrs[NL802154_ATTR_IFINDEX] &&
103 !attrs[NL802154_ATTR_WPAN_DEV])
104 return ERR_PTR(-EINVAL);
106 if (attrs[NL802154_ATTR_WPAN_PHY])
107 rdev = cfg802154_rdev_by_wpan_phy_idx(
108 nla_get_u32(attrs[NL802154_ATTR_WPAN_PHY]));
110 if (attrs[NL802154_ATTR_WPAN_DEV]) {
111 u64 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
112 struct wpan_dev *wpan_dev;
115 tmp = cfg802154_rdev_by_wpan_phy_idx(wpan_dev_id >> 32);
117 /* make sure wpan_dev exists */
118 list_for_each_entry(wpan_dev, &tmp->wpan_dev_list, list) {
119 if (wpan_dev->identifier != (u32)wpan_dev_id)
128 if (rdev && tmp != rdev)
129 return ERR_PTR(-EINVAL);
134 if (attrs[NL802154_ATTR_IFINDEX]) {
135 int ifindex = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
137 netdev = __dev_get_by_index(netns, ifindex);
139 if (netdev->ieee802154_ptr)
140 tmp = wpan_phy_to_rdev(
141 netdev->ieee802154_ptr->wpan_phy);
145 /* not wireless device -- return error */
147 return ERR_PTR(-EINVAL);
149 /* mismatch -- return error */
150 if (rdev && tmp != rdev)
151 return ERR_PTR(-EINVAL);
158 return ERR_PTR(-ENODEV);
160 if (netns != wpan_phy_net(&rdev->wpan_phy))
161 return ERR_PTR(-ENODEV);
166 /* This function returns a pointer to the driver
167 * that the genl_info item that is passed refers to.
169 * The result of this can be a PTR_ERR and hence must
170 * be checked with IS_ERR() for errors.
172 static struct cfg802154_registered_device *
173 cfg802154_get_dev_from_info(struct net *netns, struct genl_info *info)
175 return __cfg802154_rdev_from_attrs(netns, info->attrs);
178 /* policy for the attributes */
179 static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
180 [NL802154_ATTR_WPAN_PHY] = { .type = NLA_U32 },
181 [NL802154_ATTR_WPAN_PHY_NAME] = { .type = NLA_NUL_STRING,
184 [NL802154_ATTR_IFINDEX] = { .type = NLA_U32 },
185 [NL802154_ATTR_IFTYPE] = { .type = NLA_U32 },
186 [NL802154_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
188 [NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
190 [NL802154_ATTR_PAGE] = NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_PAGE),
191 [NL802154_ATTR_CHANNEL] = NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_CHANNEL),
193 [NL802154_ATTR_TX_POWER] = { .type = NLA_S32, },
195 [NL802154_ATTR_CCA_MODE] = { .type = NLA_U32, },
196 [NL802154_ATTR_CCA_OPT] = { .type = NLA_U32, },
197 [NL802154_ATTR_CCA_ED_LEVEL] = { .type = NLA_S32, },
199 [NL802154_ATTR_SUPPORTED_CHANNEL] = { .type = NLA_U32, },
201 [NL802154_ATTR_PAN_ID] = { .type = NLA_U16, },
202 [NL802154_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
203 [NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16, },
205 [NL802154_ATTR_MIN_BE] = { .type = NLA_U8, },
206 [NL802154_ATTR_MAX_BE] = { .type = NLA_U8, },
207 [NL802154_ATTR_MAX_CSMA_BACKOFFS] = { .type = NLA_U8, },
209 [NL802154_ATTR_MAX_FRAME_RETRIES] = { .type = NLA_S8, },
211 [NL802154_ATTR_LBT_MODE] = { .type = NLA_U8, },
213 [NL802154_ATTR_WPAN_PHY_CAPS] = { .type = NLA_NESTED },
215 [NL802154_ATTR_SUPPORTED_COMMANDS] = { .type = NLA_NESTED },
217 [NL802154_ATTR_ACKREQ_DEFAULT] = { .type = NLA_U8 },
219 [NL802154_ATTR_PID] = { .type = NLA_U32 },
220 [NL802154_ATTR_NETNS_FD] = { .type = NLA_U32 },
222 [NL802154_ATTR_COORDINATOR] = { .type = NLA_NESTED },
224 [NL802154_ATTR_SCAN_TYPE] =
225 NLA_POLICY_RANGE(NLA_U8, NL802154_SCAN_ED, NL802154_SCAN_RIT_PASSIVE),
226 [NL802154_ATTR_SCAN_CHANNELS] =
227 NLA_POLICY_MASK(NLA_U32, GENMASK(IEEE802154_MAX_CHANNEL, 0)),
228 [NL802154_ATTR_SCAN_PREAMBLE_CODES] = { .type = NLA_REJECT },
229 [NL802154_ATTR_SCAN_MEAN_PRF] = { .type = NLA_REJECT },
230 [NL802154_ATTR_SCAN_DURATION] =
231 NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_SCAN_DURATION),
232 [NL802154_ATTR_SCAN_DONE_REASON] =
233 NLA_POLICY_RANGE(NLA_U8, NL802154_SCAN_DONE_REASON_FINISHED,
234 NL802154_SCAN_DONE_REASON_ABORTED),
235 [NL802154_ATTR_BEACON_INTERVAL] =
236 NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_SCAN_DURATION),
238 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
239 [NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
240 [NL802154_ATTR_SEC_OUT_LEVEL] = { .type = NLA_U32, },
241 [NL802154_ATTR_SEC_OUT_KEY_ID] = { .type = NLA_NESTED, },
242 [NL802154_ATTR_SEC_FRAME_COUNTER] = { .type = NLA_U32 },
244 [NL802154_ATTR_SEC_LEVEL] = { .type = NLA_NESTED },
245 [NL802154_ATTR_SEC_DEVICE] = { .type = NLA_NESTED },
246 [NL802154_ATTR_SEC_DEVKEY] = { .type = NLA_NESTED },
247 [NL802154_ATTR_SEC_KEY] = { .type = NLA_NESTED },
248 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
251 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
253 nl802154_prepare_wpan_dev_dump(struct sk_buff *skb,
254 struct netlink_callback *cb,
255 struct cfg802154_registered_device **rdev,
256 struct wpan_dev **wpan_dev)
258 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
264 *wpan_dev = __cfg802154_wpan_dev_from_attrs(sock_net(skb->sk),
266 if (IS_ERR(*wpan_dev)) {
267 err = PTR_ERR(*wpan_dev);
270 *rdev = wpan_phy_to_rdev((*wpan_dev)->wpan_phy);
271 /* 0 is the first index - add 1 to parse only once */
272 cb->args[0] = (*rdev)->wpan_phy_idx + 1;
273 cb->args[1] = (*wpan_dev)->identifier;
275 /* subtract the 1 again here */
276 struct wpan_phy *wpan_phy = wpan_phy_idx_to_wpan_phy(cb->args[0] - 1);
277 struct wpan_dev *tmp;
283 *rdev = wpan_phy_to_rdev(wpan_phy);
286 list_for_each_entry(tmp, &(*rdev)->wpan_dev_list, list) {
287 if (tmp->identifier == cb->args[1]) {
306 nl802154_finish_wpan_dev_dump(struct cfg802154_registered_device *rdev)
310 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
312 /* message building helper */
313 static inline void *nl802154hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
316 /* since there is no private header just add the generic one */
317 return genlmsg_put(skb, portid, seq, &nl802154_fam, flags, cmd);
321 nl802154_put_flags(struct sk_buff *msg, int attr, u32 mask)
323 struct nlattr *nl_flags = nla_nest_start_noflag(msg, attr);
331 if ((mask & 1) && nla_put_flag(msg, i))
338 nla_nest_end(msg, nl_flags);
343 nl802154_send_wpan_phy_channels(struct cfg802154_registered_device *rdev,
346 struct nlattr *nl_page;
349 nl_page = nla_nest_start_noflag(msg, NL802154_ATTR_CHANNELS_SUPPORTED);
353 for (page = 0; page <= IEEE802154_MAX_PAGE; page++) {
354 if (nla_put_u32(msg, NL802154_ATTR_SUPPORTED_CHANNEL,
355 rdev->wpan_phy.supported.channels[page]))
358 nla_nest_end(msg, nl_page);
364 nl802154_put_capabilities(struct sk_buff *msg,
365 struct cfg802154_registered_device *rdev)
367 const struct wpan_phy_supported *caps = &rdev->wpan_phy.supported;
368 struct nlattr *nl_caps, *nl_channels;
371 nl_caps = nla_nest_start_noflag(msg, NL802154_ATTR_WPAN_PHY_CAPS);
375 nl_channels = nla_nest_start_noflag(msg, NL802154_CAP_ATTR_CHANNELS);
379 for (i = 0; i <= IEEE802154_MAX_PAGE; i++) {
380 if (caps->channels[i]) {
381 if (nl802154_put_flags(msg, i, caps->channels[i]))
386 nla_nest_end(msg, nl_channels);
388 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
389 struct nlattr *nl_ed_lvls;
391 nl_ed_lvls = nla_nest_start_noflag(msg,
392 NL802154_CAP_ATTR_CCA_ED_LEVELS);
396 for (i = 0; i < caps->cca_ed_levels_size; i++) {
397 if (nla_put_s32(msg, i, caps->cca_ed_levels[i]))
401 nla_nest_end(msg, nl_ed_lvls);
404 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
405 struct nlattr *nl_tx_pwrs;
407 nl_tx_pwrs = nla_nest_start_noflag(msg,
408 NL802154_CAP_ATTR_TX_POWERS);
412 for (i = 0; i < caps->tx_powers_size; i++) {
413 if (nla_put_s32(msg, i, caps->tx_powers[i]))
417 nla_nest_end(msg, nl_tx_pwrs);
420 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
421 if (nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_MODES,
423 nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_OPTS,
428 if (nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MINBE, caps->min_minbe) ||
429 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MINBE, caps->max_minbe) ||
430 nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MAXBE, caps->min_maxbe) ||
431 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MAXBE, caps->max_maxbe) ||
432 nla_put_u8(msg, NL802154_CAP_ATTR_MIN_CSMA_BACKOFFS,
433 caps->min_csma_backoffs) ||
434 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_CSMA_BACKOFFS,
435 caps->max_csma_backoffs) ||
436 nla_put_s8(msg, NL802154_CAP_ATTR_MIN_FRAME_RETRIES,
437 caps->min_frame_retries) ||
438 nla_put_s8(msg, NL802154_CAP_ATTR_MAX_FRAME_RETRIES,
439 caps->max_frame_retries) ||
440 nl802154_put_flags(msg, NL802154_CAP_ATTR_IFTYPES,
442 nla_put_u32(msg, NL802154_CAP_ATTR_LBT, caps->lbt))
445 nla_nest_end(msg, nl_caps);
450 static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev,
451 enum nl802154_commands cmd,
452 struct sk_buff *msg, u32 portid, u32 seq,
455 struct nlattr *nl_cmds;
459 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
463 if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
464 nla_put_string(msg, NL802154_ATTR_WPAN_PHY_NAME,
465 wpan_phy_name(&rdev->wpan_phy)) ||
466 nla_put_u32(msg, NL802154_ATTR_GENERATION,
467 cfg802154_rdev_list_generation))
468 goto nla_put_failure;
470 if (cmd != NL802154_CMD_NEW_WPAN_PHY)
475 /* current channel settings */
476 if (nla_put_u8(msg, NL802154_ATTR_PAGE,
477 rdev->wpan_phy.current_page) ||
478 nla_put_u8(msg, NL802154_ATTR_CHANNEL,
479 rdev->wpan_phy.current_channel))
480 goto nla_put_failure;
482 /* TODO remove this behaviour, we still keep support it for a while
483 * so users can change the behaviour to the new one.
485 if (nl802154_send_wpan_phy_channels(rdev, msg))
486 goto nla_put_failure;
489 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
490 if (nla_put_u32(msg, NL802154_ATTR_CCA_MODE,
491 rdev->wpan_phy.cca.mode))
492 goto nla_put_failure;
494 if (rdev->wpan_phy.cca.mode == NL802154_CCA_ENERGY_CARRIER) {
495 if (nla_put_u32(msg, NL802154_ATTR_CCA_OPT,
496 rdev->wpan_phy.cca.opt))
497 goto nla_put_failure;
501 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
502 if (nla_put_s32(msg, NL802154_ATTR_TX_POWER,
503 rdev->wpan_phy.transmit_power))
504 goto nla_put_failure;
507 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
508 if (nla_put_s32(msg, NL802154_ATTR_CCA_ED_LEVEL,
509 rdev->wpan_phy.cca_ed_level))
510 goto nla_put_failure;
513 if (nl802154_put_capabilities(msg, rdev))
514 goto nla_put_failure;
516 nl_cmds = nla_nest_start_noflag(msg, NL802154_ATTR_SUPPORTED_COMMANDS);
518 goto nla_put_failure;
523 if (rdev->ops->op) { \
525 if (nla_put_u32(msg, i, NL802154_CMD_ ## n)) \
526 goto nla_put_failure; \
530 CMD(add_virtual_intf, NEW_INTERFACE);
531 CMD(del_virtual_intf, DEL_INTERFACE);
532 CMD(set_channel, SET_CHANNEL);
533 CMD(set_pan_id, SET_PAN_ID);
534 CMD(set_short_addr, SET_SHORT_ADDR);
535 CMD(set_backoff_exponent, SET_BACKOFF_EXPONENT);
536 CMD(set_max_csma_backoffs, SET_MAX_CSMA_BACKOFFS);
537 CMD(set_max_frame_retries, SET_MAX_FRAME_RETRIES);
538 CMD(set_lbt_mode, SET_LBT_MODE);
539 CMD(set_ackreq_default, SET_ACKREQ_DEFAULT);
541 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER)
542 CMD(set_tx_power, SET_TX_POWER);
544 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL)
545 CMD(set_cca_ed_level, SET_CCA_ED_LEVEL);
547 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE)
548 CMD(set_cca_mode, SET_CCA_MODE);
551 nla_nest_end(msg, nl_cmds);
554 genlmsg_end(msg, hdr);
558 genlmsg_cancel(msg, hdr);
562 struct nl802154_dump_wpan_phy_state {
568 static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb,
569 struct netlink_callback *cb,
570 struct nl802154_dump_wpan_phy_state *state)
572 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
573 struct nlattr **tb = info->attrs;
575 if (tb[NL802154_ATTR_WPAN_PHY])
576 state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]);
577 if (tb[NL802154_ATTR_WPAN_DEV])
578 state->filter_wpan_phy = nla_get_u64(tb[NL802154_ATTR_WPAN_DEV]) >> 32;
579 if (tb[NL802154_ATTR_IFINDEX]) {
580 struct net_device *netdev;
581 struct cfg802154_registered_device *rdev;
582 int ifidx = nla_get_u32(tb[NL802154_ATTR_IFINDEX]);
584 netdev = __dev_get_by_index(&init_net, ifidx);
587 if (netdev->ieee802154_ptr) {
588 rdev = wpan_phy_to_rdev(
589 netdev->ieee802154_ptr->wpan_phy);
590 state->filter_wpan_phy = rdev->wpan_phy_idx;
598 nl802154_dump_wpan_phy(struct sk_buff *skb, struct netlink_callback *cb)
601 struct nl802154_dump_wpan_phy_state *state = (void *)cb->args[0];
602 struct cfg802154_registered_device *rdev;
606 state = kzalloc(sizeof(*state), GFP_KERNEL);
611 state->filter_wpan_phy = -1;
612 ret = nl802154_dump_wpan_phy_parse(skb, cb, state);
618 cb->args[0] = (long)state;
621 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
622 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
624 if (++idx <= state->start)
626 if (state->filter_wpan_phy != -1 &&
627 state->filter_wpan_phy != rdev->wpan_phy_idx)
629 /* attempt to fit multiple wpan_phy data chunks into the skb */
630 ret = nl802154_send_wpan_phy(rdev,
631 NL802154_CMD_NEW_WPAN_PHY,
633 NETLINK_CB(cb->skb).portid,
634 cb->nlh->nlmsg_seq, NLM_F_MULTI);
636 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
637 !skb->len && cb->min_dump_alloc < 4096) {
638 cb->min_dump_alloc = 4096;
654 static int nl802154_dump_wpan_phy_done(struct netlink_callback *cb)
656 kfree((void *)cb->args[0]);
660 static int nl802154_get_wpan_phy(struct sk_buff *skb, struct genl_info *info)
663 struct cfg802154_registered_device *rdev = info->user_ptr[0];
665 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
669 if (nl802154_send_wpan_phy(rdev, NL802154_CMD_NEW_WPAN_PHY, msg,
670 info->snd_portid, info->snd_seq, 0) < 0) {
675 return genlmsg_reply(msg, info);
678 static inline u64 wpan_dev_id(struct wpan_dev *wpan_dev)
680 return (u64)wpan_dev->identifier |
681 ((u64)wpan_phy_to_rdev(wpan_dev->wpan_phy)->wpan_phy_idx << 32);
684 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
685 #include <net/ieee802154_netdev.h>
688 ieee802154_llsec_send_key_id(struct sk_buff *msg,
689 const struct ieee802154_llsec_key_id *desc)
691 struct nlattr *nl_dev_addr;
693 if (nla_put_u32(msg, NL802154_KEY_ID_ATTR_MODE, desc->mode))
696 switch (desc->mode) {
697 case NL802154_KEY_ID_MODE_IMPLICIT:
698 nl_dev_addr = nla_nest_start_noflag(msg,
699 NL802154_KEY_ID_ATTR_IMPLICIT);
703 if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_PAN_ID,
704 desc->device_addr.pan_id) ||
705 nla_put_u32(msg, NL802154_DEV_ADDR_ATTR_MODE,
706 desc->device_addr.mode))
709 switch (desc->device_addr.mode) {
710 case NL802154_DEV_ADDR_SHORT:
711 if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_SHORT,
712 desc->device_addr.short_addr))
715 case NL802154_DEV_ADDR_EXTENDED:
716 if (nla_put_le64(msg, NL802154_DEV_ADDR_ATTR_EXTENDED,
717 desc->device_addr.extended_addr,
718 NL802154_DEV_ADDR_ATTR_PAD))
722 /* userspace should handle unknown */
726 nla_nest_end(msg, nl_dev_addr);
728 case NL802154_KEY_ID_MODE_INDEX:
730 case NL802154_KEY_ID_MODE_INDEX_SHORT:
731 /* TODO renmae short_source? */
732 if (nla_put_le32(msg, NL802154_KEY_ID_ATTR_SOURCE_SHORT,
736 case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
737 if (nla_put_le64(msg, NL802154_KEY_ID_ATTR_SOURCE_EXTENDED,
738 desc->extended_source,
739 NL802154_KEY_ID_ATTR_PAD))
743 /* userspace should handle unknown */
747 /* TODO key_id to key_idx ? Check naming */
748 if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
749 if (nla_put_u8(msg, NL802154_KEY_ID_ATTR_INDEX, desc->id))
756 static int nl802154_get_llsec_params(struct sk_buff *msg,
757 struct cfg802154_registered_device *rdev,
758 struct wpan_dev *wpan_dev)
760 struct nlattr *nl_key_id;
761 struct ieee802154_llsec_params params;
764 ret = rdev_get_llsec_params(rdev, wpan_dev, ¶ms);
768 if (nla_put_u8(msg, NL802154_ATTR_SEC_ENABLED, params.enabled) ||
769 nla_put_u32(msg, NL802154_ATTR_SEC_OUT_LEVEL, params.out_level) ||
770 nla_put_be32(msg, NL802154_ATTR_SEC_FRAME_COUNTER,
771 params.frame_counter))
774 nl_key_id = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_OUT_KEY_ID);
778 ret = ieee802154_llsec_send_key_id(msg, ¶ms.out_key);
782 nla_nest_end(msg, nl_key_id);
786 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
789 nl802154_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
790 struct cfg802154_registered_device *rdev,
791 struct wpan_dev *wpan_dev)
793 struct net_device *dev = wpan_dev->netdev;
796 hdr = nl802154hdr_put(msg, portid, seq, flags,
797 NL802154_CMD_NEW_INTERFACE);
802 (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex) ||
803 nla_put_string(msg, NL802154_ATTR_IFNAME, dev->name)))
804 goto nla_put_failure;
806 if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
807 nla_put_u32(msg, NL802154_ATTR_IFTYPE, wpan_dev->iftype) ||
808 nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
809 wpan_dev_id(wpan_dev), NL802154_ATTR_PAD) ||
810 nla_put_u32(msg, NL802154_ATTR_GENERATION,
811 rdev->devlist_generation ^
812 (cfg802154_rdev_list_generation << 2)))
813 goto nla_put_failure;
815 /* address settings */
816 if (nla_put_le64(msg, NL802154_ATTR_EXTENDED_ADDR,
817 wpan_dev->extended_addr,
818 NL802154_ATTR_PAD) ||
819 nla_put_le16(msg, NL802154_ATTR_SHORT_ADDR,
820 wpan_dev->short_addr) ||
821 nla_put_le16(msg, NL802154_ATTR_PAN_ID, wpan_dev->pan_id))
822 goto nla_put_failure;
825 if (nla_put_s8(msg, NL802154_ATTR_MAX_FRAME_RETRIES,
826 wpan_dev->frame_retries) ||
827 nla_put_u8(msg, NL802154_ATTR_MAX_BE, wpan_dev->max_be) ||
828 nla_put_u8(msg, NL802154_ATTR_MAX_CSMA_BACKOFFS,
829 wpan_dev->csma_retries) ||
830 nla_put_u8(msg, NL802154_ATTR_MIN_BE, wpan_dev->min_be))
831 goto nla_put_failure;
833 /* listen before transmit */
834 if (nla_put_u8(msg, NL802154_ATTR_LBT_MODE, wpan_dev->lbt))
835 goto nla_put_failure;
837 /* ackreq default behaviour */
838 if (nla_put_u8(msg, NL802154_ATTR_ACKREQ_DEFAULT, wpan_dev->ackreq))
839 goto nla_put_failure;
841 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
842 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
845 if (nl802154_get_llsec_params(msg, rdev, wpan_dev) < 0)
846 goto nla_put_failure;
849 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
851 genlmsg_end(msg, hdr);
855 genlmsg_cancel(msg, hdr);
860 nl802154_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
864 int wp_start = cb->args[0];
865 int if_start = cb->args[1];
866 struct cfg802154_registered_device *rdev;
867 struct wpan_dev *wpan_dev;
870 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
871 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
873 if (wp_idx < wp_start) {
879 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
880 if (if_idx < if_start) {
884 if (nl802154_send_iface(skb, NETLINK_CB(cb->skb).portid,
885 cb->nlh->nlmsg_seq, NLM_F_MULTI,
886 rdev, wpan_dev) < 0) {
897 cb->args[0] = wp_idx;
898 cb->args[1] = if_idx;
903 static int nl802154_get_interface(struct sk_buff *skb, struct genl_info *info)
906 struct cfg802154_registered_device *rdev = info->user_ptr[0];
907 struct wpan_dev *wdev = info->user_ptr[1];
909 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
913 if (nl802154_send_iface(msg, info->snd_portid, info->snd_seq, 0,
919 return genlmsg_reply(msg, info);
922 static int nl802154_new_interface(struct sk_buff *skb, struct genl_info *info)
924 struct cfg802154_registered_device *rdev = info->user_ptr[0];
925 enum nl802154_iftype type = NL802154_IFTYPE_UNSPEC;
926 __le64 extended_addr = cpu_to_le64(0x0000000000000000ULL);
928 /* TODO avoid failing a new interface
929 * creation due to pending removal?
932 if (!info->attrs[NL802154_ATTR_IFNAME])
935 if (info->attrs[NL802154_ATTR_IFTYPE]) {
936 type = nla_get_u32(info->attrs[NL802154_ATTR_IFTYPE]);
937 if (type > NL802154_IFTYPE_MAX ||
938 !(rdev->wpan_phy.supported.iftypes & BIT(type)))
942 if (info->attrs[NL802154_ATTR_EXTENDED_ADDR])
943 extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
945 if (!rdev->ops->add_virtual_intf)
948 return rdev_add_virtual_intf(rdev,
949 nla_data(info->attrs[NL802154_ATTR_IFNAME]),
950 NET_NAME_USER, type, extended_addr);
953 static int nl802154_del_interface(struct sk_buff *skb, struct genl_info *info)
955 struct cfg802154_registered_device *rdev = info->user_ptr[0];
956 struct wpan_dev *wpan_dev = info->user_ptr[1];
958 if (!rdev->ops->del_virtual_intf)
961 /* If we remove a wpan device without a netdev then clear
962 * user_ptr[1] so that nl802154_post_doit won't dereference it
963 * to check if it needs to do dev_put(). Otherwise it crashes
964 * since the wpan_dev has been freed, unlike with a netdev where
965 * we need the dev_put() for the netdev to really be freed.
967 if (!wpan_dev->netdev)
968 info->user_ptr[1] = NULL;
970 return rdev_del_virtual_intf(rdev, wpan_dev);
973 static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info)
975 struct cfg802154_registered_device *rdev = info->user_ptr[0];
978 if (!info->attrs[NL802154_ATTR_PAGE] ||
979 !info->attrs[NL802154_ATTR_CHANNEL])
982 page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
983 channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]);
985 /* check 802.15.4 constraints */
986 if (!ieee802154_chan_is_valid(&rdev->wpan_phy, page, channel))
989 return rdev_set_channel(rdev, page, channel);
992 static int nl802154_set_cca_mode(struct sk_buff *skb, struct genl_info *info)
994 struct cfg802154_registered_device *rdev = info->user_ptr[0];
995 struct wpan_phy_cca cca;
997 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE))
1000 if (!info->attrs[NL802154_ATTR_CCA_MODE])
1003 cca.mode = nla_get_u32(info->attrs[NL802154_ATTR_CCA_MODE]);
1004 /* checking 802.15.4 constraints */
1005 if (cca.mode < NL802154_CCA_ENERGY ||
1006 cca.mode > NL802154_CCA_ATTR_MAX ||
1007 !(rdev->wpan_phy.supported.cca_modes & BIT(cca.mode)))
1010 if (cca.mode == NL802154_CCA_ENERGY_CARRIER) {
1011 if (!info->attrs[NL802154_ATTR_CCA_OPT])
1014 cca.opt = nla_get_u32(info->attrs[NL802154_ATTR_CCA_OPT]);
1015 if (cca.opt > NL802154_CCA_OPT_ATTR_MAX ||
1016 !(rdev->wpan_phy.supported.cca_opts & BIT(cca.opt)))
1020 return rdev_set_cca_mode(rdev, &cca);
1023 static int nl802154_set_cca_ed_level(struct sk_buff *skb, struct genl_info *info)
1025 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1029 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL))
1032 if (!info->attrs[NL802154_ATTR_CCA_ED_LEVEL])
1035 ed_level = nla_get_s32(info->attrs[NL802154_ATTR_CCA_ED_LEVEL]);
1037 for (i = 0; i < rdev->wpan_phy.supported.cca_ed_levels_size; i++) {
1038 if (ed_level == rdev->wpan_phy.supported.cca_ed_levels[i])
1039 return rdev_set_cca_ed_level(rdev, ed_level);
1045 static int nl802154_set_tx_power(struct sk_buff *skb, struct genl_info *info)
1047 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1051 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER))
1054 if (!info->attrs[NL802154_ATTR_TX_POWER])
1057 power = nla_get_s32(info->attrs[NL802154_ATTR_TX_POWER]);
1059 for (i = 0; i < rdev->wpan_phy.supported.tx_powers_size; i++) {
1060 if (power == rdev->wpan_phy.supported.tx_powers[i])
1061 return rdev_set_tx_power(rdev, power);
1067 static int nl802154_set_pan_id(struct sk_buff *skb, struct genl_info *info)
1069 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1070 struct net_device *dev = info->user_ptr[1];
1071 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1074 /* conflict here while tx/rx calls */
1075 if (netif_running(dev))
1078 if (wpan_dev->lowpan_dev) {
1079 if (netif_running(wpan_dev->lowpan_dev))
1083 /* don't change address fields on monitor */
1084 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1085 !info->attrs[NL802154_ATTR_PAN_ID])
1088 pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
1091 * I am not sure about to check here on broadcast pan_id.
1092 * Broadcast is a valid setting, comment from 802.15.4:
1093 * If this value is 0xffff, the device is not associated.
1095 * This could useful to simple deassociate an device.
1097 if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
1100 return rdev_set_pan_id(rdev, wpan_dev, pan_id);
1103 static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info)
1105 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1106 struct net_device *dev = info->user_ptr[1];
1107 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1110 /* conflict here while tx/rx calls */
1111 if (netif_running(dev))
1114 if (wpan_dev->lowpan_dev) {
1115 if (netif_running(wpan_dev->lowpan_dev))
1119 /* don't change address fields on monitor */
1120 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1121 !info->attrs[NL802154_ATTR_SHORT_ADDR])
1124 short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
1127 * I am not sure about to check here on broadcast short_addr.
1128 * Broadcast is a valid setting, comment from 802.15.4:
1129 * A value of 0xfffe indicates that the device has
1130 * associated but has not been allocated an address. A
1131 * value of 0xffff indicates that the device does not
1132 * have a short address.
1134 * I think we should allow to set these settings but
1135 * don't allow to allow socket communication with it.
1137 if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
1138 short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
1141 return rdev_set_short_addr(rdev, wpan_dev, short_addr);
1145 nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info)
1147 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1148 struct net_device *dev = info->user_ptr[1];
1149 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1152 /* should be set on netif open inside phy settings */
1153 if (netif_running(dev))
1156 if (!info->attrs[NL802154_ATTR_MIN_BE] ||
1157 !info->attrs[NL802154_ATTR_MAX_BE])
1160 min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]);
1161 max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]);
1163 /* check 802.15.4 constraints */
1164 if (min_be < rdev->wpan_phy.supported.min_minbe ||
1165 min_be > rdev->wpan_phy.supported.max_minbe ||
1166 max_be < rdev->wpan_phy.supported.min_maxbe ||
1167 max_be > rdev->wpan_phy.supported.max_maxbe ||
1171 return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
1175 nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info)
1177 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1178 struct net_device *dev = info->user_ptr[1];
1179 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1180 u8 max_csma_backoffs;
1182 /* conflict here while other running iface settings */
1183 if (netif_running(dev))
1186 if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS])
1189 max_csma_backoffs = nla_get_u8(
1190 info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]);
1192 /* check 802.15.4 constraints */
1193 if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs ||
1194 max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs)
1197 return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
1201 nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info)
1203 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1204 struct net_device *dev = info->user_ptr[1];
1205 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1206 s8 max_frame_retries;
1208 if (netif_running(dev))
1211 if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES])
1214 max_frame_retries = nla_get_s8(
1215 info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]);
1217 /* check 802.15.4 constraints */
1218 if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries ||
1219 max_frame_retries > rdev->wpan_phy.supported.max_frame_retries)
1222 return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
1225 static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info)
1227 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1228 struct net_device *dev = info->user_ptr[1];
1229 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1232 if (netif_running(dev))
1235 if (!info->attrs[NL802154_ATTR_LBT_MODE])
1238 mode = nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
1240 if (mode != 0 && mode != 1)
1243 if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt))
1246 return rdev_set_lbt_mode(rdev, wpan_dev, mode);
1250 nl802154_set_ackreq_default(struct sk_buff *skb, struct genl_info *info)
1252 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1253 struct net_device *dev = info->user_ptr[1];
1254 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1257 if (netif_running(dev))
1260 if (!info->attrs[NL802154_ATTR_ACKREQ_DEFAULT])
1263 ackreq = nla_get_u8(info->attrs[NL802154_ATTR_ACKREQ_DEFAULT]);
1265 if (ackreq != 0 && ackreq != 1)
1268 return rdev_set_ackreq_default(rdev, wpan_dev, ackreq);
1271 static int nl802154_wpan_phy_netns(struct sk_buff *skb, struct genl_info *info)
1273 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1277 if (info->attrs[NL802154_ATTR_PID]) {
1278 u32 pid = nla_get_u32(info->attrs[NL802154_ATTR_PID]);
1280 net = get_net_ns_by_pid(pid);
1281 } else if (info->attrs[NL802154_ATTR_NETNS_FD]) {
1282 u32 fd = nla_get_u32(info->attrs[NL802154_ATTR_NETNS_FD]);
1284 net = get_net_ns_by_fd(fd);
1290 return PTR_ERR(net);
1294 /* check if anything to do */
1295 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), net))
1296 err = cfg802154_switch_netns(rdev, net);
1302 static int nl802154_prep_scan_event_msg(struct sk_buff *msg,
1303 struct cfg802154_registered_device *rdev,
1304 struct wpan_dev *wpan_dev,
1305 u32 portid, u32 seq, int flags, u8 cmd,
1306 struct ieee802154_coord_desc *desc)
1311 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1315 if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx))
1316 goto nla_put_failure;
1318 if (wpan_dev->netdev &&
1319 nla_put_u32(msg, NL802154_ATTR_IFINDEX, wpan_dev->netdev->ifindex))
1320 goto nla_put_failure;
1322 if (nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
1323 wpan_dev_id(wpan_dev), NL802154_ATTR_PAD))
1324 goto nla_put_failure;
1326 nla = nla_nest_start_noflag(msg, NL802154_ATTR_COORDINATOR);
1328 goto nla_put_failure;
1330 if (nla_put(msg, NL802154_COORD_PANID, IEEE802154_PAN_ID_LEN,
1331 &desc->addr.pan_id))
1332 goto nla_put_failure;
1334 if (desc->addr.mode == IEEE802154_ADDR_SHORT) {
1335 if (nla_put(msg, NL802154_COORD_ADDR,
1336 IEEE802154_SHORT_ADDR_LEN,
1337 &desc->addr.short_addr))
1338 goto nla_put_failure;
1340 if (nla_put(msg, NL802154_COORD_ADDR,
1341 IEEE802154_EXTENDED_ADDR_LEN,
1342 &desc->addr.extended_addr))
1343 goto nla_put_failure;
1346 if (nla_put_u8(msg, NL802154_COORD_CHANNEL, desc->channel))
1347 goto nla_put_failure;
1349 if (nla_put_u8(msg, NL802154_COORD_PAGE, desc->page))
1350 goto nla_put_failure;
1352 if (nla_put_u16(msg, NL802154_COORD_SUPERFRAME_SPEC,
1353 desc->superframe_spec))
1354 goto nla_put_failure;
1356 if (nla_put_u8(msg, NL802154_COORD_LINK_QUALITY, desc->link_quality))
1357 goto nla_put_failure;
1359 if (desc->gts_permit && nla_put_flag(msg, NL802154_COORD_GTS_PERMIT))
1360 goto nla_put_failure;
1362 /* TODO: NL802154_COORD_PAYLOAD_DATA if any */
1364 nla_nest_end(msg, nla);
1366 genlmsg_end(msg, hdr);
1371 genlmsg_cancel(msg, hdr);
1376 int nl802154_scan_event(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
1377 struct ieee802154_coord_desc *desc)
1379 struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(wpan_phy);
1380 struct sk_buff *msg;
1383 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1387 ret = nl802154_prep_scan_event_msg(msg, rdev, wpan_dev, 0, 0, 0,
1388 NL802154_CMD_SCAN_EVENT,
1395 return genlmsg_multicast_netns(&nl802154_fam, wpan_phy_net(wpan_phy),
1396 msg, 0, NL802154_MCGRP_SCAN, GFP_ATOMIC);
1398 EXPORT_SYMBOL_GPL(nl802154_scan_event);
1400 static int nl802154_trigger_scan(struct sk_buff *skb, struct genl_info *info)
1402 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1403 struct net_device *dev = info->user_ptr[1];
1404 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1405 struct wpan_phy *wpan_phy = &rdev->wpan_phy;
1406 struct cfg802154_scan_request *request;
1410 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1411 NL_SET_ERR_MSG(info->extack, "Monitors are not allowed to perform scans");
1415 if (!info->attrs[NL802154_ATTR_SCAN_TYPE]) {
1416 NL_SET_ERR_MSG(info->extack, "Malformed request, missing scan type");
1420 request = kzalloc(sizeof(*request), GFP_KERNEL);
1424 request->wpan_dev = wpan_dev;
1425 request->wpan_phy = wpan_phy;
1427 type = nla_get_u8(info->attrs[NL802154_ATTR_SCAN_TYPE]);
1429 case NL802154_SCAN_PASSIVE:
1430 request->type = type;
1433 NL_SET_ERR_MSG_FMT(info->extack, "Unsupported scan type: %d", type);
1438 /* Use current page by default */
1439 if (info->attrs[NL802154_ATTR_PAGE])
1440 request->page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
1442 request->page = wpan_phy->current_page;
1444 /* Scan all supported channels by default */
1445 if (info->attrs[NL802154_ATTR_SCAN_CHANNELS])
1446 request->channels = nla_get_u32(info->attrs[NL802154_ATTR_SCAN_CHANNELS]);
1448 request->channels = wpan_phy->supported.channels[request->page];
1450 /* Use maximum duration order by default */
1451 if (info->attrs[NL802154_ATTR_SCAN_DURATION])
1452 request->duration = nla_get_u8(info->attrs[NL802154_ATTR_SCAN_DURATION]);
1454 request->duration = IEEE802154_MAX_SCAN_DURATION;
1456 err = rdev_trigger_scan(rdev, request);
1458 pr_err("Failure starting scanning (%d)\n", err);
1470 static int nl802154_prep_scan_msg(struct sk_buff *msg,
1471 struct cfg802154_registered_device *rdev,
1472 struct wpan_dev *wpan_dev, u32 portid,
1473 u32 seq, int flags, u8 cmd, u8 arg)
1477 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1481 if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx))
1482 goto nla_put_failure;
1484 if (wpan_dev->netdev &&
1485 nla_put_u32(msg, NL802154_ATTR_IFINDEX, wpan_dev->netdev->ifindex))
1486 goto nla_put_failure;
1488 if (nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
1489 wpan_dev_id(wpan_dev), NL802154_ATTR_PAD))
1490 goto nla_put_failure;
1492 if (cmd == NL802154_CMD_SCAN_DONE &&
1493 nla_put_u8(msg, NL802154_ATTR_SCAN_DONE_REASON, arg))
1494 goto nla_put_failure;
1496 genlmsg_end(msg, hdr);
1501 genlmsg_cancel(msg, hdr);
1506 static int nl802154_send_scan_msg(struct cfg802154_registered_device *rdev,
1507 struct wpan_dev *wpan_dev, u8 cmd, u8 arg)
1509 struct sk_buff *msg;
1512 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1516 ret = nl802154_prep_scan_msg(msg, rdev, wpan_dev, 0, 0, 0, cmd, arg);
1522 return genlmsg_multicast_netns(&nl802154_fam,
1523 wpan_phy_net(&rdev->wpan_phy), msg, 0,
1524 NL802154_MCGRP_SCAN, GFP_KERNEL);
1527 int nl802154_scan_started(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev)
1529 struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(wpan_phy);
1532 /* Ignore errors when there are no listeners */
1533 err = nl802154_send_scan_msg(rdev, wpan_dev, NL802154_CMD_TRIGGER_SCAN, 0);
1539 EXPORT_SYMBOL_GPL(nl802154_scan_started);
1541 int nl802154_scan_done(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
1542 enum nl802154_scan_done_reasons reason)
1544 struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(wpan_phy);
1547 /* Ignore errors when there are no listeners */
1548 err = nl802154_send_scan_msg(rdev, wpan_dev, NL802154_CMD_SCAN_DONE, reason);
1554 EXPORT_SYMBOL_GPL(nl802154_scan_done);
1556 static int nl802154_abort_scan(struct sk_buff *skb, struct genl_info *info)
1558 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1559 struct net_device *dev = info->user_ptr[1];
1560 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1562 /* Resources are released in the notification helper above */
1563 return rdev_abort_scan(rdev, wpan_dev);
1567 nl802154_send_beacons(struct sk_buff *skb, struct genl_info *info)
1569 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1570 struct net_device *dev = info->user_ptr[1];
1571 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1572 struct wpan_phy *wpan_phy = &rdev->wpan_phy;
1573 struct cfg802154_beacon_request *request;
1576 if (wpan_dev->iftype != NL802154_IFTYPE_COORD) {
1577 NL_SET_ERR_MSG(info->extack, "Only coordinators can send beacons");
1581 if (wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
1582 NL_SET_ERR_MSG(info->extack, "Device is not part of any PAN");
1586 request = kzalloc(sizeof(*request), GFP_KERNEL);
1590 request->wpan_dev = wpan_dev;
1591 request->wpan_phy = wpan_phy;
1593 /* Use maximum duration order by default */
1594 if (info->attrs[NL802154_ATTR_BEACON_INTERVAL])
1595 request->interval = nla_get_u8(info->attrs[NL802154_ATTR_BEACON_INTERVAL]);
1597 request->interval = IEEE802154_MAX_SCAN_DURATION;
1599 err = rdev_send_beacons(rdev, request);
1601 pr_err("Failure starting sending beacons (%d)\n", err);
1613 void nl802154_beaconing_done(struct wpan_dev *wpan_dev)
1617 EXPORT_SYMBOL_GPL(nl802154_beaconing_done);
1620 nl802154_stop_beacons(struct sk_buff *skb, struct genl_info *info)
1622 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1623 struct net_device *dev = info->user_ptr[1];
1624 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1626 /* Resources are released in the notification helper above */
1627 return rdev_stop_beacons(rdev, wpan_dev);
1630 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
1631 static const struct nla_policy nl802154_dev_addr_policy[NL802154_DEV_ADDR_ATTR_MAX + 1] = {
1632 [NL802154_DEV_ADDR_ATTR_PAN_ID] = { .type = NLA_U16 },
1633 [NL802154_DEV_ADDR_ATTR_MODE] = { .type = NLA_U32 },
1634 [NL802154_DEV_ADDR_ATTR_SHORT] = { .type = NLA_U16 },
1635 [NL802154_DEV_ADDR_ATTR_EXTENDED] = { .type = NLA_U64 },
1639 ieee802154_llsec_parse_dev_addr(struct nlattr *nla,
1640 struct ieee802154_addr *addr)
1642 struct nlattr *attrs[NL802154_DEV_ADDR_ATTR_MAX + 1];
1644 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ADDR_ATTR_MAX, nla, nl802154_dev_addr_policy, NULL))
1647 if (!attrs[NL802154_DEV_ADDR_ATTR_PAN_ID] || !attrs[NL802154_DEV_ADDR_ATTR_MODE])
1650 addr->pan_id = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_PAN_ID]);
1651 addr->mode = nla_get_u32(attrs[NL802154_DEV_ADDR_ATTR_MODE]);
1652 switch (addr->mode) {
1653 case NL802154_DEV_ADDR_SHORT:
1654 if (!attrs[NL802154_DEV_ADDR_ATTR_SHORT])
1656 addr->short_addr = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_SHORT]);
1658 case NL802154_DEV_ADDR_EXTENDED:
1659 if (!attrs[NL802154_DEV_ADDR_ATTR_EXTENDED])
1661 addr->extended_addr = nla_get_le64(attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]);
1670 static const struct nla_policy nl802154_key_id_policy[NL802154_KEY_ID_ATTR_MAX + 1] = {
1671 [NL802154_KEY_ID_ATTR_MODE] = { .type = NLA_U32 },
1672 [NL802154_KEY_ID_ATTR_INDEX] = { .type = NLA_U8 },
1673 [NL802154_KEY_ID_ATTR_IMPLICIT] = { .type = NLA_NESTED },
1674 [NL802154_KEY_ID_ATTR_SOURCE_SHORT] = { .type = NLA_U32 },
1675 [NL802154_KEY_ID_ATTR_SOURCE_EXTENDED] = { .type = NLA_U64 },
1679 ieee802154_llsec_parse_key_id(struct nlattr *nla,
1680 struct ieee802154_llsec_key_id *desc)
1682 struct nlattr *attrs[NL802154_KEY_ID_ATTR_MAX + 1];
1684 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_KEY_ID_ATTR_MAX, nla, nl802154_key_id_policy, NULL))
1687 if (!attrs[NL802154_KEY_ID_ATTR_MODE])
1690 desc->mode = nla_get_u32(attrs[NL802154_KEY_ID_ATTR_MODE]);
1691 switch (desc->mode) {
1692 case NL802154_KEY_ID_MODE_IMPLICIT:
1693 if (!attrs[NL802154_KEY_ID_ATTR_IMPLICIT])
1696 if (ieee802154_llsec_parse_dev_addr(attrs[NL802154_KEY_ID_ATTR_IMPLICIT],
1697 &desc->device_addr) < 0)
1700 case NL802154_KEY_ID_MODE_INDEX:
1702 case NL802154_KEY_ID_MODE_INDEX_SHORT:
1703 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT])
1706 desc->short_source = nla_get_le32(attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT]);
1708 case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
1709 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED])
1712 desc->extended_source = nla_get_le64(attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED]);
1718 if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
1719 if (!attrs[NL802154_KEY_ID_ATTR_INDEX])
1722 /* TODO change id to idx */
1723 desc->id = nla_get_u8(attrs[NL802154_KEY_ID_ATTR_INDEX]);
1729 static int nl802154_set_llsec_params(struct sk_buff *skb,
1730 struct genl_info *info)
1732 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1733 struct net_device *dev = info->user_ptr[1];
1734 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1735 struct ieee802154_llsec_params params;
1739 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1742 if (info->attrs[NL802154_ATTR_SEC_ENABLED]) {
1745 enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1746 if (enabled != 0 && enabled != 1)
1749 params.enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1750 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
1753 if (info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID]) {
1754 ret = ieee802154_llsec_parse_key_id(info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID],
1759 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
1762 if (info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]) {
1763 params.out_level = nla_get_u32(info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]);
1764 if (params.out_level > NL802154_SECLEVEL_MAX)
1767 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
1770 if (info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]) {
1771 params.frame_counter = nla_get_be32(info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]);
1772 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
1775 return rdev_set_llsec_params(rdev, wpan_dev, ¶ms, changed);
1778 static int nl802154_send_key(struct sk_buff *msg, u32 cmd, u32 portid,
1780 struct cfg802154_registered_device *rdev,
1781 struct net_device *dev,
1782 const struct ieee802154_llsec_key_entry *key)
1785 u32 commands[NL802154_CMD_FRAME_NR_IDS / 32];
1786 struct nlattr *nl_key, *nl_key_id;
1788 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1792 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1793 goto nla_put_failure;
1795 nl_key = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_KEY);
1797 goto nla_put_failure;
1799 nl_key_id = nla_nest_start_noflag(msg, NL802154_KEY_ATTR_ID);
1801 goto nla_put_failure;
1803 if (ieee802154_llsec_send_key_id(msg, &key->id) < 0)
1804 goto nla_put_failure;
1806 nla_nest_end(msg, nl_key_id);
1808 if (nla_put_u8(msg, NL802154_KEY_ATTR_USAGE_FRAMES,
1809 key->key->frame_types))
1810 goto nla_put_failure;
1812 if (key->key->frame_types & BIT(NL802154_FRAME_CMD)) {
1813 /* TODO for each nested */
1814 memset(commands, 0, sizeof(commands));
1815 commands[7] = key->key->cmd_frame_ids;
1816 if (nla_put(msg, NL802154_KEY_ATTR_USAGE_CMDS,
1817 sizeof(commands), commands))
1818 goto nla_put_failure;
1821 if (nla_put(msg, NL802154_KEY_ATTR_BYTES, NL802154_KEY_SIZE,
1823 goto nla_put_failure;
1825 nla_nest_end(msg, nl_key);
1826 genlmsg_end(msg, hdr);
1831 genlmsg_cancel(msg, hdr);
1836 nl802154_dump_llsec_key(struct sk_buff *skb, struct netlink_callback *cb)
1838 struct cfg802154_registered_device *rdev = NULL;
1839 struct ieee802154_llsec_key_entry *key;
1840 struct ieee802154_llsec_table *table;
1841 struct wpan_dev *wpan_dev;
1844 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1848 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1853 if (!wpan_dev->netdev) {
1858 rdev_lock_llsec_table(rdev, wpan_dev);
1859 rdev_get_llsec_table(rdev, wpan_dev, &table);
1861 /* TODO make it like station dump */
1865 list_for_each_entry(key, &table->keys, list) {
1866 if (nl802154_send_key(skb, NL802154_CMD_NEW_SEC_KEY,
1867 NETLINK_CB(cb->skb).portid,
1868 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1869 rdev, wpan_dev->netdev, key) < 0) {
1872 rdev_unlock_llsec_table(rdev, wpan_dev);
1880 rdev_unlock_llsec_table(rdev, wpan_dev);
1883 nl802154_finish_wpan_dev_dump(rdev);
1888 static const struct nla_policy nl802154_key_policy[NL802154_KEY_ATTR_MAX + 1] = {
1889 [NL802154_KEY_ATTR_ID] = { NLA_NESTED },
1890 /* TODO handle it as for_each_nested and NLA_FLAG? */
1891 [NL802154_KEY_ATTR_USAGE_FRAMES] = { NLA_U8 },
1892 /* TODO handle it as for_each_nested, not static array? */
1893 [NL802154_KEY_ATTR_USAGE_CMDS] = { .len = NL802154_CMD_FRAME_NR_IDS / 8 },
1894 [NL802154_KEY_ATTR_BYTES] = { .len = NL802154_KEY_SIZE },
1897 static int nl802154_add_llsec_key(struct sk_buff *skb, struct genl_info *info)
1899 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1900 struct net_device *dev = info->user_ptr[1];
1901 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1902 struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1903 struct ieee802154_llsec_key key = { };
1904 struct ieee802154_llsec_key_id id = { };
1905 u32 commands[NL802154_CMD_FRAME_NR_IDS / 32] = { };
1907 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1910 if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
1911 nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
1914 if (!attrs[NL802154_KEY_ATTR_USAGE_FRAMES] ||
1915 !attrs[NL802154_KEY_ATTR_BYTES])
1918 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1921 key.frame_types = nla_get_u8(attrs[NL802154_KEY_ATTR_USAGE_FRAMES]);
1922 if (key.frame_types > BIT(NL802154_FRAME_MAX) ||
1923 ((key.frame_types & BIT(NL802154_FRAME_CMD)) &&
1924 !attrs[NL802154_KEY_ATTR_USAGE_CMDS]))
1927 if (attrs[NL802154_KEY_ATTR_USAGE_CMDS]) {
1928 /* TODO for each nested */
1929 nla_memcpy(commands, attrs[NL802154_KEY_ATTR_USAGE_CMDS],
1930 NL802154_CMD_FRAME_NR_IDS / 8);
1932 /* TODO understand the -EINVAL logic here? last condition */
1933 if (commands[0] || commands[1] || commands[2] || commands[3] ||
1934 commands[4] || commands[5] || commands[6] ||
1935 commands[7] > BIT(NL802154_CMD_FRAME_MAX))
1938 key.cmd_frame_ids = commands[7];
1940 key.cmd_frame_ids = 0;
1943 nla_memcpy(key.key, attrs[NL802154_KEY_ATTR_BYTES], NL802154_KEY_SIZE);
1945 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1948 return rdev_add_llsec_key(rdev, wpan_dev, &id, &key);
1951 static int nl802154_del_llsec_key(struct sk_buff *skb, struct genl_info *info)
1953 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1954 struct net_device *dev = info->user_ptr[1];
1955 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1956 struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1957 struct ieee802154_llsec_key_id id;
1959 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1962 if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
1963 nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
1966 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1969 return rdev_del_llsec_key(rdev, wpan_dev, &id);
1972 static int nl802154_send_device(struct sk_buff *msg, u32 cmd, u32 portid,
1974 struct cfg802154_registered_device *rdev,
1975 struct net_device *dev,
1976 const struct ieee802154_llsec_device *dev_desc)
1979 struct nlattr *nl_device;
1981 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1985 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1986 goto nla_put_failure;
1988 nl_device = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVICE);
1990 goto nla_put_failure;
1992 if (nla_put_u32(msg, NL802154_DEV_ATTR_FRAME_COUNTER,
1993 dev_desc->frame_counter) ||
1994 nla_put_le16(msg, NL802154_DEV_ATTR_PAN_ID, dev_desc->pan_id) ||
1995 nla_put_le16(msg, NL802154_DEV_ATTR_SHORT_ADDR,
1996 dev_desc->short_addr) ||
1997 nla_put_le64(msg, NL802154_DEV_ATTR_EXTENDED_ADDR,
1998 dev_desc->hwaddr, NL802154_DEV_ATTR_PAD) ||
1999 nla_put_u8(msg, NL802154_DEV_ATTR_SECLEVEL_EXEMPT,
2000 dev_desc->seclevel_exempt) ||
2001 nla_put_u32(msg, NL802154_DEV_ATTR_KEY_MODE, dev_desc->key_mode))
2002 goto nla_put_failure;
2004 nla_nest_end(msg, nl_device);
2005 genlmsg_end(msg, hdr);
2010 genlmsg_cancel(msg, hdr);
2015 nl802154_dump_llsec_dev(struct sk_buff *skb, struct netlink_callback *cb)
2017 struct cfg802154_registered_device *rdev = NULL;
2018 struct ieee802154_llsec_device *dev;
2019 struct ieee802154_llsec_table *table;
2020 struct wpan_dev *wpan_dev;
2023 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2027 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2032 if (!wpan_dev->netdev) {
2037 rdev_lock_llsec_table(rdev, wpan_dev);
2038 rdev_get_llsec_table(rdev, wpan_dev, &table);
2040 /* TODO make it like station dump */
2044 list_for_each_entry(dev, &table->devices, list) {
2045 if (nl802154_send_device(skb, NL802154_CMD_NEW_SEC_LEVEL,
2046 NETLINK_CB(cb->skb).portid,
2047 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2048 rdev, wpan_dev->netdev, dev) < 0) {
2051 rdev_unlock_llsec_table(rdev, wpan_dev);
2059 rdev_unlock_llsec_table(rdev, wpan_dev);
2062 nl802154_finish_wpan_dev_dump(rdev);
2067 static const struct nla_policy nl802154_dev_policy[NL802154_DEV_ATTR_MAX + 1] = {
2068 [NL802154_DEV_ATTR_FRAME_COUNTER] = { NLA_U32 },
2069 [NL802154_DEV_ATTR_PAN_ID] = { .type = NLA_U16 },
2070 [NL802154_DEV_ATTR_SHORT_ADDR] = { .type = NLA_U16 },
2071 [NL802154_DEV_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
2072 [NL802154_DEV_ATTR_SECLEVEL_EXEMPT] = { NLA_U8 },
2073 [NL802154_DEV_ATTR_KEY_MODE] = { NLA_U32 },
2077 ieee802154_llsec_parse_device(struct nlattr *nla,
2078 struct ieee802154_llsec_device *dev)
2080 struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
2082 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, nla, nl802154_dev_policy, NULL))
2085 memset(dev, 0, sizeof(*dev));
2087 if (!attrs[NL802154_DEV_ATTR_FRAME_COUNTER] ||
2088 !attrs[NL802154_DEV_ATTR_PAN_ID] ||
2089 !attrs[NL802154_DEV_ATTR_SHORT_ADDR] ||
2090 !attrs[NL802154_DEV_ATTR_EXTENDED_ADDR] ||
2091 !attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] ||
2092 !attrs[NL802154_DEV_ATTR_KEY_MODE])
2096 dev->frame_counter = nla_get_u32(attrs[NL802154_DEV_ATTR_FRAME_COUNTER]);
2097 dev->pan_id = nla_get_le16(attrs[NL802154_DEV_ATTR_PAN_ID]);
2098 dev->short_addr = nla_get_le16(attrs[NL802154_DEV_ATTR_SHORT_ADDR]);
2099 /* TODO rename hwaddr to extended_addr */
2100 dev->hwaddr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
2101 dev->seclevel_exempt = nla_get_u8(attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT]);
2102 dev->key_mode = nla_get_u32(attrs[NL802154_DEV_ATTR_KEY_MODE]);
2104 if (dev->key_mode > NL802154_DEVKEY_MAX ||
2105 (dev->seclevel_exempt != 0 && dev->seclevel_exempt != 1))
2111 static int nl802154_add_llsec_dev(struct sk_buff *skb, struct genl_info *info)
2113 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2114 struct net_device *dev = info->user_ptr[1];
2115 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2116 struct ieee802154_llsec_device dev_desc;
2118 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2121 if (ieee802154_llsec_parse_device(info->attrs[NL802154_ATTR_SEC_DEVICE],
2125 return rdev_add_device(rdev, wpan_dev, &dev_desc);
2128 static int nl802154_del_llsec_dev(struct sk_buff *skb, struct genl_info *info)
2130 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2131 struct net_device *dev = info->user_ptr[1];
2132 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2133 struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
2134 __le64 extended_addr;
2136 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2139 if (!info->attrs[NL802154_ATTR_SEC_DEVICE] ||
2140 nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVICE], nl802154_dev_policy, info->extack))
2143 if (!attrs[NL802154_DEV_ATTR_EXTENDED_ADDR])
2146 extended_addr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
2147 return rdev_del_device(rdev, wpan_dev, extended_addr);
2150 static int nl802154_send_devkey(struct sk_buff *msg, u32 cmd, u32 portid,
2152 struct cfg802154_registered_device *rdev,
2153 struct net_device *dev, __le64 extended_addr,
2154 const struct ieee802154_llsec_device_key *devkey)
2157 struct nlattr *nl_devkey, *nl_key_id;
2159 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
2163 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
2164 goto nla_put_failure;
2166 nl_devkey = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVKEY);
2168 goto nla_put_failure;
2170 if (nla_put_le64(msg, NL802154_DEVKEY_ATTR_EXTENDED_ADDR,
2171 extended_addr, NL802154_DEVKEY_ATTR_PAD) ||
2172 nla_put_u32(msg, NL802154_DEVKEY_ATTR_FRAME_COUNTER,
2173 devkey->frame_counter))
2174 goto nla_put_failure;
2176 nl_key_id = nla_nest_start_noflag(msg, NL802154_DEVKEY_ATTR_ID);
2178 goto nla_put_failure;
2180 if (ieee802154_llsec_send_key_id(msg, &devkey->key_id) < 0)
2181 goto nla_put_failure;
2183 nla_nest_end(msg, nl_key_id);
2184 nla_nest_end(msg, nl_devkey);
2185 genlmsg_end(msg, hdr);
2190 genlmsg_cancel(msg, hdr);
2195 nl802154_dump_llsec_devkey(struct sk_buff *skb, struct netlink_callback *cb)
2197 struct cfg802154_registered_device *rdev = NULL;
2198 struct ieee802154_llsec_device_key *kpos;
2199 struct ieee802154_llsec_device *dpos;
2200 struct ieee802154_llsec_table *table;
2201 struct wpan_dev *wpan_dev;
2204 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2208 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2213 if (!wpan_dev->netdev) {
2218 rdev_lock_llsec_table(rdev, wpan_dev);
2219 rdev_get_llsec_table(rdev, wpan_dev, &table);
2221 /* TODO make it like station dump */
2225 /* TODO look if remove devkey and do some nested attribute */
2226 list_for_each_entry(dpos, &table->devices, list) {
2227 list_for_each_entry(kpos, &dpos->keys, list) {
2228 if (nl802154_send_devkey(skb,
2229 NL802154_CMD_NEW_SEC_LEVEL,
2230 NETLINK_CB(cb->skb).portid,
2238 rdev_unlock_llsec_table(rdev, wpan_dev);
2247 rdev_unlock_llsec_table(rdev, wpan_dev);
2250 nl802154_finish_wpan_dev_dump(rdev);
2255 static const struct nla_policy nl802154_devkey_policy[NL802154_DEVKEY_ATTR_MAX + 1] = {
2256 [NL802154_DEVKEY_ATTR_FRAME_COUNTER] = { NLA_U32 },
2257 [NL802154_DEVKEY_ATTR_EXTENDED_ADDR] = { NLA_U64 },
2258 [NL802154_DEVKEY_ATTR_ID] = { NLA_NESTED },
2261 static int nl802154_add_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
2263 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2264 struct net_device *dev = info->user_ptr[1];
2265 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2266 struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
2267 struct ieee802154_llsec_device_key key;
2268 __le64 extended_addr;
2270 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2273 if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
2274 nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack) < 0)
2277 if (!attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER] ||
2278 !attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
2281 /* TODO change key.id ? */
2282 if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
2287 key.frame_counter = nla_get_u32(attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER]);
2288 /* TODO change naming hwaddr -> extended_addr
2289 * check unique identifier short+pan OR extended_addr
2291 extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
2292 return rdev_add_devkey(rdev, wpan_dev, extended_addr, &key);
2295 static int nl802154_del_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
2297 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2298 struct net_device *dev = info->user_ptr[1];
2299 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2300 struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
2301 struct ieee802154_llsec_device_key key;
2302 __le64 extended_addr;
2304 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2307 if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
2308 nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack))
2311 if (!attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
2314 /* TODO change key.id ? */
2315 if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
2319 /* TODO change naming hwaddr -> extended_addr
2320 * check unique identifier short+pan OR extended_addr
2322 extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
2323 return rdev_del_devkey(rdev, wpan_dev, extended_addr, &key);
2326 static int nl802154_send_seclevel(struct sk_buff *msg, u32 cmd, u32 portid,
2328 struct cfg802154_registered_device *rdev,
2329 struct net_device *dev,
2330 const struct ieee802154_llsec_seclevel *sl)
2333 struct nlattr *nl_seclevel;
2335 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
2339 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
2340 goto nla_put_failure;
2342 nl_seclevel = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_LEVEL);
2344 goto nla_put_failure;
2346 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_FRAME, sl->frame_type) ||
2347 nla_put_u32(msg, NL802154_SECLEVEL_ATTR_LEVELS, sl->sec_levels) ||
2348 nla_put_u8(msg, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE,
2349 sl->device_override))
2350 goto nla_put_failure;
2352 if (sl->frame_type == NL802154_FRAME_CMD) {
2353 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_CMD_FRAME,
2355 goto nla_put_failure;
2358 nla_nest_end(msg, nl_seclevel);
2359 genlmsg_end(msg, hdr);
2364 genlmsg_cancel(msg, hdr);
2369 nl802154_dump_llsec_seclevel(struct sk_buff *skb, struct netlink_callback *cb)
2371 struct cfg802154_registered_device *rdev = NULL;
2372 struct ieee802154_llsec_seclevel *sl;
2373 struct ieee802154_llsec_table *table;
2374 struct wpan_dev *wpan_dev;
2377 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2381 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2386 if (!wpan_dev->netdev) {
2391 rdev_lock_llsec_table(rdev, wpan_dev);
2392 rdev_get_llsec_table(rdev, wpan_dev, &table);
2394 /* TODO make it like station dump */
2398 list_for_each_entry(sl, &table->security_levels, list) {
2399 if (nl802154_send_seclevel(skb, NL802154_CMD_NEW_SEC_LEVEL,
2400 NETLINK_CB(cb->skb).portid,
2401 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2402 rdev, wpan_dev->netdev, sl) < 0) {
2405 rdev_unlock_llsec_table(rdev, wpan_dev);
2413 rdev_unlock_llsec_table(rdev, wpan_dev);
2416 nl802154_finish_wpan_dev_dump(rdev);
2421 static const struct nla_policy nl802154_seclevel_policy[NL802154_SECLEVEL_ATTR_MAX + 1] = {
2422 [NL802154_SECLEVEL_ATTR_LEVELS] = { .type = NLA_U8 },
2423 [NL802154_SECLEVEL_ATTR_FRAME] = { .type = NLA_U32 },
2424 [NL802154_SECLEVEL_ATTR_CMD_FRAME] = { .type = NLA_U32 },
2425 [NL802154_SECLEVEL_ATTR_DEV_OVERRIDE] = { .type = NLA_U8 },
2429 llsec_parse_seclevel(struct nlattr *nla, struct ieee802154_llsec_seclevel *sl)
2431 struct nlattr *attrs[NL802154_SECLEVEL_ATTR_MAX + 1];
2433 if (!nla || nla_parse_nested_deprecated(attrs, NL802154_SECLEVEL_ATTR_MAX, nla, nl802154_seclevel_policy, NULL))
2436 memset(sl, 0, sizeof(*sl));
2438 if (!attrs[NL802154_SECLEVEL_ATTR_LEVELS] ||
2439 !attrs[NL802154_SECLEVEL_ATTR_FRAME] ||
2440 !attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE])
2443 sl->sec_levels = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_LEVELS]);
2444 sl->frame_type = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_FRAME]);
2445 sl->device_override = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE]);
2446 if (sl->frame_type > NL802154_FRAME_MAX ||
2447 (sl->device_override != 0 && sl->device_override != 1))
2450 if (sl->frame_type == NL802154_FRAME_CMD) {
2451 if (!attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME])
2454 sl->cmd_frame_id = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME]);
2455 if (sl->cmd_frame_id > NL802154_CMD_FRAME_MAX)
2462 static int nl802154_add_llsec_seclevel(struct sk_buff *skb,
2463 struct genl_info *info)
2465 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2466 struct net_device *dev = info->user_ptr[1];
2467 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2468 struct ieee802154_llsec_seclevel sl;
2470 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2473 if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2477 return rdev_add_seclevel(rdev, wpan_dev, &sl);
2480 static int nl802154_del_llsec_seclevel(struct sk_buff *skb,
2481 struct genl_info *info)
2483 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2484 struct net_device *dev = info->user_ptr[1];
2485 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2486 struct ieee802154_llsec_seclevel sl;
2488 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2491 if (!info->attrs[NL802154_ATTR_SEC_LEVEL] ||
2492 llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2496 return rdev_del_seclevel(rdev, wpan_dev, &sl);
2498 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2500 #define NL802154_FLAG_NEED_WPAN_PHY 0x01
2501 #define NL802154_FLAG_NEED_NETDEV 0x02
2502 #define NL802154_FLAG_NEED_RTNL 0x04
2503 #define NL802154_FLAG_CHECK_NETDEV_UP 0x08
2504 #define NL802154_FLAG_NEED_WPAN_DEV 0x10
2506 static int nl802154_pre_doit(const struct genl_split_ops *ops,
2507 struct sk_buff *skb,
2508 struct genl_info *info)
2510 struct cfg802154_registered_device *rdev;
2511 struct wpan_dev *wpan_dev;
2512 struct net_device *dev;
2513 bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
2518 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
2519 rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
2523 return PTR_ERR(rdev);
2525 info->user_ptr[0] = rdev;
2526 } else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
2527 ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2529 wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
2531 if (IS_ERR(wpan_dev)) {
2534 return PTR_ERR(wpan_dev);
2537 dev = wpan_dev->netdev;
2538 rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
2540 if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
2547 info->user_ptr[1] = dev;
2549 info->user_ptr[1] = wpan_dev;
2553 if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
2554 !netif_running(dev)) {
2563 info->user_ptr[0] = rdev;
2569 static void nl802154_post_doit(const struct genl_split_ops *ops,
2570 struct sk_buff *skb,
2571 struct genl_info *info)
2573 if (info->user_ptr[1]) {
2574 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2575 struct wpan_dev *wpan_dev = info->user_ptr[1];
2577 dev_put(wpan_dev->netdev);
2579 dev_put(info->user_ptr[1]);
2583 if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
2587 static const struct genl_ops nl802154_ops[] = {
2589 .cmd = NL802154_CMD_GET_WPAN_PHY,
2590 .validate = GENL_DONT_VALIDATE_STRICT |
2591 GENL_DONT_VALIDATE_DUMP_STRICT,
2592 .doit = nl802154_get_wpan_phy,
2593 .dumpit = nl802154_dump_wpan_phy,
2594 .done = nl802154_dump_wpan_phy_done,
2595 /* can be retrieved by unprivileged users */
2596 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2597 NL802154_FLAG_NEED_RTNL,
2600 .cmd = NL802154_CMD_GET_INTERFACE,
2601 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2602 .doit = nl802154_get_interface,
2603 .dumpit = nl802154_dump_interface,
2604 /* can be retrieved by unprivileged users */
2605 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2606 NL802154_FLAG_NEED_RTNL,
2609 .cmd = NL802154_CMD_NEW_INTERFACE,
2610 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2611 .doit = nl802154_new_interface,
2612 .flags = GENL_ADMIN_PERM,
2613 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2614 NL802154_FLAG_NEED_RTNL,
2617 .cmd = NL802154_CMD_DEL_INTERFACE,
2618 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2619 .doit = nl802154_del_interface,
2620 .flags = GENL_ADMIN_PERM,
2621 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2622 NL802154_FLAG_NEED_RTNL,
2625 .cmd = NL802154_CMD_SET_CHANNEL,
2626 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2627 .doit = nl802154_set_channel,
2628 .flags = GENL_ADMIN_PERM,
2629 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2630 NL802154_FLAG_NEED_RTNL,
2633 .cmd = NL802154_CMD_SET_CCA_MODE,
2634 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2635 .doit = nl802154_set_cca_mode,
2636 .flags = GENL_ADMIN_PERM,
2637 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2638 NL802154_FLAG_NEED_RTNL,
2641 .cmd = NL802154_CMD_SET_CCA_ED_LEVEL,
2642 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2643 .doit = nl802154_set_cca_ed_level,
2644 .flags = GENL_ADMIN_PERM,
2645 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2646 NL802154_FLAG_NEED_RTNL,
2649 .cmd = NL802154_CMD_SET_TX_POWER,
2650 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2651 .doit = nl802154_set_tx_power,
2652 .flags = GENL_ADMIN_PERM,
2653 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2654 NL802154_FLAG_NEED_RTNL,
2657 .cmd = NL802154_CMD_SET_WPAN_PHY_NETNS,
2658 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2659 .doit = nl802154_wpan_phy_netns,
2660 .flags = GENL_ADMIN_PERM,
2661 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2662 NL802154_FLAG_NEED_RTNL,
2665 .cmd = NL802154_CMD_SET_PAN_ID,
2666 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2667 .doit = nl802154_set_pan_id,
2668 .flags = GENL_ADMIN_PERM,
2669 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2670 NL802154_FLAG_NEED_RTNL,
2673 .cmd = NL802154_CMD_SET_SHORT_ADDR,
2674 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2675 .doit = nl802154_set_short_addr,
2676 .flags = GENL_ADMIN_PERM,
2677 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2678 NL802154_FLAG_NEED_RTNL,
2681 .cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
2682 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2683 .doit = nl802154_set_backoff_exponent,
2684 .flags = GENL_ADMIN_PERM,
2685 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2686 NL802154_FLAG_NEED_RTNL,
2689 .cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
2690 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2691 .doit = nl802154_set_max_csma_backoffs,
2692 .flags = GENL_ADMIN_PERM,
2693 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2694 NL802154_FLAG_NEED_RTNL,
2697 .cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
2698 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2699 .doit = nl802154_set_max_frame_retries,
2700 .flags = GENL_ADMIN_PERM,
2701 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2702 NL802154_FLAG_NEED_RTNL,
2705 .cmd = NL802154_CMD_SET_LBT_MODE,
2706 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2707 .doit = nl802154_set_lbt_mode,
2708 .flags = GENL_ADMIN_PERM,
2709 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2710 NL802154_FLAG_NEED_RTNL,
2713 .cmd = NL802154_CMD_SET_ACKREQ_DEFAULT,
2714 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2715 .doit = nl802154_set_ackreq_default,
2716 .flags = GENL_ADMIN_PERM,
2717 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2718 NL802154_FLAG_NEED_RTNL,
2721 .cmd = NL802154_CMD_TRIGGER_SCAN,
2722 .doit = nl802154_trigger_scan,
2723 .flags = GENL_ADMIN_PERM,
2724 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2725 NL802154_FLAG_CHECK_NETDEV_UP |
2726 NL802154_FLAG_NEED_RTNL,
2729 .cmd = NL802154_CMD_ABORT_SCAN,
2730 .doit = nl802154_abort_scan,
2731 .flags = GENL_ADMIN_PERM,
2732 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2733 NL802154_FLAG_CHECK_NETDEV_UP |
2734 NL802154_FLAG_NEED_RTNL,
2737 .cmd = NL802154_CMD_SEND_BEACONS,
2738 .doit = nl802154_send_beacons,
2739 .flags = GENL_ADMIN_PERM,
2740 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2741 NL802154_FLAG_CHECK_NETDEV_UP |
2742 NL802154_FLAG_NEED_RTNL,
2745 .cmd = NL802154_CMD_STOP_BEACONS,
2746 .doit = nl802154_stop_beacons,
2747 .flags = GENL_ADMIN_PERM,
2748 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2749 NL802154_FLAG_CHECK_NETDEV_UP |
2750 NL802154_FLAG_NEED_RTNL,
2752 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
2754 .cmd = NL802154_CMD_SET_SEC_PARAMS,
2755 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2756 .doit = nl802154_set_llsec_params,
2757 .flags = GENL_ADMIN_PERM,
2758 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2759 NL802154_FLAG_NEED_RTNL,
2762 .cmd = NL802154_CMD_GET_SEC_KEY,
2763 .validate = GENL_DONT_VALIDATE_STRICT |
2764 GENL_DONT_VALIDATE_DUMP_STRICT,
2765 /* TODO .doit by matching key id? */
2766 .dumpit = nl802154_dump_llsec_key,
2767 .flags = GENL_ADMIN_PERM,
2768 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2769 NL802154_FLAG_NEED_RTNL,
2772 .cmd = NL802154_CMD_NEW_SEC_KEY,
2773 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2774 .doit = nl802154_add_llsec_key,
2775 .flags = GENL_ADMIN_PERM,
2776 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2777 NL802154_FLAG_NEED_RTNL,
2780 .cmd = NL802154_CMD_DEL_SEC_KEY,
2781 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2782 .doit = nl802154_del_llsec_key,
2783 .flags = GENL_ADMIN_PERM,
2784 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2785 NL802154_FLAG_NEED_RTNL,
2787 /* TODO unique identifier must short+pan OR extended_addr */
2789 .cmd = NL802154_CMD_GET_SEC_DEV,
2790 .validate = GENL_DONT_VALIDATE_STRICT |
2791 GENL_DONT_VALIDATE_DUMP_STRICT,
2792 /* TODO .doit by matching extended_addr? */
2793 .dumpit = nl802154_dump_llsec_dev,
2794 .flags = GENL_ADMIN_PERM,
2795 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2796 NL802154_FLAG_NEED_RTNL,
2799 .cmd = NL802154_CMD_NEW_SEC_DEV,
2800 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2801 .doit = nl802154_add_llsec_dev,
2802 .flags = GENL_ADMIN_PERM,
2803 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2804 NL802154_FLAG_NEED_RTNL,
2807 .cmd = NL802154_CMD_DEL_SEC_DEV,
2808 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2809 .doit = nl802154_del_llsec_dev,
2810 .flags = GENL_ADMIN_PERM,
2811 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2812 NL802154_FLAG_NEED_RTNL,
2814 /* TODO remove complete devkey, put it as nested? */
2816 .cmd = NL802154_CMD_GET_SEC_DEVKEY,
2817 .validate = GENL_DONT_VALIDATE_STRICT |
2818 GENL_DONT_VALIDATE_DUMP_STRICT,
2819 /* TODO doit by matching ??? */
2820 .dumpit = nl802154_dump_llsec_devkey,
2821 .flags = GENL_ADMIN_PERM,
2822 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2823 NL802154_FLAG_NEED_RTNL,
2826 .cmd = NL802154_CMD_NEW_SEC_DEVKEY,
2827 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2828 .doit = nl802154_add_llsec_devkey,
2829 .flags = GENL_ADMIN_PERM,
2830 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2831 NL802154_FLAG_NEED_RTNL,
2834 .cmd = NL802154_CMD_DEL_SEC_DEVKEY,
2835 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2836 .doit = nl802154_del_llsec_devkey,
2837 .flags = GENL_ADMIN_PERM,
2838 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2839 NL802154_FLAG_NEED_RTNL,
2842 .cmd = NL802154_CMD_GET_SEC_LEVEL,
2843 .validate = GENL_DONT_VALIDATE_STRICT |
2844 GENL_DONT_VALIDATE_DUMP_STRICT,
2845 /* TODO .doit by matching frame_type? */
2846 .dumpit = nl802154_dump_llsec_seclevel,
2847 .flags = GENL_ADMIN_PERM,
2848 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2849 NL802154_FLAG_NEED_RTNL,
2852 .cmd = NL802154_CMD_NEW_SEC_LEVEL,
2853 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2854 .doit = nl802154_add_llsec_seclevel,
2855 .flags = GENL_ADMIN_PERM,
2856 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2857 NL802154_FLAG_NEED_RTNL,
2860 .cmd = NL802154_CMD_DEL_SEC_LEVEL,
2861 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2862 /* TODO match frame_type only? */
2863 .doit = nl802154_del_llsec_seclevel,
2864 .flags = GENL_ADMIN_PERM,
2865 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2866 NL802154_FLAG_NEED_RTNL,
2868 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2871 static struct genl_family nl802154_fam __ro_after_init = {
2872 .name = NL802154_GENL_NAME, /* have users key off the name instead */
2873 .hdrsize = 0, /* no private header */
2874 .version = 1, /* no particular meaning now */
2875 .maxattr = NL802154_ATTR_MAX,
2876 .policy = nl802154_policy,
2878 .pre_doit = nl802154_pre_doit,
2879 .post_doit = nl802154_post_doit,
2880 .module = THIS_MODULE,
2881 .ops = nl802154_ops,
2882 .n_ops = ARRAY_SIZE(nl802154_ops),
2883 .resv_start_op = NL802154_CMD_DEL_SEC_LEVEL + 1,
2884 .mcgrps = nl802154_mcgrps,
2885 .n_mcgrps = ARRAY_SIZE(nl802154_mcgrps),
2888 /* initialisation/exit functions */
2889 int __init nl802154_init(void)
2891 return genl_register_family(&nl802154_fam);
2894 void nl802154_exit(void)
2896 genl_unregister_family(&nl802154_fam);