blob: fcfaa7006b28e81515ddb8f025d768292b2f90eb [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 Rissanend7b6b0a2014-10-01 15:59:14 +0300512 struct net_device *netdev)
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 Rissanend7b6b0a2014-10-01 15:59:14 +0300519 * we run out of credits.
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300520 */
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300521 chan->data = skb;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300522
523 memset(&msg, 0, sizeof(msg));
524 msg.msg_iov = (struct iovec *) &iv;
525 msg.msg_iovlen = 1;
526 iv.iov_base = skb->data;
527 iv.iov_len = skb->len;
528
529 err = l2cap_chan_send(chan, &msg, skb->len);
530 if (err > 0) {
531 netdev->stats.tx_bytes += err;
532 netdev->stats.tx_packets++;
533 return 0;
534 }
535
536 if (!err)
537 err = lowpan_cb(skb)->status;
538
539 if (err < 0) {
540 if (err == -EAGAIN)
541 netdev->stats.tx_dropped++;
542 else
543 netdev->stats.tx_errors++;
544 }
545
546 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200547}
548
549static void send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
550{
551 struct sk_buff *local_skb;
552 struct lowpan_dev *entry, *tmp;
553 unsigned long flags;
554
555 read_lock_irqsave(&devices_lock, flags);
556
557 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
558 struct lowpan_peer *pentry, *ptmp;
559 struct lowpan_dev *dev;
560
561 if (entry->netdev != netdev)
562 continue;
563
564 dev = lowpan_dev(entry->netdev);
565
566 list_for_each_entry_safe(pentry, ptmp, &dev->peers, list) {
567 local_skb = skb_clone(skb, GFP_ATOMIC);
568
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300569 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
570 netdev->name,
571 &pentry->chan->dst, pentry->chan->dst_type,
572 &pentry->peer_addr, pentry->chan);
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300573 send_pkt(pentry->chan, local_skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200574
575 kfree_skb(local_skb);
576 }
577 }
578
579 read_unlock_irqrestore(&devices_lock, flags);
580}
581
582static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
583{
584 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200585 bdaddr_t addr;
586 u8 addr_type;
Jukka Rissanena7807d732014-10-01 11:30:57 +0300587 struct sk_buff *tmpskb;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200588
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300589 /* We must take a copy of the skb before we modify/replace the ipv6
590 * header as the header could be used elsewhere
591 */
Jukka Rissanena7807d732014-10-01 11:30:57 +0300592 tmpskb = skb_unshare(skb, GFP_ATOMIC);
593 if (!tmpskb) {
594 kfree_skb(skb);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300595 return NET_XMIT_DROP;
Jukka Rissanena7807d732014-10-01 11:30:57 +0300596 }
597 skb = tmpskb;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300598
599 /* Return values from setup_header()
600 * <0 - error, packet is dropped
601 * 0 - this is a multicast packet
602 * 1 - this is unicast packet
603 */
604 err = setup_header(skb, netdev, &addr, &addr_type);
605 if (err < 0) {
606 kfree_skb(skb);
607 return NET_XMIT_DROP;
608 }
609
610 if (err) {
611 if (lowpan_cb(skb)->chan) {
612 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
613 netdev->name, &addr, addr_type,
614 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300615 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300616 } else {
617 err = -ENOENT;
618 }
619 } else {
620 /* We need to send the packet to every device behind this
621 * interface.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200622 */
623 send_mcast_pkt(skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200624 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200625
Jukka Rissanenfc125182014-10-01 11:30:26 +0300626 dev_kfree_skb(skb);
627
Jukka Rissanen18722c22013-12-11 17:05:37 +0200628 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;
Jukka Rissanen156395c2014-09-29 16:37:26 +0300651 dev->flags = IFF_RUNNING | IFF_POINTOPOINT |
652 IFF_MULTICAST;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200653 dev->watchdog_timeo = 0;
654
655 dev->netdev_ops = &netdev_ops;
656 dev->header_ops = &header_ops;
657 dev->destructor = free_netdev;
658}
659
660static struct device_type bt_type = {
661 .name = "bluetooth",
662};
663
664static void set_addr(u8 *eui, u8 *addr, u8 addr_type)
665{
666 /* addr is the BT address in little-endian format */
667 eui[0] = addr[5];
668 eui[1] = addr[4];
669 eui[2] = addr[3];
670 eui[3] = 0xFF;
671 eui[4] = 0xFE;
672 eui[5] = addr[2];
673 eui[6] = addr[1];
674 eui[7] = addr[0];
675
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300676 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300677 if (addr_type == BDADDR_LE_PUBLIC)
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300678 eui[0] &= ~0x02;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200679 else
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300680 eui[0] |= 0x02;
681
682 BT_DBG("type %d addr %*phC", addr_type, 8, eui);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200683}
684
685static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr,
686 u8 addr_type)
687{
688 netdev->addr_assign_type = NET_ADDR_PERM;
689 set_addr(netdev->dev_addr, addr->b, addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200690}
691
692static void ifup(struct net_device *netdev)
693{
694 int err;
695
696 rtnl_lock();
697 err = dev_open(netdev);
698 if (err < 0)
699 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
700 rtnl_unlock();
701}
702
Jukka Rissanen7f118252014-06-18 16:37:11 +0300703static void ifdown(struct net_device *netdev)
704{
705 int err;
706
707 rtnl_lock();
708 err = dev_close(netdev);
709 if (err < 0)
710 BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
711 rtnl_unlock();
712}
713
Jukka Rissanen18722c22013-12-11 17:05:37 +0200714static void do_notify_peers(struct work_struct *work)
715{
716 struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
717 notify_peers.work);
718
719 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
720}
721
722static bool is_bt_6lowpan(struct hci_conn *hcon)
723{
724 if (hcon->type != LE_LINK)
725 return false;
726
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300727 if (!psm_6lowpan)
728 return false;
729
730 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200731}
732
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300733static struct l2cap_chan *chan_create(void)
734{
735 struct l2cap_chan *chan;
736
737 chan = l2cap_chan_create();
738 if (!chan)
739 return NULL;
740
741 l2cap_chan_set_defaults(chan);
742
743 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
744 chan->mode = L2CAP_MODE_LE_FLOWCTL;
745 chan->omtu = 65535;
746 chan->imtu = chan->omtu;
747
748 return chan;
749}
750
751static struct l2cap_chan *chan_open(struct l2cap_chan *pchan)
752{
753 struct l2cap_chan *chan;
754
755 chan = chan_create();
756 if (!chan)
757 return NULL;
758
759 chan->remote_mps = chan->omtu;
760 chan->mps = chan->omtu;
761
762 chan->state = BT_CONNECTED;
763
764 return chan;
765}
766
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300767static void set_ip_addr_bits(u8 addr_type, u8 *addr)
768{
769 if (addr_type == BDADDR_LE_PUBLIC)
770 *addr |= 0x02;
771 else
772 *addr &= ~0x02;
773}
774
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300775static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
776 struct lowpan_dev *dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200777{
778 struct lowpan_peer *peer;
779 unsigned long flags;
780
781 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
782 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300783 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200784
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300785 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200786 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
787
788 /* RFC 2464 ch. 5 */
789 peer->peer_addr.s6_addr[0] = 0xFE;
790 peer->peer_addr.s6_addr[1] = 0x80;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300791 set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
792 chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200793
794 memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
795 EUI64_ADDR_LEN);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200796
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300797 /* IPv6 address needs to have the U/L bit set properly so toggle
798 * it back here.
799 */
800 set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8);
801
Jukka Rissanen18722c22013-12-11 17:05:37 +0200802 write_lock_irqsave(&devices_lock, flags);
803 INIT_LIST_HEAD(&peer->list);
804 peer_add(dev, peer);
805 write_unlock_irqrestore(&devices_lock, flags);
806
807 /* Notifying peers about us needs to be done without locks held */
808 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
809 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
810
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300811 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200812}
813
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300814static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200815{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200816 struct net_device *netdev;
817 int err = 0;
818 unsigned long flags;
819
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300820 netdev = alloc_netdev(sizeof(struct lowpan_dev), IFACE_NAME_TEMPLATE,
Tom Gundersenc835a672014-07-14 16:37:24 +0200821 NET_NAME_UNKNOWN, netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200822 if (!netdev)
823 return -ENOMEM;
824
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300825 set_dev_addr(netdev, &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200826
827 netdev->netdev_ops = &netdev_ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300828 SET_NETDEV_DEV(netdev, &chan->conn->hcon->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200829 SET_NETDEV_DEVTYPE(netdev, &bt_type);
830
831 err = register_netdev(netdev);
832 if (err < 0) {
833 BT_INFO("register_netdev failed %d", err);
834 free_netdev(netdev);
835 goto out;
836 }
837
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300838 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
839 netdev->ifindex, &chan->dst, chan->dst_type,
840 &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200841 set_bit(__LINK_STATE_PRESENT, &netdev->state);
842
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300843 *dev = netdev_priv(netdev);
844 (*dev)->netdev = netdev;
845 (*dev)->hdev = chan->conn->hcon->hdev;
846 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200847
848 write_lock_irqsave(&devices_lock, flags);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300849 INIT_LIST_HEAD(&(*dev)->list);
850 list_add(&(*dev)->list, &bt_6lowpan_devices);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200851 write_unlock_irqrestore(&devices_lock, flags);
852
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300853 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200854
855out:
856 return err;
857}
858
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300859static inline void chan_ready_cb(struct l2cap_chan *chan)
860{
861 struct lowpan_dev *dev;
862
863 dev = lookup_dev(chan->conn);
864
865 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
866
867 if (!dev) {
868 if (setup_netdev(chan, &dev) < 0) {
869 l2cap_chan_del(chan, -ENOENT);
870 return;
871 }
872 }
873
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300874 if (!try_module_get(THIS_MODULE))
875 return;
876
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300877 add_peer_chan(chan, dev);
878 ifup(dev->netdev);
879}
880
Johan Hedberg2b293492014-08-07 10:03:32 +0300881static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300882{
Johan Hedberg2b293492014-08-07 10:03:32 +0300883 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300884
Johan Hedberg2b293492014-08-07 10:03:32 +0300885 chan = chan_open(pchan);
886 chan->ops = pchan->ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300887
888 BT_DBG("chan %p pchan %p", chan, pchan);
889
Johan Hedberg2b293492014-08-07 10:03:32 +0300890 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300891}
892
Jukka Rissanen18722c22013-12-11 17:05:37 +0200893static void delete_netdev(struct work_struct *work)
894{
895 struct lowpan_dev *entry = container_of(work, struct lowpan_dev,
896 delete_netdev);
897
898 unregister_netdev(entry->netdev);
899
900 /* The entry pointer is deleted in device_event() */
901}
902
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300903static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200904{
905 struct lowpan_dev *entry, *tmp;
906 struct lowpan_dev *dev = NULL;
907 struct lowpan_peer *peer;
908 int err = -ENOENT;
909 unsigned long flags;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300910 bool last = false, removed = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200911
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300912 BT_DBG("chan %p conn %p", chan, chan->conn);
913
914 if (chan->conn && chan->conn->hcon) {
915 if (!is_bt_6lowpan(chan->conn->hcon))
916 return;
917
918 /* If conn is set, then the netdev is also there and we should
919 * not remove it.
920 */
921 removed = false;
922 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200923
924 write_lock_irqsave(&devices_lock, flags);
925
926 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
927 dev = lowpan_dev(entry->netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300928 peer = peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200929 if (peer) {
930 last = peer_del(dev, peer);
931 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300932
933 BT_DBG("dev %p removing %speer %p", dev,
934 last ? "last " : "1 ", peer);
935 BT_DBG("chan %p orig refcnt %d", chan,
936 atomic_read(&chan->kref.refcount));
937
938 l2cap_chan_put(chan);
939 kfree(peer);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200940 break;
941 }
942 }
943
944 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
945 write_unlock_irqrestore(&devices_lock, flags);
946
947 cancel_delayed_work_sync(&dev->notify_peers);
948
Jukka Rissanen7f118252014-06-18 16:37:11 +0300949 ifdown(dev->netdev);
950
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300951 if (!removed) {
952 INIT_WORK(&entry->delete_netdev, delete_netdev);
953 schedule_work(&entry->delete_netdev);
954 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200955 } else {
956 write_unlock_irqrestore(&devices_lock, flags);
957 }
958
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300959 return;
960}
961
962static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
963{
964 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
965 state_to_string(state), err);
966}
967
968static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
969 unsigned long hdr_len,
970 unsigned long len, int nb)
971{
972 /* Note that we must allocate using GFP_ATOMIC here as
973 * this function is called originally from netdev hard xmit
974 * function in atomic context.
975 */
976 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
977}
978
979static void chan_suspend_cb(struct l2cap_chan *chan)
980{
981 struct sk_buff *skb = chan->data;
982
983 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
984
Jukka Rissanen59790aa2014-09-29 10:55:46 +0300985 if (!skb)
986 return;
987
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300988 lowpan_cb(skb)->status = -EAGAIN;
989}
990
991static void chan_resume_cb(struct l2cap_chan *chan)
992{
993 struct sk_buff *skb = chan->data;
994
995 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
996
Jukka Rissanen59790aa2014-09-29 10:55:46 +0300997 if (!skb)
998 return;
999
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001000 lowpan_cb(skb)->status = 0;
1001}
1002
1003static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
1004{
Jukka Rissanen2ae50d82014-09-08 12:11:43 +03001005 return L2CAP_CONN_TIMEOUT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001006}
1007
1008static const struct l2cap_ops bt_6lowpan_chan_ops = {
1009 .name = "L2CAP 6LoWPAN channel",
1010 .new_connection = chan_new_conn_cb,
1011 .recv = chan_recv_cb,
1012 .close = chan_close_cb,
1013 .state_change = chan_state_change_cb,
1014 .ready = chan_ready_cb,
1015 .resume = chan_resume_cb,
1016 .suspend = chan_suspend_cb,
1017 .get_sndtimeo = chan_get_sndtimeo_cb,
1018 .alloc_skb = chan_alloc_skb_cb,
1019 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1020
1021 .teardown = l2cap_chan_no_teardown,
1022 .defer = l2cap_chan_no_defer,
1023 .set_shutdown = l2cap_chan_no_set_shutdown,
1024};
1025
1026static inline __u8 bdaddr_type(__u8 type)
1027{
1028 if (type == ADDR_LE_DEV_PUBLIC)
1029 return BDADDR_LE_PUBLIC;
1030 else
1031 return BDADDR_LE_RANDOM;
1032}
1033
1034static struct l2cap_chan *chan_get(void)
1035{
1036 struct l2cap_chan *pchan;
1037
1038 pchan = chan_create();
1039 if (!pchan)
1040 return NULL;
1041
1042 pchan->ops = &bt_6lowpan_chan_ops;
1043
1044 return pchan;
1045}
1046
1047static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
1048{
1049 struct l2cap_chan *pchan;
1050 int err;
1051
1052 pchan = chan_get();
1053 if (!pchan)
1054 return -EINVAL;
1055
1056 err = l2cap_chan_connect(pchan, cpu_to_le16(psm_6lowpan), 0,
1057 addr, dst_type);
1058
1059 BT_DBG("chan %p err %d", pchan, err);
1060 if (err < 0)
1061 l2cap_chan_put(pchan);
1062
Jukka Rissanen18722c22013-12-11 17:05:37 +02001063 return err;
1064}
1065
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001066static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
1067{
1068 struct lowpan_peer *peer;
1069
1070 BT_DBG("conn %p dst type %d", conn, dst_type);
1071
1072 peer = lookup_peer(conn);
1073 if (!peer)
1074 return -ENOENT;
1075
1076 BT_DBG("peer %p chan %p", peer, peer->chan);
1077
1078 l2cap_chan_close(peer->chan, ENOENT);
1079
1080 return 0;
1081}
1082
1083static struct l2cap_chan *bt_6lowpan_listen(void)
1084{
1085 bdaddr_t *addr = BDADDR_ANY;
1086 struct l2cap_chan *pchan;
1087 int err;
1088
1089 if (psm_6lowpan == 0)
1090 return NULL;
1091
1092 pchan = chan_get();
1093 if (!pchan)
1094 return NULL;
1095
1096 pchan->state = BT_LISTEN;
1097 pchan->src_type = BDADDR_LE_PUBLIC;
1098
1099 BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan, pchan,
1100 pchan->src_type);
1101
1102 err = l2cap_add_psm(pchan, addr, cpu_to_le16(psm_6lowpan));
1103 if (err) {
1104 l2cap_chan_put(pchan);
1105 BT_ERR("psm cannot be added err %d", err);
1106 return NULL;
1107 }
1108
1109 return pchan;
1110}
1111
1112static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
1113 struct l2cap_conn **conn)
1114{
1115 struct hci_conn *hcon;
1116 struct hci_dev *hdev;
1117 bdaddr_t *src = BDADDR_ANY;
1118 int n;
1119
1120 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1121 &addr->b[5], &addr->b[4], &addr->b[3],
1122 &addr->b[2], &addr->b[1], &addr->b[0],
1123 addr_type);
1124
1125 if (n < 7)
1126 return -EINVAL;
1127
1128 hdev = hci_get_route(addr, src);
1129 if (!hdev)
1130 return -ENOENT;
1131
1132 hci_dev_lock(hdev);
1133 hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
1134 hci_dev_unlock(hdev);
1135
1136 if (!hcon)
1137 return -ENOENT;
1138
1139 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1140
1141 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1142
1143 return 0;
1144}
1145
1146static void disconnect_all_peers(void)
1147{
1148 struct lowpan_dev *entry, *tmp_dev;
1149 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1150 struct list_head peers;
1151 unsigned long flags;
1152
1153 INIT_LIST_HEAD(&peers);
1154
1155 /* We make a separate list of peers as the close_cb() will
1156 * modify the device peers list so it is better not to mess
1157 * with the same list at the same time.
1158 */
1159
1160 read_lock_irqsave(&devices_lock, flags);
1161
1162 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1163 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list) {
1164 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1165 if (!new_peer)
1166 break;
1167
1168 new_peer->chan = peer->chan;
1169 INIT_LIST_HEAD(&new_peer->list);
1170
1171 list_add(&new_peer->list, &peers);
1172 }
1173 }
1174
1175 read_unlock_irqrestore(&devices_lock, flags);
1176
1177 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1178 l2cap_chan_close(peer->chan, ENOENT);
1179 kfree(peer);
1180 }
1181}
1182
1183static int lowpan_psm_set(void *data, u64 val)
1184{
1185 u16 psm;
1186
1187 psm = val;
1188 if (psm == 0 || psm_6lowpan != psm)
1189 /* Disconnect existing connections if 6lowpan is
1190 * disabled (psm = 0), or if psm changes.
1191 */
1192 disconnect_all_peers();
1193
1194 psm_6lowpan = psm;
1195
1196 if (listen_chan) {
1197 l2cap_chan_close(listen_chan, 0);
1198 l2cap_chan_put(listen_chan);
1199 }
1200
1201 listen_chan = bt_6lowpan_listen();
1202
1203 return 0;
1204}
1205
1206static int lowpan_psm_get(void *data, u64 *val)
1207{
1208 *val = psm_6lowpan;
1209 return 0;
1210}
1211
1212DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops, lowpan_psm_get,
1213 lowpan_psm_set, "%llu\n");
1214
1215static ssize_t lowpan_control_write(struct file *fp,
1216 const char __user *user_buffer,
1217 size_t count,
1218 loff_t *position)
1219{
1220 char buf[32];
1221 size_t buf_size = min(count, sizeof(buf) - 1);
1222 int ret;
1223 bdaddr_t addr;
1224 u8 addr_type;
1225 struct l2cap_conn *conn = NULL;
1226
1227 if (copy_from_user(buf, user_buffer, buf_size))
1228 return -EFAULT;
1229
1230 buf[buf_size] = '\0';
1231
1232 if (memcmp(buf, "connect ", 8) == 0) {
1233 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1234 if (ret == -EINVAL)
1235 return ret;
1236
1237 if (listen_chan) {
1238 l2cap_chan_close(listen_chan, 0);
1239 l2cap_chan_put(listen_chan);
1240 listen_chan = NULL;
1241 }
1242
1243 if (conn) {
1244 struct lowpan_peer *peer;
1245
1246 if (!is_bt_6lowpan(conn->hcon))
1247 return -EINVAL;
1248
1249 peer = lookup_peer(conn);
1250 if (peer) {
1251 BT_DBG("6LoWPAN connection already exists");
1252 return -EALREADY;
1253 }
1254
1255 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1256 &conn->hcon->dst, conn->hcon->dst_type,
1257 addr_type);
1258 }
1259
1260 ret = bt_6lowpan_connect(&addr, addr_type);
1261 if (ret < 0)
1262 return ret;
1263
1264 return count;
1265 }
1266
1267 if (memcmp(buf, "disconnect ", 11) == 0) {
1268 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1269 if (ret < 0)
1270 return ret;
1271
1272 ret = bt_6lowpan_disconnect(conn, addr_type);
1273 if (ret < 0)
1274 return ret;
1275
1276 return count;
1277 }
1278
1279 return count;
1280}
1281
1282static int lowpan_control_show(struct seq_file *f, void *ptr)
1283{
1284 struct lowpan_dev *entry, *tmp_dev;
1285 struct lowpan_peer *peer, *tmp_peer;
1286 unsigned long flags;
1287
1288 read_lock_irqsave(&devices_lock, flags);
1289
1290 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1291 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list)
1292 seq_printf(f, "%pMR (type %u)\n",
1293 &peer->chan->dst, peer->chan->dst_type);
1294 }
1295
1296 read_unlock_irqrestore(&devices_lock, flags);
1297
1298 return 0;
1299}
1300
1301static int lowpan_control_open(struct inode *inode, struct file *file)
1302{
1303 return single_open(file, lowpan_control_show, inode->i_private);
1304}
1305
1306static const struct file_operations lowpan_control_fops = {
1307 .open = lowpan_control_open,
1308 .read = seq_read,
1309 .write = lowpan_control_write,
1310 .llseek = seq_lseek,
1311 .release = single_release,
1312};
1313
Jukka Rissanen7f118252014-06-18 16:37:11 +03001314static void disconnect_devices(void)
1315{
1316 struct lowpan_dev *entry, *tmp, *new_dev;
1317 struct list_head devices;
1318 unsigned long flags;
1319
1320 INIT_LIST_HEAD(&devices);
1321
1322 /* We make a separate list of devices because the unregister_netdev()
1323 * will call device_event() which will also want to modify the same
1324 * devices list.
1325 */
1326
1327 read_lock_irqsave(&devices_lock, flags);
1328
1329 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
1330 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1331 if (!new_dev)
1332 break;
1333
1334 new_dev->netdev = entry->netdev;
1335 INIT_LIST_HEAD(&new_dev->list);
1336
1337 list_add(&new_dev->list, &devices);
1338 }
1339
1340 read_unlock_irqrestore(&devices_lock, flags);
1341
1342 list_for_each_entry_safe(entry, tmp, &devices, list) {
1343 ifdown(entry->netdev);
1344 BT_DBG("Unregistering netdev %s %p",
1345 entry->netdev->name, entry->netdev);
1346 unregister_netdev(entry->netdev);
1347 kfree(entry);
1348 }
1349}
1350
Jukka Rissanen18722c22013-12-11 17:05:37 +02001351static int device_event(struct notifier_block *unused,
1352 unsigned long event, void *ptr)
1353{
1354 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1355 struct lowpan_dev *entry, *tmp;
1356 unsigned long flags;
1357
1358 if (netdev->type != ARPHRD_6LOWPAN)
1359 return NOTIFY_DONE;
1360
1361 switch (event) {
1362 case NETDEV_UNREGISTER:
1363 write_lock_irqsave(&devices_lock, flags);
1364 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices,
1365 list) {
1366 if (entry->netdev == netdev) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001367 BT_DBG("Unregistered netdev %s %p",
1368 netdev->name, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001369 list_del(&entry->list);
1370 kfree(entry);
1371 break;
1372 }
1373 }
1374 write_unlock_irqrestore(&devices_lock, flags);
1375 break;
1376 }
1377
1378 return NOTIFY_DONE;
1379}
1380
1381static struct notifier_block bt_6lowpan_dev_notifier = {
1382 .notifier_call = device_event,
1383};
1384
Jukka Rissanen5547e482014-06-18 16:37:09 +03001385static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001386{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001387 lowpan_psm_debugfs = debugfs_create_file("6lowpan_psm", 0644,
1388 bt_debugfs, NULL,
1389 &lowpan_psm_fops);
1390 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1391 bt_debugfs, NULL,
1392 &lowpan_control_fops);
1393
Jukka Rissanen18722c22013-12-11 17:05:37 +02001394 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1395}
1396
Jukka Rissanen5547e482014-06-18 16:37:09 +03001397static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001398{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001399 debugfs_remove(lowpan_psm_debugfs);
1400 debugfs_remove(lowpan_control_debugfs);
1401
1402 if (listen_chan) {
1403 l2cap_chan_close(listen_chan, 0);
1404 l2cap_chan_put(listen_chan);
1405 }
1406
Jukka Rissanen7f118252014-06-18 16:37:11 +03001407 disconnect_devices();
1408
Jukka Rissanen18722c22013-12-11 17:05:37 +02001409 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1410}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001411
1412module_init(bt_6lowpan_init);
1413module_exit(bt_6lowpan_exit);
1414
1415MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1416MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1417MODULE_VERSION(VERSION);
1418MODULE_LICENSE("GPL");