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
) ||
59 nla_put_s8(msg
, IEEE802154_ATTR_TXPOWER
, phy
->transmit_power
))
61 for (i
= 0; i
< 32; i
++) {
62 if (phy
->channels_supported
[i
])
63 buf
[pages
++] = phy
->channels_supported
[i
] | (i
<< 27);
66 nla_put(msg
, IEEE802154_ATTR_CHANNEL_PAGE_LIST
,
67 pages
* sizeof(uint32_t), buf
))
69 mutex_unlock(&phy
->pib_lock
);
71 return genlmsg_end(msg
, hdr
);
74 mutex_unlock(&phy
->pib_lock
);
75 genlmsg_cancel(msg
, hdr
);
81 int ieee802154_list_phy(struct sk_buff
*skb
, 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_portid
, 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
).portid
,
142 data
->cb
->nlh
->nlmsg_seq
,
154 int ieee802154_dump_phy(struct sk_buff
*skb
, 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 int ieee802154_add_iface(struct sk_buff
*skb
, struct genl_info
*info
)
175 struct wpan_phy
*phy
;
179 struct net_device
*dev
;
180 int type
= __IEEE802154_DEV_INVALID
;
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 if (info
->attrs
[IEEE802154_ATTR_HW_ADDR
] &&
217 nla_len(info
->attrs
[IEEE802154_ATTR_HW_ADDR
]) !=
218 IEEE802154_ADDR_LEN
) {
220 goto nla_put_failure
;
223 if (info
->attrs
[IEEE802154_ATTR_DEV_TYPE
]) {
224 type
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_DEV_TYPE
]);
225 if (type
>= __IEEE802154_DEV_MAX
) {
227 goto nla_put_failure
;
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 int ieee802154_del_iface(struct sk_buff
*skb
, struct genl_info
*info
)
279 struct wpan_phy
*phy
;
282 struct net_device
*dev
;
284 pr_debug("%s\n", __func__
);
286 if (!info
->attrs
[IEEE802154_ATTR_DEV_NAME
])
289 name
= nla_data(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]);
290 if (name
[nla_len(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]) - 1] != '\0')
291 return -EINVAL
; /* name should be null-terminated */
293 dev
= dev_get_by_name(genl_info_net(info
), name
);
297 phy
= ieee802154_mlme_ops(dev
)->get_phy(dev
);
301 /* phy name is optional, but should be checked if it's given */
302 if (info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) {
303 struct wpan_phy
*phy2
;
306 nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
307 if (pname
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1]
309 /* name should be null-terminated */
312 phy2
= wpan_phy_find(pname
);
324 msg
= ieee802154_nl_new_reply(info
, 0, IEEE802154_DEL_IFACE
);
328 if (!phy
->del_iface
) {
330 goto nla_put_failure
;
334 phy
->del_iface(phy
, dev
);
336 /* We don't have device anymore */
342 if (nla_put_string(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
)) ||
343 nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, name
))
344 goto nla_put_failure
;
347 return ieee802154_nl_reply(msg
, info
);
359 int ieee802154_set_phyparams(struct sk_buff
*skb
, struct genl_info
*info
)
361 struct wpan_phy
*phy
;
366 pr_debug("%s\n", __func__
);
368 if (!info
->attrs
[IEEE802154_ATTR_PHY_NAME
])
371 name
= nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
372 if (name
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1] != '\0')
373 return -EINVAL
; /* phy name should be null-terminated */
375 txpower
= nla_get_s8(info
->attrs
[IEEE802154_ATTR_TXPOWER
]);
377 phy
= wpan_phy_find(name
);
381 if (!phy
->set_txpower
)
384 mutex_lock(&phy
->pib_lock
);
386 rc
= phy
->set_txpower(phy
, txpower
);
388 mutex_unlock(&phy
->pib_lock
);
392 phy
->transmit_power
= txpower
;
394 mutex_unlock(&phy
->pib_lock
);