Blame view

net/ieee802154/nl802154.c 66.8 KB
79fe1a2aa   Alexander Aring   ieee802154: add n...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /* This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2
   * as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * Authors:
   * Alexander Aring <aar@pengutronix.de>
   *
   * Based on: net/wireless/nl80211.c
   */
  
  #include <linux/rtnetlink.h>
  
  #include <net/cfg802154.h>
  #include <net/genetlink.h>
  #include <net/mac802154.h>
  #include <net/netlink.h>
  #include <net/nl802154.h>
  #include <net/sock.h>
  
  #include "nl802154.h"
ab0bd5617   Alexander Aring   ieee820154: add c...
26
  #include "rdev-ops.h"
79fe1a2aa   Alexander Aring   ieee802154: add n...
27
  #include "core.h"
79fe1a2aa   Alexander Aring   ieee802154: add n...
28
  /* the netlink family */
489111e5c   Johannes Berg   genetlink: static...
29
  static struct genl_family nl802154_fam;
79fe1a2aa   Alexander Aring   ieee802154: add n...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  
  /* multicast groups */
  enum nl802154_multicast_groups {
  	NL802154_MCGRP_CONFIG,
  };
  
  static const struct genl_multicast_group nl802154_mcgrps[] = {
  	[NL802154_MCGRP_CONFIG] = { .name = "config", },
  };
  
  /* returns ERR_PTR values */
  static struct wpan_dev *
  __cfg802154_wpan_dev_from_attrs(struct net *netns, struct nlattr **attrs)
  {
  	struct cfg802154_registered_device *rdev;
  	struct wpan_dev *result = NULL;
  	bool have_ifidx = attrs[NL802154_ATTR_IFINDEX];
  	bool have_wpan_dev_id = attrs[NL802154_ATTR_WPAN_DEV];
  	u64 wpan_dev_id;
  	int wpan_phy_idx = -1;
  	int ifidx = -1;
  
  	ASSERT_RTNL();
  
  	if (!have_ifidx && !have_wpan_dev_id)
  		return ERR_PTR(-EINVAL);
  
  	if (have_ifidx)
  		ifidx = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
  	if (have_wpan_dev_id) {
  		wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
  		wpan_phy_idx = wpan_dev_id >> 32;
  	}
  
  	list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  		struct wpan_dev *wpan_dev;
66e5c2672   Alexander Aring   ieee802154: add n...
66
67
  		if (wpan_phy_net(&rdev->wpan_phy) != netns)
  			continue;
79fe1a2aa   Alexander Aring   ieee802154: add n...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  
  		if (have_wpan_dev_id && rdev->wpan_phy_idx != wpan_phy_idx)
  			continue;
  
  		list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
  			if (have_ifidx && wpan_dev->netdev &&
  			    wpan_dev->netdev->ifindex == ifidx) {
  				result = wpan_dev;
  				break;
  			}
  			if (have_wpan_dev_id &&
  			    wpan_dev->identifier == (u32)wpan_dev_id) {
  				result = wpan_dev;
  				break;
  			}
  		}
  
  		if (result)
  			break;
  	}
  
  	if (result)
  		return result;
  
  	return ERR_PTR(-ENODEV);
  }
  
  static struct cfg802154_registered_device *
  __cfg802154_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
  {
  	struct cfg802154_registered_device *rdev = NULL, *tmp;
  	struct net_device *netdev;
  
  	ASSERT_RTNL();
  
  	if (!attrs[NL802154_ATTR_WPAN_PHY] &&
  	    !attrs[NL802154_ATTR_IFINDEX] &&
  	    !attrs[NL802154_ATTR_WPAN_DEV])
  		return ERR_PTR(-EINVAL);
  
  	if (attrs[NL802154_ATTR_WPAN_PHY])
  		rdev = cfg802154_rdev_by_wpan_phy_idx(
  				nla_get_u32(attrs[NL802154_ATTR_WPAN_PHY]));
  
  	if (attrs[NL802154_ATTR_WPAN_DEV]) {
  		u64 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
  		struct wpan_dev *wpan_dev;
  		bool found = false;
  
  		tmp = cfg802154_rdev_by_wpan_phy_idx(wpan_dev_id >> 32);
  		if (tmp) {
  			/* make sure wpan_dev exists */
  			list_for_each_entry(wpan_dev, &tmp->wpan_dev_list, list) {
  				if (wpan_dev->identifier != (u32)wpan_dev_id)
  					continue;
  				found = true;
  				break;
  			}
  
  			if (!found)
  				tmp = NULL;
  
  			if (rdev && tmp != rdev)
  				return ERR_PTR(-EINVAL);
  			rdev = tmp;
  		}
  	}
  
  	if (attrs[NL802154_ATTR_IFINDEX]) {
  		int ifindex = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
  
  		netdev = __dev_get_by_index(netns, ifindex);
  		if (netdev) {
  			if (netdev->ieee802154_ptr)
  				tmp = wpan_phy_to_rdev(
  						netdev->ieee802154_ptr->wpan_phy);
  			else
  				tmp = NULL;
  
  			/* not wireless device -- return error */
  			if (!tmp)
  				return ERR_PTR(-EINVAL);
  
  			/* mismatch -- return error */
  			if (rdev && tmp != rdev)
  				return ERR_PTR(-EINVAL);
  
  			rdev = tmp;
  		}
  	}
  
  	if (!rdev)
  		return ERR_PTR(-ENODEV);
66e5c2672   Alexander Aring   ieee802154: add n...
161
162
  	if (netns != wpan_phy_net(&rdev->wpan_phy))
  		return ERR_PTR(-ENODEV);
79fe1a2aa   Alexander Aring   ieee802154: add n...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  
  	return rdev;
  }
  
  /* This function returns a pointer to the driver
   * that the genl_info item that is passed refers to.
   *
   * The result of this can be a PTR_ERR and hence must
   * be checked with IS_ERR() for errors.
   */
  static struct cfg802154_registered_device *
  cfg802154_get_dev_from_info(struct net *netns, struct genl_info *info)
  {
  	return __cfg802154_rdev_from_attrs(netns, info->attrs);
  }
  
  /* policy for the attributes */
  static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
ca20ce201   Alexander Aring   ieee802154: add w...
181
182
183
184
185
  	[NL802154_ATTR_WPAN_PHY] = { .type = NLA_U32 },
  	[NL802154_ATTR_WPAN_PHY_NAME] = { .type = NLA_NUL_STRING,
  					  .len = 20-1 },
  
  	[NL802154_ATTR_IFINDEX] = { .type = NLA_U32 },
4b96aea0f   Alexander Aring   ieee802154: add w...
186
187
  	[NL802154_ATTR_IFTYPE] = { .type = NLA_U32 },
  	[NL802154_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
ca20ce201   Alexander Aring   ieee802154: add w...
188
189
190
191
192
  
  	[NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
  
  	[NL802154_ATTR_PAGE] = { .type = NLA_U8, },
  	[NL802154_ATTR_CHANNEL] = { .type = NLA_U8, },
1a19cb680   Alexander Aring   ieee802154: chang...
193
  	[NL802154_ATTR_TX_POWER] = { .type = NLA_S32, },
ca20ce201   Alexander Aring   ieee802154: add w...
194

ba2a9506a   Alexander Aring   nl802154: introdu...
195
196
  	[NL802154_ATTR_CCA_MODE] = { .type = NLA_U32, },
  	[NL802154_ATTR_CCA_OPT] = { .type = NLA_U32, },
e4390592a   Alexander Aring   nl802154: add sup...
197
  	[NL802154_ATTR_CCA_ED_LEVEL] = { .type = NLA_S32, },
ca20ce201   Alexander Aring   ieee802154: add w...
198
199
  
  	[NL802154_ATTR_SUPPORTED_CHANNEL] = { .type = NLA_U32, },
4b96aea0f   Alexander Aring   ieee802154: add w...
200
201
202
203
204
205
206
207
208
209
210
211
  
  	[NL802154_ATTR_PAN_ID] = { .type = NLA_U16, },
  	[NL802154_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
  	[NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16, },
  
  	[NL802154_ATTR_MIN_BE] = { .type = NLA_U8, },
  	[NL802154_ATTR_MAX_BE] = { .type = NLA_U8, },
  	[NL802154_ATTR_MAX_CSMA_BACKOFFS] = { .type = NLA_U8, },
  
  	[NL802154_ATTR_MAX_FRAME_RETRIES] = { .type = NLA_S8, },
  
  	[NL802154_ATTR_LBT_MODE] = { .type = NLA_U8, },
0e6654570   Alexander Aring   nl802154: add sup...
212
213
  
  	[NL802154_ATTR_WPAN_PHY_CAPS] = { .type = NLA_NESTED },
133be0264   Varka Bhadram   nl802154: export ...
214
215
  
  	[NL802154_ATTR_SUPPORTED_COMMANDS] = { .type = NLA_NESTED },
c91208d81   Alexander Aring   ieee802154: add a...
216
217
  
  	[NL802154_ATTR_ACKREQ_DEFAULT] = { .type = NLA_U8 },
a26c5fd76   Alexander Aring   nl802154: add sup...
218

66e5c2672   Alexander Aring   ieee802154: add n...
219
220
  	[NL802154_ATTR_PID] = { .type = NLA_U32 },
  	[NL802154_ATTR_NETNS_FD] = { .type = NLA_U32 },
a26c5fd76   Alexander Aring   nl802154: add sup...
221
222
223
224
225
226
227
228
229
230
231
  #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  	[NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
  	[NL802154_ATTR_SEC_OUT_LEVEL] = { .type = NLA_U32, },
  	[NL802154_ATTR_SEC_OUT_KEY_ID] = { .type = NLA_NESTED, },
  	[NL802154_ATTR_SEC_FRAME_COUNTER] = { .type = NLA_U32 },
  
  	[NL802154_ATTR_SEC_LEVEL] = { .type = NLA_NESTED },
  	[NL802154_ATTR_SEC_DEVICE] = { .type = NLA_NESTED },
  	[NL802154_ATTR_SEC_DEVKEY] = { .type = NLA_NESTED },
  	[NL802154_ATTR_SEC_KEY] = { .type = NLA_NESTED },
  #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
79fe1a2aa   Alexander Aring   ieee802154: add n...
232
  };
a26c5fd76   Alexander Aring   nl802154: add sup...
233
234
235
236
237
238
239
240
241
242
243
244
245
  #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  static int
  nl802154_prepare_wpan_dev_dump(struct sk_buff *skb,
  			       struct netlink_callback *cb,
  			       struct cfg802154_registered_device **rdev,
  			       struct wpan_dev **wpan_dev)
  {
  	int err;
  
  	rtnl_lock();
  
  	if (!cb->args[0]) {
  		err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize,
c90c39dab   Johannes Berg   genetlink: introd...
246
  				  genl_family_attrbuf(&nl802154_fam),
fceb6435e   Johannes Berg   netlink: pass ext...
247
  				  nl802154_fam.maxattr, nl802154_policy, NULL);
a26c5fd76   Alexander Aring   nl802154: add sup...
248
249
250
251
  		if (err)
  			goto out_unlock;
  
  		*wpan_dev = __cfg802154_wpan_dev_from_attrs(sock_net(skb->sk),
c90c39dab   Johannes Berg   genetlink: introd...
252
  							    genl_family_attrbuf(&nl802154_fam));
a26c5fd76   Alexander Aring   nl802154: add sup...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  		if (IS_ERR(*wpan_dev)) {
  			err = PTR_ERR(*wpan_dev);
  			goto out_unlock;
  		}
  		*rdev = wpan_phy_to_rdev((*wpan_dev)->wpan_phy);
  		/* 0 is the first index - add 1 to parse only once */
  		cb->args[0] = (*rdev)->wpan_phy_idx + 1;
  		cb->args[1] = (*wpan_dev)->identifier;
  	} else {
  		/* subtract the 1 again here */
  		struct wpan_phy *wpan_phy = wpan_phy_idx_to_wpan_phy(cb->args[0] - 1);
  		struct wpan_dev *tmp;
  
  		if (!wpan_phy) {
  			err = -ENODEV;
  			goto out_unlock;
  		}
  		*rdev = wpan_phy_to_rdev(wpan_phy);
  		*wpan_dev = NULL;
  
  		list_for_each_entry(tmp, &(*rdev)->wpan_dev_list, list) {
  			if (tmp->identifier == cb->args[1]) {
  				*wpan_dev = tmp;
  				break;
  			}
  		}
  
  		if (!*wpan_dev) {
  			err = -ENODEV;
  			goto out_unlock;
  		}
  	}
  
  	return 0;
   out_unlock:
  	rtnl_unlock();
  	return err;
  }
  
  static void
  nl802154_finish_wpan_dev_dump(struct cfg802154_registered_device *rdev)
  {
  	rtnl_unlock();
  }
  #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
79fe1a2aa   Alexander Aring   ieee802154: add n...
298
299
300
301
302
303
304
  /* message building helper */
  static inline void *nl802154hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
  				    int flags, u8 cmd)
  {
  	/* since there is no private header just add the generic one */
  	return genlmsg_put(skb, portid, seq, &nl802154_fam, flags, cmd);
  }
ca20ce201   Alexander Aring   ieee802154: add w...
305
  static int
0e6654570   Alexander Aring   nl802154: add sup...
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  nl802154_put_flags(struct sk_buff *msg, int attr, u32 mask)
  {
  	struct nlattr *nl_flags = nla_nest_start(msg, attr);
  	int i;
  
  	if (!nl_flags)
  		return -ENOBUFS;
  
  	i = 0;
  	while (mask) {
  		if ((mask & 1) && nla_put_flag(msg, i))
  			return -ENOBUFS;
  
  		mask >>= 1;
  		i++;
  	}
  
  	nla_nest_end(msg, nl_flags);
  	return 0;
  }
  
  static int
ca20ce201   Alexander Aring   ieee802154: add w...
328
329
330
331
332
333
334
335
336
  nl802154_send_wpan_phy_channels(struct cfg802154_registered_device *rdev,
  				struct sk_buff *msg)
  {
  	struct nlattr *nl_page;
  	unsigned long page;
  
  	nl_page = nla_nest_start(msg, NL802154_ATTR_CHANNELS_SUPPORTED);
  	if (!nl_page)
  		return -ENOBUFS;
cb41c8dd0   Alexander Aring   ieee802154: renam...
337
  	for (page = 0; page <= IEEE802154_MAX_PAGE; page++) {
ca20ce201   Alexander Aring   ieee802154: add w...
338
  		if (nla_put_u32(msg, NL802154_ATTR_SUPPORTED_CHANNEL,
72f655e44   Alexander Aring   ieee802154: intro...
339
  				rdev->wpan_phy.supported.channels[page]))
ca20ce201   Alexander Aring   ieee802154: add w...
340
341
342
343
344
345
  			return -ENOBUFS;
  	}
  	nla_nest_end(msg, nl_page);
  
  	return 0;
  }
0e6654570   Alexander Aring   nl802154: add sup...
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
  static int
  nl802154_put_capabilities(struct sk_buff *msg,
  			  struct cfg802154_registered_device *rdev)
  {
  	const struct wpan_phy_supported *caps = &rdev->wpan_phy.supported;
  	struct nlattr *nl_caps, *nl_channels;
  	int i;
  
  	nl_caps = nla_nest_start(msg, NL802154_ATTR_WPAN_PHY_CAPS);
  	if (!nl_caps)
  		return -ENOBUFS;
  
  	nl_channels = nla_nest_start(msg, NL802154_CAP_ATTR_CHANNELS);
  	if (!nl_channels)
  		return -ENOBUFS;
  
  	for (i = 0; i <= IEEE802154_MAX_PAGE; i++) {
  		if (caps->channels[i]) {
  			if (nl802154_put_flags(msg, i, caps->channels[i]))
  				return -ENOBUFS;
  		}
  	}
  
  	nla_nest_end(msg, nl_channels);
  
  	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
  		struct nlattr *nl_ed_lvls;
  
  		nl_ed_lvls = nla_nest_start(msg,
  					    NL802154_CAP_ATTR_CCA_ED_LEVELS);
  		if (!nl_ed_lvls)
  			return -ENOBUFS;
  
  		for (i = 0; i < caps->cca_ed_levels_size; i++) {
  			if (nla_put_s32(msg, i, caps->cca_ed_levels[i]))
  				return -ENOBUFS;
  		}
  
  		nla_nest_end(msg, nl_ed_lvls);
  	}
  
  	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
  		struct nlattr *nl_tx_pwrs;
  
  		nl_tx_pwrs = nla_nest_start(msg, NL802154_CAP_ATTR_TX_POWERS);
  		if (!nl_tx_pwrs)
  			return -ENOBUFS;
  
  		for (i = 0; i < caps->tx_powers_size; i++) {
  			if (nla_put_s32(msg, i, caps->tx_powers[i]))
  				return -ENOBUFS;
  		}
  
  		nla_nest_end(msg, nl_tx_pwrs);
  	}
  
  	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
  		if (nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_MODES,
  				       caps->cca_modes) ||
  		    nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_OPTS,
  				       caps->cca_opts))
  			return -ENOBUFS;
  	}
  
  	if (nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MINBE, caps->min_minbe) ||
  	    nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MINBE, caps->max_minbe) ||
  	    nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MAXBE, caps->min_maxbe) ||
  	    nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MAXBE, caps->max_maxbe) ||
  	    nla_put_u8(msg, NL802154_CAP_ATTR_MIN_CSMA_BACKOFFS,
  		       caps->min_csma_backoffs) ||
  	    nla_put_u8(msg, NL802154_CAP_ATTR_MAX_CSMA_BACKOFFS,
  		       caps->max_csma_backoffs) ||
  	    nla_put_s8(msg, NL802154_CAP_ATTR_MIN_FRAME_RETRIES,
  		       caps->min_frame_retries) ||
  	    nla_put_s8(msg, NL802154_CAP_ATTR_MAX_FRAME_RETRIES,
  		       caps->max_frame_retries) ||
  	    nl802154_put_flags(msg, NL802154_CAP_ATTR_IFTYPES,
  			       caps->iftypes) ||
  	    nla_put_u32(msg, NL802154_CAP_ATTR_LBT, caps->lbt))
  		return -ENOBUFS;
  
  	nla_nest_end(msg, nl_caps);
  
  	return 0;
  }
ca20ce201   Alexander Aring   ieee802154: add w...
431
432
433
434
435
  static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev,
  				  enum nl802154_commands cmd,
  				  struct sk_buff *msg, u32 portid, u32 seq,
  				  int flags)
  {
133be0264   Varka Bhadram   nl802154: export ...
436
  	struct nlattr *nl_cmds;
ca20ce201   Alexander Aring   ieee802154: add w...
437
  	void *hdr;
133be0264   Varka Bhadram   nl802154: export ...
438
  	int i;
ca20ce201   Alexander Aring   ieee802154: add w...
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
  
  	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
  	if (!hdr)
  		return -ENOBUFS;
  
  	if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
  	    nla_put_string(msg, NL802154_ATTR_WPAN_PHY_NAME,
  			   wpan_phy_name(&rdev->wpan_phy)) ||
  	    nla_put_u32(msg, NL802154_ATTR_GENERATION,
  			cfg802154_rdev_list_generation))
  		goto nla_put_failure;
  
  	if (cmd != NL802154_CMD_NEW_WPAN_PHY)
  		goto finish;
  
  	/* DUMP PHY PIB */
  
  	/* current channel settings */
  	if (nla_put_u8(msg, NL802154_ATTR_PAGE,
  		       rdev->wpan_phy.current_page) ||
  	    nla_put_u8(msg, NL802154_ATTR_CHANNEL,
  		       rdev->wpan_phy.current_channel))
  		goto nla_put_failure;
0e6654570   Alexander Aring   nl802154: add sup...
462
463
464
  	/* TODO remove this behaviour, we still keep support it for a while
  	 * so users can change the behaviour to the new one.
  	 */
ca20ce201   Alexander Aring   ieee802154: add w...
465
466
467
468
  	if (nl802154_send_wpan_phy_channels(rdev, msg))
  		goto nla_put_failure;
  
  	/* cca mode */
edea8f7c7   Alexander Aring   cfg802154: introd...
469
470
471
  	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
  		if (nla_put_u32(msg, NL802154_ATTR_CCA_MODE,
  				rdev->wpan_phy.cca.mode))
ba2a9506a   Alexander Aring   nl802154: introdu...
472
  			goto nla_put_failure;
edea8f7c7   Alexander Aring   cfg802154: introd...
473
474
475
476
477
478
  
  		if (rdev->wpan_phy.cca.mode == NL802154_CCA_ENERGY_CARRIER) {
  			if (nla_put_u32(msg, NL802154_ATTR_CCA_OPT,
  					rdev->wpan_phy.cca.opt))
  				goto nla_put_failure;
  		}
ba2a9506a   Alexander Aring   nl802154: introdu...
479
  	}
edea8f7c7   Alexander Aring   cfg802154: introd...
480
481
482
483
484
  	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
  		if (nla_put_s32(msg, NL802154_ATTR_TX_POWER,
  				rdev->wpan_phy.transmit_power))
  			goto nla_put_failure;
  	}
ca20ce201   Alexander Aring   ieee802154: add w...
485

e4390592a   Alexander Aring   nl802154: add sup...
486
487
488
489
490
  	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
  		if (nla_put_s32(msg, NL802154_ATTR_CCA_ED_LEVEL,
  				rdev->wpan_phy.cca_ed_level))
  			goto nla_put_failure;
  	}
0e6654570   Alexander Aring   nl802154: add sup...
491
492
  	if (nl802154_put_capabilities(msg, rdev))
  		goto nla_put_failure;
133be0264   Varka Bhadram   nl802154: export ...
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
  	nl_cmds = nla_nest_start(msg, NL802154_ATTR_SUPPORTED_COMMANDS);
  	if (!nl_cmds)
  		goto nla_put_failure;
  
  	i = 0;
  #define CMD(op, n)							\
  	do {								\
  		if (rdev->ops->op) {					\
  			i++;						\
  			if (nla_put_u32(msg, i, NL802154_CMD_ ## n))	\
  				goto nla_put_failure;			\
  		}							\
  	} while (0)
  
  	CMD(add_virtual_intf, NEW_INTERFACE);
  	CMD(del_virtual_intf, DEL_INTERFACE);
  	CMD(set_channel, SET_CHANNEL);
  	CMD(set_pan_id, SET_PAN_ID);
  	CMD(set_short_addr, SET_SHORT_ADDR);
  	CMD(set_backoff_exponent, SET_BACKOFF_EXPONENT);
  	CMD(set_max_csma_backoffs, SET_MAX_CSMA_BACKOFFS);
  	CMD(set_max_frame_retries, SET_MAX_FRAME_RETRIES);
  	CMD(set_lbt_mode, SET_LBT_MODE);
c91208d81   Alexander Aring   ieee802154: add a...
516
  	CMD(set_ackreq_default, SET_ACKREQ_DEFAULT);
133be0264   Varka Bhadram   nl802154: export ...
517
518
519
520
521
522
523
524
525
526
527
528
  
  	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER)
  		CMD(set_tx_power, SET_TX_POWER);
  
  	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL)
  		CMD(set_cca_ed_level, SET_CCA_ED_LEVEL);
  
  	if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE)
  		CMD(set_cca_mode, SET_CCA_MODE);
  
  #undef CMD
  	nla_nest_end(msg, nl_cmds);
ca20ce201   Alexander Aring   ieee802154: add w...
529
  finish:
053c095a8   Johannes Berg   netlink: make nlm...
530
531
  	genlmsg_end(msg, hdr);
  	return 0;
ca20ce201   Alexander Aring   ieee802154: add w...
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
  
  nla_put_failure:
  	genlmsg_cancel(msg, hdr);
  	return -EMSGSIZE;
  }
  
  struct nl802154_dump_wpan_phy_state {
  	s64 filter_wpan_phy;
  	long start;
  
  };
  
  static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb,
  					struct netlink_callback *cb,
  					struct nl802154_dump_wpan_phy_state *state)
  {
c90c39dab   Johannes Berg   genetlink: introd...
548
  	struct nlattr **tb = genl_family_attrbuf(&nl802154_fam);
fceb6435e   Johannes Berg   netlink: pass ext...
549
550
  	int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize, tb,
  			      nl802154_fam.maxattr, nl802154_policy, NULL);
ca20ce201   Alexander Aring   ieee802154: add w...
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
  
  	/* TODO check if we can handle error here,
  	 * we have no backward compatibility
  	 */
  	if (ret)
  		return 0;
  
  	if (tb[NL802154_ATTR_WPAN_PHY])
  		state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]);
  	if (tb[NL802154_ATTR_WPAN_DEV])
  		state->filter_wpan_phy = nla_get_u64(tb[NL802154_ATTR_WPAN_DEV]) >> 32;
  	if (tb[NL802154_ATTR_IFINDEX]) {
  		struct net_device *netdev;
  		struct cfg802154_registered_device *rdev;
  		int ifidx = nla_get_u32(tb[NL802154_ATTR_IFINDEX]);
ca20ce201   Alexander Aring   ieee802154: add w...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
  		netdev = __dev_get_by_index(&init_net, ifidx);
  		if (!netdev)
  			return -ENODEV;
  		if (netdev->ieee802154_ptr) {
  			rdev = wpan_phy_to_rdev(
  					netdev->ieee802154_ptr->wpan_phy);
  			state->filter_wpan_phy = rdev->wpan_phy_idx;
  		}
  	}
  
  	return 0;
  }
  
  static int
  nl802154_dump_wpan_phy(struct sk_buff *skb, struct netlink_callback *cb)
  {
  	int idx = 0, ret;
  	struct nl802154_dump_wpan_phy_state *state = (void *)cb->args[0];
  	struct cfg802154_registered_device *rdev;
  
  	rtnl_lock();
  	if (!state) {
  		state = kzalloc(sizeof(*state), GFP_KERNEL);
  		if (!state) {
  			rtnl_unlock();
  			return -ENOMEM;
  		}
  		state->filter_wpan_phy = -1;
  		ret = nl802154_dump_wpan_phy_parse(skb, cb, state);
  		if (ret) {
  			kfree(state);
  			rtnl_unlock();
  			return ret;
  		}
  		cb->args[0] = (long)state;
  	}
  
  	list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
66e5c2672   Alexander Aring   ieee802154: add n...
604
605
  		if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
  			continue;
ca20ce201   Alexander Aring   ieee802154: add w...
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
  		if (++idx <= state->start)
  			continue;
  		if (state->filter_wpan_phy != -1 &&
  		    state->filter_wpan_phy != rdev->wpan_phy_idx)
  			continue;
  		/* attempt to fit multiple wpan_phy data chunks into the skb */
  		ret = nl802154_send_wpan_phy(rdev,
  					     NL802154_CMD_NEW_WPAN_PHY,
  					     skb,
  					     NETLINK_CB(cb->skb).portid,
  					     cb->nlh->nlmsg_seq, NLM_F_MULTI);
  		if (ret < 0) {
  			if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
  			    !skb->len && cb->min_dump_alloc < 4096) {
  				cb->min_dump_alloc = 4096;
  				rtnl_unlock();
  				return 1;
  			}
  			idx--;
  			break;
  		}
  		break;
  	}
  	rtnl_unlock();
  
  	state->start = idx;
  
  	return skb->len;
  }
  
  static int nl802154_dump_wpan_phy_done(struct netlink_callback *cb)
  {
  	kfree((void *)cb->args[0]);
  	return 0;
  }
  
  static int nl802154_get_wpan_phy(struct sk_buff *skb, struct genl_info *info)
  {
  	struct sk_buff *msg;
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  
  	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!msg)
  		return -ENOMEM;
  
  	if (nl802154_send_wpan_phy(rdev, NL802154_CMD_NEW_WPAN_PHY, msg,
  				   info->snd_portid, info->snd_seq, 0) < 0) {
  		nlmsg_free(msg);
  		return -ENOBUFS;
  	}
  
  	return genlmsg_reply(msg, info);
  }
4b96aea0f   Alexander Aring   ieee802154: add w...
659
660
661
662
663
  static inline u64 wpan_dev_id(struct wpan_dev *wpan_dev)
  {
  	return (u64)wpan_dev->identifier |
  	       ((u64)wpan_phy_to_rdev(wpan_dev->wpan_phy)->wpan_phy_idx << 32);
  }
a26c5fd76   Alexander Aring   nl802154: add sup...
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
  #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  #include <net/ieee802154_netdev.h>
  
  static int
  ieee802154_llsec_send_key_id(struct sk_buff *msg,
  			     const struct ieee802154_llsec_key_id *desc)
  {
  	struct nlattr *nl_dev_addr;
  
  	if (nla_put_u32(msg, NL802154_KEY_ID_ATTR_MODE, desc->mode))
  		return -ENOBUFS;
  
  	switch (desc->mode) {
  	case NL802154_KEY_ID_MODE_IMPLICIT:
  		nl_dev_addr = nla_nest_start(msg, NL802154_KEY_ID_ATTR_IMPLICIT);
  		if (!nl_dev_addr)
  			return -ENOBUFS;
  
  		if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_PAN_ID,
  				 desc->device_addr.pan_id) ||
  		    nla_put_u32(msg,  NL802154_DEV_ADDR_ATTR_MODE,
  				desc->device_addr.mode))
  			return -ENOBUFS;
  
  		switch (desc->device_addr.mode) {
  		case NL802154_DEV_ADDR_SHORT:
  			if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_SHORT,
  					 desc->device_addr.short_addr))
  				return -ENOBUFS;
  			break;
  		case NL802154_DEV_ADDR_EXTENDED:
  			if (nla_put_le64(msg, NL802154_DEV_ADDR_ATTR_EXTENDED,
e7479122b   Nicolas Dichtel   libnl: nla_put_le...
696
697
  					 desc->device_addr.extended_addr,
  					 NL802154_DEV_ADDR_ATTR_PAD))
a26c5fd76   Alexander Aring   nl802154: add sup...
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
  				return -ENOBUFS;
  			break;
  		default:
  			/* userspace should handle unknown */
  			break;
  		}
  
  		nla_nest_end(msg, nl_dev_addr);
  		break;
  	case NL802154_KEY_ID_MODE_INDEX:
  		break;
  	case NL802154_KEY_ID_MODE_INDEX_SHORT:
  		/* TODO renmae short_source? */
  		if (nla_put_le32(msg, NL802154_KEY_ID_ATTR_SOURCE_SHORT,
  				 desc->short_source))
  			return -ENOBUFS;
  		break;
  	case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
  		if (nla_put_le64(msg, NL802154_KEY_ID_ATTR_SOURCE_EXTENDED,
e7479122b   Nicolas Dichtel   libnl: nla_put_le...
717
718
  				 desc->extended_source,
  				 NL802154_KEY_ID_ATTR_PAD))
a26c5fd76   Alexander Aring   nl802154: add sup...
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
  			return -ENOBUFS;
  		break;
  	default:
  		/* userspace should handle unknown */
  		break;
  	}
  
  	/* TODO key_id to key_idx ? Check naming */
  	if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
  		if (nla_put_u8(msg, NL802154_KEY_ID_ATTR_INDEX, desc->id))
  			return -ENOBUFS;
  	}
  
  	return 0;
  }
  
  static int nl802154_get_llsec_params(struct sk_buff *msg,
  				     struct cfg802154_registered_device *rdev,
  				     struct wpan_dev *wpan_dev)
  {
  	struct nlattr *nl_key_id;
  	struct ieee802154_llsec_params params;
  	int ret;
  
  	ret = rdev_get_llsec_params(rdev, wpan_dev, &params);
  	if (ret < 0)
  		return ret;
  
  	if (nla_put_u8(msg, NL802154_ATTR_SEC_ENABLED, params.enabled) ||
  	    nla_put_u32(msg, NL802154_ATTR_SEC_OUT_LEVEL, params.out_level) ||
  	    nla_put_be32(msg, NL802154_ATTR_SEC_FRAME_COUNTER,
  			 params.frame_counter))
  		return -ENOBUFS;
  
  	nl_key_id = nla_nest_start(msg, NL802154_ATTR_SEC_OUT_KEY_ID);
  	if (!nl_key_id)
  		return -ENOBUFS;
  
  	ret = ieee802154_llsec_send_key_id(msg, &params.out_key);
  	if (ret < 0)
  		return ret;
  
  	nla_nest_end(msg, nl_key_id);
  
  	return 0;
  }
  #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
4b96aea0f   Alexander Aring   ieee802154: add w...
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
  static int
  nl802154_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
  		    struct cfg802154_registered_device *rdev,
  		    struct wpan_dev *wpan_dev)
  {
  	struct net_device *dev = wpan_dev->netdev;
  	void *hdr;
  
  	hdr = nl802154hdr_put(msg, portid, seq, flags,
  			      NL802154_CMD_NEW_INTERFACE);
  	if (!hdr)
  		return -1;
  
  	if (dev &&
  	    (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex) ||
  	     nla_put_string(msg, NL802154_ATTR_IFNAME, dev->name)))
  		goto nla_put_failure;
  
  	if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
  	    nla_put_u32(msg, NL802154_ATTR_IFTYPE, wpan_dev->iftype) ||
a558da091   Nicolas Dichtel   ieee802154: use n...
786
787
  	    nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
  			      wpan_dev_id(wpan_dev), NL802154_ATTR_PAD) ||
4b96aea0f   Alexander Aring   ieee802154: add w...
788
789
790
791
792
793
794
  	    nla_put_u32(msg, NL802154_ATTR_GENERATION,
  			rdev->devlist_generation ^
  			(cfg802154_rdev_list_generation << 2)))
  		goto nla_put_failure;
  
  	/* address settings */
  	if (nla_put_le64(msg, NL802154_ATTR_EXTENDED_ADDR,
e7479122b   Nicolas Dichtel   libnl: nla_put_le...
795
796
  			 wpan_dev->extended_addr,
  			 NL802154_ATTR_PAD) ||
4b96aea0f   Alexander Aring   ieee802154: add w...
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
  	    nla_put_le16(msg, NL802154_ATTR_SHORT_ADDR,
  			 wpan_dev->short_addr) ||
  	    nla_put_le16(msg, NL802154_ATTR_PAN_ID, wpan_dev->pan_id))
  		goto nla_put_failure;
  
  	/* ARET handling */
  	if (nla_put_s8(msg, NL802154_ATTR_MAX_FRAME_RETRIES,
  		       wpan_dev->frame_retries) ||
  	    nla_put_u8(msg, NL802154_ATTR_MAX_BE, wpan_dev->max_be) ||
  	    nla_put_u8(msg, NL802154_ATTR_MAX_CSMA_BACKOFFS,
  		       wpan_dev->csma_retries) ||
  	    nla_put_u8(msg, NL802154_ATTR_MIN_BE, wpan_dev->min_be))
  		goto nla_put_failure;
  
  	/* listen before transmit */
  	if (nla_put_u8(msg, NL802154_ATTR_LBT_MODE, wpan_dev->lbt))
  		goto nla_put_failure;
c91208d81   Alexander Aring   ieee802154: add a...
814
815
816
  	/* ackreq default behaviour */
  	if (nla_put_u8(msg, NL802154_ATTR_ACKREQ_DEFAULT, wpan_dev->ackreq))
  		goto nla_put_failure;
a26c5fd76   Alexander Aring   nl802154: add sup...
817
818
819
820
  #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  	if (nl802154_get_llsec_params(msg, rdev, wpan_dev) < 0)
  		goto nla_put_failure;
  #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
053c095a8   Johannes Berg   netlink: make nlm...
821
822
  	genlmsg_end(msg, hdr);
  	return 0;
4b96aea0f   Alexander Aring   ieee802154: add w...
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
  
  nla_put_failure:
  	genlmsg_cancel(msg, hdr);
  	return -EMSGSIZE;
  }
  
  static int
  nl802154_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
  {
  	int wp_idx = 0;
  	int if_idx = 0;
  	int wp_start = cb->args[0];
  	int if_start = cb->args[1];
  	struct cfg802154_registered_device *rdev;
  	struct wpan_dev *wpan_dev;
  
  	rtnl_lock();
  	list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
66e5c2672   Alexander Aring   ieee802154: add n...
841
842
  		if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
  			continue;
4b96aea0f   Alexander Aring   ieee802154: add w...
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
  		if (wp_idx < wp_start) {
  			wp_idx++;
  			continue;
  		}
  		if_idx = 0;
  
  		list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
  			if (if_idx < if_start) {
  				if_idx++;
  				continue;
  			}
  			if (nl802154_send_iface(skb, NETLINK_CB(cb->skb).portid,
  						cb->nlh->nlmsg_seq, NLM_F_MULTI,
  						rdev, wpan_dev) < 0) {
  				goto out;
  			}
  			if_idx++;
  		}
  
  		wp_idx++;
  	}
  out:
  	rtnl_unlock();
  
  	cb->args[0] = wp_idx;
  	cb->args[1] = if_idx;
  
  	return skb->len;
  }
  
  static int nl802154_get_interface(struct sk_buff *skb, struct genl_info *info)
  {
  	struct sk_buff *msg;
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct wpan_dev *wdev = info->user_ptr[1];
  
  	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!msg)
  		return -ENOMEM;
  
  	if (nl802154_send_iface(msg, info->snd_portid, info->snd_seq, 0,
  				rdev, wdev) < 0) {
  		nlmsg_free(msg);
  		return -ENOBUFS;
  	}
  
  	return genlmsg_reply(msg, info);
  }
f3ea5e442   Alexander Aring   ieee802154: add n...
891
892
893
894
  static int nl802154_new_interface(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	enum nl802154_iftype type = NL802154_IFTYPE_UNSPEC;
0e57547eb   Alexander Aring   ieee802154: setti...
895
  	__le64 extended_addr = cpu_to_le64(0x0000000000000000ULL);
f3ea5e442   Alexander Aring   ieee802154: add n...
896
897
898
899
900
901
902
903
904
905
  
  	/* TODO avoid failing a new interface
  	 * creation due to pending removal?
  	 */
  
  	if (!info->attrs[NL802154_ATTR_IFNAME])
  		return -EINVAL;
  
  	if (info->attrs[NL802154_ATTR_IFTYPE]) {
  		type = nla_get_u32(info->attrs[NL802154_ATTR_IFTYPE]);
65318680c   Alexander Aring   ieee802154: add i...
906
907
  		if (type > NL802154_IFTYPE_MAX ||
  		    !(rdev->wpan_phy.supported.iftypes & BIT(type)))
f3ea5e442   Alexander Aring   ieee802154: add n...
908
909
  			return -EINVAL;
  	}
0e57547eb   Alexander Aring   ieee802154: setti...
910
  	if (info->attrs[NL802154_ATTR_EXTENDED_ADDR])
1ee06ef15   Alexander Aring   nl802154: use nla...
911
  		extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
0e57547eb   Alexander Aring   ieee802154: setti...
912

f3ea5e442   Alexander Aring   ieee802154: add n...
913
914
915
916
917
  	if (!rdev->ops->add_virtual_intf)
  		return -EOPNOTSUPP;
  
  	return rdev_add_virtual_intf(rdev,
  				     nla_data(info->attrs[NL802154_ATTR_IFNAME]),
5b4a10390   Varka Bhadram   cfg802154: pass n...
918
  				     NET_NAME_USER, type, extended_addr);
f3ea5e442   Alexander Aring   ieee802154: add n...
919
  }
b821ecd4c   Alexander Aring   ieee802154: add d...
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
  static int nl802154_del_interface(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct wpan_dev *wpan_dev = info->user_ptr[1];
  
  	if (!rdev->ops->del_virtual_intf)
  		return -EOPNOTSUPP;
  
  	/* If we remove a wpan device without a netdev then clear
  	 * user_ptr[1] so that nl802154_post_doit won't dereference it
  	 * to check if it needs to do dev_put(). Otherwise it crashes
  	 * since the wpan_dev has been freed, unlike with a netdev where
  	 * we need the dev_put() for the netdev to really be freed.
  	 */
  	if (!wpan_dev->netdev)
  		info->user_ptr[1] = NULL;
  
  	return rdev_del_virtual_intf(rdev, wpan_dev);
  }
ab0bd5617   Alexander Aring   ieee820154: add c...
939
940
941
942
943
944
945
946
947
948
949
950
951
  static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	u8 channel, page;
  
  	if (!info->attrs[NL802154_ATTR_PAGE] ||
  	    !info->attrs[NL802154_ATTR_CHANNEL])
  		return -EINVAL;
  
  	page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
  	channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]);
  
  	/* check 802.15.4 constraints */
673692faf   Alexander Aring   ieee802154: move ...
952
  	if (page > IEEE802154_MAX_PAGE || channel > IEEE802154_MAX_CHANNEL ||
72f655e44   Alexander Aring   ieee802154: intro...
953
  	    !(rdev->wpan_phy.supported.channels[page] & BIT(channel)))
ab0bd5617   Alexander Aring   ieee820154: add c...
954
955
956
957
  		return -EINVAL;
  
  	return rdev_set_channel(rdev, page, channel);
  }
ba2a9506a   Alexander Aring   nl802154: introdu...
958
959
960
961
  static int nl802154_set_cca_mode(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct wpan_phy_cca cca;
fc4f80524   Alexander Aring   nl802154: fix cca...
962
  	if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE))
edea8f7c7   Alexander Aring   cfg802154: introd...
963
  		return -EOPNOTSUPP;
ba2a9506a   Alexander Aring   nl802154: introdu...
964
965
966
967
968
  	if (!info->attrs[NL802154_ATTR_CCA_MODE])
  		return -EINVAL;
  
  	cca.mode = nla_get_u32(info->attrs[NL802154_ATTR_CCA_MODE]);
  	/* checking 802.15.4 constraints */
fea3318d2   Alexander Aring   ieee802154: add s...
969
970
971
  	if (cca.mode < NL802154_CCA_ENERGY ||
  	    cca.mode > NL802154_CCA_ATTR_MAX ||
  	    !(rdev->wpan_phy.supported.cca_modes & BIT(cca.mode)))
ba2a9506a   Alexander Aring   nl802154: introdu...
972
973
974
975
976
977
978
  		return -EINVAL;
  
  	if (cca.mode == NL802154_CCA_ENERGY_CARRIER) {
  		if (!info->attrs[NL802154_ATTR_CCA_OPT])
  			return -EINVAL;
  
  		cca.opt = nla_get_u32(info->attrs[NL802154_ATTR_CCA_OPT]);
fea3318d2   Alexander Aring   ieee802154: add s...
979
980
  		if (cca.opt > NL802154_CCA_OPT_ATTR_MAX ||
  		    !(rdev->wpan_phy.supported.cca_opts & BIT(cca.opt)))
ba2a9506a   Alexander Aring   nl802154: introdu...
981
982
983
984
985
  			return -EINVAL;
  	}
  
  	return rdev_set_cca_mode(rdev, &cca);
  }
b69644c1c   Alexander Aring   nl802154: add sup...
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
  static int nl802154_set_cca_ed_level(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	s32 ed_level;
  	int i;
  
  	if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL))
  		return -EOPNOTSUPP;
  
  	if (!info->attrs[NL802154_ATTR_CCA_ED_LEVEL])
  		return -EINVAL;
  
  	ed_level = nla_get_s32(info->attrs[NL802154_ATTR_CCA_ED_LEVEL]);
  
  	for (i = 0; i < rdev->wpan_phy.supported.cca_ed_levels_size; i++) {
  		if (ed_level == rdev->wpan_phy.supported.cca_ed_levels[i])
  			return rdev_set_cca_ed_level(rdev, ed_level);
  	}
  
  	return -EINVAL;
  }
0f999b09f   Varka Bhadram   ieee802154: add s...
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
  static int nl802154_set_tx_power(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	s32 power;
  	int i;
  
  	if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER))
  		return -EOPNOTSUPP;
  
  	if (!info->attrs[NL802154_ATTR_TX_POWER])
  		return -EINVAL;
  
  	power = nla_get_s32(info->attrs[NL802154_ATTR_TX_POWER]);
  
  	for (i = 0; i < rdev->wpan_phy.supported.tx_powers_size; i++) {
  		if (power == rdev->wpan_phy.supported.tx_powers[i])
  			return rdev_set_tx_power(rdev, power);
  	}
  
  	return -EINVAL;
  }
702bf3712   Alexander Aring   ieee820154: add p...
1028
1029
1030
1031
1032
  static int nl802154_set_pan_id(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
ee7b9053b   Alexander Aring   ieee802154: fix b...
1033
  	__le16 pan_id;
702bf3712   Alexander Aring   ieee820154: add p...
1034
1035
1036
1037
  
  	/* conflict here while tx/rx calls */
  	if (netif_running(dev))
  		return -EBUSY;
9e3b71f34   Alexander Aring   nl802154: avoid a...
1038
1039
1040
1041
  	if (wpan_dev->lowpan_dev) {
  		if (netif_running(wpan_dev->lowpan_dev))
  			return -EBUSY;
  	}
702bf3712   Alexander Aring   ieee820154: add p...
1042
  	/* don't change address fields on monitor */
0cf0879ac   Alexander Aring   nl802154: cleanup...
1043
1044
  	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
  	    !info->attrs[NL802154_ATTR_PAN_ID])
702bf3712   Alexander Aring   ieee820154: add p...
1045
  		return -EINVAL;
ee7b9053b   Alexander Aring   ieee802154: fix b...
1046
  	pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
702bf3712   Alexander Aring   ieee820154: add p...
1047

673692faf   Alexander Aring   ieee802154: move ...
1048
1049
1050
1051
1052
1053
1054
1055
1056
  	/* TODO
  	 * I am not sure about to check here on broadcast pan_id.
  	 * Broadcast is a valid setting, comment from 802.15.4:
  	 * If this value is 0xffff, the device is not associated.
  	 *
  	 * This could useful to simple deassociate an device.
  	 */
  	if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
  		return -EINVAL;
702bf3712   Alexander Aring   ieee820154: add p...
1057
1058
  	return rdev_set_pan_id(rdev, wpan_dev, pan_id);
  }
9830c62a0   Alexander Aring   ieee820154: add s...
1059
1060
1061
1062
1063
  static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
ee7b9053b   Alexander Aring   ieee802154: fix b...
1064
  	__le16 short_addr;
9830c62a0   Alexander Aring   ieee820154: add s...
1065
1066
1067
1068
  
  	/* conflict here while tx/rx calls */
  	if (netif_running(dev))
  		return -EBUSY;
9e3b71f34   Alexander Aring   nl802154: avoid a...
1069
1070
1071
1072
  	if (wpan_dev->lowpan_dev) {
  		if (netif_running(wpan_dev->lowpan_dev))
  			return -EBUSY;
  	}
9830c62a0   Alexander Aring   ieee820154: add s...
1073
  	/* don't change address fields on monitor */
0cf0879ac   Alexander Aring   nl802154: cleanup...
1074
1075
  	if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
  	    !info->attrs[NL802154_ATTR_SHORT_ADDR])
9830c62a0   Alexander Aring   ieee820154: add s...
1076
  		return -EINVAL;
ee7b9053b   Alexander Aring   ieee802154: fix b...
1077
  	short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
9830c62a0   Alexander Aring   ieee820154: add s...
1078

673692faf   Alexander Aring   ieee802154: move ...
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
  	/* TODO
  	 * I am not sure about to check here on broadcast short_addr.
  	 * Broadcast is a valid setting, comment from 802.15.4:
  	 * A value of 0xfffe indicates that the device has
  	 * associated but has not been allocated an address. A
  	 * value of 0xffff indicates that the device does not
  	 * have a short address.
  	 *
  	 * I think we should allow to set these settings but
  	 * don't allow to allow socket communication with it.
  	 */
  	if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
  	    short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
  		return -EINVAL;
9830c62a0   Alexander Aring   ieee820154: add s...
1093
1094
  	return rdev_set_short_addr(rdev, wpan_dev, short_addr);
  }
656a999e8   Alexander Aring   ieee820154: add b...
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
  static int
  nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	u8 min_be, max_be;
  
  	/* should be set on netif open inside phy settings */
  	if (netif_running(dev))
  		return -EBUSY;
  
  	if (!info->attrs[NL802154_ATTR_MIN_BE] ||
  	    !info->attrs[NL802154_ATTR_MAX_BE])
  		return -EINVAL;
  
  	min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]);
  	max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]);
  
  	/* check 802.15.4 constraints */
fea3318d2   Alexander Aring   ieee802154: add s...
1115
1116
1117
1118
1119
  	if (min_be < rdev->wpan_phy.supported.min_minbe ||
  	    min_be > rdev->wpan_phy.supported.max_minbe ||
  	    max_be < rdev->wpan_phy.supported.min_maxbe ||
  	    max_be > rdev->wpan_phy.supported.max_maxbe ||
  	    min_be > max_be)
656a999e8   Alexander Aring   ieee820154: add b...
1120
1121
1122
1123
  		return -EINVAL;
  
  	return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
  }
a01ba7652   Alexander Aring   ieee820154: add m...
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
  static int
  nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	u8 max_csma_backoffs;
  
  	/* conflict here while other running iface settings */
  	if (netif_running(dev))
  		return -EBUSY;
  
  	if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS])
  		return -EINVAL;
  
  	max_csma_backoffs = nla_get_u8(
  			info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]);
  
  	/* check 802.15.4 constraints */
fea3318d2   Alexander Aring   ieee802154: add s...
1143
1144
  	if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs ||
  	    max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs)
a01ba7652   Alexander Aring   ieee820154: add m...
1145
1146
1147
1148
  		return -EINVAL;
  
  	return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
  }
17a3a46bf   Alexander Aring   ieee820154: add m...
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
  static int
  nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	s8 max_frame_retries;
  
  	if (netif_running(dev))
  		return -EBUSY;
  
  	if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES])
  		return -EINVAL;
  
  	max_frame_retries = nla_get_s8(
  			info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]);
  
  	/* check 802.15.4 constraints */
fea3318d2   Alexander Aring   ieee802154: add s...
1167
1168
  	if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries ||
  	    max_frame_retries > rdev->wpan_phy.supported.max_frame_retries)
17a3a46bf   Alexander Aring   ieee820154: add m...
1169
1170
1171
1172
  		return -EINVAL;
  
  	return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
  }
c8937a1d1   Alexander Aring   ieee820154: add l...
1173
1174
1175
1176
1177
  static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
4e1795de1   Stefan Schmidt   nl802154: stricte...
1178
  	int mode;
c8937a1d1   Alexander Aring   ieee820154: add l...
1179
1180
1181
1182
1183
1184
  
  	if (netif_running(dev))
  		return -EBUSY;
  
  	if (!info->attrs[NL802154_ATTR_LBT_MODE])
  		return -EINVAL;
4e1795de1   Stefan Schmidt   nl802154: stricte...
1185
1186
1187
1188
  	mode = nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
  
  	if (mode != 0 && mode != 1)
  		return -EINVAL;
fea3318d2   Alexander Aring   ieee802154: add s...
1189
1190
  	if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt))
  		return -EINVAL;
c8937a1d1   Alexander Aring   ieee820154: add l...
1191
1192
  	return rdev_set_lbt_mode(rdev, wpan_dev, mode);
  }
c91208d81   Alexander Aring   ieee802154: add a...
1193
1194
1195
1196
1197
1198
  static int
  nl802154_set_ackreq_default(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
4e1795de1   Stefan Schmidt   nl802154: stricte...
1199
  	int ackreq;
c91208d81   Alexander Aring   ieee802154: add a...
1200
1201
1202
1203
1204
1205
  
  	if (netif_running(dev))
  		return -EBUSY;
  
  	if (!info->attrs[NL802154_ATTR_ACKREQ_DEFAULT])
  		return -EINVAL;
4e1795de1   Stefan Schmidt   nl802154: stricte...
1206
1207
1208
1209
  	ackreq = nla_get_u8(info->attrs[NL802154_ATTR_ACKREQ_DEFAULT]);
  
  	if (ackreq != 0 && ackreq != 1)
  		return -EINVAL;
c91208d81   Alexander Aring   ieee802154: add a...
1210
1211
  	return rdev_set_ackreq_default(rdev, wpan_dev, ackreq);
  }
66e5c2672   Alexander Aring   ieee802154: add n...
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
  static int nl802154_wpan_phy_netns(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net *net;
  	int err;
  
  	if (info->attrs[NL802154_ATTR_PID]) {
  		u32 pid = nla_get_u32(info->attrs[NL802154_ATTR_PID]);
  
  		net = get_net_ns_by_pid(pid);
  	} else if (info->attrs[NL802154_ATTR_NETNS_FD]) {
  		u32 fd = nla_get_u32(info->attrs[NL802154_ATTR_NETNS_FD]);
  
  		net = get_net_ns_by_fd(fd);
  	} else {
  		return -EINVAL;
  	}
  
  	if (IS_ERR(net))
  		return PTR_ERR(net);
  
  	err = 0;
  
  	/* check if anything to do */
  	if (!net_eq(wpan_phy_net(&rdev->wpan_phy), net))
  		err = cfg802154_switch_netns(rdev, net);
  
  	put_net(net);
  	return err;
  }
a26c5fd76   Alexander Aring   nl802154: add sup...
1242
1243
1244
1245
1246
1247
1248
  #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  static const struct nla_policy nl802154_dev_addr_policy[NL802154_DEV_ADDR_ATTR_MAX + 1] = {
  	[NL802154_DEV_ADDR_ATTR_PAN_ID] = { .type = NLA_U16 },
  	[NL802154_DEV_ADDR_ATTR_MODE] = { .type = NLA_U32 },
  	[NL802154_DEV_ADDR_ATTR_SHORT] = { .type = NLA_U16 },
  	[NL802154_DEV_ADDR_ATTR_EXTENDED] = { .type = NLA_U64 },
  };
79fe1a2aa   Alexander Aring   ieee802154: add n...
1249

a26c5fd76   Alexander Aring   nl802154: add sup...
1250
1251
1252
  static int
  ieee802154_llsec_parse_dev_addr(struct nlattr *nla,
  				struct ieee802154_addr *addr)
79fe1a2aa   Alexander Aring   ieee802154: add n...
1253
  {
a26c5fd76   Alexander Aring   nl802154: add sup...
1254
  	struct nlattr *attrs[NL802154_DEV_ADDR_ATTR_MAX + 1];
79fe1a2aa   Alexander Aring   ieee802154: add n...
1255

a26c5fd76   Alexander Aring   nl802154: add sup...
1256
  	if (!nla || nla_parse_nested(attrs, NL802154_DEV_ADDR_ATTR_MAX, nla,
fceb6435e   Johannes Berg   netlink: pass ext...
1257
  				     nl802154_dev_addr_policy, NULL))
a26c5fd76   Alexander Aring   nl802154: add sup...
1258
  		return -EINVAL;
79fe1a2aa   Alexander Aring   ieee802154: add n...
1259

421eeea10   Baozeng Ding   ieee802154: fix l...
1260
1261
  	if (!attrs[NL802154_DEV_ADDR_ATTR_PAN_ID] ||
  	    !attrs[NL802154_DEV_ADDR_ATTR_MODE] ||
a26c5fd76   Alexander Aring   nl802154: add sup...
1262
1263
1264
  	    !(attrs[NL802154_DEV_ADDR_ATTR_SHORT] ||
  	      attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]))
  		return -EINVAL;
79fe1a2aa   Alexander Aring   ieee802154: add n...
1265

a26c5fd76   Alexander Aring   nl802154: add sup...
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
  	addr->pan_id = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_PAN_ID]);
  	addr->mode = nla_get_u32(attrs[NL802154_DEV_ADDR_ATTR_MODE]);
  	switch (addr->mode) {
  	case NL802154_DEV_ADDR_SHORT:
  		addr->short_addr = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_SHORT]);
  		break;
  	case NL802154_DEV_ADDR_EXTENDED:
  		addr->extended_addr = nla_get_le64(attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]);
  		break;
  	default:
  		return -EINVAL;
  	}
79fe1a2aa   Alexander Aring   ieee802154: add n...
1278

a26c5fd76   Alexander Aring   nl802154: add sup...
1279
1280
  	return 0;
  }
79fe1a2aa   Alexander Aring   ieee802154: add n...
1281

a26c5fd76   Alexander Aring   nl802154: add sup...
1282
1283
1284
1285
1286
1287
1288
  static const struct nla_policy nl802154_key_id_policy[NL802154_KEY_ID_ATTR_MAX + 1] = {
  	[NL802154_KEY_ID_ATTR_MODE] = { .type = NLA_U32 },
  	[NL802154_KEY_ID_ATTR_INDEX] = { .type = NLA_U8 },
  	[NL802154_KEY_ID_ATTR_IMPLICIT] = { .type = NLA_NESTED },
  	[NL802154_KEY_ID_ATTR_SOURCE_SHORT] = { .type = NLA_U32 },
  	[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED] = { .type = NLA_U64 },
  };
79fe1a2aa   Alexander Aring   ieee802154: add n...
1289

a26c5fd76   Alexander Aring   nl802154: add sup...
1290
1291
1292
1293
1294
  static int
  ieee802154_llsec_parse_key_id(struct nlattr *nla,
  			      struct ieee802154_llsec_key_id *desc)
  {
  	struct nlattr *attrs[NL802154_KEY_ID_ATTR_MAX + 1];
79fe1a2aa   Alexander Aring   ieee802154: add n...
1295

a26c5fd76   Alexander Aring   nl802154: add sup...
1296
  	if (!nla || nla_parse_nested(attrs, NL802154_KEY_ID_ATTR_MAX, nla,
fceb6435e   Johannes Berg   netlink: pass ext...
1297
  				     nl802154_key_id_policy, NULL))
a26c5fd76   Alexander Aring   nl802154: add sup...
1298
  		return -EINVAL;
79fe1a2aa   Alexander Aring   ieee802154: add n...
1299

a26c5fd76   Alexander Aring   nl802154: add sup...
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
  	if (!attrs[NL802154_KEY_ID_ATTR_MODE])
  		return -EINVAL;
  
  	desc->mode = nla_get_u32(attrs[NL802154_KEY_ID_ATTR_MODE]);
  	switch (desc->mode) {
  	case NL802154_KEY_ID_MODE_IMPLICIT:
  		if (!attrs[NL802154_KEY_ID_ATTR_IMPLICIT])
  			return -EINVAL;
  
  		if (ieee802154_llsec_parse_dev_addr(attrs[NL802154_KEY_ID_ATTR_IMPLICIT],
  						    &desc->device_addr) < 0)
  			return -EINVAL;
  		break;
  	case NL802154_KEY_ID_MODE_INDEX:
  		break;
  	case NL802154_KEY_ID_MODE_INDEX_SHORT:
  		if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT])
  			return -EINVAL;
  
  		desc->short_source = nla_get_le32(attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT]);
  		break;
  	case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
  		if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED])
  			return -EINVAL;
  
  		desc->extended_source = nla_get_le64(attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED]);
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
  		if (!attrs[NL802154_KEY_ID_ATTR_INDEX])
  			return -EINVAL;
  
  		/* TODO change id to idx */
  		desc->id = nla_get_u8(attrs[NL802154_KEY_ID_ATTR_INDEX]);
79fe1a2aa   Alexander Aring   ieee802154: add n...
1337
1338
1339
1340
  	}
  
  	return 0;
  }
a26c5fd76   Alexander Aring   nl802154: add sup...
1341
1342
  static int nl802154_set_llsec_params(struct sk_buff *skb,
  				     struct genl_info *info)
79fe1a2aa   Alexander Aring   ieee802154: add n...
1343
  {
a26c5fd76   Alexander Aring   nl802154: add sup...
1344
1345
1346
1347
1348
1349
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	struct ieee802154_llsec_params params;
  	u32 changed = 0;
  	int ret;
79fe1a2aa   Alexander Aring   ieee802154: add n...
1350

a26c5fd76   Alexander Aring   nl802154: add sup...
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
  	if (info->attrs[NL802154_ATTR_SEC_ENABLED]) {
  		u8 enabled;
  
  		enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
  		if (enabled != 0 && enabled != 1)
  			return -EINVAL;
  
  		params.enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
  		changed |= IEEE802154_LLSEC_PARAM_ENABLED;
  	}
  
  	if (info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID]) {
  		ret = ieee802154_llsec_parse_key_id(info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID],
  						    &params.out_key);
  		if (ret < 0)
  			return ret;
  
  		changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
  	}
  
  	if (info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]) {
  		params.out_level = nla_get_u32(info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]);
  		if (params.out_level > NL802154_SECLEVEL_MAX)
  			return -EINVAL;
  
  		changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
  	}
  
  	if (info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]) {
  		params.frame_counter = nla_get_be32(info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]);
  		changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
  	}
  
  	return rdev_set_llsec_params(rdev, wpan_dev, &params, changed);
  }
  
  static int nl802154_send_key(struct sk_buff *msg, u32 cmd, u32 portid,
  			     u32 seq, int flags,
  			     struct cfg802154_registered_device *rdev,
  			     struct net_device *dev,
  			     const struct ieee802154_llsec_key_entry *key)
  {
  	void *hdr;
  	u32 commands[NL802154_CMD_FRAME_NR_IDS / 32];
  	struct nlattr *nl_key, *nl_key_id;
  
  	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
  	if (!hdr)
  		return -1;
  
  	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
  		goto nla_put_failure;
  
  	nl_key = nla_nest_start(msg, NL802154_ATTR_SEC_KEY);
  	if (!nl_key)
  		goto nla_put_failure;
  
  	nl_key_id = nla_nest_start(msg, NL802154_KEY_ATTR_ID);
  	if (!nl_key_id)
  		goto nla_put_failure;
  
  	if (ieee802154_llsec_send_key_id(msg, &key->id) < 0)
  		goto nla_put_failure;
  
  	nla_nest_end(msg, nl_key_id);
  
  	if (nla_put_u8(msg, NL802154_KEY_ATTR_USAGE_FRAMES,
  		       key->key->frame_types))
  		goto nla_put_failure;
  
  	if (key->key->frame_types & BIT(NL802154_FRAME_CMD)) {
  		/* TODO for each nested */
  		memset(commands, 0, sizeof(commands));
  		commands[7] = key->key->cmd_frame_ids;
  		if (nla_put(msg, NL802154_KEY_ATTR_USAGE_CMDS,
  			    sizeof(commands), commands))
  			goto nla_put_failure;
  	}
  
  	if (nla_put(msg, NL802154_KEY_ATTR_BYTES, NL802154_KEY_SIZE,
  		    key->key->key))
  		goto nla_put_failure;
  
  	nla_nest_end(msg, nl_key);
  	genlmsg_end(msg, hdr);
  
  	return 0;
  
  nla_put_failure:
  	genlmsg_cancel(msg, hdr);
  	return -EMSGSIZE;
  }
  
  static int
  nl802154_dump_llsec_key(struct sk_buff *skb, struct netlink_callback *cb)
  {
  	struct cfg802154_registered_device *rdev = NULL;
  	struct ieee802154_llsec_key_entry *key;
  	struct ieee802154_llsec_table *table;
  	struct wpan_dev *wpan_dev;
  	int err;
  
  	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
  	if (err)
  		return err;
  
  	if (!wpan_dev->netdev) {
  		err = -EINVAL;
  		goto out_err;
  	}
  
  	rdev_lock_llsec_table(rdev, wpan_dev);
  	rdev_get_llsec_table(rdev, wpan_dev, &table);
  
  	/* TODO make it like station dump */
  	if (cb->args[2])
  		goto out;
  
  	list_for_each_entry(key, &table->keys, list) {
  		if (nl802154_send_key(skb, NL802154_CMD_NEW_SEC_KEY,
  				      NETLINK_CB(cb->skb).portid,
  				      cb->nlh->nlmsg_seq, NLM_F_MULTI,
  				      rdev, wpan_dev->netdev, key) < 0) {
  			/* TODO */
  			err = -EIO;
  			rdev_unlock_llsec_table(rdev, wpan_dev);
  			goto out_err;
79fe1a2aa   Alexander Aring   ieee802154: add n...
1478
1479
  		}
  	}
a26c5fd76   Alexander Aring   nl802154: add sup...
1480
1481
1482
1483
1484
1485
1486
1487
1488
  	cb->args[2] = 1;
  
  out:
  	rdev_unlock_llsec_table(rdev, wpan_dev);
  	err = skb->len;
  out_err:
  	nl802154_finish_wpan_dev_dump(rdev);
  
  	return err;
79fe1a2aa   Alexander Aring   ieee802154: add n...
1489
  }
a26c5fd76   Alexander Aring   nl802154: add sup...
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
  static const struct nla_policy nl802154_key_policy[NL802154_KEY_ATTR_MAX + 1] = {
  	[NL802154_KEY_ATTR_ID] = { NLA_NESTED },
  	/* TODO handle it as for_each_nested and NLA_FLAG? */
  	[NL802154_KEY_ATTR_USAGE_FRAMES] = { NLA_U8 },
  	/* TODO handle it as for_each_nested, not static array? */
  	[NL802154_KEY_ATTR_USAGE_CMDS] = { .len = NL802154_CMD_FRAME_NR_IDS / 8 },
  	[NL802154_KEY_ATTR_BYTES] = { .len = NL802154_KEY_SIZE },
  };
  
  static int nl802154_add_llsec_key(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
  	struct ieee802154_llsec_key key = { };
  	struct ieee802154_llsec_key_id id = { };
  	u32 commands[NL802154_CMD_FRAME_NR_IDS / 32] = { };
  
  	if (nla_parse_nested(attrs, NL802154_KEY_ATTR_MAX,
  			     info->attrs[NL802154_ATTR_SEC_KEY],
fe52145f9   Johannes Berg   netlink: pass ext...
1511
  			     nl802154_key_policy, info->extack))
a26c5fd76   Alexander Aring   nl802154: add sup...
1512
1513
1514
1515
  		return -EINVAL;
  
  	if (!attrs[NL802154_KEY_ATTR_USAGE_FRAMES] ||
  	    !attrs[NL802154_KEY_ATTR_BYTES])
aa6555622   Dan Carpenter   nl802154: Missing...
1516
  		return -EINVAL;
a26c5fd76   Alexander Aring   nl802154: add sup...
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
  
  	if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
  		return -ENOBUFS;
  
  	key.frame_types = nla_get_u8(attrs[NL802154_KEY_ATTR_USAGE_FRAMES]);
  	if (key.frame_types > BIT(NL802154_FRAME_MAX) ||
  	    ((key.frame_types & BIT(NL802154_FRAME_CMD)) &&
  	     !attrs[NL802154_KEY_ATTR_USAGE_CMDS]))
  		return -EINVAL;
  
  	if (attrs[NL802154_KEY_ATTR_USAGE_CMDS]) {
  		/* TODO for each nested */
  		nla_memcpy(commands, attrs[NL802154_KEY_ATTR_USAGE_CMDS],
  			   NL802154_CMD_FRAME_NR_IDS / 8);
  
  		/* TODO understand the -EINVAL logic here? last condition */
  		if (commands[0] || commands[1] || commands[2] || commands[3] ||
  		    commands[4] || commands[5] || commands[6] ||
  		    commands[7] > BIT(NL802154_CMD_FRAME_MAX))
  			return -EINVAL;
  
  		key.cmd_frame_ids = commands[7];
  	} else {
  		key.cmd_frame_ids = 0;
  	}
  
  	nla_memcpy(key.key, attrs[NL802154_KEY_ATTR_BYTES], NL802154_KEY_SIZE);
  
  	if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
  		return -ENOBUFS;
  
  	return rdev_add_llsec_key(rdev, wpan_dev, &id, &key);
  }
  
  static int nl802154_del_llsec_key(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
  	struct ieee802154_llsec_key_id id;
  
  	if (nla_parse_nested(attrs, NL802154_KEY_ATTR_MAX,
  			     info->attrs[NL802154_ATTR_SEC_KEY],
fe52145f9   Johannes Berg   netlink: pass ext...
1561
  			     nl802154_key_policy, info->extack))
a26c5fd76   Alexander Aring   nl802154: add sup...
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
  		return -EINVAL;
  
  	if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
  		return -ENOBUFS;
  
  	return rdev_del_llsec_key(rdev, wpan_dev, &id);
  }
  
  static int nl802154_send_device(struct sk_buff *msg, u32 cmd, u32 portid,
  				u32 seq, int flags,
  				struct cfg802154_registered_device *rdev,
  				struct net_device *dev,
  				const struct ieee802154_llsec_device *dev_desc)
  {
  	void *hdr;
  	struct nlattr *nl_device;
  
  	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
  	if (!hdr)
  		return -1;
  
  	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
  		goto nla_put_failure;
  
  	nl_device = nla_nest_start(msg, NL802154_ATTR_SEC_DEVICE);
  	if (!nl_device)
  		goto nla_put_failure;
  
  	if (nla_put_u32(msg, NL802154_DEV_ATTR_FRAME_COUNTER,
  			dev_desc->frame_counter) ||
  	    nla_put_le16(msg, NL802154_DEV_ATTR_PAN_ID, dev_desc->pan_id) ||
  	    nla_put_le16(msg, NL802154_DEV_ATTR_SHORT_ADDR,
  			 dev_desc->short_addr) ||
  	    nla_put_le64(msg, NL802154_DEV_ATTR_EXTENDED_ADDR,
e7479122b   Nicolas Dichtel   libnl: nla_put_le...
1596
  			 dev_desc->hwaddr, NL802154_DEV_ATTR_PAD) ||
a26c5fd76   Alexander Aring   nl802154: add sup...
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
  	    nla_put_u8(msg, NL802154_DEV_ATTR_SECLEVEL_EXEMPT,
  		       dev_desc->seclevel_exempt) ||
  	    nla_put_u32(msg, NL802154_DEV_ATTR_KEY_MODE, dev_desc->key_mode))
  		goto nla_put_failure;
  
  	nla_nest_end(msg, nl_device);
  	genlmsg_end(msg, hdr);
  
  	return 0;
  
  nla_put_failure:
  	genlmsg_cancel(msg, hdr);
  	return -EMSGSIZE;
  }
  
  static int
  nl802154_dump_llsec_dev(struct sk_buff *skb, struct netlink_callback *cb)
  {
  	struct cfg802154_registered_device *rdev = NULL;
  	struct ieee802154_llsec_device *dev;
  	struct ieee802154_llsec_table *table;
  	struct wpan_dev *wpan_dev;
  	int err;
  
  	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
  	if (err)
  		return err;
  
  	if (!wpan_dev->netdev) {
  		err = -EINVAL;
  		goto out_err;
  	}
  
  	rdev_lock_llsec_table(rdev, wpan_dev);
  	rdev_get_llsec_table(rdev, wpan_dev, &table);
  
  	/* TODO make it like station dump */
  	if (cb->args[2])
  		goto out;
  
  	list_for_each_entry(dev, &table->devices, list) {
  		if (nl802154_send_device(skb, NL802154_CMD_NEW_SEC_LEVEL,
  					 NETLINK_CB(cb->skb).portid,
  					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
  					 rdev, wpan_dev->netdev, dev) < 0) {
  			/* TODO */
  			err = -EIO;
  			rdev_unlock_llsec_table(rdev, wpan_dev);
  			goto out_err;
  		}
  	}
  
  	cb->args[2] = 1;
  
  out:
  	rdev_unlock_llsec_table(rdev, wpan_dev);
  	err = skb->len;
  out_err:
  	nl802154_finish_wpan_dev_dump(rdev);
  
  	return err;
  }
  
  static const struct nla_policy nl802154_dev_policy[NL802154_DEV_ATTR_MAX + 1] = {
  	[NL802154_DEV_ATTR_FRAME_COUNTER] = { NLA_U32 },
  	[NL802154_DEV_ATTR_PAN_ID] = { .type = NLA_U16 },
  	[NL802154_DEV_ATTR_SHORT_ADDR] = { .type = NLA_U16 },
  	[NL802154_DEV_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
  	[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] = { NLA_U8 },
  	[NL802154_DEV_ATTR_KEY_MODE] = { NLA_U32 },
  };
  
  static int
  ieee802154_llsec_parse_device(struct nlattr *nla,
  			      struct ieee802154_llsec_device *dev)
  {
  	struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
fceb6435e   Johannes Berg   netlink: pass ext...
1674
1675
  	if (!nla || nla_parse_nested(attrs, NL802154_DEV_ATTR_MAX,
  				     nla, nl802154_dev_policy, NULL))
a26c5fd76   Alexander Aring   nl802154: add sup...
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
  		return -EINVAL;
  
  	memset(dev, 0, sizeof(*dev));
  
  	if (!attrs[NL802154_DEV_ATTR_FRAME_COUNTER] ||
  	    !attrs[NL802154_DEV_ATTR_PAN_ID] ||
  	    !attrs[NL802154_DEV_ATTR_SHORT_ADDR] ||
  	    !attrs[NL802154_DEV_ATTR_EXTENDED_ADDR] ||
  	    !attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] ||
  	    !attrs[NL802154_DEV_ATTR_KEY_MODE])
  		return -EINVAL;
  
  	/* TODO be32 */
  	dev->frame_counter = nla_get_u32(attrs[NL802154_DEV_ATTR_FRAME_COUNTER]);
  	dev->pan_id = nla_get_le16(attrs[NL802154_DEV_ATTR_PAN_ID]);
  	dev->short_addr = nla_get_le16(attrs[NL802154_DEV_ATTR_SHORT_ADDR]);
  	/* TODO rename hwaddr to extended_addr */
  	dev->hwaddr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
  	dev->seclevel_exempt = nla_get_u8(attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT]);
  	dev->key_mode = nla_get_u32(attrs[NL802154_DEV_ATTR_KEY_MODE]);
  
  	if (dev->key_mode > NL802154_DEVKEY_MAX ||
  	    (dev->seclevel_exempt != 0 && dev->seclevel_exempt != 1))
  		return -EINVAL;
  
  	return 0;
  }
  
  static int nl802154_add_llsec_dev(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	struct ieee802154_llsec_device dev_desc;
  
  	if (ieee802154_llsec_parse_device(info->attrs[NL802154_ATTR_SEC_DEVICE],
  					  &dev_desc) < 0)
  		return -EINVAL;
  
  	return rdev_add_device(rdev, wpan_dev, &dev_desc);
  }
  
  static int nl802154_del_llsec_dev(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
  	__le64 extended_addr;
  
  	if (nla_parse_nested(attrs, NL802154_DEV_ATTR_MAX,
  			     info->attrs[NL802154_ATTR_SEC_DEVICE],
fe52145f9   Johannes Berg   netlink: pass ext...
1728
  			     nl802154_dev_policy, info->extack))
a26c5fd76   Alexander Aring   nl802154: add sup...
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
  		return -EINVAL;
  
  	if (!attrs[NL802154_DEV_ATTR_EXTENDED_ADDR])
  		return -EINVAL;
  
  	extended_addr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
  	return rdev_del_device(rdev, wpan_dev, extended_addr);
  }
  
  static int nl802154_send_devkey(struct sk_buff *msg, u32 cmd, u32 portid,
  				u32 seq, int flags,
  				struct cfg802154_registered_device *rdev,
  				struct net_device *dev, __le64 extended_addr,
  				const struct ieee802154_llsec_device_key *devkey)
  {
  	void *hdr;
  	struct nlattr *nl_devkey, *nl_key_id;
  
  	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
  	if (!hdr)
  		return -1;
  
  	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
  		goto nla_put_failure;
  
  	nl_devkey = nla_nest_start(msg, NL802154_ATTR_SEC_DEVKEY);
  	if (!nl_devkey)
  		goto nla_put_failure;
  
  	if (nla_put_le64(msg, NL802154_DEVKEY_ATTR_EXTENDED_ADDR,
e7479122b   Nicolas Dichtel   libnl: nla_put_le...
1759
  			 extended_addr, NL802154_DEVKEY_ATTR_PAD) ||
a26c5fd76   Alexander Aring   nl802154: add sup...
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
  	    nla_put_u32(msg, NL802154_DEVKEY_ATTR_FRAME_COUNTER,
  			devkey->frame_counter))
  		goto nla_put_failure;
  
  	nl_key_id = nla_nest_start(msg, NL802154_DEVKEY_ATTR_ID);
  	if (!nl_key_id)
  		goto nla_put_failure;
  
  	if (ieee802154_llsec_send_key_id(msg, &devkey->key_id) < 0)
  		goto nla_put_failure;
  
  	nla_nest_end(msg, nl_key_id);
  	nla_nest_end(msg, nl_devkey);
  	genlmsg_end(msg, hdr);
  
  	return 0;
  
  nla_put_failure:
  	genlmsg_cancel(msg, hdr);
  	return -EMSGSIZE;
  }
  
  static int
  nl802154_dump_llsec_devkey(struct sk_buff *skb, struct netlink_callback *cb)
  {
  	struct cfg802154_registered_device *rdev = NULL;
  	struct ieee802154_llsec_device_key *kpos;
  	struct ieee802154_llsec_device *dpos;
  	struct ieee802154_llsec_table *table;
  	struct wpan_dev *wpan_dev;
  	int err;
  
  	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
  	if (err)
  		return err;
  
  	if (!wpan_dev->netdev) {
  		err = -EINVAL;
  		goto out_err;
  	}
  
  	rdev_lock_llsec_table(rdev, wpan_dev);
  	rdev_get_llsec_table(rdev, wpan_dev, &table);
  
  	/* TODO make it like station dump */
  	if (cb->args[2])
  		goto out;
  
  	/* TODO look if remove devkey and do some nested attribute */
  	list_for_each_entry(dpos, &table->devices, list) {
  		list_for_each_entry(kpos, &dpos->keys, list) {
  			if (nl802154_send_devkey(skb,
  						 NL802154_CMD_NEW_SEC_LEVEL,
  						 NETLINK_CB(cb->skb).portid,
  						 cb->nlh->nlmsg_seq,
  						 NLM_F_MULTI, rdev,
  						 wpan_dev->netdev,
  						 dpos->hwaddr,
  						 kpos) < 0) {
  				/* TODO */
  				err = -EIO;
  				rdev_unlock_llsec_table(rdev, wpan_dev);
  				goto out_err;
  			}
  		}
  	}
  
  	cb->args[2] = 1;
  
  out:
  	rdev_unlock_llsec_table(rdev, wpan_dev);
  	err = skb->len;
  out_err:
  	nl802154_finish_wpan_dev_dump(rdev);
  
  	return err;
  }
  
  static const struct nla_policy nl802154_devkey_policy[NL802154_DEVKEY_ATTR_MAX + 1] = {
  	[NL802154_DEVKEY_ATTR_FRAME_COUNTER] = { NLA_U32 },
  	[NL802154_DEVKEY_ATTR_EXTENDED_ADDR] = { NLA_U64 },
  	[NL802154_DEVKEY_ATTR_ID] = { NLA_NESTED },
  };
  
  static int nl802154_add_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
  	struct ieee802154_llsec_device_key key;
  	__le64 extended_addr;
  
  	if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
  	    nla_parse_nested(attrs, NL802154_DEVKEY_ATTR_MAX,
  			     info->attrs[NL802154_ATTR_SEC_DEVKEY],
fe52145f9   Johannes Berg   netlink: pass ext...
1856
  			     nl802154_devkey_policy, info->extack) < 0)
a26c5fd76   Alexander Aring   nl802154: add sup...
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
  		return -EINVAL;
  
  	if (!attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER] ||
  	    !attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
  		return -EINVAL;
  
  	/* TODO change key.id ? */
  	if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
  					  &key.key_id) < 0)
  		return -ENOBUFS;
  
  	/* TODO be32 */
  	key.frame_counter = nla_get_u32(attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER]);
  	/* TODO change naming hwaddr -> extended_addr
  	 * check unique identifier short+pan OR extended_addr
  	 */
  	extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
  	return rdev_add_devkey(rdev, wpan_dev, extended_addr, &key);
  }
  
  static int nl802154_del_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
  	struct ieee802154_llsec_device_key key;
  	__le64 extended_addr;
  
  	if (nla_parse_nested(attrs, NL802154_DEVKEY_ATTR_MAX,
  			     info->attrs[NL802154_ATTR_SEC_DEVKEY],
fe52145f9   Johannes Berg   netlink: pass ext...
1888
  			     nl802154_devkey_policy, info->extack))
a26c5fd76   Alexander Aring   nl802154: add sup...
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
  		return -EINVAL;
  
  	if (!attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
  		return -EINVAL;
  
  	/* TODO change key.id ? */
  	if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
  					  &key.key_id) < 0)
  		return -ENOBUFS;
  
  	/* TODO change naming hwaddr -> extended_addr
  	 * check unique identifier short+pan OR extended_addr
  	 */
  	extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
  	return rdev_del_devkey(rdev, wpan_dev, extended_addr, &key);
  }
  
  static int nl802154_send_seclevel(struct sk_buff *msg, u32 cmd, u32 portid,
  				  u32 seq, int flags,
  				  struct cfg802154_registered_device *rdev,
  				  struct net_device *dev,
  				  const struct ieee802154_llsec_seclevel *sl)
  {
  	void *hdr;
  	struct nlattr *nl_seclevel;
  
  	hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
  	if (!hdr)
  		return -1;
  
  	if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
  		goto nla_put_failure;
  
  	nl_seclevel = nla_nest_start(msg, NL802154_ATTR_SEC_LEVEL);
  	if (!nl_seclevel)
  		goto nla_put_failure;
  
  	if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_FRAME, sl->frame_type) ||
  	    nla_put_u32(msg, NL802154_SECLEVEL_ATTR_LEVELS, sl->sec_levels) ||
  	    nla_put_u8(msg, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE,
  		       sl->device_override))
  		goto nla_put_failure;
  
  	if (sl->frame_type == NL802154_FRAME_CMD) {
  		if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_CMD_FRAME,
  				sl->cmd_frame_id))
  			goto nla_put_failure;
  	}
  
  	nla_nest_end(msg, nl_seclevel);
  	genlmsg_end(msg, hdr);
  
  	return 0;
  
  nla_put_failure:
  	genlmsg_cancel(msg, hdr);
  	return -EMSGSIZE;
  }
  
  static int
  nl802154_dump_llsec_seclevel(struct sk_buff *skb, struct netlink_callback *cb)
  {
  	struct cfg802154_registered_device *rdev = NULL;
  	struct ieee802154_llsec_seclevel *sl;
  	struct ieee802154_llsec_table *table;
  	struct wpan_dev *wpan_dev;
  	int err;
  
  	err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
  	if (err)
  		return err;
  
  	if (!wpan_dev->netdev) {
  		err = -EINVAL;
  		goto out_err;
  	}
  
  	rdev_lock_llsec_table(rdev, wpan_dev);
  	rdev_get_llsec_table(rdev, wpan_dev, &table);
  
  	/* TODO make it like station dump */
  	if (cb->args[2])
  		goto out;
  
  	list_for_each_entry(sl, &table->security_levels, list) {
  		if (nl802154_send_seclevel(skb, NL802154_CMD_NEW_SEC_LEVEL,
  					   NETLINK_CB(cb->skb).portid,
  					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
  					   rdev, wpan_dev->netdev, sl) < 0) {
  			/* TODO */
  			err = -EIO;
  			rdev_unlock_llsec_table(rdev, wpan_dev);
  			goto out_err;
  		}
  	}
  
  	cb->args[2] = 1;
  
  out:
  	rdev_unlock_llsec_table(rdev, wpan_dev);
  	err = skb->len;
  out_err:
  	nl802154_finish_wpan_dev_dump(rdev);
  
  	return err;
  }
  
  static const struct nla_policy nl802154_seclevel_policy[NL802154_SECLEVEL_ATTR_MAX + 1] = {
  	[NL802154_SECLEVEL_ATTR_LEVELS] = { .type = NLA_U8 },
  	[NL802154_SECLEVEL_ATTR_FRAME] = { .type = NLA_U32 },
  	[NL802154_SECLEVEL_ATTR_CMD_FRAME] = { .type = NLA_U32 },
  	[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE] = { .type = NLA_U8 },
  };
  
  static int
  llsec_parse_seclevel(struct nlattr *nla, struct ieee802154_llsec_seclevel *sl)
  {
  	struct nlattr *attrs[NL802154_SECLEVEL_ATTR_MAX + 1];
fceb6435e   Johannes Berg   netlink: pass ext...
2007
2008
  	if (!nla || nla_parse_nested(attrs, NL802154_SECLEVEL_ATTR_MAX,
  				     nla, nl802154_seclevel_policy, NULL))
a26c5fd76   Alexander Aring   nl802154: add sup...
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
  		return -EINVAL;
  
  	memset(sl, 0, sizeof(*sl));
  
  	if (!attrs[NL802154_SECLEVEL_ATTR_LEVELS] ||
  	    !attrs[NL802154_SECLEVEL_ATTR_FRAME] ||
  	    !attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE])
  		return -EINVAL;
  
  	sl->sec_levels = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_LEVELS]);
  	sl->frame_type = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_FRAME]);
  	sl->device_override = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE]);
  	if (sl->frame_type > NL802154_FRAME_MAX ||
  	    (sl->device_override != 0 && sl->device_override != 1))
  		return -EINVAL;
  
  	if (sl->frame_type == NL802154_FRAME_CMD) {
  		if (!attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME])
  			return -EINVAL;
  
  		sl->cmd_frame_id = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME]);
  		if (sl->cmd_frame_id > NL802154_CMD_FRAME_MAX)
  			return -EINVAL;
  	}
  
  	return 0;
  }
  
  static int nl802154_add_llsec_seclevel(struct sk_buff *skb,
  				       struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	struct ieee802154_llsec_seclevel sl;
  
  	if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
  				 &sl) < 0)
  		return -EINVAL;
  
  	return rdev_add_seclevel(rdev, wpan_dev, &sl);
  }
  
  static int nl802154_del_llsec_seclevel(struct sk_buff *skb,
  				       struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev = info->user_ptr[0];
  	struct net_device *dev = info->user_ptr[1];
  	struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  	struct ieee802154_llsec_seclevel sl;
  
  	if (!info->attrs[NL802154_ATTR_SEC_LEVEL] ||
  	    llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
  				 &sl) < 0)
  		return -EINVAL;
  
  	return rdev_del_seclevel(rdev, wpan_dev, &sl);
  }
  #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  
  #define NL802154_FLAG_NEED_WPAN_PHY	0x01
  #define NL802154_FLAG_NEED_NETDEV	0x02
  #define NL802154_FLAG_NEED_RTNL		0x04
  #define NL802154_FLAG_CHECK_NETDEV_UP	0x08
  #define NL802154_FLAG_NEED_NETDEV_UP	(NL802154_FLAG_NEED_NETDEV |\
  					 NL802154_FLAG_CHECK_NETDEV_UP)
  #define NL802154_FLAG_NEED_WPAN_DEV	0x10
  #define NL802154_FLAG_NEED_WPAN_DEV_UP	(NL802154_FLAG_NEED_WPAN_DEV |\
  					 NL802154_FLAG_CHECK_NETDEV_UP)
  
  static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
  			     struct genl_info *info)
  {
  	struct cfg802154_registered_device *rdev;
  	struct wpan_dev *wpan_dev;
  	struct net_device *dev;
  	bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
  
  	if (rtnl)
  		rtnl_lock();
  
  	if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
  		rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
  		if (IS_ERR(rdev)) {
  			if (rtnl)
  				rtnl_unlock();
  			return PTR_ERR(rdev);
  		}
  		info->user_ptr[0] = rdev;
  	} else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
  		   ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
  		ASSERT_RTNL();
  		wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
  							   info->attrs);
  		if (IS_ERR(wpan_dev)) {
  			if (rtnl)
  				rtnl_unlock();
  			return PTR_ERR(wpan_dev);
  		}
  
  		dev = wpan_dev->netdev;
  		rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
  
  		if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
  			if (!dev) {
  				if (rtnl)
  					rtnl_unlock();
  				return -EINVAL;
  			}
  
  			info->user_ptr[1] = dev;
  		} else {
  			info->user_ptr[1] = wpan_dev;
  		}
  
  		if (dev) {
  			if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
  			    !netif_running(dev)) {
  				if (rtnl)
  					rtnl_unlock();
  				return -ENETDOWN;
  			}
  
  			dev_hold(dev);
  		}
  
  		info->user_ptr[0] = rdev;
  	}
  
  	return 0;
  }
  
  static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
  			       struct genl_info *info)
  {
  	if (info->user_ptr[1]) {
  		if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
  			struct wpan_dev *wpan_dev = info->user_ptr[1];
  
  			if (wpan_dev->netdev)
  				dev_put(wpan_dev->netdev);
  		} else {
  			dev_put(info->user_ptr[1]);
  		}
  	}
  
  	if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
  		rtnl_unlock();
  }
  
  static const struct genl_ops nl802154_ops[] = {
  	{
  		.cmd = NL802154_CMD_GET_WPAN_PHY,
  		.doit = nl802154_get_wpan_phy,
  		.dumpit = nl802154_dump_wpan_phy,
  		.done = nl802154_dump_wpan_phy_done,
  		.policy = nl802154_policy,
  		/* can be retrieved by unprivileged users */
  		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_GET_INTERFACE,
  		.doit = nl802154_get_interface,
  		.dumpit = nl802154_dump_interface,
  		.policy = nl802154_policy,
  		/* can be retrieved by unprivileged users */
  		.internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_NEW_INTERFACE,
  		.doit = nl802154_new_interface,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
f3ea5e442   Alexander Aring   ieee802154: add n...
2185
2186
2187
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
b821ecd4c   Alexander Aring   ieee802154: add d...
2188
2189
2190
2191
2192
2193
2194
2195
  		.cmd = NL802154_CMD_DEL_INTERFACE,
  		.doit = nl802154_del_interface,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
ab0bd5617   Alexander Aring   ieee820154: add c...
2196
2197
2198
2199
2200
2201
2202
  		.cmd = NL802154_CMD_SET_CHANNEL,
  		.doit = nl802154_set_channel,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  				  NL802154_FLAG_NEED_RTNL,
  	},
702bf3712   Alexander Aring   ieee820154: add p...
2203
  	{
ba2a9506a   Alexander Aring   nl802154: introdu...
2204
2205
2206
2207
2208
2209
2210
2211
  		.cmd = NL802154_CMD_SET_CCA_MODE,
  		.doit = nl802154_set_cca_mode,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
b69644c1c   Alexander Aring   nl802154: add sup...
2212
2213
2214
2215
2216
2217
2218
2219
  		.cmd = NL802154_CMD_SET_CCA_ED_LEVEL,
  		.doit = nl802154_set_cca_ed_level,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
0f999b09f   Varka Bhadram   ieee802154: add s...
2220
2221
2222
2223
  		.cmd = NL802154_CMD_SET_TX_POWER,
  		.doit = nl802154_set_tx_power,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
66e5c2672   Alexander Aring   ieee802154: add n...
2224
2225
2226
2227
2228
2229
2230
2231
  		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_SET_WPAN_PHY_NETNS,
  		.doit = nl802154_wpan_phy_netns,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
0f999b09f   Varka Bhadram   ieee802154: add s...
2232
2233
2234
2235
  		.internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
702bf3712   Alexander Aring   ieee820154: add p...
2236
2237
2238
2239
2240
2241
2242
  		.cmd = NL802154_CMD_SET_PAN_ID,
  		.doit = nl802154_set_pan_id,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
9830c62a0   Alexander Aring   ieee820154: add s...
2243
2244
2245
2246
2247
2248
2249
2250
  	{
  		.cmd = NL802154_CMD_SET_SHORT_ADDR,
  		.doit = nl802154_set_short_addr,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
656a999e8   Alexander Aring   ieee820154: add b...
2251
2252
2253
2254
2255
2256
2257
2258
  	{
  		.cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
  		.doit = nl802154_set_backoff_exponent,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
a01ba7652   Alexander Aring   ieee820154: add m...
2259
2260
2261
2262
2263
2264
2265
2266
  	{
  		.cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
  		.doit = nl802154_set_max_csma_backoffs,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
17a3a46bf   Alexander Aring   ieee820154: add m...
2267
2268
2269
2270
2271
2272
2273
2274
  	{
  		.cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
  		.doit = nl802154_set_max_frame_retries,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
c8937a1d1   Alexander Aring   ieee820154: add l...
2275
2276
2277
2278
2279
2280
2281
2282
  	{
  		.cmd = NL802154_CMD_SET_LBT_MODE,
  		.doit = nl802154_set_lbt_mode,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
c91208d81   Alexander Aring   ieee802154: add a...
2283
2284
2285
2286
2287
2288
2289
2290
  	{
  		.cmd = NL802154_CMD_SET_ACKREQ_DEFAULT,
  		.doit = nl802154_set_ackreq_default,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
a26c5fd76   Alexander Aring   nl802154: add sup...
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
  #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  	{
  		.cmd = NL802154_CMD_SET_SEC_PARAMS,
  		.doit = nl802154_set_llsec_params,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_GET_SEC_KEY,
  		/* TODO .doit by matching key id? */
  		.dumpit = nl802154_dump_llsec_key,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_NEW_SEC_KEY,
  		.doit = nl802154_add_llsec_key,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_DEL_SEC_KEY,
  		.doit = nl802154_del_llsec_key,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	/* TODO unique identifier must short+pan OR extended_addr */
  	{
  		.cmd = NL802154_CMD_GET_SEC_DEV,
  		/* TODO .doit by matching extended_addr? */
  		.dumpit = nl802154_dump_llsec_dev,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_NEW_SEC_DEV,
  		.doit = nl802154_add_llsec_dev,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_DEL_SEC_DEV,
  		.doit = nl802154_del_llsec_dev,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	/* TODO remove complete devkey, put it as nested? */
  	{
  		.cmd = NL802154_CMD_GET_SEC_DEVKEY,
  		/* TODO doit by matching ??? */
  		.dumpit = nl802154_dump_llsec_devkey,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_NEW_SEC_DEVKEY,
  		.doit = nl802154_add_llsec_devkey,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_DEL_SEC_DEVKEY,
  		.doit = nl802154_del_llsec_devkey,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_GET_SEC_LEVEL,
  		/* TODO .doit by matching frame_type? */
  		.dumpit = nl802154_dump_llsec_seclevel,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_NEW_SEC_LEVEL,
  		.doit = nl802154_add_llsec_seclevel,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  	{
  		.cmd = NL802154_CMD_DEL_SEC_LEVEL,
  		/* TODO match frame_type only? */
  		.doit = nl802154_del_llsec_seclevel,
  		.policy = nl802154_policy,
  		.flags = GENL_ADMIN_PERM,
  		.internal_flags = NL802154_FLAG_NEED_NETDEV |
  				  NL802154_FLAG_NEED_RTNL,
  	},
  #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
79fe1a2aa   Alexander Aring   ieee802154: add n...
2404
  };
56989f6d8   Johannes Berg   genetlink: mark f...
2405
  static struct genl_family nl802154_fam __ro_after_init = {
489111e5c   Johannes Berg   genetlink: static...
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
  	.name = NL802154_GENL_NAME,	/* have users key off the name instead */
  	.hdrsize = 0,			/* no private header */
  	.version = 1,			/* no particular meaning now */
  	.maxattr = NL802154_ATTR_MAX,
  	.netnsok = true,
  	.pre_doit = nl802154_pre_doit,
  	.post_doit = nl802154_post_doit,
  	.module = THIS_MODULE,
  	.ops = nl802154_ops,
  	.n_ops = ARRAY_SIZE(nl802154_ops),
  	.mcgrps = nl802154_mcgrps,
  	.n_mcgrps = ARRAY_SIZE(nl802154_mcgrps),
  };
79fe1a2aa   Alexander Aring   ieee802154: add n...
2419
  /* initialisation/exit functions */
56989f6d8   Johannes Berg   genetlink: mark f...
2420
  int __init nl802154_init(void)
79fe1a2aa   Alexander Aring   ieee802154: add n...
2421
  {
489111e5c   Johannes Berg   genetlink: static...
2422
  	return genl_register_family(&nl802154_fam);
79fe1a2aa   Alexander Aring   ieee802154: add n...
2423
2424
2425
2426
2427
2428
  }
  
  void nl802154_exit(void)
  {
  	genl_unregister_family(&nl802154_fam);
  }