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 <net/netlink.h>
28 #include <net/genetlink.h>
29 #include <net/wpan-phy.h>
30 #include <net/af_ieee802154.h>
31 #include <net/ieee802154_netdev.h>
32 #include <net/rtnetlink.h> /* for rtnl_{un,}lock */
33 #include <linux/nl802154.h>
35 #include "ieee802154.h"
37 static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 pid,
38 u32 seq, int flags, struct wpan_phy *phy)
42 uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL);
44 pr_debug("%s\n", __func__);
49 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
54 mutex_lock(&phy->pib_lock);
55 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 for (i = 0; i < 32; i++) {
60 if (phy->channels_supported[i])
61 buf[pages++] = phy->channels_supported[i] | (i << 27);
64 NLA_PUT(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
65 pages * sizeof(uint32_t), buf);
67 mutex_unlock(&phy->pib_lock);
69 return genlmsg_end(msg, hdr);
72 mutex_unlock(&phy->pib_lock);
73 genlmsg_cancel(msg, hdr);
79 static int ieee802154_list_phy(struct sk_buff *skb,
80 struct genl_info *info)
82 /* Request for interface name, index, type, IEEE address,
83 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 */
99 phy = wpan_phy_find(name);
103 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
107 rc = ieee802154_nl_fill_phy(msg, info->snd_pid, info->snd_seq,
114 return genlmsg_reply(msg, info);
123 struct dump_phy_data {
125 struct netlink_callback *cb;
129 static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
132 struct dump_phy_data *data = _data;
134 pr_debug("%s\n", __func__);
136 if (data->idx++ < data->s_idx)
139 rc = ieee802154_nl_fill_phy(data->skb,
140 NETLINK_CB(data->cb->skb).pid,
141 data->cb->nlh->nlmsg_seq,
153 static int ieee802154_dump_phy(struct sk_buff *skb,
154 struct netlink_callback *cb)
156 struct dump_phy_data data = {
159 .s_idx = cb->args[0],
163 pr_debug("%s\n", __func__);
165 wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
167 cb->args[0] = data.idx;
172 static int ieee802154_add_iface(struct sk_buff *skb,
173 struct genl_info *info)
176 struct wpan_phy *phy;
180 struct net_device *dev;
182 pr_debug("%s\n", __func__);
184 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
187 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
188 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
189 return -EINVAL; /* phy name should be null-terminated */
191 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
192 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
193 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
195 return -EINVAL; /* phy name should be null-terminated */
200 if (strlen(devname) >= IFNAMSIZ)
201 return -ENAMETOOLONG;
203 phy = wpan_phy_find(name);
207 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
211 if (!phy->add_iface) {
213 goto nla_put_failure;
216 dev = phy->add_iface(phy, devname);
219 goto nla_put_failure;
222 NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
223 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
229 return ieee802154_nl_reply(msg, info);
238 static int ieee802154_del_iface(struct sk_buff *skb,
239 struct genl_info *info)
242 struct wpan_phy *phy;
245 struct net_device *dev;
247 pr_debug("%s\n", __func__);
249 if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
252 name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
253 if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
254 return -EINVAL; /* name should be null-terminated */
256 dev = dev_get_by_name(genl_info_net(info), name);
260 phy = ieee802154_mlme_ops(dev)->get_phy(dev);
264 /* phy name is optional, but should be checked if it's given */
265 if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
266 struct wpan_phy *phy2;
269 nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
270 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
272 /* name should be null-terminated */
275 phy2 = wpan_phy_find(pname);
287 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
291 if (!phy->del_iface) {
293 goto nla_put_failure;
297 phy->del_iface(phy, dev);
299 /* We don't have device anymore */
306 NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
307 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, name);
311 return ieee802154_nl_reply(msg, info);
323 static struct genl_ops ieee802154_phy_ops[] = {
324 IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
325 ieee802154_dump_phy),
326 IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
327 IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
331 * No need to unregister as family unregistration will do it.
333 int nl802154_phy_register(void)
338 for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) {
339 rc = genl_register_ops(&nl802154_family,
340 &ieee802154_phy_ops[i]);