blob: 3f20dce9d671204497d9a7fc885f1e7c237ac31a [file] [log] [blame]
Jukka Rissanen18722c22013-12-11 17:05:37 +02001/*
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03002 Copyright (c) 2013-2014 Intel Corp.
Jukka Rissanen18722c22013-12-11 17:05:37 +02003
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.
7
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.
12*/
13
Jukka Rissanen18722c22013-12-11 17:05:37 +020014#include <linux/if_arp.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
Jukka Rissanen5547e482014-06-18 16:37:09 +030017#include <linux/module.h>
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030018#include <linux/debugfs.h>
Jukka Rissanen18722c22013-12-11 17:05:37 +020019
20#include <net/ipv6.h>
21#include <net/ip6_route.h>
22#include <net/addrconf.h>
23
24#include <net/af_ieee802154.h> /* to get the address type */
25
26#include <net/bluetooth/bluetooth.h>
27#include <net/bluetooth/hci_core.h>
28#include <net/bluetooth/l2cap.h>
29
Alexander Aringcefc8c82014-03-05 14:29:05 +010030#include <net/6lowpan.h> /* for the compression support */
Jukka Rissanen18722c22013-12-11 17:05:37 +020031
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030032#define VERSION "0.1"
33
34static struct dentry *lowpan_psm_debugfs;
35static struct dentry *lowpan_control_debugfs;
36
Jukka Rissanen18722c22013-12-11 17:05:37 +020037#define IFACE_NAME_TEMPLATE "bt%d"
38#define EUI64_ADDR_LEN 8
39
40struct skb_cb {
41 struct in6_addr addr;
Jukka Rissanen39e90c72014-09-08 12:11:45 +030042 struct in6_addr gw;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030043 struct l2cap_chan *chan;
44 int status;
Jukka Rissanen18722c22013-12-11 17:05:37 +020045};
46#define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
47
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.
54 */
55static LIST_HEAD(bt_6lowpan_devices);
Jukka Rissanen90305822014-10-28 17:16:47 +020056static DEFINE_SPINLOCK(devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +020057
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030058/* If psm is set to 0 (default value), then 6lowpan is disabled.
59 * Other values are used to indicate a Protocol Service Multiplexer
60 * value for 6lowpan.
61 */
62static u16 psm_6lowpan;
63
64/* We are listening incoming connections via this channel
65 */
66static struct l2cap_chan *listen_chan;
67
Jukka Rissanen18722c22013-12-11 17:05:37 +020068struct lowpan_peer {
69 struct list_head list;
Jukka Rissanen90305822014-10-28 17:16:47 +020070 struct rcu_head rcu;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030071 struct l2cap_chan *chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +020072
73 /* peer addresses in various formats */
74 unsigned char eui64_addr[EUI64_ADDR_LEN];
75 struct in6_addr peer_addr;
76};
77
78struct lowpan_dev {
79 struct list_head list;
80
81 struct hci_dev *hdev;
82 struct net_device *netdev;
83 struct list_head peers;
84 atomic_t peer_count; /* number of items in peers list */
85
86 struct work_struct delete_netdev;
87 struct delayed_work notify_peers;
88};
89
Jukka Rissanen90305822014-10-28 17:16:47 +020090static inline void peer_free(struct rcu_head *head)
91{
92 struct lowpan_peer *e = container_of(head, struct lowpan_peer, rcu);
93
94 kfree(e);
95}
96
Jukka Rissanen18722c22013-12-11 17:05:37 +020097static inline struct lowpan_dev *lowpan_dev(const struct net_device *netdev)
98{
99 return netdev_priv(netdev);
100}
101
102static inline void peer_add(struct lowpan_dev *dev, struct lowpan_peer *peer)
103{
Jukka Rissanen90305822014-10-28 17:16:47 +0200104 list_add_rcu(&peer->list, &dev->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200105 atomic_inc(&dev->peer_count);
106}
107
108static inline bool peer_del(struct lowpan_dev *dev, struct lowpan_peer *peer)
109{
Jukka Rissanen90305822014-10-28 17:16:47 +0200110 list_del_rcu(&peer->list);
111 call_rcu(&peer->rcu, peer_free);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200112
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300113 module_put(THIS_MODULE);
114
Jukka Rissanen18722c22013-12-11 17:05:37 +0200115 if (atomic_dec_and_test(&dev->peer_count)) {
116 BT_DBG("last peer");
117 return true;
118 }
119
120 return false;
121}
122
123static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_dev *dev,
124 bdaddr_t *ba, __u8 type)
125{
Jukka Rissanen90305822014-10-28 17:16:47 +0200126 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200127
128 BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count),
129 ba, type);
130
Jukka Rissanen90305822014-10-28 17:16:47 +0200131 rcu_read_lock();
132
133 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300134 BT_DBG("dst addr %pMR dst type %d",
135 &peer->chan->dst, peer->chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200136
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300137 if (bacmp(&peer->chan->dst, ba))
Jukka Rissanen18722c22013-12-11 17:05:37 +0200138 continue;
139
Jukka Rissanen90305822014-10-28 17:16:47 +0200140 if (type == peer->chan->dst_type) {
141 rcu_read_unlock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300142 return peer;
Jukka Rissanen90305822014-10-28 17:16:47 +0200143 }
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300144 }
145
Jukka Rissanen90305822014-10-28 17:16:47 +0200146 rcu_read_unlock();
147
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300148 return NULL;
149}
150
Jukka Rissanen90305822014-10-28 17:16:47 +0200151static inline struct lowpan_peer *__peer_lookup_chan(struct lowpan_dev *dev,
152 struct l2cap_chan *chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300153{
Jukka Rissanen90305822014-10-28 17:16:47 +0200154 struct lowpan_peer *peer;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300155
Jukka Rissanen90305822014-10-28 17:16:47 +0200156 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300157 if (peer->chan == chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200158 return peer;
159 }
160
161 return NULL;
162}
163
Jukka Rissanen90305822014-10-28 17:16:47 +0200164static inline struct lowpan_peer *__peer_lookup_conn(struct lowpan_dev *dev,
165 struct l2cap_conn *conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200166{
Jukka Rissanen90305822014-10-28 17:16:47 +0200167 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200168
Jukka Rissanen90305822014-10-28 17:16:47 +0200169 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300170 if (peer->chan->conn == conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200171 return peer;
172 }
173
174 return NULL;
175}
176
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300177static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_dev *dev,
178 struct in6_addr *daddr,
179 struct sk_buff *skb)
180{
Jukka Rissanen90305822014-10-28 17:16:47 +0200181 struct lowpan_peer *peer;
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300182 struct in6_addr *nexthop;
183 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
184 int count = atomic_read(&dev->peer_count);
185
186 BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt);
187
188 /* If we have multiple 6lowpan peers, then check where we should
189 * send the packet. If only one peer exists, then we can send the
190 * packet right away.
191 */
Jukka Rissanen90305822014-10-28 17:16:47 +0200192 if (count == 1) {
193 rcu_read_lock();
194 peer = list_first_or_null_rcu(&dev->peers, struct lowpan_peer,
195 list);
196 rcu_read_unlock();
197 return peer;
198 }
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300199
200 if (!rt) {
201 nexthop = &lowpan_cb(skb)->gw;
202
203 if (ipv6_addr_any(nexthop))
204 return NULL;
205 } else {
206 nexthop = rt6_nexthop(rt);
207
208 /* We need to remember the address because it is needed
209 * by bt_xmit() when sending the packet. In bt_xmit(), the
210 * destination routing info is not set.
211 */
212 memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr));
213 }
214
215 BT_DBG("gw %pI6c", nexthop);
216
Jukka Rissanen90305822014-10-28 17:16:47 +0200217 rcu_read_lock();
218
219 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300220 BT_DBG("dst addr %pMR dst type %d ip %pI6c",
221 &peer->chan->dst, peer->chan->dst_type,
222 &peer->peer_addr);
223
Jukka Rissanen90305822014-10-28 17:16:47 +0200224 if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) {
225 rcu_read_unlock();
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300226 return peer;
Jukka Rissanen90305822014-10-28 17:16:47 +0200227 }
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300228 }
229
Jukka Rissanen90305822014-10-28 17:16:47 +0200230 rcu_read_unlock();
231
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300232 return NULL;
233}
234
Jukka Rissanen18722c22013-12-11 17:05:37 +0200235static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
236{
Jukka Rissanen90305822014-10-28 17:16:47 +0200237 struct lowpan_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200238 struct lowpan_peer *peer = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200239
Jukka Rissanen90305822014-10-28 17:16:47 +0200240 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200241
Jukka Rissanen90305822014-10-28 17:16:47 +0200242 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
243 peer = __peer_lookup_conn(entry, conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200244 if (peer)
245 break;
246 }
247
Jukka Rissanen90305822014-10-28 17:16:47 +0200248 rcu_read_unlock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200249
250 return peer;
251}
252
253static struct lowpan_dev *lookup_dev(struct l2cap_conn *conn)
254{
Jukka Rissanen90305822014-10-28 17:16:47 +0200255 struct lowpan_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200256 struct lowpan_dev *dev = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200257
Jukka Rissanen90305822014-10-28 17:16:47 +0200258 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200259
Jukka Rissanen90305822014-10-28 17:16:47 +0200260 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +0200261 if (conn->hcon->hdev == entry->hdev) {
262 dev = entry;
263 break;
264 }
265 }
266
Jukka Rissanen90305822014-10-28 17:16:47 +0200267 rcu_read_unlock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200268
269 return dev;
270}
271
Jukka Rissanen18722c22013-12-11 17:05:37 +0200272static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
273{
274 struct sk_buff *skb_cp;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200275
276 skb_cp = skb_copy(skb, GFP_ATOMIC);
277 if (!skb_cp)
Martin Townsendf8b36172014-10-23 15:40:53 +0100278 return NET_RX_DROP;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200279
Li RongQing4456c502014-10-16 10:21:55 +0800280 return netif_rx(skb_cp);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200281}
282
Martin Townsend01141232014-10-23 15:40:56 +0100283static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev,
284 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200285{
286 const u8 *saddr, *daddr;
287 u8 iphc0, iphc1;
288 struct lowpan_dev *dev;
289 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200290
291 dev = lowpan_dev(netdev);
292
Jukka Rissanen90305822014-10-28 17:16:47 +0200293 rcu_read_lock();
294 peer = __peer_lookup_chan(dev, chan);
295 rcu_read_unlock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200296 if (!peer)
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000297 return -EINVAL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200298
299 saddr = peer->eui64_addr;
300 daddr = dev->netdev->dev_addr;
301
302 /* at least two bytes will be used for the encoding */
303 if (skb->len < 2)
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000304 return -EINVAL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200305
306 if (lowpan_fetch_skb_u8(skb, &iphc0))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000307 return -EINVAL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200308
309 if (lowpan_fetch_skb_u8(skb, &iphc1))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000310 return -EINVAL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200311
Martin Townsend01141232014-10-23 15:40:56 +0100312 return lowpan_header_decompress(skb, netdev,
313 saddr, IEEE802154_ADDR_LONG,
314 EUI64_ADDR_LEN, daddr,
315 IEEE802154_ADDR_LONG, EUI64_ADDR_LEN,
316 iphc0, iphc1);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200317
Jukka Rissanen18722c22013-12-11 17:05:37 +0200318}
319
320static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300321 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200322{
323 struct sk_buff *local_skb;
324 int ret;
325
326 if (!netif_running(dev))
327 goto drop;
328
329 if (dev->type != ARPHRD_6LOWPAN)
330 goto drop;
331
Martin Townsend11e3ff72014-10-13 11:00:56 +0100332 skb = skb_share_check(skb, GFP_ATOMIC);
333 if (!skb)
334 goto drop;
335
Jukka Rissanen18722c22013-12-11 17:05:37 +0200336 /* check that it's our buffer */
337 if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
338 /* Copy the packet so that the IPv6 header is
339 * properly aligned.
340 */
341 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
342 skb_tailroom(skb), GFP_ATOMIC);
343 if (!local_skb)
344 goto drop;
345
346 local_skb->protocol = htons(ETH_P_IPV6);
347 local_skb->pkt_type = PACKET_HOST;
348
349 skb_reset_network_header(local_skb);
350 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
351
352 if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
353 kfree_skb(local_skb);
354 goto drop;
355 }
356
357 dev->stats.rx_bytes += skb->len;
358 dev->stats.rx_packets++;
359
Martin Townsend3c400b82014-10-23 15:40:55 +0100360 consume_skb(local_skb);
361 consume_skb(skb);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200362 } else {
363 switch (skb->data[0] & 0xe0) {
364 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
365 local_skb = skb_clone(skb, GFP_ATOMIC);
366 if (!local_skb)
367 goto drop;
368
Martin Townsend01141232014-10-23 15:40:56 +0100369 ret = iphc_decompress(local_skb, dev, chan);
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000370 if (ret < 0) {
371 kfree_skb(local_skb);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200372 goto drop;
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000373 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200374
Martin Townsendf8b36172014-10-23 15:40:53 +0100375 local_skb->protocol = htons(ETH_P_IPV6);
376 local_skb->pkt_type = PACKET_HOST;
377 local_skb->dev = dev;
378
379 if (give_skb_to_upper(local_skb, dev)
380 != NET_RX_SUCCESS) {
381 kfree_skb(local_skb);
382 goto drop;
383 }
384
Jukka Rissanen18722c22013-12-11 17:05:37 +0200385 dev->stats.rx_bytes += skb->len;
386 dev->stats.rx_packets++;
387
Martin Townsend3c400b82014-10-23 15:40:55 +0100388 consume_skb(local_skb);
389 consume_skb(skb);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200390 break;
391 default:
392 break;
393 }
394 }
395
396 return NET_RX_SUCCESS;
397
398drop:
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300399 dev->stats.rx_dropped++;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200400 kfree_skb(skb);
401 return NET_RX_DROP;
402}
403
404/* Packet from BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300405static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200406{
407 struct lowpan_dev *dev;
408 struct lowpan_peer *peer;
409 int err;
410
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300411 peer = lookup_peer(chan->conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200412 if (!peer)
413 return -ENOENT;
414
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300415 dev = lookup_dev(chan->conn);
Johan Hedberg30d3db42013-12-12 09:53:21 +0200416 if (!dev || !dev->netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200417 return -ENOENT;
418
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300419 err = recv_pkt(skb, dev->netdev, chan);
420 if (err) {
421 BT_DBG("recv pkt %d", err);
422 err = -EAGAIN;
423 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200424
425 return err;
426}
427
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300428static u8 get_addr_type_from_eui64(u8 byte)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200429{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300430 /* Is universal(0) or local(1) bit */
431 return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC);
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300432}
433
434static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr)
435{
436 u8 *eui64 = ip6_daddr->s6_addr + 8;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200437
438 addr->b[0] = eui64[7];
439 addr->b[1] = eui64[6];
440 addr->b[2] = eui64[5];
441 addr->b[3] = eui64[2];
442 addr->b[4] = eui64[1];
443 addr->b[5] = eui64[0];
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300444}
Jukka Rissanen18722c22013-12-11 17:05:37 +0200445
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300446static void convert_dest_bdaddr(struct in6_addr *ip6_daddr,
447 bdaddr_t *addr, u8 *addr_type)
448{
449 copy_to_bdaddr(ip6_daddr, addr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200450
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300451 /* We need to toggle the U/L bit that we got from IPv6 address
452 * so that we get the proper address and type of the BD address.
453 */
454 addr->b[5] ^= 0x02;
455
456 *addr_type = get_addr_type_from_eui64(addr->b[5]);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200457}
458
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300459static int setup_header(struct sk_buff *skb, struct net_device *netdev,
460 bdaddr_t *peer_addr, u8 *peer_addr_type)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200461{
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300462 struct in6_addr ipv6_daddr;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200463 struct lowpan_dev *dev;
464 struct lowpan_peer *peer;
465 bdaddr_t addr, *any = BDADDR_ANY;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300466 u8 *daddr = any->b;
467 int err, status = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200468
469 dev = lowpan_dev(netdev);
470
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300471 memcpy(&ipv6_daddr, &lowpan_cb(skb)->addr, sizeof(ipv6_daddr));
472
473 if (ipv6_addr_is_multicast(&ipv6_daddr)) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300474 lowpan_cb(skb)->chan = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200475 } else {
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300476 u8 addr_type;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200477
478 /* Get destination BT device from skb.
479 * If there is no such peer then discard the packet.
480 */
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300481 convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200482
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300483 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr,
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300484 addr_type, &ipv6_daddr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200485
Jukka Rissanen18722c22013-12-11 17:05:37 +0200486 peer = peer_lookup_ba(dev, &addr, addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200487 if (!peer) {
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300488 /* The packet might be sent to 6lowpan interface
489 * because of routing (either via default route
490 * or user set route) so get peer according to
491 * the destination address.
492 */
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300493 peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300494 if (!peer) {
495 BT_DBG("no such peer %pMR found", &addr);
496 return -ENOENT;
497 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200498 }
499
500 daddr = peer->eui64_addr;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300501 *peer_addr = addr;
502 *peer_addr_type = addr_type;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300503 lowpan_cb(skb)->chan = peer->chan;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300504
505 status = 1;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200506 }
507
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300508 lowpan_header_compress(skb, netdev, ETH_P_IPV6, daddr,
509 dev->netdev->dev_addr, skb->len);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200510
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300511 err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
512 if (err < 0)
513 return err;
514
515 return status;
516}
517
518static int header_create(struct sk_buff *skb, struct net_device *netdev,
519 unsigned short type, const void *_daddr,
520 const void *_saddr, unsigned int len)
521{
522 struct ipv6hdr *hdr;
523
524 if (type != ETH_P_IPV6)
525 return -EINVAL;
526
527 hdr = ipv6_hdr(skb);
528
529 memcpy(&lowpan_cb(skb)->addr, &hdr->daddr, sizeof(struct in6_addr));
530
531 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200532}
533
534/* Packet to BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300535static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300536 struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200537{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300538 struct msghdr msg;
539 struct kvec iv;
540 int err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200541
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300542 /* Remember the skb so that we can send EAGAIN to the caller if
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300543 * we run out of credits.
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300544 */
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300545 chan->data = skb;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300546
547 memset(&msg, 0, sizeof(msg));
548 msg.msg_iov = (struct iovec *) &iv;
549 msg.msg_iovlen = 1;
550 iv.iov_base = skb->data;
551 iv.iov_len = skb->len;
552
553 err = l2cap_chan_send(chan, &msg, skb->len);
554 if (err > 0) {
555 netdev->stats.tx_bytes += err;
556 netdev->stats.tx_packets++;
557 return 0;
558 }
559
560 if (!err)
561 err = lowpan_cb(skb)->status;
562
563 if (err < 0) {
564 if (err == -EAGAIN)
565 netdev->stats.tx_dropped++;
566 else
567 netdev->stats.tx_errors++;
568 }
569
570 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200571}
572
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300573static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200574{
575 struct sk_buff *local_skb;
Jukka Rissanen90305822014-10-28 17:16:47 +0200576 struct lowpan_dev *entry;
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300577 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200578
Jukka Rissanen90305822014-10-28 17:16:47 +0200579 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200580
Jukka Rissanen90305822014-10-28 17:16:47 +0200581 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
582 struct lowpan_peer *pentry;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200583 struct lowpan_dev *dev;
584
585 if (entry->netdev != netdev)
586 continue;
587
588 dev = lowpan_dev(entry->netdev);
589
Jukka Rissanen90305822014-10-28 17:16:47 +0200590 list_for_each_entry_rcu(pentry, &dev->peers, list) {
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300591 int ret;
592
Jukka Rissanen18722c22013-12-11 17:05:37 +0200593 local_skb = skb_clone(skb, GFP_ATOMIC);
594
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300595 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
596 netdev->name,
597 &pentry->chan->dst, pentry->chan->dst_type,
598 &pentry->peer_addr, pentry->chan);
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300599 ret = send_pkt(pentry->chan, local_skb, netdev);
600 if (ret < 0)
601 err = ret;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200602
603 kfree_skb(local_skb);
604 }
605 }
606
Jukka Rissanen90305822014-10-28 17:16:47 +0200607 rcu_read_unlock();
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300608
609 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200610}
611
612static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
613{
614 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200615 bdaddr_t addr;
616 u8 addr_type;
617
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300618 /* We must take a copy of the skb before we modify/replace the ipv6
619 * header as the header could be used elsewhere
620 */
Alexander Aringb0c42cd2014-10-08 10:24:53 +0200621 skb = skb_unshare(skb, GFP_ATOMIC);
622 if (!skb)
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300623 return NET_XMIT_DROP;
624
625 /* Return values from setup_header()
626 * <0 - error, packet is dropped
627 * 0 - this is a multicast packet
628 * 1 - this is unicast packet
629 */
630 err = setup_header(skb, netdev, &addr, &addr_type);
631 if (err < 0) {
632 kfree_skb(skb);
633 return NET_XMIT_DROP;
634 }
635
636 if (err) {
637 if (lowpan_cb(skb)->chan) {
638 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
639 netdev->name, &addr, addr_type,
640 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300641 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300642 } else {
643 err = -ENOENT;
644 }
645 } else {
646 /* We need to send the packet to every device behind this
647 * interface.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200648 */
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300649 err = send_mcast_pkt(skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200650 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200651
Jukka Rissanenfc125182014-10-01 11:30:26 +0300652 dev_kfree_skb(skb);
653
Jukka Rissanen18722c22013-12-11 17:05:37 +0200654 if (err)
655 BT_DBG("ERROR: xmit failed (%d)", err);
656
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300657 return err < 0 ? NET_XMIT_DROP : err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200658}
659
Jukka Rissanendf092302014-10-28 17:16:48 +0200660static struct lock_class_key bt_tx_busylock;
661static struct lock_class_key bt_netdev_xmit_lock_key;
662
663static void bt_set_lockdep_class_one(struct net_device *dev,
664 struct netdev_queue *txq,
665 void *_unused)
666{
667 lockdep_set_class(&txq->_xmit_lock, &bt_netdev_xmit_lock_key);
668}
669
670static int bt_dev_init(struct net_device *dev)
671{
672 netdev_for_each_tx_queue(dev, bt_set_lockdep_class_one, NULL);
673 dev->qdisc_tx_busylock = &bt_tx_busylock;
674
675 return 0;
676}
677
Jukka Rissanen18722c22013-12-11 17:05:37 +0200678static const struct net_device_ops netdev_ops = {
Jukka Rissanendf092302014-10-28 17:16:48 +0200679 .ndo_init = bt_dev_init,
Jukka Rissanen18722c22013-12-11 17:05:37 +0200680 .ndo_start_xmit = bt_xmit,
681};
682
683static struct header_ops header_ops = {
684 .create = header_create,
685};
686
687static void netdev_setup(struct net_device *dev)
688{
689 dev->addr_len = EUI64_ADDR_LEN;
690 dev->type = ARPHRD_6LOWPAN;
691
692 dev->hard_header_len = 0;
693 dev->needed_tailroom = 0;
694 dev->mtu = IPV6_MIN_MTU;
695 dev->tx_queue_len = 0;
Jukka Rissanen156395c2014-09-29 16:37:26 +0300696 dev->flags = IFF_RUNNING | IFF_POINTOPOINT |
697 IFF_MULTICAST;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200698 dev->watchdog_timeo = 0;
699
700 dev->netdev_ops = &netdev_ops;
701 dev->header_ops = &header_ops;
702 dev->destructor = free_netdev;
703}
704
705static struct device_type bt_type = {
706 .name = "bluetooth",
707};
708
709static void set_addr(u8 *eui, u8 *addr, u8 addr_type)
710{
711 /* addr is the BT address in little-endian format */
712 eui[0] = addr[5];
713 eui[1] = addr[4];
714 eui[2] = addr[3];
715 eui[3] = 0xFF;
716 eui[4] = 0xFE;
717 eui[5] = addr[2];
718 eui[6] = addr[1];
719 eui[7] = addr[0];
720
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300721 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300722 if (addr_type == BDADDR_LE_PUBLIC)
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300723 eui[0] &= ~0x02;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200724 else
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300725 eui[0] |= 0x02;
726
727 BT_DBG("type %d addr %*phC", addr_type, 8, eui);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200728}
729
730static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr,
731 u8 addr_type)
732{
733 netdev->addr_assign_type = NET_ADDR_PERM;
734 set_addr(netdev->dev_addr, addr->b, addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200735}
736
737static void ifup(struct net_device *netdev)
738{
739 int err;
740
741 rtnl_lock();
742 err = dev_open(netdev);
743 if (err < 0)
744 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
745 rtnl_unlock();
746}
747
Jukka Rissanen7f118252014-06-18 16:37:11 +0300748static void ifdown(struct net_device *netdev)
749{
750 int err;
751
752 rtnl_lock();
753 err = dev_close(netdev);
754 if (err < 0)
755 BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
756 rtnl_unlock();
757}
758
Jukka Rissanen18722c22013-12-11 17:05:37 +0200759static void do_notify_peers(struct work_struct *work)
760{
761 struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
762 notify_peers.work);
763
764 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
765}
766
767static bool is_bt_6lowpan(struct hci_conn *hcon)
768{
769 if (hcon->type != LE_LINK)
770 return false;
771
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300772 if (!psm_6lowpan)
773 return false;
774
775 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200776}
777
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300778static struct l2cap_chan *chan_create(void)
779{
780 struct l2cap_chan *chan;
781
782 chan = l2cap_chan_create();
783 if (!chan)
784 return NULL;
785
786 l2cap_chan_set_defaults(chan);
787
788 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
789 chan->mode = L2CAP_MODE_LE_FLOWCTL;
790 chan->omtu = 65535;
791 chan->imtu = chan->omtu;
792
793 return chan;
794}
795
796static struct l2cap_chan *chan_open(struct l2cap_chan *pchan)
797{
798 struct l2cap_chan *chan;
799
800 chan = chan_create();
801 if (!chan)
802 return NULL;
803
804 chan->remote_mps = chan->omtu;
805 chan->mps = chan->omtu;
806
807 chan->state = BT_CONNECTED;
808
809 return chan;
810}
811
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300812static void set_ip_addr_bits(u8 addr_type, u8 *addr)
813{
814 if (addr_type == BDADDR_LE_PUBLIC)
815 *addr |= 0x02;
816 else
817 *addr &= ~0x02;
818}
819
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300820static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
821 struct lowpan_dev *dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200822{
823 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200824
825 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
826 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300827 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200828
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300829 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200830 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
831
832 /* RFC 2464 ch. 5 */
833 peer->peer_addr.s6_addr[0] = 0xFE;
834 peer->peer_addr.s6_addr[1] = 0x80;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300835 set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
836 chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200837
838 memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
839 EUI64_ADDR_LEN);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200840
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300841 /* IPv6 address needs to have the U/L bit set properly so toggle
842 * it back here.
843 */
844 set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8);
845
Jukka Rissanen90305822014-10-28 17:16:47 +0200846 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200847 INIT_LIST_HEAD(&peer->list);
848 peer_add(dev, peer);
Jukka Rissanen90305822014-10-28 17:16:47 +0200849 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200850
851 /* Notifying peers about us needs to be done without locks held */
852 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
853 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
854
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300855 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200856}
857
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300858static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200859{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200860 struct net_device *netdev;
861 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200862
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300863 netdev = alloc_netdev(sizeof(struct lowpan_dev), IFACE_NAME_TEMPLATE,
Tom Gundersenc835a672014-07-14 16:37:24 +0200864 NET_NAME_UNKNOWN, netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200865 if (!netdev)
866 return -ENOMEM;
867
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300868 set_dev_addr(netdev, &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200869
870 netdev->netdev_ops = &netdev_ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300871 SET_NETDEV_DEV(netdev, &chan->conn->hcon->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200872 SET_NETDEV_DEVTYPE(netdev, &bt_type);
873
874 err = register_netdev(netdev);
875 if (err < 0) {
876 BT_INFO("register_netdev failed %d", err);
877 free_netdev(netdev);
878 goto out;
879 }
880
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300881 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
882 netdev->ifindex, &chan->dst, chan->dst_type,
883 &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200884 set_bit(__LINK_STATE_PRESENT, &netdev->state);
885
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300886 *dev = netdev_priv(netdev);
887 (*dev)->netdev = netdev;
888 (*dev)->hdev = chan->conn->hcon->hdev;
889 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200890
Jukka Rissanen90305822014-10-28 17:16:47 +0200891 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300892 INIT_LIST_HEAD(&(*dev)->list);
Jukka Rissanen90305822014-10-28 17:16:47 +0200893 list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
894 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200895
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300896 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200897
898out:
899 return err;
900}
901
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300902static inline void chan_ready_cb(struct l2cap_chan *chan)
903{
904 struct lowpan_dev *dev;
905
906 dev = lookup_dev(chan->conn);
907
908 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
909
910 if (!dev) {
911 if (setup_netdev(chan, &dev) < 0) {
912 l2cap_chan_del(chan, -ENOENT);
913 return;
914 }
915 }
916
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300917 if (!try_module_get(THIS_MODULE))
918 return;
919
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300920 add_peer_chan(chan, dev);
921 ifup(dev->netdev);
922}
923
Johan Hedberg2b293492014-08-07 10:03:32 +0300924static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300925{
Johan Hedberg2b293492014-08-07 10:03:32 +0300926 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300927
Johan Hedberg2b293492014-08-07 10:03:32 +0300928 chan = chan_open(pchan);
929 chan->ops = pchan->ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300930
931 BT_DBG("chan %p pchan %p", chan, pchan);
932
Johan Hedberg2b293492014-08-07 10:03:32 +0300933 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300934}
935
Jukka Rissanen18722c22013-12-11 17:05:37 +0200936static void delete_netdev(struct work_struct *work)
937{
938 struct lowpan_dev *entry = container_of(work, struct lowpan_dev,
939 delete_netdev);
940
941 unregister_netdev(entry->netdev);
942
943 /* The entry pointer is deleted in device_event() */
944}
945
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300946static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200947{
Jukka Rissanen90305822014-10-28 17:16:47 +0200948 struct lowpan_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200949 struct lowpan_dev *dev = NULL;
950 struct lowpan_peer *peer;
951 int err = -ENOENT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300952 bool last = false, removed = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200953
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300954 BT_DBG("chan %p conn %p", chan, chan->conn);
955
956 if (chan->conn && chan->conn->hcon) {
957 if (!is_bt_6lowpan(chan->conn->hcon))
958 return;
959
960 /* If conn is set, then the netdev is also there and we should
961 * not remove it.
962 */
963 removed = false;
964 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200965
Jukka Rissanen90305822014-10-28 17:16:47 +0200966 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200967
Jukka Rissanen90305822014-10-28 17:16:47 +0200968 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +0200969 dev = lowpan_dev(entry->netdev);
Jukka Rissanen90305822014-10-28 17:16:47 +0200970 peer = __peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200971 if (peer) {
972 last = peer_del(dev, peer);
973 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300974
975 BT_DBG("dev %p removing %speer %p", dev,
976 last ? "last " : "1 ", peer);
977 BT_DBG("chan %p orig refcnt %d", chan,
978 atomic_read(&chan->kref.refcount));
979
980 l2cap_chan_put(chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200981 break;
982 }
983 }
984
985 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
Jukka Rissanen90305822014-10-28 17:16:47 +0200986 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200987
988 cancel_delayed_work_sync(&dev->notify_peers);
989
Jukka Rissanen7f118252014-06-18 16:37:11 +0300990 ifdown(dev->netdev);
991
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300992 if (!removed) {
993 INIT_WORK(&entry->delete_netdev, delete_netdev);
994 schedule_work(&entry->delete_netdev);
995 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200996 } else {
Jukka Rissanen90305822014-10-28 17:16:47 +0200997 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200998 }
999
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001000 return;
1001}
1002
1003static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
1004{
1005 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
1006 state_to_string(state), err);
1007}
1008
1009static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
1010 unsigned long hdr_len,
1011 unsigned long len, int nb)
1012{
1013 /* Note that we must allocate using GFP_ATOMIC here as
1014 * this function is called originally from netdev hard xmit
1015 * function in atomic context.
1016 */
1017 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
1018}
1019
1020static void chan_suspend_cb(struct l2cap_chan *chan)
1021{
1022 struct sk_buff *skb = chan->data;
1023
1024 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
1025
Jukka Rissanen59790aa2014-09-29 10:55:46 +03001026 if (!skb)
1027 return;
1028
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001029 lowpan_cb(skb)->status = -EAGAIN;
1030}
1031
1032static void chan_resume_cb(struct l2cap_chan *chan)
1033{
1034 struct sk_buff *skb = chan->data;
1035
1036 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
1037
Jukka Rissanen59790aa2014-09-29 10:55:46 +03001038 if (!skb)
1039 return;
1040
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001041 lowpan_cb(skb)->status = 0;
1042}
1043
1044static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
1045{
Jukka Rissanen2ae50d82014-09-08 12:11:43 +03001046 return L2CAP_CONN_TIMEOUT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001047}
1048
1049static const struct l2cap_ops bt_6lowpan_chan_ops = {
1050 .name = "L2CAP 6LoWPAN channel",
1051 .new_connection = chan_new_conn_cb,
1052 .recv = chan_recv_cb,
1053 .close = chan_close_cb,
1054 .state_change = chan_state_change_cb,
1055 .ready = chan_ready_cb,
1056 .resume = chan_resume_cb,
1057 .suspend = chan_suspend_cb,
1058 .get_sndtimeo = chan_get_sndtimeo_cb,
1059 .alloc_skb = chan_alloc_skb_cb,
1060 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1061
1062 .teardown = l2cap_chan_no_teardown,
1063 .defer = l2cap_chan_no_defer,
1064 .set_shutdown = l2cap_chan_no_set_shutdown,
1065};
1066
1067static inline __u8 bdaddr_type(__u8 type)
1068{
1069 if (type == ADDR_LE_DEV_PUBLIC)
1070 return BDADDR_LE_PUBLIC;
1071 else
1072 return BDADDR_LE_RANDOM;
1073}
1074
1075static struct l2cap_chan *chan_get(void)
1076{
1077 struct l2cap_chan *pchan;
1078
1079 pchan = chan_create();
1080 if (!pchan)
1081 return NULL;
1082
1083 pchan->ops = &bt_6lowpan_chan_ops;
1084
1085 return pchan;
1086}
1087
1088static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
1089{
1090 struct l2cap_chan *pchan;
1091 int err;
1092
1093 pchan = chan_get();
1094 if (!pchan)
1095 return -EINVAL;
1096
1097 err = l2cap_chan_connect(pchan, cpu_to_le16(psm_6lowpan), 0,
1098 addr, dst_type);
1099
1100 BT_DBG("chan %p err %d", pchan, err);
1101 if (err < 0)
1102 l2cap_chan_put(pchan);
1103
Jukka Rissanen18722c22013-12-11 17:05:37 +02001104 return err;
1105}
1106
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001107static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
1108{
1109 struct lowpan_peer *peer;
1110
1111 BT_DBG("conn %p dst type %d", conn, dst_type);
1112
1113 peer = lookup_peer(conn);
1114 if (!peer)
1115 return -ENOENT;
1116
1117 BT_DBG("peer %p chan %p", peer, peer->chan);
1118
1119 l2cap_chan_close(peer->chan, ENOENT);
1120
1121 return 0;
1122}
1123
1124static struct l2cap_chan *bt_6lowpan_listen(void)
1125{
1126 bdaddr_t *addr = BDADDR_ANY;
1127 struct l2cap_chan *pchan;
1128 int err;
1129
1130 if (psm_6lowpan == 0)
1131 return NULL;
1132
1133 pchan = chan_get();
1134 if (!pchan)
1135 return NULL;
1136
1137 pchan->state = BT_LISTEN;
1138 pchan->src_type = BDADDR_LE_PUBLIC;
1139
1140 BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan, pchan,
1141 pchan->src_type);
1142
1143 err = l2cap_add_psm(pchan, addr, cpu_to_le16(psm_6lowpan));
1144 if (err) {
1145 l2cap_chan_put(pchan);
1146 BT_ERR("psm cannot be added err %d", err);
1147 return NULL;
1148 }
1149
1150 return pchan;
1151}
1152
1153static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
1154 struct l2cap_conn **conn)
1155{
1156 struct hci_conn *hcon;
1157 struct hci_dev *hdev;
1158 bdaddr_t *src = BDADDR_ANY;
1159 int n;
1160
1161 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1162 &addr->b[5], &addr->b[4], &addr->b[3],
1163 &addr->b[2], &addr->b[1], &addr->b[0],
1164 addr_type);
1165
1166 if (n < 7)
1167 return -EINVAL;
1168
1169 hdev = hci_get_route(addr, src);
1170 if (!hdev)
1171 return -ENOENT;
1172
1173 hci_dev_lock(hdev);
1174 hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
1175 hci_dev_unlock(hdev);
1176
1177 if (!hcon)
1178 return -ENOENT;
1179
1180 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1181
1182 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1183
1184 return 0;
1185}
1186
1187static void disconnect_all_peers(void)
1188{
Jukka Rissanen90305822014-10-28 17:16:47 +02001189 struct lowpan_dev *entry;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001190 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1191 struct list_head peers;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001192
1193 INIT_LIST_HEAD(&peers);
1194
1195 /* We make a separate list of peers as the close_cb() will
1196 * modify the device peers list so it is better not to mess
1197 * with the same list at the same time.
1198 */
1199
Jukka Rissanen90305822014-10-28 17:16:47 +02001200 rcu_read_lock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001201
Jukka Rissanen90305822014-10-28 17:16:47 +02001202 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1203 list_for_each_entry_rcu(peer, &entry->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001204 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1205 if (!new_peer)
1206 break;
1207
1208 new_peer->chan = peer->chan;
1209 INIT_LIST_HEAD(&new_peer->list);
1210
1211 list_add(&new_peer->list, &peers);
1212 }
1213 }
1214
Jukka Rissanen90305822014-10-28 17:16:47 +02001215 rcu_read_unlock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001216
Jukka Rissanen90305822014-10-28 17:16:47 +02001217 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001218 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1219 l2cap_chan_close(peer->chan, ENOENT);
Jukka Rissanen90305822014-10-28 17:16:47 +02001220
1221 list_del_rcu(&peer->list);
1222 call_rcu(&peer->rcu, peer_free);
1223
1224 module_put(THIS_MODULE);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001225 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001226 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001227}
1228
Jukka Rissanen90305822014-10-28 17:16:47 +02001229struct set_psm {
1230 struct work_struct work;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001231 u16 psm;
Jukka Rissanen90305822014-10-28 17:16:47 +02001232};
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001233
Jukka Rissanen90305822014-10-28 17:16:47 +02001234static void do_psm_set(struct work_struct *work)
1235{
1236 struct set_psm *set_psm = container_of(work, struct set_psm, work);
1237
1238 if (set_psm->psm == 0 || psm_6lowpan != set_psm->psm)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001239 /* Disconnect existing connections if 6lowpan is
1240 * disabled (psm = 0), or if psm changes.
1241 */
1242 disconnect_all_peers();
1243
Jukka Rissanen90305822014-10-28 17:16:47 +02001244 psm_6lowpan = set_psm->psm;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001245
1246 if (listen_chan) {
1247 l2cap_chan_close(listen_chan, 0);
1248 l2cap_chan_put(listen_chan);
1249 }
1250
1251 listen_chan = bt_6lowpan_listen();
1252
Jukka Rissanen90305822014-10-28 17:16:47 +02001253 kfree(set_psm);
1254}
1255
1256static int lowpan_psm_set(void *data, u64 val)
1257{
1258 struct set_psm *set_psm;
1259
1260 set_psm = kzalloc(sizeof(*set_psm), GFP_KERNEL);
1261 if (!set_psm)
1262 return -ENOMEM;
1263
1264 set_psm->psm = val;
1265 INIT_WORK(&set_psm->work, do_psm_set);
1266
1267 schedule_work(&set_psm->work);
1268
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001269 return 0;
1270}
1271
1272static int lowpan_psm_get(void *data, u64 *val)
1273{
1274 *val = psm_6lowpan;
1275 return 0;
1276}
1277
1278DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops, lowpan_psm_get,
1279 lowpan_psm_set, "%llu\n");
1280
1281static ssize_t lowpan_control_write(struct file *fp,
1282 const char __user *user_buffer,
1283 size_t count,
1284 loff_t *position)
1285{
1286 char buf[32];
1287 size_t buf_size = min(count, sizeof(buf) - 1);
1288 int ret;
1289 bdaddr_t addr;
1290 u8 addr_type;
1291 struct l2cap_conn *conn = NULL;
1292
1293 if (copy_from_user(buf, user_buffer, buf_size))
1294 return -EFAULT;
1295
1296 buf[buf_size] = '\0';
1297
1298 if (memcmp(buf, "connect ", 8) == 0) {
1299 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1300 if (ret == -EINVAL)
1301 return ret;
1302
1303 if (listen_chan) {
1304 l2cap_chan_close(listen_chan, 0);
1305 l2cap_chan_put(listen_chan);
1306 listen_chan = NULL;
1307 }
1308
1309 if (conn) {
1310 struct lowpan_peer *peer;
1311
1312 if (!is_bt_6lowpan(conn->hcon))
1313 return -EINVAL;
1314
1315 peer = lookup_peer(conn);
1316 if (peer) {
1317 BT_DBG("6LoWPAN connection already exists");
1318 return -EALREADY;
1319 }
1320
1321 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1322 &conn->hcon->dst, conn->hcon->dst_type,
1323 addr_type);
1324 }
1325
1326 ret = bt_6lowpan_connect(&addr, addr_type);
1327 if (ret < 0)
1328 return ret;
1329
1330 return count;
1331 }
1332
1333 if (memcmp(buf, "disconnect ", 11) == 0) {
1334 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1335 if (ret < 0)
1336 return ret;
1337
1338 ret = bt_6lowpan_disconnect(conn, addr_type);
1339 if (ret < 0)
1340 return ret;
1341
1342 return count;
1343 }
1344
1345 return count;
1346}
1347
1348static int lowpan_control_show(struct seq_file *f, void *ptr)
1349{
Jukka Rissanen90305822014-10-28 17:16:47 +02001350 struct lowpan_dev *entry;
1351 struct lowpan_peer *peer;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001352
Jukka Rissanen90305822014-10-28 17:16:47 +02001353 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001354
Jukka Rissanen90305822014-10-28 17:16:47 +02001355 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1356 list_for_each_entry(peer, &entry->peers, list)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001357 seq_printf(f, "%pMR (type %u)\n",
1358 &peer->chan->dst, peer->chan->dst_type);
1359 }
1360
Jukka Rissanen90305822014-10-28 17:16:47 +02001361 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001362
1363 return 0;
1364}
1365
1366static int lowpan_control_open(struct inode *inode, struct file *file)
1367{
1368 return single_open(file, lowpan_control_show, inode->i_private);
1369}
1370
1371static const struct file_operations lowpan_control_fops = {
1372 .open = lowpan_control_open,
1373 .read = seq_read,
1374 .write = lowpan_control_write,
1375 .llseek = seq_lseek,
1376 .release = single_release,
1377};
1378
Jukka Rissanen7f118252014-06-18 16:37:11 +03001379static void disconnect_devices(void)
1380{
Dan Carpenterdaac1972014-10-29 19:10:57 +03001381 struct lowpan_dev *entry, *tmp, *new_dev;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001382 struct list_head devices;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001383
1384 INIT_LIST_HEAD(&devices);
1385
1386 /* We make a separate list of devices because the unregister_netdev()
1387 * will call device_event() which will also want to modify the same
1388 * devices list.
1389 */
1390
Jukka Rissanen90305822014-10-28 17:16:47 +02001391 rcu_read_lock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001392
Jukka Rissanen90305822014-10-28 17:16:47 +02001393 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001394 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1395 if (!new_dev)
1396 break;
1397
1398 new_dev->netdev = entry->netdev;
1399 INIT_LIST_HEAD(&new_dev->list);
1400
Jukka Rissanen90305822014-10-28 17:16:47 +02001401 list_add_rcu(&new_dev->list, &devices);
Jukka Rissanen7f118252014-06-18 16:37:11 +03001402 }
1403
Jukka Rissanen90305822014-10-28 17:16:47 +02001404 rcu_read_unlock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001405
Dan Carpenterdaac1972014-10-29 19:10:57 +03001406 list_for_each_entry_safe(entry, tmp, &devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001407 ifdown(entry->netdev);
1408 BT_DBG("Unregistering netdev %s %p",
1409 entry->netdev->name, entry->netdev);
1410 unregister_netdev(entry->netdev);
1411 kfree(entry);
1412 }
1413}
1414
Jukka Rissanen18722c22013-12-11 17:05:37 +02001415static int device_event(struct notifier_block *unused,
1416 unsigned long event, void *ptr)
1417{
1418 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
Jukka Rissanen90305822014-10-28 17:16:47 +02001419 struct lowpan_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +02001420
1421 if (netdev->type != ARPHRD_6LOWPAN)
1422 return NOTIFY_DONE;
1423
1424 switch (event) {
1425 case NETDEV_UNREGISTER:
Jukka Rissanen90305822014-10-28 17:16:47 +02001426 spin_lock(&devices_lock);
1427 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +02001428 if (entry->netdev == netdev) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001429 BT_DBG("Unregistered netdev %s %p",
1430 netdev->name, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001431 list_del(&entry->list);
1432 kfree(entry);
1433 break;
1434 }
1435 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001436 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001437 break;
1438 }
1439
1440 return NOTIFY_DONE;
1441}
1442
1443static struct notifier_block bt_6lowpan_dev_notifier = {
1444 .notifier_call = device_event,
1445};
1446
Jukka Rissanen5547e482014-06-18 16:37:09 +03001447static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001448{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001449 lowpan_psm_debugfs = debugfs_create_file("6lowpan_psm", 0644,
1450 bt_debugfs, NULL,
1451 &lowpan_psm_fops);
1452 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1453 bt_debugfs, NULL,
1454 &lowpan_control_fops);
1455
Jukka Rissanen18722c22013-12-11 17:05:37 +02001456 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1457}
1458
Jukka Rissanen5547e482014-06-18 16:37:09 +03001459static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001460{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001461 debugfs_remove(lowpan_psm_debugfs);
1462 debugfs_remove(lowpan_control_debugfs);
1463
1464 if (listen_chan) {
1465 l2cap_chan_close(listen_chan, 0);
1466 l2cap_chan_put(listen_chan);
1467 }
1468
Jukka Rissanen7f118252014-06-18 16:37:11 +03001469 disconnect_devices();
1470
Jukka Rissanen18722c22013-12-11 17:05:37 +02001471 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1472}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001473
1474module_init(bt_6lowpan_init);
1475module_exit(bt_6lowpan_exit);
1476
1477MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1478MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1479MODULE_VERSION(VERSION);
1480MODULE_LICENSE("GPL");