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