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