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