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/if_arp.h>
27 #include <linux/netdevice.h>
28 #include <net/netlink.h>
29 #include <net/genetlink.h>
31 #include <linux/nl802154.h>
32 #include <net/af_ieee802154.h>
33 #include <net/nl802154.h>
34 #include <net/ieee802154.h>
35 #include <net/ieee802154_netdev.h>
37 static unsigned int ieee802154_seq_num;
39 static struct genl_family ieee802154_coordinator_family = {
40 .id = GENL_ID_GENERATE,
42 .name = IEEE802154_NL_NAME,
44 .maxattr = IEEE802154_ATTR_MAX,
47 static struct genl_multicast_group ieee802154_coord_mcgrp = {
48 .name = IEEE802154_MCAST_COORD_NAME,
51 static struct genl_multicast_group ieee802154_beacon_mcgrp = {
52 .name = IEEE802154_MCAST_BEACON_NAME,
55 /* Requests to userspace */
56 static struct sk_buff *ieee802154_nl_create(int flags, u8 req)
59 struct sk_buff *msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
64 hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
65 &ieee802154_coordinator_family, flags, req);
74 static int ieee802154_nl_finish(struct sk_buff *msg)
76 /* XXX: nlh is right at the start of msg */
77 void *hdr = genlmsg_data(NLMSG_DATA(msg->data));
79 if (genlmsg_end(msg, hdr) < 0)
82 return genlmsg_multicast(msg, 0, ieee802154_coord_mcgrp.id,
89 int ieee802154_nl_assoc_indic(struct net_device *dev,
90 struct ieee802154_addr *addr, u8 cap)
94 pr_debug("%s\n", __func__);
96 if (addr->addr_type != IEEE802154_ADDR_LONG) {
97 pr_err("%s: received non-long source address!\n", __func__);
101 msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_INDIC);
105 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
106 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
107 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
110 NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
113 NLA_PUT_U8(msg, IEEE802154_ATTR_CAPABILITY, cap);
115 return ieee802154_nl_finish(msg);
121 EXPORT_SYMBOL(ieee802154_nl_assoc_indic);
123 int ieee802154_nl_assoc_confirm(struct net_device *dev, u16 short_addr,
128 pr_debug("%s\n", __func__);
130 msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_CONF);
134 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
135 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
136 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
139 NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr);
140 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
142 return ieee802154_nl_finish(msg);
148 EXPORT_SYMBOL(ieee802154_nl_assoc_confirm);
150 int ieee802154_nl_disassoc_indic(struct net_device *dev,
151 struct ieee802154_addr *addr, u8 reason)
155 pr_debug("%s\n", __func__);
157 msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_INDIC);
161 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
162 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
163 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
166 if (addr->addr_type == IEEE802154_ADDR_LONG)
167 NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
170 NLA_PUT_U16(msg, IEEE802154_ATTR_SRC_SHORT_ADDR,
173 NLA_PUT_U8(msg, IEEE802154_ATTR_REASON, reason);
175 return ieee802154_nl_finish(msg);
181 EXPORT_SYMBOL(ieee802154_nl_disassoc_indic);
183 int ieee802154_nl_disassoc_confirm(struct net_device *dev, u8 status)
187 pr_debug("%s\n", __func__);
189 msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_CONF);
193 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
194 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
195 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
198 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
200 return ieee802154_nl_finish(msg);
206 EXPORT_SYMBOL(ieee802154_nl_disassoc_confirm);
208 int ieee802154_nl_beacon_indic(struct net_device *dev,
209 u16 panid, u16 coord_addr)
213 pr_debug("%s\n", __func__);
215 msg = ieee802154_nl_create(0, IEEE802154_BEACON_NOTIFY_INDIC);
219 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
220 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
221 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
223 NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_SHORT_ADDR, coord_addr);
224 NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_PAN_ID, panid);
226 return ieee802154_nl_finish(msg);
232 EXPORT_SYMBOL(ieee802154_nl_beacon_indic);
234 int ieee802154_nl_scan_confirm(struct net_device *dev,
235 u8 status, u8 scan_type, u32 unscanned, u8 page,
236 u8 *edl/* , struct list_head *pan_desc_list */)
240 pr_debug("%s\n", __func__);
242 msg = ieee802154_nl_create(0, IEEE802154_SCAN_CONF);
246 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
247 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
248 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
251 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
252 NLA_PUT_U8(msg, IEEE802154_ATTR_SCAN_TYPE, scan_type);
253 NLA_PUT_U32(msg, IEEE802154_ATTR_CHANNELS, unscanned);
254 NLA_PUT_U8(msg, IEEE802154_ATTR_PAGE, page);
257 NLA_PUT(msg, IEEE802154_ATTR_ED_LIST, 27, edl);
259 return ieee802154_nl_finish(msg);
265 EXPORT_SYMBOL(ieee802154_nl_scan_confirm);
267 int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
271 pr_debug("%s\n", __func__);
273 msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
277 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
278 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
279 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
282 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
284 return ieee802154_nl_finish(msg);
290 EXPORT_SYMBOL(ieee802154_nl_start_confirm);
292 static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 pid,
293 u32 seq, int flags, struct net_device *dev)
297 pr_debug("%s\n", __func__);
299 hdr = genlmsg_put(msg, 0, seq, &ieee802154_coordinator_family, flags,
300 IEEE802154_LIST_IFACE);
304 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
305 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
307 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
309 NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR,
310 ieee802154_mlme_ops(dev)->get_short_addr(dev));
311 NLA_PUT_U16(msg, IEEE802154_ATTR_PAN_ID,
312 ieee802154_mlme_ops(dev)->get_pan_id(dev));
313 return genlmsg_end(msg, hdr);
316 genlmsg_cancel(msg, hdr);
321 /* Requests from userspace */
322 static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
324 struct net_device *dev;
326 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
327 char name[IFNAMSIZ + 1];
328 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
330 dev = dev_get_by_name(&init_net, name);
331 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX])
332 dev = dev_get_by_index(&init_net,
333 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
340 if (dev->type != ARPHRD_IEEE802154) {
348 static int ieee802154_associate_req(struct sk_buff *skb,
349 struct genl_info *info)
351 struct net_device *dev;
352 struct ieee802154_addr addr;
356 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
357 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
358 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
359 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
360 !info->attrs[IEEE802154_ATTR_CAPABILITY])
363 dev = ieee802154_nl_get_dev(info);
367 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
368 addr.addr_type = IEEE802154_ADDR_LONG;
369 nla_memcpy(addr.hwaddr,
370 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR],
371 IEEE802154_ADDR_LEN);
373 addr.addr_type = IEEE802154_ADDR_SHORT;
374 addr.short_addr = nla_get_u16(
375 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
377 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
379 if (info->attrs[IEEE802154_ATTR_PAGE])
380 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
384 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
385 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
387 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
393 static int ieee802154_associate_resp(struct sk_buff *skb,
394 struct genl_info *info)
396 struct net_device *dev;
397 struct ieee802154_addr addr;
400 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
401 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
402 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
405 dev = ieee802154_nl_get_dev(info);
409 addr.addr_type = IEEE802154_ADDR_LONG;
410 nla_memcpy(addr.hwaddr, info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
411 IEEE802154_ADDR_LEN);
412 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
415 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
416 nla_get_u16(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
417 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
423 static int ieee802154_disassociate_req(struct sk_buff *skb,
424 struct genl_info *info)
426 struct net_device *dev;
427 struct ieee802154_addr addr;
430 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
431 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
432 !info->attrs[IEEE802154_ATTR_REASON])
435 dev = ieee802154_nl_get_dev(info);
439 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
440 addr.addr_type = IEEE802154_ADDR_LONG;
441 nla_memcpy(addr.hwaddr,
442 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
443 IEEE802154_ADDR_LEN);
445 addr.addr_type = IEEE802154_ADDR_SHORT;
446 addr.short_addr = nla_get_u16(
447 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
449 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
451 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
452 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
459 * PANid, channel, beacon_order = 15, superframe_order = 15,
460 * PAN_coordinator, battery_life_extension = 0,
461 * coord_realignment = 0, security_enable = 0
463 static int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
465 struct net_device *dev;
466 struct ieee802154_addr addr;
468 u8 channel, bcn_ord, sf_ord;
470 int pan_coord, blx, coord_realign;
473 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
474 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
475 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
476 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
477 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
478 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
479 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
480 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
484 dev = ieee802154_nl_get_dev(info);
488 addr.addr_type = IEEE802154_ADDR_SHORT;
489 addr.short_addr = nla_get_u16(
490 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
491 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
493 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
494 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
495 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
496 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
497 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
498 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
500 if (info->attrs[IEEE802154_ATTR_PAGE])
501 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
506 if (addr.short_addr == IEEE802154_ADDR_BROADCAST) {
507 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
512 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
513 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
519 static int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
521 struct net_device *dev;
528 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
529 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
530 !info->attrs[IEEE802154_ATTR_DURATION])
533 dev = ieee802154_nl_get_dev(info);
537 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
538 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
539 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
541 if (info->attrs[IEEE802154_ATTR_PAGE])
542 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
547 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels, page,
554 static int ieee802154_list_iface(struct sk_buff *skb,
555 struct genl_info *info)
557 /* Request for interface name, index, type, IEEE address,
558 PAN Id, short address */
560 struct net_device *dev = NULL;
563 pr_debug("%s\n", __func__);
565 dev = ieee802154_nl_get_dev(info);
569 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
573 rc = ieee802154_nl_fill_iface(msg, info->snd_pid, info->snd_seq,
580 return genlmsg_unicast(&init_net, msg, info->snd_pid);
589 static int ieee802154_dump_iface(struct sk_buff *skb,
590 struct netlink_callback *cb)
592 struct net *net = sock_net(skb->sk);
593 struct net_device *dev;
595 int s_idx = cb->args[0];
597 pr_debug("%s\n", __func__);
600 for_each_netdev(net, dev) {
601 if (idx < s_idx || (dev->type != ARPHRD_IEEE802154))
604 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).pid,
605 cb->nlh->nlmsg_seq, NLM_F_MULTI, dev) < 0)
615 #define IEEE802154_OP(_cmd, _func) \
618 .policy = ieee802154_policy, \
621 .flags = GENL_ADMIN_PERM, \
624 #define IEEE802154_DUMP(_cmd, _func, _dump) \
627 .policy = ieee802154_policy, \
632 static struct genl_ops ieee802154_coordinator_ops[] = {
633 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
634 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
635 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
636 IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
637 IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
638 IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
639 ieee802154_dump_iface),
642 static int __init ieee802154_nl_init(void)
647 rc = genl_register_family(&ieee802154_coordinator_family);
651 rc = genl_register_mc_group(&ieee802154_coordinator_family,
652 &ieee802154_coord_mcgrp);
656 rc = genl_register_mc_group(&ieee802154_coordinator_family,
657 &ieee802154_beacon_mcgrp);
662 for (i = 0; i < ARRAY_SIZE(ieee802154_coordinator_ops); i++) {
663 rc = genl_register_ops(&ieee802154_coordinator_family,
664 &ieee802154_coordinator_ops[i]);
672 genl_unregister_family(&ieee802154_coordinator_family);
675 module_init(ieee802154_nl_init);
677 static void __exit ieee802154_nl_exit(void)
679 genl_unregister_family(&ieee802154_coordinator_family);
681 module_exit(ieee802154_nl_exit);
683 MODULE_LICENSE("GPL v2");
684 MODULE_DESCRIPTION("ieee 802.15.4 configuration interface");