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_netdev.h>
36 static unsigned int ieee802154_seq_num;
38 static struct genl_family ieee802154_coordinator_family = {
39 .id = GENL_ID_GENERATE,
41 .name = IEEE802154_NL_NAME,
43 .maxattr = IEEE802154_ATTR_MAX,
46 static struct genl_multicast_group ieee802154_coord_mcgrp = {
47 .name = IEEE802154_MCAST_COORD_NAME,
50 static struct genl_multicast_group ieee802154_beacon_mcgrp = {
51 .name = IEEE802154_MCAST_BEACON_NAME,
54 /* Requests to userspace */
55 static struct sk_buff *ieee802154_nl_create(int flags, u8 req)
58 struct sk_buff *msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
63 hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
64 &ieee802154_coordinator_family, flags, req);
73 static int ieee802154_nl_finish(struct sk_buff *msg)
75 /* XXX: nlh is right at the start of msg */
76 void *hdr = genlmsg_data(NLMSG_DATA(msg->data));
78 if (genlmsg_end(msg, hdr) < 0)
81 return genlmsg_multicast(msg, 0, ieee802154_coord_mcgrp.id,
88 int ieee802154_nl_assoc_indic(struct net_device *dev,
89 struct ieee802154_addr *addr, u8 cap)
93 pr_debug("%s\n", __func__);
95 if (addr->addr_type != IEEE802154_ADDR_LONG) {
96 pr_err("%s: received non-long source address!\n", __func__);
100 msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_INDIC);
104 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
105 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
106 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
109 NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
112 NLA_PUT_U8(msg, IEEE802154_ATTR_CAPABILITY, cap);
114 return ieee802154_nl_finish(msg);
120 EXPORT_SYMBOL(ieee802154_nl_assoc_indic);
122 int ieee802154_nl_assoc_confirm(struct net_device *dev, u16 short_addr,
127 pr_debug("%s\n", __func__);
129 msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_CONF);
133 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
134 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
135 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
138 NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr);
139 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
141 return ieee802154_nl_finish(msg);
147 EXPORT_SYMBOL(ieee802154_nl_assoc_confirm);
149 int ieee802154_nl_disassoc_indic(struct net_device *dev,
150 struct ieee802154_addr *addr, u8 reason)
154 pr_debug("%s\n", __func__);
156 msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_INDIC);
160 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
161 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
162 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
165 if (addr->addr_type == IEEE802154_ADDR_LONG)
166 NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
169 NLA_PUT_U16(msg, IEEE802154_ATTR_SRC_SHORT_ADDR,
172 NLA_PUT_U8(msg, IEEE802154_ATTR_REASON, reason);
174 return ieee802154_nl_finish(msg);
180 EXPORT_SYMBOL(ieee802154_nl_disassoc_indic);
182 int ieee802154_nl_disassoc_confirm(struct net_device *dev, u8 status)
186 pr_debug("%s\n", __func__);
188 msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_CONF);
192 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
193 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
194 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
197 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
199 return ieee802154_nl_finish(msg);
205 EXPORT_SYMBOL(ieee802154_nl_disassoc_confirm);
207 int ieee802154_nl_beacon_indic(struct net_device *dev,
208 u16 panid, u16 coord_addr)
212 pr_debug("%s\n", __func__);
214 msg = ieee802154_nl_create(0, IEEE802154_BEACON_NOTIFY_INDIC);
218 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
219 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
220 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
222 NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_SHORT_ADDR, coord_addr);
223 NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_PAN_ID, panid);
225 return ieee802154_nl_finish(msg);
231 EXPORT_SYMBOL(ieee802154_nl_beacon_indic);
233 int ieee802154_nl_scan_confirm(struct net_device *dev,
234 u8 status, u8 scan_type, u32 unscanned,
235 u8 *edl/* , struct list_head *pan_desc_list */)
239 pr_debug("%s\n", __func__);
241 msg = ieee802154_nl_create(0, IEEE802154_SCAN_CONF);
245 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
246 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
247 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
250 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
251 NLA_PUT_U8(msg, IEEE802154_ATTR_SCAN_TYPE, scan_type);
252 NLA_PUT_U32(msg, IEEE802154_ATTR_CHANNELS, unscanned);
255 NLA_PUT(msg, IEEE802154_ATTR_ED_LIST, 27, edl);
257 return ieee802154_nl_finish(msg);
263 EXPORT_SYMBOL(ieee802154_nl_scan_confirm);
265 static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 pid,
266 u32 seq, int flags, struct net_device *dev)
270 pr_debug("%s\n", __func__);
272 hdr = genlmsg_put(msg, 0, seq, &ieee802154_coordinator_family, flags,
273 IEEE802154_LIST_IFACE);
277 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
278 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
280 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
282 NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR,
283 ieee802154_mlme_ops(dev)->get_short_addr(dev));
284 NLA_PUT_U16(msg, IEEE802154_ATTR_PAN_ID,
285 ieee802154_mlme_ops(dev)->get_pan_id(dev));
286 return genlmsg_end(msg, hdr);
289 genlmsg_cancel(msg, hdr);
294 /* Requests from userspace */
295 static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
297 struct net_device *dev;
299 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
300 char name[IFNAMSIZ + 1];
301 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
303 dev = dev_get_by_name(&init_net, name);
304 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX])
305 dev = dev_get_by_index(&init_net,
306 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
313 if (dev->type != ARPHRD_IEEE802154) {
321 static int ieee802154_associate_req(struct sk_buff *skb,
322 struct genl_info *info)
324 struct net_device *dev;
325 struct ieee802154_addr addr;
328 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
329 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
330 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
331 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
332 !info->attrs[IEEE802154_ATTR_CAPABILITY])
335 dev = ieee802154_nl_get_dev(info);
339 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
340 addr.addr_type = IEEE802154_ADDR_LONG;
341 nla_memcpy(addr.hwaddr,
342 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR],
343 IEEE802154_ADDR_LEN);
345 addr.addr_type = IEEE802154_ADDR_SHORT;
346 addr.short_addr = nla_get_u16(
347 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
349 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
351 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
352 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;
435 int pan_coord, blx, coord_realign;
438 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
439 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
440 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
441 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
442 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
443 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
444 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
445 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
449 dev = ieee802154_nl_get_dev(info);
453 addr.addr_type = IEEE802154_ADDR_SHORT;
454 addr.short_addr = nla_get_u16(
455 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
456 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
458 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
459 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
460 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
461 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
462 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
463 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
465 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel,
466 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
472 static int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
474 struct net_device *dev;
480 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
481 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
482 !info->attrs[IEEE802154_ATTR_DURATION])
485 dev = ieee802154_nl_get_dev(info);
489 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
490 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
491 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
493 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
500 static int ieee802154_list_iface(struct sk_buff *skb,
501 struct genl_info *info)
503 /* Request for interface name, index, type, IEEE address,
504 PAN Id, short address */
506 struct net_device *dev = NULL;
509 pr_debug("%s\n", __func__);
511 dev = ieee802154_nl_get_dev(info);
515 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
519 rc = ieee802154_nl_fill_iface(msg, info->snd_pid, info->snd_seq,
526 return genlmsg_unicast(&init_net, msg, info->snd_pid);
535 static int ieee802154_dump_iface(struct sk_buff *skb,
536 struct netlink_callback *cb)
538 struct net *net = sock_net(skb->sk);
539 struct net_device *dev;
541 int s_idx = cb->args[0];
543 pr_debug("%s\n", __func__);
546 for_each_netdev(net, dev) {
547 if (idx < s_idx || (dev->type != ARPHRD_IEEE802154))
550 if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).pid,
551 cb->nlh->nlmsg_seq, NLM_F_MULTI, dev) < 0)
561 #define IEEE802154_OP(_cmd, _func) \
564 .policy = ieee802154_policy, \
567 .flags = GENL_ADMIN_PERM, \
570 #define IEEE802154_DUMP(_cmd, _func, _dump) \
573 .policy = ieee802154_policy, \
578 static struct genl_ops ieee802154_coordinator_ops[] = {
579 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
580 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
581 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
582 IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
583 IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
584 IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
585 ieee802154_dump_iface),
588 static int __init ieee802154_nl_init(void)
593 rc = genl_register_family(&ieee802154_coordinator_family);
597 rc = genl_register_mc_group(&ieee802154_coordinator_family,
598 &ieee802154_coord_mcgrp);
602 rc = genl_register_mc_group(&ieee802154_coordinator_family,
603 &ieee802154_beacon_mcgrp);
608 for (i = 0; i < ARRAY_SIZE(ieee802154_coordinator_ops); i++) {
609 rc = genl_register_ops(&ieee802154_coordinator_family,
610 &ieee802154_coordinator_ops[i]);
618 genl_unregister_family(&ieee802154_coordinator_family);
621 module_init(ieee802154_nl_init);
623 static void __exit ieee802154_nl_exit(void)
625 genl_unregister_family(&ieee802154_coordinator_family);
627 module_exit(ieee802154_nl_exit);
629 MODULE_LICENSE("GPL v2");
630 MODULE_DESCRIPTION("ieee 802.15.4 configuration interface");