6lowpan: remove lowpan_is_addr_broadcast
[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
6350047e
AA
61/* Values of fields within the IPHC encoding first byte
62 * (C stands for compressed and I for inline)
63 */
64#define LOWPAN_IPHC_TF 0x18
65
66#define LOWPAN_IPHC_FL_C 0x10
67#define LOWPAN_IPHC_TC_C 0x08
68#define LOWPAN_IPHC_NH_C 0x04
69#define LOWPAN_IPHC_TTL_1 0x01
70#define LOWPAN_IPHC_TTL_64 0x02
71#define LOWPAN_IPHC_TTL_255 0x03
72#define LOWPAN_IPHC_TTL_I 0x00
73
74/* Values of fields within the IPHC encoding second byte */
75#define LOWPAN_IPHC_CID 0x80
76
77#define LOWPAN_IPHC_ADDR_00 0x00
78#define LOWPAN_IPHC_ADDR_01 0x01
79#define LOWPAN_IPHC_ADDR_02 0x02
80#define LOWPAN_IPHC_ADDR_03 0x03
81
82#define LOWPAN_IPHC_SAC 0x40
83#define LOWPAN_IPHC_SAM 0x30
84
85#define LOWPAN_IPHC_SAM_BIT 4
86
87#define LOWPAN_IPHC_M 0x08
88#define LOWPAN_IPHC_DAC 0x04
89#define LOWPAN_IPHC_DAM_00 0x00
90#define LOWPAN_IPHC_DAM_01 0x01
91#define LOWPAN_IPHC_DAM_10 0x02
92#define LOWPAN_IPHC_DAM_11 0x03
93
94#define LOWPAN_IPHC_DAM_BIT 0
95
96/* ipv6 address based on mac
97 * second bit-flip (Universe/Local) is done according RFC2464
98 */
99#define is_addr_mac_addr_based(a, m) \
100 ((((a)->s6_addr[8]) == (((m)[0]) ^ 0x02)) && \
101 (((a)->s6_addr[9]) == (m)[1]) && \
102 (((a)->s6_addr[10]) == (m)[2]) && \
103 (((a)->s6_addr[11]) == (m)[3]) && \
104 (((a)->s6_addr[12]) == (m)[4]) && \
105 (((a)->s6_addr[13]) == (m)[5]) && \
106 (((a)->s6_addr[14]) == (m)[6]) && \
107 (((a)->s6_addr[15]) == (m)[7]))
108
109/* check whether we can compress the IID to 16 bits,
110 * it's possible for unicast addresses with first 49 bits are zero only.
111 */
112#define lowpan_is_iid_16_bit_compressable(a) \
113 ((((a)->s6_addr16[4]) == 0) && \
114 (((a)->s6_addr[10]) == 0) && \
115 (((a)->s6_addr[11]) == 0xff) && \
116 (((a)->s6_addr[12]) == 0xfe) && \
117 (((a)->s6_addr[13]) == 0))
118
119/* check whether the 112-bit gid of the multicast address is mappable to: */
120
121/* 48 bits, FFXX::00XX:XXXX:XXXX */
122#define lowpan_is_mcast_addr_compressable48(a) \
123 ((((a)->s6_addr16[1]) == 0) && \
124 (((a)->s6_addr16[2]) == 0) && \
125 (((a)->s6_addr16[3]) == 0) && \
126 (((a)->s6_addr16[4]) == 0) && \
127 (((a)->s6_addr[10]) == 0))
128
129/* 32 bits, FFXX::00XX:XXXX */
130#define lowpan_is_mcast_addr_compressable32(a) \
131 ((((a)->s6_addr16[1]) == 0) && \
132 (((a)->s6_addr16[2]) == 0) && \
133 (((a)->s6_addr16[3]) == 0) && \
134 (((a)->s6_addr16[4]) == 0) && \
135 (((a)->s6_addr16[5]) == 0) && \
136 (((a)->s6_addr[12]) == 0))
137
138/* 8 bits, FF02::00XX */
139#define lowpan_is_mcast_addr_compressable8(a) \
140 ((((a)->s6_addr[1]) == 2) && \
141 (((a)->s6_addr16[1]) == 0) && \
142 (((a)->s6_addr16[2]) == 0) && \
143 (((a)->s6_addr16[3]) == 0) && \
144 (((a)->s6_addr16[4]) == 0) && \
145 (((a)->s6_addr16[5]) == 0) && \
146 (((a)->s6_addr16[6]) == 0) && \
147 (((a)->s6_addr[14]) == 0))
148
8911d774
AA
149static inline void iphc_uncompress_eui64_lladdr(struct in6_addr *ipaddr,
150 const void *lladdr)
151{
152 /* fe:80::XXXX:XXXX:XXXX:XXXX
153 * \_________________/
154 * hwaddr
155 */
156 ipaddr->s6_addr[0] = 0xFE;
157 ipaddr->s6_addr[1] = 0x80;
158 memcpy(&ipaddr->s6_addr[8], lladdr, EUI64_ADDR_LEN);
159 /* second bit-flip (Universe/Local)
160 * is done according RFC2464
161 */
162 ipaddr->s6_addr[8] ^= 0x02;
163}
164
165static inline void iphc_uncompress_802154_lladdr(struct in6_addr *ipaddr,
166 const void *lladdr)
167{
168 const struct ieee802154_addr *addr = lladdr;
169 u8 eui64[EUI64_ADDR_LEN] = { };
170
171 switch (addr->mode) {
172 case IEEE802154_ADDR_LONG:
173 ieee802154_le64_to_be64(eui64, &addr->extended_addr);
174 iphc_uncompress_eui64_lladdr(ipaddr, eui64);
175 break;
176 case IEEE802154_ADDR_SHORT:
177 /* fe:80::ff:fe00:XXXX
178 * \__/
179 * short_addr
180 *
181 * Universe/Local bit is zero.
182 */
183 ipaddr->s6_addr[0] = 0xFE;
184 ipaddr->s6_addr[1] = 0x80;
185 ipaddr->s6_addr[11] = 0xFF;
186 ipaddr->s6_addr[12] = 0xFE;
187 ieee802154_le16_to_be16(&ipaddr->s6_addr16[7],
188 &addr->short_addr);
189 break;
190 default:
191 /* should never handled and filtered by 802154 6lowpan */
192 WARN_ON_ONCE(1);
193 break;
194 }
195}
196
4710d806 197/* Uncompress address function for source and
8df8c56a
JR
198 * destination address(non-multicast).
199 *
200 * address_mode is sam value or dam value.
201 */
8911d774
AA
202static int uncompress_addr(struct sk_buff *skb, const struct net_device *dev,
203 struct in6_addr *ipaddr, u8 address_mode,
204 const void *lladdr)
8df8c56a
JR
205{
206 bool fail;
207
208 switch (address_mode) {
209 case LOWPAN_IPHC_ADDR_00:
210 /* for global link addresses */
211 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
212 break;
213 case LOWPAN_IPHC_ADDR_01:
214 /* fe:80::XXXX:XXXX:XXXX:XXXX */
215 ipaddr->s6_addr[0] = 0xFE;
216 ipaddr->s6_addr[1] = 0x80;
217 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8);
218 break;
219 case LOWPAN_IPHC_ADDR_02:
220 /* fe:80::ff:fe00:XXXX */
221 ipaddr->s6_addr[0] = 0xFE;
222 ipaddr->s6_addr[1] = 0x80;
223 ipaddr->s6_addr[11] = 0xFF;
224 ipaddr->s6_addr[12] = 0xFE;
225 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2);
226 break;
227 case LOWPAN_IPHC_ADDR_03:
228 fail = false;
8911d774
AA
229 switch (lowpan_priv(dev)->lltype) {
230 case LOWPAN_LLTYPE_IEEE802154:
231 iphc_uncompress_802154_lladdr(ipaddr, lladdr);
8df8c56a
JR
232 break;
233 default:
8911d774
AA
234 iphc_uncompress_eui64_lladdr(ipaddr, lladdr);
235 break;
8df8c56a
JR
236 }
237 break;
238 default:
239 pr_debug("Invalid address mode value: 0x%x\n", address_mode);
240 return -EINVAL;
241 }
242
243 if (fail) {
244 pr_debug("Failed to fetch skb data\n");
245 return -EIO;
246 }
247
248 raw_dump_inline(NULL, "Reconstructed ipv6 addr is",
249 ipaddr->s6_addr, 16);
250
251 return 0;
252}
253
4710d806 254/* Uncompress address function for source context
8df8c56a
JR
255 * based address(non-multicast).
256 */
257static int uncompress_context_based_src_addr(struct sk_buff *skb,
4710d806
VB
258 struct in6_addr *ipaddr,
259 const u8 sam)
8df8c56a
JR
260{
261 switch (sam) {
262 case LOWPAN_IPHC_ADDR_00:
263 /* unspec address ::
264 * Do nothing, address is already ::
265 */
266 break;
267 case LOWPAN_IPHC_ADDR_01:
268 /* TODO */
269 case LOWPAN_IPHC_ADDR_02:
270 /* TODO */
271 case LOWPAN_IPHC_ADDR_03:
272 /* TODO */
273 netdev_warn(skb->dev, "SAM value 0x%x not supported\n", sam);
274 return -EINVAL;
275 default:
276 pr_debug("Invalid sam value: 0x%x\n", sam);
277 return -EINVAL;
278 }
279
280 raw_dump_inline(NULL,
281 "Reconstructed context based ipv6 src addr is",
282 ipaddr->s6_addr, 16);
283
284 return 0;
285}
286
8df8c56a
JR
287/* Uncompress function for multicast destination address,
288 * when M bit is set.
289 */
7fc4cfda
MH
290static int lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
291 struct in6_addr *ipaddr,
292 const u8 dam)
8df8c56a
JR
293{
294 bool fail;
295
296 switch (dam) {
297 case LOWPAN_IPHC_DAM_00:
298 /* 00: 128 bits. The full address
299 * is carried in-line.
300 */
301 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
302 break;
303 case LOWPAN_IPHC_DAM_01:
304 /* 01: 48 bits. The address takes
305 * the form ffXX::00XX:XXXX:XXXX.
306 */
307 ipaddr->s6_addr[0] = 0xFF;
308 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
309 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
310 break;
311 case LOWPAN_IPHC_DAM_10:
312 /* 10: 32 bits. The address takes
313 * the form ffXX::00XX:XXXX.
314 */
315 ipaddr->s6_addr[0] = 0xFF;
316 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
317 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
318 break;
319 case LOWPAN_IPHC_DAM_11:
320 /* 11: 8 bits. The address takes
321 * the form ff02::00XX.
322 */
323 ipaddr->s6_addr[0] = 0xFF;
324 ipaddr->s6_addr[1] = 0x02;
325 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
326 break;
327 default:
328 pr_debug("DAM value has a wrong value: 0x%x\n", dam);
329 return -EINVAL;
330 }
331
332 if (fail) {
333 pr_debug("Failed to fetch skb data\n");
334 return -EIO;
335 }
336
337 raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is",
7fc4cfda 338 ipaddr->s6_addr, 16);
8df8c56a
JR
339
340 return 0;
341}
342
8df8c56a
JR
343/* TTL uncompression values */
344static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 };
345
8911d774
AA
346int lowpan_header_decompress(struct sk_buff *skb, const struct net_device *dev,
347 const void *daddr, const void *saddr)
8df8c56a
JR
348{
349 struct ipv6hdr hdr = {};
8911d774 350 u8 iphc0, iphc1, tmp, num_context = 0;
8df8c56a
JR
351 int err;
352
353 raw_dump_table(__func__, "raw skb data dump uncompressed",
4710d806 354 skb->data, skb->len);
8df8c56a 355
478208e3
AA
356 if (lowpan_fetch_skb(skb, &iphc0, sizeof(iphc0)) ||
357 lowpan_fetch_skb(skb, &iphc1, sizeof(iphc1)))
8911d774
AA
358 return -EINVAL;
359
8df8c56a
JR
360 /* another if the CID flag is set */
361 if (iphc1 & LOWPAN_IPHC_CID) {
362 pr_debug("CID flag is set, increase header with one\n");
4ebc960f 363 if (lowpan_fetch_skb(skb, &num_context, sizeof(num_context)))
56b2c3ee 364 return -EINVAL;
8df8c56a
JR
365 }
366
367 hdr.version = 6;
368
369 /* Traffic Class and Flow Label */
370 switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
4710d806 371 /* Traffic Class and FLow Label carried in-line
8df8c56a
JR
372 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
373 */
374 case 0: /* 00b */
4ebc960f 375 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
56b2c3ee 376 return -EINVAL;
8df8c56a
JR
377
378 memcpy(&hdr.flow_lbl, &skb->data[0], 3);
379 skb_pull(skb, 3);
380 hdr.priority = ((tmp >> 2) & 0x0f);
381 hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
382 (hdr.flow_lbl[0] & 0x0f);
383 break;
4710d806 384 /* Traffic class carried in-line
8df8c56a
JR
385 * ECN + DSCP (1 byte), Flow Label is elided
386 */
387 case 2: /* 10b */
4ebc960f 388 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
56b2c3ee 389 return -EINVAL;
8df8c56a
JR
390
391 hdr.priority = ((tmp >> 2) & 0x0f);
392 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
393 break;
4710d806 394 /* Flow Label carried in-line
8df8c56a
JR
395 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
396 */
397 case 1: /* 01b */
4ebc960f 398 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
56b2c3ee 399 return -EINVAL;
8df8c56a 400
77e867b5 401 hdr.flow_lbl[0] = (tmp & 0x0F) | ((tmp >> 2) & 0x30);
8df8c56a
JR
402 memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
403 skb_pull(skb, 2);
404 break;
405 /* Traffic Class and Flow Label are elided */
406 case 3: /* 11b */
407 break;
408 default:
409 break;
410 }
411
412 /* Next Header */
413 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
414 /* Next header is carried inline */
4ebc960f 415 if (lowpan_fetch_skb(skb, &hdr.nexthdr, sizeof(hdr.nexthdr)))
56b2c3ee 416 return -EINVAL;
8df8c56a
JR
417
418 pr_debug("NH flag is set, next header carried inline: %02x\n",
419 hdr.nexthdr);
420 }
421
422 /* Hop Limit */
4710d806 423 if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I) {
8df8c56a 424 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
4710d806 425 } else {
4ebc960f
AA
426 if (lowpan_fetch_skb(skb, &hdr.hop_limit,
427 sizeof(hdr.hop_limit)))
56b2c3ee 428 return -EINVAL;
8df8c56a
JR
429 }
430
431 /* Extract SAM to the tmp variable */
432 tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
433
434 if (iphc1 & LOWPAN_IPHC_SAC) {
435 /* Source address context based uncompression */
436 pr_debug("SAC bit is set. Handle context based source address.\n");
7fc4cfda 437 err = uncompress_context_based_src_addr(skb, &hdr.saddr, tmp);
8df8c56a
JR
438 } else {
439 /* Source address uncompression */
440 pr_debug("source address stateless compression\n");
8911d774 441 err = uncompress_addr(skb, dev, &hdr.saddr, tmp, saddr);
8df8c56a
JR
442 }
443
444 /* Check on error of previous branch */
445 if (err)
56b2c3ee 446 return -EINVAL;
8df8c56a
JR
447
448 /* Extract DAM to the tmp variable */
449 tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
450
451 /* check for Multicast Compression */
452 if (iphc1 & LOWPAN_IPHC_M) {
453 if (iphc1 & LOWPAN_IPHC_DAC) {
454 pr_debug("dest: context-based mcast compression\n");
455 /* TODO: implement this */
456 } else {
7fc4cfda
MH
457 err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr,
458 tmp);
459
8df8c56a 460 if (err)
56b2c3ee 461 return -EINVAL;
8df8c56a
JR
462 }
463 } else {
8911d774 464 err = uncompress_addr(skb, dev, &hdr.daddr, tmp, daddr);
8df8c56a 465 pr_debug("dest: stateless compression mode %d dest %pI6c\n",
4710d806 466 tmp, &hdr.daddr);
8df8c56a 467 if (err)
56b2c3ee 468 return -EINVAL;
8df8c56a
JR
469 }
470
cc6ed268 471 /* Next header data uncompression */
8df8c56a 472 if (iphc0 & LOWPAN_IPHC_NH_C) {
cc6ed268
AA
473 err = lowpan_nhc_do_uncompression(skb, dev, &hdr);
474 if (err < 0)
11e3ff70 475 return err;
11e3ff70
MT
476 } else {
477 err = skb_cow(skb, sizeof(hdr));
56b2c3ee 478 if (unlikely(err))
11e3ff70 479 return err;
8df8c56a
JR
480 }
481
72a5e6bb
AA
482 switch (lowpan_priv(dev)->lltype) {
483 case LOWPAN_LLTYPE_IEEE802154:
484 if (lowpan_802154_cb(skb)->d_size)
485 hdr.payload_len = htons(lowpan_802154_cb(skb)->d_size -
486 sizeof(struct ipv6hdr));
487 else
488 hdr.payload_len = htons(skb->len);
489 break;
490 default:
491 hdr.payload_len = htons(skb->len);
492 break;
493 }
8df8c56a
JR
494
495 pr_debug("skb headroom size = %d, data length = %d\n",
496 skb_headroom(skb), skb->len);
497
498 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
499 "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
500 hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
501 hdr.hop_limit, &hdr.daddr);
502
f8b36176
MT
503 skb_push(skb, sizeof(hdr));
504 skb_reset_network_header(skb);
505 skb_copy_to_linear_data(skb, &hdr, sizeof(hdr));
8df8c56a 506
f8b36176 507 raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
8df8c56a 508
f8b36176 509 return 0;
8df8c56a 510}
01141234 511EXPORT_SYMBOL_GPL(lowpan_header_decompress);
8df8c56a 512
84ca5e03 513static u8 lowpan_compress_addr_64(u8 **hc_ptr, u8 shift,
4710d806
VB
514 const struct in6_addr *ipaddr,
515 const unsigned char *lladdr)
8df8c56a
JR
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 */
85c71240 524 lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
8df8c56a
JR
525 val = 2; /* 16-bits */
526 raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
84ca5e03 527 *hc_ptr - 2, 2);
8df8c56a
JR
528 } else {
529 /* do not compress IID => xxxx::IID */
85c71240 530 lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
8df8c56a
JR
531 val = 1; /* 64-bits */
532 raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
84ca5e03 533 *hc_ptr - 8, 8);
8df8c56a
JR
534 }
535
536 return rol8(val, shift);
537}
538
a6f77389
AA
539int lowpan_header_compress(struct sk_buff *skb, const struct net_device *dev,
540 const void *daddr, const void *saddr)
8df8c56a 541{
84ca5e03 542 u8 tmp, iphc0, iphc1, *hc_ptr;
8df8c56a 543 struct ipv6hdr *hdr;
bf513fd6 544 u8 head[LOWPAN_IPHC_MAX_HC_BUF_LEN] = {};
cc6ed268 545 int ret, addr_type;
8df8c56a 546
a6f77389 547 if (skb->protocol != htons(ETH_P_IPV6))
8df8c56a
JR
548 return -EINVAL;
549
550 hdr = ipv6_hdr(skb);
84ca5e03 551 hc_ptr = head + 2;
8df8c56a
JR
552
553 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
554 "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
4710d806
VB
555 hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
556 hdr->hop_limit, &hdr->daddr);
8df8c56a
JR
557
558 raw_dump_table(__func__, "raw skb network header dump",
4710d806 559 skb_network_header(skb), sizeof(struct ipv6hdr));
8df8c56a 560
4710d806 561 /* As we copy some bit-length fields, in the IPHC encoding bytes,
8df8c56a
JR
562 * we sometimes use |=
563 * If the field is 0, and the current bit value in memory is 1,
564 * this does not work. We therefore reset the IPHC encoding here
565 */
566 iphc0 = LOWPAN_DISPATCH_IPHC;
567 iphc1 = 0;
568
569 /* TODO: context lookup */
570
a6f77389
AA
571 raw_dump_inline(__func__, "saddr", saddr, EUI64_ADDR_LEN);
572 raw_dump_inline(__func__, "daddr", daddr, EUI64_ADDR_LEN);
8df8c56a 573
7fc4cfda 574 raw_dump_table(__func__, "sending raw skb network uncompressed packet",
4710d806 575 skb->data, skb->len);
8df8c56a 576
4710d806 577 /* Traffic class, flow label
8df8c56a
JR
578 * If flow label is 0, compress it. If traffic class is 0, compress it
579 * We have to process both in the same time as the offset of traffic
580 * class depends on the presence of version and flow label
581 */
582
84ca5e03 583 /* hc format of TC is ECN | DSCP , original one is DSCP | ECN */
8df8c56a
JR
584 tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
585 tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
586
587 if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
4710d806 588 (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
8df8c56a
JR
589 /* flow label can be compressed */
590 iphc0 |= LOWPAN_IPHC_FL_C;
591 if ((hdr->priority == 0) &&
4710d806 592 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
8df8c56a
JR
593 /* compress (elide) all */
594 iphc0 |= LOWPAN_IPHC_TC_C;
595 } else {
596 /* compress only the flow label */
84ca5e03
AA
597 *hc_ptr = tmp;
598 hc_ptr += 1;
8df8c56a
JR
599 }
600 } else {
601 /* Flow label cannot be compressed */
602 if ((hdr->priority == 0) &&
4710d806 603 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
8df8c56a
JR
604 /* compress only traffic class */
605 iphc0 |= LOWPAN_IPHC_TC_C;
84ca5e03
AA
606 *hc_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
607 memcpy(hc_ptr + 1, &hdr->flow_lbl[1], 2);
608 hc_ptr += 3;
8df8c56a
JR
609 } else {
610 /* compress nothing */
84ca5e03 611 memcpy(hc_ptr, hdr, 4);
8df8c56a 612 /* replace the top byte with new ECN | DSCP format */
84ca5e03
AA
613 *hc_ptr = tmp;
614 hc_ptr += 4;
8df8c56a
JR
615 }
616 }
617
618 /* NOTE: payload length is always compressed */
619
cc6ed268
AA
620 /* Check if we provide the nhc format for nexthdr and compression
621 * functionality. If not nexthdr is handled inline and not compressed.
622 */
607b0bd3
AA
623 ret = lowpan_nhc_check_compression(skb, hdr, &hc_ptr);
624 if (ret == -ENOENT)
625 lowpan_push_hc_data(&hc_ptr, &hdr->nexthdr,
626 sizeof(hdr->nexthdr));
627 else
628 iphc0 |= LOWPAN_IPHC_NH_C;
8df8c56a 629
4710d806 630 /* Hop limit
8df8c56a
JR
631 * if 1: compress, encoding is 01
632 * if 64: compress, encoding is 10
633 * if 255: compress, encoding is 11
634 * else do not compress
635 */
636 switch (hdr->hop_limit) {
637 case 1:
638 iphc0 |= LOWPAN_IPHC_TTL_1;
639 break;
640 case 64:
641 iphc0 |= LOWPAN_IPHC_TTL_64;
642 break;
643 case 255:
644 iphc0 |= LOWPAN_IPHC_TTL_255;
645 break;
646 default:
85c71240
AA
647 lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
648 sizeof(hdr->hop_limit));
8df8c56a
JR
649 }
650
556a5bfc 651 addr_type = ipv6_addr_type(&hdr->saddr);
8df8c56a 652 /* source address compression */
556a5bfc 653 if (addr_type == IPV6_ADDR_ANY) {
8df8c56a
JR
654 pr_debug("source address is unspecified, setting SAC\n");
655 iphc1 |= LOWPAN_IPHC_SAC;
8df8c56a 656 } else {
556a5bfc
AA
657 if (addr_type & IPV6_ADDR_LINKLOCAL) {
658 iphc1 |= lowpan_compress_addr_64(&hc_ptr,
659 LOWPAN_IPHC_SAM_BIT,
a6f77389 660 &hdr->saddr, saddr);
556a5bfc
AA
661 pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n",
662 &hdr->saddr, iphc1);
663 } else {
664 pr_debug("send the full source address\n");
665 lowpan_push_hc_data(&hc_ptr, hdr->saddr.s6_addr, 16);
666 }
8df8c56a
JR
667 }
668
556a5bfc 669 addr_type = ipv6_addr_type(&hdr->daddr);
8df8c56a 670 /* destination address compression */
556a5bfc 671 if (addr_type & IPV6_ADDR_MULTICAST) {
8df8c56a
JR
672 pr_debug("destination address is multicast: ");
673 iphc1 |= LOWPAN_IPHC_M;
674 if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
675 pr_debug("compressed to 1 octet\n");
676 iphc1 |= LOWPAN_IPHC_DAM_11;
677 /* use last byte */
85c71240
AA
678 lowpan_push_hc_data(&hc_ptr,
679 &hdr->daddr.s6_addr[15], 1);
8df8c56a
JR
680 } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
681 pr_debug("compressed to 4 octets\n");
682 iphc1 |= LOWPAN_IPHC_DAM_10;
683 /* second byte + the last three */
85c71240
AA
684 lowpan_push_hc_data(&hc_ptr,
685 &hdr->daddr.s6_addr[1], 1);
686 lowpan_push_hc_data(&hc_ptr,
687 &hdr->daddr.s6_addr[13], 3);
8df8c56a
JR
688 } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
689 pr_debug("compressed to 6 octets\n");
690 iphc1 |= LOWPAN_IPHC_DAM_01;
691 /* second byte + the last five */
85c71240
AA
692 lowpan_push_hc_data(&hc_ptr,
693 &hdr->daddr.s6_addr[1], 1);
694 lowpan_push_hc_data(&hc_ptr,
695 &hdr->daddr.s6_addr[11], 5);
8df8c56a
JR
696 } else {
697 pr_debug("using full address\n");
698 iphc1 |= LOWPAN_IPHC_DAM_00;
85c71240 699 lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
8df8c56a
JR
700 }
701 } else {
556a5bfc
AA
702 if (addr_type & IPV6_ADDR_LINKLOCAL) {
703 /* TODO: context lookup */
84ca5e03 704 iphc1 |= lowpan_compress_addr_64(&hc_ptr,
a6f77389 705 LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
8df8c56a 706 pr_debug("dest address unicast link-local %pI6c "
7fc4cfda 707 "iphc1 0x%02x\n", &hdr->daddr, iphc1);
8df8c56a
JR
708 } else {
709 pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
85c71240 710 lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
8df8c56a
JR
711 }
712 }
713
cc6ed268
AA
714 /* next header compression */
715 if (iphc0 & LOWPAN_IPHC_NH_C) {
716 ret = lowpan_nhc_do_compression(skb, hdr, &hc_ptr);
717 if (ret < 0)
718 return ret;
719 }
8df8c56a
JR
720
721 head[0] = iphc0;
722 head[1] = iphc1;
723
724 skb_pull(skb, sizeof(struct ipv6hdr));
725 skb_reset_transport_header(skb);
84ca5e03 726 memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
8df8c56a
JR
727 skb_reset_network_header(skb);
728
84ca5e03 729 pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
8df8c56a
JR
730
731 raw_dump_table(__func__, "raw skb data dump compressed",
4710d806 732 skb->data, skb->len);
8df8c56a
JR
733 return 0;
734}
735EXPORT_SYMBOL_GPL(lowpan_header_compress);
This page took 0.143449 seconds and 5 git commands to generate.