blob: b229aed3bf9a53c3d6f7cc5348bca59ba934135e [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
Jukka Rissanen7f118252014-06-18 16:37:11 +0300610static void ifdown(struct net_device *netdev)
611{
612 int err;
613
614 rtnl_lock();
615 err = dev_close(netdev);
616 if (err < 0)
617 BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
618 rtnl_unlock();
619}
620
Jukka Rissanen18722c22013-12-11 17:05:37 +0200621static void do_notify_peers(struct work_struct *work)
622{
623 struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
624 notify_peers.work);
625
626 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
627}
628
629static bool is_bt_6lowpan(struct hci_conn *hcon)
630{
631 if (hcon->type != LE_LINK)
632 return false;
633
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300634 if (!psm_6lowpan)
635 return false;
636
637 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200638}
639
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300640static struct l2cap_chan *chan_create(void)
641{
642 struct l2cap_chan *chan;
643
644 chan = l2cap_chan_create();
645 if (!chan)
646 return NULL;
647
648 l2cap_chan_set_defaults(chan);
649
650 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
651 chan->mode = L2CAP_MODE_LE_FLOWCTL;
652 chan->omtu = 65535;
653 chan->imtu = chan->omtu;
654
655 return chan;
656}
657
658static struct l2cap_chan *chan_open(struct l2cap_chan *pchan)
659{
660 struct l2cap_chan *chan;
661
662 chan = chan_create();
663 if (!chan)
664 return NULL;
665
666 chan->remote_mps = chan->omtu;
667 chan->mps = chan->omtu;
668
669 chan->state = BT_CONNECTED;
670
671 return chan;
672}
673
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300674static void set_ip_addr_bits(u8 addr_type, u8 *addr)
675{
676 if (addr_type == BDADDR_LE_PUBLIC)
677 *addr |= 0x02;
678 else
679 *addr &= ~0x02;
680}
681
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300682static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
683 struct lowpan_dev *dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200684{
685 struct lowpan_peer *peer;
686 unsigned long flags;
687
688 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
689 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300690 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200691
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300692 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200693 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
694
695 /* RFC 2464 ch. 5 */
696 peer->peer_addr.s6_addr[0] = 0xFE;
697 peer->peer_addr.s6_addr[1] = 0x80;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300698 set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
699 chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200700
701 memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
702 EUI64_ADDR_LEN);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200703
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300704 /* IPv6 address needs to have the U/L bit set properly so toggle
705 * it back here.
706 */
707 set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8);
708
Jukka Rissanen18722c22013-12-11 17:05:37 +0200709 write_lock_irqsave(&devices_lock, flags);
710 INIT_LIST_HEAD(&peer->list);
711 peer_add(dev, peer);
712 write_unlock_irqrestore(&devices_lock, flags);
713
714 /* Notifying peers about us needs to be done without locks held */
715 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
716 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
717
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300718 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200719}
720
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300721static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200722{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200723 struct net_device *netdev;
724 int err = 0;
725 unsigned long flags;
726
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300727 netdev = alloc_netdev(sizeof(struct lowpan_dev), IFACE_NAME_TEMPLATE,
Tom Gundersenc835a672014-07-14 16:37:24 +0200728 NET_NAME_UNKNOWN, netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200729 if (!netdev)
730 return -ENOMEM;
731
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300732 set_dev_addr(netdev, &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200733
734 netdev->netdev_ops = &netdev_ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300735 SET_NETDEV_DEV(netdev, &chan->conn->hcon->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200736 SET_NETDEV_DEVTYPE(netdev, &bt_type);
737
738 err = register_netdev(netdev);
739 if (err < 0) {
740 BT_INFO("register_netdev failed %d", err);
741 free_netdev(netdev);
742 goto out;
743 }
744
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300745 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
746 netdev->ifindex, &chan->dst, chan->dst_type,
747 &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200748 set_bit(__LINK_STATE_PRESENT, &netdev->state);
749
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300750 *dev = netdev_priv(netdev);
751 (*dev)->netdev = netdev;
752 (*dev)->hdev = chan->conn->hcon->hdev;
753 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200754
755 write_lock_irqsave(&devices_lock, flags);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300756 INIT_LIST_HEAD(&(*dev)->list);
757 list_add(&(*dev)->list, &bt_6lowpan_devices);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200758 write_unlock_irqrestore(&devices_lock, flags);
759
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300760 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200761
762out:
763 return err;
764}
765
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300766static inline void chan_ready_cb(struct l2cap_chan *chan)
767{
768 struct lowpan_dev *dev;
769
770 dev = lookup_dev(chan->conn);
771
772 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
773
774 if (!dev) {
775 if (setup_netdev(chan, &dev) < 0) {
776 l2cap_chan_del(chan, -ENOENT);
777 return;
778 }
779 }
780
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300781 if (!try_module_get(THIS_MODULE))
782 return;
783
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300784 add_peer_chan(chan, dev);
785 ifup(dev->netdev);
786}
787
Johan Hedberg2b293492014-08-07 10:03:32 +0300788static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300789{
Johan Hedberg2b293492014-08-07 10:03:32 +0300790 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300791
Johan Hedberg2b293492014-08-07 10:03:32 +0300792 chan = chan_open(pchan);
793 chan->ops = pchan->ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300794
795 BT_DBG("chan %p pchan %p", chan, pchan);
796
Johan Hedberg2b293492014-08-07 10:03:32 +0300797 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300798}
799
Jukka Rissanen18722c22013-12-11 17:05:37 +0200800static void delete_netdev(struct work_struct *work)
801{
802 struct lowpan_dev *entry = container_of(work, struct lowpan_dev,
803 delete_netdev);
804
805 unregister_netdev(entry->netdev);
806
807 /* The entry pointer is deleted in device_event() */
808}
809
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300810static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200811{
812 struct lowpan_dev *entry, *tmp;
813 struct lowpan_dev *dev = NULL;
814 struct lowpan_peer *peer;
815 int err = -ENOENT;
816 unsigned long flags;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300817 bool last = false, removed = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200818
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300819 BT_DBG("chan %p conn %p", chan, chan->conn);
820
821 if (chan->conn && chan->conn->hcon) {
822 if (!is_bt_6lowpan(chan->conn->hcon))
823 return;
824
825 /* If conn is set, then the netdev is also there and we should
826 * not remove it.
827 */
828 removed = false;
829 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200830
831 write_lock_irqsave(&devices_lock, flags);
832
833 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
834 dev = lowpan_dev(entry->netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300835 peer = peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200836 if (peer) {
837 last = peer_del(dev, peer);
838 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300839
840 BT_DBG("dev %p removing %speer %p", dev,
841 last ? "last " : "1 ", peer);
842 BT_DBG("chan %p orig refcnt %d", chan,
843 atomic_read(&chan->kref.refcount));
844
845 l2cap_chan_put(chan);
846 kfree(peer);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200847 break;
848 }
849 }
850
851 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
852 write_unlock_irqrestore(&devices_lock, flags);
853
854 cancel_delayed_work_sync(&dev->notify_peers);
855
Jukka Rissanen7f118252014-06-18 16:37:11 +0300856 ifdown(dev->netdev);
857
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300858 if (!removed) {
859 INIT_WORK(&entry->delete_netdev, delete_netdev);
860 schedule_work(&entry->delete_netdev);
861 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200862 } else {
863 write_unlock_irqrestore(&devices_lock, flags);
864 }
865
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300866 return;
867}
868
869static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
870{
871 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
872 state_to_string(state), err);
873}
874
875static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
876 unsigned long hdr_len,
877 unsigned long len, int nb)
878{
879 /* Note that we must allocate using GFP_ATOMIC here as
880 * this function is called originally from netdev hard xmit
881 * function in atomic context.
882 */
883 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
884}
885
886static void chan_suspend_cb(struct l2cap_chan *chan)
887{
888 struct sk_buff *skb = chan->data;
889
890 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
891
892 lowpan_cb(skb)->status = -EAGAIN;
893}
894
895static void chan_resume_cb(struct l2cap_chan *chan)
896{
897 struct sk_buff *skb = chan->data;
898
899 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
900
901 lowpan_cb(skb)->status = 0;
902}
903
904static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
905{
Jukka Rissanen2ae50d82014-09-08 12:11:43 +0300906 return L2CAP_CONN_TIMEOUT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300907}
908
909static const struct l2cap_ops bt_6lowpan_chan_ops = {
910 .name = "L2CAP 6LoWPAN channel",
911 .new_connection = chan_new_conn_cb,
912 .recv = chan_recv_cb,
913 .close = chan_close_cb,
914 .state_change = chan_state_change_cb,
915 .ready = chan_ready_cb,
916 .resume = chan_resume_cb,
917 .suspend = chan_suspend_cb,
918 .get_sndtimeo = chan_get_sndtimeo_cb,
919 .alloc_skb = chan_alloc_skb_cb,
920 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
921
922 .teardown = l2cap_chan_no_teardown,
923 .defer = l2cap_chan_no_defer,
924 .set_shutdown = l2cap_chan_no_set_shutdown,
925};
926
927static inline __u8 bdaddr_type(__u8 type)
928{
929 if (type == ADDR_LE_DEV_PUBLIC)
930 return BDADDR_LE_PUBLIC;
931 else
932 return BDADDR_LE_RANDOM;
933}
934
935static struct l2cap_chan *chan_get(void)
936{
937 struct l2cap_chan *pchan;
938
939 pchan = chan_create();
940 if (!pchan)
941 return NULL;
942
943 pchan->ops = &bt_6lowpan_chan_ops;
944
945 return pchan;
946}
947
948static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
949{
950 struct l2cap_chan *pchan;
951 int err;
952
953 pchan = chan_get();
954 if (!pchan)
955 return -EINVAL;
956
957 err = l2cap_chan_connect(pchan, cpu_to_le16(psm_6lowpan), 0,
958 addr, dst_type);
959
960 BT_DBG("chan %p err %d", pchan, err);
961 if (err < 0)
962 l2cap_chan_put(pchan);
963
Jukka Rissanen18722c22013-12-11 17:05:37 +0200964 return err;
965}
966
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300967static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
968{
969 struct lowpan_peer *peer;
970
971 BT_DBG("conn %p dst type %d", conn, dst_type);
972
973 peer = lookup_peer(conn);
974 if (!peer)
975 return -ENOENT;
976
977 BT_DBG("peer %p chan %p", peer, peer->chan);
978
979 l2cap_chan_close(peer->chan, ENOENT);
980
981 return 0;
982}
983
984static struct l2cap_chan *bt_6lowpan_listen(void)
985{
986 bdaddr_t *addr = BDADDR_ANY;
987 struct l2cap_chan *pchan;
988 int err;
989
990 if (psm_6lowpan == 0)
991 return NULL;
992
993 pchan = chan_get();
994 if (!pchan)
995 return NULL;
996
997 pchan->state = BT_LISTEN;
998 pchan->src_type = BDADDR_LE_PUBLIC;
999
1000 BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan, pchan,
1001 pchan->src_type);
1002
1003 err = l2cap_add_psm(pchan, addr, cpu_to_le16(psm_6lowpan));
1004 if (err) {
1005 l2cap_chan_put(pchan);
1006 BT_ERR("psm cannot be added err %d", err);
1007 return NULL;
1008 }
1009
1010 return pchan;
1011}
1012
1013static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
1014 struct l2cap_conn **conn)
1015{
1016 struct hci_conn *hcon;
1017 struct hci_dev *hdev;
1018 bdaddr_t *src = BDADDR_ANY;
1019 int n;
1020
1021 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1022 &addr->b[5], &addr->b[4], &addr->b[3],
1023 &addr->b[2], &addr->b[1], &addr->b[0],
1024 addr_type);
1025
1026 if (n < 7)
1027 return -EINVAL;
1028
1029 hdev = hci_get_route(addr, src);
1030 if (!hdev)
1031 return -ENOENT;
1032
1033 hci_dev_lock(hdev);
1034 hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
1035 hci_dev_unlock(hdev);
1036
1037 if (!hcon)
1038 return -ENOENT;
1039
1040 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1041
1042 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1043
1044 return 0;
1045}
1046
1047static void disconnect_all_peers(void)
1048{
1049 struct lowpan_dev *entry, *tmp_dev;
1050 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1051 struct list_head peers;
1052 unsigned long flags;
1053
1054 INIT_LIST_HEAD(&peers);
1055
1056 /* We make a separate list of peers as the close_cb() will
1057 * modify the device peers list so it is better not to mess
1058 * with the same list at the same time.
1059 */
1060
1061 read_lock_irqsave(&devices_lock, flags);
1062
1063 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1064 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list) {
1065 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1066 if (!new_peer)
1067 break;
1068
1069 new_peer->chan = peer->chan;
1070 INIT_LIST_HEAD(&new_peer->list);
1071
1072 list_add(&new_peer->list, &peers);
1073 }
1074 }
1075
1076 read_unlock_irqrestore(&devices_lock, flags);
1077
1078 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1079 l2cap_chan_close(peer->chan, ENOENT);
1080 kfree(peer);
1081 }
1082}
1083
1084static int lowpan_psm_set(void *data, u64 val)
1085{
1086 u16 psm;
1087
1088 psm = val;
1089 if (psm == 0 || psm_6lowpan != psm)
1090 /* Disconnect existing connections if 6lowpan is
1091 * disabled (psm = 0), or if psm changes.
1092 */
1093 disconnect_all_peers();
1094
1095 psm_6lowpan = psm;
1096
1097 if (listen_chan) {
1098 l2cap_chan_close(listen_chan, 0);
1099 l2cap_chan_put(listen_chan);
1100 }
1101
1102 listen_chan = bt_6lowpan_listen();
1103
1104 return 0;
1105}
1106
1107static int lowpan_psm_get(void *data, u64 *val)
1108{
1109 *val = psm_6lowpan;
1110 return 0;
1111}
1112
1113DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops, lowpan_psm_get,
1114 lowpan_psm_set, "%llu\n");
1115
1116static ssize_t lowpan_control_write(struct file *fp,
1117 const char __user *user_buffer,
1118 size_t count,
1119 loff_t *position)
1120{
1121 char buf[32];
1122 size_t buf_size = min(count, sizeof(buf) - 1);
1123 int ret;
1124 bdaddr_t addr;
1125 u8 addr_type;
1126 struct l2cap_conn *conn = NULL;
1127
1128 if (copy_from_user(buf, user_buffer, buf_size))
1129 return -EFAULT;
1130
1131 buf[buf_size] = '\0';
1132
1133 if (memcmp(buf, "connect ", 8) == 0) {
1134 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1135 if (ret == -EINVAL)
1136 return ret;
1137
1138 if (listen_chan) {
1139 l2cap_chan_close(listen_chan, 0);
1140 l2cap_chan_put(listen_chan);
1141 listen_chan = NULL;
1142 }
1143
1144 if (conn) {
1145 struct lowpan_peer *peer;
1146
1147 if (!is_bt_6lowpan(conn->hcon))
1148 return -EINVAL;
1149
1150 peer = lookup_peer(conn);
1151 if (peer) {
1152 BT_DBG("6LoWPAN connection already exists");
1153 return -EALREADY;
1154 }
1155
1156 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1157 &conn->hcon->dst, conn->hcon->dst_type,
1158 addr_type);
1159 }
1160
1161 ret = bt_6lowpan_connect(&addr, addr_type);
1162 if (ret < 0)
1163 return ret;
1164
1165 return count;
1166 }
1167
1168 if (memcmp(buf, "disconnect ", 11) == 0) {
1169 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1170 if (ret < 0)
1171 return ret;
1172
1173 ret = bt_6lowpan_disconnect(conn, addr_type);
1174 if (ret < 0)
1175 return ret;
1176
1177 return count;
1178 }
1179
1180 return count;
1181}
1182
1183static int lowpan_control_show(struct seq_file *f, void *ptr)
1184{
1185 struct lowpan_dev *entry, *tmp_dev;
1186 struct lowpan_peer *peer, *tmp_peer;
1187 unsigned long flags;
1188
1189 read_lock_irqsave(&devices_lock, flags);
1190
1191 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1192 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list)
1193 seq_printf(f, "%pMR (type %u)\n",
1194 &peer->chan->dst, peer->chan->dst_type);
1195 }
1196
1197 read_unlock_irqrestore(&devices_lock, flags);
1198
1199 return 0;
1200}
1201
1202static int lowpan_control_open(struct inode *inode, struct file *file)
1203{
1204 return single_open(file, lowpan_control_show, inode->i_private);
1205}
1206
1207static const struct file_operations lowpan_control_fops = {
1208 .open = lowpan_control_open,
1209 .read = seq_read,
1210 .write = lowpan_control_write,
1211 .llseek = seq_lseek,
1212 .release = single_release,
1213};
1214
Jukka Rissanen7f118252014-06-18 16:37:11 +03001215static void disconnect_devices(void)
1216{
1217 struct lowpan_dev *entry, *tmp, *new_dev;
1218 struct list_head devices;
1219 unsigned long flags;
1220
1221 INIT_LIST_HEAD(&devices);
1222
1223 /* We make a separate list of devices because the unregister_netdev()
1224 * will call device_event() which will also want to modify the same
1225 * devices list.
1226 */
1227
1228 read_lock_irqsave(&devices_lock, flags);
1229
1230 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
1231 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1232 if (!new_dev)
1233 break;
1234
1235 new_dev->netdev = entry->netdev;
1236 INIT_LIST_HEAD(&new_dev->list);
1237
1238 list_add(&new_dev->list, &devices);
1239 }
1240
1241 read_unlock_irqrestore(&devices_lock, flags);
1242
1243 list_for_each_entry_safe(entry, tmp, &devices, list) {
1244 ifdown(entry->netdev);
1245 BT_DBG("Unregistering netdev %s %p",
1246 entry->netdev->name, entry->netdev);
1247 unregister_netdev(entry->netdev);
1248 kfree(entry);
1249 }
1250}
1251
Jukka Rissanen18722c22013-12-11 17:05:37 +02001252static int device_event(struct notifier_block *unused,
1253 unsigned long event, void *ptr)
1254{
1255 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1256 struct lowpan_dev *entry, *tmp;
1257 unsigned long flags;
1258
1259 if (netdev->type != ARPHRD_6LOWPAN)
1260 return NOTIFY_DONE;
1261
1262 switch (event) {
1263 case NETDEV_UNREGISTER:
1264 write_lock_irqsave(&devices_lock, flags);
1265 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices,
1266 list) {
1267 if (entry->netdev == netdev) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001268 BT_DBG("Unregistered netdev %s %p",
1269 netdev->name, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001270 list_del(&entry->list);
1271 kfree(entry);
1272 break;
1273 }
1274 }
1275 write_unlock_irqrestore(&devices_lock, flags);
1276 break;
1277 }
1278
1279 return NOTIFY_DONE;
1280}
1281
1282static struct notifier_block bt_6lowpan_dev_notifier = {
1283 .notifier_call = device_event,
1284};
1285
Jukka Rissanen5547e482014-06-18 16:37:09 +03001286static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001287{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001288 lowpan_psm_debugfs = debugfs_create_file("6lowpan_psm", 0644,
1289 bt_debugfs, NULL,
1290 &lowpan_psm_fops);
1291 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1292 bt_debugfs, NULL,
1293 &lowpan_control_fops);
1294
Jukka Rissanen18722c22013-12-11 17:05:37 +02001295 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1296}
1297
Jukka Rissanen5547e482014-06-18 16:37:09 +03001298static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001299{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001300 debugfs_remove(lowpan_psm_debugfs);
1301 debugfs_remove(lowpan_control_debugfs);
1302
1303 if (listen_chan) {
1304 l2cap_chan_close(listen_chan, 0);
1305 l2cap_chan_put(listen_chan);
1306 }
1307
Jukka Rissanen7f118252014-06-18 16:37:11 +03001308 disconnect_devices();
1309
Jukka Rissanen18722c22013-12-11 17:05:37 +02001310 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1311}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001312
1313module_init(bt_6lowpan_init);
1314module_exit(bt_6lowpan_exit);
1315
1316MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1317MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1318MODULE_VERSION(VERSION);
1319MODULE_LICENSE("GPL");