2 * Netlink inteface for IEEE 802.15.4 stack
4 * Copyright 2007, 2008 Siemens AG
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.
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.
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.
20 * Sergey Lapin <slapin@ossfans.org>
21 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
22 * Maxim Osipov <maxim.osipov@siemens.com>
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>
36 #include "ieee802154.h"
38 static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
39 u32 seq, int flags, struct wpan_phy *phy)
43 uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL);
45 pr_debug("%s\n", __func__);
50 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
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))
63 for (i = 0; i < 32; i++) {
64 if (phy->channels_supported[i])
65 buf[pages++] = phy->channels_supported[i] | (i << 27);
68 nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
69 pages * sizeof(uint32_t), buf))
71 mutex_unlock(&phy->pib_lock);
73 return genlmsg_end(msg, hdr);
76 mutex_unlock(&phy->pib_lock);
77 genlmsg_cancel(msg, hdr);
83 int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
85 /* Request for interface name, index, type, IEEE address,
86 PAN Id, short address */
92 pr_debug("%s\n", __func__);
94 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
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 */
102 phy = wpan_phy_find(name);
106 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
110 rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq,
117 return genlmsg_reply(msg, info);
126 struct dump_phy_data {
128 struct netlink_callback *cb;
132 static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
135 struct dump_phy_data *data = _data;
137 pr_debug("%s\n", __func__);
139 if (data->idx++ < data->s_idx)
142 rc = ieee802154_nl_fill_phy(data->skb,
143 NETLINK_CB(data->cb->skb).portid,
144 data->cb->nlh->nlmsg_seq,
156 int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
158 struct dump_phy_data data = {
161 .s_idx = cb->args[0],
165 pr_debug("%s\n", __func__);
167 wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
169 cb->args[0] = data.idx;
174 int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info)
177 struct wpan_phy *phy;
181 struct net_device *dev;
182 int type = __IEEE802154_DEV_INVALID;
184 pr_debug("%s\n", __func__);
186 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
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 */
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]
197 return -EINVAL; /* phy name should be null-terminated */
202 if (strlen(devname) >= IFNAMSIZ)
203 return -ENAMETOOLONG;
205 phy = wpan_phy_find(name);
209 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
213 if (!phy->add_iface) {
215 goto nla_put_failure;
218 if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
219 nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
220 IEEE802154_ADDR_LEN) {
222 goto nla_put_failure;
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) {
229 goto nla_put_failure;
233 dev = phy->add_iface(phy, devname, type);
236 goto nla_put_failure;
239 if (info->attrs[IEEE802154_ATTR_HW_ADDR]) {
240 struct sockaddr addr;
242 addr.sa_family = ARPHRD_IEEE802154;
243 nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR],
244 IEEE802154_ADDR_LEN);
247 * strangely enough, some callbacks (inetdev_event) from
248 * dev_set_mac_address require RTNL_LOCK
251 rc = dev_set_mac_address(dev, &addr);
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;
264 return ieee802154_nl_reply(msg, info);
267 rtnl_lock(); /* del_iface must be called with RTNL lock */
268 phy->del_iface(phy, dev);
278 int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
281 struct wpan_phy *phy;
284 struct net_device *dev;
286 pr_debug("%s\n", __func__);
288 if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
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 */
295 dev = dev_get_by_name(genl_info_net(info), name);
299 phy = ieee802154_mlme_ops(dev)->get_phy(dev);
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;
308 nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
309 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
311 /* name should be null-terminated */
314 phy2 = wpan_phy_find(pname);
326 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
330 if (!phy->del_iface) {
332 goto nla_put_failure;
336 phy->del_iface(phy, dev);
338 /* We don't have device anymore */
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;
349 return ieee802154_nl_reply(msg, info);
361 static int phy_set_txpower(struct wpan_phy *phy, struct genl_info *info)
363 int txpower = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]);
366 rc = phy->set_txpower(phy, txpower);
370 phy->transmit_power = txpower;
375 static int phy_set_lbt(struct wpan_phy *phy, struct genl_info *info)
377 u8 on = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
380 rc = phy->set_lbt(phy, on);
389 static int phy_set_cca_mode(struct wpan_phy *phy, struct genl_info *info)
391 u8 mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
397 rc = phy->set_cca_mode(phy, mode);
401 phy->cca_mode = mode;
406 int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info)
408 struct wpan_phy *phy;
412 pr_debug("%s\n", __func__);
414 if (!info->attrs[IEEE802154_ATTR_PHY_NAME] &&
415 !info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
416 !info->attrs[IEEE802154_ATTR_CCA_MODE])
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 */
423 phy = wpan_phy_find(name);
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]))
432 mutex_lock(&phy->pib_lock);
434 if (info->attrs[IEEE802154_ATTR_TXPOWER]) {
435 rc = phy_set_txpower(phy, info);
440 if (info->attrs[IEEE802154_ATTR_LBT_ENABLED]) {
441 rc = phy_set_lbt(phy, info);
446 if (info->attrs[IEEE802154_ATTR_CCA_MODE]) {
447 rc = phy_set_cca_mode(phy, info);
452 mutex_unlock(&phy->pib_lock);
459 mutex_unlock(&phy->pib_lock);