GNU Linux-libre 4.9.309-gnu1
[releases.git] / net / ieee802154 / nl-mac.c
1 /*
2  * Netlink interface for IEEE 802.15.4 stack
3  *
4  * Copyright 2007, 2008 Siemens AG
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * Written by:
16  * Sergey Lapin <slapin@ossfans.org>
17  * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
18  * Maxim Osipov <maxim.osipov@siemens.com>
19  */
20
21 #include <linux/gfp.h>
22 #include <linux/kernel.h>
23 #include <linux/if_arp.h>
24 #include <linux/netdevice.h>
25 #include <linux/ieee802154.h>
26 #include <net/netlink.h>
27 #include <net/genetlink.h>
28 #include <net/sock.h>
29 #include <linux/nl802154.h>
30 #include <linux/export.h>
31 #include <net/af_ieee802154.h>
32 #include <net/ieee802154_netdev.h>
33 #include <net/cfg802154.h>
34
35 #include "ieee802154.h"
36
37 static int nla_put_hwaddr(struct sk_buff *msg, int type, __le64 hwaddr,
38                           int padattr)
39 {
40         return nla_put_u64_64bit(msg, type, swab64((__force u64)hwaddr),
41                                  padattr);
42 }
43
44 static __le64 nla_get_hwaddr(const struct nlattr *nla)
45 {
46         return ieee802154_devaddr_from_raw(nla_data(nla));
47 }
48
49 static int nla_put_shortaddr(struct sk_buff *msg, int type, __le16 addr)
50 {
51         return nla_put_u16(msg, type, le16_to_cpu(addr));
52 }
53
54 static __le16 nla_get_shortaddr(const struct nlattr *nla)
55 {
56         return cpu_to_le16(nla_get_u16(nla));
57 }
58
59 static int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
60 {
61         struct sk_buff *msg;
62
63         pr_debug("%s\n", __func__);
64
65         msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
66         if (!msg)
67                 return -ENOBUFS;
68
69         if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
70             nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
71             nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
72                     dev->dev_addr) ||
73             nla_put_u8(msg, IEEE802154_ATTR_STATUS, status))
74                 goto nla_put_failure;
75         return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
76
77 nla_put_failure:
78         nlmsg_free(msg);
79         return -ENOBUFS;
80 }
81
82 static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid,
83                                     u32 seq, int flags, struct net_device *dev)
84 {
85         void *hdr;
86         struct wpan_phy *phy;
87         struct ieee802154_mlme_ops *ops;
88         __le16 short_addr, pan_id;
89
90         pr_debug("%s\n", __func__);
91
92         hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
93                           IEEE802154_LIST_IFACE);
94         if (!hdr)
95                 goto out;
96
97         ops = ieee802154_mlme_ops(dev);
98         phy = dev->ieee802154_ptr->wpan_phy;
99         BUG_ON(!phy);
100         get_device(&phy->dev);
101
102         rtnl_lock();
103         short_addr = dev->ieee802154_ptr->short_addr;
104         pan_id = dev->ieee802154_ptr->pan_id;
105         rtnl_unlock();
106
107         if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
108             nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
109             nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
110             nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
111                     dev->dev_addr) ||
112             nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) ||
113             nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, pan_id))
114                 goto nla_put_failure;
115
116         if (ops->get_mac_params) {
117                 struct ieee802154_mac_params params;
118
119                 rtnl_lock();
120                 ops->get_mac_params(dev, &params);
121                 rtnl_unlock();
122
123                 if (nla_put_s8(msg, IEEE802154_ATTR_TXPOWER,
124                                params.transmit_power / 100) ||
125                     nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, params.lbt) ||
126                     nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE,
127                                params.cca.mode) ||
128                     nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL,
129                                 params.cca_ed_level / 100) ||
130                     nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES,
131                                params.csma_retries) ||
132                     nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE,
133                                params.min_be) ||
134                     nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE,
135                                params.max_be) ||
136                     nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES,
137                                params.frame_retries))
138                         goto nla_put_failure;
139         }
140
141         wpan_phy_put(phy);
142         genlmsg_end(msg, hdr);
143         return 0;
144
145 nla_put_failure:
146         wpan_phy_put(phy);
147         genlmsg_cancel(msg, hdr);
148 out:
149         return -EMSGSIZE;
150 }
151
152 /* Requests from userspace */
153 static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
154 {
155         struct net_device *dev;
156
157         if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
158                 char name[IFNAMSIZ + 1];
159
160                 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
161                             sizeof(name));
162                 dev = dev_get_by_name(&init_net, name);
163         } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX]) {
164                 dev = dev_get_by_index(&init_net,
165                         nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
166         } else {
167                 return NULL;
168         }
169
170         if (!dev)
171                 return NULL;
172
173         if (dev->type != ARPHRD_IEEE802154) {
174                 dev_put(dev);
175                 return NULL;
176         }
177
178         return dev;
179 }
180
181 int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info)
182 {
183         struct net_device *dev;
184         struct ieee802154_addr addr;
185         u8 page;
186         int ret = -EOPNOTSUPP;
187
188         if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
189             !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
190             (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
191                 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
192             !info->attrs[IEEE802154_ATTR_CAPABILITY])
193                 return -EINVAL;
194
195         dev = ieee802154_nl_get_dev(info);
196         if (!dev)
197                 return -ENODEV;
198         if (!ieee802154_mlme_ops(dev)->assoc_req)
199                 goto out;
200
201         if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
202                 addr.mode = IEEE802154_ADDR_LONG;
203                 addr.extended_addr = nla_get_hwaddr(
204                                 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]);
205         } else {
206                 addr.mode = IEEE802154_ADDR_SHORT;
207                 addr.short_addr = nla_get_shortaddr(
208                                 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
209         }
210         addr.pan_id = nla_get_shortaddr(
211                         info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
212
213         if (info->attrs[IEEE802154_ATTR_PAGE])
214                 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
215         else
216                 page = 0;
217
218         ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
219                         nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
220                         page,
221                         nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
222
223 out:
224         dev_put(dev);
225         return ret;
226 }
227
228 int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info)
229 {
230         struct net_device *dev;
231         struct ieee802154_addr addr;
232         int ret = -EOPNOTSUPP;
233
234         if (!info->attrs[IEEE802154_ATTR_STATUS] ||
235             !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
236             !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
237                 return -EINVAL;
238
239         dev = ieee802154_nl_get_dev(info);
240         if (!dev)
241                 return -ENODEV;
242         if (!ieee802154_mlme_ops(dev)->assoc_resp)
243                 goto out;
244
245         addr.mode = IEEE802154_ADDR_LONG;
246         addr.extended_addr = nla_get_hwaddr(
247                         info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
248         rtnl_lock();
249         addr.pan_id = dev->ieee802154_ptr->pan_id;
250         rtnl_unlock();
251
252         ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
253                 nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
254                 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
255
256 out:
257         dev_put(dev);
258         return ret;
259 }
260
261 int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info)
262 {
263         struct net_device *dev;
264         struct ieee802154_addr addr;
265         int ret = -EOPNOTSUPP;
266
267         if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
268             !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
269             !info->attrs[IEEE802154_ATTR_REASON])
270                 return -EINVAL;
271
272         dev = ieee802154_nl_get_dev(info);
273         if (!dev)
274                 return -ENODEV;
275         if (!ieee802154_mlme_ops(dev)->disassoc_req)
276                 goto out;
277
278         if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
279                 addr.mode = IEEE802154_ADDR_LONG;
280                 addr.extended_addr = nla_get_hwaddr(
281                                 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
282         } else {
283                 addr.mode = IEEE802154_ADDR_SHORT;
284                 addr.short_addr = nla_get_shortaddr(
285                                 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
286         }
287         rtnl_lock();
288         addr.pan_id = dev->ieee802154_ptr->pan_id;
289         rtnl_unlock();
290
291         ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
292                         nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
293
294 out:
295         dev_put(dev);
296         return ret;
297 }
298
299 /* PANid, channel, beacon_order = 15, superframe_order = 15,
300  * PAN_coordinator, battery_life_extension = 0,
301  * coord_realignment = 0, security_enable = 0
302 */
303 int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
304 {
305         struct net_device *dev;
306         struct ieee802154_addr addr;
307
308         u8 channel, bcn_ord, sf_ord;
309         u8 page;
310         int pan_coord, blx, coord_realign;
311         int ret = -EBUSY;
312
313         if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
314             !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
315             !info->attrs[IEEE802154_ATTR_CHANNEL] ||
316             !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
317             !info->attrs[IEEE802154_ATTR_SF_ORD] ||
318             !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
319             !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
320             !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
321          )
322                 return -EINVAL;
323
324         dev = ieee802154_nl_get_dev(info);
325         if (!dev)
326                 return -ENODEV;
327
328         if (netif_running(dev))
329                 goto out;
330
331         if (!ieee802154_mlme_ops(dev)->start_req) {
332                 ret = -EOPNOTSUPP;
333                 goto out;
334         }
335
336         addr.mode = IEEE802154_ADDR_SHORT;
337         addr.short_addr = nla_get_shortaddr(
338                         info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
339         addr.pan_id = nla_get_shortaddr(
340                         info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
341
342         channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
343         bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
344         sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
345         pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
346         blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
347         coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
348
349         if (info->attrs[IEEE802154_ATTR_PAGE])
350                 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
351         else
352                 page = 0;
353
354         if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
355                 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
356                 dev_put(dev);
357                 return -EINVAL;
358         }
359
360         rtnl_lock();
361         ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
362                 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
363         rtnl_unlock();
364
365         /* FIXME: add validation for unused parameters to be sane
366          * for SoftMAC
367          */
368         ieee802154_nl_start_confirm(dev, IEEE802154_SUCCESS);
369
370 out:
371         dev_put(dev);
372         return ret;
373 }
374
375 int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
376 {
377         struct net_device *dev;
378         int ret = -EOPNOTSUPP;
379         u8 type;
380         u32 channels;
381         u8 duration;
382         u8 page;
383
384         if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
385             !info->attrs[IEEE802154_ATTR_CHANNELS] ||
386             !info->attrs[IEEE802154_ATTR_DURATION])
387                 return -EINVAL;
388
389         dev = ieee802154_nl_get_dev(info);
390         if (!dev)
391                 return -ENODEV;
392         if (!ieee802154_mlme_ops(dev)->scan_req)
393                 goto out;
394
395         type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
396         channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
397         duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
398
399         if (info->attrs[IEEE802154_ATTR_PAGE])
400                 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
401         else
402                 page = 0;
403
404         ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
405                                                  page, duration);
406
407 out:
408         dev_put(dev);
409         return ret;
410 }
411
412 int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info)
413 {
414         /* Request for interface name, index, type, IEEE address,
415          * PAN Id, short address
416          */
417         struct sk_buff *msg;
418         struct net_device *dev = NULL;
419         int rc = -ENOBUFS;
420
421         pr_debug("%s\n", __func__);
422
423         dev = ieee802154_nl_get_dev(info);
424         if (!dev)
425                 return -ENODEV;
426
427         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
428         if (!msg)
429                 goto out_dev;
430
431         rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq,
432                                       0, dev);
433         if (rc < 0)
434                 goto out_free;
435
436         dev_put(dev);
437
438         return genlmsg_reply(msg, info);
439 out_free:
440         nlmsg_free(msg);
441 out_dev:
442         dev_put(dev);
443         return rc;
444 }
445
446 int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb)
447 {
448         struct net *net = sock_net(skb->sk);
449         struct net_device *dev;
450         int idx;
451         int s_idx = cb->args[0];
452
453         pr_debug("%s\n", __func__);
454
455         idx = 0;
456         for_each_netdev(net, dev) {
457                 if (idx < s_idx || dev->type != ARPHRD_IEEE802154)
458                         goto cont;
459
460                 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid,
461                                              cb->nlh->nlmsg_seq,
462                                              NLM_F_MULTI, dev) < 0)
463                         break;
464 cont:
465                 idx++;
466         }
467         cb->args[0] = idx;
468
469         return skb->len;
470 }
471
472 int ieee802154_set_macparams(struct sk_buff *skb, struct genl_info *info)
473 {
474         struct net_device *dev = NULL;
475         struct ieee802154_mlme_ops *ops;
476         struct ieee802154_mac_params params;
477         struct wpan_phy *phy;
478         int rc = -EINVAL;
479
480         pr_debug("%s\n", __func__);
481
482         dev = ieee802154_nl_get_dev(info);
483         if (!dev)
484                 return -ENODEV;
485
486         ops = ieee802154_mlme_ops(dev);
487
488         if (!ops->get_mac_params || !ops->set_mac_params) {
489                 rc = -EOPNOTSUPP;
490                 goto out;
491         }
492
493         if (netif_running(dev)) {
494                 rc = -EBUSY;
495                 goto out;
496         }
497
498         if (!info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
499             !info->attrs[IEEE802154_ATTR_CCA_MODE] &&
500             !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] &&
501             !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] &&
502             !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] &&
503             !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] &&
504             !info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
505                 goto out;
506
507         phy = dev->ieee802154_ptr->wpan_phy;
508         get_device(&phy->dev);
509
510         rtnl_lock();
511         ops->get_mac_params(dev, &params);
512
513         if (info->attrs[IEEE802154_ATTR_TXPOWER])
514                 params.transmit_power = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]) * 100;
515
516         if (info->attrs[IEEE802154_ATTR_LBT_ENABLED])
517                 params.lbt = nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
518
519         if (info->attrs[IEEE802154_ATTR_CCA_MODE])
520                 params.cca.mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
521
522         if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])
523                 params.cca_ed_level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]) * 100;
524
525         if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES])
526                 params.csma_retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]);
527
528         if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE])
529                 params.min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]);
530
531         if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE])
532                 params.max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]);
533
534         if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
535                 params.frame_retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]);
536
537         rc = ops->set_mac_params(dev, &params);
538         rtnl_unlock();
539
540         wpan_phy_put(phy);
541         dev_put(dev);
542
543         return 0;
544
545 out:
546         dev_put(dev);
547         return rc;
548 }
549
550 static int
551 ieee802154_llsec_parse_key_id(struct genl_info *info,
552                               struct ieee802154_llsec_key_id *desc)
553 {
554         memset(desc, 0, sizeof(*desc));
555
556         if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE])
557                 return -EINVAL;
558
559         desc->mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]);
560
561         if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
562                 if (!info->attrs[IEEE802154_ATTR_PAN_ID])
563                         return -EINVAL;
564
565                 desc->device_addr.pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
566
567                 if (info->attrs[IEEE802154_ATTR_SHORT_ADDR]) {
568                         desc->device_addr.mode = IEEE802154_ADDR_SHORT;
569                         desc->device_addr.short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
570                 } else {
571                         if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
572                                 return -EINVAL;
573
574                         desc->device_addr.mode = IEEE802154_ADDR_LONG;
575                         desc->device_addr.extended_addr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
576                 }
577         }
578
579         if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
580             !info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID])
581                 return -EINVAL;
582
583         if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
584             !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT])
585                 return -EINVAL;
586
587         if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
588             !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED])
589                 return -EINVAL;
590
591         if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT)
592                 desc->id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]);
593
594         switch (desc->mode) {
595         case IEEE802154_SCF_KEY_SHORT_INDEX:
596         {
597                 u32 source = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]);
598
599                 desc->short_source = cpu_to_le32(source);
600                 break;
601         }
602         case IEEE802154_SCF_KEY_HW_INDEX:
603                 desc->extended_source = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]);
604                 break;
605         }
606
607         return 0;
608 }
609
610 static int
611 ieee802154_llsec_fill_key_id(struct sk_buff *msg,
612                              const struct ieee802154_llsec_key_id *desc)
613 {
614         if (nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_MODE, desc->mode))
615                 return -EMSGSIZE;
616
617         if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
618                 if (nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID,
619                                       desc->device_addr.pan_id))
620                         return -EMSGSIZE;
621
622                 if (desc->device_addr.mode == IEEE802154_ADDR_SHORT &&
623                     nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
624                                       desc->device_addr.short_addr))
625                         return -EMSGSIZE;
626
627                 if (desc->device_addr.mode == IEEE802154_ADDR_LONG &&
628                     nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR,
629                                    desc->device_addr.extended_addr,
630                                    IEEE802154_ATTR_PAD))
631                         return -EMSGSIZE;
632         }
633
634         if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
635             nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_ID, desc->id))
636                 return -EMSGSIZE;
637
638         if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
639             nla_put_u32(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT,
640                         le32_to_cpu(desc->short_source)))
641                 return -EMSGSIZE;
642
643         if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
644             nla_put_hwaddr(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED,
645                            desc->extended_source, IEEE802154_ATTR_PAD))
646                 return -EMSGSIZE;
647
648         return 0;
649 }
650
651 int ieee802154_llsec_getparams(struct sk_buff *skb, struct genl_info *info)
652 {
653         struct sk_buff *msg;
654         struct net_device *dev = NULL;
655         int rc = -ENOBUFS;
656         struct ieee802154_mlme_ops *ops;
657         void *hdr;
658         struct ieee802154_llsec_params params;
659
660         pr_debug("%s\n", __func__);
661
662         dev = ieee802154_nl_get_dev(info);
663         if (!dev)
664                 return -ENODEV;
665
666         ops = ieee802154_mlme_ops(dev);
667         if (!ops->llsec) {
668                 rc = -EOPNOTSUPP;
669                 goto out_dev;
670         }
671
672         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
673         if (!msg)
674                 goto out_dev;
675
676         hdr = genlmsg_put(msg, 0, info->snd_seq, &nl802154_family, 0,
677                           IEEE802154_LLSEC_GETPARAMS);
678         if (!hdr)
679                 goto out_free;
680
681         rc = ops->llsec->get_params(dev, &params);
682         if (rc < 0)
683                 goto out_free;
684
685         if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
686             nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
687             nla_put_u8(msg, IEEE802154_ATTR_LLSEC_ENABLED, params.enabled) ||
688             nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVEL, params.out_level) ||
689             nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
690                         be32_to_cpu(params.frame_counter)) ||
691             ieee802154_llsec_fill_key_id(msg, &params.out_key)) {
692                 rc = -ENOBUFS;
693                 goto out_free;
694         }
695
696         dev_put(dev);
697
698         return ieee802154_nl_reply(msg, info);
699 out_free:
700         nlmsg_free(msg);
701 out_dev:
702         dev_put(dev);
703         return rc;
704 }
705
706 int ieee802154_llsec_setparams(struct sk_buff *skb, struct genl_info *info)
707 {
708         struct net_device *dev = NULL;
709         int rc = -EINVAL;
710         struct ieee802154_mlme_ops *ops;
711         struct ieee802154_llsec_params params;
712         int changed = 0;
713
714         pr_debug("%s\n", __func__);
715
716         dev = ieee802154_nl_get_dev(info);
717         if (!dev)
718                 return -ENODEV;
719
720         if (!info->attrs[IEEE802154_ATTR_LLSEC_ENABLED] &&
721             !info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE] &&
722             !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL])
723                 goto out;
724
725         ops = ieee802154_mlme_ops(dev);
726         if (!ops->llsec) {
727                 rc = -EOPNOTSUPP;
728                 goto out;
729         }
730
731         if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL] &&
732             nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) > 7)
733                 goto out;
734
735         if (info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]) {
736                 params.enabled = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]);
737                 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
738         }
739
740         if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) {
741                 if (ieee802154_llsec_parse_key_id(info, &params.out_key))
742                         goto out;
743
744                 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
745         }
746
747         if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) {
748                 params.out_level = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]);
749                 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
750         }
751
752         if (info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]) {
753                 u32 fc = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
754
755                 params.frame_counter = cpu_to_be32(fc);
756                 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
757         }
758
759         rc = ops->llsec->set_params(dev, &params, changed);
760
761         dev_put(dev);
762
763         return rc;
764 out:
765         dev_put(dev);
766         return rc;
767 }
768
769 struct llsec_dump_data {
770         struct sk_buff *skb;
771         int s_idx, s_idx2;
772         int portid;
773         int nlmsg_seq;
774         struct net_device *dev;
775         struct ieee802154_mlme_ops *ops;
776         struct ieee802154_llsec_table *table;
777 };
778
779 static int
780 ieee802154_llsec_dump_table(struct sk_buff *skb, struct netlink_callback *cb,
781                             int (*step)(struct llsec_dump_data *))
782 {
783         struct net *net = sock_net(skb->sk);
784         struct net_device *dev;
785         struct llsec_dump_data data;
786         int idx = 0;
787         int first_dev = cb->args[0];
788         int rc;
789
790         for_each_netdev(net, dev) {
791                 if (idx < first_dev || dev->type != ARPHRD_IEEE802154)
792                         goto skip;
793
794                 data.ops = ieee802154_mlme_ops(dev);
795                 if (!data.ops->llsec)
796                         goto skip;
797
798                 data.skb = skb;
799                 data.s_idx = cb->args[1];
800                 data.s_idx2 = cb->args[2];
801                 data.dev = dev;
802                 data.portid = NETLINK_CB(cb->skb).portid;
803                 data.nlmsg_seq = cb->nlh->nlmsg_seq;
804
805                 data.ops->llsec->lock_table(dev);
806                 data.ops->llsec->get_table(data.dev, &data.table);
807                 rc = step(&data);
808                 data.ops->llsec->unlock_table(dev);
809
810                 if (rc < 0)
811                         break;
812
813 skip:
814                 idx++;
815         }
816         cb->args[0] = idx;
817
818         return skb->len;
819 }
820
821 static int
822 ieee802154_nl_llsec_change(struct sk_buff *skb, struct genl_info *info,
823                            int (*fn)(struct net_device*, struct genl_info*))
824 {
825         struct net_device *dev = NULL;
826         int rc = -EINVAL;
827
828         dev = ieee802154_nl_get_dev(info);
829         if (!dev)
830                 return -ENODEV;
831
832         if (!ieee802154_mlme_ops(dev)->llsec)
833                 rc = -EOPNOTSUPP;
834         else
835                 rc = fn(dev, info);
836
837         dev_put(dev);
838         return rc;
839 }
840
841 static int
842 ieee802154_llsec_parse_key(struct genl_info *info,
843                            struct ieee802154_llsec_key *key)
844 {
845         u8 frames;
846         u32 commands[256 / 32];
847
848         memset(key, 0, sizeof(*key));
849
850         if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES] ||
851             !info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES])
852                 return -EINVAL;
853
854         frames = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES]);
855         if ((frames & BIT(IEEE802154_FC_TYPE_MAC_CMD)) &&
856             !info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS])
857                 return -EINVAL;
858
859         if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) {
860                 nla_memcpy(commands,
861                            info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS],
862                            256 / 8);
863
864                 if (commands[0] || commands[1] || commands[2] || commands[3] ||
865                     commands[4] || commands[5] || commands[6] ||
866                     commands[7] >= BIT(IEEE802154_CMD_GTS_REQ + 1))
867                         return -EINVAL;
868
869                 key->cmd_frame_ids = commands[7];
870         }
871
872         key->frame_types = frames;
873
874         nla_memcpy(key->key, info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES],
875                    IEEE802154_LLSEC_KEY_SIZE);
876
877         return 0;
878 }
879
880 static int llsec_add_key(struct net_device *dev, struct genl_info *info)
881 {
882         struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
883         struct ieee802154_llsec_key key;
884         struct ieee802154_llsec_key_id id;
885
886         if (ieee802154_llsec_parse_key(info, &key) ||
887             ieee802154_llsec_parse_key_id(info, &id))
888                 return -EINVAL;
889
890         return ops->llsec->add_key(dev, &id, &key);
891 }
892
893 int ieee802154_llsec_add_key(struct sk_buff *skb, struct genl_info *info)
894 {
895         if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
896             (NLM_F_CREATE | NLM_F_EXCL))
897                 return -EINVAL;
898
899         return ieee802154_nl_llsec_change(skb, info, llsec_add_key);
900 }
901
902 static int llsec_remove_key(struct net_device *dev, struct genl_info *info)
903 {
904         struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
905         struct ieee802154_llsec_key_id id;
906
907         if (ieee802154_llsec_parse_key_id(info, &id))
908                 return -EINVAL;
909
910         return ops->llsec->del_key(dev, &id);
911 }
912
913 int ieee802154_llsec_del_key(struct sk_buff *skb, struct genl_info *info)
914 {
915         return ieee802154_nl_llsec_change(skb, info, llsec_remove_key);
916 }
917
918 static int
919 ieee802154_nl_fill_key(struct sk_buff *msg, u32 portid, u32 seq,
920                        const struct ieee802154_llsec_key_entry *key,
921                        const struct net_device *dev)
922 {
923         void *hdr;
924         u32 commands[256 / 32];
925
926         hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
927                           IEEE802154_LLSEC_LIST_KEY);
928         if (!hdr)
929                 goto out;
930
931         if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
932             nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
933             ieee802154_llsec_fill_key_id(msg, &key->id) ||
934             nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES,
935                        key->key->frame_types))
936                 goto nla_put_failure;
937
938         if (key->key->frame_types & BIT(IEEE802154_FC_TYPE_MAC_CMD)) {
939                 memset(commands, 0, sizeof(commands));
940                 commands[7] = key->key->cmd_frame_ids;
941                 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS,
942                             sizeof(commands), commands))
943                         goto nla_put_failure;
944         }
945
946         if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_BYTES,
947                     IEEE802154_LLSEC_KEY_SIZE, key->key->key))
948                 goto nla_put_failure;
949
950         genlmsg_end(msg, hdr);
951         return 0;
952
953 nla_put_failure:
954         genlmsg_cancel(msg, hdr);
955 out:
956         return -EMSGSIZE;
957 }
958
959 static int llsec_iter_keys(struct llsec_dump_data *data)
960 {
961         struct ieee802154_llsec_key_entry *pos;
962         int rc = 0, idx = 0;
963
964         list_for_each_entry(pos, &data->table->keys, list) {
965                 if (idx++ < data->s_idx)
966                         continue;
967
968                 if (ieee802154_nl_fill_key(data->skb, data->portid,
969                                            data->nlmsg_seq, pos, data->dev)) {
970                         rc = -EMSGSIZE;
971                         break;
972                 }
973
974                 data->s_idx++;
975         }
976
977         return rc;
978 }
979
980 int ieee802154_llsec_dump_keys(struct sk_buff *skb, struct netlink_callback *cb)
981 {
982         return ieee802154_llsec_dump_table(skb, cb, llsec_iter_keys);
983 }
984
985 static int
986 llsec_parse_dev(struct genl_info *info,
987                 struct ieee802154_llsec_device *dev)
988 {
989         memset(dev, 0, sizeof(*dev));
990
991         if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
992             !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
993             !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE] ||
994             !info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE] ||
995             (!!info->attrs[IEEE802154_ATTR_PAN_ID] !=
996              !!info->attrs[IEEE802154_ATTR_SHORT_ADDR]))
997                 return -EINVAL;
998
999         if (info->attrs[IEEE802154_ATTR_PAN_ID]) {
1000                 dev->pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
1001                 dev->short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
1002         } else {
1003                 dev->short_addr = cpu_to_le16(IEEE802154_ADDR_UNDEF);
1004         }
1005
1006         dev->hwaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1007         dev->frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1008         dev->seclevel_exempt = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1009         dev->key_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE]);
1010
1011         if (dev->key_mode >= __IEEE802154_LLSEC_DEVKEY_MAX)
1012                 return -EINVAL;
1013
1014         return 0;
1015 }
1016
1017 static int llsec_add_dev(struct net_device *dev, struct genl_info *info)
1018 {
1019         struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1020         struct ieee802154_llsec_device desc;
1021
1022         if (llsec_parse_dev(info, &desc))
1023                 return -EINVAL;
1024
1025         return ops->llsec->add_dev(dev, &desc);
1026 }
1027
1028 int ieee802154_llsec_add_dev(struct sk_buff *skb, struct genl_info *info)
1029 {
1030         if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1031             (NLM_F_CREATE | NLM_F_EXCL))
1032                 return -EINVAL;
1033
1034         return ieee802154_nl_llsec_change(skb, info, llsec_add_dev);
1035 }
1036
1037 static int llsec_del_dev(struct net_device *dev, struct genl_info *info)
1038 {
1039         struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1040         __le64 devaddr;
1041
1042         if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
1043                 return -EINVAL;
1044
1045         devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1046
1047         return ops->llsec->del_dev(dev, devaddr);
1048 }
1049
1050 int ieee802154_llsec_del_dev(struct sk_buff *skb, struct genl_info *info)
1051 {
1052         return ieee802154_nl_llsec_change(skb, info, llsec_del_dev);
1053 }
1054
1055 static int
1056 ieee802154_nl_fill_dev(struct sk_buff *msg, u32 portid, u32 seq,
1057                        const struct ieee802154_llsec_device *desc,
1058                        const struct net_device *dev)
1059 {
1060         void *hdr;
1061
1062         hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1063                           IEEE802154_LLSEC_LIST_DEV);
1064         if (!hdr)
1065                 goto out;
1066
1067         if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1068             nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1069             nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, desc->pan_id) ||
1070             nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
1071                               desc->short_addr) ||
1072             nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, desc->hwaddr,
1073                            IEEE802154_ATTR_PAD) ||
1074             nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1075                         desc->frame_counter) ||
1076             nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1077                        desc->seclevel_exempt) ||
1078             nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE, desc->key_mode))
1079                 goto nla_put_failure;
1080
1081         genlmsg_end(msg, hdr);
1082         return 0;
1083
1084 nla_put_failure:
1085         genlmsg_cancel(msg, hdr);
1086 out:
1087         return -EMSGSIZE;
1088 }
1089
1090 static int llsec_iter_devs(struct llsec_dump_data *data)
1091 {
1092         struct ieee802154_llsec_device *pos;
1093         int rc = 0, idx = 0;
1094
1095         list_for_each_entry(pos, &data->table->devices, list) {
1096                 if (idx++ < data->s_idx)
1097                         continue;
1098
1099                 if (ieee802154_nl_fill_dev(data->skb, data->portid,
1100                                            data->nlmsg_seq, pos, data->dev)) {
1101                         rc = -EMSGSIZE;
1102                         break;
1103                 }
1104
1105                 data->s_idx++;
1106         }
1107
1108         return rc;
1109 }
1110
1111 int ieee802154_llsec_dump_devs(struct sk_buff *skb, struct netlink_callback *cb)
1112 {
1113         return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devs);
1114 }
1115
1116 static int llsec_add_devkey(struct net_device *dev, struct genl_info *info)
1117 {
1118         struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1119         struct ieee802154_llsec_device_key key;
1120         __le64 devaddr;
1121
1122         if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
1123             !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1124             ieee802154_llsec_parse_key_id(info, &key.key_id))
1125                 return -EINVAL;
1126
1127         devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1128         key.frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1129
1130         return ops->llsec->add_devkey(dev, devaddr, &key);
1131 }
1132
1133 int ieee802154_llsec_add_devkey(struct sk_buff *skb, struct genl_info *info)
1134 {
1135         if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1136             (NLM_F_CREATE | NLM_F_EXCL))
1137                 return -EINVAL;
1138
1139         return ieee802154_nl_llsec_change(skb, info, llsec_add_devkey);
1140 }
1141
1142 static int llsec_del_devkey(struct net_device *dev, struct genl_info *info)
1143 {
1144         struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1145         struct ieee802154_llsec_device_key key;
1146         __le64 devaddr;
1147
1148         if (!info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1149             ieee802154_llsec_parse_key_id(info, &key.key_id))
1150                 return -EINVAL;
1151
1152         devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1153
1154         return ops->llsec->del_devkey(dev, devaddr, &key);
1155 }
1156
1157 int ieee802154_llsec_del_devkey(struct sk_buff *skb, struct genl_info *info)
1158 {
1159         return ieee802154_nl_llsec_change(skb, info, llsec_del_devkey);
1160 }
1161
1162 static int
1163 ieee802154_nl_fill_devkey(struct sk_buff *msg, u32 portid, u32 seq,
1164                           __le64 devaddr,
1165                           const struct ieee802154_llsec_device_key *devkey,
1166                           const struct net_device *dev)
1167 {
1168         void *hdr;
1169
1170         hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1171                           IEEE802154_LLSEC_LIST_DEVKEY);
1172         if (!hdr)
1173                 goto out;
1174
1175         if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1176             nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1177             nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, devaddr,
1178                            IEEE802154_ATTR_PAD) ||
1179             nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1180                         devkey->frame_counter) ||
1181             ieee802154_llsec_fill_key_id(msg, &devkey->key_id))
1182                 goto nla_put_failure;
1183
1184         genlmsg_end(msg, hdr);
1185         return 0;
1186
1187 nla_put_failure:
1188         genlmsg_cancel(msg, hdr);
1189 out:
1190         return -EMSGSIZE;
1191 }
1192
1193 static int llsec_iter_devkeys(struct llsec_dump_data *data)
1194 {
1195         struct ieee802154_llsec_device *dpos;
1196         struct ieee802154_llsec_device_key *kpos;
1197         int rc = 0, idx = 0, idx2;
1198
1199         list_for_each_entry(dpos, &data->table->devices, list) {
1200                 if (idx++ < data->s_idx)
1201                         continue;
1202
1203                 idx2 = 0;
1204
1205                 list_for_each_entry(kpos, &dpos->keys, list) {
1206                         if (idx2++ < data->s_idx2)
1207                                 continue;
1208
1209                         if (ieee802154_nl_fill_devkey(data->skb, data->portid,
1210                                                       data->nlmsg_seq,
1211                                                       dpos->hwaddr, kpos,
1212                                                       data->dev)) {
1213                                 return rc = -EMSGSIZE;
1214                         }
1215
1216                         data->s_idx2++;
1217                 }
1218
1219                 data->s_idx++;
1220         }
1221
1222         return rc;
1223 }
1224
1225 int ieee802154_llsec_dump_devkeys(struct sk_buff *skb,
1226                                   struct netlink_callback *cb)
1227 {
1228         return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devkeys);
1229 }
1230
1231 static int
1232 llsec_parse_seclevel(struct genl_info *info,
1233                      struct ieee802154_llsec_seclevel *sl)
1234 {
1235         memset(sl, 0, sizeof(*sl));
1236
1237         if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE] ||
1238             !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS] ||
1239             !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE])
1240                 return -EINVAL;
1241
1242         sl->frame_type = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE]);
1243         if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD) {
1244                 if (!info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID])
1245                         return -EINVAL;
1246
1247                 sl->cmd_frame_id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]);
1248         }
1249
1250         sl->sec_levels = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS]);
1251         sl->device_override = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1252
1253         return 0;
1254 }
1255
1256 static int llsec_add_seclevel(struct net_device *dev, struct genl_info *info)
1257 {
1258         struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1259         struct ieee802154_llsec_seclevel sl;
1260
1261         if (llsec_parse_seclevel(info, &sl))
1262                 return -EINVAL;
1263
1264         return ops->llsec->add_seclevel(dev, &sl);
1265 }
1266
1267 int ieee802154_llsec_add_seclevel(struct sk_buff *skb, struct genl_info *info)
1268 {
1269         if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1270             (NLM_F_CREATE | NLM_F_EXCL))
1271                 return -EINVAL;
1272
1273         return ieee802154_nl_llsec_change(skb, info, llsec_add_seclevel);
1274 }
1275
1276 static int llsec_del_seclevel(struct net_device *dev, struct genl_info *info)
1277 {
1278         struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1279         struct ieee802154_llsec_seclevel sl;
1280
1281         if (llsec_parse_seclevel(info, &sl))
1282                 return -EINVAL;
1283
1284         return ops->llsec->del_seclevel(dev, &sl);
1285 }
1286
1287 int ieee802154_llsec_del_seclevel(struct sk_buff *skb, struct genl_info *info)
1288 {
1289         return ieee802154_nl_llsec_change(skb, info, llsec_del_seclevel);
1290 }
1291
1292 static int
1293 ieee802154_nl_fill_seclevel(struct sk_buff *msg, u32 portid, u32 seq,
1294                             const struct ieee802154_llsec_seclevel *sl,
1295                             const struct net_device *dev)
1296 {
1297         void *hdr;
1298
1299         hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1300                           IEEE802154_LLSEC_LIST_SECLEVEL);
1301         if (!hdr)
1302                 goto out;
1303
1304         if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1305             nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1306             nla_put_u8(msg, IEEE802154_ATTR_LLSEC_FRAME_TYPE, sl->frame_type) ||
1307             nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVELS, sl->sec_levels) ||
1308             nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1309                        sl->device_override))
1310                 goto nla_put_failure;
1311
1312         if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD &&
1313             nla_put_u8(msg, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID,
1314                        sl->cmd_frame_id))
1315                 goto nla_put_failure;
1316
1317         genlmsg_end(msg, hdr);
1318         return 0;
1319
1320 nla_put_failure:
1321         genlmsg_cancel(msg, hdr);
1322 out:
1323         return -EMSGSIZE;
1324 }
1325
1326 static int llsec_iter_seclevels(struct llsec_dump_data *data)
1327 {
1328         struct ieee802154_llsec_seclevel *pos;
1329         int rc = 0, idx = 0;
1330
1331         list_for_each_entry(pos, &data->table->security_levels, list) {
1332                 if (idx++ < data->s_idx)
1333                         continue;
1334
1335                 if (ieee802154_nl_fill_seclevel(data->skb, data->portid,
1336                                                 data->nlmsg_seq, pos,
1337                                                 data->dev)) {
1338                         rc = -EMSGSIZE;
1339                         break;
1340                 }
1341
1342                 data->s_idx++;
1343         }
1344
1345         return rc;
1346 }
1347
1348 int ieee802154_llsec_dump_seclevels(struct sk_buff *skb,
1349                                     struct netlink_callback *cb)
1350 {
1351         return ieee802154_llsec_dump_table(skb, cb, llsec_iter_seclevels);
1352 }