2 * Copyright 2011, Siemens AG
3 * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
6 /* Based on patches from Jon Smirl <jonsmirl@gmail.com>
7 * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /* Jon's code is based on 6lowpan implementation for Contiki which is:
24 * Copyright (c) 2008, Swedish Institute of Computer Science.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. Neither the name of the Institute nor the names of its contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 #include <linux/bitops.h>
53 #include <linux/if_arp.h>
54 #include <linux/module.h>
55 #include <linux/netdevice.h>
56 #include <net/6lowpan.h>
58 #include <net/af_ieee802154.h>
60 /* Uncompress address function for source and
61 * destination address(non-multicast).
63 * address_mode is sam value or dam value.
65 static int uncompress_addr(struct sk_buff *skb,
66 struct in6_addr *ipaddr, const u8 address_mode,
67 const u8 *lladdr, const u8 addr_type,
72 switch (address_mode) {
73 case LOWPAN_IPHC_ADDR_00:
74 /* for global link addresses */
75 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
77 case LOWPAN_IPHC_ADDR_01:
78 /* fe:80::XXXX:XXXX:XXXX:XXXX */
79 ipaddr->s6_addr[0] = 0xFE;
80 ipaddr->s6_addr[1] = 0x80;
81 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8);
83 case LOWPAN_IPHC_ADDR_02:
84 /* fe:80::ff:fe00:XXXX */
85 ipaddr->s6_addr[0] = 0xFE;
86 ipaddr->s6_addr[1] = 0x80;
87 ipaddr->s6_addr[11] = 0xFF;
88 ipaddr->s6_addr[12] = 0xFE;
89 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2);
91 case LOWPAN_IPHC_ADDR_03:
94 case IEEE802154_ADDR_LONG:
95 /* fe:80::XXXX:XXXX:XXXX:XXXX
99 ipaddr->s6_addr[0] = 0xFE;
100 ipaddr->s6_addr[1] = 0x80;
101 memcpy(&ipaddr->s6_addr[8], lladdr, addr_len);
102 /* second bit-flip (Universe/Local)
103 * is done according RFC2464
105 ipaddr->s6_addr[8] ^= 0x02;
107 case IEEE802154_ADDR_SHORT:
108 /* fe:80::ff:fe00:XXXX
112 * Universe/Local bit is zero.
114 ipaddr->s6_addr[0] = 0xFE;
115 ipaddr->s6_addr[1] = 0x80;
116 ipaddr->s6_addr[11] = 0xFF;
117 ipaddr->s6_addr[12] = 0xFE;
118 ipaddr->s6_addr16[7] = htons(*((u16 *)lladdr));
121 pr_debug("Invalid addr_type set\n");
126 pr_debug("Invalid address mode value: 0x%x\n", address_mode);
131 pr_debug("Failed to fetch skb data\n");
135 raw_dump_inline(NULL, "Reconstructed ipv6 addr is",
136 ipaddr->s6_addr, 16);
141 /* Uncompress address function for source context
142 * based address(non-multicast).
144 static int uncompress_context_based_src_addr(struct sk_buff *skb,
145 struct in6_addr *ipaddr,
149 case LOWPAN_IPHC_ADDR_00:
151 * Do nothing, address is already ::
154 case LOWPAN_IPHC_ADDR_01:
156 case LOWPAN_IPHC_ADDR_02:
158 case LOWPAN_IPHC_ADDR_03:
160 netdev_warn(skb->dev, "SAM value 0x%x not supported\n", sam);
163 pr_debug("Invalid sam value: 0x%x\n", sam);
167 raw_dump_inline(NULL,
168 "Reconstructed context based ipv6 src addr is",
169 ipaddr->s6_addr, 16);
174 static int skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr,
175 struct net_device *dev, skb_delivery_cb deliver_skb)
180 new = skb_copy_expand(skb, sizeof(struct ipv6hdr),
181 skb_tailroom(skb), GFP_ATOMIC);
187 skb_push(new, sizeof(struct ipv6hdr));
188 skb_reset_network_header(new);
189 skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
191 new->protocol = htons(ETH_P_IPV6);
192 new->pkt_type = PACKET_HOST;
195 raw_dump_table(__func__, "raw skb data dump before receiving",
196 new->data, new->len);
198 stat = deliver_skb(new, dev);
205 /* Uncompress function for multicast destination address,
209 lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
210 struct in6_addr *ipaddr,
216 case LOWPAN_IPHC_DAM_00:
217 /* 00: 128 bits. The full address
218 * is carried in-line.
220 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
222 case LOWPAN_IPHC_DAM_01:
223 /* 01: 48 bits. The address takes
224 * the form ffXX::00XX:XXXX:XXXX.
226 ipaddr->s6_addr[0] = 0xFF;
227 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
228 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
230 case LOWPAN_IPHC_DAM_10:
231 /* 10: 32 bits. The address takes
232 * the form ffXX::00XX:XXXX.
234 ipaddr->s6_addr[0] = 0xFF;
235 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
236 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
238 case LOWPAN_IPHC_DAM_11:
239 /* 11: 8 bits. The address takes
240 * the form ff02::00XX.
242 ipaddr->s6_addr[0] = 0xFF;
243 ipaddr->s6_addr[1] = 0x02;
244 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
247 pr_debug("DAM value has a wrong value: 0x%x\n", dam);
252 pr_debug("Failed to fetch skb data\n");
256 raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is",
257 ipaddr->s6_addr, 16);
263 uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
271 fail = lowpan_fetch_skb(skb, &tmp, 1);
273 if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
274 pr_debug("UDP header uncompression\n");
275 switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
276 case LOWPAN_NHC_UDP_CS_P_00:
277 fail |= lowpan_fetch_skb(skb, &uh->source, 2);
278 fail |= lowpan_fetch_skb(skb, &uh->dest, 2);
280 case LOWPAN_NHC_UDP_CS_P_01:
281 fail |= lowpan_fetch_skb(skb, &uh->source, 2);
282 fail |= lowpan_fetch_skb(skb, &val, 1);
283 uh->dest = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
285 case LOWPAN_NHC_UDP_CS_P_10:
286 fail |= lowpan_fetch_skb(skb, &val, 1);
287 uh->source = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
288 fail |= lowpan_fetch_skb(skb, &uh->dest, 2);
290 case LOWPAN_NHC_UDP_CS_P_11:
291 fail |= lowpan_fetch_skb(skb, &val, 1);
292 uh->source = htons(LOWPAN_NHC_UDP_4BIT_PORT +
294 uh->dest = htons(LOWPAN_NHC_UDP_4BIT_PORT +
298 pr_debug("ERROR: unknown UDP format\n");
302 pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
303 ntohs(uh->source), ntohs(uh->dest));
306 if (tmp & LOWPAN_NHC_UDP_CS_C) {
307 pr_debug_ratelimited("checksum elided currently not supported\n");
310 fail |= lowpan_fetch_skb(skb, &uh->check, 2);
313 /* UDP lenght needs to be infered from the lower layers
314 * here, we obtain the hint from the remaining size of the
317 uh->len = htons(skb->len + sizeof(struct udphdr));
318 pr_debug("uncompressed UDP length: src = %d", ntohs(uh->len));
320 pr_debug("ERROR: unsupported NH format\n");
332 /* TTL uncompression values */
333 static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 };
335 int lowpan_process_data(struct sk_buff *skb, struct net_device *dev,
336 const u8 *saddr, const u8 saddr_type,
337 const u8 saddr_len, const u8 *daddr,
338 const u8 daddr_type, const u8 daddr_len,
339 u8 iphc0, u8 iphc1, skb_delivery_cb deliver_skb)
341 struct ipv6hdr hdr = {};
342 u8 tmp, num_context = 0;
345 raw_dump_table(__func__, "raw skb data dump uncompressed",
346 skb->data, skb->len);
348 /* another if the CID flag is set */
349 if (iphc1 & LOWPAN_IPHC_CID) {
350 pr_debug("CID flag is set, increase header with one\n");
351 if (lowpan_fetch_skb_u8(skb, &num_context))
357 /* Traffic Class and Flow Label */
358 switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
359 /* Traffic Class and FLow Label carried in-line
360 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
363 if (lowpan_fetch_skb_u8(skb, &tmp))
366 memcpy(&hdr.flow_lbl, &skb->data[0], 3);
368 hdr.priority = ((tmp >> 2) & 0x0f);
369 hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
370 (hdr.flow_lbl[0] & 0x0f);
372 /* Traffic class carried in-line
373 * ECN + DSCP (1 byte), Flow Label is elided
376 if (lowpan_fetch_skb_u8(skb, &tmp))
379 hdr.priority = ((tmp >> 2) & 0x0f);
380 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
382 /* Flow Label carried in-line
383 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
386 if (lowpan_fetch_skb_u8(skb, &tmp))
389 hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
390 memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
393 /* Traffic Class and Flow Label are elided */
401 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
402 /* Next header is carried inline */
403 if (lowpan_fetch_skb_u8(skb, &(hdr.nexthdr)))
406 pr_debug("NH flag is set, next header carried inline: %02x\n",
411 if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I) {
412 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
414 if (lowpan_fetch_skb_u8(skb, &(hdr.hop_limit)))
418 /* Extract SAM to the tmp variable */
419 tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
421 if (iphc1 & LOWPAN_IPHC_SAC) {
422 /* Source address context based uncompression */
423 pr_debug("SAC bit is set. Handle context based source address.\n");
424 err = uncompress_context_based_src_addr(
425 skb, &hdr.saddr, tmp);
427 /* Source address uncompression */
428 pr_debug("source address stateless compression\n");
429 err = uncompress_addr(skb, &hdr.saddr, tmp, saddr,
430 saddr_type, saddr_len);
433 /* Check on error of previous branch */
437 /* Extract DAM to the tmp variable */
438 tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
440 /* check for Multicast Compression */
441 if (iphc1 & LOWPAN_IPHC_M) {
442 if (iphc1 & LOWPAN_IPHC_DAC) {
443 pr_debug("dest: context-based mcast compression\n");
444 /* TODO: implement this */
446 err = lowpan_uncompress_multicast_daddr(
447 skb, &hdr.daddr, tmp);
452 err = uncompress_addr(skb, &hdr.daddr, tmp, daddr,
453 daddr_type, daddr_len);
454 pr_debug("dest: stateless compression mode %d dest %pI6c\n",
460 /* UDP data uncompression */
461 if (iphc0 & LOWPAN_IPHC_NH_C) {
465 if (uncompress_udp_header(skb, &uh))
468 /* replace the compressed UDP head by the uncompressed UDP
471 new = skb_copy_expand(skb, sizeof(struct udphdr),
472 skb_tailroom(skb), GFP_ATOMIC);
480 skb_push(skb, sizeof(struct udphdr));
481 skb_reset_transport_header(skb);
482 skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
484 raw_dump_table(__func__, "raw UDP header dump",
485 (u8 *)&uh, sizeof(uh));
487 hdr.nexthdr = UIP_PROTO_UDP;
490 hdr.payload_len = htons(skb->len);
492 pr_debug("skb headroom size = %d, data length = %d\n",
493 skb_headroom(skb), skb->len);
495 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
496 "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
497 hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
498 hdr.hop_limit, &hdr.daddr);
500 raw_dump_table(__func__, "raw header dump",
501 (u8 *)&hdr, sizeof(hdr));
503 return skb_deliver(skb, &hdr, dev, deliver_skb);
509 EXPORT_SYMBOL_GPL(lowpan_process_data);
511 static u8 lowpan_compress_addr_64(u8 **hc06_ptr, u8 shift,
512 const struct in6_addr *ipaddr,
513 const unsigned char *lladdr)
517 if (is_addr_mac_addr_based(ipaddr, lladdr)) {
518 val = 3; /* 0-bits */
519 pr_debug("address compression 0 bits\n");
520 } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
521 /* compress IID to 16 bits xxxx::XXXX */
522 memcpy(*hc06_ptr, &ipaddr->s6_addr16[7], 2);
524 val = 2; /* 16-bits */
525 raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
528 /* do not compress IID => xxxx::IID */
529 memcpy(*hc06_ptr, &ipaddr->s6_addr16[4], 8);
531 val = 1; /* 64-bits */
532 raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
536 return rol8(val, shift);
539 static void compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
541 struct udphdr *uh = udp_hdr(skb);
544 if (((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_MASK) ==
545 LOWPAN_NHC_UDP_4BIT_PORT) &&
546 ((ntohs(uh->dest) & LOWPAN_NHC_UDP_4BIT_MASK) ==
547 LOWPAN_NHC_UDP_4BIT_PORT)) {
548 pr_debug("UDP header: both ports compression to 4 bits\n");
549 /* compression value */
550 tmp = LOWPAN_NHC_UDP_CS_P_11;
551 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
552 /* source and destination port */
553 tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT +
554 ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4);
555 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
556 } else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
557 LOWPAN_NHC_UDP_8BIT_PORT) {
558 pr_debug("UDP header: remove 8 bits of dest\n");
559 /* compression value */
560 tmp = LOWPAN_NHC_UDP_CS_P_01;
561 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
563 lowpan_push_hc_data(hc06_ptr, &uh->source, sizeof(uh->source));
564 /* destination port */
565 tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT;
566 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
567 } else if ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) ==
568 LOWPAN_NHC_UDP_8BIT_PORT) {
569 pr_debug("UDP header: remove 8 bits of source\n");
570 /* compression value */
571 tmp = LOWPAN_NHC_UDP_CS_P_10;
572 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
574 tmp = ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT;
575 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
576 /* destination port */
577 lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
579 pr_debug("UDP header: can't compress\n");
580 /* compression value */
581 tmp = LOWPAN_NHC_UDP_CS_P_00;
582 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
584 lowpan_push_hc_data(hc06_ptr, &uh->source, sizeof(uh->source));
585 /* destination port */
586 lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
589 /* checksum is always inline */
590 lowpan_push_hc_data(hc06_ptr, &uh->check, sizeof(uh->check));
592 /* skip the UDP header */
593 skb_pull(skb, sizeof(struct udphdr));
596 int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
597 unsigned short type, const void *_daddr,
598 const void *_saddr, unsigned int len)
600 u8 tmp, iphc0, iphc1, *hc06_ptr;
604 if (type != ETH_P_IPV6)
610 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
611 "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
612 hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
613 hdr->hop_limit, &hdr->daddr);
615 raw_dump_table(__func__, "raw skb network header dump",
616 skb_network_header(skb), sizeof(struct ipv6hdr));
618 /* As we copy some bit-length fields, in the IPHC encoding bytes,
619 * we sometimes use |=
620 * If the field is 0, and the current bit value in memory is 1,
621 * this does not work. We therefore reset the IPHC encoding here
623 iphc0 = LOWPAN_DISPATCH_IPHC;
626 /* TODO: context lookup */
628 raw_dump_inline(__func__, "saddr",
629 (unsigned char *)_saddr, IEEE802154_ADDR_LEN);
630 raw_dump_inline(__func__, "daddr",
631 (unsigned char *)_daddr, IEEE802154_ADDR_LEN);
633 raw_dump_table(__func__,
634 "sending raw skb network uncompressed packet",
635 skb->data, skb->len);
637 /* Traffic class, flow label
638 * If flow label is 0, compress it. If traffic class is 0, compress it
639 * We have to process both in the same time as the offset of traffic
640 * class depends on the presence of version and flow label
643 /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
644 tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
645 tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
647 if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
648 (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
649 /* flow label can be compressed */
650 iphc0 |= LOWPAN_IPHC_FL_C;
651 if ((hdr->priority == 0) &&
652 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
653 /* compress (elide) all */
654 iphc0 |= LOWPAN_IPHC_TC_C;
656 /* compress only the flow label */
661 /* Flow label cannot be compressed */
662 if ((hdr->priority == 0) &&
663 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
664 /* compress only traffic class */
665 iphc0 |= LOWPAN_IPHC_TC_C;
666 *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
667 memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
670 /* compress nothing */
671 memcpy(hc06_ptr, hdr, 4);
672 /* replace the top byte with new ECN | DSCP format */
678 /* NOTE: payload length is always compressed */
680 /* Next Header is compress if UDP */
681 if (hdr->nexthdr == UIP_PROTO_UDP)
682 iphc0 |= LOWPAN_IPHC_NH_C;
684 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
685 *hc06_ptr = hdr->nexthdr;
690 * if 1: compress, encoding is 01
691 * if 64: compress, encoding is 10
692 * if 255: compress, encoding is 11
693 * else do not compress
695 switch (hdr->hop_limit) {
697 iphc0 |= LOWPAN_IPHC_TTL_1;
700 iphc0 |= LOWPAN_IPHC_TTL_64;
703 iphc0 |= LOWPAN_IPHC_TTL_255;
706 *hc06_ptr = hdr->hop_limit;
711 /* source address compression */
712 if (is_addr_unspecified(&hdr->saddr)) {
713 pr_debug("source address is unspecified, setting SAC\n");
714 iphc1 |= LOWPAN_IPHC_SAC;
715 /* TODO: context lookup */
716 } else if (is_addr_link_local(&hdr->saddr)) {
717 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
718 LOWPAN_IPHC_SAM_BIT, &hdr->saddr, _saddr);
719 pr_debug("source address unicast link-local %pI6c "
720 "iphc1 0x%02x\n", &hdr->saddr, iphc1);
722 pr_debug("send the full source address\n");
723 memcpy(hc06_ptr, &hdr->saddr.s6_addr16[0], 16);
727 /* destination address compression */
728 if (is_addr_mcast(&hdr->daddr)) {
729 pr_debug("destination address is multicast: ");
730 iphc1 |= LOWPAN_IPHC_M;
731 if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
732 pr_debug("compressed to 1 octet\n");
733 iphc1 |= LOWPAN_IPHC_DAM_11;
735 *hc06_ptr = hdr->daddr.s6_addr[15];
737 } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
738 pr_debug("compressed to 4 octets\n");
739 iphc1 |= LOWPAN_IPHC_DAM_10;
740 /* second byte + the last three */
741 *hc06_ptr = hdr->daddr.s6_addr[1];
742 memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[13], 3);
744 } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
745 pr_debug("compressed to 6 octets\n");
746 iphc1 |= LOWPAN_IPHC_DAM_01;
747 /* second byte + the last five */
748 *hc06_ptr = hdr->daddr.s6_addr[1];
749 memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[11], 5);
752 pr_debug("using full address\n");
753 iphc1 |= LOWPAN_IPHC_DAM_00;
754 memcpy(hc06_ptr, &hdr->daddr.s6_addr[0], 16);
758 /* TODO: context lookup */
759 if (is_addr_link_local(&hdr->daddr)) {
760 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
761 LOWPAN_IPHC_DAM_BIT, &hdr->daddr, _daddr);
762 pr_debug("dest address unicast link-local %pI6c "
763 "iphc1 0x%02x\n", &hdr->daddr, iphc1);
765 pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
766 memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
771 /* UDP header compression */
772 if (hdr->nexthdr == UIP_PROTO_UDP)
773 compress_udp_header(&hc06_ptr, skb);
778 skb_pull(skb, sizeof(struct ipv6hdr));
779 skb_reset_transport_header(skb);
780 memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
781 skb_reset_network_header(skb);
783 pr_debug("header len %d skb %u\n", (int)(hc06_ptr - head), skb->len);
785 raw_dump_table(__func__, "raw skb data dump compressed",
786 skb->data, skb->len);
789 EXPORT_SYMBOL_GPL(lowpan_header_compress);
791 MODULE_LICENSE("GPL");