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