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_enable_debugfs
;
35 static struct dentry
*lowpan_control_debugfs
;
37 #define IFACE_NAME_TEMPLATE "bt%d"
38 #define EUI64_ADDR_LEN 8
43 struct l2cap_chan
*chan
;
46 #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
48 /* The devices list contains those devices that we are acting
49 * as a proxy. The BT 6LoWPAN device is a virtual device that
50 * connects to the Bluetooth LE device. The real connection to
51 * BT device is done via l2cap layer. There exists one
52 * virtual device / one BT 6LoWPAN network (=hciX device).
53 * The list contains struct lowpan_dev elements.
55 static LIST_HEAD(bt_6lowpan_devices
);
56 static DEFINE_SPINLOCK(devices_lock
);
58 static bool enable_6lowpan
;
60 /* We are listening incoming connections via this channel
62 static struct l2cap_chan
*listen_chan
;
65 struct list_head list
;
67 struct l2cap_chan
*chan
;
69 /* peer addresses in various formats */
70 unsigned char eui64_addr
[EUI64_ADDR_LEN
];
71 struct in6_addr peer_addr
;
75 struct list_head list
;
78 struct net_device
*netdev
;
79 struct list_head peers
;
80 atomic_t peer_count
; /* number of items in peers list */
82 struct work_struct delete_netdev
;
83 struct delayed_work notify_peers
;
86 static inline struct lowpan_dev
*lowpan_dev(const struct net_device
*netdev
)
88 return netdev_priv(netdev
);
91 static inline void peer_add(struct lowpan_dev
*dev
, struct lowpan_peer
*peer
)
93 list_add_rcu(&peer
->list
, &dev
->peers
);
94 atomic_inc(&dev
->peer_count
);
97 static inline bool peer_del(struct lowpan_dev
*dev
, struct lowpan_peer
*peer
)
99 list_del_rcu(&peer
->list
);
100 kfree_rcu(peer
, rcu
);
102 module_put(THIS_MODULE
);
104 if (atomic_dec_and_test(&dev
->peer_count
)) {
112 static inline struct lowpan_peer
*peer_lookup_ba(struct lowpan_dev
*dev
,
113 bdaddr_t
*ba
, __u8 type
)
115 struct lowpan_peer
*peer
;
117 BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev
->peer_count
),
122 list_for_each_entry_rcu(peer
, &dev
->peers
, list
) {
123 BT_DBG("dst addr %pMR dst type %d",
124 &peer
->chan
->dst
, peer
->chan
->dst_type
);
126 if (bacmp(&peer
->chan
->dst
, ba
))
129 if (type
== peer
->chan
->dst_type
) {
140 static inline struct lowpan_peer
*__peer_lookup_chan(struct lowpan_dev
*dev
,
141 struct l2cap_chan
*chan
)
143 struct lowpan_peer
*peer
;
145 list_for_each_entry_rcu(peer
, &dev
->peers
, list
) {
146 if (peer
->chan
== chan
)
153 static inline struct lowpan_peer
*__peer_lookup_conn(struct lowpan_dev
*dev
,
154 struct l2cap_conn
*conn
)
156 struct lowpan_peer
*peer
;
158 list_for_each_entry_rcu(peer
, &dev
->peers
, list
) {
159 if (peer
->chan
->conn
== conn
)
166 static inline struct lowpan_peer
*peer_lookup_dst(struct lowpan_dev
*dev
,
167 struct in6_addr
*daddr
,
170 struct lowpan_peer
*peer
;
171 struct in6_addr
*nexthop
;
172 struct rt6_info
*rt
= (struct rt6_info
*)skb_dst(skb
);
173 int count
= atomic_read(&dev
->peer_count
);
175 BT_DBG("peers %d addr %pI6c rt %p", count
, daddr
, rt
);
177 /* If we have multiple 6lowpan peers, then check where we should
178 * send the packet. If only one peer exists, then we can send the
183 peer
= list_first_or_null_rcu(&dev
->peers
, struct lowpan_peer
,
190 nexthop
= &lowpan_cb(skb
)->gw
;
192 if (ipv6_addr_any(nexthop
))
195 nexthop
= rt6_nexthop(rt
, daddr
);
197 /* We need to remember the address because it is needed
198 * by bt_xmit() when sending the packet. In bt_xmit(), the
199 * destination routing info is not set.
201 memcpy(&lowpan_cb(skb
)->gw
, nexthop
, sizeof(struct in6_addr
));
204 BT_DBG("gw %pI6c", nexthop
);
208 list_for_each_entry_rcu(peer
, &dev
->peers
, list
) {
209 BT_DBG("dst addr %pMR dst type %d ip %pI6c",
210 &peer
->chan
->dst
, peer
->chan
->dst_type
,
213 if (!ipv6_addr_cmp(&peer
->peer_addr
, nexthop
)) {
224 static struct lowpan_peer
*lookup_peer(struct l2cap_conn
*conn
)
226 struct lowpan_dev
*entry
;
227 struct lowpan_peer
*peer
= NULL
;
231 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
232 peer
= __peer_lookup_conn(entry
, conn
);
242 static struct lowpan_dev
*lookup_dev(struct l2cap_conn
*conn
)
244 struct lowpan_dev
*entry
;
245 struct lowpan_dev
*dev
= NULL
;
249 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
250 if (conn
->hcon
->hdev
== entry
->hdev
) {
261 static int give_skb_to_upper(struct sk_buff
*skb
, struct net_device
*dev
)
263 struct sk_buff
*skb_cp
;
265 skb_cp
= skb_copy(skb
, GFP_ATOMIC
);
269 return netif_rx(skb_cp
);
272 static int iphc_decompress(struct sk_buff
*skb
, struct net_device
*netdev
,
273 struct l2cap_chan
*chan
)
275 const u8
*saddr
, *daddr
;
277 struct lowpan_dev
*dev
;
278 struct lowpan_peer
*peer
;
280 dev
= lowpan_dev(netdev
);
283 peer
= __peer_lookup_chan(dev
, chan
);
288 saddr
= peer
->eui64_addr
;
289 daddr
= dev
->netdev
->dev_addr
;
291 /* at least two bytes will be used for the encoding */
295 if (lowpan_fetch_skb_u8(skb
, &iphc0
))
298 if (lowpan_fetch_skb_u8(skb
, &iphc1
))
301 return lowpan_header_decompress(skb
, netdev
,
302 saddr
, IEEE802154_ADDR_LONG
,
303 EUI64_ADDR_LEN
, daddr
,
304 IEEE802154_ADDR_LONG
, EUI64_ADDR_LEN
,
309 static int recv_pkt(struct sk_buff
*skb
, struct net_device
*dev
,
310 struct l2cap_chan
*chan
)
312 struct sk_buff
*local_skb
;
315 if (!netif_running(dev
))
318 if (dev
->type
!= ARPHRD_6LOWPAN
)
321 skb
= skb_share_check(skb
, GFP_ATOMIC
);
325 /* check that it's our buffer */
326 if (skb
->data
[0] == LOWPAN_DISPATCH_IPV6
) {
327 /* Copy the packet so that the IPv6 header is
330 local_skb
= skb_copy_expand(skb
, NET_SKB_PAD
- 1,
331 skb_tailroom(skb
), GFP_ATOMIC
);
335 local_skb
->protocol
= htons(ETH_P_IPV6
);
336 local_skb
->pkt_type
= PACKET_HOST
;
338 skb_reset_network_header(local_skb
);
339 skb_set_transport_header(local_skb
, sizeof(struct ipv6hdr
));
341 if (give_skb_to_upper(local_skb
, dev
) != NET_RX_SUCCESS
) {
342 kfree_skb(local_skb
);
346 dev
->stats
.rx_bytes
+= skb
->len
;
347 dev
->stats
.rx_packets
++;
349 consume_skb(local_skb
);
352 switch (skb
->data
[0] & 0xe0) {
353 case LOWPAN_DISPATCH_IPHC
: /* ipv6 datagram */
354 local_skb
= skb_clone(skb
, GFP_ATOMIC
);
358 ret
= iphc_decompress(local_skb
, dev
, chan
);
360 kfree_skb(local_skb
);
364 local_skb
->protocol
= htons(ETH_P_IPV6
);
365 local_skb
->pkt_type
= PACKET_HOST
;
366 local_skb
->dev
= dev
;
368 if (give_skb_to_upper(local_skb
, dev
)
370 kfree_skb(local_skb
);
374 dev
->stats
.rx_bytes
+= skb
->len
;
375 dev
->stats
.rx_packets
++;
377 consume_skb(local_skb
);
385 return NET_RX_SUCCESS
;
388 dev
->stats
.rx_dropped
++;
392 /* Packet from BT LE device */
393 static int chan_recv_cb(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
395 struct lowpan_dev
*dev
;
396 struct lowpan_peer
*peer
;
399 peer
= lookup_peer(chan
->conn
);
403 dev
= lookup_dev(chan
->conn
);
404 if (!dev
|| !dev
->netdev
)
407 err
= recv_pkt(skb
, dev
->netdev
, chan
);
409 BT_DBG("recv pkt %d", err
);
416 static u8
get_addr_type_from_eui64(u8 byte
)
418 /* Is universal(0) or local(1) bit */
419 return ((byte
& 0x02) ? BDADDR_LE_RANDOM
: BDADDR_LE_PUBLIC
);
422 static void copy_to_bdaddr(struct in6_addr
*ip6_daddr
, bdaddr_t
*addr
)
424 u8
*eui64
= ip6_daddr
->s6_addr
+ 8;
426 addr
->b
[0] = eui64
[7];
427 addr
->b
[1] = eui64
[6];
428 addr
->b
[2] = eui64
[5];
429 addr
->b
[3] = eui64
[2];
430 addr
->b
[4] = eui64
[1];
431 addr
->b
[5] = eui64
[0];
434 static void convert_dest_bdaddr(struct in6_addr
*ip6_daddr
,
435 bdaddr_t
*addr
, u8
*addr_type
)
437 copy_to_bdaddr(ip6_daddr
, addr
);
439 /* We need to toggle the U/L bit that we got from IPv6 address
440 * so that we get the proper address and type of the BD address.
444 *addr_type
= get_addr_type_from_eui64(addr
->b
[5]);
447 static int setup_header(struct sk_buff
*skb
, struct net_device
*netdev
,
448 bdaddr_t
*peer_addr
, u8
*peer_addr_type
)
450 struct in6_addr ipv6_daddr
;
451 struct lowpan_dev
*dev
;
452 struct lowpan_peer
*peer
;
453 bdaddr_t addr
, *any
= BDADDR_ANY
;
457 dev
= lowpan_dev(netdev
);
459 memcpy(&ipv6_daddr
, &lowpan_cb(skb
)->addr
, sizeof(ipv6_daddr
));
461 if (ipv6_addr_is_multicast(&ipv6_daddr
)) {
462 lowpan_cb(skb
)->chan
= NULL
;
466 /* Get destination BT device from skb.
467 * If there is no such peer then discard the packet.
469 convert_dest_bdaddr(&ipv6_daddr
, &addr
, &addr_type
);
471 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr
,
472 addr_type
, &ipv6_daddr
);
474 peer
= peer_lookup_ba(dev
, &addr
, addr_type
);
476 /* The packet might be sent to 6lowpan interface
477 * because of routing (either via default route
478 * or user set route) so get peer according to
479 * the destination address.
481 peer
= peer_lookup_dst(dev
, &ipv6_daddr
, skb
);
483 BT_DBG("no such peer %pMR found", &addr
);
488 daddr
= peer
->eui64_addr
;
490 *peer_addr_type
= addr_type
;
491 lowpan_cb(skb
)->chan
= peer
->chan
;
496 lowpan_header_compress(skb
, netdev
, ETH_P_IPV6
, daddr
,
497 dev
->netdev
->dev_addr
, skb
->len
);
499 err
= dev_hard_header(skb
, netdev
, ETH_P_IPV6
, NULL
, NULL
, 0);
506 static int header_create(struct sk_buff
*skb
, struct net_device
*netdev
,
507 unsigned short type
, const void *_daddr
,
508 const void *_saddr
, unsigned int len
)
512 if (type
!= ETH_P_IPV6
)
517 memcpy(&lowpan_cb(skb
)->addr
, &hdr
->daddr
, sizeof(struct in6_addr
));
522 /* Packet to BT LE device */
523 static int send_pkt(struct l2cap_chan
*chan
, struct sk_buff
*skb
,
524 struct net_device
*netdev
)
530 /* Remember the skb so that we can send EAGAIN to the caller if
531 * we run out of credits.
535 iv
.iov_base
= skb
->data
;
536 iv
.iov_len
= skb
->len
;
538 memset(&msg
, 0, sizeof(msg
));
539 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, &iv
, 1, skb
->len
);
541 err
= l2cap_chan_send(chan
, &msg
, skb
->len
);
543 netdev
->stats
.tx_bytes
+= err
;
544 netdev
->stats
.tx_packets
++;
549 err
= lowpan_cb(skb
)->status
;
553 netdev
->stats
.tx_dropped
++;
555 netdev
->stats
.tx_errors
++;
561 static int send_mcast_pkt(struct sk_buff
*skb
, struct net_device
*netdev
)
563 struct sk_buff
*local_skb
;
564 struct lowpan_dev
*entry
;
569 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
570 struct lowpan_peer
*pentry
;
571 struct lowpan_dev
*dev
;
573 if (entry
->netdev
!= netdev
)
576 dev
= lowpan_dev(entry
->netdev
);
578 list_for_each_entry_rcu(pentry
, &dev
->peers
, list
) {
581 local_skb
= skb_clone(skb
, GFP_ATOMIC
);
583 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
585 &pentry
->chan
->dst
, pentry
->chan
->dst_type
,
586 &pentry
->peer_addr
, pentry
->chan
);
587 ret
= send_pkt(pentry
->chan
, local_skb
, netdev
);
591 kfree_skb(local_skb
);
600 static netdev_tx_t
bt_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
606 /* We must take a copy of the skb before we modify/replace the ipv6
607 * header as the header could be used elsewhere
609 skb
= skb_unshare(skb
, GFP_ATOMIC
);
611 return NET_XMIT_DROP
;
613 /* Return values from setup_header()
614 * <0 - error, packet is dropped
615 * 0 - this is a multicast packet
616 * 1 - this is unicast packet
618 err
= setup_header(skb
, netdev
, &addr
, &addr_type
);
621 return NET_XMIT_DROP
;
625 if (lowpan_cb(skb
)->chan
) {
626 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
627 netdev
->name
, &addr
, addr_type
,
628 &lowpan_cb(skb
)->addr
, lowpan_cb(skb
)->chan
);
629 err
= send_pkt(lowpan_cb(skb
)->chan
, skb
, netdev
);
634 /* We need to send the packet to every device behind this
637 err
= send_mcast_pkt(skb
, netdev
);
643 BT_DBG("ERROR: xmit failed (%d)", err
);
645 return err
< 0 ? NET_XMIT_DROP
: err
;
648 static struct lock_class_key bt_tx_busylock
;
649 static struct lock_class_key bt_netdev_xmit_lock_key
;
651 static void bt_set_lockdep_class_one(struct net_device
*dev
,
652 struct netdev_queue
*txq
,
655 lockdep_set_class(&txq
->_xmit_lock
, &bt_netdev_xmit_lock_key
);
658 static int bt_dev_init(struct net_device
*dev
)
660 netdev_for_each_tx_queue(dev
, bt_set_lockdep_class_one
, NULL
);
661 dev
->qdisc_tx_busylock
= &bt_tx_busylock
;
666 static const struct net_device_ops netdev_ops
= {
667 .ndo_init
= bt_dev_init
,
668 .ndo_start_xmit
= bt_xmit
,
671 static struct header_ops header_ops
= {
672 .create
= header_create
,
675 static void netdev_setup(struct net_device
*dev
)
677 dev
->addr_len
= EUI64_ADDR_LEN
;
678 dev
->type
= ARPHRD_6LOWPAN
;
680 dev
->hard_header_len
= 0;
681 dev
->needed_tailroom
= 0;
682 dev
->mtu
= IPV6_MIN_MTU
;
683 dev
->tx_queue_len
= 0;
684 dev
->flags
= IFF_RUNNING
| IFF_POINTOPOINT
|
686 dev
->watchdog_timeo
= 0;
688 dev
->netdev_ops
= &netdev_ops
;
689 dev
->header_ops
= &header_ops
;
690 dev
->destructor
= free_netdev
;
693 static struct device_type bt_type
= {
697 static void set_addr(u8
*eui
, u8
*addr
, u8 addr_type
)
699 /* addr is the BT address in little-endian format */
709 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
710 if (addr_type
== BDADDR_LE_PUBLIC
)
715 BT_DBG("type %d addr %*phC", addr_type
, 8, eui
);
718 static void set_dev_addr(struct net_device
*netdev
, bdaddr_t
*addr
,
721 netdev
->addr_assign_type
= NET_ADDR_PERM
;
722 set_addr(netdev
->dev_addr
, addr
->b
, addr_type
);
725 static void ifup(struct net_device
*netdev
)
730 err
= dev_open(netdev
);
732 BT_INFO("iface %s cannot be opened (%d)", netdev
->name
, err
);
736 static void ifdown(struct net_device
*netdev
)
741 err
= dev_close(netdev
);
743 BT_INFO("iface %s cannot be closed (%d)", netdev
->name
, err
);
747 static void do_notify_peers(struct work_struct
*work
)
749 struct lowpan_dev
*dev
= container_of(work
, struct lowpan_dev
,
752 netdev_notify_peers(dev
->netdev
); /* send neighbour adv at startup */
755 static bool is_bt_6lowpan(struct hci_conn
*hcon
)
757 if (hcon
->type
!= LE_LINK
)
766 static struct l2cap_chan
*chan_create(void)
768 struct l2cap_chan
*chan
;
770 chan
= l2cap_chan_create();
774 l2cap_chan_set_defaults(chan
);
776 chan
->chan_type
= L2CAP_CHAN_CONN_ORIENTED
;
777 chan
->mode
= L2CAP_MODE_LE_FLOWCTL
;
779 chan
->imtu
= chan
->omtu
;
784 static struct l2cap_chan
*chan_open(struct l2cap_chan
*pchan
)
786 struct l2cap_chan
*chan
;
788 chan
= chan_create();
792 chan
->remote_mps
= chan
->omtu
;
793 chan
->mps
= chan
->omtu
;
795 chan
->state
= BT_CONNECTED
;
800 static void set_ip_addr_bits(u8 addr_type
, u8
*addr
)
802 if (addr_type
== BDADDR_LE_PUBLIC
)
808 static struct l2cap_chan
*add_peer_chan(struct l2cap_chan
*chan
,
809 struct lowpan_dev
*dev
)
811 struct lowpan_peer
*peer
;
813 peer
= kzalloc(sizeof(*peer
), GFP_ATOMIC
);
818 memset(&peer
->peer_addr
, 0, sizeof(struct in6_addr
));
821 peer
->peer_addr
.s6_addr
[0] = 0xFE;
822 peer
->peer_addr
.s6_addr
[1] = 0x80;
823 set_addr((u8
*)&peer
->peer_addr
.s6_addr
+ 8, chan
->dst
.b
,
826 memcpy(&peer
->eui64_addr
, (u8
*)&peer
->peer_addr
.s6_addr
+ 8,
829 /* IPv6 address needs to have the U/L bit set properly so toggle
832 set_ip_addr_bits(chan
->dst_type
, (u8
*)&peer
->peer_addr
.s6_addr
+ 8);
834 spin_lock(&devices_lock
);
835 INIT_LIST_HEAD(&peer
->list
);
837 spin_unlock(&devices_lock
);
839 /* Notifying peers about us needs to be done without locks held */
840 INIT_DELAYED_WORK(&dev
->notify_peers
, do_notify_peers
);
841 schedule_delayed_work(&dev
->notify_peers
, msecs_to_jiffies(100));
846 static int setup_netdev(struct l2cap_chan
*chan
, struct lowpan_dev
**dev
)
848 struct net_device
*netdev
;
851 netdev
= alloc_netdev(sizeof(struct lowpan_dev
), IFACE_NAME_TEMPLATE
,
852 NET_NAME_UNKNOWN
, netdev_setup
);
856 set_dev_addr(netdev
, &chan
->src
, chan
->src_type
);
858 netdev
->netdev_ops
= &netdev_ops
;
859 SET_NETDEV_DEV(netdev
, &chan
->conn
->hcon
->hdev
->dev
);
860 SET_NETDEV_DEVTYPE(netdev
, &bt_type
);
862 err
= register_netdev(netdev
);
864 BT_INFO("register_netdev failed %d", err
);
869 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
870 netdev
->ifindex
, &chan
->dst
, chan
->dst_type
,
871 &chan
->src
, chan
->src_type
);
872 set_bit(__LINK_STATE_PRESENT
, &netdev
->state
);
874 *dev
= netdev_priv(netdev
);
875 (*dev
)->netdev
= netdev
;
876 (*dev
)->hdev
= chan
->conn
->hcon
->hdev
;
877 INIT_LIST_HEAD(&(*dev
)->peers
);
879 spin_lock(&devices_lock
);
880 INIT_LIST_HEAD(&(*dev
)->list
);
881 list_add_rcu(&(*dev
)->list
, &bt_6lowpan_devices
);
882 spin_unlock(&devices_lock
);
890 static inline void chan_ready_cb(struct l2cap_chan
*chan
)
892 struct lowpan_dev
*dev
;
894 dev
= lookup_dev(chan
->conn
);
896 BT_DBG("chan %p conn %p dev %p", chan
, chan
->conn
, dev
);
899 if (setup_netdev(chan
, &dev
) < 0) {
900 l2cap_chan_del(chan
, -ENOENT
);
905 if (!try_module_get(THIS_MODULE
))
908 add_peer_chan(chan
, dev
);
912 static inline struct l2cap_chan
*chan_new_conn_cb(struct l2cap_chan
*pchan
)
914 struct l2cap_chan
*chan
;
916 chan
= chan_open(pchan
);
917 chan
->ops
= pchan
->ops
;
919 BT_DBG("chan %p pchan %p", chan
, pchan
);
924 static void delete_netdev(struct work_struct
*work
)
926 struct lowpan_dev
*entry
= container_of(work
, struct lowpan_dev
,
929 unregister_netdev(entry
->netdev
);
931 /* The entry pointer is deleted by the netdev destructor. */
934 static void chan_close_cb(struct l2cap_chan
*chan
)
936 struct lowpan_dev
*entry
;
937 struct lowpan_dev
*dev
= NULL
;
938 struct lowpan_peer
*peer
;
940 bool last
= false, remove
= true;
942 BT_DBG("chan %p conn %p", chan
, chan
->conn
);
944 if (chan
->conn
&& chan
->conn
->hcon
) {
945 if (!is_bt_6lowpan(chan
->conn
->hcon
))
948 /* If conn is set, then the netdev is also there and we should
954 spin_lock(&devices_lock
);
956 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
957 dev
= lowpan_dev(entry
->netdev
);
958 peer
= __peer_lookup_chan(dev
, chan
);
960 last
= peer_del(dev
, peer
);
963 BT_DBG("dev %p removing %speer %p", dev
,
964 last
? "last " : "1 ", peer
);
965 BT_DBG("chan %p orig refcnt %d", chan
,
966 atomic_read(&chan
->kref
.refcount
));
968 l2cap_chan_put(chan
);
973 if (!err
&& last
&& dev
&& !atomic_read(&dev
->peer_count
)) {
974 spin_unlock(&devices_lock
);
976 cancel_delayed_work_sync(&dev
->notify_peers
);
981 INIT_WORK(&entry
->delete_netdev
, delete_netdev
);
982 schedule_work(&entry
->delete_netdev
);
985 spin_unlock(&devices_lock
);
991 static void chan_state_change_cb(struct l2cap_chan
*chan
, int state
, int err
)
993 BT_DBG("chan %p conn %p state %s err %d", chan
, chan
->conn
,
994 state_to_string(state
), err
);
997 static struct sk_buff
*chan_alloc_skb_cb(struct l2cap_chan
*chan
,
998 unsigned long hdr_len
,
999 unsigned long len
, int nb
)
1001 /* Note that we must allocate using GFP_ATOMIC here as
1002 * this function is called originally from netdev hard xmit
1003 * function in atomic context.
1005 return bt_skb_alloc(hdr_len
+ len
, GFP_ATOMIC
);
1008 static void chan_suspend_cb(struct l2cap_chan
*chan
)
1010 struct sk_buff
*skb
= chan
->data
;
1012 BT_DBG("chan %p conn %p skb %p", chan
, chan
->conn
, skb
);
1017 lowpan_cb(skb
)->status
= -EAGAIN
;
1020 static void chan_resume_cb(struct l2cap_chan
*chan
)
1022 struct sk_buff
*skb
= chan
->data
;
1024 BT_DBG("chan %p conn %p skb %p", chan
, chan
->conn
, skb
);
1029 lowpan_cb(skb
)->status
= 0;
1032 static long chan_get_sndtimeo_cb(struct l2cap_chan
*chan
)
1034 return L2CAP_CONN_TIMEOUT
;
1037 static const struct l2cap_ops bt_6lowpan_chan_ops
= {
1038 .name
= "L2CAP 6LoWPAN channel",
1039 .new_connection
= chan_new_conn_cb
,
1040 .recv
= chan_recv_cb
,
1041 .close
= chan_close_cb
,
1042 .state_change
= chan_state_change_cb
,
1043 .ready
= chan_ready_cb
,
1044 .resume
= chan_resume_cb
,
1045 .suspend
= chan_suspend_cb
,
1046 .get_sndtimeo
= chan_get_sndtimeo_cb
,
1047 .alloc_skb
= chan_alloc_skb_cb
,
1049 .teardown
= l2cap_chan_no_teardown
,
1050 .defer
= l2cap_chan_no_defer
,
1051 .set_shutdown
= l2cap_chan_no_set_shutdown
,
1054 static inline __u8
bdaddr_type(__u8 type
)
1056 if (type
== ADDR_LE_DEV_PUBLIC
)
1057 return BDADDR_LE_PUBLIC
;
1059 return BDADDR_LE_RANDOM
;
1062 static struct l2cap_chan
*chan_get(void)
1064 struct l2cap_chan
*pchan
;
1066 pchan
= chan_create();
1070 pchan
->ops
= &bt_6lowpan_chan_ops
;
1075 static int bt_6lowpan_connect(bdaddr_t
*addr
, u8 dst_type
)
1077 struct l2cap_chan
*pchan
;
1084 err
= l2cap_chan_connect(pchan
, cpu_to_le16(L2CAP_PSM_IPSP
), 0,
1087 BT_DBG("chan %p err %d", pchan
, err
);
1089 l2cap_chan_put(pchan
);
1094 static int bt_6lowpan_disconnect(struct l2cap_conn
*conn
, u8 dst_type
)
1096 struct lowpan_peer
*peer
;
1098 BT_DBG("conn %p dst type %d", conn
, dst_type
);
1100 peer
= lookup_peer(conn
);
1104 BT_DBG("peer %p chan %p", peer
, peer
->chan
);
1106 l2cap_chan_close(peer
->chan
, ENOENT
);
1111 static struct l2cap_chan
*bt_6lowpan_listen(void)
1113 bdaddr_t
*addr
= BDADDR_ANY
;
1114 struct l2cap_chan
*pchan
;
1117 if (!enable_6lowpan
)
1124 pchan
->state
= BT_LISTEN
;
1125 pchan
->src_type
= BDADDR_LE_PUBLIC
;
1127 atomic_set(&pchan
->nesting
, L2CAP_NESTING_PARENT
);
1129 BT_DBG("chan %p src type %d", pchan
, pchan
->src_type
);
1131 err
= l2cap_add_psm(pchan
, addr
, cpu_to_le16(L2CAP_PSM_IPSP
));
1133 l2cap_chan_put(pchan
);
1134 BT_ERR("psm cannot be added err %d", err
);
1141 static int get_l2cap_conn(char *buf
, bdaddr_t
*addr
, u8
*addr_type
,
1142 struct l2cap_conn
**conn
)
1144 struct hci_conn
*hcon
;
1145 struct hci_dev
*hdev
;
1146 bdaddr_t
*src
= BDADDR_ANY
;
1149 n
= sscanf(buf
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1150 &addr
->b
[5], &addr
->b
[4], &addr
->b
[3],
1151 &addr
->b
[2], &addr
->b
[1], &addr
->b
[0],
1157 hdev
= hci_get_route(addr
, src
);
1162 hcon
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
, addr
);
1163 hci_dev_unlock(hdev
);
1168 *conn
= (struct l2cap_conn
*)hcon
->l2cap_data
;
1170 BT_DBG("conn %p dst %pMR type %d", *conn
, &hcon
->dst
, hcon
->dst_type
);
1175 static void disconnect_all_peers(void)
1177 struct lowpan_dev
*entry
;
1178 struct lowpan_peer
*peer
, *tmp_peer
, *new_peer
;
1179 struct list_head peers
;
1181 INIT_LIST_HEAD(&peers
);
1183 /* We make a separate list of peers as the close_cb() will
1184 * modify the device peers list so it is better not to mess
1185 * with the same list at the same time.
1190 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
1191 list_for_each_entry_rcu(peer
, &entry
->peers
, list
) {
1192 new_peer
= kmalloc(sizeof(*new_peer
), GFP_ATOMIC
);
1196 new_peer
->chan
= peer
->chan
;
1197 INIT_LIST_HEAD(&new_peer
->list
);
1199 list_add(&new_peer
->list
, &peers
);
1205 spin_lock(&devices_lock
);
1206 list_for_each_entry_safe(peer
, tmp_peer
, &peers
, list
) {
1207 l2cap_chan_close(peer
->chan
, ENOENT
);
1209 list_del_rcu(&peer
->list
);
1210 kfree_rcu(peer
, rcu
);
1212 spin_unlock(&devices_lock
);
1216 struct work_struct work
;
1220 static void do_enable_set(struct work_struct
*work
)
1222 struct set_enable
*set_enable
= container_of(work
,
1223 struct set_enable
, work
);
1225 if (!set_enable
->flag
|| enable_6lowpan
!= set_enable
->flag
)
1226 /* Disconnect existing connections if 6lowpan is
1229 disconnect_all_peers();
1231 enable_6lowpan
= set_enable
->flag
;
1234 l2cap_chan_close(listen_chan
, 0);
1235 l2cap_chan_put(listen_chan
);
1238 listen_chan
= bt_6lowpan_listen();
1243 static int lowpan_enable_set(void *data
, u64 val
)
1245 struct set_enable
*set_enable
;
1247 set_enable
= kzalloc(sizeof(*set_enable
), GFP_KERNEL
);
1251 set_enable
->flag
= !!val
;
1252 INIT_WORK(&set_enable
->work
, do_enable_set
);
1254 schedule_work(&set_enable
->work
);
1259 static int lowpan_enable_get(void *data
, u64
*val
)
1261 *val
= enable_6lowpan
;
1265 DEFINE_SIMPLE_ATTRIBUTE(lowpan_enable_fops
, lowpan_enable_get
,
1266 lowpan_enable_set
, "%llu\n");
1268 static ssize_t
lowpan_control_write(struct file
*fp
,
1269 const char __user
*user_buffer
,
1274 size_t buf_size
= min(count
, sizeof(buf
) - 1);
1278 struct l2cap_conn
*conn
= NULL
;
1280 if (copy_from_user(buf
, user_buffer
, buf_size
))
1283 buf
[buf_size
] = '\0';
1285 if (memcmp(buf
, "connect ", 8) == 0) {
1286 ret
= get_l2cap_conn(&buf
[8], &addr
, &addr_type
, &conn
);
1291 l2cap_chan_close(listen_chan
, 0);
1292 l2cap_chan_put(listen_chan
);
1297 struct lowpan_peer
*peer
;
1299 if (!is_bt_6lowpan(conn
->hcon
))
1302 peer
= lookup_peer(conn
);
1304 BT_DBG("6LoWPAN connection already exists");
1308 BT_DBG("conn %p dst %pMR type %d user %d", conn
,
1309 &conn
->hcon
->dst
, conn
->hcon
->dst_type
,
1313 ret
= bt_6lowpan_connect(&addr
, addr_type
);
1320 if (memcmp(buf
, "disconnect ", 11) == 0) {
1321 ret
= get_l2cap_conn(&buf
[11], &addr
, &addr_type
, &conn
);
1325 ret
= bt_6lowpan_disconnect(conn
, addr_type
);
1335 static int lowpan_control_show(struct seq_file
*f
, void *ptr
)
1337 struct lowpan_dev
*entry
;
1338 struct lowpan_peer
*peer
;
1340 spin_lock(&devices_lock
);
1342 list_for_each_entry(entry
, &bt_6lowpan_devices
, list
) {
1343 list_for_each_entry(peer
, &entry
->peers
, list
)
1344 seq_printf(f
, "%pMR (type %u)\n",
1345 &peer
->chan
->dst
, peer
->chan
->dst_type
);
1348 spin_unlock(&devices_lock
);
1353 static int lowpan_control_open(struct inode
*inode
, struct file
*file
)
1355 return single_open(file
, lowpan_control_show
, inode
->i_private
);
1358 static const struct file_operations lowpan_control_fops
= {
1359 .open
= lowpan_control_open
,
1361 .write
= lowpan_control_write
,
1362 .llseek
= seq_lseek
,
1363 .release
= single_release
,
1366 static void disconnect_devices(void)
1368 struct lowpan_dev
*entry
, *tmp
, *new_dev
;
1369 struct list_head devices
;
1371 INIT_LIST_HEAD(&devices
);
1373 /* We make a separate list of devices because the unregister_netdev()
1374 * will call device_event() which will also want to modify the same
1380 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
1381 new_dev
= kmalloc(sizeof(*new_dev
), GFP_ATOMIC
);
1385 new_dev
->netdev
= entry
->netdev
;
1386 INIT_LIST_HEAD(&new_dev
->list
);
1388 list_add_rcu(&new_dev
->list
, &devices
);
1393 list_for_each_entry_safe(entry
, tmp
, &devices
, list
) {
1394 ifdown(entry
->netdev
);
1395 BT_DBG("Unregistering netdev %s %p",
1396 entry
->netdev
->name
, entry
->netdev
);
1397 unregister_netdev(entry
->netdev
);
1402 static int device_event(struct notifier_block
*unused
,
1403 unsigned long event
, void *ptr
)
1405 struct net_device
*netdev
= netdev_notifier_info_to_dev(ptr
);
1406 struct lowpan_dev
*entry
;
1408 if (netdev
->type
!= ARPHRD_6LOWPAN
)
1412 case NETDEV_UNREGISTER
:
1413 spin_lock(&devices_lock
);
1414 list_for_each_entry(entry
, &bt_6lowpan_devices
, list
) {
1415 if (entry
->netdev
== netdev
) {
1416 BT_DBG("Unregistered netdev %s %p",
1417 netdev
->name
, netdev
);
1418 list_del(&entry
->list
);
1422 spin_unlock(&devices_lock
);
1429 static struct notifier_block bt_6lowpan_dev_notifier
= {
1430 .notifier_call
= device_event
,
1433 static int __init
bt_6lowpan_init(void)
1435 lowpan_enable_debugfs
= debugfs_create_file("6lowpan_enable", 0644,
1437 &lowpan_enable_fops
);
1438 lowpan_control_debugfs
= debugfs_create_file("6lowpan_control", 0644,
1440 &lowpan_control_fops
);
1442 return register_netdevice_notifier(&bt_6lowpan_dev_notifier
);
1445 static void __exit
bt_6lowpan_exit(void)
1447 debugfs_remove(lowpan_enable_debugfs
);
1448 debugfs_remove(lowpan_control_debugfs
);
1451 l2cap_chan_close(listen_chan
, 0);
1452 l2cap_chan_put(listen_chan
);
1455 disconnect_devices();
1457 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier
);
1460 module_init(bt_6lowpan_init
);
1461 module_exit(bt_6lowpan_exit
);
1463 MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1464 MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1465 MODULE_VERSION(VERSION
);
1466 MODULE_LICENSE("GPL");