Merge tag 'net-6.16-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux-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.
02171da6 4 Copyright 2023-2024 NXP
1da177e4
LT
5
6 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation;
11
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
8e87d142
YH
16 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1da177e4
LT
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
8e87d142
YH
21 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
1da177e4
LT
23 SOFTWARE IS DISCLAIMED.
24*/
25
26/* Bluetooth HCI connection handling. */
27
8c520a59 28#include <linux/export.h>
23b9ceb7 29#include <linux/debugfs.h>
134f4b39 30#include <linux/errqueue.h>
1da177e4
LT
31
32#include <net/bluetooth/bluetooth.h>
33#include <net/bluetooth/hci_core.h>
4bc58f51 34#include <net/bluetooth/l2cap.h>
eca0ae4a
LAD
35#include <net/bluetooth/iso.h>
36#include <net/bluetooth/mgmt.h>
1da177e4 37
ac4b7236 38#include "smp.h"
eca0ae4a 39#include "eir.h"
7024728e 40
2dea632f
FD
41struct sco_param {
42 u16 pkt_type;
43 u16 max_latency;
c7da5797 44 u8 retrans_effort;
2dea632f
FD
45};
46
e07a06b4
BG
47struct conn_handle_t {
48 struct hci_conn *conn;
49 __u16 handle;
50};
51
48e68ff5 52static const struct sco_param esco_param_cvsd[] = {
c7da5797
JH
53 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */
54 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */
55 { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */
56 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */
57 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */
2dea632f
FD
58};
59
48e68ff5 60static const struct sco_param sco_param_cvsd[] = {
c7da5797
JH
61 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */
62 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */
48e68ff5
BT
63};
64
565766b0 65static const struct sco_param esco_param_msbc[] = {
c7da5797
JH
66 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */
67 { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */
2dea632f
FD
68};
69
f75113a2 70/* This function requires the caller holds hdev->lock */
881559af 71void hci_connect_le_scan_cleanup(struct hci_conn *conn, u8 status)
f75113a2
JP
72{
73 struct hci_conn_params *params;
b5c2b621 74 struct hci_dev *hdev = conn->hdev;
f75113a2
JP
75 struct smp_irk *irk;
76 bdaddr_t *bdaddr;
77 u8 bdaddr_type;
78
79 bdaddr = &conn->dst;
80 bdaddr_type = conn->dst_type;
81
82 /* Check if we need to convert to identity address */
b5c2b621 83 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
f75113a2
JP
84 if (irk) {
85 bdaddr = &irk->bdaddr;
86 bdaddr_type = irk->addr_type;
87 }
88
17bc08f0
JH
89 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr,
90 bdaddr_type);
19cf60bf 91 if (!params)
f75113a2
JP
92 return;
93
19cf60bf
LAD
94 if (params->conn) {
95 hci_conn_drop(params->conn);
96 hci_conn_put(params->conn);
97 params->conn = NULL;
98 }
99
100 if (!params->explicit_connect)
101 return;
102
103 /* If the status indicates successful cancellation of
104 * the attempt (i.e. Unknown Connection Id) there's no point of
105 * notifying failure since we'll go back to keep trying to
106 * connect. The only exception is explicit connect requests
107 * where a timeout + cancel does indicate an actual failure.
108 */
109 if (status && status != HCI_ERROR_UNKNOWN_CONN_ID)
d47da6bd 110 mgmt_connect_failed(hdev, conn, status);
19cf60bf 111
f75113a2
JP
112 /* The connection attempt was doing scan for new RPA, and is
113 * in scan phase. If params are not associated with any other
114 * autoconnect action, remove them completely. If they are, just unmark
115 * them as waiting for connection, by clearing explicit_connect field.
116 */
9ad3e6ff
JH
117 params->explicit_connect = false;
118
195ef75e 119 hci_pend_le_list_del_init(params);
9ad3e6ff
JH
120
121 switch (params->auto_connect) {
122 case HCI_AUTO_CONN_EXPLICIT:
b5c2b621 123 hci_conn_params_del(hdev, bdaddr, bdaddr_type);
9ad3e6ff
JH
124 /* return instead of break to avoid duplicate scan update */
125 return;
126 case HCI_AUTO_CONN_DIRECT:
127 case HCI_AUTO_CONN_ALWAYS:
195ef75e 128 hci_pend_le_list_add(params, &hdev->pend_le_conns);
9ad3e6ff
JH
129 break;
130 case HCI_AUTO_CONN_REPORT:
195ef75e 131 hci_pend_le_list_add(params, &hdev->pend_le_reports);
9ad3e6ff
JH
132 break;
133 default:
134 break;
168b8a25 135 }
9ad3e6ff 136
5bee2fd6 137 hci_update_passive_scan(hdev);
f75113a2
JP
138}
139
b958f9a3
JH
140static void hci_conn_cleanup(struct hci_conn *conn)
141{
142 struct hci_dev *hdev = conn->hdev;
143
144 if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags))
145 hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type);
146
629f66aa
AM
147 if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
148 hci_remove_link_key(hdev, &conn->dst);
149
b958f9a3
JH
150 hci_chan_list_flush(conn);
151
152 hci_conn_hash_del(hdev, conn);
153
181a42ed
ZX
154 if (HCI_CONN_HANDLE_UNSET(conn->handle))
155 ida_free(&hdev->unset_handle_ida, conn->handle);
156
26afbd82
LAD
157 if (conn->cleanup)
158 conn->cleanup(conn);
159
1f8330ea
SN
160 if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
161 switch (conn->setting & SCO_AIRMODE_MASK) {
162 case SCO_AIRMODE_CVSD:
163 case SCO_AIRMODE_TRANSP:
164 if (hdev->notify)
165 hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO);
166 break;
167 }
168 } else {
169 if (hdev->notify)
170 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
171 }
b958f9a3 172
b958f9a3
JH
173 debugfs_remove_recursive(conn->debugfs);
174
a85fb91e 175 hci_conn_del_sysfs(conn);
b958f9a3 176
a85fb91e 177 hci_dev_put(hdev);
b958f9a3
JH
178}
179
e3b679d5 180int hci_disconnect(struct hci_conn *conn, __u8 reason)
1da177e4 181{
38b3fef1 182 BT_DBG("hcon %p", conn);
1da177e4 183
74be523c 184 /* When we are central of an established connection and it enters
839035a7
JH
185 * the disconnect timeout, then go ahead and try to read the
186 * current clock offset. Processing of the result is done
187 * within the event handling and hci_clock_offset_evt function.
188 */
88d07feb
JH
189 if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER &&
190 (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) {
839035a7 191 struct hci_dev *hdev = conn->hdev;
4f639ede 192 struct hci_cp_read_clock_offset clkoff_cp;
839035a7 193
4f639ede
FF
194 clkoff_cp.handle = cpu_to_le16(conn->handle);
195 hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp),
196 &clkoff_cp);
839035a7
JH
197 }
198
88d07feb 199 return hci_abort_conn(conn, reason);
1da177e4
LT
200}
201
57f5d0d1 202static void hci_add_sco(struct hci_conn *conn, __u16 handle)
1da177e4
LT
203{
204 struct hci_dev *hdev = conn->hdev;
205 struct hci_cp_add_sco cp;
206
38b3fef1 207 BT_DBG("hcon %p", conn);
1da177e4
LT
208
209 conn->state = BT_CONNECT;
a0c808b3 210 conn->out = true;
1da177e4 211
efc7688b
MH
212 conn->attempt++;
213
aca3192c 214 cp.handle = cpu_to_le16(handle);
a8746417 215 cp.pkt_type = cpu_to_le16(conn->pkt_type);
1da177e4 216
a9de9248 217 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
1da177e4
LT
218}
219
8b1c324c
YL
220static bool find_next_esco_param(struct hci_conn *conn,
221 const struct sco_param *esco_param, int size)
222{
06149746
LAD
223 if (!conn->parent)
224 return false;
225
8b1c324c 226 for (; conn->attempt <= size; conn->attempt++) {
06149746 227 if (lmp_esco_2m_capable(conn->parent) ||
8b1c324c
YL
228 (esco_param[conn->attempt - 1].pkt_type & ESCO_2EV3))
229 break;
230 BT_DBG("hcon %p skipped attempt %d, eSCO 2M not supported",
231 conn, conn->attempt);
232 }
233
234 return conn->attempt <= size;
235}
236
e07a06b4 237static int configure_datapath_sync(struct hci_dev *hdev, struct bt_codec *codec)
b2af264a 238{
e07a06b4
BG
239 int err;
240 __u8 vnd_len, *vnd_data = NULL;
241 struct hci_op_configure_data_path *cmd = NULL;
242
d68d8a7a
ZH
243 /* Do not take below 2 checks as error since the 1st means user do not
244 * want to use HFP offload mode and the 2nd means the vendor controller
245 * do not need to send below HCI command for offload mode.
246 */
132d0fd0
ZH
247 if (!codec->data_path || !hdev->get_codec_config_data)
248 return 0;
249
e07a06b4
BG
250 err = hdev->get_codec_config_data(hdev, ESCO_LINK, codec, &vnd_len,
251 &vnd_data);
252 if (err < 0)
253 goto error;
254
255 cmd = kzalloc(sizeof(*cmd) + vnd_len, GFP_KERNEL);
256 if (!cmd) {
257 err = -ENOMEM;
258 goto error;
259 }
260
261 err = hdev->get_data_path_id(hdev, &cmd->data_path_id);
262 if (err < 0)
263 goto error;
264
265 cmd->vnd_len = vnd_len;
266 memcpy(cmd->vnd_data, vnd_data, vnd_len);
267
268 cmd->direction = 0x00;
269 __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH,
270 sizeof(*cmd) + vnd_len, cmd, HCI_CMD_TIMEOUT);
271
272 cmd->direction = 0x01;
273 err = __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH,
274 sizeof(*cmd) + vnd_len, cmd,
275 HCI_CMD_TIMEOUT);
276error:
277
278 kfree(cmd);
279 kfree(vnd_data);
280 return err;
281}
282
283static int hci_enhanced_setup_sync(struct hci_dev *hdev, void *data)
284{
285 struct conn_handle_t *conn_handle = data;
286 struct hci_conn *conn = conn_handle->conn;
287 __u16 handle = conn_handle->handle;
b2af264a
K
288 struct hci_cp_enhanced_setup_sync_conn cp;
289 const struct sco_param *param;
290
e07a06b4
BG
291 kfree(conn_handle);
292
18fd04ad
LAD
293 if (!hci_conn_valid(hdev, conn))
294 return -ECANCELED;
295
b2af264a
K
296 bt_dev_dbg(hdev, "hcon %p", conn);
297
132d0fd0 298 configure_datapath_sync(hdev, &conn->codec);
9798fbde 299
b2af264a
K
300 conn->state = BT_CONNECT;
301 conn->out = true;
302
303 conn->attempt++;
304
305 memset(&cp, 0x00, sizeof(cp));
306
307 cp.handle = cpu_to_le16(handle);
308
309 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
310 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
311
312 switch (conn->codec.id) {
904c139a
K
313 case BT_CODEC_MSBC:
314 if (!find_next_esco_param(conn, esco_param_msbc,
315 ARRAY_SIZE(esco_param_msbc)))
e07a06b4 316 return -EINVAL;
904c139a
K
317
318 param = &esco_param_msbc[conn->attempt - 1];
319 cp.tx_coding_format.id = 0x05;
320 cp.rx_coding_format.id = 0x05;
321 cp.tx_codec_frame_size = __cpu_to_le16(60);
322 cp.rx_codec_frame_size = __cpu_to_le16(60);
323 cp.in_bandwidth = __cpu_to_le32(32000);
324 cp.out_bandwidth = __cpu_to_le32(32000);
325 cp.in_coding_format.id = 0x04;
326 cp.out_coding_format.id = 0x04;
327 cp.in_coded_data_size = __cpu_to_le16(16);
328 cp.out_coded_data_size = __cpu_to_le16(16);
329 cp.in_pcm_data_format = 2;
330 cp.out_pcm_data_format = 2;
331 cp.in_pcm_sample_payload_msb_pos = 0;
332 cp.out_pcm_sample_payload_msb_pos = 0;
333 cp.in_data_path = conn->codec.data_path;
334 cp.out_data_path = conn->codec.data_path;
335 cp.in_transport_unit_size = 1;
336 cp.out_transport_unit_size = 1;
337 break;
338
b2af264a
K
339 case BT_CODEC_TRANSPARENT:
340 if (!find_next_esco_param(conn, esco_param_msbc,
341 ARRAY_SIZE(esco_param_msbc)))
342 return false;
343 param = &esco_param_msbc[conn->attempt - 1];
344 cp.tx_coding_format.id = 0x03;
345 cp.rx_coding_format.id = 0x03;
346 cp.tx_codec_frame_size = __cpu_to_le16(60);
347 cp.rx_codec_frame_size = __cpu_to_le16(60);
348 cp.in_bandwidth = __cpu_to_le32(0x1f40);
349 cp.out_bandwidth = __cpu_to_le32(0x1f40);
350 cp.in_coding_format.id = 0x03;
351 cp.out_coding_format.id = 0x03;
352 cp.in_coded_data_size = __cpu_to_le16(16);
353 cp.out_coded_data_size = __cpu_to_le16(16);
354 cp.in_pcm_data_format = 2;
355 cp.out_pcm_data_format = 2;
356 cp.in_pcm_sample_payload_msb_pos = 0;
357 cp.out_pcm_sample_payload_msb_pos = 0;
358 cp.in_data_path = conn->codec.data_path;
359 cp.out_data_path = conn->codec.data_path;
360 cp.in_transport_unit_size = 1;
361 cp.out_transport_unit_size = 1;
362 break;
363
364 case BT_CODEC_CVSD:
06149746 365 if (conn->parent && lmp_esco_capable(conn->parent)) {
b2af264a
K
366 if (!find_next_esco_param(conn, esco_param_cvsd,
367 ARRAY_SIZE(esco_param_cvsd)))
e07a06b4 368 return -EINVAL;
b2af264a
K
369 param = &esco_param_cvsd[conn->attempt - 1];
370 } else {
371 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
e07a06b4 372 return -EINVAL;
b2af264a
K
373 param = &sco_param_cvsd[conn->attempt - 1];
374 }
375 cp.tx_coding_format.id = 2;
376 cp.rx_coding_format.id = 2;
377 cp.tx_codec_frame_size = __cpu_to_le16(60);
378 cp.rx_codec_frame_size = __cpu_to_le16(60);
379 cp.in_bandwidth = __cpu_to_le32(16000);
380 cp.out_bandwidth = __cpu_to_le32(16000);
381 cp.in_coding_format.id = 4;
382 cp.out_coding_format.id = 4;
383 cp.in_coded_data_size = __cpu_to_le16(16);
384 cp.out_coded_data_size = __cpu_to_le16(16);
385 cp.in_pcm_data_format = 2;
386 cp.out_pcm_data_format = 2;
387 cp.in_pcm_sample_payload_msb_pos = 0;
388 cp.out_pcm_sample_payload_msb_pos = 0;
389 cp.in_data_path = conn->codec.data_path;
390 cp.out_data_path = conn->codec.data_path;
391 cp.in_transport_unit_size = 16;
392 cp.out_transport_unit_size = 16;
393 break;
394 default:
e07a06b4 395 return -EINVAL;
b2af264a
K
396 }
397
398 cp.retrans_effort = param->retrans_effort;
399 cp.pkt_type = __cpu_to_le16(param->pkt_type);
400 cp.max_latency = __cpu_to_le16(param->max_latency);
401
402 if (hci_send_cmd(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
e07a06b4 403 return -EIO;
b2af264a 404
e07a06b4 405 return 0;
b2af264a
K
406}
407
408static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle)
b6a0dc82
MH
409{
410 struct hci_dev *hdev = conn->hdev;
411 struct hci_cp_setup_sync_conn cp;
2dea632f 412 const struct sco_param *param;
b6a0dc82 413
b2af264a 414 bt_dev_dbg(hdev, "hcon %p", conn);
b6a0dc82
MH
415
416 conn->state = BT_CONNECT;
a0c808b3 417 conn->out = true;
b6a0dc82 418
efc7688b
MH
419 conn->attempt++;
420
b6a0dc82 421 cp.handle = cpu_to_le16(handle);
b6a0dc82 422
dcf4adbf
JP
423 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
424 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
10c62ddc
FD
425 cp.voice_setting = cpu_to_le16(conn->setting);
426
427 switch (conn->setting & SCO_AIRMODE_MASK) {
428 case SCO_AIRMODE_TRANSP:
8b1c324c
YL
429 if (!find_next_esco_param(conn, esco_param_msbc,
430 ARRAY_SIZE(esco_param_msbc)))
2dea632f 431 return false;
565766b0 432 param = &esco_param_msbc[conn->attempt - 1];
10c62ddc
FD
433 break;
434 case SCO_AIRMODE_CVSD:
06149746 435 if (conn->parent && lmp_esco_capable(conn->parent)) {
8b1c324c
YL
436 if (!find_next_esco_param(conn, esco_param_cvsd,
437 ARRAY_SIZE(esco_param_cvsd)))
48e68ff5 438 return false;
48e68ff5
BT
439 param = &esco_param_cvsd[conn->attempt - 1];
440 } else {
441 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
442 return false;
48e68ff5
BT
443 param = &sco_param_cvsd[conn->attempt - 1];
444 }
10c62ddc 445 break;
2dea632f
FD
446 default:
447 return false;
10c62ddc 448 }
b6a0dc82 449
c7da5797 450 cp.retrans_effort = param->retrans_effort;
2dea632f
FD
451 cp.pkt_type = __cpu_to_le16(param->pkt_type);
452 cp.max_latency = __cpu_to_le16(param->max_latency);
453
454 if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
455 return false;
456
457 return true;
b6a0dc82
MH
458}
459
b2af264a
K
460bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
461{
e07a06b4
BG
462 int result;
463 struct conn_handle_t *conn_handle;
464
465 if (enhanced_sync_conn_capable(conn->hdev)) {
466 conn_handle = kzalloc(sizeof(*conn_handle), GFP_KERNEL);
467
468 if (!conn_handle)
469 return false;
470
471 conn_handle->conn = conn;
472 conn_handle->handle = handle;
473 result = hci_cmd_sync_queue(conn->hdev, hci_enhanced_setup_sync,
474 conn_handle, NULL);
475 if (result < 0)
476 kfree(conn_handle);
477
478 return result == 0;
479 }
b2af264a
K
480
481 return hci_setup_sync_conn(conn, handle);
482}
483
7d6ca693
JH
484u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
485 u16 to_multiplier)
2ce603eb 486{
2ce603eb 487 struct hci_dev *hdev = conn->hdev;
f044eb05
MH
488 struct hci_conn_params *params;
489 struct hci_cp_le_conn_update cp;
2ce603eb 490
f044eb05 491 hci_dev_lock(hdev);
2ce603eb 492
f044eb05
MH
493 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
494 if (params) {
495 params->conn_min_interval = min;
496 params->conn_max_interval = max;
497 params->conn_latency = latency;
498 params->supervision_timeout = to_multiplier;
499 }
500
501 hci_dev_unlock(hdev);
502
503 memset(&cp, 0, sizeof(cp));
2ce603eb
CT
504 cp.handle = cpu_to_le16(conn->handle);
505 cp.conn_interval_min = cpu_to_le16(min);
506 cp.conn_interval_max = cpu_to_le16(max);
507 cp.conn_latency = cpu_to_le16(latency);
508 cp.supervision_timeout = cpu_to_le16(to_multiplier);
dcf4adbf
JP
509 cp.min_ce_len = cpu_to_le16(0x0000);
510 cp.max_ce_len = cpu_to_le16(0x0000);
2ce603eb
CT
511
512 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
7d6ca693
JH
513
514 if (params)
515 return 0x01;
516
517 return 0x00;
2ce603eb 518}
2ce603eb 519
fe39c7b2 520void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
8b76ce34 521 __u8 ltk[16], __u8 key_size)
a7a595f6
VCG
522{
523 struct hci_dev *hdev = conn->hdev;
524 struct hci_cp_le_start_enc cp;
525
38b3fef1 526 BT_DBG("hcon %p", conn);
a7a595f6
VCG
527
528 memset(&cp, 0, sizeof(cp));
529
530 cp.handle = cpu_to_le16(conn->handle);
fe39c7b2 531 cp.rand = rand;
a7a595f6 532 cp.ediv = ediv;
8b76ce34 533 memcpy(cp.ltk, ltk, key_size);
a7a595f6
VCG
534
535 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
536}
a7a595f6 537
e73439d8
MH
538/* Device _must_ be locked */
539void hci_sco_setup(struct hci_conn *conn, __u8 status)
540{
06149746 541 struct hci_link *link;
e73439d8 542
06149746
LAD
543 link = list_first_entry_or_null(&conn->link_list, struct hci_link, list);
544 if (!link || !link->conn)
e73439d8
MH
545 return;
546
38b3fef1
AE
547 BT_DBG("hcon %p", conn);
548
e73439d8
MH
549 if (!status) {
550 if (lmp_esco_capable(conn->hdev))
06149746 551 hci_setup_sync(link->conn, conn->handle);
e73439d8 552 else
06149746 553 hci_add_sco(link->conn, conn->handle);
e73439d8 554 } else {
06149746
LAD
555 hci_connect_cfm(link->conn, status);
556 hci_conn_del(link->conn);
e73439d8
MH
557 }
558}
559
19c40e3b 560static void hci_conn_timeout(struct work_struct *work)
1da177e4 561{
19c40e3b 562 struct hci_conn *conn = container_of(work, struct hci_conn,
5974e4c4 563 disc_work.work);
1d56dc4f 564 int refcnt = atomic_read(&conn->refcnt);
1da177e4 565
38b3fef1 566 BT_DBG("hcon %p state %s", conn, state_to_string(conn->state));
1da177e4 567
1d56dc4f
LR
568 WARN_ON(refcnt < 0);
569
570 /* FIXME: It was observed that in pairing failed scenario, refcnt
571 * drops below 0. Probably this is because l2cap_conn_del calls
572 * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is
573 * dropped. After that loop hci_chan_del is called which also drops
574 * conn. For now make sure that ACL is alive if refcnt is higher then 0,
575 * otherwise drop it.
576 */
577 if (refcnt > 0)
1da177e4
LT
578 return;
579
89e0ccc8 580 hci_abort_conn(conn, hci_proto_disconn_ind(conn));
1da177e4
LT
581}
582
416dc94b 583/* Enter sniff mode */
a74a84f6 584static void hci_conn_idle(struct work_struct *work)
416dc94b 585{
a74a84f6
JH
586 struct hci_conn *conn = container_of(work, struct hci_conn,
587 idle_work.work);
416dc94b
GP
588 struct hci_dev *hdev = conn->hdev;
589
38b3fef1 590 BT_DBG("hcon %p mode %d", conn, conn->mode);
416dc94b 591
416dc94b
GP
592 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
593 return;
594
595 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
596 return;
597
598 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
599 struct hci_cp_sniff_subrate cp;
600 cp.handle = cpu_to_le16(conn->handle);
dcf4adbf
JP
601 cp.max_latency = cpu_to_le16(0);
602 cp.min_remote_timeout = cpu_to_le16(0);
603 cp.min_local_timeout = cpu_to_le16(0);
416dc94b
GP
604 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
605 }
606
51a8efd7 607 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
416dc94b
GP
608 struct hci_cp_sniff_mode cp;
609 cp.handle = cpu_to_le16(conn->handle);
610 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
611 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
dcf4adbf
JP
612 cp.attempt = cpu_to_le16(4);
613 cp.timeout = cpu_to_le16(1);
416dc94b
GP
614 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
615 }
616}
617
7bc18d9d 618static void hci_conn_auto_accept(struct work_struct *work)
9f61656a 619{
7bc18d9d
JH
620 struct hci_conn *conn = container_of(work, struct hci_conn,
621 auto_accept_work.work);
9f61656a 622
7bc18d9d 623 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
5974e4c4 624 &conn->dst);
9f61656a
JH
625}
626
c3bed4de
SN
627static void le_disable_advertising(struct hci_dev *hdev)
628{
629 if (ext_adv_capable(hdev)) {
630 struct hci_cp_le_set_ext_adv_enable cp;
631
632 cp.enable = 0x00;
633 cp.num_of_sets = 0x00;
634
635 hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp),
636 &cp);
637 } else {
638 u8 enable = 0x00;
639 hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
640 &enable);
641 }
642}
643
9489eca4
JH
644static void le_conn_timeout(struct work_struct *work)
645{
646 struct hci_conn *conn = container_of(work, struct hci_conn,
647 le_conn_timeout.work);
3c857757 648 struct hci_dev *hdev = conn->hdev;
9489eca4
JH
649
650 BT_DBG("");
651
3c857757
JH
652 /* We could end up here due to having done directed advertising,
653 * so clean up the state if necessary. This should however only
654 * happen with broken hardware or if low duty cycle was used
655 * (which doesn't have a timeout of its own).
656 */
0b1db38c 657 if (conn->role == HCI_ROLE_SLAVE) {
c3bed4de
SN
658 /* Disable LE Advertising */
659 le_disable_advertising(hdev);
9fa6b4cd 660 hci_dev_lock(hdev);
9b3628d7 661 hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
9fa6b4cd 662 hci_dev_unlock(hdev);
3c857757
JH
663 return;
664 }
665
89e0ccc8 666 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
9489eca4
JH
667}
668
eca0ae4a
LAD
669struct iso_list_data {
670 union {
671 u8 cig;
672 u8 big;
673 };
674 union {
675 u8 cis;
676 u8 bis;
677 u16 sync_handle;
678 };
679 int count;
a0bfde16 680 bool big_term;
fbdc4bc4 681 bool pa_sync_term;
f777d882 682 bool big_sync_term;
eca0ae4a
LAD
683};
684
685static void bis_list(struct hci_conn *conn, void *data)
686{
687 struct iso_list_data *d = data;
688
689 /* Skip if not broadcast/ANY address */
690 if (bacmp(&conn->dst, BDADDR_ANY))
691 return;
692
0fe8c8d0
IT
693 if (d->big != conn->iso_qos.bcast.big || d->bis == BT_ISO_QOS_BIS_UNSET ||
694 d->bis != conn->iso_qos.bcast.bis)
eca0ae4a
LAD
695 return;
696
697 d->count++;
698}
699
eca0ae4a
LAD
700static int terminate_big_sync(struct hci_dev *hdev, void *data)
701{
702 struct iso_list_data *d = data;
703
704 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", d->big, d->bis);
705
a254b90c 706 hci_disable_per_advertising_sync(hdev, d->bis);
eca0ae4a
LAD
707 hci_remove_ext_adv_instance_sync(hdev, d->bis, NULL);
708
a0bfde16
IT
709 /* Only terminate BIG if it has been created */
710 if (!d->big_term)
eca0ae4a
LAD
711 return 0;
712
713 return hci_le_terminate_big_sync(hdev, d->big,
714 HCI_ERROR_LOCAL_HOST_TERM);
715}
716
717static void terminate_big_destroy(struct hci_dev *hdev, void *data, int err)
718{
719 kfree(data);
720}
721
a0bfde16 722static int hci_le_terminate_big(struct hci_dev *hdev, struct hci_conn *conn)
eca0ae4a
LAD
723{
724 struct iso_list_data *d;
3aa21311 725 int ret;
eca0ae4a 726
a0bfde16
IT
727 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", conn->iso_qos.bcast.big,
728 conn->iso_qos.bcast.bis);
eca0ae4a 729
3958e877 730 d = kzalloc(sizeof(*d), GFP_KERNEL);
eca0ae4a
LAD
731 if (!d)
732 return -ENOMEM;
733
a0bfde16
IT
734 d->big = conn->iso_qos.bcast.big;
735 d->bis = conn->iso_qos.bcast.bis;
736 d->big_term = test_and_clear_bit(HCI_CONN_BIG_CREATED, &conn->flags);
eca0ae4a 737
3aa21311
ZS
738 ret = hci_cmd_sync_queue(hdev, terminate_big_sync, d,
739 terminate_big_destroy);
740 if (ret)
741 kfree(d);
742
743 return ret;
eca0ae4a
LAD
744}
745
746static int big_terminate_sync(struct hci_dev *hdev, void *data)
747{
748 struct iso_list_data *d = data;
749
750 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", d->big,
751 d->sync_handle);
752
f777d882
IT
753 if (d->big_sync_term)
754 hci_le_big_terminate_sync(hdev, d->big);
eca0ae4a 755
fbdc4bc4
IT
756 if (d->pa_sync_term)
757 return hci_le_pa_terminate_sync(hdev, d->sync_handle);
758
759 return 0;
eca0ae4a
LAD
760}
761
fcb89f12
IT
762static void find_bis(struct hci_conn *conn, void *data)
763{
764 struct iso_list_data *d = data;
765
766 /* Ignore if BIG doesn't match */
767 if (d->big != conn->iso_qos.bcast.big)
768 return;
769
770 d->count++;
771}
772
f777d882 773static int hci_le_big_terminate(struct hci_dev *hdev, u8 big, struct hci_conn *conn)
eca0ae4a
LAD
774{
775 struct iso_list_data *d;
3aa21311 776 int ret;
eca0ae4a 777
f777d882 778 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", big, conn->sync_handle);
eca0ae4a 779
3958e877 780 d = kzalloc(sizeof(*d), GFP_KERNEL);
eca0ae4a
LAD
781 if (!d)
782 return -ENOMEM;
783
eca0ae4a 784 d->big = big;
f777d882 785 d->sync_handle = conn->sync_handle;
fcb89f12
IT
786
787 if (test_and_clear_bit(HCI_CONN_PA_SYNC, &conn->flags)) {
23205562 788 hci_conn_hash_list_flag(hdev, find_bis, BIS_LINK,
fcb89f12
IT
789 HCI_CONN_PA_SYNC, d);
790
791 if (!d->count)
792 d->pa_sync_term = true;
793
794 d->count = 0;
795 }
796
797 if (test_and_clear_bit(HCI_CONN_BIG_SYNC, &conn->flags)) {
23205562 798 hci_conn_hash_list_flag(hdev, find_bis, BIS_LINK,
fcb89f12
IT
799 HCI_CONN_BIG_SYNC, d);
800
801 if (!d->count)
802 d->big_sync_term = true;
803 }
eca0ae4a 804
3aa21311
ZS
805 ret = hci_cmd_sync_queue(hdev, big_terminate_sync, d,
806 terminate_big_destroy);
807 if (ret)
808 kfree(d);
809
810 return ret;
eca0ae4a
LAD
811}
812
813/* Cleanup BIS connection
814 *
815 * Detects if there any BIS left connected in a BIG
816 * broadcaster: Remove advertising instance and terminate BIG.
817 * broadcaster receiver: Teminate BIG sync and terminate PA sync.
818 */
819static void bis_cleanup(struct hci_conn *conn)
820{
821 struct hci_dev *hdev = conn->hdev;
a0bfde16 822 struct hci_conn *bis;
eca0ae4a
LAD
823
824 bt_dev_dbg(hdev, "conn %p", conn);
825
826 if (conn->role == HCI_ROLE_MASTER) {
827 if (!test_and_clear_bit(HCI_CONN_PER_ADV, &conn->flags))
828 return;
829
a0bfde16
IT
830 /* Check if ISO connection is a BIS and terminate advertising
831 * set and BIG if there are no other connections using it.
832 */
6a42e9bf 833 bis = hci_conn_hash_lookup_big(hdev, conn->iso_qos.bcast.big);
a0bfde16
IT
834 if (bis)
835 return;
836
837 hci_le_terminate_big(hdev, conn);
eca0ae4a 838 } else {
0fe8c8d0 839 hci_le_big_terminate(hdev, conn->iso_qos.bcast.big,
f777d882 840 conn);
eca0ae4a
LAD
841 }
842}
843
844static int remove_cig_sync(struct hci_dev *hdev, void *data)
845{
a1f6c3ae 846 u8 handle = PTR_UINT(data);
eca0ae4a
LAD
847
848 return hci_le_remove_cig_sync(hdev, handle);
849}
850
851static int hci_le_remove_cig(struct hci_dev *hdev, u8 handle)
852{
853 bt_dev_dbg(hdev, "handle 0x%2.2x", handle);
854
a1f6c3ae
LAD
855 return hci_cmd_sync_queue(hdev, remove_cig_sync, UINT_PTR(handle),
856 NULL);
eca0ae4a
LAD
857}
858
859static void find_cis(struct hci_conn *conn, void *data)
860{
861 struct iso_list_data *d = data;
862
31c5f916
PV
863 /* Ignore broadcast or if CIG don't match */
864 if (!bacmp(&conn->dst, BDADDR_ANY) || d->cig != conn->iso_qos.ucast.cig)
eca0ae4a
LAD
865 return;
866
867 d->count++;
868}
869
870/* Cleanup CIS connection:
871 *
872 * Detects if there any CIS left connected in a CIG and remove it.
873 */
874static void cis_cleanup(struct hci_conn *conn)
875{
876 struct hci_dev *hdev = conn->hdev;
877 struct iso_list_data d;
878
31c5f916
PV
879 if (conn->iso_qos.ucast.cig == BT_ISO_QOS_CIG_UNSET)
880 return;
881
eca0ae4a 882 memset(&d, 0, sizeof(d));
0fe8c8d0 883 d.cig = conn->iso_qos.ucast.cig;
eca0ae4a
LAD
884
885 /* Check if ISO connection is a CIS and remove CIG if there are
886 * no other connections using it.
887 */
23205562
PV
888 hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_BOUND, &d);
889 hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_CONNECT,
890 &d);
891 hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_CONNECTED,
892 &d);
eca0ae4a
LAD
893 if (d.count)
894 return;
895
0fe8c8d0 896 hci_le_remove_cig(hdev, conn->iso_qos.ucast.cig);
eca0ae4a
LAD
897}
898
181a42ed 899static int hci_conn_hash_alloc_unset(struct hci_dev *hdev)
9f78191c 900{
181a42ed
ZX
901 return ida_alloc_range(&hdev->unset_handle_ida, HCI_CONN_HANDLE_MAX + 1,
902 U16_MAX, GFP_ATOMIC);
9f78191c
LAD
903}
904
1cc18c2a
PS
905static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
906 u8 role, u16 handle)
1da177e4
LT
907{
908 struct hci_conn *conn;
909
a5b862c6
SK
910 switch (type) {
911 case ACL_LINK:
912 if (!hdev->acl_mtu)
913 return ERR_PTR(-ECONNREFUSED);
914 break;
23205562
PV
915 case CIS_LINK:
916 case BIS_LINK:
a5b862c6
SK
917 if (hdev->iso_mtu)
918 /* Dedicated ISO Buffer exists */
919 break;
920 fallthrough;
921 case LE_LINK:
922 if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
923 return ERR_PTR(-ECONNREFUSED);
924 if (!hdev->le_mtu && hdev->acl_mtu < HCI_MIN_LE_MTU)
925 return ERR_PTR(-ECONNREFUSED);
926 break;
927 case SCO_LINK:
928 case ESCO_LINK:
929 if (!hdev->sco_pkts)
930 /* Controller does not support SCO or eSCO over HCI */
931 return ERR_PTR(-ECONNREFUSED);
932 break;
933 default:
934 return ERR_PTR(-ECONNREFUSED);
935 }
936
181a42ed 937 bt_dev_dbg(hdev, "dst %pMR handle 0x%4.4x", dst, handle);
1da177e4 938
27f70f3e 939 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
04837f64 940 if (!conn)
a5b862c6 941 return ERR_PTR(-ENOMEM);
1da177e4
LT
942
943 bacpy(&conn->dst, dst);
662e8820 944 bacpy(&conn->src, &hdev->bdaddr);
181a42ed 945 conn->handle = handle;
a8746417
MH
946 conn->hdev = hdev;
947 conn->type = type;
a5c4e309 948 conn->role = role;
a8746417
MH
949 conn->mode = HCI_CM_ACTIVE;
950 conn->state = BT_OPEN;
93f19c9f 951 conn->auth_type = HCI_AT_GENERAL_BONDING;
17fa4b9d 952 conn->io_capability = hdev->io_capability;
a9583556 953 conn->remote_auth = 0xff;
13d39315 954 conn->key_type = 0xff;
ebf86aa3 955 conn->rssi = HCI_RSSI_INVALID;
5a134fae 956 conn->tx_power = HCI_TX_POWER_INVALID;
d0455ed9 957 conn->max_tx_power = HCI_TX_POWER_INVALID;
1d11d70d 958 conn->sync_handle = HCI_SYNC_HANDLE_INVALID;
4a5e0ba6 959 conn->sid = HCI_SID_INVALID;
1da177e4 960
58a681ef 961 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
052b30b0 962 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
04837f64 963
302975cb
SRK
964 /* Set Default Authenticated payload timeout to 30s */
965 conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT;
966
a5c4e309
JH
967 if (conn->role == HCI_ROLE_MASTER)
968 conn->out = true;
969
a8746417
MH
970 switch (type) {
971 case ACL_LINK:
972 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
a5b862c6 973 conn->mtu = hdev->acl_mtu;
a8746417 974 break;
9c84d1da 975 case LE_LINK:
eca0ae4a
LAD
976 /* conn->src should reflect the local identity address */
977 hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
a5b862c6 978 conn->mtu = hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu;
eca0ae4a 979 break;
23205562
PV
980 case CIS_LINK:
981 case BIS_LINK:
9c84d1da
JH
982 /* conn->src should reflect the local identity address */
983 hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
eca0ae4a
LAD
984
985 /* set proper cleanup function */
986 if (!bacmp(dst, BDADDR_ANY))
987 conn->cleanup = bis_cleanup;
988 else if (conn->role == HCI_ROLE_MASTER)
989 conn->cleanup = cis_cleanup;
990
a5b862c6
SK
991 conn->mtu = hdev->iso_mtu ? hdev->iso_mtu :
992 hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu;
9c84d1da 993 break;
a8746417
MH
994 case SCO_LINK:
995 if (lmp_esco_capable(hdev))
efc7688b
MH
996 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
997 (hdev->esco_type & EDR_ESCO_MASK);
a8746417
MH
998 else
999 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
a5b862c6
SK
1000
1001 conn->mtu = hdev->sco_mtu;
a8746417
MH
1002 break;
1003 case ESCO_LINK:
efc7688b 1004 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
a5b862c6 1005 conn->mtu = hdev->sco_mtu;
a8746417
MH
1006 break;
1007 }
1008
1da177e4 1009 skb_queue_head_init(&conn->data_q);
134f4b39 1010 skb_queue_head_init(&conn->tx_q.queue);
04837f64 1011
70c1f20b 1012 INIT_LIST_HEAD(&conn->chan_list);
06149746 1013 INIT_LIST_HEAD(&conn->link_list);
73d80deb 1014
19c40e3b 1015 INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
7bc18d9d 1016 INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
a74a84f6 1017 INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
9489eca4 1018 INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
1da177e4
LT
1019
1020 atomic_set(&conn->refcnt, 0);
1021
1022 hci_dev_hold(hdev);
1023
1da177e4 1024 hci_conn_hash_add(hdev, conn);
1f8330ea
SN
1025
1026 /* The SCO and eSCO connections will only be notified when their
1027 * setup has been completed. This is different to ACL links which
1028 * can be notified right away.
1029 */
1030 if (conn->type != SCO_LINK && conn->type != ESCO_LINK) {
1031 if (hdev->notify)
1032 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
1033 }
1da177e4 1034
a67e899c
MH
1035 hci_conn_init_sysfs(conn);
1036
1da177e4
LT
1037 return conn;
1038}
1039
181a42ed
ZX
1040struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type,
1041 bdaddr_t *dst, u8 role)
1042{
1043 int handle;
1044
1045 bt_dev_dbg(hdev, "dst %pMR", dst);
1046
1047 handle = hci_conn_hash_alloc_unset(hdev);
1048 if (unlikely(handle < 0))
a5b862c6 1049 return ERR_PTR(-ECONNREFUSED);
181a42ed 1050
1cc18c2a
PS
1051 return __hci_conn_add(hdev, type, dst, role, handle);
1052}
1053
1054struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
1055 u8 role, u16 handle)
1056{
1057 if (handle > HCI_CONN_HANDLE_MAX)
1058 return ERR_PTR(-EINVAL);
1059
1060 return __hci_conn_add(hdev, type, dst, role, handle);
181a42ed
ZX
1061}
1062
3344d318
PV
1063static void hci_conn_cleanup_child(struct hci_conn *conn, u8 reason)
1064{
1065 if (!reason)
1066 reason = HCI_ERROR_REMOTE_USER_TERM;
1067
1068 /* Due to race, SCO/ISO conn might be not established yet at this point,
1069 * and nothing else will clean it up. In other cases it is done via HCI
1070 * events.
1071 */
1072 switch (conn->type) {
1073 case SCO_LINK:
1074 case ESCO_LINK:
1075 if (HCI_CONN_HANDLE_UNSET(conn->handle))
1076 hci_conn_failed(conn, reason);
1077 break;
23205562
PV
1078 case CIS_LINK:
1079 case BIS_LINK:
fa224d0c
IT
1080 if ((conn->state != BT_CONNECTED &&
1081 !test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) ||
1082 test_bit(HCI_CONN_BIG_CREATED, &conn->flags))
3344d318
PV
1083 hci_conn_failed(conn, reason);
1084 break;
1085 }
1086}
1087
06149746 1088static void hci_conn_unlink(struct hci_conn *conn)
5dc7d23e 1089{
06149746
LAD
1090 struct hci_dev *hdev = conn->hdev;
1091
1092 bt_dev_dbg(hdev, "hcon %p", conn);
1093
1094 if (!conn->parent) {
1095 struct hci_link *link, *t;
1096
ca1fd42e
RL
1097 list_for_each_entry_safe(link, t, &conn->link_list, list) {
1098 struct hci_conn *child = link->conn;
1099
1100 hci_conn_unlink(child);
1101
a2ac591c
RL
1102 /* If hdev is down it means
1103 * hci_dev_close_sync/hci_conn_hash_flush is in progress
1104 * and links don't need to be cleanup as all connections
1105 * would be cleanup.
1106 */
1107 if (!test_bit(HCI_UP, &hdev->flags))
1108 continue;
1109
3344d318 1110 hci_conn_cleanup_child(child, conn->abort_reason);
ca1fd42e 1111 }
06149746
LAD
1112
1113 return;
1114 }
1115
5dc7d23e 1116 if (!conn->link)
06149746 1117 return;
5dc7d23e 1118
06149746
LAD
1119 list_del_rcu(&conn->link->list);
1120 synchronize_rcu();
1121
a2904d28 1122 hci_conn_drop(conn->parent);
2910431a
RL
1123 hci_conn_put(conn->parent);
1124 conn->parent = NULL;
1125
06149746 1126 kfree(conn->link);
5dc7d23e 1127 conn->link = NULL;
5dc7d23e
LAD
1128}
1129
a2ac591c 1130void hci_conn_del(struct hci_conn *conn)
1da177e4
LT
1131{
1132 struct hci_dev *hdev = conn->hdev;
1133
38b3fef1 1134 BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
1da177e4 1135
a2904d28
RL
1136 hci_conn_unlink(conn);
1137
2b0f2fc9
LAD
1138 disable_delayed_work_sync(&conn->disc_work);
1139 disable_delayed_work_sync(&conn->auto_accept_work);
1140 disable_delayed_work_sync(&conn->idle_work);
9f61656a 1141
5b7f9909 1142 if (conn->type == ACL_LINK) {
1da177e4
LT
1143 /* Unacked frames */
1144 hdev->acl_cnt += conn->sent;
6ed58ec5 1145 } else if (conn->type == LE_LINK) {
980ffc0a 1146 cancel_delayed_work(&conn->le_conn_timeout);
9489eca4 1147
6ed58ec5
VT
1148 if (hdev->le_pkts)
1149 hdev->le_cnt += conn->sent;
1150 else
1151 hdev->acl_cnt += conn->sent;
5b7f9909 1152 } else {
5638d9ea 1153 /* Unacked ISO frames */
23205562
PV
1154 if (conn->type == CIS_LINK ||
1155 conn->type == BIS_LINK) {
5638d9ea
LAD
1156 if (hdev->iso_pkts)
1157 hdev->iso_cnt += conn->sent;
1158 else if (hdev->le_pkts)
1159 hdev->le_cnt += conn->sent;
1160 else
1161 hdev->acl_cnt += conn->sent;
1162 }
1da177e4
LT
1163 }
1164
1da177e4 1165 skb_queue_purge(&conn->data_q);
134f4b39 1166 skb_queue_purge(&conn->tx_q.queue);
1da177e4 1167
b958f9a3
JH
1168 /* Remove the connection from the list and cleanup its remaining
1169 * state. This is a separate function since for some cases like
1170 * BT_CONNECT_SCAN we *only* want the cleanup part without the
1171 * rest of hci_conn_del.
1172 */
1173 hci_conn_cleanup(conn);
881559af
LAD
1174
1175 /* Dequeue callbacks using connection pointer as data */
1176 hci_cmd_sync_dequeue(hdev, NULL, conn, NULL);
1da177e4
LT
1177}
1178
39385cb5 1179struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type)
1da177e4
LT
1180{
1181 int use_src = bacmp(src, BDADDR_ANY);
8035ded4 1182 struct hci_dev *hdev = NULL, *d;
1da177e4 1183
6ed93dc6 1184 BT_DBG("%pMR -> %pMR", src, dst);
1da177e4 1185
f20d09d5 1186 read_lock(&hci_dev_list_lock);
1da177e4 1187
8035ded4 1188 list_for_each_entry(d, &hci_dev_list, list) {
8fc9ced3 1189 if (!test_bit(HCI_UP, &d->flags) ||
84a4bb65 1190 hci_dev_test_flag(d, HCI_USER_CHANNEL))
1da177e4
LT
1191 continue;
1192
8e87d142 1193 /* Simple routing:
1da177e4
LT
1194 * No source address - find interface with bdaddr != dst
1195 * Source address - find interface with bdaddr == src
1196 */
1197
1198 if (use_src) {
39385cb5
JH
1199 bdaddr_t id_addr;
1200 u8 id_addr_type;
1201
1202 if (src_type == BDADDR_BREDR) {
1203 if (!lmp_bredr_capable(d))
1204 continue;
1205 bacpy(&id_addr, &d->bdaddr);
1206 id_addr_type = BDADDR_BREDR;
1207 } else {
1208 if (!lmp_le_capable(d))
1209 continue;
1210
1211 hci_copy_identity_address(d, &id_addr,
1212 &id_addr_type);
1213
1214 /* Convert from HCI to three-value type */
1215 if (id_addr_type == ADDR_LE_DEV_PUBLIC)
1216 id_addr_type = BDADDR_LE_PUBLIC;
1217 else
1218 id_addr_type = BDADDR_LE_RANDOM;
1219 }
1220
1221 if (!bacmp(&id_addr, src) && id_addr_type == src_type) {
1da177e4
LT
1222 hdev = d; break;
1223 }
1224 } else {
1225 if (bacmp(&d->bdaddr, dst)) {
1226 hdev = d; break;
1227 }
1228 }
1229 }
1230
1231 if (hdev)
1232 hdev = hci_dev_hold(hdev);
1233
f20d09d5 1234 read_unlock(&hci_dev_list_lock);
1da177e4
LT
1235 return hdev;
1236}
1237EXPORT_SYMBOL(hci_get_route);
1238
9bb3c01f 1239/* This function requires the caller holds hdev->lock */
9b3628d7 1240static void hci_le_conn_failed(struct hci_conn *conn, u8 status)
9bb3c01f
AG
1241{
1242 struct hci_dev *hdev = conn->hdev;
f161dd41 1243
19cf60bf 1244 hci_connect_le_scan_cleanup(conn, status);
3c857757 1245
abfeea47 1246 /* Enable advertising in case this was a failed connection
3c857757
JH
1247 * attempt as a peripheral.
1248 */
abfeea47 1249 hci_enable_advertising(hdev);
9bb3c01f
AG
1250}
1251
9b3628d7
LAD
1252/* This function requires the caller holds hdev->lock */
1253void hci_conn_failed(struct hci_conn *conn, u8 status)
1254{
1255 struct hci_dev *hdev = conn->hdev;
1256
1257 bt_dev_dbg(hdev, "status 0x%2.2x", status);
1258
1259 switch (conn->type) {
1260 case LE_LINK:
1261 hci_le_conn_failed(conn, status);
1262 break;
1263 case ACL_LINK:
d47da6bd 1264 mgmt_connect_failed(hdev, conn, status);
9b3628d7
LAD
1265 break;
1266 }
1267
a254b90c
IT
1268 /* In case of BIG/PA sync failed, clear conn flags so that
1269 * the conns will be correctly cleaned up by ISO layer
1270 */
1271 test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags);
1272 test_and_clear_bit(HCI_CONN_PA_SYNC_FAILED, &conn->flags);
1273
9b3628d7
LAD
1274 conn->state = BT_CLOSED;
1275 hci_connect_cfm(conn, status);
1276 hci_conn_del(conn);
1277}
1278
16e3b642
LAD
1279/* This function requires the caller holds hdev->lock */
1280u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle)
1281{
1282 struct hci_dev *hdev = conn->hdev;
1283
1284 bt_dev_dbg(hdev, "hcon %p handle 0x%4.4x", conn, handle);
1285
1286 if (conn->handle == handle)
1287 return 0;
1288
1289 if (handle > HCI_CONN_HANDLE_MAX) {
1290 bt_dev_err(hdev, "Invalid handle: 0x%4.4x > 0x%4.4x",
1291 handle, HCI_CONN_HANDLE_MAX);
1292 return HCI_ERROR_INVALID_PARAMETERS;
1293 }
1294
1295 /* If abort_reason has been sent it means the connection is being
1296 * aborted and the handle shall not be changed.
1297 */
1298 if (conn->abort_reason)
1299 return conn->abort_reason;
1300
181a42ed
ZX
1301 if (HCI_CONN_HANDLE_UNSET(conn->handle))
1302 ida_free(&hdev->unset_handle_ida, conn->handle);
1303
16e3b642
LAD
1304 conn->handle = handle;
1305
1306 return 0;
1307}
1308
04a6c589 1309struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
d850bf08 1310 u8 dst_type, bool dst_resolved, u8 sec_level,
2e7ed5f5 1311 u16 conn_timeout, u8 role, u8 phy, u8 sec_phy)
1da177e4 1312{
e2caced4 1313 struct hci_conn *conn;
1ebfcc1f 1314 struct smp_irk *irk;
1d399ae5 1315 int err;
1da177e4 1316
152d386e 1317 /* Let's make sure that le is enabled.*/
d7a5a11d 1318 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
152d386e
LR
1319 if (lmp_le_capable(hdev))
1320 return ERR_PTR(-ECONNREFUSED);
1321
1322 return ERR_PTR(-EOPNOTSUPP);
1323 }
1324
658aead9
JH
1325 /* Since the controller supports only one LE connection attempt at a
1326 * time, we return -EBUSY if there is any connection attempt running.
1327 */
1328 if (hci_lookup_le_connect(hdev))
1329 return ERR_PTR(-EBUSY);
1330
e2caced4
JH
1331 /* If there's already a connection object but it's not in
1332 * scanning state it means it must already be established, in
1333 * which case we can't do anything else except report a failure
1334 * to connect.
620ad521 1335 */
9d4c1cc1 1336 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
e2caced4
JH
1337 if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) {
1338 return ERR_PTR(-EBUSY);
620ad521 1339 }
dfc94dbd 1340
d850bf08
LAD
1341 /* Check if the destination address has been resolved by the controller
1342 * since if it did then the identity address shall be used.
edb4b466 1343 */
d850bf08
LAD
1344 if (!dst_resolved) {
1345 /* When given an identity address with existing identity
1346 * resolving key, the connection needs to be established
1347 * to a resolvable random address.
1348 *
1349 * Storing the resolvable random address is required here
1350 * to handle connection failures. The address will later
1351 * be resolved back into the original identity address
1352 * from the connect request.
1353 */
1354 irk = hci_find_irk_by_addr(hdev, dst, dst_type);
1355 if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
1356 dst = &irk->rpa;
1357 dst_type = ADDR_LE_DEV_RANDOM;
1358 }
1ebfcc1f
JH
1359 }
1360
e2caced4 1361 if (conn) {
28a667c9
JP
1362 bacpy(&conn->dst, dst);
1363 } else {
181a42ed 1364 conn = hci_conn_add_unset(hdev, LE_LINK, dst, role);
a5b862c6
SK
1365 if (IS_ERR(conn))
1366 return conn;
e2caced4
JH
1367 hci_conn_hold(conn);
1368 conn->pending_sec_level = sec_level;
28a667c9
JP
1369 }
1370
1ebfcc1f 1371 conn->dst_type = dst_type;
620ad521 1372 conn->sec_level = BT_SECURITY_LOW;
09ae260b 1373 conn->conn_timeout = conn_timeout;
2e7ed5f5
LAD
1374 conn->le_adv_phy = phy;
1375 conn->le_adv_sec_phy = sec_phy;
4292f1f3 1376
881559af 1377 err = hci_connect_le_sync(hdev, conn);
2acf3d90
AG
1378 if (err) {
1379 hci_conn_del(conn);
620ad521 1380 return ERR_PTR(err);
2acf3d90 1381 }
fcd89c09 1382
f1e5d547 1383 return conn;
d04aef4c 1384}
fcd89c09 1385
f75113a2
JP
1386static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
1387{
1388 struct hci_conn *conn;
1389
9d4c1cc1 1390 conn = hci_conn_hash_lookup_le(hdev, addr, type);
f75113a2
JP
1391 if (!conn)
1392 return false;
1393
f75113a2
JP
1394 if (conn->state != BT_CONNECTED)
1395 return false;
1396
1397 return true;
1398}
1399
1400/* This function requires the caller holds hdev->lock */
84235d22 1401static int hci_explicit_conn_params_set(struct hci_dev *hdev,
f75113a2
JP
1402 bdaddr_t *addr, u8 addr_type)
1403{
f75113a2
JP
1404 struct hci_conn_params *params;
1405
1406 if (is_connected(hdev, addr, addr_type))
1407 return -EISCONN;
1408
5157b8a5
JP
1409 params = hci_conn_params_lookup(hdev, addr, addr_type);
1410 if (!params) {
1411 params = hci_conn_params_add(hdev, addr, addr_type);
1412 if (!params)
1413 return -ENOMEM;
1414
1415 /* If we created new params, mark them to be deleted in
1416 * hci_connect_le_scan_cleanup. It's different case than
1417 * existing disabled params, those will stay after cleanup.
1418 */
1419 params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
1420 }
f75113a2 1421
5157b8a5 1422 /* We're trying to connect, so make sure params are at pend_le_conns */
49c50922 1423 if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
5157b8a5
JP
1424 params->auto_connect == HCI_AUTO_CONN_REPORT ||
1425 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
195ef75e
PV
1426 hci_pend_le_list_del_init(params);
1427 hci_pend_le_list_add(params, &hdev->pend_le_conns);
f75113a2
JP
1428 }
1429
1430 params->explicit_connect = true;
f75113a2
JP
1431
1432 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
1433 params->auto_connect);
1434
1435 return 0;
1436}
1437
eca0ae4a
LAD
1438static int qos_set_big(struct hci_dev *hdev, struct bt_iso_qos *qos)
1439{
6a42e9bf
IT
1440 struct hci_conn *conn;
1441 u8 big;
eca0ae4a
LAD
1442
1443 /* Allocate a BIG if not set */
0fe8c8d0 1444 if (qos->bcast.big == BT_ISO_QOS_BIG_UNSET) {
6a42e9bf 1445 for (big = 0x00; big < 0xef; big++) {
eca0ae4a 1446
6a42e9bf
IT
1447 conn = hci_conn_hash_lookup_big(hdev, big);
1448 if (!conn)
eca0ae4a
LAD
1449 break;
1450 }
1451
6a42e9bf 1452 if (big == 0xef)
eca0ae4a
LAD
1453 return -EADDRNOTAVAIL;
1454
1455 /* Update BIG */
6a42e9bf 1456 qos->bcast.big = big;
eca0ae4a
LAD
1457 }
1458
1459 return 0;
1460}
1461
1462static int qos_set_bis(struct hci_dev *hdev, struct bt_iso_qos *qos)
1463{
6a42e9bf
IT
1464 struct hci_conn *conn;
1465 u8 bis;
eca0ae4a
LAD
1466
1467 /* Allocate BIS if not set */
0fe8c8d0 1468 if (qos->bcast.bis == BT_ISO_QOS_BIS_UNSET) {
71b7bb48
IT
1469 if (qos->bcast.big != BT_ISO_QOS_BIG_UNSET) {
1470 conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big);
1471
1472 if (conn) {
1473 /* If the BIG handle is already matched to an advertising
1474 * handle, do not allocate a new one.
1475 */
1476 qos->bcast.bis = conn->iso_qos.bcast.bis;
1477 return 0;
1478 }
1479 }
1480
eca0ae4a
LAD
1481 /* Find an unused adv set to advertise BIS, skip instance 0x00
1482 * since it is reserved as general purpose set.
1483 */
6a42e9bf
IT
1484 for (bis = 0x01; bis < hdev->le_num_of_adv_sets;
1485 bis++) {
eca0ae4a 1486
6a42e9bf
IT
1487 conn = hci_conn_hash_lookup_bis(hdev, BDADDR_ANY, bis);
1488 if (!conn)
eca0ae4a
LAD
1489 break;
1490 }
1491
6a42e9bf 1492 if (bis == hdev->le_num_of_adv_sets)
eca0ae4a
LAD
1493 return -EADDRNOTAVAIL;
1494
1495 /* Update BIS */
6a42e9bf 1496 qos->bcast.bis = bis;
eca0ae4a
LAD
1497 }
1498
1499 return 0;
1500}
1501
1502/* This function requires the caller holds hdev->lock */
1503static struct hci_conn *hci_add_bis(struct hci_dev *hdev, bdaddr_t *dst,
5842c01a
LAD
1504 __u8 sid, struct bt_iso_qos *qos,
1505 __u8 base_len, __u8 *base)
eca0ae4a
LAD
1506{
1507 struct hci_conn *conn;
eca0ae4a
LAD
1508 int err;
1509
1510 /* Let's make sure that le is enabled.*/
1511 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1512 if (lmp_le_capable(hdev))
1513 return ERR_PTR(-ECONNREFUSED);
1514 return ERR_PTR(-EOPNOTSUPP);
1515 }
1516
1517 err = qos_set_big(hdev, qos);
1518 if (err)
1519 return ERR_PTR(err);
1520
1521 err = qos_set_bis(hdev, qos);
1522 if (err)
1523 return ERR_PTR(err);
1524
a0bfde16
IT
1525 /* Check if the LE Create BIG command has already been sent */
1526 conn = hci_conn_hash_lookup_per_adv_bis(hdev, dst, qos->bcast.big,
1527 qos->bcast.big);
1528 if (conn)
eca0ae4a
LAD
1529 return ERR_PTR(-EADDRINUSE);
1530
a0bfde16
IT
1531 /* Check BIS settings against other bound BISes, since all
1532 * BISes in a BIG must have the same value for all parameters
1533 */
6a42e9bf 1534 conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big);
a0bfde16
IT
1535
1536 if (conn && (memcmp(qos, &conn->iso_qos, sizeof(*qos)) ||
1537 base_len != conn->le_per_adv_data_len ||
1538 memcmp(conn->le_per_adv_data, base, base_len)))
eca0ae4a
LAD
1539 return ERR_PTR(-EADDRINUSE);
1540
23205562 1541 conn = hci_conn_add_unset(hdev, BIS_LINK, dst, HCI_ROLE_MASTER);
a5b862c6
SK
1542 if (IS_ERR(conn))
1543 return conn;
eca0ae4a 1544
eca0ae4a 1545 conn->state = BT_CONNECT;
5842c01a 1546 conn->sid = sid;
eca0ae4a
LAD
1547
1548 hci_conn_hold(conn);
1549 return conn;
1550}
1551
f75113a2
JP
1552/* This function requires the caller holds hdev->lock */
1553struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1554 u8 dst_type, u8 sec_level,
76b13996
MM
1555 u16 conn_timeout,
1556 enum conn_reasons conn_reason)
f75113a2
JP
1557{
1558 struct hci_conn *conn;
f75113a2
JP
1559
1560 /* Let's make sure that le is enabled.*/
1561 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1562 if (lmp_le_capable(hdev))
1563 return ERR_PTR(-ECONNREFUSED);
1564
1565 return ERR_PTR(-EOPNOTSUPP);
1566 }
1567
1568 /* Some devices send ATT messages as soon as the physical link is
1569 * established. To be able to handle these ATT messages, the user-
1570 * space first establishes the connection and then starts the pairing
1571 * process.
1572 *
1573 * So if a hci_conn object already exists for the following connection
1574 * attempt, we simply update pending_sec_level and auth_type fields
1575 * and return the object found.
1576 */
9d4c1cc1 1577 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
f75113a2
JP
1578 if (conn) {
1579 if (conn->pending_sec_level < sec_level)
1580 conn->pending_sec_level = sec_level;
1581 goto done;
1582 }
1583
1584 BT_DBG("requesting refresh of dst_addr");
1585
181a42ed 1586 conn = hci_conn_add_unset(hdev, LE_LINK, dst, HCI_ROLE_MASTER);
a5b862c6
SK
1587 if (IS_ERR(conn))
1588 return conn;
f75113a2 1589
d088337c
NE
1590 if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) {
1591 hci_conn_del(conn);
f75113a2 1592 return ERR_PTR(-EBUSY);
d088337c 1593 }
f75113a2
JP
1594
1595 conn->state = BT_CONNECT;
1596 set_bit(HCI_CONN_SCANNING, &conn->flags);
f75113a2
JP
1597 conn->dst_type = dst_type;
1598 conn->sec_level = BT_SECURITY_LOW;
1599 conn->pending_sec_level = sec_level;
1600 conn->conn_timeout = conn_timeout;
76b13996 1601 conn->conn_reason = conn_reason;
f75113a2 1602
5bee2fd6 1603 hci_update_passive_scan(hdev);
84235d22 1604
f75113a2
JP
1605done:
1606 hci_conn_hold(conn);
1607 return conn;
1608}
1609
04a6c589 1610struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
76b13996 1611 u8 sec_level, u8 auth_type,
bf98feea 1612 enum conn_reasons conn_reason, u16 timeout)
1da177e4
LT
1613{
1614 struct hci_conn *acl;
fcd89c09 1615
d7a5a11d 1616 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
c411110e
LR
1617 if (lmp_bredr_capable(hdev))
1618 return ERR_PTR(-ECONNREFUSED);
1619
beb19e4c 1620 return ERR_PTR(-EOPNOTSUPP);
c411110e 1621 }
56f87901 1622
1ffc6f8c
LCY
1623 /* Reject outgoing connection to device with same BD ADDR against
1624 * CVE-2020-26555
1625 */
1626 if (!bacmp(&hdev->bdaddr, dst)) {
1627 bt_dev_dbg(hdev, "Reject connection with same BD_ADDR %pMR\n",
1628 dst);
1629 return ERR_PTR(-ECONNREFUSED);
1630 }
1631
70f23020
AE
1632 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
1633 if (!acl) {
181a42ed 1634 acl = hci_conn_add_unset(hdev, ACL_LINK, dst, HCI_ROLE_MASTER);
a5b862c6
SK
1635 if (IS_ERR(acl))
1636 return acl;
1da177e4
LT
1637 }
1638
1639 hci_conn_hold(acl);
1640
76b13996 1641 acl->conn_reason = conn_reason;
09ab6f4c 1642 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
45340097
JD
1643 int err;
1644
765c2a96
JH
1645 acl->sec_level = BT_SECURITY_LOW;
1646 acl->pending_sec_level = sec_level;
09ab6f4c 1647 acl->auth_type = auth_type;
bf98feea 1648 acl->conn_timeout = timeout;
45340097 1649
5f641f03 1650 err = hci_connect_acl_sync(hdev, acl);
45340097
JD
1651 if (err) {
1652 hci_conn_del(acl);
1653 return ERR_PTR(err);
1654 }
09ab6f4c 1655 }
1da177e4 1656
db474275
VCG
1657 return acl;
1658}
1659
06149746
LAD
1660static struct hci_link *hci_conn_link(struct hci_conn *parent,
1661 struct hci_conn *conn)
1662{
1663 struct hci_dev *hdev = parent->hdev;
1664 struct hci_link *link;
1665
1666 bt_dev_dbg(hdev, "parent %p hcon %p", parent, conn);
1667
1668 if (conn->link)
1669 return conn->link;
1670
1671 if (conn->parent)
1672 return NULL;
1673
1674 link = kzalloc(sizeof(*link), GFP_KERNEL);
1675 if (!link)
1676 return NULL;
1677
1678 link->conn = hci_conn_hold(conn);
1679 conn->link = link;
1680 conn->parent = hci_conn_get(parent);
1681
1682 /* Use list_add_tail_rcu append to the list */
1683 list_add_tail_rcu(&link->list, &parent->link_list);
1684
1685 return link;
1686}
1687
10c62ddc 1688struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
bf98feea
LAD
1689 __u16 setting, struct bt_codec *codec,
1690 u16 timeout)
db474275
VCG
1691{
1692 struct hci_conn *acl;
1693 struct hci_conn *sco;
06149746 1694 struct hci_link *link;
db474275 1695
76b13996 1696 acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING,
bf98feea 1697 CONN_REASON_SCO_CONNECT, timeout);
db474275 1698 if (IS_ERR(acl))
5b7f9909 1699 return acl;
1da177e4 1700
70f23020
AE
1701 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
1702 if (!sco) {
181a42ed 1703 sco = hci_conn_add_unset(hdev, type, dst, HCI_ROLE_MASTER);
a5b862c6 1704 if (IS_ERR(sco)) {
76a68ba0 1705 hci_conn_drop(acl);
a5b862c6 1706 return sco;
1da177e4 1707 }
5b7f9909 1708 }
1da177e4 1709
06149746
LAD
1710 link = hci_conn_link(acl, sco);
1711 if (!link) {
1712 hci_conn_drop(acl);
1713 hci_conn_drop(sco);
b4066eb0 1714 return ERR_PTR(-ENOLINK);
06149746 1715 }
1da177e4 1716
10c62ddc 1717 sco->setting = setting;
b2af264a 1718 sco->codec = *codec;
10c62ddc 1719
5b7f9909 1720 if (acl->state == BT_CONNECTED &&
5974e4c4 1721 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
58a681ef 1722 set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
14b12d0b 1723 hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
c390216b 1724
51a8efd7 1725 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
e73439d8 1726 /* defer SCO setup until mode change completed */
51a8efd7 1727 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
e73439d8
MH
1728 return sco;
1729 }
1730
1731 hci_sco_setup(acl, 0x00);
b6a0dc82 1732 }
5b7f9909
MH
1733
1734 return sco;
1da177e4 1735}
1da177e4 1736
eca0ae4a
LAD
1737static int hci_le_create_big(struct hci_conn *conn, struct bt_iso_qos *qos)
1738{
1739 struct hci_dev *hdev = conn->hdev;
1740 struct hci_cp_le_create_big cp;
a0bfde16 1741 struct iso_list_data data;
eca0ae4a
LAD
1742
1743 memset(&cp, 0, sizeof(cp));
1744
a0bfde16
IT
1745 data.big = qos->bcast.big;
1746 data.bis = qos->bcast.bis;
1747 data.count = 0;
1748
1749 /* Create a BIS for each bound connection */
23205562 1750 hci_conn_hash_list_state(hdev, bis_list, BIS_LINK,
a0bfde16
IT
1751 BT_BOUND, &data);
1752
0fe8c8d0
IT
1753 cp.handle = qos->bcast.big;
1754 cp.adv_handle = qos->bcast.bis;
a0bfde16 1755 cp.num_bis = data.count;
0fe8c8d0
IT
1756 hci_cpu_to_le24(qos->bcast.out.interval, cp.bis.sdu_interval);
1757 cp.bis.sdu = cpu_to_le16(qos->bcast.out.sdu);
1758 cp.bis.latency = cpu_to_le16(qos->bcast.out.latency);
1759 cp.bis.rtn = qos->bcast.out.rtn;
1760 cp.bis.phy = qos->bcast.out.phy;
1761 cp.bis.packing = qos->bcast.packing;
1762 cp.bis.framing = qos->bcast.framing;
1763 cp.bis.encryption = qos->bcast.encryption;
1764 memcpy(cp.bis.bcode, qos->bcast.bcode, sizeof(cp.bis.bcode));
eca0ae4a
LAD
1765
1766 return hci_send_cmd(hdev, HCI_OP_LE_CREATE_BIG, sizeof(cp), &cp);
1767}
1768
a0912892 1769static int set_cig_params_sync(struct hci_dev *hdev, void *data)
6b9545dc 1770{
ea9e148c 1771 DEFINE_FLEX(struct hci_cp_le_set_cig_params, pdu, cis, num_cis, 0x1f);
a1f6c3ae 1772 u8 cig_id = PTR_UINT(data);
a0912892
LAD
1773 struct hci_conn *conn;
1774 struct bt_iso_qos *qos;
ea9e148c 1775 u8 aux_num_cis = 0;
a0912892 1776 u8 cis_id;
6b9545dc 1777
a0912892
LAD
1778 conn = hci_conn_hash_lookup_cig(hdev, cig_id);
1779 if (!conn)
1780 return 0;
6b9545dc 1781
a0912892 1782 qos = &conn->iso_qos;
ea9e148c
GS
1783 pdu->cig_id = cig_id;
1784 hci_cpu_to_le24(qos->ucast.out.interval, pdu->c_interval);
1785 hci_cpu_to_le24(qos->ucast.in.interval, pdu->p_interval);
1786 pdu->sca = qos->ucast.sca;
1787 pdu->packing = qos->ucast.packing;
1788 pdu->framing = qos->ucast.framing;
1789 pdu->c_latency = cpu_to_le16(qos->ucast.out.latency);
1790 pdu->p_latency = cpu_to_le16(qos->ucast.in.latency);
6b9545dc 1791
a0912892
LAD
1792 /* Reprogram all CIS(s) with the same CIG, valid range are:
1793 * num_cis: 0x00 to 0x1F
1794 * cis_id: 0x00 to 0xEF
1795 */
1796 for (cis_id = 0x00; cis_id < 0xf0 &&
ea9e148c 1797 aux_num_cis < pdu->num_cis; cis_id++) {
a0912892
LAD
1798 struct hci_cis_params *cis;
1799
1800 conn = hci_conn_hash_lookup_cis(hdev, NULL, 0, cig_id, cis_id);
1801 if (!conn)
1802 continue;
6b9545dc 1803
a0912892
LAD
1804 qos = &conn->iso_qos;
1805
ea9e148c 1806 cis = &pdu->cis[aux_num_cis++];
a0912892
LAD
1807 cis->cis_id = cis_id;
1808 cis->c_sdu = cpu_to_le16(conn->iso_qos.ucast.out.sdu);
1809 cis->p_sdu = cpu_to_le16(conn->iso_qos.ucast.in.sdu);
1810 cis->c_phy = qos->ucast.out.phy ? qos->ucast.out.phy :
1811 qos->ucast.in.phy;
1812 cis->p_phy = qos->ucast.in.phy ? qos->ucast.in.phy :
1813 qos->ucast.out.phy;
1814 cis->c_rtn = qos->ucast.out.rtn;
1815 cis->p_rtn = qos->ucast.in.rtn;
1816 }
ea9e148c 1817 pdu->num_cis = aux_num_cis;
a0912892 1818
ea9e148c 1819 if (!pdu->num_cis)
a0912892
LAD
1820 return 0;
1821
1822 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_CIG_PARAMS,
ea9e148c
GS
1823 struct_size(pdu, cis, pdu->num_cis),
1824 pdu, HCI_CMD_TIMEOUT);
6b9545dc
PV
1825}
1826
26afbd82
LAD
1827static bool hci_le_set_cig_params(struct hci_conn *conn, struct bt_iso_qos *qos)
1828{
1829 struct hci_dev *hdev = conn->hdev;
1830 struct iso_list_data data;
1831
1832 memset(&data, 0, sizeof(data));
1833
e6a7a46b 1834 /* Allocate first still reconfigurable CIG if not set */
0fe8c8d0 1835 if (qos->ucast.cig == BT_ISO_QOS_CIG_UNSET) {
e6a7a46b 1836 for (data.cig = 0x00; data.cig < 0xf0; data.cig++) {
26afbd82 1837 data.count = 0;
26afbd82 1838
23205562 1839 hci_conn_hash_list_state(hdev, find_cis, CIS_LINK,
e6a7a46b 1840 BT_CONNECT, &data);
26afbd82
LAD
1841 if (data.count)
1842 continue;
1843
23205562 1844 hci_conn_hash_list_state(hdev, find_cis, CIS_LINK,
26afbd82
LAD
1845 BT_CONNECTED, &data);
1846 if (!data.count)
1847 break;
1848 }
1849
e6a7a46b 1850 if (data.cig == 0xf0)
26afbd82
LAD
1851 return false;
1852
1853 /* Update CIG */
0fe8c8d0 1854 qos->ucast.cig = data.cig;
26afbd82
LAD
1855 }
1856
0fe8c8d0 1857 if (qos->ucast.cis != BT_ISO_QOS_CIS_UNSET) {
a0912892
LAD
1858 if (hci_conn_hash_lookup_cis(hdev, NULL, 0, qos->ucast.cig,
1859 qos->ucast.cis))
26afbd82 1860 return false;
a0912892 1861 goto done;
26afbd82
LAD
1862 }
1863
a0912892
LAD
1864 /* Allocate first available CIS if not set */
1865 for (data.cig = qos->ucast.cig, data.cis = 0x00; data.cis < 0xf0;
1866 data.cis++) {
1867 if (!hci_conn_hash_lookup_cis(hdev, NULL, 0, data.cig,
1868 data.cis)) {
26afbd82 1869 /* Update CIS */
0fe8c8d0 1870 qos->ucast.cis = data.cis;
a0912892 1871 break;
26afbd82
LAD
1872 }
1873 }
1874
a0912892 1875 if (qos->ucast.cis == BT_ISO_QOS_CIS_UNSET)
26afbd82
LAD
1876 return false;
1877
a0912892
LAD
1878done:
1879 if (hci_cmd_sync_queue(hdev, set_cig_params_sync,
a1f6c3ae 1880 UINT_PTR(qos->ucast.cig), NULL) < 0)
6b9545dc 1881 return false;
6b9545dc 1882
26afbd82
LAD
1883 return true;
1884}
1885
26afbd82
LAD
1886struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst,
1887 __u8 dst_type, struct bt_iso_qos *qos)
1888{
1889 struct hci_conn *cis;
1890
c14516fa
LAD
1891 cis = hci_conn_hash_lookup_cis(hdev, dst, dst_type, qos->ucast.cig,
1892 qos->ucast.cis);
26afbd82 1893 if (!cis) {
23205562
PV
1894 cis = hci_conn_add_unset(hdev, CIS_LINK, dst,
1895 HCI_ROLE_MASTER);
a5b862c6
SK
1896 if (IS_ERR(cis))
1897 return cis;
26afbd82 1898 cis->cleanup = cis_cleanup;
b36a234d 1899 cis->dst_type = dst_type;
b5793de3
PV
1900 cis->iso_qos.ucast.cig = BT_ISO_QOS_CIG_UNSET;
1901 cis->iso_qos.ucast.cis = BT_ISO_QOS_CIS_UNSET;
26afbd82
LAD
1902 }
1903
1904 if (cis->state == BT_CONNECTED)
1905 return cis;
1906
1907 /* Check if CIS has been set and the settings matches */
1908 if (cis->state == BT_BOUND &&
1909 !memcmp(&cis->iso_qos, qos, sizeof(*qos)))
1910 return cis;
1911
1912 /* Update LINK PHYs according to QoS preference */
0fe8c8d0
IT
1913 cis->le_tx_phy = qos->ucast.out.phy;
1914 cis->le_rx_phy = qos->ucast.in.phy;
26afbd82
LAD
1915
1916 /* If output interval is not set use the input interval as it cannot be
1917 * 0x000000.
1918 */
0fe8c8d0
IT
1919 if (!qos->ucast.out.interval)
1920 qos->ucast.out.interval = qos->ucast.in.interval;
26afbd82
LAD
1921
1922 /* If input interval is not set use the output interval as it cannot be
1923 * 0x000000.
1924 */
0fe8c8d0
IT
1925 if (!qos->ucast.in.interval)
1926 qos->ucast.in.interval = qos->ucast.out.interval;
26afbd82
LAD
1927
1928 /* If output latency is not set use the input latency as it cannot be
1929 * 0x0000.
1930 */
0fe8c8d0
IT
1931 if (!qos->ucast.out.latency)
1932 qos->ucast.out.latency = qos->ucast.in.latency;
26afbd82
LAD
1933
1934 /* If input latency is not set use the output latency as it cannot be
1935 * 0x0000.
1936 */
0fe8c8d0
IT
1937 if (!qos->ucast.in.latency)
1938 qos->ucast.in.latency = qos->ucast.out.latency;
26afbd82 1939
26afbd82
LAD
1940 if (!hci_le_set_cig_params(cis, qos)) {
1941 hci_conn_drop(cis);
1942 return ERR_PTR(-EINVAL);
1943 }
1944
69997d50
PV
1945 hci_conn_hold(cis);
1946
26afbd82
LAD
1947 cis->iso_qos = *qos;
1948 cis->state = BT_BOUND;
1949
1950 return cis;
1951}
1952
1953bool hci_iso_setup_path(struct hci_conn *conn)
1954{
1955 struct hci_dev *hdev = conn->hdev;
1956 struct hci_cp_le_setup_iso_path cmd;
1957
1958 memset(&cmd, 0, sizeof(cmd));
1959
0fe8c8d0 1960 if (conn->iso_qos.ucast.out.sdu) {
26afbd82
LAD
1961 cmd.handle = cpu_to_le16(conn->handle);
1962 cmd.direction = 0x00; /* Input (Host to Controller) */
1963 cmd.path = 0x00; /* HCI path if enabled */
1964 cmd.codec = 0x03; /* Transparent Data */
1965
1966 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
1967 &cmd) < 0)
1968 return false;
1969 }
1970
0fe8c8d0 1971 if (conn->iso_qos.ucast.in.sdu) {
26afbd82
LAD
1972 cmd.handle = cpu_to_le16(conn->handle);
1973 cmd.direction = 0x01; /* Output (Controller to Host) */
1974 cmd.path = 0x00; /* HCI path if enabled */
1975 cmd.codec = 0x03; /* Transparent Data */
1976
1977 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
1978 &cmd) < 0)
1979 return false;
1980 }
1981
1982 return true;
1983}
1984
7f74563e 1985int hci_conn_check_create_cis(struct hci_conn *conn)
26afbd82 1986{
23205562 1987 if (conn->type != CIS_LINK)
7f74563e 1988 return -EINVAL;
26afbd82 1989
7f74563e 1990 if (!conn->parent || conn->parent->state != BT_CONNECTED ||
9f78191c 1991 conn->state != BT_CONNECT || HCI_CONN_HANDLE_UNSET(conn->handle))
7f74563e 1992 return 1;
26afbd82 1993
7f74563e
PV
1994 return 0;
1995}
06149746 1996
7f74563e
PV
1997static int hci_create_cis_sync(struct hci_dev *hdev, void *data)
1998{
1999 return hci_le_create_cis_sync(hdev);
2000}
06149746 2001
7f74563e
PV
2002int hci_le_create_cis_pending(struct hci_dev *hdev)
2003{
2004 struct hci_conn *conn;
2005 bool pending = false;
06149746 2006
7f74563e 2007 rcu_read_lock();
06149746 2008
7f74563e
PV
2009 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
2010 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) {
2011 rcu_read_unlock();
2012 return -EBUSY;
06149746
LAD
2013 }
2014
7f74563e
PV
2015 if (!hci_conn_check_create_cis(conn))
2016 pending = true;
26afbd82
LAD
2017 }
2018
7f74563e
PV
2019 rcu_read_unlock();
2020
2021 if (!pending)
26afbd82
LAD
2022 return 0;
2023
2024 /* Queue Create CIS */
7f74563e 2025 return hci_cmd_sync_queue(hdev, hci_create_cis_sync, NULL, NULL);
26afbd82
LAD
2026}
2027
2028static void hci_iso_qos_setup(struct hci_dev *hdev, struct hci_conn *conn,
2029 struct bt_iso_io_qos *qos, __u8 phy)
2030{
2031 /* Only set MTU if PHY is enabled */
a5b862c6
SK
2032 if (!qos->sdu && qos->phy)
2033 qos->sdu = conn->mtu;
26afbd82
LAD
2034
2035 /* Use the same PHY as ACL if set to any */
2036 if (qos->phy == BT_ISO_PHY_ANY)
2037 qos->phy = phy;
2038
2039 /* Use LE ACL connection interval if not set */
2040 if (!qos->interval)
2041 /* ACL interval unit in 1.25 ms to us */
2042 qos->interval = conn->le_conn_interval * 1250;
2043
2044 /* Use LE ACL connection latency if not set */
2045 if (!qos->latency)
2046 qos->latency = conn->le_conn_latency;
2047}
2048
eca0ae4a
LAD
2049static int create_big_sync(struct hci_dev *hdev, void *data)
2050{
2051 struct hci_conn *conn = data;
2052 struct bt_iso_qos *qos = &conn->iso_qos;
2053 u16 interval, sync_interval = 0;
2054 u32 flags = 0;
2055 int err;
2056
0fe8c8d0 2057 if (qos->bcast.out.phy == 0x02)
eca0ae4a
LAD
2058 flags |= MGMT_ADV_FLAG_SEC_2M;
2059
2060 /* Align intervals */
14f0dcec 2061 interval = (qos->bcast.out.interval / 1250) * qos->bcast.sync_factor;
eca0ae4a 2062
0fe8c8d0 2063 if (qos->bcast.bis)
14f0dcec 2064 sync_interval = interval * 4;
eca0ae4a 2065
5842c01a
LAD
2066 err = hci_start_per_adv_sync(hdev, qos->bcast.bis, conn->sid,
2067 conn->le_per_adv_data_len,
eca0ae4a
LAD
2068 conn->le_per_adv_data, flags, interval,
2069 interval, sync_interval);
2070 if (err)
2071 return err;
2072
2073 return hci_le_create_big(conn, &conn->iso_qos);
2074}
2075
02171da6
IT
2076struct hci_conn *hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst,
2077 __u8 dst_type, __u8 sid,
2078 struct bt_iso_qos *qos)
eca0ae4a 2079{
02171da6 2080 struct hci_conn *conn;
02171da6 2081
e2d471b7
LAD
2082 bt_dev_dbg(hdev, "dst %pMR type %d sid %d", dst, dst_type, sid);
2083
23205562 2084 conn = hci_conn_add_unset(hdev, BIS_LINK, dst, HCI_ROLE_SLAVE);
a5b862c6
SK
2085 if (IS_ERR(conn))
2086 return conn;
02171da6
IT
2087
2088 conn->iso_qos = *qos;
4a5e0ba6
IT
2089 conn->dst_type = dst_type;
2090 conn->sid = sid;
02171da6 2091 conn->state = BT_LISTEN;
6d0417e4 2092 conn->conn_timeout = msecs_to_jiffies(qos->bcast.sync_timeout * 10);
02171da6
IT
2093
2094 hci_conn_hold(conn);
eca0ae4a 2095
6d0417e4 2096 hci_connect_pa_sync(hdev, conn);
02171da6
IT
2097
2098 return conn;
eca0ae4a
LAD
2099}
2100
024421cf
LAD
2101int hci_conn_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon,
2102 struct bt_iso_qos *qos, __u16 sync_handle,
2103 __u8 num_bis, __u8 bis[])
eca0ae4a 2104{
eca0ae4a
LAD
2105 int err;
2106
42ecf194 2107 if (num_bis < 0x01 || num_bis > ISO_MAX_NUM_BIS)
eca0ae4a
LAD
2108 return -EINVAL;
2109
2110 err = qos_set_big(hdev, qos);
2111 if (err)
2112 return err;
2113
42ecf194
IT
2114 if (hcon) {
2115 /* Update hcon QoS */
2116 hcon->iso_qos = *qos;
fbdc4bc4 2117
42ecf194
IT
2118 hcon->num_bis = num_bis;
2119 memcpy(hcon->bis, bis, num_bis);
024421cf 2120 hcon->conn_timeout = msecs_to_jiffies(qos->bcast.timeout * 10);
42ecf194 2121 }
eca0ae4a 2122
024421cf 2123 return hci_connect_big_sync(hdev, hcon);
eca0ae4a
LAD
2124}
2125
2126static void create_big_complete(struct hci_dev *hdev, void *data, int err)
2127{
2128 struct hci_conn *conn = data;
2129
2130 bt_dev_dbg(hdev, "conn %p", conn);
2131
2132 if (err) {
2133 bt_dev_err(hdev, "Unable to create BIG: %d", err);
2134 hci_connect_cfm(conn, err);
2135 hci_conn_del(conn);
2136 }
2137}
2138
5842c01a 2139struct hci_conn *hci_bind_bis(struct hci_dev *hdev, bdaddr_t *dst, __u8 sid,
a0bfde16
IT
2140 struct bt_iso_qos *qos,
2141 __u8 base_len, __u8 *base)
eca0ae4a
LAD
2142{
2143 struct hci_conn *conn;
fa224d0c 2144 struct hci_conn *parent;
a0bfde16 2145 __u8 eir[HCI_MAX_PER_AD_LENGTH];
fa224d0c
IT
2146 struct hci_link *link;
2147
2148 /* Look for any BIS that is open for rebinding */
2149 conn = hci_conn_hash_lookup_big_state(hdev, qos->bcast.big, BT_OPEN);
2150 if (conn) {
2151 memcpy(qos, &conn->iso_qos, sizeof(*qos));
2152 conn->state = BT_CONNECTED;
2153 return conn;
2154 }
a0bfde16
IT
2155
2156 if (base_len && base)
2157 base_len = eir_append_service_data(eir, 0, 0x1851,
2158 base, base_len);
eca0ae4a
LAD
2159
2160 /* We need hci_conn object using the BDADDR_ANY as dst */
5842c01a 2161 conn = hci_add_bis(hdev, dst, sid, qos, base_len, eir);
eca0ae4a
LAD
2162 if (IS_ERR(conn))
2163 return conn;
2164
a0bfde16
IT
2165 /* Update LINK PHYs according to QoS preference */
2166 conn->le_tx_phy = qos->bcast.out.phy;
2167 conn->le_tx_phy = qos->bcast.out.phy;
eca0ae4a
LAD
2168
2169 /* Add Basic Announcement into Peridic Adv Data if BASE is set */
2170 if (base_len && base) {
a0bfde16 2171 memcpy(conn->le_per_adv_data, eir, sizeof(eir));
eca0ae4a
LAD
2172 conn->le_per_adv_data_len = base_len;
2173 }
2174
a0bfde16
IT
2175 hci_iso_qos_setup(hdev, conn, &qos->bcast.out,
2176 conn->le_tx_phy ? conn->le_tx_phy :
2177 hdev->le_tx_def_phys);
2178
2179 conn->iso_qos = *qos;
2180 conn->state = BT_BOUND;
2181
fa224d0c
IT
2182 /* Link BISes together */
2183 parent = hci_conn_hash_lookup_big(hdev,
2184 conn->iso_qos.bcast.big);
2185 if (parent && parent != conn) {
2186 link = hci_conn_link(parent, conn);
fa224d0c 2187 hci_conn_drop(conn);
d96b543c
ME
2188 if (!link)
2189 return ERR_PTR(-ENOLINK);
fa224d0c
IT
2190 }
2191
a0bfde16
IT
2192 return conn;
2193}
2194
2195static void bis_mark_per_adv(struct hci_conn *conn, void *data)
2196{
2197 struct iso_list_data *d = data;
2198
2199 /* Skip if not broadcast/ANY address */
2200 if (bacmp(&conn->dst, BDADDR_ANY))
2201 return;
2202
2203 if (d->big != conn->iso_qos.bcast.big ||
2204 d->bis == BT_ISO_QOS_BIS_UNSET ||
2205 d->bis != conn->iso_qos.bcast.bis)
2206 return;
2207
2208 set_bit(HCI_CONN_PER_ADV, &conn->flags);
2209}
2210
2211struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst,
5842c01a
LAD
2212 __u8 dst_type, __u8 sid,
2213 struct bt_iso_qos *qos,
a0bfde16
IT
2214 __u8 base_len, __u8 *base)
2215{
2216 struct hci_conn *conn;
2217 int err;
2218 struct iso_list_data data;
2219
5842c01a 2220 conn = hci_bind_bis(hdev, dst, sid, qos, base_len, base);
a0bfde16
IT
2221 if (IS_ERR(conn))
2222 return conn;
2223
fa224d0c
IT
2224 if (conn->state == BT_CONNECTED)
2225 return conn;
2226
5842c01a
LAD
2227 /* Check if SID needs to be allocated then search for the first
2228 * available.
2229 */
2230 if (conn->sid == HCI_SID_INVALID) {
2231 u8 sid;
2232
2233 for (sid = 0; sid <= 0x0f; sid++) {
2234 if (!hci_find_adv_sid(hdev, sid)) {
2235 conn->sid = sid;
2236 break;
2237 }
2238 }
2239 }
2240
a0bfde16
IT
2241 data.big = qos->bcast.big;
2242 data.bis = qos->bcast.bis;
2243
2244 /* Set HCI_CONN_PER_ADV for all bound connections, to mark that
2245 * the start periodic advertising and create BIG commands have
2246 * been queued
2247 */
23205562 2248 hci_conn_hash_list_state(hdev, bis_mark_per_adv, BIS_LINK,
a0bfde16
IT
2249 BT_BOUND, &data);
2250
eca0ae4a
LAD
2251 /* Queue start periodic advertising and create BIG */
2252 err = hci_cmd_sync_queue(hdev, create_big_sync, conn,
2253 create_big_complete);
2254 if (err < 0) {
2255 hci_conn_drop(conn);
2256 return ERR_PTR(err);
2257 }
2258
eca0ae4a
LAD
2259 return conn;
2260}
2261
26afbd82
LAD
2262struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst,
2263 __u8 dst_type, struct bt_iso_qos *qos)
2264{
2265 struct hci_conn *le;
2266 struct hci_conn *cis;
06149746 2267 struct hci_link *link;
26afbd82 2268
26afbd82
LAD
2269 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
2270 le = hci_connect_le(hdev, dst, dst_type, false,
2271 BT_SECURITY_LOW,
2272 HCI_LE_CONN_TIMEOUT,
2e7ed5f5 2273 HCI_ROLE_SLAVE, 0, 0);
26afbd82
LAD
2274 else
2275 le = hci_connect_le_scan(hdev, dst, dst_type,
2276 BT_SECURITY_LOW,
2277 HCI_LE_CONN_TIMEOUT,
2278 CONN_REASON_ISO_CONNECT);
2279 if (IS_ERR(le))
2280 return le;
2281
0fe8c8d0 2282 hci_iso_qos_setup(hdev, le, &qos->ucast.out,
26afbd82 2283 le->le_tx_phy ? le->le_tx_phy : hdev->le_tx_def_phys);
0fe8c8d0 2284 hci_iso_qos_setup(hdev, le, &qos->ucast.in,
26afbd82
LAD
2285 le->le_rx_phy ? le->le_rx_phy : hdev->le_rx_def_phys);
2286
2287 cis = hci_bind_cis(hdev, dst, dst_type, qos);
2288 if (IS_ERR(cis)) {
2289 hci_conn_drop(le);
2290 return cis;
2291 }
2292
06149746 2293 link = hci_conn_link(le, cis);
d96b543c 2294 hci_conn_drop(cis);
06149746
LAD
2295 if (!link) {
2296 hci_conn_drop(le);
b4066eb0 2297 return ERR_PTR(-ENOLINK);
06149746 2298 }
26afbd82 2299
7f74563e
PV
2300 cis->state = BT_CONNECT;
2301
2302 hci_le_create_cis_pending(hdev);
26afbd82
LAD
2303
2304 return cis;
2305}
2306
e7c29cb1
MH
2307/* Check link security requirement */
2308int hci_conn_check_link_mode(struct hci_conn *conn)
2309{
38b3fef1 2310 BT_DBG("hcon %p", conn);
e7c29cb1 2311
40b552aa
MH
2312 /* In Secure Connections Only mode, it is required that Secure
2313 * Connections is used and the link is encrypted with AES-CCM
2314 * using a P-256 authenticated combination key.
2315 */
d7a5a11d 2316 if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
40b552aa
MH
2317 if (!hci_conn_sc_enabled(conn) ||
2318 !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
2319 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
2320 return 0;
2321 }
2322
8746f135
LAD
2323 /* AES encryption is required for Level 4:
2324 *
2325 * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part C
2326 * page 1319:
2327 *
2328 * 128-bit equivalent strength for link and encryption keys
2329 * required using FIPS approved algorithms (E0 not allowed,
2330 * SAFER+ not allowed, and P-192 not allowed; encryption key
2331 * not shortened)
2332 */
2333 if (conn->sec_level == BT_SECURITY_FIPS &&
2334 !test_bit(HCI_CONN_AES_CCM, &conn->flags)) {
2335 bt_dev_err(conn->hdev,
2336 "Invalid security: Missing AES-CCM usage");
2337 return 0;
2338 }
2339
4dae2798
JH
2340 if (hci_conn_ssp_enabled(conn) &&
2341 !test_bit(HCI_CONN_ENCRYPT, &conn->flags))
e7c29cb1
MH
2342 return 0;
2343
2344 return 1;
2345}
e7c29cb1 2346
1da177e4 2347/* Authenticate remote device */
0684e5f9 2348static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
1da177e4 2349{
38b3fef1 2350 BT_DBG("hcon %p", conn);
1da177e4 2351
765c2a96
JH
2352 if (conn->pending_sec_level > sec_level)
2353 sec_level = conn->pending_sec_level;
2354
96a31833 2355 if (sec_level > conn->sec_level)
765c2a96 2356 conn->pending_sec_level = sec_level;
4dae2798 2357 else if (test_bit(HCI_CONN_AUTH, &conn->flags))
1da177e4
LT
2358 return 1;
2359
65cf686e
JH
2360 /* Make sure we preserve an existing MITM requirement*/
2361 auth_type |= (conn->auth_type & 0x01);
2362
96a31833
MH
2363 conn->auth_type = auth_type;
2364
51a8efd7 2365 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
1da177e4 2366 struct hci_cp_auth_requested cp;
b7d05bad 2367
aca3192c 2368 cp.handle = cpu_to_le16(conn->handle);
40be492f 2369 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
5974e4c4 2370 sizeof(cp), &cp);
09da1f34 2371
d03376c1
LAD
2372 /* Set the ENCRYPT_PEND to trigger encryption after
2373 * authentication.
09da1f34 2374 */
d03376c1 2375 if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
09da1f34 2376 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
1da177e4 2377 }
8c1b2355 2378
1da177e4
LT
2379 return 0;
2380}
1da177e4 2381
bb6d6895 2382/* Encrypt the link */
13d39315
WR
2383static void hci_conn_encrypt(struct hci_conn *conn)
2384{
38b3fef1 2385 BT_DBG("hcon %p", conn);
13d39315 2386
51a8efd7 2387 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
13d39315
WR
2388 struct hci_cp_set_conn_encrypt cp;
2389 cp.handle = cpu_to_le16(conn->handle);
2390 cp.encrypt = 0x01;
2391 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
5974e4c4 2392 &cp);
13d39315
WR
2393 }
2394}
2395
8c1b2355 2396/* Enable security */
e7cafc45
JH
2397int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
2398 bool initiator)
1da177e4 2399{
38b3fef1 2400 BT_DBG("hcon %p", conn);
1da177e4 2401
d8343f12
VCG
2402 if (conn->type == LE_LINK)
2403 return smp_conn_security(conn, sec_level);
2404
13d39315 2405 /* For sdp we don't need the link key. */
8c1b2355
MH
2406 if (sec_level == BT_SECURITY_SDP)
2407 return 1;
2408
13d39315
WR
2409 /* For non 2.1 devices and low security level we don't need the link
2410 key. */
aa64a8b5 2411 if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
3fdca1e1 2412 return 1;
8c1b2355 2413
13d39315 2414 /* For other security levels we need the link key. */
4dae2798 2415 if (!test_bit(HCI_CONN_AUTH, &conn->flags))
13d39315
WR
2416 goto auth;
2417
1d8e8014
YH
2418 switch (conn->key_type) {
2419 case HCI_LK_AUTH_COMBINATION_P256:
2420 /* An authenticated FIPS approved combination key has
2421 * sufficient security for security level 4 or lower.
2422 */
2423 if (sec_level <= BT_SECURITY_FIPS)
2424 goto encrypt;
2425 break;
2426 case HCI_LK_AUTH_COMBINATION_P192:
2427 /* An authenticated combination key has sufficient security for
2428 * security level 3 or lower.
2429 */
2430 if (sec_level <= BT_SECURITY_HIGH)
2431 goto encrypt;
2432 break;
2433 case HCI_LK_UNAUTH_COMBINATION_P192:
2434 case HCI_LK_UNAUTH_COMBINATION_P256:
2435 /* An unauthenticated combination key has sufficient security
2436 * for security level 2 or lower.
2437 */
2438 if (sec_level <= BT_SECURITY_MEDIUM)
2439 goto encrypt;
2440 break;
2441 case HCI_LK_COMBINATION:
2442 /* A combination key has always sufficient security for the
2443 * security levels 2 or lower. High security level requires the
2444 * combination key is generated using maximum PIN code length
2445 * (16). For pre 2.1 units.
2446 */
2447 if (sec_level <= BT_SECURITY_MEDIUM || conn->pin_length == 16)
2448 goto encrypt;
2449 break;
2450 default:
2451 break;
2452 }
13d39315
WR
2453
2454auth:
51a8efd7 2455 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
1da177e4
LT
2456 return 0;
2457
977f8fce
JH
2458 if (initiator)
2459 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2460
6fdf658c
LAD
2461 if (!hci_conn_auth(conn, sec_level, auth_type))
2462 return 0;
13d39315
WR
2463
2464encrypt:
693cd8ce
MH
2465 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) {
2466 /* Ensure that the encryption key size has been read,
2467 * otherwise stall the upper layer responses.
2468 */
2469 if (!conn->enc_key_size)
2470 return 0;
2471
2472 /* Nothing else needed, all requirements are met */
13d39315 2473 return 1;
693cd8ce 2474 }
8c1b2355 2475
13d39315 2476 hci_conn_encrypt(conn);
1da177e4
LT
2477 return 0;
2478}
8c1b2355 2479EXPORT_SYMBOL(hci_conn_security);
1da177e4 2480
b3b1b061
WR
2481/* Check secure link requirement */
2482int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
2483{
38b3fef1 2484 BT_DBG("hcon %p", conn);
b3b1b061 2485
9cb2e030
MH
2486 /* Accept if non-secure or higher security level is required */
2487 if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS)
2488 return 1;
b3b1b061 2489
9cb2e030
MH
2490 /* Accept if secure or higher security level is already present */
2491 if (conn->sec_level == BT_SECURITY_HIGH ||
2492 conn->sec_level == BT_SECURITY_FIPS)
b3b1b061
WR
2493 return 1;
2494
9cb2e030
MH
2495 /* Reject not secure link */
2496 return 0;
b3b1b061
WR
2497}
2498EXPORT_SYMBOL(hci_conn_check_secure);
2499
1da177e4 2500/* Switch role */
8c1b2355 2501int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
1da177e4 2502{
38b3fef1 2503 BT_DBG("hcon %p", conn);
1da177e4 2504
40bef302 2505 if (role == conn->role)
1da177e4
LT
2506 return 1;
2507
51a8efd7 2508 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
1da177e4
LT
2509 struct hci_cp_switch_role cp;
2510 bacpy(&cp.bdaddr, &conn->dst);
2511 cp.role = role;
a9de9248 2512 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
1da177e4 2513 }
8c1b2355 2514
1da177e4
LT
2515 return 0;
2516}
2517EXPORT_SYMBOL(hci_conn_switch_role);
2518
04837f64 2519/* Enter active mode */
14b12d0b 2520void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
04837f64
MH
2521{
2522 struct hci_dev *hdev = conn->hdev;
2523
38b3fef1 2524 BT_DBG("hcon %p mode %d", conn, conn->mode);
04837f64 2525
14b12d0b
JG
2526 if (conn->mode != HCI_CM_SNIFF)
2527 goto timer;
2528
58a681ef 2529 if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
04837f64
MH
2530 goto timer;
2531
51a8efd7 2532 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
04837f64 2533 struct hci_cp_exit_sniff_mode cp;
aca3192c 2534 cp.handle = cpu_to_le16(conn->handle);
a9de9248 2535 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
04837f64
MH
2536 }
2537
2538timer:
2539 if (hdev->idle_timeout > 0)
a74a84f6
JH
2540 queue_delayed_work(hdev->workqueue, &conn->idle_work,
2541 msecs_to_jiffies(hdev->idle_timeout));
04837f64
MH
2542}
2543
1da177e4
LT
2544/* Drop all connection on the device */
2545void hci_conn_hash_flush(struct hci_dev *hdev)
2546{
a2ac591c
RL
2547 struct list_head *head = &hdev->conn_hash.list;
2548 struct hci_conn *conn;
1da177e4
LT
2549
2550 BT_DBG("hdev %s", hdev->name);
2551
a2ac591c
RL
2552 /* We should not traverse the list here, because hci_conn_del
2553 * can remove extra links, which may cause the list traversal
2554 * to hit items that have already been released.
2555 */
2556 while ((conn = list_first_entry_or_null(head,
2557 struct hci_conn,
2558 list)) != NULL) {
2559 conn->state = BT_CLOSED;
2560 hci_disconn_cfm(conn, HCI_ERROR_LOCAL_HOST_TERM);
a2ac591c 2561 hci_conn_del(conn);
1da177e4
LT
2562 }
2563}
2564
4dae2798
JH
2565static u32 get_link_mode(struct hci_conn *conn)
2566{
2567 u32 link_mode = 0;
2568
40bef302 2569 if (conn->role == HCI_ROLE_MASTER)
4dae2798
JH
2570 link_mode |= HCI_LM_MASTER;
2571
2572 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
2573 link_mode |= HCI_LM_ENCRYPT;
2574
2575 if (test_bit(HCI_CONN_AUTH, &conn->flags))
2576 link_mode |= HCI_LM_AUTH;
2577
2578 if (test_bit(HCI_CONN_SECURE, &conn->flags))
2579 link_mode |= HCI_LM_SECURE;
2580
2581 if (test_bit(HCI_CONN_FIPS, &conn->flags))
2582 link_mode |= HCI_LM_FIPS;
2583
2584 return link_mode;
2585}
2586
1da177e4
LT
2587int hci_get_conn_list(void __user *arg)
2588{
fc5fef61 2589 struct hci_conn *c;
1da177e4
LT
2590 struct hci_conn_list_req req, *cl;
2591 struct hci_conn_info *ci;
2592 struct hci_dev *hdev;
1da177e4
LT
2593 int n = 0, size, err;
2594
2595 if (copy_from_user(&req, arg, sizeof(req)))
2596 return -EFAULT;
2597
2598 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
2599 return -EINVAL;
2600
2601 size = sizeof(req) + req.conn_num * sizeof(*ci);
2602
70f23020
AE
2603 cl = kmalloc(size, GFP_KERNEL);
2604 if (!cl)
1da177e4
LT
2605 return -ENOMEM;
2606
70f23020
AE
2607 hdev = hci_dev_get(req.dev_id);
2608 if (!hdev) {
1da177e4
LT
2609 kfree(cl);
2610 return -ENODEV;
2611 }
2612
2613 ci = cl->conn_info;
2614
09fd0de5 2615 hci_dev_lock(hdev);
8035ded4 2616 list_for_each_entry(c, &hdev->conn_hash.list, list) {
1da177e4
LT
2617 bacpy(&(ci + n)->bdaddr, &c->dst);
2618 (ci + n)->handle = c->handle;
2619 (ci + n)->type = c->type;
2620 (ci + n)->out = c->out;
2621 (ci + n)->state = c->state;
4dae2798 2622 (ci + n)->link_mode = get_link_mode(c);
1da177e4
LT
2623 if (++n >= req.conn_num)
2624 break;
2625 }
09fd0de5 2626 hci_dev_unlock(hdev);
1da177e4
LT
2627
2628 cl->dev_id = hdev->id;
2629 cl->conn_num = n;
2630 size = sizeof(req) + n * sizeof(*ci);
2631
2632 hci_dev_put(hdev);
2633
2634 err = copy_to_user(arg, cl, size);
2635 kfree(cl);
2636
2637 return err ? -EFAULT : 0;
2638}
2639
2640int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
2641{
2642 struct hci_conn_info_req req;
2643 struct hci_conn_info ci;
2644 struct hci_conn *conn;
2645 char __user *ptr = arg + sizeof(req);
2646
2647 if (copy_from_user(&req, arg, sizeof(req)))
2648 return -EFAULT;
2649
09fd0de5 2650 hci_dev_lock(hdev);
1da177e4
LT
2651 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
2652 if (conn) {
2653 bacpy(&ci.bdaddr, &conn->dst);
2654 ci.handle = conn->handle;
2655 ci.type = conn->type;
2656 ci.out = conn->out;
2657 ci.state = conn->state;
4dae2798 2658 ci.link_mode = get_link_mode(conn);
1da177e4 2659 }
09fd0de5 2660 hci_dev_unlock(hdev);
1da177e4
LT
2661
2662 if (!conn)
2663 return -ENOENT;
2664
2665 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
2666}
40be492f
MH
2667
2668int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
2669{
2670 struct hci_auth_info_req req;
2671 struct hci_conn *conn;
2672
2673 if (copy_from_user(&req, arg, sizeof(req)))
2674 return -EFAULT;
2675
09fd0de5 2676 hci_dev_lock(hdev);
40be492f
MH
2677 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
2678 if (conn)
2679 req.type = conn->auth_type;
09fd0de5 2680 hci_dev_unlock(hdev);
40be492f
MH
2681
2682 if (!conn)
2683 return -ENOENT;
2684
2685 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
2686}
73d80deb
LAD
2687
2688struct hci_chan *hci_chan_create(struct hci_conn *conn)
2689{
2690 struct hci_dev *hdev = conn->hdev;
2691 struct hci_chan *chan;
2692
38b3fef1 2693 BT_DBG("%s hcon %p", hdev->name, conn);
73d80deb 2694
f94b665d
JH
2695 if (test_bit(HCI_CONN_DROP, &conn->flags)) {
2696 BT_DBG("Refusing to create new hci_chan");
2697 return NULL;
2698 }
2699
27f70f3e 2700 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
73d80deb
LAD
2701 if (!chan)
2702 return NULL;
2703
6c388d32 2704 chan->conn = hci_conn_get(conn);
73d80deb 2705 skb_queue_head_init(&chan->data_q);
168df8e5 2706 chan->state = BT_CONNECTED;
73d80deb 2707
8192edef 2708 list_add_rcu(&chan->list, &conn->chan_list);
73d80deb
LAD
2709
2710 return chan;
2711}
2712
9472007c 2713void hci_chan_del(struct hci_chan *chan)
73d80deb
LAD
2714{
2715 struct hci_conn *conn = chan->conn;
2716 struct hci_dev *hdev = conn->hdev;
2717
38b3fef1 2718 BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan);
73d80deb 2719
8192edef
GP
2720 list_del_rcu(&chan->list);
2721
2722 synchronize_rcu();
73d80deb 2723
bcbb655a 2724 /* Prevent new hci_chan's to be created for this hci_conn */
f94b665d 2725 set_bit(HCI_CONN_DROP, &conn->flags);
b3ff670a 2726
6c388d32 2727 hci_conn_put(conn);
e9b02748 2728
73d80deb
LAD
2729 skb_queue_purge(&chan->data_q);
2730 kfree(chan);
73d80deb
LAD
2731}
2732
2c33c06a 2733void hci_chan_list_flush(struct hci_conn *conn)
73d80deb 2734{
2a5a5ec6 2735 struct hci_chan *chan, *n;
73d80deb 2736
38b3fef1 2737 BT_DBG("hcon %p", conn);
73d80deb 2738
2a5a5ec6 2739 list_for_each_entry_safe(chan, n, &conn->chan_list, list)
73d80deb
LAD
2740 hci_chan_del(chan);
2741}
42c4e53e
AE
2742
2743static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon,
2744 __u16 handle)
2745{
2746 struct hci_chan *hchan;
2747
2748 list_for_each_entry(hchan, &hcon->chan_list, list) {
2749 if (hchan->handle == handle)
2750 return hchan;
2751 }
2752
2753 return NULL;
2754}
2755
2756struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle)
2757{
2758 struct hci_conn_hash *h = &hdev->conn_hash;
2759 struct hci_conn *hcon;
2760 struct hci_chan *hchan = NULL;
2761
2762 rcu_read_lock();
2763
2764 list_for_each_entry_rcu(hcon, &h->list, list) {
2765 hchan = __hci_chan_lookup_handle(hcon, handle);
2766 if (hchan)
2767 break;
2768 }
2769
2770 rcu_read_unlock();
2771
2772 return hchan;
2773}
eab2404b
LAD
2774
2775u32 hci_conn_get_phy(struct hci_conn *conn)
2776{
2777 u32 phys = 0;
2778
eab2404b
LAD
2779 /* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471:
2780 * Table 6.2: Packets defined for synchronous, asynchronous, and
6397729b 2781 * CPB logical transport types.
eab2404b
LAD
2782 */
2783 switch (conn->type) {
2784 case SCO_LINK:
2785 /* SCO logical transport (1 Mb/s):
2786 * HV1, HV2, HV3 and DV.
2787 */
2788 phys |= BT_PHY_BR_1M_1SLOT;
2789
2790 break;
2791
2792 case ACL_LINK:
2793 /* ACL logical transport (1 Mb/s) ptt=0:
2794 * DH1, DM3, DH3, DM5 and DH5.
2795 */
2796 phys |= BT_PHY_BR_1M_1SLOT;
2797
2798 if (conn->pkt_type & (HCI_DM3 | HCI_DH3))
2799 phys |= BT_PHY_BR_1M_3SLOT;
2800
2801 if (conn->pkt_type & (HCI_DM5 | HCI_DH5))
2802 phys |= BT_PHY_BR_1M_5SLOT;
2803
2804 /* ACL logical transport (2 Mb/s) ptt=1:
2805 * 2-DH1, 2-DH3 and 2-DH5.
2806 */
2807 if (!(conn->pkt_type & HCI_2DH1))
2808 phys |= BT_PHY_EDR_2M_1SLOT;
2809
2810 if (!(conn->pkt_type & HCI_2DH3))
2811 phys |= BT_PHY_EDR_2M_3SLOT;
2812
2813 if (!(conn->pkt_type & HCI_2DH5))
2814 phys |= BT_PHY_EDR_2M_5SLOT;
2815
2816 /* ACL logical transport (3 Mb/s) ptt=1:
2817 * 3-DH1, 3-DH3 and 3-DH5.
2818 */
2819 if (!(conn->pkt_type & HCI_3DH1))
2820 phys |= BT_PHY_EDR_3M_1SLOT;
2821
2822 if (!(conn->pkt_type & HCI_3DH3))
2823 phys |= BT_PHY_EDR_3M_3SLOT;
2824
2825 if (!(conn->pkt_type & HCI_3DH5))
2826 phys |= BT_PHY_EDR_3M_5SLOT;
2827
2828 break;
2829
2830 case ESCO_LINK:
2831 /* eSCO logical transport (1 Mb/s): EV3, EV4 and EV5 */
2832 phys |= BT_PHY_BR_1M_1SLOT;
2833
2834 if (!(conn->pkt_type & (ESCO_EV4 | ESCO_EV5)))
2835 phys |= BT_PHY_BR_1M_3SLOT;
2836
2837 /* eSCO logical transport (2 Mb/s): 2-EV3, 2-EV5 */
2838 if (!(conn->pkt_type & ESCO_2EV3))
2839 phys |= BT_PHY_EDR_2M_1SLOT;
2840
2841 if (!(conn->pkt_type & ESCO_2EV5))
2842 phys |= BT_PHY_EDR_2M_3SLOT;
2843
2844 /* eSCO logical transport (3 Mb/s): 3-EV3, 3-EV5 */
2845 if (!(conn->pkt_type & ESCO_3EV3))
2846 phys |= BT_PHY_EDR_3M_1SLOT;
2847
2848 if (!(conn->pkt_type & ESCO_3EV5))
2849 phys |= BT_PHY_EDR_3M_3SLOT;
2850
2851 break;
2852
2853 case LE_LINK:
2854 if (conn->le_tx_phy & HCI_LE_SET_PHY_1M)
2855 phys |= BT_PHY_LE_1M_TX;
2856
2857 if (conn->le_rx_phy & HCI_LE_SET_PHY_1M)
2858 phys |= BT_PHY_LE_1M_RX;
2859
2860 if (conn->le_tx_phy & HCI_LE_SET_PHY_2M)
2861 phys |= BT_PHY_LE_2M_TX;
2862
2863 if (conn->le_rx_phy & HCI_LE_SET_PHY_2M)
2864 phys |= BT_PHY_LE_2M_RX;
2865
2866 if (conn->le_tx_phy & HCI_LE_SET_PHY_CODED)
2867 phys |= BT_PHY_LE_CODED_TX;
2868
2869 if (conn->le_rx_phy & HCI_LE_SET_PHY_CODED)
2870 phys |= BT_PHY_LE_CODED_RX;
2871
2872 break;
2873 }
2874
eab2404b
LAD
2875 return phys;
2876}
1a942de0 2877
a13f316e 2878static int abort_conn_sync(struct hci_dev *hdev, void *data)
1a942de0 2879{
881559af 2880 struct hci_conn *conn = data;
1a942de0 2881
881559af
LAD
2882 if (!hci_conn_valid(hdev, conn))
2883 return -ECANCELED;
b62e7220 2884
a13f316e
LAD
2885 return hci_abort_conn_sync(hdev, conn, conn->abort_reason);
2886}
1a942de0 2887
a13f316e
LAD
2888int hci_abort_conn(struct hci_conn *conn, u8 reason)
2889{
2890 struct hci_dev *hdev = conn->hdev;
1a942de0 2891
a13f316e
LAD
2892 /* If abort_reason has already been set it means the connection is
2893 * already being aborted so don't attempt to overwrite it.
2894 */
2895 if (conn->abort_reason)
2896 return 0;
1a942de0 2897
a13f316e 2898 bt_dev_dbg(hdev, "handle 0x%2.2x reason 0x%2.2x", conn->handle, reason);
1a942de0 2899
a13f316e 2900 conn->abort_reason = reason;
1a942de0 2901
a13f316e
LAD
2902 /* If the connection is pending check the command opcode since that
2903 * might be blocking on hci_cmd_sync_work while waiting its respective
2904 * event so we need to hci_cmd_sync_cancel to cancel it.
04a51d61
LAD
2905 *
2906 * hci_connect_le serializes the connection attempts so only one
2907 * connection can be in BT_CONNECT at time.
a13f316e
LAD
2908 */
2909 if (conn->state == BT_CONNECT && hdev->req_status == HCI_REQ_PEND) {
2910 switch (hci_skb_event(hdev->sent_cmd)) {
5f641f03 2911 case HCI_EV_CONN_COMPLETE:
a13f316e
LAD
2912 case HCI_EV_LE_CONN_COMPLETE:
2913 case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
2914 case HCI_EVT_LE_CIS_ESTABLISHED:
2615fd9a 2915 hci_cmd_sync_cancel(hdev, ECANCELED);
a13f316e 2916 break;
1a942de0 2917 }
881559af
LAD
2918 /* Cancel connect attempt if still queued/pending */
2919 } else if (!hci_cancel_connect_sync(hdev, conn)) {
2920 return 0;
1a942de0
BG
2921 }
2922
227a0cdf
LAD
2923 /* Run immediately if on cmd_sync_work since this may be called
2924 * as a result to MGMT_OP_DISCONNECT/MGMT_OP_UNPAIR which does
2925 * already queue its callback on cmd_sync_work.
2926 */
2927 return hci_cmd_sync_run_once(hdev, abort_conn_sync, conn, NULL);
1a942de0 2928}
134f4b39
PV
2929
2930void hci_setup_tx_timestamp(struct sk_buff *skb, size_t key_offset,
2931 const struct sockcm_cookie *sockc)
2932{
2933 struct sock *sk = skb ? skb->sk : NULL;
c174cd09 2934 int key;
134f4b39
PV
2935
2936 /* This shall be called on a single skb of those generated by user
2937 * sendmsg(), and only when the sendmsg() does not return error to
2938 * user. This is required for keeping the tskey that increments here in
2939 * sync with possible sendmsg() counting by user.
2940 *
2941 * Stream sockets shall set key_offset to sendmsg() length in bytes
2942 * and call with the last fragment, others to 1 and first fragment.
2943 */
2944
2945 if (!skb || !sockc || !sk || !key_offset)
2946 return;
2947
2948 sock_tx_timestamp(sk, sockc, &skb_shinfo(skb)->tx_flags);
2949
c174cd09
PV
2950 if (sk->sk_type == SOCK_STREAM)
2951 key = atomic_add_return(key_offset, &sk->sk_tskey);
2952
134f4b39
PV
2953 if (sockc->tsflags & SOF_TIMESTAMPING_OPT_ID &&
2954 sockc->tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK) {
2955 if (sockc->tsflags & SOCKCM_FLAG_TS_OPT_ID) {
2956 skb_shinfo(skb)->tskey = sockc->ts_opt_id;
2957 } else {
c174cd09
PV
2958 if (sk->sk_type != SOCK_STREAM)
2959 key = atomic_inc_return(&sk->sk_tskey);
134f4b39
PV
2960 skb_shinfo(skb)->tskey = key - 1;
2961 }
2962 }
2963}
2964
2965void hci_conn_tx_queue(struct hci_conn *conn, struct sk_buff *skb)
2966{
2967 struct tx_queue *comp = &conn->tx_q;
2968 bool track = false;
2969
2970 /* Emit SND now, ie. just before sending to driver */
2971 if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP)
2972 __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SND);
2973
2974 /* COMPLETION tstamp is emitted for tracked skb later in Number of
2975 * Completed Packets event. Available only for flow controlled cases.
2976 *
2977 * TODO: SCO support without flowctl (needs to be done in drivers)
2978 */
2979 switch (conn->type) {
23205562
PV
2980 case CIS_LINK:
2981 case BIS_LINK:
134f4b39
PV
2982 case ACL_LINK:
2983 case LE_LINK:
2984 break;
2985 case SCO_LINK:
2986 case ESCO_LINK:
2987 if (!hci_dev_test_flag(conn->hdev, HCI_SCO_FLOWCTL))
2988 return;
2989 break;
2990 default:
2991 return;
2992 }
2993
2994 if (skb->sk && (skb_shinfo(skb)->tx_flags & SKBTX_COMPLETION_TSTAMP))
2995 track = true;
2996
2997 /* If nothing is tracked, just count extra skbs at the queue head */
2998 if (!track && !comp->tracked) {
2999 comp->extra++;
3000 return;
3001 }
3002
3003 if (track) {
3004 skb = skb_clone_sk(skb);
3005 if (!skb)
3006 goto count_only;
3007
3008 comp->tracked++;
3009 } else {
3010 skb = skb_clone(skb, GFP_KERNEL);
3011 if (!skb)
3012 goto count_only;
3013 }
3014
3015 skb_queue_tail(&comp->queue, skb);
3016 return;
3017
3018count_only:
3019 /* Stop tracking skbs, and only count. This will not emit timestamps for
3020 * the packets, but if we get here something is more seriously wrong.
3021 */
3022 comp->tracked = 0;
3023 comp->extra += skb_queue_len(&comp->queue) + 1;
3024 skb_queue_purge(&comp->queue);
3025}
3026
3027void hci_conn_tx_dequeue(struct hci_conn *conn)
3028{
3029 struct tx_queue *comp = &conn->tx_q;
3030 struct sk_buff *skb;
3031
3032 /* If there are tracked skbs, the counted extra go before dequeuing real
3033 * skbs, to keep ordering. When nothing is tracked, the ordering doesn't
3034 * matter so dequeue real skbs first to get rid of them ASAP.
3035 */
3036 if (comp->extra && (comp->tracked || skb_queue_empty(&comp->queue))) {
3037 comp->extra--;
3038 return;
3039 }
3040
3041 skb = skb_dequeue(&comp->queue);
3042 if (!skb)
3043 return;
3044
3045 if (skb->sk) {
3046 comp->tracked--;
3047 __skb_tstamp_tx(skb, NULL, NULL, skb->sk,
3048 SCM_TSTAMP_COMPLETION);
3049 }
3050
3051 kfree_skb(skb);
3052}
c82b6357
LAD
3053
3054u8 *hci_conn_key_enc_size(struct hci_conn *conn)
3055{
3056 if (conn->type == ACL_LINK) {
3057 struct link_key *key;
3058
3059 key = hci_find_link_key(conn->hdev, &conn->dst);
3060 if (!key)
3061 return NULL;
3062
3063 return &key->pin_len;
3064 } else if (conn->type == LE_LINK) {
3065 struct smp_ltk *ltk;
3066
3067 ltk = hci_find_ltk(conn->hdev, &conn->dst, conn->dst_type,
3068 conn->role);
3069 if (!ltk)
3070 return NULL;
3071
3072 return &ltk->enc_size;
3073 }
3074
3075 return NULL;
3076}
dd0ccf85
PV
3077
3078int hci_ethtool_ts_info(unsigned int index, int sk_proto,
3079 struct kernel_ethtool_ts_info *info)
3080{
3081 struct hci_dev *hdev;
3082
3083 hdev = hci_dev_get(index);
3084 if (!hdev)
3085 return -ENODEV;
3086
3087 info->so_timestamping =
3088 SOF_TIMESTAMPING_RX_SOFTWARE |
3089 SOF_TIMESTAMPING_SOFTWARE;
3090 info->phc_index = -1;
3091 info->tx_types = BIT(HWTSTAMP_TX_OFF);
3092 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE);
3093
3094 switch (sk_proto) {
3095 case BTPROTO_ISO:
3096 case BTPROTO_L2CAP:
3097 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
3098 info->so_timestamping |= SOF_TIMESTAMPING_TX_COMPLETION;
3099 break;
3100 case BTPROTO_SCO:
3101 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
3102 if (hci_dev_test_flag(hdev, HCI_SCO_FLOWCTL))
3103 info->so_timestamping |= SOF_TIMESTAMPING_TX_COMPLETION;
3104 break;
3105 }
3106
3107 hci_dev_put(hdev);
3108 return 0;
3109}