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};
91 /* Link local prefix */
92 static const u8 lowpan_llprefix[] = {0xfe, 0x80};
94 /* private device info */
95 struct lowpan_dev_info {
96 struct net_device *real_dev; /* real WPAN device ptr */
97 struct mutex dev_list_mtx; /* mutex for list ops */
98 unsigned short fragment_tag;
101 struct lowpan_dev_record {
102 struct net_device *ldev;
103 struct list_head list;
106 struct lowpan_fragment {
107 struct sk_buff *skb; /* skb to be assembled */
108 u16 length; /* length to be assemled */
109 u32 bytes_rcv; /* bytes received */
110 u16 tag; /* current fragment tag */
111 struct timer_list timer; /* assembling timer */
112 struct list_head list; /* fragments list */
115 static LIST_HEAD(lowpan_fragments);
116 static DEFINE_SPINLOCK(flist_lock);
119 lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
121 return netdev_priv(dev);
124 static inline void lowpan_address_flip(u8 *src, u8 *dest)
127 for (i = 0; i < IEEE802154_ADDR_LEN; i++)
128 (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i];
131 /* list of all 6lowpan devices, uses for package delivering */
132 /* print data in line */
133 static inline void lowpan_raw_dump_inline(const char *caller, char *msg,
134 unsigned char *buf, int len)
138 pr_debug("(%s) %s: ", caller, msg);
139 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE,
140 16, 1, buf, len, false);
145 * print data in a table format:
147 * addr: xx xx xx xx xx xx
148 * addr: xx xx xx xx xx xx
151 static inline void lowpan_raw_dump_table(const char *caller, char *msg,
152 unsigned char *buf, int len)
156 pr_debug("(%s) %s:\n", caller, msg);
157 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET,
158 16, 1, buf, len, false);
163 lowpan_compress_addr_64(u8 **hc06_ptr, u8 shift, const struct in6_addr *ipaddr,
164 const unsigned char *lladdr)
168 if (is_addr_mac_addr_based(ipaddr, lladdr))
169 val = 3; /* 0-bits */
170 else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
171 /* compress IID to 16 bits xxxx::XXXX */
172 memcpy(*hc06_ptr, &ipaddr->s6_addr16[7], 2);
174 val = 2; /* 16-bits */
176 /* do not compress IID => xxxx::IID */
177 memcpy(*hc06_ptr, &ipaddr->s6_addr16[4], 8);
179 val = 1; /* 64-bits */
182 return rol8(val, shift);
186 lowpan_uip_ds6_set_addr_iid(struct in6_addr *ipaddr, unsigned char *lladdr)
188 memcpy(&ipaddr->s6_addr[8], lladdr, IEEE802154_ADDR_LEN);
189 /* second bit-flip (Universe/Local) is done according RFC2464 */
190 ipaddr->s6_addr[8] ^= 0x02;
194 * Uncompress addresses based on a prefix and a postfix with zeroes in
195 * between. If the postfix is zero in length it will use the link address
196 * to configure the IP address (autoconf style).
197 * pref_post_count takes a byte where the first nibble specify prefix count
198 * and the second postfix count (NOTE: 15/0xf => 16 bytes copy).
201 lowpan_uncompress_addr(struct sk_buff *skb, struct in6_addr *ipaddr,
202 u8 const *prefix, u8 pref_post_count, unsigned char *lladdr)
204 u8 prefcount = pref_post_count >> 4;
205 u8 postcount = pref_post_count & 0x0f;
207 /* full nibble 15 => 16 */
208 prefcount = (prefcount == 15 ? 16 : prefcount);
209 postcount = (postcount == 15 ? 16 : postcount);
212 lowpan_raw_dump_inline(__func__, "linklocal address",
213 lladdr, IEEE802154_ADDR_LEN);
215 memcpy(ipaddr, prefix, prefcount);
218 memcpy(&ipaddr->s6_addr[16 - postcount], skb->data, postcount);
219 skb_pull(skb, postcount);
220 } else if (prefcount > 0) {
224 /* no IID based configuration if no prefix and no data */
225 lowpan_uip_ds6_set_addr_iid(ipaddr, lladdr);
228 pr_debug("uncompressing %d + %d => ", prefcount, postcount);
229 lowpan_raw_dump_inline(NULL, NULL, ipaddr->s6_addr, 16);
234 /* Uncompress function for multicast destination address,
238 lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
239 struct in6_addr *ipaddr,
245 case LOWPAN_IPHC_DAM_00:
246 /* 00: 128 bits. The full address
247 * is carried in-line.
249 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
251 case LOWPAN_IPHC_DAM_01:
252 /* 01: 48 bits. The address takes
253 * the form ffXX::00XX:XXXX:XXXX.
255 ipaddr->s6_addr[0] = 0xFF;
256 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
257 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
259 case LOWPAN_IPHC_DAM_10:
260 /* 10: 32 bits. The address takes
261 * the form ffXX::00XX:XXXX.
263 ipaddr->s6_addr[0] = 0xFF;
264 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
265 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
267 case LOWPAN_IPHC_DAM_11:
268 /* 11: 8 bits. The address takes
269 * the form ff02::00XX.
271 ipaddr->s6_addr[0] = 0xFF;
272 ipaddr->s6_addr[1] = 0x02;
273 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
276 pr_debug("DAM value has a wrong value: 0x%x\n", dam);
281 pr_debug("Failed to fetch skb data\n");
285 lowpan_raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is:\n",
286 ipaddr->s6_addr, 16);
292 lowpan_compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
294 struct udphdr *uh = udp_hdr(skb);
296 if (((uh->source & LOWPAN_NHC_UDP_4BIT_MASK) ==
297 LOWPAN_NHC_UDP_4BIT_PORT) &&
298 ((uh->dest & LOWPAN_NHC_UDP_4BIT_MASK) ==
299 LOWPAN_NHC_UDP_4BIT_PORT)) {
300 pr_debug("UDP header: both ports compression to 4 bits\n");
301 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_11;
302 **(hc06_ptr + 1) = /* subtraction is faster */
303 (u8)((uh->dest - LOWPAN_NHC_UDP_4BIT_PORT) +
304 ((uh->source & LOWPAN_NHC_UDP_4BIT_PORT) << 4));
306 } else if ((uh->dest & LOWPAN_NHC_UDP_8BIT_MASK) ==
307 LOWPAN_NHC_UDP_8BIT_PORT) {
308 pr_debug("UDP header: remove 8 bits of dest\n");
309 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_01;
310 memcpy(*hc06_ptr + 1, &uh->source, 2);
311 **(hc06_ptr + 3) = (u8)(uh->dest - LOWPAN_NHC_UDP_8BIT_PORT);
313 } else if ((uh->source & LOWPAN_NHC_UDP_8BIT_MASK) ==
314 LOWPAN_NHC_UDP_8BIT_PORT) {
315 pr_debug("UDP header: remove 8 bits of source\n");
316 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_10;
317 memcpy(*hc06_ptr + 1, &uh->dest, 2);
318 **(hc06_ptr + 3) = (u8)(uh->source - LOWPAN_NHC_UDP_8BIT_PORT);
321 pr_debug("UDP header: can't compress\n");
322 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_00;
323 memcpy(*hc06_ptr + 1, &uh->source, 2);
324 memcpy(*hc06_ptr + 3, &uh->dest, 2);
328 /* checksum is always inline */
329 memcpy(*hc06_ptr, &uh->check, 2);
332 /* skip the UDP header */
333 skb_pull(skb, sizeof(struct udphdr));
336 static inline int lowpan_fetch_skb_u8(struct sk_buff *skb, u8 *val)
338 if (unlikely(!pskb_may_pull(skb, 1)))
347 static inline int lowpan_fetch_skb_u16(struct sk_buff *skb, u16 *val)
349 if (unlikely(!pskb_may_pull(skb, 2)))
352 *val = (skb->data[0] << 8) | skb->data[1];
359 lowpan_uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
366 if (lowpan_fetch_skb_u8(skb, &tmp))
369 if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
370 pr_debug("UDP header uncompression\n");
371 switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
372 case LOWPAN_NHC_UDP_CS_P_00:
373 memcpy(&uh->source, &skb->data[0], 2);
374 memcpy(&uh->dest, &skb->data[2], 2);
377 case LOWPAN_NHC_UDP_CS_P_01:
378 memcpy(&uh->source, &skb->data[0], 2);
380 skb->data[2] + LOWPAN_NHC_UDP_8BIT_PORT;
383 case LOWPAN_NHC_UDP_CS_P_10:
384 uh->source = skb->data[0] + LOWPAN_NHC_UDP_8BIT_PORT;
385 memcpy(&uh->dest, &skb->data[1], 2);
388 case LOWPAN_NHC_UDP_CS_P_11:
390 LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] >> 4);
392 LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] & 0x0f);
396 pr_debug("ERROR: unknown UDP format\n");
401 pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
402 uh->source, uh->dest);
405 memcpy(&uh->check, &skb->data[0], 2);
409 * UDP lenght needs to be infered from the lower layers
410 * here, we obtain the hint from the remaining size of the
413 uh->len = htons(skb->len + sizeof(struct udphdr));
414 pr_debug("uncompressed UDP length: src = %d", uh->len);
416 pr_debug("ERROR: unsupported NH format\n");
425 static int lowpan_header_create(struct sk_buff *skb,
426 struct net_device *dev,
427 unsigned short type, const void *_daddr,
428 const void *_saddr, unsigned int len)
430 u8 tmp, iphc0, iphc1, *hc06_ptr;
432 const u8 *saddr = _saddr;
433 const u8 *daddr = _daddr;
435 struct ieee802154_addr sa, da;
438 * if this package isn't ipv6 one, where should it be routed?
440 if (type != ETH_P_IPV6)
446 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
447 "\tnexthdr = 0x%02x\n\thop_lim = %d\n", hdr->version,
448 ntohs(hdr->payload_len), hdr->nexthdr, hdr->hop_limit);
450 lowpan_raw_dump_table(__func__, "raw skb network header dump",
451 skb_network_header(skb), sizeof(struct ipv6hdr));
454 saddr = dev->dev_addr;
456 lowpan_raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
459 * As we copy some bit-length fields, in the IPHC encoding bytes,
460 * we sometimes use |=
461 * If the field is 0, and the current bit value in memory is 1,
462 * this does not work. We therefore reset the IPHC encoding here
464 iphc0 = LOWPAN_DISPATCH_IPHC;
467 /* TODO: context lookup */
469 lowpan_raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
472 * Traffic class, flow label
473 * If flow label is 0, compress it. If traffic class is 0, compress it
474 * We have to process both in the same time as the offset of traffic
475 * class depends on the presence of version and flow label
478 /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
479 tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
480 tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
482 if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
483 (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
484 /* flow label can be compressed */
485 iphc0 |= LOWPAN_IPHC_FL_C;
486 if ((hdr->priority == 0) &&
487 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
488 /* compress (elide) all */
489 iphc0 |= LOWPAN_IPHC_TC_C;
491 /* compress only the flow label */
496 /* Flow label cannot be compressed */
497 if ((hdr->priority == 0) &&
498 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
499 /* compress only traffic class */
500 iphc0 |= LOWPAN_IPHC_TC_C;
501 *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
502 memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
505 /* compress nothing */
506 memcpy(hc06_ptr, &hdr, 4);
507 /* replace the top byte with new ECN | DSCP format */
513 /* NOTE: payload length is always compressed */
515 /* Next Header is compress if UDP */
516 if (hdr->nexthdr == UIP_PROTO_UDP)
517 iphc0 |= LOWPAN_IPHC_NH_C;
519 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
520 *hc06_ptr = hdr->nexthdr;
526 * if 1: compress, encoding is 01
527 * if 64: compress, encoding is 10
528 * if 255: compress, encoding is 11
529 * else do not compress
531 switch (hdr->hop_limit) {
533 iphc0 |= LOWPAN_IPHC_TTL_1;
536 iphc0 |= LOWPAN_IPHC_TTL_64;
539 iphc0 |= LOWPAN_IPHC_TTL_255;
542 *hc06_ptr = hdr->hop_limit;
547 /* source address compression */
548 if (is_addr_unspecified(&hdr->saddr)) {
549 pr_debug("source address is unspecified, setting SAC\n");
550 iphc1 |= LOWPAN_IPHC_SAC;
551 /* TODO: context lookup */
552 } else if (is_addr_link_local(&hdr->saddr)) {
553 pr_debug("source address is link-local\n");
554 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
555 LOWPAN_IPHC_SAM_BIT, &hdr->saddr, saddr);
557 pr_debug("send the full source address\n");
558 memcpy(hc06_ptr, &hdr->saddr.s6_addr16[0], 16);
562 /* destination address compression */
563 if (is_addr_mcast(&hdr->daddr)) {
564 pr_debug("destination address is multicast: ");
565 iphc1 |= LOWPAN_IPHC_M;
566 if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
567 pr_debug("compressed to 1 octet\n");
568 iphc1 |= LOWPAN_IPHC_DAM_11;
570 *hc06_ptr = hdr->daddr.s6_addr[15];
572 } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
573 pr_debug("compressed to 4 octets\n");
574 iphc1 |= LOWPAN_IPHC_DAM_10;
575 /* second byte + the last three */
576 *hc06_ptr = hdr->daddr.s6_addr[1];
577 memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[13], 3);
579 } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
580 pr_debug("compressed to 6 octets\n");
581 iphc1 |= LOWPAN_IPHC_DAM_01;
582 /* second byte + the last five */
583 *hc06_ptr = hdr->daddr.s6_addr[1];
584 memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[11], 5);
587 pr_debug("using full address\n");
588 iphc1 |= LOWPAN_IPHC_DAM_00;
589 memcpy(hc06_ptr, &hdr->daddr.s6_addr[0], 16);
593 /* TODO: context lookup */
594 if (is_addr_link_local(&hdr->daddr)) {
595 pr_debug("dest address is unicast and link-local\n");
596 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
597 LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
599 pr_debug("dest address is unicast: using full one\n");
600 memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
605 /* UDP header compression */
606 if (hdr->nexthdr == UIP_PROTO_UDP)
607 lowpan_compress_udp_header(&hc06_ptr, skb);
612 skb_pull(skb, sizeof(struct ipv6hdr));
613 memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
615 lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
619 * NOTE1: I'm still unsure about the fact that compression and WPAN
620 * header are created here and not later in the xmit. So wait for
621 * an opinion of net maintainers.
624 * NOTE2: to be absolutely correct, we must derive PANid information
625 * from MAC subif of the 'dev' and 'real_dev' network devices, but
626 * this isn't implemented in mainline yet, so currently we assign 0xff
629 mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
630 mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
632 /* prepare wpan address data */
633 sa.addr_type = IEEE802154_ADDR_LONG;
634 sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
636 memcpy(&(sa.hwaddr), saddr, 8);
637 /* intra-PAN communications */
638 da.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
641 * if the destination address is the broadcast address, use the
642 * corresponding short address
644 if (lowpan_is_addr_broadcast(daddr)) {
645 da.addr_type = IEEE802154_ADDR_SHORT;
646 da.short_addr = IEEE802154_ADDR_BROADCAST;
648 da.addr_type = IEEE802154_ADDR_LONG;
649 memcpy(&(da.hwaddr), daddr, IEEE802154_ADDR_LEN);
651 /* request acknowledgment */
652 mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ;
655 return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
656 type, (void *)&da, (void *)&sa, skb->len);
660 static int lowpan_give_skb_to_devices(struct sk_buff *skb)
662 struct lowpan_dev_record *entry;
663 struct sk_buff *skb_cp;
664 int stat = NET_RX_SUCCESS;
667 list_for_each_entry_rcu(entry, &lowpan_devices, list)
668 if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) {
669 skb_cp = skb_copy(skb, GFP_ATOMIC);
675 skb_cp->dev = entry->ldev;
676 stat = netif_rx(skb_cp);
683 static int lowpan_skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr)
686 int stat = NET_RX_SUCCESS;
688 new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
695 skb_push(new, sizeof(struct ipv6hdr));
696 skb_reset_network_header(new);
697 skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
699 new->protocol = htons(ETH_P_IPV6);
700 new->pkt_type = PACKET_HOST;
702 stat = lowpan_give_skb_to_devices(new);
709 static void lowpan_fragment_timer_expired(unsigned long entry_addr)
711 struct lowpan_fragment *entry = (struct lowpan_fragment *)entry_addr;
713 pr_debug("timer expired for frame with tag %d\n", entry->tag);
715 list_del(&entry->list);
716 dev_kfree_skb(entry->skb);
720 static struct lowpan_fragment *
721 lowpan_alloc_new_frame(struct sk_buff *skb, u16 len, u16 tag)
723 struct lowpan_fragment *frame;
725 frame = kzalloc(sizeof(struct lowpan_fragment),
730 INIT_LIST_HEAD(&frame->list);
735 /* allocate buffer for frame assembling */
736 frame->skb = netdev_alloc_skb_ip_align(skb->dev, frame->length +
737 sizeof(struct ipv6hdr));
742 frame->skb->priority = skb->priority;
743 frame->skb->dev = skb->dev;
745 /* reserve headroom for uncompressed ipv6 header */
746 skb_reserve(frame->skb, sizeof(struct ipv6hdr));
747 skb_put(frame->skb, frame->length);
749 /* copy the first control block to keep a
750 * trace of the link-layer addresses in case
751 * of a link-local compressed address
753 memcpy(frame->skb->cb, skb->cb, sizeof(skb->cb));
755 init_timer(&frame->timer);
756 /* time out is the same as for ipv6 - 60 sec */
757 frame->timer.expires = jiffies + LOWPAN_FRAG_TIMEOUT;
758 frame->timer.data = (unsigned long)frame;
759 frame->timer.function = lowpan_fragment_timer_expired;
761 add_timer(&frame->timer);
763 list_add_tail(&frame->list, &lowpan_fragments);
774 lowpan_process_data(struct sk_buff *skb)
776 struct ipv6hdr hdr = {};
777 u8 tmp, iphc0, iphc1, num_context = 0;
781 lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
783 /* at least two bytes will be used for the encoding */
787 if (lowpan_fetch_skb_u8(skb, &iphc0))
790 /* fragments assembling */
791 switch (iphc0 & LOWPAN_DISPATCH_MASK) {
792 case LOWPAN_DISPATCH_FRAG1:
793 case LOWPAN_DISPATCH_FRAGN:
795 struct lowpan_fragment *frame;
796 /* slen stores the rightmost 8 bits of the 11 bits length */
801 if (lowpan_fetch_skb_u8(skb, &slen) || /* frame length */
802 lowpan_fetch_skb_u16(skb, &tag)) /* fragment tag */
805 /* adds the 3 MSB to the 8 LSB to retrieve the 11 bits length */
806 len = ((iphc0 & 7) << 8) | slen;
808 if ((iphc0 & LOWPAN_DISPATCH_MASK) == LOWPAN_DISPATCH_FRAG1) {
809 pr_debug("%s received a FRAG1 packet (tag: %d, "
810 "size of the entire IP packet: %d)",
813 if (lowpan_fetch_skb_u8(skb, &offset))
814 goto unlock_and_drop;
815 pr_debug("%s received a FRAGN packet (tag: %d, "
816 "size of the entire IP packet: %d, "
817 "offset: %d)", __func__, tag, len, offset * 8);
821 * check if frame assembling with the same tag is
822 * already in progress
824 spin_lock_bh(&flist_lock);
826 list_for_each_entry(frame, &lowpan_fragments, list)
827 if (frame->tag == tag) {
832 /* alloc new frame structure */
834 pr_debug("%s first fragment received for tag %d, "
835 "begin packet reassembly", __func__, tag);
836 frame = lowpan_alloc_new_frame(skb, len, tag);
838 goto unlock_and_drop;
841 /* if payload fits buffer, copy it */
842 if (likely((offset * 8 + skb->len) <= frame->length))
843 skb_copy_to_linear_data_offset(frame->skb, offset * 8,
844 skb->data, skb->len);
846 goto unlock_and_drop;
848 frame->bytes_rcv += skb->len;
850 /* frame assembling complete */
851 if ((frame->bytes_rcv == frame->length) &&
852 frame->timer.expires > jiffies) {
853 /* if timer haven't expired - first of all delete it */
854 del_timer_sync(&frame->timer);
855 list_del(&frame->list);
856 spin_unlock_bh(&flist_lock);
858 pr_debug("%s successfully reassembled fragment "
859 "(tag %d)", __func__, tag);
865 if (lowpan_fetch_skb_u8(skb, &iphc0))
870 spin_unlock_bh(&flist_lock);
872 return kfree_skb(skb), 0;
878 if (lowpan_fetch_skb_u8(skb, &iphc1))
881 _saddr = mac_cb(skb)->sa.hwaddr;
882 _daddr = mac_cb(skb)->da.hwaddr;
884 pr_debug("iphc0 = %02x, iphc1 = %02x\n", iphc0, iphc1);
886 /* another if the CID flag is set */
887 if (iphc1 & LOWPAN_IPHC_CID) {
888 pr_debug("CID flag is set, increase header with one\n");
889 if (lowpan_fetch_skb_u8(skb, &num_context))
895 /* Traffic Class and Flow Label */
896 switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
898 * Traffic Class and FLow Label carried in-line
899 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
902 if (lowpan_fetch_skb_u8(skb, &tmp))
905 memcpy(&hdr.flow_lbl, &skb->data[0], 3);
907 hdr.priority = ((tmp >> 2) & 0x0f);
908 hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
909 (hdr.flow_lbl[0] & 0x0f);
912 * Traffic class carried in-line
913 * ECN + DSCP (1 byte), Flow Label is elided
916 if (lowpan_fetch_skb_u8(skb, &tmp))
919 hdr.priority = ((tmp >> 2) & 0x0f);
920 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
923 * Flow Label carried in-line
924 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
927 if (lowpan_fetch_skb_u8(skb, &tmp))
930 hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
931 memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
934 /* Traffic Class and Flow Label are elided */
942 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
943 /* Next header is carried inline */
944 if (lowpan_fetch_skb_u8(skb, &(hdr.nexthdr)))
947 pr_debug("NH flag is set, next header carried inline: %02x\n",
952 if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
953 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
955 if (lowpan_fetch_skb_u8(skb, &(hdr.hop_limit)))
959 /* Extract SAM to the tmp variable */
960 tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
962 /* Source address uncompression */
963 pr_debug("source address stateless compression\n");
964 err = lowpan_uncompress_addr(skb, &hdr.saddr, lowpan_llprefix,
965 lowpan_unc_llconf[tmp], skb->data);
969 /* Extract DAM to the tmp variable */
970 tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
972 /* check for Multicast Compression */
973 if (iphc1 & LOWPAN_IPHC_M) {
974 if (iphc1 & LOWPAN_IPHC_DAC) {
975 pr_debug("dest: context-based mcast compression\n");
976 /* TODO: implement this */
978 err = lowpan_uncompress_multicast_daddr(
979 skb, &hdr.daddr, tmp);
984 pr_debug("dest: stateless compression\n");
985 err = lowpan_uncompress_addr(skb, &hdr.daddr, lowpan_llprefix,
986 lowpan_unc_llconf[tmp], skb->data);
991 /* UDP data uncompression */
992 if (iphc0 & LOWPAN_IPHC_NH_C) {
995 if (lowpan_uncompress_udp_header(skb, &uh))
999 * replace the compressed UDP head by the uncompressed UDP
1002 new = skb_copy_expand(skb, sizeof(struct udphdr),
1003 skb_tailroom(skb), GFP_ATOMIC);
1011 skb_push(skb, sizeof(struct udphdr));
1012 skb_reset_transport_header(skb);
1013 skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
1015 lowpan_raw_dump_table(__func__, "raw UDP header dump",
1016 (u8 *)&uh, sizeof(uh));
1018 hdr.nexthdr = UIP_PROTO_UDP;
1021 /* Not fragmented package */
1022 hdr.payload_len = htons(skb->len);
1024 pr_debug("skb headroom size = %d, data length = %d\n",
1025 skb_headroom(skb), skb->len);
1027 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
1028 "nexthdr = 0x%02x\n\thop_lim = %d\n", hdr.version,
1029 ntohs(hdr.payload_len), hdr.nexthdr, hdr.hop_limit);
1031 lowpan_raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
1033 return lowpan_skb_deliver(skb, &hdr);
1036 spin_unlock_bh(&flist_lock);
1042 static int lowpan_set_address(struct net_device *dev, void *p)
1044 struct sockaddr *sa = p;
1046 if (netif_running(dev))
1049 /* TODO: validate addr */
1050 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
1055 static int lowpan_get_mac_header_length(struct sk_buff *skb)
1058 * Currently long addressing mode is supported only, so the overall
1059 * header size is 21:
1060 * FC SeqNum DPAN DA SA Sec
1061 * 2 + 1 + 2 + 8 + 8 + 0 = 21
1067 lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
1068 int mlen, int plen, int offset, int type)
1070 struct sk_buff *frag;
1073 hlen = (type == LOWPAN_DISPATCH_FRAG1) ?
1074 LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE;
1076 lowpan_raw_dump_inline(__func__, "6lowpan fragment header", head, hlen);
1078 frag = dev_alloc_skb(hlen + mlen + plen + IEEE802154_MFR_SIZE);
1082 frag->priority = skb->priority;
1083 frag->dev = skb->dev;
1085 /* copy header, MFR and payload */
1086 memcpy(skb_put(frag, mlen), skb->data, mlen);
1087 memcpy(skb_put(frag, hlen), head, hlen);
1090 skb_copy_from_linear_data_offset(skb, offset + mlen,
1091 skb_put(frag, plen), plen);
1093 lowpan_raw_dump_table(__func__, " raw fragment dump", frag->data,
1096 ret = dev_queue_xmit(frag);
1102 lowpan_skb_fragmentation(struct sk_buff *skb, struct net_device *dev)
1104 int err, header_length, payload_length, tag, offset = 0;
1107 header_length = lowpan_get_mac_header_length(skb);
1108 payload_length = skb->len - header_length;
1109 tag = lowpan_dev_info(dev)->fragment_tag++;
1111 /* first fragment header */
1112 head[0] = LOWPAN_DISPATCH_FRAG1 | ((payload_length >> 8) & 0x7);
1113 head[1] = payload_length & 0xff;
1115 head[3] = tag & 0xff;
1117 err = lowpan_fragment_xmit(skb, head, header_length, LOWPAN_FRAG_SIZE,
1118 0, LOWPAN_DISPATCH_FRAG1);
1121 pr_debug("%s unable to send FRAG1 packet (tag: %d)",
1126 offset = LOWPAN_FRAG_SIZE;
1128 /* next fragment header */
1129 head[0] &= ~LOWPAN_DISPATCH_FRAG1;
1130 head[0] |= LOWPAN_DISPATCH_FRAGN;
1132 while ((payload_length - offset > 0) && (err >= 0)) {
1133 int len = LOWPAN_FRAG_SIZE;
1135 head[4] = offset / 8;
1137 if (payload_length - offset < len)
1138 len = payload_length - offset;
1140 err = lowpan_fragment_xmit(skb, head, header_length,
1141 len, offset, LOWPAN_DISPATCH_FRAGN);
1143 pr_debug("%s unable to send a subsequent FRAGN packet "
1144 "(tag: %d, offset: %d", __func__, tag, offset);
1155 static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
1159 pr_debug("package xmit\n");
1161 skb->dev = lowpan_dev_info(dev)->real_dev;
1162 if (skb->dev == NULL) {
1163 pr_debug("ERROR: no real wpan device found\n");
1167 /* Send directly if less than the MTU minus the 2 checksum bytes. */
1168 if (skb->len <= IEEE802154_MTU - IEEE802154_MFR_SIZE) {
1169 err = dev_queue_xmit(skb);
1173 pr_debug("frame is too big, fragmentation is needed\n");
1174 err = lowpan_skb_fragmentation(skb, dev);
1179 pr_debug("ERROR: xmit failed\n");
1181 return (err < 0) ? NET_XMIT_DROP : err;
1184 static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
1186 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
1187 return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
1190 static u16 lowpan_get_pan_id(const struct net_device *dev)
1192 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
1193 return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
1196 static u16 lowpan_get_short_addr(const struct net_device *dev)
1198 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
1199 return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
1202 static u8 lowpan_get_dsn(const struct net_device *dev)
1204 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
1205 return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev);
1208 static struct header_ops lowpan_header_ops = {
1209 .create = lowpan_header_create,
1212 static const struct net_device_ops lowpan_netdev_ops = {
1213 .ndo_start_xmit = lowpan_xmit,
1214 .ndo_set_mac_address = lowpan_set_address,
1217 static struct ieee802154_mlme_ops lowpan_mlme = {
1218 .get_pan_id = lowpan_get_pan_id,
1219 .get_phy = lowpan_get_phy,
1220 .get_short_addr = lowpan_get_short_addr,
1221 .get_dsn = lowpan_get_dsn,
1224 static void lowpan_setup(struct net_device *dev)
1226 dev->addr_len = IEEE802154_ADDR_LEN;
1227 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
1228 dev->type = ARPHRD_IEEE802154;
1229 /* Frame Control + Sequence Number + Address fields + Security Header */
1230 dev->hard_header_len = 2 + 1 + 20 + 14;
1231 dev->needed_tailroom = 2; /* FCS */
1233 dev->tx_queue_len = 0;
1234 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
1235 dev->watchdog_timeo = 0;
1237 dev->netdev_ops = &lowpan_netdev_ops;
1238 dev->header_ops = &lowpan_header_ops;
1239 dev->ml_priv = &lowpan_mlme;
1240 dev->destructor = free_netdev;
1243 static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
1245 if (tb[IFLA_ADDRESS]) {
1246 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
1252 static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
1253 struct packet_type *pt, struct net_device *orig_dev)
1255 struct sk_buff *local_skb;
1257 if (!netif_running(dev))
1260 if (dev->type != ARPHRD_IEEE802154)
1263 /* check that it's our buffer */
1264 if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
1265 /* Copy the packet so that the IPv6 header is
1268 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
1269 skb_tailroom(skb), GFP_ATOMIC);
1273 local_skb->protocol = htons(ETH_P_IPV6);
1274 local_skb->pkt_type = PACKET_HOST;
1276 /* Pull off the 1-byte of 6lowpan header. */
1277 skb_pull(local_skb, 1);
1278 skb_reset_network_header(local_skb);
1279 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
1281 lowpan_give_skb_to_devices(local_skb);
1283 kfree_skb(local_skb);
1286 switch (skb->data[0] & 0xe0) {
1287 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
1288 case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
1289 case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
1290 local_skb = skb_clone(skb, GFP_ATOMIC);
1293 lowpan_process_data(local_skb);
1302 return NET_RX_SUCCESS;
1309 static int lowpan_newlink(struct net *src_net, struct net_device *dev,
1310 struct nlattr *tb[], struct nlattr *data[])
1312 struct net_device *real_dev;
1313 struct lowpan_dev_record *entry;
1315 pr_debug("adding new link\n");
1319 /* find and hold real wpan device */
1320 real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1324 lowpan_dev_info(dev)->real_dev = real_dev;
1325 lowpan_dev_info(dev)->fragment_tag = 0;
1326 mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
1328 entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
1331 lowpan_dev_info(dev)->real_dev = NULL;
1337 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
1338 INIT_LIST_HEAD(&entry->list);
1339 list_add_tail(&entry->list, &lowpan_devices);
1340 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
1342 register_netdevice(dev);
1347 static void lowpan_dellink(struct net_device *dev, struct list_head *head)
1349 struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
1350 struct net_device *real_dev = lowpan_dev->real_dev;
1351 struct lowpan_dev_record *entry, *tmp;
1355 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
1356 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
1357 if (entry->ldev == dev) {
1358 list_del(&entry->list);
1362 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
1364 mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
1366 unregister_netdevice_queue(dev, head);
1371 static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
1373 .priv_size = sizeof(struct lowpan_dev_info),
1374 .setup = lowpan_setup,
1375 .newlink = lowpan_newlink,
1376 .dellink = lowpan_dellink,
1377 .validate = lowpan_validate,
1380 static inline int __init lowpan_netlink_init(void)
1382 return rtnl_link_register(&lowpan_link_ops);
1385 static inline void lowpan_netlink_fini(void)
1387 rtnl_link_unregister(&lowpan_link_ops);
1390 static int lowpan_device_event(struct notifier_block *unused,
1391 unsigned long event, void *ptr)
1393 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1394 LIST_HEAD(del_list);
1395 struct lowpan_dev_record *entry, *tmp;
1397 if (dev->type != ARPHRD_IEEE802154)
1400 if (event == NETDEV_UNREGISTER) {
1401 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
1402 if (lowpan_dev_info(entry->ldev)->real_dev == dev)
1403 lowpan_dellink(entry->ldev, &del_list);
1406 unregister_netdevice_many(&del_list);
1413 static struct notifier_block lowpan_dev_notifier = {
1414 .notifier_call = lowpan_device_event,
1417 static struct packet_type lowpan_packet_type = {
1418 .type = __constant_htons(ETH_P_IEEE802154),
1422 static int __init lowpan_init_module(void)
1426 err = lowpan_netlink_init();
1430 dev_add_pack(&lowpan_packet_type);
1432 err = register_netdevice_notifier(&lowpan_dev_notifier);
1434 dev_remove_pack(&lowpan_packet_type);
1435 lowpan_netlink_fini();
1441 static void __exit lowpan_cleanup_module(void)
1443 struct lowpan_fragment *frame, *tframe;
1445 lowpan_netlink_fini();
1447 dev_remove_pack(&lowpan_packet_type);
1449 unregister_netdevice_notifier(&lowpan_dev_notifier);
1451 /* Now 6lowpan packet_type is removed, so no new fragments are
1452 * expected on RX, therefore that's the time to clean incomplete
1455 spin_lock_bh(&flist_lock);
1456 list_for_each_entry_safe(frame, tframe, &lowpan_fragments, list) {
1457 del_timer_sync(&frame->timer);
1458 list_del(&frame->list);
1459 dev_kfree_skb(frame->skb);
1462 spin_unlock_bh(&flist_lock);
1465 module_init(lowpan_init_module);
1466 module_exit(lowpan_cleanup_module);
1467 MODULE_LICENSE("GPL");
1468 MODULE_ALIAS_RTNL_LINK("lowpan");