drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / net / ieee802154 / nl-mac.c
blob74ef0a310afbb3a0754a15144a0df531d05743b9
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Netlink interface for IEEE 802.15.4 stack
5 * Copyright 2007, 2008 Siemens AG
7 * Written by:
8 * Sergey Lapin <slapin@ossfans.org>
9 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
10 * Maxim Osipov <maxim.osipov@siemens.com>
13 #include <linux/gfp.h>
14 #include <linux/kernel.h>
15 #include <linux/if_arp.h>
16 #include <linux/netdevice.h>
17 #include <linux/ieee802154.h>
18 #include <net/netlink.h>
19 #include <net/genetlink.h>
20 #include <net/sock.h>
21 #include <linux/nl802154.h>
22 #include <linux/export.h>
23 #include <net/af_ieee802154.h>
24 #include <net/ieee802154_netdev.h>
25 #include <net/cfg802154.h>
27 #include "ieee802154.h"
29 static int nla_put_hwaddr(struct sk_buff *msg, int type, __le64 hwaddr,
30 int padattr)
32 return nla_put_u64_64bit(msg, type, swab64((__force u64)hwaddr),
33 padattr);
36 static __le64 nla_get_hwaddr(const struct nlattr *nla)
38 return ieee802154_devaddr_from_raw(nla_data(nla));
41 static int nla_put_shortaddr(struct sk_buff *msg, int type, __le16 addr)
43 return nla_put_u16(msg, type, le16_to_cpu(addr));
46 static __le16 nla_get_shortaddr(const struct nlattr *nla)
48 return cpu_to_le16(nla_get_u16(nla));
51 static int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
53 struct sk_buff *msg;
55 pr_debug("%s\n", __func__);
57 msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
58 if (!msg)
59 return -ENOBUFS;
61 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
62 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
63 nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
64 dev->dev_addr) ||
65 nla_put_u8(msg, IEEE802154_ATTR_STATUS, status))
66 goto nla_put_failure;
67 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
69 nla_put_failure:
70 nlmsg_free(msg);
71 return -ENOBUFS;
74 static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid,
75 u32 seq, int flags, struct net_device *dev)
77 void *hdr;
78 struct wpan_phy *phy;
79 struct ieee802154_mlme_ops *ops;
80 __le16 short_addr, pan_id;
82 pr_debug("%s\n", __func__);
84 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
85 IEEE802154_LIST_IFACE);
86 if (!hdr)
87 goto out;
89 ops = ieee802154_mlme_ops(dev);
90 phy = dev->ieee802154_ptr->wpan_phy;
91 BUG_ON(!phy);
92 get_device(&phy->dev);
94 rtnl_lock();
95 short_addr = dev->ieee802154_ptr->short_addr;
96 pan_id = dev->ieee802154_ptr->pan_id;
97 rtnl_unlock();
99 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
100 nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
101 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
102 nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
103 dev->dev_addr) ||
104 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) ||
105 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, pan_id))
106 goto nla_put_failure;
108 if (ops->get_mac_params) {
109 struct ieee802154_mac_params params;
111 rtnl_lock();
112 ops->get_mac_params(dev, &params);
113 rtnl_unlock();
115 if (nla_put_s8(msg, IEEE802154_ATTR_TXPOWER,
116 params.transmit_power / 100) ||
117 nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, params.lbt) ||
118 nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE,
119 params.cca.mode) ||
120 nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL,
121 params.cca_ed_level / 100) ||
122 nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES,
123 params.csma_retries) ||
124 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE,
125 params.min_be) ||
126 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE,
127 params.max_be) ||
128 nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES,
129 params.frame_retries))
130 goto nla_put_failure;
133 wpan_phy_put(phy);
134 genlmsg_end(msg, hdr);
135 return 0;
137 nla_put_failure:
138 wpan_phy_put(phy);
139 genlmsg_cancel(msg, hdr);
140 out:
141 return -EMSGSIZE;
144 /* Requests from userspace */
145 static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
147 struct net_device *dev;
149 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
150 char name[IFNAMSIZ + 1];
152 nla_strscpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
153 sizeof(name));
154 dev = dev_get_by_name(&init_net, name);
155 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX]) {
156 dev = dev_get_by_index(&init_net,
157 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
158 } else {
159 return NULL;
162 if (!dev)
163 return NULL;
165 if (dev->type != ARPHRD_IEEE802154) {
166 dev_put(dev);
167 return NULL;
170 return dev;
173 int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info)
175 struct net_device *dev;
176 struct ieee802154_addr addr;
177 u8 page;
178 int ret = -EOPNOTSUPP;
180 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
181 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
182 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
183 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
184 !info->attrs[IEEE802154_ATTR_CAPABILITY])
185 return -EINVAL;
187 dev = ieee802154_nl_get_dev(info);
188 if (!dev)
189 return -ENODEV;
190 if (!ieee802154_mlme_ops(dev)->assoc_req)
191 goto out;
193 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
194 addr.mode = IEEE802154_ADDR_LONG;
195 addr.extended_addr = nla_get_hwaddr(
196 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]);
197 } else {
198 addr.mode = IEEE802154_ADDR_SHORT;
199 addr.short_addr = nla_get_shortaddr(
200 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
202 addr.pan_id = nla_get_shortaddr(
203 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
205 page = nla_get_u8_default(info->attrs[IEEE802154_ATTR_PAGE], 0);
207 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
208 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
209 page,
210 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
212 out:
213 dev_put(dev);
214 return ret;
217 int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info)
219 struct net_device *dev;
220 struct ieee802154_addr addr;
221 int ret = -EOPNOTSUPP;
223 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
224 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
225 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
226 return -EINVAL;
228 dev = ieee802154_nl_get_dev(info);
229 if (!dev)
230 return -ENODEV;
231 if (!ieee802154_mlme_ops(dev)->assoc_resp)
232 goto out;
234 addr.mode = IEEE802154_ADDR_LONG;
235 addr.extended_addr = nla_get_hwaddr(
236 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
237 rtnl_lock();
238 addr.pan_id = dev->ieee802154_ptr->pan_id;
239 rtnl_unlock();
241 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
242 nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
243 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
245 out:
246 dev_put(dev);
247 return ret;
250 int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info)
252 struct net_device *dev;
253 struct ieee802154_addr addr;
254 int ret = -EOPNOTSUPP;
256 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
257 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
258 !info->attrs[IEEE802154_ATTR_REASON])
259 return -EINVAL;
261 dev = ieee802154_nl_get_dev(info);
262 if (!dev)
263 return -ENODEV;
264 if (!ieee802154_mlme_ops(dev)->disassoc_req)
265 goto out;
267 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
268 addr.mode = IEEE802154_ADDR_LONG;
269 addr.extended_addr = nla_get_hwaddr(
270 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
271 } else {
272 addr.mode = IEEE802154_ADDR_SHORT;
273 addr.short_addr = nla_get_shortaddr(
274 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
276 rtnl_lock();
277 addr.pan_id = dev->ieee802154_ptr->pan_id;
278 rtnl_unlock();
280 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
281 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
283 out:
284 dev_put(dev);
285 return ret;
288 /* PANid, channel, beacon_order = 15, superframe_order = 15,
289 * PAN_coordinator, battery_life_extension = 0,
290 * coord_realignment = 0, security_enable = 0
292 int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
294 struct net_device *dev;
295 struct ieee802154_addr addr;
297 u8 channel, bcn_ord, sf_ord;
298 u8 page;
299 int pan_coord, blx, coord_realign;
300 int ret = -EBUSY;
302 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
303 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
304 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
305 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
306 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
307 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
308 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
309 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
311 return -EINVAL;
313 dev = ieee802154_nl_get_dev(info);
314 if (!dev)
315 return -ENODEV;
317 if (netif_running(dev))
318 goto out;
320 if (!ieee802154_mlme_ops(dev)->start_req) {
321 ret = -EOPNOTSUPP;
322 goto out;
325 addr.mode = IEEE802154_ADDR_SHORT;
326 addr.short_addr = nla_get_shortaddr(
327 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
328 addr.pan_id = nla_get_shortaddr(
329 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
331 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
332 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
333 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
334 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
335 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
336 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
338 page = nla_get_u8_default(info->attrs[IEEE802154_ATTR_PAGE], 0);
340 if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
341 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
342 dev_put(dev);
343 return -EINVAL;
346 rtnl_lock();
347 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
348 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
349 rtnl_unlock();
351 /* FIXME: add validation for unused parameters to be sane
352 * for SoftMAC
354 ieee802154_nl_start_confirm(dev, IEEE802154_SUCCESS);
356 out:
357 dev_put(dev);
358 return ret;
361 int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
363 struct net_device *dev;
364 int ret = -EOPNOTSUPP;
365 u8 type;
366 u32 channels;
367 u8 duration;
368 u8 page;
370 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
371 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
372 !info->attrs[IEEE802154_ATTR_DURATION])
373 return -EINVAL;
375 dev = ieee802154_nl_get_dev(info);
376 if (!dev)
377 return -ENODEV;
378 if (!ieee802154_mlme_ops(dev)->scan_req)
379 goto out;
381 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
382 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
383 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
385 page = nla_get_u8_default(info->attrs[IEEE802154_ATTR_PAGE], 0);
387 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
388 page, duration);
390 out:
391 dev_put(dev);
392 return ret;
395 int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info)
397 /* Request for interface name, index, type, IEEE address,
398 * PAN Id, short address
400 struct sk_buff *msg;
401 struct net_device *dev = NULL;
402 int rc = -ENOBUFS;
404 pr_debug("%s\n", __func__);
406 dev = ieee802154_nl_get_dev(info);
407 if (!dev)
408 return -ENODEV;
410 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
411 if (!msg)
412 goto out_dev;
414 rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq,
415 0, dev);
416 if (rc < 0)
417 goto out_free;
419 dev_put(dev);
421 return genlmsg_reply(msg, info);
422 out_free:
423 nlmsg_free(msg);
424 out_dev:
425 dev_put(dev);
426 return rc;
429 int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb)
431 struct net *net = sock_net(skb->sk);
432 struct net_device *dev;
433 int idx;
434 int s_idx = cb->args[0];
436 pr_debug("%s\n", __func__);
438 idx = 0;
439 for_each_netdev(net, dev) {
440 if (idx < s_idx || dev->type != ARPHRD_IEEE802154)
441 goto cont;
443 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid,
444 cb->nlh->nlmsg_seq,
445 NLM_F_MULTI, dev) < 0)
446 break;
447 cont:
448 idx++;
450 cb->args[0] = idx;
452 return skb->len;
455 int ieee802154_set_macparams(struct sk_buff *skb, struct genl_info *info)
457 struct net_device *dev = NULL;
458 struct ieee802154_mlme_ops *ops;
459 struct ieee802154_mac_params params;
460 struct wpan_phy *phy;
461 int rc = -EINVAL;
463 pr_debug("%s\n", __func__);
465 dev = ieee802154_nl_get_dev(info);
466 if (!dev)
467 return -ENODEV;
469 ops = ieee802154_mlme_ops(dev);
471 if (!ops->get_mac_params || !ops->set_mac_params) {
472 rc = -EOPNOTSUPP;
473 goto out;
476 if (netif_running(dev)) {
477 rc = -EBUSY;
478 goto out;
481 if (!info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
482 !info->attrs[IEEE802154_ATTR_CCA_MODE] &&
483 !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] &&
484 !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] &&
485 !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] &&
486 !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] &&
487 !info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
488 goto out;
490 phy = dev->ieee802154_ptr->wpan_phy;
491 get_device(&phy->dev);
493 rtnl_lock();
494 ops->get_mac_params(dev, &params);
496 if (info->attrs[IEEE802154_ATTR_TXPOWER])
497 params.transmit_power = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]) * 100;
499 if (info->attrs[IEEE802154_ATTR_LBT_ENABLED])
500 params.lbt = nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
502 if (info->attrs[IEEE802154_ATTR_CCA_MODE])
503 params.cca.mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
505 if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])
506 params.cca_ed_level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]) * 100;
508 if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES])
509 params.csma_retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]);
511 if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE])
512 params.min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]);
514 if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE])
515 params.max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]);
517 if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
518 params.frame_retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]);
520 rc = ops->set_mac_params(dev, &params);
521 rtnl_unlock();
523 wpan_phy_put(phy);
524 dev_put(dev);
526 return 0;
528 out:
529 dev_put(dev);
530 return rc;
533 static int
534 ieee802154_llsec_parse_key_id(struct genl_info *info,
535 struct ieee802154_llsec_key_id *desc)
537 memset(desc, 0, sizeof(*desc));
539 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE])
540 return -EINVAL;
542 desc->mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]);
544 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
545 if (!info->attrs[IEEE802154_ATTR_PAN_ID])
546 return -EINVAL;
548 desc->device_addr.pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
550 if (info->attrs[IEEE802154_ATTR_SHORT_ADDR]) {
551 desc->device_addr.mode = IEEE802154_ADDR_SHORT;
552 desc->device_addr.short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
553 } else {
554 if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
555 return -EINVAL;
557 desc->device_addr.mode = IEEE802154_ADDR_LONG;
558 desc->device_addr.extended_addr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
562 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
563 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID])
564 return -EINVAL;
566 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
567 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT])
568 return -EINVAL;
570 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
571 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED])
572 return -EINVAL;
574 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT)
575 desc->id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]);
577 switch (desc->mode) {
578 case IEEE802154_SCF_KEY_SHORT_INDEX:
580 u32 source = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]);
582 desc->short_source = cpu_to_le32(source);
583 break;
585 case IEEE802154_SCF_KEY_HW_INDEX:
586 desc->extended_source = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]);
587 break;
590 return 0;
593 static int
594 ieee802154_llsec_fill_key_id(struct sk_buff *msg,
595 const struct ieee802154_llsec_key_id *desc)
597 if (nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_MODE, desc->mode))
598 return -EMSGSIZE;
600 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
601 if (nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID,
602 desc->device_addr.pan_id))
603 return -EMSGSIZE;
605 if (desc->device_addr.mode == IEEE802154_ADDR_SHORT &&
606 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
607 desc->device_addr.short_addr))
608 return -EMSGSIZE;
610 if (desc->device_addr.mode == IEEE802154_ADDR_LONG &&
611 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR,
612 desc->device_addr.extended_addr,
613 IEEE802154_ATTR_PAD))
614 return -EMSGSIZE;
617 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
618 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_ID, desc->id))
619 return -EMSGSIZE;
621 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
622 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT,
623 le32_to_cpu(desc->short_source)))
624 return -EMSGSIZE;
626 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
627 nla_put_hwaddr(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED,
628 desc->extended_source, IEEE802154_ATTR_PAD))
629 return -EMSGSIZE;
631 return 0;
634 int ieee802154_llsec_getparams(struct sk_buff *skb, struct genl_info *info)
636 struct sk_buff *msg;
637 struct net_device *dev = NULL;
638 int rc = -ENOBUFS;
639 struct ieee802154_mlme_ops *ops;
640 void *hdr;
641 struct ieee802154_llsec_params params;
643 pr_debug("%s\n", __func__);
645 dev = ieee802154_nl_get_dev(info);
646 if (!dev)
647 return -ENODEV;
649 ops = ieee802154_mlme_ops(dev);
650 if (!ops->llsec) {
651 rc = -EOPNOTSUPP;
652 goto out_dev;
655 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
656 if (!msg)
657 goto out_dev;
659 hdr = genlmsg_put(msg, 0, info->snd_seq, &nl802154_family, 0,
660 IEEE802154_LLSEC_GETPARAMS);
661 if (!hdr)
662 goto out_free;
664 rc = ops->llsec->get_params(dev, &params);
665 if (rc < 0)
666 goto out_free;
668 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
669 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
670 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_ENABLED, params.enabled) ||
671 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVEL, params.out_level) ||
672 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
673 be32_to_cpu(params.frame_counter)) ||
674 ieee802154_llsec_fill_key_id(msg, &params.out_key)) {
675 rc = -ENOBUFS;
676 goto out_free;
679 dev_put(dev);
681 return ieee802154_nl_reply(msg, info);
682 out_free:
683 nlmsg_free(msg);
684 out_dev:
685 dev_put(dev);
686 return rc;
689 int ieee802154_llsec_setparams(struct sk_buff *skb, struct genl_info *info)
691 struct net_device *dev = NULL;
692 int rc = -EINVAL;
693 struct ieee802154_mlme_ops *ops;
694 struct ieee802154_llsec_params params;
695 int changed = 0;
697 pr_debug("%s\n", __func__);
699 dev = ieee802154_nl_get_dev(info);
700 if (!dev)
701 return -ENODEV;
703 if (!info->attrs[IEEE802154_ATTR_LLSEC_ENABLED] &&
704 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE] &&
705 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL])
706 goto out;
708 ops = ieee802154_mlme_ops(dev);
709 if (!ops->llsec) {
710 rc = -EOPNOTSUPP;
711 goto out;
714 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL] &&
715 nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) > 7)
716 goto out;
718 if (info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]) {
719 params.enabled = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]);
720 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
723 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) {
724 if (ieee802154_llsec_parse_key_id(info, &params.out_key))
725 goto out;
727 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
730 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) {
731 params.out_level = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]);
732 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
735 if (info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]) {
736 u32 fc = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
738 params.frame_counter = cpu_to_be32(fc);
739 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
742 rc = ops->llsec->set_params(dev, &params, changed);
744 dev_put(dev);
746 return rc;
747 out:
748 dev_put(dev);
749 return rc;
752 struct llsec_dump_data {
753 struct sk_buff *skb;
754 int s_idx, s_idx2;
755 int portid;
756 int nlmsg_seq;
757 struct net_device *dev;
758 struct ieee802154_mlme_ops *ops;
759 struct ieee802154_llsec_table *table;
762 static int
763 ieee802154_llsec_dump_table(struct sk_buff *skb, struct netlink_callback *cb,
764 int (*step)(struct llsec_dump_data *))
766 struct net *net = sock_net(skb->sk);
767 struct net_device *dev;
768 struct llsec_dump_data data;
769 int idx = 0;
770 int first_dev = cb->args[0];
771 int rc;
773 for_each_netdev(net, dev) {
774 if (idx < first_dev || dev->type != ARPHRD_IEEE802154)
775 goto skip;
777 data.ops = ieee802154_mlme_ops(dev);
778 if (!data.ops->llsec)
779 goto skip;
781 data.skb = skb;
782 data.s_idx = cb->args[1];
783 data.s_idx2 = cb->args[2];
784 data.dev = dev;
785 data.portid = NETLINK_CB(cb->skb).portid;
786 data.nlmsg_seq = cb->nlh->nlmsg_seq;
788 data.ops->llsec->lock_table(dev);
789 data.ops->llsec->get_table(data.dev, &data.table);
790 rc = step(&data);
791 data.ops->llsec->unlock_table(dev);
793 if (rc < 0)
794 break;
796 skip:
797 idx++;
799 cb->args[0] = idx;
801 return skb->len;
804 static int
805 ieee802154_nl_llsec_change(struct sk_buff *skb, struct genl_info *info,
806 int (*fn)(struct net_device*, struct genl_info*))
808 struct net_device *dev = NULL;
809 int rc = -EINVAL;
811 dev = ieee802154_nl_get_dev(info);
812 if (!dev)
813 return -ENODEV;
815 if (!ieee802154_mlme_ops(dev)->llsec)
816 rc = -EOPNOTSUPP;
817 else
818 rc = fn(dev, info);
820 dev_put(dev);
821 return rc;
824 static int
825 ieee802154_llsec_parse_key(struct genl_info *info,
826 struct ieee802154_llsec_key *key)
828 u8 frames;
829 u32 commands[256 / 32];
831 memset(key, 0, sizeof(*key));
833 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES] ||
834 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES])
835 return -EINVAL;
837 frames = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES]);
838 if ((frames & BIT(IEEE802154_FC_TYPE_MAC_CMD)) &&
839 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS])
840 return -EINVAL;
842 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) {
843 nla_memcpy(commands,
844 info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS],
845 256 / 8);
847 if (commands[0] || commands[1] || commands[2] || commands[3] ||
848 commands[4] || commands[5] || commands[6] ||
849 commands[7] >= BIT(IEEE802154_CMD_GTS_REQ + 1))
850 return -EINVAL;
852 key->cmd_frame_ids = commands[7];
855 key->frame_types = frames;
857 nla_memcpy(key->key, info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES],
858 IEEE802154_LLSEC_KEY_SIZE);
860 return 0;
863 static int llsec_add_key(struct net_device *dev, struct genl_info *info)
865 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
866 struct ieee802154_llsec_key key;
867 struct ieee802154_llsec_key_id id;
869 if (ieee802154_llsec_parse_key(info, &key) ||
870 ieee802154_llsec_parse_key_id(info, &id))
871 return -EINVAL;
873 return ops->llsec->add_key(dev, &id, &key);
876 int ieee802154_llsec_add_key(struct sk_buff *skb, struct genl_info *info)
878 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
879 (NLM_F_CREATE | NLM_F_EXCL))
880 return -EINVAL;
882 return ieee802154_nl_llsec_change(skb, info, llsec_add_key);
885 static int llsec_remove_key(struct net_device *dev, struct genl_info *info)
887 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
888 struct ieee802154_llsec_key_id id;
890 if (ieee802154_llsec_parse_key_id(info, &id))
891 return -EINVAL;
893 return ops->llsec->del_key(dev, &id);
896 int ieee802154_llsec_del_key(struct sk_buff *skb, struct genl_info *info)
898 return ieee802154_nl_llsec_change(skb, info, llsec_remove_key);
901 static int
902 ieee802154_nl_fill_key(struct sk_buff *msg, u32 portid, u32 seq,
903 const struct ieee802154_llsec_key_entry *key,
904 const struct net_device *dev)
906 void *hdr;
907 u32 commands[256 / 32];
909 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
910 IEEE802154_LLSEC_LIST_KEY);
911 if (!hdr)
912 goto out;
914 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
915 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
916 ieee802154_llsec_fill_key_id(msg, &key->id) ||
917 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES,
918 key->key->frame_types))
919 goto nla_put_failure;
921 if (key->key->frame_types & BIT(IEEE802154_FC_TYPE_MAC_CMD)) {
922 memset(commands, 0, sizeof(commands));
923 commands[7] = key->key->cmd_frame_ids;
924 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS,
925 sizeof(commands), commands))
926 goto nla_put_failure;
929 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_BYTES,
930 IEEE802154_LLSEC_KEY_SIZE, key->key->key))
931 goto nla_put_failure;
933 genlmsg_end(msg, hdr);
934 return 0;
936 nla_put_failure:
937 genlmsg_cancel(msg, hdr);
938 out:
939 return -EMSGSIZE;
942 static int llsec_iter_keys(struct llsec_dump_data *data)
944 struct ieee802154_llsec_key_entry *pos;
945 int rc = 0, idx = 0;
947 list_for_each_entry(pos, &data->table->keys, list) {
948 if (idx++ < data->s_idx)
949 continue;
951 if (ieee802154_nl_fill_key(data->skb, data->portid,
952 data->nlmsg_seq, pos, data->dev)) {
953 rc = -EMSGSIZE;
954 break;
957 data->s_idx++;
960 return rc;
963 int ieee802154_llsec_dump_keys(struct sk_buff *skb, struct netlink_callback *cb)
965 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_keys);
968 static int
969 llsec_parse_dev(struct genl_info *info,
970 struct ieee802154_llsec_device *dev)
972 memset(dev, 0, sizeof(*dev));
974 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
975 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
976 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE] ||
977 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE] ||
978 (!!info->attrs[IEEE802154_ATTR_PAN_ID] !=
979 !!info->attrs[IEEE802154_ATTR_SHORT_ADDR]))
980 return -EINVAL;
982 if (info->attrs[IEEE802154_ATTR_PAN_ID]) {
983 dev->pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
984 dev->short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
985 } else {
986 dev->short_addr = cpu_to_le16(IEEE802154_ADDR_UNDEF);
989 dev->hwaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
990 dev->frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
991 dev->seclevel_exempt = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
992 dev->key_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE]);
994 if (dev->key_mode >= __IEEE802154_LLSEC_DEVKEY_MAX)
995 return -EINVAL;
997 return 0;
1000 static int llsec_add_dev(struct net_device *dev, struct genl_info *info)
1002 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1003 struct ieee802154_llsec_device desc;
1005 if (llsec_parse_dev(info, &desc))
1006 return -EINVAL;
1008 return ops->llsec->add_dev(dev, &desc);
1011 int ieee802154_llsec_add_dev(struct sk_buff *skb, struct genl_info *info)
1013 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1014 (NLM_F_CREATE | NLM_F_EXCL))
1015 return -EINVAL;
1017 return ieee802154_nl_llsec_change(skb, info, llsec_add_dev);
1020 static int llsec_del_dev(struct net_device *dev, struct genl_info *info)
1022 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1023 __le64 devaddr;
1025 if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
1026 return -EINVAL;
1028 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1030 return ops->llsec->del_dev(dev, devaddr);
1033 int ieee802154_llsec_del_dev(struct sk_buff *skb, struct genl_info *info)
1035 return ieee802154_nl_llsec_change(skb, info, llsec_del_dev);
1038 static int
1039 ieee802154_nl_fill_dev(struct sk_buff *msg, u32 portid, u32 seq,
1040 const struct ieee802154_llsec_device *desc,
1041 const struct net_device *dev)
1043 void *hdr;
1045 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1046 IEEE802154_LLSEC_LIST_DEV);
1047 if (!hdr)
1048 goto out;
1050 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1051 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1052 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, desc->pan_id) ||
1053 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
1054 desc->short_addr) ||
1055 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, desc->hwaddr,
1056 IEEE802154_ATTR_PAD) ||
1057 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1058 desc->frame_counter) ||
1059 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1060 desc->seclevel_exempt) ||
1061 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE, desc->key_mode))
1062 goto nla_put_failure;
1064 genlmsg_end(msg, hdr);
1065 return 0;
1067 nla_put_failure:
1068 genlmsg_cancel(msg, hdr);
1069 out:
1070 return -EMSGSIZE;
1073 static int llsec_iter_devs(struct llsec_dump_data *data)
1075 struct ieee802154_llsec_device *pos;
1076 int rc = 0, idx = 0;
1078 list_for_each_entry(pos, &data->table->devices, list) {
1079 if (idx++ < data->s_idx)
1080 continue;
1082 if (ieee802154_nl_fill_dev(data->skb, data->portid,
1083 data->nlmsg_seq, pos, data->dev)) {
1084 rc = -EMSGSIZE;
1085 break;
1088 data->s_idx++;
1091 return rc;
1094 int ieee802154_llsec_dump_devs(struct sk_buff *skb, struct netlink_callback *cb)
1096 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devs);
1099 static int llsec_add_devkey(struct net_device *dev, struct genl_info *info)
1101 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1102 struct ieee802154_llsec_device_key key;
1103 __le64 devaddr;
1105 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
1106 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1107 ieee802154_llsec_parse_key_id(info, &key.key_id))
1108 return -EINVAL;
1110 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1111 key.frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1113 return ops->llsec->add_devkey(dev, devaddr, &key);
1116 int ieee802154_llsec_add_devkey(struct sk_buff *skb, struct genl_info *info)
1118 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1119 (NLM_F_CREATE | NLM_F_EXCL))
1120 return -EINVAL;
1122 return ieee802154_nl_llsec_change(skb, info, llsec_add_devkey);
1125 static int llsec_del_devkey(struct net_device *dev, struct genl_info *info)
1127 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1128 struct ieee802154_llsec_device_key key;
1129 __le64 devaddr;
1131 if (!info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1132 ieee802154_llsec_parse_key_id(info, &key.key_id))
1133 return -EINVAL;
1135 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1137 return ops->llsec->del_devkey(dev, devaddr, &key);
1140 int ieee802154_llsec_del_devkey(struct sk_buff *skb, struct genl_info *info)
1142 return ieee802154_nl_llsec_change(skb, info, llsec_del_devkey);
1145 static int
1146 ieee802154_nl_fill_devkey(struct sk_buff *msg, u32 portid, u32 seq,
1147 __le64 devaddr,
1148 const struct ieee802154_llsec_device_key *devkey,
1149 const struct net_device *dev)
1151 void *hdr;
1153 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1154 IEEE802154_LLSEC_LIST_DEVKEY);
1155 if (!hdr)
1156 goto out;
1158 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1159 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1160 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, devaddr,
1161 IEEE802154_ATTR_PAD) ||
1162 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1163 devkey->frame_counter) ||
1164 ieee802154_llsec_fill_key_id(msg, &devkey->key_id))
1165 goto nla_put_failure;
1167 genlmsg_end(msg, hdr);
1168 return 0;
1170 nla_put_failure:
1171 genlmsg_cancel(msg, hdr);
1172 out:
1173 return -EMSGSIZE;
1176 static int llsec_iter_devkeys(struct llsec_dump_data *data)
1178 struct ieee802154_llsec_device *dpos;
1179 struct ieee802154_llsec_device_key *kpos;
1180 int idx = 0, idx2;
1182 list_for_each_entry(dpos, &data->table->devices, list) {
1183 if (idx++ < data->s_idx)
1184 continue;
1186 idx2 = 0;
1188 list_for_each_entry(kpos, &dpos->keys, list) {
1189 if (idx2++ < data->s_idx2)
1190 continue;
1192 if (ieee802154_nl_fill_devkey(data->skb, data->portid,
1193 data->nlmsg_seq,
1194 dpos->hwaddr, kpos,
1195 data->dev)) {
1196 return -EMSGSIZE;
1199 data->s_idx2++;
1202 data->s_idx++;
1205 return 0;
1208 int ieee802154_llsec_dump_devkeys(struct sk_buff *skb,
1209 struct netlink_callback *cb)
1211 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devkeys);
1214 static int
1215 llsec_parse_seclevel(struct genl_info *info,
1216 struct ieee802154_llsec_seclevel *sl)
1218 memset(sl, 0, sizeof(*sl));
1220 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE] ||
1221 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS] ||
1222 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE])
1223 return -EINVAL;
1225 sl->frame_type = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE]);
1226 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD) {
1227 if (!info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID])
1228 return -EINVAL;
1230 sl->cmd_frame_id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]);
1233 sl->sec_levels = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS]);
1234 sl->device_override = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1236 return 0;
1239 static int llsec_add_seclevel(struct net_device *dev, struct genl_info *info)
1241 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1242 struct ieee802154_llsec_seclevel sl;
1244 if (llsec_parse_seclevel(info, &sl))
1245 return -EINVAL;
1247 return ops->llsec->add_seclevel(dev, &sl);
1250 int ieee802154_llsec_add_seclevel(struct sk_buff *skb, struct genl_info *info)
1252 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1253 (NLM_F_CREATE | NLM_F_EXCL))
1254 return -EINVAL;
1256 return ieee802154_nl_llsec_change(skb, info, llsec_add_seclevel);
1259 static int llsec_del_seclevel(struct net_device *dev, struct genl_info *info)
1261 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1262 struct ieee802154_llsec_seclevel sl;
1264 if (llsec_parse_seclevel(info, &sl))
1265 return -EINVAL;
1267 return ops->llsec->del_seclevel(dev, &sl);
1270 int ieee802154_llsec_del_seclevel(struct sk_buff *skb, struct genl_info *info)
1272 return ieee802154_nl_llsec_change(skb, info, llsec_del_seclevel);
1275 static int
1276 ieee802154_nl_fill_seclevel(struct sk_buff *msg, u32 portid, u32 seq,
1277 const struct ieee802154_llsec_seclevel *sl,
1278 const struct net_device *dev)
1280 void *hdr;
1282 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1283 IEEE802154_LLSEC_LIST_SECLEVEL);
1284 if (!hdr)
1285 goto out;
1287 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1288 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1289 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_FRAME_TYPE, sl->frame_type) ||
1290 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVELS, sl->sec_levels) ||
1291 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1292 sl->device_override))
1293 goto nla_put_failure;
1295 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD &&
1296 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID,
1297 sl->cmd_frame_id))
1298 goto nla_put_failure;
1300 genlmsg_end(msg, hdr);
1301 return 0;
1303 nla_put_failure:
1304 genlmsg_cancel(msg, hdr);
1305 out:
1306 return -EMSGSIZE;
1309 static int llsec_iter_seclevels(struct llsec_dump_data *data)
1311 struct ieee802154_llsec_seclevel *pos;
1312 int rc = 0, idx = 0;
1314 list_for_each_entry(pos, &data->table->security_levels, list) {
1315 if (idx++ < data->s_idx)
1316 continue;
1318 if (ieee802154_nl_fill_seclevel(data->skb, data->portid,
1319 data->nlmsg_seq, pos,
1320 data->dev)) {
1321 rc = -EMSGSIZE;
1322 break;
1325 data->s_idx++;
1328 return rc;
1331 int ieee802154_llsec_dump_seclevels(struct sk_buff *skb,
1332 struct netlink_callback *cb)
1334 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_seclevels);