Commit | Line | Data |
---|---|---|
79fe1a2a AA |
1 | /* This program is free software; you can redistribute it and/or modify |
2 | * it under the terms of the GNU General Public License version 2 | |
3 | * as published by the Free Software Foundation. | |
4 | * | |
5 | * This program is distributed in the hope that it will be useful, | |
6 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
7 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
8 | * GNU General Public License for more details. | |
9 | * | |
10 | * Authors: | |
11 | * Alexander Aring <aar@pengutronix.de> | |
12 | * | |
13 | * Based on: net/wireless/nl80211.c | |
14 | */ | |
15 | ||
16 | #include <linux/rtnetlink.h> | |
17 | ||
18 | #include <net/cfg802154.h> | |
19 | #include <net/genetlink.h> | |
20 | #include <net/mac802154.h> | |
21 | #include <net/netlink.h> | |
22 | #include <net/nl802154.h> | |
23 | #include <net/sock.h> | |
24 | ||
25 | #include "nl802154.h" | |
ab0bd561 | 26 | #include "rdev-ops.h" |
79fe1a2a AA |
27 | #include "core.h" |
28 | ||
29 | static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb, | |
30 | struct genl_info *info); | |
31 | ||
32 | static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb, | |
33 | struct genl_info *info); | |
34 | ||
35 | /* the netlink family */ | |
36 | static struct genl_family nl802154_fam = { | |
37 | .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */ | |
38 | .name = NL802154_GENL_NAME, /* have users key off the name instead */ | |
39 | .hdrsize = 0, /* no private header */ | |
40 | .version = 1, /* no particular meaning now */ | |
41 | .maxattr = NL802154_ATTR_MAX, | |
42 | .netnsok = true, | |
43 | .pre_doit = nl802154_pre_doit, | |
44 | .post_doit = nl802154_post_doit, | |
45 | }; | |
46 | ||
47 | /* multicast groups */ | |
48 | enum nl802154_multicast_groups { | |
49 | NL802154_MCGRP_CONFIG, | |
50 | }; | |
51 | ||
52 | static const struct genl_multicast_group nl802154_mcgrps[] = { | |
53 | [NL802154_MCGRP_CONFIG] = { .name = "config", }, | |
54 | }; | |
55 | ||
56 | /* returns ERR_PTR values */ | |
57 | static struct wpan_dev * | |
58 | __cfg802154_wpan_dev_from_attrs(struct net *netns, struct nlattr **attrs) | |
59 | { | |
60 | struct cfg802154_registered_device *rdev; | |
61 | struct wpan_dev *result = NULL; | |
62 | bool have_ifidx = attrs[NL802154_ATTR_IFINDEX]; | |
63 | bool have_wpan_dev_id = attrs[NL802154_ATTR_WPAN_DEV]; | |
64 | u64 wpan_dev_id; | |
65 | int wpan_phy_idx = -1; | |
66 | int ifidx = -1; | |
67 | ||
68 | ASSERT_RTNL(); | |
69 | ||
70 | if (!have_ifidx && !have_wpan_dev_id) | |
71 | return ERR_PTR(-EINVAL); | |
72 | ||
73 | if (have_ifidx) | |
74 | ifidx = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]); | |
75 | if (have_wpan_dev_id) { | |
76 | wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]); | |
77 | wpan_phy_idx = wpan_dev_id >> 32; | |
78 | } | |
79 | ||
80 | list_for_each_entry(rdev, &cfg802154_rdev_list, list) { | |
81 | struct wpan_dev *wpan_dev; | |
82 | ||
83 | /* TODO netns compare */ | |
84 | ||
85 | if (have_wpan_dev_id && rdev->wpan_phy_idx != wpan_phy_idx) | |
86 | continue; | |
87 | ||
88 | list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) { | |
89 | if (have_ifidx && wpan_dev->netdev && | |
90 | wpan_dev->netdev->ifindex == ifidx) { | |
91 | result = wpan_dev; | |
92 | break; | |
93 | } | |
94 | if (have_wpan_dev_id && | |
95 | wpan_dev->identifier == (u32)wpan_dev_id) { | |
96 | result = wpan_dev; | |
97 | break; | |
98 | } | |
99 | } | |
100 | ||
101 | if (result) | |
102 | break; | |
103 | } | |
104 | ||
105 | if (result) | |
106 | return result; | |
107 | ||
108 | return ERR_PTR(-ENODEV); | |
109 | } | |
110 | ||
111 | static struct cfg802154_registered_device * | |
112 | __cfg802154_rdev_from_attrs(struct net *netns, struct nlattr **attrs) | |
113 | { | |
114 | struct cfg802154_registered_device *rdev = NULL, *tmp; | |
115 | struct net_device *netdev; | |
116 | ||
117 | ASSERT_RTNL(); | |
118 | ||
119 | if (!attrs[NL802154_ATTR_WPAN_PHY] && | |
120 | !attrs[NL802154_ATTR_IFINDEX] && | |
121 | !attrs[NL802154_ATTR_WPAN_DEV]) | |
122 | return ERR_PTR(-EINVAL); | |
123 | ||
124 | if (attrs[NL802154_ATTR_WPAN_PHY]) | |
125 | rdev = cfg802154_rdev_by_wpan_phy_idx( | |
126 | nla_get_u32(attrs[NL802154_ATTR_WPAN_PHY])); | |
127 | ||
128 | if (attrs[NL802154_ATTR_WPAN_DEV]) { | |
129 | u64 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]); | |
130 | struct wpan_dev *wpan_dev; | |
131 | bool found = false; | |
132 | ||
133 | tmp = cfg802154_rdev_by_wpan_phy_idx(wpan_dev_id >> 32); | |
134 | if (tmp) { | |
135 | /* make sure wpan_dev exists */ | |
136 | list_for_each_entry(wpan_dev, &tmp->wpan_dev_list, list) { | |
137 | if (wpan_dev->identifier != (u32)wpan_dev_id) | |
138 | continue; | |
139 | found = true; | |
140 | break; | |
141 | } | |
142 | ||
143 | if (!found) | |
144 | tmp = NULL; | |
145 | ||
146 | if (rdev && tmp != rdev) | |
147 | return ERR_PTR(-EINVAL); | |
148 | rdev = tmp; | |
149 | } | |
150 | } | |
151 | ||
152 | if (attrs[NL802154_ATTR_IFINDEX]) { | |
153 | int ifindex = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]); | |
154 | ||
155 | netdev = __dev_get_by_index(netns, ifindex); | |
156 | if (netdev) { | |
157 | if (netdev->ieee802154_ptr) | |
158 | tmp = wpan_phy_to_rdev( | |
159 | netdev->ieee802154_ptr->wpan_phy); | |
160 | else | |
161 | tmp = NULL; | |
162 | ||
163 | /* not wireless device -- return error */ | |
164 | if (!tmp) | |
165 | return ERR_PTR(-EINVAL); | |
166 | ||
167 | /* mismatch -- return error */ | |
168 | if (rdev && tmp != rdev) | |
169 | return ERR_PTR(-EINVAL); | |
170 | ||
171 | rdev = tmp; | |
172 | } | |
173 | } | |
174 | ||
175 | if (!rdev) | |
176 | return ERR_PTR(-ENODEV); | |
177 | ||
178 | /* TODO netns compare */ | |
179 | ||
180 | return rdev; | |
181 | } | |
182 | ||
183 | /* This function returns a pointer to the driver | |
184 | * that the genl_info item that is passed refers to. | |
185 | * | |
186 | * The result of this can be a PTR_ERR and hence must | |
187 | * be checked with IS_ERR() for errors. | |
188 | */ | |
189 | static struct cfg802154_registered_device * | |
190 | cfg802154_get_dev_from_info(struct net *netns, struct genl_info *info) | |
191 | { | |
192 | return __cfg802154_rdev_from_attrs(netns, info->attrs); | |
193 | } | |
194 | ||
195 | /* policy for the attributes */ | |
196 | static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = { | |
ca20ce20 AA |
197 | [NL802154_ATTR_WPAN_PHY] = { .type = NLA_U32 }, |
198 | [NL802154_ATTR_WPAN_PHY_NAME] = { .type = NLA_NUL_STRING, | |
199 | .len = 20-1 }, | |
200 | ||
201 | [NL802154_ATTR_IFINDEX] = { .type = NLA_U32 }, | |
4b96aea0 AA |
202 | [NL802154_ATTR_IFTYPE] = { .type = NLA_U32 }, |
203 | [NL802154_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 }, | |
ca20ce20 AA |
204 | |
205 | [NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 }, | |
206 | ||
207 | [NL802154_ATTR_PAGE] = { .type = NLA_U8, }, | |
208 | [NL802154_ATTR_CHANNEL] = { .type = NLA_U8, }, | |
209 | ||
1a19cb68 | 210 | [NL802154_ATTR_TX_POWER] = { .type = NLA_S32, }, |
ca20ce20 | 211 | |
ba2a9506 AA |
212 | [NL802154_ATTR_CCA_MODE] = { .type = NLA_U32, }, |
213 | [NL802154_ATTR_CCA_OPT] = { .type = NLA_U32, }, | |
ca20ce20 AA |
214 | |
215 | [NL802154_ATTR_SUPPORTED_CHANNEL] = { .type = NLA_U32, }, | |
4b96aea0 AA |
216 | |
217 | [NL802154_ATTR_PAN_ID] = { .type = NLA_U16, }, | |
218 | [NL802154_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 }, | |
219 | [NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16, }, | |
220 | ||
221 | [NL802154_ATTR_MIN_BE] = { .type = NLA_U8, }, | |
222 | [NL802154_ATTR_MAX_BE] = { .type = NLA_U8, }, | |
223 | [NL802154_ATTR_MAX_CSMA_BACKOFFS] = { .type = NLA_U8, }, | |
224 | ||
225 | [NL802154_ATTR_MAX_FRAME_RETRIES] = { .type = NLA_S8, }, | |
226 | ||
227 | [NL802154_ATTR_LBT_MODE] = { .type = NLA_U8, }, | |
0e665457 AA |
228 | |
229 | [NL802154_ATTR_WPAN_PHY_CAPS] = { .type = NLA_NESTED }, | |
79fe1a2a AA |
230 | }; |
231 | ||
232 | /* message building helper */ | |
233 | static inline void *nl802154hdr_put(struct sk_buff *skb, u32 portid, u32 seq, | |
234 | int flags, u8 cmd) | |
235 | { | |
236 | /* since there is no private header just add the generic one */ | |
237 | return genlmsg_put(skb, portid, seq, &nl802154_fam, flags, cmd); | |
238 | } | |
239 | ||
0e665457 AA |
240 | static int |
241 | nl802154_put_flags(struct sk_buff *msg, int attr, u32 mask) | |
242 | { | |
243 | struct nlattr *nl_flags = nla_nest_start(msg, attr); | |
244 | int i; | |
245 | ||
246 | if (!nl_flags) | |
247 | return -ENOBUFS; | |
248 | ||
249 | i = 0; | |
250 | while (mask) { | |
251 | if ((mask & 1) && nla_put_flag(msg, i)) | |
252 | return -ENOBUFS; | |
253 | ||
254 | mask >>= 1; | |
255 | i++; | |
256 | } | |
257 | ||
258 | nla_nest_end(msg, nl_flags); | |
259 | return 0; | |
260 | } | |
261 | ||
ca20ce20 AA |
262 | static int |
263 | nl802154_send_wpan_phy_channels(struct cfg802154_registered_device *rdev, | |
264 | struct sk_buff *msg) | |
265 | { | |
266 | struct nlattr *nl_page; | |
267 | unsigned long page; | |
268 | ||
269 | nl_page = nla_nest_start(msg, NL802154_ATTR_CHANNELS_SUPPORTED); | |
270 | if (!nl_page) | |
271 | return -ENOBUFS; | |
272 | ||
cb41c8dd | 273 | for (page = 0; page <= IEEE802154_MAX_PAGE; page++) { |
ca20ce20 | 274 | if (nla_put_u32(msg, NL802154_ATTR_SUPPORTED_CHANNEL, |
72f655e4 | 275 | rdev->wpan_phy.supported.channels[page])) |
ca20ce20 AA |
276 | return -ENOBUFS; |
277 | } | |
278 | nla_nest_end(msg, nl_page); | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
0e665457 AA |
283 | static int |
284 | nl802154_put_capabilities(struct sk_buff *msg, | |
285 | struct cfg802154_registered_device *rdev) | |
286 | { | |
287 | const struct wpan_phy_supported *caps = &rdev->wpan_phy.supported; | |
288 | struct nlattr *nl_caps, *nl_channels; | |
289 | int i; | |
290 | ||
291 | nl_caps = nla_nest_start(msg, NL802154_ATTR_WPAN_PHY_CAPS); | |
292 | if (!nl_caps) | |
293 | return -ENOBUFS; | |
294 | ||
295 | nl_channels = nla_nest_start(msg, NL802154_CAP_ATTR_CHANNELS); | |
296 | if (!nl_channels) | |
297 | return -ENOBUFS; | |
298 | ||
299 | for (i = 0; i <= IEEE802154_MAX_PAGE; i++) { | |
300 | if (caps->channels[i]) { | |
301 | if (nl802154_put_flags(msg, i, caps->channels[i])) | |
302 | return -ENOBUFS; | |
303 | } | |
304 | } | |
305 | ||
306 | nla_nest_end(msg, nl_channels); | |
307 | ||
308 | if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) { | |
309 | struct nlattr *nl_ed_lvls; | |
310 | ||
311 | nl_ed_lvls = nla_nest_start(msg, | |
312 | NL802154_CAP_ATTR_CCA_ED_LEVELS); | |
313 | if (!nl_ed_lvls) | |
314 | return -ENOBUFS; | |
315 | ||
316 | for (i = 0; i < caps->cca_ed_levels_size; i++) { | |
317 | if (nla_put_s32(msg, i, caps->cca_ed_levels[i])) | |
318 | return -ENOBUFS; | |
319 | } | |
320 | ||
321 | nla_nest_end(msg, nl_ed_lvls); | |
322 | } | |
323 | ||
324 | if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) { | |
325 | struct nlattr *nl_tx_pwrs; | |
326 | ||
327 | nl_tx_pwrs = nla_nest_start(msg, NL802154_CAP_ATTR_TX_POWERS); | |
328 | if (!nl_tx_pwrs) | |
329 | return -ENOBUFS; | |
330 | ||
331 | for (i = 0; i < caps->tx_powers_size; i++) { | |
332 | if (nla_put_s32(msg, i, caps->tx_powers[i])) | |
333 | return -ENOBUFS; | |
334 | } | |
335 | ||
336 | nla_nest_end(msg, nl_tx_pwrs); | |
337 | } | |
338 | ||
339 | if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) { | |
340 | if (nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_MODES, | |
341 | caps->cca_modes) || | |
342 | nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_OPTS, | |
343 | caps->cca_opts)) | |
344 | return -ENOBUFS; | |
345 | } | |
346 | ||
347 | if (nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MINBE, caps->min_minbe) || | |
348 | nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MINBE, caps->max_minbe) || | |
349 | nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MAXBE, caps->min_maxbe) || | |
350 | nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MAXBE, caps->max_maxbe) || | |
351 | nla_put_u8(msg, NL802154_CAP_ATTR_MIN_CSMA_BACKOFFS, | |
352 | caps->min_csma_backoffs) || | |
353 | nla_put_u8(msg, NL802154_CAP_ATTR_MAX_CSMA_BACKOFFS, | |
354 | caps->max_csma_backoffs) || | |
355 | nla_put_s8(msg, NL802154_CAP_ATTR_MIN_FRAME_RETRIES, | |
356 | caps->min_frame_retries) || | |
357 | nla_put_s8(msg, NL802154_CAP_ATTR_MAX_FRAME_RETRIES, | |
358 | caps->max_frame_retries) || | |
359 | nl802154_put_flags(msg, NL802154_CAP_ATTR_IFTYPES, | |
360 | caps->iftypes) || | |
361 | nla_put_u32(msg, NL802154_CAP_ATTR_LBT, caps->lbt)) | |
362 | return -ENOBUFS; | |
363 | ||
364 | nla_nest_end(msg, nl_caps); | |
365 | ||
366 | return 0; | |
367 | } | |
368 | ||
ca20ce20 AA |
369 | static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev, |
370 | enum nl802154_commands cmd, | |
371 | struct sk_buff *msg, u32 portid, u32 seq, | |
372 | int flags) | |
373 | { | |
374 | void *hdr; | |
375 | ||
376 | hdr = nl802154hdr_put(msg, portid, seq, flags, cmd); | |
377 | if (!hdr) | |
378 | return -ENOBUFS; | |
379 | ||
380 | if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) || | |
381 | nla_put_string(msg, NL802154_ATTR_WPAN_PHY_NAME, | |
382 | wpan_phy_name(&rdev->wpan_phy)) || | |
383 | nla_put_u32(msg, NL802154_ATTR_GENERATION, | |
384 | cfg802154_rdev_list_generation)) | |
385 | goto nla_put_failure; | |
386 | ||
387 | if (cmd != NL802154_CMD_NEW_WPAN_PHY) | |
388 | goto finish; | |
389 | ||
390 | /* DUMP PHY PIB */ | |
391 | ||
392 | /* current channel settings */ | |
393 | if (nla_put_u8(msg, NL802154_ATTR_PAGE, | |
394 | rdev->wpan_phy.current_page) || | |
395 | nla_put_u8(msg, NL802154_ATTR_CHANNEL, | |
396 | rdev->wpan_phy.current_channel)) | |
397 | goto nla_put_failure; | |
398 | ||
0e665457 AA |
399 | /* TODO remove this behaviour, we still keep support it for a while |
400 | * so users can change the behaviour to the new one. | |
401 | */ | |
ca20ce20 AA |
402 | if (nl802154_send_wpan_phy_channels(rdev, msg)) |
403 | goto nla_put_failure; | |
404 | ||
405 | /* cca mode */ | |
edea8f7c AA |
406 | if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) { |
407 | if (nla_put_u32(msg, NL802154_ATTR_CCA_MODE, | |
408 | rdev->wpan_phy.cca.mode)) | |
ba2a9506 | 409 | goto nla_put_failure; |
edea8f7c AA |
410 | |
411 | if (rdev->wpan_phy.cca.mode == NL802154_CCA_ENERGY_CARRIER) { | |
412 | if (nla_put_u32(msg, NL802154_ATTR_CCA_OPT, | |
413 | rdev->wpan_phy.cca.opt)) | |
414 | goto nla_put_failure; | |
415 | } | |
ba2a9506 AA |
416 | } |
417 | ||
edea8f7c AA |
418 | if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) { |
419 | if (nla_put_s32(msg, NL802154_ATTR_TX_POWER, | |
420 | rdev->wpan_phy.transmit_power)) | |
421 | goto nla_put_failure; | |
422 | } | |
ca20ce20 | 423 | |
0e665457 AA |
424 | if (nl802154_put_capabilities(msg, rdev)) |
425 | goto nla_put_failure; | |
426 | ||
ca20ce20 | 427 | finish: |
053c095a JB |
428 | genlmsg_end(msg, hdr); |
429 | return 0; | |
ca20ce20 AA |
430 | |
431 | nla_put_failure: | |
432 | genlmsg_cancel(msg, hdr); | |
433 | return -EMSGSIZE; | |
434 | } | |
435 | ||
436 | struct nl802154_dump_wpan_phy_state { | |
437 | s64 filter_wpan_phy; | |
438 | long start; | |
439 | ||
440 | }; | |
441 | ||
442 | static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb, | |
443 | struct netlink_callback *cb, | |
444 | struct nl802154_dump_wpan_phy_state *state) | |
445 | { | |
446 | struct nlattr **tb = nl802154_fam.attrbuf; | |
447 | int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize, | |
448 | tb, nl802154_fam.maxattr, nl802154_policy); | |
449 | ||
450 | /* TODO check if we can handle error here, | |
451 | * we have no backward compatibility | |
452 | */ | |
453 | if (ret) | |
454 | return 0; | |
455 | ||
456 | if (tb[NL802154_ATTR_WPAN_PHY]) | |
457 | state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]); | |
458 | if (tb[NL802154_ATTR_WPAN_DEV]) | |
459 | state->filter_wpan_phy = nla_get_u64(tb[NL802154_ATTR_WPAN_DEV]) >> 32; | |
460 | if (tb[NL802154_ATTR_IFINDEX]) { | |
461 | struct net_device *netdev; | |
462 | struct cfg802154_registered_device *rdev; | |
463 | int ifidx = nla_get_u32(tb[NL802154_ATTR_IFINDEX]); | |
464 | ||
465 | /* TODO netns */ | |
466 | netdev = __dev_get_by_index(&init_net, ifidx); | |
467 | if (!netdev) | |
468 | return -ENODEV; | |
469 | if (netdev->ieee802154_ptr) { | |
470 | rdev = wpan_phy_to_rdev( | |
471 | netdev->ieee802154_ptr->wpan_phy); | |
472 | state->filter_wpan_phy = rdev->wpan_phy_idx; | |
473 | } | |
474 | } | |
475 | ||
476 | return 0; | |
477 | } | |
478 | ||
479 | static int | |
480 | nl802154_dump_wpan_phy(struct sk_buff *skb, struct netlink_callback *cb) | |
481 | { | |
482 | int idx = 0, ret; | |
483 | struct nl802154_dump_wpan_phy_state *state = (void *)cb->args[0]; | |
484 | struct cfg802154_registered_device *rdev; | |
485 | ||
486 | rtnl_lock(); | |
487 | if (!state) { | |
488 | state = kzalloc(sizeof(*state), GFP_KERNEL); | |
489 | if (!state) { | |
490 | rtnl_unlock(); | |
491 | return -ENOMEM; | |
492 | } | |
493 | state->filter_wpan_phy = -1; | |
494 | ret = nl802154_dump_wpan_phy_parse(skb, cb, state); | |
495 | if (ret) { | |
496 | kfree(state); | |
497 | rtnl_unlock(); | |
498 | return ret; | |
499 | } | |
500 | cb->args[0] = (long)state; | |
501 | } | |
502 | ||
503 | list_for_each_entry(rdev, &cfg802154_rdev_list, list) { | |
504 | /* TODO net ns compare */ | |
505 | if (++idx <= state->start) | |
506 | continue; | |
507 | if (state->filter_wpan_phy != -1 && | |
508 | state->filter_wpan_phy != rdev->wpan_phy_idx) | |
509 | continue; | |
510 | /* attempt to fit multiple wpan_phy data chunks into the skb */ | |
511 | ret = nl802154_send_wpan_phy(rdev, | |
512 | NL802154_CMD_NEW_WPAN_PHY, | |
513 | skb, | |
514 | NETLINK_CB(cb->skb).portid, | |
515 | cb->nlh->nlmsg_seq, NLM_F_MULTI); | |
516 | if (ret < 0) { | |
517 | if ((ret == -ENOBUFS || ret == -EMSGSIZE) && | |
518 | !skb->len && cb->min_dump_alloc < 4096) { | |
519 | cb->min_dump_alloc = 4096; | |
520 | rtnl_unlock(); | |
521 | return 1; | |
522 | } | |
523 | idx--; | |
524 | break; | |
525 | } | |
526 | break; | |
527 | } | |
528 | rtnl_unlock(); | |
529 | ||
530 | state->start = idx; | |
531 | ||
532 | return skb->len; | |
533 | } | |
534 | ||
535 | static int nl802154_dump_wpan_phy_done(struct netlink_callback *cb) | |
536 | { | |
537 | kfree((void *)cb->args[0]); | |
538 | return 0; | |
539 | } | |
540 | ||
541 | static int nl802154_get_wpan_phy(struct sk_buff *skb, struct genl_info *info) | |
542 | { | |
543 | struct sk_buff *msg; | |
544 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
545 | ||
546 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
547 | if (!msg) | |
548 | return -ENOMEM; | |
549 | ||
550 | if (nl802154_send_wpan_phy(rdev, NL802154_CMD_NEW_WPAN_PHY, msg, | |
551 | info->snd_portid, info->snd_seq, 0) < 0) { | |
552 | nlmsg_free(msg); | |
553 | return -ENOBUFS; | |
554 | } | |
555 | ||
556 | return genlmsg_reply(msg, info); | |
557 | } | |
558 | ||
4b96aea0 AA |
559 | static inline u64 wpan_dev_id(struct wpan_dev *wpan_dev) |
560 | { | |
561 | return (u64)wpan_dev->identifier | | |
562 | ((u64)wpan_phy_to_rdev(wpan_dev->wpan_phy)->wpan_phy_idx << 32); | |
563 | } | |
564 | ||
565 | static int | |
566 | nl802154_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags, | |
567 | struct cfg802154_registered_device *rdev, | |
568 | struct wpan_dev *wpan_dev) | |
569 | { | |
570 | struct net_device *dev = wpan_dev->netdev; | |
571 | void *hdr; | |
572 | ||
573 | hdr = nl802154hdr_put(msg, portid, seq, flags, | |
574 | NL802154_CMD_NEW_INTERFACE); | |
575 | if (!hdr) | |
576 | return -1; | |
577 | ||
578 | if (dev && | |
579 | (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex) || | |
580 | nla_put_string(msg, NL802154_ATTR_IFNAME, dev->name))) | |
581 | goto nla_put_failure; | |
582 | ||
583 | if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) || | |
584 | nla_put_u32(msg, NL802154_ATTR_IFTYPE, wpan_dev->iftype) || | |
585 | nla_put_u64(msg, NL802154_ATTR_WPAN_DEV, wpan_dev_id(wpan_dev)) || | |
586 | nla_put_u32(msg, NL802154_ATTR_GENERATION, | |
587 | rdev->devlist_generation ^ | |
588 | (cfg802154_rdev_list_generation << 2))) | |
589 | goto nla_put_failure; | |
590 | ||
591 | /* address settings */ | |
592 | if (nla_put_le64(msg, NL802154_ATTR_EXTENDED_ADDR, | |
593 | wpan_dev->extended_addr) || | |
594 | nla_put_le16(msg, NL802154_ATTR_SHORT_ADDR, | |
595 | wpan_dev->short_addr) || | |
596 | nla_put_le16(msg, NL802154_ATTR_PAN_ID, wpan_dev->pan_id)) | |
597 | goto nla_put_failure; | |
598 | ||
599 | /* ARET handling */ | |
600 | if (nla_put_s8(msg, NL802154_ATTR_MAX_FRAME_RETRIES, | |
601 | wpan_dev->frame_retries) || | |
602 | nla_put_u8(msg, NL802154_ATTR_MAX_BE, wpan_dev->max_be) || | |
603 | nla_put_u8(msg, NL802154_ATTR_MAX_CSMA_BACKOFFS, | |
604 | wpan_dev->csma_retries) || | |
605 | nla_put_u8(msg, NL802154_ATTR_MIN_BE, wpan_dev->min_be)) | |
606 | goto nla_put_failure; | |
607 | ||
608 | /* listen before transmit */ | |
609 | if (nla_put_u8(msg, NL802154_ATTR_LBT_MODE, wpan_dev->lbt)) | |
610 | goto nla_put_failure; | |
611 | ||
053c095a JB |
612 | genlmsg_end(msg, hdr); |
613 | return 0; | |
4b96aea0 AA |
614 | |
615 | nla_put_failure: | |
616 | genlmsg_cancel(msg, hdr); | |
617 | return -EMSGSIZE; | |
618 | } | |
619 | ||
620 | static int | |
621 | nl802154_dump_interface(struct sk_buff *skb, struct netlink_callback *cb) | |
622 | { | |
623 | int wp_idx = 0; | |
624 | int if_idx = 0; | |
625 | int wp_start = cb->args[0]; | |
626 | int if_start = cb->args[1]; | |
627 | struct cfg802154_registered_device *rdev; | |
628 | struct wpan_dev *wpan_dev; | |
629 | ||
630 | rtnl_lock(); | |
631 | list_for_each_entry(rdev, &cfg802154_rdev_list, list) { | |
632 | /* TODO netns compare */ | |
633 | if (wp_idx < wp_start) { | |
634 | wp_idx++; | |
635 | continue; | |
636 | } | |
637 | if_idx = 0; | |
638 | ||
639 | list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) { | |
640 | if (if_idx < if_start) { | |
641 | if_idx++; | |
642 | continue; | |
643 | } | |
644 | if (nl802154_send_iface(skb, NETLINK_CB(cb->skb).portid, | |
645 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
646 | rdev, wpan_dev) < 0) { | |
647 | goto out; | |
648 | } | |
649 | if_idx++; | |
650 | } | |
651 | ||
652 | wp_idx++; | |
653 | } | |
654 | out: | |
655 | rtnl_unlock(); | |
656 | ||
657 | cb->args[0] = wp_idx; | |
658 | cb->args[1] = if_idx; | |
659 | ||
660 | return skb->len; | |
661 | } | |
662 | ||
663 | static int nl802154_get_interface(struct sk_buff *skb, struct genl_info *info) | |
664 | { | |
665 | struct sk_buff *msg; | |
666 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
667 | struct wpan_dev *wdev = info->user_ptr[1]; | |
668 | ||
669 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
670 | if (!msg) | |
671 | return -ENOMEM; | |
672 | ||
673 | if (nl802154_send_iface(msg, info->snd_portid, info->snd_seq, 0, | |
674 | rdev, wdev) < 0) { | |
675 | nlmsg_free(msg); | |
676 | return -ENOBUFS; | |
677 | } | |
678 | ||
679 | return genlmsg_reply(msg, info); | |
680 | } | |
681 | ||
f3ea5e44 AA |
682 | static int nl802154_new_interface(struct sk_buff *skb, struct genl_info *info) |
683 | { | |
684 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
685 | enum nl802154_iftype type = NL802154_IFTYPE_UNSPEC; | |
0e57547e | 686 | __le64 extended_addr = cpu_to_le64(0x0000000000000000ULL); |
f3ea5e44 AA |
687 | |
688 | /* TODO avoid failing a new interface | |
689 | * creation due to pending removal? | |
690 | */ | |
691 | ||
692 | if (!info->attrs[NL802154_ATTR_IFNAME]) | |
693 | return -EINVAL; | |
694 | ||
695 | if (info->attrs[NL802154_ATTR_IFTYPE]) { | |
696 | type = nla_get_u32(info->attrs[NL802154_ATTR_IFTYPE]); | |
65318680 AA |
697 | if (type > NL802154_IFTYPE_MAX || |
698 | !(rdev->wpan_phy.supported.iftypes & BIT(type))) | |
f3ea5e44 AA |
699 | return -EINVAL; |
700 | } | |
701 | ||
0e57547e AA |
702 | /* TODO add nla_get_le64 to netlink */ |
703 | if (info->attrs[NL802154_ATTR_EXTENDED_ADDR]) | |
704 | extended_addr = (__force __le64)nla_get_u64( | |
705 | info->attrs[NL802154_ATTR_EXTENDED_ADDR]); | |
706 | ||
f3ea5e44 AA |
707 | if (!rdev->ops->add_virtual_intf) |
708 | return -EOPNOTSUPP; | |
709 | ||
710 | return rdev_add_virtual_intf(rdev, | |
711 | nla_data(info->attrs[NL802154_ATTR_IFNAME]), | |
5b4a1039 | 712 | NET_NAME_USER, type, extended_addr); |
f3ea5e44 AA |
713 | } |
714 | ||
b821ecd4 AA |
715 | static int nl802154_del_interface(struct sk_buff *skb, struct genl_info *info) |
716 | { | |
717 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
718 | struct wpan_dev *wpan_dev = info->user_ptr[1]; | |
719 | ||
720 | if (!rdev->ops->del_virtual_intf) | |
721 | return -EOPNOTSUPP; | |
722 | ||
723 | /* If we remove a wpan device without a netdev then clear | |
724 | * user_ptr[1] so that nl802154_post_doit won't dereference it | |
725 | * to check if it needs to do dev_put(). Otherwise it crashes | |
726 | * since the wpan_dev has been freed, unlike with a netdev where | |
727 | * we need the dev_put() for the netdev to really be freed. | |
728 | */ | |
729 | if (!wpan_dev->netdev) | |
730 | info->user_ptr[1] = NULL; | |
731 | ||
732 | return rdev_del_virtual_intf(rdev, wpan_dev); | |
733 | } | |
734 | ||
ab0bd561 AA |
735 | static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info) |
736 | { | |
737 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
738 | u8 channel, page; | |
739 | ||
740 | if (!info->attrs[NL802154_ATTR_PAGE] || | |
741 | !info->attrs[NL802154_ATTR_CHANNEL]) | |
742 | return -EINVAL; | |
743 | ||
744 | page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]); | |
745 | channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]); | |
746 | ||
747 | /* check 802.15.4 constraints */ | |
673692fa | 748 | if (page > IEEE802154_MAX_PAGE || channel > IEEE802154_MAX_CHANNEL || |
72f655e4 | 749 | !(rdev->wpan_phy.supported.channels[page] & BIT(channel))) |
ab0bd561 AA |
750 | return -EINVAL; |
751 | ||
752 | return rdev_set_channel(rdev, page, channel); | |
753 | } | |
754 | ||
ba2a9506 AA |
755 | static int nl802154_set_cca_mode(struct sk_buff *skb, struct genl_info *info) |
756 | { | |
757 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
758 | struct wpan_phy_cca cca; | |
759 | ||
fc4f8052 | 760 | if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE)) |
edea8f7c AA |
761 | return -EOPNOTSUPP; |
762 | ||
ba2a9506 AA |
763 | if (!info->attrs[NL802154_ATTR_CCA_MODE]) |
764 | return -EINVAL; | |
765 | ||
766 | cca.mode = nla_get_u32(info->attrs[NL802154_ATTR_CCA_MODE]); | |
767 | /* checking 802.15.4 constraints */ | |
fea3318d AA |
768 | if (cca.mode < NL802154_CCA_ENERGY || |
769 | cca.mode > NL802154_CCA_ATTR_MAX || | |
770 | !(rdev->wpan_phy.supported.cca_modes & BIT(cca.mode))) | |
ba2a9506 AA |
771 | return -EINVAL; |
772 | ||
773 | if (cca.mode == NL802154_CCA_ENERGY_CARRIER) { | |
774 | if (!info->attrs[NL802154_ATTR_CCA_OPT]) | |
775 | return -EINVAL; | |
776 | ||
777 | cca.opt = nla_get_u32(info->attrs[NL802154_ATTR_CCA_OPT]); | |
fea3318d AA |
778 | if (cca.opt > NL802154_CCA_OPT_ATTR_MAX || |
779 | !(rdev->wpan_phy.supported.cca_opts & BIT(cca.opt))) | |
ba2a9506 AA |
780 | return -EINVAL; |
781 | } | |
782 | ||
783 | return rdev_set_cca_mode(rdev, &cca); | |
784 | } | |
785 | ||
0f999b09 VB |
786 | static int nl802154_set_tx_power(struct sk_buff *skb, struct genl_info *info) |
787 | { | |
788 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
789 | s32 power; | |
790 | int i; | |
791 | ||
792 | if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER)) | |
793 | return -EOPNOTSUPP; | |
794 | ||
795 | if (!info->attrs[NL802154_ATTR_TX_POWER]) | |
796 | return -EINVAL; | |
797 | ||
798 | power = nla_get_s32(info->attrs[NL802154_ATTR_TX_POWER]); | |
799 | ||
800 | for (i = 0; i < rdev->wpan_phy.supported.tx_powers_size; i++) { | |
801 | if (power == rdev->wpan_phy.supported.tx_powers[i]) | |
802 | return rdev_set_tx_power(rdev, power); | |
803 | } | |
804 | ||
805 | return -EINVAL; | |
806 | } | |
807 | ||
702bf371 AA |
808 | static int nl802154_set_pan_id(struct sk_buff *skb, struct genl_info *info) |
809 | { | |
810 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
811 | struct net_device *dev = info->user_ptr[1]; | |
812 | struct wpan_dev *wpan_dev = dev->ieee802154_ptr; | |
ee7b9053 | 813 | __le16 pan_id; |
702bf371 AA |
814 | |
815 | /* conflict here while tx/rx calls */ | |
816 | if (netif_running(dev)) | |
817 | return -EBUSY; | |
818 | ||
819 | /* don't change address fields on monitor */ | |
0cf0879a AA |
820 | if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR || |
821 | !info->attrs[NL802154_ATTR_PAN_ID]) | |
702bf371 AA |
822 | return -EINVAL; |
823 | ||
ee7b9053 | 824 | pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]); |
702bf371 | 825 | |
673692fa AA |
826 | /* TODO |
827 | * I am not sure about to check here on broadcast pan_id. | |
828 | * Broadcast is a valid setting, comment from 802.15.4: | |
829 | * If this value is 0xffff, the device is not associated. | |
830 | * | |
831 | * This could useful to simple deassociate an device. | |
832 | */ | |
833 | if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST)) | |
834 | return -EINVAL; | |
835 | ||
702bf371 AA |
836 | return rdev_set_pan_id(rdev, wpan_dev, pan_id); |
837 | } | |
838 | ||
9830c62a AA |
839 | static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info) |
840 | { | |
841 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
842 | struct net_device *dev = info->user_ptr[1]; | |
843 | struct wpan_dev *wpan_dev = dev->ieee802154_ptr; | |
ee7b9053 | 844 | __le16 short_addr; |
9830c62a AA |
845 | |
846 | /* conflict here while tx/rx calls */ | |
847 | if (netif_running(dev)) | |
848 | return -EBUSY; | |
849 | ||
850 | /* don't change address fields on monitor */ | |
0cf0879a AA |
851 | if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR || |
852 | !info->attrs[NL802154_ATTR_SHORT_ADDR]) | |
9830c62a AA |
853 | return -EINVAL; |
854 | ||
ee7b9053 | 855 | short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]); |
9830c62a | 856 | |
673692fa AA |
857 | /* TODO |
858 | * I am not sure about to check here on broadcast short_addr. | |
859 | * Broadcast is a valid setting, comment from 802.15.4: | |
860 | * A value of 0xfffe indicates that the device has | |
861 | * associated but has not been allocated an address. A | |
862 | * value of 0xffff indicates that the device does not | |
863 | * have a short address. | |
864 | * | |
865 | * I think we should allow to set these settings but | |
866 | * don't allow to allow socket communication with it. | |
867 | */ | |
868 | if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) || | |
869 | short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST)) | |
870 | return -EINVAL; | |
871 | ||
9830c62a AA |
872 | return rdev_set_short_addr(rdev, wpan_dev, short_addr); |
873 | } | |
874 | ||
656a999e AA |
875 | static int |
876 | nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info) | |
877 | { | |
878 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
879 | struct net_device *dev = info->user_ptr[1]; | |
880 | struct wpan_dev *wpan_dev = dev->ieee802154_ptr; | |
881 | u8 min_be, max_be; | |
882 | ||
883 | /* should be set on netif open inside phy settings */ | |
884 | if (netif_running(dev)) | |
885 | return -EBUSY; | |
886 | ||
887 | if (!info->attrs[NL802154_ATTR_MIN_BE] || | |
888 | !info->attrs[NL802154_ATTR_MAX_BE]) | |
889 | return -EINVAL; | |
890 | ||
891 | min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]); | |
892 | max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]); | |
893 | ||
894 | /* check 802.15.4 constraints */ | |
fea3318d AA |
895 | if (min_be < rdev->wpan_phy.supported.min_minbe || |
896 | min_be > rdev->wpan_phy.supported.max_minbe || | |
897 | max_be < rdev->wpan_phy.supported.min_maxbe || | |
898 | max_be > rdev->wpan_phy.supported.max_maxbe || | |
899 | min_be > max_be) | |
656a999e AA |
900 | return -EINVAL; |
901 | ||
902 | return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be); | |
903 | } | |
904 | ||
a01ba765 AA |
905 | static int |
906 | nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info) | |
907 | { | |
908 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
909 | struct net_device *dev = info->user_ptr[1]; | |
910 | struct wpan_dev *wpan_dev = dev->ieee802154_ptr; | |
911 | u8 max_csma_backoffs; | |
912 | ||
913 | /* conflict here while other running iface settings */ | |
914 | if (netif_running(dev)) | |
915 | return -EBUSY; | |
916 | ||
917 | if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]) | |
918 | return -EINVAL; | |
919 | ||
920 | max_csma_backoffs = nla_get_u8( | |
921 | info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]); | |
922 | ||
923 | /* check 802.15.4 constraints */ | |
fea3318d AA |
924 | if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs || |
925 | max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs) | |
a01ba765 AA |
926 | return -EINVAL; |
927 | ||
928 | return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs); | |
929 | } | |
930 | ||
17a3a46b AA |
931 | static int |
932 | nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info) | |
933 | { | |
934 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
935 | struct net_device *dev = info->user_ptr[1]; | |
936 | struct wpan_dev *wpan_dev = dev->ieee802154_ptr; | |
937 | s8 max_frame_retries; | |
938 | ||
939 | if (netif_running(dev)) | |
940 | return -EBUSY; | |
941 | ||
942 | if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]) | |
943 | return -EINVAL; | |
944 | ||
945 | max_frame_retries = nla_get_s8( | |
946 | info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]); | |
947 | ||
948 | /* check 802.15.4 constraints */ | |
fea3318d AA |
949 | if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries || |
950 | max_frame_retries > rdev->wpan_phy.supported.max_frame_retries) | |
17a3a46b AA |
951 | return -EINVAL; |
952 | ||
953 | return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries); | |
954 | } | |
955 | ||
c8937a1d AA |
956 | static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info) |
957 | { | |
958 | struct cfg802154_registered_device *rdev = info->user_ptr[0]; | |
959 | struct net_device *dev = info->user_ptr[1]; | |
960 | struct wpan_dev *wpan_dev = dev->ieee802154_ptr; | |
961 | bool mode; | |
962 | ||
963 | if (netif_running(dev)) | |
964 | return -EBUSY; | |
965 | ||
966 | if (!info->attrs[NL802154_ATTR_LBT_MODE]) | |
967 | return -EINVAL; | |
968 | ||
969 | mode = !!nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]); | |
fea3318d AA |
970 | if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt)) |
971 | return -EINVAL; | |
972 | ||
c8937a1d AA |
973 | return rdev_set_lbt_mode(rdev, wpan_dev, mode); |
974 | } | |
975 | ||
79fe1a2a AA |
976 | #define NL802154_FLAG_NEED_WPAN_PHY 0x01 |
977 | #define NL802154_FLAG_NEED_NETDEV 0x02 | |
978 | #define NL802154_FLAG_NEED_RTNL 0x04 | |
979 | #define NL802154_FLAG_CHECK_NETDEV_UP 0x08 | |
980 | #define NL802154_FLAG_NEED_NETDEV_UP (NL802154_FLAG_NEED_NETDEV |\ | |
981 | NL802154_FLAG_CHECK_NETDEV_UP) | |
982 | #define NL802154_FLAG_NEED_WPAN_DEV 0x10 | |
983 | #define NL802154_FLAG_NEED_WPAN_DEV_UP (NL802154_FLAG_NEED_WPAN_DEV |\ | |
984 | NL802154_FLAG_CHECK_NETDEV_UP) | |
985 | ||
986 | static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb, | |
987 | struct genl_info *info) | |
988 | { | |
989 | struct cfg802154_registered_device *rdev; | |
990 | struct wpan_dev *wpan_dev; | |
991 | struct net_device *dev; | |
992 | bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL; | |
993 | ||
994 | if (rtnl) | |
995 | rtnl_lock(); | |
996 | ||
997 | if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) { | |
998 | rdev = cfg802154_get_dev_from_info(genl_info_net(info), info); | |
999 | if (IS_ERR(rdev)) { | |
1000 | if (rtnl) | |
1001 | rtnl_unlock(); | |
1002 | return PTR_ERR(rdev); | |
1003 | } | |
1004 | info->user_ptr[0] = rdev; | |
1005 | } else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV || | |
1006 | ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) { | |
1007 | ASSERT_RTNL(); | |
1008 | wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info), | |
1009 | info->attrs); | |
1010 | if (IS_ERR(wpan_dev)) { | |
1011 | if (rtnl) | |
1012 | rtnl_unlock(); | |
1013 | return PTR_ERR(wpan_dev); | |
1014 | } | |
1015 | ||
1016 | dev = wpan_dev->netdev; | |
1017 | rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy); | |
1018 | ||
1019 | if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) { | |
1020 | if (!dev) { | |
1021 | if (rtnl) | |
1022 | rtnl_unlock(); | |
1023 | return -EINVAL; | |
1024 | } | |
1025 | ||
1026 | info->user_ptr[1] = dev; | |
1027 | } else { | |
1028 | info->user_ptr[1] = wpan_dev; | |
1029 | } | |
1030 | ||
1031 | if (dev) { | |
1032 | if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP && | |
1033 | !netif_running(dev)) { | |
1034 | if (rtnl) | |
1035 | rtnl_unlock(); | |
1036 | return -ENETDOWN; | |
1037 | } | |
1038 | ||
1039 | dev_hold(dev); | |
1040 | } | |
1041 | ||
1042 | info->user_ptr[0] = rdev; | |
1043 | } | |
1044 | ||
1045 | return 0; | |
1046 | } | |
1047 | ||
1048 | static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb, | |
1049 | struct genl_info *info) | |
1050 | { | |
1051 | if (info->user_ptr[1]) { | |
1052 | if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) { | |
1053 | struct wpan_dev *wpan_dev = info->user_ptr[1]; | |
1054 | ||
1055 | if (wpan_dev->netdev) | |
1056 | dev_put(wpan_dev->netdev); | |
1057 | } else { | |
1058 | dev_put(info->user_ptr[1]); | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | if (ops->internal_flags & NL802154_FLAG_NEED_RTNL) | |
1063 | rtnl_unlock(); | |
1064 | } | |
1065 | ||
1066 | static const struct genl_ops nl802154_ops[] = { | |
ca20ce20 AA |
1067 | { |
1068 | .cmd = NL802154_CMD_GET_WPAN_PHY, | |
1069 | .doit = nl802154_get_wpan_phy, | |
1070 | .dumpit = nl802154_dump_wpan_phy, | |
1071 | .done = nl802154_dump_wpan_phy_done, | |
1072 | .policy = nl802154_policy, | |
1073 | /* can be retrieved by unprivileged users */ | |
1074 | .internal_flags = NL802154_FLAG_NEED_WPAN_PHY | | |
1075 | NL802154_FLAG_NEED_RTNL, | |
1076 | }, | |
4b96aea0 AA |
1077 | { |
1078 | .cmd = NL802154_CMD_GET_INTERFACE, | |
1079 | .doit = nl802154_get_interface, | |
1080 | .dumpit = nl802154_dump_interface, | |
1081 | .policy = nl802154_policy, | |
1082 | /* can be retrieved by unprivileged users */ | |
1083 | .internal_flags = NL802154_FLAG_NEED_WPAN_DEV | | |
1084 | NL802154_FLAG_NEED_RTNL, | |
1085 | }, | |
f3ea5e44 AA |
1086 | { |
1087 | .cmd = NL802154_CMD_NEW_INTERFACE, | |
1088 | .doit = nl802154_new_interface, | |
1089 | .policy = nl802154_policy, | |
1090 | .flags = GENL_ADMIN_PERM, | |
1091 | .internal_flags = NL802154_FLAG_NEED_WPAN_PHY | | |
1092 | NL802154_FLAG_NEED_RTNL, | |
1093 | }, | |
b821ecd4 AA |
1094 | { |
1095 | .cmd = NL802154_CMD_DEL_INTERFACE, | |
1096 | .doit = nl802154_del_interface, | |
1097 | .policy = nl802154_policy, | |
1098 | .flags = GENL_ADMIN_PERM, | |
1099 | .internal_flags = NL802154_FLAG_NEED_WPAN_DEV | | |
1100 | NL802154_FLAG_NEED_RTNL, | |
1101 | }, | |
ab0bd561 AA |
1102 | { |
1103 | .cmd = NL802154_CMD_SET_CHANNEL, | |
1104 | .doit = nl802154_set_channel, | |
1105 | .policy = nl802154_policy, | |
1106 | .flags = GENL_ADMIN_PERM, | |
1107 | .internal_flags = NL802154_FLAG_NEED_WPAN_PHY | | |
1108 | NL802154_FLAG_NEED_RTNL, | |
1109 | }, | |
ba2a9506 AA |
1110 | { |
1111 | .cmd = NL802154_CMD_SET_CCA_MODE, | |
1112 | .doit = nl802154_set_cca_mode, | |
1113 | .policy = nl802154_policy, | |
1114 | .flags = GENL_ADMIN_PERM, | |
1115 | .internal_flags = NL802154_FLAG_NEED_WPAN_PHY | | |
1116 | NL802154_FLAG_NEED_RTNL, | |
1117 | }, | |
0f999b09 VB |
1118 | { |
1119 | .cmd = NL802154_CMD_SET_TX_POWER, | |
1120 | .doit = nl802154_set_tx_power, | |
1121 | .policy = nl802154_policy, | |
1122 | .flags = GENL_ADMIN_PERM, | |
1123 | .internal_flags = NL802154_FLAG_NEED_WPAN_PHY | | |
1124 | NL802154_FLAG_NEED_RTNL, | |
1125 | }, | |
702bf371 AA |
1126 | { |
1127 | .cmd = NL802154_CMD_SET_PAN_ID, | |
1128 | .doit = nl802154_set_pan_id, | |
1129 | .policy = nl802154_policy, | |
1130 | .flags = GENL_ADMIN_PERM, | |
1131 | .internal_flags = NL802154_FLAG_NEED_NETDEV | | |
1132 | NL802154_FLAG_NEED_RTNL, | |
1133 | }, | |
9830c62a AA |
1134 | { |
1135 | .cmd = NL802154_CMD_SET_SHORT_ADDR, | |
1136 | .doit = nl802154_set_short_addr, | |
1137 | .policy = nl802154_policy, | |
1138 | .flags = GENL_ADMIN_PERM, | |
1139 | .internal_flags = NL802154_FLAG_NEED_NETDEV | | |
1140 | NL802154_FLAG_NEED_RTNL, | |
1141 | }, | |
656a999e AA |
1142 | { |
1143 | .cmd = NL802154_CMD_SET_BACKOFF_EXPONENT, | |
1144 | .doit = nl802154_set_backoff_exponent, | |
1145 | .policy = nl802154_policy, | |
1146 | .flags = GENL_ADMIN_PERM, | |
1147 | .internal_flags = NL802154_FLAG_NEED_NETDEV | | |
1148 | NL802154_FLAG_NEED_RTNL, | |
1149 | }, | |
a01ba765 AA |
1150 | { |
1151 | .cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS, | |
1152 | .doit = nl802154_set_max_csma_backoffs, | |
1153 | .policy = nl802154_policy, | |
1154 | .flags = GENL_ADMIN_PERM, | |
1155 | .internal_flags = NL802154_FLAG_NEED_NETDEV | | |
1156 | NL802154_FLAG_NEED_RTNL, | |
1157 | }, | |
17a3a46b AA |
1158 | { |
1159 | .cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES, | |
1160 | .doit = nl802154_set_max_frame_retries, | |
1161 | .policy = nl802154_policy, | |
1162 | .flags = GENL_ADMIN_PERM, | |
1163 | .internal_flags = NL802154_FLAG_NEED_NETDEV | | |
1164 | NL802154_FLAG_NEED_RTNL, | |
1165 | }, | |
c8937a1d AA |
1166 | { |
1167 | .cmd = NL802154_CMD_SET_LBT_MODE, | |
1168 | .doit = nl802154_set_lbt_mode, | |
1169 | .policy = nl802154_policy, | |
1170 | .flags = GENL_ADMIN_PERM, | |
1171 | .internal_flags = NL802154_FLAG_NEED_NETDEV | | |
1172 | NL802154_FLAG_NEED_RTNL, | |
1173 | }, | |
79fe1a2a AA |
1174 | }; |
1175 | ||
1176 | /* initialisation/exit functions */ | |
1177 | int nl802154_init(void) | |
1178 | { | |
1179 | return genl_register_family_with_ops_groups(&nl802154_fam, nl802154_ops, | |
1180 | nl802154_mcgrps); | |
1181 | } | |
1182 | ||
1183 | void nl802154_exit(void) | |
1184 | { | |
1185 | genl_unregister_family(&nl802154_fam); | |
1186 | } |