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
) ||
60 nla_put_u8(msg
, IEEE802154_ATTR_LBT_ENABLED
, phy
->lbt
) ||
61 nla_put_u8(msg
, IEEE802154_ATTR_CCA_MODE
, phy
->cca_mode
) ||
62 nla_put_s32(msg
, IEEE802154_ATTR_CCA_ED_LEVEL
, phy
->cca_ed_level
) ||
63 nla_put_u8(msg
, IEEE802154_ATTR_CSMA_RETRIES
, phy
->csma_retries
) ||
64 nla_put_u8(msg
, IEEE802154_ATTR_CSMA_MIN_BE
, phy
->min_be
) ||
65 nla_put_u8(msg
, IEEE802154_ATTR_CSMA_MAX_BE
, phy
->max_be
) ||
66 nla_put_s8(msg
, IEEE802154_ATTR_FRAME_RETRIES
, phy
->frame_retries
))
68 for (i
= 0; i
< 32; i
++) {
69 if (phy
->channels_supported
[i
])
70 buf
[pages
++] = phy
->channels_supported
[i
] | (i
<< 27);
73 nla_put(msg
, IEEE802154_ATTR_CHANNEL_PAGE_LIST
,
74 pages
* sizeof(uint32_t), buf
))
76 mutex_unlock(&phy
->pib_lock
);
78 return genlmsg_end(msg
, hdr
);
81 mutex_unlock(&phy
->pib_lock
);
82 genlmsg_cancel(msg
, hdr
);
88 int ieee802154_list_phy(struct sk_buff
*skb
, struct genl_info
*info
)
90 /* Request for interface name, index, type, IEEE address,
91 PAN Id, short address */
97 pr_debug("%s\n", __func__
);
99 if (!info
->attrs
[IEEE802154_ATTR_PHY_NAME
])
102 name
= nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
103 if (name
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1] != '\0')
104 return -EINVAL
; /* phy name should be null-terminated */
107 phy
= wpan_phy_find(name
);
111 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
115 rc
= ieee802154_nl_fill_phy(msg
, info
->snd_portid
, info
->snd_seq
,
122 return genlmsg_reply(msg
, info
);
131 struct dump_phy_data
{
133 struct netlink_callback
*cb
;
137 static int ieee802154_dump_phy_iter(struct wpan_phy
*phy
, void *_data
)
140 struct dump_phy_data
*data
= _data
;
142 pr_debug("%s\n", __func__
);
144 if (data
->idx
++ < data
->s_idx
)
147 rc
= ieee802154_nl_fill_phy(data
->skb
,
148 NETLINK_CB(data
->cb
->skb
).portid
,
149 data
->cb
->nlh
->nlmsg_seq
,
161 int ieee802154_dump_phy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
163 struct dump_phy_data data
= {
166 .s_idx
= cb
->args
[0],
170 pr_debug("%s\n", __func__
);
172 wpan_phy_for_each(ieee802154_dump_phy_iter
, &data
);
174 cb
->args
[0] = data
.idx
;
179 int ieee802154_add_iface(struct sk_buff
*skb
, struct genl_info
*info
)
182 struct wpan_phy
*phy
;
186 struct net_device
*dev
;
187 int type
= __IEEE802154_DEV_INVALID
;
189 pr_debug("%s\n", __func__
);
191 if (!info
->attrs
[IEEE802154_ATTR_PHY_NAME
])
194 name
= nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
195 if (name
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1] != '\0')
196 return -EINVAL
; /* phy name should be null-terminated */
198 if (info
->attrs
[IEEE802154_ATTR_DEV_NAME
]) {
199 devname
= nla_data(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]);
200 if (devname
[nla_len(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]) - 1]
202 return -EINVAL
; /* phy name should be null-terminated */
207 if (strlen(devname
) >= IFNAMSIZ
)
208 return -ENAMETOOLONG
;
210 phy
= wpan_phy_find(name
);
214 msg
= ieee802154_nl_new_reply(info
, 0, IEEE802154_ADD_IFACE
);
218 if (!phy
->add_iface
) {
220 goto nla_put_failure
;
223 if (info
->attrs
[IEEE802154_ATTR_HW_ADDR
] &&
224 nla_len(info
->attrs
[IEEE802154_ATTR_HW_ADDR
]) !=
225 IEEE802154_ADDR_LEN
) {
227 goto nla_put_failure
;
230 if (info
->attrs
[IEEE802154_ATTR_DEV_TYPE
]) {
231 type
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_DEV_TYPE
]);
232 if (type
>= __IEEE802154_DEV_MAX
) {
234 goto nla_put_failure
;
238 dev
= phy
->add_iface(phy
, devname
, type
);
241 goto nla_put_failure
;
244 if (info
->attrs
[IEEE802154_ATTR_HW_ADDR
]) {
245 struct sockaddr addr
;
247 addr
.sa_family
= ARPHRD_IEEE802154
;
248 nla_memcpy(&addr
.sa_data
, info
->attrs
[IEEE802154_ATTR_HW_ADDR
],
249 IEEE802154_ADDR_LEN
);
252 * strangely enough, some callbacks (inetdev_event) from
253 * dev_set_mac_address require RTNL_LOCK
256 rc
= dev_set_mac_address(dev
, &addr
);
262 if (nla_put_string(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
)) ||
263 nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
))
264 goto nla_put_failure
;
269 return ieee802154_nl_reply(msg
, info
);
272 rtnl_lock(); /* del_iface must be called with RTNL lock */
273 phy
->del_iface(phy
, dev
);
283 int ieee802154_del_iface(struct sk_buff
*skb
, struct genl_info
*info
)
286 struct wpan_phy
*phy
;
289 struct net_device
*dev
;
291 pr_debug("%s\n", __func__
);
293 if (!info
->attrs
[IEEE802154_ATTR_DEV_NAME
])
296 name
= nla_data(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]);
297 if (name
[nla_len(info
->attrs
[IEEE802154_ATTR_DEV_NAME
]) - 1] != '\0')
298 return -EINVAL
; /* name should be null-terminated */
300 dev
= dev_get_by_name(genl_info_net(info
), name
);
304 phy
= ieee802154_mlme_ops(dev
)->get_phy(dev
);
308 /* phy name is optional, but should be checked if it's given */
309 if (info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) {
310 struct wpan_phy
*phy2
;
313 nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
314 if (pname
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1]
316 /* name should be null-terminated */
319 phy2
= wpan_phy_find(pname
);
331 msg
= ieee802154_nl_new_reply(info
, 0, IEEE802154_DEL_IFACE
);
335 if (!phy
->del_iface
) {
337 goto nla_put_failure
;
341 phy
->del_iface(phy
, dev
);
343 /* We don't have device anymore */
349 if (nla_put_string(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
)) ||
350 nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, name
))
351 goto nla_put_failure
;
354 return ieee802154_nl_reply(msg
, info
);
366 static int phy_set_txpower(struct wpan_phy
*phy
, struct genl_info
*info
)
368 int txpower
= nla_get_s8(info
->attrs
[IEEE802154_ATTR_TXPOWER
]);
371 rc
= phy
->set_txpower(phy
, txpower
);
375 phy
->transmit_power
= txpower
;
380 static int phy_set_lbt(struct wpan_phy
*phy
, struct genl_info
*info
)
382 u8 on
= !!nla_get_u8(info
->attrs
[IEEE802154_ATTR_LBT_ENABLED
]);
385 rc
= phy
->set_lbt(phy
, on
);
394 static int phy_set_cca_mode(struct wpan_phy
*phy
, struct genl_info
*info
)
396 u8 mode
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_CCA_MODE
]);
402 rc
= phy
->set_cca_mode(phy
, mode
);
406 phy
->cca_mode
= mode
;
411 static int phy_set_cca_ed_level(struct wpan_phy
*phy
, struct genl_info
*info
)
413 s32 level
= nla_get_s32(info
->attrs
[IEEE802154_ATTR_CCA_ED_LEVEL
]);
416 rc
= phy
->set_cca_ed_level(phy
, level
);
420 phy
->cca_ed_level
= level
;
425 static int phy_set_csma_params(struct wpan_phy
*phy
, struct genl_info
*info
)
428 u8 min_be
= phy
->min_be
;
429 u8 max_be
= phy
->max_be
;
430 u8 retries
= phy
->csma_retries
;
432 if (info
->attrs
[IEEE802154_ATTR_CSMA_RETRIES
])
433 retries
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_CSMA_RETRIES
]);
434 if (info
->attrs
[IEEE802154_ATTR_CSMA_MIN_BE
])
435 min_be
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_CSMA_MIN_BE
]);
436 if (info
->attrs
[IEEE802154_ATTR_CSMA_MAX_BE
])
437 max_be
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_CSMA_MAX_BE
]);
439 if (retries
> 5 || max_be
< 3 || max_be
> 8 || min_be
> max_be
)
442 rc
= phy
->set_csma_params(phy
, min_be
, max_be
, retries
);
446 phy
->min_be
= min_be
;
447 phy
->max_be
= max_be
;
448 phy
->csma_retries
= retries
;
453 static int phy_set_frame_retries(struct wpan_phy
*phy
, struct genl_info
*info
)
455 s8 retries
= nla_get_s8(info
->attrs
[IEEE802154_ATTR_FRAME_RETRIES
]);
458 if (retries
< -1 || retries
> 7)
461 rc
= phy
->set_frame_retries(phy
, retries
);
465 phy
->frame_retries
= retries
;
470 int ieee802154_set_phyparams(struct sk_buff
*skb
, struct genl_info
*info
)
472 struct wpan_phy
*phy
;
476 pr_debug("%s\n", __func__
);
478 if (!info
->attrs
[IEEE802154_ATTR_PHY_NAME
] &&
479 !info
->attrs
[IEEE802154_ATTR_LBT_ENABLED
] &&
480 !info
->attrs
[IEEE802154_ATTR_CCA_MODE
] &&
481 !info
->attrs
[IEEE802154_ATTR_CCA_ED_LEVEL
] &&
482 !info
->attrs
[IEEE802154_ATTR_CSMA_RETRIES
] &&
483 !info
->attrs
[IEEE802154_ATTR_CSMA_MIN_BE
] &&
484 !info
->attrs
[IEEE802154_ATTR_CSMA_MAX_BE
] &&
485 !info
->attrs
[IEEE802154_ATTR_FRAME_RETRIES
])
488 name
= nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
489 if (name
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1] != '\0')
490 return -EINVAL
; /* phy name should be null-terminated */
492 phy
= wpan_phy_find(name
);
496 if ((!phy
->set_txpower
&& info
->attrs
[IEEE802154_ATTR_TXPOWER
]) ||
497 (!phy
->set_lbt
&& info
->attrs
[IEEE802154_ATTR_LBT_ENABLED
]) ||
498 (!phy
->set_cca_mode
&& info
->attrs
[IEEE802154_ATTR_CCA_MODE
]) ||
499 (!phy
->set_cca_ed_level
&&
500 info
->attrs
[IEEE802154_ATTR_CCA_ED_LEVEL
]))
503 mutex_lock(&phy
->pib_lock
);
505 if (info
->attrs
[IEEE802154_ATTR_TXPOWER
]) {
506 rc
= phy_set_txpower(phy
, info
);
511 if (info
->attrs
[IEEE802154_ATTR_LBT_ENABLED
]) {
512 rc
= phy_set_lbt(phy
, info
);
517 if (info
->attrs
[IEEE802154_ATTR_CCA_MODE
]) {
518 rc
= phy_set_cca_mode(phy
, info
);
523 if (info
->attrs
[IEEE802154_ATTR_CCA_ED_LEVEL
]) {
524 rc
= phy_set_cca_ed_level(phy
, info
);
529 if (info
->attrs
[IEEE802154_ATTR_CSMA_RETRIES
] ||
530 info
->attrs
[IEEE802154_ATTR_CSMA_MIN_BE
] ||
531 info
->attrs
[IEEE802154_ATTR_CSMA_MAX_BE
]) {
532 rc
= phy_set_csma_params(phy
, info
);
537 if (info
->attrs
[IEEE802154_ATTR_FRAME_RETRIES
]) {
538 rc
= phy_set_frame_retries(phy
, info
);
543 mutex_unlock(&phy
->pib_lock
);
550 mutex_unlock(&phy
->pib_lock
);