blob: da78fae5b3013311f378b733300ace981068cdcf [file] [log] [blame]
Alexander Aringd57fec82014-02-28 07:32:48 +01001/* Copyright 2011, Siemens AG
Alexander Smirnov44331fe2011-08-24 19:34:42 -07002 * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
3 */
4
Alexander Aringd57fec82014-02-28 07:32:48 +01005/* Based on patches from Jon Smirl <jonsmirl@gmail.com>
Alexander Smirnov44331fe2011-08-24 19:34:42 -07006 * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Alexander Smirnov44331fe2011-08-24 19:34:42 -070016 */
17
18/* Jon's code is based on 6lowpan implementation for Contiki which is:
19 * Copyright (c) 2008, Swedish Institute of Computer Science.
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. Neither the name of the Institute nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 */
46
Alexander Smirnov44331fe2011-08-24 19:34:42 -070047#include <linux/bitops.h>
48#include <linux/if_arp.h>
49#include <linux/module.h>
50#include <linux/moduleparam.h>
51#include <linux/netdevice.h>
52#include <net/af_ieee802154.h>
53#include <net/ieee802154.h>
54#include <net/ieee802154_netdev.h>
Alexander Aringcefc8c82014-03-05 14:29:05 +010055#include <net/6lowpan.h>
Alexander Smirnov44331fe2011-08-24 19:34:42 -070056#include <net/ipv6.h>
57
Alexander Aring7240cde2014-02-28 07:32:50 +010058#include "reassembly.h"
Alexander Smirnov44331fe2011-08-24 19:34:42 -070059
Alexander Smirnov44331fe2011-08-24 19:34:42 -070060static LIST_HEAD(lowpan_devices);
Alexander Aring1ae26052014-10-13 10:33:06 +020061static int lowpan_open_count;
Alexander Smirnov44331fe2011-08-24 19:34:42 -070062
Alexander Smirnov44331fe2011-08-24 19:34:42 -070063/* private device info */
64struct lowpan_dev_info {
65 struct net_device *real_dev; /* real WPAN device ptr */
66 struct mutex dev_list_mtx; /* mutex for list ops */
Alexander Aringcd97a712014-10-06 11:00:50 +020067 u16 fragment_tag;
Alexander Smirnov44331fe2011-08-24 19:34:42 -070068};
69
70struct lowpan_dev_record {
71 struct net_device *ldev;
72 struct list_head list;
73};
74
Simon Vincentf19f4f92014-09-24 12:21:33 +020075/* don't save pan id, it's intra pan */
76struct lowpan_addr {
77 u8 mode;
78 union {
79 /* IPv6 needs big endian here */
80 __be64 extended_addr;
81 __be16 short_addr;
82 } u;
83};
84
85struct lowpan_addr_info {
86 struct lowpan_addr daddr;
87 struct lowpan_addr saddr;
88};
89
Alexander Smirnov44331fe2011-08-24 19:34:42 -070090static inline struct
91lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
92{
93 return netdev_priv(dev);
94}
95
Simon Vincentf19f4f92014-09-24 12:21:33 +020096static inline struct
97lowpan_addr_info *lowpan_skb_priv(const struct sk_buff *skb)
98{
99 WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct lowpan_addr_info));
100 return (struct lowpan_addr_info *)(skb->data -
101 sizeof(struct lowpan_addr_info));
102}
103
Varka Bhadram4710d802014-07-02 09:01:09 +0530104static int lowpan_header_create(struct sk_buff *skb, struct net_device *dev,
105 unsigned short type, const void *_daddr,
106 const void *_saddr, unsigned int len)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700107{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700108 const u8 *saddr = _saddr;
109 const u8 *daddr = _daddr;
Simon Vincentf19f4f92014-09-24 12:21:33 +0200110 struct lowpan_addr_info *info;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700111
Alexander Aringfc4e98d2013-02-05 10:23:43 +0000112 /* TODO:
113 * if this package isn't ipv6 one, where should it be routed?
114 */
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700115 if (type != ETH_P_IPV6)
116 return 0;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700117
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700118 if (!saddr)
119 saddr = dev->dev_addr;
120
Alexander Aring841a5ec2013-12-12 20:15:25 +0100121 raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
122 raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700123
Simon Vincentf19f4f92014-09-24 12:21:33 +0200124 info = lowpan_skb_priv(skb);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700125
Simon Vincentf19f4f92014-09-24 12:21:33 +0200126 /* TODO: Currently we only support extended_addr */
127 info->daddr.mode = IEEE802154_ADDR_LONG;
128 memcpy(&info->daddr.u.extended_addr, daddr,
129 sizeof(info->daddr.u.extended_addr));
130 info->saddr.mode = IEEE802154_ADDR_LONG;
131 memcpy(&info->saddr.u.extended_addr, saddr,
132 sizeof(info->daddr.u.extended_addr));
Tony Cheneau58ef67c2013-03-25 17:59:25 +0000133
Simon Vincentf19f4f92014-09-24 12:21:33 +0200134 return 0;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700135}
136
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200137static int lowpan_give_skb_to_devices(struct sk_buff *skb,
Varka Bhadram4710d802014-07-02 09:01:09 +0530138 struct net_device *dev)
Alan Ott0c446212013-01-16 19:09:47 +0000139{
140 struct lowpan_dev_record *entry;
141 struct sk_buff *skb_cp;
142 int stat = NET_RX_SUCCESS;
143
144 rcu_read_lock();
145 list_for_each_entry_rcu(entry, &lowpan_devices, list)
146 if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) {
147 skb_cp = skb_copy(skb, GFP_ATOMIC);
148 if (!skb_cp) {
149 stat = -ENOMEM;
150 break;
151 }
152
153 skb_cp->dev = entry->ldev;
154 stat = netif_rx(skb_cp);
155 }
156 rcu_read_unlock();
157
158 return stat;
159}
160
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100161static int process_data(struct sk_buff *skb, const struct ieee802154_hdr *hdr)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700162{
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200163 u8 iphc0, iphc1;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100164 struct ieee802154_addr_sa sa, da;
165 void *sap, *dap;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700166
Alexander Aring841a5ec2013-12-12 20:15:25 +0100167 raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700168 /* at least two bytes will be used for the encoding */
169 if (skb->len < 2)
170 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000171
172 if (lowpan_fetch_skb_u8(skb, &iphc0))
173 goto drop;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000174
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000175 if (lowpan_fetch_skb_u8(skb, &iphc1))
176 goto drop;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700177
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100178 ieee802154_addr_to_sa(&sa, &hdr->source);
179 ieee802154_addr_to_sa(&da, &hdr->dest);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700180
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100181 if (sa.addr_type == IEEE802154_ADDR_SHORT)
182 sap = &sa.short_addr;
183 else
184 sap = &sa.hwaddr;
185
186 if (da.addr_type == IEEE802154_ADDR_SHORT)
187 dap = &da.short_addr;
188 else
189 dap = &da.hwaddr;
190
191 return lowpan_process_data(skb, skb->dev, sap, sa.addr_type,
192 IEEE802154_ADDR_LEN, dap, da.addr_type,
193 IEEE802154_ADDR_LEN, iphc0, iphc1,
194 lowpan_give_skb_to_devices);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000195
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700196drop:
Dan Carpenter90d09632011-08-30 03:45:52 +0000197 kfree_skb(skb);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700198 return -EINVAL;
199}
200
alex.bluesman.smirnov@gmail.com42c36292012-07-01 19:58:46 +0000201static int lowpan_set_address(struct net_device *dev, void *p)
202{
203 struct sockaddr *sa = p;
204
205 if (netif_running(dev))
206 return -EBUSY;
207
208 /* TODO: validate addr */
209 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
210
211 return 0;
212}
213
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200214static struct sk_buff*
215lowpan_alloc_frag(struct sk_buff *skb, int size,
216 const struct ieee802154_hdr *master_hdr)
217{
218 struct net_device *real_dev = lowpan_dev_info(skb->dev)->real_dev;
219 struct sk_buff *frag;
220 int rc;
221
222 frag = alloc_skb(real_dev->hard_header_len +
223 real_dev->needed_tailroom + size,
224 GFP_ATOMIC);
225
226 if (likely(frag)) {
227 frag->dev = real_dev;
228 frag->priority = skb->priority;
229 skb_reserve(frag, real_dev->hard_header_len);
230 skb_reset_network_header(frag);
231 *mac_cb(frag) = *mac_cb(skb);
232
233 rc = dev_hard_header(frag, real_dev, 0, &master_hdr->dest,
234 &master_hdr->source, size);
235 if (rc < 0) {
236 kfree_skb(frag);
Alexander Aringc0bffc72014-10-06 11:00:52 +0200237 return ERR_PTR(rc);
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200238 }
239 } else {
Alexander Aringc4cb9012014-08-19 19:03:29 +0200240 frag = ERR_PTR(-ENOMEM);
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200241 }
242
243 return frag;
244}
245
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000246static int
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200247lowpan_xmit_fragment(struct sk_buff *skb, const struct ieee802154_hdr *wpan_hdr,
248 u8 *frag_hdr, int frag_hdrlen,
249 int offset, int len)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000250{
251 struct sk_buff *frag;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000252
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200253 raw_dump_inline(__func__, " fragment header", frag_hdr, frag_hdrlen);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000254
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200255 frag = lowpan_alloc_frag(skb, frag_hdrlen + len, wpan_hdr);
256 if (IS_ERR(frag))
257 return -PTR_ERR(frag);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000258
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200259 memcpy(skb_put(frag, frag_hdrlen), frag_hdr, frag_hdrlen);
260 memcpy(skb_put(frag, len), skb_network_header(skb) + offset, len);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000261
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200262 raw_dump_table(__func__, " fragment dump", frag->data, frag->len);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000263
Alexander Aring545f3612013-10-28 10:24:16 +0100264 return dev_queue_xmit(frag);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000265}
266
267static int
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200268lowpan_xmit_fragmented(struct sk_buff *skb, struct net_device *dev,
269 const struct ieee802154_hdr *wpan_hdr)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000270{
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200271 u16 dgram_size, dgram_offset;
272 __be16 frag_tag;
273 u8 frag_hdr[5];
274 int frag_cap, frag_len, payload_cap, rc;
275 int skb_unprocessed, skb_offset;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000276
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100277 dgram_size = lowpan_uncompress_size(skb, &dgram_offset) -
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200278 skb->mac_len;
Alexander Aringcd97a712014-10-06 11:00:50 +0200279 frag_tag = htons(lowpan_dev_info(dev)->fragment_tag);
280 lowpan_dev_info(dev)->fragment_tag++;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000281
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200282 frag_hdr[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x07);
283 frag_hdr[1] = dgram_size & 0xff;
284 memcpy(frag_hdr + 2, &frag_tag, sizeof(frag_tag));
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000285
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200286 payload_cap = ieee802154_max_payload(wpan_hdr);
Tony Cheneaud991b982013-03-25 17:59:26 +0000287
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200288 frag_len = round_down(payload_cap - LOWPAN_FRAG1_HEAD_SIZE -
289 skb_network_header_len(skb), 8);
290
291 skb_offset = skb_network_header_len(skb);
292 skb_unprocessed = skb->len - skb->mac_len - skb_offset;
293
294 rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
295 LOWPAN_FRAG1_HEAD_SIZE, 0,
296 frag_len + skb_network_header_len(skb));
297 if (rc) {
Tony Cheneau9da29242013-03-25 17:59:27 +0000298 pr_debug("%s unable to send FRAG1 packet (tag: %d)",
Alexander Aringcd97a712014-10-06 11:00:50 +0200299 __func__, ntohs(frag_tag));
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200300 goto err;
Tony Cheneau9da29242013-03-25 17:59:27 +0000301 }
Tony Cheneaud991b982013-03-25 17:59:26 +0000302
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200303 frag_hdr[0] &= ~LOWPAN_DISPATCH_FRAG1;
304 frag_hdr[0] |= LOWPAN_DISPATCH_FRAGN;
305 frag_cap = round_down(payload_cap - LOWPAN_FRAGN_HEAD_SIZE, 8);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000306
Alexander Aring51263ff2014-06-02 13:21:57 +0200307 do {
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200308 dgram_offset += frag_len;
309 skb_offset += frag_len;
310 skb_unprocessed -= frag_len;
311 frag_len = min(frag_cap, skb_unprocessed);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000312
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200313 frag_hdr[4] = dgram_offset >> 3;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000314
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200315 rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
316 LOWPAN_FRAGN_HEAD_SIZE, skb_offset,
317 frag_len);
318 if (rc) {
Alexander Aringd57fec82014-02-28 07:32:48 +0100319 pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n",
Alexander Aringcd97a712014-10-06 11:00:50 +0200320 __func__, ntohs(frag_tag), skb_offset);
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200321 goto err;
Tony Cheneau9da29242013-03-25 17:59:27 +0000322 }
Alexander Aringeb064812014-06-02 13:21:58 +0200323 } while (skb_unprocessed > frag_cap);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000324
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200325 consume_skb(skb);
326 return NET_XMIT_SUCCESS;
327
328err:
329 kfree_skb(skb);
330 return rc;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000331}
332
Simon Vincentf19f4f92014-09-24 12:21:33 +0200333static int lowpan_header(struct sk_buff *skb, struct net_device *dev)
334{
335 struct ieee802154_addr sa, da;
336 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
337 struct lowpan_addr_info info;
338 void *daddr, *saddr;
339
340 memcpy(&info, lowpan_skb_priv(skb), sizeof(info));
341
342 /* TODO: Currently we only support extended_addr */
343 daddr = &info.daddr.u.extended_addr;
344 saddr = &info.saddr.u.extended_addr;
345
346 lowpan_header_compress(skb, dev, ETH_P_IPV6, daddr, saddr, skb->len);
347
348 cb->type = IEEE802154_FC_TYPE_DATA;
349
350 /* prepare wpan address data */
351 sa.mode = IEEE802154_ADDR_LONG;
352 sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
353 sa.extended_addr = ieee802154_devaddr_from_raw(saddr);
354
355 /* intra-PAN communications */
356 da.pan_id = sa.pan_id;
357
358 /* if the destination address is the broadcast address, use the
359 * corresponding short address
360 */
361 if (lowpan_is_addr_broadcast((const u8 *)daddr)) {
362 da.mode = IEEE802154_ADDR_SHORT;
363 da.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
364 cb->ackreq = false;
365 } else {
366 da.mode = IEEE802154_ADDR_LONG;
367 da.extended_addr = ieee802154_devaddr_from_raw(daddr);
368 cb->ackreq = true;
369 }
370
371 return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
372 ETH_P_IPV6, (void *)&da, (void *)&sa, 0);
373}
374
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700375static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
376{
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200377 struct ieee802154_hdr wpan_hdr;
Simon Vincentf19f4f92014-09-24 12:21:33 +0200378 int max_single, ret;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700379
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000380 pr_debug("package xmit\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700381
Simon Vincentf19f4f92014-09-24 12:21:33 +0200382 /* We must take a copy of the skb before we modify/replace the ipv6
383 * header as the header could be used elsewhere
384 */
385 skb = skb_unshare(skb, GFP_ATOMIC);
386 if (!skb)
387 return NET_XMIT_DROP;
388
389 ret = lowpan_header(skb, dev);
390 if (ret < 0) {
391 kfree_skb(skb);
392 return NET_XMIT_DROP;
393 }
394
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200395 if (ieee802154_hdr_peek(skb, &wpan_hdr) < 0) {
396 kfree_skb(skb);
397 return NET_XMIT_DROP;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000398 }
399
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200400 max_single = ieee802154_max_payload(&wpan_hdr);
401
402 if (skb_tail_pointer(skb) - skb_network_header(skb) <= max_single) {
403 skb->dev = lowpan_dev_info(dev)->real_dev;
404 return dev_queue_xmit(skb);
405 } else {
406 netdev_tx_t rc;
407
408 pr_debug("frame is too big, fragmentation is needed\n");
409 rc = lowpan_xmit_fragmented(skb, dev, &wpan_hdr);
410
411 return rc < 0 ? NET_XMIT_DROP : rc;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000412 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700413}
414
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000415static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
416{
417 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
Varka Bhadram4710d802014-07-02 09:01:09 +0530418
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000419 return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
420}
421
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100422static __le16 lowpan_get_pan_id(const struct net_device *dev)
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000423{
424 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
Varka Bhadram4710d802014-07-02 09:01:09 +0530425
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000426 return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
427}
428
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100429static __le16 lowpan_get_short_addr(const struct net_device *dev)
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000430{
431 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
Varka Bhadram4710d802014-07-02 09:01:09 +0530432
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000433 return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
434}
435
Tony Cheneauc7d0ab22013-03-25 17:59:30 +0000436static u8 lowpan_get_dsn(const struct net_device *dev)
437{
438 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
Varka Bhadram4710d802014-07-02 09:01:09 +0530439
Tony Cheneauc7d0ab22013-03-25 17:59:30 +0000440 return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev);
441}
442
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700443static struct header_ops lowpan_header_ops = {
444 .create = lowpan_header_create,
445};
446
Eric Dumazet20e7c4e82014-02-10 11:42:35 -0800447static struct lock_class_key lowpan_tx_busylock;
448static struct lock_class_key lowpan_netdev_xmit_lock_key;
449
450static void lowpan_set_lockdep_class_one(struct net_device *dev,
451 struct netdev_queue *txq,
452 void *_unused)
453{
454 lockdep_set_class(&txq->_xmit_lock,
455 &lowpan_netdev_xmit_lock_key);
456}
457
458
459static int lowpan_dev_init(struct net_device *dev)
460{
461 netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL);
462 dev->qdisc_tx_busylock = &lowpan_tx_busylock;
463 return 0;
464}
465
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700466static const struct net_device_ops lowpan_netdev_ops = {
Eric Dumazet20e7c4e82014-02-10 11:42:35 -0800467 .ndo_init = lowpan_dev_init,
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700468 .ndo_start_xmit = lowpan_xmit,
alex.bluesman.smirnov@gmail.com42c36292012-07-01 19:58:46 +0000469 .ndo_set_mac_address = lowpan_set_address,
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700470};
471
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000472static struct ieee802154_mlme_ops lowpan_mlme = {
473 .get_pan_id = lowpan_get_pan_id,
474 .get_phy = lowpan_get_phy,
475 .get_short_addr = lowpan_get_short_addr,
Tony Cheneauc7d0ab22013-03-25 17:59:30 +0000476 .get_dsn = lowpan_get_dsn,
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000477};
478
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700479static void lowpan_setup(struct net_device *dev)
480{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700481 dev->addr_len = IEEE802154_ADDR_LEN;
482 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
483 dev->type = ARPHRD_IEEE802154;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700484 /* Frame Control + Sequence Number + Address fields + Security Header */
485 dev->hard_header_len = 2 + 1 + 20 + 14;
486 dev->needed_tailroom = 2; /* FCS */
Alexander Aring685d6322014-08-19 19:03:31 +0200487 dev->mtu = IPV6_MIN_MTU;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700488 dev->tx_queue_len = 0;
alex.bluesman.smirnov@gmail.com4d039f62011-11-10 07:39:37 +0000489 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700490 dev->watchdog_timeo = 0;
491
492 dev->netdev_ops = &lowpan_netdev_ops;
493 dev->header_ops = &lowpan_header_ops;
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000494 dev->ml_priv = &lowpan_mlme;
Alan Otta2dc375e2012-09-01 05:57:07 +0000495 dev->destructor = free_netdev;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700496}
497
498static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
499{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700500 if (tb[IFLA_ADDRESS]) {
501 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
502 return -EINVAL;
503 }
504 return 0;
505}
506
507static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
Varka Bhadram4710d802014-07-02 09:01:09 +0530508 struct packet_type *pt, struct net_device *orig_dev)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700509{
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100510 struct ieee802154_hdr hdr;
Alexander Aring7240cde2014-02-28 07:32:50 +0100511 int ret;
Alan Otta437d272012-09-01 05:57:06 +0000512
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100513 skb = skb_share_check(skb, GFP_ATOMIC);
514 if (!skb)
515 goto drop;
516
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700517 if (!netif_running(dev))
Alexander Aring7240cde2014-02-28 07:32:50 +0100518 goto drop_skb;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700519
Simon Vincent39f6eb12014-10-05 20:23:12 +0200520 if (skb->pkt_type == PACKET_OTHERHOST)
521 goto drop_skb;
522
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700523 if (dev->type != ARPHRD_IEEE802154)
Alexander Aring7240cde2014-02-28 07:32:50 +0100524 goto drop_skb;
525
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100526 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
527 goto drop_skb;
528
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700529 /* check that it's our buffer */
Alan Ottee21c7e2013-01-16 19:09:48 +0000530 if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100531 skb->protocol = htons(ETH_P_IPV6);
532 skb->pkt_type = PACKET_HOST;
Alan Ottee21c7e2013-01-16 19:09:48 +0000533
534 /* Pull off the 1-byte of 6lowpan header. */
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100535 skb_pull(skb, 1);
Alan Ottee21c7e2013-01-16 19:09:48 +0000536
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100537 ret = lowpan_give_skb_to_devices(skb, NULL);
Alexander Aring7240cde2014-02-28 07:32:50 +0100538 if (ret == NET_RX_DROP)
539 goto drop;
Alan Ottee21c7e2013-01-16 19:09:48 +0000540 } else {
541 switch (skb->data[0] & 0xe0) {
542 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100543 ret = process_data(skb, &hdr);
Alexander Aring7240cde2014-02-28 07:32:50 +0100544 if (ret == NET_RX_DROP)
Alan Ottee21c7e2013-01-16 19:09:48 +0000545 goto drop;
Alexander Aring7240cde2014-02-28 07:32:50 +0100546 break;
547 case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100548 ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1);
Alexander Aring7240cde2014-02-28 07:32:50 +0100549 if (ret == 1) {
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100550 ret = process_data(skb, &hdr);
Alexander Aring7240cde2014-02-28 07:32:50 +0100551 if (ret == NET_RX_DROP)
552 goto drop;
553 }
554 break;
555 case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100556 ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN);
Alexander Aring7240cde2014-02-28 07:32:50 +0100557 if (ret == 1) {
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100558 ret = process_data(skb, &hdr);
Alexander Aring7240cde2014-02-28 07:32:50 +0100559 if (ret == NET_RX_DROP)
560 goto drop;
561 }
Alan Ottee21c7e2013-01-16 19:09:48 +0000562 break;
563 default:
564 break;
565 }
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000566 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700567
568 return NET_RX_SUCCESS;
Alexander Aring7240cde2014-02-28 07:32:50 +0100569drop_skb:
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700570 kfree_skb(skb);
Alexander Aring7240cde2014-02-28 07:32:50 +0100571drop:
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700572 return NET_RX_DROP;
573}
574
Alexander Aring1ae26052014-10-13 10:33:06 +0200575static struct packet_type lowpan_packet_type = {
576 .type = htons(ETH_P_IEEE802154),
577 .func = lowpan_rcv,
578};
579
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700580static int lowpan_newlink(struct net *src_net, struct net_device *dev,
581 struct nlattr *tb[], struct nlattr *data[])
582{
583 struct net_device *real_dev;
584 struct lowpan_dev_record *entry;
Alexander Aring1ae26052014-10-13 10:33:06 +0200585 int ret;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700586
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000587 pr_debug("adding new link\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700588
589 if (!tb[IFLA_LINK])
590 return -EINVAL;
591 /* find and hold real wpan device */
592 real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
593 if (!real_dev)
594 return -ENODEV;
Dan Carpenter78032f92013-11-07 10:44:45 +0300595 if (real_dev->type != ARPHRD_IEEE802154) {
596 dev_put(real_dev);
Alan Ott7adac1e2013-10-05 23:15:18 -0400597 return -EINVAL;
Dan Carpenter78032f92013-11-07 10:44:45 +0300598 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700599
600 lowpan_dev_info(dev)->real_dev = real_dev;
601 mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
602
Alexander Aringd57fec82014-02-28 07:32:48 +0100603 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Dan Carpenterdc00fd42011-08-30 03:51:09 +0000604 if (!entry) {
605 dev_put(real_dev);
606 lowpan_dev_info(dev)->real_dev = NULL;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700607 return -ENOMEM;
Dan Carpenterdc00fd42011-08-30 03:51:09 +0000608 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700609
610 entry->ldev = dev;
611
Alan Ottab2d95d2013-10-05 23:15:19 -0400612 /* Set the lowpan harware address to the wpan hardware address. */
613 memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN);
614
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700615 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
616 INIT_LIST_HEAD(&entry->list);
617 list_add_tail(&entry->list, &lowpan_devices);
618 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
619
Alexander Aring1ae26052014-10-13 10:33:06 +0200620 ret = register_netdevice(dev);
621 if (ret >= 0) {
622 if (!lowpan_open_count)
623 dev_add_pack(&lowpan_packet_type);
624 lowpan_open_count++;
625 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700626
Alexander Aring1ae26052014-10-13 10:33:06 +0200627 return ret;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700628}
629
630static void lowpan_dellink(struct net_device *dev, struct list_head *head)
631{
632 struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
633 struct net_device *real_dev = lowpan_dev->real_dev;
alex.bluesman.smirnov@gmail.com8deff4a2012-04-25 23:24:57 +0000634 struct lowpan_dev_record *entry, *tmp;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700635
636 ASSERT_RTNL();
637
Alexander Aring1ae26052014-10-13 10:33:06 +0200638 lowpan_open_count--;
639 if (!lowpan_open_count)
640 dev_remove_pack(&lowpan_packet_type);
641
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700642 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
Dan Carpenteraec9db32011-08-30 03:46:40 +0000643 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700644 if (entry->ldev == dev) {
645 list_del(&entry->list);
646 kfree(entry);
647 }
Dan Carpenteraec9db32011-08-30 03:46:40 +0000648 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700649 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
650
651 mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
652
653 unregister_netdevice_queue(dev, head);
654
655 dev_put(real_dev);
656}
657
658static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
659 .kind = "lowpan",
660 .priv_size = sizeof(struct lowpan_dev_info),
661 .setup = lowpan_setup,
662 .newlink = lowpan_newlink,
663 .dellink = lowpan_dellink,
664 .validate = lowpan_validate,
665};
666
667static inline int __init lowpan_netlink_init(void)
668{
669 return rtnl_link_register(&lowpan_link_ops);
670}
671
David S. Millera07fdce2013-02-06 15:54:38 -0500672static inline void lowpan_netlink_fini(void)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700673{
674 rtnl_link_unregister(&lowpan_link_ops);
675}
676
Alan Otta2dc375e2012-09-01 05:57:07 +0000677static int lowpan_device_event(struct notifier_block *unused,
Jiri Pirko351638e2013-05-28 01:30:21 +0000678 unsigned long event, void *ptr)
Alan Otta2dc375e2012-09-01 05:57:07 +0000679{
Jiri Pirko351638e2013-05-28 01:30:21 +0000680 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Alan Otta2dc375e2012-09-01 05:57:07 +0000681 LIST_HEAD(del_list);
682 struct lowpan_dev_record *entry, *tmp;
683
684 if (dev->type != ARPHRD_IEEE802154)
685 goto out;
686
687 if (event == NETDEV_UNREGISTER) {
688 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
689 if (lowpan_dev_info(entry->ldev)->real_dev == dev)
690 lowpan_dellink(entry->ldev, &del_list);
691 }
692
693 unregister_netdevice_many(&del_list);
Peter Senna Tschudin4c835012012-09-18 07:10:43 +0000694 }
Alan Otta2dc375e2012-09-01 05:57:07 +0000695
696out:
697 return NOTIFY_DONE;
698}
699
700static struct notifier_block lowpan_dev_notifier = {
701 .notifier_call = lowpan_device_event,
702};
703
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700704static int __init lowpan_init_module(void)
705{
706 int err = 0;
707
Alexander Aring7240cde2014-02-28 07:32:50 +0100708 err = lowpan_net_frag_init();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700709 if (err < 0)
710 goto out;
711
Alexander Aring7240cde2014-02-28 07:32:50 +0100712 err = lowpan_netlink_init();
713 if (err < 0)
714 goto out_frag;
715
Alan Otta2dc375e2012-09-01 05:57:07 +0000716 err = register_netdevice_notifier(&lowpan_dev_notifier);
Alexander Aring7240cde2014-02-28 07:32:50 +0100717 if (err < 0)
718 goto out_pack;
719
720 return 0;
721
722out_pack:
Alexander Aring7240cde2014-02-28 07:32:50 +0100723 lowpan_netlink_fini();
724out_frag:
725 lowpan_net_frag_exit();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700726out:
727 return err;
728}
729
730static void __exit lowpan_cleanup_module(void)
731{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700732 lowpan_netlink_fini();
733
Alexander Aring7240cde2014-02-28 07:32:50 +0100734 lowpan_net_frag_exit();
Alan Otta2dc375e2012-09-01 05:57:07 +0000735
Alexander Aring7240cde2014-02-28 07:32:50 +0100736 unregister_netdevice_notifier(&lowpan_dev_notifier);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700737}
738
739module_init(lowpan_init_module);
740module_exit(lowpan_cleanup_module);
741MODULE_LICENSE("GPL");
742MODULE_ALIAS_RTNL_LINK("lowpan");