xref: /linux/net/bluetooth/6lowpan.c (revision 0d8f6981f55e28d8fac703bbd0e29bd9496c8b5b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3    Copyright (c) 2013-2014 Intel Corp.
4 
5 */
6 
7 #include <linux/if_arp.h>
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <linux/module.h>
11 #include <linux/debugfs.h>
12 
13 #include <net/ipv6.h>
14 #include <net/ip6_route.h>
15 #include <net/addrconf.h>
16 #include <net/netdev_lock.h>
17 #include <net/pkt_sched.h>
18 
19 #include <net/bluetooth/bluetooth.h>
20 #include <net/bluetooth/hci_core.h>
21 #include <net/bluetooth/l2cap.h>
22 
23 #include <net/6lowpan.h> /* for the compression support */
24 
25 #define VERSION "0.1"
26 
27 static struct dentry *lowpan_enable_debugfs;
28 static struct dentry *lowpan_control_debugfs;
29 
30 #define IFACE_NAME_TEMPLATE "bt%d"
31 
32 struct skb_cb {
33 	struct in6_addr addr;
34 	struct in6_addr gw;
35 	struct l2cap_chan *chan;
36 };
37 #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
38 
39 /* The devices list contains those devices that we are acting
40  * as a proxy. The BT 6LoWPAN device is a virtual device that
41  * connects to the Bluetooth LE device. The real connection to
42  * BT device is done via l2cap layer. There exists one
43  * virtual device / one BT 6LoWPAN network (=hciX device).
44  * The list contains struct lowpan_dev elements.
45  */
46 static LIST_HEAD(bt_6lowpan_devices);
47 static DEFINE_SPINLOCK(devices_lock);
48 
49 static bool enable_6lowpan;
50 
51 /* We are listening incoming connections via this channel
52  */
53 static struct l2cap_chan *listen_chan;
54 static DEFINE_MUTEX(set_lock);
55 
56 enum {
57 	LOWPAN_PEER_CLOSING,
58 	LOWPAN_PEER_MAXBITS
59 };
60 
61 struct lowpan_peer {
62 	struct list_head list;
63 	struct rcu_head rcu;
64 	struct l2cap_chan *chan;
65 
66 	/* peer addresses in various formats */
67 	unsigned char lladdr[ETH_ALEN];
68 	struct in6_addr peer_addr;
69 
70 	DECLARE_BITMAP(flags, LOWPAN_PEER_MAXBITS);
71 };
72 
73 struct lowpan_btle_dev {
74 	struct list_head list;
75 
76 	struct hci_dev *hdev;
77 	struct net_device *netdev;
78 	struct list_head peers;
79 	atomic_t peer_count; /* number of items in peers list */
80 
81 	struct work_struct delete_netdev;
82 	struct delayed_work notify_peers;
83 };
84 
85 static inline struct lowpan_btle_dev *
86 lowpan_btle_dev(const struct net_device *netdev)
87 {
88 	return (struct lowpan_btle_dev *)lowpan_dev(netdev)->priv;
89 }
90 
91 static inline void peer_add(struct lowpan_btle_dev *dev,
92 			    struct lowpan_peer *peer)
93 {
94 	list_add_rcu(&peer->list, &dev->peers);
95 	atomic_inc(&dev->peer_count);
96 }
97 
98 static inline bool peer_del(struct lowpan_btle_dev *dev,
99 			    struct lowpan_peer *peer)
100 {
101 	list_del_rcu(&peer->list);
102 	kfree_rcu(peer, rcu);
103 
104 	module_put(THIS_MODULE);
105 
106 	if (atomic_dec_and_test(&dev->peer_count)) {
107 		BT_DBG("last peer");
108 		return true;
109 	}
110 
111 	return false;
112 }
113 
114 static inline struct lowpan_peer *
115 __peer_lookup_chan(struct lowpan_btle_dev *dev, struct l2cap_chan *chan)
116 {
117 	struct lowpan_peer *peer;
118 
119 	list_for_each_entry_rcu(peer, &dev->peers, list) {
120 		if (peer->chan == chan)
121 			return peer;
122 	}
123 
124 	return NULL;
125 }
126 
127 static inline struct lowpan_peer *
128 __peer_lookup_conn(struct lowpan_btle_dev *dev, struct l2cap_conn *conn)
129 {
130 	struct lowpan_peer *peer;
131 
132 	list_for_each_entry_rcu(peer, &dev->peers, list) {
133 		if (peer->chan->conn == conn)
134 			return peer;
135 	}
136 
137 	return NULL;
138 }
139 
140 static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_btle_dev *dev,
141 						  struct in6_addr *daddr,
142 						  struct sk_buff *skb)
143 {
144 	struct rt6_info *rt = dst_rt6_info(skb_dst(skb));
145 	int count = atomic_read(&dev->peer_count);
146 	const struct in6_addr *nexthop;
147 	struct lowpan_peer *peer;
148 	struct neighbour *neigh;
149 
150 	BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt);
151 
152 	if (!rt) {
153 		if (ipv6_addr_any(&lowpan_cb(skb)->gw)) {
154 			/* There is neither route nor gateway,
155 			 * probably the destination is a direct peer.
156 			 */
157 			nexthop = daddr;
158 		} else {
159 			/* There is a known gateway
160 			 */
161 			nexthop = &lowpan_cb(skb)->gw;
162 		}
163 	} else {
164 		nexthop = rt6_nexthop(rt, daddr);
165 
166 		/* We need to remember the address because it is needed
167 		 * by bt_xmit() when sending the packet. In bt_xmit(), the
168 		 * destination routing info is not set.
169 		 */
170 		memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr));
171 	}
172 
173 	BT_DBG("gw %pI6c", nexthop);
174 
175 	rcu_read_lock();
176 
177 	list_for_each_entry_rcu(peer, &dev->peers, list) {
178 		BT_DBG("dst addr %pMR dst type %u ip %pI6c",
179 		       &peer->chan->dst, peer->chan->dst_type,
180 		       &peer->peer_addr);
181 
182 		if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) {
183 			rcu_read_unlock();
184 			return peer;
185 		}
186 	}
187 
188 	/* use the neighbour cache for matching addresses assigned by SLAAC */
189 	neigh = __ipv6_neigh_lookup(dev->netdev, nexthop);
190 	if (neigh) {
191 		list_for_each_entry_rcu(peer, &dev->peers, list) {
192 			if (!memcmp(neigh->ha, peer->lladdr, ETH_ALEN)) {
193 				neigh_release(neigh);
194 				rcu_read_unlock();
195 				return peer;
196 			}
197 		}
198 		neigh_release(neigh);
199 	}
200 
201 	rcu_read_unlock();
202 
203 	return NULL;
204 }
205 
206 static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
207 {
208 	struct lowpan_btle_dev *entry;
209 	struct lowpan_peer *peer = NULL;
210 
211 	rcu_read_lock();
212 
213 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
214 		peer = __peer_lookup_conn(entry, conn);
215 		if (peer)
216 			break;
217 	}
218 
219 	rcu_read_unlock();
220 
221 	return peer;
222 }
223 
224 static struct lowpan_btle_dev *lookup_dev(struct l2cap_conn *conn)
225 {
226 	struct lowpan_btle_dev *entry;
227 	struct lowpan_btle_dev *dev = NULL;
228 
229 	rcu_read_lock();
230 
231 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
232 		if (conn->hcon->hdev == entry->hdev) {
233 			dev = entry;
234 			break;
235 		}
236 	}
237 
238 	rcu_read_unlock();
239 
240 	return dev;
241 }
242 
243 static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
244 {
245 	struct sk_buff *skb_cp;
246 
247 	skb_cp = skb_copy(skb, GFP_ATOMIC);
248 	if (!skb_cp)
249 		return NET_RX_DROP;
250 
251 	return netif_rx(skb_cp);
252 }
253 
254 static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev,
255 			   struct lowpan_peer *peer)
256 {
257 	const u8 *saddr;
258 
259 	saddr = peer->lladdr;
260 
261 	return lowpan_header_decompress(skb, netdev, netdev->dev_addr, saddr);
262 }
263 
264 static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
265 		    struct lowpan_peer *peer)
266 {
267 	struct sk_buff *local_skb;
268 	int ret;
269 
270 	if (!netif_running(dev))
271 		goto drop;
272 
273 	if (dev->type != ARPHRD_6LOWPAN || !skb->len)
274 		goto drop;
275 
276 	skb_reset_network_header(skb);
277 
278 	skb = skb_share_check(skb, GFP_ATOMIC);
279 	if (!skb)
280 		goto drop;
281 
282 	/* check that it's our buffer */
283 	if (lowpan_is_ipv6(*skb_network_header(skb))) {
284 		/* Pull off the 1-byte of 6lowpan header. */
285 		skb_pull(skb, 1);
286 
287 		/* Copy the packet so that the IPv6 header is
288 		 * properly aligned.
289 		 */
290 		local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
291 					    skb_tailroom(skb), GFP_ATOMIC);
292 		if (!local_skb)
293 			goto drop;
294 
295 		local_skb->protocol = htons(ETH_P_IPV6);
296 		local_skb->pkt_type = PACKET_HOST;
297 		local_skb->dev = dev;
298 
299 		skb_reset_mac_header(local_skb);
300 		skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
301 
302 		if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
303 			kfree_skb(local_skb);
304 			goto drop;
305 		}
306 
307 		dev->stats.rx_bytes += skb->len;
308 		dev->stats.rx_packets++;
309 
310 		consume_skb(local_skb);
311 		consume_skb(skb);
312 	} else if (lowpan_is_iphc(*skb_network_header(skb))) {
313 		local_skb = skb_clone(skb, GFP_ATOMIC);
314 		if (!local_skb)
315 			goto drop;
316 
317 		local_skb->dev = dev;
318 
319 		ret = iphc_decompress(local_skb, dev, peer);
320 		if (ret < 0) {
321 			BT_DBG("iphc_decompress failed: %d", ret);
322 			kfree_skb(local_skb);
323 			goto drop;
324 		}
325 
326 		local_skb->protocol = htons(ETH_P_IPV6);
327 		local_skb->pkt_type = PACKET_HOST;
328 
329 		if (give_skb_to_upper(local_skb, dev)
330 				!= NET_RX_SUCCESS) {
331 			kfree_skb(local_skb);
332 			goto drop;
333 		}
334 
335 		dev->stats.rx_bytes += skb->len;
336 		dev->stats.rx_packets++;
337 
338 		consume_skb(local_skb);
339 		consume_skb(skb);
340 	} else {
341 		BT_DBG("unknown packet type");
342 		goto drop;
343 	}
344 
345 	return NET_RX_SUCCESS;
346 
347 drop:
348 	kfree_skb(skb);
349 	dev->stats.rx_dropped++;
350 	return NET_RX_DROP;
351 }
352 
353 /* Packet from BT LE device */
354 static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
355 {
356 	struct lowpan_btle_dev *dev;
357 	struct lowpan_peer *peer;
358 	int err;
359 
360 	/* The skb is always consumed, so never return an error that would
361 	 * make the caller free it again.
362 	 */
363 	peer = lookup_peer(chan->conn);
364 	if (!peer)
365 		goto drop;
366 
367 	dev = lookup_dev(chan->conn);
368 	if (!dev || !dev->netdev)
369 		goto drop;
370 
371 	err = recv_pkt(skb, dev->netdev, peer);
372 	if (err)
373 		BT_DBG("recv pkt %d", err);
374 
375 	return 0;
376 
377 drop:
378 	kfree_skb(skb);
379 	return 0;
380 }
381 
382 static int setup_header(struct sk_buff *skb, struct net_device *netdev,
383 			bdaddr_t *peer_addr, u8 *peer_addr_type)
384 {
385 	struct in6_addr ipv6_daddr;
386 	struct ipv6hdr *hdr;
387 	struct lowpan_btle_dev *dev;
388 	struct lowpan_peer *peer;
389 	u8 *daddr;
390 	int err, status = 0;
391 
392 	hdr = ipv6_hdr(skb);
393 
394 	dev = lowpan_btle_dev(netdev);
395 
396 	memcpy(&ipv6_daddr, &hdr->daddr, sizeof(ipv6_daddr));
397 
398 	if (ipv6_addr_is_multicast(&ipv6_daddr)) {
399 		lowpan_cb(skb)->chan = NULL;
400 		daddr = NULL;
401 	} else {
402 		BT_DBG("dest IP %pI6c", &ipv6_daddr);
403 
404 		/* The packet might be sent to 6lowpan interface
405 		 * because of routing (either via default route
406 		 * or user set route) so get peer according to
407 		 * the destination address.
408 		 */
409 		peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
410 		if (!peer) {
411 			BT_DBG("no such peer");
412 			return -ENOENT;
413 		}
414 
415 		daddr = peer->lladdr;
416 		*peer_addr = peer->chan->dst;
417 		*peer_addr_type = peer->chan->dst_type;
418 		lowpan_cb(skb)->chan = peer->chan;
419 
420 		status = 1;
421 	}
422 
423 	lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr);
424 
425 	err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
426 	if (err < 0)
427 		return err;
428 
429 	return status;
430 }
431 
432 static int header_create(struct sk_buff *skb, struct net_device *netdev,
433 			 unsigned short type, const void *_daddr,
434 			 const void *_saddr, unsigned int len)
435 {
436 	if (type != ETH_P_IPV6)
437 		return -EINVAL;
438 
439 	return 0;
440 }
441 
442 /* Packet to BT LE device */
443 static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
444 		    struct net_device *netdev)
445 {
446 	struct msghdr msg;
447 	struct kvec iv;
448 	int err;
449 
450 	/* Remember the skb so that we can send EAGAIN to the caller if
451 	 * we run out of credits.
452 	 */
453 	chan->data = skb;
454 
455 	iv.iov_base = skb->data;
456 	iv.iov_len = skb->len;
457 
458 	memset(&msg, 0, sizeof(msg));
459 	iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iv, 1, skb->len);
460 
461 	err = l2cap_chan_send(chan, &msg, skb->len, NULL);
462 	if (err > 0) {
463 		netdev->stats.tx_bytes += err;
464 		netdev->stats.tx_packets++;
465 		return 0;
466 	}
467 
468 	if (err < 0)
469 		netdev->stats.tx_errors++;
470 
471 	return err;
472 }
473 
474 static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
475 {
476 	struct sk_buff *local_skb;
477 	struct lowpan_btle_dev *entry;
478 	int err = 0;
479 
480 	rcu_read_lock();
481 
482 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
483 		struct lowpan_peer *pentry;
484 		struct lowpan_btle_dev *dev;
485 
486 		if (entry->netdev != netdev)
487 			continue;
488 
489 		dev = lowpan_btle_dev(entry->netdev);
490 
491 		list_for_each_entry_rcu(pentry, &dev->peers, list) {
492 			int ret;
493 
494 			local_skb = skb_clone(skb, GFP_ATOMIC);
495 			if (!local_skb)
496 				continue;
497 
498 			BT_DBG("xmit %s to %pMR type %u IP %pI6c chan %p",
499 			       netdev->name,
500 			       &pentry->chan->dst, pentry->chan->dst_type,
501 			       &pentry->peer_addr, pentry->chan);
502 			ret = send_pkt(pentry->chan, local_skb, netdev);
503 			if (ret < 0)
504 				err = ret;
505 
506 			kfree_skb(local_skb);
507 		}
508 	}
509 
510 	rcu_read_unlock();
511 
512 	return err;
513 }
514 
515 static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
516 {
517 	int err = 0;
518 	bdaddr_t addr;
519 	u8 addr_type;
520 
521 	/* We must take a copy of the skb before we modify/replace the ipv6
522 	 * header as the header could be used elsewhere
523 	 */
524 	skb = skb_unclone_full(skb, GFP_ATOMIC);
525 	if (!skb)
526 		return NET_XMIT_DROP;
527 
528 	/* Return values from setup_header()
529 	 *  <0 - error, packet is dropped
530 	 *   0 - this is a multicast packet
531 	 *   1 - this is unicast packet
532 	 */
533 	err = setup_header(skb, netdev, &addr, &addr_type);
534 	if (err < 0) {
535 		kfree_skb(skb);
536 		return NET_XMIT_DROP;
537 	}
538 
539 	if (err) {
540 		if (lowpan_cb(skb)->chan) {
541 			BT_DBG("xmit %s to %pMR type %u IP %pI6c chan %p",
542 			       netdev->name, &addr, addr_type,
543 			       &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
544 			err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
545 		} else {
546 			err = -ENOENT;
547 		}
548 	} else {
549 		/* We need to send the packet to every device behind this
550 		 * interface.
551 		 */
552 		err = send_mcast_pkt(skb, netdev);
553 	}
554 
555 	dev_kfree_skb(skb);
556 
557 	if (err)
558 		BT_DBG("ERROR: xmit failed (%d)", err);
559 
560 	return err < 0 ? NET_XMIT_DROP : err;
561 }
562 
563 static int bt_dev_init(struct net_device *dev)
564 {
565 	netdev_lockdep_set_classes(dev);
566 
567 	return 0;
568 }
569 
570 static const struct net_device_ops netdev_ops = {
571 	.ndo_init		= bt_dev_init,
572 	.ndo_start_xmit		= bt_xmit,
573 };
574 
575 static const struct header_ops header_ops = {
576 	.create	= header_create,
577 };
578 
579 static void netdev_setup(struct net_device *dev)
580 {
581 	dev->hard_header_len	= 0;
582 	dev->needed_tailroom	= 0;
583 	dev->flags		= IFF_RUNNING | IFF_MULTICAST;
584 	dev->watchdog_timeo	= 0;
585 	dev->tx_queue_len	= DEFAULT_TX_QUEUE_LEN;
586 
587 	dev->netdev_ops		= &netdev_ops;
588 	dev->header_ops		= &header_ops;
589 	dev->needs_free_netdev	= true;
590 }
591 
592 static const struct device_type bt_type = {
593 	.name	= "bluetooth",
594 };
595 
596 static void ifup(struct net_device *netdev)
597 {
598 	int err;
599 
600 	rtnl_lock();
601 	err = dev_open(netdev, NULL);
602 	if (err < 0)
603 		BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
604 	rtnl_unlock();
605 }
606 
607 static void ifdown(struct net_device *netdev)
608 {
609 	rtnl_lock();
610 	dev_close(netdev);
611 	rtnl_unlock();
612 }
613 
614 static void do_notify_peers(struct work_struct *work)
615 {
616 	struct lowpan_btle_dev *dev = container_of(work, struct lowpan_btle_dev,
617 						   notify_peers.work);
618 
619 	netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
620 }
621 
622 static bool is_bt_6lowpan(struct hci_conn *hcon)
623 {
624 	if (hcon->type != LE_LINK)
625 		return false;
626 
627 	if (!enable_6lowpan)
628 		return false;
629 
630 	return true;
631 }
632 
633 static struct l2cap_chan *chan_create(void)
634 {
635 	struct l2cap_chan *chan;
636 
637 	chan = l2cap_chan_create();
638 	if (!chan)
639 		return NULL;
640 
641 	l2cap_chan_set_defaults(chan, NULL);
642 
643 	chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
644 	chan->mode = L2CAP_MODE_LE_FLOWCTL;
645 	chan->imtu = 1280;
646 
647 	return chan;
648 }
649 
650 static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
651 					struct lowpan_btle_dev *dev,
652 					bool new_netdev)
653 {
654 	struct lowpan_peer *peer;
655 
656 	peer = kzalloc_obj(*peer, GFP_ATOMIC);
657 	if (!peer)
658 		return NULL;
659 
660 	peer->chan = chan;
661 
662 	baswap((void *)peer->lladdr, &chan->dst);
663 
664 	lowpan_iphc_uncompress_eui48_lladdr(&peer->peer_addr, peer->lladdr);
665 
666 	spin_lock(&devices_lock);
667 	INIT_LIST_HEAD(&peer->list);
668 	peer_add(dev, peer);
669 	spin_unlock(&devices_lock);
670 
671 	/* Notifying peers about us needs to be done without locks held */
672 	if (new_netdev)
673 		INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
674 	schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
675 
676 	return peer->chan;
677 }
678 
679 static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
680 {
681 	struct net_device *netdev;
682 	bdaddr_t addr;
683 	int err;
684 
685 	netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_btle_dev)),
686 			      IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN,
687 			      netdev_setup);
688 	if (!netdev)
689 		return -ENOMEM;
690 
691 	netdev->addr_assign_type = NET_ADDR_PERM;
692 	baswap(&addr, &chan->src);
693 	__dev_addr_set(netdev, &addr, sizeof(addr));
694 
695 	netdev->netdev_ops = &netdev_ops;
696 	SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev);
697 	SET_NETDEV_DEVTYPE(netdev, &bt_type);
698 
699 	*dev = lowpan_btle_dev(netdev);
700 	(*dev)->netdev = netdev;
701 	(*dev)->hdev = chan->conn->hcon->hdev;
702 	INIT_LIST_HEAD(&(*dev)->peers);
703 
704 	spin_lock(&devices_lock);
705 	INIT_LIST_HEAD(&(*dev)->list);
706 	list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
707 	spin_unlock(&devices_lock);
708 
709 	err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE);
710 	if (err < 0) {
711 		BT_INFO("register_netdev failed %d", err);
712 		spin_lock(&devices_lock);
713 		list_del_rcu(&(*dev)->list);
714 		spin_unlock(&devices_lock);
715 		synchronize_rcu();
716 		free_netdev(netdev);
717 		goto out;
718 	}
719 
720 	BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
721 	       netdev->ifindex, &chan->dst, chan->dst_type,
722 	       &chan->src, chan->src_type);
723 	set_bit(__LINK_STATE_PRESENT, &netdev->state);
724 
725 	return 0;
726 
727 out:
728 	return err;
729 }
730 
731 static inline void chan_ready_cb(struct l2cap_chan *chan)
732 	__must_hold(&chan->lock)
733 	__must_hold(&chan->conn->lock)
734 {
735 	struct lowpan_btle_dev *dev;
736 	bool new_netdev = false;
737 
738 	dev = lookup_dev(chan->conn);
739 
740 	BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
741 
742 	if (!dev) {
743 		if (setup_netdev(chan, &dev) < 0) {
744 			l2cap_chan_del(chan, -ENOENT);
745 			return;
746 		}
747 		new_netdev = true;
748 	}
749 
750 	if (!try_module_get(THIS_MODULE))
751 		return;
752 
753 	add_peer_chan(chan, dev, new_netdev);
754 	ifup(dev->netdev);
755 }
756 
757 static void unregister_dev(struct lowpan_btle_dev *dev)
758 {
759 	struct hci_dev *hdev = READ_ONCE(dev->hdev);
760 
761 	/* If netdev holds last reference to hci_dev (its parent device), this
762 	 * leads to theoretical cyclic locking on lowpan_unregister_netdev:
763 	 *
764 	 * rtnl_lock -> put_device(parent) -> hci_release_dev ->
765 	 * destroy_workqueue -> hci_rx_work -> l2cap_recv_acldata ->
766 	 * chan_ready_cb -> ifup -> rtnl_lock
767 	 *
768 	 * However, hci_rx_work is disabled in hci_unregister_dev, so this
769 	 * should not occur. Make lockdep happy by postponing hdev release after
770 	 * netdev put.
771 	 */
772 	hci_dev_hold(hdev);
773 	lowpan_unregister_netdev(dev->netdev);
774 	hci_dev_put(hdev);
775 }
776 
777 static void delete_netdev(struct work_struct *work)
778 {
779 	struct lowpan_btle_dev *entry = container_of(work,
780 						     struct lowpan_btle_dev,
781 						     delete_netdev);
782 
783 	unregister_dev(entry);
784 
785 	/* The entry pointer is deleted by the netdev destructor. */
786 }
787 
788 static void chan_close_cb(struct l2cap_chan *chan)
789 {
790 	struct lowpan_btle_dev *entry;
791 	struct lowpan_btle_dev *dev = NULL;
792 	struct lowpan_peer *peer;
793 	int err = -ENOENT;
794 	bool last = false;
795 
796 	BT_DBG("chan %p conn %p", chan, chan->conn);
797 
798 	spin_lock(&devices_lock);
799 
800 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
801 		dev = lowpan_btle_dev(entry->netdev);
802 		peer = __peer_lookup_chan(dev, chan);
803 		if (peer) {
804 			last = peer_del(dev, peer);
805 			err = 0;
806 
807 			BT_DBG("dev %p removing %speer %p", dev,
808 			       last ? "last " : "1 ", peer);
809 			BT_DBG("chan %p orig refcnt %u", chan,
810 			       kref_read(&chan->kref));
811 
812 			l2cap_chan_put(chan);
813 			break;
814 		}
815 	}
816 
817 	if (!err && last && dev && !atomic_read(&dev->peer_count)) {
818 		spin_unlock(&devices_lock);
819 
820 		cancel_delayed_work_sync(&dev->notify_peers);
821 
822 		ifdown(dev->netdev);
823 
824 		INIT_WORK(&entry->delete_netdev, delete_netdev);
825 		schedule_work(&entry->delete_netdev);
826 	} else {
827 		spin_unlock(&devices_lock);
828 	}
829 }
830 
831 static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
832 {
833 	BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
834 	       state_to_string(state), err);
835 }
836 
837 static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
838 					 unsigned long hdr_len,
839 					 unsigned long len, int nb)
840 {
841 	struct sk_buff *skb;
842 
843 	/* Note that we must allocate using GFP_ATOMIC here as
844 	 * this function is called originally from netdev hard xmit
845 	 * function in atomic context.
846 	 */
847 	skb = bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
848 	if (!skb)
849 		return ERR_PTR(-ENOMEM);
850 	return skb;
851 }
852 
853 static void chan_suspend_cb(struct l2cap_chan *chan)
854 {
855 	struct lowpan_btle_dev *dev;
856 
857 	BT_DBG("chan %p suspend", chan);
858 
859 	dev = lookup_dev(chan->conn);
860 	if (!dev || !dev->netdev)
861 		return;
862 
863 	netif_stop_queue(dev->netdev);
864 }
865 
866 static void chan_resume_cb(struct l2cap_chan *chan)
867 {
868 	struct lowpan_btle_dev *dev;
869 
870 	BT_DBG("chan %p resume", chan);
871 
872 	dev = lookup_dev(chan->conn);
873 	if (!dev || !dev->netdev)
874 		return;
875 
876 	netif_wake_queue(dev->netdev);
877 }
878 
879 static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
880 {
881 	return L2CAP_CONN_TIMEOUT;
882 }
883 
884 static const struct l2cap_ops bt_6lowpan_chan_ops = {
885 	.name			= "L2CAP 6LoWPAN channel",
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 
895 	.teardown		= l2cap_chan_no_teardown,
896 	.defer			= l2cap_chan_no_defer,
897 	.set_shutdown		= l2cap_chan_no_set_shutdown,
898 };
899 
900 static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
901 {
902 	struct l2cap_chan *chan;
903 	int err;
904 
905 	chan = chan_create();
906 	if (!chan)
907 		return -EINVAL;
908 
909 	chan->ops = &bt_6lowpan_chan_ops;
910 
911 	err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0,
912 				 addr, dst_type, L2CAP_CONN_TIMEOUT);
913 
914 	BT_DBG("chan %p err %d", chan, err);
915 	if (err < 0)
916 		l2cap_chan_put(chan);
917 
918 	return err;
919 }
920 
921 static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
922 {
923 	struct lowpan_peer *peer;
924 	struct l2cap_chan *chan;
925 
926 	BT_DBG("conn %p dst type %u", conn, dst_type);
927 
928 	spin_lock(&devices_lock);
929 
930 	peer = lookup_peer(conn);
931 	if (!peer) {
932 		spin_unlock(&devices_lock);
933 		return -ENOENT;
934 	}
935 
936 	chan = peer->chan;
937 	l2cap_chan_hold(chan);
938 
939 	spin_unlock(&devices_lock);
940 
941 	BT_DBG("peer %p chan %p", peer, chan);
942 
943 	l2cap_chan_close_unlocked(chan, ENOENT);
944 	l2cap_chan_put(chan);
945 
946 	return 0;
947 }
948 
949 static struct l2cap_chan *bt_6lowpan_listen(void)
950 {
951 	bdaddr_t *addr = BDADDR_ANY;
952 	struct l2cap_chan *chan;
953 	int err;
954 
955 	if (!enable_6lowpan)
956 		return NULL;
957 
958 	chan = chan_create();
959 	if (!chan)
960 		return NULL;
961 
962 	chan->ops = &bt_6lowpan_chan_ops;
963 	chan->state = BT_LISTEN;
964 	chan->src_type = BDADDR_LE_PUBLIC;
965 
966 	atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
967 
968 	BT_DBG("chan %p src type %u", chan, chan->src_type);
969 
970 	err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP));
971 	if (err) {
972 		l2cap_chan_put(chan);
973 		BT_ERR("psm cannot be added err %d", err);
974 		return NULL;
975 	}
976 
977 	return chan;
978 }
979 
980 static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
981 			  struct l2cap_conn **conn, bool disconnect)
982 {
983 	struct hci_conn *hcon;
984 	struct hci_dev *hdev;
985 	int le_addr_type;
986 	int n;
987 
988 	n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
989 		   &addr->b[5], &addr->b[4], &addr->b[3],
990 		   &addr->b[2], &addr->b[1], &addr->b[0],
991 		   addr_type);
992 
993 	if (n < 7)
994 		return -EINVAL;
995 
996 	if (disconnect) {
997 		/* The "disconnect" debugfs command has used different address
998 		 * type constants than "connect" since 2015. Let's retain that
999 		 * for now even though it's obviously buggy...
1000 		 */
1001 		*addr_type += 1;
1002 	}
1003 
1004 	switch (*addr_type) {
1005 	case BDADDR_LE_PUBLIC:
1006 		le_addr_type = ADDR_LE_DEV_PUBLIC;
1007 		break;
1008 	case BDADDR_LE_RANDOM:
1009 		le_addr_type = ADDR_LE_DEV_RANDOM;
1010 		break;
1011 	default:
1012 		return -EINVAL;
1013 	}
1014 
1015 	/* The LE_PUBLIC address type is ignored because of BDADDR_ANY */
1016 	hdev = hci_get_route(addr, BDADDR_ANY, BDADDR_LE_PUBLIC);
1017 	if (!hdev)
1018 		return -ENOENT;
1019 
1020 	hci_dev_lock(hdev);
1021 	hcon = hci_conn_hash_lookup_le(hdev, addr, le_addr_type);
1022 	if (!hcon) {
1023 		hci_dev_unlock(hdev);
1024 		hci_dev_put(hdev);
1025 		return -ENOENT;
1026 	}
1027 
1028 	lockdep_assert_held(&hcon->hdev->lock);
1029 
1030 	*conn = l2cap_conn_hold_unless_zero(hcon->l2cap_data);
1031 
1032 	BT_DBG("conn %p dst %pMR type %u", *conn, &hcon->dst, hcon->dst_type);
1033 
1034 	hci_dev_unlock(hdev);
1035 	hci_dev_put(hdev);
1036 
1037 	return 0;
1038 }
1039 
1040 static void disconnect_all_peers(void)
1041 {
1042 	struct lowpan_btle_dev *entry;
1043 	struct lowpan_peer *peer;
1044 	int nchans;
1045 
1046 	/* l2cap_chan_close_unlocked() cannot be called from RCU, and lock
1047 	 * ordering chan->lock > devices_lock prevents taking write side lock,
1048 	 * so copy then close.
1049 	 */
1050 
1051 	rcu_read_lock();
1052 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list)
1053 		list_for_each_entry_rcu(peer, &entry->peers, list)
1054 			clear_bit(LOWPAN_PEER_CLOSING, peer->flags);
1055 	rcu_read_unlock();
1056 
1057 	do {
1058 		struct l2cap_chan *chans[32];
1059 		int i;
1060 
1061 		nchans = 0;
1062 
1063 		spin_lock(&devices_lock);
1064 
1065 		list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1066 			list_for_each_entry_rcu(peer, &entry->peers, list) {
1067 				if (test_and_set_bit(LOWPAN_PEER_CLOSING,
1068 						     peer->flags))
1069 					continue;
1070 
1071 				l2cap_chan_hold(peer->chan);
1072 				chans[nchans++] = peer->chan;
1073 
1074 				if (nchans >= ARRAY_SIZE(chans))
1075 					goto done;
1076 			}
1077 		}
1078 
1079 done:
1080 		spin_unlock(&devices_lock);
1081 
1082 		for (i = 0; i < nchans; ++i) {
1083 			l2cap_chan_close_unlocked(chans[i], ENOENT);
1084 			l2cap_chan_put(chans[i]);
1085 		}
1086 	} while (nchans);
1087 }
1088 
1089 static void do_enable_set(bool flag)
1090 {
1091 	if (!flag || enable_6lowpan != flag)
1092 		/* Disconnect existing connections if 6lowpan is
1093 		 * disabled
1094 		 */
1095 		disconnect_all_peers();
1096 
1097 	enable_6lowpan = flag;
1098 
1099 	mutex_lock(&set_lock);
1100 	if (listen_chan) {
1101 		l2cap_chan_close_unlocked(listen_chan, 0);
1102 		l2cap_chan_put(listen_chan);
1103 	}
1104 
1105 	listen_chan = bt_6lowpan_listen();
1106 	mutex_unlock(&set_lock);
1107 }
1108 
1109 static int lowpan_enable_set(void *data, u64 val)
1110 {
1111 	do_enable_set(!!val);
1112 
1113 	return 0;
1114 }
1115 
1116 static int lowpan_enable_get(void *data, u64 *val)
1117 {
1118 	*val = enable_6lowpan;
1119 	return 0;
1120 }
1121 
1122 DEFINE_DEBUGFS_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get,
1123 			 lowpan_enable_set, "%llu\n");
1124 
1125 static ssize_t lowpan_control_write(struct file *fp,
1126 				    const char __user *user_buffer,
1127 				    size_t count,
1128 				    loff_t *position)
1129 {
1130 	char buf[32];
1131 	size_t buf_size = min(count, sizeof(buf) - 1);
1132 	int ret;
1133 	bdaddr_t addr;
1134 	u8 addr_type;
1135 	struct l2cap_conn *conn = NULL;
1136 
1137 	if (copy_from_user(buf, user_buffer, buf_size))
1138 		return -EFAULT;
1139 
1140 	buf[buf_size] = '\0';
1141 
1142 	if (memcmp(buf, "connect ", 8) == 0) {
1143 		ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn, false);
1144 		if (ret == -EINVAL)
1145 			return ret;
1146 
1147 		mutex_lock(&set_lock);
1148 		if (listen_chan) {
1149 			l2cap_chan_close_unlocked(listen_chan, 0);
1150 			l2cap_chan_put(listen_chan);
1151 			listen_chan = NULL;
1152 		}
1153 		mutex_unlock(&set_lock);
1154 
1155 		if (conn) {
1156 			struct lowpan_peer *peer;
1157 
1158 			if (!is_bt_6lowpan(conn->hcon)) {
1159 				l2cap_conn_put(conn);
1160 				return -EINVAL;
1161 			}
1162 
1163 			peer = lookup_peer(conn);
1164 			if (peer) {
1165 				BT_DBG("6LoWPAN connection already exists");
1166 				l2cap_conn_put(conn);
1167 				return -EALREADY;
1168 			}
1169 
1170 			BT_DBG("conn %p dst %pMR type %d user %u", conn,
1171 			       &conn->hcon->dst, conn->hcon->dst_type,
1172 			       addr_type);
1173 			l2cap_conn_put(conn);
1174 		}
1175 
1176 		ret = bt_6lowpan_connect(&addr, addr_type);
1177 		if (ret < 0)
1178 			return ret;
1179 
1180 		return count;
1181 	}
1182 
1183 	if (memcmp(buf, "disconnect ", 11) == 0) {
1184 		ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn, true);
1185 		if (ret < 0)
1186 			return ret;
1187 
1188 		ret = bt_6lowpan_disconnect(conn, addr_type);
1189 		if (conn)
1190 			l2cap_conn_put(conn);
1191 		if (ret < 0)
1192 			return ret;
1193 
1194 		return count;
1195 	}
1196 
1197 	return count;
1198 }
1199 
1200 static int lowpan_control_show(struct seq_file *f, void *ptr)
1201 {
1202 	struct lowpan_btle_dev *entry;
1203 	struct lowpan_peer *peer;
1204 
1205 	spin_lock(&devices_lock);
1206 
1207 	list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1208 		list_for_each_entry(peer, &entry->peers, list)
1209 			seq_printf(f, "%pMR (type %u)\n",
1210 				   &peer->chan->dst, peer->chan->dst_type);
1211 	}
1212 
1213 	spin_unlock(&devices_lock);
1214 
1215 	return 0;
1216 }
1217 
1218 static int lowpan_control_open(struct inode *inode, struct file *file)
1219 {
1220 	return single_open(file, lowpan_control_show, inode->i_private);
1221 }
1222 
1223 static const struct file_operations lowpan_control_fops = {
1224 	.owner		= THIS_MODULE,
1225 	.open		= lowpan_control_open,
1226 	.read		= seq_read,
1227 	.write		= lowpan_control_write,
1228 	.llseek		= seq_lseek,
1229 	.release	= single_release,
1230 };
1231 
1232 static void disconnect_devices(void)
1233 {
1234 	struct lowpan_btle_dev *entry, *tmp, *new_dev;
1235 	struct list_head devices;
1236 
1237 	INIT_LIST_HEAD(&devices);
1238 
1239 	/* We make a separate list of devices because the unregister_netdev()
1240 	 * will call device_event() which will also want to modify the same
1241 	 * devices list.
1242 	 */
1243 
1244 	rcu_read_lock();
1245 
1246 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1247 		new_dev = kmalloc_obj(*new_dev, GFP_ATOMIC);
1248 		if (!new_dev)
1249 			break;
1250 
1251 		new_dev->netdev = entry->netdev;
1252 		new_dev->hdev = entry->hdev;
1253 		INIT_LIST_HEAD(&new_dev->list);
1254 
1255 		list_add_rcu(&new_dev->list, &devices);
1256 	}
1257 
1258 	rcu_read_unlock();
1259 
1260 	list_for_each_entry_safe(entry, tmp, &devices, list) {
1261 		ifdown(entry->netdev);
1262 		BT_DBG("Unregistering netdev %s %p",
1263 		       entry->netdev->name, entry->netdev);
1264 		unregister_dev(entry);
1265 		kfree(entry);
1266 	}
1267 }
1268 
1269 static int device_event(struct notifier_block *unused,
1270 			unsigned long event, void *ptr)
1271 {
1272 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1273 	struct lowpan_btle_dev *entry;
1274 
1275 	if (netdev->type != ARPHRD_6LOWPAN)
1276 		return NOTIFY_DONE;
1277 
1278 	switch (event) {
1279 	case NETDEV_UNREGISTER:
1280 		spin_lock(&devices_lock);
1281 		list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1282 			if (entry->netdev == netdev) {
1283 				BT_DBG("Unregistered netdev %s %p",
1284 				       netdev->name, netdev);
1285 				list_del_rcu(&entry->list);
1286 				break;
1287 			}
1288 		}
1289 		spin_unlock(&devices_lock);
1290 		break;
1291 	}
1292 
1293 	return NOTIFY_DONE;
1294 }
1295 
1296 static struct notifier_block bt_6lowpan_dev_notifier = {
1297 	.notifier_call = device_event,
1298 };
1299 
1300 static int __init bt_6lowpan_init(void)
1301 {
1302 	lowpan_enable_debugfs = debugfs_create_file_unsafe("6lowpan_enable",
1303 							   0644, bt_debugfs,
1304 							   NULL,
1305 							   &lowpan_enable_fops);
1306 	lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1307 						     bt_debugfs, NULL,
1308 						     &lowpan_control_fops);
1309 
1310 	return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1311 }
1312 
1313 static void __exit bt_6lowpan_exit(void)
1314 {
1315 	debugfs_remove(lowpan_enable_debugfs);
1316 	debugfs_remove(lowpan_control_debugfs);
1317 
1318 	if (listen_chan) {
1319 		l2cap_chan_close_unlocked(listen_chan, 0);
1320 		l2cap_chan_put(listen_chan);
1321 		listen_chan = NULL;
1322 	}
1323 
1324 	disconnect_devices();
1325 
1326 	unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1327 }
1328 
1329 module_init(bt_6lowpan_init);
1330 module_exit(bt_6lowpan_exit);
1331 
1332 MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1333 MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1334 MODULE_VERSION(VERSION);
1335 MODULE_LICENSE("GPL");
1336