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.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
19 * Sergey Lapin <slapin@ossfans.org>
20 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
21 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
24 #include <linux/netdevice.h>
25 #include <linux/module.h>
26 #include <linux/if_arp.h>
28 #include <net/rtnetlink.h>
29 #include <linux/nl802154.h>
30 #include <net/af_ieee802154.h>
31 #include <net/mac802154.h>
32 #include <net/ieee802154_netdev.h>
33 #include <net/ieee802154.h>
34 #include <net/wpan-phy.h>
36 #include "mac802154.h"
38 static inline int mac802154_fetch_skb_u8(struct sk_buff
*skb
, u8
*val
)
40 if (unlikely(!pskb_may_pull(skb
, 1)))
49 static inline int mac802154_fetch_skb_u16(struct sk_buff
*skb
, u16
*val
)
51 if (unlikely(!pskb_may_pull(skb
, 2)))
54 *val
= skb
->data
[0] | (skb
->data
[1] << 8);
60 static inline void mac802154_haddr_copy_swap(u8
*dest
, const u8
*src
)
63 for (i
= 0; i
< IEEE802154_ADDR_LEN
; i
++)
64 dest
[IEEE802154_ADDR_LEN
- i
- 1] = src
[i
];
68 mac802154_wpan_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
70 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
71 struct sockaddr_ieee802154
*sa
=
72 (struct sockaddr_ieee802154
*)&ifr
->ifr_addr
;
73 int err
= -ENOIOCTLCMD
;
75 spin_lock_bh(&priv
->mib_lock
);
79 if (priv
->pan_id
== IEEE802154_PANID_BROADCAST
||
80 priv
->short_addr
== IEEE802154_ADDR_BROADCAST
) {
85 sa
->family
= AF_IEEE802154
;
86 sa
->addr
.addr_type
= IEEE802154_ADDR_SHORT
;
87 sa
->addr
.pan_id
= priv
->pan_id
;
88 sa
->addr
.short_addr
= priv
->short_addr
;
94 "Using DEBUGing ioctl SIOCSIFADDR isn't recommened!\n");
95 if (sa
->family
!= AF_IEEE802154
||
96 sa
->addr
.addr_type
!= IEEE802154_ADDR_SHORT
||
97 sa
->addr
.pan_id
== IEEE802154_PANID_BROADCAST
||
98 sa
->addr
.short_addr
== IEEE802154_ADDR_BROADCAST
||
99 sa
->addr
.short_addr
== IEEE802154_ADDR_UNDEF
) {
104 priv
->pan_id
= sa
->addr
.pan_id
;
105 priv
->short_addr
= sa
->addr
.short_addr
;
111 spin_unlock_bh(&priv
->mib_lock
);
115 static int mac802154_wpan_mac_addr(struct net_device
*dev
, void *p
)
117 struct sockaddr
*addr
= p
;
119 if (netif_running(dev
))
122 /* FIXME: validate addr */
123 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
124 mac802154_dev_set_ieee_addr(dev
);
128 static int mac802154_header_create(struct sk_buff
*skb
,
129 struct net_device
*dev
,
135 const struct ieee802154_addr
*saddr
= _saddr
;
136 const struct ieee802154_addr
*daddr
= _daddr
;
137 struct ieee802154_addr dev_addr
;
138 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
146 head
= kzalloc(MAC802154_FRAME_HARD_HEADER_LEN
, GFP_KERNEL
);
150 head
[pos
++] = mac_cb(skb
)->seq
; /* DSN/BSN */
151 fc
= mac_cb_type(skb
);
154 spin_lock_bh(&priv
->mib_lock
);
156 if (priv
->short_addr
== IEEE802154_ADDR_BROADCAST
||
157 priv
->short_addr
== IEEE802154_ADDR_UNDEF
||
158 priv
->pan_id
== IEEE802154_PANID_BROADCAST
) {
159 dev_addr
.addr_type
= IEEE802154_ADDR_LONG
;
160 memcpy(dev_addr
.hwaddr
, dev
->dev_addr
,
161 IEEE802154_ADDR_LEN
);
163 dev_addr
.addr_type
= IEEE802154_ADDR_SHORT
;
164 dev_addr
.short_addr
= priv
->short_addr
;
167 dev_addr
.pan_id
= priv
->pan_id
;
170 spin_unlock_bh(&priv
->mib_lock
);
173 if (daddr
->addr_type
!= IEEE802154_ADDR_NONE
) {
174 fc
|= (daddr
->addr_type
<< IEEE802154_FC_DAMODE_SHIFT
);
176 head
[pos
++] = daddr
->pan_id
& 0xff;
177 head
[pos
++] = daddr
->pan_id
>> 8;
179 if (daddr
->addr_type
== IEEE802154_ADDR_SHORT
) {
180 head
[pos
++] = daddr
->short_addr
& 0xff;
181 head
[pos
++] = daddr
->short_addr
>> 8;
183 mac802154_haddr_copy_swap(head
+ pos
, daddr
->hwaddr
);
184 pos
+= IEEE802154_ADDR_LEN
;
188 if (saddr
->addr_type
!= IEEE802154_ADDR_NONE
) {
189 fc
|= (saddr
->addr_type
<< IEEE802154_FC_SAMODE_SHIFT
);
191 if ((saddr
->pan_id
== daddr
->pan_id
) &&
192 (saddr
->pan_id
!= IEEE802154_PANID_BROADCAST
)) {
193 /* PANID compression/intra PAN */
194 fc
|= IEEE802154_FC_INTRA_PAN
;
196 head
[pos
++] = saddr
->pan_id
& 0xff;
197 head
[pos
++] = saddr
->pan_id
>> 8;
200 if (saddr
->addr_type
== IEEE802154_ADDR_SHORT
) {
201 head
[pos
++] = saddr
->short_addr
& 0xff;
202 head
[pos
++] = saddr
->short_addr
>> 8;
204 mac802154_haddr_copy_swap(head
+ pos
, saddr
->hwaddr
);
205 pos
+= IEEE802154_ADDR_LEN
;
212 memcpy(skb_push(skb
, pos
), head
, pos
);
219 mac802154_header_parse(const struct sk_buff
*skb
, unsigned char *haddr
)
221 const u8
*hdr
= skb_mac_header(skb
);
222 const u8
*tail
= skb_tail_pointer(skb
);
223 struct ieee802154_addr
*addr
= (struct ieee802154_addr
*)haddr
;
230 fc
= hdr
[0] | (hdr
[1] << 8);
234 da_type
= IEEE802154_FC_DAMODE(fc
);
235 addr
->addr_type
= IEEE802154_FC_SAMODE(fc
);
238 case IEEE802154_ADDR_NONE
:
239 if (fc
& IEEE802154_FC_INTRA_PAN
)
242 case IEEE802154_ADDR_LONG
:
243 if (fc
& IEEE802154_FC_INTRA_PAN
) {
246 addr
->pan_id
= hdr
[0] | (hdr
[1] << 8);
250 if (hdr
+ IEEE802154_ADDR_LEN
> tail
)
253 hdr
+= IEEE802154_ADDR_LEN
;
255 case IEEE802154_ADDR_SHORT
:
256 if (fc
& IEEE802154_FC_INTRA_PAN
) {
259 addr
->pan_id
= hdr
[0] | (hdr
[1] << 8);
273 switch (addr
->addr_type
) {
274 case IEEE802154_ADDR_NONE
:
276 case IEEE802154_ADDR_LONG
:
277 if (!(fc
& IEEE802154_FC_INTRA_PAN
)) {
280 addr
->pan_id
= hdr
[0] | (hdr
[1] << 8);
284 if (hdr
+ IEEE802154_ADDR_LEN
> tail
)
287 mac802154_haddr_copy_swap(addr
->hwaddr
, hdr
);
288 hdr
+= IEEE802154_ADDR_LEN
;
290 case IEEE802154_ADDR_SHORT
:
291 if (!(fc
& IEEE802154_FC_INTRA_PAN
)) {
294 addr
->pan_id
= hdr
[0] | (hdr
[1] << 8);
301 addr
->short_addr
= hdr
[0] | (hdr
[1] << 8);
308 return sizeof(struct ieee802154_addr
);
311 pr_debug("malformed packet\n");
316 mac802154_wpan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
318 struct mac802154_sub_if_data
*priv
;
321 priv
= netdev_priv(dev
);
323 spin_lock_bh(&priv
->mib_lock
);
326 spin_unlock_bh(&priv
->mib_lock
);
328 if (chan
== MAC802154_CHAN_NONE
||
329 page
>= WPAN_NUM_PAGES
||
330 chan
>= WPAN_NUM_CHANNELS
)
333 skb
->skb_iif
= dev
->ifindex
;
334 dev
->stats
.tx_packets
++;
335 dev
->stats
.tx_bytes
+= skb
->len
;
337 return mac802154_tx(priv
->hw
, skb
, page
, chan
);
340 static struct header_ops mac802154_header_ops
= {
341 .create
= mac802154_header_create
,
342 .parse
= mac802154_header_parse
,
345 static const struct net_device_ops mac802154_wpan_ops
= {
346 .ndo_open
= mac802154_slave_open
,
347 .ndo_stop
= mac802154_slave_close
,
348 .ndo_start_xmit
= mac802154_wpan_xmit
,
349 .ndo_do_ioctl
= mac802154_wpan_ioctl
,
350 .ndo_set_mac_address
= mac802154_wpan_mac_addr
,
353 void mac802154_wpan_setup(struct net_device
*dev
)
355 struct mac802154_sub_if_data
*priv
;
357 dev
->addr_len
= IEEE802154_ADDR_LEN
;
358 memset(dev
->broadcast
, 0xff, IEEE802154_ADDR_LEN
);
360 dev
->hard_header_len
= MAC802154_FRAME_HARD_HEADER_LEN
;
361 dev
->header_ops
= &mac802154_header_ops
;
362 dev
->needed_tailroom
= 2; /* FCS */
363 dev
->mtu
= IEEE802154_MTU
;
364 dev
->tx_queue_len
= 10;
365 dev
->type
= ARPHRD_IEEE802154
;
366 dev
->flags
= IFF_NOARP
| IFF_BROADCAST
;
367 dev
->watchdog_timeo
= 0;
369 dev
->destructor
= free_netdev
;
370 dev
->netdev_ops
= &mac802154_wpan_ops
;
371 dev
->ml_priv
= &mac802154_mlme_wpan
;
373 priv
= netdev_priv(dev
);
374 priv
->type
= IEEE802154_DEV_WPAN
;
376 priv
->chan
= MAC802154_CHAN_NONE
;
379 spin_lock_init(&priv
->mib_lock
);
381 get_random_bytes(&priv
->bsn
, 1);
382 get_random_bytes(&priv
->dsn
, 1);
384 priv
->pan_id
= IEEE802154_PANID_BROADCAST
;
385 priv
->short_addr
= IEEE802154_ADDR_BROADCAST
;
388 static int mac802154_process_data(struct net_device
*dev
, struct sk_buff
*skb
)
390 return netif_rx(skb
);
394 mac802154_subif_frame(struct mac802154_sub_if_data
*sdata
, struct sk_buff
*skb
)
396 pr_debug("getting packet via slave interface %s\n", sdata
->dev
->name
);
398 spin_lock_bh(&sdata
->mib_lock
);
400 switch (mac_cb(skb
)->da
.addr_type
) {
401 case IEEE802154_ADDR_NONE
:
402 if (mac_cb(skb
)->sa
.addr_type
!= IEEE802154_ADDR_NONE
)
403 /* FIXME: check if we are PAN coordinator */
404 skb
->pkt_type
= PACKET_OTHERHOST
;
406 /* ACK comes with both addresses empty */
407 skb
->pkt_type
= PACKET_HOST
;
409 case IEEE802154_ADDR_LONG
:
410 if (mac_cb(skb
)->da
.pan_id
!= sdata
->pan_id
&&
411 mac_cb(skb
)->da
.pan_id
!= IEEE802154_PANID_BROADCAST
)
412 skb
->pkt_type
= PACKET_OTHERHOST
;
413 else if (!memcmp(mac_cb(skb
)->da
.hwaddr
, sdata
->dev
->dev_addr
,
414 IEEE802154_ADDR_LEN
))
415 skb
->pkt_type
= PACKET_HOST
;
417 skb
->pkt_type
= PACKET_OTHERHOST
;
419 case IEEE802154_ADDR_SHORT
:
420 if (mac_cb(skb
)->da
.pan_id
!= sdata
->pan_id
&&
421 mac_cb(skb
)->da
.pan_id
!= IEEE802154_PANID_BROADCAST
)
422 skb
->pkt_type
= PACKET_OTHERHOST
;
423 else if (mac_cb(skb
)->da
.short_addr
== sdata
->short_addr
)
424 skb
->pkt_type
= PACKET_HOST
;
425 else if (mac_cb(skb
)->da
.short_addr
==
426 IEEE802154_ADDR_BROADCAST
)
427 skb
->pkt_type
= PACKET_BROADCAST
;
429 skb
->pkt_type
= PACKET_OTHERHOST
;
435 spin_unlock_bh(&sdata
->mib_lock
);
437 skb
->dev
= sdata
->dev
;
439 sdata
->dev
->stats
.rx_packets
++;
440 sdata
->dev
->stats
.rx_bytes
+= skb
->len
;
442 switch (mac_cb_type(skb
)) {
443 case IEEE802154_FC_TYPE_DATA
:
444 return mac802154_process_data(sdata
->dev
, skb
);
446 pr_warning("ieee802154: bad frame received (type = %d)\n",
453 static int mac802154_parse_frame_start(struct sk_buff
*skb
)
455 u8
*head
= skb
->data
;
458 if (mac802154_fetch_skb_u16(skb
, &fc
) ||
459 mac802154_fetch_skb_u8(skb
, &(mac_cb(skb
)->seq
)))
462 pr_debug("fc: %04x dsn: %02x\n", fc
, head
[2]);
464 mac_cb(skb
)->flags
= IEEE802154_FC_TYPE(fc
);
465 mac_cb(skb
)->sa
.addr_type
= IEEE802154_FC_SAMODE(fc
);
466 mac_cb(skb
)->da
.addr_type
= IEEE802154_FC_DAMODE(fc
);
468 if (fc
& IEEE802154_FC_INTRA_PAN
)
469 mac_cb(skb
)->flags
|= MAC_CB_FLAG_INTRAPAN
;
471 if (mac_cb(skb
)->da
.addr_type
!= IEEE802154_ADDR_NONE
) {
472 if (mac802154_fetch_skb_u16(skb
, &(mac_cb(skb
)->da
.pan_id
)))
475 /* source PAN id compression */
476 if (mac_cb_is_intrapan(skb
))
477 mac_cb(skb
)->sa
.pan_id
= mac_cb(skb
)->da
.pan_id
;
479 pr_debug("dest PAN addr: %04x\n", mac_cb(skb
)->da
.pan_id
);
481 if (mac_cb(skb
)->da
.addr_type
== IEEE802154_ADDR_SHORT
) {
482 u16
*da
= &(mac_cb(skb
)->da
.short_addr
);
484 if (mac802154_fetch_skb_u16(skb
, da
))
487 pr_debug("destination address is short: %04x\n",
488 mac_cb(skb
)->da
.short_addr
);
490 if (!pskb_may_pull(skb
, IEEE802154_ADDR_LEN
))
493 mac802154_haddr_copy_swap(mac_cb(skb
)->da
.hwaddr
,
495 skb_pull(skb
, IEEE802154_ADDR_LEN
);
497 pr_debug("destination address is hardware\n");
501 if (mac_cb(skb
)->sa
.addr_type
!= IEEE802154_ADDR_NONE
) {
502 /* non PAN-compression, fetch source address id */
503 if (!(mac_cb_is_intrapan(skb
))) {
504 u16
*sa_pan
= &(mac_cb(skb
)->sa
.pan_id
);
506 if (mac802154_fetch_skb_u16(skb
, sa_pan
))
510 pr_debug("source PAN addr: %04x\n", mac_cb(skb
)->da
.pan_id
);
512 if (mac_cb(skb
)->sa
.addr_type
== IEEE802154_ADDR_SHORT
) {
513 u16
*sa
= &(mac_cb(skb
)->sa
.short_addr
);
515 if (mac802154_fetch_skb_u16(skb
, sa
))
518 pr_debug("source address is short: %04x\n",
519 mac_cb(skb
)->sa
.short_addr
);
521 if (!pskb_may_pull(skb
, IEEE802154_ADDR_LEN
))
524 mac802154_haddr_copy_swap(mac_cb(skb
)->sa
.hwaddr
,
526 skb_pull(skb
, IEEE802154_ADDR_LEN
);
528 pr_debug("source address is hardware\n");
537 void mac802154_wpans_rx(struct mac802154_priv
*priv
, struct sk_buff
*skb
)
540 struct sk_buff
*sskb
;
541 struct mac802154_sub_if_data
*sdata
;
543 ret
= mac802154_parse_frame_start(skb
);
545 pr_debug("got invalid frame\n");
550 list_for_each_entry_rcu(sdata
, &priv
->slaves
, list
) {
551 if (sdata
->type
!= IEEE802154_DEV_WPAN
)
554 sskb
= skb_clone(skb
, GFP_ATOMIC
);
556 mac802154_subif_frame(sdata
, sskb
);