6LoWPAN: UDP header compression
[deliverable/linux.git] / net / ieee802154 / 6lowpan.c
CommitLineData
44331fe2
AS
1/*
2 * Copyright 2011, Siemens AG
3 * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
4 */
5
6/*
7 * Based on patches from Jon Smirl <jonsmirl@gmail.com>
8 * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24/* Jon's code is based on 6lowpan implementation for Contiki which is:
25 * Copyright (c) 2008, Swedish Institute of Computer Science.
26 * All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the Institute nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 */
52
44331fe2
AS
53#include <linux/bitops.h>
54#include <linux/if_arp.h>
55#include <linux/module.h>
56#include <linux/moduleparam.h>
57#include <linux/netdevice.h>
58#include <net/af_ieee802154.h>
59#include <net/ieee802154.h>
60#include <net/ieee802154_netdev.h>
61#include <net/ipv6.h>
62
63#include "6lowpan.h"
64
65/* TTL uncompression values */
66static const u8 lowpan_ttl_values[] = {0, 1, 64, 255};
67
68static LIST_HEAD(lowpan_devices);
69
70/*
71 * Uncompression of linklocal:
72 * 0 -> 16 bytes from packet
73 * 1 -> 2 bytes from prefix - bunch of zeroes and 8 from packet
74 * 2 -> 2 bytes from prefix - zeroes + 2 from packet
75 * 3 -> 2 bytes from prefix - infer 8 bytes from lladdr
76 *
77 * NOTE: => the uncompress function does change 0xf to 0x10
78 * NOTE: 0x00 => no-autoconfig => unspecified
79 */
80static const u8 lowpan_unc_llconf[] = {0x0f, 0x28, 0x22, 0x20};
81
82/*
83 * Uncompression of ctx-based:
84 * 0 -> 0 bits from packet [unspecified / reserved]
85 * 1 -> 8 bytes from prefix - bunch of zeroes and 8 from packet
86 * 2 -> 8 bytes from prefix - zeroes + 2 from packet
87 * 3 -> 8 bytes from prefix - infer 8 bytes from lladdr
88 */
89static const u8 lowpan_unc_ctxconf[] = {0x00, 0x88, 0x82, 0x80};
90
91/*
92 * Uncompression of ctx-base
93 * 0 -> 0 bits from packet
94 * 1 -> 2 bytes from prefix - bunch of zeroes 5 from packet
95 * 2 -> 2 bytes from prefix - zeroes + 3 from packet
96 * 3 -> 2 bytes from prefix - infer 1 bytes from lladdr
97 */
98static const u8 lowpan_unc_mxconf[] = {0x0f, 0x25, 0x23, 0x21};
99
100/* Link local prefix */
101static const u8 lowpan_llprefix[] = {0xfe, 0x80};
102
103/* private device info */
104struct lowpan_dev_info {
105 struct net_device *real_dev; /* real WPAN device ptr */
106 struct mutex dev_list_mtx; /* mutex for list ops */
107};
108
109struct lowpan_dev_record {
110 struct net_device *ldev;
111 struct list_head list;
112};
113
719269af 114struct lowpan_fragment {
115 struct sk_buff *skb; /* skb to be assembled */
116 spinlock_t lock; /* concurency lock */
117 u16 length; /* length to be assemled */
118 u32 bytes_rcv; /* bytes received */
119 u16 tag; /* current fragment tag */
120 struct timer_list timer; /* assembling timer */
121 struct list_head list; /* fragments list */
122};
123
124static unsigned short fragment_tag;
125static LIST_HEAD(lowpan_fragments);
126spinlock_t flist_lock;
127
44331fe2
AS
128static inline struct
129lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
130{
131 return netdev_priv(dev);
132}
133
134static inline void lowpan_address_flip(u8 *src, u8 *dest)
135{
136 int i;
137 for (i = 0; i < IEEE802154_ADDR_LEN; i++)
138 (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i];
139}
140
141/* list of all 6lowpan devices, uses for package delivering */
142/* print data in line */
143static inline void lowpan_raw_dump_inline(const char *caller, char *msg,
144 unsigned char *buf, int len)
145{
146#ifdef DEBUG
147 if (msg)
148 pr_debug("(%s) %s: ", caller, msg);
149 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE,
150 16, 1, buf, len, false);
151#endif /* DEBUG */
152}
153
154/*
155 * print data in a table format:
156 *
157 * addr: xx xx xx xx xx xx
158 * addr: xx xx xx xx xx xx
159 * ...
160 */
161static inline void lowpan_raw_dump_table(const char *caller, char *msg,
162 unsigned char *buf, int len)
163{
164#ifdef DEBUG
165 if (msg)
166 pr_debug("(%s) %s:\n", caller, msg);
167 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET,
168 16, 1, buf, len, false);
169#endif /* DEBUG */
170}
171
172static u8
173lowpan_compress_addr_64(u8 **hc06_ptr, u8 shift, const struct in6_addr *ipaddr,
174 const unsigned char *lladdr)
175{
176 u8 val = 0;
177
178 if (is_addr_mac_addr_based(ipaddr, lladdr))
179 val = 3; /* 0-bits */
180 else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
181 /* compress IID to 16 bits xxxx::XXXX */
182 memcpy(*hc06_ptr, &ipaddr->s6_addr16[7], 2);
183 *hc06_ptr += 2;
184 val = 2; /* 16-bits */
185 } else {
186 /* do not compress IID => xxxx::IID */
187 memcpy(*hc06_ptr, &ipaddr->s6_addr16[4], 8);
188 *hc06_ptr += 8;
189 val = 1; /* 64-bits */
190 }
191
192 return rol8(val, shift);
193}
194
195static void
196lowpan_uip_ds6_set_addr_iid(struct in6_addr *ipaddr, unsigned char *lladdr)
197{
198 memcpy(&ipaddr->s6_addr[8], lladdr, IEEE802154_ALEN);
199 /* second bit-flip (Universe/Local) is done according RFC2464 */
200 ipaddr->s6_addr[8] ^= 0x02;
201}
202
203/*
204 * Uncompress addresses based on a prefix and a postfix with zeroes in
205 * between. If the postfix is zero in length it will use the link address
206 * to configure the IP address (autoconf style).
207 * pref_post_count takes a byte where the first nibble specify prefix count
208 * and the second postfix count (NOTE: 15/0xf => 16 bytes copy).
209 */
210static int
211lowpan_uncompress_addr(struct sk_buff *skb, struct in6_addr *ipaddr,
212 u8 const *prefix, u8 pref_post_count, unsigned char *lladdr)
213{
214 u8 prefcount = pref_post_count >> 4;
215 u8 postcount = pref_post_count & 0x0f;
216
217 /* full nibble 15 => 16 */
218 prefcount = (prefcount == 15 ? 16 : prefcount);
219 postcount = (postcount == 15 ? 16 : postcount);
220
221 if (lladdr)
222 lowpan_raw_dump_inline(__func__, "linklocal address",
223 lladdr, IEEE802154_ALEN);
224 if (prefcount > 0)
225 memcpy(ipaddr, prefix, prefcount);
226
227 if (prefcount + postcount < 16)
228 memset(&ipaddr->s6_addr[prefcount], 0,
229 16 - (prefcount + postcount));
230
231 if (postcount > 0) {
232 memcpy(&ipaddr->s6_addr[16 - postcount], skb->data, postcount);
233 skb_pull(skb, postcount);
234 } else if (prefcount > 0) {
235 if (lladdr == NULL)
236 return -EINVAL;
237
238 /* no IID based configuration if no prefix and no data */
239 lowpan_uip_ds6_set_addr_iid(ipaddr, lladdr);
240 }
241
242 pr_debug("(%s): uncompressing %d + %d => ", __func__, prefcount,
243 postcount);
244 lowpan_raw_dump_inline(NULL, NULL, ipaddr->s6_addr, 16);
245
246 return 0;
247}
248
3bd5b958 249static void
250lowpan_compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
251{
252 struct udphdr *uh = udp_hdr(skb);
253
254 pr_debug("(%s): UDP header compression\n", __func__);
255
256 if (((uh->source & LOWPAN_NHC_UDP_4BIT_MASK) ==
257 LOWPAN_NHC_UDP_4BIT_PORT) &&
258 ((uh->dest & LOWPAN_NHC_UDP_4BIT_MASK) ==
259 LOWPAN_NHC_UDP_4BIT_PORT)) {
260 pr_debug("(%s): both ports compression to 4 bits\n", __func__);
261 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_11;
262 **(hc06_ptr + 1) = /* subtraction is faster */
263 (u8)((uh->dest - LOWPAN_NHC_UDP_4BIT_PORT) +
264 ((uh->source & LOWPAN_NHC_UDP_4BIT_PORT) << 4));
265 *hc06_ptr += 2;
266 } else if ((uh->dest & LOWPAN_NHC_UDP_8BIT_MASK) ==
267 LOWPAN_NHC_UDP_8BIT_PORT) {
268 pr_debug("(%s): remove 8 bits of dest\n", __func__);
269 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_01;
270 memcpy(*hc06_ptr + 1, &uh->source, 2);
271 **(hc06_ptr + 3) = (u8)(uh->dest - LOWPAN_NHC_UDP_8BIT_PORT);
272 *hc06_ptr += 4;
273 } else if ((uh->source & LOWPAN_NHC_UDP_8BIT_MASK) ==
274 LOWPAN_NHC_UDP_8BIT_PORT) {
275 pr_debug("(%s): remove 8 bits of source\n", __func__);
276 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_10;
277 memcpy(*hc06_ptr + 1, &uh->dest, 2);
278 **(hc06_ptr + 3) = (u8)(uh->source - LOWPAN_NHC_UDP_8BIT_PORT);
279 *hc06_ptr += 4;
280 } else {
281 pr_debug("(%s): can't compress header\n", __func__);
282 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_00;
283 memcpy(*hc06_ptr + 1, &uh->source, 2);
284 memcpy(*hc06_ptr + 3, &uh->dest, 2);
285 *hc06_ptr += 5;
286 }
287
288 /* checksum is always inline */
289 memcpy(*hc06_ptr, &uh->check, 2);
290 *hc06_ptr += 2;
291}
292
44331fe2
AS
293static u8 lowpan_fetch_skb_u8(struct sk_buff *skb)
294{
295 u8 ret;
296
297 ret = skb->data[0];
298 skb_pull(skb, 1);
299
300 return ret;
301}
302
719269af 303static u16 lowpan_fetch_skb_u16(struct sk_buff *skb)
304{
305 u16 ret;
306
307 BUG_ON(!pskb_may_pull(skb, 2));
308
309 ret = skb->data[0] | (skb->data[1] << 8);
310 skb_pull(skb, 2);
311 return ret;
312}
313
44331fe2
AS
314static int lowpan_header_create(struct sk_buff *skb,
315 struct net_device *dev,
316 unsigned short type, const void *_daddr,
317 const void *_saddr, unsigned len)
318{
319 u8 tmp, iphc0, iphc1, *hc06_ptr;
320 struct ipv6hdr *hdr;
321 const u8 *saddr = _saddr;
322 const u8 *daddr = _daddr;
323 u8 *head;
324 struct ieee802154_addr sa, da;
325
326 if (type != ETH_P_IPV6)
327 return 0;
328 /* TODO:
329 * if this package isn't ipv6 one, where should it be routed?
330 */
331 head = kzalloc(100, GFP_KERNEL);
332 if (head == NULL)
333 return -ENOMEM;
334
335 hdr = ipv6_hdr(skb);
336 hc06_ptr = head + 2;
337
338 pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
339 "\tnexthdr = 0x%02x\n\thop_lim = %d\n", __func__,
340 hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
341 hdr->hop_limit);
342
343 lowpan_raw_dump_table(__func__, "raw skb network header dump",
344 skb_network_header(skb), sizeof(struct ipv6hdr));
345
346 if (!saddr)
347 saddr = dev->dev_addr;
348
349 lowpan_raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
350
351 /*
352 * As we copy some bit-length fields, in the IPHC encoding bytes,
353 * we sometimes use |=
354 * If the field is 0, and the current bit value in memory is 1,
355 * this does not work. We therefore reset the IPHC encoding here
356 */
357 iphc0 = LOWPAN_DISPATCH_IPHC;
358 iphc1 = 0;
359
360 /* TODO: context lookup */
361
362 lowpan_raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
363
364 /*
365 * Traffic class, flow label
366 * If flow label is 0, compress it. If traffic class is 0, compress it
367 * We have to process both in the same time as the offset of traffic
368 * class depends on the presence of version and flow label
369 */
370
371 /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
372 tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
373 tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
374
375 if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
376 (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
377 /* flow label can be compressed */
378 iphc0 |= LOWPAN_IPHC_FL_C;
379 if ((hdr->priority == 0) &&
380 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
381 /* compress (elide) all */
382 iphc0 |= LOWPAN_IPHC_TC_C;
383 } else {
384 /* compress only the flow label */
385 *hc06_ptr = tmp;
386 hc06_ptr += 1;
387 }
388 } else {
389 /* Flow label cannot be compressed */
390 if ((hdr->priority == 0) &&
391 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
392 /* compress only traffic class */
393 iphc0 |= LOWPAN_IPHC_TC_C;
394 *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
395 memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
396 hc06_ptr += 3;
397 } else {
398 /* compress nothing */
399 memcpy(hc06_ptr, &hdr, 4);
400 /* replace the top byte with new ECN | DSCP format */
401 *hc06_ptr = tmp;
402 hc06_ptr += 4;
403 }
404 }
405
406 /* NOTE: payload length is always compressed */
407
408 /* Next Header is compress if UDP */
409 if (hdr->nexthdr == UIP_PROTO_UDP)
410 iphc0 |= LOWPAN_IPHC_NH_C;
411
44331fe2
AS
412 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
413 *hc06_ptr = hdr->nexthdr;
414 hc06_ptr += 1;
415 }
416
417 /*
418 * Hop limit
419 * if 1: compress, encoding is 01
420 * if 64: compress, encoding is 10
421 * if 255: compress, encoding is 11
422 * else do not compress
423 */
424 switch (hdr->hop_limit) {
425 case 1:
426 iphc0 |= LOWPAN_IPHC_TTL_1;
427 break;
428 case 64:
429 iphc0 |= LOWPAN_IPHC_TTL_64;
430 break;
431 case 255:
432 iphc0 |= LOWPAN_IPHC_TTL_255;
433 break;
434 default:
435 *hc06_ptr = hdr->hop_limit;
436 break;
437 }
438
439 /* source address compression */
440 if (is_addr_unspecified(&hdr->saddr)) {
441 pr_debug("(%s): source address is unspecified, setting SAC\n",
442 __func__);
443 iphc1 |= LOWPAN_IPHC_SAC;
444 /* TODO: context lookup */
445 } else if (is_addr_link_local(&hdr->saddr)) {
446 pr_debug("(%s): source address is link-local\n", __func__);
447 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
448 LOWPAN_IPHC_SAM_BIT, &hdr->saddr, saddr);
449 } else {
450 pr_debug("(%s): send the full source address\n", __func__);
451 memcpy(hc06_ptr, &hdr->saddr.s6_addr16[0], 16);
452 hc06_ptr += 16;
453 }
454
455 /* destination address compression */
456 if (is_addr_mcast(&hdr->daddr)) {
457 pr_debug("(%s): destination address is multicast", __func__);
458 iphc1 |= LOWPAN_IPHC_M;
459 if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
460 pr_debug("compressed to 1 octet\n");
461 iphc1 |= LOWPAN_IPHC_DAM_11;
462 /* use last byte */
463 *hc06_ptr = hdr->daddr.s6_addr[15];
464 hc06_ptr += 1;
465 } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
466 pr_debug("compressed to 4 octets\n");
467 iphc1 |= LOWPAN_IPHC_DAM_10;
468 /* second byte + the last three */
469 *hc06_ptr = hdr->daddr.s6_addr[1];
470 memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[13], 3);
471 hc06_ptr += 4;
472 } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
473 pr_debug("compressed to 6 octets\n");
474 iphc1 |= LOWPAN_IPHC_DAM_01;
475 /* second byte + the last five */
476 *hc06_ptr = hdr->daddr.s6_addr[1];
477 memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[11], 5);
478 hc06_ptr += 6;
479 } else {
480 pr_debug("using full address\n");
481 iphc1 |= LOWPAN_IPHC_DAM_00;
482 memcpy(hc06_ptr, &hdr->daddr.s6_addr[0], 16);
483 hc06_ptr += 16;
484 }
485 } else {
486 pr_debug("(%s): destination address is unicast: ", __func__);
487 /* TODO: context lookup */
488 if (is_addr_link_local(&hdr->daddr)) {
489 pr_debug("destination address is link-local\n");
490 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
491 LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
492 } else {
493 pr_debug("using full address\n");
494 memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
495 hc06_ptr += 16;
496 }
497 }
498
3bd5b958 499 /* UDP header compression */
500 if (hdr->nexthdr == UIP_PROTO_UDP)
501 lowpan_compress_udp_header(&hc06_ptr, skb);
44331fe2
AS
502
503 head[0] = iphc0;
504 head[1] = iphc1;
505
506 skb_pull(skb, sizeof(struct ipv6hdr));
507 memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
508
509 kfree(head);
510
511 lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
512 skb->len);
513
514 /*
515 * NOTE1: I'm still unsure about the fact that compression and WPAN
516 * header are created here and not later in the xmit. So wait for
517 * an opinion of net maintainers.
518 */
519 /*
520 * NOTE2: to be absolutely correct, we must derive PANid information
521 * from MAC subif of the 'dev' and 'real_dev' network devices, but
522 * this isn't implemented in mainline yet, so currently we assign 0xff
523 */
524 {
525 /* prepare wpan address data */
526 sa.addr_type = IEEE802154_ADDR_LONG;
527 sa.pan_id = 0xff;
528
529 da.addr_type = IEEE802154_ADDR_LONG;
530 da.pan_id = 0xff;
531
532 memcpy(&(da.hwaddr), daddr, 8);
533 memcpy(&(sa.hwaddr), saddr, 8);
534
535 mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
719269af 536
44331fe2
AS
537 return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
538 type, (void *)&da, (void *)&sa, skb->len);
539 }
540}
541
542static int lowpan_skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr)
543{
544 struct sk_buff *new;
545 struct lowpan_dev_record *entry;
546 int stat = NET_RX_SUCCESS;
547
548 new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
dcef1151 549 GFP_ATOMIC);
44331fe2
AS
550 kfree_skb(skb);
551
dcef1151 552 if (!new)
44331fe2
AS
553 return -ENOMEM;
554
555 skb_push(new, sizeof(struct ipv6hdr));
556 skb_reset_network_header(new);
557 skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
558
559 new->protocol = htons(ETH_P_IPV6);
560 new->pkt_type = PACKET_HOST;
561
562 rcu_read_lock();
563 list_for_each_entry_rcu(entry, &lowpan_devices, list)
564 if (lowpan_dev_info(entry->ldev)->real_dev == new->dev) {
dcef1151 565 skb = skb_copy(new, GFP_ATOMIC);
566 if (!skb) {
567 stat = -ENOMEM;
568 break;
569 }
44331fe2 570
dcef1151 571 skb->dev = entry->ldev;
572 stat = netif_rx(skb);
44331fe2
AS
573 }
574 rcu_read_unlock();
575
576 kfree_skb(new);
577
578 return stat;
579}
580
719269af 581static void lowpan_fragment_timer_expired(unsigned long entry_addr)
582{
583 struct lowpan_fragment *entry = (struct lowpan_fragment *)entry_addr;
584
585 pr_debug("%s: timer expired for frame with tag %d\n", __func__,
586 entry->tag);
587
588 spin_lock(&flist_lock);
589 list_del(&entry->list);
590 spin_unlock(&flist_lock);
591
592 dev_kfree_skb(entry->skb);
593 kfree(entry);
594}
595
44331fe2
AS
596static int
597lowpan_process_data(struct sk_buff *skb)
598{
599 struct ipv6hdr hdr;
600 u8 tmp, iphc0, iphc1, num_context = 0;
601 u8 *_saddr, *_daddr;
602 int err;
603
604 lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
605 skb->len);
606 /* at least two bytes will be used for the encoding */
607 if (skb->len < 2)
608 goto drop;
609 iphc0 = lowpan_fetch_skb_u8(skb);
719269af 610
611 /* fragments assembling */
612 switch (iphc0 & LOWPAN_DISPATCH_MASK) {
613 case LOWPAN_DISPATCH_FRAG1:
614 case LOWPAN_DISPATCH_FRAGN:
615 {
616 struct lowpan_fragment *frame;
617 u8 len, offset;
618 u16 tag;
619 bool found = false;
620
621 len = lowpan_fetch_skb_u8(skb); /* frame length */
622 tag = lowpan_fetch_skb_u16(skb);
623
624 /*
625 * check if frame assembling with the same tag is
626 * already in progress
627 */
628 spin_lock(&flist_lock);
629
630 list_for_each_entry(frame, &lowpan_fragments, list)
631 if (frame->tag == tag) {
632 found = true;
633 break;
634 }
635
636 /* alloc new frame structure */
637 if (!found) {
638 frame = kzalloc(sizeof(struct lowpan_fragment),
639 GFP_ATOMIC);
640 if (!frame)
641 goto unlock_and_drop;
642
643 INIT_LIST_HEAD(&frame->list);
644
645 frame->length = (iphc0 & 7) | (len << 3);
646 frame->tag = tag;
647
648 /* allocate buffer for frame assembling */
649 frame->skb = alloc_skb(frame->length +
650 sizeof(struct ipv6hdr), GFP_ATOMIC);
651
652 if (!frame->skb) {
653 kfree(frame);
654 goto unlock_and_drop;
655 }
656
657 frame->skb->priority = skb->priority;
658 frame->skb->dev = skb->dev;
659
660 /* reserve headroom for uncompressed ipv6 header */
661 skb_reserve(frame->skb, sizeof(struct ipv6hdr));
662 skb_put(frame->skb, frame->length);
663
664 init_timer(&frame->timer);
665 /* time out is the same as for ipv6 - 60 sec */
666 frame->timer.expires = jiffies + LOWPAN_FRAG_TIMEOUT;
667 frame->timer.data = (unsigned long)frame;
668 frame->timer.function = lowpan_fragment_timer_expired;
669
670 add_timer(&frame->timer);
671
672 list_add_tail(&frame->list, &lowpan_fragments);
673 }
674
675 if ((iphc0 & LOWPAN_DISPATCH_MASK) == LOWPAN_DISPATCH_FRAG1)
676 goto unlock_and_drop;
677
678 offset = lowpan_fetch_skb_u8(skb); /* fetch offset */
679
680 /* if payload fits buffer, copy it */
681 if (likely((offset * 8 + skb->len) <= frame->length))
682 skb_copy_to_linear_data_offset(frame->skb, offset * 8,
683 skb->data, skb->len);
684 else
685 goto unlock_and_drop;
686
687 frame->bytes_rcv += skb->len;
688
689 /* frame assembling complete */
690 if ((frame->bytes_rcv == frame->length) &&
691 frame->timer.expires > jiffies) {
692 /* if timer haven't expired - first of all delete it */
693 del_timer(&frame->timer);
694 list_del(&frame->list);
695 spin_unlock(&flist_lock);
696
697 dev_kfree_skb(skb);
698 skb = frame->skb;
699 kfree(frame);
700 iphc0 = lowpan_fetch_skb_u8(skb);
701 break;
702 }
703 spin_unlock(&flist_lock);
704
705 return kfree_skb(skb), 0;
706 }
707 default:
708 break;
709 }
710
44331fe2
AS
711 iphc1 = lowpan_fetch_skb_u8(skb);
712
713 _saddr = mac_cb(skb)->sa.hwaddr;
714 _daddr = mac_cb(skb)->da.hwaddr;
715
716 pr_debug("(%s): iphc0 = %02x, iphc1 = %02x\n", __func__, iphc0, iphc1);
717
718 /* another if the CID flag is set */
719 if (iphc1 & LOWPAN_IPHC_CID) {
720 pr_debug("(%s): CID flag is set, increase header with one\n",
721 __func__);
722 if (!skb->len)
723 goto drop;
724 num_context = lowpan_fetch_skb_u8(skb);
725 }
726
727 hdr.version = 6;
728
729 /* Traffic Class and Flow Label */
730 switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
731 /*
732 * Traffic Class and FLow Label carried in-line
733 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
734 */
735 case 0: /* 00b */
736 if (!skb->len)
737 goto drop;
738 tmp = lowpan_fetch_skb_u8(skb);
739 memcpy(&hdr.flow_lbl, &skb->data[0], 3);
740 skb_pull(skb, 3);
741 hdr.priority = ((tmp >> 2) & 0x0f);
742 hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
743 (hdr.flow_lbl[0] & 0x0f);
744 break;
745 /*
746 * Traffic class carried in-line
747 * ECN + DSCP (1 byte), Flow Label is elided
748 */
749 case 1: /* 10b */
750 if (!skb->len)
751 goto drop;
752 tmp = lowpan_fetch_skb_u8(skb);
753 hdr.priority = ((tmp >> 2) & 0x0f);
754 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
755 hdr.flow_lbl[1] = 0;
756 hdr.flow_lbl[2] = 0;
757 break;
758 /*
759 * Flow Label carried in-line
760 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
761 */
762 case 2: /* 01b */
763 if (!skb->len)
764 goto drop;
765 tmp = lowpan_fetch_skb_u8(skb);
766 hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
767 memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
768 skb_pull(skb, 2);
769 break;
770 /* Traffic Class and Flow Label are elided */
771 case 3: /* 11b */
772 hdr.priority = 0;
773 hdr.flow_lbl[0] = 0;
774 hdr.flow_lbl[1] = 0;
775 hdr.flow_lbl[2] = 0;
776 break;
777 default:
778 break;
779 }
780
781 /* Next Header */
782 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
783 /* Next header is carried inline */
784 if (!skb->len)
785 goto drop;
786 hdr.nexthdr = lowpan_fetch_skb_u8(skb);
787 pr_debug("(%s): NH flag is set, next header is carried "
788 "inline: %02x\n", __func__, hdr.nexthdr);
789 }
790
791 /* Hop Limit */
792 if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
793 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
794 else {
795 if (!skb->len)
796 goto drop;
797 hdr.hop_limit = lowpan_fetch_skb_u8(skb);
798 }
799
800 /* Extract SAM to the tmp variable */
801 tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
802
803 /* Source address uncompression */
804 pr_debug("(%s): source address stateless compression\n", __func__);
805 err = lowpan_uncompress_addr(skb, &hdr.saddr, lowpan_llprefix,
806 lowpan_unc_llconf[tmp], skb->data);
807 if (err)
808 goto drop;
809
810 /* Extract DAM to the tmp variable */
811 tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
812
813 /* check for Multicast Compression */
814 if (iphc1 & LOWPAN_IPHC_M) {
815 if (iphc1 & LOWPAN_IPHC_DAC) {
816 pr_debug("(%s): destination address context-based "
817 "multicast compression\n", __func__);
818 /* TODO: implement this */
819 } else {
820 u8 prefix[] = {0xff, 0x02};
821
822 pr_debug("(%s): destination address non-context-based"
823 " multicast compression\n", __func__);
824 if (0 < tmp && tmp < 3) {
825 if (!skb->len)
826 goto drop;
827 else
828 prefix[1] = lowpan_fetch_skb_u8(skb);
829 }
830
831 err = lowpan_uncompress_addr(skb, &hdr.daddr, prefix,
832 lowpan_unc_mxconf[tmp], NULL);
833 if (err)
834 goto drop;
835 }
836 } else {
837 pr_debug("(%s): destination address stateless compression\n",
838 __func__);
839 err = lowpan_uncompress_addr(skb, &hdr.daddr, lowpan_llprefix,
840 lowpan_unc_llconf[tmp], skb->data);
841 if (err)
842 goto drop;
843 }
844
845 /* TODO: UDP header parse */
846
847 /* Not fragmented package */
848 hdr.payload_len = htons(skb->len);
849
850 pr_debug("(%s): skb headroom size = %d, data length = %d\n", __func__,
851 skb_headroom(skb), skb->len);
852
853 pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
854 "nexthdr = 0x%02x\n\thop_lim = %d\n", __func__, hdr.version,
855 ntohs(hdr.payload_len), hdr.nexthdr, hdr.hop_limit);
856
857 lowpan_raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
858 sizeof(hdr));
859 return lowpan_skb_deliver(skb, &hdr);
719269af 860
861unlock_and_drop:
862 spin_unlock(&flist_lock);
44331fe2 863drop:
90d0963d 864 kfree_skb(skb);
44331fe2
AS
865 return -EINVAL;
866}
867
868static int lowpan_set_address(struct net_device *dev, void *p)
869{
870 struct sockaddr *sa = p;
871
872 if (netif_running(dev))
873 return -EBUSY;
874
875 /* TODO: validate addr */
876 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
877
878 return 0;
879}
880
719269af 881static int lowpan_get_mac_header_length(struct sk_buff *skb)
882{
883 /*
884 * Currently long addressing mode is supported only, so the overall
885 * header size is 21:
886 * FC SeqNum DPAN DA SA Sec
887 * 2 + 1 + 2 + 8 + 8 + 0 = 21
888 */
889 return 21;
890}
891
892static int
893lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
894 int mlen, int plen, int offset)
895{
896 struct sk_buff *frag;
897 int hlen, ret;
898
899 /* if payload length is zero, therefore it's a first fragment */
900 hlen = (plen == 0 ? LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE);
901
902 lowpan_raw_dump_inline(__func__, "6lowpan fragment header", head, hlen);
903
904 frag = dev_alloc_skb(hlen + mlen + plen + IEEE802154_MFR_SIZE);
905 if (!frag)
906 return -ENOMEM;
907
908 frag->priority = skb->priority;
909 frag->dev = skb->dev;
910
911 /* copy header, MFR and payload */
912 memcpy(skb_put(frag, mlen), skb->data, mlen);
913 memcpy(skb_put(frag, hlen), head, hlen);
914
915 if (plen)
916 skb_copy_from_linear_data_offset(skb, offset + mlen,
917 skb_put(frag, plen), plen);
918
919 lowpan_raw_dump_table(__func__, " raw fragment dump", frag->data,
920 frag->len);
921
922 ret = dev_queue_xmit(frag);
923
924 if (ret < 0)
925 dev_kfree_skb(frag);
926
927 return ret;
928}
929
930static int
931lowpan_skb_fragmentation(struct sk_buff *skb)
932{
933 int err, header_length, payload_length, tag, offset = 0;
934 u8 head[5];
935
936 header_length = lowpan_get_mac_header_length(skb);
937 payload_length = skb->len - header_length;
938 tag = fragment_tag++;
939
940 /* first fragment header */
941 head[0] = LOWPAN_DISPATCH_FRAG1 | (payload_length & 0x7);
942 head[1] = (payload_length >> 3) & 0xff;
943 head[2] = tag & 0xff;
944 head[3] = tag >> 8;
945
946 err = lowpan_fragment_xmit(skb, head, header_length, 0, 0);
947
948 /* next fragment header */
949 head[0] &= ~LOWPAN_DISPATCH_FRAG1;
950 head[0] |= LOWPAN_DISPATCH_FRAGN;
951
952 while ((payload_length - offset > 0) && (err >= 0)) {
953 int len = LOWPAN_FRAG_SIZE;
954
955 head[4] = offset / 8;
956
957 if (payload_length - offset < len)
958 len = payload_length - offset;
959
960 err = lowpan_fragment_xmit(skb, head, header_length,
961 len, offset);
962 offset += len;
963 }
964
965 return err;
966}
967
44331fe2
AS
968static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
969{
719269af 970 int err = -1;
44331fe2
AS
971
972 pr_debug("(%s): package xmit\n", __func__);
973
974 skb->dev = lowpan_dev_info(dev)->real_dev;
975 if (skb->dev == NULL) {
976 pr_debug("(%s) ERROR: no real wpan device found\n", __func__);
719269af 977 goto error;
978 }
979
980 if (skb->len <= IEEE802154_MTU) {
44331fe2 981 err = dev_queue_xmit(skb);
719269af 982 goto out;
983 }
984
985 pr_debug("(%s): frame is too big, fragmentation is needed\n",
986 __func__);
987 err = lowpan_skb_fragmentation(skb);
988error:
989 dev_kfree_skb(skb);
990out:
991 if (err < 0)
992 pr_debug("(%s): ERROR: xmit failed\n", __func__);
44331fe2
AS
993
994 return (err < 0 ? NETDEV_TX_BUSY : NETDEV_TX_OK);
995}
996
997static void lowpan_dev_free(struct net_device *dev)
998{
999 dev_put(lowpan_dev_info(dev)->real_dev);
1000 free_netdev(dev);
1001}
1002
1003static struct header_ops lowpan_header_ops = {
1004 .create = lowpan_header_create,
1005};
1006
1007static const struct net_device_ops lowpan_netdev_ops = {
1008 .ndo_start_xmit = lowpan_xmit,
1009 .ndo_set_mac_address = lowpan_set_address,
1010};
1011
1012static void lowpan_setup(struct net_device *dev)
1013{
1014 pr_debug("(%s)\n", __func__);
1015
1016 dev->addr_len = IEEE802154_ADDR_LEN;
1017 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
1018 dev->type = ARPHRD_IEEE802154;
44331fe2
AS
1019 /* Frame Control + Sequence Number + Address fields + Security Header */
1020 dev->hard_header_len = 2 + 1 + 20 + 14;
1021 dev->needed_tailroom = 2; /* FCS */
1022 dev->mtu = 1281;
1023 dev->tx_queue_len = 0;
4d039f68 1024 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
44331fe2
AS
1025 dev->watchdog_timeo = 0;
1026
1027 dev->netdev_ops = &lowpan_netdev_ops;
1028 dev->header_ops = &lowpan_header_ops;
1029 dev->destructor = lowpan_dev_free;
1030}
1031
1032static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
1033{
1034 pr_debug("(%s)\n", __func__);
1035
1036 if (tb[IFLA_ADDRESS]) {
1037 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
1038 return -EINVAL;
1039 }
1040 return 0;
1041}
1042
1043static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
1044 struct packet_type *pt, struct net_device *orig_dev)
1045{
1046 if (!netif_running(dev))
1047 goto drop;
1048
1049 if (dev->type != ARPHRD_IEEE802154)
1050 goto drop;
1051
1052 /* check that it's our buffer */
719269af 1053 switch (skb->data[0] & 0xe0) {
1054 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
1055 case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
1056 case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
44331fe2 1057 lowpan_process_data(skb);
719269af 1058 break;
1059 default:
1060 break;
1061 }
44331fe2
AS
1062
1063 return NET_RX_SUCCESS;
1064
1065drop:
1066 kfree_skb(skb);
1067 return NET_RX_DROP;
1068}
1069
1070static int lowpan_newlink(struct net *src_net, struct net_device *dev,
1071 struct nlattr *tb[], struct nlattr *data[])
1072{
1073 struct net_device *real_dev;
1074 struct lowpan_dev_record *entry;
1075
1076 pr_debug("(%s)\n", __func__);
1077
1078 if (!tb[IFLA_LINK])
1079 return -EINVAL;
1080 /* find and hold real wpan device */
1081 real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1082 if (!real_dev)
1083 return -ENODEV;
1084
1085 lowpan_dev_info(dev)->real_dev = real_dev;
1086 mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
1087
1088 entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
dc00fd44
DC
1089 if (!entry) {
1090 dev_put(real_dev);
1091 lowpan_dev_info(dev)->real_dev = NULL;
44331fe2 1092 return -ENOMEM;
dc00fd44 1093 }
44331fe2
AS
1094
1095 entry->ldev = dev;
1096
1097 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
1098 INIT_LIST_HEAD(&entry->list);
1099 list_add_tail(&entry->list, &lowpan_devices);
1100 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
1101
1102 register_netdevice(dev);
1103
1104 return 0;
1105}
1106
1107static void lowpan_dellink(struct net_device *dev, struct list_head *head)
1108{
1109 struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
1110 struct net_device *real_dev = lowpan_dev->real_dev;
1111 struct lowpan_dev_record *entry;
aec9db35 1112 struct lowpan_dev_record *tmp;
44331fe2
AS
1113
1114 ASSERT_RTNL();
1115
1116 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
aec9db35 1117 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
44331fe2
AS
1118 if (entry->ldev == dev) {
1119 list_del(&entry->list);
1120 kfree(entry);
1121 }
aec9db35 1122 }
44331fe2
AS
1123 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
1124
1125 mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
1126
1127 unregister_netdevice_queue(dev, head);
1128
1129 dev_put(real_dev);
1130}
1131
1132static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
1133 .kind = "lowpan",
1134 .priv_size = sizeof(struct lowpan_dev_info),
1135 .setup = lowpan_setup,
1136 .newlink = lowpan_newlink,
1137 .dellink = lowpan_dellink,
1138 .validate = lowpan_validate,
1139};
1140
1141static inline int __init lowpan_netlink_init(void)
1142{
1143 return rtnl_link_register(&lowpan_link_ops);
1144}
1145
1146static inline void __init lowpan_netlink_fini(void)
1147{
1148 rtnl_link_unregister(&lowpan_link_ops);
1149}
1150
1151static struct packet_type lowpan_packet_type = {
1152 .type = __constant_htons(ETH_P_IEEE802154),
1153 .func = lowpan_rcv,
1154};
1155
1156static int __init lowpan_init_module(void)
1157{
1158 int err = 0;
1159
1160 pr_debug("(%s)\n", __func__);
1161
1162 err = lowpan_netlink_init();
1163 if (err < 0)
1164 goto out;
1165
1166 dev_add_pack(&lowpan_packet_type);
1167out:
1168 return err;
1169}
1170
1171static void __exit lowpan_cleanup_module(void)
1172{
1173 pr_debug("(%s)\n", __func__);
1174
1175 lowpan_netlink_fini();
1176
1177 dev_remove_pack(&lowpan_packet_type);
1178}
1179
1180module_init(lowpan_init_module);
1181module_exit(lowpan_cleanup_module);
1182MODULE_LICENSE("GPL");
1183MODULE_ALIAS_RTNL_LINK("lowpan");
This page took 0.098819 seconds and 5 git commands to generate.