blob: dc23c55f1ab66331a478a2bcc6b0a52c803929ae [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;
Jukka Rissanena7807d732014-10-01 11:30:57 +0300617 struct sk_buff *tmpskb;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200618
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300619 /* We must take a copy of the skb before we modify/replace the ipv6
620 * header as the header could be used elsewhere
621 */
Jukka Rissanena7807d732014-10-01 11:30:57 +0300622 tmpskb = skb_unshare(skb, GFP_ATOMIC);
623 if (!tmpskb) {
624 kfree_skb(skb);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300625 return NET_XMIT_DROP;
Jukka Rissanena7807d732014-10-01 11:30:57 +0300626 }
627 skb = tmpskb;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300628
629 /* Return values from setup_header()
630 * <0 - error, packet is dropped
631 * 0 - this is a multicast packet
632 * 1 - this is unicast packet
633 */
634 err = setup_header(skb, netdev, &addr, &addr_type);
635 if (err < 0) {
636 kfree_skb(skb);
637 return NET_XMIT_DROP;
638 }
639
640 if (err) {
641 if (lowpan_cb(skb)->chan) {
642 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
643 netdev->name, &addr, addr_type,
644 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300645 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300646 } else {
647 err = -ENOENT;
648 }
649 } else {
650 /* We need to send the packet to every device behind this
651 * interface.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200652 */
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300653 err = send_mcast_pkt(skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200654 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200655
Jukka Rissanenfc125182014-10-01 11:30:26 +0300656 dev_kfree_skb(skb);
657
Jukka Rissanen18722c22013-12-11 17:05:37 +0200658 if (err)
659 BT_DBG("ERROR: xmit failed (%d)", err);
660
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300661 return err < 0 ? NET_XMIT_DROP : err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200662}
663
Jukka Rissanendf092302014-10-28 17:16:48 +0200664static struct lock_class_key bt_tx_busylock;
665static struct lock_class_key bt_netdev_xmit_lock_key;
666
667static void bt_set_lockdep_class_one(struct net_device *dev,
668 struct netdev_queue *txq,
669 void *_unused)
670{
671 lockdep_set_class(&txq->_xmit_lock, &bt_netdev_xmit_lock_key);
672}
673
674static int bt_dev_init(struct net_device *dev)
675{
676 netdev_for_each_tx_queue(dev, bt_set_lockdep_class_one, NULL);
677 dev->qdisc_tx_busylock = &bt_tx_busylock;
678
679 return 0;
680}
681
Jukka Rissanen18722c22013-12-11 17:05:37 +0200682static const struct net_device_ops netdev_ops = {
Jukka Rissanendf092302014-10-28 17:16:48 +0200683 .ndo_init = bt_dev_init,
Jukka Rissanen18722c22013-12-11 17:05:37 +0200684 .ndo_start_xmit = bt_xmit,
685};
686
687static struct header_ops header_ops = {
688 .create = header_create,
689};
690
691static void netdev_setup(struct net_device *dev)
692{
693 dev->addr_len = EUI64_ADDR_LEN;
694 dev->type = ARPHRD_6LOWPAN;
695
696 dev->hard_header_len = 0;
697 dev->needed_tailroom = 0;
698 dev->mtu = IPV6_MIN_MTU;
699 dev->tx_queue_len = 0;
Jukka Rissanen156395c2014-09-29 16:37:26 +0300700 dev->flags = IFF_RUNNING | IFF_POINTOPOINT |
701 IFF_MULTICAST;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200702 dev->watchdog_timeo = 0;
703
704 dev->netdev_ops = &netdev_ops;
705 dev->header_ops = &header_ops;
706 dev->destructor = free_netdev;
707}
708
709static struct device_type bt_type = {
710 .name = "bluetooth",
711};
712
713static void set_addr(u8 *eui, u8 *addr, u8 addr_type)
714{
715 /* addr is the BT address in little-endian format */
716 eui[0] = addr[5];
717 eui[1] = addr[4];
718 eui[2] = addr[3];
719 eui[3] = 0xFF;
720 eui[4] = 0xFE;
721 eui[5] = addr[2];
722 eui[6] = addr[1];
723 eui[7] = addr[0];
724
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300725 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300726 if (addr_type == BDADDR_LE_PUBLIC)
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300727 eui[0] &= ~0x02;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200728 else
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300729 eui[0] |= 0x02;
730
731 BT_DBG("type %d addr %*phC", addr_type, 8, eui);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200732}
733
734static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr,
735 u8 addr_type)
736{
737 netdev->addr_assign_type = NET_ADDR_PERM;
738 set_addr(netdev->dev_addr, addr->b, addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200739}
740
741static void ifup(struct net_device *netdev)
742{
743 int err;
744
745 rtnl_lock();
746 err = dev_open(netdev);
747 if (err < 0)
748 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
749 rtnl_unlock();
750}
751
Jukka Rissanen7f118252014-06-18 16:37:11 +0300752static void ifdown(struct net_device *netdev)
753{
754 int err;
755
756 rtnl_lock();
757 err = dev_close(netdev);
758 if (err < 0)
759 BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
760 rtnl_unlock();
761}
762
Jukka Rissanen18722c22013-12-11 17:05:37 +0200763static void do_notify_peers(struct work_struct *work)
764{
765 struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
766 notify_peers.work);
767
768 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
769}
770
771static bool is_bt_6lowpan(struct hci_conn *hcon)
772{
773 if (hcon->type != LE_LINK)
774 return false;
775
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300776 if (!psm_6lowpan)
777 return false;
778
779 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200780}
781
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300782static struct l2cap_chan *chan_create(void)
783{
784 struct l2cap_chan *chan;
785
786 chan = l2cap_chan_create();
787 if (!chan)
788 return NULL;
789
790 l2cap_chan_set_defaults(chan);
791
792 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
793 chan->mode = L2CAP_MODE_LE_FLOWCTL;
794 chan->omtu = 65535;
795 chan->imtu = chan->omtu;
796
797 return chan;
798}
799
800static struct l2cap_chan *chan_open(struct l2cap_chan *pchan)
801{
802 struct l2cap_chan *chan;
803
804 chan = chan_create();
805 if (!chan)
806 return NULL;
807
808 chan->remote_mps = chan->omtu;
809 chan->mps = chan->omtu;
810
811 chan->state = BT_CONNECTED;
812
813 return chan;
814}
815
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300816static void set_ip_addr_bits(u8 addr_type, u8 *addr)
817{
818 if (addr_type == BDADDR_LE_PUBLIC)
819 *addr |= 0x02;
820 else
821 *addr &= ~0x02;
822}
823
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300824static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
825 struct lowpan_dev *dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200826{
827 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200828
829 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
830 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300831 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200832
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300833 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200834 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
835
836 /* RFC 2464 ch. 5 */
837 peer->peer_addr.s6_addr[0] = 0xFE;
838 peer->peer_addr.s6_addr[1] = 0x80;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300839 set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
840 chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200841
842 memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
843 EUI64_ADDR_LEN);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200844
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300845 /* IPv6 address needs to have the U/L bit set properly so toggle
846 * it back here.
847 */
848 set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8);
849
Jukka Rissanen90305822014-10-28 17:16:47 +0200850 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200851 INIT_LIST_HEAD(&peer->list);
852 peer_add(dev, peer);
Jukka Rissanen90305822014-10-28 17:16:47 +0200853 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200854
855 /* Notifying peers about us needs to be done without locks held */
856 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
857 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
858
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300859 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200860}
861
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300862static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200863{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200864 struct net_device *netdev;
865 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200866
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300867 netdev = alloc_netdev(sizeof(struct lowpan_dev), IFACE_NAME_TEMPLATE,
Tom Gundersenc835a672014-07-14 16:37:24 +0200868 NET_NAME_UNKNOWN, netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200869 if (!netdev)
870 return -ENOMEM;
871
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300872 set_dev_addr(netdev, &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200873
874 netdev->netdev_ops = &netdev_ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300875 SET_NETDEV_DEV(netdev, &chan->conn->hcon->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200876 SET_NETDEV_DEVTYPE(netdev, &bt_type);
877
878 err = register_netdev(netdev);
879 if (err < 0) {
880 BT_INFO("register_netdev failed %d", err);
881 free_netdev(netdev);
882 goto out;
883 }
884
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300885 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
886 netdev->ifindex, &chan->dst, chan->dst_type,
887 &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200888 set_bit(__LINK_STATE_PRESENT, &netdev->state);
889
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300890 *dev = netdev_priv(netdev);
891 (*dev)->netdev = netdev;
892 (*dev)->hdev = chan->conn->hcon->hdev;
893 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200894
Jukka Rissanen90305822014-10-28 17:16:47 +0200895 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300896 INIT_LIST_HEAD(&(*dev)->list);
Jukka Rissanen90305822014-10-28 17:16:47 +0200897 list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
898 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200899
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300900 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200901
902out:
903 return err;
904}
905
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300906static inline void chan_ready_cb(struct l2cap_chan *chan)
907{
908 struct lowpan_dev *dev;
909
910 dev = lookup_dev(chan->conn);
911
912 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
913
914 if (!dev) {
915 if (setup_netdev(chan, &dev) < 0) {
916 l2cap_chan_del(chan, -ENOENT);
917 return;
918 }
919 }
920
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300921 if (!try_module_get(THIS_MODULE))
922 return;
923
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300924 add_peer_chan(chan, dev);
925 ifup(dev->netdev);
926}
927
Johan Hedberg2b293492014-08-07 10:03:32 +0300928static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300929{
Johan Hedberg2b293492014-08-07 10:03:32 +0300930 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300931
Johan Hedberg2b293492014-08-07 10:03:32 +0300932 chan = chan_open(pchan);
933 chan->ops = pchan->ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300934
935 BT_DBG("chan %p pchan %p", chan, pchan);
936
Johan Hedberg2b293492014-08-07 10:03:32 +0300937 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300938}
939
Jukka Rissanen18722c22013-12-11 17:05:37 +0200940static void delete_netdev(struct work_struct *work)
941{
942 struct lowpan_dev *entry = container_of(work, struct lowpan_dev,
943 delete_netdev);
944
945 unregister_netdev(entry->netdev);
946
947 /* The entry pointer is deleted in device_event() */
948}
949
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300950static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200951{
Jukka Rissanen90305822014-10-28 17:16:47 +0200952 struct lowpan_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200953 struct lowpan_dev *dev = NULL;
954 struct lowpan_peer *peer;
955 int err = -ENOENT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300956 bool last = false, removed = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200957
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300958 BT_DBG("chan %p conn %p", chan, chan->conn);
959
960 if (chan->conn && chan->conn->hcon) {
961 if (!is_bt_6lowpan(chan->conn->hcon))
962 return;
963
964 /* If conn is set, then the netdev is also there and we should
965 * not remove it.
966 */
967 removed = false;
968 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200969
Jukka Rissanen90305822014-10-28 17:16:47 +0200970 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200971
Jukka Rissanen90305822014-10-28 17:16:47 +0200972 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +0200973 dev = lowpan_dev(entry->netdev);
Jukka Rissanen90305822014-10-28 17:16:47 +0200974 peer = __peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200975 if (peer) {
976 last = peer_del(dev, peer);
977 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300978
979 BT_DBG("dev %p removing %speer %p", dev,
980 last ? "last " : "1 ", peer);
981 BT_DBG("chan %p orig refcnt %d", chan,
982 atomic_read(&chan->kref.refcount));
983
984 l2cap_chan_put(chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200985 break;
986 }
987 }
988
989 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
Jukka Rissanen90305822014-10-28 17:16:47 +0200990 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200991
992 cancel_delayed_work_sync(&dev->notify_peers);
993
Jukka Rissanen7f118252014-06-18 16:37:11 +0300994 ifdown(dev->netdev);
995
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300996 if (!removed) {
997 INIT_WORK(&entry->delete_netdev, delete_netdev);
998 schedule_work(&entry->delete_netdev);
999 }
Jukka Rissanen18722c22013-12-11 17:05:37 +02001000 } else {
Jukka Rissanen90305822014-10-28 17:16:47 +02001001 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001002 }
1003
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001004 return;
1005}
1006
1007static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
1008{
1009 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
1010 state_to_string(state), err);
1011}
1012
1013static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
1014 unsigned long hdr_len,
1015 unsigned long len, int nb)
1016{
1017 /* Note that we must allocate using GFP_ATOMIC here as
1018 * this function is called originally from netdev hard xmit
1019 * function in atomic context.
1020 */
1021 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
1022}
1023
1024static void chan_suspend_cb(struct l2cap_chan *chan)
1025{
1026 struct sk_buff *skb = chan->data;
1027
1028 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
1029
Jukka Rissanen59790aa2014-09-29 10:55:46 +03001030 if (!skb)
1031 return;
1032
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001033 lowpan_cb(skb)->status = -EAGAIN;
1034}
1035
1036static void chan_resume_cb(struct l2cap_chan *chan)
1037{
1038 struct sk_buff *skb = chan->data;
1039
1040 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
1041
Jukka Rissanen59790aa2014-09-29 10:55:46 +03001042 if (!skb)
1043 return;
1044
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001045 lowpan_cb(skb)->status = 0;
1046}
1047
1048static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
1049{
Jukka Rissanen2ae50d82014-09-08 12:11:43 +03001050 return L2CAP_CONN_TIMEOUT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001051}
1052
1053static const struct l2cap_ops bt_6lowpan_chan_ops = {
1054 .name = "L2CAP 6LoWPAN channel",
1055 .new_connection = chan_new_conn_cb,
1056 .recv = chan_recv_cb,
1057 .close = chan_close_cb,
1058 .state_change = chan_state_change_cb,
1059 .ready = chan_ready_cb,
1060 .resume = chan_resume_cb,
1061 .suspend = chan_suspend_cb,
1062 .get_sndtimeo = chan_get_sndtimeo_cb,
1063 .alloc_skb = chan_alloc_skb_cb,
1064 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1065
1066 .teardown = l2cap_chan_no_teardown,
1067 .defer = l2cap_chan_no_defer,
1068 .set_shutdown = l2cap_chan_no_set_shutdown,
1069};
1070
1071static inline __u8 bdaddr_type(__u8 type)
1072{
1073 if (type == ADDR_LE_DEV_PUBLIC)
1074 return BDADDR_LE_PUBLIC;
1075 else
1076 return BDADDR_LE_RANDOM;
1077}
1078
1079static struct l2cap_chan *chan_get(void)
1080{
1081 struct l2cap_chan *pchan;
1082
1083 pchan = chan_create();
1084 if (!pchan)
1085 return NULL;
1086
1087 pchan->ops = &bt_6lowpan_chan_ops;
1088
1089 return pchan;
1090}
1091
1092static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
1093{
1094 struct l2cap_chan *pchan;
1095 int err;
1096
1097 pchan = chan_get();
1098 if (!pchan)
1099 return -EINVAL;
1100
1101 err = l2cap_chan_connect(pchan, cpu_to_le16(psm_6lowpan), 0,
1102 addr, dst_type);
1103
1104 BT_DBG("chan %p err %d", pchan, err);
1105 if (err < 0)
1106 l2cap_chan_put(pchan);
1107
Jukka Rissanen18722c22013-12-11 17:05:37 +02001108 return err;
1109}
1110
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001111static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
1112{
1113 struct lowpan_peer *peer;
1114
1115 BT_DBG("conn %p dst type %d", conn, dst_type);
1116
1117 peer = lookup_peer(conn);
1118 if (!peer)
1119 return -ENOENT;
1120
1121 BT_DBG("peer %p chan %p", peer, peer->chan);
1122
1123 l2cap_chan_close(peer->chan, ENOENT);
1124
1125 return 0;
1126}
1127
1128static struct l2cap_chan *bt_6lowpan_listen(void)
1129{
1130 bdaddr_t *addr = BDADDR_ANY;
1131 struct l2cap_chan *pchan;
1132 int err;
1133
1134 if (psm_6lowpan == 0)
1135 return NULL;
1136
1137 pchan = chan_get();
1138 if (!pchan)
1139 return NULL;
1140
1141 pchan->state = BT_LISTEN;
1142 pchan->src_type = BDADDR_LE_PUBLIC;
1143
1144 BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan, pchan,
1145 pchan->src_type);
1146
1147 err = l2cap_add_psm(pchan, addr, cpu_to_le16(psm_6lowpan));
1148 if (err) {
1149 l2cap_chan_put(pchan);
1150 BT_ERR("psm cannot be added err %d", err);
1151 return NULL;
1152 }
1153
1154 return pchan;
1155}
1156
1157static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
1158 struct l2cap_conn **conn)
1159{
1160 struct hci_conn *hcon;
1161 struct hci_dev *hdev;
1162 bdaddr_t *src = BDADDR_ANY;
1163 int n;
1164
1165 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1166 &addr->b[5], &addr->b[4], &addr->b[3],
1167 &addr->b[2], &addr->b[1], &addr->b[0],
1168 addr_type);
1169
1170 if (n < 7)
1171 return -EINVAL;
1172
1173 hdev = hci_get_route(addr, src);
1174 if (!hdev)
1175 return -ENOENT;
1176
1177 hci_dev_lock(hdev);
1178 hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
1179 hci_dev_unlock(hdev);
1180
1181 if (!hcon)
1182 return -ENOENT;
1183
1184 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1185
1186 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1187
1188 return 0;
1189}
1190
1191static void disconnect_all_peers(void)
1192{
Jukka Rissanen90305822014-10-28 17:16:47 +02001193 struct lowpan_dev *entry;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001194 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1195 struct list_head peers;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001196
1197 INIT_LIST_HEAD(&peers);
1198
1199 /* We make a separate list of peers as the close_cb() will
1200 * modify the device peers list so it is better not to mess
1201 * with the same list at the same time.
1202 */
1203
Jukka Rissanen90305822014-10-28 17:16:47 +02001204 rcu_read_lock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001205
Jukka Rissanen90305822014-10-28 17:16:47 +02001206 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1207 list_for_each_entry_rcu(peer, &entry->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001208 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1209 if (!new_peer)
1210 break;
1211
1212 new_peer->chan = peer->chan;
1213 INIT_LIST_HEAD(&new_peer->list);
1214
1215 list_add(&new_peer->list, &peers);
1216 }
1217 }
1218
Jukka Rissanen90305822014-10-28 17:16:47 +02001219 rcu_read_unlock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001220
Jukka Rissanen90305822014-10-28 17:16:47 +02001221 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001222 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1223 l2cap_chan_close(peer->chan, ENOENT);
Jukka Rissanen90305822014-10-28 17:16:47 +02001224
1225 list_del_rcu(&peer->list);
1226 call_rcu(&peer->rcu, peer_free);
1227
1228 module_put(THIS_MODULE);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001229 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001230 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001231}
1232
Jukka Rissanen90305822014-10-28 17:16:47 +02001233struct set_psm {
1234 struct work_struct work;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001235 u16 psm;
Jukka Rissanen90305822014-10-28 17:16:47 +02001236};
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001237
Jukka Rissanen90305822014-10-28 17:16:47 +02001238static void do_psm_set(struct work_struct *work)
1239{
1240 struct set_psm *set_psm = container_of(work, struct set_psm, work);
1241
1242 if (set_psm->psm == 0 || psm_6lowpan != set_psm->psm)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001243 /* Disconnect existing connections if 6lowpan is
1244 * disabled (psm = 0), or if psm changes.
1245 */
1246 disconnect_all_peers();
1247
Jukka Rissanen90305822014-10-28 17:16:47 +02001248 psm_6lowpan = set_psm->psm;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001249
1250 if (listen_chan) {
1251 l2cap_chan_close(listen_chan, 0);
1252 l2cap_chan_put(listen_chan);
1253 }
1254
1255 listen_chan = bt_6lowpan_listen();
1256
Jukka Rissanen90305822014-10-28 17:16:47 +02001257 kfree(set_psm);
1258}
1259
1260static int lowpan_psm_set(void *data, u64 val)
1261{
1262 struct set_psm *set_psm;
1263
1264 set_psm = kzalloc(sizeof(*set_psm), GFP_KERNEL);
1265 if (!set_psm)
1266 return -ENOMEM;
1267
1268 set_psm->psm = val;
1269 INIT_WORK(&set_psm->work, do_psm_set);
1270
1271 schedule_work(&set_psm->work);
1272
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001273 return 0;
1274}
1275
1276static int lowpan_psm_get(void *data, u64 *val)
1277{
1278 *val = psm_6lowpan;
1279 return 0;
1280}
1281
1282DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops, lowpan_psm_get,
1283 lowpan_psm_set, "%llu\n");
1284
1285static ssize_t lowpan_control_write(struct file *fp,
1286 const char __user *user_buffer,
1287 size_t count,
1288 loff_t *position)
1289{
1290 char buf[32];
1291 size_t buf_size = min(count, sizeof(buf) - 1);
1292 int ret;
1293 bdaddr_t addr;
1294 u8 addr_type;
1295 struct l2cap_conn *conn = NULL;
1296
1297 if (copy_from_user(buf, user_buffer, buf_size))
1298 return -EFAULT;
1299
1300 buf[buf_size] = '\0';
1301
1302 if (memcmp(buf, "connect ", 8) == 0) {
1303 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1304 if (ret == -EINVAL)
1305 return ret;
1306
1307 if (listen_chan) {
1308 l2cap_chan_close(listen_chan, 0);
1309 l2cap_chan_put(listen_chan);
1310 listen_chan = NULL;
1311 }
1312
1313 if (conn) {
1314 struct lowpan_peer *peer;
1315
1316 if (!is_bt_6lowpan(conn->hcon))
1317 return -EINVAL;
1318
1319 peer = lookup_peer(conn);
1320 if (peer) {
1321 BT_DBG("6LoWPAN connection already exists");
1322 return -EALREADY;
1323 }
1324
1325 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1326 &conn->hcon->dst, conn->hcon->dst_type,
1327 addr_type);
1328 }
1329
1330 ret = bt_6lowpan_connect(&addr, addr_type);
1331 if (ret < 0)
1332 return ret;
1333
1334 return count;
1335 }
1336
1337 if (memcmp(buf, "disconnect ", 11) == 0) {
1338 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1339 if (ret < 0)
1340 return ret;
1341
1342 ret = bt_6lowpan_disconnect(conn, addr_type);
1343 if (ret < 0)
1344 return ret;
1345
1346 return count;
1347 }
1348
1349 return count;
1350}
1351
1352static int lowpan_control_show(struct seq_file *f, void *ptr)
1353{
Jukka Rissanen90305822014-10-28 17:16:47 +02001354 struct lowpan_dev *entry;
1355 struct lowpan_peer *peer;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001356
Jukka Rissanen90305822014-10-28 17:16:47 +02001357 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001358
Jukka Rissanen90305822014-10-28 17:16:47 +02001359 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1360 list_for_each_entry(peer, &entry->peers, list)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001361 seq_printf(f, "%pMR (type %u)\n",
1362 &peer->chan->dst, peer->chan->dst_type);
1363 }
1364
Jukka Rissanen90305822014-10-28 17:16:47 +02001365 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001366
1367 return 0;
1368}
1369
1370static int lowpan_control_open(struct inode *inode, struct file *file)
1371{
1372 return single_open(file, lowpan_control_show, inode->i_private);
1373}
1374
1375static const struct file_operations lowpan_control_fops = {
1376 .open = lowpan_control_open,
1377 .read = seq_read,
1378 .write = lowpan_control_write,
1379 .llseek = seq_lseek,
1380 .release = single_release,
1381};
1382
Jukka Rissanen7f118252014-06-18 16:37:11 +03001383static void disconnect_devices(void)
1384{
Dan Carpenterdaac1972014-10-29 19:10:57 +03001385 struct lowpan_dev *entry, *tmp, *new_dev;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001386 struct list_head devices;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001387
1388 INIT_LIST_HEAD(&devices);
1389
1390 /* We make a separate list of devices because the unregister_netdev()
1391 * will call device_event() which will also want to modify the same
1392 * devices list.
1393 */
1394
Jukka Rissanen90305822014-10-28 17:16:47 +02001395 rcu_read_lock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001396
Jukka Rissanen90305822014-10-28 17:16:47 +02001397 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001398 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1399 if (!new_dev)
1400 break;
1401
1402 new_dev->netdev = entry->netdev;
1403 INIT_LIST_HEAD(&new_dev->list);
1404
Jukka Rissanen90305822014-10-28 17:16:47 +02001405 list_add_rcu(&new_dev->list, &devices);
Jukka Rissanen7f118252014-06-18 16:37:11 +03001406 }
1407
Jukka Rissanen90305822014-10-28 17:16:47 +02001408 rcu_read_unlock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001409
Dan Carpenterdaac1972014-10-29 19:10:57 +03001410 list_for_each_entry_safe(entry, tmp, &devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001411 ifdown(entry->netdev);
1412 BT_DBG("Unregistering netdev %s %p",
1413 entry->netdev->name, entry->netdev);
1414 unregister_netdev(entry->netdev);
1415 kfree(entry);
1416 }
1417}
1418
Jukka Rissanen18722c22013-12-11 17:05:37 +02001419static int device_event(struct notifier_block *unused,
1420 unsigned long event, void *ptr)
1421{
1422 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
Jukka Rissanen90305822014-10-28 17:16:47 +02001423 struct lowpan_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +02001424
1425 if (netdev->type != ARPHRD_6LOWPAN)
1426 return NOTIFY_DONE;
1427
1428 switch (event) {
1429 case NETDEV_UNREGISTER:
Jukka Rissanen90305822014-10-28 17:16:47 +02001430 spin_lock(&devices_lock);
1431 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +02001432 if (entry->netdev == netdev) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001433 BT_DBG("Unregistered netdev %s %p",
1434 netdev->name, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001435 list_del(&entry->list);
1436 kfree(entry);
1437 break;
1438 }
1439 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001440 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001441 break;
1442 }
1443
1444 return NOTIFY_DONE;
1445}
1446
1447static struct notifier_block bt_6lowpan_dev_notifier = {
1448 .notifier_call = device_event,
1449};
1450
Jukka Rissanen5547e482014-06-18 16:37:09 +03001451static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001452{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001453 lowpan_psm_debugfs = debugfs_create_file("6lowpan_psm", 0644,
1454 bt_debugfs, NULL,
1455 &lowpan_psm_fops);
1456 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1457 bt_debugfs, NULL,
1458 &lowpan_control_fops);
1459
Jukka Rissanen18722c22013-12-11 17:05:37 +02001460 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1461}
1462
Jukka Rissanen5547e482014-06-18 16:37:09 +03001463static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001464{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001465 debugfs_remove(lowpan_psm_debugfs);
1466 debugfs_remove(lowpan_control_debugfs);
1467
1468 if (listen_chan) {
1469 l2cap_chan_close(listen_chan, 0);
1470 l2cap_chan_put(listen_chan);
1471 }
1472
Jukka Rissanen7f118252014-06-18 16:37:11 +03001473 disconnect_devices();
1474
Jukka Rissanen18722c22013-12-11 17:05:37 +02001475 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1476}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001477
1478module_init(bt_6lowpan_init);
1479module_exit(bt_6lowpan_exit);
1480
1481MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1482MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1483MODULE_VERSION(VERSION);
1484MODULE_LICENSE("GPL");