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