1 /* This program is free software; you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License version 2
3 * as published by the Free Software Foundation.
5 * This program is distributed in the hope that it will be useful,
6 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 * GNU General Public License for more details.
11 * Alexander Aring <aar@pengutronix.de>
13 * Based on: net/wireless/nl80211.c
16 #include <linux/rtnetlink.h>
18 #include <net/cfg802154.h>
19 #include <net/genetlink.h>
20 #include <net/mac802154.h>
21 #include <net/netlink.h>
22 #include <net/nl802154.h>
29 static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
30 struct genl_info *info);
32 static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
35 /* the netlink family */
36 static struct genl_family nl802154_fam = {
37 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
38 .name = NL802154_GENL_NAME, /* have users key off the name instead */
39 .hdrsize = 0, /* no private header */
40 .version = 1, /* no particular meaning now */
41 .maxattr = NL802154_ATTR_MAX,
43 .pre_doit = nl802154_pre_doit,
44 .post_doit = nl802154_post_doit,
47 /* multicast groups */
48 enum nl802154_multicast_groups {
49 NL802154_MCGRP_CONFIG,
52 static const struct genl_multicast_group nl802154_mcgrps[] = {
53 [NL802154_MCGRP_CONFIG] = { .name = "config", },
56 /* returns ERR_PTR values */
57 static struct wpan_dev *
58 __cfg802154_wpan_dev_from_attrs(struct net *netns, struct nlattr **attrs)
60 struct cfg802154_registered_device *rdev;
61 struct wpan_dev *result = NULL;
62 bool have_ifidx = attrs[NL802154_ATTR_IFINDEX];
63 bool have_wpan_dev_id = attrs[NL802154_ATTR_WPAN_DEV];
65 int wpan_phy_idx = -1;
70 if (!have_ifidx && !have_wpan_dev_id)
71 return ERR_PTR(-EINVAL);
74 ifidx = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
75 if (have_wpan_dev_id) {
76 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
77 wpan_phy_idx = wpan_dev_id >> 32;
80 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
81 struct wpan_dev *wpan_dev;
83 /* TODO netns compare */
85 if (have_wpan_dev_id && rdev->wpan_phy_idx != wpan_phy_idx)
88 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
89 if (have_ifidx && wpan_dev->netdev &&
90 wpan_dev->netdev->ifindex == ifidx) {
94 if (have_wpan_dev_id &&
95 wpan_dev->identifier == (u32)wpan_dev_id) {
108 return ERR_PTR(-ENODEV);
111 static struct cfg802154_registered_device *
112 __cfg802154_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
114 struct cfg802154_registered_device *rdev = NULL, *tmp;
115 struct net_device *netdev;
119 if (!attrs[NL802154_ATTR_WPAN_PHY] &&
120 !attrs[NL802154_ATTR_IFINDEX] &&
121 !attrs[NL802154_ATTR_WPAN_DEV])
122 return ERR_PTR(-EINVAL);
124 if (attrs[NL802154_ATTR_WPAN_PHY])
125 rdev = cfg802154_rdev_by_wpan_phy_idx(
126 nla_get_u32(attrs[NL802154_ATTR_WPAN_PHY]));
128 if (attrs[NL802154_ATTR_WPAN_DEV]) {
129 u64 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
130 struct wpan_dev *wpan_dev;
133 tmp = cfg802154_rdev_by_wpan_phy_idx(wpan_dev_id >> 32);
135 /* make sure wpan_dev exists */
136 list_for_each_entry(wpan_dev, &tmp->wpan_dev_list, list) {
137 if (wpan_dev->identifier != (u32)wpan_dev_id)
146 if (rdev && tmp != rdev)
147 return ERR_PTR(-EINVAL);
152 if (attrs[NL802154_ATTR_IFINDEX]) {
153 int ifindex = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
155 netdev = __dev_get_by_index(netns, ifindex);
157 if (netdev->ieee802154_ptr)
158 tmp = wpan_phy_to_rdev(
159 netdev->ieee802154_ptr->wpan_phy);
163 /* not wireless device -- return error */
165 return ERR_PTR(-EINVAL);
167 /* mismatch -- return error */
168 if (rdev && tmp != rdev)
169 return ERR_PTR(-EINVAL);
176 return ERR_PTR(-ENODEV);
178 /* TODO netns compare */
183 /* This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
186 * The result of this can be a PTR_ERR and hence must
187 * be checked with IS_ERR() for errors.
189 static struct cfg802154_registered_device *
190 cfg802154_get_dev_from_info(struct net *netns, struct genl_info *info)
192 return __cfg802154_rdev_from_attrs(netns, info->attrs);
195 /* policy for the attributes */
196 static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
197 [NL802154_ATTR_WPAN_PHY] = { .type = NLA_U32 },
198 [NL802154_ATTR_WPAN_PHY_NAME] = { .type = NLA_NUL_STRING,
201 [NL802154_ATTR_IFINDEX] = { .type = NLA_U32 },
202 [NL802154_ATTR_IFTYPE] = { .type = NLA_U32 },
203 [NL802154_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
205 [NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
207 [NL802154_ATTR_PAGE] = { .type = NLA_U8, },
208 [NL802154_ATTR_CHANNEL] = { .type = NLA_U8, },
210 [NL802154_ATTR_TX_POWER] = { .type = NLA_S32, },
212 [NL802154_ATTR_CCA_MODE] = { .type = NLA_U32, },
213 [NL802154_ATTR_CCA_OPT] = { .type = NLA_U32, },
214 [NL802154_ATTR_CCA_ED_LEVEL] = { .type = NLA_S32, },
216 [NL802154_ATTR_SUPPORTED_CHANNEL] = { .type = NLA_U32, },
218 [NL802154_ATTR_PAN_ID] = { .type = NLA_U16, },
219 [NL802154_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
220 [NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16, },
222 [NL802154_ATTR_MIN_BE] = { .type = NLA_U8, },
223 [NL802154_ATTR_MAX_BE] = { .type = NLA_U8, },
224 [NL802154_ATTR_MAX_CSMA_BACKOFFS] = { .type = NLA_U8, },
226 [NL802154_ATTR_MAX_FRAME_RETRIES] = { .type = NLA_S8, },
228 [NL802154_ATTR_LBT_MODE] = { .type = NLA_U8, },
230 [NL802154_ATTR_WPAN_PHY_CAPS] = { .type = NLA_NESTED },
232 [NL802154_ATTR_SUPPORTED_COMMANDS] = { .type = NLA_NESTED },
234 [NL802154_ATTR_ACKREQ_DEFAULT] = { .type = NLA_U8 },
236 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
237 [NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
238 [NL802154_ATTR_SEC_OUT_LEVEL] = { .type = NLA_U32, },
239 [NL802154_ATTR_SEC_OUT_KEY_ID] = { .type = NLA_NESTED, },
240 [NL802154_ATTR_SEC_FRAME_COUNTER] = { .type = NLA_U32 },
242 [NL802154_ATTR_SEC_LEVEL] = { .type = NLA_NESTED },
243 [NL802154_ATTR_SEC_DEVICE] = { .type = NLA_NESTED },
244 [NL802154_ATTR_SEC_DEVKEY] = { .type = NLA_NESTED },
245 [NL802154_ATTR_SEC_KEY] = { .type = NLA_NESTED },
246 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
249 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
251 nl802154_prepare_wpan_dev_dump(struct sk_buff *skb,
252 struct netlink_callback *cb,
253 struct cfg802154_registered_device **rdev,
254 struct wpan_dev **wpan_dev)
261 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize,
262 nl802154_fam.attrbuf, nl802154_fam.maxattr,
267 *wpan_dev = __cfg802154_wpan_dev_from_attrs(sock_net(skb->sk),
268 nl802154_fam.attrbuf);
269 if (IS_ERR(*wpan_dev)) {
270 err = PTR_ERR(*wpan_dev);
273 *rdev = wpan_phy_to_rdev((*wpan_dev)->wpan_phy);
274 /* 0 is the first index - add 1 to parse only once */
275 cb->args[0] = (*rdev)->wpan_phy_idx + 1;
276 cb->args[1] = (*wpan_dev)->identifier;
278 /* subtract the 1 again here */
279 struct wpan_phy *wpan_phy = wpan_phy_idx_to_wpan_phy(cb->args[0] - 1);
280 struct wpan_dev *tmp;
286 *rdev = wpan_phy_to_rdev(wpan_phy);
289 list_for_each_entry(tmp, &(*rdev)->wpan_dev_list, list) {
290 if (tmp->identifier == cb->args[1]) {
309 nl802154_finish_wpan_dev_dump(struct cfg802154_registered_device *rdev)
313 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
315 /* message building helper */
316 static inline void *nl802154hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
319 /* since there is no private header just add the generic one */
320 return genlmsg_put(skb, portid, seq, &nl802154_fam, flags, cmd);
324 nl802154_put_flags(struct sk_buff *msg, int attr, u32 mask)
326 struct nlattr *nl_flags = nla_nest_start(msg, attr);
334 if ((mask & 1) && nla_put_flag(msg, i))
341 nla_nest_end(msg, nl_flags);
346 nl802154_send_wpan_phy_channels(struct cfg802154_registered_device *rdev,
349 struct nlattr *nl_page;
352 nl_page = nla_nest_start(msg, NL802154_ATTR_CHANNELS_SUPPORTED);
356 for (page = 0; page <= IEEE802154_MAX_PAGE; page++) {
357 if (nla_put_u32(msg, NL802154_ATTR_SUPPORTED_CHANNEL,
358 rdev->wpan_phy.supported.channels[page]))
361 nla_nest_end(msg, nl_page);
367 nl802154_put_capabilities(struct sk_buff *msg,
368 struct cfg802154_registered_device *rdev)
370 const struct wpan_phy_supported *caps = &rdev->wpan_phy.supported;
371 struct nlattr *nl_caps, *nl_channels;
374 nl_caps = nla_nest_start(msg, NL802154_ATTR_WPAN_PHY_CAPS);
378 nl_channels = nla_nest_start(msg, NL802154_CAP_ATTR_CHANNELS);
382 for (i = 0; i <= IEEE802154_MAX_PAGE; i++) {
383 if (caps->channels[i]) {
384 if (nl802154_put_flags(msg, i, caps->channels[i]))
389 nla_nest_end(msg, nl_channels);
391 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
392 struct nlattr *nl_ed_lvls;
394 nl_ed_lvls = nla_nest_start(msg,
395 NL802154_CAP_ATTR_CCA_ED_LEVELS);
399 for (i = 0; i < caps->cca_ed_levels_size; i++) {
400 if (nla_put_s32(msg, i, caps->cca_ed_levels[i]))
404 nla_nest_end(msg, nl_ed_lvls);
407 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
408 struct nlattr *nl_tx_pwrs;
410 nl_tx_pwrs = nla_nest_start(msg, NL802154_CAP_ATTR_TX_POWERS);
414 for (i = 0; i < caps->tx_powers_size; i++) {
415 if (nla_put_s32(msg, i, caps->tx_powers[i]))
419 nla_nest_end(msg, nl_tx_pwrs);
422 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
423 if (nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_MODES,
425 nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_OPTS,
430 if (nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MINBE, caps->min_minbe) ||
431 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MINBE, caps->max_minbe) ||
432 nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MAXBE, caps->min_maxbe) ||
433 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MAXBE, caps->max_maxbe) ||
434 nla_put_u8(msg, NL802154_CAP_ATTR_MIN_CSMA_BACKOFFS,
435 caps->min_csma_backoffs) ||
436 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_CSMA_BACKOFFS,
437 caps->max_csma_backoffs) ||
438 nla_put_s8(msg, NL802154_CAP_ATTR_MIN_FRAME_RETRIES,
439 caps->min_frame_retries) ||
440 nla_put_s8(msg, NL802154_CAP_ATTR_MAX_FRAME_RETRIES,
441 caps->max_frame_retries) ||
442 nl802154_put_flags(msg, NL802154_CAP_ATTR_IFTYPES,
444 nla_put_u32(msg, NL802154_CAP_ATTR_LBT, caps->lbt))
447 nla_nest_end(msg, nl_caps);
452 static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev,
453 enum nl802154_commands cmd,
454 struct sk_buff *msg, u32 portid, u32 seq,
457 struct nlattr *nl_cmds;
461 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
465 if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
466 nla_put_string(msg, NL802154_ATTR_WPAN_PHY_NAME,
467 wpan_phy_name(&rdev->wpan_phy)) ||
468 nla_put_u32(msg, NL802154_ATTR_GENERATION,
469 cfg802154_rdev_list_generation))
470 goto nla_put_failure;
472 if (cmd != NL802154_CMD_NEW_WPAN_PHY)
477 /* current channel settings */
478 if (nla_put_u8(msg, NL802154_ATTR_PAGE,
479 rdev->wpan_phy.current_page) ||
480 nla_put_u8(msg, NL802154_ATTR_CHANNEL,
481 rdev->wpan_phy.current_channel))
482 goto nla_put_failure;
484 /* TODO remove this behaviour, we still keep support it for a while
485 * so users can change the behaviour to the new one.
487 if (nl802154_send_wpan_phy_channels(rdev, msg))
488 goto nla_put_failure;
491 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
492 if (nla_put_u32(msg, NL802154_ATTR_CCA_MODE,
493 rdev->wpan_phy.cca.mode))
494 goto nla_put_failure;
496 if (rdev->wpan_phy.cca.mode == NL802154_CCA_ENERGY_CARRIER) {
497 if (nla_put_u32(msg, NL802154_ATTR_CCA_OPT,
498 rdev->wpan_phy.cca.opt))
499 goto nla_put_failure;
503 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
504 if (nla_put_s32(msg, NL802154_ATTR_TX_POWER,
505 rdev->wpan_phy.transmit_power))
506 goto nla_put_failure;
509 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
510 if (nla_put_s32(msg, NL802154_ATTR_CCA_ED_LEVEL,
511 rdev->wpan_phy.cca_ed_level))
512 goto nla_put_failure;
515 if (nl802154_put_capabilities(msg, rdev))
516 goto nla_put_failure;
518 nl_cmds = nla_nest_start(msg, NL802154_ATTR_SUPPORTED_COMMANDS);
520 goto nla_put_failure;
525 if (rdev->ops->op) { \
527 if (nla_put_u32(msg, i, NL802154_CMD_ ## n)) \
528 goto nla_put_failure; \
532 CMD(add_virtual_intf, NEW_INTERFACE);
533 CMD(del_virtual_intf, DEL_INTERFACE);
534 CMD(set_channel, SET_CHANNEL);
535 CMD(set_pan_id, SET_PAN_ID);
536 CMD(set_short_addr, SET_SHORT_ADDR);
537 CMD(set_backoff_exponent, SET_BACKOFF_EXPONENT);
538 CMD(set_max_csma_backoffs, SET_MAX_CSMA_BACKOFFS);
539 CMD(set_max_frame_retries, SET_MAX_FRAME_RETRIES);
540 CMD(set_lbt_mode, SET_LBT_MODE);
541 CMD(set_ackreq_default, SET_ACKREQ_DEFAULT);
543 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER)
544 CMD(set_tx_power, SET_TX_POWER);
546 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL)
547 CMD(set_cca_ed_level, SET_CCA_ED_LEVEL);
549 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE)
550 CMD(set_cca_mode, SET_CCA_MODE);
553 nla_nest_end(msg, nl_cmds);
556 genlmsg_end(msg, hdr);
560 genlmsg_cancel(msg, hdr);
564 struct nl802154_dump_wpan_phy_state {
570 static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb,
571 struct netlink_callback *cb,
572 struct nl802154_dump_wpan_phy_state *state)
574 struct nlattr **tb = nl802154_fam.attrbuf;
575 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize,
576 tb, nl802154_fam.maxattr, nl802154_policy);
578 /* TODO check if we can handle error here,
579 * we have no backward compatibility
584 if (tb[NL802154_ATTR_WPAN_PHY])
585 state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]);
586 if (tb[NL802154_ATTR_WPAN_DEV])
587 state->filter_wpan_phy = nla_get_u64(tb[NL802154_ATTR_WPAN_DEV]) >> 32;
588 if (tb[NL802154_ATTR_IFINDEX]) {
589 struct net_device *netdev;
590 struct cfg802154_registered_device *rdev;
591 int ifidx = nla_get_u32(tb[NL802154_ATTR_IFINDEX]);
594 netdev = __dev_get_by_index(&init_net, ifidx);
597 if (netdev->ieee802154_ptr) {
598 rdev = wpan_phy_to_rdev(
599 netdev->ieee802154_ptr->wpan_phy);
600 state->filter_wpan_phy = rdev->wpan_phy_idx;
608 nl802154_dump_wpan_phy(struct sk_buff *skb, struct netlink_callback *cb)
611 struct nl802154_dump_wpan_phy_state *state = (void *)cb->args[0];
612 struct cfg802154_registered_device *rdev;
616 state = kzalloc(sizeof(*state), GFP_KERNEL);
621 state->filter_wpan_phy = -1;
622 ret = nl802154_dump_wpan_phy_parse(skb, cb, state);
628 cb->args[0] = (long)state;
631 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
632 /* TODO net ns compare */
633 if (++idx <= state->start)
635 if (state->filter_wpan_phy != -1 &&
636 state->filter_wpan_phy != rdev->wpan_phy_idx)
638 /* attempt to fit multiple wpan_phy data chunks into the skb */
639 ret = nl802154_send_wpan_phy(rdev,
640 NL802154_CMD_NEW_WPAN_PHY,
642 NETLINK_CB(cb->skb).portid,
643 cb->nlh->nlmsg_seq, NLM_F_MULTI);
645 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
646 !skb->len && cb->min_dump_alloc < 4096) {
647 cb->min_dump_alloc = 4096;
663 static int nl802154_dump_wpan_phy_done(struct netlink_callback *cb)
665 kfree((void *)cb->args[0]);
669 static int nl802154_get_wpan_phy(struct sk_buff *skb, struct genl_info *info)
672 struct cfg802154_registered_device *rdev = info->user_ptr[0];
674 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
678 if (nl802154_send_wpan_phy(rdev, NL802154_CMD_NEW_WPAN_PHY, msg,
679 info->snd_portid, info->snd_seq, 0) < 0) {
684 return genlmsg_reply(msg, info);
687 static inline u64 wpan_dev_id(struct wpan_dev *wpan_dev)
689 return (u64)wpan_dev->identifier |
690 ((u64)wpan_phy_to_rdev(wpan_dev->wpan_phy)->wpan_phy_idx << 32);
693 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
694 #include <net/ieee802154_netdev.h>
697 ieee802154_llsec_send_key_id(struct sk_buff *msg,
698 const struct ieee802154_llsec_key_id *desc)
700 struct nlattr *nl_dev_addr;
702 if (nla_put_u32(msg, NL802154_KEY_ID_ATTR_MODE, desc->mode))
705 switch (desc->mode) {
706 case NL802154_KEY_ID_MODE_IMPLICIT:
707 nl_dev_addr = nla_nest_start(msg, NL802154_KEY_ID_ATTR_IMPLICIT);
711 if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_PAN_ID,
712 desc->device_addr.pan_id) ||
713 nla_put_u32(msg, NL802154_DEV_ADDR_ATTR_MODE,
714 desc->device_addr.mode))
717 switch (desc->device_addr.mode) {
718 case NL802154_DEV_ADDR_SHORT:
719 if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_SHORT,
720 desc->device_addr.short_addr))
723 case NL802154_DEV_ADDR_EXTENDED:
724 if (nla_put_le64(msg, NL802154_DEV_ADDR_ATTR_EXTENDED,
725 desc->device_addr.extended_addr))
729 /* userspace should handle unknown */
733 nla_nest_end(msg, nl_dev_addr);
735 case NL802154_KEY_ID_MODE_INDEX:
737 case NL802154_KEY_ID_MODE_INDEX_SHORT:
738 /* TODO renmae short_source? */
739 if (nla_put_le32(msg, NL802154_KEY_ID_ATTR_SOURCE_SHORT,
743 case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
744 if (nla_put_le64(msg, NL802154_KEY_ID_ATTR_SOURCE_EXTENDED,
745 desc->extended_source))
749 /* userspace should handle unknown */
753 /* TODO key_id to key_idx ? Check naming */
754 if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
755 if (nla_put_u8(msg, NL802154_KEY_ID_ATTR_INDEX, desc->id))
762 static int nl802154_get_llsec_params(struct sk_buff *msg,
763 struct cfg802154_registered_device *rdev,
764 struct wpan_dev *wpan_dev)
766 struct nlattr *nl_key_id;
767 struct ieee802154_llsec_params params;
770 ret = rdev_get_llsec_params(rdev, wpan_dev, ¶ms);
774 if (nla_put_u8(msg, NL802154_ATTR_SEC_ENABLED, params.enabled) ||
775 nla_put_u32(msg, NL802154_ATTR_SEC_OUT_LEVEL, params.out_level) ||
776 nla_put_be32(msg, NL802154_ATTR_SEC_FRAME_COUNTER,
777 params.frame_counter))
780 nl_key_id = nla_nest_start(msg, NL802154_ATTR_SEC_OUT_KEY_ID);
784 ret = ieee802154_llsec_send_key_id(msg, ¶ms.out_key);
788 nla_nest_end(msg, nl_key_id);
792 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
795 nl802154_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
796 struct cfg802154_registered_device *rdev,
797 struct wpan_dev *wpan_dev)
799 struct net_device *dev = wpan_dev->netdev;
802 hdr = nl802154hdr_put(msg, portid, seq, flags,
803 NL802154_CMD_NEW_INTERFACE);
808 (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex) ||
809 nla_put_string(msg, NL802154_ATTR_IFNAME, dev->name)))
810 goto nla_put_failure;
812 if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
813 nla_put_u32(msg, NL802154_ATTR_IFTYPE, wpan_dev->iftype) ||
814 nla_put_u64(msg, NL802154_ATTR_WPAN_DEV, wpan_dev_id(wpan_dev)) ||
815 nla_put_u32(msg, NL802154_ATTR_GENERATION,
816 rdev->devlist_generation ^
817 (cfg802154_rdev_list_generation << 2)))
818 goto nla_put_failure;
820 /* address settings */
821 if (nla_put_le64(msg, NL802154_ATTR_EXTENDED_ADDR,
822 wpan_dev->extended_addr) ||
823 nla_put_le16(msg, NL802154_ATTR_SHORT_ADDR,
824 wpan_dev->short_addr) ||
825 nla_put_le16(msg, NL802154_ATTR_PAN_ID, wpan_dev->pan_id))
826 goto nla_put_failure;
829 if (nla_put_s8(msg, NL802154_ATTR_MAX_FRAME_RETRIES,
830 wpan_dev->frame_retries) ||
831 nla_put_u8(msg, NL802154_ATTR_MAX_BE, wpan_dev->max_be) ||
832 nla_put_u8(msg, NL802154_ATTR_MAX_CSMA_BACKOFFS,
833 wpan_dev->csma_retries) ||
834 nla_put_u8(msg, NL802154_ATTR_MIN_BE, wpan_dev->min_be))
835 goto nla_put_failure;
837 /* listen before transmit */
838 if (nla_put_u8(msg, NL802154_ATTR_LBT_MODE, wpan_dev->lbt))
839 goto nla_put_failure;
841 /* ackreq default behaviour */
842 if (nla_put_u8(msg, NL802154_ATTR_ACKREQ_DEFAULT, wpan_dev->ackreq))
843 goto nla_put_failure;
845 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
846 if (nl802154_get_llsec_params(msg, rdev, wpan_dev) < 0)
847 goto nla_put_failure;
848 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
850 genlmsg_end(msg, hdr);
854 genlmsg_cancel(msg, hdr);
859 nl802154_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
863 int wp_start = cb->args[0];
864 int if_start = cb->args[1];
865 struct cfg802154_registered_device *rdev;
866 struct wpan_dev *wpan_dev;
869 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
870 /* TODO netns compare */
871 if (wp_idx < wp_start) {
877 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
878 if (if_idx < if_start) {
882 if (nl802154_send_iface(skb, NETLINK_CB(cb->skb).portid,
883 cb->nlh->nlmsg_seq, NLM_F_MULTI,
884 rdev, wpan_dev) < 0) {
895 cb->args[0] = wp_idx;
896 cb->args[1] = if_idx;
901 static int nl802154_get_interface(struct sk_buff *skb, struct genl_info *info)
904 struct cfg802154_registered_device *rdev = info->user_ptr[0];
905 struct wpan_dev *wdev = info->user_ptr[1];
907 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
911 if (nl802154_send_iface(msg, info->snd_portid, info->snd_seq, 0,
917 return genlmsg_reply(msg, info);
920 static int nl802154_new_interface(struct sk_buff *skb, struct genl_info *info)
922 struct cfg802154_registered_device *rdev = info->user_ptr[0];
923 enum nl802154_iftype type = NL802154_IFTYPE_UNSPEC;
924 __le64 extended_addr = cpu_to_le64(0x0000000000000000ULL);
926 /* TODO avoid failing a new interface
927 * creation due to pending removal?
930 if (!info->attrs[NL802154_ATTR_IFNAME])
933 if (info->attrs[NL802154_ATTR_IFTYPE]) {
934 type = nla_get_u32(info->attrs[NL802154_ATTR_IFTYPE]);
935 if (type > NL802154_IFTYPE_MAX ||
936 !(rdev->wpan_phy.supported.iftypes & BIT(type)))
940 if (info->attrs[NL802154_ATTR_EXTENDED_ADDR])
941 extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
943 if (!rdev->ops->add_virtual_intf)
946 return rdev_add_virtual_intf(rdev,
947 nla_data(info->attrs[NL802154_ATTR_IFNAME]),
948 NET_NAME_USER, type, extended_addr);
951 static int nl802154_del_interface(struct sk_buff *skb, struct genl_info *info)
953 struct cfg802154_registered_device *rdev = info->user_ptr[0];
954 struct wpan_dev *wpan_dev = info->user_ptr[1];
956 if (!rdev->ops->del_virtual_intf)
959 /* If we remove a wpan device without a netdev then clear
960 * user_ptr[1] so that nl802154_post_doit won't dereference it
961 * to check if it needs to do dev_put(). Otherwise it crashes
962 * since the wpan_dev has been freed, unlike with a netdev where
963 * we need the dev_put() for the netdev to really be freed.
965 if (!wpan_dev->netdev)
966 info->user_ptr[1] = NULL;
968 return rdev_del_virtual_intf(rdev, wpan_dev);
971 static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info)
973 struct cfg802154_registered_device *rdev = info->user_ptr[0];
976 if (!info->attrs[NL802154_ATTR_PAGE] ||
977 !info->attrs[NL802154_ATTR_CHANNEL])
980 page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
981 channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]);
983 /* check 802.15.4 constraints */
984 if (page > IEEE802154_MAX_PAGE || channel > IEEE802154_MAX_CHANNEL ||
985 !(rdev->wpan_phy.supported.channels[page] & BIT(channel)))
988 return rdev_set_channel(rdev, page, channel);
991 static int nl802154_set_cca_mode(struct sk_buff *skb, struct genl_info *info)
993 struct cfg802154_registered_device *rdev = info->user_ptr[0];
994 struct wpan_phy_cca cca;
996 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE))
999 if (!info->attrs[NL802154_ATTR_CCA_MODE])
1002 cca.mode = nla_get_u32(info->attrs[NL802154_ATTR_CCA_MODE]);
1003 /* checking 802.15.4 constraints */
1004 if (cca.mode < NL802154_CCA_ENERGY ||
1005 cca.mode > NL802154_CCA_ATTR_MAX ||
1006 !(rdev->wpan_phy.supported.cca_modes & BIT(cca.mode)))
1009 if (cca.mode == NL802154_CCA_ENERGY_CARRIER) {
1010 if (!info->attrs[NL802154_ATTR_CCA_OPT])
1013 cca.opt = nla_get_u32(info->attrs[NL802154_ATTR_CCA_OPT]);
1014 if (cca.opt > NL802154_CCA_OPT_ATTR_MAX ||
1015 !(rdev->wpan_phy.supported.cca_opts & BIT(cca.opt)))
1019 return rdev_set_cca_mode(rdev, &cca);
1022 static int nl802154_set_cca_ed_level(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_CCA_ED_LEVEL))
1031 if (!info->attrs[NL802154_ATTR_CCA_ED_LEVEL])
1034 ed_level = nla_get_s32(info->attrs[NL802154_ATTR_CCA_ED_LEVEL]);
1036 for (i = 0; i < rdev->wpan_phy.supported.cca_ed_levels_size; i++) {
1037 if (ed_level == rdev->wpan_phy.supported.cca_ed_levels[i])
1038 return rdev_set_cca_ed_level(rdev, ed_level);
1044 static int nl802154_set_tx_power(struct sk_buff *skb, struct genl_info *info)
1046 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1050 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER))
1053 if (!info->attrs[NL802154_ATTR_TX_POWER])
1056 power = nla_get_s32(info->attrs[NL802154_ATTR_TX_POWER]);
1058 for (i = 0; i < rdev->wpan_phy.supported.tx_powers_size; i++) {
1059 if (power == rdev->wpan_phy.supported.tx_powers[i])
1060 return rdev_set_tx_power(rdev, power);
1066 static int nl802154_set_pan_id(struct sk_buff *skb, struct genl_info *info)
1068 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1069 struct net_device *dev = info->user_ptr[1];
1070 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1073 /* conflict here while tx/rx calls */
1074 if (netif_running(dev))
1077 /* don't change address fields on monitor */
1078 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1079 !info->attrs[NL802154_ATTR_PAN_ID])
1082 pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
1085 * I am not sure about to check here on broadcast pan_id.
1086 * Broadcast is a valid setting, comment from 802.15.4:
1087 * If this value is 0xffff, the device is not associated.
1089 * This could useful to simple deassociate an device.
1091 if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
1094 return rdev_set_pan_id(rdev, wpan_dev, pan_id);
1097 static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info)
1099 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1100 struct net_device *dev = info->user_ptr[1];
1101 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1104 /* conflict here while tx/rx calls */
1105 if (netif_running(dev))
1108 /* don't change address fields on monitor */
1109 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1110 !info->attrs[NL802154_ATTR_SHORT_ADDR])
1113 short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
1116 * I am not sure about to check here on broadcast short_addr.
1117 * Broadcast is a valid setting, comment from 802.15.4:
1118 * A value of 0xfffe indicates that the device has
1119 * associated but has not been allocated an address. A
1120 * value of 0xffff indicates that the device does not
1121 * have a short address.
1123 * I think we should allow to set these settings but
1124 * don't allow to allow socket communication with it.
1126 if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
1127 short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
1130 return rdev_set_short_addr(rdev, wpan_dev, short_addr);
1134 nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info)
1136 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1137 struct net_device *dev = info->user_ptr[1];
1138 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1141 /* should be set on netif open inside phy settings */
1142 if (netif_running(dev))
1145 if (!info->attrs[NL802154_ATTR_MIN_BE] ||
1146 !info->attrs[NL802154_ATTR_MAX_BE])
1149 min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]);
1150 max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]);
1152 /* check 802.15.4 constraints */
1153 if (min_be < rdev->wpan_phy.supported.min_minbe ||
1154 min_be > rdev->wpan_phy.supported.max_minbe ||
1155 max_be < rdev->wpan_phy.supported.min_maxbe ||
1156 max_be > rdev->wpan_phy.supported.max_maxbe ||
1160 return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
1164 nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info)
1166 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1167 struct net_device *dev = info->user_ptr[1];
1168 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1169 u8 max_csma_backoffs;
1171 /* conflict here while other running iface settings */
1172 if (netif_running(dev))
1175 if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS])
1178 max_csma_backoffs = nla_get_u8(
1179 info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]);
1181 /* check 802.15.4 constraints */
1182 if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs ||
1183 max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs)
1186 return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
1190 nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info)
1192 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1193 struct net_device *dev = info->user_ptr[1];
1194 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1195 s8 max_frame_retries;
1197 if (netif_running(dev))
1200 if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES])
1203 max_frame_retries = nla_get_s8(
1204 info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]);
1206 /* check 802.15.4 constraints */
1207 if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries ||
1208 max_frame_retries > rdev->wpan_phy.supported.max_frame_retries)
1211 return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
1214 static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info)
1216 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1217 struct net_device *dev = info->user_ptr[1];
1218 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1221 if (netif_running(dev))
1224 if (!info->attrs[NL802154_ATTR_LBT_MODE])
1227 mode = nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
1229 if (mode != 0 && mode != 1)
1232 if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt))
1235 return rdev_set_lbt_mode(rdev, wpan_dev, mode);
1239 nl802154_set_ackreq_default(struct sk_buff *skb, struct genl_info *info)
1241 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1242 struct net_device *dev = info->user_ptr[1];
1243 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1246 if (netif_running(dev))
1249 if (!info->attrs[NL802154_ATTR_ACKREQ_DEFAULT])
1252 ackreq = nla_get_u8(info->attrs[NL802154_ATTR_ACKREQ_DEFAULT]);
1254 if (ackreq != 0 && ackreq != 1)
1257 return rdev_set_ackreq_default(rdev, wpan_dev, ackreq);
1260 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
1261 static const struct nla_policy nl802154_dev_addr_policy[NL802154_DEV_ADDR_ATTR_MAX + 1] = {
1262 [NL802154_DEV_ADDR_ATTR_PAN_ID] = { .type = NLA_U16 },
1263 [NL802154_DEV_ADDR_ATTR_MODE] = { .type = NLA_U32 },
1264 [NL802154_DEV_ADDR_ATTR_SHORT] = { .type = NLA_U16 },
1265 [NL802154_DEV_ADDR_ATTR_EXTENDED] = { .type = NLA_U64 },
1269 ieee802154_llsec_parse_dev_addr(struct nlattr *nla,
1270 struct ieee802154_addr *addr)
1272 struct nlattr *attrs[NL802154_DEV_ADDR_ATTR_MAX + 1];
1274 if (!nla || nla_parse_nested(attrs, NL802154_DEV_ADDR_ATTR_MAX, nla,
1275 nl802154_dev_addr_policy))
1278 if (!attrs[NL802154_DEV_ADDR_ATTR_PAN_ID] &&
1279 !attrs[NL802154_DEV_ADDR_ATTR_MODE] &&
1280 !(attrs[NL802154_DEV_ADDR_ATTR_SHORT] ||
1281 attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]))
1284 addr->pan_id = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_PAN_ID]);
1285 addr->mode = nla_get_u32(attrs[NL802154_DEV_ADDR_ATTR_MODE]);
1286 switch (addr->mode) {
1287 case NL802154_DEV_ADDR_SHORT:
1288 addr->short_addr = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_SHORT]);
1290 case NL802154_DEV_ADDR_EXTENDED:
1291 addr->extended_addr = nla_get_le64(attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]);
1300 static const struct nla_policy nl802154_key_id_policy[NL802154_KEY_ID_ATTR_MAX + 1] = {
1301 [NL802154_KEY_ID_ATTR_MODE] = { .type = NLA_U32 },
1302 [NL802154_KEY_ID_ATTR_INDEX] = { .type = NLA_U8 },
1303 [NL802154_KEY_ID_ATTR_IMPLICIT] = { .type = NLA_NESTED },
1304 [NL802154_KEY_ID_ATTR_SOURCE_SHORT] = { .type = NLA_U32 },
1305 [NL802154_KEY_ID_ATTR_SOURCE_EXTENDED] = { .type = NLA_U64 },
1309 ieee802154_llsec_parse_key_id(struct nlattr *nla,
1310 struct ieee802154_llsec_key_id *desc)
1312 struct nlattr *attrs[NL802154_KEY_ID_ATTR_MAX + 1];
1314 if (!nla || nla_parse_nested(attrs, NL802154_KEY_ID_ATTR_MAX, nla,
1315 nl802154_key_id_policy))
1318 if (!attrs[NL802154_KEY_ID_ATTR_MODE])
1321 desc->mode = nla_get_u32(attrs[NL802154_KEY_ID_ATTR_MODE]);
1322 switch (desc->mode) {
1323 case NL802154_KEY_ID_MODE_IMPLICIT:
1324 if (!attrs[NL802154_KEY_ID_ATTR_IMPLICIT])
1327 if (ieee802154_llsec_parse_dev_addr(attrs[NL802154_KEY_ID_ATTR_IMPLICIT],
1328 &desc->device_addr) < 0)
1331 case NL802154_KEY_ID_MODE_INDEX:
1333 case NL802154_KEY_ID_MODE_INDEX_SHORT:
1334 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT])
1337 desc->short_source = nla_get_le32(attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT]);
1339 case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
1340 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED])
1343 desc->extended_source = nla_get_le64(attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED]);
1349 if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
1350 if (!attrs[NL802154_KEY_ID_ATTR_INDEX])
1353 /* TODO change id to idx */
1354 desc->id = nla_get_u8(attrs[NL802154_KEY_ID_ATTR_INDEX]);
1360 static int nl802154_set_llsec_params(struct sk_buff *skb,
1361 struct genl_info *info)
1363 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1364 struct net_device *dev = info->user_ptr[1];
1365 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1366 struct ieee802154_llsec_params params;
1370 if (info->attrs[NL802154_ATTR_SEC_ENABLED]) {
1373 enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1374 if (enabled != 0 && enabled != 1)
1377 params.enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1378 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
1381 if (info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID]) {
1382 ret = ieee802154_llsec_parse_key_id(info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID],
1387 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
1390 if (info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]) {
1391 params.out_level = nla_get_u32(info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]);
1392 if (params.out_level > NL802154_SECLEVEL_MAX)
1395 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
1398 if (info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]) {
1399 params.frame_counter = nla_get_be32(info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]);
1400 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
1403 return rdev_set_llsec_params(rdev, wpan_dev, ¶ms, changed);
1406 static int nl802154_send_key(struct sk_buff *msg, u32 cmd, u32 portid,
1408 struct cfg802154_registered_device *rdev,
1409 struct net_device *dev,
1410 const struct ieee802154_llsec_key_entry *key)
1413 u32 commands[NL802154_CMD_FRAME_NR_IDS / 32];
1414 struct nlattr *nl_key, *nl_key_id;
1416 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1420 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1421 goto nla_put_failure;
1423 nl_key = nla_nest_start(msg, NL802154_ATTR_SEC_KEY);
1425 goto nla_put_failure;
1427 nl_key_id = nla_nest_start(msg, NL802154_KEY_ATTR_ID);
1429 goto nla_put_failure;
1431 if (ieee802154_llsec_send_key_id(msg, &key->id) < 0)
1432 goto nla_put_failure;
1434 nla_nest_end(msg, nl_key_id);
1436 if (nla_put_u8(msg, NL802154_KEY_ATTR_USAGE_FRAMES,
1437 key->key->frame_types))
1438 goto nla_put_failure;
1440 if (key->key->frame_types & BIT(NL802154_FRAME_CMD)) {
1441 /* TODO for each nested */
1442 memset(commands, 0, sizeof(commands));
1443 commands[7] = key->key->cmd_frame_ids;
1444 if (nla_put(msg, NL802154_KEY_ATTR_USAGE_CMDS,
1445 sizeof(commands), commands))
1446 goto nla_put_failure;
1449 if (nla_put(msg, NL802154_KEY_ATTR_BYTES, NL802154_KEY_SIZE,
1451 goto nla_put_failure;
1453 nla_nest_end(msg, nl_key);
1454 genlmsg_end(msg, hdr);
1459 genlmsg_cancel(msg, hdr);
1464 nl802154_dump_llsec_key(struct sk_buff *skb, struct netlink_callback *cb)
1466 struct cfg802154_registered_device *rdev = NULL;
1467 struct ieee802154_llsec_key_entry *key;
1468 struct ieee802154_llsec_table *table;
1469 struct wpan_dev *wpan_dev;
1472 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1476 if (!wpan_dev->netdev) {
1481 rdev_lock_llsec_table(rdev, wpan_dev);
1482 rdev_get_llsec_table(rdev, wpan_dev, &table);
1484 /* TODO make it like station dump */
1488 list_for_each_entry(key, &table->keys, list) {
1489 if (nl802154_send_key(skb, NL802154_CMD_NEW_SEC_KEY,
1490 NETLINK_CB(cb->skb).portid,
1491 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1492 rdev, wpan_dev->netdev, key) < 0) {
1495 rdev_unlock_llsec_table(rdev, wpan_dev);
1503 rdev_unlock_llsec_table(rdev, wpan_dev);
1506 nl802154_finish_wpan_dev_dump(rdev);
1511 static const struct nla_policy nl802154_key_policy[NL802154_KEY_ATTR_MAX + 1] = {
1512 [NL802154_KEY_ATTR_ID] = { NLA_NESTED },
1513 /* TODO handle it as for_each_nested and NLA_FLAG? */
1514 [NL802154_KEY_ATTR_USAGE_FRAMES] = { NLA_U8 },
1515 /* TODO handle it as for_each_nested, not static array? */
1516 [NL802154_KEY_ATTR_USAGE_CMDS] = { .len = NL802154_CMD_FRAME_NR_IDS / 8 },
1517 [NL802154_KEY_ATTR_BYTES] = { .len = NL802154_KEY_SIZE },
1520 static int nl802154_add_llsec_key(struct sk_buff *skb, struct genl_info *info)
1522 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1523 struct net_device *dev = info->user_ptr[1];
1524 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1525 struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1526 struct ieee802154_llsec_key key = { };
1527 struct ieee802154_llsec_key_id id = { };
1528 u32 commands[NL802154_CMD_FRAME_NR_IDS / 32] = { };
1530 if (nla_parse_nested(attrs, NL802154_KEY_ATTR_MAX,
1531 info->attrs[NL802154_ATTR_SEC_KEY],
1532 nl802154_key_policy))
1535 if (!attrs[NL802154_KEY_ATTR_USAGE_FRAMES] ||
1536 !attrs[NL802154_KEY_ATTR_BYTES])
1539 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1542 key.frame_types = nla_get_u8(attrs[NL802154_KEY_ATTR_USAGE_FRAMES]);
1543 if (key.frame_types > BIT(NL802154_FRAME_MAX) ||
1544 ((key.frame_types & BIT(NL802154_FRAME_CMD)) &&
1545 !attrs[NL802154_KEY_ATTR_USAGE_CMDS]))
1548 if (attrs[NL802154_KEY_ATTR_USAGE_CMDS]) {
1549 /* TODO for each nested */
1550 nla_memcpy(commands, attrs[NL802154_KEY_ATTR_USAGE_CMDS],
1551 NL802154_CMD_FRAME_NR_IDS / 8);
1553 /* TODO understand the -EINVAL logic here? last condition */
1554 if (commands[0] || commands[1] || commands[2] || commands[3] ||
1555 commands[4] || commands[5] || commands[6] ||
1556 commands[7] > BIT(NL802154_CMD_FRAME_MAX))
1559 key.cmd_frame_ids = commands[7];
1561 key.cmd_frame_ids = 0;
1564 nla_memcpy(key.key, attrs[NL802154_KEY_ATTR_BYTES], NL802154_KEY_SIZE);
1566 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1569 return rdev_add_llsec_key(rdev, wpan_dev, &id, &key);
1572 static int nl802154_del_llsec_key(struct sk_buff *skb, struct genl_info *info)
1574 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1575 struct net_device *dev = info->user_ptr[1];
1576 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1577 struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1578 struct ieee802154_llsec_key_id id;
1580 if (nla_parse_nested(attrs, NL802154_KEY_ATTR_MAX,
1581 info->attrs[NL802154_ATTR_SEC_KEY],
1582 nl802154_key_policy))
1585 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1588 return rdev_del_llsec_key(rdev, wpan_dev, &id);
1591 static int nl802154_send_device(struct sk_buff *msg, u32 cmd, u32 portid,
1593 struct cfg802154_registered_device *rdev,
1594 struct net_device *dev,
1595 const struct ieee802154_llsec_device *dev_desc)
1598 struct nlattr *nl_device;
1600 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1604 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1605 goto nla_put_failure;
1607 nl_device = nla_nest_start(msg, NL802154_ATTR_SEC_DEVICE);
1609 goto nla_put_failure;
1611 if (nla_put_u32(msg, NL802154_DEV_ATTR_FRAME_COUNTER,
1612 dev_desc->frame_counter) ||
1613 nla_put_le16(msg, NL802154_DEV_ATTR_PAN_ID, dev_desc->pan_id) ||
1614 nla_put_le16(msg, NL802154_DEV_ATTR_SHORT_ADDR,
1615 dev_desc->short_addr) ||
1616 nla_put_le64(msg, NL802154_DEV_ATTR_EXTENDED_ADDR,
1617 dev_desc->hwaddr) ||
1618 nla_put_u8(msg, NL802154_DEV_ATTR_SECLEVEL_EXEMPT,
1619 dev_desc->seclevel_exempt) ||
1620 nla_put_u32(msg, NL802154_DEV_ATTR_KEY_MODE, dev_desc->key_mode))
1621 goto nla_put_failure;
1623 nla_nest_end(msg, nl_device);
1624 genlmsg_end(msg, hdr);
1629 genlmsg_cancel(msg, hdr);
1634 nl802154_dump_llsec_dev(struct sk_buff *skb, struct netlink_callback *cb)
1636 struct cfg802154_registered_device *rdev = NULL;
1637 struct ieee802154_llsec_device *dev;
1638 struct ieee802154_llsec_table *table;
1639 struct wpan_dev *wpan_dev;
1642 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1646 if (!wpan_dev->netdev) {
1651 rdev_lock_llsec_table(rdev, wpan_dev);
1652 rdev_get_llsec_table(rdev, wpan_dev, &table);
1654 /* TODO make it like station dump */
1658 list_for_each_entry(dev, &table->devices, list) {
1659 if (nl802154_send_device(skb, NL802154_CMD_NEW_SEC_LEVEL,
1660 NETLINK_CB(cb->skb).portid,
1661 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1662 rdev, wpan_dev->netdev, dev) < 0) {
1665 rdev_unlock_llsec_table(rdev, wpan_dev);
1673 rdev_unlock_llsec_table(rdev, wpan_dev);
1676 nl802154_finish_wpan_dev_dump(rdev);
1681 static const struct nla_policy nl802154_dev_policy[NL802154_DEV_ATTR_MAX + 1] = {
1682 [NL802154_DEV_ATTR_FRAME_COUNTER] = { NLA_U32 },
1683 [NL802154_DEV_ATTR_PAN_ID] = { .type = NLA_U16 },
1684 [NL802154_DEV_ATTR_SHORT_ADDR] = { .type = NLA_U16 },
1685 [NL802154_DEV_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
1686 [NL802154_DEV_ATTR_SECLEVEL_EXEMPT] = { NLA_U8 },
1687 [NL802154_DEV_ATTR_KEY_MODE] = { NLA_U32 },
1691 ieee802154_llsec_parse_device(struct nlattr *nla,
1692 struct ieee802154_llsec_device *dev)
1694 struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
1696 if (!nla || nla_parse_nested(attrs, NL802154_DEV_ATTR_MAX, nla,
1697 nl802154_dev_policy))
1700 memset(dev, 0, sizeof(*dev));
1702 if (!attrs[NL802154_DEV_ATTR_FRAME_COUNTER] ||
1703 !attrs[NL802154_DEV_ATTR_PAN_ID] ||
1704 !attrs[NL802154_DEV_ATTR_SHORT_ADDR] ||
1705 !attrs[NL802154_DEV_ATTR_EXTENDED_ADDR] ||
1706 !attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] ||
1707 !attrs[NL802154_DEV_ATTR_KEY_MODE])
1711 dev->frame_counter = nla_get_u32(attrs[NL802154_DEV_ATTR_FRAME_COUNTER]);
1712 dev->pan_id = nla_get_le16(attrs[NL802154_DEV_ATTR_PAN_ID]);
1713 dev->short_addr = nla_get_le16(attrs[NL802154_DEV_ATTR_SHORT_ADDR]);
1714 /* TODO rename hwaddr to extended_addr */
1715 dev->hwaddr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
1716 dev->seclevel_exempt = nla_get_u8(attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT]);
1717 dev->key_mode = nla_get_u32(attrs[NL802154_DEV_ATTR_KEY_MODE]);
1719 if (dev->key_mode > NL802154_DEVKEY_MAX ||
1720 (dev->seclevel_exempt != 0 && dev->seclevel_exempt != 1))
1726 static int nl802154_add_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1728 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1729 struct net_device *dev = info->user_ptr[1];
1730 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1731 struct ieee802154_llsec_device dev_desc;
1733 if (ieee802154_llsec_parse_device(info->attrs[NL802154_ATTR_SEC_DEVICE],
1737 return rdev_add_device(rdev, wpan_dev, &dev_desc);
1740 static int nl802154_del_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1742 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1743 struct net_device *dev = info->user_ptr[1];
1744 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1745 struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
1746 __le64 extended_addr;
1748 if (nla_parse_nested(attrs, NL802154_DEV_ATTR_MAX,
1749 info->attrs[NL802154_ATTR_SEC_DEVICE],
1750 nl802154_dev_policy))
1753 if (!attrs[NL802154_DEV_ATTR_EXTENDED_ADDR])
1756 extended_addr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
1757 return rdev_del_device(rdev, wpan_dev, extended_addr);
1760 static int nl802154_send_devkey(struct sk_buff *msg, u32 cmd, u32 portid,
1762 struct cfg802154_registered_device *rdev,
1763 struct net_device *dev, __le64 extended_addr,
1764 const struct ieee802154_llsec_device_key *devkey)
1767 struct nlattr *nl_devkey, *nl_key_id;
1769 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1773 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1774 goto nla_put_failure;
1776 nl_devkey = nla_nest_start(msg, NL802154_ATTR_SEC_DEVKEY);
1778 goto nla_put_failure;
1780 if (nla_put_le64(msg, NL802154_DEVKEY_ATTR_EXTENDED_ADDR,
1782 nla_put_u32(msg, NL802154_DEVKEY_ATTR_FRAME_COUNTER,
1783 devkey->frame_counter))
1784 goto nla_put_failure;
1786 nl_key_id = nla_nest_start(msg, NL802154_DEVKEY_ATTR_ID);
1788 goto nla_put_failure;
1790 if (ieee802154_llsec_send_key_id(msg, &devkey->key_id) < 0)
1791 goto nla_put_failure;
1793 nla_nest_end(msg, nl_key_id);
1794 nla_nest_end(msg, nl_devkey);
1795 genlmsg_end(msg, hdr);
1800 genlmsg_cancel(msg, hdr);
1805 nl802154_dump_llsec_devkey(struct sk_buff *skb, struct netlink_callback *cb)
1807 struct cfg802154_registered_device *rdev = NULL;
1808 struct ieee802154_llsec_device_key *kpos;
1809 struct ieee802154_llsec_device *dpos;
1810 struct ieee802154_llsec_table *table;
1811 struct wpan_dev *wpan_dev;
1814 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1818 if (!wpan_dev->netdev) {
1823 rdev_lock_llsec_table(rdev, wpan_dev);
1824 rdev_get_llsec_table(rdev, wpan_dev, &table);
1826 /* TODO make it like station dump */
1830 /* TODO look if remove devkey and do some nested attribute */
1831 list_for_each_entry(dpos, &table->devices, list) {
1832 list_for_each_entry(kpos, &dpos->keys, list) {
1833 if (nl802154_send_devkey(skb,
1834 NL802154_CMD_NEW_SEC_LEVEL,
1835 NETLINK_CB(cb->skb).portid,
1843 rdev_unlock_llsec_table(rdev, wpan_dev);
1852 rdev_unlock_llsec_table(rdev, wpan_dev);
1855 nl802154_finish_wpan_dev_dump(rdev);
1860 static const struct nla_policy nl802154_devkey_policy[NL802154_DEVKEY_ATTR_MAX + 1] = {
1861 [NL802154_DEVKEY_ATTR_FRAME_COUNTER] = { NLA_U32 },
1862 [NL802154_DEVKEY_ATTR_EXTENDED_ADDR] = { NLA_U64 },
1863 [NL802154_DEVKEY_ATTR_ID] = { NLA_NESTED },
1866 static int nl802154_add_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
1868 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1869 struct net_device *dev = info->user_ptr[1];
1870 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1871 struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
1872 struct ieee802154_llsec_device_key key;
1873 __le64 extended_addr;
1875 if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
1876 nla_parse_nested(attrs, NL802154_DEVKEY_ATTR_MAX,
1877 info->attrs[NL802154_ATTR_SEC_DEVKEY],
1878 nl802154_devkey_policy) < 0)
1881 if (!attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER] ||
1882 !attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1885 /* TODO change key.id ? */
1886 if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1891 key.frame_counter = nla_get_u32(attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER]);
1892 /* TODO change naming hwaddr -> extended_addr
1893 * check unique identifier short+pan OR extended_addr
1895 extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1896 return rdev_add_devkey(rdev, wpan_dev, extended_addr, &key);
1899 static int nl802154_del_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
1901 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1902 struct net_device *dev = info->user_ptr[1];
1903 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1904 struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
1905 struct ieee802154_llsec_device_key key;
1906 __le64 extended_addr;
1908 if (nla_parse_nested(attrs, NL802154_DEVKEY_ATTR_MAX,
1909 info->attrs[NL802154_ATTR_SEC_DEVKEY],
1910 nl802154_devkey_policy))
1913 if (!attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1916 /* TODO change key.id ? */
1917 if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1921 /* TODO change naming hwaddr -> extended_addr
1922 * check unique identifier short+pan OR extended_addr
1924 extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1925 return rdev_del_devkey(rdev, wpan_dev, extended_addr, &key);
1928 static int nl802154_send_seclevel(struct sk_buff *msg, u32 cmd, u32 portid,
1930 struct cfg802154_registered_device *rdev,
1931 struct net_device *dev,
1932 const struct ieee802154_llsec_seclevel *sl)
1935 struct nlattr *nl_seclevel;
1937 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1941 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1942 goto nla_put_failure;
1944 nl_seclevel = nla_nest_start(msg, NL802154_ATTR_SEC_LEVEL);
1946 goto nla_put_failure;
1948 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_FRAME, sl->frame_type) ||
1949 nla_put_u32(msg, NL802154_SECLEVEL_ATTR_LEVELS, sl->sec_levels) ||
1950 nla_put_u8(msg, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE,
1951 sl->device_override))
1952 goto nla_put_failure;
1954 if (sl->frame_type == NL802154_FRAME_CMD) {
1955 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_CMD_FRAME,
1957 goto nla_put_failure;
1960 nla_nest_end(msg, nl_seclevel);
1961 genlmsg_end(msg, hdr);
1966 genlmsg_cancel(msg, hdr);
1971 nl802154_dump_llsec_seclevel(struct sk_buff *skb, struct netlink_callback *cb)
1973 struct cfg802154_registered_device *rdev = NULL;
1974 struct ieee802154_llsec_seclevel *sl;
1975 struct ieee802154_llsec_table *table;
1976 struct wpan_dev *wpan_dev;
1979 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1983 if (!wpan_dev->netdev) {
1988 rdev_lock_llsec_table(rdev, wpan_dev);
1989 rdev_get_llsec_table(rdev, wpan_dev, &table);
1991 /* TODO make it like station dump */
1995 list_for_each_entry(sl, &table->security_levels, list) {
1996 if (nl802154_send_seclevel(skb, NL802154_CMD_NEW_SEC_LEVEL,
1997 NETLINK_CB(cb->skb).portid,
1998 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1999 rdev, wpan_dev->netdev, sl) < 0) {
2002 rdev_unlock_llsec_table(rdev, wpan_dev);
2010 rdev_unlock_llsec_table(rdev, wpan_dev);
2013 nl802154_finish_wpan_dev_dump(rdev);
2018 static const struct nla_policy nl802154_seclevel_policy[NL802154_SECLEVEL_ATTR_MAX + 1] = {
2019 [NL802154_SECLEVEL_ATTR_LEVELS] = { .type = NLA_U8 },
2020 [NL802154_SECLEVEL_ATTR_FRAME] = { .type = NLA_U32 },
2021 [NL802154_SECLEVEL_ATTR_CMD_FRAME] = { .type = NLA_U32 },
2022 [NL802154_SECLEVEL_ATTR_DEV_OVERRIDE] = { .type = NLA_U8 },
2026 llsec_parse_seclevel(struct nlattr *nla, struct ieee802154_llsec_seclevel *sl)
2028 struct nlattr *attrs[NL802154_SECLEVEL_ATTR_MAX + 1];
2030 if (!nla || nla_parse_nested(attrs, NL802154_SECLEVEL_ATTR_MAX, nla,
2031 nl802154_seclevel_policy))
2034 memset(sl, 0, sizeof(*sl));
2036 if (!attrs[NL802154_SECLEVEL_ATTR_LEVELS] ||
2037 !attrs[NL802154_SECLEVEL_ATTR_FRAME] ||
2038 !attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE])
2041 sl->sec_levels = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_LEVELS]);
2042 sl->frame_type = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_FRAME]);
2043 sl->device_override = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE]);
2044 if (sl->frame_type > NL802154_FRAME_MAX ||
2045 (sl->device_override != 0 && sl->device_override != 1))
2048 if (sl->frame_type == NL802154_FRAME_CMD) {
2049 if (!attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME])
2052 sl->cmd_frame_id = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME]);
2053 if (sl->cmd_frame_id > NL802154_CMD_FRAME_MAX)
2060 static int nl802154_add_llsec_seclevel(struct sk_buff *skb,
2061 struct genl_info *info)
2063 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2064 struct net_device *dev = info->user_ptr[1];
2065 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2066 struct ieee802154_llsec_seclevel sl;
2068 if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2072 return rdev_add_seclevel(rdev, wpan_dev, &sl);
2075 static int nl802154_del_llsec_seclevel(struct sk_buff *skb,
2076 struct genl_info *info)
2078 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2079 struct net_device *dev = info->user_ptr[1];
2080 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2081 struct ieee802154_llsec_seclevel sl;
2083 if (!info->attrs[NL802154_ATTR_SEC_LEVEL] ||
2084 llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2088 return rdev_del_seclevel(rdev, wpan_dev, &sl);
2090 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2092 #define NL802154_FLAG_NEED_WPAN_PHY 0x01
2093 #define NL802154_FLAG_NEED_NETDEV 0x02
2094 #define NL802154_FLAG_NEED_RTNL 0x04
2095 #define NL802154_FLAG_CHECK_NETDEV_UP 0x08
2096 #define NL802154_FLAG_NEED_NETDEV_UP (NL802154_FLAG_NEED_NETDEV |\
2097 NL802154_FLAG_CHECK_NETDEV_UP)
2098 #define NL802154_FLAG_NEED_WPAN_DEV 0x10
2099 #define NL802154_FLAG_NEED_WPAN_DEV_UP (NL802154_FLAG_NEED_WPAN_DEV |\
2100 NL802154_FLAG_CHECK_NETDEV_UP)
2102 static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
2103 struct genl_info *info)
2105 struct cfg802154_registered_device *rdev;
2106 struct wpan_dev *wpan_dev;
2107 struct net_device *dev;
2108 bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
2113 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
2114 rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
2118 return PTR_ERR(rdev);
2120 info->user_ptr[0] = rdev;
2121 } else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
2122 ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2124 wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
2126 if (IS_ERR(wpan_dev)) {
2129 return PTR_ERR(wpan_dev);
2132 dev = wpan_dev->netdev;
2133 rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
2135 if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
2142 info->user_ptr[1] = dev;
2144 info->user_ptr[1] = wpan_dev;
2148 if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
2149 !netif_running(dev)) {
2158 info->user_ptr[0] = rdev;
2164 static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
2165 struct genl_info *info)
2167 if (info->user_ptr[1]) {
2168 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2169 struct wpan_dev *wpan_dev = info->user_ptr[1];
2171 if (wpan_dev->netdev)
2172 dev_put(wpan_dev->netdev);
2174 dev_put(info->user_ptr[1]);
2178 if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
2182 static const struct genl_ops nl802154_ops[] = {
2184 .cmd = NL802154_CMD_GET_WPAN_PHY,
2185 .doit = nl802154_get_wpan_phy,
2186 .dumpit = nl802154_dump_wpan_phy,
2187 .done = nl802154_dump_wpan_phy_done,
2188 .policy = nl802154_policy,
2189 /* can be retrieved by unprivileged users */
2190 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2191 NL802154_FLAG_NEED_RTNL,
2194 .cmd = NL802154_CMD_GET_INTERFACE,
2195 .doit = nl802154_get_interface,
2196 .dumpit = nl802154_dump_interface,
2197 .policy = nl802154_policy,
2198 /* can be retrieved by unprivileged users */
2199 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2200 NL802154_FLAG_NEED_RTNL,
2203 .cmd = NL802154_CMD_NEW_INTERFACE,
2204 .doit = nl802154_new_interface,
2205 .policy = nl802154_policy,
2206 .flags = GENL_ADMIN_PERM,
2207 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2208 NL802154_FLAG_NEED_RTNL,
2211 .cmd = NL802154_CMD_DEL_INTERFACE,
2212 .doit = nl802154_del_interface,
2213 .policy = nl802154_policy,
2214 .flags = GENL_ADMIN_PERM,
2215 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2216 NL802154_FLAG_NEED_RTNL,
2219 .cmd = NL802154_CMD_SET_CHANNEL,
2220 .doit = nl802154_set_channel,
2221 .policy = nl802154_policy,
2222 .flags = GENL_ADMIN_PERM,
2223 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2224 NL802154_FLAG_NEED_RTNL,
2227 .cmd = NL802154_CMD_SET_CCA_MODE,
2228 .doit = nl802154_set_cca_mode,
2229 .policy = nl802154_policy,
2230 .flags = GENL_ADMIN_PERM,
2231 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2232 NL802154_FLAG_NEED_RTNL,
2235 .cmd = NL802154_CMD_SET_CCA_ED_LEVEL,
2236 .doit = nl802154_set_cca_ed_level,
2237 .policy = nl802154_policy,
2238 .flags = GENL_ADMIN_PERM,
2239 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2240 NL802154_FLAG_NEED_RTNL,
2243 .cmd = NL802154_CMD_SET_TX_POWER,
2244 .doit = nl802154_set_tx_power,
2245 .policy = nl802154_policy,
2246 .flags = GENL_ADMIN_PERM,
2247 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2248 NL802154_FLAG_NEED_RTNL,
2251 .cmd = NL802154_CMD_SET_PAN_ID,
2252 .doit = nl802154_set_pan_id,
2253 .policy = nl802154_policy,
2254 .flags = GENL_ADMIN_PERM,
2255 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2256 NL802154_FLAG_NEED_RTNL,
2259 .cmd = NL802154_CMD_SET_SHORT_ADDR,
2260 .doit = nl802154_set_short_addr,
2261 .policy = nl802154_policy,
2262 .flags = GENL_ADMIN_PERM,
2263 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2264 NL802154_FLAG_NEED_RTNL,
2267 .cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
2268 .doit = nl802154_set_backoff_exponent,
2269 .policy = nl802154_policy,
2270 .flags = GENL_ADMIN_PERM,
2271 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2272 NL802154_FLAG_NEED_RTNL,
2275 .cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
2276 .doit = nl802154_set_max_csma_backoffs,
2277 .policy = nl802154_policy,
2278 .flags = GENL_ADMIN_PERM,
2279 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2280 NL802154_FLAG_NEED_RTNL,
2283 .cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
2284 .doit = nl802154_set_max_frame_retries,
2285 .policy = nl802154_policy,
2286 .flags = GENL_ADMIN_PERM,
2287 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2288 NL802154_FLAG_NEED_RTNL,
2291 .cmd = NL802154_CMD_SET_LBT_MODE,
2292 .doit = nl802154_set_lbt_mode,
2293 .policy = nl802154_policy,
2294 .flags = GENL_ADMIN_PERM,
2295 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2296 NL802154_FLAG_NEED_RTNL,
2299 .cmd = NL802154_CMD_SET_ACKREQ_DEFAULT,
2300 .doit = nl802154_set_ackreq_default,
2301 .policy = nl802154_policy,
2302 .flags = GENL_ADMIN_PERM,
2303 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2304 NL802154_FLAG_NEED_RTNL,
2306 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
2308 .cmd = NL802154_CMD_SET_SEC_PARAMS,
2309 .doit = nl802154_set_llsec_params,
2310 .policy = nl802154_policy,
2311 .flags = GENL_ADMIN_PERM,
2312 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2313 NL802154_FLAG_NEED_RTNL,
2316 .cmd = NL802154_CMD_GET_SEC_KEY,
2317 /* TODO .doit by matching key id? */
2318 .dumpit = nl802154_dump_llsec_key,
2319 .policy = nl802154_policy,
2320 .flags = GENL_ADMIN_PERM,
2321 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2322 NL802154_FLAG_NEED_RTNL,
2325 .cmd = NL802154_CMD_NEW_SEC_KEY,
2326 .doit = nl802154_add_llsec_key,
2327 .policy = nl802154_policy,
2328 .flags = GENL_ADMIN_PERM,
2329 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2330 NL802154_FLAG_NEED_RTNL,
2333 .cmd = NL802154_CMD_DEL_SEC_KEY,
2334 .doit = nl802154_del_llsec_key,
2335 .policy = nl802154_policy,
2336 .flags = GENL_ADMIN_PERM,
2337 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2338 NL802154_FLAG_NEED_RTNL,
2340 /* TODO unique identifier must short+pan OR extended_addr */
2342 .cmd = NL802154_CMD_GET_SEC_DEV,
2343 /* TODO .doit by matching extended_addr? */
2344 .dumpit = nl802154_dump_llsec_dev,
2345 .policy = nl802154_policy,
2346 .flags = GENL_ADMIN_PERM,
2347 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2348 NL802154_FLAG_NEED_RTNL,
2351 .cmd = NL802154_CMD_NEW_SEC_DEV,
2352 .doit = nl802154_add_llsec_dev,
2353 .policy = nl802154_policy,
2354 .flags = GENL_ADMIN_PERM,
2355 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2356 NL802154_FLAG_NEED_RTNL,
2359 .cmd = NL802154_CMD_DEL_SEC_DEV,
2360 .doit = nl802154_del_llsec_dev,
2361 .policy = nl802154_policy,
2362 .flags = GENL_ADMIN_PERM,
2363 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2364 NL802154_FLAG_NEED_RTNL,
2366 /* TODO remove complete devkey, put it as nested? */
2368 .cmd = NL802154_CMD_GET_SEC_DEVKEY,
2369 /* TODO doit by matching ??? */
2370 .dumpit = nl802154_dump_llsec_devkey,
2371 .policy = nl802154_policy,
2372 .flags = GENL_ADMIN_PERM,
2373 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2374 NL802154_FLAG_NEED_RTNL,
2377 .cmd = NL802154_CMD_NEW_SEC_DEVKEY,
2378 .doit = nl802154_add_llsec_devkey,
2379 .policy = nl802154_policy,
2380 .flags = GENL_ADMIN_PERM,
2381 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2382 NL802154_FLAG_NEED_RTNL,
2385 .cmd = NL802154_CMD_DEL_SEC_DEVKEY,
2386 .doit = nl802154_del_llsec_devkey,
2387 .policy = nl802154_policy,
2388 .flags = GENL_ADMIN_PERM,
2389 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2390 NL802154_FLAG_NEED_RTNL,
2393 .cmd = NL802154_CMD_GET_SEC_LEVEL,
2394 /* TODO .doit by matching frame_type? */
2395 .dumpit = nl802154_dump_llsec_seclevel,
2396 .policy = nl802154_policy,
2397 .flags = GENL_ADMIN_PERM,
2398 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2399 NL802154_FLAG_NEED_RTNL,
2402 .cmd = NL802154_CMD_NEW_SEC_LEVEL,
2403 .doit = nl802154_add_llsec_seclevel,
2404 .policy = nl802154_policy,
2405 .flags = GENL_ADMIN_PERM,
2406 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2407 NL802154_FLAG_NEED_RTNL,
2410 .cmd = NL802154_CMD_DEL_SEC_LEVEL,
2411 /* TODO match frame_type only? */
2412 .doit = nl802154_del_llsec_seclevel,
2413 .policy = nl802154_policy,
2414 .flags = GENL_ADMIN_PERM,
2415 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2416 NL802154_FLAG_NEED_RTNL,
2418 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2421 /* initialisation/exit functions */
2422 int nl802154_init(void)
2424 return genl_register_family_with_ops_groups(&nl802154_fam, nl802154_ops,
2428 void nl802154_exit(void)
2430 genl_unregister_family(&nl802154_fam);