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