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, status); 110 111 /* The connection attempt was doing scan for new RPA, and is 112 * in scan phase. If params are not associated with any other 113 * autoconnect action, remove them completely. If they are, just unmark 114 * them as waiting for connection, by clearing explicit_connect field. 115 */ 116 params->explicit_connect = false; 117 118 hci_pend_le_list_del_init(params); 119 120 switch (params->auto_connect) { 121 case HCI_AUTO_CONN_EXPLICIT: 122 hci_conn_params_del(hdev, bdaddr, bdaddr_type); 123 /* return instead of break to avoid duplicate scan update */ 124 return; 125 case HCI_AUTO_CONN_DIRECT: 126 case HCI_AUTO_CONN_ALWAYS: 127 hci_pend_le_list_add(params, &hdev->pend_le_conns); 128 break; 129 case HCI_AUTO_CONN_REPORT: 130 hci_pend_le_list_add(params, &hdev->pend_le_reports); 131 break; 132 default: 133 break; 134 } 135 136 hci_update_passive_scan(hdev); 137 } 138 139 static void hci_conn_cleanup(struct hci_conn *conn) 140 { 141 struct hci_dev *hdev = conn->hdev; 142 143 if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags)) 144 hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type); 145 146 if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags)) 147 hci_remove_link_key(hdev, &conn->dst); 148 149 hci_chan_list_flush(conn); 150 151 hci_conn_hash_del(hdev, conn); 152 153 if (HCI_CONN_HANDLE_UNSET(conn->handle)) 154 ida_free(&hdev->unset_handle_ida, conn->handle); 155 156 if (conn->cleanup) 157 conn->cleanup(conn); 158 159 if (conn->type == SCO_LINK || conn->type == ESCO_LINK) { 160 switch (conn->setting & SCO_AIRMODE_MASK) { 161 case SCO_AIRMODE_CVSD: 162 case SCO_AIRMODE_TRANSP: 163 if (hdev->notify) 164 hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO); 165 break; 166 } 167 } else { 168 if (hdev->notify) 169 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); 170 } 171 172 debugfs_remove_recursive(conn->debugfs); 173 174 hci_conn_del_sysfs(conn); 175 176 hci_dev_put(hdev); 177 } 178 179 int hci_disconnect(struct hci_conn *conn, __u8 reason) 180 { 181 BT_DBG("hcon %p", conn); 182 183 /* When we are central of an established connection and it enters 184 * the disconnect timeout, then go ahead and try to read the 185 * current clock offset. Processing of the result is done 186 * within the event handling and hci_clock_offset_evt function. 187 */ 188 if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER && 189 (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) { 190 struct hci_dev *hdev = conn->hdev; 191 struct hci_cp_read_clock_offset clkoff_cp; 192 193 clkoff_cp.handle = cpu_to_le16(conn->handle); 194 hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp), 195 &clkoff_cp); 196 } 197 198 return hci_abort_conn(conn, reason); 199 } 200 201 static void hci_add_sco(struct hci_conn *conn, __u16 handle) 202 { 203 struct hci_dev *hdev = conn->hdev; 204 struct hci_cp_add_sco cp; 205 206 BT_DBG("hcon %p", conn); 207 208 conn->state = BT_CONNECT; 209 conn->out = true; 210 211 conn->attempt++; 212 213 cp.handle = cpu_to_le16(handle); 214 cp.pkt_type = cpu_to_le16(conn->pkt_type); 215 216 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); 217 } 218 219 static bool find_next_esco_param(struct hci_conn *conn, 220 const struct sco_param *esco_param, int size) 221 { 222 if (!conn->parent) 223 return false; 224 225 for (; conn->attempt <= size; conn->attempt++) { 226 if (lmp_esco_2m_capable(conn->parent) || 227 (esco_param[conn->attempt - 1].pkt_type & ESCO_2EV3)) 228 break; 229 BT_DBG("hcon %p skipped attempt %d, eSCO 2M not supported", 230 conn, conn->attempt); 231 } 232 233 return conn->attempt <= size; 234 } 235 236 static int configure_datapath_sync(struct hci_dev *hdev, struct bt_codec *codec) 237 { 238 int err; 239 __u8 vnd_len, *vnd_data = NULL; 240 struct hci_op_configure_data_path *cmd = NULL; 241 242 /* Do not take below 2 checks as error since the 1st means user do not 243 * want to use HFP offload mode and the 2nd means the vendor controller 244 * do not need to send below HCI command for offload mode. 245 */ 246 if (!codec->data_path || !hdev->get_codec_config_data) 247 return 0; 248 249 err = hdev->get_codec_config_data(hdev, ESCO_LINK, codec, &vnd_len, 250 &vnd_data); 251 if (err < 0) 252 goto error; 253 254 cmd = kzalloc(sizeof(*cmd) + vnd_len, GFP_KERNEL); 255 if (!cmd) { 256 err = -ENOMEM; 257 goto error; 258 } 259 260 err = hdev->get_data_path_id(hdev, &cmd->data_path_id); 261 if (err < 0) 262 goto error; 263 264 cmd->vnd_len = vnd_len; 265 memcpy(cmd->vnd_data, vnd_data, vnd_len); 266 267 cmd->direction = 0x00; 268 __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH, 269 sizeof(*cmd) + vnd_len, cmd, HCI_CMD_TIMEOUT); 270 271 cmd->direction = 0x01; 272 err = __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH, 273 sizeof(*cmd) + vnd_len, cmd, 274 HCI_CMD_TIMEOUT); 275 error: 276 277 kfree(cmd); 278 kfree(vnd_data); 279 return err; 280 } 281 282 static int hci_enhanced_setup_sync(struct hci_dev *hdev, void *data) 283 { 284 struct conn_handle_t *conn_handle = data; 285 struct hci_conn *conn = conn_handle->conn; 286 __u16 handle = conn_handle->handle; 287 struct hci_cp_enhanced_setup_sync_conn cp; 288 const struct sco_param *param; 289 290 kfree(conn_handle); 291 292 if (!hci_conn_valid(hdev, conn)) 293 return -ECANCELED; 294 295 bt_dev_dbg(hdev, "hcon %p", conn); 296 297 configure_datapath_sync(hdev, &conn->codec); 298 299 conn->state = BT_CONNECT; 300 conn->out = true; 301 302 conn->attempt++; 303 304 memset(&cp, 0x00, sizeof(cp)); 305 306 cp.handle = cpu_to_le16(handle); 307 308 cp.tx_bandwidth = cpu_to_le32(0x00001f40); 309 cp.rx_bandwidth = cpu_to_le32(0x00001f40); 310 311 switch (conn->codec.id) { 312 case BT_CODEC_MSBC: 313 if (!find_next_esco_param(conn, esco_param_msbc, 314 ARRAY_SIZE(esco_param_msbc))) 315 return -EINVAL; 316 317 param = &esco_param_msbc[conn->attempt - 1]; 318 cp.tx_coding_format.id = 0x05; 319 cp.rx_coding_format.id = 0x05; 320 cp.tx_codec_frame_size = __cpu_to_le16(60); 321 cp.rx_codec_frame_size = __cpu_to_le16(60); 322 cp.in_bandwidth = __cpu_to_le32(32000); 323 cp.out_bandwidth = __cpu_to_le32(32000); 324 cp.in_coding_format.id = 0x04; 325 cp.out_coding_format.id = 0x04; 326 cp.in_coded_data_size = __cpu_to_le16(16); 327 cp.out_coded_data_size = __cpu_to_le16(16); 328 cp.in_pcm_data_format = 2; 329 cp.out_pcm_data_format = 2; 330 cp.in_pcm_sample_payload_msb_pos = 0; 331 cp.out_pcm_sample_payload_msb_pos = 0; 332 cp.in_data_path = conn->codec.data_path; 333 cp.out_data_path = conn->codec.data_path; 334 cp.in_transport_unit_size = 1; 335 cp.out_transport_unit_size = 1; 336 break; 337 338 case BT_CODEC_TRANSPARENT: 339 if (!find_next_esco_param(conn, esco_param_msbc, 340 ARRAY_SIZE(esco_param_msbc))) 341 return false; 342 param = &esco_param_msbc[conn->attempt - 1]; 343 cp.tx_coding_format.id = 0x03; 344 cp.rx_coding_format.id = 0x03; 345 cp.tx_codec_frame_size = __cpu_to_le16(60); 346 cp.rx_codec_frame_size = __cpu_to_le16(60); 347 cp.in_bandwidth = __cpu_to_le32(0x1f40); 348 cp.out_bandwidth = __cpu_to_le32(0x1f40); 349 cp.in_coding_format.id = 0x03; 350 cp.out_coding_format.id = 0x03; 351 cp.in_coded_data_size = __cpu_to_le16(16); 352 cp.out_coded_data_size = __cpu_to_le16(16); 353 cp.in_pcm_data_format = 2; 354 cp.out_pcm_data_format = 2; 355 cp.in_pcm_sample_payload_msb_pos = 0; 356 cp.out_pcm_sample_payload_msb_pos = 0; 357 cp.in_data_path = conn->codec.data_path; 358 cp.out_data_path = conn->codec.data_path; 359 cp.in_transport_unit_size = 1; 360 cp.out_transport_unit_size = 1; 361 break; 362 363 case BT_CODEC_CVSD: 364 if (conn->parent && lmp_esco_capable(conn->parent)) { 365 if (!find_next_esco_param(conn, esco_param_cvsd, 366 ARRAY_SIZE(esco_param_cvsd))) 367 return -EINVAL; 368 param = &esco_param_cvsd[conn->attempt - 1]; 369 } else { 370 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) 371 return -EINVAL; 372 param = &sco_param_cvsd[conn->attempt - 1]; 373 } 374 cp.tx_coding_format.id = 2; 375 cp.rx_coding_format.id = 2; 376 cp.tx_codec_frame_size = __cpu_to_le16(60); 377 cp.rx_codec_frame_size = __cpu_to_le16(60); 378 cp.in_bandwidth = __cpu_to_le32(16000); 379 cp.out_bandwidth = __cpu_to_le32(16000); 380 cp.in_coding_format.id = 4; 381 cp.out_coding_format.id = 4; 382 cp.in_coded_data_size = __cpu_to_le16(16); 383 cp.out_coded_data_size = __cpu_to_le16(16); 384 cp.in_pcm_data_format = 2; 385 cp.out_pcm_data_format = 2; 386 cp.in_pcm_sample_payload_msb_pos = 0; 387 cp.out_pcm_sample_payload_msb_pos = 0; 388 cp.in_data_path = conn->codec.data_path; 389 cp.out_data_path = conn->codec.data_path; 390 cp.in_transport_unit_size = 16; 391 cp.out_transport_unit_size = 16; 392 break; 393 default: 394 return -EINVAL; 395 } 396 397 cp.retrans_effort = param->retrans_effort; 398 cp.pkt_type = __cpu_to_le16(param->pkt_type); 399 cp.max_latency = __cpu_to_le16(param->max_latency); 400 401 if (hci_send_cmd(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0) 402 return -EIO; 403 404 return 0; 405 } 406 407 static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle) 408 { 409 struct hci_dev *hdev = conn->hdev; 410 struct hci_cp_setup_sync_conn cp; 411 const struct sco_param *param; 412 413 bt_dev_dbg(hdev, "hcon %p", conn); 414 415 conn->state = BT_CONNECT; 416 conn->out = true; 417 418 conn->attempt++; 419 420 cp.handle = cpu_to_le16(handle); 421 422 cp.tx_bandwidth = cpu_to_le32(0x00001f40); 423 cp.rx_bandwidth = cpu_to_le32(0x00001f40); 424 cp.voice_setting = cpu_to_le16(conn->setting); 425 426 switch (conn->setting & SCO_AIRMODE_MASK) { 427 case SCO_AIRMODE_TRANSP: 428 if (!find_next_esco_param(conn, esco_param_msbc, 429 ARRAY_SIZE(esco_param_msbc))) 430 return false; 431 param = &esco_param_msbc[conn->attempt - 1]; 432 break; 433 case SCO_AIRMODE_CVSD: 434 if (conn->parent && lmp_esco_capable(conn->parent)) { 435 if (!find_next_esco_param(conn, esco_param_cvsd, 436 ARRAY_SIZE(esco_param_cvsd))) 437 return false; 438 param = &esco_param_cvsd[conn->attempt - 1]; 439 } else { 440 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) 441 return false; 442 param = &sco_param_cvsd[conn->attempt - 1]; 443 } 444 break; 445 default: 446 return false; 447 } 448 449 cp.retrans_effort = param->retrans_effort; 450 cp.pkt_type = __cpu_to_le16(param->pkt_type); 451 cp.max_latency = __cpu_to_le16(param->max_latency); 452 453 if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0) 454 return false; 455 456 return true; 457 } 458 459 bool hci_setup_sync(struct hci_conn *conn, __u16 handle) 460 { 461 int result; 462 struct conn_handle_t *conn_handle; 463 464 if (enhanced_sync_conn_capable(conn->hdev)) { 465 conn_handle = kzalloc(sizeof(*conn_handle), GFP_KERNEL); 466 467 if (!conn_handle) 468 return false; 469 470 conn_handle->conn = conn; 471 conn_handle->handle = handle; 472 result = hci_cmd_sync_queue(conn->hdev, hci_enhanced_setup_sync, 473 conn_handle, NULL); 474 if (result < 0) 475 kfree(conn_handle); 476 477 return result == 0; 478 } 479 480 return hci_setup_sync_conn(conn, handle); 481 } 482 483 u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency, 484 u16 to_multiplier) 485 { 486 struct hci_dev *hdev = conn->hdev; 487 struct hci_conn_params *params; 488 struct hci_cp_le_conn_update cp; 489 490 hci_dev_lock(hdev); 491 492 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); 493 if (params) { 494 params->conn_min_interval = min; 495 params->conn_max_interval = max; 496 params->conn_latency = latency; 497 params->supervision_timeout = to_multiplier; 498 } 499 500 hci_dev_unlock(hdev); 501 502 memset(&cp, 0, sizeof(cp)); 503 cp.handle = cpu_to_le16(conn->handle); 504 cp.conn_interval_min = cpu_to_le16(min); 505 cp.conn_interval_max = cpu_to_le16(max); 506 cp.conn_latency = cpu_to_le16(latency); 507 cp.supervision_timeout = cpu_to_le16(to_multiplier); 508 cp.min_ce_len = cpu_to_le16(0x0000); 509 cp.max_ce_len = cpu_to_le16(0x0000); 510 511 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); 512 513 if (params) 514 return 0x01; 515 516 return 0x00; 517 } 518 519 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand, 520 __u8 ltk[16], __u8 key_size) 521 { 522 struct hci_dev *hdev = conn->hdev; 523 struct hci_cp_le_start_enc cp; 524 525 BT_DBG("hcon %p", conn); 526 527 memset(&cp, 0, sizeof(cp)); 528 529 cp.handle = cpu_to_le16(conn->handle); 530 cp.rand = rand; 531 cp.ediv = ediv; 532 memcpy(cp.ltk, ltk, key_size); 533 534 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); 535 } 536 537 /* Device _must_ be locked */ 538 void hci_sco_setup(struct hci_conn *conn, __u8 status) 539 { 540 struct hci_link *link; 541 542 link = list_first_entry_or_null(&conn->link_list, struct hci_link, list); 543 if (!link || !link->conn) 544 return; 545 546 BT_DBG("hcon %p", conn); 547 548 if (!status) { 549 if (lmp_esco_capable(conn->hdev)) 550 hci_setup_sync(link->conn, conn->handle); 551 else 552 hci_add_sco(link->conn, conn->handle); 553 } else { 554 hci_connect_cfm(link->conn, status); 555 hci_conn_del(link->conn); 556 } 557 } 558 559 static void hci_conn_timeout(struct work_struct *work) 560 { 561 struct hci_conn *conn = container_of(work, struct hci_conn, 562 disc_work.work); 563 int refcnt = atomic_read(&conn->refcnt); 564 565 BT_DBG("hcon %p state %s", conn, state_to_string(conn->state)); 566 567 WARN_ON(refcnt < 0); 568 569 /* FIXME: It was observed that in pairing failed scenario, refcnt 570 * drops below 0. Probably this is because l2cap_conn_del calls 571 * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is 572 * dropped. After that loop hci_chan_del is called which also drops 573 * conn. For now make sure that ACL is alive if refcnt is higher then 0, 574 * otherwise drop it. 575 */ 576 if (refcnt > 0) 577 return; 578 579 hci_abort_conn(conn, hci_proto_disconn_ind(conn)); 580 } 581 582 /* Enter sniff mode */ 583 static void hci_conn_idle(struct work_struct *work) 584 { 585 struct hci_conn *conn = container_of(work, struct hci_conn, 586 idle_work.work); 587 struct hci_dev *hdev = conn->hdev; 588 589 BT_DBG("hcon %p mode %d", conn, conn->mode); 590 591 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) 592 return; 593 594 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) 595 return; 596 597 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { 598 struct hci_cp_sniff_subrate cp; 599 cp.handle = cpu_to_le16(conn->handle); 600 cp.max_latency = cpu_to_le16(0); 601 cp.min_remote_timeout = cpu_to_le16(0); 602 cp.min_local_timeout = cpu_to_le16(0); 603 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); 604 } 605 606 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { 607 struct hci_cp_sniff_mode cp; 608 cp.handle = cpu_to_le16(conn->handle); 609 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); 610 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); 611 cp.attempt = cpu_to_le16(4); 612 cp.timeout = cpu_to_le16(1); 613 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); 614 } 615 } 616 617 static void hci_conn_auto_accept(struct work_struct *work) 618 { 619 struct hci_conn *conn = container_of(work, struct hci_conn, 620 auto_accept_work.work); 621 622 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), 623 &conn->dst); 624 } 625 626 static void le_disable_advertising(struct hci_dev *hdev) 627 { 628 if (ext_adv_capable(hdev)) { 629 struct hci_cp_le_set_ext_adv_enable cp; 630 631 cp.enable = 0x00; 632 cp.num_of_sets = 0x00; 633 634 hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp), 635 &cp); 636 } else { 637 u8 enable = 0x00; 638 hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), 639 &enable); 640 } 641 } 642 643 static void le_conn_timeout(struct work_struct *work) 644 { 645 struct hci_conn *conn = container_of(work, struct hci_conn, 646 le_conn_timeout.work); 647 struct hci_dev *hdev = conn->hdev; 648 649 BT_DBG(""); 650 651 /* We could end up here due to having done directed advertising, 652 * so clean up the state if necessary. This should however only 653 * happen with broken hardware or if low duty cycle was used 654 * (which doesn't have a timeout of its own). 655 */ 656 if (conn->role == HCI_ROLE_SLAVE) { 657 /* Disable LE Advertising */ 658 le_disable_advertising(hdev); 659 hci_dev_lock(hdev); 660 hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT); 661 hci_dev_unlock(hdev); 662 return; 663 } 664 665 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); 666 } 667 668 struct iso_list_data { 669 union { 670 u8 cig; 671 u8 big; 672 }; 673 union { 674 u8 cis; 675 u8 bis; 676 u16 sync_handle; 677 }; 678 int count; 679 bool big_term; 680 bool pa_sync_term; 681 bool big_sync_term; 682 }; 683 684 static void bis_list(struct hci_conn *conn, void *data) 685 { 686 struct iso_list_data *d = data; 687 688 /* Skip if not broadcast/ANY address */ 689 if (bacmp(&conn->dst, BDADDR_ANY)) 690 return; 691 692 if (d->big != conn->iso_qos.bcast.big || d->bis == BT_ISO_QOS_BIS_UNSET || 693 d->bis != conn->iso_qos.bcast.bis) 694 return; 695 696 d->count++; 697 } 698 699 static int terminate_big_sync(struct hci_dev *hdev, void *data) 700 { 701 struct iso_list_data *d = data; 702 703 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", d->big, d->bis); 704 705 hci_disable_per_advertising_sync(hdev, d->bis); 706 hci_remove_ext_adv_instance_sync(hdev, d->bis, NULL); 707 708 /* Only terminate BIG if it has been created */ 709 if (!d->big_term) 710 return 0; 711 712 return hci_le_terminate_big_sync(hdev, d->big, 713 HCI_ERROR_LOCAL_HOST_TERM); 714 } 715 716 static void terminate_big_destroy(struct hci_dev *hdev, void *data, int err) 717 { 718 kfree(data); 719 } 720 721 static int hci_le_terminate_big(struct hci_dev *hdev, struct hci_conn *conn) 722 { 723 struct iso_list_data *d; 724 int ret; 725 726 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", conn->iso_qos.bcast.big, 727 conn->iso_qos.bcast.bis); 728 729 d = kzalloc(sizeof(*d), GFP_KERNEL); 730 if (!d) 731 return -ENOMEM; 732 733 d->big = conn->iso_qos.bcast.big; 734 d->bis = conn->iso_qos.bcast.bis; 735 d->big_term = test_and_clear_bit(HCI_CONN_BIG_CREATED, &conn->flags); 736 737 ret = hci_cmd_sync_queue(hdev, terminate_big_sync, d, 738 terminate_big_destroy); 739 if (ret) 740 kfree(d); 741 742 return ret; 743 } 744 745 static int big_terminate_sync(struct hci_dev *hdev, void *data) 746 { 747 struct iso_list_data *d = data; 748 749 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", d->big, 750 d->sync_handle); 751 752 if (d->big_sync_term) 753 hci_le_big_terminate_sync(hdev, d->big); 754 755 if (d->pa_sync_term) 756 return hci_le_pa_terminate_sync(hdev, d->sync_handle); 757 758 return 0; 759 } 760 761 static void find_bis(struct hci_conn *conn, void *data) 762 { 763 struct iso_list_data *d = data; 764 765 /* Ignore if BIG doesn't match */ 766 if (d->big != conn->iso_qos.bcast.big) 767 return; 768 769 d->count++; 770 } 771 772 static int hci_le_big_terminate(struct hci_dev *hdev, u8 big, struct hci_conn *conn) 773 { 774 struct iso_list_data *d; 775 int ret; 776 777 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", big, conn->sync_handle); 778 779 d = kzalloc(sizeof(*d), GFP_KERNEL); 780 if (!d) 781 return -ENOMEM; 782 783 d->big = big; 784 d->sync_handle = conn->sync_handle; 785 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); 789 790 if (!d->count) 791 d->pa_sync_term = true; 792 793 d->count = 0; 794 } 795 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); 799 800 if (!d->count) 801 d->big_sync_term = true; 802 } 803 804 ret = hci_cmd_sync_queue(hdev, big_terminate_sync, d, 805 terminate_big_destroy); 806 if (ret) 807 kfree(d); 808 809 return ret; 810 } 811 812 /* Cleanup BIS connection 813 * 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. 817 */ 818 static void bis_cleanup(struct hci_conn *conn) 819 { 820 struct hci_dev *hdev = conn->hdev; 821 struct hci_conn *bis; 822 823 bt_dev_dbg(hdev, "conn %p", conn); 824 825 if (conn->role == HCI_ROLE_MASTER) { 826 if (!test_and_clear_bit(HCI_CONN_PER_ADV, &conn->flags)) 827 return; 828 829 /* Check if ISO connection is a BIS and terminate advertising 830 * set and BIG if there are no other connections using it. 831 */ 832 bis = hci_conn_hash_lookup_big(hdev, conn->iso_qos.bcast.big); 833 if (bis) 834 return; 835 836 hci_le_terminate_big(hdev, conn); 837 } else { 838 hci_le_big_terminate(hdev, conn->iso_qos.bcast.big, 839 conn); 840 } 841 } 842 843 static int remove_cig_sync(struct hci_dev *hdev, void *data) 844 { 845 u8 handle = PTR_UINT(data); 846 847 return hci_le_remove_cig_sync(hdev, handle); 848 } 849 850 static int hci_le_remove_cig(struct hci_dev *hdev, u8 handle) 851 { 852 bt_dev_dbg(hdev, "handle 0x%2.2x", handle); 853 854 return hci_cmd_sync_queue(hdev, remove_cig_sync, UINT_PTR(handle), 855 NULL); 856 } 857 858 static void find_cis(struct hci_conn *conn, void *data) 859 { 860 struct iso_list_data *d = data; 861 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; 865 866 d->count++; 867 } 868 869 /* Cleanup CIS connection: 870 * 871 * Detects if there any CIS left connected in a CIG and remove it. 872 */ 873 static void cis_cleanup(struct hci_conn *conn) 874 { 875 struct hci_dev *hdev = conn->hdev; 876 struct iso_list_data d; 877 878 if (conn->iso_qos.ucast.cig == BT_ISO_QOS_CIG_UNSET) 879 return; 880 881 memset(&d, 0, sizeof(d)); 882 d.cig = conn->iso_qos.ucast.cig; 883 884 /* Check if ISO connection is a CIS and remove CIG if there are 885 * no other connections using it. 886 */ 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; 892 893 hci_le_remove_cig(hdev, conn->iso_qos.ucast.cig); 894 } 895 896 static int hci_conn_hash_alloc_unset(struct hci_dev *hdev) 897 { 898 return ida_alloc_range(&hdev->unset_handle_ida, HCI_CONN_HANDLE_MAX + 1, 899 U16_MAX, GFP_ATOMIC); 900 } 901 902 static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst, 903 u8 role, u16 handle) 904 { 905 struct hci_conn *conn; 906 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); 931 } 932 933 bt_dev_dbg(hdev, "dst %pMR handle 0x%4.4x", dst, handle); 934 935 conn = kzalloc(sizeof(*conn), GFP_KERNEL); 936 if (!conn) 937 return ERR_PTR(-ENOMEM); 938 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; 955 956 set_bit(HCI_CONN_POWER_SAVE, &conn->flags); 957 conn->disc_timeout = HCI_DISCONN_TIMEOUT; 958 959 /* Set Default Authenticated payload timeout to 30s */ 960 conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT; 961 962 if (conn->role == HCI_ROLE_MASTER) 963 conn->out = true; 964 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); 978 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; 984 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; 994 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; 1001 } 1002 1003 skb_queue_head_init(&conn->data_q); 1004 1005 INIT_LIST_HEAD(&conn->chan_list); 1006 INIT_LIST_HEAD(&conn->link_list); 1007 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); 1012 1013 atomic_set(&conn->refcnt, 0); 1014 1015 hci_dev_hold(hdev); 1016 1017 hci_conn_hash_add(hdev, conn); 1018 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. 1022 */ 1023 if (conn->type != SCO_LINK && conn->type != ESCO_LINK) { 1024 if (hdev->notify) 1025 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); 1026 } 1027 1028 hci_conn_init_sysfs(conn); 1029 1030 return conn; 1031 } 1032 1033 struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type, 1034 bdaddr_t *dst, u8 role) 1035 { 1036 int handle; 1037 1038 bt_dev_dbg(hdev, "dst %pMR", dst); 1039 1040 handle = hci_conn_hash_alloc_unset(hdev); 1041 if (unlikely(handle < 0)) 1042 return ERR_PTR(-ECONNREFUSED); 1043 1044 return __hci_conn_add(hdev, type, dst, role, handle); 1045 } 1046 1047 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst, 1048 u8 role, u16 handle) 1049 { 1050 if (handle > HCI_CONN_HANDLE_MAX) 1051 return ERR_PTR(-EINVAL); 1052 1053 return __hci_conn_add(hdev, type, dst, role, handle); 1054 } 1055 1056 static void hci_conn_cleanup_child(struct hci_conn *conn, u8 reason) 1057 { 1058 if (!reason) 1059 reason = HCI_ERROR_REMOTE_USER_TERM; 1060 1061 /* Due to race, SCO/ISO conn might be not established yet at this point, 1062 * and nothing else will clean it up. In other cases it is done via HCI 1063 * events. 1064 */ 1065 switch (conn->type) { 1066 case SCO_LINK: 1067 case ESCO_LINK: 1068 if (HCI_CONN_HANDLE_UNSET(conn->handle)) 1069 hci_conn_failed(conn, reason); 1070 break; 1071 case ISO_LINK: 1072 if ((conn->state != BT_CONNECTED && 1073 !test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) || 1074 test_bit(HCI_CONN_BIG_CREATED, &conn->flags)) 1075 hci_conn_failed(conn, reason); 1076 break; 1077 } 1078 } 1079 1080 static void hci_conn_unlink(struct hci_conn *conn) 1081 { 1082 struct hci_dev *hdev = conn->hdev; 1083 1084 bt_dev_dbg(hdev, "hcon %p", conn); 1085 1086 if (!conn->parent) { 1087 struct hci_link *link, *t; 1088 1089 list_for_each_entry_safe(link, t, &conn->link_list, list) { 1090 struct hci_conn *child = link->conn; 1091 1092 hci_conn_unlink(child); 1093 1094 /* If hdev is down it means 1095 * hci_dev_close_sync/hci_conn_hash_flush is in progress 1096 * and links don't need to be cleanup as all connections 1097 * would be cleanup. 1098 */ 1099 if (!test_bit(HCI_UP, &hdev->flags)) 1100 continue; 1101 1102 hci_conn_cleanup_child(child, conn->abort_reason); 1103 } 1104 1105 return; 1106 } 1107 1108 if (!conn->link) 1109 return; 1110 1111 list_del_rcu(&conn->link->list); 1112 synchronize_rcu(); 1113 1114 hci_conn_drop(conn->parent); 1115 hci_conn_put(conn->parent); 1116 conn->parent = NULL; 1117 1118 kfree(conn->link); 1119 conn->link = NULL; 1120 } 1121 1122 void hci_conn_del(struct hci_conn *conn) 1123 { 1124 struct hci_dev *hdev = conn->hdev; 1125 1126 BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle); 1127 1128 hci_conn_unlink(conn); 1129 1130 cancel_delayed_work_sync(&conn->disc_work); 1131 cancel_delayed_work_sync(&conn->auto_accept_work); 1132 cancel_delayed_work_sync(&conn->idle_work); 1133 1134 if (conn->type == ACL_LINK) { 1135 /* Unacked frames */ 1136 hdev->acl_cnt += conn->sent; 1137 } else if (conn->type == LE_LINK) { 1138 cancel_delayed_work(&conn->le_conn_timeout); 1139 1140 if (hdev->le_pkts) 1141 hdev->le_cnt += conn->sent; 1142 else 1143 hdev->acl_cnt += conn->sent; 1144 } else { 1145 /* Unacked ISO frames */ 1146 if (conn->type == ISO_LINK) { 1147 if (hdev->iso_pkts) 1148 hdev->iso_cnt += conn->sent; 1149 else if (hdev->le_pkts) 1150 hdev->le_cnt += conn->sent; 1151 else 1152 hdev->acl_cnt += conn->sent; 1153 } 1154 } 1155 1156 skb_queue_purge(&conn->data_q); 1157 1158 /* Remove the connection from the list and cleanup its remaining 1159 * state. This is a separate function since for some cases like 1160 * BT_CONNECT_SCAN we *only* want the cleanup part without the 1161 * rest of hci_conn_del. 1162 */ 1163 hci_conn_cleanup(conn); 1164 1165 /* Dequeue callbacks using connection pointer as data */ 1166 hci_cmd_sync_dequeue(hdev, NULL, conn, NULL); 1167 } 1168 1169 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type) 1170 { 1171 int use_src = bacmp(src, BDADDR_ANY); 1172 struct hci_dev *hdev = NULL, *d; 1173 1174 BT_DBG("%pMR -> %pMR", src, dst); 1175 1176 read_lock(&hci_dev_list_lock); 1177 1178 list_for_each_entry(d, &hci_dev_list, list) { 1179 if (!test_bit(HCI_UP, &d->flags) || 1180 hci_dev_test_flag(d, HCI_USER_CHANNEL)) 1181 continue; 1182 1183 /* Simple routing: 1184 * No source address - find interface with bdaddr != dst 1185 * Source address - find interface with bdaddr == src 1186 */ 1187 1188 if (use_src) { 1189 bdaddr_t id_addr; 1190 u8 id_addr_type; 1191 1192 if (src_type == BDADDR_BREDR) { 1193 if (!lmp_bredr_capable(d)) 1194 continue; 1195 bacpy(&id_addr, &d->bdaddr); 1196 id_addr_type = BDADDR_BREDR; 1197 } else { 1198 if (!lmp_le_capable(d)) 1199 continue; 1200 1201 hci_copy_identity_address(d, &id_addr, 1202 &id_addr_type); 1203 1204 /* Convert from HCI to three-value type */ 1205 if (id_addr_type == ADDR_LE_DEV_PUBLIC) 1206 id_addr_type = BDADDR_LE_PUBLIC; 1207 else 1208 id_addr_type = BDADDR_LE_RANDOM; 1209 } 1210 1211 if (!bacmp(&id_addr, src) && id_addr_type == src_type) { 1212 hdev = d; break; 1213 } 1214 } else { 1215 if (bacmp(&d->bdaddr, dst)) { 1216 hdev = d; break; 1217 } 1218 } 1219 } 1220 1221 if (hdev) 1222 hdev = hci_dev_hold(hdev); 1223 1224 read_unlock(&hci_dev_list_lock); 1225 return hdev; 1226 } 1227 EXPORT_SYMBOL(hci_get_route); 1228 1229 /* This function requires the caller holds hdev->lock */ 1230 static void hci_le_conn_failed(struct hci_conn *conn, u8 status) 1231 { 1232 struct hci_dev *hdev = conn->hdev; 1233 1234 hci_connect_le_scan_cleanup(conn, status); 1235 1236 /* Enable advertising in case this was a failed connection 1237 * attempt as a peripheral. 1238 */ 1239 hci_enable_advertising(hdev); 1240 } 1241 1242 /* This function requires the caller holds hdev->lock */ 1243 void hci_conn_failed(struct hci_conn *conn, u8 status) 1244 { 1245 struct hci_dev *hdev = conn->hdev; 1246 1247 bt_dev_dbg(hdev, "status 0x%2.2x", status); 1248 1249 switch (conn->type) { 1250 case LE_LINK: 1251 hci_le_conn_failed(conn, status); 1252 break; 1253 case ACL_LINK: 1254 mgmt_connect_failed(hdev, conn, 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 /* Run immediately if on cmd_sync_work since this may be called 2956 * as a result to MGMT_OP_DISCONNECT/MGMT_OP_UNPAIR which does 2957 * already queue its callback on cmd_sync_work. 2958 */ 2959 return hci_cmd_sync_run_once(hdev, abort_conn_sync, conn, NULL); 2960 } 2961