blob: 94bbb6611bc5b4095126efc30cb32ad3dc993c16 [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;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200252
253 skb_cp = skb_copy(skb, GFP_ATOMIC);
254 if (!skb_cp)
Martin Townsendf8b36172014-10-23 15:40:53 +0100255 return NET_RX_DROP;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200256
Li RongQing4456c502014-10-16 10:21:55 +0800257 return netif_rx(skb_cp);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200258}
259
260static int process_data(struct sk_buff *skb, struct net_device *netdev,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300261 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200262{
263 const u8 *saddr, *daddr;
264 u8 iphc0, iphc1;
265 struct lowpan_dev *dev;
266 struct lowpan_peer *peer;
267 unsigned long flags;
268
269 dev = lowpan_dev(netdev);
270
271 read_lock_irqsave(&devices_lock, flags);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300272 peer = peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200273 read_unlock_irqrestore(&devices_lock, flags);
274 if (!peer)
275 goto drop;
276
277 saddr = peer->eui64_addr;
278 daddr = dev->netdev->dev_addr;
279
280 /* at least two bytes will be used for the encoding */
281 if (skb->len < 2)
282 goto drop;
283
284 if (lowpan_fetch_skb_u8(skb, &iphc0))
285 goto drop;
286
287 if (lowpan_fetch_skb_u8(skb, &iphc1))
288 goto drop;
289
290 return lowpan_process_data(skb, netdev,
291 saddr, IEEE802154_ADDR_LONG, EUI64_ADDR_LEN,
292 daddr, IEEE802154_ADDR_LONG, EUI64_ADDR_LEN,
Martin Townsendf8b36172014-10-23 15:40:53 +0100293 iphc0, iphc1);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200294
295drop:
296 kfree_skb(skb);
297 return -EINVAL;
298}
299
300static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300301 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200302{
303 struct sk_buff *local_skb;
304 int ret;
305
306 if (!netif_running(dev))
307 goto drop;
308
309 if (dev->type != ARPHRD_6LOWPAN)
310 goto drop;
311
Martin Townsend11e3ff72014-10-13 11:00:56 +0100312 skb = skb_share_check(skb, GFP_ATOMIC);
313 if (!skb)
314 goto drop;
315
Jukka Rissanen18722c22013-12-11 17:05:37 +0200316 /* check that it's our buffer */
317 if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
318 /* Copy the packet so that the IPv6 header is
319 * properly aligned.
320 */
321 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
322 skb_tailroom(skb), GFP_ATOMIC);
323 if (!local_skb)
324 goto drop;
325
326 local_skb->protocol = htons(ETH_P_IPV6);
327 local_skb->pkt_type = PACKET_HOST;
328
329 skb_reset_network_header(local_skb);
330 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
331
332 if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
333 kfree_skb(local_skb);
334 goto drop;
335 }
336
337 dev->stats.rx_bytes += skb->len;
338 dev->stats.rx_packets++;
339
340 kfree_skb(local_skb);
341 kfree_skb(skb);
342 } else {
343 switch (skb->data[0] & 0xe0) {
344 case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
345 local_skb = skb_clone(skb, GFP_ATOMIC);
346 if (!local_skb)
347 goto drop;
348
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300349 ret = process_data(local_skb, dev, chan);
Martin Townsend04dfd732014-10-23 15:40:54 +0100350 if (ret < 0)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200351 goto drop;
352
Martin Townsendf8b36172014-10-23 15:40:53 +0100353 local_skb->protocol = htons(ETH_P_IPV6);
354 local_skb->pkt_type = PACKET_HOST;
355 local_skb->dev = dev;
356
357 if (give_skb_to_upper(local_skb, dev)
358 != NET_RX_SUCCESS) {
359 kfree_skb(local_skb);
360 goto drop;
361 }
362
Jukka Rissanen18722c22013-12-11 17:05:37 +0200363 dev->stats.rx_bytes += skb->len;
364 dev->stats.rx_packets++;
365
366 kfree_skb(skb);
367 break;
368 default:
369 break;
370 }
371 }
372
373 return NET_RX_SUCCESS;
374
375drop:
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300376 dev->stats.rx_dropped++;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200377 kfree_skb(skb);
378 return NET_RX_DROP;
379}
380
381/* Packet from BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300382static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200383{
384 struct lowpan_dev *dev;
385 struct lowpan_peer *peer;
386 int err;
387
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300388 peer = lookup_peer(chan->conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200389 if (!peer)
390 return -ENOENT;
391
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300392 dev = lookup_dev(chan->conn);
Johan Hedberg30d3db42013-12-12 09:53:21 +0200393 if (!dev || !dev->netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200394 return -ENOENT;
395
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300396 err = recv_pkt(skb, dev->netdev, chan);
397 if (err) {
398 BT_DBG("recv pkt %d", err);
399 err = -EAGAIN;
400 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200401
402 return err;
403}
404
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300405static u8 get_addr_type_from_eui64(u8 byte)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200406{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300407 /* Is universal(0) or local(1) bit */
408 return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC);
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300409}
410
411static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr)
412{
413 u8 *eui64 = ip6_daddr->s6_addr + 8;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200414
415 addr->b[0] = eui64[7];
416 addr->b[1] = eui64[6];
417 addr->b[2] = eui64[5];
418 addr->b[3] = eui64[2];
419 addr->b[4] = eui64[1];
420 addr->b[5] = eui64[0];
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300421}
Jukka Rissanen18722c22013-12-11 17:05:37 +0200422
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300423static void convert_dest_bdaddr(struct in6_addr *ip6_daddr,
424 bdaddr_t *addr, u8 *addr_type)
425{
426 copy_to_bdaddr(ip6_daddr, addr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200427
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300428 /* We need to toggle the U/L bit that we got from IPv6 address
429 * so that we get the proper address and type of the BD address.
430 */
431 addr->b[5] ^= 0x02;
432
433 *addr_type = get_addr_type_from_eui64(addr->b[5]);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200434}
435
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300436static int setup_header(struct sk_buff *skb, struct net_device *netdev,
437 bdaddr_t *peer_addr, u8 *peer_addr_type)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200438{
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300439 struct in6_addr ipv6_daddr;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200440 struct lowpan_dev *dev;
441 struct lowpan_peer *peer;
442 bdaddr_t addr, *any = BDADDR_ANY;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300443 u8 *daddr = any->b;
444 int err, status = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200445
446 dev = lowpan_dev(netdev);
447
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300448 memcpy(&ipv6_daddr, &lowpan_cb(skb)->addr, sizeof(ipv6_daddr));
449
450 if (ipv6_addr_is_multicast(&ipv6_daddr)) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300451 lowpan_cb(skb)->chan = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200452 } else {
453 unsigned long flags;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300454 u8 addr_type;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200455
456 /* Get destination BT device from skb.
457 * If there is no such peer then discard the packet.
458 */
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300459 convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200460
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300461 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr,
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300462 addr_type, &ipv6_daddr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200463
464 read_lock_irqsave(&devices_lock, flags);
465 peer = peer_lookup_ba(dev, &addr, addr_type);
466 read_unlock_irqrestore(&devices_lock, flags);
467
468 if (!peer) {
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300469 /* The packet might be sent to 6lowpan interface
470 * because of routing (either via default route
471 * or user set route) so get peer according to
472 * the destination address.
473 */
474 read_lock_irqsave(&devices_lock, flags);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300475 peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300476 read_unlock_irqrestore(&devices_lock, flags);
477 if (!peer) {
478 BT_DBG("no such peer %pMR found", &addr);
479 return -ENOENT;
480 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200481 }
482
483 daddr = peer->eui64_addr;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300484 *peer_addr = addr;
485 *peer_addr_type = addr_type;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300486 lowpan_cb(skb)->chan = peer->chan;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300487
488 status = 1;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200489 }
490
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300491 lowpan_header_compress(skb, netdev, ETH_P_IPV6, daddr,
492 dev->netdev->dev_addr, skb->len);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200493
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300494 err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
495 if (err < 0)
496 return err;
497
498 return status;
499}
500
501static int header_create(struct sk_buff *skb, struct net_device *netdev,
502 unsigned short type, const void *_daddr,
503 const void *_saddr, unsigned int len)
504{
505 struct ipv6hdr *hdr;
506
507 if (type != ETH_P_IPV6)
508 return -EINVAL;
509
510 hdr = ipv6_hdr(skb);
511
512 memcpy(&lowpan_cb(skb)->addr, &hdr->daddr, sizeof(struct in6_addr));
513
514 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200515}
516
517/* Packet to BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300518static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300519 struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200520{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300521 struct msghdr msg;
522 struct kvec iv;
523 int err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200524
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300525 /* Remember the skb so that we can send EAGAIN to the caller if
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300526 * we run out of credits.
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300527 */
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300528 chan->data = skb;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300529
530 memset(&msg, 0, sizeof(msg));
531 msg.msg_iov = (struct iovec *) &iv;
532 msg.msg_iovlen = 1;
533 iv.iov_base = skb->data;
534 iv.iov_len = skb->len;
535
536 err = l2cap_chan_send(chan, &msg, skb->len);
537 if (err > 0) {
538 netdev->stats.tx_bytes += err;
539 netdev->stats.tx_packets++;
540 return 0;
541 }
542
543 if (!err)
544 err = lowpan_cb(skb)->status;
545
546 if (err < 0) {
547 if (err == -EAGAIN)
548 netdev->stats.tx_dropped++;
549 else
550 netdev->stats.tx_errors++;
551 }
552
553 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200554}
555
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300556static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200557{
558 struct sk_buff *local_skb;
559 struct lowpan_dev *entry, *tmp;
560 unsigned long flags;
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300561 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200562
563 read_lock_irqsave(&devices_lock, flags);
564
565 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
566 struct lowpan_peer *pentry, *ptmp;
567 struct lowpan_dev *dev;
568
569 if (entry->netdev != netdev)
570 continue;
571
572 dev = lowpan_dev(entry->netdev);
573
574 list_for_each_entry_safe(pentry, ptmp, &dev->peers, list) {
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300575 int ret;
576
Jukka Rissanen18722c22013-12-11 17:05:37 +0200577 local_skb = skb_clone(skb, GFP_ATOMIC);
578
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300579 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
580 netdev->name,
581 &pentry->chan->dst, pentry->chan->dst_type,
582 &pentry->peer_addr, pentry->chan);
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300583 ret = send_pkt(pentry->chan, local_skb, netdev);
584 if (ret < 0)
585 err = ret;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200586
587 kfree_skb(local_skb);
588 }
589 }
590
591 read_unlock_irqrestore(&devices_lock, flags);
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300592
593 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200594}
595
596static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
597{
598 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200599 bdaddr_t addr;
600 u8 addr_type;
Jukka Rissanena7807d732014-10-01 11:30:57 +0300601 struct sk_buff *tmpskb;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200602
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300603 /* We must take a copy of the skb before we modify/replace the ipv6
604 * header as the header could be used elsewhere
605 */
Jukka Rissanena7807d732014-10-01 11:30:57 +0300606 tmpskb = skb_unshare(skb, GFP_ATOMIC);
607 if (!tmpskb) {
608 kfree_skb(skb);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300609 return NET_XMIT_DROP;
Jukka Rissanena7807d732014-10-01 11:30:57 +0300610 }
611 skb = tmpskb;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300612
613 /* Return values from setup_header()
614 * <0 - error, packet is dropped
615 * 0 - this is a multicast packet
616 * 1 - this is unicast packet
617 */
618 err = setup_header(skb, netdev, &addr, &addr_type);
619 if (err < 0) {
620 kfree_skb(skb);
621 return NET_XMIT_DROP;
622 }
623
624 if (err) {
625 if (lowpan_cb(skb)->chan) {
626 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
627 netdev->name, &addr, addr_type,
628 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300629 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300630 } else {
631 err = -ENOENT;
632 }
633 } else {
634 /* We need to send the packet to every device behind this
635 * interface.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200636 */
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300637 err = send_mcast_pkt(skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200638 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200639
Jukka Rissanenfc125182014-10-01 11:30:26 +0300640 dev_kfree_skb(skb);
641
Jukka Rissanen18722c22013-12-11 17:05:37 +0200642 if (err)
643 BT_DBG("ERROR: xmit failed (%d)", err);
644
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300645 return err < 0 ? NET_XMIT_DROP : err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200646}
647
648static const struct net_device_ops netdev_ops = {
649 .ndo_start_xmit = bt_xmit,
650};
651
652static struct header_ops header_ops = {
653 .create = header_create,
654};
655
656static void netdev_setup(struct net_device *dev)
657{
658 dev->addr_len = EUI64_ADDR_LEN;
659 dev->type = ARPHRD_6LOWPAN;
660
661 dev->hard_header_len = 0;
662 dev->needed_tailroom = 0;
663 dev->mtu = IPV6_MIN_MTU;
664 dev->tx_queue_len = 0;
Jukka Rissanen156395c2014-09-29 16:37:26 +0300665 dev->flags = IFF_RUNNING | IFF_POINTOPOINT |
666 IFF_MULTICAST;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200667 dev->watchdog_timeo = 0;
668
669 dev->netdev_ops = &netdev_ops;
670 dev->header_ops = &header_ops;
671 dev->destructor = free_netdev;
672}
673
674static struct device_type bt_type = {
675 .name = "bluetooth",
676};
677
678static void set_addr(u8 *eui, u8 *addr, u8 addr_type)
679{
680 /* addr is the BT address in little-endian format */
681 eui[0] = addr[5];
682 eui[1] = addr[4];
683 eui[2] = addr[3];
684 eui[3] = 0xFF;
685 eui[4] = 0xFE;
686 eui[5] = addr[2];
687 eui[6] = addr[1];
688 eui[7] = addr[0];
689
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300690 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300691 if (addr_type == BDADDR_LE_PUBLIC)
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300692 eui[0] &= ~0x02;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200693 else
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300694 eui[0] |= 0x02;
695
696 BT_DBG("type %d addr %*phC", addr_type, 8, eui);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200697}
698
699static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr,
700 u8 addr_type)
701{
702 netdev->addr_assign_type = NET_ADDR_PERM;
703 set_addr(netdev->dev_addr, addr->b, addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200704}
705
706static void ifup(struct net_device *netdev)
707{
708 int err;
709
710 rtnl_lock();
711 err = dev_open(netdev);
712 if (err < 0)
713 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
714 rtnl_unlock();
715}
716
Jukka Rissanen7f118252014-06-18 16:37:11 +0300717static void ifdown(struct net_device *netdev)
718{
719 int err;
720
721 rtnl_lock();
722 err = dev_close(netdev);
723 if (err < 0)
724 BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
725 rtnl_unlock();
726}
727
Jukka Rissanen18722c22013-12-11 17:05:37 +0200728static void do_notify_peers(struct work_struct *work)
729{
730 struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
731 notify_peers.work);
732
733 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
734}
735
736static bool is_bt_6lowpan(struct hci_conn *hcon)
737{
738 if (hcon->type != LE_LINK)
739 return false;
740
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300741 if (!psm_6lowpan)
742 return false;
743
744 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200745}
746
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300747static struct l2cap_chan *chan_create(void)
748{
749 struct l2cap_chan *chan;
750
751 chan = l2cap_chan_create();
752 if (!chan)
753 return NULL;
754
755 l2cap_chan_set_defaults(chan);
756
757 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
758 chan->mode = L2CAP_MODE_LE_FLOWCTL;
759 chan->omtu = 65535;
760 chan->imtu = chan->omtu;
761
762 return chan;
763}
764
765static struct l2cap_chan *chan_open(struct l2cap_chan *pchan)
766{
767 struct l2cap_chan *chan;
768
769 chan = chan_create();
770 if (!chan)
771 return NULL;
772
773 chan->remote_mps = chan->omtu;
774 chan->mps = chan->omtu;
775
776 chan->state = BT_CONNECTED;
777
778 return chan;
779}
780
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300781static void set_ip_addr_bits(u8 addr_type, u8 *addr)
782{
783 if (addr_type == BDADDR_LE_PUBLIC)
784 *addr |= 0x02;
785 else
786 *addr &= ~0x02;
787}
788
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300789static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
790 struct lowpan_dev *dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200791{
792 struct lowpan_peer *peer;
793 unsigned long flags;
794
795 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
796 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300797 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200798
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300799 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200800 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
801
802 /* RFC 2464 ch. 5 */
803 peer->peer_addr.s6_addr[0] = 0xFE;
804 peer->peer_addr.s6_addr[1] = 0x80;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300805 set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
806 chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200807
808 memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
809 EUI64_ADDR_LEN);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200810
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300811 /* IPv6 address needs to have the U/L bit set properly so toggle
812 * it back here.
813 */
814 set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8);
815
Jukka Rissanen18722c22013-12-11 17:05:37 +0200816 write_lock_irqsave(&devices_lock, flags);
817 INIT_LIST_HEAD(&peer->list);
818 peer_add(dev, peer);
819 write_unlock_irqrestore(&devices_lock, flags);
820
821 /* Notifying peers about us needs to be done without locks held */
822 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
823 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
824
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300825 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200826}
827
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300828static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200829{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200830 struct net_device *netdev;
831 int err = 0;
832 unsigned long flags;
833
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300834 netdev = alloc_netdev(sizeof(struct lowpan_dev), IFACE_NAME_TEMPLATE,
Tom Gundersenc835a672014-07-14 16:37:24 +0200835 NET_NAME_UNKNOWN, netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200836 if (!netdev)
837 return -ENOMEM;
838
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300839 set_dev_addr(netdev, &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200840
841 netdev->netdev_ops = &netdev_ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300842 SET_NETDEV_DEV(netdev, &chan->conn->hcon->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200843 SET_NETDEV_DEVTYPE(netdev, &bt_type);
844
845 err = register_netdev(netdev);
846 if (err < 0) {
847 BT_INFO("register_netdev failed %d", err);
848 free_netdev(netdev);
849 goto out;
850 }
851
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300852 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
853 netdev->ifindex, &chan->dst, chan->dst_type,
854 &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200855 set_bit(__LINK_STATE_PRESENT, &netdev->state);
856
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300857 *dev = netdev_priv(netdev);
858 (*dev)->netdev = netdev;
859 (*dev)->hdev = chan->conn->hcon->hdev;
860 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200861
862 write_lock_irqsave(&devices_lock, flags);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300863 INIT_LIST_HEAD(&(*dev)->list);
864 list_add(&(*dev)->list, &bt_6lowpan_devices);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200865 write_unlock_irqrestore(&devices_lock, flags);
866
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300867 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200868
869out:
870 return err;
871}
872
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300873static inline void chan_ready_cb(struct l2cap_chan *chan)
874{
875 struct lowpan_dev *dev;
876
877 dev = lookup_dev(chan->conn);
878
879 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
880
881 if (!dev) {
882 if (setup_netdev(chan, &dev) < 0) {
883 l2cap_chan_del(chan, -ENOENT);
884 return;
885 }
886 }
887
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300888 if (!try_module_get(THIS_MODULE))
889 return;
890
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300891 add_peer_chan(chan, dev);
892 ifup(dev->netdev);
893}
894
Johan Hedberg2b293492014-08-07 10:03:32 +0300895static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300896{
Johan Hedberg2b293492014-08-07 10:03:32 +0300897 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300898
Johan Hedberg2b293492014-08-07 10:03:32 +0300899 chan = chan_open(pchan);
900 chan->ops = pchan->ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300901
902 BT_DBG("chan %p pchan %p", chan, pchan);
903
Johan Hedberg2b293492014-08-07 10:03:32 +0300904 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300905}
906
Jukka Rissanen18722c22013-12-11 17:05:37 +0200907static void delete_netdev(struct work_struct *work)
908{
909 struct lowpan_dev *entry = container_of(work, struct lowpan_dev,
910 delete_netdev);
911
912 unregister_netdev(entry->netdev);
913
914 /* The entry pointer is deleted in device_event() */
915}
916
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300917static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200918{
919 struct lowpan_dev *entry, *tmp;
920 struct lowpan_dev *dev = NULL;
921 struct lowpan_peer *peer;
922 int err = -ENOENT;
923 unsigned long flags;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300924 bool last = false, removed = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200925
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300926 BT_DBG("chan %p conn %p", chan, chan->conn);
927
928 if (chan->conn && chan->conn->hcon) {
929 if (!is_bt_6lowpan(chan->conn->hcon))
930 return;
931
932 /* If conn is set, then the netdev is also there and we should
933 * not remove it.
934 */
935 removed = false;
936 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200937
938 write_lock_irqsave(&devices_lock, flags);
939
940 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
941 dev = lowpan_dev(entry->netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300942 peer = peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200943 if (peer) {
944 last = peer_del(dev, peer);
945 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300946
947 BT_DBG("dev %p removing %speer %p", dev,
948 last ? "last " : "1 ", peer);
949 BT_DBG("chan %p orig refcnt %d", chan,
950 atomic_read(&chan->kref.refcount));
951
952 l2cap_chan_put(chan);
953 kfree(peer);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200954 break;
955 }
956 }
957
958 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
959 write_unlock_irqrestore(&devices_lock, flags);
960
961 cancel_delayed_work_sync(&dev->notify_peers);
962
Jukka Rissanen7f118252014-06-18 16:37:11 +0300963 ifdown(dev->netdev);
964
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300965 if (!removed) {
966 INIT_WORK(&entry->delete_netdev, delete_netdev);
967 schedule_work(&entry->delete_netdev);
968 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200969 } else {
970 write_unlock_irqrestore(&devices_lock, flags);
971 }
972
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300973 return;
974}
975
976static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
977{
978 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
979 state_to_string(state), err);
980}
981
982static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
983 unsigned long hdr_len,
984 unsigned long len, int nb)
985{
986 /* Note that we must allocate using GFP_ATOMIC here as
987 * this function is called originally from netdev hard xmit
988 * function in atomic context.
989 */
990 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
991}
992
993static void chan_suspend_cb(struct l2cap_chan *chan)
994{
995 struct sk_buff *skb = chan->data;
996
997 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
998
Jukka Rissanen59790aa2014-09-29 10:55:46 +0300999 if (!skb)
1000 return;
1001
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001002 lowpan_cb(skb)->status = -EAGAIN;
1003}
1004
1005static void chan_resume_cb(struct l2cap_chan *chan)
1006{
1007 struct sk_buff *skb = chan->data;
1008
1009 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
1010
Jukka Rissanen59790aa2014-09-29 10:55:46 +03001011 if (!skb)
1012 return;
1013
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001014 lowpan_cb(skb)->status = 0;
1015}
1016
1017static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
1018{
Jukka Rissanen2ae50d82014-09-08 12:11:43 +03001019 return L2CAP_CONN_TIMEOUT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001020}
1021
1022static const struct l2cap_ops bt_6lowpan_chan_ops = {
1023 .name = "L2CAP 6LoWPAN channel",
1024 .new_connection = chan_new_conn_cb,
1025 .recv = chan_recv_cb,
1026 .close = chan_close_cb,
1027 .state_change = chan_state_change_cb,
1028 .ready = chan_ready_cb,
1029 .resume = chan_resume_cb,
1030 .suspend = chan_suspend_cb,
1031 .get_sndtimeo = chan_get_sndtimeo_cb,
1032 .alloc_skb = chan_alloc_skb_cb,
1033 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1034
1035 .teardown = l2cap_chan_no_teardown,
1036 .defer = l2cap_chan_no_defer,
1037 .set_shutdown = l2cap_chan_no_set_shutdown,
1038};
1039
1040static inline __u8 bdaddr_type(__u8 type)
1041{
1042 if (type == ADDR_LE_DEV_PUBLIC)
1043 return BDADDR_LE_PUBLIC;
1044 else
1045 return BDADDR_LE_RANDOM;
1046}
1047
1048static struct l2cap_chan *chan_get(void)
1049{
1050 struct l2cap_chan *pchan;
1051
1052 pchan = chan_create();
1053 if (!pchan)
1054 return NULL;
1055
1056 pchan->ops = &bt_6lowpan_chan_ops;
1057
1058 return pchan;
1059}
1060
1061static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
1062{
1063 struct l2cap_chan *pchan;
1064 int err;
1065
1066 pchan = chan_get();
1067 if (!pchan)
1068 return -EINVAL;
1069
1070 err = l2cap_chan_connect(pchan, cpu_to_le16(psm_6lowpan), 0,
1071 addr, dst_type);
1072
1073 BT_DBG("chan %p err %d", pchan, err);
1074 if (err < 0)
1075 l2cap_chan_put(pchan);
1076
Jukka Rissanen18722c22013-12-11 17:05:37 +02001077 return err;
1078}
1079
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001080static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
1081{
1082 struct lowpan_peer *peer;
1083
1084 BT_DBG("conn %p dst type %d", conn, dst_type);
1085
1086 peer = lookup_peer(conn);
1087 if (!peer)
1088 return -ENOENT;
1089
1090 BT_DBG("peer %p chan %p", peer, peer->chan);
1091
1092 l2cap_chan_close(peer->chan, ENOENT);
1093
1094 return 0;
1095}
1096
1097static struct l2cap_chan *bt_6lowpan_listen(void)
1098{
1099 bdaddr_t *addr = BDADDR_ANY;
1100 struct l2cap_chan *pchan;
1101 int err;
1102
1103 if (psm_6lowpan == 0)
1104 return NULL;
1105
1106 pchan = chan_get();
1107 if (!pchan)
1108 return NULL;
1109
1110 pchan->state = BT_LISTEN;
1111 pchan->src_type = BDADDR_LE_PUBLIC;
1112
1113 BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan, pchan,
1114 pchan->src_type);
1115
1116 err = l2cap_add_psm(pchan, addr, cpu_to_le16(psm_6lowpan));
1117 if (err) {
1118 l2cap_chan_put(pchan);
1119 BT_ERR("psm cannot be added err %d", err);
1120 return NULL;
1121 }
1122
1123 return pchan;
1124}
1125
1126static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
1127 struct l2cap_conn **conn)
1128{
1129 struct hci_conn *hcon;
1130 struct hci_dev *hdev;
1131 bdaddr_t *src = BDADDR_ANY;
1132 int n;
1133
1134 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1135 &addr->b[5], &addr->b[4], &addr->b[3],
1136 &addr->b[2], &addr->b[1], &addr->b[0],
1137 addr_type);
1138
1139 if (n < 7)
1140 return -EINVAL;
1141
1142 hdev = hci_get_route(addr, src);
1143 if (!hdev)
1144 return -ENOENT;
1145
1146 hci_dev_lock(hdev);
1147 hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
1148 hci_dev_unlock(hdev);
1149
1150 if (!hcon)
1151 return -ENOENT;
1152
1153 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1154
1155 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1156
1157 return 0;
1158}
1159
1160static void disconnect_all_peers(void)
1161{
1162 struct lowpan_dev *entry, *tmp_dev;
1163 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1164 struct list_head peers;
1165 unsigned long flags;
1166
1167 INIT_LIST_HEAD(&peers);
1168
1169 /* We make a separate list of peers as the close_cb() will
1170 * modify the device peers list so it is better not to mess
1171 * with the same list at the same time.
1172 */
1173
1174 read_lock_irqsave(&devices_lock, flags);
1175
1176 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1177 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list) {
1178 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1179 if (!new_peer)
1180 break;
1181
1182 new_peer->chan = peer->chan;
1183 INIT_LIST_HEAD(&new_peer->list);
1184
1185 list_add(&new_peer->list, &peers);
1186 }
1187 }
1188
1189 read_unlock_irqrestore(&devices_lock, flags);
1190
1191 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1192 l2cap_chan_close(peer->chan, ENOENT);
1193 kfree(peer);
1194 }
1195}
1196
1197static int lowpan_psm_set(void *data, u64 val)
1198{
1199 u16 psm;
1200
1201 psm = val;
1202 if (psm == 0 || psm_6lowpan != psm)
1203 /* Disconnect existing connections if 6lowpan is
1204 * disabled (psm = 0), or if psm changes.
1205 */
1206 disconnect_all_peers();
1207
1208 psm_6lowpan = psm;
1209
1210 if (listen_chan) {
1211 l2cap_chan_close(listen_chan, 0);
1212 l2cap_chan_put(listen_chan);
1213 }
1214
1215 listen_chan = bt_6lowpan_listen();
1216
1217 return 0;
1218}
1219
1220static int lowpan_psm_get(void *data, u64 *val)
1221{
1222 *val = psm_6lowpan;
1223 return 0;
1224}
1225
1226DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops, lowpan_psm_get,
1227 lowpan_psm_set, "%llu\n");
1228
1229static ssize_t lowpan_control_write(struct file *fp,
1230 const char __user *user_buffer,
1231 size_t count,
1232 loff_t *position)
1233{
1234 char buf[32];
1235 size_t buf_size = min(count, sizeof(buf) - 1);
1236 int ret;
1237 bdaddr_t addr;
1238 u8 addr_type;
1239 struct l2cap_conn *conn = NULL;
1240
1241 if (copy_from_user(buf, user_buffer, buf_size))
1242 return -EFAULT;
1243
1244 buf[buf_size] = '\0';
1245
1246 if (memcmp(buf, "connect ", 8) == 0) {
1247 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1248 if (ret == -EINVAL)
1249 return ret;
1250
1251 if (listen_chan) {
1252 l2cap_chan_close(listen_chan, 0);
1253 l2cap_chan_put(listen_chan);
1254 listen_chan = NULL;
1255 }
1256
1257 if (conn) {
1258 struct lowpan_peer *peer;
1259
1260 if (!is_bt_6lowpan(conn->hcon))
1261 return -EINVAL;
1262
1263 peer = lookup_peer(conn);
1264 if (peer) {
1265 BT_DBG("6LoWPAN connection already exists");
1266 return -EALREADY;
1267 }
1268
1269 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1270 &conn->hcon->dst, conn->hcon->dst_type,
1271 addr_type);
1272 }
1273
1274 ret = bt_6lowpan_connect(&addr, addr_type);
1275 if (ret < 0)
1276 return ret;
1277
1278 return count;
1279 }
1280
1281 if (memcmp(buf, "disconnect ", 11) == 0) {
1282 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1283 if (ret < 0)
1284 return ret;
1285
1286 ret = bt_6lowpan_disconnect(conn, addr_type);
1287 if (ret < 0)
1288 return ret;
1289
1290 return count;
1291 }
1292
1293 return count;
1294}
1295
1296static int lowpan_control_show(struct seq_file *f, void *ptr)
1297{
1298 struct lowpan_dev *entry, *tmp_dev;
1299 struct lowpan_peer *peer, *tmp_peer;
1300 unsigned long flags;
1301
1302 read_lock_irqsave(&devices_lock, flags);
1303
1304 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1305 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list)
1306 seq_printf(f, "%pMR (type %u)\n",
1307 &peer->chan->dst, peer->chan->dst_type);
1308 }
1309
1310 read_unlock_irqrestore(&devices_lock, flags);
1311
1312 return 0;
1313}
1314
1315static int lowpan_control_open(struct inode *inode, struct file *file)
1316{
1317 return single_open(file, lowpan_control_show, inode->i_private);
1318}
1319
1320static const struct file_operations lowpan_control_fops = {
1321 .open = lowpan_control_open,
1322 .read = seq_read,
1323 .write = lowpan_control_write,
1324 .llseek = seq_lseek,
1325 .release = single_release,
1326};
1327
Jukka Rissanen7f118252014-06-18 16:37:11 +03001328static void disconnect_devices(void)
1329{
1330 struct lowpan_dev *entry, *tmp, *new_dev;
1331 struct list_head devices;
1332 unsigned long flags;
1333
1334 INIT_LIST_HEAD(&devices);
1335
1336 /* We make a separate list of devices because the unregister_netdev()
1337 * will call device_event() which will also want to modify the same
1338 * devices list.
1339 */
1340
1341 read_lock_irqsave(&devices_lock, flags);
1342
1343 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
1344 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1345 if (!new_dev)
1346 break;
1347
1348 new_dev->netdev = entry->netdev;
1349 INIT_LIST_HEAD(&new_dev->list);
1350
1351 list_add(&new_dev->list, &devices);
1352 }
1353
1354 read_unlock_irqrestore(&devices_lock, flags);
1355
1356 list_for_each_entry_safe(entry, tmp, &devices, list) {
1357 ifdown(entry->netdev);
1358 BT_DBG("Unregistering netdev %s %p",
1359 entry->netdev->name, entry->netdev);
1360 unregister_netdev(entry->netdev);
1361 kfree(entry);
1362 }
1363}
1364
Jukka Rissanen18722c22013-12-11 17:05:37 +02001365static int device_event(struct notifier_block *unused,
1366 unsigned long event, void *ptr)
1367{
1368 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1369 struct lowpan_dev *entry, *tmp;
1370 unsigned long flags;
1371
1372 if (netdev->type != ARPHRD_6LOWPAN)
1373 return NOTIFY_DONE;
1374
1375 switch (event) {
1376 case NETDEV_UNREGISTER:
1377 write_lock_irqsave(&devices_lock, flags);
1378 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices,
1379 list) {
1380 if (entry->netdev == netdev) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001381 BT_DBG("Unregistered netdev %s %p",
1382 netdev->name, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001383 list_del(&entry->list);
1384 kfree(entry);
1385 break;
1386 }
1387 }
1388 write_unlock_irqrestore(&devices_lock, flags);
1389 break;
1390 }
1391
1392 return NOTIFY_DONE;
1393}
1394
1395static struct notifier_block bt_6lowpan_dev_notifier = {
1396 .notifier_call = device_event,
1397};
1398
Jukka Rissanen5547e482014-06-18 16:37:09 +03001399static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001400{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001401 lowpan_psm_debugfs = debugfs_create_file("6lowpan_psm", 0644,
1402 bt_debugfs, NULL,
1403 &lowpan_psm_fops);
1404 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1405 bt_debugfs, NULL,
1406 &lowpan_control_fops);
1407
Jukka Rissanen18722c22013-12-11 17:05:37 +02001408 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1409}
1410
Jukka Rissanen5547e482014-06-18 16:37:09 +03001411static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001412{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001413 debugfs_remove(lowpan_psm_debugfs);
1414 debugfs_remove(lowpan_control_debugfs);
1415
1416 if (listen_chan) {
1417 l2cap_chan_close(listen_chan, 0);
1418 l2cap_chan_put(listen_chan);
1419 }
1420
Jukka Rissanen7f118252014-06-18 16:37:11 +03001421 disconnect_devices();
1422
Jukka Rissanen18722c22013-12-11 17:05:37 +02001423 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1424}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001425
1426module_init(bt_6lowpan_init);
1427module_exit(bt_6lowpan_exit);
1428
1429MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1430MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1431MODULE_VERSION(VERSION);
1432MODULE_LICENSE("GPL");