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