1 // SPDX-License-Identifier: GPL-2.0-only
3 Copyright (c) 2013-2014 Intel Corp.
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>
14 #include <net/ip6_route.h>
15 #include <net/addrconf.h>
16 #include <net/pkt_sched.h>
18 #include <net/bluetooth/bluetooth.h>
19 #include <net/bluetooth/hci_core.h>
20 #include <net/bluetooth/l2cap.h>
22 #include <net/6lowpan.h> /* for the compression support */
26 static struct dentry *lowpan_enable_debugfs;
27 static struct dentry *lowpan_control_debugfs;
29 #define IFACE_NAME_TEMPLATE "bt%d"
34 struct l2cap_chan *chan;
36 #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
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.
45 static LIST_HEAD(bt_6lowpan_devices);
46 static DEFINE_SPINLOCK(devices_lock);
48 static bool enable_6lowpan;
50 /* We are listening incoming connections via this channel
52 static struct l2cap_chan *listen_chan;
53 static DEFINE_MUTEX(set_lock);
56 struct list_head list;
58 struct l2cap_chan *chan;
60 /* peer addresses in various formats */
61 unsigned char lladdr[ETH_ALEN];
62 struct in6_addr peer_addr;
65 struct lowpan_btle_dev {
66 struct list_head list;
69 struct net_device *netdev;
70 struct list_head peers;
71 atomic_t peer_count; /* number of items in peers list */
73 struct work_struct delete_netdev;
74 struct delayed_work notify_peers;
77 static inline struct lowpan_btle_dev *
78 lowpan_btle_dev(const struct net_device *netdev)
80 return (struct lowpan_btle_dev *)lowpan_dev(netdev)->priv;
83 static inline void peer_add(struct lowpan_btle_dev *dev,
84 struct lowpan_peer *peer)
86 list_add_rcu(&peer->list, &dev->peers);
87 atomic_inc(&dev->peer_count);
90 static inline bool peer_del(struct lowpan_btle_dev *dev,
91 struct lowpan_peer *peer)
93 list_del_rcu(&peer->list);
96 module_put(THIS_MODULE);
98 if (atomic_dec_and_test(&dev->peer_count)) {
106 static inline struct lowpan_peer *
107 __peer_lookup_chan(struct lowpan_btle_dev *dev, struct l2cap_chan *chan)
109 struct lowpan_peer *peer;
111 list_for_each_entry_rcu(peer, &dev->peers, list) {
112 if (peer->chan == chan)
119 static inline struct lowpan_peer *
120 __peer_lookup_conn(struct lowpan_btle_dev *dev, struct l2cap_conn *conn)
122 struct lowpan_peer *peer;
124 list_for_each_entry_rcu(peer, &dev->peers, list) {
125 if (peer->chan->conn == conn)
132 static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_btle_dev *dev,
133 struct in6_addr *daddr,
136 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
137 int count = atomic_read(&dev->peer_count);
138 const struct in6_addr *nexthop;
139 struct lowpan_peer *peer;
140 struct neighbour *neigh;
142 BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt);
145 if (ipv6_addr_any(&lowpan_cb(skb)->gw)) {
146 /* There is neither route nor gateway,
147 * probably the destination is a direct peer.
151 /* There is a known gateway
153 nexthop = &lowpan_cb(skb)->gw;
156 nexthop = rt6_nexthop(rt, daddr);
158 /* We need to remember the address because it is needed
159 * by bt_xmit() when sending the packet. In bt_xmit(), the
160 * destination routing info is not set.
162 memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr));
165 BT_DBG("gw %pI6c", nexthop);
169 list_for_each_entry_rcu(peer, &dev->peers, list) {
170 BT_DBG("dst addr %pMR dst type %u ip %pI6c",
171 &peer->chan->dst, peer->chan->dst_type,
174 if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) {
180 /* use the neighbour cache for matching addresses assigned by SLAAC */
181 neigh = __ipv6_neigh_lookup(dev->netdev, nexthop);
183 list_for_each_entry_rcu(peer, &dev->peers, list) {
184 if (!memcmp(neigh->ha, peer->lladdr, ETH_ALEN)) {
185 neigh_release(neigh);
190 neigh_release(neigh);
198 static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
200 struct lowpan_btle_dev *entry;
201 struct lowpan_peer *peer = NULL;
205 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
206 peer = __peer_lookup_conn(entry, conn);
216 static struct lowpan_btle_dev *lookup_dev(struct l2cap_conn *conn)
218 struct lowpan_btle_dev *entry;
219 struct lowpan_btle_dev *dev = NULL;
223 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
224 if (conn->hcon->hdev == entry->hdev) {
235 static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
237 struct sk_buff *skb_cp;
239 skb_cp = skb_copy(skb, GFP_ATOMIC);
243 return netif_rx(skb_cp);
246 static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev,
247 struct lowpan_peer *peer)
251 saddr = peer->lladdr;
253 return lowpan_header_decompress(skb, netdev, netdev->dev_addr, saddr);
256 static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
257 struct lowpan_peer *peer)
259 struct sk_buff *local_skb;
262 if (!netif_running(dev))
265 if (dev->type != ARPHRD_6LOWPAN || !skb->len)
268 skb_reset_network_header(skb);
270 skb = skb_share_check(skb, GFP_ATOMIC);
274 /* check that it's our buffer */
275 if (lowpan_is_ipv6(*skb_network_header(skb))) {
276 /* Pull off the 1-byte of 6lowpan header. */
279 /* Copy the packet so that the IPv6 header is
282 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
283 skb_tailroom(skb), GFP_ATOMIC);
287 local_skb->protocol = htons(ETH_P_IPV6);
288 local_skb->pkt_type = PACKET_HOST;
289 local_skb->dev = dev;
291 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
293 if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
294 kfree_skb(local_skb);
298 dev->stats.rx_bytes += skb->len;
299 dev->stats.rx_packets++;
301 consume_skb(local_skb);
303 } else if (lowpan_is_iphc(*skb_network_header(skb))) {
304 local_skb = skb_clone(skb, GFP_ATOMIC);
308 local_skb->dev = dev;
310 ret = iphc_decompress(local_skb, dev, peer);
312 BT_DBG("iphc_decompress failed: %d", ret);
313 kfree_skb(local_skb);
317 local_skb->protocol = htons(ETH_P_IPV6);
318 local_skb->pkt_type = PACKET_HOST;
320 if (give_skb_to_upper(local_skb, dev)
322 kfree_skb(local_skb);
326 dev->stats.rx_bytes += skb->len;
327 dev->stats.rx_packets++;
329 consume_skb(local_skb);
332 BT_DBG("unknown packet type");
336 return NET_RX_SUCCESS;
339 dev->stats.rx_dropped++;
343 /* Packet from BT LE device */
344 static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
346 struct lowpan_btle_dev *dev;
347 struct lowpan_peer *peer;
350 peer = lookup_peer(chan->conn);
354 dev = lookup_dev(chan->conn);
355 if (!dev || !dev->netdev)
358 err = recv_pkt(skb, dev->netdev, peer);
360 BT_DBG("recv pkt %d", err);
367 static int setup_header(struct sk_buff *skb, struct net_device *netdev,
368 bdaddr_t *peer_addr, u8 *peer_addr_type)
370 struct in6_addr ipv6_daddr;
372 struct lowpan_btle_dev *dev;
373 struct lowpan_peer *peer;
379 dev = lowpan_btle_dev(netdev);
381 memcpy(&ipv6_daddr, &hdr->daddr, sizeof(ipv6_daddr));
383 if (ipv6_addr_is_multicast(&ipv6_daddr)) {
384 lowpan_cb(skb)->chan = NULL;
387 BT_DBG("dest IP %pI6c", &ipv6_daddr);
389 /* The packet might be sent to 6lowpan interface
390 * because of routing (either via default route
391 * or user set route) so get peer according to
392 * the destination address.
394 peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
396 BT_DBG("no such peer");
400 daddr = peer->lladdr;
401 *peer_addr = peer->chan->dst;
402 *peer_addr_type = peer->chan->dst_type;
403 lowpan_cb(skb)->chan = peer->chan;
408 lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr);
410 err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
417 static int header_create(struct sk_buff *skb, struct net_device *netdev,
418 unsigned short type, const void *_daddr,
419 const void *_saddr, unsigned int len)
421 if (type != ETH_P_IPV6)
427 /* Packet to BT LE device */
428 static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
429 struct net_device *netdev)
435 /* Remember the skb so that we can send EAGAIN to the caller if
436 * we run out of credits.
440 iv.iov_base = skb->data;
441 iv.iov_len = skb->len;
443 memset(&msg, 0, sizeof(msg));
444 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iv, 1, skb->len);
446 err = l2cap_chan_send(chan, &msg, skb->len);
448 netdev->stats.tx_bytes += err;
449 netdev->stats.tx_packets++;
454 netdev->stats.tx_errors++;
459 static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
461 struct sk_buff *local_skb;
462 struct lowpan_btle_dev *entry;
467 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
468 struct lowpan_peer *pentry;
469 struct lowpan_btle_dev *dev;
471 if (entry->netdev != netdev)
474 dev = lowpan_btle_dev(entry->netdev);
476 list_for_each_entry_rcu(pentry, &dev->peers, list) {
479 local_skb = skb_clone(skb, GFP_ATOMIC);
481 BT_DBG("xmit %s to %pMR type %u IP %pI6c chan %p",
483 &pentry->chan->dst, pentry->chan->dst_type,
484 &pentry->peer_addr, pentry->chan);
485 ret = send_pkt(pentry->chan, local_skb, netdev);
489 kfree_skb(local_skb);
498 static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
504 /* We must take a copy of the skb before we modify/replace the ipv6
505 * header as the header could be used elsewhere
507 skb = skb_unshare(skb, GFP_ATOMIC);
509 return NET_XMIT_DROP;
511 /* Return values from setup_header()
512 * <0 - error, packet is dropped
513 * 0 - this is a multicast packet
514 * 1 - this is unicast packet
516 err = setup_header(skb, netdev, &addr, &addr_type);
519 return NET_XMIT_DROP;
523 if (lowpan_cb(skb)->chan) {
524 BT_DBG("xmit %s to %pMR type %u IP %pI6c chan %p",
525 netdev->name, &addr, addr_type,
526 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
527 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
532 /* We need to send the packet to every device behind this
535 err = send_mcast_pkt(skb, netdev);
541 BT_DBG("ERROR: xmit failed (%d)", err);
543 return err < 0 ? NET_XMIT_DROP : err;
546 static int bt_dev_init(struct net_device *dev)
548 netdev_lockdep_set_classes(dev);
553 static const struct net_device_ops netdev_ops = {
554 .ndo_init = bt_dev_init,
555 .ndo_start_xmit = bt_xmit,
558 static const struct header_ops header_ops = {
559 .create = header_create,
562 static void netdev_setup(struct net_device *dev)
564 dev->hard_header_len = 0;
565 dev->needed_tailroom = 0;
566 dev->flags = IFF_RUNNING | IFF_MULTICAST;
567 dev->watchdog_timeo = 0;
568 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
570 dev->netdev_ops = &netdev_ops;
571 dev->header_ops = &header_ops;
572 dev->needs_free_netdev = true;
575 static struct device_type bt_type = {
579 static void ifup(struct net_device *netdev)
584 err = dev_open(netdev, NULL);
586 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
590 static void ifdown(struct net_device *netdev)
597 static void do_notify_peers(struct work_struct *work)
599 struct lowpan_btle_dev *dev = container_of(work, struct lowpan_btle_dev,
602 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
605 static bool is_bt_6lowpan(struct hci_conn *hcon)
607 if (hcon->type != LE_LINK)
616 static struct l2cap_chan *chan_create(void)
618 struct l2cap_chan *chan;
620 chan = l2cap_chan_create();
624 l2cap_chan_set_defaults(chan);
626 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
627 chan->mode = L2CAP_MODE_LE_FLOWCTL;
633 static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
634 struct lowpan_btle_dev *dev,
637 struct lowpan_peer *peer;
639 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
645 baswap((void *)peer->lladdr, &chan->dst);
647 lowpan_iphc_uncompress_eui48_lladdr(&peer->peer_addr, peer->lladdr);
649 spin_lock(&devices_lock);
650 INIT_LIST_HEAD(&peer->list);
652 spin_unlock(&devices_lock);
654 /* Notifying peers about us needs to be done without locks held */
656 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
657 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
662 static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
664 struct net_device *netdev;
668 netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_btle_dev)),
669 IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN,
674 netdev->addr_assign_type = NET_ADDR_PERM;
675 baswap(&addr, &chan->src);
676 __dev_addr_set(netdev, &addr, sizeof(addr));
678 netdev->netdev_ops = &netdev_ops;
679 SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev);
680 SET_NETDEV_DEVTYPE(netdev, &bt_type);
682 *dev = lowpan_btle_dev(netdev);
683 (*dev)->netdev = netdev;
684 (*dev)->hdev = chan->conn->hcon->hdev;
685 INIT_LIST_HEAD(&(*dev)->peers);
687 spin_lock(&devices_lock);
688 INIT_LIST_HEAD(&(*dev)->list);
689 list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
690 spin_unlock(&devices_lock);
692 err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE);
694 BT_INFO("register_netdev failed %d", err);
695 spin_lock(&devices_lock);
696 list_del_rcu(&(*dev)->list);
697 spin_unlock(&devices_lock);
702 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
703 netdev->ifindex, &chan->dst, chan->dst_type,
704 &chan->src, chan->src_type);
705 set_bit(__LINK_STATE_PRESENT, &netdev->state);
713 static inline void chan_ready_cb(struct l2cap_chan *chan)
715 struct lowpan_btle_dev *dev;
716 bool new_netdev = false;
718 dev = lookup_dev(chan->conn);
720 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
723 if (setup_netdev(chan, &dev) < 0) {
724 l2cap_chan_del(chan, -ENOENT);
730 if (!try_module_get(THIS_MODULE))
733 add_peer_chan(chan, dev, new_netdev);
737 static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
739 struct l2cap_chan *chan;
741 chan = chan_create();
745 chan->ops = pchan->ops;
747 BT_DBG("chan %p pchan %p", chan, pchan);
752 static void delete_netdev(struct work_struct *work)
754 struct lowpan_btle_dev *entry = container_of(work,
755 struct lowpan_btle_dev,
758 lowpan_unregister_netdev(entry->netdev);
760 /* The entry pointer is deleted by the netdev destructor. */
763 static void chan_close_cb(struct l2cap_chan *chan)
765 struct lowpan_btle_dev *entry;
766 struct lowpan_btle_dev *dev = NULL;
767 struct lowpan_peer *peer;
769 bool last = false, remove = true;
771 BT_DBG("chan %p conn %p", chan, chan->conn);
773 if (chan->conn && chan->conn->hcon) {
774 if (!is_bt_6lowpan(chan->conn->hcon))
777 /* If conn is set, then the netdev is also there and we should
783 spin_lock(&devices_lock);
785 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
786 dev = lowpan_btle_dev(entry->netdev);
787 peer = __peer_lookup_chan(dev, chan);
789 last = peer_del(dev, peer);
792 BT_DBG("dev %p removing %speer %p", dev,
793 last ? "last " : "1 ", peer);
794 BT_DBG("chan %p orig refcnt %u", chan,
795 kref_read(&chan->kref));
797 l2cap_chan_put(chan);
802 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
803 spin_unlock(&devices_lock);
805 cancel_delayed_work_sync(&dev->notify_peers);
810 INIT_WORK(&entry->delete_netdev, delete_netdev);
811 schedule_work(&entry->delete_netdev);
814 spin_unlock(&devices_lock);
818 static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
820 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
821 state_to_string(state), err);
824 static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
825 unsigned long hdr_len,
826 unsigned long len, int nb)
828 /* Note that we must allocate using GFP_ATOMIC here as
829 * this function is called originally from netdev hard xmit
830 * function in atomic context.
832 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
835 static void chan_suspend_cb(struct l2cap_chan *chan)
837 struct lowpan_btle_dev *dev;
839 BT_DBG("chan %p suspend", chan);
841 dev = lookup_dev(chan->conn);
842 if (!dev || !dev->netdev)
845 netif_stop_queue(dev->netdev);
848 static void chan_resume_cb(struct l2cap_chan *chan)
850 struct lowpan_btle_dev *dev;
852 BT_DBG("chan %p resume", chan);
854 dev = lookup_dev(chan->conn);
855 if (!dev || !dev->netdev)
858 netif_wake_queue(dev->netdev);
861 static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
863 return L2CAP_CONN_TIMEOUT;
866 static const struct l2cap_ops bt_6lowpan_chan_ops = {
867 .name = "L2CAP 6LoWPAN channel",
868 .new_connection = chan_new_conn_cb,
869 .recv = chan_recv_cb,
870 .close = chan_close_cb,
871 .state_change = chan_state_change_cb,
872 .ready = chan_ready_cb,
873 .resume = chan_resume_cb,
874 .suspend = chan_suspend_cb,
875 .get_sndtimeo = chan_get_sndtimeo_cb,
876 .alloc_skb = chan_alloc_skb_cb,
878 .teardown = l2cap_chan_no_teardown,
879 .defer = l2cap_chan_no_defer,
880 .set_shutdown = l2cap_chan_no_set_shutdown,
883 static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
885 struct l2cap_chan *chan;
888 chan = chan_create();
892 chan->ops = &bt_6lowpan_chan_ops;
894 err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0,
897 BT_DBG("chan %p err %d", chan, err);
899 l2cap_chan_put(chan);
904 static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
906 struct lowpan_peer *peer;
908 BT_DBG("conn %p dst type %u", conn, dst_type);
910 peer = lookup_peer(conn);
914 BT_DBG("peer %p chan %p", peer, peer->chan);
916 l2cap_chan_close(peer->chan, ENOENT);
921 static struct l2cap_chan *bt_6lowpan_listen(void)
923 bdaddr_t *addr = BDADDR_ANY;
924 struct l2cap_chan *chan;
930 chan = chan_create();
934 chan->ops = &bt_6lowpan_chan_ops;
935 chan->state = BT_LISTEN;
936 chan->src_type = BDADDR_LE_PUBLIC;
938 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
940 BT_DBG("chan %p src type %u", chan, chan->src_type);
942 err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP));
944 l2cap_chan_put(chan);
945 BT_ERR("psm cannot be added err %d", err);
952 static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
953 struct l2cap_conn **conn)
955 struct hci_conn *hcon;
956 struct hci_dev *hdev;
959 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
960 &addr->b[5], &addr->b[4], &addr->b[3],
961 &addr->b[2], &addr->b[1], &addr->b[0],
967 /* The LE_PUBLIC address type is ignored because of BDADDR_ANY */
968 hdev = hci_get_route(addr, BDADDR_ANY, BDADDR_LE_PUBLIC);
973 hcon = hci_conn_hash_lookup_le(hdev, addr, *addr_type);
974 hci_dev_unlock(hdev);
980 *conn = (struct l2cap_conn *)hcon->l2cap_data;
982 BT_DBG("conn %p dst %pMR type %u", *conn, &hcon->dst, hcon->dst_type);
987 static void disconnect_all_peers(void)
989 struct lowpan_btle_dev *entry;
990 struct lowpan_peer *peer, *tmp_peer, *new_peer;
991 struct list_head peers;
993 INIT_LIST_HEAD(&peers);
995 /* We make a separate list of peers as the close_cb() will
996 * modify the device peers list so it is better not to mess
997 * with the same list at the same time.
1002 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1003 list_for_each_entry_rcu(peer, &entry->peers, list) {
1004 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1008 new_peer->chan = peer->chan;
1009 INIT_LIST_HEAD(&new_peer->list);
1011 list_add(&new_peer->list, &peers);
1017 spin_lock(&devices_lock);
1018 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1019 l2cap_chan_close(peer->chan, ENOENT);
1021 list_del_rcu(&peer->list);
1022 kfree_rcu(peer, rcu);
1024 spin_unlock(&devices_lock);
1028 struct work_struct work;
1032 static void do_enable_set(struct work_struct *work)
1034 struct set_enable *set_enable = container_of(work,
1035 struct set_enable, work);
1037 if (!set_enable->flag || enable_6lowpan != set_enable->flag)
1038 /* Disconnect existing connections if 6lowpan is
1041 disconnect_all_peers();
1043 enable_6lowpan = set_enable->flag;
1045 mutex_lock(&set_lock);
1047 l2cap_chan_close(listen_chan, 0);
1048 l2cap_chan_put(listen_chan);
1051 listen_chan = bt_6lowpan_listen();
1052 mutex_unlock(&set_lock);
1057 static int lowpan_enable_set(void *data, u64 val)
1059 struct set_enable *set_enable;
1061 set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL);
1065 set_enable->flag = !!val;
1066 INIT_WORK(&set_enable->work, do_enable_set);
1068 schedule_work(&set_enable->work);
1073 static int lowpan_enable_get(void *data, u64 *val)
1075 *val = enable_6lowpan;
1079 DEFINE_DEBUGFS_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get,
1080 lowpan_enable_set, "%llu\n");
1082 static ssize_t lowpan_control_write(struct file *fp,
1083 const char __user *user_buffer,
1088 size_t buf_size = min(count, sizeof(buf) - 1);
1092 struct l2cap_conn *conn = NULL;
1094 if (copy_from_user(buf, user_buffer, buf_size))
1097 buf[buf_size] = '\0';
1099 if (memcmp(buf, "connect ", 8) == 0) {
1100 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1104 mutex_lock(&set_lock);
1106 l2cap_chan_close(listen_chan, 0);
1107 l2cap_chan_put(listen_chan);
1110 mutex_unlock(&set_lock);
1113 struct lowpan_peer *peer;
1115 if (!is_bt_6lowpan(conn->hcon))
1118 peer = lookup_peer(conn);
1120 BT_DBG("6LoWPAN connection already exists");
1124 BT_DBG("conn %p dst %pMR type %d user %u", conn,
1125 &conn->hcon->dst, conn->hcon->dst_type,
1129 ret = bt_6lowpan_connect(&addr, addr_type);
1136 if (memcmp(buf, "disconnect ", 11) == 0) {
1137 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1141 ret = bt_6lowpan_disconnect(conn, addr_type);
1151 static int lowpan_control_show(struct seq_file *f, void *ptr)
1153 struct lowpan_btle_dev *entry;
1154 struct lowpan_peer *peer;
1156 spin_lock(&devices_lock);
1158 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1159 list_for_each_entry(peer, &entry->peers, list)
1160 seq_printf(f, "%pMR (type %u)\n",
1161 &peer->chan->dst, peer->chan->dst_type);
1164 spin_unlock(&devices_lock);
1169 static int lowpan_control_open(struct inode *inode, struct file *file)
1171 return single_open(file, lowpan_control_show, inode->i_private);
1174 static const struct file_operations lowpan_control_fops = {
1175 .open = lowpan_control_open,
1177 .write = lowpan_control_write,
1178 .llseek = seq_lseek,
1179 .release = single_release,
1182 static void disconnect_devices(void)
1184 struct lowpan_btle_dev *entry, *tmp, *new_dev;
1185 struct list_head devices;
1187 INIT_LIST_HEAD(&devices);
1189 /* We make a separate list of devices because the unregister_netdev()
1190 * will call device_event() which will also want to modify the same
1196 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1197 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1201 new_dev->netdev = entry->netdev;
1202 INIT_LIST_HEAD(&new_dev->list);
1204 list_add_rcu(&new_dev->list, &devices);
1209 list_for_each_entry_safe(entry, tmp, &devices, list) {
1210 ifdown(entry->netdev);
1211 BT_DBG("Unregistering netdev %s %p",
1212 entry->netdev->name, entry->netdev);
1213 lowpan_unregister_netdev(entry->netdev);
1218 static int device_event(struct notifier_block *unused,
1219 unsigned long event, void *ptr)
1221 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1222 struct lowpan_btle_dev *entry;
1224 if (netdev->type != ARPHRD_6LOWPAN)
1228 case NETDEV_UNREGISTER:
1229 spin_lock(&devices_lock);
1230 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1231 if (entry->netdev == netdev) {
1232 BT_DBG("Unregistered netdev %s %p",
1233 netdev->name, netdev);
1234 list_del(&entry->list);
1238 spin_unlock(&devices_lock);
1245 static struct notifier_block bt_6lowpan_dev_notifier = {
1246 .notifier_call = device_event,
1249 static int __init bt_6lowpan_init(void)
1251 lowpan_enable_debugfs = debugfs_create_file_unsafe("6lowpan_enable",
1254 &lowpan_enable_fops);
1255 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1257 &lowpan_control_fops);
1259 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1262 static void __exit bt_6lowpan_exit(void)
1264 debugfs_remove(lowpan_enable_debugfs);
1265 debugfs_remove(lowpan_control_debugfs);
1268 l2cap_chan_close(listen_chan, 0);
1269 l2cap_chan_put(listen_chan);
1272 disconnect_devices();
1274 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1277 module_init(bt_6lowpan_init);
1278 module_exit(bt_6lowpan_exit);
1280 MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1281 MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1282 MODULE_VERSION(VERSION);
1283 MODULE_LICENSE("GPL");