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 | ||
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 | ||
cefc8c8a | 30 | #include <net/6lowpan.h> /* for the compression support */ |
18722c24 | 31 | |
6b8d4a6a JR |
32 | #define VERSION "0.1" |
33 | ||
7b2ed60e | 34 | static struct dentry *lowpan_enable_debugfs; |
6b8d4a6a JR |
35 | static struct dentry *lowpan_control_debugfs; |
36 | ||
18722c24 | 37 | #define IFACE_NAME_TEMPLATE "bt%d" |
18722c24 JR |
38 | |
39 | struct skb_cb { | |
40 | struct in6_addr addr; | |
39e90c77 | 41 | struct in6_addr gw; |
6b8d4a6a JR |
42 | struct l2cap_chan *chan; |
43 | int status; | |
18722c24 JR |
44 | }; |
45 | #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb)) | |
46 | ||
47 | /* The devices list contains those devices that we are acting | |
48 | * as a proxy. The BT 6LoWPAN device is a virtual device that | |
49 | * connects to the Bluetooth LE device. The real connection to | |
50 | * BT device is done via l2cap layer. There exists one | |
51 | * virtual device / one BT 6LoWPAN network (=hciX device). | |
52 | * The list contains struct lowpan_dev elements. | |
53 | */ | |
54 | static LIST_HEAD(bt_6lowpan_devices); | |
90305829 | 55 | static DEFINE_SPINLOCK(devices_lock); |
18722c24 | 56 | |
7b2ed60e | 57 | static bool enable_6lowpan; |
6b8d4a6a JR |
58 | |
59 | /* We are listening incoming connections via this channel | |
60 | */ | |
61 | static struct l2cap_chan *listen_chan; | |
62 | ||
18722c24 JR |
63 | struct lowpan_peer { |
64 | struct list_head list; | |
90305829 | 65 | struct rcu_head rcu; |
6b8d4a6a | 66 | struct l2cap_chan *chan; |
18722c24 JR |
67 | |
68 | /* peer addresses in various formats */ | |
69 | unsigned char eui64_addr[EUI64_ADDR_LEN]; | |
70 | struct in6_addr peer_addr; | |
71 | }; | |
72 | ||
73 | struct lowpan_dev { | |
74 | struct list_head list; | |
75 | ||
76 | struct hci_dev *hdev; | |
77 | struct net_device *netdev; | |
78 | struct list_head peers; | |
79 | atomic_t peer_count; /* number of items in peers list */ | |
80 | ||
81 | struct work_struct delete_netdev; | |
82 | struct delayed_work notify_peers; | |
83 | }; | |
84 | ||
85 | static inline struct lowpan_dev *lowpan_dev(const struct net_device *netdev) | |
86 | { | |
b72f6f51 | 87 | return (struct lowpan_dev *)lowpan_priv(netdev)->priv; |
18722c24 JR |
88 | } |
89 | ||
90 | static inline void peer_add(struct lowpan_dev *dev, struct lowpan_peer *peer) | |
91 | { | |
90305829 | 92 | list_add_rcu(&peer->list, &dev->peers); |
18722c24 JR |
93 | atomic_inc(&dev->peer_count); |
94 | } | |
95 | ||
96 | static inline bool peer_del(struct lowpan_dev *dev, struct lowpan_peer *peer) | |
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 | ||
111 | static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_dev *dev, | |
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 | ||
90305829 JR |
139 | static inline struct lowpan_peer *__peer_lookup_chan(struct lowpan_dev *dev, |
140 | 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 | ||
90305829 JR |
152 | static inline struct lowpan_peer *__peer_lookup_conn(struct lowpan_dev *dev, |
153 | 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 | ||
39e90c77 JR |
165 | static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_dev *dev, |
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 | { | |
90305829 | 225 | struct lowpan_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 | ||
241 | static struct lowpan_dev *lookup_dev(struct l2cap_conn *conn) | |
242 | { | |
90305829 | 243 | struct lowpan_dev *entry; |
18722c24 | 244 | struct lowpan_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 | |
4456c50d | 268 | return netif_rx(skb_cp); |
18722c24 JR |
269 | } |
270 | ||
01141234 MT |
271 | static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev, |
272 | struct l2cap_chan *chan) | |
18722c24 JR |
273 | { |
274 | const u8 *saddr, *daddr; | |
275 | u8 iphc0, iphc1; | |
276 | struct lowpan_dev *dev; | |
277 | struct lowpan_peer *peer; | |
18722c24 JR |
278 | |
279 | dev = lowpan_dev(netdev); | |
280 | ||
90305829 JR |
281 | rcu_read_lock(); |
282 | peer = __peer_lookup_chan(dev, chan); | |
283 | rcu_read_unlock(); | |
18722c24 | 284 | if (!peer) |
56b2c3ee | 285 | return -EINVAL; |
18722c24 JR |
286 | |
287 | saddr = peer->eui64_addr; | |
288 | daddr = dev->netdev->dev_addr; | |
289 | ||
290 | /* at least two bytes will be used for the encoding */ | |
291 | if (skb->len < 2) | |
56b2c3ee | 292 | return -EINVAL; |
18722c24 JR |
293 | |
294 | if (lowpan_fetch_skb_u8(skb, &iphc0)) | |
56b2c3ee | 295 | return -EINVAL; |
18722c24 JR |
296 | |
297 | if (lowpan_fetch_skb_u8(skb, &iphc1)) | |
56b2c3ee | 298 | return -EINVAL; |
18722c24 | 299 | |
01141234 MT |
300 | return lowpan_header_decompress(skb, netdev, |
301 | saddr, IEEE802154_ADDR_LONG, | |
302 | EUI64_ADDR_LEN, daddr, | |
303 | IEEE802154_ADDR_LONG, EUI64_ADDR_LEN, | |
304 | iphc0, iphc1); | |
18722c24 | 305 | |
18722c24 JR |
306 | } |
307 | ||
308 | static int recv_pkt(struct sk_buff *skb, struct net_device *dev, | |
6b8d4a6a | 309 | struct l2cap_chan *chan) |
18722c24 JR |
310 | { |
311 | struct sk_buff *local_skb; | |
312 | int ret; | |
313 | ||
314 | if (!netif_running(dev)) | |
315 | goto drop; | |
316 | ||
cefdb801 | 317 | if (dev->type != ARPHRD_6LOWPAN || !skb->len) |
18722c24 JR |
318 | goto drop; |
319 | ||
cefdb801 AA |
320 | skb_reset_network_header(skb); |
321 | ||
11e3ff70 MT |
322 | skb = skb_share_check(skb, GFP_ATOMIC); |
323 | if (!skb) | |
324 | goto drop; | |
325 | ||
18722c24 | 326 | /* check that it's our buffer */ |
cefdb801 | 327 | if (lowpan_is_ipv6(*skb_network_header(skb))) { |
18722c24 JR |
328 | /* Copy the packet so that the IPv6 header is |
329 | * properly aligned. | |
330 | */ | |
331 | local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1, | |
332 | skb_tailroom(skb), GFP_ATOMIC); | |
333 | if (!local_skb) | |
334 | goto drop; | |
335 | ||
336 | local_skb->protocol = htons(ETH_P_IPV6); | |
337 | local_skb->pkt_type = PACKET_HOST; | |
338 | ||
18722c24 JR |
339 | skb_set_transport_header(local_skb, sizeof(struct ipv6hdr)); |
340 | ||
341 | if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) { | |
342 | kfree_skb(local_skb); | |
343 | goto drop; | |
344 | } | |
345 | ||
346 | dev->stats.rx_bytes += skb->len; | |
347 | dev->stats.rx_packets++; | |
348 | ||
3c400b84 MT |
349 | consume_skb(local_skb); |
350 | consume_skb(skb); | |
cefdb801 AA |
351 | } else if (lowpan_is_iphc(*skb_network_header(skb))) { |
352 | local_skb = skb_clone(skb, GFP_ATOMIC); | |
353 | if (!local_skb) | |
354 | goto drop; | |
355 | ||
356 | ret = iphc_decompress(local_skb, dev, chan); | |
357 | if (ret < 0) { | |
358 | kfree_skb(local_skb); | |
359 | goto drop; | |
360 | } | |
361 | ||
362 | local_skb->protocol = htons(ETH_P_IPV6); | |
363 | local_skb->pkt_type = PACKET_HOST; | |
364 | local_skb->dev = dev; | |
365 | ||
366 | if (give_skb_to_upper(local_skb, dev) | |
367 | != NET_RX_SUCCESS) { | |
368 | kfree_skb(local_skb); | |
369 | goto drop; | |
18722c24 | 370 | } |
cefdb801 AA |
371 | |
372 | dev->stats.rx_bytes += skb->len; | |
373 | dev->stats.rx_packets++; | |
374 | ||
375 | consume_skb(local_skb); | |
376 | consume_skb(skb); | |
377 | } else { | |
378 | goto drop; | |
18722c24 JR |
379 | } |
380 | ||
381 | return NET_RX_SUCCESS; | |
382 | ||
383 | drop: | |
6b8d4a6a | 384 | dev->stats.rx_dropped++; |
18722c24 JR |
385 | return NET_RX_DROP; |
386 | } | |
387 | ||
388 | /* Packet from BT LE device */ | |
6b8d4a6a | 389 | static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
18722c24 JR |
390 | { |
391 | struct lowpan_dev *dev; | |
392 | struct lowpan_peer *peer; | |
393 | int err; | |
394 | ||
6b8d4a6a | 395 | peer = lookup_peer(chan->conn); |
18722c24 JR |
396 | if (!peer) |
397 | return -ENOENT; | |
398 | ||
6b8d4a6a | 399 | dev = lookup_dev(chan->conn); |
30d3db44 | 400 | if (!dev || !dev->netdev) |
18722c24 JR |
401 | return -ENOENT; |
402 | ||
6b8d4a6a JR |
403 | err = recv_pkt(skb, dev->netdev, chan); |
404 | if (err) { | |
405 | BT_DBG("recv pkt %d", err); | |
406 | err = -EAGAIN; | |
18722c24 JR |
407 | } |
408 | ||
6b8d4a6a | 409 | return err; |
18722c24 JR |
410 | } |
411 | ||
62bbd5b3 | 412 | static u8 get_addr_type_from_eui64(u8 byte) |
18722c24 | 413 | { |
6b8d4a6a JR |
414 | /* Is universal(0) or local(1) bit */ |
415 | return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC); | |
62bbd5b3 JR |
416 | } |
417 | ||
418 | static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr) | |
419 | { | |
420 | u8 *eui64 = ip6_daddr->s6_addr + 8; | |
18722c24 JR |
421 | |
422 | addr->b[0] = eui64[7]; | |
423 | addr->b[1] = eui64[6]; | |
424 | addr->b[2] = eui64[5]; | |
425 | addr->b[3] = eui64[2]; | |
426 | addr->b[4] = eui64[1]; | |
427 | addr->b[5] = eui64[0]; | |
62bbd5b3 | 428 | } |
18722c24 | 429 | |
62bbd5b3 JR |
430 | static void convert_dest_bdaddr(struct in6_addr *ip6_daddr, |
431 | bdaddr_t *addr, u8 *addr_type) | |
432 | { | |
433 | copy_to_bdaddr(ip6_daddr, addr); | |
18722c24 | 434 | |
62bbd5b3 JR |
435 | /* We need to toggle the U/L bit that we got from IPv6 address |
436 | * so that we get the proper address and type of the BD address. | |
437 | */ | |
438 | addr->b[5] ^= 0x02; | |
439 | ||
440 | *addr_type = get_addr_type_from_eui64(addr->b[5]); | |
18722c24 JR |
441 | } |
442 | ||
36b3dd25 JR |
443 | static int setup_header(struct sk_buff *skb, struct net_device *netdev, |
444 | bdaddr_t *peer_addr, u8 *peer_addr_type) | |
18722c24 | 445 | { |
36b3dd25 | 446 | struct in6_addr ipv6_daddr; |
18722c24 JR |
447 | struct lowpan_dev *dev; |
448 | struct lowpan_peer *peer; | |
449 | bdaddr_t addr, *any = BDADDR_ANY; | |
36b3dd25 JR |
450 | u8 *daddr = any->b; |
451 | int err, status = 0; | |
18722c24 JR |
452 | |
453 | dev = lowpan_dev(netdev); | |
454 | ||
36b3dd25 JR |
455 | memcpy(&ipv6_daddr, &lowpan_cb(skb)->addr, sizeof(ipv6_daddr)); |
456 | ||
457 | if (ipv6_addr_is_multicast(&ipv6_daddr)) { | |
6b8d4a6a | 458 | lowpan_cb(skb)->chan = NULL; |
18722c24 | 459 | } else { |
36b3dd25 | 460 | u8 addr_type; |
18722c24 JR |
461 | |
462 | /* Get destination BT device from skb. | |
463 | * If there is no such peer then discard the packet. | |
464 | */ | |
36b3dd25 | 465 | convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type); |
18722c24 | 466 | |
6b8d4a6a | 467 | BT_DBG("dest addr %pMR type %d IP %pI6c", &addr, |
36b3dd25 | 468 | addr_type, &ipv6_daddr); |
18722c24 | 469 | |
18722c24 | 470 | peer = peer_lookup_ba(dev, &addr, addr_type); |
18722c24 | 471 | if (!peer) { |
39e90c77 JR |
472 | /* The packet might be sent to 6lowpan interface |
473 | * because of routing (either via default route | |
474 | * or user set route) so get peer according to | |
475 | * the destination address. | |
476 | */ | |
36b3dd25 | 477 | peer = peer_lookup_dst(dev, &ipv6_daddr, skb); |
39e90c77 JR |
478 | if (!peer) { |
479 | BT_DBG("no such peer %pMR found", &addr); | |
480 | return -ENOENT; | |
481 | } | |
18722c24 JR |
482 | } |
483 | ||
484 | daddr = peer->eui64_addr; | |
36b3dd25 JR |
485 | *peer_addr = addr; |
486 | *peer_addr_type = addr_type; | |
6b8d4a6a | 487 | lowpan_cb(skb)->chan = peer->chan; |
36b3dd25 JR |
488 | |
489 | status = 1; | |
18722c24 JR |
490 | } |
491 | ||
36b3dd25 JR |
492 | lowpan_header_compress(skb, netdev, ETH_P_IPV6, daddr, |
493 | dev->netdev->dev_addr, skb->len); | |
494 | ||
495 | 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 | ||
502 | static 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)); | |
18722c24 | 514 | |
36b3dd25 | 515 | return 0; |
18722c24 JR |
516 | } |
517 | ||
518 | /* Packet to BT LE device */ | |
6b8d4a6a | 519 | static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb, |
d7b6b0a5 | 520 | struct net_device *netdev) |
18722c24 | 521 | { |
6b8d4a6a JR |
522 | struct msghdr msg; |
523 | struct kvec iv; | |
524 | int err; | |
525 | ||
526 | /* Remember the skb so that we can send EAGAIN to the caller if | |
d7b6b0a5 | 527 | * we run out of credits. |
6b8d4a6a | 528 | */ |
d7b6b0a5 | 529 | chan->data = skb; |
6b8d4a6a | 530 | |
6b8d4a6a JR |
531 | iv.iov_base = skb->data; |
532 | iv.iov_len = skb->len; | |
533 | ||
c0371da6 | 534 | memset(&msg, 0, sizeof(msg)); |
17836394 | 535 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iv, 1, skb->len); |
c0371da6 | 536 | |
6b8d4a6a JR |
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 | } | |
18722c24 | 553 | |
6b8d4a6a | 554 | return err; |
18722c24 JR |
555 | } |
556 | ||
9c238ca8 | 557 | static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev) |
18722c24 JR |
558 | { |
559 | struct sk_buff *local_skb; | |
90305829 | 560 | struct lowpan_dev *entry; |
9c238ca8 | 561 | int err = 0; |
18722c24 | 562 | |
90305829 | 563 | rcu_read_lock(); |
18722c24 | 564 | |
90305829 JR |
565 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
566 | struct lowpan_peer *pentry; | |
18722c24 JR |
567 | struct lowpan_dev *dev; |
568 | ||
569 | if (entry->netdev != netdev) | |
570 | continue; | |
571 | ||
572 | dev = lowpan_dev(entry->netdev); | |
573 | ||
90305829 | 574 | list_for_each_entry_rcu(pentry, &dev->peers, list) { |
9c238ca8 JR |
575 | int ret; |
576 | ||
18722c24 JR |
577 | local_skb = skb_clone(skb, GFP_ATOMIC); |
578 | ||
36b3dd25 JR |
579 | BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p", |
580 | netdev->name, | |
581 | &pentry->chan->dst, pentry->chan->dst_type, | |
582 | &pentry->peer_addr, pentry->chan); | |
9c238ca8 JR |
583 | ret = send_pkt(pentry->chan, local_skb, netdev); |
584 | if (ret < 0) | |
585 | err = ret; | |
18722c24 JR |
586 | |
587 | kfree_skb(local_skb); | |
588 | } | |
589 | } | |
590 | ||
90305829 | 591 | rcu_read_unlock(); |
9c238ca8 JR |
592 | |
593 | return err; | |
18722c24 JR |
594 | } |
595 | ||
596 | static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev) | |
597 | { | |
598 | int err = 0; | |
18722c24 JR |
599 | bdaddr_t addr; |
600 | u8 addr_type; | |
601 | ||
36b3dd25 JR |
602 | /* We must take a copy of the skb before we modify/replace the ipv6 |
603 | * header as the header could be used elsewhere | |
604 | */ | |
b0c42cd7 AA |
605 | skb = skb_unshare(skb, GFP_ATOMIC); |
606 | if (!skb) | |
36b3dd25 JR |
607 | return NET_XMIT_DROP; |
608 | ||
609 | /* Return values from setup_header() | |
610 | * <0 - error, packet is dropped | |
611 | * 0 - this is a multicast packet | |
612 | * 1 - this is unicast packet | |
613 | */ | |
614 | err = setup_header(skb, netdev, &addr, &addr_type); | |
615 | if (err < 0) { | |
616 | kfree_skb(skb); | |
617 | return NET_XMIT_DROP; | |
618 | } | |
18722c24 | 619 | |
36b3dd25 JR |
620 | if (err) { |
621 | if (lowpan_cb(skb)->chan) { | |
622 | BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p", | |
623 | netdev->name, &addr, addr_type, | |
624 | &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan); | |
d7b6b0a5 | 625 | err = send_pkt(lowpan_cb(skb)->chan, skb, netdev); |
36b3dd25 | 626 | } else { |
6b8d4a6a | 627 | err = -ENOENT; |
36b3dd25 JR |
628 | } |
629 | } else { | |
630 | /* We need to send the packet to every device behind this | |
631 | * interface. | |
632 | */ | |
9c238ca8 | 633 | err = send_mcast_pkt(skb, netdev); |
18722c24 | 634 | } |
18722c24 | 635 | |
fc12518a JR |
636 | dev_kfree_skb(skb); |
637 | ||
18722c24 JR |
638 | if (err) |
639 | BT_DBG("ERROR: xmit failed (%d)", err); | |
640 | ||
36b3dd25 | 641 | return err < 0 ? NET_XMIT_DROP : err; |
18722c24 JR |
642 | } |
643 | ||
df092306 JR |
644 | static struct lock_class_key bt_tx_busylock; |
645 | static struct lock_class_key bt_netdev_xmit_lock_key; | |
646 | ||
647 | static void bt_set_lockdep_class_one(struct net_device *dev, | |
648 | struct netdev_queue *txq, | |
649 | void *_unused) | |
650 | { | |
651 | lockdep_set_class(&txq->_xmit_lock, &bt_netdev_xmit_lock_key); | |
652 | } | |
653 | ||
654 | static int bt_dev_init(struct net_device *dev) | |
655 | { | |
656 | netdev_for_each_tx_queue(dev, bt_set_lockdep_class_one, NULL); | |
657 | dev->qdisc_tx_busylock = &bt_tx_busylock; | |
658 | ||
659 | return 0; | |
660 | } | |
661 | ||
18722c24 | 662 | static const struct net_device_ops netdev_ops = { |
df092306 | 663 | .ndo_init = bt_dev_init, |
18722c24 JR |
664 | .ndo_start_xmit = bt_xmit, |
665 | }; | |
666 | ||
667 | static struct header_ops header_ops = { | |
668 | .create = header_create, | |
669 | }; | |
670 | ||
671 | static void netdev_setup(struct net_device *dev) | |
672 | { | |
18722c24 JR |
673 | dev->hard_header_len = 0; |
674 | dev->needed_tailroom = 0; | |
156395c9 JR |
675 | dev->flags = IFF_RUNNING | IFF_POINTOPOINT | |
676 | IFF_MULTICAST; | |
18722c24 JR |
677 | dev->watchdog_timeo = 0; |
678 | ||
679 | dev->netdev_ops = &netdev_ops; | |
680 | dev->header_ops = &header_ops; | |
681 | dev->destructor = free_netdev; | |
682 | } | |
683 | ||
684 | static struct device_type bt_type = { | |
685 | .name = "bluetooth", | |
686 | }; | |
687 | ||
688 | static void set_addr(u8 *eui, u8 *addr, u8 addr_type) | |
689 | { | |
690 | /* addr is the BT address in little-endian format */ | |
691 | eui[0] = addr[5]; | |
692 | eui[1] = addr[4]; | |
693 | eui[2] = addr[3]; | |
694 | eui[3] = 0xFF; | |
695 | eui[4] = 0xFE; | |
696 | eui[5] = addr[2]; | |
697 | eui[6] = addr[1]; | |
698 | eui[7] = addr[0]; | |
699 | ||
62bbd5b3 | 700 | /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */ |
6b8d4a6a | 701 | if (addr_type == BDADDR_LE_PUBLIC) |
62bbd5b3 | 702 | eui[0] &= ~0x02; |
18722c24 | 703 | else |
62bbd5b3 JR |
704 | eui[0] |= 0x02; |
705 | ||
706 | BT_DBG("type %d addr %*phC", addr_type, 8, eui); | |
18722c24 JR |
707 | } |
708 | ||
709 | static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr, | |
710 | u8 addr_type) | |
711 | { | |
712 | netdev->addr_assign_type = NET_ADDR_PERM; | |
713 | set_addr(netdev->dev_addr, addr->b, addr_type); | |
18722c24 JR |
714 | } |
715 | ||
716 | static void ifup(struct net_device *netdev) | |
717 | { | |
718 | int err; | |
719 | ||
720 | rtnl_lock(); | |
721 | err = dev_open(netdev); | |
722 | if (err < 0) | |
723 | BT_INFO("iface %s cannot be opened (%d)", netdev->name, err); | |
724 | rtnl_unlock(); | |
725 | } | |
726 | ||
7f118253 JR |
727 | static void ifdown(struct net_device *netdev) |
728 | { | |
729 | int err; | |
730 | ||
731 | rtnl_lock(); | |
732 | err = dev_close(netdev); | |
733 | if (err < 0) | |
734 | BT_INFO("iface %s cannot be closed (%d)", netdev->name, err); | |
735 | rtnl_unlock(); | |
736 | } | |
737 | ||
18722c24 JR |
738 | static void do_notify_peers(struct work_struct *work) |
739 | { | |
740 | struct lowpan_dev *dev = container_of(work, struct lowpan_dev, | |
741 | notify_peers.work); | |
742 | ||
743 | netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */ | |
744 | } | |
745 | ||
746 | static bool is_bt_6lowpan(struct hci_conn *hcon) | |
747 | { | |
748 | if (hcon->type != LE_LINK) | |
749 | return false; | |
750 | ||
7b2ed60e | 751 | if (!enable_6lowpan) |
6b8d4a6a JR |
752 | return false; |
753 | ||
754 | return true; | |
18722c24 JR |
755 | } |
756 | ||
6b8d4a6a JR |
757 | static struct l2cap_chan *chan_create(void) |
758 | { | |
759 | struct l2cap_chan *chan; | |
760 | ||
761 | chan = l2cap_chan_create(); | |
762 | if (!chan) | |
763 | return NULL; | |
764 | ||
765 | l2cap_chan_set_defaults(chan); | |
766 | ||
767 | chan->chan_type = L2CAP_CHAN_CONN_ORIENTED; | |
768 | chan->mode = L2CAP_MODE_LE_FLOWCTL; | |
301de2cb | 769 | chan->imtu = 1280; |
6b8d4a6a JR |
770 | |
771 | return chan; | |
772 | } | |
773 | ||
b2799cec JR |
774 | static void set_ip_addr_bits(u8 addr_type, u8 *addr) |
775 | { | |
776 | if (addr_type == BDADDR_LE_PUBLIC) | |
777 | *addr |= 0x02; | |
778 | else | |
779 | *addr &= ~0x02; | |
780 | } | |
781 | ||
6b8d4a6a JR |
782 | static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan, |
783 | struct lowpan_dev *dev) | |
18722c24 JR |
784 | { |
785 | struct lowpan_peer *peer; | |
18722c24 JR |
786 | |
787 | peer = kzalloc(sizeof(*peer), GFP_ATOMIC); | |
788 | if (!peer) | |
6b8d4a6a | 789 | return NULL; |
18722c24 | 790 | |
6b8d4a6a | 791 | peer->chan = chan; |
18722c24 JR |
792 | memset(&peer->peer_addr, 0, sizeof(struct in6_addr)); |
793 | ||
794 | /* RFC 2464 ch. 5 */ | |
795 | peer->peer_addr.s6_addr[0] = 0xFE; | |
796 | peer->peer_addr.s6_addr[1] = 0x80; | |
6b8d4a6a JR |
797 | set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b, |
798 | chan->dst_type); | |
18722c24 JR |
799 | |
800 | memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8, | |
801 | EUI64_ADDR_LEN); | |
18722c24 | 802 | |
b2799cec JR |
803 | /* IPv6 address needs to have the U/L bit set properly so toggle |
804 | * it back here. | |
805 | */ | |
806 | set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8); | |
807 | ||
90305829 | 808 | spin_lock(&devices_lock); |
18722c24 JR |
809 | INIT_LIST_HEAD(&peer->list); |
810 | peer_add(dev, peer); | |
90305829 | 811 | spin_unlock(&devices_lock); |
18722c24 JR |
812 | |
813 | /* Notifying peers about us needs to be done without locks held */ | |
814 | INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers); | |
815 | schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100)); | |
816 | ||
6b8d4a6a | 817 | return peer->chan; |
18722c24 JR |
818 | } |
819 | ||
6b8d4a6a | 820 | static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev) |
18722c24 | 821 | { |
18722c24 JR |
822 | struct net_device *netdev; |
823 | int err = 0; | |
18722c24 | 824 | |
b72f6f51 AA |
825 | netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_dev)), |
826 | IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN, | |
827 | netdev_setup); | |
18722c24 JR |
828 | if (!netdev) |
829 | return -ENOMEM; | |
830 | ||
6b8d4a6a | 831 | set_dev_addr(netdev, &chan->src, chan->src_type); |
18722c24 JR |
832 | |
833 | netdev->netdev_ops = &netdev_ops; | |
fc84242f | 834 | SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev); |
18722c24 JR |
835 | SET_NETDEV_DEVTYPE(netdev, &bt_type); |
836 | ||
a42bbba5 | 837 | *dev = lowpan_dev(netdev); |
5857d1db AA |
838 | (*dev)->netdev = netdev; |
839 | (*dev)->hdev = chan->conn->hcon->hdev; | |
840 | INIT_LIST_HEAD(&(*dev)->peers); | |
841 | ||
842 | spin_lock(&devices_lock); | |
843 | INIT_LIST_HEAD(&(*dev)->list); | |
844 | list_add_rcu(&(*dev)->list, &bt_6lowpan_devices); | |
845 | spin_unlock(&devices_lock); | |
846 | ||
b72f6f51 AA |
847 | lowpan_netdev_setup(netdev, LOWPAN_LLTYPE_BTLE); |
848 | ||
18722c24 JR |
849 | err = register_netdev(netdev); |
850 | if (err < 0) { | |
851 | BT_INFO("register_netdev failed %d", err); | |
5857d1db AA |
852 | spin_lock(&devices_lock); |
853 | list_del_rcu(&(*dev)->list); | |
854 | spin_unlock(&devices_lock); | |
18722c24 JR |
855 | free_netdev(netdev); |
856 | goto out; | |
857 | } | |
858 | ||
6b8d4a6a JR |
859 | BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d", |
860 | netdev->ifindex, &chan->dst, chan->dst_type, | |
861 | &chan->src, chan->src_type); | |
18722c24 JR |
862 | set_bit(__LINK_STATE_PRESENT, &netdev->state); |
863 | ||
6b8d4a6a | 864 | return 0; |
18722c24 JR |
865 | |
866 | out: | |
867 | return err; | |
868 | } | |
869 | ||
6b8d4a6a JR |
870 | static inline void chan_ready_cb(struct l2cap_chan *chan) |
871 | { | |
872 | struct lowpan_dev *dev; | |
873 | ||
874 | dev = lookup_dev(chan->conn); | |
875 | ||
876 | BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev); | |
877 | ||
878 | if (!dev) { | |
879 | if (setup_netdev(chan, &dev) < 0) { | |
880 | l2cap_chan_del(chan, -ENOENT); | |
881 | return; | |
882 | } | |
883 | } | |
884 | ||
18d93c17 JR |
885 | if (!try_module_get(THIS_MODULE)) |
886 | return; | |
887 | ||
6b8d4a6a JR |
888 | add_peer_chan(chan, dev); |
889 | ifup(dev->netdev); | |
890 | } | |
891 | ||
2b293490 | 892 | static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan) |
6b8d4a6a | 893 | { |
2b293490 | 894 | struct l2cap_chan *chan; |
6b8d4a6a | 895 | |
630ef791 JH |
896 | chan = chan_create(); |
897 | if (!chan) | |
898 | return NULL; | |
899 | ||
2b293490 | 900 | chan->ops = pchan->ops; |
6b8d4a6a JR |
901 | |
902 | BT_DBG("chan %p pchan %p", chan, pchan); | |
903 | ||
2b293490 | 904 | return chan; |
6b8d4a6a JR |
905 | } |
906 | ||
18722c24 JR |
907 | static void delete_netdev(struct work_struct *work) |
908 | { | |
909 | struct lowpan_dev *entry = container_of(work, struct lowpan_dev, | |
910 | delete_netdev); | |
911 | ||
912 | unregister_netdev(entry->netdev); | |
913 | ||
2ad88fb2 | 914 | /* The entry pointer is deleted by the netdev destructor. */ |
18722c24 JR |
915 | } |
916 | ||
6b8d4a6a | 917 | static void chan_close_cb(struct l2cap_chan *chan) |
18722c24 | 918 | { |
90305829 | 919 | struct lowpan_dev *entry; |
18722c24 JR |
920 | struct lowpan_dev *dev = NULL; |
921 | struct lowpan_peer *peer; | |
922 | int err = -ENOENT; | |
f63666d2 | 923 | bool last = false, remove = true; |
18722c24 | 924 | |
6b8d4a6a JR |
925 | BT_DBG("chan %p conn %p", chan, chan->conn); |
926 | ||
927 | if (chan->conn && chan->conn->hcon) { | |
928 | if (!is_bt_6lowpan(chan->conn->hcon)) | |
929 | return; | |
930 | ||
931 | /* If conn is set, then the netdev is also there and we should | |
932 | * not remove it. | |
933 | */ | |
f63666d2 | 934 | remove = false; |
6b8d4a6a | 935 | } |
18722c24 | 936 | |
90305829 | 937 | spin_lock(&devices_lock); |
18722c24 | 938 | |
90305829 | 939 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
18722c24 | 940 | dev = lowpan_dev(entry->netdev); |
90305829 | 941 | peer = __peer_lookup_chan(dev, chan); |
18722c24 JR |
942 | if (peer) { |
943 | last = peer_del(dev, peer); | |
944 | err = 0; | |
6b8d4a6a JR |
945 | |
946 | BT_DBG("dev %p removing %speer %p", dev, | |
947 | last ? "last " : "1 ", peer); | |
948 | BT_DBG("chan %p orig refcnt %d", chan, | |
949 | atomic_read(&chan->kref.refcount)); | |
950 | ||
951 | l2cap_chan_put(chan); | |
18722c24 JR |
952 | break; |
953 | } | |
954 | } | |
955 | ||
956 | if (!err && last && dev && !atomic_read(&dev->peer_count)) { | |
90305829 | 957 | spin_unlock(&devices_lock); |
18722c24 JR |
958 | |
959 | cancel_delayed_work_sync(&dev->notify_peers); | |
960 | ||
7f118253 JR |
961 | ifdown(dev->netdev); |
962 | ||
f63666d2 | 963 | if (remove) { |
6b8d4a6a JR |
964 | INIT_WORK(&entry->delete_netdev, delete_netdev); |
965 | schedule_work(&entry->delete_netdev); | |
966 | } | |
18722c24 | 967 | } else { |
90305829 | 968 | spin_unlock(&devices_lock); |
18722c24 JR |
969 | } |
970 | ||
6b8d4a6a JR |
971 | return; |
972 | } | |
973 | ||
974 | static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err) | |
975 | { | |
976 | BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn, | |
977 | state_to_string(state), err); | |
978 | } | |
979 | ||
980 | static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan, | |
981 | unsigned long hdr_len, | |
982 | unsigned long len, int nb) | |
983 | { | |
984 | /* Note that we must allocate using GFP_ATOMIC here as | |
985 | * this function is called originally from netdev hard xmit | |
986 | * function in atomic context. | |
987 | */ | |
988 | return bt_skb_alloc(hdr_len + len, GFP_ATOMIC); | |
989 | } | |
990 | ||
991 | static void chan_suspend_cb(struct l2cap_chan *chan) | |
992 | { | |
993 | struct sk_buff *skb = chan->data; | |
994 | ||
995 | BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb); | |
996 | ||
59790aa2 JR |
997 | if (!skb) |
998 | return; | |
999 | ||
6b8d4a6a JR |
1000 | lowpan_cb(skb)->status = -EAGAIN; |
1001 | } | |
1002 | ||
1003 | static void chan_resume_cb(struct l2cap_chan *chan) | |
1004 | { | |
1005 | struct sk_buff *skb = chan->data; | |
1006 | ||
1007 | BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb); | |
1008 | ||
59790aa2 JR |
1009 | if (!skb) |
1010 | return; | |
1011 | ||
6b8d4a6a JR |
1012 | lowpan_cb(skb)->status = 0; |
1013 | } | |
1014 | ||
1015 | static long chan_get_sndtimeo_cb(struct l2cap_chan *chan) | |
1016 | { | |
2ae50d8d | 1017 | return L2CAP_CONN_TIMEOUT; |
6b8d4a6a JR |
1018 | } |
1019 | ||
1020 | static const struct l2cap_ops bt_6lowpan_chan_ops = { | |
1021 | .name = "L2CAP 6LoWPAN channel", | |
1022 | .new_connection = chan_new_conn_cb, | |
1023 | .recv = chan_recv_cb, | |
1024 | .close = chan_close_cb, | |
1025 | .state_change = chan_state_change_cb, | |
1026 | .ready = chan_ready_cb, | |
1027 | .resume = chan_resume_cb, | |
1028 | .suspend = chan_suspend_cb, | |
1029 | .get_sndtimeo = chan_get_sndtimeo_cb, | |
1030 | .alloc_skb = chan_alloc_skb_cb, | |
6b8d4a6a JR |
1031 | |
1032 | .teardown = l2cap_chan_no_teardown, | |
1033 | .defer = l2cap_chan_no_defer, | |
1034 | .set_shutdown = l2cap_chan_no_set_shutdown, | |
1035 | }; | |
1036 | ||
1037 | static inline __u8 bdaddr_type(__u8 type) | |
1038 | { | |
1039 | if (type == ADDR_LE_DEV_PUBLIC) | |
1040 | return BDADDR_LE_PUBLIC; | |
1041 | else | |
1042 | return BDADDR_LE_RANDOM; | |
1043 | } | |
1044 | ||
6b8d4a6a JR |
1045 | static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type) |
1046 | { | |
0cd088fc | 1047 | struct l2cap_chan *chan; |
6b8d4a6a JR |
1048 | int err; |
1049 | ||
26d46dff | 1050 | chan = chan_create(); |
0cd088fc | 1051 | if (!chan) |
6b8d4a6a JR |
1052 | return -EINVAL; |
1053 | ||
26d46dff JH |
1054 | chan->ops = &bt_6lowpan_chan_ops; |
1055 | ||
0cd088fc | 1056 | err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0, |
6b8d4a6a JR |
1057 | addr, dst_type); |
1058 | ||
0cd088fc | 1059 | BT_DBG("chan %p err %d", chan, err); |
6b8d4a6a | 1060 | if (err < 0) |
0cd088fc | 1061 | l2cap_chan_put(chan); |
6b8d4a6a | 1062 | |
18722c24 JR |
1063 | return err; |
1064 | } | |
1065 | ||
6b8d4a6a JR |
1066 | static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type) |
1067 | { | |
1068 | struct lowpan_peer *peer; | |
1069 | ||
1070 | BT_DBG("conn %p dst type %d", conn, dst_type); | |
1071 | ||
1072 | peer = lookup_peer(conn); | |
1073 | if (!peer) | |
1074 | return -ENOENT; | |
1075 | ||
1076 | BT_DBG("peer %p chan %p", peer, peer->chan); | |
1077 | ||
1078 | l2cap_chan_close(peer->chan, ENOENT); | |
1079 | ||
1080 | return 0; | |
1081 | } | |
1082 | ||
1083 | static struct l2cap_chan *bt_6lowpan_listen(void) | |
1084 | { | |
1085 | bdaddr_t *addr = BDADDR_ANY; | |
0cd088fc | 1086 | struct l2cap_chan *chan; |
6b8d4a6a JR |
1087 | int err; |
1088 | ||
7b2ed60e | 1089 | if (!enable_6lowpan) |
6b8d4a6a JR |
1090 | return NULL; |
1091 | ||
26d46dff | 1092 | chan = chan_create(); |
0cd088fc | 1093 | if (!chan) |
6b8d4a6a JR |
1094 | return NULL; |
1095 | ||
26d46dff | 1096 | chan->ops = &bt_6lowpan_chan_ops; |
0cd088fc JH |
1097 | chan->state = BT_LISTEN; |
1098 | chan->src_type = BDADDR_LE_PUBLIC; | |
6b8d4a6a | 1099 | |
0cd088fc | 1100 | atomic_set(&chan->nesting, L2CAP_NESTING_PARENT); |
2773b024 | 1101 | |
0cd088fc | 1102 | BT_DBG("chan %p src type %d", chan, chan->src_type); |
6b8d4a6a | 1103 | |
0cd088fc | 1104 | err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP)); |
6b8d4a6a | 1105 | if (err) { |
0cd088fc | 1106 | l2cap_chan_put(chan); |
6b8d4a6a JR |
1107 | BT_ERR("psm cannot be added err %d", err); |
1108 | return NULL; | |
1109 | } | |
1110 | ||
0cd088fc | 1111 | return chan; |
6b8d4a6a JR |
1112 | } |
1113 | ||
1114 | static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type, | |
1115 | struct l2cap_conn **conn) | |
1116 | { | |
1117 | struct hci_conn *hcon; | |
1118 | struct hci_dev *hdev; | |
1119 | bdaddr_t *src = BDADDR_ANY; | |
1120 | int n; | |
1121 | ||
1122 | n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu", | |
1123 | &addr->b[5], &addr->b[4], &addr->b[3], | |
1124 | &addr->b[2], &addr->b[1], &addr->b[0], | |
1125 | addr_type); | |
1126 | ||
1127 | if (n < 7) | |
1128 | return -EINVAL; | |
1129 | ||
1130 | hdev = hci_get_route(addr, src); | |
1131 | if (!hdev) | |
1132 | return -ENOENT; | |
1133 | ||
1134 | hci_dev_lock(hdev); | |
1135 | hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); | |
1136 | hci_dev_unlock(hdev); | |
1137 | ||
1138 | if (!hcon) | |
1139 | return -ENOENT; | |
1140 | ||
1141 | *conn = (struct l2cap_conn *)hcon->l2cap_data; | |
1142 | ||
1143 | BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type); | |
1144 | ||
1145 | return 0; | |
1146 | } | |
1147 | ||
1148 | static void disconnect_all_peers(void) | |
1149 | { | |
90305829 | 1150 | struct lowpan_dev *entry; |
6b8d4a6a JR |
1151 | struct lowpan_peer *peer, *tmp_peer, *new_peer; |
1152 | struct list_head peers; | |
6b8d4a6a JR |
1153 | |
1154 | INIT_LIST_HEAD(&peers); | |
1155 | ||
1156 | /* We make a separate list of peers as the close_cb() will | |
1157 | * modify the device peers list so it is better not to mess | |
1158 | * with the same list at the same time. | |
1159 | */ | |
1160 | ||
90305829 | 1161 | rcu_read_lock(); |
6b8d4a6a | 1162 | |
90305829 JR |
1163 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
1164 | list_for_each_entry_rcu(peer, &entry->peers, list) { | |
6b8d4a6a JR |
1165 | new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC); |
1166 | if (!new_peer) | |
1167 | break; | |
1168 | ||
1169 | new_peer->chan = peer->chan; | |
1170 | INIT_LIST_HEAD(&new_peer->list); | |
1171 | ||
1172 | list_add(&new_peer->list, &peers); | |
1173 | } | |
1174 | } | |
1175 | ||
90305829 | 1176 | rcu_read_unlock(); |
6b8d4a6a | 1177 | |
90305829 | 1178 | spin_lock(&devices_lock); |
6b8d4a6a JR |
1179 | list_for_each_entry_safe(peer, tmp_peer, &peers, list) { |
1180 | l2cap_chan_close(peer->chan, ENOENT); | |
90305829 JR |
1181 | |
1182 | list_del_rcu(&peer->list); | |
4e790226 | 1183 | kfree_rcu(peer, rcu); |
6b8d4a6a | 1184 | } |
90305829 | 1185 | spin_unlock(&devices_lock); |
6b8d4a6a JR |
1186 | } |
1187 | ||
7b2ed60e | 1188 | struct set_enable { |
90305829 | 1189 | struct work_struct work; |
7b2ed60e | 1190 | bool flag; |
90305829 | 1191 | }; |
6b8d4a6a | 1192 | |
7b2ed60e | 1193 | static void do_enable_set(struct work_struct *work) |
90305829 | 1194 | { |
7b2ed60e JR |
1195 | struct set_enable *set_enable = container_of(work, |
1196 | struct set_enable, work); | |
90305829 | 1197 | |
7b2ed60e | 1198 | if (!set_enable->flag || enable_6lowpan != set_enable->flag) |
6b8d4a6a | 1199 | /* Disconnect existing connections if 6lowpan is |
7b2ed60e | 1200 | * disabled |
6b8d4a6a JR |
1201 | */ |
1202 | disconnect_all_peers(); | |
1203 | ||
7b2ed60e | 1204 | enable_6lowpan = set_enable->flag; |
6b8d4a6a JR |
1205 | |
1206 | if (listen_chan) { | |
1207 | l2cap_chan_close(listen_chan, 0); | |
1208 | l2cap_chan_put(listen_chan); | |
1209 | } | |
1210 | ||
1211 | listen_chan = bt_6lowpan_listen(); | |
1212 | ||
7b2ed60e | 1213 | kfree(set_enable); |
90305829 JR |
1214 | } |
1215 | ||
7b2ed60e | 1216 | static int lowpan_enable_set(void *data, u64 val) |
90305829 | 1217 | { |
7b2ed60e | 1218 | struct set_enable *set_enable; |
90305829 | 1219 | |
7b2ed60e JR |
1220 | set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL); |
1221 | if (!set_enable) | |
90305829 JR |
1222 | return -ENOMEM; |
1223 | ||
7b2ed60e JR |
1224 | set_enable->flag = !!val; |
1225 | INIT_WORK(&set_enable->work, do_enable_set); | |
90305829 | 1226 | |
7b2ed60e | 1227 | schedule_work(&set_enable->work); |
90305829 | 1228 | |
6b8d4a6a JR |
1229 | return 0; |
1230 | } | |
1231 | ||
7b2ed60e | 1232 | static int lowpan_enable_get(void *data, u64 *val) |
6b8d4a6a | 1233 | { |
7b2ed60e | 1234 | *val = enable_6lowpan; |
6b8d4a6a JR |
1235 | return 0; |
1236 | } | |
1237 | ||
7b2ed60e JR |
1238 | DEFINE_SIMPLE_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get, |
1239 | lowpan_enable_set, "%llu\n"); | |
6b8d4a6a JR |
1240 | |
1241 | static ssize_t lowpan_control_write(struct file *fp, | |
1242 | const char __user *user_buffer, | |
1243 | size_t count, | |
1244 | loff_t *position) | |
1245 | { | |
1246 | char buf[32]; | |
1247 | size_t buf_size = min(count, sizeof(buf) - 1); | |
1248 | int ret; | |
1249 | bdaddr_t addr; | |
1250 | u8 addr_type; | |
1251 | struct l2cap_conn *conn = NULL; | |
1252 | ||
1253 | if (copy_from_user(buf, user_buffer, buf_size)) | |
1254 | return -EFAULT; | |
1255 | ||
1256 | buf[buf_size] = '\0'; | |
1257 | ||
1258 | if (memcmp(buf, "connect ", 8) == 0) { | |
1259 | ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn); | |
1260 | if (ret == -EINVAL) | |
1261 | return ret; | |
1262 | ||
1263 | if (listen_chan) { | |
1264 | l2cap_chan_close(listen_chan, 0); | |
1265 | l2cap_chan_put(listen_chan); | |
1266 | listen_chan = NULL; | |
1267 | } | |
1268 | ||
1269 | if (conn) { | |
1270 | struct lowpan_peer *peer; | |
1271 | ||
1272 | if (!is_bt_6lowpan(conn->hcon)) | |
1273 | return -EINVAL; | |
1274 | ||
1275 | peer = lookup_peer(conn); | |
1276 | if (peer) { | |
1277 | BT_DBG("6LoWPAN connection already exists"); | |
1278 | return -EALREADY; | |
1279 | } | |
1280 | ||
1281 | BT_DBG("conn %p dst %pMR type %d user %d", conn, | |
1282 | &conn->hcon->dst, conn->hcon->dst_type, | |
1283 | addr_type); | |
1284 | } | |
1285 | ||
1286 | ret = bt_6lowpan_connect(&addr, addr_type); | |
1287 | if (ret < 0) | |
1288 | return ret; | |
1289 | ||
1290 | return count; | |
1291 | } | |
1292 | ||
1293 | if (memcmp(buf, "disconnect ", 11) == 0) { | |
1294 | ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn); | |
1295 | if (ret < 0) | |
1296 | return ret; | |
1297 | ||
1298 | ret = bt_6lowpan_disconnect(conn, addr_type); | |
1299 | if (ret < 0) | |
1300 | return ret; | |
1301 | ||
1302 | return count; | |
1303 | } | |
1304 | ||
1305 | return count; | |
1306 | } | |
1307 | ||
1308 | static int lowpan_control_show(struct seq_file *f, void *ptr) | |
1309 | { | |
90305829 JR |
1310 | struct lowpan_dev *entry; |
1311 | struct lowpan_peer *peer; | |
6b8d4a6a | 1312 | |
90305829 | 1313 | spin_lock(&devices_lock); |
6b8d4a6a | 1314 | |
90305829 JR |
1315 | list_for_each_entry(entry, &bt_6lowpan_devices, list) { |
1316 | list_for_each_entry(peer, &entry->peers, list) | |
6b8d4a6a JR |
1317 | seq_printf(f, "%pMR (type %u)\n", |
1318 | &peer->chan->dst, peer->chan->dst_type); | |
1319 | } | |
1320 | ||
90305829 | 1321 | spin_unlock(&devices_lock); |
6b8d4a6a JR |
1322 | |
1323 | return 0; | |
1324 | } | |
1325 | ||
1326 | static int lowpan_control_open(struct inode *inode, struct file *file) | |
1327 | { | |
1328 | return single_open(file, lowpan_control_show, inode->i_private); | |
1329 | } | |
1330 | ||
1331 | static const struct file_operations lowpan_control_fops = { | |
1332 | .open = lowpan_control_open, | |
1333 | .read = seq_read, | |
1334 | .write = lowpan_control_write, | |
1335 | .llseek = seq_lseek, | |
1336 | .release = single_release, | |
1337 | }; | |
1338 | ||
7f118253 JR |
1339 | static void disconnect_devices(void) |
1340 | { | |
daac197c | 1341 | struct lowpan_dev *entry, *tmp, *new_dev; |
7f118253 | 1342 | struct list_head devices; |
7f118253 JR |
1343 | |
1344 | INIT_LIST_HEAD(&devices); | |
1345 | ||
1346 | /* We make a separate list of devices because the unregister_netdev() | |
1347 | * will call device_event() which will also want to modify the same | |
1348 | * devices list. | |
1349 | */ | |
1350 | ||
90305829 | 1351 | rcu_read_lock(); |
7f118253 | 1352 | |
90305829 | 1353 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
7f118253 JR |
1354 | new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC); |
1355 | if (!new_dev) | |
1356 | break; | |
1357 | ||
1358 | new_dev->netdev = entry->netdev; | |
1359 | INIT_LIST_HEAD(&new_dev->list); | |
1360 | ||
90305829 | 1361 | list_add_rcu(&new_dev->list, &devices); |
7f118253 JR |
1362 | } |
1363 | ||
90305829 | 1364 | rcu_read_unlock(); |
7f118253 | 1365 | |
daac197c | 1366 | list_for_each_entry_safe(entry, tmp, &devices, list) { |
7f118253 JR |
1367 | ifdown(entry->netdev); |
1368 | BT_DBG("Unregistering netdev %s %p", | |
1369 | entry->netdev->name, entry->netdev); | |
1370 | unregister_netdev(entry->netdev); | |
1371 | kfree(entry); | |
1372 | } | |
1373 | } | |
1374 | ||
18722c24 JR |
1375 | static int device_event(struct notifier_block *unused, |
1376 | unsigned long event, void *ptr) | |
1377 | { | |
1378 | struct net_device *netdev = netdev_notifier_info_to_dev(ptr); | |
90305829 | 1379 | struct lowpan_dev *entry; |
18722c24 JR |
1380 | |
1381 | if (netdev->type != ARPHRD_6LOWPAN) | |
1382 | return NOTIFY_DONE; | |
1383 | ||
1384 | switch (event) { | |
1385 | case NETDEV_UNREGISTER: | |
90305829 JR |
1386 | spin_lock(&devices_lock); |
1387 | list_for_each_entry(entry, &bt_6lowpan_devices, list) { | |
18722c24 | 1388 | if (entry->netdev == netdev) { |
7f118253 JR |
1389 | BT_DBG("Unregistered netdev %s %p", |
1390 | netdev->name, netdev); | |
18722c24 | 1391 | list_del(&entry->list); |
18722c24 JR |
1392 | break; |
1393 | } | |
1394 | } | |
90305829 | 1395 | spin_unlock(&devices_lock); |
18722c24 JR |
1396 | break; |
1397 | } | |
1398 | ||
1399 | return NOTIFY_DONE; | |
1400 | } | |
1401 | ||
1402 | static struct notifier_block bt_6lowpan_dev_notifier = { | |
1403 | .notifier_call = device_event, | |
1404 | }; | |
1405 | ||
5547e48c | 1406 | static int __init bt_6lowpan_init(void) |
18722c24 | 1407 | { |
7b2ed60e JR |
1408 | lowpan_enable_debugfs = debugfs_create_file("6lowpan_enable", 0644, |
1409 | bt_debugfs, NULL, | |
1410 | &lowpan_enable_fops); | |
6b8d4a6a JR |
1411 | lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644, |
1412 | bt_debugfs, NULL, | |
1413 | &lowpan_control_fops); | |
1414 | ||
18722c24 JR |
1415 | return register_netdevice_notifier(&bt_6lowpan_dev_notifier); |
1416 | } | |
1417 | ||
5547e48c | 1418 | static void __exit bt_6lowpan_exit(void) |
18722c24 | 1419 | { |
7b2ed60e | 1420 | debugfs_remove(lowpan_enable_debugfs); |
6b8d4a6a JR |
1421 | debugfs_remove(lowpan_control_debugfs); |
1422 | ||
1423 | if (listen_chan) { | |
1424 | l2cap_chan_close(listen_chan, 0); | |
1425 | l2cap_chan_put(listen_chan); | |
1426 | } | |
1427 | ||
7f118253 JR |
1428 | disconnect_devices(); |
1429 | ||
18722c24 JR |
1430 | unregister_netdevice_notifier(&bt_6lowpan_dev_notifier); |
1431 | } | |
5547e48c JR |
1432 | |
1433 | module_init(bt_6lowpan_init); | |
1434 | module_exit(bt_6lowpan_exit); | |
1435 | ||
1436 | MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>"); | |
1437 | MODULE_DESCRIPTION("Bluetooth 6LoWPAN"); | |
1438 | MODULE_VERSION(VERSION); | |
1439 | MODULE_LICENSE("GPL"); |