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>
24 #include <linux/kernel.h>
25 #include <linux/if_arp.h>
26 #include <linux/netdevice.h>
27 #include <net/netlink.h>
28 #include <net/genetlink.h>
29 #include <linux/nl802154.h>
30 #include <net/ieee802154/af_ieee802154.h>
31 #include <net/ieee802154/nl802154.h>
32 #include <net/ieee802154/netdevice.h>
34 static unsigned int ieee802154_seq_num;
36 static struct genl_family ieee802154_coordinator_family = {
37 .id = GENL_ID_GENERATE,
39 .name = IEEE802154_NL_NAME,
41 .maxattr = IEEE802154_ATTR_MAX,
44 static struct genl_multicast_group ieee802154_coord_mcgrp = {
45 .name = IEEE802154_MCAST_COORD_NAME,
48 static struct genl_multicast_group ieee802154_beacon_mcgrp = {
49 .name = IEEE802154_MCAST_BEACON_NAME,
52 /* Requests to userspace */
53 static struct sk_buff *ieee802154_nl_create(int flags, u8 req)
56 struct sk_buff *msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
61 hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
62 &ieee802154_coordinator_family, flags, req);
71 static int ieee802154_nl_finish(struct sk_buff *msg)
73 /* XXX: nlh is right at the start of msg */
74 void *hdr = genlmsg_data(NLMSG_DATA(msg->data));
76 if (!genlmsg_end(msg, hdr))
79 return genlmsg_multicast(msg, 0, ieee802154_coord_mcgrp.id,
86 int ieee802154_nl_assoc_indic(struct net_device *dev,
87 struct ieee802154_addr *addr, u8 cap)
91 pr_debug("%s\n", __func__);
93 if (addr->addr_type != IEEE802154_ADDR_LONG) {
94 pr_err("%s: received non-long source address!\n", __func__);
98 msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_INDIC);
102 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
103 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
104 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
107 NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
110 NLA_PUT_U8(msg, IEEE802154_ATTR_CAPABILITY, cap);
112 return ieee802154_nl_finish(msg);
118 EXPORT_SYMBOL(ieee802154_nl_assoc_indic);
120 int ieee802154_nl_assoc_confirm(struct net_device *dev, u16 short_addr,
125 pr_debug("%s\n", __func__);
127 msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_CONF);
131 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
132 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
133 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
136 NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr);
137 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
139 return ieee802154_nl_finish(msg);
145 EXPORT_SYMBOL(ieee802154_nl_assoc_confirm);
147 int ieee802154_nl_disassoc_indic(struct net_device *dev,
148 struct ieee802154_addr *addr, u8 reason)
152 pr_debug("%s\n", __func__);
154 msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_INDIC);
158 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
159 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
160 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
163 if (addr->addr_type == IEEE802154_ADDR_LONG)
164 NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
167 NLA_PUT_U16(msg, IEEE802154_ATTR_SRC_SHORT_ADDR,
170 NLA_PUT_U8(msg, IEEE802154_ATTR_REASON, reason);
172 return ieee802154_nl_finish(msg);
178 EXPORT_SYMBOL(ieee802154_nl_disassoc_indic);
180 int ieee802154_nl_disassoc_confirm(struct net_device *dev, u8 status)
184 pr_debug("%s\n", __func__);
186 msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_CONF);
190 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
191 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
192 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
195 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
197 return ieee802154_nl_finish(msg);
203 EXPORT_SYMBOL(ieee802154_nl_disassoc_confirm);
205 int ieee802154_nl_beacon_indic(struct net_device *dev,
206 u16 panid, u16 coord_addr)
210 pr_debug("%s\n", __func__);
212 msg = ieee802154_nl_create(0, IEEE802154_BEACON_NOTIFY_INDIC);
216 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
217 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
218 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
220 NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_SHORT_ADDR, coord_addr);
221 NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_PAN_ID, panid);
223 return ieee802154_nl_finish(msg);
229 EXPORT_SYMBOL(ieee802154_nl_beacon_indic);
231 int ieee802154_nl_scan_confirm(struct net_device *dev,
232 u8 status, u8 scan_type, u32 unscanned,
233 u8 *edl/* , struct list_head *pan_desc_list */)
237 pr_debug("%s\n", __func__);
239 msg = ieee802154_nl_create(0, IEEE802154_SCAN_CONF);
243 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
244 NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
245 NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
248 NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
249 NLA_PUT_U8(msg, IEEE802154_ATTR_SCAN_TYPE, scan_type);
250 NLA_PUT_U32(msg, IEEE802154_ATTR_CHANNELS, unscanned);
253 NLA_PUT(msg, IEEE802154_ATTR_ED_LIST, 27, edl);
255 return ieee802154_nl_finish(msg);
261 EXPORT_SYMBOL(ieee802154_nl_scan_confirm);
263 /* Requests from userspace */
264 static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
266 struct net_device *dev;
268 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
269 char name[IFNAMSIZ + 1];
270 nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
272 dev = dev_get_by_name(&init_net, name);
273 } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX])
274 dev = dev_get_by_index(&init_net,
275 nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
282 if (dev->type != ARPHRD_IEEE802154) {
290 static int ieee802154_associate_req(struct sk_buff *skb,
291 struct genl_info *info)
293 struct net_device *dev;
294 struct ieee802154_addr addr;
297 if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
298 !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
299 (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
300 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
301 !info->attrs[IEEE802154_ATTR_CAPABILITY])
304 dev = ieee802154_nl_get_dev(info);
308 if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
309 addr.addr_type = IEEE802154_ADDR_LONG;
310 nla_memcpy(addr.hwaddr,
311 info->attrs[IEEE802154_ATTR_COORD_HW_ADDR],
312 IEEE802154_ADDR_LEN);
314 addr.addr_type = IEEE802154_ADDR_SHORT;
315 addr.short_addr = nla_get_u16(
316 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
318 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
320 ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
321 nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
322 nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
328 static int ieee802154_associate_resp(struct sk_buff *skb,
329 struct genl_info *info)
331 struct net_device *dev;
332 struct ieee802154_addr addr;
335 if (!info->attrs[IEEE802154_ATTR_STATUS] ||
336 !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
337 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
340 dev = ieee802154_nl_get_dev(info);
344 addr.addr_type = IEEE802154_ADDR_LONG;
345 nla_memcpy(addr.hwaddr, info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
346 IEEE802154_ADDR_LEN);
347 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
350 ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
351 nla_get_u16(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
352 nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
358 static int ieee802154_disassociate_req(struct sk_buff *skb,
359 struct genl_info *info)
361 struct net_device *dev;
362 struct ieee802154_addr addr;
365 if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
366 !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
367 !info->attrs[IEEE802154_ATTR_REASON])
370 dev = ieee802154_nl_get_dev(info);
374 if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
375 addr.addr_type = IEEE802154_ADDR_LONG;
376 nla_memcpy(addr.hwaddr,
377 info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
378 IEEE802154_ADDR_LEN);
380 addr.addr_type = IEEE802154_ADDR_SHORT;
381 addr.short_addr = nla_get_u16(
382 info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
384 addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
386 ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
387 nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
394 * PANid, channel, beacon_order = 15, superframe_order = 15,
395 * PAN_coordinator, battery_life_extension = 0,
396 * coord_realignment = 0, security_enable = 0
398 static int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
400 struct net_device *dev;
401 struct ieee802154_addr addr;
403 u8 channel, bcn_ord, sf_ord;
404 int pan_coord, blx, coord_realign;
407 if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
408 !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
409 !info->attrs[IEEE802154_ATTR_CHANNEL] ||
410 !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
411 !info->attrs[IEEE802154_ATTR_SF_ORD] ||
412 !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
413 !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
414 !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
418 dev = ieee802154_nl_get_dev(info);
422 addr.addr_type = IEEE802154_ADDR_SHORT;
423 addr.short_addr = nla_get_u16(
424 info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
425 addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
427 channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
428 bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
429 sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
430 pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
431 blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
432 coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
434 ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel,
435 bcn_ord, sf_ord, pan_coord, blx, coord_realign);
441 static int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
443 struct net_device *dev;
449 if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
450 !info->attrs[IEEE802154_ATTR_CHANNELS] ||
451 !info->attrs[IEEE802154_ATTR_DURATION])
454 dev = ieee802154_nl_get_dev(info);
458 type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
459 channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
460 duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
462 ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
469 #define IEEE802154_OP(_cmd, _func) \
472 .policy = ieee802154_policy, \
475 .flags = GENL_ADMIN_PERM, \
478 static struct genl_ops ieee802154_coordinator_ops[] = {
479 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
480 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
481 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
482 IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
483 IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
486 static int __init ieee802154_nl_init(void)
491 rc = genl_register_family(&ieee802154_coordinator_family);
495 rc = genl_register_mc_group(&ieee802154_coordinator_family,
496 &ieee802154_coord_mcgrp);
500 rc = genl_register_mc_group(&ieee802154_coordinator_family,
501 &ieee802154_beacon_mcgrp);
506 for (i = 0; i < ARRAY_SIZE(ieee802154_coordinator_ops); i++) {
507 rc = genl_register_ops(&ieee802154_coordinator_family,
508 &ieee802154_coordinator_ops[i]);
516 genl_unregister_family(&ieee802154_coordinator_family);
519 module_init(ieee802154_nl_init);
521 static void __exit ieee802154_nl_exit(void)
523 genl_unregister_family(&ieee802154_coordinator_family);
525 module_exit(ieee802154_nl_exit);
527 MODULE_LICENSE("GPL v2");
528 MODULE_DESCRIPTION("ieee 802.15.4 configuration interface");