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
))
60 for (i
= 0; i
< 32; i
++) {
61 if (phy
->channels_supported
[i
])
62 buf
[pages
++] = phy
->channels_supported
[i
] | (i
<< 27);
65 nla_put(msg
, IEEE802154_ATTR_CHANNEL_PAGE_LIST
,
66 pages
* sizeof(uint32_t), buf
))
68 mutex_unlock(&phy
->pib_lock
);
70 return genlmsg_end(msg
, hdr
);
73 mutex_unlock(&phy
->pib_lock
);
74 genlmsg_cancel(msg
, hdr
);
80 int ieee802154_list_phy(struct sk_buff
*skb
, 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_DEFAULT_SIZE
, GFP_KERNEL
);
107 rc
= ieee802154_nl_fill_phy(msg
, info
->snd_portid
, 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
).portid
,
141 data
->cb
->nlh
->nlmsg_seq
,
153 int ieee802154_dump_phy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
155 struct dump_phy_data data
= {
158 .s_idx
= cb
->args
[0],
162 pr_debug("%s\n", __func__
);
164 wpan_phy_for_each(ieee802154_dump_phy_iter
, &data
);
166 cb
->args
[0] = data
.idx
;
171 int ieee802154_add_iface(struct sk_buff
*skb
, struct genl_info
*info
)
174 struct wpan_phy
*phy
;
178 struct net_device
*dev
;
179 int type
= __IEEE802154_DEV_INVALID
;
181 pr_debug("%s\n", __func__
);
183 if (!info
->attrs
[IEEE802154_ATTR_PHY_NAME
])
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 */
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]
194 return -EINVAL
; /* phy name should be null-terminated */
199 if (strlen(devname
) >= IFNAMSIZ
)
200 return -ENAMETOOLONG
;
202 phy
= wpan_phy_find(name
);
206 msg
= ieee802154_nl_new_reply(info
, 0, IEEE802154_ADD_IFACE
);
210 if (!phy
->add_iface
) {
212 goto nla_put_failure
;
215 if (info
->attrs
[IEEE802154_ATTR_HW_ADDR
] &&
216 nla_len(info
->attrs
[IEEE802154_ATTR_HW_ADDR
]) !=
217 IEEE802154_ADDR_LEN
) {
219 goto nla_put_failure
;
222 if (info
->attrs
[IEEE802154_ATTR_DEV_TYPE
]) {
223 type
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_DEV_TYPE
]);
224 if (type
>= __IEEE802154_DEV_MAX
) {
226 goto nla_put_failure
;
230 dev
= phy
->add_iface(phy
, devname
, type
);
233 goto nla_put_failure
;
236 if (info
->attrs
[IEEE802154_ATTR_HW_ADDR
]) {
237 struct sockaddr addr
;
239 addr
.sa_family
= ARPHRD_IEEE802154
;
240 nla_memcpy(&addr
.sa_data
, info
->attrs
[IEEE802154_ATTR_HW_ADDR
],
241 IEEE802154_ADDR_LEN
);
244 * strangely enough, some callbacks (inetdev_event) from
245 * dev_set_mac_address require RTNL_LOCK
248 rc
= dev_set_mac_address(dev
, &addr
);
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
;
261 return ieee802154_nl_reply(msg
, info
);
264 rtnl_lock(); /* del_iface must be called with RTNL lock */
265 phy
->del_iface(phy
, dev
);
275 int ieee802154_del_iface(struct sk_buff
*skb
, struct genl_info
*info
)
278 struct wpan_phy
*phy
;
281 struct net_device
*dev
;
283 pr_debug("%s\n", __func__
);
285 if (!info
->attrs
[IEEE802154_ATTR_DEV_NAME
])
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 */
292 dev
= dev_get_by_name(genl_info_net(info
), name
);
296 phy
= ieee802154_mlme_ops(dev
)->get_phy(dev
);
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
;
305 nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
306 if (pname
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1]
308 /* name should be null-terminated */
311 phy2
= wpan_phy_find(pname
);
323 msg
= ieee802154_nl_new_reply(info
, 0, IEEE802154_DEL_IFACE
);
327 if (!phy
->del_iface
) {
329 goto nla_put_failure
;
333 phy
->del_iface(phy
, dev
);
335 /* We don't have device anymore */
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
;
346 return ieee802154_nl_reply(msg
, info
);