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 /* the netlink family */
30 static struct genl_family nl802154_fam;
32 /* multicast groups */
33 enum nl802154_multicast_groups {
34 NL802154_MCGRP_CONFIG,
37 static const struct genl_multicast_group nl802154_mcgrps[] = {
38 [NL802154_MCGRP_CONFIG] = { .name = "config", },
41 /* returns ERR_PTR values */
42 static struct wpan_dev *
43 __cfg802154_wpan_dev_from_attrs(struct net *netns, struct nlattr **attrs)
45 struct cfg802154_registered_device *rdev;
46 struct wpan_dev *result = NULL;
47 bool have_ifidx = attrs[NL802154_ATTR_IFINDEX];
48 bool have_wpan_dev_id = attrs[NL802154_ATTR_WPAN_DEV];
50 int wpan_phy_idx = -1;
55 if (!have_ifidx && !have_wpan_dev_id)
56 return ERR_PTR(-EINVAL);
59 ifidx = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
60 if (have_wpan_dev_id) {
61 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
62 wpan_phy_idx = wpan_dev_id >> 32;
65 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
66 struct wpan_dev *wpan_dev;
68 if (wpan_phy_net(&rdev->wpan_phy) != netns)
71 if (have_wpan_dev_id && rdev->wpan_phy_idx != wpan_phy_idx)
74 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
75 if (have_ifidx && wpan_dev->netdev &&
76 wpan_dev->netdev->ifindex == ifidx) {
80 if (have_wpan_dev_id &&
81 wpan_dev->identifier == (u32)wpan_dev_id) {
94 return ERR_PTR(-ENODEV);
97 static struct cfg802154_registered_device *
98 __cfg802154_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
100 struct cfg802154_registered_device *rdev = NULL, *tmp;
101 struct net_device *netdev;
105 if (!attrs[NL802154_ATTR_WPAN_PHY] &&
106 !attrs[NL802154_ATTR_IFINDEX] &&
107 !attrs[NL802154_ATTR_WPAN_DEV])
108 return ERR_PTR(-EINVAL);
110 if (attrs[NL802154_ATTR_WPAN_PHY])
111 rdev = cfg802154_rdev_by_wpan_phy_idx(
112 nla_get_u32(attrs[NL802154_ATTR_WPAN_PHY]));
114 if (attrs[NL802154_ATTR_WPAN_DEV]) {
115 u64 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
116 struct wpan_dev *wpan_dev;
119 tmp = cfg802154_rdev_by_wpan_phy_idx(wpan_dev_id >> 32);
121 /* make sure wpan_dev exists */
122 list_for_each_entry(wpan_dev, &tmp->wpan_dev_list, list) {
123 if (wpan_dev->identifier != (u32)wpan_dev_id)
132 if (rdev && tmp != rdev)
133 return ERR_PTR(-EINVAL);
138 if (attrs[NL802154_ATTR_IFINDEX]) {
139 int ifindex = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
141 netdev = __dev_get_by_index(netns, ifindex);
143 if (netdev->ieee802154_ptr)
144 tmp = wpan_phy_to_rdev(
145 netdev->ieee802154_ptr->wpan_phy);
149 /* not wireless device -- return error */
151 return ERR_PTR(-EINVAL);
153 /* mismatch -- return error */
154 if (rdev && tmp != rdev)
155 return ERR_PTR(-EINVAL);
162 return ERR_PTR(-ENODEV);
164 if (netns != wpan_phy_net(&rdev->wpan_phy))
165 return ERR_PTR(-ENODEV);
170 /* This function returns a pointer to the driver
171 * that the genl_info item that is passed refers to.
173 * The result of this can be a PTR_ERR and hence must
174 * be checked with IS_ERR() for errors.
176 static struct cfg802154_registered_device *
177 cfg802154_get_dev_from_info(struct net *netns, struct genl_info *info)
179 return __cfg802154_rdev_from_attrs(netns, info->attrs);
182 /* policy for the attributes */
183 static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
184 [NL802154_ATTR_WPAN_PHY] = { .type = NLA_U32 },
185 [NL802154_ATTR_WPAN_PHY_NAME] = { .type = NLA_NUL_STRING,
188 [NL802154_ATTR_IFINDEX] = { .type = NLA_U32 },
189 [NL802154_ATTR_IFTYPE] = { .type = NLA_U32 },
190 [NL802154_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
192 [NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
194 [NL802154_ATTR_PAGE] = { .type = NLA_U8, },
195 [NL802154_ATTR_CHANNEL] = { .type = NLA_U8, },
197 [NL802154_ATTR_TX_POWER] = { .type = NLA_S32, },
199 [NL802154_ATTR_CCA_MODE] = { .type = NLA_U32, },
200 [NL802154_ATTR_CCA_OPT] = { .type = NLA_U32, },
201 [NL802154_ATTR_CCA_ED_LEVEL] = { .type = NLA_S32, },
203 [NL802154_ATTR_SUPPORTED_CHANNEL] = { .type = NLA_U32, },
205 [NL802154_ATTR_PAN_ID] = { .type = NLA_U16, },
206 [NL802154_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
207 [NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16, },
209 [NL802154_ATTR_MIN_BE] = { .type = NLA_U8, },
210 [NL802154_ATTR_MAX_BE] = { .type = NLA_U8, },
211 [NL802154_ATTR_MAX_CSMA_BACKOFFS] = { .type = NLA_U8, },
213 [NL802154_ATTR_MAX_FRAME_RETRIES] = { .type = NLA_S8, },
215 [NL802154_ATTR_LBT_MODE] = { .type = NLA_U8, },
217 [NL802154_ATTR_WPAN_PHY_CAPS] = { .type = NLA_NESTED },
219 [NL802154_ATTR_SUPPORTED_COMMANDS] = { .type = NLA_NESTED },
221 [NL802154_ATTR_ACKREQ_DEFAULT] = { .type = NLA_U8 },
223 [NL802154_ATTR_PID] = { .type = NLA_U32 },
224 [NL802154_ATTR_NETNS_FD] = { .type = NLA_U32 },
225 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
226 [NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
227 [NL802154_ATTR_SEC_OUT_LEVEL] = { .type = NLA_U32, },
228 [NL802154_ATTR_SEC_OUT_KEY_ID] = { .type = NLA_NESTED, },
229 [NL802154_ATTR_SEC_FRAME_COUNTER] = { .type = NLA_U32 },
231 [NL802154_ATTR_SEC_LEVEL] = { .type = NLA_NESTED },
232 [NL802154_ATTR_SEC_DEVICE] = { .type = NLA_NESTED },
233 [NL802154_ATTR_SEC_DEVKEY] = { .type = NLA_NESTED },
234 [NL802154_ATTR_SEC_KEY] = { .type = NLA_NESTED },
235 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
238 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
240 nl802154_prepare_wpan_dev_dump(struct sk_buff *skb,
241 struct netlink_callback *cb,
242 struct cfg802154_registered_device **rdev,
243 struct wpan_dev **wpan_dev)
250 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize,
251 genl_family_attrbuf(&nl802154_fam),
252 nl802154_fam.maxattr, nl802154_policy, NULL);
256 *wpan_dev = __cfg802154_wpan_dev_from_attrs(sock_net(skb->sk),
257 genl_family_attrbuf(&nl802154_fam));
258 if (IS_ERR(*wpan_dev)) {
259 err = PTR_ERR(*wpan_dev);
262 *rdev = wpan_phy_to_rdev((*wpan_dev)->wpan_phy);
263 /* 0 is the first index - add 1 to parse only once */
264 cb->args[0] = (*rdev)->wpan_phy_idx + 1;
265 cb->args[1] = (*wpan_dev)->identifier;
267 /* subtract the 1 again here */
268 struct wpan_phy *wpan_phy = wpan_phy_idx_to_wpan_phy(cb->args[0] - 1);
269 struct wpan_dev *tmp;
275 *rdev = wpan_phy_to_rdev(wpan_phy);
278 list_for_each_entry(tmp, &(*rdev)->wpan_dev_list, list) {
279 if (tmp->identifier == cb->args[1]) {
298 nl802154_finish_wpan_dev_dump(struct cfg802154_registered_device *rdev)
302 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
304 /* message building helper */
305 static inline void *nl802154hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
308 /* since there is no private header just add the generic one */
309 return genlmsg_put(skb, portid, seq, &nl802154_fam, flags, cmd);
313 nl802154_put_flags(struct sk_buff *msg, int attr, u32 mask)
315 struct nlattr *nl_flags = nla_nest_start(msg, attr);
323 if ((mask & 1) && nla_put_flag(msg, i))
330 nla_nest_end(msg, nl_flags);
335 nl802154_send_wpan_phy_channels(struct cfg802154_registered_device *rdev,
338 struct nlattr *nl_page;
341 nl_page = nla_nest_start(msg, NL802154_ATTR_CHANNELS_SUPPORTED);
345 for (page = 0; page <= IEEE802154_MAX_PAGE; page++) {
346 if (nla_put_u32(msg, NL802154_ATTR_SUPPORTED_CHANNEL,
347 rdev->wpan_phy.supported.channels[page]))
350 nla_nest_end(msg, nl_page);
356 nl802154_put_capabilities(struct sk_buff *msg,
357 struct cfg802154_registered_device *rdev)
359 const struct wpan_phy_supported *caps = &rdev->wpan_phy.supported;
360 struct nlattr *nl_caps, *nl_channels;
363 nl_caps = nla_nest_start(msg, NL802154_ATTR_WPAN_PHY_CAPS);
367 nl_channels = nla_nest_start(msg, NL802154_CAP_ATTR_CHANNELS);
371 for (i = 0; i <= IEEE802154_MAX_PAGE; i++) {
372 if (caps->channels[i]) {
373 if (nl802154_put_flags(msg, i, caps->channels[i]))
378 nla_nest_end(msg, nl_channels);
380 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
381 struct nlattr *nl_ed_lvls;
383 nl_ed_lvls = nla_nest_start(msg,
384 NL802154_CAP_ATTR_CCA_ED_LEVELS);
388 for (i = 0; i < caps->cca_ed_levels_size; i++) {
389 if (nla_put_s32(msg, i, caps->cca_ed_levels[i]))
393 nla_nest_end(msg, nl_ed_lvls);
396 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
397 struct nlattr *nl_tx_pwrs;
399 nl_tx_pwrs = nla_nest_start(msg, NL802154_CAP_ATTR_TX_POWERS);
403 for (i = 0; i < caps->tx_powers_size; i++) {
404 if (nla_put_s32(msg, i, caps->tx_powers[i]))
408 nla_nest_end(msg, nl_tx_pwrs);
411 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
412 if (nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_MODES,
414 nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_OPTS,
419 if (nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MINBE, caps->min_minbe) ||
420 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MINBE, caps->max_minbe) ||
421 nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MAXBE, caps->min_maxbe) ||
422 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MAXBE, caps->max_maxbe) ||
423 nla_put_u8(msg, NL802154_CAP_ATTR_MIN_CSMA_BACKOFFS,
424 caps->min_csma_backoffs) ||
425 nla_put_u8(msg, NL802154_CAP_ATTR_MAX_CSMA_BACKOFFS,
426 caps->max_csma_backoffs) ||
427 nla_put_s8(msg, NL802154_CAP_ATTR_MIN_FRAME_RETRIES,
428 caps->min_frame_retries) ||
429 nla_put_s8(msg, NL802154_CAP_ATTR_MAX_FRAME_RETRIES,
430 caps->max_frame_retries) ||
431 nl802154_put_flags(msg, NL802154_CAP_ATTR_IFTYPES,
433 nla_put_u32(msg, NL802154_CAP_ATTR_LBT, caps->lbt))
436 nla_nest_end(msg, nl_caps);
441 static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev,
442 enum nl802154_commands cmd,
443 struct sk_buff *msg, u32 portid, u32 seq,
446 struct nlattr *nl_cmds;
450 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
454 if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
455 nla_put_string(msg, NL802154_ATTR_WPAN_PHY_NAME,
456 wpan_phy_name(&rdev->wpan_phy)) ||
457 nla_put_u32(msg, NL802154_ATTR_GENERATION,
458 cfg802154_rdev_list_generation))
459 goto nla_put_failure;
461 if (cmd != NL802154_CMD_NEW_WPAN_PHY)
466 /* current channel settings */
467 if (nla_put_u8(msg, NL802154_ATTR_PAGE,
468 rdev->wpan_phy.current_page) ||
469 nla_put_u8(msg, NL802154_ATTR_CHANNEL,
470 rdev->wpan_phy.current_channel))
471 goto nla_put_failure;
473 /* TODO remove this behaviour, we still keep support it for a while
474 * so users can change the behaviour to the new one.
476 if (nl802154_send_wpan_phy_channels(rdev, msg))
477 goto nla_put_failure;
480 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
481 if (nla_put_u32(msg, NL802154_ATTR_CCA_MODE,
482 rdev->wpan_phy.cca.mode))
483 goto nla_put_failure;
485 if (rdev->wpan_phy.cca.mode == NL802154_CCA_ENERGY_CARRIER) {
486 if (nla_put_u32(msg, NL802154_ATTR_CCA_OPT,
487 rdev->wpan_phy.cca.opt))
488 goto nla_put_failure;
492 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
493 if (nla_put_s32(msg, NL802154_ATTR_TX_POWER,
494 rdev->wpan_phy.transmit_power))
495 goto nla_put_failure;
498 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
499 if (nla_put_s32(msg, NL802154_ATTR_CCA_ED_LEVEL,
500 rdev->wpan_phy.cca_ed_level))
501 goto nla_put_failure;
504 if (nl802154_put_capabilities(msg, rdev))
505 goto nla_put_failure;
507 nl_cmds = nla_nest_start(msg, NL802154_ATTR_SUPPORTED_COMMANDS);
509 goto nla_put_failure;
514 if (rdev->ops->op) { \
516 if (nla_put_u32(msg, i, NL802154_CMD_ ## n)) \
517 goto nla_put_failure; \
521 CMD(add_virtual_intf, NEW_INTERFACE);
522 CMD(del_virtual_intf, DEL_INTERFACE);
523 CMD(set_channel, SET_CHANNEL);
524 CMD(set_pan_id, SET_PAN_ID);
525 CMD(set_short_addr, SET_SHORT_ADDR);
526 CMD(set_backoff_exponent, SET_BACKOFF_EXPONENT);
527 CMD(set_max_csma_backoffs, SET_MAX_CSMA_BACKOFFS);
528 CMD(set_max_frame_retries, SET_MAX_FRAME_RETRIES);
529 CMD(set_lbt_mode, SET_LBT_MODE);
530 CMD(set_ackreq_default, SET_ACKREQ_DEFAULT);
532 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER)
533 CMD(set_tx_power, SET_TX_POWER);
535 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL)
536 CMD(set_cca_ed_level, SET_CCA_ED_LEVEL);
538 if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE)
539 CMD(set_cca_mode, SET_CCA_MODE);
542 nla_nest_end(msg, nl_cmds);
545 genlmsg_end(msg, hdr);
549 genlmsg_cancel(msg, hdr);
553 struct nl802154_dump_wpan_phy_state {
559 static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb,
560 struct netlink_callback *cb,
561 struct nl802154_dump_wpan_phy_state *state)
563 struct nlattr **tb = genl_family_attrbuf(&nl802154_fam);
564 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize, tb,
565 nl802154_fam.maxattr, nl802154_policy, NULL);
567 /* TODO check if we can handle error here,
568 * we have no backward compatibility
573 if (tb[NL802154_ATTR_WPAN_PHY])
574 state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]);
575 if (tb[NL802154_ATTR_WPAN_DEV])
576 state->filter_wpan_phy = nla_get_u64(tb[NL802154_ATTR_WPAN_DEV]) >> 32;
577 if (tb[NL802154_ATTR_IFINDEX]) {
578 struct net_device *netdev;
579 struct cfg802154_registered_device *rdev;
580 int ifidx = nla_get_u32(tb[NL802154_ATTR_IFINDEX]);
582 netdev = __dev_get_by_index(&init_net, ifidx);
585 if (netdev->ieee802154_ptr) {
586 rdev = wpan_phy_to_rdev(
587 netdev->ieee802154_ptr->wpan_phy);
588 state->filter_wpan_phy = rdev->wpan_phy_idx;
596 nl802154_dump_wpan_phy(struct sk_buff *skb, struct netlink_callback *cb)
599 struct nl802154_dump_wpan_phy_state *state = (void *)cb->args[0];
600 struct cfg802154_registered_device *rdev;
604 state = kzalloc(sizeof(*state), GFP_KERNEL);
609 state->filter_wpan_phy = -1;
610 ret = nl802154_dump_wpan_phy_parse(skb, cb, state);
616 cb->args[0] = (long)state;
619 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
620 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
622 if (++idx <= state->start)
624 if (state->filter_wpan_phy != -1 &&
625 state->filter_wpan_phy != rdev->wpan_phy_idx)
627 /* attempt to fit multiple wpan_phy data chunks into the skb */
628 ret = nl802154_send_wpan_phy(rdev,
629 NL802154_CMD_NEW_WPAN_PHY,
631 NETLINK_CB(cb->skb).portid,
632 cb->nlh->nlmsg_seq, NLM_F_MULTI);
634 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
635 !skb->len && cb->min_dump_alloc < 4096) {
636 cb->min_dump_alloc = 4096;
652 static int nl802154_dump_wpan_phy_done(struct netlink_callback *cb)
654 kfree((void *)cb->args[0]);
658 static int nl802154_get_wpan_phy(struct sk_buff *skb, struct genl_info *info)
661 struct cfg802154_registered_device *rdev = info->user_ptr[0];
663 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
667 if (nl802154_send_wpan_phy(rdev, NL802154_CMD_NEW_WPAN_PHY, msg,
668 info->snd_portid, info->snd_seq, 0) < 0) {
673 return genlmsg_reply(msg, info);
676 static inline u64 wpan_dev_id(struct wpan_dev *wpan_dev)
678 return (u64)wpan_dev->identifier |
679 ((u64)wpan_phy_to_rdev(wpan_dev->wpan_phy)->wpan_phy_idx << 32);
682 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
683 #include <net/ieee802154_netdev.h>
686 ieee802154_llsec_send_key_id(struct sk_buff *msg,
687 const struct ieee802154_llsec_key_id *desc)
689 struct nlattr *nl_dev_addr;
691 if (nla_put_u32(msg, NL802154_KEY_ID_ATTR_MODE, desc->mode))
694 switch (desc->mode) {
695 case NL802154_KEY_ID_MODE_IMPLICIT:
696 nl_dev_addr = nla_nest_start(msg, NL802154_KEY_ID_ATTR_IMPLICIT);
700 if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_PAN_ID,
701 desc->device_addr.pan_id) ||
702 nla_put_u32(msg, NL802154_DEV_ADDR_ATTR_MODE,
703 desc->device_addr.mode))
706 switch (desc->device_addr.mode) {
707 case NL802154_DEV_ADDR_SHORT:
708 if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_SHORT,
709 desc->device_addr.short_addr))
712 case NL802154_DEV_ADDR_EXTENDED:
713 if (nla_put_le64(msg, NL802154_DEV_ADDR_ATTR_EXTENDED,
714 desc->device_addr.extended_addr,
715 NL802154_DEV_ADDR_ATTR_PAD))
719 /* userspace should handle unknown */
723 nla_nest_end(msg, nl_dev_addr);
725 case NL802154_KEY_ID_MODE_INDEX:
727 case NL802154_KEY_ID_MODE_INDEX_SHORT:
728 /* TODO renmae short_source? */
729 if (nla_put_le32(msg, NL802154_KEY_ID_ATTR_SOURCE_SHORT,
733 case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
734 if (nla_put_le64(msg, NL802154_KEY_ID_ATTR_SOURCE_EXTENDED,
735 desc->extended_source,
736 NL802154_KEY_ID_ATTR_PAD))
740 /* userspace should handle unknown */
744 /* TODO key_id to key_idx ? Check naming */
745 if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
746 if (nla_put_u8(msg, NL802154_KEY_ID_ATTR_INDEX, desc->id))
753 static int nl802154_get_llsec_params(struct sk_buff *msg,
754 struct cfg802154_registered_device *rdev,
755 struct wpan_dev *wpan_dev)
757 struct nlattr *nl_key_id;
758 struct ieee802154_llsec_params params;
761 ret = rdev_get_llsec_params(rdev, wpan_dev, ¶ms);
765 if (nla_put_u8(msg, NL802154_ATTR_SEC_ENABLED, params.enabled) ||
766 nla_put_u32(msg, NL802154_ATTR_SEC_OUT_LEVEL, params.out_level) ||
767 nla_put_be32(msg, NL802154_ATTR_SEC_FRAME_COUNTER,
768 params.frame_counter))
771 nl_key_id = nla_nest_start(msg, NL802154_ATTR_SEC_OUT_KEY_ID);
775 ret = ieee802154_llsec_send_key_id(msg, ¶ms.out_key);
779 nla_nest_end(msg, nl_key_id);
783 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
786 nl802154_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
787 struct cfg802154_registered_device *rdev,
788 struct wpan_dev *wpan_dev)
790 struct net_device *dev = wpan_dev->netdev;
793 hdr = nl802154hdr_put(msg, portid, seq, flags,
794 NL802154_CMD_NEW_INTERFACE);
799 (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex) ||
800 nla_put_string(msg, NL802154_ATTR_IFNAME, dev->name)))
801 goto nla_put_failure;
803 if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
804 nla_put_u32(msg, NL802154_ATTR_IFTYPE, wpan_dev->iftype) ||
805 nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
806 wpan_dev_id(wpan_dev), NL802154_ATTR_PAD) ||
807 nla_put_u32(msg, NL802154_ATTR_GENERATION,
808 rdev->devlist_generation ^
809 (cfg802154_rdev_list_generation << 2)))
810 goto nla_put_failure;
812 /* address settings */
813 if (nla_put_le64(msg, NL802154_ATTR_EXTENDED_ADDR,
814 wpan_dev->extended_addr,
815 NL802154_ATTR_PAD) ||
816 nla_put_le16(msg, NL802154_ATTR_SHORT_ADDR,
817 wpan_dev->short_addr) ||
818 nla_put_le16(msg, NL802154_ATTR_PAN_ID, wpan_dev->pan_id))
819 goto nla_put_failure;
822 if (nla_put_s8(msg, NL802154_ATTR_MAX_FRAME_RETRIES,
823 wpan_dev->frame_retries) ||
824 nla_put_u8(msg, NL802154_ATTR_MAX_BE, wpan_dev->max_be) ||
825 nla_put_u8(msg, NL802154_ATTR_MAX_CSMA_BACKOFFS,
826 wpan_dev->csma_retries) ||
827 nla_put_u8(msg, NL802154_ATTR_MIN_BE, wpan_dev->min_be))
828 goto nla_put_failure;
830 /* listen before transmit */
831 if (nla_put_u8(msg, NL802154_ATTR_LBT_MODE, wpan_dev->lbt))
832 goto nla_put_failure;
834 /* ackreq default behaviour */
835 if (nla_put_u8(msg, NL802154_ATTR_ACKREQ_DEFAULT, wpan_dev->ackreq))
836 goto nla_put_failure;
838 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
839 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
842 if (nl802154_get_llsec_params(msg, rdev, wpan_dev) < 0)
843 goto nla_put_failure;
846 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
848 genlmsg_end(msg, hdr);
852 genlmsg_cancel(msg, hdr);
857 nl802154_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
861 int wp_start = cb->args[0];
862 int if_start = cb->args[1];
863 struct cfg802154_registered_device *rdev;
864 struct wpan_dev *wpan_dev;
867 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
868 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
870 if (wp_idx < wp_start) {
876 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
877 if (if_idx < if_start) {
881 if (nl802154_send_iface(skb, NETLINK_CB(cb->skb).portid,
882 cb->nlh->nlmsg_seq, NLM_F_MULTI,
883 rdev, wpan_dev) < 0) {
894 cb->args[0] = wp_idx;
895 cb->args[1] = if_idx;
900 static int nl802154_get_interface(struct sk_buff *skb, struct genl_info *info)
903 struct cfg802154_registered_device *rdev = info->user_ptr[0];
904 struct wpan_dev *wdev = info->user_ptr[1];
906 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
910 if (nl802154_send_iface(msg, info->snd_portid, info->snd_seq, 0,
916 return genlmsg_reply(msg, info);
919 static int nl802154_new_interface(struct sk_buff *skb, struct genl_info *info)
921 struct cfg802154_registered_device *rdev = info->user_ptr[0];
922 enum nl802154_iftype type = NL802154_IFTYPE_UNSPEC;
923 __le64 extended_addr = cpu_to_le64(0x0000000000000000ULL);
925 /* TODO avoid failing a new interface
926 * creation due to pending removal?
929 if (!info->attrs[NL802154_ATTR_IFNAME])
932 if (info->attrs[NL802154_ATTR_IFTYPE]) {
933 type = nla_get_u32(info->attrs[NL802154_ATTR_IFTYPE]);
934 if (type > NL802154_IFTYPE_MAX ||
935 !(rdev->wpan_phy.supported.iftypes & BIT(type)))
939 if (info->attrs[NL802154_ATTR_EXTENDED_ADDR])
940 extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
942 if (!rdev->ops->add_virtual_intf)
945 return rdev_add_virtual_intf(rdev,
946 nla_data(info->attrs[NL802154_ATTR_IFNAME]),
947 NET_NAME_USER, type, extended_addr);
950 static int nl802154_del_interface(struct sk_buff *skb, struct genl_info *info)
952 struct cfg802154_registered_device *rdev = info->user_ptr[0];
953 struct wpan_dev *wpan_dev = info->user_ptr[1];
955 if (!rdev->ops->del_virtual_intf)
958 /* If we remove a wpan device without a netdev then clear
959 * user_ptr[1] so that nl802154_post_doit won't dereference it
960 * to check if it needs to do dev_put(). Otherwise it crashes
961 * since the wpan_dev has been freed, unlike with a netdev where
962 * we need the dev_put() for the netdev to really be freed.
964 if (!wpan_dev->netdev)
965 info->user_ptr[1] = NULL;
967 return rdev_del_virtual_intf(rdev, wpan_dev);
970 static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info)
972 struct cfg802154_registered_device *rdev = info->user_ptr[0];
975 if (!info->attrs[NL802154_ATTR_PAGE] ||
976 !info->attrs[NL802154_ATTR_CHANNEL])
979 page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
980 channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]);
982 /* check 802.15.4 constraints */
983 if (page > IEEE802154_MAX_PAGE || channel > IEEE802154_MAX_CHANNEL ||
984 !(rdev->wpan_phy.supported.channels[page] & BIT(channel)))
987 return rdev_set_channel(rdev, page, channel);
990 static int nl802154_set_cca_mode(struct sk_buff *skb, struct genl_info *info)
992 struct cfg802154_registered_device *rdev = info->user_ptr[0];
993 struct wpan_phy_cca cca;
995 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE))
998 if (!info->attrs[NL802154_ATTR_CCA_MODE])
1001 cca.mode = nla_get_u32(info->attrs[NL802154_ATTR_CCA_MODE]);
1002 /* checking 802.15.4 constraints */
1003 if (cca.mode < NL802154_CCA_ENERGY ||
1004 cca.mode > NL802154_CCA_ATTR_MAX ||
1005 !(rdev->wpan_phy.supported.cca_modes & BIT(cca.mode)))
1008 if (cca.mode == NL802154_CCA_ENERGY_CARRIER) {
1009 if (!info->attrs[NL802154_ATTR_CCA_OPT])
1012 cca.opt = nla_get_u32(info->attrs[NL802154_ATTR_CCA_OPT]);
1013 if (cca.opt > NL802154_CCA_OPT_ATTR_MAX ||
1014 !(rdev->wpan_phy.supported.cca_opts & BIT(cca.opt)))
1018 return rdev_set_cca_mode(rdev, &cca);
1021 static int nl802154_set_cca_ed_level(struct sk_buff *skb, struct genl_info *info)
1023 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1027 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL))
1030 if (!info->attrs[NL802154_ATTR_CCA_ED_LEVEL])
1033 ed_level = nla_get_s32(info->attrs[NL802154_ATTR_CCA_ED_LEVEL]);
1035 for (i = 0; i < rdev->wpan_phy.supported.cca_ed_levels_size; i++) {
1036 if (ed_level == rdev->wpan_phy.supported.cca_ed_levels[i])
1037 return rdev_set_cca_ed_level(rdev, ed_level);
1043 static int nl802154_set_tx_power(struct sk_buff *skb, struct genl_info *info)
1045 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1049 if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER))
1052 if (!info->attrs[NL802154_ATTR_TX_POWER])
1055 power = nla_get_s32(info->attrs[NL802154_ATTR_TX_POWER]);
1057 for (i = 0; i < rdev->wpan_phy.supported.tx_powers_size; i++) {
1058 if (power == rdev->wpan_phy.supported.tx_powers[i])
1059 return rdev_set_tx_power(rdev, power);
1065 static int nl802154_set_pan_id(struct sk_buff *skb, struct genl_info *info)
1067 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1068 struct net_device *dev = info->user_ptr[1];
1069 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1072 /* conflict here while tx/rx calls */
1073 if (netif_running(dev))
1076 if (wpan_dev->lowpan_dev) {
1077 if (netif_running(wpan_dev->lowpan_dev))
1081 /* don't change address fields on monitor */
1082 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1083 !info->attrs[NL802154_ATTR_PAN_ID])
1086 pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
1089 * I am not sure about to check here on broadcast pan_id.
1090 * Broadcast is a valid setting, comment from 802.15.4:
1091 * If this value is 0xffff, the device is not associated.
1093 * This could useful to simple deassociate an device.
1095 if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
1098 return rdev_set_pan_id(rdev, wpan_dev, pan_id);
1101 static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info)
1103 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1104 struct net_device *dev = info->user_ptr[1];
1105 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1108 /* conflict here while tx/rx calls */
1109 if (netif_running(dev))
1112 if (wpan_dev->lowpan_dev) {
1113 if (netif_running(wpan_dev->lowpan_dev))
1117 /* don't change address fields on monitor */
1118 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1119 !info->attrs[NL802154_ATTR_SHORT_ADDR])
1122 short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
1125 * I am not sure about to check here on broadcast short_addr.
1126 * Broadcast is a valid setting, comment from 802.15.4:
1127 * A value of 0xfffe indicates that the device has
1128 * associated but has not been allocated an address. A
1129 * value of 0xffff indicates that the device does not
1130 * have a short address.
1132 * I think we should allow to set these settings but
1133 * don't allow to allow socket communication with it.
1135 if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
1136 short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
1139 return rdev_set_short_addr(rdev, wpan_dev, short_addr);
1143 nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info)
1145 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1146 struct net_device *dev = info->user_ptr[1];
1147 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1150 /* should be set on netif open inside phy settings */
1151 if (netif_running(dev))
1154 if (!info->attrs[NL802154_ATTR_MIN_BE] ||
1155 !info->attrs[NL802154_ATTR_MAX_BE])
1158 min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]);
1159 max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]);
1161 /* check 802.15.4 constraints */
1162 if (min_be < rdev->wpan_phy.supported.min_minbe ||
1163 min_be > rdev->wpan_phy.supported.max_minbe ||
1164 max_be < rdev->wpan_phy.supported.min_maxbe ||
1165 max_be > rdev->wpan_phy.supported.max_maxbe ||
1169 return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
1173 nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info)
1175 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1176 struct net_device *dev = info->user_ptr[1];
1177 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1178 u8 max_csma_backoffs;
1180 /* conflict here while other running iface settings */
1181 if (netif_running(dev))
1184 if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS])
1187 max_csma_backoffs = nla_get_u8(
1188 info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]);
1190 /* check 802.15.4 constraints */
1191 if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs ||
1192 max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs)
1195 return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
1199 nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info)
1201 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1202 struct net_device *dev = info->user_ptr[1];
1203 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1204 s8 max_frame_retries;
1206 if (netif_running(dev))
1209 if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES])
1212 max_frame_retries = nla_get_s8(
1213 info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]);
1215 /* check 802.15.4 constraints */
1216 if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries ||
1217 max_frame_retries > rdev->wpan_phy.supported.max_frame_retries)
1220 return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
1223 static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info)
1225 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1226 struct net_device *dev = info->user_ptr[1];
1227 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1230 if (netif_running(dev))
1233 if (!info->attrs[NL802154_ATTR_LBT_MODE])
1236 mode = nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
1238 if (mode != 0 && mode != 1)
1241 if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt))
1244 return rdev_set_lbt_mode(rdev, wpan_dev, mode);
1248 nl802154_set_ackreq_default(struct sk_buff *skb, struct genl_info *info)
1250 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1251 struct net_device *dev = info->user_ptr[1];
1252 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1255 if (netif_running(dev))
1258 if (!info->attrs[NL802154_ATTR_ACKREQ_DEFAULT])
1261 ackreq = nla_get_u8(info->attrs[NL802154_ATTR_ACKREQ_DEFAULT]);
1263 if (ackreq != 0 && ackreq != 1)
1266 return rdev_set_ackreq_default(rdev, wpan_dev, ackreq);
1269 static int nl802154_wpan_phy_netns(struct sk_buff *skb, struct genl_info *info)
1271 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1275 if (info->attrs[NL802154_ATTR_PID]) {
1276 u32 pid = nla_get_u32(info->attrs[NL802154_ATTR_PID]);
1278 net = get_net_ns_by_pid(pid);
1279 } else if (info->attrs[NL802154_ATTR_NETNS_FD]) {
1280 u32 fd = nla_get_u32(info->attrs[NL802154_ATTR_NETNS_FD]);
1282 net = get_net_ns_by_fd(fd);
1288 return PTR_ERR(net);
1292 /* check if anything to do */
1293 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), net))
1294 err = cfg802154_switch_netns(rdev, net);
1300 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
1301 static const struct nla_policy nl802154_dev_addr_policy[NL802154_DEV_ADDR_ATTR_MAX + 1] = {
1302 [NL802154_DEV_ADDR_ATTR_PAN_ID] = { .type = NLA_U16 },
1303 [NL802154_DEV_ADDR_ATTR_MODE] = { .type = NLA_U32 },
1304 [NL802154_DEV_ADDR_ATTR_SHORT] = { .type = NLA_U16 },
1305 [NL802154_DEV_ADDR_ATTR_EXTENDED] = { .type = NLA_U64 },
1309 ieee802154_llsec_parse_dev_addr(struct nlattr *nla,
1310 struct ieee802154_addr *addr)
1312 struct nlattr *attrs[NL802154_DEV_ADDR_ATTR_MAX + 1];
1314 if (!nla || nla_parse_nested(attrs, NL802154_DEV_ADDR_ATTR_MAX, nla,
1315 nl802154_dev_addr_policy, NULL))
1318 if (!attrs[NL802154_DEV_ADDR_ATTR_PAN_ID] || !attrs[NL802154_DEV_ADDR_ATTR_MODE])
1321 addr->pan_id = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_PAN_ID]);
1322 addr->mode = nla_get_u32(attrs[NL802154_DEV_ADDR_ATTR_MODE]);
1323 switch (addr->mode) {
1324 case NL802154_DEV_ADDR_SHORT:
1325 if (!attrs[NL802154_DEV_ADDR_ATTR_SHORT])
1327 addr->short_addr = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_SHORT]);
1329 case NL802154_DEV_ADDR_EXTENDED:
1330 if (!attrs[NL802154_DEV_ADDR_ATTR_EXTENDED])
1332 addr->extended_addr = nla_get_le64(attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]);
1341 static const struct nla_policy nl802154_key_id_policy[NL802154_KEY_ID_ATTR_MAX + 1] = {
1342 [NL802154_KEY_ID_ATTR_MODE] = { .type = NLA_U32 },
1343 [NL802154_KEY_ID_ATTR_INDEX] = { .type = NLA_U8 },
1344 [NL802154_KEY_ID_ATTR_IMPLICIT] = { .type = NLA_NESTED },
1345 [NL802154_KEY_ID_ATTR_SOURCE_SHORT] = { .type = NLA_U32 },
1346 [NL802154_KEY_ID_ATTR_SOURCE_EXTENDED] = { .type = NLA_U64 },
1350 ieee802154_llsec_parse_key_id(struct nlattr *nla,
1351 struct ieee802154_llsec_key_id *desc)
1353 struct nlattr *attrs[NL802154_KEY_ID_ATTR_MAX + 1];
1355 if (!nla || nla_parse_nested(attrs, NL802154_KEY_ID_ATTR_MAX, nla,
1356 nl802154_key_id_policy, NULL))
1359 if (!attrs[NL802154_KEY_ID_ATTR_MODE])
1362 desc->mode = nla_get_u32(attrs[NL802154_KEY_ID_ATTR_MODE]);
1363 switch (desc->mode) {
1364 case NL802154_KEY_ID_MODE_IMPLICIT:
1365 if (!attrs[NL802154_KEY_ID_ATTR_IMPLICIT])
1368 if (ieee802154_llsec_parse_dev_addr(attrs[NL802154_KEY_ID_ATTR_IMPLICIT],
1369 &desc->device_addr) < 0)
1372 case NL802154_KEY_ID_MODE_INDEX:
1374 case NL802154_KEY_ID_MODE_INDEX_SHORT:
1375 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT])
1378 desc->short_source = nla_get_le32(attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT]);
1380 case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
1381 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED])
1384 desc->extended_source = nla_get_le64(attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED]);
1390 if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
1391 if (!attrs[NL802154_KEY_ID_ATTR_INDEX])
1394 /* TODO change id to idx */
1395 desc->id = nla_get_u8(attrs[NL802154_KEY_ID_ATTR_INDEX]);
1401 static int nl802154_set_llsec_params(struct sk_buff *skb,
1402 struct genl_info *info)
1404 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1405 struct net_device *dev = info->user_ptr[1];
1406 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1407 struct ieee802154_llsec_params params;
1411 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1414 if (info->attrs[NL802154_ATTR_SEC_ENABLED]) {
1417 enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1418 if (enabled != 0 && enabled != 1)
1421 params.enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1422 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
1425 if (info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID]) {
1426 ret = ieee802154_llsec_parse_key_id(info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID],
1431 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
1434 if (info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]) {
1435 params.out_level = nla_get_u32(info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]);
1436 if (params.out_level > NL802154_SECLEVEL_MAX)
1439 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
1442 if (info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]) {
1443 params.frame_counter = nla_get_be32(info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]);
1444 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
1447 return rdev_set_llsec_params(rdev, wpan_dev, ¶ms, changed);
1450 static int nl802154_send_key(struct sk_buff *msg, u32 cmd, u32 portid,
1452 struct cfg802154_registered_device *rdev,
1453 struct net_device *dev,
1454 const struct ieee802154_llsec_key_entry *key)
1457 u32 commands[NL802154_CMD_FRAME_NR_IDS / 32];
1458 struct nlattr *nl_key, *nl_key_id;
1460 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1464 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1465 goto nla_put_failure;
1467 nl_key = nla_nest_start(msg, NL802154_ATTR_SEC_KEY);
1469 goto nla_put_failure;
1471 nl_key_id = nla_nest_start(msg, NL802154_KEY_ATTR_ID);
1473 goto nla_put_failure;
1475 if (ieee802154_llsec_send_key_id(msg, &key->id) < 0)
1476 goto nla_put_failure;
1478 nla_nest_end(msg, nl_key_id);
1480 if (nla_put_u8(msg, NL802154_KEY_ATTR_USAGE_FRAMES,
1481 key->key->frame_types))
1482 goto nla_put_failure;
1484 if (key->key->frame_types & BIT(NL802154_FRAME_CMD)) {
1485 /* TODO for each nested */
1486 memset(commands, 0, sizeof(commands));
1487 commands[7] = key->key->cmd_frame_ids;
1488 if (nla_put(msg, NL802154_KEY_ATTR_USAGE_CMDS,
1489 sizeof(commands), commands))
1490 goto nla_put_failure;
1493 if (nla_put(msg, NL802154_KEY_ATTR_BYTES, NL802154_KEY_SIZE,
1495 goto nla_put_failure;
1497 nla_nest_end(msg, nl_key);
1498 genlmsg_end(msg, hdr);
1503 genlmsg_cancel(msg, hdr);
1508 nl802154_dump_llsec_key(struct sk_buff *skb, struct netlink_callback *cb)
1510 struct cfg802154_registered_device *rdev = NULL;
1511 struct ieee802154_llsec_key_entry *key;
1512 struct ieee802154_llsec_table *table;
1513 struct wpan_dev *wpan_dev;
1516 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1520 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1525 if (!wpan_dev->netdev) {
1530 rdev_lock_llsec_table(rdev, wpan_dev);
1531 rdev_get_llsec_table(rdev, wpan_dev, &table);
1533 /* TODO make it like station dump */
1537 list_for_each_entry(key, &table->keys, list) {
1538 if (nl802154_send_key(skb, NL802154_CMD_NEW_SEC_KEY,
1539 NETLINK_CB(cb->skb).portid,
1540 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1541 rdev, wpan_dev->netdev, key) < 0) {
1544 rdev_unlock_llsec_table(rdev, wpan_dev);
1552 rdev_unlock_llsec_table(rdev, wpan_dev);
1555 nl802154_finish_wpan_dev_dump(rdev);
1560 static const struct nla_policy nl802154_key_policy[NL802154_KEY_ATTR_MAX + 1] = {
1561 [NL802154_KEY_ATTR_ID] = { NLA_NESTED },
1562 /* TODO handle it as for_each_nested and NLA_FLAG? */
1563 [NL802154_KEY_ATTR_USAGE_FRAMES] = { NLA_U8 },
1564 /* TODO handle it as for_each_nested, not static array? */
1565 [NL802154_KEY_ATTR_USAGE_CMDS] = { .len = NL802154_CMD_FRAME_NR_IDS / 8 },
1566 [NL802154_KEY_ATTR_BYTES] = { .len = NL802154_KEY_SIZE },
1569 static int nl802154_add_llsec_key(struct sk_buff *skb, struct genl_info *info)
1571 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1572 struct net_device *dev = info->user_ptr[1];
1573 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1574 struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1575 struct ieee802154_llsec_key key = { };
1576 struct ieee802154_llsec_key_id id = { };
1577 u32 commands[NL802154_CMD_FRAME_NR_IDS / 32] = { };
1579 if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
1580 nla_parse_nested(attrs, NL802154_KEY_ATTR_MAX,
1581 info->attrs[NL802154_ATTR_SEC_KEY],
1582 nl802154_key_policy, info->extack))
1585 if (!attrs[NL802154_KEY_ATTR_USAGE_FRAMES] ||
1586 !attrs[NL802154_KEY_ATTR_BYTES])
1589 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1592 key.frame_types = nla_get_u8(attrs[NL802154_KEY_ATTR_USAGE_FRAMES]);
1593 if (key.frame_types > BIT(NL802154_FRAME_MAX) ||
1594 ((key.frame_types & BIT(NL802154_FRAME_CMD)) &&
1595 !attrs[NL802154_KEY_ATTR_USAGE_CMDS]))
1598 if (attrs[NL802154_KEY_ATTR_USAGE_CMDS]) {
1599 /* TODO for each nested */
1600 nla_memcpy(commands, attrs[NL802154_KEY_ATTR_USAGE_CMDS],
1601 NL802154_CMD_FRAME_NR_IDS / 8);
1603 /* TODO understand the -EINVAL logic here? last condition */
1604 if (commands[0] || commands[1] || commands[2] || commands[3] ||
1605 commands[4] || commands[5] || commands[6] ||
1606 commands[7] > BIT(NL802154_CMD_FRAME_MAX))
1609 key.cmd_frame_ids = commands[7];
1611 key.cmd_frame_ids = 0;
1614 nla_memcpy(key.key, attrs[NL802154_KEY_ATTR_BYTES], NL802154_KEY_SIZE);
1616 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1619 return rdev_add_llsec_key(rdev, wpan_dev, &id, &key);
1622 static int nl802154_del_llsec_key(struct sk_buff *skb, struct genl_info *info)
1624 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1625 struct net_device *dev = info->user_ptr[1];
1626 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1627 struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1628 struct ieee802154_llsec_key_id id;
1630 if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
1631 nla_parse_nested(attrs, NL802154_KEY_ATTR_MAX,
1632 info->attrs[NL802154_ATTR_SEC_KEY],
1633 nl802154_key_policy, info->extack))
1636 if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1639 return rdev_del_llsec_key(rdev, wpan_dev, &id);
1642 static int nl802154_send_device(struct sk_buff *msg, u32 cmd, u32 portid,
1644 struct cfg802154_registered_device *rdev,
1645 struct net_device *dev,
1646 const struct ieee802154_llsec_device *dev_desc)
1649 struct nlattr *nl_device;
1651 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1655 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1656 goto nla_put_failure;
1658 nl_device = nla_nest_start(msg, NL802154_ATTR_SEC_DEVICE);
1660 goto nla_put_failure;
1662 if (nla_put_u32(msg, NL802154_DEV_ATTR_FRAME_COUNTER,
1663 dev_desc->frame_counter) ||
1664 nla_put_le16(msg, NL802154_DEV_ATTR_PAN_ID, dev_desc->pan_id) ||
1665 nla_put_le16(msg, NL802154_DEV_ATTR_SHORT_ADDR,
1666 dev_desc->short_addr) ||
1667 nla_put_le64(msg, NL802154_DEV_ATTR_EXTENDED_ADDR,
1668 dev_desc->hwaddr, NL802154_DEV_ATTR_PAD) ||
1669 nla_put_u8(msg, NL802154_DEV_ATTR_SECLEVEL_EXEMPT,
1670 dev_desc->seclevel_exempt) ||
1671 nla_put_u32(msg, NL802154_DEV_ATTR_KEY_MODE, dev_desc->key_mode))
1672 goto nla_put_failure;
1674 nla_nest_end(msg, nl_device);
1675 genlmsg_end(msg, hdr);
1680 genlmsg_cancel(msg, hdr);
1685 nl802154_dump_llsec_dev(struct sk_buff *skb, struct netlink_callback *cb)
1687 struct cfg802154_registered_device *rdev = NULL;
1688 struct ieee802154_llsec_device *dev;
1689 struct ieee802154_llsec_table *table;
1690 struct wpan_dev *wpan_dev;
1693 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1697 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1702 if (!wpan_dev->netdev) {
1707 rdev_lock_llsec_table(rdev, wpan_dev);
1708 rdev_get_llsec_table(rdev, wpan_dev, &table);
1710 /* TODO make it like station dump */
1714 list_for_each_entry(dev, &table->devices, list) {
1715 if (nl802154_send_device(skb, NL802154_CMD_NEW_SEC_LEVEL,
1716 NETLINK_CB(cb->skb).portid,
1717 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1718 rdev, wpan_dev->netdev, dev) < 0) {
1721 rdev_unlock_llsec_table(rdev, wpan_dev);
1729 rdev_unlock_llsec_table(rdev, wpan_dev);
1732 nl802154_finish_wpan_dev_dump(rdev);
1737 static const struct nla_policy nl802154_dev_policy[NL802154_DEV_ATTR_MAX + 1] = {
1738 [NL802154_DEV_ATTR_FRAME_COUNTER] = { NLA_U32 },
1739 [NL802154_DEV_ATTR_PAN_ID] = { .type = NLA_U16 },
1740 [NL802154_DEV_ATTR_SHORT_ADDR] = { .type = NLA_U16 },
1741 [NL802154_DEV_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
1742 [NL802154_DEV_ATTR_SECLEVEL_EXEMPT] = { NLA_U8 },
1743 [NL802154_DEV_ATTR_KEY_MODE] = { NLA_U32 },
1747 ieee802154_llsec_parse_device(struct nlattr *nla,
1748 struct ieee802154_llsec_device *dev)
1750 struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
1752 if (!nla || nla_parse_nested(attrs, NL802154_DEV_ATTR_MAX,
1753 nla, nl802154_dev_policy, NULL))
1756 memset(dev, 0, sizeof(*dev));
1758 if (!attrs[NL802154_DEV_ATTR_FRAME_COUNTER] ||
1759 !attrs[NL802154_DEV_ATTR_PAN_ID] ||
1760 !attrs[NL802154_DEV_ATTR_SHORT_ADDR] ||
1761 !attrs[NL802154_DEV_ATTR_EXTENDED_ADDR] ||
1762 !attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] ||
1763 !attrs[NL802154_DEV_ATTR_KEY_MODE])
1767 dev->frame_counter = nla_get_u32(attrs[NL802154_DEV_ATTR_FRAME_COUNTER]);
1768 dev->pan_id = nla_get_le16(attrs[NL802154_DEV_ATTR_PAN_ID]);
1769 dev->short_addr = nla_get_le16(attrs[NL802154_DEV_ATTR_SHORT_ADDR]);
1770 /* TODO rename hwaddr to extended_addr */
1771 dev->hwaddr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
1772 dev->seclevel_exempt = nla_get_u8(attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT]);
1773 dev->key_mode = nla_get_u32(attrs[NL802154_DEV_ATTR_KEY_MODE]);
1775 if (dev->key_mode > NL802154_DEVKEY_MAX ||
1776 (dev->seclevel_exempt != 0 && dev->seclevel_exempt != 1))
1782 static int nl802154_add_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1784 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1785 struct net_device *dev = info->user_ptr[1];
1786 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1787 struct ieee802154_llsec_device dev_desc;
1789 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1792 if (ieee802154_llsec_parse_device(info->attrs[NL802154_ATTR_SEC_DEVICE],
1796 return rdev_add_device(rdev, wpan_dev, &dev_desc);
1799 static int nl802154_del_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1801 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1802 struct net_device *dev = info->user_ptr[1];
1803 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1804 struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
1805 __le64 extended_addr;
1807 if (!info->attrs[NL802154_ATTR_SEC_DEVICE] ||
1808 nla_parse_nested(attrs, NL802154_DEV_ATTR_MAX,
1809 info->attrs[NL802154_ATTR_SEC_DEVICE],
1810 nl802154_dev_policy, info->extack))
1813 if (!attrs[NL802154_DEV_ATTR_EXTENDED_ADDR])
1816 extended_addr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
1817 return rdev_del_device(rdev, wpan_dev, extended_addr);
1820 static int nl802154_send_devkey(struct sk_buff *msg, u32 cmd, u32 portid,
1822 struct cfg802154_registered_device *rdev,
1823 struct net_device *dev, __le64 extended_addr,
1824 const struct ieee802154_llsec_device_key *devkey)
1827 struct nlattr *nl_devkey, *nl_key_id;
1829 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1833 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1834 goto nla_put_failure;
1836 nl_devkey = nla_nest_start(msg, NL802154_ATTR_SEC_DEVKEY);
1838 goto nla_put_failure;
1840 if (nla_put_le64(msg, NL802154_DEVKEY_ATTR_EXTENDED_ADDR,
1841 extended_addr, NL802154_DEVKEY_ATTR_PAD) ||
1842 nla_put_u32(msg, NL802154_DEVKEY_ATTR_FRAME_COUNTER,
1843 devkey->frame_counter))
1844 goto nla_put_failure;
1846 nl_key_id = nla_nest_start(msg, NL802154_DEVKEY_ATTR_ID);
1848 goto nla_put_failure;
1850 if (ieee802154_llsec_send_key_id(msg, &devkey->key_id) < 0)
1851 goto nla_put_failure;
1853 nla_nest_end(msg, nl_key_id);
1854 nla_nest_end(msg, nl_devkey);
1855 genlmsg_end(msg, hdr);
1860 genlmsg_cancel(msg, hdr);
1865 nl802154_dump_llsec_devkey(struct sk_buff *skb, struct netlink_callback *cb)
1867 struct cfg802154_registered_device *rdev = NULL;
1868 struct ieee802154_llsec_device_key *kpos;
1869 struct ieee802154_llsec_device *dpos;
1870 struct ieee802154_llsec_table *table;
1871 struct wpan_dev *wpan_dev;
1874 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1878 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1883 if (!wpan_dev->netdev) {
1888 rdev_lock_llsec_table(rdev, wpan_dev);
1889 rdev_get_llsec_table(rdev, wpan_dev, &table);
1891 /* TODO make it like station dump */
1895 /* TODO look if remove devkey and do some nested attribute */
1896 list_for_each_entry(dpos, &table->devices, list) {
1897 list_for_each_entry(kpos, &dpos->keys, list) {
1898 if (nl802154_send_devkey(skb,
1899 NL802154_CMD_NEW_SEC_LEVEL,
1900 NETLINK_CB(cb->skb).portid,
1908 rdev_unlock_llsec_table(rdev, wpan_dev);
1917 rdev_unlock_llsec_table(rdev, wpan_dev);
1920 nl802154_finish_wpan_dev_dump(rdev);
1925 static const struct nla_policy nl802154_devkey_policy[NL802154_DEVKEY_ATTR_MAX + 1] = {
1926 [NL802154_DEVKEY_ATTR_FRAME_COUNTER] = { NLA_U32 },
1927 [NL802154_DEVKEY_ATTR_EXTENDED_ADDR] = { NLA_U64 },
1928 [NL802154_DEVKEY_ATTR_ID] = { NLA_NESTED },
1931 static int nl802154_add_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
1933 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1934 struct net_device *dev = info->user_ptr[1];
1935 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1936 struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
1937 struct ieee802154_llsec_device_key key;
1938 __le64 extended_addr;
1940 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1943 if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
1944 nla_parse_nested(attrs, NL802154_DEVKEY_ATTR_MAX,
1945 info->attrs[NL802154_ATTR_SEC_DEVKEY],
1946 nl802154_devkey_policy, info->extack) < 0)
1949 if (!attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER] ||
1950 !attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1953 /* TODO change key.id ? */
1954 if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1959 key.frame_counter = nla_get_u32(attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER]);
1960 /* TODO change naming hwaddr -> extended_addr
1961 * check unique identifier short+pan OR extended_addr
1963 extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1964 return rdev_add_devkey(rdev, wpan_dev, extended_addr, &key);
1967 static int nl802154_del_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
1969 struct cfg802154_registered_device *rdev = info->user_ptr[0];
1970 struct net_device *dev = info->user_ptr[1];
1971 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1972 struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
1973 struct ieee802154_llsec_device_key key;
1974 __le64 extended_addr;
1976 if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
1977 nla_parse_nested(attrs, NL802154_DEVKEY_ATTR_MAX,
1978 info->attrs[NL802154_ATTR_SEC_DEVKEY],
1979 nl802154_devkey_policy, info->extack))
1982 if (!attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1985 /* TODO change key.id ? */
1986 if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1990 /* TODO change naming hwaddr -> extended_addr
1991 * check unique identifier short+pan OR extended_addr
1993 extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1994 return rdev_del_devkey(rdev, wpan_dev, extended_addr, &key);
1997 static int nl802154_send_seclevel(struct sk_buff *msg, u32 cmd, u32 portid,
1999 struct cfg802154_registered_device *rdev,
2000 struct net_device *dev,
2001 const struct ieee802154_llsec_seclevel *sl)
2004 struct nlattr *nl_seclevel;
2006 hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
2010 if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
2011 goto nla_put_failure;
2013 nl_seclevel = nla_nest_start(msg, NL802154_ATTR_SEC_LEVEL);
2015 goto nla_put_failure;
2017 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_FRAME, sl->frame_type) ||
2018 nla_put_u32(msg, NL802154_SECLEVEL_ATTR_LEVELS, sl->sec_levels) ||
2019 nla_put_u8(msg, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE,
2020 sl->device_override))
2021 goto nla_put_failure;
2023 if (sl->frame_type == NL802154_FRAME_CMD) {
2024 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_CMD_FRAME,
2026 goto nla_put_failure;
2029 nla_nest_end(msg, nl_seclevel);
2030 genlmsg_end(msg, hdr);
2035 genlmsg_cancel(msg, hdr);
2040 nl802154_dump_llsec_seclevel(struct sk_buff *skb, struct netlink_callback *cb)
2042 struct cfg802154_registered_device *rdev = NULL;
2043 struct ieee802154_llsec_seclevel *sl;
2044 struct ieee802154_llsec_table *table;
2045 struct wpan_dev *wpan_dev;
2048 err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2052 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2057 if (!wpan_dev->netdev) {
2062 rdev_lock_llsec_table(rdev, wpan_dev);
2063 rdev_get_llsec_table(rdev, wpan_dev, &table);
2065 /* TODO make it like station dump */
2069 list_for_each_entry(sl, &table->security_levels, list) {
2070 if (nl802154_send_seclevel(skb, NL802154_CMD_NEW_SEC_LEVEL,
2071 NETLINK_CB(cb->skb).portid,
2072 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2073 rdev, wpan_dev->netdev, sl) < 0) {
2076 rdev_unlock_llsec_table(rdev, wpan_dev);
2084 rdev_unlock_llsec_table(rdev, wpan_dev);
2087 nl802154_finish_wpan_dev_dump(rdev);
2092 static const struct nla_policy nl802154_seclevel_policy[NL802154_SECLEVEL_ATTR_MAX + 1] = {
2093 [NL802154_SECLEVEL_ATTR_LEVELS] = { .type = NLA_U8 },
2094 [NL802154_SECLEVEL_ATTR_FRAME] = { .type = NLA_U32 },
2095 [NL802154_SECLEVEL_ATTR_CMD_FRAME] = { .type = NLA_U32 },
2096 [NL802154_SECLEVEL_ATTR_DEV_OVERRIDE] = { .type = NLA_U8 },
2100 llsec_parse_seclevel(struct nlattr *nla, struct ieee802154_llsec_seclevel *sl)
2102 struct nlattr *attrs[NL802154_SECLEVEL_ATTR_MAX + 1];
2104 if (!nla || nla_parse_nested(attrs, NL802154_SECLEVEL_ATTR_MAX,
2105 nla, nl802154_seclevel_policy, NULL))
2108 memset(sl, 0, sizeof(*sl));
2110 if (!attrs[NL802154_SECLEVEL_ATTR_LEVELS] ||
2111 !attrs[NL802154_SECLEVEL_ATTR_FRAME] ||
2112 !attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE])
2115 sl->sec_levels = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_LEVELS]);
2116 sl->frame_type = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_FRAME]);
2117 sl->device_override = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE]);
2118 if (sl->frame_type > NL802154_FRAME_MAX ||
2119 (sl->device_override != 0 && sl->device_override != 1))
2122 if (sl->frame_type == NL802154_FRAME_CMD) {
2123 if (!attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME])
2126 sl->cmd_frame_id = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME]);
2127 if (sl->cmd_frame_id > NL802154_CMD_FRAME_MAX)
2134 static int nl802154_add_llsec_seclevel(struct sk_buff *skb,
2135 struct genl_info *info)
2137 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2138 struct net_device *dev = info->user_ptr[1];
2139 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2140 struct ieee802154_llsec_seclevel sl;
2142 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2145 if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2149 return rdev_add_seclevel(rdev, wpan_dev, &sl);
2152 static int nl802154_del_llsec_seclevel(struct sk_buff *skb,
2153 struct genl_info *info)
2155 struct cfg802154_registered_device *rdev = info->user_ptr[0];
2156 struct net_device *dev = info->user_ptr[1];
2157 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2158 struct ieee802154_llsec_seclevel sl;
2160 if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2163 if (!info->attrs[NL802154_ATTR_SEC_LEVEL] ||
2164 llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2168 return rdev_del_seclevel(rdev, wpan_dev, &sl);
2170 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2172 #define NL802154_FLAG_NEED_WPAN_PHY 0x01
2173 #define NL802154_FLAG_NEED_NETDEV 0x02
2174 #define NL802154_FLAG_NEED_RTNL 0x04
2175 #define NL802154_FLAG_CHECK_NETDEV_UP 0x08
2176 #define NL802154_FLAG_NEED_NETDEV_UP (NL802154_FLAG_NEED_NETDEV |\
2177 NL802154_FLAG_CHECK_NETDEV_UP)
2178 #define NL802154_FLAG_NEED_WPAN_DEV 0x10
2179 #define NL802154_FLAG_NEED_WPAN_DEV_UP (NL802154_FLAG_NEED_WPAN_DEV |\
2180 NL802154_FLAG_CHECK_NETDEV_UP)
2182 static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
2183 struct genl_info *info)
2185 struct cfg802154_registered_device *rdev;
2186 struct wpan_dev *wpan_dev;
2187 struct net_device *dev;
2188 bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
2193 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
2194 rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
2198 return PTR_ERR(rdev);
2200 info->user_ptr[0] = rdev;
2201 } else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
2202 ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2204 wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
2206 if (IS_ERR(wpan_dev)) {
2209 return PTR_ERR(wpan_dev);
2212 dev = wpan_dev->netdev;
2213 rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
2215 if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
2222 info->user_ptr[1] = dev;
2224 info->user_ptr[1] = wpan_dev;
2228 if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
2229 !netif_running(dev)) {
2238 info->user_ptr[0] = rdev;
2244 static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
2245 struct genl_info *info)
2247 if (info->user_ptr[1]) {
2248 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2249 struct wpan_dev *wpan_dev = info->user_ptr[1];
2251 if (wpan_dev->netdev)
2252 dev_put(wpan_dev->netdev);
2254 dev_put(info->user_ptr[1]);
2258 if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
2262 static const struct genl_ops nl802154_ops[] = {
2264 .cmd = NL802154_CMD_GET_WPAN_PHY,
2265 .doit = nl802154_get_wpan_phy,
2266 .dumpit = nl802154_dump_wpan_phy,
2267 .done = nl802154_dump_wpan_phy_done,
2268 .policy = nl802154_policy,
2269 /* can be retrieved by unprivileged users */
2270 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2271 NL802154_FLAG_NEED_RTNL,
2274 .cmd = NL802154_CMD_GET_INTERFACE,
2275 .doit = nl802154_get_interface,
2276 .dumpit = nl802154_dump_interface,
2277 .policy = nl802154_policy,
2278 /* can be retrieved by unprivileged users */
2279 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2280 NL802154_FLAG_NEED_RTNL,
2283 .cmd = NL802154_CMD_NEW_INTERFACE,
2284 .doit = nl802154_new_interface,
2285 .policy = nl802154_policy,
2286 .flags = GENL_ADMIN_PERM,
2287 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2288 NL802154_FLAG_NEED_RTNL,
2291 .cmd = NL802154_CMD_DEL_INTERFACE,
2292 .doit = nl802154_del_interface,
2293 .policy = nl802154_policy,
2294 .flags = GENL_ADMIN_PERM,
2295 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2296 NL802154_FLAG_NEED_RTNL,
2299 .cmd = NL802154_CMD_SET_CHANNEL,
2300 .doit = nl802154_set_channel,
2301 .policy = nl802154_policy,
2302 .flags = GENL_ADMIN_PERM,
2303 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2304 NL802154_FLAG_NEED_RTNL,
2307 .cmd = NL802154_CMD_SET_CCA_MODE,
2308 .doit = nl802154_set_cca_mode,
2309 .policy = nl802154_policy,
2310 .flags = GENL_ADMIN_PERM,
2311 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2312 NL802154_FLAG_NEED_RTNL,
2315 .cmd = NL802154_CMD_SET_CCA_ED_LEVEL,
2316 .doit = nl802154_set_cca_ed_level,
2317 .policy = nl802154_policy,
2318 .flags = GENL_ADMIN_PERM,
2319 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2320 NL802154_FLAG_NEED_RTNL,
2323 .cmd = NL802154_CMD_SET_TX_POWER,
2324 .doit = nl802154_set_tx_power,
2325 .policy = nl802154_policy,
2326 .flags = GENL_ADMIN_PERM,
2327 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2328 NL802154_FLAG_NEED_RTNL,
2331 .cmd = NL802154_CMD_SET_WPAN_PHY_NETNS,
2332 .doit = nl802154_wpan_phy_netns,
2333 .policy = nl802154_policy,
2334 .flags = GENL_ADMIN_PERM,
2335 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2336 NL802154_FLAG_NEED_RTNL,
2339 .cmd = NL802154_CMD_SET_PAN_ID,
2340 .doit = nl802154_set_pan_id,
2341 .policy = nl802154_policy,
2342 .flags = GENL_ADMIN_PERM,
2343 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2344 NL802154_FLAG_NEED_RTNL,
2347 .cmd = NL802154_CMD_SET_SHORT_ADDR,
2348 .doit = nl802154_set_short_addr,
2349 .policy = nl802154_policy,
2350 .flags = GENL_ADMIN_PERM,
2351 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2352 NL802154_FLAG_NEED_RTNL,
2355 .cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
2356 .doit = nl802154_set_backoff_exponent,
2357 .policy = nl802154_policy,
2358 .flags = GENL_ADMIN_PERM,
2359 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2360 NL802154_FLAG_NEED_RTNL,
2363 .cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
2364 .doit = nl802154_set_max_csma_backoffs,
2365 .policy = nl802154_policy,
2366 .flags = GENL_ADMIN_PERM,
2367 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2368 NL802154_FLAG_NEED_RTNL,
2371 .cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
2372 .doit = nl802154_set_max_frame_retries,
2373 .policy = nl802154_policy,
2374 .flags = GENL_ADMIN_PERM,
2375 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2376 NL802154_FLAG_NEED_RTNL,
2379 .cmd = NL802154_CMD_SET_LBT_MODE,
2380 .doit = nl802154_set_lbt_mode,
2381 .policy = nl802154_policy,
2382 .flags = GENL_ADMIN_PERM,
2383 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2384 NL802154_FLAG_NEED_RTNL,
2387 .cmd = NL802154_CMD_SET_ACKREQ_DEFAULT,
2388 .doit = nl802154_set_ackreq_default,
2389 .policy = nl802154_policy,
2390 .flags = GENL_ADMIN_PERM,
2391 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2392 NL802154_FLAG_NEED_RTNL,
2394 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
2396 .cmd = NL802154_CMD_SET_SEC_PARAMS,
2397 .doit = nl802154_set_llsec_params,
2398 .policy = nl802154_policy,
2399 .flags = GENL_ADMIN_PERM,
2400 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2401 NL802154_FLAG_NEED_RTNL,
2404 .cmd = NL802154_CMD_GET_SEC_KEY,
2405 /* TODO .doit by matching key id? */
2406 .dumpit = nl802154_dump_llsec_key,
2407 .policy = nl802154_policy,
2408 .flags = GENL_ADMIN_PERM,
2409 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2410 NL802154_FLAG_NEED_RTNL,
2413 .cmd = NL802154_CMD_NEW_SEC_KEY,
2414 .doit = nl802154_add_llsec_key,
2415 .policy = nl802154_policy,
2416 .flags = GENL_ADMIN_PERM,
2417 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2418 NL802154_FLAG_NEED_RTNL,
2421 .cmd = NL802154_CMD_DEL_SEC_KEY,
2422 .doit = nl802154_del_llsec_key,
2423 .policy = nl802154_policy,
2424 .flags = GENL_ADMIN_PERM,
2425 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2426 NL802154_FLAG_NEED_RTNL,
2428 /* TODO unique identifier must short+pan OR extended_addr */
2430 .cmd = NL802154_CMD_GET_SEC_DEV,
2431 /* TODO .doit by matching extended_addr? */
2432 .dumpit = nl802154_dump_llsec_dev,
2433 .policy = nl802154_policy,
2434 .flags = GENL_ADMIN_PERM,
2435 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2436 NL802154_FLAG_NEED_RTNL,
2439 .cmd = NL802154_CMD_NEW_SEC_DEV,
2440 .doit = nl802154_add_llsec_dev,
2441 .policy = nl802154_policy,
2442 .flags = GENL_ADMIN_PERM,
2443 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2444 NL802154_FLAG_NEED_RTNL,
2447 .cmd = NL802154_CMD_DEL_SEC_DEV,
2448 .doit = nl802154_del_llsec_dev,
2449 .policy = nl802154_policy,
2450 .flags = GENL_ADMIN_PERM,
2451 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2452 NL802154_FLAG_NEED_RTNL,
2454 /* TODO remove complete devkey, put it as nested? */
2456 .cmd = NL802154_CMD_GET_SEC_DEVKEY,
2457 /* TODO doit by matching ??? */
2458 .dumpit = nl802154_dump_llsec_devkey,
2459 .policy = nl802154_policy,
2460 .flags = GENL_ADMIN_PERM,
2461 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2462 NL802154_FLAG_NEED_RTNL,
2465 .cmd = NL802154_CMD_NEW_SEC_DEVKEY,
2466 .doit = nl802154_add_llsec_devkey,
2467 .policy = nl802154_policy,
2468 .flags = GENL_ADMIN_PERM,
2469 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2470 NL802154_FLAG_NEED_RTNL,
2473 .cmd = NL802154_CMD_DEL_SEC_DEVKEY,
2474 .doit = nl802154_del_llsec_devkey,
2475 .policy = nl802154_policy,
2476 .flags = GENL_ADMIN_PERM,
2477 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2478 NL802154_FLAG_NEED_RTNL,
2481 .cmd = NL802154_CMD_GET_SEC_LEVEL,
2482 /* TODO .doit by matching frame_type? */
2483 .dumpit = nl802154_dump_llsec_seclevel,
2484 .policy = nl802154_policy,
2485 .flags = GENL_ADMIN_PERM,
2486 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2487 NL802154_FLAG_NEED_RTNL,
2490 .cmd = NL802154_CMD_NEW_SEC_LEVEL,
2491 .doit = nl802154_add_llsec_seclevel,
2492 .policy = nl802154_policy,
2493 .flags = GENL_ADMIN_PERM,
2494 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2495 NL802154_FLAG_NEED_RTNL,
2498 .cmd = NL802154_CMD_DEL_SEC_LEVEL,
2499 /* TODO match frame_type only? */
2500 .doit = nl802154_del_llsec_seclevel,
2501 .policy = nl802154_policy,
2502 .flags = GENL_ADMIN_PERM,
2503 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2504 NL802154_FLAG_NEED_RTNL,
2506 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2509 static struct genl_family nl802154_fam __ro_after_init = {
2510 .name = NL802154_GENL_NAME, /* have users key off the name instead */
2511 .hdrsize = 0, /* no private header */
2512 .version = 1, /* no particular meaning now */
2513 .maxattr = NL802154_ATTR_MAX,
2515 .pre_doit = nl802154_pre_doit,
2516 .post_doit = nl802154_post_doit,
2517 .module = THIS_MODULE,
2518 .ops = nl802154_ops,
2519 .n_ops = ARRAY_SIZE(nl802154_ops),
2520 .mcgrps = nl802154_mcgrps,
2521 .n_mcgrps = ARRAY_SIZE(nl802154_mcgrps),
2524 /* initialisation/exit functions */
2525 int __init nl802154_init(void)
2527 return genl_register_family(&nl802154_fam);
2530 void nl802154_exit(void)
2532 genl_unregister_family(&nl802154_fam);