2 * Copyright 2011, Siemens AG
3 * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
7 * Based on patches from Jon Smirl <jonsmirl@gmail.com>
8 * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 /* Jon's code is based on 6lowpan implementation for Contiki which is:
25 * Copyright (c) 2008, Swedish Institute of Computer Science.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the Institute nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 #include <linux/bitops.h>
54 #include <linux/if_arp.h>
55 #include <linux/module.h>
56 #include <linux/moduleparam.h>
57 #include <linux/netdevice.h>
58 #include <net/af_ieee802154.h>
59 #include <net/ieee802154.h>
60 #include <net/ieee802154_netdev.h>
65 /* TTL uncompression values */
66 static const u8 lowpan_ttl_values
[] = {0, 1, 64, 255};
68 static LIST_HEAD(lowpan_devices
);
71 * Uncompression of linklocal:
72 * 0 -> 16 bytes from packet
73 * 1 -> 2 bytes from prefix - bunch of zeroes and 8 from packet
74 * 2 -> 2 bytes from prefix - zeroes + 2 from packet
75 * 3 -> 2 bytes from prefix - infer 8 bytes from lladdr
77 * NOTE: => the uncompress function does change 0xf to 0x10
78 * NOTE: 0x00 => no-autoconfig => unspecified
80 static const u8 lowpan_unc_llconf
[] = {0x0f, 0x28, 0x22, 0x20};
83 * Uncompression of ctx-based:
84 * 0 -> 0 bits from packet [unspecified / reserved]
85 * 1 -> 8 bytes from prefix - bunch of zeroes and 8 from packet
86 * 2 -> 8 bytes from prefix - zeroes + 2 from packet
87 * 3 -> 8 bytes from prefix - infer 8 bytes from lladdr
89 static const u8 lowpan_unc_ctxconf
[] = {0x00, 0x88, 0x82, 0x80};
92 * Uncompression of ctx-base
93 * 0 -> 0 bits from packet
94 * 1 -> 2 bytes from prefix - bunch of zeroes 5 from packet
95 * 2 -> 2 bytes from prefix - zeroes + 3 from packet
96 * 3 -> 2 bytes from prefix - infer 1 bytes from lladdr
98 static const u8 lowpan_unc_mxconf
[] = {0x0f, 0x25, 0x23, 0x21};
100 /* Link local prefix */
101 static const u8 lowpan_llprefix
[] = {0xfe, 0x80};
103 /* private device info */
104 struct lowpan_dev_info
{
105 struct net_device
*real_dev
; /* real WPAN device ptr */
106 struct mutex dev_list_mtx
; /* mutex for list ops */
109 struct lowpan_dev_record
{
110 struct net_device
*ldev
;
111 struct list_head list
;
114 struct lowpan_fragment
{
115 struct sk_buff
*skb
; /* skb to be assembled */
116 spinlock_t lock
; /* concurency lock */
117 u16 length
; /* length to be assemled */
118 u32 bytes_rcv
; /* bytes received */
119 u16 tag
; /* current fragment tag */
120 struct timer_list timer
; /* assembling timer */
121 struct list_head list
; /* fragments list */
124 static unsigned short fragment_tag
;
125 static LIST_HEAD(lowpan_fragments
);
126 spinlock_t flist_lock
;
129 lowpan_dev_info
*lowpan_dev_info(const struct net_device
*dev
)
131 return netdev_priv(dev
);
134 static inline void lowpan_address_flip(u8
*src
, u8
*dest
)
137 for (i
= 0; i
< IEEE802154_ADDR_LEN
; i
++)
138 (dest
)[IEEE802154_ADDR_LEN
- i
- 1] = (src
)[i
];
141 /* list of all 6lowpan devices, uses for package delivering */
142 /* print data in line */
143 static inline void lowpan_raw_dump_inline(const char *caller
, char *msg
,
144 unsigned char *buf
, int len
)
148 pr_debug("(%s) %s: ", caller
, msg
);
149 print_hex_dump(KERN_DEBUG
, "", DUMP_PREFIX_NONE
,
150 16, 1, buf
, len
, false);
155 * print data in a table format:
157 * addr: xx xx xx xx xx xx
158 * addr: xx xx xx xx xx xx
161 static inline void lowpan_raw_dump_table(const char *caller
, char *msg
,
162 unsigned char *buf
, int len
)
166 pr_debug("(%s) %s:\n", caller
, msg
);
167 print_hex_dump(KERN_DEBUG
, "\t", DUMP_PREFIX_OFFSET
,
168 16, 1, buf
, len
, false);
173 lowpan_compress_addr_64(u8
**hc06_ptr
, u8 shift
, const struct in6_addr
*ipaddr
,
174 const unsigned char *lladdr
)
178 if (is_addr_mac_addr_based(ipaddr
, lladdr
))
179 val
= 3; /* 0-bits */
180 else if (lowpan_is_iid_16_bit_compressable(ipaddr
)) {
181 /* compress IID to 16 bits xxxx::XXXX */
182 memcpy(*hc06_ptr
, &ipaddr
->s6_addr16
[7], 2);
184 val
= 2; /* 16-bits */
186 /* do not compress IID => xxxx::IID */
187 memcpy(*hc06_ptr
, &ipaddr
->s6_addr16
[4], 8);
189 val
= 1; /* 64-bits */
192 return rol8(val
, shift
);
196 lowpan_uip_ds6_set_addr_iid(struct in6_addr
*ipaddr
, unsigned char *lladdr
)
198 memcpy(&ipaddr
->s6_addr
[8], lladdr
, IEEE802154_ALEN
);
199 /* second bit-flip (Universe/Local) is done according RFC2464 */
200 ipaddr
->s6_addr
[8] ^= 0x02;
204 * Uncompress addresses based on a prefix and a postfix with zeroes in
205 * between. If the postfix is zero in length it will use the link address
206 * to configure the IP address (autoconf style).
207 * pref_post_count takes a byte where the first nibble specify prefix count
208 * and the second postfix count (NOTE: 15/0xf => 16 bytes copy).
211 lowpan_uncompress_addr(struct sk_buff
*skb
, struct in6_addr
*ipaddr
,
212 u8
const *prefix
, u8 pref_post_count
, unsigned char *lladdr
)
214 u8 prefcount
= pref_post_count
>> 4;
215 u8 postcount
= pref_post_count
& 0x0f;
217 /* full nibble 15 => 16 */
218 prefcount
= (prefcount
== 15 ? 16 : prefcount
);
219 postcount
= (postcount
== 15 ? 16 : postcount
);
222 lowpan_raw_dump_inline(__func__
, "linklocal address",
223 lladdr
, IEEE802154_ALEN
);
225 memcpy(ipaddr
, prefix
, prefcount
);
227 if (prefcount
+ postcount
< 16)
228 memset(&ipaddr
->s6_addr
[prefcount
], 0,
229 16 - (prefcount
+ postcount
));
232 memcpy(&ipaddr
->s6_addr
[16 - postcount
], skb
->data
, postcount
);
233 skb_pull(skb
, postcount
);
234 } else if (prefcount
> 0) {
238 /* no IID based configuration if no prefix and no data */
239 lowpan_uip_ds6_set_addr_iid(ipaddr
, lladdr
);
242 pr_debug("(%s): uncompressing %d + %d => ", __func__
, prefcount
,
244 lowpan_raw_dump_inline(NULL
, NULL
, ipaddr
->s6_addr
, 16);
249 static u8
lowpan_fetch_skb_u8(struct sk_buff
*skb
)
259 static u16
lowpan_fetch_skb_u16(struct sk_buff
*skb
)
263 BUG_ON(!pskb_may_pull(skb
, 2));
265 ret
= skb
->data
[0] | (skb
->data
[1] << 8);
270 static int lowpan_header_create(struct sk_buff
*skb
,
271 struct net_device
*dev
,
272 unsigned short type
, const void *_daddr
,
273 const void *_saddr
, unsigned len
)
275 u8 tmp
, iphc0
, iphc1
, *hc06_ptr
;
277 const u8
*saddr
= _saddr
;
278 const u8
*daddr
= _daddr
;
280 struct ieee802154_addr sa
, da
;
282 if (type
!= ETH_P_IPV6
)
285 * if this package isn't ipv6 one, where should it be routed?
287 head
= kzalloc(100, GFP_KERNEL
);
294 pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
295 "\tnexthdr = 0x%02x\n\thop_lim = %d\n", __func__
,
296 hdr
->version
, ntohs(hdr
->payload_len
), hdr
->nexthdr
,
299 lowpan_raw_dump_table(__func__
, "raw skb network header dump",
300 skb_network_header(skb
), sizeof(struct ipv6hdr
));
303 saddr
= dev
->dev_addr
;
305 lowpan_raw_dump_inline(__func__
, "saddr", (unsigned char *)saddr
, 8);
308 * As we copy some bit-length fields, in the IPHC encoding bytes,
309 * we sometimes use |=
310 * If the field is 0, and the current bit value in memory is 1,
311 * this does not work. We therefore reset the IPHC encoding here
313 iphc0
= LOWPAN_DISPATCH_IPHC
;
316 /* TODO: context lookup */
318 lowpan_raw_dump_inline(__func__
, "daddr", (unsigned char *)daddr
, 8);
321 * Traffic class, flow label
322 * If flow label is 0, compress it. If traffic class is 0, compress it
323 * We have to process both in the same time as the offset of traffic
324 * class depends on the presence of version and flow label
327 /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
328 tmp
= (hdr
->priority
<< 4) | (hdr
->flow_lbl
[0] >> 4);
329 tmp
= ((tmp
& 0x03) << 6) | (tmp
>> 2);
331 if (((hdr
->flow_lbl
[0] & 0x0F) == 0) &&
332 (hdr
->flow_lbl
[1] == 0) && (hdr
->flow_lbl
[2] == 0)) {
333 /* flow label can be compressed */
334 iphc0
|= LOWPAN_IPHC_FL_C
;
335 if ((hdr
->priority
== 0) &&
336 ((hdr
->flow_lbl
[0] & 0xF0) == 0)) {
337 /* compress (elide) all */
338 iphc0
|= LOWPAN_IPHC_TC_C
;
340 /* compress only the flow label */
345 /* Flow label cannot be compressed */
346 if ((hdr
->priority
== 0) &&
347 ((hdr
->flow_lbl
[0] & 0xF0) == 0)) {
348 /* compress only traffic class */
349 iphc0
|= LOWPAN_IPHC_TC_C
;
350 *hc06_ptr
= (tmp
& 0xc0) | (hdr
->flow_lbl
[0] & 0x0F);
351 memcpy(hc06_ptr
+ 1, &hdr
->flow_lbl
[1], 2);
354 /* compress nothing */
355 memcpy(hc06_ptr
, &hdr
, 4);
356 /* replace the top byte with new ECN | DSCP format */
362 /* NOTE: payload length is always compressed */
364 /* Next Header is compress if UDP */
365 if (hdr
->nexthdr
== UIP_PROTO_UDP
)
366 iphc0
|= LOWPAN_IPHC_NH_C
;
368 /* TODO: next header compression */
370 if ((iphc0
& LOWPAN_IPHC_NH_C
) == 0) {
371 *hc06_ptr
= hdr
->nexthdr
;
377 * if 1: compress, encoding is 01
378 * if 64: compress, encoding is 10
379 * if 255: compress, encoding is 11
380 * else do not compress
382 switch (hdr
->hop_limit
) {
384 iphc0
|= LOWPAN_IPHC_TTL_1
;
387 iphc0
|= LOWPAN_IPHC_TTL_64
;
390 iphc0
|= LOWPAN_IPHC_TTL_255
;
393 *hc06_ptr
= hdr
->hop_limit
;
397 /* source address compression */
398 if (is_addr_unspecified(&hdr
->saddr
)) {
399 pr_debug("(%s): source address is unspecified, setting SAC\n",
401 iphc1
|= LOWPAN_IPHC_SAC
;
402 /* TODO: context lookup */
403 } else if (is_addr_link_local(&hdr
->saddr
)) {
404 pr_debug("(%s): source address is link-local\n", __func__
);
405 iphc1
|= lowpan_compress_addr_64(&hc06_ptr
,
406 LOWPAN_IPHC_SAM_BIT
, &hdr
->saddr
, saddr
);
408 pr_debug("(%s): send the full source address\n", __func__
);
409 memcpy(hc06_ptr
, &hdr
->saddr
.s6_addr16
[0], 16);
413 /* destination address compression */
414 if (is_addr_mcast(&hdr
->daddr
)) {
415 pr_debug("(%s): destination address is multicast", __func__
);
416 iphc1
|= LOWPAN_IPHC_M
;
417 if (lowpan_is_mcast_addr_compressable8(&hdr
->daddr
)) {
418 pr_debug("compressed to 1 octet\n");
419 iphc1
|= LOWPAN_IPHC_DAM_11
;
421 *hc06_ptr
= hdr
->daddr
.s6_addr
[15];
423 } else if (lowpan_is_mcast_addr_compressable32(&hdr
->daddr
)) {
424 pr_debug("compressed to 4 octets\n");
425 iphc1
|= LOWPAN_IPHC_DAM_10
;
426 /* second byte + the last three */
427 *hc06_ptr
= hdr
->daddr
.s6_addr
[1];
428 memcpy(hc06_ptr
+ 1, &hdr
->daddr
.s6_addr
[13], 3);
430 } else if (lowpan_is_mcast_addr_compressable48(&hdr
->daddr
)) {
431 pr_debug("compressed to 6 octets\n");
432 iphc1
|= LOWPAN_IPHC_DAM_01
;
433 /* second byte + the last five */
434 *hc06_ptr
= hdr
->daddr
.s6_addr
[1];
435 memcpy(hc06_ptr
+ 1, &hdr
->daddr
.s6_addr
[11], 5);
438 pr_debug("using full address\n");
439 iphc1
|= LOWPAN_IPHC_DAM_00
;
440 memcpy(hc06_ptr
, &hdr
->daddr
.s6_addr
[0], 16);
444 pr_debug("(%s): destination address is unicast: ", __func__
);
445 /* TODO: context lookup */
446 if (is_addr_link_local(&hdr
->daddr
)) {
447 pr_debug("destination address is link-local\n");
448 iphc1
|= lowpan_compress_addr_64(&hc06_ptr
,
449 LOWPAN_IPHC_DAM_BIT
, &hdr
->daddr
, daddr
);
451 pr_debug("using full address\n");
452 memcpy(hc06_ptr
, &hdr
->daddr
.s6_addr16
[0], 16);
457 /* TODO: UDP header compression */
458 /* TODO: Next Header compression */
463 skb_pull(skb
, sizeof(struct ipv6hdr
));
464 memcpy(skb_push(skb
, hc06_ptr
- head
), head
, hc06_ptr
- head
);
468 lowpan_raw_dump_table(__func__
, "raw skb data dump", skb
->data
,
472 * NOTE1: I'm still unsure about the fact that compression and WPAN
473 * header are created here and not later in the xmit. So wait for
474 * an opinion of net maintainers.
477 * NOTE2: to be absolutely correct, we must derive PANid information
478 * from MAC subif of the 'dev' and 'real_dev' network devices, but
479 * this isn't implemented in mainline yet, so currently we assign 0xff
482 /* prepare wpan address data */
483 sa
.addr_type
= IEEE802154_ADDR_LONG
;
486 da
.addr_type
= IEEE802154_ADDR_LONG
;
489 memcpy(&(da
.hwaddr
), daddr
, 8);
490 memcpy(&(sa
.hwaddr
), saddr
, 8);
492 mac_cb(skb
)->flags
= IEEE802154_FC_TYPE_DATA
;
494 return dev_hard_header(skb
, lowpan_dev_info(dev
)->real_dev
,
495 type
, (void *)&da
, (void *)&sa
, skb
->len
);
499 static int lowpan_skb_deliver(struct sk_buff
*skb
, struct ipv6hdr
*hdr
)
502 struct lowpan_dev_record
*entry
;
503 int stat
= NET_RX_SUCCESS
;
505 new = skb_copy_expand(skb
, sizeof(struct ipv6hdr
), skb_tailroom(skb
),
512 skb_push(new, sizeof(struct ipv6hdr
));
513 skb_reset_network_header(new);
514 skb_copy_to_linear_data(new, hdr
, sizeof(struct ipv6hdr
));
516 new->protocol
= htons(ETH_P_IPV6
);
517 new->pkt_type
= PACKET_HOST
;
520 list_for_each_entry_rcu(entry
, &lowpan_devices
, list
)
521 if (lowpan_dev_info(entry
->ldev
)->real_dev
== new->dev
) {
522 skb
= skb_copy(new, GFP_ATOMIC
);
528 skb
->dev
= entry
->ldev
;
529 stat
= netif_rx(skb
);
538 static void lowpan_fragment_timer_expired(unsigned long entry_addr
)
540 struct lowpan_fragment
*entry
= (struct lowpan_fragment
*)entry_addr
;
542 pr_debug("%s: timer expired for frame with tag %d\n", __func__
,
545 spin_lock(&flist_lock
);
546 list_del(&entry
->list
);
547 spin_unlock(&flist_lock
);
549 dev_kfree_skb(entry
->skb
);
554 lowpan_process_data(struct sk_buff
*skb
)
557 u8 tmp
, iphc0
, iphc1
, num_context
= 0;
561 lowpan_raw_dump_table(__func__
, "raw skb data dump", skb
->data
,
563 /* at least two bytes will be used for the encoding */
566 iphc0
= lowpan_fetch_skb_u8(skb
);
568 /* fragments assembling */
569 switch (iphc0
& LOWPAN_DISPATCH_MASK
) {
570 case LOWPAN_DISPATCH_FRAG1
:
571 case LOWPAN_DISPATCH_FRAGN
:
573 struct lowpan_fragment
*frame
;
578 len
= lowpan_fetch_skb_u8(skb
); /* frame length */
579 tag
= lowpan_fetch_skb_u16(skb
);
582 * check if frame assembling with the same tag is
583 * already in progress
585 spin_lock(&flist_lock
);
587 list_for_each_entry(frame
, &lowpan_fragments
, list
)
588 if (frame
->tag
== tag
) {
593 /* alloc new frame structure */
595 frame
= kzalloc(sizeof(struct lowpan_fragment
),
598 goto unlock_and_drop
;
600 INIT_LIST_HEAD(&frame
->list
);
602 frame
->length
= (iphc0
& 7) | (len
<< 3);
605 /* allocate buffer for frame assembling */
606 frame
->skb
= alloc_skb(frame
->length
+
607 sizeof(struct ipv6hdr
), GFP_ATOMIC
);
611 goto unlock_and_drop
;
614 frame
->skb
->priority
= skb
->priority
;
615 frame
->skb
->dev
= skb
->dev
;
617 /* reserve headroom for uncompressed ipv6 header */
618 skb_reserve(frame
->skb
, sizeof(struct ipv6hdr
));
619 skb_put(frame
->skb
, frame
->length
);
621 init_timer(&frame
->timer
);
622 /* time out is the same as for ipv6 - 60 sec */
623 frame
->timer
.expires
= jiffies
+ LOWPAN_FRAG_TIMEOUT
;
624 frame
->timer
.data
= (unsigned long)frame
;
625 frame
->timer
.function
= lowpan_fragment_timer_expired
;
627 add_timer(&frame
->timer
);
629 list_add_tail(&frame
->list
, &lowpan_fragments
);
632 if ((iphc0
& LOWPAN_DISPATCH_MASK
) == LOWPAN_DISPATCH_FRAG1
)
633 goto unlock_and_drop
;
635 offset
= lowpan_fetch_skb_u8(skb
); /* fetch offset */
637 /* if payload fits buffer, copy it */
638 if (likely((offset
* 8 + skb
->len
) <= frame
->length
))
639 skb_copy_to_linear_data_offset(frame
->skb
, offset
* 8,
640 skb
->data
, skb
->len
);
642 goto unlock_and_drop
;
644 frame
->bytes_rcv
+= skb
->len
;
646 /* frame assembling complete */
647 if ((frame
->bytes_rcv
== frame
->length
) &&
648 frame
->timer
.expires
> jiffies
) {
649 /* if timer haven't expired - first of all delete it */
650 del_timer(&frame
->timer
);
651 list_del(&frame
->list
);
652 spin_unlock(&flist_lock
);
657 iphc0
= lowpan_fetch_skb_u8(skb
);
660 spin_unlock(&flist_lock
);
662 return kfree_skb(skb
), 0;
668 iphc1
= lowpan_fetch_skb_u8(skb
);
670 _saddr
= mac_cb(skb
)->sa
.hwaddr
;
671 _daddr
= mac_cb(skb
)->da
.hwaddr
;
673 pr_debug("(%s): iphc0 = %02x, iphc1 = %02x\n", __func__
, iphc0
, iphc1
);
675 /* another if the CID flag is set */
676 if (iphc1
& LOWPAN_IPHC_CID
) {
677 pr_debug("(%s): CID flag is set, increase header with one\n",
681 num_context
= lowpan_fetch_skb_u8(skb
);
686 /* Traffic Class and Flow Label */
687 switch ((iphc0
& LOWPAN_IPHC_TF
) >> 3) {
689 * Traffic Class and FLow Label carried in-line
690 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
695 tmp
= lowpan_fetch_skb_u8(skb
);
696 memcpy(&hdr
.flow_lbl
, &skb
->data
[0], 3);
698 hdr
.priority
= ((tmp
>> 2) & 0x0f);
699 hdr
.flow_lbl
[0] = ((tmp
>> 2) & 0x30) | (tmp
<< 6) |
700 (hdr
.flow_lbl
[0] & 0x0f);
703 * Traffic class carried in-line
704 * ECN + DSCP (1 byte), Flow Label is elided
709 tmp
= lowpan_fetch_skb_u8(skb
);
710 hdr
.priority
= ((tmp
>> 2) & 0x0f);
711 hdr
.flow_lbl
[0] = ((tmp
<< 6) & 0xC0) | ((tmp
>> 2) & 0x30);
716 * Flow Label carried in-line
717 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
722 tmp
= lowpan_fetch_skb_u8(skb
);
723 hdr
.flow_lbl
[0] = (skb
->data
[0] & 0x0F) | ((tmp
>> 2) & 0x30);
724 memcpy(&hdr
.flow_lbl
[1], &skb
->data
[0], 2);
727 /* Traffic Class and Flow Label are elided */
739 if ((iphc0
& LOWPAN_IPHC_NH_C
) == 0) {
740 /* Next header is carried inline */
743 hdr
.nexthdr
= lowpan_fetch_skb_u8(skb
);
744 pr_debug("(%s): NH flag is set, next header is carried "
745 "inline: %02x\n", __func__
, hdr
.nexthdr
);
749 if ((iphc0
& 0x03) != LOWPAN_IPHC_TTL_I
)
750 hdr
.hop_limit
= lowpan_ttl_values
[iphc0
& 0x03];
754 hdr
.hop_limit
= lowpan_fetch_skb_u8(skb
);
757 /* Extract SAM to the tmp variable */
758 tmp
= ((iphc1
& LOWPAN_IPHC_SAM
) >> LOWPAN_IPHC_SAM_BIT
) & 0x03;
760 /* Source address uncompression */
761 pr_debug("(%s): source address stateless compression\n", __func__
);
762 err
= lowpan_uncompress_addr(skb
, &hdr
.saddr
, lowpan_llprefix
,
763 lowpan_unc_llconf
[tmp
], skb
->data
);
767 /* Extract DAM to the tmp variable */
768 tmp
= ((iphc1
& LOWPAN_IPHC_DAM_11
) >> LOWPAN_IPHC_DAM_BIT
) & 0x03;
770 /* check for Multicast Compression */
771 if (iphc1
& LOWPAN_IPHC_M
) {
772 if (iphc1
& LOWPAN_IPHC_DAC
) {
773 pr_debug("(%s): destination address context-based "
774 "multicast compression\n", __func__
);
775 /* TODO: implement this */
777 u8 prefix
[] = {0xff, 0x02};
779 pr_debug("(%s): destination address non-context-based"
780 " multicast compression\n", __func__
);
781 if (0 < tmp
&& tmp
< 3) {
785 prefix
[1] = lowpan_fetch_skb_u8(skb
);
788 err
= lowpan_uncompress_addr(skb
, &hdr
.daddr
, prefix
,
789 lowpan_unc_mxconf
[tmp
], NULL
);
794 pr_debug("(%s): destination address stateless compression\n",
796 err
= lowpan_uncompress_addr(skb
, &hdr
.daddr
, lowpan_llprefix
,
797 lowpan_unc_llconf
[tmp
], skb
->data
);
802 /* TODO: UDP header parse */
804 /* Not fragmented package */
805 hdr
.payload_len
= htons(skb
->len
);
807 pr_debug("(%s): skb headroom size = %d, data length = %d\n", __func__
,
808 skb_headroom(skb
), skb
->len
);
810 pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
811 "nexthdr = 0x%02x\n\thop_lim = %d\n", __func__
, hdr
.version
,
812 ntohs(hdr
.payload_len
), hdr
.nexthdr
, hdr
.hop_limit
);
814 lowpan_raw_dump_table(__func__
, "raw header dump", (u8
*)&hdr
,
816 return lowpan_skb_deliver(skb
, &hdr
);
819 spin_unlock(&flist_lock
);
825 static int lowpan_set_address(struct net_device
*dev
, void *p
)
827 struct sockaddr
*sa
= p
;
829 if (netif_running(dev
))
832 /* TODO: validate addr */
833 memcpy(dev
->dev_addr
, sa
->sa_data
, dev
->addr_len
);
838 static int lowpan_get_mac_header_length(struct sk_buff
*skb
)
841 * Currently long addressing mode is supported only, so the overall
843 * FC SeqNum DPAN DA SA Sec
844 * 2 + 1 + 2 + 8 + 8 + 0 = 21
850 lowpan_fragment_xmit(struct sk_buff
*skb
, u8
*head
,
851 int mlen
, int plen
, int offset
)
853 struct sk_buff
*frag
;
856 /* if payload length is zero, therefore it's a first fragment */
857 hlen
= (plen
== 0 ? LOWPAN_FRAG1_HEAD_SIZE
: LOWPAN_FRAGN_HEAD_SIZE
);
859 lowpan_raw_dump_inline(__func__
, "6lowpan fragment header", head
, hlen
);
861 frag
= dev_alloc_skb(hlen
+ mlen
+ plen
+ IEEE802154_MFR_SIZE
);
865 frag
->priority
= skb
->priority
;
866 frag
->dev
= skb
->dev
;
868 /* copy header, MFR and payload */
869 memcpy(skb_put(frag
, mlen
), skb
->data
, mlen
);
870 memcpy(skb_put(frag
, hlen
), head
, hlen
);
873 skb_copy_from_linear_data_offset(skb
, offset
+ mlen
,
874 skb_put(frag
, plen
), plen
);
876 lowpan_raw_dump_table(__func__
, " raw fragment dump", frag
->data
,
879 ret
= dev_queue_xmit(frag
);
888 lowpan_skb_fragmentation(struct sk_buff
*skb
)
890 int err
, header_length
, payload_length
, tag
, offset
= 0;
893 header_length
= lowpan_get_mac_header_length(skb
);
894 payload_length
= skb
->len
- header_length
;
895 tag
= fragment_tag
++;
897 /* first fragment header */
898 head
[0] = LOWPAN_DISPATCH_FRAG1
| (payload_length
& 0x7);
899 head
[1] = (payload_length
>> 3) & 0xff;
900 head
[2] = tag
& 0xff;
903 err
= lowpan_fragment_xmit(skb
, head
, header_length
, 0, 0);
905 /* next fragment header */
906 head
[0] &= ~LOWPAN_DISPATCH_FRAG1
;
907 head
[0] |= LOWPAN_DISPATCH_FRAGN
;
909 while ((payload_length
- offset
> 0) && (err
>= 0)) {
910 int len
= LOWPAN_FRAG_SIZE
;
912 head
[4] = offset
/ 8;
914 if (payload_length
- offset
< len
)
915 len
= payload_length
- offset
;
917 err
= lowpan_fragment_xmit(skb
, head
, header_length
,
925 static netdev_tx_t
lowpan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
929 pr_debug("(%s): package xmit\n", __func__
);
931 skb
->dev
= lowpan_dev_info(dev
)->real_dev
;
932 if (skb
->dev
== NULL
) {
933 pr_debug("(%s) ERROR: no real wpan device found\n", __func__
);
937 if (skb
->len
<= IEEE802154_MTU
) {
938 err
= dev_queue_xmit(skb
);
942 pr_debug("(%s): frame is too big, fragmentation is needed\n",
944 err
= lowpan_skb_fragmentation(skb
);
949 pr_debug("(%s): ERROR: xmit failed\n", __func__
);
951 return (err
< 0 ? NETDEV_TX_BUSY
: NETDEV_TX_OK
);
954 static void lowpan_dev_free(struct net_device
*dev
)
956 dev_put(lowpan_dev_info(dev
)->real_dev
);
960 static struct header_ops lowpan_header_ops
= {
961 .create
= lowpan_header_create
,
964 static const struct net_device_ops lowpan_netdev_ops
= {
965 .ndo_start_xmit
= lowpan_xmit
,
966 .ndo_set_mac_address
= lowpan_set_address
,
969 static void lowpan_setup(struct net_device
*dev
)
971 pr_debug("(%s)\n", __func__
);
973 dev
->addr_len
= IEEE802154_ADDR_LEN
;
974 memset(dev
->broadcast
, 0xff, IEEE802154_ADDR_LEN
);
975 dev
->type
= ARPHRD_IEEE802154
;
976 /* Frame Control + Sequence Number + Address fields + Security Header */
977 dev
->hard_header_len
= 2 + 1 + 20 + 14;
978 dev
->needed_tailroom
= 2; /* FCS */
980 dev
->tx_queue_len
= 0;
981 dev
->flags
= IFF_BROADCAST
| IFF_MULTICAST
;
982 dev
->watchdog_timeo
= 0;
984 dev
->netdev_ops
= &lowpan_netdev_ops
;
985 dev
->header_ops
= &lowpan_header_ops
;
986 dev
->destructor
= lowpan_dev_free
;
989 static int lowpan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
991 pr_debug("(%s)\n", __func__
);
993 if (tb
[IFLA_ADDRESS
]) {
994 if (nla_len(tb
[IFLA_ADDRESS
]) != IEEE802154_ADDR_LEN
)
1000 static int lowpan_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
1001 struct packet_type
*pt
, struct net_device
*orig_dev
)
1003 if (!netif_running(dev
))
1006 if (dev
->type
!= ARPHRD_IEEE802154
)
1009 /* check that it's our buffer */
1010 switch (skb
->data
[0] & 0xe0) {
1011 case LOWPAN_DISPATCH_IPHC
: /* ipv6 datagram */
1012 case LOWPAN_DISPATCH_FRAG1
: /* first fragment header */
1013 case LOWPAN_DISPATCH_FRAGN
: /* next fragments headers */
1014 lowpan_process_data(skb
);
1020 return NET_RX_SUCCESS
;
1027 static int lowpan_newlink(struct net
*src_net
, struct net_device
*dev
,
1028 struct nlattr
*tb
[], struct nlattr
*data
[])
1030 struct net_device
*real_dev
;
1031 struct lowpan_dev_record
*entry
;
1033 pr_debug("(%s)\n", __func__
);
1037 /* find and hold real wpan device */
1038 real_dev
= dev_get_by_index(src_net
, nla_get_u32(tb
[IFLA_LINK
]));
1042 lowpan_dev_info(dev
)->real_dev
= real_dev
;
1043 mutex_init(&lowpan_dev_info(dev
)->dev_list_mtx
);
1045 entry
= kzalloc(sizeof(struct lowpan_dev_record
), GFP_KERNEL
);
1048 lowpan_dev_info(dev
)->real_dev
= NULL
;
1054 mutex_lock(&lowpan_dev_info(dev
)->dev_list_mtx
);
1055 INIT_LIST_HEAD(&entry
->list
);
1056 list_add_tail(&entry
->list
, &lowpan_devices
);
1057 mutex_unlock(&lowpan_dev_info(dev
)->dev_list_mtx
);
1059 register_netdevice(dev
);
1064 static void lowpan_dellink(struct net_device
*dev
, struct list_head
*head
)
1066 struct lowpan_dev_info
*lowpan_dev
= lowpan_dev_info(dev
);
1067 struct net_device
*real_dev
= lowpan_dev
->real_dev
;
1068 struct lowpan_dev_record
*entry
;
1069 struct lowpan_dev_record
*tmp
;
1073 mutex_lock(&lowpan_dev_info(dev
)->dev_list_mtx
);
1074 list_for_each_entry_safe(entry
, tmp
, &lowpan_devices
, list
) {
1075 if (entry
->ldev
== dev
) {
1076 list_del(&entry
->list
);
1080 mutex_unlock(&lowpan_dev_info(dev
)->dev_list_mtx
);
1082 mutex_destroy(&lowpan_dev_info(dev
)->dev_list_mtx
);
1084 unregister_netdevice_queue(dev
, head
);
1089 static struct rtnl_link_ops lowpan_link_ops __read_mostly
= {
1091 .priv_size
= sizeof(struct lowpan_dev_info
),
1092 .setup
= lowpan_setup
,
1093 .newlink
= lowpan_newlink
,
1094 .dellink
= lowpan_dellink
,
1095 .validate
= lowpan_validate
,
1098 static inline int __init
lowpan_netlink_init(void)
1100 return rtnl_link_register(&lowpan_link_ops
);
1103 static inline void __init
lowpan_netlink_fini(void)
1105 rtnl_link_unregister(&lowpan_link_ops
);
1108 static struct packet_type lowpan_packet_type
= {
1109 .type
= __constant_htons(ETH_P_IEEE802154
),
1113 static int __init
lowpan_init_module(void)
1117 pr_debug("(%s)\n", __func__
);
1119 err
= lowpan_netlink_init();
1123 dev_add_pack(&lowpan_packet_type
);
1128 static void __exit
lowpan_cleanup_module(void)
1130 pr_debug("(%s)\n", __func__
);
1132 lowpan_netlink_fini();
1134 dev_remove_pack(&lowpan_packet_type
);
1137 module_init(lowpan_init_module
);
1138 module_exit(lowpan_cleanup_module
);
1139 MODULE_LICENSE("GPL");
1140 MODULE_ALIAS_RTNL_LINK("lowpan");