]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/ieee802154/nl-phy.c
ieee802154: add support for CCA mode in wpan phys
[karo-tx-linux.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             nla_put_s8(msg, IEEE802154_ATTR_TXPOWER, phy->transmit_power) ||
60             nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, phy->lbt) ||
61             nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE, phy->cca_mode))
62                 goto nla_put_failure;
63         for (i = 0; i < 32; i++) {
64                 if (phy->channels_supported[i])
65                         buf[pages++] = phy->channels_supported[i] | (i << 27);
66         }
67         if (pages &&
68             nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
69                     pages * sizeof(uint32_t), buf))
70                 goto nla_put_failure;
71         mutex_unlock(&phy->pib_lock);
72         kfree(buf);
73         return genlmsg_end(msg, hdr);
74
75 nla_put_failure:
76         mutex_unlock(&phy->pib_lock);
77         genlmsg_cancel(msg, hdr);
78 out:
79         kfree(buf);
80         return -EMSGSIZE;
81 }
82
83 int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
84 {
85         /* Request for interface name, index, type, IEEE address,
86            PAN Id, short address */
87         struct sk_buff *msg;
88         struct wpan_phy *phy;
89         const char *name;
90         int rc = -ENOBUFS;
91
92         pr_debug("%s\n", __func__);
93
94         if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
95                 return -EINVAL;
96
97         name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
98         if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
99                 return -EINVAL; /* phy name should be null-terminated */
100
101
102         phy = wpan_phy_find(name);
103         if (!phy)
104                 return -ENODEV;
105
106         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
107         if (!msg)
108                 goto out_dev;
109
110         rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq,
111                         0, phy);
112         if (rc < 0)
113                 goto out_free;
114
115         wpan_phy_put(phy);
116
117         return genlmsg_reply(msg, info);
118 out_free:
119         nlmsg_free(msg);
120 out_dev:
121         wpan_phy_put(phy);
122         return rc;
123
124 }
125
126 struct dump_phy_data {
127         struct sk_buff *skb;
128         struct netlink_callback *cb;
129         int idx, s_idx;
130 };
131
132 static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
133 {
134         int rc;
135         struct dump_phy_data *data = _data;
136
137         pr_debug("%s\n", __func__);
138
139         if (data->idx++ < data->s_idx)
140                 return 0;
141
142         rc = ieee802154_nl_fill_phy(data->skb,
143                         NETLINK_CB(data->cb->skb).portid,
144                         data->cb->nlh->nlmsg_seq,
145                         NLM_F_MULTI,
146                         phy);
147
148         if (rc < 0) {
149                 data->idx--;
150                 return rc;
151         }
152
153         return 0;
154 }
155
156 int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
157 {
158         struct dump_phy_data data = {
159                 .cb = cb,
160                 .skb = skb,
161                 .s_idx = cb->args[0],
162                 .idx = 0,
163         };
164
165         pr_debug("%s\n", __func__);
166
167         wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
168
169         cb->args[0] = data.idx;
170
171         return skb->len;
172 }
173
174 int ieee802154_add_iface(struct sk_buff *skb, 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 int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
279 {
280         struct sk_buff *msg;
281         struct wpan_phy *phy;
282         const char *name;
283         int rc;
284         struct net_device *dev;
285
286         pr_debug("%s\n", __func__);
287
288         if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
289                 return -EINVAL;
290
291         name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
292         if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
293                 return -EINVAL; /* name should be null-terminated */
294
295         dev = dev_get_by_name(genl_info_net(info), name);
296         if (!dev)
297                 return -ENODEV;
298
299         phy = ieee802154_mlme_ops(dev)->get_phy(dev);
300         BUG_ON(!phy);
301
302         rc = -EINVAL;
303         /* phy name is optional, but should be checked if it's given */
304         if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
305                 struct wpan_phy *phy2;
306
307                 const char *pname =
308                         nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
309                 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
310                                 != '\0')
311                         /* name should be null-terminated */
312                         goto out_dev;
313
314                 phy2 = wpan_phy_find(pname);
315                 if (!phy2)
316                         goto out_dev;
317
318                 if (phy != phy2) {
319                         wpan_phy_put(phy2);
320                         goto out_dev;
321                 }
322         }
323
324         rc = -ENOBUFS;
325
326         msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
327         if (!msg)
328                 goto out_dev;
329
330         if (!phy->del_iface) {
331                 rc = -EINVAL;
332                 goto nla_put_failure;
333         }
334
335         rtnl_lock();
336         phy->del_iface(phy, dev);
337
338         /* We don't have device anymore */
339         dev_put(dev);
340         dev = NULL;
341
342         rtnl_unlock();
343
344         if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
345             nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
346                 goto nla_put_failure;
347         wpan_phy_put(phy);
348
349         return ieee802154_nl_reply(msg, info);
350
351 nla_put_failure:
352         nlmsg_free(msg);
353 out_dev:
354         wpan_phy_put(phy);
355         if (dev)
356                 dev_put(dev);
357
358         return rc;
359 }
360
361 static int phy_set_txpower(struct wpan_phy *phy, struct genl_info *info)
362 {
363         int txpower = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]);
364         int rc;
365
366         rc = phy->set_txpower(phy, txpower);
367         if (rc < 0)
368                 return rc;
369
370         phy->transmit_power = txpower;
371
372         return 0;
373 }
374
375 static int phy_set_lbt(struct wpan_phy *phy, struct genl_info *info)
376 {
377         u8 on = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
378         int rc;
379
380         rc = phy->set_lbt(phy, on);
381         if (rc < 0)
382                 return rc;
383
384         phy->lbt = on;
385
386         return 0;
387 }
388
389 static int phy_set_cca_mode(struct wpan_phy *phy, struct genl_info *info)
390 {
391         u8 mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
392         int rc;
393
394         if (mode > 3)
395                 return -EINVAL;
396
397         rc = phy->set_cca_mode(phy, mode);
398         if (rc < 0)
399                 return rc;
400
401         phy->cca_mode = mode;
402
403         return 0;
404 }
405
406 int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info)
407 {
408         struct wpan_phy *phy;
409         const char *name;
410         int rc = -ENOTSUPP;
411
412         pr_debug("%s\n", __func__);
413
414         if (!info->attrs[IEEE802154_ATTR_PHY_NAME] &&
415             !info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
416             !info->attrs[IEEE802154_ATTR_CCA_MODE])
417                 return -EINVAL;
418
419         name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
420         if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
421                 return -EINVAL; /* phy name should be null-terminated */
422
423         phy = wpan_phy_find(name);
424         if (!phy)
425                 return -ENODEV;
426
427         if ((!phy->set_txpower && info->attrs[IEEE802154_ATTR_TXPOWER]) ||
428             (!phy->set_lbt && info->attrs[IEEE802154_ATTR_LBT_ENABLED]) ||
429             (!phy->set_cca_mode && info->attrs[IEEE802154_ATTR_CCA_MODE]))
430                 goto out;
431
432         mutex_lock(&phy->pib_lock);
433
434         if (info->attrs[IEEE802154_ATTR_TXPOWER]) {
435                 rc = phy_set_txpower(phy, info);
436                 if (rc < 0)
437                         goto error;
438         }
439
440         if (info->attrs[IEEE802154_ATTR_LBT_ENABLED]) {
441                 rc = phy_set_lbt(phy, info);
442                 if (rc < 0)
443                         goto error;
444         }
445
446         if (info->attrs[IEEE802154_ATTR_CCA_MODE]) {
447                 rc = phy_set_cca_mode(phy, info);
448                 if (rc < 0)
449                         goto error;
450         }
451
452         mutex_unlock(&phy->pib_lock);
453
454         wpan_phy_put(phy);
455
456         return 0;
457
458 error:
459         mutex_unlock(&phy->pib_lock);
460 out:
461         wpan_phy_put(phy);
462         return rc;
463 }