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