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> | |
52 | #include <net/af_ieee802154.h> | |
53 | #include <net/ieee802154.h> | |
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 AS |
60 | static LIST_HEAD(lowpan_devices); |
61 | ||
44331fe2 AS |
62 | /* private device info */ |
63 | struct lowpan_dev_info { | |
64 | struct net_device *real_dev; /* real WPAN device ptr */ | |
65 | struct mutex dev_list_mtx; /* mutex for list ops */ | |
02600d0d | 66 | __be16 fragment_tag; |
44331fe2 AS |
67 | }; |
68 | ||
69 | struct lowpan_dev_record { | |
70 | struct net_device *ldev; | |
71 | struct list_head list; | |
72 | }; | |
73 | ||
74 | static inline struct | |
75 | lowpan_dev_info *lowpan_dev_info(const struct net_device *dev) | |
76 | { | |
77 | return netdev_priv(dev); | |
78 | } | |
79 | ||
80 | static 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 | ||
44331fe2 AS |
87 | static int lowpan_header_create(struct sk_buff *skb, |
88 | struct net_device *dev, | |
89 | unsigned short type, const void *_daddr, | |
95c96174 | 90 | const void *_saddr, unsigned int len) |
44331fe2 | 91 | { |
44331fe2 AS |
92 | const u8 *saddr = _saddr; |
93 | const u8 *daddr = _daddr; | |
e6278d92 | 94 | struct ieee802154_addr sa, da; |
44331fe2 | 95 | |
fc4e98db AA |
96 | /* TODO: |
97 | * if this package isn't ipv6 one, where should it be routed? | |
98 | */ | |
44331fe2 AS |
99 | if (type != ETH_P_IPV6) |
100 | return 0; | |
44331fe2 | 101 | |
44331fe2 AS |
102 | if (!saddr) |
103 | saddr = dev->dev_addr; | |
104 | ||
841a5ec7 AA |
105 | raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8); |
106 | raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8); | |
44331fe2 | 107 | |
8df8c56a | 108 | lowpan_header_compress(skb, dev, type, daddr, saddr, len); |
44331fe2 | 109 | |
d57fec84 | 110 | /* NOTE1: I'm still unsure about the fact that compression and WPAN |
44331fe2 AS |
111 | * header are created here and not later in the xmit. So wait for |
112 | * an opinion of net maintainers. | |
113 | */ | |
d57fec84 | 114 | /* NOTE2: to be absolutely correct, we must derive PANid information |
44331fe2 AS |
115 | * 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 | */ | |
8df8c56a JR |
118 | mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA; |
119 | mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev); | |
58ef67c3 | 120 | |
8df8c56a | 121 | /* prepare wpan address data */ |
e6278d92 PB |
122 | 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); | |
43de7aa6 | 125 | |
8df8c56a | 126 | /* intra-PAN communications */ |
b70ab2e8 | 127 | da.pan_id = sa.pan_id; |
44331fe2 | 128 | |
d57fec84 | 129 | /* if the destination address is the broadcast address, use the |
8df8c56a JR |
130 | * corresponding short address |
131 | */ | |
132 | if (lowpan_is_addr_broadcast(daddr)) { | |
e6278d92 PB |
133 | da.mode = IEEE802154_ADDR_SHORT; |
134 | da.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST); | |
8df8c56a | 135 | } else { |
e6278d92 PB |
136 | da.mode = IEEE802154_ADDR_LONG; |
137 | da.extended_addr = ieee802154_devaddr_from_raw(daddr); | |
f333a15a | 138 | |
8df8c56a JR |
139 | /* request acknowledgment */ |
140 | mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ; | |
44331fe2 | 141 | } |
8df8c56a JR |
142 | |
143 | return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev, | |
d1d7358e | 144 | type, (void *)&da, (void *)&sa, 0); |
44331fe2 AS |
145 | } |
146 | ||
8df8c56a JR |
147 | static int lowpan_give_skb_to_devices(struct sk_buff *skb, |
148 | struct net_device *dev) | |
0c446212 AO |
149 | { |
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 | ||
ae531b94 | 171 | static int process_data(struct sk_buff *skb, const struct ieee802154_hdr *hdr) |
44331fe2 | 172 | { |
8df8c56a | 173 | u8 iphc0, iphc1; |
ae531b94 PB |
174 | struct ieee802154_addr_sa sa, da; |
175 | void *sap, *dap; | |
44331fe2 | 176 | |
841a5ec7 | 177 | raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len); |
44331fe2 AS |
178 | /* at least two bytes will be used for the encoding */ |
179 | if (skb->len < 2) | |
180 | goto drop; | |
c5d3687f | 181 | |
182 | if (lowpan_fetch_skb_u8(skb, &iphc0)) | |
183 | goto drop; | |
719269af | 184 | |
c5d3687f | 185 | if (lowpan_fetch_skb_u8(skb, &iphc1)) |
186 | goto drop; | |
44331fe2 | 187 | |
ae531b94 PB |
188 | ieee802154_addr_to_sa(&sa, &hdr->source); |
189 | ieee802154_addr_to_sa(&da, &hdr->dest); | |
44331fe2 | 190 | |
ae531b94 PB |
191 | if (sa.addr_type == IEEE802154_ADDR_SHORT) |
192 | sap = &sa.short_addr; | |
193 | else | |
194 | sap = &sa.hwaddr; | |
195 | ||
196 | if (da.addr_type == IEEE802154_ADDR_SHORT) | |
197 | dap = &da.short_addr; | |
198 | else | |
199 | dap = &da.hwaddr; | |
200 | ||
201 | return lowpan_process_data(skb, skb->dev, sap, sa.addr_type, | |
202 | IEEE802154_ADDR_LEN, dap, da.addr_type, | |
203 | IEEE802154_ADDR_LEN, iphc0, iphc1, | |
204 | lowpan_give_skb_to_devices); | |
719269af | 205 | |
44331fe2 | 206 | drop: |
90d0963d | 207 | kfree_skb(skb); |
44331fe2 AS |
208 | return -EINVAL; |
209 | } | |
210 | ||
42c36295 | 211 | static int lowpan_set_address(struct net_device *dev, void *p) |
212 | { | |
213 | struct sockaddr *sa = p; | |
214 | ||
215 | if (netif_running(dev)) | |
216 | return -EBUSY; | |
217 | ||
218 | /* TODO: validate addr */ | |
219 | memcpy(dev->dev_addr, sa->sa_data, dev->addr_len); | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
719269af | 224 | static int |
225 | lowpan_fragment_xmit(struct sk_buff *skb, u8 *head, | |
d57fec84 | 226 | int mlen, int plen, int offset, int type) |
719269af | 227 | { |
228 | struct sk_buff *frag; | |
545f3613 | 229 | int hlen; |
719269af | 230 | |
d991b98f TC |
231 | hlen = (type == LOWPAN_DISPATCH_FRAG1) ? |
232 | LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE; | |
719269af | 233 | |
841a5ec7 | 234 | raw_dump_inline(__func__, "6lowpan fragment header", head, hlen); |
719269af | 235 | |
b614442f AA |
236 | frag = netdev_alloc_skb(skb->dev, |
237 | hlen + mlen + plen + IEEE802154_MFR_SIZE); | |
719269af | 238 | if (!frag) |
239 | return -ENOMEM; | |
240 | ||
241 | frag->priority = skb->priority; | |
719269af | 242 | |
243 | /* copy header, MFR and payload */ | |
3582b900 AA |
244 | skb_put(frag, mlen); |
245 | skb_copy_to_linear_data(frag, skb_mac_header(skb), mlen); | |
719269af | 246 | |
3582b900 AA |
247 | skb_put(frag, hlen); |
248 | skb_copy_to_linear_data_offset(frag, mlen, head, hlen); | |
249 | ||
250 | skb_put(frag, plen); | |
251 | skb_copy_to_linear_data_offset(frag, mlen + hlen, | |
252 | skb_network_header(skb) + offset, plen); | |
719269af | 253 | |
841a5ec7 | 254 | raw_dump_table(__func__, " raw fragment dump", frag->data, frag->len); |
719269af | 255 | |
545f3613 | 256 | return dev_queue_xmit(frag); |
719269af | 257 | } |
258 | ||
259 | static int | |
d4ac3236 | 260 | lowpan_skb_fragmentation(struct sk_buff *skb, struct net_device *dev) |
719269af | 261 | { |
96cb3eb7 AA |
262 | int err; |
263 | u16 dgram_offset, dgram_size, payload_length, header_length, | |
02600d0d AA |
264 | lowpan_size, frag_plen, offset; |
265 | __be16 tag; | |
719269af | 266 | u8 head[5]; |
267 | ||
3e69162e | 268 | header_length = skb->mac_len; |
719269af | 269 | payload_length = skb->len - header_length; |
d4ac3236 | 270 | tag = lowpan_dev_info(dev)->fragment_tag++; |
96cb3eb7 AA |
271 | lowpan_size = skb_network_header_len(skb); |
272 | dgram_size = lowpan_uncompress_size(skb, &dgram_offset) - | |
273 | header_length; | |
719269af | 274 | |
275 | /* first fragment header */ | |
96cb3eb7 AA |
276 | head[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x7); |
277 | head[1] = dgram_size & 0xff; | |
b6f82fc0 | 278 | memcpy(head + 2, &tag, sizeof(tag)); |
719269af | 279 | |
96cb3eb7 AA |
280 | /* calc the nearest payload length(divided to 8) for first fragment |
281 | * which fits into a IEEE802154_MTU | |
282 | */ | |
283 | frag_plen = round_down(IEEE802154_MTU - header_length - | |
284 | LOWPAN_FRAG1_HEAD_SIZE - lowpan_size - | |
285 | IEEE802154_MFR_SIZE, 8); | |
d991b98f | 286 | |
96cb3eb7 AA |
287 | err = lowpan_fragment_xmit(skb, head, header_length, |
288 | frag_plen + lowpan_size, 0, | |
289 | LOWPAN_DISPATCH_FRAG1); | |
9da2924c TC |
290 | if (err) { |
291 | pr_debug("%s unable to send FRAG1 packet (tag: %d)", | |
292 | __func__, tag); | |
d991b98f | 293 | goto exit; |
9da2924c | 294 | } |
d991b98f | 295 | |
96cb3eb7 AA |
296 | offset = lowpan_size + frag_plen; |
297 | dgram_offset += frag_plen; | |
719269af | 298 | |
299 | /* next fragment header */ | |
300 | head[0] &= ~LOWPAN_DISPATCH_FRAG1; | |
301 | head[0] |= LOWPAN_DISPATCH_FRAGN; | |
302 | ||
96cb3eb7 AA |
303 | frag_plen = round_down(IEEE802154_MTU - header_length - |
304 | LOWPAN_FRAGN_HEAD_SIZE - IEEE802154_MFR_SIZE, 8); | |
305 | ||
53cb5717 | 306 | while (payload_length - offset > 0) { |
96cb3eb7 | 307 | int len = frag_plen; |
719269af | 308 | |
96cb3eb7 | 309 | head[4] = dgram_offset >> 3; |
719269af | 310 | |
311 | if (payload_length - offset < len) | |
312 | len = payload_length - offset; | |
313 | ||
96cb3eb7 AA |
314 | err = lowpan_fragment_xmit(skb, head, header_length, len, |
315 | offset, LOWPAN_DISPATCH_FRAGN); | |
9da2924c | 316 | if (err) { |
d57fec84 AA |
317 | pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n", |
318 | __func__, tag, offset); | |
d991b98f | 319 | goto exit; |
9da2924c | 320 | } |
d991b98f | 321 | |
719269af | 322 | offset += len; |
96cb3eb7 | 323 | dgram_offset += len; |
719269af | 324 | } |
325 | ||
d991b98f | 326 | exit: |
719269af | 327 | return err; |
328 | } | |
329 | ||
44331fe2 AS |
330 | static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev) |
331 | { | |
719269af | 332 | int err = -1; |
44331fe2 | 333 | |
e71094f9 | 334 | pr_debug("package xmit\n"); |
44331fe2 AS |
335 | |
336 | skb->dev = lowpan_dev_info(dev)->real_dev; | |
337 | if (skb->dev == NULL) { | |
e71094f9 | 338 | pr_debug("ERROR: no real wpan device found\n"); |
719269af | 339 | goto error; |
340 | } | |
341 | ||
b333b7e6 AO |
342 | /* Send directly if less than the MTU minus the 2 checksum bytes. */ |
343 | if (skb->len <= IEEE802154_MTU - IEEE802154_MFR_SIZE) { | |
44331fe2 | 344 | err = dev_queue_xmit(skb); |
719269af | 345 | goto out; |
346 | } | |
347 | ||
e71094f9 | 348 | pr_debug("frame is too big, fragmentation is needed\n"); |
d4ac3236 | 349 | err = lowpan_skb_fragmentation(skb, dev); |
719269af | 350 | error: |
351 | dev_kfree_skb(skb); | |
352 | out: | |
fc52eea4 | 353 | if (err) |
e71094f9 | 354 | pr_debug("ERROR: xmit failed\n"); |
44331fe2 | 355 | |
fc52eea4 | 356 | return (err < 0) ? NET_XMIT_DROP : err; |
44331fe2 AS |
357 | } |
358 | ||
0848e404 | 359 | static struct wpan_phy *lowpan_get_phy(const struct net_device *dev) |
360 | { | |
361 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; | |
362 | return ieee802154_mlme_ops(real_dev)->get_phy(real_dev); | |
363 | } | |
364 | ||
b70ab2e8 | 365 | static __le16 lowpan_get_pan_id(const struct net_device *dev) |
0848e404 | 366 | { |
367 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; | |
368 | return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev); | |
369 | } | |
370 | ||
b70ab2e8 | 371 | static __le16 lowpan_get_short_addr(const struct net_device *dev) |
0848e404 | 372 | { |
373 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; | |
374 | return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev); | |
375 | } | |
376 | ||
c7d0ab28 TC |
377 | static u8 lowpan_get_dsn(const struct net_device *dev) |
378 | { | |
379 | struct net_device *real_dev = lowpan_dev_info(dev)->real_dev; | |
380 | return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev); | |
381 | } | |
382 | ||
44331fe2 AS |
383 | static struct header_ops lowpan_header_ops = { |
384 | .create = lowpan_header_create, | |
385 | }; | |
386 | ||
20e7c4e8 ED |
387 | static struct lock_class_key lowpan_tx_busylock; |
388 | static struct lock_class_key lowpan_netdev_xmit_lock_key; | |
389 | ||
390 | static void lowpan_set_lockdep_class_one(struct net_device *dev, | |
391 | struct netdev_queue *txq, | |
392 | void *_unused) | |
393 | { | |
394 | lockdep_set_class(&txq->_xmit_lock, | |
395 | &lowpan_netdev_xmit_lock_key); | |
396 | } | |
397 | ||
398 | ||
399 | static int lowpan_dev_init(struct net_device *dev) | |
400 | { | |
401 | netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL); | |
402 | dev->qdisc_tx_busylock = &lowpan_tx_busylock; | |
403 | return 0; | |
404 | } | |
405 | ||
44331fe2 | 406 | static const struct net_device_ops lowpan_netdev_ops = { |
20e7c4e8 | 407 | .ndo_init = lowpan_dev_init, |
44331fe2 | 408 | .ndo_start_xmit = lowpan_xmit, |
42c36295 | 409 | .ndo_set_mac_address = lowpan_set_address, |
44331fe2 AS |
410 | }; |
411 | ||
0848e404 | 412 | static struct ieee802154_mlme_ops lowpan_mlme = { |
413 | .get_pan_id = lowpan_get_pan_id, | |
414 | .get_phy = lowpan_get_phy, | |
415 | .get_short_addr = lowpan_get_short_addr, | |
c7d0ab28 | 416 | .get_dsn = lowpan_get_dsn, |
0848e404 | 417 | }; |
418 | ||
44331fe2 AS |
419 | static void lowpan_setup(struct net_device *dev) |
420 | { | |
44331fe2 AS |
421 | dev->addr_len = IEEE802154_ADDR_LEN; |
422 | memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN); | |
423 | dev->type = ARPHRD_IEEE802154; | |
44331fe2 AS |
424 | /* Frame Control + Sequence Number + Address fields + Security Header */ |
425 | dev->hard_header_len = 2 + 1 + 20 + 14; | |
426 | dev->needed_tailroom = 2; /* FCS */ | |
427 | dev->mtu = 1281; | |
428 | dev->tx_queue_len = 0; | |
4d039f68 | 429 | dev->flags = IFF_BROADCAST | IFF_MULTICAST; |
44331fe2 AS |
430 | dev->watchdog_timeo = 0; |
431 | ||
432 | dev->netdev_ops = &lowpan_netdev_ops; | |
433 | dev->header_ops = &lowpan_header_ops; | |
0848e404 | 434 | dev->ml_priv = &lowpan_mlme; |
a2dc375e | 435 | dev->destructor = free_netdev; |
44331fe2 AS |
436 | } |
437 | ||
438 | static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[]) | |
439 | { | |
44331fe2 AS |
440 | if (tb[IFLA_ADDRESS]) { |
441 | if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN) | |
442 | return -EINVAL; | |
443 | } | |
444 | return 0; | |
445 | } | |
446 | ||
447 | static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev, | |
448 | struct packet_type *pt, struct net_device *orig_dev) | |
449 | { | |
ae531b94 | 450 | struct ieee802154_hdr hdr; |
7240cdec | 451 | int ret; |
a437d274 | 452 | |
8cfad496 PB |
453 | skb = skb_share_check(skb, GFP_ATOMIC); |
454 | if (!skb) | |
455 | goto drop; | |
456 | ||
44331fe2 | 457 | if (!netif_running(dev)) |
7240cdec | 458 | goto drop_skb; |
44331fe2 AS |
459 | |
460 | if (dev->type != ARPHRD_IEEE802154) | |
7240cdec AA |
461 | goto drop_skb; |
462 | ||
ae531b94 PB |
463 | if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) |
464 | goto drop_skb; | |
465 | ||
44331fe2 | 466 | /* check that it's our buffer */ |
ee21c7e0 | 467 | if (skb->data[0] == LOWPAN_DISPATCH_IPV6) { |
8cfad496 PB |
468 | skb->protocol = htons(ETH_P_IPV6); |
469 | skb->pkt_type = PACKET_HOST; | |
ee21c7e0 AO |
470 | |
471 | /* Pull off the 1-byte of 6lowpan header. */ | |
8cfad496 | 472 | skb_pull(skb, 1); |
ee21c7e0 | 473 | |
8cfad496 | 474 | ret = lowpan_give_skb_to_devices(skb, NULL); |
7240cdec AA |
475 | if (ret == NET_RX_DROP) |
476 | goto drop; | |
ee21c7e0 AO |
477 | } else { |
478 | switch (skb->data[0] & 0xe0) { | |
479 | case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */ | |
8cfad496 | 480 | ret = process_data(skb, &hdr); |
7240cdec AA |
481 | if (ret == NET_RX_DROP) |
482 | goto drop; | |
483 | break; | |
ee21c7e0 | 484 | case LOWPAN_DISPATCH_FRAG1: /* first fragment header */ |
8cfad496 | 485 | ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1); |
7240cdec | 486 | if (ret == 1) { |
8cfad496 | 487 | ret = process_data(skb, &hdr); |
7240cdec AA |
488 | if (ret == NET_RX_DROP) |
489 | goto drop; | |
490 | } | |
491 | break; | |
ee21c7e0 | 492 | case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */ |
8cfad496 | 493 | ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN); |
7240cdec | 494 | if (ret == 1) { |
8cfad496 | 495 | ret = process_data(skb, &hdr); |
7240cdec AA |
496 | if (ret == NET_RX_DROP) |
497 | goto drop; | |
498 | } | |
ee21c7e0 AO |
499 | break; |
500 | default: | |
501 | break; | |
502 | } | |
719269af | 503 | } |
44331fe2 AS |
504 | |
505 | return NET_RX_SUCCESS; | |
7240cdec | 506 | drop_skb: |
44331fe2 | 507 | kfree_skb(skb); |
7240cdec | 508 | drop: |
44331fe2 AS |
509 | return NET_RX_DROP; |
510 | } | |
511 | ||
512 | static int lowpan_newlink(struct net *src_net, struct net_device *dev, | |
513 | struct nlattr *tb[], struct nlattr *data[]) | |
514 | { | |
515 | struct net_device *real_dev; | |
516 | struct lowpan_dev_record *entry; | |
517 | ||
e71094f9 | 518 | pr_debug("adding new link\n"); |
44331fe2 AS |
519 | |
520 | if (!tb[IFLA_LINK]) | |
521 | return -EINVAL; | |
522 | /* find and hold real wpan device */ | |
523 | real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); | |
524 | if (!real_dev) | |
525 | return -ENODEV; | |
78032f9b DC |
526 | if (real_dev->type != ARPHRD_IEEE802154) { |
527 | dev_put(real_dev); | |
7adac1ec | 528 | return -EINVAL; |
78032f9b | 529 | } |
44331fe2 AS |
530 | |
531 | lowpan_dev_info(dev)->real_dev = real_dev; | |
532 | mutex_init(&lowpan_dev_info(dev)->dev_list_mtx); | |
533 | ||
d57fec84 | 534 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
dc00fd44 DC |
535 | if (!entry) { |
536 | dev_put(real_dev); | |
537 | lowpan_dev_info(dev)->real_dev = NULL; | |
44331fe2 | 538 | return -ENOMEM; |
dc00fd44 | 539 | } |
44331fe2 AS |
540 | |
541 | entry->ldev = dev; | |
542 | ||
ab2d95df AO |
543 | /* Set the lowpan harware address to the wpan hardware address. */ |
544 | memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN); | |
545 | ||
44331fe2 AS |
546 | mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx); |
547 | INIT_LIST_HEAD(&entry->list); | |
548 | list_add_tail(&entry->list, &lowpan_devices); | |
549 | mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx); | |
550 | ||
551 | register_netdevice(dev); | |
552 | ||
553 | return 0; | |
554 | } | |
555 | ||
556 | static void lowpan_dellink(struct net_device *dev, struct list_head *head) | |
557 | { | |
558 | struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev); | |
559 | struct net_device *real_dev = lowpan_dev->real_dev; | |
8deff4af | 560 | struct lowpan_dev_record *entry, *tmp; |
44331fe2 AS |
561 | |
562 | ASSERT_RTNL(); | |
563 | ||
564 | mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx); | |
aec9db35 | 565 | list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) { |
44331fe2 AS |
566 | if (entry->ldev == dev) { |
567 | list_del(&entry->list); | |
568 | kfree(entry); | |
569 | } | |
aec9db35 | 570 | } |
44331fe2 AS |
571 | mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx); |
572 | ||
573 | mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx); | |
574 | ||
575 | unregister_netdevice_queue(dev, head); | |
576 | ||
577 | dev_put(real_dev); | |
578 | } | |
579 | ||
580 | static struct rtnl_link_ops lowpan_link_ops __read_mostly = { | |
581 | .kind = "lowpan", | |
582 | .priv_size = sizeof(struct lowpan_dev_info), | |
583 | .setup = lowpan_setup, | |
584 | .newlink = lowpan_newlink, | |
585 | .dellink = lowpan_dellink, | |
586 | .validate = lowpan_validate, | |
587 | }; | |
588 | ||
589 | static inline int __init lowpan_netlink_init(void) | |
590 | { | |
591 | return rtnl_link_register(&lowpan_link_ops); | |
592 | } | |
593 | ||
a07fdcec | 594 | static inline void lowpan_netlink_fini(void) |
44331fe2 AS |
595 | { |
596 | rtnl_link_unregister(&lowpan_link_ops); | |
597 | } | |
598 | ||
a2dc375e | 599 | static int lowpan_device_event(struct notifier_block *unused, |
351638e7 | 600 | unsigned long event, void *ptr) |
a2dc375e | 601 | { |
351638e7 | 602 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
a2dc375e AO |
603 | LIST_HEAD(del_list); |
604 | struct lowpan_dev_record *entry, *tmp; | |
605 | ||
606 | if (dev->type != ARPHRD_IEEE802154) | |
607 | goto out; | |
608 | ||
609 | if (event == NETDEV_UNREGISTER) { | |
610 | list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) { | |
611 | if (lowpan_dev_info(entry->ldev)->real_dev == dev) | |
612 | lowpan_dellink(entry->ldev, &del_list); | |
613 | } | |
614 | ||
615 | unregister_netdevice_many(&del_list); | |
4c835019 | 616 | } |
a2dc375e AO |
617 | |
618 | out: | |
619 | return NOTIFY_DONE; | |
620 | } | |
621 | ||
622 | static struct notifier_block lowpan_dev_notifier = { | |
623 | .notifier_call = lowpan_device_event, | |
624 | }; | |
625 | ||
44331fe2 | 626 | static struct packet_type lowpan_packet_type = { |
ec633eb5 | 627 | .type = htons(ETH_P_IEEE802154), |
44331fe2 AS |
628 | .func = lowpan_rcv, |
629 | }; | |
630 | ||
631 | static int __init lowpan_init_module(void) | |
632 | { | |
633 | int err = 0; | |
634 | ||
7240cdec | 635 | err = lowpan_net_frag_init(); |
44331fe2 AS |
636 | if (err < 0) |
637 | goto out; | |
638 | ||
7240cdec AA |
639 | err = lowpan_netlink_init(); |
640 | if (err < 0) | |
641 | goto out_frag; | |
642 | ||
44331fe2 | 643 | dev_add_pack(&lowpan_packet_type); |
a2dc375e AO |
644 | |
645 | err = register_netdevice_notifier(&lowpan_dev_notifier); | |
7240cdec AA |
646 | if (err < 0) |
647 | goto out_pack; | |
648 | ||
649 | return 0; | |
650 | ||
651 | out_pack: | |
652 | dev_remove_pack(&lowpan_packet_type); | |
653 | lowpan_netlink_fini(); | |
654 | out_frag: | |
655 | lowpan_net_frag_exit(); | |
44331fe2 AS |
656 | out: |
657 | return err; | |
658 | } | |
659 | ||
660 | static void __exit lowpan_cleanup_module(void) | |
661 | { | |
44331fe2 AS |
662 | lowpan_netlink_fini(); |
663 | ||
664 | dev_remove_pack(&lowpan_packet_type); | |
33c34c5e | 665 | |
7240cdec | 666 | lowpan_net_frag_exit(); |
a2dc375e | 667 | |
7240cdec | 668 | unregister_netdevice_notifier(&lowpan_dev_notifier); |
44331fe2 AS |
669 | } |
670 | ||
671 | module_init(lowpan_init_module); | |
672 | module_exit(lowpan_cleanup_module); | |
673 | MODULE_LICENSE("GPL"); | |
674 | MODULE_ALIAS_RTNL_LINK("lowpan"); |