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
++;
397 /* Packet from BT LE device */
398 static int chan_recv_cb(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
400 struct lowpan_dev
*dev
;
401 struct lowpan_peer
*peer
;
404 peer
= lookup_peer(chan
->conn
);
408 dev
= lookup_dev(chan
->conn
);
409 if (!dev
|| !dev
->netdev
)
412 err
= recv_pkt(skb
, dev
->netdev
, chan
);
414 BT_DBG("recv pkt %d", err
);
421 static u8
get_addr_type_from_eui64(u8 byte
)
423 /* Is universal(0) or local(1) bit */
424 return ((byte
& 0x02) ? BDADDR_LE_RANDOM
: BDADDR_LE_PUBLIC
);
427 static void copy_to_bdaddr(struct in6_addr
*ip6_daddr
, bdaddr_t
*addr
)
429 u8
*eui64
= ip6_daddr
->s6_addr
+ 8;
431 addr
->b
[0] = eui64
[7];
432 addr
->b
[1] = eui64
[6];
433 addr
->b
[2] = eui64
[5];
434 addr
->b
[3] = eui64
[2];
435 addr
->b
[4] = eui64
[1];
436 addr
->b
[5] = eui64
[0];
439 static void convert_dest_bdaddr(struct in6_addr
*ip6_daddr
,
440 bdaddr_t
*addr
, u8
*addr_type
)
442 copy_to_bdaddr(ip6_daddr
, addr
);
444 /* We need to toggle the U/L bit that we got from IPv6 address
445 * so that we get the proper address and type of the BD address.
449 *addr_type
= get_addr_type_from_eui64(addr
->b
[5]);
452 static int setup_header(struct sk_buff
*skb
, struct net_device
*netdev
,
453 bdaddr_t
*peer_addr
, u8
*peer_addr_type
)
455 struct in6_addr ipv6_daddr
;
456 struct lowpan_dev
*dev
;
457 struct lowpan_peer
*peer
;
458 bdaddr_t addr
, *any
= BDADDR_ANY
;
462 dev
= lowpan_dev(netdev
);
464 memcpy(&ipv6_daddr
, &lowpan_cb(skb
)->addr
, sizeof(ipv6_daddr
));
466 if (ipv6_addr_is_multicast(&ipv6_daddr
)) {
467 lowpan_cb(skb
)->chan
= NULL
;
471 /* Get destination BT device from skb.
472 * If there is no such peer then discard the packet.
474 convert_dest_bdaddr(&ipv6_daddr
, &addr
, &addr_type
);
476 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr
,
477 addr_type
, &ipv6_daddr
);
479 peer
= peer_lookup_ba(dev
, &addr
, addr_type
);
481 /* The packet might be sent to 6lowpan interface
482 * because of routing (either via default route
483 * or user set route) so get peer according to
484 * the destination address.
486 peer
= peer_lookup_dst(dev
, &ipv6_daddr
, skb
);
488 BT_DBG("no such peer %pMR found", &addr
);
493 daddr
= peer
->eui64_addr
;
495 *peer_addr_type
= addr_type
;
496 lowpan_cb(skb
)->chan
= peer
->chan
;
501 lowpan_header_compress(skb
, netdev
, ETH_P_IPV6
, daddr
,
502 dev
->netdev
->dev_addr
, skb
->len
);
504 err
= dev_hard_header(skb
, netdev
, ETH_P_IPV6
, NULL
, NULL
, 0);
511 static int header_create(struct sk_buff
*skb
, struct net_device
*netdev
,
512 unsigned short type
, const void *_daddr
,
513 const void *_saddr
, unsigned int len
)
517 if (type
!= ETH_P_IPV6
)
522 memcpy(&lowpan_cb(skb
)->addr
, &hdr
->daddr
, sizeof(struct in6_addr
));
527 /* Packet to BT LE device */
528 static int send_pkt(struct l2cap_chan
*chan
, struct sk_buff
*skb
,
529 struct net_device
*netdev
)
535 /* Remember the skb so that we can send EAGAIN to the caller if
536 * we run out of credits.
540 iv
.iov_base
= skb
->data
;
541 iv
.iov_len
= skb
->len
;
543 memset(&msg
, 0, sizeof(msg
));
544 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, &iv
, 1, skb
->len
);
546 err
= l2cap_chan_send(chan
, &msg
, skb
->len
);
548 netdev
->stats
.tx_bytes
+= err
;
549 netdev
->stats
.tx_packets
++;
554 err
= lowpan_cb(skb
)->status
;
558 netdev
->stats
.tx_dropped
++;
560 netdev
->stats
.tx_errors
++;
566 static int send_mcast_pkt(struct sk_buff
*skb
, struct net_device
*netdev
)
568 struct sk_buff
*local_skb
;
569 struct lowpan_dev
*entry
;
574 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
575 struct lowpan_peer
*pentry
;
576 struct lowpan_dev
*dev
;
578 if (entry
->netdev
!= netdev
)
581 dev
= lowpan_dev(entry
->netdev
);
583 list_for_each_entry_rcu(pentry
, &dev
->peers
, list
) {
586 local_skb
= skb_clone(skb
, GFP_ATOMIC
);
588 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
590 &pentry
->chan
->dst
, pentry
->chan
->dst_type
,
591 &pentry
->peer_addr
, pentry
->chan
);
592 ret
= send_pkt(pentry
->chan
, local_skb
, netdev
);
596 kfree_skb(local_skb
);
605 static netdev_tx_t
bt_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
611 /* We must take a copy of the skb before we modify/replace the ipv6
612 * header as the header could be used elsewhere
614 skb
= skb_unshare(skb
, GFP_ATOMIC
);
616 return NET_XMIT_DROP
;
618 /* Return values from setup_header()
619 * <0 - error, packet is dropped
620 * 0 - this is a multicast packet
621 * 1 - this is unicast packet
623 err
= setup_header(skb
, netdev
, &addr
, &addr_type
);
626 return NET_XMIT_DROP
;
630 if (lowpan_cb(skb
)->chan
) {
631 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
632 netdev
->name
, &addr
, addr_type
,
633 &lowpan_cb(skb
)->addr
, lowpan_cb(skb
)->chan
);
634 err
= send_pkt(lowpan_cb(skb
)->chan
, skb
, netdev
);
639 /* We need to send the packet to every device behind this
642 err
= send_mcast_pkt(skb
, netdev
);
648 BT_DBG("ERROR: xmit failed (%d)", err
);
650 return err
< 0 ? NET_XMIT_DROP
: err
;
653 static struct lock_class_key bt_tx_busylock
;
654 static struct lock_class_key bt_netdev_xmit_lock_key
;
656 static void bt_set_lockdep_class_one(struct net_device
*dev
,
657 struct netdev_queue
*txq
,
660 lockdep_set_class(&txq
->_xmit_lock
, &bt_netdev_xmit_lock_key
);
663 static int bt_dev_init(struct net_device
*dev
)
665 netdev_for_each_tx_queue(dev
, bt_set_lockdep_class_one
, NULL
);
666 dev
->qdisc_tx_busylock
= &bt_tx_busylock
;
671 static const struct net_device_ops netdev_ops
= {
672 .ndo_init
= bt_dev_init
,
673 .ndo_start_xmit
= bt_xmit
,
676 static struct header_ops header_ops
= {
677 .create
= header_create
,
680 static void netdev_setup(struct net_device
*dev
)
682 dev
->addr_len
= EUI64_ADDR_LEN
;
683 dev
->type
= ARPHRD_6LOWPAN
;
685 dev
->hard_header_len
= 0;
686 dev
->needed_tailroom
= 0;
687 dev
->mtu
= IPV6_MIN_MTU
;
688 dev
->tx_queue_len
= 0;
689 dev
->flags
= IFF_RUNNING
| IFF_POINTOPOINT
|
691 dev
->watchdog_timeo
= 0;
693 dev
->netdev_ops
= &netdev_ops
;
694 dev
->header_ops
= &header_ops
;
695 dev
->destructor
= free_netdev
;
698 static struct device_type bt_type
= {
702 static void set_addr(u8
*eui
, u8
*addr
, u8 addr_type
)
704 /* addr is the BT address in little-endian format */
714 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
715 if (addr_type
== BDADDR_LE_PUBLIC
)
720 BT_DBG("type %d addr %*phC", addr_type
, 8, eui
);
723 static void set_dev_addr(struct net_device
*netdev
, bdaddr_t
*addr
,
726 netdev
->addr_assign_type
= NET_ADDR_PERM
;
727 set_addr(netdev
->dev_addr
, addr
->b
, addr_type
);
730 static void ifup(struct net_device
*netdev
)
735 err
= dev_open(netdev
);
737 BT_INFO("iface %s cannot be opened (%d)", netdev
->name
, err
);
741 static void ifdown(struct net_device
*netdev
)
746 err
= dev_close(netdev
);
748 BT_INFO("iface %s cannot be closed (%d)", netdev
->name
, err
);
752 static void do_notify_peers(struct work_struct
*work
)
754 struct lowpan_dev
*dev
= container_of(work
, struct lowpan_dev
,
757 netdev_notify_peers(dev
->netdev
); /* send neighbour adv at startup */
760 static bool is_bt_6lowpan(struct hci_conn
*hcon
)
762 if (hcon
->type
!= LE_LINK
)
771 static struct l2cap_chan
*chan_create(void)
773 struct l2cap_chan
*chan
;
775 chan
= l2cap_chan_create();
779 l2cap_chan_set_defaults(chan
);
781 chan
->chan_type
= L2CAP_CHAN_CONN_ORIENTED
;
782 chan
->mode
= L2CAP_MODE_LE_FLOWCTL
;
784 chan
->imtu
= chan
->omtu
;
789 static struct l2cap_chan
*chan_open(struct l2cap_chan
*pchan
)
791 struct l2cap_chan
*chan
;
793 chan
= chan_create();
797 chan
->remote_mps
= chan
->omtu
;
798 chan
->mps
= chan
->omtu
;
800 chan
->state
= BT_CONNECTED
;
805 static void set_ip_addr_bits(u8 addr_type
, u8
*addr
)
807 if (addr_type
== BDADDR_LE_PUBLIC
)
813 static struct l2cap_chan
*add_peer_chan(struct l2cap_chan
*chan
,
814 struct lowpan_dev
*dev
)
816 struct lowpan_peer
*peer
;
818 peer
= kzalloc(sizeof(*peer
), GFP_ATOMIC
);
823 memset(&peer
->peer_addr
, 0, sizeof(struct in6_addr
));
826 peer
->peer_addr
.s6_addr
[0] = 0xFE;
827 peer
->peer_addr
.s6_addr
[1] = 0x80;
828 set_addr((u8
*)&peer
->peer_addr
.s6_addr
+ 8, chan
->dst
.b
,
831 memcpy(&peer
->eui64_addr
, (u8
*)&peer
->peer_addr
.s6_addr
+ 8,
834 /* IPv6 address needs to have the U/L bit set properly so toggle
837 set_ip_addr_bits(chan
->dst_type
, (u8
*)&peer
->peer_addr
.s6_addr
+ 8);
839 spin_lock(&devices_lock
);
840 INIT_LIST_HEAD(&peer
->list
);
842 spin_unlock(&devices_lock
);
844 /* Notifying peers about us needs to be done without locks held */
845 INIT_DELAYED_WORK(&dev
->notify_peers
, do_notify_peers
);
846 schedule_delayed_work(&dev
->notify_peers
, msecs_to_jiffies(100));
851 static int setup_netdev(struct l2cap_chan
*chan
, struct lowpan_dev
**dev
)
853 struct net_device
*netdev
;
856 netdev
= alloc_netdev(sizeof(struct lowpan_dev
), IFACE_NAME_TEMPLATE
,
857 NET_NAME_UNKNOWN
, netdev_setup
);
861 set_dev_addr(netdev
, &chan
->src
, chan
->src_type
);
863 netdev
->netdev_ops
= &netdev_ops
;
864 SET_NETDEV_DEV(netdev
, &chan
->conn
->hcon
->dev
);
865 SET_NETDEV_DEVTYPE(netdev
, &bt_type
);
867 err
= register_netdev(netdev
);
869 BT_INFO("register_netdev failed %d", err
);
874 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
875 netdev
->ifindex
, &chan
->dst
, chan
->dst_type
,
876 &chan
->src
, chan
->src_type
);
877 set_bit(__LINK_STATE_PRESENT
, &netdev
->state
);
879 *dev
= netdev_priv(netdev
);
880 (*dev
)->netdev
= netdev
;
881 (*dev
)->hdev
= chan
->conn
->hcon
->hdev
;
882 INIT_LIST_HEAD(&(*dev
)->peers
);
884 spin_lock(&devices_lock
);
885 INIT_LIST_HEAD(&(*dev
)->list
);
886 list_add_rcu(&(*dev
)->list
, &bt_6lowpan_devices
);
887 spin_unlock(&devices_lock
);
895 static inline void chan_ready_cb(struct l2cap_chan
*chan
)
897 struct lowpan_dev
*dev
;
899 dev
= lookup_dev(chan
->conn
);
901 BT_DBG("chan %p conn %p dev %p", chan
, chan
->conn
, dev
);
904 if (setup_netdev(chan
, &dev
) < 0) {
905 l2cap_chan_del(chan
, -ENOENT
);
910 if (!try_module_get(THIS_MODULE
))
913 add_peer_chan(chan
, dev
);
917 static inline struct l2cap_chan
*chan_new_conn_cb(struct l2cap_chan
*pchan
)
919 struct l2cap_chan
*chan
;
921 chan
= chan_open(pchan
);
922 chan
->ops
= pchan
->ops
;
924 BT_DBG("chan %p pchan %p", chan
, pchan
);
929 static void delete_netdev(struct work_struct
*work
)
931 struct lowpan_dev
*entry
= container_of(work
, struct lowpan_dev
,
934 unregister_netdev(entry
->netdev
);
936 /* The entry pointer is deleted in device_event() */
939 static void chan_close_cb(struct l2cap_chan
*chan
)
941 struct lowpan_dev
*entry
;
942 struct lowpan_dev
*dev
= NULL
;
943 struct lowpan_peer
*peer
;
945 bool last
= false, removed
= true;
947 BT_DBG("chan %p conn %p", chan
, chan
->conn
);
949 if (chan
->conn
&& chan
->conn
->hcon
) {
950 if (!is_bt_6lowpan(chan
->conn
->hcon
))
953 /* If conn is set, then the netdev is also there and we should
959 spin_lock(&devices_lock
);
961 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
962 dev
= lowpan_dev(entry
->netdev
);
963 peer
= __peer_lookup_chan(dev
, chan
);
965 last
= peer_del(dev
, peer
);
968 BT_DBG("dev %p removing %speer %p", dev
,
969 last
? "last " : "1 ", peer
);
970 BT_DBG("chan %p orig refcnt %d", chan
,
971 atomic_read(&chan
->kref
.refcount
));
973 l2cap_chan_put(chan
);
978 if (!err
&& last
&& dev
&& !atomic_read(&dev
->peer_count
)) {
979 spin_unlock(&devices_lock
);
981 cancel_delayed_work_sync(&dev
->notify_peers
);
986 INIT_WORK(&entry
->delete_netdev
, delete_netdev
);
987 schedule_work(&entry
->delete_netdev
);
990 spin_unlock(&devices_lock
);
996 static void chan_state_change_cb(struct l2cap_chan
*chan
, int state
, int err
)
998 BT_DBG("chan %p conn %p state %s err %d", chan
, chan
->conn
,
999 state_to_string(state
), err
);
1002 static struct sk_buff
*chan_alloc_skb_cb(struct l2cap_chan
*chan
,
1003 unsigned long hdr_len
,
1004 unsigned long len
, int nb
)
1006 /* Note that we must allocate using GFP_ATOMIC here as
1007 * this function is called originally from netdev hard xmit
1008 * function in atomic context.
1010 return bt_skb_alloc(hdr_len
+ len
, GFP_ATOMIC
);
1013 static void chan_suspend_cb(struct l2cap_chan
*chan
)
1015 struct sk_buff
*skb
= chan
->data
;
1017 BT_DBG("chan %p conn %p skb %p", chan
, chan
->conn
, skb
);
1022 lowpan_cb(skb
)->status
= -EAGAIN
;
1025 static void chan_resume_cb(struct l2cap_chan
*chan
)
1027 struct sk_buff
*skb
= chan
->data
;
1029 BT_DBG("chan %p conn %p skb %p", chan
, chan
->conn
, skb
);
1034 lowpan_cb(skb
)->status
= 0;
1037 static long chan_get_sndtimeo_cb(struct l2cap_chan
*chan
)
1039 return L2CAP_CONN_TIMEOUT
;
1042 static const struct l2cap_ops bt_6lowpan_chan_ops
= {
1043 .name
= "L2CAP 6LoWPAN channel",
1044 .new_connection
= chan_new_conn_cb
,
1045 .recv
= chan_recv_cb
,
1046 .close
= chan_close_cb
,
1047 .state_change
= chan_state_change_cb
,
1048 .ready
= chan_ready_cb
,
1049 .resume
= chan_resume_cb
,
1050 .suspend
= chan_suspend_cb
,
1051 .get_sndtimeo
= chan_get_sndtimeo_cb
,
1052 .alloc_skb
= chan_alloc_skb_cb
,
1054 .teardown
= l2cap_chan_no_teardown
,
1055 .defer
= l2cap_chan_no_defer
,
1056 .set_shutdown
= l2cap_chan_no_set_shutdown
,
1059 static inline __u8
bdaddr_type(__u8 type
)
1061 if (type
== ADDR_LE_DEV_PUBLIC
)
1062 return BDADDR_LE_PUBLIC
;
1064 return BDADDR_LE_RANDOM
;
1067 static struct l2cap_chan
*chan_get(void)
1069 struct l2cap_chan
*pchan
;
1071 pchan
= chan_create();
1075 pchan
->ops
= &bt_6lowpan_chan_ops
;
1080 static int bt_6lowpan_connect(bdaddr_t
*addr
, u8 dst_type
)
1082 struct l2cap_chan
*pchan
;
1089 err
= l2cap_chan_connect(pchan
, cpu_to_le16(psm_6lowpan
), 0,
1092 BT_DBG("chan %p err %d", pchan
, err
);
1094 l2cap_chan_put(pchan
);
1099 static int bt_6lowpan_disconnect(struct l2cap_conn
*conn
, u8 dst_type
)
1101 struct lowpan_peer
*peer
;
1103 BT_DBG("conn %p dst type %d", conn
, dst_type
);
1105 peer
= lookup_peer(conn
);
1109 BT_DBG("peer %p chan %p", peer
, peer
->chan
);
1111 l2cap_chan_close(peer
->chan
, ENOENT
);
1116 static struct l2cap_chan
*bt_6lowpan_listen(void)
1118 bdaddr_t
*addr
= BDADDR_ANY
;
1119 struct l2cap_chan
*pchan
;
1122 if (psm_6lowpan
== 0)
1129 pchan
->state
= BT_LISTEN
;
1130 pchan
->src_type
= BDADDR_LE_PUBLIC
;
1132 atomic_set(&pchan
->nesting
, L2CAP_NESTING_PARENT
);
1134 BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan
, pchan
,
1137 err
= l2cap_add_psm(pchan
, addr
, cpu_to_le16(psm_6lowpan
));
1139 l2cap_chan_put(pchan
);
1140 BT_ERR("psm cannot be added err %d", err
);
1147 static int get_l2cap_conn(char *buf
, bdaddr_t
*addr
, u8
*addr_type
,
1148 struct l2cap_conn
**conn
)
1150 struct hci_conn
*hcon
;
1151 struct hci_dev
*hdev
;
1152 bdaddr_t
*src
= BDADDR_ANY
;
1155 n
= sscanf(buf
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1156 &addr
->b
[5], &addr
->b
[4], &addr
->b
[3],
1157 &addr
->b
[2], &addr
->b
[1], &addr
->b
[0],
1163 hdev
= hci_get_route(addr
, src
);
1168 hcon
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
, addr
);
1169 hci_dev_unlock(hdev
);
1174 *conn
= (struct l2cap_conn
*)hcon
->l2cap_data
;
1176 BT_DBG("conn %p dst %pMR type %d", *conn
, &hcon
->dst
, hcon
->dst_type
);
1181 static void disconnect_all_peers(void)
1183 struct lowpan_dev
*entry
;
1184 struct lowpan_peer
*peer
, *tmp_peer
, *new_peer
;
1185 struct list_head peers
;
1187 INIT_LIST_HEAD(&peers
);
1189 /* We make a separate list of peers as the close_cb() will
1190 * modify the device peers list so it is better not to mess
1191 * with the same list at the same time.
1196 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
1197 list_for_each_entry_rcu(peer
, &entry
->peers
, list
) {
1198 new_peer
= kmalloc(sizeof(*new_peer
), GFP_ATOMIC
);
1202 new_peer
->chan
= peer
->chan
;
1203 INIT_LIST_HEAD(&new_peer
->list
);
1205 list_add(&new_peer
->list
, &peers
);
1211 spin_lock(&devices_lock
);
1212 list_for_each_entry_safe(peer
, tmp_peer
, &peers
, list
) {
1213 l2cap_chan_close(peer
->chan
, ENOENT
);
1215 list_del_rcu(&peer
->list
);
1216 kfree_rcu(peer
, rcu
);
1218 module_put(THIS_MODULE
);
1220 spin_unlock(&devices_lock
);
1224 struct work_struct work
;
1228 static void do_psm_set(struct work_struct
*work
)
1230 struct set_psm
*set_psm
= container_of(work
, struct set_psm
, work
);
1232 if (set_psm
->psm
== 0 || psm_6lowpan
!= set_psm
->psm
)
1233 /* Disconnect existing connections if 6lowpan is
1234 * disabled (psm = 0), or if psm changes.
1236 disconnect_all_peers();
1238 psm_6lowpan
= set_psm
->psm
;
1241 l2cap_chan_close(listen_chan
, 0);
1242 l2cap_chan_put(listen_chan
);
1245 listen_chan
= bt_6lowpan_listen();
1250 static int lowpan_psm_set(void *data
, u64 val
)
1252 struct set_psm
*set_psm
;
1254 set_psm
= kzalloc(sizeof(*set_psm
), GFP_KERNEL
);
1259 INIT_WORK(&set_psm
->work
, do_psm_set
);
1261 schedule_work(&set_psm
->work
);
1266 static int lowpan_psm_get(void *data
, u64
*val
)
1272 DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops
, lowpan_psm_get
,
1273 lowpan_psm_set
, "%llu\n");
1275 static ssize_t
lowpan_control_write(struct file
*fp
,
1276 const char __user
*user_buffer
,
1281 size_t buf_size
= min(count
, sizeof(buf
) - 1);
1285 struct l2cap_conn
*conn
= NULL
;
1287 if (copy_from_user(buf
, user_buffer
, buf_size
))
1290 buf
[buf_size
] = '\0';
1292 if (memcmp(buf
, "connect ", 8) == 0) {
1293 ret
= get_l2cap_conn(&buf
[8], &addr
, &addr_type
, &conn
);
1298 l2cap_chan_close(listen_chan
, 0);
1299 l2cap_chan_put(listen_chan
);
1304 struct lowpan_peer
*peer
;
1306 if (!is_bt_6lowpan(conn
->hcon
))
1309 peer
= lookup_peer(conn
);
1311 BT_DBG("6LoWPAN connection already exists");
1315 BT_DBG("conn %p dst %pMR type %d user %d", conn
,
1316 &conn
->hcon
->dst
, conn
->hcon
->dst_type
,
1320 ret
= bt_6lowpan_connect(&addr
, addr_type
);
1327 if (memcmp(buf
, "disconnect ", 11) == 0) {
1328 ret
= get_l2cap_conn(&buf
[11], &addr
, &addr_type
, &conn
);
1332 ret
= bt_6lowpan_disconnect(conn
, addr_type
);
1342 static int lowpan_control_show(struct seq_file
*f
, void *ptr
)
1344 struct lowpan_dev
*entry
;
1345 struct lowpan_peer
*peer
;
1347 spin_lock(&devices_lock
);
1349 list_for_each_entry(entry
, &bt_6lowpan_devices
, list
) {
1350 list_for_each_entry(peer
, &entry
->peers
, list
)
1351 seq_printf(f
, "%pMR (type %u)\n",
1352 &peer
->chan
->dst
, peer
->chan
->dst_type
);
1355 spin_unlock(&devices_lock
);
1360 static int lowpan_control_open(struct inode
*inode
, struct file
*file
)
1362 return single_open(file
, lowpan_control_show
, inode
->i_private
);
1365 static const struct file_operations lowpan_control_fops
= {
1366 .open
= lowpan_control_open
,
1368 .write
= lowpan_control_write
,
1369 .llseek
= seq_lseek
,
1370 .release
= single_release
,
1373 static void disconnect_devices(void)
1375 struct lowpan_dev
*entry
, *tmp
, *new_dev
;
1376 struct list_head devices
;
1378 INIT_LIST_HEAD(&devices
);
1380 /* We make a separate list of devices because the unregister_netdev()
1381 * will call device_event() which will also want to modify the same
1387 list_for_each_entry_rcu(entry
, &bt_6lowpan_devices
, list
) {
1388 new_dev
= kmalloc(sizeof(*new_dev
), GFP_ATOMIC
);
1392 new_dev
->netdev
= entry
->netdev
;
1393 INIT_LIST_HEAD(&new_dev
->list
);
1395 list_add_rcu(&new_dev
->list
, &devices
);
1400 list_for_each_entry_safe(entry
, tmp
, &devices
, list
) {
1401 ifdown(entry
->netdev
);
1402 BT_DBG("Unregistering netdev %s %p",
1403 entry
->netdev
->name
, entry
->netdev
);
1404 unregister_netdev(entry
->netdev
);
1409 static int device_event(struct notifier_block
*unused
,
1410 unsigned long event
, void *ptr
)
1412 struct net_device
*netdev
= netdev_notifier_info_to_dev(ptr
);
1413 struct lowpan_dev
*entry
;
1415 if (netdev
->type
!= ARPHRD_6LOWPAN
)
1419 case NETDEV_UNREGISTER
:
1420 spin_lock(&devices_lock
);
1421 list_for_each_entry(entry
, &bt_6lowpan_devices
, list
) {
1422 if (entry
->netdev
== netdev
) {
1423 BT_DBG("Unregistered netdev %s %p",
1424 netdev
->name
, netdev
);
1425 list_del(&entry
->list
);
1430 spin_unlock(&devices_lock
);
1437 static struct notifier_block bt_6lowpan_dev_notifier
= {
1438 .notifier_call
= device_event
,
1441 static int __init
bt_6lowpan_init(void)
1443 lowpan_psm_debugfs
= debugfs_create_file("6lowpan_psm", 0644,
1446 lowpan_control_debugfs
= debugfs_create_file("6lowpan_control", 0644,
1448 &lowpan_control_fops
);
1450 return register_netdevice_notifier(&bt_6lowpan_dev_notifier
);
1453 static void __exit
bt_6lowpan_exit(void)
1455 debugfs_remove(lowpan_psm_debugfs
);
1456 debugfs_remove(lowpan_control_debugfs
);
1459 l2cap_chan_close(listen_chan
, 0);
1460 l2cap_chan_put(listen_chan
);
1463 disconnect_devices();
1465 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier
);
1468 module_init(bt_6lowpan_init
);
1469 module_exit(bt_6lowpan_exit
);
1471 MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1472 MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1473 MODULE_VERSION(VERSION
);
1474 MODULE_LICENSE("GPL");