1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 BlueZ - Bluetooth protocol stack for Linux 4 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved. 5 Copyright 2023-2024 NXP 6 7 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 8 9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 20 SOFTWARE IS DISCLAIMED. 21 */ 22 23 /* Bluetooth HCI connection handling. */ 24 25 #include <linux/export.h> 26 #include <linux/debugfs.h> 27 #include <linux/errqueue.h> 28 29 #include <net/bluetooth/bluetooth.h> 30 #include <net/bluetooth/hci_core.h> 31 #include <net/bluetooth/l2cap.h> 32 #include <net/bluetooth/iso.h> 33 #include <net/bluetooth/mgmt.h> 34 35 #include "smp.h" 36 #include "eir.h" 37 38 struct sco_param { 39 u16 pkt_type; 40 u16 max_latency; 41 u8 retrans_effort; 42 }; 43 44 struct conn_handle_t { 45 struct hci_conn *conn; 46 __u16 handle; 47 }; 48 49 static const struct sco_param esco_param_cvsd[] = { 50 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */ 51 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */ 52 { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */ 53 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */ 54 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */ 55 }; 56 57 static const struct sco_param sco_param_cvsd[] = { 58 { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */ 59 { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */ 60 }; 61 62 static const struct sco_param esco_param_msbc[] = { 63 { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */ 64 { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */ 65 }; 66 67 /* This function requires the caller holds hdev->lock */ 68 void hci_connect_le_scan_cleanup(struct hci_conn *conn, u8 status) 69 { 70 struct hci_conn_params *params; 71 struct hci_dev *hdev = conn->hdev; 72 struct smp_irk *irk; 73 bdaddr_t *bdaddr; 74 u8 bdaddr_type; 75 76 bdaddr = &conn->dst; 77 bdaddr_type = conn->dst_type; 78 79 /* Check if we need to convert to identity address */ 80 irk = hci_get_irk(hdev, bdaddr, bdaddr_type); 81 if (irk) { 82 bdaddr = &irk->bdaddr; 83 bdaddr_type = irk->addr_type; 84 } 85 86 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr, 87 bdaddr_type); 88 if (!params) 89 return; 90 91 if (params->conn) { 92 hci_conn_drop(params->conn); 93 hci_conn_put(params->conn); 94 params->conn = NULL; 95 } 96 97 if (!params->explicit_connect) 98 return; 99 100 /* If the status indicates successful cancellation of 101 * the attempt (i.e. Unknown Connection Id) there's no point of 102 * notifying failure since we'll go back to keep trying to 103 * connect. The only exception is explicit connect requests 104 * where a timeout + cancel does indicate an actual failure. 105 */ 106 if (status && status != HCI_ERROR_UNKNOWN_CONN_ID) 107 mgmt_connect_failed(hdev, conn, status); 108 109 /* The connection attempt was doing scan for new RPA, and is 110 * in scan phase. If params are not associated with any other 111 * autoconnect action, remove them completely. If they are, just unmark 112 * them as waiting for connection, by clearing explicit_connect field. 113 */ 114 params->explicit_connect = false; 115 116 hci_pend_le_list_del_init(params); 117 118 switch (params->auto_connect) { 119 case HCI_AUTO_CONN_EXPLICIT: 120 hci_conn_params_del(hdev, bdaddr, bdaddr_type); 121 /* return instead of break to avoid duplicate scan update */ 122 return; 123 case HCI_AUTO_CONN_DIRECT: 124 case HCI_AUTO_CONN_ALWAYS: 125 hci_pend_le_list_add(params, &hdev->pend_le_conns); 126 break; 127 case HCI_AUTO_CONN_REPORT: 128 hci_pend_le_list_add(params, &hdev->pend_le_reports); 129 break; 130 default: 131 break; 132 } 133 134 hci_update_passive_scan(hdev); 135 } 136 137 static void hci_conn_cleanup(struct hci_conn *conn) 138 { 139 struct hci_dev *hdev = conn->hdev; 140 141 if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags)) 142 hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type); 143 144 if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags)) 145 hci_remove_link_key(hdev, &conn->dst); 146 147 hci_chan_list_flush(conn); 148 149 if (HCI_CONN_HANDLE_UNSET(conn->handle)) 150 ida_free(&hdev->unset_handle_ida, conn->handle); 151 152 if (conn->cleanup) 153 conn->cleanup(conn); 154 155 if (conn->type == SCO_LINK || conn->type == ESCO_LINK) { 156 switch (conn->setting & SCO_AIRMODE_MASK) { 157 case SCO_AIRMODE_CVSD: 158 case SCO_AIRMODE_TRANSP: 159 if (hdev->notify) 160 hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO); 161 break; 162 } 163 } else { 164 if (hdev->notify) 165 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); 166 } 167 168 debugfs_remove_recursive(conn->debugfs); 169 170 hci_conn_del_sysfs(conn); 171 172 hci_dev_put(hdev); 173 } 174 175 int hci_disconnect(struct hci_conn *conn, __u8 reason) 176 { 177 BT_DBG("hcon %p", conn); 178 179 /* When we are central of an established connection and it enters 180 * the disconnect timeout, then go ahead and try to read the 181 * current clock offset. Processing of the result is done 182 * within the event handling and hci_clock_offset_evt function. 183 */ 184 if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER && 185 (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) { 186 struct hci_dev *hdev = conn->hdev; 187 struct hci_cp_read_clock_offset clkoff_cp; 188 189 clkoff_cp.handle = cpu_to_le16(conn->handle); 190 hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp), 191 &clkoff_cp); 192 } 193 194 return hci_abort_conn(conn, reason); 195 } 196 197 static void hci_add_sco(struct hci_conn *conn, __u16 handle) 198 { 199 struct hci_dev *hdev = conn->hdev; 200 struct hci_cp_add_sco cp; 201 202 BT_DBG("hcon %p", conn); 203 204 conn->state = BT_CONNECT; 205 conn->out = true; 206 207 conn->attempt++; 208 209 cp.handle = cpu_to_le16(handle); 210 cp.pkt_type = cpu_to_le16(conn->pkt_type); 211 212 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); 213 } 214 215 static bool find_next_esco_param(struct hci_conn *conn, 216 const struct sco_param *esco_param, int size) 217 { 218 if (!conn->parent) 219 return false; 220 221 for (; conn->attempt <= size; conn->attempt++) { 222 if (lmp_esco_2m_capable(conn->parent) || 223 (esco_param[conn->attempt - 1].pkt_type & ESCO_2EV3)) 224 break; 225 BT_DBG("hcon %p skipped attempt %d, eSCO 2M not supported", 226 conn, conn->attempt); 227 } 228 229 return conn->attempt <= size; 230 } 231 232 static int configure_datapath_sync(struct hci_dev *hdev, struct bt_codec *codec) 233 { 234 int err; 235 __u8 vnd_len, *vnd_data = NULL; 236 struct hci_op_configure_data_path *cmd = NULL; 237 238 /* Do not take below 2 checks as error since the 1st means user do not 239 * want to use HFP offload mode and the 2nd means the vendor controller 240 * do not need to send below HCI command for offload mode. 241 */ 242 if (!codec->data_path || !hdev->get_codec_config_data) 243 return 0; 244 245 err = hdev->get_codec_config_data(hdev, ESCO_LINK, codec, &vnd_len, 246 &vnd_data); 247 if (err < 0) 248 goto error; 249 250 cmd = kzalloc(sizeof(*cmd) + vnd_len, GFP_KERNEL); 251 if (!cmd) { 252 err = -ENOMEM; 253 goto error; 254 } 255 256 err = hdev->get_data_path_id(hdev, &cmd->data_path_id); 257 if (err < 0) 258 goto error; 259 260 cmd->vnd_len = vnd_len; 261 memcpy(cmd->vnd_data, vnd_data, vnd_len); 262 263 cmd->direction = 0x00; 264 __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH, 265 sizeof(*cmd) + vnd_len, cmd, HCI_CMD_TIMEOUT); 266 267 cmd->direction = 0x01; 268 err = __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH, 269 sizeof(*cmd) + vnd_len, cmd, 270 HCI_CMD_TIMEOUT); 271 error: 272 273 kfree(cmd); 274 kfree(vnd_data); 275 return err; 276 } 277 278 static int hci_enhanced_setup_sync(struct hci_dev *hdev, void *data) 279 { 280 struct conn_handle_t *conn_handle = data; 281 struct hci_conn *conn = conn_handle->conn; 282 __u16 handle = conn_handle->handle; 283 struct hci_cp_enhanced_setup_sync_conn cp; 284 const struct sco_param *param; 285 286 if (!hci_conn_valid(hdev, conn)) 287 return -ECANCELED; 288 289 bt_dev_dbg(hdev, "hcon %p", conn); 290 291 configure_datapath_sync(hdev, &conn->codec); 292 293 conn->state = BT_CONNECT; 294 conn->out = true; 295 296 conn->attempt++; 297 298 memset(&cp, 0x00, sizeof(cp)); 299 300 cp.handle = cpu_to_le16(handle); 301 302 cp.tx_bandwidth = cpu_to_le32(0x00001f40); 303 cp.rx_bandwidth = cpu_to_le32(0x00001f40); 304 305 hci_dev_lock(hdev); 306 307 switch (conn->codec.id) { 308 case BT_CODEC_MSBC: 309 if (!find_next_esco_param(conn, esco_param_msbc, 310 ARRAY_SIZE(esco_param_msbc))) 311 goto unlock; 312 313 param = &esco_param_msbc[conn->attempt - 1]; 314 cp.tx_coding_format.id = 0x05; 315 cp.rx_coding_format.id = 0x05; 316 cp.tx_codec_frame_size = __cpu_to_le16(60); 317 cp.rx_codec_frame_size = __cpu_to_le16(60); 318 cp.in_bandwidth = __cpu_to_le32(32000); 319 cp.out_bandwidth = __cpu_to_le32(32000); 320 cp.in_coding_format.id = 0x04; 321 cp.out_coding_format.id = 0x04; 322 cp.in_coded_data_size = __cpu_to_le16(16); 323 cp.out_coded_data_size = __cpu_to_le16(16); 324 cp.in_pcm_data_format = 2; 325 cp.out_pcm_data_format = 2; 326 cp.in_pcm_sample_payload_msb_pos = 0; 327 cp.out_pcm_sample_payload_msb_pos = 0; 328 cp.in_data_path = conn->codec.data_path; 329 cp.out_data_path = conn->codec.data_path; 330 cp.in_transport_unit_size = 1; 331 cp.out_transport_unit_size = 1; 332 break; 333 334 case BT_CODEC_TRANSPARENT: 335 if (!find_next_esco_param(conn, esco_param_msbc, 336 ARRAY_SIZE(esco_param_msbc))) 337 goto unlock; 338 339 param = &esco_param_msbc[conn->attempt - 1]; 340 cp.tx_coding_format.id = 0x03; 341 cp.rx_coding_format.id = 0x03; 342 cp.tx_codec_frame_size = __cpu_to_le16(60); 343 cp.rx_codec_frame_size = __cpu_to_le16(60); 344 cp.in_bandwidth = __cpu_to_le32(0x1f40); 345 cp.out_bandwidth = __cpu_to_le32(0x1f40); 346 cp.in_coding_format.id = 0x03; 347 cp.out_coding_format.id = 0x03; 348 cp.in_coded_data_size = __cpu_to_le16(16); 349 cp.out_coded_data_size = __cpu_to_le16(16); 350 cp.in_pcm_data_format = 2; 351 cp.out_pcm_data_format = 2; 352 cp.in_pcm_sample_payload_msb_pos = 0; 353 cp.out_pcm_sample_payload_msb_pos = 0; 354 cp.in_data_path = conn->codec.data_path; 355 cp.out_data_path = conn->codec.data_path; 356 cp.in_transport_unit_size = 1; 357 cp.out_transport_unit_size = 1; 358 break; 359 360 case BT_CODEC_CVSD: 361 if (conn->parent && lmp_esco_capable(conn->parent)) { 362 if (!find_next_esco_param(conn, esco_param_cvsd, 363 ARRAY_SIZE(esco_param_cvsd))) 364 goto unlock; 365 param = &esco_param_cvsd[conn->attempt - 1]; 366 } else { 367 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) 368 goto unlock; 369 param = &sco_param_cvsd[conn->attempt - 1]; 370 } 371 cp.tx_coding_format.id = 2; 372 cp.rx_coding_format.id = 2; 373 cp.tx_codec_frame_size = __cpu_to_le16(60); 374 cp.rx_codec_frame_size = __cpu_to_le16(60); 375 cp.in_bandwidth = __cpu_to_le32(16000); 376 cp.out_bandwidth = __cpu_to_le32(16000); 377 cp.in_coding_format.id = 4; 378 cp.out_coding_format.id = 4; 379 cp.in_coded_data_size = __cpu_to_le16(16); 380 cp.out_coded_data_size = __cpu_to_le16(16); 381 cp.in_pcm_data_format = 2; 382 cp.out_pcm_data_format = 2; 383 cp.in_pcm_sample_payload_msb_pos = 0; 384 cp.out_pcm_sample_payload_msb_pos = 0; 385 cp.in_data_path = conn->codec.data_path; 386 cp.out_data_path = conn->codec.data_path; 387 cp.in_transport_unit_size = 16; 388 cp.out_transport_unit_size = 16; 389 break; 390 default: 391 goto unlock; 392 } 393 394 hci_dev_unlock(hdev); 395 396 cp.retrans_effort = param->retrans_effort; 397 cp.pkt_type = __cpu_to_le16(param->pkt_type); 398 cp.max_latency = __cpu_to_le16(param->max_latency); 399 400 if (hci_send_cmd(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0) 401 return -EIO; 402 403 return 0; 404 405 unlock: 406 hci_dev_unlock(hdev); 407 return -EINVAL; 408 } 409 410 static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle) 411 { 412 struct hci_dev *hdev = conn->hdev; 413 struct hci_cp_setup_sync_conn cp; 414 const struct sco_param *param; 415 416 bt_dev_dbg(hdev, "hcon %p", conn); 417 418 conn->state = BT_CONNECT; 419 conn->out = true; 420 421 conn->attempt++; 422 423 cp.handle = cpu_to_le16(handle); 424 425 cp.tx_bandwidth = cpu_to_le32(0x00001f40); 426 cp.rx_bandwidth = cpu_to_le32(0x00001f40); 427 cp.voice_setting = cpu_to_le16(conn->setting); 428 429 switch (conn->setting & SCO_AIRMODE_MASK) { 430 case SCO_AIRMODE_TRANSP: 431 if (!find_next_esco_param(conn, esco_param_msbc, 432 ARRAY_SIZE(esco_param_msbc))) 433 return false; 434 param = &esco_param_msbc[conn->attempt - 1]; 435 break; 436 case SCO_AIRMODE_CVSD: 437 if (conn->parent && lmp_esco_capable(conn->parent)) { 438 if (!find_next_esco_param(conn, esco_param_cvsd, 439 ARRAY_SIZE(esco_param_cvsd))) 440 return false; 441 param = &esco_param_cvsd[conn->attempt - 1]; 442 } else { 443 if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) 444 return false; 445 param = &sco_param_cvsd[conn->attempt - 1]; 446 } 447 break; 448 default: 449 return false; 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_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0) 457 return false; 458 459 return true; 460 } 461 462 static void hci_enhanced_setup_sync_destroy(struct hci_dev *hdev, void *data, 463 int err) 464 { 465 struct conn_handle_t *conn_handle = data; 466 467 hci_conn_put(conn_handle->conn); 468 kfree(conn_handle); 469 } 470 471 bool hci_setup_sync(struct hci_conn *conn, __u16 handle) 472 { 473 int result; 474 struct conn_handle_t *conn_handle; 475 476 if (enhanced_sync_conn_capable(conn->hdev)) { 477 conn_handle = kzalloc_obj(*conn_handle); 478 479 if (!conn_handle) 480 return false; 481 482 conn_handle->conn = hci_conn_get(conn); 483 conn_handle->handle = handle; 484 result = hci_cmd_sync_queue(conn->hdev, hci_enhanced_setup_sync, 485 conn_handle, 486 hci_enhanced_setup_sync_destroy); 487 if (result < 0) { 488 hci_conn_put(conn); 489 kfree(conn_handle); 490 } 491 492 return result == 0; 493 } 494 495 return hci_setup_sync_conn(conn, handle); 496 } 497 498 struct le_conn_update_data { 499 struct hci_conn *conn; 500 u16 min; 501 u16 max; 502 u16 latency; 503 u16 to_multiplier; 504 }; 505 506 static int le_conn_update_sync(struct hci_dev *hdev, void *data) 507 { 508 struct le_conn_update_data *d = data; 509 struct hci_conn *conn = d->conn; 510 struct hci_conn_params *params; 511 struct hci_cp_le_conn_update cp; 512 u16 timeout; 513 u8 store_hint; 514 int err; 515 516 /* Verify connection is still alive and read conn fields under 517 * the same lock to prevent a concurrent disconnect from freeing 518 * or reusing the connection while we build the HCI command. 519 */ 520 hci_dev_lock(hdev); 521 522 if (!hci_conn_valid(hdev, conn)) { 523 hci_dev_unlock(hdev); 524 return -ECANCELED; 525 } 526 527 memset(&cp, 0, sizeof(cp)); 528 cp.handle = cpu_to_le16(conn->handle); 529 cp.conn_interval_min = cpu_to_le16(d->min); 530 cp.conn_interval_max = cpu_to_le16(d->max); 531 cp.conn_latency = cpu_to_le16(d->latency); 532 cp.supervision_timeout = cpu_to_le16(d->to_multiplier); 533 cp.min_ce_len = cpu_to_le16(0x0000); 534 cp.max_ce_len = cpu_to_le16(0x0000); 535 timeout = conn->conn_timeout; 536 537 hci_dev_unlock(hdev); 538 539 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CONN_UPDATE, 540 sizeof(cp), &cp, 541 HCI_EV_LE_CONN_UPDATE_COMPLETE, 542 timeout, NULL); 543 if (err) 544 return err; 545 546 /* Update stored connection parameters after the controller has 547 * confirmed the update via the LE Connection Update Complete event. 548 */ 549 hci_dev_lock(hdev); 550 551 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); 552 if (params) { 553 params->conn_min_interval = d->min; 554 params->conn_max_interval = d->max; 555 params->conn_latency = d->latency; 556 params->supervision_timeout = d->to_multiplier; 557 store_hint = 0x01; 558 } else { 559 store_hint = 0x00; 560 } 561 562 hci_dev_unlock(hdev); 563 564 mgmt_new_conn_param(hdev, &conn->dst, conn->dst_type, store_hint, 565 d->min, d->max, d->latency, d->to_multiplier); 566 567 return 0; 568 } 569 570 static void le_conn_update_complete(struct hci_dev *hdev, void *data, int err) 571 { 572 struct le_conn_update_data *d = data; 573 574 hci_conn_put(d->conn); 575 kfree(d); 576 } 577 578 void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency, 579 u16 to_multiplier) 580 { 581 struct le_conn_update_data *d; 582 583 d = kzalloc_obj(*d); 584 if (!d) 585 return; 586 587 hci_conn_get(conn); 588 d->conn = conn; 589 d->min = min; 590 d->max = max; 591 d->latency = latency; 592 d->to_multiplier = to_multiplier; 593 594 if (hci_cmd_sync_queue(conn->hdev, le_conn_update_sync, d, 595 le_conn_update_complete) < 0) { 596 hci_conn_put(conn); 597 kfree(d); 598 } 599 } 600 601 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand, 602 __u8 ltk[16], __u8 key_size) 603 { 604 struct hci_dev *hdev = conn->hdev; 605 struct hci_cp_le_start_enc cp; 606 607 BT_DBG("hcon %p", conn); 608 609 memset(&cp, 0, sizeof(cp)); 610 611 cp.handle = cpu_to_le16(conn->handle); 612 cp.rand = rand; 613 cp.ediv = ediv; 614 memcpy(cp.ltk, ltk, key_size); 615 616 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); 617 } 618 619 /* Device _must_ be locked */ 620 void hci_sco_setup(struct hci_conn *conn, __u8 status) 621 { 622 struct hci_link *link; 623 624 link = list_first_entry_or_null(&conn->link_list, struct hci_link, list); 625 if (!link || !link->conn) 626 return; 627 628 BT_DBG("hcon %p", conn); 629 630 if (!status) { 631 if (lmp_esco_capable(conn->hdev)) 632 hci_setup_sync(link->conn, conn->handle); 633 else 634 hci_add_sco(link->conn, conn->handle); 635 } else { 636 hci_connect_cfm(link->conn, status); 637 hci_conn_del(link->conn); 638 } 639 } 640 641 static void hci_conn_timeout(struct work_struct *work) 642 { 643 struct hci_conn *conn = container_of(work, struct hci_conn, 644 disc_work.work); 645 int refcnt = atomic_read(&conn->refcnt); 646 647 BT_DBG("hcon %p state %s", conn, state_to_string(conn->state)); 648 649 WARN_ON(refcnt < 0); 650 651 /* FIXME: It was observed that in pairing failed scenario, refcnt 652 * drops below 0. Probably this is because l2cap_conn_del calls 653 * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is 654 * dropped. After that loop hci_chan_del is called which also drops 655 * conn. For now make sure that ACL is alive if refcnt is higher then 0, 656 * otherwise drop it. 657 */ 658 if (refcnt > 0) 659 return; 660 661 hci_abort_conn(conn, hci_proto_disconn_ind(conn)); 662 } 663 664 /* Enter sniff mode */ 665 static void hci_conn_idle(struct work_struct *work) 666 { 667 struct hci_conn *conn = container_of(work, struct hci_conn, 668 idle_work.work); 669 struct hci_dev *hdev = conn->hdev; 670 671 BT_DBG("hcon %p mode %d", conn, conn->mode); 672 673 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) 674 return; 675 676 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) 677 return; 678 679 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { 680 struct hci_cp_sniff_subrate cp; 681 cp.handle = cpu_to_le16(conn->handle); 682 cp.max_latency = cpu_to_le16(0); 683 cp.min_remote_timeout = cpu_to_le16(0); 684 cp.min_local_timeout = cpu_to_le16(0); 685 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); 686 } 687 688 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { 689 struct hci_cp_sniff_mode cp; 690 cp.handle = cpu_to_le16(conn->handle); 691 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); 692 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); 693 cp.attempt = cpu_to_le16(4); 694 cp.timeout = cpu_to_le16(1); 695 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); 696 } 697 } 698 699 static void hci_conn_auto_accept(struct work_struct *work) 700 { 701 struct hci_conn *conn = container_of(work, struct hci_conn, 702 auto_accept_work.work); 703 704 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), 705 &conn->dst); 706 } 707 708 static void le_disable_advertising(struct hci_dev *hdev) 709 { 710 if (ext_adv_capable(hdev)) { 711 struct hci_cp_le_set_ext_adv_enable cp; 712 713 cp.enable = 0x00; 714 cp.num_of_sets = 0x00; 715 716 hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp), 717 &cp); 718 } else { 719 u8 enable = 0x00; 720 hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), 721 &enable); 722 } 723 } 724 725 static void le_conn_timeout(struct work_struct *work) 726 { 727 struct hci_conn *conn = container_of(work, struct hci_conn, 728 le_conn_timeout.work); 729 struct hci_dev *hdev = conn->hdev; 730 731 BT_DBG(""); 732 733 /* We could end up here due to having done directed advertising, 734 * so clean up the state if necessary. This should however only 735 * happen with broken hardware or if low duty cycle was used 736 * (which doesn't have a timeout of its own). 737 */ 738 if (conn->role == HCI_ROLE_SLAVE) { 739 /* Disable LE Advertising */ 740 le_disable_advertising(hdev); 741 hci_dev_lock(hdev); 742 hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT); 743 hci_dev_unlock(hdev); 744 return; 745 } 746 747 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); 748 } 749 750 struct iso_list_data { 751 union { 752 u8 cig; 753 u8 big; 754 }; 755 union { 756 u8 cis; 757 u8 bis; 758 u16 sync_handle; 759 }; 760 int count; 761 bool big_term; 762 bool pa_sync_term; 763 bool big_sync_term; 764 }; 765 766 static void bis_list(struct hci_conn *conn, void *data) 767 { 768 struct iso_list_data *d = data; 769 770 /* Skip if not broadcast/ANY address */ 771 if (bacmp(&conn->dst, BDADDR_ANY)) 772 return; 773 774 if (d->big != conn->iso_qos.bcast.big || d->bis == BT_ISO_QOS_BIS_UNSET || 775 d->bis != conn->iso_qos.bcast.bis) 776 return; 777 778 d->count++; 779 } 780 781 static int terminate_big_sync(struct hci_dev *hdev, void *data) 782 { 783 struct iso_list_data *d = data; 784 785 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", d->big, d->bis); 786 787 hci_disable_per_advertising_sync(hdev, d->bis); 788 hci_remove_ext_adv_instance_sync(hdev, d->bis, NULL); 789 790 /* Only terminate BIG if it has been created */ 791 if (!d->big_term) 792 return 0; 793 794 return hci_le_terminate_big_sync(hdev, d->big, 795 HCI_ERROR_LOCAL_HOST_TERM); 796 } 797 798 static void terminate_big_destroy(struct hci_dev *hdev, void *data, int err) 799 { 800 kfree(data); 801 } 802 803 static int hci_le_terminate_big(struct hci_dev *hdev, struct hci_conn *conn) 804 { 805 struct iso_list_data *d; 806 int ret; 807 808 bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", conn->iso_qos.bcast.big, 809 conn->iso_qos.bcast.bis); 810 811 d = kzalloc_obj(*d); 812 if (!d) 813 return -ENOMEM; 814 815 d->big = conn->iso_qos.bcast.big; 816 d->bis = conn->iso_qos.bcast.bis; 817 d->big_term = test_and_clear_bit(HCI_CONN_BIG_CREATED, &conn->flags); 818 819 ret = hci_cmd_sync_queue(hdev, terminate_big_sync, d, 820 terminate_big_destroy); 821 if (ret) 822 kfree(d); 823 824 return ret; 825 } 826 827 static int big_terminate_sync(struct hci_dev *hdev, void *data) 828 { 829 struct iso_list_data *d = data; 830 831 bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", d->big, 832 d->sync_handle); 833 834 if (d->big_sync_term) 835 hci_le_big_terminate_sync(hdev, d->big); 836 837 if (d->pa_sync_term) 838 return hci_le_pa_terminate_sync(hdev, d->sync_handle); 839 840 return 0; 841 } 842 843 static void find_bis(struct hci_conn *conn, void *data) 844 { 845 struct iso_list_data *d = data; 846 847 /* Ignore if BIG doesn't match */ 848 if (d->big != conn->iso_qos.bcast.big) 849 return; 850 851 d->count++; 852 } 853 854 static int hci_le_big_terminate(struct hci_dev *hdev, struct hci_conn *conn) 855 { 856 struct iso_list_data *d; 857 int ret; 858 859 bt_dev_dbg(hdev, "hcon %p big 0x%2.2x sync_handle 0x%4.4x", conn, 860 conn->iso_qos.bcast.big, conn->sync_handle); 861 862 d = kzalloc_obj(*d); 863 if (!d) 864 return -ENOMEM; 865 866 d->big = conn->iso_qos.bcast.big; 867 d->sync_handle = conn->sync_handle; 868 869 if (conn->type == PA_LINK && 870 test_and_clear_bit(HCI_CONN_PA_SYNC, &conn->flags)) { 871 hci_conn_hash_list_flag(hdev, find_bis, PA_LINK, 872 HCI_CONN_PA_SYNC, d); 873 874 if (!d->count) 875 d->pa_sync_term = true; 876 877 d->count = 0; 878 } 879 880 if (test_and_clear_bit(HCI_CONN_BIG_SYNC, &conn->flags)) { 881 hci_conn_hash_list_flag(hdev, find_bis, BIS_LINK, 882 HCI_CONN_BIG_SYNC, d); 883 884 if (!d->count) 885 d->big_sync_term = true; 886 } 887 888 if (!d->pa_sync_term && !d->big_sync_term) { 889 kfree(d); 890 return 0; 891 } 892 893 ret = hci_cmd_sync_queue(hdev, big_terminate_sync, d, 894 terminate_big_destroy); 895 if (ret) 896 kfree(d); 897 898 return ret; 899 } 900 901 /* Cleanup BIS connection 902 * 903 * Detects if there any BIS left connected in a BIG 904 * broadcaster: Remove advertising instance and terminate BIG. 905 * broadcaster receiver: Terminate BIG sync and terminate PA sync. 906 */ 907 static void bis_cleanup(struct hci_conn *conn) 908 { 909 struct hci_dev *hdev = conn->hdev; 910 struct hci_conn *bis; 911 912 bt_dev_dbg(hdev, "conn %p", conn); 913 914 if (conn->role == HCI_ROLE_MASTER) { 915 if (!test_and_clear_bit(HCI_CONN_PER_ADV, &conn->flags)) 916 return; 917 918 /* Check if ISO connection is a BIS and terminate advertising 919 * set and BIG if there are no other connections using it. 920 */ 921 bis = hci_conn_hash_lookup_big_state(hdev, 922 conn->iso_qos.bcast.big, 923 BT_CONNECTED, 924 HCI_ROLE_MASTER); 925 if (bis) 926 return; 927 928 bis = hci_conn_hash_lookup_big_state(hdev, 929 conn->iso_qos.bcast.big, 930 BT_CONNECT, 931 HCI_ROLE_MASTER); 932 if (bis) 933 return; 934 935 bis = hci_conn_hash_lookup_big_state(hdev, 936 conn->iso_qos.bcast.big, 937 BT_OPEN, 938 HCI_ROLE_MASTER); 939 if (bis) 940 return; 941 942 hci_le_terminate_big(hdev, conn); 943 } else { 944 hci_le_big_terminate(hdev, conn); 945 } 946 } 947 948 static int remove_cig_sync(struct hci_dev *hdev, void *data) 949 { 950 u8 handle = PTR_UINT(data); 951 952 return hci_le_remove_cig_sync(hdev, handle); 953 } 954 955 static int hci_le_remove_cig(struct hci_dev *hdev, u8 handle) 956 { 957 bt_dev_dbg(hdev, "handle 0x%2.2x", handle); 958 959 return hci_cmd_sync_queue(hdev, remove_cig_sync, UINT_PTR(handle), 960 NULL); 961 } 962 963 static void find_cis(struct hci_conn *conn, void *data) 964 { 965 struct iso_list_data *d = data; 966 967 /* Ignore broadcast or if CIG don't match */ 968 if (!bacmp(&conn->dst, BDADDR_ANY) || d->cig != conn->iso_qos.ucast.cig) 969 return; 970 971 d->count++; 972 } 973 974 /* Cleanup CIS connection: 975 * 976 * Detects if there any CIS left connected in a CIG and remove it. 977 */ 978 static void cis_cleanup(struct hci_conn *conn) 979 { 980 struct hci_dev *hdev = conn->hdev; 981 struct iso_list_data d; 982 983 if (conn->iso_qos.ucast.cig == BT_ISO_QOS_CIG_UNSET) 984 return; 985 986 memset(&d, 0, sizeof(d)); 987 d.cig = conn->iso_qos.ucast.cig; 988 989 /* Check if ISO connection is a CIS and remove CIG if there are 990 * no other connections using it. 991 */ 992 hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_BOUND, &d); 993 hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_CONNECT, 994 &d); 995 hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_CONNECTED, 996 &d); 997 if (d.count) 998 return; 999 1000 hci_le_remove_cig(hdev, conn->iso_qos.ucast.cig); 1001 } 1002 1003 static int hci_conn_hash_alloc_unset(struct hci_dev *hdev) 1004 { 1005 return ida_alloc_range(&hdev->unset_handle_ida, HCI_CONN_HANDLE_MAX + 1, 1006 U16_MAX, GFP_ATOMIC); 1007 } 1008 1009 static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type, 1010 bdaddr_t *dst, u8 dst_type, 1011 u8 role, u16 handle) 1012 { 1013 struct hci_conn *conn; 1014 struct smp_irk *irk = NULL; 1015 1016 switch (type) { 1017 case ACL_LINK: 1018 if (!hdev->acl_mtu) 1019 return ERR_PTR(-ECONNREFUSED); 1020 break; 1021 case CIS_LINK: 1022 case BIS_LINK: 1023 case PA_LINK: 1024 if (!hdev->iso_mtu) 1025 return ERR_PTR(-ECONNREFUSED); 1026 irk = hci_get_irk(hdev, dst, dst_type); 1027 break; 1028 case LE_LINK: 1029 if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU) 1030 return ERR_PTR(-ECONNREFUSED); 1031 if (!hdev->le_mtu && hdev->acl_mtu < HCI_MIN_LE_MTU) 1032 return ERR_PTR(-ECONNREFUSED); 1033 irk = hci_get_irk(hdev, dst, dst_type); 1034 /* An identity address only reaches a peer advertising an RPA 1035 * if the controller translates it. Unless address resolution 1036 * is enabled and this peer is programmed into the resolving 1037 * list, keep the RPA the peer is on air with; 1038 * le_conn_complete_evt() resolves it back once the link is 1039 * up. 1040 */ 1041 if (irk && 1042 (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION) || 1043 !hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, 1044 &irk->bdaddr, 1045 irk->addr_type))) 1046 irk = NULL; 1047 break; 1048 case SCO_LINK: 1049 case ESCO_LINK: 1050 if (!hdev->sco_pkts) 1051 /* Controller does not support SCO or eSCO over HCI */ 1052 return ERR_PTR(-ECONNREFUSED); 1053 break; 1054 default: 1055 return ERR_PTR(-ECONNREFUSED); 1056 } 1057 1058 bt_dev_dbg(hdev, "dst %pMR handle 0x%4.4x", dst, handle); 1059 1060 conn = kzalloc_obj(*conn); 1061 if (!conn) 1062 return ERR_PTR(-ENOMEM); 1063 1064 /* If and IRK exists use its identity address */ 1065 if (!irk) { 1066 bacpy(&conn->dst, dst); 1067 conn->dst_type = dst_type; 1068 } else { 1069 bacpy(&conn->dst, &irk->bdaddr); 1070 conn->dst_type = irk->addr_type; 1071 } 1072 1073 bacpy(&conn->src, &hdev->bdaddr); 1074 conn->handle = handle; 1075 conn->hdev = hdev; 1076 conn->type = type; 1077 conn->role = role; 1078 conn->mode = HCI_CM_ACTIVE; 1079 conn->state = BT_OPEN; 1080 conn->auth_type = HCI_AT_GENERAL_BONDING; 1081 conn->io_capability = hdev->io_capability; 1082 conn->remote_auth = 0xff; 1083 conn->key_type = 0xff; 1084 conn->rssi = HCI_RSSI_INVALID; 1085 conn->tx_power = HCI_TX_POWER_INVALID; 1086 conn->max_tx_power = HCI_TX_POWER_INVALID; 1087 conn->sync_handle = HCI_SYNC_HANDLE_INVALID; 1088 conn->sid = HCI_SID_INVALID; 1089 1090 set_bit(HCI_CONN_POWER_SAVE, &conn->flags); 1091 conn->disc_timeout = HCI_DISCONN_TIMEOUT; 1092 1093 /* Set Default Authenticated payload timeout to 30s */ 1094 conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT; 1095 1096 if (conn->role == HCI_ROLE_MASTER) 1097 conn->out = true; 1098 1099 switch (type) { 1100 case ACL_LINK: 1101 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; 1102 conn->link_policy = hdev->link_policy; 1103 conn->mtu = hdev->acl_mtu; 1104 break; 1105 case LE_LINK: 1106 /* conn->src should reflect the local identity address */ 1107 hci_copy_identity_address(hdev, &conn->src, &conn->src_type); 1108 conn->mtu = hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu; 1109 /* Use the controller supported PHYS as default until the 1110 * remote features are resolved. 1111 */ 1112 conn->le_tx_def_phys = hdev->le_tx_def_phys; 1113 conn->le_rx_def_phys = hdev->le_tx_def_phys; 1114 break; 1115 case CIS_LINK: 1116 /* conn->src should reflect the local identity address */ 1117 hci_copy_identity_address(hdev, &conn->src, &conn->src_type); 1118 1119 if (conn->role == HCI_ROLE_MASTER) 1120 conn->cleanup = cis_cleanup; 1121 1122 conn->mtu = hdev->iso_mtu; 1123 break; 1124 case PA_LINK: 1125 case BIS_LINK: 1126 /* conn->src should reflect the local identity address */ 1127 hci_copy_identity_address(hdev, &conn->src, &conn->src_type); 1128 conn->cleanup = bis_cleanup; 1129 conn->mtu = hdev->iso_mtu; 1130 break; 1131 case SCO_LINK: 1132 if (lmp_esco_capable(hdev)) 1133 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | 1134 (hdev->esco_type & EDR_ESCO_MASK); 1135 else 1136 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; 1137 1138 conn->mtu = hdev->sco_mtu; 1139 break; 1140 case ESCO_LINK: 1141 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; 1142 conn->mtu = hdev->sco_mtu; 1143 break; 1144 } 1145 1146 skb_queue_head_init(&conn->data_q); 1147 skb_queue_head_init(&conn->tx_q.queue); 1148 1149 INIT_LIST_HEAD(&conn->chan_list); 1150 INIT_LIST_HEAD(&conn->link_list); 1151 1152 INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout); 1153 INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept); 1154 INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle); 1155 INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout); 1156 1157 spin_lock_init(&conn->proto_lock); 1158 1159 atomic_set(&conn->refcnt, 0); 1160 1161 hci_dev_hold(hdev); 1162 1163 hci_conn_hash_add(hdev, conn); 1164 1165 /* The SCO and eSCO connections will only be notified when their 1166 * setup has been completed. This is different to ACL links which 1167 * can be notified right away. 1168 */ 1169 if (conn->type != SCO_LINK && conn->type != ESCO_LINK) { 1170 if (hdev->notify) 1171 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); 1172 } 1173 1174 hci_conn_init_sysfs(conn); 1175 return conn; 1176 } 1177 1178 struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type, 1179 bdaddr_t *dst, u8 dst_type, u8 role) 1180 { 1181 int handle; 1182 1183 bt_dev_dbg(hdev, "dst %pMR", dst); 1184 1185 handle = hci_conn_hash_alloc_unset(hdev); 1186 if (unlikely(handle < 0)) 1187 return ERR_PTR(-ECONNREFUSED); 1188 1189 return __hci_conn_add(hdev, type, dst, dst_type, role, handle); 1190 } 1191 1192 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst, 1193 u8 dst_type, u8 role, u16 handle) 1194 { 1195 if (handle > HCI_CONN_HANDLE_MAX) 1196 return ERR_PTR(-EINVAL); 1197 1198 return __hci_conn_add(hdev, type, dst, dst_type, role, handle); 1199 } 1200 1201 static void hci_conn_cleanup_child(struct hci_conn *conn, u8 reason) 1202 { 1203 if (!reason) 1204 reason = HCI_ERROR_REMOTE_USER_TERM; 1205 1206 /* Due to race, SCO/ISO conn might be not established yet at this point, 1207 * and nothing else will clean it up. In other cases it is done via HCI 1208 * events. 1209 */ 1210 switch (conn->type) { 1211 case SCO_LINK: 1212 case ESCO_LINK: 1213 if (HCI_CONN_HANDLE_UNSET(conn->handle)) 1214 hci_conn_failed(conn, reason); 1215 break; 1216 case CIS_LINK: 1217 case BIS_LINK: 1218 case PA_LINK: 1219 if ((conn->state != BT_CONNECTED && 1220 !test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) || 1221 test_bit(HCI_CONN_BIG_CREATED, &conn->flags)) 1222 hci_conn_failed(conn, reason); 1223 break; 1224 } 1225 } 1226 1227 static void hci_conn_unlink(struct hci_conn *conn) 1228 { 1229 struct hci_dev *hdev = conn->hdev; 1230 1231 bt_dev_dbg(hdev, "hcon %p", conn); 1232 1233 if (!conn->parent) { 1234 struct hci_link *link, *t; 1235 1236 conn->sec_level = BT_SECURITY_SDP; 1237 clear_bit(HCI_CONN_ENCRYPT, &conn->flags); 1238 clear_bit(HCI_CONN_AES_CCM, &conn->flags); 1239 /* mgmt_security_level_changed() reports the reset only if 1240 * this connection was actually reported to userspace as 1241 * connected (it checks HCI_CONN_MGMT_CONNECTED itself), so it 1242 * must be called before the flag is cleared below. 1243 * Callers that already know whether the connection was 1244 * mgmt-connected (e.g. disconnect event handlers) only peek 1245 * at the flag, so it is still set here; this is the place 1246 * actually clearing it. 1247 * 1248 * Skip the report if hdev is down: this means 1249 * hci_dev_close_sync()/hci_conn_hash_flush() is in progress 1250 * and no MGMT_EV_DEVICE_DISCONNECTED was ever sent for this 1251 * connection, so sending a security level reset here would 1252 * be a spurious, order-less event; the adapter-wide powered 1253 * off notification is enough for userspace to reset state. 1254 */ 1255 if (test_bit(HCI_UP, &hdev->flags)) 1256 mgmt_security_level_changed(conn); 1257 clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags); 1258 1259 list_for_each_entry_safe(link, t, &conn->link_list, list) { 1260 struct hci_conn *child = link->conn; 1261 1262 hci_conn_unlink(child); 1263 1264 /* If hdev is down it means 1265 * hci_dev_close_sync/hci_conn_hash_flush is in progress 1266 * and links don't need to be cleanup as all connections 1267 * would be cleanup. 1268 */ 1269 if (!test_bit(HCI_UP, &hdev->flags)) 1270 continue; 1271 1272 hci_conn_cleanup_child(child, conn->abort_reason); 1273 } 1274 1275 return; 1276 } 1277 1278 if (!conn->link) 1279 return; 1280 1281 list_del_rcu(&conn->link->list); 1282 synchronize_rcu(); 1283 1284 hci_conn_drop(conn->parent); 1285 hci_conn_put(conn->parent); 1286 conn->parent = NULL; 1287 1288 kfree(conn->link); 1289 conn->link = NULL; 1290 } 1291 1292 void hci_conn_del(struct hci_conn *conn) 1293 { 1294 struct hci_dev *hdev = conn->hdev; 1295 1296 BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle); 1297 1298 hci_conn_unlink(conn); 1299 1300 disable_delayed_work_sync(&conn->disc_work); 1301 disable_delayed_work_sync(&conn->auto_accept_work); 1302 disable_delayed_work_sync(&conn->idle_work); 1303 1304 /* Remove the connection from the list so unacked logic can detect when 1305 * a certain pool is not being utilized. 1306 */ 1307 hci_conn_hash_del(hdev, conn); 1308 1309 /* Handle unacked frames: 1310 * 1311 * - In case there are no connection, or if restoring the buffers 1312 * considered in transist would overflow, restore all buffers to the 1313 * pool. 1314 * - Otherwise restore just the buffers considered in transit for the 1315 * hci_conn 1316 */ 1317 switch (conn->type) { 1318 case ACL_LINK: 1319 if (!hci_conn_num(hdev, ACL_LINK) || 1320 hdev->acl_cnt + conn->sent > hdev->acl_pkts) 1321 hdev->acl_cnt = hdev->acl_pkts; 1322 else 1323 hdev->acl_cnt += conn->sent; 1324 break; 1325 case LE_LINK: 1326 cancel_delayed_work(&conn->le_conn_timeout); 1327 1328 if (hdev->le_pkts) { 1329 if (!hci_conn_num(hdev, LE_LINK) || 1330 hdev->le_cnt + conn->sent > hdev->le_pkts) 1331 hdev->le_cnt = hdev->le_pkts; 1332 else 1333 hdev->le_cnt += conn->sent; 1334 } else { 1335 if ((!hci_conn_num(hdev, LE_LINK) && 1336 !hci_conn_num(hdev, ACL_LINK)) || 1337 hdev->acl_cnt + conn->sent > hdev->acl_pkts) 1338 hdev->acl_cnt = hdev->acl_pkts; 1339 else 1340 hdev->acl_cnt += conn->sent; 1341 } 1342 break; 1343 case CIS_LINK: 1344 case BIS_LINK: 1345 case PA_LINK: 1346 if (!hci_iso_count(hdev) || 1347 hdev->iso_cnt + conn->sent > hdev->iso_pkts) 1348 hdev->iso_cnt = hdev->iso_pkts; 1349 else 1350 hdev->iso_cnt += conn->sent; 1351 break; 1352 } 1353 1354 skb_queue_purge(&conn->data_q); 1355 skb_queue_purge(&conn->tx_q.queue); 1356 1357 /* Remove the connection from the list and cleanup its remaining 1358 * state. This is a separate function since for some cases like 1359 * BT_CONNECT_SCAN we *only* want the cleanup part without the 1360 * rest of hci_conn_del. 1361 */ 1362 hci_conn_cleanup(conn); 1363 1364 /* Dequeue callbacks using connection pointer as data */ 1365 hci_cmd_sync_dequeue(hdev, NULL, conn, NULL); 1366 } 1367 1368 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type) 1369 { 1370 int use_src = bacmp(src, BDADDR_ANY); 1371 struct hci_dev *hdev = NULL, *d; 1372 1373 BT_DBG("%pMR -> %pMR", src, dst); 1374 1375 read_lock(&hci_dev_list_lock); 1376 1377 list_for_each_entry(d, &hci_dev_list, list) { 1378 if (!test_bit(HCI_UP, &d->flags) || 1379 hci_dev_test_flag(d, HCI_USER_CHANNEL)) 1380 continue; 1381 1382 /* Simple routing: 1383 * No source address - find interface with bdaddr != dst 1384 * Source address - find interface with bdaddr == src 1385 */ 1386 1387 if (use_src) { 1388 bdaddr_t id_addr; 1389 u8 id_addr_type; 1390 1391 if (src_type == BDADDR_BREDR) { 1392 if (!lmp_bredr_capable(d)) 1393 continue; 1394 bacpy(&id_addr, &d->bdaddr); 1395 id_addr_type = BDADDR_BREDR; 1396 } else { 1397 if (!lmp_le_capable(d)) 1398 continue; 1399 1400 hci_copy_identity_address(d, &id_addr, 1401 &id_addr_type); 1402 1403 /* Convert from HCI to three-value type */ 1404 if (id_addr_type == ADDR_LE_DEV_PUBLIC) 1405 id_addr_type = BDADDR_LE_PUBLIC; 1406 else 1407 id_addr_type = BDADDR_LE_RANDOM; 1408 } 1409 1410 if (!bacmp(&id_addr, src) && id_addr_type == src_type) { 1411 hdev = d; break; 1412 } 1413 } else { 1414 if (bacmp(&d->bdaddr, dst)) { 1415 hdev = d; break; 1416 } 1417 } 1418 } 1419 1420 if (hdev) 1421 hdev = hci_dev_hold(hdev); 1422 1423 read_unlock(&hci_dev_list_lock); 1424 return hdev; 1425 } 1426 EXPORT_SYMBOL(hci_get_route); 1427 1428 /* This function requires the caller holds hdev->lock */ 1429 static void hci_le_conn_failed(struct hci_conn *conn, u8 status) 1430 { 1431 struct hci_dev *hdev = conn->hdev; 1432 1433 hci_connect_le_scan_cleanup(conn, status); 1434 1435 /* Enable advertising in case this was a failed connection 1436 * attempt as a peripheral. 1437 */ 1438 if (conn->role == HCI_ROLE_SLAVE) 1439 hci_enable_advertising(hdev); 1440 } 1441 1442 /* This function requires the caller holds hdev->lock */ 1443 void hci_conn_failed(struct hci_conn *conn, u8 status) 1444 { 1445 struct hci_dev *hdev = conn->hdev; 1446 1447 bt_dev_dbg(hdev, "status 0x%2.2x", status); 1448 1449 switch (conn->type) { 1450 case LE_LINK: 1451 hci_le_conn_failed(conn, status); 1452 break; 1453 case ACL_LINK: 1454 mgmt_connect_failed(hdev, conn, status); 1455 break; 1456 } 1457 1458 /* In case of BIG/PA sync failed, clear conn flags so that 1459 * the conns will be correctly cleaned up by ISO layer 1460 */ 1461 test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags); 1462 test_and_clear_bit(HCI_CONN_PA_SYNC_FAILED, &conn->flags); 1463 1464 conn->state = BT_CLOSED; 1465 hci_connect_cfm(conn, status); 1466 hci_conn_del(conn); 1467 } 1468 1469 /* This function requires the caller holds hdev->lock */ 1470 u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle) 1471 { 1472 struct hci_dev *hdev = conn->hdev; 1473 1474 bt_dev_dbg(hdev, "hcon %p handle 0x%4.4x", conn, handle); 1475 1476 if (conn->handle == handle) 1477 return 0; 1478 1479 if (handle > HCI_CONN_HANDLE_MAX) { 1480 bt_dev_err(hdev, "Invalid handle: 0x%4.4x > 0x%4.4x", 1481 handle, HCI_CONN_HANDLE_MAX); 1482 return HCI_ERROR_INVALID_PARAMETERS; 1483 } 1484 1485 /* If abort_reason has been sent it means the connection is being 1486 * aborted and the handle shall not be changed. 1487 */ 1488 if (conn->abort_reason) 1489 return conn->abort_reason; 1490 1491 if (HCI_CONN_HANDLE_UNSET(conn->handle)) 1492 ida_free(&hdev->unset_handle_ida, conn->handle); 1493 1494 conn->handle = handle; 1495 1496 return 0; 1497 } 1498 1499 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, 1500 u8 dst_type, bool dst_resolved, u8 sec_level, 1501 u16 conn_timeout, u8 role, u8 phy, u8 sec_phy) 1502 { 1503 struct hci_conn *conn; 1504 struct smp_irk *irk; 1505 int err; 1506 1507 /* Let's make sure that le is enabled.*/ 1508 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 1509 if (lmp_le_capable(hdev)) 1510 return ERR_PTR(-ECONNREFUSED); 1511 1512 return ERR_PTR(-EOPNOTSUPP); 1513 } 1514 1515 /* Since the controller supports only one LE connection attempt at a 1516 * time, we return -EBUSY if there is any connection attempt running. 1517 */ 1518 if (hci_lookup_le_connect(hdev)) 1519 return ERR_PTR(-EBUSY); 1520 1521 /* If there's already a connection object but it's not in 1522 * scanning state it means it must already be established, in 1523 * which case we can't do anything else except report a failure 1524 * to connect. 1525 */ 1526 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type); 1527 if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) { 1528 return ERR_PTR(-EBUSY); 1529 } 1530 1531 /* Check if the destination address has been resolved by the controller 1532 * since if it did then the identity address shall be used. 1533 */ 1534 if (!dst_resolved) { 1535 /* When given an identity address with existing identity 1536 * resolving key, the connection needs to be established 1537 * to a resolvable random address. 1538 * 1539 * Storing the resolvable random address is required here 1540 * to handle connection failures. The address will later 1541 * be resolved back into the original identity address 1542 * from the connect request. 1543 */ 1544 irk = hci_find_irk_by_addr(hdev, dst, dst_type); 1545 if (irk && bacmp(&irk->rpa, BDADDR_ANY)) { 1546 dst = &irk->rpa; 1547 dst_type = ADDR_LE_DEV_RANDOM; 1548 } 1549 } 1550 1551 if (conn) { 1552 /* dst may just have been swapped for the peer's RPA above, and 1553 * dst_type describes dst -- it has to travel with it. Leaving 1554 * the identity type behind makes the pair describe a peer that 1555 * does not exist, and nothing downstream repairs it: 1556 * hci_bdaddr_is_rpa() tests the type before the address, so 1557 * the RPA is never treated as one. 1558 */ 1559 bacpy(&conn->dst, dst); 1560 conn->dst_type = dst_type; 1561 } else { 1562 conn = hci_conn_add_unset(hdev, LE_LINK, dst, dst_type, role); 1563 if (IS_ERR(conn)) 1564 return conn; 1565 hci_conn_hold(conn); 1566 conn->pending_sec_level = sec_level; 1567 } 1568 1569 /* Do not report the security level here: conn->dst may still hold 1570 * the peer's RPA instead of its identity address, and the connection 1571 * is not established yet. le_conn_complete_evt() reports it once the 1572 * connection completes and the identity address has been resolved. 1573 */ 1574 conn->sec_level = BT_SECURITY_LOW; 1575 conn->conn_timeout = conn_timeout; 1576 conn->le_adv_phy = phy; 1577 conn->le_adv_sec_phy = sec_phy; 1578 1579 err = hci_connect_le_sync(hdev, conn); 1580 if (err) { 1581 hci_conn_del(conn); 1582 return ERR_PTR(err); 1583 } 1584 1585 return conn; 1586 } 1587 1588 static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) 1589 { 1590 struct hci_conn *conn; 1591 1592 conn = hci_conn_hash_lookup_le(hdev, addr, type); 1593 if (!conn) 1594 return false; 1595 1596 if (conn->state != BT_CONNECTED) 1597 return false; 1598 1599 return true; 1600 } 1601 1602 /* This function requires the caller holds hdev->lock */ 1603 static int hci_explicit_conn_params_set(struct hci_dev *hdev, 1604 bdaddr_t *addr, u8 addr_type) 1605 { 1606 struct hci_conn_params *params; 1607 1608 if (is_connected(hdev, addr, addr_type)) 1609 return -EISCONN; 1610 1611 params = hci_conn_params_lookup(hdev, addr, addr_type); 1612 if (!params) { 1613 params = hci_conn_params_add(hdev, addr, addr_type); 1614 if (!params) 1615 return -ENOMEM; 1616 1617 /* If we created new params, mark them to be deleted in 1618 * hci_connect_le_scan_cleanup. It's different case than 1619 * existing disabled params, those will stay after cleanup. 1620 */ 1621 params->auto_connect = HCI_AUTO_CONN_EXPLICIT; 1622 } 1623 1624 /* We're trying to connect, so make sure params are at pend_le_conns */ 1625 if (params->auto_connect == HCI_AUTO_CONN_DISABLED || 1626 params->auto_connect == HCI_AUTO_CONN_REPORT || 1627 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { 1628 hci_pend_le_list_del_init(params); 1629 hci_pend_le_list_add(params, &hdev->pend_le_conns); 1630 } 1631 1632 params->explicit_connect = true; 1633 1634 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, 1635 params->auto_connect); 1636 1637 return 0; 1638 } 1639 1640 static int qos_set_big(struct hci_dev *hdev, struct bt_iso_qos *qos) 1641 { 1642 struct hci_conn *conn; 1643 u8 big; 1644 1645 /* Allocate a BIG if not set */ 1646 if (qos->bcast.big == BT_ISO_QOS_BIG_UNSET) { 1647 for (big = 0x00; big < 0xef; big++) { 1648 1649 conn = hci_conn_hash_lookup_big(hdev, big); 1650 if (!conn) 1651 break; 1652 } 1653 1654 if (big == 0xef) 1655 return -EADDRNOTAVAIL; 1656 1657 /* Update BIG */ 1658 qos->bcast.big = big; 1659 } 1660 1661 return 0; 1662 } 1663 1664 static int qos_set_bis(struct hci_dev *hdev, struct bt_iso_qos *qos) 1665 { 1666 struct hci_conn *conn; 1667 u8 bis; 1668 1669 /* Allocate BIS if not set */ 1670 if (qos->bcast.bis == BT_ISO_QOS_BIS_UNSET) { 1671 if (qos->bcast.big != BT_ISO_QOS_BIG_UNSET) { 1672 conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big); 1673 1674 if (conn) { 1675 /* If the BIG handle is already matched to an advertising 1676 * handle, do not allocate a new one. 1677 */ 1678 qos->bcast.bis = conn->iso_qos.bcast.bis; 1679 return 0; 1680 } 1681 } 1682 1683 /* Find an unused adv set to advertise BIS, skip instance 0x00 1684 * since it is reserved as general purpose set. 1685 */ 1686 for (bis = 0x01; bis < hdev->le_num_of_adv_sets; 1687 bis++) { 1688 1689 conn = hci_conn_hash_lookup_bis(hdev, BDADDR_ANY, bis); 1690 if (!conn) 1691 break; 1692 } 1693 1694 if (bis == hdev->le_num_of_adv_sets) 1695 return -EADDRNOTAVAIL; 1696 1697 /* Update BIS */ 1698 qos->bcast.bis = bis; 1699 } 1700 1701 return 0; 1702 } 1703 1704 /* This function requires the caller holds hdev->lock */ 1705 static struct hci_conn *hci_add_bis(struct hci_dev *hdev, bdaddr_t *dst, 1706 __u8 sid, struct bt_iso_qos *qos, 1707 __u8 base_len, __u8 *base, u16 timeout) 1708 { 1709 struct hci_conn *conn; 1710 int err; 1711 1712 /* Let's make sure that le is enabled.*/ 1713 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 1714 if (lmp_le_capable(hdev)) 1715 return ERR_PTR(-ECONNREFUSED); 1716 return ERR_PTR(-EOPNOTSUPP); 1717 } 1718 1719 err = qos_set_big(hdev, qos); 1720 if (err) 1721 return ERR_PTR(err); 1722 1723 err = qos_set_bis(hdev, qos); 1724 if (err) 1725 return ERR_PTR(err); 1726 1727 /* Check if the LE Create BIG command has already been sent */ 1728 conn = hci_conn_hash_lookup_per_adv_bis(hdev, dst, qos->bcast.big, 1729 qos->bcast.big); 1730 if (conn) 1731 return ERR_PTR(-EADDRINUSE); 1732 1733 /* Check BIS settings against other bound BISes, since all 1734 * BISes in a BIG must have the same value for all parameters 1735 */ 1736 conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big); 1737 1738 if (conn && (memcmp(qos, &conn->iso_qos, sizeof(*qos)) || 1739 base_len != conn->le_per_adv_data_len || 1740 memcmp(conn->le_per_adv_data, base, base_len))) 1741 return ERR_PTR(-EADDRINUSE); 1742 1743 conn = hci_conn_add_unset(hdev, BIS_LINK, dst, 0, HCI_ROLE_MASTER); 1744 if (IS_ERR(conn)) 1745 return conn; 1746 1747 conn->state = BT_CONNECT; 1748 conn->sid = sid; 1749 conn->conn_timeout = timeout; 1750 1751 hci_conn_hold(conn); 1752 return conn; 1753 } 1754 1755 /* This function requires the caller holds hdev->lock */ 1756 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst, 1757 u8 dst_type, u8 sec_level, 1758 u16 conn_timeout, 1759 enum conn_reasons conn_reason) 1760 { 1761 struct hci_conn *conn; 1762 1763 /* Let's make sure that le is enabled.*/ 1764 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 1765 if (lmp_le_capable(hdev)) 1766 return ERR_PTR(-ECONNREFUSED); 1767 1768 return ERR_PTR(-EOPNOTSUPP); 1769 } 1770 1771 /* Some devices send ATT messages as soon as the physical link is 1772 * established. To be able to handle these ATT messages, the user- 1773 * space first establishes the connection and then starts the pairing 1774 * process. 1775 * 1776 * So if a hci_conn object already exists for the following connection 1777 * attempt, we simply update pending_sec_level and auth_type fields 1778 * and return the object found. 1779 */ 1780 conn = hci_conn_hash_lookup_le(hdev, dst, dst_type); 1781 if (conn) { 1782 if (conn->pending_sec_level < sec_level) 1783 conn->pending_sec_level = sec_level; 1784 goto done; 1785 } 1786 1787 BT_DBG("requesting refresh of dst_addr"); 1788 1789 conn = hci_conn_add_unset(hdev, LE_LINK, dst, dst_type, 1790 HCI_ROLE_MASTER); 1791 if (IS_ERR(conn)) 1792 return conn; 1793 1794 if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) { 1795 hci_conn_del(conn); 1796 return ERR_PTR(-EBUSY); 1797 } 1798 1799 conn->state = BT_CONNECT; 1800 set_bit(HCI_CONN_SCANNING, &conn->flags); 1801 conn->sec_level = BT_SECURITY_LOW; 1802 conn->pending_sec_level = sec_level; 1803 conn->conn_timeout = conn_timeout; 1804 conn->conn_reason = conn_reason; 1805 1806 hci_update_passive_scan(hdev); 1807 1808 done: 1809 hci_conn_hold(conn); 1810 return conn; 1811 } 1812 1813 struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst, 1814 u8 sec_level, u8 auth_type, 1815 enum conn_reasons conn_reason, u16 timeout) 1816 { 1817 struct hci_conn *acl; 1818 1819 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 1820 if (lmp_bredr_capable(hdev)) 1821 return ERR_PTR(-ECONNREFUSED); 1822 1823 return ERR_PTR(-EOPNOTSUPP); 1824 } 1825 1826 /* Reject outgoing connection to device with same BD ADDR against 1827 * CVE-2020-26555 1828 */ 1829 if (!bacmp(&hdev->bdaddr, dst)) { 1830 bt_dev_dbg(hdev, "Reject connection with same BD_ADDR %pMR\n", 1831 dst); 1832 return ERR_PTR(-ECONNREFUSED); 1833 } 1834 1835 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); 1836 if (!acl) { 1837 acl = hci_conn_add_unset(hdev, ACL_LINK, dst, 0, 1838 HCI_ROLE_MASTER); 1839 if (IS_ERR(acl)) 1840 return acl; 1841 } 1842 1843 hci_conn_hold(acl); 1844 1845 acl->conn_reason = conn_reason; 1846 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { 1847 int err; 1848 1849 acl->sec_level = BT_SECURITY_LOW; 1850 acl->pending_sec_level = sec_level; 1851 acl->auth_type = auth_type; 1852 acl->conn_timeout = timeout; 1853 1854 err = hci_connect_acl_sync(hdev, acl); 1855 if (err) { 1856 hci_conn_del(acl); 1857 return ERR_PTR(err); 1858 } 1859 } 1860 1861 return acl; 1862 } 1863 1864 static struct hci_link *hci_conn_link(struct hci_conn *parent, 1865 struct hci_conn *conn) 1866 { 1867 struct hci_dev *hdev = parent->hdev; 1868 struct hci_link *link; 1869 1870 bt_dev_dbg(hdev, "parent %p hcon %p", parent, conn); 1871 1872 if (conn->link) 1873 return conn->link; 1874 1875 if (conn->parent) 1876 return NULL; 1877 1878 link = kzalloc_obj(*link); 1879 if (!link) 1880 return NULL; 1881 1882 link->conn = hci_conn_hold(conn); 1883 conn->link = link; 1884 conn->parent = hci_conn_get(parent); 1885 1886 /* Use list_add_tail_rcu append to the list */ 1887 list_add_tail_rcu(&link->list, &parent->link_list); 1888 1889 return link; 1890 } 1891 1892 struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst, 1893 __u16 setting, struct bt_codec *codec, 1894 u16 timeout) 1895 { 1896 struct hci_conn *acl; 1897 struct hci_conn *sco; 1898 struct hci_link *link; 1899 1900 acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING, 1901 CONN_REASON_SCO_CONNECT, timeout); 1902 if (IS_ERR(acl)) 1903 return acl; 1904 1905 sco = hci_conn_hash_lookup_ba(hdev, type, dst); 1906 if (!sco) { 1907 sco = hci_conn_add_unset(hdev, type, dst, 0, HCI_ROLE_MASTER); 1908 if (IS_ERR(sco)) { 1909 hci_conn_drop(acl); 1910 return sco; 1911 } 1912 } 1913 1914 link = hci_conn_link(acl, sco); 1915 if (!link) { 1916 hci_conn_drop(acl); 1917 hci_conn_drop(sco); 1918 return ERR_PTR(-ENOLINK); 1919 } 1920 1921 sco->setting = setting; 1922 sco->codec = *codec; 1923 1924 if (acl->state == BT_CONNECTED && 1925 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { 1926 set_bit(HCI_CONN_POWER_SAVE, &acl->flags); 1927 hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON); 1928 1929 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) { 1930 /* defer SCO setup until mode change completed */ 1931 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags); 1932 return sco; 1933 } 1934 1935 hci_sco_setup(acl, 0x00); 1936 } 1937 1938 return sco; 1939 } 1940 1941 static int hci_le_create_big(struct hci_conn *conn, struct bt_iso_qos *qos) 1942 { 1943 struct hci_dev *hdev = conn->hdev; 1944 struct hci_cp_le_create_big cp; 1945 struct iso_list_data data; 1946 1947 memset(&cp, 0, sizeof(cp)); 1948 1949 data.big = qos->bcast.big; 1950 data.bis = qos->bcast.bis; 1951 data.count = 0; 1952 1953 /* Create a BIS for each bound connection */ 1954 hci_conn_hash_list_state(hdev, bis_list, BIS_LINK, 1955 BT_BOUND, &data); 1956 1957 cp.handle = qos->bcast.big; 1958 cp.adv_handle = qos->bcast.bis; 1959 cp.num_bis = data.count; 1960 hci_cpu_to_le24(qos->bcast.out.interval, cp.bis.sdu_interval); 1961 cp.bis.sdu = cpu_to_le16(qos->bcast.out.sdu); 1962 cp.bis.latency = cpu_to_le16(qos->bcast.out.latency); 1963 cp.bis.rtn = qos->bcast.out.rtn; 1964 cp.bis.phy = qos->bcast.out.phys; 1965 cp.bis.packing = qos->bcast.packing; 1966 cp.bis.framing = qos->bcast.framing; 1967 cp.bis.encryption = qos->bcast.encryption; 1968 memcpy(cp.bis.bcode, qos->bcast.bcode, sizeof(cp.bis.bcode)); 1969 1970 return hci_send_cmd(hdev, HCI_OP_LE_CREATE_BIG, sizeof(cp), &cp); 1971 } 1972 1973 static int set_cig_params_sync(struct hci_dev *hdev, void *data) 1974 { 1975 DEFINE_FLEX(struct hci_cp_le_set_cig_params, pdu, cis, num_cis, 0x1f); 1976 u8 cig_id = PTR_UINT(data); 1977 struct hci_conn *conn; 1978 struct bt_iso_qos *qos; 1979 u8 aux_num_cis = 0; 1980 u8 cis_id; 1981 1982 hci_dev_lock(hdev); 1983 1984 conn = hci_conn_hash_lookup_cig(hdev, cig_id); 1985 if (!conn) { 1986 hci_dev_unlock(hdev); 1987 return 0; 1988 } 1989 1990 qos = &conn->iso_qos; 1991 pdu->cig_id = cig_id; 1992 hci_cpu_to_le24(qos->ucast.out.interval, pdu->c_interval); 1993 hci_cpu_to_le24(qos->ucast.in.interval, pdu->p_interval); 1994 pdu->sca = qos->ucast.sca; 1995 pdu->packing = qos->ucast.packing; 1996 pdu->framing = qos->ucast.framing; 1997 pdu->c_latency = cpu_to_le16(qos->ucast.out.latency); 1998 pdu->p_latency = cpu_to_le16(qos->ucast.in.latency); 1999 2000 /* Reprogram all CIS(s) with the same CIG, valid range are: 2001 * num_cis: 0x00 to 0x1F 2002 * cis_id: 0x00 to 0xEF 2003 */ 2004 for (cis_id = 0x00; cis_id < 0xf0 && 2005 aux_num_cis < pdu->num_cis; cis_id++) { 2006 struct hci_cis_params *cis; 2007 2008 conn = hci_conn_hash_lookup_cis(hdev, NULL, 0, cig_id, cis_id); 2009 if (!conn) 2010 continue; 2011 2012 qos = &conn->iso_qos; 2013 2014 cis = &pdu->cis[aux_num_cis++]; 2015 cis->cis_id = cis_id; 2016 cis->c_sdu = cpu_to_le16(conn->iso_qos.ucast.out.sdu); 2017 cis->p_sdu = cpu_to_le16(conn->iso_qos.ucast.in.sdu); 2018 cis->c_phys = qos->ucast.out.phys ? qos->ucast.out.phys : 2019 qos->ucast.in.phys; 2020 cis->p_phys = qos->ucast.in.phys ? qos->ucast.in.phys : 2021 qos->ucast.out.phys; 2022 cis->c_rtn = qos->ucast.out.rtn; 2023 cis->p_rtn = qos->ucast.in.rtn; 2024 } 2025 pdu->num_cis = aux_num_cis; 2026 2027 hci_dev_unlock(hdev); 2028 2029 if (!pdu->num_cis) 2030 return 0; 2031 2032 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_CIG_PARAMS, 2033 struct_size(pdu, cis, pdu->num_cis), 2034 pdu, HCI_CMD_TIMEOUT); 2035 } 2036 2037 static bool hci_le_set_cig_params(struct hci_conn *conn, struct bt_iso_qos *qos) 2038 { 2039 struct hci_dev *hdev = conn->hdev; 2040 struct iso_list_data data; 2041 2042 memset(&data, 0, sizeof(data)); 2043 2044 /* Allocate first still reconfigurable CIG if not set */ 2045 if (qos->ucast.cig == BT_ISO_QOS_CIG_UNSET) { 2046 for (data.cig = 0x00; data.cig < 0xf0; data.cig++) { 2047 data.count = 0; 2048 2049 hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, 2050 BT_CONNECT, &data); 2051 if (data.count) 2052 continue; 2053 2054 hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, 2055 BT_CONNECTED, &data); 2056 if (!data.count) 2057 break; 2058 } 2059 2060 if (data.cig == 0xf0) 2061 return false; 2062 2063 /* Update CIG */ 2064 qos->ucast.cig = data.cig; 2065 } 2066 2067 if (qos->ucast.cis != BT_ISO_QOS_CIS_UNSET) { 2068 if (hci_conn_hash_lookup_cis(hdev, NULL, 0, qos->ucast.cig, 2069 qos->ucast.cis)) 2070 return false; 2071 goto done; 2072 } 2073 2074 /* Allocate first available CIS if not set */ 2075 for (data.cig = qos->ucast.cig, data.cis = 0x00; data.cis < 0xf0; 2076 data.cis++) { 2077 if (!hci_conn_hash_lookup_cis(hdev, NULL, 0, data.cig, 2078 data.cis)) { 2079 /* Update CIS */ 2080 qos->ucast.cis = data.cis; 2081 break; 2082 } 2083 } 2084 2085 if (qos->ucast.cis == BT_ISO_QOS_CIS_UNSET) 2086 return false; 2087 2088 done: 2089 conn->iso_qos = *qos; 2090 2091 if (hci_cmd_sync_queue(hdev, set_cig_params_sync, 2092 UINT_PTR(qos->ucast.cig), NULL) < 0) 2093 return false; 2094 2095 return true; 2096 } 2097 2098 struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst, 2099 __u8 dst_type, struct bt_iso_qos *qos, 2100 u16 timeout) 2101 { 2102 struct hci_conn *cis; 2103 2104 cis = hci_conn_hash_lookup_cis(hdev, dst, dst_type, qos->ucast.cig, 2105 qos->ucast.cis); 2106 if (!cis) { 2107 cis = hci_conn_add_unset(hdev, CIS_LINK, dst, dst_type, 2108 HCI_ROLE_MASTER); 2109 if (IS_ERR(cis)) 2110 return cis; 2111 cis->cleanup = cis_cleanup; 2112 cis->dst_type = dst_type; 2113 cis->iso_qos.ucast.cig = BT_ISO_QOS_CIG_UNSET; 2114 cis->iso_qos.ucast.cis = BT_ISO_QOS_CIS_UNSET; 2115 cis->conn_timeout = timeout; 2116 } 2117 2118 hci_conn_hold(cis); 2119 2120 if (cis->state == BT_CONNECTED) 2121 return cis; 2122 2123 /* Check if CIS has been set and the settings matches */ 2124 if (cis->state == BT_BOUND && 2125 !memcmp(&cis->iso_qos, qos, sizeof(*qos))) 2126 return cis; 2127 2128 /* Update LINK PHYs according to QoS preference */ 2129 cis->le_tx_phy = qos->ucast.out.phys; 2130 cis->le_rx_phy = qos->ucast.in.phys; 2131 2132 /* If output interval is not set use the input interval as it cannot be 2133 * 0x000000. 2134 */ 2135 if (!qos->ucast.out.interval) 2136 qos->ucast.out.interval = qos->ucast.in.interval; 2137 2138 /* If input interval is not set use the output interval as it cannot be 2139 * 0x000000. 2140 */ 2141 if (!qos->ucast.in.interval) 2142 qos->ucast.in.interval = qos->ucast.out.interval; 2143 2144 /* If output latency is not set use the input latency as it cannot be 2145 * 0x0000. 2146 */ 2147 if (!qos->ucast.out.latency) 2148 qos->ucast.out.latency = qos->ucast.in.latency; 2149 2150 /* If input latency is not set use the output latency as it cannot be 2151 * 0x0000. 2152 */ 2153 if (!qos->ucast.in.latency) 2154 qos->ucast.in.latency = qos->ucast.out.latency; 2155 2156 if (!hci_le_set_cig_params(cis, qos)) { 2157 hci_conn_drop(cis); 2158 return ERR_PTR(-EINVAL); 2159 } 2160 2161 cis->state = BT_BOUND; 2162 2163 return cis; 2164 } 2165 2166 bool hci_iso_setup_path(struct hci_conn *conn) 2167 { 2168 struct hci_dev *hdev = conn->hdev; 2169 struct hci_cp_le_setup_iso_path cmd; 2170 2171 memset(&cmd, 0, sizeof(cmd)); 2172 2173 if (conn->iso_qos.ucast.out.sdu) { 2174 cmd.handle = cpu_to_le16(conn->handle); 2175 cmd.direction = 0x00; /* Input (Host to Controller) */ 2176 cmd.path = 0x00; /* HCI path if enabled */ 2177 cmd.codec = 0x03; /* Transparent Data */ 2178 2179 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd), 2180 &cmd) < 0) 2181 return false; 2182 } 2183 2184 if (conn->iso_qos.ucast.in.sdu) { 2185 cmd.handle = cpu_to_le16(conn->handle); 2186 cmd.direction = 0x01; /* Output (Controller to Host) */ 2187 cmd.path = 0x00; /* HCI path if enabled */ 2188 cmd.codec = 0x03; /* Transparent Data */ 2189 2190 if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd), 2191 &cmd) < 0) 2192 return false; 2193 } 2194 2195 return true; 2196 } 2197 2198 int hci_conn_check_create_cis(struct hci_conn *conn) 2199 { 2200 if (conn->type != CIS_LINK) 2201 return -EINVAL; 2202 2203 if (!conn->parent || conn->parent->state != BT_CONNECTED || 2204 conn->state != BT_CONNECT || HCI_CONN_HANDLE_UNSET(conn->handle)) 2205 return 1; 2206 2207 return 0; 2208 } 2209 2210 static int hci_create_cis_sync(struct hci_dev *hdev, void *data) 2211 { 2212 return hci_le_create_cis_sync(hdev); 2213 } 2214 2215 int hci_le_create_cis_pending(struct hci_dev *hdev) 2216 { 2217 struct hci_conn *conn; 2218 bool pending = false; 2219 2220 rcu_read_lock(); 2221 2222 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) { 2223 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) { 2224 rcu_read_unlock(); 2225 return -EBUSY; 2226 } 2227 2228 if (!hci_conn_check_create_cis(conn)) 2229 pending = true; 2230 } 2231 2232 rcu_read_unlock(); 2233 2234 if (!pending) 2235 return 0; 2236 2237 /* Queue Create CIS */ 2238 return hci_cmd_sync_queue(hdev, hci_create_cis_sync, NULL, NULL); 2239 } 2240 2241 static void hci_iso_qos_setup(struct hci_dev *hdev, struct hci_conn *conn, 2242 struct bt_iso_io_qos *qos, __u8 phys) 2243 { 2244 /* Only set MTU if PHY is enabled */ 2245 if (!qos->sdu && qos->phys) 2246 qos->sdu = conn->mtu; 2247 2248 /* Use the same PHY as ACL if set to any */ 2249 if (qos->phys == BT_ISO_PHY_ANY) 2250 qos->phys = phys; 2251 2252 /* Use LE ACL connection interval if not set */ 2253 if (!qos->interval) 2254 /* ACL interval unit in 1.25 ms to us */ 2255 qos->interval = conn->le_conn_interval * 1250; 2256 2257 /* Use LE ACL connection latency if not set */ 2258 if (!qos->latency) 2259 qos->latency = conn->le_conn_latency; 2260 } 2261 2262 static int create_big_sync(struct hci_dev *hdev, void *data) 2263 { 2264 struct hci_conn *conn = data; 2265 struct bt_iso_qos *qos = &conn->iso_qos; 2266 u16 interval, sync_interval = 0; 2267 u32 flags = 0; 2268 int err; 2269 2270 if (!hci_conn_valid(hdev, conn)) 2271 return -ECANCELED; 2272 2273 if (qos->bcast.out.phys == BIT(1)) 2274 flags |= MGMT_ADV_FLAG_SEC_2M; 2275 2276 /* Align intervals */ 2277 interval = (qos->bcast.out.interval / 1250) * qos->bcast.sync_factor; 2278 2279 if (qos->bcast.bis) 2280 sync_interval = interval * 4; 2281 2282 err = hci_start_per_adv_sync(hdev, qos->bcast.bis, conn->sid, 2283 conn->le_per_adv_data_len, 2284 conn->le_per_adv_data, flags, interval, 2285 interval, sync_interval); 2286 if (err) 2287 return err; 2288 2289 return hci_le_create_big(conn, &conn->iso_qos); 2290 } 2291 2292 struct hci_conn *hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst, 2293 __u8 dst_type, __u8 sid, 2294 struct bt_iso_qos *qos) 2295 { 2296 struct hci_conn *conn; 2297 2298 bt_dev_dbg(hdev, "dst %pMR type %d sid %d", dst, dst_type, sid); 2299 2300 conn = hci_conn_add_unset(hdev, PA_LINK, dst, dst_type, HCI_ROLE_SLAVE); 2301 if (IS_ERR(conn)) 2302 return conn; 2303 2304 conn->iso_qos = *qos; 2305 conn->sid = sid; 2306 conn->state = BT_LISTEN; 2307 conn->conn_timeout = msecs_to_jiffies(qos->bcast.sync_timeout * 10); 2308 2309 hci_conn_hold(conn); 2310 2311 hci_connect_pa_sync(hdev, conn); 2312 2313 return conn; 2314 } 2315 2316 int hci_conn_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon, 2317 struct bt_iso_qos *qos, __u16 sync_handle, 2318 __u8 num_bis, __u8 bis[]) 2319 { 2320 int err; 2321 2322 if (num_bis < 0x01 || num_bis > ISO_MAX_NUM_BIS) 2323 return -EINVAL; 2324 2325 err = qos_set_big(hdev, qos); 2326 if (err) 2327 return err; 2328 2329 if (hcon) { 2330 /* Update hcon QoS */ 2331 hcon->iso_qos = *qos; 2332 2333 hcon->num_bis = num_bis; 2334 memcpy(hcon->bis, bis, num_bis); 2335 hcon->conn_timeout = msecs_to_jiffies(qos->bcast.timeout * 10); 2336 } 2337 2338 return hci_connect_big_sync(hdev, hcon); 2339 } 2340 2341 static void create_big_complete(struct hci_dev *hdev, void *data, int err) 2342 { 2343 struct hci_conn *conn = data; 2344 2345 bt_dev_dbg(hdev, "conn %p", conn); 2346 2347 if (err == -ECANCELED) 2348 goto done; 2349 2350 hci_dev_lock(hdev); 2351 2352 if (!hci_conn_valid(hdev, conn)) 2353 goto unlock; 2354 2355 if (err) { 2356 bt_dev_err(hdev, "Unable to create BIG: %d", err); 2357 hci_connect_cfm(conn, err); 2358 hci_conn_del(conn); 2359 } 2360 2361 unlock: 2362 hci_dev_unlock(hdev); 2363 done: 2364 hci_conn_put(conn); 2365 } 2366 2367 struct hci_conn *hci_bind_bis(struct hci_dev *hdev, bdaddr_t *dst, __u8 sid, 2368 struct bt_iso_qos *qos, 2369 __u8 base_len, __u8 *base, u16 timeout) 2370 { 2371 struct hci_conn *conn; 2372 struct hci_conn *parent; 2373 __u8 eir[HCI_MAX_PER_AD_LENGTH]; 2374 struct hci_link *link; 2375 2376 /* Look for any BIS that is open for rebinding */ 2377 conn = hci_conn_hash_lookup_big_state(hdev, qos->bcast.big, BT_OPEN, 2378 HCI_ROLE_MASTER); 2379 if (conn) { 2380 memcpy(qos, &conn->iso_qos, sizeof(*qos)); 2381 conn->state = BT_CONNECTED; 2382 return conn; 2383 } 2384 2385 if (base_len && base) 2386 base_len = eir_append_service_data(eir, 0, 0x1851, 2387 base, base_len); 2388 2389 /* We need hci_conn object using the BDADDR_ANY as dst */ 2390 conn = hci_add_bis(hdev, dst, sid, qos, base_len, eir, timeout); 2391 if (IS_ERR(conn)) 2392 return conn; 2393 2394 /* Update LINK PHYs according to QoS preference */ 2395 conn->le_tx_def_phys = qos->bcast.out.phys; 2396 2397 /* Add Basic Announcement into Peridic Adv Data if BASE is set */ 2398 if (base_len && base) { 2399 memcpy(conn->le_per_adv_data, eir, sizeof(eir)); 2400 conn->le_per_adv_data_len = base_len; 2401 } 2402 2403 hci_iso_qos_setup(hdev, conn, &qos->bcast.out, 2404 conn->le_tx_def_phys ? conn->le_tx_def_phys : 2405 hdev->le_tx_def_phys); 2406 2407 conn->iso_qos = *qos; 2408 conn->state = BT_BOUND; 2409 2410 /* Link BISes together */ 2411 parent = hci_conn_hash_lookup_big(hdev, 2412 conn->iso_qos.bcast.big); 2413 if (parent && parent != conn) { 2414 hci_conn_hold(parent); 2415 link = hci_conn_link(parent, conn); 2416 hci_conn_drop(conn); 2417 if (!link) { 2418 hci_conn_drop(parent); 2419 return ERR_PTR(-ENOLINK); 2420 } 2421 } 2422 2423 return conn; 2424 } 2425 2426 int hci_past_bis(struct hci_conn *conn, bdaddr_t *dst, __u8 dst_type) 2427 { 2428 struct hci_conn *le; 2429 2430 /* Lookup existing LE connection to rebind to */ 2431 le = hci_conn_hash_lookup_le(conn->hdev, dst, dst_type); 2432 if (!le) 2433 return -EINVAL; 2434 2435 return hci_past_sync(conn, le); 2436 } 2437 2438 static void bis_mark_per_adv(struct hci_conn *conn, void *data) 2439 { 2440 struct iso_list_data *d = data; 2441 2442 /* Skip if not broadcast/ANY address */ 2443 if (bacmp(&conn->dst, BDADDR_ANY)) 2444 return; 2445 2446 if (d->big != conn->iso_qos.bcast.big || 2447 d->bis == BT_ISO_QOS_BIS_UNSET || 2448 d->bis != conn->iso_qos.bcast.bis) 2449 return; 2450 2451 set_bit(HCI_CONN_PER_ADV, &conn->flags); 2452 } 2453 2454 struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst, 2455 __u8 dst_type, __u8 sid, 2456 struct bt_iso_qos *qos, 2457 __u8 base_len, __u8 *base, u16 timeout) 2458 { 2459 struct hci_conn *conn; 2460 int err; 2461 struct iso_list_data data; 2462 2463 conn = hci_bind_bis(hdev, dst, sid, qos, base_len, base, timeout); 2464 if (IS_ERR(conn)) 2465 return conn; 2466 2467 if (conn->state == BT_CONNECTED) 2468 return conn; 2469 2470 /* Check if SID needs to be allocated then search for the first 2471 * available. 2472 */ 2473 if (conn->sid == HCI_SID_INVALID) { 2474 u8 sid; 2475 2476 for (sid = 0; sid <= 0x0f; sid++) { 2477 if (!hci_find_adv_sid(hdev, sid)) { 2478 conn->sid = sid; 2479 break; 2480 } 2481 } 2482 } 2483 2484 data.big = qos->bcast.big; 2485 data.bis = qos->bcast.bis; 2486 2487 /* Set HCI_CONN_PER_ADV for all bound connections, to mark that 2488 * the start periodic advertising and create BIG commands have 2489 * been queued 2490 */ 2491 hci_conn_hash_list_state(hdev, bis_mark_per_adv, BIS_LINK, 2492 BT_BOUND, &data); 2493 2494 /* Queue start periodic advertising and create BIG */ 2495 err = hci_cmd_sync_queue(hdev, create_big_sync, hci_conn_get(conn), 2496 create_big_complete); 2497 if (err < 0) { 2498 hci_conn_drop(conn); 2499 hci_conn_put(conn); 2500 return ERR_PTR(err); 2501 } 2502 2503 return conn; 2504 } 2505 2506 struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst, 2507 __u8 dst_type, struct bt_iso_qos *qos, 2508 u16 timeout) 2509 { 2510 struct hci_conn *le; 2511 struct hci_conn *cis; 2512 struct hci_link *link; 2513 2514 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) 2515 le = hci_connect_le(hdev, dst, dst_type, false, 2516 BT_SECURITY_LOW, 2517 HCI_LE_CONN_TIMEOUT, 2518 HCI_ROLE_SLAVE, 0, 0); 2519 else 2520 le = hci_connect_le_scan(hdev, dst, dst_type, 2521 BT_SECURITY_LOW, 2522 HCI_LE_CONN_TIMEOUT, 2523 CONN_REASON_ISO_CONNECT); 2524 if (IS_ERR(le)) 2525 return le; 2526 2527 hci_iso_qos_setup(hdev, le, &qos->ucast.out, 2528 le->le_tx_def_phys ? le->le_tx_def_phys : 2529 hdev->le_tx_def_phys); 2530 hci_iso_qos_setup(hdev, le, &qos->ucast.in, 2531 le->le_rx_def_phys ? le->le_rx_def_phys : 2532 hdev->le_rx_def_phys); 2533 2534 cis = hci_bind_cis(hdev, dst, dst_type, qos, timeout); 2535 if (IS_ERR(cis)) { 2536 hci_conn_drop(le); 2537 return cis; 2538 } 2539 2540 /* The existing link already owns the hold on its parent. */ 2541 if (cis->link) { 2542 hci_conn_drop(le); 2543 return cis; 2544 } 2545 2546 link = hci_conn_link(le, cis); 2547 hci_conn_drop(cis); 2548 if (!link) { 2549 hci_conn_drop(le); 2550 return ERR_PTR(-ENOLINK); 2551 } 2552 2553 cis->state = BT_CONNECT; 2554 2555 hci_le_create_cis_pending(hdev); 2556 2557 return cis; 2558 } 2559 2560 /* Check link security requirement */ 2561 int hci_conn_check_link_mode(struct hci_conn *conn) 2562 { 2563 BT_DBG("hcon %p", conn); 2564 2565 /* In Secure Connections Only mode, it is required that Secure 2566 * Connections is used and the link is encrypted with AES-CCM 2567 * using a P-256 authenticated combination key. 2568 */ 2569 if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) { 2570 if (!hci_conn_sc_enabled(conn) || 2571 !test_bit(HCI_CONN_AES_CCM, &conn->flags) || 2572 conn->key_type != HCI_LK_AUTH_COMBINATION_P256) 2573 return 0; 2574 } 2575 2576 /* AES encryption is required for Level 4: 2577 * 2578 * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part C 2579 * page 1319: 2580 * 2581 * 128-bit equivalent strength for link and encryption keys 2582 * required using FIPS approved algorithms (E0 not allowed, 2583 * SAFER+ not allowed, and P-192 not allowed; encryption key 2584 * not shortened) 2585 */ 2586 if (conn->sec_level == BT_SECURITY_FIPS && 2587 !test_bit(HCI_CONN_AES_CCM, &conn->flags)) { 2588 bt_dev_err(conn->hdev, 2589 "Invalid security: Missing AES-CCM usage"); 2590 return 0; 2591 } 2592 2593 if (hci_conn_ssp_enabled(conn) && 2594 !test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 2595 return 0; 2596 2597 return 1; 2598 } 2599 2600 /* Authenticate remote device */ 2601 static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) 2602 { 2603 BT_DBG("hcon %p", conn); 2604 2605 if (conn->pending_sec_level > sec_level) 2606 sec_level = conn->pending_sec_level; 2607 2608 if (sec_level > conn->sec_level) 2609 conn->pending_sec_level = sec_level; 2610 else if (test_bit(HCI_CONN_AUTH, &conn->flags)) 2611 return 1; 2612 2613 /* Make sure we preserve an existing MITM requirement*/ 2614 auth_type |= (conn->auth_type & 0x01); 2615 2616 conn->auth_type = auth_type; 2617 2618 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { 2619 struct hci_cp_auth_requested cp; 2620 2621 cp.handle = cpu_to_le16(conn->handle); 2622 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, 2623 sizeof(cp), &cp); 2624 2625 /* Set the ENCRYPT_PEND to trigger encryption after 2626 * authentication. 2627 */ 2628 if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 2629 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags); 2630 } 2631 2632 return 0; 2633 } 2634 2635 /* Encrypt the link */ 2636 static void hci_conn_encrypt(struct hci_conn *conn) 2637 { 2638 BT_DBG("hcon %p", conn); 2639 2640 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) { 2641 struct hci_cp_set_conn_encrypt cp; 2642 cp.handle = cpu_to_le16(conn->handle); 2643 cp.encrypt = 0x01; 2644 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), 2645 &cp); 2646 } 2647 } 2648 2649 /* Enable security */ 2650 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type, 2651 bool initiator) 2652 { 2653 BT_DBG("hcon %p", conn); 2654 2655 if (conn->type == LE_LINK) 2656 return smp_conn_security(conn, sec_level); 2657 2658 /* For sdp we don't need the link key. */ 2659 if (sec_level == BT_SECURITY_SDP) 2660 return 1; 2661 2662 /* For non 2.1 devices and low security level we don't need the link 2663 key. */ 2664 if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn)) 2665 return 1; 2666 2667 /* For other security levels we need the link key. */ 2668 if (!test_bit(HCI_CONN_AUTH, &conn->flags)) 2669 goto auth; 2670 2671 switch (conn->key_type) { 2672 case HCI_LK_AUTH_COMBINATION_P256: 2673 /* An authenticated FIPS approved combination key has 2674 * sufficient security for security level 4 or lower. 2675 */ 2676 if (sec_level <= BT_SECURITY_FIPS) 2677 goto encrypt; 2678 break; 2679 case HCI_LK_AUTH_COMBINATION_P192: 2680 /* An authenticated combination key has sufficient security for 2681 * security level 3 or lower. 2682 */ 2683 if (sec_level <= BT_SECURITY_HIGH) 2684 goto encrypt; 2685 break; 2686 case HCI_LK_UNAUTH_COMBINATION_P192: 2687 case HCI_LK_UNAUTH_COMBINATION_P256: 2688 /* An unauthenticated combination key has sufficient security 2689 * for security level 2 or lower. 2690 */ 2691 if (sec_level <= BT_SECURITY_MEDIUM) 2692 goto encrypt; 2693 break; 2694 case HCI_LK_COMBINATION: 2695 /* A combination key has always sufficient security for the 2696 * security levels 2 or lower. High security level requires the 2697 * combination key is generated using maximum PIN code length 2698 * (16). For pre 2.1 units. 2699 */ 2700 if (sec_level <= BT_SECURITY_MEDIUM || conn->pin_length == 16) 2701 goto encrypt; 2702 break; 2703 default: 2704 break; 2705 } 2706 2707 auth: 2708 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) 2709 return 0; 2710 2711 if (initiator) 2712 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags); 2713 2714 if (!hci_conn_auth(conn, sec_level, auth_type)) 2715 return 0; 2716 2717 encrypt: 2718 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) { 2719 /* Ensure that the encryption key size has been read, 2720 * otherwise stall the upper layer responses. 2721 */ 2722 if (!conn->enc_key_size) 2723 return 0; 2724 2725 /* Nothing else needed, all requirements are met */ 2726 return 1; 2727 } 2728 2729 hci_conn_encrypt(conn); 2730 return 0; 2731 } 2732 EXPORT_SYMBOL(hci_conn_security); 2733 2734 /* Check secure link requirement */ 2735 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level) 2736 { 2737 BT_DBG("hcon %p", conn); 2738 2739 /* Accept if non-secure or higher security level is required */ 2740 if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS) 2741 return 1; 2742 2743 /* Accept if secure or higher security level is already present */ 2744 if (conn->sec_level == BT_SECURITY_HIGH || 2745 conn->sec_level == BT_SECURITY_FIPS) 2746 return 1; 2747 2748 /* Reject not secure link */ 2749 return 0; 2750 } 2751 EXPORT_SYMBOL(hci_conn_check_secure); 2752 2753 /* Switch role */ 2754 int hci_conn_switch_role(struct hci_conn *conn, __u8 role) 2755 { 2756 BT_DBG("hcon %p", conn); 2757 2758 if (role == conn->role) 2759 return 1; 2760 2761 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) { 2762 struct hci_cp_switch_role cp; 2763 bacpy(&cp.bdaddr, &conn->dst); 2764 cp.role = role; 2765 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); 2766 } 2767 2768 return 0; 2769 } 2770 EXPORT_SYMBOL(hci_conn_switch_role); 2771 2772 /* Enter active mode */ 2773 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active) 2774 { 2775 struct hci_dev *hdev = conn->hdev; 2776 2777 BT_DBG("hcon %p mode %d", conn, conn->mode); 2778 2779 if (conn->mode != HCI_CM_SNIFF) 2780 goto timer; 2781 2782 if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active) 2783 goto timer; 2784 2785 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { 2786 struct hci_cp_exit_sniff_mode cp; 2787 cp.handle = cpu_to_le16(conn->handle); 2788 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); 2789 } 2790 2791 timer: 2792 if (hdev->idle_timeout > 0) 2793 mod_delayed_work(hdev->workqueue, &conn->idle_work, 2794 msecs_to_jiffies(hdev->idle_timeout)); 2795 } 2796 2797 /* Drop all connection on the device */ 2798 void hci_conn_hash_flush(struct hci_dev *hdev) 2799 { 2800 struct list_head *head = &hdev->conn_hash.list; 2801 struct hci_conn *conn; 2802 2803 BT_DBG("hdev %s", hdev->name); 2804 2805 /* We should not traverse the list here, because hci_conn_del 2806 * can remove extra links, which may cause the list traversal 2807 * to hit items that have already been released. 2808 */ 2809 while ((conn = list_first_entry_or_null(head, 2810 struct hci_conn, 2811 list)) != NULL) { 2812 conn->state = BT_CLOSED; 2813 hci_disconn_cfm(conn, HCI_ERROR_LOCAL_HOST_TERM); 2814 hci_conn_del(conn); 2815 } 2816 } 2817 2818 static u32 get_link_mode(struct hci_conn *conn) 2819 { 2820 u32 link_mode = 0; 2821 2822 if (conn->role == HCI_ROLE_MASTER) 2823 link_mode |= HCI_LM_MASTER; 2824 2825 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 2826 link_mode |= HCI_LM_ENCRYPT; 2827 2828 if (test_bit(HCI_CONN_AUTH, &conn->flags)) 2829 link_mode |= HCI_LM_AUTH; 2830 2831 if (test_bit(HCI_CONN_SECURE, &conn->flags)) 2832 link_mode |= HCI_LM_SECURE; 2833 2834 if (test_bit(HCI_CONN_FIPS, &conn->flags)) 2835 link_mode |= HCI_LM_FIPS; 2836 2837 return link_mode; 2838 } 2839 2840 int hci_get_conn_list(void __user *arg) 2841 { 2842 struct hci_conn *c; 2843 struct hci_conn_list_req req, *cl; 2844 struct hci_conn_info *ci; 2845 struct hci_dev *hdev; 2846 int n = 0, size, err; 2847 2848 if (copy_from_user(&req, arg, sizeof(req))) 2849 return -EFAULT; 2850 2851 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) 2852 return -EINVAL; 2853 2854 size = sizeof(req) + req.conn_num * sizeof(*ci); 2855 2856 cl = kmalloc(size, GFP_KERNEL); 2857 if (!cl) 2858 return -ENOMEM; 2859 2860 hdev = hci_dev_get(req.dev_id); 2861 if (!hdev) { 2862 kfree(cl); 2863 return -ENODEV; 2864 } 2865 2866 ci = cl->conn_info; 2867 2868 hci_dev_lock(hdev); 2869 list_for_each_entry(c, &hdev->conn_hash.list, list) { 2870 bacpy(&(ci + n)->bdaddr, &c->dst); 2871 (ci + n)->handle = c->handle; 2872 (ci + n)->type = c->type; 2873 (ci + n)->out = c->out; 2874 (ci + n)->state = c->state; 2875 (ci + n)->link_mode = get_link_mode(c); 2876 if (++n >= req.conn_num) 2877 break; 2878 } 2879 hci_dev_unlock(hdev); 2880 2881 cl->dev_id = hdev->id; 2882 cl->conn_num = n; 2883 size = sizeof(req) + n * sizeof(*ci); 2884 2885 hci_dev_put(hdev); 2886 2887 err = copy_to_user(arg, cl, size); 2888 kfree(cl); 2889 2890 return err ? -EFAULT : 0; 2891 } 2892 2893 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) 2894 { 2895 struct hci_conn_info_req req; 2896 struct hci_conn_info ci; 2897 struct hci_conn *conn; 2898 char __user *ptr = arg + sizeof(req); 2899 2900 if (copy_from_user(&req, arg, sizeof(req))) 2901 return -EFAULT; 2902 2903 hci_dev_lock(hdev); 2904 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); 2905 if (conn) { 2906 bacpy(&ci.bdaddr, &conn->dst); 2907 ci.handle = conn->handle; 2908 ci.type = conn->type; 2909 ci.out = conn->out; 2910 ci.state = conn->state; 2911 ci.link_mode = get_link_mode(conn); 2912 } 2913 hci_dev_unlock(hdev); 2914 2915 if (!conn) 2916 return -ENOENT; 2917 2918 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; 2919 } 2920 2921 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) 2922 { 2923 struct hci_auth_info_req req; 2924 struct hci_conn *conn; 2925 2926 if (copy_from_user(&req, arg, sizeof(req))) 2927 return -EFAULT; 2928 2929 hci_dev_lock(hdev); 2930 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); 2931 if (conn) 2932 req.type = conn->auth_type; 2933 hci_dev_unlock(hdev); 2934 2935 if (!conn) 2936 return -ENOENT; 2937 2938 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; 2939 } 2940 2941 struct hci_chan *hci_chan_create(struct hci_conn *conn) 2942 { 2943 struct hci_dev *hdev = conn->hdev; 2944 struct hci_chan *chan; 2945 2946 BT_DBG("%s hcon %p", hdev->name, conn); 2947 2948 if (test_bit(HCI_CONN_DROP, &conn->flags)) { 2949 BT_DBG("Refusing to create new hci_chan"); 2950 return NULL; 2951 } 2952 2953 chan = kzalloc_obj(*chan); 2954 if (!chan) 2955 return NULL; 2956 2957 chan->conn = hci_conn_get(conn); 2958 skb_queue_head_init(&chan->data_q); 2959 chan->state = BT_CONNECTED; 2960 2961 list_add_rcu(&chan->list, &conn->chan_list); 2962 2963 return chan; 2964 } 2965 2966 void hci_chan_del(struct hci_chan *chan) 2967 { 2968 struct hci_conn *conn = chan->conn; 2969 struct hci_dev *hdev = conn->hdev; 2970 2971 BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan); 2972 2973 list_del_rcu(&chan->list); 2974 2975 synchronize_rcu(); 2976 2977 /* Prevent new hci_chan's to be created for this hci_conn */ 2978 set_bit(HCI_CONN_DROP, &conn->flags); 2979 2980 hci_conn_put(conn); 2981 2982 skb_queue_purge(&chan->data_q); 2983 kfree(chan); 2984 } 2985 2986 void hci_chan_list_flush(struct hci_conn *conn) 2987 { 2988 struct hci_chan *chan, *n; 2989 2990 BT_DBG("hcon %p", conn); 2991 2992 list_for_each_entry_safe(chan, n, &conn->chan_list, list) 2993 hci_chan_del(chan); 2994 } 2995 2996 static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon, 2997 __u16 handle) 2998 { 2999 struct hci_chan *hchan; 3000 3001 list_for_each_entry(hchan, &hcon->chan_list, list) { 3002 if (hchan->handle == handle) 3003 return hchan; 3004 } 3005 3006 return NULL; 3007 } 3008 3009 struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle) 3010 { 3011 struct hci_conn_hash *h = &hdev->conn_hash; 3012 struct hci_conn *hcon; 3013 struct hci_chan *hchan = NULL; 3014 3015 rcu_read_lock(); 3016 3017 list_for_each_entry_rcu(hcon, &h->list, list) { 3018 hchan = __hci_chan_lookup_handle(hcon, handle); 3019 if (hchan) 3020 break; 3021 } 3022 3023 rcu_read_unlock(); 3024 3025 return hchan; 3026 } 3027 3028 u32 hci_conn_get_phy(struct hci_conn *conn) 3029 { 3030 u32 phys = 0; 3031 3032 /* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471: 3033 * Table 6.2: Packets defined for synchronous, asynchronous, and 3034 * CPB logical transport types. 3035 */ 3036 switch (conn->type) { 3037 case SCO_LINK: 3038 /* SCO logical transport (1 Mb/s): 3039 * HV1, HV2, HV3 and DV. 3040 */ 3041 phys |= BT_PHY_BR_1M_1SLOT; 3042 3043 break; 3044 3045 case ACL_LINK: 3046 /* ACL logical transport (1 Mb/s) ptt=0: 3047 * DH1, DM3, DH3, DM5 and DH5. 3048 */ 3049 phys |= BT_PHY_BR_1M_1SLOT; 3050 3051 if (conn->pkt_type & (HCI_DM3 | HCI_DH3)) 3052 phys |= BT_PHY_BR_1M_3SLOT; 3053 3054 if (conn->pkt_type & (HCI_DM5 | HCI_DH5)) 3055 phys |= BT_PHY_BR_1M_5SLOT; 3056 3057 /* ACL logical transport (2 Mb/s) ptt=1: 3058 * 2-DH1, 2-DH3 and 2-DH5. 3059 */ 3060 if (!(conn->pkt_type & HCI_2DH1)) 3061 phys |= BT_PHY_EDR_2M_1SLOT; 3062 3063 if (!(conn->pkt_type & HCI_2DH3)) 3064 phys |= BT_PHY_EDR_2M_3SLOT; 3065 3066 if (!(conn->pkt_type & HCI_2DH5)) 3067 phys |= BT_PHY_EDR_2M_5SLOT; 3068 3069 /* ACL logical transport (3 Mb/s) ptt=1: 3070 * 3-DH1, 3-DH3 and 3-DH5. 3071 */ 3072 if (!(conn->pkt_type & HCI_3DH1)) 3073 phys |= BT_PHY_EDR_3M_1SLOT; 3074 3075 if (!(conn->pkt_type & HCI_3DH3)) 3076 phys |= BT_PHY_EDR_3M_3SLOT; 3077 3078 if (!(conn->pkt_type & HCI_3DH5)) 3079 phys |= BT_PHY_EDR_3M_5SLOT; 3080 3081 break; 3082 3083 case ESCO_LINK: 3084 /* eSCO logical transport (1 Mb/s): EV3, EV4 and EV5 */ 3085 phys |= BT_PHY_BR_1M_1SLOT; 3086 3087 if (!(conn->pkt_type & (ESCO_EV4 | ESCO_EV5))) 3088 phys |= BT_PHY_BR_1M_3SLOT; 3089 3090 /* eSCO logical transport (2 Mb/s): 2-EV3, 2-EV5 */ 3091 if (!(conn->pkt_type & ESCO_2EV3)) 3092 phys |= BT_PHY_EDR_2M_1SLOT; 3093 3094 if (!(conn->pkt_type & ESCO_2EV5)) 3095 phys |= BT_PHY_EDR_2M_3SLOT; 3096 3097 /* eSCO logical transport (3 Mb/s): 3-EV3, 3-EV5 */ 3098 if (!(conn->pkt_type & ESCO_3EV3)) 3099 phys |= BT_PHY_EDR_3M_1SLOT; 3100 3101 if (!(conn->pkt_type & ESCO_3EV5)) 3102 phys |= BT_PHY_EDR_3M_3SLOT; 3103 3104 break; 3105 3106 case LE_LINK: 3107 if (conn->le_tx_def_phys & HCI_LE_SET_PHY_1M) 3108 phys |= BT_PHY_LE_1M_TX; 3109 3110 if (conn->le_rx_def_phys & HCI_LE_SET_PHY_1M) 3111 phys |= BT_PHY_LE_1M_RX; 3112 3113 if (conn->le_tx_def_phys & HCI_LE_SET_PHY_2M) 3114 phys |= BT_PHY_LE_2M_TX; 3115 3116 if (conn->le_rx_def_phys & HCI_LE_SET_PHY_2M) 3117 phys |= BT_PHY_LE_2M_RX; 3118 3119 if (conn->le_tx_def_phys & HCI_LE_SET_PHY_CODED) 3120 phys |= BT_PHY_LE_CODED_TX; 3121 3122 if (conn->le_rx_def_phys & HCI_LE_SET_PHY_CODED) 3123 phys |= BT_PHY_LE_CODED_RX; 3124 3125 break; 3126 } 3127 3128 return phys; 3129 } 3130 3131 static u16 bt_phy_pkt_type(struct hci_conn *conn, u32 phys) 3132 { 3133 u16 pkt_type = conn->pkt_type; 3134 3135 if (phys & BT_PHY_BR_1M_3SLOT) 3136 pkt_type |= HCI_DM3 | HCI_DH3; 3137 else 3138 pkt_type &= ~(HCI_DM3 | HCI_DH3); 3139 3140 if (phys & BT_PHY_BR_1M_5SLOT) 3141 pkt_type |= HCI_DM5 | HCI_DH5; 3142 else 3143 pkt_type &= ~(HCI_DM5 | HCI_DH5); 3144 3145 if (phys & BT_PHY_EDR_2M_1SLOT) 3146 pkt_type &= ~HCI_2DH1; 3147 else 3148 pkt_type |= HCI_2DH1; 3149 3150 if (phys & BT_PHY_EDR_2M_3SLOT) 3151 pkt_type &= ~HCI_2DH3; 3152 else 3153 pkt_type |= HCI_2DH3; 3154 3155 if (phys & BT_PHY_EDR_2M_5SLOT) 3156 pkt_type &= ~HCI_2DH5; 3157 else 3158 pkt_type |= HCI_2DH5; 3159 3160 if (phys & BT_PHY_EDR_3M_1SLOT) 3161 pkt_type &= ~HCI_3DH1; 3162 else 3163 pkt_type |= HCI_3DH1; 3164 3165 if (phys & BT_PHY_EDR_3M_3SLOT) 3166 pkt_type &= ~HCI_3DH3; 3167 else 3168 pkt_type |= HCI_3DH3; 3169 3170 if (phys & BT_PHY_EDR_3M_5SLOT) 3171 pkt_type &= ~HCI_3DH5; 3172 else 3173 pkt_type |= HCI_3DH5; 3174 3175 return pkt_type; 3176 } 3177 3178 static int bt_phy_le_phy(u32 phys, u8 *tx_phys, u8 *rx_phys) 3179 { 3180 if (!tx_phys || !rx_phys) 3181 return -EINVAL; 3182 3183 *tx_phys = 0; 3184 *rx_phys = 0; 3185 3186 if (phys & BT_PHY_LE_1M_TX) 3187 *tx_phys |= HCI_LE_SET_PHY_1M; 3188 3189 if (phys & BT_PHY_LE_1M_RX) 3190 *rx_phys |= HCI_LE_SET_PHY_1M; 3191 3192 if (phys & BT_PHY_LE_2M_TX) 3193 *tx_phys |= HCI_LE_SET_PHY_2M; 3194 3195 if (phys & BT_PHY_LE_2M_RX) 3196 *rx_phys |= HCI_LE_SET_PHY_2M; 3197 3198 if (phys & BT_PHY_LE_CODED_TX) 3199 *tx_phys |= HCI_LE_SET_PHY_CODED; 3200 3201 if (phys & BT_PHY_LE_CODED_RX) 3202 *rx_phys |= HCI_LE_SET_PHY_CODED; 3203 3204 return 0; 3205 } 3206 3207 int hci_conn_set_phy(struct hci_conn *conn, u32 phys) 3208 { 3209 u8 tx_phys, rx_phys; 3210 3211 switch (conn->type) { 3212 case SCO_LINK: 3213 case ESCO_LINK: 3214 return -EINVAL; 3215 case ACL_LINK: 3216 /* Only allow setting BR/EDR PHYs if link type is ACL */ 3217 if (phys & ~BT_PHY_BREDR_MASK) 3218 return -EINVAL; 3219 3220 return hci_acl_change_pkt_type(conn, 3221 bt_phy_pkt_type(conn, phys)); 3222 case LE_LINK: 3223 /* Only allow setting LE PHYs if link type is LE */ 3224 if (phys & ~BT_PHY_LE_MASK) 3225 return -EINVAL; 3226 3227 if (bt_phy_le_phy(phys, &tx_phys, &rx_phys)) 3228 return -EINVAL; 3229 3230 return hci_le_set_phy(conn, tx_phys, rx_phys); 3231 default: 3232 return -EINVAL; 3233 } 3234 } 3235 3236 static int abort_conn_sync(struct hci_dev *hdev, void *data) 3237 { 3238 struct hci_conn *conn = data; 3239 3240 if (!hci_conn_valid(hdev, conn)) 3241 return -ECANCELED; 3242 3243 return hci_abort_conn_sync(hdev, conn, conn->abort_reason); 3244 } 3245 3246 static void abort_conn_destroy(struct hci_dev *hdev, void *data, int err) 3247 { 3248 struct hci_conn *conn = data; 3249 3250 hci_conn_put(conn); 3251 } 3252 3253 int hci_abort_conn(struct hci_conn *conn, u8 reason) 3254 { 3255 struct hci_dev *hdev = conn->hdev; 3256 int err; 3257 3258 /* If abort_reason has already been set it means the connection is 3259 * already being aborted so don't attempt to overwrite it. 3260 */ 3261 if (conn->abort_reason) 3262 return 0; 3263 3264 bt_dev_dbg(hdev, "handle 0x%2.2x reason 0x%2.2x", conn->handle, reason); 3265 3266 conn->abort_reason = reason; 3267 3268 /* Cancel the connect attempt. A return of 0 means the create command 3269 * was still queued and got dequeued, so there is nothing to disconnect. 3270 */ 3271 if (!hci_cancel_connect_sync(hdev, conn)) 3272 return 0; 3273 3274 /* Run immediately if on cmd_sync_work since this may be called 3275 * as a result to MGMT_OP_DISCONNECT/MGMT_OP_UNPAIR which does 3276 * already queue its callback on cmd_sync_work. 3277 */ 3278 err = hci_cmd_sync_run_once(hdev, abort_conn_sync, hci_conn_get(conn), 3279 abort_conn_destroy); 3280 if (err) 3281 hci_conn_put(conn); 3282 return (err == -EEXIST) ? 0 : err; 3283 } 3284 3285 void hci_setup_tx_timestamp(struct sk_buff *skb, size_t key_offset, 3286 const struct sockcm_cookie *sockc) 3287 { 3288 struct sock *sk = skb ? skb->sk : NULL; 3289 int key; 3290 3291 /* This shall be called on a single skb of those generated by user 3292 * sendmsg(), and only when the sendmsg() does not return error to 3293 * user. This is required for keeping the tskey that increments here in 3294 * sync with possible sendmsg() counting by user. 3295 * 3296 * Stream sockets shall set key_offset to sendmsg() length in bytes 3297 * and call with the last fragment, others to 1 and first fragment. 3298 */ 3299 3300 if (!skb || !sockc || !sk || !key_offset) 3301 return; 3302 3303 sock_tx_timestamp(sk, sockc, &skb_shinfo(skb)->tx_flags); 3304 3305 if (sk->sk_type == SOCK_STREAM) 3306 key = atomic_add_return(key_offset, &sk->sk_tskey); 3307 3308 if (sockc->tsflags & SOF_TIMESTAMPING_OPT_ID && 3309 sockc->tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK) { 3310 if (sockc->tsflags & SOCKCM_FLAG_TS_OPT_ID) { 3311 skb_shinfo(skb)->tskey = sockc->ts_opt_id; 3312 } else { 3313 if (sk->sk_type != SOCK_STREAM) 3314 key = atomic_inc_return(&sk->sk_tskey); 3315 skb_shinfo(skb)->tskey = key - 1; 3316 } 3317 } 3318 } 3319 3320 void hci_conn_tx_queue(struct hci_conn *conn, struct sk_buff *skb) 3321 { 3322 struct tx_queue *comp = &conn->tx_q; 3323 bool track = false; 3324 3325 /* Emit SND now, ie. just before sending to driver */ 3326 if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP) 3327 __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SND); 3328 3329 /* COMPLETION tstamp is emitted for tracked skb later in Number of 3330 * Completed Packets event. Available only for flow controlled cases. 3331 * 3332 * TODO: SCO support without flowctl (needs to be done in drivers) 3333 */ 3334 switch (conn->type) { 3335 case CIS_LINK: 3336 case BIS_LINK: 3337 case PA_LINK: 3338 case ACL_LINK: 3339 case LE_LINK: 3340 break; 3341 case SCO_LINK: 3342 case ESCO_LINK: 3343 if (!hci_dev_test_flag(conn->hdev, HCI_SCO_FLOWCTL)) 3344 return; 3345 break; 3346 default: 3347 return; 3348 } 3349 3350 if (skb->sk && (skb_shinfo(skb)->tx_flags & SKBTX_COMPLETION_TSTAMP)) 3351 track = true; 3352 3353 /* If nothing is tracked, just count extra skbs at the queue head */ 3354 if (!track && !comp->tracked) { 3355 comp->extra++; 3356 return; 3357 } 3358 3359 if (track) { 3360 skb = skb_clone_sk(skb); 3361 if (!skb) 3362 goto count_only; 3363 3364 comp->tracked++; 3365 } else { 3366 skb = skb_clone(skb, GFP_KERNEL); 3367 if (!skb) 3368 goto count_only; 3369 } 3370 3371 skb_queue_tail(&comp->queue, skb); 3372 return; 3373 3374 count_only: 3375 /* Stop tracking skbs, and only count. This will not emit timestamps for 3376 * the packets, but if we get here something is more seriously wrong. 3377 */ 3378 comp->tracked = 0; 3379 comp->extra += skb_queue_len(&comp->queue) + 1; 3380 skb_queue_purge(&comp->queue); 3381 } 3382 3383 void hci_conn_tx_dequeue(struct hci_conn *conn) 3384 { 3385 struct tx_queue *comp = &conn->tx_q; 3386 struct sk_buff *skb; 3387 3388 /* If there are tracked skbs, the counted extra go before dequeuing real 3389 * skbs, to keep ordering. When nothing is tracked, the ordering doesn't 3390 * matter so dequeue real skbs first to get rid of them ASAP. 3391 */ 3392 if (comp->extra && (comp->tracked || skb_queue_empty(&comp->queue))) { 3393 comp->extra--; 3394 return; 3395 } 3396 3397 skb = skb_dequeue(&comp->queue); 3398 if (!skb) 3399 return; 3400 3401 if (skb->sk) { 3402 comp->tracked--; 3403 __skb_tstamp_tx(skb, NULL, NULL, skb->sk, 3404 SCM_TSTAMP_COMPLETION); 3405 } 3406 3407 kfree_skb(skb); 3408 } 3409 3410 u8 *hci_conn_key_enc_size(struct hci_conn *conn) 3411 { 3412 if (conn->type == ACL_LINK) { 3413 struct link_key *key; 3414 3415 key = hci_find_link_key(conn->hdev, &conn->dst); 3416 if (!key) 3417 return NULL; 3418 3419 return &key->pin_len; 3420 } else if (conn->type == LE_LINK) { 3421 struct smp_ltk *ltk; 3422 3423 ltk = hci_find_ltk(conn->hdev, &conn->dst, conn->dst_type, 3424 conn->role); 3425 if (!ltk) 3426 return NULL; 3427 3428 return <k->enc_size; 3429 } 3430 3431 return NULL; 3432 } 3433 3434 int hci_ethtool_ts_info(unsigned int index, int sk_proto, 3435 struct kernel_ethtool_ts_info *info) 3436 { 3437 struct hci_dev *hdev; 3438 3439 hdev = hci_dev_get(index); 3440 if (!hdev) 3441 return -ENODEV; 3442 3443 info->so_timestamping = 3444 SOF_TIMESTAMPING_RX_SOFTWARE | 3445 SOF_TIMESTAMPING_SOFTWARE; 3446 info->phc_index = -1; 3447 info->tx_types = BIT(HWTSTAMP_TX_OFF); 3448 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE); 3449 3450 switch (sk_proto) { 3451 case BTPROTO_ISO: 3452 case BTPROTO_L2CAP: 3453 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE; 3454 info->so_timestamping |= SOF_TIMESTAMPING_TX_COMPLETION; 3455 break; 3456 case BTPROTO_SCO: 3457 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE; 3458 if (hci_dev_test_flag(hdev, HCI_SCO_FLOWCTL)) 3459 info->so_timestamping |= SOF_TIMESTAMPING_TX_COMPLETION; 3460 break; 3461 } 3462 3463 hci_dev_put(hdev); 3464 return 0; 3465 } 3466