blob: 50e9863823a5035f78fc209260991a3da31a2ddb [file] [log] [blame]
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +04001/*
2 * Netlink inteface for IEEE 802.15.4 stack
3 *
4 * Copyright 2007, 2008 Siemens AG
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040015 * Written by:
16 * Sergey Lapin <slapin@ossfans.org>
17 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
18 * Maxim Osipov <maxim.osipov@siemens.com>
19 */
20
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040022#include <linux/kernel.h>
23#include <linux/if_arp.h>
24#include <linux/netdevice.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020025#include <linux/ieee802154.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040026#include <net/netlink.h>
27#include <net/genetlink.h>
28#include <net/sock.h>
29#include <linux/nl802154.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040030#include <linux/export.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040031#include <net/af_ieee802154.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040032#include <net/ieee802154_netdev.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020033#include <net/cfg802154.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040034
35#include "ieee802154.h"
36
Phoebe Buckheisterae531b92014-03-14 21:24:02 +010037static int nla_put_hwaddr(struct sk_buff *msg, int type, __le64 hwaddr)
38{
39 return nla_put_u64(msg, type, swab64((__force u64)hwaddr));
40}
41
42static __le64 nla_get_hwaddr(const struct nlattr *nla)
43{
44 return ieee802154_devaddr_from_raw(nla_data(nla));
45}
46
47static int nla_put_shortaddr(struct sk_buff *msg, int type, __le16 addr)
48{
49 return nla_put_u16(msg, type, le16_to_cpu(addr));
50}
51
52static __le16 nla_get_shortaddr(const struct nlattr *nla)
53{
54 return cpu_to_le16(nla_get_u16(nla));
55}
56
Alexander Aring9f3295b2014-11-05 20:51:13 +010057static int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040058{
59 struct sk_buff *msg;
60
61 pr_debug("%s\n", __func__);
62
63 msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
64 if (!msg)
65 return -ENOBUFS;
66
David S. Millerbe51da02012-04-01 20:45:25 -040067 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
68 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
69 nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
70 dev->dev_addr) ||
71 nla_put_u8(msg, IEEE802154_ATTR_STATUS, status))
72 goto nla_put_failure;
Johannes Berg2a94fe42013-11-19 15:19:39 +010073 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040074
75nla_put_failure:
76 nlmsg_free(msg);
77 return -ENOBUFS;
78}
79EXPORT_SYMBOL(ieee802154_nl_start_confirm);
80
Eric W. Biederman15e47302012-09-07 20:12:54 +000081static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid,
Varka Bhadram4710d802014-07-02 09:01:09 +053082 u32 seq, int flags, struct net_device *dev)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040083{
84 void *hdr;
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +030085 struct wpan_phy *phy;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +020086 struct ieee802154_mlme_ops *ops;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +010087 __le16 short_addr, pan_id;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040088
89 pr_debug("%s\n", __func__);
90
91 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
Varka Bhadram4710d802014-07-02 09:01:09 +053092 IEEE802154_LIST_IFACE);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040093 if (!hdr)
94 goto out;
95
Phoebe Buckheistere462ded2014-03-31 21:37:46 +020096 ops = ieee802154_mlme_ops(dev);
Alexander Aringbd28a112014-11-05 20:51:18 +010097 phy = dev->ieee802154_ptr->wpan_phy;
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +030098 BUG_ON(!phy);
Alexander Aringbd28a112014-11-05 20:51:18 +010099 get_device(&phy->dev);
Dmitry Eremin-Solenikov0a868b22009-10-29 16:28:52 +0300100
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200101 short_addr = ops->get_short_addr(dev);
102 pan_id = ops->get_pan_id(dev);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100103
David S. Millerbe51da02012-04-01 20:45:25 -0400104 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
105 nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
106 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
107 nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
108 dev->dev_addr) ||
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100109 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) ||
110 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, pan_id))
David S. Millerbe51da02012-04-01 20:45:25 -0400111 goto nla_put_failure;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200112
113 if (ops->get_mac_params) {
114 struct ieee802154_mac_params params;
115
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
169 if (dev->type != ARPHRD_IEEE802154) {
170 dev_put(dev);
171 return NULL;
172 }
173
174 return dev;
175}
176
Johannes Berg1c582d92013-11-14 17:14:41 +0100177int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400178{
179 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100180 struct ieee802154_addr addr;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400181 u8 page;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000182 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400183
184 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
185 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
186 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
187 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
188 !info->attrs[IEEE802154_ATTR_CAPABILITY])
189 return -EINVAL;
190
191 dev = ieee802154_nl_get_dev(info);
192 if (!dev)
193 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000194 if (!ieee802154_mlme_ops(dev)->assoc_req)
195 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400196
197 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100198 addr.mode = IEEE802154_ADDR_LONG;
199 addr.extended_addr = nla_get_hwaddr(
200 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400201 } else {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100202 addr.mode = IEEE802154_ADDR_SHORT;
203 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400204 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
205 }
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100206 addr.pan_id = nla_get_shortaddr(
207 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400208
209 if (info->attrs[IEEE802154_ATTR_PAGE])
210 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
211 else
212 page = 0;
213
214 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
215 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
216 page,
217 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
218
Werner Almesberger56aa0912013-04-04 06:32:35 +0000219out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400220 dev_put(dev);
221 return ret;
222}
223
Johannes Berg1c582d92013-11-14 17:14:41 +0100224int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400225{
226 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100227 struct ieee802154_addr addr;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000228 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400229
230 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
231 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
232 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
233 return -EINVAL;
234
235 dev = ieee802154_nl_get_dev(info);
236 if (!dev)
237 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000238 if (!ieee802154_mlme_ops(dev)->assoc_resp)
239 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400240
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100241 addr.mode = IEEE802154_ADDR_LONG;
242 addr.extended_addr = nla_get_hwaddr(
243 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
244 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400245
246 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100247 nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400248 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
249
Werner Almesberger56aa0912013-04-04 06:32:35 +0000250out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400251 dev_put(dev);
252 return ret;
253}
254
Johannes Berg1c582d92013-11-14 17:14:41 +0100255int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400256{
257 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100258 struct ieee802154_addr addr;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000259 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400260
261 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
Varka Bhadram4710d802014-07-02 09:01:09 +0530262 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400263 !info->attrs[IEEE802154_ATTR_REASON])
264 return -EINVAL;
265
266 dev = ieee802154_nl_get_dev(info);
267 if (!dev)
268 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000269 if (!ieee802154_mlme_ops(dev)->disassoc_req)
270 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400271
272 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100273 addr.mode = IEEE802154_ADDR_LONG;
274 addr.extended_addr = nla_get_hwaddr(
275 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400276 } else {
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100277 addr.mode = IEEE802154_ADDR_SHORT;
278 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400279 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
280 }
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100281 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400282
283 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
284 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
285
Werner Almesberger56aa0912013-04-04 06:32:35 +0000286out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400287 dev_put(dev);
288 return ret;
289}
290
Varka Bhadram4710d802014-07-02 09:01:09 +0530291/* PANid, channel, beacon_order = 15, superframe_order = 15,
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400292 * PAN_coordinator, battery_life_extension = 0,
293 * coord_realignment = 0, security_enable = 0
294*/
Johannes Berg1c582d92013-11-14 17:14:41 +0100295int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400296{
297 struct net_device *dev;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100298 struct ieee802154_addr addr;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400299
300 u8 channel, bcn_ord, sf_ord;
301 u8 page;
302 int pan_coord, blx, coord_realign;
Alexander Aring8f499f92014-11-02 04:18:39 +0100303 int ret = -EBUSY;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400304
305 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
306 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
307 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
308 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
309 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
310 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
311 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
312 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
313 )
314 return -EINVAL;
315
316 dev = ieee802154_nl_get_dev(info);
317 if (!dev)
318 return -ENODEV;
Alexander Aring8f499f92014-11-02 04:18:39 +0100319
320 if (netif_running(dev))
Werner Almesberger56aa0912013-04-04 06:32:35 +0000321 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400322
Alexander Aring8f499f92014-11-02 04:18:39 +0100323 if (!ieee802154_mlme_ops(dev)->start_req) {
324 ret = -EOPNOTSUPP;
325 goto out;
326 }
327
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100328 addr.mode = IEEE802154_ADDR_SHORT;
329 addr.short_addr = nla_get_shortaddr(
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400330 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100331 addr.pan_id = nla_get_shortaddr(
332 info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400333
334 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
335 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
336 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
337 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
338 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
339 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
340
341 if (info->attrs[IEEE802154_ATTR_PAGE])
342 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
343 else
344 page = 0;
345
346
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100347 if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400348 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
349 dev_put(dev);
350 return -EINVAL;
351 }
352
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100353 rtnl_lock();
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400354 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
355 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100356 rtnl_unlock();
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400357
Alexander Aring9f3295b2014-11-05 20:51:13 +0100358 /* FIXME: add validation for unused parameters to be sane
359 * for SoftMAC
360 */
361 ieee802154_nl_start_confirm(dev, IEEE802154_SUCCESS);
362
Werner Almesberger56aa0912013-04-04 06:32:35 +0000363out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400364 dev_put(dev);
365 return ret;
366}
367
Johannes Berg1c582d92013-11-14 17:14:41 +0100368int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400369{
370 struct net_device *dev;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000371 int ret = -EOPNOTSUPP;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400372 u8 type;
373 u32 channels;
374 u8 duration;
375 u8 page;
376
377 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
378 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
379 !info->attrs[IEEE802154_ATTR_DURATION])
380 return -EINVAL;
381
382 dev = ieee802154_nl_get_dev(info);
383 if (!dev)
384 return -ENODEV;
Werner Almesberger56aa0912013-04-04 06:32:35 +0000385 if (!ieee802154_mlme_ops(dev)->scan_req)
386 goto out;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400387
388 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
389 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
390 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
391
392 if (info->attrs[IEEE802154_ATTR_PAGE])
393 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
394 else
395 page = 0;
396
397
Varka Bhadram4710d802014-07-02 09:01:09 +0530398 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
399 page, duration);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400400
Werner Almesberger56aa0912013-04-04 06:32:35 +0000401out:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400402 dev_put(dev);
403 return ret;
404}
405
Johannes Berg1c582d92013-11-14 17:14:41 +0100406int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400407{
408 /* Request for interface name, index, type, IEEE address,
Varka Bhadram4710d802014-07-02 09:01:09 +0530409 * PAN Id, short address
410 */
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400411 struct sk_buff *msg;
412 struct net_device *dev = NULL;
413 int rc = -ENOBUFS;
414
415 pr_debug("%s\n", __func__);
416
417 dev = ieee802154_nl_get_dev(info);
418 if (!dev)
419 return -ENODEV;
420
Thomas Graf58050fc2012-06-28 03:57:45 +0000421 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400422 if (!msg)
423 goto out_dev;
424
Eric W. Biederman15e47302012-09-07 20:12:54 +0000425 rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq,
Varka Bhadram4710d802014-07-02 09:01:09 +0530426 0, dev);
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400427 if (rc < 0)
428 goto out_free;
429
430 dev_put(dev);
431
432 return genlmsg_reply(msg, info);
433out_free:
434 nlmsg_free(msg);
435out_dev:
436 dev_put(dev);
437 return rc;
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400438}
439
Johannes Berg1c582d92013-11-14 17:14:41 +0100440int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400441{
442 struct net *net = sock_net(skb->sk);
443 struct net_device *dev;
444 int idx;
445 int s_idx = cb->args[0];
446
447 pr_debug("%s\n", __func__);
448
449 idx = 0;
450 for_each_netdev(net, dev) {
451 if (idx < s_idx || (dev->type != ARPHRD_IEEE802154))
452 goto cont;
453
Eric W. Biederman15e47302012-09-07 20:12:54 +0000454 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid,
Varka Bhadram4710d802014-07-02 09:01:09 +0530455 cb->nlh->nlmsg_seq,
456 NLM_F_MULTI, dev) < 0)
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400457 break;
458cont:
459 idx++;
460 }
461 cb->args[0] = idx;
462
463 return skb->len;
464}
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200465
466int ieee802154_set_macparams(struct sk_buff *skb, struct genl_info *info)
467{
468 struct net_device *dev = NULL;
469 struct ieee802154_mlme_ops *ops;
470 struct ieee802154_mac_params params;
471 struct wpan_phy *phy;
472 int rc = -EINVAL;
473
474 pr_debug("%s\n", __func__);
475
476 dev = ieee802154_nl_get_dev(info);
477 if (!dev)
478 return -ENODEV;
479
480 ops = ieee802154_mlme_ops(dev);
481
482 if (!ops->get_mac_params || !ops->set_mac_params) {
483 rc = -EOPNOTSUPP;
484 goto out;
485 }
486
487 if (netif_running(dev)) {
488 rc = -EBUSY;
489 goto out;
490 }
491
492 if (!info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
493 !info->attrs[IEEE802154_ATTR_CCA_MODE] &&
494 !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] &&
495 !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] &&
496 !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] &&
497 !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] &&
498 !info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
499 goto out;
500
Alexander Aringbd28a112014-11-05 20:51:18 +0100501 phy = dev->ieee802154_ptr->wpan_phy;
502 get_device(&phy->dev);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200503
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100504 rtnl_lock();
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200505 ops->get_mac_params(dev, &params);
506
507 if (info->attrs[IEEE802154_ATTR_TXPOWER])
508 params.transmit_power = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]);
509
510 if (info->attrs[IEEE802154_ATTR_LBT_ENABLED])
511 params.lbt = nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
512
513 if (info->attrs[IEEE802154_ATTR_CCA_MODE])
514 params.cca_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
515
516 if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])
517 params.cca_ed_level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]);
518
519 if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES])
520 params.csma_retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]);
521
522 if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE])
523 params.min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]);
524
525 if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE])
526 params.max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]);
527
528 if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
529 params.frame_retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]);
530
531 rc = ops->set_mac_params(dev, &params);
Alexander Aring7bea1ea2014-11-09 08:36:57 +0100532 rtnl_unlock();
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200533
534 wpan_phy_put(phy);
535 dev_put(dev);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200536
Alexander Aringa543c592014-10-28 18:21:23 +0100537 return 0;
538
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200539out:
540 dev_put(dev);
541 return rc;
542}
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200543
544
545
546static int
547ieee802154_llsec_parse_key_id(struct genl_info *info,
548 struct ieee802154_llsec_key_id *desc)
549{
550 memset(desc, 0, sizeof(*desc));
551
552 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE])
553 return -EINVAL;
554
555 desc->mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]);
556
557 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
558 if (!info->attrs[IEEE802154_ATTR_PAN_ID] &&
559 !(info->attrs[IEEE802154_ATTR_SHORT_ADDR] ||
560 info->attrs[IEEE802154_ATTR_HW_ADDR]))
561 return -EINVAL;
562
563 desc->device_addr.pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
564
565 if (info->attrs[IEEE802154_ATTR_SHORT_ADDR]) {
566 desc->device_addr.mode = IEEE802154_ADDR_SHORT;
567 desc->device_addr.short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
568 } else {
569 desc->device_addr.mode = IEEE802154_ADDR_LONG;
570 desc->device_addr.extended_addr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
571 }
572 }
573
574 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
575 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID])
576 return -EINVAL;
577
578 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
579 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT])
580 return -EINVAL;
581
582 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
583 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED])
584 return -EINVAL;
585
586 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT)
587 desc->id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]);
588
589 switch (desc->mode) {
590 case IEEE802154_SCF_KEY_SHORT_INDEX:
591 {
592 u32 source = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]);
Varka Bhadram4710d802014-07-02 09:01:09 +0530593
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200594 desc->short_source = cpu_to_le32(source);
595 break;
596 }
597 case IEEE802154_SCF_KEY_HW_INDEX:
598 desc->extended_source = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]);
599 break;
600 }
601
602 return 0;
603}
604
605static int
606ieee802154_llsec_fill_key_id(struct sk_buff *msg,
607 const struct ieee802154_llsec_key_id *desc)
608{
609 if (nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_MODE, desc->mode))
610 return -EMSGSIZE;
611
612 if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
613 if (nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID,
614 desc->device_addr.pan_id))
615 return -EMSGSIZE;
616
617 if (desc->device_addr.mode == IEEE802154_ADDR_SHORT &&
618 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
619 desc->device_addr.short_addr))
620 return -EMSGSIZE;
621
622 if (desc->device_addr.mode == IEEE802154_ADDR_LONG &&
623 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR,
624 desc->device_addr.extended_addr))
625 return -EMSGSIZE;
626 }
627
628 if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
629 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_ID, desc->id))
630 return -EMSGSIZE;
631
632 if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
633 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT,
634 le32_to_cpu(desc->short_source)))
635 return -EMSGSIZE;
636
637 if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
638 nla_put_hwaddr(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED,
639 desc->extended_source))
640 return -EMSGSIZE;
641
642 return 0;
643}
644
645int ieee802154_llsec_getparams(struct sk_buff *skb, struct genl_info *info)
646{
647 struct sk_buff *msg;
648 struct net_device *dev = NULL;
649 int rc = -ENOBUFS;
650 struct ieee802154_mlme_ops *ops;
651 void *hdr;
652 struct ieee802154_llsec_params params;
653
654 pr_debug("%s\n", __func__);
655
656 dev = ieee802154_nl_get_dev(info);
657 if (!dev)
658 return -ENODEV;
659
660 ops = ieee802154_mlme_ops(dev);
Dan Carpenterb3f7a7b2014-05-22 10:53:06 +0300661 if (!ops->llsec) {
662 rc = -EOPNOTSUPP;
663 goto out_dev;
664 }
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200665
666 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
667 if (!msg)
668 goto out_dev;
669
670 hdr = genlmsg_put(msg, 0, info->snd_seq, &nl802154_family, 0,
Varka Bhadram4710d802014-07-02 09:01:09 +0530671 IEEE802154_LLSEC_GETPARAMS);
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200672 if (!hdr)
673 goto out_free;
674
675 rc = ops->llsec->get_params(dev, &params);
676 if (rc < 0)
677 goto out_free;
678
679 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
680 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
681 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_ENABLED, params.enabled) ||
682 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVEL, params.out_level) ||
683 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
684 be32_to_cpu(params.frame_counter)) ||
685 ieee802154_llsec_fill_key_id(msg, &params.out_key))
686 goto out_free;
687
688 dev_put(dev);
689
690 return ieee802154_nl_reply(msg, info);
691out_free:
692 nlmsg_free(msg);
693out_dev:
694 dev_put(dev);
695 return rc;
696}
697
698int ieee802154_llsec_setparams(struct sk_buff *skb, struct genl_info *info)
699{
700 struct net_device *dev = NULL;
701 int rc = -EINVAL;
702 struct ieee802154_mlme_ops *ops;
703 struct ieee802154_llsec_params params;
704 int changed = 0;
705
706 pr_debug("%s\n", __func__);
707
708 dev = ieee802154_nl_get_dev(info);
709 if (!dev)
710 return -ENODEV;
711
712 if (!info->attrs[IEEE802154_ATTR_LLSEC_ENABLED] &&
713 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE] &&
714 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL])
715 goto out;
716
717 ops = ieee802154_mlme_ops(dev);
718 if (!ops->llsec) {
719 rc = -EOPNOTSUPP;
720 goto out;
721 }
722
723 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL] &&
724 nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) > 7)
725 goto out;
726
727 if (info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]) {
728 params.enabled = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]);
729 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
730 }
731
732 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) {
733 if (ieee802154_llsec_parse_key_id(info, &params.out_key))
734 goto out;
735
736 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
737 }
738
739 if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) {
740 params.out_level = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]);
741 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
742 }
743
744 if (info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]) {
745 u32 fc = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
746
747 params.frame_counter = cpu_to_be32(fc);
748 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
749 }
750
751 rc = ops->llsec->set_params(dev, &params, changed);
752
753 dev_put(dev);
754
755 return rc;
756out:
757 dev_put(dev);
758 return rc;
759}
760
761
762
763struct 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) {
785 if (idx < first_dev || dev->type != ARPHRD_IEEE802154)
786 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
835
836
837static int
838ieee802154_llsec_parse_key(struct genl_info *info,
839 struct ieee802154_llsec_key *key)
840{
841 u8 frames;
842 u32 commands[256 / 32];
843
844 memset(key, 0, sizeof(*key));
845
846 if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES] ||
847 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES])
848 return -EINVAL;
849
850 frames = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES]);
851 if ((frames & BIT(IEEE802154_FC_TYPE_MAC_CMD)) &&
852 !info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS])
853 return -EINVAL;
854
855 if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) {
856 nla_memcpy(commands,
857 info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS],
858 256 / 8);
859
860 if (commands[0] || commands[1] || commands[2] || commands[3] ||
861 commands[4] || commands[5] || commands[6] ||
862 commands[7] >= BIT(IEEE802154_CMD_GTS_REQ + 1))
863 return -EINVAL;
864
865 key->cmd_frame_ids = commands[7];
866 }
867
868 key->frame_types = frames;
869
870 nla_memcpy(key->key, info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES],
871 IEEE802154_LLSEC_KEY_SIZE);
872
873 return 0;
874}
875
876static int llsec_add_key(struct net_device *dev, struct genl_info *info)
877{
878 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
879 struct ieee802154_llsec_key key;
880 struct ieee802154_llsec_key_id id;
881
882 if (ieee802154_llsec_parse_key(info, &key) ||
883 ieee802154_llsec_parse_key_id(info, &id))
884 return -EINVAL;
885
886 return ops->llsec->add_key(dev, &id, &key);
887}
888
889int ieee802154_llsec_add_key(struct sk_buff *skb, struct genl_info *info)
890{
891 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
892 (NLM_F_CREATE | NLM_F_EXCL))
893 return -EINVAL;
894
895 return ieee802154_nl_llsec_change(skb, info, llsec_add_key);
896}
897
898static int llsec_remove_key(struct net_device *dev, struct genl_info *info)
899{
900 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
901 struct ieee802154_llsec_key_id id;
902
903 if (ieee802154_llsec_parse_key_id(info, &id))
904 return -EINVAL;
905
906 return ops->llsec->del_key(dev, &id);
907}
908
909int ieee802154_llsec_del_key(struct sk_buff *skb, struct genl_info *info)
910{
911 return ieee802154_nl_llsec_change(skb, info, llsec_remove_key);
912}
913
914static int
915ieee802154_nl_fill_key(struct sk_buff *msg, u32 portid, u32 seq,
916 const struct ieee802154_llsec_key_entry *key,
917 const struct net_device *dev)
918{
919 void *hdr;
920 u32 commands[256 / 32];
921
922 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
923 IEEE802154_LLSEC_LIST_KEY);
924 if (!hdr)
925 goto out;
926
927 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
928 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
929 ieee802154_llsec_fill_key_id(msg, &key->id) ||
930 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES,
931 key->key->frame_types))
932 goto nla_put_failure;
933
934 if (key->key->frame_types & BIT(IEEE802154_FC_TYPE_MAC_CMD)) {
935 memset(commands, 0, sizeof(commands));
936 commands[7] = key->key->cmd_frame_ids;
937 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS,
938 sizeof(commands), commands))
939 goto nla_put_failure;
940 }
941
942 if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_BYTES,
943 IEEE802154_LLSEC_KEY_SIZE, key->key->key))
944 goto nla_put_failure;
945
946 genlmsg_end(msg, hdr);
947 return 0;
948
949nla_put_failure:
950 genlmsg_cancel(msg, hdr);
951out:
952 return -EMSGSIZE;
953}
954
955static int llsec_iter_keys(struct llsec_dump_data *data)
956{
957 struct ieee802154_llsec_key_entry *pos;
958 int rc = 0, idx = 0;
959
960 list_for_each_entry(pos, &data->table->keys, list) {
961 if (idx++ < data->s_idx)
962 continue;
963
964 if (ieee802154_nl_fill_key(data->skb, data->portid,
965 data->nlmsg_seq, pos, data->dev)) {
966 rc = -EMSGSIZE;
967 break;
968 }
969
970 data->s_idx++;
971 }
972
973 return rc;
974}
975
976int ieee802154_llsec_dump_keys(struct sk_buff *skb, struct netlink_callback *cb)
977{
978 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_keys);
979}
980
981
982
983static int
984llsec_parse_dev(struct genl_info *info,
985 struct ieee802154_llsec_device *dev)
986{
987 memset(dev, 0, sizeof(*dev));
988
989 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
990 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
991 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE] ||
992 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE] ||
993 (!!info->attrs[IEEE802154_ATTR_PAN_ID] !=
994 !!info->attrs[IEEE802154_ATTR_SHORT_ADDR]))
995 return -EINVAL;
996
997 if (info->attrs[IEEE802154_ATTR_PAN_ID]) {
998 dev->pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
999 dev->short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
1000 } else {
1001 dev->short_addr = cpu_to_le16(IEEE802154_ADDR_UNDEF);
1002 }
1003
1004 dev->hwaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1005 dev->frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1006 dev->seclevel_exempt = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1007 dev->key_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE]);
1008
1009 if (dev->key_mode >= __IEEE802154_LLSEC_DEVKEY_MAX)
1010 return -EINVAL;
1011
1012 return 0;
1013}
1014
1015static int llsec_add_dev(struct net_device *dev, struct genl_info *info)
1016{
1017 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1018 struct ieee802154_llsec_device desc;
1019
1020 if (llsec_parse_dev(info, &desc))
1021 return -EINVAL;
1022
1023 return ops->llsec->add_dev(dev, &desc);
1024}
1025
1026int ieee802154_llsec_add_dev(struct sk_buff *skb, struct genl_info *info)
1027{
1028 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1029 (NLM_F_CREATE | NLM_F_EXCL))
1030 return -EINVAL;
1031
1032 return ieee802154_nl_llsec_change(skb, info, llsec_add_dev);
1033}
1034
1035static int llsec_del_dev(struct net_device *dev, struct genl_info *info)
1036{
1037 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1038 __le64 devaddr;
1039
1040 if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
1041 return -EINVAL;
1042
1043 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1044
1045 return ops->llsec->del_dev(dev, devaddr);
1046}
1047
1048int ieee802154_llsec_del_dev(struct sk_buff *skb, struct genl_info *info)
1049{
1050 return ieee802154_nl_llsec_change(skb, info, llsec_del_dev);
1051}
1052
1053static int
1054ieee802154_nl_fill_dev(struct sk_buff *msg, u32 portid, u32 seq,
1055 const struct ieee802154_llsec_device *desc,
1056 const struct net_device *dev)
1057{
1058 void *hdr;
1059
1060 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1061 IEEE802154_LLSEC_LIST_DEV);
1062 if (!hdr)
1063 goto out;
1064
1065 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1066 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1067 nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, desc->pan_id) ||
1068 nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
1069 desc->short_addr) ||
1070 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, desc->hwaddr) ||
1071 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1072 desc->frame_counter) ||
1073 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1074 desc->seclevel_exempt) ||
1075 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE, desc->key_mode))
1076 goto nla_put_failure;
1077
1078 genlmsg_end(msg, hdr);
1079 return 0;
1080
1081nla_put_failure:
1082 genlmsg_cancel(msg, hdr);
1083out:
1084 return -EMSGSIZE;
1085}
1086
1087static int llsec_iter_devs(struct llsec_dump_data *data)
1088{
1089 struct ieee802154_llsec_device *pos;
1090 int rc = 0, idx = 0;
1091
1092 list_for_each_entry(pos, &data->table->devices, list) {
1093 if (idx++ < data->s_idx)
1094 continue;
1095
1096 if (ieee802154_nl_fill_dev(data->skb, data->portid,
1097 data->nlmsg_seq, pos, data->dev)) {
1098 rc = -EMSGSIZE;
1099 break;
1100 }
1101
1102 data->s_idx++;
1103 }
1104
1105 return rc;
1106}
1107
1108int ieee802154_llsec_dump_devs(struct sk_buff *skb, struct netlink_callback *cb)
1109{
1110 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devs);
1111}
1112
1113
1114
1115static int llsec_add_devkey(struct net_device *dev, struct genl_info *info)
1116{
1117 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1118 struct ieee802154_llsec_device_key key;
1119 __le64 devaddr;
1120
1121 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
1122 !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1123 ieee802154_llsec_parse_key_id(info, &key.key_id))
1124 return -EINVAL;
1125
1126 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1127 key.frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
1128
1129 return ops->llsec->add_devkey(dev, devaddr, &key);
1130}
1131
1132int ieee802154_llsec_add_devkey(struct sk_buff *skb, struct genl_info *info)
1133{
1134 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1135 (NLM_F_CREATE | NLM_F_EXCL))
1136 return -EINVAL;
1137
1138 return ieee802154_nl_llsec_change(skb, info, llsec_add_devkey);
1139}
1140
1141static int llsec_del_devkey(struct net_device *dev, struct genl_info *info)
1142{
1143 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1144 struct ieee802154_llsec_device_key key;
1145 __le64 devaddr;
1146
1147 if (!info->attrs[IEEE802154_ATTR_HW_ADDR] ||
1148 ieee802154_llsec_parse_key_id(info, &key.key_id))
1149 return -EINVAL;
1150
1151 devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
1152
1153 return ops->llsec->del_devkey(dev, devaddr, &key);
1154}
1155
1156int ieee802154_llsec_del_devkey(struct sk_buff *skb, struct genl_info *info)
1157{
1158 return ieee802154_nl_llsec_change(skb, info, llsec_del_devkey);
1159}
1160
1161static int
1162ieee802154_nl_fill_devkey(struct sk_buff *msg, u32 portid, u32 seq,
1163 __le64 devaddr,
1164 const struct ieee802154_llsec_device_key *devkey,
1165 const struct net_device *dev)
1166{
1167 void *hdr;
1168
1169 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1170 IEEE802154_LLSEC_LIST_DEVKEY);
1171 if (!hdr)
1172 goto out;
1173
1174 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1175 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1176 nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, devaddr) ||
1177 nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
1178 devkey->frame_counter) ||
1179 ieee802154_llsec_fill_key_id(msg, &devkey->key_id))
1180 goto nla_put_failure;
1181
1182 genlmsg_end(msg, hdr);
1183 return 0;
1184
1185nla_put_failure:
1186 genlmsg_cancel(msg, hdr);
1187out:
1188 return -EMSGSIZE;
1189}
1190
1191static int llsec_iter_devkeys(struct llsec_dump_data *data)
1192{
1193 struct ieee802154_llsec_device *dpos;
1194 struct ieee802154_llsec_device_key *kpos;
1195 int rc = 0, idx = 0, idx2;
1196
1197 list_for_each_entry(dpos, &data->table->devices, list) {
1198 if (idx++ < data->s_idx)
1199 continue;
1200
1201 idx2 = 0;
1202
1203 list_for_each_entry(kpos, &dpos->keys, list) {
1204 if (idx2++ < data->s_idx2)
1205 continue;
1206
1207 if (ieee802154_nl_fill_devkey(data->skb, data->portid,
1208 data->nlmsg_seq,
1209 dpos->hwaddr, kpos,
1210 data->dev)) {
1211 return rc = -EMSGSIZE;
1212 }
1213
1214 data->s_idx2++;
1215 }
1216
1217 data->s_idx++;
1218 }
1219
1220 return rc;
1221}
1222
1223int ieee802154_llsec_dump_devkeys(struct sk_buff *skb,
1224 struct netlink_callback *cb)
1225{
1226 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devkeys);
1227}
1228
1229
1230
1231static int
1232llsec_parse_seclevel(struct genl_info *info,
1233 struct ieee802154_llsec_seclevel *sl)
1234{
1235 memset(sl, 0, sizeof(*sl));
1236
1237 if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE] ||
1238 !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS] ||
1239 !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE])
1240 return -EINVAL;
1241
1242 sl->frame_type = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE]);
1243 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD) {
1244 if (!info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID])
1245 return -EINVAL;
1246
1247 sl->cmd_frame_id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]);
1248 }
1249
1250 sl->sec_levels = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS]);
1251 sl->device_override = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
1252
1253 return 0;
1254}
1255
1256static int llsec_add_seclevel(struct net_device *dev, struct genl_info *info)
1257{
1258 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1259 struct ieee802154_llsec_seclevel sl;
1260
1261 if (llsec_parse_seclevel(info, &sl))
1262 return -EINVAL;
1263
1264 return ops->llsec->add_seclevel(dev, &sl);
1265}
1266
1267int ieee802154_llsec_add_seclevel(struct sk_buff *skb, struct genl_info *info)
1268{
1269 if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
1270 (NLM_F_CREATE | NLM_F_EXCL))
1271 return -EINVAL;
1272
1273 return ieee802154_nl_llsec_change(skb, info, llsec_add_seclevel);
1274}
1275
1276static int llsec_del_seclevel(struct net_device *dev, struct genl_info *info)
1277{
1278 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
1279 struct ieee802154_llsec_seclevel sl;
1280
1281 if (llsec_parse_seclevel(info, &sl))
1282 return -EINVAL;
1283
1284 return ops->llsec->del_seclevel(dev, &sl);
1285}
1286
1287int ieee802154_llsec_del_seclevel(struct sk_buff *skb, struct genl_info *info)
1288{
1289 return ieee802154_nl_llsec_change(skb, info, llsec_del_seclevel);
1290}
1291
1292static int
1293ieee802154_nl_fill_seclevel(struct sk_buff *msg, u32 portid, u32 seq,
1294 const struct ieee802154_llsec_seclevel *sl,
1295 const struct net_device *dev)
1296{
1297 void *hdr;
1298
1299 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
1300 IEEE802154_LLSEC_LIST_SECLEVEL);
1301 if (!hdr)
1302 goto out;
1303
1304 if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
1305 nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
1306 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_FRAME_TYPE, sl->frame_type) ||
1307 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVELS, sl->sec_levels) ||
1308 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
1309 sl->device_override))
1310 goto nla_put_failure;
1311
1312 if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD &&
1313 nla_put_u8(msg, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID,
1314 sl->cmd_frame_id))
1315 goto nla_put_failure;
1316
1317 genlmsg_end(msg, hdr);
1318 return 0;
1319
1320nla_put_failure:
1321 genlmsg_cancel(msg, hdr);
1322out:
1323 return -EMSGSIZE;
1324}
1325
1326static int llsec_iter_seclevels(struct llsec_dump_data *data)
1327{
1328 struct ieee802154_llsec_seclevel *pos;
1329 int rc = 0, idx = 0;
1330
1331 list_for_each_entry(pos, &data->table->security_levels, list) {
1332 if (idx++ < data->s_idx)
1333 continue;
1334
1335 if (ieee802154_nl_fill_seclevel(data->skb, data->portid,
1336 data->nlmsg_seq, pos,
1337 data->dev)) {
1338 rc = -EMSGSIZE;
1339 break;
1340 }
1341
1342 data->s_idx++;
1343 }
1344
1345 return rc;
1346}
1347
1348int ieee802154_llsec_dump_seclevels(struct sk_buff *skb,
1349 struct netlink_callback *cb)
1350{
1351 return ieee802154_llsec_dump_table(skb, cb, llsec_iter_seclevels);
1352}