GNU Linux-libre 4.14.266-gnu1
[releases.git] / net / ieee802154 / nl802154.c
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"
26 #include "rdev-ops.h"
27 #include "core.h"
28
29 /* the netlink family */
30 static struct genl_family nl802154_fam;
31
32 /* multicast groups */
33 enum nl802154_multicast_groups {
34         NL802154_MCGRP_CONFIG,
35 };
36
37 static const struct genl_multicast_group nl802154_mcgrps[] = {
38         [NL802154_MCGRP_CONFIG] = { .name = "config", },
39 };
40
41 /* returns ERR_PTR values */
42 static struct wpan_dev *
43 __cfg802154_wpan_dev_from_attrs(struct net *netns, struct nlattr **attrs)
44 {
45         struct cfg802154_registered_device *rdev;
46         struct wpan_dev *result = NULL;
47         bool have_ifidx = attrs[NL802154_ATTR_IFINDEX];
48         bool have_wpan_dev_id = attrs[NL802154_ATTR_WPAN_DEV];
49         u64 wpan_dev_id;
50         int wpan_phy_idx = -1;
51         int ifidx = -1;
52
53         ASSERT_RTNL();
54
55         if (!have_ifidx && !have_wpan_dev_id)
56                 return ERR_PTR(-EINVAL);
57
58         if (have_ifidx)
59                 ifidx = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
60         if (have_wpan_dev_id) {
61                 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
62                 wpan_phy_idx = wpan_dev_id >> 32;
63         }
64
65         list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
66                 struct wpan_dev *wpan_dev;
67
68                 if (wpan_phy_net(&rdev->wpan_phy) != netns)
69                         continue;
70
71                 if (have_wpan_dev_id && rdev->wpan_phy_idx != wpan_phy_idx)
72                         continue;
73
74                 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
75                         if (have_ifidx && wpan_dev->netdev &&
76                             wpan_dev->netdev->ifindex == ifidx) {
77                                 result = wpan_dev;
78                                 break;
79                         }
80                         if (have_wpan_dev_id &&
81                             wpan_dev->identifier == (u32)wpan_dev_id) {
82                                 result = wpan_dev;
83                                 break;
84                         }
85                 }
86
87                 if (result)
88                         break;
89         }
90
91         if (result)
92                 return result;
93
94         return ERR_PTR(-ENODEV);
95 }
96
97 static struct cfg802154_registered_device *
98 __cfg802154_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
99 {
100         struct cfg802154_registered_device *rdev = NULL, *tmp;
101         struct net_device *netdev;
102
103         ASSERT_RTNL();
104
105         if (!attrs[NL802154_ATTR_WPAN_PHY] &&
106             !attrs[NL802154_ATTR_IFINDEX] &&
107             !attrs[NL802154_ATTR_WPAN_DEV])
108                 return ERR_PTR(-EINVAL);
109
110         if (attrs[NL802154_ATTR_WPAN_PHY])
111                 rdev = cfg802154_rdev_by_wpan_phy_idx(
112                                 nla_get_u32(attrs[NL802154_ATTR_WPAN_PHY]));
113
114         if (attrs[NL802154_ATTR_WPAN_DEV]) {
115                 u64 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
116                 struct wpan_dev *wpan_dev;
117                 bool found = false;
118
119                 tmp = cfg802154_rdev_by_wpan_phy_idx(wpan_dev_id >> 32);
120                 if (tmp) {
121                         /* make sure wpan_dev exists */
122                         list_for_each_entry(wpan_dev, &tmp->wpan_dev_list, list) {
123                                 if (wpan_dev->identifier != (u32)wpan_dev_id)
124                                         continue;
125                                 found = true;
126                                 break;
127                         }
128
129                         if (!found)
130                                 tmp = NULL;
131
132                         if (rdev && tmp != rdev)
133                                 return ERR_PTR(-EINVAL);
134                         rdev = tmp;
135                 }
136         }
137
138         if (attrs[NL802154_ATTR_IFINDEX]) {
139                 int ifindex = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
140
141                 netdev = __dev_get_by_index(netns, ifindex);
142                 if (netdev) {
143                         if (netdev->ieee802154_ptr)
144                                 tmp = wpan_phy_to_rdev(
145                                                 netdev->ieee802154_ptr->wpan_phy);
146                         else
147                                 tmp = NULL;
148
149                         /* not wireless device -- return error */
150                         if (!tmp)
151                                 return ERR_PTR(-EINVAL);
152
153                         /* mismatch -- return error */
154                         if (rdev && tmp != rdev)
155                                 return ERR_PTR(-EINVAL);
156
157                         rdev = tmp;
158                 }
159         }
160
161         if (!rdev)
162                 return ERR_PTR(-ENODEV);
163
164         if (netns != wpan_phy_net(&rdev->wpan_phy))
165                 return ERR_PTR(-ENODEV);
166
167         return rdev;
168 }
169
170 /* This function returns a pointer to the driver
171  * that the genl_info item that is passed refers to.
172  *
173  * The result of this can be a PTR_ERR and hence must
174  * be checked with IS_ERR() for errors.
175  */
176 static struct cfg802154_registered_device *
177 cfg802154_get_dev_from_info(struct net *netns, struct genl_info *info)
178 {
179         return __cfg802154_rdev_from_attrs(netns, info->attrs);
180 }
181
182 /* policy for the attributes */
183 static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
184         [NL802154_ATTR_WPAN_PHY] = { .type = NLA_U32 },
185         [NL802154_ATTR_WPAN_PHY_NAME] = { .type = NLA_NUL_STRING,
186                                           .len = 20-1 },
187
188         [NL802154_ATTR_IFINDEX] = { .type = NLA_U32 },
189         [NL802154_ATTR_IFTYPE] = { .type = NLA_U32 },
190         [NL802154_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
191
192         [NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
193
194         [NL802154_ATTR_PAGE] = { .type = NLA_U8, },
195         [NL802154_ATTR_CHANNEL] = { .type = NLA_U8, },
196
197         [NL802154_ATTR_TX_POWER] = { .type = NLA_S32, },
198
199         [NL802154_ATTR_CCA_MODE] = { .type = NLA_U32, },
200         [NL802154_ATTR_CCA_OPT] = { .type = NLA_U32, },
201         [NL802154_ATTR_CCA_ED_LEVEL] = { .type = NLA_S32, },
202
203         [NL802154_ATTR_SUPPORTED_CHANNEL] = { .type = NLA_U32, },
204
205         [NL802154_ATTR_PAN_ID] = { .type = NLA_U16, },
206         [NL802154_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
207         [NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16, },
208
209         [NL802154_ATTR_MIN_BE] = { .type = NLA_U8, },
210         [NL802154_ATTR_MAX_BE] = { .type = NLA_U8, },
211         [NL802154_ATTR_MAX_CSMA_BACKOFFS] = { .type = NLA_U8, },
212
213         [NL802154_ATTR_MAX_FRAME_RETRIES] = { .type = NLA_S8, },
214
215         [NL802154_ATTR_LBT_MODE] = { .type = NLA_U8, },
216
217         [NL802154_ATTR_WPAN_PHY_CAPS] = { .type = NLA_NESTED },
218
219         [NL802154_ATTR_SUPPORTED_COMMANDS] = { .type = NLA_NESTED },
220
221         [NL802154_ATTR_ACKREQ_DEFAULT] = { .type = NLA_U8 },
222
223         [NL802154_ATTR_PID] = { .type = NLA_U32 },
224         [NL802154_ATTR_NETNS_FD] = { .type = NLA_U32 },
225 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
226         [NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
227         [NL802154_ATTR_SEC_OUT_LEVEL] = { .type = NLA_U32, },
228         [NL802154_ATTR_SEC_OUT_KEY_ID] = { .type = NLA_NESTED, },
229         [NL802154_ATTR_SEC_FRAME_COUNTER] = { .type = NLA_U32 },
230
231         [NL802154_ATTR_SEC_LEVEL] = { .type = NLA_NESTED },
232         [NL802154_ATTR_SEC_DEVICE] = { .type = NLA_NESTED },
233         [NL802154_ATTR_SEC_DEVKEY] = { .type = NLA_NESTED },
234         [NL802154_ATTR_SEC_KEY] = { .type = NLA_NESTED },
235 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
236 };
237
238 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
239 static int
240 nl802154_prepare_wpan_dev_dump(struct sk_buff *skb,
241                                struct netlink_callback *cb,
242                                struct cfg802154_registered_device **rdev,
243                                struct wpan_dev **wpan_dev)
244 {
245         int err;
246
247         rtnl_lock();
248
249         if (!cb->args[0]) {
250                 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize,
251                                   genl_family_attrbuf(&nl802154_fam),
252                                   nl802154_fam.maxattr, nl802154_policy, NULL);
253                 if (err)
254                         goto out_unlock;
255
256                 *wpan_dev = __cfg802154_wpan_dev_from_attrs(sock_net(skb->sk),
257                                                             genl_family_attrbuf(&nl802154_fam));
258                 if (IS_ERR(*wpan_dev)) {
259                         err = PTR_ERR(*wpan_dev);
260                         goto out_unlock;
261                 }
262                 *rdev = wpan_phy_to_rdev((*wpan_dev)->wpan_phy);
263                 /* 0 is the first index - add 1 to parse only once */
264                 cb->args[0] = (*rdev)->wpan_phy_idx + 1;
265                 cb->args[1] = (*wpan_dev)->identifier;
266         } else {
267                 /* subtract the 1 again here */
268                 struct wpan_phy *wpan_phy = wpan_phy_idx_to_wpan_phy(cb->args[0] - 1);
269                 struct wpan_dev *tmp;
270
271                 if (!wpan_phy) {
272                         err = -ENODEV;
273                         goto out_unlock;
274                 }
275                 *rdev = wpan_phy_to_rdev(wpan_phy);
276                 *wpan_dev = NULL;
277
278                 list_for_each_entry(tmp, &(*rdev)->wpan_dev_list, list) {
279                         if (tmp->identifier == cb->args[1]) {
280                                 *wpan_dev = tmp;
281                                 break;
282                         }
283                 }
284
285                 if (!*wpan_dev) {
286                         err = -ENODEV;
287                         goto out_unlock;
288                 }
289         }
290
291         return 0;
292  out_unlock:
293         rtnl_unlock();
294         return err;
295 }
296
297 static void
298 nl802154_finish_wpan_dev_dump(struct cfg802154_registered_device *rdev)
299 {
300         rtnl_unlock();
301 }
302 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
303
304 /* message building helper */
305 static inline void *nl802154hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
306                                     int flags, u8 cmd)
307 {
308         /* since there is no private header just add the generic one */
309         return genlmsg_put(skb, portid, seq, &nl802154_fam, flags, cmd);
310 }
311
312 static int
313 nl802154_put_flags(struct sk_buff *msg, int attr, u32 mask)
314 {
315         struct nlattr *nl_flags = nla_nest_start(msg, attr);
316         int i;
317
318         if (!nl_flags)
319                 return -ENOBUFS;
320
321         i = 0;
322         while (mask) {
323                 if ((mask & 1) && nla_put_flag(msg, i))
324                         return -ENOBUFS;
325
326                 mask >>= 1;
327                 i++;
328         }
329
330         nla_nest_end(msg, nl_flags);
331         return 0;
332 }
333
334 static int
335 nl802154_send_wpan_phy_channels(struct cfg802154_registered_device *rdev,
336                                 struct sk_buff *msg)
337 {
338         struct nlattr *nl_page;
339         unsigned long page;
340
341         nl_page = nla_nest_start(msg, NL802154_ATTR_CHANNELS_SUPPORTED);
342         if (!nl_page)
343                 return -ENOBUFS;
344
345         for (page = 0; page <= IEEE802154_MAX_PAGE; page++) {
346                 if (nla_put_u32(msg, NL802154_ATTR_SUPPORTED_CHANNEL,
347                                 rdev->wpan_phy.supported.channels[page]))
348                         return -ENOBUFS;
349         }
350         nla_nest_end(msg, nl_page);
351
352         return 0;
353 }
354
355 static int
356 nl802154_put_capabilities(struct sk_buff *msg,
357                           struct cfg802154_registered_device *rdev)
358 {
359         const struct wpan_phy_supported *caps = &rdev->wpan_phy.supported;
360         struct nlattr *nl_caps, *nl_channels;
361         int i;
362
363         nl_caps = nla_nest_start(msg, NL802154_ATTR_WPAN_PHY_CAPS);
364         if (!nl_caps)
365                 return -ENOBUFS;
366
367         nl_channels = nla_nest_start(msg, NL802154_CAP_ATTR_CHANNELS);
368         if (!nl_channels)
369                 return -ENOBUFS;
370
371         for (i = 0; i <= IEEE802154_MAX_PAGE; i++) {
372                 if (caps->channels[i]) {
373                         if (nl802154_put_flags(msg, i, caps->channels[i]))
374                                 return -ENOBUFS;
375                 }
376         }
377
378         nla_nest_end(msg, nl_channels);
379
380         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
381                 struct nlattr *nl_ed_lvls;
382
383                 nl_ed_lvls = nla_nest_start(msg,
384                                             NL802154_CAP_ATTR_CCA_ED_LEVELS);
385                 if (!nl_ed_lvls)
386                         return -ENOBUFS;
387
388                 for (i = 0; i < caps->cca_ed_levels_size; i++) {
389                         if (nla_put_s32(msg, i, caps->cca_ed_levels[i]))
390                                 return -ENOBUFS;
391                 }
392
393                 nla_nest_end(msg, nl_ed_lvls);
394         }
395
396         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
397                 struct nlattr *nl_tx_pwrs;
398
399                 nl_tx_pwrs = nla_nest_start(msg, NL802154_CAP_ATTR_TX_POWERS);
400                 if (!nl_tx_pwrs)
401                         return -ENOBUFS;
402
403                 for (i = 0; i < caps->tx_powers_size; i++) {
404                         if (nla_put_s32(msg, i, caps->tx_powers[i]))
405                                 return -ENOBUFS;
406                 }
407
408                 nla_nest_end(msg, nl_tx_pwrs);
409         }
410
411         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
412                 if (nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_MODES,
413                                        caps->cca_modes) ||
414                     nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_OPTS,
415                                        caps->cca_opts))
416                         return -ENOBUFS;
417         }
418
419         if (nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MINBE, caps->min_minbe) ||
420             nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MINBE, caps->max_minbe) ||
421             nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MAXBE, caps->min_maxbe) ||
422             nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MAXBE, caps->max_maxbe) ||
423             nla_put_u8(msg, NL802154_CAP_ATTR_MIN_CSMA_BACKOFFS,
424                        caps->min_csma_backoffs) ||
425             nla_put_u8(msg, NL802154_CAP_ATTR_MAX_CSMA_BACKOFFS,
426                        caps->max_csma_backoffs) ||
427             nla_put_s8(msg, NL802154_CAP_ATTR_MIN_FRAME_RETRIES,
428                        caps->min_frame_retries) ||
429             nla_put_s8(msg, NL802154_CAP_ATTR_MAX_FRAME_RETRIES,
430                        caps->max_frame_retries) ||
431             nl802154_put_flags(msg, NL802154_CAP_ATTR_IFTYPES,
432                                caps->iftypes) ||
433             nla_put_u32(msg, NL802154_CAP_ATTR_LBT, caps->lbt))
434                 return -ENOBUFS;
435
436         nla_nest_end(msg, nl_caps);
437
438         return 0;
439 }
440
441 static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev,
442                                   enum nl802154_commands cmd,
443                                   struct sk_buff *msg, u32 portid, u32 seq,
444                                   int flags)
445 {
446         struct nlattr *nl_cmds;
447         void *hdr;
448         int i;
449
450         hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
451         if (!hdr)
452                 return -ENOBUFS;
453
454         if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
455             nla_put_string(msg, NL802154_ATTR_WPAN_PHY_NAME,
456                            wpan_phy_name(&rdev->wpan_phy)) ||
457             nla_put_u32(msg, NL802154_ATTR_GENERATION,
458                         cfg802154_rdev_list_generation))
459                 goto nla_put_failure;
460
461         if (cmd != NL802154_CMD_NEW_WPAN_PHY)
462                 goto finish;
463
464         /* DUMP PHY PIB */
465
466         /* current channel settings */
467         if (nla_put_u8(msg, NL802154_ATTR_PAGE,
468                        rdev->wpan_phy.current_page) ||
469             nla_put_u8(msg, NL802154_ATTR_CHANNEL,
470                        rdev->wpan_phy.current_channel))
471                 goto nla_put_failure;
472
473         /* TODO remove this behaviour, we still keep support it for a while
474          * so users can change the behaviour to the new one.
475          */
476         if (nl802154_send_wpan_phy_channels(rdev, msg))
477                 goto nla_put_failure;
478
479         /* cca mode */
480         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
481                 if (nla_put_u32(msg, NL802154_ATTR_CCA_MODE,
482                                 rdev->wpan_phy.cca.mode))
483                         goto nla_put_failure;
484
485                 if (rdev->wpan_phy.cca.mode == NL802154_CCA_ENERGY_CARRIER) {
486                         if (nla_put_u32(msg, NL802154_ATTR_CCA_OPT,
487                                         rdev->wpan_phy.cca.opt))
488                                 goto nla_put_failure;
489                 }
490         }
491
492         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
493                 if (nla_put_s32(msg, NL802154_ATTR_TX_POWER,
494                                 rdev->wpan_phy.transmit_power))
495                         goto nla_put_failure;
496         }
497
498         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
499                 if (nla_put_s32(msg, NL802154_ATTR_CCA_ED_LEVEL,
500                                 rdev->wpan_phy.cca_ed_level))
501                         goto nla_put_failure;
502         }
503
504         if (nl802154_put_capabilities(msg, rdev))
505                 goto nla_put_failure;
506
507         nl_cmds = nla_nest_start(msg, NL802154_ATTR_SUPPORTED_COMMANDS);
508         if (!nl_cmds)
509                 goto nla_put_failure;
510
511         i = 0;
512 #define CMD(op, n)                                                      \
513         do {                                                            \
514                 if (rdev->ops->op) {                                    \
515                         i++;                                            \
516                         if (nla_put_u32(msg, i, NL802154_CMD_ ## n))    \
517                                 goto nla_put_failure;                   \
518                 }                                                       \
519         } while (0)
520
521         CMD(add_virtual_intf, NEW_INTERFACE);
522         CMD(del_virtual_intf, DEL_INTERFACE);
523         CMD(set_channel, SET_CHANNEL);
524         CMD(set_pan_id, SET_PAN_ID);
525         CMD(set_short_addr, SET_SHORT_ADDR);
526         CMD(set_backoff_exponent, SET_BACKOFF_EXPONENT);
527         CMD(set_max_csma_backoffs, SET_MAX_CSMA_BACKOFFS);
528         CMD(set_max_frame_retries, SET_MAX_FRAME_RETRIES);
529         CMD(set_lbt_mode, SET_LBT_MODE);
530         CMD(set_ackreq_default, SET_ACKREQ_DEFAULT);
531
532         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER)
533                 CMD(set_tx_power, SET_TX_POWER);
534
535         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL)
536                 CMD(set_cca_ed_level, SET_CCA_ED_LEVEL);
537
538         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE)
539                 CMD(set_cca_mode, SET_CCA_MODE);
540
541 #undef CMD
542         nla_nest_end(msg, nl_cmds);
543
544 finish:
545         genlmsg_end(msg, hdr);
546         return 0;
547
548 nla_put_failure:
549         genlmsg_cancel(msg, hdr);
550         return -EMSGSIZE;
551 }
552
553 struct nl802154_dump_wpan_phy_state {
554         s64 filter_wpan_phy;
555         long start;
556
557 };
558
559 static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb,
560                                         struct netlink_callback *cb,
561                                         struct nl802154_dump_wpan_phy_state *state)
562 {
563         struct nlattr **tb = genl_family_attrbuf(&nl802154_fam);
564         int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize, tb,
565                               nl802154_fam.maxattr, nl802154_policy, NULL);
566
567         /* TODO check if we can handle error here,
568          * we have no backward compatibility
569          */
570         if (ret)
571                 return 0;
572
573         if (tb[NL802154_ATTR_WPAN_PHY])
574                 state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]);
575         if (tb[NL802154_ATTR_WPAN_DEV])
576                 state->filter_wpan_phy = nla_get_u64(tb[NL802154_ATTR_WPAN_DEV]) >> 32;
577         if (tb[NL802154_ATTR_IFINDEX]) {
578                 struct net_device *netdev;
579                 struct cfg802154_registered_device *rdev;
580                 int ifidx = nla_get_u32(tb[NL802154_ATTR_IFINDEX]);
581
582                 netdev = __dev_get_by_index(&init_net, ifidx);
583                 if (!netdev)
584                         return -ENODEV;
585                 if (netdev->ieee802154_ptr) {
586                         rdev = wpan_phy_to_rdev(
587                                         netdev->ieee802154_ptr->wpan_phy);
588                         state->filter_wpan_phy = rdev->wpan_phy_idx;
589                 }
590         }
591
592         return 0;
593 }
594
595 static int
596 nl802154_dump_wpan_phy(struct sk_buff *skb, struct netlink_callback *cb)
597 {
598         int idx = 0, ret;
599         struct nl802154_dump_wpan_phy_state *state = (void *)cb->args[0];
600         struct cfg802154_registered_device *rdev;
601
602         rtnl_lock();
603         if (!state) {
604                 state = kzalloc(sizeof(*state), GFP_KERNEL);
605                 if (!state) {
606                         rtnl_unlock();
607                         return -ENOMEM;
608                 }
609                 state->filter_wpan_phy = -1;
610                 ret = nl802154_dump_wpan_phy_parse(skb, cb, state);
611                 if (ret) {
612                         kfree(state);
613                         rtnl_unlock();
614                         return ret;
615                 }
616                 cb->args[0] = (long)state;
617         }
618
619         list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
620                 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
621                         continue;
622                 if (++idx <= state->start)
623                         continue;
624                 if (state->filter_wpan_phy != -1 &&
625                     state->filter_wpan_phy != rdev->wpan_phy_idx)
626                         continue;
627                 /* attempt to fit multiple wpan_phy data chunks into the skb */
628                 ret = nl802154_send_wpan_phy(rdev,
629                                              NL802154_CMD_NEW_WPAN_PHY,
630                                              skb,
631                                              NETLINK_CB(cb->skb).portid,
632                                              cb->nlh->nlmsg_seq, NLM_F_MULTI);
633                 if (ret < 0) {
634                         if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
635                             !skb->len && cb->min_dump_alloc < 4096) {
636                                 cb->min_dump_alloc = 4096;
637                                 rtnl_unlock();
638                                 return 1;
639                         }
640                         idx--;
641                         break;
642                 }
643                 break;
644         }
645         rtnl_unlock();
646
647         state->start = idx;
648
649         return skb->len;
650 }
651
652 static int nl802154_dump_wpan_phy_done(struct netlink_callback *cb)
653 {
654         kfree((void *)cb->args[0]);
655         return 0;
656 }
657
658 static int nl802154_get_wpan_phy(struct sk_buff *skb, struct genl_info *info)
659 {
660         struct sk_buff *msg;
661         struct cfg802154_registered_device *rdev = info->user_ptr[0];
662
663         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
664         if (!msg)
665                 return -ENOMEM;
666
667         if (nl802154_send_wpan_phy(rdev, NL802154_CMD_NEW_WPAN_PHY, msg,
668                                    info->snd_portid, info->snd_seq, 0) < 0) {
669                 nlmsg_free(msg);
670                 return -ENOBUFS;
671         }
672
673         return genlmsg_reply(msg, info);
674 }
675
676 static inline u64 wpan_dev_id(struct wpan_dev *wpan_dev)
677 {
678         return (u64)wpan_dev->identifier |
679                ((u64)wpan_phy_to_rdev(wpan_dev->wpan_phy)->wpan_phy_idx << 32);
680 }
681
682 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
683 #include <net/ieee802154_netdev.h>
684
685 static int
686 ieee802154_llsec_send_key_id(struct sk_buff *msg,
687                              const struct ieee802154_llsec_key_id *desc)
688 {
689         struct nlattr *nl_dev_addr;
690
691         if (nla_put_u32(msg, NL802154_KEY_ID_ATTR_MODE, desc->mode))
692                 return -ENOBUFS;
693
694         switch (desc->mode) {
695         case NL802154_KEY_ID_MODE_IMPLICIT:
696                 nl_dev_addr = nla_nest_start(msg, NL802154_KEY_ID_ATTR_IMPLICIT);
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,
714                                          desc->device_addr.extended_addr,
715                                          NL802154_DEV_ADDR_ATTR_PAD))
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,
735                                  desc->extended_source,
736                                  NL802154_KEY_ID_ATTR_PAD))
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, &params);
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
771         nl_key_id = nla_nest_start(msg, NL802154_ATTR_SEC_OUT_KEY_ID);
772         if (!nl_key_id)
773                 return -ENOBUFS;
774
775         ret = ieee802154_llsec_send_key_id(msg, &params.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
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) ||
805             nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
806                               wpan_dev_id(wpan_dev), NL802154_ATTR_PAD) ||
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,
814                          wpan_dev->extended_addr,
815                          NL802154_ATTR_PAD) ||
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
834         /* ackreq default behaviour */
835         if (nla_put_u8(msg, NL802154_ATTR_ACKREQ_DEFAULT, wpan_dev->ackreq))
836                 goto nla_put_failure;
837
838 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
839         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
840                 goto out;
841
842         if (nl802154_get_llsec_params(msg, rdev, wpan_dev) < 0)
843                 goto nla_put_failure;
844
845 out:
846 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
847
848         genlmsg_end(msg, hdr);
849         return 0;
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) {
868                 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
869                         continue;
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
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;
923         __le64 extended_addr = cpu_to_le64(0x0000000000000000ULL);
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]);
934                 if (type > NL802154_IFTYPE_MAX ||
935                     !(rdev->wpan_phy.supported.iftypes & BIT(type)))
936                         return -EINVAL;
937         }
938
939         if (info->attrs[NL802154_ATTR_EXTENDED_ADDR])
940                 extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
941
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]),
947                                      NET_NAME_USER, type, extended_addr);
948 }
949
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
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 */
983         if (page > IEEE802154_MAX_PAGE || channel > IEEE802154_MAX_CHANNEL ||
984             !(rdev->wpan_phy.supported.channels[page] & BIT(channel)))
985                 return -EINVAL;
986
987         return rdev_set_channel(rdev, page, channel);
988 }
989
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
995         if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE))
996                 return -EOPNOTSUPP;
997
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 */
1003         if (cca.mode < NL802154_CCA_ENERGY ||
1004             cca.mode > NL802154_CCA_ATTR_MAX ||
1005             !(rdev->wpan_phy.supported.cca_modes & BIT(cca.mode)))
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]);
1013                 if (cca.opt > NL802154_CCA_OPT_ATTR_MAX ||
1014                     !(rdev->wpan_phy.supported.cca_opts & BIT(cca.opt)))
1015                         return -EINVAL;
1016         }
1017
1018         return rdev_set_cca_mode(rdev, &cca);
1019 }
1020
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
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
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;
1070         __le16 pan_id;
1071
1072         /* conflict here while tx/rx calls */
1073         if (netif_running(dev))
1074                 return -EBUSY;
1075
1076         if (wpan_dev->lowpan_dev) {
1077                 if (netif_running(wpan_dev->lowpan_dev))
1078                         return -EBUSY;
1079         }
1080
1081         /* don't change address fields on monitor */
1082         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1083             !info->attrs[NL802154_ATTR_PAN_ID])
1084                 return -EINVAL;
1085
1086         pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
1087
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
1098         return rdev_set_pan_id(rdev, wpan_dev, pan_id);
1099 }
1100
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;
1106         __le16 short_addr;
1107
1108         /* conflict here while tx/rx calls */
1109         if (netif_running(dev))
1110                 return -EBUSY;
1111
1112         if (wpan_dev->lowpan_dev) {
1113                 if (netif_running(wpan_dev->lowpan_dev))
1114                         return -EBUSY;
1115         }
1116
1117         /* don't change address fields on monitor */
1118         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1119             !info->attrs[NL802154_ATTR_SHORT_ADDR])
1120                 return -EINVAL;
1121
1122         short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
1123
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
1139         return rdev_set_short_addr(rdev, wpan_dev, short_addr);
1140 }
1141
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 */
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)
1167                 return -EINVAL;
1168
1169         return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
1170 }
1171
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 */
1191         if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs ||
1192             max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs)
1193                 return -EINVAL;
1194
1195         return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
1196 }
1197
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 */
1216         if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries ||
1217             max_frame_retries > rdev->wpan_phy.supported.max_frame_retries)
1218                 return -EINVAL;
1219
1220         return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
1221 }
1222
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;
1228         int mode;
1229
1230         if (netif_running(dev))
1231                 return -EBUSY;
1232
1233         if (!info->attrs[NL802154_ATTR_LBT_MODE])
1234                 return -EINVAL;
1235
1236         mode = nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
1237
1238         if (mode != 0 && mode != 1)
1239                 return -EINVAL;
1240
1241         if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt))
1242                 return -EINVAL;
1243
1244         return rdev_set_lbt_mode(rdev, wpan_dev, mode);
1245 }
1246
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;
1253         int ackreq;
1254
1255         if (netif_running(dev))
1256                 return -EBUSY;
1257
1258         if (!info->attrs[NL802154_ATTR_ACKREQ_DEFAULT])
1259                 return -EINVAL;
1260
1261         ackreq = nla_get_u8(info->attrs[NL802154_ATTR_ACKREQ_DEFAULT]);
1262
1263         if (ackreq != 0 && ackreq != 1)
1264                 return -EINVAL;
1265
1266         return rdev_set_ackreq_default(rdev, wpan_dev, ackreq);
1267 }
1268
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
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 };
1307
1308 static int
1309 ieee802154_llsec_parse_dev_addr(struct nlattr *nla,
1310                                 struct ieee802154_addr *addr)
1311 {
1312         struct nlattr *attrs[NL802154_DEV_ADDR_ATTR_MAX + 1];
1313
1314         if (!nla || nla_parse_nested(attrs, NL802154_DEV_ADDR_ATTR_MAX, nla,
1315                                      nl802154_dev_addr_policy, NULL))
1316                 return -EINVAL;
1317
1318         if (!attrs[NL802154_DEV_ADDR_ATTR_PAN_ID] || !attrs[NL802154_DEV_ADDR_ATTR_MODE])
1319                 return -EINVAL;
1320
1321         addr->pan_id = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_PAN_ID]);
1322         addr->mode = nla_get_u32(attrs[NL802154_DEV_ADDR_ATTR_MODE]);
1323         switch (addr->mode) {
1324         case NL802154_DEV_ADDR_SHORT:
1325                 if (!attrs[NL802154_DEV_ADDR_ATTR_SHORT])
1326                         return -EINVAL;
1327                 addr->short_addr = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_SHORT]);
1328                 break;
1329         case NL802154_DEV_ADDR_EXTENDED:
1330                 if (!attrs[NL802154_DEV_ADDR_ATTR_EXTENDED])
1331                         return -EINVAL;
1332                 addr->extended_addr = nla_get_le64(attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]);
1333                 break;
1334         default:
1335                 return -EINVAL;
1336         }
1337
1338         return 0;
1339 }
1340
1341 static const struct nla_policy nl802154_key_id_policy[NL802154_KEY_ID_ATTR_MAX + 1] = {
1342         [NL802154_KEY_ID_ATTR_MODE] = { .type = NLA_U32 },
1343         [NL802154_KEY_ID_ATTR_INDEX] = { .type = NLA_U8 },
1344         [NL802154_KEY_ID_ATTR_IMPLICIT] = { .type = NLA_NESTED },
1345         [NL802154_KEY_ID_ATTR_SOURCE_SHORT] = { .type = NLA_U32 },
1346         [NL802154_KEY_ID_ATTR_SOURCE_EXTENDED] = { .type = NLA_U64 },
1347 };
1348
1349 static int
1350 ieee802154_llsec_parse_key_id(struct nlattr *nla,
1351                               struct ieee802154_llsec_key_id *desc)
1352 {
1353         struct nlattr *attrs[NL802154_KEY_ID_ATTR_MAX + 1];
1354
1355         if (!nla || nla_parse_nested(attrs, NL802154_KEY_ID_ATTR_MAX, nla,
1356                                      nl802154_key_id_policy, NULL))
1357                 return -EINVAL;
1358
1359         if (!attrs[NL802154_KEY_ID_ATTR_MODE])
1360                 return -EINVAL;
1361
1362         desc->mode = nla_get_u32(attrs[NL802154_KEY_ID_ATTR_MODE]);
1363         switch (desc->mode) {
1364         case NL802154_KEY_ID_MODE_IMPLICIT:
1365                 if (!attrs[NL802154_KEY_ID_ATTR_IMPLICIT])
1366                         return -EINVAL;
1367
1368                 if (ieee802154_llsec_parse_dev_addr(attrs[NL802154_KEY_ID_ATTR_IMPLICIT],
1369                                                     &desc->device_addr) < 0)
1370                         return -EINVAL;
1371                 break;
1372         case NL802154_KEY_ID_MODE_INDEX:
1373                 break;
1374         case NL802154_KEY_ID_MODE_INDEX_SHORT:
1375                 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT])
1376                         return -EINVAL;
1377
1378                 desc->short_source = nla_get_le32(attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT]);
1379                 break;
1380         case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
1381                 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED])
1382                         return -EINVAL;
1383
1384                 desc->extended_source = nla_get_le64(attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED]);
1385                 break;
1386         default:
1387                 return -EINVAL;
1388         }
1389
1390         if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
1391                 if (!attrs[NL802154_KEY_ID_ATTR_INDEX])
1392                         return -EINVAL;
1393
1394                 /* TODO change id to idx */
1395                 desc->id = nla_get_u8(attrs[NL802154_KEY_ID_ATTR_INDEX]);
1396         }
1397
1398         return 0;
1399 }
1400
1401 static int nl802154_set_llsec_params(struct sk_buff *skb,
1402                                      struct genl_info *info)
1403 {
1404         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1405         struct net_device *dev = info->user_ptr[1];
1406         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1407         struct ieee802154_llsec_params params;
1408         u32 changed = 0;
1409         int ret;
1410
1411         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1412                 return -EOPNOTSUPP;
1413
1414         if (info->attrs[NL802154_ATTR_SEC_ENABLED]) {
1415                 u8 enabled;
1416
1417                 enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1418                 if (enabled != 0 && enabled != 1)
1419                         return -EINVAL;
1420
1421                 params.enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1422                 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
1423         }
1424
1425         if (info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID]) {
1426                 ret = ieee802154_llsec_parse_key_id(info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID],
1427                                                     &params.out_key);
1428                 if (ret < 0)
1429                         return ret;
1430
1431                 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
1432         }
1433
1434         if (info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]) {
1435                 params.out_level = nla_get_u32(info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]);
1436                 if (params.out_level > NL802154_SECLEVEL_MAX)
1437                         return -EINVAL;
1438
1439                 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
1440         }
1441
1442         if (info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]) {
1443                 params.frame_counter = nla_get_be32(info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]);
1444                 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
1445         }
1446
1447         return rdev_set_llsec_params(rdev, wpan_dev, &params, changed);
1448 }
1449
1450 static int nl802154_send_key(struct sk_buff *msg, u32 cmd, u32 portid,
1451                              u32 seq, int flags,
1452                              struct cfg802154_registered_device *rdev,
1453                              struct net_device *dev,
1454                              const struct ieee802154_llsec_key_entry *key)
1455 {
1456         void *hdr;
1457         u32 commands[NL802154_CMD_FRAME_NR_IDS / 32];
1458         struct nlattr *nl_key, *nl_key_id;
1459
1460         hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1461         if (!hdr)
1462                 return -ENOBUFS;
1463
1464         if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1465                 goto nla_put_failure;
1466
1467         nl_key = nla_nest_start(msg, NL802154_ATTR_SEC_KEY);
1468         if (!nl_key)
1469                 goto nla_put_failure;
1470
1471         nl_key_id = nla_nest_start(msg, NL802154_KEY_ATTR_ID);
1472         if (!nl_key_id)
1473                 goto nla_put_failure;
1474
1475         if (ieee802154_llsec_send_key_id(msg, &key->id) < 0)
1476                 goto nla_put_failure;
1477
1478         nla_nest_end(msg, nl_key_id);
1479
1480         if (nla_put_u8(msg, NL802154_KEY_ATTR_USAGE_FRAMES,
1481                        key->key->frame_types))
1482                 goto nla_put_failure;
1483
1484         if (key->key->frame_types & BIT(NL802154_FRAME_CMD)) {
1485                 /* TODO for each nested */
1486                 memset(commands, 0, sizeof(commands));
1487                 commands[7] = key->key->cmd_frame_ids;
1488                 if (nla_put(msg, NL802154_KEY_ATTR_USAGE_CMDS,
1489                             sizeof(commands), commands))
1490                         goto nla_put_failure;
1491         }
1492
1493         if (nla_put(msg, NL802154_KEY_ATTR_BYTES, NL802154_KEY_SIZE,
1494                     key->key->key))
1495                 goto nla_put_failure;
1496
1497         nla_nest_end(msg, nl_key);
1498         genlmsg_end(msg, hdr);
1499
1500         return 0;
1501
1502 nla_put_failure:
1503         genlmsg_cancel(msg, hdr);
1504         return -EMSGSIZE;
1505 }
1506
1507 static int
1508 nl802154_dump_llsec_key(struct sk_buff *skb, struct netlink_callback *cb)
1509 {
1510         struct cfg802154_registered_device *rdev = NULL;
1511         struct ieee802154_llsec_key_entry *key;
1512         struct ieee802154_llsec_table *table;
1513         struct wpan_dev *wpan_dev;
1514         int err;
1515
1516         err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1517         if (err)
1518                 return err;
1519
1520         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1521                 err = skb->len;
1522                 goto out_err;
1523         }
1524
1525         if (!wpan_dev->netdev) {
1526                 err = -EINVAL;
1527                 goto out_err;
1528         }
1529
1530         rdev_lock_llsec_table(rdev, wpan_dev);
1531         rdev_get_llsec_table(rdev, wpan_dev, &table);
1532
1533         /* TODO make it like station dump */
1534         if (cb->args[2])
1535                 goto out;
1536
1537         list_for_each_entry(key, &table->keys, list) {
1538                 if (nl802154_send_key(skb, NL802154_CMD_NEW_SEC_KEY,
1539                                       NETLINK_CB(cb->skb).portid,
1540                                       cb->nlh->nlmsg_seq, NLM_F_MULTI,
1541                                       rdev, wpan_dev->netdev, key) < 0) {
1542                         /* TODO */
1543                         err = -EIO;
1544                         rdev_unlock_llsec_table(rdev, wpan_dev);
1545                         goto out_err;
1546                 }
1547         }
1548
1549         cb->args[2] = 1;
1550
1551 out:
1552         rdev_unlock_llsec_table(rdev, wpan_dev);
1553         err = skb->len;
1554 out_err:
1555         nl802154_finish_wpan_dev_dump(rdev);
1556
1557         return err;
1558 }
1559
1560 static const struct nla_policy nl802154_key_policy[NL802154_KEY_ATTR_MAX + 1] = {
1561         [NL802154_KEY_ATTR_ID] = { NLA_NESTED },
1562         /* TODO handle it as for_each_nested and NLA_FLAG? */
1563         [NL802154_KEY_ATTR_USAGE_FRAMES] = { NLA_U8 },
1564         /* TODO handle it as for_each_nested, not static array? */
1565         [NL802154_KEY_ATTR_USAGE_CMDS] = { .len = NL802154_CMD_FRAME_NR_IDS / 8 },
1566         [NL802154_KEY_ATTR_BYTES] = { .len = NL802154_KEY_SIZE },
1567 };
1568
1569 static int nl802154_add_llsec_key(struct sk_buff *skb, struct genl_info *info)
1570 {
1571         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1572         struct net_device *dev = info->user_ptr[1];
1573         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1574         struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1575         struct ieee802154_llsec_key key = { };
1576         struct ieee802154_llsec_key_id id = { };
1577         u32 commands[NL802154_CMD_FRAME_NR_IDS / 32] = { };
1578
1579         if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
1580             nla_parse_nested(attrs, NL802154_KEY_ATTR_MAX,
1581                              info->attrs[NL802154_ATTR_SEC_KEY],
1582                              nl802154_key_policy, info->extack))
1583                 return -EINVAL;
1584
1585         if (!attrs[NL802154_KEY_ATTR_USAGE_FRAMES] ||
1586             !attrs[NL802154_KEY_ATTR_BYTES])
1587                 return -EINVAL;
1588
1589         if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1590                 return -ENOBUFS;
1591
1592         key.frame_types = nla_get_u8(attrs[NL802154_KEY_ATTR_USAGE_FRAMES]);
1593         if (key.frame_types > BIT(NL802154_FRAME_MAX) ||
1594             ((key.frame_types & BIT(NL802154_FRAME_CMD)) &&
1595              !attrs[NL802154_KEY_ATTR_USAGE_CMDS]))
1596                 return -EINVAL;
1597
1598         if (attrs[NL802154_KEY_ATTR_USAGE_CMDS]) {
1599                 /* TODO for each nested */
1600                 nla_memcpy(commands, attrs[NL802154_KEY_ATTR_USAGE_CMDS],
1601                            NL802154_CMD_FRAME_NR_IDS / 8);
1602
1603                 /* TODO understand the -EINVAL logic here? last condition */
1604                 if (commands[0] || commands[1] || commands[2] || commands[3] ||
1605                     commands[4] || commands[5] || commands[6] ||
1606                     commands[7] > BIT(NL802154_CMD_FRAME_MAX))
1607                         return -EINVAL;
1608
1609                 key.cmd_frame_ids = commands[7];
1610         } else {
1611                 key.cmd_frame_ids = 0;
1612         }
1613
1614         nla_memcpy(key.key, attrs[NL802154_KEY_ATTR_BYTES], NL802154_KEY_SIZE);
1615
1616         if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1617                 return -ENOBUFS;
1618
1619         return rdev_add_llsec_key(rdev, wpan_dev, &id, &key);
1620 }
1621
1622 static int nl802154_del_llsec_key(struct sk_buff *skb, struct genl_info *info)
1623 {
1624         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1625         struct net_device *dev = info->user_ptr[1];
1626         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1627         struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1628         struct ieee802154_llsec_key_id id;
1629
1630         if (!info->attrs[NL802154_ATTR_SEC_KEY] ||
1631             nla_parse_nested(attrs, NL802154_KEY_ATTR_MAX,
1632                              info->attrs[NL802154_ATTR_SEC_KEY],
1633                              nl802154_key_policy, info->extack))
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)
1653                 return -ENOBUFS;
1654
1655         if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1656                 goto nla_put_failure;
1657
1658         nl_device = nla_nest_start(msg, NL802154_ATTR_SEC_DEVICE);
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,
1668                          dev_desc->hwaddr, NL802154_DEV_ATTR_PAD) ||
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
1697         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1698                 err = skb->len;
1699                 goto out_err;
1700         }
1701
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
1752         if (!nla || nla_parse_nested(attrs, NL802154_DEV_ATTR_MAX,
1753                                      nla, nl802154_dev_policy, NULL))
1754                 return -EINVAL;
1755
1756         memset(dev, 0, sizeof(*dev));
1757
1758         if (!attrs[NL802154_DEV_ATTR_FRAME_COUNTER] ||
1759             !attrs[NL802154_DEV_ATTR_PAN_ID] ||
1760             !attrs[NL802154_DEV_ATTR_SHORT_ADDR] ||
1761             !attrs[NL802154_DEV_ATTR_EXTENDED_ADDR] ||
1762             !attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] ||
1763             !attrs[NL802154_DEV_ATTR_KEY_MODE])
1764                 return -EINVAL;
1765
1766         /* TODO be32 */
1767         dev->frame_counter = nla_get_u32(attrs[NL802154_DEV_ATTR_FRAME_COUNTER]);
1768         dev->pan_id = nla_get_le16(attrs[NL802154_DEV_ATTR_PAN_ID]);
1769         dev->short_addr = nla_get_le16(attrs[NL802154_DEV_ATTR_SHORT_ADDR]);
1770         /* TODO rename hwaddr to extended_addr */
1771         dev->hwaddr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
1772         dev->seclevel_exempt = nla_get_u8(attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT]);
1773         dev->key_mode = nla_get_u32(attrs[NL802154_DEV_ATTR_KEY_MODE]);
1774
1775         if (dev->key_mode > NL802154_DEVKEY_MAX ||
1776             (dev->seclevel_exempt != 0 && dev->seclevel_exempt != 1))
1777                 return -EINVAL;
1778
1779         return 0;
1780 }
1781
1782 static int nl802154_add_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1783 {
1784         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1785         struct net_device *dev = info->user_ptr[1];
1786         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1787         struct ieee802154_llsec_device dev_desc;
1788
1789         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1790                 return -EOPNOTSUPP;
1791
1792         if (ieee802154_llsec_parse_device(info->attrs[NL802154_ATTR_SEC_DEVICE],
1793                                           &dev_desc) < 0)
1794                 return -EINVAL;
1795
1796         return rdev_add_device(rdev, wpan_dev, &dev_desc);
1797 }
1798
1799 static int nl802154_del_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1800 {
1801         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1802         struct net_device *dev = info->user_ptr[1];
1803         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1804         struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
1805         __le64 extended_addr;
1806
1807         if (!info->attrs[NL802154_ATTR_SEC_DEVICE] ||
1808             nla_parse_nested(attrs, NL802154_DEV_ATTR_MAX,
1809                              info->attrs[NL802154_ATTR_SEC_DEVICE],
1810                              nl802154_dev_policy, info->extack))
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)
1831                 return -ENOBUFS;
1832
1833         if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1834                 goto nla_put_failure;
1835
1836         nl_devkey = nla_nest_start(msg, NL802154_ATTR_SEC_DEVKEY);
1837         if (!nl_devkey)
1838                 goto nla_put_failure;
1839
1840         if (nla_put_le64(msg, NL802154_DEVKEY_ATTR_EXTENDED_ADDR,
1841                          extended_addr, NL802154_DEVKEY_ATTR_PAD) ||
1842             nla_put_u32(msg, NL802154_DEVKEY_ATTR_FRAME_COUNTER,
1843                         devkey->frame_counter))
1844                 goto nla_put_failure;
1845
1846         nl_key_id = nla_nest_start(msg, NL802154_DEVKEY_ATTR_ID);
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
1878         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
1879                 err = skb->len;
1880                 goto out_err;
1881         }
1882
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
1940         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
1941                 return -EOPNOTSUPP;
1942
1943         if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
1944             nla_parse_nested(attrs, NL802154_DEVKEY_ATTR_MAX,
1945                              info->attrs[NL802154_ATTR_SEC_DEVKEY],
1946                              nl802154_devkey_policy, info->extack) < 0)
1947                 return -EINVAL;
1948
1949         if (!attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER] ||
1950             !attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1951                 return -EINVAL;
1952
1953         /* TODO change key.id ? */
1954         if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1955                                           &key.key_id) < 0)
1956                 return -ENOBUFS;
1957
1958         /* TODO be32 */
1959         key.frame_counter = nla_get_u32(attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER]);
1960         /* TODO change naming hwaddr -> extended_addr
1961          * check unique identifier short+pan OR extended_addr
1962          */
1963         extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1964         return rdev_add_devkey(rdev, wpan_dev, extended_addr, &key);
1965 }
1966
1967 static int nl802154_del_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
1968 {
1969         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1970         struct net_device *dev = info->user_ptr[1];
1971         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1972         struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
1973         struct ieee802154_llsec_device_key key;
1974         __le64 extended_addr;
1975
1976         if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
1977             nla_parse_nested(attrs, NL802154_DEVKEY_ATTR_MAX,
1978                              info->attrs[NL802154_ATTR_SEC_DEVKEY],
1979                              nl802154_devkey_policy, info->extack))
1980                 return -EINVAL;
1981
1982         if (!attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1983                 return -EINVAL;
1984
1985         /* TODO change key.id ? */
1986         if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1987                                           &key.key_id) < 0)
1988                 return -ENOBUFS;
1989
1990         /* TODO change naming hwaddr -> extended_addr
1991          * check unique identifier short+pan OR extended_addr
1992          */
1993         extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1994         return rdev_del_devkey(rdev, wpan_dev, extended_addr, &key);
1995 }
1996
1997 static int nl802154_send_seclevel(struct sk_buff *msg, u32 cmd, u32 portid,
1998                                   u32 seq, int flags,
1999                                   struct cfg802154_registered_device *rdev,
2000                                   struct net_device *dev,
2001                                   const struct ieee802154_llsec_seclevel *sl)
2002 {
2003         void *hdr;
2004         struct nlattr *nl_seclevel;
2005
2006         hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
2007         if (!hdr)
2008                 return -ENOBUFS;
2009
2010         if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
2011                 goto nla_put_failure;
2012
2013         nl_seclevel = nla_nest_start(msg, NL802154_ATTR_SEC_LEVEL);
2014         if (!nl_seclevel)
2015                 goto nla_put_failure;
2016
2017         if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_FRAME, sl->frame_type) ||
2018             nla_put_u32(msg, NL802154_SECLEVEL_ATTR_LEVELS, sl->sec_levels) ||
2019             nla_put_u8(msg, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE,
2020                        sl->device_override))
2021                 goto nla_put_failure;
2022
2023         if (sl->frame_type == NL802154_FRAME_CMD) {
2024                 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_CMD_FRAME,
2025                                 sl->cmd_frame_id))
2026                         goto nla_put_failure;
2027         }
2028
2029         nla_nest_end(msg, nl_seclevel);
2030         genlmsg_end(msg, hdr);
2031
2032         return 0;
2033
2034 nla_put_failure:
2035         genlmsg_cancel(msg, hdr);
2036         return -EMSGSIZE;
2037 }
2038
2039 static int
2040 nl802154_dump_llsec_seclevel(struct sk_buff *skb, struct netlink_callback *cb)
2041 {
2042         struct cfg802154_registered_device *rdev = NULL;
2043         struct ieee802154_llsec_seclevel *sl;
2044         struct ieee802154_llsec_table *table;
2045         struct wpan_dev *wpan_dev;
2046         int err;
2047
2048         err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2049         if (err)
2050                 return err;
2051
2052         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR) {
2053                 err = skb->len;
2054                 goto out_err;
2055         }
2056
2057         if (!wpan_dev->netdev) {
2058                 err = -EINVAL;
2059                 goto out_err;
2060         }
2061
2062         rdev_lock_llsec_table(rdev, wpan_dev);
2063         rdev_get_llsec_table(rdev, wpan_dev, &table);
2064
2065         /* TODO make it like station dump */
2066         if (cb->args[2])
2067                 goto out;
2068
2069         list_for_each_entry(sl, &table->security_levels, list) {
2070                 if (nl802154_send_seclevel(skb, NL802154_CMD_NEW_SEC_LEVEL,
2071                                            NETLINK_CB(cb->skb).portid,
2072                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
2073                                            rdev, wpan_dev->netdev, sl) < 0) {
2074                         /* TODO */
2075                         err = -EIO;
2076                         rdev_unlock_llsec_table(rdev, wpan_dev);
2077                         goto out_err;
2078                 }
2079         }
2080
2081         cb->args[2] = 1;
2082
2083 out:
2084         rdev_unlock_llsec_table(rdev, wpan_dev);
2085         err = skb->len;
2086 out_err:
2087         nl802154_finish_wpan_dev_dump(rdev);
2088
2089         return err;
2090 }
2091
2092 static const struct nla_policy nl802154_seclevel_policy[NL802154_SECLEVEL_ATTR_MAX + 1] = {
2093         [NL802154_SECLEVEL_ATTR_LEVELS] = { .type = NLA_U8 },
2094         [NL802154_SECLEVEL_ATTR_FRAME] = { .type = NLA_U32 },
2095         [NL802154_SECLEVEL_ATTR_CMD_FRAME] = { .type = NLA_U32 },
2096         [NL802154_SECLEVEL_ATTR_DEV_OVERRIDE] = { .type = NLA_U8 },
2097 };
2098
2099 static int
2100 llsec_parse_seclevel(struct nlattr *nla, struct ieee802154_llsec_seclevel *sl)
2101 {
2102         struct nlattr *attrs[NL802154_SECLEVEL_ATTR_MAX + 1];
2103
2104         if (!nla || nla_parse_nested(attrs, NL802154_SECLEVEL_ATTR_MAX,
2105                                      nla, nl802154_seclevel_policy, NULL))
2106                 return -EINVAL;
2107
2108         memset(sl, 0, sizeof(*sl));
2109
2110         if (!attrs[NL802154_SECLEVEL_ATTR_LEVELS] ||
2111             !attrs[NL802154_SECLEVEL_ATTR_FRAME] ||
2112             !attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE])
2113                 return -EINVAL;
2114
2115         sl->sec_levels = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_LEVELS]);
2116         sl->frame_type = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_FRAME]);
2117         sl->device_override = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE]);
2118         if (sl->frame_type > NL802154_FRAME_MAX ||
2119             (sl->device_override != 0 && sl->device_override != 1))
2120                 return -EINVAL;
2121
2122         if (sl->frame_type == NL802154_FRAME_CMD) {
2123                 if (!attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME])
2124                         return -EINVAL;
2125
2126                 sl->cmd_frame_id = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME]);
2127                 if (sl->cmd_frame_id > NL802154_CMD_FRAME_MAX)
2128                         return -EINVAL;
2129         }
2130
2131         return 0;
2132 }
2133
2134 static int nl802154_add_llsec_seclevel(struct sk_buff *skb,
2135                                        struct genl_info *info)
2136 {
2137         struct cfg802154_registered_device *rdev = info->user_ptr[0];
2138         struct net_device *dev = info->user_ptr[1];
2139         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2140         struct ieee802154_llsec_seclevel sl;
2141
2142         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2143                 return -EOPNOTSUPP;
2144
2145         if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2146                                  &sl) < 0)
2147                 return -EINVAL;
2148
2149         return rdev_add_seclevel(rdev, wpan_dev, &sl);
2150 }
2151
2152 static int nl802154_del_llsec_seclevel(struct sk_buff *skb,
2153                                        struct genl_info *info)
2154 {
2155         struct cfg802154_registered_device *rdev = info->user_ptr[0];
2156         struct net_device *dev = info->user_ptr[1];
2157         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2158         struct ieee802154_llsec_seclevel sl;
2159
2160         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
2161                 return -EOPNOTSUPP;
2162
2163         if (!info->attrs[NL802154_ATTR_SEC_LEVEL] ||
2164             llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2165                                  &sl) < 0)
2166                 return -EINVAL;
2167
2168         return rdev_del_seclevel(rdev, wpan_dev, &sl);
2169 }
2170 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2171
2172 #define NL802154_FLAG_NEED_WPAN_PHY     0x01
2173 #define NL802154_FLAG_NEED_NETDEV       0x02
2174 #define NL802154_FLAG_NEED_RTNL         0x04
2175 #define NL802154_FLAG_CHECK_NETDEV_UP   0x08
2176 #define NL802154_FLAG_NEED_NETDEV_UP    (NL802154_FLAG_NEED_NETDEV |\
2177                                          NL802154_FLAG_CHECK_NETDEV_UP)
2178 #define NL802154_FLAG_NEED_WPAN_DEV     0x10
2179 #define NL802154_FLAG_NEED_WPAN_DEV_UP  (NL802154_FLAG_NEED_WPAN_DEV |\
2180                                          NL802154_FLAG_CHECK_NETDEV_UP)
2181
2182 static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
2183                              struct genl_info *info)
2184 {
2185         struct cfg802154_registered_device *rdev;
2186         struct wpan_dev *wpan_dev;
2187         struct net_device *dev;
2188         bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
2189
2190         if (rtnl)
2191                 rtnl_lock();
2192
2193         if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
2194                 rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
2195                 if (IS_ERR(rdev)) {
2196                         if (rtnl)
2197                                 rtnl_unlock();
2198                         return PTR_ERR(rdev);
2199                 }
2200                 info->user_ptr[0] = rdev;
2201         } else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
2202                    ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2203                 ASSERT_RTNL();
2204                 wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
2205                                                            info->attrs);
2206                 if (IS_ERR(wpan_dev)) {
2207                         if (rtnl)
2208                                 rtnl_unlock();
2209                         return PTR_ERR(wpan_dev);
2210                 }
2211
2212                 dev = wpan_dev->netdev;
2213                 rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
2214
2215                 if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
2216                         if (!dev) {
2217                                 if (rtnl)
2218                                         rtnl_unlock();
2219                                 return -EINVAL;
2220                         }
2221
2222                         info->user_ptr[1] = dev;
2223                 } else {
2224                         info->user_ptr[1] = wpan_dev;
2225                 }
2226
2227                 if (dev) {
2228                         if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
2229                             !netif_running(dev)) {
2230                                 if (rtnl)
2231                                         rtnl_unlock();
2232                                 return -ENETDOWN;
2233                         }
2234
2235                         dev_hold(dev);
2236                 }
2237
2238                 info->user_ptr[0] = rdev;
2239         }
2240
2241         return 0;
2242 }
2243
2244 static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
2245                                struct genl_info *info)
2246 {
2247         if (info->user_ptr[1]) {
2248                 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2249                         struct wpan_dev *wpan_dev = info->user_ptr[1];
2250
2251                         if (wpan_dev->netdev)
2252                                 dev_put(wpan_dev->netdev);
2253                 } else {
2254                         dev_put(info->user_ptr[1]);
2255                 }
2256         }
2257
2258         if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
2259                 rtnl_unlock();
2260 }
2261
2262 static const struct genl_ops nl802154_ops[] = {
2263         {
2264                 .cmd = NL802154_CMD_GET_WPAN_PHY,
2265                 .doit = nl802154_get_wpan_phy,
2266                 .dumpit = nl802154_dump_wpan_phy,
2267                 .done = nl802154_dump_wpan_phy_done,
2268                 .policy = nl802154_policy,
2269                 /* can be retrieved by unprivileged users */
2270                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2271                                   NL802154_FLAG_NEED_RTNL,
2272         },
2273         {
2274                 .cmd = NL802154_CMD_GET_INTERFACE,
2275                 .doit = nl802154_get_interface,
2276                 .dumpit = nl802154_dump_interface,
2277                 .policy = nl802154_policy,
2278                 /* can be retrieved by unprivileged users */
2279                 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2280                                   NL802154_FLAG_NEED_RTNL,
2281         },
2282         {
2283                 .cmd = NL802154_CMD_NEW_INTERFACE,
2284                 .doit = nl802154_new_interface,
2285                 .policy = nl802154_policy,
2286                 .flags = GENL_ADMIN_PERM,
2287                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2288                                   NL802154_FLAG_NEED_RTNL,
2289         },
2290         {
2291                 .cmd = NL802154_CMD_DEL_INTERFACE,
2292                 .doit = nl802154_del_interface,
2293                 .policy = nl802154_policy,
2294                 .flags = GENL_ADMIN_PERM,
2295                 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2296                                   NL802154_FLAG_NEED_RTNL,
2297         },
2298         {
2299                 .cmd = NL802154_CMD_SET_CHANNEL,
2300                 .doit = nl802154_set_channel,
2301                 .policy = nl802154_policy,
2302                 .flags = GENL_ADMIN_PERM,
2303                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2304                                   NL802154_FLAG_NEED_RTNL,
2305         },
2306         {
2307                 .cmd = NL802154_CMD_SET_CCA_MODE,
2308                 .doit = nl802154_set_cca_mode,
2309                 .policy = nl802154_policy,
2310                 .flags = GENL_ADMIN_PERM,
2311                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2312                                   NL802154_FLAG_NEED_RTNL,
2313         },
2314         {
2315                 .cmd = NL802154_CMD_SET_CCA_ED_LEVEL,
2316                 .doit = nl802154_set_cca_ed_level,
2317                 .policy = nl802154_policy,
2318                 .flags = GENL_ADMIN_PERM,
2319                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2320                                   NL802154_FLAG_NEED_RTNL,
2321         },
2322         {
2323                 .cmd = NL802154_CMD_SET_TX_POWER,
2324                 .doit = nl802154_set_tx_power,
2325                 .policy = nl802154_policy,
2326                 .flags = GENL_ADMIN_PERM,
2327                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2328                                   NL802154_FLAG_NEED_RTNL,
2329         },
2330         {
2331                 .cmd = NL802154_CMD_SET_WPAN_PHY_NETNS,
2332                 .doit = nl802154_wpan_phy_netns,
2333                 .policy = nl802154_policy,
2334                 .flags = GENL_ADMIN_PERM,
2335                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2336                                   NL802154_FLAG_NEED_RTNL,
2337         },
2338         {
2339                 .cmd = NL802154_CMD_SET_PAN_ID,
2340                 .doit = nl802154_set_pan_id,
2341                 .policy = nl802154_policy,
2342                 .flags = GENL_ADMIN_PERM,
2343                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2344                                   NL802154_FLAG_NEED_RTNL,
2345         },
2346         {
2347                 .cmd = NL802154_CMD_SET_SHORT_ADDR,
2348                 .doit = nl802154_set_short_addr,
2349                 .policy = nl802154_policy,
2350                 .flags = GENL_ADMIN_PERM,
2351                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2352                                   NL802154_FLAG_NEED_RTNL,
2353         },
2354         {
2355                 .cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
2356                 .doit = nl802154_set_backoff_exponent,
2357                 .policy = nl802154_policy,
2358                 .flags = GENL_ADMIN_PERM,
2359                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2360                                   NL802154_FLAG_NEED_RTNL,
2361         },
2362         {
2363                 .cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
2364                 .doit = nl802154_set_max_csma_backoffs,
2365                 .policy = nl802154_policy,
2366                 .flags = GENL_ADMIN_PERM,
2367                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2368                                   NL802154_FLAG_NEED_RTNL,
2369         },
2370         {
2371                 .cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
2372                 .doit = nl802154_set_max_frame_retries,
2373                 .policy = nl802154_policy,
2374                 .flags = GENL_ADMIN_PERM,
2375                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2376                                   NL802154_FLAG_NEED_RTNL,
2377         },
2378         {
2379                 .cmd = NL802154_CMD_SET_LBT_MODE,
2380                 .doit = nl802154_set_lbt_mode,
2381                 .policy = nl802154_policy,
2382                 .flags = GENL_ADMIN_PERM,
2383                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2384                                   NL802154_FLAG_NEED_RTNL,
2385         },
2386         {
2387                 .cmd = NL802154_CMD_SET_ACKREQ_DEFAULT,
2388                 .doit = nl802154_set_ackreq_default,
2389                 .policy = nl802154_policy,
2390                 .flags = GENL_ADMIN_PERM,
2391                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2392                                   NL802154_FLAG_NEED_RTNL,
2393         },
2394 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
2395         {
2396                 .cmd = NL802154_CMD_SET_SEC_PARAMS,
2397                 .doit = nl802154_set_llsec_params,
2398                 .policy = nl802154_policy,
2399                 .flags = GENL_ADMIN_PERM,
2400                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2401                                   NL802154_FLAG_NEED_RTNL,
2402         },
2403         {
2404                 .cmd = NL802154_CMD_GET_SEC_KEY,
2405                 /* TODO .doit by matching key id? */
2406                 .dumpit = nl802154_dump_llsec_key,
2407                 .policy = nl802154_policy,
2408                 .flags = GENL_ADMIN_PERM,
2409                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2410                                   NL802154_FLAG_NEED_RTNL,
2411         },
2412         {
2413                 .cmd = NL802154_CMD_NEW_SEC_KEY,
2414                 .doit = nl802154_add_llsec_key,
2415                 .policy = nl802154_policy,
2416                 .flags = GENL_ADMIN_PERM,
2417                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2418                                   NL802154_FLAG_NEED_RTNL,
2419         },
2420         {
2421                 .cmd = NL802154_CMD_DEL_SEC_KEY,
2422                 .doit = nl802154_del_llsec_key,
2423                 .policy = nl802154_policy,
2424                 .flags = GENL_ADMIN_PERM,
2425                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2426                                   NL802154_FLAG_NEED_RTNL,
2427         },
2428         /* TODO unique identifier must short+pan OR extended_addr */
2429         {
2430                 .cmd = NL802154_CMD_GET_SEC_DEV,
2431                 /* TODO .doit by matching extended_addr? */
2432                 .dumpit = nl802154_dump_llsec_dev,
2433                 .policy = nl802154_policy,
2434                 .flags = GENL_ADMIN_PERM,
2435                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2436                                   NL802154_FLAG_NEED_RTNL,
2437         },
2438         {
2439                 .cmd = NL802154_CMD_NEW_SEC_DEV,
2440                 .doit = nl802154_add_llsec_dev,
2441                 .policy = nl802154_policy,
2442                 .flags = GENL_ADMIN_PERM,
2443                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2444                                   NL802154_FLAG_NEED_RTNL,
2445         },
2446         {
2447                 .cmd = NL802154_CMD_DEL_SEC_DEV,
2448                 .doit = nl802154_del_llsec_dev,
2449                 .policy = nl802154_policy,
2450                 .flags = GENL_ADMIN_PERM,
2451                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2452                                   NL802154_FLAG_NEED_RTNL,
2453         },
2454         /* TODO remove complete devkey, put it as nested? */
2455         {
2456                 .cmd = NL802154_CMD_GET_SEC_DEVKEY,
2457                 /* TODO doit by matching ??? */
2458                 .dumpit = nl802154_dump_llsec_devkey,
2459                 .policy = nl802154_policy,
2460                 .flags = GENL_ADMIN_PERM,
2461                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2462                                   NL802154_FLAG_NEED_RTNL,
2463         },
2464         {
2465                 .cmd = NL802154_CMD_NEW_SEC_DEVKEY,
2466                 .doit = nl802154_add_llsec_devkey,
2467                 .policy = nl802154_policy,
2468                 .flags = GENL_ADMIN_PERM,
2469                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2470                                   NL802154_FLAG_NEED_RTNL,
2471         },
2472         {
2473                 .cmd = NL802154_CMD_DEL_SEC_DEVKEY,
2474                 .doit = nl802154_del_llsec_devkey,
2475                 .policy = nl802154_policy,
2476                 .flags = GENL_ADMIN_PERM,
2477                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2478                                   NL802154_FLAG_NEED_RTNL,
2479         },
2480         {
2481                 .cmd = NL802154_CMD_GET_SEC_LEVEL,
2482                 /* TODO .doit by matching frame_type? */
2483                 .dumpit = nl802154_dump_llsec_seclevel,
2484                 .policy = nl802154_policy,
2485                 .flags = GENL_ADMIN_PERM,
2486                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2487                                   NL802154_FLAG_NEED_RTNL,
2488         },
2489         {
2490                 .cmd = NL802154_CMD_NEW_SEC_LEVEL,
2491                 .doit = nl802154_add_llsec_seclevel,
2492                 .policy = nl802154_policy,
2493                 .flags = GENL_ADMIN_PERM,
2494                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2495                                   NL802154_FLAG_NEED_RTNL,
2496         },
2497         {
2498                 .cmd = NL802154_CMD_DEL_SEC_LEVEL,
2499                 /* TODO match frame_type only? */
2500                 .doit = nl802154_del_llsec_seclevel,
2501                 .policy = nl802154_policy,
2502                 .flags = GENL_ADMIN_PERM,
2503                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2504                                   NL802154_FLAG_NEED_RTNL,
2505         },
2506 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2507 };
2508
2509 static struct genl_family nl802154_fam __ro_after_init = {
2510         .name = NL802154_GENL_NAME,     /* have users key off the name instead */
2511         .hdrsize = 0,                   /* no private header */
2512         .version = 1,                   /* no particular meaning now */
2513         .maxattr = NL802154_ATTR_MAX,
2514         .netnsok = true,
2515         .pre_doit = nl802154_pre_doit,
2516         .post_doit = nl802154_post_doit,
2517         .module = THIS_MODULE,
2518         .ops = nl802154_ops,
2519         .n_ops = ARRAY_SIZE(nl802154_ops),
2520         .mcgrps = nl802154_mcgrps,
2521         .n_mcgrps = ARRAY_SIZE(nl802154_mcgrps),
2522 };
2523
2524 /* initialisation/exit functions */
2525 int __init nl802154_init(void)
2526 {
2527         return genl_register_family(&nl802154_fam);
2528 }
2529
2530 void nl802154_exit(void)
2531 {
2532         genl_unregister_family(&nl802154_fam);
2533 }