2 * Netlink interface 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.
16 * Sergey Lapin <slapin@ossfans.org>
17 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
18 * Maxim Osipov <maxim.osipov@siemens.com>
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>
32 #include "ieee802154.h"
36 static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
37 u32 seq, int flags, struct wpan_phy *phy)
41 uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL);
43 pr_debug("%s\n", __func__);
48 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
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))
58 for (i = 0; i < 32; i++) {
59 if (phy->channels_supported[i])
60 buf[pages++] = phy->channels_supported[i] | (i << 27);
63 nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
64 pages * sizeof(uint32_t), buf))
66 mutex_unlock(&phy->pib_lock);
68 genlmsg_end(msg, hdr);
72 mutex_unlock(&phy->pib_lock);
73 genlmsg_cancel(msg, hdr);
79 int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
81 /* Request for interface name, index, type, IEEE address,
82 * PAN Id, short address
89 pr_debug("%s\n", __func__);
91 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
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 */
98 phy = wpan_phy_find(name);
102 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
106 rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq,
113 return genlmsg_reply(msg, info);
121 struct dump_phy_data {
123 struct netlink_callback *cb;
127 static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
130 struct dump_phy_data *data = _data;
132 pr_debug("%s\n", __func__);
134 if (data->idx++ < data->s_idx)
137 rc = ieee802154_nl_fill_phy(data->skb,
138 NETLINK_CB(data->cb->skb).portid,
139 data->cb->nlh->nlmsg_seq,
151 int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
153 struct dump_phy_data data = {
156 .s_idx = cb->args[0],
160 pr_debug("%s\n", __func__);
162 wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
164 cb->args[0] = data.idx;
169 int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info)
172 struct wpan_phy *phy;
176 struct net_device *dev;
177 int type = __IEEE802154_DEV_INVALID;
179 pr_debug("%s\n", __func__);
181 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
184 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
185 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
186 return -EINVAL; /* phy name should be null-terminated */
188 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
189 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
190 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
192 return -EINVAL; /* phy name should be null-terminated */
197 if (strlen(devname) >= IFNAMSIZ)
198 return -ENAMETOOLONG;
200 phy = wpan_phy_find(name);
204 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
208 if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
209 nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
210 IEEE802154_ADDR_LEN) {
212 goto nla_put_failure;
215 if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
216 type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
217 if (type >= __IEEE802154_DEV_MAX) {
219 goto nla_put_failure;
223 dev = rdev_add_virtual_intf_deprecated(wpan_phy_to_rdev(phy), devname,
227 goto nla_put_failure;
231 if (info->attrs[IEEE802154_ATTR_HW_ADDR]) {
232 struct sockaddr addr;
234 addr.sa_family = ARPHRD_IEEE802154;
235 nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR],
236 IEEE802154_ADDR_LEN);
238 /* strangely enough, some callbacks (inetdev_event) from
239 * dev_set_mac_address require RTNL_LOCK
242 rc = dev_set_mac_address(dev, &addr);
248 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
249 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name))
250 goto nla_put_failure;
255 return ieee802154_nl_reply(msg, info);
258 rtnl_lock(); /* del_iface must be called with RTNL lock */
259 rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
269 int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
272 struct wpan_phy *phy;
275 struct net_device *dev;
277 pr_debug("%s\n", __func__);
279 if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
282 name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
283 if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
284 return -EINVAL; /* name should be null-terminated */
286 dev = dev_get_by_name(genl_info_net(info), name);
290 phy = dev->ieee802154_ptr->wpan_phy;
292 get_device(&phy->dev);
295 /* phy name is optional, but should be checked if it's given */
296 if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
297 struct wpan_phy *phy2;
300 nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
301 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
303 /* name should be null-terminated */
306 phy2 = wpan_phy_find(pname);
318 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
323 rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
325 /* We don't have device anymore */
331 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
332 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
333 goto nla_put_failure;
336 return ieee802154_nl_reply(msg, info);