blob: 94a375c04f21cd5a9b525f7dffb4a14657deea89 [file] [log] [blame]
Jukka Rissanen8df8c562013-12-11 17:05:34 +02001/*
2 * Copyright 2011, Siemens AG
3 * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
4 */
5
Varka Bhadram4710d802014-07-02 09:01:09 +05306/* Based on patches from Jon Smirl <jonsmirl@gmail.com>
Jukka Rissanen8df8c562013-12-11 17:05:34 +02007 * 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 *
Jukka Rissanen8df8c562013-12-11 17:05:34 +020018 */
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>
Yann Droneaud5ae5e992014-01-22 20:25:24 +010051#include <linux/module.h>
Jukka Rissanen8df8c562013-12-11 17:05:34 +020052#include <linux/netdevice.h>
Alexander Aringcefc8c82014-03-05 14:29:05 +010053#include <net/6lowpan.h>
Jukka Rissanen8df8c562013-12-11 17:05:34 +020054#include <net/ipv6.h>
55#include <net/af_ieee802154.h>
56
Alexander Aringcc6ed262015-01-09 16:42:58 +010057#include "nhc.h"
58
Varka Bhadram4710d802014-07-02 09:01:09 +053059/* Uncompress address function for source and
Jukka Rissanen8df8c562013-12-11 17:05:34 +020060 * destination address(non-multicast).
61 *
62 * address_mode is sam value or dam value.
63 */
64static int uncompress_addr(struct sk_buff *skb,
Varka Bhadram4710d802014-07-02 09:01:09 +053065 struct in6_addr *ipaddr, const u8 address_mode,
66 const u8 *lladdr, const u8 addr_type,
67 const u8 addr_len)
Jukka Rissanen8df8c562013-12-11 17:05:34 +020068{
69 bool fail;
70
71 switch (address_mode) {
72 case LOWPAN_IPHC_ADDR_00:
73 /* for global link addresses */
74 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
75 break;
76 case LOWPAN_IPHC_ADDR_01:
77 /* fe:80::XXXX:XXXX:XXXX:XXXX */
78 ipaddr->s6_addr[0] = 0xFE;
79 ipaddr->s6_addr[1] = 0x80;
80 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8);
81 break;
82 case LOWPAN_IPHC_ADDR_02:
83 /* fe:80::ff:fe00:XXXX */
84 ipaddr->s6_addr[0] = 0xFE;
85 ipaddr->s6_addr[1] = 0x80;
86 ipaddr->s6_addr[11] = 0xFF;
87 ipaddr->s6_addr[12] = 0xFE;
88 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2);
89 break;
90 case LOWPAN_IPHC_ADDR_03:
91 fail = false;
92 switch (addr_type) {
93 case IEEE802154_ADDR_LONG:
94 /* fe:80::XXXX:XXXX:XXXX:XXXX
95 * \_________________/
96 * hwaddr
97 */
98 ipaddr->s6_addr[0] = 0xFE;
99 ipaddr->s6_addr[1] = 0x80;
100 memcpy(&ipaddr->s6_addr[8], lladdr, addr_len);
101 /* second bit-flip (Universe/Local)
102 * is done according RFC2464
103 */
104 ipaddr->s6_addr[8] ^= 0x02;
105 break;
106 case IEEE802154_ADDR_SHORT:
107 /* fe:80::ff:fe00:XXXX
108 * \__/
109 * short_addr
110 *
111 * Universe/Local bit is zero.
112 */
113 ipaddr->s6_addr[0] = 0xFE;
114 ipaddr->s6_addr[1] = 0x80;
115 ipaddr->s6_addr[11] = 0xFF;
116 ipaddr->s6_addr[12] = 0xFE;
117 ipaddr->s6_addr16[7] = htons(*((u16 *)lladdr));
118 break;
119 default:
120 pr_debug("Invalid addr_type set\n");
121 return -EINVAL;
122 }
123 break;
124 default:
125 pr_debug("Invalid address mode value: 0x%x\n", address_mode);
126 return -EINVAL;
127 }
128
129 if (fail) {
130 pr_debug("Failed to fetch skb data\n");
131 return -EIO;
132 }
133
134 raw_dump_inline(NULL, "Reconstructed ipv6 addr is",
135 ipaddr->s6_addr, 16);
136
137 return 0;
138}
139
Varka Bhadram4710d802014-07-02 09:01:09 +0530140/* Uncompress address function for source context
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200141 * based address(non-multicast).
142 */
143static int uncompress_context_based_src_addr(struct sk_buff *skb,
Varka Bhadram4710d802014-07-02 09:01:09 +0530144 struct in6_addr *ipaddr,
145 const u8 sam)
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200146{
147 switch (sam) {
148 case LOWPAN_IPHC_ADDR_00:
149 /* unspec address ::
150 * Do nothing, address is already ::
151 */
152 break;
153 case LOWPAN_IPHC_ADDR_01:
154 /* TODO */
155 case LOWPAN_IPHC_ADDR_02:
156 /* TODO */
157 case LOWPAN_IPHC_ADDR_03:
158 /* TODO */
159 netdev_warn(skb->dev, "SAM value 0x%x not supported\n", sam);
160 return -EINVAL;
161 default:
162 pr_debug("Invalid sam value: 0x%x\n", sam);
163 return -EINVAL;
164 }
165
166 raw_dump_inline(NULL,
167 "Reconstructed context based ipv6 src addr is",
168 ipaddr->s6_addr, 16);
169
170 return 0;
171}
172
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200173/* Uncompress function for multicast destination address,
174 * when M bit is set.
175 */
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200176static int lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
177 struct in6_addr *ipaddr,
178 const u8 dam)
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200179{
180 bool fail;
181
182 switch (dam) {
183 case LOWPAN_IPHC_DAM_00:
184 /* 00: 128 bits. The full address
185 * is carried in-line.
186 */
187 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
188 break;
189 case LOWPAN_IPHC_DAM_01:
190 /* 01: 48 bits. The address takes
191 * the form ffXX::00XX:XXXX:XXXX.
192 */
193 ipaddr->s6_addr[0] = 0xFF;
194 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
195 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
196 break;
197 case LOWPAN_IPHC_DAM_10:
198 /* 10: 32 bits. The address takes
199 * the form ffXX::00XX:XXXX.
200 */
201 ipaddr->s6_addr[0] = 0xFF;
202 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
203 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
204 break;
205 case LOWPAN_IPHC_DAM_11:
206 /* 11: 8 bits. The address takes
207 * the form ff02::00XX.
208 */
209 ipaddr->s6_addr[0] = 0xFF;
210 ipaddr->s6_addr[1] = 0x02;
211 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
212 break;
213 default:
214 pr_debug("DAM value has a wrong value: 0x%x\n", dam);
215 return -EINVAL;
216 }
217
218 if (fail) {
219 pr_debug("Failed to fetch skb data\n");
220 return -EIO;
221 }
222
223 raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is",
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200224 ipaddr->s6_addr, 16);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200225
226 return 0;
227}
228
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200229/* TTL uncompression values */
230static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 };
231
Martin Townsend01141232014-10-23 15:40:56 +0100232int
233lowpan_header_decompress(struct sk_buff *skb, struct net_device *dev,
234 const u8 *saddr, const u8 saddr_type,
235 const u8 saddr_len, const u8 *daddr,
236 const u8 daddr_type, const u8 daddr_len,
237 u8 iphc0, u8 iphc1)
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200238{
239 struct ipv6hdr hdr = {};
240 u8 tmp, num_context = 0;
241 int err;
242
243 raw_dump_table(__func__, "raw skb data dump uncompressed",
Varka Bhadram4710d802014-07-02 09:01:09 +0530244 skb->data, skb->len);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200245
246 /* another if the CID flag is set */
247 if (iphc1 & LOWPAN_IPHC_CID) {
248 pr_debug("CID flag is set, increase header with one\n");
Alexander Aring4ebc9602014-07-29 23:47:00 +0200249 if (lowpan_fetch_skb(skb, &num_context, sizeof(num_context)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000250 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200251 }
252
253 hdr.version = 6;
254
255 /* Traffic Class and Flow Label */
256 switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
Varka Bhadram4710d802014-07-02 09:01:09 +0530257 /* Traffic Class and FLow Label carried in-line
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200258 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
259 */
260 case 0: /* 00b */
Alexander Aring4ebc9602014-07-29 23:47:00 +0200261 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000262 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200263
264 memcpy(&hdr.flow_lbl, &skb->data[0], 3);
265 skb_pull(skb, 3);
266 hdr.priority = ((tmp >> 2) & 0x0f);
267 hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
268 (hdr.flow_lbl[0] & 0x0f);
269 break;
Varka Bhadram4710d802014-07-02 09:01:09 +0530270 /* Traffic class carried in-line
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200271 * ECN + DSCP (1 byte), Flow Label is elided
272 */
273 case 2: /* 10b */
Alexander Aring4ebc9602014-07-29 23:47:00 +0200274 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000275 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200276
277 hdr.priority = ((tmp >> 2) & 0x0f);
278 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
279 break;
Varka Bhadram4710d802014-07-02 09:01:09 +0530280 /* Flow Label carried in-line
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200281 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
282 */
283 case 1: /* 01b */
Alexander Aring4ebc9602014-07-29 23:47:00 +0200284 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000285 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200286
287 hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
288 memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
289 skb_pull(skb, 2);
290 break;
291 /* Traffic Class and Flow Label are elided */
292 case 3: /* 11b */
293 break;
294 default:
295 break;
296 }
297
298 /* Next Header */
299 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
300 /* Next header is carried inline */
Alexander Aring4ebc9602014-07-29 23:47:00 +0200301 if (lowpan_fetch_skb(skb, &hdr.nexthdr, sizeof(hdr.nexthdr)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000302 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200303
304 pr_debug("NH flag is set, next header carried inline: %02x\n",
305 hdr.nexthdr);
306 }
307
308 /* Hop Limit */
Varka Bhadram4710d802014-07-02 09:01:09 +0530309 if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200310 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
Varka Bhadram4710d802014-07-02 09:01:09 +0530311 } else {
Alexander Aring4ebc9602014-07-29 23:47:00 +0200312 if (lowpan_fetch_skb(skb, &hdr.hop_limit,
313 sizeof(hdr.hop_limit)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000314 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200315 }
316
317 /* Extract SAM to the tmp variable */
318 tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
319
320 if (iphc1 & LOWPAN_IPHC_SAC) {
321 /* Source address context based uncompression */
322 pr_debug("SAC bit is set. Handle context based source address.\n");
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200323 err = uncompress_context_based_src_addr(skb, &hdr.saddr, tmp);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200324 } else {
325 /* Source address uncompression */
326 pr_debug("source address stateless compression\n");
327 err = uncompress_addr(skb, &hdr.saddr, tmp, saddr,
Varka Bhadram4710d802014-07-02 09:01:09 +0530328 saddr_type, saddr_len);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200329 }
330
331 /* Check on error of previous branch */
332 if (err)
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000333 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200334
335 /* Extract DAM to the tmp variable */
336 tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
337
338 /* check for Multicast Compression */
339 if (iphc1 & LOWPAN_IPHC_M) {
340 if (iphc1 & LOWPAN_IPHC_DAC) {
341 pr_debug("dest: context-based mcast compression\n");
342 /* TODO: implement this */
343 } else {
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200344 err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr,
345 tmp);
346
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200347 if (err)
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000348 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200349 }
350 } else {
351 err = uncompress_addr(skb, &hdr.daddr, tmp, daddr,
Varka Bhadram4710d802014-07-02 09:01:09 +0530352 daddr_type, daddr_len);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200353 pr_debug("dest: stateless compression mode %d dest %pI6c\n",
Varka Bhadram4710d802014-07-02 09:01:09 +0530354 tmp, &hdr.daddr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200355 if (err)
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000356 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200357 }
358
Alexander Aringcc6ed262015-01-09 16:42:58 +0100359 /* Next header data uncompression */
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200360 if (iphc0 & LOWPAN_IPHC_NH_C) {
Alexander Aringcc6ed262015-01-09 16:42:58 +0100361 err = lowpan_nhc_do_uncompression(skb, dev, &hdr);
362 if (err < 0)
Martin Townsend11e3ff72014-10-13 11:00:56 +0100363 return err;
Martin Townsend11e3ff72014-10-13 11:00:56 +0100364 } else {
365 err = skb_cow(skb, sizeof(hdr));
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000366 if (unlikely(err))
Martin Townsend11e3ff72014-10-13 11:00:56 +0100367 return err;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200368 }
369
370 hdr.payload_len = htons(skb->len);
371
372 pr_debug("skb headroom size = %d, data length = %d\n",
373 skb_headroom(skb), skb->len);
374
375 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
376 "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
377 hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
378 hdr.hop_limit, &hdr.daddr);
379
Martin Townsendf8b36172014-10-23 15:40:53 +0100380 skb_push(skb, sizeof(hdr));
381 skb_reset_network_header(skb);
382 skb_copy_to_linear_data(skb, &hdr, sizeof(hdr));
383
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200384 raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200385
Martin Townsendf8b36172014-10-23 15:40:53 +0100386 return 0;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200387}
Martin Townsend01141232014-10-23 15:40:56 +0100388EXPORT_SYMBOL_GPL(lowpan_header_decompress);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200389
Alexander Aring84ca5e02014-07-29 23:46:58 +0200390static u8 lowpan_compress_addr_64(u8 **hc_ptr, u8 shift,
Varka Bhadram4710d802014-07-02 09:01:09 +0530391 const struct in6_addr *ipaddr,
392 const unsigned char *lladdr)
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200393{
394 u8 val = 0;
395
396 if (is_addr_mac_addr_based(ipaddr, lladdr)) {
397 val = 3; /* 0-bits */
398 pr_debug("address compression 0 bits\n");
399 } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
400 /* compress IID to 16 bits xxxx::XXXX */
Alexander Aring85c71242014-07-29 23:47:01 +0200401 lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200402 val = 2; /* 16-bits */
403 raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
Alexander Aring84ca5e02014-07-29 23:46:58 +0200404 *hc_ptr - 2, 2);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200405 } else {
406 /* do not compress IID => xxxx::IID */
Alexander Aring85c71242014-07-29 23:47:01 +0200407 lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200408 val = 1; /* 64-bits */
409 raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
Alexander Aring84ca5e02014-07-29 23:46:58 +0200410 *hc_ptr - 8, 8);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200411 }
412
413 return rol8(val, shift);
414}
415
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200416int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
Varka Bhadram4710d802014-07-02 09:01:09 +0530417 unsigned short type, const void *_daddr,
418 const void *_saddr, unsigned int len)
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200419{
Alexander Aring84ca5e02014-07-29 23:46:58 +0200420 u8 tmp, iphc0, iphc1, *hc_ptr;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200421 struct ipv6hdr *hdr;
422 u8 head[100] = {};
Alexander Aringcc6ed262015-01-09 16:42:58 +0100423 int ret, addr_type;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200424
425 if (type != ETH_P_IPV6)
426 return -EINVAL;
427
428 hdr = ipv6_hdr(skb);
Alexander Aring84ca5e02014-07-29 23:46:58 +0200429 hc_ptr = head + 2;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200430
431 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
432 "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
Varka Bhadram4710d802014-07-02 09:01:09 +0530433 hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
434 hdr->hop_limit, &hdr->daddr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200435
436 raw_dump_table(__func__, "raw skb network header dump",
Varka Bhadram4710d802014-07-02 09:01:09 +0530437 skb_network_header(skb), sizeof(struct ipv6hdr));
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200438
Varka Bhadram4710d802014-07-02 09:01:09 +0530439 /* As we copy some bit-length fields, in the IPHC encoding bytes,
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200440 * we sometimes use |=
441 * If the field is 0, and the current bit value in memory is 1,
442 * this does not work. We therefore reset the IPHC encoding here
443 */
444 iphc0 = LOWPAN_DISPATCH_IPHC;
445 iphc1 = 0;
446
447 /* TODO: context lookup */
448
449 raw_dump_inline(__func__, "saddr",
450 (unsigned char *)_saddr, IEEE802154_ADDR_LEN);
451 raw_dump_inline(__func__, "daddr",
452 (unsigned char *)_daddr, IEEE802154_ADDR_LEN);
453
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200454 raw_dump_table(__func__, "sending raw skb network uncompressed packet",
Varka Bhadram4710d802014-07-02 09:01:09 +0530455 skb->data, skb->len);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200456
Varka Bhadram4710d802014-07-02 09:01:09 +0530457 /* Traffic class, flow label
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200458 * If flow label is 0, compress it. If traffic class is 0, compress it
459 * We have to process both in the same time as the offset of traffic
460 * class depends on the presence of version and flow label
461 */
462
Alexander Aring84ca5e02014-07-29 23:46:58 +0200463 /* hc format of TC is ECN | DSCP , original one is DSCP | ECN */
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200464 tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
465 tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
466
467 if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
Varka Bhadram4710d802014-07-02 09:01:09 +0530468 (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200469 /* flow label can be compressed */
470 iphc0 |= LOWPAN_IPHC_FL_C;
471 if ((hdr->priority == 0) &&
Varka Bhadram4710d802014-07-02 09:01:09 +0530472 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200473 /* compress (elide) all */
474 iphc0 |= LOWPAN_IPHC_TC_C;
475 } else {
476 /* compress only the flow label */
Alexander Aring84ca5e02014-07-29 23:46:58 +0200477 *hc_ptr = tmp;
478 hc_ptr += 1;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200479 }
480 } else {
481 /* Flow label cannot be compressed */
482 if ((hdr->priority == 0) &&
Varka Bhadram4710d802014-07-02 09:01:09 +0530483 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200484 /* compress only traffic class */
485 iphc0 |= LOWPAN_IPHC_TC_C;
Alexander Aring84ca5e02014-07-29 23:46:58 +0200486 *hc_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
487 memcpy(hc_ptr + 1, &hdr->flow_lbl[1], 2);
488 hc_ptr += 3;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200489 } else {
490 /* compress nothing */
Alexander Aring84ca5e02014-07-29 23:46:58 +0200491 memcpy(hc_ptr, hdr, 4);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200492 /* replace the top byte with new ECN | DSCP format */
Alexander Aring84ca5e02014-07-29 23:46:58 +0200493 *hc_ptr = tmp;
494 hc_ptr += 4;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200495 }
496 }
497
498 /* NOTE: payload length is always compressed */
499
Alexander Aringcc6ed262015-01-09 16:42:58 +0100500 /* Check if we provide the nhc format for nexthdr and compression
501 * functionality. If not nexthdr is handled inline and not compressed.
502 */
503 ret = lowpan_nhc_check_compression(skb, hdr, &hc_ptr, &iphc0);
504 if (ret < 0)
505 return ret;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200506
Varka Bhadram4710d802014-07-02 09:01:09 +0530507 /* Hop limit
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200508 * if 1: compress, encoding is 01
509 * if 64: compress, encoding is 10
510 * if 255: compress, encoding is 11
511 * else do not compress
512 */
513 switch (hdr->hop_limit) {
514 case 1:
515 iphc0 |= LOWPAN_IPHC_TTL_1;
516 break;
517 case 64:
518 iphc0 |= LOWPAN_IPHC_TTL_64;
519 break;
520 case 255:
521 iphc0 |= LOWPAN_IPHC_TTL_255;
522 break;
523 default:
Alexander Aring85c71242014-07-29 23:47:01 +0200524 lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
525 sizeof(hdr->hop_limit));
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200526 }
527
Alexander Aring556a5bf2014-07-29 23:47:02 +0200528 addr_type = ipv6_addr_type(&hdr->saddr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200529 /* source address compression */
Alexander Aring556a5bf2014-07-29 23:47:02 +0200530 if (addr_type == IPV6_ADDR_ANY) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200531 pr_debug("source address is unspecified, setting SAC\n");
532 iphc1 |= LOWPAN_IPHC_SAC;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200533 } else {
Alexander Aring556a5bf2014-07-29 23:47:02 +0200534 if (addr_type & IPV6_ADDR_LINKLOCAL) {
535 iphc1 |= lowpan_compress_addr_64(&hc_ptr,
536 LOWPAN_IPHC_SAM_BIT,
537 &hdr->saddr, _saddr);
538 pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n",
539 &hdr->saddr, iphc1);
540 } else {
541 pr_debug("send the full source address\n");
542 lowpan_push_hc_data(&hc_ptr, hdr->saddr.s6_addr, 16);
543 }
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200544 }
545
Alexander Aring556a5bf2014-07-29 23:47:02 +0200546 addr_type = ipv6_addr_type(&hdr->daddr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200547 /* destination address compression */
Alexander Aring556a5bf2014-07-29 23:47:02 +0200548 if (addr_type & IPV6_ADDR_MULTICAST) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200549 pr_debug("destination address is multicast: ");
550 iphc1 |= LOWPAN_IPHC_M;
551 if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
552 pr_debug("compressed to 1 octet\n");
553 iphc1 |= LOWPAN_IPHC_DAM_11;
554 /* use last byte */
Alexander Aring85c71242014-07-29 23:47:01 +0200555 lowpan_push_hc_data(&hc_ptr,
556 &hdr->daddr.s6_addr[15], 1);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200557 } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
558 pr_debug("compressed to 4 octets\n");
559 iphc1 |= LOWPAN_IPHC_DAM_10;
560 /* second byte + the last three */
Alexander Aring85c71242014-07-29 23:47:01 +0200561 lowpan_push_hc_data(&hc_ptr,
562 &hdr->daddr.s6_addr[1], 1);
563 lowpan_push_hc_data(&hc_ptr,
564 &hdr->daddr.s6_addr[13], 3);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200565 } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
566 pr_debug("compressed to 6 octets\n");
567 iphc1 |= LOWPAN_IPHC_DAM_01;
568 /* second byte + the last five */
Alexander Aring85c71242014-07-29 23:47:01 +0200569 lowpan_push_hc_data(&hc_ptr,
570 &hdr->daddr.s6_addr[1], 1);
571 lowpan_push_hc_data(&hc_ptr,
572 &hdr->daddr.s6_addr[11], 5);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200573 } else {
574 pr_debug("using full address\n");
575 iphc1 |= LOWPAN_IPHC_DAM_00;
Alexander Aring85c71242014-07-29 23:47:01 +0200576 lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200577 }
578 } else {
Alexander Aring556a5bf2014-07-29 23:47:02 +0200579 if (addr_type & IPV6_ADDR_LINKLOCAL) {
580 /* TODO: context lookup */
Alexander Aring84ca5e02014-07-29 23:46:58 +0200581 iphc1 |= lowpan_compress_addr_64(&hc_ptr,
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200582 LOWPAN_IPHC_DAM_BIT, &hdr->daddr, _daddr);
583 pr_debug("dest address unicast link-local %pI6c "
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200584 "iphc1 0x%02x\n", &hdr->daddr, iphc1);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200585 } else {
586 pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
Alexander Aring85c71242014-07-29 23:47:01 +0200587 lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200588 }
589 }
590
Alexander Aringcc6ed262015-01-09 16:42:58 +0100591 /* next header compression */
592 if (iphc0 & LOWPAN_IPHC_NH_C) {
593 ret = lowpan_nhc_do_compression(skb, hdr, &hc_ptr);
594 if (ret < 0)
595 return ret;
596 }
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200597
598 head[0] = iphc0;
599 head[1] = iphc1;
600
601 skb_pull(skb, sizeof(struct ipv6hdr));
602 skb_reset_transport_header(skb);
Alexander Aring84ca5e02014-07-29 23:46:58 +0200603 memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200604 skb_reset_network_header(skb);
605
Alexander Aring84ca5e02014-07-29 23:46:58 +0200606 pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200607
608 raw_dump_table(__func__, "raw skb data dump compressed",
Varka Bhadram4710d802014-07-02 09:01:09 +0530609 skb->data, skb->len);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200610 return 0;
611}
612EXPORT_SYMBOL_GPL(lowpan_header_compress);
Yann Droneaud5ae5e992014-01-22 20:25:24 +0100613
Alexander Aringcc6ed262015-01-09 16:42:58 +0100614static int __init lowpan_module_init(void)
615{
Alexander Aringff0fcc22015-01-09 16:42:59 +0100616 request_module_nowait("nhc_dest");
617 request_module_nowait("nhc_fragment");
618 request_module_nowait("nhc_hop");
619 request_module_nowait("nhc_ipv6");
620 request_module_nowait("nhc_mobility");
621 request_module_nowait("nhc_routing");
Alexander Aringcc6ed262015-01-09 16:42:58 +0100622 request_module_nowait("nhc_udp");
623
624 return 0;
625}
626module_init(lowpan_module_init);
627
Yann Droneaud5ae5e992014-01-22 20:25:24 +0100628MODULE_LICENSE("GPL");