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