blob: 40e2cec7fcefd2b463e7d434746febf9934b8c91 [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
Martin Townsend3c400b82014-10-23 15:40:55 +0100340 consume_skb(local_skb);
341 consume_skb(skb);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200342 } 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
Martin Townsend3c400b82014-10-23 15:40:55 +0100366 consume_skb(local_skb);
367 consume_skb(skb);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200368 break;
369 default:
370 break;
371 }
372 }
373
374 return NET_RX_SUCCESS;
375
376drop:
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300377 dev->stats.rx_dropped++;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200378 kfree_skb(skb);
379 return NET_RX_DROP;
380}
381
382/* Packet from BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300383static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200384{
385 struct lowpan_dev *dev;
386 struct lowpan_peer *peer;
387 int err;
388
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300389 peer = lookup_peer(chan->conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200390 if (!peer)
391 return -ENOENT;
392
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300393 dev = lookup_dev(chan->conn);
Johan Hedberg30d3db42013-12-12 09:53:21 +0200394 if (!dev || !dev->netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200395 return -ENOENT;
396
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300397 err = recv_pkt(skb, dev->netdev, chan);
398 if (err) {
399 BT_DBG("recv pkt %d", err);
400 err = -EAGAIN;
401 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200402
403 return err;
404}
405
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300406static u8 get_addr_type_from_eui64(u8 byte)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200407{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300408 /* Is universal(0) or local(1) bit */
409 return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC);
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300410}
411
412static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr)
413{
414 u8 *eui64 = ip6_daddr->s6_addr + 8;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200415
416 addr->b[0] = eui64[7];
417 addr->b[1] = eui64[6];
418 addr->b[2] = eui64[5];
419 addr->b[3] = eui64[2];
420 addr->b[4] = eui64[1];
421 addr->b[5] = eui64[0];
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300422}
Jukka Rissanen18722c22013-12-11 17:05:37 +0200423
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300424static void convert_dest_bdaddr(struct in6_addr *ip6_daddr,
425 bdaddr_t *addr, u8 *addr_type)
426{
427 copy_to_bdaddr(ip6_daddr, addr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200428
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300429 /* We need to toggle the U/L bit that we got from IPv6 address
430 * so that we get the proper address and type of the BD address.
431 */
432 addr->b[5] ^= 0x02;
433
434 *addr_type = get_addr_type_from_eui64(addr->b[5]);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200435}
436
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300437static int setup_header(struct sk_buff *skb, struct net_device *netdev,
438 bdaddr_t *peer_addr, u8 *peer_addr_type)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200439{
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300440 struct in6_addr ipv6_daddr;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200441 struct lowpan_dev *dev;
442 struct lowpan_peer *peer;
443 bdaddr_t addr, *any = BDADDR_ANY;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300444 u8 *daddr = any->b;
445 int err, status = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200446
447 dev = lowpan_dev(netdev);
448
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300449 memcpy(&ipv6_daddr, &lowpan_cb(skb)->addr, sizeof(ipv6_daddr));
450
451 if (ipv6_addr_is_multicast(&ipv6_daddr)) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300452 lowpan_cb(skb)->chan = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200453 } else {
454 unsigned long flags;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300455 u8 addr_type;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200456
457 /* Get destination BT device from skb.
458 * If there is no such peer then discard the packet.
459 */
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300460 convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200461
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300462 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr,
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300463 addr_type, &ipv6_daddr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200464
465 read_lock_irqsave(&devices_lock, flags);
466 peer = peer_lookup_ba(dev, &addr, addr_type);
467 read_unlock_irqrestore(&devices_lock, flags);
468
469 if (!peer) {
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300470 /* The packet might be sent to 6lowpan interface
471 * because of routing (either via default route
472 * or user set route) so get peer according to
473 * the destination address.
474 */
475 read_lock_irqsave(&devices_lock, flags);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300476 peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300477 read_unlock_irqrestore(&devices_lock, flags);
478 if (!peer) {
479 BT_DBG("no such peer %pMR found", &addr);
480 return -ENOENT;
481 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200482 }
483
484 daddr = peer->eui64_addr;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300485 *peer_addr = addr;
486 *peer_addr_type = addr_type;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300487 lowpan_cb(skb)->chan = peer->chan;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300488
489 status = 1;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200490 }
491
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300492 lowpan_header_compress(skb, netdev, ETH_P_IPV6, daddr,
493 dev->netdev->dev_addr, skb->len);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200494
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300495 err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
496 if (err < 0)
497 return err;
498
499 return status;
500}
501
502static int header_create(struct sk_buff *skb, struct net_device *netdev,
503 unsigned short type, const void *_daddr,
504 const void *_saddr, unsigned int len)
505{
506 struct ipv6hdr *hdr;
507
508 if (type != ETH_P_IPV6)
509 return -EINVAL;
510
511 hdr = ipv6_hdr(skb);
512
513 memcpy(&lowpan_cb(skb)->addr, &hdr->daddr, sizeof(struct in6_addr));
514
515 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200516}
517
518/* Packet to BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300519static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300520 struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200521{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300522 struct msghdr msg;
523 struct kvec iv;
524 int err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200525
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300526 /* Remember the skb so that we can send EAGAIN to the caller if
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300527 * we run out of credits.
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300528 */
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300529 chan->data = skb;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300530
531 memset(&msg, 0, sizeof(msg));
532 msg.msg_iov = (struct iovec *) &iv;
533 msg.msg_iovlen = 1;
534 iv.iov_base = skb->data;
535 iv.iov_len = skb->len;
536
537 err = l2cap_chan_send(chan, &msg, skb->len);
538 if (err > 0) {
539 netdev->stats.tx_bytes += err;
540 netdev->stats.tx_packets++;
541 return 0;
542 }
543
544 if (!err)
545 err = lowpan_cb(skb)->status;
546
547 if (err < 0) {
548 if (err == -EAGAIN)
549 netdev->stats.tx_dropped++;
550 else
551 netdev->stats.tx_errors++;
552 }
553
554 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200555}
556
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300557static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200558{
559 struct sk_buff *local_skb;
560 struct lowpan_dev *entry, *tmp;
561 unsigned long flags;
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300562 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200563
564 read_lock_irqsave(&devices_lock, flags);
565
566 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
567 struct lowpan_peer *pentry, *ptmp;
568 struct lowpan_dev *dev;
569
570 if (entry->netdev != netdev)
571 continue;
572
573 dev = lowpan_dev(entry->netdev);
574
575 list_for_each_entry_safe(pentry, ptmp, &dev->peers, list) {
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300576 int ret;
577
Jukka Rissanen18722c22013-12-11 17:05:37 +0200578 local_skb = skb_clone(skb, GFP_ATOMIC);
579
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300580 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
581 netdev->name,
582 &pentry->chan->dst, pentry->chan->dst_type,
583 &pentry->peer_addr, pentry->chan);
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300584 ret = send_pkt(pentry->chan, local_skb, netdev);
585 if (ret < 0)
586 err = ret;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200587
588 kfree_skb(local_skb);
589 }
590 }
591
592 read_unlock_irqrestore(&devices_lock, flags);
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300593
594 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200595}
596
597static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
598{
599 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200600 bdaddr_t addr;
601 u8 addr_type;
Jukka Rissanena7807d732014-10-01 11:30:57 +0300602 struct sk_buff *tmpskb;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200603
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300604 /* We must take a copy of the skb before we modify/replace the ipv6
605 * header as the header could be used elsewhere
606 */
Jukka Rissanena7807d732014-10-01 11:30:57 +0300607 tmpskb = skb_unshare(skb, GFP_ATOMIC);
608 if (!tmpskb) {
609 kfree_skb(skb);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300610 return NET_XMIT_DROP;
Jukka Rissanena7807d732014-10-01 11:30:57 +0300611 }
612 skb = tmpskb;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300613
614 /* Return values from setup_header()
615 * <0 - error, packet is dropped
616 * 0 - this is a multicast packet
617 * 1 - this is unicast packet
618 */
619 err = setup_header(skb, netdev, &addr, &addr_type);
620 if (err < 0) {
621 kfree_skb(skb);
622 return NET_XMIT_DROP;
623 }
624
625 if (err) {
626 if (lowpan_cb(skb)->chan) {
627 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
628 netdev->name, &addr, addr_type,
629 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300630 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300631 } else {
632 err = -ENOENT;
633 }
634 } else {
635 /* We need to send the packet to every device behind this
636 * interface.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200637 */
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300638 err = send_mcast_pkt(skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200639 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200640
Jukka Rissanenfc125182014-10-01 11:30:26 +0300641 dev_kfree_skb(skb);
642
Jukka Rissanen18722c22013-12-11 17:05:37 +0200643 if (err)
644 BT_DBG("ERROR: xmit failed (%d)", err);
645
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300646 return err < 0 ? NET_XMIT_DROP : err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200647}
648
649static const struct net_device_ops netdev_ops = {
650 .ndo_start_xmit = bt_xmit,
651};
652
653static struct header_ops header_ops = {
654 .create = header_create,
655};
656
657static void netdev_setup(struct net_device *dev)
658{
659 dev->addr_len = EUI64_ADDR_LEN;
660 dev->type = ARPHRD_6LOWPAN;
661
662 dev->hard_header_len = 0;
663 dev->needed_tailroom = 0;
664 dev->mtu = IPV6_MIN_MTU;
665 dev->tx_queue_len = 0;
Jukka Rissanen156395c2014-09-29 16:37:26 +0300666 dev->flags = IFF_RUNNING | IFF_POINTOPOINT |
667 IFF_MULTICAST;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200668 dev->watchdog_timeo = 0;
669
670 dev->netdev_ops = &netdev_ops;
671 dev->header_ops = &header_ops;
672 dev->destructor = free_netdev;
673}
674
675static struct device_type bt_type = {
676 .name = "bluetooth",
677};
678
679static void set_addr(u8 *eui, u8 *addr, u8 addr_type)
680{
681 /* addr is the BT address in little-endian format */
682 eui[0] = addr[5];
683 eui[1] = addr[4];
684 eui[2] = addr[3];
685 eui[3] = 0xFF;
686 eui[4] = 0xFE;
687 eui[5] = addr[2];
688 eui[6] = addr[1];
689 eui[7] = addr[0];
690
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300691 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300692 if (addr_type == BDADDR_LE_PUBLIC)
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300693 eui[0] &= ~0x02;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200694 else
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300695 eui[0] |= 0x02;
696
697 BT_DBG("type %d addr %*phC", addr_type, 8, eui);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200698}
699
700static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr,
701 u8 addr_type)
702{
703 netdev->addr_assign_type = NET_ADDR_PERM;
704 set_addr(netdev->dev_addr, addr->b, addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200705}
706
707static void ifup(struct net_device *netdev)
708{
709 int err;
710
711 rtnl_lock();
712 err = dev_open(netdev);
713 if (err < 0)
714 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
715 rtnl_unlock();
716}
717
Jukka Rissanen7f118252014-06-18 16:37:11 +0300718static void ifdown(struct net_device *netdev)
719{
720 int err;
721
722 rtnl_lock();
723 err = dev_close(netdev);
724 if (err < 0)
725 BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
726 rtnl_unlock();
727}
728
Jukka Rissanen18722c22013-12-11 17:05:37 +0200729static void do_notify_peers(struct work_struct *work)
730{
731 struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
732 notify_peers.work);
733
734 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
735}
736
737static bool is_bt_6lowpan(struct hci_conn *hcon)
738{
739 if (hcon->type != LE_LINK)
740 return false;
741
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300742 if (!psm_6lowpan)
743 return false;
744
745 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200746}
747
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300748static struct l2cap_chan *chan_create(void)
749{
750 struct l2cap_chan *chan;
751
752 chan = l2cap_chan_create();
753 if (!chan)
754 return NULL;
755
756 l2cap_chan_set_defaults(chan);
757
758 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
759 chan->mode = L2CAP_MODE_LE_FLOWCTL;
760 chan->omtu = 65535;
761 chan->imtu = chan->omtu;
762
763 return chan;
764}
765
766static struct l2cap_chan *chan_open(struct l2cap_chan *pchan)
767{
768 struct l2cap_chan *chan;
769
770 chan = chan_create();
771 if (!chan)
772 return NULL;
773
774 chan->remote_mps = chan->omtu;
775 chan->mps = chan->omtu;
776
777 chan->state = BT_CONNECTED;
778
779 return chan;
780}
781
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300782static void set_ip_addr_bits(u8 addr_type, u8 *addr)
783{
784 if (addr_type == BDADDR_LE_PUBLIC)
785 *addr |= 0x02;
786 else
787 *addr &= ~0x02;
788}
789
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300790static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
791 struct lowpan_dev *dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200792{
793 struct lowpan_peer *peer;
794 unsigned long flags;
795
796 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
797 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300798 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200799
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300800 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200801 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
802
803 /* RFC 2464 ch. 5 */
804 peer->peer_addr.s6_addr[0] = 0xFE;
805 peer->peer_addr.s6_addr[1] = 0x80;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300806 set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
807 chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200808
809 memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
810 EUI64_ADDR_LEN);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200811
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300812 /* IPv6 address needs to have the U/L bit set properly so toggle
813 * it back here.
814 */
815 set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8);
816
Jukka Rissanen18722c22013-12-11 17:05:37 +0200817 write_lock_irqsave(&devices_lock, flags);
818 INIT_LIST_HEAD(&peer->list);
819 peer_add(dev, peer);
820 write_unlock_irqrestore(&devices_lock, flags);
821
822 /* Notifying peers about us needs to be done without locks held */
823 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
824 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
825
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300826 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200827}
828
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300829static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200830{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200831 struct net_device *netdev;
832 int err = 0;
833 unsigned long flags;
834
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300835 netdev = alloc_netdev(sizeof(struct lowpan_dev), IFACE_NAME_TEMPLATE,
Tom Gundersenc835a672014-07-14 16:37:24 +0200836 NET_NAME_UNKNOWN, netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200837 if (!netdev)
838 return -ENOMEM;
839
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300840 set_dev_addr(netdev, &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200841
842 netdev->netdev_ops = &netdev_ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300843 SET_NETDEV_DEV(netdev, &chan->conn->hcon->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200844 SET_NETDEV_DEVTYPE(netdev, &bt_type);
845
846 err = register_netdev(netdev);
847 if (err < 0) {
848 BT_INFO("register_netdev failed %d", err);
849 free_netdev(netdev);
850 goto out;
851 }
852
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300853 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
854 netdev->ifindex, &chan->dst, chan->dst_type,
855 &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200856 set_bit(__LINK_STATE_PRESENT, &netdev->state);
857
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300858 *dev = netdev_priv(netdev);
859 (*dev)->netdev = netdev;
860 (*dev)->hdev = chan->conn->hcon->hdev;
861 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200862
863 write_lock_irqsave(&devices_lock, flags);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300864 INIT_LIST_HEAD(&(*dev)->list);
865 list_add(&(*dev)->list, &bt_6lowpan_devices);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200866 write_unlock_irqrestore(&devices_lock, flags);
867
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300868 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200869
870out:
871 return err;
872}
873
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300874static inline void chan_ready_cb(struct l2cap_chan *chan)
875{
876 struct lowpan_dev *dev;
877
878 dev = lookup_dev(chan->conn);
879
880 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
881
882 if (!dev) {
883 if (setup_netdev(chan, &dev) < 0) {
884 l2cap_chan_del(chan, -ENOENT);
885 return;
886 }
887 }
888
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300889 if (!try_module_get(THIS_MODULE))
890 return;
891
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300892 add_peer_chan(chan, dev);
893 ifup(dev->netdev);
894}
895
Johan Hedberg2b293492014-08-07 10:03:32 +0300896static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300897{
Johan Hedberg2b293492014-08-07 10:03:32 +0300898 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300899
Johan Hedberg2b293492014-08-07 10:03:32 +0300900 chan = chan_open(pchan);
901 chan->ops = pchan->ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300902
903 BT_DBG("chan %p pchan %p", chan, pchan);
904
Johan Hedberg2b293492014-08-07 10:03:32 +0300905 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300906}
907
Jukka Rissanen18722c22013-12-11 17:05:37 +0200908static void delete_netdev(struct work_struct *work)
909{
910 struct lowpan_dev *entry = container_of(work, struct lowpan_dev,
911 delete_netdev);
912
913 unregister_netdev(entry->netdev);
914
915 /* The entry pointer is deleted in device_event() */
916}
917
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300918static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200919{
920 struct lowpan_dev *entry, *tmp;
921 struct lowpan_dev *dev = NULL;
922 struct lowpan_peer *peer;
923 int err = -ENOENT;
924 unsigned long flags;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300925 bool last = false, removed = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200926
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300927 BT_DBG("chan %p conn %p", chan, chan->conn);
928
929 if (chan->conn && chan->conn->hcon) {
930 if (!is_bt_6lowpan(chan->conn->hcon))
931 return;
932
933 /* If conn is set, then the netdev is also there and we should
934 * not remove it.
935 */
936 removed = false;
937 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200938
939 write_lock_irqsave(&devices_lock, flags);
940
941 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
942 dev = lowpan_dev(entry->netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300943 peer = peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200944 if (peer) {
945 last = peer_del(dev, peer);
946 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300947
948 BT_DBG("dev %p removing %speer %p", dev,
949 last ? "last " : "1 ", peer);
950 BT_DBG("chan %p orig refcnt %d", chan,
951 atomic_read(&chan->kref.refcount));
952
953 l2cap_chan_put(chan);
954 kfree(peer);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200955 break;
956 }
957 }
958
959 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
960 write_unlock_irqrestore(&devices_lock, flags);
961
962 cancel_delayed_work_sync(&dev->notify_peers);
963
Jukka Rissanen7f118252014-06-18 16:37:11 +0300964 ifdown(dev->netdev);
965
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300966 if (!removed) {
967 INIT_WORK(&entry->delete_netdev, delete_netdev);
968 schedule_work(&entry->delete_netdev);
969 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200970 } else {
971 write_unlock_irqrestore(&devices_lock, flags);
972 }
973
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300974 return;
975}
976
977static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
978{
979 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
980 state_to_string(state), err);
981}
982
983static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
984 unsigned long hdr_len,
985 unsigned long len, int nb)
986{
987 /* Note that we must allocate using GFP_ATOMIC here as
988 * this function is called originally from netdev hard xmit
989 * function in atomic context.
990 */
991 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
992}
993
994static void chan_suspend_cb(struct l2cap_chan *chan)
995{
996 struct sk_buff *skb = chan->data;
997
998 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
999
Jukka Rissanen59790aa2014-09-29 10:55:46 +03001000 if (!skb)
1001 return;
1002
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001003 lowpan_cb(skb)->status = -EAGAIN;
1004}
1005
1006static void chan_resume_cb(struct l2cap_chan *chan)
1007{
1008 struct sk_buff *skb = chan->data;
1009
1010 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
1011
Jukka Rissanen59790aa2014-09-29 10:55:46 +03001012 if (!skb)
1013 return;
1014
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001015 lowpan_cb(skb)->status = 0;
1016}
1017
1018static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
1019{
Jukka Rissanen2ae50d82014-09-08 12:11:43 +03001020 return L2CAP_CONN_TIMEOUT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001021}
1022
1023static const struct l2cap_ops bt_6lowpan_chan_ops = {
1024 .name = "L2CAP 6LoWPAN channel",
1025 .new_connection = chan_new_conn_cb,
1026 .recv = chan_recv_cb,
1027 .close = chan_close_cb,
1028 .state_change = chan_state_change_cb,
1029 .ready = chan_ready_cb,
1030 .resume = chan_resume_cb,
1031 .suspend = chan_suspend_cb,
1032 .get_sndtimeo = chan_get_sndtimeo_cb,
1033 .alloc_skb = chan_alloc_skb_cb,
1034 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
1035
1036 .teardown = l2cap_chan_no_teardown,
1037 .defer = l2cap_chan_no_defer,
1038 .set_shutdown = l2cap_chan_no_set_shutdown,
1039};
1040
1041static inline __u8 bdaddr_type(__u8 type)
1042{
1043 if (type == ADDR_LE_DEV_PUBLIC)
1044 return BDADDR_LE_PUBLIC;
1045 else
1046 return BDADDR_LE_RANDOM;
1047}
1048
1049static struct l2cap_chan *chan_get(void)
1050{
1051 struct l2cap_chan *pchan;
1052
1053 pchan = chan_create();
1054 if (!pchan)
1055 return NULL;
1056
1057 pchan->ops = &bt_6lowpan_chan_ops;
1058
1059 return pchan;
1060}
1061
1062static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
1063{
1064 struct l2cap_chan *pchan;
1065 int err;
1066
1067 pchan = chan_get();
1068 if (!pchan)
1069 return -EINVAL;
1070
1071 err = l2cap_chan_connect(pchan, cpu_to_le16(psm_6lowpan), 0,
1072 addr, dst_type);
1073
1074 BT_DBG("chan %p err %d", pchan, err);
1075 if (err < 0)
1076 l2cap_chan_put(pchan);
1077
Jukka Rissanen18722c22013-12-11 17:05:37 +02001078 return err;
1079}
1080
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001081static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
1082{
1083 struct lowpan_peer *peer;
1084
1085 BT_DBG("conn %p dst type %d", conn, dst_type);
1086
1087 peer = lookup_peer(conn);
1088 if (!peer)
1089 return -ENOENT;
1090
1091 BT_DBG("peer %p chan %p", peer, peer->chan);
1092
1093 l2cap_chan_close(peer->chan, ENOENT);
1094
1095 return 0;
1096}
1097
1098static struct l2cap_chan *bt_6lowpan_listen(void)
1099{
1100 bdaddr_t *addr = BDADDR_ANY;
1101 struct l2cap_chan *pchan;
1102 int err;
1103
1104 if (psm_6lowpan == 0)
1105 return NULL;
1106
1107 pchan = chan_get();
1108 if (!pchan)
1109 return NULL;
1110
1111 pchan->state = BT_LISTEN;
1112 pchan->src_type = BDADDR_LE_PUBLIC;
1113
1114 BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan, pchan,
1115 pchan->src_type);
1116
1117 err = l2cap_add_psm(pchan, addr, cpu_to_le16(psm_6lowpan));
1118 if (err) {
1119 l2cap_chan_put(pchan);
1120 BT_ERR("psm cannot be added err %d", err);
1121 return NULL;
1122 }
1123
1124 return pchan;
1125}
1126
1127static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
1128 struct l2cap_conn **conn)
1129{
1130 struct hci_conn *hcon;
1131 struct hci_dev *hdev;
1132 bdaddr_t *src = BDADDR_ANY;
1133 int n;
1134
1135 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1136 &addr->b[5], &addr->b[4], &addr->b[3],
1137 &addr->b[2], &addr->b[1], &addr->b[0],
1138 addr_type);
1139
1140 if (n < 7)
1141 return -EINVAL;
1142
1143 hdev = hci_get_route(addr, src);
1144 if (!hdev)
1145 return -ENOENT;
1146
1147 hci_dev_lock(hdev);
1148 hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
1149 hci_dev_unlock(hdev);
1150
1151 if (!hcon)
1152 return -ENOENT;
1153
1154 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1155
1156 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1157
1158 return 0;
1159}
1160
1161static void disconnect_all_peers(void)
1162{
1163 struct lowpan_dev *entry, *tmp_dev;
1164 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1165 struct list_head peers;
1166 unsigned long flags;
1167
1168 INIT_LIST_HEAD(&peers);
1169
1170 /* We make a separate list of peers as the close_cb() will
1171 * modify the device peers list so it is better not to mess
1172 * with the same list at the same time.
1173 */
1174
1175 read_lock_irqsave(&devices_lock, flags);
1176
1177 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1178 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list) {
1179 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1180 if (!new_peer)
1181 break;
1182
1183 new_peer->chan = peer->chan;
1184 INIT_LIST_HEAD(&new_peer->list);
1185
1186 list_add(&new_peer->list, &peers);
1187 }
1188 }
1189
1190 read_unlock_irqrestore(&devices_lock, flags);
1191
1192 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1193 l2cap_chan_close(peer->chan, ENOENT);
1194 kfree(peer);
1195 }
1196}
1197
1198static int lowpan_psm_set(void *data, u64 val)
1199{
1200 u16 psm;
1201
1202 psm = val;
1203 if (psm == 0 || psm_6lowpan != psm)
1204 /* Disconnect existing connections if 6lowpan is
1205 * disabled (psm = 0), or if psm changes.
1206 */
1207 disconnect_all_peers();
1208
1209 psm_6lowpan = psm;
1210
1211 if (listen_chan) {
1212 l2cap_chan_close(listen_chan, 0);
1213 l2cap_chan_put(listen_chan);
1214 }
1215
1216 listen_chan = bt_6lowpan_listen();
1217
1218 return 0;
1219}
1220
1221static int lowpan_psm_get(void *data, u64 *val)
1222{
1223 *val = psm_6lowpan;
1224 return 0;
1225}
1226
1227DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops, lowpan_psm_get,
1228 lowpan_psm_set, "%llu\n");
1229
1230static ssize_t lowpan_control_write(struct file *fp,
1231 const char __user *user_buffer,
1232 size_t count,
1233 loff_t *position)
1234{
1235 char buf[32];
1236 size_t buf_size = min(count, sizeof(buf) - 1);
1237 int ret;
1238 bdaddr_t addr;
1239 u8 addr_type;
1240 struct l2cap_conn *conn = NULL;
1241
1242 if (copy_from_user(buf, user_buffer, buf_size))
1243 return -EFAULT;
1244
1245 buf[buf_size] = '\0';
1246
1247 if (memcmp(buf, "connect ", 8) == 0) {
1248 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1249 if (ret == -EINVAL)
1250 return ret;
1251
1252 if (listen_chan) {
1253 l2cap_chan_close(listen_chan, 0);
1254 l2cap_chan_put(listen_chan);
1255 listen_chan = NULL;
1256 }
1257
1258 if (conn) {
1259 struct lowpan_peer *peer;
1260
1261 if (!is_bt_6lowpan(conn->hcon))
1262 return -EINVAL;
1263
1264 peer = lookup_peer(conn);
1265 if (peer) {
1266 BT_DBG("6LoWPAN connection already exists");
1267 return -EALREADY;
1268 }
1269
1270 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1271 &conn->hcon->dst, conn->hcon->dst_type,
1272 addr_type);
1273 }
1274
1275 ret = bt_6lowpan_connect(&addr, addr_type);
1276 if (ret < 0)
1277 return ret;
1278
1279 return count;
1280 }
1281
1282 if (memcmp(buf, "disconnect ", 11) == 0) {
1283 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1284 if (ret < 0)
1285 return ret;
1286
1287 ret = bt_6lowpan_disconnect(conn, addr_type);
1288 if (ret < 0)
1289 return ret;
1290
1291 return count;
1292 }
1293
1294 return count;
1295}
1296
1297static int lowpan_control_show(struct seq_file *f, void *ptr)
1298{
1299 struct lowpan_dev *entry, *tmp_dev;
1300 struct lowpan_peer *peer, *tmp_peer;
1301 unsigned long flags;
1302
1303 read_lock_irqsave(&devices_lock, flags);
1304
1305 list_for_each_entry_safe(entry, tmp_dev, &bt_6lowpan_devices, list) {
1306 list_for_each_entry_safe(peer, tmp_peer, &entry->peers, list)
1307 seq_printf(f, "%pMR (type %u)\n",
1308 &peer->chan->dst, peer->chan->dst_type);
1309 }
1310
1311 read_unlock_irqrestore(&devices_lock, flags);
1312
1313 return 0;
1314}
1315
1316static int lowpan_control_open(struct inode *inode, struct file *file)
1317{
1318 return single_open(file, lowpan_control_show, inode->i_private);
1319}
1320
1321static const struct file_operations lowpan_control_fops = {
1322 .open = lowpan_control_open,
1323 .read = seq_read,
1324 .write = lowpan_control_write,
1325 .llseek = seq_lseek,
1326 .release = single_release,
1327};
1328
Jukka Rissanen7f118252014-06-18 16:37:11 +03001329static void disconnect_devices(void)
1330{
1331 struct lowpan_dev *entry, *tmp, *new_dev;
1332 struct list_head devices;
1333 unsigned long flags;
1334
1335 INIT_LIST_HEAD(&devices);
1336
1337 /* We make a separate list of devices because the unregister_netdev()
1338 * will call device_event() which will also want to modify the same
1339 * devices list.
1340 */
1341
1342 read_lock_irqsave(&devices_lock, flags);
1343
1344 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
1345 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1346 if (!new_dev)
1347 break;
1348
1349 new_dev->netdev = entry->netdev;
1350 INIT_LIST_HEAD(&new_dev->list);
1351
1352 list_add(&new_dev->list, &devices);
1353 }
1354
1355 read_unlock_irqrestore(&devices_lock, flags);
1356
1357 list_for_each_entry_safe(entry, tmp, &devices, list) {
1358 ifdown(entry->netdev);
1359 BT_DBG("Unregistering netdev %s %p",
1360 entry->netdev->name, entry->netdev);
1361 unregister_netdev(entry->netdev);
1362 kfree(entry);
1363 }
1364}
1365
Jukka Rissanen18722c22013-12-11 17:05:37 +02001366static int device_event(struct notifier_block *unused,
1367 unsigned long event, void *ptr)
1368{
1369 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1370 struct lowpan_dev *entry, *tmp;
1371 unsigned long flags;
1372
1373 if (netdev->type != ARPHRD_6LOWPAN)
1374 return NOTIFY_DONE;
1375
1376 switch (event) {
1377 case NETDEV_UNREGISTER:
1378 write_lock_irqsave(&devices_lock, flags);
1379 list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices,
1380 list) {
1381 if (entry->netdev == netdev) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001382 BT_DBG("Unregistered netdev %s %p",
1383 netdev->name, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001384 list_del(&entry->list);
1385 kfree(entry);
1386 break;
1387 }
1388 }
1389 write_unlock_irqrestore(&devices_lock, flags);
1390 break;
1391 }
1392
1393 return NOTIFY_DONE;
1394}
1395
1396static struct notifier_block bt_6lowpan_dev_notifier = {
1397 .notifier_call = device_event,
1398};
1399
Jukka Rissanen5547e482014-06-18 16:37:09 +03001400static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001401{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001402 lowpan_psm_debugfs = debugfs_create_file("6lowpan_psm", 0644,
1403 bt_debugfs, NULL,
1404 &lowpan_psm_fops);
1405 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1406 bt_debugfs, NULL,
1407 &lowpan_control_fops);
1408
Jukka Rissanen18722c22013-12-11 17:05:37 +02001409 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1410}
1411
Jukka Rissanen5547e482014-06-18 16:37:09 +03001412static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001413{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001414 debugfs_remove(lowpan_psm_debugfs);
1415 debugfs_remove(lowpan_control_debugfs);
1416
1417 if (listen_chan) {
1418 l2cap_chan_close(listen_chan, 0);
1419 l2cap_chan_put(listen_chan);
1420 }
1421
Jukka Rissanen7f118252014-06-18 16:37:11 +03001422 disconnect_devices();
1423
Jukka Rissanen18722c22013-12-11 17:05:37 +02001424 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1425}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001426
1427module_init(bt_6lowpan_init);
1428module_exit(bt_6lowpan_exit);
1429
1430MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1431MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1432MODULE_VERSION(VERSION);
1433MODULE_LICENSE("GPL");