Merge tag 'fscache-fixes-20140917' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / net / ieee802154 / 6lowpan_rtnl.c
CommitLineData
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
60static LIST_HEAD(lowpan_devices);
61
44331fe2
AS
62/* 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 */
02600d0d 66 __be16 fragment_tag;
44331fe2
AS
67};
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;
4710d806 83
44331fe2
AS
84 for (i = 0; i < IEEE802154_ADDR_LEN; i++)
85 (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i];
86}
87
4710d806
VB
88static int lowpan_header_create(struct sk_buff *skb, struct net_device *dev,
89 unsigned short type, const void *_daddr,
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;
32edc40a 95 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
44331fe2 96
fc4e98db
AA
97 /* TODO:
98 * if this package isn't ipv6 one, where should it be routed?
99 */
44331fe2
AS
100 if (type != ETH_P_IPV6)
101 return 0;
44331fe2 102
44331fe2
AS
103 if (!saddr)
104 saddr = dev->dev_addr;
105
841a5ec7
AA
106 raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
107 raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
44331fe2 108
8df8c56a 109 lowpan_header_compress(skb, dev, type, daddr, saddr, len);
44331fe2 110
d57fec84 111 /* NOTE1: I'm still unsure about the fact that compression and WPAN
44331fe2
AS
112 * header are created here and not later in the xmit. So wait for
113 * an opinion of net maintainers.
114 */
d57fec84 115 /* NOTE2: to be absolutely correct, we must derive PANid information
44331fe2
AS
116 * 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 */
32edc40a 119 cb->type = IEEE802154_FC_TYPE_DATA;
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);
44331fe2 138 }
8df8c56a 139
32edc40a
PB
140 cb->ackreq = !lowpan_is_addr_broadcast(daddr);
141
8df8c56a 142 return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
d1d7358e 143 type, (void *)&da, (void *)&sa, 0);
44331fe2
AS
144}
145
8df8c56a 146static int lowpan_give_skb_to_devices(struct sk_buff *skb,
4710d806 147 struct net_device *dev)
0c446212
AO
148{
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
ae531b94 170static int process_data(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)
179 goto drop;
c5d3687f 180
181 if (lowpan_fetch_skb_u8(skb, &iphc0))
182 goto drop;
719269af 183
c5d3687f 184 if (lowpan_fetch_skb_u8(skb, &iphc1))
185 goto drop;
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
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);
719269af 204
44331fe2 205drop:
90d0963d 206 kfree_skb(skb);
44331fe2
AS
207 return -EINVAL;
208}
209
42c36295 210static 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
d4b2816d
PB
223static struct sk_buff*
224lowpan_alloc_frag(struct sk_buff *skb, int size,
225 const struct ieee802154_hdr *master_hdr)
719269af 226{
d4b2816d 227 struct net_device *real_dev = lowpan_dev_info(skb->dev)->real_dev;
719269af 228 struct sk_buff *frag;
d4b2816d
PB
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 {
c4cb901a 249 frag = ERR_PTR(-ENOMEM);
d4b2816d 250 }
719269af 251
d4b2816d
PB
252 return frag;
253}
719269af 254
d4b2816d
PB
255static int
256lowpan_xmit_fragment(struct sk_buff *skb, const struct ieee802154_hdr *wpan_hdr,
257 u8 *frag_hdr, int frag_hdrlen,
258 int offset, int len)
259{
260 struct sk_buff *frag;
719269af 261
d4b2816d 262 raw_dump_inline(__func__, " fragment header", frag_hdr, frag_hdrlen);
719269af 263
d4b2816d
PB
264 frag = lowpan_alloc_frag(skb, frag_hdrlen + len, wpan_hdr);
265 if (IS_ERR(frag))
266 return -PTR_ERR(frag);
3582b900 267
d4b2816d
PB
268 memcpy(skb_put(frag, frag_hdrlen), frag_hdr, frag_hdrlen);
269 memcpy(skb_put(frag, len), skb_network_header(skb) + offset, len);
719269af 270
d4b2816d 271 raw_dump_table(__func__, " fragment dump", frag->data, frag->len);
719269af 272
545f3613 273 return dev_queue_xmit(frag);
719269af 274}
275
276static int
d4b2816d
PB
277lowpan_xmit_fragmented(struct sk_buff *skb, struct net_device *dev,
278 const struct ieee802154_hdr *wpan_hdr)
719269af 279{
d4b2816d
PB
280 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;
285
96cb3eb7 286 dgram_size = lowpan_uncompress_size(skb, &dgram_offset) -
d4b2816d
PB
287 skb->mac_len;
288 frag_tag = lowpan_dev_info(dev)->fragment_tag++;
719269af 289
d4b2816d
PB
290 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));
719269af 293
d4b2816d 294 payload_cap = ieee802154_max_payload(wpan_hdr);
d991b98f 295
d4b2816d
PB
296 frag_len = round_down(payload_cap - LOWPAN_FRAG1_HEAD_SIZE -
297 skb_network_header_len(skb), 8);
719269af 298
d4b2816d
PB
299 skb_offset = skb_network_header_len(skb);
300 skb_unprocessed = skb->len - skb->mac_len - skb_offset;
719269af 301
d4b2816d
PB
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) {
306 pr_debug("%s unable to send FRAG1 packet (tag: %d)",
307 __func__, frag_tag);
308 goto err;
309 }
96cb3eb7 310
d4b2816d
PB
311 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);
719269af 314
51263fff 315 do {
d4b2816d
PB
316 dgram_offset += frag_len;
317 skb_offset += frag_len;
318 skb_unprocessed -= frag_len;
319 frag_len = min(frag_cap, skb_unprocessed);
719269af 320
d4b2816d 321 frag_hdr[4] = dgram_offset >> 3;
719269af 322
d4b2816d
PB
323 rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
324 LOWPAN_FRAGN_HEAD_SIZE, skb_offset,
325 frag_len);
326 if (rc) {
d57fec84 327 pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n",
d4b2816d
PB
328 __func__, frag_tag, skb_offset);
329 goto err;
9da2924c 330 }
eb06481d 331 } while (skb_unprocessed > frag_cap);
719269af 332
d4b2816d
PB
333 consume_skb(skb);
334 return NET_XMIT_SUCCESS;
335
336err:
337 kfree_skb(skb);
338 return rc;
719269af 339}
340
44331fe2
AS
341static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
342{
d4b2816d
PB
343 struct ieee802154_hdr wpan_hdr;
344 int max_single;
44331fe2 345
e71094f9 346 pr_debug("package xmit\n");
44331fe2 347
d4b2816d
PB
348 if (ieee802154_hdr_peek(skb, &wpan_hdr) < 0) {
349 kfree_skb(skb);
350 return NET_XMIT_DROP;
719269af 351 }
352
d4b2816d 353 max_single = ieee802154_max_payload(&wpan_hdr);
719269af 354
d4b2816d
PB
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;
44331fe2 360
d4b2816d
PB
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;
365 }
44331fe2
AS
366}
367
0848e404 368static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
369{
370 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
4710d806 371
0848e404 372 return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
373}
374
b70ab2e8 375static __le16 lowpan_get_pan_id(const struct net_device *dev)
0848e404 376{
377 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
4710d806 378
0848e404 379 return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
380}
381
b70ab2e8 382static __le16 lowpan_get_short_addr(const struct net_device *dev)
0848e404 383{
384 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
4710d806 385
0848e404 386 return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
387}
388
c7d0ab28
TC
389static u8 lowpan_get_dsn(const struct net_device *dev)
390{
391 struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
4710d806 392
c7d0ab28
TC
393 return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev);
394}
395
44331fe2
AS
396static struct header_ops lowpan_header_ops = {
397 .create = lowpan_header_create,
398};
399
20e7c4e8
ED
400static struct lock_class_key lowpan_tx_busylock;
401static struct lock_class_key lowpan_netdev_xmit_lock_key;
402
403static void lowpan_set_lockdep_class_one(struct net_device *dev,
404 struct netdev_queue *txq,
405 void *_unused)
406{
407 lockdep_set_class(&txq->_xmit_lock,
408 &lowpan_netdev_xmit_lock_key);
409}
410
411
412static int lowpan_dev_init(struct net_device *dev)
413{
414 netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL);
415 dev->qdisc_tx_busylock = &lowpan_tx_busylock;
416 return 0;
417}
418
44331fe2 419static const struct net_device_ops lowpan_netdev_ops = {
20e7c4e8 420 .ndo_init = lowpan_dev_init,
44331fe2 421 .ndo_start_xmit = lowpan_xmit,
42c36295 422 .ndo_set_mac_address = lowpan_set_address,
44331fe2
AS
423};
424
0848e404 425static struct ieee802154_mlme_ops lowpan_mlme = {
426 .get_pan_id = lowpan_get_pan_id,
427 .get_phy = lowpan_get_phy,
428 .get_short_addr = lowpan_get_short_addr,
c7d0ab28 429 .get_dsn = lowpan_get_dsn,
0848e404 430};
431
44331fe2
AS
432static void lowpan_setup(struct net_device *dev)
433{
44331fe2
AS
434 dev->addr_len = IEEE802154_ADDR_LEN;
435 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
436 dev->type = ARPHRD_IEEE802154;
44331fe2
AS
437 /* Frame Control + Sequence Number + Address fields + Security Header */
438 dev->hard_header_len = 2 + 1 + 20 + 14;
439 dev->needed_tailroom = 2; /* FCS */
685d6328 440 dev->mtu = IPV6_MIN_MTU;
44331fe2 441 dev->tx_queue_len = 0;
4d039f68 442 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
44331fe2
AS
443 dev->watchdog_timeo = 0;
444
445 dev->netdev_ops = &lowpan_netdev_ops;
446 dev->header_ops = &lowpan_header_ops;
0848e404 447 dev->ml_priv = &lowpan_mlme;
a2dc375e 448 dev->destructor = free_netdev;
44331fe2
AS
449}
450
451static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
452{
44331fe2
AS
453 if (tb[IFLA_ADDRESS]) {
454 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
455 return -EINVAL;
456 }
457 return 0;
458}
459
460static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
4710d806 461 struct packet_type *pt, struct net_device *orig_dev)
44331fe2 462{
ae531b94 463 struct ieee802154_hdr hdr;
7240cdec 464 int ret;
a437d274 465
8cfad496
PB
466 skb = skb_share_check(skb, GFP_ATOMIC);
467 if (!skb)
468 goto drop;
469
44331fe2 470 if (!netif_running(dev))
7240cdec 471 goto drop_skb;
44331fe2
AS
472
473 if (dev->type != ARPHRD_IEEE802154)
7240cdec
AA
474 goto drop_skb;
475
ae531b94
PB
476 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
477 goto drop_skb;
478
44331fe2 479 /* check that it's our buffer */
ee21c7e0 480 if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
8cfad496
PB
481 skb->protocol = htons(ETH_P_IPV6);
482 skb->pkt_type = PACKET_HOST;
ee21c7e0
AO
483
484 /* Pull off the 1-byte of 6lowpan header. */
8cfad496 485 skb_pull(skb, 1);
ee21c7e0 486
8cfad496 487 ret = lowpan_give_skb_to_devices(skb, NULL);
7240cdec
AA
488 if (ret == NET_RX_DROP)
489 goto drop;
ee21c7e0
AO
490 } else {
491 switch (skb->data[0] & 0xe0) {
492 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
8cfad496 493 ret = process_data(skb, &hdr);
7240cdec
AA
494 if (ret == NET_RX_DROP)
495 goto drop;
496 break;
ee21c7e0 497 case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
8cfad496 498 ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1);
7240cdec 499 if (ret == 1) {
8cfad496 500 ret = process_data(skb, &hdr);
7240cdec
AA
501 if (ret == NET_RX_DROP)
502 goto drop;
503 }
504 break;
ee21c7e0 505 case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
8cfad496 506 ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN);
7240cdec 507 if (ret == 1) {
8cfad496 508 ret = process_data(skb, &hdr);
7240cdec
AA
509 if (ret == NET_RX_DROP)
510 goto drop;
511 }
ee21c7e0
AO
512 break;
513 default:
514 break;
515 }
719269af 516 }
44331fe2
AS
517
518 return NET_RX_SUCCESS;
7240cdec 519drop_skb:
44331fe2 520 kfree_skb(skb);
7240cdec 521drop:
44331fe2
AS
522 return NET_RX_DROP;
523}
524
525static int lowpan_newlink(struct net *src_net, struct net_device *dev,
526 struct nlattr *tb[], struct nlattr *data[])
527{
528 struct net_device *real_dev;
529 struct lowpan_dev_record *entry;
530
e71094f9 531 pr_debug("adding new link\n");
44331fe2
AS
532
533 if (!tb[IFLA_LINK])
534 return -EINVAL;
535 /* find and hold real wpan device */
536 real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
537 if (!real_dev)
538 return -ENODEV;
78032f9b
DC
539 if (real_dev->type != ARPHRD_IEEE802154) {
540 dev_put(real_dev);
7adac1ec 541 return -EINVAL;
78032f9b 542 }
44331fe2
AS
543
544 lowpan_dev_info(dev)->real_dev = real_dev;
545 mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
546
d57fec84 547 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
dc00fd44
DC
548 if (!entry) {
549 dev_put(real_dev);
550 lowpan_dev_info(dev)->real_dev = NULL;
44331fe2 551 return -ENOMEM;
dc00fd44 552 }
44331fe2
AS
553
554 entry->ldev = dev;
555
ab2d95df
AO
556 /* Set the lowpan harware address to the wpan hardware address. */
557 memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN);
558
44331fe2
AS
559 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
560 INIT_LIST_HEAD(&entry->list);
561 list_add_tail(&entry->list, &lowpan_devices);
562 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
563
564 register_netdevice(dev);
565
566 return 0;
567}
568
569static void lowpan_dellink(struct net_device *dev, struct list_head *head)
570{
571 struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
572 struct net_device *real_dev = lowpan_dev->real_dev;
8deff4af 573 struct lowpan_dev_record *entry, *tmp;
44331fe2
AS
574
575 ASSERT_RTNL();
576
577 mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
aec9db35 578 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
44331fe2
AS
579 if (entry->ldev == dev) {
580 list_del(&entry->list);
581 kfree(entry);
582 }
aec9db35 583 }
44331fe2
AS
584 mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
585
586 mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
587
588 unregister_netdevice_queue(dev, head);
589
590 dev_put(real_dev);
591}
592
593static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
594 .kind = "lowpan",
595 .priv_size = sizeof(struct lowpan_dev_info),
596 .setup = lowpan_setup,
597 .newlink = lowpan_newlink,
598 .dellink = lowpan_dellink,
599 .validate = lowpan_validate,
600};
601
602static inline int __init lowpan_netlink_init(void)
603{
604 return rtnl_link_register(&lowpan_link_ops);
605}
606
a07fdcec 607static inline void lowpan_netlink_fini(void)
44331fe2
AS
608{
609 rtnl_link_unregister(&lowpan_link_ops);
610}
611
a2dc375e 612static int lowpan_device_event(struct notifier_block *unused,
351638e7 613 unsigned long event, void *ptr)
a2dc375e 614{
351638e7 615 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
a2dc375e
AO
616 LIST_HEAD(del_list);
617 struct lowpan_dev_record *entry, *tmp;
618
619 if (dev->type != ARPHRD_IEEE802154)
620 goto out;
621
622 if (event == NETDEV_UNREGISTER) {
623 list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
624 if (lowpan_dev_info(entry->ldev)->real_dev == dev)
625 lowpan_dellink(entry->ldev, &del_list);
626 }
627
628 unregister_netdevice_many(&del_list);
4c835019 629 }
a2dc375e
AO
630
631out:
632 return NOTIFY_DONE;
633}
634
635static struct notifier_block lowpan_dev_notifier = {
636 .notifier_call = lowpan_device_event,
637};
638
44331fe2 639static struct packet_type lowpan_packet_type = {
ec633eb5 640 .type = htons(ETH_P_IEEE802154),
44331fe2
AS
641 .func = lowpan_rcv,
642};
643
644static int __init lowpan_init_module(void)
645{
646 int err = 0;
647
7240cdec 648 err = lowpan_net_frag_init();
44331fe2
AS
649 if (err < 0)
650 goto out;
651
7240cdec
AA
652 err = lowpan_netlink_init();
653 if (err < 0)
654 goto out_frag;
655
44331fe2 656 dev_add_pack(&lowpan_packet_type);
a2dc375e
AO
657
658 err = register_netdevice_notifier(&lowpan_dev_notifier);
7240cdec
AA
659 if (err < 0)
660 goto out_pack;
661
662 return 0;
663
664out_pack:
665 dev_remove_pack(&lowpan_packet_type);
666 lowpan_netlink_fini();
667out_frag:
668 lowpan_net_frag_exit();
44331fe2
AS
669out:
670 return err;
671}
672
673static void __exit lowpan_cleanup_module(void)
674{
44331fe2
AS
675 lowpan_netlink_fini();
676
677 dev_remove_pack(&lowpan_packet_type);
33c34c5e 678
7240cdec 679 lowpan_net_frag_exit();
a2dc375e 680
7240cdec 681 unregister_netdevice_notifier(&lowpan_dev_notifier);
44331fe2
AS
682}
683
684module_init(lowpan_init_module);
685module_exit(lowpan_cleanup_module);
686MODULE_LICENSE("GPL");
687MODULE_ALIAS_RTNL_LINK("lowpan");