xref: /linux/net/ieee802154/nl802154.c (revision 42d37fc0c819b81f6f6afd108b55d04ba9d32d0f)
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 	[NL802154_ATTR_MAX_ASSOCIATIONS] = { .type = NLA_U32 },
238 	[NL802154_ATTR_PEER] = { .type = NLA_NESTED },
239 
240 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
241 	[NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
242 	[NL802154_ATTR_SEC_OUT_LEVEL] = { .type = NLA_U32, },
243 	[NL802154_ATTR_SEC_OUT_KEY_ID] = { .type = NLA_NESTED, },
244 	[NL802154_ATTR_SEC_FRAME_COUNTER] = { .type = NLA_U32 },
245 
246 	[NL802154_ATTR_SEC_LEVEL] = { .type = NLA_NESTED },
247 	[NL802154_ATTR_SEC_DEVICE] = { .type = NLA_NESTED },
248 	[NL802154_ATTR_SEC_DEVKEY] = { .type = NLA_NESTED },
249 	[NL802154_ATTR_SEC_KEY] = { .type = NLA_NESTED },
250 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
251 };
252 
253 static int
254 nl802154_prepare_wpan_dev_dump(struct sk_buff *skb,
255 			       struct netlink_callback *cb,
256 			       struct cfg802154_registered_device **rdev,
257 			       struct wpan_dev **wpan_dev)
258 {
259 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
260 	int err;
261 
262 	rtnl_lock();
263 
264 	if (!cb->args[0]) {
265 		*wpan_dev = __cfg802154_wpan_dev_from_attrs(sock_net(skb->sk),
266 							    info->info.attrs);
267 		if (IS_ERR(*wpan_dev)) {
268 			err = PTR_ERR(*wpan_dev);
269 			goto out_unlock;
270 		}
271 		*rdev = wpan_phy_to_rdev((*wpan_dev)->wpan_phy);
272 		/* 0 is the first index - add 1 to parse only once */
273 		cb->args[0] = (*rdev)->wpan_phy_idx + 1;
274 		cb->args[1] = (*wpan_dev)->identifier;
275 	} else {
276 		/* subtract the 1 again here */
277 		struct wpan_phy *wpan_phy = wpan_phy_idx_to_wpan_phy(cb->args[0] - 1);
278 		struct wpan_dev *tmp;
279 
280 		if (!wpan_phy) {
281 			err = -ENODEV;
282 			goto out_unlock;
283 		}
284 		*rdev = wpan_phy_to_rdev(wpan_phy);
285 		*wpan_dev = NULL;
286 
287 		list_for_each_entry(tmp, &(*rdev)->wpan_dev_list, list) {
288 			if (tmp->identifier == cb->args[1]) {
289 				*wpan_dev = tmp;
290 				break;
291 			}
292 		}
293 
294 		if (!*wpan_dev) {
295 			err = -ENODEV;
296 			goto out_unlock;
297 		}
298 	}
299 
300 	return 0;
301  out_unlock:
302 	rtnl_unlock();
303 	return err;
304 }
305 
306 static void
307 nl802154_finish_wpan_dev_dump(struct cfg802154_registered_device *rdev)
308 {
309 	rtnl_unlock();
310 }
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 	/* Only allow changing the PAN ID when the device has no more
1091 	 * associations ongoing to avoid confusing peers.
1092 	 */
1093 	if (cfg802154_device_is_associated(wpan_dev)) {
1094 		NL_SET_ERR_MSG(info->extack,
1095 			       "Existing associations, changing PAN ID forbidden");
1096 		return -EINVAL;
1097 	}
1098 
1099 	return rdev_set_pan_id(rdev, wpan_dev, pan_id);
1100 }
1101 
1102 static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info)
1103 {
1104 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1105 	struct net_device *dev = info->user_ptr[1];
1106 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1107 	__le16 short_addr;
1108 
1109 	/* conflict here while tx/rx calls */
1110 	if (netif_running(dev))
1111 		return -EBUSY;
1112 
1113 	if (wpan_dev->lowpan_dev) {
1114 		if (netif_running(wpan_dev->lowpan_dev))
1115 			return -EBUSY;
1116 	}
1117 
1118 	/* don't change address fields on monitor */
1119 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1120 	    !info->attrs[NL802154_ATTR_SHORT_ADDR])
1121 		return -EINVAL;
1122 
1123 	short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
1124 
1125 	/* The short address only has a meaning when part of a PAN, after a
1126 	 * proper association procedure. However, we want to still offer the
1127 	 * possibility to create static networks so changing the short address
1128 	 * is only allowed when not already associated to other devices with
1129 	 * the official handshake.
1130 	 */
1131 	if (cfg802154_device_is_associated(wpan_dev)) {
1132 		NL_SET_ERR_MSG(info->extack,
1133 			       "Existing associations, changing short address forbidden");
1134 		return -EINVAL;
1135 	}
1136 
1137 	return rdev_set_short_addr(rdev, wpan_dev, short_addr);
1138 }
1139 
1140 static int
1141 nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info)
1142 {
1143 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1144 	struct net_device *dev = info->user_ptr[1];
1145 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1146 	u8 min_be, max_be;
1147 
1148 	/* should be set on netif open inside phy settings */
1149 	if (netif_running(dev))
1150 		return -EBUSY;
1151 
1152 	if (!info->attrs[NL802154_ATTR_MIN_BE] ||
1153 	    !info->attrs[NL802154_ATTR_MAX_BE])
1154 		return -EINVAL;
1155 
1156 	min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]);
1157 	max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]);
1158 
1159 	/* check 802.15.4 constraints */
1160 	if (min_be < rdev->wpan_phy.supported.min_minbe ||
1161 	    min_be > rdev->wpan_phy.supported.max_minbe ||
1162 	    max_be < rdev->wpan_phy.supported.min_maxbe ||
1163 	    max_be > rdev->wpan_phy.supported.max_maxbe ||
1164 	    min_be > max_be)
1165 		return -EINVAL;
1166 
1167 	return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
1168 }
1169 
1170 static int
1171 nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info)
1172 {
1173 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1174 	struct net_device *dev = info->user_ptr[1];
1175 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1176 	u8 max_csma_backoffs;
1177 
1178 	/* conflict here while other running iface settings */
1179 	if (netif_running(dev))
1180 		return -EBUSY;
1181 
1182 	if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS])
1183 		return -EINVAL;
1184 
1185 	max_csma_backoffs = nla_get_u8(
1186 			info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]);
1187 
1188 	/* check 802.15.4 constraints */
1189 	if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs ||
1190 	    max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs)
1191 		return -EINVAL;
1192 
1193 	return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
1194 }
1195 
1196 static int
1197 nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info)
1198 {
1199 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1200 	struct net_device *dev = info->user_ptr[1];
1201 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1202 	s8 max_frame_retries;
1203 
1204 	if (netif_running(dev))
1205 		return -EBUSY;
1206 
1207 	if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES])
1208 		return -EINVAL;
1209 
1210 	max_frame_retries = nla_get_s8(
1211 			info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]);
1212 
1213 	/* check 802.15.4 constraints */
1214 	if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries ||
1215 	    max_frame_retries > rdev->wpan_phy.supported.max_frame_retries)
1216 		return -EINVAL;
1217 
1218 	return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
1219 }
1220 
1221 static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info)
1222 {
1223 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1224 	struct net_device *dev = info->user_ptr[1];
1225 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1226 	int mode;
1227 
1228 	if (netif_running(dev))
1229 		return -EBUSY;
1230 
1231 	if (!info->attrs[NL802154_ATTR_LBT_MODE])
1232 		return -EINVAL;
1233 
1234 	mode = nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
1235 
1236 	if (mode != 0 && mode != 1)
1237 		return -EINVAL;
1238 
1239 	if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt))
1240 		return -EINVAL;
1241 
1242 	return rdev_set_lbt_mode(rdev, wpan_dev, mode);
1243 }
1244 
1245 static int
1246 nl802154_set_ackreq_default(struct sk_buff *skb, struct genl_info *info)
1247 {
1248 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1249 	struct net_device *dev = info->user_ptr[1];
1250 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1251 	int ackreq;
1252 
1253 	if (netif_running(dev))
1254 		return -EBUSY;
1255 
1256 	if (!info->attrs[NL802154_ATTR_ACKREQ_DEFAULT])
1257 		return -EINVAL;
1258 
1259 	ackreq = nla_get_u8(info->attrs[NL802154_ATTR_ACKREQ_DEFAULT]);
1260 
1261 	if (ackreq != 0 && ackreq != 1)
1262 		return -EINVAL;
1263 
1264 	return rdev_set_ackreq_default(rdev, wpan_dev, ackreq);
1265 }
1266 
1267 static int nl802154_wpan_phy_netns(struct sk_buff *skb, struct genl_info *info)
1268 {
1269 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1270 	struct net *net;
1271 	int err;
1272 
1273 	if (info->attrs[NL802154_ATTR_PID]) {
1274 		u32 pid = nla_get_u32(info->attrs[NL802154_ATTR_PID]);
1275 
1276 		net = get_net_ns_by_pid(pid);
1277 	} else if (info->attrs[NL802154_ATTR_NETNS_FD]) {
1278 		u32 fd = nla_get_u32(info->attrs[NL802154_ATTR_NETNS_FD]);
1279 
1280 		net = get_net_ns_by_fd(fd);
1281 	} else {
1282 		return -EINVAL;
1283 	}
1284 
1285 	if (IS_ERR(net))
1286 		return PTR_ERR(net);
1287 
1288 	err = 0;
1289 
1290 	/* check if anything to do */
1291 	if (!net_eq(wpan_phy_net(&rdev->wpan_phy), net))
1292 		err = cfg802154_switch_netns(rdev, net);
1293 
1294 	put_net(net);
1295 	return err;
1296 }
1297 
1298 static int nl802154_prep_scan_event_msg(struct sk_buff *msg,
1299 					struct cfg802154_registered_device *rdev,
1300 					struct wpan_dev *wpan_dev,
1301 					u32 portid, u32 seq, int flags, u8 cmd,
1302 					struct ieee802154_coord_desc *desc)
1303 {
1304 	struct nlattr *nla;
1305 	void *hdr;
1306 
1307 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1308 	if (!hdr)
1309 		return -ENOBUFS;
1310 
1311 	if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx))
1312 		goto nla_put_failure;
1313 
1314 	if (wpan_dev->netdev &&
1315 	    nla_put_u32(msg, NL802154_ATTR_IFINDEX, wpan_dev->netdev->ifindex))
1316 		goto nla_put_failure;
1317 
1318 	if (nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
1319 			      wpan_dev_id(wpan_dev), NL802154_ATTR_PAD))
1320 		goto nla_put_failure;
1321 
1322 	nla = nla_nest_start_noflag(msg, NL802154_ATTR_COORDINATOR);
1323 	if (!nla)
1324 		goto nla_put_failure;
1325 
1326 	if (nla_put(msg, NL802154_COORD_PANID, IEEE802154_PAN_ID_LEN,
1327 		    &desc->addr.pan_id))
1328 		goto nla_put_failure;
1329 
1330 	if (desc->addr.mode == IEEE802154_ADDR_SHORT) {
1331 		if (nla_put(msg, NL802154_COORD_ADDR,
1332 			    IEEE802154_SHORT_ADDR_LEN,
1333 			    &desc->addr.short_addr))
1334 			goto nla_put_failure;
1335 	} else {
1336 		if (nla_put(msg, NL802154_COORD_ADDR,
1337 			    IEEE802154_EXTENDED_ADDR_LEN,
1338 			    &desc->addr.extended_addr))
1339 			goto nla_put_failure;
1340 	}
1341 
1342 	if (nla_put_u8(msg, NL802154_COORD_CHANNEL, desc->channel))
1343 		goto nla_put_failure;
1344 
1345 	if (nla_put_u8(msg, NL802154_COORD_PAGE, desc->page))
1346 		goto nla_put_failure;
1347 
1348 	if (nla_put_u16(msg, NL802154_COORD_SUPERFRAME_SPEC,
1349 			desc->superframe_spec))
1350 		goto nla_put_failure;
1351 
1352 	if (nla_put_u8(msg, NL802154_COORD_LINK_QUALITY, desc->link_quality))
1353 		goto nla_put_failure;
1354 
1355 	if (desc->gts_permit && nla_put_flag(msg, NL802154_COORD_GTS_PERMIT))
1356 		goto nla_put_failure;
1357 
1358 	/* TODO: NL802154_COORD_PAYLOAD_DATA if any */
1359 
1360 	nla_nest_end(msg, nla);
1361 
1362 	genlmsg_end(msg, hdr);
1363 
1364 	return 0;
1365 
1366  nla_put_failure:
1367 	genlmsg_cancel(msg, hdr);
1368 
1369 	return -EMSGSIZE;
1370 }
1371 
1372 int nl802154_scan_event(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
1373 			struct ieee802154_coord_desc *desc)
1374 {
1375 	struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(wpan_phy);
1376 	struct sk_buff *msg;
1377 	int ret;
1378 
1379 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1380 	if (!msg)
1381 		return -ENOMEM;
1382 
1383 	ret = nl802154_prep_scan_event_msg(msg, rdev, wpan_dev, 0, 0, 0,
1384 					   NL802154_CMD_SCAN_EVENT,
1385 					   desc);
1386 	if (ret < 0) {
1387 		nlmsg_free(msg);
1388 		return ret;
1389 	}
1390 
1391 	return genlmsg_multicast_netns(&nl802154_fam, wpan_phy_net(wpan_phy),
1392 				       msg, 0, NL802154_MCGRP_SCAN, GFP_ATOMIC);
1393 }
1394 EXPORT_SYMBOL_GPL(nl802154_scan_event);
1395 
1396 static int nl802154_trigger_scan(struct sk_buff *skb, struct genl_info *info)
1397 {
1398 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1399 	struct net_device *dev = info->user_ptr[1];
1400 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1401 	struct wpan_phy *wpan_phy = &rdev->wpan_phy;
1402 	struct cfg802154_scan_request *request;
1403 	u8 type;
1404 	int err;
1405 
1406 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1407 		NL_SET_ERR_MSG(info->extack, "Monitors are not allowed to perform scans");
1408 		return -EOPNOTSUPP;
1409 	}
1410 
1411 	if (!info->attrs[NL802154_ATTR_SCAN_TYPE]) {
1412 		NL_SET_ERR_MSG(info->extack, "Malformed request, missing scan type");
1413 		return -EINVAL;
1414 	}
1415 
1416 	if (wpan_phy->flags & WPAN_PHY_FLAG_DATAGRAMS_ONLY) {
1417 		NL_SET_ERR_MSG(info->extack, "PHY only supports datagrams");
1418 		return -EOPNOTSUPP;
1419 	}
1420 
1421 	request = kzalloc(sizeof(*request), GFP_KERNEL);
1422 	if (!request)
1423 		return -ENOMEM;
1424 
1425 	request->wpan_dev = wpan_dev;
1426 	request->wpan_phy = wpan_phy;
1427 
1428 	type = nla_get_u8(info->attrs[NL802154_ATTR_SCAN_TYPE]);
1429 	switch (type) {
1430 	case NL802154_SCAN_ACTIVE:
1431 	case NL802154_SCAN_PASSIVE:
1432 		request->type = type;
1433 		break;
1434 	default:
1435 		NL_SET_ERR_MSG_FMT(info->extack, "Unsupported scan type: %d", type);
1436 		err = -EINVAL;
1437 		goto free_request;
1438 	}
1439 
1440 	/* Use current page by default */
1441 	if (info->attrs[NL802154_ATTR_PAGE])
1442 		request->page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
1443 	else
1444 		request->page = wpan_phy->current_page;
1445 
1446 	/* Scan all supported channels by default */
1447 	if (info->attrs[NL802154_ATTR_SCAN_CHANNELS])
1448 		request->channels = nla_get_u32(info->attrs[NL802154_ATTR_SCAN_CHANNELS]);
1449 	else
1450 		request->channels = wpan_phy->supported.channels[request->page];
1451 
1452 	/* Use maximum duration order by default */
1453 	if (info->attrs[NL802154_ATTR_SCAN_DURATION])
1454 		request->duration = nla_get_u8(info->attrs[NL802154_ATTR_SCAN_DURATION]);
1455 	else
1456 		request->duration = IEEE802154_MAX_SCAN_DURATION;
1457 
1458 	err = rdev_trigger_scan(rdev, request);
1459 	if (err) {
1460 		pr_err("Failure starting scanning (%d)\n", err);
1461 		goto free_request;
1462 	}
1463 
1464 	return 0;
1465 
1466 free_request:
1467 	kfree(request);
1468 
1469 	return err;
1470 }
1471 
1472 static int nl802154_prep_scan_msg(struct sk_buff *msg,
1473 				  struct cfg802154_registered_device *rdev,
1474 				  struct wpan_dev *wpan_dev, u32 portid,
1475 				  u32 seq, int flags, u8 cmd, u8 arg)
1476 {
1477 	void *hdr;
1478 
1479 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1480 	if (!hdr)
1481 		return -ENOBUFS;
1482 
1483 	if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx))
1484 		goto nla_put_failure;
1485 
1486 	if (wpan_dev->netdev &&
1487 	    nla_put_u32(msg, NL802154_ATTR_IFINDEX, wpan_dev->netdev->ifindex))
1488 		goto nla_put_failure;
1489 
1490 	if (nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
1491 			      wpan_dev_id(wpan_dev), NL802154_ATTR_PAD))
1492 		goto nla_put_failure;
1493 
1494 	if (cmd == NL802154_CMD_SCAN_DONE &&
1495 	    nla_put_u8(msg, NL802154_ATTR_SCAN_DONE_REASON, arg))
1496 		goto nla_put_failure;
1497 
1498 	genlmsg_end(msg, hdr);
1499 
1500 	return 0;
1501 
1502 nla_put_failure:
1503 	genlmsg_cancel(msg, hdr);
1504 
1505 	return -EMSGSIZE;
1506 }
1507 
1508 static int nl802154_send_scan_msg(struct cfg802154_registered_device *rdev,
1509 				  struct wpan_dev *wpan_dev, u8 cmd, u8 arg)
1510 {
1511 	struct sk_buff *msg;
1512 	int ret;
1513 
1514 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1515 	if (!msg)
1516 		return -ENOMEM;
1517 
1518 	ret = nl802154_prep_scan_msg(msg, rdev, wpan_dev, 0, 0, 0, cmd, arg);
1519 	if (ret < 0) {
1520 		nlmsg_free(msg);
1521 		return ret;
1522 	}
1523 
1524 	return genlmsg_multicast_netns(&nl802154_fam,
1525 				       wpan_phy_net(&rdev->wpan_phy), msg, 0,
1526 				       NL802154_MCGRP_SCAN, GFP_KERNEL);
1527 }
1528 
1529 int nl802154_scan_started(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev)
1530 {
1531 	struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(wpan_phy);
1532 	int err;
1533 
1534 	/* Ignore errors when there are no listeners */
1535 	err = nl802154_send_scan_msg(rdev, wpan_dev, NL802154_CMD_TRIGGER_SCAN, 0);
1536 	if (err == -ESRCH)
1537 		err = 0;
1538 
1539 	return err;
1540 }
1541 EXPORT_SYMBOL_GPL(nl802154_scan_started);
1542 
1543 int nl802154_scan_done(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
1544 		       enum nl802154_scan_done_reasons reason)
1545 {
1546 	struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(wpan_phy);
1547 	int err;
1548 
1549 	/* Ignore errors when there are no listeners */
1550 	err = nl802154_send_scan_msg(rdev, wpan_dev, NL802154_CMD_SCAN_DONE, reason);
1551 	if (err == -ESRCH)
1552 		err = 0;
1553 
1554 	return err;
1555 }
1556 EXPORT_SYMBOL_GPL(nl802154_scan_done);
1557 
1558 static int nl802154_abort_scan(struct sk_buff *skb, struct genl_info *info)
1559 {
1560 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1561 	struct net_device *dev = info->user_ptr[1];
1562 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1563 
1564 	/* Resources are released in the notification helper above */
1565 	return rdev_abort_scan(rdev, wpan_dev);
1566 }
1567 
1568 static int
1569 nl802154_send_beacons(struct sk_buff *skb, struct genl_info *info)
1570 {
1571 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1572 	struct net_device *dev = info->user_ptr[1];
1573 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1574 	struct wpan_phy *wpan_phy = &rdev->wpan_phy;
1575 	struct cfg802154_beacon_request *request;
1576 	int err;
1577 
1578 	if (wpan_dev->iftype != NL802154_IFTYPE_COORD) {
1579 		NL_SET_ERR_MSG(info->extack, "Only coordinators can send beacons");
1580 		return -EOPNOTSUPP;
1581 	}
1582 
1583 	if (wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
1584 		NL_SET_ERR_MSG(info->extack, "Device is not part of any PAN");
1585 		return -EPERM;
1586 	}
1587 
1588 	if (wpan_phy->flags & WPAN_PHY_FLAG_DATAGRAMS_ONLY) {
1589 		NL_SET_ERR_MSG(info->extack, "PHY only supports datagrams");
1590 		return -EOPNOTSUPP;
1591 	}
1592 
1593 	request = kzalloc(sizeof(*request), GFP_KERNEL);
1594 	if (!request)
1595 		return -ENOMEM;
1596 
1597 	request->wpan_dev = wpan_dev;
1598 	request->wpan_phy = wpan_phy;
1599 
1600 	/* Use maximum duration order by default */
1601 	if (info->attrs[NL802154_ATTR_BEACON_INTERVAL])
1602 		request->interval = nla_get_u8(info->attrs[NL802154_ATTR_BEACON_INTERVAL]);
1603 	else
1604 		request->interval = IEEE802154_MAX_SCAN_DURATION;
1605 
1606 	err = rdev_send_beacons(rdev, request);
1607 	if (err) {
1608 		pr_err("Failure starting sending beacons (%d)\n", err);
1609 		goto free_request;
1610 	}
1611 
1612 	return 0;
1613 
1614 free_request:
1615 	kfree(request);
1616 
1617 	return err;
1618 }
1619 
1620 void nl802154_beaconing_done(struct wpan_dev *wpan_dev)
1621 {
1622 	/* NOP */
1623 }
1624 EXPORT_SYMBOL_GPL(nl802154_beaconing_done);
1625 
1626 static int
1627 nl802154_stop_beacons(struct sk_buff *skb, struct genl_info *info)
1628 {
1629 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1630 	struct net_device *dev = info->user_ptr[1];
1631 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1632 
1633 	/* Resources are released in the notification helper above */
1634 	return rdev_stop_beacons(rdev, wpan_dev);
1635 }
1636 
1637 static int nl802154_associate(struct sk_buff *skb, struct genl_info *info)
1638 {
1639 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1640 	struct net_device *dev = info->user_ptr[1];
1641 	struct wpan_dev *wpan_dev;
1642 	struct wpan_phy *wpan_phy;
1643 	struct ieee802154_addr coord;
1644 	int err;
1645 
1646 	wpan_dev = dev->ieee802154_ptr;
1647 	wpan_phy = &rdev->wpan_phy;
1648 
1649 	if (wpan_phy->flags & WPAN_PHY_FLAG_DATAGRAMS_ONLY) {
1650 		NL_SET_ERR_MSG(info->extack, "PHY only supports datagrams");
1651 		return -EOPNOTSUPP;
1652 	}
1653 
1654 	if (!info->attrs[NL802154_ATTR_PAN_ID] ||
1655 	    !info->attrs[NL802154_ATTR_EXTENDED_ADDR])
1656 		return -EINVAL;
1657 
1658 	coord.pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
1659 	coord.mode = IEEE802154_ADDR_LONG;
1660 	coord.extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
1661 
1662 	mutex_lock(&wpan_dev->association_lock);
1663 	err = rdev_associate(rdev, wpan_dev, &coord);
1664 	mutex_unlock(&wpan_dev->association_lock);
1665 	if (err)
1666 		pr_err("Association with PAN ID 0x%x failed (%d)\n",
1667 		       le16_to_cpu(coord.pan_id), err);
1668 
1669 	return err;
1670 }
1671 
1672 static int nl802154_disassociate(struct sk_buff *skb, struct genl_info *info)
1673 {
1674 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1675 	struct net_device *dev = info->user_ptr[1];
1676 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1677 	struct wpan_phy *wpan_phy = &rdev->wpan_phy;
1678 	struct ieee802154_addr target;
1679 
1680 	if (wpan_phy->flags & WPAN_PHY_FLAG_DATAGRAMS_ONLY) {
1681 		NL_SET_ERR_MSG(info->extack, "PHY only supports datagrams");
1682 		return -EOPNOTSUPP;
1683 	}
1684 
1685 	target.pan_id = wpan_dev->pan_id;
1686 
1687 	if (info->attrs[NL802154_ATTR_EXTENDED_ADDR]) {
1688 		target.mode = IEEE802154_ADDR_LONG;
1689 		target.extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
1690 	} else if (info->attrs[NL802154_ATTR_SHORT_ADDR]) {
1691 		target.mode = IEEE802154_ADDR_SHORT;
1692 		target.short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
1693 	} else {
1694 		NL_SET_ERR_MSG(info->extack, "Device address is missing");
1695 		return -EINVAL;
1696 	}
1697 
1698 	mutex_lock(&wpan_dev->association_lock);
1699 	rdev_disassociate(rdev, wpan_dev, &target);
1700 	mutex_unlock(&wpan_dev->association_lock);
1701 
1702 	return 0;
1703 }
1704 
1705 static int nl802154_set_max_associations(struct sk_buff *skb, struct genl_info *info)
1706 {
1707 	struct net_device *dev = info->user_ptr[1];
1708 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1709 	unsigned int max_assoc;
1710 
1711 	if (!info->attrs[NL802154_ATTR_MAX_ASSOCIATIONS]) {
1712 		NL_SET_ERR_MSG(info->extack, "No maximum number of association given");
1713 		return -EINVAL;
1714 	}
1715 
1716 	max_assoc = nla_get_u32(info->attrs[NL802154_ATTR_MAX_ASSOCIATIONS]);
1717 
1718 	mutex_lock(&wpan_dev->association_lock);
1719 	cfg802154_set_max_associations(wpan_dev, max_assoc);
1720 	mutex_unlock(&wpan_dev->association_lock);
1721 
1722 	return 0;
1723 }
1724 
1725 static int nl802154_send_peer_info(struct sk_buff *msg,
1726 				   struct netlink_callback *cb,
1727 				   u32 seq, int flags,
1728 				   struct cfg802154_registered_device *rdev,
1729 				   struct wpan_dev *wpan_dev,
1730 				   struct ieee802154_pan_device *peer,
1731 				   enum nl802154_peer_type type)
1732 {
1733 	struct nlattr *nla;
1734 	void *hdr;
1735 
1736 	ASSERT_RTNL();
1737 
1738 	hdr = nl802154hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
1739 			      NL802154_CMD_LIST_ASSOCIATIONS);
1740 	if (!hdr)
1741 		return -ENOBUFS;
1742 
1743 	genl_dump_check_consistent(cb, hdr);
1744 
1745 	nla = nla_nest_start_noflag(msg, NL802154_ATTR_PEER);
1746 	if (!nla)
1747 		goto nla_put_failure;
1748 
1749 	if (nla_put_u8(msg, NL802154_DEV_ADDR_ATTR_PEER_TYPE, type))
1750 		goto nla_put_failure;
1751 
1752 	if (nla_put_u8(msg, NL802154_DEV_ADDR_ATTR_MODE, peer->mode))
1753 		goto nla_put_failure;
1754 
1755 	if (nla_put(msg, NL802154_DEV_ADDR_ATTR_SHORT,
1756 		    IEEE802154_SHORT_ADDR_LEN, &peer->short_addr))
1757 		goto nla_put_failure;
1758 
1759 	if (nla_put(msg, NL802154_DEV_ADDR_ATTR_EXTENDED,
1760 		    IEEE802154_EXTENDED_ADDR_LEN, &peer->extended_addr))
1761 		goto nla_put_failure;
1762 
1763 	nla_nest_end(msg, nla);
1764 
1765 	genlmsg_end(msg, hdr);
1766 
1767 	return 0;
1768 
1769  nla_put_failure:
1770 	genlmsg_cancel(msg, hdr);
1771 	return -EMSGSIZE;
1772 }
1773 
1774 static int nl802154_list_associations(struct sk_buff *skb,
1775 				      struct netlink_callback *cb)
1776 {
1777 	struct cfg802154_registered_device *rdev;
1778 	struct ieee802154_pan_device *child;
1779 	struct wpan_dev *wpan_dev;
1780 	int err;
1781 
1782 	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1783 	if (err)
1784 		return err;
1785 
1786 	mutex_lock(&wpan_dev->association_lock);
1787 
1788 	if (cb->args[2])
1789 		goto out;
1790 
1791 	if (wpan_dev->parent) {
1792 		err = nl802154_send_peer_info(skb, cb, cb->nlh->nlmsg_seq,
1793 					      NLM_F_MULTI, rdev, wpan_dev,
1794 					      wpan_dev->parent,
1795 					      NL802154_PEER_TYPE_PARENT);
1796 		if (err < 0)
1797 			goto out_err;
1798 	}
1799 
1800 	list_for_each_entry(child, &wpan_dev->children, node) {
1801 		err = nl802154_send_peer_info(skb, cb, cb->nlh->nlmsg_seq,
1802 					      NLM_F_MULTI, rdev, wpan_dev,
1803 					      child,
1804 					      NL802154_PEER_TYPE_CHILD);
1805 		if (err < 0)
1806 			goto out_err;
1807 	}
1808 
1809 	cb->args[2] = 1;
1810 out:
1811 	err = skb->len;
1812 out_err:
1813 	mutex_unlock(&wpan_dev->association_lock);
1814 
1815 	nl802154_finish_wpan_dev_dump(rdev);
1816 
1817 	return err;
1818 }
1819 
1820 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
1821 static const struct nla_policy nl802154_dev_addr_policy[NL802154_DEV_ADDR_ATTR_MAX + 1] = {
1822 	[NL802154_DEV_ADDR_ATTR_PAN_ID] = { .type = NLA_U16 },
1823 	[NL802154_DEV_ADDR_ATTR_MODE] = { .type = NLA_U32 },
1824 	[NL802154_DEV_ADDR_ATTR_SHORT] = { .type = NLA_U16 },
1825 	[NL802154_DEV_ADDR_ATTR_EXTENDED] = { .type = NLA_U64 },
1826 };
1827 
1828 static int
1829 ieee802154_llsec_parse_dev_addr(struct nlattr *nla,
1830 				struct ieee802154_addr *addr)
1831 {
1832 	struct nlattr *attrs[NL802154_DEV_ADDR_ATTR_MAX + 1];
1833 
1834 	if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ADDR_ATTR_MAX, nla, nl802154_dev_addr_policy, NULL))
1835 		return -EINVAL;
1836 
1837 	if (!attrs[NL802154_DEV_ADDR_ATTR_PAN_ID] || !attrs[NL802154_DEV_ADDR_ATTR_MODE])
1838 		return -EINVAL;
1839 
1840 	addr->pan_id = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_PAN_ID]);
1841 	addr->mode = nla_get_u32(attrs[NL802154_DEV_ADDR_ATTR_MODE]);
1842 	switch (addr->mode) {
1843 	case NL802154_DEV_ADDR_SHORT:
1844 		if (!attrs[NL802154_DEV_ADDR_ATTR_SHORT])
1845 			return -EINVAL;
1846 		addr->short_addr = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_SHORT]);
1847 		break;
1848 	case NL802154_DEV_ADDR_EXTENDED:
1849 		if (!attrs[NL802154_DEV_ADDR_ATTR_EXTENDED])
1850 			return -EINVAL;
1851 		addr->extended_addr = nla_get_le64(attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]);
1852 		break;
1853 	default:
1854 		return -EINVAL;
1855 	}
1856 
1857 	return 0;
1858 }
1859 
1860 static const struct nla_policy nl802154_key_id_policy[NL802154_KEY_ID_ATTR_MAX + 1] = {
1861 	[NL802154_KEY_ID_ATTR_MODE] = { .type = NLA_U32 },
1862 	[NL802154_KEY_ID_ATTR_INDEX] = { .type = NLA_U8 },
1863 	[NL802154_KEY_ID_ATTR_IMPLICIT] = { .type = NLA_NESTED },
1864 	[NL802154_KEY_ID_ATTR_SOURCE_SHORT] = { .type = NLA_U32 },
1865 	[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED] = { .type = NLA_U64 },
1866 };
1867 
1868 static int
1869 ieee802154_llsec_parse_key_id(struct nlattr *nla,
1870 			      struct ieee802154_llsec_key_id *desc)
1871 {
1872 	struct nlattr *attrs[NL802154_KEY_ID_ATTR_MAX + 1];
1873 
1874 	if (!nla || nla_parse_nested_deprecated(attrs, NL802154_KEY_ID_ATTR_MAX, nla, nl802154_key_id_policy, NULL))
1875 		return -EINVAL;
1876 
1877 	if (!attrs[NL802154_KEY_ID_ATTR_MODE])
1878 		return -EINVAL;
1879 
1880 	desc->mode = nla_get_u32(attrs[NL802154_KEY_ID_ATTR_MODE]);
1881 	switch (desc->mode) {
1882 	case NL802154_KEY_ID_MODE_IMPLICIT:
1883 		if (!attrs[NL802154_KEY_ID_ATTR_IMPLICIT])
1884 			return -EINVAL;
1885 
1886 		if (ieee802154_llsec_parse_dev_addr(attrs[NL802154_KEY_ID_ATTR_IMPLICIT],
1887 						    &desc->device_addr) < 0)
1888 			return -EINVAL;
1889 		break;
1890 	case NL802154_KEY_ID_MODE_INDEX:
1891 		break;
1892 	case NL802154_KEY_ID_MODE_INDEX_SHORT:
1893 		if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT])
1894 			return -EINVAL;
1895 
1896 		desc->short_source = nla_get_le32(attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT]);
1897 		break;
1898 	case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
1899 		if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED])
1900 			return -EINVAL;
1901 
1902 		desc->extended_source = nla_get_le64(attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED]);
1903 		break;
1904 	default:
1905 		return -EINVAL;
1906 	}
1907 
1908 	if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
1909 		if (!attrs[NL802154_KEY_ID_ATTR_INDEX])
1910 			return -EINVAL;
1911 
1912 		/* TODO change id to idx */
1913 		desc->id = nla_get_u8(attrs[NL802154_KEY_ID_ATTR_INDEX]);
1914 	}
1915 
1916 	return 0;
1917 }
1918 
1919 static int nl802154_set_llsec_params(struct sk_buff *skb,
1920 				     struct genl_info *info)
1921 {
1922 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
1923 	struct net_device *dev = info->user_ptr[1];
1924 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1925 	struct ieee802154_llsec_params params;
1926 	u32 changed = 0;
1927 	int ret;
1928 
1929 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1930 		return -EOPNOTSUPP;
1931 
1932 	if (info->attrs[NL802154_ATTR_SEC_ENABLED]) {
1933 		u8 enabled;
1934 
1935 		enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1936 		if (enabled != 0 && enabled != 1)
1937 			return -EINVAL;
1938 
1939 		params.enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1940 		changed |= IEEE802154_LLSEC_PARAM_ENABLED;
1941 	}
1942 
1943 	if (info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID]) {
1944 		ret = ieee802154_llsec_parse_key_id(info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID],
1945 						    &params.out_key);
1946 		if (ret < 0)
1947 			return ret;
1948 
1949 		changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
1950 	}
1951 
1952 	if (info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]) {
1953 		params.out_level = nla_get_u32(info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]);
1954 		if (params.out_level > NL802154_SECLEVEL_MAX)
1955 			return -EINVAL;
1956 
1957 		changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
1958 	}
1959 
1960 	if (info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]) {
1961 		params.frame_counter = nla_get_be32(info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]);
1962 		changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
1963 	}
1964 
1965 	return rdev_set_llsec_params(rdev, wpan_dev, &params, changed);
1966 }
1967 
1968 static int nl802154_send_key(struct sk_buff *msg, u32 cmd, u32 portid,
1969 			     u32 seq, int flags,
1970 			     struct cfg802154_registered_device *rdev,
1971 			     struct net_device *dev,
1972 			     const struct ieee802154_llsec_key_entry *key)
1973 {
1974 	void *hdr;
1975 	u32 commands[NL802154_CMD_FRAME_NR_IDS / 32];
1976 	struct nlattr *nl_key, *nl_key_id;
1977 
1978 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1979 	if (!hdr)
1980 		return -ENOBUFS;
1981 
1982 	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1983 		goto nla_put_failure;
1984 
1985 	nl_key = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_KEY);
1986 	if (!nl_key)
1987 		goto nla_put_failure;
1988 
1989 	nl_key_id = nla_nest_start_noflag(msg, NL802154_KEY_ATTR_ID);
1990 	if (!nl_key_id)
1991 		goto nla_put_failure;
1992 
1993 	if (ieee802154_llsec_send_key_id(msg, &key->id) < 0)
1994 		goto nla_put_failure;
1995 
1996 	nla_nest_end(msg, nl_key_id);
1997 
1998 	if (nla_put_u8(msg, NL802154_KEY_ATTR_USAGE_FRAMES,
1999 		       key->key->frame_types))
2000 		goto nla_put_failure;
2001 
2002 	if (key->key->frame_types & BIT(NL802154_FRAME_CMD)) {
2003 		/* TODO for each nested */
2004 		memset(commands, 0, sizeof(commands));
2005 		commands[7] = key->key->cmd_frame_ids;
2006 		if (nla_put(msg, NL802154_KEY_ATTR_USAGE_CMDS,
2007 			    sizeof(commands), commands))
2008 			goto nla_put_failure;
2009 	}
2010 
2011 	if (nla_put(msg, NL802154_KEY_ATTR_BYTES, NL802154_KEY_SIZE,
2012 		    key->key->key))
2013 		goto nla_put_failure;
2014 
2015 	nla_nest_end(msg, nl_key);
2016 	genlmsg_end(msg, hdr);
2017 
2018 	return 0;
2019 
2020 nla_put_failure:
2021 	genlmsg_cancel(msg, hdr);
2022 	return -EMSGSIZE;
2023 }
2024 
2025 static int
2026 nl802154_dump_llsec_key(struct sk_buff *skb, struct netlink_callback *cb)
2027 {
2028 	struct cfg802154_registered_device *rdev = NULL;
2029 	struct ieee802154_llsec_key_entry *key;
2030 	struct ieee802154_llsec_table *table;
2031 	struct wpan_dev *wpan_dev;
2032 	int err;
2033 
2034 	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2035 	if (err)
2036 		return err;
2037 
2038 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2039 		err = skb->len;
2040 		goto out_err;
2041 	}
2042 
2043 	if (!wpan_dev->netdev) {
2044 		err = -EINVAL;
2045 		goto out_err;
2046 	}
2047 
2048 	rdev_lock_llsec_table(rdev, wpan_dev);
2049 	rdev_get_llsec_table(rdev, wpan_dev, &table);
2050 
2051 	/* TODO make it like station dump */
2052 	if (cb->args[2])
2053 		goto out;
2054 
2055 	list_for_each_entry(key, &table->keys, list) {
2056 		if (nl802154_send_key(skb, NL802154_CMD_NEW_SEC_KEY,
2057 				      NETLINK_CB(cb->skb).portid,
2058 				      cb->nlh->nlmsg_seq, NLM_F_MULTI,
2059 				      rdev, wpan_dev->netdev, key) < 0) {
2060 			/* TODO */
2061 			err = -EIO;
2062 			rdev_unlock_llsec_table(rdev, wpan_dev);
2063 			goto out_err;
2064 		}
2065 	}
2066 
2067 	cb->args[2] = 1;
2068 
2069 out:
2070 	rdev_unlock_llsec_table(rdev, wpan_dev);
2071 	err = skb->len;
2072 out_err:
2073 	nl802154_finish_wpan_dev_dump(rdev);
2074 
2075 	return err;
2076 }
2077 
2078 static const struct nla_policy nl802154_key_policy[NL802154_KEY_ATTR_MAX + 1] = {
2079 	[NL802154_KEY_ATTR_ID] = { NLA_NESTED },
2080 	/* TODO handle it as for_each_nested and NLA_FLAG? */
2081 	[NL802154_KEY_ATTR_USAGE_FRAMES] = { NLA_U8 },
2082 	/* TODO handle it as for_each_nested, not static array? */
2083 	[NL802154_KEY_ATTR_USAGE_CMDS] = { .len = NL802154_CMD_FRAME_NR_IDS / 8 },
2084 	[NL802154_KEY_ATTR_BYTES] = { .len = NL802154_KEY_SIZE },
2085 };
2086 
2087 static int nl802154_add_llsec_key(struct sk_buff *skb, struct genl_info *info)
2088 {
2089 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2090 	struct net_device *dev = info->user_ptr[1];
2091 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2092 	struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
2093 	struct ieee802154_llsec_key key = { };
2094 	struct ieee802154_llsec_key_id id = { };
2095 	u32 commands[NL802154_CMD_FRAME_NR_IDS / 32] = { };
2096 
2097 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2098 		return -EOPNOTSUPP;
2099 
2100 	if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
2101 	    nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
2102 		return -EINVAL;
2103 
2104 	if (!attrs[NL802154_KEY_ATTR_USAGE_FRAMES] ||
2105 	    !attrs[NL802154_KEY_ATTR_BYTES])
2106 		return -EINVAL;
2107 
2108 	if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
2109 		return -ENOBUFS;
2110 
2111 	key.frame_types = nla_get_u8(attrs[NL802154_KEY_ATTR_USAGE_FRAMES]);
2112 	if (key.frame_types > BIT(NL802154_FRAME_MAX) ||
2113 	    ((key.frame_types & BIT(NL802154_FRAME_CMD)) &&
2114 	     !attrs[NL802154_KEY_ATTR_USAGE_CMDS]))
2115 		return -EINVAL;
2116 
2117 	if (attrs[NL802154_KEY_ATTR_USAGE_CMDS]) {
2118 		/* TODO for each nested */
2119 		nla_memcpy(commands, attrs[NL802154_KEY_ATTR_USAGE_CMDS],
2120 			   NL802154_CMD_FRAME_NR_IDS / 8);
2121 
2122 		/* TODO understand the -EINVAL logic here? last condition */
2123 		if (commands[0] || commands[1] || commands[2] || commands[3] ||
2124 		    commands[4] || commands[5] || commands[6] ||
2125 		    commands[7] > BIT(NL802154_CMD_FRAME_MAX))
2126 			return -EINVAL;
2127 
2128 		key.cmd_frame_ids = commands[7];
2129 	} else {
2130 		key.cmd_frame_ids = 0;
2131 	}
2132 
2133 	nla_memcpy(key.key, attrs[NL802154_KEY_ATTR_BYTES], NL802154_KEY_SIZE);
2134 
2135 	if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
2136 		return -ENOBUFS;
2137 
2138 	return rdev_add_llsec_key(rdev, wpan_dev, &id, &key);
2139 }
2140 
2141 static int nl802154_del_llsec_key(struct sk_buff *skb, struct genl_info *info)
2142 {
2143 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2144 	struct net_device *dev = info->user_ptr[1];
2145 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2146 	struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
2147 	struct ieee802154_llsec_key_id id;
2148 
2149 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2150 		return -EOPNOTSUPP;
2151 
2152 	if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
2153 	    nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
2154 		return -EINVAL;
2155 
2156 	if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
2157 		return -ENOBUFS;
2158 
2159 	return rdev_del_llsec_key(rdev, wpan_dev, &id);
2160 }
2161 
2162 static int nl802154_send_device(struct sk_buff *msg, u32 cmd, u32 portid,
2163 				u32 seq, int flags,
2164 				struct cfg802154_registered_device *rdev,
2165 				struct net_device *dev,
2166 				const struct ieee802154_llsec_device *dev_desc)
2167 {
2168 	void *hdr;
2169 	struct nlattr *nl_device;
2170 
2171 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
2172 	if (!hdr)
2173 		return -ENOBUFS;
2174 
2175 	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
2176 		goto nla_put_failure;
2177 
2178 	nl_device = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVICE);
2179 	if (!nl_device)
2180 		goto nla_put_failure;
2181 
2182 	if (nla_put_u32(msg, NL802154_DEV_ATTR_FRAME_COUNTER,
2183 			dev_desc->frame_counter) ||
2184 	    nla_put_le16(msg, NL802154_DEV_ATTR_PAN_ID, dev_desc->pan_id) ||
2185 	    nla_put_le16(msg, NL802154_DEV_ATTR_SHORT_ADDR,
2186 			 dev_desc->short_addr) ||
2187 	    nla_put_le64(msg, NL802154_DEV_ATTR_EXTENDED_ADDR,
2188 			 dev_desc->hwaddr, NL802154_DEV_ATTR_PAD) ||
2189 	    nla_put_u8(msg, NL802154_DEV_ATTR_SECLEVEL_EXEMPT,
2190 		       dev_desc->seclevel_exempt) ||
2191 	    nla_put_u32(msg, NL802154_DEV_ATTR_KEY_MODE, dev_desc->key_mode))
2192 		goto nla_put_failure;
2193 
2194 	nla_nest_end(msg, nl_device);
2195 	genlmsg_end(msg, hdr);
2196 
2197 	return 0;
2198 
2199 nla_put_failure:
2200 	genlmsg_cancel(msg, hdr);
2201 	return -EMSGSIZE;
2202 }
2203 
2204 static int
2205 nl802154_dump_llsec_dev(struct sk_buff *skb, struct netlink_callback *cb)
2206 {
2207 	struct cfg802154_registered_device *rdev = NULL;
2208 	struct ieee802154_llsec_device *dev;
2209 	struct ieee802154_llsec_table *table;
2210 	struct wpan_dev *wpan_dev;
2211 	int err;
2212 
2213 	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2214 	if (err)
2215 		return err;
2216 
2217 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2218 		err = skb->len;
2219 		goto out_err;
2220 	}
2221 
2222 	if (!wpan_dev->netdev) {
2223 		err = -EINVAL;
2224 		goto out_err;
2225 	}
2226 
2227 	rdev_lock_llsec_table(rdev, wpan_dev);
2228 	rdev_get_llsec_table(rdev, wpan_dev, &table);
2229 
2230 	/* TODO make it like station dump */
2231 	if (cb->args[2])
2232 		goto out;
2233 
2234 	list_for_each_entry(dev, &table->devices, list) {
2235 		if (nl802154_send_device(skb, NL802154_CMD_NEW_SEC_LEVEL,
2236 					 NETLINK_CB(cb->skb).portid,
2237 					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2238 					 rdev, wpan_dev->netdev, dev) < 0) {
2239 			/* TODO */
2240 			err = -EIO;
2241 			rdev_unlock_llsec_table(rdev, wpan_dev);
2242 			goto out_err;
2243 		}
2244 	}
2245 
2246 	cb->args[2] = 1;
2247 
2248 out:
2249 	rdev_unlock_llsec_table(rdev, wpan_dev);
2250 	err = skb->len;
2251 out_err:
2252 	nl802154_finish_wpan_dev_dump(rdev);
2253 
2254 	return err;
2255 }
2256 
2257 static const struct nla_policy nl802154_dev_policy[NL802154_DEV_ATTR_MAX + 1] = {
2258 	[NL802154_DEV_ATTR_FRAME_COUNTER] = { NLA_U32 },
2259 	[NL802154_DEV_ATTR_PAN_ID] = { .type = NLA_U16 },
2260 	[NL802154_DEV_ATTR_SHORT_ADDR] = { .type = NLA_U16 },
2261 	[NL802154_DEV_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
2262 	[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] = { NLA_U8 },
2263 	[NL802154_DEV_ATTR_KEY_MODE] = { NLA_U32 },
2264 };
2265 
2266 static int
2267 ieee802154_llsec_parse_device(struct nlattr *nla,
2268 			      struct ieee802154_llsec_device *dev)
2269 {
2270 	struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
2271 
2272 	if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, nla, nl802154_dev_policy, NULL))
2273 		return -EINVAL;
2274 
2275 	memset(dev, 0, sizeof(*dev));
2276 
2277 	if (!attrs[NL802154_DEV_ATTR_FRAME_COUNTER] ||
2278 	    !attrs[NL802154_DEV_ATTR_PAN_ID] ||
2279 	    !attrs[NL802154_DEV_ATTR_SHORT_ADDR] ||
2280 	    !attrs[NL802154_DEV_ATTR_EXTENDED_ADDR] ||
2281 	    !attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] ||
2282 	    !attrs[NL802154_DEV_ATTR_KEY_MODE])
2283 		return -EINVAL;
2284 
2285 	/* TODO be32 */
2286 	dev->frame_counter = nla_get_u32(attrs[NL802154_DEV_ATTR_FRAME_COUNTER]);
2287 	dev->pan_id = nla_get_le16(attrs[NL802154_DEV_ATTR_PAN_ID]);
2288 	dev->short_addr = nla_get_le16(attrs[NL802154_DEV_ATTR_SHORT_ADDR]);
2289 	/* TODO rename hwaddr to extended_addr */
2290 	dev->hwaddr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
2291 	dev->seclevel_exempt = nla_get_u8(attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT]);
2292 	dev->key_mode = nla_get_u32(attrs[NL802154_DEV_ATTR_KEY_MODE]);
2293 
2294 	if (dev->key_mode > NL802154_DEVKEY_MAX ||
2295 	    (dev->seclevel_exempt != 0 && dev->seclevel_exempt != 1))
2296 		return -EINVAL;
2297 
2298 	return 0;
2299 }
2300 
2301 static int nl802154_add_llsec_dev(struct sk_buff *skb, struct genl_info *info)
2302 {
2303 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2304 	struct net_device *dev = info->user_ptr[1];
2305 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2306 	struct ieee802154_llsec_device dev_desc;
2307 
2308 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2309 		return -EOPNOTSUPP;
2310 
2311 	if (ieee802154_llsec_parse_device(info->attrs[NL802154_ATTR_SEC_DEVICE],
2312 					  &dev_desc) < 0)
2313 		return -EINVAL;
2314 
2315 	return rdev_add_device(rdev, wpan_dev, &dev_desc);
2316 }
2317 
2318 static int nl802154_del_llsec_dev(struct sk_buff *skb, struct genl_info *info)
2319 {
2320 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2321 	struct net_device *dev = info->user_ptr[1];
2322 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2323 	struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
2324 	__le64 extended_addr;
2325 
2326 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2327 		return -EOPNOTSUPP;
2328 
2329 	if (!info->attrs[NL802154_ATTR_SEC_DEVICE] ||
2330 	    nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVICE], nl802154_dev_policy, info->extack))
2331 		return -EINVAL;
2332 
2333 	if (!attrs[NL802154_DEV_ATTR_EXTENDED_ADDR])
2334 		return -EINVAL;
2335 
2336 	extended_addr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
2337 	return rdev_del_device(rdev, wpan_dev, extended_addr);
2338 }
2339 
2340 static int nl802154_send_devkey(struct sk_buff *msg, u32 cmd, u32 portid,
2341 				u32 seq, int flags,
2342 				struct cfg802154_registered_device *rdev,
2343 				struct net_device *dev, __le64 extended_addr,
2344 				const struct ieee802154_llsec_device_key *devkey)
2345 {
2346 	void *hdr;
2347 	struct nlattr *nl_devkey, *nl_key_id;
2348 
2349 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
2350 	if (!hdr)
2351 		return -ENOBUFS;
2352 
2353 	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
2354 		goto nla_put_failure;
2355 
2356 	nl_devkey = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVKEY);
2357 	if (!nl_devkey)
2358 		goto nla_put_failure;
2359 
2360 	if (nla_put_le64(msg, NL802154_DEVKEY_ATTR_EXTENDED_ADDR,
2361 			 extended_addr, NL802154_DEVKEY_ATTR_PAD) ||
2362 	    nla_put_u32(msg, NL802154_DEVKEY_ATTR_FRAME_COUNTER,
2363 			devkey->frame_counter))
2364 		goto nla_put_failure;
2365 
2366 	nl_key_id = nla_nest_start_noflag(msg, NL802154_DEVKEY_ATTR_ID);
2367 	if (!nl_key_id)
2368 		goto nla_put_failure;
2369 
2370 	if (ieee802154_llsec_send_key_id(msg, &devkey->key_id) < 0)
2371 		goto nla_put_failure;
2372 
2373 	nla_nest_end(msg, nl_key_id);
2374 	nla_nest_end(msg, nl_devkey);
2375 	genlmsg_end(msg, hdr);
2376 
2377 	return 0;
2378 
2379 nla_put_failure:
2380 	genlmsg_cancel(msg, hdr);
2381 	return -EMSGSIZE;
2382 }
2383 
2384 static int
2385 nl802154_dump_llsec_devkey(struct sk_buff *skb, struct netlink_callback *cb)
2386 {
2387 	struct cfg802154_registered_device *rdev = NULL;
2388 	struct ieee802154_llsec_device_key *kpos;
2389 	struct ieee802154_llsec_device *dpos;
2390 	struct ieee802154_llsec_table *table;
2391 	struct wpan_dev *wpan_dev;
2392 	int err;
2393 
2394 	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2395 	if (err)
2396 		return err;
2397 
2398 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2399 		err = skb->len;
2400 		goto out_err;
2401 	}
2402 
2403 	if (!wpan_dev->netdev) {
2404 		err = -EINVAL;
2405 		goto out_err;
2406 	}
2407 
2408 	rdev_lock_llsec_table(rdev, wpan_dev);
2409 	rdev_get_llsec_table(rdev, wpan_dev, &table);
2410 
2411 	/* TODO make it like station dump */
2412 	if (cb->args[2])
2413 		goto out;
2414 
2415 	/* TODO look if remove devkey and do some nested attribute */
2416 	list_for_each_entry(dpos, &table->devices, list) {
2417 		list_for_each_entry(kpos, &dpos->keys, list) {
2418 			if (nl802154_send_devkey(skb,
2419 						 NL802154_CMD_NEW_SEC_LEVEL,
2420 						 NETLINK_CB(cb->skb).portid,
2421 						 cb->nlh->nlmsg_seq,
2422 						 NLM_F_MULTI, rdev,
2423 						 wpan_dev->netdev,
2424 						 dpos->hwaddr,
2425 						 kpos) < 0) {
2426 				/* TODO */
2427 				err = -EIO;
2428 				rdev_unlock_llsec_table(rdev, wpan_dev);
2429 				goto out_err;
2430 			}
2431 		}
2432 	}
2433 
2434 	cb->args[2] = 1;
2435 
2436 out:
2437 	rdev_unlock_llsec_table(rdev, wpan_dev);
2438 	err = skb->len;
2439 out_err:
2440 	nl802154_finish_wpan_dev_dump(rdev);
2441 
2442 	return err;
2443 }
2444 
2445 static const struct nla_policy nl802154_devkey_policy[NL802154_DEVKEY_ATTR_MAX + 1] = {
2446 	[NL802154_DEVKEY_ATTR_FRAME_COUNTER] = { NLA_U32 },
2447 	[NL802154_DEVKEY_ATTR_EXTENDED_ADDR] = { NLA_U64 },
2448 	[NL802154_DEVKEY_ATTR_ID] = { NLA_NESTED },
2449 };
2450 
2451 static int nl802154_add_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
2452 {
2453 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2454 	struct net_device *dev = info->user_ptr[1];
2455 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2456 	struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
2457 	struct ieee802154_llsec_device_key key;
2458 	__le64 extended_addr;
2459 
2460 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2461 		return -EOPNOTSUPP;
2462 
2463 	if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
2464 	    nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack) < 0)
2465 		return -EINVAL;
2466 
2467 	if (!attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER] ||
2468 	    !attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
2469 		return -EINVAL;
2470 
2471 	/* TODO change key.id ? */
2472 	if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
2473 					  &key.key_id) < 0)
2474 		return -ENOBUFS;
2475 
2476 	/* TODO be32 */
2477 	key.frame_counter = nla_get_u32(attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER]);
2478 	/* TODO change naming hwaddr -> extended_addr
2479 	 * check unique identifier short+pan OR extended_addr
2480 	 */
2481 	extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
2482 	return rdev_add_devkey(rdev, wpan_dev, extended_addr, &key);
2483 }
2484 
2485 static int nl802154_del_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
2486 {
2487 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2488 	struct net_device *dev = info->user_ptr[1];
2489 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2490 	struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
2491 	struct ieee802154_llsec_device_key key;
2492 	__le64 extended_addr;
2493 
2494 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2495 		return -EOPNOTSUPP;
2496 
2497 	if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
2498 	    nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack))
2499 		return -EINVAL;
2500 
2501 	if (!attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
2502 		return -EINVAL;
2503 
2504 	/* TODO change key.id ? */
2505 	if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
2506 					  &key.key_id) < 0)
2507 		return -ENOBUFS;
2508 
2509 	/* TODO change naming hwaddr -> extended_addr
2510 	 * check unique identifier short+pan OR extended_addr
2511 	 */
2512 	extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
2513 	return rdev_del_devkey(rdev, wpan_dev, extended_addr, &key);
2514 }
2515 
2516 static int nl802154_send_seclevel(struct sk_buff *msg, u32 cmd, u32 portid,
2517 				  u32 seq, int flags,
2518 				  struct cfg802154_registered_device *rdev,
2519 				  struct net_device *dev,
2520 				  const struct ieee802154_llsec_seclevel *sl)
2521 {
2522 	void *hdr;
2523 	struct nlattr *nl_seclevel;
2524 
2525 	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
2526 	if (!hdr)
2527 		return -ENOBUFS;
2528 
2529 	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
2530 		goto nla_put_failure;
2531 
2532 	nl_seclevel = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_LEVEL);
2533 	if (!nl_seclevel)
2534 		goto nla_put_failure;
2535 
2536 	if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_FRAME, sl->frame_type) ||
2537 	    nla_put_u32(msg, NL802154_SECLEVEL_ATTR_LEVELS, sl->sec_levels) ||
2538 	    nla_put_u8(msg, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE,
2539 		       sl->device_override))
2540 		goto nla_put_failure;
2541 
2542 	if (sl->frame_type == NL802154_FRAME_CMD) {
2543 		if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_CMD_FRAME,
2544 				sl->cmd_frame_id))
2545 			goto nla_put_failure;
2546 	}
2547 
2548 	nla_nest_end(msg, nl_seclevel);
2549 	genlmsg_end(msg, hdr);
2550 
2551 	return 0;
2552 
2553 nla_put_failure:
2554 	genlmsg_cancel(msg, hdr);
2555 	return -EMSGSIZE;
2556 }
2557 
2558 static int
2559 nl802154_dump_llsec_seclevel(struct sk_buff *skb, struct netlink_callback *cb)
2560 {
2561 	struct cfg802154_registered_device *rdev = NULL;
2562 	struct ieee802154_llsec_seclevel *sl;
2563 	struct ieee802154_llsec_table *table;
2564 	struct wpan_dev *wpan_dev;
2565 	int err;
2566 
2567 	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2568 	if (err)
2569 		return err;
2570 
2571 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2572 		err = skb->len;
2573 		goto out_err;
2574 	}
2575 
2576 	if (!wpan_dev->netdev) {
2577 		err = -EINVAL;
2578 		goto out_err;
2579 	}
2580 
2581 	rdev_lock_llsec_table(rdev, wpan_dev);
2582 	rdev_get_llsec_table(rdev, wpan_dev, &table);
2583 
2584 	/* TODO make it like station dump */
2585 	if (cb->args[2])
2586 		goto out;
2587 
2588 	list_for_each_entry(sl, &table->security_levels, list) {
2589 		if (nl802154_send_seclevel(skb, NL802154_CMD_NEW_SEC_LEVEL,
2590 					   NETLINK_CB(cb->skb).portid,
2591 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
2592 					   rdev, wpan_dev->netdev, sl) < 0) {
2593 			/* TODO */
2594 			err = -EIO;
2595 			rdev_unlock_llsec_table(rdev, wpan_dev);
2596 			goto out_err;
2597 		}
2598 	}
2599 
2600 	cb->args[2] = 1;
2601 
2602 out:
2603 	rdev_unlock_llsec_table(rdev, wpan_dev);
2604 	err = skb->len;
2605 out_err:
2606 	nl802154_finish_wpan_dev_dump(rdev);
2607 
2608 	return err;
2609 }
2610 
2611 static const struct nla_policy nl802154_seclevel_policy[NL802154_SECLEVEL_ATTR_MAX + 1] = {
2612 	[NL802154_SECLEVEL_ATTR_LEVELS] = { .type = NLA_U8 },
2613 	[NL802154_SECLEVEL_ATTR_FRAME] = { .type = NLA_U32 },
2614 	[NL802154_SECLEVEL_ATTR_CMD_FRAME] = { .type = NLA_U32 },
2615 	[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE] = { .type = NLA_U8 },
2616 };
2617 
2618 static int
2619 llsec_parse_seclevel(struct nlattr *nla, struct ieee802154_llsec_seclevel *sl)
2620 {
2621 	struct nlattr *attrs[NL802154_SECLEVEL_ATTR_MAX + 1];
2622 
2623 	if (!nla || nla_parse_nested_deprecated(attrs, NL802154_SECLEVEL_ATTR_MAX, nla, nl802154_seclevel_policy, NULL))
2624 		return -EINVAL;
2625 
2626 	memset(sl, 0, sizeof(*sl));
2627 
2628 	if (!attrs[NL802154_SECLEVEL_ATTR_LEVELS] ||
2629 	    !attrs[NL802154_SECLEVEL_ATTR_FRAME] ||
2630 	    !attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE])
2631 		return -EINVAL;
2632 
2633 	sl->sec_levels = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_LEVELS]);
2634 	sl->frame_type = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_FRAME]);
2635 	sl->device_override = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE]);
2636 	if (sl->frame_type > NL802154_FRAME_MAX ||
2637 	    (sl->device_override != 0 && sl->device_override != 1))
2638 		return -EINVAL;
2639 
2640 	if (sl->frame_type == NL802154_FRAME_CMD) {
2641 		if (!attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME])
2642 			return -EINVAL;
2643 
2644 		sl->cmd_frame_id = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME]);
2645 		if (sl->cmd_frame_id > NL802154_CMD_FRAME_MAX)
2646 			return -EINVAL;
2647 	}
2648 
2649 	return 0;
2650 }
2651 
2652 static int nl802154_add_llsec_seclevel(struct sk_buff *skb,
2653 				       struct genl_info *info)
2654 {
2655 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2656 	struct net_device *dev = info->user_ptr[1];
2657 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2658 	struct ieee802154_llsec_seclevel sl;
2659 
2660 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2661 		return -EOPNOTSUPP;
2662 
2663 	if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2664 				 &sl) < 0)
2665 		return -EINVAL;
2666 
2667 	return rdev_add_seclevel(rdev, wpan_dev, &sl);
2668 }
2669 
2670 static int nl802154_del_llsec_seclevel(struct sk_buff *skb,
2671 				       struct genl_info *info)
2672 {
2673 	struct cfg802154_registered_device *rdev = info->user_ptr[0];
2674 	struct net_device *dev = info->user_ptr[1];
2675 	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2676 	struct ieee802154_llsec_seclevel sl;
2677 
2678 	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2679 		return -EOPNOTSUPP;
2680 
2681 	if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2682 				 &sl) < 0)
2683 		return -EINVAL;
2684 
2685 	return rdev_del_seclevel(rdev, wpan_dev, &sl);
2686 }
2687 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2688 
2689 #define NL802154_FLAG_NEED_WPAN_PHY	0x01
2690 #define NL802154_FLAG_NEED_NETDEV	0x02
2691 #define NL802154_FLAG_NEED_RTNL		0x04
2692 #define NL802154_FLAG_CHECK_NETDEV_UP	0x08
2693 #define NL802154_FLAG_NEED_WPAN_DEV	0x10
2694 
2695 static int nl802154_pre_doit(const struct genl_split_ops *ops,
2696 			     struct sk_buff *skb,
2697 			     struct genl_info *info)
2698 {
2699 	struct cfg802154_registered_device *rdev;
2700 	struct wpan_dev *wpan_dev;
2701 	struct net_device *dev;
2702 	bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
2703 
2704 	if (rtnl)
2705 		rtnl_lock();
2706 
2707 	if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
2708 		rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
2709 		if (IS_ERR(rdev)) {
2710 			if (rtnl)
2711 				rtnl_unlock();
2712 			return PTR_ERR(rdev);
2713 		}
2714 		info->user_ptr[0] = rdev;
2715 	} else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
2716 		   ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2717 		ASSERT_RTNL();
2718 		wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
2719 							   info->attrs);
2720 		if (IS_ERR(wpan_dev)) {
2721 			if (rtnl)
2722 				rtnl_unlock();
2723 			return PTR_ERR(wpan_dev);
2724 		}
2725 
2726 		dev = wpan_dev->netdev;
2727 		rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
2728 
2729 		if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
2730 			if (!dev) {
2731 				if (rtnl)
2732 					rtnl_unlock();
2733 				return -EINVAL;
2734 			}
2735 
2736 			info->user_ptr[1] = dev;
2737 		} else {
2738 			info->user_ptr[1] = wpan_dev;
2739 		}
2740 
2741 		if (dev) {
2742 			if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
2743 			    !netif_running(dev)) {
2744 				if (rtnl)
2745 					rtnl_unlock();
2746 				return -ENETDOWN;
2747 			}
2748 
2749 			dev_hold(dev);
2750 		}
2751 
2752 		info->user_ptr[0] = rdev;
2753 	}
2754 
2755 	return 0;
2756 }
2757 
2758 static void nl802154_post_doit(const struct genl_split_ops *ops,
2759 			       struct sk_buff *skb,
2760 			       struct genl_info *info)
2761 {
2762 	if (info->user_ptr[1]) {
2763 		if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2764 			struct wpan_dev *wpan_dev = info->user_ptr[1];
2765 
2766 			dev_put(wpan_dev->netdev);
2767 		} else {
2768 			dev_put(info->user_ptr[1]);
2769 		}
2770 	}
2771 
2772 	if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
2773 		rtnl_unlock();
2774 }
2775 
2776 static const struct genl_ops nl802154_ops[] = {
2777 	{
2778 		.cmd = NL802154_CMD_GET_WPAN_PHY,
2779 		.validate = GENL_DONT_VALIDATE_STRICT |
2780 			    GENL_DONT_VALIDATE_DUMP_STRICT,
2781 		.doit = nl802154_get_wpan_phy,
2782 		.dumpit = nl802154_dump_wpan_phy,
2783 		.done = nl802154_dump_wpan_phy_done,
2784 		/* can be retrieved by unprivileged users */
2785 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2786 				  NL802154_FLAG_NEED_RTNL,
2787 	},
2788 	{
2789 		.cmd = NL802154_CMD_GET_INTERFACE,
2790 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2791 		.doit = nl802154_get_interface,
2792 		.dumpit = nl802154_dump_interface,
2793 		/* can be retrieved by unprivileged users */
2794 		.internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2795 				  NL802154_FLAG_NEED_RTNL,
2796 	},
2797 	{
2798 		.cmd = NL802154_CMD_NEW_INTERFACE,
2799 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2800 		.doit = nl802154_new_interface,
2801 		.flags = GENL_ADMIN_PERM,
2802 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2803 				  NL802154_FLAG_NEED_RTNL,
2804 	},
2805 	{
2806 		.cmd = NL802154_CMD_DEL_INTERFACE,
2807 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2808 		.doit = nl802154_del_interface,
2809 		.flags = GENL_ADMIN_PERM,
2810 		.internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2811 				  NL802154_FLAG_NEED_RTNL,
2812 	},
2813 	{
2814 		.cmd = NL802154_CMD_SET_CHANNEL,
2815 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2816 		.doit = nl802154_set_channel,
2817 		.flags = GENL_ADMIN_PERM,
2818 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2819 				  NL802154_FLAG_NEED_RTNL,
2820 	},
2821 	{
2822 		.cmd = NL802154_CMD_SET_CCA_MODE,
2823 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2824 		.doit = nl802154_set_cca_mode,
2825 		.flags = GENL_ADMIN_PERM,
2826 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2827 				  NL802154_FLAG_NEED_RTNL,
2828 	},
2829 	{
2830 		.cmd = NL802154_CMD_SET_CCA_ED_LEVEL,
2831 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2832 		.doit = nl802154_set_cca_ed_level,
2833 		.flags = GENL_ADMIN_PERM,
2834 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2835 				  NL802154_FLAG_NEED_RTNL,
2836 	},
2837 	{
2838 		.cmd = NL802154_CMD_SET_TX_POWER,
2839 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2840 		.doit = nl802154_set_tx_power,
2841 		.flags = GENL_ADMIN_PERM,
2842 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2843 				  NL802154_FLAG_NEED_RTNL,
2844 	},
2845 	{
2846 		.cmd = NL802154_CMD_SET_WPAN_PHY_NETNS,
2847 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2848 		.doit = nl802154_wpan_phy_netns,
2849 		.flags = GENL_ADMIN_PERM,
2850 		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2851 				  NL802154_FLAG_NEED_RTNL,
2852 	},
2853 	{
2854 		.cmd = NL802154_CMD_SET_PAN_ID,
2855 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2856 		.doit = nl802154_set_pan_id,
2857 		.flags = GENL_ADMIN_PERM,
2858 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2859 				  NL802154_FLAG_NEED_RTNL,
2860 	},
2861 	{
2862 		.cmd = NL802154_CMD_SET_SHORT_ADDR,
2863 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2864 		.doit = nl802154_set_short_addr,
2865 		.flags = GENL_ADMIN_PERM,
2866 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2867 				  NL802154_FLAG_NEED_RTNL,
2868 	},
2869 	{
2870 		.cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
2871 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2872 		.doit = nl802154_set_backoff_exponent,
2873 		.flags = GENL_ADMIN_PERM,
2874 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2875 				  NL802154_FLAG_NEED_RTNL,
2876 	},
2877 	{
2878 		.cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
2879 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2880 		.doit = nl802154_set_max_csma_backoffs,
2881 		.flags = GENL_ADMIN_PERM,
2882 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2883 				  NL802154_FLAG_NEED_RTNL,
2884 	},
2885 	{
2886 		.cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
2887 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2888 		.doit = nl802154_set_max_frame_retries,
2889 		.flags = GENL_ADMIN_PERM,
2890 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2891 				  NL802154_FLAG_NEED_RTNL,
2892 	},
2893 	{
2894 		.cmd = NL802154_CMD_SET_LBT_MODE,
2895 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2896 		.doit = nl802154_set_lbt_mode,
2897 		.flags = GENL_ADMIN_PERM,
2898 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2899 				  NL802154_FLAG_NEED_RTNL,
2900 	},
2901 	{
2902 		.cmd = NL802154_CMD_SET_ACKREQ_DEFAULT,
2903 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2904 		.doit = nl802154_set_ackreq_default,
2905 		.flags = GENL_ADMIN_PERM,
2906 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2907 				  NL802154_FLAG_NEED_RTNL,
2908 	},
2909 	{
2910 		.cmd = NL802154_CMD_TRIGGER_SCAN,
2911 		.doit = nl802154_trigger_scan,
2912 		.flags = GENL_ADMIN_PERM,
2913 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2914 				  NL802154_FLAG_CHECK_NETDEV_UP |
2915 				  NL802154_FLAG_NEED_RTNL,
2916 	},
2917 	{
2918 		.cmd = NL802154_CMD_ABORT_SCAN,
2919 		.doit = nl802154_abort_scan,
2920 		.flags = GENL_ADMIN_PERM,
2921 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2922 				  NL802154_FLAG_CHECK_NETDEV_UP |
2923 				  NL802154_FLAG_NEED_RTNL,
2924 	},
2925 	{
2926 		.cmd = NL802154_CMD_SEND_BEACONS,
2927 		.doit = nl802154_send_beacons,
2928 		.flags = GENL_ADMIN_PERM,
2929 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2930 				  NL802154_FLAG_CHECK_NETDEV_UP |
2931 				  NL802154_FLAG_NEED_RTNL,
2932 	},
2933 	{
2934 		.cmd = NL802154_CMD_STOP_BEACONS,
2935 		.doit = nl802154_stop_beacons,
2936 		.flags = GENL_ADMIN_PERM,
2937 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2938 				  NL802154_FLAG_CHECK_NETDEV_UP |
2939 				  NL802154_FLAG_NEED_RTNL,
2940 	},
2941 	{
2942 		.cmd = NL802154_CMD_ASSOCIATE,
2943 		.doit = nl802154_associate,
2944 		.flags = GENL_ADMIN_PERM,
2945 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2946 				  NL802154_FLAG_CHECK_NETDEV_UP |
2947 				  NL802154_FLAG_NEED_RTNL,
2948 	},
2949 	{
2950 		.cmd = NL802154_CMD_DISASSOCIATE,
2951 		.doit = nl802154_disassociate,
2952 		.flags = GENL_ADMIN_PERM,
2953 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2954 				  NL802154_FLAG_CHECK_NETDEV_UP |
2955 				  NL802154_FLAG_NEED_RTNL,
2956 	},
2957 	{
2958 		.cmd = NL802154_CMD_SET_MAX_ASSOCIATIONS,
2959 		.doit = nl802154_set_max_associations,
2960 		.flags = GENL_ADMIN_PERM,
2961 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2962 				  NL802154_FLAG_NEED_RTNL,
2963 	},
2964 	{
2965 		.cmd = NL802154_CMD_LIST_ASSOCIATIONS,
2966 		.dumpit = nl802154_list_associations,
2967 		/* can be retrieved by unprivileged users */
2968 	},
2969 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
2970 	{
2971 		.cmd = NL802154_CMD_SET_SEC_PARAMS,
2972 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2973 		.doit = nl802154_set_llsec_params,
2974 		.flags = GENL_ADMIN_PERM,
2975 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2976 				  NL802154_FLAG_NEED_RTNL,
2977 	},
2978 	{
2979 		.cmd = NL802154_CMD_GET_SEC_KEY,
2980 		.validate = GENL_DONT_VALIDATE_STRICT |
2981 			    GENL_DONT_VALIDATE_DUMP_STRICT,
2982 		/* TODO .doit by matching key id? */
2983 		.dumpit = nl802154_dump_llsec_key,
2984 		.flags = GENL_ADMIN_PERM,
2985 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2986 				  NL802154_FLAG_NEED_RTNL,
2987 	},
2988 	{
2989 		.cmd = NL802154_CMD_NEW_SEC_KEY,
2990 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2991 		.doit = nl802154_add_llsec_key,
2992 		.flags = GENL_ADMIN_PERM,
2993 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
2994 				  NL802154_FLAG_NEED_RTNL,
2995 	},
2996 	{
2997 		.cmd = NL802154_CMD_DEL_SEC_KEY,
2998 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2999 		.doit = nl802154_del_llsec_key,
3000 		.flags = GENL_ADMIN_PERM,
3001 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
3002 				  NL802154_FLAG_NEED_RTNL,
3003 	},
3004 	/* TODO unique identifier must short+pan OR extended_addr */
3005 	{
3006 		.cmd = NL802154_CMD_GET_SEC_DEV,
3007 		.validate = GENL_DONT_VALIDATE_STRICT |
3008 			    GENL_DONT_VALIDATE_DUMP_STRICT,
3009 		/* TODO .doit by matching extended_addr? */
3010 		.dumpit = nl802154_dump_llsec_dev,
3011 		.flags = GENL_ADMIN_PERM,
3012 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
3013 				  NL802154_FLAG_NEED_RTNL,
3014 	},
3015 	{
3016 		.cmd = NL802154_CMD_NEW_SEC_DEV,
3017 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3018 		.doit = nl802154_add_llsec_dev,
3019 		.flags = GENL_ADMIN_PERM,
3020 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
3021 				  NL802154_FLAG_NEED_RTNL,
3022 	},
3023 	{
3024 		.cmd = NL802154_CMD_DEL_SEC_DEV,
3025 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3026 		.doit = nl802154_del_llsec_dev,
3027 		.flags = GENL_ADMIN_PERM,
3028 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
3029 				  NL802154_FLAG_NEED_RTNL,
3030 	},
3031 	/* TODO remove complete devkey, put it as nested? */
3032 	{
3033 		.cmd = NL802154_CMD_GET_SEC_DEVKEY,
3034 		.validate = GENL_DONT_VALIDATE_STRICT |
3035 			    GENL_DONT_VALIDATE_DUMP_STRICT,
3036 		/* TODO doit by matching ??? */
3037 		.dumpit = nl802154_dump_llsec_devkey,
3038 		.flags = GENL_ADMIN_PERM,
3039 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
3040 				  NL802154_FLAG_NEED_RTNL,
3041 	},
3042 	{
3043 		.cmd = NL802154_CMD_NEW_SEC_DEVKEY,
3044 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3045 		.doit = nl802154_add_llsec_devkey,
3046 		.flags = GENL_ADMIN_PERM,
3047 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
3048 				  NL802154_FLAG_NEED_RTNL,
3049 	},
3050 	{
3051 		.cmd = NL802154_CMD_DEL_SEC_DEVKEY,
3052 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3053 		.doit = nl802154_del_llsec_devkey,
3054 		.flags = GENL_ADMIN_PERM,
3055 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
3056 				  NL802154_FLAG_NEED_RTNL,
3057 	},
3058 	{
3059 		.cmd = NL802154_CMD_GET_SEC_LEVEL,
3060 		.validate = GENL_DONT_VALIDATE_STRICT |
3061 			    GENL_DONT_VALIDATE_DUMP_STRICT,
3062 		/* TODO .doit by matching frame_type? */
3063 		.dumpit = nl802154_dump_llsec_seclevel,
3064 		.flags = GENL_ADMIN_PERM,
3065 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
3066 				  NL802154_FLAG_NEED_RTNL,
3067 	},
3068 	{
3069 		.cmd = NL802154_CMD_NEW_SEC_LEVEL,
3070 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3071 		.doit = nl802154_add_llsec_seclevel,
3072 		.flags = GENL_ADMIN_PERM,
3073 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
3074 				  NL802154_FLAG_NEED_RTNL,
3075 	},
3076 	{
3077 		.cmd = NL802154_CMD_DEL_SEC_LEVEL,
3078 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
3079 		/* TODO match frame_type only? */
3080 		.doit = nl802154_del_llsec_seclevel,
3081 		.flags = GENL_ADMIN_PERM,
3082 		.internal_flags = NL802154_FLAG_NEED_NETDEV |
3083 				  NL802154_FLAG_NEED_RTNL,
3084 	},
3085 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
3086 };
3087 
3088 static struct genl_family nl802154_fam __ro_after_init = {
3089 	.name = NL802154_GENL_NAME,	/* have users key off the name instead */
3090 	.hdrsize = 0,			/* no private header */
3091 	.version = 1,			/* no particular meaning now */
3092 	.maxattr = NL802154_ATTR_MAX,
3093 	.policy = nl802154_policy,
3094 	.netnsok = true,
3095 	.pre_doit = nl802154_pre_doit,
3096 	.post_doit = nl802154_post_doit,
3097 	.module = THIS_MODULE,
3098 	.ops = nl802154_ops,
3099 	.n_ops = ARRAY_SIZE(nl802154_ops),
3100 	.resv_start_op = NL802154_CMD_DEL_SEC_LEVEL + 1,
3101 	.mcgrps = nl802154_mcgrps,
3102 	.n_mcgrps = ARRAY_SIZE(nl802154_mcgrps),
3103 };
3104 
3105 /* initialisation/exit functions */
3106 int __init nl802154_init(void)
3107 {
3108 	return genl_register_family(&nl802154_fam);
3109 }
3110 
3111 void nl802154_exit(void)
3112 {
3113 	genl_unregister_family(&nl802154_fam);
3114 }
3115