6lowpan: remove lowpan_fetch_skb_u8
[deliverable/linux.git] / net / 6lowpan / iphc.c
CommitLineData
8df8c56a
JR
1/*
2 * Copyright 2011, Siemens AG
3 * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
4 */
5
4710d806 6/* Based on patches from Jon Smirl <jonsmirl@gmail.com>
8df8c56a
JR
7 * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
8 *
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.
12 *
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.
17 *
8df8c56a
JR
18 */
19
20/* Jon's code is based on 6lowpan implementation for Contiki which is:
21 * Copyright (c) 2008, Swedish Institute of Computer Science.
22 * All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. Neither the name of the Institute nor the names of its contributors
33 * may be used to endorse or promote products derived from this software
34 * without specific prior written permission.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 */
48
49#include <linux/bitops.h>
50#include <linux/if_arp.h>
51#include <linux/netdevice.h>
8911d774 52
cefc8c8a 53#include <net/6lowpan.h>
8df8c56a 54#include <net/ipv6.h>
8911d774
AA
55
56/* special link-layer handling */
57#include <net/mac802154.h>
8df8c56a 58
cc6ed268
AA
59#include "nhc.h"
60
8911d774
AA
61static inline void iphc_uncompress_eui64_lladdr(struct in6_addr *ipaddr,
62 const void *lladdr)
63{
64 /* fe:80::XXXX:XXXX:XXXX:XXXX
65 * \_________________/
66 * hwaddr
67 */
68 ipaddr->s6_addr[0] = 0xFE;
69 ipaddr->s6_addr[1] = 0x80;
70 memcpy(&ipaddr->s6_addr[8], lladdr, EUI64_ADDR_LEN);
71 /* second bit-flip (Universe/Local)
72 * is done according RFC2464
73 */
74 ipaddr->s6_addr[8] ^= 0x02;
75}
76
77static inline void iphc_uncompress_802154_lladdr(struct in6_addr *ipaddr,
78 const void *lladdr)
79{
80 const struct ieee802154_addr *addr = lladdr;
81 u8 eui64[EUI64_ADDR_LEN] = { };
82
83 switch (addr->mode) {
84 case IEEE802154_ADDR_LONG:
85 ieee802154_le64_to_be64(eui64, &addr->extended_addr);
86 iphc_uncompress_eui64_lladdr(ipaddr, eui64);
87 break;
88 case IEEE802154_ADDR_SHORT:
89 /* fe:80::ff:fe00:XXXX
90 * \__/
91 * short_addr
92 *
93 * Universe/Local bit is zero.
94 */
95 ipaddr->s6_addr[0] = 0xFE;
96 ipaddr->s6_addr[1] = 0x80;
97 ipaddr->s6_addr[11] = 0xFF;
98 ipaddr->s6_addr[12] = 0xFE;
99 ieee802154_le16_to_be16(&ipaddr->s6_addr16[7],
100 &addr->short_addr);
101 break;
102 default:
103 /* should never handled and filtered by 802154 6lowpan */
104 WARN_ON_ONCE(1);
105 break;
106 }
107}
108
4710d806 109/* Uncompress address function for source and
8df8c56a
JR
110 * destination address(non-multicast).
111 *
112 * address_mode is sam value or dam value.
113 */
8911d774
AA
114static int uncompress_addr(struct sk_buff *skb, const struct net_device *dev,
115 struct in6_addr *ipaddr, u8 address_mode,
116 const void *lladdr)
8df8c56a
JR
117{
118 bool fail;
119
120 switch (address_mode) {
121 case LOWPAN_IPHC_ADDR_00:
122 /* for global link addresses */
123 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
124 break;
125 case LOWPAN_IPHC_ADDR_01:
126 /* fe:80::XXXX:XXXX:XXXX:XXXX */
127 ipaddr->s6_addr[0] = 0xFE;
128 ipaddr->s6_addr[1] = 0x80;
129 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8);
130 break;
131 case LOWPAN_IPHC_ADDR_02:
132 /* fe:80::ff:fe00:XXXX */
133 ipaddr->s6_addr[0] = 0xFE;
134 ipaddr->s6_addr[1] = 0x80;
135 ipaddr->s6_addr[11] = 0xFF;
136 ipaddr->s6_addr[12] = 0xFE;
137 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2);
138 break;
139 case LOWPAN_IPHC_ADDR_03:
140 fail = false;
8911d774
AA
141 switch (lowpan_priv(dev)->lltype) {
142 case LOWPAN_LLTYPE_IEEE802154:
143 iphc_uncompress_802154_lladdr(ipaddr, lladdr);
8df8c56a
JR
144 break;
145 default:
8911d774
AA
146 iphc_uncompress_eui64_lladdr(ipaddr, lladdr);
147 break;
8df8c56a
JR
148 }
149 break;
150 default:
151 pr_debug("Invalid address mode value: 0x%x\n", address_mode);
152 return -EINVAL;
153 }
154
155 if (fail) {
156 pr_debug("Failed to fetch skb data\n");
157 return -EIO;
158 }
159
160 raw_dump_inline(NULL, "Reconstructed ipv6 addr is",
161 ipaddr->s6_addr, 16);
162
163 return 0;
164}
165
4710d806 166/* Uncompress address function for source context
8df8c56a
JR
167 * based address(non-multicast).
168 */
169static int uncompress_context_based_src_addr(struct sk_buff *skb,
4710d806
VB
170 struct in6_addr *ipaddr,
171 const u8 sam)
8df8c56a
JR
172{
173 switch (sam) {
174 case LOWPAN_IPHC_ADDR_00:
175 /* unspec address ::
176 * Do nothing, address is already ::
177 */
178 break;
179 case LOWPAN_IPHC_ADDR_01:
180 /* TODO */
181 case LOWPAN_IPHC_ADDR_02:
182 /* TODO */
183 case LOWPAN_IPHC_ADDR_03:
184 /* TODO */
185 netdev_warn(skb->dev, "SAM value 0x%x not supported\n", sam);
186 return -EINVAL;
187 default:
188 pr_debug("Invalid sam value: 0x%x\n", sam);
189 return -EINVAL;
190 }
191
192 raw_dump_inline(NULL,
193 "Reconstructed context based ipv6 src addr is",
194 ipaddr->s6_addr, 16);
195
196 return 0;
197}
198
8df8c56a
JR
199/* Uncompress function for multicast destination address,
200 * when M bit is set.
201 */
7fc4cfda
MH
202static int lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
203 struct in6_addr *ipaddr,
204 const u8 dam)
8df8c56a
JR
205{
206 bool fail;
207
208 switch (dam) {
209 case LOWPAN_IPHC_DAM_00:
210 /* 00: 128 bits. The full address
211 * is carried in-line.
212 */
213 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
214 break;
215 case LOWPAN_IPHC_DAM_01:
216 /* 01: 48 bits. The address takes
217 * the form ffXX::00XX:XXXX:XXXX.
218 */
219 ipaddr->s6_addr[0] = 0xFF;
220 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
221 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
222 break;
223 case LOWPAN_IPHC_DAM_10:
224 /* 10: 32 bits. The address takes
225 * the form ffXX::00XX:XXXX.
226 */
227 ipaddr->s6_addr[0] = 0xFF;
228 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
229 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
230 break;
231 case LOWPAN_IPHC_DAM_11:
232 /* 11: 8 bits. The address takes
233 * the form ff02::00XX.
234 */
235 ipaddr->s6_addr[0] = 0xFF;
236 ipaddr->s6_addr[1] = 0x02;
237 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
238 break;
239 default:
240 pr_debug("DAM value has a wrong value: 0x%x\n", dam);
241 return -EINVAL;
242 }
243
244 if (fail) {
245 pr_debug("Failed to fetch skb data\n");
246 return -EIO;
247 }
248
249 raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is",
7fc4cfda 250 ipaddr->s6_addr, 16);
8df8c56a
JR
251
252 return 0;
253}
254
8df8c56a
JR
255/* TTL uncompression values */
256static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 };
257
8911d774
AA
258int lowpan_header_decompress(struct sk_buff *skb, const struct net_device *dev,
259 const void *daddr, const void *saddr)
8df8c56a
JR
260{
261 struct ipv6hdr hdr = {};
8911d774 262 u8 iphc0, iphc1, tmp, num_context = 0;
8df8c56a
JR
263 int err;
264
265 raw_dump_table(__func__, "raw skb data dump uncompressed",
4710d806 266 skb->data, skb->len);
8df8c56a 267
478208e3
AA
268 if (lowpan_fetch_skb(skb, &iphc0, sizeof(iphc0)) ||
269 lowpan_fetch_skb(skb, &iphc1, sizeof(iphc1)))
8911d774
AA
270 return -EINVAL;
271
8df8c56a
JR
272 /* another if the CID flag is set */
273 if (iphc1 & LOWPAN_IPHC_CID) {
274 pr_debug("CID flag is set, increase header with one\n");
4ebc960f 275 if (lowpan_fetch_skb(skb, &num_context, sizeof(num_context)))
56b2c3ee 276 return -EINVAL;
8df8c56a
JR
277 }
278
279 hdr.version = 6;
280
281 /* Traffic Class and Flow Label */
282 switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
4710d806 283 /* Traffic Class and FLow Label carried in-line
8df8c56a
JR
284 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
285 */
286 case 0: /* 00b */
4ebc960f 287 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
56b2c3ee 288 return -EINVAL;
8df8c56a
JR
289
290 memcpy(&hdr.flow_lbl, &skb->data[0], 3);
291 skb_pull(skb, 3);
292 hdr.priority = ((tmp >> 2) & 0x0f);
293 hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
294 (hdr.flow_lbl[0] & 0x0f);
295 break;
4710d806 296 /* Traffic class carried in-line
8df8c56a
JR
297 * ECN + DSCP (1 byte), Flow Label is elided
298 */
299 case 2: /* 10b */
4ebc960f 300 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
56b2c3ee 301 return -EINVAL;
8df8c56a
JR
302
303 hdr.priority = ((tmp >> 2) & 0x0f);
304 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
305 break;
4710d806 306 /* Flow Label carried in-line
8df8c56a
JR
307 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
308 */
309 case 1: /* 01b */
4ebc960f 310 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
56b2c3ee 311 return -EINVAL;
8df8c56a 312
77e867b5 313 hdr.flow_lbl[0] = (tmp & 0x0F) | ((tmp >> 2) & 0x30);
8df8c56a
JR
314 memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
315 skb_pull(skb, 2);
316 break;
317 /* Traffic Class and Flow Label are elided */
318 case 3: /* 11b */
319 break;
320 default:
321 break;
322 }
323
324 /* Next Header */
325 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
326 /* Next header is carried inline */
4ebc960f 327 if (lowpan_fetch_skb(skb, &hdr.nexthdr, sizeof(hdr.nexthdr)))
56b2c3ee 328 return -EINVAL;
8df8c56a
JR
329
330 pr_debug("NH flag is set, next header carried inline: %02x\n",
331 hdr.nexthdr);
332 }
333
334 /* Hop Limit */
4710d806 335 if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I) {
8df8c56a 336 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
4710d806 337 } else {
4ebc960f
AA
338 if (lowpan_fetch_skb(skb, &hdr.hop_limit,
339 sizeof(hdr.hop_limit)))
56b2c3ee 340 return -EINVAL;
8df8c56a
JR
341 }
342
343 /* Extract SAM to the tmp variable */
344 tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
345
346 if (iphc1 & LOWPAN_IPHC_SAC) {
347 /* Source address context based uncompression */
348 pr_debug("SAC bit is set. Handle context based source address.\n");
7fc4cfda 349 err = uncompress_context_based_src_addr(skb, &hdr.saddr, tmp);
8df8c56a
JR
350 } else {
351 /* Source address uncompression */
352 pr_debug("source address stateless compression\n");
8911d774 353 err = uncompress_addr(skb, dev, &hdr.saddr, tmp, saddr);
8df8c56a
JR
354 }
355
356 /* Check on error of previous branch */
357 if (err)
56b2c3ee 358 return -EINVAL;
8df8c56a
JR
359
360 /* Extract DAM to the tmp variable */
361 tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
362
363 /* check for Multicast Compression */
364 if (iphc1 & LOWPAN_IPHC_M) {
365 if (iphc1 & LOWPAN_IPHC_DAC) {
366 pr_debug("dest: context-based mcast compression\n");
367 /* TODO: implement this */
368 } else {
7fc4cfda
MH
369 err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr,
370 tmp);
371
8df8c56a 372 if (err)
56b2c3ee 373 return -EINVAL;
8df8c56a
JR
374 }
375 } else {
8911d774 376 err = uncompress_addr(skb, dev, &hdr.daddr, tmp, daddr);
8df8c56a 377 pr_debug("dest: stateless compression mode %d dest %pI6c\n",
4710d806 378 tmp, &hdr.daddr);
8df8c56a 379 if (err)
56b2c3ee 380 return -EINVAL;
8df8c56a
JR
381 }
382
cc6ed268 383 /* Next header data uncompression */
8df8c56a 384 if (iphc0 & LOWPAN_IPHC_NH_C) {
cc6ed268
AA
385 err = lowpan_nhc_do_uncompression(skb, dev, &hdr);
386 if (err < 0)
11e3ff70 387 return err;
11e3ff70
MT
388 } else {
389 err = skb_cow(skb, sizeof(hdr));
56b2c3ee 390 if (unlikely(err))
11e3ff70 391 return err;
8df8c56a
JR
392 }
393
72a5e6bb
AA
394 switch (lowpan_priv(dev)->lltype) {
395 case LOWPAN_LLTYPE_IEEE802154:
396 if (lowpan_802154_cb(skb)->d_size)
397 hdr.payload_len = htons(lowpan_802154_cb(skb)->d_size -
398 sizeof(struct ipv6hdr));
399 else
400 hdr.payload_len = htons(skb->len);
401 break;
402 default:
403 hdr.payload_len = htons(skb->len);
404 break;
405 }
8df8c56a
JR
406
407 pr_debug("skb headroom size = %d, data length = %d\n",
408 skb_headroom(skb), skb->len);
409
410 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
411 "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
412 hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
413 hdr.hop_limit, &hdr.daddr);
414
f8b36176
MT
415 skb_push(skb, sizeof(hdr));
416 skb_reset_network_header(skb);
417 skb_copy_to_linear_data(skb, &hdr, sizeof(hdr));
8df8c56a 418
f8b36176 419 raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
8df8c56a 420
f8b36176 421 return 0;
8df8c56a 422}
01141234 423EXPORT_SYMBOL_GPL(lowpan_header_decompress);
8df8c56a 424
84ca5e03 425static u8 lowpan_compress_addr_64(u8 **hc_ptr, u8 shift,
4710d806
VB
426 const struct in6_addr *ipaddr,
427 const unsigned char *lladdr)
8df8c56a
JR
428{
429 u8 val = 0;
430
431 if (is_addr_mac_addr_based(ipaddr, lladdr)) {
432 val = 3; /* 0-bits */
433 pr_debug("address compression 0 bits\n");
434 } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
435 /* compress IID to 16 bits xxxx::XXXX */
85c71240 436 lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
8df8c56a
JR
437 val = 2; /* 16-bits */
438 raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
84ca5e03 439 *hc_ptr - 2, 2);
8df8c56a
JR
440 } else {
441 /* do not compress IID => xxxx::IID */
85c71240 442 lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
8df8c56a
JR
443 val = 1; /* 64-bits */
444 raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
84ca5e03 445 *hc_ptr - 8, 8);
8df8c56a
JR
446 }
447
448 return rol8(val, shift);
449}
450
a6f77389
AA
451int lowpan_header_compress(struct sk_buff *skb, const struct net_device *dev,
452 const void *daddr, const void *saddr)
8df8c56a 453{
84ca5e03 454 u8 tmp, iphc0, iphc1, *hc_ptr;
8df8c56a 455 struct ipv6hdr *hdr;
bf513fd6 456 u8 head[LOWPAN_IPHC_MAX_HC_BUF_LEN] = {};
cc6ed268 457 int ret, addr_type;
8df8c56a 458
a6f77389 459 if (skb->protocol != htons(ETH_P_IPV6))
8df8c56a
JR
460 return -EINVAL;
461
462 hdr = ipv6_hdr(skb);
84ca5e03 463 hc_ptr = head + 2;
8df8c56a
JR
464
465 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
466 "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
4710d806
VB
467 hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
468 hdr->hop_limit, &hdr->daddr);
8df8c56a
JR
469
470 raw_dump_table(__func__, "raw skb network header dump",
4710d806 471 skb_network_header(skb), sizeof(struct ipv6hdr));
8df8c56a 472
4710d806 473 /* As we copy some bit-length fields, in the IPHC encoding bytes,
8df8c56a
JR
474 * we sometimes use |=
475 * If the field is 0, and the current bit value in memory is 1,
476 * this does not work. We therefore reset the IPHC encoding here
477 */
478 iphc0 = LOWPAN_DISPATCH_IPHC;
479 iphc1 = 0;
480
481 /* TODO: context lookup */
482
a6f77389
AA
483 raw_dump_inline(__func__, "saddr", saddr, EUI64_ADDR_LEN);
484 raw_dump_inline(__func__, "daddr", daddr, EUI64_ADDR_LEN);
8df8c56a 485
7fc4cfda 486 raw_dump_table(__func__, "sending raw skb network uncompressed packet",
4710d806 487 skb->data, skb->len);
8df8c56a 488
4710d806 489 /* Traffic class, flow label
8df8c56a
JR
490 * If flow label is 0, compress it. If traffic class is 0, compress it
491 * We have to process both in the same time as the offset of traffic
492 * class depends on the presence of version and flow label
493 */
494
84ca5e03 495 /* hc format of TC is ECN | DSCP , original one is DSCP | ECN */
8df8c56a
JR
496 tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
497 tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
498
499 if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
4710d806 500 (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
8df8c56a
JR
501 /* flow label can be compressed */
502 iphc0 |= LOWPAN_IPHC_FL_C;
503 if ((hdr->priority == 0) &&
4710d806 504 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
8df8c56a
JR
505 /* compress (elide) all */
506 iphc0 |= LOWPAN_IPHC_TC_C;
507 } else {
508 /* compress only the flow label */
84ca5e03
AA
509 *hc_ptr = tmp;
510 hc_ptr += 1;
8df8c56a
JR
511 }
512 } else {
513 /* Flow label cannot be compressed */
514 if ((hdr->priority == 0) &&
4710d806 515 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
8df8c56a
JR
516 /* compress only traffic class */
517 iphc0 |= LOWPAN_IPHC_TC_C;
84ca5e03
AA
518 *hc_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
519 memcpy(hc_ptr + 1, &hdr->flow_lbl[1], 2);
520 hc_ptr += 3;
8df8c56a
JR
521 } else {
522 /* compress nothing */
84ca5e03 523 memcpy(hc_ptr, hdr, 4);
8df8c56a 524 /* replace the top byte with new ECN | DSCP format */
84ca5e03
AA
525 *hc_ptr = tmp;
526 hc_ptr += 4;
8df8c56a
JR
527 }
528 }
529
530 /* NOTE: payload length is always compressed */
531
cc6ed268
AA
532 /* Check if we provide the nhc format for nexthdr and compression
533 * functionality. If not nexthdr is handled inline and not compressed.
534 */
535 ret = lowpan_nhc_check_compression(skb, hdr, &hc_ptr, &iphc0);
536 if (ret < 0)
537 return ret;
8df8c56a 538
4710d806 539 /* Hop limit
8df8c56a
JR
540 * if 1: compress, encoding is 01
541 * if 64: compress, encoding is 10
542 * if 255: compress, encoding is 11
543 * else do not compress
544 */
545 switch (hdr->hop_limit) {
546 case 1:
547 iphc0 |= LOWPAN_IPHC_TTL_1;
548 break;
549 case 64:
550 iphc0 |= LOWPAN_IPHC_TTL_64;
551 break;
552 case 255:
553 iphc0 |= LOWPAN_IPHC_TTL_255;
554 break;
555 default:
85c71240
AA
556 lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
557 sizeof(hdr->hop_limit));
8df8c56a
JR
558 }
559
556a5bfc 560 addr_type = ipv6_addr_type(&hdr->saddr);
8df8c56a 561 /* source address compression */
556a5bfc 562 if (addr_type == IPV6_ADDR_ANY) {
8df8c56a
JR
563 pr_debug("source address is unspecified, setting SAC\n");
564 iphc1 |= LOWPAN_IPHC_SAC;
8df8c56a 565 } else {
556a5bfc
AA
566 if (addr_type & IPV6_ADDR_LINKLOCAL) {
567 iphc1 |= lowpan_compress_addr_64(&hc_ptr,
568 LOWPAN_IPHC_SAM_BIT,
a6f77389 569 &hdr->saddr, saddr);
556a5bfc
AA
570 pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n",
571 &hdr->saddr, iphc1);
572 } else {
573 pr_debug("send the full source address\n");
574 lowpan_push_hc_data(&hc_ptr, hdr->saddr.s6_addr, 16);
575 }
8df8c56a
JR
576 }
577
556a5bfc 578 addr_type = ipv6_addr_type(&hdr->daddr);
8df8c56a 579 /* destination address compression */
556a5bfc 580 if (addr_type & IPV6_ADDR_MULTICAST) {
8df8c56a
JR
581 pr_debug("destination address is multicast: ");
582 iphc1 |= LOWPAN_IPHC_M;
583 if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
584 pr_debug("compressed to 1 octet\n");
585 iphc1 |= LOWPAN_IPHC_DAM_11;
586 /* use last byte */
85c71240
AA
587 lowpan_push_hc_data(&hc_ptr,
588 &hdr->daddr.s6_addr[15], 1);
8df8c56a
JR
589 } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
590 pr_debug("compressed to 4 octets\n");
591 iphc1 |= LOWPAN_IPHC_DAM_10;
592 /* second byte + the last three */
85c71240
AA
593 lowpan_push_hc_data(&hc_ptr,
594 &hdr->daddr.s6_addr[1], 1);
595 lowpan_push_hc_data(&hc_ptr,
596 &hdr->daddr.s6_addr[13], 3);
8df8c56a
JR
597 } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
598 pr_debug("compressed to 6 octets\n");
599 iphc1 |= LOWPAN_IPHC_DAM_01;
600 /* second byte + the last five */
85c71240
AA
601 lowpan_push_hc_data(&hc_ptr,
602 &hdr->daddr.s6_addr[1], 1);
603 lowpan_push_hc_data(&hc_ptr,
604 &hdr->daddr.s6_addr[11], 5);
8df8c56a
JR
605 } else {
606 pr_debug("using full address\n");
607 iphc1 |= LOWPAN_IPHC_DAM_00;
85c71240 608 lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
8df8c56a
JR
609 }
610 } else {
556a5bfc
AA
611 if (addr_type & IPV6_ADDR_LINKLOCAL) {
612 /* TODO: context lookup */
84ca5e03 613 iphc1 |= lowpan_compress_addr_64(&hc_ptr,
a6f77389 614 LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
8df8c56a 615 pr_debug("dest address unicast link-local %pI6c "
7fc4cfda 616 "iphc1 0x%02x\n", &hdr->daddr, iphc1);
8df8c56a
JR
617 } else {
618 pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
85c71240 619 lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
8df8c56a
JR
620 }
621 }
622
cc6ed268
AA
623 /* next header compression */
624 if (iphc0 & LOWPAN_IPHC_NH_C) {
625 ret = lowpan_nhc_do_compression(skb, hdr, &hc_ptr);
626 if (ret < 0)
627 return ret;
628 }
8df8c56a
JR
629
630 head[0] = iphc0;
631 head[1] = iphc1;
632
633 skb_pull(skb, sizeof(struct ipv6hdr));
634 skb_reset_transport_header(skb);
84ca5e03 635 memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
8df8c56a
JR
636 skb_reset_network_header(skb);
637
84ca5e03 638 pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
8df8c56a
JR
639
640 raw_dump_table(__func__, "raw skb data dump compressed",
4710d806 641 skb->data, skb->len);
8df8c56a
JR
642 return 0;
643}
644EXPORT_SYMBOL_GPL(lowpan_header_compress);
This page took 0.136449 seconds and 5 git commands to generate.