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/gfp.h>
26
#include <linux/kernel.h>
27
#include <linux/if_arp.h>
28
#include <linux/netdevice.h>
29
#include <net/netlink.h>
30
#include <net/genetlink.h>
32
#include <linux/nl802154.h>
33
#include <linux/export.h>
34
#include <net/af_ieee802154.h>
35
#include <net/nl802154.h>
36
#include <net/ieee802154.h>
37
#include <net/ieee802154_netdev.h>
38
#include <net/wpan-phy.h>
40
#include "ieee802154.h"
42
static struct genl_multicast_group ieee802154_coord_mcgrp = {
43
.name = IEEE802154_MCAST_COORD_NAME,
46
static struct genl_multicast_group ieee802154_beacon_mcgrp = {
47
.name = IEEE802154_MCAST_BEACON_NAME,
50
int ieee802154_nl_assoc_indic(struct net_device *dev,
51
struct ieee802154_addr *addr, u8 cap)
55
pr_debug("%s\n", __func__);
57
if (addr->addr_type != IEEE802154_ADDR_LONG) {
58
pr_err("%s: received non-long source address!\n", __func__);
62
msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_INDIC);
66
NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
67
NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
68
NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
71
NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
74
NLA_PUT_U8(msg, IEEE802154_ATTR_CAPABILITY, cap);
76
return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
82
EXPORT_SYMBOL(ieee802154_nl_assoc_indic);
84
int ieee802154_nl_assoc_confirm(struct net_device *dev, u16 short_addr,
89
pr_debug("%s\n", __func__);
91
msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_CONF);
95
NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
96
NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
97
NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
100
NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr);
101
NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
103
return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
109
EXPORT_SYMBOL(ieee802154_nl_assoc_confirm);
111
int ieee802154_nl_disassoc_indic(struct net_device *dev,
112
struct ieee802154_addr *addr, u8 reason)
116
pr_debug("%s\n", __func__);
118
msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_INDIC);
122
NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
123
NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
124
NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
127
if (addr->addr_type == IEEE802154_ADDR_LONG)
128
NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
131
NLA_PUT_U16(msg, IEEE802154_ATTR_SRC_SHORT_ADDR,
134
NLA_PUT_U8(msg, IEEE802154_ATTR_REASON, reason);
136
return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
142
EXPORT_SYMBOL(ieee802154_nl_disassoc_indic);
144
int ieee802154_nl_disassoc_confirm(struct net_device *dev, u8 status)
148
pr_debug("%s\n", __func__);
150
msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_CONF);
154
NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
155
NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
156
NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
159
NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
161
return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
167
EXPORT_SYMBOL(ieee802154_nl_disassoc_confirm);
169
int ieee802154_nl_beacon_indic(struct net_device *dev,
170
u16 panid, u16 coord_addr)
174
pr_debug("%s\n", __func__);
176
msg = ieee802154_nl_create(0, IEEE802154_BEACON_NOTIFY_INDIC);
180
NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
181
NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
182
NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
184
NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_SHORT_ADDR, coord_addr);
185
NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_PAN_ID, panid);
187
return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
193
EXPORT_SYMBOL(ieee802154_nl_beacon_indic);
195
int ieee802154_nl_scan_confirm(struct net_device *dev,
196
u8 status, u8 scan_type, u32 unscanned, u8 page,
197
u8 *edl/* , struct list_head *pan_desc_list */)
201
pr_debug("%s\n", __func__);
203
msg = ieee802154_nl_create(0, IEEE802154_SCAN_CONF);
207
NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
208
NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
209
NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
212
NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
213
NLA_PUT_U8(msg, IEEE802154_ATTR_SCAN_TYPE, scan_type);
214
NLA_PUT_U32(msg, IEEE802154_ATTR_CHANNELS, unscanned);
215
NLA_PUT_U8(msg, IEEE802154_ATTR_PAGE, page);
218
NLA_PUT(msg, IEEE802154_ATTR_ED_LIST, 27, edl);
220
return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
226
EXPORT_SYMBOL(ieee802154_nl_scan_confirm);
228
int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
232
pr_debug("%s\n", __func__);
234
msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
238
NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
239
NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
240
NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
243
NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
245
return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
251
EXPORT_SYMBOL(ieee802154_nl_start_confirm);
253
static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 pid,
254
u32 seq, int flags, struct net_device *dev)
257
struct wpan_phy *phy;
259
pr_debug("%s\n", __func__);
261
hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
262
IEEE802154_LIST_IFACE);
266
phy = ieee802154_mlme_ops(dev)->get_phy(dev);
269
NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
270
NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
271
NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
273
NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
275
NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR,
276
ieee802154_mlme_ops(dev)->get_short_addr(dev));
277
NLA_PUT_U16(msg, IEEE802154_ATTR_PAN_ID,
278
ieee802154_mlme_ops(dev)->get_pan_id(dev));
280
return genlmsg_end(msg, hdr);
284
genlmsg_cancel(msg, hdr);
289
/* Requests from userspace */
290
static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
292
struct net_device *dev;
294
if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
295
char name[IFNAMSIZ + 1];
296
nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
298
dev = dev_get_by_name(&init_net, name);
299
} else if (info->attrs[IEEE802154_ATTR_DEV_INDEX])
300
dev = dev_get_by_index(&init_net,
301
nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
308
if (dev->type != ARPHRD_IEEE802154) {
316
static int ieee802154_associate_req(struct sk_buff *skb,
317
struct genl_info *info)
319
struct net_device *dev;
320
struct ieee802154_addr addr;
324
if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
325
!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
326
(!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
327
!info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
328
!info->attrs[IEEE802154_ATTR_CAPABILITY])
331
dev = ieee802154_nl_get_dev(info);
335
if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
336
addr.addr_type = IEEE802154_ADDR_LONG;
337
nla_memcpy(addr.hwaddr,
338
info->attrs[IEEE802154_ATTR_COORD_HW_ADDR],
339
IEEE802154_ADDR_LEN);
341
addr.addr_type = IEEE802154_ADDR_SHORT;
342
addr.short_addr = nla_get_u16(
343
info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
345
addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
347
if (info->attrs[IEEE802154_ATTR_PAGE])
348
page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
352
ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
353
nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
355
nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
361
static int ieee802154_associate_resp(struct sk_buff *skb,
362
struct genl_info *info)
364
struct net_device *dev;
365
struct ieee802154_addr addr;
368
if (!info->attrs[IEEE802154_ATTR_STATUS] ||
369
!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
370
!info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
373
dev = ieee802154_nl_get_dev(info);
377
addr.addr_type = IEEE802154_ADDR_LONG;
378
nla_memcpy(addr.hwaddr, info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
379
IEEE802154_ADDR_LEN);
380
addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
383
ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
384
nla_get_u16(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
385
nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
391
static int ieee802154_disassociate_req(struct sk_buff *skb,
392
struct genl_info *info)
394
struct net_device *dev;
395
struct ieee802154_addr addr;
398
if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
399
!info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
400
!info->attrs[IEEE802154_ATTR_REASON])
403
dev = ieee802154_nl_get_dev(info);
407
if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
408
addr.addr_type = IEEE802154_ADDR_LONG;
409
nla_memcpy(addr.hwaddr,
410
info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
411
IEEE802154_ADDR_LEN);
413
addr.addr_type = IEEE802154_ADDR_SHORT;
414
addr.short_addr = nla_get_u16(
415
info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
417
addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
419
ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
420
nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
427
* PANid, channel, beacon_order = 15, superframe_order = 15,
428
* PAN_coordinator, battery_life_extension = 0,
429
* coord_realignment = 0, security_enable = 0
431
static int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
433
struct net_device *dev;
434
struct ieee802154_addr addr;
436
u8 channel, bcn_ord, sf_ord;
438
int pan_coord, blx, coord_realign;
441
if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
442
!info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
443
!info->attrs[IEEE802154_ATTR_CHANNEL] ||
444
!info->attrs[IEEE802154_ATTR_BCN_ORD] ||
445
!info->attrs[IEEE802154_ATTR_SF_ORD] ||
446
!info->attrs[IEEE802154_ATTR_PAN_COORD] ||
447
!info->attrs[IEEE802154_ATTR_BAT_EXT] ||
448
!info->attrs[IEEE802154_ATTR_COORD_REALIGN]
452
dev = ieee802154_nl_get_dev(info);
456
addr.addr_type = IEEE802154_ADDR_SHORT;
457
addr.short_addr = nla_get_u16(
458
info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
459
addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
461
channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
462
bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
463
sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
464
pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
465
blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
466
coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
468
if (info->attrs[IEEE802154_ATTR_PAGE])
469
page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
474
if (addr.short_addr == IEEE802154_ADDR_BROADCAST) {
475
ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
480
ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
481
bcn_ord, sf_ord, pan_coord, blx, coord_realign);
487
static int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
489
struct net_device *dev;
496
if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
497
!info->attrs[IEEE802154_ATTR_CHANNELS] ||
498
!info->attrs[IEEE802154_ATTR_DURATION])
501
dev = ieee802154_nl_get_dev(info);
505
type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
506
channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
507
duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
509
if (info->attrs[IEEE802154_ATTR_PAGE])
510
page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
515
ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels, page,
522
static int ieee802154_list_iface(struct sk_buff *skb,
523
struct genl_info *info)
525
/* Request for interface name, index, type, IEEE address,
526
PAN Id, short address */
528
struct net_device *dev = NULL;
531
pr_debug("%s\n", __func__);
533
dev = ieee802154_nl_get_dev(info);
537
msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
541
rc = ieee802154_nl_fill_iface(msg, info->snd_pid, info->snd_seq,
548
return genlmsg_reply(msg, info);
557
static int ieee802154_dump_iface(struct sk_buff *skb,
558
struct netlink_callback *cb)
560
struct net *net = sock_net(skb->sk);
561
struct net_device *dev;
563
int s_idx = cb->args[0];
565
pr_debug("%s\n", __func__);
568
for_each_netdev(net, dev) {
569
if (idx < s_idx || (dev->type != ARPHRD_IEEE802154))
572
if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).pid,
573
cb->nlh->nlmsg_seq, NLM_F_MULTI, dev) < 0)
583
static struct genl_ops ieee802154_coordinator_ops[] = {
584
IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
585
IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
586
IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
587
IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
588
IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
589
IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
590
ieee802154_dump_iface),
594
* No need to unregister as family unregistration will do it.
596
int nl802154_mac_register(void)
601
rc = genl_register_mc_group(&nl802154_family,
602
&ieee802154_coord_mcgrp);
606
rc = genl_register_mc_group(&nl802154_family,
607
&ieee802154_beacon_mcgrp);
611
for (i = 0; i < ARRAY_SIZE(ieee802154_coordinator_ops); i++) {
612
rc = genl_register_ops(&nl802154_family,
613
&ieee802154_coordinator_ops[i]);