blob: 3503c38954f9f9e09e88706357c838a37b46ee17 [file] [log] [blame]
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +04001/*
Varka Bhadram69bb6312014-11-25 10:04:57 +05302 * Netlink interface for IEEE 802.15.4 stack
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +04003 *
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}
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040079
Eric W. Biederman15e47302012-09-07 20:12:54 +000080static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid,
Varka Bhadram4710d802014-07-02 09:01:09 +053081 u32 seq, int flags, struct net_device *dev)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040082{
83 void *hdr;
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +030084 struct wpan_phy *phy;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +020085 struct ieee802154_mlme_ops *ops;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +010086 __le16 short_addr, pan_id;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040087
88 pr_debug("%s\n", __func__);
89
90 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
Varka Bhadram4710d802014-07-02 09:01:09 +053091 IEEE802154_LIST_IFACE);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040092 if (!hdr)
93 goto out;
94
Phoebe Buckheistere462ded2014-03-31 21:37:46 +020095 ops = ieee802154_mlme_ops(dev);
Alexander Aringbd28a112014-11-05 20:51:18 +010096 phy = dev->ieee802154_ptr->wpan_phy;
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +030097 BUG_ON(!phy);
Alexander Aringbd28a112014-11-05 20:51:18 +010098 get_device(&phy->dev);
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +030099
Alexander Aringc947f7e2015-05-22 17:43:54 +0200100 rtnl_lock();
101 short_addr = dev->ieee802154_ptr->short_addr;
102 pan_id = dev->ieee802154_ptr->pan_id;
103 rtnl_unlock();
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100104
David S. Millerbe51da02012-04-01 20:45:25 -0400105 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
106 nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
107 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
108 nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
109 dev->dev_addr) ||
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100110 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) ||
111 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, pan_id))
David S. Millerbe51da02012-04-01 20:45:25 -0400112 goto nla_put_failure;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200113
114 if (ops->get_mac_params) {
115 struct ieee802154_mac_params params;
116
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100117 rtnl_lock();
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200118 ops->get_mac_params(dev, &params);
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100119 rtnl_unlock();
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200120
121 if (nla_put_s8(msg, IEEE802154_ATTR_TXPOWER,
Alexander Aringe2eb1732015-05-17 21:44:40 +0200122 params.transmit_power / 100) ||
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200123 nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, params.lbt) ||
124 nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE,
Alexander Aring7fe9a382014-12-10 15:33:12 +0100125 params.cca.mode) ||
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200126 nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL,
Alexander Aring32b23552015-05-17 21:44:41 +0200127 params.cca_ed_level / 100) ||
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200128 nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES,
129 params.csma_retries) ||
130 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE,
131 params.min_be) ||
132 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE,
133 params.max_be) ||
134 nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES,
135 params.frame_retries))
136 goto nla_put_failure;
137 }
138
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +0300139 wpan_phy_put(phy);
Johannes Berg053c0952015-01-16 22:09:00 +0100140 genlmsg_end(msg, hdr);
141 return 0;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400142
143nla_put_failure:
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +0300144 wpan_phy_put(phy);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400145 genlmsg_cancel(msg, hdr);
146out:
147 return -EMSGSIZE;
148}
149
150/* Requests from userspace */
151static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
152{
153 struct net_device *dev;
154
155 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
156 char name[IFNAMSIZ + 1];
Varka Bhadram4710d802014-07-02 09:01:09 +0530157
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400158 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
Varka Bhadram4710d802014-07-02 09:01:09 +0530159 sizeof(name));
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400160 dev = dev_get_by_name(&init_net, name);
Varka Bhadram4710d802014-07-02 09:01:09 +0530161 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX]) {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400162 dev = dev_get_by_index(&init_net,
163 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
Varka Bhadram4710d802014-07-02 09:01:09 +0530164 } else {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400165 return NULL;
Varka Bhadram4710d802014-07-02 09:01:09 +0530166 }
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400167
168 if (!dev)
169 return NULL;
170
Lennert Buytenhek01c8d2b2015-05-25 15:38:39 +0300171 if (dev->type != ARPHRD_IEEE802154) {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400172 dev_put(dev);
173 return NULL;
174 }
175
176 return dev;
177}
178
Johannes Berg1c582d92013-11-14 17:14:41 +0100179int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400180{
181 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100182 struct ieee802154_addr addr;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400183 u8 page;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000184 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400185
186 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
187 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
188 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
189 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
190 !info->attrs[IEEE802154_ATTR_CAPABILITY])
191 return -EINVAL;
192
193 dev = ieee802154_nl_get_dev(info);
194 if (!dev)
195 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000196 if (!ieee802154_mlme_ops(dev)->assoc_req)
197 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400198
199 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100200 addr.mode = IEEE802154_ADDR_LONG;
201 addr.extended_addr = nla_get_hwaddr(
202 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400203 } else {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100204 addr.mode = IEEE802154_ADDR_SHORT;
205 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400206 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
207 }
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100208 addr.pan_id = nla_get_shortaddr(
209 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400210
211 if (info->attrs[IEEE802154_ATTR_PAGE])
212 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
213 else
214 page = 0;
215
216 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
217 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
218 page,
219 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
220
Werner Almesberger56aa0912013-04-04 06:32:35 +0000221out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400222 dev_put(dev);
223 return ret;
224}
225
Johannes Berg1c582d92013-11-14 17:14:41 +0100226int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400227{
228 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100229 struct ieee802154_addr addr;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000230 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400231
232 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
233 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
234 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
235 return -EINVAL;
236
237 dev = ieee802154_nl_get_dev(info);
238 if (!dev)
239 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000240 if (!ieee802154_mlme_ops(dev)->assoc_resp)
241 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400242
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100243 addr.mode = IEEE802154_ADDR_LONG;
244 addr.extended_addr = nla_get_hwaddr(
245 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
Alexander Aringc947f7e2015-05-22 17:43:54 +0200246 rtnl_lock();
247 addr.pan_id = dev->ieee802154_ptr->pan_id;
248 rtnl_unlock();
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400249
250 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100251 nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400252 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
253
Werner Almesberger56aa0912013-04-04 06:32:35 +0000254out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400255 dev_put(dev);
256 return ret;
257}
258
Johannes Berg1c582d92013-11-14 17:14:41 +0100259int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400260{
261 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100262 struct ieee802154_addr addr;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000263 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400264
265 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
Varka Bhadram4710d802014-07-02 09:01:09 +0530266 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400267 !info->attrs[IEEE802154_ATTR_REASON])
268 return -EINVAL;
269
270 dev = ieee802154_nl_get_dev(info);
271 if (!dev)
272 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000273 if (!ieee802154_mlme_ops(dev)->disassoc_req)
274 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400275
276 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100277 addr.mode = IEEE802154_ADDR_LONG;
278 addr.extended_addr = nla_get_hwaddr(
279 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400280 } else {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100281 addr.mode = IEEE802154_ADDR_SHORT;
282 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400283 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
284 }
Alexander Aringc947f7e2015-05-22 17:43:54 +0200285 rtnl_lock();
286 addr.pan_id = dev->ieee802154_ptr->pan_id;
287 rtnl_unlock();
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400288
289 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
290 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
291
Werner Almesberger56aa0912013-04-04 06:32:35 +0000292out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400293 dev_put(dev);
294 return ret;
295}
296
Varka Bhadram4710d802014-07-02 09:01:09 +0530297/* PANid, channel, beacon_order = 15, superframe_order = 15,
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400298 * PAN_coordinator, battery_life_extension = 0,
299 * coord_realignment = 0, security_enable = 0
300*/
Johannes Berg1c582d92013-11-14 17:14:41 +0100301int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400302{
303 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100304 struct ieee802154_addr addr;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400305
306 u8 channel, bcn_ord, sf_ord;
307 u8 page;
308 int pan_coord, blx, coord_realign;
Alexander Aring8f499f92014-11-02 04:18:39 +0100309 int ret = -EBUSY;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400310
311 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
312 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
313 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
314 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
315 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
316 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
317 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
318 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
319 )
320 return -EINVAL;
321
322 dev = ieee802154_nl_get_dev(info);
323 if (!dev)
324 return -ENODEV;
Alexander Aring8f499f92014-11-02 04:18:39 +0100325
326 if (netif_running(dev))
Werner Almesberger56aa0912013-04-04 06:32:35 +0000327 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400328
Alexander Aring8f499f92014-11-02 04:18:39 +0100329 if (!ieee802154_mlme_ops(dev)->start_req) {
330 ret = -EOPNOTSUPP;
331 goto out;
332 }
333
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100334 addr.mode = IEEE802154_ADDR_SHORT;
335 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400336 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100337 addr.pan_id = nla_get_shortaddr(
338 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400339
340 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
341 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
342 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
343 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
344 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
345 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
346
347 if (info->attrs[IEEE802154_ATTR_PAGE])
348 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
349 else
350 page = 0;
351
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100352 if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400353 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
354 dev_put(dev);
355 return -EINVAL;
356 }
357
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100358 rtnl_lock();
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400359 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
360 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100361 rtnl_unlock();
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400362
Alexander Aring9f3295b2014-11-05 20:51:13 +0100363 /* FIXME: add validation for unused parameters to be sane
364 * for SoftMAC
365 */
366 ieee802154_nl_start_confirm(dev, IEEE802154_SUCCESS);
367
Werner Almesberger56aa0912013-04-04 06:32:35 +0000368out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400369 dev_put(dev);
370 return ret;
371}
372
Johannes Berg1c582d92013-11-14 17:14:41 +0100373int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400374{
375 struct net_device *dev;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000376 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400377 u8 type;
378 u32 channels;
379 u8 duration;
380 u8 page;
381
382 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
383 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
384 !info->attrs[IEEE802154_ATTR_DURATION])
385 return -EINVAL;
386
387 dev = ieee802154_nl_get_dev(info);
388 if (!dev)
389 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000390 if (!ieee802154_mlme_ops(dev)->scan_req)
391 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400392
393 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
394 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
395 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
396
397 if (info->attrs[IEEE802154_ATTR_PAGE])
398 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
399 else
400 page = 0;
401
Varka Bhadram4710d802014-07-02 09:01:09 +0530402 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
403 page, duration);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400404
Werner Almesberger56aa0912013-04-04 06:32:35 +0000405out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400406 dev_put(dev);
407 return ret;
408}
409
Johannes Berg1c582d92013-11-14 17:14:41 +0100410int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400411{
412 /* Request for interface name, index, type, IEEE address,
Varka Bhadram4710d802014-07-02 09:01:09 +0530413 * PAN Id, short address
414 */
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400415 struct sk_buff *msg;
416 struct net_device *dev = NULL;
417 int rc = -ENOBUFS;
418
419 pr_debug("%s\n", __func__);
420
421 dev = ieee802154_nl_get_dev(info);
422 if (!dev)
423 return -ENODEV;
424
Thomas Graf58050fc2012-06-28 03:57:45 +0000425 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400426 if (!msg)
427 goto out_dev;
428
Eric W. Biederman15e47302012-09-07 20:12:54 +0000429 rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq,
Varka Bhadram4710d802014-07-02 09:01:09 +0530430 0, dev);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400431 if (rc < 0)
432 goto out_free;
433
434 dev_put(dev);
435
436 return genlmsg_reply(msg, info);
437out_free:
438 nlmsg_free(msg);
439out_dev:
440 dev_put(dev);
441 return rc;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400442}
443
Johannes Berg1c582d92013-11-14 17:14:41 +0100444int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400445{
446 struct net *net = sock_net(skb->sk);
447 struct net_device *dev;
448 int idx;
449 int s_idx = cb->args[0];
450
451 pr_debug("%s\n", __func__);
452
453 idx = 0;
454 for_each_netdev(net, dev) {
Lennert Buytenhek01c8d2b2015-05-25 15:38:39 +0300455 if (idx < s_idx || dev->type != ARPHRD_IEEE802154)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400456 goto cont;
457
Eric W. Biederman15e47302012-09-07 20:12:54 +0000458 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid,
Varka Bhadram4710d802014-07-02 09:01:09 +0530459 cb->nlh->nlmsg_seq,
460 NLM_F_MULTI, dev) < 0)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400461 break;
462cont:
463 idx++;
464 }
465 cb->args[0] = idx;
466
467 return skb->len;
468}
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200469
470int ieee802154_set_macparams(struct sk_buff *skb, struct genl_info *info)
471{
472 struct net_device *dev = NULL;
473 struct ieee802154_mlme_ops *ops;
474 struct ieee802154_mac_params params;
475 struct wpan_phy *phy;
476 int rc = -EINVAL;
477
478 pr_debug("%s\n", __func__);
479
480 dev = ieee802154_nl_get_dev(info);
481 if (!dev)
482 return -ENODEV;
483
484 ops = ieee802154_mlme_ops(dev);
485
486 if (!ops->get_mac_params || !ops->set_mac_params) {
487 rc = -EOPNOTSUPP;
488 goto out;
489 }
490
491 if (netif_running(dev)) {
492 rc = -EBUSY;
493 goto out;
494 }
495
496 if (!info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
497 !info->attrs[IEEE802154_ATTR_CCA_MODE] &&
498 !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] &&
499 !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] &&
500 !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] &&
501 !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] &&
502 !info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
503 goto out;
504
Alexander Aringbd28a112014-11-05 20:51:18 +0100505 phy = dev->ieee802154_ptr->wpan_phy;
506 get_device(&phy->dev);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200507
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100508 rtnl_lock();
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200509 ops->get_mac_params(dev, &params);
510
511 if (info->attrs[IEEE802154_ATTR_TXPOWER])
Alexander Aringe2eb1732015-05-17 21:44:40 +0200512 params.transmit_power = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]) * 100;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200513
514 if (info->attrs[IEEE802154_ATTR_LBT_ENABLED])
515 params.lbt = nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
516
517 if (info->attrs[IEEE802154_ATTR_CCA_MODE])
Alexander Aring7fe9a382014-12-10 15:33:12 +0100518 params.cca.mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200519
520 if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])
Alexander Aring32b23552015-05-17 21:44:41 +0200521 params.cca_ed_level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]) * 100;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200522
523 if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES])
524 params.csma_retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]);
525
526 if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE])
527 params.min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]);
528
529 if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE])
530 params.max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]);
531
532 if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
533 params.frame_retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]);
534
535 rc = ops->set_mac_params(dev, &params);
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100536 rtnl_unlock();
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200537
538 wpan_phy_put(phy);
539 dev_put(dev);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200540
Alexander Aringa543c592014-10-28 18:21:23 +0100541 return 0;
542
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200543out:
544 dev_put(dev);
545 return rc;
546}
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200547
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200548static int
549ieee802154_llsec_parse_key_id(struct genl_info *info,
550 struct ieee802154_llsec_key_id *desc)
551{
552 memset(desc, 0, sizeof(*desc));
553
554 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE])
555 return -EINVAL;
556
557 desc->mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]);
558
559 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
560 if (!info->attrs[IEEE802154_ATTR_PAN_ID] &&
561 !(info->attrs[IEEE802154_ATTR_SHORT_ADDR] ||
562 info->attrs[IEEE802154_ATTR_HW_ADDR]))
563 return -EINVAL;
564
565 desc->device_addr.pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
566
567 if (info->attrs[IEEE802154_ATTR_SHORT_ADDR]) {
568 desc->device_addr.mode = IEEE802154_ADDR_SHORT;
569 desc->device_addr.short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
570 } else {
571 desc->device_addr.mode = IEEE802154_ADDR_LONG;
572 desc->device_addr.extended_addr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
573 }
574 }
575
576 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
577 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID])
578 return -EINVAL;
579
580 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
581 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT])
582 return -EINVAL;
583
584 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
585 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED])
586 return -EINVAL;
587
588 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT)
589 desc->id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]);
590
591 switch (desc->mode) {
592 case IEEE802154_SCF_KEY_SHORT_INDEX:
593 {
594 u32 source = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]);
Varka Bhadram4710d802014-07-02 09:01:09 +0530595
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200596 desc->short_source = cpu_to_le32(source);
597 break;
598 }
599 case IEEE802154_SCF_KEY_HW_INDEX:
600 desc->extended_source = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]);
601 break;
602 }
603
604 return 0;
605}
606
607static int
608ieee802154_llsec_fill_key_id(struct sk_buff *msg,
609 const struct ieee802154_llsec_key_id *desc)
610{
611 if (nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_MODE, desc->mode))
612 return -EMSGSIZE;
613
614 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
615 if (nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID,
616 desc->device_addr.pan_id))
617 return -EMSGSIZE;
618
619 if (desc->device_addr.mode == IEEE802154_ADDR_SHORT &&
620 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
621 desc->device_addr.short_addr))
622 return -EMSGSIZE;
623
624 if (desc->device_addr.mode == IEEE802154_ADDR_LONG &&
625 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR,
626 desc->device_addr.extended_addr))
627 return -EMSGSIZE;
628 }
629
630 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
631 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_ID, desc->id))
632 return -EMSGSIZE;
633
634 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
635 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT,
636 le32_to_cpu(desc->short_source)))
637 return -EMSGSIZE;
638
639 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
640 nla_put_hwaddr(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED,
641 desc->extended_source))
642 return -EMSGSIZE;
643
644 return 0;
645}
646
647int ieee802154_llsec_getparams(struct sk_buff *skb, struct genl_info *info)
648{
649 struct sk_buff *msg;
650 struct net_device *dev = NULL;
651 int rc = -ENOBUFS;
652 struct ieee802154_mlme_ops *ops;
653 void *hdr;
654 struct ieee802154_llsec_params params;
655
656 pr_debug("%s\n", __func__);
657
658 dev = ieee802154_nl_get_dev(info);
659 if (!dev)
660 return -ENODEV;
661
662 ops = ieee802154_mlme_ops(dev);
Dan Carpenterb3f7a7b2014-05-22 10:53:06 +0300663 if (!ops->llsec) {
664 rc = -EOPNOTSUPP;
665 goto out_dev;
666 }
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200667
668 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
669 if (!msg)
670 goto out_dev;
671
672 hdr = genlmsg_put(msg, 0, info->snd_seq, &nl802154_family, 0,
Varka Bhadram4710d802014-07-02 09:01:09 +0530673 IEEE802154_LLSEC_GETPARAMS);
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200674 if (!hdr)
675 goto out_free;
676
677 rc = ops->llsec->get_params(dev, &params);
678 if (rc < 0)
679 goto out_free;
680
681 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
682 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
683 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_ENABLED, params.enabled) ||
684 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVEL, params.out_level) ||
685 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
686 be32_to_cpu(params.frame_counter)) ||
687 ieee802154_llsec_fill_key_id(msg, &params.out_key))
688 goto out_free;
689
690 dev_put(dev);
691
692 return ieee802154_nl_reply(msg, info);
693out_free:
694 nlmsg_free(msg);
695out_dev:
696 dev_put(dev);
697 return rc;
698}
699
700int ieee802154_llsec_setparams(struct sk_buff *skb, struct genl_info *info)
701{
702 struct net_device *dev = NULL;
703 int rc = -EINVAL;
704 struct ieee802154_mlme_ops *ops;
705 struct ieee802154_llsec_params params;
706 int changed = 0;
707
708 pr_debug("%s\n", __func__);
709
710 dev = ieee802154_nl_get_dev(info);
711 if (!dev)
712 return -ENODEV;
713
714 if (!info->attrs[IEEE802154_ATTR_LLSEC_ENABLED] &&
715 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE] &&
716 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL])
717 goto out;
718
719 ops = ieee802154_mlme_ops(dev);
720 if (!ops->llsec) {
721 rc = -EOPNOTSUPP;
722 goto out;
723 }
724
725 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL] &&
726 nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) > 7)
727 goto out;
728
729 if (info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]) {
730 params.enabled = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]);
731 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
732 }
733
734 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) {
735 if (ieee802154_llsec_parse_key_id(info, &params.out_key))
736 goto out;
737
738 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
739 }
740
741 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) {
742 params.out_level = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]);
743 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
744 }
745
746 if (info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]) {
747 u32 fc = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
748
749 params.frame_counter = cpu_to_be32(fc);
750 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
751 }
752
753 rc = ops->llsec->set_params(dev, &params, changed);
754
755 dev_put(dev);
756
757 return rc;
758out:
759 dev_put(dev);
760 return rc;
761}
762
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200763struct llsec_dump_data {
764 struct sk_buff *skb;
765 int s_idx, s_idx2;
766 int portid;
767 int nlmsg_seq;
768 struct net_device *dev;
769 struct ieee802154_mlme_ops *ops;
770 struct ieee802154_llsec_table *table;
771};
772
773static int
774ieee802154_llsec_dump_table(struct sk_buff *skb, struct netlink_callback *cb,
Varka Bhadram4710d802014-07-02 09:01:09 +0530775 int (*step)(struct llsec_dump_data *))
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200776{
777 struct net *net = sock_net(skb->sk);
778 struct net_device *dev;
779 struct llsec_dump_data data;
780 int idx = 0;
781 int first_dev = cb->args[0];
782 int rc;
783
784 for_each_netdev(net, dev) {
Lennert Buytenhek01c8d2b2015-05-25 15:38:39 +0300785 if (idx < first_dev || dev->type != ARPHRD_IEEE802154)
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200786 goto skip;
787
788 data.ops = ieee802154_mlme_ops(dev);
789 if (!data.ops->llsec)
790 goto skip;
791
792 data.skb = skb;
793 data.s_idx = cb->args[1];
794 data.s_idx2 = cb->args[2];
795 data.dev = dev;
796 data.portid = NETLINK_CB(cb->skb).portid;
797 data.nlmsg_seq = cb->nlh->nlmsg_seq;
798
799 data.ops->llsec->lock_table(dev);
800 data.ops->llsec->get_table(data.dev, &data.table);
801 rc = step(&data);
802 data.ops->llsec->unlock_table(dev);
803
804 if (rc < 0)
805 break;
806
807skip:
808 idx++;
809 }
810 cb->args[0] = idx;
811
812 return skb->len;
813}
814
815static int
816ieee802154_nl_llsec_change(struct sk_buff *skb, struct genl_info *info,
817 int (*fn)(struct net_device*, struct genl_info*))
818{
819 struct net_device *dev = NULL;
820 int rc = -EINVAL;
821
822 dev = ieee802154_nl_get_dev(info);
823 if (!dev)
824 return -ENODEV;
825
826 if (!ieee802154_mlme_ops(dev)->llsec)
827 rc = -EOPNOTSUPP;
828 else
829 rc = fn(dev, info);
830
831 dev_put(dev);
832 return rc;
833}
834
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200835static int
836ieee802154_llsec_parse_key(struct genl_info *info,
837 struct ieee802154_llsec_key *key)
838{
839 u8 frames;
840 u32 commands[256 / 32];
841
842 memset(key, 0, sizeof(*key));
843
844 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES] ||
845 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES])
846 return -EINVAL;
847
848 frames = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES]);
849 if ((frames & BIT(IEEE802154_FC_TYPE_MAC_CMD)) &&
850 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS])
851 return -EINVAL;
852
853 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) {
854 nla_memcpy(commands,
855 info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS],
856 256 / 8);
857
858 if (commands[0] || commands[1] || commands[2] || commands[3] ||
859 commands[4] || commands[5] || commands[6] ||
860 commands[7] >= BIT(IEEE802154_CMD_GTS_REQ + 1))
861 return -EINVAL;
862
863 key->cmd_frame_ids = commands[7];
864 }
865
866 key->frame_types = frames;
867
868 nla_memcpy(key->key, info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES],
869 IEEE802154_LLSEC_KEY_SIZE);
870
871 return 0;
872}
873
874static int llsec_add_key(struct net_device *dev, struct genl_info *info)
875{
876 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
877 struct ieee802154_llsec_key key;
878 struct ieee802154_llsec_key_id id;
879
880 if (ieee802154_llsec_parse_key(info, &key) ||
881 ieee802154_llsec_parse_key_id(info, &id))
882 return -EINVAL;
883
884 return ops->llsec->add_key(dev, &id, &key);
885}
886
887int ieee802154_llsec_add_key(struct sk_buff *skb, struct genl_info *info)
888{
889 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
890 (NLM_F_CREATE | NLM_F_EXCL))
891 return -EINVAL;
892
893 return ieee802154_nl_llsec_change(skb, info, llsec_add_key);
894}
895
896static int llsec_remove_key(struct net_device *dev, struct genl_info *info)
897{
898 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
899 struct ieee802154_llsec_key_id id;
900
901 if (ieee802154_llsec_parse_key_id(info, &id))
902 return -EINVAL;
903
904 return ops->llsec->del_key(dev, &id);
905}
906
907int ieee802154_llsec_del_key(struct sk_buff *skb, struct genl_info *info)
908{
909 return ieee802154_nl_llsec_change(skb, info, llsec_remove_key);
910}
911
912static int
913ieee802154_nl_fill_key(struct sk_buff *msg, u32 portid, u32 seq,
914 const struct ieee802154_llsec_key_entry *key,
915 const struct net_device *dev)
916{
917 void *hdr;
918 u32 commands[256 / 32];
919
920 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
921 IEEE802154_LLSEC_LIST_KEY);
922 if (!hdr)
923 goto out;
924
925 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
926 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
927 ieee802154_llsec_fill_key_id(msg, &key->id) ||
928 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES,
929 key->key->frame_types))
930 goto nla_put_failure;
931
932 if (key->key->frame_types & BIT(IEEE802154_FC_TYPE_MAC_CMD)) {
933 memset(commands, 0, sizeof(commands));
934 commands[7] = key->key->cmd_frame_ids;
935 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS,
936 sizeof(commands), commands))
937 goto nla_put_failure;
938 }
939
940 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_BYTES,
941 IEEE802154_LLSEC_KEY_SIZE, key->key->key))
942 goto nla_put_failure;
943
944 genlmsg_end(msg, hdr);
945 return 0;
946
947nla_put_failure:
948 genlmsg_cancel(msg, hdr);
949out:
950 return -EMSGSIZE;
951}
952
953static int llsec_iter_keys(struct llsec_dump_data *data)
954{
955 struct ieee802154_llsec_key_entry *pos;
956 int rc = 0, idx = 0;
957
958 list_for_each_entry(pos, &data->table->keys, list) {
959 if (idx++ < data->s_idx)
960 continue;
961
962 if (ieee802154_nl_fill_key(data->skb, data->portid,
963 data->nlmsg_seq, pos, data->dev)) {
964 rc = -EMSGSIZE;
965 break;
966 }
967
968 data->s_idx++;
969 }
970
971 return rc;
972}
973
974int ieee802154_llsec_dump_keys(struct sk_buff *skb, struct netlink_callback *cb)
975{
976 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_keys);
977}
978
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200979static int
980llsec_parse_dev(struct genl_info *info,
981 struct ieee802154_llsec_device *dev)
982{
983 memset(dev, 0, sizeof(*dev));
984
985 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
986 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
987 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE] ||
988 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE] ||
989 (!!info->attrs[IEEE802154_ATTR_PAN_ID] !=
990 !!info->attrs[IEEE802154_ATTR_SHORT_ADDR]))
991 return -EINVAL;
992
993 if (info->attrs[IEEE802154_ATTR_PAN_ID]) {
994 dev->pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
995 dev->short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
996 } else {
997 dev->short_addr = cpu_to_le16(IEEE802154_ADDR_UNDEF);
998 }
999
1000 dev->hwaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1001 dev->frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1002 dev->seclevel_exempt = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1003 dev->key_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE]);
1004
1005 if (dev->key_mode >= __IEEE802154_LLSEC_DEVKEY_MAX)
1006 return -EINVAL;
1007
1008 return 0;
1009}
1010
1011static int llsec_add_dev(struct net_device *dev, struct genl_info *info)
1012{
1013 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1014 struct ieee802154_llsec_device desc;
1015
1016 if (llsec_parse_dev(info, &desc))
1017 return -EINVAL;
1018
1019 return ops->llsec->add_dev(dev, &desc);
1020}
1021
1022int ieee802154_llsec_add_dev(struct sk_buff *skb, struct genl_info *info)
1023{
1024 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1025 (NLM_F_CREATE | NLM_F_EXCL))
1026 return -EINVAL;
1027
1028 return ieee802154_nl_llsec_change(skb, info, llsec_add_dev);
1029}
1030
1031static int llsec_del_dev(struct net_device *dev, struct genl_info *info)
1032{
1033 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1034 __le64 devaddr;
1035
1036 if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
1037 return -EINVAL;
1038
1039 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1040
1041 return ops->llsec->del_dev(dev, devaddr);
1042}
1043
1044int ieee802154_llsec_del_dev(struct sk_buff *skb, struct genl_info *info)
1045{
1046 return ieee802154_nl_llsec_change(skb, info, llsec_del_dev);
1047}
1048
1049static int
1050ieee802154_nl_fill_dev(struct sk_buff *msg, u32 portid, u32 seq,
1051 const struct ieee802154_llsec_device *desc,
1052 const struct net_device *dev)
1053{
1054 void *hdr;
1055
1056 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1057 IEEE802154_LLSEC_LIST_DEV);
1058 if (!hdr)
1059 goto out;
1060
1061 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1062 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1063 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, desc->pan_id) ||
1064 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
1065 desc->short_addr) ||
1066 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, desc->hwaddr) ||
1067 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1068 desc->frame_counter) ||
1069 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1070 desc->seclevel_exempt) ||
1071 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE, desc->key_mode))
1072 goto nla_put_failure;
1073
1074 genlmsg_end(msg, hdr);
1075 return 0;
1076
1077nla_put_failure:
1078 genlmsg_cancel(msg, hdr);
1079out:
1080 return -EMSGSIZE;
1081}
1082
1083static int llsec_iter_devs(struct llsec_dump_data *data)
1084{
1085 struct ieee802154_llsec_device *pos;
1086 int rc = 0, idx = 0;
1087
1088 list_for_each_entry(pos, &data->table->devices, list) {
1089 if (idx++ < data->s_idx)
1090 continue;
1091
1092 if (ieee802154_nl_fill_dev(data->skb, data->portid,
1093 data->nlmsg_seq, pos, data->dev)) {
1094 rc = -EMSGSIZE;
1095 break;
1096 }
1097
1098 data->s_idx++;
1099 }
1100
1101 return rc;
1102}
1103
1104int ieee802154_llsec_dump_devs(struct sk_buff *skb, struct netlink_callback *cb)
1105{
1106 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devs);
1107}
1108
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +02001109static 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
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +02001223static int
1224llsec_parse_seclevel(struct genl_info *info,
1225 struct ieee802154_llsec_seclevel *sl)
1226{
1227 memset(sl, 0, sizeof(*sl));
1228
1229 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE] ||
1230 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS] ||
1231 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE])
1232 return -EINVAL;
1233
1234 sl->frame_type = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE]);
1235 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD) {
1236 if (!info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID])
1237 return -EINVAL;
1238
1239 sl->cmd_frame_id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]);
1240 }
1241
1242 sl->sec_levels = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS]);
1243 sl->device_override = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1244
1245 return 0;
1246}
1247
1248static int llsec_add_seclevel(struct net_device *dev, struct genl_info *info)
1249{
1250 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1251 struct ieee802154_llsec_seclevel sl;
1252
1253 if (llsec_parse_seclevel(info, &sl))
1254 return -EINVAL;
1255
1256 return ops->llsec->add_seclevel(dev, &sl);
1257}
1258
1259int ieee802154_llsec_add_seclevel(struct sk_buff *skb, struct genl_info *info)
1260{
1261 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1262 (NLM_F_CREATE | NLM_F_EXCL))
1263 return -EINVAL;
1264
1265 return ieee802154_nl_llsec_change(skb, info, llsec_add_seclevel);
1266}
1267
1268static int llsec_del_seclevel(struct net_device *dev, struct genl_info *info)
1269{
1270 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1271 struct ieee802154_llsec_seclevel sl;
1272
1273 if (llsec_parse_seclevel(info, &sl))
1274 return -EINVAL;
1275
1276 return ops->llsec->del_seclevel(dev, &sl);
1277}
1278
1279int ieee802154_llsec_del_seclevel(struct sk_buff *skb, struct genl_info *info)
1280{
1281 return ieee802154_nl_llsec_change(skb, info, llsec_del_seclevel);
1282}
1283
1284static int
1285ieee802154_nl_fill_seclevel(struct sk_buff *msg, u32 portid, u32 seq,
1286 const struct ieee802154_llsec_seclevel *sl,
1287 const struct net_device *dev)
1288{
1289 void *hdr;
1290
1291 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1292 IEEE802154_LLSEC_LIST_SECLEVEL);
1293 if (!hdr)
1294 goto out;
1295
1296 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1297 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1298 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_FRAME_TYPE, sl->frame_type) ||
1299 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVELS, sl->sec_levels) ||
1300 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1301 sl->device_override))
1302 goto nla_put_failure;
1303
1304 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD &&
1305 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID,
1306 sl->cmd_frame_id))
1307 goto nla_put_failure;
1308
1309 genlmsg_end(msg, hdr);
1310 return 0;
1311
1312nla_put_failure:
1313 genlmsg_cancel(msg, hdr);
1314out:
1315 return -EMSGSIZE;
1316}
1317
1318static int llsec_iter_seclevels(struct llsec_dump_data *data)
1319{
1320 struct ieee802154_llsec_seclevel *pos;
1321 int rc = 0, idx = 0;
1322
1323 list_for_each_entry(pos, &data->table->security_levels, list) {
1324 if (idx++ < data->s_idx)
1325 continue;
1326
1327 if (ieee802154_nl_fill_seclevel(data->skb, data->portid,
1328 data->nlmsg_seq, pos,
1329 data->dev)) {
1330 rc = -EMSGSIZE;
1331 break;
1332 }
1333
1334 data->s_idx++;
1335 }
1336
1337 return rc;
1338}
1339
1340int ieee802154_llsec_dump_seclevels(struct sk_buff *skb,
1341 struct netlink_callback *cb)
1342{
1343 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_seclevels);
1344}