blob: 7127b9d1a68409457a1d570117ca1a962b6f2293 [file] [log] [blame]
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +04001/*
2 * Netlink inteface 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 *
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040015 * Written by:
16 * Sergey Lapin <slapin@ossfans.org>
17 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
18 * Maxim Osipov <maxim.osipov@siemens.com>
19 */
20
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040022#include <linux/kernel.h>
23#include <linux/if_arp.h>
24#include <linux/netdevice.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020025#include <linux/ieee802154.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040026#include <net/netlink.h>
27#include <net/genetlink.h>
28#include <net/sock.h>
29#include <linux/nl802154.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040030#include <linux/export.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040031#include <net/af_ieee802154.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040032#include <net/ieee802154_netdev.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020033#include <net/cfg802154.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040034
35#include "ieee802154.h"
36
Phoebe Buckheisterae531b92014-03-14 21:24:02 +010037static 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
Alexander Aring9f3295b2014-11-05 20:51:13 +010057static int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040058{
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
David S. Millerbe51da02012-04-01 20:45:25 -040067 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;
Johannes Berg2a94fe42013-11-19 15:19:39 +010073 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040074
75nla_put_failure:
76 nlmsg_free(msg);
77 return -ENOBUFS;
78}
79EXPORT_SYMBOL(ieee802154_nl_start_confirm);
80
Eric W. Biederman15e47302012-09-07 20:12:54 +000081static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid,
Varka Bhadram4710d802014-07-02 09:01:09 +053082 u32 seq, int flags, struct net_device *dev)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040083{
84 void *hdr;
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +030085 struct wpan_phy *phy;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +020086 struct ieee802154_mlme_ops *ops;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +010087 __le16 short_addr, pan_id;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040088
89 pr_debug("%s\n", __func__);
90
91 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
Varka Bhadram4710d802014-07-02 09:01:09 +053092 IEEE802154_LIST_IFACE);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040093 if (!hdr)
94 goto out;
95
Phoebe Buckheistere462ded2014-03-31 21:37:46 +020096 ops = ieee802154_mlme_ops(dev);
Alexander Aringbd28a112014-11-05 20:51:18 +010097 phy = dev->ieee802154_ptr->wpan_phy;
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +030098 BUG_ON(!phy);
Alexander Aringbd28a112014-11-05 20:51:18 +010099 get_device(&phy->dev);
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +0300100
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200101 short_addr = ops->get_short_addr(dev);
102 pan_id = ops->get_pan_id(dev);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100103
David S. Millerbe51da02012-04-01 20:45:25 -0400104 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
105 nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
106 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
107 nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
108 dev->dev_addr) ||
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100109 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) ||
110 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, pan_id))
David S. Millerbe51da02012-04-01 20:45:25 -0400111 goto nla_put_failure;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200112
113 if (ops->get_mac_params) {
114 struct ieee802154_mac_params params;
115
116 ops->get_mac_params(dev, &params);
117
118 if (nla_put_s8(msg, IEEE802154_ATTR_TXPOWER,
119 params.transmit_power) ||
120 nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, params.lbt) ||
121 nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE,
122 params.cca_mode) ||
123 nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL,
124 params.cca_ed_level) ||
125 nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES,
126 params.csma_retries) ||
127 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE,
128 params.min_be) ||
129 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE,
130 params.max_be) ||
131 nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES,
132 params.frame_retries))
133 goto nla_put_failure;
134 }
135
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +0300136 wpan_phy_put(phy);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400137 return genlmsg_end(msg, hdr);
138
139nla_put_failure:
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +0300140 wpan_phy_put(phy);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400141 genlmsg_cancel(msg, hdr);
142out:
143 return -EMSGSIZE;
144}
145
146/* Requests from userspace */
147static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
148{
149 struct net_device *dev;
150
151 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
152 char name[IFNAMSIZ + 1];
Varka Bhadram4710d802014-07-02 09:01:09 +0530153
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400154 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
Varka Bhadram4710d802014-07-02 09:01:09 +0530155 sizeof(name));
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400156 dev = dev_get_by_name(&init_net, name);
Varka Bhadram4710d802014-07-02 09:01:09 +0530157 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX]) {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400158 dev = dev_get_by_index(&init_net,
159 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
Varka Bhadram4710d802014-07-02 09:01:09 +0530160 } else {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400161 return NULL;
Varka Bhadram4710d802014-07-02 09:01:09 +0530162 }
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400163
164 if (!dev)
165 return NULL;
166
167 if (dev->type != ARPHRD_IEEE802154) {
168 dev_put(dev);
169 return NULL;
170 }
171
172 return dev;
173}
174
Johannes Berg1c582d92013-11-14 17:14:41 +0100175int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400176{
177 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100178 struct ieee802154_addr addr;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400179 u8 page;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000180 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400181
182 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
183 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
184 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
185 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
186 !info->attrs[IEEE802154_ATTR_CAPABILITY])
187 return -EINVAL;
188
189 dev = ieee802154_nl_get_dev(info);
190 if (!dev)
191 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000192 if (!ieee802154_mlme_ops(dev)->assoc_req)
193 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400194
195 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100196 addr.mode = IEEE802154_ADDR_LONG;
197 addr.extended_addr = nla_get_hwaddr(
198 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400199 } else {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100200 addr.mode = IEEE802154_ADDR_SHORT;
201 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400202 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
203 }
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100204 addr.pan_id = nla_get_shortaddr(
205 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400206
207 if (info->attrs[IEEE802154_ATTR_PAGE])
208 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
209 else
210 page = 0;
211
212 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
213 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
214 page,
215 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
216
Werner Almesberger56aa0912013-04-04 06:32:35 +0000217out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400218 dev_put(dev);
219 return ret;
220}
221
Johannes Berg1c582d92013-11-14 17:14:41 +0100222int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400223{
224 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100225 struct ieee802154_addr addr;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000226 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400227
228 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
229 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
230 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
231 return -EINVAL;
232
233 dev = ieee802154_nl_get_dev(info);
234 if (!dev)
235 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000236 if (!ieee802154_mlme_ops(dev)->assoc_resp)
237 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400238
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100239 addr.mode = IEEE802154_ADDR_LONG;
240 addr.extended_addr = nla_get_hwaddr(
241 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
242 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400243
244 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100245 nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400246 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
247
Werner Almesberger56aa0912013-04-04 06:32:35 +0000248out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400249 dev_put(dev);
250 return ret;
251}
252
Johannes Berg1c582d92013-11-14 17:14:41 +0100253int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400254{
255 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100256 struct ieee802154_addr addr;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000257 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400258
259 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
Varka Bhadram4710d802014-07-02 09:01:09 +0530260 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400261 !info->attrs[IEEE802154_ATTR_REASON])
262 return -EINVAL;
263
264 dev = ieee802154_nl_get_dev(info);
265 if (!dev)
266 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000267 if (!ieee802154_mlme_ops(dev)->disassoc_req)
268 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400269
270 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100271 addr.mode = IEEE802154_ADDR_LONG;
272 addr.extended_addr = nla_get_hwaddr(
273 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400274 } else {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100275 addr.mode = IEEE802154_ADDR_SHORT;
276 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400277 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
278 }
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100279 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400280
281 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
282 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
283
Werner Almesberger56aa0912013-04-04 06:32:35 +0000284out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400285 dev_put(dev);
286 return ret;
287}
288
Varka Bhadram4710d802014-07-02 09:01:09 +0530289/* PANid, channel, beacon_order = 15, superframe_order = 15,
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400290 * PAN_coordinator, battery_life_extension = 0,
291 * coord_realignment = 0, security_enable = 0
292*/
Johannes Berg1c582d92013-11-14 17:14:41 +0100293int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400294{
295 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100296 struct ieee802154_addr addr;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400297
298 u8 channel, bcn_ord, sf_ord;
299 u8 page;
300 int pan_coord, blx, coord_realign;
Alexander Aring8f499f92014-11-02 04:18:39 +0100301 int ret = -EBUSY;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400302
303 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
304 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
305 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
306 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
307 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
308 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
309 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
310 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
311 )
312 return -EINVAL;
313
314 dev = ieee802154_nl_get_dev(info);
315 if (!dev)
316 return -ENODEV;
Alexander Aring8f499f92014-11-02 04:18:39 +0100317
318 if (netif_running(dev))
Werner Almesberger56aa0912013-04-04 06:32:35 +0000319 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400320
Alexander Aring8f499f92014-11-02 04:18:39 +0100321 if (!ieee802154_mlme_ops(dev)->start_req) {
322 ret = -EOPNOTSUPP;
323 goto out;
324 }
325
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100326 addr.mode = IEEE802154_ADDR_SHORT;
327 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400328 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100329 addr.pan_id = nla_get_shortaddr(
330 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400331
332 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
333 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
334 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
335 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
336 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
337 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
338
339 if (info->attrs[IEEE802154_ATTR_PAGE])
340 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
341 else
342 page = 0;
343
344
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100345 if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400346 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
347 dev_put(dev);
348 return -EINVAL;
349 }
350
351 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
352 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
353
Alexander Aring9f3295b2014-11-05 20:51:13 +0100354 /* FIXME: add validation for unused parameters to be sane
355 * for SoftMAC
356 */
357 ieee802154_nl_start_confirm(dev, IEEE802154_SUCCESS);
358
Werner Almesberger56aa0912013-04-04 06:32:35 +0000359out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400360 dev_put(dev);
361 return ret;
362}
363
Johannes Berg1c582d92013-11-14 17:14:41 +0100364int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400365{
366 struct net_device *dev;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000367 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400368 u8 type;
369 u32 channels;
370 u8 duration;
371 u8 page;
372
373 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
374 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
375 !info->attrs[IEEE802154_ATTR_DURATION])
376 return -EINVAL;
377
378 dev = ieee802154_nl_get_dev(info);
379 if (!dev)
380 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000381 if (!ieee802154_mlme_ops(dev)->scan_req)
382 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400383
384 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
385 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
386 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
387
388 if (info->attrs[IEEE802154_ATTR_PAGE])
389 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
390 else
391 page = 0;
392
393
Varka Bhadram4710d802014-07-02 09:01:09 +0530394 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
395 page, duration);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400396
Werner Almesberger56aa0912013-04-04 06:32:35 +0000397out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400398 dev_put(dev);
399 return ret;
400}
401
Johannes Berg1c582d92013-11-14 17:14:41 +0100402int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400403{
404 /* Request for interface name, index, type, IEEE address,
Varka Bhadram4710d802014-07-02 09:01:09 +0530405 * PAN Id, short address
406 */
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400407 struct sk_buff *msg;
408 struct net_device *dev = NULL;
409 int rc = -ENOBUFS;
410
411 pr_debug("%s\n", __func__);
412
413 dev = ieee802154_nl_get_dev(info);
414 if (!dev)
415 return -ENODEV;
416
Thomas Graf58050fc2012-06-28 03:57:45 +0000417 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400418 if (!msg)
419 goto out_dev;
420
Eric W. Biederman15e47302012-09-07 20:12:54 +0000421 rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq,
Varka Bhadram4710d802014-07-02 09:01:09 +0530422 0, dev);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400423 if (rc < 0)
424 goto out_free;
425
426 dev_put(dev);
427
428 return genlmsg_reply(msg, info);
429out_free:
430 nlmsg_free(msg);
431out_dev:
432 dev_put(dev);
433 return rc;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400434}
435
Johannes Berg1c582d92013-11-14 17:14:41 +0100436int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400437{
438 struct net *net = sock_net(skb->sk);
439 struct net_device *dev;
440 int idx;
441 int s_idx = cb->args[0];
442
443 pr_debug("%s\n", __func__);
444
445 idx = 0;
446 for_each_netdev(net, dev) {
447 if (idx < s_idx || (dev->type != ARPHRD_IEEE802154))
448 goto cont;
449
Eric W. Biederman15e47302012-09-07 20:12:54 +0000450 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid,
Varka Bhadram4710d802014-07-02 09:01:09 +0530451 cb->nlh->nlmsg_seq,
452 NLM_F_MULTI, dev) < 0)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400453 break;
454cont:
455 idx++;
456 }
457 cb->args[0] = idx;
458
459 return skb->len;
460}
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200461
462int ieee802154_set_macparams(struct sk_buff *skb, struct genl_info *info)
463{
464 struct net_device *dev = NULL;
465 struct ieee802154_mlme_ops *ops;
466 struct ieee802154_mac_params params;
467 struct wpan_phy *phy;
468 int rc = -EINVAL;
469
470 pr_debug("%s\n", __func__);
471
472 dev = ieee802154_nl_get_dev(info);
473 if (!dev)
474 return -ENODEV;
475
476 ops = ieee802154_mlme_ops(dev);
477
478 if (!ops->get_mac_params || !ops->set_mac_params) {
479 rc = -EOPNOTSUPP;
480 goto out;
481 }
482
483 if (netif_running(dev)) {
484 rc = -EBUSY;
485 goto out;
486 }
487
488 if (!info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
489 !info->attrs[IEEE802154_ATTR_CCA_MODE] &&
490 !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] &&
491 !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] &&
492 !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] &&
493 !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] &&
494 !info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
495 goto out;
496
Alexander Aringbd28a112014-11-05 20:51:18 +0100497 phy = dev->ieee802154_ptr->wpan_phy;
498 get_device(&phy->dev);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200499
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200500 ops->get_mac_params(dev, &params);
501
502 if (info->attrs[IEEE802154_ATTR_TXPOWER])
503 params.transmit_power = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]);
504
505 if (info->attrs[IEEE802154_ATTR_LBT_ENABLED])
506 params.lbt = nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
507
508 if (info->attrs[IEEE802154_ATTR_CCA_MODE])
509 params.cca_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
510
511 if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])
512 params.cca_ed_level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]);
513
514 if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES])
515 params.csma_retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]);
516
517 if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE])
518 params.min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]);
519
520 if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE])
521 params.max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]);
522
523 if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
524 params.frame_retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]);
525
526 rc = ops->set_mac_params(dev, &params);
527
528 wpan_phy_put(phy);
529 dev_put(dev);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200530
Alexander Aringa543c592014-10-28 18:21:23 +0100531 return 0;
532
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200533out:
534 dev_put(dev);
535 return rc;
536}
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200537
538
539
540static int
541ieee802154_llsec_parse_key_id(struct genl_info *info,
542 struct ieee802154_llsec_key_id *desc)
543{
544 memset(desc, 0, sizeof(*desc));
545
546 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE])
547 return -EINVAL;
548
549 desc->mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]);
550
551 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
552 if (!info->attrs[IEEE802154_ATTR_PAN_ID] &&
553 !(info->attrs[IEEE802154_ATTR_SHORT_ADDR] ||
554 info->attrs[IEEE802154_ATTR_HW_ADDR]))
555 return -EINVAL;
556
557 desc->device_addr.pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
558
559 if (info->attrs[IEEE802154_ATTR_SHORT_ADDR]) {
560 desc->device_addr.mode = IEEE802154_ADDR_SHORT;
561 desc->device_addr.short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
562 } else {
563 desc->device_addr.mode = IEEE802154_ADDR_LONG;
564 desc->device_addr.extended_addr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
565 }
566 }
567
568 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
569 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID])
570 return -EINVAL;
571
572 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
573 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT])
574 return -EINVAL;
575
576 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
577 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED])
578 return -EINVAL;
579
580 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT)
581 desc->id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]);
582
583 switch (desc->mode) {
584 case IEEE802154_SCF_KEY_SHORT_INDEX:
585 {
586 u32 source = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]);
Varka Bhadram4710d802014-07-02 09:01:09 +0530587
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200588 desc->short_source = cpu_to_le32(source);
589 break;
590 }
591 case IEEE802154_SCF_KEY_HW_INDEX:
592 desc->extended_source = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]);
593 break;
594 }
595
596 return 0;
597}
598
599static int
600ieee802154_llsec_fill_key_id(struct sk_buff *msg,
601 const struct ieee802154_llsec_key_id *desc)
602{
603 if (nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_MODE, desc->mode))
604 return -EMSGSIZE;
605
606 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
607 if (nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID,
608 desc->device_addr.pan_id))
609 return -EMSGSIZE;
610
611 if (desc->device_addr.mode == IEEE802154_ADDR_SHORT &&
612 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
613 desc->device_addr.short_addr))
614 return -EMSGSIZE;
615
616 if (desc->device_addr.mode == IEEE802154_ADDR_LONG &&
617 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR,
618 desc->device_addr.extended_addr))
619 return -EMSGSIZE;
620 }
621
622 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
623 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_ID, desc->id))
624 return -EMSGSIZE;
625
626 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
627 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT,
628 le32_to_cpu(desc->short_source)))
629 return -EMSGSIZE;
630
631 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
632 nla_put_hwaddr(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED,
633 desc->extended_source))
634 return -EMSGSIZE;
635
636 return 0;
637}
638
639int ieee802154_llsec_getparams(struct sk_buff *skb, struct genl_info *info)
640{
641 struct sk_buff *msg;
642 struct net_device *dev = NULL;
643 int rc = -ENOBUFS;
644 struct ieee802154_mlme_ops *ops;
645 void *hdr;
646 struct ieee802154_llsec_params params;
647
648 pr_debug("%s\n", __func__);
649
650 dev = ieee802154_nl_get_dev(info);
651 if (!dev)
652 return -ENODEV;
653
654 ops = ieee802154_mlme_ops(dev);
Dan Carpenterb3f7a7b2014-05-22 10:53:06 +0300655 if (!ops->llsec) {
656 rc = -EOPNOTSUPP;
657 goto out_dev;
658 }
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200659
660 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
661 if (!msg)
662 goto out_dev;
663
664 hdr = genlmsg_put(msg, 0, info->snd_seq, &nl802154_family, 0,
Varka Bhadram4710d802014-07-02 09:01:09 +0530665 IEEE802154_LLSEC_GETPARAMS);
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200666 if (!hdr)
667 goto out_free;
668
669 rc = ops->llsec->get_params(dev, &params);
670 if (rc < 0)
671 goto out_free;
672
673 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
674 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
675 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_ENABLED, params.enabled) ||
676 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVEL, params.out_level) ||
677 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
678 be32_to_cpu(params.frame_counter)) ||
679 ieee802154_llsec_fill_key_id(msg, &params.out_key))
680 goto out_free;
681
682 dev_put(dev);
683
684 return ieee802154_nl_reply(msg, info);
685out_free:
686 nlmsg_free(msg);
687out_dev:
688 dev_put(dev);
689 return rc;
690}
691
692int ieee802154_llsec_setparams(struct sk_buff *skb, struct genl_info *info)
693{
694 struct net_device *dev = NULL;
695 int rc = -EINVAL;
696 struct ieee802154_mlme_ops *ops;
697 struct ieee802154_llsec_params params;
698 int changed = 0;
699
700 pr_debug("%s\n", __func__);
701
702 dev = ieee802154_nl_get_dev(info);
703 if (!dev)
704 return -ENODEV;
705
706 if (!info->attrs[IEEE802154_ATTR_LLSEC_ENABLED] &&
707 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE] &&
708 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL])
709 goto out;
710
711 ops = ieee802154_mlme_ops(dev);
712 if (!ops->llsec) {
713 rc = -EOPNOTSUPP;
714 goto out;
715 }
716
717 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL] &&
718 nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) > 7)
719 goto out;
720
721 if (info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]) {
722 params.enabled = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]);
723 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
724 }
725
726 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) {
727 if (ieee802154_llsec_parse_key_id(info, &params.out_key))
728 goto out;
729
730 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
731 }
732
733 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) {
734 params.out_level = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]);
735 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
736 }
737
738 if (info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]) {
739 u32 fc = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
740
741 params.frame_counter = cpu_to_be32(fc);
742 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
743 }
744
745 rc = ops->llsec->set_params(dev, &params, changed);
746
747 dev_put(dev);
748
749 return rc;
750out:
751 dev_put(dev);
752 return rc;
753}
754
755
756
757struct llsec_dump_data {
758 struct sk_buff *skb;
759 int s_idx, s_idx2;
760 int portid;
761 int nlmsg_seq;
762 struct net_device *dev;
763 struct ieee802154_mlme_ops *ops;
764 struct ieee802154_llsec_table *table;
765};
766
767static int
768ieee802154_llsec_dump_table(struct sk_buff *skb, struct netlink_callback *cb,
Varka Bhadram4710d802014-07-02 09:01:09 +0530769 int (*step)(struct llsec_dump_data *))
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200770{
771 struct net *net = sock_net(skb->sk);
772 struct net_device *dev;
773 struct llsec_dump_data data;
774 int idx = 0;
775 int first_dev = cb->args[0];
776 int rc;
777
778 for_each_netdev(net, dev) {
779 if (idx < first_dev || dev->type != ARPHRD_IEEE802154)
780 goto skip;
781
782 data.ops = ieee802154_mlme_ops(dev);
783 if (!data.ops->llsec)
784 goto skip;
785
786 data.skb = skb;
787 data.s_idx = cb->args[1];
788 data.s_idx2 = cb->args[2];
789 data.dev = dev;
790 data.portid = NETLINK_CB(cb->skb).portid;
791 data.nlmsg_seq = cb->nlh->nlmsg_seq;
792
793 data.ops->llsec->lock_table(dev);
794 data.ops->llsec->get_table(data.dev, &data.table);
795 rc = step(&data);
796 data.ops->llsec->unlock_table(dev);
797
798 if (rc < 0)
799 break;
800
801skip:
802 idx++;
803 }
804 cb->args[0] = idx;
805
806 return skb->len;
807}
808
809static int
810ieee802154_nl_llsec_change(struct sk_buff *skb, struct genl_info *info,
811 int (*fn)(struct net_device*, struct genl_info*))
812{
813 struct net_device *dev = NULL;
814 int rc = -EINVAL;
815
816 dev = ieee802154_nl_get_dev(info);
817 if (!dev)
818 return -ENODEV;
819
820 if (!ieee802154_mlme_ops(dev)->llsec)
821 rc = -EOPNOTSUPP;
822 else
823 rc = fn(dev, info);
824
825 dev_put(dev);
826 return rc;
827}
828
829
830
831static int
832ieee802154_llsec_parse_key(struct genl_info *info,
833 struct ieee802154_llsec_key *key)
834{
835 u8 frames;
836 u32 commands[256 / 32];
837
838 memset(key, 0, sizeof(*key));
839
840 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES] ||
841 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES])
842 return -EINVAL;
843
844 frames = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES]);
845 if ((frames & BIT(IEEE802154_FC_TYPE_MAC_CMD)) &&
846 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS])
847 return -EINVAL;
848
849 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) {
850 nla_memcpy(commands,
851 info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS],
852 256 / 8);
853
854 if (commands[0] || commands[1] || commands[2] || commands[3] ||
855 commands[4] || commands[5] || commands[6] ||
856 commands[7] >= BIT(IEEE802154_CMD_GTS_REQ + 1))
857 return -EINVAL;
858
859 key->cmd_frame_ids = commands[7];
860 }
861
862 key->frame_types = frames;
863
864 nla_memcpy(key->key, info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES],
865 IEEE802154_LLSEC_KEY_SIZE);
866
867 return 0;
868}
869
870static int llsec_add_key(struct net_device *dev, struct genl_info *info)
871{
872 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
873 struct ieee802154_llsec_key key;
874 struct ieee802154_llsec_key_id id;
875
876 if (ieee802154_llsec_parse_key(info, &key) ||
877 ieee802154_llsec_parse_key_id(info, &id))
878 return -EINVAL;
879
880 return ops->llsec->add_key(dev, &id, &key);
881}
882
883int ieee802154_llsec_add_key(struct sk_buff *skb, struct genl_info *info)
884{
885 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
886 (NLM_F_CREATE | NLM_F_EXCL))
887 return -EINVAL;
888
889 return ieee802154_nl_llsec_change(skb, info, llsec_add_key);
890}
891
892static int llsec_remove_key(struct net_device *dev, struct genl_info *info)
893{
894 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
895 struct ieee802154_llsec_key_id id;
896
897 if (ieee802154_llsec_parse_key_id(info, &id))
898 return -EINVAL;
899
900 return ops->llsec->del_key(dev, &id);
901}
902
903int ieee802154_llsec_del_key(struct sk_buff *skb, struct genl_info *info)
904{
905 return ieee802154_nl_llsec_change(skb, info, llsec_remove_key);
906}
907
908static int
909ieee802154_nl_fill_key(struct sk_buff *msg, u32 portid, u32 seq,
910 const struct ieee802154_llsec_key_entry *key,
911 const struct net_device *dev)
912{
913 void *hdr;
914 u32 commands[256 / 32];
915
916 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
917 IEEE802154_LLSEC_LIST_KEY);
918 if (!hdr)
919 goto out;
920
921 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
922 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
923 ieee802154_llsec_fill_key_id(msg, &key->id) ||
924 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES,
925 key->key->frame_types))
926 goto nla_put_failure;
927
928 if (key->key->frame_types & BIT(IEEE802154_FC_TYPE_MAC_CMD)) {
929 memset(commands, 0, sizeof(commands));
930 commands[7] = key->key->cmd_frame_ids;
931 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS,
932 sizeof(commands), commands))
933 goto nla_put_failure;
934 }
935
936 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_BYTES,
937 IEEE802154_LLSEC_KEY_SIZE, key->key->key))
938 goto nla_put_failure;
939
940 genlmsg_end(msg, hdr);
941 return 0;
942
943nla_put_failure:
944 genlmsg_cancel(msg, hdr);
945out:
946 return -EMSGSIZE;
947}
948
949static int llsec_iter_keys(struct llsec_dump_data *data)
950{
951 struct ieee802154_llsec_key_entry *pos;
952 int rc = 0, idx = 0;
953
954 list_for_each_entry(pos, &data->table->keys, list) {
955 if (idx++ < data->s_idx)
956 continue;
957
958 if (ieee802154_nl_fill_key(data->skb, data->portid,
959 data->nlmsg_seq, pos, data->dev)) {
960 rc = -EMSGSIZE;
961 break;
962 }
963
964 data->s_idx++;
965 }
966
967 return rc;
968}
969
970int ieee802154_llsec_dump_keys(struct sk_buff *skb, struct netlink_callback *cb)
971{
972 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_keys);
973}
974
975
976
977static int
978llsec_parse_dev(struct genl_info *info,
979 struct ieee802154_llsec_device *dev)
980{
981 memset(dev, 0, sizeof(*dev));
982
983 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
984 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
985 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE] ||
986 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE] ||
987 (!!info->attrs[IEEE802154_ATTR_PAN_ID] !=
988 !!info->attrs[IEEE802154_ATTR_SHORT_ADDR]))
989 return -EINVAL;
990
991 if (info->attrs[IEEE802154_ATTR_PAN_ID]) {
992 dev->pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
993 dev->short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
994 } else {
995 dev->short_addr = cpu_to_le16(IEEE802154_ADDR_UNDEF);
996 }
997
998 dev->hwaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
999 dev->frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1000 dev->seclevel_exempt = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1001 dev->key_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE]);
1002
1003 if (dev->key_mode >= __IEEE802154_LLSEC_DEVKEY_MAX)
1004 return -EINVAL;
1005
1006 return 0;
1007}
1008
1009static int llsec_add_dev(struct net_device *dev, struct genl_info *info)
1010{
1011 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1012 struct ieee802154_llsec_device desc;
1013
1014 if (llsec_parse_dev(info, &desc))
1015 return -EINVAL;
1016
1017 return ops->llsec->add_dev(dev, &desc);
1018}
1019
1020int ieee802154_llsec_add_dev(struct sk_buff *skb, struct genl_info *info)
1021{
1022 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1023 (NLM_F_CREATE | NLM_F_EXCL))
1024 return -EINVAL;
1025
1026 return ieee802154_nl_llsec_change(skb, info, llsec_add_dev);
1027}
1028
1029static int llsec_del_dev(struct net_device *dev, struct genl_info *info)
1030{
1031 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1032 __le64 devaddr;
1033
1034 if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
1035 return -EINVAL;
1036
1037 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1038
1039 return ops->llsec->del_dev(dev, devaddr);
1040}
1041
1042int ieee802154_llsec_del_dev(struct sk_buff *skb, struct genl_info *info)
1043{
1044 return ieee802154_nl_llsec_change(skb, info, llsec_del_dev);
1045}
1046
1047static int
1048ieee802154_nl_fill_dev(struct sk_buff *msg, u32 portid, u32 seq,
1049 const struct ieee802154_llsec_device *desc,
1050 const struct net_device *dev)
1051{
1052 void *hdr;
1053
1054 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1055 IEEE802154_LLSEC_LIST_DEV);
1056 if (!hdr)
1057 goto out;
1058
1059 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1060 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1061 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, desc->pan_id) ||
1062 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
1063 desc->short_addr) ||
1064 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, desc->hwaddr) ||
1065 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1066 desc->frame_counter) ||
1067 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1068 desc->seclevel_exempt) ||
1069 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE, desc->key_mode))
1070 goto nla_put_failure;
1071
1072 genlmsg_end(msg, hdr);
1073 return 0;
1074
1075nla_put_failure:
1076 genlmsg_cancel(msg, hdr);
1077out:
1078 return -EMSGSIZE;
1079}
1080
1081static int llsec_iter_devs(struct llsec_dump_data *data)
1082{
1083 struct ieee802154_llsec_device *pos;
1084 int rc = 0, idx = 0;
1085
1086 list_for_each_entry(pos, &data->table->devices, list) {
1087 if (idx++ < data->s_idx)
1088 continue;
1089
1090 if (ieee802154_nl_fill_dev(data->skb, data->portid,
1091 data->nlmsg_seq, pos, data->dev)) {
1092 rc = -EMSGSIZE;
1093 break;
1094 }
1095
1096 data->s_idx++;
1097 }
1098
1099 return rc;
1100}
1101
1102int ieee802154_llsec_dump_devs(struct sk_buff *skb, struct netlink_callback *cb)
1103{
1104 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devs);
1105}
1106
1107
1108
1109static int llsec_add_devkey(struct net_device *dev, struct genl_info *info)
1110{
1111 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1112 struct ieee802154_llsec_device_key key;
1113 __le64 devaddr;
1114
1115 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
1116 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1117 ieee802154_llsec_parse_key_id(info, &key.key_id))
1118 return -EINVAL;
1119
1120 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1121 key.frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1122
1123 return ops->llsec->add_devkey(dev, devaddr, &key);
1124}
1125
1126int ieee802154_llsec_add_devkey(struct sk_buff *skb, struct genl_info *info)
1127{
1128 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1129 (NLM_F_CREATE | NLM_F_EXCL))
1130 return -EINVAL;
1131
1132 return ieee802154_nl_llsec_change(skb, info, llsec_add_devkey);
1133}
1134
1135static int llsec_del_devkey(struct net_device *dev, struct genl_info *info)
1136{
1137 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1138 struct ieee802154_llsec_device_key key;
1139 __le64 devaddr;
1140
1141 if (!info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1142 ieee802154_llsec_parse_key_id(info, &key.key_id))
1143 return -EINVAL;
1144
1145 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1146
1147 return ops->llsec->del_devkey(dev, devaddr, &key);
1148}
1149
1150int ieee802154_llsec_del_devkey(struct sk_buff *skb, struct genl_info *info)
1151{
1152 return ieee802154_nl_llsec_change(skb, info, llsec_del_devkey);
1153}
1154
1155static int
1156ieee802154_nl_fill_devkey(struct sk_buff *msg, u32 portid, u32 seq,
1157 __le64 devaddr,
1158 const struct ieee802154_llsec_device_key *devkey,
1159 const struct net_device *dev)
1160{
1161 void *hdr;
1162
1163 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1164 IEEE802154_LLSEC_LIST_DEVKEY);
1165 if (!hdr)
1166 goto out;
1167
1168 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1169 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1170 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, devaddr) ||
1171 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1172 devkey->frame_counter) ||
1173 ieee802154_llsec_fill_key_id(msg, &devkey->key_id))
1174 goto nla_put_failure;
1175
1176 genlmsg_end(msg, hdr);
1177 return 0;
1178
1179nla_put_failure:
1180 genlmsg_cancel(msg, hdr);
1181out:
1182 return -EMSGSIZE;
1183}
1184
1185static int llsec_iter_devkeys(struct llsec_dump_data *data)
1186{
1187 struct ieee802154_llsec_device *dpos;
1188 struct ieee802154_llsec_device_key *kpos;
1189 int rc = 0, idx = 0, idx2;
1190
1191 list_for_each_entry(dpos, &data->table->devices, list) {
1192 if (idx++ < data->s_idx)
1193 continue;
1194
1195 idx2 = 0;
1196
1197 list_for_each_entry(kpos, &dpos->keys, list) {
1198 if (idx2++ < data->s_idx2)
1199 continue;
1200
1201 if (ieee802154_nl_fill_devkey(data->skb, data->portid,
1202 data->nlmsg_seq,
1203 dpos->hwaddr, kpos,
1204 data->dev)) {
1205 return rc = -EMSGSIZE;
1206 }
1207
1208 data->s_idx2++;
1209 }
1210
1211 data->s_idx++;
1212 }
1213
1214 return rc;
1215}
1216
1217int ieee802154_llsec_dump_devkeys(struct sk_buff *skb,
1218 struct netlink_callback *cb)
1219{
1220 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devkeys);
1221}
1222
1223
1224
1225static int
1226llsec_parse_seclevel(struct genl_info *info,
1227 struct ieee802154_llsec_seclevel *sl)
1228{
1229 memset(sl, 0, sizeof(*sl));
1230
1231 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE] ||
1232 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS] ||
1233 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE])
1234 return -EINVAL;
1235
1236 sl->frame_type = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE]);
1237 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD) {
1238 if (!info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID])
1239 return -EINVAL;
1240
1241 sl->cmd_frame_id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]);
1242 }
1243
1244 sl->sec_levels = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS]);
1245 sl->device_override = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1246
1247 return 0;
1248}
1249
1250static int llsec_add_seclevel(struct net_device *dev, struct genl_info *info)
1251{
1252 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1253 struct ieee802154_llsec_seclevel sl;
1254
1255 if (llsec_parse_seclevel(info, &sl))
1256 return -EINVAL;
1257
1258 return ops->llsec->add_seclevel(dev, &sl);
1259}
1260
1261int ieee802154_llsec_add_seclevel(struct sk_buff *skb, struct genl_info *info)
1262{
1263 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1264 (NLM_F_CREATE | NLM_F_EXCL))
1265 return -EINVAL;
1266
1267 return ieee802154_nl_llsec_change(skb, info, llsec_add_seclevel);
1268}
1269
1270static int llsec_del_seclevel(struct net_device *dev, struct genl_info *info)
1271{
1272 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1273 struct ieee802154_llsec_seclevel sl;
1274
1275 if (llsec_parse_seclevel(info, &sl))
1276 return -EINVAL;
1277
1278 return ops->llsec->del_seclevel(dev, &sl);
1279}
1280
1281int ieee802154_llsec_del_seclevel(struct sk_buff *skb, struct genl_info *info)
1282{
1283 return ieee802154_nl_llsec_change(skb, info, llsec_del_seclevel);
1284}
1285
1286static int
1287ieee802154_nl_fill_seclevel(struct sk_buff *msg, u32 portid, u32 seq,
1288 const struct ieee802154_llsec_seclevel *sl,
1289 const struct net_device *dev)
1290{
1291 void *hdr;
1292
1293 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1294 IEEE802154_LLSEC_LIST_SECLEVEL);
1295 if (!hdr)
1296 goto out;
1297
1298 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1299 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1300 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_FRAME_TYPE, sl->frame_type) ||
1301 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVELS, sl->sec_levels) ||
1302 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1303 sl->device_override))
1304 goto nla_put_failure;
1305
1306 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD &&
1307 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID,
1308 sl->cmd_frame_id))
1309 goto nla_put_failure;
1310
1311 genlmsg_end(msg, hdr);
1312 return 0;
1313
1314nla_put_failure:
1315 genlmsg_cancel(msg, hdr);
1316out:
1317 return -EMSGSIZE;
1318}
1319
1320static int llsec_iter_seclevels(struct llsec_dump_data *data)
1321{
1322 struct ieee802154_llsec_seclevel *pos;
1323 int rc = 0, idx = 0;
1324
1325 list_for_each_entry(pos, &data->table->security_levels, list) {
1326 if (idx++ < data->s_idx)
1327 continue;
1328
1329 if (ieee802154_nl_fill_seclevel(data->skb, data->portid,
1330 data->nlmsg_seq, pos,
1331 data->dev)) {
1332 rc = -EMSGSIZE;
1333 break;
1334 }
1335
1336 data->s_idx++;
1337 }
1338
1339 return rc;
1340}
1341
1342int ieee802154_llsec_dump_seclevels(struct sk_buff *skb,
1343 struct netlink_callback *cb)
1344{
1345 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_seclevels);
1346}