mac802154: basic mib support
[linux-block.git] / net / ieee802154 / nl-phy.c
CommitLineData
1eaa9d03
DES
1/*
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>
5a0e3ad6 26#include <linux/slab.h>
060e4179 27#include <linux/if_arp.h>
1eaa9d03
DES
28#include <net/netlink.h>
29#include <net/genetlink.h>
30#include <net/wpan-phy.h>
bb1cafb8
DES
31#include <net/af_ieee802154.h>
32#include <net/ieee802154_netdev.h>
33#include <net/rtnetlink.h> /* for rtnl_{un,}lock */
1eaa9d03
DES
34#include <linux/nl802154.h>
35
36#include "ieee802154.h"
37
38static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 pid,
39 u32 seq, int flags, struct wpan_phy *phy)
40{
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)
b9cabe52 48 return -EMSGSIZE;
1eaa9d03
DES
49
50 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
51 IEEE802154_LIST_PHY);
52 if (!hdr)
53 goto out;
54
55 mutex_lock(&phy->pib_lock);
be51da0f
DM
56 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;
1eaa9d03
DES
60 for (i = 0; i < 32; i++) {
61 if (phy->channels_supported[i])
62 buf[pages++] = phy->channels_supported[i] | (i << 27);
63 }
be51da0f
DM
64 if (pages &&
65 nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
66 pages * sizeof(uint32_t), buf))
67 goto nla_put_failure;
1eaa9d03 68 mutex_unlock(&phy->pib_lock);
b9cabe52 69 kfree(buf);
1eaa9d03
DES
70 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
80static int ieee802154_list_phy(struct sk_buff *skb,
81 struct genl_info *info)
82{
83 /* Request for interface name, index, type, IEEE address,
84 PAN Id, short address */
85 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
104 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
105 if (!msg)
106 goto out_dev;
107
108 rc = ieee802154_nl_fill_phy(msg, info->snd_pid, info->snd_seq,
109 0, phy);
110 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;
121
122}
123
124struct dump_phy_data {
125 struct sk_buff *skb;
126 struct netlink_callback *cb;
127 int idx, s_idx;
128};
129
130static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
131{
132 int rc;
133 struct dump_phy_data *data = _data;
134
135 pr_debug("%s\n", __func__);
136
137 if (data->idx++ < data->s_idx)
138 return 0;
139
140 rc = ieee802154_nl_fill_phy(data->skb,
141 NETLINK_CB(data->cb->skb).pid,
142 data->cb->nlh->nlmsg_seq,
143 NLM_F_MULTI,
144 phy);
145
146 if (rc < 0) {
147 data->idx--;
148 return rc;
149 }
150
151 return 0;
152}
153
154static int ieee802154_dump_phy(struct sk_buff *skb,
155 struct netlink_callback *cb)
156{
157 struct dump_phy_data data = {
158 .cb = cb,
159 .skb = skb,
160 .s_idx = cb->args[0],
161 .idx = 0,
162 };
163
164 pr_debug("%s\n", __func__);
165
166 wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
167
168 cb->args[0] = data.idx;
169
170 return skb->len;
171}
172
bb1cafb8
DES
173static int ieee802154_add_iface(struct sk_buff *skb,
174 struct genl_info *info)
175{
176 struct sk_buff *msg;
177 struct wpan_phy *phy;
178 const char *name;
179 const char *devname;
180 int rc = -ENOBUFS;
181 struct net_device *dev;
182
183 pr_debug("%s\n", __func__);
184
185 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
186 return -EINVAL;
187
188 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
189 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
190 return -EINVAL; /* phy name should be null-terminated */
191
192 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
193 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
194 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
195 != '\0')
196 return -EINVAL; /* phy name should be null-terminated */
197 } else {
198 devname = "wpan%d";
199 }
200
201 if (strlen(devname) >= IFNAMSIZ)
202 return -ENAMETOOLONG;
203
204 phy = wpan_phy_find(name);
205 if (!phy)
206 return -ENODEV;
207
208 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
209 if (!msg)
210 goto out_dev;
211
212 if (!phy->add_iface) {
213 rc = -EINVAL;
214 goto nla_put_failure;
215 }
216
060e4179
DES
217 if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
218 nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
219 IEEE802154_ADDR_LEN) {
220 rc = -EINVAL;
221 goto nla_put_failure;
222 }
223
bb1cafb8
DES
224 dev = phy->add_iface(phy, devname);
225 if (IS_ERR(dev)) {
226 rc = PTR_ERR(dev);
227 goto nla_put_failure;
228 }
229
060e4179
DES
230 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],
235 IEEE802154_ADDR_LEN);
236
237 /*
238 * strangely enough, some callbacks (inetdev_event) from
239 * dev_set_mac_address require RTNL_LOCK
240 */
241 rtnl_lock();
242 rc = dev_set_mac_address(dev, &addr);
243 rtnl_unlock();
244 if (rc)
245 goto dev_unregister;
246 }
247
be51da0f
DM
248 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
249 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name))
250 goto nla_put_failure;
bb1cafb8
DES
251 dev_put(dev);
252
253 wpan_phy_put(phy);
254
255 return ieee802154_nl_reply(msg, info);
256
060e4179
DES
257dev_unregister:
258 rtnl_lock(); /* del_iface must be called with RTNL lock */
259 phy->del_iface(phy, dev);
260 dev_put(dev);
261 rtnl_unlock();
bb1cafb8
DES
262nla_put_failure:
263 nlmsg_free(msg);
264out_dev:
265 wpan_phy_put(phy);
266 return rc;
267}
268
269static int ieee802154_del_iface(struct sk_buff *skb,
270 struct genl_info *info)
271{
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
be51da0f
DM
336 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;
bb1cafb8
DES
339 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}
352
1eaa9d03
DES
353static struct genl_ops ieee802154_phy_ops[] = {
354 IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
355 ieee802154_dump_phy),
bb1cafb8
DES
356 IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
357 IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
1eaa9d03
DES
358};
359
360/*
361 * No need to unregister as family unregistration will do it.
362 */
363int nl802154_phy_register(void)
364{
365 int i;
366 int rc;
367
368 for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) {
369 rc = genl_register_ops(&nl802154_family,
370 &ieee802154_phy_ops[i]);
371 if (rc)
372 return rc;
373 }
374
375 return 0;
376}