blob: 8e48ccd2af30e20b41b0358986a3b55e4213eff9 [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Ron Shaffer2d0a0342010-05-28 11:53:46 -04003 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
Iulia Tanasescu02171da2024-02-23 15:14:41 +02004 Copyright 2023-2024 NXP
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
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
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090016 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090021 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 SOFTWARE IS DISCLAIMED.
24*/
25
26/* Bluetooth HCI connection handling. */
27
Gustavo Padovan8c520a52012-05-23 04:04:22 -030028#include <linux/export.h>
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +010029#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <net/bluetooth/bluetooth.h>
32#include <net/bluetooth/hci_core.h>
Johan Hedberg4bc58f52014-05-20 09:45:47 +030033#include <net/bluetooth/l2cap.h>
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -080034#include <net/bluetooth/iso.h>
35#include <net/bluetooth/mgmt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Marcel Holtmannac4b7232013-10-10 14:54:16 -070037#include "smp.h"
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -080038#include "eir.h"
Marcel Holtmann70247282013-10-10 14:54:15 -070039
Frédéric Dalleau2dea6322013-08-19 14:24:03 +020040struct sco_param {
41 u16 pkt_type;
42 u16 max_latency;
Johan Hedbergc7da5792014-09-24 22:41:46 +030043 u8 retrans_effort;
Frédéric Dalleau2dea6322013-08-19 14:24:03 +020044};
45
Brian Gixe07a06b2022-08-05 16:42:31 -070046struct conn_handle_t {
47 struct hci_conn *conn;
48 __u16 handle;
49};
50
Bernhard Thaler48e68ff2014-09-23 11:01:07 +020051static const struct sco_param esco_param_cvsd[] = {
Johan Hedbergc7da5792014-09-24 22:41:46 +030052 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */
53 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */
54 { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */
55 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */
56 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +020057};
58
Bernhard Thaler48e68ff2014-09-23 11:01:07 +020059static const struct sco_param sco_param_cvsd[] = {
Johan Hedbergc7da5792014-09-24 22:41:46 +030060 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */
61 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */
Bernhard Thaler48e68ff2014-09-23 11:01:07 +020062};
63
Johan Hedberg565766b2014-09-25 09:48:01 +030064static const struct sco_param esco_param_msbc[] = {
Johan Hedbergc7da5792014-09-24 22:41:46 +030065 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */
66 { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +020067};
68
Jakub Pawlowskif75113a2015-08-07 20:22:53 +020069/* This function requires the caller holds hdev->lock */
Luiz Augusto von Dentz881559a2024-02-13 09:59:32 -050070void hci_connect_le_scan_cleanup(struct hci_conn *conn, u8 status)
Jakub Pawlowskif75113a2015-08-07 20:22:53 +020071{
72 struct hci_conn_params *params;
Johan Hedbergb5c2b622015-10-21 18:03:09 +030073 struct hci_dev *hdev = conn->hdev;
Jakub Pawlowskif75113a2015-08-07 20:22:53 +020074 struct smp_irk *irk;
75 bdaddr_t *bdaddr;
76 u8 bdaddr_type;
77
78 bdaddr = &conn->dst;
79 bdaddr_type = conn->dst_type;
80
81 /* Check if we need to convert to identity address */
Johan Hedbergb5c2b622015-10-21 18:03:09 +030082 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
Jakub Pawlowskif75113a2015-08-07 20:22:53 +020083 if (irk) {
84 bdaddr = &irk->bdaddr;
85 bdaddr_type = irk->addr_type;
86 }
87
Johan Hedberg17bc08f2015-10-21 18:03:10 +030088 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr,
89 bdaddr_type);
Luiz Augusto von Dentz19cf60b2023-03-24 10:57:55 -070090 if (!params)
Jakub Pawlowskif75113a2015-08-07 20:22:53 +020091 return;
92
Luiz Augusto von Dentz19cf60b2023-03-24 10:57:55 -070093 if (params->conn) {
94 hci_conn_drop(params->conn);
95 hci_conn_put(params->conn);
96 params->conn = NULL;
97 }
98
99 if (!params->explicit_connect)
100 return;
101
102 /* If the status indicates successful cancellation of
103 * the attempt (i.e. Unknown Connection Id) there's no point of
104 * notifying failure since we'll go back to keep trying to
105 * connect. The only exception is explicit connect requests
106 * where a timeout + cancel does indicate an actual failure.
107 */
108 if (status && status != HCI_ERROR_UNKNOWN_CONN_ID)
109 mgmt_connect_failed(hdev, &conn->dst, conn->type,
110 conn->dst_type, status);
111
Jakub Pawlowskif75113a2015-08-07 20:22:53 +0200112 /* 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 */
Johan Hedberg9ad3e6f2015-10-16 10:07:53 +0300117 params->explicit_connect = false;
118
Pauli Virtanen195ef752023-06-19 01:04:31 +0300119 hci_pend_le_list_del_init(params);
Johan Hedberg9ad3e6f2015-10-16 10:07:53 +0300120
121 switch (params->auto_connect) {
122 case HCI_AUTO_CONN_EXPLICIT:
Johan Hedbergb5c2b622015-10-21 18:03:09 +0300123 hci_conn_params_del(hdev, bdaddr, bdaddr_type);
Johan Hedberg9ad3e6f2015-10-16 10:07:53 +0300124 /* return instead of break to avoid duplicate scan update */
125 return;
126 case HCI_AUTO_CONN_DIRECT:
127 case HCI_AUTO_CONN_ALWAYS:
Pauli Virtanen195ef752023-06-19 01:04:31 +0300128 hci_pend_le_list_add(params, &hdev->pend_le_conns);
Johan Hedberg9ad3e6f2015-10-16 10:07:53 +0300129 break;
130 case HCI_AUTO_CONN_REPORT:
Pauli Virtanen195ef752023-06-19 01:04:31 +0300131 hci_pend_le_list_add(params, &hdev->pend_le_reports);
Johan Hedberg9ad3e6f2015-10-16 10:07:53 +0300132 break;
133 default:
134 break;
Jakub Pawlowski168b8a22015-10-16 10:07:49 +0300135 }
Johan Hedberg9ad3e6f2015-10-16 10:07:53 +0300136
Luiz Augusto von Dentz5bee2fd2021-10-27 16:58:43 -0700137 hci_update_passive_scan(hdev);
Jakub Pawlowskif75113a2015-08-07 20:22:53 +0200138}
139
Johan Hedbergb958f9a2015-10-16 10:07:50 +0300140static 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
Alain Michaud629f66a2022-06-02 15:30:03 +0000147 if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
148 hci_remove_link_key(hdev, &conn->dst);
149
Johan Hedbergb958f9a2015-10-16 10:07:50 +0300150 hci_chan_list_flush(conn);
151
152 hci_conn_hash_del(hdev, conn);
153
Ziyang Xuan181a42e2023-10-11 17:57:31 +0800154 if (HCI_CONN_HANDLE_UNSET(conn->handle))
155 ida_free(&hdev->unset_handle_ida, conn->handle);
156
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +0300157 if (conn->cleanup)
158 conn->cleanup(conn);
159
Sathish Narsimman1f8330e2020-04-03 21:43:58 +0200160 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 }
Johan Hedbergb958f9a2015-10-16 10:07:50 +0300172
Johan Hedbergb958f9a2015-10-16 10:07:50 +0300173 debugfs_remove_recursive(conn->debugfs);
174
ZhengHan Wanga85fb912023-10-18 12:30:55 +0200175 hci_conn_del_sysfs(conn);
Johan Hedbergb958f9a2015-10-16 10:07:50 +0300176
ZhengHan Wanga85fb912023-10-18 12:30:55 +0200177 hci_dev_put(hdev);
Johan Hedbergb958f9a2015-10-16 10:07:50 +0300178}
179
Johan Hedberge3b679d2014-08-18 20:33:32 +0300180int hci_disconnect(struct hci_conn *conn, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +0300182 BT_DBG("hcon %p", conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Archie Pusaka74be5232021-06-04 16:26:25 +0800184 /* When we are central of an established connection and it enters
Johan Hedberg839035a2014-08-18 20:33:34 +0300185 * 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 */
Johan Hedberg88d07fe2015-10-22 10:49:39 +0300189 if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER &&
190 (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) {
Johan Hedberg839035a2014-08-18 20:33:34 +0300191 struct hci_dev *hdev = conn->hdev;
Fabian Frederick4f639ed2014-10-25 10:48:58 +0200192 struct hci_cp_read_clock_offset clkoff_cp;
Johan Hedberg839035a2014-08-18 20:33:34 +0300193
Fabian Frederick4f639ed2014-10-25 10:48:58 +0200194 clkoff_cp.handle = cpu_to_le16(conn->handle);
195 hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp),
196 &clkoff_cp);
Johan Hedberg839035a2014-08-18 20:33:34 +0300197 }
198
Johan Hedberg88d07fe2015-10-22 10:49:39 +0300199 return hci_abort_conn(conn, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Vinicius Costa Gomes57f5d0d2012-07-27 19:32:54 -0300202static void hci_add_sco(struct hci_conn *conn, __u16 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct hci_dev *hdev = conn->hdev;
205 struct hci_cp_add_sco cp;
206
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +0300207 BT_DBG("hcon %p", conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 conn->state = BT_CONNECT;
Johan Hedberga0c808b2012-01-16 09:49:58 +0200210 conn->out = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Marcel Holtmannefc76882009-02-06 09:13:37 +0100212 conn->attempt++;
213
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700214 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200215 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200217 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Yu Liu8b1c3242021-01-29 13:53:48 -0800220static bool find_next_esco_param(struct hci_conn *conn,
221 const struct sco_param *esco_param, int size)
222{
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -0700223 if (!conn->parent)
224 return false;
225
Yu Liu8b1c3242021-01-29 13:53:48 -0800226 for (; conn->attempt <= size; conn->attempt++) {
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -0700227 if (lmp_esco_2m_capable(conn->parent) ||
Yu Liu8b1c3242021-01-29 13:53:48 -0800228 (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
Brian Gixe07a06b2022-08-05 16:42:31 -0700237static int configure_datapath_sync(struct hci_dev *hdev, struct bt_codec *codec)
Kiran Kb2af2642021-09-07 15:42:43 +0530238{
Brian Gixe07a06b2022-08-05 16:42:31 -0700239 int err;
240 __u8 vnd_len, *vnd_data = NULL;
241 struct hci_op_configure_data_path *cmd = NULL;
242
Zijun Hud68d8a72024-04-22 22:46:34 +0800243 /* 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 */
Zijun Hu132d0fd2023-12-08 09:51:26 +0800247 if (!codec->data_path || !hdev->get_codec_config_data)
248 return 0;
249
Brian Gixe07a06b2022-08-05 16:42:31 -0700250 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;
Kiran Kb2af2642021-09-07 15:42:43 +0530288 struct hci_cp_enhanced_setup_sync_conn cp;
289 const struct sco_param *param;
290
Brian Gixe07a06b2022-08-05 16:42:31 -0700291 kfree(conn_handle);
292
Kiran Kb2af2642021-09-07 15:42:43 +0530293 bt_dev_dbg(hdev, "hcon %p", conn);
294
Zijun Hu132d0fd2023-12-08 09:51:26 +0800295 configure_datapath_sync(hdev, &conn->codec);
Kiran K9798fbd2021-09-07 15:42:44 +0530296
Kiran Kb2af2642021-09-07 15:42:43 +0530297 conn->state = BT_CONNECT;
298 conn->out = true;
299
300 conn->attempt++;
301
302 memset(&cp, 0x00, sizeof(cp));
303
304 cp.handle = cpu_to_le16(handle);
305
306 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
307 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
308
309 switch (conn->codec.id) {
Kiran K904c1392021-09-07 15:42:46 +0530310 case BT_CODEC_MSBC:
311 if (!find_next_esco_param(conn, esco_param_msbc,
312 ARRAY_SIZE(esco_param_msbc)))
Brian Gixe07a06b2022-08-05 16:42:31 -0700313 return -EINVAL;
Kiran K904c1392021-09-07 15:42:46 +0530314
315 param = &esco_param_msbc[conn->attempt - 1];
316 cp.tx_coding_format.id = 0x05;
317 cp.rx_coding_format.id = 0x05;
318 cp.tx_codec_frame_size = __cpu_to_le16(60);
319 cp.rx_codec_frame_size = __cpu_to_le16(60);
320 cp.in_bandwidth = __cpu_to_le32(32000);
321 cp.out_bandwidth = __cpu_to_le32(32000);
322 cp.in_coding_format.id = 0x04;
323 cp.out_coding_format.id = 0x04;
324 cp.in_coded_data_size = __cpu_to_le16(16);
325 cp.out_coded_data_size = __cpu_to_le16(16);
326 cp.in_pcm_data_format = 2;
327 cp.out_pcm_data_format = 2;
328 cp.in_pcm_sample_payload_msb_pos = 0;
329 cp.out_pcm_sample_payload_msb_pos = 0;
330 cp.in_data_path = conn->codec.data_path;
331 cp.out_data_path = conn->codec.data_path;
332 cp.in_transport_unit_size = 1;
333 cp.out_transport_unit_size = 1;
334 break;
335
Kiran Kb2af2642021-09-07 15:42:43 +0530336 case BT_CODEC_TRANSPARENT:
337 if (!find_next_esco_param(conn, esco_param_msbc,
338 ARRAY_SIZE(esco_param_msbc)))
339 return false;
340 param = &esco_param_msbc[conn->attempt - 1];
341 cp.tx_coding_format.id = 0x03;
342 cp.rx_coding_format.id = 0x03;
343 cp.tx_codec_frame_size = __cpu_to_le16(60);
344 cp.rx_codec_frame_size = __cpu_to_le16(60);
345 cp.in_bandwidth = __cpu_to_le32(0x1f40);
346 cp.out_bandwidth = __cpu_to_le32(0x1f40);
347 cp.in_coding_format.id = 0x03;
348 cp.out_coding_format.id = 0x03;
349 cp.in_coded_data_size = __cpu_to_le16(16);
350 cp.out_coded_data_size = __cpu_to_le16(16);
351 cp.in_pcm_data_format = 2;
352 cp.out_pcm_data_format = 2;
353 cp.in_pcm_sample_payload_msb_pos = 0;
354 cp.out_pcm_sample_payload_msb_pos = 0;
355 cp.in_data_path = conn->codec.data_path;
356 cp.out_data_path = conn->codec.data_path;
357 cp.in_transport_unit_size = 1;
358 cp.out_transport_unit_size = 1;
359 break;
360
361 case BT_CODEC_CVSD:
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -0700362 if (conn->parent && lmp_esco_capable(conn->parent)) {
Kiran Kb2af2642021-09-07 15:42:43 +0530363 if (!find_next_esco_param(conn, esco_param_cvsd,
364 ARRAY_SIZE(esco_param_cvsd)))
Brian Gixe07a06b2022-08-05 16:42:31 -0700365 return -EINVAL;
Kiran Kb2af2642021-09-07 15:42:43 +0530366 param = &esco_param_cvsd[conn->attempt - 1];
367 } else {
368 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
Brian Gixe07a06b2022-08-05 16:42:31 -0700369 return -EINVAL;
Kiran Kb2af2642021-09-07 15:42:43 +0530370 param = &sco_param_cvsd[conn->attempt - 1];
371 }
372 cp.tx_coding_format.id = 2;
373 cp.rx_coding_format.id = 2;
374 cp.tx_codec_frame_size = __cpu_to_le16(60);
375 cp.rx_codec_frame_size = __cpu_to_le16(60);
376 cp.in_bandwidth = __cpu_to_le32(16000);
377 cp.out_bandwidth = __cpu_to_le32(16000);
378 cp.in_coding_format.id = 4;
379 cp.out_coding_format.id = 4;
380 cp.in_coded_data_size = __cpu_to_le16(16);
381 cp.out_coded_data_size = __cpu_to_le16(16);
382 cp.in_pcm_data_format = 2;
383 cp.out_pcm_data_format = 2;
384 cp.in_pcm_sample_payload_msb_pos = 0;
385 cp.out_pcm_sample_payload_msb_pos = 0;
386 cp.in_data_path = conn->codec.data_path;
387 cp.out_data_path = conn->codec.data_path;
388 cp.in_transport_unit_size = 16;
389 cp.out_transport_unit_size = 16;
390 break;
391 default:
Brian Gixe07a06b2022-08-05 16:42:31 -0700392 return -EINVAL;
Kiran Kb2af2642021-09-07 15:42:43 +0530393 }
394
395 cp.retrans_effort = param->retrans_effort;
396 cp.pkt_type = __cpu_to_le16(param->pkt_type);
397 cp.max_latency = __cpu_to_le16(param->max_latency);
398
399 if (hci_send_cmd(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
Brian Gixe07a06b2022-08-05 16:42:31 -0700400 return -EIO;
Kiran Kb2af2642021-09-07 15:42:43 +0530401
Brian Gixe07a06b2022-08-05 16:42:31 -0700402 return 0;
Kiran Kb2af2642021-09-07 15:42:43 +0530403}
404
405static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle)
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200406{
407 struct hci_dev *hdev = conn->hdev;
408 struct hci_cp_setup_sync_conn cp;
Frédéric Dalleau2dea6322013-08-19 14:24:03 +0200409 const struct sco_param *param;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200410
Kiran Kb2af2642021-09-07 15:42:43 +0530411 bt_dev_dbg(hdev, "hcon %p", conn);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200412
413 conn->state = BT_CONNECT;
Johan Hedberga0c808b2012-01-16 09:49:58 +0200414 conn->out = true;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200415
Marcel Holtmannefc76882009-02-06 09:13:37 +0100416 conn->attempt++;
417
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200418 cp.handle = cpu_to_le16(handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200419
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700420 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
421 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Frédéric Dalleau10c62dd2013-08-19 14:23:59 +0200422 cp.voice_setting = cpu_to_le16(conn->setting);
423
424 switch (conn->setting & SCO_AIRMODE_MASK) {
425 case SCO_AIRMODE_TRANSP:
Yu Liu8b1c3242021-01-29 13:53:48 -0800426 if (!find_next_esco_param(conn, esco_param_msbc,
427 ARRAY_SIZE(esco_param_msbc)))
Frédéric Dalleau2dea6322013-08-19 14:24:03 +0200428 return false;
Johan Hedberg565766b2014-09-25 09:48:01 +0300429 param = &esco_param_msbc[conn->attempt - 1];
Frédéric Dalleau10c62dd2013-08-19 14:23:59 +0200430 break;
431 case SCO_AIRMODE_CVSD:
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -0700432 if (conn->parent && lmp_esco_capable(conn->parent)) {
Yu Liu8b1c3242021-01-29 13:53:48 -0800433 if (!find_next_esco_param(conn, esco_param_cvsd,
434 ARRAY_SIZE(esco_param_cvsd)))
Bernhard Thaler48e68ff2014-09-23 11:01:07 +0200435 return false;
Bernhard Thaler48e68ff2014-09-23 11:01:07 +0200436 param = &esco_param_cvsd[conn->attempt - 1];
437 } else {
438 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
439 return false;
Bernhard Thaler48e68ff2014-09-23 11:01:07 +0200440 param = &sco_param_cvsd[conn->attempt - 1];
441 }
Frédéric Dalleau10c62dd2013-08-19 14:23:59 +0200442 break;
Frédéric Dalleau2dea6322013-08-19 14:24:03 +0200443 default:
444 return false;
Frédéric Dalleau10c62dd2013-08-19 14:23:59 +0200445 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200446
Johan Hedbergc7da5792014-09-24 22:41:46 +0300447 cp.retrans_effort = param->retrans_effort;
Frédéric Dalleau2dea6322013-08-19 14:24:03 +0200448 cp.pkt_type = __cpu_to_le16(param->pkt_type);
449 cp.max_latency = __cpu_to_le16(param->max_latency);
450
451 if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
452 return false;
453
454 return true;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200455}
456
Kiran Kb2af2642021-09-07 15:42:43 +0530457bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
458{
Brian Gixe07a06b2022-08-05 16:42:31 -0700459 int result;
460 struct conn_handle_t *conn_handle;
461
462 if (enhanced_sync_conn_capable(conn->hdev)) {
463 conn_handle = kzalloc(sizeof(*conn_handle), GFP_KERNEL);
464
465 if (!conn_handle)
466 return false;
467
468 conn_handle->conn = conn;
469 conn_handle->handle = handle;
470 result = hci_cmd_sync_queue(conn->hdev, hci_enhanced_setup_sync,
471 conn_handle, NULL);
472 if (result < 0)
473 kfree(conn_handle);
474
475 return result == 0;
476 }
Kiran Kb2af2642021-09-07 15:42:43 +0530477
478 return hci_setup_sync_conn(conn, handle);
479}
480
Johan Hedberg7d6ca692014-07-02 17:37:31 +0300481u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
482 u16 to_multiplier)
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200483{
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200484 struct hci_dev *hdev = conn->hdev;
Marcel Holtmannf044eb02014-06-29 16:43:26 +0200485 struct hci_conn_params *params;
486 struct hci_cp_le_conn_update cp;
487
488 hci_dev_lock(hdev);
489
490 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
491 if (params) {
492 params->conn_min_interval = min;
493 params->conn_max_interval = max;
494 params->conn_latency = latency;
495 params->supervision_timeout = to_multiplier;
496 }
497
498 hci_dev_unlock(hdev);
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200499
500 memset(&cp, 0, sizeof(cp));
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200501 cp.handle = cpu_to_le16(conn->handle);
502 cp.conn_interval_min = cpu_to_le16(min);
503 cp.conn_interval_max = cpu_to_le16(max);
504 cp.conn_latency = cpu_to_le16(latency);
505 cp.supervision_timeout = cpu_to_le16(to_multiplier);
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700506 cp.min_ce_len = cpu_to_le16(0x0000);
507 cp.max_ce_len = cpu_to_le16(0x0000);
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200508
509 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
Johan Hedberg7d6ca692014-07-02 17:37:31 +0300510
511 if (params)
512 return 0x01;
513
514 return 0x00;
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200515}
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200516
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800517void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
Johan Hedberg8b76ce32015-06-08 18:14:39 +0300518 __u8 ltk[16], __u8 key_size)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -0300519{
520 struct hci_dev *hdev = conn->hdev;
521 struct hci_cp_le_start_enc cp;
522
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +0300523 BT_DBG("hcon %p", conn);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -0300524
525 memset(&cp, 0, sizeof(cp));
526
527 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800528 cp.rand = rand;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -0300529 cp.ediv = ediv;
Johan Hedberg8b76ce32015-06-08 18:14:39 +0300530 memcpy(cp.ltk, ltk, key_size);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -0300531
532 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
533}
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -0300534
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400535/* Device _must_ be locked */
536void hci_sco_setup(struct hci_conn *conn, __u8 status)
537{
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -0700538 struct hci_link *link;
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400539
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -0700540 link = list_first_entry_or_null(&conn->link_list, struct hci_link, list);
541 if (!link || !link->conn)
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400542 return;
543
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +0300544 BT_DBG("hcon %p", conn);
545
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400546 if (!status) {
547 if (lmp_esco_capable(conn->hdev))
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -0700548 hci_setup_sync(link->conn, conn->handle);
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400549 else
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -0700550 hci_add_sco(link->conn, conn->handle);
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400551 } else {
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -0700552 hci_connect_cfm(link->conn, status);
553 hci_conn_del(link->conn);
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400554 }
555}
556
Gustavo F. Padovan19c40e32011-06-17 13:03:21 -0300557static void hci_conn_timeout(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Gustavo F. Padovan19c40e32011-06-17 13:03:21 -0300559 struct hci_conn *conn = container_of(work, struct hci_conn,
Gustavo Padovan5974e4c2012-05-17 00:36:25 -0300560 disc_work.work);
Lukasz Rymanowski1d56dc42014-06-17 13:04:20 +0200561 int refcnt = atomic_read(&conn->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +0300563 BT_DBG("hcon %p state %s", conn, state_to_string(conn->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Lukasz Rymanowski1d56dc42014-06-17 13:04:20 +0200565 WARN_ON(refcnt < 0);
566
567 /* FIXME: It was observed that in pairing failed scenario, refcnt
568 * drops below 0. Probably this is because l2cap_conn_del calls
569 * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is
570 * dropped. After that loop hci_chan_del is called which also drops
571 * conn. For now make sure that ACL is alive if refcnt is higher then 0,
572 * otherwise drop it.
573 */
574 if (refcnt > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return;
576
Johan Hedberg89e0ccc2015-10-22 10:49:38 +0300577 hci_abort_conn(conn, hci_proto_disconn_ind(conn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Gustavo F. Padovan416dc942011-12-07 13:24:33 -0200580/* Enter sniff mode */
Johan Hedberga74a84f2013-10-16 18:11:40 +0300581static void hci_conn_idle(struct work_struct *work)
Gustavo F. Padovan416dc942011-12-07 13:24:33 -0200582{
Johan Hedberga74a84f2013-10-16 18:11:40 +0300583 struct hci_conn *conn = container_of(work, struct hci_conn,
584 idle_work.work);
Gustavo F. Padovan416dc942011-12-07 13:24:33 -0200585 struct hci_dev *hdev = conn->hdev;
586
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +0300587 BT_DBG("hcon %p mode %d", conn, conn->mode);
Gustavo F. Padovan416dc942011-12-07 13:24:33 -0200588
Gustavo F. Padovan416dc942011-12-07 13:24:33 -0200589 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
590 return;
591
592 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
593 return;
594
595 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
596 struct hci_cp_sniff_subrate cp;
597 cp.handle = cpu_to_le16(conn->handle);
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700598 cp.max_latency = cpu_to_le16(0);
599 cp.min_remote_timeout = cpu_to_le16(0);
600 cp.min_local_timeout = cpu_to_le16(0);
Gustavo F. Padovan416dc942011-12-07 13:24:33 -0200601 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
602 }
603
Johan Hedberg51a8efd2012-01-16 06:10:31 +0200604 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
Gustavo F. Padovan416dc942011-12-07 13:24:33 -0200605 struct hci_cp_sniff_mode cp;
606 cp.handle = cpu_to_le16(conn->handle);
607 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
608 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700609 cp.attempt = cpu_to_le16(4);
610 cp.timeout = cpu_to_le16(1);
Gustavo F. Padovan416dc942011-12-07 13:24:33 -0200611 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
612 }
613}
614
Johan Hedberg7bc18d92013-10-16 18:11:39 +0300615static void hci_conn_auto_accept(struct work_struct *work)
Johan Hedberg9f616562011-04-28 11:28:54 -0700616{
Johan Hedberg7bc18d92013-10-16 18:11:39 +0300617 struct hci_conn *conn = container_of(work, struct hci_conn,
618 auto_accept_work.work);
Johan Hedberg9f616562011-04-28 11:28:54 -0700619
Johan Hedberg7bc18d92013-10-16 18:11:39 +0300620 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
Gustavo Padovan5974e4c2012-05-17 00:36:25 -0300621 &conn->dst);
Johan Hedberg9f616562011-04-28 11:28:54 -0700622}
623
Sathish Narsimmanc3bed4d2020-02-24 10:53:40 +0530624static void le_disable_advertising(struct hci_dev *hdev)
625{
626 if (ext_adv_capable(hdev)) {
627 struct hci_cp_le_set_ext_adv_enable cp;
628
629 cp.enable = 0x00;
630 cp.num_of_sets = 0x00;
631
632 hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp),
633 &cp);
634 } else {
635 u8 enable = 0x00;
636 hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
637 &enable);
638 }
639}
640
Johan Hedberg9489eca2014-02-28 17:45:46 +0200641static void le_conn_timeout(struct work_struct *work)
642{
643 struct hci_conn *conn = container_of(work, struct hci_conn,
644 le_conn_timeout.work);
Johan Hedberg3c857752014-03-25 10:30:49 +0200645 struct hci_dev *hdev = conn->hdev;
Johan Hedberg9489eca2014-02-28 17:45:46 +0200646
647 BT_DBG("");
648
Johan Hedberg3c857752014-03-25 10:30:49 +0200649 /* We could end up here due to having done directed advertising,
650 * so clean up the state if necessary. This should however only
651 * happen with broken hardware or if low duty cycle was used
652 * (which doesn't have a timeout of its own).
653 */
Johan Hedberg0b1db38c2014-10-28 22:23:27 +0100654 if (conn->role == HCI_ROLE_SLAVE) {
Sathish Narsimmanc3bed4d2020-02-24 10:53:40 +0530655 /* Disable LE Advertising */
656 le_disable_advertising(hdev);
Niels Dossche9fa6b4c2022-03-16 16:33:50 +0100657 hci_dev_lock(hdev);
Luiz Augusto von Dentz9b3628d2022-04-22 12:58:18 -0700658 hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
Niels Dossche9fa6b4c2022-03-16 16:33:50 +0100659 hci_dev_unlock(hdev);
Johan Hedberg3c857752014-03-25 10:30:49 +0200660 return;
661 }
662
Johan Hedberg89e0ccc2015-10-22 10:49:38 +0300663 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberg9489eca2014-02-28 17:45:46 +0200664}
665
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800666struct iso_list_data {
667 union {
668 u8 cig;
669 u8 big;
670 };
671 union {
672 u8 cis;
673 u8 bis;
674 u16 sync_handle;
675 };
676 int count;
Iulia Tanasescua0bfde12023-05-30 17:21:59 +0300677 bool big_term;
Iulia Tanasescufbdc4bc2023-08-17 09:44:27 +0300678 bool pa_sync_term;
Iulia Tanasescuf777d882023-07-03 10:02:38 +0300679 bool big_sync_term;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800680};
681
682static void bis_list(struct hci_conn *conn, void *data)
683{
684 struct iso_list_data *d = data;
685
686 /* Skip if not broadcast/ANY address */
687 if (bacmp(&conn->dst, BDADDR_ANY))
688 return;
689
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +0300690 if (d->big != conn->iso_qos.bcast.big || d->bis == BT_ISO_QOS_BIS_UNSET ||
691 d->bis != conn->iso_qos.bcast.bis)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800692 return;
693
694 d->count++;
695}
696
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800697static int terminate_big_sync(struct hci_dev *hdev, void *data)
698{
699 struct iso_list_data *d = data;
700
701 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", d->big, d->bis);
702
Iulia Tanasescua254b902023-09-06 16:59:54 +0300703 hci_disable_per_advertising_sync(hdev, d->bis);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800704 hci_remove_ext_adv_instance_sync(hdev, d->bis, NULL);
705
Iulia Tanasescua0bfde12023-05-30 17:21:59 +0300706 /* Only terminate BIG if it has been created */
707 if (!d->big_term)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800708 return 0;
709
710 return hci_le_terminate_big_sync(hdev, d->big,
711 HCI_ERROR_LOCAL_HOST_TERM);
712}
713
714static void terminate_big_destroy(struct hci_dev *hdev, void *data, int err)
715{
716 kfree(data);
717}
718
Iulia Tanasescua0bfde12023-05-30 17:21:59 +0300719static int hci_le_terminate_big(struct hci_dev *hdev, struct hci_conn *conn)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800720{
721 struct iso_list_data *d;
Zhengchao Shao3aa21312023-01-04 14:46:23 +0800722 int ret;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800723
Iulia Tanasescua0bfde12023-05-30 17:21:59 +0300724 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", conn->iso_qos.bcast.big,
725 conn->iso_qos.bcast.bis);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800726
Kang Minchul3958e8772022-10-31 03:17:22 +0900727 d = kzalloc(sizeof(*d), GFP_KERNEL);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800728 if (!d)
729 return -ENOMEM;
730
Iulia Tanasescua0bfde12023-05-30 17:21:59 +0300731 d->big = conn->iso_qos.bcast.big;
732 d->bis = conn->iso_qos.bcast.bis;
733 d->big_term = test_and_clear_bit(HCI_CONN_BIG_CREATED, &conn->flags);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800734
Zhengchao Shao3aa21312023-01-04 14:46:23 +0800735 ret = hci_cmd_sync_queue(hdev, terminate_big_sync, d,
736 terminate_big_destroy);
737 if (ret)
738 kfree(d);
739
740 return ret;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800741}
742
743static int big_terminate_sync(struct hci_dev *hdev, void *data)
744{
745 struct iso_list_data *d = data;
746
747 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", d->big,
748 d->sync_handle);
749
Iulia Tanasescuf777d882023-07-03 10:02:38 +0300750 if (d->big_sync_term)
751 hci_le_big_terminate_sync(hdev, d->big);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800752
Iulia Tanasescufbdc4bc2023-08-17 09:44:27 +0300753 if (d->pa_sync_term)
754 return hci_le_pa_terminate_sync(hdev, d->sync_handle);
755
756 return 0;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800757}
758
Iulia Tanasescufcb89f12023-10-11 17:24:07 +0300759static void find_bis(struct hci_conn *conn, void *data)
760{
761 struct iso_list_data *d = data;
762
763 /* Ignore if BIG doesn't match */
764 if (d->big != conn->iso_qos.bcast.big)
765 return;
766
767 d->count++;
768}
769
Iulia Tanasescuf777d882023-07-03 10:02:38 +0300770static int hci_le_big_terminate(struct hci_dev *hdev, u8 big, struct hci_conn *conn)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800771{
772 struct iso_list_data *d;
Zhengchao Shao3aa21312023-01-04 14:46:23 +0800773 int ret;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800774
Iulia Tanasescuf777d882023-07-03 10:02:38 +0300775 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", big, conn->sync_handle);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800776
Kang Minchul3958e8772022-10-31 03:17:22 +0900777 d = kzalloc(sizeof(*d), GFP_KERNEL);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800778 if (!d)
779 return -ENOMEM;
780
Iulia Tanasescufcb89f12023-10-11 17:24:07 +0300781 memset(d, 0, sizeof(*d));
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800782 d->big = big;
Iulia Tanasescuf777d882023-07-03 10:02:38 +0300783 d->sync_handle = conn->sync_handle;
Iulia Tanasescufcb89f12023-10-11 17:24:07 +0300784
785 if (test_and_clear_bit(HCI_CONN_PA_SYNC, &conn->flags)) {
786 hci_conn_hash_list_flag(hdev, find_bis, ISO_LINK,
787 HCI_CONN_PA_SYNC, d);
788
789 if (!d->count)
790 d->pa_sync_term = true;
791
792 d->count = 0;
793 }
794
795 if (test_and_clear_bit(HCI_CONN_BIG_SYNC, &conn->flags)) {
796 hci_conn_hash_list_flag(hdev, find_bis, ISO_LINK,
797 HCI_CONN_BIG_SYNC, d);
798
799 if (!d->count)
800 d->big_sync_term = true;
801 }
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800802
Zhengchao Shao3aa21312023-01-04 14:46:23 +0800803 ret = hci_cmd_sync_queue(hdev, big_terminate_sync, d,
804 terminate_big_destroy);
805 if (ret)
806 kfree(d);
807
808 return ret;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800809}
810
811/* Cleanup BIS connection
812 *
813 * Detects if there any BIS left connected in a BIG
814 * broadcaster: Remove advertising instance and terminate BIG.
815 * broadcaster receiver: Teminate BIG sync and terminate PA sync.
816 */
817static void bis_cleanup(struct hci_conn *conn)
818{
819 struct hci_dev *hdev = conn->hdev;
Iulia Tanasescua0bfde12023-05-30 17:21:59 +0300820 struct hci_conn *bis;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800821
822 bt_dev_dbg(hdev, "conn %p", conn);
823
824 if (conn->role == HCI_ROLE_MASTER) {
825 if (!test_and_clear_bit(HCI_CONN_PER_ADV, &conn->flags))
826 return;
827
Iulia Tanasescua0bfde12023-05-30 17:21:59 +0300828 /* Check if ISO connection is a BIS and terminate advertising
829 * set and BIG if there are no other connections using it.
830 */
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +0300831 bis = hci_conn_hash_lookup_big(hdev, conn->iso_qos.bcast.big);
Iulia Tanasescua0bfde12023-05-30 17:21:59 +0300832 if (bis)
833 return;
834
835 hci_le_terminate_big(hdev, conn);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800836 } else {
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +0300837 hci_le_big_terminate(hdev, conn->iso_qos.bcast.big,
Iulia Tanasescuf777d882023-07-03 10:02:38 +0300838 conn);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800839 }
840}
841
842static int remove_cig_sync(struct hci_dev *hdev, void *data)
843{
Luiz Augusto von Dentza1f6c3a2023-08-04 16:23:41 -0700844 u8 handle = PTR_UINT(data);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800845
846 return hci_le_remove_cig_sync(hdev, handle);
847}
848
849static int hci_le_remove_cig(struct hci_dev *hdev, u8 handle)
850{
851 bt_dev_dbg(hdev, "handle 0x%2.2x", handle);
852
Luiz Augusto von Dentza1f6c3a2023-08-04 16:23:41 -0700853 return hci_cmd_sync_queue(hdev, remove_cig_sync, UINT_PTR(handle),
854 NULL);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800855}
856
857static void find_cis(struct hci_conn *conn, void *data)
858{
859 struct iso_list_data *d = data;
860
Pauli Virtanen31c5f912023-05-21 15:48:28 +0000861 /* Ignore broadcast or if CIG don't match */
862 if (!bacmp(&conn->dst, BDADDR_ANY) || d->cig != conn->iso_qos.ucast.cig)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800863 return;
864
865 d->count++;
866}
867
868/* Cleanup CIS connection:
869 *
870 * Detects if there any CIS left connected in a CIG and remove it.
871 */
872static void cis_cleanup(struct hci_conn *conn)
873{
874 struct hci_dev *hdev = conn->hdev;
875 struct iso_list_data d;
876
Pauli Virtanen31c5f912023-05-21 15:48:28 +0000877 if (conn->iso_qos.ucast.cig == BT_ISO_QOS_CIG_UNSET)
878 return;
879
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800880 memset(&d, 0, sizeof(d));
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +0300881 d.cig = conn->iso_qos.ucast.cig;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800882
883 /* Check if ISO connection is a CIS and remove CIG if there are
884 * no other connections using it.
885 */
Pauli Virtanen6c242c62023-06-01 09:34:44 +0300886 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_BOUND, &d);
887 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_CONNECT, &d);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800888 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_CONNECTED, &d);
889 if (d.count)
890 return;
891
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +0300892 hci_le_remove_cig(hdev, conn->iso_qos.ucast.cig);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800893}
894
Ziyang Xuan181a42e2023-10-11 17:57:31 +0800895static int hci_conn_hash_alloc_unset(struct hci_dev *hdev)
Luiz Augusto von Dentz9f781912023-06-28 12:15:53 -0700896{
Ziyang Xuan181a42e2023-10-11 17:57:31 +0800897 return ida_alloc_range(&hdev->unset_handle_ida, HCI_CONN_HANDLE_MAX + 1,
898 U16_MAX, GFP_ATOMIC);
Luiz Augusto von Dentz9f781912023-06-28 12:15:53 -0700899}
900
Pavel Skripkin1cc18c22024-06-20 22:27:47 +0300901static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
902 u8 role, u16 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 struct hci_conn *conn;
905
Sungwoo Kima5b862c2024-05-04 15:23:29 -0400906 switch (type) {
907 case ACL_LINK:
908 if (!hdev->acl_mtu)
909 return ERR_PTR(-ECONNREFUSED);
910 break;
911 case ISO_LINK:
912 if (hdev->iso_mtu)
913 /* Dedicated ISO Buffer exists */
914 break;
915 fallthrough;
916 case LE_LINK:
917 if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
918 return ERR_PTR(-ECONNREFUSED);
919 if (!hdev->le_mtu && hdev->acl_mtu < HCI_MIN_LE_MTU)
920 return ERR_PTR(-ECONNREFUSED);
921 break;
922 case SCO_LINK:
923 case ESCO_LINK:
924 if (!hdev->sco_pkts)
925 /* Controller does not support SCO or eSCO over HCI */
926 return ERR_PTR(-ECONNREFUSED);
927 break;
928 default:
929 return ERR_PTR(-ECONNREFUSED);
930 }
931
Ziyang Xuan181a42e2023-10-11 17:57:31 +0800932 bt_dev_dbg(hdev, "dst %pMR handle 0x%4.4x", dst, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Johan Hedberg27f70f32014-07-21 10:50:06 +0300934 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200935 if (!conn)
Sungwoo Kima5b862c2024-05-04 15:23:29 -0400936 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 bacpy(&conn->dst, dst);
Marcel Holtmann662e8822013-10-13 05:23:59 -0700939 bacpy(&conn->src, &hdev->bdaddr);
Ziyang Xuan181a42e2023-10-11 17:57:31 +0800940 conn->handle = handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200941 conn->hdev = hdev;
942 conn->type = type;
Johan Hedberga5c4e302014-07-16 11:56:07 +0300943 conn->role = role;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200944 conn->mode = HCI_CM_ACTIVE;
945 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300946 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200947 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300948 conn->remote_auth = 0xff;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200949 conn->key_type = 0xff;
Johan Hedbergebf86aa2014-12-05 13:36:08 +0200950 conn->rssi = HCI_RSSI_INVALID;
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +0200951 conn->tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +0200952 conn->max_tx_power = HCI_TX_POWER_INVALID;
Iulia Tanasescu1d11d702023-09-06 17:01:03 +0300953 conn->sync_handle = HCI_SYNC_HANDLE_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Johan Hedberg58a681e2012-01-16 06:47:28 +0200955 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200956 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200957
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +0530958 /* Set Default Authenticated payload timeout to 30s */
959 conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT;
960
Johan Hedberga5c4e302014-07-16 11:56:07 +0300961 if (conn->role == HCI_ROLE_MASTER)
962 conn->out = true;
963
Marcel Holtmanna8746412008-07-14 20:13:46 +0200964 switch (type) {
965 case ACL_LINK:
966 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
Sungwoo Kima5b862c2024-05-04 15:23:29 -0400967 conn->mtu = hdev->acl_mtu;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200968 break;
Johan Hedberg9c84d1d2014-03-24 20:21:50 +0200969 case LE_LINK:
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800970 /* conn->src should reflect the local identity address */
971 hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
Sungwoo Kima5b862c2024-05-04 15:23:29 -0400972 conn->mtu = hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800973 break;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +0300974 case ISO_LINK:
Johan Hedberg9c84d1d2014-03-24 20:21:50 +0200975 /* conn->src should reflect the local identity address */
976 hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -0800977
978 /* set proper cleanup function */
979 if (!bacmp(dst, BDADDR_ANY))
980 conn->cleanup = bis_cleanup;
981 else if (conn->role == HCI_ROLE_MASTER)
982 conn->cleanup = cis_cleanup;
983
Sungwoo Kima5b862c2024-05-04 15:23:29 -0400984 conn->mtu = hdev->iso_mtu ? hdev->iso_mtu :
985 hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu;
Johan Hedberg9c84d1d2014-03-24 20:21:50 +0200986 break;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200987 case SCO_LINK:
988 if (lmp_esco_capable(hdev))
Marcel Holtmannefc76882009-02-06 09:13:37 +0100989 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
990 (hdev->esco_type & EDR_ESCO_MASK);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200991 else
992 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
Sungwoo Kima5b862c2024-05-04 15:23:29 -0400993
994 conn->mtu = hdev->sco_mtu;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200995 break;
996 case ESCO_LINK:
Marcel Holtmannefc76882009-02-06 09:13:37 +0100997 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
Sungwoo Kima5b862c2024-05-04 15:23:29 -0400998 conn->mtu = hdev->sco_mtu;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200999 break;
1000 }
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001003
Marcel Holtmann70c1f202012-02-22 12:06:43 +01001004 INIT_LIST_HEAD(&conn->chan_list);
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001005 INIT_LIST_HEAD(&conn->link_list);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001006
Gustavo F. Padovan19c40e32011-06-17 13:03:21 -03001007 INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03001008 INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
Johan Hedberga74a84f2013-10-16 18:11:40 +03001009 INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
Johan Hedberg9489eca2014-02-28 17:45:46 +02001010 INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 atomic_set(&conn->refcnt, 0);
1013
1014 hci_dev_hold(hdev);
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 hci_conn_hash_add(hdev, conn);
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02001017
1018 /* The SCO and eSCO connections will only be notified when their
1019 * setup has been completed. This is different to ACL links which
1020 * can be notified right away.
1021 */
1022 if (conn->type != SCO_LINK && conn->type != ESCO_LINK) {
1023 if (hdev->notify)
1024 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Marcel Holtmanna67e8992009-05-02 18:24:06 -07001027 hci_conn_init_sysfs(conn);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return conn;
1030}
1031
Ziyang Xuan181a42e2023-10-11 17:57:31 +08001032struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type,
1033 bdaddr_t *dst, u8 role)
1034{
1035 int handle;
1036
1037 bt_dev_dbg(hdev, "dst %pMR", dst);
1038
1039 handle = hci_conn_hash_alloc_unset(hdev);
1040 if (unlikely(handle < 0))
Sungwoo Kima5b862c2024-05-04 15:23:29 -04001041 return ERR_PTR(-ECONNREFUSED);
Ziyang Xuan181a42e2023-10-11 17:57:31 +08001042
Pavel Skripkin1cc18c22024-06-20 22:27:47 +03001043 return __hci_conn_add(hdev, type, dst, role, handle);
1044}
1045
1046struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
1047 u8 role, u16 handle)
1048{
1049 if (handle > HCI_CONN_HANDLE_MAX)
1050 return ERR_PTR(-EINVAL);
1051
1052 return __hci_conn_add(hdev, type, dst, role, handle);
Ziyang Xuan181a42e2023-10-11 17:57:31 +08001053}
1054
Pauli Virtanen3344d312023-08-19 16:33:36 +03001055static void hci_conn_cleanup_child(struct hci_conn *conn, u8 reason)
1056{
1057 if (!reason)
1058 reason = HCI_ERROR_REMOTE_USER_TERM;
1059
1060 /* Due to race, SCO/ISO conn might be not established yet at this point,
1061 * and nothing else will clean it up. In other cases it is done via HCI
1062 * events.
1063 */
1064 switch (conn->type) {
1065 case SCO_LINK:
1066 case ESCO_LINK:
1067 if (HCI_CONN_HANDLE_UNSET(conn->handle))
1068 hci_conn_failed(conn, reason);
1069 break;
1070 case ISO_LINK:
Iulia Tanasescufa224d02023-11-13 17:38:00 +02001071 if ((conn->state != BT_CONNECTED &&
1072 !test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) ||
1073 test_bit(HCI_CONN_BIG_CREATED, &conn->flags))
Pauli Virtanen3344d312023-08-19 16:33:36 +03001074 hci_conn_failed(conn, reason);
1075 break;
1076 }
1077}
1078
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001079static void hci_conn_unlink(struct hci_conn *conn)
Luiz Augusto von Dentz5dc7d232023-04-03 14:19:14 -07001080{
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001081 struct hci_dev *hdev = conn->hdev;
Luiz Augusto von Dentz5dc7d232023-04-03 14:19:14 -07001082
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001083 bt_dev_dbg(hdev, "hcon %p", conn);
1084
1085 if (!conn->parent) {
1086 struct hci_link *link, *t;
1087
Ruihan Lica1fd422023-05-03 21:39:34 +08001088 list_for_each_entry_safe(link, t, &conn->link_list, list) {
1089 struct hci_conn *child = link->conn;
1090
1091 hci_conn_unlink(child);
1092
Ruihan Lia2ac5912023-05-03 21:39:36 +08001093 /* If hdev is down it means
1094 * hci_dev_close_sync/hci_conn_hash_flush is in progress
1095 * and links don't need to be cleanup as all connections
1096 * would be cleanup.
1097 */
1098 if (!test_bit(HCI_UP, &hdev->flags))
1099 continue;
1100
Pauli Virtanen3344d312023-08-19 16:33:36 +03001101 hci_conn_cleanup_child(child, conn->abort_reason);
Ruihan Lica1fd422023-05-03 21:39:34 +08001102 }
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001103
1104 return;
1105 }
1106
1107 if (!conn->link)
1108 return;
1109
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001110 list_del_rcu(&conn->link->list);
1111 synchronize_rcu();
1112
Ruihan Lia2904d22023-05-03 21:39:37 +08001113 hci_conn_drop(conn->parent);
Ruihan Li29104312023-05-03 21:39:35 +08001114 hci_conn_put(conn->parent);
1115 conn->parent = NULL;
1116
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001117 kfree(conn->link);
Luiz Augusto von Dentz5dc7d232023-04-03 14:19:14 -07001118 conn->link = NULL;
Luiz Augusto von Dentz5dc7d232023-04-03 14:19:14 -07001119}
1120
Ruihan Lia2ac5912023-05-03 21:39:36 +08001121void hci_conn_del(struct hci_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 struct hci_dev *hdev = conn->hdev;
1124
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +03001125 BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Ruihan Lia2904d22023-05-03 21:39:37 +08001127 hci_conn_unlink(conn);
1128
Gustavo F. Padovan19c40e32011-06-17 13:03:21 -03001129 cancel_delayed_work_sync(&conn->disc_work);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03001130 cancel_delayed_work_sync(&conn->auto_accept_work);
Johan Hedberga74a84f2013-10-16 18:11:40 +03001131 cancel_delayed_work_sync(&conn->idle_work);
Johan Hedberg9f616562011-04-28 11:28:54 -07001132
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001133 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /* Unacked frames */
1135 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001136 } else if (conn->type == LE_LINK) {
Johan Hedberg980ffc02014-10-28 22:23:26 +01001137 cancel_delayed_work(&conn->le_conn_timeout);
Johan Hedberg9489eca2014-02-28 17:45:46 +02001138
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001139 if (hdev->le_pkts)
1140 hdev->le_cnt += conn->sent;
1141 else
1142 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001143 } else {
Luiz Augusto von Dentz5638d9e2022-10-17 15:36:23 -07001144 /* Unacked ISO frames */
1145 if (conn->type == ISO_LINK) {
1146 if (hdev->iso_pkts)
1147 hdev->iso_cnt += conn->sent;
1148 else if (hdev->le_pkts)
1149 hdev->le_cnt += conn->sent;
1150 else
1151 hdev->acl_cnt += conn->sent;
1152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Johan Hedbergb958f9a2015-10-16 10:07:50 +03001157 /* Remove the connection from the list and cleanup its remaining
1158 * state. This is a separate function since for some cases like
1159 * BT_CONNECT_SCAN we *only* want the cleanup part without the
1160 * rest of hci_conn_del.
1161 */
1162 hci_conn_cleanup(conn);
Luiz Augusto von Dentz881559a2024-02-13 09:59:32 -05001163
1164 /* Dequeue callbacks using connection pointer as data */
1165 hci_cmd_sync_dequeue(hdev, NULL, conn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Johan Hedberg39385cb2016-11-12 17:03:07 +02001168struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 int use_src = bacmp(src, BDADDR_ANY);
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001171 struct hci_dev *hdev = NULL, *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001173 BT_DBG("%pMR -> %pMR", src, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001175 read_lock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02001177 list_for_each_entry(d, &hci_dev_list, list) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03001178 if (!test_bit(HCI_UP, &d->flags) ||
Luiz Augusto von Dentz84a4bb62024-05-06 18:33:52 -04001179 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 continue;
1181
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001182 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 * No source address - find interface with bdaddr != dst
1184 * Source address - find interface with bdaddr == src
1185 */
1186
1187 if (use_src) {
Johan Hedberg39385cb2016-11-12 17:03:07 +02001188 bdaddr_t id_addr;
1189 u8 id_addr_type;
1190
1191 if (src_type == BDADDR_BREDR) {
1192 if (!lmp_bredr_capable(d))
1193 continue;
1194 bacpy(&id_addr, &d->bdaddr);
1195 id_addr_type = BDADDR_BREDR;
1196 } else {
1197 if (!lmp_le_capable(d))
1198 continue;
1199
1200 hci_copy_identity_address(d, &id_addr,
1201 &id_addr_type);
1202
1203 /* Convert from HCI to three-value type */
1204 if (id_addr_type == ADDR_LE_DEV_PUBLIC)
1205 id_addr_type = BDADDR_LE_PUBLIC;
1206 else
1207 id_addr_type = BDADDR_LE_RANDOM;
1208 }
1209
1210 if (!bacmp(&id_addr, src) && id_addr_type == src_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 hdev = d; break;
1212 }
1213 } else {
1214 if (bacmp(&d->bdaddr, dst)) {
1215 hdev = d; break;
1216 }
1217 }
1218 }
1219
1220 if (hdev)
1221 hdev = hci_dev_hold(hdev);
1222
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001223 read_unlock(&hci_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return hdev;
1225}
1226EXPORT_SYMBOL(hci_get_route);
1227
Andre Guedes9bb3c012014-01-30 18:22:08 -03001228/* This function requires the caller holds hdev->lock */
Luiz Augusto von Dentz9b3628d2022-04-22 12:58:18 -07001229static void hci_le_conn_failed(struct hci_conn *conn, u8 status)
Andre Guedes9bb3c012014-01-30 18:22:08 -03001230{
1231 struct hci_dev *hdev = conn->hdev;
Johan Hedbergf161dd42014-08-15 21:06:54 +03001232
Luiz Augusto von Dentz19cf60b2023-03-24 10:57:55 -07001233 hci_connect_le_scan_cleanup(conn, status);
Johan Hedberg3c857752014-03-25 10:30:49 +02001234
Luiz Augusto von Dentzabfeea42021-10-27 16:58:45 -07001235 /* Enable advertising in case this was a failed connection
Johan Hedberg3c857752014-03-25 10:30:49 +02001236 * attempt as a peripheral.
1237 */
Luiz Augusto von Dentzabfeea42021-10-27 16:58:45 -07001238 hci_enable_advertising(hdev);
Andre Guedes9bb3c012014-01-30 18:22:08 -03001239}
1240
Luiz Augusto von Dentz9b3628d2022-04-22 12:58:18 -07001241/* This function requires the caller holds hdev->lock */
1242void hci_conn_failed(struct hci_conn *conn, u8 status)
1243{
1244 struct hci_dev *hdev = conn->hdev;
1245
1246 bt_dev_dbg(hdev, "status 0x%2.2x", status);
1247
1248 switch (conn->type) {
1249 case LE_LINK:
1250 hci_le_conn_failed(conn, status);
1251 break;
1252 case ACL_LINK:
1253 mgmt_connect_failed(hdev, &conn->dst, conn->type,
1254 conn->dst_type, status);
1255 break;
1256 }
1257
Iulia Tanasescua254b902023-09-06 16:59:54 +03001258 /* In case of BIG/PA sync failed, clear conn flags so that
1259 * the conns will be correctly cleaned up by ISO layer
1260 */
1261 test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags);
1262 test_and_clear_bit(HCI_CONN_PA_SYNC_FAILED, &conn->flags);
1263
Luiz Augusto von Dentz9b3628d2022-04-22 12:58:18 -07001264 conn->state = BT_CLOSED;
1265 hci_connect_cfm(conn, status);
1266 hci_conn_del(conn);
1267}
1268
Luiz Augusto von Dentz16e3b642023-08-03 14:49:14 -07001269/* This function requires the caller holds hdev->lock */
1270u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle)
1271{
1272 struct hci_dev *hdev = conn->hdev;
1273
1274 bt_dev_dbg(hdev, "hcon %p handle 0x%4.4x", conn, handle);
1275
1276 if (conn->handle == handle)
1277 return 0;
1278
1279 if (handle > HCI_CONN_HANDLE_MAX) {
1280 bt_dev_err(hdev, "Invalid handle: 0x%4.4x > 0x%4.4x",
1281 handle, HCI_CONN_HANDLE_MAX);
1282 return HCI_ERROR_INVALID_PARAMETERS;
1283 }
1284
1285 /* If abort_reason has been sent it means the connection is being
1286 * aborted and the handle shall not be changed.
1287 */
1288 if (conn->abort_reason)
1289 return conn->abort_reason;
1290
Ziyang Xuan181a42e2023-10-11 17:57:31 +08001291 if (HCI_CONN_HANDLE_UNSET(conn->handle))
1292 ida_free(&hdev->unset_handle_ida, conn->handle);
1293
Luiz Augusto von Dentz16e3b642023-08-03 14:49:14 -07001294 conn->handle = handle;
1295
1296 return 0;
1297}
1298
Andre Guedes04a6c582014-02-26 20:21:44 -03001299struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07001300 u8 dst_type, bool dst_resolved, u8 sec_level,
Luiz Augusto von Dentz2e7ed5f2024-04-05 16:40:33 -04001301 u16 conn_timeout, u8 role, u8 phy, u8 sec_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Johan Hedberge2caced2015-11-11 14:44:59 +02001303 struct hci_conn *conn;
Johan Hedberg1ebfcc12014-02-18 21:41:36 +02001304 struct smp_irk *irk;
Andre Guedes1d399ae2013-10-08 08:21:17 -03001305 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Lukasz Rymanowski152d3862015-02-11 12:31:40 +01001307 /* Let's make sure that le is enabled.*/
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001308 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Lukasz Rymanowski152d3862015-02-11 12:31:40 +01001309 if (lmp_le_capable(hdev))
1310 return ERR_PTR(-ECONNREFUSED);
1311
1312 return ERR_PTR(-EOPNOTSUPP);
1313 }
1314
Johan Hedberg658aead2015-11-11 14:44:58 +02001315 /* Since the controller supports only one LE connection attempt at a
1316 * time, we return -EBUSY if there is any connection attempt running.
1317 */
1318 if (hci_lookup_le_connect(hdev))
1319 return ERR_PTR(-EBUSY);
1320
Johan Hedberge2caced2015-11-11 14:44:59 +02001321 /* If there's already a connection object but it's not in
1322 * scanning state it means it must already be established, in
1323 * which case we can't do anything else except report a failure
1324 * to connect.
Andre Guedes620ad522013-10-08 08:21:18 -03001325 */
Johan Hedberg9d4c1cc2015-10-21 18:03:01 +03001326 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
Johan Hedberge2caced2015-11-11 14:44:59 +02001327 if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) {
1328 return ERR_PTR(-EBUSY);
Ville Tervofcd89c02011-02-10 22:38:47 -03001329 }
1330
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07001331 /* Check if the destination address has been resolved by the controller
1332 * since if it did then the identity address shall be used.
Marcel Holtmannedb4b462014-02-18 15:13:43 -08001333 */
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07001334 if (!dst_resolved) {
1335 /* When given an identity address with existing identity
1336 * resolving key, the connection needs to be established
1337 * to a resolvable random address.
1338 *
1339 * Storing the resolvable random address is required here
1340 * to handle connection failures. The address will later
1341 * be resolved back into the original identity address
1342 * from the connect request.
1343 */
1344 irk = hci_find_irk_by_addr(hdev, dst, dst_type);
1345 if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
1346 dst = &irk->rpa;
1347 dst_type = ADDR_LE_DEV_RANDOM;
1348 }
Johan Hedberg1ebfcc12014-02-18 21:41:36 +02001349 }
1350
Johan Hedberge2caced2015-11-11 14:44:59 +02001351 if (conn) {
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02001352 bacpy(&conn->dst, dst);
1353 } else {
Ziyang Xuan181a42e2023-10-11 17:57:31 +08001354 conn = hci_conn_add_unset(hdev, LE_LINK, dst, role);
Sungwoo Kima5b862c2024-05-04 15:23:29 -04001355 if (IS_ERR(conn))
1356 return conn;
Johan Hedberge2caced2015-11-11 14:44:59 +02001357 hci_conn_hold(conn);
1358 conn->pending_sec_level = sec_level;
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02001359 }
1360
Johan Hedberg1ebfcc12014-02-18 21:41:36 +02001361 conn->dst_type = dst_type;
Andre Guedes620ad522013-10-08 08:21:18 -03001362 conn->sec_level = BT_SECURITY_LOW;
Johan Hedberg09ae2602014-07-06 13:41:15 +03001363 conn->conn_timeout = conn_timeout;
Luiz Augusto von Dentz2e7ed5f2024-04-05 16:40:33 -04001364 conn->le_adv_phy = phy;
1365 conn->le_adv_sec_phy = sec_phy;
Andre Guedes4292f1f2014-02-03 13:56:19 -03001366
Luiz Augusto von Dentz881559a2024-02-13 09:59:32 -05001367 err = hci_connect_le_sync(hdev, conn);
Andre Guedes2acf3d92014-02-26 20:21:42 -03001368 if (err) {
1369 hci_conn_del(conn);
Andre Guedes620ad522013-10-08 08:21:18 -03001370 return ERR_PTR(err);
Andre Guedes2acf3d92014-02-26 20:21:42 -03001371 }
Vinicius Costa Gomesd04aef42012-07-27 19:32:56 -03001372
Andre Guedesf1e5d542013-10-03 18:25:44 -03001373 return conn;
Vinicius Costa Gomesd04aef42012-07-27 19:32:56 -03001374}
1375
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001376static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
1377{
1378 struct hci_conn *conn;
1379
Johan Hedberg9d4c1cc2015-10-21 18:03:01 +03001380 conn = hci_conn_hash_lookup_le(hdev, addr, type);
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001381 if (!conn)
1382 return false;
1383
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001384 if (conn->state != BT_CONNECTED)
1385 return false;
1386
1387 return true;
1388}
1389
1390/* This function requires the caller holds hdev->lock */
Johan Hedberg84235d22015-11-11 08:11:20 +02001391static int hci_explicit_conn_params_set(struct hci_dev *hdev,
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001392 bdaddr_t *addr, u8 addr_type)
1393{
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001394 struct hci_conn_params *params;
1395
1396 if (is_connected(hdev, addr, addr_type))
1397 return -EISCONN;
1398
Jakub Pawlowski5157b8a2015-10-16 10:07:54 +03001399 params = hci_conn_params_lookup(hdev, addr, addr_type);
1400 if (!params) {
1401 params = hci_conn_params_add(hdev, addr, addr_type);
1402 if (!params)
1403 return -ENOMEM;
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001404
Jakub Pawlowski5157b8a2015-10-16 10:07:54 +03001405 /* If we created new params, mark them to be deleted in
1406 * hci_connect_le_scan_cleanup. It's different case than
1407 * existing disabled params, those will stay after cleanup.
1408 */
1409 params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
1410 }
1411
1412 /* We're trying to connect, so make sure params are at pend_le_conns */
Johan Hedberg49c50922015-10-16 10:07:51 +03001413 if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
Jakub Pawlowski5157b8a2015-10-16 10:07:54 +03001414 params->auto_connect == HCI_AUTO_CONN_REPORT ||
1415 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
Pauli Virtanen195ef752023-06-19 01:04:31 +03001416 hci_pend_le_list_del_init(params);
1417 hci_pend_le_list_add(params, &hdev->pend_le_conns);
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001418 }
1419
1420 params->explicit_connect = true;
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001421
1422 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
1423 params->auto_connect);
1424
1425 return 0;
1426}
1427
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001428static int qos_set_big(struct hci_dev *hdev, struct bt_iso_qos *qos)
1429{
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +03001430 struct hci_conn *conn;
1431 u8 big;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001432
1433 /* Allocate a BIG if not set */
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001434 if (qos->bcast.big == BT_ISO_QOS_BIG_UNSET) {
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +03001435 for (big = 0x00; big < 0xef; big++) {
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001436
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +03001437 conn = hci_conn_hash_lookup_big(hdev, big);
1438 if (!conn)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001439 break;
1440 }
1441
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +03001442 if (big == 0xef)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001443 return -EADDRNOTAVAIL;
1444
1445 /* Update BIG */
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +03001446 qos->bcast.big = big;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001447 }
1448
1449 return 0;
1450}
1451
1452static int qos_set_bis(struct hci_dev *hdev, struct bt_iso_qos *qos)
1453{
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +03001454 struct hci_conn *conn;
1455 u8 bis;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001456
1457 /* Allocate BIS if not set */
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001458 if (qos->bcast.bis == BT_ISO_QOS_BIS_UNSET) {
Iulia Tanasescu71b7bb42023-10-03 17:37:39 +03001459 if (qos->bcast.big != BT_ISO_QOS_BIG_UNSET) {
1460 conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big);
1461
1462 if (conn) {
1463 /* If the BIG handle is already matched to an advertising
1464 * handle, do not allocate a new one.
1465 */
1466 qos->bcast.bis = conn->iso_qos.bcast.bis;
1467 return 0;
1468 }
1469 }
1470
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001471 /* Find an unused adv set to advertise BIS, skip instance 0x00
1472 * since it is reserved as general purpose set.
1473 */
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +03001474 for (bis = 0x01; bis < hdev->le_num_of_adv_sets;
1475 bis++) {
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001476
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +03001477 conn = hci_conn_hash_lookup_bis(hdev, BDADDR_ANY, bis);
1478 if (!conn)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001479 break;
1480 }
1481
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +03001482 if (bis == hdev->le_num_of_adv_sets)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001483 return -EADDRNOTAVAIL;
1484
1485 /* Update BIS */
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +03001486 qos->bcast.bis = bis;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001487 }
1488
1489 return 0;
1490}
1491
1492/* This function requires the caller holds hdev->lock */
1493static struct hci_conn *hci_add_bis(struct hci_dev *hdev, bdaddr_t *dst,
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03001494 struct bt_iso_qos *qos, __u8 base_len,
1495 __u8 *base)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001496{
1497 struct hci_conn *conn;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001498 int err;
1499
1500 /* Let's make sure that le is enabled.*/
1501 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1502 if (lmp_le_capable(hdev))
1503 return ERR_PTR(-ECONNREFUSED);
1504 return ERR_PTR(-EOPNOTSUPP);
1505 }
1506
1507 err = qos_set_big(hdev, qos);
1508 if (err)
1509 return ERR_PTR(err);
1510
1511 err = qos_set_bis(hdev, qos);
1512 if (err)
1513 return ERR_PTR(err);
1514
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03001515 /* Check if the LE Create BIG command has already been sent */
1516 conn = hci_conn_hash_lookup_per_adv_bis(hdev, dst, qos->bcast.big,
1517 qos->bcast.big);
1518 if (conn)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001519 return ERR_PTR(-EADDRINUSE);
1520
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03001521 /* Check BIS settings against other bound BISes, since all
1522 * BISes in a BIG must have the same value for all parameters
1523 */
Iulia Tanasescu6a42e9b2023-06-19 17:53:16 +03001524 conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big);
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03001525
1526 if (conn && (memcmp(qos, &conn->iso_qos, sizeof(*qos)) ||
1527 base_len != conn->le_per_adv_data_len ||
1528 memcmp(conn->le_per_adv_data, base, base_len)))
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001529 return ERR_PTR(-EADDRINUSE);
1530
Ziyang Xuan181a42e2023-10-11 17:57:31 +08001531 conn = hci_conn_add_unset(hdev, ISO_LINK, dst, HCI_ROLE_MASTER);
Sungwoo Kima5b862c2024-05-04 15:23:29 -04001532 if (IS_ERR(conn))
1533 return conn;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001534
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001535 conn->state = BT_CONNECT;
1536
1537 hci_conn_hold(conn);
1538 return conn;
1539}
1540
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001541/* This function requires the caller holds hdev->lock */
1542struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1543 u8 dst_type, u8 sec_level,
Manish Mandlik76b13992020-06-17 16:39:19 +02001544 u16 conn_timeout,
1545 enum conn_reasons conn_reason)
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001546{
1547 struct hci_conn *conn;
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001548
1549 /* Let's make sure that le is enabled.*/
1550 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1551 if (lmp_le_capable(hdev))
1552 return ERR_PTR(-ECONNREFUSED);
1553
1554 return ERR_PTR(-EOPNOTSUPP);
1555 }
1556
1557 /* Some devices send ATT messages as soon as the physical link is
1558 * established. To be able to handle these ATT messages, the user-
1559 * space first establishes the connection and then starts the pairing
1560 * process.
1561 *
1562 * So if a hci_conn object already exists for the following connection
1563 * attempt, we simply update pending_sec_level and auth_type fields
1564 * and return the object found.
1565 */
Johan Hedberg9d4c1cc2015-10-21 18:03:01 +03001566 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001567 if (conn) {
1568 if (conn->pending_sec_level < sec_level)
1569 conn->pending_sec_level = sec_level;
1570 goto done;
1571 }
1572
1573 BT_DBG("requesting refresh of dst_addr");
1574
Ziyang Xuan181a42e2023-10-11 17:57:31 +08001575 conn = hci_conn_add_unset(hdev, LE_LINK, dst, HCI_ROLE_MASTER);
Sungwoo Kima5b862c2024-05-04 15:23:29 -04001576 if (IS_ERR(conn))
1577 return conn;
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001578
Navid Emamdoostd0883372019-11-21 14:20:36 -06001579 if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) {
1580 hci_conn_del(conn);
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001581 return ERR_PTR(-EBUSY);
Navid Emamdoostd0883372019-11-21 14:20:36 -06001582 }
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001583
1584 conn->state = BT_CONNECT;
1585 set_bit(HCI_CONN_SCANNING, &conn->flags);
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001586 conn->dst_type = dst_type;
1587 conn->sec_level = BT_SECURITY_LOW;
1588 conn->pending_sec_level = sec_level;
1589 conn->conn_timeout = conn_timeout;
Manish Mandlik76b13992020-06-17 16:39:19 +02001590 conn->conn_reason = conn_reason;
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001591
Luiz Augusto von Dentz5bee2fd2021-10-27 16:58:43 -07001592 hci_update_passive_scan(hdev);
Johan Hedberg84235d22015-11-11 08:11:20 +02001593
Jakub Pawlowskif75113a2015-08-07 20:22:53 +02001594done:
1595 hci_conn_hold(conn);
1596 return conn;
1597}
1598
Andre Guedes04a6c582014-02-26 20:21:44 -03001599struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
Manish Mandlik76b13992020-06-17 16:39:19 +02001600 u8 sec_level, u8 auth_type,
Luiz Augusto von Dentzbf98fee2024-02-07 15:26:20 -05001601 enum conn_reasons conn_reason, u16 timeout)
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001602{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 struct hci_conn *acl;
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001604
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001605 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Lukasz Rymanowskic4111102015-02-11 12:31:41 +01001606 if (lmp_bredr_capable(hdev))
1607 return ERR_PTR(-ECONNREFUSED);
1608
Johan Hedbergbeb19e42014-07-18 11:15:26 +03001609 return ERR_PTR(-EOPNOTSUPP);
Lukasz Rymanowskic4111102015-02-11 12:31:41 +01001610 }
Johan Hedberg56f87902013-10-02 13:43:13 +03001611
Lee, Chun-Yi1ffc6f82023-10-01 16:59:58 +08001612 /* Reject outgoing connection to device with same BD ADDR against
1613 * CVE-2020-26555
1614 */
1615 if (!bacmp(&hdev->bdaddr, dst)) {
1616 bt_dev_dbg(hdev, "Reject connection with same BD_ADDR %pMR\n",
1617 dst);
1618 return ERR_PTR(-ECONNREFUSED);
1619 }
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
1622 if (!acl) {
Ziyang Xuan181a42e2023-10-11 17:57:31 +08001623 acl = hci_conn_add_unset(hdev, ACL_LINK, dst, HCI_ROLE_MASTER);
Sungwoo Kima5b862c2024-05-04 15:23:29 -04001624 if (IS_ERR(acl))
1625 return acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627
1628 hci_conn_hold(acl);
1629
Manish Mandlik76b13992020-06-17 16:39:19 +02001630 acl->conn_reason = conn_reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Jonas Dreßler45340092024-02-06 12:08:13 +01001632 int err;
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 acl->sec_level = BT_SECURITY_LOW;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001635 acl->pending_sec_level = sec_level;
1636 acl->auth_type = auth_type;
Luiz Augusto von Dentzbf98fee2024-02-07 15:26:20 -05001637 acl->conn_timeout = timeout;
Jonas Dreßler45340092024-02-06 12:08:13 +01001638
Luiz Augusto von Dentz5f641f02024-02-09 09:08:06 -05001639 err = hci_connect_acl_sync(hdev, acl);
Jonas Dreßler45340092024-02-06 12:08:13 +01001640 if (err) {
1641 hci_conn_del(acl);
1642 return ERR_PTR(err);
1643 }
Nick Pellyc3902162009-11-13 14:16:32 -08001644 }
1645
Vinicius Costa Gomesdb474272012-07-28 22:35:59 -03001646 return acl;
1647}
1648
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001649static struct hci_link *hci_conn_link(struct hci_conn *parent,
1650 struct hci_conn *conn)
1651{
1652 struct hci_dev *hdev = parent->hdev;
1653 struct hci_link *link;
1654
1655 bt_dev_dbg(hdev, "parent %p hcon %p", parent, conn);
1656
1657 if (conn->link)
1658 return conn->link;
1659
1660 if (conn->parent)
1661 return NULL;
1662
1663 link = kzalloc(sizeof(*link), GFP_KERNEL);
1664 if (!link)
1665 return NULL;
1666
1667 link->conn = hci_conn_hold(conn);
1668 conn->link = link;
1669 conn->parent = hci_conn_get(parent);
1670
1671 /* Use list_add_tail_rcu append to the list */
1672 list_add_tail_rcu(&link->list, &parent->link_list);
1673
1674 return link;
1675}
1676
Frédéric Dalleau10c62dd2013-08-19 14:23:59 +02001677struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
Luiz Augusto von Dentzbf98fee2024-02-07 15:26:20 -05001678 __u16 setting, struct bt_codec *codec,
1679 u16 timeout)
Vinicius Costa Gomesdb474272012-07-28 22:35:59 -03001680{
1681 struct hci_conn *acl;
1682 struct hci_conn *sco;
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001683 struct hci_link *link;
Vinicius Costa Gomesdb474272012-07-28 22:35:59 -03001684
Manish Mandlik76b13992020-06-17 16:39:19 +02001685 acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING,
Luiz Augusto von Dentzbf98fee2024-02-07 15:26:20 -05001686 CONN_REASON_SCO_CONNECT, timeout);
Vinicius Costa Gomesdb474272012-07-28 22:35:59 -03001687 if (IS_ERR(acl))
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001688 return acl;
1689
1690 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
1691 if (!sco) {
Ziyang Xuan181a42e2023-10-11 17:57:31 +08001692 sco = hci_conn_add_unset(hdev, type, dst, HCI_ROLE_MASTER);
Sungwoo Kima5b862c2024-05-04 15:23:29 -04001693 if (IS_ERR(sco)) {
David Herrmann76a68ba2013-04-06 20:28:37 +02001694 hci_conn_drop(acl);
Sungwoo Kima5b862c2024-05-04 15:23:29 -04001695 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697 }
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02001698
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001699 link = hci_conn_link(acl, sco);
1700 if (!link) {
1701 hci_conn_drop(acl);
1702 hci_conn_drop(sco);
Siddh Raman Pantb4066eb2023-07-11 18:43:53 +05301703 return ERR_PTR(-ENOLINK);
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001704 }
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02001705
Frédéric Dalleau10c62dd2013-08-19 14:23:59 +02001706 sco->setting = setting;
Kiran Kb2af2642021-09-07 15:42:43 +05301707 sco->codec = *codec;
Frédéric Dalleau10c62dd2013-08-19 14:23:59 +02001708
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02001709 if (acl->state == BT_CONNECTED &&
Gustavo Padovan5974e4c2012-05-17 00:36:25 -03001710 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Johan Hedberg58a681e2012-01-16 06:47:28 +02001711 set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -07001712 hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02001713
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001714 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02001715 /* defer SCO setup until mode change completed */
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001716 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
Marcel Holtmann0684e5f2009-02-09 02:48:38 +01001717 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
1720 hci_sco_setup(acl, 0x00);
Marcel Holtmann96a318332009-02-12 16:23:03 +01001721 }
Marcel Holtmann0684e5f2009-02-09 02:48:38 +01001722
Marcel Holtmann96a318332009-02-12 16:23:03 +01001723 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001726static int hci_le_create_big(struct hci_conn *conn, struct bt_iso_qos *qos)
1727{
1728 struct hci_dev *hdev = conn->hdev;
1729 struct hci_cp_le_create_big cp;
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03001730 struct iso_list_data data;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001731
1732 memset(&cp, 0, sizeof(cp));
1733
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03001734 data.big = qos->bcast.big;
1735 data.bis = qos->bcast.bis;
1736 data.count = 0;
1737
1738 /* Create a BIS for each bound connection */
1739 hci_conn_hash_list_state(hdev, bis_list, ISO_LINK,
1740 BT_BOUND, &data);
1741
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001742 cp.handle = qos->bcast.big;
1743 cp.adv_handle = qos->bcast.bis;
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03001744 cp.num_bis = data.count;
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001745 hci_cpu_to_le24(qos->bcast.out.interval, cp.bis.sdu_interval);
1746 cp.bis.sdu = cpu_to_le16(qos->bcast.out.sdu);
1747 cp.bis.latency = cpu_to_le16(qos->bcast.out.latency);
1748 cp.bis.rtn = qos->bcast.out.rtn;
1749 cp.bis.phy = qos->bcast.out.phy;
1750 cp.bis.packing = qos->bcast.packing;
1751 cp.bis.framing = qos->bcast.framing;
1752 cp.bis.encryption = qos->bcast.encryption;
1753 memcpy(cp.bis.bcode, qos->bcast.bcode, sizeof(cp.bis.bcode));
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08001754
1755 return hci_send_cmd(hdev, HCI_OP_LE_CREATE_BIG, sizeof(cp), &cp);
1756}
1757
Pauli Virtanen6b9545d2023-06-01 09:34:43 +03001758static int set_cig_params_sync(struct hci_dev *hdev, void *data)
1759{
Gustavo A. R. Silvaea9e1482024-05-02 10:22:00 -06001760 DEFINE_FLEX(struct hci_cp_le_set_cig_params, pdu, cis, num_cis, 0x1f);
Luiz Augusto von Dentza1f6c3a2023-08-04 16:23:41 -07001761 u8 cig_id = PTR_UINT(data);
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001762 struct hci_conn *conn;
1763 struct bt_iso_qos *qos;
Gustavo A. R. Silvaea9e1482024-05-02 10:22:00 -06001764 u8 aux_num_cis = 0;
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001765 u8 cis_id;
Pauli Virtanen6b9545d2023-06-01 09:34:43 +03001766
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001767 conn = hci_conn_hash_lookup_cig(hdev, cig_id);
1768 if (!conn)
1769 return 0;
1770
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001771 qos = &conn->iso_qos;
Gustavo A. R. Silvaea9e1482024-05-02 10:22:00 -06001772 pdu->cig_id = cig_id;
1773 hci_cpu_to_le24(qos->ucast.out.interval, pdu->c_interval);
1774 hci_cpu_to_le24(qos->ucast.in.interval, pdu->p_interval);
1775 pdu->sca = qos->ucast.sca;
1776 pdu->packing = qos->ucast.packing;
1777 pdu->framing = qos->ucast.framing;
1778 pdu->c_latency = cpu_to_le16(qos->ucast.out.latency);
1779 pdu->p_latency = cpu_to_le16(qos->ucast.in.latency);
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001780
1781 /* Reprogram all CIS(s) with the same CIG, valid range are:
1782 * num_cis: 0x00 to 0x1F
1783 * cis_id: 0x00 to 0xEF
1784 */
1785 for (cis_id = 0x00; cis_id < 0xf0 &&
Gustavo A. R. Silvaea9e1482024-05-02 10:22:00 -06001786 aux_num_cis < pdu->num_cis; cis_id++) {
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001787 struct hci_cis_params *cis;
1788
1789 conn = hci_conn_hash_lookup_cis(hdev, NULL, 0, cig_id, cis_id);
1790 if (!conn)
1791 continue;
1792
1793 qos = &conn->iso_qos;
1794
Gustavo A. R. Silvaea9e1482024-05-02 10:22:00 -06001795 cis = &pdu->cis[aux_num_cis++];
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001796 cis->cis_id = cis_id;
1797 cis->c_sdu = cpu_to_le16(conn->iso_qos.ucast.out.sdu);
1798 cis->p_sdu = cpu_to_le16(conn->iso_qos.ucast.in.sdu);
1799 cis->c_phy = qos->ucast.out.phy ? qos->ucast.out.phy :
1800 qos->ucast.in.phy;
1801 cis->p_phy = qos->ucast.in.phy ? qos->ucast.in.phy :
1802 qos->ucast.out.phy;
1803 cis->c_rtn = qos->ucast.out.rtn;
1804 cis->p_rtn = qos->ucast.in.rtn;
1805 }
Gustavo A. R. Silvaea9e1482024-05-02 10:22:00 -06001806 pdu->num_cis = aux_num_cis;
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001807
Gustavo A. R. Silvaea9e1482024-05-02 10:22:00 -06001808 if (!pdu->num_cis)
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001809 return 0;
1810
1811 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_CIG_PARAMS,
Gustavo A. R. Silvaea9e1482024-05-02 10:22:00 -06001812 struct_size(pdu, cis, pdu->num_cis),
1813 pdu, HCI_CMD_TIMEOUT);
Pauli Virtanen6b9545d2023-06-01 09:34:43 +03001814}
1815
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001816static bool hci_le_set_cig_params(struct hci_conn *conn, struct bt_iso_qos *qos)
1817{
1818 struct hci_dev *hdev = conn->hdev;
1819 struct iso_list_data data;
1820
1821 memset(&data, 0, sizeof(data));
1822
Pauli Virtanene6a7a462023-05-21 15:48:29 +00001823 /* Allocate first still reconfigurable CIG if not set */
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001824 if (qos->ucast.cig == BT_ISO_QOS_CIG_UNSET) {
Pauli Virtanene6a7a462023-05-21 15:48:29 +00001825 for (data.cig = 0x00; data.cig < 0xf0; data.cig++) {
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001826 data.count = 0;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001827
Pauli Virtanene6a7a462023-05-21 15:48:29 +00001828 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK,
1829 BT_CONNECT, &data);
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001830 if (data.count)
1831 continue;
1832
Pauli Virtanene6a7a462023-05-21 15:48:29 +00001833 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK,
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001834 BT_CONNECTED, &data);
1835 if (!data.count)
1836 break;
1837 }
1838
Pauli Virtanene6a7a462023-05-21 15:48:29 +00001839 if (data.cig == 0xf0)
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001840 return false;
1841
1842 /* Update CIG */
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001843 qos->ucast.cig = data.cig;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001844 }
1845
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001846 if (qos->ucast.cis != BT_ISO_QOS_CIS_UNSET) {
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001847 if (hci_conn_hash_lookup_cis(hdev, NULL, 0, qos->ucast.cig,
1848 qos->ucast.cis))
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001849 return false;
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001850 goto done;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001851 }
1852
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001853 /* Allocate first available CIS if not set */
1854 for (data.cig = qos->ucast.cig, data.cis = 0x00; data.cis < 0xf0;
1855 data.cis++) {
1856 if (!hci_conn_hash_lookup_cis(hdev, NULL, 0, data.cig,
1857 data.cis)) {
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001858 /* Update CIS */
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001859 qos->ucast.cis = data.cis;
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001860 break;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001861 }
1862 }
1863
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001864 if (qos->ucast.cis == BT_ISO_QOS_CIS_UNSET)
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001865 return false;
1866
Luiz Augusto von Dentza0912892023-08-04 14:54:09 -07001867done:
1868 if (hci_cmd_sync_queue(hdev, set_cig_params_sync,
Luiz Augusto von Dentza1f6c3a2023-08-04 16:23:41 -07001869 UINT_PTR(qos->ucast.cig), NULL) < 0)
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001870 return false;
1871
1872 return true;
1873}
1874
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001875struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst,
1876 __u8 dst_type, struct bt_iso_qos *qos)
1877{
1878 struct hci_conn *cis;
1879
Luiz Augusto von Dentzc14516f2023-04-11 16:14:25 -07001880 cis = hci_conn_hash_lookup_cis(hdev, dst, dst_type, qos->ucast.cig,
1881 qos->ucast.cis);
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001882 if (!cis) {
Ziyang Xuan181a42e2023-10-11 17:57:31 +08001883 cis = hci_conn_add_unset(hdev, ISO_LINK, dst, HCI_ROLE_MASTER);
Sungwoo Kima5b862c2024-05-04 15:23:29 -04001884 if (IS_ERR(cis))
1885 return cis;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001886 cis->cleanup = cis_cleanup;
Pauli Virtanenb36a2342022-10-11 22:25:33 +03001887 cis->dst_type = dst_type;
Pauli Virtanenb5793de2023-08-05 19:08:42 +03001888 cis->iso_qos.ucast.cig = BT_ISO_QOS_CIG_UNSET;
1889 cis->iso_qos.ucast.cis = BT_ISO_QOS_CIS_UNSET;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001890 }
1891
1892 if (cis->state == BT_CONNECTED)
1893 return cis;
1894
1895 /* Check if CIS has been set and the settings matches */
1896 if (cis->state == BT_BOUND &&
1897 !memcmp(&cis->iso_qos, qos, sizeof(*qos)))
1898 return cis;
1899
1900 /* Update LINK PHYs according to QoS preference */
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001901 cis->le_tx_phy = qos->ucast.out.phy;
1902 cis->le_rx_phy = qos->ucast.in.phy;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001903
1904 /* If output interval is not set use the input interval as it cannot be
1905 * 0x000000.
1906 */
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001907 if (!qos->ucast.out.interval)
1908 qos->ucast.out.interval = qos->ucast.in.interval;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001909
1910 /* If input interval is not set use the output interval as it cannot be
1911 * 0x000000.
1912 */
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001913 if (!qos->ucast.in.interval)
1914 qos->ucast.in.interval = qos->ucast.out.interval;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001915
1916 /* If output latency is not set use the input latency as it cannot be
1917 * 0x0000.
1918 */
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001919 if (!qos->ucast.out.latency)
1920 qos->ucast.out.latency = qos->ucast.in.latency;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001921
1922 /* If input latency is not set use the output latency as it cannot be
1923 * 0x0000.
1924 */
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001925 if (!qos->ucast.in.latency)
1926 qos->ucast.in.latency = qos->ucast.out.latency;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001927
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001928 if (!hci_le_set_cig_params(cis, qos)) {
1929 hci_conn_drop(cis);
1930 return ERR_PTR(-EINVAL);
1931 }
1932
Pauli Virtanen69997d52023-07-27 00:25:26 +03001933 hci_conn_hold(cis);
1934
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001935 cis->iso_qos = *qos;
1936 cis->state = BT_BOUND;
1937
1938 return cis;
1939}
1940
1941bool hci_iso_setup_path(struct hci_conn *conn)
1942{
1943 struct hci_dev *hdev = conn->hdev;
1944 struct hci_cp_le_setup_iso_path cmd;
1945
1946 memset(&cmd, 0, sizeof(cmd));
1947
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001948 if (conn->iso_qos.ucast.out.sdu) {
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001949 cmd.handle = cpu_to_le16(conn->handle);
1950 cmd.direction = 0x00; /* Input (Host to Controller) */
1951 cmd.path = 0x00; /* HCI path if enabled */
1952 cmd.codec = 0x03; /* Transparent Data */
1953
1954 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
1955 &cmd) < 0)
1956 return false;
1957 }
1958
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03001959 if (conn->iso_qos.ucast.in.sdu) {
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001960 cmd.handle = cpu_to_le16(conn->handle);
1961 cmd.direction = 0x01; /* Output (Controller to Host) */
1962 cmd.path = 0x00; /* HCI path if enabled */
1963 cmd.codec = 0x03; /* Transparent Data */
1964
1965 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
1966 &cmd) < 0)
1967 return false;
1968 }
1969
1970 return true;
1971}
1972
Pauli Virtanen7f745632023-06-01 09:34:46 +03001973int hci_conn_check_create_cis(struct hci_conn *conn)
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001974{
Pauli Virtanen7f745632023-06-01 09:34:46 +03001975 if (conn->type != ISO_LINK || !bacmp(&conn->dst, BDADDR_ANY))
1976 return -EINVAL;
1977
1978 if (!conn->parent || conn->parent->state != BT_CONNECTED ||
Luiz Augusto von Dentz9f781912023-06-28 12:15:53 -07001979 conn->state != BT_CONNECT || HCI_CONN_HANDLE_UNSET(conn->handle))
Pauli Virtanen7f745632023-06-01 09:34:46 +03001980 return 1;
1981
1982 return 0;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001983}
1984
Pauli Virtanen7f745632023-06-01 09:34:46 +03001985static int hci_create_cis_sync(struct hci_dev *hdev, void *data)
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001986{
Pauli Virtanen7f745632023-06-01 09:34:46 +03001987 return hci_le_create_cis_sync(hdev);
1988}
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03001989
Pauli Virtanen7f745632023-06-01 09:34:46 +03001990int hci_le_create_cis_pending(struct hci_dev *hdev)
1991{
1992 struct hci_conn *conn;
1993 bool pending = false;
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001994
Pauli Virtanen7f745632023-06-01 09:34:46 +03001995 rcu_read_lock();
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07001996
Pauli Virtanen7f745632023-06-01 09:34:46 +03001997 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
1998 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) {
1999 rcu_read_unlock();
2000 return -EBUSY;
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07002001 }
2002
Pauli Virtanen7f745632023-06-01 09:34:46 +03002003 if (!hci_conn_check_create_cis(conn))
2004 pending = true;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002005 }
2006
Pauli Virtanen7f745632023-06-01 09:34:46 +03002007 rcu_read_unlock();
2008
2009 if (!pending)
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002010 return 0;
2011
2012 /* Queue Create CIS */
Pauli Virtanen7f745632023-06-01 09:34:46 +03002013 return hci_cmd_sync_queue(hdev, hci_create_cis_sync, NULL, NULL);
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002014}
2015
2016static void hci_iso_qos_setup(struct hci_dev *hdev, struct hci_conn *conn,
2017 struct bt_iso_io_qos *qos, __u8 phy)
2018{
2019 /* Only set MTU if PHY is enabled */
Sungwoo Kima5b862c2024-05-04 15:23:29 -04002020 if (!qos->sdu && qos->phy)
2021 qos->sdu = conn->mtu;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002022
2023 /* Use the same PHY as ACL if set to any */
2024 if (qos->phy == BT_ISO_PHY_ANY)
2025 qos->phy = phy;
2026
2027 /* Use LE ACL connection interval if not set */
2028 if (!qos->interval)
2029 /* ACL interval unit in 1.25 ms to us */
2030 qos->interval = conn->le_conn_interval * 1250;
2031
2032 /* Use LE ACL connection latency if not set */
2033 if (!qos->latency)
2034 qos->latency = conn->le_conn_latency;
2035}
2036
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002037static int create_big_sync(struct hci_dev *hdev, void *data)
2038{
2039 struct hci_conn *conn = data;
2040 struct bt_iso_qos *qos = &conn->iso_qos;
2041 u16 interval, sync_interval = 0;
2042 u32 flags = 0;
2043 int err;
2044
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03002045 if (qos->bcast.out.phy == 0x02)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002046 flags |= MGMT_ADV_FLAG_SEC_2M;
2047
2048 /* Align intervals */
Luiz Augusto von Dentz14f0dce2023-06-08 11:12:18 -07002049 interval = (qos->bcast.out.interval / 1250) * qos->bcast.sync_factor;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002050
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03002051 if (qos->bcast.bis)
Luiz Augusto von Dentz14f0dce2023-06-08 11:12:18 -07002052 sync_interval = interval * 4;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002053
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03002054 err = hci_start_per_adv_sync(hdev, qos->bcast.bis, conn->le_per_adv_data_len,
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002055 conn->le_per_adv_data, flags, interval,
2056 interval, sync_interval);
2057 if (err)
2058 return err;
2059
2060 return hci_le_create_big(conn, &conn->iso_qos);
2061}
2062
2063static void create_pa_complete(struct hci_dev *hdev, void *data, int err)
2064{
2065 struct hci_cp_le_pa_create_sync *cp = data;
2066
2067 bt_dev_dbg(hdev, "");
2068
2069 if (err)
2070 bt_dev_err(hdev, "Unable to create PA: %d", err);
2071
2072 kfree(cp);
2073}
2074
2075static int create_pa_sync(struct hci_dev *hdev, void *data)
2076{
2077 struct hci_cp_le_pa_create_sync *cp = data;
2078 int err;
2079
2080 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_CREATE_SYNC,
2081 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
2082 if (err) {
2083 hci_dev_clear_flag(hdev, HCI_PA_SYNC);
2084 return err;
2085 }
2086
2087 return hci_update_passive_scan_sync(hdev);
2088}
2089
Iulia Tanasescu02171da2024-02-23 15:14:41 +02002090struct hci_conn *hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst,
2091 __u8 dst_type, __u8 sid,
2092 struct bt_iso_qos *qos)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002093{
2094 struct hci_cp_le_pa_create_sync *cp;
Iulia Tanasescu02171da2024-02-23 15:14:41 +02002095 struct hci_conn *conn;
2096 int err;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002097
2098 if (hci_dev_test_and_set_flag(hdev, HCI_PA_SYNC))
Iulia Tanasescu02171da2024-02-23 15:14:41 +02002099 return ERR_PTR(-EBUSY);
2100
2101 conn = hci_conn_add_unset(hdev, ISO_LINK, dst, HCI_ROLE_SLAVE);
Sungwoo Kima5b862c2024-05-04 15:23:29 -04002102 if (IS_ERR(conn))
2103 return conn;
Iulia Tanasescu02171da2024-02-23 15:14:41 +02002104
2105 conn->iso_qos = *qos;
2106 conn->state = BT_LISTEN;
2107
2108 hci_conn_hold(conn);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002109
Jiapeng Chong37224a22022-10-17 13:47:13 +08002110 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002111 if (!cp) {
2112 hci_dev_clear_flag(hdev, HCI_PA_SYNC);
Iulia Tanasescu02171da2024-02-23 15:14:41 +02002113 hci_conn_drop(conn);
2114 return ERR_PTR(-ENOMEM);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002115 }
2116
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03002117 cp->options = qos->bcast.options;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002118 cp->sid = sid;
2119 cp->addr_type = dst_type;
2120 bacpy(&cp->addr, dst);
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03002121 cp->skip = cpu_to_le16(qos->bcast.skip);
2122 cp->sync_timeout = cpu_to_le16(qos->bcast.sync_timeout);
2123 cp->sync_cte_type = qos->bcast.sync_cte_type;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002124
2125 /* Queue start pa_create_sync and scan */
Iulia Tanasescu02171da2024-02-23 15:14:41 +02002126 err = hci_cmd_sync_queue(hdev, create_pa_sync, cp, create_pa_complete);
2127 if (err < 0) {
2128 hci_conn_drop(conn);
2129 kfree(cp);
2130 return ERR_PTR(err);
2131 }
2132
2133 return conn;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002134}
2135
Iulia Tanasescufbdc4bc2023-08-17 09:44:27 +03002136int hci_le_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon,
2137 struct bt_iso_qos *qos,
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002138 __u16 sync_handle, __u8 num_bis, __u8 bis[])
2139{
Gustavo A. R. Silvac90748b2024-04-26 10:45:17 -06002140 DEFINE_FLEX(struct hci_cp_le_big_create_sync, pdu, bis, num_bis, 0x11);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002141 int err;
2142
Gustavo A. R. Silvac90748b2024-04-26 10:45:17 -06002143 if (num_bis < 0x01 || num_bis > pdu->num_bis)
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002144 return -EINVAL;
2145
2146 err = qos_set_big(hdev, qos);
2147 if (err)
2148 return err;
2149
Iulia Tanasescufbdc4bc2023-08-17 09:44:27 +03002150 if (hcon)
2151 hcon->iso_qos.bcast.big = qos->bcast.big;
2152
Gustavo A. R. Silvac90748b2024-04-26 10:45:17 -06002153 pdu->handle = qos->bcast.big;
2154 pdu->sync_handle = cpu_to_le16(sync_handle);
2155 pdu->encryption = qos->bcast.encryption;
2156 memcpy(pdu->bcode, qos->bcast.bcode, sizeof(pdu->bcode));
2157 pdu->mse = qos->bcast.mse;
2158 pdu->timeout = cpu_to_le16(qos->bcast.timeout);
2159 pdu->num_bis = num_bis;
2160 memcpy(pdu->bis, bis, num_bis);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002161
2162 return hci_send_cmd(hdev, HCI_OP_LE_BIG_CREATE_SYNC,
Gustavo A. R. Silvad6bb8782024-05-01 12:09:30 -06002163 struct_size(pdu, bis, num_bis), pdu);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002164}
2165
2166static void create_big_complete(struct hci_dev *hdev, void *data, int err)
2167{
2168 struct hci_conn *conn = data;
2169
2170 bt_dev_dbg(hdev, "conn %p", conn);
2171
2172 if (err) {
2173 bt_dev_err(hdev, "Unable to create BIG: %d", err);
2174 hci_connect_cfm(conn, err);
2175 hci_conn_del(conn);
2176 }
2177}
2178
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03002179struct hci_conn *hci_bind_bis(struct hci_dev *hdev, bdaddr_t *dst,
2180 struct bt_iso_qos *qos,
2181 __u8 base_len, __u8 *base)
2182{
2183 struct hci_conn *conn;
Iulia Tanasescufa224d02023-11-13 17:38:00 +02002184 struct hci_conn *parent;
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03002185 __u8 eir[HCI_MAX_PER_AD_LENGTH];
Iulia Tanasescufa224d02023-11-13 17:38:00 +02002186 struct hci_link *link;
2187
2188 /* Look for any BIS that is open for rebinding */
2189 conn = hci_conn_hash_lookup_big_state(hdev, qos->bcast.big, BT_OPEN);
2190 if (conn) {
2191 memcpy(qos, &conn->iso_qos, sizeof(*qos));
2192 conn->state = BT_CONNECTED;
2193 return conn;
2194 }
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03002195
2196 if (base_len && base)
2197 base_len = eir_append_service_data(eir, 0, 0x1851,
2198 base, base_len);
2199
2200 /* We need hci_conn object using the BDADDR_ANY as dst */
2201 conn = hci_add_bis(hdev, dst, qos, base_len, eir);
2202 if (IS_ERR(conn))
2203 return conn;
2204
2205 /* Update LINK PHYs according to QoS preference */
2206 conn->le_tx_phy = qos->bcast.out.phy;
2207 conn->le_tx_phy = qos->bcast.out.phy;
2208
2209 /* Add Basic Announcement into Peridic Adv Data if BASE is set */
2210 if (base_len && base) {
2211 memcpy(conn->le_per_adv_data, eir, sizeof(eir));
2212 conn->le_per_adv_data_len = base_len;
2213 }
2214
2215 hci_iso_qos_setup(hdev, conn, &qos->bcast.out,
2216 conn->le_tx_phy ? conn->le_tx_phy :
2217 hdev->le_tx_def_phys);
2218
2219 conn->iso_qos = *qos;
2220 conn->state = BT_BOUND;
2221
Iulia Tanasescufa224d02023-11-13 17:38:00 +02002222 /* Link BISes together */
2223 parent = hci_conn_hash_lookup_big(hdev,
2224 conn->iso_qos.bcast.big);
2225 if (parent && parent != conn) {
2226 link = hci_conn_link(parent, conn);
2227 if (!link) {
2228 hci_conn_drop(conn);
2229 return ERR_PTR(-ENOLINK);
2230 }
2231
2232 /* Link takes the refcount */
2233 hci_conn_drop(conn);
2234 }
2235
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03002236 return conn;
2237}
2238
2239static void bis_mark_per_adv(struct hci_conn *conn, void *data)
2240{
2241 struct iso_list_data *d = data;
2242
2243 /* Skip if not broadcast/ANY address */
2244 if (bacmp(&conn->dst, BDADDR_ANY))
2245 return;
2246
2247 if (d->big != conn->iso_qos.bcast.big ||
2248 d->bis == BT_ISO_QOS_BIS_UNSET ||
2249 d->bis != conn->iso_qos.bcast.bis)
2250 return;
2251
2252 set_bit(HCI_CONN_PER_ADV, &conn->flags);
2253}
2254
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002255struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst,
2256 __u8 dst_type, struct bt_iso_qos *qos,
2257 __u8 base_len, __u8 *base)
2258{
2259 struct hci_conn *conn;
2260 int err;
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03002261 struct iso_list_data data;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002262
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03002263 conn = hci_bind_bis(hdev, dst, qos, base_len, base);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002264 if (IS_ERR(conn))
2265 return conn;
2266
Iulia Tanasescufa224d02023-11-13 17:38:00 +02002267 if (conn->state == BT_CONNECTED)
2268 return conn;
2269
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03002270 data.big = qos->bcast.big;
2271 data.bis = qos->bcast.bis;
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002272
Iulia Tanasescua0bfde12023-05-30 17:21:59 +03002273 /* Set HCI_CONN_PER_ADV for all bound connections, to mark that
2274 * the start periodic advertising and create BIG commands have
2275 * been queued
2276 */
2277 hci_conn_hash_list_state(hdev, bis_mark_per_adv, ISO_LINK,
2278 BT_BOUND, &data);
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002279
2280 /* Queue start periodic advertising and create BIG */
2281 err = hci_cmd_sync_queue(hdev, create_big_sync, conn,
2282 create_big_complete);
2283 if (err < 0) {
2284 hci_conn_drop(conn);
2285 return ERR_PTR(err);
2286 }
2287
Luiz Augusto von Dentzeca0ae42022-03-09 13:22:20 -08002288 return conn;
2289}
2290
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002291struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst,
2292 __u8 dst_type, struct bt_iso_qos *qos)
2293{
2294 struct hci_conn *le;
2295 struct hci_conn *cis;
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07002296 struct hci_link *link;
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002297
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002298 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
2299 le = hci_connect_le(hdev, dst, dst_type, false,
2300 BT_SECURITY_LOW,
2301 HCI_LE_CONN_TIMEOUT,
Luiz Augusto von Dentz2e7ed5f2024-04-05 16:40:33 -04002302 HCI_ROLE_SLAVE, 0, 0);
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002303 else
2304 le = hci_connect_le_scan(hdev, dst, dst_type,
2305 BT_SECURITY_LOW,
2306 HCI_LE_CONN_TIMEOUT,
2307 CONN_REASON_ISO_CONNECT);
2308 if (IS_ERR(le))
2309 return le;
2310
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03002311 hci_iso_qos_setup(hdev, le, &qos->ucast.out,
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002312 le->le_tx_phy ? le->le_tx_phy : hdev->le_tx_def_phys);
Iulia Tanasescu0fe8c8d2023-03-31 18:38:01 +03002313 hci_iso_qos_setup(hdev, le, &qos->ucast.in,
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002314 le->le_rx_phy ? le->le_rx_phy : hdev->le_rx_def_phys);
2315
2316 cis = hci_bind_cis(hdev, dst, dst_type, qos);
2317 if (IS_ERR(cis)) {
2318 hci_conn_drop(le);
2319 return cis;
2320 }
2321
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07002322 link = hci_conn_link(le, cis);
2323 if (!link) {
2324 hci_conn_drop(le);
2325 hci_conn_drop(cis);
Siddh Raman Pantb4066eb2023-07-11 18:43:53 +05302326 return ERR_PTR(-ENOLINK);
Luiz Augusto von Dentz06149742023-04-11 16:02:22 -07002327 }
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002328
Pauli Virtanen69997d52023-07-27 00:25:26 +03002329 /* Link takes the refcount */
2330 hci_conn_drop(cis);
2331
Pauli Virtanen7f745632023-06-01 09:34:46 +03002332 cis->state = BT_CONNECT;
2333
2334 hci_le_create_cis_pending(hdev);
Luiz Augusto von Dentz26afbd82019-07-29 18:15:43 +03002335
2336 return cis;
2337}
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339/* Check link security requirement */
2340int hci_conn_check_link_mode(struct hci_conn *conn)
2341{
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +03002342 BT_DBG("hcon %p", conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Marcel Holtmann40b552a2014-03-19 14:10:25 -07002344 /* In Secure Connections Only mode, it is required that Secure
2345 * Connections is used and the link is encrypted with AES-CCM
2346 * using a P-256 authenticated combination key.
2347 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002348 if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
Marcel Holtmann40b552a2014-03-19 14:10:25 -07002349 if (!hci_conn_sc_enabled(conn) ||
2350 !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
2351 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
2352 return 0;
2353 }
2354
Luiz Augusto von Dentz8746f132020-05-20 14:20:14 -07002355 /* AES encryption is required for Level 4:
2356 *
2357 * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part C
2358 * page 1319:
2359 *
2360 * 128-bit equivalent strength for link and encryption keys
2361 * required using FIPS approved algorithms (E0 not allowed,
2362 * SAFER+ not allowed, and P-192 not allowed; encryption key
2363 * not shortened)
2364 */
2365 if (conn->sec_level == BT_SECURITY_FIPS &&
2366 !test_bit(HCI_CONN_AES_CCM, &conn->flags)) {
2367 bt_dev_err(conn->hdev,
2368 "Invalid security: Missing AES-CCM usage");
2369 return 0;
2370 }
2371
Johan Hedberg4dae2792014-06-24 17:03:50 +03002372 if (hci_conn_ssp_enabled(conn) &&
2373 !test_bit(HCI_CONN_ENCRYPT, &conn->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return 0;
2375
2376 return 1;
2377}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379/* Authenticate remote device */
2380static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
2381{
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +03002382 BT_DBG("hcon %p", conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Johan Hedberg765c2a92011-01-19 12:06:52 +05302384 if (conn->pending_sec_level > sec_level)
2385 sec_level = conn->pending_sec_level;
2386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +05302388 conn->pending_sec_level = sec_level;
Johan Hedberg4dae2792014-06-24 17:03:50 +03002389 else if (test_bit(HCI_CONN_AUTH, &conn->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return 1;
2391
Johan Hedberg65cf6862011-01-19 12:06:49 +05302392 /* Make sure we preserve an existing MITM requirement*/
2393 auth_type |= (conn->auth_type & 0x01);
2394
Marcel Holtmann96a318332009-02-12 16:23:03 +01002395 conn->auth_type = auth_type;
2396
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002397 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 struct hci_cp_auth_requested cp;
Peter Hurleyb7d05ba2012-01-13 15:11:30 +01002399
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002400 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +02002401 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
Gustavo Padovan5974e4c2012-05-17 00:36:25 -03002402 sizeof(cp), &cp);
Johan Hedberg09da1f32014-04-11 12:02:32 -07002403
Luiz Augusto von Dentzd03376c2023-11-30 14:58:03 +01002404 /* Set the ENCRYPT_PEND to trigger encryption after
2405 * authentication.
Johan Hedberg09da1f32014-04-11 12:02:32 -07002406 */
Luiz Augusto von Dentzd03376c2023-11-30 14:58:03 +01002407 if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
Johan Hedberg09da1f32014-04-11 12:02:32 -07002408 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return 0;
2412}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Randy Dunlapbb6d6892020-09-17 21:35:18 -07002414/* Encrypt the link */
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002415static void hci_conn_encrypt(struct hci_conn *conn)
2416{
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +03002417 BT_DBG("hcon %p", conn);
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002418
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002419 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002420 struct hci_cp_set_conn_encrypt cp;
2421 cp.handle = cpu_to_le16(conn->handle);
2422 cp.encrypt = 0x01;
2423 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan5974e4c2012-05-17 00:36:25 -03002424 &cp);
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002425 }
2426}
2427
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002428/* Enable security */
Johan Hedberge7cafc42014-07-17 15:35:38 +03002429int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
2430 bool initiator)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +03002432 BT_DBG("hcon %p", conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Vinicius Costa Gomesd8343f12012-08-23 21:32:44 -03002434 if (conn->type == LE_LINK)
2435 return smp_conn_security(conn, sec_level);
2436
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002437 /* For sdp we don't need the link key. */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002438 if (sec_level == BT_SECURITY_SDP)
2439 return 1;
2440
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002441 /* For non 2.1 devices and low security level we don't need the link
2442 key. */
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002443 if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -07002444 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002445
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002446 /* For other security levels we need the link key. */
Johan Hedberg4dae2792014-06-24 17:03:50 +03002447 if (!test_bit(HCI_CONN_AUTH, &conn->flags))
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002448 goto auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Ying Hsu1d8e8012023-09-07 04:39:34 +00002450 switch (conn->key_type) {
2451 case HCI_LK_AUTH_COMBINATION_P256:
2452 /* An authenticated FIPS approved combination key has
2453 * sufficient security for security level 4 or lower.
2454 */
2455 if (sec_level <= BT_SECURITY_FIPS)
2456 goto encrypt;
2457 break;
2458 case HCI_LK_AUTH_COMBINATION_P192:
2459 /* An authenticated combination key has sufficient security for
2460 * security level 3 or lower.
2461 */
2462 if (sec_level <= BT_SECURITY_HIGH)
2463 goto encrypt;
2464 break;
2465 case HCI_LK_UNAUTH_COMBINATION_P192:
2466 case HCI_LK_UNAUTH_COMBINATION_P256:
2467 /* An unauthenticated combination key has sufficient security
2468 * for security level 2 or lower.
2469 */
2470 if (sec_level <= BT_SECURITY_MEDIUM)
2471 goto encrypt;
2472 break;
2473 case HCI_LK_COMBINATION:
2474 /* A combination key has always sufficient security for the
2475 * security levels 2 or lower. High security level requires the
2476 * combination key is generated using maximum PIN code length
2477 * (16). For pre 2.1 units.
2478 */
2479 if (sec_level <= BT_SECURITY_MEDIUM || conn->pin_length == 16)
2480 goto encrypt;
2481 break;
2482 default:
2483 break;
2484 }
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002485
2486auth:
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002487 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return 0;
2489
Johan Hedberg977f8fce2014-07-17 15:35:39 +03002490 if (initiator)
2491 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2492
Luiz Augusto von Dentz6fdf6582011-06-13 15:37:35 +03002493 if (!hci_conn_auth(conn, sec_level, auth_type))
2494 return 0;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002495
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002496encrypt:
Marcel Holtmann693cd8c2019-06-22 15:47:01 +02002497 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) {
2498 /* Ensure that the encryption key size has been read,
2499 * otherwise stall the upper layer responses.
2500 */
2501 if (!conn->enc_key_size)
2502 return 0;
2503
2504 /* Nothing else needed, all requirements are met */
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002505 return 1;
Marcel Holtmann693cd8c2019-06-22 15:47:01 +02002506 }
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002507
2508 hci_conn_encrypt(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return 0;
2510}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002511EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +02002513/* Check secure link requirement */
2514int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
2515{
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +03002516 BT_DBG("hcon %p", conn);
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +02002517
Marcel Holtmann9cb2e032014-02-01 11:32:25 -08002518 /* Accept if non-secure or higher security level is required */
2519 if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS)
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +02002520 return 1;
2521
Marcel Holtmann9cb2e032014-02-01 11:32:25 -08002522 /* Accept if secure or higher security level is already present */
2523 if (conn->sec_level == BT_SECURITY_HIGH ||
2524 conn->sec_level == BT_SECURITY_FIPS)
2525 return 1;
2526
2527 /* Reject not secure link */
2528 return 0;
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +02002529}
2530EXPORT_SYMBOL(hci_conn_check_secure);
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002533int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +03002535 BT_DBG("hcon %p", conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Johan Hedberg40bef302014-07-16 11:42:27 +03002537 if (role == conn->role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return 1;
2539
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002540 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 struct hci_cp_switch_role cp;
2542 bacpy(&cp.bdaddr, &conn->dst);
2543 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002544 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return 0;
2548}
2549EXPORT_SYMBOL(hci_conn_switch_role);
2550
Marcel Holtmann04837f62006-07-03 10:02:33 +02002551/* Enter active mode */
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -07002552void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +02002553{
2554 struct hci_dev *hdev = conn->hdev;
2555
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +03002556 BT_DBG("hcon %p mode %d", conn, conn->mode);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002557
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -07002558 if (conn->mode != HCI_CM_SNIFF)
2559 goto timer;
2560
Johan Hedberg58a681e2012-01-16 06:47:28 +02002561 if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +02002562 goto timer;
2563
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002564 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02002565 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002566 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002567 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002568 }
2569
2570timer:
2571 if (hdev->idle_timeout > 0)
Johan Hedberga74a84f2013-10-16 18:11:40 +03002572 queue_delayed_work(hdev->workqueue, &conn->idle_work,
2573 msecs_to_jiffies(hdev->idle_timeout));
Marcel Holtmann04837f62006-07-03 10:02:33 +02002574}
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576/* Drop all connection on the device */
2577void hci_conn_hash_flush(struct hci_dev *hdev)
2578{
Ruihan Lia2ac5912023-05-03 21:39:36 +08002579 struct list_head *head = &hdev->conn_hash.list;
2580 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 BT_DBG("hdev %s", hdev->name);
2583
Ruihan Lia2ac5912023-05-03 21:39:36 +08002584 /* We should not traverse the list here, because hci_conn_del
2585 * can remove extra links, which may cause the list traversal
2586 * to hit items that have already been released.
2587 */
2588 while ((conn = list_first_entry_or_null(head,
2589 struct hci_conn,
2590 list)) != NULL) {
2591 conn->state = BT_CLOSED;
2592 hci_disconn_cfm(conn, HCI_ERROR_LOCAL_HOST_TERM);
Ruihan Lia2ac5912023-05-03 21:39:36 +08002593 hci_conn_del(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
2595}
2596
Johan Hedberg4dae2792014-06-24 17:03:50 +03002597static u32 get_link_mode(struct hci_conn *conn)
2598{
2599 u32 link_mode = 0;
2600
Johan Hedberg40bef302014-07-16 11:42:27 +03002601 if (conn->role == HCI_ROLE_MASTER)
Johan Hedberg4dae2792014-06-24 17:03:50 +03002602 link_mode |= HCI_LM_MASTER;
2603
2604 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
2605 link_mode |= HCI_LM_ENCRYPT;
2606
2607 if (test_bit(HCI_CONN_AUTH, &conn->flags))
2608 link_mode |= HCI_LM_AUTH;
2609
2610 if (test_bit(HCI_CONN_SECURE, &conn->flags))
2611 link_mode |= HCI_LM_SECURE;
2612
2613 if (test_bit(HCI_CONN_FIPS, &conn->flags))
2614 link_mode |= HCI_LM_FIPS;
2615
2616 return link_mode;
2617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619int hci_get_conn_list(void __user *arg)
2620{
Gustavo Padovanfc5fef62012-05-23 04:04:19 -03002621 struct hci_conn *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 struct hci_conn_list_req req, *cl;
2623 struct hci_conn_info *ci;
2624 struct hci_dev *hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 int n = 0, size, err;
2626
2627 if (copy_from_user(&req, arg, sizeof(req)))
2628 return -EFAULT;
2629
2630 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
2631 return -EINVAL;
2632
2633 size = sizeof(req) + req.conn_num * sizeof(*ci);
2634
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002635 cl = kmalloc(size, GFP_KERNEL);
2636 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return -ENOMEM;
2638
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002639 hdev = hci_dev_get(req.dev_id);
2640 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 kfree(cl);
2642 return -ENODEV;
2643 }
2644
2645 ci = cl->conn_info;
2646
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002647 hci_dev_lock(hdev);
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002648 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 bacpy(&(ci + n)->bdaddr, &c->dst);
2650 (ci + n)->handle = c->handle;
2651 (ci + n)->type = c->type;
2652 (ci + n)->out = c->out;
2653 (ci + n)->state = c->state;
Johan Hedberg4dae2792014-06-24 17:03:50 +03002654 (ci + n)->link_mode = get_link_mode(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 if (++n >= req.conn_num)
2656 break;
2657 }
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002658 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 cl->dev_id = hdev->id;
2661 cl->conn_num = n;
2662 size = sizeof(req) + n * sizeof(*ci);
2663
2664 hci_dev_put(hdev);
2665
2666 err = copy_to_user(arg, cl, size);
2667 kfree(cl);
2668
2669 return err ? -EFAULT : 0;
2670}
2671
2672int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
2673{
2674 struct hci_conn_info_req req;
2675 struct hci_conn_info ci;
2676 struct hci_conn *conn;
2677 char __user *ptr = arg + sizeof(req);
2678
2679 if (copy_from_user(&req, arg, sizeof(req)))
2680 return -EFAULT;
2681
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002682 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
2684 if (conn) {
2685 bacpy(&ci.bdaddr, &conn->dst);
2686 ci.handle = conn->handle;
2687 ci.type = conn->type;
2688 ci.out = conn->out;
2689 ci.state = conn->state;
Johan Hedberg4dae2792014-06-24 17:03:50 +03002690 ci.link_mode = get_link_mode(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002692 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694 if (!conn)
2695 return -ENOENT;
2696
2697 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
2698}
Marcel Holtmann40be4922008-07-14 20:13:50 +02002699
2700int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
2701{
2702 struct hci_auth_info_req req;
2703 struct hci_conn *conn;
2704
2705 if (copy_from_user(&req, arg, sizeof(req)))
2706 return -EFAULT;
2707
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002708 hci_dev_lock(hdev);
Marcel Holtmann40be4922008-07-14 20:13:50 +02002709 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
2710 if (conn)
2711 req.type = conn->auth_type;
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002712 hci_dev_unlock(hdev);
Marcel Holtmann40be4922008-07-14 20:13:50 +02002713
2714 if (!conn)
2715 return -ENOENT;
2716
2717 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
2718}
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002719
2720struct hci_chan *hci_chan_create(struct hci_conn *conn)
2721{
2722 struct hci_dev *hdev = conn->hdev;
2723 struct hci_chan *chan;
2724
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +03002725 BT_DBG("%s hcon %p", hdev->name, conn);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002726
Johan Hedbergf94b6652014-08-18 00:41:44 +03002727 if (test_bit(HCI_CONN_DROP, &conn->flags)) {
2728 BT_DBG("Refusing to create new hci_chan");
2729 return NULL;
2730 }
2731
Johan Hedberg27f70f32014-07-21 10:50:06 +03002732 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002733 if (!chan)
2734 return NULL;
2735
Johan Hedberg6c388d32014-08-18 00:41:42 +03002736 chan->conn = hci_conn_get(conn);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002737 skb_queue_head_init(&chan->data_q);
Mat Martineau168df8e2012-10-23 15:24:13 -07002738 chan->state = BT_CONNECTED;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002739
Gustavo F. Padovan8192ede2011-12-14 15:08:48 -02002740 list_add_rcu(&chan->list, &conn->chan_list);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002741
2742 return chan;
2743}
2744
Andrei Emeltchenko94720072012-09-06 15:05:43 +03002745void hci_chan_del(struct hci_chan *chan)
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002746{
2747 struct hci_conn *conn = chan->conn;
2748 struct hci_dev *hdev = conn->hdev;
2749
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +03002750 BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002751
Gustavo F. Padovan8192ede2011-12-14 15:08:48 -02002752 list_del_rcu(&chan->list);
2753
2754 synchronize_rcu();
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002755
Johan Hedbergbcbb6552014-08-18 20:33:27 +03002756 /* Prevent new hci_chan's to be created for this hci_conn */
Johan Hedbergf94b6652014-08-18 00:41:44 +03002757 set_bit(HCI_CONN_DROP, &conn->flags);
Johan Hedbergb3ff6702014-08-18 00:41:43 +03002758
Johan Hedberg6c388d32014-08-18 00:41:42 +03002759 hci_conn_put(conn);
Andrei Emeltchenkoe9b02742012-10-25 15:20:51 +03002760
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002761 skb_queue_purge(&chan->data_q);
2762 kfree(chan);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002763}
2764
Gustavo F. Padovan2c33c062011-12-14 13:02:51 -02002765void hci_chan_list_flush(struct hci_conn *conn)
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002766{
Andrei Emeltchenko2a5a5ec2012-02-02 10:32:18 +02002767 struct hci_chan *chan, *n;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002768
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +03002769 BT_DBG("hcon %p", conn);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002770
Andrei Emeltchenko2a5a5ec2012-02-02 10:32:18 +02002771 list_for_each_entry_safe(chan, n, &conn->chan_list, list)
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02002772 hci_chan_del(chan);
2773}
Andrei Emeltchenko42c4e532012-10-10 17:38:28 +03002774
2775static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon,
2776 __u16 handle)
2777{
2778 struct hci_chan *hchan;
2779
2780 list_for_each_entry(hchan, &hcon->chan_list, list) {
2781 if (hchan->handle == handle)
2782 return hchan;
2783 }
2784
2785 return NULL;
2786}
2787
2788struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle)
2789{
2790 struct hci_conn_hash *h = &hdev->conn_hash;
2791 struct hci_conn *hcon;
2792 struct hci_chan *hchan = NULL;
2793
2794 rcu_read_lock();
2795
2796 list_for_each_entry_rcu(hcon, &h->list, list) {
2797 hchan = __hci_chan_lookup_handle(hcon, handle);
2798 if (hchan)
2799 break;
2800 }
2801
2802 rcu_read_unlock();
2803
2804 return hchan;
2805}
Luiz Augusto von Dentzeab24042020-02-14 10:08:57 -08002806
2807u32 hci_conn_get_phy(struct hci_conn *conn)
2808{
2809 u32 phys = 0;
2810
Luiz Augusto von Dentzeab24042020-02-14 10:08:57 -08002811 /* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471:
2812 * Table 6.2: Packets defined for synchronous, asynchronous, and
Archie Pusaka63977292021-05-31 16:37:22 +08002813 * CPB logical transport types.
Luiz Augusto von Dentzeab24042020-02-14 10:08:57 -08002814 */
2815 switch (conn->type) {
2816 case SCO_LINK:
2817 /* SCO logical transport (1 Mb/s):
2818 * HV1, HV2, HV3 and DV.
2819 */
2820 phys |= BT_PHY_BR_1M_1SLOT;
2821
2822 break;
2823
2824 case ACL_LINK:
2825 /* ACL logical transport (1 Mb/s) ptt=0:
2826 * DH1, DM3, DH3, DM5 and DH5.
2827 */
2828 phys |= BT_PHY_BR_1M_1SLOT;
2829
2830 if (conn->pkt_type & (HCI_DM3 | HCI_DH3))
2831 phys |= BT_PHY_BR_1M_3SLOT;
2832
2833 if (conn->pkt_type & (HCI_DM5 | HCI_DH5))
2834 phys |= BT_PHY_BR_1M_5SLOT;
2835
2836 /* ACL logical transport (2 Mb/s) ptt=1:
2837 * 2-DH1, 2-DH3 and 2-DH5.
2838 */
2839 if (!(conn->pkt_type & HCI_2DH1))
2840 phys |= BT_PHY_EDR_2M_1SLOT;
2841
2842 if (!(conn->pkt_type & HCI_2DH3))
2843 phys |= BT_PHY_EDR_2M_3SLOT;
2844
2845 if (!(conn->pkt_type & HCI_2DH5))
2846 phys |= BT_PHY_EDR_2M_5SLOT;
2847
2848 /* ACL logical transport (3 Mb/s) ptt=1:
2849 * 3-DH1, 3-DH3 and 3-DH5.
2850 */
2851 if (!(conn->pkt_type & HCI_3DH1))
2852 phys |= BT_PHY_EDR_3M_1SLOT;
2853
2854 if (!(conn->pkt_type & HCI_3DH3))
2855 phys |= BT_PHY_EDR_3M_3SLOT;
2856
2857 if (!(conn->pkt_type & HCI_3DH5))
2858 phys |= BT_PHY_EDR_3M_5SLOT;
2859
2860 break;
2861
2862 case ESCO_LINK:
2863 /* eSCO logical transport (1 Mb/s): EV3, EV4 and EV5 */
2864 phys |= BT_PHY_BR_1M_1SLOT;
2865
2866 if (!(conn->pkt_type & (ESCO_EV4 | ESCO_EV5)))
2867 phys |= BT_PHY_BR_1M_3SLOT;
2868
2869 /* eSCO logical transport (2 Mb/s): 2-EV3, 2-EV5 */
2870 if (!(conn->pkt_type & ESCO_2EV3))
2871 phys |= BT_PHY_EDR_2M_1SLOT;
2872
2873 if (!(conn->pkt_type & ESCO_2EV5))
2874 phys |= BT_PHY_EDR_2M_3SLOT;
2875
2876 /* eSCO logical transport (3 Mb/s): 3-EV3, 3-EV5 */
2877 if (!(conn->pkt_type & ESCO_3EV3))
2878 phys |= BT_PHY_EDR_3M_1SLOT;
2879
2880 if (!(conn->pkt_type & ESCO_3EV5))
2881 phys |= BT_PHY_EDR_3M_3SLOT;
2882
2883 break;
2884
2885 case LE_LINK:
2886 if (conn->le_tx_phy & HCI_LE_SET_PHY_1M)
2887 phys |= BT_PHY_LE_1M_TX;
2888
2889 if (conn->le_rx_phy & HCI_LE_SET_PHY_1M)
2890 phys |= BT_PHY_LE_1M_RX;
2891
2892 if (conn->le_tx_phy & HCI_LE_SET_PHY_2M)
2893 phys |= BT_PHY_LE_2M_TX;
2894
2895 if (conn->le_rx_phy & HCI_LE_SET_PHY_2M)
2896 phys |= BT_PHY_LE_2M_RX;
2897
2898 if (conn->le_tx_phy & HCI_LE_SET_PHY_CODED)
2899 phys |= BT_PHY_LE_CODED_TX;
2900
2901 if (conn->le_rx_phy & HCI_LE_SET_PHY_CODED)
2902 phys |= BT_PHY_LE_CODED_RX;
2903
2904 break;
2905 }
2906
Luiz Augusto von Dentzeab24042020-02-14 10:08:57 -08002907 return phys;
2908}
Brian Gix1a942de2022-08-16 09:41:20 -07002909
Luiz Augusto von Dentza13f3162023-06-26 17:25:06 -07002910static int abort_conn_sync(struct hci_dev *hdev, void *data)
Brian Gix1a942de2022-08-16 09:41:20 -07002911{
Luiz Augusto von Dentz881559a2024-02-13 09:59:32 -05002912 struct hci_conn *conn = data;
Brian Gix1a942de2022-08-16 09:41:20 -07002913
Luiz Augusto von Dentz881559a2024-02-13 09:59:32 -05002914 if (!hci_conn_valid(hdev, conn))
2915 return -ECANCELED;
Luiz Augusto von Dentzb62e7222023-03-24 13:18:20 -07002916
Luiz Augusto von Dentza13f3162023-06-26 17:25:06 -07002917 return hci_abort_conn_sync(hdev, conn, conn->abort_reason);
2918}
Brian Gix1a942de2022-08-16 09:41:20 -07002919
Luiz Augusto von Dentza13f3162023-06-26 17:25:06 -07002920int hci_abort_conn(struct hci_conn *conn, u8 reason)
2921{
2922 struct hci_dev *hdev = conn->hdev;
Brian Gix1a942de2022-08-16 09:41:20 -07002923
Luiz Augusto von Dentza13f3162023-06-26 17:25:06 -07002924 /* If abort_reason has already been set it means the connection is
2925 * already being aborted so don't attempt to overwrite it.
2926 */
2927 if (conn->abort_reason)
2928 return 0;
2929
2930 bt_dev_dbg(hdev, "handle 0x%2.2x reason 0x%2.2x", conn->handle, reason);
2931
2932 conn->abort_reason = reason;
2933
2934 /* If the connection is pending check the command opcode since that
2935 * might be blocking on hci_cmd_sync_work while waiting its respective
2936 * event so we need to hci_cmd_sync_cancel to cancel it.
Luiz Augusto von Dentz04a51d62023-06-27 15:55:47 -07002937 *
2938 * hci_connect_le serializes the connection attempts so only one
2939 * connection can be in BT_CONNECT at time.
Luiz Augusto von Dentza13f3162023-06-26 17:25:06 -07002940 */
2941 if (conn->state == BT_CONNECT && hdev->req_status == HCI_REQ_PEND) {
2942 switch (hci_skb_event(hdev->sent_cmd)) {
Luiz Augusto von Dentz5f641f02024-02-09 09:08:06 -05002943 case HCI_EV_CONN_COMPLETE:
Luiz Augusto von Dentza13f3162023-06-26 17:25:06 -07002944 case HCI_EV_LE_CONN_COMPLETE:
2945 case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
2946 case HCI_EVT_LE_CIS_ESTABLISHED:
Luiz Augusto von Dentz2615fd92024-02-16 16:20:11 -05002947 hci_cmd_sync_cancel(hdev, ECANCELED);
Luiz Augusto von Dentza13f3162023-06-26 17:25:06 -07002948 break;
Brian Gix1a942de2022-08-16 09:41:20 -07002949 }
Luiz Augusto von Dentz881559a2024-02-13 09:59:32 -05002950 /* Cancel connect attempt if still queued/pending */
2951 } else if (!hci_cancel_connect_sync(hdev, conn)) {
2952 return 0;
Brian Gix1a942de2022-08-16 09:41:20 -07002953 }
2954
Luiz Augusto von Dentz881559a2024-02-13 09:59:32 -05002955 return hci_cmd_sync_queue_once(hdev, abort_conn_sync, conn, NULL);
Brian Gix1a942de2022-08-16 09:41:20 -07002956}