blob: ba6c641636852119c69991d13d537d917d7372d5 [file] [log] [blame]
Jukka Rissanen18722c22013-12-11 17:05:37 +02001/*
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03002 Copyright (c) 2013-2014 Intel Corp.
Jukka Rissanen18722c22013-12-11 17:05:37 +02003
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 and
6 only version 2 as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12*/
13
Jukka Rissanen18722c22013-12-11 17:05:37 +020014#include <linux/if_arp.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
Jukka Rissanen5547e482014-06-18 16:37:09 +030017#include <linux/module.h>
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030018#include <linux/debugfs.h>
Jukka Rissanen18722c22013-12-11 17:05:37 +020019
20#include <net/ipv6.h>
21#include <net/ip6_route.h>
22#include <net/addrconf.h>
23
24#include <net/af_ieee802154.h> /* to get the address type */
25
26#include <net/bluetooth/bluetooth.h>
27#include <net/bluetooth/hci_core.h>
28#include <net/bluetooth/l2cap.h>
29
Alexander Aringcefc8c82014-03-05 14:29:05 +010030#include <net/6lowpan.h> /* for the compression support */
Jukka Rissanen18722c22013-12-11 17:05:37 +020031
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030032#define VERSION "0.1"
33
34static struct dentry *lowpan_psm_debugfs;
35static struct dentry *lowpan_control_debugfs;
36
Jukka Rissanen18722c22013-12-11 17:05:37 +020037#define IFACE_NAME_TEMPLATE "bt%d"
38#define EUI64_ADDR_LEN 8
39
40struct skb_cb {
41 struct in6_addr addr;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030042 struct l2cap_chan *chan;
43 int status;
Jukka Rissanen18722c22013-12-11 17:05:37 +020044};
45#define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
46
47/* The devices list contains those devices that we are acting
48 * as a proxy. The BT 6LoWPAN device is a virtual device that
49 * connects to the Bluetooth LE device. The real connection to
50 * BT device is done via l2cap layer. There exists one
51 * virtual device / one BT 6LoWPAN network (=hciX device).
52 * The list contains struct lowpan_dev elements.
53 */
54static LIST_HEAD(bt_6lowpan_devices);
55static DEFINE_RWLOCK(devices_lock);
56
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030057/* If psm is set to 0 (default value), then 6lowpan is disabled.
58 * Other values are used to indicate a Protocol Service Multiplexer
59 * value for 6lowpan.
60 */
61static u16 psm_6lowpan;
62
63/* We are listening incoming connections via this channel
64 */
65static struct l2cap_chan *listen_chan;
66
Jukka Rissanen18722c22013-12-11 17:05:37 +020067struct lowpan_peer {
68 struct list_head list;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030069 struct l2cap_chan *chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +020070
71 /* peer addresses in various formats */
72 unsigned char eui64_addr[EUI64_ADDR_LEN];
73 struct in6_addr peer_addr;
74};
75
76struct lowpan_dev {
77 struct list_head list;
78
79 struct hci_dev *hdev;
80 struct net_device *netdev;
81 struct list_head peers;
82 atomic_t peer_count; /* number of items in peers list */
83
84 struct work_struct delete_netdev;
85 struct delayed_work notify_peers;
86};
87
88static inline struct lowpan_dev *lowpan_dev(const struct net_device *netdev)
89{
90 return netdev_priv(netdev);
91}
92
93static inline void peer_add(struct lowpan_dev *dev, struct lowpan_peer *peer)
94{
95 list_add(&peer->list, &dev->peers);
96 atomic_inc(&dev->peer_count);
97}
98
99static inline bool peer_del(struct lowpan_dev *dev, struct lowpan_peer *peer)
100{
101 list_del(&peer->list);
102
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300103 module_put(THIS_MODULE);
104
Jukka Rissanen18722c22013-12-11 17:05:37 +0200105 if (atomic_dec_and_test(&dev->peer_count)) {
106 BT_DBG("last peer");
107 return true;
108 }
109
110 return false;
111}
112
113static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_dev *dev,
114 bdaddr_t *ba, __u8 type)
115{
116 struct lowpan_peer *peer, *tmp;
117
118 BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count),
119 ba, type);
120
121 list_for_each_entry_safe(peer, tmp, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300122 BT_DBG("dst addr %pMR dst type %d",
123 &peer->chan->dst, peer->chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200124
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300125 if (bacmp(&peer->chan->dst, ba))
Jukka Rissanen18722c22013-12-11 17:05:37 +0200126 continue;
127
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300128 if (type == peer->chan->dst_type)
129 return peer;
130 }
131
132 return NULL;
133}
134
135static inline struct lowpan_peer *peer_lookup_chan(struct lowpan_dev *dev,
136 struct l2cap_chan *chan)
137{
138 struct lowpan_peer *peer, *tmp;
139
140 list_for_each_entry_safe(peer, tmp, &dev->peers, list) {
141 if (peer->chan == chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200142 return peer;
143 }
144
145 return NULL;
146}
147
148static inline struct lowpan_peer *peer_lookup_conn(struct lowpan_dev *dev,
149 struct l2cap_conn *conn)
150{
151 struct lowpan_peer *peer, *tmp;
152
153 list_for_each_entry_safe(peer, tmp, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300154 if (peer->chan->conn == conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200155 return peer;
156 }
157
158 return NULL;
159}
160
161static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
162{
163 struct lowpan_dev *entry, *tmp;
164 struct lowpan_peer *peer = NULL;
165 unsigned long flags;
166
167 read_lock_irqsave(&devices_lock, flags);
168
169 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
170 peer = peer_lookup_conn(entry, conn);
171 if (peer)
172 break;
173 }
174
175 read_unlock_irqrestore(&devices_lock, flags);
176
177 return peer;
178}
179
180static struct lowpan_dev *lookup_dev(struct l2cap_conn *conn)
181{
182 struct lowpan_dev *entry, *tmp;
183 struct lowpan_dev *dev = NULL;
184 unsigned long flags;
185
186 read_lock_irqsave(&devices_lock, flags);
187
188 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
189 if (conn->hcon->hdev == entry->hdev) {
190 dev = entry;
191 break;
192 }
193 }
194
195 read_unlock_irqrestore(&devices_lock, flags);
196
197 return dev;
198}
199
Jukka Rissanen18722c22013-12-11 17:05:37 +0200200static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
201{
202 struct sk_buff *skb_cp;
203 int ret;
204
205 skb_cp = skb_copy(skb, GFP_ATOMIC);
206 if (!skb_cp)
207 return -ENOMEM;
208
209 ret = netif_rx(skb_cp);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300210 if (ret < 0) {
211 BT_DBG("receive skb %d", ret);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200212 return NET_RX_DROP;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300213 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200214
215 return ret;
216}
217
218static int process_data(struct sk_buff *skb, struct net_device *netdev,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300219 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200220{
221 const u8 *saddr, *daddr;
222 u8 iphc0, iphc1;
223 struct lowpan_dev *dev;
224 struct lowpan_peer *peer;
225 unsigned long flags;
226
227 dev = lowpan_dev(netdev);
228
229 read_lock_irqsave(&devices_lock, flags);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300230 peer = peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200231 read_unlock_irqrestore(&devices_lock, flags);
232 if (!peer)
233 goto drop;
234
235 saddr = peer->eui64_addr;
236 daddr = dev->netdev->dev_addr;
237
238 /* at least two bytes will be used for the encoding */
239 if (skb->len < 2)
240 goto drop;
241
242 if (lowpan_fetch_skb_u8(skb, &iphc0))
243 goto drop;
244
245 if (lowpan_fetch_skb_u8(skb, &iphc1))
246 goto drop;
247
248 return lowpan_process_data(skb, netdev,
249 saddr, IEEE802154_ADDR_LONG, EUI64_ADDR_LEN,
250 daddr, IEEE802154_ADDR_LONG, EUI64_ADDR_LEN,
251 iphc0, iphc1, give_skb_to_upper);
252
253drop:
254 kfree_skb(skb);
255 return -EINVAL;
256}
257
258static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300259 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200260{
261 struct sk_buff *local_skb;
262 int ret;
263
264 if (!netif_running(dev))
265 goto drop;
266
267 if (dev->type != ARPHRD_6LOWPAN)
268 goto drop;
269
270 /* check that it's our buffer */
271 if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
272 /* Copy the packet so that the IPv6 header is
273 * properly aligned.
274 */
275 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
276 skb_tailroom(skb), GFP_ATOMIC);
277 if (!local_skb)
278 goto drop;
279
280 local_skb->protocol = htons(ETH_P_IPV6);
281 local_skb->pkt_type = PACKET_HOST;
282
283 skb_reset_network_header(local_skb);
284 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
285
286 if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
287 kfree_skb(local_skb);
288 goto drop;
289 }
290
291 dev->stats.rx_bytes += skb->len;
292 dev->stats.rx_packets++;
293
294 kfree_skb(local_skb);
295 kfree_skb(skb);
296 } else {
297 switch (skb->data[0] & 0xe0) {
298 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
299 local_skb = skb_clone(skb, GFP_ATOMIC);
300 if (!local_skb)
301 goto drop;
302
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300303 ret = process_data(local_skb, dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200304 if (ret != NET_RX_SUCCESS)
305 goto drop;
306
307 dev->stats.rx_bytes += skb->len;
308 dev->stats.rx_packets++;
309
310 kfree_skb(skb);
311 break;
312 default:
313 break;
314 }
315 }
316
317 return NET_RX_SUCCESS;
318
319drop:
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300320 dev->stats.rx_dropped++;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200321 kfree_skb(skb);
322 return NET_RX_DROP;
323}
324
325/* Packet from BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300326static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200327{
328 struct lowpan_dev *dev;
329 struct lowpan_peer *peer;
330 int err;
331
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300332 peer = lookup_peer(chan->conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200333 if (!peer)
334 return -ENOENT;
335
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300336 dev = lookup_dev(chan->conn);
Johan Hedberg30d3db42013-12-12 09:53:21 +0200337 if (!dev || !dev->netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200338 return -ENOENT;
339
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300340 err = recv_pkt(skb, dev->netdev, chan);
341 if (err) {
342 BT_DBG("recv pkt %d", err);
343 err = -EAGAIN;
344 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200345
346 return err;
347}
348
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300349static u8 get_addr_type_from_eui64(u8 byte)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200350{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300351 /* Is universal(0) or local(1) bit */
352 return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC);
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300353}
354
355static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr)
356{
357 u8 *eui64 = ip6_daddr->s6_addr + 8;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200358
359 addr->b[0] = eui64[7];
360 addr->b[1] = eui64[6];
361 addr->b[2] = eui64[5];
362 addr->b[3] = eui64[2];
363 addr->b[4] = eui64[1];
364 addr->b[5] = eui64[0];
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300365}
Jukka Rissanen18722c22013-12-11 17:05:37 +0200366
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300367static void convert_dest_bdaddr(struct in6_addr *ip6_daddr,
368 bdaddr_t *addr, u8 *addr_type)
369{
370 copy_to_bdaddr(ip6_daddr, addr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200371
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300372 /* We need to toggle the U/L bit that we got from IPv6 address
373 * so that we get the proper address and type of the BD address.
374 */
375 addr->b[5] ^= 0x02;
376
377 *addr_type = get_addr_type_from_eui64(addr->b[5]);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200378}
379
380static int header_create(struct sk_buff *skb, struct net_device *netdev,
381 unsigned short type, const void *_daddr,
382 const void *_saddr, unsigned int len)
383{
384 struct ipv6hdr *hdr;
385 struct lowpan_dev *dev;
386 struct lowpan_peer *peer;
387 bdaddr_t addr, *any = BDADDR_ANY;
388 u8 *saddr, *daddr = any->b;
389 u8 addr_type;
390
391 if (type != ETH_P_IPV6)
392 return -EINVAL;
393
394 hdr = ipv6_hdr(skb);
395
396 dev = lowpan_dev(netdev);
397
398 if (ipv6_addr_is_multicast(&hdr->daddr)) {
399 memcpy(&lowpan_cb(skb)->addr, &hdr->daddr,
400 sizeof(struct in6_addr));
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300401 lowpan_cb(skb)->chan = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200402 } else {
403 unsigned long flags;
404
405 /* Get destination BT device from skb.
406 * If there is no such peer then discard the packet.
407 */
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300408 convert_dest_bdaddr(&hdr->daddr, &addr, &addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200409
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300410 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr,
411 addr_type, &hdr->daddr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200412
413 read_lock_irqsave(&devices_lock, flags);
414 peer = peer_lookup_ba(dev, &addr, addr_type);
415 read_unlock_irqrestore(&devices_lock, flags);
416
417 if (!peer) {
418 BT_DBG("no such peer %pMR found", &addr);
419 return -ENOENT;
420 }
421
422 daddr = peer->eui64_addr;
423
424 memcpy(&lowpan_cb(skb)->addr, &hdr->daddr,
425 sizeof(struct in6_addr));
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300426 lowpan_cb(skb)->chan = peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200427 }
428
429 saddr = dev->netdev->dev_addr;
430
431 return lowpan_header_compress(skb, netdev, type, daddr, saddr, len);
432}
433
434/* Packet to BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300435static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
Jukka Rissanen18722c22013-12-11 17:05:37 +0200436 struct net_device *netdev)
437{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300438 struct msghdr msg;
439 struct kvec iv;
440 int err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200441
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300442 /* Remember the skb so that we can send EAGAIN to the caller if
443 * we run out of credits.
444 */
445 chan->data = skb;
446
447 memset(&msg, 0, sizeof(msg));
448 msg.msg_iov = (struct iovec *) &iv;
449 msg.msg_iovlen = 1;
450 iv.iov_base = skb->data;
451 iv.iov_len = skb->len;
452
453 err = l2cap_chan_send(chan, &msg, skb->len);
454 if (err > 0) {
455 netdev->stats.tx_bytes += err;
456 netdev->stats.tx_packets++;
457 return 0;
458 }
459
460 if (!err)
461 err = lowpan_cb(skb)->status;
462
463 if (err < 0) {
464 if (err == -EAGAIN)
465 netdev->stats.tx_dropped++;
466 else
467 netdev->stats.tx_errors++;
468 }
469
470 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200471}
472
473static void send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
474{
475 struct sk_buff *local_skb;
476 struct lowpan_dev *entry, *tmp;
477 unsigned long flags;
478
479 read_lock_irqsave(&devices_lock, flags);
480
481 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
482 struct lowpan_peer *pentry, *ptmp;
483 struct lowpan_dev *dev;
484
485 if (entry->netdev != netdev)
486 continue;
487
488 dev = lowpan_dev(entry->netdev);
489
490 list_for_each_entry_safe(pentry, ptmp, &dev->peers, list) {
491 local_skb = skb_clone(skb, GFP_ATOMIC);
492
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300493 send_pkt(pentry->chan, local_skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200494
495 kfree_skb(local_skb);
496 }
497 }
498
499 read_unlock_irqrestore(&devices_lock, flags);
500}
501
502static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
503{
504 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200505 struct lowpan_dev *dev;
506 struct lowpan_peer *peer;
507 bdaddr_t addr;
508 u8 addr_type;
509
510 if (ipv6_addr_is_multicast(&lowpan_cb(skb)->addr)) {
511 /* We need to send the packet to every device
512 * behind this interface.
513 */
514 send_mcast_pkt(skb, netdev);
515 } else {
516 unsigned long flags;
517
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300518 convert_dest_bdaddr(&lowpan_cb(skb)->addr, &addr, &addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200519 dev = lowpan_dev(netdev);
520
521 read_lock_irqsave(&devices_lock, flags);
522 peer = peer_lookup_ba(dev, &addr, addr_type);
523 read_unlock_irqrestore(&devices_lock, flags);
524
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300525 BT_DBG("xmit %s to %pMR type %d IP %pI6c peer %p",
526 netdev->name, &addr, addr_type,
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300527 &lowpan_cb(skb)->addr, peer);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200528
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300529 if (peer && peer->chan)
530 err = send_pkt(peer->chan, skb, netdev);
531 else
532 err = -ENOENT;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200533 }
534 dev_kfree_skb(skb);
535
536 if (err)
537 BT_DBG("ERROR: xmit failed (%d)", err);
538
539 return (err < 0) ? NET_XMIT_DROP : err;
540}
541
542static const struct net_device_ops netdev_ops = {
543 .ndo_start_xmit = bt_xmit,
544};
545
546static struct header_ops header_ops = {
547 .create = header_create,
548};
549
550static void netdev_setup(struct net_device *dev)
551{
552 dev->addr_len = EUI64_ADDR_LEN;
553 dev->type = ARPHRD_6LOWPAN;
554
555 dev->hard_header_len = 0;
556 dev->needed_tailroom = 0;
557 dev->mtu = IPV6_MIN_MTU;
558 dev->tx_queue_len = 0;
559 dev->flags = IFF_RUNNING | IFF_POINTOPOINT;
560 dev->watchdog_timeo = 0;
561
562 dev->netdev_ops = &netdev_ops;
563 dev->header_ops = &header_ops;
564 dev->destructor = free_netdev;
565}
566
567static struct device_type bt_type = {
568 .name = "bluetooth",
569};
570
571static void set_addr(u8 *eui, u8 *addr, u8 addr_type)
572{
573 /* addr is the BT address in little-endian format */
574 eui[0] = addr[5];
575 eui[1] = addr[4];
576 eui[2] = addr[3];
577 eui[3] = 0xFF;
578 eui[4] = 0xFE;
579 eui[5] = addr[2];
580 eui[6] = addr[1];
581 eui[7] = addr[0];
582
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300583 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300584 if (addr_type == BDADDR_LE_PUBLIC)
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300585 eui[0] &= ~0x02;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200586 else
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300587 eui[0] |= 0x02;
588
589 BT_DBG("type %d addr %*phC", addr_type, 8, eui);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200590}
591
592static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr,
593 u8 addr_type)
594{
595 netdev->addr_assign_type = NET_ADDR_PERM;
596 set_addr(netdev->dev_addr, addr->b, addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200597}
598
599static void ifup(struct net_device *netdev)
600{
601 int err;
602
603 rtnl_lock();
604 err = dev_open(netdev);
605 if (err < 0)
606 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
607 rtnl_unlock();
608}
609
610static void do_notify_peers(struct work_struct *work)
611{
612 struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
613 notify_peers.work);
614
615 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
616}
617
618static bool is_bt_6lowpan(struct hci_conn *hcon)
619{
620 if (hcon->type != LE_LINK)
621 return false;
622
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300623 if (!psm_6lowpan)
624 return false;
625
626 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200627}
628
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300629static struct l2cap_chan *chan_create(void)
630{
631 struct l2cap_chan *chan;
632
633 chan = l2cap_chan_create();
634 if (!chan)
635 return NULL;
636
637 l2cap_chan_set_defaults(chan);
638
639 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
640 chan->mode = L2CAP_MODE_LE_FLOWCTL;
641 chan->omtu = 65535;
642 chan->imtu = chan->omtu;
643
644 return chan;
645}
646
647static struct l2cap_chan *chan_open(struct l2cap_chan *pchan)
648{
649 struct l2cap_chan *chan;
650
651 chan = chan_create();
652 if (!chan)
653 return NULL;
654
655 chan->remote_mps = chan->omtu;
656 chan->mps = chan->omtu;
657
658 chan->state = BT_CONNECTED;
659
660 return chan;
661}
662
663static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
664 struct lowpan_dev *dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200665{
666 struct lowpan_peer *peer;
667 unsigned long flags;
668
669 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
670 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300671 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200672
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300673 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200674 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
675
676 /* RFC 2464 ch. 5 */
677 peer->peer_addr.s6_addr[0] = 0xFE;
678 peer->peer_addr.s6_addr[1] = 0x80;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300679 set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
680 chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200681
682 memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
683 EUI64_ADDR_LEN);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200684
685 write_lock_irqsave(&devices_lock, flags);
686 INIT_LIST_HEAD(&peer->list);
687 peer_add(dev, peer);
688 write_unlock_irqrestore(&devices_lock, flags);
689
690 /* Notifying peers about us needs to be done without locks held */
691 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
692 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
693
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300694 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200695}
696
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300697static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200698{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200699 struct net_device *netdev;
700 int err = 0;
701 unsigned long flags;
702
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300703 netdev = alloc_netdev(sizeof(struct lowpan_dev), IFACE_NAME_TEMPLATE,
704 netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200705 if (!netdev)
706 return -ENOMEM;
707
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300708 set_dev_addr(netdev, &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200709
710 netdev->netdev_ops = &netdev_ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300711 SET_NETDEV_DEV(netdev, &chan->conn->hcon->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200712 SET_NETDEV_DEVTYPE(netdev, &bt_type);
713
714 err = register_netdev(netdev);
715 if (err < 0) {
716 BT_INFO("register_netdev failed %d", err);
717 free_netdev(netdev);
718 goto out;
719 }
720
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300721 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
722 netdev->ifindex, &chan->dst, chan->dst_type,
723 &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200724 set_bit(__LINK_STATE_PRESENT, &netdev->state);
725
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300726 *dev = netdev_priv(netdev);
727 (*dev)->netdev = netdev;
728 (*dev)->hdev = chan->conn->hcon->hdev;
729 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200730
731 write_lock_irqsave(&devices_lock, flags);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300732 INIT_LIST_HEAD(&(*dev)->list);
733 list_add(&(*dev)->list, &bt_6lowpan_devices);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200734 write_unlock_irqrestore(&devices_lock, flags);
735
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300736 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200737
738out:
739 return err;
740}
741
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300742static inline void chan_ready_cb(struct l2cap_chan *chan)
743{
744 struct lowpan_dev *dev;
745
746 dev = lookup_dev(chan->conn);
747
748 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
749
750 if (!dev) {
751 if (setup_netdev(chan, &dev) < 0) {
752 l2cap_chan_del(chan, -ENOENT);
753 return;
754 }
755 }
756
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300757 if (!try_module_get(THIS_MODULE))
758 return;
759
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300760 add_peer_chan(chan, dev);
761 ifup(dev->netdev);
762}
763
764static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *chan)
765{
766 struct l2cap_chan *pchan;
767
768 pchan = chan_open(chan);
769 pchan->ops = chan->ops;
770
771 BT_DBG("chan %p pchan %p", chan, pchan);
772
773 return pchan;
774}
775
Jukka Rissanen18722c22013-12-11 17:05:37 +0200776static void delete_netdev(struct work_struct *work)
777{
778 struct lowpan_dev *entry = container_of(work, struct lowpan_dev,
779 delete_netdev);
780
781 unregister_netdev(entry->netdev);
782
783 /* The entry pointer is deleted in device_event() */
784}
785
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300786static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200787{
788 struct lowpan_dev *entry, *tmp;
789 struct lowpan_dev *dev = NULL;
790 struct lowpan_peer *peer;
791 int err = -ENOENT;
792 unsigned long flags;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300793 bool last = false, removed = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200794
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300795 BT_DBG("chan %p conn %p", chan, chan->conn);
796
797 if (chan->conn && chan->conn->hcon) {
798 if (!is_bt_6lowpan(chan->conn->hcon))
799 return;
800
801 /* If conn is set, then the netdev is also there and we should
802 * not remove it.
803 */
804 removed = false;
805 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200806
807 write_lock_irqsave(&devices_lock, flags);
808
809 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
810 dev = lowpan_dev(entry->netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300811 peer = peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200812 if (peer) {
813 last = peer_del(dev, peer);
814 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300815
816 BT_DBG("dev %p removing %speer %p", dev,
817 last ? "last " : "1 ", peer);
818 BT_DBG("chan %p orig refcnt %d", chan,
819 atomic_read(&chan->kref.refcount));
820
821 l2cap_chan_put(chan);
822 kfree(peer);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200823 break;
824 }
825 }
826
827 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
828 write_unlock_irqrestore(&devices_lock, flags);
829
830 cancel_delayed_work_sync(&dev->notify_peers);
831
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300832 if (!removed) {
833 INIT_WORK(&entry->delete_netdev, delete_netdev);
834 schedule_work(&entry->delete_netdev);
835 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200836 } else {
837 write_unlock_irqrestore(&devices_lock, flags);
838 }
839
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300840 return;
841}
842
843static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
844{
845 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
846 state_to_string(state), err);
847}
848
849static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
850 unsigned long hdr_len,
851 unsigned long len, int nb)
852{
853 /* Note that we must allocate using GFP_ATOMIC here as
854 * this function is called originally from netdev hard xmit
855 * function in atomic context.
856 */
857 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
858}
859
860static void chan_suspend_cb(struct l2cap_chan *chan)
861{
862 struct sk_buff *skb = chan->data;
863
864 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
865
866 lowpan_cb(skb)->status = -EAGAIN;
867}
868
869static void chan_resume_cb(struct l2cap_chan *chan)
870{
871 struct sk_buff *skb = chan->data;
872
873 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
874
875 lowpan_cb(skb)->status = 0;
876}
877
878static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
879{
880 return msecs_to_jiffies(1000);
881}
882
883static const struct l2cap_ops bt_6lowpan_chan_ops = {
884 .name = "L2CAP 6LoWPAN channel",
885 .new_connection = chan_new_conn_cb,
886 .recv = chan_recv_cb,
887 .close = chan_close_cb,
888 .state_change = chan_state_change_cb,
889 .ready = chan_ready_cb,
890 .resume = chan_resume_cb,
891 .suspend = chan_suspend_cb,
892 .get_sndtimeo = chan_get_sndtimeo_cb,
893 .alloc_skb = chan_alloc_skb_cb,
894 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
895
896 .teardown = l2cap_chan_no_teardown,
897 .defer = l2cap_chan_no_defer,
898 .set_shutdown = l2cap_chan_no_set_shutdown,
899};
900
901static inline __u8 bdaddr_type(__u8 type)
902{
903 if (type == ADDR_LE_DEV_PUBLIC)
904 return BDADDR_LE_PUBLIC;
905 else
906 return BDADDR_LE_RANDOM;
907}
908
909static struct l2cap_chan *chan_get(void)
910{
911 struct l2cap_chan *pchan;
912
913 pchan = chan_create();
914 if (!pchan)
915 return NULL;
916
917 pchan->ops = &bt_6lowpan_chan_ops;
918
919 return pchan;
920}
921
922static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
923{
924 struct l2cap_chan *pchan;
925 int err;
926
927 pchan = chan_get();
928 if (!pchan)
929 return -EINVAL;
930
931 err = l2cap_chan_connect(pchan, cpu_to_le16(psm_6lowpan), 0,
932 addr, dst_type);
933
934 BT_DBG("chan %p err %d", pchan, err);
935 if (err < 0)
936 l2cap_chan_put(pchan);
937
Jukka Rissanen18722c22013-12-11 17:05:37 +0200938 return err;
939}
940
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300941static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
942{
943 struct lowpan_peer *peer;
944
945 BT_DBG("conn %p dst type %d", conn, dst_type);
946
947 peer = lookup_peer(conn);
948 if (!peer)
949 return -ENOENT;
950
951 BT_DBG("peer %p chan %p", peer, peer->chan);
952
953 l2cap_chan_close(peer->chan, ENOENT);
954
955 return 0;
956}
957
958static struct l2cap_chan *bt_6lowpan_listen(void)
959{
960 bdaddr_t *addr = BDADDR_ANY;
961 struct l2cap_chan *pchan;
962 int err;
963
964 if (psm_6lowpan == 0)
965 return NULL;
966
967 pchan = chan_get();
968 if (!pchan)
969 return NULL;
970
971 pchan->state = BT_LISTEN;
972 pchan->src_type = BDADDR_LE_PUBLIC;
973
974 BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan, pchan,
975 pchan->src_type);
976
977 err = l2cap_add_psm(pchan, addr, cpu_to_le16(psm_6lowpan));
978 if (err) {
979 l2cap_chan_put(pchan);
980 BT_ERR("psm cannot be added err %d", err);
981 return NULL;
982 }
983
984 return pchan;
985}
986
987static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
988 struct l2cap_conn **conn)
989{
990 struct hci_conn *hcon;
991 struct hci_dev *hdev;
992 bdaddr_t *src = BDADDR_ANY;
993 int n;
994
995 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
996 &addr->b[5], &addr->b[4], &addr->b[3],
997 &addr->b[2], &addr->b[1], &addr->b[0],
998 addr_type);
999
1000 if (n < 7)
1001 return -EINVAL;
1002
1003 hdev = hci_get_route(addr, src);
1004 if (!hdev)
1005 return -ENOENT;
1006
1007 hci_dev_lock(hdev);
1008 hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
1009 hci_dev_unlock(hdev);
1010
1011 if (!hcon)
1012 return -ENOENT;
1013
1014 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1015
1016 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1017
1018 return 0;
1019}
1020
1021static void disconnect_all_peers(void)
1022{
1023 struct lowpan_dev *entry, *tmp_dev;
1024 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1025 struct list_head peers;
1026 unsigned long flags;
1027
1028 INIT_LIST_HEAD(&peers);
1029
1030 /* We make a separate list of peers as the close_cb() will
1031 * modify the device peers list so it is better not to mess
1032 * with the same list at the same time.
1033 */
1034
1035 read_lock_irqsave(&devices_lock, flags);
1036
1037 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1038 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list) {
1039 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1040 if (!new_peer)
1041 break;
1042
1043 new_peer->chan = peer->chan;
1044 INIT_LIST_HEAD(&new_peer->list);
1045
1046 list_add(&new_peer->list, &peers);
1047 }
1048 }
1049
1050 read_unlock_irqrestore(&devices_lock, flags);
1051
1052 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1053 l2cap_chan_close(peer->chan, ENOENT);
1054 kfree(peer);
1055 }
1056}
1057
1058static int lowpan_psm_set(void *data, u64 val)
1059{
1060 u16 psm;
1061
1062 psm = val;
1063 if (psm == 0 || psm_6lowpan != psm)
1064 /* Disconnect existing connections if 6lowpan is
1065 * disabled (psm = 0), or if psm changes.
1066 */
1067 disconnect_all_peers();
1068
1069 psm_6lowpan = psm;
1070
1071 if (listen_chan) {
1072 l2cap_chan_close(listen_chan, 0);
1073 l2cap_chan_put(listen_chan);
1074 }
1075
1076 listen_chan = bt_6lowpan_listen();
1077
1078 return 0;
1079}
1080
1081static int lowpan_psm_get(void *data, u64 *val)
1082{
1083 *val = psm_6lowpan;
1084 return 0;
1085}
1086
1087DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops, lowpan_psm_get,
1088 lowpan_psm_set, "%llu\n");
1089
1090static ssize_t lowpan_control_write(struct file *fp,
1091 const char __user *user_buffer,
1092 size_t count,
1093 loff_t *position)
1094{
1095 char buf[32];
1096 size_t buf_size = min(count, sizeof(buf) - 1);
1097 int ret;
1098 bdaddr_t addr;
1099 u8 addr_type;
1100 struct l2cap_conn *conn = NULL;
1101
1102 if (copy_from_user(buf, user_buffer, buf_size))
1103 return -EFAULT;
1104
1105 buf[buf_size] = '\0';
1106
1107 if (memcmp(buf, "connect ", 8) == 0) {
1108 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1109 if (ret == -EINVAL)
1110 return ret;
1111
1112 if (listen_chan) {
1113 l2cap_chan_close(listen_chan, 0);
1114 l2cap_chan_put(listen_chan);
1115 listen_chan = NULL;
1116 }
1117
1118 if (conn) {
1119 struct lowpan_peer *peer;
1120
1121 if (!is_bt_6lowpan(conn->hcon))
1122 return -EINVAL;
1123
1124 peer = lookup_peer(conn);
1125 if (peer) {
1126 BT_DBG("6LoWPAN connection already exists");
1127 return -EALREADY;
1128 }
1129
1130 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1131 &conn->hcon->dst, conn->hcon->dst_type,
1132 addr_type);
1133 }
1134
1135 ret = bt_6lowpan_connect(&addr, addr_type);
1136 if (ret < 0)
1137 return ret;
1138
1139 return count;
1140 }
1141
1142 if (memcmp(buf, "disconnect ", 11) == 0) {
1143 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1144 if (ret < 0)
1145 return ret;
1146
1147 ret = bt_6lowpan_disconnect(conn, addr_type);
1148 if (ret < 0)
1149 return ret;
1150
1151 return count;
1152 }
1153
1154 return count;
1155}
1156
1157static int lowpan_control_show(struct seq_file *f, void *ptr)
1158{
1159 struct lowpan_dev *entry, *tmp_dev;
1160 struct lowpan_peer *peer, *tmp_peer;
1161 unsigned long flags;
1162
1163 read_lock_irqsave(&devices_lock, flags);
1164
1165 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1166 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list)
1167 seq_printf(f, "%pMR (type %u)\n",
1168 &peer->chan->dst, peer->chan->dst_type);
1169 }
1170
1171 read_unlock_irqrestore(&devices_lock, flags);
1172
1173 return 0;
1174}
1175
1176static int lowpan_control_open(struct inode *inode, struct file *file)
1177{
1178 return single_open(file, lowpan_control_show, inode->i_private);
1179}
1180
1181static const struct file_operations lowpan_control_fops = {
1182 .open = lowpan_control_open,
1183 .read = seq_read,
1184 .write = lowpan_control_write,
1185 .llseek = seq_lseek,
1186 .release = single_release,
1187};
1188
Jukka Rissanen18722c22013-12-11 17:05:37 +02001189static int device_event(struct notifier_block *unused,
1190 unsigned long event, void *ptr)
1191{
1192 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1193 struct lowpan_dev *entry, *tmp;
1194 unsigned long flags;
1195
1196 if (netdev->type != ARPHRD_6LOWPAN)
1197 return NOTIFY_DONE;
1198
1199 switch (event) {
1200 case NETDEV_UNREGISTER:
1201 write_lock_irqsave(&devices_lock, flags);
1202 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices,
1203 list) {
1204 if (entry->netdev == netdev) {
1205 list_del(&entry->list);
1206 kfree(entry);
1207 break;
1208 }
1209 }
1210 write_unlock_irqrestore(&devices_lock, flags);
1211 break;
1212 }
1213
1214 return NOTIFY_DONE;
1215}
1216
1217static struct notifier_block bt_6lowpan_dev_notifier = {
1218 .notifier_call = device_event,
1219};
1220
Jukka Rissanen5547e482014-06-18 16:37:09 +03001221static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001222{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001223 lowpan_psm_debugfs = debugfs_create_file("6lowpan_psm", 0644,
1224 bt_debugfs, NULL,
1225 &lowpan_psm_fops);
1226 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1227 bt_debugfs, NULL,
1228 &lowpan_control_fops);
1229
Jukka Rissanen18722c22013-12-11 17:05:37 +02001230 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1231}
1232
Jukka Rissanen5547e482014-06-18 16:37:09 +03001233static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001234{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001235 debugfs_remove(lowpan_psm_debugfs);
1236 debugfs_remove(lowpan_control_debugfs);
1237
1238 if (listen_chan) {
1239 l2cap_chan_close(listen_chan, 0);
1240 l2cap_chan_put(listen_chan);
1241 }
1242
Jukka Rissanen18722c22013-12-11 17:05:37 +02001243 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1244}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001245
1246module_init(bt_6lowpan_init);
1247module_exit(bt_6lowpan_exit);
1248
1249MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1250MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1251MODULE_VERSION(VERSION);
1252MODULE_LICENSE("GPL");