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.
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"
34 static int ieee802154_nl_fill_phy(struct sk_buff
*msg
, u32 portid
,
35 u32 seq
, int flags
, struct wpan_phy
*phy
)
39 uint32_t *buf
= kzalloc(32 * sizeof(uint32_t), GFP_KERNEL
);
41 pr_debug("%s\n", __func__
);
46 hdr
= genlmsg_put(msg
, 0, seq
, &nl802154_family
, flags
,
51 mutex_lock(&phy
->pib_lock
);
52 if (nla_put_string(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
)) ||
53 nla_put_u8(msg
, IEEE802154_ATTR_PAGE
, phy
->current_page
) ||
54 nla_put_u8(msg
, IEEE802154_ATTR_CHANNEL
, phy
->current_channel
))
56 for (i
= 0; i
< 32; i
++) {
57 if (phy
->channels_supported
[i
])
58 buf
[pages
++] = phy
->channels_supported
[i
] | (i
<< 27);
61 nla_put(msg
, IEEE802154_ATTR_CHANNEL_PAGE_LIST
,
62 pages
* sizeof(uint32_t), buf
))
64 mutex_unlock(&phy
->pib_lock
);
66 return genlmsg_end(msg
, hdr
);
69 mutex_unlock(&phy
->pib_lock
);
70 genlmsg_cancel(msg
, hdr
);
76 int ieee802154_list_phy(struct sk_buff
*skb
, struct genl_info
*info
)
78 /* Request for interface name, index, type, IEEE address,
79 * PAN Id, short address
86 pr_debug("%s\n", __func__
);
88 if (!info
->attrs
[IEEE802154_ATTR_PHY_NAME
])
91 name
= nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
92 if (name
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1] != '\0')
93 return -EINVAL
; /* phy name should be null-terminated */
96 phy
= wpan_phy_find(name
);
100 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
104 rc
= ieee802154_nl_fill_phy(msg
, info
->snd_portid
, info
->snd_seq
,
111 return genlmsg_reply(msg
, info
);
119 struct dump_phy_data
{
121 struct netlink_callback
*cb
;
125 static int ieee802154_dump_phy_iter(struct wpan_phy
*phy
, void *_data
)
128 struct dump_phy_data
*data
= _data
;
130 pr_debug("%s\n", __func__
);
132 if (data
->idx
++ < data
->s_idx
)
135 rc
= ieee802154_nl_fill_phy(data
->skb
,
136 NETLINK_CB(data
->cb
->skb
).portid
,
137 data
->cb
->nlh
->nlmsg_seq
,
149 int ieee802154_dump_phy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
151 struct dump_phy_data data
= {
154 .s_idx
= cb
->args
[0],
158 pr_debug("%s\n", __func__
);
160 wpan_phy_for_each(ieee802154_dump_phy_iter
, &data
);
162 cb
->args
[0] = data
.idx
;
167 int ieee802154_add_iface(struct sk_buff
*skb
, struct genl_info
*info
)
170 struct wpan_phy
*phy
;
174 struct net_device
*dev
;
175 int type
= __IEEE802154_DEV_INVALID
;
177 pr_debug("%s\n", __func__
);
179 if (!info
->attrs
[IEEE802154_ATTR_PHY_NAME
])
182 name
= nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
183 if (name
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1] != '\0')
184 return -EINVAL
; /* phy name should be null-terminated */
186 if (info
->attrs
[IEEE802154_ATTR_DEV_NAME
]) {
187 devname
= nla_data(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]);
188 if (devname
[nla_len(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]) - 1]
190 return -EINVAL
; /* phy name should be null-terminated */
195 if (strlen(devname
) >= IFNAMSIZ
)
196 return -ENAMETOOLONG
;
198 phy
= wpan_phy_find(name
);
202 msg
= ieee802154_nl_new_reply(info
, 0, IEEE802154_ADD_IFACE
);
206 if (!phy
->add_iface
) {
208 goto nla_put_failure
;
211 if (info
->attrs
[IEEE802154_ATTR_HW_ADDR
] &&
212 nla_len(info
->attrs
[IEEE802154_ATTR_HW_ADDR
]) !=
213 IEEE802154_ADDR_LEN
) {
215 goto nla_put_failure
;
218 if (info
->attrs
[IEEE802154_ATTR_DEV_TYPE
]) {
219 type
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_DEV_TYPE
]);
220 if (type
>= __IEEE802154_DEV_MAX
) {
222 goto nla_put_failure
;
226 dev
= phy
->add_iface(phy
, devname
, type
);
229 goto nla_put_failure
;
232 if (info
->attrs
[IEEE802154_ATTR_HW_ADDR
]) {
233 struct sockaddr addr
;
235 addr
.sa_family
= ARPHRD_IEEE802154
;
236 nla_memcpy(&addr
.sa_data
, info
->attrs
[IEEE802154_ATTR_HW_ADDR
],
237 IEEE802154_ADDR_LEN
);
239 /* strangely enough, some callbacks (inetdev_event) from
240 * dev_set_mac_address require RTNL_LOCK
243 rc
= dev_set_mac_address(dev
, &addr
);
249 if (nla_put_string(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
)) ||
250 nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
))
251 goto nla_put_failure
;
256 return ieee802154_nl_reply(msg
, info
);
259 rtnl_lock(); /* del_iface must be called with RTNL lock */
260 phy
->del_iface(phy
, dev
);
270 int ieee802154_del_iface(struct sk_buff
*skb
, struct genl_info
*info
)
273 struct wpan_phy
*phy
;
276 struct net_device
*dev
;
278 pr_debug("%s\n", __func__
);
280 if (!info
->attrs
[IEEE802154_ATTR_DEV_NAME
])
283 name
= nla_data(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]);
284 if (name
[nla_len(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]) - 1] != '\0')
285 return -EINVAL
; /* name should be null-terminated */
287 dev
= dev_get_by_name(genl_info_net(info
), name
);
291 phy
= ieee802154_mlme_ops(dev
)->get_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
);
322 if (!phy
->del_iface
) {
324 goto nla_put_failure
;
328 phy
->del_iface(phy
, dev
);
330 /* We don't have device anymore */
336 if (nla_put_string(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
)) ||
337 nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, name
))
338 goto nla_put_failure
;
341 return ieee802154_nl_reply(msg
, info
);