blob: 0afe760ff5126d6e24530236377d6b9e19b71728 [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"
33
Eric W. Biederman15e47302012-09-07 20:12:54 +000034static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
Varka Bhadram4710d802014-07-02 09:01:09 +053035 u32 seq, int flags, struct wpan_phy *phy)
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040036{
37 void *hdr;
38 int i, pages = 0;
39 uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL);
40
41 pr_debug("%s\n", __func__);
42
43 if (!buf)
Jesper Juhlb9cabe52011-06-12 04:28:16 +000044 return -EMSGSIZE;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040045
46 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
Varka Bhadram4710d802014-07-02 09:01:09 +053047 IEEE802154_LIST_PHY);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040048 if (!hdr)
49 goto out;
50
51 mutex_lock(&phy->pib_lock);
David S. Millerbe51da02012-04-01 20:45:25 -040052 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
53 nla_put_u8(msg, IEEE802154_ATTR_PAGE, phy->current_page) ||
54 nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel))
55 goto nla_put_failure;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040056 for (i = 0; i < 32; i++) {
57 if (phy->channels_supported[i])
58 buf[pages++] = phy->channels_supported[i] | (i << 27);
59 }
David S. Millerbe51da02012-04-01 20:45:25 -040060 if (pages &&
61 nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
62 pages * sizeof(uint32_t), buf))
63 goto nla_put_failure;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040064 mutex_unlock(&phy->pib_lock);
Jesper Juhlb9cabe52011-06-12 04:28:16 +000065 kfree(buf);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040066 return genlmsg_end(msg, hdr);
67
68nla_put_failure:
69 mutex_unlock(&phy->pib_lock);
70 genlmsg_cancel(msg, hdr);
71out:
72 kfree(buf);
73 return -EMSGSIZE;
74}
75
Johannes Berg1c582d92013-11-14 17:14:41 +010076int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040077{
78 /* Request for interface name, index, type, IEEE address,
Varka Bhadram4710d802014-07-02 09:01:09 +053079 * PAN Id, short address
80 */
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040081 struct sk_buff *msg;
82 struct wpan_phy *phy;
83 const char *name;
84 int rc = -ENOBUFS;
85
86 pr_debug("%s\n", __func__);
87
88 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
89 return -EINVAL;
90
91 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
92 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
93 return -EINVAL; /* phy name should be null-terminated */
94
95
96 phy = wpan_phy_find(name);
97 if (!phy)
98 return -ENODEV;
99
Thomas Graf58050fc2012-06-28 03:57:45 +0000100 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400101 if (!msg)
102 goto out_dev;
103
Eric W. Biederman15e47302012-09-07 20:12:54 +0000104 rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq,
Varka Bhadram4710d802014-07-02 09:01:09 +0530105 0, phy);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400106 if (rc < 0)
107 goto out_free;
108
109 wpan_phy_put(phy);
110
111 return genlmsg_reply(msg, info);
112out_free:
113 nlmsg_free(msg);
114out_dev:
115 wpan_phy_put(phy);
116 return rc;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400117}
118
119struct dump_phy_data {
120 struct sk_buff *skb;
121 struct netlink_callback *cb;
122 int idx, s_idx;
123};
124
125static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
126{
127 int rc;
128 struct dump_phy_data *data = _data;
129
130 pr_debug("%s\n", __func__);
131
132 if (data->idx++ < data->s_idx)
133 return 0;
134
135 rc = ieee802154_nl_fill_phy(data->skb,
Varka Bhadram4710d802014-07-02 09:01:09 +0530136 NETLINK_CB(data->cb->skb).portid,
137 data->cb->nlh->nlmsg_seq,
138 NLM_F_MULTI,
139 phy);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400140
141 if (rc < 0) {
142 data->idx--;
143 return rc;
144 }
145
146 return 0;
147}
148
Johannes Berg1c582d92013-11-14 17:14:41 +0100149int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400150{
151 struct dump_phy_data data = {
152 .cb = cb,
153 .skb = skb,
154 .s_idx = cb->args[0],
155 .idx = 0,
156 };
157
158 pr_debug("%s\n", __func__);
159
160 wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
161
162 cb->args[0] = data.idx;
163
164 return skb->len;
165}
166
Johannes Berg1c582d92013-11-14 17:14:41 +0100167int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300168{
169 struct sk_buff *msg;
170 struct wpan_phy *phy;
171 const char *name;
172 const char *devname;
173 int rc = -ENOBUFS;
174 struct net_device *dev;
alex.bluesman.smirnov@gmail.com90c049b2012-05-15 20:50:27 +0000175 int type = __IEEE802154_DEV_INVALID;
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300176
177 pr_debug("%s\n", __func__);
178
179 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
180 return -EINVAL;
181
182 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
183 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
184 return -EINVAL; /* phy name should be null-terminated */
185
186 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
187 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
188 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
189 != '\0')
190 return -EINVAL; /* phy name should be null-terminated */
191 } else {
192 devname = "wpan%d";
193 }
194
195 if (strlen(devname) >= IFNAMSIZ)
196 return -ENAMETOOLONG;
197
198 phy = wpan_phy_find(name);
199 if (!phy)
200 return -ENODEV;
201
202 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
203 if (!msg)
204 goto out_dev;
205
206 if (!phy->add_iface) {
207 rc = -EINVAL;
208 goto nla_put_failure;
209 }
210
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300211 if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
212 nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
213 IEEE802154_ADDR_LEN) {
214 rc = -EINVAL;
215 goto nla_put_failure;
216 }
217
alex.bluesman.smirnov@gmail.com90c049b2012-05-15 20:50:27 +0000218 if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
219 type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
Christian Engelmayer267d29a2014-01-11 22:19:30 +0100220 if (type >= __IEEE802154_DEV_MAX) {
221 rc = -EINVAL;
222 goto nla_put_failure;
223 }
alex.bluesman.smirnov@gmail.com90c049b2012-05-15 20:50:27 +0000224 }
225
226 dev = phy->add_iface(phy, devname, type);
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300227 if (IS_ERR(dev)) {
228 rc = PTR_ERR(dev);
229 goto nla_put_failure;
230 }
231
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300232 if (info->attrs[IEEE802154_ATTR_HW_ADDR]) {
233 struct sockaddr addr;
234
235 addr.sa_family = ARPHRD_IEEE802154;
236 nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR],
Varka Bhadram4710d802014-07-02 09:01:09 +0530237 IEEE802154_ADDR_LEN);
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300238
Varka Bhadram4710d802014-07-02 09:01:09 +0530239 /* strangely enough, some callbacks (inetdev_event) from
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300240 * dev_set_mac_address require RTNL_LOCK
241 */
242 rtnl_lock();
243 rc = dev_set_mac_address(dev, &addr);
244 rtnl_unlock();
245 if (rc)
246 goto dev_unregister;
247 }
248
David S. Millerbe51da02012-04-01 20:45:25 -0400249 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
250 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name))
251 goto nla_put_failure;
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300252 dev_put(dev);
253
254 wpan_phy_put(phy);
255
256 return ieee802154_nl_reply(msg, info);
257
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300258dev_unregister:
259 rtnl_lock(); /* del_iface must be called with RTNL lock */
260 phy->del_iface(phy, dev);
261 dev_put(dev);
262 rtnl_unlock();
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300263nla_put_failure:
264 nlmsg_free(msg);
265out_dev:
266 wpan_phy_put(phy);
267 return rc;
268}
269
Johannes Berg1c582d92013-11-14 17:14:41 +0100270int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300271{
272 struct sk_buff *msg;
273 struct wpan_phy *phy;
274 const char *name;
275 int rc;
276 struct net_device *dev;
277
278 pr_debug("%s\n", __func__);
279
280 if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
281 return -EINVAL;
282
283 name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
284 if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
285 return -EINVAL; /* name should be null-terminated */
286
287 dev = dev_get_by_name(genl_info_net(info), name);
288 if (!dev)
289 return -ENODEV;
290
291 phy = ieee802154_mlme_ops(dev)->get_phy(dev);
292 BUG_ON(!phy);
293
294 rc = -EINVAL;
295 /* phy name is optional, but should be checked if it's given */
296 if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
297 struct wpan_phy *phy2;
298
299 const char *pname =
300 nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
301 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
302 != '\0')
303 /* name should be null-terminated */
304 goto out_dev;
305
306 phy2 = wpan_phy_find(pname);
307 if (!phy2)
308 goto out_dev;
309
310 if (phy != phy2) {
311 wpan_phy_put(phy2);
312 goto out_dev;
313 }
314 }
315
316 rc = -ENOBUFS;
317
318 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
319 if (!msg)
320 goto out_dev;
321
322 if (!phy->del_iface) {
323 rc = -EINVAL;
324 goto nla_put_failure;
325 }
326
327 rtnl_lock();
328 phy->del_iface(phy, dev);
329
330 /* We don't have device anymore */
331 dev_put(dev);
332 dev = NULL;
333
334 rtnl_unlock();
335
David S. Millerbe51da02012-04-01 20:45:25 -0400336 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
337 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
338 goto nla_put_failure;
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300339 wpan_phy_put(phy);
340
341 return ieee802154_nl_reply(msg, info);
342
343nla_put_failure:
344 nlmsg_free(msg);
345out_dev:
346 wpan_phy_put(phy);
347 if (dev)
348 dev_put(dev);
349
350 return rc;
351}