6lowpan: lowpan_uncompress_addr with address_mode
[deliverable/linux.git] / net / ieee802154 / 6lowpan.c
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
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 */
66 static const u8 lowpan_ttl_values[] = {0, 1, 64, 255};
67
68 static LIST_HEAD(lowpan_devices);
69
70 /* private device info */
71 struct lowpan_dev_info {
72 struct net_device *real_dev; /* real WPAN device ptr */
73 struct mutex dev_list_mtx; /* mutex for list ops */
74 unsigned short fragment_tag;
75 };
76
77 struct lowpan_dev_record {
78 struct net_device *ldev;
79 struct list_head list;
80 };
81
82 struct lowpan_fragment {
83 struct sk_buff *skb; /* skb to be assembled */
84 u16 length; /* length to be assemled */
85 u32 bytes_rcv; /* bytes received */
86 u16 tag; /* current fragment tag */
87 struct timer_list timer; /* assembling timer */
88 struct list_head list; /* fragments list */
89 };
90
91 static LIST_HEAD(lowpan_fragments);
92 static DEFINE_SPINLOCK(flist_lock);
93
94 static inline struct
95 lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
96 {
97 return netdev_priv(dev);
98 }
99
100 static inline void lowpan_address_flip(u8 *src, u8 *dest)
101 {
102 int i;
103 for (i = 0; i < IEEE802154_ADDR_LEN; i++)
104 (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i];
105 }
106
107 /* list of all 6lowpan devices, uses for package delivering */
108 /* print data in line */
109 static inline void lowpan_raw_dump_inline(const char *caller, char *msg,
110 unsigned char *buf, int len)
111 {
112 #ifdef DEBUG
113 if (msg)
114 pr_debug("(%s) %s: ", caller, msg);
115 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE,
116 16, 1, buf, len, false);
117 #endif /* DEBUG */
118 }
119
120 /*
121 * print data in a table format:
122 *
123 * addr: xx xx xx xx xx xx
124 * addr: xx xx xx xx xx xx
125 * ...
126 */
127 static inline void lowpan_raw_dump_table(const char *caller, char *msg,
128 unsigned char *buf, int len)
129 {
130 #ifdef DEBUG
131 if (msg)
132 pr_debug("(%s) %s:\n", caller, msg);
133 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET,
134 16, 1, buf, len, false);
135 #endif /* DEBUG */
136 }
137
138 static u8
139 lowpan_compress_addr_64(u8 **hc06_ptr, u8 shift, const struct in6_addr *ipaddr,
140 const unsigned char *lladdr)
141 {
142 u8 val = 0;
143
144 if (is_addr_mac_addr_based(ipaddr, lladdr))
145 val = 3; /* 0-bits */
146 else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
147 /* compress IID to 16 bits xxxx::XXXX */
148 memcpy(*hc06_ptr, &ipaddr->s6_addr16[7], 2);
149 *hc06_ptr += 2;
150 val = 2; /* 16-bits */
151 } else {
152 /* do not compress IID => xxxx::IID */
153 memcpy(*hc06_ptr, &ipaddr->s6_addr16[4], 8);
154 *hc06_ptr += 8;
155 val = 1; /* 64-bits */
156 }
157
158 return rol8(val, shift);
159 }
160
161 /*
162 * Uncompress address function for source and
163 * destination address(non-multicast).
164 *
165 * address_mode is sam value or dam value.
166 */
167 static int
168 lowpan_uncompress_addr(struct sk_buff *skb,
169 struct in6_addr *ipaddr,
170 const u8 address_mode,
171 const struct ieee802154_addr *lladdr)
172 {
173 bool fail;
174
175 switch (address_mode) {
176 case LOWPAN_IPHC_ADDR_00:
177 /* for global link addresses */
178 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
179 break;
180 case LOWPAN_IPHC_ADDR_01:
181 /* fe:80::XXXX:XXXX:XXXX:XXXX */
182 ipaddr->s6_addr[0] = 0xFE;
183 ipaddr->s6_addr[1] = 0x80;
184 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8);
185 break;
186 case LOWPAN_IPHC_ADDR_02:
187 /* fe:80::ff:fe00:XXXX */
188 ipaddr->s6_addr[0] = 0xFE;
189 ipaddr->s6_addr[1] = 0x80;
190 ipaddr->s6_addr[11] = 0xFF;
191 ipaddr->s6_addr[12] = 0xFE;
192 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2);
193 break;
194 case LOWPAN_IPHC_ADDR_03:
195 fail = false;
196 switch (lladdr->addr_type) {
197 case IEEE802154_ADDR_LONG:
198 /* fe:80::XXXX:XXXX:XXXX:XXXX
199 * \_________________/
200 * hwaddr
201 */
202 ipaddr->s6_addr[0] = 0xFE;
203 ipaddr->s6_addr[1] = 0x80;
204 memcpy(&ipaddr->s6_addr[8], lladdr->hwaddr,
205 IEEE802154_ADDR_LEN);
206 /* second bit-flip (Universe/Local)
207 * is done according RFC2464
208 */
209 ipaddr->s6_addr[8] ^= 0x02;
210 break;
211 case IEEE802154_ADDR_SHORT:
212 /* fe:80::ff:fe00:XXXX
213 * \__/
214 * short_addr
215 *
216 * Universe/Local bit is zero.
217 */
218 ipaddr->s6_addr[0] = 0xFE;
219 ipaddr->s6_addr[1] = 0x80;
220 ipaddr->s6_addr[11] = 0xFF;
221 ipaddr->s6_addr[12] = 0xFE;
222 ipaddr->s6_addr16[7] = htons(lladdr->short_addr);
223 break;
224 default:
225 pr_debug("Invalid addr_type set\n");
226 return -EINVAL;
227 }
228 break;
229 default:
230 pr_debug("Invalid address mode value: 0x%x\n", address_mode);
231 return -EINVAL;
232 }
233
234 if (fail) {
235 pr_debug("Failed to fetch skb data\n");
236 return -EIO;
237 }
238
239 lowpan_raw_dump_inline(NULL, "Reconstructed ipv6 addr is:\n",
240 ipaddr->s6_addr, 16);
241
242 return 0;
243 }
244
245 /* Uncompress function for multicast destination address,
246 * when M bit is set.
247 */
248 static int
249 lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
250 struct in6_addr *ipaddr,
251 const u8 dam)
252 {
253 bool fail;
254
255 switch (dam) {
256 case LOWPAN_IPHC_DAM_00:
257 /* 00: 128 bits. The full address
258 * is carried in-line.
259 */
260 fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
261 break;
262 case LOWPAN_IPHC_DAM_01:
263 /* 01: 48 bits. The address takes
264 * the form ffXX::00XX:XXXX:XXXX.
265 */
266 ipaddr->s6_addr[0] = 0xFF;
267 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
268 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
269 break;
270 case LOWPAN_IPHC_DAM_10:
271 /* 10: 32 bits. The address takes
272 * the form ffXX::00XX:XXXX.
273 */
274 ipaddr->s6_addr[0] = 0xFF;
275 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
276 fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
277 break;
278 case LOWPAN_IPHC_DAM_11:
279 /* 11: 8 bits. The address takes
280 * the form ff02::00XX.
281 */
282 ipaddr->s6_addr[0] = 0xFF;
283 ipaddr->s6_addr[1] = 0x02;
284 fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
285 break;
286 default:
287 pr_debug("DAM value has a wrong value: 0x%x\n", dam);
288 return -EINVAL;
289 }
290
291 if (fail) {
292 pr_debug("Failed to fetch skb data\n");
293 return -EIO;
294 }
295
296 lowpan_raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is:\n",
297 ipaddr->s6_addr, 16);
298
299 return 0;
300 }
301
302 static void
303 lowpan_compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
304 {
305 struct udphdr *uh = udp_hdr(skb);
306
307 if (((uh->source & LOWPAN_NHC_UDP_4BIT_MASK) ==
308 LOWPAN_NHC_UDP_4BIT_PORT) &&
309 ((uh->dest & LOWPAN_NHC_UDP_4BIT_MASK) ==
310 LOWPAN_NHC_UDP_4BIT_PORT)) {
311 pr_debug("UDP header: both ports compression to 4 bits\n");
312 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_11;
313 **(hc06_ptr + 1) = /* subtraction is faster */
314 (u8)((uh->dest - LOWPAN_NHC_UDP_4BIT_PORT) +
315 ((uh->source & LOWPAN_NHC_UDP_4BIT_PORT) << 4));
316 *hc06_ptr += 2;
317 } else if ((uh->dest & LOWPAN_NHC_UDP_8BIT_MASK) ==
318 LOWPAN_NHC_UDP_8BIT_PORT) {
319 pr_debug("UDP header: remove 8 bits of dest\n");
320 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_01;
321 memcpy(*hc06_ptr + 1, &uh->source, 2);
322 **(hc06_ptr + 3) = (u8)(uh->dest - LOWPAN_NHC_UDP_8BIT_PORT);
323 *hc06_ptr += 4;
324 } else if ((uh->source & LOWPAN_NHC_UDP_8BIT_MASK) ==
325 LOWPAN_NHC_UDP_8BIT_PORT) {
326 pr_debug("UDP header: remove 8 bits of source\n");
327 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_10;
328 memcpy(*hc06_ptr + 1, &uh->dest, 2);
329 **(hc06_ptr + 3) = (u8)(uh->source - LOWPAN_NHC_UDP_8BIT_PORT);
330 *hc06_ptr += 4;
331 } else {
332 pr_debug("UDP header: can't compress\n");
333 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_00;
334 memcpy(*hc06_ptr + 1, &uh->source, 2);
335 memcpy(*hc06_ptr + 3, &uh->dest, 2);
336 *hc06_ptr += 5;
337 }
338
339 /* checksum is always inline */
340 memcpy(*hc06_ptr, &uh->check, 2);
341 *hc06_ptr += 2;
342
343 /* skip the UDP header */
344 skb_pull(skb, sizeof(struct udphdr));
345 }
346
347 static inline int lowpan_fetch_skb_u8(struct sk_buff *skb, u8 *val)
348 {
349 if (unlikely(!pskb_may_pull(skb, 1)))
350 return -EINVAL;
351
352 *val = skb->data[0];
353 skb_pull(skb, 1);
354
355 return 0;
356 }
357
358 static inline int lowpan_fetch_skb_u16(struct sk_buff *skb, u16 *val)
359 {
360 if (unlikely(!pskb_may_pull(skb, 2)))
361 return -EINVAL;
362
363 *val = (skb->data[0] << 8) | skb->data[1];
364 skb_pull(skb, 2);
365
366 return 0;
367 }
368
369 static int
370 lowpan_uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
371 {
372 u8 tmp;
373
374 if (!uh)
375 goto err;
376
377 if (lowpan_fetch_skb_u8(skb, &tmp))
378 goto err;
379
380 if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
381 pr_debug("UDP header uncompression\n");
382 switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
383 case LOWPAN_NHC_UDP_CS_P_00:
384 memcpy(&uh->source, &skb->data[0], 2);
385 memcpy(&uh->dest, &skb->data[2], 2);
386 skb_pull(skb, 4);
387 break;
388 case LOWPAN_NHC_UDP_CS_P_01:
389 memcpy(&uh->source, &skb->data[0], 2);
390 uh->dest =
391 skb->data[2] + LOWPAN_NHC_UDP_8BIT_PORT;
392 skb_pull(skb, 3);
393 break;
394 case LOWPAN_NHC_UDP_CS_P_10:
395 uh->source = skb->data[0] + LOWPAN_NHC_UDP_8BIT_PORT;
396 memcpy(&uh->dest, &skb->data[1], 2);
397 skb_pull(skb, 3);
398 break;
399 case LOWPAN_NHC_UDP_CS_P_11:
400 uh->source =
401 LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] >> 4);
402 uh->dest =
403 LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] & 0x0f);
404 skb_pull(skb, 1);
405 break;
406 default:
407 pr_debug("ERROR: unknown UDP format\n");
408 goto err;
409 break;
410 }
411
412 pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
413 uh->source, uh->dest);
414
415 /* copy checksum */
416 memcpy(&uh->check, &skb->data[0], 2);
417 skb_pull(skb, 2);
418
419 /*
420 * UDP lenght needs to be infered from the lower layers
421 * here, we obtain the hint from the remaining size of the
422 * frame
423 */
424 uh->len = htons(skb->len + sizeof(struct udphdr));
425 pr_debug("uncompressed UDP length: src = %d", uh->len);
426 } else {
427 pr_debug("ERROR: unsupported NH format\n");
428 goto err;
429 }
430
431 return 0;
432 err:
433 return -EINVAL;
434 }
435
436 static int lowpan_header_create(struct sk_buff *skb,
437 struct net_device *dev,
438 unsigned short type, const void *_daddr,
439 const void *_saddr, unsigned int len)
440 {
441 u8 tmp, iphc0, iphc1, *hc06_ptr;
442 struct ipv6hdr *hdr;
443 const u8 *saddr = _saddr;
444 const u8 *daddr = _daddr;
445 u8 head[100];
446 struct ieee802154_addr sa, da;
447
448 /* TODO:
449 * if this package isn't ipv6 one, where should it be routed?
450 */
451 if (type != ETH_P_IPV6)
452 return 0;
453
454 hdr = ipv6_hdr(skb);
455 hc06_ptr = head + 2;
456
457 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
458 "\tnexthdr = 0x%02x\n\thop_lim = %d\n", hdr->version,
459 ntohs(hdr->payload_len), hdr->nexthdr, hdr->hop_limit);
460
461 lowpan_raw_dump_table(__func__, "raw skb network header dump",
462 skb_network_header(skb), sizeof(struct ipv6hdr));
463
464 if (!saddr)
465 saddr = dev->dev_addr;
466
467 lowpan_raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
468
469 /*
470 * As we copy some bit-length fields, in the IPHC encoding bytes,
471 * we sometimes use |=
472 * If the field is 0, and the current bit value in memory is 1,
473 * this does not work. We therefore reset the IPHC encoding here
474 */
475 iphc0 = LOWPAN_DISPATCH_IPHC;
476 iphc1 = 0;
477
478 /* TODO: context lookup */
479
480 lowpan_raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
481
482 /*
483 * Traffic class, flow label
484 * If flow label is 0, compress it. If traffic class is 0, compress it
485 * We have to process both in the same time as the offset of traffic
486 * class depends on the presence of version and flow label
487 */
488
489 /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
490 tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
491 tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
492
493 if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
494 (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
495 /* flow label can be compressed */
496 iphc0 |= LOWPAN_IPHC_FL_C;
497 if ((hdr->priority == 0) &&
498 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
499 /* compress (elide) all */
500 iphc0 |= LOWPAN_IPHC_TC_C;
501 } else {
502 /* compress only the flow label */
503 *hc06_ptr = tmp;
504 hc06_ptr += 1;
505 }
506 } else {
507 /* Flow label cannot be compressed */
508 if ((hdr->priority == 0) &&
509 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
510 /* compress only traffic class */
511 iphc0 |= LOWPAN_IPHC_TC_C;
512 *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
513 memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
514 hc06_ptr += 3;
515 } else {
516 /* compress nothing */
517 memcpy(hc06_ptr, &hdr, 4);
518 /* replace the top byte with new ECN | DSCP format */
519 *hc06_ptr = tmp;
520 hc06_ptr += 4;
521 }
522 }
523
524 /* NOTE: payload length is always compressed */
525
526 /* Next Header is compress if UDP */
527 if (hdr->nexthdr == UIP_PROTO_UDP)
528 iphc0 |= LOWPAN_IPHC_NH_C;
529
530 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
531 *hc06_ptr = hdr->nexthdr;
532 hc06_ptr += 1;
533 }
534
535 /*
536 * Hop limit
537 * if 1: compress, encoding is 01
538 * if 64: compress, encoding is 10
539 * if 255: compress, encoding is 11
540 * else do not compress
541 */
542 switch (hdr->hop_limit) {
543 case 1:
544 iphc0 |= LOWPAN_IPHC_TTL_1;
545 break;
546 case 64:
547 iphc0 |= LOWPAN_IPHC_TTL_64;
548 break;
549 case 255:
550 iphc0 |= LOWPAN_IPHC_TTL_255;
551 break;
552 default:
553 *hc06_ptr = hdr->hop_limit;
554 hc06_ptr += 1;
555 break;
556 }
557
558 /* source address compression */
559 if (is_addr_unspecified(&hdr->saddr)) {
560 pr_debug("source address is unspecified, setting SAC\n");
561 iphc1 |= LOWPAN_IPHC_SAC;
562 /* TODO: context lookup */
563 } else if (is_addr_link_local(&hdr->saddr)) {
564 pr_debug("source address is link-local\n");
565 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
566 LOWPAN_IPHC_SAM_BIT, &hdr->saddr, saddr);
567 } else {
568 pr_debug("send the full source address\n");
569 memcpy(hc06_ptr, &hdr->saddr.s6_addr16[0], 16);
570 hc06_ptr += 16;
571 }
572
573 /* destination address compression */
574 if (is_addr_mcast(&hdr->daddr)) {
575 pr_debug("destination address is multicast: ");
576 iphc1 |= LOWPAN_IPHC_M;
577 if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
578 pr_debug("compressed to 1 octet\n");
579 iphc1 |= LOWPAN_IPHC_DAM_11;
580 /* use last byte */
581 *hc06_ptr = hdr->daddr.s6_addr[15];
582 hc06_ptr += 1;
583 } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
584 pr_debug("compressed to 4 octets\n");
585 iphc1 |= LOWPAN_IPHC_DAM_10;
586 /* second byte + the last three */
587 *hc06_ptr = hdr->daddr.s6_addr[1];
588 memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[13], 3);
589 hc06_ptr += 4;
590 } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
591 pr_debug("compressed to 6 octets\n");
592 iphc1 |= LOWPAN_IPHC_DAM_01;
593 /* second byte + the last five */
594 *hc06_ptr = hdr->daddr.s6_addr[1];
595 memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[11], 5);
596 hc06_ptr += 6;
597 } else {
598 pr_debug("using full address\n");
599 iphc1 |= LOWPAN_IPHC_DAM_00;
600 memcpy(hc06_ptr, &hdr->daddr.s6_addr[0], 16);
601 hc06_ptr += 16;
602 }
603 } else {
604 /* TODO: context lookup */
605 if (is_addr_link_local(&hdr->daddr)) {
606 pr_debug("dest address is unicast and link-local\n");
607 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
608 LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
609 } else {
610 pr_debug("dest address is unicast: using full one\n");
611 memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
612 hc06_ptr += 16;
613 }
614 }
615
616 /* UDP header compression */
617 if (hdr->nexthdr == UIP_PROTO_UDP)
618 lowpan_compress_udp_header(&hc06_ptr, skb);
619
620 head[0] = iphc0;
621 head[1] = iphc1;
622
623 skb_pull(skb, sizeof(struct ipv6hdr));
624 memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
625
626 lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
627 skb->len);
628
629 /*
630 * NOTE1: I'm still unsure about the fact that compression and WPAN
631 * header are created here and not later in the xmit. So wait for
632 * an opinion of net maintainers.
633 */
634 /*
635 * NOTE2: to be absolutely correct, we must derive PANid information
636 * from MAC subif of the 'dev' and 'real_dev' network devices, but
637 * this isn't implemented in mainline yet, so currently we assign 0xff
638 */
639 {
640 mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
641 mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
642
643 /* prepare wpan address data */
644 sa.addr_type = IEEE802154_ADDR_LONG;
645 sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
646
647 memcpy(&(sa.hwaddr), saddr, 8);
648 /* intra-PAN communications */
649 da.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
650
651 /*
652 * if the destination address is the broadcast address, use the
653 * corresponding short address
654 */
655 if (lowpan_is_addr_broadcast(daddr)) {
656 da.addr_type = IEEE802154_ADDR_SHORT;
657 da.short_addr = IEEE802154_ADDR_BROADCAST;
658 } else {
659 da.addr_type = IEEE802154_ADDR_LONG;
660 memcpy(&(da.hwaddr), daddr, IEEE802154_ADDR_LEN);
661
662 /* request acknowledgment */
663 mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ;
664 }
665
666 return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
667 type, (void *)&da, (void *)&sa, skb->len);
668 }
669 }
670
671 static int lowpan_give_skb_to_devices(struct sk_buff *skb)
672 {
673 struct lowpan_dev_record *entry;
674 struct sk_buff *skb_cp;
675 int stat = NET_RX_SUCCESS;
676
677 rcu_read_lock();
678 list_for_each_entry_rcu(entry, &lowpan_devices, list)
679 if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) {
680 skb_cp = skb_copy(skb, GFP_ATOMIC);
681 if (!skb_cp) {
682 stat = -ENOMEM;
683 break;
684 }
685
686 skb_cp->dev = entry->ldev;
687 stat = netif_rx(skb_cp);
688 }
689 rcu_read_unlock();
690
691 return stat;
692 }
693
694 static int lowpan_skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr)
695 {
696 struct sk_buff *new;
697 int stat = NET_RX_SUCCESS;
698
699 new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
700 GFP_ATOMIC);
701 kfree_skb(skb);
702
703 if (!new)
704 return -ENOMEM;
705
706 skb_push(new, sizeof(struct ipv6hdr));
707 skb_reset_network_header(new);
708 skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
709
710 new->protocol = htons(ETH_P_IPV6);
711 new->pkt_type = PACKET_HOST;
712
713 stat = lowpan_give_skb_to_devices(new);
714
715 kfree_skb(new);
716
717 return stat;
718 }
719
720 static void lowpan_fragment_timer_expired(unsigned long entry_addr)
721 {
722 struct lowpan_fragment *entry = (struct lowpan_fragment *)entry_addr;
723
724 pr_debug("timer expired for frame with tag %d\n", entry->tag);
725
726 list_del(&entry->list);
727 dev_kfree_skb(entry->skb);
728 kfree(entry);
729 }
730
731 static struct lowpan_fragment *
732 lowpan_alloc_new_frame(struct sk_buff *skb, u16 len, u16 tag)
733 {
734 struct lowpan_fragment *frame;
735
736 frame = kzalloc(sizeof(struct lowpan_fragment),
737 GFP_ATOMIC);
738 if (!frame)
739 goto frame_err;
740
741 INIT_LIST_HEAD(&frame->list);
742
743 frame->length = len;
744 frame->tag = tag;
745
746 /* allocate buffer for frame assembling */
747 frame->skb = netdev_alloc_skb_ip_align(skb->dev, frame->length +
748 sizeof(struct ipv6hdr));
749
750 if (!frame->skb)
751 goto skb_err;
752
753 frame->skb->priority = skb->priority;
754 frame->skb->dev = skb->dev;
755
756 /* reserve headroom for uncompressed ipv6 header */
757 skb_reserve(frame->skb, sizeof(struct ipv6hdr));
758 skb_put(frame->skb, frame->length);
759
760 /* copy the first control block to keep a
761 * trace of the link-layer addresses in case
762 * of a link-local compressed address
763 */
764 memcpy(frame->skb->cb, skb->cb, sizeof(skb->cb));
765
766 init_timer(&frame->timer);
767 /* time out is the same as for ipv6 - 60 sec */
768 frame->timer.expires = jiffies + LOWPAN_FRAG_TIMEOUT;
769 frame->timer.data = (unsigned long)frame;
770 frame->timer.function = lowpan_fragment_timer_expired;
771
772 add_timer(&frame->timer);
773
774 list_add_tail(&frame->list, &lowpan_fragments);
775
776 return frame;
777
778 skb_err:
779 kfree(frame);
780 frame_err:
781 return NULL;
782 }
783
784 static int
785 lowpan_process_data(struct sk_buff *skb)
786 {
787 struct ipv6hdr hdr = {};
788 u8 tmp, iphc0, iphc1, num_context = 0;
789 const struct ieee802154_addr *_saddr, *_daddr;
790 int err;
791
792 lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
793 skb->len);
794 /* at least two bytes will be used for the encoding */
795 if (skb->len < 2)
796 goto drop;
797
798 if (lowpan_fetch_skb_u8(skb, &iphc0))
799 goto drop;
800
801 /* fragments assembling */
802 switch (iphc0 & LOWPAN_DISPATCH_MASK) {
803 case LOWPAN_DISPATCH_FRAG1:
804 case LOWPAN_DISPATCH_FRAGN:
805 {
806 struct lowpan_fragment *frame;
807 /* slen stores the rightmost 8 bits of the 11 bits length */
808 u8 slen, offset = 0;
809 u16 len, tag;
810 bool found = false;
811
812 if (lowpan_fetch_skb_u8(skb, &slen) || /* frame length */
813 lowpan_fetch_skb_u16(skb, &tag)) /* fragment tag */
814 goto drop;
815
816 /* adds the 3 MSB to the 8 LSB to retrieve the 11 bits length */
817 len = ((iphc0 & 7) << 8) | slen;
818
819 if ((iphc0 & LOWPAN_DISPATCH_MASK) == LOWPAN_DISPATCH_FRAG1) {
820 pr_debug("%s received a FRAG1 packet (tag: %d, "
821 "size of the entire IP packet: %d)",
822 __func__, tag, len);
823 } else { /* FRAGN */
824 if (lowpan_fetch_skb_u8(skb, &offset))
825 goto unlock_and_drop;
826 pr_debug("%s received a FRAGN packet (tag: %d, "
827 "size of the entire IP packet: %d, "
828 "offset: %d)", __func__, tag, len, offset * 8);
829 }
830
831 /*
832 * check if frame assembling with the same tag is
833 * already in progress
834 */
835 spin_lock_bh(&flist_lock);
836
837 list_for_each_entry(frame, &lowpan_fragments, list)
838 if (frame->tag == tag) {
839 found = true;
840 break;
841 }
842
843 /* alloc new frame structure */
844 if (!found) {
845 pr_debug("%s first fragment received for tag %d, "
846 "begin packet reassembly", __func__, tag);
847 frame = lowpan_alloc_new_frame(skb, len, tag);
848 if (!frame)
849 goto unlock_and_drop;
850 }
851
852 /* if payload fits buffer, copy it */
853 if (likely((offset * 8 + skb->len) <= frame->length))
854 skb_copy_to_linear_data_offset(frame->skb, offset * 8,
855 skb->data, skb->len);
856 else
857 goto unlock_and_drop;
858
859 frame->bytes_rcv += skb->len;
860
861 /* frame assembling complete */
862 if ((frame->bytes_rcv == frame->length) &&
863 frame->timer.expires > jiffies) {
864 /* if timer haven't expired - first of all delete it */
865 del_timer_sync(&frame->timer);
866 list_del(&frame->list);
867 spin_unlock_bh(&flist_lock);
868
869 pr_debug("%s successfully reassembled fragment "
870 "(tag %d)", __func__, tag);
871
872 dev_kfree_skb(skb);
873 skb = frame->skb;
874 kfree(frame);
875
876 if (lowpan_fetch_skb_u8(skb, &iphc0))
877 goto drop;
878
879 break;
880 }
881 spin_unlock_bh(&flist_lock);
882
883 return kfree_skb(skb), 0;
884 }
885 default:
886 break;
887 }
888
889 if (lowpan_fetch_skb_u8(skb, &iphc1))
890 goto drop;
891
892 _saddr = &mac_cb(skb)->sa;
893 _daddr = &mac_cb(skb)->da;
894
895 pr_debug("iphc0 = %02x, iphc1 = %02x\n", iphc0, iphc1);
896
897 /* another if the CID flag is set */
898 if (iphc1 & LOWPAN_IPHC_CID) {
899 pr_debug("CID flag is set, increase header with one\n");
900 if (lowpan_fetch_skb_u8(skb, &num_context))
901 goto drop;
902 }
903
904 hdr.version = 6;
905
906 /* Traffic Class and Flow Label */
907 switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
908 /*
909 * Traffic Class and FLow Label carried in-line
910 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
911 */
912 case 0: /* 00b */
913 if (lowpan_fetch_skb_u8(skb, &tmp))
914 goto drop;
915
916 memcpy(&hdr.flow_lbl, &skb->data[0], 3);
917 skb_pull(skb, 3);
918 hdr.priority = ((tmp >> 2) & 0x0f);
919 hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
920 (hdr.flow_lbl[0] & 0x0f);
921 break;
922 /*
923 * Traffic class carried in-line
924 * ECN + DSCP (1 byte), Flow Label is elided
925 */
926 case 1: /* 10b */
927 if (lowpan_fetch_skb_u8(skb, &tmp))
928 goto drop;
929
930 hdr.priority = ((tmp >> 2) & 0x0f);
931 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
932 break;
933 /*
934 * Flow Label carried in-line
935 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
936 */
937 case 2: /* 01b */
938 if (lowpan_fetch_skb_u8(skb, &tmp))
939 goto drop;
940
941 hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
942 memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
943 skb_pull(skb, 2);
944 break;
945 /* Traffic Class and Flow Label are elided */
946 case 3: /* 11b */
947 break;
948 default:
949 break;
950 }
951
952 /* Next Header */
953 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
954 /* Next header is carried inline */
955 if (lowpan_fetch_skb_u8(skb, &(hdr.nexthdr)))
956 goto drop;
957
958 pr_debug("NH flag is set, next header carried inline: %02x\n",
959 hdr.nexthdr);
960 }
961
962 /* Hop Limit */
963 if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
964 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
965 else {
966 if (lowpan_fetch_skb_u8(skb, &(hdr.hop_limit)))
967 goto drop;
968 }
969
970 /* Extract SAM to the tmp variable */
971 tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
972
973 /* Source address uncompression */
974 pr_debug("source address stateless compression\n");
975 err = lowpan_uncompress_addr(skb, &hdr.saddr, tmp, _saddr);
976 if (err)
977 goto drop;
978
979 /* Extract DAM to the tmp variable */
980 tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
981
982 /* check for Multicast Compression */
983 if (iphc1 & LOWPAN_IPHC_M) {
984 if (iphc1 & LOWPAN_IPHC_DAC) {
985 pr_debug("dest: context-based mcast compression\n");
986 /* TODO: implement this */
987 } else {
988 err = lowpan_uncompress_multicast_daddr(
989 skb, &hdr.daddr, tmp);
990 if (err)
991 goto drop;
992 }
993 } else {
994 pr_debug("dest: stateless compression\n");
995 err = lowpan_uncompress_addr(skb, &hdr.daddr, tmp, _daddr);
996 if (err)
997 goto drop;
998 }
999
1000 /* UDP data uncompression */
1001 if (iphc0 & LOWPAN_IPHC_NH_C) {
1002 struct udphdr uh;
1003 struct sk_buff *new;
1004 if (lowpan_uncompress_udp_header(skb, &uh))
1005 goto drop;
1006
1007 /*
1008 * replace the compressed UDP head by the uncompressed UDP
1009 * header
1010 */
1011 new = skb_copy_expand(skb, sizeof(struct udphdr),
1012 skb_tailroom(skb), GFP_ATOMIC);
1013 kfree_skb(skb);
1014
1015 if (!new)
1016 return -ENOMEM;
1017
1018 skb = new;
1019
1020 skb_push(skb, sizeof(struct udphdr));
1021 skb_reset_transport_header(skb);
1022 skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
1023
1024 lowpan_raw_dump_table(__func__, "raw UDP header dump",
1025 (u8 *)&uh, sizeof(uh));
1026
1027 hdr.nexthdr = UIP_PROTO_UDP;
1028 }
1029
1030 /* Not fragmented package */
1031 hdr.payload_len = htons(skb->len);
1032
1033 pr_debug("skb headroom size = %d, data length = %d\n",
1034 skb_headroom(skb), skb->len);
1035
1036 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
1037 "nexthdr = 0x%02x\n\thop_lim = %d\n", hdr.version,
1038 ntohs(hdr.payload_len), hdr.nexthdr, hdr.hop_limit);
1039
1040 lowpan_raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
1041 sizeof(hdr));
1042 return lowpan_skb_deliver(skb, &hdr);
1043
1044 unlock_and_drop:
1045 spin_unlock_bh(&flist_lock);
1046 drop:
1047 kfree_skb(skb);
1048 return -EINVAL;
1049 }
1050
1051 static int lowpan_set_address(struct net_device *dev, void *p)
1052 {
1053 struct sockaddr *sa = p;
1054
1055 if (netif_running(dev))
1056 return -EBUSY;
1057
1058 /* TODO: validate addr */
1059 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
1060
1061 return 0;
1062 }
1063
1064 static int lowpan_get_mac_header_length(struct sk_buff *skb)
1065 {
1066 /*
1067 * Currently long addressing mode is supported only, so the overall
1068 * header size is 21:
1069 * FC SeqNum DPAN DA SA Sec
1070 * 2 + 1 + 2 + 8 + 8 + 0 = 21
1071 */
1072 return 21;
1073 }
1074
1075 static int
1076 lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
1077 int mlen, int plen, int offset, int type)
1078 {
1079 struct sk_buff *frag;
1080 int hlen, ret;
1081
1082 hlen = (type == LOWPAN_DISPATCH_FRAG1) ?
1083 LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE;
1084
1085 lowpan_raw_dump_inline(__func__, "6lowpan fragment header", head, hlen);
1086
1087 frag = dev_alloc_skb(hlen + mlen + plen + IEEE802154_MFR_SIZE);
1088 if (!frag)
1089 return -ENOMEM;
1090
1091 frag->priority = skb->priority;
1092 frag->dev = skb->dev;
1093
1094 /* copy header, MFR and payload */
1095 memcpy(skb_put(frag, mlen), skb->data, mlen);
1096 memcpy(skb_put(frag, hlen), head, hlen);
1097
1098 if (plen)
1099 skb_copy_from_linear_data_offset(skb, offset + mlen,
1100 skb_put(frag, plen), plen);
1101
1102 lowpan_raw_dump_table(__func__, " raw fragment dump", frag->data,
1103 frag->len);
1104
1105 ret = dev_queue_xmit(frag);
1106
1107 return ret;
1108 }
1109
1110 static int
1111 lowpan_skb_fragmentation(struct sk_buff *skb, struct net_device *dev)
1112 {
1113 int err, header_length, payload_length, tag, offset = 0;
1114 u8 head[5];
1115
1116 header_length = lowpan_get_mac_header_length(skb);
1117 payload_length = skb->len - header_length;
1118 tag = lowpan_dev_info(dev)->fragment_tag++;
1119
1120 /* first fragment header */
1121 head[0] = LOWPAN_DISPATCH_FRAG1 | ((payload_length >> 8) & 0x7);
1122 head[1] = payload_length & 0xff;
1123 head[2] = tag >> 8;
1124 head[3] = tag & 0xff;
1125
1126 err = lowpan_fragment_xmit(skb, head, header_length, LOWPAN_FRAG_SIZE,
1127 0, LOWPAN_DISPATCH_FRAG1);
1128
1129 if (err) {
1130 pr_debug("%s unable to send FRAG1 packet (tag: %d)",
1131 __func__, tag);
1132 goto exit;
1133 }
1134
1135 offset = LOWPAN_FRAG_SIZE;
1136
1137 /* next fragment header */
1138 head[0] &= ~LOWPAN_DISPATCH_FRAG1;
1139 head[0] |= LOWPAN_DISPATCH_FRAGN;
1140
1141 while ((payload_length - offset > 0) && (err >= 0)) {
1142 int len = LOWPAN_FRAG_SIZE;
1143
1144 head[4] = offset / 8;
1145
1146 if (payload_length - offset < len)
1147 len = payload_length - offset;
1148
1149 err = lowpan_fragment_xmit(skb, head, header_length,
1150 len, offset, LOWPAN_DISPATCH_FRAGN);
1151 if (err) {
1152 pr_debug("%s unable to send a subsequent FRAGN packet "
1153 "(tag: %d, offset: %d", __func__, tag, offset);
1154 goto exit;
1155 }
1156
1157 offset += len;
1158 }
1159
1160 exit:
1161 return err;
1162 }
1163
1164 static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
1165 {
1166 int err = -1;
1167
1168 pr_debug("package xmit\n");
1169
1170 skb->dev = lowpan_dev_info(dev)->real_dev;
1171 if (skb->dev == NULL) {
1172 pr_debug("ERROR: no real wpan device found\n");
1173 goto error;
1174 }
1175
1176 /* Send directly if less than the MTU minus the 2 checksum bytes. */
1177 if (skb->len <= IEEE802154_MTU - IEEE802154_MFR_SIZE) {
1178 err = dev_queue_xmit(skb);
1179 goto out;
1180 }
1181
1182 pr_debug("frame is too big, fragmentation is needed\n");
1183 err = lowpan_skb_fragmentation(skb, dev);
1184 error:
1185 dev_kfree_skb(skb);
1186 out:
1187 if (err)
1188 pr_debug("ERROR: xmit failed\n");
1189
1190 return (err < 0) ? NET_XMIT_DROP : err;
1191 }
1192
1193 static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
1194 {
1195 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
1196 return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
1197 }
1198
1199 static u16 lowpan_get_pan_id(const struct net_device *dev)
1200 {
1201 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
1202 return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
1203 }
1204
1205 static u16 lowpan_get_short_addr(const struct net_device *dev)
1206 {
1207 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
1208 return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
1209 }
1210
1211 static u8 lowpan_get_dsn(const struct net_device *dev)
1212 {
1213 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
1214 return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev);
1215 }
1216
1217 static struct header_ops lowpan_header_ops = {
1218 .create = lowpan_header_create,
1219 };
1220
1221 static const struct net_device_ops lowpan_netdev_ops = {
1222 .ndo_start_xmit = lowpan_xmit,
1223 .ndo_set_mac_address = lowpan_set_address,
1224 };
1225
1226 static struct ieee802154_mlme_ops lowpan_mlme = {
1227 .get_pan_id = lowpan_get_pan_id,
1228 .get_phy = lowpan_get_phy,
1229 .get_short_addr = lowpan_get_short_addr,
1230 .get_dsn = lowpan_get_dsn,
1231 };
1232
1233 static void lowpan_setup(struct net_device *dev)
1234 {
1235 dev->addr_len = IEEE802154_ADDR_LEN;
1236 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
1237 dev->type = ARPHRD_IEEE802154;
1238 /* Frame Control + Sequence Number + Address fields + Security Header */
1239 dev->hard_header_len = 2 + 1 + 20 + 14;
1240 dev->needed_tailroom = 2; /* FCS */
1241 dev->mtu = 1281;
1242 dev->tx_queue_len = 0;
1243 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
1244 dev->watchdog_timeo = 0;
1245
1246 dev->netdev_ops = &lowpan_netdev_ops;
1247 dev->header_ops = &lowpan_header_ops;
1248 dev->ml_priv = &lowpan_mlme;
1249 dev->destructor = free_netdev;
1250 }
1251
1252 static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
1253 {
1254 if (tb[IFLA_ADDRESS]) {
1255 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
1256 return -EINVAL;
1257 }
1258 return 0;
1259 }
1260
1261 static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
1262 struct packet_type *pt, struct net_device *orig_dev)
1263 {
1264 struct sk_buff *local_skb;
1265
1266 if (!netif_running(dev))
1267 goto drop;
1268
1269 if (dev->type != ARPHRD_IEEE802154)
1270 goto drop;
1271
1272 /* check that it's our buffer */
1273 if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
1274 /* Copy the packet so that the IPv6 header is
1275 * properly aligned.
1276 */
1277 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
1278 skb_tailroom(skb), GFP_ATOMIC);
1279 if (!local_skb)
1280 goto drop;
1281
1282 local_skb->protocol = htons(ETH_P_IPV6);
1283 local_skb->pkt_type = PACKET_HOST;
1284
1285 /* Pull off the 1-byte of 6lowpan header. */
1286 skb_pull(local_skb, 1);
1287 skb_reset_network_header(local_skb);
1288 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
1289
1290 lowpan_give_skb_to_devices(local_skb);
1291
1292 kfree_skb(local_skb);
1293 kfree_skb(skb);
1294 } else {
1295 switch (skb->data[0] & 0xe0) {
1296 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
1297 case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
1298 case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
1299 local_skb = skb_clone(skb, GFP_ATOMIC);
1300 if (!local_skb)
1301 goto drop;
1302 lowpan_process_data(local_skb);
1303
1304 kfree_skb(skb);
1305 break;
1306 default:
1307 break;
1308 }
1309 }
1310
1311 return NET_RX_SUCCESS;
1312
1313 drop:
1314 kfree_skb(skb);
1315 return NET_RX_DROP;
1316 }
1317
1318 static int lowpan_newlink(struct net *src_net, struct net_device *dev,
1319 struct nlattr *tb[], struct nlattr *data[])
1320 {
1321 struct net_device *real_dev;
1322 struct lowpan_dev_record *entry;
1323
1324 pr_debug("adding new link\n");
1325
1326 if (!tb[IFLA_LINK])
1327 return -EINVAL;
1328 /* find and hold real wpan device */
1329 real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1330 if (!real_dev)
1331 return -ENODEV;
1332
1333 lowpan_dev_info(dev)->real_dev = real_dev;
1334 lowpan_dev_info(dev)->fragment_tag = 0;
1335 mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
1336
1337 entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
1338 if (!entry) {
1339 dev_put(real_dev);
1340 lowpan_dev_info(dev)->real_dev = NULL;
1341 return -ENOMEM;
1342 }
1343
1344 entry->ldev = dev;
1345
1346 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
1347 INIT_LIST_HEAD(&entry->list);
1348 list_add_tail(&entry->list, &lowpan_devices);
1349 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
1350
1351 register_netdevice(dev);
1352
1353 return 0;
1354 }
1355
1356 static void lowpan_dellink(struct net_device *dev, struct list_head *head)
1357 {
1358 struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
1359 struct net_device *real_dev = lowpan_dev->real_dev;
1360 struct lowpan_dev_record *entry, *tmp;
1361
1362 ASSERT_RTNL();
1363
1364 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
1365 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
1366 if (entry->ldev == dev) {
1367 list_del(&entry->list);
1368 kfree(entry);
1369 }
1370 }
1371 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
1372
1373 mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
1374
1375 unregister_netdevice_queue(dev, head);
1376
1377 dev_put(real_dev);
1378 }
1379
1380 static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
1381 .kind = "lowpan",
1382 .priv_size = sizeof(struct lowpan_dev_info),
1383 .setup = lowpan_setup,
1384 .newlink = lowpan_newlink,
1385 .dellink = lowpan_dellink,
1386 .validate = lowpan_validate,
1387 };
1388
1389 static inline int __init lowpan_netlink_init(void)
1390 {
1391 return rtnl_link_register(&lowpan_link_ops);
1392 }
1393
1394 static inline void lowpan_netlink_fini(void)
1395 {
1396 rtnl_link_unregister(&lowpan_link_ops);
1397 }
1398
1399 static int lowpan_device_event(struct notifier_block *unused,
1400 unsigned long event, void *ptr)
1401 {
1402 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1403 LIST_HEAD(del_list);
1404 struct lowpan_dev_record *entry, *tmp;
1405
1406 if (dev->type != ARPHRD_IEEE802154)
1407 goto out;
1408
1409 if (event == NETDEV_UNREGISTER) {
1410 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
1411 if (lowpan_dev_info(entry->ldev)->real_dev == dev)
1412 lowpan_dellink(entry->ldev, &del_list);
1413 }
1414
1415 unregister_netdevice_many(&del_list);
1416 }
1417
1418 out:
1419 return NOTIFY_DONE;
1420 }
1421
1422 static struct notifier_block lowpan_dev_notifier = {
1423 .notifier_call = lowpan_device_event,
1424 };
1425
1426 static struct packet_type lowpan_packet_type = {
1427 .type = __constant_htons(ETH_P_IEEE802154),
1428 .func = lowpan_rcv,
1429 };
1430
1431 static int __init lowpan_init_module(void)
1432 {
1433 int err = 0;
1434
1435 err = lowpan_netlink_init();
1436 if (err < 0)
1437 goto out;
1438
1439 dev_add_pack(&lowpan_packet_type);
1440
1441 err = register_netdevice_notifier(&lowpan_dev_notifier);
1442 if (err < 0) {
1443 dev_remove_pack(&lowpan_packet_type);
1444 lowpan_netlink_fini();
1445 }
1446 out:
1447 return err;
1448 }
1449
1450 static void __exit lowpan_cleanup_module(void)
1451 {
1452 struct lowpan_fragment *frame, *tframe;
1453
1454 lowpan_netlink_fini();
1455
1456 dev_remove_pack(&lowpan_packet_type);
1457
1458 unregister_netdevice_notifier(&lowpan_dev_notifier);
1459
1460 /* Now 6lowpan packet_type is removed, so no new fragments are
1461 * expected on RX, therefore that's the time to clean incomplete
1462 * fragments.
1463 */
1464 spin_lock_bh(&flist_lock);
1465 list_for_each_entry_safe(frame, tframe, &lowpan_fragments, list) {
1466 del_timer_sync(&frame->timer);
1467 list_del(&frame->list);
1468 dev_kfree_skb(frame->skb);
1469 kfree(frame);
1470 }
1471 spin_unlock_bh(&flist_lock);
1472 }
1473
1474 module_init(lowpan_init_module);
1475 module_exit(lowpan_cleanup_module);
1476 MODULE_LICENSE("GPL");
1477 MODULE_ALIAS_RTNL_LINK("lowpan");
This page took 0.091356 seconds and 6 git commands to generate.