blob: cd919493c976000e6b5cd206014110500e68886e [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}
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
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100116 rtnl_lock();
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200117 ops->get_mac_params(dev, &params);
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100118 rtnl_unlock();
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200119
120 if (nla_put_s8(msg, IEEE802154_ATTR_TXPOWER,
121 params.transmit_power) ||
122 nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, params.lbt) ||
123 nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE,
124 params.cca_mode) ||
125 nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL,
126 params.cca_ed_level) ||
127 nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES,
128 params.csma_retries) ||
129 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE,
130 params.min_be) ||
131 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE,
132 params.max_be) ||
133 nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES,
134 params.frame_retries))
135 goto nla_put_failure;
136 }
137
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +0300138 wpan_phy_put(phy);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400139 return genlmsg_end(msg, hdr);
140
141nla_put_failure:
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +0300142 wpan_phy_put(phy);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400143 genlmsg_cancel(msg, hdr);
144out:
145 return -EMSGSIZE;
146}
147
148/* Requests from userspace */
149static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
150{
151 struct net_device *dev;
152
153 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
154 char name[IFNAMSIZ + 1];
Varka Bhadram4710d802014-07-02 09:01:09 +0530155
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400156 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
Varka Bhadram4710d802014-07-02 09:01:09 +0530157 sizeof(name));
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400158 dev = dev_get_by_name(&init_net, name);
Varka Bhadram4710d802014-07-02 09:01:09 +0530159 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX]) {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400160 dev = dev_get_by_index(&init_net,
161 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
Varka Bhadram4710d802014-07-02 09:01:09 +0530162 } else {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400163 return NULL;
Varka Bhadram4710d802014-07-02 09:01:09 +0530164 }
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400165
166 if (!dev)
167 return NULL;
168
Alexander Aring87023e12014-11-09 08:36:58 +0100169 /* Check on mtu is currently a hacked solution because lowpan
170 * and wpan have the same ARPHRD type.
171 */
172 if (dev->type != ARPHRD_IEEE802154 || dev->mtu != IEEE802154_MTU) {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400173 dev_put(dev);
174 return NULL;
175 }
176
177 return dev;
178}
179
Johannes Berg1c582d92013-11-14 17:14:41 +0100180int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400181{
182 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100183 struct ieee802154_addr addr;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400184 u8 page;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000185 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400186
187 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
188 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
189 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
190 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
191 !info->attrs[IEEE802154_ATTR_CAPABILITY])
192 return -EINVAL;
193
194 dev = ieee802154_nl_get_dev(info);
195 if (!dev)
196 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000197 if (!ieee802154_mlme_ops(dev)->assoc_req)
198 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400199
200 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100201 addr.mode = IEEE802154_ADDR_LONG;
202 addr.extended_addr = nla_get_hwaddr(
203 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400204 } else {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100205 addr.mode = IEEE802154_ADDR_SHORT;
206 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400207 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
208 }
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100209 addr.pan_id = nla_get_shortaddr(
210 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400211
212 if (info->attrs[IEEE802154_ATTR_PAGE])
213 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
214 else
215 page = 0;
216
217 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
218 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
219 page,
220 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
221
Werner Almesberger56aa0912013-04-04 06:32:35 +0000222out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400223 dev_put(dev);
224 return ret;
225}
226
Johannes Berg1c582d92013-11-14 17:14:41 +0100227int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400228{
229 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100230 struct ieee802154_addr addr;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000231 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400232
233 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
234 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
235 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
236 return -EINVAL;
237
238 dev = ieee802154_nl_get_dev(info);
239 if (!dev)
240 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000241 if (!ieee802154_mlme_ops(dev)->assoc_resp)
242 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400243
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100244 addr.mode = IEEE802154_ADDR_LONG;
245 addr.extended_addr = nla_get_hwaddr(
246 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
247 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400248
249 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100250 nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400251 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
252
Werner Almesberger56aa0912013-04-04 06:32:35 +0000253out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400254 dev_put(dev);
255 return ret;
256}
257
Johannes Berg1c582d92013-11-14 17:14:41 +0100258int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400259{
260 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100261 struct ieee802154_addr addr;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000262 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400263
264 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
Varka Bhadram4710d802014-07-02 09:01:09 +0530265 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400266 !info->attrs[IEEE802154_ATTR_REASON])
267 return -EINVAL;
268
269 dev = ieee802154_nl_get_dev(info);
270 if (!dev)
271 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000272 if (!ieee802154_mlme_ops(dev)->disassoc_req)
273 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400274
275 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100276 addr.mode = IEEE802154_ADDR_LONG;
277 addr.extended_addr = nla_get_hwaddr(
278 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400279 } else {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100280 addr.mode = IEEE802154_ADDR_SHORT;
281 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400282 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
283 }
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100284 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400285
286 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
287 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
288
Werner Almesberger56aa0912013-04-04 06:32:35 +0000289out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400290 dev_put(dev);
291 return ret;
292}
293
Varka Bhadram4710d802014-07-02 09:01:09 +0530294/* PANid, channel, beacon_order = 15, superframe_order = 15,
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400295 * PAN_coordinator, battery_life_extension = 0,
296 * coord_realignment = 0, security_enable = 0
297*/
Johannes Berg1c582d92013-11-14 17:14:41 +0100298int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400299{
300 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100301 struct ieee802154_addr addr;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400302
303 u8 channel, bcn_ord, sf_ord;
304 u8 page;
305 int pan_coord, blx, coord_realign;
Alexander Aring8f499f92014-11-02 04:18:39 +0100306 int ret = -EBUSY;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400307
308 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
309 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
310 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
311 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
312 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
313 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
314 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
315 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
316 )
317 return -EINVAL;
318
319 dev = ieee802154_nl_get_dev(info);
320 if (!dev)
321 return -ENODEV;
Alexander Aring8f499f92014-11-02 04:18:39 +0100322
323 if (netif_running(dev))
Werner Almesberger56aa0912013-04-04 06:32:35 +0000324 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400325
Alexander Aring8f499f92014-11-02 04:18:39 +0100326 if (!ieee802154_mlme_ops(dev)->start_req) {
327 ret = -EOPNOTSUPP;
328 goto out;
329 }
330
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100331 addr.mode = IEEE802154_ADDR_SHORT;
332 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400333 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100334 addr.pan_id = nla_get_shortaddr(
335 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400336
337 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
338 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
339 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
340 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
341 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
342 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
343
344 if (info->attrs[IEEE802154_ATTR_PAGE])
345 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
346 else
347 page = 0;
348
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100349 if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400350 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
351 dev_put(dev);
352 return -EINVAL;
353 }
354
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100355 rtnl_lock();
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400356 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
357 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100358 rtnl_unlock();
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400359
Alexander Aring9f3295b2014-11-05 20:51:13 +0100360 /* FIXME: add validation for unused parameters to be sane
361 * for SoftMAC
362 */
363 ieee802154_nl_start_confirm(dev, IEEE802154_SUCCESS);
364
Werner Almesberger56aa0912013-04-04 06:32:35 +0000365out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400366 dev_put(dev);
367 return ret;
368}
369
Johannes Berg1c582d92013-11-14 17:14:41 +0100370int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400371{
372 struct net_device *dev;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000373 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400374 u8 type;
375 u32 channels;
376 u8 duration;
377 u8 page;
378
379 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
380 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
381 !info->attrs[IEEE802154_ATTR_DURATION])
382 return -EINVAL;
383
384 dev = ieee802154_nl_get_dev(info);
385 if (!dev)
386 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000387 if (!ieee802154_mlme_ops(dev)->scan_req)
388 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400389
390 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
391 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
392 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
393
394 if (info->attrs[IEEE802154_ATTR_PAGE])
395 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
396 else
397 page = 0;
398
Varka Bhadram4710d802014-07-02 09:01:09 +0530399 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
400 page, duration);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400401
Werner Almesberger56aa0912013-04-04 06:32:35 +0000402out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400403 dev_put(dev);
404 return ret;
405}
406
Johannes Berg1c582d92013-11-14 17:14:41 +0100407int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400408{
409 /* Request for interface name, index, type, IEEE address,
Varka Bhadram4710d802014-07-02 09:01:09 +0530410 * PAN Id, short address
411 */
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400412 struct sk_buff *msg;
413 struct net_device *dev = NULL;
414 int rc = -ENOBUFS;
415
416 pr_debug("%s\n", __func__);
417
418 dev = ieee802154_nl_get_dev(info);
419 if (!dev)
420 return -ENODEV;
421
Thomas Graf58050fc2012-06-28 03:57:45 +0000422 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400423 if (!msg)
424 goto out_dev;
425
Eric W. Biederman15e47302012-09-07 20:12:54 +0000426 rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq,
Varka Bhadram4710d802014-07-02 09:01:09 +0530427 0, dev);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400428 if (rc < 0)
429 goto out_free;
430
431 dev_put(dev);
432
433 return genlmsg_reply(msg, info);
434out_free:
435 nlmsg_free(msg);
436out_dev:
437 dev_put(dev);
438 return rc;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400439}
440
Johannes Berg1c582d92013-11-14 17:14:41 +0100441int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400442{
443 struct net *net = sock_net(skb->sk);
444 struct net_device *dev;
445 int idx;
446 int s_idx = cb->args[0];
447
448 pr_debug("%s\n", __func__);
449
450 idx = 0;
451 for_each_netdev(net, dev) {
Alexander Aring87023e12014-11-09 08:36:58 +0100452 /* Check on mtu is currently a hacked solution because lowpan
453 * and wpan have the same ARPHRD type.
454 */
455 if (idx < s_idx || dev->type != ARPHRD_IEEE802154 ||
456 dev->mtu != IEEE802154_MTU)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400457 goto cont;
458
Eric W. Biederman15e47302012-09-07 20:12:54 +0000459 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid,
Varka Bhadram4710d802014-07-02 09:01:09 +0530460 cb->nlh->nlmsg_seq,
461 NLM_F_MULTI, dev) < 0)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400462 break;
463cont:
464 idx++;
465 }
466 cb->args[0] = idx;
467
468 return skb->len;
469}
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200470
471int ieee802154_set_macparams(struct sk_buff *skb, struct genl_info *info)
472{
473 struct net_device *dev = NULL;
474 struct ieee802154_mlme_ops *ops;
475 struct ieee802154_mac_params params;
476 struct wpan_phy *phy;
477 int rc = -EINVAL;
478
479 pr_debug("%s\n", __func__);
480
481 dev = ieee802154_nl_get_dev(info);
482 if (!dev)
483 return -ENODEV;
484
485 ops = ieee802154_mlme_ops(dev);
486
487 if (!ops->get_mac_params || !ops->set_mac_params) {
488 rc = -EOPNOTSUPP;
489 goto out;
490 }
491
492 if (netif_running(dev)) {
493 rc = -EBUSY;
494 goto out;
495 }
496
497 if (!info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
498 !info->attrs[IEEE802154_ATTR_CCA_MODE] &&
499 !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] &&
500 !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] &&
501 !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] &&
502 !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] &&
503 !info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
504 goto out;
505
Alexander Aringbd28a112014-11-05 20:51:18 +0100506 phy = dev->ieee802154_ptr->wpan_phy;
507 get_device(&phy->dev);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200508
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100509 rtnl_lock();
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200510 ops->get_mac_params(dev, &params);
511
512 if (info->attrs[IEEE802154_ATTR_TXPOWER])
513 params.transmit_power = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]);
514
515 if (info->attrs[IEEE802154_ATTR_LBT_ENABLED])
516 params.lbt = nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
517
518 if (info->attrs[IEEE802154_ATTR_CCA_MODE])
519 params.cca_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
520
521 if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])
522 params.cca_ed_level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]);
523
524 if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES])
525 params.csma_retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]);
526
527 if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE])
528 params.min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]);
529
530 if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE])
531 params.max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]);
532
533 if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
534 params.frame_retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]);
535
536 rc = ops->set_mac_params(dev, &params);
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100537 rtnl_unlock();
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200538
539 wpan_phy_put(phy);
540 dev_put(dev);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200541
Alexander Aringa543c592014-10-28 18:21:23 +0100542 return 0;
543
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200544out:
545 dev_put(dev);
546 return rc;
547}
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200548
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200549static int
550ieee802154_llsec_parse_key_id(struct genl_info *info,
551 struct ieee802154_llsec_key_id *desc)
552{
553 memset(desc, 0, sizeof(*desc));
554
555 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE])
556 return -EINVAL;
557
558 desc->mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]);
559
560 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
561 if (!info->attrs[IEEE802154_ATTR_PAN_ID] &&
562 !(info->attrs[IEEE802154_ATTR_SHORT_ADDR] ||
563 info->attrs[IEEE802154_ATTR_HW_ADDR]))
564 return -EINVAL;
565
566 desc->device_addr.pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
567
568 if (info->attrs[IEEE802154_ATTR_SHORT_ADDR]) {
569 desc->device_addr.mode = IEEE802154_ADDR_SHORT;
570 desc->device_addr.short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
571 } else {
572 desc->device_addr.mode = IEEE802154_ADDR_LONG;
573 desc->device_addr.extended_addr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
574 }
575 }
576
577 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
578 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID])
579 return -EINVAL;
580
581 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
582 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT])
583 return -EINVAL;
584
585 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
586 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED])
587 return -EINVAL;
588
589 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT)
590 desc->id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]);
591
592 switch (desc->mode) {
593 case IEEE802154_SCF_KEY_SHORT_INDEX:
594 {
595 u32 source = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]);
Varka Bhadram4710d802014-07-02 09:01:09 +0530596
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200597 desc->short_source = cpu_to_le32(source);
598 break;
599 }
600 case IEEE802154_SCF_KEY_HW_INDEX:
601 desc->extended_source = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]);
602 break;
603 }
604
605 return 0;
606}
607
608static int
609ieee802154_llsec_fill_key_id(struct sk_buff *msg,
610 const struct ieee802154_llsec_key_id *desc)
611{
612 if (nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_MODE, desc->mode))
613 return -EMSGSIZE;
614
615 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
616 if (nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID,
617 desc->device_addr.pan_id))
618 return -EMSGSIZE;
619
620 if (desc->device_addr.mode == IEEE802154_ADDR_SHORT &&
621 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
622 desc->device_addr.short_addr))
623 return -EMSGSIZE;
624
625 if (desc->device_addr.mode == IEEE802154_ADDR_LONG &&
626 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR,
627 desc->device_addr.extended_addr))
628 return -EMSGSIZE;
629 }
630
631 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
632 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_ID, desc->id))
633 return -EMSGSIZE;
634
635 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
636 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT,
637 le32_to_cpu(desc->short_source)))
638 return -EMSGSIZE;
639
640 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
641 nla_put_hwaddr(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED,
642 desc->extended_source))
643 return -EMSGSIZE;
644
645 return 0;
646}
647
648int ieee802154_llsec_getparams(struct sk_buff *skb, struct genl_info *info)
649{
650 struct sk_buff *msg;
651 struct net_device *dev = NULL;
652 int rc = -ENOBUFS;
653 struct ieee802154_mlme_ops *ops;
654 void *hdr;
655 struct ieee802154_llsec_params params;
656
657 pr_debug("%s\n", __func__);
658
659 dev = ieee802154_nl_get_dev(info);
660 if (!dev)
661 return -ENODEV;
662
663 ops = ieee802154_mlme_ops(dev);
Dan Carpenterb3f7a7b2014-05-22 10:53:06 +0300664 if (!ops->llsec) {
665 rc = -EOPNOTSUPP;
666 goto out_dev;
667 }
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200668
669 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
670 if (!msg)
671 goto out_dev;
672
673 hdr = genlmsg_put(msg, 0, info->snd_seq, &nl802154_family, 0,
Varka Bhadram4710d802014-07-02 09:01:09 +0530674 IEEE802154_LLSEC_GETPARAMS);
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200675 if (!hdr)
676 goto out_free;
677
678 rc = ops->llsec->get_params(dev, &params);
679 if (rc < 0)
680 goto out_free;
681
682 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
683 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
684 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_ENABLED, params.enabled) ||
685 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVEL, params.out_level) ||
686 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
687 be32_to_cpu(params.frame_counter)) ||
688 ieee802154_llsec_fill_key_id(msg, &params.out_key))
689 goto out_free;
690
691 dev_put(dev);
692
693 return ieee802154_nl_reply(msg, info);
694out_free:
695 nlmsg_free(msg);
696out_dev:
697 dev_put(dev);
698 return rc;
699}
700
701int ieee802154_llsec_setparams(struct sk_buff *skb, struct genl_info *info)
702{
703 struct net_device *dev = NULL;
704 int rc = -EINVAL;
705 struct ieee802154_mlme_ops *ops;
706 struct ieee802154_llsec_params params;
707 int changed = 0;
708
709 pr_debug("%s\n", __func__);
710
711 dev = ieee802154_nl_get_dev(info);
712 if (!dev)
713 return -ENODEV;
714
715 if (!info->attrs[IEEE802154_ATTR_LLSEC_ENABLED] &&
716 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE] &&
717 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL])
718 goto out;
719
720 ops = ieee802154_mlme_ops(dev);
721 if (!ops->llsec) {
722 rc = -EOPNOTSUPP;
723 goto out;
724 }
725
726 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL] &&
727 nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) > 7)
728 goto out;
729
730 if (info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]) {
731 params.enabled = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]);
732 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
733 }
734
735 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) {
736 if (ieee802154_llsec_parse_key_id(info, &params.out_key))
737 goto out;
738
739 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
740 }
741
742 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) {
743 params.out_level = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]);
744 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
745 }
746
747 if (info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]) {
748 u32 fc = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
749
750 params.frame_counter = cpu_to_be32(fc);
751 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
752 }
753
754 rc = ops->llsec->set_params(dev, &params, changed);
755
756 dev_put(dev);
757
758 return rc;
759out:
760 dev_put(dev);
761 return rc;
762}
763
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200764struct llsec_dump_data {
765 struct sk_buff *skb;
766 int s_idx, s_idx2;
767 int portid;
768 int nlmsg_seq;
769 struct net_device *dev;
770 struct ieee802154_mlme_ops *ops;
771 struct ieee802154_llsec_table *table;
772};
773
774static int
775ieee802154_llsec_dump_table(struct sk_buff *skb, struct netlink_callback *cb,
Varka Bhadram4710d802014-07-02 09:01:09 +0530776 int (*step)(struct llsec_dump_data *))
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200777{
778 struct net *net = sock_net(skb->sk);
779 struct net_device *dev;
780 struct llsec_dump_data data;
781 int idx = 0;
782 int first_dev = cb->args[0];
783 int rc;
784
785 for_each_netdev(net, dev) {
Alexander Aring87023e12014-11-09 08:36:58 +0100786 /* Check on mtu is currently a hacked solution because lowpan
787 * and wpan have the same ARPHRD type.
788 */
789 if (idx < first_dev || dev->type != ARPHRD_IEEE802154 ||
790 dev->mtu != IEEE802154_MTU)
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200791 goto skip;
792
793 data.ops = ieee802154_mlme_ops(dev);
794 if (!data.ops->llsec)
795 goto skip;
796
797 data.skb = skb;
798 data.s_idx = cb->args[1];
799 data.s_idx2 = cb->args[2];
800 data.dev = dev;
801 data.portid = NETLINK_CB(cb->skb).portid;
802 data.nlmsg_seq = cb->nlh->nlmsg_seq;
803
804 data.ops->llsec->lock_table(dev);
805 data.ops->llsec->get_table(data.dev, &data.table);
806 rc = step(&data);
807 data.ops->llsec->unlock_table(dev);
808
809 if (rc < 0)
810 break;
811
812skip:
813 idx++;
814 }
815 cb->args[0] = idx;
816
817 return skb->len;
818}
819
820static int
821ieee802154_nl_llsec_change(struct sk_buff *skb, struct genl_info *info,
822 int (*fn)(struct net_device*, struct genl_info*))
823{
824 struct net_device *dev = NULL;
825 int rc = -EINVAL;
826
827 dev = ieee802154_nl_get_dev(info);
828 if (!dev)
829 return -ENODEV;
830
831 if (!ieee802154_mlme_ops(dev)->llsec)
832 rc = -EOPNOTSUPP;
833 else
834 rc = fn(dev, info);
835
836 dev_put(dev);
837 return rc;
838}
839
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200840static int
841ieee802154_llsec_parse_key(struct genl_info *info,
842 struct ieee802154_llsec_key *key)
843{
844 u8 frames;
845 u32 commands[256 / 32];
846
847 memset(key, 0, sizeof(*key));
848
849 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES] ||
850 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES])
851 return -EINVAL;
852
853 frames = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES]);
854 if ((frames & BIT(IEEE802154_FC_TYPE_MAC_CMD)) &&
855 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS])
856 return -EINVAL;
857
858 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) {
859 nla_memcpy(commands,
860 info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS],
861 256 / 8);
862
863 if (commands[0] || commands[1] || commands[2] || commands[3] ||
864 commands[4] || commands[5] || commands[6] ||
865 commands[7] >= BIT(IEEE802154_CMD_GTS_REQ + 1))
866 return -EINVAL;
867
868 key->cmd_frame_ids = commands[7];
869 }
870
871 key->frame_types = frames;
872
873 nla_memcpy(key->key, info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES],
874 IEEE802154_LLSEC_KEY_SIZE);
875
876 return 0;
877}
878
879static int llsec_add_key(struct net_device *dev, struct genl_info *info)
880{
881 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
882 struct ieee802154_llsec_key key;
883 struct ieee802154_llsec_key_id id;
884
885 if (ieee802154_llsec_parse_key(info, &key) ||
886 ieee802154_llsec_parse_key_id(info, &id))
887 return -EINVAL;
888
889 return ops->llsec->add_key(dev, &id, &key);
890}
891
892int ieee802154_llsec_add_key(struct sk_buff *skb, struct genl_info *info)
893{
894 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
895 (NLM_F_CREATE | NLM_F_EXCL))
896 return -EINVAL;
897
898 return ieee802154_nl_llsec_change(skb, info, llsec_add_key);
899}
900
901static int llsec_remove_key(struct net_device *dev, struct genl_info *info)
902{
903 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
904 struct ieee802154_llsec_key_id id;
905
906 if (ieee802154_llsec_parse_key_id(info, &id))
907 return -EINVAL;
908
909 return ops->llsec->del_key(dev, &id);
910}
911
912int ieee802154_llsec_del_key(struct sk_buff *skb, struct genl_info *info)
913{
914 return ieee802154_nl_llsec_change(skb, info, llsec_remove_key);
915}
916
917static int
918ieee802154_nl_fill_key(struct sk_buff *msg, u32 portid, u32 seq,
919 const struct ieee802154_llsec_key_entry *key,
920 const struct net_device *dev)
921{
922 void *hdr;
923 u32 commands[256 / 32];
924
925 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
926 IEEE802154_LLSEC_LIST_KEY);
927 if (!hdr)
928 goto out;
929
930 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
931 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
932 ieee802154_llsec_fill_key_id(msg, &key->id) ||
933 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES,
934 key->key->frame_types))
935 goto nla_put_failure;
936
937 if (key->key->frame_types & BIT(IEEE802154_FC_TYPE_MAC_CMD)) {
938 memset(commands, 0, sizeof(commands));
939 commands[7] = key->key->cmd_frame_ids;
940 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS,
941 sizeof(commands), commands))
942 goto nla_put_failure;
943 }
944
945 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_BYTES,
946 IEEE802154_LLSEC_KEY_SIZE, key->key->key))
947 goto nla_put_failure;
948
949 genlmsg_end(msg, hdr);
950 return 0;
951
952nla_put_failure:
953 genlmsg_cancel(msg, hdr);
954out:
955 return -EMSGSIZE;
956}
957
958static int llsec_iter_keys(struct llsec_dump_data *data)
959{
960 struct ieee802154_llsec_key_entry *pos;
961 int rc = 0, idx = 0;
962
963 list_for_each_entry(pos, &data->table->keys, list) {
964 if (idx++ < data->s_idx)
965 continue;
966
967 if (ieee802154_nl_fill_key(data->skb, data->portid,
968 data->nlmsg_seq, pos, data->dev)) {
969 rc = -EMSGSIZE;
970 break;
971 }
972
973 data->s_idx++;
974 }
975
976 return rc;
977}
978
979int ieee802154_llsec_dump_keys(struct sk_buff *skb, struct netlink_callback *cb)
980{
981 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_keys);
982}
983
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200984static int
985llsec_parse_dev(struct genl_info *info,
986 struct ieee802154_llsec_device *dev)
987{
988 memset(dev, 0, sizeof(*dev));
989
990 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
991 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
992 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE] ||
993 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE] ||
994 (!!info->attrs[IEEE802154_ATTR_PAN_ID] !=
995 !!info->attrs[IEEE802154_ATTR_SHORT_ADDR]))
996 return -EINVAL;
997
998 if (info->attrs[IEEE802154_ATTR_PAN_ID]) {
999 dev->pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
1000 dev->short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
1001 } else {
1002 dev->short_addr = cpu_to_le16(IEEE802154_ADDR_UNDEF);
1003 }
1004
1005 dev->hwaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1006 dev->frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1007 dev->seclevel_exempt = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1008 dev->key_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE]);
1009
1010 if (dev->key_mode >= __IEEE802154_LLSEC_DEVKEY_MAX)
1011 return -EINVAL;
1012
1013 return 0;
1014}
1015
1016static int llsec_add_dev(struct net_device *dev, struct genl_info *info)
1017{
1018 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1019 struct ieee802154_llsec_device desc;
1020
1021 if (llsec_parse_dev(info, &desc))
1022 return -EINVAL;
1023
1024 return ops->llsec->add_dev(dev, &desc);
1025}
1026
1027int ieee802154_llsec_add_dev(struct sk_buff *skb, struct genl_info *info)
1028{
1029 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1030 (NLM_F_CREATE | NLM_F_EXCL))
1031 return -EINVAL;
1032
1033 return ieee802154_nl_llsec_change(skb, info, llsec_add_dev);
1034}
1035
1036static int llsec_del_dev(struct net_device *dev, struct genl_info *info)
1037{
1038 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1039 __le64 devaddr;
1040
1041 if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
1042 return -EINVAL;
1043
1044 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1045
1046 return ops->llsec->del_dev(dev, devaddr);
1047}
1048
1049int ieee802154_llsec_del_dev(struct sk_buff *skb, struct genl_info *info)
1050{
1051 return ieee802154_nl_llsec_change(skb, info, llsec_del_dev);
1052}
1053
1054static int
1055ieee802154_nl_fill_dev(struct sk_buff *msg, u32 portid, u32 seq,
1056 const struct ieee802154_llsec_device *desc,
1057 const struct net_device *dev)
1058{
1059 void *hdr;
1060
1061 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1062 IEEE802154_LLSEC_LIST_DEV);
1063 if (!hdr)
1064 goto out;
1065
1066 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1067 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1068 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, desc->pan_id) ||
1069 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
1070 desc->short_addr) ||
1071 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, desc->hwaddr) ||
1072 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1073 desc->frame_counter) ||
1074 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1075 desc->seclevel_exempt) ||
1076 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE, desc->key_mode))
1077 goto nla_put_failure;
1078
1079 genlmsg_end(msg, hdr);
1080 return 0;
1081
1082nla_put_failure:
1083 genlmsg_cancel(msg, hdr);
1084out:
1085 return -EMSGSIZE;
1086}
1087
1088static int llsec_iter_devs(struct llsec_dump_data *data)
1089{
1090 struct ieee802154_llsec_device *pos;
1091 int rc = 0, idx = 0;
1092
1093 list_for_each_entry(pos, &data->table->devices, list) {
1094 if (idx++ < data->s_idx)
1095 continue;
1096
1097 if (ieee802154_nl_fill_dev(data->skb, data->portid,
1098 data->nlmsg_seq, pos, data->dev)) {
1099 rc = -EMSGSIZE;
1100 break;
1101 }
1102
1103 data->s_idx++;
1104 }
1105
1106 return rc;
1107}
1108
1109int ieee802154_llsec_dump_devs(struct sk_buff *skb, struct netlink_callback *cb)
1110{
1111 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devs);
1112}
1113
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +02001114static int llsec_add_devkey(struct net_device *dev, struct genl_info *info)
1115{
1116 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1117 struct ieee802154_llsec_device_key key;
1118 __le64 devaddr;
1119
1120 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
1121 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1122 ieee802154_llsec_parse_key_id(info, &key.key_id))
1123 return -EINVAL;
1124
1125 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1126 key.frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1127
1128 return ops->llsec->add_devkey(dev, devaddr, &key);
1129}
1130
1131int ieee802154_llsec_add_devkey(struct sk_buff *skb, struct genl_info *info)
1132{
1133 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1134 (NLM_F_CREATE | NLM_F_EXCL))
1135 return -EINVAL;
1136
1137 return ieee802154_nl_llsec_change(skb, info, llsec_add_devkey);
1138}
1139
1140static int llsec_del_devkey(struct net_device *dev, struct genl_info *info)
1141{
1142 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1143 struct ieee802154_llsec_device_key key;
1144 __le64 devaddr;
1145
1146 if (!info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1147 ieee802154_llsec_parse_key_id(info, &key.key_id))
1148 return -EINVAL;
1149
1150 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1151
1152 return ops->llsec->del_devkey(dev, devaddr, &key);
1153}
1154
1155int ieee802154_llsec_del_devkey(struct sk_buff *skb, struct genl_info *info)
1156{
1157 return ieee802154_nl_llsec_change(skb, info, llsec_del_devkey);
1158}
1159
1160static int
1161ieee802154_nl_fill_devkey(struct sk_buff *msg, u32 portid, u32 seq,
1162 __le64 devaddr,
1163 const struct ieee802154_llsec_device_key *devkey,
1164 const struct net_device *dev)
1165{
1166 void *hdr;
1167
1168 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1169 IEEE802154_LLSEC_LIST_DEVKEY);
1170 if (!hdr)
1171 goto out;
1172
1173 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1174 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1175 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, devaddr) ||
1176 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1177 devkey->frame_counter) ||
1178 ieee802154_llsec_fill_key_id(msg, &devkey->key_id))
1179 goto nla_put_failure;
1180
1181 genlmsg_end(msg, hdr);
1182 return 0;
1183
1184nla_put_failure:
1185 genlmsg_cancel(msg, hdr);
1186out:
1187 return -EMSGSIZE;
1188}
1189
1190static int llsec_iter_devkeys(struct llsec_dump_data *data)
1191{
1192 struct ieee802154_llsec_device *dpos;
1193 struct ieee802154_llsec_device_key *kpos;
1194 int rc = 0, idx = 0, idx2;
1195
1196 list_for_each_entry(dpos, &data->table->devices, list) {
1197 if (idx++ < data->s_idx)
1198 continue;
1199
1200 idx2 = 0;
1201
1202 list_for_each_entry(kpos, &dpos->keys, list) {
1203 if (idx2++ < data->s_idx2)
1204 continue;
1205
1206 if (ieee802154_nl_fill_devkey(data->skb, data->portid,
1207 data->nlmsg_seq,
1208 dpos->hwaddr, kpos,
1209 data->dev)) {
1210 return rc = -EMSGSIZE;
1211 }
1212
1213 data->s_idx2++;
1214 }
1215
1216 data->s_idx++;
1217 }
1218
1219 return rc;
1220}
1221
1222int ieee802154_llsec_dump_devkeys(struct sk_buff *skb,
1223 struct netlink_callback *cb)
1224{
1225 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devkeys);
1226}
1227
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +02001228static int
1229llsec_parse_seclevel(struct genl_info *info,
1230 struct ieee802154_llsec_seclevel *sl)
1231{
1232 memset(sl, 0, sizeof(*sl));
1233
1234 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE] ||
1235 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS] ||
1236 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE])
1237 return -EINVAL;
1238
1239 sl->frame_type = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE]);
1240 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD) {
1241 if (!info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID])
1242 return -EINVAL;
1243
1244 sl->cmd_frame_id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]);
1245 }
1246
1247 sl->sec_levels = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS]);
1248 sl->device_override = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1249
1250 return 0;
1251}
1252
1253static int llsec_add_seclevel(struct net_device *dev, struct genl_info *info)
1254{
1255 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1256 struct ieee802154_llsec_seclevel sl;
1257
1258 if (llsec_parse_seclevel(info, &sl))
1259 return -EINVAL;
1260
1261 return ops->llsec->add_seclevel(dev, &sl);
1262}
1263
1264int ieee802154_llsec_add_seclevel(struct sk_buff *skb, struct genl_info *info)
1265{
1266 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1267 (NLM_F_CREATE | NLM_F_EXCL))
1268 return -EINVAL;
1269
1270 return ieee802154_nl_llsec_change(skb, info, llsec_add_seclevel);
1271}
1272
1273static int llsec_del_seclevel(struct net_device *dev, struct genl_info *info)
1274{
1275 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1276 struct ieee802154_llsec_seclevel sl;
1277
1278 if (llsec_parse_seclevel(info, &sl))
1279 return -EINVAL;
1280
1281 return ops->llsec->del_seclevel(dev, &sl);
1282}
1283
1284int ieee802154_llsec_del_seclevel(struct sk_buff *skb, struct genl_info *info)
1285{
1286 return ieee802154_nl_llsec_change(skb, info, llsec_del_seclevel);
1287}
1288
1289static int
1290ieee802154_nl_fill_seclevel(struct sk_buff *msg, u32 portid, u32 seq,
1291 const struct ieee802154_llsec_seclevel *sl,
1292 const struct net_device *dev)
1293{
1294 void *hdr;
1295
1296 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1297 IEEE802154_LLSEC_LIST_SECLEVEL);
1298 if (!hdr)
1299 goto out;
1300
1301 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1302 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1303 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_FRAME_TYPE, sl->frame_type) ||
1304 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVELS, sl->sec_levels) ||
1305 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1306 sl->device_override))
1307 goto nla_put_failure;
1308
1309 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD &&
1310 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID,
1311 sl->cmd_frame_id))
1312 goto nla_put_failure;
1313
1314 genlmsg_end(msg, hdr);
1315 return 0;
1316
1317nla_put_failure:
1318 genlmsg_cancel(msg, hdr);
1319out:
1320 return -EMSGSIZE;
1321}
1322
1323static int llsec_iter_seclevels(struct llsec_dump_data *data)
1324{
1325 struct ieee802154_llsec_seclevel *pos;
1326 int rc = 0, idx = 0;
1327
1328 list_for_each_entry(pos, &data->table->security_levels, list) {
1329 if (idx++ < data->s_idx)
1330 continue;
1331
1332 if (ieee802154_nl_fill_seclevel(data->skb, data->portid,
1333 data->nlmsg_seq, pos,
1334 data->dev)) {
1335 rc = -EMSGSIZE;
1336 break;
1337 }
1338
1339 data->s_idx++;
1340 }
1341
1342 return rc;
1343}
1344
1345int ieee802154_llsec_dump_seclevels(struct sk_buff *skb,
1346 struct netlink_callback *cb)
1347{
1348 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_seclevels);
1349}