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