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
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 /* If psm is set to 0 (default value), then 6lowpan is disabled.
59 * Other values are used to indicate a Protocol Service Multiplexer
62 static u16 psm_6lowpan
;
64 /* We are listening incoming connections via this channel
66 static struct l2cap_chan
*listen_chan
;
69 struct list_head list
;
71 struct l2cap_chan
*chan
;
73 /* peer addresses in various formats */
74 unsigned char eui64_addr
[EUI64_ADDR_LEN
];
75 struct in6_addr peer_addr
;
79 struct list_head list
;
82 struct net_device
*netdev
;
83 struct list_head peers
;
84 atomic_t peer_count
; /* number of items in peers list */
86 struct work_struct delete_netdev
;
87 struct delayed_work notify_peers
;
90 static inline struct lowpan_dev
*lowpan_dev(const struct net_device
*netdev
)
92 return netdev_priv(netdev
);
95 static inline void peer_add(struct lowpan_dev
*dev
, struct lowpan_peer
*peer
)
97 list_add_rcu(&peer
->list
, &dev
->peers
);
98 atomic_inc(&dev
->peer_count
);
101 static inline bool peer_del(struct lowpan_dev
*dev
, struct lowpan_peer
*peer
)
103 list_del_rcu(&peer
->list
);
104 kfree_rcu(peer
, rcu
);
106 module_put(THIS_MODULE
);
108 if (atomic_dec_and_test(&dev
->peer_count
)) {
116 static inline struct lowpan_peer
*peer_lookup_ba(struct lowpan_dev
*dev
,
117 bdaddr_t
*ba
, __u8 type
)
119 struct lowpan_peer
*peer
;
121 BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev
->peer_count
),
126 list_for_each_entry_rcu(peer
, &dev
->peers
, list
) {
127 BT_DBG("dst addr %pMR dst type %d",
128 &peer
->chan
->dst
, peer
->chan
->dst_type
);
130 if (bacmp(&peer
->chan
->dst
, ba
))
133 if (type
== peer
->chan
->dst_type
) {
144 static inline struct lowpan_peer
*__peer_lookup_chan(struct lowpan_dev
*dev
,
145 struct l2cap_chan
*chan
)
147 struct lowpan_peer
*peer
;
149 list_for_each_entry_rcu(peer
, &dev
->peers
, list
) {
150 if (peer
->chan
== chan
)
157 static inline struct lowpan_peer
*__peer_lookup_conn(struct lowpan_dev
*dev
,
158 struct l2cap_conn
*conn
)
160 struct lowpan_peer
*peer
;
162 list_for_each_entry_rcu(peer
, &dev
->peers
, list
) {
163 if (peer
->chan
->conn
== conn
)
170 static inline struct lowpan_peer
*peer_lookup_dst(struct lowpan_dev
*dev
,
171 struct in6_addr
*daddr
,
174 struct lowpan_peer
*peer
;
175 struct in6_addr
*nexthop
;
176 struct rt6_info
*rt
= (struct rt6_info
*)skb_dst(skb
);
177 int count
= atomic_read(&dev
->peer_count
);
179 BT_DBG("peers %d addr %pI6c rt %p", count
, daddr
, rt
);
181 /* If we have multiple 6lowpan peers, then check where we should
182 * send the packet. If only one peer exists, then we can send the
187 peer
= list_first_or_null_rcu(&dev
->peers
, struct lowpan_peer
,
194 nexthop
= &lowpan_cb(skb
)->gw
;
196 if (ipv6_addr_any(nexthop
))
199 nexthop
= rt6_nexthop(rt
);
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(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
;
281 struct lowpan_dev
*dev
;
282 struct lowpan_peer
*peer
;
284 dev
= lowpan_dev(netdev
);
287 peer
= __peer_lookup_chan(dev
, chan
);
292 saddr
= peer
->eui64_addr
;
293 daddr
= dev
->netdev
->dev_addr
;
295 /* at least two bytes will be used for the encoding */
299 if (lowpan_fetch_skb_u8(skb
, &iphc0
))
302 if (lowpan_fetch_skb_u8(skb
, &iphc1
))
305 return lowpan_header_decompress(skb
, netdev
,
306 saddr
, IEEE802154_ADDR_LONG
,
307 EUI64_ADDR_LEN
, daddr
,
308 IEEE802154_ADDR_LONG
, EUI64_ADDR_LEN
,
313 static int recv_pkt(struct sk_buff
*skb
, struct net_device
*dev
,
314 struct l2cap_chan
*chan
)
316 struct sk_buff
*local_skb
;
319 if (!netif_running(dev
))
322 if (dev
->type
!= ARPHRD_6LOWPAN
)
325 skb
= skb_share_check(skb
, GFP_ATOMIC
);
329 /* check that it's our buffer */
330 if (skb
->data
[0] == LOWPAN_DISPATCH_IPV6
) {
331 /* Copy the packet so that the IPv6 header is
334 local_skb
= skb_copy_expand(skb
, NET_SKB_PAD
- 1,
335 skb_tailroom(skb
), GFP_ATOMIC
);
339 local_skb
->protocol
= htons(ETH_P_IPV6
);
340 local_skb
->pkt_type
= PACKET_HOST
;
342 skb_reset_network_header(local_skb
);
343 skb_set_transport_header(local_skb
, sizeof(struct ipv6hdr
));
345 if (give_skb_to_upper(local_skb
, dev
) != NET_RX_SUCCESS
) {
346 kfree_skb(local_skb
);
350 dev
->stats
.rx_bytes
+= skb
->len
;
351 dev
->stats
.rx_packets
++;
353 consume_skb(local_skb
);
356 switch (skb
->data
[0] & 0xe0) {
357 case LOWPAN_DISPATCH_IPHC
: /* ipv6 datagram */
358 local_skb
= skb_clone(skb
, GFP_ATOMIC
);
362 ret
= iphc_decompress(local_skb
, dev
, chan
);
364 kfree_skb(local_skb
);
368 local_skb
->protocol
= htons(ETH_P_IPV6
);
369 local_skb
->pkt_type
= PACKET_HOST
;
370 local_skb
->dev
= dev
;
372 if (give_skb_to_upper(local_skb
, dev
)
374 kfree_skb(local_skb
);
378 dev
->stats
.rx_bytes
+= skb
->len
;
379 dev
->stats
.rx_packets
++;
381 consume_skb(local_skb
);
389 return NET_RX_SUCCESS
;
392 dev
->stats
.rx_dropped
++;
396 /* Packet from BT LE device */
397 static int chan_recv_cb(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
399 struct lowpan_dev
*dev
;
400 struct lowpan_peer
*peer
;
403 peer
= lookup_peer(chan
->conn
);
407 dev
= lookup_dev(chan
->conn
);
408 if (!dev
|| !dev
->netdev
)
411 err
= recv_pkt(skb
, dev
->netdev
, chan
);
413 BT_DBG("recv pkt %d", err
);
420 static u8
get_addr_type_from_eui64(u8 byte
)
422 /* Is universal(0) or local(1) bit */
423 return ((byte
& 0x02) ? BDADDR_LE_RANDOM
: BDADDR_LE_PUBLIC
);
426 static void copy_to_bdaddr(struct in6_addr
*ip6_daddr
, bdaddr_t
*addr
)
428 u8
*eui64
= ip6_daddr
->s6_addr
+ 8;
430 addr
->b
[0] = eui64
[7];
431 addr
->b
[1] = eui64
[6];
432 addr
->b
[2] = eui64
[5];
433 addr
->b
[3] = eui64
[2];
434 addr
->b
[4] = eui64
[1];
435 addr
->b
[5] = eui64
[0];
438 static void convert_dest_bdaddr(struct in6_addr
*ip6_daddr
,
439 bdaddr_t
*addr
, u8
*addr_type
)
441 copy_to_bdaddr(ip6_daddr
, addr
);
443 /* We need to toggle the U/L bit that we got from IPv6 address
444 * so that we get the proper address and type of the BD address.
448 *addr_type
= get_addr_type_from_eui64(addr
->b
[5]);
451 static int setup_header(struct sk_buff
*skb
, struct net_device
*netdev
,
452 bdaddr_t
*peer_addr
, u8
*peer_addr_type
)
454 struct in6_addr ipv6_daddr
;
455 struct lowpan_dev
*dev
;
456 struct lowpan_peer
*peer
;
457 bdaddr_t addr
, *any
= BDADDR_ANY
;
461 dev
= lowpan_dev(netdev
);
463 memcpy(&ipv6_daddr
, &lowpan_cb(skb
)->addr
, sizeof(ipv6_daddr
));
465 if (ipv6_addr_is_multicast(&ipv6_daddr
)) {
466 lowpan_cb(skb
)->chan
= NULL
;
470 /* Get destination BT device from skb.
471 * If there is no such peer then discard the packet.
473 convert_dest_bdaddr(&ipv6_daddr
, &addr
, &addr_type
);
475 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr
,
476 addr_type
, &ipv6_daddr
);
478 peer
= peer_lookup_ba(dev
, &addr
, addr_type
);
480 /* The packet might be sent to 6lowpan interface
481 * because of routing (either via default route
482 * or user set route) so get peer according to
483 * the destination address.
485 peer
= peer_lookup_dst(dev
, &ipv6_daddr
, skb
);
487 BT_DBG("no such peer %pMR found", &addr
);
492 daddr
= peer
->eui64_addr
;
494 *peer_addr_type
= addr_type
;
495 lowpan_cb(skb
)->chan
= peer
->chan
;
500 lowpan_header_compress(skb
, netdev
, ETH_P_IPV6
, daddr
,
501 dev
->netdev
->dev_addr
, skb
->len
);
503 err
= dev_hard_header(skb
, netdev
, ETH_P_IPV6
, NULL
, NULL
, 0);
510 static int header_create(struct sk_buff
*skb
, struct net_device
*netdev
,
511 unsigned short type
, const void *_daddr
,
512 const void *_saddr
, unsigned int len
)
516 if (type
!= ETH_P_IPV6
)
521 memcpy(&lowpan_cb(skb
)->addr
, &hdr
->daddr
, sizeof(struct in6_addr
));
526 /* Packet to BT LE device */
527 static int send_pkt(struct l2cap_chan
*chan
, struct sk_buff
*skb
,
528 struct net_device
*netdev
)
534 /* Remember the skb so that we can send EAGAIN to the caller if
535 * we run out of credits.
539 iv
.iov_base
= skb
->data
;
540 iv
.iov_len
= skb
->len
;
542 memset(&msg
, 0, sizeof(msg
));
543 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, &iv
, 1, skb
->len
);
545 err
= l2cap_chan_send(chan
, &msg
, skb
->len
);
547 netdev
->stats
.tx_bytes
+= err
;
548 netdev
->stats
.tx_packets
++;
553 err
= lowpan_cb(skb
)->status
;
557 netdev
->stats
.tx_dropped
++;
559 netdev
->stats
.tx_errors
++;
565 static int send_mcast_pkt(struct sk_buff
*skb
, struct net_device
*netdev
)
567 struct sk_buff
*local_skb
;
568 struct lowpan_dev
*entry
;
573 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
574 struct lowpan_peer
*pentry
;
575 struct lowpan_dev
*dev
;
577 if (entry
->netdev
!= netdev
)
580 dev
= lowpan_dev(entry
->netdev
);
582 list_for_each_entry_rcu(pentry
, &dev
->peers
, list
) {
585 local_skb
= skb_clone(skb
, GFP_ATOMIC
);
587 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
589 &pentry
->chan
->dst
, pentry
->chan
->dst_type
,
590 &pentry
->peer_addr
, pentry
->chan
);
591 ret
= send_pkt(pentry
->chan
, local_skb
, netdev
);
595 kfree_skb(local_skb
);
604 static netdev_tx_t
bt_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
610 /* We must take a copy of the skb before we modify/replace the ipv6
611 * header as the header could be used elsewhere
613 skb
= skb_unshare(skb
, GFP_ATOMIC
);
615 return NET_XMIT_DROP
;
617 /* Return values from setup_header()
618 * <0 - error, packet is dropped
619 * 0 - this is a multicast packet
620 * 1 - this is unicast packet
622 err
= setup_header(skb
, netdev
, &addr
, &addr_type
);
625 return NET_XMIT_DROP
;
629 if (lowpan_cb(skb
)->chan
) {
630 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
631 netdev
->name
, &addr
, addr_type
,
632 &lowpan_cb(skb
)->addr
, lowpan_cb(skb
)->chan
);
633 err
= send_pkt(lowpan_cb(skb
)->chan
, skb
, netdev
);
638 /* We need to send the packet to every device behind this
641 err
= send_mcast_pkt(skb
, netdev
);
647 BT_DBG("ERROR: xmit failed (%d)", err
);
649 return err
< 0 ? NET_XMIT_DROP
: err
;
652 static struct lock_class_key bt_tx_busylock
;
653 static struct lock_class_key bt_netdev_xmit_lock_key
;
655 static void bt_set_lockdep_class_one(struct net_device
*dev
,
656 struct netdev_queue
*txq
,
659 lockdep_set_class(&txq
->_xmit_lock
, &bt_netdev_xmit_lock_key
);
662 static int bt_dev_init(struct net_device
*dev
)
664 netdev_for_each_tx_queue(dev
, bt_set_lockdep_class_one
, NULL
);
665 dev
->qdisc_tx_busylock
= &bt_tx_busylock
;
670 static const struct net_device_ops netdev_ops
= {
671 .ndo_init
= bt_dev_init
,
672 .ndo_start_xmit
= bt_xmit
,
675 static struct header_ops header_ops
= {
676 .create
= header_create
,
679 static void netdev_setup(struct net_device
*dev
)
681 dev
->addr_len
= EUI64_ADDR_LEN
;
682 dev
->type
= ARPHRD_6LOWPAN
;
684 dev
->hard_header_len
= 0;
685 dev
->needed_tailroom
= 0;
686 dev
->mtu
= IPV6_MIN_MTU
;
687 dev
->tx_queue_len
= 0;
688 dev
->flags
= IFF_RUNNING
| IFF_POINTOPOINT
|
690 dev
->watchdog_timeo
= 0;
692 dev
->netdev_ops
= &netdev_ops
;
693 dev
->header_ops
= &header_ops
;
694 dev
->destructor
= free_netdev
;
697 static struct device_type bt_type
= {
701 static void set_addr(u8
*eui
, u8
*addr
, u8 addr_type
)
703 /* addr is the BT address in little-endian format */
713 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
714 if (addr_type
== BDADDR_LE_PUBLIC
)
719 BT_DBG("type %d addr %*phC", addr_type
, 8, eui
);
722 static void set_dev_addr(struct net_device
*netdev
, bdaddr_t
*addr
,
725 netdev
->addr_assign_type
= NET_ADDR_PERM
;
726 set_addr(netdev
->dev_addr
, addr
->b
, addr_type
);
729 static void ifup(struct net_device
*netdev
)
734 err
= dev_open(netdev
);
736 BT_INFO("iface %s cannot be opened (%d)", netdev
->name
, err
);
740 static void ifdown(struct net_device
*netdev
)
745 err
= dev_close(netdev
);
747 BT_INFO("iface %s cannot be closed (%d)", netdev
->name
, err
);
751 static void do_notify_peers(struct work_struct
*work
)
753 struct lowpan_dev
*dev
= container_of(work
, struct lowpan_dev
,
756 netdev_notify_peers(dev
->netdev
); /* send neighbour adv at startup */
759 static bool is_bt_6lowpan(struct hci_conn
*hcon
)
761 if (hcon
->type
!= LE_LINK
)
770 static struct l2cap_chan
*chan_create(void)
772 struct l2cap_chan
*chan
;
774 chan
= l2cap_chan_create();
778 l2cap_chan_set_defaults(chan
);
780 chan
->chan_type
= L2CAP_CHAN_CONN_ORIENTED
;
781 chan
->mode
= L2CAP_MODE_LE_FLOWCTL
;
783 chan
->imtu
= chan
->omtu
;
788 static struct l2cap_chan
*chan_open(struct l2cap_chan
*pchan
)
790 struct l2cap_chan
*chan
;
792 chan
= chan_create();
796 chan
->remote_mps
= chan
->omtu
;
797 chan
->mps
= chan
->omtu
;
799 chan
->state
= BT_CONNECTED
;
804 static void set_ip_addr_bits(u8 addr_type
, u8
*addr
)
806 if (addr_type
== BDADDR_LE_PUBLIC
)
812 static struct l2cap_chan
*add_peer_chan(struct l2cap_chan
*chan
,
813 struct lowpan_dev
*dev
)
815 struct lowpan_peer
*peer
;
817 peer
= kzalloc(sizeof(*peer
), GFP_ATOMIC
);
822 memset(&peer
->peer_addr
, 0, sizeof(struct in6_addr
));
825 peer
->peer_addr
.s6_addr
[0] = 0xFE;
826 peer
->peer_addr
.s6_addr
[1] = 0x80;
827 set_addr((u8
*)&peer
->peer_addr
.s6_addr
+ 8, chan
->dst
.b
,
830 memcpy(&peer
->eui64_addr
, (u8
*)&peer
->peer_addr
.s6_addr
+ 8,
833 /* IPv6 address needs to have the U/L bit set properly so toggle
836 set_ip_addr_bits(chan
->dst_type
, (u8
*)&peer
->peer_addr
.s6_addr
+ 8);
838 spin_lock(&devices_lock
);
839 INIT_LIST_HEAD(&peer
->list
);
841 spin_unlock(&devices_lock
);
843 /* Notifying peers about us needs to be done without locks held */
844 INIT_DELAYED_WORK(&dev
->notify_peers
, do_notify_peers
);
845 schedule_delayed_work(&dev
->notify_peers
, msecs_to_jiffies(100));
850 static int setup_netdev(struct l2cap_chan
*chan
, struct lowpan_dev
**dev
)
852 struct net_device
*netdev
;
855 netdev
= alloc_netdev(sizeof(struct lowpan_dev
), IFACE_NAME_TEMPLATE
,
856 NET_NAME_UNKNOWN
, netdev_setup
);
860 set_dev_addr(netdev
, &chan
->src
, chan
->src_type
);
862 netdev
->netdev_ops
= &netdev_ops
;
863 SET_NETDEV_DEV(netdev
, &chan
->conn
->hcon
->dev
);
864 SET_NETDEV_DEVTYPE(netdev
, &bt_type
);
866 err
= register_netdev(netdev
);
868 BT_INFO("register_netdev failed %d", err
);
873 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
874 netdev
->ifindex
, &chan
->dst
, chan
->dst_type
,
875 &chan
->src
, chan
->src_type
);
876 set_bit(__LINK_STATE_PRESENT
, &netdev
->state
);
878 *dev
= netdev_priv(netdev
);
879 (*dev
)->netdev
= netdev
;
880 (*dev
)->hdev
= chan
->conn
->hcon
->hdev
;
881 INIT_LIST_HEAD(&(*dev
)->peers
);
883 spin_lock(&devices_lock
);
884 INIT_LIST_HEAD(&(*dev
)->list
);
885 list_add_rcu(&(*dev
)->list
, &bt_6lowpan_devices
);
886 spin_unlock(&devices_lock
);
894 static inline void chan_ready_cb(struct l2cap_chan
*chan
)
896 struct lowpan_dev
*dev
;
898 dev
= lookup_dev(chan
->conn
);
900 BT_DBG("chan %p conn %p dev %p", chan
, chan
->conn
, dev
);
903 if (setup_netdev(chan
, &dev
) < 0) {
904 l2cap_chan_del(chan
, -ENOENT
);
909 if (!try_module_get(THIS_MODULE
))
912 add_peer_chan(chan
, dev
);
916 static inline struct l2cap_chan
*chan_new_conn_cb(struct l2cap_chan
*pchan
)
918 struct l2cap_chan
*chan
;
920 chan
= chan_open(pchan
);
921 chan
->ops
= pchan
->ops
;
923 BT_DBG("chan %p pchan %p", chan
, pchan
);
928 static void delete_netdev(struct work_struct
*work
)
930 struct lowpan_dev
*entry
= container_of(work
, struct lowpan_dev
,
933 unregister_netdev(entry
->netdev
);
935 /* The entry pointer is deleted in device_event() */
938 static void chan_close_cb(struct l2cap_chan
*chan
)
940 struct lowpan_dev
*entry
;
941 struct lowpan_dev
*dev
= NULL
;
942 struct lowpan_peer
*peer
;
944 bool last
= false, removed
= true;
946 BT_DBG("chan %p conn %p", chan
, chan
->conn
);
948 if (chan
->conn
&& chan
->conn
->hcon
) {
949 if (!is_bt_6lowpan(chan
->conn
->hcon
))
952 /* If conn is set, then the netdev is also there and we should
958 spin_lock(&devices_lock
);
960 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
961 dev
= lowpan_dev(entry
->netdev
);
962 peer
= __peer_lookup_chan(dev
, chan
);
964 last
= peer_del(dev
, peer
);
967 BT_DBG("dev %p removing %speer %p", dev
,
968 last
? "last " : "1 ", peer
);
969 BT_DBG("chan %p orig refcnt %d", chan
,
970 atomic_read(&chan
->kref
.refcount
));
972 l2cap_chan_put(chan
);
977 if (!err
&& last
&& dev
&& !atomic_read(&dev
->peer_count
)) {
978 spin_unlock(&devices_lock
);
980 cancel_delayed_work_sync(&dev
->notify_peers
);
985 INIT_WORK(&entry
->delete_netdev
, delete_netdev
);
986 schedule_work(&entry
->delete_netdev
);
989 spin_unlock(&devices_lock
);
995 static void chan_state_change_cb(struct l2cap_chan
*chan
, int state
, int err
)
997 BT_DBG("chan %p conn %p state %s err %d", chan
, chan
->conn
,
998 state_to_string(state
), err
);
1001 static struct sk_buff
*chan_alloc_skb_cb(struct l2cap_chan
*chan
,
1002 unsigned long hdr_len
,
1003 unsigned long len
, int nb
)
1005 /* Note that we must allocate using GFP_ATOMIC here as
1006 * this function is called originally from netdev hard xmit
1007 * function in atomic context.
1009 return bt_skb_alloc(hdr_len
+ len
, GFP_ATOMIC
);
1012 static void chan_suspend_cb(struct l2cap_chan
*chan
)
1014 struct sk_buff
*skb
= chan
->data
;
1016 BT_DBG("chan %p conn %p skb %p", chan
, chan
->conn
, skb
);
1021 lowpan_cb(skb
)->status
= -EAGAIN
;
1024 static void chan_resume_cb(struct l2cap_chan
*chan
)
1026 struct sk_buff
*skb
= chan
->data
;
1028 BT_DBG("chan %p conn %p skb %p", chan
, chan
->conn
, skb
);
1033 lowpan_cb(skb
)->status
= 0;
1036 static long chan_get_sndtimeo_cb(struct l2cap_chan
*chan
)
1038 return L2CAP_CONN_TIMEOUT
;
1041 static const struct l2cap_ops bt_6lowpan_chan_ops
= {
1042 .name
= "L2CAP 6LoWPAN channel",
1043 .new_connection
= chan_new_conn_cb
,
1044 .recv
= chan_recv_cb
,
1045 .close
= chan_close_cb
,
1046 .state_change
= chan_state_change_cb
,
1047 .ready
= chan_ready_cb
,
1048 .resume
= chan_resume_cb
,
1049 .suspend
= chan_suspend_cb
,
1050 .get_sndtimeo
= chan_get_sndtimeo_cb
,
1051 .alloc_skb
= chan_alloc_skb_cb
,
1053 .teardown
= l2cap_chan_no_teardown
,
1054 .defer
= l2cap_chan_no_defer
,
1055 .set_shutdown
= l2cap_chan_no_set_shutdown
,
1058 static inline __u8
bdaddr_type(__u8 type
)
1060 if (type
== ADDR_LE_DEV_PUBLIC
)
1061 return BDADDR_LE_PUBLIC
;
1063 return BDADDR_LE_RANDOM
;
1066 static struct l2cap_chan
*chan_get(void)
1068 struct l2cap_chan
*pchan
;
1070 pchan
= chan_create();
1074 pchan
->ops
= &bt_6lowpan_chan_ops
;
1079 static int bt_6lowpan_connect(bdaddr_t
*addr
, u8 dst_type
)
1081 struct l2cap_chan
*pchan
;
1088 err
= l2cap_chan_connect(pchan
, cpu_to_le16(psm_6lowpan
), 0,
1091 BT_DBG("chan %p err %d", pchan
, err
);
1093 l2cap_chan_put(pchan
);
1098 static int bt_6lowpan_disconnect(struct l2cap_conn
*conn
, u8 dst_type
)
1100 struct lowpan_peer
*peer
;
1102 BT_DBG("conn %p dst type %d", conn
, dst_type
);
1104 peer
= lookup_peer(conn
);
1108 BT_DBG("peer %p chan %p", peer
, peer
->chan
);
1110 l2cap_chan_close(peer
->chan
, ENOENT
);
1115 static struct l2cap_chan
*bt_6lowpan_listen(void)
1117 bdaddr_t
*addr
= BDADDR_ANY
;
1118 struct l2cap_chan
*pchan
;
1121 if (psm_6lowpan
== 0)
1128 pchan
->state
= BT_LISTEN
;
1129 pchan
->src_type
= BDADDR_LE_PUBLIC
;
1131 atomic_set(&pchan
->nesting
, L2CAP_NESTING_PARENT
);
1133 BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan
, pchan
,
1136 err
= l2cap_add_psm(pchan
, addr
, cpu_to_le16(psm_6lowpan
));
1138 l2cap_chan_put(pchan
);
1139 BT_ERR("psm cannot be added err %d", err
);
1146 static int get_l2cap_conn(char *buf
, bdaddr_t
*addr
, u8
*addr_type
,
1147 struct l2cap_conn
**conn
)
1149 struct hci_conn
*hcon
;
1150 struct hci_dev
*hdev
;
1151 bdaddr_t
*src
= BDADDR_ANY
;
1154 n
= sscanf(buf
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1155 &addr
->b
[5], &addr
->b
[4], &addr
->b
[3],
1156 &addr
->b
[2], &addr
->b
[1], &addr
->b
[0],
1162 hdev
= hci_get_route(addr
, src
);
1167 hcon
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
, addr
);
1168 hci_dev_unlock(hdev
);
1173 *conn
= (struct l2cap_conn
*)hcon
->l2cap_data
;
1175 BT_DBG("conn %p dst %pMR type %d", *conn
, &hcon
->dst
, hcon
->dst_type
);
1180 static void disconnect_all_peers(void)
1182 struct lowpan_dev
*entry
;
1183 struct lowpan_peer
*peer
, *tmp_peer
, *new_peer
;
1184 struct list_head peers
;
1186 INIT_LIST_HEAD(&peers
);
1188 /* We make a separate list of peers as the close_cb() will
1189 * modify the device peers list so it is better not to mess
1190 * with the same list at the same time.
1195 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
1196 list_for_each_entry_rcu(peer
, &entry
->peers
, list
) {
1197 new_peer
= kmalloc(sizeof(*new_peer
), GFP_ATOMIC
);
1201 new_peer
->chan
= peer
->chan
;
1202 INIT_LIST_HEAD(&new_peer
->list
);
1204 list_add(&new_peer
->list
, &peers
);
1210 spin_lock(&devices_lock
);
1211 list_for_each_entry_safe(peer
, tmp_peer
, &peers
, list
) {
1212 l2cap_chan_close(peer
->chan
, ENOENT
);
1214 list_del_rcu(&peer
->list
);
1215 kfree_rcu(peer
, rcu
);
1217 module_put(THIS_MODULE
);
1219 spin_unlock(&devices_lock
);
1223 struct work_struct work
;
1227 static void do_psm_set(struct work_struct
*work
)
1229 struct set_psm
*set_psm
= container_of(work
, struct set_psm
, work
);
1231 if (set_psm
->psm
== 0 || psm_6lowpan
!= set_psm
->psm
)
1232 /* Disconnect existing connections if 6lowpan is
1233 * disabled (psm = 0), or if psm changes.
1235 disconnect_all_peers();
1237 psm_6lowpan
= set_psm
->psm
;
1240 l2cap_chan_close(listen_chan
, 0);
1241 l2cap_chan_put(listen_chan
);
1244 listen_chan
= bt_6lowpan_listen();
1249 static int lowpan_psm_set(void *data
, u64 val
)
1251 struct set_psm
*set_psm
;
1253 set_psm
= kzalloc(sizeof(*set_psm
), GFP_KERNEL
);
1258 INIT_WORK(&set_psm
->work
, do_psm_set
);
1260 schedule_work(&set_psm
->work
);
1265 static int lowpan_psm_get(void *data
, u64
*val
)
1271 DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops
, lowpan_psm_get
,
1272 lowpan_psm_set
, "%llu\n");
1274 static ssize_t
lowpan_control_write(struct file
*fp
,
1275 const char __user
*user_buffer
,
1280 size_t buf_size
= min(count
, sizeof(buf
) - 1);
1284 struct l2cap_conn
*conn
= NULL
;
1286 if (copy_from_user(buf
, user_buffer
, buf_size
))
1289 buf
[buf_size
] = '\0';
1291 if (memcmp(buf
, "connect ", 8) == 0) {
1292 ret
= get_l2cap_conn(&buf
[8], &addr
, &addr_type
, &conn
);
1297 l2cap_chan_close(listen_chan
, 0);
1298 l2cap_chan_put(listen_chan
);
1303 struct lowpan_peer
*peer
;
1305 if (!is_bt_6lowpan(conn
->hcon
))
1308 peer
= lookup_peer(conn
);
1310 BT_DBG("6LoWPAN connection already exists");
1314 BT_DBG("conn %p dst %pMR type %d user %d", conn
,
1315 &conn
->hcon
->dst
, conn
->hcon
->dst_type
,
1319 ret
= bt_6lowpan_connect(&addr
, addr_type
);
1326 if (memcmp(buf
, "disconnect ", 11) == 0) {
1327 ret
= get_l2cap_conn(&buf
[11], &addr
, &addr_type
, &conn
);
1331 ret
= bt_6lowpan_disconnect(conn
, addr_type
);
1341 static int lowpan_control_show(struct seq_file
*f
, void *ptr
)
1343 struct lowpan_dev
*entry
;
1344 struct lowpan_peer
*peer
;
1346 spin_lock(&devices_lock
);
1348 list_for_each_entry(entry
, &bt_6lowpan_devices
, list
) {
1349 list_for_each_entry(peer
, &entry
->peers
, list
)
1350 seq_printf(f
, "%pMR (type %u)\n",
1351 &peer
->chan
->dst
, peer
->chan
->dst_type
);
1354 spin_unlock(&devices_lock
);
1359 static int lowpan_control_open(struct inode
*inode
, struct file
*file
)
1361 return single_open(file
, lowpan_control_show
, inode
->i_private
);
1364 static const struct file_operations lowpan_control_fops
= {
1365 .open
= lowpan_control_open
,
1367 .write
= lowpan_control_write
,
1368 .llseek
= seq_lseek
,
1369 .release
= single_release
,
1372 static void disconnect_devices(void)
1374 struct lowpan_dev
*entry
, *tmp
, *new_dev
;
1375 struct list_head devices
;
1377 INIT_LIST_HEAD(&devices
);
1379 /* We make a separate list of devices because the unregister_netdev()
1380 * will call device_event() which will also want to modify the same
1386 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
1387 new_dev
= kmalloc(sizeof(*new_dev
), GFP_ATOMIC
);
1391 new_dev
->netdev
= entry
->netdev
;
1392 INIT_LIST_HEAD(&new_dev
->list
);
1394 list_add_rcu(&new_dev
->list
, &devices
);
1399 list_for_each_entry_safe(entry
, tmp
, &devices
, list
) {
1400 ifdown(entry
->netdev
);
1401 BT_DBG("Unregistering netdev %s %p",
1402 entry
->netdev
->name
, entry
->netdev
);
1403 unregister_netdev(entry
->netdev
);
1408 static int device_event(struct notifier_block
*unused
,
1409 unsigned long event
, void *ptr
)
1411 struct net_device
*netdev
= netdev_notifier_info_to_dev(ptr
);
1412 struct lowpan_dev
*entry
;
1414 if (netdev
->type
!= ARPHRD_6LOWPAN
)
1418 case NETDEV_UNREGISTER
:
1419 spin_lock(&devices_lock
);
1420 list_for_each_entry(entry
, &bt_6lowpan_devices
, list
) {
1421 if (entry
->netdev
== netdev
) {
1422 BT_DBG("Unregistered netdev %s %p",
1423 netdev
->name
, netdev
);
1424 list_del(&entry
->list
);
1429 spin_unlock(&devices_lock
);
1436 static struct notifier_block bt_6lowpan_dev_notifier
= {
1437 .notifier_call
= device_event
,
1440 static int __init
bt_6lowpan_init(void)
1442 lowpan_psm_debugfs
= debugfs_create_file("6lowpan_psm", 0644,
1445 lowpan_control_debugfs
= debugfs_create_file("6lowpan_control", 0644,
1447 &lowpan_control_fops
);
1449 return register_netdevice_notifier(&bt_6lowpan_dev_notifier
);
1452 static void __exit
bt_6lowpan_exit(void)
1454 debugfs_remove(lowpan_psm_debugfs
);
1455 debugfs_remove(lowpan_control_debugfs
);
1458 l2cap_chan_close(listen_chan
, 0);
1459 l2cap_chan_put(listen_chan
);
1462 disconnect_devices();
1464 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier
);
1467 module_init(bt_6lowpan_init
);
1468 module_exit(bt_6lowpan_exit
);
1470 MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1471 MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1472 MODULE_VERSION(VERSION
);
1473 MODULE_LICENSE("GPL");