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>
36 #include <net/wpan-phy.h>
38 #include "ieee802154.h"
40 static struct genl_multicast_group ieee802154_coord_mcgrp = {
41 .name = IEEE802154_MCAST_COORD_NAME,
44 static struct genl_multicast_group ieee802154_beacon_mcgrp = {
45 .name = IEEE802154_MCAST_BEACON_NAME,
48 int ieee802154_nl_assoc_indic(struct net_device *dev,
49 struct ieee802154_addr *addr, u8 cap)
53 pr_debug("%s\n", __func__);
55 if (addr->addr_type != IEEE802154_ADDR_LONG) {
56 pr_err("%s: received non-long source address!\n", __func__);
60 msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_INDIC);
64 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
65 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
66 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
69 NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
72 NLA_PUT_U8(msg, IEEE802154_ATTR_CAPABILITY, cap);
74 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
80 EXPORT_SYMBOL(ieee802154_nl_assoc_indic);
82 int ieee802154_nl_assoc_confirm(struct net_device *dev, u16 short_addr,
87 pr_debug("%s\n", __func__);
89 msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_CONF);
93 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
94 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
95 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
98 NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr);
99 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
101 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
107 EXPORT_SYMBOL(ieee802154_nl_assoc_confirm);
109 int ieee802154_nl_disassoc_indic(struct net_device *dev,
110 struct ieee802154_addr *addr, u8 reason)
114 pr_debug("%s\n", __func__);
116 msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_INDIC);
120 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
121 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
122 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
125 if (addr->addr_type == IEEE802154_ADDR_LONG)
126 NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
129 NLA_PUT_U16(msg, IEEE802154_ATTR_SRC_SHORT_ADDR,
132 NLA_PUT_U8(msg, IEEE802154_ATTR_REASON, reason);
134 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
140 EXPORT_SYMBOL(ieee802154_nl_disassoc_indic);
142 int ieee802154_nl_disassoc_confirm(struct net_device *dev, u8 status)
146 pr_debug("%s\n", __func__);
148 msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_CONF);
152 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
153 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
154 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
157 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
159 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
165 EXPORT_SYMBOL(ieee802154_nl_disassoc_confirm);
167 int ieee802154_nl_beacon_indic(struct net_device *dev,
168 u16 panid, u16 coord_addr)
172 pr_debug("%s\n", __func__);
174 msg = ieee802154_nl_create(0, IEEE802154_BEACON_NOTIFY_INDIC);
178 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
179 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
180 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
182 NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_SHORT_ADDR, coord_addr);
183 NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_PAN_ID, panid);
185 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
191 EXPORT_SYMBOL(ieee802154_nl_beacon_indic);
193 int ieee802154_nl_scan_confirm(struct net_device *dev,
194 u8 status, u8 scan_type, u32 unscanned, u8 page,
195 u8 *edl/* , struct list_head *pan_desc_list */)
199 pr_debug("%s\n", __func__);
201 msg = ieee802154_nl_create(0, IEEE802154_SCAN_CONF);
205 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
206 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
207 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
210 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
211 NLA_PUT_U8(msg, IEEE802154_ATTR_SCAN_TYPE, scan_type);
212 NLA_PUT_U32(msg, IEEE802154_ATTR_CHANNELS, unscanned);
213 NLA_PUT_U8(msg, IEEE802154_ATTR_PAGE, page);
216 NLA_PUT(msg, IEEE802154_ATTR_ED_LIST, 27, edl);
218 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
224 EXPORT_SYMBOL(ieee802154_nl_scan_confirm);
226 int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
230 pr_debug("%s\n", __func__);
232 msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
236 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
237 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
238 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
241 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
243 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
249 EXPORT_SYMBOL(ieee802154_nl_start_confirm);
251 static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 pid,
252 u32 seq, int flags, struct net_device *dev)
255 struct wpan_phy *phy;
257 pr_debug("%s\n", __func__);
259 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
260 IEEE802154_LIST_IFACE);
264 phy = ieee802154_mlme_ops(dev)->get_phy(dev);
267 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
268 NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
269 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
271 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
273 NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR,
274 ieee802154_mlme_ops(dev)->get_short_addr(dev));
275 NLA_PUT_U16(msg, IEEE802154_ATTR_PAN_ID,
276 ieee802154_mlme_ops(dev)->get_pan_id(dev));
278 return genlmsg_end(msg, hdr);
282 genlmsg_cancel(msg, hdr);
287 /* Requests from userspace */
288 static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
290 struct net_device *dev;
292 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
293 char name[IFNAMSIZ + 1];
294 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
296 dev = dev_get_by_name(&init_net, name);
297 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX])
298 dev = dev_get_by_index(&init_net,
299 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
306 if (dev->type != ARPHRD_IEEE802154) {
314 static int ieee802154_associate_req(struct sk_buff *skb,
315 struct genl_info *info)
317 struct net_device *dev;
318 struct ieee802154_addr addr;
322 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
323 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
324 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
325 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
326 !info->attrs[IEEE802154_ATTR_CAPABILITY])
329 dev = ieee802154_nl_get_dev(info);
333 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
334 addr.addr_type = IEEE802154_ADDR_LONG;
335 nla_memcpy(addr.hwaddr,
336 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR],
337 IEEE802154_ADDR_LEN);
339 addr.addr_type = IEEE802154_ADDR_SHORT;
340 addr.short_addr = nla_get_u16(
341 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
343 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
345 if (info->attrs[IEEE802154_ATTR_PAGE])
346 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
350 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
351 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
353 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
359 static int ieee802154_associate_resp(struct sk_buff *skb,
360 struct genl_info *info)
362 struct net_device *dev;
363 struct ieee802154_addr addr;
366 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
367 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
368 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
371 dev = ieee802154_nl_get_dev(info);
375 addr.addr_type = IEEE802154_ADDR_LONG;
376 nla_memcpy(addr.hwaddr, info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
377 IEEE802154_ADDR_LEN);
378 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
381 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
382 nla_get_u16(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
383 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
389 static int ieee802154_disassociate_req(struct sk_buff *skb,
390 struct genl_info *info)
392 struct net_device *dev;
393 struct ieee802154_addr addr;
396 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
397 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
398 !info->attrs[IEEE802154_ATTR_REASON])
401 dev = ieee802154_nl_get_dev(info);
405 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
406 addr.addr_type = IEEE802154_ADDR_LONG;
407 nla_memcpy(addr.hwaddr,
408 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
409 IEEE802154_ADDR_LEN);
411 addr.addr_type = IEEE802154_ADDR_SHORT;
412 addr.short_addr = nla_get_u16(
413 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
415 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
417 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
418 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
425 * PANid, channel, beacon_order = 15, superframe_order = 15,
426 * PAN_coordinator, battery_life_extension = 0,
427 * coord_realignment = 0, security_enable = 0
429 static int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
431 struct net_device *dev;
432 struct ieee802154_addr addr;
434 u8 channel, bcn_ord, sf_ord;
436 int pan_coord, blx, coord_realign;
439 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
440 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
441 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
442 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
443 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
444 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
445 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
446 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
450 dev = ieee802154_nl_get_dev(info);
454 addr.addr_type = IEEE802154_ADDR_SHORT;
455 addr.short_addr = nla_get_u16(
456 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
457 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
459 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
460 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
461 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
462 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
463 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
464 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
466 if (info->attrs[IEEE802154_ATTR_PAGE])
467 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
472 if (addr.short_addr == IEEE802154_ADDR_BROADCAST) {
473 ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
478 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
479 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
485 static int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
487 struct net_device *dev;
494 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
495 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
496 !info->attrs[IEEE802154_ATTR_DURATION])
499 dev = ieee802154_nl_get_dev(info);
503 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
504 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
505 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
507 if (info->attrs[IEEE802154_ATTR_PAGE])
508 page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
513 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels, page,
520 static int ieee802154_list_iface(struct sk_buff *skb,
521 struct genl_info *info)
523 /* Request for interface name, index, type, IEEE address,
524 PAN Id, short address */
526 struct net_device *dev = NULL;
529 pr_debug("%s\n", __func__);
531 dev = ieee802154_nl_get_dev(info);
535 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
539 rc = ieee802154_nl_fill_iface(msg, info->snd_pid, info->snd_seq,
546 return genlmsg_reply(msg, info);
555 static int ieee802154_dump_iface(struct sk_buff *skb,
556 struct netlink_callback *cb)
558 struct net *net = sock_net(skb->sk);
559 struct net_device *dev;
561 int s_idx = cb->args[0];
563 pr_debug("%s\n", __func__);
566 for_each_netdev(net, dev) {
567 if (idx < s_idx || (dev->type != ARPHRD_IEEE802154))
570 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).pid,
571 cb->nlh->nlmsg_seq, NLM_F_MULTI, dev) < 0)
581 static struct genl_ops ieee802154_coordinator_ops[] = {
582 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
583 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
584 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
585 IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
586 IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
587 IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
588 ieee802154_dump_iface),
592 * No need to unregister as family unregistration will do it.
594 int nl802154_mac_register(void)
599 rc = genl_register_mc_group(&nl802154_family,
600 &ieee802154_coord_mcgrp);
604 rc = genl_register_mc_group(&nl802154_family,
605 &ieee802154_beacon_mcgrp);
609 for (i = 0; i < ARRAY_SIZE(ieee802154_coordinator_ops); i++) {
610 rc = genl_register_ops(&nl802154_family,
611 &ieee802154_coordinator_ops[i]);