Merge branch 'v3.10/topic/gator' of git://git.linaro.org/kernel/linux-linaro-stable...
[firefly-linux-kernel-4.4.55.git] / net / ieee802154 / nl-phy.c
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>
26 #include <linux/slab.h>
27 #include <linux/if_arp.h>
28 #include <net/netlink.h>
29 #include <net/genetlink.h>
30 #include <net/wpan-phy.h>
31 #include <net/af_ieee802154.h>
32 #include <net/ieee802154_netdev.h>
33 #include <net/rtnetlink.h> /* for rtnl_{un,}lock */
34 #include <linux/nl802154.h>
35
36 #include "ieee802154.h"
37
38 static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
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)
48                 return -EMSGSIZE;
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);
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;
60         for (i = 0; i < 32; i++) {
61                 if (phy->channels_supported[i])
62                         buf[pages++] = phy->channels_supported[i] | (i << 27);
63         }
64         if (pages &&
65             nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
66                     pages * sizeof(uint32_t), buf))
67                 goto nla_put_failure;
68         mutex_unlock(&phy->pib_lock);
69         kfree(buf);
70         return genlmsg_end(msg, hdr);
71
72 nla_put_failure:
73         mutex_unlock(&phy->pib_lock);
74         genlmsg_cancel(msg, hdr);
75 out:
76         kfree(buf);
77         return -EMSGSIZE;
78 }
79
80 static 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_DEFAULT_SIZE, GFP_KERNEL);
105         if (!msg)
106                 goto out_dev;
107
108         rc = ieee802154_nl_fill_phy(msg, info->snd_portid, 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);
116 out_free:
117         nlmsg_free(msg);
118 out_dev:
119         wpan_phy_put(phy);
120         return rc;
121
122 }
123
124 struct dump_phy_data {
125         struct sk_buff *skb;
126         struct netlink_callback *cb;
127         int idx, s_idx;
128 };
129
130 static 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).portid,
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
154 static 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
173 static 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         int type = __IEEE802154_DEV_INVALID;
183
184         pr_debug("%s\n", __func__);
185
186         if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
187                 return -EINVAL;
188
189         name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
190         if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
191                 return -EINVAL; /* phy name should be null-terminated */
192
193         if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
194                 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
195                 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
196                                 != '\0')
197                         return -EINVAL; /* phy name should be null-terminated */
198         } else  {
199                 devname = "wpan%d";
200         }
201
202         if (strlen(devname) >= IFNAMSIZ)
203                 return -ENAMETOOLONG;
204
205         phy = wpan_phy_find(name);
206         if (!phy)
207                 return -ENODEV;
208
209         msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
210         if (!msg)
211                 goto out_dev;
212
213         if (!phy->add_iface) {
214                 rc = -EINVAL;
215                 goto nla_put_failure;
216         }
217
218         if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
219             nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
220                         IEEE802154_ADDR_LEN) {
221                 rc = -EINVAL;
222                 goto nla_put_failure;
223         }
224
225         if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
226                 type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
227                 if (type >= __IEEE802154_DEV_MAX) {
228                         rc = -EINVAL;
229                         goto nla_put_failure;
230                 }
231         }
232
233         dev = phy->add_iface(phy, devname, type);
234         if (IS_ERR(dev)) {
235                 rc = PTR_ERR(dev);
236                 goto nla_put_failure;
237         }
238
239         if (info->attrs[IEEE802154_ATTR_HW_ADDR]) {
240                 struct sockaddr addr;
241
242                 addr.sa_family = ARPHRD_IEEE802154;
243                 nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR],
244                                 IEEE802154_ADDR_LEN);
245
246                 /*
247                  * strangely enough, some callbacks (inetdev_event) from
248                  * dev_set_mac_address require RTNL_LOCK
249                  */
250                 rtnl_lock();
251                 rc = dev_set_mac_address(dev, &addr);
252                 rtnl_unlock();
253                 if (rc)
254                         goto dev_unregister;
255         }
256
257         if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
258             nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name))
259                 goto nla_put_failure;
260         dev_put(dev);
261
262         wpan_phy_put(phy);
263
264         return ieee802154_nl_reply(msg, info);
265
266 dev_unregister:
267         rtnl_lock(); /* del_iface must be called with RTNL lock */
268         phy->del_iface(phy, dev);
269         dev_put(dev);
270         rtnl_unlock();
271 nla_put_failure:
272         nlmsg_free(msg);
273 out_dev:
274         wpan_phy_put(phy);
275         return rc;
276 }
277
278 static int ieee802154_del_iface(struct sk_buff *skb,
279                 struct genl_info *info)
280 {
281         struct sk_buff *msg;
282         struct wpan_phy *phy;
283         const char *name;
284         int rc;
285         struct net_device *dev;
286
287         pr_debug("%s\n", __func__);
288
289         if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
290                 return -EINVAL;
291
292         name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
293         if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
294                 return -EINVAL; /* name should be null-terminated */
295
296         dev = dev_get_by_name(genl_info_net(info), name);
297         if (!dev)
298                 return -ENODEV;
299
300         phy = ieee802154_mlme_ops(dev)->get_phy(dev);
301         BUG_ON(!phy);
302
303         rc = -EINVAL;
304         /* phy name is optional, but should be checked if it's given */
305         if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
306                 struct wpan_phy *phy2;
307
308                 const char *pname =
309                         nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
310                 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
311                                 != '\0')
312                         /* name should be null-terminated */
313                         goto out_dev;
314
315                 phy2 = wpan_phy_find(pname);
316                 if (!phy2)
317                         goto out_dev;
318
319                 if (phy != phy2) {
320                         wpan_phy_put(phy2);
321                         goto out_dev;
322                 }
323         }
324
325         rc = -ENOBUFS;
326
327         msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
328         if (!msg)
329                 goto out_dev;
330
331         if (!phy->del_iface) {
332                 rc = -EINVAL;
333                 goto nla_put_failure;
334         }
335
336         rtnl_lock();
337         phy->del_iface(phy, dev);
338
339         /* We don't have device anymore */
340         dev_put(dev);
341         dev = NULL;
342
343         rtnl_unlock();
344
345         if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
346             nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
347                 goto nla_put_failure;
348         wpan_phy_put(phy);
349
350         return ieee802154_nl_reply(msg, info);
351
352 nla_put_failure:
353         nlmsg_free(msg);
354 out_dev:
355         wpan_phy_put(phy);
356         if (dev)
357                 dev_put(dev);
358
359         return rc;
360 }
361
362 static struct genl_ops ieee802154_phy_ops[] = {
363         IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
364                                                         ieee802154_dump_phy),
365         IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
366         IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
367 };
368
369 /*
370  * No need to unregister as family unregistration will do it.
371  */
372 int nl802154_phy_register(void)
373 {
374         int i;
375         int rc;
376
377         for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) {
378                 rc = genl_register_ops(&nl802154_family,
379                                 &ieee802154_phy_ops[i]);
380                 if (rc)
381                         return rc;
382         }
383
384         return 0;
385 }