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,
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);
256 NLA_PUT(msg, IEEE802154_ATTR_ED_LIST, 27, edl);
258 return ieee802154_nl_finish(msg);
264 EXPORT_SYMBOL(ieee802154_nl_scan_confirm);
266 int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
270 pr_debug("%s\n", __func__);
272 msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
276 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
277 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
278 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
281 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
283 return ieee802154_nl_finish(msg);
289 EXPORT_SYMBOL(ieee802154_nl_start_confirm);
291 static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 pid,
292 u32 seq, int flags, struct net_device *dev)
296 pr_debug("%s\n", __func__);
298 hdr = genlmsg_put(msg, 0, seq, &ieee802154_coordinator_family, flags,
299 IEEE802154_LIST_IFACE);
303 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
304 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
306 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
308 NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR,
309 ieee802154_mlme_ops(dev)->get_short_addr(dev));
310 NLA_PUT_U16(msg, IEEE802154_ATTR_PAN_ID,
311 ieee802154_mlme_ops(dev)->get_pan_id(dev));
312 return genlmsg_end(msg, hdr);
315 genlmsg_cancel(msg, hdr);
320 /* Requests from userspace */
321 static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
323 struct net_device *dev;
325 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
326 char name[IFNAMSIZ + 1];
327 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
329 dev = dev_get_by_name(&init_net, name);
330 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX])
331 dev = dev_get_by_index(&init_net,
332 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
339 if (dev->type != ARPHRD_IEEE802154) {
347 static int ieee802154_associate_req(struct sk_buff *skb,
348 struct genl_info *info)
350 struct net_device *dev;
351 struct ieee802154_addr addr;
354 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
355 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
356 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
357 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
358 !info->attrs[IEEE802154_ATTR_CAPABILITY])
361 dev = ieee802154_nl_get_dev(info);
365 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
366 addr.addr_type = IEEE802154_ADDR_LONG;
367 nla_memcpy(addr.hwaddr,
368 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR],
369 IEEE802154_ADDR_LEN);
371 addr.addr_type = IEEE802154_ADDR_SHORT;
372 addr.short_addr = nla_get_u16(
373 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
375 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
377 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
378 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
379 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
385 static int ieee802154_associate_resp(struct sk_buff *skb,
386 struct genl_info *info)
388 struct net_device *dev;
389 struct ieee802154_addr addr;
392 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
393 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
394 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
397 dev = ieee802154_nl_get_dev(info);
401 addr.addr_type = IEEE802154_ADDR_LONG;
402 nla_memcpy(addr.hwaddr, info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
403 IEEE802154_ADDR_LEN);
404 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
407 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
408 nla_get_u16(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
409 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
415 static int ieee802154_disassociate_req(struct sk_buff *skb,
416 struct genl_info *info)
418 struct net_device *dev;
419 struct ieee802154_addr addr;
422 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
423 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
424 !info->attrs[IEEE802154_ATTR_REASON])
427 dev = ieee802154_nl_get_dev(info);
431 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
432 addr.addr_type = IEEE802154_ADDR_LONG;
433 nla_memcpy(addr.hwaddr,
434 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
435 IEEE802154_ADDR_LEN);
437 addr.addr_type = IEEE802154_ADDR_SHORT;
438 addr.short_addr = nla_get_u16(
439 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
441 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
443 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
444 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
451 * PANid, channel, beacon_order = 15, superframe_order = 15,
452 * PAN_coordinator, battery_life_extension = 0,
453 * coord_realignment = 0, security_enable = 0
455 static int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
457 struct net_device *dev;
458 struct ieee802154_addr addr;
460 u8 channel, bcn_ord, sf_ord;
461 int pan_coord, blx, coord_realign;
464 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
465 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
466 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
467 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
468 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
469 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
470 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
471 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
475 dev = ieee802154_nl_get_dev(info);
479 addr.addr_type = IEEE802154_ADDR_SHORT;
480 addr.short_addr = nla_get_u16(
481 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
482 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
484 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
485 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
486 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
487 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
488 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
489 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
491 if (addr.short_addr == IEEE802154_ADDR_BROADCAST) {
492 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
497 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel,
498 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
504 static int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
506 struct net_device *dev;
512 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
513 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
514 !info->attrs[IEEE802154_ATTR_DURATION])
517 dev = ieee802154_nl_get_dev(info);
521 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
522 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
523 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
525 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
532 static int ieee802154_list_iface(struct sk_buff *skb,
533 struct genl_info *info)
535 /* Request for interface name, index, type, IEEE address,
536 PAN Id, short address */
538 struct net_device *dev = NULL;
541 pr_debug("%s\n", __func__);
543 dev = ieee802154_nl_get_dev(info);
547 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
551 rc = ieee802154_nl_fill_iface(msg, info->snd_pid, info->snd_seq,
558 return genlmsg_unicast(&init_net, msg, info->snd_pid);
567 static int ieee802154_dump_iface(struct sk_buff *skb,
568 struct netlink_callback *cb)
570 struct net *net = sock_net(skb->sk);
571 struct net_device *dev;
573 int s_idx = cb->args[0];
575 pr_debug("%s\n", __func__);
578 for_each_netdev(net, dev) {
579 if (idx < s_idx || (dev->type != ARPHRD_IEEE802154))
582 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).pid,
583 cb->nlh->nlmsg_seq, NLM_F_MULTI, dev) < 0)
593 #define IEEE802154_OP(_cmd, _func) \
596 .policy = ieee802154_policy, \
599 .flags = GENL_ADMIN_PERM, \
602 #define IEEE802154_DUMP(_cmd, _func, _dump) \
605 .policy = ieee802154_policy, \
610 static struct genl_ops ieee802154_coordinator_ops[] = {
611 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
612 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
613 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
614 IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
615 IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
616 IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
617 ieee802154_dump_iface),
620 static int __init ieee802154_nl_init(void)
625 rc = genl_register_family(&ieee802154_coordinator_family);
629 rc = genl_register_mc_group(&ieee802154_coordinator_family,
630 &ieee802154_coord_mcgrp);
634 rc = genl_register_mc_group(&ieee802154_coordinator_family,
635 &ieee802154_beacon_mcgrp);
640 for (i = 0; i < ARRAY_SIZE(ieee802154_coordinator_ops); i++) {
641 rc = genl_register_ops(&ieee802154_coordinator_family,
642 &ieee802154_coordinator_ops[i]);
650 genl_unregister_family(&ieee802154_coordinator_family);
653 module_init(ieee802154_nl_init);
655 static void __exit ieee802154_nl_exit(void)
657 genl_unregister_family(&ieee802154_coordinator_family);
659 module_exit(ieee802154_nl_exit);
661 MODULE_LICENSE("GPL v2");
662 MODULE_DESCRIPTION("ieee 802.15.4 configuration interface");