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