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/netdevice.h>
57 #include <net/af_ieee802154.h>
62 * Uncompress address function for source and
63 * destination address(non-multicast).
65 * address_mode is sam value or dam value.
67 static int uncompress_addr(struct sk_buff *skb,
68 struct in6_addr *ipaddr, const u8 address_mode,
69 const u8 *lladdr, const u8 addr_type,
74 switch (address_mode) {
75 case LOWPAN_IPHC_ADDR_00:
76 /* for global link addresses */
77 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
79 case LOWPAN_IPHC_ADDR_01:
80 /* fe:80::XXXX:XXXX:XXXX:XXXX */
81 ipaddr->s6_addr[0] = 0xFE;
82 ipaddr->s6_addr[1] = 0x80;
83 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8);
85 case LOWPAN_IPHC_ADDR_02:
86 /* fe:80::ff:fe00:XXXX */
87 ipaddr->s6_addr[0] = 0xFE;
88 ipaddr->s6_addr[1] = 0x80;
89 ipaddr->s6_addr[11] = 0xFF;
90 ipaddr->s6_addr[12] = 0xFE;
91 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2);
93 case LOWPAN_IPHC_ADDR_03:
96 case IEEE802154_ADDR_LONG:
97 /* fe:80::XXXX:XXXX:XXXX:XXXX
101 ipaddr->s6_addr[0] = 0xFE;
102 ipaddr->s6_addr[1] = 0x80;
103 memcpy(&ipaddr->s6_addr[8], lladdr, addr_len);
104 /* second bit-flip (Universe/Local)
105 * is done according RFC2464
107 ipaddr->s6_addr[8] ^= 0x02;
109 case IEEE802154_ADDR_SHORT:
110 /* fe:80::ff:fe00:XXXX
114 * Universe/Local bit is zero.
116 ipaddr->s6_addr[0] = 0xFE;
117 ipaddr->s6_addr[1] = 0x80;
118 ipaddr->s6_addr[11] = 0xFF;
119 ipaddr->s6_addr[12] = 0xFE;
120 ipaddr->s6_addr16[7] = htons(*((u16 *)lladdr));
123 pr_debug("Invalid addr_type set\n");
128 pr_debug("Invalid address mode value: 0x%x\n", address_mode);
133 pr_debug("Failed to fetch skb data\n");
137 raw_dump_inline(NULL, "Reconstructed ipv6 addr is",
138 ipaddr->s6_addr, 16);
144 * Uncompress address function for source context
145 * based address(non-multicast).
147 static int uncompress_context_based_src_addr(struct sk_buff *skb,
148 struct in6_addr *ipaddr,
152 case LOWPAN_IPHC_ADDR_00:
154 * Do nothing, address is already ::
157 case LOWPAN_IPHC_ADDR_01:
159 case LOWPAN_IPHC_ADDR_02:
161 case LOWPAN_IPHC_ADDR_03:
163 netdev_warn(skb->dev, "SAM value 0x%x not supported\n", sam);
166 pr_debug("Invalid sam value: 0x%x\n", sam);
170 raw_dump_inline(NULL,
171 "Reconstructed context based ipv6 src addr is",
172 ipaddr->s6_addr, 16);
177 static int skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr,
178 struct net_device *dev, skb_delivery_cb deliver_skb)
183 new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
190 skb_push(new, sizeof(struct ipv6hdr));
191 skb_reset_network_header(new);
192 skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
194 new->protocol = htons(ETH_P_IPV6);
195 new->pkt_type = PACKET_HOST;
198 raw_dump_table(__func__, "raw skb data dump before receiving",
199 new->data, new->len);
201 stat = deliver_skb(new, dev);
208 /* Uncompress function for multicast destination address,
212 lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
213 struct in6_addr *ipaddr,
219 case LOWPAN_IPHC_DAM_00:
220 /* 00: 128 bits. The full address
221 * is carried in-line.
223 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
225 case LOWPAN_IPHC_DAM_01:
226 /* 01: 48 bits. The address takes
227 * the form ffXX::00XX:XXXX:XXXX.
229 ipaddr->s6_addr[0] = 0xFF;
230 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
231 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
233 case LOWPAN_IPHC_DAM_10:
234 /* 10: 32 bits. The address takes
235 * the form ffXX::00XX:XXXX.
237 ipaddr->s6_addr[0] = 0xFF;
238 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
239 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
241 case LOWPAN_IPHC_DAM_11:
242 /* 11: 8 bits. The address takes
243 * the form ff02::00XX.
245 ipaddr->s6_addr[0] = 0xFF;
246 ipaddr->s6_addr[1] = 0x02;
247 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
250 pr_debug("DAM value has a wrong value: 0x%x\n", dam);
255 pr_debug("Failed to fetch skb data\n");
259 raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is",
260 ipaddr->s6_addr, 16);
266 uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
274 fail = lowpan_fetch_skb(skb, &tmp, 1);
276 if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
277 pr_debug("UDP header uncompression\n");
278 switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
279 case LOWPAN_NHC_UDP_CS_P_00:
280 fail |= lowpan_fetch_skb(skb, &uh->source, 2);
281 fail |= lowpan_fetch_skb(skb, &uh->dest, 2);
283 case LOWPAN_NHC_UDP_CS_P_01:
284 fail |= lowpan_fetch_skb(skb, &uh->source, 2);
285 fail |= lowpan_fetch_skb(skb, &val, 1);
286 uh->dest = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
288 case LOWPAN_NHC_UDP_CS_P_10:
289 fail |= lowpan_fetch_skb(skb, &val, 1);
290 uh->source = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
291 fail |= lowpan_fetch_skb(skb, &uh->dest, 2);
293 case LOWPAN_NHC_UDP_CS_P_11:
294 fail |= lowpan_fetch_skb(skb, &val, 1);
295 uh->source = htons(LOWPAN_NHC_UDP_4BIT_PORT +
297 uh->dest = htons(LOWPAN_NHC_UDP_4BIT_PORT +
301 pr_debug("ERROR: unknown UDP format\n");
306 pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
307 ntohs(uh->source), ntohs(uh->dest));
310 if (tmp & LOWPAN_NHC_UDP_CS_C) {
311 pr_debug_ratelimited("checksum elided currently not supported\n");
314 fail |= lowpan_fetch_skb(skb, &uh->check, 2);
318 * UDP lenght needs to be infered from the lower layers
319 * here, we obtain the hint from the remaining size of the
322 uh->len = htons(skb->len + sizeof(struct udphdr));
323 pr_debug("uncompressed UDP length: src = %d", ntohs(uh->len));
325 pr_debug("ERROR: unsupported NH format\n");
337 /* TTL uncompression values */
338 static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 };
340 int lowpan_process_data(struct sk_buff *skb, struct net_device *dev,
341 const u8 *saddr, const u8 saddr_type, const u8 saddr_len,
342 const u8 *daddr, const u8 daddr_type, const u8 daddr_len,
343 u8 iphc0, u8 iphc1, skb_delivery_cb deliver_skb)
345 struct ipv6hdr hdr = {};
346 u8 tmp, num_context = 0;
349 raw_dump_table(__func__, "raw skb data dump uncompressed",
350 skb->data, skb->len);
352 /* another if the CID flag is set */
353 if (iphc1 & LOWPAN_IPHC_CID) {
354 pr_debug("CID flag is set, increase header with one\n");
355 if (lowpan_fetch_skb_u8(skb, &num_context))
361 /* Traffic Class and Flow Label */
362 switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
364 * Traffic Class and FLow Label carried in-line
365 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
368 if (lowpan_fetch_skb_u8(skb, &tmp))
371 memcpy(&hdr.flow_lbl, &skb->data[0], 3);
373 hdr.priority = ((tmp >> 2) & 0x0f);
374 hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
375 (hdr.flow_lbl[0] & 0x0f);
378 * Traffic class carried in-line
379 * ECN + DSCP (1 byte), Flow Label is elided
382 if (lowpan_fetch_skb_u8(skb, &tmp))
385 hdr.priority = ((tmp >> 2) & 0x0f);
386 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
389 * Flow Label carried in-line
390 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
393 if (lowpan_fetch_skb_u8(skb, &tmp))
396 hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
397 memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
400 /* Traffic Class and Flow Label are elided */
408 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
409 /* Next header is carried inline */
410 if (lowpan_fetch_skb_u8(skb, &(hdr.nexthdr)))
413 pr_debug("NH flag is set, next header carried inline: %02x\n",
418 if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
419 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
421 if (lowpan_fetch_skb_u8(skb, &(hdr.hop_limit)))
425 /* Extract SAM to the tmp variable */
426 tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
428 if (iphc1 & LOWPAN_IPHC_SAC) {
429 /* Source address context based uncompression */
430 pr_debug("SAC bit is set. Handle context based source address.\n");
431 err = uncompress_context_based_src_addr(
432 skb, &hdr.saddr, tmp);
434 /* Source address uncompression */
435 pr_debug("source address stateless compression\n");
436 err = uncompress_addr(skb, &hdr.saddr, tmp, saddr,
437 saddr_type, saddr_len);
440 /* Check on error of previous branch */
444 /* Extract DAM to the tmp variable */
445 tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
447 /* check for Multicast Compression */
448 if (iphc1 & LOWPAN_IPHC_M) {
449 if (iphc1 & LOWPAN_IPHC_DAC) {
450 pr_debug("dest: context-based mcast compression\n");
451 /* TODO: implement this */
453 err = lowpan_uncompress_multicast_daddr(
454 skb, &hdr.daddr, tmp);
459 err = uncompress_addr(skb, &hdr.daddr, tmp, daddr,
460 daddr_type, daddr_len);
461 pr_debug("dest: stateless compression mode %d dest %pI6c\n",
467 /* UDP data uncompression */
468 if (iphc0 & LOWPAN_IPHC_NH_C) {
471 if (uncompress_udp_header(skb, &uh))
475 * replace the compressed UDP head by the uncompressed UDP
478 new = skb_copy_expand(skb, sizeof(struct udphdr),
479 skb_tailroom(skb), GFP_ATOMIC);
487 skb_push(skb, sizeof(struct udphdr));
488 skb_reset_transport_header(skb);
489 skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
491 raw_dump_table(__func__, "raw UDP header dump",
492 (u8 *)&uh, sizeof(uh));
494 hdr.nexthdr = UIP_PROTO_UDP;
497 hdr.payload_len = htons(skb->len);
499 pr_debug("skb headroom size = %d, data length = %d\n",
500 skb_headroom(skb), skb->len);
502 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
503 "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
504 hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
505 hdr.hop_limit, &hdr.daddr);
507 raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
510 return skb_deliver(skb, &hdr, dev, deliver_skb);
516 EXPORT_SYMBOL_GPL(lowpan_process_data);
518 static u8 lowpan_compress_addr_64(u8 **hc06_ptr, u8 shift,
519 const struct in6_addr *ipaddr,
520 const unsigned char *lladdr)
524 if (is_addr_mac_addr_based(ipaddr, lladdr)) {
525 val = 3; /* 0-bits */
526 pr_debug("address compression 0 bits\n");
527 } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
528 /* compress IID to 16 bits xxxx::XXXX */
529 memcpy(*hc06_ptr, &ipaddr->s6_addr16[7], 2);
531 val = 2; /* 16-bits */
532 raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
535 /* do not compress IID => xxxx::IID */
536 memcpy(*hc06_ptr, &ipaddr->s6_addr16[4], 8);
538 val = 1; /* 64-bits */
539 raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
543 return rol8(val, shift);
546 static void compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
548 struct udphdr *uh = udp_hdr(skb);
551 if (((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_MASK) ==
552 LOWPAN_NHC_UDP_4BIT_PORT) &&
553 ((ntohs(uh->dest) & LOWPAN_NHC_UDP_4BIT_MASK) ==
554 LOWPAN_NHC_UDP_4BIT_PORT)) {
555 pr_debug("UDP header: both ports compression to 4 bits\n");
556 tmp = LOWPAN_NHC_UDP_CS_P_11;
557 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
558 tmp = /* subtraction is faster */
559 (u8)((ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT) +
560 ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4));
561 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
562 } else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
563 LOWPAN_NHC_UDP_8BIT_PORT) {
564 pr_debug("UDP header: remove 8 bits of dest\n");
565 tmp = LOWPAN_NHC_UDP_CS_P_01;
566 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
567 lowpan_push_hc_data(hc06_ptr, &uh->source, sizeof(uh->source));
568 tmp = (u8)(ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT);
569 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
570 } else if ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) ==
571 LOWPAN_NHC_UDP_8BIT_PORT) {
572 pr_debug("UDP header: remove 8 bits of source\n");
573 tmp = LOWPAN_NHC_UDP_CS_P_10;
574 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
575 tmp = (u8)(ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT);
576 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
577 lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
579 pr_debug("UDP header: can't compress\n");
580 tmp = LOWPAN_NHC_UDP_CS_P_00;
581 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
582 lowpan_push_hc_data(hc06_ptr, &uh->source, sizeof(uh->source));
583 lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
586 /* checksum is always inline */
587 lowpan_push_hc_data(hc06_ptr, &uh->check, sizeof(uh->check));
589 /* skip the UDP header */
590 skb_pull(skb, sizeof(struct udphdr));
593 int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
594 unsigned short type, const void *_daddr,
595 const void *_saddr, unsigned int len)
597 u8 tmp, iphc0, iphc1, *hc06_ptr;
601 if (type != ETH_P_IPV6)
607 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
608 "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
609 hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
610 hdr->hop_limit, &hdr->daddr);
612 raw_dump_table(__func__, "raw skb network header dump",
613 skb_network_header(skb), sizeof(struct ipv6hdr));
616 * As we copy some bit-length fields, in the IPHC encoding bytes,
617 * we sometimes use |=
618 * If the field is 0, and the current bit value in memory is 1,
619 * this does not work. We therefore reset the IPHC encoding here
621 iphc0 = LOWPAN_DISPATCH_IPHC;
624 /* TODO: context lookup */
626 raw_dump_inline(__func__, "saddr",
627 (unsigned char *)_saddr, IEEE802154_ADDR_LEN);
628 raw_dump_inline(__func__, "daddr",
629 (unsigned char *)_daddr, IEEE802154_ADDR_LEN);
631 raw_dump_table(__func__,
632 "sending raw skb network uncompressed packet",
633 skb->data, skb->len);
636 * Traffic class, flow label
637 * If flow label is 0, compress it. If traffic class is 0, compress it
638 * We have to process both in the same time as the offset of traffic
639 * class depends on the presence of version and flow label
642 /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
643 tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
644 tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
646 if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
647 (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
648 /* flow label can be compressed */
649 iphc0 |= LOWPAN_IPHC_FL_C;
650 if ((hdr->priority == 0) &&
651 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
652 /* compress (elide) all */
653 iphc0 |= LOWPAN_IPHC_TC_C;
655 /* compress only the flow label */
660 /* Flow label cannot be compressed */
661 if ((hdr->priority == 0) &&
662 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
663 /* compress only traffic class */
664 iphc0 |= LOWPAN_IPHC_TC_C;
665 *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
666 memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
669 /* compress nothing */
670 memcpy(hc06_ptr, &hdr, 4);
671 /* replace the top byte with new ECN | DSCP format */
677 /* NOTE: payload length is always compressed */
679 /* Next Header is compress if UDP */
680 if (hdr->nexthdr == UIP_PROTO_UDP)
681 iphc0 |= LOWPAN_IPHC_NH_C;
683 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
684 *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);