blob: 9aea7ce8b83d9820d6fdb36ee17b86f35dbf17d8 [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>
58#include <net/af_ieee802154.h>
59#include <net/ieee802154.h>
60#include <net/ieee802154_netdev.h>
61#include <net/ipv6.h>
62
63#include "6lowpan.h"
64
65/* TTL uncompression values */
66static const u8 lowpan_ttl_values[] = {0, 1, 64, 255};
67
68static LIST_HEAD(lowpan_devices);
69
70/*
71 * Uncompression of linklocal:
72 * 0 -> 16 bytes from packet
73 * 1 -> 2 bytes from prefix - bunch of zeroes and 8 from packet
74 * 2 -> 2 bytes from prefix - zeroes + 2 from packet
75 * 3 -> 2 bytes from prefix - infer 8 bytes from lladdr
76 *
77 * NOTE: => the uncompress function does change 0xf to 0x10
78 * NOTE: 0x00 => no-autoconfig => unspecified
79 */
80static const u8 lowpan_unc_llconf[] = {0x0f, 0x28, 0x22, 0x20};
81
82/*
83 * Uncompression of ctx-based:
84 * 0 -> 0 bits from packet [unspecified / reserved]
85 * 1 -> 8 bytes from prefix - bunch of zeroes and 8 from packet
86 * 2 -> 8 bytes from prefix - zeroes + 2 from packet
87 * 3 -> 8 bytes from prefix - infer 8 bytes from lladdr
88 */
89static const u8 lowpan_unc_ctxconf[] = {0x00, 0x88, 0x82, 0x80};
90
Alexander Smirnov44331fe2011-08-24 19:34:42 -070091/* Link local prefix */
92static const u8 lowpan_llprefix[] = {0xfe, 0x80};
93
94/* private device info */
95struct lowpan_dev_info {
96 struct net_device *real_dev; /* real WPAN device ptr */
97 struct mutex dev_list_mtx; /* mutex for list ops */
Tony Cheneauababf382013-03-26 18:09:24 +000098 unsigned short fragment_tag;
Alexander Smirnov44331fe2011-08-24 19:34:42 -070099};
100
101struct lowpan_dev_record {
102 struct net_device *ldev;
103 struct list_head list;
104};
105
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000106struct lowpan_fragment {
107 struct sk_buff *skb; /* skb to be assembled */
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000108 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
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000115static LIST_HEAD(lowpan_fragments);
alex.bluesman.smirnov@gmail.com4d27de12012-07-10 21:22:42 +0000116static DEFINE_SPINLOCK(flist_lock);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000117
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700118static inline struct
119lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
120{
121 return netdev_priv(dev);
122}
123
124static 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 */
133static 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 */
151static 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
162static u8
163lowpan_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
185static void
186lowpan_uip_ds6_set_addr_iid(struct in6_addr *ipaddr, unsigned char *lladdr)
187{
alex.bluesman.smirnov@gmail.com2d319502012-04-25 23:35:51 +0000188 memcpy(&ipaddr->s6_addr[8], lladdr, IEEE802154_ADDR_LEN);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700189 /* 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 */
200static int
201lowpan_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",
alex.bluesman.smirnov@gmail.com2d319502012-04-25 23:35:51 +0000213 lladdr, IEEE802154_ADDR_LEN);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700214 if (prefcount > 0)
215 memcpy(ipaddr, prefix, prefcount);
216
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700217 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
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000228 pr_debug("uncompressing %d + %d => ", prefcount, postcount);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700229 lowpan_raw_dump_inline(NULL, NULL, ipaddr->s6_addr, 16);
230
231 return 0;
232}
233
Alexander Aring84c2e7b2013-08-16 21:59:57 +0200234/* Uncompress function for multicast destination address,
235 * when M bit is set.
236 */
237static int
238lowpan_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
alex.bluesman.smirnov@gmail.com3bd5b952011-11-10 07:40:14 +0000291static void
292lowpan_compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
293{
294 struct udphdr *uh = udp_hdr(skb);
295
alex.bluesman.smirnov@gmail.com3bd5b952011-11-10 07:40:14 +0000296 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)) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000300 pr_debug("UDP header: both ports compression to 4 bits\n");
alex.bluesman.smirnov@gmail.com3bd5b952011-11-10 07:40:14 +0000301 **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) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000308 pr_debug("UDP header: remove 8 bits of dest\n");
alex.bluesman.smirnov@gmail.com3bd5b952011-11-10 07:40:14 +0000309 **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) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000315 pr_debug("UDP header: remove 8 bits of source\n");
alex.bluesman.smirnov@gmail.com3bd5b952011-11-10 07:40:14 +0000316 **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 {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000321 pr_debug("UDP header: can't compress\n");
alex.bluesman.smirnov@gmail.com3bd5b952011-11-10 07:40:14 +0000322 **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;
Tony Cheneau24363b62013-03-25 17:59:32 +0000331
332 /* skip the UDP header */
333 skb_pull(skb, sizeof(struct udphdr));
alex.bluesman.smirnov@gmail.com3bd5b952011-11-10 07:40:14 +0000334}
335
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000336static inline int lowpan_fetch_skb_u8(struct sk_buff *skb, u8 *val)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700337{
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000338 if (unlikely(!pskb_may_pull(skb, 1)))
339 return -EINVAL;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700340
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000341 *val = skb->data[0];
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700342 skb_pull(skb, 1);
343
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000344 return 0;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700345}
346
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000347static inline int lowpan_fetch_skb_u16(struct sk_buff *skb, u16 *val)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000348{
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000349 if (unlikely(!pskb_may_pull(skb, 2)))
350 return -EINVAL;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000351
Tony Cheneau45760392012-07-11 06:51:15 +0000352 *val = (skb->data[0] << 8) | skb->data[1];
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000353 skb_pull(skb, 2);
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000354
355 return 0;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000356}
357
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000358static int
Tony Cheneau24363b62013-03-25 17:59:32 +0000359lowpan_uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000360{
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000361 u8 tmp;
362
Tony Cheneaud4787a12012-07-11 06:51:14 +0000363 if (!uh)
364 goto err;
365
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000366 if (lowpan_fetch_skb_u8(skb, &tmp))
367 goto err;
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000368
369 if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000370 pr_debug("UDP header uncompression\n");
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000371 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:
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000396 pr_debug("ERROR: unknown UDP format\n");
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000397 goto err;
398 break;
399 }
400
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000401 pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
402 uh->source, uh->dest);
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000403
404 /* copy checksum */
405 memcpy(&uh->check, &skb->data[0], 2);
406 skb_pull(skb, 2);
Tony Cheneau24363b62013-03-25 17:59:32 +0000407
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);
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000415 } else {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000416 pr_debug("ERROR: unsupported NH format\n");
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000417 goto err;
418 }
419
420 return 0;
421err:
422 return -EINVAL;
423}
424
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700425static int lowpan_header_create(struct sk_buff *skb,
426 struct net_device *dev,
427 unsigned short type, const void *_daddr,
Eric Dumazet95c96172012-04-15 05:58:06 +0000428 const void *_saddr, unsigned int len)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700429{
430 u8 tmp, iphc0, iphc1, *hc06_ptr;
431 struct ipv6hdr *hdr;
432 const u8 *saddr = _saddr;
433 const u8 *daddr = _daddr;
Alexander Aringfc4e98d2013-02-05 10:23:43 +0000434 u8 head[100];
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700435 struct ieee802154_addr sa, da;
436
Alexander Aringfc4e98d2013-02-05 10:23:43 +0000437 /* TODO:
438 * if this package isn't ipv6 one, where should it be routed?
439 */
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700440 if (type != ETH_P_IPV6)
441 return 0;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700442
443 hdr = ipv6_hdr(skb);
444 hc06_ptr = head + 2;
445
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000446 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);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700449
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
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700519 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;
alex.bluesman.smirnov@gmail.com5c00c0c2012-06-25 03:49:02 +0000543 hc06_ptr += 1;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700544 break;
545 }
546
547 /* source address compression */
548 if (is_addr_unspecified(&hdr->saddr)) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000549 pr_debug("source address is unspecified, setting SAC\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700550 iphc1 |= LOWPAN_IPHC_SAC;
551 /* TODO: context lookup */
552 } else if (is_addr_link_local(&hdr->saddr)) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000553 pr_debug("source address is link-local\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700554 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
555 LOWPAN_IPHC_SAM_BIT, &hdr->saddr, saddr);
556 } else {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000557 pr_debug("send the full source address\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700558 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)) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000564 pr_debug("destination address is multicast: ");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700565 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 {
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700593 /* TODO: context lookup */
594 if (is_addr_link_local(&hdr->daddr)) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000595 pr_debug("dest address is unicast and link-local\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700596 iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
597 LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
598 } else {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000599 pr_debug("dest address is unicast: using full one\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700600 memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
601 hc06_ptr += 16;
602 }
603 }
604
alex.bluesman.smirnov@gmail.com3bd5b952011-11-10 07:40:14 +0000605 /* UDP header compression */
606 if (hdr->nexthdr == UIP_PROTO_UDP)
607 lowpan_compress_udp_header(&hc06_ptr, skb);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700608
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
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700615 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 {
Tony Cheneau58ef67c2013-03-25 17:59:25 +0000629 mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
Tony Cheneauc7d0ab22013-03-25 17:59:30 +0000630 mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
Tony Cheneau58ef67c2013-03-25 17:59:25 +0000631
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700632 /* prepare wpan address data */
633 sa.addr_type = IEEE802154_ADDR_LONG;
Tony Cheneau43de7aa2013-03-25 17:59:31 +0000634 sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700635
Tony Cheneau43de7aa2013-03-25 17:59:31 +0000636 memcpy(&(sa.hwaddr), saddr, 8);
637 /* intra-PAN communications */
638 da.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
639
Tony Cheneau58ef67c2013-03-25 17:59:25 +0000640 /*
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;
Tony Cheneau6bdeaba2013-03-26 18:09:25 +0000649 memcpy(&(da.hwaddr), daddr, IEEE802154_ADDR_LEN);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000650
Tony Cheneau58ef67c2013-03-25 17:59:25 +0000651 /* request acknowledgment */
Tony Cheneauf333a152013-03-25 17:59:23 +0000652 mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ;
Tony Cheneau58ef67c2013-03-25 17:59:25 +0000653 }
Tony Cheneauf333a152013-03-25 17:59:23 +0000654
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700655 return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
656 type, (void *)&da, (void *)&sa, skb->len);
657 }
658}
659
Alan Ott0c446212013-01-16 19:09:47 +0000660static 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
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700683static int lowpan_skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr)
684{
685 struct sk_buff *new;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700686 int stat = NET_RX_SUCCESS;
687
688 new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
alex.bluesman.smirnov@gmail.comdcef1152011-09-01 03:55:15 +0000689 GFP_ATOMIC);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700690 kfree_skb(skb);
691
alex.bluesman.smirnov@gmail.comdcef1152011-09-01 03:55:15 +0000692 if (!new)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700693 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
Alan Ott0c446212013-01-16 19:09:47 +0000702 stat = lowpan_give_skb_to_devices(new);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700703
704 kfree_skb(new);
705
706 return stat;
707}
708
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000709static void lowpan_fragment_timer_expired(unsigned long entry_addr)
710{
711 struct lowpan_fragment *entry = (struct lowpan_fragment *)entry_addr;
712
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000713 pr_debug("timer expired for frame with tag %d\n", entry->tag);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000714
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000715 list_del(&entry->list);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000716 dev_kfree_skb(entry->skb);
717 kfree(entry);
718}
719
alex.bluesman.smirnov@gmail.comc2e94d72012-04-25 23:35:50 +0000720static struct lowpan_fragment *
Tony Cheneaud991b982013-03-25 17:59:26 +0000721lowpan_alloc_new_frame(struct sk_buff *skb, u16 len, u16 tag)
alex.bluesman.smirnov@gmail.comc2e94d72012-04-25 23:35:50 +0000722{
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
Tony Cheneau5e968552012-07-11 06:51:16 +0000732 frame->length = len;
alex.bluesman.smirnov@gmail.comc2e94d72012-04-25 23:35:50 +0000733 frame->tag = tag;
734
735 /* allocate buffer for frame assembling */
alex.bluesman.smirnov@gmail.com79ff1db2012-07-10 21:22:45 +0000736 frame->skb = netdev_alloc_skb_ip_align(skb->dev, frame->length +
737 sizeof(struct ipv6hdr));
alex.bluesman.smirnov@gmail.comc2e94d72012-04-25 23:35:50 +0000738
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
David Hauweele31afe1f2013-08-16 21:59:55 +0200749 /* 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
alex.bluesman.smirnov@gmail.comc2e94d72012-04-25 23:35:50 +0000755 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
767skb_err:
768 kfree(frame);
769frame_err:
770 return NULL;
771}
772
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700773static int
774lowpan_process_data(struct sk_buff *skb)
775{
Alexander Aring84ce1ddf2013-08-16 21:59:54 +0200776 struct ipv6hdr hdr = {};
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700777 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;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000786
787 if (lowpan_fetch_skb_u8(skb, &iphc0))
788 goto drop;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000789
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;
Tony Cheneau5e968552012-07-11 06:51:16 +0000796 /* slen stores the rightmost 8 bits of the 11 bits length */
Tony Cheneaud991b982013-03-25 17:59:26 +0000797 u8 slen, offset = 0;
Tony Cheneau5e968552012-07-11 06:51:16 +0000798 u16 len, tag;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000799 bool found = false;
800
Tony Cheneau5e968552012-07-11 06:51:16 +0000801 if (lowpan_fetch_skb_u8(skb, &slen) || /* frame length */
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000802 lowpan_fetch_skb_u16(skb, &tag)) /* fragment tag */
803 goto drop;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000804
Tony Cheneau5e968552012-07-11 06:51:16 +0000805 /* adds the 3 MSB to the 8 LSB to retrieve the 11 bits length */
806 len = ((iphc0 & 7) << 8) | slen;
807
Tony Cheneau9da29242013-03-25 17:59:27 +0000808 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 */
Tony Cheneaud991b982013-03-25 17:59:26 +0000813 if (lowpan_fetch_skb_u8(skb, &offset))
814 goto unlock_and_drop;
Tony Cheneau9da29242013-03-25 17:59:27 +0000815 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);
Tony Cheneaud991b982013-03-25 17:59:26 +0000818 }
819
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000820 /*
821 * check if frame assembling with the same tag is
822 * already in progress
823 */
alex.bluesman.smirnov@gmail.com33c34c52012-07-10 21:22:48 +0000824 spin_lock_bh(&flist_lock);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000825
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) {
Tony Cheneau9da29242013-03-25 17:59:27 +0000834 pr_debug("%s first fragment received for tag %d, "
835 "begin packet reassembly", __func__, tag);
Tony Cheneau5e968552012-07-11 06:51:16 +0000836 frame = lowpan_alloc_new_frame(skb, len, tag);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000837 if (!frame)
838 goto unlock_and_drop;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000839 }
840
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000841 /* 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 */
alex.bluesman.smirnov@gmail.com33c34c52012-07-10 21:22:48 +0000854 del_timer_sync(&frame->timer);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000855 list_del(&frame->list);
alex.bluesman.smirnov@gmail.com33c34c52012-07-10 21:22:48 +0000856 spin_unlock_bh(&flist_lock);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000857
Tony Cheneau9da29242013-03-25 17:59:27 +0000858 pr_debug("%s successfully reassembled fragment "
859 "(tag %d)", __func__, tag);
860
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000861 dev_kfree_skb(skb);
862 skb = frame->skb;
863 kfree(frame);
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000864
865 if (lowpan_fetch_skb_u8(skb, &iphc0))
Dan Carpenter747cf6e2012-06-26 20:53:09 +0000866 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000867
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000868 break;
869 }
alex.bluesman.smirnov@gmail.com33c34c52012-07-10 21:22:48 +0000870 spin_unlock_bh(&flist_lock);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000871
872 return kfree_skb(skb), 0;
873 }
874 default:
875 break;
876 }
877
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000878 if (lowpan_fetch_skb_u8(skb, &iphc1))
879 goto drop;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700880
881 _saddr = mac_cb(skb)->sa.hwaddr;
882 _daddr = mac_cb(skb)->da.hwaddr;
883
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000884 pr_debug("iphc0 = %02x, iphc1 = %02x\n", iphc0, iphc1);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700885
886 /* another if the CID flag is set */
887 if (iphc1 & LOWPAN_IPHC_CID) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000888 pr_debug("CID flag is set, increase header with one\n");
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000889 if (lowpan_fetch_skb_u8(skb, &num_context))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700890 goto drop;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700891 }
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 */
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000902 if (lowpan_fetch_skb_u8(skb, &tmp))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700903 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000904
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700905 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 */
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000916 if (lowpan_fetch_skb_u8(skb, &tmp))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700917 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000918
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700919 hdr.priority = ((tmp >> 2) & 0x0f);
920 hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700921 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 */
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000927 if (lowpan_fetch_skb_u8(skb, &tmp))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700928 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000929
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700930 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 */
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700936 break;
937 default:
938 break;
939 }
940
941 /* Next Header */
942 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
943 /* Next header is carried inline */
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000944 if (lowpan_fetch_skb_u8(skb, &(hdr.nexthdr)))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700945 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000946
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000947 pr_debug("NH flag is set, next header carried inline: %02x\n",
948 hdr.nexthdr);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700949 }
950
951 /* Hop Limit */
952 if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
953 hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
954 else {
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000955 if (lowpan_fetch_skb_u8(skb, &(hdr.hop_limit)))
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700956 goto drop;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700957 }
958
959 /* Extract SAM to the tmp variable */
960 tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
961
962 /* Source address uncompression */
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000963 pr_debug("source address stateless compression\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700964 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) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000975 pr_debug("dest: context-based mcast compression\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700976 /* TODO: implement this */
977 } else {
Alexander Aring84c2e7b2013-08-16 21:59:57 +0200978 err = lowpan_uncompress_multicast_daddr(
979 skb, &hdr.daddr, tmp);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700980 if (err)
981 goto drop;
982 }
983 } else {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000984 pr_debug("dest: stateless compression\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700985 err = lowpan_uncompress_addr(skb, &hdr.daddr, lowpan_llprefix,
986 lowpan_unc_llconf[tmp], skb->data);
987 if (err)
988 goto drop;
989 }
990
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000991 /* UDP data uncompression */
Tony Cheneauf5c20f52013-03-25 17:59:22 +0000992 if (iphc0 & LOWPAN_IPHC_NH_C) {
Tony Cheneau24363b62013-03-25 17:59:32 +0000993 struct udphdr uh;
994 struct sk_buff *new;
995 if (lowpan_uncompress_udp_header(skb, &uh))
alex.bluesman.smirnov@gmail.comf8b1b5d2011-11-10 07:40:53 +0000996 goto drop;
Tony Cheneau24363b62013-03-25 17:59:32 +0000997
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
Tony Cheneauf5c20f52013-03-25 17:59:22 +00001018 hdr.nexthdr = UIP_PROTO_UDP;
1019 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001020
1021 /* Not fragmented package */
1022 hdr.payload_len = htons(skb->len);
1023
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +00001024 pr_debug("skb headroom size = %d, data length = %d\n",
1025 skb_headroom(skb), skb->len);
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001026
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +00001027 pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
1028 "nexthdr = 0x%02x\n\thop_lim = %d\n", hdr.version,
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001029 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);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001034
1035unlock_and_drop:
alex.bluesman.smirnov@gmail.com33c34c52012-07-10 21:22:48 +00001036 spin_unlock_bh(&flist_lock);
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001037drop:
Dan Carpenter90d09632011-08-30 03:45:52 +00001038 kfree_skb(skb);
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001039 return -EINVAL;
1040}
1041
alex.bluesman.smirnov@gmail.com42c36292012-07-01 19:58:46 +00001042static 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
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001055static 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
1066static int
1067lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
Tony Cheneaud991b982013-03-25 17:59:26 +00001068 int mlen, int plen, int offset, int type)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001069{
1070 struct sk_buff *frag;
1071 int hlen, ret;
1072
Tony Cheneaud991b982013-03-25 17:59:26 +00001073 hlen = (type == LOWPAN_DISPATCH_FRAG1) ?
1074 LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001075
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
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001098 return ret;
1099}
1100
1101static int
Tony Cheneaud4ac3232013-03-25 17:59:28 +00001102lowpan_skb_fragmentation(struct sk_buff *skb, struct net_device *dev)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001103{
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;
Tony Cheneaud4ac3232013-03-25 17:59:28 +00001109 tag = lowpan_dev_info(dev)->fragment_tag++;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001110
1111 /* first fragment header */
Tony Cheneau5e968552012-07-11 06:51:16 +00001112 head[0] = LOWPAN_DISPATCH_FRAG1 | ((payload_length >> 8) & 0x7);
1113 head[1] = payload_length & 0xff;
Tony Cheneau45760392012-07-11 06:51:15 +00001114 head[2] = tag >> 8;
1115 head[3] = tag & 0xff;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001116
Tony Cheneaud991b982013-03-25 17:59:26 +00001117 err = lowpan_fragment_xmit(skb, head, header_length, LOWPAN_FRAG_SIZE,
1118 0, LOWPAN_DISPATCH_FRAG1);
1119
Tony Cheneau9da29242013-03-25 17:59:27 +00001120 if (err) {
1121 pr_debug("%s unable to send FRAG1 packet (tag: %d)",
1122 __func__, tag);
Tony Cheneaud991b982013-03-25 17:59:26 +00001123 goto exit;
Tony Cheneau9da29242013-03-25 17:59:27 +00001124 }
Tony Cheneaud991b982013-03-25 17:59:26 +00001125
1126 offset = LOWPAN_FRAG_SIZE;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001127
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,
Tony Cheneaud991b982013-03-25 17:59:26 +00001141 len, offset, LOWPAN_DISPATCH_FRAGN);
Tony Cheneau9da29242013-03-25 17:59:27 +00001142 if (err) {
1143 pr_debug("%s unable to send a subsequent FRAGN packet "
1144 "(tag: %d, offset: %d", __func__, tag, offset);
Tony Cheneaud991b982013-03-25 17:59:26 +00001145 goto exit;
Tony Cheneau9da29242013-03-25 17:59:27 +00001146 }
Tony Cheneaud991b982013-03-25 17:59:26 +00001147
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001148 offset += len;
1149 }
1150
Tony Cheneaud991b982013-03-25 17:59:26 +00001151exit:
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001152 return err;
1153}
1154
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001155static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
1156{
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001157 int err = -1;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001158
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +00001159 pr_debug("package xmit\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001160
1161 skb->dev = lowpan_dev_info(dev)->real_dev;
1162 if (skb->dev == NULL) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +00001163 pr_debug("ERROR: no real wpan device found\n");
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001164 goto error;
1165 }
1166
Alan Ottb333b7e2012-11-29 15:55:44 +00001167 /* Send directly if less than the MTU minus the 2 checksum bytes. */
1168 if (skb->len <= IEEE802154_MTU - IEEE802154_MFR_SIZE) {
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001169 err = dev_queue_xmit(skb);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001170 goto out;
1171 }
1172
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +00001173 pr_debug("frame is too big, fragmentation is needed\n");
Tony Cheneaud4ac3232013-03-25 17:59:28 +00001174 err = lowpan_skb_fragmentation(skb, dev);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001175error:
1176 dev_kfree_skb(skb);
1177out:
Alan Ottfc52eea2013-04-03 04:00:58 +00001178 if (err)
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +00001179 pr_debug("ERROR: xmit failed\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001180
Alan Ottfc52eea2013-04-03 04:00:58 +00001181 return (err < 0) ? NET_XMIT_DROP : err;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001182}
1183
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +00001184static 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
1190static 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
1196static 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
Tony Cheneauc7d0ab22013-03-25 17:59:30 +00001202static 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
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001208static struct header_ops lowpan_header_ops = {
1209 .create = lowpan_header_create,
1210};
1211
1212static const struct net_device_ops lowpan_netdev_ops = {
1213 .ndo_start_xmit = lowpan_xmit,
alex.bluesman.smirnov@gmail.com42c36292012-07-01 19:58:46 +00001214 .ndo_set_mac_address = lowpan_set_address,
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001215};
1216
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +00001217static 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,
Tony Cheneauc7d0ab22013-03-25 17:59:30 +00001221 .get_dsn = lowpan_get_dsn,
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +00001222};
1223
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001224static void lowpan_setup(struct net_device *dev)
1225{
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001226 dev->addr_len = IEEE802154_ADDR_LEN;
1227 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
1228 dev->type = ARPHRD_IEEE802154;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001229 /* 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;
alex.bluesman.smirnov@gmail.com4d039f62011-11-10 07:39:37 +00001234 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001235 dev->watchdog_timeo = 0;
1236
1237 dev->netdev_ops = &lowpan_netdev_ops;
1238 dev->header_ops = &lowpan_header_ops;
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +00001239 dev->ml_priv = &lowpan_mlme;
Alan Otta2dc375e2012-09-01 05:57:07 +00001240 dev->destructor = free_netdev;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001241}
1242
1243static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
1244{
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001245 if (tb[IFLA_ADDRESS]) {
1246 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
1247 return -EINVAL;
1248 }
1249 return 0;
1250}
1251
1252static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
1253 struct packet_type *pt, struct net_device *orig_dev)
1254{
Alan Otta437d272012-09-01 05:57:06 +00001255 struct sk_buff *local_skb;
1256
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001257 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 */
Alan Ottee21c7e2013-01-16 19:09:48 +00001264 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);
Alan Otta437d272012-09-01 05:57:06 +00001270 if (!local_skb)
1271 goto drop;
Alan Otta437d272012-09-01 05:57:06 +00001272
Alan Ottee21c7e2013-01-16 19:09:48 +00001273 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);
Alan Otta437d272012-09-01 05:57:06 +00001284 kfree_skb(skb);
Alan Ottee21c7e2013-01-16 19:09:48 +00001285 } 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 }
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +00001300 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001301
1302 return NET_RX_SUCCESS;
1303
1304drop:
1305 kfree_skb(skb);
1306 return NET_RX_DROP;
1307}
1308
1309static 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
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +00001315 pr_debug("adding new link\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001316
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;
Tony Cheneaud4ac3232013-03-25 17:59:28 +00001325 lowpan_dev_info(dev)->fragment_tag = 0;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001326 mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
1327
1328 entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
Dan Carpenterdc00fd42011-08-30 03:51:09 +00001329 if (!entry) {
1330 dev_put(real_dev);
1331 lowpan_dev_info(dev)->real_dev = NULL;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001332 return -ENOMEM;
Dan Carpenterdc00fd42011-08-30 03:51:09 +00001333 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001334
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
1347static 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;
alex.bluesman.smirnov@gmail.com8deff4a2012-04-25 23:24:57 +00001351 struct lowpan_dev_record *entry, *tmp;
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001352
1353 ASSERT_RTNL();
1354
1355 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
Dan Carpenteraec9db32011-08-30 03:46:40 +00001356 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001357 if (entry->ldev == dev) {
1358 list_del(&entry->list);
1359 kfree(entry);
1360 }
Dan Carpenteraec9db32011-08-30 03:46:40 +00001361 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001362 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
1371static 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
1380static inline int __init lowpan_netlink_init(void)
1381{
1382 return rtnl_link_register(&lowpan_link_ops);
1383}
1384
David S. Millera07fdce2013-02-06 15:54:38 -05001385static inline void lowpan_netlink_fini(void)
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001386{
1387 rtnl_link_unregister(&lowpan_link_ops);
1388}
1389
Alan Otta2dc375e2012-09-01 05:57:07 +00001390static int lowpan_device_event(struct notifier_block *unused,
Jiri Pirko351638e2013-05-28 01:30:21 +00001391 unsigned long event, void *ptr)
Alan Otta2dc375e2012-09-01 05:57:07 +00001392{
Jiri Pirko351638e2013-05-28 01:30:21 +00001393 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Alan Otta2dc375e2012-09-01 05:57:07 +00001394 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);
Peter Senna Tschudin4c835012012-09-18 07:10:43 +00001407 }
Alan Otta2dc375e2012-09-01 05:57:07 +00001408
1409out:
1410 return NOTIFY_DONE;
1411}
1412
1413static struct notifier_block lowpan_dev_notifier = {
1414 .notifier_call = lowpan_device_event,
1415};
1416
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001417static struct packet_type lowpan_packet_type = {
1418 .type = __constant_htons(ETH_P_IEEE802154),
1419 .func = lowpan_rcv,
1420};
1421
1422static int __init lowpan_init_module(void)
1423{
1424 int err = 0;
1425
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001426 err = lowpan_netlink_init();
1427 if (err < 0)
1428 goto out;
1429
1430 dev_add_pack(&lowpan_packet_type);
Alan Otta2dc375e2012-09-01 05:57:07 +00001431
1432 err = register_netdevice_notifier(&lowpan_dev_notifier);
1433 if (err < 0) {
1434 dev_remove_pack(&lowpan_packet_type);
1435 lowpan_netlink_fini();
1436 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001437out:
1438 return err;
1439}
1440
1441static void __exit lowpan_cleanup_module(void)
1442{
alex.bluesman.smirnov@gmail.com33c34c52012-07-10 21:22:48 +00001443 struct lowpan_fragment *frame, *tframe;
1444
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001445 lowpan_netlink_fini();
1446
1447 dev_remove_pack(&lowpan_packet_type);
alex.bluesman.smirnov@gmail.com33c34c52012-07-10 21:22:48 +00001448
Alan Otta2dc375e2012-09-01 05:57:07 +00001449 unregister_netdevice_notifier(&lowpan_dev_notifier);
1450
alex.bluesman.smirnov@gmail.com33c34c52012-07-10 21:22:48 +00001451 /* 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);
Alexander Smirnov44331fe2011-08-24 19:34:42 -07001463}
1464
1465module_init(lowpan_init_module);
1466module_exit(lowpan_cleanup_module);
1467MODULE_LICENSE("GPL");
1468MODULE_ALIAS_RTNL_LINK("lowpan");