Bluetooth: Introduce hci_req helper to abort a connection
[linux-2.6-block.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
8c520a59 27#include <linux/export.h>
23b9ceb7 28#include <linux/debugfs.h>
1da177e4
LT
29
30#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h>
4bc58f51 32#include <net/bluetooth/l2cap.h>
1da177e4 33
0857dd3b 34#include "hci_request.h"
ac4b7236 35#include "smp.h"
7024728e
MH
36#include "a2mp.h"
37
2dea632f
FD
38struct sco_param {
39 u16 pkt_type;
40 u16 max_latency;
c7da5797 41 u8 retrans_effort;
2dea632f
FD
42};
43
48e68ff5 44static const struct sco_param esco_param_cvsd[] = {
c7da5797
JH
45 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */
46 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */
47 { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */
48 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */
49 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */
2dea632f
FD
50};
51
48e68ff5 52static const struct sco_param sco_param_cvsd[] = {
c7da5797
JH
53 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */
54 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */
48e68ff5
BT
55};
56
565766b0 57static const struct sco_param esco_param_msbc[] = {
c7da5797
JH
58 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */
59 { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */
2dea632f
FD
60};
61
1aef8669 62static void hci_le_create_connection_cancel(struct hci_conn *conn)
fcd89c09
VT
63{
64 hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL);
65}
66
f75113a2
JP
67/* This function requires the caller holds hdev->lock */
68static void hci_connect_le_scan_cleanup(struct hci_conn *conn)
69{
70 struct hci_conn_params *params;
b5c2b621 71 struct hci_dev *hdev = conn->hdev;
f75113a2
JP
72 struct smp_irk *irk;
73 bdaddr_t *bdaddr;
74 u8 bdaddr_type;
75
76 bdaddr = &conn->dst;
77 bdaddr_type = conn->dst_type;
78
79 /* Check if we need to convert to identity address */
b5c2b621 80 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
f75113a2
JP
81 if (irk) {
82 bdaddr = &irk->bdaddr;
83 bdaddr_type = irk->addr_type;
84 }
85
17bc08f0
JH
86 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr,
87 bdaddr_type);
88 if (!params || !params->explicit_connect)
f75113a2
JP
89 return;
90
91 /* The connection attempt was doing scan for new RPA, and is
92 * in scan phase. If params are not associated with any other
93 * autoconnect action, remove them completely. If they are, just unmark
94 * them as waiting for connection, by clearing explicit_connect field.
95 */
9ad3e6ff
JH
96 params->explicit_connect = false;
97
98 list_del_init(&params->action);
99
100 switch (params->auto_connect) {
101 case HCI_AUTO_CONN_EXPLICIT:
b5c2b621 102 hci_conn_params_del(hdev, bdaddr, bdaddr_type);
9ad3e6ff
JH
103 /* return instead of break to avoid duplicate scan update */
104 return;
105 case HCI_AUTO_CONN_DIRECT:
106 case HCI_AUTO_CONN_ALWAYS:
b5c2b621 107 list_add(&params->action, &hdev->pend_le_conns);
9ad3e6ff
JH
108 break;
109 case HCI_AUTO_CONN_REPORT:
b5c2b621 110 list_add(&params->action, &hdev->pend_le_reports);
9ad3e6ff
JH
111 break;
112 default:
113 break;
168b8a25 114 }
9ad3e6ff 115
b5c2b621 116 hci_update_background_scan(hdev);
f75113a2
JP
117}
118
b958f9a3
JH
119static void hci_conn_cleanup(struct hci_conn *conn)
120{
121 struct hci_dev *hdev = conn->hdev;
122
123 if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags))
124 hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type);
125
126 hci_chan_list_flush(conn);
127
128 hci_conn_hash_del(hdev, conn);
129
130 if (hdev->notify)
131 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
132
133 hci_conn_del_sysfs(conn);
134
135 debugfs_remove_recursive(conn->debugfs);
136
137 hci_dev_put(hdev);
138
139 hci_conn_put(conn);
140}
141
8ce783dc 142static void le_scan_cleanup(struct work_struct *work)
f75113a2 143{
8ce783dc
JH
144 struct hci_conn *conn = container_of(work, struct hci_conn,
145 le_scan_cleanup);
146 struct hci_dev *hdev = conn->hdev;
147 struct hci_conn *c = NULL;
148
149 BT_DBG("%s hcon %p", hdev->name, conn);
150
151 hci_dev_lock(hdev);
152
153 /* Check that the hci_conn is still around */
154 rcu_read_lock();
155 list_for_each_entry_rcu(c, &hdev->conn_hash.list, list) {
156 if (c == conn)
157 break;
158 }
159 rcu_read_unlock();
160
161 if (c == conn) {
162 hci_connect_le_scan_cleanup(conn);
163 hci_conn_cleanup(conn);
164 }
165
166 hci_dev_unlock(hdev);
167 hci_dev_put(hdev);
168 hci_conn_put(conn);
169}
f75113a2 170
8ce783dc
JH
171static void hci_connect_le_scan_remove(struct hci_conn *conn)
172{
173 BT_DBG("%s hcon %p", conn->hdev->name, conn);
174
175 /* We can't call hci_conn_del/hci_conn_cleanup here since that
176 * could deadlock with another hci_conn_del() call that's holding
177 * hci_dev_lock and doing cancel_delayed_work_sync(&conn->disc_work).
178 * Instead, grab temporary extra references to the hci_dev and
179 * hci_conn and perform the necessary cleanup in a separate work
180 * callback.
b958f9a3 181 */
8ce783dc
JH
182
183 hci_dev_hold(conn->hdev);
184 hci_conn_get(conn);
185
186 schedule_work(&conn->le_scan_cleanup);
f75113a2
JP
187}
188
1aef8669 189static void hci_acl_create_connection(struct hci_conn *conn)
1da177e4
LT
190{
191 struct hci_dev *hdev = conn->hdev;
192 struct inquiry_entry *ie;
193 struct hci_cp_create_conn cp;
194
42d2d87c 195 BT_DBG("hcon %p", conn);
1da177e4
LT
196
197 conn->state = BT_CONNECT;
a0c808b3 198 conn->out = true;
40bef302 199 conn->role = HCI_ROLE_MASTER;
1da177e4 200
4c67bc74
MH
201 conn->attempt++;
202
e4e8e37c
MH
203 conn->link_policy = hdev->link_policy;
204
1da177e4
LT
205 memset(&cp, 0, sizeof(cp));
206 bacpy(&cp.bdaddr, &conn->dst);
207 cp.pscan_rep_mode = 0x02;
208
70f23020
AE
209 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
210 if (ie) {
41a96212
MH
211 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
212 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
213 cp.pscan_mode = ie->data.pscan_mode;
214 cp.clock_offset = ie->data.clock_offset |
dcf4adbf 215 cpu_to_le16(0x8000);
41a96212
MH
216 }
217
1da177e4 218 memcpy(conn->dev_class, ie->data.dev_class, 3);
58a681ef
JH
219 if (ie->data.ssp_mode > 0)
220 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
1da177e4
LT
221 }
222
a8746417 223 cp.pkt_type = cpu_to_le16(conn->pkt_type);
1da177e4 224 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
b6a0dc82 225 cp.role_switch = 0x01;
1da177e4 226 else
b6a0dc82 227 cp.role_switch = 0x00;
4c67bc74 228
a9de9248 229 hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
1da177e4
LT
230}
231
1aef8669 232static void hci_acl_create_connection_cancel(struct hci_conn *conn)
6ac59344
MH
233{
234 struct hci_cp_create_conn_cancel cp;
235
38b3fef1 236 BT_DBG("hcon %p", conn);
6ac59344 237
d095c1eb 238 if (conn->hdev->hci_ver < BLUETOOTH_VER_1_2)
6ac59344
MH
239 return;
240
241 bacpy(&cp.bdaddr, &conn->dst);
a9de9248 242 hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp);
6ac59344
MH
243}
244
93796fa6
CT
245static void hci_reject_sco(struct hci_conn *conn)
246{
247 struct hci_cp_reject_sync_conn_req cp;
248
d41c15cf 249 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
93796fa6
CT
250 bacpy(&cp.bdaddr, &conn->dst);
251
252 hci_send_cmd(conn->hdev, HCI_OP_REJECT_SYNC_CONN_REQ, sizeof(cp), &cp);
253}
254
e3b679d5 255int hci_disconnect(struct hci_conn *conn, __u8 reason)
1da177e4
LT
256{
257 struct hci_cp_disconnect cp;
258
38b3fef1 259 BT_DBG("hcon %p", conn);
1da177e4 260
839035a7
JH
261 /* When we are master of an established connection and it enters
262 * the disconnect timeout, then go ahead and try to read the
263 * current clock offset. Processing of the result is done
264 * within the event handling and hci_clock_offset_evt function.
265 */
266 if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER) {
267 struct hci_dev *hdev = conn->hdev;
4f639ede 268 struct hci_cp_read_clock_offset clkoff_cp;
839035a7 269
4f639ede
FF
270 clkoff_cp.handle = cpu_to_le16(conn->handle);
271 hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp),
272 &clkoff_cp);
839035a7
JH
273 }
274
1da177e4
LT
275 conn->state = BT_DISCONN;
276
aca3192c 277 cp.handle = cpu_to_le16(conn->handle);
1da177e4 278 cp.reason = reason;
e3b679d5 279 return hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp);
1da177e4
LT
280}
281
a2b1976b 282static void hci_amp_disconn(struct hci_conn *conn)
53502d69
AE
283{
284 struct hci_cp_disconn_phy_link cp;
285
286 BT_DBG("hcon %p", conn);
287
288 conn->state = BT_DISCONN;
289
290 cp.phy_handle = HCI_PHY_HANDLE(conn->handle);
a2b1976b 291 cp.reason = hci_proto_disconn_ind(conn);
53502d69
AE
292 hci_send_cmd(conn->hdev, HCI_OP_DISCONN_PHY_LINK,
293 sizeof(cp), &cp);
294}
295
57f5d0d1 296static void hci_add_sco(struct hci_conn *conn, __u16 handle)
1da177e4
LT
297{
298 struct hci_dev *hdev = conn->hdev;
299 struct hci_cp_add_sco cp;
300
38b3fef1 301 BT_DBG("hcon %p", conn);
1da177e4
LT
302
303 conn->state = BT_CONNECT;
a0c808b3 304 conn->out = true;
1da177e4 305
efc7688b
MH
306 conn->attempt++;
307
aca3192c 308 cp.handle = cpu_to_le16(handle);
a8746417 309 cp.pkt_type = cpu_to_le16(conn->pkt_type);
1da177e4 310
a9de9248 311 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
1da177e4
LT
312}
313
2dea632f 314bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
b6a0dc82
MH
315{
316 struct hci_dev *hdev = conn->hdev;
317 struct hci_cp_setup_sync_conn cp;
2dea632f 318 const struct sco_param *param;
b6a0dc82 319
38b3fef1 320 BT_DBG("hcon %p", conn);
b6a0dc82
MH
321
322 conn->state = BT_CONNECT;
a0c808b3 323 conn->out = true;
b6a0dc82 324
efc7688b
MH
325 conn->attempt++;
326
b6a0dc82 327 cp.handle = cpu_to_le16(handle);
b6a0dc82 328
dcf4adbf
JP
329 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
330 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
10c62ddc
FD
331 cp.voice_setting = cpu_to_le16(conn->setting);
332
333 switch (conn->setting & SCO_AIRMODE_MASK) {
334 case SCO_AIRMODE_TRANSP:
565766b0 335 if (conn->attempt > ARRAY_SIZE(esco_param_msbc))
2dea632f 336 return false;
565766b0 337 param = &esco_param_msbc[conn->attempt - 1];
10c62ddc
FD
338 break;
339 case SCO_AIRMODE_CVSD:
48e68ff5
BT
340 if (lmp_esco_capable(conn->link)) {
341 if (conn->attempt > ARRAY_SIZE(esco_param_cvsd))
342 return false;
48e68ff5
BT
343 param = &esco_param_cvsd[conn->attempt - 1];
344 } else {
345 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
346 return false;
48e68ff5
BT
347 param = &sco_param_cvsd[conn->attempt - 1];
348 }
10c62ddc 349 break;
2dea632f
FD
350 default:
351 return false;
10c62ddc 352 }
b6a0dc82 353
c7da5797 354 cp.retrans_effort = param->retrans_effort;
2dea632f
FD
355 cp.pkt_type = __cpu_to_le16(param->pkt_type);
356 cp.max_latency = __cpu_to_le16(param->max_latency);
357
358 if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
359 return false;
360
361 return true;
b6a0dc82
MH
362}
363
7d6ca693
JH
364u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
365 u16 to_multiplier)
2ce603eb 366{
2ce603eb 367 struct hci_dev *hdev = conn->hdev;
f044eb05
MH
368 struct hci_conn_params *params;
369 struct hci_cp_le_conn_update cp;
2ce603eb 370
f044eb05 371 hci_dev_lock(hdev);
2ce603eb 372
f044eb05
MH
373 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
374 if (params) {
375 params->conn_min_interval = min;
376 params->conn_max_interval = max;
377 params->conn_latency = latency;
378 params->supervision_timeout = to_multiplier;
379 }
380
381 hci_dev_unlock(hdev);
382
383 memset(&cp, 0, sizeof(cp));
2ce603eb
CT
384 cp.handle = cpu_to_le16(conn->handle);
385 cp.conn_interval_min = cpu_to_le16(min);
386 cp.conn_interval_max = cpu_to_le16(max);
387 cp.conn_latency = cpu_to_le16(latency);
388 cp.supervision_timeout = cpu_to_le16(to_multiplier);
dcf4adbf
JP
389 cp.min_ce_len = cpu_to_le16(0x0000);
390 cp.max_ce_len = cpu_to_le16(0x0000);
2ce603eb
CT
391
392 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
7d6ca693
JH
393
394 if (params)
395 return 0x01;
396
397 return 0x00;
2ce603eb 398}
2ce603eb 399
fe39c7b2 400void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
8b76ce34 401 __u8 ltk[16], __u8 key_size)
a7a595f6
VCG
402{
403 struct hci_dev *hdev = conn->hdev;
404 struct hci_cp_le_start_enc cp;
405
38b3fef1 406 BT_DBG("hcon %p", conn);
a7a595f6
VCG
407
408 memset(&cp, 0, sizeof(cp));
409
410 cp.handle = cpu_to_le16(conn->handle);
fe39c7b2 411 cp.rand = rand;
a7a595f6 412 cp.ediv = ediv;
8b76ce34 413 memcpy(cp.ltk, ltk, key_size);
a7a595f6
VCG
414
415 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
416}
a7a595f6 417
e73439d8
MH
418/* Device _must_ be locked */
419void hci_sco_setup(struct hci_conn *conn, __u8 status)
420{
421 struct hci_conn *sco = conn->link;
422
e73439d8
MH
423 if (!sco)
424 return;
425
38b3fef1
AE
426 BT_DBG("hcon %p", conn);
427
e73439d8
MH
428 if (!status) {
429 if (lmp_esco_capable(conn->hdev))
430 hci_setup_sync(sco, conn->handle);
431 else
432 hci_add_sco(sco, conn->handle);
433 } else {
539c496d 434 hci_connect_cfm(sco, status);
e73439d8
MH
435 hci_conn_del(sco);
436 }
437}
438
19c40e3b 439static void hci_conn_timeout(struct work_struct *work)
1da177e4 440{
19c40e3b 441 struct hci_conn *conn = container_of(work, struct hci_conn,
5974e4c4 442 disc_work.work);
1d56dc4f 443 int refcnt = atomic_read(&conn->refcnt);
1da177e4 444
38b3fef1 445 BT_DBG("hcon %p state %s", conn, state_to_string(conn->state));
1da177e4 446
1d56dc4f
LR
447 WARN_ON(refcnt < 0);
448
449 /* FIXME: It was observed that in pairing failed scenario, refcnt
450 * drops below 0. Probably this is because l2cap_conn_del calls
451 * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is
452 * dropped. After that loop hci_chan_del is called which also drops
453 * conn. For now make sure that ACL is alive if refcnt is higher then 0,
454 * otherwise drop it.
455 */
456 if (refcnt > 0)
1da177e4
LT
457 return;
458
6ac59344
MH
459 switch (conn->state) {
460 case BT_CONNECT:
769be974 461 case BT_CONNECT2:
fcd89c09
VT
462 if (conn->out) {
463 if (conn->type == ACL_LINK)
1aef8669 464 hci_acl_create_connection_cancel(conn);
cc2b6911
JP
465 else if (conn->type == LE_LINK) {
466 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
467 hci_connect_le_scan_remove(conn);
468 else
469 hci_le_create_connection_cancel(conn);
470 }
93796fa6
CT
471 } else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
472 hci_reject_sco(conn);
fcd89c09 473 }
6ac59344 474 break;
769be974 475 case BT_CONFIG:
8e87d142 476 case BT_CONNECTED:
40051e46
MH
477 if (conn->type == AMP_LINK) {
478 hci_amp_disconn(conn);
479 } else {
480 __u8 reason = hci_proto_disconn_ind(conn);
481 hci_disconnect(conn, reason);
482 }
6ac59344
MH
483 break;
484 default:
1da177e4 485 conn->state = BT_CLOSED;
6ac59344
MH
486 break;
487 }
1da177e4
LT
488}
489
416dc94b 490/* Enter sniff mode */
a74a84f6 491static void hci_conn_idle(struct work_struct *work)
416dc94b 492{
a74a84f6
JH
493 struct hci_conn *conn = container_of(work, struct hci_conn,
494 idle_work.work);
416dc94b
GP
495 struct hci_dev *hdev = conn->hdev;
496
38b3fef1 497 BT_DBG("hcon %p mode %d", conn, conn->mode);
416dc94b 498
416dc94b
GP
499 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
500 return;
501
502 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
503 return;
504
505 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
506 struct hci_cp_sniff_subrate cp;
507 cp.handle = cpu_to_le16(conn->handle);
dcf4adbf
JP
508 cp.max_latency = cpu_to_le16(0);
509 cp.min_remote_timeout = cpu_to_le16(0);
510 cp.min_local_timeout = cpu_to_le16(0);
416dc94b
GP
511 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
512 }
513
51a8efd7 514 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
416dc94b
GP
515 struct hci_cp_sniff_mode cp;
516 cp.handle = cpu_to_le16(conn->handle);
517 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
518 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
dcf4adbf
JP
519 cp.attempt = cpu_to_le16(4);
520 cp.timeout = cpu_to_le16(1);
416dc94b
GP
521 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
522 }
523}
524
7bc18d9d 525static void hci_conn_auto_accept(struct work_struct *work)
9f61656a 526{
7bc18d9d
JH
527 struct hci_conn *conn = container_of(work, struct hci_conn,
528 auto_accept_work.work);
9f61656a 529
7bc18d9d 530 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
5974e4c4 531 &conn->dst);
9f61656a
JH
532}
533
9489eca4
JH
534static void le_conn_timeout(struct work_struct *work)
535{
536 struct hci_conn *conn = container_of(work, struct hci_conn,
537 le_conn_timeout.work);
3c857757 538 struct hci_dev *hdev = conn->hdev;
9489eca4
JH
539
540 BT_DBG("");
541
3c857757
JH
542 /* We could end up here due to having done directed advertising,
543 * so clean up the state if necessary. This should however only
544 * happen with broken hardware or if low duty cycle was used
545 * (which doesn't have a timeout of its own).
546 */
0b1db38c 547 if (conn->role == HCI_ROLE_SLAVE) {
3c857757
JH
548 u8 enable = 0x00;
549 hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
550 &enable);
551 hci_le_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
552 return;
553 }
554
9489eca4
JH
555 hci_le_create_connection_cancel(conn);
556}
557
a5c4e309
JH
558struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
559 u8 role)
1da177e4
LT
560{
561 struct hci_conn *conn;
562
6ed93dc6 563 BT_DBG("%s dst %pMR", hdev->name, dst);
1da177e4 564
27f70f3e 565 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
04837f64 566 if (!conn)
1da177e4 567 return NULL;
1da177e4
LT
568
569 bacpy(&conn->dst, dst);
662e8820 570 bacpy(&conn->src, &hdev->bdaddr);
a8746417
MH
571 conn->hdev = hdev;
572 conn->type = type;
a5c4e309 573 conn->role = role;
a8746417
MH
574 conn->mode = HCI_CM_ACTIVE;
575 conn->state = BT_OPEN;
93f19c9f 576 conn->auth_type = HCI_AT_GENERAL_BONDING;
17fa4b9d 577 conn->io_capability = hdev->io_capability;
a9583556 578 conn->remote_auth = 0xff;
13d39315 579 conn->key_type = 0xff;
ebf86aa3 580 conn->rssi = HCI_RSSI_INVALID;
5a134fae 581 conn->tx_power = HCI_TX_POWER_INVALID;
d0455ed9 582 conn->max_tx_power = HCI_TX_POWER_INVALID;
1da177e4 583
58a681ef 584 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
052b30b0 585 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
04837f64 586
a5c4e309
JH
587 if (conn->role == HCI_ROLE_MASTER)
588 conn->out = true;
589
a8746417
MH
590 switch (type) {
591 case ACL_LINK:
592 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
593 break;
9c84d1da
JH
594 case LE_LINK:
595 /* conn->src should reflect the local identity address */
596 hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
597 break;
a8746417
MH
598 case SCO_LINK:
599 if (lmp_esco_capable(hdev))
efc7688b
MH
600 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
601 (hdev->esco_type & EDR_ESCO_MASK);
a8746417
MH
602 else
603 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
604 break;
605 case ESCO_LINK:
efc7688b 606 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
a8746417
MH
607 break;
608 }
609
1da177e4 610 skb_queue_head_init(&conn->data_q);
04837f64 611
70c1f20b 612 INIT_LIST_HEAD(&conn->chan_list);
73d80deb 613
19c40e3b 614 INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
7bc18d9d 615 INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
a74a84f6 616 INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
9489eca4 617 INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
8ce783dc 618 INIT_WORK(&conn->le_scan_cleanup, le_scan_cleanup);
1da177e4
LT
619
620 atomic_set(&conn->refcnt, 0);
621
622 hci_dev_hold(hdev);
623
1da177e4 624 hci_conn_hash_add(hdev, conn);
3c54711c 625 if (hdev->notify)
1da177e4
LT
626 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
627
a67e899c
MH
628 hci_conn_init_sysfs(conn);
629
1da177e4
LT
630 return conn;
631}
632
633int hci_conn_del(struct hci_conn *conn)
634{
635 struct hci_dev *hdev = conn->hdev;
636
38b3fef1 637 BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
1da177e4 638
19c40e3b 639 cancel_delayed_work_sync(&conn->disc_work);
7bc18d9d 640 cancel_delayed_work_sync(&conn->auto_accept_work);
a74a84f6 641 cancel_delayed_work_sync(&conn->idle_work);
9f61656a 642
5b7f9909 643 if (conn->type == ACL_LINK) {
1da177e4
LT
644 struct hci_conn *sco = conn->link;
645 if (sco)
646 sco->link = NULL;
647
648 /* Unacked frames */
649 hdev->acl_cnt += conn->sent;
6ed58ec5 650 } else if (conn->type == LE_LINK) {
980ffc0a 651 cancel_delayed_work(&conn->le_conn_timeout);
9489eca4 652
6ed58ec5
VT
653 if (hdev->le_pkts)
654 hdev->le_cnt += conn->sent;
655 else
656 hdev->acl_cnt += conn->sent;
5b7f9909
MH
657 } else {
658 struct hci_conn *acl = conn->link;
659 if (acl) {
660 acl->link = NULL;
76a68ba0 661 hci_conn_drop(acl);
5b7f9909 662 }
1da177e4
LT
663 }
664
9740e49d
AE
665 if (conn->amp_mgr)
666 amp_mgr_put(conn->amp_mgr);
667
1da177e4 668 skb_queue_purge(&conn->data_q);
1da177e4 669
b958f9a3
JH
670 /* Remove the connection from the list and cleanup its remaining
671 * state. This is a separate function since for some cases like
672 * BT_CONNECT_SCAN we *only* want the cleanup part without the
673 * rest of hci_conn_del.
674 */
675 hci_conn_cleanup(conn);
163f4dab 676
1da177e4
LT
677 return 0;
678}
679
680struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
681{
682 int use_src = bacmp(src, BDADDR_ANY);
8035ded4 683 struct hci_dev *hdev = NULL, *d;
1da177e4 684
6ed93dc6 685 BT_DBG("%pMR -> %pMR", src, dst);
1da177e4 686
f20d09d5 687 read_lock(&hci_dev_list_lock);
1da177e4 688
8035ded4 689 list_for_each_entry(d, &hci_dev_list, list) {
8fc9ced3 690 if (!test_bit(HCI_UP, &d->flags) ||
d7a5a11d 691 hci_dev_test_flag(d, HCI_USER_CHANNEL) ||
d300fa9b 692 d->dev_type != HCI_BREDR)
1da177e4
LT
693 continue;
694
8e87d142 695 /* Simple routing:
1da177e4
LT
696 * No source address - find interface with bdaddr != dst
697 * Source address - find interface with bdaddr == src
698 */
699
700 if (use_src) {
701 if (!bacmp(&d->bdaddr, src)) {
702 hdev = d; break;
703 }
704 } else {
705 if (bacmp(&d->bdaddr, dst)) {
706 hdev = d; break;
707 }
708 }
709 }
710
711 if (hdev)
712 hdev = hci_dev_hold(hdev);
713
f20d09d5 714 read_unlock(&hci_dev_list_lock);
1da177e4
LT
715 return hdev;
716}
717EXPORT_SYMBOL(hci_get_route);
718
9bb3c01f 719/* This function requires the caller holds hdev->lock */
06c053fb 720void hci_le_conn_failed(struct hci_conn *conn, u8 status)
9bb3c01f
AG
721{
722 struct hci_dev *hdev = conn->hdev;
f161dd41
JH
723 struct hci_conn_params *params;
724
725 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
726 conn->dst_type);
727 if (params && params->conn) {
728 hci_conn_drop(params->conn);
f8aaf9b6 729 hci_conn_put(params->conn);
f161dd41
JH
730 params->conn = NULL;
731 }
9bb3c01f
AG
732
733 conn->state = BT_CLOSED;
734
735 mgmt_connect_failed(hdev, &conn->dst, conn->type, conn->dst_type,
736 status);
737
539c496d 738 hci_connect_cfm(conn, status);
9bb3c01f
AG
739
740 hci_conn_del(conn);
a4790dbd
AG
741
742 /* Since we may have temporarily stopped the background scanning in
743 * favor of connection establishment, we should restart it.
744 */
745 hci_update_background_scan(hdev);
3c857757
JH
746
747 /* Re-enable advertising in case this was a failed connection
748 * attempt as a peripheral.
749 */
750 mgmt_reenable_advertising(hdev);
9bb3c01f
AG
751}
752
1904a853 753static void create_le_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode)
1d399ae5
AG
754{
755 struct hci_conn *conn;
756
28a667c9
JP
757 hci_dev_lock(hdev);
758
759 conn = hci_lookup_le_connect(hdev);
760
761 if (!status) {
762 hci_connect_le_scan_cleanup(conn);
763 goto done;
764 }
1d399ae5
AG
765
766 BT_ERR("HCI request failed to create LE connection: status 0x%2.2x",
767 status);
768
1d399ae5
AG
769 if (!conn)
770 goto done;
771
06c053fb 772 hci_le_conn_failed(conn, status);
1d399ae5
AG
773
774done:
775 hci_dev_unlock(hdev);
776}
777
2acf3d90
AG
778static void hci_req_add_le_create_conn(struct hci_request *req,
779 struct hci_conn *conn)
780{
781 struct hci_cp_le_create_conn cp;
782 struct hci_dev *hdev = conn->hdev;
783 u8 own_addr_type;
784
785 memset(&cp, 0, sizeof(cp));
786
787 /* Update random address, but set require_privacy to false so
9437d2ed 788 * that we never connect with an non-resolvable address.
2acf3d90
AG
789 */
790 if (hci_update_random_address(req, false, &own_addr_type))
791 return;
792
2acf3d90
AG
793 cp.scan_interval = cpu_to_le16(hdev->le_scan_interval);
794 cp.scan_window = cpu_to_le16(hdev->le_scan_window);
795 bacpy(&cp.peer_addr, &conn->dst);
796 cp.peer_addr_type = conn->dst_type;
797 cp.own_address_type = own_addr_type;
798 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
799 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
037fc415
MH
800 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
801 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
dcf4adbf
JP
802 cp.min_ce_len = cpu_to_le16(0x0000);
803 cp.max_ce_len = cpu_to_le16(0x0000);
2acf3d90
AG
804
805 hci_req_add(req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
b46e0030
JH
806
807 conn->state = BT_CONNECT;
28a667c9 808 clear_bit(HCI_CONN_SCANNING, &conn->flags);
2acf3d90
AG
809}
810
3c857757
JH
811static void hci_req_directed_advertising(struct hci_request *req,
812 struct hci_conn *conn)
813{
814 struct hci_dev *hdev = req->hdev;
815 struct hci_cp_le_set_adv_param cp;
816 u8 own_addr_type;
817 u8 enable;
818
5ce194c4 819 /* Clear the HCI_LE_ADV bit temporarily so that the
3c857757
JH
820 * hci_update_random_address knows that it's safe to go ahead
821 * and write a new random address. The flag will be set back on
822 * as soon as the SET_ADV_ENABLE HCI command completes.
823 */
a358dc11 824 hci_dev_clear_flag(hdev, HCI_LE_ADV);
3c857757
JH
825
826 /* Set require_privacy to false so that the remote device has a
827 * chance of identifying us.
828 */
829 if (hci_update_random_address(req, false, &own_addr_type) < 0)
830 return;
831
832 memset(&cp, 0, sizeof(cp));
833 cp.type = LE_ADV_DIRECT_IND;
834 cp.own_address_type = own_addr_type;
835 cp.direct_addr_type = conn->dst_type;
836 bacpy(&cp.direct_addr, &conn->dst);
837 cp.channel_map = hdev->le_adv_channel_map;
838
839 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
840
841 enable = 0x01;
842 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
843
844 conn->state = BT_CONNECT;
845}
846
04a6c589 847struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
cdd6275e 848 u8 dst_type, u8 sec_level, u16 conn_timeout,
e804d25d 849 u8 role)
1da177e4 850{
4292f1f3 851 struct hci_conn_params *params;
28a667c9 852 struct hci_conn *conn, *conn_unfinished;
1ebfcc1f 853 struct smp_irk *irk;
2acf3d90 854 struct hci_request req;
1d399ae5 855 int err;
1da177e4 856
152d386e 857 /* Let's make sure that le is enabled.*/
d7a5a11d 858 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
152d386e
LR
859 if (lmp_le_capable(hdev))
860 return ERR_PTR(-ECONNREFUSED);
861
862 return ERR_PTR(-EOPNOTSUPP);
863 }
864
620ad521
AG
865 /* Some devices send ATT messages as soon as the physical link is
866 * established. To be able to handle these ATT messages, the user-
867 * space first establishes the connection and then starts the pairing
868 * process.
869 *
870 * So if a hci_conn object already exists for the following connection
871 * attempt, we simply update pending_sec_level and auth_type fields
872 * and return the object found.
873 */
9d4c1cc1 874 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
28a667c9 875 conn_unfinished = NULL;
620ad521 876 if (conn) {
28a667c9
JP
877 if (conn->state == BT_CONNECT &&
878 test_bit(HCI_CONN_SCANNING, &conn->flags)) {
879 BT_DBG("will continue unfinished conn %pMR", dst);
880 conn_unfinished = conn;
881 } else {
882 if (conn->pending_sec_level < sec_level)
883 conn->pending_sec_level = sec_level;
884 goto done;
885 }
620ad521 886 }
dfc94dbd 887
620ad521
AG
888 /* Since the controller supports only one LE connection attempt at a
889 * time, we return -EBUSY if there is any connection attempt running.
890 */
e7d9ab73 891 if (hci_lookup_le_connect(hdev))
620ad521 892 return ERR_PTR(-EBUSY);
46a190cb 893
edb4b466
MH
894 /* When given an identity address with existing identity
895 * resolving key, the connection needs to be established
896 * to a resolvable random address.
897 *
edb4b466
MH
898 * Storing the resolvable random address is required here
899 * to handle connection failures. The address will later
900 * be resolved back into the original identity address
901 * from the connect request.
902 */
1ebfcc1f
JH
903 irk = hci_find_irk_by_addr(hdev, dst, dst_type);
904 if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
905 dst = &irk->rpa;
906 dst_type = ADDR_LE_DEV_RANDOM;
907 }
908
28a667c9
JP
909 if (conn_unfinished) {
910 conn = conn_unfinished;
911 bacpy(&conn->dst, dst);
912 } else {
913 conn = hci_conn_add(hdev, LE_LINK, dst, role);
914 }
915
620ad521
AG
916 if (!conn)
917 return ERR_PTR(-ENOMEM);
9f0caeb1 918
1ebfcc1f 919 conn->dst_type = dst_type;
620ad521 920 conn->sec_level = BT_SECURITY_LOW;
09ae260b 921 conn->conn_timeout = conn_timeout;
4292f1f3 922
28a667c9
JP
923 if (!conn_unfinished)
924 conn->pending_sec_level = sec_level;
925
3c857757
JH
926 hci_req_init(&req, hdev);
927
376f54c1
JH
928 /* Disable advertising if we're active. For master role
929 * connections most controllers will refuse to connect if
930 * advertising is enabled, and for slave role connections we
931 * anyway have to disable it in order to start directed
932 * advertising.
933 */
d7a5a11d 934 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
376f54c1
JH
935 u8 enable = 0x00;
936 hci_req_add(&req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
937 &enable);
938 }
939
cdd6275e 940 /* If requested to connect as slave use directed advertising */
e804d25d 941 if (conn->role == HCI_ROLE_SLAVE) {
e8bb6b97
JH
942 /* If we're active scanning most controllers are unable
943 * to initiate advertising. Simply reject the attempt.
944 */
d7a5a11d 945 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
e8bb6b97
JH
946 hdev->le_scan_type == LE_SCAN_ACTIVE) {
947 skb_queue_purge(&req.cmd_q);
948 hci_conn_del(conn);
949 return ERR_PTR(-EBUSY);
950 }
951
3c857757
JH
952 hci_req_directed_advertising(&req, conn);
953 goto create_conn;
954 }
955
4292f1f3
AG
956 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
957 if (params) {
958 conn->le_conn_min_interval = params->conn_min_interval;
959 conn->le_conn_max_interval = params->conn_max_interval;
037fc415
MH
960 conn->le_conn_latency = params->conn_latency;
961 conn->le_supv_timeout = params->supervision_timeout;
4292f1f3
AG
962 } else {
963 conn->le_conn_min_interval = hdev->le_conn_min_interval;
964 conn->le_conn_max_interval = hdev->le_conn_max_interval;
04fb7d90
MH
965 conn->le_conn_latency = hdev->le_conn_latency;
966 conn->le_supv_timeout = hdev->le_supv_timeout;
4292f1f3 967 }
eda42b50 968
2acf3d90 969 /* If controller is scanning, we stop it since some controllers are
81ad6fd9
JH
970 * not able to scan and connect at the same time. Also set the
971 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
972 * handler for scan disabling knows to set the correct discovery
973 * state.
2acf3d90 974 */
d7a5a11d 975 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
2acf3d90 976 hci_req_add_le_scan_disable(&req);
a1536da2 977 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
2acf3d90
AG
978 }
979
81ad6fd9
JH
980 hci_req_add_le_create_conn(&req, conn);
981
3c857757 982create_conn:
81ad6fd9 983 err = hci_req_run(&req, create_le_conn_complete);
2acf3d90
AG
984 if (err) {
985 hci_conn_del(conn);
620ad521 986 return ERR_PTR(err);
2acf3d90 987 }
fcd89c09 988
620ad521 989done:
28a667c9
JP
990 /* If this is continuation of connect started by hci_connect_le_scan,
991 * it already called hci_conn_hold and calling it again would mess the
992 * counter.
993 */
994 if (!conn_unfinished)
995 hci_conn_hold(conn);
996
f1e5d547 997 return conn;
d04aef4c 998}
fcd89c09 999
f75113a2
JP
1000static void hci_connect_le_scan_complete(struct hci_dev *hdev, u8 status,
1001 u16 opcode)
1002{
1003 struct hci_conn *conn;
1004
1005 if (!status)
1006 return;
1007
1008 BT_ERR("Failed to add device to auto conn whitelist: status 0x%2.2x",
1009 status);
1010
1011 hci_dev_lock(hdev);
1012
1013 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
1014 if (conn)
1015 hci_le_conn_failed(conn, status);
1016
1017 hci_dev_unlock(hdev);
1018}
1019
1020static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
1021{
1022 struct hci_conn *conn;
1023
9d4c1cc1 1024 conn = hci_conn_hash_lookup_le(hdev, addr, type);
f75113a2
JP
1025 if (!conn)
1026 return false;
1027
f75113a2
JP
1028 if (conn->state != BT_CONNECTED)
1029 return false;
1030
1031 return true;
1032}
1033
1034/* This function requires the caller holds hdev->lock */
1035static int hci_explicit_conn_params_set(struct hci_request *req,
1036 bdaddr_t *addr, u8 addr_type)
1037{
1038 struct hci_dev *hdev = req->hdev;
1039 struct hci_conn_params *params;
1040
1041 if (is_connected(hdev, addr, addr_type))
1042 return -EISCONN;
1043
5157b8a5
JP
1044 params = hci_conn_params_lookup(hdev, addr, addr_type);
1045 if (!params) {
1046 params = hci_conn_params_add(hdev, addr, addr_type);
1047 if (!params)
1048 return -ENOMEM;
1049
1050 /* If we created new params, mark them to be deleted in
1051 * hci_connect_le_scan_cleanup. It's different case than
1052 * existing disabled params, those will stay after cleanup.
1053 */
1054 params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
1055 }
f75113a2 1056
5157b8a5 1057 /* We're trying to connect, so make sure params are at pend_le_conns */
49c50922 1058 if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
5157b8a5
JP
1059 params->auto_connect == HCI_AUTO_CONN_REPORT ||
1060 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
f75113a2
JP
1061 list_del_init(&params->action);
1062 list_add(&params->action, &hdev->pend_le_conns);
1063 }
1064
1065 params->explicit_connect = true;
1066 __hci_update_background_scan(req);
1067
1068 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
1069 params->auto_connect);
1070
1071 return 0;
1072}
1073
1074/* This function requires the caller holds hdev->lock */
1075struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1076 u8 dst_type, u8 sec_level,
1077 u16 conn_timeout, u8 role)
1078{
1079 struct hci_conn *conn;
1080 struct hci_request req;
1081 int err;
1082
1083 /* Let's make sure that le is enabled.*/
1084 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1085 if (lmp_le_capable(hdev))
1086 return ERR_PTR(-ECONNREFUSED);
1087
1088 return ERR_PTR(-EOPNOTSUPP);
1089 }
1090
1091 /* Some devices send ATT messages as soon as the physical link is
1092 * established. To be able to handle these ATT messages, the user-
1093 * space first establishes the connection and then starts the pairing
1094 * process.
1095 *
1096 * So if a hci_conn object already exists for the following connection
1097 * attempt, we simply update pending_sec_level and auth_type fields
1098 * and return the object found.
1099 */
9d4c1cc1 1100 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
f75113a2
JP
1101 if (conn) {
1102 if (conn->pending_sec_level < sec_level)
1103 conn->pending_sec_level = sec_level;
1104 goto done;
1105 }
1106
1107 BT_DBG("requesting refresh of dst_addr");
1108
1109 conn = hci_conn_add(hdev, LE_LINK, dst, role);
1110 if (!conn)
1111 return ERR_PTR(-ENOMEM);
1112
1113 hci_req_init(&req, hdev);
1114
1115 if (hci_explicit_conn_params_set(&req, dst, dst_type) < 0)
1116 return ERR_PTR(-EBUSY);
1117
1118 conn->state = BT_CONNECT;
1119 set_bit(HCI_CONN_SCANNING, &conn->flags);
1120
1121 err = hci_req_run(&req, hci_connect_le_scan_complete);
1122 if (err && err != -ENODATA) {
1123 hci_conn_del(conn);
1124 return ERR_PTR(err);
1125 }
1126
1127 conn->dst_type = dst_type;
1128 conn->sec_level = BT_SECURITY_LOW;
1129 conn->pending_sec_level = sec_level;
1130 conn->conn_timeout = conn_timeout;
1131
1132done:
1133 hci_conn_hold(conn);
1134 return conn;
1135}
1136
04a6c589
AG
1137struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
1138 u8 sec_level, u8 auth_type)
1da177e4
LT
1139{
1140 struct hci_conn *acl;
fcd89c09 1141
d7a5a11d 1142 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
c411110e
LR
1143 if (lmp_bredr_capable(hdev))
1144 return ERR_PTR(-ECONNREFUSED);
1145
beb19e4c 1146 return ERR_PTR(-EOPNOTSUPP);
c411110e 1147 }
56f87901 1148
70f23020
AE
1149 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
1150 if (!acl) {
a5c4e309 1151 acl = hci_conn_add(hdev, ACL_LINK, dst, HCI_ROLE_MASTER);
70f23020 1152 if (!acl)
48c7aba9 1153 return ERR_PTR(-ENOMEM);
1da177e4
LT
1154 }
1155
1156 hci_conn_hold(acl);
1157
09ab6f4c 1158 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
765c2a96
JH
1159 acl->sec_level = BT_SECURITY_LOW;
1160 acl->pending_sec_level = sec_level;
09ab6f4c 1161 acl->auth_type = auth_type;
1aef8669 1162 hci_acl_create_connection(acl);
09ab6f4c 1163 }
1da177e4 1164
db474275
VCG
1165 return acl;
1166}
1167
10c62ddc
FD
1168struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
1169 __u16 setting)
db474275
VCG
1170{
1171 struct hci_conn *acl;
1172 struct hci_conn *sco;
1173
e660ed6c 1174 acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
db474275 1175 if (IS_ERR(acl))
5b7f9909 1176 return acl;
1da177e4 1177
70f23020
AE
1178 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
1179 if (!sco) {
a5c4e309 1180 sco = hci_conn_add(hdev, type, dst, HCI_ROLE_MASTER);
70f23020 1181 if (!sco) {
76a68ba0 1182 hci_conn_drop(acl);
48c7aba9 1183 return ERR_PTR(-ENOMEM);
1da177e4 1184 }
5b7f9909 1185 }
1da177e4 1186
5b7f9909
MH
1187 acl->link = sco;
1188 sco->link = acl;
1da177e4 1189
5b7f9909 1190 hci_conn_hold(sco);
1da177e4 1191
10c62ddc
FD
1192 sco->setting = setting;
1193
5b7f9909 1194 if (acl->state == BT_CONNECTED &&
5974e4c4 1195 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
58a681ef 1196 set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
14b12d0b 1197 hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
c390216b 1198
51a8efd7 1199 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
e73439d8 1200 /* defer SCO setup until mode change completed */
51a8efd7 1201 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
e73439d8
MH
1202 return sco;
1203 }
1204
1205 hci_sco_setup(acl, 0x00);
b6a0dc82 1206 }
5b7f9909
MH
1207
1208 return sco;
1da177e4 1209}
1da177e4 1210
e7c29cb1
MH
1211/* Check link security requirement */
1212int hci_conn_check_link_mode(struct hci_conn *conn)
1213{
38b3fef1 1214 BT_DBG("hcon %p", conn);
e7c29cb1 1215
40b552aa
MH
1216 /* In Secure Connections Only mode, it is required that Secure
1217 * Connections is used and the link is encrypted with AES-CCM
1218 * using a P-256 authenticated combination key.
1219 */
d7a5a11d 1220 if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
40b552aa
MH
1221 if (!hci_conn_sc_enabled(conn) ||
1222 !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
1223 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
1224 return 0;
1225 }
1226
4dae2798
JH
1227 if (hci_conn_ssp_enabled(conn) &&
1228 !test_bit(HCI_CONN_ENCRYPT, &conn->flags))
e7c29cb1
MH
1229 return 0;
1230
1231 return 1;
1232}
e7c29cb1 1233
1da177e4 1234/* Authenticate remote device */
0684e5f9 1235static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
1da177e4 1236{
38b3fef1 1237 BT_DBG("hcon %p", conn);
1da177e4 1238
765c2a96
JH
1239 if (conn->pending_sec_level > sec_level)
1240 sec_level = conn->pending_sec_level;
1241
96a31833 1242 if (sec_level > conn->sec_level)
765c2a96 1243 conn->pending_sec_level = sec_level;
4dae2798 1244 else if (test_bit(HCI_CONN_AUTH, &conn->flags))
1da177e4
LT
1245 return 1;
1246
65cf686e
JH
1247 /* Make sure we preserve an existing MITM requirement*/
1248 auth_type |= (conn->auth_type & 0x01);
1249
96a31833
MH
1250 conn->auth_type = auth_type;
1251
51a8efd7 1252 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
1da177e4 1253 struct hci_cp_auth_requested cp;
b7d05bad 1254
aca3192c 1255 cp.handle = cpu_to_le16(conn->handle);
40be492f 1256 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
5974e4c4 1257 sizeof(cp), &cp);
09da1f34
JH
1258
1259 /* If we're already encrypted set the REAUTH_PEND flag,
1260 * otherwise set the ENCRYPT_PEND.
1261 */
4dae2798 1262 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
51a8efd7 1263 set_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
09da1f34
JH
1264 else
1265 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
1da177e4 1266 }
8c1b2355 1267
1da177e4
LT
1268 return 0;
1269}
1da177e4 1270
13d39315
WR
1271/* Encrypt the the link */
1272static void hci_conn_encrypt(struct hci_conn *conn)
1273{
38b3fef1 1274 BT_DBG("hcon %p", conn);
13d39315 1275
51a8efd7 1276 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
13d39315
WR
1277 struct hci_cp_set_conn_encrypt cp;
1278 cp.handle = cpu_to_le16(conn->handle);
1279 cp.encrypt = 0x01;
1280 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
5974e4c4 1281 &cp);
13d39315
WR
1282 }
1283}
1284
8c1b2355 1285/* Enable security */
e7cafc45
JH
1286int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
1287 bool initiator)
1da177e4 1288{
38b3fef1 1289 BT_DBG("hcon %p", conn);
1da177e4 1290
d8343f12
VCG
1291 if (conn->type == LE_LINK)
1292 return smp_conn_security(conn, sec_level);
1293
13d39315 1294 /* For sdp we don't need the link key. */
8c1b2355
MH
1295 if (sec_level == BT_SECURITY_SDP)
1296 return 1;
1297
13d39315
WR
1298 /* For non 2.1 devices and low security level we don't need the link
1299 key. */
aa64a8b5 1300 if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
3fdca1e1 1301 return 1;
8c1b2355 1302
13d39315 1303 /* For other security levels we need the link key. */
4dae2798 1304 if (!test_bit(HCI_CONN_AUTH, &conn->flags))
13d39315
WR
1305 goto auth;
1306
7b5a9241
MH
1307 /* An authenticated FIPS approved combination key has sufficient
1308 * security for security level 4. */
1309 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256 &&
1310 sec_level == BT_SECURITY_FIPS)
1311 goto encrypt;
1312
1313 /* An authenticated combination key has sufficient security for
1314 security level 3. */
1315 if ((conn->key_type == HCI_LK_AUTH_COMBINATION_P192 ||
1316 conn->key_type == HCI_LK_AUTH_COMBINATION_P256) &&
1317 sec_level == BT_SECURITY_HIGH)
13d39315
WR
1318 goto encrypt;
1319
1320 /* An unauthenticated combination key has sufficient security for
1321 security level 1 and 2. */
66138ce8
MH
1322 if ((conn->key_type == HCI_LK_UNAUTH_COMBINATION_P192 ||
1323 conn->key_type == HCI_LK_UNAUTH_COMBINATION_P256) &&
5974e4c4 1324 (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW))
13d39315
WR
1325 goto encrypt;
1326
1327 /* A combination key has always sufficient security for the security
1328 levels 1 or 2. High security level requires the combination key
1329 is generated using maximum PIN code length (16).
1330 For pre 2.1 units. */
1331 if (conn->key_type == HCI_LK_COMBINATION &&
7b5a9241
MH
1332 (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW ||
1333 conn->pin_length == 16))
13d39315
WR
1334 goto encrypt;
1335
1336auth:
51a8efd7 1337 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
1da177e4
LT
1338 return 0;
1339
977f8fce
JH
1340 if (initiator)
1341 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
1342
6fdf658c
LAD
1343 if (!hci_conn_auth(conn, sec_level, auth_type))
1344 return 0;
13d39315
WR
1345
1346encrypt:
4dae2798 1347 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
13d39315 1348 return 1;
8c1b2355 1349
13d39315 1350 hci_conn_encrypt(conn);
1da177e4
LT
1351 return 0;
1352}
8c1b2355 1353EXPORT_SYMBOL(hci_conn_security);
1da177e4 1354
b3b1b061
WR
1355/* Check secure link requirement */
1356int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
1357{
38b3fef1 1358 BT_DBG("hcon %p", conn);
b3b1b061 1359
9cb2e030
MH
1360 /* Accept if non-secure or higher security level is required */
1361 if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS)
1362 return 1;
b3b1b061 1363
9cb2e030
MH
1364 /* Accept if secure or higher security level is already present */
1365 if (conn->sec_level == BT_SECURITY_HIGH ||
1366 conn->sec_level == BT_SECURITY_FIPS)
b3b1b061
WR
1367 return 1;
1368
9cb2e030
MH
1369 /* Reject not secure link */
1370 return 0;
b3b1b061
WR
1371}
1372EXPORT_SYMBOL(hci_conn_check_secure);
1373
1da177e4 1374/* Switch role */
8c1b2355 1375int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
1da177e4 1376{
38b3fef1 1377 BT_DBG("hcon %p", conn);
1da177e4 1378
40bef302 1379 if (role == conn->role)
1da177e4
LT
1380 return 1;
1381
51a8efd7 1382 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
1da177e4
LT
1383 struct hci_cp_switch_role cp;
1384 bacpy(&cp.bdaddr, &conn->dst);
1385 cp.role = role;
a9de9248 1386 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
1da177e4 1387 }
8c1b2355 1388
1da177e4
LT
1389 return 0;
1390}
1391EXPORT_SYMBOL(hci_conn_switch_role);
1392
04837f64 1393/* Enter active mode */
14b12d0b 1394void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
04837f64
MH
1395{
1396 struct hci_dev *hdev = conn->hdev;
1397
38b3fef1 1398 BT_DBG("hcon %p mode %d", conn, conn->mode);
04837f64 1399
14b12d0b
JG
1400 if (conn->mode != HCI_CM_SNIFF)
1401 goto timer;
1402
58a681ef 1403 if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
04837f64
MH
1404 goto timer;
1405
51a8efd7 1406 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
04837f64 1407 struct hci_cp_exit_sniff_mode cp;
aca3192c 1408 cp.handle = cpu_to_le16(conn->handle);
a9de9248 1409 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
04837f64
MH
1410 }
1411
1412timer:
1413 if (hdev->idle_timeout > 0)
a74a84f6
JH
1414 queue_delayed_work(hdev->workqueue, &conn->idle_work,
1415 msecs_to_jiffies(hdev->idle_timeout));
04837f64
MH
1416}
1417
1da177e4
LT
1418/* Drop all connection on the device */
1419void hci_conn_hash_flush(struct hci_dev *hdev)
1420{
1421 struct hci_conn_hash *h = &hdev->conn_hash;
3c4e0df0 1422 struct hci_conn *c, *n;
1da177e4
LT
1423
1424 BT_DBG("hdev %s", hdev->name);
1425
3c4e0df0 1426 list_for_each_entry_safe(c, n, &h->list, list) {
1da177e4
LT
1427 c->state = BT_CLOSED;
1428
3a6d576b 1429 hci_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM);
1da177e4
LT
1430 hci_conn_del(c);
1431 }
1432}
1433
a9de9248
MH
1434/* Check pending connect attempts */
1435void hci_conn_check_pending(struct hci_dev *hdev)
1436{
1437 struct hci_conn *conn;
1438
1439 BT_DBG("hdev %s", hdev->name);
1440
1441 hci_dev_lock(hdev);
1442
1443 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1444 if (conn)
1aef8669 1445 hci_acl_create_connection(conn);
a9de9248
MH
1446
1447 hci_dev_unlock(hdev);
1448}
1449
4dae2798
JH
1450static u32 get_link_mode(struct hci_conn *conn)
1451{
1452 u32 link_mode = 0;
1453
40bef302 1454 if (conn->role == HCI_ROLE_MASTER)
4dae2798
JH
1455 link_mode |= HCI_LM_MASTER;
1456
1457 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
1458 link_mode |= HCI_LM_ENCRYPT;
1459
1460 if (test_bit(HCI_CONN_AUTH, &conn->flags))
1461 link_mode |= HCI_LM_AUTH;
1462
1463 if (test_bit(HCI_CONN_SECURE, &conn->flags))
1464 link_mode |= HCI_LM_SECURE;
1465
1466 if (test_bit(HCI_CONN_FIPS, &conn->flags))
1467 link_mode |= HCI_LM_FIPS;
1468
1469 return link_mode;
1470}
1471
1da177e4
LT
1472int hci_get_conn_list(void __user *arg)
1473{
fc5fef61 1474 struct hci_conn *c;
1da177e4
LT
1475 struct hci_conn_list_req req, *cl;
1476 struct hci_conn_info *ci;
1477 struct hci_dev *hdev;
1da177e4
LT
1478 int n = 0, size, err;
1479
1480 if (copy_from_user(&req, arg, sizeof(req)))
1481 return -EFAULT;
1482
1483 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1484 return -EINVAL;
1485
1486 size = sizeof(req) + req.conn_num * sizeof(*ci);
1487
70f23020
AE
1488 cl = kmalloc(size, GFP_KERNEL);
1489 if (!cl)
1da177e4
LT
1490 return -ENOMEM;
1491
70f23020
AE
1492 hdev = hci_dev_get(req.dev_id);
1493 if (!hdev) {
1da177e4
LT
1494 kfree(cl);
1495 return -ENODEV;
1496 }
1497
1498 ci = cl->conn_info;
1499
09fd0de5 1500 hci_dev_lock(hdev);
8035ded4 1501 list_for_each_entry(c, &hdev->conn_hash.list, list) {
1da177e4
LT
1502 bacpy(&(ci + n)->bdaddr, &c->dst);
1503 (ci + n)->handle = c->handle;
1504 (ci + n)->type = c->type;
1505 (ci + n)->out = c->out;
1506 (ci + n)->state = c->state;
4dae2798 1507 (ci + n)->link_mode = get_link_mode(c);
1da177e4
LT
1508 if (++n >= req.conn_num)
1509 break;
1510 }
09fd0de5 1511 hci_dev_unlock(hdev);
1da177e4
LT
1512
1513 cl->dev_id = hdev->id;
1514 cl->conn_num = n;
1515 size = sizeof(req) + n * sizeof(*ci);
1516
1517 hci_dev_put(hdev);
1518
1519 err = copy_to_user(arg, cl, size);
1520 kfree(cl);
1521
1522 return err ? -EFAULT : 0;
1523}
1524
1525int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1526{
1527 struct hci_conn_info_req req;
1528 struct hci_conn_info ci;
1529 struct hci_conn *conn;
1530 char __user *ptr = arg + sizeof(req);
1531
1532 if (copy_from_user(&req, arg, sizeof(req)))
1533 return -EFAULT;
1534
09fd0de5 1535 hci_dev_lock(hdev);
1da177e4
LT
1536 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1537 if (conn) {
1538 bacpy(&ci.bdaddr, &conn->dst);
1539 ci.handle = conn->handle;
1540 ci.type = conn->type;
1541 ci.out = conn->out;
1542 ci.state = conn->state;
4dae2798 1543 ci.link_mode = get_link_mode(conn);
1da177e4 1544 }
09fd0de5 1545 hci_dev_unlock(hdev);
1da177e4
LT
1546
1547 if (!conn)
1548 return -ENOENT;
1549
1550 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1551}
40be492f
MH
1552
1553int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1554{
1555 struct hci_auth_info_req req;
1556 struct hci_conn *conn;
1557
1558 if (copy_from_user(&req, arg, sizeof(req)))
1559 return -EFAULT;
1560
09fd0de5 1561 hci_dev_lock(hdev);
40be492f
MH
1562 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1563 if (conn)
1564 req.type = conn->auth_type;
09fd0de5 1565 hci_dev_unlock(hdev);
40be492f
MH
1566
1567 if (!conn)
1568 return -ENOENT;
1569
1570 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1571}
73d80deb
LAD
1572
1573struct hci_chan *hci_chan_create(struct hci_conn *conn)
1574{
1575 struct hci_dev *hdev = conn->hdev;
1576 struct hci_chan *chan;
1577
38b3fef1 1578 BT_DBG("%s hcon %p", hdev->name, conn);
73d80deb 1579
f94b665d
JH
1580 if (test_bit(HCI_CONN_DROP, &conn->flags)) {
1581 BT_DBG("Refusing to create new hci_chan");
1582 return NULL;
1583 }
1584
27f70f3e 1585 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
73d80deb
LAD
1586 if (!chan)
1587 return NULL;
1588
6c388d32 1589 chan->conn = hci_conn_get(conn);
73d80deb 1590 skb_queue_head_init(&chan->data_q);
168df8e5 1591 chan->state = BT_CONNECTED;
73d80deb 1592
8192edef 1593 list_add_rcu(&chan->list, &conn->chan_list);
73d80deb
LAD
1594
1595 return chan;
1596}
1597
9472007c 1598void hci_chan_del(struct hci_chan *chan)
73d80deb
LAD
1599{
1600 struct hci_conn *conn = chan->conn;
1601 struct hci_dev *hdev = conn->hdev;
1602
38b3fef1 1603 BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan);
73d80deb 1604
8192edef
GP
1605 list_del_rcu(&chan->list);
1606
1607 synchronize_rcu();
73d80deb 1608
bcbb655a 1609 /* Prevent new hci_chan's to be created for this hci_conn */
f94b665d 1610 set_bit(HCI_CONN_DROP, &conn->flags);
b3ff670a 1611
6c388d32 1612 hci_conn_put(conn);
e9b02748 1613
73d80deb
LAD
1614 skb_queue_purge(&chan->data_q);
1615 kfree(chan);
73d80deb
LAD
1616}
1617
2c33c06a 1618void hci_chan_list_flush(struct hci_conn *conn)
73d80deb 1619{
2a5a5ec6 1620 struct hci_chan *chan, *n;
73d80deb 1621
38b3fef1 1622 BT_DBG("hcon %p", conn);
73d80deb 1623
2a5a5ec6 1624 list_for_each_entry_safe(chan, n, &conn->chan_list, list)
73d80deb
LAD
1625 hci_chan_del(chan);
1626}
42c4e53e
AE
1627
1628static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon,
1629 __u16 handle)
1630{
1631 struct hci_chan *hchan;
1632
1633 list_for_each_entry(hchan, &hcon->chan_list, list) {
1634 if (hchan->handle == handle)
1635 return hchan;
1636 }
1637
1638 return NULL;
1639}
1640
1641struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle)
1642{
1643 struct hci_conn_hash *h = &hdev->conn_hash;
1644 struct hci_conn *hcon;
1645 struct hci_chan *hchan = NULL;
1646
1647 rcu_read_lock();
1648
1649 list_for_each_entry_rcu(hcon, &h->list, list) {
1650 hchan = __hci_chan_lookup_handle(hcon, handle);
1651 if (hchan)
1652 break;
1653 }
1654
1655 rcu_read_unlock();
1656
1657 return hchan;
1658}