blob: fcf583fe67910d7ec3d27d1294989510e91b2d76 [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>
51#include <linux/netdevice.h>
Alexander Aring8911d772015-10-13 13:42:58 +020052
Alexander Aringcefc8c82014-03-05 14:29:05 +010053#include <net/6lowpan.h>
Jukka Rissanen8df8c562013-12-11 17:05:34 +020054#include <net/ipv6.h>
Alexander Aring8911d772015-10-13 13:42:58 +020055
56/* special link-layer handling */
57#include <net/mac802154.h>
Jukka Rissanen8df8c562013-12-11 17:05:34 +020058
Alexander Aringcc6ed262015-01-09 16:42:58 +010059#include "nhc.h"
60
Alexander Aring63500472015-10-20 08:31:22 +020061/* 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
Alexander Aring8911d772015-10-13 13:42:58 +0200149static 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
Varka Bhadram4710d802014-07-02 09:01:09 +0530197/* Uncompress address function for source and
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200198 * destination address(non-multicast).
199 *
200 * address_mode is sam value or dam value.
201 */
Alexander Aring8911d772015-10-13 13:42:58 +0200202static int uncompress_addr(struct sk_buff *skb, const struct net_device *dev,
203 struct in6_addr *ipaddr, u8 address_mode,
204 const void *lladdr)
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200205{
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;
Alexander Aring8911d772015-10-13 13:42:58 +0200229 switch (lowpan_priv(dev)->lltype) {
230 case LOWPAN_LLTYPE_IEEE802154:
231 iphc_uncompress_802154_lladdr(ipaddr, lladdr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200232 break;
233 default:
Alexander Aring8911d772015-10-13 13:42:58 +0200234 iphc_uncompress_eui64_lladdr(ipaddr, lladdr);
235 break;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200236 }
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
Varka Bhadram4710d802014-07-02 09:01:09 +0530254/* Uncompress address function for source context
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200255 * based address(non-multicast).
256 */
257static int uncompress_context_based_src_addr(struct sk_buff *skb,
Varka Bhadram4710d802014-07-02 09:01:09 +0530258 struct in6_addr *ipaddr,
259 const u8 sam)
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200260{
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
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200287/* Uncompress function for multicast destination address,
288 * when M bit is set.
289 */
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200290static int lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
291 struct in6_addr *ipaddr,
292 const u8 dam)
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200293{
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",
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200338 ipaddr->s6_addr, 16);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200339
340 return 0;
341}
342
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200343/* TTL uncompression values */
344static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 };
345
Alexander Aring8911d772015-10-13 13:42:58 +0200346int lowpan_header_decompress(struct sk_buff *skb, const struct net_device *dev,
347 const void *daddr, const void *saddr)
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200348{
349 struct ipv6hdr hdr = {};
Alexander Aring8911d772015-10-13 13:42:58 +0200350 u8 iphc0, iphc1, tmp, num_context = 0;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200351 int err;
352
353 raw_dump_table(__func__, "raw skb data dump uncompressed",
Varka Bhadram4710d802014-07-02 09:01:09 +0530354 skb->data, skb->len);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200355
Alexander Aring478208e2015-10-13 13:42:59 +0200356 if (lowpan_fetch_skb(skb, &iphc0, sizeof(iphc0)) ||
357 lowpan_fetch_skb(skb, &iphc1, sizeof(iphc1)))
Alexander Aring8911d772015-10-13 13:42:58 +0200358 return -EINVAL;
359
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200360 /* 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");
Alexander Aring4ebc9602014-07-29 23:47:00 +0200363 if (lowpan_fetch_skb(skb, &num_context, sizeof(num_context)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000364 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200365 }
366
367 hdr.version = 6;
368
369 /* Traffic Class and Flow Label */
370 switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
Varka Bhadram4710d802014-07-02 09:01:09 +0530371 /* Traffic Class and FLow Label carried in-line
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200372 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
373 */
374 case 0: /* 00b */
Alexander Aring4ebc9602014-07-29 23:47:00 +0200375 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000376 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200377
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;
Varka Bhadram4710d802014-07-02 09:01:09 +0530384 /* Traffic class carried in-line
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200385 * ECN + DSCP (1 byte), Flow Label is elided
386 */
387 case 2: /* 10b */
Alexander Aring4ebc9602014-07-29 23:47:00 +0200388 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000389 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200390
391 hdr.priority = ((tmp >> 2) & 0x0f);
392 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
393 break;
Varka Bhadram4710d802014-07-02 09:01:09 +0530394 /* Flow Label carried in-line
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200395 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
396 */
397 case 1: /* 01b */
Alexander Aring4ebc9602014-07-29 23:47:00 +0200398 if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000399 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200400
Lukasz Duda77e867b2015-08-10 21:15:52 +0200401 hdr.flow_lbl[0] = (tmp & 0x0F) | ((tmp >> 2) & 0x30);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200402 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 */
Alexander Aring4ebc9602014-07-29 23:47:00 +0200415 if (lowpan_fetch_skb(skb, &hdr.nexthdr, sizeof(hdr.nexthdr)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000416 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200417
418 pr_debug("NH flag is set, next header carried inline: %02x\n",
419 hdr.nexthdr);
420 }
421
422 /* Hop Limit */
Varka Bhadram4710d802014-07-02 09:01:09 +0530423 if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200424 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
Varka Bhadram4710d802014-07-02 09:01:09 +0530425 } else {
Alexander Aring4ebc9602014-07-29 23:47:00 +0200426 if (lowpan_fetch_skb(skb, &hdr.hop_limit,
427 sizeof(hdr.hop_limit)))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000428 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200429 }
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");
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200437 err = uncompress_context_based_src_addr(skb, &hdr.saddr, tmp);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200438 } else {
439 /* Source address uncompression */
440 pr_debug("source address stateless compression\n");
Alexander Aring8911d772015-10-13 13:42:58 +0200441 err = uncompress_addr(skb, dev, &hdr.saddr, tmp, saddr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200442 }
443
444 /* Check on error of previous branch */
445 if (err)
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000446 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200447
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 {
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200457 err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr,
458 tmp);
459
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200460 if (err)
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000461 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200462 }
463 } else {
Alexander Aring8911d772015-10-13 13:42:58 +0200464 err = uncompress_addr(skb, dev, &hdr.daddr, tmp, daddr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200465 pr_debug("dest: stateless compression mode %d dest %pI6c\n",
Varka Bhadram4710d802014-07-02 09:01:09 +0530466 tmp, &hdr.daddr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200467 if (err)
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000468 return -EINVAL;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200469 }
470
Alexander Aringcc6ed262015-01-09 16:42:58 +0100471 /* Next header data uncompression */
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200472 if (iphc0 & LOWPAN_IPHC_NH_C) {
Alexander Aringcc6ed262015-01-09 16:42:58 +0100473 err = lowpan_nhc_do_uncompression(skb, dev, &hdr);
474 if (err < 0)
Martin Townsend11e3ff72014-10-13 11:00:56 +0100475 return err;
Martin Townsend11e3ff72014-10-13 11:00:56 +0100476 } else {
477 err = skb_cow(skb, sizeof(hdr));
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000478 if (unlikely(err))
Martin Townsend11e3ff72014-10-13 11:00:56 +0100479 return err;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200480 }
481
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200482 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 }
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200494
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
Martin Townsendf8b36172014-10-23 15:40:53 +0100503 skb_push(skb, sizeof(hdr));
504 skb_reset_network_header(skb);
505 skb_copy_to_linear_data(skb, &hdr, sizeof(hdr));
506
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200507 raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200508
Martin Townsendf8b36172014-10-23 15:40:53 +0100509 return 0;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200510}
Martin Townsend01141232014-10-23 15:40:56 +0100511EXPORT_SYMBOL_GPL(lowpan_header_decompress);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200512
Alexander Aring84ca5e02014-07-29 23:46:58 +0200513static u8 lowpan_compress_addr_64(u8 **hc_ptr, u8 shift,
Varka Bhadram4710d802014-07-02 09:01:09 +0530514 const struct in6_addr *ipaddr,
515 const unsigned char *lladdr)
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200516{
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 */
Alexander Aring85c71242014-07-29 23:47:01 +0200524 lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200525 val = 2; /* 16-bits */
526 raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
Alexander Aring84ca5e02014-07-29 23:46:58 +0200527 *hc_ptr - 2, 2);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200528 } else {
529 /* do not compress IID => xxxx::IID */
Alexander Aring85c71242014-07-29 23:47:01 +0200530 lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200531 val = 1; /* 64-bits */
532 raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
Alexander Aring84ca5e02014-07-29 23:46:58 +0200533 *hc_ptr - 8, 8);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200534 }
535
536 return rol8(val, shift);
537}
538
Alexander Aringa6f77382015-10-13 13:42:57 +0200539int lowpan_header_compress(struct sk_buff *skb, const struct net_device *dev,
540 const void *daddr, const void *saddr)
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200541{
Alexander Aring84ca5e02014-07-29 23:46:58 +0200542 u8 tmp, iphc0, iphc1, *hc_ptr;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200543 struct ipv6hdr *hdr;
Alexander Aringbf513fd2015-10-13 13:42:56 +0200544 u8 head[LOWPAN_IPHC_MAX_HC_BUF_LEN] = {};
Alexander Aringcc6ed262015-01-09 16:42:58 +0100545 int ret, addr_type;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200546
Alexander Aringa6f77382015-10-13 13:42:57 +0200547 if (skb->protocol != htons(ETH_P_IPV6))
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200548 return -EINVAL;
549
550 hdr = ipv6_hdr(skb);
Alexander Aring84ca5e02014-07-29 23:46:58 +0200551 hc_ptr = head + 2;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200552
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",
Varka Bhadram4710d802014-07-02 09:01:09 +0530555 hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
556 hdr->hop_limit, &hdr->daddr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200557
558 raw_dump_table(__func__, "raw skb network header dump",
Varka Bhadram4710d802014-07-02 09:01:09 +0530559 skb_network_header(skb), sizeof(struct ipv6hdr));
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200560
Varka Bhadram4710d802014-07-02 09:01:09 +0530561 /* As we copy some bit-length fields, in the IPHC encoding bytes,
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200562 * 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
Alexander Aringa6f77382015-10-13 13:42:57 +0200571 raw_dump_inline(__func__, "saddr", saddr, EUI64_ADDR_LEN);
572 raw_dump_inline(__func__, "daddr", daddr, EUI64_ADDR_LEN);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200573
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200574 raw_dump_table(__func__, "sending raw skb network uncompressed packet",
Varka Bhadram4710d802014-07-02 09:01:09 +0530575 skb->data, skb->len);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200576
Varka Bhadram4710d802014-07-02 09:01:09 +0530577 /* Traffic class, flow label
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200578 * 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
Alexander Aring84ca5e02014-07-29 23:46:58 +0200583 /* hc format of TC is ECN | DSCP , original one is DSCP | ECN */
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200584 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) &&
Varka Bhadram4710d802014-07-02 09:01:09 +0530588 (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200589 /* flow label can be compressed */
590 iphc0 |= LOWPAN_IPHC_FL_C;
591 if ((hdr->priority == 0) &&
Varka Bhadram4710d802014-07-02 09:01:09 +0530592 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200593 /* compress (elide) all */
594 iphc0 |= LOWPAN_IPHC_TC_C;
595 } else {
596 /* compress only the flow label */
Alexander Aring84ca5e02014-07-29 23:46:58 +0200597 *hc_ptr = tmp;
598 hc_ptr += 1;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200599 }
600 } else {
601 /* Flow label cannot be compressed */
602 if ((hdr->priority == 0) &&
Varka Bhadram4710d802014-07-02 09:01:09 +0530603 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200604 /* compress only traffic class */
605 iphc0 |= LOWPAN_IPHC_TC_C;
Alexander Aring84ca5e02014-07-29 23:46:58 +0200606 *hc_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
607 memcpy(hc_ptr + 1, &hdr->flow_lbl[1], 2);
608 hc_ptr += 3;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200609 } else {
610 /* compress nothing */
Alexander Aring84ca5e02014-07-29 23:46:58 +0200611 memcpy(hc_ptr, hdr, 4);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200612 /* replace the top byte with new ECN | DSCP format */
Alexander Aring84ca5e02014-07-29 23:46:58 +0200613 *hc_ptr = tmp;
614 hc_ptr += 4;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200615 }
616 }
617
618 /* NOTE: payload length is always compressed */
619
Alexander Aringcc6ed262015-01-09 16:42:58 +0100620 /* Check if we provide the nhc format for nexthdr and compression
621 * functionality. If not nexthdr is handled inline and not compressed.
622 */
Alexander Aring607b0bd2015-10-20 08:31:21 +0200623 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;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200629
Varka Bhadram4710d802014-07-02 09:01:09 +0530630 /* Hop limit
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200631 * 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:
Alexander Aring85c71242014-07-29 23:47:01 +0200647 lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
648 sizeof(hdr->hop_limit));
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200649 }
650
Alexander Aring556a5bf2014-07-29 23:47:02 +0200651 addr_type = ipv6_addr_type(&hdr->saddr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200652 /* source address compression */
Alexander Aring556a5bf2014-07-29 23:47:02 +0200653 if (addr_type == IPV6_ADDR_ANY) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200654 pr_debug("source address is unspecified, setting SAC\n");
655 iphc1 |= LOWPAN_IPHC_SAC;
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200656 } else {
Alexander Aring556a5bf2014-07-29 23:47:02 +0200657 if (addr_type & IPV6_ADDR_LINKLOCAL) {
658 iphc1 |= lowpan_compress_addr_64(&hc_ptr,
659 LOWPAN_IPHC_SAM_BIT,
Alexander Aringa6f77382015-10-13 13:42:57 +0200660 &hdr->saddr, saddr);
Alexander Aring556a5bf2014-07-29 23:47:02 +0200661 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 }
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200667 }
668
Alexander Aring556a5bf2014-07-29 23:47:02 +0200669 addr_type = ipv6_addr_type(&hdr->daddr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200670 /* destination address compression */
Alexander Aring556a5bf2014-07-29 23:47:02 +0200671 if (addr_type & IPV6_ADDR_MULTICAST) {
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200672 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 */
Alexander Aring85c71242014-07-29 23:47:01 +0200678 lowpan_push_hc_data(&hc_ptr,
679 &hdr->daddr.s6_addr[15], 1);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200680 } 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 */
Alexander Aring85c71242014-07-29 23:47:01 +0200684 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);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200688 } 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 */
Alexander Aring85c71242014-07-29 23:47:01 +0200692 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);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200696 } else {
697 pr_debug("using full address\n");
698 iphc1 |= LOWPAN_IPHC_DAM_00;
Alexander Aring85c71242014-07-29 23:47:01 +0200699 lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200700 }
701 } else {
Alexander Aring556a5bf2014-07-29 23:47:02 +0200702 if (addr_type & IPV6_ADDR_LINKLOCAL) {
703 /* TODO: context lookup */
Alexander Aring84ca5e02014-07-29 23:46:58 +0200704 iphc1 |= lowpan_compress_addr_64(&hc_ptr,
Alexander Aringa6f77382015-10-13 13:42:57 +0200705 LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200706 pr_debug("dest address unicast link-local %pI6c "
Marcel Holtmann7fc4cfd2014-07-30 03:48:41 +0200707 "iphc1 0x%02x\n", &hdr->daddr, iphc1);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200708 } else {
709 pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
Alexander Aring85c71242014-07-29 23:47:01 +0200710 lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200711 }
712 }
713
Alexander Aringcc6ed262015-01-09 16:42:58 +0100714 /* 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 }
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200720
721 head[0] = iphc0;
722 head[1] = iphc1;
723
724 skb_pull(skb, sizeof(struct ipv6hdr));
725 skb_reset_transport_header(skb);
Alexander Aring84ca5e02014-07-29 23:46:58 +0200726 memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200727 skb_reset_network_header(skb);
728
Alexander Aring84ca5e02014-07-29 23:46:58 +0200729 pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200730
731 raw_dump_table(__func__, "raw skb data dump compressed",
Varka Bhadram4710d802014-07-02 09:01:09 +0530732 skb->data, skb->len);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200733 return 0;
734}
735EXPORT_SYMBOL_GPL(lowpan_header_compress);