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