blob: c23349d737aeb64c750ba2e2105c3bc23b3ee8c0 [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 Buckheister376b7bd2014-03-14 21:23:57 +010094 struct ieee802154_addr_sa sa, da;
Alexander Smirnov44331fe2011-08-24 19:34:42 -070095
Alexander Aringfc4e98d2013-02-05 10:23:43 +000096 /* TODO:
97 * if this package isn't ipv6 one, where should it be routed?
98 */
Alexander Smirnov44331fe2011-08-24 19:34:42 -070099 if (type != ETH_P_IPV6)
100 return 0;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700101
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700102 if (!saddr)
103 saddr = dev->dev_addr;
104
Alexander Aring841a5ec2013-12-12 20:15:25 +0100105 raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
106 raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700107
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200108 lowpan_header_compress(skb, dev, type, daddr, saddr, len);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700109
Alexander Aringd57fec82014-02-28 07:32:48 +0100110 /* NOTE1: I'm still unsure about the fact that compression and WPAN
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700111 * header are created here and not later in the xmit. So wait for
112 * an opinion of net maintainers.
113 */
Alexander Aringd57fec82014-02-28 07:32:48 +0100114 /* NOTE2: to be absolutely correct, we must derive PANid information
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700115 * from MAC subif of the 'dev' and 'real_dev' network devices, but
116 * this isn't implemented in mainline yet, so currently we assign 0xff
117 */
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200118 mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
119 mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
Tony Cheneau58ef67c2013-03-25 17:59:25 +0000120
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200121 /* prepare wpan address data */
122 sa.addr_type = IEEE802154_ADDR_LONG;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100123 sa.pan_id = le16_to_cpu(ieee802154_mlme_ops(dev)->get_pan_id(dev));
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700124
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200125 memcpy(&(sa.hwaddr), saddr, 8);
126 /* 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)) {
133 da.addr_type = IEEE802154_ADDR_SHORT;
134 da.short_addr = IEEE802154_ADDR_BROADCAST;
135 } else {
136 da.addr_type = IEEE802154_ADDR_LONG;
137 memcpy(&(da.hwaddr), daddr, IEEE802154_ADDR_LEN);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000138
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200139 /* request acknowledgment */
140 mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700141 }
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200142
143 return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
144 type, (void *)&da, (void *)&sa, skb->len);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700145}
146
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200147static int lowpan_give_skb_to_devices(struct sk_buff *skb,
148 struct net_device *dev)
Alan Ott0c446212013-01-16 19:09:47 +0000149{
150 struct lowpan_dev_record *entry;
151 struct sk_buff *skb_cp;
152 int stat = NET_RX_SUCCESS;
153
154 rcu_read_lock();
155 list_for_each_entry_rcu(entry, &lowpan_devices, list)
156 if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) {
157 skb_cp = skb_copy(skb, GFP_ATOMIC);
158 if (!skb_cp) {
159 stat = -ENOMEM;
160 break;
161 }
162
163 skb_cp->dev = entry->ldev;
164 stat = netif_rx(skb_cp);
165 }
166 rcu_read_unlock();
167
168 return stat;
169}
170
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200171static int process_data(struct sk_buff *skb)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700172{
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200173 u8 iphc0, iphc1;
Phoebe Buckheister376b7bd2014-03-14 21:23:57 +0100174 const struct ieee802154_addr_sa *_saddr, *_daddr;
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
Alexander Aringce2463b2013-08-16 21:59:58 +0200187 _saddr = &mac_cb(skb)->sa;
188 _daddr = &mac_cb(skb)->da;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700189
Jukka Rissanen8df8c562013-12-11 17:05:34 +0200190 return lowpan_process_data(skb, skb->dev, (u8 *)_saddr->hwaddr,
191 _saddr->addr_type, IEEE802154_ADDR_LEN,
192 (u8 *)_daddr->hwaddr, _daddr->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
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000214static int
215lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
Alexander Aringd57fec82014-02-28 07:32:48 +0100216 int mlen, int plen, int offset, int type)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000217{
218 struct sk_buff *frag;
Alexander Aring545f3612013-10-28 10:24:16 +0100219 int hlen;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000220
Tony Cheneaud991b982013-03-25 17:59:26 +0000221 hlen = (type == LOWPAN_DISPATCH_FRAG1) ?
222 LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000223
Alexander Aring841a5ec2013-12-12 20:15:25 +0100224 raw_dump_inline(__func__, "6lowpan fragment header", head, hlen);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000225
Alexander Aringb6144422013-10-28 10:24:18 +0100226 frag = netdev_alloc_skb(skb->dev,
227 hlen + mlen + plen + IEEE802154_MFR_SIZE);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000228 if (!frag)
229 return -ENOMEM;
230
231 frag->priority = skb->priority;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000232
233 /* copy header, MFR and payload */
Alexander Aring3582b902013-10-30 09:18:24 +0100234 skb_put(frag, mlen);
235 skb_copy_to_linear_data(frag, skb_mac_header(skb), mlen);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000236
Alexander Aring3582b902013-10-30 09:18:24 +0100237 skb_put(frag, hlen);
238 skb_copy_to_linear_data_offset(frag, mlen, head, hlen);
239
240 skb_put(frag, plen);
241 skb_copy_to_linear_data_offset(frag, mlen + hlen,
242 skb_network_header(skb) + offset, plen);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000243
Alexander Aring841a5ec2013-12-12 20:15:25 +0100244 raw_dump_table(__func__, " raw fragment dump", frag->data, frag->len);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000245
Alexander Aring545f3612013-10-28 10:24:16 +0100246 return dev_queue_xmit(frag);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000247}
248
249static int
Tony Cheneaud4ac3232013-03-25 17:59:28 +0000250lowpan_skb_fragmentation(struct sk_buff *skb, struct net_device *dev)
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000251{
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100252 int err;
253 u16 dgram_offset, dgram_size, payload_length, header_length,
Alexander Aring02600d02014-02-28 07:32:46 +0100254 lowpan_size, frag_plen, offset;
255 __be16 tag;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000256 u8 head[5];
257
Alexander Aring3e691622013-10-30 09:18:22 +0100258 header_length = skb->mac_len;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000259 payload_length = skb->len - header_length;
Tony Cheneaud4ac3232013-03-25 17:59:28 +0000260 tag = lowpan_dev_info(dev)->fragment_tag++;
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100261 lowpan_size = skb_network_header_len(skb);
262 dgram_size = lowpan_uncompress_size(skb, &dgram_offset) -
263 header_length;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000264
265 /* first fragment header */
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100266 head[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x7);
267 head[1] = dgram_size & 0xff;
Alexander Aringb6f82fc2014-03-02 08:09:35 +0100268 memcpy(head + 2, &tag, sizeof(tag));
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000269
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100270 /* calc the nearest payload length(divided to 8) for first fragment
271 * which fits into a IEEE802154_MTU
272 */
273 frag_plen = round_down(IEEE802154_MTU - header_length -
274 LOWPAN_FRAG1_HEAD_SIZE - lowpan_size -
275 IEEE802154_MFR_SIZE, 8);
Tony Cheneaud991b982013-03-25 17:59:26 +0000276
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100277 err = lowpan_fragment_xmit(skb, head, header_length,
278 frag_plen + lowpan_size, 0,
279 LOWPAN_DISPATCH_FRAG1);
Tony Cheneau9da29242013-03-25 17:59:27 +0000280 if (err) {
281 pr_debug("%s unable to send FRAG1 packet (tag: %d)",
282 __func__, tag);
Tony Cheneaud991b982013-03-25 17:59:26 +0000283 goto exit;
Tony Cheneau9da29242013-03-25 17:59:27 +0000284 }
Tony Cheneaud991b982013-03-25 17:59:26 +0000285
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100286 offset = lowpan_size + frag_plen;
287 dgram_offset += frag_plen;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000288
289 /* next fragment header */
290 head[0] &= ~LOWPAN_DISPATCH_FRAG1;
291 head[0] |= LOWPAN_DISPATCH_FRAGN;
292
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100293 frag_plen = round_down(IEEE802154_MTU - header_length -
294 LOWPAN_FRAGN_HEAD_SIZE - IEEE802154_MFR_SIZE, 8);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000295
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100296 while (payload_length - offset > 0) {
297 int len = frag_plen;
298
299 head[4] = dgram_offset >> 3;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000300
301 if (payload_length - offset < len)
302 len = payload_length - offset;
303
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100304 err = lowpan_fragment_xmit(skb, head, header_length, len,
305 offset, LOWPAN_DISPATCH_FRAGN);
Tony Cheneau9da29242013-03-25 17:59:27 +0000306 if (err) {
Alexander Aringd57fec82014-02-28 07:32:48 +0100307 pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n",
308 __func__, tag, offset);
Tony Cheneaud991b982013-03-25 17:59:26 +0000309 goto exit;
Tony Cheneau9da29242013-03-25 17:59:27 +0000310 }
Tony Cheneaud991b982013-03-25 17:59:26 +0000311
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000312 offset += len;
Alexander Aring96cb3eb2014-02-28 07:32:45 +0100313 dgram_offset += len;
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000314 }
315
Tony Cheneaud991b982013-03-25 17:59:26 +0000316exit:
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000317 return err;
318}
319
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700320static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
321{
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000322 int err = -1;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700323
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000324 pr_debug("package xmit\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700325
326 skb->dev = lowpan_dev_info(dev)->real_dev;
327 if (skb->dev == NULL) {
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000328 pr_debug("ERROR: no real wpan device found\n");
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000329 goto error;
330 }
331
Alan Ottb333b7e2012-11-29 15:55:44 +0000332 /* Send directly if less than the MTU minus the 2 checksum bytes. */
333 if (skb->len <= IEEE802154_MTU - IEEE802154_MFR_SIZE) {
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700334 err = dev_queue_xmit(skb);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000335 goto out;
336 }
337
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000338 pr_debug("frame is too big, fragmentation is needed\n");
Tony Cheneaud4ac3232013-03-25 17:59:28 +0000339 err = lowpan_skb_fragmentation(skb, dev);
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000340error:
341 dev_kfree_skb(skb);
342out:
Alan Ottfc52eea2013-04-03 04:00:58 +0000343 if (err)
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000344 pr_debug("ERROR: xmit failed\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700345
Alan Ottfc52eea2013-04-03 04:00:58 +0000346 return (err < 0) ? NET_XMIT_DROP : err;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700347}
348
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000349static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
350{
351 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
352 return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
353}
354
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100355static __le16 lowpan_get_pan_id(const struct net_device *dev)
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000356{
357 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
358 return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
359}
360
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100361static __le16 lowpan_get_short_addr(const struct net_device *dev)
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000362{
363 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
364 return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
365}
366
Tony Cheneauc7d0ab22013-03-25 17:59:30 +0000367static u8 lowpan_get_dsn(const struct net_device *dev)
368{
369 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
370 return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev);
371}
372
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700373static struct header_ops lowpan_header_ops = {
374 .create = lowpan_header_create,
375};
376
Eric Dumazet20e7c4e82014-02-10 11:42:35 -0800377static struct lock_class_key lowpan_tx_busylock;
378static struct lock_class_key lowpan_netdev_xmit_lock_key;
379
380static void lowpan_set_lockdep_class_one(struct net_device *dev,
381 struct netdev_queue *txq,
382 void *_unused)
383{
384 lockdep_set_class(&txq->_xmit_lock,
385 &lowpan_netdev_xmit_lock_key);
386}
387
388
389static int lowpan_dev_init(struct net_device *dev)
390{
391 netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL);
392 dev->qdisc_tx_busylock = &lowpan_tx_busylock;
393 return 0;
394}
395
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700396static const struct net_device_ops lowpan_netdev_ops = {
Eric Dumazet20e7c4e82014-02-10 11:42:35 -0800397 .ndo_init = lowpan_dev_init,
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700398 .ndo_start_xmit = lowpan_xmit,
alex.bluesman.smirnov@gmail.com42c36292012-07-01 19:58:46 +0000399 .ndo_set_mac_address = lowpan_set_address,
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700400};
401
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000402static struct ieee802154_mlme_ops lowpan_mlme = {
403 .get_pan_id = lowpan_get_pan_id,
404 .get_phy = lowpan_get_phy,
405 .get_short_addr = lowpan_get_short_addr,
Tony Cheneauc7d0ab22013-03-25 17:59:30 +0000406 .get_dsn = lowpan_get_dsn,
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000407};
408
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700409static void lowpan_setup(struct net_device *dev)
410{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700411 dev->addr_len = IEEE802154_ADDR_LEN;
412 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
413 dev->type = ARPHRD_IEEE802154;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700414 /* Frame Control + Sequence Number + Address fields + Security Header */
415 dev->hard_header_len = 2 + 1 + 20 + 14;
416 dev->needed_tailroom = 2; /* FCS */
417 dev->mtu = 1281;
418 dev->tx_queue_len = 0;
alex.bluesman.smirnov@gmail.com4d039f62011-11-10 07:39:37 +0000419 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700420 dev->watchdog_timeo = 0;
421
422 dev->netdev_ops = &lowpan_netdev_ops;
423 dev->header_ops = &lowpan_header_ops;
alex.bluesman.smirnov@gmail.com0848e402012-04-25 23:24:56 +0000424 dev->ml_priv = &lowpan_mlme;
Alan Otta2dc375e2012-09-01 05:57:07 +0000425 dev->destructor = free_netdev;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700426}
427
428static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
429{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700430 if (tb[IFLA_ADDRESS]) {
431 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
432 return -EINVAL;
433 }
434 return 0;
435}
436
437static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
438 struct packet_type *pt, struct net_device *orig_dev)
439{
Alan Otta437d272012-09-01 05:57:06 +0000440 struct sk_buff *local_skb;
Alexander Aring7240cde2014-02-28 07:32:50 +0100441 int ret;
Alan Otta437d272012-09-01 05:57:06 +0000442
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700443 if (!netif_running(dev))
Alexander Aring7240cde2014-02-28 07:32:50 +0100444 goto drop_skb;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700445
446 if (dev->type != ARPHRD_IEEE802154)
Alexander Aring7240cde2014-02-28 07:32:50 +0100447 goto drop_skb;
448
449 local_skb = skb_clone(skb, GFP_ATOMIC);
450 if (!local_skb)
451 goto drop_skb;
452
453 kfree_skb(skb);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700454
455 /* check that it's our buffer */
Alan Ottee21c7e2013-01-16 19:09:48 +0000456 if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
Alan Ottee21c7e2013-01-16 19:09:48 +0000457 local_skb->protocol = htons(ETH_P_IPV6);
458 local_skb->pkt_type = PACKET_HOST;
459
460 /* Pull off the 1-byte of 6lowpan header. */
461 skb_pull(local_skb, 1);
Alan Ottee21c7e2013-01-16 19:09:48 +0000462
Alexander Aring7240cde2014-02-28 07:32:50 +0100463 ret = lowpan_give_skb_to_devices(local_skb, NULL);
464 if (ret == NET_RX_DROP)
465 goto drop;
Alan Ottee21c7e2013-01-16 19:09:48 +0000466 } else {
467 switch (skb->data[0] & 0xe0) {
468 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
Alexander Aring7240cde2014-02-28 07:32:50 +0100469 ret = process_data(local_skb);
470 if (ret == NET_RX_DROP)
Alan Ottee21c7e2013-01-16 19:09:48 +0000471 goto drop;
Alexander Aring7240cde2014-02-28 07:32:50 +0100472 break;
473 case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
474 ret = lowpan_frag_rcv(local_skb, LOWPAN_DISPATCH_FRAG1);
475 if (ret == 1) {
476 ret = process_data(local_skb);
477 if (ret == NET_RX_DROP)
478 goto drop;
479 }
480 break;
481 case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
482 ret = lowpan_frag_rcv(local_skb, LOWPAN_DISPATCH_FRAGN);
483 if (ret == 1) {
484 ret = process_data(local_skb);
485 if (ret == NET_RX_DROP)
486 goto drop;
487 }
Alan Ottee21c7e2013-01-16 19:09:48 +0000488 break;
489 default:
490 break;
491 }
alex.bluesman.smirnov@gmail.com719269a2011-11-10 07:38:38 +0000492 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700493
494 return NET_RX_SUCCESS;
Alexander Aring7240cde2014-02-28 07:32:50 +0100495drop_skb:
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700496 kfree_skb(skb);
Alexander Aring7240cde2014-02-28 07:32:50 +0100497drop:
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700498 return NET_RX_DROP;
499}
500
501static int lowpan_newlink(struct net *src_net, struct net_device *dev,
502 struct nlattr *tb[], struct nlattr *data[])
503{
504 struct net_device *real_dev;
505 struct lowpan_dev_record *entry;
506
alex.bluesman.smirnov@gmail.come71094f2012-06-25 03:49:03 +0000507 pr_debug("adding new link\n");
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700508
509 if (!tb[IFLA_LINK])
510 return -EINVAL;
511 /* find and hold real wpan device */
512 real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
513 if (!real_dev)
514 return -ENODEV;
Dan Carpenter78032f92013-11-07 10:44:45 +0300515 if (real_dev->type != ARPHRD_IEEE802154) {
516 dev_put(real_dev);
Alan Ott7adac1e2013-10-05 23:15:18 -0400517 return -EINVAL;
Dan Carpenter78032f92013-11-07 10:44:45 +0300518 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700519
520 lowpan_dev_info(dev)->real_dev = real_dev;
521 mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
522
Alexander Aringd57fec82014-02-28 07:32:48 +0100523 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Dan Carpenterdc00fd42011-08-30 03:51:09 +0000524 if (!entry) {
525 dev_put(real_dev);
526 lowpan_dev_info(dev)->real_dev = NULL;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700527 return -ENOMEM;
Dan Carpenterdc00fd42011-08-30 03:51:09 +0000528 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700529
530 entry->ldev = dev;
531
Alan Ottab2d95d2013-10-05 23:15:19 -0400532 /* Set the lowpan harware address to the wpan hardware address. */
533 memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN);
534
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700535 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
536 INIT_LIST_HEAD(&entry->list);
537 list_add_tail(&entry->list, &lowpan_devices);
538 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
539
540 register_netdevice(dev);
541
542 return 0;
543}
544
545static void lowpan_dellink(struct net_device *dev, struct list_head *head)
546{
547 struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
548 struct net_device *real_dev = lowpan_dev->real_dev;
alex.bluesman.smirnov@gmail.com8deff4a2012-04-25 23:24:57 +0000549 struct lowpan_dev_record *entry, *tmp;
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700550
551 ASSERT_RTNL();
552
553 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
Dan Carpenteraec9db32011-08-30 03:46:40 +0000554 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700555 if (entry->ldev == dev) {
556 list_del(&entry->list);
557 kfree(entry);
558 }
Dan Carpenteraec9db32011-08-30 03:46:40 +0000559 }
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700560 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
561
562 mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
563
564 unregister_netdevice_queue(dev, head);
565
566 dev_put(real_dev);
567}
568
569static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
570 .kind = "lowpan",
571 .priv_size = sizeof(struct lowpan_dev_info),
572 .setup = lowpan_setup,
573 .newlink = lowpan_newlink,
574 .dellink = lowpan_dellink,
575 .validate = lowpan_validate,
576};
577
578static inline int __init lowpan_netlink_init(void)
579{
580 return rtnl_link_register(&lowpan_link_ops);
581}
582
David S. Millera07fdce2013-02-06 15:54:38 -0500583static inline void lowpan_netlink_fini(void)
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700584{
585 rtnl_link_unregister(&lowpan_link_ops);
586}
587
Alan Otta2dc375e2012-09-01 05:57:07 +0000588static int lowpan_device_event(struct notifier_block *unused,
Jiri Pirko351638e2013-05-28 01:30:21 +0000589 unsigned long event, void *ptr)
Alan Otta2dc375e2012-09-01 05:57:07 +0000590{
Jiri Pirko351638e2013-05-28 01:30:21 +0000591 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Alan Otta2dc375e2012-09-01 05:57:07 +0000592 LIST_HEAD(del_list);
593 struct lowpan_dev_record *entry, *tmp;
594
595 if (dev->type != ARPHRD_IEEE802154)
596 goto out;
597
598 if (event == NETDEV_UNREGISTER) {
599 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
600 if (lowpan_dev_info(entry->ldev)->real_dev == dev)
601 lowpan_dellink(entry->ldev, &del_list);
602 }
603
604 unregister_netdevice_many(&del_list);
Peter Senna Tschudin4c835012012-09-18 07:10:43 +0000605 }
Alan Otta2dc375e2012-09-01 05:57:07 +0000606
607out:
608 return NOTIFY_DONE;
609}
610
611static struct notifier_block lowpan_dev_notifier = {
612 .notifier_call = lowpan_device_event,
613};
614
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700615static struct packet_type lowpan_packet_type = {
Joe Perchesec633eb2014-03-12 10:04:18 -0700616 .type = htons(ETH_P_IEEE802154),
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700617 .func = lowpan_rcv,
618};
619
620static int __init lowpan_init_module(void)
621{
622 int err = 0;
623
Alexander Aring7240cde2014-02-28 07:32:50 +0100624 err = lowpan_net_frag_init();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700625 if (err < 0)
626 goto out;
627
Alexander Aring7240cde2014-02-28 07:32:50 +0100628 err = lowpan_netlink_init();
629 if (err < 0)
630 goto out_frag;
631
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700632 dev_add_pack(&lowpan_packet_type);
Alan Otta2dc375e2012-09-01 05:57:07 +0000633
634 err = register_netdevice_notifier(&lowpan_dev_notifier);
Alexander Aring7240cde2014-02-28 07:32:50 +0100635 if (err < 0)
636 goto out_pack;
637
638 return 0;
639
640out_pack:
641 dev_remove_pack(&lowpan_packet_type);
642 lowpan_netlink_fini();
643out_frag:
644 lowpan_net_frag_exit();
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700645out:
646 return err;
647}
648
649static void __exit lowpan_cleanup_module(void)
650{
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700651 lowpan_netlink_fini();
652
653 dev_remove_pack(&lowpan_packet_type);
alex.bluesman.smirnov@gmail.com33c34c52012-07-10 21:22:48 +0000654
Alexander Aring7240cde2014-02-28 07:32:50 +0100655 lowpan_net_frag_exit();
Alan Otta2dc375e2012-09-01 05:57:07 +0000656
Alexander Aring7240cde2014-02-28 07:32:50 +0100657 unregister_netdevice_notifier(&lowpan_dev_notifier);
Alexander Smirnov44331fe2011-08-24 19:34:42 -0700658}
659
660module_init(lowpan_init_module);
661module_exit(lowpan_cleanup_module);
662MODULE_LICENSE("GPL");
663MODULE_ALIAS_RTNL_LINK("lowpan");