]> Pileus Git - ~andy/linux/blob - net/ieee802154/6lowpan_iphc.c
Linux 3.14
[~andy/linux] / net / ieee802154 / 6lowpan_iphc.c
1 /*
2  * Copyright 2011, Siemens AG
3  * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
4  */
5
6 /*
7  * Based on patches from Jon Smirl <jonsmirl@gmail.com>
8  * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
9  *
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.
13  *
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.
18  *
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.
22  */
23
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.
27  *
28  * Redistribution and use in source and binary forms, with or without
29  * modification, are permitted provided that the following conditions
30  * are met:
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.
39  *
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
50  * SUCH DAMAGE.
51  */
52
53 #include <linux/bitops.h>
54 #include <linux/if_arp.h>
55 #include <linux/module.h>
56 #include <linux/netdevice.h>
57 #include <net/ipv6.h>
58 #include <net/af_ieee802154.h>
59
60 #include "6lowpan.h"
61
62 /*
63  * Uncompress address function for source and
64  * destination address(non-multicast).
65  *
66  * address_mode is sam value or dam value.
67  */
68 static int uncompress_addr(struct sk_buff *skb,
69                                 struct in6_addr *ipaddr, const u8 address_mode,
70                                 const u8 *lladdr, const u8 addr_type,
71                                 const u8 addr_len)
72 {
73         bool fail;
74
75         switch (address_mode) {
76         case LOWPAN_IPHC_ADDR_00:
77                 /* for global link addresses */
78                 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
79                 break;
80         case LOWPAN_IPHC_ADDR_01:
81                 /* fe:80::XXXX:XXXX:XXXX:XXXX */
82                 ipaddr->s6_addr[0] = 0xFE;
83                 ipaddr->s6_addr[1] = 0x80;
84                 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8);
85                 break;
86         case LOWPAN_IPHC_ADDR_02:
87                 /* fe:80::ff:fe00:XXXX */
88                 ipaddr->s6_addr[0] = 0xFE;
89                 ipaddr->s6_addr[1] = 0x80;
90                 ipaddr->s6_addr[11] = 0xFF;
91                 ipaddr->s6_addr[12] = 0xFE;
92                 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2);
93                 break;
94         case LOWPAN_IPHC_ADDR_03:
95                 fail = false;
96                 switch (addr_type) {
97                 case IEEE802154_ADDR_LONG:
98                         /* fe:80::XXXX:XXXX:XXXX:XXXX
99                          *        \_________________/
100                          *              hwaddr
101                          */
102                         ipaddr->s6_addr[0] = 0xFE;
103                         ipaddr->s6_addr[1] = 0x80;
104                         memcpy(&ipaddr->s6_addr[8], lladdr, addr_len);
105                         /* second bit-flip (Universe/Local)
106                          * is done according RFC2464
107                          */
108                         ipaddr->s6_addr[8] ^= 0x02;
109                         break;
110                 case IEEE802154_ADDR_SHORT:
111                         /* fe:80::ff:fe00:XXXX
112                          *                \__/
113                          *             short_addr
114                          *
115                          * Universe/Local bit is zero.
116                          */
117                         ipaddr->s6_addr[0] = 0xFE;
118                         ipaddr->s6_addr[1] = 0x80;
119                         ipaddr->s6_addr[11] = 0xFF;
120                         ipaddr->s6_addr[12] = 0xFE;
121                         ipaddr->s6_addr16[7] = htons(*((u16 *)lladdr));
122                         break;
123                 default:
124                         pr_debug("Invalid addr_type set\n");
125                         return -EINVAL;
126                 }
127                 break;
128         default:
129                 pr_debug("Invalid address mode value: 0x%x\n", address_mode);
130                 return -EINVAL;
131         }
132
133         if (fail) {
134                 pr_debug("Failed to fetch skb data\n");
135                 return -EIO;
136         }
137
138         raw_dump_inline(NULL, "Reconstructed ipv6 addr is",
139                         ipaddr->s6_addr, 16);
140
141         return 0;
142 }
143
144 /*
145  * Uncompress address function for source context
146  * based address(non-multicast).
147  */
148 static int uncompress_context_based_src_addr(struct sk_buff *skb,
149                                                 struct in6_addr *ipaddr,
150                                                 const u8 sam)
151 {
152         switch (sam) {
153         case LOWPAN_IPHC_ADDR_00:
154                 /* unspec address ::
155                  * Do nothing, address is already ::
156                  */
157                 break;
158         case LOWPAN_IPHC_ADDR_01:
159                 /* TODO */
160         case LOWPAN_IPHC_ADDR_02:
161                 /* TODO */
162         case LOWPAN_IPHC_ADDR_03:
163                 /* TODO */
164                 netdev_warn(skb->dev, "SAM value 0x%x not supported\n", sam);
165                 return -EINVAL;
166         default:
167                 pr_debug("Invalid sam value: 0x%x\n", sam);
168                 return -EINVAL;
169         }
170
171         raw_dump_inline(NULL,
172                         "Reconstructed context based ipv6 src addr is",
173                         ipaddr->s6_addr, 16);
174
175         return 0;
176 }
177
178 static int skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr,
179                 struct net_device *dev, skb_delivery_cb deliver_skb)
180 {
181         struct sk_buff *new;
182         int stat;
183
184         new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
185                                                                 GFP_ATOMIC);
186         kfree_skb(skb);
187
188         if (!new)
189                 return -ENOMEM;
190
191         skb_push(new, sizeof(struct ipv6hdr));
192         skb_reset_network_header(new);
193         skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
194
195         new->protocol = htons(ETH_P_IPV6);
196         new->pkt_type = PACKET_HOST;
197         new->dev = dev;
198
199         raw_dump_table(__func__, "raw skb data dump before receiving",
200                         new->data, new->len);
201
202         stat = deliver_skb(new, dev);
203
204         kfree_skb(new);
205
206         return stat;
207 }
208
209 /* Uncompress function for multicast destination address,
210  * when M bit is set.
211  */
212 static int
213 lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
214                 struct in6_addr *ipaddr,
215                 const u8 dam)
216 {
217         bool fail;
218
219         switch (dam) {
220         case LOWPAN_IPHC_DAM_00:
221                 /* 00:  128 bits.  The full address
222                  * is carried in-line.
223                  */
224                 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
225                 break;
226         case LOWPAN_IPHC_DAM_01:
227                 /* 01:  48 bits.  The address takes
228                  * the form ffXX::00XX:XXXX:XXXX.
229                  */
230                 ipaddr->s6_addr[0] = 0xFF;
231                 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
232                 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
233                 break;
234         case LOWPAN_IPHC_DAM_10:
235                 /* 10:  32 bits.  The address takes
236                  * the form ffXX::00XX:XXXX.
237                  */
238                 ipaddr->s6_addr[0] = 0xFF;
239                 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
240                 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
241                 break;
242         case LOWPAN_IPHC_DAM_11:
243                 /* 11:  8 bits.  The address takes
244                  * the form ff02::00XX.
245                  */
246                 ipaddr->s6_addr[0] = 0xFF;
247                 ipaddr->s6_addr[1] = 0x02;
248                 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
249                 break;
250         default:
251                 pr_debug("DAM value has a wrong value: 0x%x\n", dam);
252                 return -EINVAL;
253         }
254
255         if (fail) {
256                 pr_debug("Failed to fetch skb data\n");
257                 return -EIO;
258         }
259
260         raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is",
261                                 ipaddr->s6_addr, 16);
262
263         return 0;
264 }
265
266 static int
267 uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
268 {
269         bool fail;
270         u8 tmp = 0, val = 0;
271
272         if (!uh)
273                 goto err;
274
275         fail = lowpan_fetch_skb(skb, &tmp, 1);
276
277         if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
278                 pr_debug("UDP header uncompression\n");
279                 switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
280                 case LOWPAN_NHC_UDP_CS_P_00:
281                         fail |= lowpan_fetch_skb(skb, &uh->source, 2);
282                         fail |= lowpan_fetch_skb(skb, &uh->dest, 2);
283                         break;
284                 case LOWPAN_NHC_UDP_CS_P_01:
285                         fail |= lowpan_fetch_skb(skb, &uh->source, 2);
286                         fail |= lowpan_fetch_skb(skb, &val, 1);
287                         uh->dest = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
288                         break;
289                 case LOWPAN_NHC_UDP_CS_P_10:
290                         fail |= lowpan_fetch_skb(skb, &val, 1);
291                         uh->source = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
292                         fail |= lowpan_fetch_skb(skb, &uh->dest, 2);
293                         break;
294                 case LOWPAN_NHC_UDP_CS_P_11:
295                         fail |= lowpan_fetch_skb(skb, &val, 1);
296                         uh->source = htons(LOWPAN_NHC_UDP_4BIT_PORT +
297                                            (val >> 4));
298                         uh->dest = htons(LOWPAN_NHC_UDP_4BIT_PORT +
299                                          (val & 0x0f));
300                         break;
301                 default:
302                         pr_debug("ERROR: unknown UDP format\n");
303                         goto err;
304                         break;
305                 }
306
307                 pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
308                          ntohs(uh->source), ntohs(uh->dest));
309
310                 /* checksum */
311                 if (tmp & LOWPAN_NHC_UDP_CS_C) {
312                         pr_debug_ratelimited("checksum elided currently not supported\n");
313                         goto err;
314                 } else {
315                         fail |= lowpan_fetch_skb(skb, &uh->check, 2);
316                 }
317
318                 /*
319                  * UDP lenght needs to be infered from the lower layers
320                  * here, we obtain the hint from the remaining size of the
321                  * frame
322                  */
323                 uh->len = htons(skb->len + sizeof(struct udphdr));
324                 pr_debug("uncompressed UDP length: src = %d", ntohs(uh->len));
325         } else {
326                 pr_debug("ERROR: unsupported NH format\n");
327                 goto err;
328         }
329
330         if (fail)
331                 goto err;
332
333         return 0;
334 err:
335         return -EINVAL;
336 }
337
338 /* TTL uncompression values */
339 static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 };
340
341 int lowpan_process_data(struct sk_buff *skb, struct net_device *dev,
342                 const u8 *saddr, const u8 saddr_type, const u8 saddr_len,
343                 const u8 *daddr, const u8 daddr_type, const u8 daddr_len,
344                 u8 iphc0, u8 iphc1, skb_delivery_cb deliver_skb)
345 {
346         struct ipv6hdr hdr = {};
347         u8 tmp, num_context = 0;
348         int err;
349
350         raw_dump_table(__func__, "raw skb data dump uncompressed",
351                                 skb->data, skb->len);
352
353         /* another if the CID flag is set */
354         if (iphc1 & LOWPAN_IPHC_CID) {
355                 pr_debug("CID flag is set, increase header with one\n");
356                 if (lowpan_fetch_skb_u8(skb, &num_context))
357                         goto drop;
358         }
359
360         hdr.version = 6;
361
362         /* Traffic Class and Flow Label */
363         switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
364         /*
365          * Traffic Class and FLow Label carried in-line
366          * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
367          */
368         case 0: /* 00b */
369                 if (lowpan_fetch_skb_u8(skb, &tmp))
370                         goto drop;
371
372                 memcpy(&hdr.flow_lbl, &skb->data[0], 3);
373                 skb_pull(skb, 3);
374                 hdr.priority = ((tmp >> 2) & 0x0f);
375                 hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
376                                         (hdr.flow_lbl[0] & 0x0f);
377                 break;
378         /*
379          * Traffic class carried in-line
380          * ECN + DSCP (1 byte), Flow Label is elided
381          */
382         case 2: /* 10b */
383                 if (lowpan_fetch_skb_u8(skb, &tmp))
384                         goto drop;
385
386                 hdr.priority = ((tmp >> 2) & 0x0f);
387                 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
388                 break;
389         /*
390          * Flow Label carried in-line
391          * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
392          */
393         case 1: /* 01b */
394                 if (lowpan_fetch_skb_u8(skb, &tmp))
395                         goto drop;
396
397                 hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
398                 memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
399                 skb_pull(skb, 2);
400                 break;
401         /* Traffic Class and Flow Label are elided */
402         case 3: /* 11b */
403                 break;
404         default:
405                 break;
406         }
407
408         /* Next Header */
409         if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
410                 /* Next header is carried inline */
411                 if (lowpan_fetch_skb_u8(skb, &(hdr.nexthdr)))
412                         goto drop;
413
414                 pr_debug("NH flag is set, next header carried inline: %02x\n",
415                          hdr.nexthdr);
416         }
417
418         /* Hop Limit */
419         if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
420                 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
421         else {
422                 if (lowpan_fetch_skb_u8(skb, &(hdr.hop_limit)))
423                         goto drop;
424         }
425
426         /* Extract SAM to the tmp variable */
427         tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
428
429         if (iphc1 & LOWPAN_IPHC_SAC) {
430                 /* Source address context based uncompression */
431                 pr_debug("SAC bit is set. Handle context based source address.\n");
432                 err = uncompress_context_based_src_addr(
433                                 skb, &hdr.saddr, tmp);
434         } else {
435                 /* Source address uncompression */
436                 pr_debug("source address stateless compression\n");
437                 err = uncompress_addr(skb, &hdr.saddr, tmp, saddr,
438                                         saddr_type, saddr_len);
439         }
440
441         /* Check on error of previous branch */
442         if (err)
443                 goto drop;
444
445         /* Extract DAM to the tmp variable */
446         tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
447
448         /* check for Multicast Compression */
449         if (iphc1 & LOWPAN_IPHC_M) {
450                 if (iphc1 & LOWPAN_IPHC_DAC) {
451                         pr_debug("dest: context-based mcast compression\n");
452                         /* TODO: implement this */
453                 } else {
454                         err = lowpan_uncompress_multicast_daddr(
455                                                 skb, &hdr.daddr, tmp);
456                         if (err)
457                                 goto drop;
458                 }
459         } else {
460                 err = uncompress_addr(skb, &hdr.daddr, tmp, daddr,
461                                         daddr_type, daddr_len);
462                 pr_debug("dest: stateless compression mode %d dest %pI6c\n",
463                         tmp, &hdr.daddr);
464                 if (err)
465                         goto drop;
466         }
467
468         /* UDP data uncompression */
469         if (iphc0 & LOWPAN_IPHC_NH_C) {
470                 struct udphdr uh;
471                 struct sk_buff *new;
472                 if (uncompress_udp_header(skb, &uh))
473                         goto drop;
474
475                 /*
476                  * replace the compressed UDP head by the uncompressed UDP
477                  * header
478                  */
479                 new = skb_copy_expand(skb, sizeof(struct udphdr),
480                                       skb_tailroom(skb), GFP_ATOMIC);
481                 kfree_skb(skb);
482
483                 if (!new)
484                         return -ENOMEM;
485
486                 skb = new;
487
488                 skb_push(skb, sizeof(struct udphdr));
489                 skb_reset_transport_header(skb);
490                 skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
491
492                 raw_dump_table(__func__, "raw UDP header dump",
493                                       (u8 *)&uh, sizeof(uh));
494
495                 hdr.nexthdr = UIP_PROTO_UDP;
496         }
497
498         hdr.payload_len = htons(skb->len);
499
500         pr_debug("skb headroom size = %d, data length = %d\n",
501                  skb_headroom(skb), skb->len);
502
503         pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength  = %d\n\t"
504                  "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest    = %pI6c\n",
505                 hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
506                 hdr.hop_limit, &hdr.daddr);
507
508         raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
509                                                         sizeof(hdr));
510
511         return skb_deliver(skb, &hdr, dev, deliver_skb);
512
513 drop:
514         kfree_skb(skb);
515         return -EINVAL;
516 }
517 EXPORT_SYMBOL_GPL(lowpan_process_data);
518
519 static u8 lowpan_compress_addr_64(u8 **hc06_ptr, u8 shift,
520                                 const struct in6_addr *ipaddr,
521                                 const unsigned char *lladdr)
522 {
523         u8 val = 0;
524
525         if (is_addr_mac_addr_based(ipaddr, lladdr)) {
526                 val = 3; /* 0-bits */
527                 pr_debug("address compression 0 bits\n");
528         } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
529                 /* compress IID to 16 bits xxxx::XXXX */
530                 memcpy(*hc06_ptr, &ipaddr->s6_addr16[7], 2);
531                 *hc06_ptr += 2;
532                 val = 2; /* 16-bits */
533                 raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
534                         *hc06_ptr - 2, 2);
535         } else {
536                 /* do not compress IID => xxxx::IID */
537                 memcpy(*hc06_ptr, &ipaddr->s6_addr16[4], 8);
538                 *hc06_ptr += 8;
539                 val = 1; /* 64-bits */
540                 raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
541                         *hc06_ptr - 8, 8);
542         }
543
544         return rol8(val, shift);
545 }
546
547 static void compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
548 {
549         struct udphdr *uh = udp_hdr(skb);
550         u8 tmp;
551
552         if (((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_MASK) ==
553              LOWPAN_NHC_UDP_4BIT_PORT) &&
554             ((ntohs(uh->dest) & LOWPAN_NHC_UDP_4BIT_MASK) ==
555              LOWPAN_NHC_UDP_4BIT_PORT)) {
556                 pr_debug("UDP header: both ports compression to 4 bits\n");
557                 /* compression value */
558                 tmp = LOWPAN_NHC_UDP_CS_P_11;
559                 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
560                 /* source and destination port */
561                 tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT +
562                       ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4);
563                 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
564         } else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
565                         LOWPAN_NHC_UDP_8BIT_PORT) {
566                 pr_debug("UDP header: remove 8 bits of dest\n");
567                 /* compression value */
568                 tmp = LOWPAN_NHC_UDP_CS_P_01;
569                 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
570                 /* source port */
571                 lowpan_push_hc_data(hc06_ptr, &uh->source, sizeof(uh->source));
572                 /* destination port */
573                 tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT;
574                 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
575         } else if ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) ==
576                         LOWPAN_NHC_UDP_8BIT_PORT) {
577                 pr_debug("UDP header: remove 8 bits of source\n");
578                 /* compression value */
579                 tmp = LOWPAN_NHC_UDP_CS_P_10;
580                 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
581                 /* source port */
582                 tmp = ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT;
583                 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
584                 /* destination port */
585                 lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
586         } else {
587                 pr_debug("UDP header: can't compress\n");
588                 /* compression value */
589                 tmp = LOWPAN_NHC_UDP_CS_P_00;
590                 lowpan_push_hc_data(hc06_ptr, &tmp, sizeof(tmp));
591                 /* source port */
592                 lowpan_push_hc_data(hc06_ptr, &uh->source, sizeof(uh->source));
593                 /* destination port */
594                 lowpan_push_hc_data(hc06_ptr, &uh->dest, sizeof(uh->dest));
595         }
596
597         /* checksum is always inline */
598         lowpan_push_hc_data(hc06_ptr, &uh->check, sizeof(uh->check));
599
600         /* skip the UDP header */
601         skb_pull(skb, sizeof(struct udphdr));
602 }
603
604 int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
605                         unsigned short type, const void *_daddr,
606                         const void *_saddr, unsigned int len)
607 {
608         u8 tmp, iphc0, iphc1, *hc06_ptr;
609         struct ipv6hdr *hdr;
610         u8 head[100] = {};
611
612         if (type != ETH_P_IPV6)
613                 return -EINVAL;
614
615         hdr = ipv6_hdr(skb);
616         hc06_ptr = head + 2;
617
618         pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength  = %d\n"
619                  "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest    = %pI6c\n",
620                 hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
621                 hdr->hop_limit, &hdr->daddr);
622
623         raw_dump_table(__func__, "raw skb network header dump",
624                 skb_network_header(skb), sizeof(struct ipv6hdr));
625
626         /*
627          * As we copy some bit-length fields, in the IPHC encoding bytes,
628          * we sometimes use |=
629          * If the field is 0, and the current bit value in memory is 1,
630          * this does not work. We therefore reset the IPHC encoding here
631          */
632         iphc0 = LOWPAN_DISPATCH_IPHC;
633         iphc1 = 0;
634
635         /* TODO: context lookup */
636
637         raw_dump_inline(__func__, "saddr",
638                         (unsigned char *)_saddr, IEEE802154_ADDR_LEN);
639         raw_dump_inline(__func__, "daddr",
640                         (unsigned char *)_daddr, IEEE802154_ADDR_LEN);
641
642         raw_dump_table(__func__,
643                         "sending raw skb network uncompressed packet",
644                         skb->data, skb->len);
645
646         /*
647          * Traffic class, flow label
648          * If flow label is 0, compress it. If traffic class is 0, compress it
649          * We have to process both in the same time as the offset of traffic
650          * class depends on the presence of version and flow label
651          */
652
653         /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
654         tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
655         tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
656
657         if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
658              (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
659                 /* flow label can be compressed */
660                 iphc0 |= LOWPAN_IPHC_FL_C;
661                 if ((hdr->priority == 0) &&
662                    ((hdr->flow_lbl[0] & 0xF0) == 0)) {
663                         /* compress (elide) all */
664                         iphc0 |= LOWPAN_IPHC_TC_C;
665                 } else {
666                         /* compress only the flow label */
667                         *hc06_ptr = tmp;
668                         hc06_ptr += 1;
669                 }
670         } else {
671                 /* Flow label cannot be compressed */
672                 if ((hdr->priority == 0) &&
673                    ((hdr->flow_lbl[0] & 0xF0) == 0)) {
674                         /* compress only traffic class */
675                         iphc0 |= LOWPAN_IPHC_TC_C;
676                         *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
677                         memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
678                         hc06_ptr += 3;
679                 } else {
680                         /* compress nothing */
681                         memcpy(hc06_ptr, hdr, 4);
682                         /* replace the top byte with new ECN | DSCP format */
683                         *hc06_ptr = tmp;
684                         hc06_ptr += 4;
685                 }
686         }
687
688         /* NOTE: payload length is always compressed */
689
690         /* Next Header is compress if UDP */
691         if (hdr->nexthdr == UIP_PROTO_UDP)
692                 iphc0 |= LOWPAN_IPHC_NH_C;
693
694         if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
695                 *hc06_ptr = hdr->nexthdr;
696                 hc06_ptr += 1;
697         }
698
699         /*
700          * Hop limit
701          * if 1:   compress, encoding is 01
702          * if 64:  compress, encoding is 10
703          * if 255: compress, encoding is 11
704          * else do not compress
705          */
706         switch (hdr->hop_limit) {
707         case 1:
708                 iphc0 |= LOWPAN_IPHC_TTL_1;
709                 break;
710         case 64:
711                 iphc0 |= LOWPAN_IPHC_TTL_64;
712                 break;
713         case 255:
714                 iphc0 |= LOWPAN_IPHC_TTL_255;
715                 break;
716         default:
717                 *hc06_ptr = hdr->hop_limit;
718                 hc06_ptr += 1;
719                 break;
720         }
721
722         /* source address compression */
723         if (is_addr_unspecified(&hdr->saddr)) {
724                 pr_debug("source address is unspecified, setting SAC\n");
725                 iphc1 |= LOWPAN_IPHC_SAC;
726         /* TODO: context lookup */
727         } else if (is_addr_link_local(&hdr->saddr)) {
728                 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
729                                 LOWPAN_IPHC_SAM_BIT, &hdr->saddr, _saddr);
730                 pr_debug("source address unicast link-local %pI6c "
731                         "iphc1 0x%02x\n", &hdr->saddr, iphc1);
732         } else {
733                 pr_debug("send the full source address\n");
734                 memcpy(hc06_ptr, &hdr->saddr.s6_addr16[0], 16);
735                 hc06_ptr += 16;
736         }
737
738         /* destination address compression */
739         if (is_addr_mcast(&hdr->daddr)) {
740                 pr_debug("destination address is multicast: ");
741                 iphc1 |= LOWPAN_IPHC_M;
742                 if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
743                         pr_debug("compressed to 1 octet\n");
744                         iphc1 |= LOWPAN_IPHC_DAM_11;
745                         /* use last byte */
746                         *hc06_ptr = hdr->daddr.s6_addr[15];
747                         hc06_ptr += 1;
748                 } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
749                         pr_debug("compressed to 4 octets\n");
750                         iphc1 |= LOWPAN_IPHC_DAM_10;
751                         /* second byte + the last three */
752                         *hc06_ptr = hdr->daddr.s6_addr[1];
753                         memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[13], 3);
754                         hc06_ptr += 4;
755                 } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
756                         pr_debug("compressed to 6 octets\n");
757                         iphc1 |= LOWPAN_IPHC_DAM_01;
758                         /* second byte + the last five */
759                         *hc06_ptr = hdr->daddr.s6_addr[1];
760                         memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[11], 5);
761                         hc06_ptr += 6;
762                 } else {
763                         pr_debug("using full address\n");
764                         iphc1 |= LOWPAN_IPHC_DAM_00;
765                         memcpy(hc06_ptr, &hdr->daddr.s6_addr[0], 16);
766                         hc06_ptr += 16;
767                 }
768         } else {
769                 /* TODO: context lookup */
770                 if (is_addr_link_local(&hdr->daddr)) {
771                         iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
772                                 LOWPAN_IPHC_DAM_BIT, &hdr->daddr, _daddr);
773                         pr_debug("dest address unicast link-local %pI6c "
774                                 "iphc1 0x%02x\n", &hdr->daddr, iphc1);
775                 } else {
776                         pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
777                         memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
778                         hc06_ptr += 16;
779                 }
780         }
781
782         /* UDP header compression */
783         if (hdr->nexthdr == UIP_PROTO_UDP)
784                 compress_udp_header(&hc06_ptr, skb);
785
786         head[0] = iphc0;
787         head[1] = iphc1;
788
789         skb_pull(skb, sizeof(struct ipv6hdr));
790         skb_reset_transport_header(skb);
791         memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
792         skb_reset_network_header(skb);
793
794         pr_debug("header len %d skb %u\n", (int)(hc06_ptr - head), skb->len);
795
796         raw_dump_table(__func__, "raw skb data dump compressed",
797                                 skb->data, skb->len);
798         return 0;
799 }
800 EXPORT_SYMBOL_GPL(lowpan_header_compress);
801
802 MODULE_LICENSE("GPL");