]>
Commit | Line | Data |
---|---|---|
18722c24 | 1 | /* |
6b8d4a6a | 2 | Copyright (c) 2013-2014 Intel Corp. |
18722c24 JR |
3 | |
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 | ||
18722c24 JR |
14 | #include <linux/if_arp.h> |
15 | #include <linux/netdevice.h> | |
16 | #include <linux/etherdevice.h> | |
5547e48c | 17 | #include <linux/module.h> |
6b8d4a6a | 18 | #include <linux/debugfs.h> |
18722c24 JR |
19 | |
20 | #include <net/ipv6.h> | |
21 | #include <net/ip6_route.h> | |
22 | #include <net/addrconf.h> | |
23 | ||
18722c24 JR |
24 | #include <net/bluetooth/bluetooth.h> |
25 | #include <net/bluetooth/hci_core.h> | |
26 | #include <net/bluetooth/l2cap.h> | |
27 | ||
cefc8c8a | 28 | #include <net/6lowpan.h> /* for the compression support */ |
18722c24 | 29 | |
6b8d4a6a JR |
30 | #define VERSION "0.1" |
31 | ||
7b2ed60e | 32 | static struct dentry *lowpan_enable_debugfs; |
6b8d4a6a JR |
33 | static struct dentry *lowpan_control_debugfs; |
34 | ||
18722c24 | 35 | #define IFACE_NAME_TEMPLATE "bt%d" |
18722c24 JR |
36 | |
37 | struct skb_cb { | |
38 | struct in6_addr addr; | |
39e90c77 | 39 | struct in6_addr gw; |
6b8d4a6a | 40 | struct l2cap_chan *chan; |
18722c24 JR |
41 | }; |
42 | #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb)) | |
43 | ||
44 | /* The devices list contains those devices that we are acting | |
45 | * as a proxy. The BT 6LoWPAN device is a virtual device that | |
46 | * connects to the Bluetooth LE device. The real connection to | |
47 | * BT device is done via l2cap layer. There exists one | |
48 | * virtual device / one BT 6LoWPAN network (=hciX device). | |
49 | * The list contains struct lowpan_dev elements. | |
50 | */ | |
51 | static LIST_HEAD(bt_6lowpan_devices); | |
90305829 | 52 | static DEFINE_SPINLOCK(devices_lock); |
18722c24 | 53 | |
7b2ed60e | 54 | static bool enable_6lowpan; |
6b8d4a6a JR |
55 | |
56 | /* We are listening incoming connections via this channel | |
57 | */ | |
58 | static struct l2cap_chan *listen_chan; | |
59 | ||
18722c24 JR |
60 | struct lowpan_peer { |
61 | struct list_head list; | |
90305829 | 62 | struct rcu_head rcu; |
6b8d4a6a | 63 | struct l2cap_chan *chan; |
18722c24 JR |
64 | |
65 | /* peer addresses in various formats */ | |
fa09ae66 | 66 | unsigned char lladdr[ETH_ALEN]; |
18722c24 JR |
67 | struct in6_addr peer_addr; |
68 | }; | |
69 | ||
2e4d60cb | 70 | struct lowpan_btle_dev { |
18722c24 JR |
71 | struct list_head list; |
72 | ||
73 | struct hci_dev *hdev; | |
74 | struct net_device *netdev; | |
75 | struct list_head peers; | |
76 | atomic_t peer_count; /* number of items in peers list */ | |
77 | ||
78 | struct work_struct delete_netdev; | |
79 | struct delayed_work notify_peers; | |
80 | }; | |
81 | ||
2e4d60cb AA |
82 | static inline struct lowpan_btle_dev * |
83 | lowpan_btle_dev(const struct net_device *netdev) | |
18722c24 | 84 | { |
2e4d60cb | 85 | return (struct lowpan_btle_dev *)lowpan_dev(netdev)->priv; |
18722c24 JR |
86 | } |
87 | ||
2e4d60cb AA |
88 | static inline void peer_add(struct lowpan_btle_dev *dev, |
89 | struct lowpan_peer *peer) | |
18722c24 | 90 | { |
90305829 | 91 | list_add_rcu(&peer->list, &dev->peers); |
18722c24 JR |
92 | atomic_inc(&dev->peer_count); |
93 | } | |
94 | ||
2e4d60cb AA |
95 | static inline bool peer_del(struct lowpan_btle_dev *dev, |
96 | struct lowpan_peer *peer) | |
18722c24 | 97 | { |
90305829 | 98 | list_del_rcu(&peer->list); |
4e790226 | 99 | kfree_rcu(peer, rcu); |
18722c24 | 100 | |
18d93c17 JR |
101 | module_put(THIS_MODULE); |
102 | ||
18722c24 JR |
103 | if (atomic_dec_and_test(&dev->peer_count)) { |
104 | BT_DBG("last peer"); | |
105 | return true; | |
106 | } | |
107 | ||
108 | return false; | |
109 | } | |
110 | ||
2e4d60cb | 111 | static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_btle_dev *dev, |
18722c24 JR |
112 | bdaddr_t *ba, __u8 type) |
113 | { | |
90305829 | 114 | struct lowpan_peer *peer; |
18722c24 JR |
115 | |
116 | BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count), | |
117 | ba, type); | |
118 | ||
90305829 JR |
119 | rcu_read_lock(); |
120 | ||
121 | list_for_each_entry_rcu(peer, &dev->peers, list) { | |
6b8d4a6a JR |
122 | BT_DBG("dst addr %pMR dst type %d", |
123 | &peer->chan->dst, peer->chan->dst_type); | |
18722c24 | 124 | |
6b8d4a6a | 125 | if (bacmp(&peer->chan->dst, ba)) |
18722c24 JR |
126 | continue; |
127 | ||
90305829 JR |
128 | if (type == peer->chan->dst_type) { |
129 | rcu_read_unlock(); | |
6b8d4a6a | 130 | return peer; |
90305829 | 131 | } |
6b8d4a6a JR |
132 | } |
133 | ||
90305829 JR |
134 | rcu_read_unlock(); |
135 | ||
6b8d4a6a JR |
136 | return NULL; |
137 | } | |
138 | ||
2e4d60cb AA |
139 | static inline struct lowpan_peer * |
140 | __peer_lookup_chan(struct lowpan_btle_dev *dev, struct l2cap_chan *chan) | |
6b8d4a6a | 141 | { |
90305829 | 142 | struct lowpan_peer *peer; |
6b8d4a6a | 143 | |
90305829 | 144 | list_for_each_entry_rcu(peer, &dev->peers, list) { |
6b8d4a6a | 145 | if (peer->chan == chan) |
18722c24 JR |
146 | return peer; |
147 | } | |
148 | ||
149 | return NULL; | |
150 | } | |
151 | ||
2e4d60cb AA |
152 | static inline struct lowpan_peer * |
153 | __peer_lookup_conn(struct lowpan_btle_dev *dev, struct l2cap_conn *conn) | |
18722c24 | 154 | { |
90305829 | 155 | struct lowpan_peer *peer; |
18722c24 | 156 | |
90305829 | 157 | list_for_each_entry_rcu(peer, &dev->peers, list) { |
6b8d4a6a | 158 | if (peer->chan->conn == conn) |
18722c24 JR |
159 | return peer; |
160 | } | |
161 | ||
162 | return NULL; | |
163 | } | |
164 | ||
2e4d60cb | 165 | static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_btle_dev *dev, |
39e90c77 JR |
166 | struct in6_addr *daddr, |
167 | struct sk_buff *skb) | |
168 | { | |
90305829 | 169 | struct lowpan_peer *peer; |
39e90c77 JR |
170 | struct in6_addr *nexthop; |
171 | struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); | |
172 | int count = atomic_read(&dev->peer_count); | |
173 | ||
174 | BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt); | |
175 | ||
176 | /* If we have multiple 6lowpan peers, then check where we should | |
177 | * send the packet. If only one peer exists, then we can send the | |
178 | * packet right away. | |
179 | */ | |
90305829 JR |
180 | if (count == 1) { |
181 | rcu_read_lock(); | |
182 | peer = list_first_or_null_rcu(&dev->peers, struct lowpan_peer, | |
183 | list); | |
184 | rcu_read_unlock(); | |
185 | return peer; | |
186 | } | |
39e90c77 JR |
187 | |
188 | if (!rt) { | |
189 | nexthop = &lowpan_cb(skb)->gw; | |
190 | ||
191 | if (ipv6_addr_any(nexthop)) | |
192 | return NULL; | |
193 | } else { | |
2647a9b0 | 194 | nexthop = rt6_nexthop(rt, daddr); |
39e90c77 JR |
195 | |
196 | /* We need to remember the address because it is needed | |
197 | * by bt_xmit() when sending the packet. In bt_xmit(), the | |
198 | * destination routing info is not set. | |
199 | */ | |
200 | memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr)); | |
201 | } | |
202 | ||
203 | BT_DBG("gw %pI6c", nexthop); | |
204 | ||
90305829 JR |
205 | rcu_read_lock(); |
206 | ||
207 | list_for_each_entry_rcu(peer, &dev->peers, list) { | |
39e90c77 JR |
208 | BT_DBG("dst addr %pMR dst type %d ip %pI6c", |
209 | &peer->chan->dst, peer->chan->dst_type, | |
210 | &peer->peer_addr); | |
211 | ||
90305829 JR |
212 | if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) { |
213 | rcu_read_unlock(); | |
39e90c77 | 214 | return peer; |
90305829 | 215 | } |
39e90c77 JR |
216 | } |
217 | ||
90305829 JR |
218 | rcu_read_unlock(); |
219 | ||
39e90c77 JR |
220 | return NULL; |
221 | } | |
222 | ||
18722c24 JR |
223 | static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn) |
224 | { | |
2e4d60cb | 225 | struct lowpan_btle_dev *entry; |
18722c24 | 226 | struct lowpan_peer *peer = NULL; |
18722c24 | 227 | |
90305829 | 228 | rcu_read_lock(); |
18722c24 | 229 | |
90305829 JR |
230 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
231 | peer = __peer_lookup_conn(entry, conn); | |
18722c24 JR |
232 | if (peer) |
233 | break; | |
234 | } | |
235 | ||
90305829 | 236 | rcu_read_unlock(); |
18722c24 JR |
237 | |
238 | return peer; | |
239 | } | |
240 | ||
2e4d60cb | 241 | static struct lowpan_btle_dev *lookup_dev(struct l2cap_conn *conn) |
18722c24 | 242 | { |
2e4d60cb AA |
243 | struct lowpan_btle_dev *entry; |
244 | struct lowpan_btle_dev *dev = NULL; | |
18722c24 | 245 | |
90305829 | 246 | rcu_read_lock(); |
18722c24 | 247 | |
90305829 | 248 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
18722c24 JR |
249 | if (conn->hcon->hdev == entry->hdev) { |
250 | dev = entry; | |
251 | break; | |
252 | } | |
253 | } | |
254 | ||
90305829 | 255 | rcu_read_unlock(); |
18722c24 JR |
256 | |
257 | return dev; | |
258 | } | |
259 | ||
18722c24 JR |
260 | static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev) |
261 | { | |
262 | struct sk_buff *skb_cp; | |
18722c24 JR |
263 | |
264 | skb_cp = skb_copy(skb, GFP_ATOMIC); | |
265 | if (!skb_cp) | |
f8b36176 | 266 | return NET_RX_DROP; |
18722c24 | 267 | |
324e786e | 268 | return netif_rx_ni(skb_cp); |
18722c24 JR |
269 | } |
270 | ||
01141234 | 271 | static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev, |
27ce68a3 | 272 | struct lowpan_peer *peer) |
18722c24 | 273 | { |
c259d141 | 274 | const u8 *saddr; |
2e4d60cb | 275 | struct lowpan_btle_dev *dev; |
18722c24 | 276 | |
2e4d60cb | 277 | dev = lowpan_btle_dev(netdev); |
18722c24 | 278 | |
fa09ae66 | 279 | saddr = peer->lladdr; |
18722c24 | 280 | |
fa09ae66 | 281 | return lowpan_header_decompress(skb, netdev, netdev->dev_addr, saddr); |
18722c24 JR |
282 | } |
283 | ||
284 | static int recv_pkt(struct sk_buff *skb, struct net_device *dev, | |
27ce68a3 | 285 | struct lowpan_peer *peer) |
18722c24 JR |
286 | { |
287 | struct sk_buff *local_skb; | |
288 | int ret; | |
289 | ||
290 | if (!netif_running(dev)) | |
291 | goto drop; | |
292 | ||
cefdb801 | 293 | if (dev->type != ARPHRD_6LOWPAN || !skb->len) |
18722c24 JR |
294 | goto drop; |
295 | ||
cefdb801 AA |
296 | skb_reset_network_header(skb); |
297 | ||
11e3ff70 MT |
298 | skb = skb_share_check(skb, GFP_ATOMIC); |
299 | if (!skb) | |
300 | goto drop; | |
301 | ||
18722c24 | 302 | /* check that it's our buffer */ |
cefdb801 | 303 | if (lowpan_is_ipv6(*skb_network_header(skb))) { |
87f5fedb LD |
304 | /* Pull off the 1-byte of 6lowpan header. */ |
305 | skb_pull(skb, 1); | |
306 | ||
18722c24 JR |
307 | /* Copy the packet so that the IPv6 header is |
308 | * properly aligned. | |
309 | */ | |
310 | local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1, | |
311 | skb_tailroom(skb), GFP_ATOMIC); | |
312 | if (!local_skb) | |
313 | goto drop; | |
314 | ||
315 | local_skb->protocol = htons(ETH_P_IPV6); | |
316 | local_skb->pkt_type = PACKET_HOST; | |
4c58f328 | 317 | local_skb->dev = dev; |
18722c24 | 318 | |
18722c24 JR |
319 | skb_set_transport_header(local_skb, sizeof(struct ipv6hdr)); |
320 | ||
321 | if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) { | |
322 | kfree_skb(local_skb); | |
323 | goto drop; | |
324 | } | |
325 | ||
326 | dev->stats.rx_bytes += skb->len; | |
327 | dev->stats.rx_packets++; | |
328 | ||
3c400b84 MT |
329 | consume_skb(local_skb); |
330 | consume_skb(skb); | |
cefdb801 AA |
331 | } else if (lowpan_is_iphc(*skb_network_header(skb))) { |
332 | local_skb = skb_clone(skb, GFP_ATOMIC); | |
333 | if (!local_skb) | |
334 | goto drop; | |
335 | ||
4c58f328 GRB |
336 | local_skb->dev = dev; |
337 | ||
27ce68a3 | 338 | ret = iphc_decompress(local_skb, dev, peer); |
cefdb801 | 339 | if (ret < 0) { |
da75fdc6 | 340 | BT_DBG("iphc_decompress failed: %d", ret); |
cefdb801 AA |
341 | kfree_skb(local_skb); |
342 | goto drop; | |
343 | } | |
344 | ||
345 | local_skb->protocol = htons(ETH_P_IPV6); | |
346 | local_skb->pkt_type = PACKET_HOST; | |
cefdb801 AA |
347 | |
348 | if (give_skb_to_upper(local_skb, dev) | |
349 | != NET_RX_SUCCESS) { | |
350 | kfree_skb(local_skb); | |
351 | goto drop; | |
18722c24 | 352 | } |
cefdb801 AA |
353 | |
354 | dev->stats.rx_bytes += skb->len; | |
355 | dev->stats.rx_packets++; | |
356 | ||
357 | consume_skb(local_skb); | |
358 | consume_skb(skb); | |
359 | } else { | |
da75fdc6 | 360 | BT_DBG("unknown packet type"); |
cefdb801 | 361 | goto drop; |
18722c24 JR |
362 | } |
363 | ||
364 | return NET_RX_SUCCESS; | |
365 | ||
366 | drop: | |
6b8d4a6a | 367 | dev->stats.rx_dropped++; |
18722c24 JR |
368 | return NET_RX_DROP; |
369 | } | |
370 | ||
371 | /* Packet from BT LE device */ | |
6b8d4a6a | 372 | static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
18722c24 | 373 | { |
2e4d60cb | 374 | struct lowpan_btle_dev *dev; |
18722c24 JR |
375 | struct lowpan_peer *peer; |
376 | int err; | |
377 | ||
6b8d4a6a | 378 | peer = lookup_peer(chan->conn); |
18722c24 JR |
379 | if (!peer) |
380 | return -ENOENT; | |
381 | ||
6b8d4a6a | 382 | dev = lookup_dev(chan->conn); |
30d3db44 | 383 | if (!dev || !dev->netdev) |
18722c24 JR |
384 | return -ENOENT; |
385 | ||
27ce68a3 | 386 | err = recv_pkt(skb, dev->netdev, peer); |
6b8d4a6a JR |
387 | if (err) { |
388 | BT_DBG("recv pkt %d", err); | |
389 | err = -EAGAIN; | |
18722c24 JR |
390 | } |
391 | ||
6b8d4a6a | 392 | return err; |
18722c24 JR |
393 | } |
394 | ||
36b3dd25 JR |
395 | static int setup_header(struct sk_buff *skb, struct net_device *netdev, |
396 | bdaddr_t *peer_addr, u8 *peer_addr_type) | |
18722c24 | 397 | { |
36b3dd25 | 398 | struct in6_addr ipv6_daddr; |
55441070 | 399 | struct ipv6hdr *hdr; |
2e4d60cb | 400 | struct lowpan_btle_dev *dev; |
18722c24 | 401 | struct lowpan_peer *peer; |
9dae2e03 | 402 | u8 *daddr; |
36b3dd25 | 403 | int err, status = 0; |
18722c24 | 404 | |
55441070 GRB |
405 | hdr = ipv6_hdr(skb); |
406 | ||
2e4d60cb | 407 | dev = lowpan_btle_dev(netdev); |
18722c24 | 408 | |
55441070 | 409 | memcpy(&ipv6_daddr, &hdr->daddr, sizeof(ipv6_daddr)); |
36b3dd25 JR |
410 | |
411 | if (ipv6_addr_is_multicast(&ipv6_daddr)) { | |
6b8d4a6a | 412 | lowpan_cb(skb)->chan = NULL; |
9dae2e03 | 413 | daddr = NULL; |
18722c24 | 414 | } else { |
9dae2e03 | 415 | BT_DBG("dest IP %pI6c", &ipv6_daddr); |
18722c24 | 416 | |
9dae2e03 LAD |
417 | /* The packet might be sent to 6lowpan interface |
418 | * because of routing (either via default route | |
419 | * or user set route) so get peer according to | |
420 | * the destination address. | |
18722c24 | 421 | */ |
9dae2e03 | 422 | peer = peer_lookup_dst(dev, &ipv6_daddr, skb); |
18722c24 | 423 | if (!peer) { |
9dae2e03 LAD |
424 | BT_DBG("no such peer"); |
425 | return -ENOENT; | |
18722c24 JR |
426 | } |
427 | ||
fa09ae66 | 428 | daddr = peer->lladdr; |
fa0eaf84 | 429 | *peer_addr = peer->chan->dst; |
9dae2e03 | 430 | *peer_addr_type = peer->chan->dst_type; |
6b8d4a6a | 431 | lowpan_cb(skb)->chan = peer->chan; |
36b3dd25 JR |
432 | |
433 | status = 1; | |
18722c24 JR |
434 | } |
435 | ||
a6f77389 | 436 | lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr); |
36b3dd25 JR |
437 | |
438 | err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0); | |
439 | if (err < 0) | |
440 | return err; | |
441 | ||
442 | return status; | |
443 | } | |
444 | ||
445 | static int header_create(struct sk_buff *skb, struct net_device *netdev, | |
446 | unsigned short type, const void *_daddr, | |
447 | const void *_saddr, unsigned int len) | |
448 | { | |
36b3dd25 JR |
449 | if (type != ETH_P_IPV6) |
450 | return -EINVAL; | |
451 | ||
36b3dd25 | 452 | return 0; |
18722c24 JR |
453 | } |
454 | ||
455 | /* Packet to BT LE device */ | |
6b8d4a6a | 456 | static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb, |
d7b6b0a5 | 457 | struct net_device *netdev) |
18722c24 | 458 | { |
6b8d4a6a JR |
459 | struct msghdr msg; |
460 | struct kvec iv; | |
461 | int err; | |
462 | ||
463 | /* Remember the skb so that we can send EAGAIN to the caller if | |
d7b6b0a5 | 464 | * we run out of credits. |
6b8d4a6a | 465 | */ |
d7b6b0a5 | 466 | chan->data = skb; |
6b8d4a6a | 467 | |
6b8d4a6a JR |
468 | iv.iov_base = skb->data; |
469 | iv.iov_len = skb->len; | |
470 | ||
c0371da6 | 471 | memset(&msg, 0, sizeof(msg)); |
17836394 | 472 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iv, 1, skb->len); |
c0371da6 | 473 | |
6b8d4a6a JR |
474 | err = l2cap_chan_send(chan, &msg, skb->len); |
475 | if (err > 0) { | |
476 | netdev->stats.tx_bytes += err; | |
477 | netdev->stats.tx_packets++; | |
478 | return 0; | |
479 | } | |
480 | ||
481 | if (!err) | |
6dea44f5 | 482 | err = (!chan->tx_credits ? -EAGAIN : 0); |
6b8d4a6a JR |
483 | |
484 | if (err < 0) { | |
485 | if (err == -EAGAIN) | |
486 | netdev->stats.tx_dropped++; | |
487 | else | |
488 | netdev->stats.tx_errors++; | |
489 | } | |
18722c24 | 490 | |
6b8d4a6a | 491 | return err; |
18722c24 JR |
492 | } |
493 | ||
9c238ca8 | 494 | static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev) |
18722c24 JR |
495 | { |
496 | struct sk_buff *local_skb; | |
2e4d60cb | 497 | struct lowpan_btle_dev *entry; |
9c238ca8 | 498 | int err = 0; |
18722c24 | 499 | |
90305829 | 500 | rcu_read_lock(); |
18722c24 | 501 | |
90305829 JR |
502 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
503 | struct lowpan_peer *pentry; | |
2e4d60cb | 504 | struct lowpan_btle_dev *dev; |
18722c24 JR |
505 | |
506 | if (entry->netdev != netdev) | |
507 | continue; | |
508 | ||
2e4d60cb | 509 | dev = lowpan_btle_dev(entry->netdev); |
18722c24 | 510 | |
90305829 | 511 | list_for_each_entry_rcu(pentry, &dev->peers, list) { |
9c238ca8 JR |
512 | int ret; |
513 | ||
18722c24 JR |
514 | local_skb = skb_clone(skb, GFP_ATOMIC); |
515 | ||
36b3dd25 JR |
516 | BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p", |
517 | netdev->name, | |
518 | &pentry->chan->dst, pentry->chan->dst_type, | |
519 | &pentry->peer_addr, pentry->chan); | |
9c238ca8 JR |
520 | ret = send_pkt(pentry->chan, local_skb, netdev); |
521 | if (ret < 0) | |
522 | err = ret; | |
18722c24 JR |
523 | |
524 | kfree_skb(local_skb); | |
525 | } | |
526 | } | |
527 | ||
90305829 | 528 | rcu_read_unlock(); |
9c238ca8 JR |
529 | |
530 | return err; | |
18722c24 JR |
531 | } |
532 | ||
533 | static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev) | |
534 | { | |
535 | int err = 0; | |
18722c24 JR |
536 | bdaddr_t addr; |
537 | u8 addr_type; | |
538 | ||
36b3dd25 JR |
539 | /* We must take a copy of the skb before we modify/replace the ipv6 |
540 | * header as the header could be used elsewhere | |
541 | */ | |
b0c42cd7 AA |
542 | skb = skb_unshare(skb, GFP_ATOMIC); |
543 | if (!skb) | |
36b3dd25 JR |
544 | return NET_XMIT_DROP; |
545 | ||
546 | /* Return values from setup_header() | |
547 | * <0 - error, packet is dropped | |
548 | * 0 - this is a multicast packet | |
549 | * 1 - this is unicast packet | |
550 | */ | |
551 | err = setup_header(skb, netdev, &addr, &addr_type); | |
552 | if (err < 0) { | |
553 | kfree_skb(skb); | |
554 | return NET_XMIT_DROP; | |
555 | } | |
18722c24 | 556 | |
36b3dd25 JR |
557 | if (err) { |
558 | if (lowpan_cb(skb)->chan) { | |
559 | BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p", | |
560 | netdev->name, &addr, addr_type, | |
561 | &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan); | |
d7b6b0a5 | 562 | err = send_pkt(lowpan_cb(skb)->chan, skb, netdev); |
36b3dd25 | 563 | } else { |
6b8d4a6a | 564 | err = -ENOENT; |
36b3dd25 JR |
565 | } |
566 | } else { | |
567 | /* We need to send the packet to every device behind this | |
568 | * interface. | |
569 | */ | |
9c238ca8 | 570 | err = send_mcast_pkt(skb, netdev); |
18722c24 | 571 | } |
18722c24 | 572 | |
fc12518a JR |
573 | dev_kfree_skb(skb); |
574 | ||
18722c24 JR |
575 | if (err) |
576 | BT_DBG("ERROR: xmit failed (%d)", err); | |
577 | ||
36b3dd25 | 578 | return err < 0 ? NET_XMIT_DROP : err; |
18722c24 JR |
579 | } |
580 | ||
df092306 JR |
581 | static int bt_dev_init(struct net_device *dev) |
582 | { | |
d3fff6c4 | 583 | netdev_lockdep_set_classes(dev); |
df092306 JR |
584 | |
585 | return 0; | |
586 | } | |
587 | ||
18722c24 | 588 | static const struct net_device_ops netdev_ops = { |
df092306 | 589 | .ndo_init = bt_dev_init, |
18722c24 JR |
590 | .ndo_start_xmit = bt_xmit, |
591 | }; | |
592 | ||
593 | static struct header_ops header_ops = { | |
594 | .create = header_create, | |
595 | }; | |
596 | ||
597 | static void netdev_setup(struct net_device *dev) | |
598 | { | |
18722c24 JR |
599 | dev->hard_header_len = 0; |
600 | dev->needed_tailroom = 0; | |
156395c9 JR |
601 | dev->flags = IFF_RUNNING | IFF_POINTOPOINT | |
602 | IFF_MULTICAST; | |
18722c24 JR |
603 | dev->watchdog_timeo = 0; |
604 | ||
605 | dev->netdev_ops = &netdev_ops; | |
606 | dev->header_ops = &header_ops; | |
607 | dev->destructor = free_netdev; | |
608 | } | |
609 | ||
610 | static struct device_type bt_type = { | |
611 | .name = "bluetooth", | |
612 | }; | |
613 | ||
18722c24 JR |
614 | static void ifup(struct net_device *netdev) |
615 | { | |
616 | int err; | |
617 | ||
618 | rtnl_lock(); | |
619 | err = dev_open(netdev); | |
620 | if (err < 0) | |
621 | BT_INFO("iface %s cannot be opened (%d)", netdev->name, err); | |
622 | rtnl_unlock(); | |
623 | } | |
624 | ||
7f118253 JR |
625 | static void ifdown(struct net_device *netdev) |
626 | { | |
627 | int err; | |
628 | ||
629 | rtnl_lock(); | |
630 | err = dev_close(netdev); | |
631 | if (err < 0) | |
632 | BT_INFO("iface %s cannot be closed (%d)", netdev->name, err); | |
633 | rtnl_unlock(); | |
634 | } | |
635 | ||
18722c24 JR |
636 | static void do_notify_peers(struct work_struct *work) |
637 | { | |
2e4d60cb AA |
638 | struct lowpan_btle_dev *dev = container_of(work, struct lowpan_btle_dev, |
639 | notify_peers.work); | |
18722c24 JR |
640 | |
641 | netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */ | |
642 | } | |
643 | ||
644 | static bool is_bt_6lowpan(struct hci_conn *hcon) | |
645 | { | |
646 | if (hcon->type != LE_LINK) | |
647 | return false; | |
648 | ||
7b2ed60e | 649 | if (!enable_6lowpan) |
6b8d4a6a JR |
650 | return false; |
651 | ||
652 | return true; | |
18722c24 JR |
653 | } |
654 | ||
6b8d4a6a JR |
655 | static struct l2cap_chan *chan_create(void) |
656 | { | |
657 | struct l2cap_chan *chan; | |
658 | ||
659 | chan = l2cap_chan_create(); | |
660 | if (!chan) | |
661 | return NULL; | |
662 | ||
663 | l2cap_chan_set_defaults(chan); | |
664 | ||
665 | chan->chan_type = L2CAP_CHAN_CONN_ORIENTED; | |
666 | chan->mode = L2CAP_MODE_LE_FLOWCTL; | |
301de2cb | 667 | chan->imtu = 1280; |
6b8d4a6a JR |
668 | |
669 | return chan; | |
670 | } | |
671 | ||
6b8d4a6a | 672 | static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan, |
d2891c4d MS |
673 | struct lowpan_btle_dev *dev, |
674 | bool new_netdev) | |
18722c24 JR |
675 | { |
676 | struct lowpan_peer *peer; | |
18722c24 JR |
677 | |
678 | peer = kzalloc(sizeof(*peer), GFP_ATOMIC); | |
679 | if (!peer) | |
6b8d4a6a | 680 | return NULL; |
18722c24 | 681 | |
6b8d4a6a | 682 | peer->chan = chan; |
18722c24 JR |
683 | memset(&peer->peer_addr, 0, sizeof(struct in6_addr)); |
684 | ||
fa09ae66 | 685 | baswap((void *)peer->lladdr, &chan->dst); |
18722c24 | 686 | |
fa09ae66 | 687 | lowpan_iphc_uncompress_eui48_lladdr(&peer->peer_addr, peer->lladdr); |
18722c24 | 688 | |
90305829 | 689 | spin_lock(&devices_lock); |
18722c24 JR |
690 | INIT_LIST_HEAD(&peer->list); |
691 | peer_add(dev, peer); | |
90305829 | 692 | spin_unlock(&devices_lock); |
18722c24 JR |
693 | |
694 | /* Notifying peers about us needs to be done without locks held */ | |
d2891c4d MS |
695 | if (new_netdev) |
696 | INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers); | |
18722c24 JR |
697 | schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100)); |
698 | ||
6b8d4a6a | 699 | return peer->chan; |
18722c24 JR |
700 | } |
701 | ||
2e4d60cb | 702 | static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev) |
18722c24 | 703 | { |
18722c24 JR |
704 | struct net_device *netdev; |
705 | int err = 0; | |
18722c24 | 706 | |
2e4d60cb | 707 | netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_btle_dev)), |
b72f6f51 AA |
708 | IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN, |
709 | netdev_setup); | |
18722c24 JR |
710 | if (!netdev) |
711 | return -ENOMEM; | |
712 | ||
c259d141 PF |
713 | netdev->addr_assign_type = NET_ADDR_PERM; |
714 | baswap((void *)netdev->dev_addr, &chan->src); | |
18722c24 JR |
715 | |
716 | netdev->netdev_ops = &netdev_ops; | |
fc84242f | 717 | SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev); |
18722c24 JR |
718 | SET_NETDEV_DEVTYPE(netdev, &bt_type); |
719 | ||
2e4d60cb | 720 | *dev = lowpan_btle_dev(netdev); |
5857d1db AA |
721 | (*dev)->netdev = netdev; |
722 | (*dev)->hdev = chan->conn->hcon->hdev; | |
723 | INIT_LIST_HEAD(&(*dev)->peers); | |
724 | ||
725 | spin_lock(&devices_lock); | |
726 | INIT_LIST_HEAD(&(*dev)->list); | |
727 | list_add_rcu(&(*dev)->list, &bt_6lowpan_devices); | |
728 | spin_unlock(&devices_lock); | |
729 | ||
00f59314 | 730 | err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE); |
18722c24 JR |
731 | if (err < 0) { |
732 | BT_INFO("register_netdev failed %d", err); | |
5857d1db AA |
733 | spin_lock(&devices_lock); |
734 | list_del_rcu(&(*dev)->list); | |
735 | spin_unlock(&devices_lock); | |
18722c24 JR |
736 | free_netdev(netdev); |
737 | goto out; | |
738 | } | |
739 | ||
6b8d4a6a JR |
740 | BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d", |
741 | netdev->ifindex, &chan->dst, chan->dst_type, | |
742 | &chan->src, chan->src_type); | |
18722c24 JR |
743 | set_bit(__LINK_STATE_PRESENT, &netdev->state); |
744 | ||
6b8d4a6a | 745 | return 0; |
18722c24 JR |
746 | |
747 | out: | |
748 | return err; | |
749 | } | |
750 | ||
6b8d4a6a JR |
751 | static inline void chan_ready_cb(struct l2cap_chan *chan) |
752 | { | |
2e4d60cb | 753 | struct lowpan_btle_dev *dev; |
d2891c4d | 754 | bool new_netdev = false; |
6b8d4a6a JR |
755 | |
756 | dev = lookup_dev(chan->conn); | |
757 | ||
758 | BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev); | |
759 | ||
760 | if (!dev) { | |
761 | if (setup_netdev(chan, &dev) < 0) { | |
762 | l2cap_chan_del(chan, -ENOENT); | |
763 | return; | |
764 | } | |
d2891c4d | 765 | new_netdev = true; |
6b8d4a6a JR |
766 | } |
767 | ||
18d93c17 JR |
768 | if (!try_module_get(THIS_MODULE)) |
769 | return; | |
770 | ||
d2891c4d | 771 | add_peer_chan(chan, dev, new_netdev); |
6b8d4a6a JR |
772 | ifup(dev->netdev); |
773 | } | |
774 | ||
2b293490 | 775 | static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan) |
6b8d4a6a | 776 | { |
2b293490 | 777 | struct l2cap_chan *chan; |
6b8d4a6a | 778 | |
630ef791 JH |
779 | chan = chan_create(); |
780 | if (!chan) | |
781 | return NULL; | |
782 | ||
2b293490 | 783 | chan->ops = pchan->ops; |
6b8d4a6a JR |
784 | |
785 | BT_DBG("chan %p pchan %p", chan, pchan); | |
786 | ||
2b293490 | 787 | return chan; |
6b8d4a6a JR |
788 | } |
789 | ||
18722c24 JR |
790 | static void delete_netdev(struct work_struct *work) |
791 | { | |
2e4d60cb AA |
792 | struct lowpan_btle_dev *entry = container_of(work, |
793 | struct lowpan_btle_dev, | |
794 | delete_netdev); | |
18722c24 | 795 | |
00f59314 | 796 | lowpan_unregister_netdev(entry->netdev); |
18722c24 | 797 | |
2ad88fb2 | 798 | /* The entry pointer is deleted by the netdev destructor. */ |
18722c24 JR |
799 | } |
800 | ||
6b8d4a6a | 801 | static void chan_close_cb(struct l2cap_chan *chan) |
18722c24 | 802 | { |
2e4d60cb AA |
803 | struct lowpan_btle_dev *entry; |
804 | struct lowpan_btle_dev *dev = NULL; | |
18722c24 JR |
805 | struct lowpan_peer *peer; |
806 | int err = -ENOENT; | |
f63666d2 | 807 | bool last = false, remove = true; |
18722c24 | 808 | |
6b8d4a6a JR |
809 | BT_DBG("chan %p conn %p", chan, chan->conn); |
810 | ||
811 | if (chan->conn && chan->conn->hcon) { | |
812 | if (!is_bt_6lowpan(chan->conn->hcon)) | |
813 | return; | |
814 | ||
815 | /* If conn is set, then the netdev is also there and we should | |
816 | * not remove it. | |
817 | */ | |
f63666d2 | 818 | remove = false; |
6b8d4a6a | 819 | } |
18722c24 | 820 | |
90305829 | 821 | spin_lock(&devices_lock); |
18722c24 | 822 | |
90305829 | 823 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
2e4d60cb | 824 | dev = lowpan_btle_dev(entry->netdev); |
90305829 | 825 | peer = __peer_lookup_chan(dev, chan); |
18722c24 JR |
826 | if (peer) { |
827 | last = peer_del(dev, peer); | |
828 | err = 0; | |
6b8d4a6a JR |
829 | |
830 | BT_DBG("dev %p removing %speer %p", dev, | |
831 | last ? "last " : "1 ", peer); | |
832 | BT_DBG("chan %p orig refcnt %d", chan, | |
2c935bc5 | 833 | kref_read(&chan->kref)); |
6b8d4a6a JR |
834 | |
835 | l2cap_chan_put(chan); | |
18722c24 JR |
836 | break; |
837 | } | |
838 | } | |
839 | ||
840 | if (!err && last && dev && !atomic_read(&dev->peer_count)) { | |
90305829 | 841 | spin_unlock(&devices_lock); |
18722c24 JR |
842 | |
843 | cancel_delayed_work_sync(&dev->notify_peers); | |
844 | ||
7f118253 JR |
845 | ifdown(dev->netdev); |
846 | ||
f63666d2 | 847 | if (remove) { |
6b8d4a6a JR |
848 | INIT_WORK(&entry->delete_netdev, delete_netdev); |
849 | schedule_work(&entry->delete_netdev); | |
850 | } | |
18722c24 | 851 | } else { |
90305829 | 852 | spin_unlock(&devices_lock); |
18722c24 JR |
853 | } |
854 | ||
6b8d4a6a JR |
855 | return; |
856 | } | |
857 | ||
858 | static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err) | |
859 | { | |
860 | BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn, | |
861 | state_to_string(state), err); | |
862 | } | |
863 | ||
864 | static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan, | |
865 | unsigned long hdr_len, | |
866 | unsigned long len, int nb) | |
867 | { | |
868 | /* Note that we must allocate using GFP_ATOMIC here as | |
869 | * this function is called originally from netdev hard xmit | |
870 | * function in atomic context. | |
871 | */ | |
872 | return bt_skb_alloc(hdr_len + len, GFP_ATOMIC); | |
873 | } | |
874 | ||
875 | static void chan_suspend_cb(struct l2cap_chan *chan) | |
876 | { | |
6dea44f5 | 877 | BT_DBG("chan %p suspend", chan); |
6b8d4a6a JR |
878 | } |
879 | ||
880 | static void chan_resume_cb(struct l2cap_chan *chan) | |
881 | { | |
6dea44f5 | 882 | BT_DBG("chan %p resume", chan); |
6b8d4a6a JR |
883 | } |
884 | ||
885 | static long chan_get_sndtimeo_cb(struct l2cap_chan *chan) | |
886 | { | |
2ae50d8d | 887 | return L2CAP_CONN_TIMEOUT; |
6b8d4a6a JR |
888 | } |
889 | ||
890 | static const struct l2cap_ops bt_6lowpan_chan_ops = { | |
891 | .name = "L2CAP 6LoWPAN channel", | |
892 | .new_connection = chan_new_conn_cb, | |
893 | .recv = chan_recv_cb, | |
894 | .close = chan_close_cb, | |
895 | .state_change = chan_state_change_cb, | |
896 | .ready = chan_ready_cb, | |
897 | .resume = chan_resume_cb, | |
898 | .suspend = chan_suspend_cb, | |
899 | .get_sndtimeo = chan_get_sndtimeo_cb, | |
900 | .alloc_skb = chan_alloc_skb_cb, | |
6b8d4a6a JR |
901 | |
902 | .teardown = l2cap_chan_no_teardown, | |
903 | .defer = l2cap_chan_no_defer, | |
904 | .set_shutdown = l2cap_chan_no_set_shutdown, | |
905 | }; | |
906 | ||
907 | static inline __u8 bdaddr_type(__u8 type) | |
908 | { | |
909 | if (type == ADDR_LE_DEV_PUBLIC) | |
910 | return BDADDR_LE_PUBLIC; | |
911 | else | |
912 | return BDADDR_LE_RANDOM; | |
913 | } | |
914 | ||
6b8d4a6a JR |
915 | static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type) |
916 | { | |
0cd088fc | 917 | struct l2cap_chan *chan; |
6b8d4a6a JR |
918 | int err; |
919 | ||
26d46dff | 920 | chan = chan_create(); |
0cd088fc | 921 | if (!chan) |
6b8d4a6a JR |
922 | return -EINVAL; |
923 | ||
26d46dff JH |
924 | chan->ops = &bt_6lowpan_chan_ops; |
925 | ||
0cd088fc | 926 | err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0, |
6b8d4a6a JR |
927 | addr, dst_type); |
928 | ||
0cd088fc | 929 | BT_DBG("chan %p err %d", chan, err); |
6b8d4a6a | 930 | if (err < 0) |
0cd088fc | 931 | l2cap_chan_put(chan); |
6b8d4a6a | 932 | |
18722c24 JR |
933 | return err; |
934 | } | |
935 | ||
6b8d4a6a JR |
936 | static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type) |
937 | { | |
938 | struct lowpan_peer *peer; | |
939 | ||
940 | BT_DBG("conn %p dst type %d", conn, dst_type); | |
941 | ||
942 | peer = lookup_peer(conn); | |
943 | if (!peer) | |
944 | return -ENOENT; | |
945 | ||
946 | BT_DBG("peer %p chan %p", peer, peer->chan); | |
947 | ||
948 | l2cap_chan_close(peer->chan, ENOENT); | |
949 | ||
950 | return 0; | |
951 | } | |
952 | ||
953 | static struct l2cap_chan *bt_6lowpan_listen(void) | |
954 | { | |
955 | bdaddr_t *addr = BDADDR_ANY; | |
0cd088fc | 956 | struct l2cap_chan *chan; |
6b8d4a6a JR |
957 | int err; |
958 | ||
7b2ed60e | 959 | if (!enable_6lowpan) |
6b8d4a6a JR |
960 | return NULL; |
961 | ||
26d46dff | 962 | chan = chan_create(); |
0cd088fc | 963 | if (!chan) |
6b8d4a6a JR |
964 | return NULL; |
965 | ||
26d46dff | 966 | chan->ops = &bt_6lowpan_chan_ops; |
0cd088fc JH |
967 | chan->state = BT_LISTEN; |
968 | chan->src_type = BDADDR_LE_PUBLIC; | |
6b8d4a6a | 969 | |
0cd088fc | 970 | atomic_set(&chan->nesting, L2CAP_NESTING_PARENT); |
2773b024 | 971 | |
0cd088fc | 972 | BT_DBG("chan %p src type %d", chan, chan->src_type); |
6b8d4a6a | 973 | |
0cd088fc | 974 | err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP)); |
6b8d4a6a | 975 | if (err) { |
0cd088fc | 976 | l2cap_chan_put(chan); |
6b8d4a6a JR |
977 | BT_ERR("psm cannot be added err %d", err); |
978 | return NULL; | |
979 | } | |
980 | ||
0cd088fc | 981 | return chan; |
6b8d4a6a JR |
982 | } |
983 | ||
984 | static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type, | |
985 | struct l2cap_conn **conn) | |
986 | { | |
987 | struct hci_conn *hcon; | |
988 | struct hci_dev *hdev; | |
6b8d4a6a JR |
989 | int n; |
990 | ||
991 | n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu", | |
992 | &addr->b[5], &addr->b[4], &addr->b[3], | |
993 | &addr->b[2], &addr->b[1], &addr->b[0], | |
994 | addr_type); | |
995 | ||
996 | if (n < 7) | |
997 | return -EINVAL; | |
998 | ||
39385cb5 JH |
999 | /* The LE_PUBLIC address type is ignored because of BDADDR_ANY */ |
1000 | hdev = hci_get_route(addr, BDADDR_ANY, BDADDR_LE_PUBLIC); | |
6b8d4a6a JR |
1001 | if (!hdev) |
1002 | return -ENOENT; | |
1003 | ||
1004 | hci_dev_lock(hdev); | |
f5ad4ffc | 1005 | hcon = hci_conn_hash_lookup_le(hdev, addr, *addr_type); |
6b8d4a6a JR |
1006 | hci_dev_unlock(hdev); |
1007 | ||
1008 | if (!hcon) | |
1009 | return -ENOENT; | |
1010 | ||
1011 | *conn = (struct l2cap_conn *)hcon->l2cap_data; | |
1012 | ||
1013 | BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type); | |
1014 | ||
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | static void disconnect_all_peers(void) | |
1019 | { | |
2e4d60cb | 1020 | struct lowpan_btle_dev *entry; |
6b8d4a6a JR |
1021 | struct lowpan_peer *peer, *tmp_peer, *new_peer; |
1022 | struct list_head peers; | |
6b8d4a6a JR |
1023 | |
1024 | INIT_LIST_HEAD(&peers); | |
1025 | ||
1026 | /* We make a separate list of peers as the close_cb() will | |
1027 | * modify the device peers list so it is better not to mess | |
1028 | * with the same list at the same time. | |
1029 | */ | |
1030 | ||
90305829 | 1031 | rcu_read_lock(); |
6b8d4a6a | 1032 | |
90305829 JR |
1033 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
1034 | list_for_each_entry_rcu(peer, &entry->peers, list) { | |
6b8d4a6a JR |
1035 | new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC); |
1036 | if (!new_peer) | |
1037 | break; | |
1038 | ||
1039 | new_peer->chan = peer->chan; | |
1040 | INIT_LIST_HEAD(&new_peer->list); | |
1041 | ||
1042 | list_add(&new_peer->list, &peers); | |
1043 | } | |
1044 | } | |
1045 | ||
90305829 | 1046 | rcu_read_unlock(); |
6b8d4a6a | 1047 | |
90305829 | 1048 | spin_lock(&devices_lock); |
6b8d4a6a JR |
1049 | list_for_each_entry_safe(peer, tmp_peer, &peers, list) { |
1050 | l2cap_chan_close(peer->chan, ENOENT); | |
90305829 JR |
1051 | |
1052 | list_del_rcu(&peer->list); | |
4e790226 | 1053 | kfree_rcu(peer, rcu); |
6b8d4a6a | 1054 | } |
90305829 | 1055 | spin_unlock(&devices_lock); |
6b8d4a6a JR |
1056 | } |
1057 | ||
7b2ed60e | 1058 | struct set_enable { |
90305829 | 1059 | struct work_struct work; |
7b2ed60e | 1060 | bool flag; |
90305829 | 1061 | }; |
6b8d4a6a | 1062 | |
7b2ed60e | 1063 | static void do_enable_set(struct work_struct *work) |
90305829 | 1064 | { |
7b2ed60e JR |
1065 | struct set_enable *set_enable = container_of(work, |
1066 | struct set_enable, work); | |
90305829 | 1067 | |
7b2ed60e | 1068 | if (!set_enable->flag || enable_6lowpan != set_enable->flag) |
6b8d4a6a | 1069 | /* Disconnect existing connections if 6lowpan is |
7b2ed60e | 1070 | * disabled |
6b8d4a6a JR |
1071 | */ |
1072 | disconnect_all_peers(); | |
1073 | ||
7b2ed60e | 1074 | enable_6lowpan = set_enable->flag; |
6b8d4a6a JR |
1075 | |
1076 | if (listen_chan) { | |
1077 | l2cap_chan_close(listen_chan, 0); | |
1078 | l2cap_chan_put(listen_chan); | |
1079 | } | |
1080 | ||
1081 | listen_chan = bt_6lowpan_listen(); | |
1082 | ||
7b2ed60e | 1083 | kfree(set_enable); |
90305829 JR |
1084 | } |
1085 | ||
7b2ed60e | 1086 | static int lowpan_enable_set(void *data, u64 val) |
90305829 | 1087 | { |
7b2ed60e | 1088 | struct set_enable *set_enable; |
90305829 | 1089 | |
7b2ed60e JR |
1090 | set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL); |
1091 | if (!set_enable) | |
90305829 JR |
1092 | return -ENOMEM; |
1093 | ||
7b2ed60e JR |
1094 | set_enable->flag = !!val; |
1095 | INIT_WORK(&set_enable->work, do_enable_set); | |
90305829 | 1096 | |
7b2ed60e | 1097 | schedule_work(&set_enable->work); |
90305829 | 1098 | |
6b8d4a6a JR |
1099 | return 0; |
1100 | } | |
1101 | ||
7b2ed60e | 1102 | static int lowpan_enable_get(void *data, u64 *val) |
6b8d4a6a | 1103 | { |
7b2ed60e | 1104 | *val = enable_6lowpan; |
6b8d4a6a JR |
1105 | return 0; |
1106 | } | |
1107 | ||
7b2ed60e JR |
1108 | DEFINE_SIMPLE_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get, |
1109 | lowpan_enable_set, "%llu\n"); | |
6b8d4a6a JR |
1110 | |
1111 | static ssize_t lowpan_control_write(struct file *fp, | |
1112 | const char __user *user_buffer, | |
1113 | size_t count, | |
1114 | loff_t *position) | |
1115 | { | |
1116 | char buf[32]; | |
1117 | size_t buf_size = min(count, sizeof(buf) - 1); | |
1118 | int ret; | |
1119 | bdaddr_t addr; | |
1120 | u8 addr_type; | |
1121 | struct l2cap_conn *conn = NULL; | |
1122 | ||
1123 | if (copy_from_user(buf, user_buffer, buf_size)) | |
1124 | return -EFAULT; | |
1125 | ||
1126 | buf[buf_size] = '\0'; | |
1127 | ||
1128 | if (memcmp(buf, "connect ", 8) == 0) { | |
1129 | ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn); | |
1130 | if (ret == -EINVAL) | |
1131 | return ret; | |
1132 | ||
1133 | if (listen_chan) { | |
1134 | l2cap_chan_close(listen_chan, 0); | |
1135 | l2cap_chan_put(listen_chan); | |
1136 | listen_chan = NULL; | |
1137 | } | |
1138 | ||
1139 | if (conn) { | |
1140 | struct lowpan_peer *peer; | |
1141 | ||
1142 | if (!is_bt_6lowpan(conn->hcon)) | |
1143 | return -EINVAL; | |
1144 | ||
1145 | peer = lookup_peer(conn); | |
1146 | if (peer) { | |
1147 | BT_DBG("6LoWPAN connection already exists"); | |
1148 | return -EALREADY; | |
1149 | } | |
1150 | ||
1151 | BT_DBG("conn %p dst %pMR type %d user %d", conn, | |
1152 | &conn->hcon->dst, conn->hcon->dst_type, | |
1153 | addr_type); | |
1154 | } | |
1155 | ||
1156 | ret = bt_6lowpan_connect(&addr, addr_type); | |
1157 | if (ret < 0) | |
1158 | return ret; | |
1159 | ||
1160 | return count; | |
1161 | } | |
1162 | ||
1163 | if (memcmp(buf, "disconnect ", 11) == 0) { | |
1164 | ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn); | |
1165 | if (ret < 0) | |
1166 | return ret; | |
1167 | ||
1168 | ret = bt_6lowpan_disconnect(conn, addr_type); | |
1169 | if (ret < 0) | |
1170 | return ret; | |
1171 | ||
1172 | return count; | |
1173 | } | |
1174 | ||
1175 | return count; | |
1176 | } | |
1177 | ||
1178 | static int lowpan_control_show(struct seq_file *f, void *ptr) | |
1179 | { | |
2e4d60cb | 1180 | struct lowpan_btle_dev *entry; |
90305829 | 1181 | struct lowpan_peer *peer; |
6b8d4a6a | 1182 | |
90305829 | 1183 | spin_lock(&devices_lock); |
6b8d4a6a | 1184 | |
90305829 JR |
1185 | list_for_each_entry(entry, &bt_6lowpan_devices, list) { |
1186 | list_for_each_entry(peer, &entry->peers, list) | |
6b8d4a6a JR |
1187 | seq_printf(f, "%pMR (type %u)\n", |
1188 | &peer->chan->dst, peer->chan->dst_type); | |
1189 | } | |
1190 | ||
90305829 | 1191 | spin_unlock(&devices_lock); |
6b8d4a6a JR |
1192 | |
1193 | return 0; | |
1194 | } | |
1195 | ||
1196 | static int lowpan_control_open(struct inode *inode, struct file *file) | |
1197 | { | |
1198 | return single_open(file, lowpan_control_show, inode->i_private); | |
1199 | } | |
1200 | ||
1201 | static const struct file_operations lowpan_control_fops = { | |
1202 | .open = lowpan_control_open, | |
1203 | .read = seq_read, | |
1204 | .write = lowpan_control_write, | |
1205 | .llseek = seq_lseek, | |
1206 | .release = single_release, | |
1207 | }; | |
1208 | ||
7f118253 JR |
1209 | static void disconnect_devices(void) |
1210 | { | |
2e4d60cb | 1211 | struct lowpan_btle_dev *entry, *tmp, *new_dev; |
7f118253 | 1212 | struct list_head devices; |
7f118253 JR |
1213 | |
1214 | INIT_LIST_HEAD(&devices); | |
1215 | ||
1216 | /* We make a separate list of devices because the unregister_netdev() | |
1217 | * will call device_event() which will also want to modify the same | |
1218 | * devices list. | |
1219 | */ | |
1220 | ||
90305829 | 1221 | rcu_read_lock(); |
7f118253 | 1222 | |
90305829 | 1223 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
7f118253 JR |
1224 | new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC); |
1225 | if (!new_dev) | |
1226 | break; | |
1227 | ||
1228 | new_dev->netdev = entry->netdev; | |
1229 | INIT_LIST_HEAD(&new_dev->list); | |
1230 | ||
90305829 | 1231 | list_add_rcu(&new_dev->list, &devices); |
7f118253 JR |
1232 | } |
1233 | ||
90305829 | 1234 | rcu_read_unlock(); |
7f118253 | 1235 | |
daac197c | 1236 | list_for_each_entry_safe(entry, tmp, &devices, list) { |
7f118253 JR |
1237 | ifdown(entry->netdev); |
1238 | BT_DBG("Unregistering netdev %s %p", | |
1239 | entry->netdev->name, entry->netdev); | |
00f59314 | 1240 | lowpan_unregister_netdev(entry->netdev); |
7f118253 JR |
1241 | kfree(entry); |
1242 | } | |
1243 | } | |
1244 | ||
18722c24 JR |
1245 | static int device_event(struct notifier_block *unused, |
1246 | unsigned long event, void *ptr) | |
1247 | { | |
1248 | struct net_device *netdev = netdev_notifier_info_to_dev(ptr); | |
2e4d60cb | 1249 | struct lowpan_btle_dev *entry; |
18722c24 JR |
1250 | |
1251 | if (netdev->type != ARPHRD_6LOWPAN) | |
1252 | return NOTIFY_DONE; | |
1253 | ||
1254 | switch (event) { | |
1255 | case NETDEV_UNREGISTER: | |
90305829 JR |
1256 | spin_lock(&devices_lock); |
1257 | list_for_each_entry(entry, &bt_6lowpan_devices, list) { | |
18722c24 | 1258 | if (entry->netdev == netdev) { |
7f118253 JR |
1259 | BT_DBG("Unregistered netdev %s %p", |
1260 | netdev->name, netdev); | |
18722c24 | 1261 | list_del(&entry->list); |
18722c24 JR |
1262 | break; |
1263 | } | |
1264 | } | |
90305829 | 1265 | spin_unlock(&devices_lock); |
18722c24 JR |
1266 | break; |
1267 | } | |
1268 | ||
1269 | return NOTIFY_DONE; | |
1270 | } | |
1271 | ||
1272 | static struct notifier_block bt_6lowpan_dev_notifier = { | |
1273 | .notifier_call = device_event, | |
1274 | }; | |
1275 | ||
5547e48c | 1276 | static int __init bt_6lowpan_init(void) |
18722c24 | 1277 | { |
7b2ed60e JR |
1278 | lowpan_enable_debugfs = debugfs_create_file("6lowpan_enable", 0644, |
1279 | bt_debugfs, NULL, | |
1280 | &lowpan_enable_fops); | |
6b8d4a6a JR |
1281 | lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644, |
1282 | bt_debugfs, NULL, | |
1283 | &lowpan_control_fops); | |
1284 | ||
18722c24 JR |
1285 | return register_netdevice_notifier(&bt_6lowpan_dev_notifier); |
1286 | } | |
1287 | ||
5547e48c | 1288 | static void __exit bt_6lowpan_exit(void) |
18722c24 | 1289 | { |
7b2ed60e | 1290 | debugfs_remove(lowpan_enable_debugfs); |
6b8d4a6a JR |
1291 | debugfs_remove(lowpan_control_debugfs); |
1292 | ||
1293 | if (listen_chan) { | |
1294 | l2cap_chan_close(listen_chan, 0); | |
1295 | l2cap_chan_put(listen_chan); | |
1296 | } | |
1297 | ||
7f118253 JR |
1298 | disconnect_devices(); |
1299 | ||
18722c24 JR |
1300 | unregister_netdevice_notifier(&bt_6lowpan_dev_notifier); |
1301 | } | |
5547e48c JR |
1302 | |
1303 | module_init(bt_6lowpan_init); | |
1304 | module_exit(bt_6lowpan_exit); | |
1305 | ||
1306 | MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>"); | |
1307 | MODULE_DESCRIPTION("Bluetooth 6LoWPAN"); | |
1308 | MODULE_VERSION(VERSION); | |
1309 | MODULE_LICENSE("GPL"); |