]>
Commit | Line | Data |
---|---|---|
8e87d142 | 1 | /* |
1da177e4 | 2 | BlueZ - Bluetooth protocol stack for Linux |
2d0a0346 | 3 | Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved. |
1da177e4 LT |
4 | |
5 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License version 2 as | |
9 | published by the Free Software Foundation; | |
10 | ||
11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. | |
14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY | |
8e87d142 YH |
15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
1da177e4 LT |
18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
19 | ||
8e87d142 YH |
20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS | |
1da177e4 LT |
22 | SOFTWARE IS DISCLAIMED. |
23 | */ | |
24 | ||
25 | /* Bluetooth HCI connection handling. */ | |
26 | ||
1da177e4 LT |
27 | #include <linux/module.h> |
28 | ||
29 | #include <linux/types.h> | |
30 | #include <linux/errno.h> | |
31 | #include <linux/kernel.h> | |
1da177e4 LT |
32 | #include <linux/slab.h> |
33 | #include <linux/poll.h> | |
34 | #include <linux/fcntl.h> | |
35 | #include <linux/init.h> | |
36 | #include <linux/skbuff.h> | |
37 | #include <linux/interrupt.h> | |
38 | #include <linux/notifier.h> | |
39 | #include <net/sock.h> | |
40 | ||
41 | #include <asm/system.h> | |
70f23020 | 42 | #include <linux/uaccess.h> |
1da177e4 LT |
43 | #include <asm/unaligned.h> |
44 | ||
45 | #include <net/bluetooth/bluetooth.h> | |
46 | #include <net/bluetooth/hci_core.h> | |
47 | ||
fcd89c09 VT |
48 | static void hci_le_connect(struct hci_conn *conn) |
49 | { | |
50 | struct hci_dev *hdev = conn->hdev; | |
51 | struct hci_cp_le_create_conn cp; | |
52 | ||
53 | conn->state = BT_CONNECT; | |
54 | conn->out = 1; | |
b92a6223 | 55 | conn->link_mode |= HCI_LM_MASTER; |
fcd89c09 VT |
56 | |
57 | memset(&cp, 0, sizeof(cp)); | |
58 | cp.scan_interval = cpu_to_le16(0x0004); | |
59 | cp.scan_window = cpu_to_le16(0x0004); | |
60 | bacpy(&cp.peer_addr, &conn->dst); | |
6d3ce0e7 | 61 | cp.peer_addr_type = conn->dst_type; |
fcd89c09 VT |
62 | cp.conn_interval_min = cpu_to_le16(0x0008); |
63 | cp.conn_interval_max = cpu_to_le16(0x0100); | |
64 | cp.supervision_timeout = cpu_to_le16(0x0064); | |
65 | cp.min_ce_len = cpu_to_le16(0x0001); | |
66 | cp.max_ce_len = cpu_to_le16(0x0001); | |
67 | ||
68 | hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp); | |
69 | } | |
70 | ||
71 | static void hci_le_connect_cancel(struct hci_conn *conn) | |
72 | { | |
73 | hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL); | |
74 | } | |
75 | ||
4c67bc74 | 76 | void hci_acl_connect(struct hci_conn *conn) |
1da177e4 LT |
77 | { |
78 | struct hci_dev *hdev = conn->hdev; | |
79 | struct inquiry_entry *ie; | |
80 | struct hci_cp_create_conn cp; | |
81 | ||
82 | BT_DBG("%p", conn); | |
83 | ||
84 | conn->state = BT_CONNECT; | |
a8746417 MH |
85 | conn->out = 1; |
86 | ||
1da177e4 LT |
87 | conn->link_mode = HCI_LM_MASTER; |
88 | ||
4c67bc74 MH |
89 | conn->attempt++; |
90 | ||
e4e8e37c MH |
91 | conn->link_policy = hdev->link_policy; |
92 | ||
1da177e4 LT |
93 | memset(&cp, 0, sizeof(cp)); |
94 | bacpy(&cp.bdaddr, &conn->dst); | |
95 | cp.pscan_rep_mode = 0x02; | |
96 | ||
70f23020 AE |
97 | ie = hci_inquiry_cache_lookup(hdev, &conn->dst); |
98 | if (ie) { | |
41a96212 MH |
99 | if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) { |
100 | cp.pscan_rep_mode = ie->data.pscan_rep_mode; | |
101 | cp.pscan_mode = ie->data.pscan_mode; | |
102 | cp.clock_offset = ie->data.clock_offset | | |
103 | cpu_to_le16(0x8000); | |
104 | } | |
105 | ||
1da177e4 | 106 | memcpy(conn->dev_class, ie->data.dev_class, 3); |
41a96212 | 107 | conn->ssp_mode = ie->data.ssp_mode; |
1da177e4 LT |
108 | } |
109 | ||
a8746417 | 110 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
1da177e4 | 111 | if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER)) |
b6a0dc82 | 112 | cp.role_switch = 0x01; |
1da177e4 | 113 | else |
b6a0dc82 | 114 | cp.role_switch = 0x00; |
4c67bc74 | 115 | |
a9de9248 | 116 | hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); |
1da177e4 LT |
117 | } |
118 | ||
6ac59344 MH |
119 | static void hci_acl_connect_cancel(struct hci_conn *conn) |
120 | { | |
121 | struct hci_cp_create_conn_cancel cp; | |
122 | ||
123 | BT_DBG("%p", conn); | |
124 | ||
125 | if (conn->hdev->hci_ver < 2) | |
126 | return; | |
127 | ||
128 | bacpy(&cp.bdaddr, &conn->dst); | |
a9de9248 | 129 | hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp); |
6ac59344 MH |
130 | } |
131 | ||
1da177e4 LT |
132 | void hci_acl_disconn(struct hci_conn *conn, __u8 reason) |
133 | { | |
134 | struct hci_cp_disconnect cp; | |
135 | ||
136 | BT_DBG("%p", conn); | |
137 | ||
138 | conn->state = BT_DISCONN; | |
139 | ||
aca3192c | 140 | cp.handle = cpu_to_le16(conn->handle); |
1da177e4 | 141 | cp.reason = reason; |
a9de9248 | 142 | hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp); |
1da177e4 LT |
143 | } |
144 | ||
145 | void hci_add_sco(struct hci_conn *conn, __u16 handle) | |
146 | { | |
147 | struct hci_dev *hdev = conn->hdev; | |
148 | struct hci_cp_add_sco cp; | |
149 | ||
150 | BT_DBG("%p", conn); | |
151 | ||
152 | conn->state = BT_CONNECT; | |
153 | conn->out = 1; | |
154 | ||
efc7688b MH |
155 | conn->attempt++; |
156 | ||
aca3192c | 157 | cp.handle = cpu_to_le16(handle); |
a8746417 | 158 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
1da177e4 | 159 | |
a9de9248 | 160 | hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); |
1da177e4 LT |
161 | } |
162 | ||
b6a0dc82 MH |
163 | void hci_setup_sync(struct hci_conn *conn, __u16 handle) |
164 | { | |
165 | struct hci_dev *hdev = conn->hdev; | |
166 | struct hci_cp_setup_sync_conn cp; | |
167 | ||
168 | BT_DBG("%p", conn); | |
169 | ||
170 | conn->state = BT_CONNECT; | |
171 | conn->out = 1; | |
172 | ||
efc7688b MH |
173 | conn->attempt++; |
174 | ||
b6a0dc82 | 175 | cp.handle = cpu_to_le16(handle); |
a8746417 | 176 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
b6a0dc82 MH |
177 | |
178 | cp.tx_bandwidth = cpu_to_le32(0x00001f40); | |
179 | cp.rx_bandwidth = cpu_to_le32(0x00001f40); | |
180 | cp.max_latency = cpu_to_le16(0xffff); | |
181 | cp.voice_setting = cpu_to_le16(hdev->voice_setting); | |
182 | cp.retrans_effort = 0xff; | |
183 | ||
184 | hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp); | |
185 | } | |
186 | ||
2ce603eb CT |
187 | void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, |
188 | u16 latency, u16 to_multiplier) | |
189 | { | |
190 | struct hci_cp_le_conn_update cp; | |
191 | struct hci_dev *hdev = conn->hdev; | |
192 | ||
193 | memset(&cp, 0, sizeof(cp)); | |
194 | ||
195 | cp.handle = cpu_to_le16(conn->handle); | |
196 | cp.conn_interval_min = cpu_to_le16(min); | |
197 | cp.conn_interval_max = cpu_to_le16(max); | |
198 | cp.conn_latency = cpu_to_le16(latency); | |
199 | cp.supervision_timeout = cpu_to_le16(to_multiplier); | |
200 | cp.min_ce_len = cpu_to_le16(0x0001); | |
201 | cp.max_ce_len = cpu_to_le16(0x0001); | |
202 | ||
203 | hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); | |
204 | } | |
205 | EXPORT_SYMBOL(hci_le_conn_update); | |
206 | ||
e73439d8 MH |
207 | /* Device _must_ be locked */ |
208 | void hci_sco_setup(struct hci_conn *conn, __u8 status) | |
209 | { | |
210 | struct hci_conn *sco = conn->link; | |
211 | ||
212 | BT_DBG("%p", conn); | |
213 | ||
214 | if (!sco) | |
215 | return; | |
216 | ||
217 | if (!status) { | |
218 | if (lmp_esco_capable(conn->hdev)) | |
219 | hci_setup_sync(sco, conn->handle); | |
220 | else | |
221 | hci_add_sco(sco, conn->handle); | |
222 | } else { | |
223 | hci_proto_connect_cfm(sco, status); | |
224 | hci_conn_del(sco); | |
225 | } | |
226 | } | |
227 | ||
1da177e4 LT |
228 | static void hci_conn_timeout(unsigned long arg) |
229 | { | |
04837f64 MH |
230 | struct hci_conn *conn = (void *) arg; |
231 | struct hci_dev *hdev = conn->hdev; | |
2950f21a | 232 | __u8 reason; |
1da177e4 LT |
233 | |
234 | BT_DBG("conn %p state %d", conn, conn->state); | |
235 | ||
236 | if (atomic_read(&conn->refcnt)) | |
237 | return; | |
238 | ||
239 | hci_dev_lock(hdev); | |
6ac59344 MH |
240 | |
241 | switch (conn->state) { | |
242 | case BT_CONNECT: | |
769be974 | 243 | case BT_CONNECT2: |
fcd89c09 VT |
244 | if (conn->out) { |
245 | if (conn->type == ACL_LINK) | |
246 | hci_acl_connect_cancel(conn); | |
247 | else if (conn->type == LE_LINK) | |
248 | hci_le_connect_cancel(conn); | |
249 | } | |
6ac59344 | 250 | break; |
769be974 | 251 | case BT_CONFIG: |
8e87d142 | 252 | case BT_CONNECTED: |
2950f21a MH |
253 | reason = hci_proto_disconn_ind(conn); |
254 | hci_acl_disconn(conn, reason); | |
6ac59344 MH |
255 | break; |
256 | default: | |
1da177e4 | 257 | conn->state = BT_CLOSED; |
6ac59344 MH |
258 | break; |
259 | } | |
260 | ||
1da177e4 | 261 | hci_dev_unlock(hdev); |
1da177e4 LT |
262 | } |
263 | ||
04837f64 | 264 | static void hci_conn_idle(unsigned long arg) |
1da177e4 | 265 | { |
04837f64 MH |
266 | struct hci_conn *conn = (void *) arg; |
267 | ||
268 | BT_DBG("conn %p mode %d", conn, conn->mode); | |
269 | ||
270 | hci_conn_enter_sniff_mode(conn); | |
1da177e4 LT |
271 | } |
272 | ||
9f61656a JH |
273 | static void hci_conn_auto_accept(unsigned long arg) |
274 | { | |
275 | struct hci_conn *conn = (void *) arg; | |
276 | struct hci_dev *hdev = conn->hdev; | |
277 | ||
278 | hci_dev_lock(hdev); | |
279 | ||
280 | hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), | |
281 | &conn->dst); | |
282 | ||
283 | hci_dev_unlock(hdev); | |
284 | } | |
285 | ||
1da177e4 LT |
286 | struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst) |
287 | { | |
288 | struct hci_conn *conn; | |
289 | ||
290 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); | |
291 | ||
04837f64 MH |
292 | conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC); |
293 | if (!conn) | |
1da177e4 | 294 | return NULL; |
1da177e4 LT |
295 | |
296 | bacpy(&conn->dst, dst); | |
a8746417 MH |
297 | conn->hdev = hdev; |
298 | conn->type = type; | |
299 | conn->mode = HCI_CM_ACTIVE; | |
300 | conn->state = BT_OPEN; | |
93f19c9f | 301 | conn->auth_type = HCI_AT_GENERAL_BONDING; |
17fa4b9d | 302 | conn->io_capability = hdev->io_capability; |
a9583556 | 303 | conn->remote_auth = 0xff; |
13d39315 | 304 | conn->key_type = 0xff; |
1da177e4 | 305 | |
04837f64 | 306 | conn->power_save = 1; |
052b30b0 | 307 | conn->disc_timeout = HCI_DISCONN_TIMEOUT; |
04837f64 | 308 | |
a8746417 MH |
309 | switch (type) { |
310 | case ACL_LINK: | |
311 | conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; | |
312 | break; | |
313 | case SCO_LINK: | |
314 | if (lmp_esco_capable(hdev)) | |
efc7688b MH |
315 | conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | |
316 | (hdev->esco_type & EDR_ESCO_MASK); | |
a8746417 MH |
317 | else |
318 | conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; | |
319 | break; | |
320 | case ESCO_LINK: | |
efc7688b | 321 | conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; |
a8746417 MH |
322 | break; |
323 | } | |
324 | ||
1da177e4 | 325 | skb_queue_head_init(&conn->data_q); |
04837f64 | 326 | |
b24b8a24 PE |
327 | setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn); |
328 | setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn); | |
9f61656a JH |
329 | setup_timer(&conn->auto_accept_timer, hci_conn_auto_accept, |
330 | (unsigned long) conn); | |
1da177e4 LT |
331 | |
332 | atomic_set(&conn->refcnt, 0); | |
333 | ||
334 | hci_dev_hold(hdev); | |
335 | ||
336 | tasklet_disable(&hdev->tx_task); | |
337 | ||
338 | hci_conn_hash_add(hdev, conn); | |
339 | if (hdev->notify) | |
340 | hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); | |
341 | ||
9eba32b8 MH |
342 | atomic_set(&conn->devref, 0); |
343 | ||
a67e899c MH |
344 | hci_conn_init_sysfs(conn); |
345 | ||
1da177e4 LT |
346 | tasklet_enable(&hdev->tx_task); |
347 | ||
348 | return conn; | |
349 | } | |
350 | ||
351 | int hci_conn_del(struct hci_conn *conn) | |
352 | { | |
353 | struct hci_dev *hdev = conn->hdev; | |
354 | ||
355 | BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle); | |
356 | ||
04837f64 MH |
357 | del_timer(&conn->idle_timer); |
358 | ||
359 | del_timer(&conn->disc_timer); | |
1da177e4 | 360 | |
9f61656a JH |
361 | del_timer(&conn->auto_accept_timer); |
362 | ||
5b7f9909 | 363 | if (conn->type == ACL_LINK) { |
1da177e4 LT |
364 | struct hci_conn *sco = conn->link; |
365 | if (sco) | |
366 | sco->link = NULL; | |
367 | ||
368 | /* Unacked frames */ | |
369 | hdev->acl_cnt += conn->sent; | |
6ed58ec5 VT |
370 | } else if (conn->type == LE_LINK) { |
371 | if (hdev->le_pkts) | |
372 | hdev->le_cnt += conn->sent; | |
373 | else | |
374 | hdev->acl_cnt += conn->sent; | |
5b7f9909 MH |
375 | } else { |
376 | struct hci_conn *acl = conn->link; | |
377 | if (acl) { | |
378 | acl->link = NULL; | |
379 | hci_conn_put(acl); | |
380 | } | |
1da177e4 LT |
381 | } |
382 | ||
383 | tasklet_disable(&hdev->tx_task); | |
7d0db0a3 | 384 | |
1da177e4 LT |
385 | hci_conn_hash_del(hdev, conn); |
386 | if (hdev->notify) | |
387 | hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); | |
7d0db0a3 | 388 | |
1da177e4 | 389 | tasklet_enable(&hdev->tx_task); |
7d0db0a3 | 390 | |
1da177e4 | 391 | skb_queue_purge(&conn->data_q); |
1da177e4 | 392 | |
9eba32b8 | 393 | hci_conn_put_device(conn); |
2ae9a6be | 394 | |
384943ec MH |
395 | hci_dev_put(hdev); |
396 | ||
1da177e4 LT |
397 | return 0; |
398 | } | |
399 | ||
400 | struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src) | |
401 | { | |
402 | int use_src = bacmp(src, BDADDR_ANY); | |
403 | struct hci_dev *hdev = NULL; | |
404 | struct list_head *p; | |
405 | ||
406 | BT_DBG("%s -> %s", batostr(src), batostr(dst)); | |
407 | ||
408 | read_lock_bh(&hci_dev_list_lock); | |
409 | ||
410 | list_for_each(p, &hci_dev_list) { | |
411 | struct hci_dev *d = list_entry(p, struct hci_dev, list); | |
412 | ||
413 | if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags)) | |
414 | continue; | |
415 | ||
8e87d142 | 416 | /* Simple routing: |
1da177e4 LT |
417 | * No source address - find interface with bdaddr != dst |
418 | * Source address - find interface with bdaddr == src | |
419 | */ | |
420 | ||
421 | if (use_src) { | |
422 | if (!bacmp(&d->bdaddr, src)) { | |
423 | hdev = d; break; | |
424 | } | |
425 | } else { | |
426 | if (bacmp(&d->bdaddr, dst)) { | |
427 | hdev = d; break; | |
428 | } | |
429 | } | |
430 | } | |
431 | ||
432 | if (hdev) | |
433 | hdev = hci_dev_hold(hdev); | |
434 | ||
435 | read_unlock_bh(&hci_dev_list_lock); | |
436 | return hdev; | |
437 | } | |
438 | EXPORT_SYMBOL(hci_get_route); | |
439 | ||
fcd89c09 | 440 | /* Create SCO, ACL or LE connection. |
1da177e4 | 441 | * Device _must_ be locked */ |
8c1b2355 | 442 | struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, __u8 sec_level, __u8 auth_type) |
1da177e4 LT |
443 | { |
444 | struct hci_conn *acl; | |
5b7f9909 | 445 | struct hci_conn *sco; |
fcd89c09 | 446 | struct hci_conn *le; |
1da177e4 LT |
447 | |
448 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); | |
449 | ||
fcd89c09 | 450 | if (type == LE_LINK) { |
eda42b50 AG |
451 | struct adv_entry *entry; |
452 | ||
fcd89c09 | 453 | le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); |
15c4794f | 454 | if (le) |
30e76272 | 455 | return ERR_PTR(-EBUSY); |
eda42b50 AG |
456 | |
457 | entry = hci_find_adv_entry(hdev, dst); | |
458 | if (!entry) | |
459 | return ERR_PTR(-EHOSTUNREACH); | |
460 | ||
15c4794f | 461 | le = hci_conn_add(hdev, LE_LINK, dst); |
fcd89c09 | 462 | if (!le) |
30e76272 | 463 | return ERR_PTR(-ENOMEM); |
893d6751 | 464 | |
eda42b50 AG |
465 | le->dst_type = entry->bdaddr_type; |
466 | ||
893d6751 | 467 | hci_le_connect(le); |
fcd89c09 VT |
468 | |
469 | hci_conn_hold(le); | |
470 | ||
471 | return le; | |
472 | } | |
473 | ||
70f23020 AE |
474 | acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); |
475 | if (!acl) { | |
476 | acl = hci_conn_add(hdev, ACL_LINK, dst); | |
477 | if (!acl) | |
1da177e4 LT |
478 | return NULL; |
479 | } | |
480 | ||
481 | hci_conn_hold(acl); | |
482 | ||
09ab6f4c | 483 | if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { |
765c2a96 JH |
484 | acl->sec_level = BT_SECURITY_LOW; |
485 | acl->pending_sec_level = sec_level; | |
09ab6f4c | 486 | acl->auth_type = auth_type; |
1da177e4 | 487 | hci_acl_connect(acl); |
09ab6f4c | 488 | } |
1da177e4 | 489 | |
5b7f9909 MH |
490 | if (type == ACL_LINK) |
491 | return acl; | |
1da177e4 | 492 | |
70f23020 AE |
493 | sco = hci_conn_hash_lookup_ba(hdev, type, dst); |
494 | if (!sco) { | |
495 | sco = hci_conn_add(hdev, type, dst); | |
496 | if (!sco) { | |
5b7f9909 MH |
497 | hci_conn_put(acl); |
498 | return NULL; | |
1da177e4 | 499 | } |
5b7f9909 | 500 | } |
1da177e4 | 501 | |
5b7f9909 MH |
502 | acl->link = sco; |
503 | sco->link = acl; | |
1da177e4 | 504 | |
5b7f9909 | 505 | hci_conn_hold(sco); |
1da177e4 | 506 | |
5b7f9909 | 507 | if (acl->state == BT_CONNECTED && |
b6a0dc82 | 508 | (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { |
c390216b | 509 | acl->power_save = 1; |
14b12d0b | 510 | hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON); |
c390216b | 511 | |
e73439d8 MH |
512 | if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) { |
513 | /* defer SCO setup until mode change completed */ | |
514 | set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend); | |
515 | return sco; | |
516 | } | |
517 | ||
518 | hci_sco_setup(acl, 0x00); | |
b6a0dc82 | 519 | } |
5b7f9909 MH |
520 | |
521 | return sco; | |
1da177e4 LT |
522 | } |
523 | EXPORT_SYMBOL(hci_connect); | |
524 | ||
e7c29cb1 MH |
525 | /* Check link security requirement */ |
526 | int hci_conn_check_link_mode(struct hci_conn *conn) | |
527 | { | |
528 | BT_DBG("conn %p", conn); | |
529 | ||
530 | if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 && | |
531 | !(conn->link_mode & HCI_LM_ENCRYPT)) | |
532 | return 0; | |
533 | ||
534 | return 1; | |
535 | } | |
536 | EXPORT_SYMBOL(hci_conn_check_link_mode); | |
537 | ||
1da177e4 | 538 | /* Authenticate remote device */ |
0684e5f9 | 539 | static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
1da177e4 LT |
540 | { |
541 | BT_DBG("conn %p", conn); | |
542 | ||
765c2a96 JH |
543 | if (conn->pending_sec_level > sec_level) |
544 | sec_level = conn->pending_sec_level; | |
545 | ||
96a31833 | 546 | if (sec_level > conn->sec_level) |
765c2a96 | 547 | conn->pending_sec_level = sec_level; |
96a31833 | 548 | else if (conn->link_mode & HCI_LM_AUTH) |
1da177e4 LT |
549 | return 1; |
550 | ||
65cf686e JH |
551 | /* Make sure we preserve an existing MITM requirement*/ |
552 | auth_type |= (conn->auth_type & 0x01); | |
553 | ||
96a31833 MH |
554 | conn->auth_type = auth_type; |
555 | ||
1da177e4 LT |
556 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { |
557 | struct hci_cp_auth_requested cp; | |
aca3192c | 558 | cp.handle = cpu_to_le16(conn->handle); |
40be492f MH |
559 | hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, |
560 | sizeof(cp), &cp); | |
19f8def0 WR |
561 | if (conn->key_type != 0xff) |
562 | set_bit(HCI_CONN_REAUTH_PEND, &conn->pend); | |
1da177e4 | 563 | } |
8c1b2355 | 564 | |
1da177e4 LT |
565 | return 0; |
566 | } | |
1da177e4 | 567 | |
13d39315 WR |
568 | /* Encrypt the the link */ |
569 | static void hci_conn_encrypt(struct hci_conn *conn) | |
570 | { | |
571 | BT_DBG("conn %p", conn); | |
572 | ||
573 | if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) { | |
574 | struct hci_cp_set_conn_encrypt cp; | |
575 | cp.handle = cpu_to_le16(conn->handle); | |
576 | cp.encrypt = 0x01; | |
577 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), | |
578 | &cp); | |
579 | } | |
580 | } | |
581 | ||
8c1b2355 | 582 | /* Enable security */ |
0684e5f9 | 583 | int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
1da177e4 LT |
584 | { |
585 | BT_DBG("conn %p", conn); | |
586 | ||
13d39315 | 587 | /* For sdp we don't need the link key. */ |
8c1b2355 MH |
588 | if (sec_level == BT_SECURITY_SDP) |
589 | return 1; | |
590 | ||
13d39315 WR |
591 | /* For non 2.1 devices and low security level we don't need the link |
592 | key. */ | |
3fdca1e1 MH |
593 | if (sec_level == BT_SECURITY_LOW && |
594 | (!conn->ssp_mode || !conn->hdev->ssp_mode)) | |
595 | return 1; | |
8c1b2355 | 596 | |
13d39315 WR |
597 | /* For other security levels we need the link key. */ |
598 | if (!(conn->link_mode & HCI_LM_AUTH)) | |
599 | goto auth; | |
600 | ||
601 | /* An authenticated combination key has sufficient security for any | |
602 | security level. */ | |
603 | if (conn->key_type == HCI_LK_AUTH_COMBINATION) | |
604 | goto encrypt; | |
605 | ||
606 | /* An unauthenticated combination key has sufficient security for | |
607 | security level 1 and 2. */ | |
608 | if (conn->key_type == HCI_LK_UNAUTH_COMBINATION && | |
609 | (sec_level == BT_SECURITY_MEDIUM || | |
610 | sec_level == BT_SECURITY_LOW)) | |
611 | goto encrypt; | |
612 | ||
613 | /* A combination key has always sufficient security for the security | |
614 | levels 1 or 2. High security level requires the combination key | |
615 | is generated using maximum PIN code length (16). | |
616 | For pre 2.1 units. */ | |
617 | if (conn->key_type == HCI_LK_COMBINATION && | |
618 | (sec_level != BT_SECURITY_HIGH || | |
619 | conn->pin_length == 16)) | |
620 | goto encrypt; | |
621 | ||
622 | auth: | |
1da177e4 LT |
623 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) |
624 | return 0; | |
625 | ||
13d39315 WR |
626 | hci_conn_auth(conn, sec_level, auth_type); |
627 | return 0; | |
628 | ||
629 | encrypt: | |
630 | if (conn->link_mode & HCI_LM_ENCRYPT) | |
631 | return 1; | |
8c1b2355 | 632 | |
13d39315 | 633 | hci_conn_encrypt(conn); |
1da177e4 LT |
634 | return 0; |
635 | } | |
8c1b2355 | 636 | EXPORT_SYMBOL(hci_conn_security); |
1da177e4 | 637 | |
b3b1b061 WR |
638 | /* Check secure link requirement */ |
639 | int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level) | |
640 | { | |
641 | BT_DBG("conn %p", conn); | |
642 | ||
643 | if (sec_level != BT_SECURITY_HIGH) | |
644 | return 1; /* Accept if non-secure is required */ | |
645 | ||
ef4177e2 | 646 | if (conn->sec_level == BT_SECURITY_HIGH) |
b3b1b061 WR |
647 | return 1; |
648 | ||
649 | return 0; /* Reject not secure link */ | |
650 | } | |
651 | EXPORT_SYMBOL(hci_conn_check_secure); | |
652 | ||
1da177e4 LT |
653 | /* Change link key */ |
654 | int hci_conn_change_link_key(struct hci_conn *conn) | |
655 | { | |
656 | BT_DBG("conn %p", conn); | |
657 | ||
658 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { | |
659 | struct hci_cp_change_conn_link_key cp; | |
aca3192c | 660 | cp.handle = cpu_to_le16(conn->handle); |
40be492f MH |
661 | hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY, |
662 | sizeof(cp), &cp); | |
1da177e4 | 663 | } |
8c1b2355 | 664 | |
1da177e4 LT |
665 | return 0; |
666 | } | |
667 | EXPORT_SYMBOL(hci_conn_change_link_key); | |
668 | ||
669 | /* Switch role */ | |
8c1b2355 | 670 | int hci_conn_switch_role(struct hci_conn *conn, __u8 role) |
1da177e4 LT |
671 | { |
672 | BT_DBG("conn %p", conn); | |
673 | ||
674 | if (!role && conn->link_mode & HCI_LM_MASTER) | |
675 | return 1; | |
676 | ||
677 | if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) { | |
678 | struct hci_cp_switch_role cp; | |
679 | bacpy(&cp.bdaddr, &conn->dst); | |
680 | cp.role = role; | |
a9de9248 | 681 | hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); |
1da177e4 | 682 | } |
8c1b2355 | 683 | |
1da177e4 LT |
684 | return 0; |
685 | } | |
686 | EXPORT_SYMBOL(hci_conn_switch_role); | |
687 | ||
04837f64 | 688 | /* Enter active mode */ |
14b12d0b | 689 | void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active) |
04837f64 MH |
690 | { |
691 | struct hci_dev *hdev = conn->hdev; | |
692 | ||
693 | BT_DBG("conn %p mode %d", conn, conn->mode); | |
694 | ||
695 | if (test_bit(HCI_RAW, &hdev->flags)) | |
696 | return; | |
697 | ||
14b12d0b JG |
698 | if (conn->mode != HCI_CM_SNIFF) |
699 | goto timer; | |
700 | ||
701 | if (!conn->power_save && !force_active) | |
04837f64 MH |
702 | goto timer; |
703 | ||
704 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { | |
705 | struct hci_cp_exit_sniff_mode cp; | |
aca3192c | 706 | cp.handle = cpu_to_le16(conn->handle); |
a9de9248 | 707 | hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); |
04837f64 MH |
708 | } |
709 | ||
710 | timer: | |
711 | if (hdev->idle_timeout > 0) | |
712 | mod_timer(&conn->idle_timer, | |
713 | jiffies + msecs_to_jiffies(hdev->idle_timeout)); | |
714 | } | |
715 | ||
716 | /* Enter sniff mode */ | |
717 | void hci_conn_enter_sniff_mode(struct hci_conn *conn) | |
718 | { | |
719 | struct hci_dev *hdev = conn->hdev; | |
720 | ||
721 | BT_DBG("conn %p mode %d", conn, conn->mode); | |
722 | ||
723 | if (test_bit(HCI_RAW, &hdev->flags)) | |
724 | return; | |
725 | ||
726 | if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) | |
727 | return; | |
728 | ||
729 | if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) | |
730 | return; | |
731 | ||
732 | if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { | |
733 | struct hci_cp_sniff_subrate cp; | |
aca3192c YH |
734 | cp.handle = cpu_to_le16(conn->handle); |
735 | cp.max_latency = cpu_to_le16(0); | |
736 | cp.min_remote_timeout = cpu_to_le16(0); | |
737 | cp.min_local_timeout = cpu_to_le16(0); | |
a9de9248 | 738 | hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); |
04837f64 MH |
739 | } |
740 | ||
741 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { | |
742 | struct hci_cp_sniff_mode cp; | |
aca3192c YH |
743 | cp.handle = cpu_to_le16(conn->handle); |
744 | cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); | |
745 | cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); | |
746 | cp.attempt = cpu_to_le16(4); | |
747 | cp.timeout = cpu_to_le16(1); | |
a9de9248 | 748 | hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); |
04837f64 MH |
749 | } |
750 | } | |
751 | ||
1da177e4 LT |
752 | /* Drop all connection on the device */ |
753 | void hci_conn_hash_flush(struct hci_dev *hdev) | |
754 | { | |
755 | struct hci_conn_hash *h = &hdev->conn_hash; | |
756 | struct list_head *p; | |
757 | ||
758 | BT_DBG("hdev %s", hdev->name); | |
759 | ||
760 | p = h->list.next; | |
761 | while (p != &h->list) { | |
762 | struct hci_conn *c; | |
763 | ||
764 | c = list_entry(p, struct hci_conn, list); | |
765 | p = p->next; | |
766 | ||
767 | c->state = BT_CLOSED; | |
768 | ||
2950f21a | 769 | hci_proto_disconn_cfm(c, 0x16); |
1da177e4 LT |
770 | hci_conn_del(c); |
771 | } | |
772 | } | |
773 | ||
a9de9248 MH |
774 | /* Check pending connect attempts */ |
775 | void hci_conn_check_pending(struct hci_dev *hdev) | |
776 | { | |
777 | struct hci_conn *conn; | |
778 | ||
779 | BT_DBG("hdev %s", hdev->name); | |
780 | ||
781 | hci_dev_lock(hdev); | |
782 | ||
783 | conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); | |
784 | if (conn) | |
785 | hci_acl_connect(conn); | |
786 | ||
787 | hci_dev_unlock(hdev); | |
788 | } | |
789 | ||
9eba32b8 MH |
790 | void hci_conn_hold_device(struct hci_conn *conn) |
791 | { | |
792 | atomic_inc(&conn->devref); | |
793 | } | |
794 | EXPORT_SYMBOL(hci_conn_hold_device); | |
795 | ||
796 | void hci_conn_put_device(struct hci_conn *conn) | |
797 | { | |
798 | if (atomic_dec_and_test(&conn->devref)) | |
799 | hci_conn_del_sysfs(conn); | |
800 | } | |
801 | EXPORT_SYMBOL(hci_conn_put_device); | |
802 | ||
1da177e4 LT |
803 | int hci_get_conn_list(void __user *arg) |
804 | { | |
805 | struct hci_conn_list_req req, *cl; | |
806 | struct hci_conn_info *ci; | |
807 | struct hci_dev *hdev; | |
808 | struct list_head *p; | |
809 | int n = 0, size, err; | |
810 | ||
811 | if (copy_from_user(&req, arg, sizeof(req))) | |
812 | return -EFAULT; | |
813 | ||
814 | if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) | |
815 | return -EINVAL; | |
816 | ||
817 | size = sizeof(req) + req.conn_num * sizeof(*ci); | |
818 | ||
70f23020 AE |
819 | cl = kmalloc(size, GFP_KERNEL); |
820 | if (!cl) | |
1da177e4 LT |
821 | return -ENOMEM; |
822 | ||
70f23020 AE |
823 | hdev = hci_dev_get(req.dev_id); |
824 | if (!hdev) { | |
1da177e4 LT |
825 | kfree(cl); |
826 | return -ENODEV; | |
827 | } | |
828 | ||
829 | ci = cl->conn_info; | |
830 | ||
831 | hci_dev_lock_bh(hdev); | |
832 | list_for_each(p, &hdev->conn_hash.list) { | |
833 | register struct hci_conn *c; | |
834 | c = list_entry(p, struct hci_conn, list); | |
835 | ||
836 | bacpy(&(ci + n)->bdaddr, &c->dst); | |
837 | (ci + n)->handle = c->handle; | |
838 | (ci + n)->type = c->type; | |
839 | (ci + n)->out = c->out; | |
840 | (ci + n)->state = c->state; | |
841 | (ci + n)->link_mode = c->link_mode; | |
842 | if (++n >= req.conn_num) | |
843 | break; | |
844 | } | |
845 | hci_dev_unlock_bh(hdev); | |
846 | ||
847 | cl->dev_id = hdev->id; | |
848 | cl->conn_num = n; | |
849 | size = sizeof(req) + n * sizeof(*ci); | |
850 | ||
851 | hci_dev_put(hdev); | |
852 | ||
853 | err = copy_to_user(arg, cl, size); | |
854 | kfree(cl); | |
855 | ||
856 | return err ? -EFAULT : 0; | |
857 | } | |
858 | ||
859 | int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) | |
860 | { | |
861 | struct hci_conn_info_req req; | |
862 | struct hci_conn_info ci; | |
863 | struct hci_conn *conn; | |
864 | char __user *ptr = arg + sizeof(req); | |
865 | ||
866 | if (copy_from_user(&req, arg, sizeof(req))) | |
867 | return -EFAULT; | |
868 | ||
869 | hci_dev_lock_bh(hdev); | |
870 | conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); | |
871 | if (conn) { | |
872 | bacpy(&ci.bdaddr, &conn->dst); | |
873 | ci.handle = conn->handle; | |
874 | ci.type = conn->type; | |
875 | ci.out = conn->out; | |
876 | ci.state = conn->state; | |
877 | ci.link_mode = conn->link_mode; | |
878 | } | |
879 | hci_dev_unlock_bh(hdev); | |
880 | ||
881 | if (!conn) | |
882 | return -ENOENT; | |
883 | ||
884 | return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; | |
885 | } | |
40be492f MH |
886 | |
887 | int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) | |
888 | { | |
889 | struct hci_auth_info_req req; | |
890 | struct hci_conn *conn; | |
891 | ||
892 | if (copy_from_user(&req, arg, sizeof(req))) | |
893 | return -EFAULT; | |
894 | ||
895 | hci_dev_lock_bh(hdev); | |
896 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); | |
897 | if (conn) | |
898 | req.type = conn->auth_type; | |
899 | hci_dev_unlock_bh(hdev); | |
900 | ||
901 | if (!conn) | |
902 | return -ENOENT; | |
903 | ||
904 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; | |
905 | } |