2 * Copyright 2007-2012 Siemens AG
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
15 * Sergey Lapin <slapin@ossfans.org>
16 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
17 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
20 #include <linux/netdevice.h>
21 #include <linux/module.h>
22 #include <linux/if_arp.h>
23 #include <linux/ieee802154.h>
25 #include <net/nl802154.h>
26 #include <net/mac802154.h>
27 #include <net/ieee802154_netdev.h>
28 #include <net/cfg802154.h>
30 #include "ieee802154_i.h"
31 #include "driver-ops.h"
33 static int mac802154_wpan_update_llsec(struct net_device
*dev
)
35 struct ieee802154_sub_if_data
*sdata
= IEEE802154_DEV_TO_SUB_IF(dev
);
36 struct ieee802154_mlme_ops
*ops
= ieee802154_mlme_ops(dev
);
37 struct wpan_dev
*wpan_dev
= &sdata
->wpan_dev
;
41 struct ieee802154_llsec_params params
;
44 params
.pan_id
= wpan_dev
->pan_id
;
45 changed
|= IEEE802154_LLSEC_PARAM_PAN_ID
;
47 params
.hwaddr
= wpan_dev
->extended_addr
;
48 changed
|= IEEE802154_LLSEC_PARAM_HWADDR
;
50 rc
= ops
->llsec
->set_params(dev
, ¶ms
, changed
);
57 mac802154_wpan_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
59 struct ieee802154_sub_if_data
*sdata
= IEEE802154_DEV_TO_SUB_IF(dev
);
60 struct wpan_dev
*wpan_dev
= &sdata
->wpan_dev
;
61 struct sockaddr_ieee802154
*sa
=
62 (struct sockaddr_ieee802154
*)&ifr
->ifr_addr
;
63 int err
= -ENOIOCTLCMD
;
67 spin_lock_bh(&sdata
->mib_lock
);
72 u16 pan_id
, short_addr
;
74 pan_id
= le16_to_cpu(wpan_dev
->pan_id
);
75 short_addr
= le16_to_cpu(wpan_dev
->short_addr
);
76 if (pan_id
== IEEE802154_PANID_BROADCAST
||
77 short_addr
== IEEE802154_ADDR_BROADCAST
) {
82 sa
->family
= AF_IEEE802154
;
83 sa
->addr
.addr_type
= IEEE802154_ADDR_SHORT
;
84 sa
->addr
.pan_id
= pan_id
;
85 sa
->addr
.short_addr
= short_addr
;
91 if (netif_running(dev
)) {
92 spin_unlock_bh(&sdata
->mib_lock
);
97 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
98 if (sa
->family
!= AF_IEEE802154
||
99 sa
->addr
.addr_type
!= IEEE802154_ADDR_SHORT
||
100 sa
->addr
.pan_id
== IEEE802154_PANID_BROADCAST
||
101 sa
->addr
.short_addr
== IEEE802154_ADDR_BROADCAST
||
102 sa
->addr
.short_addr
== IEEE802154_ADDR_UNDEF
) {
107 wpan_dev
->pan_id
= cpu_to_le16(sa
->addr
.pan_id
);
108 wpan_dev
->short_addr
= cpu_to_le16(sa
->addr
.short_addr
);
110 err
= mac802154_wpan_update_llsec(dev
);
114 spin_unlock_bh(&sdata
->mib_lock
);
118 static int mac802154_wpan_mac_addr(struct net_device
*dev
, void *p
)
120 struct ieee802154_sub_if_data
*sdata
= IEEE802154_DEV_TO_SUB_IF(dev
);
121 struct sockaddr
*addr
= p
;
122 __le64 extended_addr
;
124 if (netif_running(dev
))
127 ieee802154_be64_to_le64(&extended_addr
, addr
->sa_data
);
128 if (!ieee802154_is_valid_extended_addr(extended_addr
))
131 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
132 sdata
->wpan_dev
.extended_addr
= extended_addr
;
134 return mac802154_wpan_update_llsec(dev
);
137 static int mac802154_slave_open(struct net_device
*dev
)
139 struct ieee802154_sub_if_data
*sdata
= IEEE802154_DEV_TO_SUB_IF(dev
);
140 struct ieee802154_local
*local
= sdata
->local
;
145 set_bit(SDATA_STATE_RUNNING
, &sdata
->state
);
147 if (!local
->open_count
) {
148 res
= drv_start(local
);
155 netif_start_queue(dev
);
158 /* might already be clear but that doesn't matter */
159 clear_bit(SDATA_STATE_RUNNING
, &sdata
->state
);
165 ieee802154_check_mac_settings(struct ieee802154_local
*local
,
166 struct wpan_dev
*wpan_dev
,
167 struct wpan_dev
*nwpan_dev
)
171 if (local
->hw
.flags
& IEEE802154_HW_PROMISCUOUS
) {
172 if (wpan_dev
->promiscuous_mode
!= nwpan_dev
->promiscuous_mode
)
176 if (local
->hw
.flags
& IEEE802154_HW_AFILT
) {
177 if (wpan_dev
->pan_id
!= nwpan_dev
->pan_id
||
178 wpan_dev
->short_addr
!= nwpan_dev
->short_addr
||
179 wpan_dev
->extended_addr
!= nwpan_dev
->extended_addr
)
183 if (local
->hw
.flags
& IEEE802154_HW_CSMA_PARAMS
) {
184 if (wpan_dev
->min_be
!= nwpan_dev
->min_be
||
185 wpan_dev
->max_be
!= nwpan_dev
->max_be
||
186 wpan_dev
->csma_retries
!= nwpan_dev
->csma_retries
)
190 if (local
->hw
.flags
& IEEE802154_HW_FRAME_RETRIES
) {
191 if (wpan_dev
->frame_retries
!= nwpan_dev
->frame_retries
)
195 if (local
->hw
.flags
& IEEE802154_HW_LBT
) {
196 if (wpan_dev
->lbt
!= nwpan_dev
->lbt
)
204 ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data
*sdata
,
205 enum nl802154_iftype iftype
)
207 struct ieee802154_local
*local
= sdata
->local
;
208 struct wpan_dev
*wpan_dev
= &sdata
->wpan_dev
;
209 struct ieee802154_sub_if_data
*nsdata
;
211 /* we hold the RTNL here so can safely walk the list */
212 list_for_each_entry(nsdata
, &local
->interfaces
, list
) {
213 if (nsdata
!= sdata
&& ieee802154_sdata_running(nsdata
)) {
216 /* TODO currently we don't support multiple node types
217 * we need to run skb_clone at rx path. Check if there
218 * exist really an use case if we need to support
219 * multiple node types at the same time.
221 if (sdata
->vif
.type
== NL802154_IFTYPE_NODE
&&
222 nsdata
->vif
.type
== NL802154_IFTYPE_NODE
)
225 /* check all phy mac sublayer settings are the same.
226 * We have only one phy, different values makes trouble.
228 ret
= ieee802154_check_mac_settings(local
, wpan_dev
,
238 static int mac802154_wpan_open(struct net_device
*dev
)
241 struct ieee802154_sub_if_data
*sdata
= IEEE802154_DEV_TO_SUB_IF(dev
);
242 struct ieee802154_local
*local
= sdata
->local
;
243 struct wpan_dev
*wpan_dev
= &sdata
->wpan_dev
;
244 struct wpan_phy
*phy
= sdata
->local
->phy
;
246 rc
= ieee802154_check_concurrent_iface(sdata
, sdata
->vif
.type
);
250 rc
= mac802154_slave_open(dev
);
254 mutex_lock(&phy
->pib_lock
);
256 if (local
->hw
.flags
& IEEE802154_HW_PROMISCUOUS
) {
257 rc
= drv_set_promiscuous_mode(local
,
258 wpan_dev
->promiscuous_mode
);
263 if (local
->hw
.flags
& IEEE802154_HW_AFILT
) {
264 rc
= drv_set_pan_id(local
, wpan_dev
->pan_id
);
268 rc
= drv_set_extended_addr(local
, wpan_dev
->extended_addr
);
272 rc
= drv_set_short_addr(local
, wpan_dev
->short_addr
);
277 if (local
->hw
.flags
& IEEE802154_HW_LBT
) {
278 rc
= drv_set_lbt_mode(local
, wpan_dev
->lbt
);
283 if (local
->hw
.flags
& IEEE802154_HW_CSMA_PARAMS
) {
284 rc
= drv_set_csma_params(local
, wpan_dev
->min_be
,
286 wpan_dev
->csma_retries
);
291 if (local
->hw
.flags
& IEEE802154_HW_FRAME_RETRIES
) {
292 rc
= drv_set_max_frame_retries(local
, wpan_dev
->frame_retries
);
297 mutex_unlock(&phy
->pib_lock
);
301 mutex_unlock(&phy
->pib_lock
);
305 static int mac802154_slave_close(struct net_device
*dev
)
307 struct ieee802154_sub_if_data
*sdata
= IEEE802154_DEV_TO_SUB_IF(dev
);
308 struct ieee802154_local
*local
= sdata
->local
;
312 hrtimer_cancel(&local
->ifs_timer
);
314 netif_stop_queue(dev
);
317 clear_bit(SDATA_STATE_RUNNING
, &sdata
->state
);
319 if (!local
->open_count
)
325 static int mac802154_set_header_security(struct ieee802154_sub_if_data
*sdata
,
326 struct ieee802154_hdr
*hdr
,
327 const struct ieee802154_mac_cb
*cb
)
329 struct ieee802154_llsec_params params
;
332 mac802154_llsec_get_params(&sdata
->sec
, ¶ms
);
334 if (!params
.enabled
&& cb
->secen_override
&& cb
->secen
)
336 if (!params
.enabled
||
337 (cb
->secen_override
&& !cb
->secen
) ||
340 if (cb
->seclevel_override
&& !cb
->seclevel
)
343 level
= cb
->seclevel_override
? cb
->seclevel
: params
.out_level
;
345 hdr
->fc
.security_enabled
= 1;
346 hdr
->sec
.level
= level
;
347 hdr
->sec
.key_id_mode
= params
.out_key
.mode
;
348 if (params
.out_key
.mode
== IEEE802154_SCF_KEY_SHORT_INDEX
)
349 hdr
->sec
.short_src
= params
.out_key
.short_source
;
350 else if (params
.out_key
.mode
== IEEE802154_SCF_KEY_HW_INDEX
)
351 hdr
->sec
.extended_src
= params
.out_key
.extended_source
;
352 hdr
->sec
.key_id
= params
.out_key
.id
;
357 static int mac802154_header_create(struct sk_buff
*skb
,
358 struct net_device
*dev
,
364 struct ieee802154_hdr hdr
;
365 struct ieee802154_sub_if_data
*sdata
= IEEE802154_DEV_TO_SUB_IF(dev
);
366 struct wpan_dev
*wpan_dev
= &sdata
->wpan_dev
;
367 struct ieee802154_mac_cb
*cb
= mac_cb(skb
);
373 memset(&hdr
.fc
, 0, sizeof(hdr
.fc
));
374 hdr
.fc
.type
= cb
->type
;
375 hdr
.fc
.security_enabled
= cb
->secen
;
376 hdr
.fc
.ack_request
= cb
->ackreq
;
377 hdr
.seq
= ieee802154_mlme_ops(dev
)->get_dsn(dev
);
379 if (mac802154_set_header_security(sdata
, &hdr
, cb
) < 0)
383 spin_lock_bh(&sdata
->mib_lock
);
385 if (wpan_dev
->short_addr
== cpu_to_le16(IEEE802154_ADDR_BROADCAST
) ||
386 wpan_dev
->short_addr
== cpu_to_le16(IEEE802154_ADDR_UNDEF
) ||
387 wpan_dev
->pan_id
== cpu_to_le16(IEEE802154_PANID_BROADCAST
)) {
388 hdr
.source
.mode
= IEEE802154_ADDR_LONG
;
389 hdr
.source
.extended_addr
= wpan_dev
->extended_addr
;
391 hdr
.source
.mode
= IEEE802154_ADDR_SHORT
;
392 hdr
.source
.short_addr
= wpan_dev
->short_addr
;
395 hdr
.source
.pan_id
= wpan_dev
->pan_id
;
397 spin_unlock_bh(&sdata
->mib_lock
);
399 hdr
.source
= *(const struct ieee802154_addr
*)saddr
;
402 hdr
.dest
= *(const struct ieee802154_addr
*)daddr
;
404 hlen
= ieee802154_hdr_push(skb
, &hdr
);
408 skb_reset_mac_header(skb
);
411 if (len
> ieee802154_max_payload(&hdr
))
418 mac802154_header_parse(const struct sk_buff
*skb
, unsigned char *haddr
)
420 struct ieee802154_hdr hdr
;
421 struct ieee802154_addr
*addr
= (struct ieee802154_addr
*)haddr
;
423 if (ieee802154_hdr_peek_addrs(skb
, &hdr
) < 0) {
424 pr_debug("malformed packet\n");
429 return sizeof(*addr
);
432 static struct header_ops mac802154_header_ops
= {
433 .create
= mac802154_header_create
,
434 .parse
= mac802154_header_parse
,
437 static const struct net_device_ops mac802154_wpan_ops
= {
438 .ndo_open
= mac802154_wpan_open
,
439 .ndo_stop
= mac802154_slave_close
,
440 .ndo_start_xmit
= ieee802154_subif_start_xmit
,
441 .ndo_do_ioctl
= mac802154_wpan_ioctl
,
442 .ndo_set_mac_address
= mac802154_wpan_mac_addr
,
445 static const struct net_device_ops mac802154_monitor_ops
= {
446 .ndo_open
= mac802154_wpan_open
,
447 .ndo_stop
= mac802154_slave_close
,
448 .ndo_start_xmit
= ieee802154_monitor_start_xmit
,
451 static void mac802154_wpan_free(struct net_device
*dev
)
453 struct ieee802154_sub_if_data
*sdata
= IEEE802154_DEV_TO_SUB_IF(dev
);
455 mac802154_llsec_destroy(&sdata
->sec
);
460 static void ieee802154_if_setup(struct net_device
*dev
)
462 dev
->addr_len
= IEEE802154_EXTENDED_ADDR_LEN
;
463 memset(dev
->broadcast
, 0xff, IEEE802154_EXTENDED_ADDR_LEN
);
465 dev
->hard_header_len
= MAC802154_FRAME_HARD_HEADER_LEN
;
466 dev
->needed_tailroom
= 2 + 16; /* FCS + MIC */
467 dev
->mtu
= IEEE802154_MTU
;
468 dev
->tx_queue_len
= 300;
469 dev
->flags
= IFF_NOARP
| IFF_BROADCAST
;
473 ieee802154_setup_sdata(struct ieee802154_sub_if_data
*sdata
,
474 enum nl802154_iftype type
)
476 struct wpan_dev
*wpan_dev
= &sdata
->wpan_dev
;
478 /* set some type-dependent values */
479 sdata
->vif
.type
= type
;
480 sdata
->wpan_dev
.iftype
= type
;
482 get_random_bytes(&wpan_dev
->bsn
, 1);
483 get_random_bytes(&wpan_dev
->dsn
, 1);
485 /* defaults per 802.15.4-2011 */
486 wpan_dev
->min_be
= 3;
487 wpan_dev
->max_be
= 5;
488 wpan_dev
->csma_retries
= 4;
489 /* for compatibility, actual default is 3 */
490 wpan_dev
->frame_retries
= -1;
492 wpan_dev
->pan_id
= cpu_to_le16(IEEE802154_PANID_BROADCAST
);
493 wpan_dev
->short_addr
= cpu_to_le16(IEEE802154_ADDR_BROADCAST
);
496 case NL802154_IFTYPE_NODE
:
497 ieee802154_be64_to_le64(&wpan_dev
->extended_addr
,
498 sdata
->dev
->dev_addr
);
500 sdata
->dev
->header_ops
= &mac802154_header_ops
;
501 sdata
->dev
->destructor
= mac802154_wpan_free
;
502 sdata
->dev
->netdev_ops
= &mac802154_wpan_ops
;
503 sdata
->dev
->ml_priv
= &mac802154_mlme_wpan
;
504 wpan_dev
->promiscuous_mode
= false;
506 spin_lock_init(&sdata
->mib_lock
);
507 mutex_init(&sdata
->sec_mtx
);
509 mac802154_llsec_init(&sdata
->sec
);
511 case NL802154_IFTYPE_MONITOR
:
512 sdata
->dev
->destructor
= free_netdev
;
513 sdata
->dev
->netdev_ops
= &mac802154_monitor_ops
;
514 wpan_dev
->promiscuous_mode
= true;
524 ieee802154_if_add(struct ieee802154_local
*local
, const char *name
,
525 unsigned char name_assign_type
, enum nl802154_iftype type
,
526 __le64 extended_addr
)
528 struct net_device
*ndev
= NULL
;
529 struct ieee802154_sub_if_data
*sdata
= NULL
;
534 ndev
= alloc_netdev(sizeof(*sdata
) + local
->hw
.vif_data_size
, name
,
535 name_assign_type
, ieee802154_if_setup
);
537 return ERR_PTR(-ENOMEM
);
539 ndev
->needed_headroom
= local
->hw
.extra_tx_headroom
;
541 ret
= dev_alloc_name(ndev
, ndev
->name
);
545 ieee802154_le64_to_be64(ndev
->perm_addr
,
546 &local
->hw
.phy
->perm_extended_addr
);
548 case NL802154_IFTYPE_NODE
:
549 ndev
->type
= ARPHRD_IEEE802154
;
550 if (ieee802154_is_valid_extended_addr(extended_addr
))
551 ieee802154_le64_to_be64(ndev
->dev_addr
, &extended_addr
);
553 memcpy(ndev
->dev_addr
, ndev
->perm_addr
,
554 IEEE802154_EXTENDED_ADDR_LEN
);
556 case NL802154_IFTYPE_MONITOR
:
557 ndev
->type
= ARPHRD_IEEE802154_MONITOR
;
564 /* TODO check this */
565 SET_NETDEV_DEV(ndev
, &local
->phy
->dev
);
566 sdata
= netdev_priv(ndev
);
567 ndev
->ieee802154_ptr
= &sdata
->wpan_dev
;
568 memcpy(sdata
->name
, ndev
->name
, IFNAMSIZ
);
570 sdata
->wpan_dev
.wpan_phy
= local
->hw
.phy
;
571 sdata
->local
= local
;
573 /* setup type-dependent data */
574 ret
= ieee802154_setup_sdata(sdata
, type
);
578 ret
= register_netdevice(ndev
);
582 mutex_lock(&local
->iflist_mtx
);
583 list_add_tail_rcu(&sdata
->list
, &local
->interfaces
);
584 mutex_unlock(&local
->iflist_mtx
);
593 void ieee802154_if_remove(struct ieee802154_sub_if_data
*sdata
)
597 mutex_lock(&sdata
->local
->iflist_mtx
);
598 list_del_rcu(&sdata
->list
);
599 mutex_unlock(&sdata
->local
->iflist_mtx
);
602 unregister_netdevice(sdata
->dev
);
605 void ieee802154_remove_interfaces(struct ieee802154_local
*local
)
607 struct ieee802154_sub_if_data
*sdata
, *tmp
;
609 mutex_lock(&local
->iflist_mtx
);
610 list_for_each_entry_safe(sdata
, tmp
, &local
->interfaces
, list
) {
611 list_del(&sdata
->list
);
613 unregister_netdevice(sdata
->dev
);
615 mutex_unlock(&local
->iflist_mtx
);
618 static int netdev_notify(struct notifier_block
*nb
,
619 unsigned long state
, void *ptr
)
621 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
622 struct ieee802154_sub_if_data
*sdata
;
624 if (state
!= NETDEV_CHANGENAME
)
627 if (!dev
->ieee802154_ptr
|| !dev
->ieee802154_ptr
->wpan_phy
)
630 if (dev
->ieee802154_ptr
->wpan_phy
->privid
!= mac802154_wpan_phy_privid
)
633 sdata
= IEEE802154_DEV_TO_SUB_IF(dev
);
634 memcpy(sdata
->name
, dev
->name
, IFNAMSIZ
);
639 static struct notifier_block mac802154_netdev_notifier
= {
640 .notifier_call
= netdev_notify
,
643 int ieee802154_iface_init(void)
645 return register_netdevice_notifier(&mac802154_netdev_notifier
);
648 void ieee802154_iface_exit(void)
650 unregister_netdevice_notifier(&mac802154_netdev_notifier
);