blob: 2ec7c84c20007861f74c26d7bbda8ab508d1dc8d [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);
56static DEFINE_RWLOCK(devices_lock);
57
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 Rissanen6b8d4a62014-06-18 16:37:08 +030070 struct l2cap_chan *chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +020071
72 /* peer addresses in various formats */
73 unsigned char eui64_addr[EUI64_ADDR_LEN];
74 struct in6_addr peer_addr;
75};
76
77struct lowpan_dev {
78 struct list_head list;
79
80 struct hci_dev *hdev;
81 struct net_device *netdev;
82 struct list_head peers;
83 atomic_t peer_count; /* number of items in peers list */
84
85 struct work_struct delete_netdev;
86 struct delayed_work notify_peers;
87};
88
89static inline struct lowpan_dev *lowpan_dev(const struct net_device *netdev)
90{
91 return netdev_priv(netdev);
92}
93
94static inline void peer_add(struct lowpan_dev *dev, struct lowpan_peer *peer)
95{
96 list_add(&peer->list, &dev->peers);
97 atomic_inc(&dev->peer_count);
98}
99
100static inline bool peer_del(struct lowpan_dev *dev, struct lowpan_peer *peer)
101{
102 list_del(&peer->list);
103
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300104 module_put(THIS_MODULE);
105
Jukka Rissanen18722c22013-12-11 17:05:37 +0200106 if (atomic_dec_and_test(&dev->peer_count)) {
107 BT_DBG("last peer");
108 return true;
109 }
110
111 return false;
112}
113
114static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_dev *dev,
115 bdaddr_t *ba, __u8 type)
116{
117 struct lowpan_peer *peer, *tmp;
118
119 BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count),
120 ba, type);
121
122 list_for_each_entry_safe(peer, tmp, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300123 BT_DBG("dst addr %pMR dst type %d",
124 &peer->chan->dst, peer->chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200125
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300126 if (bacmp(&peer->chan->dst, ba))
Jukka Rissanen18722c22013-12-11 17:05:37 +0200127 continue;
128
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300129 if (type == peer->chan->dst_type)
130 return peer;
131 }
132
133 return NULL;
134}
135
136static inline struct lowpan_peer *peer_lookup_chan(struct lowpan_dev *dev,
137 struct l2cap_chan *chan)
138{
139 struct lowpan_peer *peer, *tmp;
140
141 list_for_each_entry_safe(peer, tmp, &dev->peers, list) {
142 if (peer->chan == chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200143 return peer;
144 }
145
146 return NULL;
147}
148
149static inline struct lowpan_peer *peer_lookup_conn(struct lowpan_dev *dev,
150 struct l2cap_conn *conn)
151{
152 struct lowpan_peer *peer, *tmp;
153
154 list_for_each_entry_safe(peer, tmp, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300155 if (peer->chan->conn == conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200156 return peer;
157 }
158
159 return NULL;
160}
161
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300162static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_dev *dev,
163 struct in6_addr *daddr,
164 struct sk_buff *skb)
165{
166 struct lowpan_peer *peer, *tmp;
167 struct in6_addr *nexthop;
168 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
169 int count = atomic_read(&dev->peer_count);
170
171 BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt);
172
173 /* If we have multiple 6lowpan peers, then check where we should
174 * send the packet. If only one peer exists, then we can send the
175 * packet right away.
176 */
177 if (count == 1)
178 return list_first_entry(&dev->peers, struct lowpan_peer,
179 list);
180
181 if (!rt) {
182 nexthop = &lowpan_cb(skb)->gw;
183
184 if (ipv6_addr_any(nexthop))
185 return NULL;
186 } else {
187 nexthop = rt6_nexthop(rt);
188
189 /* We need to remember the address because it is needed
190 * by bt_xmit() when sending the packet. In bt_xmit(), the
191 * destination routing info is not set.
192 */
193 memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr));
194 }
195
196 BT_DBG("gw %pI6c", nexthop);
197
198 list_for_each_entry_safe(peer, tmp, &dev->peers, list) {
199 BT_DBG("dst addr %pMR dst type %d ip %pI6c",
200 &peer->chan->dst, peer->chan->dst_type,
201 &peer->peer_addr);
202
203 if (!ipv6_addr_cmp(&peer->peer_addr, nexthop))
204 return peer;
205 }
206
207 return NULL;
208}
209
Jukka Rissanen18722c22013-12-11 17:05:37 +0200210static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
211{
212 struct lowpan_dev *entry, *tmp;
213 struct lowpan_peer *peer = NULL;
214 unsigned long flags;
215
216 read_lock_irqsave(&devices_lock, flags);
217
218 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
219 peer = peer_lookup_conn(entry, conn);
220 if (peer)
221 break;
222 }
223
224 read_unlock_irqrestore(&devices_lock, flags);
225
226 return peer;
227}
228
229static struct lowpan_dev *lookup_dev(struct l2cap_conn *conn)
230{
231 struct lowpan_dev *entry, *tmp;
232 struct lowpan_dev *dev = NULL;
233 unsigned long flags;
234
235 read_lock_irqsave(&devices_lock, flags);
236
237 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
238 if (conn->hcon->hdev == entry->hdev) {
239 dev = entry;
240 break;
241 }
242 }
243
244 read_unlock_irqrestore(&devices_lock, flags);
245
246 return dev;
247}
248
Jukka Rissanen18722c22013-12-11 17:05:37 +0200249static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
250{
251 struct sk_buff *skb_cp;
252 int ret;
253
254 skb_cp = skb_copy(skb, GFP_ATOMIC);
255 if (!skb_cp)
256 return -ENOMEM;
257
258 ret = netif_rx(skb_cp);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300259 if (ret < 0) {
260 BT_DBG("receive skb %d", ret);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200261 return NET_RX_DROP;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300262 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200263
264 return ret;
265}
266
267static int process_data(struct sk_buff *skb, struct net_device *netdev,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300268 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200269{
270 const u8 *saddr, *daddr;
271 u8 iphc0, iphc1;
272 struct lowpan_dev *dev;
273 struct lowpan_peer *peer;
274 unsigned long flags;
275
276 dev = lowpan_dev(netdev);
277
278 read_lock_irqsave(&devices_lock, flags);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300279 peer = peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200280 read_unlock_irqrestore(&devices_lock, flags);
281 if (!peer)
282 goto drop;
283
284 saddr = peer->eui64_addr;
285 daddr = dev->netdev->dev_addr;
286
287 /* at least two bytes will be used for the encoding */
288 if (skb->len < 2)
289 goto drop;
290
291 if (lowpan_fetch_skb_u8(skb, &iphc0))
292 goto drop;
293
294 if (lowpan_fetch_skb_u8(skb, &iphc1))
295 goto drop;
296
297 return lowpan_process_data(skb, netdev,
298 saddr, IEEE802154_ADDR_LONG, EUI64_ADDR_LEN,
299 daddr, IEEE802154_ADDR_LONG, EUI64_ADDR_LEN,
300 iphc0, iphc1, give_skb_to_upper);
301
302drop:
303 kfree_skb(skb);
304 return -EINVAL;
305}
306
307static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300308 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200309{
310 struct sk_buff *local_skb;
311 int ret;
312
313 if (!netif_running(dev))
314 goto drop;
315
316 if (dev->type != ARPHRD_6LOWPAN)
317 goto drop;
318
319 /* check that it's our buffer */
320 if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
321 /* Copy the packet so that the IPv6 header is
322 * properly aligned.
323 */
324 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
325 skb_tailroom(skb), GFP_ATOMIC);
326 if (!local_skb)
327 goto drop;
328
329 local_skb->protocol = htons(ETH_P_IPV6);
330 local_skb->pkt_type = PACKET_HOST;
331
332 skb_reset_network_header(local_skb);
333 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
334
335 if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
336 kfree_skb(local_skb);
337 goto drop;
338 }
339
340 dev->stats.rx_bytes += skb->len;
341 dev->stats.rx_packets++;
342
343 kfree_skb(local_skb);
344 kfree_skb(skb);
345 } else {
346 switch (skb->data[0] & 0xe0) {
347 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
348 local_skb = skb_clone(skb, GFP_ATOMIC);
349 if (!local_skb)
350 goto drop;
351
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300352 ret = process_data(local_skb, dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200353 if (ret != NET_RX_SUCCESS)
354 goto drop;
355
356 dev->stats.rx_bytes += skb->len;
357 dev->stats.rx_packets++;
358
359 kfree_skb(skb);
360 break;
361 default:
362 break;
363 }
364 }
365
366 return NET_RX_SUCCESS;
367
368drop:
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300369 dev->stats.rx_dropped++;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200370 kfree_skb(skb);
371 return NET_RX_DROP;
372}
373
374/* Packet from BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300375static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200376{
377 struct lowpan_dev *dev;
378 struct lowpan_peer *peer;
379 int err;
380
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300381 peer = lookup_peer(chan->conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200382 if (!peer)
383 return -ENOENT;
384
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300385 dev = lookup_dev(chan->conn);
Johan Hedberg30d3db42013-12-12 09:53:21 +0200386 if (!dev || !dev->netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200387 return -ENOENT;
388
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300389 err = recv_pkt(skb, dev->netdev, chan);
390 if (err) {
391 BT_DBG("recv pkt %d", err);
392 err = -EAGAIN;
393 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200394
395 return err;
396}
397
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300398static u8 get_addr_type_from_eui64(u8 byte)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200399{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300400 /* Is universal(0) or local(1) bit */
401 return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC);
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300402}
403
404static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr)
405{
406 u8 *eui64 = ip6_daddr->s6_addr + 8;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200407
408 addr->b[0] = eui64[7];
409 addr->b[1] = eui64[6];
410 addr->b[2] = eui64[5];
411 addr->b[3] = eui64[2];
412 addr->b[4] = eui64[1];
413 addr->b[5] = eui64[0];
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300414}
Jukka Rissanen18722c22013-12-11 17:05:37 +0200415
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300416static void convert_dest_bdaddr(struct in6_addr *ip6_daddr,
417 bdaddr_t *addr, u8 *addr_type)
418{
419 copy_to_bdaddr(ip6_daddr, addr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200420
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300421 /* We need to toggle the U/L bit that we got from IPv6 address
422 * so that we get the proper address and type of the BD address.
423 */
424 addr->b[5] ^= 0x02;
425
426 *addr_type = get_addr_type_from_eui64(addr->b[5]);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200427}
428
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300429static int setup_header(struct sk_buff *skb, struct net_device *netdev,
430 bdaddr_t *peer_addr, u8 *peer_addr_type)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200431{
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300432 struct in6_addr ipv6_daddr;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200433 struct lowpan_dev *dev;
434 struct lowpan_peer *peer;
435 bdaddr_t addr, *any = BDADDR_ANY;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300436 u8 *daddr = any->b;
437 int err, status = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200438
439 dev = lowpan_dev(netdev);
440
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300441 memcpy(&ipv6_daddr, &lowpan_cb(skb)->addr, sizeof(ipv6_daddr));
442
443 if (ipv6_addr_is_multicast(&ipv6_daddr)) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300444 lowpan_cb(skb)->chan = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200445 } else {
446 unsigned long flags;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300447 u8 addr_type;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200448
449 /* Get destination BT device from skb.
450 * If there is no such peer then discard the packet.
451 */
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300452 convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200453
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300454 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr,
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300455 addr_type, &ipv6_daddr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200456
457 read_lock_irqsave(&devices_lock, flags);
458 peer = peer_lookup_ba(dev, &addr, addr_type);
459 read_unlock_irqrestore(&devices_lock, flags);
460
461 if (!peer) {
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300462 /* The packet might be sent to 6lowpan interface
463 * because of routing (either via default route
464 * or user set route) so get peer according to
465 * the destination address.
466 */
467 read_lock_irqsave(&devices_lock, flags);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300468 peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300469 read_unlock_irqrestore(&devices_lock, flags);
470 if (!peer) {
471 BT_DBG("no such peer %pMR found", &addr);
472 return -ENOENT;
473 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200474 }
475
476 daddr = peer->eui64_addr;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300477 *peer_addr = addr;
478 *peer_addr_type = addr_type;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300479 lowpan_cb(skb)->chan = peer->chan;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300480
481 status = 1;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200482 }
483
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300484 lowpan_header_compress(skb, netdev, ETH_P_IPV6, daddr,
485 dev->netdev->dev_addr, skb->len);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200486
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300487 err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
488 if (err < 0)
489 return err;
490
491 return status;
492}
493
494static int header_create(struct sk_buff *skb, struct net_device *netdev,
495 unsigned short type, const void *_daddr,
496 const void *_saddr, unsigned int len)
497{
498 struct ipv6hdr *hdr;
499
500 if (type != ETH_P_IPV6)
501 return -EINVAL;
502
503 hdr = ipv6_hdr(skb);
504
505 memcpy(&lowpan_cb(skb)->addr, &hdr->daddr, sizeof(struct in6_addr));
506
507 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200508}
509
510/* Packet to BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300511static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300512 struct net_device *netdev, bool is_mcast)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200513{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300514 struct msghdr msg;
515 struct kvec iv;
516 int err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200517
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300518 /* Remember the skb so that we can send EAGAIN to the caller if
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300519 * we run out of credits. This is not done for multicast packets
520 * because we generate mcast packet in this module and are not
521 * really able to remember the skb after this packet is sent.
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300522 */
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300523 if (is_mcast)
524 chan->data = NULL;
525 else
526 chan->data = skb;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300527
528 memset(&msg, 0, sizeof(msg));
529 msg.msg_iov = (struct iovec *) &iv;
530 msg.msg_iovlen = 1;
531 iv.iov_base = skb->data;
532 iv.iov_len = skb->len;
533
534 err = l2cap_chan_send(chan, &msg, skb->len);
535 if (err > 0) {
536 netdev->stats.tx_bytes += err;
537 netdev->stats.tx_packets++;
538 return 0;
539 }
540
541 if (!err)
542 err = lowpan_cb(skb)->status;
543
544 if (err < 0) {
545 if (err == -EAGAIN)
546 netdev->stats.tx_dropped++;
547 else
548 netdev->stats.tx_errors++;
549 }
550
551 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200552}
553
554static void send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
555{
556 struct sk_buff *local_skb;
557 struct lowpan_dev *entry, *tmp;
558 unsigned long flags;
559
560 read_lock_irqsave(&devices_lock, flags);
561
562 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
563 struct lowpan_peer *pentry, *ptmp;
564 struct lowpan_dev *dev;
565
566 if (entry->netdev != netdev)
567 continue;
568
569 dev = lowpan_dev(entry->netdev);
570
571 list_for_each_entry_safe(pentry, ptmp, &dev->peers, list) {
572 local_skb = skb_clone(skb, GFP_ATOMIC);
573
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300574 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
575 netdev->name,
576 &pentry->chan->dst, pentry->chan->dst_type,
577 &pentry->peer_addr, pentry->chan);
578 send_pkt(pentry->chan, local_skb, netdev, true);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200579
580 kfree_skb(local_skb);
581 }
582 }
583
584 read_unlock_irqrestore(&devices_lock, flags);
585}
586
587static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
588{
589 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200590 bdaddr_t addr;
591 u8 addr_type;
592
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300593 /* We must take a copy of the skb before we modify/replace the ipv6
594 * header as the header could be used elsewhere
595 */
596 skb = skb_unshare(skb, GFP_ATOMIC);
597 if (!skb)
598 return NET_XMIT_DROP;
599
600 /* Return values from setup_header()
601 * <0 - error, packet is dropped
602 * 0 - this is a multicast packet
603 * 1 - this is unicast packet
604 */
605 err = setup_header(skb, netdev, &addr, &addr_type);
606 if (err < 0) {
607 kfree_skb(skb);
608 return NET_XMIT_DROP;
609 }
610
611 if (err) {
612 if (lowpan_cb(skb)->chan) {
613 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
614 netdev->name, &addr, addr_type,
615 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
616 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev,
617 false);
618 } else {
619 err = -ENOENT;
620 }
621 } else {
622 /* We need to send the packet to every device behind this
623 * interface.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200624 */
625 send_mcast_pkt(skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200626 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200627
628 if (err)
629 BT_DBG("ERROR: xmit failed (%d)", err);
630
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300631 return err < 0 ? NET_XMIT_DROP : err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200632}
633
634static const struct net_device_ops netdev_ops = {
635 .ndo_start_xmit = bt_xmit,
636};
637
638static struct header_ops header_ops = {
639 .create = header_create,
640};
641
642static void netdev_setup(struct net_device *dev)
643{
644 dev->addr_len = EUI64_ADDR_LEN;
645 dev->type = ARPHRD_6LOWPAN;
646
647 dev->hard_header_len = 0;
648 dev->needed_tailroom = 0;
649 dev->mtu = IPV6_MIN_MTU;
650 dev->tx_queue_len = 0;
651 dev->flags = IFF_RUNNING | IFF_POINTOPOINT;
652 dev->watchdog_timeo = 0;
653
654 dev->netdev_ops = &netdev_ops;
655 dev->header_ops = &header_ops;
656 dev->destructor = free_netdev;
657}
658
659static struct device_type bt_type = {
660 .name = "bluetooth",
661};
662
663static void set_addr(u8 *eui, u8 *addr, u8 addr_type)
664{
665 /* addr is the BT address in little-endian format */
666 eui[0] = addr[5];
667 eui[1] = addr[4];
668 eui[2] = addr[3];
669 eui[3] = 0xFF;
670 eui[4] = 0xFE;
671 eui[5] = addr[2];
672 eui[6] = addr[1];
673 eui[7] = addr[0];
674
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300675 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300676 if (addr_type == BDADDR_LE_PUBLIC)
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300677 eui[0] &= ~0x02;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200678 else
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300679 eui[0] |= 0x02;
680
681 BT_DBG("type %d addr %*phC", addr_type, 8, eui);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200682}
683
684static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr,
685 u8 addr_type)
686{
687 netdev->addr_assign_type = NET_ADDR_PERM;
688 set_addr(netdev->dev_addr, addr->b, addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200689}
690
691static void ifup(struct net_device *netdev)
692{
693 int err;
694
695 rtnl_lock();
696 err = dev_open(netdev);
697 if (err < 0)
698 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
699 rtnl_unlock();
700}
701
Jukka Rissanen7f118252014-06-18 16:37:11 +0300702static void ifdown(struct net_device *netdev)
703{
704 int err;
705
706 rtnl_lock();
707 err = dev_close(netdev);
708 if (err < 0)
709 BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
710 rtnl_unlock();
711}
712
Jukka Rissanen18722c22013-12-11 17:05:37 +0200713static void do_notify_peers(struct work_struct *work)
714{
715 struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
716 notify_peers.work);
717
718 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
719}
720
721static bool is_bt_6lowpan(struct hci_conn *hcon)
722{
723 if (hcon->type != LE_LINK)
724 return false;
725
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300726 if (!psm_6lowpan)
727 return false;
728
729 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200730}
731
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300732static struct l2cap_chan *chan_create(void)
733{
734 struct l2cap_chan *chan;
735
736 chan = l2cap_chan_create();
737 if (!chan)
738 return NULL;
739
740 l2cap_chan_set_defaults(chan);
741
742 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
743 chan->mode = L2CAP_MODE_LE_FLOWCTL;
744 chan->omtu = 65535;
745 chan->imtu = chan->omtu;
746
747 return chan;
748}
749
750static struct l2cap_chan *chan_open(struct l2cap_chan *pchan)
751{
752 struct l2cap_chan *chan;
753
754 chan = chan_create();
755 if (!chan)
756 return NULL;
757
758 chan->remote_mps = chan->omtu;
759 chan->mps = chan->omtu;
760
761 chan->state = BT_CONNECTED;
762
763 return chan;
764}
765
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300766static void set_ip_addr_bits(u8 addr_type, u8 *addr)
767{
768 if (addr_type == BDADDR_LE_PUBLIC)
769 *addr |= 0x02;
770 else
771 *addr &= ~0x02;
772}
773
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300774static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
775 struct lowpan_dev *dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200776{
777 struct lowpan_peer *peer;
778 unsigned long flags;
779
780 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
781 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300782 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200783
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300784 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200785 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
786
787 /* RFC 2464 ch. 5 */
788 peer->peer_addr.s6_addr[0] = 0xFE;
789 peer->peer_addr.s6_addr[1] = 0x80;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300790 set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
791 chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200792
793 memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
794 EUI64_ADDR_LEN);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200795
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300796 /* IPv6 address needs to have the U/L bit set properly so toggle
797 * it back here.
798 */
799 set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8);
800
Jukka Rissanen18722c22013-12-11 17:05:37 +0200801 write_lock_irqsave(&devices_lock, flags);
802 INIT_LIST_HEAD(&peer->list);
803 peer_add(dev, peer);
804 write_unlock_irqrestore(&devices_lock, flags);
805
806 /* Notifying peers about us needs to be done without locks held */
807 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
808 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
809
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300810 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200811}
812
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300813static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200814{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200815 struct net_device *netdev;
816 int err = 0;
817 unsigned long flags;
818
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300819 netdev = alloc_netdev(sizeof(struct lowpan_dev), IFACE_NAME_TEMPLATE,
Tom Gundersenc835a672014-07-14 16:37:24 +0200820 NET_NAME_UNKNOWN, netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200821 if (!netdev)
822 return -ENOMEM;
823
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300824 set_dev_addr(netdev, &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200825
826 netdev->netdev_ops = &netdev_ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300827 SET_NETDEV_DEV(netdev, &chan->conn->hcon->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200828 SET_NETDEV_DEVTYPE(netdev, &bt_type);
829
830 err = register_netdev(netdev);
831 if (err < 0) {
832 BT_INFO("register_netdev failed %d", err);
833 free_netdev(netdev);
834 goto out;
835 }
836
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300837 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
838 netdev->ifindex, &chan->dst, chan->dst_type,
839 &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200840 set_bit(__LINK_STATE_PRESENT, &netdev->state);
841
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300842 *dev = netdev_priv(netdev);
843 (*dev)->netdev = netdev;
844 (*dev)->hdev = chan->conn->hcon->hdev;
845 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200846
847 write_lock_irqsave(&devices_lock, flags);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300848 INIT_LIST_HEAD(&(*dev)->list);
849 list_add(&(*dev)->list, &bt_6lowpan_devices);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200850 write_unlock_irqrestore(&devices_lock, flags);
851
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300852 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200853
854out:
855 return err;
856}
857
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300858static inline void chan_ready_cb(struct l2cap_chan *chan)
859{
860 struct lowpan_dev *dev;
861
862 dev = lookup_dev(chan->conn);
863
864 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
865
866 if (!dev) {
867 if (setup_netdev(chan, &dev) < 0) {
868 l2cap_chan_del(chan, -ENOENT);
869 return;
870 }
871 }
872
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300873 if (!try_module_get(THIS_MODULE))
874 return;
875
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300876 add_peer_chan(chan, dev);
877 ifup(dev->netdev);
878}
879
Johan Hedberg2b293492014-08-07 10:03:32 +0300880static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300881{
Johan Hedberg2b293492014-08-07 10:03:32 +0300882 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300883
Johan Hedberg2b293492014-08-07 10:03:32 +0300884 chan = chan_open(pchan);
885 chan->ops = pchan->ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300886
887 BT_DBG("chan %p pchan %p", chan, pchan);
888
Johan Hedberg2b293492014-08-07 10:03:32 +0300889 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300890}
891
Jukka Rissanen18722c22013-12-11 17:05:37 +0200892static void delete_netdev(struct work_struct *work)
893{
894 struct lowpan_dev *entry = container_of(work, struct lowpan_dev,
895 delete_netdev);
896
897 unregister_netdev(entry->netdev);
898
899 /* The entry pointer is deleted in device_event() */
900}
901
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300902static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200903{
904 struct lowpan_dev *entry, *tmp;
905 struct lowpan_dev *dev = NULL;
906 struct lowpan_peer *peer;
907 int err = -ENOENT;
908 unsigned long flags;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300909 bool last = false, removed = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200910
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300911 BT_DBG("chan %p conn %p", chan, chan->conn);
912
913 if (chan->conn && chan->conn->hcon) {
914 if (!is_bt_6lowpan(chan->conn->hcon))
915 return;
916
917 /* If conn is set, then the netdev is also there and we should
918 * not remove it.
919 */
920 removed = false;
921 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200922
923 write_lock_irqsave(&devices_lock, flags);
924
925 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
926 dev = lowpan_dev(entry->netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300927 peer = peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200928 if (peer) {
929 last = peer_del(dev, peer);
930 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300931
932 BT_DBG("dev %p removing %speer %p", dev,
933 last ? "last " : "1 ", peer);
934 BT_DBG("chan %p orig refcnt %d", chan,
935 atomic_read(&chan->kref.refcount));
936
937 l2cap_chan_put(chan);
938 kfree(peer);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200939 break;
940 }
941 }
942
943 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
944 write_unlock_irqrestore(&devices_lock, flags);
945
946 cancel_delayed_work_sync(&dev->notify_peers);
947
Jukka Rissanen7f118252014-06-18 16:37:11 +0300948 ifdown(dev->netdev);
949
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300950 if (!removed) {
951 INIT_WORK(&entry->delete_netdev, delete_netdev);
952 schedule_work(&entry->delete_netdev);
953 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200954 } else {
955 write_unlock_irqrestore(&devices_lock, flags);
956 }
957
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300958 return;
959}
960
961static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
962{
963 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
964 state_to_string(state), err);
965}
966
967static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
968 unsigned long hdr_len,
969 unsigned long len, int nb)
970{
971 /* Note that we must allocate using GFP_ATOMIC here as
972 * this function is called originally from netdev hard xmit
973 * function in atomic context.
974 */
975 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
976}
977
978static void chan_suspend_cb(struct l2cap_chan *chan)
979{
980 struct sk_buff *skb = chan->data;
981
982 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
983
Jukka Rissanen59790aa2014-09-29 10:55:46 +0300984 if (!skb)
985 return;
986
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300987 lowpan_cb(skb)->status = -EAGAIN;
988}
989
990static void chan_resume_cb(struct l2cap_chan *chan)
991{
992 struct sk_buff *skb = chan->data;
993
994 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
995
Jukka Rissanen59790aa2014-09-29 10:55:46 +0300996 if (!skb)
997 return;
998
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300999 lowpan_cb(skb)->status = 0;
1000}
1001
1002static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
1003{
Jukka Rissanen2ae50d82014-09-08 12:11:43 +03001004 return L2CAP_CONN_TIMEOUT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001005}
1006
1007static const struct l2cap_ops bt_6lowpan_chan_ops = {
1008 .name = "L2CAP 6LoWPAN channel",
1009 .new_connection = chan_new_conn_cb,
1010 .recv = chan_recv_cb,
1011 .close = chan_close_cb,
1012 .state_change = chan_state_change_cb,
1013 .ready = chan_ready_cb,
1014 .resume = chan_resume_cb,
1015 .suspend = chan_suspend_cb,
1016 .get_sndtimeo = chan_get_sndtimeo_cb,
1017 .alloc_skb = chan_alloc_skb_cb,
1018 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1019
1020 .teardown = l2cap_chan_no_teardown,
1021 .defer = l2cap_chan_no_defer,
1022 .set_shutdown = l2cap_chan_no_set_shutdown,
1023};
1024
1025static inline __u8 bdaddr_type(__u8 type)
1026{
1027 if (type == ADDR_LE_DEV_PUBLIC)
1028 return BDADDR_LE_PUBLIC;
1029 else
1030 return BDADDR_LE_RANDOM;
1031}
1032
1033static struct l2cap_chan *chan_get(void)
1034{
1035 struct l2cap_chan *pchan;
1036
1037 pchan = chan_create();
1038 if (!pchan)
1039 return NULL;
1040
1041 pchan->ops = &bt_6lowpan_chan_ops;
1042
1043 return pchan;
1044}
1045
1046static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
1047{
1048 struct l2cap_chan *pchan;
1049 int err;
1050
1051 pchan = chan_get();
1052 if (!pchan)
1053 return -EINVAL;
1054
1055 err = l2cap_chan_connect(pchan, cpu_to_le16(psm_6lowpan), 0,
1056 addr, dst_type);
1057
1058 BT_DBG("chan %p err %d", pchan, err);
1059 if (err < 0)
1060 l2cap_chan_put(pchan);
1061
Jukka Rissanen18722c22013-12-11 17:05:37 +02001062 return err;
1063}
1064
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001065static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
1066{
1067 struct lowpan_peer *peer;
1068
1069 BT_DBG("conn %p dst type %d", conn, dst_type);
1070
1071 peer = lookup_peer(conn);
1072 if (!peer)
1073 return -ENOENT;
1074
1075 BT_DBG("peer %p chan %p", peer, peer->chan);
1076
1077 l2cap_chan_close(peer->chan, ENOENT);
1078
1079 return 0;
1080}
1081
1082static struct l2cap_chan *bt_6lowpan_listen(void)
1083{
1084 bdaddr_t *addr = BDADDR_ANY;
1085 struct l2cap_chan *pchan;
1086 int err;
1087
1088 if (psm_6lowpan == 0)
1089 return NULL;
1090
1091 pchan = chan_get();
1092 if (!pchan)
1093 return NULL;
1094
1095 pchan->state = BT_LISTEN;
1096 pchan->src_type = BDADDR_LE_PUBLIC;
1097
1098 BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan, pchan,
1099 pchan->src_type);
1100
1101 err = l2cap_add_psm(pchan, addr, cpu_to_le16(psm_6lowpan));
1102 if (err) {
1103 l2cap_chan_put(pchan);
1104 BT_ERR("psm cannot be added err %d", err);
1105 return NULL;
1106 }
1107
1108 return pchan;
1109}
1110
1111static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
1112 struct l2cap_conn **conn)
1113{
1114 struct hci_conn *hcon;
1115 struct hci_dev *hdev;
1116 bdaddr_t *src = BDADDR_ANY;
1117 int n;
1118
1119 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1120 &addr->b[5], &addr->b[4], &addr->b[3],
1121 &addr->b[2], &addr->b[1], &addr->b[0],
1122 addr_type);
1123
1124 if (n < 7)
1125 return -EINVAL;
1126
1127 hdev = hci_get_route(addr, src);
1128 if (!hdev)
1129 return -ENOENT;
1130
1131 hci_dev_lock(hdev);
1132 hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
1133 hci_dev_unlock(hdev);
1134
1135 if (!hcon)
1136 return -ENOENT;
1137
1138 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1139
1140 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1141
1142 return 0;
1143}
1144
1145static void disconnect_all_peers(void)
1146{
1147 struct lowpan_dev *entry, *tmp_dev;
1148 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1149 struct list_head peers;
1150 unsigned long flags;
1151
1152 INIT_LIST_HEAD(&peers);
1153
1154 /* We make a separate list of peers as the close_cb() will
1155 * modify the device peers list so it is better not to mess
1156 * with the same list at the same time.
1157 */
1158
1159 read_lock_irqsave(&devices_lock, flags);
1160
1161 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1162 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list) {
1163 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1164 if (!new_peer)
1165 break;
1166
1167 new_peer->chan = peer->chan;
1168 INIT_LIST_HEAD(&new_peer->list);
1169
1170 list_add(&new_peer->list, &peers);
1171 }
1172 }
1173
1174 read_unlock_irqrestore(&devices_lock, flags);
1175
1176 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1177 l2cap_chan_close(peer->chan, ENOENT);
1178 kfree(peer);
1179 }
1180}
1181
1182static int lowpan_psm_set(void *data, u64 val)
1183{
1184 u16 psm;
1185
1186 psm = val;
1187 if (psm == 0 || psm_6lowpan != psm)
1188 /* Disconnect existing connections if 6lowpan is
1189 * disabled (psm = 0), or if psm changes.
1190 */
1191 disconnect_all_peers();
1192
1193 psm_6lowpan = psm;
1194
1195 if (listen_chan) {
1196 l2cap_chan_close(listen_chan, 0);
1197 l2cap_chan_put(listen_chan);
1198 }
1199
1200 listen_chan = bt_6lowpan_listen();
1201
1202 return 0;
1203}
1204
1205static int lowpan_psm_get(void *data, u64 *val)
1206{
1207 *val = psm_6lowpan;
1208 return 0;
1209}
1210
1211DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops, lowpan_psm_get,
1212 lowpan_psm_set, "%llu\n");
1213
1214static ssize_t lowpan_control_write(struct file *fp,
1215 const char __user *user_buffer,
1216 size_t count,
1217 loff_t *position)
1218{
1219 char buf[32];
1220 size_t buf_size = min(count, sizeof(buf) - 1);
1221 int ret;
1222 bdaddr_t addr;
1223 u8 addr_type;
1224 struct l2cap_conn *conn = NULL;
1225
1226 if (copy_from_user(buf, user_buffer, buf_size))
1227 return -EFAULT;
1228
1229 buf[buf_size] = '\0';
1230
1231 if (memcmp(buf, "connect ", 8) == 0) {
1232 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1233 if (ret == -EINVAL)
1234 return ret;
1235
1236 if (listen_chan) {
1237 l2cap_chan_close(listen_chan, 0);
1238 l2cap_chan_put(listen_chan);
1239 listen_chan = NULL;
1240 }
1241
1242 if (conn) {
1243 struct lowpan_peer *peer;
1244
1245 if (!is_bt_6lowpan(conn->hcon))
1246 return -EINVAL;
1247
1248 peer = lookup_peer(conn);
1249 if (peer) {
1250 BT_DBG("6LoWPAN connection already exists");
1251 return -EALREADY;
1252 }
1253
1254 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1255 &conn->hcon->dst, conn->hcon->dst_type,
1256 addr_type);
1257 }
1258
1259 ret = bt_6lowpan_connect(&addr, addr_type);
1260 if (ret < 0)
1261 return ret;
1262
1263 return count;
1264 }
1265
1266 if (memcmp(buf, "disconnect ", 11) == 0) {
1267 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1268 if (ret < 0)
1269 return ret;
1270
1271 ret = bt_6lowpan_disconnect(conn, addr_type);
1272 if (ret < 0)
1273 return ret;
1274
1275 return count;
1276 }
1277
1278 return count;
1279}
1280
1281static int lowpan_control_show(struct seq_file *f, void *ptr)
1282{
1283 struct lowpan_dev *entry, *tmp_dev;
1284 struct lowpan_peer *peer, *tmp_peer;
1285 unsigned long flags;
1286
1287 read_lock_irqsave(&devices_lock, flags);
1288
1289 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1290 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list)
1291 seq_printf(f, "%pMR (type %u)\n",
1292 &peer->chan->dst, peer->chan->dst_type);
1293 }
1294
1295 read_unlock_irqrestore(&devices_lock, flags);
1296
1297 return 0;
1298}
1299
1300static int lowpan_control_open(struct inode *inode, struct file *file)
1301{
1302 return single_open(file, lowpan_control_show, inode->i_private);
1303}
1304
1305static const struct file_operations lowpan_control_fops = {
1306 .open = lowpan_control_open,
1307 .read = seq_read,
1308 .write = lowpan_control_write,
1309 .llseek = seq_lseek,
1310 .release = single_release,
1311};
1312
Jukka Rissanen7f118252014-06-18 16:37:11 +03001313static void disconnect_devices(void)
1314{
1315 struct lowpan_dev *entry, *tmp, *new_dev;
1316 struct list_head devices;
1317 unsigned long flags;
1318
1319 INIT_LIST_HEAD(&devices);
1320
1321 /* We make a separate list of devices because the unregister_netdev()
1322 * will call device_event() which will also want to modify the same
1323 * devices list.
1324 */
1325
1326 read_lock_irqsave(&devices_lock, flags);
1327
1328 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
1329 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1330 if (!new_dev)
1331 break;
1332
1333 new_dev->netdev = entry->netdev;
1334 INIT_LIST_HEAD(&new_dev->list);
1335
1336 list_add(&new_dev->list, &devices);
1337 }
1338
1339 read_unlock_irqrestore(&devices_lock, flags);
1340
1341 list_for_each_entry_safe(entry, tmp, &devices, list) {
1342 ifdown(entry->netdev);
1343 BT_DBG("Unregistering netdev %s %p",
1344 entry->netdev->name, entry->netdev);
1345 unregister_netdev(entry->netdev);
1346 kfree(entry);
1347 }
1348}
1349
Jukka Rissanen18722c22013-12-11 17:05:37 +02001350static int device_event(struct notifier_block *unused,
1351 unsigned long event, void *ptr)
1352{
1353 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1354 struct lowpan_dev *entry, *tmp;
1355 unsigned long flags;
1356
1357 if (netdev->type != ARPHRD_6LOWPAN)
1358 return NOTIFY_DONE;
1359
1360 switch (event) {
1361 case NETDEV_UNREGISTER:
1362 write_lock_irqsave(&devices_lock, flags);
1363 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices,
1364 list) {
1365 if (entry->netdev == netdev) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001366 BT_DBG("Unregistered netdev %s %p",
1367 netdev->name, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001368 list_del(&entry->list);
1369 kfree(entry);
1370 break;
1371 }
1372 }
1373 write_unlock_irqrestore(&devices_lock, flags);
1374 break;
1375 }
1376
1377 return NOTIFY_DONE;
1378}
1379
1380static struct notifier_block bt_6lowpan_dev_notifier = {
1381 .notifier_call = device_event,
1382};
1383
Jukka Rissanen5547e482014-06-18 16:37:09 +03001384static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001385{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001386 lowpan_psm_debugfs = debugfs_create_file("6lowpan_psm", 0644,
1387 bt_debugfs, NULL,
1388 &lowpan_psm_fops);
1389 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1390 bt_debugfs, NULL,
1391 &lowpan_control_fops);
1392
Jukka Rissanen18722c22013-12-11 17:05:37 +02001393 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1394}
1395
Jukka Rissanen5547e482014-06-18 16:37:09 +03001396static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001397{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001398 debugfs_remove(lowpan_psm_debugfs);
1399 debugfs_remove(lowpan_control_debugfs);
1400
1401 if (listen_chan) {
1402 l2cap_chan_close(listen_chan, 0);
1403 l2cap_chan_put(listen_chan);
1404 }
1405
Jukka Rissanen7f118252014-06-18 16:37:11 +03001406 disconnect_devices();
1407
Jukka Rissanen18722c22013-12-11 17:05:37 +02001408 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1409}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001410
1411module_init(bt_6lowpan_init);
1412module_exit(bt_6lowpan_exit);
1413
1414MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1415MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1416MODULE_VERSION(VERSION);
1417MODULE_LICENSE("GPL");