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 5 6 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License version 2 as 10 published by the Free Software Foundation; 11 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 13 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 15 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 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. 20 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. 24 */ 25 26 /* Bluetooth HCI connection handling. */ 27 28 #include <linux/export.h> 29 #include <linux/debugfs.h> 30 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> 36 37 #include "smp.h" 38 #include "eir.h" 39 40 struct sco_param { 41 u16 pkt_type; 42 u16 max_latency; 43 u8 retrans_effort; 44 }; 45 46 struct conn_handle_t { 47 struct hci_conn *conn; 48 __u16 handle; 49 }; 50 51 static const struct sco_param esco_param_cvsd[] = { 52 { 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 */ 57 }; 58 59 static const struct sco_param sco_param_cvsd[] = { 60 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */ 61 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */ 62 }; 63 64 static const struct sco_param esco_param_msbc[] = { 65 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */ 66 { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */ 67 }; 68 69 /* This function requires the caller holds hdev->lock */ 70 void hci_connect_le_scan_cleanup(struct hci_conn *conn, u8 status) 71 { 72 struct hci_conn_params *params; 73 struct hci_dev *hdev = conn->hdev; 74 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 */ 82 irk = hci_get_irk(hdev, bdaddr, bdaddr_type); 83 if (irk) { 84 bdaddr = &irk->bdaddr; 85 bdaddr_type = irk->addr_type; 86 } 87 88 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr, 89 bdaddr_type); 90 if (!params) 91 return; 92 93 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 112 /* The connection attempt was doing scan for new RPA, and is 113 * in scan phase. If params are not associated with any other 114 * autoconnect action, remove them completely. If they are, just unmark 115 * them as waiting for connection, by clearing explicit_connect field. 116 */ 117 params->explicit_connect = false; 118 119 hci_pend_le_list_del_init(params); 120 121 switch (params->auto_connect) { 122 case HCI_AUTO_CONN_EXPLICIT: 123 hci_conn_params_del(hdev, bdaddr, bdaddr_type); 124 /* return instead of break to avoid duplicate scan update */ 125 return; 126 case HCI_AUTO_CONN_DIRECT: 127 case HCI_AUTO_CONN_ALWAYS: 128 hci_pend_le_list_add(params, &hdev->pend_le_conns); 129 break; 130 case HCI_AUTO_CONN_REPORT: 131 hci_pend_le_list_add(params, &hdev->pend_le_reports); 132 break; 133 default: 134 break; 135 } 136 137 hci_update_passive_scan(hdev); 138 } 139 140 static 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 147 if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags)) 148 hci_remove_link_key(hdev, &conn->dst); 149 150 hci_chan_list_flush(conn); 151 152 hci_conn_hash_del(hdev, conn); 153 154 if (HCI_CONN_HANDLE_UNSET(conn->handle)) 155 ida_free(&hdev->unset_handle_ida, conn->handle); 156 157 if (conn->cleanup) 158 conn->cleanup(conn); 159 160 if (conn->type == SCO_LINK || conn->type == ESCO_LINK) { 161 switch (conn->setting & SCO_AIRMODE_MASK) { 162 case SCO_AIRMODE_CVSD: 163 case SCO_AIRMODE_TRANSP: 164 if (hdev->notify) 165 hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO); 166 break; 167 } 168 } else { 169 if (hdev->notify) 170 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); 171 } 172 173 debugfs_remove_recursive(conn->debugfs); 174 175 hci_conn_del_sysfs(conn); 176 177 hci_dev_put(hdev); 178 } 179 180 int hci_disconnect(struct hci_conn *conn, __u8 reason) 181 { 182 BT_DBG("hcon %p", conn); 183 184 /* When we are central of an established connection and it enters 185 * the disconnect timeout, then go ahead and try to read the 186 * current clock offset. Processing of the result is done 187 * within the event handling and hci_clock_offset_evt function. 188 */ 189 if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER && 190 (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) { 191 struct hci_dev *hdev = conn->hdev; 192 struct hci_cp_read_clock_offset clkoff_cp; 193 194 clkoff_cp.handle = cpu_to_le16(conn->handle); 195 hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp), 196 &clkoff_cp); 197 } 198 199 return hci_abort_conn(conn, reason); 200 } 201 202 static void hci_add_sco(struct hci_conn *conn, __u16 handle) 203 { 204 struct hci_dev *hdev = conn->hdev; 205 struct hci_cp_add_sco cp; 206 207 BT_DBG("hcon %p", conn); 208 209 conn->state = BT_CONNECT; 210 conn->out = true; 211 212 conn->attempt++; 213 214 cp.handle = cpu_to_le16(handle); 215 cp.pkt_type = cpu_to_le16(conn->pkt_type); 216 217 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); 218 } 219 220 static bool find_next_esco_param(struct hci_conn *conn, 221 const struct sco_param *esco_param, int size) 222 { 223 if (!conn->parent) 224 return false; 225 226 for (; conn->attempt <= size; conn->attempt++) { 227 if (lmp_esco_2m_capable(conn->parent) || 228 (esco_param[conn->attempt - 1].pkt_type & ESCO_2EV3)) 229 break; 230 BT_DBG("hcon %p skipped attempt %d, eSCO 2M not supported", 231 conn, conn->attempt); 232 } 233 234 return conn->attempt <= size; 235 } 236 237 static int configure_datapath_sync(struct hci_dev *hdev, struct bt_codec *codec) 238 { 239 int err; 240 __u8 vnd_len, *vnd_data = NULL; 241 struct hci_op_configure_data_path *cmd = NULL; 242 243 /* Do not take below 2 checks as error since the 1st means user do not 244 * want to use HFP offload mode and the 2nd means the vendor controller 245 * do not need to send below HCI command for offload mode. 246 */ 247 if (!codec->data_path || !hdev->get_codec_config_data) 248 return 0; 249 250 err = hdev->get_codec_config_data(hdev, ESCO_LINK, codec, &vnd_len, 251 &vnd_data); 252 if (err < 0) 253 goto error; 254 255 cmd = kzalloc(sizeof(*cmd) + vnd_len, GFP_KERNEL); 256 if (!cmd) { 257 err = -ENOMEM; 258 goto error; 259 } 260 261 err = hdev->get_data_path_id(hdev, &cmd->data_path_id); 262 if (err < 0) 263 goto error; 264 265 cmd->vnd_len = vnd_len; 266 memcpy(cmd->vnd_data, vnd_data, vnd_len); 267 268 cmd->direction = 0x00; 269 __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH, 270 sizeof(*cmd) + vnd_len, cmd, HCI_CMD_TIMEOUT); 271 272 cmd->direction = 0x01; 273 err = __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH, 274 sizeof(*cmd) + vnd_len, cmd, 275 HCI_CMD_TIMEOUT); 276 error: 277 278 kfree(cmd); 279 kfree(vnd_data); 280 return err; 281 } 282 283 static 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; 288 struct hci_cp_enhanced_setup_sync_conn cp; 289 const struct sco_param *param; 290 291 kfree(conn_handle); 292 293 bt_dev_dbg(hdev, "hcon %p", conn); 294 295 configure_datapath_sync(hdev, &conn->codec); 296 297 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) { 310 case BT_CODEC_MSBC: 311 if (!find_next_esco_param(conn, esco_param_msbc, 312 ARRAY_SIZE(esco_param_msbc))) 313 return -EINVAL; 314 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 336 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: 362 if (conn->parent && lmp_esco_capable(conn->parent)) { 363 if (!find_next_esco_param(conn, esco_param_cvsd, 364 ARRAY_SIZE(esco_param_cvsd))) 365 return -EINVAL; 366 param = &esco_param_cvsd[conn->attempt - 1]; 367 } else { 368 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) 369 return -EINVAL; 370 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: 392 return -EINVAL; 393 } 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) 400 return -EIO; 401 402 return 0; 403 } 404 405 static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle) 406 { 407 struct hci_dev *hdev = conn->hdev; 408 struct hci_cp_setup_sync_conn cp; 409 const struct sco_param *param; 410 411 bt_dev_dbg(hdev, "hcon %p", conn); 412 413 conn->state = BT_CONNECT; 414 conn->out = true; 415 416 conn->attempt++; 417 418 cp.handle = cpu_to_le16(handle); 419 420 cp.tx_bandwidth = cpu_to_le32(0x00001f40); 421 cp.rx_bandwidth = cpu_to_le32(0x00001f40); 422 cp.voice_setting = cpu_to_le16(conn->setting); 423 424 switch (conn->setting & SCO_AIRMODE_MASK) { 425 case SCO_AIRMODE_TRANSP: 426 if (!find_next_esco_param(conn, esco_param_msbc, 427 ARRAY_SIZE(esco_param_msbc))) 428 return false; 429 param = &esco_param_msbc[conn->attempt - 1]; 430 break; 431 case SCO_AIRMODE_CVSD: 432 if (conn->parent && lmp_esco_capable(conn->parent)) { 433 if (!find_next_esco_param(conn, esco_param_cvsd, 434 ARRAY_SIZE(esco_param_cvsd))) 435 return false; 436 param = &esco_param_cvsd[conn->attempt - 1]; 437 } else { 438 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) 439 return false; 440 param = &sco_param_cvsd[conn->attempt - 1]; 441 } 442 break; 443 default: 444 return false; 445 } 446 447 cp.retrans_effort = param->retrans_effort; 448 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; 455 } 456 457 bool hci_setup_sync(struct hci_conn *conn, __u16 handle) 458 { 459 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 } 477 478 return hci_setup_sync_conn(conn, handle); 479 } 480 481 u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency, 482 u16 to_multiplier) 483 { 484 struct hci_dev *hdev = conn->hdev; 485 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); 499 500 memset(&cp, 0, sizeof(cp)); 501 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); 506 cp.min_ce_len = cpu_to_le16(0x0000); 507 cp.max_ce_len = cpu_to_le16(0x0000); 508 509 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); 510 511 if (params) 512 return 0x01; 513 514 return 0x00; 515 } 516 517 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand, 518 __u8 ltk[16], __u8 key_size) 519 { 520 struct hci_dev *hdev = conn->hdev; 521 struct hci_cp_le_start_enc cp; 522 523 BT_DBG("hcon %p", conn); 524 525 memset(&cp, 0, sizeof(cp)); 526 527 cp.handle = cpu_to_le16(conn->handle); 528 cp.rand = rand; 529 cp.ediv = ediv; 530 memcpy(cp.ltk, ltk, key_size); 531 532 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); 533 } 534 535 /* Device _must_ be locked */ 536 void hci_sco_setup(struct hci_conn *conn, __u8 status) 537 { 538 struct hci_link *link; 539 540 link = list_first_entry_or_null(&conn->link_list, struct hci_link, list); 541 if (!link || !link->conn) 542 return; 543 544 BT_DBG("hcon %p", conn); 545 546 if (!status) { 547 if (lmp_esco_capable(conn->hdev)) 548 hci_setup_sync(link->conn, conn->handle); 549 else 550 hci_add_sco(link->conn, conn->handle); 551 } else { 552 hci_connect_cfm(link->conn, status); 553 hci_conn_del(link->conn); 554 } 555 } 556 557 static void hci_conn_timeout(struct work_struct *work) 558 { 559 struct hci_conn *conn = container_of(work, struct hci_conn, 560 disc_work.work); 561 int refcnt = atomic_read(&conn->refcnt); 562 563 BT_DBG("hcon %p state %s", conn, state_to_string(conn->state)); 564 565 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) 575 return; 576 577 hci_abort_conn(conn, hci_proto_disconn_ind(conn)); 578 } 579 580 /* Enter sniff mode */ 581 static void hci_conn_idle(struct work_struct *work) 582 { 583 struct hci_conn *conn = container_of(work, struct hci_conn, 584 idle_work.work); 585 struct hci_dev *hdev = conn->hdev; 586 587 BT_DBG("hcon %p mode %d", conn, conn->mode); 588 589 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); 598 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); 601 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); 602 } 603 604 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { 605 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); 609 cp.attempt = cpu_to_le16(4); 610 cp.timeout = cpu_to_le16(1); 611 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); 612 } 613 } 614 615 static void hci_conn_auto_accept(struct work_struct *work) 616 { 617 struct hci_conn *conn = container_of(work, struct hci_conn, 618 auto_accept_work.work); 619 620 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), 621 &conn->dst); 622 } 623 624 static 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 641 static 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); 645 struct hci_dev *hdev = conn->hdev; 646 647 BT_DBG(""); 648 649 /* 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 */ 654 if (conn->role == HCI_ROLE_SLAVE) { 655 /* Disable LE Advertising */ 656 le_disable_advertising(hdev); 657 hci_dev_lock(hdev); 658 hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT); 659 hci_dev_unlock(hdev); 660 return; 661 } 662 663 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); 664 } 665 666 struct 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; 677 bool big_term; 678 bool pa_sync_term; 679 bool big_sync_term; 680 }; 681 682 static 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 690 if (d->big != conn->iso_qos.bcast.big || d->bis == BT_ISO_QOS_BIS_UNSET || 691 d->bis != conn->iso_qos.bcast.bis) 692 return; 693 694 d->count++; 695 } 696 697 static 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 703 hci_disable_per_advertising_sync(hdev, d->bis); 704 hci_remove_ext_adv_instance_sync(hdev, d->bis, NULL); 705 706 /* Only terminate BIG if it has been created */ 707 if (!d->big_term) 708 return 0; 709 710 return hci_le_terminate_big_sync(hdev, d->big, 711 HCI_ERROR_LOCAL_HOST_TERM); 712 } 713 714 static void terminate_big_destroy(struct hci_dev *hdev, void *data, int err) 715 { 716 kfree(data); 717 } 718 719 static int hci_le_terminate_big(struct hci_dev *hdev, struct hci_conn *conn) 720 { 721 struct iso_list_data *d; 722 int ret; 723 724 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", conn->iso_qos.bcast.big, 725 conn->iso_qos.bcast.bis); 726 727 d = kzalloc(sizeof(*d), GFP_KERNEL); 728 if (!d) 729 return -ENOMEM; 730 731 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); 734 735 ret = hci_cmd_sync_queue(hdev, terminate_big_sync, d, 736 terminate_big_destroy); 737 if (ret) 738 kfree(d); 739 740 return ret; 741 } 742 743 static 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 750 if (d->big_sync_term) 751 hci_le_big_terminate_sync(hdev, d->big); 752 753 if (d->pa_sync_term) 754 return hci_le_pa_terminate_sync(hdev, d->sync_handle); 755 756 return 0; 757 } 758 759 static 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 770 static int hci_le_big_terminate(struct hci_dev *hdev, u8 big, struct hci_conn *conn) 771 { 772 struct iso_list_data *d; 773 int ret; 774 775 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", big, conn->sync_handle); 776 777 d = kzalloc(sizeof(*d), GFP_KERNEL); 778 if (!d) 779 return -ENOMEM; 780 781 memset(d, 0, sizeof(*d)); 782 d->big = big; 783 d->sync_handle = conn->sync_handle; 784 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 } 802 803 ret = hci_cmd_sync_queue(hdev, big_terminate_sync, d, 804 terminate_big_destroy); 805 if (ret) 806 kfree(d); 807 808 return ret; 809 } 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 */ 817 static void bis_cleanup(struct hci_conn *conn) 818 { 819 struct hci_dev *hdev = conn->hdev; 820 struct hci_conn *bis; 821 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 828 /* Check if ISO connection is a BIS and terminate advertising 829 * set and BIG if there are no other connections using it. 830 */ 831 bis = hci_conn_hash_lookup_big(hdev, conn->iso_qos.bcast.big); 832 if (bis) 833 return; 834 835 hci_le_terminate_big(hdev, conn); 836 } else { 837 hci_le_big_terminate(hdev, conn->iso_qos.bcast.big, 838 conn); 839 } 840 } 841 842 static int remove_cig_sync(struct hci_dev *hdev, void *data) 843 { 844 u8 handle = PTR_UINT(data); 845 846 return hci_le_remove_cig_sync(hdev, handle); 847 } 848 849 static int hci_le_remove_cig(struct hci_dev *hdev, u8 handle) 850 { 851 bt_dev_dbg(hdev, "handle 0x%2.2x", handle); 852 853 return hci_cmd_sync_queue(hdev, remove_cig_sync, UINT_PTR(handle), 854 NULL); 855 } 856 857 static void find_cis(struct hci_conn *conn, void *data) 858 { 859 struct iso_list_data *d = data; 860 861 /* Ignore broadcast or if CIG don't match */ 862 if (!bacmp(&conn->dst, BDADDR_ANY) || d->cig != conn->iso_qos.ucast.cig) 863 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 */ 872 static void cis_cleanup(struct hci_conn *conn) 873 { 874 struct hci_dev *hdev = conn->hdev; 875 struct iso_list_data d; 876 877 if (conn->iso_qos.ucast.cig == BT_ISO_QOS_CIG_UNSET) 878 return; 879 880 memset(&d, 0, sizeof(d)); 881 d.cig = conn->iso_qos.ucast.cig; 882 883 /* Check if ISO connection is a CIS and remove CIG if there are 884 * no other connections using it. 885 */ 886 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); 888 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_CONNECTED, &d); 889 if (d.count) 890 return; 891 892 hci_le_remove_cig(hdev, conn->iso_qos.ucast.cig); 893 } 894 895 static int hci_conn_hash_alloc_unset(struct hci_dev *hdev) 896 { 897 return ida_alloc_range(&hdev->unset_handle_ida, HCI_CONN_HANDLE_MAX + 1, 898 U16_MAX, GFP_ATOMIC); 899 } 900 901 static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst, 902 u8 role, u16 handle) 903 { 904 struct hci_conn *conn; 905 906 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 932 bt_dev_dbg(hdev, "dst %pMR handle 0x%4.4x", dst, handle); 933 934 conn = kzalloc(sizeof(*conn), GFP_KERNEL); 935 if (!conn) 936 return ERR_PTR(-ENOMEM); 937 938 bacpy(&conn->dst, dst); 939 bacpy(&conn->src, &hdev->bdaddr); 940 conn->handle = handle; 941 conn->hdev = hdev; 942 conn->type = type; 943 conn->role = role; 944 conn->mode = HCI_CM_ACTIVE; 945 conn->state = BT_OPEN; 946 conn->auth_type = HCI_AT_GENERAL_BONDING; 947 conn->io_capability = hdev->io_capability; 948 conn->remote_auth = 0xff; 949 conn->key_type = 0xff; 950 conn->rssi = HCI_RSSI_INVALID; 951 conn->tx_power = HCI_TX_POWER_INVALID; 952 conn->max_tx_power = HCI_TX_POWER_INVALID; 953 conn->sync_handle = HCI_SYNC_HANDLE_INVALID; 954 955 set_bit(HCI_CONN_POWER_SAVE, &conn->flags); 956 conn->disc_timeout = HCI_DISCONN_TIMEOUT; 957 958 /* Set Default Authenticated payload timeout to 30s */ 959 conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT; 960 961 if (conn->role == HCI_ROLE_MASTER) 962 conn->out = true; 963 964 switch (type) { 965 case ACL_LINK: 966 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; 967 conn->mtu = hdev->acl_mtu; 968 break; 969 case LE_LINK: 970 /* conn->src should reflect the local identity address */ 971 hci_copy_identity_address(hdev, &conn->src, &conn->src_type); 972 conn->mtu = hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu; 973 break; 974 case ISO_LINK: 975 /* conn->src should reflect the local identity address */ 976 hci_copy_identity_address(hdev, &conn->src, &conn->src_type); 977 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 984 conn->mtu = hdev->iso_mtu ? hdev->iso_mtu : 985 hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu; 986 break; 987 case SCO_LINK: 988 if (lmp_esco_capable(hdev)) 989 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | 990 (hdev->esco_type & EDR_ESCO_MASK); 991 else 992 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; 993 994 conn->mtu = hdev->sco_mtu; 995 break; 996 case ESCO_LINK: 997 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; 998 conn->mtu = hdev->sco_mtu; 999 break; 1000 } 1001 1002 skb_queue_head_init(&conn->data_q); 1003 1004 INIT_LIST_HEAD(&conn->chan_list); 1005 INIT_LIST_HEAD(&conn->link_list); 1006 1007 INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout); 1008 INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept); 1009 INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle); 1010 INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout); 1011 1012 atomic_set(&conn->refcnt, 0); 1013 1014 hci_dev_hold(hdev); 1015 1016 hci_conn_hash_add(hdev, conn); 1017 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 } 1026 1027 hci_conn_init_sysfs(conn); 1028 1029 return conn; 1030 } 1031 1032 struct 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)) 1041 return ERR_PTR(-ECONNREFUSED); 1042 1043 return __hci_conn_add(hdev, type, dst, role, handle); 1044 } 1045 1046 struct 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); 1053 } 1054 1055 static 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: 1071 if ((conn->state != BT_CONNECTED && 1072 !test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) || 1073 test_bit(HCI_CONN_BIG_CREATED, &conn->flags)) 1074 hci_conn_failed(conn, reason); 1075 break; 1076 } 1077 } 1078 1079 static void hci_conn_unlink(struct hci_conn *conn) 1080 { 1081 struct hci_dev *hdev = conn->hdev; 1082 1083 bt_dev_dbg(hdev, "hcon %p", conn); 1084 1085 if (!conn->parent) { 1086 struct hci_link *link, *t; 1087 1088 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 1093 /* 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 1101 hci_conn_cleanup_child(child, conn->abort_reason); 1102 } 1103 1104 return; 1105 } 1106 1107 if (!conn->link) 1108 return; 1109 1110 list_del_rcu(&conn->link->list); 1111 synchronize_rcu(); 1112 1113 hci_conn_drop(conn->parent); 1114 hci_conn_put(conn->parent); 1115 conn->parent = NULL; 1116 1117 kfree(conn->link); 1118 conn->link = NULL; 1119 } 1120 1121 void hci_conn_del(struct hci_conn *conn) 1122 { 1123 struct hci_dev *hdev = conn->hdev; 1124 1125 BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle); 1126 1127 hci_conn_unlink(conn); 1128 1129 cancel_delayed_work_sync(&conn->disc_work); 1130 cancel_delayed_work_sync(&conn->auto_accept_work); 1131 cancel_delayed_work_sync(&conn->idle_work); 1132 1133 if (conn->type == ACL_LINK) { 1134 /* Unacked frames */ 1135 hdev->acl_cnt += conn->sent; 1136 } else if (conn->type == LE_LINK) { 1137 cancel_delayed_work(&conn->le_conn_timeout); 1138 1139 if (hdev->le_pkts) 1140 hdev->le_cnt += conn->sent; 1141 else 1142 hdev->acl_cnt += conn->sent; 1143 } else { 1144 /* 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 } 1153 } 1154 1155 skb_queue_purge(&conn->data_q); 1156 1157 /* 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); 1163 1164 /* Dequeue callbacks using connection pointer as data */ 1165 hci_cmd_sync_dequeue(hdev, NULL, conn, NULL); 1166 } 1167 1168 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type) 1169 { 1170 int use_src = bacmp(src, BDADDR_ANY); 1171 struct hci_dev *hdev = NULL, *d; 1172 1173 BT_DBG("%pMR -> %pMR", src, dst); 1174 1175 read_lock(&hci_dev_list_lock); 1176 1177 list_for_each_entry(d, &hci_dev_list, list) { 1178 if (!test_bit(HCI_UP, &d->flags) || 1179 hci_dev_test_flag(d, HCI_USER_CHANNEL)) 1180 continue; 1181 1182 /* Simple routing: 1183 * No source address - find interface with bdaddr != dst 1184 * Source address - find interface with bdaddr == src 1185 */ 1186 1187 if (use_src) { 1188 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) { 1211 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 1223 read_unlock(&hci_dev_list_lock); 1224 return hdev; 1225 } 1226 EXPORT_SYMBOL(hci_get_route); 1227 1228 /* This function requires the caller holds hdev->lock */ 1229 static void hci_le_conn_failed(struct hci_conn *conn, u8 status) 1230 { 1231 struct hci_dev *hdev = conn->hdev; 1232 1233 hci_connect_le_scan_cleanup(conn, status); 1234 1235 /* Enable advertising in case this was a failed connection 1236 * attempt as a peripheral. 1237 */ 1238 hci_enable_advertising(hdev); 1239 } 1240 1241 /* This function requires the caller holds hdev->lock */ 1242 void 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 1258 /* 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 1264 conn->state = BT_CLOSED; 1265 hci_connect_cfm(conn, status); 1266 hci_conn_del(conn); 1267 } 1268 1269 /* This function requires the caller holds hdev->lock */ 1270 u8 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 1291 if (HCI_CONN_HANDLE_UNSET(conn->handle)) 1292 ida_free(&hdev->unset_handle_ida, conn->handle); 1293 1294 conn->handle = handle; 1295 1296 return 0; 1297 } 1298 1299 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, 1300 u8 dst_type, bool dst_resolved, u8 sec_level, 1301 u16 conn_timeout, u8 role, u8 phy, u8 sec_phy) 1302 { 1303 struct hci_conn *conn; 1304 struct smp_irk *irk; 1305 int err; 1306 1307 /* Let's make sure that le is enabled.*/ 1308 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 1309 if (lmp_le_capable(hdev)) 1310 return ERR_PTR(-ECONNREFUSED); 1311 1312 return ERR_PTR(-EOPNOTSUPP); 1313 } 1314 1315 /* 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 1321 /* 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. 1325 */ 1326 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type); 1327 if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) { 1328 return ERR_PTR(-EBUSY); 1329 } 1330 1331 /* Check if the destination address has been resolved by the controller 1332 * since if it did then the identity address shall be used. 1333 */ 1334 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 } 1349 } 1350 1351 if (conn) { 1352 bacpy(&conn->dst, dst); 1353 } else { 1354 conn = hci_conn_add_unset(hdev, LE_LINK, dst, role); 1355 if (IS_ERR(conn)) 1356 return conn; 1357 hci_conn_hold(conn); 1358 conn->pending_sec_level = sec_level; 1359 } 1360 1361 conn->dst_type = dst_type; 1362 conn->sec_level = BT_SECURITY_LOW; 1363 conn->conn_timeout = conn_timeout; 1364 conn->le_adv_phy = phy; 1365 conn->le_adv_sec_phy = sec_phy; 1366 1367 err = hci_connect_le_sync(hdev, conn); 1368 if (err) { 1369 hci_conn_del(conn); 1370 return ERR_PTR(err); 1371 } 1372 1373 return conn; 1374 } 1375 1376 static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) 1377 { 1378 struct hci_conn *conn; 1379 1380 conn = hci_conn_hash_lookup_le(hdev, addr, type); 1381 if (!conn) 1382 return false; 1383 1384 if (conn->state != BT_CONNECTED) 1385 return false; 1386 1387 return true; 1388 } 1389 1390 /* This function requires the caller holds hdev->lock */ 1391 static int hci_explicit_conn_params_set(struct hci_dev *hdev, 1392 bdaddr_t *addr, u8 addr_type) 1393 { 1394 struct hci_conn_params *params; 1395 1396 if (is_connected(hdev, addr, addr_type)) 1397 return -EISCONN; 1398 1399 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; 1404 1405 /* 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 */ 1413 if (params->auto_connect == HCI_AUTO_CONN_DISABLED || 1414 params->auto_connect == HCI_AUTO_CONN_REPORT || 1415 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { 1416 hci_pend_le_list_del_init(params); 1417 hci_pend_le_list_add(params, &hdev->pend_le_conns); 1418 } 1419 1420 params->explicit_connect = true; 1421 1422 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, 1423 params->auto_connect); 1424 1425 return 0; 1426 } 1427 1428 static int qos_set_big(struct hci_dev *hdev, struct bt_iso_qos *qos) 1429 { 1430 struct hci_conn *conn; 1431 u8 big; 1432 1433 /* Allocate a BIG if not set */ 1434 if (qos->bcast.big == BT_ISO_QOS_BIG_UNSET) { 1435 for (big = 0x00; big < 0xef; big++) { 1436 1437 conn = hci_conn_hash_lookup_big(hdev, big); 1438 if (!conn) 1439 break; 1440 } 1441 1442 if (big == 0xef) 1443 return -EADDRNOTAVAIL; 1444 1445 /* Update BIG */ 1446 qos->bcast.big = big; 1447 } 1448 1449 return 0; 1450 } 1451 1452 static int qos_set_bis(struct hci_dev *hdev, struct bt_iso_qos *qos) 1453 { 1454 struct hci_conn *conn; 1455 u8 bis; 1456 1457 /* Allocate BIS if not set */ 1458 if (qos->bcast.bis == BT_ISO_QOS_BIS_UNSET) { 1459 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 1471 /* Find an unused adv set to advertise BIS, skip instance 0x00 1472 * since it is reserved as general purpose set. 1473 */ 1474 for (bis = 0x01; bis < hdev->le_num_of_adv_sets; 1475 bis++) { 1476 1477 conn = hci_conn_hash_lookup_bis(hdev, BDADDR_ANY, bis); 1478 if (!conn) 1479 break; 1480 } 1481 1482 if (bis == hdev->le_num_of_adv_sets) 1483 return -EADDRNOTAVAIL; 1484 1485 /* Update BIS */ 1486 qos->bcast.bis = bis; 1487 } 1488 1489 return 0; 1490 } 1491 1492 /* This function requires the caller holds hdev->lock */ 1493 static struct hci_conn *hci_add_bis(struct hci_dev *hdev, bdaddr_t *dst, 1494 struct bt_iso_qos *qos, __u8 base_len, 1495 __u8 *base) 1496 { 1497 struct hci_conn *conn; 1498 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 1515 /* 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) 1519 return ERR_PTR(-EADDRINUSE); 1520 1521 /* Check BIS settings against other bound BISes, since all 1522 * BISes in a BIG must have the same value for all parameters 1523 */ 1524 conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big); 1525 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))) 1529 return ERR_PTR(-EADDRINUSE); 1530 1531 conn = hci_conn_add_unset(hdev, ISO_LINK, dst, HCI_ROLE_MASTER); 1532 if (IS_ERR(conn)) 1533 return conn; 1534 1535 conn->state = BT_CONNECT; 1536 1537 hci_conn_hold(conn); 1538 return conn; 1539 } 1540 1541 /* This function requires the caller holds hdev->lock */ 1542 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst, 1543 u8 dst_type, u8 sec_level, 1544 u16 conn_timeout, 1545 enum conn_reasons conn_reason) 1546 { 1547 struct hci_conn *conn; 1548 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 */ 1566 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type); 1567 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 1575 conn = hci_conn_add_unset(hdev, LE_LINK, dst, HCI_ROLE_MASTER); 1576 if (IS_ERR(conn)) 1577 return conn; 1578 1579 if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) { 1580 hci_conn_del(conn); 1581 return ERR_PTR(-EBUSY); 1582 } 1583 1584 conn->state = BT_CONNECT; 1585 set_bit(HCI_CONN_SCANNING, &conn->flags); 1586 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; 1590 conn->conn_reason = conn_reason; 1591 1592 hci_update_passive_scan(hdev); 1593 1594 done: 1595 hci_conn_hold(conn); 1596 return conn; 1597 } 1598 1599 struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst, 1600 u8 sec_level, u8 auth_type, 1601 enum conn_reasons conn_reason, u16 timeout) 1602 { 1603 struct hci_conn *acl; 1604 1605 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 1606 if (lmp_bredr_capable(hdev)) 1607 return ERR_PTR(-ECONNREFUSED); 1608 1609 return ERR_PTR(-EOPNOTSUPP); 1610 } 1611 1612 /* 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 1621 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); 1622 if (!acl) { 1623 acl = hci_conn_add_unset(hdev, ACL_LINK, dst, HCI_ROLE_MASTER); 1624 if (IS_ERR(acl)) 1625 return acl; 1626 } 1627 1628 hci_conn_hold(acl); 1629 1630 acl->conn_reason = conn_reason; 1631 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { 1632 int err; 1633 1634 acl->sec_level = BT_SECURITY_LOW; 1635 acl->pending_sec_level = sec_level; 1636 acl->auth_type = auth_type; 1637 acl->conn_timeout = timeout; 1638 1639 err = hci_connect_acl_sync(hdev, acl); 1640 if (err) { 1641 hci_conn_del(acl); 1642 return ERR_PTR(err); 1643 } 1644 } 1645 1646 return acl; 1647 } 1648 1649 static 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 1677 struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst, 1678 __u16 setting, struct bt_codec *codec, 1679 u16 timeout) 1680 { 1681 struct hci_conn *acl; 1682 struct hci_conn *sco; 1683 struct hci_link *link; 1684 1685 acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING, 1686 CONN_REASON_SCO_CONNECT, timeout); 1687 if (IS_ERR(acl)) 1688 return acl; 1689 1690 sco = hci_conn_hash_lookup_ba(hdev, type, dst); 1691 if (!sco) { 1692 sco = hci_conn_add_unset(hdev, type, dst, HCI_ROLE_MASTER); 1693 if (IS_ERR(sco)) { 1694 hci_conn_drop(acl); 1695 return sco; 1696 } 1697 } 1698 1699 link = hci_conn_link(acl, sco); 1700 if (!link) { 1701 hci_conn_drop(acl); 1702 hci_conn_drop(sco); 1703 return ERR_PTR(-ENOLINK); 1704 } 1705 1706 sco->setting = setting; 1707 sco->codec = *codec; 1708 1709 if (acl->state == BT_CONNECTED && 1710 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { 1711 set_bit(HCI_CONN_POWER_SAVE, &acl->flags); 1712 hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON); 1713 1714 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) { 1715 /* defer SCO setup until mode change completed */ 1716 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags); 1717 return sco; 1718 } 1719 1720 hci_sco_setup(acl, 0x00); 1721 } 1722 1723 return sco; 1724 } 1725 1726 static 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; 1730 struct iso_list_data data; 1731 1732 memset(&cp, 0, sizeof(cp)); 1733 1734 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 1742 cp.handle = qos->bcast.big; 1743 cp.adv_handle = qos->bcast.bis; 1744 cp.num_bis = data.count; 1745 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)); 1754 1755 return hci_send_cmd(hdev, HCI_OP_LE_CREATE_BIG, sizeof(cp), &cp); 1756 } 1757 1758 static int set_cig_params_sync(struct hci_dev *hdev, void *data) 1759 { 1760 DEFINE_FLEX(struct hci_cp_le_set_cig_params, pdu, cis, num_cis, 0x1f); 1761 u8 cig_id = PTR_UINT(data); 1762 struct hci_conn *conn; 1763 struct bt_iso_qos *qos; 1764 u8 aux_num_cis = 0; 1765 u8 cis_id; 1766 1767 conn = hci_conn_hash_lookup_cig(hdev, cig_id); 1768 if (!conn) 1769 return 0; 1770 1771 qos = &conn->iso_qos; 1772 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); 1780 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 && 1786 aux_num_cis < pdu->num_cis; cis_id++) { 1787 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 1795 cis = &pdu->cis[aux_num_cis++]; 1796 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 } 1806 pdu->num_cis = aux_num_cis; 1807 1808 if (!pdu->num_cis) 1809 return 0; 1810 1811 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_CIG_PARAMS, 1812 struct_size(pdu, cis, pdu->num_cis), 1813 pdu, HCI_CMD_TIMEOUT); 1814 } 1815 1816 static 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 1823 /* Allocate first still reconfigurable CIG if not set */ 1824 if (qos->ucast.cig == BT_ISO_QOS_CIG_UNSET) { 1825 for (data.cig = 0x00; data.cig < 0xf0; data.cig++) { 1826 data.count = 0; 1827 1828 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, 1829 BT_CONNECT, &data); 1830 if (data.count) 1831 continue; 1832 1833 hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, 1834 BT_CONNECTED, &data); 1835 if (!data.count) 1836 break; 1837 } 1838 1839 if (data.cig == 0xf0) 1840 return false; 1841 1842 /* Update CIG */ 1843 qos->ucast.cig = data.cig; 1844 } 1845 1846 if (qos->ucast.cis != BT_ISO_QOS_CIS_UNSET) { 1847 if (hci_conn_hash_lookup_cis(hdev, NULL, 0, qos->ucast.cig, 1848 qos->ucast.cis)) 1849 return false; 1850 goto done; 1851 } 1852 1853 /* 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)) { 1858 /* Update CIS */ 1859 qos->ucast.cis = data.cis; 1860 break; 1861 } 1862 } 1863 1864 if (qos->ucast.cis == BT_ISO_QOS_CIS_UNSET) 1865 return false; 1866 1867 done: 1868 if (hci_cmd_sync_queue(hdev, set_cig_params_sync, 1869 UINT_PTR(qos->ucast.cig), NULL) < 0) 1870 return false; 1871 1872 return true; 1873 } 1874 1875 struct 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 1880 cis = hci_conn_hash_lookup_cis(hdev, dst, dst_type, qos->ucast.cig, 1881 qos->ucast.cis); 1882 if (!cis) { 1883 cis = hci_conn_add_unset(hdev, ISO_LINK, dst, HCI_ROLE_MASTER); 1884 if (IS_ERR(cis)) 1885 return cis; 1886 cis->cleanup = cis_cleanup; 1887 cis->dst_type = dst_type; 1888 cis->iso_qos.ucast.cig = BT_ISO_QOS_CIG_UNSET; 1889 cis->iso_qos.ucast.cis = BT_ISO_QOS_CIS_UNSET; 1890 } 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 */ 1901 cis->le_tx_phy = qos->ucast.out.phy; 1902 cis->le_rx_phy = qos->ucast.in.phy; 1903 1904 /* If output interval is not set use the input interval as it cannot be 1905 * 0x000000. 1906 */ 1907 if (!qos->ucast.out.interval) 1908 qos->ucast.out.interval = qos->ucast.in.interval; 1909 1910 /* If input interval is not set use the output interval as it cannot be 1911 * 0x000000. 1912 */ 1913 if (!qos->ucast.in.interval) 1914 qos->ucast.in.interval = qos->ucast.out.interval; 1915 1916 /* If output latency is not set use the input latency as it cannot be 1917 * 0x0000. 1918 */ 1919 if (!qos->ucast.out.latency) 1920 qos->ucast.out.latency = qos->ucast.in.latency; 1921 1922 /* If input latency is not set use the output latency as it cannot be 1923 * 0x0000. 1924 */ 1925 if (!qos->ucast.in.latency) 1926 qos->ucast.in.latency = qos->ucast.out.latency; 1927 1928 if (!hci_le_set_cig_params(cis, qos)) { 1929 hci_conn_drop(cis); 1930 return ERR_PTR(-EINVAL); 1931 } 1932 1933 hci_conn_hold(cis); 1934 1935 cis->iso_qos = *qos; 1936 cis->state = BT_BOUND; 1937 1938 return cis; 1939 } 1940 1941 bool 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 1948 if (conn->iso_qos.ucast.out.sdu) { 1949 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 1959 if (conn->iso_qos.ucast.in.sdu) { 1960 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 1973 int hci_conn_check_create_cis(struct hci_conn *conn) 1974 { 1975 if (conn->type != ISO_LINK || !bacmp(&conn->dst, BDADDR_ANY)) 1976 return -EINVAL; 1977 1978 if (!conn->parent || conn->parent->state != BT_CONNECTED || 1979 conn->state != BT_CONNECT || HCI_CONN_HANDLE_UNSET(conn->handle)) 1980 return 1; 1981 1982 return 0; 1983 } 1984 1985 static int hci_create_cis_sync(struct hci_dev *hdev, void *data) 1986 { 1987 return hci_le_create_cis_sync(hdev); 1988 } 1989 1990 int hci_le_create_cis_pending(struct hci_dev *hdev) 1991 { 1992 struct hci_conn *conn; 1993 bool pending = false; 1994 1995 rcu_read_lock(); 1996 1997 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; 2001 } 2002 2003 if (!hci_conn_check_create_cis(conn)) 2004 pending = true; 2005 } 2006 2007 rcu_read_unlock(); 2008 2009 if (!pending) 2010 return 0; 2011 2012 /* Queue Create CIS */ 2013 return hci_cmd_sync_queue(hdev, hci_create_cis_sync, NULL, NULL); 2014 } 2015 2016 static 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 */ 2020 if (!qos->sdu && qos->phy) 2021 qos->sdu = conn->mtu; 2022 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 2037 static 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 2045 if (qos->bcast.out.phy == 0x02) 2046 flags |= MGMT_ADV_FLAG_SEC_2M; 2047 2048 /* Align intervals */ 2049 interval = (qos->bcast.out.interval / 1250) * qos->bcast.sync_factor; 2050 2051 if (qos->bcast.bis) 2052 sync_interval = interval * 4; 2053 2054 err = hci_start_per_adv_sync(hdev, qos->bcast.bis, conn->le_per_adv_data_len, 2055 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 2063 static 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 2075 static 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 2090 struct 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) 2093 { 2094 struct hci_cp_le_pa_create_sync *cp; 2095 struct hci_conn *conn; 2096 int err; 2097 2098 if (hci_dev_test_and_set_flag(hdev, HCI_PA_SYNC)) 2099 return ERR_PTR(-EBUSY); 2100 2101 conn = hci_conn_add_unset(hdev, ISO_LINK, dst, HCI_ROLE_SLAVE); 2102 if (IS_ERR(conn)) 2103 return conn; 2104 2105 conn->iso_qos = *qos; 2106 conn->state = BT_LISTEN; 2107 2108 hci_conn_hold(conn); 2109 2110 cp = kzalloc(sizeof(*cp), GFP_KERNEL); 2111 if (!cp) { 2112 hci_dev_clear_flag(hdev, HCI_PA_SYNC); 2113 hci_conn_drop(conn); 2114 return ERR_PTR(-ENOMEM); 2115 } 2116 2117 cp->options = qos->bcast.options; 2118 cp->sid = sid; 2119 cp->addr_type = dst_type; 2120 bacpy(&cp->addr, dst); 2121 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; 2124 2125 /* Queue start pa_create_sync and scan */ 2126 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; 2134 } 2135 2136 int hci_le_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon, 2137 struct bt_iso_qos *qos, 2138 __u16 sync_handle, __u8 num_bis, __u8 bis[]) 2139 { 2140 DEFINE_FLEX(struct hci_cp_le_big_create_sync, pdu, bis, num_bis, 0x11); 2141 int err; 2142 2143 if (num_bis < 0x01 || num_bis > pdu->num_bis) 2144 return -EINVAL; 2145 2146 err = qos_set_big(hdev, qos); 2147 if (err) 2148 return err; 2149 2150 if (hcon) 2151 hcon->iso_qos.bcast.big = qos->bcast.big; 2152 2153 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); 2161 2162 return hci_send_cmd(hdev, HCI_OP_LE_BIG_CREATE_SYNC, 2163 struct_size(pdu, bis, num_bis), pdu); 2164 } 2165 2166 static 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 2179 struct 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; 2184 struct hci_conn *parent; 2185 __u8 eir[HCI_MAX_PER_AD_LENGTH]; 2186 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 } 2195 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 2222 /* 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 2236 return conn; 2237 } 2238 2239 static 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 2255 struct 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; 2261 struct iso_list_data data; 2262 2263 conn = hci_bind_bis(hdev, dst, qos, base_len, base); 2264 if (IS_ERR(conn)) 2265 return conn; 2266 2267 if (conn->state == BT_CONNECTED) 2268 return conn; 2269 2270 data.big = qos->bcast.big; 2271 data.bis = qos->bcast.bis; 2272 2273 /* 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); 2279 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 2288 return conn; 2289 } 2290 2291 struct 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; 2296 struct hci_link *link; 2297 2298 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, 2302 HCI_ROLE_SLAVE, 0, 0); 2303 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 2311 hci_iso_qos_setup(hdev, le, &qos->ucast.out, 2312 le->le_tx_phy ? le->le_tx_phy : hdev->le_tx_def_phys); 2313 hci_iso_qos_setup(hdev, le, &qos->ucast.in, 2314 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 2322 link = hci_conn_link(le, cis); 2323 if (!link) { 2324 hci_conn_drop(le); 2325 hci_conn_drop(cis); 2326 return ERR_PTR(-ENOLINK); 2327 } 2328 2329 /* Link takes the refcount */ 2330 hci_conn_drop(cis); 2331 2332 cis->state = BT_CONNECT; 2333 2334 hci_le_create_cis_pending(hdev); 2335 2336 return cis; 2337 } 2338 2339 /* Check link security requirement */ 2340 int hci_conn_check_link_mode(struct hci_conn *conn) 2341 { 2342 BT_DBG("hcon %p", conn); 2343 2344 /* 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 */ 2348 if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) { 2349 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 2355 /* 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 2372 if (hci_conn_ssp_enabled(conn) && 2373 !test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 2374 return 0; 2375 2376 return 1; 2377 } 2378 2379 /* Authenticate remote device */ 2380 static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) 2381 { 2382 BT_DBG("hcon %p", conn); 2383 2384 if (conn->pending_sec_level > sec_level) 2385 sec_level = conn->pending_sec_level; 2386 2387 if (sec_level > conn->sec_level) 2388 conn->pending_sec_level = sec_level; 2389 else if (test_bit(HCI_CONN_AUTH, &conn->flags)) 2390 return 1; 2391 2392 /* Make sure we preserve an existing MITM requirement*/ 2393 auth_type |= (conn->auth_type & 0x01); 2394 2395 conn->auth_type = auth_type; 2396 2397 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { 2398 struct hci_cp_auth_requested cp; 2399 2400 cp.handle = cpu_to_le16(conn->handle); 2401 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, 2402 sizeof(cp), &cp); 2403 2404 /* Set the ENCRYPT_PEND to trigger encryption after 2405 * authentication. 2406 */ 2407 if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 2408 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags); 2409 } 2410 2411 return 0; 2412 } 2413 2414 /* Encrypt the link */ 2415 static void hci_conn_encrypt(struct hci_conn *conn) 2416 { 2417 BT_DBG("hcon %p", conn); 2418 2419 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) { 2420 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), 2424 &cp); 2425 } 2426 } 2427 2428 /* Enable security */ 2429 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type, 2430 bool initiator) 2431 { 2432 BT_DBG("hcon %p", conn); 2433 2434 if (conn->type == LE_LINK) 2435 return smp_conn_security(conn, sec_level); 2436 2437 /* For sdp we don't need the link key. */ 2438 if (sec_level == BT_SECURITY_SDP) 2439 return 1; 2440 2441 /* For non 2.1 devices and low security level we don't need the link 2442 key. */ 2443 if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn)) 2444 return 1; 2445 2446 /* For other security levels we need the link key. */ 2447 if (!test_bit(HCI_CONN_AUTH, &conn->flags)) 2448 goto auth; 2449 2450 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 } 2485 2486 auth: 2487 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) 2488 return 0; 2489 2490 if (initiator) 2491 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags); 2492 2493 if (!hci_conn_auth(conn, sec_level, auth_type)) 2494 return 0; 2495 2496 encrypt: 2497 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 */ 2505 return 1; 2506 } 2507 2508 hci_conn_encrypt(conn); 2509 return 0; 2510 } 2511 EXPORT_SYMBOL(hci_conn_security); 2512 2513 /* Check secure link requirement */ 2514 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level) 2515 { 2516 BT_DBG("hcon %p", conn); 2517 2518 /* Accept if non-secure or higher security level is required */ 2519 if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS) 2520 return 1; 2521 2522 /* 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; 2529 } 2530 EXPORT_SYMBOL(hci_conn_check_secure); 2531 2532 /* Switch role */ 2533 int hci_conn_switch_role(struct hci_conn *conn, __u8 role) 2534 { 2535 BT_DBG("hcon %p", conn); 2536 2537 if (role == conn->role) 2538 return 1; 2539 2540 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) { 2541 struct hci_cp_switch_role cp; 2542 bacpy(&cp.bdaddr, &conn->dst); 2543 cp.role = role; 2544 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); 2545 } 2546 2547 return 0; 2548 } 2549 EXPORT_SYMBOL(hci_conn_switch_role); 2550 2551 /* Enter active mode */ 2552 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active) 2553 { 2554 struct hci_dev *hdev = conn->hdev; 2555 2556 BT_DBG("hcon %p mode %d", conn, conn->mode); 2557 2558 if (conn->mode != HCI_CM_SNIFF) 2559 goto timer; 2560 2561 if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active) 2562 goto timer; 2563 2564 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { 2565 struct hci_cp_exit_sniff_mode cp; 2566 cp.handle = cpu_to_le16(conn->handle); 2567 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); 2568 } 2569 2570 timer: 2571 if (hdev->idle_timeout > 0) 2572 queue_delayed_work(hdev->workqueue, &conn->idle_work, 2573 msecs_to_jiffies(hdev->idle_timeout)); 2574 } 2575 2576 /* Drop all connection on the device */ 2577 void hci_conn_hash_flush(struct hci_dev *hdev) 2578 { 2579 struct list_head *head = &hdev->conn_hash.list; 2580 struct hci_conn *conn; 2581 2582 BT_DBG("hdev %s", hdev->name); 2583 2584 /* 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); 2593 hci_conn_del(conn); 2594 } 2595 } 2596 2597 static u32 get_link_mode(struct hci_conn *conn) 2598 { 2599 u32 link_mode = 0; 2600 2601 if (conn->role == HCI_ROLE_MASTER) 2602 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 2619 int hci_get_conn_list(void __user *arg) 2620 { 2621 struct hci_conn *c; 2622 struct hci_conn_list_req req, *cl; 2623 struct hci_conn_info *ci; 2624 struct hci_dev *hdev; 2625 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 2635 cl = kmalloc(size, GFP_KERNEL); 2636 if (!cl) 2637 return -ENOMEM; 2638 2639 hdev = hci_dev_get(req.dev_id); 2640 if (!hdev) { 2641 kfree(cl); 2642 return -ENODEV; 2643 } 2644 2645 ci = cl->conn_info; 2646 2647 hci_dev_lock(hdev); 2648 list_for_each_entry(c, &hdev->conn_hash.list, list) { 2649 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; 2654 (ci + n)->link_mode = get_link_mode(c); 2655 if (++n >= req.conn_num) 2656 break; 2657 } 2658 hci_dev_unlock(hdev); 2659 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 2672 int 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 2682 hci_dev_lock(hdev); 2683 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; 2690 ci.link_mode = get_link_mode(conn); 2691 } 2692 hci_dev_unlock(hdev); 2693 2694 if (!conn) 2695 return -ENOENT; 2696 2697 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; 2698 } 2699 2700 int 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 2708 hci_dev_lock(hdev); 2709 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); 2710 if (conn) 2711 req.type = conn->auth_type; 2712 hci_dev_unlock(hdev); 2713 2714 if (!conn) 2715 return -ENOENT; 2716 2717 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; 2718 } 2719 2720 struct hci_chan *hci_chan_create(struct hci_conn *conn) 2721 { 2722 struct hci_dev *hdev = conn->hdev; 2723 struct hci_chan *chan; 2724 2725 BT_DBG("%s hcon %p", hdev->name, conn); 2726 2727 if (test_bit(HCI_CONN_DROP, &conn->flags)) { 2728 BT_DBG("Refusing to create new hci_chan"); 2729 return NULL; 2730 } 2731 2732 chan = kzalloc(sizeof(*chan), GFP_KERNEL); 2733 if (!chan) 2734 return NULL; 2735 2736 chan->conn = hci_conn_get(conn); 2737 skb_queue_head_init(&chan->data_q); 2738 chan->state = BT_CONNECTED; 2739 2740 list_add_rcu(&chan->list, &conn->chan_list); 2741 2742 return chan; 2743 } 2744 2745 void hci_chan_del(struct hci_chan *chan) 2746 { 2747 struct hci_conn *conn = chan->conn; 2748 struct hci_dev *hdev = conn->hdev; 2749 2750 BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan); 2751 2752 list_del_rcu(&chan->list); 2753 2754 synchronize_rcu(); 2755 2756 /* Prevent new hci_chan's to be created for this hci_conn */ 2757 set_bit(HCI_CONN_DROP, &conn->flags); 2758 2759 hci_conn_put(conn); 2760 2761 skb_queue_purge(&chan->data_q); 2762 kfree(chan); 2763 } 2764 2765 void hci_chan_list_flush(struct hci_conn *conn) 2766 { 2767 struct hci_chan *chan, *n; 2768 2769 BT_DBG("hcon %p", conn); 2770 2771 list_for_each_entry_safe(chan, n, &conn->chan_list, list) 2772 hci_chan_del(chan); 2773 } 2774 2775 static 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 2788 struct 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 } 2806 2807 u32 hci_conn_get_phy(struct hci_conn *conn) 2808 { 2809 u32 phys = 0; 2810 2811 /* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471: 2812 * Table 6.2: Packets defined for synchronous, asynchronous, and 2813 * CPB logical transport types. 2814 */ 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 2907 return phys; 2908 } 2909 2910 static int abort_conn_sync(struct hci_dev *hdev, void *data) 2911 { 2912 struct hci_conn *conn = data; 2913 2914 if (!hci_conn_valid(hdev, conn)) 2915 return -ECANCELED; 2916 2917 return hci_abort_conn_sync(hdev, conn, conn->abort_reason); 2918 } 2919 2920 int hci_abort_conn(struct hci_conn *conn, u8 reason) 2921 { 2922 struct hci_dev *hdev = conn->hdev; 2923 2924 /* 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. 2937 * 2938 * hci_connect_le serializes the connection attempts so only one 2939 * connection can be in BT_CONNECT at time. 2940 */ 2941 if (conn->state == BT_CONNECT && hdev->req_status == HCI_REQ_PEND) { 2942 switch (hci_skb_event(hdev->sent_cmd)) { 2943 case HCI_EV_CONN_COMPLETE: 2944 case HCI_EV_LE_CONN_COMPLETE: 2945 case HCI_EV_LE_ENHANCED_CONN_COMPLETE: 2946 case HCI_EVT_LE_CIS_ESTABLISHED: 2947 hci_cmd_sync_cancel(hdev, ECANCELED); 2948 break; 2949 } 2950 /* Cancel connect attempt if still queued/pending */ 2951 } else if (!hci_cancel_connect_sync(hdev, conn)) { 2952 return 0; 2953 } 2954 2955 return hci_cmd_sync_queue_once(hdev, abort_conn_sync, conn, NULL); 2956 } 2957