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