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 <net/af_ieee802154.h>
34 #include <net/nl802154.h>
35 #include <net/ieee802154.h>
36 #include <net/ieee802154_netdev.h>
37 #include <net/wpan-phy.h>
39 #include "ieee802154.h"
41 static struct genl_multicast_group ieee802154_coord_mcgrp
= {
42 .name
= IEEE802154_MCAST_COORD_NAME
,
45 static struct genl_multicast_group ieee802154_beacon_mcgrp
= {
46 .name
= IEEE802154_MCAST_BEACON_NAME
,
49 int ieee802154_nl_assoc_indic(struct net_device
*dev
,
50 struct ieee802154_addr
*addr
, u8 cap
)
54 pr_debug("%s\n", __func__
);
56 if (addr
->addr_type
!= IEEE802154_ADDR_LONG
) {
57 pr_err("%s: received non-long source address!\n", __func__
);
61 msg
= ieee802154_nl_create(0, IEEE802154_ASSOCIATE_INDIC
);
65 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
66 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
67 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
70 NLA_PUT(msg
, IEEE802154_ATTR_SRC_HW_ADDR
, IEEE802154_ADDR_LEN
,
73 NLA_PUT_U8(msg
, IEEE802154_ATTR_CAPABILITY
, cap
);
75 return ieee802154_nl_mcast(msg
, ieee802154_coord_mcgrp
.id
);
81 EXPORT_SYMBOL(ieee802154_nl_assoc_indic
);
83 int ieee802154_nl_assoc_confirm(struct net_device
*dev
, u16 short_addr
,
88 pr_debug("%s\n", __func__
);
90 msg
= ieee802154_nl_create(0, IEEE802154_ASSOCIATE_CONF
);
94 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
95 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
96 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
99 NLA_PUT_U16(msg
, IEEE802154_ATTR_SHORT_ADDR
, short_addr
);
100 NLA_PUT_U8(msg
, IEEE802154_ATTR_STATUS
, status
);
102 return ieee802154_nl_mcast(msg
, ieee802154_coord_mcgrp
.id
);
108 EXPORT_SYMBOL(ieee802154_nl_assoc_confirm
);
110 int ieee802154_nl_disassoc_indic(struct net_device
*dev
,
111 struct ieee802154_addr
*addr
, u8 reason
)
115 pr_debug("%s\n", __func__
);
117 msg
= ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_INDIC
);
121 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
122 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
123 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
126 if (addr
->addr_type
== IEEE802154_ADDR_LONG
)
127 NLA_PUT(msg
, IEEE802154_ATTR_SRC_HW_ADDR
, IEEE802154_ADDR_LEN
,
130 NLA_PUT_U16(msg
, IEEE802154_ATTR_SRC_SHORT_ADDR
,
133 NLA_PUT_U8(msg
, IEEE802154_ATTR_REASON
, reason
);
135 return ieee802154_nl_mcast(msg
, ieee802154_coord_mcgrp
.id
);
141 EXPORT_SYMBOL(ieee802154_nl_disassoc_indic
);
143 int ieee802154_nl_disassoc_confirm(struct net_device
*dev
, u8 status
)
147 pr_debug("%s\n", __func__
);
149 msg
= ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_CONF
);
153 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
154 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
155 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
158 NLA_PUT_U8(msg
, IEEE802154_ATTR_STATUS
, status
);
160 return ieee802154_nl_mcast(msg
, ieee802154_coord_mcgrp
.id
);
166 EXPORT_SYMBOL(ieee802154_nl_disassoc_confirm
);
168 int ieee802154_nl_beacon_indic(struct net_device
*dev
,
169 u16 panid
, u16 coord_addr
)
173 pr_debug("%s\n", __func__
);
175 msg
= ieee802154_nl_create(0, IEEE802154_BEACON_NOTIFY_INDIC
);
179 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
180 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
181 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
183 NLA_PUT_U16(msg
, IEEE802154_ATTR_COORD_SHORT_ADDR
, coord_addr
);
184 NLA_PUT_U16(msg
, IEEE802154_ATTR_COORD_PAN_ID
, panid
);
186 return ieee802154_nl_mcast(msg
, ieee802154_coord_mcgrp
.id
);
192 EXPORT_SYMBOL(ieee802154_nl_beacon_indic
);
194 int ieee802154_nl_scan_confirm(struct net_device
*dev
,
195 u8 status
, u8 scan_type
, u32 unscanned
, u8 page
,
196 u8
*edl
/* , struct list_head *pan_desc_list */)
200 pr_debug("%s\n", __func__
);
202 msg
= ieee802154_nl_create(0, IEEE802154_SCAN_CONF
);
206 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
207 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
208 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
211 NLA_PUT_U8(msg
, IEEE802154_ATTR_STATUS
, status
);
212 NLA_PUT_U8(msg
, IEEE802154_ATTR_SCAN_TYPE
, scan_type
);
213 NLA_PUT_U32(msg
, IEEE802154_ATTR_CHANNELS
, unscanned
);
214 NLA_PUT_U8(msg
, IEEE802154_ATTR_PAGE
, page
);
217 NLA_PUT(msg
, IEEE802154_ATTR_ED_LIST
, 27, edl
);
219 return ieee802154_nl_mcast(msg
, ieee802154_coord_mcgrp
.id
);
225 EXPORT_SYMBOL(ieee802154_nl_scan_confirm
);
227 int ieee802154_nl_start_confirm(struct net_device
*dev
, u8 status
)
231 pr_debug("%s\n", __func__
);
233 msg
= ieee802154_nl_create(0, IEEE802154_START_CONF
);
237 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
238 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
239 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
242 NLA_PUT_U8(msg
, IEEE802154_ATTR_STATUS
, status
);
244 return ieee802154_nl_mcast(msg
, ieee802154_coord_mcgrp
.id
);
250 EXPORT_SYMBOL(ieee802154_nl_start_confirm
);
252 static int ieee802154_nl_fill_iface(struct sk_buff
*msg
, u32 pid
,
253 u32 seq
, int flags
, struct net_device
*dev
)
256 struct wpan_phy
*phy
;
258 pr_debug("%s\n", __func__
);
260 hdr
= genlmsg_put(msg
, 0, seq
, &nl802154_family
, flags
,
261 IEEE802154_LIST_IFACE
);
265 phy
= ieee802154_mlme_ops(dev
)->get_phy(dev
);
268 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
269 NLA_PUT_STRING(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
));
270 NLA_PUT_U32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
);
272 NLA_PUT(msg
, IEEE802154_ATTR_HW_ADDR
, IEEE802154_ADDR_LEN
,
274 NLA_PUT_U16(msg
, IEEE802154_ATTR_SHORT_ADDR
,
275 ieee802154_mlme_ops(dev
)->get_short_addr(dev
));
276 NLA_PUT_U16(msg
, IEEE802154_ATTR_PAN_ID
,
277 ieee802154_mlme_ops(dev
)->get_pan_id(dev
));
279 return genlmsg_end(msg
, hdr
);
283 genlmsg_cancel(msg
, hdr
);
288 /* Requests from userspace */
289 static struct net_device
*ieee802154_nl_get_dev(struct genl_info
*info
)
291 struct net_device
*dev
;
293 if (info
->attrs
[IEEE802154_ATTR_DEV_NAME
]) {
294 char name
[IFNAMSIZ
+ 1];
295 nla_strlcpy(name
, info
->attrs
[IEEE802154_ATTR_DEV_NAME
],
297 dev
= dev_get_by_name(&init_net
, name
);
298 } else if (info
->attrs
[IEEE802154_ATTR_DEV_INDEX
])
299 dev
= dev_get_by_index(&init_net
,
300 nla_get_u32(info
->attrs
[IEEE802154_ATTR_DEV_INDEX
]));
307 if (dev
->type
!= ARPHRD_IEEE802154
) {
315 static int ieee802154_associate_req(struct sk_buff
*skb
,
316 struct genl_info
*info
)
318 struct net_device
*dev
;
319 struct ieee802154_addr addr
;
323 if (!info
->attrs
[IEEE802154_ATTR_CHANNEL
] ||
324 !info
->attrs
[IEEE802154_ATTR_COORD_PAN_ID
] ||
325 (!info
->attrs
[IEEE802154_ATTR_COORD_HW_ADDR
] &&
326 !info
->attrs
[IEEE802154_ATTR_COORD_SHORT_ADDR
]) ||
327 !info
->attrs
[IEEE802154_ATTR_CAPABILITY
])
330 dev
= ieee802154_nl_get_dev(info
);
334 if (info
->attrs
[IEEE802154_ATTR_COORD_HW_ADDR
]) {
335 addr
.addr_type
= IEEE802154_ADDR_LONG
;
336 nla_memcpy(addr
.hwaddr
,
337 info
->attrs
[IEEE802154_ATTR_COORD_HW_ADDR
],
338 IEEE802154_ADDR_LEN
);
340 addr
.addr_type
= IEEE802154_ADDR_SHORT
;
341 addr
.short_addr
= nla_get_u16(
342 info
->attrs
[IEEE802154_ATTR_COORD_SHORT_ADDR
]);
344 addr
.pan_id
= nla_get_u16(info
->attrs
[IEEE802154_ATTR_COORD_PAN_ID
]);
346 if (info
->attrs
[IEEE802154_ATTR_PAGE
])
347 page
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_PAGE
]);
351 ret
= ieee802154_mlme_ops(dev
)->assoc_req(dev
, &addr
,
352 nla_get_u8(info
->attrs
[IEEE802154_ATTR_CHANNEL
]),
354 nla_get_u8(info
->attrs
[IEEE802154_ATTR_CAPABILITY
]));
360 static int ieee802154_associate_resp(struct sk_buff
*skb
,
361 struct genl_info
*info
)
363 struct net_device
*dev
;
364 struct ieee802154_addr addr
;
367 if (!info
->attrs
[IEEE802154_ATTR_STATUS
] ||
368 !info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
] ||
369 !info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
])
372 dev
= ieee802154_nl_get_dev(info
);
376 addr
.addr_type
= IEEE802154_ADDR_LONG
;
377 nla_memcpy(addr
.hwaddr
, info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
],
378 IEEE802154_ADDR_LEN
);
379 addr
.pan_id
= ieee802154_mlme_ops(dev
)->get_pan_id(dev
);
382 ret
= ieee802154_mlme_ops(dev
)->assoc_resp(dev
, &addr
,
383 nla_get_u16(info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
]),
384 nla_get_u8(info
->attrs
[IEEE802154_ATTR_STATUS
]));
390 static int ieee802154_disassociate_req(struct sk_buff
*skb
,
391 struct genl_info
*info
)
393 struct net_device
*dev
;
394 struct ieee802154_addr addr
;
397 if ((!info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
] &&
398 !info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
]) ||
399 !info
->attrs
[IEEE802154_ATTR_REASON
])
402 dev
= ieee802154_nl_get_dev(info
);
406 if (info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
]) {
407 addr
.addr_type
= IEEE802154_ADDR_LONG
;
408 nla_memcpy(addr
.hwaddr
,
409 info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
],
410 IEEE802154_ADDR_LEN
);
412 addr
.addr_type
= IEEE802154_ADDR_SHORT
;
413 addr
.short_addr
= nla_get_u16(
414 info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
]);
416 addr
.pan_id
= ieee802154_mlme_ops(dev
)->get_pan_id(dev
);
418 ret
= ieee802154_mlme_ops(dev
)->disassoc_req(dev
, &addr
,
419 nla_get_u8(info
->attrs
[IEEE802154_ATTR_REASON
]));
426 * PANid, channel, beacon_order = 15, superframe_order = 15,
427 * PAN_coordinator, battery_life_extension = 0,
428 * coord_realignment = 0, security_enable = 0
430 static int ieee802154_start_req(struct sk_buff
*skb
, struct genl_info
*info
)
432 struct net_device
*dev
;
433 struct ieee802154_addr addr
;
435 u8 channel
, bcn_ord
, sf_ord
;
437 int pan_coord
, blx
, coord_realign
;
440 if (!info
->attrs
[IEEE802154_ATTR_COORD_PAN_ID
] ||
441 !info
->attrs
[IEEE802154_ATTR_COORD_SHORT_ADDR
] ||
442 !info
->attrs
[IEEE802154_ATTR_CHANNEL
] ||
443 !info
->attrs
[IEEE802154_ATTR_BCN_ORD
] ||
444 !info
->attrs
[IEEE802154_ATTR_SF_ORD
] ||
445 !info
->attrs
[IEEE802154_ATTR_PAN_COORD
] ||
446 !info
->attrs
[IEEE802154_ATTR_BAT_EXT
] ||
447 !info
->attrs
[IEEE802154_ATTR_COORD_REALIGN
]
451 dev
= ieee802154_nl_get_dev(info
);
455 addr
.addr_type
= IEEE802154_ADDR_SHORT
;
456 addr
.short_addr
= nla_get_u16(
457 info
->attrs
[IEEE802154_ATTR_COORD_SHORT_ADDR
]);
458 addr
.pan_id
= nla_get_u16(info
->attrs
[IEEE802154_ATTR_COORD_PAN_ID
]);
460 channel
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_CHANNEL
]);
461 bcn_ord
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_BCN_ORD
]);
462 sf_ord
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_SF_ORD
]);
463 pan_coord
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_PAN_COORD
]);
464 blx
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_BAT_EXT
]);
465 coord_realign
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_COORD_REALIGN
]);
467 if (info
->attrs
[IEEE802154_ATTR_PAGE
])
468 page
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_PAGE
]);
473 if (addr
.short_addr
== IEEE802154_ADDR_BROADCAST
) {
474 ieee802154_nl_start_confirm(dev
, IEEE802154_NO_SHORT_ADDRESS
);
479 ret
= ieee802154_mlme_ops(dev
)->start_req(dev
, &addr
, channel
, page
,
480 bcn_ord
, sf_ord
, pan_coord
, blx
, coord_realign
);
486 static int ieee802154_scan_req(struct sk_buff
*skb
, struct genl_info
*info
)
488 struct net_device
*dev
;
495 if (!info
->attrs
[IEEE802154_ATTR_SCAN_TYPE
] ||
496 !info
->attrs
[IEEE802154_ATTR_CHANNELS
] ||
497 !info
->attrs
[IEEE802154_ATTR_DURATION
])
500 dev
= ieee802154_nl_get_dev(info
);
504 type
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_SCAN_TYPE
]);
505 channels
= nla_get_u32(info
->attrs
[IEEE802154_ATTR_CHANNELS
]);
506 duration
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_DURATION
]);
508 if (info
->attrs
[IEEE802154_ATTR_PAGE
])
509 page
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_PAGE
]);
514 ret
= ieee802154_mlme_ops(dev
)->scan_req(dev
, type
, channels
, page
,
521 static int ieee802154_list_iface(struct sk_buff
*skb
,
522 struct genl_info
*info
)
524 /* Request for interface name, index, type, IEEE address,
525 PAN Id, short address */
527 struct net_device
*dev
= NULL
;
530 pr_debug("%s\n", __func__
);
532 dev
= ieee802154_nl_get_dev(info
);
536 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
540 rc
= ieee802154_nl_fill_iface(msg
, info
->snd_pid
, info
->snd_seq
,
547 return genlmsg_reply(msg
, info
);
556 static int ieee802154_dump_iface(struct sk_buff
*skb
,
557 struct netlink_callback
*cb
)
559 struct net
*net
= sock_net(skb
->sk
);
560 struct net_device
*dev
;
562 int s_idx
= cb
->args
[0];
564 pr_debug("%s\n", __func__
);
567 for_each_netdev(net
, dev
) {
568 if (idx
< s_idx
|| (dev
->type
!= ARPHRD_IEEE802154
))
571 if (ieee802154_nl_fill_iface(skb
, NETLINK_CB(cb
->skb
).pid
,
572 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, dev
) < 0)
582 static struct genl_ops ieee802154_coordinator_ops
[] = {
583 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ
, ieee802154_associate_req
),
584 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP
, ieee802154_associate_resp
),
585 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ
, ieee802154_disassociate_req
),
586 IEEE802154_OP(IEEE802154_SCAN_REQ
, ieee802154_scan_req
),
587 IEEE802154_OP(IEEE802154_START_REQ
, ieee802154_start_req
),
588 IEEE802154_DUMP(IEEE802154_LIST_IFACE
, ieee802154_list_iface
,
589 ieee802154_dump_iface
),
593 * No need to unregister as family unregistration will do it.
595 int nl802154_mac_register(void)
600 rc
= genl_register_mc_group(&nl802154_family
,
601 &ieee802154_coord_mcgrp
);
605 rc
= genl_register_mc_group(&nl802154_family
,
606 &ieee802154_beacon_mcgrp
);
610 for (i
= 0; i
< ARRAY_SIZE(ieee802154_coordinator_ops
); i
++) {
611 rc
= genl_register_ops(&nl802154_family
,
612 &ieee802154_coordinator_ops
[i
]);