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