Commit | Line | Data |
---|---|---|
d57fec84 | 1 | /* Copyright 2011, Siemens AG |
44331fe2 AS |
2 | * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com> |
3 | */ | |
4 | ||
d57fec84 | 5 | /* Based on patches from Jon Smirl <jonsmirl@gmail.com> |
44331fe2 AS |
6 | * 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. | |
44331fe2 AS |
16 | */ |
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 | ||
44331fe2 AS |
47 | #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> | |
4ca24aca | 52 | #include <linux/ieee802154.h> |
44331fe2 | 53 | #include <net/af_ieee802154.h> |
44331fe2 | 54 | #include <net/ieee802154_netdev.h> |
cefc8c8a | 55 | #include <net/6lowpan.h> |
44331fe2 AS |
56 | #include <net/ipv6.h> |
57 | ||
7240cdec | 58 | #include "reassembly.h" |
44331fe2 | 59 | |
44331fe2 | 60 | static LIST_HEAD(lowpan_devices); |
1ae2605e | 61 | static int lowpan_open_count; |
44331fe2 | 62 | |
44331fe2 AS |
63 | /* private device info */ |
64 | struct lowpan_dev_info { | |
65 | struct net_device *real_dev; /* real WPAN device ptr */ | |
66 | struct mutex dev_list_mtx; /* mutex for list ops */ | |
cd97a713 | 67 | u16 fragment_tag; |
44331fe2 AS |
68 | }; |
69 | ||
70 | struct lowpan_dev_record { | |
71 | struct net_device *ldev; | |
72 | struct list_head list; | |
73 | }; | |
74 | ||
f19f4f95 SV |
75 | /* don't save pan id, it's intra pan */ |
76 | struct 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 | ||
85 | struct lowpan_addr_info { | |
86 | struct lowpan_addr daddr; | |
87 | struct lowpan_addr saddr; | |
88 | }; | |
89 | ||
44331fe2 AS |
90 | static inline struct |
91 | lowpan_dev_info *lowpan_dev_info(const struct net_device *dev) | |
92 | { | |
93 | return netdev_priv(dev); | |
94 | } | |
95 | ||
f19f4f95 SV |
96 | static inline struct |
97 | lowpan_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 | ||
4710d806 VB |
104 | static 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) | |
44331fe2 | 107 | { |
44331fe2 AS |
108 | const u8 *saddr = _saddr; |
109 | const u8 *daddr = _daddr; | |
f19f4f95 | 110 | struct lowpan_addr_info *info; |
44331fe2 | 111 | |
fc4e98db AA |
112 | /* TODO: |
113 | * if this package isn't ipv6 one, where should it be routed? | |
114 | */ | |
44331fe2 AS |
115 | if (type != ETH_P_IPV6) |
116 | return 0; | |
44331fe2 | 117 | |
44331fe2 AS |
118 | if (!saddr) |
119 | saddr = dev->dev_addr; | |
120 | ||
841a5ec7 AA |
121 | raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8); |
122 | raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8); | |
44331fe2 | 123 | |
f19f4f95 | 124 | info = lowpan_skb_priv(skb); |
8df8c56a | 125 | |
f19f4f95 SV |
126 | /* 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)); | |
32edc40a | 133 | |
f19f4f95 | 134 | return 0; |
44331fe2 AS |
135 | } |
136 | ||
8df8c56a | 137 | static int lowpan_give_skb_to_devices(struct sk_buff *skb, |
4710d806 | 138 | struct net_device *dev) |
0c446212 AO |
139 | { |
140 | struct lowpan_dev_record *entry; | |
141 | struct sk_buff *skb_cp; | |
142 | int stat = NET_RX_SUCCESS; | |
143 | ||
f8b36176 MT |
144 | skb->protocol = htons(ETH_P_IPV6); |
145 | skb->pkt_type = PACKET_HOST; | |
146 | ||
0c446212 AO |
147 | rcu_read_lock(); |
148 | list_for_each_entry_rcu(entry, &lowpan_devices, list) | |
149 | if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) { | |
150 | skb_cp = skb_copy(skb, GFP_ATOMIC); | |
151 | if (!skb_cp) { | |
f8b36176 MT |
152 | kfree_skb(skb); |
153 | rcu_read_unlock(); | |
154 | return NET_RX_DROP; | |
0c446212 AO |
155 | } |
156 | ||
157 | skb_cp->dev = entry->ldev; | |
158 | stat = netif_rx(skb_cp); | |
f8b36176 MT |
159 | if (stat == NET_RX_DROP) |
160 | break; | |
0c446212 AO |
161 | } |
162 | rcu_read_unlock(); | |
163 | ||
f8b36176 MT |
164 | consume_skb(skb); |
165 | ||
0c446212 AO |
166 | return stat; |
167 | } | |
168 | ||
01141234 MT |
169 | static int |
170 | iphc_decompress(struct sk_buff *skb, const struct ieee802154_hdr *hdr) | |
44331fe2 | 171 | { |
8df8c56a | 172 | u8 iphc0, iphc1; |
ae531b94 PB |
173 | struct ieee802154_addr_sa sa, da; |
174 | void *sap, *dap; | |
44331fe2 | 175 | |
841a5ec7 | 176 | raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len); |
44331fe2 AS |
177 | /* at least two bytes will be used for the encoding */ |
178 | if (skb->len < 2) | |
56b2c3ee | 179 | return -EINVAL; |
c5d3687f | 180 | |
181 | if (lowpan_fetch_skb_u8(skb, &iphc0)) | |
56b2c3ee | 182 | return -EINVAL; |
719269af | 183 | |
c5d3687f | 184 | if (lowpan_fetch_skb_u8(skb, &iphc1)) |
56b2c3ee | 185 | return -EINVAL; |
44331fe2 | 186 | |
ae531b94 PB |
187 | ieee802154_addr_to_sa(&sa, &hdr->source); |
188 | ieee802154_addr_to_sa(&da, &hdr->dest); | |
44331fe2 | 189 | |
ae531b94 PB |
190 | 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 | ||
01141234 MT |
200 | return lowpan_header_decompress(skb, skb->dev, sap, sa.addr_type, |
201 | IEEE802154_ADDR_LEN, dap, da.addr_type, | |
202 | IEEE802154_ADDR_LEN, iphc0, iphc1); | |
44331fe2 AS |
203 | } |
204 | ||
d4b2816d PB |
205 | static struct sk_buff* |
206 | lowpan_alloc_frag(struct sk_buff *skb, int size, | |
207 | const struct ieee802154_hdr *master_hdr) | |
719269af | 208 | { |
d4b2816d | 209 | struct net_device *real_dev = lowpan_dev_info(skb->dev)->real_dev; |
719269af | 210 | struct sk_buff *frag; |
d4b2816d PB |
211 | int rc; |
212 | ||
213 | frag = alloc_skb(real_dev->hard_header_len + | |
214 | real_dev->needed_tailroom + size, | |
215 | GFP_ATOMIC); | |
216 | ||
217 | if (likely(frag)) { | |
218 | frag->dev = real_dev; | |
219 | frag->priority = skb->priority; | |
220 | skb_reserve(frag, real_dev->hard_header_len); | |
221 | skb_reset_network_header(frag); | |
222 | *mac_cb(frag) = *mac_cb(skb); | |
223 | ||
224 | rc = dev_hard_header(frag, real_dev, 0, &master_hdr->dest, | |
225 | &master_hdr->source, size); | |
226 | if (rc < 0) { | |
227 | kfree_skb(frag); | |
c0bffc7d | 228 | return ERR_PTR(rc); |
d4b2816d PB |
229 | } |
230 | } else { | |
c4cb901a | 231 | frag = ERR_PTR(-ENOMEM); |
d4b2816d | 232 | } |
719269af | 233 | |
d4b2816d PB |
234 | return frag; |
235 | } | |
719269af | 236 | |
d4b2816d PB |
237 | static int |
238 | lowpan_xmit_fragment(struct sk_buff *skb, const struct ieee802154_hdr *wpan_hdr, | |
239 | u8 *frag_hdr, int frag_hdrlen, | |
240 | int offset, int len) | |
241 | { | |
242 | struct sk_buff *frag; | |
719269af | 243 | |
d4b2816d | 244 | raw_dump_inline(__func__, " fragment header", frag_hdr, frag_hdrlen); |
719269af | 245 | |
d4b2816d PB |
246 | frag = lowpan_alloc_frag(skb, frag_hdrlen + len, wpan_hdr); |
247 | if (IS_ERR(frag)) | |
248 | return -PTR_ERR(frag); | |
3582b900 | 249 | |
d4b2816d PB |
250 | memcpy(skb_put(frag, frag_hdrlen), frag_hdr, frag_hdrlen); |
251 | memcpy(skb_put(frag, len), skb_network_header(skb) + offset, len); | |
719269af | 252 | |
d4b2816d | 253 | raw_dump_table(__func__, " fragment dump", frag->data, frag->len); |
719269af | 254 | |
545f3613 | 255 | return dev_queue_xmit(frag); |
719269af | 256 | } |
257 | ||
258 | static int | |
d4b2816d PB |
259 | lowpan_xmit_fragmented(struct sk_buff *skb, struct net_device *dev, |
260 | const struct ieee802154_hdr *wpan_hdr) | |
719269af | 261 | { |
d4b2816d PB |
262 | u16 dgram_size, dgram_offset; |
263 | __be16 frag_tag; | |
264 | u8 frag_hdr[5]; | |
265 | int frag_cap, frag_len, payload_cap, rc; | |
266 | int skb_unprocessed, skb_offset; | |
267 | ||
96cb3eb7 | 268 | dgram_size = lowpan_uncompress_size(skb, &dgram_offset) - |
d4b2816d | 269 | skb->mac_len; |
cd97a713 AA |
270 | frag_tag = htons(lowpan_dev_info(dev)->fragment_tag); |
271 | lowpan_dev_info(dev)->fragment_tag++; | |
719269af | 272 | |
d4b2816d PB |
273 | frag_hdr[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x07); |
274 | frag_hdr[1] = dgram_size & 0xff; | |
275 | memcpy(frag_hdr + 2, &frag_tag, sizeof(frag_tag)); | |
719269af | 276 | |
d4b2816d | 277 | payload_cap = ieee802154_max_payload(wpan_hdr); |
d991b98f | 278 | |
d4b2816d PB |
279 | frag_len = round_down(payload_cap - LOWPAN_FRAG1_HEAD_SIZE - |
280 | skb_network_header_len(skb), 8); | |
719269af | 281 | |
d4b2816d PB |
282 | skb_offset = skb_network_header_len(skb); |
283 | skb_unprocessed = skb->len - skb->mac_len - skb_offset; | |
719269af | 284 | |
d4b2816d PB |
285 | rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr, |
286 | LOWPAN_FRAG1_HEAD_SIZE, 0, | |
287 | frag_len + skb_network_header_len(skb)); | |
288 | if (rc) { | |
289 | pr_debug("%s unable to send FRAG1 packet (tag: %d)", | |
cd97a713 | 290 | __func__, ntohs(frag_tag)); |
d4b2816d PB |
291 | goto err; |
292 | } | |
96cb3eb7 | 293 | |
d4b2816d PB |
294 | frag_hdr[0] &= ~LOWPAN_DISPATCH_FRAG1; |
295 | frag_hdr[0] |= LOWPAN_DISPATCH_FRAGN; | |
296 | frag_cap = round_down(payload_cap - LOWPAN_FRAGN_HEAD_SIZE, 8); | |
719269af | 297 | |
51263fff | 298 | do { |
d4b2816d PB |
299 | dgram_offset += frag_len; |
300 | skb_offset += frag_len; | |
301 | skb_unprocessed -= frag_len; | |
302 | frag_len = min(frag_cap, skb_unprocessed); | |
719269af | 303 | |
d4b2816d | 304 | frag_hdr[4] = dgram_offset >> 3; |
719269af | 305 | |
d4b2816d PB |
306 | rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr, |
307 | LOWPAN_FRAGN_HEAD_SIZE, skb_offset, | |
308 | frag_len); | |
309 | if (rc) { | |
d57fec84 | 310 | pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n", |
cd97a713 | 311 | __func__, ntohs(frag_tag), skb_offset); |
d4b2816d | 312 | goto err; |
9da2924c | 313 | } |
eb06481d | 314 | } while (skb_unprocessed > frag_cap); |
719269af | 315 | |
d4b2816d PB |
316 | consume_skb(skb); |
317 | return NET_XMIT_SUCCESS; | |
318 | ||
319 | err: | |
320 | kfree_skb(skb); | |
321 | return rc; | |
719269af | 322 | } |
323 | ||
f19f4f95 SV |
324 | static int lowpan_header(struct sk_buff *skb, struct net_device *dev) |
325 | { | |
326 | struct ieee802154_addr sa, da; | |
327 | struct ieee802154_mac_cb *cb = mac_cb_init(skb); | |
328 | struct lowpan_addr_info info; | |
329 | void *daddr, *saddr; | |
330 | ||
331 | memcpy(&info, lowpan_skb_priv(skb), sizeof(info)); | |
332 | ||
333 | /* TODO: Currently we only support extended_addr */ | |
334 | daddr = &info.daddr.u.extended_addr; | |
335 | saddr = &info.saddr.u.extended_addr; | |
336 | ||
337 | lowpan_header_compress(skb, dev, ETH_P_IPV6, daddr, saddr, skb->len); | |
338 | ||
339 | cb->type = IEEE802154_FC_TYPE_DATA; | |
340 | ||
341 | /* prepare wpan address data */ | |
342 | sa.mode = IEEE802154_ADDR_LONG; | |
343 | sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev); | |
344 | sa.extended_addr = ieee802154_devaddr_from_raw(saddr); | |
345 | ||
346 | /* intra-PAN communications */ | |
347 | da.pan_id = sa.pan_id; | |
348 | ||
349 | /* if the destination address is the broadcast address, use the | |
350 | * corresponding short address | |
351 | */ | |
352 | if (lowpan_is_addr_broadcast((const u8 *)daddr)) { | |
353 | da.mode = IEEE802154_ADDR_SHORT; | |
354 | da.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST); | |
355 | cb->ackreq = false; | |
356 | } else { | |
357 | da.mode = IEEE802154_ADDR_LONG; | |
358 | da.extended_addr = ieee802154_devaddr_from_raw(daddr); | |
359 | cb->ackreq = true; | |
360 | } | |
361 | ||
362 | return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev, | |
363 | ETH_P_IPV6, (void *)&da, (void *)&sa, 0); | |
364 | } | |
365 | ||
44331fe2 AS |
366 | static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev) |
367 | { | |
d4b2816d | 368 | struct ieee802154_hdr wpan_hdr; |
f19f4f95 | 369 | int max_single, ret; |
44331fe2 | 370 | |
e71094f9 | 371 | pr_debug("package xmit\n"); |
44331fe2 | 372 | |
f19f4f95 SV |
373 | /* We must take a copy of the skb before we modify/replace the ipv6 |
374 | * header as the header could be used elsewhere | |
375 | */ | |
376 | skb = skb_unshare(skb, GFP_ATOMIC); | |
377 | if (!skb) | |
378 | return NET_XMIT_DROP; | |
379 | ||
380 | ret = lowpan_header(skb, dev); | |
381 | if (ret < 0) { | |
382 | kfree_skb(skb); | |
383 | return NET_XMIT_DROP; | |
384 | } | |
385 | ||
d4b2816d PB |
386 | if (ieee802154_hdr_peek(skb, &wpan_hdr) < 0) { |
387 | kfree_skb(skb); | |
388 | return NET_XMIT_DROP; | |
719269af | 389 | } |
390 | ||
d4b2816d | 391 | max_single = ieee802154_max_payload(&wpan_hdr); |
719269af | 392 | |
d4b2816d PB |
393 | if (skb_tail_pointer(skb) - skb_network_header(skb) <= max_single) { |
394 | skb->dev = lowpan_dev_info(dev)->real_dev; | |
395 | return dev_queue_xmit(skb); | |
396 | } else { | |
397 | netdev_tx_t rc; | |
44331fe2 | 398 | |
d4b2816d PB |
399 | pr_debug("frame is too big, fragmentation is needed\n"); |
400 | rc = lowpan_xmit_fragmented(skb, dev, &wpan_hdr); | |
401 | ||
402 | return rc < 0 ? NET_XMIT_DROP : rc; | |
403 | } | |
44331fe2 AS |
404 | } |
405 | ||
b70ab2e8 | 406 | static __le16 lowpan_get_pan_id(const struct net_device *dev) |
0848e404 | 407 | { |
408 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; | |
4710d806 | 409 | |
0848e404 | 410 | return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev); |
411 | } | |
412 | ||
b70ab2e8 | 413 | static __le16 lowpan_get_short_addr(const struct net_device *dev) |
0848e404 | 414 | { |
415 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; | |
4710d806 | 416 | |
0848e404 | 417 | return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev); |
418 | } | |
419 | ||
c7d0ab28 TC |
420 | static u8 lowpan_get_dsn(const struct net_device *dev) |
421 | { | |
422 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; | |
4710d806 | 423 | |
c7d0ab28 TC |
424 | return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev); |
425 | } | |
426 | ||
44331fe2 AS |
427 | static struct header_ops lowpan_header_ops = { |
428 | .create = lowpan_header_create, | |
429 | }; | |
430 | ||
20e7c4e8 ED |
431 | static struct lock_class_key lowpan_tx_busylock; |
432 | static struct lock_class_key lowpan_netdev_xmit_lock_key; | |
433 | ||
434 | static void lowpan_set_lockdep_class_one(struct net_device *dev, | |
435 | struct netdev_queue *txq, | |
436 | void *_unused) | |
437 | { | |
438 | lockdep_set_class(&txq->_xmit_lock, | |
439 | &lowpan_netdev_xmit_lock_key); | |
440 | } | |
441 | ||
442 | ||
443 | static int lowpan_dev_init(struct net_device *dev) | |
444 | { | |
445 | netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL); | |
446 | dev->qdisc_tx_busylock = &lowpan_tx_busylock; | |
447 | return 0; | |
448 | } | |
449 | ||
44331fe2 | 450 | static const struct net_device_ops lowpan_netdev_ops = { |
20e7c4e8 | 451 | .ndo_init = lowpan_dev_init, |
44331fe2 | 452 | .ndo_start_xmit = lowpan_xmit, |
44331fe2 AS |
453 | }; |
454 | ||
0848e404 | 455 | static struct ieee802154_mlme_ops lowpan_mlme = { |
456 | .get_pan_id = lowpan_get_pan_id, | |
0848e404 | 457 | .get_short_addr = lowpan_get_short_addr, |
c7d0ab28 | 458 | .get_dsn = lowpan_get_dsn, |
0848e404 | 459 | }; |
460 | ||
44331fe2 AS |
461 | static void lowpan_setup(struct net_device *dev) |
462 | { | |
44331fe2 AS |
463 | dev->addr_len = IEEE802154_ADDR_LEN; |
464 | memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN); | |
465 | dev->type = ARPHRD_IEEE802154; | |
44331fe2 AS |
466 | /* Frame Control + Sequence Number + Address fields + Security Header */ |
467 | dev->hard_header_len = 2 + 1 + 20 + 14; | |
468 | dev->needed_tailroom = 2; /* FCS */ | |
685d6328 | 469 | dev->mtu = IPV6_MIN_MTU; |
44331fe2 | 470 | dev->tx_queue_len = 0; |
4d039f68 | 471 | dev->flags = IFF_BROADCAST | IFF_MULTICAST; |
44331fe2 AS |
472 | dev->watchdog_timeo = 0; |
473 | ||
474 | dev->netdev_ops = &lowpan_netdev_ops; | |
475 | dev->header_ops = &lowpan_header_ops; | |
0848e404 | 476 | dev->ml_priv = &lowpan_mlme; |
a2dc375e | 477 | dev->destructor = free_netdev; |
44331fe2 AS |
478 | } |
479 | ||
480 | static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[]) | |
481 | { | |
44331fe2 AS |
482 | if (tb[IFLA_ADDRESS]) { |
483 | if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN) | |
484 | return -EINVAL; | |
485 | } | |
486 | return 0; | |
487 | } | |
488 | ||
489 | static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev, | |
4710d806 | 490 | struct packet_type *pt, struct net_device *orig_dev) |
44331fe2 | 491 | { |
ae531b94 | 492 | struct ieee802154_hdr hdr; |
7240cdec | 493 | int ret; |
a437d274 | 494 | |
8cfad496 PB |
495 | skb = skb_share_check(skb, GFP_ATOMIC); |
496 | if (!skb) | |
497 | goto drop; | |
498 | ||
44331fe2 | 499 | if (!netif_running(dev)) |
7240cdec | 500 | goto drop_skb; |
44331fe2 | 501 | |
39f6eb19 SV |
502 | if (skb->pkt_type == PACKET_OTHERHOST) |
503 | goto drop_skb; | |
504 | ||
44331fe2 | 505 | if (dev->type != ARPHRD_IEEE802154) |
7240cdec AA |
506 | goto drop_skb; |
507 | ||
ae531b94 PB |
508 | if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) |
509 | goto drop_skb; | |
510 | ||
44331fe2 | 511 | /* check that it's our buffer */ |
ee21c7e0 | 512 | if (skb->data[0] == LOWPAN_DISPATCH_IPV6) { |
ee21c7e0 | 513 | /* Pull off the 1-byte of 6lowpan header. */ |
8cfad496 | 514 | skb_pull(skb, 1); |
f8b36176 | 515 | return lowpan_give_skb_to_devices(skb, NULL); |
ee21c7e0 AO |
516 | } else { |
517 | switch (skb->data[0] & 0xe0) { | |
518 | case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */ | |
01141234 | 519 | ret = iphc_decompress(skb, &hdr); |
04dfd738 | 520 | if (ret < 0) |
56b2c3ee | 521 | goto drop_skb; |
f8b36176 MT |
522 | |
523 | return lowpan_give_skb_to_devices(skb, NULL); | |
ee21c7e0 | 524 | case LOWPAN_DISPATCH_FRAG1: /* first fragment header */ |
8cfad496 | 525 | ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1); |
7240cdec | 526 | if (ret == 1) { |
01141234 | 527 | ret = iphc_decompress(skb, &hdr); |
04dfd738 | 528 | if (ret < 0) |
56b2c3ee | 529 | goto drop_skb; |
f8b36176 MT |
530 | |
531 | return lowpan_give_skb_to_devices(skb, NULL); | |
532 | } else if (ret == -1) { | |
533 | return NET_RX_DROP; | |
534 | } else { | |
535 | return NET_RX_SUCCESS; | |
7240cdec | 536 | } |
ee21c7e0 | 537 | case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */ |
8cfad496 | 538 | ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN); |
7240cdec | 539 | if (ret == 1) { |
01141234 | 540 | ret = iphc_decompress(skb, &hdr); |
04dfd738 | 541 | if (ret < 0) |
56b2c3ee | 542 | goto drop_skb; |
f8b36176 MT |
543 | |
544 | return lowpan_give_skb_to_devices(skb, NULL); | |
545 | } else if (ret == -1) { | |
546 | return NET_RX_DROP; | |
547 | } else { | |
548 | return NET_RX_SUCCESS; | |
7240cdec | 549 | } |
ee21c7e0 AO |
550 | default: |
551 | break; | |
552 | } | |
719269af | 553 | } |
44331fe2 | 554 | |
7240cdec | 555 | drop_skb: |
44331fe2 | 556 | kfree_skb(skb); |
7240cdec | 557 | drop: |
44331fe2 AS |
558 | return NET_RX_DROP; |
559 | } | |
560 | ||
1ae2605e AA |
561 | static struct packet_type lowpan_packet_type = { |
562 | .type = htons(ETH_P_IEEE802154), | |
563 | .func = lowpan_rcv, | |
564 | }; | |
565 | ||
44331fe2 AS |
566 | static int lowpan_newlink(struct net *src_net, struct net_device *dev, |
567 | struct nlattr *tb[], struct nlattr *data[]) | |
568 | { | |
569 | struct net_device *real_dev; | |
570 | struct lowpan_dev_record *entry; | |
1ae2605e | 571 | int ret; |
44331fe2 | 572 | |
c37a8106 AA |
573 | ASSERT_RTNL(); |
574 | ||
e71094f9 | 575 | pr_debug("adding new link\n"); |
44331fe2 AS |
576 | |
577 | if (!tb[IFLA_LINK]) | |
578 | return -EINVAL; | |
579 | /* find and hold real wpan device */ | |
580 | real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); | |
581 | if (!real_dev) | |
582 | return -ENODEV; | |
78032f9b DC |
583 | if (real_dev->type != ARPHRD_IEEE802154) { |
584 | dev_put(real_dev); | |
7adac1ec | 585 | return -EINVAL; |
78032f9b | 586 | } |
44331fe2 AS |
587 | |
588 | lowpan_dev_info(dev)->real_dev = real_dev; | |
589 | mutex_init(&lowpan_dev_info(dev)->dev_list_mtx); | |
590 | ||
d57fec84 | 591 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
dc00fd44 DC |
592 | if (!entry) { |
593 | dev_put(real_dev); | |
594 | lowpan_dev_info(dev)->real_dev = NULL; | |
44331fe2 | 595 | return -ENOMEM; |
dc00fd44 | 596 | } |
44331fe2 AS |
597 | |
598 | entry->ldev = dev; | |
599 | ||
69bb631e | 600 | /* Set the lowpan hardware address to the wpan hardware address. */ |
ab2d95df AO |
601 | memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN); |
602 | ||
44331fe2 AS |
603 | mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx); |
604 | INIT_LIST_HEAD(&entry->list); | |
605 | list_add_tail(&entry->list, &lowpan_devices); | |
606 | mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx); | |
607 | ||
1ae2605e AA |
608 | ret = register_netdevice(dev); |
609 | if (ret >= 0) { | |
610 | if (!lowpan_open_count) | |
611 | dev_add_pack(&lowpan_packet_type); | |
612 | lowpan_open_count++; | |
613 | } | |
44331fe2 | 614 | |
1ae2605e | 615 | return ret; |
44331fe2 AS |
616 | } |
617 | ||
618 | static void lowpan_dellink(struct net_device *dev, struct list_head *head) | |
619 | { | |
620 | struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev); | |
621 | struct net_device *real_dev = lowpan_dev->real_dev; | |
8deff4af | 622 | struct lowpan_dev_record *entry, *tmp; |
44331fe2 AS |
623 | |
624 | ASSERT_RTNL(); | |
625 | ||
1ae2605e AA |
626 | lowpan_open_count--; |
627 | if (!lowpan_open_count) | |
628 | dev_remove_pack(&lowpan_packet_type); | |
629 | ||
44331fe2 | 630 | mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx); |
aec9db35 | 631 | list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) { |
44331fe2 AS |
632 | if (entry->ldev == dev) { |
633 | list_del(&entry->list); | |
634 | kfree(entry); | |
635 | } | |
aec9db35 | 636 | } |
44331fe2 AS |
637 | mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx); |
638 | ||
639 | mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx); | |
640 | ||
641 | unregister_netdevice_queue(dev, head); | |
642 | ||
643 | dev_put(real_dev); | |
644 | } | |
645 | ||
646 | static struct rtnl_link_ops lowpan_link_ops __read_mostly = { | |
647 | .kind = "lowpan", | |
648 | .priv_size = sizeof(struct lowpan_dev_info), | |
649 | .setup = lowpan_setup, | |
650 | .newlink = lowpan_newlink, | |
651 | .dellink = lowpan_dellink, | |
652 | .validate = lowpan_validate, | |
653 | }; | |
654 | ||
655 | static inline int __init lowpan_netlink_init(void) | |
656 | { | |
657 | return rtnl_link_register(&lowpan_link_ops); | |
658 | } | |
659 | ||
a07fdcec | 660 | static inline void lowpan_netlink_fini(void) |
44331fe2 AS |
661 | { |
662 | rtnl_link_unregister(&lowpan_link_ops); | |
663 | } | |
664 | ||
a2dc375e | 665 | static int lowpan_device_event(struct notifier_block *unused, |
351638e7 | 666 | unsigned long event, void *ptr) |
a2dc375e | 667 | { |
351638e7 | 668 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
a2dc375e AO |
669 | LIST_HEAD(del_list); |
670 | struct lowpan_dev_record *entry, *tmp; | |
671 | ||
672 | if (dev->type != ARPHRD_IEEE802154) | |
673 | goto out; | |
674 | ||
675 | if (event == NETDEV_UNREGISTER) { | |
676 | list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) { | |
677 | if (lowpan_dev_info(entry->ldev)->real_dev == dev) | |
678 | lowpan_dellink(entry->ldev, &del_list); | |
679 | } | |
680 | ||
681 | unregister_netdevice_many(&del_list); | |
4c835019 | 682 | } |
a2dc375e AO |
683 | |
684 | out: | |
685 | return NOTIFY_DONE; | |
686 | } | |
687 | ||
688 | static struct notifier_block lowpan_dev_notifier = { | |
689 | .notifier_call = lowpan_device_event, | |
690 | }; | |
691 | ||
44331fe2 AS |
692 | static int __init lowpan_init_module(void) |
693 | { | |
694 | int err = 0; | |
695 | ||
7240cdec | 696 | err = lowpan_net_frag_init(); |
44331fe2 AS |
697 | if (err < 0) |
698 | goto out; | |
699 | ||
7240cdec AA |
700 | err = lowpan_netlink_init(); |
701 | if (err < 0) | |
702 | goto out_frag; | |
703 | ||
a2dc375e | 704 | err = register_netdevice_notifier(&lowpan_dev_notifier); |
7240cdec AA |
705 | if (err < 0) |
706 | goto out_pack; | |
707 | ||
708 | return 0; | |
709 | ||
710 | out_pack: | |
7240cdec AA |
711 | lowpan_netlink_fini(); |
712 | out_frag: | |
713 | lowpan_net_frag_exit(); | |
44331fe2 AS |
714 | out: |
715 | return err; | |
716 | } | |
717 | ||
718 | static void __exit lowpan_cleanup_module(void) | |
719 | { | |
44331fe2 AS |
720 | lowpan_netlink_fini(); |
721 | ||
7240cdec | 722 | lowpan_net_frag_exit(); |
a2dc375e | 723 | |
7240cdec | 724 | unregister_netdevice_notifier(&lowpan_dev_notifier); |
44331fe2 AS |
725 | } |
726 | ||
727 | module_init(lowpan_init_module); | |
728 | module_exit(lowpan_cleanup_module); | |
729 | MODULE_LICENSE("GPL"); | |
730 | MODULE_ALIAS_RTNL_LINK("lowpan"); |