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