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 #include "ieee802154.h"
39 static struct genl_multicast_group ieee802154_coord_mcgrp = {
40 .name = IEEE802154_MCAST_COORD_NAME,
43 static struct genl_multicast_group ieee802154_beacon_mcgrp = {
44 .name = IEEE802154_MCAST_BEACON_NAME,
47 int ieee802154_nl_assoc_indic(struct net_device *dev,
48 struct ieee802154_addr *addr, u8 cap)
52 pr_debug("%s\n", __func__);
54 if (addr->addr_type != IEEE802154_ADDR_LONG) {
55 pr_err("%s: received non-long source address!\n", __func__);
59 msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_INDIC);
63 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
64 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
65 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
68 NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
71 NLA_PUT_U8(msg, IEEE802154_ATTR_CAPABILITY, cap);
73 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
79 EXPORT_SYMBOL(ieee802154_nl_assoc_indic);
81 int ieee802154_nl_assoc_confirm(struct net_device *dev, u16 short_addr,
86 pr_debug("%s\n", __func__);
88 msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_CONF);
92 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
93 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
94 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
97 NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr);
98 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
100 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
106 EXPORT_SYMBOL(ieee802154_nl_assoc_confirm);
108 int ieee802154_nl_disassoc_indic(struct net_device *dev,
109 struct ieee802154_addr *addr, u8 reason)
113 pr_debug("%s\n", __func__);
115 msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_INDIC);
119 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
120 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
121 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
124 if (addr->addr_type == IEEE802154_ADDR_LONG)
125 NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
128 NLA_PUT_U16(msg, IEEE802154_ATTR_SRC_SHORT_ADDR,
131 NLA_PUT_U8(msg, IEEE802154_ATTR_REASON, reason);
133 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
139 EXPORT_SYMBOL(ieee802154_nl_disassoc_indic);
141 int ieee802154_nl_disassoc_confirm(struct net_device *dev, u8 status)
145 pr_debug("%s\n", __func__);
147 msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_CONF);
151 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
152 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
153 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
156 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
158 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
164 EXPORT_SYMBOL(ieee802154_nl_disassoc_confirm);
166 int ieee802154_nl_beacon_indic(struct net_device *dev,
167 u16 panid, u16 coord_addr)
171 pr_debug("%s\n", __func__);
173 msg = ieee802154_nl_create(0, IEEE802154_BEACON_NOTIFY_INDIC);
177 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
178 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
179 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
181 NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_SHORT_ADDR, coord_addr);
182 NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_PAN_ID, panid);
184 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
190 EXPORT_SYMBOL(ieee802154_nl_beacon_indic);
192 int ieee802154_nl_scan_confirm(struct net_device *dev,
193 u8 status, u8 scan_type, u32 unscanned, u8 page,
194 u8 *edl/* , struct list_head *pan_desc_list */)
198 pr_debug("%s\n", __func__);
200 msg = ieee802154_nl_create(0, IEEE802154_SCAN_CONF);
204 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
205 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
206 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
209 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
210 NLA_PUT_U8(msg, IEEE802154_ATTR_SCAN_TYPE, scan_type);
211 NLA_PUT_U32(msg, IEEE802154_ATTR_CHANNELS, unscanned);
212 NLA_PUT_U8(msg, IEEE802154_ATTR_PAGE, page);
215 NLA_PUT(msg, IEEE802154_ATTR_ED_LIST, 27, edl);
217 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
223 EXPORT_SYMBOL(ieee802154_nl_scan_confirm);
225 int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
229 pr_debug("%s\n", __func__);
231 msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
235 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
236 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
237 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
240 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
242 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
248 EXPORT_SYMBOL(ieee802154_nl_start_confirm);
250 static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 pid,
251 u32 seq, int flags, struct net_device *dev)
255 pr_debug("%s\n", __func__);
257 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
258 IEEE802154_LIST_IFACE);
262 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
263 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
265 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
267 NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR,
268 ieee802154_mlme_ops(dev)->get_short_addr(dev));
269 NLA_PUT_U16(msg, IEEE802154_ATTR_PAN_ID,
270 ieee802154_mlme_ops(dev)->get_pan_id(dev));
271 return genlmsg_end(msg, hdr);
274 genlmsg_cancel(msg, hdr);
279 /* Requests from userspace */
280 static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
282 struct net_device *dev;
284 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
285 char name[IFNAMSIZ + 1];
286 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
288 dev = dev_get_by_name(&init_net, name);
289 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX])
290 dev = dev_get_by_index(&init_net,
291 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
298 if (dev->type != ARPHRD_IEEE802154) {
306 static int ieee802154_associate_req(struct sk_buff *skb,
307 struct genl_info *info)
309 struct net_device *dev;
310 struct ieee802154_addr addr;
314 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
315 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
316 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
317 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
318 !info->attrs[IEEE802154_ATTR_CAPABILITY])
321 dev = ieee802154_nl_get_dev(info);
325 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
326 addr.addr_type = IEEE802154_ADDR_LONG;
327 nla_memcpy(addr.hwaddr,
328 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR],
329 IEEE802154_ADDR_LEN);
331 addr.addr_type = IEEE802154_ADDR_SHORT;
332 addr.short_addr = nla_get_u16(
333 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
335 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
337 if (info->attrs[IEEE802154_ATTR_PAGE])
338 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
342 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
343 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
345 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
351 static int ieee802154_associate_resp(struct sk_buff *skb,
352 struct genl_info *info)
354 struct net_device *dev;
355 struct ieee802154_addr addr;
358 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
359 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
360 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
363 dev = ieee802154_nl_get_dev(info);
367 addr.addr_type = IEEE802154_ADDR_LONG;
368 nla_memcpy(addr.hwaddr, info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
369 IEEE802154_ADDR_LEN);
370 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
373 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
374 nla_get_u16(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
375 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
381 static int ieee802154_disassociate_req(struct sk_buff *skb,
382 struct genl_info *info)
384 struct net_device *dev;
385 struct ieee802154_addr addr;
388 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
389 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
390 !info->attrs[IEEE802154_ATTR_REASON])
393 dev = ieee802154_nl_get_dev(info);
397 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
398 addr.addr_type = IEEE802154_ADDR_LONG;
399 nla_memcpy(addr.hwaddr,
400 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
401 IEEE802154_ADDR_LEN);
403 addr.addr_type = IEEE802154_ADDR_SHORT;
404 addr.short_addr = nla_get_u16(
405 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
407 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
409 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
410 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
417 * PANid, channel, beacon_order = 15, superframe_order = 15,
418 * PAN_coordinator, battery_life_extension = 0,
419 * coord_realignment = 0, security_enable = 0
421 static int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
423 struct net_device *dev;
424 struct ieee802154_addr addr;
426 u8 channel, bcn_ord, sf_ord;
428 int pan_coord, blx, coord_realign;
431 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
432 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
433 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
434 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
435 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
436 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
437 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
438 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
442 dev = ieee802154_nl_get_dev(info);
446 addr.addr_type = IEEE802154_ADDR_SHORT;
447 addr.short_addr = nla_get_u16(
448 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
449 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
451 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
452 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
453 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
454 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
455 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
456 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
458 if (info->attrs[IEEE802154_ATTR_PAGE])
459 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
464 if (addr.short_addr == IEEE802154_ADDR_BROADCAST) {
465 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
470 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
471 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
477 static int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
479 struct net_device *dev;
486 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
487 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
488 !info->attrs[IEEE802154_ATTR_DURATION])
491 dev = ieee802154_nl_get_dev(info);
495 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
496 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
497 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
499 if (info->attrs[IEEE802154_ATTR_PAGE])
500 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
505 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels, page,
512 static int ieee802154_list_iface(struct sk_buff *skb,
513 struct genl_info *info)
515 /* Request for interface name, index, type, IEEE address,
516 PAN Id, short address */
518 struct net_device *dev = NULL;
521 pr_debug("%s\n", __func__);
523 dev = ieee802154_nl_get_dev(info);
527 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
531 rc = ieee802154_nl_fill_iface(msg, info->snd_pid, info->snd_seq,
538 return genlmsg_reply(msg, info);
547 static int ieee802154_dump_iface(struct sk_buff *skb,
548 struct netlink_callback *cb)
550 struct net *net = sock_net(skb->sk);
551 struct net_device *dev;
553 int s_idx = cb->args[0];
555 pr_debug("%s\n", __func__);
558 for_each_netdev(net, dev) {
559 if (idx < s_idx || (dev->type != ARPHRD_IEEE802154))
562 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).pid,
563 cb->nlh->nlmsg_seq, NLM_F_MULTI, dev) < 0)
573 static struct genl_ops ieee802154_coordinator_ops[] = {
574 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
575 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
576 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
577 IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
578 IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
579 IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
580 ieee802154_dump_iface),
584 * No need to unregister as family unregistration will do it.
586 int nl802154_mac_register(void)
591 rc = genl_register_mc_group(&nl802154_family,
592 &ieee802154_coord_mcgrp);
596 rc = genl_register_mc_group(&nl802154_family,
597 &ieee802154_beacon_mcgrp);
601 for (i = 0; i < ARRAY_SIZE(ieee802154_coordinator_ops); i++) {
602 rc = genl_register_ops(&nl802154_family,
603 &ieee802154_coordinator_ops[i]);