xref: /linux/net/ieee802154/nl802154.c (revision 05db59a0619969a47ab87050985344177c662cab)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Authors:
5  * Alexander Aring <aar@pengutronix.de>
6  *
7  * Based on: net/wireless/nl80211.c
8  */
9 
10 #include <linux/rtnetlink.h>
11 
12 #include <net/cfg802154.h>
13 #include <net/genetlink.h>
14 #include <net/mac802154.h>
15 #include <net/netlink.h>
16 #include <net/nl802154.h>
17 #include <net/sock.h>
18 
19 #include "nl802154.h"
20 #include "rdev-ops.h"
21 #include "core.h"
22 
23 /* the netlink family */
24 static struct genl_family nl802154_fam;
25 
26 /* multicast groups */
27 enum nl802154_multicast_groups {
28 	NL802154_MCGRP_CONFIG,
29 	NL802154_MCGRP_SCAN,
30 };
31 
32 static const struct genl_multicast_group nl802154_mcgrps[] = {
33 	[NL802154_MCGRP_CONFIG] = { .name = "config", },
34 	[NL802154_MCGRP_SCAN] = { .name = "scan", },
35 };
36 
37 /* returns ERR_PTR values */
38 static struct wpan_dev *
39 __cfg802154_wpan_dev_from_attrs(struct net *netns, struct nlattr **attrs)
40 {
41 	struct cfg802154_registered_device *rdev;
42 	struct wpan_dev *result = NULL;
43 	bool have_ifidx = attrs[NL802154_ATTR_IFINDEX];
44 	bool have_wpan_dev_id = attrs[NL802154_ATTR_WPAN_DEV];
45 	u64 wpan_dev_id;
46 	int wpan_phy_idx = -1;
47 	int ifidx = -1;
48 
49 	ASSERT_RTNL();
50 
51 	if (!have_ifidx && !have_wpan_dev_id)
52 		return ERR_PTR(-EINVAL);
53 
54 	if (have_ifidx)
55 		ifidx = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
56 	if (have_wpan_dev_id) {
57 		wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
58 		wpan_phy_idx = wpan_dev_id >> 32;
59 	}
60 
61 	list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
62 		struct wpan_dev *wpan_dev;
63 
64 		if (wpan_phy_net(&rdev->wpan_phy) != netns)
65 			continue;
66 
67 		if (have_wpan_dev_id && rdev->wpan_phy_idx != wpan_phy_idx)
68 			continue;
69 
70 		list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
71 			if (have_ifidx && wpan_dev->netdev &&
72 			    wpan_dev->netdev->ifindex == ifidx) {
73 				result = wpan_dev;
74 				break;
75 			}
76 			if (have_wpan_dev_id &&
77 			    wpan_dev->identifier == (u32)wpan_dev_id) {
78 				result = wpan_dev;
79 				break;
80 			}
81 		}
82 
83 		if (result)
84 			break;
85 	}
86 
87 	if (result)
88 		return result;
89 
90 	return ERR_PTR(-ENODEV);
91 }
92 
93 static struct cfg802154_registered_device *
94 __cfg802154_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
95 {
96 	struct cfg802154_registered_device *rdev = NULL, *tmp;
97 	struct net_device *netdev;
98 
99 	ASSERT_RTNL();
100 
101 	if (!attrs[NL802154_ATTR_WPAN_PHY] &&
102 	    !attrs[NL802154_ATTR_IFINDEX] &&
103 	    !attrs[NL802154_ATTR_WPAN_DEV])
104 		return ERR_PTR(-EINVAL);
105 
106 	if (attrs[NL802154_ATTR_WPAN_PHY])
107 		rdev = cfg802154_rdev_by_wpan_phy_idx(
108 				nla_get_u32(attrs[NL802154_ATTR_WPAN_PHY]));
109 
110 	if (attrs[NL802154_ATTR_WPAN_DEV]) {
111 		u64 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
112 		struct wpan_dev *wpan_dev;
113 		bool found = false;
114 
115 		tmp = cfg802154_rdev_by_wpan_phy_idx(wpan_dev_id >> 32);
116 		if (tmp) {
117 			/* make sure wpan_dev exists */
118 			list_for_each_entry(wpan_dev, &tmp->wpan_dev_list, list) {
119 				if (wpan_dev->identifier != (u32)wpan_dev_id)
120 					continue;
121 				found = true;
122 				break;
123 			}
124 
125 			if (!found)
126 				tmp = NULL;
127 
128 			if (rdev && tmp != rdev)
129 				return ERR_PTR(-EINVAL);
130 			rdev = tmp;
131 		}
132 	}
133 
134 	if (attrs[NL802154_ATTR_IFINDEX]) {
135 		int ifindex = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
136 
137 		netdev = __dev_get_by_index(netns, ifindex);
138 		if (netdev) {
139 			if (netdev->ieee802154_ptr)
140 				tmp = wpan_phy_to_rdev(
141 						netdev->ieee802154_ptr->wpan_phy);
142 			else
143 				tmp = NULL;
144 
145 			/* not wireless device -- return error */
146 			if (!tmp)
147 				return ERR_PTR(-EINVAL);
148 
149 			/* mismatch -- return error */
150 			if (rdev && tmp != rdev)
151 				return ERR_PTR(-EINVAL);
152 
153 			rdev = tmp;
154 		}
155 	}
156 
157 	if (!rdev)
158 		return ERR_PTR(-ENODEV);
159 
160 	if (netns != wpan_phy_net(&rdev->wpan_phy))
161 		return ERR_PTR(-ENODEV);
162 
163 	return rdev;
164 }
165 
166 /* This function returns a pointer to the driver
167  * that the genl_info item that is passed refers to.
168  *
169  * The result of this can be a PTR_ERR and hence must
170  * be checked with IS_ERR() for errors.
171  */
172 static struct cfg802154_registered_device *
173 cfg802154_get_dev_from_info(struct net *netns, struct genl_info *info)
174 {
175 	return __cfg802154_rdev_from_attrs(netns, info->attrs);
176 }
177 
178 /* policy for the attributes */
179 static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
180 	[NL802154_ATTR_WPAN_PHY] = { .type = NLA_U32 },
181 	[NL802154_ATTR_WPAN_PHY_NAME] = { .type = NLA_NUL_STRING,
182 					  .len = 20-1 },
183 
184 	[NL802154_ATTR_IFINDEX] = { .type = NLA_U32 },
185 	[NL802154_ATTR_IFTYPE] = { .type = NLA_U32 },
186 	[NL802154_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
187 
188 	[NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
189 
190 	[NL802154_ATTR_PAGE] = NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_PAGE),
191 	[NL802154_ATTR_CHANNEL] = NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_CHANNEL),
192 
193 	[NL802154_ATTR_TX_POWER] = { .type = NLA_S32, },
194 
195 	[NL802154_ATTR_CCA_MODE] = { .type = NLA_U32, },
196 	[NL802154_ATTR_CCA_OPT] = { .type = NLA_U32, },
197 	[NL802154_ATTR_CCA_ED_LEVEL] = { .type = NLA_S32, },
198 
199 	[NL802154_ATTR_SUPPORTED_CHANNEL] = { .type = NLA_U32, },
200 
201 	[NL802154_ATTR_PAN_ID] = { .type = NLA_U16, },
202 	[NL802154_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
203 	[NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16, },
204 
205 	[NL802154_ATTR_MIN_BE] = { .type = NLA_U8, },
206 	[NL802154_ATTR_MAX_BE] = { .type = NLA_U8, },
207 	[NL802154_ATTR_MAX_CSMA_BACKOFFS] = { .type = NLA_U8, },
208 
209 	[NL802154_ATTR_MAX_FRAME_RETRIES] = { .type = NLA_S8, },
210 
211 	[NL802154_ATTR_LBT_MODE] = { .type = NLA_U8, },
212 
213 	[NL802154_ATTR_WPAN_PHY_CAPS] = { .type = NLA_NESTED },
214 
215 	[NL802154_ATTR_SUPPORTED_COMMANDS] = { .type = NLA_NESTED },
216 
217 	[NL802154_ATTR_ACKREQ_DEFAULT] = { .type = NLA_U8 },
218 
219 	[NL802154_ATTR_PID] = { .type = NLA_U32 },
220 	[NL802154_ATTR_NETNS_FD] = { .type = NLA_U32 },
221 
222 	[NL802154_ATTR_COORDINATOR] = { .type = NLA_NESTED },
223 
224 	[NL802154_ATTR_SCAN_TYPE] =
225 		NLA_POLICY_RANGE(NLA_U8, NL802154_SCAN_ED, NL802154_SCAN_RIT_PASSIVE),
226 	[NL802154_ATTR_SCAN_CHANNELS] =
227 		NLA_POLICY_MASK(NLA_U32, GENMASK(IEEE802154_MAX_CHANNEL, 0)),
228 	[NL802154_ATTR_SCAN_PREAMBLE_CODES] = { .type = NLA_REJECT },
229 	[NL802154_ATTR_SCAN_MEAN_PRF] = { .type = NLA_REJECT },
230 	[NL802154_ATTR_SCAN_DURATION] =
231 		NLA_POLICY_MAX(NLA_U8, IEEE802154_MAX_SCAN_DURATION),
232 	[NL802154_ATTR_SCAN_DONE_REASON] =
233 		NLA_POLICY_RANGE(NLA_U8, NL802154_SCAN_DONE_REASON_FINISHED,
234 				 NL802154_SCAN_DONE_REASON_ABORTED),
235 	[NL802154_ATTR_BEACON_INTERVAL] =
236 		NLA_POLICY_MAX(NLA_U8, IEEE802154_ACTIVE_SCAN_DURATION),
237 
238 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
239 	[NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
240 	[NL802154_ATTR_SEC_OUT_LEVEL] = { .type = NLA_U32, },
241 	[NL802154_ATTR_SEC_OUT_KEY_ID] = { .type = NLA_NESTED, },
242 	[NL802154_ATTR_SEC_FRAME_COUNTER] = { .type = NLA_U32 },
243 
244 	[NL802154_ATTR_SEC_LEVEL] = { .type = NLA_NESTED },
245 	[NL802154_ATTR_SEC_DEVICE] = { .type = NLA_NESTED },
246 	[NL802154_ATTR_SEC_DEVKEY] = { .type = NLA_NESTED },
247 	[NL802154_ATTR_SEC_KEY] = { .type = NLA_NESTED },
248 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
249 };
250 
251 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
252 static int
253 nl802154_prepare_wpan_dev_dump(struct sk_buff *skb,
254 			       struct netlink_callback *cb,
255 			       struct cfg802154_registered_device **rdev,
256 			       struct wpan_dev **wpan_dev)
257 {
258 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
259 	int err;
260 
261 	rtnl_lock();
262 
263 	if (!cb->args[0]) {
264 		*wpan_dev = __cfg802154_wpan_dev_from_attrs(sock_net(skb->sk),
265 							    info->info.attrs);
266 		if (IS_ERR(*wpan_dev)) {
267 			err = PTR_ERR(*wpan_dev);
268 			goto out_unlock;
269 		}
270 		*rdev = wpan_phy_to_rdev((*wpan_dev)->wpan_phy);
271 		/* 0 is the first index - add 1 to parse only once */
272 		cb->args[0] = (*rdev)->wpan_phy_idx + 1;
273 		cb->args[1] = (*wpan_dev)->identifier;
274 	} else {
275 		/* subtract the 1 again here */
276 		struct wpan_phy *wpan_phy = wpan_phy_idx_to_wpan_phy(cb->args[0] - 1);
277 		struct wpan_dev *tmp;
278 
279 		if (!wpan_phy) {
280 			err = -ENODEV;
281 			goto out_unlock;
282 		}
283 		*rdev = wpan_phy_to_rdev(wpan_phy);
284 		*wpan_dev = NULL;
285 
286 		list_for_each_entry(tmp, &(*rdev)->wpan_dev_list, list) {
287 			if (tmp->identifier == cb->args[1]) {
288 				*wpan_dev = tmp;
289 				break;
290 			}
291 		}
292 
293 		if (!*wpan_dev) {
294 			err = -ENODEV;
295 			goto out_unlock;
296 		}
297 	}
298 
299 	return 0;
300  out_unlock:
301 	rtnl_unlock();
302 	return err;
303 }
304 
305 static void
306 nl802154_finish_wpan_dev_dump(struct cfg802154_registered_device *rdev)
307 {
308 	rtnl_unlock();
309 }
310 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
311 
312 /* message building helper */
313 static inline void *nl802154hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
314 				    int flags, u8 cmd)
315 {
316 	/* since there is no private header just add the generic one */
317 	return genlmsg_put(skb, portid, seq, &nl802154_fam, flags, cmd);
318 }
319 
320 static int
321 nl802154_put_flags(struct sk_buff *msg, int attr, u32 mask)
322 {
323 	struct nlattr *nl_flags = nla_nest_start_noflag(msg, attr);
324 	int i;
325 
326 	if (!nl_flags)
327 		return -ENOBUFS;
328 
329 	i = 0;
330 	while (mask) {
331 		if ((mask & 1) && nla_put_flag(msg, i))
332 			return -ENOBUFS;
333 
334 		mask >>= 1;
335 		i++;
336 	}
337 
338 	nla_nest_end(msg, nl_flags);
339 	return 0;
340 }
341 
342 static int
343 nl802154_send_wpan_phy_channels(struct cfg802154_registered_device *rdev,
344 				struct sk_buff *msg)
345 {
346 	struct nlattr *nl_page;
347 	unsigned long page;
348 
349 	nl_page = nla_nest_start_noflag(msg, NL802154_ATTR_CHANNELS_SUPPORTED);
350 	if (!nl_page)
351 		return -ENOBUFS;
352 
353 	for (page = 0; page <= IEEE802154_MAX_PAGE; page++) {
354 		if (nla_put_u32(msg, NL802154_ATTR_SUPPORTED_CHANNEL,
355 				rdev->wpan_phy.supported.channels[page]))
356 			return -ENOBUFS;
357 	}
358 	nla_nest_end(msg, nl_page);
359 
360 	return 0;
361 }
362 
363 static int
364 nl802154_put_capabilities(struct sk_buff *msg,
365 			  struct cfg802154_registered_device *rdev)
366 {
367 	const struct wpan_phy_supported *caps = &rdev->wpan_phy.supported;
368 	struct nlattr *nl_caps, *nl_channels;
369 	int i;
370 
371 	nl_caps = nla_nest_start_noflag(msg, NL802154_ATTR_WPAN_PHY_CAPS);
372 	if (!nl_caps)
373 		return -ENOBUFS;
374 
375 	nl_channels = nla_nest_start_noflag(msg, NL802154_CAP_ATTR_CHANNELS);
376 	if (!nl_channels)
377 		return -ENOBUFS;
378 
379 	for (i = 0; i <= IEEE802154_MAX_PAGE; i++) {
380 		if (caps->channels[i]) {
381 			if (nl802154_put_flags(msg, i, caps->channels[i]))
382 				return -ENOBUFS;
383 		}
384 	}
385 
386 	nla_nest_end(msg, nl_channels);
387 
388 	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
389 		struct nlattr *nl_ed_lvls;
390 
391 		nl_ed_lvls = nla_nest_start_noflag(msg,
392 						   NL802154_CAP_ATTR_CCA_ED_LEVELS);
393 		if (!nl_ed_lvls)
394 			return -ENOBUFS;
395 
396 		for (i = 0; i < caps->cca_ed_levels_size; i++) {
397 			if (nla_put_s32(msg, i, caps->cca_ed_levels[i]))
398 				return -ENOBUFS;
399 		}
400 
401 		nla_nest_end(msg, nl_ed_lvls);
402 	}
403 
404 	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
405 		struct nlattr *nl_tx_pwrs;
406 
407 		nl_tx_pwrs = nla_nest_start_noflag(msg,
408 						   NL802154_CAP_ATTR_TX_POWERS);
409 		if (!nl_tx_pwrs)
410 			return -ENOBUFS;
411 
412 		for (i = 0; i < caps->tx_powers_size; i++) {
413 			if (nla_put_s32(msg, i, caps->tx_powers[i]))
414 				return -ENOBUFS;
415 		}
416 
417 		nla_nest_end(msg, nl_tx_pwrs);
418 	}
419 
420 	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
421 		if (nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_MODES,
422 				       caps->cca_modes) ||
423 		    nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_OPTS,
424 				       caps->cca_opts))
425 			return -ENOBUFS;
426 	}
427 
428 	if (nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MINBE, caps->min_minbe) ||
429 	    nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MINBE, caps->max_minbe) ||
430 	    nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MAXBE, caps->min_maxbe) ||
431 	    nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MAXBE, caps->max_maxbe) ||
432 	    nla_put_u8(msg, NL802154_CAP_ATTR_MIN_CSMA_BACKOFFS,
433 		       caps->min_csma_backoffs) ||
434 	    nla_put_u8(msg, NL802154_CAP_ATTR_MAX_CSMA_BACKOFFS,
435 		       caps->max_csma_backoffs) ||
436 	    nla_put_s8(msg, NL802154_CAP_ATTR_MIN_FRAME_RETRIES,
437 		       caps->min_frame_retries) ||
438 	    nla_put_s8(msg, NL802154_CAP_ATTR_MAX_FRAME_RETRIES,
439 		       caps->max_frame_retries) ||
440 	    nl802154_put_flags(msg, NL802154_CAP_ATTR_IFTYPES,
441 			       caps->iftypes) ||
442 	    nla_put_u32(msg, NL802154_CAP_ATTR_LBT, caps->lbt))
443 		return -ENOBUFS;
444 
445 	nla_nest_end(msg, nl_caps);
446 
447 	return 0;
448 }
449 
450 static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev,
451 				  enum nl802154_commands cmd,
452 				  struct sk_buff *msg, u32 portid, u32 seq,
453 				  int flags)
454 {
455 	struct nlattr *nl_cmds;
456 	void *hdr;
457 	int i;
458 
459 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
460 	if (!hdr)
461 		return -ENOBUFS;
462 
463 	if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
464 	    nla_put_string(msg, NL802154_ATTR_WPAN_PHY_NAME,
465 			   wpan_phy_name(&rdev->wpan_phy)) ||
466 	    nla_put_u32(msg, NL802154_ATTR_GENERATION,
467 			cfg802154_rdev_list_generation))
468 		goto nla_put_failure;
469 
470 	if (cmd != NL802154_CMD_NEW_WPAN_PHY)
471 		goto finish;
472 
473 	/* DUMP PHY PIB */
474 
475 	/* current channel settings */
476 	if (nla_put_u8(msg, NL802154_ATTR_PAGE,
477 		       rdev->wpan_phy.current_page) ||
478 	    nla_put_u8(msg, NL802154_ATTR_CHANNEL,
479 		       rdev->wpan_phy.current_channel))
480 		goto nla_put_failure;
481 
482 	/* TODO remove this behaviour, we still keep support it for a while
483 	 * so users can change the behaviour to the new one.
484 	 */
485 	if (nl802154_send_wpan_phy_channels(rdev, msg))
486 		goto nla_put_failure;
487 
488 	/* cca mode */
489 	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
490 		if (nla_put_u32(msg, NL802154_ATTR_CCA_MODE,
491 				rdev->wpan_phy.cca.mode))
492 			goto nla_put_failure;
493 
494 		if (rdev->wpan_phy.cca.mode == NL802154_CCA_ENERGY_CARRIER) {
495 			if (nla_put_u32(msg, NL802154_ATTR_CCA_OPT,
496 					rdev->wpan_phy.cca.opt))
497 				goto nla_put_failure;
498 		}
499 	}
500 
501 	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
502 		if (nla_put_s32(msg, NL802154_ATTR_TX_POWER,
503 				rdev->wpan_phy.transmit_power))
504 			goto nla_put_failure;
505 	}
506 
507 	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
508 		if (nla_put_s32(msg, NL802154_ATTR_CCA_ED_LEVEL,
509 				rdev->wpan_phy.cca_ed_level))
510 			goto nla_put_failure;
511 	}
512 
513 	if (nl802154_put_capabilities(msg, rdev))
514 		goto nla_put_failure;
515 
516 	nl_cmds = nla_nest_start_noflag(msg, NL802154_ATTR_SUPPORTED_COMMANDS);
517 	if (!nl_cmds)
518 		goto nla_put_failure;
519 
520 	i = 0;
521 #define CMD(op, n)							\
522 	do {								\
523 		if (rdev->ops->op) {					\
524 			i++;						\
525 			if (nla_put_u32(msg, i, NL802154_CMD_ ## n))	\
526 				goto nla_put_failure;			\
527 		}							\
528 	} while (0)
529 
530 	CMD(add_virtual_intf, NEW_INTERFACE);
531 	CMD(del_virtual_intf, DEL_INTERFACE);
532 	CMD(set_channel, SET_CHANNEL);
533 	CMD(set_pan_id, SET_PAN_ID);
534 	CMD(set_short_addr, SET_SHORT_ADDR);
535 	CMD(set_backoff_exponent, SET_BACKOFF_EXPONENT);
536 	CMD(set_max_csma_backoffs, SET_MAX_CSMA_BACKOFFS);
537 	CMD(set_max_frame_retries, SET_MAX_FRAME_RETRIES);
538 	CMD(set_lbt_mode, SET_LBT_MODE);
539 	CMD(set_ackreq_default, SET_ACKREQ_DEFAULT);
540 
541 	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER)
542 		CMD(set_tx_power, SET_TX_POWER);
543 
544 	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL)
545 		CMD(set_cca_ed_level, SET_CCA_ED_LEVEL);
546 
547 	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE)
548 		CMD(set_cca_mode, SET_CCA_MODE);
549 
550 #undef CMD
551 	nla_nest_end(msg, nl_cmds);
552 
553 finish:
554 	genlmsg_end(msg, hdr);
555 	return 0;
556 
557 nla_put_failure:
558 	genlmsg_cancel(msg, hdr);
559 	return -EMSGSIZE;
560 }
561 
562 struct nl802154_dump_wpan_phy_state {
563 	s64 filter_wpan_phy;
564 	long start;
565 
566 };
567 
568 static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb,
569 					struct netlink_callback *cb,
570 					struct nl802154_dump_wpan_phy_state *state)
571 {
572 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
573 	struct nlattr **tb = info->info.attrs;
574 
575 	if (tb[NL802154_ATTR_WPAN_PHY])
576 		state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]);
577 	if (tb[NL802154_ATTR_WPAN_DEV])
578 		state->filter_wpan_phy = nla_get_u64(tb[NL802154_ATTR_WPAN_DEV]) >> 32;
579 	if (tb[NL802154_ATTR_IFINDEX]) {
580 		struct net_device *netdev;
581 		struct cfg802154_registered_device *rdev;
582 		int ifidx = nla_get_u32(tb[NL802154_ATTR_IFINDEX]);
583 
584 		netdev = __dev_get_by_index(&init_net, ifidx);
585 		if (!netdev)
586 			return -ENODEV;
587 		if (netdev->ieee802154_ptr) {
588 			rdev = wpan_phy_to_rdev(
589 					netdev->ieee802154_ptr->wpan_phy);
590 			state->filter_wpan_phy = rdev->wpan_phy_idx;
591 		}
592 	}
593 
594 	return 0;
595 }
596 
597 static int
598 nl802154_dump_wpan_phy(struct sk_buff *skb, struct netlink_callback *cb)
599 {
600 	int idx = 0, ret;
601 	struct nl802154_dump_wpan_phy_state *state = (void *)cb->args[0];
602 	struct cfg802154_registered_device *rdev;
603 
604 	rtnl_lock();
605 	if (!state) {
606 		state = kzalloc(sizeof(*state), GFP_KERNEL);
607 		if (!state) {
608 			rtnl_unlock();
609 			return -ENOMEM;
610 		}
611 		state->filter_wpan_phy = -1;
612 		ret = nl802154_dump_wpan_phy_parse(skb, cb, state);
613 		if (ret) {
614 			kfree(state);
615 			rtnl_unlock();
616 			return ret;
617 		}
618 		cb->args[0] = (long)state;
619 	}
620 
621 	list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
622 		if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
623 			continue;
624 		if (++idx <= state->start)
625 			continue;
626 		if (state->filter_wpan_phy != -1 &&
627 		    state->filter_wpan_phy != rdev->wpan_phy_idx)
628 			continue;
629 		/* attempt to fit multiple wpan_phy data chunks into the skb */
630 		ret = nl802154_send_wpan_phy(rdev,
631 					     NL802154_CMD_NEW_WPAN_PHY,
632 					     skb,
633 					     NETLINK_CB(cb->skb).portid,
634 					     cb->nlh->nlmsg_seq, NLM_F_MULTI);
635 		if (ret < 0) {
636 			if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
637 			    !skb->len && cb->min_dump_alloc < 4096) {
638 				cb->min_dump_alloc = 4096;
639 				rtnl_unlock();
640 				return 1;
641 			}
642 			idx--;
643 			break;
644 		}
645 		break;
646 	}
647 	rtnl_unlock();
648 
649 	state->start = idx;
650 
651 	return skb->len;
652 }
653 
654 static int nl802154_dump_wpan_phy_done(struct netlink_callback *cb)
655 {
656 	kfree((void *)cb->args[0]);
657 	return 0;
658 }
659 
660 static int nl802154_get_wpan_phy(struct sk_buff *skb, struct genl_info *info)
661 {
662 	struct sk_buff *msg;
663 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
664 
665 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
666 	if (!msg)
667 		return -ENOMEM;
668 
669 	if (nl802154_send_wpan_phy(rdev, NL802154_CMD_NEW_WPAN_PHY, msg,
670 				   info->snd_portid, info->snd_seq, 0) < 0) {
671 		nlmsg_free(msg);
672 		return -ENOBUFS;
673 	}
674 
675 	return genlmsg_reply(msg, info);
676 }
677 
678 static inline u64 wpan_dev_id(struct wpan_dev *wpan_dev)
679 {
680 	return (u64)wpan_dev->identifier |
681 	       ((u64)wpan_phy_to_rdev(wpan_dev->wpan_phy)->wpan_phy_idx << 32);
682 }
683 
684 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
685 #include <net/ieee802154_netdev.h>
686 
687 static int
688 ieee802154_llsec_send_key_id(struct sk_buff *msg,
689 			     const struct ieee802154_llsec_key_id *desc)
690 {
691 	struct nlattr *nl_dev_addr;
692 
693 	if (nla_put_u32(msg, NL802154_KEY_ID_ATTR_MODE, desc->mode))
694 		return -ENOBUFS;
695 
696 	switch (desc->mode) {
697 	case NL802154_KEY_ID_MODE_IMPLICIT:
698 		nl_dev_addr = nla_nest_start_noflag(msg,
699 						    NL802154_KEY_ID_ATTR_IMPLICIT);
700 		if (!nl_dev_addr)
701 			return -ENOBUFS;
702 
703 		if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_PAN_ID,
704 				 desc->device_addr.pan_id) ||
705 		    nla_put_u32(msg,  NL802154_DEV_ADDR_ATTR_MODE,
706 				desc->device_addr.mode))
707 			return -ENOBUFS;
708 
709 		switch (desc->device_addr.mode) {
710 		case NL802154_DEV_ADDR_SHORT:
711 			if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_SHORT,
712 					 desc->device_addr.short_addr))
713 				return -ENOBUFS;
714 			break;
715 		case NL802154_DEV_ADDR_EXTENDED:
716 			if (nla_put_le64(msg, NL802154_DEV_ADDR_ATTR_EXTENDED,
717 					 desc->device_addr.extended_addr,
718 					 NL802154_DEV_ADDR_ATTR_PAD))
719 				return -ENOBUFS;
720 			break;
721 		default:
722 			/* userspace should handle unknown */
723 			break;
724 		}
725 
726 		nla_nest_end(msg, nl_dev_addr);
727 		break;
728 	case NL802154_KEY_ID_MODE_INDEX:
729 		break;
730 	case NL802154_KEY_ID_MODE_INDEX_SHORT:
731 		/* TODO renmae short_source? */
732 		if (nla_put_le32(msg, NL802154_KEY_ID_ATTR_SOURCE_SHORT,
733 				 desc->short_source))
734 			return -ENOBUFS;
735 		break;
736 	case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
737 		if (nla_put_le64(msg, NL802154_KEY_ID_ATTR_SOURCE_EXTENDED,
738 				 desc->extended_source,
739 				 NL802154_KEY_ID_ATTR_PAD))
740 			return -ENOBUFS;
741 		break;
742 	default:
743 		/* userspace should handle unknown */
744 		break;
745 	}
746 
747 	/* TODO key_id to key_idx ? Check naming */
748 	if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
749 		if (nla_put_u8(msg, NL802154_KEY_ID_ATTR_INDEX, desc->id))
750 			return -ENOBUFS;
751 	}
752 
753 	return 0;
754 }
755 
756 static int nl802154_get_llsec_params(struct sk_buff *msg,
757 				     struct cfg802154_registered_device *rdev,
758 				     struct wpan_dev *wpan_dev)
759 {
760 	struct nlattr *nl_key_id;
761 	struct ieee802154_llsec_params params;
762 	int ret;
763 
764 	ret = rdev_get_llsec_params(rdev, wpan_dev, &params);
765 	if (ret < 0)
766 		return ret;
767 
768 	if (nla_put_u8(msg, NL802154_ATTR_SEC_ENABLED, params.enabled) ||
769 	    nla_put_u32(msg, NL802154_ATTR_SEC_OUT_LEVEL, params.out_level) ||
770 	    nla_put_be32(msg, NL802154_ATTR_SEC_FRAME_COUNTER,
771 			 params.frame_counter))
772 		return -ENOBUFS;
773 
774 	nl_key_id = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_OUT_KEY_ID);
775 	if (!nl_key_id)
776 		return -ENOBUFS;
777 
778 	ret = ieee802154_llsec_send_key_id(msg, &params.out_key);
779 	if (ret < 0)
780 		return ret;
781 
782 	nla_nest_end(msg, nl_key_id);
783 
784 	return 0;
785 }
786 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
787 
788 static int
789 nl802154_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
790 		    struct cfg802154_registered_device *rdev,
791 		    struct wpan_dev *wpan_dev)
792 {
793 	struct net_device *dev = wpan_dev->netdev;
794 	void *hdr;
795 
796 	hdr = nl802154hdr_put(msg, portid, seq, flags,
797 			      NL802154_CMD_NEW_INTERFACE);
798 	if (!hdr)
799 		return -1;
800 
801 	if (dev &&
802 	    (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex) ||
803 	     nla_put_string(msg, NL802154_ATTR_IFNAME, dev->name)))
804 		goto nla_put_failure;
805 
806 	if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
807 	    nla_put_u32(msg, NL802154_ATTR_IFTYPE, wpan_dev->iftype) ||
808 	    nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
809 			      wpan_dev_id(wpan_dev), NL802154_ATTR_PAD) ||
810 	    nla_put_u32(msg, NL802154_ATTR_GENERATION,
811 			rdev->devlist_generation ^
812 			(cfg802154_rdev_list_generation << 2)))
813 		goto nla_put_failure;
814 
815 	/* address settings */
816 	if (nla_put_le64(msg, NL802154_ATTR_EXTENDED_ADDR,
817 			 wpan_dev->extended_addr,
818 			 NL802154_ATTR_PAD) ||
819 	    nla_put_le16(msg, NL802154_ATTR_SHORT_ADDR,
820 			 wpan_dev->short_addr) ||
821 	    nla_put_le16(msg, NL802154_ATTR_PAN_ID, wpan_dev->pan_id))
822 		goto nla_put_failure;
823 
824 	/* ARET handling */
825 	if (nla_put_s8(msg, NL802154_ATTR_MAX_FRAME_RETRIES,
826 		       wpan_dev->frame_retries) ||
827 	    nla_put_u8(msg, NL802154_ATTR_MAX_BE, wpan_dev->max_be) ||
828 	    nla_put_u8(msg, NL802154_ATTR_MAX_CSMA_BACKOFFS,
829 		       wpan_dev->csma_retries) ||
830 	    nla_put_u8(msg, NL802154_ATTR_MIN_BE, wpan_dev->min_be))
831 		goto nla_put_failure;
832 
833 	/* listen before transmit */
834 	if (nla_put_u8(msg, NL802154_ATTR_LBT_MODE, wpan_dev->lbt))
835 		goto nla_put_failure;
836 
837 	/* ackreq default behaviour */
838 	if (nla_put_u8(msg, NL802154_ATTR_ACKREQ_DEFAULT, wpan_dev->ackreq))
839 		goto nla_put_failure;
840 
841 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
842 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
843 		goto out;
844 
845 	if (nl802154_get_llsec_params(msg, rdev, wpan_dev) < 0)
846 		goto nla_put_failure;
847 
848 out:
849 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
850 
851 	genlmsg_end(msg, hdr);
852 	return 0;
853 
854 nla_put_failure:
855 	genlmsg_cancel(msg, hdr);
856 	return -EMSGSIZE;
857 }
858 
859 static int
860 nl802154_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
861 {
862 	int wp_idx = 0;
863 	int if_idx = 0;
864 	int wp_start = cb->args[0];
865 	int if_start = cb->args[1];
866 	struct cfg802154_registered_device *rdev;
867 	struct wpan_dev *wpan_dev;
868 
869 	rtnl_lock();
870 	list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
871 		if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
872 			continue;
873 		if (wp_idx < wp_start) {
874 			wp_idx++;
875 			continue;
876 		}
877 		if_idx = 0;
878 
879 		list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
880 			if (if_idx < if_start) {
881 				if_idx++;
882 				continue;
883 			}
884 			if (nl802154_send_iface(skb, NETLINK_CB(cb->skb).portid,
885 						cb->nlh->nlmsg_seq, NLM_F_MULTI,
886 						rdev, wpan_dev) < 0) {
887 				goto out;
888 			}
889 			if_idx++;
890 		}
891 
892 		wp_idx++;
893 	}
894 out:
895 	rtnl_unlock();
896 
897 	cb->args[0] = wp_idx;
898 	cb->args[1] = if_idx;
899 
900 	return skb->len;
901 }
902 
903 static int nl802154_get_interface(struct sk_buff *skb, struct genl_info *info)
904 {
905 	struct sk_buff *msg;
906 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
907 	struct wpan_dev *wdev = info->user_ptr[1];
908 
909 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
910 	if (!msg)
911 		return -ENOMEM;
912 
913 	if (nl802154_send_iface(msg, info->snd_portid, info->snd_seq, 0,
914 				rdev, wdev) < 0) {
915 		nlmsg_free(msg);
916 		return -ENOBUFS;
917 	}
918 
919 	return genlmsg_reply(msg, info);
920 }
921 
922 static int nl802154_new_interface(struct sk_buff *skb, struct genl_info *info)
923 {
924 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
925 	enum nl802154_iftype type = NL802154_IFTYPE_UNSPEC;
926 	__le64 extended_addr = cpu_to_le64(0x0000000000000000ULL);
927 
928 	/* TODO avoid failing a new interface
929 	 * creation due to pending removal?
930 	 */
931 
932 	if (!info->attrs[NL802154_ATTR_IFNAME])
933 		return -EINVAL;
934 
935 	if (info->attrs[NL802154_ATTR_IFTYPE]) {
936 		type = nla_get_u32(info->attrs[NL802154_ATTR_IFTYPE]);
937 		if (type > NL802154_IFTYPE_MAX ||
938 		    !(rdev->wpan_phy.supported.iftypes & BIT(type)))
939 			return -EINVAL;
940 	}
941 
942 	if (info->attrs[NL802154_ATTR_EXTENDED_ADDR])
943 		extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
944 
945 	if (!rdev->ops->add_virtual_intf)
946 		return -EOPNOTSUPP;
947 
948 	return rdev_add_virtual_intf(rdev,
949 				     nla_data(info->attrs[NL802154_ATTR_IFNAME]),
950 				     NET_NAME_USER, type, extended_addr);
951 }
952 
953 static int nl802154_del_interface(struct sk_buff *skb, struct genl_info *info)
954 {
955 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
956 	struct wpan_dev *wpan_dev = info->user_ptr[1];
957 
958 	if (!rdev->ops->del_virtual_intf)
959 		return -EOPNOTSUPP;
960 
961 	/* If we remove a wpan device without a netdev then clear
962 	 * user_ptr[1] so that nl802154_post_doit won't dereference it
963 	 * to check if it needs to do dev_put(). Otherwise it crashes
964 	 * since the wpan_dev has been freed, unlike with a netdev where
965 	 * we need the dev_put() for the netdev to really be freed.
966 	 */
967 	if (!wpan_dev->netdev)
968 		info->user_ptr[1] = NULL;
969 
970 	return rdev_del_virtual_intf(rdev, wpan_dev);
971 }
972 
973 static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info)
974 {
975 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
976 	u8 channel, page;
977 
978 	if (!info->attrs[NL802154_ATTR_PAGE] ||
979 	    !info->attrs[NL802154_ATTR_CHANNEL])
980 		return -EINVAL;
981 
982 	page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
983 	channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]);
984 
985 	/* check 802.15.4 constraints */
986 	if (!ieee802154_chan_is_valid(&rdev->wpan_phy, page, channel))
987 		return -EINVAL;
988 
989 	return rdev_set_channel(rdev, page, channel);
990 }
991 
992 static int nl802154_set_cca_mode(struct sk_buff *skb, struct genl_info *info)
993 {
994 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
995 	struct wpan_phy_cca cca;
996 
997 	if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE))
998 		return -EOPNOTSUPP;
999 
1000 	if (!info->attrs[NL802154_ATTR_CCA_MODE])
1001 		return -EINVAL;
1002 
1003 	cca.mode = nla_get_u32(info->attrs[NL802154_ATTR_CCA_MODE]);
1004 	/* checking 802.15.4 constraints */
1005 	if (cca.mode < NL802154_CCA_ENERGY ||
1006 	    cca.mode > NL802154_CCA_ATTR_MAX ||
1007 	    !(rdev->wpan_phy.supported.cca_modes & BIT(cca.mode)))
1008 		return -EINVAL;
1009 
1010 	if (cca.mode == NL802154_CCA_ENERGY_CARRIER) {
1011 		if (!info->attrs[NL802154_ATTR_CCA_OPT])
1012 			return -EINVAL;
1013 
1014 		cca.opt = nla_get_u32(info->attrs[NL802154_ATTR_CCA_OPT]);
1015 		if (cca.opt > NL802154_CCA_OPT_ATTR_MAX ||
1016 		    !(rdev->wpan_phy.supported.cca_opts & BIT(cca.opt)))
1017 			return -EINVAL;
1018 	}
1019 
1020 	return rdev_set_cca_mode(rdev, &cca);
1021 }
1022 
1023 static int nl802154_set_cca_ed_level(struct sk_buff *skb, struct genl_info *info)
1024 {
1025 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1026 	s32 ed_level;
1027 	int i;
1028 
1029 	if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL))
1030 		return -EOPNOTSUPP;
1031 
1032 	if (!info->attrs[NL802154_ATTR_CCA_ED_LEVEL])
1033 		return -EINVAL;
1034 
1035 	ed_level = nla_get_s32(info->attrs[NL802154_ATTR_CCA_ED_LEVEL]);
1036 
1037 	for (i = 0; i < rdev->wpan_phy.supported.cca_ed_levels_size; i++) {
1038 		if (ed_level == rdev->wpan_phy.supported.cca_ed_levels[i])
1039 			return rdev_set_cca_ed_level(rdev, ed_level);
1040 	}
1041 
1042 	return -EINVAL;
1043 }
1044 
1045 static int nl802154_set_tx_power(struct sk_buff *skb, struct genl_info *info)
1046 {
1047 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1048 	s32 power;
1049 	int i;
1050 
1051 	if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER))
1052 		return -EOPNOTSUPP;
1053 
1054 	if (!info->attrs[NL802154_ATTR_TX_POWER])
1055 		return -EINVAL;
1056 
1057 	power = nla_get_s32(info->attrs[NL802154_ATTR_TX_POWER]);
1058 
1059 	for (i = 0; i < rdev->wpan_phy.supported.tx_powers_size; i++) {
1060 		if (power == rdev->wpan_phy.supported.tx_powers[i])
1061 			return rdev_set_tx_power(rdev, power);
1062 	}
1063 
1064 	return -EINVAL;
1065 }
1066 
1067 static int nl802154_set_pan_id(struct sk_buff *skb, struct genl_info *info)
1068 {
1069 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1070 	struct net_device *dev = info->user_ptr[1];
1071 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1072 	__le16 pan_id;
1073 
1074 	/* conflict here while tx/rx calls */
1075 	if (netif_running(dev))
1076 		return -EBUSY;
1077 
1078 	if (wpan_dev->lowpan_dev) {
1079 		if (netif_running(wpan_dev->lowpan_dev))
1080 			return -EBUSY;
1081 	}
1082 
1083 	/* don't change address fields on monitor */
1084 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1085 	    !info->attrs[NL802154_ATTR_PAN_ID])
1086 		return -EINVAL;
1087 
1088 	pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
1089 
1090 	return rdev_set_pan_id(rdev, wpan_dev, pan_id);
1091 }
1092 
1093 static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info)
1094 {
1095 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1096 	struct net_device *dev = info->user_ptr[1];
1097 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1098 	__le16 short_addr;
1099 
1100 	/* conflict here while tx/rx calls */
1101 	if (netif_running(dev))
1102 		return -EBUSY;
1103 
1104 	if (wpan_dev->lowpan_dev) {
1105 		if (netif_running(wpan_dev->lowpan_dev))
1106 			return -EBUSY;
1107 	}
1108 
1109 	/* don't change address fields on monitor */
1110 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1111 	    !info->attrs[NL802154_ATTR_SHORT_ADDR])
1112 		return -EINVAL;
1113 
1114 	short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
1115 
1116 	/* TODO
1117 	 * I am not sure about to check here on broadcast short_addr.
1118 	 * Broadcast is a valid setting, comment from 802.15.4:
1119 	 * A value of 0xfffe indicates that the device has
1120 	 * associated but has not been allocated an address. A
1121 	 * value of 0xffff indicates that the device does not
1122 	 * have a short address.
1123 	 *
1124 	 * I think we should allow to set these settings but
1125 	 * don't allow to allow socket communication with it.
1126 	 */
1127 	if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
1128 	    short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
1129 		return -EINVAL;
1130 
1131 	return rdev_set_short_addr(rdev, wpan_dev, short_addr);
1132 }
1133 
1134 static int
1135 nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info)
1136 {
1137 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1138 	struct net_device *dev = info->user_ptr[1];
1139 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1140 	u8 min_be, max_be;
1141 
1142 	/* should be set on netif open inside phy settings */
1143 	if (netif_running(dev))
1144 		return -EBUSY;
1145 
1146 	if (!info->attrs[NL802154_ATTR_MIN_BE] ||
1147 	    !info->attrs[NL802154_ATTR_MAX_BE])
1148 		return -EINVAL;
1149 
1150 	min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]);
1151 	max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]);
1152 
1153 	/* check 802.15.4 constraints */
1154 	if (min_be < rdev->wpan_phy.supported.min_minbe ||
1155 	    min_be > rdev->wpan_phy.supported.max_minbe ||
1156 	    max_be < rdev->wpan_phy.supported.min_maxbe ||
1157 	    max_be > rdev->wpan_phy.supported.max_maxbe ||
1158 	    min_be > max_be)
1159 		return -EINVAL;
1160 
1161 	return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
1162 }
1163 
1164 static int
1165 nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info)
1166 {
1167 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1168 	struct net_device *dev = info->user_ptr[1];
1169 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1170 	u8 max_csma_backoffs;
1171 
1172 	/* conflict here while other running iface settings */
1173 	if (netif_running(dev))
1174 		return -EBUSY;
1175 
1176 	if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS])
1177 		return -EINVAL;
1178 
1179 	max_csma_backoffs = nla_get_u8(
1180 			info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]);
1181 
1182 	/* check 802.15.4 constraints */
1183 	if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs ||
1184 	    max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs)
1185 		return -EINVAL;
1186 
1187 	return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
1188 }
1189 
1190 static int
1191 nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info)
1192 {
1193 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1194 	struct net_device *dev = info->user_ptr[1];
1195 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1196 	s8 max_frame_retries;
1197 
1198 	if (netif_running(dev))
1199 		return -EBUSY;
1200 
1201 	if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES])
1202 		return -EINVAL;
1203 
1204 	max_frame_retries = nla_get_s8(
1205 			info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]);
1206 
1207 	/* check 802.15.4 constraints */
1208 	if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries ||
1209 	    max_frame_retries > rdev->wpan_phy.supported.max_frame_retries)
1210 		return -EINVAL;
1211 
1212 	return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
1213 }
1214 
1215 static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info)
1216 {
1217 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1218 	struct net_device *dev = info->user_ptr[1];
1219 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1220 	int mode;
1221 
1222 	if (netif_running(dev))
1223 		return -EBUSY;
1224 
1225 	if (!info->attrs[NL802154_ATTR_LBT_MODE])
1226 		return -EINVAL;
1227 
1228 	mode = nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
1229 
1230 	if (mode != 0 && mode != 1)
1231 		return -EINVAL;
1232 
1233 	if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt))
1234 		return -EINVAL;
1235 
1236 	return rdev_set_lbt_mode(rdev, wpan_dev, mode);
1237 }
1238 
1239 static int
1240 nl802154_set_ackreq_default(struct sk_buff *skb, struct genl_info *info)
1241 {
1242 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1243 	struct net_device *dev = info->user_ptr[1];
1244 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1245 	int ackreq;
1246 
1247 	if (netif_running(dev))
1248 		return -EBUSY;
1249 
1250 	if (!info->attrs[NL802154_ATTR_ACKREQ_DEFAULT])
1251 		return -EINVAL;
1252 
1253 	ackreq = nla_get_u8(info->attrs[NL802154_ATTR_ACKREQ_DEFAULT]);
1254 
1255 	if (ackreq != 0 && ackreq != 1)
1256 		return -EINVAL;
1257 
1258 	return rdev_set_ackreq_default(rdev, wpan_dev, ackreq);
1259 }
1260 
1261 static int nl802154_wpan_phy_netns(struct sk_buff *skb, struct genl_info *info)
1262 {
1263 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1264 	struct net *net;
1265 	int err;
1266 
1267 	if (info->attrs[NL802154_ATTR_PID]) {
1268 		u32 pid = nla_get_u32(info->attrs[NL802154_ATTR_PID]);
1269 
1270 		net = get_net_ns_by_pid(pid);
1271 	} else if (info->attrs[NL802154_ATTR_NETNS_FD]) {
1272 		u32 fd = nla_get_u32(info->attrs[NL802154_ATTR_NETNS_FD]);
1273 
1274 		net = get_net_ns_by_fd(fd);
1275 	} else {
1276 		return -EINVAL;
1277 	}
1278 
1279 	if (IS_ERR(net))
1280 		return PTR_ERR(net);
1281 
1282 	err = 0;
1283 
1284 	/* check if anything to do */
1285 	if (!net_eq(wpan_phy_net(&rdev->wpan_phy), net))
1286 		err = cfg802154_switch_netns(rdev, net);
1287 
1288 	put_net(net);
1289 	return err;
1290 }
1291 
1292 static int nl802154_prep_scan_event_msg(struct sk_buff *msg,
1293 					struct cfg802154_registered_device *rdev,
1294 					struct wpan_dev *wpan_dev,
1295 					u32 portid, u32 seq, int flags, u8 cmd,
1296 					struct ieee802154_coord_desc *desc)
1297 {
1298 	struct nlattr *nla;
1299 	void *hdr;
1300 
1301 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1302 	if (!hdr)
1303 		return -ENOBUFS;
1304 
1305 	if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx))
1306 		goto nla_put_failure;
1307 
1308 	if (wpan_dev->netdev &&
1309 	    nla_put_u32(msg, NL802154_ATTR_IFINDEX, wpan_dev->netdev->ifindex))
1310 		goto nla_put_failure;
1311 
1312 	if (nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
1313 			      wpan_dev_id(wpan_dev), NL802154_ATTR_PAD))
1314 		goto nla_put_failure;
1315 
1316 	nla = nla_nest_start_noflag(msg, NL802154_ATTR_COORDINATOR);
1317 	if (!nla)
1318 		goto nla_put_failure;
1319 
1320 	if (nla_put(msg, NL802154_COORD_PANID, IEEE802154_PAN_ID_LEN,
1321 		    &desc->addr.pan_id))
1322 		goto nla_put_failure;
1323 
1324 	if (desc->addr.mode == IEEE802154_ADDR_SHORT) {
1325 		if (nla_put(msg, NL802154_COORD_ADDR,
1326 			    IEEE802154_SHORT_ADDR_LEN,
1327 			    &desc->addr.short_addr))
1328 			goto nla_put_failure;
1329 	} else {
1330 		if (nla_put(msg, NL802154_COORD_ADDR,
1331 			    IEEE802154_EXTENDED_ADDR_LEN,
1332 			    &desc->addr.extended_addr))
1333 			goto nla_put_failure;
1334 	}
1335 
1336 	if (nla_put_u8(msg, NL802154_COORD_CHANNEL, desc->channel))
1337 		goto nla_put_failure;
1338 
1339 	if (nla_put_u8(msg, NL802154_COORD_PAGE, desc->page))
1340 		goto nla_put_failure;
1341 
1342 	if (nla_put_u16(msg, NL802154_COORD_SUPERFRAME_SPEC,
1343 			desc->superframe_spec))
1344 		goto nla_put_failure;
1345 
1346 	if (nla_put_u8(msg, NL802154_COORD_LINK_QUALITY, desc->link_quality))
1347 		goto nla_put_failure;
1348 
1349 	if (desc->gts_permit && nla_put_flag(msg, NL802154_COORD_GTS_PERMIT))
1350 		goto nla_put_failure;
1351 
1352 	/* TODO: NL802154_COORD_PAYLOAD_DATA if any */
1353 
1354 	nla_nest_end(msg, nla);
1355 
1356 	genlmsg_end(msg, hdr);
1357 
1358 	return 0;
1359 
1360  nla_put_failure:
1361 	genlmsg_cancel(msg, hdr);
1362 
1363 	return -EMSGSIZE;
1364 }
1365 
1366 int nl802154_scan_event(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
1367 			struct ieee802154_coord_desc *desc)
1368 {
1369 	struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(wpan_phy);
1370 	struct sk_buff *msg;
1371 	int ret;
1372 
1373 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1374 	if (!msg)
1375 		return -ENOMEM;
1376 
1377 	ret = nl802154_prep_scan_event_msg(msg, rdev, wpan_dev, 0, 0, 0,
1378 					   NL802154_CMD_SCAN_EVENT,
1379 					   desc);
1380 	if (ret < 0) {
1381 		nlmsg_free(msg);
1382 		return ret;
1383 	}
1384 
1385 	return genlmsg_multicast_netns(&nl802154_fam, wpan_phy_net(wpan_phy),
1386 				       msg, 0, NL802154_MCGRP_SCAN, GFP_ATOMIC);
1387 }
1388 EXPORT_SYMBOL_GPL(nl802154_scan_event);
1389 
1390 static int nl802154_trigger_scan(struct sk_buff *skb, struct genl_info *info)
1391 {
1392 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1393 	struct net_device *dev = info->user_ptr[1];
1394 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1395 	struct wpan_phy *wpan_phy = &rdev->wpan_phy;
1396 	struct cfg802154_scan_request *request;
1397 	u8 type;
1398 	int err;
1399 
1400 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1401 		NL_SET_ERR_MSG(info->extack, "Monitors are not allowed to perform scans");
1402 		return -EOPNOTSUPP;
1403 	}
1404 
1405 	if (!info->attrs[NL802154_ATTR_SCAN_TYPE]) {
1406 		NL_SET_ERR_MSG(info->extack, "Malformed request, missing scan type");
1407 		return -EINVAL;
1408 	}
1409 
1410 	if (wpan_phy->flags & WPAN_PHY_FLAG_DATAGRAMS_ONLY) {
1411 		NL_SET_ERR_MSG(info->extack, "PHY only supports datagrams");
1412 		return -EOPNOTSUPP;
1413 	}
1414 
1415 	request = kzalloc(sizeof(*request), GFP_KERNEL);
1416 	if (!request)
1417 		return -ENOMEM;
1418 
1419 	request->wpan_dev = wpan_dev;
1420 	request->wpan_phy = wpan_phy;
1421 
1422 	type = nla_get_u8(info->attrs[NL802154_ATTR_SCAN_TYPE]);
1423 	switch (type) {
1424 	case NL802154_SCAN_ACTIVE:
1425 	case NL802154_SCAN_PASSIVE:
1426 		request->type = type;
1427 		break;
1428 	default:
1429 		NL_SET_ERR_MSG_FMT(info->extack, "Unsupported scan type: %d", type);
1430 		err = -EINVAL;
1431 		goto free_request;
1432 	}
1433 
1434 	/* Use current page by default */
1435 	if (info->attrs[NL802154_ATTR_PAGE])
1436 		request->page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
1437 	else
1438 		request->page = wpan_phy->current_page;
1439 
1440 	/* Scan all supported channels by default */
1441 	if (info->attrs[NL802154_ATTR_SCAN_CHANNELS])
1442 		request->channels = nla_get_u32(info->attrs[NL802154_ATTR_SCAN_CHANNELS]);
1443 	else
1444 		request->channels = wpan_phy->supported.channels[request->page];
1445 
1446 	/* Use maximum duration order by default */
1447 	if (info->attrs[NL802154_ATTR_SCAN_DURATION])
1448 		request->duration = nla_get_u8(info->attrs[NL802154_ATTR_SCAN_DURATION]);
1449 	else
1450 		request->duration = IEEE802154_MAX_SCAN_DURATION;
1451 
1452 	err = rdev_trigger_scan(rdev, request);
1453 	if (err) {
1454 		pr_err("Failure starting scanning (%d)\n", err);
1455 		goto free_request;
1456 	}
1457 
1458 	return 0;
1459 
1460 free_request:
1461 	kfree(request);
1462 
1463 	return err;
1464 }
1465 
1466 static int nl802154_prep_scan_msg(struct sk_buff *msg,
1467 				  struct cfg802154_registered_device *rdev,
1468 				  struct wpan_dev *wpan_dev, u32 portid,
1469 				  u32 seq, int flags, u8 cmd, u8 arg)
1470 {
1471 	void *hdr;
1472 
1473 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1474 	if (!hdr)
1475 		return -ENOBUFS;
1476 
1477 	if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx))
1478 		goto nla_put_failure;
1479 
1480 	if (wpan_dev->netdev &&
1481 	    nla_put_u32(msg, NL802154_ATTR_IFINDEX, wpan_dev->netdev->ifindex))
1482 		goto nla_put_failure;
1483 
1484 	if (nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
1485 			      wpan_dev_id(wpan_dev), NL802154_ATTR_PAD))
1486 		goto nla_put_failure;
1487 
1488 	if (cmd == NL802154_CMD_SCAN_DONE &&
1489 	    nla_put_u8(msg, NL802154_ATTR_SCAN_DONE_REASON, arg))
1490 		goto nla_put_failure;
1491 
1492 	genlmsg_end(msg, hdr);
1493 
1494 	return 0;
1495 
1496 nla_put_failure:
1497 	genlmsg_cancel(msg, hdr);
1498 
1499 	return -EMSGSIZE;
1500 }
1501 
1502 static int nl802154_send_scan_msg(struct cfg802154_registered_device *rdev,
1503 				  struct wpan_dev *wpan_dev, u8 cmd, u8 arg)
1504 {
1505 	struct sk_buff *msg;
1506 	int ret;
1507 
1508 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1509 	if (!msg)
1510 		return -ENOMEM;
1511 
1512 	ret = nl802154_prep_scan_msg(msg, rdev, wpan_dev, 0, 0, 0, cmd, arg);
1513 	if (ret < 0) {
1514 		nlmsg_free(msg);
1515 		return ret;
1516 	}
1517 
1518 	return genlmsg_multicast_netns(&nl802154_fam,
1519 				       wpan_phy_net(&rdev->wpan_phy), msg, 0,
1520 				       NL802154_MCGRP_SCAN, GFP_KERNEL);
1521 }
1522 
1523 int nl802154_scan_started(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev)
1524 {
1525 	struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(wpan_phy);
1526 	int err;
1527 
1528 	/* Ignore errors when there are no listeners */
1529 	err = nl802154_send_scan_msg(rdev, wpan_dev, NL802154_CMD_TRIGGER_SCAN, 0);
1530 	if (err == -ESRCH)
1531 		err = 0;
1532 
1533 	return err;
1534 }
1535 EXPORT_SYMBOL_GPL(nl802154_scan_started);
1536 
1537 int nl802154_scan_done(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
1538 		       enum nl802154_scan_done_reasons reason)
1539 {
1540 	struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(wpan_phy);
1541 	int err;
1542 
1543 	/* Ignore errors when there are no listeners */
1544 	err = nl802154_send_scan_msg(rdev, wpan_dev, NL802154_CMD_SCAN_DONE, reason);
1545 	if (err == -ESRCH)
1546 		err = 0;
1547 
1548 	return err;
1549 }
1550 EXPORT_SYMBOL_GPL(nl802154_scan_done);
1551 
1552 static int nl802154_abort_scan(struct sk_buff *skb, struct genl_info *info)
1553 {
1554 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1555 	struct net_device *dev = info->user_ptr[1];
1556 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1557 
1558 	/* Resources are released in the notification helper above */
1559 	return rdev_abort_scan(rdev, wpan_dev);
1560 }
1561 
1562 static int
1563 nl802154_send_beacons(struct sk_buff *skb, struct genl_info *info)
1564 {
1565 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1566 	struct net_device *dev = info->user_ptr[1];
1567 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1568 	struct wpan_phy *wpan_phy = &rdev->wpan_phy;
1569 	struct cfg802154_beacon_request *request;
1570 	int err;
1571 
1572 	if (wpan_dev->iftype != NL802154_IFTYPE_COORD) {
1573 		NL_SET_ERR_MSG(info->extack, "Only coordinators can send beacons");
1574 		return -EOPNOTSUPP;
1575 	}
1576 
1577 	if (wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
1578 		NL_SET_ERR_MSG(info->extack, "Device is not part of any PAN");
1579 		return -EPERM;
1580 	}
1581 
1582 	if (wpan_phy->flags & WPAN_PHY_FLAG_DATAGRAMS_ONLY) {
1583 		NL_SET_ERR_MSG(info->extack, "PHY only supports datagrams");
1584 		return -EOPNOTSUPP;
1585 	}
1586 
1587 	request = kzalloc(sizeof(*request), GFP_KERNEL);
1588 	if (!request)
1589 		return -ENOMEM;
1590 
1591 	request->wpan_dev = wpan_dev;
1592 	request->wpan_phy = wpan_phy;
1593 
1594 	/* Use maximum duration order by default */
1595 	if (info->attrs[NL802154_ATTR_BEACON_INTERVAL])
1596 		request->interval = nla_get_u8(info->attrs[NL802154_ATTR_BEACON_INTERVAL]);
1597 	else
1598 		request->interval = IEEE802154_MAX_SCAN_DURATION;
1599 
1600 	err = rdev_send_beacons(rdev, request);
1601 	if (err) {
1602 		pr_err("Failure starting sending beacons (%d)\n", err);
1603 		goto free_request;
1604 	}
1605 
1606 	return 0;
1607 
1608 free_request:
1609 	kfree(request);
1610 
1611 	return err;
1612 }
1613 
1614 void nl802154_beaconing_done(struct wpan_dev *wpan_dev)
1615 {
1616 	/* NOP */
1617 }
1618 EXPORT_SYMBOL_GPL(nl802154_beaconing_done);
1619 
1620 static int
1621 nl802154_stop_beacons(struct sk_buff *skb, struct genl_info *info)
1622 {
1623 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1624 	struct net_device *dev = info->user_ptr[1];
1625 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1626 
1627 	/* Resources are released in the notification helper above */
1628 	return rdev_stop_beacons(rdev, wpan_dev);
1629 }
1630 
1631 static int nl802154_associate(struct sk_buff *skb, struct genl_info *info)
1632 {
1633 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1634 	struct net_device *dev = info->user_ptr[1];
1635 	struct wpan_dev *wpan_dev;
1636 	struct wpan_phy *wpan_phy;
1637 	struct ieee802154_addr coord;
1638 	int err;
1639 
1640 	wpan_dev = dev->ieee802154_ptr;
1641 	wpan_phy = &rdev->wpan_phy;
1642 
1643 	if (wpan_phy->flags & WPAN_PHY_FLAG_DATAGRAMS_ONLY) {
1644 		NL_SET_ERR_MSG(info->extack, "PHY only supports datagrams");
1645 		return -EOPNOTSUPP;
1646 	}
1647 
1648 	if (!info->attrs[NL802154_ATTR_PAN_ID] ||
1649 	    !info->attrs[NL802154_ATTR_EXTENDED_ADDR])
1650 		return -EINVAL;
1651 
1652 	coord.pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
1653 	coord.mode = IEEE802154_ADDR_LONG;
1654 	coord.extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
1655 
1656 	mutex_lock(&wpan_dev->association_lock);
1657 	err = rdev_associate(rdev, wpan_dev, &coord);
1658 	mutex_unlock(&wpan_dev->association_lock);
1659 	if (err)
1660 		pr_err("Association with PAN ID 0x%x failed (%d)\n",
1661 		       le16_to_cpu(coord.pan_id), err);
1662 
1663 	return err;
1664 }
1665 
1666 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
1667 static const struct nla_policy nl802154_dev_addr_policy[NL802154_DEV_ADDR_ATTR_MAX + 1] = {
1668 	[NL802154_DEV_ADDR_ATTR_PAN_ID] = { .type = NLA_U16 },
1669 	[NL802154_DEV_ADDR_ATTR_MODE] = { .type = NLA_U32 },
1670 	[NL802154_DEV_ADDR_ATTR_SHORT] = { .type = NLA_U16 },
1671 	[NL802154_DEV_ADDR_ATTR_EXTENDED] = { .type = NLA_U64 },
1672 };
1673 
1674 static int
1675 ieee802154_llsec_parse_dev_addr(struct nlattr *nla,
1676 				struct ieee802154_addr *addr)
1677 {
1678 	struct nlattr *attrs[NL802154_DEV_ADDR_ATTR_MAX + 1];
1679 
1680 	if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ADDR_ATTR_MAX, nla, nl802154_dev_addr_policy, NULL))
1681 		return -EINVAL;
1682 
1683 	if (!attrs[NL802154_DEV_ADDR_ATTR_PAN_ID] || !attrs[NL802154_DEV_ADDR_ATTR_MODE])
1684 		return -EINVAL;
1685 
1686 	addr->pan_id = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_PAN_ID]);
1687 	addr->mode = nla_get_u32(attrs[NL802154_DEV_ADDR_ATTR_MODE]);
1688 	switch (addr->mode) {
1689 	case NL802154_DEV_ADDR_SHORT:
1690 		if (!attrs[NL802154_DEV_ADDR_ATTR_SHORT])
1691 			return -EINVAL;
1692 		addr->short_addr = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_SHORT]);
1693 		break;
1694 	case NL802154_DEV_ADDR_EXTENDED:
1695 		if (!attrs[NL802154_DEV_ADDR_ATTR_EXTENDED])
1696 			return -EINVAL;
1697 		addr->extended_addr = nla_get_le64(attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]);
1698 		break;
1699 	default:
1700 		return -EINVAL;
1701 	}
1702 
1703 	return 0;
1704 }
1705 
1706 static const struct nla_policy nl802154_key_id_policy[NL802154_KEY_ID_ATTR_MAX + 1] = {
1707 	[NL802154_KEY_ID_ATTR_MODE] = { .type = NLA_U32 },
1708 	[NL802154_KEY_ID_ATTR_INDEX] = { .type = NLA_U8 },
1709 	[NL802154_KEY_ID_ATTR_IMPLICIT] = { .type = NLA_NESTED },
1710 	[NL802154_KEY_ID_ATTR_SOURCE_SHORT] = { .type = NLA_U32 },
1711 	[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED] = { .type = NLA_U64 },
1712 };
1713 
1714 static int
1715 ieee802154_llsec_parse_key_id(struct nlattr *nla,
1716 			      struct ieee802154_llsec_key_id *desc)
1717 {
1718 	struct nlattr *attrs[NL802154_KEY_ID_ATTR_MAX + 1];
1719 
1720 	if (!nla || nla_parse_nested_deprecated(attrs, NL802154_KEY_ID_ATTR_MAX, nla, nl802154_key_id_policy, NULL))
1721 		return -EINVAL;
1722 
1723 	if (!attrs[NL802154_KEY_ID_ATTR_MODE])
1724 		return -EINVAL;
1725 
1726 	desc->mode = nla_get_u32(attrs[NL802154_KEY_ID_ATTR_MODE]);
1727 	switch (desc->mode) {
1728 	case NL802154_KEY_ID_MODE_IMPLICIT:
1729 		if (!attrs[NL802154_KEY_ID_ATTR_IMPLICIT])
1730 			return -EINVAL;
1731 
1732 		if (ieee802154_llsec_parse_dev_addr(attrs[NL802154_KEY_ID_ATTR_IMPLICIT],
1733 						    &desc->device_addr) < 0)
1734 			return -EINVAL;
1735 		break;
1736 	case NL802154_KEY_ID_MODE_INDEX:
1737 		break;
1738 	case NL802154_KEY_ID_MODE_INDEX_SHORT:
1739 		if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT])
1740 			return -EINVAL;
1741 
1742 		desc->short_source = nla_get_le32(attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT]);
1743 		break;
1744 	case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
1745 		if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED])
1746 			return -EINVAL;
1747 
1748 		desc->extended_source = nla_get_le64(attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED]);
1749 		break;
1750 	default:
1751 		return -EINVAL;
1752 	}
1753 
1754 	if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
1755 		if (!attrs[NL802154_KEY_ID_ATTR_INDEX])
1756 			return -EINVAL;
1757 
1758 		/* TODO change id to idx */
1759 		desc->id = nla_get_u8(attrs[NL802154_KEY_ID_ATTR_INDEX]);
1760 	}
1761 
1762 	return 0;
1763 }
1764 
1765 static int nl802154_set_llsec_params(struct sk_buff *skb,
1766 				     struct genl_info *info)
1767 {
1768 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1769 	struct net_device *dev = info->user_ptr[1];
1770 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1771 	struct ieee802154_llsec_params params;
1772 	u32 changed = 0;
1773 	int ret;
1774 
1775 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1776 		return -EOPNOTSUPP;
1777 
1778 	if (info->attrs[NL802154_ATTR_SEC_ENABLED]) {
1779 		u8 enabled;
1780 
1781 		enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1782 		if (enabled != 0 && enabled != 1)
1783 			return -EINVAL;
1784 
1785 		params.enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1786 		changed |= IEEE802154_LLSEC_PARAM_ENABLED;
1787 	}
1788 
1789 	if (info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID]) {
1790 		ret = ieee802154_llsec_parse_key_id(info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID],
1791 						    &params.out_key);
1792 		if (ret < 0)
1793 			return ret;
1794 
1795 		changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
1796 	}
1797 
1798 	if (info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]) {
1799 		params.out_level = nla_get_u32(info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]);
1800 		if (params.out_level > NL802154_SECLEVEL_MAX)
1801 			return -EINVAL;
1802 
1803 		changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
1804 	}
1805 
1806 	if (info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]) {
1807 		params.frame_counter = nla_get_be32(info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]);
1808 		changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
1809 	}
1810 
1811 	return rdev_set_llsec_params(rdev, wpan_dev, &params, changed);
1812 }
1813 
1814 static int nl802154_send_key(struct sk_buff *msg, u32 cmd, u32 portid,
1815 			     u32 seq, int flags,
1816 			     struct cfg802154_registered_device *rdev,
1817 			     struct net_device *dev,
1818 			     const struct ieee802154_llsec_key_entry *key)
1819 {
1820 	void *hdr;
1821 	u32 commands[NL802154_CMD_FRAME_NR_IDS / 32];
1822 	struct nlattr *nl_key, *nl_key_id;
1823 
1824 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1825 	if (!hdr)
1826 		return -ENOBUFS;
1827 
1828 	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1829 		goto nla_put_failure;
1830 
1831 	nl_key = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_KEY);
1832 	if (!nl_key)
1833 		goto nla_put_failure;
1834 
1835 	nl_key_id = nla_nest_start_noflag(msg, NL802154_KEY_ATTR_ID);
1836 	if (!nl_key_id)
1837 		goto nla_put_failure;
1838 
1839 	if (ieee802154_llsec_send_key_id(msg, &key->id) < 0)
1840 		goto nla_put_failure;
1841 
1842 	nla_nest_end(msg, nl_key_id);
1843 
1844 	if (nla_put_u8(msg, NL802154_KEY_ATTR_USAGE_FRAMES,
1845 		       key->key->frame_types))
1846 		goto nla_put_failure;
1847 
1848 	if (key->key->frame_types & BIT(NL802154_FRAME_CMD)) {
1849 		/* TODO for each nested */
1850 		memset(commands, 0, sizeof(commands));
1851 		commands[7] = key->key->cmd_frame_ids;
1852 		if (nla_put(msg, NL802154_KEY_ATTR_USAGE_CMDS,
1853 			    sizeof(commands), commands))
1854 			goto nla_put_failure;
1855 	}
1856 
1857 	if (nla_put(msg, NL802154_KEY_ATTR_BYTES, NL802154_KEY_SIZE,
1858 		    key->key->key))
1859 		goto nla_put_failure;
1860 
1861 	nla_nest_end(msg, nl_key);
1862 	genlmsg_end(msg, hdr);
1863 
1864 	return 0;
1865 
1866 nla_put_failure:
1867 	genlmsg_cancel(msg, hdr);
1868 	return -EMSGSIZE;
1869 }
1870 
1871 static int
1872 nl802154_dump_llsec_key(struct sk_buff *skb, struct netlink_callback *cb)
1873 {
1874 	struct cfg802154_registered_device *rdev = NULL;
1875 	struct ieee802154_llsec_key_entry *key;
1876 	struct ieee802154_llsec_table *table;
1877 	struct wpan_dev *wpan_dev;
1878 	int err;
1879 
1880 	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1881 	if (err)
1882 		return err;
1883 
1884 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1885 		err = skb->len;
1886 		goto out_err;
1887 	}
1888 
1889 	if (!wpan_dev->netdev) {
1890 		err = -EINVAL;
1891 		goto out_err;
1892 	}
1893 
1894 	rdev_lock_llsec_table(rdev, wpan_dev);
1895 	rdev_get_llsec_table(rdev, wpan_dev, &table);
1896 
1897 	/* TODO make it like station dump */
1898 	if (cb->args[2])
1899 		goto out;
1900 
1901 	list_for_each_entry(key, &table->keys, list) {
1902 		if (nl802154_send_key(skb, NL802154_CMD_NEW_SEC_KEY,
1903 				      NETLINK_CB(cb->skb).portid,
1904 				      cb->nlh->nlmsg_seq, NLM_F_MULTI,
1905 				      rdev, wpan_dev->netdev, key) < 0) {
1906 			/* TODO */
1907 			err = -EIO;
1908 			rdev_unlock_llsec_table(rdev, wpan_dev);
1909 			goto out_err;
1910 		}
1911 	}
1912 
1913 	cb->args[2] = 1;
1914 
1915 out:
1916 	rdev_unlock_llsec_table(rdev, wpan_dev);
1917 	err = skb->len;
1918 out_err:
1919 	nl802154_finish_wpan_dev_dump(rdev);
1920 
1921 	return err;
1922 }
1923 
1924 static const struct nla_policy nl802154_key_policy[NL802154_KEY_ATTR_MAX + 1] = {
1925 	[NL802154_KEY_ATTR_ID] = { NLA_NESTED },
1926 	/* TODO handle it as for_each_nested and NLA_FLAG? */
1927 	[NL802154_KEY_ATTR_USAGE_FRAMES] = { NLA_U8 },
1928 	/* TODO handle it as for_each_nested, not static array? */
1929 	[NL802154_KEY_ATTR_USAGE_CMDS] = { .len = NL802154_CMD_FRAME_NR_IDS / 8 },
1930 	[NL802154_KEY_ATTR_BYTES] = { .len = NL802154_KEY_SIZE },
1931 };
1932 
1933 static int nl802154_add_llsec_key(struct sk_buff *skb, struct genl_info *info)
1934 {
1935 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1936 	struct net_device *dev = info->user_ptr[1];
1937 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1938 	struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1939 	struct ieee802154_llsec_key key = { };
1940 	struct ieee802154_llsec_key_id id = { };
1941 	u32 commands[NL802154_CMD_FRAME_NR_IDS / 32] = { };
1942 
1943 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1944 		return -EOPNOTSUPP;
1945 
1946 	if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
1947 	    nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
1948 		return -EINVAL;
1949 
1950 	if (!attrs[NL802154_KEY_ATTR_USAGE_FRAMES] ||
1951 	    !attrs[NL802154_KEY_ATTR_BYTES])
1952 		return -EINVAL;
1953 
1954 	if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1955 		return -ENOBUFS;
1956 
1957 	key.frame_types = nla_get_u8(attrs[NL802154_KEY_ATTR_USAGE_FRAMES]);
1958 	if (key.frame_types > BIT(NL802154_FRAME_MAX) ||
1959 	    ((key.frame_types & BIT(NL802154_FRAME_CMD)) &&
1960 	     !attrs[NL802154_KEY_ATTR_USAGE_CMDS]))
1961 		return -EINVAL;
1962 
1963 	if (attrs[NL802154_KEY_ATTR_USAGE_CMDS]) {
1964 		/* TODO for each nested */
1965 		nla_memcpy(commands, attrs[NL802154_KEY_ATTR_USAGE_CMDS],
1966 			   NL802154_CMD_FRAME_NR_IDS / 8);
1967 
1968 		/* TODO understand the -EINVAL logic here? last condition */
1969 		if (commands[0] || commands[1] || commands[2] || commands[3] ||
1970 		    commands[4] || commands[5] || commands[6] ||
1971 		    commands[7] > BIT(NL802154_CMD_FRAME_MAX))
1972 			return -EINVAL;
1973 
1974 		key.cmd_frame_ids = commands[7];
1975 	} else {
1976 		key.cmd_frame_ids = 0;
1977 	}
1978 
1979 	nla_memcpy(key.key, attrs[NL802154_KEY_ATTR_BYTES], NL802154_KEY_SIZE);
1980 
1981 	if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1982 		return -ENOBUFS;
1983 
1984 	return rdev_add_llsec_key(rdev, wpan_dev, &id, &key);
1985 }
1986 
1987 static int nl802154_del_llsec_key(struct sk_buff *skb, struct genl_info *info)
1988 {
1989 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1990 	struct net_device *dev = info->user_ptr[1];
1991 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1992 	struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1993 	struct ieee802154_llsec_key_id id;
1994 
1995 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1996 		return -EOPNOTSUPP;
1997 
1998 	if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
1999 	    nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
2000 		return -EINVAL;
2001 
2002 	if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
2003 		return -ENOBUFS;
2004 
2005 	return rdev_del_llsec_key(rdev, wpan_dev, &id);
2006 }
2007 
2008 static int nl802154_send_device(struct sk_buff *msg, u32 cmd, u32 portid,
2009 				u32 seq, int flags,
2010 				struct cfg802154_registered_device *rdev,
2011 				struct net_device *dev,
2012 				const struct ieee802154_llsec_device *dev_desc)
2013 {
2014 	void *hdr;
2015 	struct nlattr *nl_device;
2016 
2017 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
2018 	if (!hdr)
2019 		return -ENOBUFS;
2020 
2021 	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
2022 		goto nla_put_failure;
2023 
2024 	nl_device = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVICE);
2025 	if (!nl_device)
2026 		goto nla_put_failure;
2027 
2028 	if (nla_put_u32(msg, NL802154_DEV_ATTR_FRAME_COUNTER,
2029 			dev_desc->frame_counter) ||
2030 	    nla_put_le16(msg, NL802154_DEV_ATTR_PAN_ID, dev_desc->pan_id) ||
2031 	    nla_put_le16(msg, NL802154_DEV_ATTR_SHORT_ADDR,
2032 			 dev_desc->short_addr) ||
2033 	    nla_put_le64(msg, NL802154_DEV_ATTR_EXTENDED_ADDR,
2034 			 dev_desc->hwaddr, NL802154_DEV_ATTR_PAD) ||
2035 	    nla_put_u8(msg, NL802154_DEV_ATTR_SECLEVEL_EXEMPT,
2036 		       dev_desc->seclevel_exempt) ||
2037 	    nla_put_u32(msg, NL802154_DEV_ATTR_KEY_MODE, dev_desc->key_mode))
2038 		goto nla_put_failure;
2039 
2040 	nla_nest_end(msg, nl_device);
2041 	genlmsg_end(msg, hdr);
2042 
2043 	return 0;
2044 
2045 nla_put_failure:
2046 	genlmsg_cancel(msg, hdr);
2047 	return -EMSGSIZE;
2048 }
2049 
2050 static int
2051 nl802154_dump_llsec_dev(struct sk_buff *skb, struct netlink_callback *cb)
2052 {
2053 	struct cfg802154_registered_device *rdev = NULL;
2054 	struct ieee802154_llsec_device *dev;
2055 	struct ieee802154_llsec_table *table;
2056 	struct wpan_dev *wpan_dev;
2057 	int err;
2058 
2059 	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2060 	if (err)
2061 		return err;
2062 
2063 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2064 		err = skb->len;
2065 		goto out_err;
2066 	}
2067 
2068 	if (!wpan_dev->netdev) {
2069 		err = -EINVAL;
2070 		goto out_err;
2071 	}
2072 
2073 	rdev_lock_llsec_table(rdev, wpan_dev);
2074 	rdev_get_llsec_table(rdev, wpan_dev, &table);
2075 
2076 	/* TODO make it like station dump */
2077 	if (cb->args[2])
2078 		goto out;
2079 
2080 	list_for_each_entry(dev, &table->devices, list) {
2081 		if (nl802154_send_device(skb, NL802154_CMD_NEW_SEC_LEVEL,
2082 					 NETLINK_CB(cb->skb).portid,
2083 					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2084 					 rdev, wpan_dev->netdev, dev) < 0) {
2085 			/* TODO */
2086 			err = -EIO;
2087 			rdev_unlock_llsec_table(rdev, wpan_dev);
2088 			goto out_err;
2089 		}
2090 	}
2091 
2092 	cb->args[2] = 1;
2093 
2094 out:
2095 	rdev_unlock_llsec_table(rdev, wpan_dev);
2096 	err = skb->len;
2097 out_err:
2098 	nl802154_finish_wpan_dev_dump(rdev);
2099 
2100 	return err;
2101 }
2102 
2103 static const struct nla_policy nl802154_dev_policy[NL802154_DEV_ATTR_MAX + 1] = {
2104 	[NL802154_DEV_ATTR_FRAME_COUNTER] = { NLA_U32 },
2105 	[NL802154_DEV_ATTR_PAN_ID] = { .type = NLA_U16 },
2106 	[NL802154_DEV_ATTR_SHORT_ADDR] = { .type = NLA_U16 },
2107 	[NL802154_DEV_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
2108 	[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] = { NLA_U8 },
2109 	[NL802154_DEV_ATTR_KEY_MODE] = { NLA_U32 },
2110 };
2111 
2112 static int
2113 ieee802154_llsec_parse_device(struct nlattr *nla,
2114 			      struct ieee802154_llsec_device *dev)
2115 {
2116 	struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
2117 
2118 	if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, nla, nl802154_dev_policy, NULL))
2119 		return -EINVAL;
2120 
2121 	memset(dev, 0, sizeof(*dev));
2122 
2123 	if (!attrs[NL802154_DEV_ATTR_FRAME_COUNTER] ||
2124 	    !attrs[NL802154_DEV_ATTR_PAN_ID] ||
2125 	    !attrs[NL802154_DEV_ATTR_SHORT_ADDR] ||
2126 	    !attrs[NL802154_DEV_ATTR_EXTENDED_ADDR] ||
2127 	    !attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] ||
2128 	    !attrs[NL802154_DEV_ATTR_KEY_MODE])
2129 		return -EINVAL;
2130 
2131 	/* TODO be32 */
2132 	dev->frame_counter = nla_get_u32(attrs[NL802154_DEV_ATTR_FRAME_COUNTER]);
2133 	dev->pan_id = nla_get_le16(attrs[NL802154_DEV_ATTR_PAN_ID]);
2134 	dev->short_addr = nla_get_le16(attrs[NL802154_DEV_ATTR_SHORT_ADDR]);
2135 	/* TODO rename hwaddr to extended_addr */
2136 	dev->hwaddr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
2137 	dev->seclevel_exempt = nla_get_u8(attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT]);
2138 	dev->key_mode = nla_get_u32(attrs[NL802154_DEV_ATTR_KEY_MODE]);
2139 
2140 	if (dev->key_mode > NL802154_DEVKEY_MAX ||
2141 	    (dev->seclevel_exempt != 0 && dev->seclevel_exempt != 1))
2142 		return -EINVAL;
2143 
2144 	return 0;
2145 }
2146 
2147 static int nl802154_add_llsec_dev(struct sk_buff *skb, struct genl_info *info)
2148 {
2149 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2150 	struct net_device *dev = info->user_ptr[1];
2151 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2152 	struct ieee802154_llsec_device dev_desc;
2153 
2154 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2155 		return -EOPNOTSUPP;
2156 
2157 	if (ieee802154_llsec_parse_device(info->attrs[NL802154_ATTR_SEC_DEVICE],
2158 					  &dev_desc) < 0)
2159 		return -EINVAL;
2160 
2161 	return rdev_add_device(rdev, wpan_dev, &dev_desc);
2162 }
2163 
2164 static int nl802154_del_llsec_dev(struct sk_buff *skb, struct genl_info *info)
2165 {
2166 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2167 	struct net_device *dev = info->user_ptr[1];
2168 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2169 	struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
2170 	__le64 extended_addr;
2171 
2172 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2173 		return -EOPNOTSUPP;
2174 
2175 	if (!info->attrs[NL802154_ATTR_SEC_DEVICE] ||
2176 	    nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVICE], nl802154_dev_policy, info->extack))
2177 		return -EINVAL;
2178 
2179 	if (!attrs[NL802154_DEV_ATTR_EXTENDED_ADDR])
2180 		return -EINVAL;
2181 
2182 	extended_addr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
2183 	return rdev_del_device(rdev, wpan_dev, extended_addr);
2184 }
2185 
2186 static int nl802154_send_devkey(struct sk_buff *msg, u32 cmd, u32 portid,
2187 				u32 seq, int flags,
2188 				struct cfg802154_registered_device *rdev,
2189 				struct net_device *dev, __le64 extended_addr,
2190 				const struct ieee802154_llsec_device_key *devkey)
2191 {
2192 	void *hdr;
2193 	struct nlattr *nl_devkey, *nl_key_id;
2194 
2195 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
2196 	if (!hdr)
2197 		return -ENOBUFS;
2198 
2199 	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
2200 		goto nla_put_failure;
2201 
2202 	nl_devkey = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVKEY);
2203 	if (!nl_devkey)
2204 		goto nla_put_failure;
2205 
2206 	if (nla_put_le64(msg, NL802154_DEVKEY_ATTR_EXTENDED_ADDR,
2207 			 extended_addr, NL802154_DEVKEY_ATTR_PAD) ||
2208 	    nla_put_u32(msg, NL802154_DEVKEY_ATTR_FRAME_COUNTER,
2209 			devkey->frame_counter))
2210 		goto nla_put_failure;
2211 
2212 	nl_key_id = nla_nest_start_noflag(msg, NL802154_DEVKEY_ATTR_ID);
2213 	if (!nl_key_id)
2214 		goto nla_put_failure;
2215 
2216 	if (ieee802154_llsec_send_key_id(msg, &devkey->key_id) < 0)
2217 		goto nla_put_failure;
2218 
2219 	nla_nest_end(msg, nl_key_id);
2220 	nla_nest_end(msg, nl_devkey);
2221 	genlmsg_end(msg, hdr);
2222 
2223 	return 0;
2224 
2225 nla_put_failure:
2226 	genlmsg_cancel(msg, hdr);
2227 	return -EMSGSIZE;
2228 }
2229 
2230 static int
2231 nl802154_dump_llsec_devkey(struct sk_buff *skb, struct netlink_callback *cb)
2232 {
2233 	struct cfg802154_registered_device *rdev = NULL;
2234 	struct ieee802154_llsec_device_key *kpos;
2235 	struct ieee802154_llsec_device *dpos;
2236 	struct ieee802154_llsec_table *table;
2237 	struct wpan_dev *wpan_dev;
2238 	int err;
2239 
2240 	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2241 	if (err)
2242 		return err;
2243 
2244 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2245 		err = skb->len;
2246 		goto out_err;
2247 	}
2248 
2249 	if (!wpan_dev->netdev) {
2250 		err = -EINVAL;
2251 		goto out_err;
2252 	}
2253 
2254 	rdev_lock_llsec_table(rdev, wpan_dev);
2255 	rdev_get_llsec_table(rdev, wpan_dev, &table);
2256 
2257 	/* TODO make it like station dump */
2258 	if (cb->args[2])
2259 		goto out;
2260 
2261 	/* TODO look if remove devkey and do some nested attribute */
2262 	list_for_each_entry(dpos, &table->devices, list) {
2263 		list_for_each_entry(kpos, &dpos->keys, list) {
2264 			if (nl802154_send_devkey(skb,
2265 						 NL802154_CMD_NEW_SEC_LEVEL,
2266 						 NETLINK_CB(cb->skb).portid,
2267 						 cb->nlh->nlmsg_seq,
2268 						 NLM_F_MULTI, rdev,
2269 						 wpan_dev->netdev,
2270 						 dpos->hwaddr,
2271 						 kpos) < 0) {
2272 				/* TODO */
2273 				err = -EIO;
2274 				rdev_unlock_llsec_table(rdev, wpan_dev);
2275 				goto out_err;
2276 			}
2277 		}
2278 	}
2279 
2280 	cb->args[2] = 1;
2281 
2282 out:
2283 	rdev_unlock_llsec_table(rdev, wpan_dev);
2284 	err = skb->len;
2285 out_err:
2286 	nl802154_finish_wpan_dev_dump(rdev);
2287 
2288 	return err;
2289 }
2290 
2291 static const struct nla_policy nl802154_devkey_policy[NL802154_DEVKEY_ATTR_MAX + 1] = {
2292 	[NL802154_DEVKEY_ATTR_FRAME_COUNTER] = { NLA_U32 },
2293 	[NL802154_DEVKEY_ATTR_EXTENDED_ADDR] = { NLA_U64 },
2294 	[NL802154_DEVKEY_ATTR_ID] = { NLA_NESTED },
2295 };
2296 
2297 static int nl802154_add_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
2298 {
2299 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2300 	struct net_device *dev = info->user_ptr[1];
2301 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2302 	struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
2303 	struct ieee802154_llsec_device_key key;
2304 	__le64 extended_addr;
2305 
2306 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2307 		return -EOPNOTSUPP;
2308 
2309 	if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
2310 	    nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack) < 0)
2311 		return -EINVAL;
2312 
2313 	if (!attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER] ||
2314 	    !attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
2315 		return -EINVAL;
2316 
2317 	/* TODO change key.id ? */
2318 	if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
2319 					  &key.key_id) < 0)
2320 		return -ENOBUFS;
2321 
2322 	/* TODO be32 */
2323 	key.frame_counter = nla_get_u32(attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER]);
2324 	/* TODO change naming hwaddr -> extended_addr
2325 	 * check unique identifier short+pan OR extended_addr
2326 	 */
2327 	extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
2328 	return rdev_add_devkey(rdev, wpan_dev, extended_addr, &key);
2329 }
2330 
2331 static int nl802154_del_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
2332 {
2333 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2334 	struct net_device *dev = info->user_ptr[1];
2335 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2336 	struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
2337 	struct ieee802154_llsec_device_key key;
2338 	__le64 extended_addr;
2339 
2340 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2341 		return -EOPNOTSUPP;
2342 
2343 	if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
2344 	    nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack))
2345 		return -EINVAL;
2346 
2347 	if (!attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
2348 		return -EINVAL;
2349 
2350 	/* TODO change key.id ? */
2351 	if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
2352 					  &key.key_id) < 0)
2353 		return -ENOBUFS;
2354 
2355 	/* TODO change naming hwaddr -> extended_addr
2356 	 * check unique identifier short+pan OR extended_addr
2357 	 */
2358 	extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
2359 	return rdev_del_devkey(rdev, wpan_dev, extended_addr, &key);
2360 }
2361 
2362 static int nl802154_send_seclevel(struct sk_buff *msg, u32 cmd, u32 portid,
2363 				  u32 seq, int flags,
2364 				  struct cfg802154_registered_device *rdev,
2365 				  struct net_device *dev,
2366 				  const struct ieee802154_llsec_seclevel *sl)
2367 {
2368 	void *hdr;
2369 	struct nlattr *nl_seclevel;
2370 
2371 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
2372 	if (!hdr)
2373 		return -ENOBUFS;
2374 
2375 	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
2376 		goto nla_put_failure;
2377 
2378 	nl_seclevel = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_LEVEL);
2379 	if (!nl_seclevel)
2380 		goto nla_put_failure;
2381 
2382 	if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_FRAME, sl->frame_type) ||
2383 	    nla_put_u32(msg, NL802154_SECLEVEL_ATTR_LEVELS, sl->sec_levels) ||
2384 	    nla_put_u8(msg, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE,
2385 		       sl->device_override))
2386 		goto nla_put_failure;
2387 
2388 	if (sl->frame_type == NL802154_FRAME_CMD) {
2389 		if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_CMD_FRAME,
2390 				sl->cmd_frame_id))
2391 			goto nla_put_failure;
2392 	}
2393 
2394 	nla_nest_end(msg, nl_seclevel);
2395 	genlmsg_end(msg, hdr);
2396 
2397 	return 0;
2398 
2399 nla_put_failure:
2400 	genlmsg_cancel(msg, hdr);
2401 	return -EMSGSIZE;
2402 }
2403 
2404 static int
2405 nl802154_dump_llsec_seclevel(struct sk_buff *skb, struct netlink_callback *cb)
2406 {
2407 	struct cfg802154_registered_device *rdev = NULL;
2408 	struct ieee802154_llsec_seclevel *sl;
2409 	struct ieee802154_llsec_table *table;
2410 	struct wpan_dev *wpan_dev;
2411 	int err;
2412 
2413 	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2414 	if (err)
2415 		return err;
2416 
2417 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2418 		err = skb->len;
2419 		goto out_err;
2420 	}
2421 
2422 	if (!wpan_dev->netdev) {
2423 		err = -EINVAL;
2424 		goto out_err;
2425 	}
2426 
2427 	rdev_lock_llsec_table(rdev, wpan_dev);
2428 	rdev_get_llsec_table(rdev, wpan_dev, &table);
2429 
2430 	/* TODO make it like station dump */
2431 	if (cb->args[2])
2432 		goto out;
2433 
2434 	list_for_each_entry(sl, &table->security_levels, list) {
2435 		if (nl802154_send_seclevel(skb, NL802154_CMD_NEW_SEC_LEVEL,
2436 					   NETLINK_CB(cb->skb).portid,
2437 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
2438 					   rdev, wpan_dev->netdev, sl) < 0) {
2439 			/* TODO */
2440 			err = -EIO;
2441 			rdev_unlock_llsec_table(rdev, wpan_dev);
2442 			goto out_err;
2443 		}
2444 	}
2445 
2446 	cb->args[2] = 1;
2447 
2448 out:
2449 	rdev_unlock_llsec_table(rdev, wpan_dev);
2450 	err = skb->len;
2451 out_err:
2452 	nl802154_finish_wpan_dev_dump(rdev);
2453 
2454 	return err;
2455 }
2456 
2457 static const struct nla_policy nl802154_seclevel_policy[NL802154_SECLEVEL_ATTR_MAX + 1] = {
2458 	[NL802154_SECLEVEL_ATTR_LEVELS] = { .type = NLA_U8 },
2459 	[NL802154_SECLEVEL_ATTR_FRAME] = { .type = NLA_U32 },
2460 	[NL802154_SECLEVEL_ATTR_CMD_FRAME] = { .type = NLA_U32 },
2461 	[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE] = { .type = NLA_U8 },
2462 };
2463 
2464 static int
2465 llsec_parse_seclevel(struct nlattr *nla, struct ieee802154_llsec_seclevel *sl)
2466 {
2467 	struct nlattr *attrs[NL802154_SECLEVEL_ATTR_MAX + 1];
2468 
2469 	if (!nla || nla_parse_nested_deprecated(attrs, NL802154_SECLEVEL_ATTR_MAX, nla, nl802154_seclevel_policy, NULL))
2470 		return -EINVAL;
2471 
2472 	memset(sl, 0, sizeof(*sl));
2473 
2474 	if (!attrs[NL802154_SECLEVEL_ATTR_LEVELS] ||
2475 	    !attrs[NL802154_SECLEVEL_ATTR_FRAME] ||
2476 	    !attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE])
2477 		return -EINVAL;
2478 
2479 	sl->sec_levels = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_LEVELS]);
2480 	sl->frame_type = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_FRAME]);
2481 	sl->device_override = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE]);
2482 	if (sl->frame_type > NL802154_FRAME_MAX ||
2483 	    (sl->device_override != 0 && sl->device_override != 1))
2484 		return -EINVAL;
2485 
2486 	if (sl->frame_type == NL802154_FRAME_CMD) {
2487 		if (!attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME])
2488 			return -EINVAL;
2489 
2490 		sl->cmd_frame_id = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME]);
2491 		if (sl->cmd_frame_id > NL802154_CMD_FRAME_MAX)
2492 			return -EINVAL;
2493 	}
2494 
2495 	return 0;
2496 }
2497 
2498 static int nl802154_add_llsec_seclevel(struct sk_buff *skb,
2499 				       struct genl_info *info)
2500 {
2501 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2502 	struct net_device *dev = info->user_ptr[1];
2503 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2504 	struct ieee802154_llsec_seclevel sl;
2505 
2506 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2507 		return -EOPNOTSUPP;
2508 
2509 	if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2510 				 &sl) < 0)
2511 		return -EINVAL;
2512 
2513 	return rdev_add_seclevel(rdev, wpan_dev, &sl);
2514 }
2515 
2516 static int nl802154_del_llsec_seclevel(struct sk_buff *skb,
2517 				       struct genl_info *info)
2518 {
2519 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2520 	struct net_device *dev = info->user_ptr[1];
2521 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2522 	struct ieee802154_llsec_seclevel sl;
2523 
2524 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2525 		return -EOPNOTSUPP;
2526 
2527 	if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2528 				 &sl) < 0)
2529 		return -EINVAL;
2530 
2531 	return rdev_del_seclevel(rdev, wpan_dev, &sl);
2532 }
2533 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2534 
2535 #define NL802154_FLAG_NEED_WPAN_PHY	0x01
2536 #define NL802154_FLAG_NEED_NETDEV	0x02
2537 #define NL802154_FLAG_NEED_RTNL		0x04
2538 #define NL802154_FLAG_CHECK_NETDEV_UP	0x08
2539 #define NL802154_FLAG_NEED_WPAN_DEV	0x10
2540 
2541 static int nl802154_pre_doit(const struct genl_split_ops *ops,
2542 			     struct sk_buff *skb,
2543 			     struct genl_info *info)
2544 {
2545 	struct cfg802154_registered_device *rdev;
2546 	struct wpan_dev *wpan_dev;
2547 	struct net_device *dev;
2548 	bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
2549 
2550 	if (rtnl)
2551 		rtnl_lock();
2552 
2553 	if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
2554 		rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
2555 		if (IS_ERR(rdev)) {
2556 			if (rtnl)
2557 				rtnl_unlock();
2558 			return PTR_ERR(rdev);
2559 		}
2560 		info->user_ptr[0] = rdev;
2561 	} else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
2562 		   ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2563 		ASSERT_RTNL();
2564 		wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
2565 							   info->attrs);
2566 		if (IS_ERR(wpan_dev)) {
2567 			if (rtnl)
2568 				rtnl_unlock();
2569 			return PTR_ERR(wpan_dev);
2570 		}
2571 
2572 		dev = wpan_dev->netdev;
2573 		rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
2574 
2575 		if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
2576 			if (!dev) {
2577 				if (rtnl)
2578 					rtnl_unlock();
2579 				return -EINVAL;
2580 			}
2581 
2582 			info->user_ptr[1] = dev;
2583 		} else {
2584 			info->user_ptr[1] = wpan_dev;
2585 		}
2586 
2587 		if (dev) {
2588 			if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
2589 			    !netif_running(dev)) {
2590 				if (rtnl)
2591 					rtnl_unlock();
2592 				return -ENETDOWN;
2593 			}
2594 
2595 			dev_hold(dev);
2596 		}
2597 
2598 		info->user_ptr[0] = rdev;
2599 	}
2600 
2601 	return 0;
2602 }
2603 
2604 static void nl802154_post_doit(const struct genl_split_ops *ops,
2605 			       struct sk_buff *skb,
2606 			       struct genl_info *info)
2607 {
2608 	if (info->user_ptr[1]) {
2609 		if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2610 			struct wpan_dev *wpan_dev = info->user_ptr[1];
2611 
2612 			dev_put(wpan_dev->netdev);
2613 		} else {
2614 			dev_put(info->user_ptr[1]);
2615 		}
2616 	}
2617 
2618 	if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
2619 		rtnl_unlock();
2620 }
2621 
2622 static const struct genl_ops nl802154_ops[] = {
2623 	{
2624 		.cmd = NL802154_CMD_GET_WPAN_PHY,
2625 		.validate = GENL_DONT_VALIDATE_STRICT |
2626 			    GENL_DONT_VALIDATE_DUMP_STRICT,
2627 		.doit = nl802154_get_wpan_phy,
2628 		.dumpit = nl802154_dump_wpan_phy,
2629 		.done = nl802154_dump_wpan_phy_done,
2630 		/* can be retrieved by unprivileged users */
2631 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2632 				  NL802154_FLAG_NEED_RTNL,
2633 	},
2634 	{
2635 		.cmd = NL802154_CMD_GET_INTERFACE,
2636 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2637 		.doit = nl802154_get_interface,
2638 		.dumpit = nl802154_dump_interface,
2639 		/* can be retrieved by unprivileged users */
2640 		.internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2641 				  NL802154_FLAG_NEED_RTNL,
2642 	},
2643 	{
2644 		.cmd = NL802154_CMD_NEW_INTERFACE,
2645 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2646 		.doit = nl802154_new_interface,
2647 		.flags = GENL_ADMIN_PERM,
2648 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2649 				  NL802154_FLAG_NEED_RTNL,
2650 	},
2651 	{
2652 		.cmd = NL802154_CMD_DEL_INTERFACE,
2653 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2654 		.doit = nl802154_del_interface,
2655 		.flags = GENL_ADMIN_PERM,
2656 		.internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2657 				  NL802154_FLAG_NEED_RTNL,
2658 	},
2659 	{
2660 		.cmd = NL802154_CMD_SET_CHANNEL,
2661 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2662 		.doit = nl802154_set_channel,
2663 		.flags = GENL_ADMIN_PERM,
2664 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2665 				  NL802154_FLAG_NEED_RTNL,
2666 	},
2667 	{
2668 		.cmd = NL802154_CMD_SET_CCA_MODE,
2669 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2670 		.doit = nl802154_set_cca_mode,
2671 		.flags = GENL_ADMIN_PERM,
2672 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2673 				  NL802154_FLAG_NEED_RTNL,
2674 	},
2675 	{
2676 		.cmd = NL802154_CMD_SET_CCA_ED_LEVEL,
2677 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2678 		.doit = nl802154_set_cca_ed_level,
2679 		.flags = GENL_ADMIN_PERM,
2680 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2681 				  NL802154_FLAG_NEED_RTNL,
2682 	},
2683 	{
2684 		.cmd = NL802154_CMD_SET_TX_POWER,
2685 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2686 		.doit = nl802154_set_tx_power,
2687 		.flags = GENL_ADMIN_PERM,
2688 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2689 				  NL802154_FLAG_NEED_RTNL,
2690 	},
2691 	{
2692 		.cmd = NL802154_CMD_SET_WPAN_PHY_NETNS,
2693 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2694 		.doit = nl802154_wpan_phy_netns,
2695 		.flags = GENL_ADMIN_PERM,
2696 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2697 				  NL802154_FLAG_NEED_RTNL,
2698 	},
2699 	{
2700 		.cmd = NL802154_CMD_SET_PAN_ID,
2701 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2702 		.doit = nl802154_set_pan_id,
2703 		.flags = GENL_ADMIN_PERM,
2704 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2705 				  NL802154_FLAG_NEED_RTNL,
2706 	},
2707 	{
2708 		.cmd = NL802154_CMD_SET_SHORT_ADDR,
2709 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2710 		.doit = nl802154_set_short_addr,
2711 		.flags = GENL_ADMIN_PERM,
2712 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2713 				  NL802154_FLAG_NEED_RTNL,
2714 	},
2715 	{
2716 		.cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
2717 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2718 		.doit = nl802154_set_backoff_exponent,
2719 		.flags = GENL_ADMIN_PERM,
2720 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2721 				  NL802154_FLAG_NEED_RTNL,
2722 	},
2723 	{
2724 		.cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
2725 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2726 		.doit = nl802154_set_max_csma_backoffs,
2727 		.flags = GENL_ADMIN_PERM,
2728 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2729 				  NL802154_FLAG_NEED_RTNL,
2730 	},
2731 	{
2732 		.cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
2733 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2734 		.doit = nl802154_set_max_frame_retries,
2735 		.flags = GENL_ADMIN_PERM,
2736 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2737 				  NL802154_FLAG_NEED_RTNL,
2738 	},
2739 	{
2740 		.cmd = NL802154_CMD_SET_LBT_MODE,
2741 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2742 		.doit = nl802154_set_lbt_mode,
2743 		.flags = GENL_ADMIN_PERM,
2744 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2745 				  NL802154_FLAG_NEED_RTNL,
2746 	},
2747 	{
2748 		.cmd = NL802154_CMD_SET_ACKREQ_DEFAULT,
2749 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2750 		.doit = nl802154_set_ackreq_default,
2751 		.flags = GENL_ADMIN_PERM,
2752 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2753 				  NL802154_FLAG_NEED_RTNL,
2754 	},
2755 	{
2756 		.cmd = NL802154_CMD_TRIGGER_SCAN,
2757 		.doit = nl802154_trigger_scan,
2758 		.flags = GENL_ADMIN_PERM,
2759 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2760 				  NL802154_FLAG_CHECK_NETDEV_UP |
2761 				  NL802154_FLAG_NEED_RTNL,
2762 	},
2763 	{
2764 		.cmd = NL802154_CMD_ABORT_SCAN,
2765 		.doit = nl802154_abort_scan,
2766 		.flags = GENL_ADMIN_PERM,
2767 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2768 				  NL802154_FLAG_CHECK_NETDEV_UP |
2769 				  NL802154_FLAG_NEED_RTNL,
2770 	},
2771 	{
2772 		.cmd = NL802154_CMD_SEND_BEACONS,
2773 		.doit = nl802154_send_beacons,
2774 		.flags = GENL_ADMIN_PERM,
2775 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2776 				  NL802154_FLAG_CHECK_NETDEV_UP |
2777 				  NL802154_FLAG_NEED_RTNL,
2778 	},
2779 	{
2780 		.cmd = NL802154_CMD_STOP_BEACONS,
2781 		.doit = nl802154_stop_beacons,
2782 		.flags = GENL_ADMIN_PERM,
2783 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2784 				  NL802154_FLAG_CHECK_NETDEV_UP |
2785 				  NL802154_FLAG_NEED_RTNL,
2786 	},
2787 	{
2788 		.cmd = NL802154_CMD_ASSOCIATE,
2789 		.doit = nl802154_associate,
2790 		.flags = GENL_ADMIN_PERM,
2791 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2792 				  NL802154_FLAG_CHECK_NETDEV_UP |
2793 				  NL802154_FLAG_NEED_RTNL,
2794 	},
2795 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
2796 	{
2797 		.cmd = NL802154_CMD_SET_SEC_PARAMS,
2798 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2799 		.doit = nl802154_set_llsec_params,
2800 		.flags = GENL_ADMIN_PERM,
2801 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2802 				  NL802154_FLAG_NEED_RTNL,
2803 	},
2804 	{
2805 		.cmd = NL802154_CMD_GET_SEC_KEY,
2806 		.validate = GENL_DONT_VALIDATE_STRICT |
2807 			    GENL_DONT_VALIDATE_DUMP_STRICT,
2808 		/* TODO .doit by matching key id? */
2809 		.dumpit = nl802154_dump_llsec_key,
2810 		.flags = GENL_ADMIN_PERM,
2811 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2812 				  NL802154_FLAG_NEED_RTNL,
2813 	},
2814 	{
2815 		.cmd = NL802154_CMD_NEW_SEC_KEY,
2816 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2817 		.doit = nl802154_add_llsec_key,
2818 		.flags = GENL_ADMIN_PERM,
2819 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2820 				  NL802154_FLAG_NEED_RTNL,
2821 	},
2822 	{
2823 		.cmd = NL802154_CMD_DEL_SEC_KEY,
2824 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2825 		.doit = nl802154_del_llsec_key,
2826 		.flags = GENL_ADMIN_PERM,
2827 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2828 				  NL802154_FLAG_NEED_RTNL,
2829 	},
2830 	/* TODO unique identifier must short+pan OR extended_addr */
2831 	{
2832 		.cmd = NL802154_CMD_GET_SEC_DEV,
2833 		.validate = GENL_DONT_VALIDATE_STRICT |
2834 			    GENL_DONT_VALIDATE_DUMP_STRICT,
2835 		/* TODO .doit by matching extended_addr? */
2836 		.dumpit = nl802154_dump_llsec_dev,
2837 		.flags = GENL_ADMIN_PERM,
2838 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2839 				  NL802154_FLAG_NEED_RTNL,
2840 	},
2841 	{
2842 		.cmd = NL802154_CMD_NEW_SEC_DEV,
2843 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2844 		.doit = nl802154_add_llsec_dev,
2845 		.flags = GENL_ADMIN_PERM,
2846 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2847 				  NL802154_FLAG_NEED_RTNL,
2848 	},
2849 	{
2850 		.cmd = NL802154_CMD_DEL_SEC_DEV,
2851 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2852 		.doit = nl802154_del_llsec_dev,
2853 		.flags = GENL_ADMIN_PERM,
2854 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2855 				  NL802154_FLAG_NEED_RTNL,
2856 	},
2857 	/* TODO remove complete devkey, put it as nested? */
2858 	{
2859 		.cmd = NL802154_CMD_GET_SEC_DEVKEY,
2860 		.validate = GENL_DONT_VALIDATE_STRICT |
2861 			    GENL_DONT_VALIDATE_DUMP_STRICT,
2862 		/* TODO doit by matching ??? */
2863 		.dumpit = nl802154_dump_llsec_devkey,
2864 		.flags = GENL_ADMIN_PERM,
2865 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2866 				  NL802154_FLAG_NEED_RTNL,
2867 	},
2868 	{
2869 		.cmd = NL802154_CMD_NEW_SEC_DEVKEY,
2870 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2871 		.doit = nl802154_add_llsec_devkey,
2872 		.flags = GENL_ADMIN_PERM,
2873 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2874 				  NL802154_FLAG_NEED_RTNL,
2875 	},
2876 	{
2877 		.cmd = NL802154_CMD_DEL_SEC_DEVKEY,
2878 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2879 		.doit = nl802154_del_llsec_devkey,
2880 		.flags = GENL_ADMIN_PERM,
2881 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2882 				  NL802154_FLAG_NEED_RTNL,
2883 	},
2884 	{
2885 		.cmd = NL802154_CMD_GET_SEC_LEVEL,
2886 		.validate = GENL_DONT_VALIDATE_STRICT |
2887 			    GENL_DONT_VALIDATE_DUMP_STRICT,
2888 		/* TODO .doit by matching frame_type? */
2889 		.dumpit = nl802154_dump_llsec_seclevel,
2890 		.flags = GENL_ADMIN_PERM,
2891 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2892 				  NL802154_FLAG_NEED_RTNL,
2893 	},
2894 	{
2895 		.cmd = NL802154_CMD_NEW_SEC_LEVEL,
2896 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2897 		.doit = nl802154_add_llsec_seclevel,
2898 		.flags = GENL_ADMIN_PERM,
2899 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2900 				  NL802154_FLAG_NEED_RTNL,
2901 	},
2902 	{
2903 		.cmd = NL802154_CMD_DEL_SEC_LEVEL,
2904 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2905 		/* TODO match frame_type only? */
2906 		.doit = nl802154_del_llsec_seclevel,
2907 		.flags = GENL_ADMIN_PERM,
2908 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2909 				  NL802154_FLAG_NEED_RTNL,
2910 	},
2911 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2912 };
2913 
2914 static struct genl_family nl802154_fam __ro_after_init = {
2915 	.name = NL802154_GENL_NAME,	/* have users key off the name instead */
2916 	.hdrsize = 0,			/* no private header */
2917 	.version = 1,			/* no particular meaning now */
2918 	.maxattr = NL802154_ATTR_MAX,
2919 	.policy = nl802154_policy,
2920 	.netnsok = true,
2921 	.pre_doit = nl802154_pre_doit,
2922 	.post_doit = nl802154_post_doit,
2923 	.module = THIS_MODULE,
2924 	.ops = nl802154_ops,
2925 	.n_ops = ARRAY_SIZE(nl802154_ops),
2926 	.resv_start_op = NL802154_CMD_DEL_SEC_LEVEL + 1,
2927 	.mcgrps = nl802154_mcgrps,
2928 	.n_mcgrps = ARRAY_SIZE(nl802154_mcgrps),
2929 };
2930 
2931 /* initialisation/exit functions */
2932 int __init nl802154_init(void)
2933 {
2934 	return genl_register_family(&nl802154_fam);
2935 }
2936 
2937 void nl802154_exit(void)
2938 {
2939 	genl_unregister_family(&nl802154_fam);
2940 }
2941