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 pid
,
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 static int ieee802154_list_phy(struct sk_buff
*skb
,
81 struct genl_info
*info
)
83 /* Request for interface name, index, type, IEEE address,
84 PAN Id, short address */
90 pr_debug("%s\n", __func__
);
92 if (!info
->attrs
[IEEE802154_ATTR_PHY_NAME
])
95 name
= nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
96 if (name
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1] != '\0')
97 return -EINVAL
; /* phy name should be null-terminated */
100 phy
= wpan_phy_find(name
);
104 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
108 rc
= ieee802154_nl_fill_phy(msg
, info
->snd_pid
, info
->snd_seq
,
115 return genlmsg_reply(msg
, info
);
124 struct dump_phy_data
{
126 struct netlink_callback
*cb
;
130 static int ieee802154_dump_phy_iter(struct wpan_phy
*phy
, void *_data
)
133 struct dump_phy_data
*data
= _data
;
135 pr_debug("%s\n", __func__
);
137 if (data
->idx
++ < data
->s_idx
)
140 rc
= ieee802154_nl_fill_phy(data
->skb
,
141 NETLINK_CB(data
->cb
->skb
).pid
,
142 data
->cb
->nlh
->nlmsg_seq
,
154 static int ieee802154_dump_phy(struct sk_buff
*skb
,
155 struct netlink_callback
*cb
)
157 struct dump_phy_data data
= {
160 .s_idx
= cb
->args
[0],
164 pr_debug("%s\n", __func__
);
166 wpan_phy_for_each(ieee802154_dump_phy_iter
, &data
);
168 cb
->args
[0] = data
.idx
;
173 static int ieee802154_add_iface(struct sk_buff
*skb
,
174 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
)
231 dev
= phy
->add_iface(phy
, devname
, type
);
234 goto nla_put_failure
;
237 if (info
->attrs
[IEEE802154_ATTR_HW_ADDR
]) {
238 struct sockaddr addr
;
240 addr
.sa_family
= ARPHRD_IEEE802154
;
241 nla_memcpy(&addr
.sa_data
, info
->attrs
[IEEE802154_ATTR_HW_ADDR
],
242 IEEE802154_ADDR_LEN
);
245 * strangely enough, some callbacks (inetdev_event) from
246 * dev_set_mac_address require RTNL_LOCK
249 rc
= dev_set_mac_address(dev
, &addr
);
255 if (nla_put_string(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
)) ||
256 nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
))
257 goto nla_put_failure
;
262 return ieee802154_nl_reply(msg
, info
);
265 rtnl_lock(); /* del_iface must be called with RTNL lock */
266 phy
->del_iface(phy
, dev
);
276 static int ieee802154_del_iface(struct sk_buff
*skb
,
277 struct genl_info
*info
)
280 struct wpan_phy
*phy
;
283 struct net_device
*dev
;
285 pr_debug("%s\n", __func__
);
287 if (!info
->attrs
[IEEE802154_ATTR_DEV_NAME
])
290 name
= nla_data(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]);
291 if (name
[nla_len(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]) - 1] != '\0')
292 return -EINVAL
; /* name should be null-terminated */
294 dev
= dev_get_by_name(genl_info_net(info
), name
);
298 phy
= ieee802154_mlme_ops(dev
)->get_phy(dev
);
302 /* phy name is optional, but should be checked if it's given */
303 if (info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) {
304 struct wpan_phy
*phy2
;
307 nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
308 if (pname
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1]
310 /* name should be null-terminated */
313 phy2
= wpan_phy_find(pname
);
325 msg
= ieee802154_nl_new_reply(info
, 0, IEEE802154_DEL_IFACE
);
329 if (!phy
->del_iface
) {
331 goto nla_put_failure
;
335 phy
->del_iface(phy
, dev
);
337 /* We don't have device anymore */
343 if (nla_put_string(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
)) ||
344 nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, name
))
345 goto nla_put_failure
;
348 return ieee802154_nl_reply(msg
, info
);
360 static struct genl_ops ieee802154_phy_ops
[] = {
361 IEEE802154_DUMP(IEEE802154_LIST_PHY
, ieee802154_list_phy
,
362 ieee802154_dump_phy
),
363 IEEE802154_OP(IEEE802154_ADD_IFACE
, ieee802154_add_iface
),
364 IEEE802154_OP(IEEE802154_DEL_IFACE
, ieee802154_del_iface
),
368 * No need to unregister as family unregistration will do it.
370 int nl802154_phy_register(void)
375 for (i
= 0; i
< ARRAY_SIZE(ieee802154_phy_ops
); i
++) {
376 rc
= genl_register_ops(&nl802154_family
,
377 &ieee802154_phy_ops
[i
]);