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