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 (nl802154_get_llsec_params(msg
, rdev
, wpan_dev
) < 0)
840 goto nla_put_failure
;
841 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
843 genlmsg_end(msg
, hdr
);
847 genlmsg_cancel(msg
, hdr
);
852 nl802154_dump_interface(struct sk_buff
*skb
, struct netlink_callback
*cb
)
856 int wp_start
= cb
->args
[0];
857 int if_start
= cb
->args
[1];
858 struct cfg802154_registered_device
*rdev
;
859 struct wpan_dev
*wpan_dev
;
862 list_for_each_entry(rdev
, &cfg802154_rdev_list
, list
) {
863 if (!net_eq(wpan_phy_net(&rdev
->wpan_phy
), sock_net(skb
->sk
)))
865 if (wp_idx
< wp_start
) {
871 list_for_each_entry(wpan_dev
, &rdev
->wpan_dev_list
, list
) {
872 if (if_idx
< if_start
) {
876 if (nl802154_send_iface(skb
, NETLINK_CB(cb
->skb
).portid
,
877 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
878 rdev
, wpan_dev
) < 0) {
889 cb
->args
[0] = wp_idx
;
890 cb
->args
[1] = if_idx
;
895 static int nl802154_get_interface(struct sk_buff
*skb
, struct genl_info
*info
)
898 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
899 struct wpan_dev
*wdev
= info
->user_ptr
[1];
901 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
905 if (nl802154_send_iface(msg
, info
->snd_portid
, info
->snd_seq
, 0,
911 return genlmsg_reply(msg
, info
);
914 static int nl802154_new_interface(struct sk_buff
*skb
, struct genl_info
*info
)
916 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
917 enum nl802154_iftype type
= NL802154_IFTYPE_UNSPEC
;
918 __le64 extended_addr
= cpu_to_le64(0x0000000000000000ULL
);
920 /* TODO avoid failing a new interface
921 * creation due to pending removal?
924 if (!info
->attrs
[NL802154_ATTR_IFNAME
])
927 if (info
->attrs
[NL802154_ATTR_IFTYPE
]) {
928 type
= nla_get_u32(info
->attrs
[NL802154_ATTR_IFTYPE
]);
929 if (type
> NL802154_IFTYPE_MAX
||
930 !(rdev
->wpan_phy
.supported
.iftypes
& BIT(type
)))
934 if (info
->attrs
[NL802154_ATTR_EXTENDED_ADDR
])
935 extended_addr
= nla_get_le64(info
->attrs
[NL802154_ATTR_EXTENDED_ADDR
]);
937 if (!rdev
->ops
->add_virtual_intf
)
940 return rdev_add_virtual_intf(rdev
,
941 nla_data(info
->attrs
[NL802154_ATTR_IFNAME
]),
942 NET_NAME_USER
, type
, extended_addr
);
945 static int nl802154_del_interface(struct sk_buff
*skb
, struct genl_info
*info
)
947 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
948 struct wpan_dev
*wpan_dev
= info
->user_ptr
[1];
950 if (!rdev
->ops
->del_virtual_intf
)
953 /* If we remove a wpan device without a netdev then clear
954 * user_ptr[1] so that nl802154_post_doit won't dereference it
955 * to check if it needs to do dev_put(). Otherwise it crashes
956 * since the wpan_dev has been freed, unlike with a netdev where
957 * we need the dev_put() for the netdev to really be freed.
959 if (!wpan_dev
->netdev
)
960 info
->user_ptr
[1] = NULL
;
962 return rdev_del_virtual_intf(rdev
, wpan_dev
);
965 static int nl802154_set_channel(struct sk_buff
*skb
, struct genl_info
*info
)
967 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
970 if (!info
->attrs
[NL802154_ATTR_PAGE
] ||
971 !info
->attrs
[NL802154_ATTR_CHANNEL
])
974 page
= nla_get_u8(info
->attrs
[NL802154_ATTR_PAGE
]);
975 channel
= nla_get_u8(info
->attrs
[NL802154_ATTR_CHANNEL
]);
977 /* check 802.15.4 constraints */
978 if (page
> IEEE802154_MAX_PAGE
|| channel
> IEEE802154_MAX_CHANNEL
||
979 !(rdev
->wpan_phy
.supported
.channels
[page
] & BIT(channel
)))
982 return rdev_set_channel(rdev
, page
, channel
);
985 static int nl802154_set_cca_mode(struct sk_buff
*skb
, struct genl_info
*info
)
987 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
988 struct wpan_phy_cca cca
;
990 if (!(rdev
->wpan_phy
.flags
& WPAN_PHY_FLAG_CCA_MODE
))
993 if (!info
->attrs
[NL802154_ATTR_CCA_MODE
])
996 cca
.mode
= nla_get_u32(info
->attrs
[NL802154_ATTR_CCA_MODE
]);
997 /* checking 802.15.4 constraints */
998 if (cca
.mode
< NL802154_CCA_ENERGY
||
999 cca
.mode
> NL802154_CCA_ATTR_MAX
||
1000 !(rdev
->wpan_phy
.supported
.cca_modes
& BIT(cca
.mode
)))
1003 if (cca
.mode
== NL802154_CCA_ENERGY_CARRIER
) {
1004 if (!info
->attrs
[NL802154_ATTR_CCA_OPT
])
1007 cca
.opt
= nla_get_u32(info
->attrs
[NL802154_ATTR_CCA_OPT
]);
1008 if (cca
.opt
> NL802154_CCA_OPT_ATTR_MAX
||
1009 !(rdev
->wpan_phy
.supported
.cca_opts
& BIT(cca
.opt
)))
1013 return rdev_set_cca_mode(rdev
, &cca
);
1016 static int nl802154_set_cca_ed_level(struct sk_buff
*skb
, struct genl_info
*info
)
1018 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1022 if (!(rdev
->wpan_phy
.flags
& WPAN_PHY_FLAG_CCA_ED_LEVEL
))
1025 if (!info
->attrs
[NL802154_ATTR_CCA_ED_LEVEL
])
1028 ed_level
= nla_get_s32(info
->attrs
[NL802154_ATTR_CCA_ED_LEVEL
]);
1030 for (i
= 0; i
< rdev
->wpan_phy
.supported
.cca_ed_levels_size
; i
++) {
1031 if (ed_level
== rdev
->wpan_phy
.supported
.cca_ed_levels
[i
])
1032 return rdev_set_cca_ed_level(rdev
, ed_level
);
1038 static int nl802154_set_tx_power(struct sk_buff
*skb
, struct genl_info
*info
)
1040 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1044 if (!(rdev
->wpan_phy
.flags
& WPAN_PHY_FLAG_TXPOWER
))
1047 if (!info
->attrs
[NL802154_ATTR_TX_POWER
])
1050 power
= nla_get_s32(info
->attrs
[NL802154_ATTR_TX_POWER
]);
1052 for (i
= 0; i
< rdev
->wpan_phy
.supported
.tx_powers_size
; i
++) {
1053 if (power
== rdev
->wpan_phy
.supported
.tx_powers
[i
])
1054 return rdev_set_tx_power(rdev
, power
);
1060 static int nl802154_set_pan_id(struct sk_buff
*skb
, struct genl_info
*info
)
1062 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1063 struct net_device
*dev
= info
->user_ptr
[1];
1064 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1067 /* conflict here while tx/rx calls */
1068 if (netif_running(dev
))
1071 if (wpan_dev
->lowpan_dev
) {
1072 if (netif_running(wpan_dev
->lowpan_dev
))
1076 /* don't change address fields on monitor */
1077 if (wpan_dev
->iftype
== NL802154_IFTYPE_MONITOR
||
1078 !info
->attrs
[NL802154_ATTR_PAN_ID
])
1081 pan_id
= nla_get_le16(info
->attrs
[NL802154_ATTR_PAN_ID
]);
1084 * I am not sure about to check here on broadcast pan_id.
1085 * Broadcast is a valid setting, comment from 802.15.4:
1086 * If this value is 0xffff, the device is not associated.
1088 * This could useful to simple deassociate an device.
1090 if (pan_id
== cpu_to_le16(IEEE802154_PAN_ID_BROADCAST
))
1093 return rdev_set_pan_id(rdev
, wpan_dev
, pan_id
);
1096 static int nl802154_set_short_addr(struct sk_buff
*skb
, struct genl_info
*info
)
1098 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1099 struct net_device
*dev
= info
->user_ptr
[1];
1100 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1103 /* conflict here while tx/rx calls */
1104 if (netif_running(dev
))
1107 if (wpan_dev
->lowpan_dev
) {
1108 if (netif_running(wpan_dev
->lowpan_dev
))
1112 /* don't change address fields on monitor */
1113 if (wpan_dev
->iftype
== NL802154_IFTYPE_MONITOR
||
1114 !info
->attrs
[NL802154_ATTR_SHORT_ADDR
])
1117 short_addr
= nla_get_le16(info
->attrs
[NL802154_ATTR_SHORT_ADDR
]);
1120 * I am not sure about to check here on broadcast short_addr.
1121 * Broadcast is a valid setting, comment from 802.15.4:
1122 * A value of 0xfffe indicates that the device has
1123 * associated but has not been allocated an address. A
1124 * value of 0xffff indicates that the device does not
1125 * have a short address.
1127 * I think we should allow to set these settings but
1128 * don't allow to allow socket communication with it.
1130 if (short_addr
== cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC
) ||
1131 short_addr
== cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST
))
1134 return rdev_set_short_addr(rdev
, wpan_dev
, short_addr
);
1138 nl802154_set_backoff_exponent(struct sk_buff
*skb
, struct genl_info
*info
)
1140 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1141 struct net_device
*dev
= info
->user_ptr
[1];
1142 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1145 /* should be set on netif open inside phy settings */
1146 if (netif_running(dev
))
1149 if (!info
->attrs
[NL802154_ATTR_MIN_BE
] ||
1150 !info
->attrs
[NL802154_ATTR_MAX_BE
])
1153 min_be
= nla_get_u8(info
->attrs
[NL802154_ATTR_MIN_BE
]);
1154 max_be
= nla_get_u8(info
->attrs
[NL802154_ATTR_MAX_BE
]);
1156 /* check 802.15.4 constraints */
1157 if (min_be
< rdev
->wpan_phy
.supported
.min_minbe
||
1158 min_be
> rdev
->wpan_phy
.supported
.max_minbe
||
1159 max_be
< rdev
->wpan_phy
.supported
.min_maxbe
||
1160 max_be
> rdev
->wpan_phy
.supported
.max_maxbe
||
1164 return rdev_set_backoff_exponent(rdev
, wpan_dev
, min_be
, max_be
);
1168 nl802154_set_max_csma_backoffs(struct sk_buff
*skb
, struct genl_info
*info
)
1170 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1171 struct net_device
*dev
= info
->user_ptr
[1];
1172 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1173 u8 max_csma_backoffs
;
1175 /* conflict here while other running iface settings */
1176 if (netif_running(dev
))
1179 if (!info
->attrs
[NL802154_ATTR_MAX_CSMA_BACKOFFS
])
1182 max_csma_backoffs
= nla_get_u8(
1183 info
->attrs
[NL802154_ATTR_MAX_CSMA_BACKOFFS
]);
1185 /* check 802.15.4 constraints */
1186 if (max_csma_backoffs
< rdev
->wpan_phy
.supported
.min_csma_backoffs
||
1187 max_csma_backoffs
> rdev
->wpan_phy
.supported
.max_csma_backoffs
)
1190 return rdev_set_max_csma_backoffs(rdev
, wpan_dev
, max_csma_backoffs
);
1194 nl802154_set_max_frame_retries(struct sk_buff
*skb
, struct genl_info
*info
)
1196 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1197 struct net_device
*dev
= info
->user_ptr
[1];
1198 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1199 s8 max_frame_retries
;
1201 if (netif_running(dev
))
1204 if (!info
->attrs
[NL802154_ATTR_MAX_FRAME_RETRIES
])
1207 max_frame_retries
= nla_get_s8(
1208 info
->attrs
[NL802154_ATTR_MAX_FRAME_RETRIES
]);
1210 /* check 802.15.4 constraints */
1211 if (max_frame_retries
< rdev
->wpan_phy
.supported
.min_frame_retries
||
1212 max_frame_retries
> rdev
->wpan_phy
.supported
.max_frame_retries
)
1215 return rdev_set_max_frame_retries(rdev
, wpan_dev
, max_frame_retries
);
1218 static int nl802154_set_lbt_mode(struct sk_buff
*skb
, struct genl_info
*info
)
1220 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1221 struct net_device
*dev
= info
->user_ptr
[1];
1222 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1225 if (netif_running(dev
))
1228 if (!info
->attrs
[NL802154_ATTR_LBT_MODE
])
1231 mode
= nla_get_u8(info
->attrs
[NL802154_ATTR_LBT_MODE
]);
1233 if (mode
!= 0 && mode
!= 1)
1236 if (!wpan_phy_supported_bool(mode
, rdev
->wpan_phy
.supported
.lbt
))
1239 return rdev_set_lbt_mode(rdev
, wpan_dev
, mode
);
1243 nl802154_set_ackreq_default(struct sk_buff
*skb
, struct genl_info
*info
)
1245 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1246 struct net_device
*dev
= info
->user_ptr
[1];
1247 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1250 if (netif_running(dev
))
1253 if (!info
->attrs
[NL802154_ATTR_ACKREQ_DEFAULT
])
1256 ackreq
= nla_get_u8(info
->attrs
[NL802154_ATTR_ACKREQ_DEFAULT
]);
1258 if (ackreq
!= 0 && ackreq
!= 1)
1261 return rdev_set_ackreq_default(rdev
, wpan_dev
, ackreq
);
1264 static int nl802154_wpan_phy_netns(struct sk_buff
*skb
, struct genl_info
*info
)
1266 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1270 if (info
->attrs
[NL802154_ATTR_PID
]) {
1271 u32 pid
= nla_get_u32(info
->attrs
[NL802154_ATTR_PID
]);
1273 net
= get_net_ns_by_pid(pid
);
1274 } else if (info
->attrs
[NL802154_ATTR_NETNS_FD
]) {
1275 u32 fd
= nla_get_u32(info
->attrs
[NL802154_ATTR_NETNS_FD
]);
1277 net
= get_net_ns_by_fd(fd
);
1283 return PTR_ERR(net
);
1287 /* check if anything to do */
1288 if (!net_eq(wpan_phy_net(&rdev
->wpan_phy
), net
))
1289 err
= cfg802154_switch_netns(rdev
, net
);
1295 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
1296 static const struct nla_policy nl802154_dev_addr_policy
[NL802154_DEV_ADDR_ATTR_MAX
+ 1] = {
1297 [NL802154_DEV_ADDR_ATTR_PAN_ID
] = { .type
= NLA_U16
},
1298 [NL802154_DEV_ADDR_ATTR_MODE
] = { .type
= NLA_U32
},
1299 [NL802154_DEV_ADDR_ATTR_SHORT
] = { .type
= NLA_U16
},
1300 [NL802154_DEV_ADDR_ATTR_EXTENDED
] = { .type
= NLA_U64
},
1304 ieee802154_llsec_parse_dev_addr(struct nlattr
*nla
,
1305 struct ieee802154_addr
*addr
)
1307 struct nlattr
*attrs
[NL802154_DEV_ADDR_ATTR_MAX
+ 1];
1309 if (!nla
|| nla_parse_nested(attrs
, NL802154_DEV_ADDR_ATTR_MAX
, nla
,
1310 nl802154_dev_addr_policy
, NULL
))
1313 if (!attrs
[NL802154_DEV_ADDR_ATTR_PAN_ID
] ||
1314 !attrs
[NL802154_DEV_ADDR_ATTR_MODE
] ||
1315 !(attrs
[NL802154_DEV_ADDR_ATTR_SHORT
] ||
1316 attrs
[NL802154_DEV_ADDR_ATTR_EXTENDED
]))
1319 addr
->pan_id
= nla_get_le16(attrs
[NL802154_DEV_ADDR_ATTR_PAN_ID
]);
1320 addr
->mode
= nla_get_u32(attrs
[NL802154_DEV_ADDR_ATTR_MODE
]);
1321 switch (addr
->mode
) {
1322 case NL802154_DEV_ADDR_SHORT
:
1323 addr
->short_addr
= nla_get_le16(attrs
[NL802154_DEV_ADDR_ATTR_SHORT
]);
1325 case NL802154_DEV_ADDR_EXTENDED
:
1326 addr
->extended_addr
= nla_get_le64(attrs
[NL802154_DEV_ADDR_ATTR_EXTENDED
]);
1335 static const struct nla_policy nl802154_key_id_policy
[NL802154_KEY_ID_ATTR_MAX
+ 1] = {
1336 [NL802154_KEY_ID_ATTR_MODE
] = { .type
= NLA_U32
},
1337 [NL802154_KEY_ID_ATTR_INDEX
] = { .type
= NLA_U8
},
1338 [NL802154_KEY_ID_ATTR_IMPLICIT
] = { .type
= NLA_NESTED
},
1339 [NL802154_KEY_ID_ATTR_SOURCE_SHORT
] = { .type
= NLA_U32
},
1340 [NL802154_KEY_ID_ATTR_SOURCE_EXTENDED
] = { .type
= NLA_U64
},
1344 ieee802154_llsec_parse_key_id(struct nlattr
*nla
,
1345 struct ieee802154_llsec_key_id
*desc
)
1347 struct nlattr
*attrs
[NL802154_KEY_ID_ATTR_MAX
+ 1];
1349 if (!nla
|| nla_parse_nested(attrs
, NL802154_KEY_ID_ATTR_MAX
, nla
,
1350 nl802154_key_id_policy
, NULL
))
1353 if (!attrs
[NL802154_KEY_ID_ATTR_MODE
])
1356 desc
->mode
= nla_get_u32(attrs
[NL802154_KEY_ID_ATTR_MODE
]);
1357 switch (desc
->mode
) {
1358 case NL802154_KEY_ID_MODE_IMPLICIT
:
1359 if (!attrs
[NL802154_KEY_ID_ATTR_IMPLICIT
])
1362 if (ieee802154_llsec_parse_dev_addr(attrs
[NL802154_KEY_ID_ATTR_IMPLICIT
],
1363 &desc
->device_addr
) < 0)
1366 case NL802154_KEY_ID_MODE_INDEX
:
1368 case NL802154_KEY_ID_MODE_INDEX_SHORT
:
1369 if (!attrs
[NL802154_KEY_ID_ATTR_SOURCE_SHORT
])
1372 desc
->short_source
= nla_get_le32(attrs
[NL802154_KEY_ID_ATTR_SOURCE_SHORT
]);
1374 case NL802154_KEY_ID_MODE_INDEX_EXTENDED
:
1375 if (!attrs
[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED
])
1378 desc
->extended_source
= nla_get_le64(attrs
[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED
]);
1384 if (desc
->mode
!= NL802154_KEY_ID_MODE_IMPLICIT
) {
1385 if (!attrs
[NL802154_KEY_ID_ATTR_INDEX
])
1388 /* TODO change id to idx */
1389 desc
->id
= nla_get_u8(attrs
[NL802154_KEY_ID_ATTR_INDEX
]);
1395 static int nl802154_set_llsec_params(struct sk_buff
*skb
,
1396 struct genl_info
*info
)
1398 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1399 struct net_device
*dev
= info
->user_ptr
[1];
1400 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1401 struct ieee802154_llsec_params params
;
1405 if (info
->attrs
[NL802154_ATTR_SEC_ENABLED
]) {
1408 enabled
= nla_get_u8(info
->attrs
[NL802154_ATTR_SEC_ENABLED
]);
1409 if (enabled
!= 0 && enabled
!= 1)
1412 params
.enabled
= nla_get_u8(info
->attrs
[NL802154_ATTR_SEC_ENABLED
]);
1413 changed
|= IEEE802154_LLSEC_PARAM_ENABLED
;
1416 if (info
->attrs
[NL802154_ATTR_SEC_OUT_KEY_ID
]) {
1417 ret
= ieee802154_llsec_parse_key_id(info
->attrs
[NL802154_ATTR_SEC_OUT_KEY_ID
],
1422 changed
|= IEEE802154_LLSEC_PARAM_OUT_KEY
;
1425 if (info
->attrs
[NL802154_ATTR_SEC_OUT_LEVEL
]) {
1426 params
.out_level
= nla_get_u32(info
->attrs
[NL802154_ATTR_SEC_OUT_LEVEL
]);
1427 if (params
.out_level
> NL802154_SECLEVEL_MAX
)
1430 changed
|= IEEE802154_LLSEC_PARAM_OUT_LEVEL
;
1433 if (info
->attrs
[NL802154_ATTR_SEC_FRAME_COUNTER
]) {
1434 params
.frame_counter
= nla_get_be32(info
->attrs
[NL802154_ATTR_SEC_FRAME_COUNTER
]);
1435 changed
|= IEEE802154_LLSEC_PARAM_FRAME_COUNTER
;
1438 return rdev_set_llsec_params(rdev
, wpan_dev
, ¶ms
, changed
);
1441 static int nl802154_send_key(struct sk_buff
*msg
, u32 cmd
, u32 portid
,
1443 struct cfg802154_registered_device
*rdev
,
1444 struct net_device
*dev
,
1445 const struct ieee802154_llsec_key_entry
*key
)
1448 u32 commands
[NL802154_CMD_FRAME_NR_IDS
/ 32];
1449 struct nlattr
*nl_key
, *nl_key_id
;
1451 hdr
= nl802154hdr_put(msg
, portid
, seq
, flags
, cmd
);
1455 if (nla_put_u32(msg
, NL802154_ATTR_IFINDEX
, dev
->ifindex
))
1456 goto nla_put_failure
;
1458 nl_key
= nla_nest_start(msg
, NL802154_ATTR_SEC_KEY
);
1460 goto nla_put_failure
;
1462 nl_key_id
= nla_nest_start(msg
, NL802154_KEY_ATTR_ID
);
1464 goto nla_put_failure
;
1466 if (ieee802154_llsec_send_key_id(msg
, &key
->id
) < 0)
1467 goto nla_put_failure
;
1469 nla_nest_end(msg
, nl_key_id
);
1471 if (nla_put_u8(msg
, NL802154_KEY_ATTR_USAGE_FRAMES
,
1472 key
->key
->frame_types
))
1473 goto nla_put_failure
;
1475 if (key
->key
->frame_types
& BIT(NL802154_FRAME_CMD
)) {
1476 /* TODO for each nested */
1477 memset(commands
, 0, sizeof(commands
));
1478 commands
[7] = key
->key
->cmd_frame_ids
;
1479 if (nla_put(msg
, NL802154_KEY_ATTR_USAGE_CMDS
,
1480 sizeof(commands
), commands
))
1481 goto nla_put_failure
;
1484 if (nla_put(msg
, NL802154_KEY_ATTR_BYTES
, NL802154_KEY_SIZE
,
1486 goto nla_put_failure
;
1488 nla_nest_end(msg
, nl_key
);
1489 genlmsg_end(msg
, hdr
);
1494 genlmsg_cancel(msg
, hdr
);
1499 nl802154_dump_llsec_key(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1501 struct cfg802154_registered_device
*rdev
= NULL
;
1502 struct ieee802154_llsec_key_entry
*key
;
1503 struct ieee802154_llsec_table
*table
;
1504 struct wpan_dev
*wpan_dev
;
1507 err
= nl802154_prepare_wpan_dev_dump(skb
, cb
, &rdev
, &wpan_dev
);
1511 if (!wpan_dev
->netdev
) {
1516 rdev_lock_llsec_table(rdev
, wpan_dev
);
1517 rdev_get_llsec_table(rdev
, wpan_dev
, &table
);
1519 /* TODO make it like station dump */
1523 list_for_each_entry(key
, &table
->keys
, list
) {
1524 if (nl802154_send_key(skb
, NL802154_CMD_NEW_SEC_KEY
,
1525 NETLINK_CB(cb
->skb
).portid
,
1526 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1527 rdev
, wpan_dev
->netdev
, key
) < 0) {
1530 rdev_unlock_llsec_table(rdev
, wpan_dev
);
1538 rdev_unlock_llsec_table(rdev
, wpan_dev
);
1541 nl802154_finish_wpan_dev_dump(rdev
);
1546 static const struct nla_policy nl802154_key_policy
[NL802154_KEY_ATTR_MAX
+ 1] = {
1547 [NL802154_KEY_ATTR_ID
] = { NLA_NESTED
},
1548 /* TODO handle it as for_each_nested and NLA_FLAG? */
1549 [NL802154_KEY_ATTR_USAGE_FRAMES
] = { NLA_U8
},
1550 /* TODO handle it as for_each_nested, not static array? */
1551 [NL802154_KEY_ATTR_USAGE_CMDS
] = { .len
= NL802154_CMD_FRAME_NR_IDS
/ 8 },
1552 [NL802154_KEY_ATTR_BYTES
] = { .len
= NL802154_KEY_SIZE
},
1555 static int nl802154_add_llsec_key(struct sk_buff
*skb
, struct genl_info
*info
)
1557 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1558 struct net_device
*dev
= info
->user_ptr
[1];
1559 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1560 struct nlattr
*attrs
[NL802154_KEY_ATTR_MAX
+ 1];
1561 struct ieee802154_llsec_key key
= { };
1562 struct ieee802154_llsec_key_id id
= { };
1563 u32 commands
[NL802154_CMD_FRAME_NR_IDS
/ 32] = { };
1565 if (nla_parse_nested(attrs
, NL802154_KEY_ATTR_MAX
,
1566 info
->attrs
[NL802154_ATTR_SEC_KEY
],
1567 nl802154_key_policy
, info
->extack
))
1570 if (!attrs
[NL802154_KEY_ATTR_USAGE_FRAMES
] ||
1571 !attrs
[NL802154_KEY_ATTR_BYTES
])
1574 if (ieee802154_llsec_parse_key_id(attrs
[NL802154_KEY_ATTR_ID
], &id
) < 0)
1577 key
.frame_types
= nla_get_u8(attrs
[NL802154_KEY_ATTR_USAGE_FRAMES
]);
1578 if (key
.frame_types
> BIT(NL802154_FRAME_MAX
) ||
1579 ((key
.frame_types
& BIT(NL802154_FRAME_CMD
)) &&
1580 !attrs
[NL802154_KEY_ATTR_USAGE_CMDS
]))
1583 if (attrs
[NL802154_KEY_ATTR_USAGE_CMDS
]) {
1584 /* TODO for each nested */
1585 nla_memcpy(commands
, attrs
[NL802154_KEY_ATTR_USAGE_CMDS
],
1586 NL802154_CMD_FRAME_NR_IDS
/ 8);
1588 /* TODO understand the -EINVAL logic here? last condition */
1589 if (commands
[0] || commands
[1] || commands
[2] || commands
[3] ||
1590 commands
[4] || commands
[5] || commands
[6] ||
1591 commands
[7] > BIT(NL802154_CMD_FRAME_MAX
))
1594 key
.cmd_frame_ids
= commands
[7];
1596 key
.cmd_frame_ids
= 0;
1599 nla_memcpy(key
.key
, attrs
[NL802154_KEY_ATTR_BYTES
], NL802154_KEY_SIZE
);
1601 if (ieee802154_llsec_parse_key_id(attrs
[NL802154_KEY_ATTR_ID
], &id
) < 0)
1604 return rdev_add_llsec_key(rdev
, wpan_dev
, &id
, &key
);
1607 static int nl802154_del_llsec_key(struct sk_buff
*skb
, struct genl_info
*info
)
1609 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1610 struct net_device
*dev
= info
->user_ptr
[1];
1611 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1612 struct nlattr
*attrs
[NL802154_KEY_ATTR_MAX
+ 1];
1613 struct ieee802154_llsec_key_id id
;
1615 if (nla_parse_nested(attrs
, NL802154_KEY_ATTR_MAX
,
1616 info
->attrs
[NL802154_ATTR_SEC_KEY
],
1617 nl802154_key_policy
, info
->extack
))
1620 if (ieee802154_llsec_parse_key_id(attrs
[NL802154_KEY_ATTR_ID
], &id
) < 0)
1623 return rdev_del_llsec_key(rdev
, wpan_dev
, &id
);
1626 static int nl802154_send_device(struct sk_buff
*msg
, u32 cmd
, u32 portid
,
1628 struct cfg802154_registered_device
*rdev
,
1629 struct net_device
*dev
,
1630 const struct ieee802154_llsec_device
*dev_desc
)
1633 struct nlattr
*nl_device
;
1635 hdr
= nl802154hdr_put(msg
, portid
, seq
, flags
, cmd
);
1639 if (nla_put_u32(msg
, NL802154_ATTR_IFINDEX
, dev
->ifindex
))
1640 goto nla_put_failure
;
1642 nl_device
= nla_nest_start(msg
, NL802154_ATTR_SEC_DEVICE
);
1644 goto nla_put_failure
;
1646 if (nla_put_u32(msg
, NL802154_DEV_ATTR_FRAME_COUNTER
,
1647 dev_desc
->frame_counter
) ||
1648 nla_put_le16(msg
, NL802154_DEV_ATTR_PAN_ID
, dev_desc
->pan_id
) ||
1649 nla_put_le16(msg
, NL802154_DEV_ATTR_SHORT_ADDR
,
1650 dev_desc
->short_addr
) ||
1651 nla_put_le64(msg
, NL802154_DEV_ATTR_EXTENDED_ADDR
,
1652 dev_desc
->hwaddr
, NL802154_DEV_ATTR_PAD
) ||
1653 nla_put_u8(msg
, NL802154_DEV_ATTR_SECLEVEL_EXEMPT
,
1654 dev_desc
->seclevel_exempt
) ||
1655 nla_put_u32(msg
, NL802154_DEV_ATTR_KEY_MODE
, dev_desc
->key_mode
))
1656 goto nla_put_failure
;
1658 nla_nest_end(msg
, nl_device
);
1659 genlmsg_end(msg
, hdr
);
1664 genlmsg_cancel(msg
, hdr
);
1669 nl802154_dump_llsec_dev(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1671 struct cfg802154_registered_device
*rdev
= NULL
;
1672 struct ieee802154_llsec_device
*dev
;
1673 struct ieee802154_llsec_table
*table
;
1674 struct wpan_dev
*wpan_dev
;
1677 err
= nl802154_prepare_wpan_dev_dump(skb
, cb
, &rdev
, &wpan_dev
);
1681 if (!wpan_dev
->netdev
) {
1686 rdev_lock_llsec_table(rdev
, wpan_dev
);
1687 rdev_get_llsec_table(rdev
, wpan_dev
, &table
);
1689 /* TODO make it like station dump */
1693 list_for_each_entry(dev
, &table
->devices
, list
) {
1694 if (nl802154_send_device(skb
, NL802154_CMD_NEW_SEC_LEVEL
,
1695 NETLINK_CB(cb
->skb
).portid
,
1696 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1697 rdev
, wpan_dev
->netdev
, dev
) < 0) {
1700 rdev_unlock_llsec_table(rdev
, wpan_dev
);
1708 rdev_unlock_llsec_table(rdev
, wpan_dev
);
1711 nl802154_finish_wpan_dev_dump(rdev
);
1716 static const struct nla_policy nl802154_dev_policy
[NL802154_DEV_ATTR_MAX
+ 1] = {
1717 [NL802154_DEV_ATTR_FRAME_COUNTER
] = { NLA_U32
},
1718 [NL802154_DEV_ATTR_PAN_ID
] = { .type
= NLA_U16
},
1719 [NL802154_DEV_ATTR_SHORT_ADDR
] = { .type
= NLA_U16
},
1720 [NL802154_DEV_ATTR_EXTENDED_ADDR
] = { .type
= NLA_U64
},
1721 [NL802154_DEV_ATTR_SECLEVEL_EXEMPT
] = { NLA_U8
},
1722 [NL802154_DEV_ATTR_KEY_MODE
] = { NLA_U32
},
1726 ieee802154_llsec_parse_device(struct nlattr
*nla
,
1727 struct ieee802154_llsec_device
*dev
)
1729 struct nlattr
*attrs
[NL802154_DEV_ATTR_MAX
+ 1];
1731 if (!nla
|| nla_parse_nested(attrs
, NL802154_DEV_ATTR_MAX
,
1732 nla
, nl802154_dev_policy
, NULL
))
1735 memset(dev
, 0, sizeof(*dev
));
1737 if (!attrs
[NL802154_DEV_ATTR_FRAME_COUNTER
] ||
1738 !attrs
[NL802154_DEV_ATTR_PAN_ID
] ||
1739 !attrs
[NL802154_DEV_ATTR_SHORT_ADDR
] ||
1740 !attrs
[NL802154_DEV_ATTR_EXTENDED_ADDR
] ||
1741 !attrs
[NL802154_DEV_ATTR_SECLEVEL_EXEMPT
] ||
1742 !attrs
[NL802154_DEV_ATTR_KEY_MODE
])
1746 dev
->frame_counter
= nla_get_u32(attrs
[NL802154_DEV_ATTR_FRAME_COUNTER
]);
1747 dev
->pan_id
= nla_get_le16(attrs
[NL802154_DEV_ATTR_PAN_ID
]);
1748 dev
->short_addr
= nla_get_le16(attrs
[NL802154_DEV_ATTR_SHORT_ADDR
]);
1749 /* TODO rename hwaddr to extended_addr */
1750 dev
->hwaddr
= nla_get_le64(attrs
[NL802154_DEV_ATTR_EXTENDED_ADDR
]);
1751 dev
->seclevel_exempt
= nla_get_u8(attrs
[NL802154_DEV_ATTR_SECLEVEL_EXEMPT
]);
1752 dev
->key_mode
= nla_get_u32(attrs
[NL802154_DEV_ATTR_KEY_MODE
]);
1754 if (dev
->key_mode
> NL802154_DEVKEY_MAX
||
1755 (dev
->seclevel_exempt
!= 0 && dev
->seclevel_exempt
!= 1))
1761 static int nl802154_add_llsec_dev(struct sk_buff
*skb
, struct genl_info
*info
)
1763 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1764 struct net_device
*dev
= info
->user_ptr
[1];
1765 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1766 struct ieee802154_llsec_device dev_desc
;
1768 if (ieee802154_llsec_parse_device(info
->attrs
[NL802154_ATTR_SEC_DEVICE
],
1772 return rdev_add_device(rdev
, wpan_dev
, &dev_desc
);
1775 static int nl802154_del_llsec_dev(struct sk_buff
*skb
, struct genl_info
*info
)
1777 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1778 struct net_device
*dev
= info
->user_ptr
[1];
1779 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1780 struct nlattr
*attrs
[NL802154_DEV_ATTR_MAX
+ 1];
1781 __le64 extended_addr
;
1783 if (nla_parse_nested(attrs
, NL802154_DEV_ATTR_MAX
,
1784 info
->attrs
[NL802154_ATTR_SEC_DEVICE
],
1785 nl802154_dev_policy
, info
->extack
))
1788 if (!attrs
[NL802154_DEV_ATTR_EXTENDED_ADDR
])
1791 extended_addr
= nla_get_le64(attrs
[NL802154_DEV_ATTR_EXTENDED_ADDR
]);
1792 return rdev_del_device(rdev
, wpan_dev
, extended_addr
);
1795 static int nl802154_send_devkey(struct sk_buff
*msg
, u32 cmd
, u32 portid
,
1797 struct cfg802154_registered_device
*rdev
,
1798 struct net_device
*dev
, __le64 extended_addr
,
1799 const struct ieee802154_llsec_device_key
*devkey
)
1802 struct nlattr
*nl_devkey
, *nl_key_id
;
1804 hdr
= nl802154hdr_put(msg
, portid
, seq
, flags
, cmd
);
1808 if (nla_put_u32(msg
, NL802154_ATTR_IFINDEX
, dev
->ifindex
))
1809 goto nla_put_failure
;
1811 nl_devkey
= nla_nest_start(msg
, NL802154_ATTR_SEC_DEVKEY
);
1813 goto nla_put_failure
;
1815 if (nla_put_le64(msg
, NL802154_DEVKEY_ATTR_EXTENDED_ADDR
,
1816 extended_addr
, NL802154_DEVKEY_ATTR_PAD
) ||
1817 nla_put_u32(msg
, NL802154_DEVKEY_ATTR_FRAME_COUNTER
,
1818 devkey
->frame_counter
))
1819 goto nla_put_failure
;
1821 nl_key_id
= nla_nest_start(msg
, NL802154_DEVKEY_ATTR_ID
);
1823 goto nla_put_failure
;
1825 if (ieee802154_llsec_send_key_id(msg
, &devkey
->key_id
) < 0)
1826 goto nla_put_failure
;
1828 nla_nest_end(msg
, nl_key_id
);
1829 nla_nest_end(msg
, nl_devkey
);
1830 genlmsg_end(msg
, hdr
);
1835 genlmsg_cancel(msg
, hdr
);
1840 nl802154_dump_llsec_devkey(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1842 struct cfg802154_registered_device
*rdev
= NULL
;
1843 struct ieee802154_llsec_device_key
*kpos
;
1844 struct ieee802154_llsec_device
*dpos
;
1845 struct ieee802154_llsec_table
*table
;
1846 struct wpan_dev
*wpan_dev
;
1849 err
= nl802154_prepare_wpan_dev_dump(skb
, cb
, &rdev
, &wpan_dev
);
1853 if (!wpan_dev
->netdev
) {
1858 rdev_lock_llsec_table(rdev
, wpan_dev
);
1859 rdev_get_llsec_table(rdev
, wpan_dev
, &table
);
1861 /* TODO make it like station dump */
1865 /* TODO look if remove devkey and do some nested attribute */
1866 list_for_each_entry(dpos
, &table
->devices
, list
) {
1867 list_for_each_entry(kpos
, &dpos
->keys
, list
) {
1868 if (nl802154_send_devkey(skb
,
1869 NL802154_CMD_NEW_SEC_LEVEL
,
1870 NETLINK_CB(cb
->skb
).portid
,
1878 rdev_unlock_llsec_table(rdev
, wpan_dev
);
1887 rdev_unlock_llsec_table(rdev
, wpan_dev
);
1890 nl802154_finish_wpan_dev_dump(rdev
);
1895 static const struct nla_policy nl802154_devkey_policy
[NL802154_DEVKEY_ATTR_MAX
+ 1] = {
1896 [NL802154_DEVKEY_ATTR_FRAME_COUNTER
] = { NLA_U32
},
1897 [NL802154_DEVKEY_ATTR_EXTENDED_ADDR
] = { NLA_U64
},
1898 [NL802154_DEVKEY_ATTR_ID
] = { NLA_NESTED
},
1901 static int nl802154_add_llsec_devkey(struct sk_buff
*skb
, struct genl_info
*info
)
1903 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1904 struct net_device
*dev
= info
->user_ptr
[1];
1905 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1906 struct nlattr
*attrs
[NL802154_DEVKEY_ATTR_MAX
+ 1];
1907 struct ieee802154_llsec_device_key key
;
1908 __le64 extended_addr
;
1910 if (!info
->attrs
[NL802154_ATTR_SEC_DEVKEY
] ||
1911 nla_parse_nested(attrs
, NL802154_DEVKEY_ATTR_MAX
,
1912 info
->attrs
[NL802154_ATTR_SEC_DEVKEY
],
1913 nl802154_devkey_policy
, info
->extack
) < 0)
1916 if (!attrs
[NL802154_DEVKEY_ATTR_FRAME_COUNTER
] ||
1917 !attrs
[NL802154_DEVKEY_ATTR_EXTENDED_ADDR
])
1920 /* TODO change key.id ? */
1921 if (ieee802154_llsec_parse_key_id(attrs
[NL802154_DEVKEY_ATTR_ID
],
1926 key
.frame_counter
= nla_get_u32(attrs
[NL802154_DEVKEY_ATTR_FRAME_COUNTER
]);
1927 /* TODO change naming hwaddr -> extended_addr
1928 * check unique identifier short+pan OR extended_addr
1930 extended_addr
= nla_get_le64(attrs
[NL802154_DEVKEY_ATTR_EXTENDED_ADDR
]);
1931 return rdev_add_devkey(rdev
, wpan_dev
, extended_addr
, &key
);
1934 static int nl802154_del_llsec_devkey(struct sk_buff
*skb
, struct genl_info
*info
)
1936 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
1937 struct net_device
*dev
= info
->user_ptr
[1];
1938 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
1939 struct nlattr
*attrs
[NL802154_DEVKEY_ATTR_MAX
+ 1];
1940 struct ieee802154_llsec_device_key key
;
1941 __le64 extended_addr
;
1943 if (nla_parse_nested(attrs
, NL802154_DEVKEY_ATTR_MAX
,
1944 info
->attrs
[NL802154_ATTR_SEC_DEVKEY
],
1945 nl802154_devkey_policy
, info
->extack
))
1948 if (!attrs
[NL802154_DEVKEY_ATTR_EXTENDED_ADDR
])
1951 /* TODO change key.id ? */
1952 if (ieee802154_llsec_parse_key_id(attrs
[NL802154_DEVKEY_ATTR_ID
],
1956 /* TODO change naming hwaddr -> extended_addr
1957 * check unique identifier short+pan OR extended_addr
1959 extended_addr
= nla_get_le64(attrs
[NL802154_DEVKEY_ATTR_EXTENDED_ADDR
]);
1960 return rdev_del_devkey(rdev
, wpan_dev
, extended_addr
, &key
);
1963 static int nl802154_send_seclevel(struct sk_buff
*msg
, u32 cmd
, u32 portid
,
1965 struct cfg802154_registered_device
*rdev
,
1966 struct net_device
*dev
,
1967 const struct ieee802154_llsec_seclevel
*sl
)
1970 struct nlattr
*nl_seclevel
;
1972 hdr
= nl802154hdr_put(msg
, portid
, seq
, flags
, cmd
);
1976 if (nla_put_u32(msg
, NL802154_ATTR_IFINDEX
, dev
->ifindex
))
1977 goto nla_put_failure
;
1979 nl_seclevel
= nla_nest_start(msg
, NL802154_ATTR_SEC_LEVEL
);
1981 goto nla_put_failure
;
1983 if (nla_put_u32(msg
, NL802154_SECLEVEL_ATTR_FRAME
, sl
->frame_type
) ||
1984 nla_put_u32(msg
, NL802154_SECLEVEL_ATTR_LEVELS
, sl
->sec_levels
) ||
1985 nla_put_u8(msg
, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE
,
1986 sl
->device_override
))
1987 goto nla_put_failure
;
1989 if (sl
->frame_type
== NL802154_FRAME_CMD
) {
1990 if (nla_put_u32(msg
, NL802154_SECLEVEL_ATTR_CMD_FRAME
,
1992 goto nla_put_failure
;
1995 nla_nest_end(msg
, nl_seclevel
);
1996 genlmsg_end(msg
, hdr
);
2001 genlmsg_cancel(msg
, hdr
);
2006 nl802154_dump_llsec_seclevel(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2008 struct cfg802154_registered_device
*rdev
= NULL
;
2009 struct ieee802154_llsec_seclevel
*sl
;
2010 struct ieee802154_llsec_table
*table
;
2011 struct wpan_dev
*wpan_dev
;
2014 err
= nl802154_prepare_wpan_dev_dump(skb
, cb
, &rdev
, &wpan_dev
);
2018 if (!wpan_dev
->netdev
) {
2023 rdev_lock_llsec_table(rdev
, wpan_dev
);
2024 rdev_get_llsec_table(rdev
, wpan_dev
, &table
);
2026 /* TODO make it like station dump */
2030 list_for_each_entry(sl
, &table
->security_levels
, list
) {
2031 if (nl802154_send_seclevel(skb
, NL802154_CMD_NEW_SEC_LEVEL
,
2032 NETLINK_CB(cb
->skb
).portid
,
2033 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
2034 rdev
, wpan_dev
->netdev
, sl
) < 0) {
2037 rdev_unlock_llsec_table(rdev
, wpan_dev
);
2045 rdev_unlock_llsec_table(rdev
, wpan_dev
);
2048 nl802154_finish_wpan_dev_dump(rdev
);
2053 static const struct nla_policy nl802154_seclevel_policy
[NL802154_SECLEVEL_ATTR_MAX
+ 1] = {
2054 [NL802154_SECLEVEL_ATTR_LEVELS
] = { .type
= NLA_U8
},
2055 [NL802154_SECLEVEL_ATTR_FRAME
] = { .type
= NLA_U32
},
2056 [NL802154_SECLEVEL_ATTR_CMD_FRAME
] = { .type
= NLA_U32
},
2057 [NL802154_SECLEVEL_ATTR_DEV_OVERRIDE
] = { .type
= NLA_U8
},
2061 llsec_parse_seclevel(struct nlattr
*nla
, struct ieee802154_llsec_seclevel
*sl
)
2063 struct nlattr
*attrs
[NL802154_SECLEVEL_ATTR_MAX
+ 1];
2065 if (!nla
|| nla_parse_nested(attrs
, NL802154_SECLEVEL_ATTR_MAX
,
2066 nla
, nl802154_seclevel_policy
, NULL
))
2069 memset(sl
, 0, sizeof(*sl
));
2071 if (!attrs
[NL802154_SECLEVEL_ATTR_LEVELS
] ||
2072 !attrs
[NL802154_SECLEVEL_ATTR_FRAME
] ||
2073 !attrs
[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE
])
2076 sl
->sec_levels
= nla_get_u8(attrs
[NL802154_SECLEVEL_ATTR_LEVELS
]);
2077 sl
->frame_type
= nla_get_u32(attrs
[NL802154_SECLEVEL_ATTR_FRAME
]);
2078 sl
->device_override
= nla_get_u8(attrs
[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE
]);
2079 if (sl
->frame_type
> NL802154_FRAME_MAX
||
2080 (sl
->device_override
!= 0 && sl
->device_override
!= 1))
2083 if (sl
->frame_type
== NL802154_FRAME_CMD
) {
2084 if (!attrs
[NL802154_SECLEVEL_ATTR_CMD_FRAME
])
2087 sl
->cmd_frame_id
= nla_get_u32(attrs
[NL802154_SECLEVEL_ATTR_CMD_FRAME
]);
2088 if (sl
->cmd_frame_id
> NL802154_CMD_FRAME_MAX
)
2095 static int nl802154_add_llsec_seclevel(struct sk_buff
*skb
,
2096 struct genl_info
*info
)
2098 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
2099 struct net_device
*dev
= info
->user_ptr
[1];
2100 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
2101 struct ieee802154_llsec_seclevel sl
;
2103 if (llsec_parse_seclevel(info
->attrs
[NL802154_ATTR_SEC_LEVEL
],
2107 return rdev_add_seclevel(rdev
, wpan_dev
, &sl
);
2110 static int nl802154_del_llsec_seclevel(struct sk_buff
*skb
,
2111 struct genl_info
*info
)
2113 struct cfg802154_registered_device
*rdev
= info
->user_ptr
[0];
2114 struct net_device
*dev
= info
->user_ptr
[1];
2115 struct wpan_dev
*wpan_dev
= dev
->ieee802154_ptr
;
2116 struct ieee802154_llsec_seclevel sl
;
2118 if (!info
->attrs
[NL802154_ATTR_SEC_LEVEL
] ||
2119 llsec_parse_seclevel(info
->attrs
[NL802154_ATTR_SEC_LEVEL
],
2123 return rdev_del_seclevel(rdev
, wpan_dev
, &sl
);
2125 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2127 #define NL802154_FLAG_NEED_WPAN_PHY 0x01
2128 #define NL802154_FLAG_NEED_NETDEV 0x02
2129 #define NL802154_FLAG_NEED_RTNL 0x04
2130 #define NL802154_FLAG_CHECK_NETDEV_UP 0x08
2131 #define NL802154_FLAG_NEED_NETDEV_UP (NL802154_FLAG_NEED_NETDEV |\
2132 NL802154_FLAG_CHECK_NETDEV_UP)
2133 #define NL802154_FLAG_NEED_WPAN_DEV 0x10
2134 #define NL802154_FLAG_NEED_WPAN_DEV_UP (NL802154_FLAG_NEED_WPAN_DEV |\
2135 NL802154_FLAG_CHECK_NETDEV_UP)
2137 static int nl802154_pre_doit(const struct genl_ops
*ops
, struct sk_buff
*skb
,
2138 struct genl_info
*info
)
2140 struct cfg802154_registered_device
*rdev
;
2141 struct wpan_dev
*wpan_dev
;
2142 struct net_device
*dev
;
2143 bool rtnl
= ops
->internal_flags
& NL802154_FLAG_NEED_RTNL
;
2148 if (ops
->internal_flags
& NL802154_FLAG_NEED_WPAN_PHY
) {
2149 rdev
= cfg802154_get_dev_from_info(genl_info_net(info
), info
);
2153 return PTR_ERR(rdev
);
2155 info
->user_ptr
[0] = rdev
;
2156 } else if (ops
->internal_flags
& NL802154_FLAG_NEED_NETDEV
||
2157 ops
->internal_flags
& NL802154_FLAG_NEED_WPAN_DEV
) {
2159 wpan_dev
= __cfg802154_wpan_dev_from_attrs(genl_info_net(info
),
2161 if (IS_ERR(wpan_dev
)) {
2164 return PTR_ERR(wpan_dev
);
2167 dev
= wpan_dev
->netdev
;
2168 rdev
= wpan_phy_to_rdev(wpan_dev
->wpan_phy
);
2170 if (ops
->internal_flags
& NL802154_FLAG_NEED_NETDEV
) {
2177 info
->user_ptr
[1] = dev
;
2179 info
->user_ptr
[1] = wpan_dev
;
2183 if (ops
->internal_flags
& NL802154_FLAG_CHECK_NETDEV_UP
&&
2184 !netif_running(dev
)) {
2193 info
->user_ptr
[0] = rdev
;
2199 static void nl802154_post_doit(const struct genl_ops
*ops
, struct sk_buff
*skb
,
2200 struct genl_info
*info
)
2202 if (info
->user_ptr
[1]) {
2203 if (ops
->internal_flags
& NL802154_FLAG_NEED_WPAN_DEV
) {
2204 struct wpan_dev
*wpan_dev
= info
->user_ptr
[1];
2206 if (wpan_dev
->netdev
)
2207 dev_put(wpan_dev
->netdev
);
2209 dev_put(info
->user_ptr
[1]);
2213 if (ops
->internal_flags
& NL802154_FLAG_NEED_RTNL
)
2217 static const struct genl_ops nl802154_ops
[] = {
2219 .cmd
= NL802154_CMD_GET_WPAN_PHY
,
2220 .doit
= nl802154_get_wpan_phy
,
2221 .dumpit
= nl802154_dump_wpan_phy
,
2222 .done
= nl802154_dump_wpan_phy_done
,
2223 .policy
= nl802154_policy
,
2224 /* can be retrieved by unprivileged users */
2225 .internal_flags
= NL802154_FLAG_NEED_WPAN_PHY
|
2226 NL802154_FLAG_NEED_RTNL
,
2229 .cmd
= NL802154_CMD_GET_INTERFACE
,
2230 .doit
= nl802154_get_interface
,
2231 .dumpit
= nl802154_dump_interface
,
2232 .policy
= nl802154_policy
,
2233 /* can be retrieved by unprivileged users */
2234 .internal_flags
= NL802154_FLAG_NEED_WPAN_DEV
|
2235 NL802154_FLAG_NEED_RTNL
,
2238 .cmd
= NL802154_CMD_NEW_INTERFACE
,
2239 .doit
= nl802154_new_interface
,
2240 .policy
= nl802154_policy
,
2241 .flags
= GENL_ADMIN_PERM
,
2242 .internal_flags
= NL802154_FLAG_NEED_WPAN_PHY
|
2243 NL802154_FLAG_NEED_RTNL
,
2246 .cmd
= NL802154_CMD_DEL_INTERFACE
,
2247 .doit
= nl802154_del_interface
,
2248 .policy
= nl802154_policy
,
2249 .flags
= GENL_ADMIN_PERM
,
2250 .internal_flags
= NL802154_FLAG_NEED_WPAN_DEV
|
2251 NL802154_FLAG_NEED_RTNL
,
2254 .cmd
= NL802154_CMD_SET_CHANNEL
,
2255 .doit
= nl802154_set_channel
,
2256 .policy
= nl802154_policy
,
2257 .flags
= GENL_ADMIN_PERM
,
2258 .internal_flags
= NL802154_FLAG_NEED_WPAN_PHY
|
2259 NL802154_FLAG_NEED_RTNL
,
2262 .cmd
= NL802154_CMD_SET_CCA_MODE
,
2263 .doit
= nl802154_set_cca_mode
,
2264 .policy
= nl802154_policy
,
2265 .flags
= GENL_ADMIN_PERM
,
2266 .internal_flags
= NL802154_FLAG_NEED_WPAN_PHY
|
2267 NL802154_FLAG_NEED_RTNL
,
2270 .cmd
= NL802154_CMD_SET_CCA_ED_LEVEL
,
2271 .doit
= nl802154_set_cca_ed_level
,
2272 .policy
= nl802154_policy
,
2273 .flags
= GENL_ADMIN_PERM
,
2274 .internal_flags
= NL802154_FLAG_NEED_WPAN_PHY
|
2275 NL802154_FLAG_NEED_RTNL
,
2278 .cmd
= NL802154_CMD_SET_TX_POWER
,
2279 .doit
= nl802154_set_tx_power
,
2280 .policy
= nl802154_policy
,
2281 .flags
= GENL_ADMIN_PERM
,
2282 .internal_flags
= NL802154_FLAG_NEED_WPAN_PHY
|
2283 NL802154_FLAG_NEED_RTNL
,
2286 .cmd
= NL802154_CMD_SET_WPAN_PHY_NETNS
,
2287 .doit
= nl802154_wpan_phy_netns
,
2288 .policy
= nl802154_policy
,
2289 .flags
= GENL_ADMIN_PERM
,
2290 .internal_flags
= NL802154_FLAG_NEED_WPAN_PHY
|
2291 NL802154_FLAG_NEED_RTNL
,
2294 .cmd
= NL802154_CMD_SET_PAN_ID
,
2295 .doit
= nl802154_set_pan_id
,
2296 .policy
= nl802154_policy
,
2297 .flags
= GENL_ADMIN_PERM
,
2298 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2299 NL802154_FLAG_NEED_RTNL
,
2302 .cmd
= NL802154_CMD_SET_SHORT_ADDR
,
2303 .doit
= nl802154_set_short_addr
,
2304 .policy
= nl802154_policy
,
2305 .flags
= GENL_ADMIN_PERM
,
2306 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2307 NL802154_FLAG_NEED_RTNL
,
2310 .cmd
= NL802154_CMD_SET_BACKOFF_EXPONENT
,
2311 .doit
= nl802154_set_backoff_exponent
,
2312 .policy
= nl802154_policy
,
2313 .flags
= GENL_ADMIN_PERM
,
2314 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2315 NL802154_FLAG_NEED_RTNL
,
2318 .cmd
= NL802154_CMD_SET_MAX_CSMA_BACKOFFS
,
2319 .doit
= nl802154_set_max_csma_backoffs
,
2320 .policy
= nl802154_policy
,
2321 .flags
= GENL_ADMIN_PERM
,
2322 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2323 NL802154_FLAG_NEED_RTNL
,
2326 .cmd
= NL802154_CMD_SET_MAX_FRAME_RETRIES
,
2327 .doit
= nl802154_set_max_frame_retries
,
2328 .policy
= nl802154_policy
,
2329 .flags
= GENL_ADMIN_PERM
,
2330 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2331 NL802154_FLAG_NEED_RTNL
,
2334 .cmd
= NL802154_CMD_SET_LBT_MODE
,
2335 .doit
= nl802154_set_lbt_mode
,
2336 .policy
= nl802154_policy
,
2337 .flags
= GENL_ADMIN_PERM
,
2338 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2339 NL802154_FLAG_NEED_RTNL
,
2342 .cmd
= NL802154_CMD_SET_ACKREQ_DEFAULT
,
2343 .doit
= nl802154_set_ackreq_default
,
2344 .policy
= nl802154_policy
,
2345 .flags
= GENL_ADMIN_PERM
,
2346 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2347 NL802154_FLAG_NEED_RTNL
,
2349 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
2351 .cmd
= NL802154_CMD_SET_SEC_PARAMS
,
2352 .doit
= nl802154_set_llsec_params
,
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_GET_SEC_KEY
,
2360 /* TODO .doit by matching key id? */
2361 .dumpit
= nl802154_dump_llsec_key
,
2362 .policy
= nl802154_policy
,
2363 .flags
= GENL_ADMIN_PERM
,
2364 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2365 NL802154_FLAG_NEED_RTNL
,
2368 .cmd
= NL802154_CMD_NEW_SEC_KEY
,
2369 .doit
= nl802154_add_llsec_key
,
2370 .policy
= nl802154_policy
,
2371 .flags
= GENL_ADMIN_PERM
,
2372 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2373 NL802154_FLAG_NEED_RTNL
,
2376 .cmd
= NL802154_CMD_DEL_SEC_KEY
,
2377 .doit
= nl802154_del_llsec_key
,
2378 .policy
= nl802154_policy
,
2379 .flags
= GENL_ADMIN_PERM
,
2380 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2381 NL802154_FLAG_NEED_RTNL
,
2383 /* TODO unique identifier must short+pan OR extended_addr */
2385 .cmd
= NL802154_CMD_GET_SEC_DEV
,
2386 /* TODO .doit by matching extended_addr? */
2387 .dumpit
= nl802154_dump_llsec_dev
,
2388 .policy
= nl802154_policy
,
2389 .flags
= GENL_ADMIN_PERM
,
2390 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2391 NL802154_FLAG_NEED_RTNL
,
2394 .cmd
= NL802154_CMD_NEW_SEC_DEV
,
2395 .doit
= nl802154_add_llsec_dev
,
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_DEL_SEC_DEV
,
2403 .doit
= nl802154_del_llsec_dev
,
2404 .policy
= nl802154_policy
,
2405 .flags
= GENL_ADMIN_PERM
,
2406 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2407 NL802154_FLAG_NEED_RTNL
,
2409 /* TODO remove complete devkey, put it as nested? */
2411 .cmd
= NL802154_CMD_GET_SEC_DEVKEY
,
2412 /* TODO doit by matching ??? */
2413 .dumpit
= nl802154_dump_llsec_devkey
,
2414 .policy
= nl802154_policy
,
2415 .flags
= GENL_ADMIN_PERM
,
2416 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2417 NL802154_FLAG_NEED_RTNL
,
2420 .cmd
= NL802154_CMD_NEW_SEC_DEVKEY
,
2421 .doit
= nl802154_add_llsec_devkey
,
2422 .policy
= nl802154_policy
,
2423 .flags
= GENL_ADMIN_PERM
,
2424 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2425 NL802154_FLAG_NEED_RTNL
,
2428 .cmd
= NL802154_CMD_DEL_SEC_DEVKEY
,
2429 .doit
= nl802154_del_llsec_devkey
,
2430 .policy
= nl802154_policy
,
2431 .flags
= GENL_ADMIN_PERM
,
2432 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2433 NL802154_FLAG_NEED_RTNL
,
2436 .cmd
= NL802154_CMD_GET_SEC_LEVEL
,
2437 /* TODO .doit by matching frame_type? */
2438 .dumpit
= nl802154_dump_llsec_seclevel
,
2439 .policy
= nl802154_policy
,
2440 .flags
= GENL_ADMIN_PERM
,
2441 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2442 NL802154_FLAG_NEED_RTNL
,
2445 .cmd
= NL802154_CMD_NEW_SEC_LEVEL
,
2446 .doit
= nl802154_add_llsec_seclevel
,
2447 .policy
= nl802154_policy
,
2448 .flags
= GENL_ADMIN_PERM
,
2449 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2450 NL802154_FLAG_NEED_RTNL
,
2453 .cmd
= NL802154_CMD_DEL_SEC_LEVEL
,
2454 /* TODO match frame_type only? */
2455 .doit
= nl802154_del_llsec_seclevel
,
2456 .policy
= nl802154_policy
,
2457 .flags
= GENL_ADMIN_PERM
,
2458 .internal_flags
= NL802154_FLAG_NEED_NETDEV
|
2459 NL802154_FLAG_NEED_RTNL
,
2461 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2464 static struct genl_family nl802154_fam __ro_after_init
= {
2465 .name
= NL802154_GENL_NAME
, /* have users key off the name instead */
2466 .hdrsize
= 0, /* no private header */
2467 .version
= 1, /* no particular meaning now */
2468 .maxattr
= NL802154_ATTR_MAX
,
2470 .pre_doit
= nl802154_pre_doit
,
2471 .post_doit
= nl802154_post_doit
,
2472 .module
= THIS_MODULE
,
2473 .ops
= nl802154_ops
,
2474 .n_ops
= ARRAY_SIZE(nl802154_ops
),
2475 .mcgrps
= nl802154_mcgrps
,
2476 .n_mcgrps
= ARRAY_SIZE(nl802154_mcgrps
),
2479 /* initialisation/exit functions */
2480 int __init
nl802154_init(void)
2482 return genl_register_family(&nl802154_fam
);
2485 void nl802154_exit(void)
2487 genl_unregister_family(&nl802154_fam
);