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 static unsigned int ieee802154_seq_num
;
38 static DEFINE_SPINLOCK(ieee802154_seq_lock
);
40 static struct genl_family ieee802154_coordinator_family
= {
41 .id
= GENL_ID_GENERATE
,
43 .name
= IEEE802154_NL_NAME
,
45 .maxattr
= IEEE802154_ATTR_MAX
,
48 static struct genl_multicast_group ieee802154_coord_mcgrp
= {
49 .name
= IEEE802154_MCAST_COORD_NAME
,
52 static struct genl_multicast_group ieee802154_beacon_mcgrp
= {
53 .name
= IEEE802154_MCAST_BEACON_NAME
,
56 /* Requests to userspace */
57 static struct sk_buff
*ieee802154_nl_create(int flags
, u8 req
)
60 struct sk_buff
*msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_ATOMIC
);
66 spin_lock_irqsave(&ieee802154_seq_lock
, f
);
67 hdr
= genlmsg_put(msg
, 0, ieee802154_seq_num
++,
68 &ieee802154_coordinator_family
, flags
, req
);
69 spin_unlock_irqrestore(&ieee802154_seq_lock
, f
);
78 static int ieee802154_nl_finish(struct sk_buff
*msg
)
80 /* XXX: nlh is right at the start of msg */
81 void *hdr
= genlmsg_data(NLMSG_DATA(msg
->data
));
83 if (genlmsg_end(msg
, hdr
) < 0)
86 return genlmsg_multicast(msg
, 0, ieee802154_coord_mcgrp
.id
,
93 int ieee802154_nl_assoc_indic(struct net_device
*dev
,
94 struct ieee802154_addr
*addr
, u8 cap
)
98 pr_debug("%s\n", __func__
);
100 if (addr
->addr_type
!= IEEE802154_ADDR_LONG
) {
101 pr_err("%s: received non-long source address!\n", __func__
);
105 msg
= ieee802154_nl_create(0, IEEE802154_ASSOCIATE_INDIC
);
109 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
110 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
111 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
114 NLA_PUT(msg
, IEEE802154_ATTR_SRC_HW_ADDR
, IEEE802154_ADDR_LEN
,
117 NLA_PUT_U8(msg
, IEEE802154_ATTR_CAPABILITY
, cap
);
119 return ieee802154_nl_finish(msg
);
125 EXPORT_SYMBOL(ieee802154_nl_assoc_indic
);
127 int ieee802154_nl_assoc_confirm(struct net_device
*dev
, u16 short_addr
,
132 pr_debug("%s\n", __func__
);
134 msg
= ieee802154_nl_create(0, IEEE802154_ASSOCIATE_CONF
);
138 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
139 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
140 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
143 NLA_PUT_U16(msg
, IEEE802154_ATTR_SHORT_ADDR
, short_addr
);
144 NLA_PUT_U8(msg
, IEEE802154_ATTR_STATUS
, status
);
146 return ieee802154_nl_finish(msg
);
152 EXPORT_SYMBOL(ieee802154_nl_assoc_confirm
);
154 int ieee802154_nl_disassoc_indic(struct net_device
*dev
,
155 struct ieee802154_addr
*addr
, u8 reason
)
159 pr_debug("%s\n", __func__
);
161 msg
= ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_INDIC
);
165 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
166 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
167 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
170 if (addr
->addr_type
== IEEE802154_ADDR_LONG
)
171 NLA_PUT(msg
, IEEE802154_ATTR_SRC_HW_ADDR
, IEEE802154_ADDR_LEN
,
174 NLA_PUT_U16(msg
, IEEE802154_ATTR_SRC_SHORT_ADDR
,
177 NLA_PUT_U8(msg
, IEEE802154_ATTR_REASON
, reason
);
179 return ieee802154_nl_finish(msg
);
185 EXPORT_SYMBOL(ieee802154_nl_disassoc_indic
);
187 int ieee802154_nl_disassoc_confirm(struct net_device
*dev
, u8 status
)
191 pr_debug("%s\n", __func__
);
193 msg
= ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_CONF
);
197 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
198 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
199 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
202 NLA_PUT_U8(msg
, IEEE802154_ATTR_STATUS
, status
);
204 return ieee802154_nl_finish(msg
);
210 EXPORT_SYMBOL(ieee802154_nl_disassoc_confirm
);
212 int ieee802154_nl_beacon_indic(struct net_device
*dev
,
213 u16 panid
, u16 coord_addr
)
217 pr_debug("%s\n", __func__
);
219 msg
= ieee802154_nl_create(0, IEEE802154_BEACON_NOTIFY_INDIC
);
223 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
224 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
225 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
227 NLA_PUT_U16(msg
, IEEE802154_ATTR_COORD_SHORT_ADDR
, coord_addr
);
228 NLA_PUT_U16(msg
, IEEE802154_ATTR_COORD_PAN_ID
, panid
);
230 return ieee802154_nl_finish(msg
);
236 EXPORT_SYMBOL(ieee802154_nl_beacon_indic
);
238 int ieee802154_nl_scan_confirm(struct net_device
*dev
,
239 u8 status
, u8 scan_type
, u32 unscanned
, u8 page
,
240 u8
*edl
/* , struct list_head *pan_desc_list */)
244 pr_debug("%s\n", __func__
);
246 msg
= ieee802154_nl_create(0, IEEE802154_SCAN_CONF
);
250 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
251 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
252 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
255 NLA_PUT_U8(msg
, IEEE802154_ATTR_STATUS
, status
);
256 NLA_PUT_U8(msg
, IEEE802154_ATTR_SCAN_TYPE
, scan_type
);
257 NLA_PUT_U32(msg
, IEEE802154_ATTR_CHANNELS
, unscanned
);
258 NLA_PUT_U8(msg
, IEEE802154_ATTR_PAGE
, page
);
261 NLA_PUT(msg
, IEEE802154_ATTR_ED_LIST
, 27, edl
);
263 return ieee802154_nl_finish(msg
);
269 EXPORT_SYMBOL(ieee802154_nl_scan_confirm
);
271 int ieee802154_nl_start_confirm(struct net_device
*dev
, u8 status
)
275 pr_debug("%s\n", __func__
);
277 msg
= ieee802154_nl_create(0, IEEE802154_START_CONF
);
281 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
282 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
283 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
286 NLA_PUT_U8(msg
, IEEE802154_ATTR_STATUS
, status
);
288 return ieee802154_nl_finish(msg
);
294 EXPORT_SYMBOL(ieee802154_nl_start_confirm
);
296 static int ieee802154_nl_fill_iface(struct sk_buff
*msg
, u32 pid
,
297 u32 seq
, int flags
, struct net_device
*dev
)
301 pr_debug("%s\n", __func__
);
303 hdr
= genlmsg_put(msg
, 0, seq
, &ieee802154_coordinator_family
, flags
,
304 IEEE802154_LIST_IFACE
);
308 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
309 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
311 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
313 NLA_PUT_U16(msg
, IEEE802154_ATTR_SHORT_ADDR
,
314 ieee802154_mlme_ops(dev
)->get_short_addr(dev
));
315 NLA_PUT_U16(msg
, IEEE802154_ATTR_PAN_ID
,
316 ieee802154_mlme_ops(dev
)->get_pan_id(dev
));
317 return genlmsg_end(msg
, hdr
);
320 genlmsg_cancel(msg
, hdr
);
325 /* Requests from userspace */
326 static struct net_device
*ieee802154_nl_get_dev(struct genl_info
*info
)
328 struct net_device
*dev
;
330 if (info
->attrs
[IEEE802154_ATTR_DEV_NAME
]) {
331 char name
[IFNAMSIZ
+ 1];
332 nla_strlcpy(name
, info
->attrs
[IEEE802154_ATTR_DEV_NAME
],
334 dev
= dev_get_by_name(&init_net
, name
);
335 } else if (info
->attrs
[IEEE802154_ATTR_DEV_INDEX
])
336 dev
= dev_get_by_index(&init_net
,
337 nla_get_u32(info
->attrs
[IEEE802154_ATTR_DEV_INDEX
]));
344 if (dev
->type
!= ARPHRD_IEEE802154
) {
352 static int ieee802154_associate_req(struct sk_buff
*skb
,
353 struct genl_info
*info
)
355 struct net_device
*dev
;
356 struct ieee802154_addr addr
;
360 if (!info
->attrs
[IEEE802154_ATTR_CHANNEL
] ||
361 !info
->attrs
[IEEE802154_ATTR_COORD_PAN_ID
] ||
362 (!info
->attrs
[IEEE802154_ATTR_COORD_HW_ADDR
] &&
363 !info
->attrs
[IEEE802154_ATTR_COORD_SHORT_ADDR
]) ||
364 !info
->attrs
[IEEE802154_ATTR_CAPABILITY
])
367 dev
= ieee802154_nl_get_dev(info
);
371 if (info
->attrs
[IEEE802154_ATTR_COORD_HW_ADDR
]) {
372 addr
.addr_type
= IEEE802154_ADDR_LONG
;
373 nla_memcpy(addr
.hwaddr
,
374 info
->attrs
[IEEE802154_ATTR_COORD_HW_ADDR
],
375 IEEE802154_ADDR_LEN
);
377 addr
.addr_type
= IEEE802154_ADDR_SHORT
;
378 addr
.short_addr
= nla_get_u16(
379 info
->attrs
[IEEE802154_ATTR_COORD_SHORT_ADDR
]);
381 addr
.pan_id
= nla_get_u16(info
->attrs
[IEEE802154_ATTR_COORD_PAN_ID
]);
383 if (info
->attrs
[IEEE802154_ATTR_PAGE
])
384 page
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_PAGE
]);
388 ret
= ieee802154_mlme_ops(dev
)->assoc_req(dev
, &addr
,
389 nla_get_u8(info
->attrs
[IEEE802154_ATTR_CHANNEL
]),
391 nla_get_u8(info
->attrs
[IEEE802154_ATTR_CAPABILITY
]));
397 static int ieee802154_associate_resp(struct sk_buff
*skb
,
398 struct genl_info
*info
)
400 struct net_device
*dev
;
401 struct ieee802154_addr addr
;
404 if (!info
->attrs
[IEEE802154_ATTR_STATUS
] ||
405 !info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
] ||
406 !info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
])
409 dev
= ieee802154_nl_get_dev(info
);
413 addr
.addr_type
= IEEE802154_ADDR_LONG
;
414 nla_memcpy(addr
.hwaddr
, info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
],
415 IEEE802154_ADDR_LEN
);
416 addr
.pan_id
= ieee802154_mlme_ops(dev
)->get_pan_id(dev
);
419 ret
= ieee802154_mlme_ops(dev
)->assoc_resp(dev
, &addr
,
420 nla_get_u16(info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
]),
421 nla_get_u8(info
->attrs
[IEEE802154_ATTR_STATUS
]));
427 static int ieee802154_disassociate_req(struct sk_buff
*skb
,
428 struct genl_info
*info
)
430 struct net_device
*dev
;
431 struct ieee802154_addr addr
;
434 if ((!info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
] &&
435 !info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
]) ||
436 !info
->attrs
[IEEE802154_ATTR_REASON
])
439 dev
= ieee802154_nl_get_dev(info
);
443 if (info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
]) {
444 addr
.addr_type
= IEEE802154_ADDR_LONG
;
445 nla_memcpy(addr
.hwaddr
,
446 info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
],
447 IEEE802154_ADDR_LEN
);
449 addr
.addr_type
= IEEE802154_ADDR_SHORT
;
450 addr
.short_addr
= nla_get_u16(
451 info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
]);
453 addr
.pan_id
= ieee802154_mlme_ops(dev
)->get_pan_id(dev
);
455 ret
= ieee802154_mlme_ops(dev
)->disassoc_req(dev
, &addr
,
456 nla_get_u8(info
->attrs
[IEEE802154_ATTR_REASON
]));
463 * PANid, channel, beacon_order = 15, superframe_order = 15,
464 * PAN_coordinator, battery_life_extension = 0,
465 * coord_realignment = 0, security_enable = 0
467 static int ieee802154_start_req(struct sk_buff
*skb
, struct genl_info
*info
)
469 struct net_device
*dev
;
470 struct ieee802154_addr addr
;
472 u8 channel
, bcn_ord
, sf_ord
;
474 int pan_coord
, blx
, coord_realign
;
477 if (!info
->attrs
[IEEE802154_ATTR_COORD_PAN_ID
] ||
478 !info
->attrs
[IEEE802154_ATTR_COORD_SHORT_ADDR
] ||
479 !info
->attrs
[IEEE802154_ATTR_CHANNEL
] ||
480 !info
->attrs
[IEEE802154_ATTR_BCN_ORD
] ||
481 !info
->attrs
[IEEE802154_ATTR_SF_ORD
] ||
482 !info
->attrs
[IEEE802154_ATTR_PAN_COORD
] ||
483 !info
->attrs
[IEEE802154_ATTR_BAT_EXT
] ||
484 !info
->attrs
[IEEE802154_ATTR_COORD_REALIGN
]
488 dev
= ieee802154_nl_get_dev(info
);
492 addr
.addr_type
= IEEE802154_ADDR_SHORT
;
493 addr
.short_addr
= nla_get_u16(
494 info
->attrs
[IEEE802154_ATTR_COORD_SHORT_ADDR
]);
495 addr
.pan_id
= nla_get_u16(info
->attrs
[IEEE802154_ATTR_COORD_PAN_ID
]);
497 channel
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_CHANNEL
]);
498 bcn_ord
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_BCN_ORD
]);
499 sf_ord
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_SF_ORD
]);
500 pan_coord
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_PAN_COORD
]);
501 blx
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_BAT_EXT
]);
502 coord_realign
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_COORD_REALIGN
]);
504 if (info
->attrs
[IEEE802154_ATTR_PAGE
])
505 page
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_PAGE
]);
510 if (addr
.short_addr
== IEEE802154_ADDR_BROADCAST
) {
511 ieee802154_nl_start_confirm(dev
, IEEE802154_NO_SHORT_ADDRESS
);
516 ret
= ieee802154_mlme_ops(dev
)->start_req(dev
, &addr
, channel
, page
,
517 bcn_ord
, sf_ord
, pan_coord
, blx
, coord_realign
);
523 static int ieee802154_scan_req(struct sk_buff
*skb
, struct genl_info
*info
)
525 struct net_device
*dev
;
532 if (!info
->attrs
[IEEE802154_ATTR_SCAN_TYPE
] ||
533 !info
->attrs
[IEEE802154_ATTR_CHANNELS
] ||
534 !info
->attrs
[IEEE802154_ATTR_DURATION
])
537 dev
= ieee802154_nl_get_dev(info
);
541 type
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_SCAN_TYPE
]);
542 channels
= nla_get_u32(info
->attrs
[IEEE802154_ATTR_CHANNELS
]);
543 duration
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_DURATION
]);
545 if (info
->attrs
[IEEE802154_ATTR_PAGE
])
546 page
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_PAGE
]);
551 ret
= ieee802154_mlme_ops(dev
)->scan_req(dev
, type
, channels
, page
,
558 static int ieee802154_list_iface(struct sk_buff
*skb
,
559 struct genl_info
*info
)
561 /* Request for interface name, index, type, IEEE address,
562 PAN Id, short address */
564 struct net_device
*dev
= NULL
;
567 pr_debug("%s\n", __func__
);
569 dev
= ieee802154_nl_get_dev(info
);
573 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
577 rc
= ieee802154_nl_fill_iface(msg
, info
->snd_pid
, info
->snd_seq
,
584 return genlmsg_unicast(&init_net
, msg
, info
->snd_pid
);
593 static int ieee802154_dump_iface(struct sk_buff
*skb
,
594 struct netlink_callback
*cb
)
596 struct net
*net
= sock_net(skb
->sk
);
597 struct net_device
*dev
;
599 int s_idx
= cb
->args
[0];
601 pr_debug("%s\n", __func__
);
604 for_each_netdev(net
, dev
) {
605 if (idx
< s_idx
|| (dev
->type
!= ARPHRD_IEEE802154
))
608 if (ieee802154_nl_fill_iface(skb
, NETLINK_CB(cb
->skb
).pid
,
609 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, dev
) < 0)
619 #define IEEE802154_OP(_cmd, _func) \
622 .policy = ieee802154_policy, \
625 .flags = GENL_ADMIN_PERM, \
628 #define IEEE802154_DUMP(_cmd, _func, _dump) \
631 .policy = ieee802154_policy, \
636 static struct genl_ops ieee802154_coordinator_ops
[] = {
637 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ
, ieee802154_associate_req
),
638 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP
, ieee802154_associate_resp
),
639 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ
, ieee802154_disassociate_req
),
640 IEEE802154_OP(IEEE802154_SCAN_REQ
, ieee802154_scan_req
),
641 IEEE802154_OP(IEEE802154_START_REQ
, ieee802154_start_req
),
642 IEEE802154_DUMP(IEEE802154_LIST_IFACE
, ieee802154_list_iface
,
643 ieee802154_dump_iface
),
646 static int __init
ieee802154_nl_init(void)
651 rc
= genl_register_family(&ieee802154_coordinator_family
);
655 rc
= genl_register_mc_group(&ieee802154_coordinator_family
,
656 &ieee802154_coord_mcgrp
);
660 rc
= genl_register_mc_group(&ieee802154_coordinator_family
,
661 &ieee802154_beacon_mcgrp
);
666 for (i
= 0; i
< ARRAY_SIZE(ieee802154_coordinator_ops
); i
++) {
667 rc
= genl_register_ops(&ieee802154_coordinator_family
,
668 &ieee802154_coordinator_ops
[i
]);
676 genl_unregister_family(&ieee802154_coordinator_family
);
679 module_init(ieee802154_nl_init
);
681 static void __exit
ieee802154_nl_exit(void)
683 genl_unregister_family(&ieee802154_coordinator_family
);
685 module_exit(ieee802154_nl_exit
);
687 MODULE_LICENSE("GPL v2");
688 MODULE_DESCRIPTION("ieee 802.15.4 configuration interface");