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