2 Copyright (c) 2013-2014 Intel Corp.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 and
6 only version 2 as published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
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>
21 #include <net/ip6_route.h>
22 #include <net/addrconf.h>
24 #include <net/bluetooth/bluetooth.h>
25 #include <net/bluetooth/hci_core.h>
26 #include <net/bluetooth/l2cap.h>
28 #include <net/6lowpan.h> /* for the compression support */
32 static struct dentry *lowpan_enable_debugfs;
33 static struct dentry *lowpan_control_debugfs;
35 #define IFACE_NAME_TEMPLATE "bt%d"
40 struct l2cap_chan *chan;
43 #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
45 /* The devices list contains those devices that we are acting
46 * as a proxy. The BT 6LoWPAN device is a virtual device that
47 * connects to the Bluetooth LE device. The real connection to
48 * BT device is done via l2cap layer. There exists one
49 * virtual device / one BT 6LoWPAN network (=hciX device).
50 * The list contains struct lowpan_dev elements.
52 static LIST_HEAD(bt_6lowpan_devices);
53 static DEFINE_SPINLOCK(devices_lock);
55 static bool enable_6lowpan;
57 /* We are listening incoming connections via this channel
59 static struct l2cap_chan *listen_chan;
60 static DEFINE_MUTEX(set_lock);
63 struct list_head list;
65 struct l2cap_chan *chan;
67 /* peer addresses in various formats */
68 unsigned char eui64_addr[EUI64_ADDR_LEN];
69 struct in6_addr peer_addr;
73 struct list_head list;
76 struct net_device *netdev;
77 struct list_head peers;
78 atomic_t peer_count; /* number of items in peers list */
80 struct work_struct delete_netdev;
81 struct delayed_work notify_peers;
84 static inline struct lowpan_dev *lowpan_dev(const struct net_device *netdev)
86 return (struct lowpan_dev *)lowpan_priv(netdev)->priv;
89 static inline void peer_add(struct lowpan_dev *dev, struct lowpan_peer *peer)
91 list_add_rcu(&peer->list, &dev->peers);
92 atomic_inc(&dev->peer_count);
95 static inline bool peer_del(struct lowpan_dev *dev, struct lowpan_peer *peer)
97 list_del_rcu(&peer->list);
100 module_put(THIS_MODULE);
102 if (atomic_dec_and_test(&dev->peer_count)) {
110 static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_dev *dev,
111 bdaddr_t *ba, __u8 type)
113 struct lowpan_peer *peer;
115 BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count),
120 list_for_each_entry_rcu(peer, &dev->peers, list) {
121 BT_DBG("dst addr %pMR dst type %d",
122 &peer->chan->dst, peer->chan->dst_type);
124 if (bacmp(&peer->chan->dst, ba))
127 if (type == peer->chan->dst_type) {
138 static inline struct lowpan_peer *__peer_lookup_chan(struct lowpan_dev *dev,
139 struct l2cap_chan *chan)
141 struct lowpan_peer *peer;
143 list_for_each_entry_rcu(peer, &dev->peers, list) {
144 if (peer->chan == chan)
151 static inline struct lowpan_peer *__peer_lookup_conn(struct lowpan_dev *dev,
152 struct l2cap_conn *conn)
154 struct lowpan_peer *peer;
156 list_for_each_entry_rcu(peer, &dev->peers, list) {
157 if (peer->chan->conn == conn)
164 static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_dev *dev,
165 struct in6_addr *daddr,
168 struct lowpan_peer *peer;
169 struct in6_addr *nexthop;
170 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
171 int count = atomic_read(&dev->peer_count);
173 BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt);
175 /* If we have multiple 6lowpan peers, then check where we should
176 * send the packet. If only one peer exists, then we can send the
181 peer = list_first_or_null_rcu(&dev->peers, struct lowpan_peer,
188 if (ipv6_addr_any(&lowpan_cb(skb)->gw)) {
189 /* There is neither route nor gateway,
190 * probably the destination is a direct peer.
194 /* There is a known gateway
196 nexthop = &lowpan_cb(skb)->gw;
199 nexthop = rt6_nexthop(rt, daddr);
201 /* We need to remember the address because it is needed
202 * by bt_xmit() when sending the packet. In bt_xmit(), the
203 * destination routing info is not set.
205 memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr));
208 BT_DBG("gw %pI6c", nexthop);
212 list_for_each_entry_rcu(peer, &dev->peers, list) {
213 BT_DBG("dst addr %pMR dst type %d ip %pI6c",
214 &peer->chan->dst, peer->chan->dst_type,
217 if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) {
228 static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
230 struct lowpan_dev *entry;
231 struct lowpan_peer *peer = NULL;
235 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
236 peer = __peer_lookup_conn(entry, conn);
246 static struct lowpan_dev *lookup_dev(struct l2cap_conn *conn)
248 struct lowpan_dev *entry;
249 struct lowpan_dev *dev = NULL;
253 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
254 if (conn->hcon->hdev == entry->hdev) {
265 static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
267 struct sk_buff *skb_cp;
269 skb_cp = skb_copy(skb, GFP_ATOMIC);
273 return netif_rx_ni(skb_cp);
276 static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev,
277 struct l2cap_chan *chan)
279 const u8 *saddr, *daddr;
280 struct lowpan_dev *dev;
281 struct lowpan_peer *peer;
283 dev = lowpan_dev(netdev);
286 peer = __peer_lookup_chan(dev, chan);
291 saddr = peer->eui64_addr;
292 daddr = dev->netdev->dev_addr;
294 return lowpan_header_decompress(skb, netdev, daddr, saddr);
297 static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
298 struct l2cap_chan *chan)
300 struct sk_buff *local_skb;
303 if (!netif_running(dev))
306 if (dev->type != ARPHRD_6LOWPAN || !skb->len)
309 skb_reset_network_header(skb);
311 skb = skb_share_check(skb, GFP_ATOMIC);
315 /* check that it's our buffer */
316 if (lowpan_is_ipv6(*skb_network_header(skb))) {
317 /* Pull off the 1-byte of 6lowpan header. */
320 /* Copy the packet so that the IPv6 header is
323 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
324 skb_tailroom(skb), GFP_ATOMIC);
328 local_skb->protocol = htons(ETH_P_IPV6);
329 local_skb->pkt_type = PACKET_HOST;
330 local_skb->dev = dev;
332 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
334 if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
335 kfree_skb(local_skb);
339 dev->stats.rx_bytes += skb->len;
340 dev->stats.rx_packets++;
342 consume_skb(local_skb);
344 } else if (lowpan_is_iphc(*skb_network_header(skb))) {
345 local_skb = skb_clone(skb, GFP_ATOMIC);
349 local_skb->dev = dev;
351 ret = iphc_decompress(local_skb, dev, chan);
353 kfree_skb(local_skb);
357 local_skb->protocol = htons(ETH_P_IPV6);
358 local_skb->pkt_type = PACKET_HOST;
360 if (give_skb_to_upper(local_skb, dev)
362 kfree_skb(local_skb);
366 dev->stats.rx_bytes += skb->len;
367 dev->stats.rx_packets++;
369 consume_skb(local_skb);
375 return NET_RX_SUCCESS;
378 dev->stats.rx_dropped++;
382 /* Packet from BT LE device */
383 static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
385 struct lowpan_dev *dev;
386 struct lowpan_peer *peer;
389 peer = lookup_peer(chan->conn);
393 dev = lookup_dev(chan->conn);
394 if (!dev || !dev->netdev)
397 err = recv_pkt(skb, dev->netdev, chan);
399 BT_DBG("recv pkt %d", err);
406 static u8 get_addr_type_from_eui64(u8 byte)
408 /* Is universal(0) or local(1) bit */
409 return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC);
412 static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr)
414 u8 *eui64 = ip6_daddr->s6_addr + 8;
416 addr->b[0] = eui64[7];
417 addr->b[1] = eui64[6];
418 addr->b[2] = eui64[5];
419 addr->b[3] = eui64[2];
420 addr->b[4] = eui64[1];
421 addr->b[5] = eui64[0];
424 static void convert_dest_bdaddr(struct in6_addr *ip6_daddr,
425 bdaddr_t *addr, u8 *addr_type)
427 copy_to_bdaddr(ip6_daddr, addr);
429 /* We need to toggle the U/L bit that we got from IPv6 address
430 * so that we get the proper address and type of the BD address.
434 *addr_type = get_addr_type_from_eui64(addr->b[5]);
437 static int setup_header(struct sk_buff *skb, struct net_device *netdev,
438 bdaddr_t *peer_addr, u8 *peer_addr_type)
440 struct in6_addr ipv6_daddr;
441 struct lowpan_dev *dev;
442 struct lowpan_peer *peer;
443 bdaddr_t addr, *any = BDADDR_ANY;
447 dev = lowpan_dev(netdev);
449 memcpy(&ipv6_daddr, &lowpan_cb(skb)->addr, sizeof(ipv6_daddr));
451 if (ipv6_addr_is_multicast(&ipv6_daddr)) {
452 lowpan_cb(skb)->chan = NULL;
456 /* Get destination BT device from skb.
457 * If there is no such peer then discard the packet.
459 convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type);
461 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr,
462 addr_type, &ipv6_daddr);
464 peer = peer_lookup_ba(dev, &addr, addr_type);
466 /* The packet might be sent to 6lowpan interface
467 * because of routing (either via default route
468 * or user set route) so get peer according to
469 * the destination address.
471 peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
473 BT_DBG("no such peer %pMR found", &addr);
478 daddr = peer->eui64_addr;
480 *peer_addr_type = addr_type;
481 lowpan_cb(skb)->chan = peer->chan;
486 lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr);
488 err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
495 static int header_create(struct sk_buff *skb, struct net_device *netdev,
496 unsigned short type, const void *_daddr,
497 const void *_saddr, unsigned int len)
501 if (type != ETH_P_IPV6)
506 memcpy(&lowpan_cb(skb)->addr, &hdr->daddr, sizeof(struct in6_addr));
511 /* Packet to BT LE device */
512 static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
513 struct net_device *netdev)
519 /* Remember the skb so that we can send EAGAIN to the caller if
520 * we run out of credits.
524 iv.iov_base = skb->data;
525 iv.iov_len = skb->len;
527 memset(&msg, 0, sizeof(msg));
528 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iv, 1, skb->len);
530 err = l2cap_chan_send(chan, &msg, skb->len);
532 netdev->stats.tx_bytes += err;
533 netdev->stats.tx_packets++;
538 err = lowpan_cb(skb)->status;
542 netdev->stats.tx_dropped++;
544 netdev->stats.tx_errors++;
550 static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
552 struct sk_buff *local_skb;
553 struct lowpan_dev *entry;
558 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
559 struct lowpan_peer *pentry;
560 struct lowpan_dev *dev;
562 if (entry->netdev != netdev)
565 dev = lowpan_dev(entry->netdev);
567 list_for_each_entry_rcu(pentry, &dev->peers, list) {
570 local_skb = skb_clone(skb, GFP_ATOMIC);
572 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
574 &pentry->chan->dst, pentry->chan->dst_type,
575 &pentry->peer_addr, pentry->chan);
576 ret = send_pkt(pentry->chan, local_skb, netdev);
580 kfree_skb(local_skb);
589 static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
595 /* We must take a copy of the skb before we modify/replace the ipv6
596 * header as the header could be used elsewhere
598 skb = skb_unshare(skb, GFP_ATOMIC);
600 return NET_XMIT_DROP;
602 /* Return values from setup_header()
603 * <0 - error, packet is dropped
604 * 0 - this is a multicast packet
605 * 1 - this is unicast packet
607 err = setup_header(skb, netdev, &addr, &addr_type);
610 return NET_XMIT_DROP;
614 if (lowpan_cb(skb)->chan) {
615 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
616 netdev->name, &addr, addr_type,
617 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
618 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
623 /* We need to send the packet to every device behind this
626 err = send_mcast_pkt(skb, netdev);
632 BT_DBG("ERROR: xmit failed (%d)", err);
634 return err < 0 ? NET_XMIT_DROP : err;
637 static struct lock_class_key bt_tx_busylock;
638 static struct lock_class_key bt_netdev_xmit_lock_key;
640 static void bt_set_lockdep_class_one(struct net_device *dev,
641 struct netdev_queue *txq,
644 lockdep_set_class(&txq->_xmit_lock, &bt_netdev_xmit_lock_key);
647 static int bt_dev_init(struct net_device *dev)
649 netdev_for_each_tx_queue(dev, bt_set_lockdep_class_one, NULL);
650 dev->qdisc_tx_busylock = &bt_tx_busylock;
655 static const struct net_device_ops netdev_ops = {
656 .ndo_init = bt_dev_init,
657 .ndo_start_xmit = bt_xmit,
660 static struct header_ops header_ops = {
661 .create = header_create,
664 static void netdev_setup(struct net_device *dev)
666 dev->hard_header_len = 0;
667 dev->needed_tailroom = 0;
668 dev->flags = IFF_RUNNING | IFF_POINTOPOINT |
670 dev->watchdog_timeo = 0;
672 dev->netdev_ops = &netdev_ops;
673 dev->header_ops = &header_ops;
674 dev->destructor = free_netdev;
677 static struct device_type bt_type = {
681 static void set_addr(u8 *eui, u8 *addr, u8 addr_type)
683 /* addr is the BT address in little-endian format */
693 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
694 if (addr_type == BDADDR_LE_PUBLIC)
699 BT_DBG("type %d addr %*phC", addr_type, 8, eui);
702 static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr,
705 netdev->addr_assign_type = NET_ADDR_PERM;
706 set_addr(netdev->dev_addr, addr->b, addr_type);
709 static void ifup(struct net_device *netdev)
714 err = dev_open(netdev);
716 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
720 static void ifdown(struct net_device *netdev)
725 err = dev_close(netdev);
727 BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
731 static void do_notify_peers(struct work_struct *work)
733 struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
736 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
739 static bool is_bt_6lowpan(struct hci_conn *hcon)
741 if (hcon->type != LE_LINK)
750 static struct l2cap_chan *chan_create(void)
752 struct l2cap_chan *chan;
754 chan = l2cap_chan_create();
758 l2cap_chan_set_defaults(chan);
760 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
761 chan->mode = L2CAP_MODE_LE_FLOWCTL;
767 static void set_ip_addr_bits(u8 addr_type, u8 *addr)
769 if (addr_type == BDADDR_LE_PUBLIC)
775 static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
776 struct lowpan_dev *dev)
778 struct lowpan_peer *peer;
780 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
785 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
788 peer->peer_addr.s6_addr[0] = 0xFE;
789 peer->peer_addr.s6_addr[1] = 0x80;
790 set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
793 memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
796 /* IPv6 address needs to have the U/L bit set properly so toggle
799 set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8);
801 spin_lock(&devices_lock);
802 INIT_LIST_HEAD(&peer->list);
804 spin_unlock(&devices_lock);
806 /* Notifying peers about us needs to be done without locks held */
807 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
808 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
813 static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
815 struct net_device *netdev;
818 netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_dev)),
819 IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN,
824 set_dev_addr(netdev, &chan->src, chan->src_type);
826 netdev->netdev_ops = &netdev_ops;
827 SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev);
828 SET_NETDEV_DEVTYPE(netdev, &bt_type);
830 *dev = lowpan_dev(netdev);
831 (*dev)->netdev = netdev;
832 (*dev)->hdev = chan->conn->hcon->hdev;
833 INIT_LIST_HEAD(&(*dev)->peers);
835 spin_lock(&devices_lock);
836 INIT_LIST_HEAD(&(*dev)->list);
837 list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
838 spin_unlock(&devices_lock);
840 lowpan_netdev_setup(netdev, LOWPAN_LLTYPE_BTLE);
842 err = register_netdev(netdev);
844 BT_INFO("register_netdev failed %d", err);
845 spin_lock(&devices_lock);
846 list_del_rcu(&(*dev)->list);
847 spin_unlock(&devices_lock);
852 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
853 netdev->ifindex, &chan->dst, chan->dst_type,
854 &chan->src, chan->src_type);
855 set_bit(__LINK_STATE_PRESENT, &netdev->state);
863 static inline void chan_ready_cb(struct l2cap_chan *chan)
865 struct lowpan_dev *dev;
867 dev = lookup_dev(chan->conn);
869 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
872 if (setup_netdev(chan, &dev) < 0) {
873 l2cap_chan_del(chan, -ENOENT);
878 if (!try_module_get(THIS_MODULE))
881 add_peer_chan(chan, dev);
885 static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
887 struct l2cap_chan *chan;
889 chan = chan_create();
893 chan->ops = pchan->ops;
895 BT_DBG("chan %p pchan %p", chan, pchan);
900 static void delete_netdev(struct work_struct *work)
902 struct lowpan_dev *entry = container_of(work, struct lowpan_dev,
905 unregister_netdev(entry->netdev);
907 /* The entry pointer is deleted by the netdev destructor. */
910 static void chan_close_cb(struct l2cap_chan *chan)
912 struct lowpan_dev *entry;
913 struct lowpan_dev *dev = NULL;
914 struct lowpan_peer *peer;
916 bool last = false, remove = true;
918 BT_DBG("chan %p conn %p", chan, chan->conn);
920 if (chan->conn && chan->conn->hcon) {
921 if (!is_bt_6lowpan(chan->conn->hcon))
924 /* If conn is set, then the netdev is also there and we should
930 spin_lock(&devices_lock);
932 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
933 dev = lowpan_dev(entry->netdev);
934 peer = __peer_lookup_chan(dev, chan);
936 last = peer_del(dev, peer);
939 BT_DBG("dev %p removing %speer %p", dev,
940 last ? "last " : "1 ", peer);
941 BT_DBG("chan %p orig refcnt %d", chan,
942 atomic_read(&chan->kref.refcount));
944 l2cap_chan_put(chan);
949 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
950 spin_unlock(&devices_lock);
952 cancel_delayed_work_sync(&dev->notify_peers);
957 INIT_WORK(&entry->delete_netdev, delete_netdev);
958 schedule_work(&entry->delete_netdev);
961 spin_unlock(&devices_lock);
967 static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
969 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
970 state_to_string(state), err);
973 static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
974 unsigned long hdr_len,
975 unsigned long len, int nb)
977 /* Note that we must allocate using GFP_ATOMIC here as
978 * this function is called originally from netdev hard xmit
979 * function in atomic context.
981 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
984 static void chan_suspend_cb(struct l2cap_chan *chan)
986 struct sk_buff *skb = chan->data;
988 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
993 lowpan_cb(skb)->status = -EAGAIN;
996 static void chan_resume_cb(struct l2cap_chan *chan)
998 struct sk_buff *skb = chan->data;
1000 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
1005 lowpan_cb(skb)->status = 0;
1008 static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
1010 return L2CAP_CONN_TIMEOUT;
1013 static const struct l2cap_ops bt_6lowpan_chan_ops = {
1014 .name = "L2CAP 6LoWPAN channel",
1015 .new_connection = chan_new_conn_cb,
1016 .recv = chan_recv_cb,
1017 .close = chan_close_cb,
1018 .state_change = chan_state_change_cb,
1019 .ready = chan_ready_cb,
1020 .resume = chan_resume_cb,
1021 .suspend = chan_suspend_cb,
1022 .get_sndtimeo = chan_get_sndtimeo_cb,
1023 .alloc_skb = chan_alloc_skb_cb,
1025 .teardown = l2cap_chan_no_teardown,
1026 .defer = l2cap_chan_no_defer,
1027 .set_shutdown = l2cap_chan_no_set_shutdown,
1030 static inline __u8 bdaddr_type(__u8 type)
1032 if (type == ADDR_LE_DEV_PUBLIC)
1033 return BDADDR_LE_PUBLIC;
1035 return BDADDR_LE_RANDOM;
1038 static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
1040 struct l2cap_chan *chan;
1043 chan = chan_create();
1047 chan->ops = &bt_6lowpan_chan_ops;
1049 err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0,
1052 BT_DBG("chan %p err %d", chan, err);
1054 l2cap_chan_put(chan);
1059 static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
1061 struct lowpan_peer *peer;
1063 BT_DBG("conn %p dst type %d", conn, dst_type);
1065 peer = lookup_peer(conn);
1069 BT_DBG("peer %p chan %p", peer, peer->chan);
1071 l2cap_chan_close(peer->chan, ENOENT);
1076 static struct l2cap_chan *bt_6lowpan_listen(void)
1078 bdaddr_t *addr = BDADDR_ANY;
1079 struct l2cap_chan *chan;
1082 if (!enable_6lowpan)
1085 chan = chan_create();
1089 chan->ops = &bt_6lowpan_chan_ops;
1090 chan->state = BT_LISTEN;
1091 chan->src_type = BDADDR_LE_PUBLIC;
1093 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
1095 BT_DBG("chan %p src type %d", chan, chan->src_type);
1097 err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP));
1099 l2cap_chan_put(chan);
1100 BT_ERR("psm cannot be added err %d", err);
1107 static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
1108 struct l2cap_conn **conn)
1110 struct hci_conn *hcon;
1111 struct hci_dev *hdev;
1112 bdaddr_t *src = BDADDR_ANY;
1115 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1116 &addr->b[5], &addr->b[4], &addr->b[3],
1117 &addr->b[2], &addr->b[1], &addr->b[0],
1123 hdev = hci_get_route(addr, src);
1128 hcon = hci_conn_hash_lookup_le(hdev, addr, *addr_type);
1129 hci_dev_unlock(hdev);
1134 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1136 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1141 static void disconnect_all_peers(void)
1143 struct lowpan_dev *entry;
1144 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1145 struct list_head peers;
1147 INIT_LIST_HEAD(&peers);
1149 /* We make a separate list of peers as the close_cb() will
1150 * modify the device peers list so it is better not to mess
1151 * with the same list at the same time.
1156 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1157 list_for_each_entry_rcu(peer, &entry->peers, list) {
1158 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1162 new_peer->chan = peer->chan;
1163 INIT_LIST_HEAD(&new_peer->list);
1165 list_add(&new_peer->list, &peers);
1171 spin_lock(&devices_lock);
1172 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1173 l2cap_chan_close(peer->chan, ENOENT);
1175 list_del_rcu(&peer->list);
1176 kfree_rcu(peer, rcu);
1178 spin_unlock(&devices_lock);
1182 struct work_struct work;
1186 static void do_enable_set(struct work_struct *work)
1188 struct set_enable *set_enable = container_of(work,
1189 struct set_enable, work);
1191 if (!set_enable->flag || enable_6lowpan != set_enable->flag)
1192 /* Disconnect existing connections if 6lowpan is
1195 disconnect_all_peers();
1197 enable_6lowpan = set_enable->flag;
1199 mutex_lock(&set_lock);
1201 l2cap_chan_close(listen_chan, 0);
1202 l2cap_chan_put(listen_chan);
1205 listen_chan = bt_6lowpan_listen();
1206 mutex_unlock(&set_lock);
1211 static int lowpan_enable_set(void *data, u64 val)
1213 struct set_enable *set_enable;
1215 set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL);
1219 set_enable->flag = !!val;
1220 INIT_WORK(&set_enable->work, do_enable_set);
1222 schedule_work(&set_enable->work);
1227 static int lowpan_enable_get(void *data, u64 *val)
1229 *val = enable_6lowpan;
1233 DEFINE_SIMPLE_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get,
1234 lowpan_enable_set, "%llu\n");
1236 static ssize_t lowpan_control_write(struct file *fp,
1237 const char __user *user_buffer,
1242 size_t buf_size = min(count, sizeof(buf) - 1);
1246 struct l2cap_conn *conn = NULL;
1248 if (copy_from_user(buf, user_buffer, buf_size))
1251 buf[buf_size] = '\0';
1253 if (memcmp(buf, "connect ", 8) == 0) {
1254 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1258 mutex_lock(&set_lock);
1260 l2cap_chan_close(listen_chan, 0);
1261 l2cap_chan_put(listen_chan);
1264 mutex_unlock(&set_lock);
1267 struct lowpan_peer *peer;
1269 if (!is_bt_6lowpan(conn->hcon))
1272 peer = lookup_peer(conn);
1274 BT_DBG("6LoWPAN connection already exists");
1278 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1279 &conn->hcon->dst, conn->hcon->dst_type,
1283 ret = bt_6lowpan_connect(&addr, addr_type);
1290 if (memcmp(buf, "disconnect ", 11) == 0) {
1291 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1295 ret = bt_6lowpan_disconnect(conn, addr_type);
1305 static int lowpan_control_show(struct seq_file *f, void *ptr)
1307 struct lowpan_dev *entry;
1308 struct lowpan_peer *peer;
1310 spin_lock(&devices_lock);
1312 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1313 list_for_each_entry(peer, &entry->peers, list)
1314 seq_printf(f, "%pMR (type %u)\n",
1315 &peer->chan->dst, peer->chan->dst_type);
1318 spin_unlock(&devices_lock);
1323 static int lowpan_control_open(struct inode *inode, struct file *file)
1325 return single_open(file, lowpan_control_show, inode->i_private);
1328 static const struct file_operations lowpan_control_fops = {
1329 .open = lowpan_control_open,
1331 .write = lowpan_control_write,
1332 .llseek = seq_lseek,
1333 .release = single_release,
1336 static void disconnect_devices(void)
1338 struct lowpan_dev *entry, *tmp, *new_dev;
1339 struct list_head devices;
1341 INIT_LIST_HEAD(&devices);
1343 /* We make a separate list of devices because the unregister_netdev()
1344 * will call device_event() which will also want to modify the same
1350 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1351 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1355 new_dev->netdev = entry->netdev;
1356 INIT_LIST_HEAD(&new_dev->list);
1358 list_add_rcu(&new_dev->list, &devices);
1363 list_for_each_entry_safe(entry, tmp, &devices, list) {
1364 ifdown(entry->netdev);
1365 BT_DBG("Unregistering netdev %s %p",
1366 entry->netdev->name, entry->netdev);
1367 unregister_netdev(entry->netdev);
1372 static int device_event(struct notifier_block *unused,
1373 unsigned long event, void *ptr)
1375 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1376 struct lowpan_dev *entry;
1378 if (netdev->type != ARPHRD_6LOWPAN)
1382 case NETDEV_UNREGISTER:
1383 spin_lock(&devices_lock);
1384 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1385 if (entry->netdev == netdev) {
1386 BT_DBG("Unregistered netdev %s %p",
1387 netdev->name, netdev);
1388 list_del(&entry->list);
1392 spin_unlock(&devices_lock);
1399 static struct notifier_block bt_6lowpan_dev_notifier = {
1400 .notifier_call = device_event,
1403 static int __init bt_6lowpan_init(void)
1405 lowpan_enable_debugfs = debugfs_create_file("6lowpan_enable", 0644,
1407 &lowpan_enable_fops);
1408 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1410 &lowpan_control_fops);
1412 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1415 static void __exit bt_6lowpan_exit(void)
1417 debugfs_remove(lowpan_enable_debugfs);
1418 debugfs_remove(lowpan_control_debugfs);
1421 l2cap_chan_close(listen_chan, 0);
1422 l2cap_chan_put(listen_chan);
1425 disconnect_devices();
1427 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1430 module_init(bt_6lowpan_init);
1431 module_exit(bt_6lowpan_exit);
1433 MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1434 MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1435 MODULE_VERSION(VERSION);
1436 MODULE_LICENSE("GPL");