blob: 972baf83411af7c64a6f11826ff7b1ab61b43ec4 [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 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Written by:
20 * Sergey Lapin <slapin@ossfans.org>
21 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
22 * Maxim Osipov <maxim.osipov@siemens.com>
23 */
24
25#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +030027#include <linux/if_arp.h>
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040028#include <net/netlink.h>
29#include <net/genetlink.h>
30#include <net/wpan-phy.h>
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +030031#include <net/af_ieee802154.h>
32#include <net/ieee802154_netdev.h>
33#include <net/rtnetlink.h> /* for rtnl_{un,}lock */
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040034#include <linux/nl802154.h>
35
36#include "ieee802154.h"
37
Eric W. Biederman15e47302012-09-07 20:12:54 +000038static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
Varka Bhadram4710d802014-07-02 09:01:09 +053039 u32 seq, int flags, struct wpan_phy *phy)
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040040{
41 void *hdr;
42 int i, pages = 0;
43 uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL);
44
45 pr_debug("%s\n", __func__);
46
47 if (!buf)
Jesper Juhlb9cabe52011-06-12 04:28:16 +000048 return -EMSGSIZE;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040049
50 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
Varka Bhadram4710d802014-07-02 09:01:09 +053051 IEEE802154_LIST_PHY);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040052 if (!hdr)
53 goto out;
54
55 mutex_lock(&phy->pib_lock);
David S. Millerbe51da02012-04-01 20:45:25 -040056 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
57 nla_put_u8(msg, IEEE802154_ATTR_PAGE, phy->current_page) ||
58 nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel))
59 goto nla_put_failure;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040060 for (i = 0; i < 32; i++) {
61 if (phy->channels_supported[i])
62 buf[pages++] = phy->channels_supported[i] | (i << 27);
63 }
David S. Millerbe51da02012-04-01 20:45:25 -040064 if (pages &&
65 nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
66 pages * sizeof(uint32_t), buf))
67 goto nla_put_failure;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040068 mutex_unlock(&phy->pib_lock);
Jesper Juhlb9cabe52011-06-12 04:28:16 +000069 kfree(buf);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040070 return genlmsg_end(msg, hdr);
71
72nla_put_failure:
73 mutex_unlock(&phy->pib_lock);
74 genlmsg_cancel(msg, hdr);
75out:
76 kfree(buf);
77 return -EMSGSIZE;
78}
79
Johannes Berg1c582d92013-11-14 17:14:41 +010080int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040081{
82 /* Request for interface name, index, type, IEEE address,
Varka Bhadram4710d802014-07-02 09:01:09 +053083 * PAN Id, short address
84 */
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040085 struct sk_buff *msg;
86 struct wpan_phy *phy;
87 const char *name;
88 int rc = -ENOBUFS;
89
90 pr_debug("%s\n", __func__);
91
92 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
93 return -EINVAL;
94
95 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
96 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
97 return -EINVAL; /* phy name should be null-terminated */
98
99
100 phy = wpan_phy_find(name);
101 if (!phy)
102 return -ENODEV;
103
Thomas Graf58050fc2012-06-28 03:57:45 +0000104 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400105 if (!msg)
106 goto out_dev;
107
Eric W. Biederman15e47302012-09-07 20:12:54 +0000108 rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq,
Varka Bhadram4710d802014-07-02 09:01:09 +0530109 0, phy);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400110 if (rc < 0)
111 goto out_free;
112
113 wpan_phy_put(phy);
114
115 return genlmsg_reply(msg, info);
116out_free:
117 nlmsg_free(msg);
118out_dev:
119 wpan_phy_put(phy);
120 return rc;
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400121}
122
123struct dump_phy_data {
124 struct sk_buff *skb;
125 struct netlink_callback *cb;
126 int idx, s_idx;
127};
128
129static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
130{
131 int rc;
132 struct dump_phy_data *data = _data;
133
134 pr_debug("%s\n", __func__);
135
136 if (data->idx++ < data->s_idx)
137 return 0;
138
139 rc = ieee802154_nl_fill_phy(data->skb,
Varka Bhadram4710d802014-07-02 09:01:09 +0530140 NETLINK_CB(data->cb->skb).portid,
141 data->cb->nlh->nlmsg_seq,
142 NLM_F_MULTI,
143 phy);
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400144
145 if (rc < 0) {
146 data->idx--;
147 return rc;
148 }
149
150 return 0;
151}
152
Johannes Berg1c582d92013-11-14 17:14:41 +0100153int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400154{
155 struct dump_phy_data data = {
156 .cb = cb,
157 .skb = skb,
158 .s_idx = cb->args[0],
159 .idx = 0,
160 };
161
162 pr_debug("%s\n", __func__);
163
164 wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
165
166 cb->args[0] = data.idx;
167
168 return skb->len;
169}
170
Johannes Berg1c582d92013-11-14 17:14:41 +0100171int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300172{
173 struct sk_buff *msg;
174 struct wpan_phy *phy;
175 const char *name;
176 const char *devname;
177 int rc = -ENOBUFS;
178 struct net_device *dev;
alex.bluesman.smirnov@gmail.com90c049b2012-05-15 20:50:27 +0000179 int type = __IEEE802154_DEV_INVALID;
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300180
181 pr_debug("%s\n", __func__);
182
183 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
184 return -EINVAL;
185
186 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
187 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
188 return -EINVAL; /* phy name should be null-terminated */
189
190 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
191 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
192 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
193 != '\0')
194 return -EINVAL; /* phy name should be null-terminated */
195 } else {
196 devname = "wpan%d";
197 }
198
199 if (strlen(devname) >= IFNAMSIZ)
200 return -ENAMETOOLONG;
201
202 phy = wpan_phy_find(name);
203 if (!phy)
204 return -ENODEV;
205
206 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
207 if (!msg)
208 goto out_dev;
209
210 if (!phy->add_iface) {
211 rc = -EINVAL;
212 goto nla_put_failure;
213 }
214
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300215 if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
216 nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
217 IEEE802154_ADDR_LEN) {
218 rc = -EINVAL;
219 goto nla_put_failure;
220 }
221
alex.bluesman.smirnov@gmail.com90c049b2012-05-15 20:50:27 +0000222 if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
223 type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
Christian Engelmayer267d29a2014-01-11 22:19:30 +0100224 if (type >= __IEEE802154_DEV_MAX) {
225 rc = -EINVAL;
226 goto nla_put_failure;
227 }
alex.bluesman.smirnov@gmail.com90c049b2012-05-15 20:50:27 +0000228 }
229
230 dev = phy->add_iface(phy, devname, type);
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300231 if (IS_ERR(dev)) {
232 rc = PTR_ERR(dev);
233 goto nla_put_failure;
234 }
235
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300236 if (info->attrs[IEEE802154_ATTR_HW_ADDR]) {
237 struct sockaddr addr;
238
239 addr.sa_family = ARPHRD_IEEE802154;
240 nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR],
Varka Bhadram4710d802014-07-02 09:01:09 +0530241 IEEE802154_ADDR_LEN);
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300242
Varka Bhadram4710d802014-07-02 09:01:09 +0530243 /* strangely enough, some callbacks (inetdev_event) from
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300244 * dev_set_mac_address require RTNL_LOCK
245 */
246 rtnl_lock();
247 rc = dev_set_mac_address(dev, &addr);
248 rtnl_unlock();
249 if (rc)
250 goto dev_unregister;
251 }
252
David S. Millerbe51da02012-04-01 20:45:25 -0400253 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
254 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name))
255 goto nla_put_failure;
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300256 dev_put(dev);
257
258 wpan_phy_put(phy);
259
260 return ieee802154_nl_reply(msg, info);
261
Dmitry Eremin-Solenikov060e4172010-03-18 19:26:23 +0300262dev_unregister:
263 rtnl_lock(); /* del_iface must be called with RTNL lock */
264 phy->del_iface(phy, dev);
265 dev_put(dev);
266 rtnl_unlock();
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300267nla_put_failure:
268 nlmsg_free(msg);
269out_dev:
270 wpan_phy_put(phy);
271 return rc;
272}
273
Johannes Berg1c582d92013-11-14 17:14:41 +0100274int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300275{
276 struct sk_buff *msg;
277 struct wpan_phy *phy;
278 const char *name;
279 int rc;
280 struct net_device *dev;
281
282 pr_debug("%s\n", __func__);
283
284 if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
285 return -EINVAL;
286
287 name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
288 if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
289 return -EINVAL; /* name should be null-terminated */
290
291 dev = dev_get_by_name(genl_info_net(info), name);
292 if (!dev)
293 return -ENODEV;
294
295 phy = ieee802154_mlme_ops(dev)->get_phy(dev);
296 BUG_ON(!phy);
297
298 rc = -EINVAL;
299 /* phy name is optional, but should be checked if it's given */
300 if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
301 struct wpan_phy *phy2;
302
303 const char *pname =
304 nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
305 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
306 != '\0')
307 /* name should be null-terminated */
308 goto out_dev;
309
310 phy2 = wpan_phy_find(pname);
311 if (!phy2)
312 goto out_dev;
313
314 if (phy != phy2) {
315 wpan_phy_put(phy2);
316 goto out_dev;
317 }
318 }
319
320 rc = -ENOBUFS;
321
322 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
323 if (!msg)
324 goto out_dev;
325
326 if (!phy->del_iface) {
327 rc = -EINVAL;
328 goto nla_put_failure;
329 }
330
331 rtnl_lock();
332 phy->del_iface(phy, dev);
333
334 /* We don't have device anymore */
335 dev_put(dev);
336 dev = NULL;
337
338 rtnl_unlock();
339
David S. Millerbe51da02012-04-01 20:45:25 -0400340 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
341 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
342 goto nla_put_failure;
Dmitry Eremin-Solenikovbb1cafb2009-11-05 16:56:23 +0300343 wpan_phy_put(phy);
344
345 return ieee802154_nl_reply(msg, info);
346
347nla_put_failure:
348 nlmsg_free(msg);
349out_dev:
350 wpan_phy_put(phy);
351 if (dev)
352 dev_put(dev);
353
354 return rc;
355}