net: cls_bpf: fix size mismatch on filter preparation
[deliverable/linux.git] / net / ieee802154 / nl-phy.c
1 /*
2 * Netlink interface 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 * 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>
22 #include <linux/slab.h>
23 #include <linux/if_arp.h>
24 #include <net/netlink.h>
25 #include <net/genetlink.h>
26 #include <net/cfg802154.h>
27 #include <net/af_ieee802154.h>
28 #include <net/ieee802154_netdev.h>
29 #include <net/rtnetlink.h> /* for rtnl_{un,}lock */
30 #include <linux/nl802154.h>
31
32 #include "ieee802154.h"
33 #include "rdev-ops.h"
34 #include "core.h"
35
36 static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
37 u32 seq, int flags, struct wpan_phy *phy)
38 {
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)
46 return -EMSGSIZE;
47
48 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
49 IEEE802154_LIST_PHY);
50 if (!hdr)
51 goto out;
52
53 mutex_lock(&phy->pib_lock);
54 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;
58 for (i = 0; i < 32; i++) {
59 if (phy->channels_supported[i])
60 buf[pages++] = phy->channels_supported[i] | (i << 27);
61 }
62 if (pages &&
63 nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
64 pages * sizeof(uint32_t), buf))
65 goto nla_put_failure;
66 mutex_unlock(&phy->pib_lock);
67 kfree(buf);
68 return genlmsg_end(msg, hdr);
69
70 nla_put_failure:
71 mutex_unlock(&phy->pib_lock);
72 genlmsg_cancel(msg, hdr);
73 out:
74 kfree(buf);
75 return -EMSGSIZE;
76 }
77
78 int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
79 {
80 /* Request for interface name, index, type, IEEE address,
81 * PAN Id, short address
82 */
83 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 phy = wpan_phy_find(name);
98 if (!phy)
99 return -ENODEV;
100
101 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
102 if (!msg)
103 goto out_dev;
104
105 rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq,
106 0, phy);
107 if (rc < 0)
108 goto out_free;
109
110 wpan_phy_put(phy);
111
112 return genlmsg_reply(msg, info);
113 out_free:
114 nlmsg_free(msg);
115 out_dev:
116 wpan_phy_put(phy);
117 return rc;
118 }
119
120 struct dump_phy_data {
121 struct sk_buff *skb;
122 struct netlink_callback *cb;
123 int idx, s_idx;
124 };
125
126 static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
127 {
128 int rc;
129 struct dump_phy_data *data = _data;
130
131 pr_debug("%s\n", __func__);
132
133 if (data->idx++ < data->s_idx)
134 return 0;
135
136 rc = ieee802154_nl_fill_phy(data->skb,
137 NETLINK_CB(data->cb->skb).portid,
138 data->cb->nlh->nlmsg_seq,
139 NLM_F_MULTI,
140 phy);
141
142 if (rc < 0) {
143 data->idx--;
144 return rc;
145 }
146
147 return 0;
148 }
149
150 int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
151 {
152 struct dump_phy_data data = {
153 .cb = cb,
154 .skb = skb,
155 .s_idx = cb->args[0],
156 .idx = 0,
157 };
158
159 pr_debug("%s\n", __func__);
160
161 wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
162
163 cb->args[0] = data.idx;
164
165 return skb->len;
166 }
167
168 int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info)
169 {
170 struct sk_buff *msg;
171 struct wpan_phy *phy;
172 const char *name;
173 const char *devname;
174 int rc = -ENOBUFS;
175 struct net_device *dev;
176 int type = __IEEE802154_DEV_INVALID;
177
178 pr_debug("%s\n", __func__);
179
180 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
181 return -EINVAL;
182
183 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
184 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
185 return -EINVAL; /* phy name should be null-terminated */
186
187 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
188 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
189 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
190 != '\0')
191 return -EINVAL; /* phy name should be null-terminated */
192 } else {
193 devname = "wpan%d";
194 }
195
196 if (strlen(devname) >= IFNAMSIZ)
197 return -ENAMETOOLONG;
198
199 phy = wpan_phy_find(name);
200 if (!phy)
201 return -ENODEV;
202
203 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
204 if (!msg)
205 goto out_dev;
206
207 if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
208 nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
209 IEEE802154_ADDR_LEN) {
210 rc = -EINVAL;
211 goto nla_put_failure;
212 }
213
214 if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
215 type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
216 if (type >= __IEEE802154_DEV_MAX) {
217 rc = -EINVAL;
218 goto nla_put_failure;
219 }
220 }
221
222 dev = rdev_add_virtual_intf_deprecated(wpan_phy_to_rdev(phy), devname,
223 type);
224 if (IS_ERR(dev)) {
225 rc = PTR_ERR(dev);
226 goto nla_put_failure;
227 }
228 dev_hold(dev);
229
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 /* strangely enough, some callbacks (inetdev_event) from
238 * 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
247 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;
250 dev_put(dev);
251
252 wpan_phy_put(phy);
253
254 return ieee802154_nl_reply(msg, info);
255
256 dev_unregister:
257 rtnl_lock(); /* del_iface must be called with RTNL lock */
258 rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
259 dev_put(dev);
260 rtnl_unlock();
261 nla_put_failure:
262 nlmsg_free(msg);
263 out_dev:
264 wpan_phy_put(phy);
265 return rc;
266 }
267
268 int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
269 {
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 = dev->ieee802154_ptr->wpan_phy;
290 BUG_ON(!phy);
291 get_device(&phy->dev);
292
293 rc = -EINVAL;
294 /* phy name is optional, but should be checked if it's given */
295 if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
296 struct wpan_phy *phy2;
297
298 const char *pname =
299 nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
300 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
301 != '\0')
302 /* name should be null-terminated */
303 goto out_dev;
304
305 phy2 = wpan_phy_find(pname);
306 if (!phy2)
307 goto out_dev;
308
309 if (phy != phy2) {
310 wpan_phy_put(phy2);
311 goto out_dev;
312 }
313 }
314
315 rc = -ENOBUFS;
316
317 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
318 if (!msg)
319 goto out_dev;
320
321 rtnl_lock();
322 rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
323
324 /* We don't have device anymore */
325 dev_put(dev);
326 dev = NULL;
327
328 rtnl_unlock();
329
330 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
331 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
332 goto nla_put_failure;
333 wpan_phy_put(phy);
334
335 return ieee802154_nl_reply(msg, info);
336
337 nla_put_failure:
338 nlmsg_free(msg);
339 out_dev:
340 wpan_phy_put(phy);
341 if (dev)
342 dev_put(dev);
343
344 return rc;
345 }
This page took 0.058057 seconds and 5 git commands to generate.