Linux 6.10-rc3
[linux-stable.git] / net / bluetooth / hci_conn.c
blob0c76dcde53619627cdb4544b36b02a8bfc8cc936
1 /*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
4 Copyright 2023-2024 NXP
6 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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;
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
16 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
23 SOFTWARE IS DISCLAIMED.
26 /* Bluetooth HCI connection handling. */
28 #include <linux/export.h>
29 #include <linux/debugfs.h>
31 #include <net/bluetooth/bluetooth.h>
32 #include <net/bluetooth/hci_core.h>
33 #include <net/bluetooth/l2cap.h>
34 #include <net/bluetooth/iso.h>
35 #include <net/bluetooth/mgmt.h>
37 #include "hci_request.h"
38 #include "smp.h"
39 #include "eir.h"
41 struct sco_param {
42 u16 pkt_type;
43 u16 max_latency;
44 u8 retrans_effort;
47 struct conn_handle_t {
48 struct hci_conn *conn;
49 __u16 handle;
52 static const struct sco_param esco_param_cvsd[] = {
53 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */
54 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */
55 { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */
56 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */
57 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */
60 static const struct sco_param sco_param_cvsd[] = {
61 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */
62 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */
65 static const struct sco_param esco_param_msbc[] = {
66 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */
67 { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */
70 /* This function requires the caller holds hdev->lock */
71 void hci_connect_le_scan_cleanup(struct hci_conn *conn, u8 status)
73 struct hci_conn_params *params;
74 struct hci_dev *hdev = conn->hdev;
75 struct smp_irk *irk;
76 bdaddr_t *bdaddr;
77 u8 bdaddr_type;
79 bdaddr = &conn->dst;
80 bdaddr_type = conn->dst_type;
82 /* Check if we need to convert to identity address */
83 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
84 if (irk) {
85 bdaddr = &irk->bdaddr;
86 bdaddr_type = irk->addr_type;
89 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr,
90 bdaddr_type);
91 if (!params)
92 return;
94 if (params->conn) {
95 hci_conn_drop(params->conn);
96 hci_conn_put(params->conn);
97 params->conn = NULL;
100 if (!params->explicit_connect)
101 return;
103 /* If the status indicates successful cancellation of
104 * the attempt (i.e. Unknown Connection Id) there's no point of
105 * notifying failure since we'll go back to keep trying to
106 * connect. The only exception is explicit connect requests
107 * where a timeout + cancel does indicate an actual failure.
109 if (status && status != HCI_ERROR_UNKNOWN_CONN_ID)
110 mgmt_connect_failed(hdev, &conn->dst, conn->type,
111 conn->dst_type, status);
113 /* The connection attempt was doing scan for new RPA, and is
114 * in scan phase. If params are not associated with any other
115 * autoconnect action, remove them completely. If they are, just unmark
116 * them as waiting for connection, by clearing explicit_connect field.
118 params->explicit_connect = false;
120 hci_pend_le_list_del_init(params);
122 switch (params->auto_connect) {
123 case HCI_AUTO_CONN_EXPLICIT:
124 hci_conn_params_del(hdev, bdaddr, bdaddr_type);
125 /* return instead of break to avoid duplicate scan update */
126 return;
127 case HCI_AUTO_CONN_DIRECT:
128 case HCI_AUTO_CONN_ALWAYS:
129 hci_pend_le_list_add(params, &hdev->pend_le_conns);
130 break;
131 case HCI_AUTO_CONN_REPORT:
132 hci_pend_le_list_add(params, &hdev->pend_le_reports);
133 break;
134 default:
135 break;
138 hci_update_passive_scan(hdev);
141 static void hci_conn_cleanup(struct hci_conn *conn)
143 struct hci_dev *hdev = conn->hdev;
145 if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags))
146 hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type);
148 if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
149 hci_remove_link_key(hdev, &conn->dst);
151 hci_chan_list_flush(conn);
153 hci_conn_hash_del(hdev, conn);
155 if (HCI_CONN_HANDLE_UNSET(conn->handle))
156 ida_free(&hdev->unset_handle_ida, conn->handle);
158 if (conn->cleanup)
159 conn->cleanup(conn);
161 if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
162 switch (conn->setting & SCO_AIRMODE_MASK) {
163 case SCO_AIRMODE_CVSD:
164 case SCO_AIRMODE_TRANSP:
165 if (hdev->notify)
166 hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO);
167 break;
169 } else {
170 if (hdev->notify)
171 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
174 debugfs_remove_recursive(conn->debugfs);
176 hci_conn_del_sysfs(conn);
178 hci_dev_put(hdev);
181 int hci_disconnect(struct hci_conn *conn, __u8 reason)
183 BT_DBG("hcon %p", conn);
185 /* When we are central of an established connection and it enters
186 * the disconnect timeout, then go ahead and try to read the
187 * current clock offset. Processing of the result is done
188 * within the event handling and hci_clock_offset_evt function.
190 if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER &&
191 (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) {
192 struct hci_dev *hdev = conn->hdev;
193 struct hci_cp_read_clock_offset clkoff_cp;
195 clkoff_cp.handle = cpu_to_le16(conn->handle);
196 hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp),
197 &clkoff_cp);
200 return hci_abort_conn(conn, reason);
203 static void hci_add_sco(struct hci_conn *conn, __u16 handle)
205 struct hci_dev *hdev = conn->hdev;
206 struct hci_cp_add_sco cp;
208 BT_DBG("hcon %p", conn);
210 conn->state = BT_CONNECT;
211 conn->out = true;
213 conn->attempt++;
215 cp.handle = cpu_to_le16(handle);
216 cp.pkt_type = cpu_to_le16(conn->pkt_type);
218 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
221 static bool find_next_esco_param(struct hci_conn *conn,
222 const struct sco_param *esco_param, int size)
224 if (!conn->parent)
225 return false;
227 for (; conn->attempt <= size; conn->attempt++) {
228 if (lmp_esco_2m_capable(conn->parent) ||
229 (esco_param[conn->attempt - 1].pkt_type & ESCO_2EV3))
230 break;
231 BT_DBG("hcon %p skipped attempt %d, eSCO 2M not supported",
232 conn, conn->attempt);
235 return conn->attempt <= size;
238 static int configure_datapath_sync(struct hci_dev *hdev, struct bt_codec *codec)
240 int err;
241 __u8 vnd_len, *vnd_data = NULL;
242 struct hci_op_configure_data_path *cmd = NULL;
244 /* Do not take below 2 checks as error since the 1st means user do not
245 * want to use HFP offload mode and the 2nd means the vendor controller
246 * do not need to send below HCI command for offload mode.
248 if (!codec->data_path || !hdev->get_codec_config_data)
249 return 0;
251 err = hdev->get_codec_config_data(hdev, ESCO_LINK, codec, &vnd_len,
252 &vnd_data);
253 if (err < 0)
254 goto error;
256 cmd = kzalloc(sizeof(*cmd) + vnd_len, GFP_KERNEL);
257 if (!cmd) {
258 err = -ENOMEM;
259 goto error;
262 err = hdev->get_data_path_id(hdev, &cmd->data_path_id);
263 if (err < 0)
264 goto error;
266 cmd->vnd_len = vnd_len;
267 memcpy(cmd->vnd_data, vnd_data, vnd_len);
269 cmd->direction = 0x00;
270 __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH,
271 sizeof(*cmd) + vnd_len, cmd, HCI_CMD_TIMEOUT);
273 cmd->direction = 0x01;
274 err = __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH,
275 sizeof(*cmd) + vnd_len, cmd,
276 HCI_CMD_TIMEOUT);
277 error:
279 kfree(cmd);
280 kfree(vnd_data);
281 return err;
284 static int hci_enhanced_setup_sync(struct hci_dev *hdev, void *data)
286 struct conn_handle_t *conn_handle = data;
287 struct hci_conn *conn = conn_handle->conn;
288 __u16 handle = conn_handle->handle;
289 struct hci_cp_enhanced_setup_sync_conn cp;
290 const struct sco_param *param;
292 kfree(conn_handle);
294 bt_dev_dbg(hdev, "hcon %p", conn);
296 configure_datapath_sync(hdev, &conn->codec);
298 conn->state = BT_CONNECT;
299 conn->out = true;
301 conn->attempt++;
303 memset(&cp, 0x00, sizeof(cp));
305 cp.handle = cpu_to_le16(handle);
307 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
308 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
310 switch (conn->codec.id) {
311 case BT_CODEC_MSBC:
312 if (!find_next_esco_param(conn, esco_param_msbc,
313 ARRAY_SIZE(esco_param_msbc)))
314 return -EINVAL;
316 param = &esco_param_msbc[conn->attempt - 1];
317 cp.tx_coding_format.id = 0x05;
318 cp.rx_coding_format.id = 0x05;
319 cp.tx_codec_frame_size = __cpu_to_le16(60);
320 cp.rx_codec_frame_size = __cpu_to_le16(60);
321 cp.in_bandwidth = __cpu_to_le32(32000);
322 cp.out_bandwidth = __cpu_to_le32(32000);
323 cp.in_coding_format.id = 0x04;
324 cp.out_coding_format.id = 0x04;
325 cp.in_coded_data_size = __cpu_to_le16(16);
326 cp.out_coded_data_size = __cpu_to_le16(16);
327 cp.in_pcm_data_format = 2;
328 cp.out_pcm_data_format = 2;
329 cp.in_pcm_sample_payload_msb_pos = 0;
330 cp.out_pcm_sample_payload_msb_pos = 0;
331 cp.in_data_path = conn->codec.data_path;
332 cp.out_data_path = conn->codec.data_path;
333 cp.in_transport_unit_size = 1;
334 cp.out_transport_unit_size = 1;
335 break;
337 case BT_CODEC_TRANSPARENT:
338 if (!find_next_esco_param(conn, esco_param_msbc,
339 ARRAY_SIZE(esco_param_msbc)))
340 return false;
341 param = &esco_param_msbc[conn->attempt - 1];
342 cp.tx_coding_format.id = 0x03;
343 cp.rx_coding_format.id = 0x03;
344 cp.tx_codec_frame_size = __cpu_to_le16(60);
345 cp.rx_codec_frame_size = __cpu_to_le16(60);
346 cp.in_bandwidth = __cpu_to_le32(0x1f40);
347 cp.out_bandwidth = __cpu_to_le32(0x1f40);
348 cp.in_coding_format.id = 0x03;
349 cp.out_coding_format.id = 0x03;
350 cp.in_coded_data_size = __cpu_to_le16(16);
351 cp.out_coded_data_size = __cpu_to_le16(16);
352 cp.in_pcm_data_format = 2;
353 cp.out_pcm_data_format = 2;
354 cp.in_pcm_sample_payload_msb_pos = 0;
355 cp.out_pcm_sample_payload_msb_pos = 0;
356 cp.in_data_path = conn->codec.data_path;
357 cp.out_data_path = conn->codec.data_path;
358 cp.in_transport_unit_size = 1;
359 cp.out_transport_unit_size = 1;
360 break;
362 case BT_CODEC_CVSD:
363 if (conn->parent && lmp_esco_capable(conn->parent)) {
364 if (!find_next_esco_param(conn, esco_param_cvsd,
365 ARRAY_SIZE(esco_param_cvsd)))
366 return -EINVAL;
367 param = &esco_param_cvsd[conn->attempt - 1];
368 } else {
369 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
370 return -EINVAL;
371 param = &sco_param_cvsd[conn->attempt - 1];
373 cp.tx_coding_format.id = 2;
374 cp.rx_coding_format.id = 2;
375 cp.tx_codec_frame_size = __cpu_to_le16(60);
376 cp.rx_codec_frame_size = __cpu_to_le16(60);
377 cp.in_bandwidth = __cpu_to_le32(16000);
378 cp.out_bandwidth = __cpu_to_le32(16000);
379 cp.in_coding_format.id = 4;
380 cp.out_coding_format.id = 4;
381 cp.in_coded_data_size = __cpu_to_le16(16);
382 cp.out_coded_data_size = __cpu_to_le16(16);
383 cp.in_pcm_data_format = 2;
384 cp.out_pcm_data_format = 2;
385 cp.in_pcm_sample_payload_msb_pos = 0;
386 cp.out_pcm_sample_payload_msb_pos = 0;
387 cp.in_data_path = conn->codec.data_path;
388 cp.out_data_path = conn->codec.data_path;
389 cp.in_transport_unit_size = 16;
390 cp.out_transport_unit_size = 16;
391 break;
392 default:
393 return -EINVAL;
396 cp.retrans_effort = param->retrans_effort;
397 cp.pkt_type = __cpu_to_le16(param->pkt_type);
398 cp.max_latency = __cpu_to_le16(param->max_latency);
400 if (hci_send_cmd(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
401 return -EIO;
403 return 0;
406 static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle)
408 struct hci_dev *hdev = conn->hdev;
409 struct hci_cp_setup_sync_conn cp;
410 const struct sco_param *param;
412 bt_dev_dbg(hdev, "hcon %p", conn);
414 conn->state = BT_CONNECT;
415 conn->out = true;
417 conn->attempt++;
419 cp.handle = cpu_to_le16(handle);
421 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
422 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
423 cp.voice_setting = cpu_to_le16(conn->setting);
425 switch (conn->setting & SCO_AIRMODE_MASK) {
426 case SCO_AIRMODE_TRANSP:
427 if (!find_next_esco_param(conn, esco_param_msbc,
428 ARRAY_SIZE(esco_param_msbc)))
429 return false;
430 param = &esco_param_msbc[conn->attempt - 1];
431 break;
432 case SCO_AIRMODE_CVSD:
433 if (conn->parent && lmp_esco_capable(conn->parent)) {
434 if (!find_next_esco_param(conn, esco_param_cvsd,
435 ARRAY_SIZE(esco_param_cvsd)))
436 return false;
437 param = &esco_param_cvsd[conn->attempt - 1];
438 } else {
439 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
440 return false;
441 param = &sco_param_cvsd[conn->attempt - 1];
443 break;
444 default:
445 return false;
448 cp.retrans_effort = param->retrans_effort;
449 cp.pkt_type = __cpu_to_le16(param->pkt_type);
450 cp.max_latency = __cpu_to_le16(param->max_latency);
452 if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
453 return false;
455 return true;
458 bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
460 int result;
461 struct conn_handle_t *conn_handle;
463 if (enhanced_sync_conn_capable(conn->hdev)) {
464 conn_handle = kzalloc(sizeof(*conn_handle), GFP_KERNEL);
466 if (!conn_handle)
467 return false;
469 conn_handle->conn = conn;
470 conn_handle->handle = handle;
471 result = hci_cmd_sync_queue(conn->hdev, hci_enhanced_setup_sync,
472 conn_handle, NULL);
473 if (result < 0)
474 kfree(conn_handle);
476 return result == 0;
479 return hci_setup_sync_conn(conn, handle);
482 u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
483 u16 to_multiplier)
485 struct hci_dev *hdev = conn->hdev;
486 struct hci_conn_params *params;
487 struct hci_cp_le_conn_update cp;
489 hci_dev_lock(hdev);
491 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
492 if (params) {
493 params->conn_min_interval = min;
494 params->conn_max_interval = max;
495 params->conn_latency = latency;
496 params->supervision_timeout = to_multiplier;
499 hci_dev_unlock(hdev);
501 memset(&cp, 0, sizeof(cp));
502 cp.handle = cpu_to_le16(conn->handle);
503 cp.conn_interval_min = cpu_to_le16(min);
504 cp.conn_interval_max = cpu_to_le16(max);
505 cp.conn_latency = cpu_to_le16(latency);
506 cp.supervision_timeout = cpu_to_le16(to_multiplier);
507 cp.min_ce_len = cpu_to_le16(0x0000);
508 cp.max_ce_len = cpu_to_le16(0x0000);
510 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
512 if (params)
513 return 0x01;
515 return 0x00;
518 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
519 __u8 ltk[16], __u8 key_size)
521 struct hci_dev *hdev = conn->hdev;
522 struct hci_cp_le_start_enc cp;
524 BT_DBG("hcon %p", conn);
526 memset(&cp, 0, sizeof(cp));
528 cp.handle = cpu_to_le16(conn->handle);
529 cp.rand = rand;
530 cp.ediv = ediv;
531 memcpy(cp.ltk, ltk, key_size);
533 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
536 /* Device _must_ be locked */
537 void hci_sco_setup(struct hci_conn *conn, __u8 status)
539 struct hci_link *link;
541 link = list_first_entry_or_null(&conn->link_list, struct hci_link, list);
542 if (!link || !link->conn)
543 return;
545 BT_DBG("hcon %p", conn);
547 if (!status) {
548 if (lmp_esco_capable(conn->hdev))
549 hci_setup_sync(link->conn, conn->handle);
550 else
551 hci_add_sco(link->conn, conn->handle);
552 } else {
553 hci_connect_cfm(link->conn, status);
554 hci_conn_del(link->conn);
558 static void hci_conn_timeout(struct work_struct *work)
560 struct hci_conn *conn = container_of(work, struct hci_conn,
561 disc_work.work);
562 int refcnt = atomic_read(&conn->refcnt);
564 BT_DBG("hcon %p state %s", conn, state_to_string(conn->state));
566 WARN_ON(refcnt < 0);
568 /* FIXME: It was observed that in pairing failed scenario, refcnt
569 * drops below 0. Probably this is because l2cap_conn_del calls
570 * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is
571 * dropped. After that loop hci_chan_del is called which also drops
572 * conn. For now make sure that ACL is alive if refcnt is higher then 0,
573 * otherwise drop it.
575 if (refcnt > 0)
576 return;
578 hci_abort_conn(conn, hci_proto_disconn_ind(conn));
581 /* Enter sniff mode */
582 static void hci_conn_idle(struct work_struct *work)
584 struct hci_conn *conn = container_of(work, struct hci_conn,
585 idle_work.work);
586 struct hci_dev *hdev = conn->hdev;
588 BT_DBG("hcon %p mode %d", conn, conn->mode);
590 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
591 return;
593 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
594 return;
596 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
597 struct hci_cp_sniff_subrate cp;
598 cp.handle = cpu_to_le16(conn->handle);
599 cp.max_latency = cpu_to_le16(0);
600 cp.min_remote_timeout = cpu_to_le16(0);
601 cp.min_local_timeout = cpu_to_le16(0);
602 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
605 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
606 struct hci_cp_sniff_mode cp;
607 cp.handle = cpu_to_le16(conn->handle);
608 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
609 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
610 cp.attempt = cpu_to_le16(4);
611 cp.timeout = cpu_to_le16(1);
612 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
616 static void hci_conn_auto_accept(struct work_struct *work)
618 struct hci_conn *conn = container_of(work, struct hci_conn,
619 auto_accept_work.work);
621 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
622 &conn->dst);
625 static void le_disable_advertising(struct hci_dev *hdev)
627 if (ext_adv_capable(hdev)) {
628 struct hci_cp_le_set_ext_adv_enable cp;
630 cp.enable = 0x00;
631 cp.num_of_sets = 0x00;
633 hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp),
634 &cp);
635 } else {
636 u8 enable = 0x00;
637 hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
638 &enable);
642 static void le_conn_timeout(struct work_struct *work)
644 struct hci_conn *conn = container_of(work, struct hci_conn,
645 le_conn_timeout.work);
646 struct hci_dev *hdev = conn->hdev;
648 BT_DBG("");
650 /* We could end up here due to having done directed advertising,
651 * so clean up the state if necessary. This should however only
652 * happen with broken hardware or if low duty cycle was used
653 * (which doesn't have a timeout of its own).
655 if (conn->role == HCI_ROLE_SLAVE) {
656 /* Disable LE Advertising */
657 le_disable_advertising(hdev);
658 hci_dev_lock(hdev);
659 hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
660 hci_dev_unlock(hdev);
661 return;
664 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
667 struct iso_list_data {
668 union {
669 u8 cig;
670 u8 big;
672 union {
673 u8 cis;
674 u8 bis;
675 u16 sync_handle;
677 int count;
678 bool big_term;
679 bool pa_sync_term;
680 bool big_sync_term;
683 static void bis_list(struct hci_conn *conn, void *data)
685 struct iso_list_data *d = data;
687 /* Skip if not broadcast/ANY address */
688 if (bacmp(&conn->dst, BDADDR_ANY))
689 return;
691 if (d->big != conn->iso_qos.bcast.big || d->bis == BT_ISO_QOS_BIS_UNSET ||
692 d->bis != conn->iso_qos.bcast.bis)
693 return;
695 d->count++;
698 static int terminate_big_sync(struct hci_dev *hdev, void *data)
700 struct iso_list_data *d = data;
702 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", d->big, d->bis);
704 hci_disable_per_advertising_sync(hdev, d->bis);
705 hci_remove_ext_adv_instance_sync(hdev, d->bis, NULL);
707 /* Only terminate BIG if it has been created */
708 if (!d->big_term)
709 return 0;
711 return hci_le_terminate_big_sync(hdev, d->big,
712 HCI_ERROR_LOCAL_HOST_TERM);
715 static void terminate_big_destroy(struct hci_dev *hdev, void *data, int err)
717 kfree(data);
720 static int hci_le_terminate_big(struct hci_dev *hdev, struct hci_conn *conn)
722 struct iso_list_data *d;
723 int ret;
725 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", conn->iso_qos.bcast.big,
726 conn->iso_qos.bcast.bis);
728 d = kzalloc(sizeof(*d), GFP_KERNEL);
729 if (!d)
730 return -ENOMEM;
732 d->big = conn->iso_qos.bcast.big;
733 d->bis = conn->iso_qos.bcast.bis;
734 d->big_term = test_and_clear_bit(HCI_CONN_BIG_CREATED, &conn->flags);
736 ret = hci_cmd_sync_queue(hdev, terminate_big_sync, d,
737 terminate_big_destroy);
738 if (ret)
739 kfree(d);
741 return ret;
744 static int big_terminate_sync(struct hci_dev *hdev, void *data)
746 struct iso_list_data *d = data;
748 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", d->big,
749 d->sync_handle);
751 if (d->big_sync_term)
752 hci_le_big_terminate_sync(hdev, d->big);
754 if (d->pa_sync_term)
755 return hci_le_pa_terminate_sync(hdev, d->sync_handle);
757 return 0;
760 static void find_bis(struct hci_conn *conn, void *data)
762 struct iso_list_data *d = data;
764 /* Ignore if BIG doesn't match */
765 if (d->big != conn->iso_qos.bcast.big)
766 return;
768 d->count++;
771 static int hci_le_big_terminate(struct hci_dev *hdev, u8 big, struct hci_conn *conn)
773 struct iso_list_data *d;
774 int ret;
776 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", big, conn->sync_handle);
778 d = kzalloc(sizeof(*d), GFP_KERNEL);
779 if (!d)
780 return -ENOMEM;
782 memset(d, 0, sizeof(*d));
783 d->big = big;
784 d->sync_handle = conn->sync_handle;
786 if (test_and_clear_bit(HCI_CONN_PA_SYNC, &conn->flags)) {
787 hci_conn_hash_list_flag(hdev, find_bis, ISO_LINK,
788 HCI_CONN_PA_SYNC, d);
790 if (!d->count)
791 d->pa_sync_term = true;
793 d->count = 0;
796 if (test_and_clear_bit(HCI_CONN_BIG_SYNC, &conn->flags)) {
797 hci_conn_hash_list_flag(hdev, find_bis, ISO_LINK,
798 HCI_CONN_BIG_SYNC, d);
800 if (!d->count)
801 d->big_sync_term = true;
804 ret = hci_cmd_sync_queue(hdev, big_terminate_sync, d,
805 terminate_big_destroy);
806 if (ret)
807 kfree(d);
809 return ret;
812 /* Cleanup BIS connection
814 * Detects if there any BIS left connected in a BIG
815 * broadcaster: Remove advertising instance and terminate BIG.
816 * broadcaster receiver: Teminate BIG sync and terminate PA sync.
818 static void bis_cleanup(struct hci_conn *conn)
820 struct hci_dev *hdev = conn->hdev;
821 struct hci_conn *bis;
823 bt_dev_dbg(hdev, "conn %p", conn);
825 if (conn->role == HCI_ROLE_MASTER) {
826 if (!test_and_clear_bit(HCI_CONN_PER_ADV, &conn->flags))
827 return;
829 /* Check if ISO connection is a BIS and terminate advertising
830 * set and BIG if there are no other connections using it.
832 bis = hci_conn_hash_lookup_big(hdev, conn->iso_qos.bcast.big);
833 if (bis)
834 return;
836 hci_le_terminate_big(hdev, conn);
837 } else {
838 hci_le_big_terminate(hdev, conn->iso_qos.bcast.big,
839 conn);
843 static int remove_cig_sync(struct hci_dev *hdev, void *data)
845 u8 handle = PTR_UINT(data);
847 return hci_le_remove_cig_sync(hdev, handle);
850 static int hci_le_remove_cig(struct hci_dev *hdev, u8 handle)
852 bt_dev_dbg(hdev, "handle 0x%2.2x", handle);
854 return hci_cmd_sync_queue(hdev, remove_cig_sync, UINT_PTR(handle),
855 NULL);
858 static void find_cis(struct hci_conn *conn, void *data)
860 struct iso_list_data *d = data;
862 /* Ignore broadcast or if CIG don't match */
863 if (!bacmp(&conn->dst, BDADDR_ANY) || d->cig != conn->iso_qos.ucast.cig)
864 return;
866 d->count++;
869 /* Cleanup CIS connection:
871 * Detects if there any CIS left connected in a CIG and remove it.
873 static void cis_cleanup(struct hci_conn *conn)
875 struct hci_dev *hdev = conn->hdev;
876 struct iso_list_data d;
878 if (conn->iso_qos.ucast.cig == BT_ISO_QOS_CIG_UNSET)
879 return;
881 memset(&d, 0, sizeof(d));
882 d.cig = conn->iso_qos.ucast.cig;
884 /* Check if ISO connection is a CIS and remove CIG if there are
885 * no other connections using it.
887 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_BOUND, &d);
888 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_CONNECT, &d);
889 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_CONNECTED, &d);
890 if (d.count)
891 return;
893 hci_le_remove_cig(hdev, conn->iso_qos.ucast.cig);
896 static int hci_conn_hash_alloc_unset(struct hci_dev *hdev)
898 return ida_alloc_range(&hdev->unset_handle_ida, HCI_CONN_HANDLE_MAX + 1,
899 U16_MAX, GFP_ATOMIC);
902 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
903 u8 role, u16 handle)
905 struct hci_conn *conn;
907 switch (type) {
908 case ACL_LINK:
909 if (!hdev->acl_mtu)
910 return ERR_PTR(-ECONNREFUSED);
911 break;
912 case ISO_LINK:
913 if (hdev->iso_mtu)
914 /* Dedicated ISO Buffer exists */
915 break;
916 fallthrough;
917 case LE_LINK:
918 if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
919 return ERR_PTR(-ECONNREFUSED);
920 if (!hdev->le_mtu && hdev->acl_mtu < HCI_MIN_LE_MTU)
921 return ERR_PTR(-ECONNREFUSED);
922 break;
923 case SCO_LINK:
924 case ESCO_LINK:
925 if (!hdev->sco_pkts)
926 /* Controller does not support SCO or eSCO over HCI */
927 return ERR_PTR(-ECONNREFUSED);
928 break;
929 default:
930 return ERR_PTR(-ECONNREFUSED);
933 bt_dev_dbg(hdev, "dst %pMR handle 0x%4.4x", dst, handle);
935 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
936 if (!conn)
937 return ERR_PTR(-ENOMEM);
939 bacpy(&conn->dst, dst);
940 bacpy(&conn->src, &hdev->bdaddr);
941 conn->handle = handle;
942 conn->hdev = hdev;
943 conn->type = type;
944 conn->role = role;
945 conn->mode = HCI_CM_ACTIVE;
946 conn->state = BT_OPEN;
947 conn->auth_type = HCI_AT_GENERAL_BONDING;
948 conn->io_capability = hdev->io_capability;
949 conn->remote_auth = 0xff;
950 conn->key_type = 0xff;
951 conn->rssi = HCI_RSSI_INVALID;
952 conn->tx_power = HCI_TX_POWER_INVALID;
953 conn->max_tx_power = HCI_TX_POWER_INVALID;
954 conn->sync_handle = HCI_SYNC_HANDLE_INVALID;
956 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
957 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
959 /* Set Default Authenticated payload timeout to 30s */
960 conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT;
962 if (conn->role == HCI_ROLE_MASTER)
963 conn->out = true;
965 switch (type) {
966 case ACL_LINK:
967 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
968 conn->mtu = hdev->acl_mtu;
969 break;
970 case LE_LINK:
971 /* conn->src should reflect the local identity address */
972 hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
973 conn->mtu = hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu;
974 break;
975 case ISO_LINK:
976 /* conn->src should reflect the local identity address */
977 hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
979 /* set proper cleanup function */
980 if (!bacmp(dst, BDADDR_ANY))
981 conn->cleanup = bis_cleanup;
982 else if (conn->role == HCI_ROLE_MASTER)
983 conn->cleanup = cis_cleanup;
985 conn->mtu = hdev->iso_mtu ? hdev->iso_mtu :
986 hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu;
987 break;
988 case SCO_LINK:
989 if (lmp_esco_capable(hdev))
990 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
991 (hdev->esco_type & EDR_ESCO_MASK);
992 else
993 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
995 conn->mtu = hdev->sco_mtu;
996 break;
997 case ESCO_LINK:
998 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
999 conn->mtu = hdev->sco_mtu;
1000 break;
1003 skb_queue_head_init(&conn->data_q);
1005 INIT_LIST_HEAD(&conn->chan_list);
1006 INIT_LIST_HEAD(&conn->link_list);
1008 INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
1009 INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
1010 INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
1011 INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
1013 atomic_set(&conn->refcnt, 0);
1015 hci_dev_hold(hdev);
1017 hci_conn_hash_add(hdev, conn);
1019 /* The SCO and eSCO connections will only be notified when their
1020 * setup has been completed. This is different to ACL links which
1021 * can be notified right away.
1023 if (conn->type != SCO_LINK && conn->type != ESCO_LINK) {
1024 if (hdev->notify)
1025 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
1028 hci_conn_init_sysfs(conn);
1030 return conn;
1033 struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type,
1034 bdaddr_t *dst, u8 role)
1036 int handle;
1038 bt_dev_dbg(hdev, "dst %pMR", dst);
1040 handle = hci_conn_hash_alloc_unset(hdev);
1041 if (unlikely(handle < 0))
1042 return ERR_PTR(-ECONNREFUSED);
1044 return hci_conn_add(hdev, type, dst, role, handle);
1047 static void hci_conn_cleanup_child(struct hci_conn *conn, u8 reason)
1049 if (!reason)
1050 reason = HCI_ERROR_REMOTE_USER_TERM;
1052 /* Due to race, SCO/ISO conn might be not established yet at this point,
1053 * and nothing else will clean it up. In other cases it is done via HCI
1054 * events.
1056 switch (conn->type) {
1057 case SCO_LINK:
1058 case ESCO_LINK:
1059 if (HCI_CONN_HANDLE_UNSET(conn->handle))
1060 hci_conn_failed(conn, reason);
1061 break;
1062 case ISO_LINK:
1063 if ((conn->state != BT_CONNECTED &&
1064 !test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) ||
1065 test_bit(HCI_CONN_BIG_CREATED, &conn->flags))
1066 hci_conn_failed(conn, reason);
1067 break;
1071 static void hci_conn_unlink(struct hci_conn *conn)
1073 struct hci_dev *hdev = conn->hdev;
1075 bt_dev_dbg(hdev, "hcon %p", conn);
1077 if (!conn->parent) {
1078 struct hci_link *link, *t;
1080 list_for_each_entry_safe(link, t, &conn->link_list, list) {
1081 struct hci_conn *child = link->conn;
1083 hci_conn_unlink(child);
1085 /* If hdev is down it means
1086 * hci_dev_close_sync/hci_conn_hash_flush is in progress
1087 * and links don't need to be cleanup as all connections
1088 * would be cleanup.
1090 if (!test_bit(HCI_UP, &hdev->flags))
1091 continue;
1093 hci_conn_cleanup_child(child, conn->abort_reason);
1096 return;
1099 if (!conn->link)
1100 return;
1102 list_del_rcu(&conn->link->list);
1103 synchronize_rcu();
1105 hci_conn_drop(conn->parent);
1106 hci_conn_put(conn->parent);
1107 conn->parent = NULL;
1109 kfree(conn->link);
1110 conn->link = NULL;
1113 void hci_conn_del(struct hci_conn *conn)
1115 struct hci_dev *hdev = conn->hdev;
1117 BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
1119 hci_conn_unlink(conn);
1121 cancel_delayed_work_sync(&conn->disc_work);
1122 cancel_delayed_work_sync(&conn->auto_accept_work);
1123 cancel_delayed_work_sync(&conn->idle_work);
1125 if (conn->type == ACL_LINK) {
1126 /* Unacked frames */
1127 hdev->acl_cnt += conn->sent;
1128 } else if (conn->type == LE_LINK) {
1129 cancel_delayed_work(&conn->le_conn_timeout);
1131 if (hdev->le_pkts)
1132 hdev->le_cnt += conn->sent;
1133 else
1134 hdev->acl_cnt += conn->sent;
1135 } else {
1136 /* Unacked ISO frames */
1137 if (conn->type == ISO_LINK) {
1138 if (hdev->iso_pkts)
1139 hdev->iso_cnt += conn->sent;
1140 else if (hdev->le_pkts)
1141 hdev->le_cnt += conn->sent;
1142 else
1143 hdev->acl_cnt += conn->sent;
1147 skb_queue_purge(&conn->data_q);
1149 /* Remove the connection from the list and cleanup its remaining
1150 * state. This is a separate function since for some cases like
1151 * BT_CONNECT_SCAN we *only* want the cleanup part without the
1152 * rest of hci_conn_del.
1154 hci_conn_cleanup(conn);
1156 /* Dequeue callbacks using connection pointer as data */
1157 hci_cmd_sync_dequeue(hdev, NULL, conn, NULL);
1160 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type)
1162 int use_src = bacmp(src, BDADDR_ANY);
1163 struct hci_dev *hdev = NULL, *d;
1165 BT_DBG("%pMR -> %pMR", src, dst);
1167 read_lock(&hci_dev_list_lock);
1169 list_for_each_entry(d, &hci_dev_list, list) {
1170 if (!test_bit(HCI_UP, &d->flags) ||
1171 hci_dev_test_flag(d, HCI_USER_CHANNEL))
1172 continue;
1174 /* Simple routing:
1175 * No source address - find interface with bdaddr != dst
1176 * Source address - find interface with bdaddr == src
1179 if (use_src) {
1180 bdaddr_t id_addr;
1181 u8 id_addr_type;
1183 if (src_type == BDADDR_BREDR) {
1184 if (!lmp_bredr_capable(d))
1185 continue;
1186 bacpy(&id_addr, &d->bdaddr);
1187 id_addr_type = BDADDR_BREDR;
1188 } else {
1189 if (!lmp_le_capable(d))
1190 continue;
1192 hci_copy_identity_address(d, &id_addr,
1193 &id_addr_type);
1195 /* Convert from HCI to three-value type */
1196 if (id_addr_type == ADDR_LE_DEV_PUBLIC)
1197 id_addr_type = BDADDR_LE_PUBLIC;
1198 else
1199 id_addr_type = BDADDR_LE_RANDOM;
1202 if (!bacmp(&id_addr, src) && id_addr_type == src_type) {
1203 hdev = d; break;
1205 } else {
1206 if (bacmp(&d->bdaddr, dst)) {
1207 hdev = d; break;
1212 if (hdev)
1213 hdev = hci_dev_hold(hdev);
1215 read_unlock(&hci_dev_list_lock);
1216 return hdev;
1218 EXPORT_SYMBOL(hci_get_route);
1220 /* This function requires the caller holds hdev->lock */
1221 static void hci_le_conn_failed(struct hci_conn *conn, u8 status)
1223 struct hci_dev *hdev = conn->hdev;
1225 hci_connect_le_scan_cleanup(conn, status);
1227 /* Enable advertising in case this was a failed connection
1228 * attempt as a peripheral.
1230 hci_enable_advertising(hdev);
1233 /* This function requires the caller holds hdev->lock */
1234 void hci_conn_failed(struct hci_conn *conn, u8 status)
1236 struct hci_dev *hdev = conn->hdev;
1238 bt_dev_dbg(hdev, "status 0x%2.2x", status);
1240 switch (conn->type) {
1241 case LE_LINK:
1242 hci_le_conn_failed(conn, status);
1243 break;
1244 case ACL_LINK:
1245 mgmt_connect_failed(hdev, &conn->dst, conn->type,
1246 conn->dst_type, status);
1247 break;
1250 /* In case of BIG/PA sync failed, clear conn flags so that
1251 * the conns will be correctly cleaned up by ISO layer
1253 test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags);
1254 test_and_clear_bit(HCI_CONN_PA_SYNC_FAILED, &conn->flags);
1256 conn->state = BT_CLOSED;
1257 hci_connect_cfm(conn, status);
1258 hci_conn_del(conn);
1261 /* This function requires the caller holds hdev->lock */
1262 u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle)
1264 struct hci_dev *hdev = conn->hdev;
1266 bt_dev_dbg(hdev, "hcon %p handle 0x%4.4x", conn, handle);
1268 if (conn->handle == handle)
1269 return 0;
1271 if (handle > HCI_CONN_HANDLE_MAX) {
1272 bt_dev_err(hdev, "Invalid handle: 0x%4.4x > 0x%4.4x",
1273 handle, HCI_CONN_HANDLE_MAX);
1274 return HCI_ERROR_INVALID_PARAMETERS;
1277 /* If abort_reason has been sent it means the connection is being
1278 * aborted and the handle shall not be changed.
1280 if (conn->abort_reason)
1281 return conn->abort_reason;
1283 if (HCI_CONN_HANDLE_UNSET(conn->handle))
1284 ida_free(&hdev->unset_handle_ida, conn->handle);
1286 conn->handle = handle;
1288 return 0;
1291 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
1292 u8 dst_type, bool dst_resolved, u8 sec_level,
1293 u16 conn_timeout, u8 role, u8 phy, u8 sec_phy)
1295 struct hci_conn *conn;
1296 struct smp_irk *irk;
1297 int err;
1299 /* Let's make sure that le is enabled.*/
1300 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1301 if (lmp_le_capable(hdev))
1302 return ERR_PTR(-ECONNREFUSED);
1304 return ERR_PTR(-EOPNOTSUPP);
1307 /* Since the controller supports only one LE connection attempt at a
1308 * time, we return -EBUSY if there is any connection attempt running.
1310 if (hci_lookup_le_connect(hdev))
1311 return ERR_PTR(-EBUSY);
1313 /* If there's already a connection object but it's not in
1314 * scanning state it means it must already be established, in
1315 * which case we can't do anything else except report a failure
1316 * to connect.
1318 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
1319 if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) {
1320 return ERR_PTR(-EBUSY);
1323 /* Check if the destination address has been resolved by the controller
1324 * since if it did then the identity address shall be used.
1326 if (!dst_resolved) {
1327 /* When given an identity address with existing identity
1328 * resolving key, the connection needs to be established
1329 * to a resolvable random address.
1331 * Storing the resolvable random address is required here
1332 * to handle connection failures. The address will later
1333 * be resolved back into the original identity address
1334 * from the connect request.
1336 irk = hci_find_irk_by_addr(hdev, dst, dst_type);
1337 if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
1338 dst = &irk->rpa;
1339 dst_type = ADDR_LE_DEV_RANDOM;
1343 if (conn) {
1344 bacpy(&conn->dst, dst);
1345 } else {
1346 conn = hci_conn_add_unset(hdev, LE_LINK, dst, role);
1347 if (IS_ERR(conn))
1348 return conn;
1349 hci_conn_hold(conn);
1350 conn->pending_sec_level = sec_level;
1353 conn->dst_type = dst_type;
1354 conn->sec_level = BT_SECURITY_LOW;
1355 conn->conn_timeout = conn_timeout;
1356 conn->le_adv_phy = phy;
1357 conn->le_adv_sec_phy = sec_phy;
1359 err = hci_connect_le_sync(hdev, conn);
1360 if (err) {
1361 hci_conn_del(conn);
1362 return ERR_PTR(err);
1365 return conn;
1368 static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
1370 struct hci_conn *conn;
1372 conn = hci_conn_hash_lookup_le(hdev, addr, type);
1373 if (!conn)
1374 return false;
1376 if (conn->state != BT_CONNECTED)
1377 return false;
1379 return true;
1382 /* This function requires the caller holds hdev->lock */
1383 static int hci_explicit_conn_params_set(struct hci_dev *hdev,
1384 bdaddr_t *addr, u8 addr_type)
1386 struct hci_conn_params *params;
1388 if (is_connected(hdev, addr, addr_type))
1389 return -EISCONN;
1391 params = hci_conn_params_lookup(hdev, addr, addr_type);
1392 if (!params) {
1393 params = hci_conn_params_add(hdev, addr, addr_type);
1394 if (!params)
1395 return -ENOMEM;
1397 /* If we created new params, mark them to be deleted in
1398 * hci_connect_le_scan_cleanup. It's different case than
1399 * existing disabled params, those will stay after cleanup.
1401 params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
1404 /* We're trying to connect, so make sure params are at pend_le_conns */
1405 if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
1406 params->auto_connect == HCI_AUTO_CONN_REPORT ||
1407 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
1408 hci_pend_le_list_del_init(params);
1409 hci_pend_le_list_add(params, &hdev->pend_le_conns);
1412 params->explicit_connect = true;
1414 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
1415 params->auto_connect);
1417 return 0;
1420 static int qos_set_big(struct hci_dev *hdev, struct bt_iso_qos *qos)
1422 struct hci_conn *conn;
1423 u8 big;
1425 /* Allocate a BIG if not set */
1426 if (qos->bcast.big == BT_ISO_QOS_BIG_UNSET) {
1427 for (big = 0x00; big < 0xef; big++) {
1429 conn = hci_conn_hash_lookup_big(hdev, big);
1430 if (!conn)
1431 break;
1434 if (big == 0xef)
1435 return -EADDRNOTAVAIL;
1437 /* Update BIG */
1438 qos->bcast.big = big;
1441 return 0;
1444 static int qos_set_bis(struct hci_dev *hdev, struct bt_iso_qos *qos)
1446 struct hci_conn *conn;
1447 u8 bis;
1449 /* Allocate BIS if not set */
1450 if (qos->bcast.bis == BT_ISO_QOS_BIS_UNSET) {
1451 if (qos->bcast.big != BT_ISO_QOS_BIG_UNSET) {
1452 conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big);
1454 if (conn) {
1455 /* If the BIG handle is already matched to an advertising
1456 * handle, do not allocate a new one.
1458 qos->bcast.bis = conn->iso_qos.bcast.bis;
1459 return 0;
1463 /* Find an unused adv set to advertise BIS, skip instance 0x00
1464 * since it is reserved as general purpose set.
1466 for (bis = 0x01; bis < hdev->le_num_of_adv_sets;
1467 bis++) {
1469 conn = hci_conn_hash_lookup_bis(hdev, BDADDR_ANY, bis);
1470 if (!conn)
1471 break;
1474 if (bis == hdev->le_num_of_adv_sets)
1475 return -EADDRNOTAVAIL;
1477 /* Update BIS */
1478 qos->bcast.bis = bis;
1481 return 0;
1484 /* This function requires the caller holds hdev->lock */
1485 static struct hci_conn *hci_add_bis(struct hci_dev *hdev, bdaddr_t *dst,
1486 struct bt_iso_qos *qos, __u8 base_len,
1487 __u8 *base)
1489 struct hci_conn *conn;
1490 int err;
1492 /* Let's make sure that le is enabled.*/
1493 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1494 if (lmp_le_capable(hdev))
1495 return ERR_PTR(-ECONNREFUSED);
1496 return ERR_PTR(-EOPNOTSUPP);
1499 err = qos_set_big(hdev, qos);
1500 if (err)
1501 return ERR_PTR(err);
1503 err = qos_set_bis(hdev, qos);
1504 if (err)
1505 return ERR_PTR(err);
1507 /* Check if the LE Create BIG command has already been sent */
1508 conn = hci_conn_hash_lookup_per_adv_bis(hdev, dst, qos->bcast.big,
1509 qos->bcast.big);
1510 if (conn)
1511 return ERR_PTR(-EADDRINUSE);
1513 /* Check BIS settings against other bound BISes, since all
1514 * BISes in a BIG must have the same value for all parameters
1516 conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big);
1518 if (conn && (memcmp(qos, &conn->iso_qos, sizeof(*qos)) ||
1519 base_len != conn->le_per_adv_data_len ||
1520 memcmp(conn->le_per_adv_data, base, base_len)))
1521 return ERR_PTR(-EADDRINUSE);
1523 conn = hci_conn_add_unset(hdev, ISO_LINK, dst, HCI_ROLE_MASTER);
1524 if (IS_ERR(conn))
1525 return conn;
1527 conn->state = BT_CONNECT;
1529 hci_conn_hold(conn);
1530 return conn;
1533 /* This function requires the caller holds hdev->lock */
1534 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1535 u8 dst_type, u8 sec_level,
1536 u16 conn_timeout,
1537 enum conn_reasons conn_reason)
1539 struct hci_conn *conn;
1541 /* Let's make sure that le is enabled.*/
1542 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1543 if (lmp_le_capable(hdev))
1544 return ERR_PTR(-ECONNREFUSED);
1546 return ERR_PTR(-EOPNOTSUPP);
1549 /* Some devices send ATT messages as soon as the physical link is
1550 * established. To be able to handle these ATT messages, the user-
1551 * space first establishes the connection and then starts the pairing
1552 * process.
1554 * So if a hci_conn object already exists for the following connection
1555 * attempt, we simply update pending_sec_level and auth_type fields
1556 * and return the object found.
1558 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
1559 if (conn) {
1560 if (conn->pending_sec_level < sec_level)
1561 conn->pending_sec_level = sec_level;
1562 goto done;
1565 BT_DBG("requesting refresh of dst_addr");
1567 conn = hci_conn_add_unset(hdev, LE_LINK, dst, HCI_ROLE_MASTER);
1568 if (IS_ERR(conn))
1569 return conn;
1571 if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) {
1572 hci_conn_del(conn);
1573 return ERR_PTR(-EBUSY);
1576 conn->state = BT_CONNECT;
1577 set_bit(HCI_CONN_SCANNING, &conn->flags);
1578 conn->dst_type = dst_type;
1579 conn->sec_level = BT_SECURITY_LOW;
1580 conn->pending_sec_level = sec_level;
1581 conn->conn_timeout = conn_timeout;
1582 conn->conn_reason = conn_reason;
1584 hci_update_passive_scan(hdev);
1586 done:
1587 hci_conn_hold(conn);
1588 return conn;
1591 struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
1592 u8 sec_level, u8 auth_type,
1593 enum conn_reasons conn_reason, u16 timeout)
1595 struct hci_conn *acl;
1597 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1598 if (lmp_bredr_capable(hdev))
1599 return ERR_PTR(-ECONNREFUSED);
1601 return ERR_PTR(-EOPNOTSUPP);
1604 /* Reject outgoing connection to device with same BD ADDR against
1605 * CVE-2020-26555
1607 if (!bacmp(&hdev->bdaddr, dst)) {
1608 bt_dev_dbg(hdev, "Reject connection with same BD_ADDR %pMR\n",
1609 dst);
1610 return ERR_PTR(-ECONNREFUSED);
1613 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
1614 if (!acl) {
1615 acl = hci_conn_add_unset(hdev, ACL_LINK, dst, HCI_ROLE_MASTER);
1616 if (IS_ERR(acl))
1617 return acl;
1620 hci_conn_hold(acl);
1622 acl->conn_reason = conn_reason;
1623 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
1624 int err;
1626 acl->sec_level = BT_SECURITY_LOW;
1627 acl->pending_sec_level = sec_level;
1628 acl->auth_type = auth_type;
1629 acl->conn_timeout = timeout;
1631 err = hci_connect_acl_sync(hdev, acl);
1632 if (err) {
1633 hci_conn_del(acl);
1634 return ERR_PTR(err);
1638 return acl;
1641 static struct hci_link *hci_conn_link(struct hci_conn *parent,
1642 struct hci_conn *conn)
1644 struct hci_dev *hdev = parent->hdev;
1645 struct hci_link *link;
1647 bt_dev_dbg(hdev, "parent %p hcon %p", parent, conn);
1649 if (conn->link)
1650 return conn->link;
1652 if (conn->parent)
1653 return NULL;
1655 link = kzalloc(sizeof(*link), GFP_KERNEL);
1656 if (!link)
1657 return NULL;
1659 link->conn = hci_conn_hold(conn);
1660 conn->link = link;
1661 conn->parent = hci_conn_get(parent);
1663 /* Use list_add_tail_rcu append to the list */
1664 list_add_tail_rcu(&link->list, &parent->link_list);
1666 return link;
1669 struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
1670 __u16 setting, struct bt_codec *codec,
1671 u16 timeout)
1673 struct hci_conn *acl;
1674 struct hci_conn *sco;
1675 struct hci_link *link;
1677 acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING,
1678 CONN_REASON_SCO_CONNECT, timeout);
1679 if (IS_ERR(acl))
1680 return acl;
1682 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
1683 if (!sco) {
1684 sco = hci_conn_add_unset(hdev, type, dst, HCI_ROLE_MASTER);
1685 if (IS_ERR(sco)) {
1686 hci_conn_drop(acl);
1687 return sco;
1691 link = hci_conn_link(acl, sco);
1692 if (!link) {
1693 hci_conn_drop(acl);
1694 hci_conn_drop(sco);
1695 return ERR_PTR(-ENOLINK);
1698 sco->setting = setting;
1699 sco->codec = *codec;
1701 if (acl->state == BT_CONNECTED &&
1702 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
1703 set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
1704 hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
1706 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
1707 /* defer SCO setup until mode change completed */
1708 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
1709 return sco;
1712 hci_sco_setup(acl, 0x00);
1715 return sco;
1718 static int hci_le_create_big(struct hci_conn *conn, struct bt_iso_qos *qos)
1720 struct hci_dev *hdev = conn->hdev;
1721 struct hci_cp_le_create_big cp;
1722 struct iso_list_data data;
1724 memset(&cp, 0, sizeof(cp));
1726 data.big = qos->bcast.big;
1727 data.bis = qos->bcast.bis;
1728 data.count = 0;
1730 /* Create a BIS for each bound connection */
1731 hci_conn_hash_list_state(hdev, bis_list, ISO_LINK,
1732 BT_BOUND, &data);
1734 cp.handle = qos->bcast.big;
1735 cp.adv_handle = qos->bcast.bis;
1736 cp.num_bis = data.count;
1737 hci_cpu_to_le24(qos->bcast.out.interval, cp.bis.sdu_interval);
1738 cp.bis.sdu = cpu_to_le16(qos->bcast.out.sdu);
1739 cp.bis.latency = cpu_to_le16(qos->bcast.out.latency);
1740 cp.bis.rtn = qos->bcast.out.rtn;
1741 cp.bis.phy = qos->bcast.out.phy;
1742 cp.bis.packing = qos->bcast.packing;
1743 cp.bis.framing = qos->bcast.framing;
1744 cp.bis.encryption = qos->bcast.encryption;
1745 memcpy(cp.bis.bcode, qos->bcast.bcode, sizeof(cp.bis.bcode));
1747 return hci_send_cmd(hdev, HCI_OP_LE_CREATE_BIG, sizeof(cp), &cp);
1750 static int set_cig_params_sync(struct hci_dev *hdev, void *data)
1752 DEFINE_FLEX(struct hci_cp_le_set_cig_params, pdu, cis, num_cis, 0x1f);
1753 u8 cig_id = PTR_UINT(data);
1754 struct hci_conn *conn;
1755 struct bt_iso_qos *qos;
1756 u8 aux_num_cis = 0;
1757 u8 cis_id;
1759 conn = hci_conn_hash_lookup_cig(hdev, cig_id);
1760 if (!conn)
1761 return 0;
1763 qos = &conn->iso_qos;
1764 pdu->cig_id = cig_id;
1765 hci_cpu_to_le24(qos->ucast.out.interval, pdu->c_interval);
1766 hci_cpu_to_le24(qos->ucast.in.interval, pdu->p_interval);
1767 pdu->sca = qos->ucast.sca;
1768 pdu->packing = qos->ucast.packing;
1769 pdu->framing = qos->ucast.framing;
1770 pdu->c_latency = cpu_to_le16(qos->ucast.out.latency);
1771 pdu->p_latency = cpu_to_le16(qos->ucast.in.latency);
1773 /* Reprogram all CIS(s) with the same CIG, valid range are:
1774 * num_cis: 0x00 to 0x1F
1775 * cis_id: 0x00 to 0xEF
1777 for (cis_id = 0x00; cis_id < 0xf0 &&
1778 aux_num_cis < pdu->num_cis; cis_id++) {
1779 struct hci_cis_params *cis;
1781 conn = hci_conn_hash_lookup_cis(hdev, NULL, 0, cig_id, cis_id);
1782 if (!conn)
1783 continue;
1785 qos = &conn->iso_qos;
1787 cis = &pdu->cis[aux_num_cis++];
1788 cis->cis_id = cis_id;
1789 cis->c_sdu = cpu_to_le16(conn->iso_qos.ucast.out.sdu);
1790 cis->p_sdu = cpu_to_le16(conn->iso_qos.ucast.in.sdu);
1791 cis->c_phy = qos->ucast.out.phy ? qos->ucast.out.phy :
1792 qos->ucast.in.phy;
1793 cis->p_phy = qos->ucast.in.phy ? qos->ucast.in.phy :
1794 qos->ucast.out.phy;
1795 cis->c_rtn = qos->ucast.out.rtn;
1796 cis->p_rtn = qos->ucast.in.rtn;
1798 pdu->num_cis = aux_num_cis;
1800 if (!pdu->num_cis)
1801 return 0;
1803 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_CIG_PARAMS,
1804 struct_size(pdu, cis, pdu->num_cis),
1805 pdu, HCI_CMD_TIMEOUT);
1808 static bool hci_le_set_cig_params(struct hci_conn *conn, struct bt_iso_qos *qos)
1810 struct hci_dev *hdev = conn->hdev;
1811 struct iso_list_data data;
1813 memset(&data, 0, sizeof(data));
1815 /* Allocate first still reconfigurable CIG if not set */
1816 if (qos->ucast.cig == BT_ISO_QOS_CIG_UNSET) {
1817 for (data.cig = 0x00; data.cig < 0xf0; data.cig++) {
1818 data.count = 0;
1820 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK,
1821 BT_CONNECT, &data);
1822 if (data.count)
1823 continue;
1825 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK,
1826 BT_CONNECTED, &data);
1827 if (!data.count)
1828 break;
1831 if (data.cig == 0xf0)
1832 return false;
1834 /* Update CIG */
1835 qos->ucast.cig = data.cig;
1838 if (qos->ucast.cis != BT_ISO_QOS_CIS_UNSET) {
1839 if (hci_conn_hash_lookup_cis(hdev, NULL, 0, qos->ucast.cig,
1840 qos->ucast.cis))
1841 return false;
1842 goto done;
1845 /* Allocate first available CIS if not set */
1846 for (data.cig = qos->ucast.cig, data.cis = 0x00; data.cis < 0xf0;
1847 data.cis++) {
1848 if (!hci_conn_hash_lookup_cis(hdev, NULL, 0, data.cig,
1849 data.cis)) {
1850 /* Update CIS */
1851 qos->ucast.cis = data.cis;
1852 break;
1856 if (qos->ucast.cis == BT_ISO_QOS_CIS_UNSET)
1857 return false;
1859 done:
1860 if (hci_cmd_sync_queue(hdev, set_cig_params_sync,
1861 UINT_PTR(qos->ucast.cig), NULL) < 0)
1862 return false;
1864 return true;
1867 struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst,
1868 __u8 dst_type, struct bt_iso_qos *qos)
1870 struct hci_conn *cis;
1872 cis = hci_conn_hash_lookup_cis(hdev, dst, dst_type, qos->ucast.cig,
1873 qos->ucast.cis);
1874 if (!cis) {
1875 cis = hci_conn_add_unset(hdev, ISO_LINK, dst, HCI_ROLE_MASTER);
1876 if (IS_ERR(cis))
1877 return cis;
1878 cis->cleanup = cis_cleanup;
1879 cis->dst_type = dst_type;
1880 cis->iso_qos.ucast.cig = BT_ISO_QOS_CIG_UNSET;
1881 cis->iso_qos.ucast.cis = BT_ISO_QOS_CIS_UNSET;
1884 if (cis->state == BT_CONNECTED)
1885 return cis;
1887 /* Check if CIS has been set and the settings matches */
1888 if (cis->state == BT_BOUND &&
1889 !memcmp(&cis->iso_qos, qos, sizeof(*qos)))
1890 return cis;
1892 /* Update LINK PHYs according to QoS preference */
1893 cis->le_tx_phy = qos->ucast.out.phy;
1894 cis->le_rx_phy = qos->ucast.in.phy;
1896 /* If output interval is not set use the input interval as it cannot be
1897 * 0x000000.
1899 if (!qos->ucast.out.interval)
1900 qos->ucast.out.interval = qos->ucast.in.interval;
1902 /* If input interval is not set use the output interval as it cannot be
1903 * 0x000000.
1905 if (!qos->ucast.in.interval)
1906 qos->ucast.in.interval = qos->ucast.out.interval;
1908 /* If output latency is not set use the input latency as it cannot be
1909 * 0x0000.
1911 if (!qos->ucast.out.latency)
1912 qos->ucast.out.latency = qos->ucast.in.latency;
1914 /* If input latency is not set use the output latency as it cannot be
1915 * 0x0000.
1917 if (!qos->ucast.in.latency)
1918 qos->ucast.in.latency = qos->ucast.out.latency;
1920 if (!hci_le_set_cig_params(cis, qos)) {
1921 hci_conn_drop(cis);
1922 return ERR_PTR(-EINVAL);
1925 hci_conn_hold(cis);
1927 cis->iso_qos = *qos;
1928 cis->state = BT_BOUND;
1930 return cis;
1933 bool hci_iso_setup_path(struct hci_conn *conn)
1935 struct hci_dev *hdev = conn->hdev;
1936 struct hci_cp_le_setup_iso_path cmd;
1938 memset(&cmd, 0, sizeof(cmd));
1940 if (conn->iso_qos.ucast.out.sdu) {
1941 cmd.handle = cpu_to_le16(conn->handle);
1942 cmd.direction = 0x00; /* Input (Host to Controller) */
1943 cmd.path = 0x00; /* HCI path if enabled */
1944 cmd.codec = 0x03; /* Transparent Data */
1946 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
1947 &cmd) < 0)
1948 return false;
1951 if (conn->iso_qos.ucast.in.sdu) {
1952 cmd.handle = cpu_to_le16(conn->handle);
1953 cmd.direction = 0x01; /* Output (Controller to Host) */
1954 cmd.path = 0x00; /* HCI path if enabled */
1955 cmd.codec = 0x03; /* Transparent Data */
1957 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
1958 &cmd) < 0)
1959 return false;
1962 return true;
1965 int hci_conn_check_create_cis(struct hci_conn *conn)
1967 if (conn->type != ISO_LINK || !bacmp(&conn->dst, BDADDR_ANY))
1968 return -EINVAL;
1970 if (!conn->parent || conn->parent->state != BT_CONNECTED ||
1971 conn->state != BT_CONNECT || HCI_CONN_HANDLE_UNSET(conn->handle))
1972 return 1;
1974 return 0;
1977 static int hci_create_cis_sync(struct hci_dev *hdev, void *data)
1979 return hci_le_create_cis_sync(hdev);
1982 int hci_le_create_cis_pending(struct hci_dev *hdev)
1984 struct hci_conn *conn;
1985 bool pending = false;
1987 rcu_read_lock();
1989 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
1990 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) {
1991 rcu_read_unlock();
1992 return -EBUSY;
1995 if (!hci_conn_check_create_cis(conn))
1996 pending = true;
1999 rcu_read_unlock();
2001 if (!pending)
2002 return 0;
2004 /* Queue Create CIS */
2005 return hci_cmd_sync_queue(hdev, hci_create_cis_sync, NULL, NULL);
2008 static void hci_iso_qos_setup(struct hci_dev *hdev, struct hci_conn *conn,
2009 struct bt_iso_io_qos *qos, __u8 phy)
2011 /* Only set MTU if PHY is enabled */
2012 if (!qos->sdu && qos->phy)
2013 qos->sdu = conn->mtu;
2015 /* Use the same PHY as ACL if set to any */
2016 if (qos->phy == BT_ISO_PHY_ANY)
2017 qos->phy = phy;
2019 /* Use LE ACL connection interval if not set */
2020 if (!qos->interval)
2021 /* ACL interval unit in 1.25 ms to us */
2022 qos->interval = conn->le_conn_interval * 1250;
2024 /* Use LE ACL connection latency if not set */
2025 if (!qos->latency)
2026 qos->latency = conn->le_conn_latency;
2029 static int create_big_sync(struct hci_dev *hdev, void *data)
2031 struct hci_conn *conn = data;
2032 struct bt_iso_qos *qos = &conn->iso_qos;
2033 u16 interval, sync_interval = 0;
2034 u32 flags = 0;
2035 int err;
2037 if (qos->bcast.out.phy == 0x02)
2038 flags |= MGMT_ADV_FLAG_SEC_2M;
2040 /* Align intervals */
2041 interval = (qos->bcast.out.interval / 1250) * qos->bcast.sync_factor;
2043 if (qos->bcast.bis)
2044 sync_interval = interval * 4;
2046 err = hci_start_per_adv_sync(hdev, qos->bcast.bis, conn->le_per_adv_data_len,
2047 conn->le_per_adv_data, flags, interval,
2048 interval, sync_interval);
2049 if (err)
2050 return err;
2052 return hci_le_create_big(conn, &conn->iso_qos);
2055 static void create_pa_complete(struct hci_dev *hdev, void *data, int err)
2057 struct hci_cp_le_pa_create_sync *cp = data;
2059 bt_dev_dbg(hdev, "");
2061 if (err)
2062 bt_dev_err(hdev, "Unable to create PA: %d", err);
2064 kfree(cp);
2067 static int create_pa_sync(struct hci_dev *hdev, void *data)
2069 struct hci_cp_le_pa_create_sync *cp = data;
2070 int err;
2072 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_CREATE_SYNC,
2073 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
2074 if (err) {
2075 hci_dev_clear_flag(hdev, HCI_PA_SYNC);
2076 return err;
2079 return hci_update_passive_scan_sync(hdev);
2082 struct hci_conn *hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst,
2083 __u8 dst_type, __u8 sid,
2084 struct bt_iso_qos *qos)
2086 struct hci_cp_le_pa_create_sync *cp;
2087 struct hci_conn *conn;
2088 int err;
2090 if (hci_dev_test_and_set_flag(hdev, HCI_PA_SYNC))
2091 return ERR_PTR(-EBUSY);
2093 conn = hci_conn_add_unset(hdev, ISO_LINK, dst, HCI_ROLE_SLAVE);
2094 if (IS_ERR(conn))
2095 return conn;
2097 conn->iso_qos = *qos;
2098 conn->state = BT_LISTEN;
2100 hci_conn_hold(conn);
2102 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2103 if (!cp) {
2104 hci_dev_clear_flag(hdev, HCI_PA_SYNC);
2105 hci_conn_drop(conn);
2106 return ERR_PTR(-ENOMEM);
2109 cp->options = qos->bcast.options;
2110 cp->sid = sid;
2111 cp->addr_type = dst_type;
2112 bacpy(&cp->addr, dst);
2113 cp->skip = cpu_to_le16(qos->bcast.skip);
2114 cp->sync_timeout = cpu_to_le16(qos->bcast.sync_timeout);
2115 cp->sync_cte_type = qos->bcast.sync_cte_type;
2117 /* Queue start pa_create_sync and scan */
2118 err = hci_cmd_sync_queue(hdev, create_pa_sync, cp, create_pa_complete);
2119 if (err < 0) {
2120 hci_conn_drop(conn);
2121 kfree(cp);
2122 return ERR_PTR(err);
2125 return conn;
2128 int hci_le_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon,
2129 struct bt_iso_qos *qos,
2130 __u16 sync_handle, __u8 num_bis, __u8 bis[])
2132 DEFINE_FLEX(struct hci_cp_le_big_create_sync, pdu, bis, num_bis, 0x11);
2133 int err;
2135 if (num_bis < 0x01 || num_bis > pdu->num_bis)
2136 return -EINVAL;
2138 err = qos_set_big(hdev, qos);
2139 if (err)
2140 return err;
2142 if (hcon)
2143 hcon->iso_qos.bcast.big = qos->bcast.big;
2145 pdu->handle = qos->bcast.big;
2146 pdu->sync_handle = cpu_to_le16(sync_handle);
2147 pdu->encryption = qos->bcast.encryption;
2148 memcpy(pdu->bcode, qos->bcast.bcode, sizeof(pdu->bcode));
2149 pdu->mse = qos->bcast.mse;
2150 pdu->timeout = cpu_to_le16(qos->bcast.timeout);
2151 pdu->num_bis = num_bis;
2152 memcpy(pdu->bis, bis, num_bis);
2154 return hci_send_cmd(hdev, HCI_OP_LE_BIG_CREATE_SYNC,
2155 struct_size(pdu, bis, num_bis), pdu);
2158 static void create_big_complete(struct hci_dev *hdev, void *data, int err)
2160 struct hci_conn *conn = data;
2162 bt_dev_dbg(hdev, "conn %p", conn);
2164 if (err) {
2165 bt_dev_err(hdev, "Unable to create BIG: %d", err);
2166 hci_connect_cfm(conn, err);
2167 hci_conn_del(conn);
2171 struct hci_conn *hci_bind_bis(struct hci_dev *hdev, bdaddr_t *dst,
2172 struct bt_iso_qos *qos,
2173 __u8 base_len, __u8 *base)
2175 struct hci_conn *conn;
2176 struct hci_conn *parent;
2177 __u8 eir[HCI_MAX_PER_AD_LENGTH];
2178 struct hci_link *link;
2180 /* Look for any BIS that is open for rebinding */
2181 conn = hci_conn_hash_lookup_big_state(hdev, qos->bcast.big, BT_OPEN);
2182 if (conn) {
2183 memcpy(qos, &conn->iso_qos, sizeof(*qos));
2184 conn->state = BT_CONNECTED;
2185 return conn;
2188 if (base_len && base)
2189 base_len = eir_append_service_data(eir, 0, 0x1851,
2190 base, base_len);
2192 /* We need hci_conn object using the BDADDR_ANY as dst */
2193 conn = hci_add_bis(hdev, dst, qos, base_len, eir);
2194 if (IS_ERR(conn))
2195 return conn;
2197 /* Update LINK PHYs according to QoS preference */
2198 conn->le_tx_phy = qos->bcast.out.phy;
2199 conn->le_tx_phy = qos->bcast.out.phy;
2201 /* Add Basic Announcement into Peridic Adv Data if BASE is set */
2202 if (base_len && base) {
2203 memcpy(conn->le_per_adv_data, eir, sizeof(eir));
2204 conn->le_per_adv_data_len = base_len;
2207 hci_iso_qos_setup(hdev, conn, &qos->bcast.out,
2208 conn->le_tx_phy ? conn->le_tx_phy :
2209 hdev->le_tx_def_phys);
2211 conn->iso_qos = *qos;
2212 conn->state = BT_BOUND;
2214 /* Link BISes together */
2215 parent = hci_conn_hash_lookup_big(hdev,
2216 conn->iso_qos.bcast.big);
2217 if (parent && parent != conn) {
2218 link = hci_conn_link(parent, conn);
2219 if (!link) {
2220 hci_conn_drop(conn);
2221 return ERR_PTR(-ENOLINK);
2224 /* Link takes the refcount */
2225 hci_conn_drop(conn);
2228 return conn;
2231 static void bis_mark_per_adv(struct hci_conn *conn, void *data)
2233 struct iso_list_data *d = data;
2235 /* Skip if not broadcast/ANY address */
2236 if (bacmp(&conn->dst, BDADDR_ANY))
2237 return;
2239 if (d->big != conn->iso_qos.bcast.big ||
2240 d->bis == BT_ISO_QOS_BIS_UNSET ||
2241 d->bis != conn->iso_qos.bcast.bis)
2242 return;
2244 set_bit(HCI_CONN_PER_ADV, &conn->flags);
2247 struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst,
2248 __u8 dst_type, struct bt_iso_qos *qos,
2249 __u8 base_len, __u8 *base)
2251 struct hci_conn *conn;
2252 int err;
2253 struct iso_list_data data;
2255 conn = hci_bind_bis(hdev, dst, qos, base_len, base);
2256 if (IS_ERR(conn))
2257 return conn;
2259 if (conn->state == BT_CONNECTED)
2260 return conn;
2262 data.big = qos->bcast.big;
2263 data.bis = qos->bcast.bis;
2265 /* Set HCI_CONN_PER_ADV for all bound connections, to mark that
2266 * the start periodic advertising and create BIG commands have
2267 * been queued
2269 hci_conn_hash_list_state(hdev, bis_mark_per_adv, ISO_LINK,
2270 BT_BOUND, &data);
2272 /* Queue start periodic advertising and create BIG */
2273 err = hci_cmd_sync_queue(hdev, create_big_sync, conn,
2274 create_big_complete);
2275 if (err < 0) {
2276 hci_conn_drop(conn);
2277 return ERR_PTR(err);
2280 return conn;
2283 struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst,
2284 __u8 dst_type, struct bt_iso_qos *qos)
2286 struct hci_conn *le;
2287 struct hci_conn *cis;
2288 struct hci_link *link;
2290 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
2291 le = hci_connect_le(hdev, dst, dst_type, false,
2292 BT_SECURITY_LOW,
2293 HCI_LE_CONN_TIMEOUT,
2294 HCI_ROLE_SLAVE, 0, 0);
2295 else
2296 le = hci_connect_le_scan(hdev, dst, dst_type,
2297 BT_SECURITY_LOW,
2298 HCI_LE_CONN_TIMEOUT,
2299 CONN_REASON_ISO_CONNECT);
2300 if (IS_ERR(le))
2301 return le;
2303 hci_iso_qos_setup(hdev, le, &qos->ucast.out,
2304 le->le_tx_phy ? le->le_tx_phy : hdev->le_tx_def_phys);
2305 hci_iso_qos_setup(hdev, le, &qos->ucast.in,
2306 le->le_rx_phy ? le->le_rx_phy : hdev->le_rx_def_phys);
2308 cis = hci_bind_cis(hdev, dst, dst_type, qos);
2309 if (IS_ERR(cis)) {
2310 hci_conn_drop(le);
2311 return cis;
2314 link = hci_conn_link(le, cis);
2315 if (!link) {
2316 hci_conn_drop(le);
2317 hci_conn_drop(cis);
2318 return ERR_PTR(-ENOLINK);
2321 /* Link takes the refcount */
2322 hci_conn_drop(cis);
2324 cis->state = BT_CONNECT;
2326 hci_le_create_cis_pending(hdev);
2328 return cis;
2331 /* Check link security requirement */
2332 int hci_conn_check_link_mode(struct hci_conn *conn)
2334 BT_DBG("hcon %p", conn);
2336 /* In Secure Connections Only mode, it is required that Secure
2337 * Connections is used and the link is encrypted with AES-CCM
2338 * using a P-256 authenticated combination key.
2340 if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
2341 if (!hci_conn_sc_enabled(conn) ||
2342 !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
2343 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
2344 return 0;
2347 /* AES encryption is required for Level 4:
2349 * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part C
2350 * page 1319:
2352 * 128-bit equivalent strength for link and encryption keys
2353 * required using FIPS approved algorithms (E0 not allowed,
2354 * SAFER+ not allowed, and P-192 not allowed; encryption key
2355 * not shortened)
2357 if (conn->sec_level == BT_SECURITY_FIPS &&
2358 !test_bit(HCI_CONN_AES_CCM, &conn->flags)) {
2359 bt_dev_err(conn->hdev,
2360 "Invalid security: Missing AES-CCM usage");
2361 return 0;
2364 if (hci_conn_ssp_enabled(conn) &&
2365 !test_bit(HCI_CONN_ENCRYPT, &conn->flags))
2366 return 0;
2368 return 1;
2371 /* Authenticate remote device */
2372 static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
2374 BT_DBG("hcon %p", conn);
2376 if (conn->pending_sec_level > sec_level)
2377 sec_level = conn->pending_sec_level;
2379 if (sec_level > conn->sec_level)
2380 conn->pending_sec_level = sec_level;
2381 else if (test_bit(HCI_CONN_AUTH, &conn->flags))
2382 return 1;
2384 /* Make sure we preserve an existing MITM requirement*/
2385 auth_type |= (conn->auth_type & 0x01);
2387 conn->auth_type = auth_type;
2389 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
2390 struct hci_cp_auth_requested cp;
2392 cp.handle = cpu_to_le16(conn->handle);
2393 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
2394 sizeof(cp), &cp);
2396 /* Set the ENCRYPT_PEND to trigger encryption after
2397 * authentication.
2399 if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
2400 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
2403 return 0;
2406 /* Encrypt the link */
2407 static void hci_conn_encrypt(struct hci_conn *conn)
2409 BT_DBG("hcon %p", conn);
2411 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
2412 struct hci_cp_set_conn_encrypt cp;
2413 cp.handle = cpu_to_le16(conn->handle);
2414 cp.encrypt = 0x01;
2415 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
2416 &cp);
2420 /* Enable security */
2421 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
2422 bool initiator)
2424 BT_DBG("hcon %p", conn);
2426 if (conn->type == LE_LINK)
2427 return smp_conn_security(conn, sec_level);
2429 /* For sdp we don't need the link key. */
2430 if (sec_level == BT_SECURITY_SDP)
2431 return 1;
2433 /* For non 2.1 devices and low security level we don't need the link
2434 key. */
2435 if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
2436 return 1;
2438 /* For other security levels we need the link key. */
2439 if (!test_bit(HCI_CONN_AUTH, &conn->flags))
2440 goto auth;
2442 switch (conn->key_type) {
2443 case HCI_LK_AUTH_COMBINATION_P256:
2444 /* An authenticated FIPS approved combination key has
2445 * sufficient security for security level 4 or lower.
2447 if (sec_level <= BT_SECURITY_FIPS)
2448 goto encrypt;
2449 break;
2450 case HCI_LK_AUTH_COMBINATION_P192:
2451 /* An authenticated combination key has sufficient security for
2452 * security level 3 or lower.
2454 if (sec_level <= BT_SECURITY_HIGH)
2455 goto encrypt;
2456 break;
2457 case HCI_LK_UNAUTH_COMBINATION_P192:
2458 case HCI_LK_UNAUTH_COMBINATION_P256:
2459 /* An unauthenticated combination key has sufficient security
2460 * for security level 2 or lower.
2462 if (sec_level <= BT_SECURITY_MEDIUM)
2463 goto encrypt;
2464 break;
2465 case HCI_LK_COMBINATION:
2466 /* A combination key has always sufficient security for the
2467 * security levels 2 or lower. High security level requires the
2468 * combination key is generated using maximum PIN code length
2469 * (16). For pre 2.1 units.
2471 if (sec_level <= BT_SECURITY_MEDIUM || conn->pin_length == 16)
2472 goto encrypt;
2473 break;
2474 default:
2475 break;
2478 auth:
2479 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
2480 return 0;
2482 if (initiator)
2483 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2485 if (!hci_conn_auth(conn, sec_level, auth_type))
2486 return 0;
2488 encrypt:
2489 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) {
2490 /* Ensure that the encryption key size has been read,
2491 * otherwise stall the upper layer responses.
2493 if (!conn->enc_key_size)
2494 return 0;
2496 /* Nothing else needed, all requirements are met */
2497 return 1;
2500 hci_conn_encrypt(conn);
2501 return 0;
2503 EXPORT_SYMBOL(hci_conn_security);
2505 /* Check secure link requirement */
2506 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
2508 BT_DBG("hcon %p", conn);
2510 /* Accept if non-secure or higher security level is required */
2511 if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS)
2512 return 1;
2514 /* Accept if secure or higher security level is already present */
2515 if (conn->sec_level == BT_SECURITY_HIGH ||
2516 conn->sec_level == BT_SECURITY_FIPS)
2517 return 1;
2519 /* Reject not secure link */
2520 return 0;
2522 EXPORT_SYMBOL(hci_conn_check_secure);
2524 /* Switch role */
2525 int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
2527 BT_DBG("hcon %p", conn);
2529 if (role == conn->role)
2530 return 1;
2532 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
2533 struct hci_cp_switch_role cp;
2534 bacpy(&cp.bdaddr, &conn->dst);
2535 cp.role = role;
2536 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
2539 return 0;
2541 EXPORT_SYMBOL(hci_conn_switch_role);
2543 /* Enter active mode */
2544 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
2546 struct hci_dev *hdev = conn->hdev;
2548 BT_DBG("hcon %p mode %d", conn, conn->mode);
2550 if (conn->mode != HCI_CM_SNIFF)
2551 goto timer;
2553 if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
2554 goto timer;
2556 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
2557 struct hci_cp_exit_sniff_mode cp;
2558 cp.handle = cpu_to_le16(conn->handle);
2559 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
2562 timer:
2563 if (hdev->idle_timeout > 0)
2564 queue_delayed_work(hdev->workqueue, &conn->idle_work,
2565 msecs_to_jiffies(hdev->idle_timeout));
2568 /* Drop all connection on the device */
2569 void hci_conn_hash_flush(struct hci_dev *hdev)
2571 struct list_head *head = &hdev->conn_hash.list;
2572 struct hci_conn *conn;
2574 BT_DBG("hdev %s", hdev->name);
2576 /* We should not traverse the list here, because hci_conn_del
2577 * can remove extra links, which may cause the list traversal
2578 * to hit items that have already been released.
2580 while ((conn = list_first_entry_or_null(head,
2581 struct hci_conn,
2582 list)) != NULL) {
2583 conn->state = BT_CLOSED;
2584 hci_disconn_cfm(conn, HCI_ERROR_LOCAL_HOST_TERM);
2585 hci_conn_del(conn);
2589 static u32 get_link_mode(struct hci_conn *conn)
2591 u32 link_mode = 0;
2593 if (conn->role == HCI_ROLE_MASTER)
2594 link_mode |= HCI_LM_MASTER;
2596 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
2597 link_mode |= HCI_LM_ENCRYPT;
2599 if (test_bit(HCI_CONN_AUTH, &conn->flags))
2600 link_mode |= HCI_LM_AUTH;
2602 if (test_bit(HCI_CONN_SECURE, &conn->flags))
2603 link_mode |= HCI_LM_SECURE;
2605 if (test_bit(HCI_CONN_FIPS, &conn->flags))
2606 link_mode |= HCI_LM_FIPS;
2608 return link_mode;
2611 int hci_get_conn_list(void __user *arg)
2613 struct hci_conn *c;
2614 struct hci_conn_list_req req, *cl;
2615 struct hci_conn_info *ci;
2616 struct hci_dev *hdev;
2617 int n = 0, size, err;
2619 if (copy_from_user(&req, arg, sizeof(req)))
2620 return -EFAULT;
2622 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
2623 return -EINVAL;
2625 size = sizeof(req) + req.conn_num * sizeof(*ci);
2627 cl = kmalloc(size, GFP_KERNEL);
2628 if (!cl)
2629 return -ENOMEM;
2631 hdev = hci_dev_get(req.dev_id);
2632 if (!hdev) {
2633 kfree(cl);
2634 return -ENODEV;
2637 ci = cl->conn_info;
2639 hci_dev_lock(hdev);
2640 list_for_each_entry(c, &hdev->conn_hash.list, list) {
2641 bacpy(&(ci + n)->bdaddr, &c->dst);
2642 (ci + n)->handle = c->handle;
2643 (ci + n)->type = c->type;
2644 (ci + n)->out = c->out;
2645 (ci + n)->state = c->state;
2646 (ci + n)->link_mode = get_link_mode(c);
2647 if (++n >= req.conn_num)
2648 break;
2650 hci_dev_unlock(hdev);
2652 cl->dev_id = hdev->id;
2653 cl->conn_num = n;
2654 size = sizeof(req) + n * sizeof(*ci);
2656 hci_dev_put(hdev);
2658 err = copy_to_user(arg, cl, size);
2659 kfree(cl);
2661 return err ? -EFAULT : 0;
2664 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
2666 struct hci_conn_info_req req;
2667 struct hci_conn_info ci;
2668 struct hci_conn *conn;
2669 char __user *ptr = arg + sizeof(req);
2671 if (copy_from_user(&req, arg, sizeof(req)))
2672 return -EFAULT;
2674 hci_dev_lock(hdev);
2675 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
2676 if (conn) {
2677 bacpy(&ci.bdaddr, &conn->dst);
2678 ci.handle = conn->handle;
2679 ci.type = conn->type;
2680 ci.out = conn->out;
2681 ci.state = conn->state;
2682 ci.link_mode = get_link_mode(conn);
2684 hci_dev_unlock(hdev);
2686 if (!conn)
2687 return -ENOENT;
2689 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
2692 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
2694 struct hci_auth_info_req req;
2695 struct hci_conn *conn;
2697 if (copy_from_user(&req, arg, sizeof(req)))
2698 return -EFAULT;
2700 hci_dev_lock(hdev);
2701 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
2702 if (conn)
2703 req.type = conn->auth_type;
2704 hci_dev_unlock(hdev);
2706 if (!conn)
2707 return -ENOENT;
2709 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
2712 struct hci_chan *hci_chan_create(struct hci_conn *conn)
2714 struct hci_dev *hdev = conn->hdev;
2715 struct hci_chan *chan;
2717 BT_DBG("%s hcon %p", hdev->name, conn);
2719 if (test_bit(HCI_CONN_DROP, &conn->flags)) {
2720 BT_DBG("Refusing to create new hci_chan");
2721 return NULL;
2724 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
2725 if (!chan)
2726 return NULL;
2728 chan->conn = hci_conn_get(conn);
2729 skb_queue_head_init(&chan->data_q);
2730 chan->state = BT_CONNECTED;
2732 list_add_rcu(&chan->list, &conn->chan_list);
2734 return chan;
2737 void hci_chan_del(struct hci_chan *chan)
2739 struct hci_conn *conn = chan->conn;
2740 struct hci_dev *hdev = conn->hdev;
2742 BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan);
2744 list_del_rcu(&chan->list);
2746 synchronize_rcu();
2748 /* Prevent new hci_chan's to be created for this hci_conn */
2749 set_bit(HCI_CONN_DROP, &conn->flags);
2751 hci_conn_put(conn);
2753 skb_queue_purge(&chan->data_q);
2754 kfree(chan);
2757 void hci_chan_list_flush(struct hci_conn *conn)
2759 struct hci_chan *chan, *n;
2761 BT_DBG("hcon %p", conn);
2763 list_for_each_entry_safe(chan, n, &conn->chan_list, list)
2764 hci_chan_del(chan);
2767 static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon,
2768 __u16 handle)
2770 struct hci_chan *hchan;
2772 list_for_each_entry(hchan, &hcon->chan_list, list) {
2773 if (hchan->handle == handle)
2774 return hchan;
2777 return NULL;
2780 struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle)
2782 struct hci_conn_hash *h = &hdev->conn_hash;
2783 struct hci_conn *hcon;
2784 struct hci_chan *hchan = NULL;
2786 rcu_read_lock();
2788 list_for_each_entry_rcu(hcon, &h->list, list) {
2789 hchan = __hci_chan_lookup_handle(hcon, handle);
2790 if (hchan)
2791 break;
2794 rcu_read_unlock();
2796 return hchan;
2799 u32 hci_conn_get_phy(struct hci_conn *conn)
2801 u32 phys = 0;
2803 /* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471:
2804 * Table 6.2: Packets defined for synchronous, asynchronous, and
2805 * CPB logical transport types.
2807 switch (conn->type) {
2808 case SCO_LINK:
2809 /* SCO logical transport (1 Mb/s):
2810 * HV1, HV2, HV3 and DV.
2812 phys |= BT_PHY_BR_1M_1SLOT;
2814 break;
2816 case ACL_LINK:
2817 /* ACL logical transport (1 Mb/s) ptt=0:
2818 * DH1, DM3, DH3, DM5 and DH5.
2820 phys |= BT_PHY_BR_1M_1SLOT;
2822 if (conn->pkt_type & (HCI_DM3 | HCI_DH3))
2823 phys |= BT_PHY_BR_1M_3SLOT;
2825 if (conn->pkt_type & (HCI_DM5 | HCI_DH5))
2826 phys |= BT_PHY_BR_1M_5SLOT;
2828 /* ACL logical transport (2 Mb/s) ptt=1:
2829 * 2-DH1, 2-DH3 and 2-DH5.
2831 if (!(conn->pkt_type & HCI_2DH1))
2832 phys |= BT_PHY_EDR_2M_1SLOT;
2834 if (!(conn->pkt_type & HCI_2DH3))
2835 phys |= BT_PHY_EDR_2M_3SLOT;
2837 if (!(conn->pkt_type & HCI_2DH5))
2838 phys |= BT_PHY_EDR_2M_5SLOT;
2840 /* ACL logical transport (3 Mb/s) ptt=1:
2841 * 3-DH1, 3-DH3 and 3-DH5.
2843 if (!(conn->pkt_type & HCI_3DH1))
2844 phys |= BT_PHY_EDR_3M_1SLOT;
2846 if (!(conn->pkt_type & HCI_3DH3))
2847 phys |= BT_PHY_EDR_3M_3SLOT;
2849 if (!(conn->pkt_type & HCI_3DH5))
2850 phys |= BT_PHY_EDR_3M_5SLOT;
2852 break;
2854 case ESCO_LINK:
2855 /* eSCO logical transport (1 Mb/s): EV3, EV4 and EV5 */
2856 phys |= BT_PHY_BR_1M_1SLOT;
2858 if (!(conn->pkt_type & (ESCO_EV4 | ESCO_EV5)))
2859 phys |= BT_PHY_BR_1M_3SLOT;
2861 /* eSCO logical transport (2 Mb/s): 2-EV3, 2-EV5 */
2862 if (!(conn->pkt_type & ESCO_2EV3))
2863 phys |= BT_PHY_EDR_2M_1SLOT;
2865 if (!(conn->pkt_type & ESCO_2EV5))
2866 phys |= BT_PHY_EDR_2M_3SLOT;
2868 /* eSCO logical transport (3 Mb/s): 3-EV3, 3-EV5 */
2869 if (!(conn->pkt_type & ESCO_3EV3))
2870 phys |= BT_PHY_EDR_3M_1SLOT;
2872 if (!(conn->pkt_type & ESCO_3EV5))
2873 phys |= BT_PHY_EDR_3M_3SLOT;
2875 break;
2877 case LE_LINK:
2878 if (conn->le_tx_phy & HCI_LE_SET_PHY_1M)
2879 phys |= BT_PHY_LE_1M_TX;
2881 if (conn->le_rx_phy & HCI_LE_SET_PHY_1M)
2882 phys |= BT_PHY_LE_1M_RX;
2884 if (conn->le_tx_phy & HCI_LE_SET_PHY_2M)
2885 phys |= BT_PHY_LE_2M_TX;
2887 if (conn->le_rx_phy & HCI_LE_SET_PHY_2M)
2888 phys |= BT_PHY_LE_2M_RX;
2890 if (conn->le_tx_phy & HCI_LE_SET_PHY_CODED)
2891 phys |= BT_PHY_LE_CODED_TX;
2893 if (conn->le_rx_phy & HCI_LE_SET_PHY_CODED)
2894 phys |= BT_PHY_LE_CODED_RX;
2896 break;
2899 return phys;
2902 static int abort_conn_sync(struct hci_dev *hdev, void *data)
2904 struct hci_conn *conn = data;
2906 if (!hci_conn_valid(hdev, conn))
2907 return -ECANCELED;
2909 return hci_abort_conn_sync(hdev, conn, conn->abort_reason);
2912 int hci_abort_conn(struct hci_conn *conn, u8 reason)
2914 struct hci_dev *hdev = conn->hdev;
2916 /* If abort_reason has already been set it means the connection is
2917 * already being aborted so don't attempt to overwrite it.
2919 if (conn->abort_reason)
2920 return 0;
2922 bt_dev_dbg(hdev, "handle 0x%2.2x reason 0x%2.2x", conn->handle, reason);
2924 conn->abort_reason = reason;
2926 /* If the connection is pending check the command opcode since that
2927 * might be blocking on hci_cmd_sync_work while waiting its respective
2928 * event so we need to hci_cmd_sync_cancel to cancel it.
2930 * hci_connect_le serializes the connection attempts so only one
2931 * connection can be in BT_CONNECT at time.
2933 if (conn->state == BT_CONNECT && hdev->req_status == HCI_REQ_PEND) {
2934 switch (hci_skb_event(hdev->sent_cmd)) {
2935 case HCI_EV_CONN_COMPLETE:
2936 case HCI_EV_LE_CONN_COMPLETE:
2937 case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
2938 case HCI_EVT_LE_CIS_ESTABLISHED:
2939 hci_cmd_sync_cancel(hdev, ECANCELED);
2940 break;
2942 /* Cancel connect attempt if still queued/pending */
2943 } else if (!hci_cancel_connect_sync(hdev, conn)) {
2944 return 0;
2947 return hci_cmd_sync_queue_once(hdev, abort_conn_sync, conn, NULL);