Commit | Line | Data |
---|---|---|
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 | ||
15e47304 | 38 | static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid, |
1eaa9d03 DES |
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 | ||
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 | ||
1c582d91 | 80 | int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info) |
1eaa9d03 DES |
81 | { |
82 | /* Request for interface name, index, type, IEEE address, | |
83 | PAN Id, short address */ | |
84 | struct sk_buff *msg; | |
85 | struct wpan_phy *phy; | |
86 | const char *name; | |
87 | int rc = -ENOBUFS; | |
88 | ||
89 | pr_debug("%s\n", __func__); | |
90 | ||
91 | if (!info->attrs[IEEE802154_ATTR_PHY_NAME]) | |
92 | return -EINVAL; | |
93 | ||
94 | name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); | |
95 | if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0') | |
96 | return -EINVAL; /* phy name should be null-terminated */ | |
97 | ||
98 | ||
99 | phy = wpan_phy_find(name); | |
100 | if (!phy) | |
101 | return -ENODEV; | |
102 | ||
58050fce | 103 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1eaa9d03 DES |
104 | if (!msg) |
105 | goto out_dev; | |
106 | ||
15e47304 | 107 | rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq, |
1eaa9d03 DES |
108 | 0, phy); |
109 | if (rc < 0) | |
110 | goto out_free; | |
111 | ||
112 | wpan_phy_put(phy); | |
113 | ||
114 | return genlmsg_reply(msg, info); | |
115 | out_free: | |
116 | nlmsg_free(msg); | |
117 | out_dev: | |
118 | wpan_phy_put(phy); | |
119 | return rc; | |
120 | ||
121 | } | |
122 | ||
123 | struct dump_phy_data { | |
124 | struct sk_buff *skb; | |
125 | struct netlink_callback *cb; | |
126 | int idx, s_idx; | |
127 | }; | |
128 | ||
129 | static 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, | |
15e47304 | 140 | NETLINK_CB(data->cb->skb).portid, |
1eaa9d03 DES |
141 | data->cb->nlh->nlmsg_seq, |
142 | NLM_F_MULTI, | |
143 | phy); | |
144 | ||
145 | if (rc < 0) { | |
146 | data->idx--; | |
147 | return rc; | |
148 | } | |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
1c582d91 | 153 | int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb) |
1eaa9d03 DES |
154 | { |
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 | ||
1c582d91 | 171 | int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info) |
bb1cafb8 DES |
172 | { |
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; | |
90c049b2 | 179 | int type = __IEEE802154_DEV_INVALID; |
bb1cafb8 DES |
180 | |
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 | ||
060e4179 DES |
215 | 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 | ||
90c049b2 | 222 | if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) { |
223 | type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]); | |
267d29a6 CE |
224 | if (type >= __IEEE802154_DEV_MAX) { |
225 | rc = -EINVAL; | |
226 | goto nla_put_failure; | |
227 | } | |
90c049b2 | 228 | } |
229 | ||
230 | dev = phy->add_iface(phy, devname, type); | |
bb1cafb8 DES |
231 | if (IS_ERR(dev)) { |
232 | rc = PTR_ERR(dev); | |
233 | goto nla_put_failure; | |
234 | } | |
235 | ||
060e4179 DES |
236 | 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], | |
241 | IEEE802154_ADDR_LEN); | |
242 | ||
243 | /* | |
244 | * strangely enough, some callbacks (inetdev_event) from | |
245 | * dev_set_mac_address require RTNL_LOCK | |
246 | */ | |
247 | rtnl_lock(); | |
248 | rc = dev_set_mac_address(dev, &addr); | |
249 | rtnl_unlock(); | |
250 | if (rc) | |
251 | goto dev_unregister; | |
252 | } | |
253 | ||
be51da0f DM |
254 | if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || |
255 | nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name)) | |
256 | goto nla_put_failure; | |
bb1cafb8 DES |
257 | dev_put(dev); |
258 | ||
259 | wpan_phy_put(phy); | |
260 | ||
261 | return ieee802154_nl_reply(msg, info); | |
262 | ||
060e4179 DES |
263 | dev_unregister: |
264 | rtnl_lock(); /* del_iface must be called with RTNL lock */ | |
265 | phy->del_iface(phy, dev); | |
266 | dev_put(dev); | |
267 | rtnl_unlock(); | |
bb1cafb8 DES |
268 | nla_put_failure: |
269 | nlmsg_free(msg); | |
270 | out_dev: | |
271 | wpan_phy_put(phy); | |
272 | return rc; | |
273 | } | |
274 | ||
1c582d91 | 275 | int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info) |
bb1cafb8 DES |
276 | { |
277 | struct sk_buff *msg; | |
278 | struct wpan_phy *phy; | |
279 | const char *name; | |
280 | int rc; | |
281 | struct net_device *dev; | |
282 | ||
283 | pr_debug("%s\n", __func__); | |
284 | ||
285 | if (!info->attrs[IEEE802154_ATTR_DEV_NAME]) | |
286 | return -EINVAL; | |
287 | ||
288 | name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]); | |
289 | if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0') | |
290 | return -EINVAL; /* name should be null-terminated */ | |
291 | ||
292 | dev = dev_get_by_name(genl_info_net(info), name); | |
293 | if (!dev) | |
294 | return -ENODEV; | |
295 | ||
296 | phy = ieee802154_mlme_ops(dev)->get_phy(dev); | |
297 | BUG_ON(!phy); | |
298 | ||
299 | rc = -EINVAL; | |
300 | /* phy name is optional, but should be checked if it's given */ | |
301 | if (info->attrs[IEEE802154_ATTR_PHY_NAME]) { | |
302 | struct wpan_phy *phy2; | |
303 | ||
304 | const char *pname = | |
305 | nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); | |
306 | if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] | |
307 | != '\0') | |
308 | /* name should be null-terminated */ | |
309 | goto out_dev; | |
310 | ||
311 | phy2 = wpan_phy_find(pname); | |
312 | if (!phy2) | |
313 | goto out_dev; | |
314 | ||
315 | if (phy != phy2) { | |
316 | wpan_phy_put(phy2); | |
317 | goto out_dev; | |
318 | } | |
319 | } | |
320 | ||
321 | rc = -ENOBUFS; | |
322 | ||
323 | msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE); | |
324 | if (!msg) | |
325 | goto out_dev; | |
326 | ||
327 | if (!phy->del_iface) { | |
328 | rc = -EINVAL; | |
329 | goto nla_put_failure; | |
330 | } | |
331 | ||
332 | rtnl_lock(); | |
333 | phy->del_iface(phy, dev); | |
334 | ||
335 | /* We don't have device anymore */ | |
336 | dev_put(dev); | |
337 | dev = NULL; | |
338 | ||
339 | rtnl_unlock(); | |
340 | ||
be51da0f DM |
341 | if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || |
342 | nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name)) | |
343 | goto nla_put_failure; | |
bb1cafb8 DES |
344 | wpan_phy_put(phy); |
345 | ||
346 | return ieee802154_nl_reply(msg, info); | |
347 | ||
348 | nla_put_failure: | |
349 | nlmsg_free(msg); | |
350 | out_dev: | |
351 | wpan_phy_put(phy); | |
352 | if (dev) | |
353 | dev_put(dev); | |
354 | ||
355 | return rc; | |
356 | } |