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/af_ieee802154.h> /* to get the address type */
26 #include <net/bluetooth/bluetooth.h>
27 #include <net/bluetooth/hci_core.h>
28 #include <net/bluetooth/l2cap.h>
30 #include <net/6lowpan.h> /* for the compression support */
34 static struct dentry *lowpan_psm_debugfs;
35 static struct dentry *lowpan_control_debugfs;
37 #define IFACE_NAME_TEMPLATE "bt%d"
38 #define EUI64_ADDR_LEN 8
42 struct l2cap_chan *chan;
45 #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
47 /* The devices list contains those devices that we are acting
48 * as a proxy. The BT 6LoWPAN device is a virtual device that
49 * connects to the Bluetooth LE device. The real connection to
50 * BT device is done via l2cap layer. There exists one
51 * virtual device / one BT 6LoWPAN network (=hciX device).
52 * The list contains struct lowpan_dev elements.
54 static LIST_HEAD(bt_6lowpan_devices);
55 static DEFINE_RWLOCK(devices_lock);
57 /* If psm is set to 0 (default value), then 6lowpan is disabled.
58 * Other values are used to indicate a Protocol Service Multiplexer
61 static u16 psm_6lowpan;
63 /* We are listening incoming connections via this channel
65 static struct l2cap_chan *listen_chan;
68 struct list_head list;
69 struct l2cap_chan *chan;
71 /* peer addresses in various formats */
72 unsigned char eui64_addr[EUI64_ADDR_LEN];
73 struct in6_addr peer_addr;
77 struct list_head list;
80 struct net_device *netdev;
81 struct list_head peers;
82 atomic_t peer_count; /* number of items in peers list */
84 struct work_struct delete_netdev;
85 struct delayed_work notify_peers;
88 static inline struct lowpan_dev *lowpan_dev(const struct net_device *netdev)
90 return netdev_priv(netdev);
93 static inline void peer_add(struct lowpan_dev *dev, struct lowpan_peer *peer)
95 list_add(&peer->list, &dev->peers);
96 atomic_inc(&dev->peer_count);
99 static inline bool peer_del(struct lowpan_dev *dev, struct lowpan_peer *peer)
101 list_del(&peer->list);
103 if (atomic_dec_and_test(&dev->peer_count)) {
111 static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_dev *dev,
112 bdaddr_t *ba, __u8 type)
114 struct lowpan_peer *peer, *tmp;
116 BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count),
119 list_for_each_entry_safe(peer, tmp, &dev->peers, list) {
120 BT_DBG("dst addr %pMR dst type %d",
121 &peer->chan->dst, peer->chan->dst_type);
123 if (bacmp(&peer->chan->dst, ba))
126 if (type == peer->chan->dst_type)
133 static inline struct lowpan_peer *peer_lookup_chan(struct lowpan_dev *dev,
134 struct l2cap_chan *chan)
136 struct lowpan_peer *peer, *tmp;
138 list_for_each_entry_safe(peer, tmp, &dev->peers, list) {
139 if (peer->chan == chan)
146 static inline struct lowpan_peer *peer_lookup_conn(struct lowpan_dev *dev,
147 struct l2cap_conn *conn)
149 struct lowpan_peer *peer, *tmp;
151 list_for_each_entry_safe(peer, tmp, &dev->peers, list) {
152 if (peer->chan->conn == conn)
159 static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
161 struct lowpan_dev *entry, *tmp;
162 struct lowpan_peer *peer = NULL;
165 read_lock_irqsave(&devices_lock, flags);
167 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
168 peer = peer_lookup_conn(entry, conn);
173 read_unlock_irqrestore(&devices_lock, flags);
178 static struct lowpan_dev *lookup_dev(struct l2cap_conn *conn)
180 struct lowpan_dev *entry, *tmp;
181 struct lowpan_dev *dev = NULL;
184 read_lock_irqsave(&devices_lock, flags);
186 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
187 if (conn->hcon->hdev == entry->hdev) {
193 read_unlock_irqrestore(&devices_lock, flags);
198 static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
200 struct sk_buff *skb_cp;
203 skb_cp = skb_copy(skb, GFP_ATOMIC);
207 ret = netif_rx(skb_cp);
209 BT_DBG("receive skb %d", ret);
216 static int process_data(struct sk_buff *skb, struct net_device *netdev,
217 struct l2cap_chan *chan)
219 const u8 *saddr, *daddr;
221 struct lowpan_dev *dev;
222 struct lowpan_peer *peer;
225 dev = lowpan_dev(netdev);
227 read_lock_irqsave(&devices_lock, flags);
228 peer = peer_lookup_chan(dev, chan);
229 read_unlock_irqrestore(&devices_lock, flags);
233 saddr = peer->eui64_addr;
234 daddr = dev->netdev->dev_addr;
236 /* at least two bytes will be used for the encoding */
240 if (lowpan_fetch_skb_u8(skb, &iphc0))
243 if (lowpan_fetch_skb_u8(skb, &iphc1))
246 return lowpan_process_data(skb, netdev,
247 saddr, IEEE802154_ADDR_LONG, EUI64_ADDR_LEN,
248 daddr, IEEE802154_ADDR_LONG, EUI64_ADDR_LEN,
249 iphc0, iphc1, give_skb_to_upper);
256 static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
257 struct l2cap_chan *chan)
259 struct sk_buff *local_skb;
262 if (!netif_running(dev))
265 if (dev->type != ARPHRD_6LOWPAN)
268 /* check that it's our buffer */
269 if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
270 /* Copy the packet so that the IPv6 header is
273 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
274 skb_tailroom(skb), GFP_ATOMIC);
278 local_skb->protocol = htons(ETH_P_IPV6);
279 local_skb->pkt_type = PACKET_HOST;
281 skb_reset_network_header(local_skb);
282 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
284 if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
285 kfree_skb(local_skb);
289 dev->stats.rx_bytes += skb->len;
290 dev->stats.rx_packets++;
292 kfree_skb(local_skb);
295 switch (skb->data[0] & 0xe0) {
296 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
297 local_skb = skb_clone(skb, GFP_ATOMIC);
301 ret = process_data(local_skb, dev, chan);
302 if (ret != NET_RX_SUCCESS)
305 dev->stats.rx_bytes += skb->len;
306 dev->stats.rx_packets++;
315 return NET_RX_SUCCESS;
318 dev->stats.rx_dropped++;
323 /* Packet from BT LE device */
324 static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
326 struct lowpan_dev *dev;
327 struct lowpan_peer *peer;
330 peer = lookup_peer(chan->conn);
334 dev = lookup_dev(chan->conn);
335 if (!dev || !dev->netdev)
338 err = recv_pkt(skb, dev->netdev, chan);
340 BT_DBG("recv pkt %d", err);
347 static u8 get_addr_type_from_eui64(u8 byte)
349 /* Is universal(0) or local(1) bit */
350 return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC);
353 static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr)
355 u8 *eui64 = ip6_daddr->s6_addr + 8;
357 addr->b[0] = eui64[7];
358 addr->b[1] = eui64[6];
359 addr->b[2] = eui64[5];
360 addr->b[3] = eui64[2];
361 addr->b[4] = eui64[1];
362 addr->b[5] = eui64[0];
365 static void convert_dest_bdaddr(struct in6_addr *ip6_daddr,
366 bdaddr_t *addr, u8 *addr_type)
368 copy_to_bdaddr(ip6_daddr, addr);
370 /* We need to toggle the U/L bit that we got from IPv6 address
371 * so that we get the proper address and type of the BD address.
375 *addr_type = get_addr_type_from_eui64(addr->b[5]);
378 static int header_create(struct sk_buff *skb, struct net_device *netdev,
379 unsigned short type, const void *_daddr,
380 const void *_saddr, unsigned int len)
383 struct lowpan_dev *dev;
384 struct lowpan_peer *peer;
385 bdaddr_t addr, *any = BDADDR_ANY;
386 u8 *saddr, *daddr = any->b;
389 if (type != ETH_P_IPV6)
394 dev = lowpan_dev(netdev);
396 if (ipv6_addr_is_multicast(&hdr->daddr)) {
397 memcpy(&lowpan_cb(skb)->addr, &hdr->daddr,
398 sizeof(struct in6_addr));
399 lowpan_cb(skb)->chan = NULL;
403 /* Get destination BT device from skb.
404 * If there is no such peer then discard the packet.
406 convert_dest_bdaddr(&hdr->daddr, &addr, &addr_type);
408 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr,
409 addr_type, &hdr->daddr);
411 read_lock_irqsave(&devices_lock, flags);
412 peer = peer_lookup_ba(dev, &addr, addr_type);
413 read_unlock_irqrestore(&devices_lock, flags);
416 BT_DBG("no such peer %pMR found", &addr);
420 daddr = peer->eui64_addr;
422 memcpy(&lowpan_cb(skb)->addr, &hdr->daddr,
423 sizeof(struct in6_addr));
424 lowpan_cb(skb)->chan = peer->chan;
427 saddr = dev->netdev->dev_addr;
429 return lowpan_header_compress(skb, netdev, type, daddr, saddr, len);
432 /* Packet to BT LE device */
433 static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
434 struct net_device *netdev)
440 /* Remember the skb so that we can send EAGAIN to the caller if
441 * we run out of credits.
445 memset(&msg, 0, sizeof(msg));
446 msg.msg_iov = (struct iovec *) &iv;
448 iv.iov_base = skb->data;
449 iv.iov_len = skb->len;
451 err = l2cap_chan_send(chan, &msg, skb->len);
453 netdev->stats.tx_bytes += err;
454 netdev->stats.tx_packets++;
459 err = lowpan_cb(skb)->status;
463 netdev->stats.tx_dropped++;
465 netdev->stats.tx_errors++;
471 static void send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
473 struct sk_buff *local_skb;
474 struct lowpan_dev *entry, *tmp;
477 read_lock_irqsave(&devices_lock, flags);
479 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
480 struct lowpan_peer *pentry, *ptmp;
481 struct lowpan_dev *dev;
483 if (entry->netdev != netdev)
486 dev = lowpan_dev(entry->netdev);
488 list_for_each_entry_safe(pentry, ptmp, &dev->peers, list) {
489 local_skb = skb_clone(skb, GFP_ATOMIC);
491 send_pkt(pentry->chan, local_skb, netdev);
493 kfree_skb(local_skb);
497 read_unlock_irqrestore(&devices_lock, flags);
500 static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
503 struct lowpan_dev *dev;
504 struct lowpan_peer *peer;
508 if (ipv6_addr_is_multicast(&lowpan_cb(skb)->addr)) {
509 /* We need to send the packet to every device
510 * behind this interface.
512 send_mcast_pkt(skb, netdev);
516 convert_dest_bdaddr(&lowpan_cb(skb)->addr, &addr, &addr_type);
517 dev = lowpan_dev(netdev);
519 read_lock_irqsave(&devices_lock, flags);
520 peer = peer_lookup_ba(dev, &addr, addr_type);
521 read_unlock_irqrestore(&devices_lock, flags);
523 BT_DBG("xmit %s to %pMR type %d IP %pI6c peer %p",
524 netdev->name, &addr, addr_type,
525 &lowpan_cb(skb)->addr, peer);
527 if (peer && peer->chan)
528 err = send_pkt(peer->chan, skb, netdev);
535 BT_DBG("ERROR: xmit failed (%d)", err);
537 return (err < 0) ? NET_XMIT_DROP : err;
540 static const struct net_device_ops netdev_ops = {
541 .ndo_start_xmit = bt_xmit,
544 static struct header_ops header_ops = {
545 .create = header_create,
548 static void netdev_setup(struct net_device *dev)
550 dev->addr_len = EUI64_ADDR_LEN;
551 dev->type = ARPHRD_6LOWPAN;
553 dev->hard_header_len = 0;
554 dev->needed_tailroom = 0;
555 dev->mtu = IPV6_MIN_MTU;
556 dev->tx_queue_len = 0;
557 dev->flags = IFF_RUNNING | IFF_POINTOPOINT;
558 dev->watchdog_timeo = 0;
560 dev->netdev_ops = &netdev_ops;
561 dev->header_ops = &header_ops;
562 dev->destructor = free_netdev;
565 static struct device_type bt_type = {
569 static void set_addr(u8 *eui, u8 *addr, u8 addr_type)
571 /* addr is the BT address in little-endian format */
581 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
582 if (addr_type == BDADDR_LE_PUBLIC)
587 BT_DBG("type %d addr %*phC", addr_type, 8, eui);
590 static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr,
593 netdev->addr_assign_type = NET_ADDR_PERM;
594 set_addr(netdev->dev_addr, addr->b, addr_type);
597 static void ifup(struct net_device *netdev)
602 err = dev_open(netdev);
604 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
608 static void do_notify_peers(struct work_struct *work)
610 struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
613 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
616 static bool is_bt_6lowpan(struct hci_conn *hcon)
618 if (hcon->type != LE_LINK)
627 static struct l2cap_chan *chan_create(void)
629 struct l2cap_chan *chan;
631 chan = l2cap_chan_create();
635 l2cap_chan_set_defaults(chan);
637 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
638 chan->mode = L2CAP_MODE_LE_FLOWCTL;
640 chan->imtu = chan->omtu;
645 static struct l2cap_chan *chan_open(struct l2cap_chan *pchan)
647 struct l2cap_chan *chan;
649 chan = chan_create();
653 chan->remote_mps = chan->omtu;
654 chan->mps = chan->omtu;
656 chan->state = BT_CONNECTED;
661 static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
662 struct lowpan_dev *dev)
664 struct lowpan_peer *peer;
667 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
672 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
675 peer->peer_addr.s6_addr[0] = 0xFE;
676 peer->peer_addr.s6_addr[1] = 0x80;
677 set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
680 memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
683 write_lock_irqsave(&devices_lock, flags);
684 INIT_LIST_HEAD(&peer->list);
686 write_unlock_irqrestore(&devices_lock, flags);
688 /* Notifying peers about us needs to be done without locks held */
689 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
690 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
695 static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
697 struct net_device *netdev;
701 netdev = alloc_netdev(sizeof(struct lowpan_dev), IFACE_NAME_TEMPLATE,
706 set_dev_addr(netdev, &chan->src, chan->src_type);
708 netdev->netdev_ops = &netdev_ops;
709 SET_NETDEV_DEV(netdev, &chan->conn->hcon->dev);
710 SET_NETDEV_DEVTYPE(netdev, &bt_type);
712 err = register_netdev(netdev);
714 BT_INFO("register_netdev failed %d", err);
719 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
720 netdev->ifindex, &chan->dst, chan->dst_type,
721 &chan->src, chan->src_type);
722 set_bit(__LINK_STATE_PRESENT, &netdev->state);
724 *dev = netdev_priv(netdev);
725 (*dev)->netdev = netdev;
726 (*dev)->hdev = chan->conn->hcon->hdev;
727 INIT_LIST_HEAD(&(*dev)->peers);
729 write_lock_irqsave(&devices_lock, flags);
730 INIT_LIST_HEAD(&(*dev)->list);
731 list_add(&(*dev)->list, &bt_6lowpan_devices);
732 write_unlock_irqrestore(&devices_lock, flags);
740 static inline void chan_ready_cb(struct l2cap_chan *chan)
742 struct lowpan_dev *dev;
744 dev = lookup_dev(chan->conn);
746 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
749 if (setup_netdev(chan, &dev) < 0) {
750 l2cap_chan_del(chan, -ENOENT);
755 add_peer_chan(chan, dev);
759 static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *chan)
761 struct l2cap_chan *pchan;
763 pchan = chan_open(chan);
764 pchan->ops = chan->ops;
766 BT_DBG("chan %p pchan %p", chan, pchan);
771 static void delete_netdev(struct work_struct *work)
773 struct lowpan_dev *entry = container_of(work, struct lowpan_dev,
776 unregister_netdev(entry->netdev);
778 /* The entry pointer is deleted in device_event() */
781 static void chan_close_cb(struct l2cap_chan *chan)
783 struct lowpan_dev *entry, *tmp;
784 struct lowpan_dev *dev = NULL;
785 struct lowpan_peer *peer;
788 bool last = false, removed = true;
790 BT_DBG("chan %p conn %p", chan, chan->conn);
792 if (chan->conn && chan->conn->hcon) {
793 if (!is_bt_6lowpan(chan->conn->hcon))
796 /* If conn is set, then the netdev is also there and we should
802 write_lock_irqsave(&devices_lock, flags);
804 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
805 dev = lowpan_dev(entry->netdev);
806 peer = peer_lookup_chan(dev, chan);
808 last = peer_del(dev, peer);
811 BT_DBG("dev %p removing %speer %p", dev,
812 last ? "last " : "1 ", peer);
813 BT_DBG("chan %p orig refcnt %d", chan,
814 atomic_read(&chan->kref.refcount));
816 l2cap_chan_put(chan);
822 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
823 write_unlock_irqrestore(&devices_lock, flags);
825 cancel_delayed_work_sync(&dev->notify_peers);
828 INIT_WORK(&entry->delete_netdev, delete_netdev);
829 schedule_work(&entry->delete_netdev);
832 write_unlock_irqrestore(&devices_lock, flags);
838 static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
840 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
841 state_to_string(state), err);
844 static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
845 unsigned long hdr_len,
846 unsigned long len, int nb)
848 /* Note that we must allocate using GFP_ATOMIC here as
849 * this function is called originally from netdev hard xmit
850 * function in atomic context.
852 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
855 static void chan_suspend_cb(struct l2cap_chan *chan)
857 struct sk_buff *skb = chan->data;
859 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
861 lowpan_cb(skb)->status = -EAGAIN;
864 static void chan_resume_cb(struct l2cap_chan *chan)
866 struct sk_buff *skb = chan->data;
868 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
870 lowpan_cb(skb)->status = 0;
873 static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
875 return msecs_to_jiffies(1000);
878 static const struct l2cap_ops bt_6lowpan_chan_ops = {
879 .name = "L2CAP 6LoWPAN channel",
880 .new_connection = chan_new_conn_cb,
881 .recv = chan_recv_cb,
882 .close = chan_close_cb,
883 .state_change = chan_state_change_cb,
884 .ready = chan_ready_cb,
885 .resume = chan_resume_cb,
886 .suspend = chan_suspend_cb,
887 .get_sndtimeo = chan_get_sndtimeo_cb,
888 .alloc_skb = chan_alloc_skb_cb,
889 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
891 .teardown = l2cap_chan_no_teardown,
892 .defer = l2cap_chan_no_defer,
893 .set_shutdown = l2cap_chan_no_set_shutdown,
896 static inline __u8 bdaddr_type(__u8 type)
898 if (type == ADDR_LE_DEV_PUBLIC)
899 return BDADDR_LE_PUBLIC;
901 return BDADDR_LE_RANDOM;
904 static struct l2cap_chan *chan_get(void)
906 struct l2cap_chan *pchan;
908 pchan = chan_create();
912 pchan->ops = &bt_6lowpan_chan_ops;
917 static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
919 struct l2cap_chan *pchan;
926 err = l2cap_chan_connect(pchan, cpu_to_le16(psm_6lowpan), 0,
929 BT_DBG("chan %p err %d", pchan, err);
931 l2cap_chan_put(pchan);
936 static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
938 struct lowpan_peer *peer;
940 BT_DBG("conn %p dst type %d", conn, dst_type);
942 peer = lookup_peer(conn);
946 BT_DBG("peer %p chan %p", peer, peer->chan);
948 l2cap_chan_close(peer->chan, ENOENT);
953 static struct l2cap_chan *bt_6lowpan_listen(void)
955 bdaddr_t *addr = BDADDR_ANY;
956 struct l2cap_chan *pchan;
959 if (psm_6lowpan == 0)
966 pchan->state = BT_LISTEN;
967 pchan->src_type = BDADDR_LE_PUBLIC;
969 BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan, pchan,
972 err = l2cap_add_psm(pchan, addr, cpu_to_le16(psm_6lowpan));
974 l2cap_chan_put(pchan);
975 BT_ERR("psm cannot be added err %d", err);
982 static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
983 struct l2cap_conn **conn)
985 struct hci_conn *hcon;
986 struct hci_dev *hdev;
987 bdaddr_t *src = BDADDR_ANY;
990 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
991 &addr->b[5], &addr->b[4], &addr->b[3],
992 &addr->b[2], &addr->b[1], &addr->b[0],
998 hdev = hci_get_route(addr, src);
1003 hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
1004 hci_dev_unlock(hdev);
1009 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1011 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1016 static void disconnect_all_peers(void)
1018 struct lowpan_dev *entry, *tmp_dev;
1019 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1020 struct list_head peers;
1021 unsigned long flags;
1023 INIT_LIST_HEAD(&peers);
1025 /* We make a separate list of peers as the close_cb() will
1026 * modify the device peers list so it is better not to mess
1027 * with the same list at the same time.
1030 read_lock_irqsave(&devices_lock, flags);
1032 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1033 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list) {
1034 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1038 new_peer->chan = peer->chan;
1039 INIT_LIST_HEAD(&new_peer->list);
1041 list_add(&new_peer->list, &peers);
1045 read_unlock_irqrestore(&devices_lock, flags);
1047 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1048 l2cap_chan_close(peer->chan, ENOENT);
1053 static int lowpan_psm_set(void *data, u64 val)
1058 if (psm == 0 || psm_6lowpan != psm)
1059 /* Disconnect existing connections if 6lowpan is
1060 * disabled (psm = 0), or if psm changes.
1062 disconnect_all_peers();
1067 l2cap_chan_close(listen_chan, 0);
1068 l2cap_chan_put(listen_chan);
1071 listen_chan = bt_6lowpan_listen();
1076 static int lowpan_psm_get(void *data, u64 *val)
1082 DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops, lowpan_psm_get,
1083 lowpan_psm_set, "%llu\n");
1085 static ssize_t lowpan_control_write(struct file *fp,
1086 const char __user *user_buffer,
1091 size_t buf_size = min(count, sizeof(buf) - 1);
1095 struct l2cap_conn *conn = NULL;
1097 if (copy_from_user(buf, user_buffer, buf_size))
1100 buf[buf_size] = '\0';
1102 if (memcmp(buf, "connect ", 8) == 0) {
1103 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1108 l2cap_chan_close(listen_chan, 0);
1109 l2cap_chan_put(listen_chan);
1114 struct lowpan_peer *peer;
1116 if (!is_bt_6lowpan(conn->hcon))
1119 peer = lookup_peer(conn);
1121 BT_DBG("6LoWPAN connection already exists");
1125 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1126 &conn->hcon->dst, conn->hcon->dst_type,
1130 ret = bt_6lowpan_connect(&addr, addr_type);
1137 if (memcmp(buf, "disconnect ", 11) == 0) {
1138 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1142 ret = bt_6lowpan_disconnect(conn, addr_type);
1152 static int lowpan_control_show(struct seq_file *f, void *ptr)
1154 struct lowpan_dev *entry, *tmp_dev;
1155 struct lowpan_peer *peer, *tmp_peer;
1156 unsigned long flags;
1158 read_lock_irqsave(&devices_lock, flags);
1160 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1161 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list)
1162 seq_printf(f, "%pMR (type %u)\n",
1163 &peer->chan->dst, peer->chan->dst_type);
1166 read_unlock_irqrestore(&devices_lock, flags);
1171 static int lowpan_control_open(struct inode *inode, struct file *file)
1173 return single_open(file, lowpan_control_show, inode->i_private);
1176 static const struct file_operations lowpan_control_fops = {
1177 .open = lowpan_control_open,
1179 .write = lowpan_control_write,
1180 .llseek = seq_lseek,
1181 .release = single_release,
1184 static int device_event(struct notifier_block *unused,
1185 unsigned long event, void *ptr)
1187 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1188 struct lowpan_dev *entry, *tmp;
1189 unsigned long flags;
1191 if (netdev->type != ARPHRD_6LOWPAN)
1195 case NETDEV_UNREGISTER:
1196 write_lock_irqsave(&devices_lock, flags);
1197 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices,
1199 if (entry->netdev == netdev) {
1200 list_del(&entry->list);
1205 write_unlock_irqrestore(&devices_lock, flags);
1212 static struct notifier_block bt_6lowpan_dev_notifier = {
1213 .notifier_call = device_event,
1216 static int __init bt_6lowpan_init(void)
1218 lowpan_psm_debugfs = debugfs_create_file("6lowpan_psm", 0644,
1221 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1223 &lowpan_control_fops);
1225 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1228 static void __exit bt_6lowpan_exit(void)
1230 debugfs_remove(lowpan_psm_debugfs);
1231 debugfs_remove(lowpan_control_debugfs);
1234 l2cap_chan_close(listen_chan, 0);
1235 l2cap_chan_put(listen_chan);
1238 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1241 module_init(bt_6lowpan_init);
1242 module_exit(bt_6lowpan_exit);
1244 MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1245 MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1246 MODULE_VERSION(VERSION);
1247 MODULE_LICENSE("GPL");