blob: fe6bd7a7108169138faf198fefcab7f3217e9c55 [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);
61
Alexander Smirnov44331fe2011-08-24 19:34:42 -070062/* private device info */
63struct lowpan_dev_info {
64 struct net_device *real_dev; /* real WPAN device ptr */
65 struct mutex dev_list_mtx; /* mutex for list ops */
Alexander Aring02600d02014-02-28 07:32:46 +010066 __be16 fragment_tag;
Alexander Smirnov44331fe2011-08-24 19:34:42 -070067};
68
69struct lowpan_dev_record {
70 struct net_device *ldev;
71 struct list_head list;
72};
73
74static inline struct
75lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
76{
77 return netdev_priv(dev);
78}
79
80static inline void lowpan_address_flip(u8 *src, u8 *dest)
81{
82 int i;
83 for (i = 0; i < IEEE802154_ADDR_LEN; i++)
84 (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i];
85}
86
Alexander Smirnov44331fe2011-08-24 19:34:42 -070087static int lowpan_header_create(struct sk_buff *skb,
88 struct net_device *dev,
89 unsigned short type, const void *_daddr,
Eric Dumazet95c96172012-04-15 05:58:06 +000090 const void *_saddr, unsigned int len)
Alexander Smirnov44331fe2011-08-24 19:34:42 -070091{
Alexander Smirnov44331fe2011-08-24 19:34:42 -070092 const u8 *saddr = _saddr;
93 const u8 *daddr = _daddr;
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010094 struct ieee802154_addr sa, da;
Phoebe Buckheister32edc402014-05-14 17:43:08 +020095 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
Alexander Smirnov44331fe2011-08-24 19:34:42 -070096
Alexander Aringfc4e98d2013-02-05 10:23:43 +000097 /* TODO:
98 * if this package isn't ipv6 one, where should it be routed?
99 */
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700100 if (type != ETH_P_IPV6)
101 return 0;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700102
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700103 if (!saddr)
104 saddr = dev->dev_addr;
105
Alexander Aring841a5ec2013-12-12 20:15:25 +0100106 raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
107 raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700108
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200109 lowpan_header_compress(skb, dev, type, daddr, saddr, len);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700110
Alexander Aringd57fec82014-02-28 07:32:48 +0100111 /* NOTE1: I'm still unsure about the fact that compression and WPAN
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700112 * header are created here and not later in the xmit. So wait for
113 * an opinion of net maintainers.
114 */
Alexander Aringd57fec82014-02-28 07:32:48 +0100115 /* NOTE2: to be absolutely correct, we must derive PANid information
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700116 * from MAC subif of the 'dev' and 'real_dev' network devices, but
117 * this isn't implemented in mainline yet, so currently we assign 0xff
118 */
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200119 cb->type = IEEE802154_FC_TYPE_DATA;
Tony Cheneau58ef67c2013-03-25 17:59:25 +0000120
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200121 /* prepare wpan address data */
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100122 sa.mode = IEEE802154_ADDR_LONG;
123 sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
124 sa.extended_addr = ieee802154_devaddr_from_raw(saddr);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700125
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200126 /* intra-PAN communications */
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100127 da.pan_id = sa.pan_id;
Tony Cheneau43de7aa2013-03-25 17:59:31 +0000128
Alexander Aringd57fec82014-02-28 07:32:48 +0100129 /* if the destination address is the broadcast address, use the
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200130 * corresponding short address
131 */
132 if (lowpan_is_addr_broadcast(daddr)) {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100133 da.mode = IEEE802154_ADDR_SHORT;
134 da.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200135 } else {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100136 da.mode = IEEE802154_ADDR_LONG;
137 da.extended_addr = ieee802154_devaddr_from_raw(daddr);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700138 }
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200139
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200140 cb->ackreq = !lowpan_is_addr_broadcast(daddr);
141
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200142 return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
Phoebe Buckheisterd1d73582014-03-14 21:24:04 +0100143 type, (void *)&da, (void *)&sa, 0);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700144}
145
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200146static int lowpan_give_skb_to_devices(struct sk_buff *skb,
147 struct net_device *dev)
Alan Ott0c446212013-01-16 19:09:47 +0000148{
149 struct lowpan_dev_record *entry;
150 struct sk_buff *skb_cp;
151 int stat = NET_RX_SUCCESS;
152
153 rcu_read_lock();
154 list_for_each_entry_rcu(entry, &lowpan_devices, list)
155 if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) {
156 skb_cp = skb_copy(skb, GFP_ATOMIC);
157 if (!skb_cp) {
158 stat = -ENOMEM;
159 break;
160 }
161
162 skb_cp->dev = entry->ldev;
163 stat = netif_rx(skb_cp);
164 }
165 rcu_read_unlock();
166
167 return stat;
168}
169
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100170static int process_data(struct sk_buff *skb, const struct ieee802154_hdr *hdr)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700171{
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200172 u8 iphc0, iphc1;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100173 struct ieee802154_addr_sa sa, da;
174 void *sap, *dap;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700175
Alexander Aring841a5ec2013-12-12 20:15:25 +0100176 raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700177 /* at least two bytes will be used for the encoding */
178 if (skb->len < 2)
179 goto drop;
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000180
181 if (lowpan_fetch_skb_u8(skb, &iphc0))
182 goto drop;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000183
alex.bluesman.smirnov@gmail.comc5d36872012-06-25 03:49:01 +0000184 if (lowpan_fetch_skb_u8(skb, &iphc1))
185 goto drop;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700186
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100187 ieee802154_addr_to_sa(&sa, &hdr->source);
188 ieee802154_addr_to_sa(&da, &hdr->dest);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700189
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100190 if (sa.addr_type == IEEE802154_ADDR_SHORT)
191 sap = &sa.short_addr;
192 else
193 sap = &sa.hwaddr;
194
195 if (da.addr_type == IEEE802154_ADDR_SHORT)
196 dap = &da.short_addr;
197 else
198 dap = &da.hwaddr;
199
200 return lowpan_process_data(skb, skb->dev, sap, sa.addr_type,
201 IEEE802154_ADDR_LEN, dap, da.addr_type,
202 IEEE802154_ADDR_LEN, iphc0, iphc1,
203 lowpan_give_skb_to_devices);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000204
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700205drop:
Dan Carpenter90d09632011-08-30 03:45:52 +0000206 kfree_skb(skb);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700207 return -EINVAL;
208}
209
alex.bluesman.smirnov@gmail.com42c36292012-07-01 19:58:46 +0000210static int lowpan_set_address(struct net_device *dev, void *p)
211{
212 struct sockaddr *sa = p;
213
214 if (netif_running(dev))
215 return -EBUSY;
216
217 /* TODO: validate addr */
218 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
219
220 return 0;
221}
222
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200223static struct sk_buff*
224lowpan_alloc_frag(struct sk_buff *skb, int size,
225 const struct ieee802154_hdr *master_hdr)
226{
227 struct net_device *real_dev = lowpan_dev_info(skb->dev)->real_dev;
228 struct sk_buff *frag;
229 int rc;
230
231 frag = alloc_skb(real_dev->hard_header_len +
232 real_dev->needed_tailroom + size,
233 GFP_ATOMIC);
234
235 if (likely(frag)) {
236 frag->dev = real_dev;
237 frag->priority = skb->priority;
238 skb_reserve(frag, real_dev->hard_header_len);
239 skb_reset_network_header(frag);
240 *mac_cb(frag) = *mac_cb(skb);
241
242 rc = dev_hard_header(frag, real_dev, 0, &master_hdr->dest,
243 &master_hdr->source, size);
244 if (rc < 0) {
245 kfree_skb(frag);
246 return ERR_PTR(-rc);
247 }
248 } else {
249 frag = ERR_PTR(ENOMEM);
250 }
251
252 return frag;
253}
254
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000255static int
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200256lowpan_xmit_fragment(struct sk_buff *skb, const struct ieee802154_hdr *wpan_hdr,
257 u8 *frag_hdr, int frag_hdrlen,
258 int offset, int len)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000259{
260 struct sk_buff *frag;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000261
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200262 raw_dump_inline(__func__, " fragment header", frag_hdr, frag_hdrlen);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000263
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200264 frag = lowpan_alloc_frag(skb, frag_hdrlen + len, wpan_hdr);
265 if (IS_ERR(frag))
266 return -PTR_ERR(frag);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000267
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200268 memcpy(skb_put(frag, frag_hdrlen), frag_hdr, frag_hdrlen);
269 memcpy(skb_put(frag, len), skb_network_header(skb) + offset, len);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000270
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200271 raw_dump_table(__func__, " fragment dump", frag->data, frag->len);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000272
Alexander Aring545f3612013-10-28 10:24:16 +0100273 return dev_queue_xmit(frag);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000274}
275
276static int
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200277lowpan_xmit_fragmented(struct sk_buff *skb, struct net_device *dev,
278 const struct ieee802154_hdr *wpan_hdr)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000279{
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200280 u16 dgram_size, dgram_offset;
281 __be16 frag_tag;
282 u8 frag_hdr[5];
283 int frag_cap, frag_len, payload_cap, rc;
284 int skb_unprocessed, skb_offset;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000285
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100286 dgram_size = lowpan_uncompress_size(skb, &dgram_offset) -
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200287 skb->mac_len;
288 frag_tag = lowpan_dev_info(dev)->fragment_tag++;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000289
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200290 frag_hdr[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x07);
291 frag_hdr[1] = dgram_size & 0xff;
292 memcpy(frag_hdr + 2, &frag_tag, sizeof(frag_tag));
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000293
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200294 payload_cap = ieee802154_max_payload(wpan_hdr);
Tony Cheneaud991b982013-03-25 17:59:26 +0000295
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200296 frag_len = round_down(payload_cap - LOWPAN_FRAG1_HEAD_SIZE -
297 skb_network_header_len(skb), 8);
298
299 skb_offset = skb_network_header_len(skb);
300 skb_unprocessed = skb->len - skb->mac_len - skb_offset;
301
302 rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
303 LOWPAN_FRAG1_HEAD_SIZE, 0,
304 frag_len + skb_network_header_len(skb));
305 if (rc) {
Tony Cheneau9da29242013-03-25 17:59:27 +0000306 pr_debug("%s unable to send FRAG1 packet (tag: %d)",
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200307 __func__, frag_tag);
308 goto err;
Tony Cheneau9da29242013-03-25 17:59:27 +0000309 }
Tony Cheneaud991b982013-03-25 17:59:26 +0000310
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200311 frag_hdr[0] &= ~LOWPAN_DISPATCH_FRAG1;
312 frag_hdr[0] |= LOWPAN_DISPATCH_FRAGN;
313 frag_cap = round_down(payload_cap - LOWPAN_FRAGN_HEAD_SIZE, 8);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000314
Alexander Aring51263ff2014-06-02 13:21:57 +0200315 do {
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200316 dgram_offset += frag_len;
317 skb_offset += frag_len;
318 skb_unprocessed -= frag_len;
319 frag_len = min(frag_cap, skb_unprocessed);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000320
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200321 frag_hdr[4] = dgram_offset >> 3;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000322
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200323 rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
324 LOWPAN_FRAGN_HEAD_SIZE, skb_offset,
325 frag_len);
326 if (rc) {
Alexander Aringd57fec82014-02-28 07:32:48 +0100327 pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n",
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200328 __func__, frag_tag, skb_offset);
329 goto err;
Tony Cheneau9da29242013-03-25 17:59:27 +0000330 }
Alexander Aringeb064812014-06-02 13:21:58 +0200331 } while (skb_unprocessed > frag_cap);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000332
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200333 consume_skb(skb);
334 return NET_XMIT_SUCCESS;
335
336err:
337 kfree_skb(skb);
338 return rc;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000339}
340
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700341static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
342{
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200343 struct ieee802154_hdr wpan_hdr;
344 int max_single;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700345
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000346 pr_debug("package xmit\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700347
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200348 if (ieee802154_hdr_peek(skb, &wpan_hdr) < 0) {
349 kfree_skb(skb);
350 return NET_XMIT_DROP;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000351 }
352
Phoebe Buckheisterd4b28162014-05-14 17:43:09 +0200353 max_single = ieee802154_max_payload(&wpan_hdr);
354
355 if (skb_tail_pointer(skb) - skb_network_header(skb) <= max_single) {
356 skb->dev = lowpan_dev_info(dev)->real_dev;
357 return dev_queue_xmit(skb);
358 } else {
359 netdev_tx_t rc;
360
361 pr_debug("frame is too big, fragmentation is needed\n");
362 rc = lowpan_xmit_fragmented(skb, dev, &wpan_hdr);
363
364 return rc < 0 ? NET_XMIT_DROP : rc;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000365 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700366}
367
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000368static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
369{
370 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
371 return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
372}
373
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100374static __le16 lowpan_get_pan_id(const struct net_device *dev)
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000375{
376 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
377 return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
378}
379
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100380static __le16 lowpan_get_short_addr(const struct net_device *dev)
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000381{
382 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
383 return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
384}
385
Tony Cheneauc7d0ab22013-03-25 17:59:30 +0000386static u8 lowpan_get_dsn(const struct net_device *dev)
387{
388 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
389 return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev);
390}
391
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700392static struct header_ops lowpan_header_ops = {
393 .create = lowpan_header_create,
394};
395
Eric Dumazet20e7c4e82014-02-10 11:42:35 -0800396static struct lock_class_key lowpan_tx_busylock;
397static struct lock_class_key lowpan_netdev_xmit_lock_key;
398
399static void lowpan_set_lockdep_class_one(struct net_device *dev,
400 struct netdev_queue *txq,
401 void *_unused)
402{
403 lockdep_set_class(&txq->_xmit_lock,
404 &lowpan_netdev_xmit_lock_key);
405}
406
407
408static int lowpan_dev_init(struct net_device *dev)
409{
410 netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL);
411 dev->qdisc_tx_busylock = &lowpan_tx_busylock;
412 return 0;
413}
414
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700415static const struct net_device_ops lowpan_netdev_ops = {
Eric Dumazet20e7c4e82014-02-10 11:42:35 -0800416 .ndo_init = lowpan_dev_init,
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700417 .ndo_start_xmit = lowpan_xmit,
alex.bluesman.smirnov@gmail.com42c36292012-07-01 19:58:46 +0000418 .ndo_set_mac_address = lowpan_set_address,
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700419};
420
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000421static struct ieee802154_mlme_ops lowpan_mlme = {
422 .get_pan_id = lowpan_get_pan_id,
423 .get_phy = lowpan_get_phy,
424 .get_short_addr = lowpan_get_short_addr,
Tony Cheneauc7d0ab22013-03-25 17:59:30 +0000425 .get_dsn = lowpan_get_dsn,
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000426};
427
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700428static void lowpan_setup(struct net_device *dev)
429{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700430 dev->addr_len = IEEE802154_ADDR_LEN;
431 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
432 dev->type = ARPHRD_IEEE802154;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700433 /* Frame Control + Sequence Number + Address fields + Security Header */
434 dev->hard_header_len = 2 + 1 + 20 + 14;
435 dev->needed_tailroom = 2; /* FCS */
436 dev->mtu = 1281;
437 dev->tx_queue_len = 0;
alex.bluesman.smirnov@gmail.com4d039f62011-11-10 07:39:37 +0000438 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700439 dev->watchdog_timeo = 0;
440
441 dev->netdev_ops = &lowpan_netdev_ops;
442 dev->header_ops = &lowpan_header_ops;
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000443 dev->ml_priv = &lowpan_mlme;
Alan Otta2dc375e2012-09-01 05:57:07 +0000444 dev->destructor = free_netdev;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700445}
446
447static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
448{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700449 if (tb[IFLA_ADDRESS]) {
450 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
451 return -EINVAL;
452 }
453 return 0;
454}
455
456static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
457 struct packet_type *pt, struct net_device *orig_dev)
458{
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100459 struct ieee802154_hdr hdr;
Alexander Aring7240cde2014-02-28 07:32:50 +0100460 int ret;
Alan Otta437d272012-09-01 05:57:06 +0000461
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100462 skb = skb_share_check(skb, GFP_ATOMIC);
463 if (!skb)
464 goto drop;
465
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700466 if (!netif_running(dev))
Alexander Aring7240cde2014-02-28 07:32:50 +0100467 goto drop_skb;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700468
469 if (dev->type != ARPHRD_IEEE802154)
Alexander Aring7240cde2014-02-28 07:32:50 +0100470 goto drop_skb;
471
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100472 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
473 goto drop_skb;
474
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700475 /* check that it's our buffer */
Alan Ottee21c7e2013-01-16 19:09:48 +0000476 if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100477 skb->protocol = htons(ETH_P_IPV6);
478 skb->pkt_type = PACKET_HOST;
Alan Ottee21c7e2013-01-16 19:09:48 +0000479
480 /* Pull off the 1-byte of 6lowpan header. */
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100481 skb_pull(skb, 1);
Alan Ottee21c7e2013-01-16 19:09:48 +0000482
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100483 ret = lowpan_give_skb_to_devices(skb, NULL);
Alexander Aring7240cde2014-02-28 07:32:50 +0100484 if (ret == NET_RX_DROP)
485 goto drop;
Alan Ottee21c7e2013-01-16 19:09:48 +0000486 } else {
487 switch (skb->data[0] & 0xe0) {
488 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100489 ret = process_data(skb, &hdr);
Alexander Aring7240cde2014-02-28 07:32:50 +0100490 if (ret == NET_RX_DROP)
Alan Ottee21c7e2013-01-16 19:09:48 +0000491 goto drop;
Alexander Aring7240cde2014-02-28 07:32:50 +0100492 break;
493 case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100494 ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1);
Alexander Aring7240cde2014-02-28 07:32:50 +0100495 if (ret == 1) {
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100496 ret = process_data(skb, &hdr);
Alexander Aring7240cde2014-02-28 07:32:50 +0100497 if (ret == NET_RX_DROP)
498 goto drop;
499 }
500 break;
501 case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100502 ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN);
Alexander Aring7240cde2014-02-28 07:32:50 +0100503 if (ret == 1) {
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100504 ret = process_data(skb, &hdr);
Alexander Aring7240cde2014-02-28 07:32:50 +0100505 if (ret == NET_RX_DROP)
506 goto drop;
507 }
Alan Ottee21c7e2013-01-16 19:09:48 +0000508 break;
509 default:
510 break;
511 }
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000512 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700513
514 return NET_RX_SUCCESS;
Alexander Aring7240cde2014-02-28 07:32:50 +0100515drop_skb:
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700516 kfree_skb(skb);
Alexander Aring7240cde2014-02-28 07:32:50 +0100517drop:
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700518 return NET_RX_DROP;
519}
520
521static int lowpan_newlink(struct net *src_net, struct net_device *dev,
522 struct nlattr *tb[], struct nlattr *data[])
523{
524 struct net_device *real_dev;
525 struct lowpan_dev_record *entry;
526
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000527 pr_debug("adding new link\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700528
529 if (!tb[IFLA_LINK])
530 return -EINVAL;
531 /* find and hold real wpan device */
532 real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
533 if (!real_dev)
534 return -ENODEV;
Dan Carpenter78032f92013-11-07 10:44:45 +0300535 if (real_dev->type != ARPHRD_IEEE802154) {
536 dev_put(real_dev);
Alan Ott7adac1e2013-10-05 23:15:18 -0400537 return -EINVAL;
Dan Carpenter78032f92013-11-07 10:44:45 +0300538 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700539
540 lowpan_dev_info(dev)->real_dev = real_dev;
541 mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
542
Alexander Aringd57fec82014-02-28 07:32:48 +0100543 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Dan Carpenterdc00fd42011-08-30 03:51:09 +0000544 if (!entry) {
545 dev_put(real_dev);
546 lowpan_dev_info(dev)->real_dev = NULL;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700547 return -ENOMEM;
Dan Carpenterdc00fd42011-08-30 03:51:09 +0000548 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700549
550 entry->ldev = dev;
551
Alan Ottab2d95d2013-10-05 23:15:19 -0400552 /* Set the lowpan harware address to the wpan hardware address. */
553 memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN);
554
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700555 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
556 INIT_LIST_HEAD(&entry->list);
557 list_add_tail(&entry->list, &lowpan_devices);
558 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
559
560 register_netdevice(dev);
561
562 return 0;
563}
564
565static void lowpan_dellink(struct net_device *dev, struct list_head *head)
566{
567 struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
568 struct net_device *real_dev = lowpan_dev->real_dev;
alex.bluesman.smirnov@gmail.com8deff4a2012-04-25 23:24:57 +0000569 struct lowpan_dev_record *entry, *tmp;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700570
571 ASSERT_RTNL();
572
573 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
Dan Carpenteraec9db32011-08-30 03:46:40 +0000574 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700575 if (entry->ldev == dev) {
576 list_del(&entry->list);
577 kfree(entry);
578 }
Dan Carpenteraec9db32011-08-30 03:46:40 +0000579 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700580 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
581
582 mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
583
584 unregister_netdevice_queue(dev, head);
585
586 dev_put(real_dev);
587}
588
589static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
590 .kind = "lowpan",
591 .priv_size = sizeof(struct lowpan_dev_info),
592 .setup = lowpan_setup,
593 .newlink = lowpan_newlink,
594 .dellink = lowpan_dellink,
595 .validate = lowpan_validate,
596};
597
598static inline int __init lowpan_netlink_init(void)
599{
600 return rtnl_link_register(&lowpan_link_ops);
601}
602
David S. Millera07fdce2013-02-06 15:54:38 -0500603static inline void lowpan_netlink_fini(void)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700604{
605 rtnl_link_unregister(&lowpan_link_ops);
606}
607
Alan Otta2dc375e2012-09-01 05:57:07 +0000608static int lowpan_device_event(struct notifier_block *unused,
Jiri Pirko351638e2013-05-28 01:30:21 +0000609 unsigned long event, void *ptr)
Alan Otta2dc375e2012-09-01 05:57:07 +0000610{
Jiri Pirko351638e2013-05-28 01:30:21 +0000611 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Alan Otta2dc375e2012-09-01 05:57:07 +0000612 LIST_HEAD(del_list);
613 struct lowpan_dev_record *entry, *tmp;
614
615 if (dev->type != ARPHRD_IEEE802154)
616 goto out;
617
618 if (event == NETDEV_UNREGISTER) {
619 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
620 if (lowpan_dev_info(entry->ldev)->real_dev == dev)
621 lowpan_dellink(entry->ldev, &del_list);
622 }
623
624 unregister_netdevice_many(&del_list);
Peter Senna Tschudin4c835012012-09-18 07:10:43 +0000625 }
Alan Otta2dc375e2012-09-01 05:57:07 +0000626
627out:
628 return NOTIFY_DONE;
629}
630
631static struct notifier_block lowpan_dev_notifier = {
632 .notifier_call = lowpan_device_event,
633};
634
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700635static struct packet_type lowpan_packet_type = {
Joe Perchesec633eb2014-03-12 10:04:18 -0700636 .type = htons(ETH_P_IEEE802154),
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700637 .func = lowpan_rcv,
638};
639
640static int __init lowpan_init_module(void)
641{
642 int err = 0;
643
Alexander Aring7240cde2014-02-28 07:32:50 +0100644 err = lowpan_net_frag_init();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700645 if (err < 0)
646 goto out;
647
Alexander Aring7240cde2014-02-28 07:32:50 +0100648 err = lowpan_netlink_init();
649 if (err < 0)
650 goto out_frag;
651
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700652 dev_add_pack(&lowpan_packet_type);
Alan Otta2dc375e2012-09-01 05:57:07 +0000653
654 err = register_netdevice_notifier(&lowpan_dev_notifier);
Alexander Aring7240cde2014-02-28 07:32:50 +0100655 if (err < 0)
656 goto out_pack;
657
658 return 0;
659
660out_pack:
661 dev_remove_pack(&lowpan_packet_type);
662 lowpan_netlink_fini();
663out_frag:
664 lowpan_net_frag_exit();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700665out:
666 return err;
667}
668
669static void __exit lowpan_cleanup_module(void)
670{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700671 lowpan_netlink_fini();
672
673 dev_remove_pack(&lowpan_packet_type);
alex.bluesman.smirnov@gmail.com33c34c52012-07-10 21:22:48 +0000674
Alexander Aring7240cde2014-02-28 07:32:50 +0100675 lowpan_net_frag_exit();
Alan Otta2dc375e2012-09-01 05:57:07 +0000676
Alexander Aring7240cde2014-02-28 07:32:50 +0100677 unregister_netdevice_notifier(&lowpan_dev_notifier);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700678}
679
680module_init(lowpan_init_module);
681module_exit(lowpan_cleanup_module);
682MODULE_LICENSE("GPL");
683MODULE_ALIAS_RTNL_LINK("lowpan");