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