blob: b45e229bc42c1b0e56c04c452c4b545533e9c100 [file] [log] [blame]
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001/*
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
Alexander Smirnov44331fe2011-08-24 19:34:42 -070053#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>
Danny Kukawkacdf49c22012-02-22 02:36:39 +000058#include <linux/etherdevice.h>
Alexander Smirnov44331fe2011-08-24 19:34:42 -070059#include <net/af_ieee802154.h>
60#include <net/ieee802154.h>
61#include <net/ieee802154_netdev.h>
62#include <net/ipv6.h>
63
64#include "6lowpan.h"
65
66/* TTL uncompression values */
67static const u8 lowpan_ttl_values[] = {0, 1, 64, 255};
68
69static LIST_HEAD(lowpan_devices);
70
71/*
72 * Uncompression of linklocal:
73 * 0 -> 16 bytes from packet
74 * 1 -> 2 bytes from prefix - bunch of zeroes and 8 from packet
75 * 2 -> 2 bytes from prefix - zeroes + 2 from packet
76 * 3 -> 2 bytes from prefix - infer 8 bytes from lladdr
77 *
78 * NOTE: => the uncompress function does change 0xf to 0x10
79 * NOTE: 0x00 => no-autoconfig => unspecified
80 */
81static const u8 lowpan_unc_llconf[] = {0x0f, 0x28, 0x22, 0x20};
82
83/*
84 * Uncompression of ctx-based:
85 * 0 -> 0 bits from packet [unspecified / reserved]
86 * 1 -> 8 bytes from prefix - bunch of zeroes and 8 from packet
87 * 2 -> 8 bytes from prefix - zeroes + 2 from packet
88 * 3 -> 8 bytes from prefix - infer 8 bytes from lladdr
89 */
90static const u8 lowpan_unc_ctxconf[] = {0x00, 0x88, 0x82, 0x80};
91
92/*
93 * Uncompression of ctx-base
94 * 0 -> 0 bits from packet
95 * 1 -> 2 bytes from prefix - bunch of zeroes 5 from packet
96 * 2 -> 2 bytes from prefix - zeroes + 3 from packet
97 * 3 -> 2 bytes from prefix - infer 1 bytes from lladdr
98 */
99static const u8 lowpan_unc_mxconf[] = {0x0f, 0x25, 0x23, 0x21};
100
101/* Link local prefix */
102static const u8 lowpan_llprefix[] = {0xfe, 0x80};
103
104/* private device info */
105struct lowpan_dev_info {
106 struct net_device *real_dev; /* real WPAN device ptr */
107 struct mutex dev_list_mtx; /* mutex for list ops */
108};
109
110struct lowpan_dev_record {
111 struct net_device *ldev;
112 struct list_head list;
113};
114
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000115struct lowpan_fragment {
116 struct sk_buff *skb; /* skb to be assembled */
117 spinlock_t lock; /* concurency lock */
118 u16 length; /* length to be assemled */
119 u32 bytes_rcv; /* bytes received */
120 u16 tag; /* current fragment tag */
121 struct timer_list timer; /* assembling timer */
122 struct list_head list; /* fragments list */
123};
124
125static unsigned short fragment_tag;
126static LIST_HEAD(lowpan_fragments);
127spinlock_t flist_lock;
128
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700129static inline struct
130lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
131{
132 return netdev_priv(dev);
133}
134
135static inline void lowpan_address_flip(u8 *src, u8 *dest)
136{
137 int i;
138 for (i = 0; i < IEEE802154_ADDR_LEN; i++)
139 (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i];
140}
141
142/* list of all 6lowpan devices, uses for package delivering */
143/* print data in line */
144static inline void lowpan_raw_dump_inline(const char *caller, char *msg,
145 unsigned char *buf, int len)
146{
147#ifdef DEBUG
148 if (msg)
149 pr_debug("(%s) %s: ", caller, msg);
150 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE,
151 16, 1, buf, len, false);
152#endif /* DEBUG */
153}
154
155/*
156 * print data in a table format:
157 *
158 * addr: xx xx xx xx xx xx
159 * addr: xx xx xx xx xx xx
160 * ...
161 */
162static inline void lowpan_raw_dump_table(const char *caller, char *msg,
163 unsigned char *buf, int len)
164{
165#ifdef DEBUG
166 if (msg)
167 pr_debug("(%s) %s:\n", caller, msg);
168 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET,
169 16, 1, buf, len, false);
170#endif /* DEBUG */
171}
172
173static u8
174lowpan_compress_addr_64(u8 **hc06_ptr, u8 shift, const struct in6_addr *ipaddr,
175 const unsigned char *lladdr)
176{
177 u8 val = 0;
178
179 if (is_addr_mac_addr_based(ipaddr, lladdr))
180 val = 3; /* 0-bits */
181 else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
182 /* compress IID to 16 bits xxxx::XXXX */
183 memcpy(*hc06_ptr, &ipaddr->s6_addr16[7], 2);
184 *hc06_ptr += 2;
185 val = 2; /* 16-bits */
186 } else {
187 /* do not compress IID => xxxx::IID */
188 memcpy(*hc06_ptr, &ipaddr->s6_addr16[4], 8);
189 *hc06_ptr += 8;
190 val = 1; /* 64-bits */
191 }
192
193 return rol8(val, shift);
194}
195
196static void
197lowpan_uip_ds6_set_addr_iid(struct in6_addr *ipaddr, unsigned char *lladdr)
198{
alex.bluesman.smirnov@gmail.com2d319502012-04-25 23:35:51 +0000199 memcpy(&ipaddr->s6_addr[8], lladdr, IEEE802154_ADDR_LEN);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700200 /* second bit-flip (Universe/Local) is done according RFC2464 */
201 ipaddr->s6_addr[8] ^= 0x02;
202}
203
204/*
205 * Uncompress addresses based on a prefix and a postfix with zeroes in
206 * between. If the postfix is zero in length it will use the link address
207 * to configure the IP address (autoconf style).
208 * pref_post_count takes a byte where the first nibble specify prefix count
209 * and the second postfix count (NOTE: 15/0xf => 16 bytes copy).
210 */
211static int
212lowpan_uncompress_addr(struct sk_buff *skb, struct in6_addr *ipaddr,
213 u8 const *prefix, u8 pref_post_count, unsigned char *lladdr)
214{
215 u8 prefcount = pref_post_count >> 4;
216 u8 postcount = pref_post_count & 0x0f;
217
218 /* full nibble 15 => 16 */
219 prefcount = (prefcount == 15 ? 16 : prefcount);
220 postcount = (postcount == 15 ? 16 : postcount);
221
222 if (lladdr)
223 lowpan_raw_dump_inline(__func__, "linklocal address",
alex.bluesman.smirnov@gmail.com2d319502012-04-25 23:35:51 +0000224 lladdr, IEEE802154_ADDR_LEN);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700225 if (prefcount > 0)
226 memcpy(ipaddr, prefix, prefcount);
227
228 if (prefcount + postcount < 16)
229 memset(&ipaddr->s6_addr[prefcount], 0,
230 16 - (prefcount + postcount));
231
232 if (postcount > 0) {
233 memcpy(&ipaddr->s6_addr[16 - postcount], skb->data, postcount);
234 skb_pull(skb, postcount);
235 } else if (prefcount > 0) {
236 if (lladdr == NULL)
237 return -EINVAL;
238
239 /* no IID based configuration if no prefix and no data */
240 lowpan_uip_ds6_set_addr_iid(ipaddr, lladdr);
241 }
242
243 pr_debug("(%s): uncompressing %d + %d => ", __func__, prefcount,
244 postcount);
245 lowpan_raw_dump_inline(NULL, NULL, ipaddr->s6_addr, 16);
246
247 return 0;
248}
249
alex.bluesman.smirnov@gmail.com3bd5b952011-11-10 07:40:14 +0000250static void
251lowpan_compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
252{
253 struct udphdr *uh = udp_hdr(skb);
254
255 pr_debug("(%s): UDP header compression\n", __func__);
256
257 if (((uh->source & LOWPAN_NHC_UDP_4BIT_MASK) ==
258 LOWPAN_NHC_UDP_4BIT_PORT) &&
259 ((uh->dest & LOWPAN_NHC_UDP_4BIT_MASK) ==
260 LOWPAN_NHC_UDP_4BIT_PORT)) {
261 pr_debug("(%s): both ports compression to 4 bits\n", __func__);
262 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_11;
263 **(hc06_ptr + 1) = /* subtraction is faster */
264 (u8)((uh->dest - LOWPAN_NHC_UDP_4BIT_PORT) +
265 ((uh->source & LOWPAN_NHC_UDP_4BIT_PORT) << 4));
266 *hc06_ptr += 2;
267 } else if ((uh->dest & LOWPAN_NHC_UDP_8BIT_MASK) ==
268 LOWPAN_NHC_UDP_8BIT_PORT) {
269 pr_debug("(%s): remove 8 bits of dest\n", __func__);
270 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_01;
271 memcpy(*hc06_ptr + 1, &uh->source, 2);
272 **(hc06_ptr + 3) = (u8)(uh->dest - LOWPAN_NHC_UDP_8BIT_PORT);
273 *hc06_ptr += 4;
274 } else if ((uh->source & LOWPAN_NHC_UDP_8BIT_MASK) ==
275 LOWPAN_NHC_UDP_8BIT_PORT) {
276 pr_debug("(%s): remove 8 bits of source\n", __func__);
277 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_10;
278 memcpy(*hc06_ptr + 1, &uh->dest, 2);
279 **(hc06_ptr + 3) = (u8)(uh->source - LOWPAN_NHC_UDP_8BIT_PORT);
280 *hc06_ptr += 4;
281 } else {
282 pr_debug("(%s): can't compress header\n", __func__);
283 **hc06_ptr = LOWPAN_NHC_UDP_CS_P_00;
284 memcpy(*hc06_ptr + 1, &uh->source, 2);
285 memcpy(*hc06_ptr + 3, &uh->dest, 2);
286 *hc06_ptr += 5;
287 }
288
289 /* checksum is always inline */
290 memcpy(*hc06_ptr, &uh->check, 2);
291 *hc06_ptr += 2;
292}
293
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000294static inline int lowpan_fetch_skb_u8(struct sk_buff *skb, u8 *val)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700295{
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000296 if (unlikely(!pskb_may_pull(skb, 1)))
297 return -EINVAL;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700298
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000299 *val = skb->data[0];
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700300 skb_pull(skb, 1);
301
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000302 return 0;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700303}
304
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000305static inline int lowpan_fetch_skb_u16(struct sk_buff *skb, u16 *val)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000306{
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000307 if (unlikely(!pskb_may_pull(skb, 2)))
308 return -EINVAL;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000309
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000310 *val = skb->data[0] | (skb->data[1] << 8);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000311 skb_pull(skb, 2);
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000312
313 return 0;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000314}
315
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000316static int
317lowpan_uncompress_udp_header(struct sk_buff *skb)
318{
319 struct udphdr *uh = udp_hdr(skb);
320 u8 tmp;
321
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000322 if (lowpan_fetch_skb_u8(skb, &tmp))
323 goto err;
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000324
325 if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
326 pr_debug("(%s): UDP header uncompression\n", __func__);
327 switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
328 case LOWPAN_NHC_UDP_CS_P_00:
329 memcpy(&uh->source, &skb->data[0], 2);
330 memcpy(&uh->dest, &skb->data[2], 2);
331 skb_pull(skb, 4);
332 break;
333 case LOWPAN_NHC_UDP_CS_P_01:
334 memcpy(&uh->source, &skb->data[0], 2);
335 uh->dest =
336 skb->data[2] + LOWPAN_NHC_UDP_8BIT_PORT;
337 skb_pull(skb, 3);
338 break;
339 case LOWPAN_NHC_UDP_CS_P_10:
340 uh->source = skb->data[0] + LOWPAN_NHC_UDP_8BIT_PORT;
341 memcpy(&uh->dest, &skb->data[1], 2);
342 skb_pull(skb, 3);
343 break;
344 case LOWPAN_NHC_UDP_CS_P_11:
345 uh->source =
346 LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] >> 4);
347 uh->dest =
348 LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] & 0x0f);
349 skb_pull(skb, 1);
350 break;
351 default:
352 pr_debug("(%s) ERROR: unknown UDP format\n", __func__);
353 goto err;
354 break;
355 }
356
357 pr_debug("(%s): uncompressed UDP ports: src = %d, dst = %d\n",
358 __func__, uh->source, uh->dest);
359
360 /* copy checksum */
361 memcpy(&uh->check, &skb->data[0], 2);
362 skb_pull(skb, 2);
363 } else {
364 pr_debug("(%s): ERROR: unsupported NH format\n", __func__);
365 goto err;
366 }
367
368 return 0;
369err:
370 return -EINVAL;
371}
372
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700373static int lowpan_header_create(struct sk_buff *skb,
374 struct net_device *dev,
375 unsigned short type, const void *_daddr,
Eric Dumazet95c96172012-04-15 05:58:06 +0000376 const void *_saddr, unsigned int len)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700377{
378 u8 tmp, iphc0, iphc1, *hc06_ptr;
379 struct ipv6hdr *hdr;
380 const u8 *saddr = _saddr;
381 const u8 *daddr = _daddr;
382 u8 *head;
383 struct ieee802154_addr sa, da;
384
385 if (type != ETH_P_IPV6)
386 return 0;
387 /* TODO:
388 * if this package isn't ipv6 one, where should it be routed?
389 */
390 head = kzalloc(100, GFP_KERNEL);
391 if (head == NULL)
392 return -ENOMEM;
393
394 hdr = ipv6_hdr(skb);
395 hc06_ptr = head + 2;
396
397 pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
398 "\tnexthdr = 0x%02x\n\thop_lim = %d\n", __func__,
399 hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
400 hdr->hop_limit);
401
402 lowpan_raw_dump_table(__func__, "raw skb network header dump",
403 skb_network_header(skb), sizeof(struct ipv6hdr));
404
405 if (!saddr)
406 saddr = dev->dev_addr;
407
408 lowpan_raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
409
410 /*
411 * As we copy some bit-length fields, in the IPHC encoding bytes,
412 * we sometimes use |=
413 * If the field is 0, and the current bit value in memory is 1,
414 * this does not work. We therefore reset the IPHC encoding here
415 */
416 iphc0 = LOWPAN_DISPATCH_IPHC;
417 iphc1 = 0;
418
419 /* TODO: context lookup */
420
421 lowpan_raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
422
423 /*
424 * Traffic class, flow label
425 * If flow label is 0, compress it. If traffic class is 0, compress it
426 * We have to process both in the same time as the offset of traffic
427 * class depends on the presence of version and flow label
428 */
429
430 /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
431 tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
432 tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
433
434 if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
435 (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
436 /* flow label can be compressed */
437 iphc0 |= LOWPAN_IPHC_FL_C;
438 if ((hdr->priority == 0) &&
439 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
440 /* compress (elide) all */
441 iphc0 |= LOWPAN_IPHC_TC_C;
442 } else {
443 /* compress only the flow label */
444 *hc06_ptr = tmp;
445 hc06_ptr += 1;
446 }
447 } else {
448 /* Flow label cannot be compressed */
449 if ((hdr->priority == 0) &&
450 ((hdr->flow_lbl[0] & 0xF0) == 0)) {
451 /* compress only traffic class */
452 iphc0 |= LOWPAN_IPHC_TC_C;
453 *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
454 memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
455 hc06_ptr += 3;
456 } else {
457 /* compress nothing */
458 memcpy(hc06_ptr, &hdr, 4);
459 /* replace the top byte with new ECN | DSCP format */
460 *hc06_ptr = tmp;
461 hc06_ptr += 4;
462 }
463 }
464
465 /* NOTE: payload length is always compressed */
466
467 /* Next Header is compress if UDP */
468 if (hdr->nexthdr == UIP_PROTO_UDP)
469 iphc0 |= LOWPAN_IPHC_NH_C;
470
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700471 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
472 *hc06_ptr = hdr->nexthdr;
473 hc06_ptr += 1;
474 }
475
476 /*
477 * Hop limit
478 * if 1: compress, encoding is 01
479 * if 64: compress, encoding is 10
480 * if 255: compress, encoding is 11
481 * else do not compress
482 */
483 switch (hdr->hop_limit) {
484 case 1:
485 iphc0 |= LOWPAN_IPHC_TTL_1;
486 break;
487 case 64:
488 iphc0 |= LOWPAN_IPHC_TTL_64;
489 break;
490 case 255:
491 iphc0 |= LOWPAN_IPHC_TTL_255;
492 break;
493 default:
494 *hc06_ptr = hdr->hop_limit;
alex.bluesman.smirnov@gmail.com5c00c0c2012-06-25 03:49:02 +0000495 hc06_ptr += 1;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700496 break;
497 }
498
499 /* source address compression */
500 if (is_addr_unspecified(&hdr->saddr)) {
501 pr_debug("(%s): source address is unspecified, setting SAC\n",
502 __func__);
503 iphc1 |= LOWPAN_IPHC_SAC;
504 /* TODO: context lookup */
505 } else if (is_addr_link_local(&hdr->saddr)) {
506 pr_debug("(%s): source address is link-local\n", __func__);
507 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
508 LOWPAN_IPHC_SAM_BIT, &hdr->saddr, saddr);
509 } else {
510 pr_debug("(%s): send the full source address\n", __func__);
511 memcpy(hc06_ptr, &hdr->saddr.s6_addr16[0], 16);
512 hc06_ptr += 16;
513 }
514
515 /* destination address compression */
516 if (is_addr_mcast(&hdr->daddr)) {
517 pr_debug("(%s): destination address is multicast", __func__);
518 iphc1 |= LOWPAN_IPHC_M;
519 if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
520 pr_debug("compressed to 1 octet\n");
521 iphc1 |= LOWPAN_IPHC_DAM_11;
522 /* use last byte */
523 *hc06_ptr = hdr->daddr.s6_addr[15];
524 hc06_ptr += 1;
525 } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
526 pr_debug("compressed to 4 octets\n");
527 iphc1 |= LOWPAN_IPHC_DAM_10;
528 /* second byte + the last three */
529 *hc06_ptr = hdr->daddr.s6_addr[1];
530 memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[13], 3);
531 hc06_ptr += 4;
532 } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
533 pr_debug("compressed to 6 octets\n");
534 iphc1 |= LOWPAN_IPHC_DAM_01;
535 /* second byte + the last five */
536 *hc06_ptr = hdr->daddr.s6_addr[1];
537 memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[11], 5);
538 hc06_ptr += 6;
539 } else {
540 pr_debug("using full address\n");
541 iphc1 |= LOWPAN_IPHC_DAM_00;
542 memcpy(hc06_ptr, &hdr->daddr.s6_addr[0], 16);
543 hc06_ptr += 16;
544 }
545 } else {
546 pr_debug("(%s): destination address is unicast: ", __func__);
547 /* TODO: context lookup */
548 if (is_addr_link_local(&hdr->daddr)) {
549 pr_debug("destination address is link-local\n");
550 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
551 LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
552 } else {
553 pr_debug("using full address\n");
554 memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
555 hc06_ptr += 16;
556 }
557 }
558
alex.bluesman.smirnov@gmail.com3bd5b952011-11-10 07:40:14 +0000559 /* UDP header compression */
560 if (hdr->nexthdr == UIP_PROTO_UDP)
561 lowpan_compress_udp_header(&hc06_ptr, skb);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700562
563 head[0] = iphc0;
564 head[1] = iphc1;
565
566 skb_pull(skb, sizeof(struct ipv6hdr));
567 memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
568
569 kfree(head);
570
571 lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
572 skb->len);
573
574 /*
575 * NOTE1: I'm still unsure about the fact that compression and WPAN
576 * header are created here and not later in the xmit. So wait for
577 * an opinion of net maintainers.
578 */
579 /*
580 * NOTE2: to be absolutely correct, we must derive PANid information
581 * from MAC subif of the 'dev' and 'real_dev' network devices, but
582 * this isn't implemented in mainline yet, so currently we assign 0xff
583 */
584 {
585 /* prepare wpan address data */
586 sa.addr_type = IEEE802154_ADDR_LONG;
587 sa.pan_id = 0xff;
588
589 da.addr_type = IEEE802154_ADDR_LONG;
590 da.pan_id = 0xff;
591
592 memcpy(&(da.hwaddr), daddr, 8);
593 memcpy(&(sa.hwaddr), saddr, 8);
594
595 mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000596
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700597 return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
598 type, (void *)&da, (void *)&sa, skb->len);
599 }
600}
601
602static int lowpan_skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr)
603{
604 struct sk_buff *new;
605 struct lowpan_dev_record *entry;
606 int stat = NET_RX_SUCCESS;
607
608 new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
alex.bluesman.smirnov@gmail.comdcef1152011-09-01 03:55:15 +0000609 GFP_ATOMIC);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700610 kfree_skb(skb);
611
alex.bluesman.smirnov@gmail.comdcef1152011-09-01 03:55:15 +0000612 if (!new)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700613 return -ENOMEM;
614
615 skb_push(new, sizeof(struct ipv6hdr));
616 skb_reset_network_header(new);
617 skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
618
619 new->protocol = htons(ETH_P_IPV6);
620 new->pkt_type = PACKET_HOST;
621
622 rcu_read_lock();
623 list_for_each_entry_rcu(entry, &lowpan_devices, list)
624 if (lowpan_dev_info(entry->ldev)->real_dev == new->dev) {
alex.bluesman.smirnov@gmail.comdcef1152011-09-01 03:55:15 +0000625 skb = skb_copy(new, GFP_ATOMIC);
626 if (!skb) {
627 stat = -ENOMEM;
628 break;
629 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700630
alex.bluesman.smirnov@gmail.comdcef1152011-09-01 03:55:15 +0000631 skb->dev = entry->ldev;
632 stat = netif_rx(skb);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700633 }
634 rcu_read_unlock();
635
636 kfree_skb(new);
637
638 return stat;
639}
640
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000641static void lowpan_fragment_timer_expired(unsigned long entry_addr)
642{
643 struct lowpan_fragment *entry = (struct lowpan_fragment *)entry_addr;
644
645 pr_debug("%s: timer expired for frame with tag %d\n", __func__,
646 entry->tag);
647
648 spin_lock(&flist_lock);
649 list_del(&entry->list);
650 spin_unlock(&flist_lock);
651
652 dev_kfree_skb(entry->skb);
653 kfree(entry);
654}
655
alex.bluesman.smirnov@gmail.comc2e94d72012-04-25 23:35:50 +0000656static struct lowpan_fragment *
657lowpan_alloc_new_frame(struct sk_buff *skb, u8 iphc0, u8 len, u8 tag)
658{
659 struct lowpan_fragment *frame;
660
661 frame = kzalloc(sizeof(struct lowpan_fragment),
662 GFP_ATOMIC);
663 if (!frame)
664 goto frame_err;
665
666 INIT_LIST_HEAD(&frame->list);
667
668 frame->length = (iphc0 & 7) | (len << 3);
669 frame->tag = tag;
670
671 /* allocate buffer for frame assembling */
672 frame->skb = alloc_skb(frame->length +
673 sizeof(struct ipv6hdr), GFP_ATOMIC);
674
675 if (!frame->skb)
676 goto skb_err;
677
678 frame->skb->priority = skb->priority;
679 frame->skb->dev = skb->dev;
680
681 /* reserve headroom for uncompressed ipv6 header */
682 skb_reserve(frame->skb, sizeof(struct ipv6hdr));
683 skb_put(frame->skb, frame->length);
684
685 init_timer(&frame->timer);
686 /* time out is the same as for ipv6 - 60 sec */
687 frame->timer.expires = jiffies + LOWPAN_FRAG_TIMEOUT;
688 frame->timer.data = (unsigned long)frame;
689 frame->timer.function = lowpan_fragment_timer_expired;
690
691 add_timer(&frame->timer);
692
693 list_add_tail(&frame->list, &lowpan_fragments);
694
695 return frame;
696
697skb_err:
698 kfree(frame);
699frame_err:
700 return NULL;
701}
702
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700703static int
704lowpan_process_data(struct sk_buff *skb)
705{
706 struct ipv6hdr hdr;
707 u8 tmp, iphc0, iphc1, num_context = 0;
708 u8 *_saddr, *_daddr;
709 int err;
710
711 lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
712 skb->len);
713 /* at least two bytes will be used for the encoding */
714 if (skb->len < 2)
715 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000716
717 if (lowpan_fetch_skb_u8(skb, &iphc0))
718 goto drop;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000719
720 /* fragments assembling */
721 switch (iphc0 & LOWPAN_DISPATCH_MASK) {
722 case LOWPAN_DISPATCH_FRAG1:
723 case LOWPAN_DISPATCH_FRAGN:
724 {
725 struct lowpan_fragment *frame;
726 u8 len, offset;
727 u16 tag;
728 bool found = false;
729
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000730 if (lowpan_fetch_skb_u8(skb, &len) || /* frame length */
731 lowpan_fetch_skb_u16(skb, &tag)) /* fragment tag */
732 goto drop;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000733
734 /*
735 * check if frame assembling with the same tag is
736 * already in progress
737 */
738 spin_lock(&flist_lock);
739
740 list_for_each_entry(frame, &lowpan_fragments, list)
741 if (frame->tag == tag) {
742 found = true;
743 break;
744 }
745
746 /* alloc new frame structure */
747 if (!found) {
alex.bluesman.smirnov@gmail.comc2e94d72012-04-25 23:35:50 +0000748 frame = lowpan_alloc_new_frame(skb, iphc0, len, tag);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000749 if (!frame)
750 goto unlock_and_drop;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000751 }
752
753 if ((iphc0 & LOWPAN_DISPATCH_MASK) == LOWPAN_DISPATCH_FRAG1)
754 goto unlock_and_drop;
755
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000756 if (lowpan_fetch_skb_u8(skb, &offset)) /* fetch offset */
757 goto unlock_and_drop;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000758
759 /* if payload fits buffer, copy it */
760 if (likely((offset * 8 + skb->len) <= frame->length))
761 skb_copy_to_linear_data_offset(frame->skb, offset * 8,
762 skb->data, skb->len);
763 else
764 goto unlock_and_drop;
765
766 frame->bytes_rcv += skb->len;
767
768 /* frame assembling complete */
769 if ((frame->bytes_rcv == frame->length) &&
770 frame->timer.expires > jiffies) {
771 /* if timer haven't expired - first of all delete it */
772 del_timer(&frame->timer);
773 list_del(&frame->list);
774 spin_unlock(&flist_lock);
775
776 dev_kfree_skb(skb);
777 skb = frame->skb;
778 kfree(frame);
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000779
780 if (lowpan_fetch_skb_u8(skb, &iphc0))
781 goto unlock_and_drop;
782
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000783 break;
784 }
785 spin_unlock(&flist_lock);
786
787 return kfree_skb(skb), 0;
788 }
789 default:
790 break;
791 }
792
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000793 if (lowpan_fetch_skb_u8(skb, &iphc1))
794 goto drop;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700795
796 _saddr = mac_cb(skb)->sa.hwaddr;
797 _daddr = mac_cb(skb)->da.hwaddr;
798
799 pr_debug("(%s): iphc0 = %02x, iphc1 = %02x\n", __func__, iphc0, iphc1);
800
801 /* another if the CID flag is set */
802 if (iphc1 & LOWPAN_IPHC_CID) {
803 pr_debug("(%s): CID flag is set, increase header with one\n",
804 __func__);
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000805 if (lowpan_fetch_skb_u8(skb, &num_context))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700806 goto drop;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700807 }
808
809 hdr.version = 6;
810
811 /* Traffic Class and Flow Label */
812 switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
813 /*
814 * Traffic Class and FLow Label carried in-line
815 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
816 */
817 case 0: /* 00b */
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000818 if (lowpan_fetch_skb_u8(skb, &tmp))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700819 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000820
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700821 memcpy(&hdr.flow_lbl, &skb->data[0], 3);
822 skb_pull(skb, 3);
823 hdr.priority = ((tmp >> 2) & 0x0f);
824 hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
825 (hdr.flow_lbl[0] & 0x0f);
826 break;
827 /*
828 * Traffic class carried in-line
829 * ECN + DSCP (1 byte), Flow Label is elided
830 */
831 case 1: /* 10b */
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000832 if (lowpan_fetch_skb_u8(skb, &tmp))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700833 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000834
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700835 hdr.priority = ((tmp >> 2) & 0x0f);
836 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
837 hdr.flow_lbl[1] = 0;
838 hdr.flow_lbl[2] = 0;
839 break;
840 /*
841 * Flow Label carried in-line
842 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
843 */
844 case 2: /* 01b */
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000845 if (lowpan_fetch_skb_u8(skb, &tmp))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700846 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000847
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700848 hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
849 memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
850 skb_pull(skb, 2);
851 break;
852 /* Traffic Class and Flow Label are elided */
853 case 3: /* 11b */
854 hdr.priority = 0;
855 hdr.flow_lbl[0] = 0;
856 hdr.flow_lbl[1] = 0;
857 hdr.flow_lbl[2] = 0;
858 break;
859 default:
860 break;
861 }
862
863 /* Next Header */
864 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
865 /* Next header is carried inline */
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000866 if (lowpan_fetch_skb_u8(skb, &(hdr.nexthdr)))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700867 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000868
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700869 pr_debug("(%s): NH flag is set, next header is carried "
870 "inline: %02x\n", __func__, hdr.nexthdr);
871 }
872
873 /* Hop Limit */
874 if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
875 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
876 else {
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000877 if (lowpan_fetch_skb_u8(skb, &(hdr.hop_limit)))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700878 goto drop;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700879 }
880
881 /* Extract SAM to the tmp variable */
882 tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
883
884 /* Source address uncompression */
885 pr_debug("(%s): source address stateless compression\n", __func__);
886 err = lowpan_uncompress_addr(skb, &hdr.saddr, lowpan_llprefix,
887 lowpan_unc_llconf[tmp], skb->data);
888 if (err)
889 goto drop;
890
891 /* Extract DAM to the tmp variable */
892 tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
893
894 /* check for Multicast Compression */
895 if (iphc1 & LOWPAN_IPHC_M) {
896 if (iphc1 & LOWPAN_IPHC_DAC) {
897 pr_debug("(%s): destination address context-based "
898 "multicast compression\n", __func__);
899 /* TODO: implement this */
900 } else {
901 u8 prefix[] = {0xff, 0x02};
902
903 pr_debug("(%s): destination address non-context-based"
904 " multicast compression\n", __func__);
905 if (0 < tmp && tmp < 3) {
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000906 if (lowpan_fetch_skb_u8(skb, &prefix[1]))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700907 goto drop;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700908 }
909
910 err = lowpan_uncompress_addr(skb, &hdr.daddr, prefix,
911 lowpan_unc_mxconf[tmp], NULL);
912 if (err)
913 goto drop;
914 }
915 } else {
916 pr_debug("(%s): destination address stateless compression\n",
917 __func__);
918 err = lowpan_uncompress_addr(skb, &hdr.daddr, lowpan_llprefix,
919 lowpan_unc_llconf[tmp], skb->data);
920 if (err)
921 goto drop;
922 }
923
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000924 /* UDP data uncompression */
925 if (iphc0 & LOWPAN_IPHC_NH_C)
926 if (lowpan_uncompress_udp_header(skb))
927 goto drop;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700928
929 /* Not fragmented package */
930 hdr.payload_len = htons(skb->len);
931
932 pr_debug("(%s): skb headroom size = %d, data length = %d\n", __func__,
933 skb_headroom(skb), skb->len);
934
935 pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
936 "nexthdr = 0x%02x\n\thop_lim = %d\n", __func__, hdr.version,
937 ntohs(hdr.payload_len), hdr.nexthdr, hdr.hop_limit);
938
939 lowpan_raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
940 sizeof(hdr));
941 return lowpan_skb_deliver(skb, &hdr);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000942
943unlock_and_drop:
944 spin_unlock(&flist_lock);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700945drop:
Dan Carpenter90d09632011-08-30 03:45:52 +0000946 kfree_skb(skb);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700947 return -EINVAL;
948}
949
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000950static int lowpan_get_mac_header_length(struct sk_buff *skb)
951{
952 /*
953 * Currently long addressing mode is supported only, so the overall
954 * header size is 21:
955 * FC SeqNum DPAN DA SA Sec
956 * 2 + 1 + 2 + 8 + 8 + 0 = 21
957 */
958 return 21;
959}
960
961static int
962lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
963 int mlen, int plen, int offset)
964{
965 struct sk_buff *frag;
966 int hlen, ret;
967
968 /* if payload length is zero, therefore it's a first fragment */
969 hlen = (plen == 0 ? LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE);
970
971 lowpan_raw_dump_inline(__func__, "6lowpan fragment header", head, hlen);
972
973 frag = dev_alloc_skb(hlen + mlen + plen + IEEE802154_MFR_SIZE);
974 if (!frag)
975 return -ENOMEM;
976
977 frag->priority = skb->priority;
978 frag->dev = skb->dev;
979
980 /* copy header, MFR and payload */
981 memcpy(skb_put(frag, mlen), skb->data, mlen);
982 memcpy(skb_put(frag, hlen), head, hlen);
983
984 if (plen)
985 skb_copy_from_linear_data_offset(skb, offset + mlen,
986 skb_put(frag, plen), plen);
987
988 lowpan_raw_dump_table(__func__, " raw fragment dump", frag->data,
989 frag->len);
990
991 ret = dev_queue_xmit(frag);
992
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000993 return ret;
994}
995
996static int
997lowpan_skb_fragmentation(struct sk_buff *skb)
998{
999 int err, header_length, payload_length, tag, offset = 0;
1000 u8 head[5];
1001
1002 header_length = lowpan_get_mac_header_length(skb);
1003 payload_length = skb->len - header_length;
1004 tag = fragment_tag++;
1005
1006 /* first fragment header */
1007 head[0] = LOWPAN_DISPATCH_FRAG1 | (payload_length & 0x7);
1008 head[1] = (payload_length >> 3) & 0xff;
1009 head[2] = tag & 0xff;
1010 head[3] = tag >> 8;
1011
1012 err = lowpan_fragment_xmit(skb, head, header_length, 0, 0);
1013
1014 /* next fragment header */
1015 head[0] &= ~LOWPAN_DISPATCH_FRAG1;
1016 head[0] |= LOWPAN_DISPATCH_FRAGN;
1017
1018 while ((payload_length - offset > 0) && (err >= 0)) {
1019 int len = LOWPAN_FRAG_SIZE;
1020
1021 head[4] = offset / 8;
1022
1023 if (payload_length - offset < len)
1024 len = payload_length - offset;
1025
1026 err = lowpan_fragment_xmit(skb, head, header_length,
1027 len, offset);
1028 offset += len;
1029 }
1030
1031 return err;
1032}
1033
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001034static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
1035{
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001036 int err = -1;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001037
1038 pr_debug("(%s): package xmit\n", __func__);
1039
1040 skb->dev = lowpan_dev_info(dev)->real_dev;
1041 if (skb->dev == NULL) {
1042 pr_debug("(%s) ERROR: no real wpan device found\n", __func__);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001043 goto error;
1044 }
1045
1046 if (skb->len <= IEEE802154_MTU) {
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001047 err = dev_queue_xmit(skb);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001048 goto out;
1049 }
1050
1051 pr_debug("(%s): frame is too big, fragmentation is needed\n",
1052 __func__);
1053 err = lowpan_skb_fragmentation(skb);
1054error:
1055 dev_kfree_skb(skb);
1056out:
1057 if (err < 0)
1058 pr_debug("(%s): ERROR: xmit failed\n", __func__);
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001059
1060 return (err < 0 ? NETDEV_TX_BUSY : NETDEV_TX_OK);
1061}
1062
1063static void lowpan_dev_free(struct net_device *dev)
1064{
1065 dev_put(lowpan_dev_info(dev)->real_dev);
1066 free_netdev(dev);
1067}
1068
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +00001069static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
1070{
1071 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
1072 return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
1073}
1074
1075static u16 lowpan_get_pan_id(const struct net_device *dev)
1076{
1077 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
1078 return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
1079}
1080
1081static u16 lowpan_get_short_addr(const struct net_device *dev)
1082{
1083 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
1084 return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
1085}
1086
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001087static struct header_ops lowpan_header_ops = {
1088 .create = lowpan_header_create,
1089};
1090
1091static const struct net_device_ops lowpan_netdev_ops = {
1092 .ndo_start_xmit = lowpan_xmit,
Danny Kukawkacdf49c22012-02-22 02:36:39 +00001093 .ndo_set_mac_address = eth_mac_addr,
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001094};
1095
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +00001096static struct ieee802154_mlme_ops lowpan_mlme = {
1097 .get_pan_id = lowpan_get_pan_id,
1098 .get_phy = lowpan_get_phy,
1099 .get_short_addr = lowpan_get_short_addr,
1100};
1101
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001102static void lowpan_setup(struct net_device *dev)
1103{
1104 pr_debug("(%s)\n", __func__);
1105
1106 dev->addr_len = IEEE802154_ADDR_LEN;
1107 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
1108 dev->type = ARPHRD_IEEE802154;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001109 /* Frame Control + Sequence Number + Address fields + Security Header */
1110 dev->hard_header_len = 2 + 1 + 20 + 14;
1111 dev->needed_tailroom = 2; /* FCS */
1112 dev->mtu = 1281;
1113 dev->tx_queue_len = 0;
alex.bluesman.smirnov@gmail.com4d039f62011-11-10 07:39:37 +00001114 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001115 dev->watchdog_timeo = 0;
1116
1117 dev->netdev_ops = &lowpan_netdev_ops;
1118 dev->header_ops = &lowpan_header_ops;
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +00001119 dev->ml_priv = &lowpan_mlme;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001120 dev->destructor = lowpan_dev_free;
1121}
1122
1123static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
1124{
1125 pr_debug("(%s)\n", __func__);
1126
1127 if (tb[IFLA_ADDRESS]) {
1128 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
1129 return -EINVAL;
1130 }
1131 return 0;
1132}
1133
1134static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
1135 struct packet_type *pt, struct net_device *orig_dev)
1136{
1137 if (!netif_running(dev))
1138 goto drop;
1139
1140 if (dev->type != ARPHRD_IEEE802154)
1141 goto drop;
1142
1143 /* check that it's our buffer */
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001144 switch (skb->data[0] & 0xe0) {
1145 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
1146 case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
1147 case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001148 lowpan_process_data(skb);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001149 break;
1150 default:
1151 break;
1152 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001153
1154 return NET_RX_SUCCESS;
1155
1156drop:
1157 kfree_skb(skb);
1158 return NET_RX_DROP;
1159}
1160
1161static int lowpan_newlink(struct net *src_net, struct net_device *dev,
1162 struct nlattr *tb[], struct nlattr *data[])
1163{
1164 struct net_device *real_dev;
1165 struct lowpan_dev_record *entry;
1166
1167 pr_debug("(%s)\n", __func__);
1168
1169 if (!tb[IFLA_LINK])
1170 return -EINVAL;
1171 /* find and hold real wpan device */
1172 real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1173 if (!real_dev)
1174 return -ENODEV;
1175
1176 lowpan_dev_info(dev)->real_dev = real_dev;
1177 mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
1178
1179 entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
Dan Carpenterdc00fd42011-08-30 03:51:09 +00001180 if (!entry) {
1181 dev_put(real_dev);
1182 lowpan_dev_info(dev)->real_dev = NULL;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001183 return -ENOMEM;
Dan Carpenterdc00fd42011-08-30 03:51:09 +00001184 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001185
1186 entry->ldev = dev;
1187
1188 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
1189 INIT_LIST_HEAD(&entry->list);
1190 list_add_tail(&entry->list, &lowpan_devices);
1191 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
1192
alex.bluesman.smirnov@gmail.com768f7c72012-04-25 23:24:58 +00001193 spin_lock_init(&flist_lock);
1194
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001195 register_netdevice(dev);
1196
1197 return 0;
1198}
1199
1200static void lowpan_dellink(struct net_device *dev, struct list_head *head)
1201{
1202 struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
1203 struct net_device *real_dev = lowpan_dev->real_dev;
alex.bluesman.smirnov@gmail.com8deff4a2012-04-25 23:24:57 +00001204 struct lowpan_dev_record *entry, *tmp;
1205 struct lowpan_fragment *frame, *tframe;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001206
1207 ASSERT_RTNL();
1208
alex.bluesman.smirnov@gmail.com8deff4a2012-04-25 23:24:57 +00001209 spin_lock(&flist_lock);
1210 list_for_each_entry_safe(frame, tframe, &lowpan_fragments, list) {
1211 del_timer(&frame->timer);
1212 list_del(&frame->list);
1213 dev_kfree_skb(frame->skb);
1214 kfree(frame);
1215 }
1216 spin_unlock(&flist_lock);
1217
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001218 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
Dan Carpenteraec9db32011-08-30 03:46:40 +00001219 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001220 if (entry->ldev == dev) {
1221 list_del(&entry->list);
1222 kfree(entry);
1223 }
Dan Carpenteraec9db32011-08-30 03:46:40 +00001224 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001225 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
1226
1227 mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
1228
1229 unregister_netdevice_queue(dev, head);
1230
1231 dev_put(real_dev);
1232}
1233
1234static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
1235 .kind = "lowpan",
1236 .priv_size = sizeof(struct lowpan_dev_info),
1237 .setup = lowpan_setup,
1238 .newlink = lowpan_newlink,
1239 .dellink = lowpan_dellink,
1240 .validate = lowpan_validate,
1241};
1242
1243static inline int __init lowpan_netlink_init(void)
1244{
1245 return rtnl_link_register(&lowpan_link_ops);
1246}
1247
1248static inline void __init lowpan_netlink_fini(void)
1249{
1250 rtnl_link_unregister(&lowpan_link_ops);
1251}
1252
1253static struct packet_type lowpan_packet_type = {
1254 .type = __constant_htons(ETH_P_IEEE802154),
1255 .func = lowpan_rcv,
1256};
1257
1258static int __init lowpan_init_module(void)
1259{
1260 int err = 0;
1261
1262 pr_debug("(%s)\n", __func__);
1263
1264 err = lowpan_netlink_init();
1265 if (err < 0)
1266 goto out;
1267
1268 dev_add_pack(&lowpan_packet_type);
1269out:
1270 return err;
1271}
1272
1273static void __exit lowpan_cleanup_module(void)
1274{
1275 pr_debug("(%s)\n", __func__);
1276
1277 lowpan_netlink_fini();
1278
1279 dev_remove_pack(&lowpan_packet_type);
1280}
1281
1282module_init(lowpan_init_module);
1283module_exit(lowpan_cleanup_module);
1284MODULE_LICENSE("GPL");
1285MODULE_ALIAS_RTNL_LINK("lowpan");