blob: 5d914d30e0b14ce34b4d959b6641c1fbcb7fdd14 [file] [log] [blame]
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +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-Solenikov1eaa9d02009-09-15 17:04:44 +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
21#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +030023#include <linux/if_arp.h>
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040024#include <net/netlink.h>
25#include <net/genetlink.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020026#include <net/cfg802154.h>
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +030027#include <net/af_ieee802154.h>
28#include <net/ieee802154_netdev.h>
29#include <net/rtnetlink.h> /* for rtnl_{un,}lock */
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040030#include <linux/nl802154.h>
31
32#include "ieee802154.h"
Alexander Aring4a9a8162014-11-02 04:18:38 +010033#include "rdev-ops.h"
34#include "core.h"
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040035
Eric W. Biederman15e47302012-09-07 20:12:54 +000036static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
Varka Bhadram4710d802014-07-02 09:01:09 +053037 u32 seq, int flags, struct wpan_phy *phy)
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040038{
39 void *hdr;
40 int i, pages = 0;
41 uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL);
42
43 pr_debug("%s\n", __func__);
44
45 if (!buf)
Jesper Juhlb9cabe52011-06-12 04:28:16 +000046 return -EMSGSIZE;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040047
48 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
Varka Bhadram4710d802014-07-02 09:01:09 +053049 IEEE802154_LIST_PHY);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040050 if (!hdr)
51 goto out;
52
53 mutex_lock(&phy->pib_lock);
David S. Millerbe51da02012-04-01 20:45:25 -040054 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
55 nla_put_u8(msg, IEEE802154_ATTR_PAGE, phy->current_page) ||
56 nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel))
57 goto nla_put_failure;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040058 for (i = 0; i < 32; i++) {
59 if (phy->channels_supported[i])
60 buf[pages++] = phy->channels_supported[i] | (i << 27);
61 }
David S. Millerbe51da02012-04-01 20:45:25 -040062 if (pages &&
63 nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
64 pages * sizeof(uint32_t), buf))
65 goto nla_put_failure;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040066 mutex_unlock(&phy->pib_lock);
Jesper Juhlb9cabe52011-06-12 04:28:16 +000067 kfree(buf);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040068 return genlmsg_end(msg, hdr);
69
70nla_put_failure:
71 mutex_unlock(&phy->pib_lock);
72 genlmsg_cancel(msg, hdr);
73out:
74 kfree(buf);
75 return -EMSGSIZE;
76}
77
Johannes Berg1c582d92013-11-14 17:14:41 +010078int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040079{
80 /* Request for interface name, index, type, IEEE address,
Varka Bhadram4710d802014-07-02 09:01:09 +053081 * PAN Id, short address
82 */
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040083 struct sk_buff *msg;
84 struct wpan_phy *phy;
85 const char *name;
86 int rc = -ENOBUFS;
87
88 pr_debug("%s\n", __func__);
89
90 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
91 return -EINVAL;
92
93 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
94 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
95 return -EINVAL; /* phy name should be null-terminated */
96
97
98 phy = wpan_phy_find(name);
99 if (!phy)
100 return -ENODEV;
101
Thomas Graf58050fc2012-06-28 03:57:45 +0000102 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400103 if (!msg)
104 goto out_dev;
105
Eric W. Biederman15e47302012-09-07 20:12:54 +0000106 rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq,
Varka Bhadram4710d802014-07-02 09:01:09 +0530107 0, phy);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400108 if (rc < 0)
109 goto out_free;
110
111 wpan_phy_put(phy);
112
113 return genlmsg_reply(msg, info);
114out_free:
115 nlmsg_free(msg);
116out_dev:
117 wpan_phy_put(phy);
118 return rc;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400119}
120
121struct dump_phy_data {
122 struct sk_buff *skb;
123 struct netlink_callback *cb;
124 int idx, s_idx;
125};
126
127static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
128{
129 int rc;
130 struct dump_phy_data *data = _data;
131
132 pr_debug("%s\n", __func__);
133
134 if (data->idx++ < data->s_idx)
135 return 0;
136
137 rc = ieee802154_nl_fill_phy(data->skb,
Varka Bhadram4710d802014-07-02 09:01:09 +0530138 NETLINK_CB(data->cb->skb).portid,
139 data->cb->nlh->nlmsg_seq,
140 NLM_F_MULTI,
141 phy);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400142
143 if (rc < 0) {
144 data->idx--;
145 return rc;
146 }
147
148 return 0;
149}
150
Johannes Berg1c582d92013-11-14 17:14:41 +0100151int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400152{
153 struct dump_phy_data data = {
154 .cb = cb,
155 .skb = skb,
156 .s_idx = cb->args[0],
157 .idx = 0,
158 };
159
160 pr_debug("%s\n", __func__);
161
162 wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
163
164 cb->args[0] = data.idx;
165
166 return skb->len;
167}
168
Johannes Berg1c582d92013-11-14 17:14:41 +0100169int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300170{
171 struct sk_buff *msg;
172 struct wpan_phy *phy;
173 const char *name;
174 const char *devname;
175 int rc = -ENOBUFS;
176 struct net_device *dev;
alex.bluesman.smirnov@gmail.com90c049b2012-05-15 20:50:27 +0000177 int type = __IEEE802154_DEV_INVALID;
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300178
179 pr_debug("%s\n", __func__);
180
181 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
182 return -EINVAL;
183
184 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
185 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
186 return -EINVAL; /* phy name should be null-terminated */
187
188 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
189 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
190 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
191 != '\0')
192 return -EINVAL; /* phy name should be null-terminated */
193 } else {
194 devname = "wpan%d";
195 }
196
197 if (strlen(devname) >= IFNAMSIZ)
198 return -ENAMETOOLONG;
199
200 phy = wpan_phy_find(name);
201 if (!phy)
202 return -ENODEV;
203
204 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
205 if (!msg)
206 goto out_dev;
207
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300208 if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
209 nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
210 IEEE802154_ADDR_LEN) {
211 rc = -EINVAL;
212 goto nla_put_failure;
213 }
214
alex.bluesman.smirnov@gmail.com90c049b2012-05-15 20:50:27 +0000215 if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
216 type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
Christian Engelmayer267d29a2014-01-11 22:19:30 +0100217 if (type >= __IEEE802154_DEV_MAX) {
218 rc = -EINVAL;
219 goto nla_put_failure;
220 }
alex.bluesman.smirnov@gmail.com90c049b2012-05-15 20:50:27 +0000221 }
222
Alexander Aring4a9a8162014-11-02 04:18:38 +0100223 dev = rdev_add_virtual_intf_deprecated(wpan_phy_to_rdev(phy), devname,
224 type);
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300225 if (IS_ERR(dev)) {
226 rc = PTR_ERR(dev);
227 goto nla_put_failure;
228 }
229
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300230 if (info->attrs[IEEE802154_ATTR_HW_ADDR]) {
231 struct sockaddr addr;
232
233 addr.sa_family = ARPHRD_IEEE802154;
234 nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR],
Varka Bhadram4710d802014-07-02 09:01:09 +0530235 IEEE802154_ADDR_LEN);
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300236
Varka Bhadram4710d802014-07-02 09:01:09 +0530237 /* strangely enough, some callbacks (inetdev_event) from
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300238 * dev_set_mac_address require RTNL_LOCK
239 */
240 rtnl_lock();
241 rc = dev_set_mac_address(dev, &addr);
242 rtnl_unlock();
243 if (rc)
244 goto dev_unregister;
245 }
246
David S. Millerbe51da02012-04-01 20:45:25 -0400247 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
248 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name))
249 goto nla_put_failure;
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300250 dev_put(dev);
251
252 wpan_phy_put(phy);
253
254 return ieee802154_nl_reply(msg, info);
255
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300256dev_unregister:
257 rtnl_lock(); /* del_iface must be called with RTNL lock */
Alexander Aring4a9a8162014-11-02 04:18:38 +0100258 rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300259 dev_put(dev);
260 rtnl_unlock();
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300261nla_put_failure:
262 nlmsg_free(msg);
263out_dev:
264 wpan_phy_put(phy);
265 return rc;
266}
267
Johannes Berg1c582d92013-11-14 17:14:41 +0100268int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300269{
270 struct sk_buff *msg;
271 struct wpan_phy *phy;
272 const char *name;
273 int rc;
274 struct net_device *dev;
275
276 pr_debug("%s\n", __func__);
277
278 if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
279 return -EINVAL;
280
281 name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
282 if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
283 return -EINVAL; /* name should be null-terminated */
284
285 dev = dev_get_by_name(genl_info_net(info), name);
286 if (!dev)
287 return -ENODEV;
288
289 phy = ieee802154_mlme_ops(dev)->get_phy(dev);
290 BUG_ON(!phy);
291
292 rc = -EINVAL;
293 /* phy name is optional, but should be checked if it's given */
294 if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
295 struct wpan_phy *phy2;
296
297 const char *pname =
298 nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
299 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
300 != '\0')
301 /* name should be null-terminated */
302 goto out_dev;
303
304 phy2 = wpan_phy_find(pname);
305 if (!phy2)
306 goto out_dev;
307
308 if (phy != phy2) {
309 wpan_phy_put(phy2);
310 goto out_dev;
311 }
312 }
313
314 rc = -ENOBUFS;
315
316 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
317 if (!msg)
318 goto out_dev;
319
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300320 rtnl_lock();
Alexander Aring4a9a8162014-11-02 04:18:38 +0100321 rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300322
323 /* We don't have device anymore */
324 dev_put(dev);
325 dev = NULL;
326
327 rtnl_unlock();
328
David S. Millerbe51da02012-04-01 20:45:25 -0400329 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
330 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
331 goto nla_put_failure;
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300332 wpan_phy_put(phy);
333
334 return ieee802154_nl_reply(msg, info);
335
336nla_put_failure:
337 nlmsg_free(msg);
338out_dev:
339 wpan_phy_put(phy);
340 if (dev)
341 dev_put(dev);
342
343 return rc;
344}