1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2022 Intel Corporation 6 * Copyright 2023-2024 NXP 7 */ 8 9 #include <linux/module.h> 10 #include <linux/debugfs.h> 11 #include <linux/seq_file.h> 12 #include <linux/sched/signal.h> 13 14 #include <net/bluetooth/bluetooth.h> 15 #include <net/bluetooth/hci_core.h> 16 #include <net/bluetooth/iso.h> 17 #include "eir.h" 18 19 static const struct proto_ops iso_sock_ops; 20 21 static struct bt_sock_list iso_sk_list = { 22 .lock = __RW_LOCK_UNLOCKED(iso_sk_list.lock) 23 }; 24 25 /* ---- ISO connections ---- */ 26 struct iso_conn { 27 struct hci_conn *hcon; 28 29 /* @lock: spinlock protecting changes to iso_conn fields */ 30 spinlock_t lock; 31 struct sock *sk; 32 33 struct delayed_work timeout_work; 34 35 struct sk_buff *rx_skb; 36 __u32 rx_len; 37 __u16 tx_sn; 38 struct kref ref; 39 }; 40 41 #define iso_conn_lock(c) spin_lock(&(c)->lock) 42 #define iso_conn_unlock(c) spin_unlock(&(c)->lock) 43 44 static void iso_sock_close(struct sock *sk); 45 static void iso_sock_kill(struct sock *sk); 46 47 /* ----- ISO socket info ----- */ 48 #define iso_pi(sk) ((struct iso_pinfo *)sk) 49 50 #define EIR_SERVICE_DATA_LENGTH 4 51 #define BASE_MAX_LENGTH (HCI_MAX_PER_AD_LENGTH - EIR_SERVICE_DATA_LENGTH) 52 #define EIR_BAA_SERVICE_UUID 0x1851 53 54 /* iso_pinfo flags values */ 55 enum { 56 BT_SK_BIG_SYNC, 57 BT_SK_PA_SYNC, 58 }; 59 60 struct iso_pinfo { 61 struct bt_sock bt; 62 bdaddr_t src; 63 __u8 src_type; 64 bdaddr_t dst; 65 __u8 dst_type; 66 __u8 bc_sid; 67 __u8 bc_num_bis; 68 __u8 bc_bis[ISO_MAX_NUM_BIS]; 69 __u16 sync_handle; 70 unsigned long flags; 71 struct bt_iso_qos qos; 72 bool qos_user_set; 73 __u8 base_len; 74 __u8 base[BASE_MAX_LENGTH]; 75 struct iso_conn *conn; 76 }; 77 78 static struct bt_iso_qos default_qos; 79 80 static bool check_ucast_qos(struct bt_iso_qos *qos); 81 static bool check_bcast_qos(struct bt_iso_qos *qos); 82 static bool iso_match_sid(struct sock *sk, void *data); 83 static bool iso_match_sync_handle(struct sock *sk, void *data); 84 static bool iso_match_sync_handle_pa_report(struct sock *sk, void *data); 85 static void iso_sock_disconn(struct sock *sk); 86 87 typedef bool (*iso_sock_match_t)(struct sock *sk, void *data); 88 89 static struct sock *iso_get_sock(bdaddr_t *src, bdaddr_t *dst, 90 enum bt_sock_state state, 91 iso_sock_match_t match, void *data); 92 93 /* ---- ISO timers ---- */ 94 #define ISO_CONN_TIMEOUT (HZ * 40) 95 #define ISO_DISCONN_TIMEOUT (HZ * 2) 96 97 static void iso_conn_free(struct kref *ref) 98 { 99 struct iso_conn *conn = container_of(ref, struct iso_conn, ref); 100 101 BT_DBG("conn %p", conn); 102 103 if (conn->sk) 104 iso_pi(conn->sk)->conn = NULL; 105 106 if (conn->hcon) { 107 conn->hcon->iso_data = NULL; 108 hci_conn_drop(conn->hcon); 109 } 110 111 /* Ensure no more work items will run since hci_conn has been dropped */ 112 disable_delayed_work_sync(&conn->timeout_work); 113 114 kfree(conn); 115 } 116 117 static void iso_conn_put(struct iso_conn *conn) 118 { 119 if (!conn) 120 return; 121 122 BT_DBG("conn %p refcnt %d", conn, kref_read(&conn->ref)); 123 124 kref_put(&conn->ref, iso_conn_free); 125 } 126 127 static struct iso_conn *iso_conn_hold_unless_zero(struct iso_conn *conn) 128 { 129 if (!conn) 130 return NULL; 131 132 BT_DBG("conn %p refcnt %u", conn, kref_read(&conn->ref)); 133 134 if (!kref_get_unless_zero(&conn->ref)) 135 return NULL; 136 137 return conn; 138 } 139 140 static struct sock *iso_sock_hold(struct iso_conn *conn) 141 { 142 if (!conn || !bt_sock_linked(&iso_sk_list, conn->sk)) 143 return NULL; 144 145 sock_hold(conn->sk); 146 147 return conn->sk; 148 } 149 150 static void iso_sock_timeout(struct work_struct *work) 151 { 152 struct iso_conn *conn = container_of(work, struct iso_conn, 153 timeout_work.work); 154 struct sock *sk; 155 156 conn = iso_conn_hold_unless_zero(conn); 157 if (!conn) 158 return; 159 160 iso_conn_lock(conn); 161 sk = iso_sock_hold(conn); 162 iso_conn_unlock(conn); 163 iso_conn_put(conn); 164 165 if (!sk) 166 return; 167 168 BT_DBG("sock %p state %d", sk, sk->sk_state); 169 170 lock_sock(sk); 171 sk->sk_err = ETIMEDOUT; 172 sk->sk_state_change(sk); 173 release_sock(sk); 174 sock_put(sk); 175 } 176 177 static void iso_sock_set_timer(struct sock *sk, long timeout) 178 { 179 if (!iso_pi(sk)->conn) 180 return; 181 182 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout); 183 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work); 184 schedule_delayed_work(&iso_pi(sk)->conn->timeout_work, timeout); 185 } 186 187 static void iso_sock_clear_timer(struct sock *sk) 188 { 189 if (!iso_pi(sk)->conn) 190 return; 191 192 BT_DBG("sock %p state %d", sk, sk->sk_state); 193 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work); 194 } 195 196 /* ---- ISO connections ---- */ 197 static struct iso_conn *iso_conn_add(struct hci_conn *hcon) 198 { 199 struct iso_conn *conn = hcon->iso_data; 200 201 conn = iso_conn_hold_unless_zero(conn); 202 if (conn) { 203 if (!conn->hcon) { 204 iso_conn_lock(conn); 205 conn->hcon = hcon; 206 iso_conn_unlock(conn); 207 } 208 iso_conn_put(conn); 209 return conn; 210 } 211 212 conn = kzalloc(sizeof(*conn), GFP_KERNEL); 213 if (!conn) 214 return NULL; 215 216 kref_init(&conn->ref); 217 spin_lock_init(&conn->lock); 218 INIT_DELAYED_WORK(&conn->timeout_work, iso_sock_timeout); 219 220 hcon->iso_data = conn; 221 conn->hcon = hcon; 222 conn->tx_sn = 0; 223 224 BT_DBG("hcon %p conn %p", hcon, conn); 225 226 return conn; 227 } 228 229 /* Delete channel. Must be called on the locked socket. */ 230 static void iso_chan_del(struct sock *sk, int err) 231 { 232 struct iso_conn *conn; 233 struct sock *parent; 234 235 conn = iso_pi(sk)->conn; 236 iso_pi(sk)->conn = NULL; 237 238 BT_DBG("sk %p, conn %p, err %d", sk, conn, err); 239 240 if (conn) { 241 iso_conn_lock(conn); 242 conn->sk = NULL; 243 iso_conn_unlock(conn); 244 iso_conn_put(conn); 245 } 246 247 sk->sk_state = BT_CLOSED; 248 sk->sk_err = err; 249 250 parent = bt_sk(sk)->parent; 251 if (parent) { 252 bt_accept_unlink(sk); 253 parent->sk_data_ready(parent); 254 } else { 255 sk->sk_state_change(sk); 256 } 257 258 sock_set_flag(sk, SOCK_ZAPPED); 259 } 260 261 static void iso_conn_del(struct hci_conn *hcon, int err) 262 { 263 struct iso_conn *conn = hcon->iso_data; 264 struct sock *sk; 265 266 conn = iso_conn_hold_unless_zero(conn); 267 if (!conn) 268 return; 269 270 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 271 272 /* Kill socket */ 273 iso_conn_lock(conn); 274 sk = iso_sock_hold(conn); 275 iso_conn_unlock(conn); 276 iso_conn_put(conn); 277 278 if (!sk) { 279 iso_conn_put(conn); 280 return; 281 } 282 283 lock_sock(sk); 284 iso_sock_clear_timer(sk); 285 iso_chan_del(sk, err); 286 release_sock(sk); 287 sock_put(sk); 288 } 289 290 static int __iso_chan_add(struct iso_conn *conn, struct sock *sk, 291 struct sock *parent) 292 { 293 BT_DBG("conn %p", conn); 294 295 if (iso_pi(sk)->conn == conn && conn->sk == sk) 296 return 0; 297 298 if (conn->sk) { 299 BT_ERR("conn->sk already set"); 300 return -EBUSY; 301 } 302 303 iso_pi(sk)->conn = conn; 304 conn->sk = sk; 305 306 if (parent) 307 bt_accept_enqueue(parent, sk, true); 308 309 return 0; 310 } 311 312 static int iso_chan_add(struct iso_conn *conn, struct sock *sk, 313 struct sock *parent) 314 { 315 int err; 316 317 iso_conn_lock(conn); 318 err = __iso_chan_add(conn, sk, parent); 319 iso_conn_unlock(conn); 320 321 return err; 322 } 323 324 static inline u8 le_addr_type(u8 bdaddr_type) 325 { 326 if (bdaddr_type == BDADDR_LE_PUBLIC) 327 return ADDR_LE_DEV_PUBLIC; 328 else 329 return ADDR_LE_DEV_RANDOM; 330 } 331 332 static int iso_connect_bis(struct sock *sk) 333 { 334 struct iso_conn *conn; 335 struct hci_conn *hcon; 336 struct hci_dev *hdev; 337 int err; 338 339 BT_DBG("%pMR", &iso_pi(sk)->src); 340 341 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src, 342 iso_pi(sk)->src_type); 343 if (!hdev) 344 return -EHOSTUNREACH; 345 346 hci_dev_lock(hdev); 347 348 if (!bis_capable(hdev)) { 349 err = -EOPNOTSUPP; 350 goto unlock; 351 } 352 353 /* Fail if user set invalid QoS */ 354 if (iso_pi(sk)->qos_user_set && !check_bcast_qos(&iso_pi(sk)->qos)) { 355 iso_pi(sk)->qos = default_qos; 356 err = -EINVAL; 357 goto unlock; 358 } 359 360 /* Fail if out PHYs are marked as disabled */ 361 if (!iso_pi(sk)->qos.bcast.out.phy) { 362 err = -EINVAL; 363 goto unlock; 364 } 365 366 /* Just bind if DEFER_SETUP has been set */ 367 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { 368 hcon = hci_bind_bis(hdev, &iso_pi(sk)->dst, 369 &iso_pi(sk)->qos, iso_pi(sk)->base_len, 370 iso_pi(sk)->base); 371 if (IS_ERR(hcon)) { 372 err = PTR_ERR(hcon); 373 goto unlock; 374 } 375 } else { 376 hcon = hci_connect_bis(hdev, &iso_pi(sk)->dst, 377 le_addr_type(iso_pi(sk)->dst_type), 378 &iso_pi(sk)->qos, iso_pi(sk)->base_len, 379 iso_pi(sk)->base); 380 if (IS_ERR(hcon)) { 381 err = PTR_ERR(hcon); 382 goto unlock; 383 } 384 } 385 386 conn = iso_conn_add(hcon); 387 if (!conn) { 388 hci_conn_drop(hcon); 389 err = -ENOMEM; 390 goto unlock; 391 } 392 393 lock_sock(sk); 394 395 err = iso_chan_add(conn, sk, NULL); 396 if (err) { 397 release_sock(sk); 398 goto unlock; 399 } 400 401 /* Update source addr of the socket */ 402 bacpy(&iso_pi(sk)->src, &hcon->src); 403 404 if (hcon->state == BT_CONNECTED) { 405 iso_sock_clear_timer(sk); 406 sk->sk_state = BT_CONNECTED; 407 } else if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { 408 iso_sock_clear_timer(sk); 409 sk->sk_state = BT_CONNECT; 410 } else { 411 sk->sk_state = BT_CONNECT; 412 iso_sock_set_timer(sk, sk->sk_sndtimeo); 413 } 414 415 release_sock(sk); 416 417 unlock: 418 hci_dev_unlock(hdev); 419 hci_dev_put(hdev); 420 return err; 421 } 422 423 static int iso_connect_cis(struct sock *sk) 424 { 425 struct iso_conn *conn; 426 struct hci_conn *hcon; 427 struct hci_dev *hdev; 428 int err; 429 430 BT_DBG("%pMR -> %pMR", &iso_pi(sk)->src, &iso_pi(sk)->dst); 431 432 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src, 433 iso_pi(sk)->src_type); 434 if (!hdev) 435 return -EHOSTUNREACH; 436 437 hci_dev_lock(hdev); 438 439 if (!cis_central_capable(hdev)) { 440 err = -EOPNOTSUPP; 441 goto unlock; 442 } 443 444 /* Fail if user set invalid QoS */ 445 if (iso_pi(sk)->qos_user_set && !check_ucast_qos(&iso_pi(sk)->qos)) { 446 iso_pi(sk)->qos = default_qos; 447 err = -EINVAL; 448 goto unlock; 449 } 450 451 /* Fail if either PHYs are marked as disabled */ 452 if (!iso_pi(sk)->qos.ucast.in.phy && !iso_pi(sk)->qos.ucast.out.phy) { 453 err = -EINVAL; 454 goto unlock; 455 } 456 457 /* Just bind if DEFER_SETUP has been set */ 458 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { 459 hcon = hci_bind_cis(hdev, &iso_pi(sk)->dst, 460 le_addr_type(iso_pi(sk)->dst_type), 461 &iso_pi(sk)->qos); 462 if (IS_ERR(hcon)) { 463 err = PTR_ERR(hcon); 464 goto unlock; 465 } 466 } else { 467 hcon = hci_connect_cis(hdev, &iso_pi(sk)->dst, 468 le_addr_type(iso_pi(sk)->dst_type), 469 &iso_pi(sk)->qos); 470 if (IS_ERR(hcon)) { 471 err = PTR_ERR(hcon); 472 goto unlock; 473 } 474 } 475 476 conn = iso_conn_add(hcon); 477 if (!conn) { 478 hci_conn_drop(hcon); 479 err = -ENOMEM; 480 goto unlock; 481 } 482 483 lock_sock(sk); 484 485 err = iso_chan_add(conn, sk, NULL); 486 if (err) { 487 release_sock(sk); 488 goto unlock; 489 } 490 491 /* Update source addr of the socket */ 492 bacpy(&iso_pi(sk)->src, &hcon->src); 493 494 if (hcon->state == BT_CONNECTED) { 495 iso_sock_clear_timer(sk); 496 sk->sk_state = BT_CONNECTED; 497 } else if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { 498 iso_sock_clear_timer(sk); 499 sk->sk_state = BT_CONNECT; 500 } else { 501 sk->sk_state = BT_CONNECT; 502 iso_sock_set_timer(sk, sk->sk_sndtimeo); 503 } 504 505 release_sock(sk); 506 507 unlock: 508 hci_dev_unlock(hdev); 509 hci_dev_put(hdev); 510 return err; 511 } 512 513 static struct bt_iso_qos *iso_sock_get_qos(struct sock *sk) 514 { 515 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONNECT2) 516 return &iso_pi(sk)->conn->hcon->iso_qos; 517 518 return &iso_pi(sk)->qos; 519 } 520 521 static int iso_send_frame(struct sock *sk, struct sk_buff *skb) 522 { 523 struct iso_conn *conn = iso_pi(sk)->conn; 524 struct bt_iso_qos *qos = iso_sock_get_qos(sk); 525 struct hci_iso_data_hdr *hdr; 526 int len = 0; 527 528 BT_DBG("sk %p len %d", sk, skb->len); 529 530 if (skb->len > qos->ucast.out.sdu) 531 return -EMSGSIZE; 532 533 len = skb->len; 534 535 /* Push ISO data header */ 536 hdr = skb_push(skb, HCI_ISO_DATA_HDR_SIZE); 537 hdr->sn = cpu_to_le16(conn->tx_sn++); 538 hdr->slen = cpu_to_le16(hci_iso_data_len_pack(len, 539 HCI_ISO_STATUS_VALID)); 540 541 if (sk->sk_state == BT_CONNECTED) 542 hci_send_iso(conn->hcon, skb); 543 else 544 len = -ENOTCONN; 545 546 return len; 547 } 548 549 static void iso_recv_frame(struct iso_conn *conn, struct sk_buff *skb) 550 { 551 struct sock *sk; 552 553 iso_conn_lock(conn); 554 sk = conn->sk; 555 iso_conn_unlock(conn); 556 557 if (!sk) 558 goto drop; 559 560 BT_DBG("sk %p len %d", sk, skb->len); 561 562 if (sk->sk_state != BT_CONNECTED) 563 goto drop; 564 565 if (!sock_queue_rcv_skb(sk, skb)) 566 return; 567 568 drop: 569 kfree_skb(skb); 570 } 571 572 /* -------- Socket interface ---------- */ 573 static struct sock *__iso_get_sock_listen_by_addr(bdaddr_t *src, bdaddr_t *dst) 574 { 575 struct sock *sk; 576 577 sk_for_each(sk, &iso_sk_list.head) { 578 if (sk->sk_state != BT_LISTEN) 579 continue; 580 581 if (bacmp(&iso_pi(sk)->dst, dst)) 582 continue; 583 584 if (!bacmp(&iso_pi(sk)->src, src)) 585 return sk; 586 } 587 588 return NULL; 589 } 590 591 static struct sock *__iso_get_sock_listen_by_sid(bdaddr_t *ba, bdaddr_t *bc, 592 __u8 sid) 593 { 594 struct sock *sk; 595 596 sk_for_each(sk, &iso_sk_list.head) { 597 if (sk->sk_state != BT_LISTEN) 598 continue; 599 600 if (bacmp(&iso_pi(sk)->src, ba)) 601 continue; 602 603 if (bacmp(&iso_pi(sk)->dst, bc)) 604 continue; 605 606 if (iso_pi(sk)->bc_sid == sid) 607 return sk; 608 } 609 610 return NULL; 611 } 612 613 /* Find socket in given state: 614 * source bdaddr (Unicast) 615 * destination bdaddr (Broadcast only) 616 * match func - pass NULL to ignore 617 * match func data - pass -1 to ignore 618 * Returns closest match. 619 */ 620 static struct sock *iso_get_sock(bdaddr_t *src, bdaddr_t *dst, 621 enum bt_sock_state state, 622 iso_sock_match_t match, void *data) 623 { 624 struct sock *sk = NULL, *sk1 = NULL; 625 626 read_lock(&iso_sk_list.lock); 627 628 sk_for_each(sk, &iso_sk_list.head) { 629 if (sk->sk_state != state) 630 continue; 631 632 /* Match Broadcast destination */ 633 if (bacmp(dst, BDADDR_ANY) && bacmp(&iso_pi(sk)->dst, dst)) 634 continue; 635 636 /* Use Match function if provided */ 637 if (match && !match(sk, data)) 638 continue; 639 640 /* Exact match. */ 641 if (!bacmp(&iso_pi(sk)->src, src)) { 642 sock_hold(sk); 643 break; 644 } 645 646 /* Closest match */ 647 if (!bacmp(&iso_pi(sk)->src, BDADDR_ANY)) { 648 if (sk1) 649 sock_put(sk1); 650 651 sk1 = sk; 652 sock_hold(sk1); 653 } 654 } 655 656 if (sk && sk1) 657 sock_put(sk1); 658 659 read_unlock(&iso_sk_list.lock); 660 661 return sk ? sk : sk1; 662 } 663 664 static struct sock *iso_get_sock_big(struct sock *match_sk, bdaddr_t *src, 665 bdaddr_t *dst, uint8_t big) 666 { 667 struct sock *sk = NULL; 668 669 read_lock(&iso_sk_list.lock); 670 671 sk_for_each(sk, &iso_sk_list.head) { 672 if (match_sk == sk) 673 continue; 674 675 /* Look for sockets that have already been 676 * connected to the BIG 677 */ 678 if (sk->sk_state != BT_CONNECTED && 679 sk->sk_state != BT_CONNECT) 680 continue; 681 682 /* Match Broadcast destination */ 683 if (bacmp(&iso_pi(sk)->dst, dst)) 684 continue; 685 686 /* Match BIG handle */ 687 if (iso_pi(sk)->qos.bcast.big != big) 688 continue; 689 690 /* Match source address */ 691 if (bacmp(&iso_pi(sk)->src, src)) 692 continue; 693 694 sock_hold(sk); 695 break; 696 } 697 698 read_unlock(&iso_sk_list.lock); 699 700 return sk; 701 } 702 703 static void iso_sock_destruct(struct sock *sk) 704 { 705 BT_DBG("sk %p", sk); 706 707 iso_conn_put(iso_pi(sk)->conn); 708 709 skb_queue_purge(&sk->sk_receive_queue); 710 skb_queue_purge(&sk->sk_write_queue); 711 } 712 713 static void iso_sock_cleanup_listen(struct sock *parent) 714 { 715 struct sock *sk; 716 717 BT_DBG("parent %p", parent); 718 719 /* Close not yet accepted channels */ 720 while ((sk = bt_accept_dequeue(parent, NULL))) { 721 iso_sock_close(sk); 722 iso_sock_kill(sk); 723 } 724 725 /* If listening socket has a hcon, properly disconnect it */ 726 if (iso_pi(parent)->conn && iso_pi(parent)->conn->hcon) { 727 iso_sock_disconn(parent); 728 return; 729 } 730 731 parent->sk_state = BT_CLOSED; 732 sock_set_flag(parent, SOCK_ZAPPED); 733 } 734 735 /* Kill socket (only if zapped and orphan) 736 * Must be called on unlocked socket. 737 */ 738 static void iso_sock_kill(struct sock *sk) 739 { 740 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket || 741 sock_flag(sk, SOCK_DEAD)) 742 return; 743 744 BT_DBG("sk %p state %d", sk, sk->sk_state); 745 746 /* Kill poor orphan */ 747 bt_sock_unlink(&iso_sk_list, sk); 748 sock_set_flag(sk, SOCK_DEAD); 749 sock_put(sk); 750 } 751 752 static void iso_sock_disconn(struct sock *sk) 753 { 754 struct sock *bis_sk; 755 struct hci_conn *hcon = iso_pi(sk)->conn->hcon; 756 757 if (test_bit(HCI_CONN_BIG_CREATED, &hcon->flags)) { 758 bis_sk = iso_get_sock_big(sk, &iso_pi(sk)->src, 759 &iso_pi(sk)->dst, 760 iso_pi(sk)->qos.bcast.big); 761 762 /* If there are any other connected sockets for the 763 * same BIG, just delete the sk and leave the bis 764 * hcon active, in case later rebinding is needed. 765 */ 766 if (bis_sk) { 767 hcon->state = BT_OPEN; 768 hcon->iso_data = NULL; 769 iso_pi(sk)->conn->hcon = NULL; 770 iso_sock_clear_timer(sk); 771 iso_chan_del(sk, bt_to_errno(hcon->abort_reason)); 772 sock_put(bis_sk); 773 return; 774 } 775 } 776 777 sk->sk_state = BT_DISCONN; 778 iso_conn_lock(iso_pi(sk)->conn); 779 hci_conn_drop(iso_pi(sk)->conn->hcon); 780 iso_pi(sk)->conn->hcon = NULL; 781 iso_conn_unlock(iso_pi(sk)->conn); 782 } 783 784 static void __iso_sock_close(struct sock *sk) 785 { 786 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket); 787 788 switch (sk->sk_state) { 789 case BT_LISTEN: 790 iso_sock_cleanup_listen(sk); 791 break; 792 793 case BT_CONNECT: 794 case BT_CONNECTED: 795 case BT_CONFIG: 796 if (iso_pi(sk)->conn->hcon) 797 iso_sock_disconn(sk); 798 else 799 iso_chan_del(sk, ECONNRESET); 800 break; 801 802 case BT_CONNECT2: 803 if (iso_pi(sk)->conn->hcon && 804 (test_bit(HCI_CONN_PA_SYNC, &iso_pi(sk)->conn->hcon->flags) || 805 test_bit(HCI_CONN_PA_SYNC_FAILED, &iso_pi(sk)->conn->hcon->flags))) 806 iso_sock_disconn(sk); 807 else 808 iso_chan_del(sk, ECONNRESET); 809 break; 810 case BT_DISCONN: 811 iso_chan_del(sk, ECONNRESET); 812 break; 813 814 default: 815 sock_set_flag(sk, SOCK_ZAPPED); 816 break; 817 } 818 } 819 820 /* Must be called on unlocked socket. */ 821 static void iso_sock_close(struct sock *sk) 822 { 823 iso_sock_clear_timer(sk); 824 lock_sock(sk); 825 __iso_sock_close(sk); 826 release_sock(sk); 827 iso_sock_kill(sk); 828 } 829 830 static void iso_sock_init(struct sock *sk, struct sock *parent) 831 { 832 BT_DBG("sk %p", sk); 833 834 if (parent) { 835 sk->sk_type = parent->sk_type; 836 bt_sk(sk)->flags = bt_sk(parent)->flags; 837 security_sk_clone(parent, sk); 838 } 839 } 840 841 static struct proto iso_proto = { 842 .name = "ISO", 843 .owner = THIS_MODULE, 844 .obj_size = sizeof(struct iso_pinfo) 845 }; 846 847 #define DEFAULT_IO_QOS \ 848 { \ 849 .interval = 10000u, \ 850 .latency = 10u, \ 851 .sdu = 40u, \ 852 .phy = BT_ISO_PHY_2M, \ 853 .rtn = 2u, \ 854 } 855 856 static struct bt_iso_qos default_qos = { 857 .bcast = { 858 .big = BT_ISO_QOS_BIG_UNSET, 859 .bis = BT_ISO_QOS_BIS_UNSET, 860 .sync_factor = 0x01, 861 .packing = 0x00, 862 .framing = 0x00, 863 .in = DEFAULT_IO_QOS, 864 .out = DEFAULT_IO_QOS, 865 .encryption = 0x00, 866 .bcode = {0x00}, 867 .options = 0x00, 868 .skip = 0x0000, 869 .sync_timeout = BT_ISO_SYNC_TIMEOUT, 870 .sync_cte_type = 0x00, 871 .mse = 0x00, 872 .timeout = BT_ISO_SYNC_TIMEOUT, 873 }, 874 }; 875 876 static struct sock *iso_sock_alloc(struct net *net, struct socket *sock, 877 int proto, gfp_t prio, int kern) 878 { 879 struct sock *sk; 880 881 sk = bt_sock_alloc(net, sock, &iso_proto, proto, prio, kern); 882 if (!sk) 883 return NULL; 884 885 sk->sk_destruct = iso_sock_destruct; 886 sk->sk_sndtimeo = ISO_CONN_TIMEOUT; 887 888 /* Set address type as public as default src address is BDADDR_ANY */ 889 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC; 890 891 iso_pi(sk)->qos = default_qos; 892 iso_pi(sk)->sync_handle = -1; 893 894 bt_sock_link(&iso_sk_list, sk); 895 return sk; 896 } 897 898 static int iso_sock_create(struct net *net, struct socket *sock, int protocol, 899 int kern) 900 { 901 struct sock *sk; 902 903 BT_DBG("sock %p", sock); 904 905 sock->state = SS_UNCONNECTED; 906 907 if (sock->type != SOCK_SEQPACKET) 908 return -ESOCKTNOSUPPORT; 909 910 sock->ops = &iso_sock_ops; 911 912 sk = iso_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern); 913 if (!sk) 914 return -ENOMEM; 915 916 iso_sock_init(sk, NULL); 917 return 0; 918 } 919 920 static int iso_sock_bind_bc(struct socket *sock, struct sockaddr *addr, 921 int addr_len) 922 { 923 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr; 924 struct sock *sk = sock->sk; 925 int i; 926 927 BT_DBG("sk %p bc_sid %u bc_num_bis %u", sk, sa->iso_bc->bc_sid, 928 sa->iso_bc->bc_num_bis); 929 930 if (addr_len != sizeof(*sa) + sizeof(*sa->iso_bc)) 931 return -EINVAL; 932 933 bacpy(&iso_pi(sk)->dst, &sa->iso_bc->bc_bdaddr); 934 935 /* Check if the address type is of LE type */ 936 if (!bdaddr_type_is_le(sa->iso_bc->bc_bdaddr_type)) 937 return -EINVAL; 938 939 iso_pi(sk)->dst_type = sa->iso_bc->bc_bdaddr_type; 940 941 if (sa->iso_bc->bc_sid > 0x0f) 942 return -EINVAL; 943 944 iso_pi(sk)->bc_sid = sa->iso_bc->bc_sid; 945 946 if (sa->iso_bc->bc_num_bis > ISO_MAX_NUM_BIS) 947 return -EINVAL; 948 949 iso_pi(sk)->bc_num_bis = sa->iso_bc->bc_num_bis; 950 951 for (i = 0; i < iso_pi(sk)->bc_num_bis; i++) 952 if (sa->iso_bc->bc_bis[i] < 0x01 || 953 sa->iso_bc->bc_bis[i] > 0x1f) 954 return -EINVAL; 955 956 memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis, 957 iso_pi(sk)->bc_num_bis); 958 959 return 0; 960 } 961 962 static int iso_sock_bind_pa_sk(struct sock *sk, struct sockaddr_iso *sa, 963 int addr_len) 964 { 965 int err = 0; 966 967 if (sk->sk_type != SOCK_SEQPACKET) { 968 err = -EINVAL; 969 goto done; 970 } 971 972 if (addr_len != sizeof(*sa) + sizeof(*sa->iso_bc)) { 973 err = -EINVAL; 974 goto done; 975 } 976 977 if (sa->iso_bc->bc_num_bis > ISO_MAX_NUM_BIS) { 978 err = -EINVAL; 979 goto done; 980 } 981 982 iso_pi(sk)->bc_num_bis = sa->iso_bc->bc_num_bis; 983 984 for (int i = 0; i < iso_pi(sk)->bc_num_bis; i++) 985 if (sa->iso_bc->bc_bis[i] < 0x01 || 986 sa->iso_bc->bc_bis[i] > 0x1f) { 987 err = -EINVAL; 988 goto done; 989 } 990 991 memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis, 992 iso_pi(sk)->bc_num_bis); 993 994 done: 995 return err; 996 } 997 998 static int iso_sock_bind(struct socket *sock, struct sockaddr *addr, 999 int addr_len) 1000 { 1001 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr; 1002 struct sock *sk = sock->sk; 1003 int err = 0; 1004 1005 BT_DBG("sk %p %pMR type %u", sk, &sa->iso_bdaddr, sa->iso_bdaddr_type); 1006 1007 if (!addr || addr_len < sizeof(struct sockaddr_iso) || 1008 addr->sa_family != AF_BLUETOOTH) 1009 return -EINVAL; 1010 1011 lock_sock(sk); 1012 1013 /* Allow the user to bind a PA sync socket to a number 1014 * of BISes to sync to. 1015 */ 1016 if ((sk->sk_state == BT_CONNECT2 || 1017 sk->sk_state == BT_CONNECTED) && 1018 test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags)) { 1019 err = iso_sock_bind_pa_sk(sk, sa, addr_len); 1020 goto done; 1021 } 1022 1023 if (sk->sk_state != BT_OPEN) { 1024 err = -EBADFD; 1025 goto done; 1026 } 1027 1028 if (sk->sk_type != SOCK_SEQPACKET) { 1029 err = -EINVAL; 1030 goto done; 1031 } 1032 1033 /* Check if the address type is of LE type */ 1034 if (!bdaddr_type_is_le(sa->iso_bdaddr_type)) { 1035 err = -EINVAL; 1036 goto done; 1037 } 1038 1039 bacpy(&iso_pi(sk)->src, &sa->iso_bdaddr); 1040 iso_pi(sk)->src_type = sa->iso_bdaddr_type; 1041 1042 /* Check for Broadcast address */ 1043 if (addr_len > sizeof(*sa)) { 1044 err = iso_sock_bind_bc(sock, addr, addr_len); 1045 if (err) 1046 goto done; 1047 } 1048 1049 sk->sk_state = BT_BOUND; 1050 1051 done: 1052 release_sock(sk); 1053 return err; 1054 } 1055 1056 static int iso_sock_connect(struct socket *sock, struct sockaddr *addr, 1057 int alen, int flags) 1058 { 1059 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr; 1060 struct sock *sk = sock->sk; 1061 int err; 1062 1063 BT_DBG("sk %p", sk); 1064 1065 if (alen < sizeof(struct sockaddr_iso) || 1066 addr->sa_family != AF_BLUETOOTH) 1067 return -EINVAL; 1068 1069 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) 1070 return -EBADFD; 1071 1072 if (sk->sk_type != SOCK_SEQPACKET) 1073 return -EINVAL; 1074 1075 /* Check if the address type is of LE type */ 1076 if (!bdaddr_type_is_le(sa->iso_bdaddr_type)) 1077 return -EINVAL; 1078 1079 lock_sock(sk); 1080 1081 bacpy(&iso_pi(sk)->dst, &sa->iso_bdaddr); 1082 iso_pi(sk)->dst_type = sa->iso_bdaddr_type; 1083 1084 release_sock(sk); 1085 1086 if (bacmp(&iso_pi(sk)->dst, BDADDR_ANY)) 1087 err = iso_connect_cis(sk); 1088 else 1089 err = iso_connect_bis(sk); 1090 1091 if (err) 1092 return err; 1093 1094 lock_sock(sk); 1095 1096 if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { 1097 err = bt_sock_wait_state(sk, BT_CONNECTED, 1098 sock_sndtimeo(sk, flags & O_NONBLOCK)); 1099 } 1100 1101 release_sock(sk); 1102 return err; 1103 } 1104 1105 static int iso_listen_bis(struct sock *sk) 1106 { 1107 struct hci_dev *hdev; 1108 int err = 0; 1109 struct iso_conn *conn; 1110 struct hci_conn *hcon; 1111 1112 BT_DBG("%pMR -> %pMR (SID 0x%2.2x)", &iso_pi(sk)->src, 1113 &iso_pi(sk)->dst, iso_pi(sk)->bc_sid); 1114 1115 write_lock(&iso_sk_list.lock); 1116 1117 if (__iso_get_sock_listen_by_sid(&iso_pi(sk)->src, &iso_pi(sk)->dst, 1118 iso_pi(sk)->bc_sid)) 1119 err = -EADDRINUSE; 1120 1121 write_unlock(&iso_sk_list.lock); 1122 1123 if (err) 1124 return err; 1125 1126 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src, 1127 iso_pi(sk)->src_type); 1128 if (!hdev) 1129 return -EHOSTUNREACH; 1130 1131 hci_dev_lock(hdev); 1132 1133 /* Fail if user set invalid QoS */ 1134 if (iso_pi(sk)->qos_user_set && !check_bcast_qos(&iso_pi(sk)->qos)) { 1135 iso_pi(sk)->qos = default_qos; 1136 err = -EINVAL; 1137 goto unlock; 1138 } 1139 1140 hcon = hci_pa_create_sync(hdev, &iso_pi(sk)->dst, 1141 le_addr_type(iso_pi(sk)->dst_type), 1142 iso_pi(sk)->bc_sid, &iso_pi(sk)->qos); 1143 if (IS_ERR(hcon)) { 1144 err = PTR_ERR(hcon); 1145 goto unlock; 1146 } 1147 1148 conn = iso_conn_add(hcon); 1149 if (!conn) { 1150 hci_conn_drop(hcon); 1151 err = -ENOMEM; 1152 goto unlock; 1153 } 1154 1155 err = iso_chan_add(conn, sk, NULL); 1156 if (err) { 1157 hci_conn_drop(hcon); 1158 goto unlock; 1159 } 1160 1161 hci_dev_put(hdev); 1162 1163 unlock: 1164 hci_dev_unlock(hdev); 1165 return err; 1166 } 1167 1168 static int iso_listen_cis(struct sock *sk) 1169 { 1170 int err = 0; 1171 1172 BT_DBG("%pMR", &iso_pi(sk)->src); 1173 1174 write_lock(&iso_sk_list.lock); 1175 1176 if (__iso_get_sock_listen_by_addr(&iso_pi(sk)->src, &iso_pi(sk)->dst)) 1177 err = -EADDRINUSE; 1178 1179 write_unlock(&iso_sk_list.lock); 1180 1181 return err; 1182 } 1183 1184 static int iso_sock_listen(struct socket *sock, int backlog) 1185 { 1186 struct sock *sk = sock->sk; 1187 int err = 0; 1188 1189 BT_DBG("sk %p backlog %d", sk, backlog); 1190 1191 lock_sock(sk); 1192 1193 if (sk->sk_state != BT_BOUND) { 1194 err = -EBADFD; 1195 goto done; 1196 } 1197 1198 if (sk->sk_type != SOCK_SEQPACKET) { 1199 err = -EINVAL; 1200 goto done; 1201 } 1202 1203 if (!bacmp(&iso_pi(sk)->dst, BDADDR_ANY)) 1204 err = iso_listen_cis(sk); 1205 else 1206 err = iso_listen_bis(sk); 1207 1208 if (err) 1209 goto done; 1210 1211 sk->sk_max_ack_backlog = backlog; 1212 sk->sk_ack_backlog = 0; 1213 1214 sk->sk_state = BT_LISTEN; 1215 1216 done: 1217 release_sock(sk); 1218 return err; 1219 } 1220 1221 static int iso_sock_accept(struct socket *sock, struct socket *newsock, 1222 struct proto_accept_arg *arg) 1223 { 1224 DEFINE_WAIT_FUNC(wait, woken_wake_function); 1225 struct sock *sk = sock->sk, *ch; 1226 long timeo; 1227 int err = 0; 1228 1229 lock_sock(sk); 1230 1231 timeo = sock_rcvtimeo(sk, arg->flags & O_NONBLOCK); 1232 1233 BT_DBG("sk %p timeo %ld", sk, timeo); 1234 1235 /* Wait for an incoming connection. (wake-one). */ 1236 add_wait_queue_exclusive(sk_sleep(sk), &wait); 1237 while (1) { 1238 if (sk->sk_state != BT_LISTEN) { 1239 err = -EBADFD; 1240 break; 1241 } 1242 1243 ch = bt_accept_dequeue(sk, newsock); 1244 if (ch) 1245 break; 1246 1247 if (!timeo) { 1248 err = -EAGAIN; 1249 break; 1250 } 1251 1252 if (signal_pending(current)) { 1253 err = sock_intr_errno(timeo); 1254 break; 1255 } 1256 1257 release_sock(sk); 1258 1259 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo); 1260 lock_sock(sk); 1261 } 1262 remove_wait_queue(sk_sleep(sk), &wait); 1263 1264 if (err) 1265 goto done; 1266 1267 newsock->state = SS_CONNECTED; 1268 1269 BT_DBG("new socket %p", ch); 1270 1271 done: 1272 release_sock(sk); 1273 return err; 1274 } 1275 1276 static int iso_sock_getname(struct socket *sock, struct sockaddr *addr, 1277 int peer) 1278 { 1279 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr; 1280 struct sock *sk = sock->sk; 1281 1282 BT_DBG("sock %p, sk %p", sock, sk); 1283 1284 addr->sa_family = AF_BLUETOOTH; 1285 1286 if (peer) { 1287 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->dst); 1288 sa->iso_bdaddr_type = iso_pi(sk)->dst_type; 1289 } else { 1290 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->src); 1291 sa->iso_bdaddr_type = iso_pi(sk)->src_type; 1292 } 1293 1294 return sizeof(struct sockaddr_iso); 1295 } 1296 1297 static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg, 1298 size_t len) 1299 { 1300 struct sock *sk = sock->sk; 1301 struct sk_buff *skb, **frag; 1302 size_t mtu; 1303 int err; 1304 1305 BT_DBG("sock %p, sk %p", sock, sk); 1306 1307 err = sock_error(sk); 1308 if (err) 1309 return err; 1310 1311 if (msg->msg_flags & MSG_OOB) 1312 return -EOPNOTSUPP; 1313 1314 lock_sock(sk); 1315 1316 if (sk->sk_state != BT_CONNECTED) { 1317 release_sock(sk); 1318 return -ENOTCONN; 1319 } 1320 1321 mtu = iso_pi(sk)->conn->hcon->mtu; 1322 1323 release_sock(sk); 1324 1325 skb = bt_skb_sendmsg(sk, msg, len, mtu, HCI_ISO_DATA_HDR_SIZE, 0); 1326 if (IS_ERR(skb)) 1327 return PTR_ERR(skb); 1328 1329 len -= skb->len; 1330 1331 BT_DBG("skb %p len %d", sk, skb->len); 1332 1333 /* Continuation fragments */ 1334 frag = &skb_shinfo(skb)->frag_list; 1335 while (len) { 1336 struct sk_buff *tmp; 1337 1338 tmp = bt_skb_sendmsg(sk, msg, len, mtu, 0, 0); 1339 if (IS_ERR(tmp)) { 1340 kfree_skb(skb); 1341 return PTR_ERR(tmp); 1342 } 1343 1344 *frag = tmp; 1345 1346 len -= tmp->len; 1347 1348 skb->len += tmp->len; 1349 skb->data_len += tmp->len; 1350 1351 BT_DBG("frag %p len %d", *frag, tmp->len); 1352 1353 frag = &(*frag)->next; 1354 } 1355 1356 lock_sock(sk); 1357 1358 if (sk->sk_state == BT_CONNECTED) 1359 err = iso_send_frame(sk, skb); 1360 else 1361 err = -ENOTCONN; 1362 1363 release_sock(sk); 1364 1365 if (err < 0) 1366 kfree_skb(skb); 1367 return err; 1368 } 1369 1370 static void iso_conn_defer_accept(struct hci_conn *conn) 1371 { 1372 struct hci_cp_le_accept_cis cp; 1373 struct hci_dev *hdev = conn->hdev; 1374 1375 BT_DBG("conn %p", conn); 1376 1377 conn->state = BT_CONFIG; 1378 1379 cp.handle = cpu_to_le16(conn->handle); 1380 1381 hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp); 1382 } 1383 1384 static void iso_conn_big_sync(struct sock *sk) 1385 { 1386 int err; 1387 struct hci_dev *hdev; 1388 1389 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src, 1390 iso_pi(sk)->src_type); 1391 1392 if (!hdev) 1393 return; 1394 1395 /* hci_le_big_create_sync requires hdev lock to be held, since 1396 * it enqueues the HCI LE BIG Create Sync command via 1397 * hci_cmd_sync_queue_once, which checks hdev flags that might 1398 * change. 1399 */ 1400 hci_dev_lock(hdev); 1401 1402 if (!test_and_set_bit(BT_SK_BIG_SYNC, &iso_pi(sk)->flags)) { 1403 err = hci_le_big_create_sync(hdev, iso_pi(sk)->conn->hcon, 1404 &iso_pi(sk)->qos, 1405 iso_pi(sk)->sync_handle, 1406 iso_pi(sk)->bc_num_bis, 1407 iso_pi(sk)->bc_bis); 1408 if (err) 1409 bt_dev_err(hdev, "hci_le_big_create_sync: %d", 1410 err); 1411 } 1412 1413 hci_dev_unlock(hdev); 1414 } 1415 1416 static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg, 1417 size_t len, int flags) 1418 { 1419 struct sock *sk = sock->sk; 1420 struct iso_pinfo *pi = iso_pi(sk); 1421 1422 BT_DBG("sk %p", sk); 1423 1424 if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { 1425 lock_sock(sk); 1426 switch (sk->sk_state) { 1427 case BT_CONNECT2: 1428 if (test_bit(BT_SK_PA_SYNC, &pi->flags)) { 1429 iso_conn_big_sync(sk); 1430 sk->sk_state = BT_LISTEN; 1431 } else { 1432 iso_conn_defer_accept(pi->conn->hcon); 1433 sk->sk_state = BT_CONFIG; 1434 } 1435 release_sock(sk); 1436 return 0; 1437 case BT_CONNECTED: 1438 if (test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags)) { 1439 iso_conn_big_sync(sk); 1440 sk->sk_state = BT_LISTEN; 1441 release_sock(sk); 1442 return 0; 1443 } 1444 1445 release_sock(sk); 1446 break; 1447 case BT_CONNECT: 1448 release_sock(sk); 1449 return iso_connect_cis(sk); 1450 default: 1451 release_sock(sk); 1452 break; 1453 } 1454 } 1455 1456 return bt_sock_recvmsg(sock, msg, len, flags); 1457 } 1458 1459 static bool check_io_qos(struct bt_iso_io_qos *qos) 1460 { 1461 /* If no PHY is enable SDU must be 0 */ 1462 if (!qos->phy && qos->sdu) 1463 return false; 1464 1465 if (qos->interval && (qos->interval < 0xff || qos->interval > 0xfffff)) 1466 return false; 1467 1468 if (qos->latency && (qos->latency < 0x05 || qos->latency > 0xfa0)) 1469 return false; 1470 1471 if (qos->phy > BT_ISO_PHY_ANY) 1472 return false; 1473 1474 return true; 1475 } 1476 1477 static bool check_ucast_qos(struct bt_iso_qos *qos) 1478 { 1479 if (qos->ucast.cig > 0xef && qos->ucast.cig != BT_ISO_QOS_CIG_UNSET) 1480 return false; 1481 1482 if (qos->ucast.cis > 0xef && qos->ucast.cis != BT_ISO_QOS_CIS_UNSET) 1483 return false; 1484 1485 if (qos->ucast.sca > 0x07) 1486 return false; 1487 1488 if (qos->ucast.packing > 0x01) 1489 return false; 1490 1491 if (qos->ucast.framing > 0x01) 1492 return false; 1493 1494 if (!check_io_qos(&qos->ucast.in)) 1495 return false; 1496 1497 if (!check_io_qos(&qos->ucast.out)) 1498 return false; 1499 1500 return true; 1501 } 1502 1503 static bool check_bcast_qos(struct bt_iso_qos *qos) 1504 { 1505 if (!qos->bcast.sync_factor) 1506 qos->bcast.sync_factor = 0x01; 1507 1508 if (qos->bcast.packing > 0x01) 1509 return false; 1510 1511 if (qos->bcast.framing > 0x01) 1512 return false; 1513 1514 if (!check_io_qos(&qos->bcast.in)) 1515 return false; 1516 1517 if (!check_io_qos(&qos->bcast.out)) 1518 return false; 1519 1520 if (qos->bcast.encryption > 0x01) 1521 return false; 1522 1523 if (qos->bcast.options > 0x07) 1524 return false; 1525 1526 if (qos->bcast.skip > 0x01f3) 1527 return false; 1528 1529 if (!qos->bcast.sync_timeout) 1530 qos->bcast.sync_timeout = BT_ISO_SYNC_TIMEOUT; 1531 1532 if (qos->bcast.sync_timeout < 0x000a || qos->bcast.sync_timeout > 0x4000) 1533 return false; 1534 1535 if (qos->bcast.sync_cte_type > 0x1f) 1536 return false; 1537 1538 if (qos->bcast.mse > 0x1f) 1539 return false; 1540 1541 if (!qos->bcast.timeout) 1542 qos->bcast.sync_timeout = BT_ISO_SYNC_TIMEOUT; 1543 1544 if (qos->bcast.timeout < 0x000a || qos->bcast.timeout > 0x4000) 1545 return false; 1546 1547 return true; 1548 } 1549 1550 static int iso_sock_setsockopt(struct socket *sock, int level, int optname, 1551 sockptr_t optval, unsigned int optlen) 1552 { 1553 struct sock *sk = sock->sk; 1554 int err = 0; 1555 struct bt_iso_qos qos = default_qos; 1556 u32 opt; 1557 1558 BT_DBG("sk %p", sk); 1559 1560 lock_sock(sk); 1561 1562 switch (optname) { 1563 case BT_DEFER_SETUP: 1564 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { 1565 err = -EINVAL; 1566 break; 1567 } 1568 1569 err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, optlen); 1570 if (err) 1571 break; 1572 1573 if (opt) 1574 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); 1575 else 1576 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); 1577 break; 1578 1579 case BT_PKT_STATUS: 1580 err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, optlen); 1581 if (err) 1582 break; 1583 1584 if (opt) 1585 set_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags); 1586 else 1587 clear_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags); 1588 break; 1589 1590 case BT_ISO_QOS: 1591 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND && 1592 sk->sk_state != BT_CONNECT2 && 1593 (!test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags) || 1594 sk->sk_state != BT_CONNECTED)) { 1595 err = -EINVAL; 1596 break; 1597 } 1598 1599 err = bt_copy_from_sockptr(&qos, sizeof(qos), optval, optlen); 1600 if (err) 1601 break; 1602 1603 iso_pi(sk)->qos = qos; 1604 iso_pi(sk)->qos_user_set = true; 1605 1606 break; 1607 1608 case BT_ISO_BASE: 1609 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND && 1610 sk->sk_state != BT_CONNECT2) { 1611 err = -EINVAL; 1612 break; 1613 } 1614 1615 if (optlen > sizeof(iso_pi(sk)->base)) { 1616 err = -EINVAL; 1617 break; 1618 } 1619 1620 err = bt_copy_from_sockptr(iso_pi(sk)->base, optlen, optval, 1621 optlen); 1622 if (err) 1623 break; 1624 1625 iso_pi(sk)->base_len = optlen; 1626 1627 break; 1628 1629 default: 1630 err = -ENOPROTOOPT; 1631 break; 1632 } 1633 1634 release_sock(sk); 1635 return err; 1636 } 1637 1638 static int iso_sock_getsockopt(struct socket *sock, int level, int optname, 1639 char __user *optval, int __user *optlen) 1640 { 1641 struct sock *sk = sock->sk; 1642 int len, err = 0; 1643 struct bt_iso_qos *qos; 1644 u8 base_len; 1645 u8 *base; 1646 1647 BT_DBG("sk %p", sk); 1648 1649 if (get_user(len, optlen)) 1650 return -EFAULT; 1651 1652 lock_sock(sk); 1653 1654 switch (optname) { 1655 case BT_DEFER_SETUP: 1656 if (sk->sk_state == BT_CONNECTED) { 1657 err = -EINVAL; 1658 break; 1659 } 1660 1661 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags), 1662 (u32 __user *)optval)) 1663 err = -EFAULT; 1664 1665 break; 1666 1667 case BT_PKT_STATUS: 1668 if (put_user(test_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags), 1669 (int __user *)optval)) 1670 err = -EFAULT; 1671 break; 1672 1673 case BT_ISO_QOS: 1674 qos = iso_sock_get_qos(sk); 1675 1676 len = min_t(unsigned int, len, sizeof(*qos)); 1677 if (copy_to_user(optval, qos, len)) 1678 err = -EFAULT; 1679 1680 break; 1681 1682 case BT_ISO_BASE: 1683 if (sk->sk_state == BT_CONNECTED && 1684 !bacmp(&iso_pi(sk)->dst, BDADDR_ANY)) { 1685 base_len = iso_pi(sk)->conn->hcon->le_per_adv_data_len; 1686 base = iso_pi(sk)->conn->hcon->le_per_adv_data; 1687 } else { 1688 base_len = iso_pi(sk)->base_len; 1689 base = iso_pi(sk)->base; 1690 } 1691 1692 len = min_t(unsigned int, len, base_len); 1693 if (copy_to_user(optval, base, len)) 1694 err = -EFAULT; 1695 if (put_user(len, optlen)) 1696 err = -EFAULT; 1697 1698 break; 1699 1700 default: 1701 err = -ENOPROTOOPT; 1702 break; 1703 } 1704 1705 release_sock(sk); 1706 return err; 1707 } 1708 1709 static int iso_sock_shutdown(struct socket *sock, int how) 1710 { 1711 struct sock *sk = sock->sk; 1712 int err = 0; 1713 1714 BT_DBG("sock %p, sk %p, how %d", sock, sk, how); 1715 1716 if (!sk) 1717 return 0; 1718 1719 sock_hold(sk); 1720 lock_sock(sk); 1721 1722 switch (how) { 1723 case SHUT_RD: 1724 if (sk->sk_shutdown & RCV_SHUTDOWN) 1725 goto unlock; 1726 sk->sk_shutdown |= RCV_SHUTDOWN; 1727 break; 1728 case SHUT_WR: 1729 if (sk->sk_shutdown & SEND_SHUTDOWN) 1730 goto unlock; 1731 sk->sk_shutdown |= SEND_SHUTDOWN; 1732 break; 1733 case SHUT_RDWR: 1734 if (sk->sk_shutdown & SHUTDOWN_MASK) 1735 goto unlock; 1736 sk->sk_shutdown |= SHUTDOWN_MASK; 1737 break; 1738 } 1739 1740 iso_sock_clear_timer(sk); 1741 __iso_sock_close(sk); 1742 1743 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime && 1744 !(current->flags & PF_EXITING)) 1745 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime); 1746 1747 unlock: 1748 release_sock(sk); 1749 sock_put(sk); 1750 1751 return err; 1752 } 1753 1754 static int iso_sock_release(struct socket *sock) 1755 { 1756 struct sock *sk = sock->sk; 1757 int err = 0; 1758 1759 BT_DBG("sock %p, sk %p", sock, sk); 1760 1761 if (!sk) 1762 return 0; 1763 1764 iso_sock_close(sk); 1765 1766 if (sock_flag(sk, SOCK_LINGER) && READ_ONCE(sk->sk_lingertime) && 1767 !(current->flags & PF_EXITING)) { 1768 lock_sock(sk); 1769 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime); 1770 release_sock(sk); 1771 } 1772 1773 sock_orphan(sk); 1774 iso_sock_kill(sk); 1775 return err; 1776 } 1777 1778 static void iso_sock_ready(struct sock *sk) 1779 { 1780 BT_DBG("sk %p", sk); 1781 1782 if (!sk) 1783 return; 1784 1785 lock_sock(sk); 1786 iso_sock_clear_timer(sk); 1787 sk->sk_state = BT_CONNECTED; 1788 sk->sk_state_change(sk); 1789 release_sock(sk); 1790 } 1791 1792 static bool iso_match_big(struct sock *sk, void *data) 1793 { 1794 struct hci_evt_le_big_sync_estabilished *ev = data; 1795 1796 return ev->handle == iso_pi(sk)->qos.bcast.big; 1797 } 1798 1799 static bool iso_match_big_hcon(struct sock *sk, void *data) 1800 { 1801 struct hci_conn *hcon = data; 1802 1803 return hcon->iso_qos.bcast.big == iso_pi(sk)->qos.bcast.big; 1804 } 1805 1806 static bool iso_match_pa_sync_flag(struct sock *sk, void *data) 1807 { 1808 return test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags); 1809 } 1810 1811 static void iso_conn_ready(struct iso_conn *conn) 1812 { 1813 struct sock *parent = NULL; 1814 struct sock *sk = conn->sk; 1815 struct hci_ev_le_big_sync_estabilished *ev = NULL; 1816 struct hci_ev_le_pa_sync_established *ev2 = NULL; 1817 struct hci_ev_le_per_adv_report *ev3 = NULL; 1818 struct hci_conn *hcon; 1819 1820 BT_DBG("conn %p", conn); 1821 1822 if (sk) { 1823 iso_sock_ready(conn->sk); 1824 } else { 1825 hcon = conn->hcon; 1826 if (!hcon) 1827 return; 1828 1829 if (test_bit(HCI_CONN_BIG_SYNC, &hcon->flags)) { 1830 /* A BIS slave hcon is notified to the ISO layer 1831 * after the Command Complete for the LE Setup 1832 * ISO Data Path command is received. Get the 1833 * parent socket that matches the hcon BIG handle. 1834 */ 1835 parent = iso_get_sock(&hcon->src, &hcon->dst, 1836 BT_LISTEN, iso_match_big_hcon, 1837 hcon); 1838 } else if (test_bit(HCI_CONN_BIG_SYNC_FAILED, &hcon->flags)) { 1839 ev = hci_recv_event_data(hcon->hdev, 1840 HCI_EVT_LE_BIG_SYNC_ESTABILISHED); 1841 1842 /* Get reference to PA sync parent socket, if it exists */ 1843 parent = iso_get_sock(&hcon->src, &hcon->dst, 1844 BT_LISTEN, 1845 iso_match_pa_sync_flag, 1846 NULL); 1847 if (!parent && ev) 1848 parent = iso_get_sock(&hcon->src, 1849 &hcon->dst, 1850 BT_LISTEN, 1851 iso_match_big, ev); 1852 } else if (test_bit(HCI_CONN_PA_SYNC_FAILED, &hcon->flags)) { 1853 ev2 = hci_recv_event_data(hcon->hdev, 1854 HCI_EV_LE_PA_SYNC_ESTABLISHED); 1855 if (ev2) 1856 parent = iso_get_sock(&hcon->src, 1857 &hcon->dst, 1858 BT_LISTEN, 1859 iso_match_sid, ev2); 1860 } else if (test_bit(HCI_CONN_PA_SYNC, &hcon->flags)) { 1861 ev3 = hci_recv_event_data(hcon->hdev, 1862 HCI_EV_LE_PER_ADV_REPORT); 1863 if (ev3) 1864 parent = iso_get_sock(&hcon->src, 1865 &hcon->dst, 1866 BT_LISTEN, 1867 iso_match_sync_handle_pa_report, 1868 ev3); 1869 } 1870 1871 if (!parent) 1872 parent = iso_get_sock(&hcon->src, BDADDR_ANY, 1873 BT_LISTEN, NULL, NULL); 1874 1875 if (!parent) 1876 return; 1877 1878 lock_sock(parent); 1879 1880 sk = iso_sock_alloc(sock_net(parent), NULL, 1881 BTPROTO_ISO, GFP_ATOMIC, 0); 1882 if (!sk) { 1883 release_sock(parent); 1884 return; 1885 } 1886 1887 iso_sock_init(sk, parent); 1888 1889 bacpy(&iso_pi(sk)->src, &hcon->src); 1890 1891 /* Convert from HCI to three-value type */ 1892 if (hcon->src_type == ADDR_LE_DEV_PUBLIC) 1893 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC; 1894 else 1895 iso_pi(sk)->src_type = BDADDR_LE_RANDOM; 1896 1897 /* If hcon has no destination address (BDADDR_ANY) it means it 1898 * was created by HCI_EV_LE_BIG_SYNC_ESTABILISHED or 1899 * HCI_EV_LE_PA_SYNC_ESTABLISHED so we need to initialize using 1900 * the parent socket destination address. 1901 */ 1902 if (!bacmp(&hcon->dst, BDADDR_ANY)) { 1903 bacpy(&hcon->dst, &iso_pi(parent)->dst); 1904 hcon->dst_type = iso_pi(parent)->dst_type; 1905 } 1906 1907 if (ev3) { 1908 iso_pi(sk)->qos = iso_pi(parent)->qos; 1909 hcon->iso_qos = iso_pi(sk)->qos; 1910 iso_pi(sk)->bc_num_bis = iso_pi(parent)->bc_num_bis; 1911 memcpy(iso_pi(sk)->bc_bis, iso_pi(parent)->bc_bis, ISO_MAX_NUM_BIS); 1912 set_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags); 1913 } 1914 1915 bacpy(&iso_pi(sk)->dst, &hcon->dst); 1916 iso_pi(sk)->dst_type = hcon->dst_type; 1917 iso_pi(sk)->sync_handle = iso_pi(parent)->sync_handle; 1918 memcpy(iso_pi(sk)->base, iso_pi(parent)->base, iso_pi(parent)->base_len); 1919 iso_pi(sk)->base_len = iso_pi(parent)->base_len; 1920 1921 hci_conn_hold(hcon); 1922 iso_chan_add(conn, sk, parent); 1923 1924 if ((ev && ((struct hci_evt_le_big_sync_estabilished *)ev)->status) || 1925 (ev2 && ev2->status)) { 1926 /* Trigger error signal on child socket */ 1927 sk->sk_err = ECONNREFUSED; 1928 sk->sk_error_report(sk); 1929 } 1930 1931 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) 1932 sk->sk_state = BT_CONNECT2; 1933 else 1934 sk->sk_state = BT_CONNECTED; 1935 1936 /* Wake up parent */ 1937 parent->sk_data_ready(parent); 1938 1939 release_sock(parent); 1940 sock_put(parent); 1941 } 1942 } 1943 1944 static bool iso_match_sid(struct sock *sk, void *data) 1945 { 1946 struct hci_ev_le_pa_sync_established *ev = data; 1947 1948 return ev->sid == iso_pi(sk)->bc_sid; 1949 } 1950 1951 static bool iso_match_sync_handle(struct sock *sk, void *data) 1952 { 1953 struct hci_evt_le_big_info_adv_report *ev = data; 1954 1955 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle; 1956 } 1957 1958 static bool iso_match_sync_handle_pa_report(struct sock *sk, void *data) 1959 { 1960 struct hci_ev_le_per_adv_report *ev = data; 1961 1962 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle; 1963 } 1964 1965 /* ----- ISO interface with lower layer (HCI) ----- */ 1966 1967 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags) 1968 { 1969 struct hci_ev_le_pa_sync_established *ev1; 1970 struct hci_evt_le_big_info_adv_report *ev2; 1971 struct hci_ev_le_per_adv_report *ev3; 1972 struct sock *sk; 1973 1974 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); 1975 1976 /* Broadcast receiver requires handling of some events before it can 1977 * proceed to establishing a BIG sync: 1978 * 1979 * 1. HCI_EV_LE_PA_SYNC_ESTABLISHED: The socket may specify a specific 1980 * SID to listen to and once sync is estabilished its handle needs to 1981 * be stored in iso_pi(sk)->sync_handle so it can be matched once 1982 * receiving the BIG Info. 1983 * 2. HCI_EVT_LE_BIG_INFO_ADV_REPORT: When connect_ind is triggered by a 1984 * a BIG Info it attempts to check if there any listening socket with 1985 * the same sync_handle and if it does then attempt to create a sync. 1986 * 3. HCI_EV_LE_PER_ADV_REPORT: When a PA report is received, it is stored 1987 * in iso_pi(sk)->base so it can be passed up to user, in the case of a 1988 * broadcast sink. 1989 */ 1990 ev1 = hci_recv_event_data(hdev, HCI_EV_LE_PA_SYNC_ESTABLISHED); 1991 if (ev1) { 1992 sk = iso_get_sock(&hdev->bdaddr, bdaddr, BT_LISTEN, 1993 iso_match_sid, ev1); 1994 if (sk && !ev1->status) 1995 iso_pi(sk)->sync_handle = le16_to_cpu(ev1->handle); 1996 1997 goto done; 1998 } 1999 2000 ev2 = hci_recv_event_data(hdev, HCI_EVT_LE_BIG_INFO_ADV_REPORT); 2001 if (ev2) { 2002 /* Check if BIGInfo report has already been handled */ 2003 sk = iso_get_sock(&hdev->bdaddr, bdaddr, BT_CONNECTED, 2004 iso_match_sync_handle, ev2); 2005 if (sk) { 2006 sock_put(sk); 2007 sk = NULL; 2008 goto done; 2009 } 2010 2011 /* Try to get PA sync socket, if it exists */ 2012 sk = iso_get_sock(&hdev->bdaddr, bdaddr, BT_CONNECT2, 2013 iso_match_sync_handle, ev2); 2014 if (!sk) 2015 sk = iso_get_sock(&hdev->bdaddr, bdaddr, 2016 BT_LISTEN, 2017 iso_match_sync_handle, 2018 ev2); 2019 2020 if (sk) { 2021 int err; 2022 struct hci_conn *hcon = iso_pi(sk)->conn->hcon; 2023 2024 iso_pi(sk)->qos.bcast.encryption = ev2->encryption; 2025 2026 if (ev2->num_bis < iso_pi(sk)->bc_num_bis) 2027 iso_pi(sk)->bc_num_bis = ev2->num_bis; 2028 2029 if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags) && 2030 !test_and_set_bit(BT_SK_BIG_SYNC, &iso_pi(sk)->flags)) { 2031 err = hci_le_big_create_sync(hdev, 2032 hcon, 2033 &iso_pi(sk)->qos, 2034 iso_pi(sk)->sync_handle, 2035 iso_pi(sk)->bc_num_bis, 2036 iso_pi(sk)->bc_bis); 2037 if (err) { 2038 bt_dev_err(hdev, "hci_le_big_create_sync: %d", 2039 err); 2040 sock_put(sk); 2041 sk = NULL; 2042 } 2043 } 2044 } 2045 2046 goto done; 2047 } 2048 2049 ev3 = hci_recv_event_data(hdev, HCI_EV_LE_PER_ADV_REPORT); 2050 if (ev3) { 2051 size_t base_len = 0; 2052 u8 *base; 2053 struct hci_conn *hcon; 2054 2055 sk = iso_get_sock(&hdev->bdaddr, bdaddr, BT_LISTEN, 2056 iso_match_sync_handle_pa_report, ev3); 2057 if (!sk) 2058 goto done; 2059 2060 hcon = iso_pi(sk)->conn->hcon; 2061 if (!hcon) 2062 goto done; 2063 2064 if (ev3->data_status == LE_PA_DATA_TRUNCATED) { 2065 /* The controller was unable to retrieve PA data. */ 2066 memset(hcon->le_per_adv_data, 0, 2067 HCI_MAX_PER_AD_TOT_LEN); 2068 hcon->le_per_adv_data_len = 0; 2069 hcon->le_per_adv_data_offset = 0; 2070 goto done; 2071 } 2072 2073 if (hcon->le_per_adv_data_offset + ev3->length > 2074 HCI_MAX_PER_AD_TOT_LEN) 2075 goto done; 2076 2077 memcpy(hcon->le_per_adv_data + hcon->le_per_adv_data_offset, 2078 ev3->data, ev3->length); 2079 hcon->le_per_adv_data_offset += ev3->length; 2080 2081 if (ev3->data_status == LE_PA_DATA_COMPLETE) { 2082 /* All PA data has been received. */ 2083 hcon->le_per_adv_data_len = 2084 hcon->le_per_adv_data_offset; 2085 hcon->le_per_adv_data_offset = 0; 2086 2087 /* Extract BASE */ 2088 base = eir_get_service_data(hcon->le_per_adv_data, 2089 hcon->le_per_adv_data_len, 2090 EIR_BAA_SERVICE_UUID, 2091 &base_len); 2092 2093 if (!base || base_len > BASE_MAX_LENGTH) 2094 goto done; 2095 2096 memcpy(iso_pi(sk)->base, base, base_len); 2097 iso_pi(sk)->base_len = base_len; 2098 } else { 2099 /* This is a PA data fragment. Keep pa_data_len set to 0 2100 * until all data has been reassembled. 2101 */ 2102 hcon->le_per_adv_data_len = 0; 2103 } 2104 } else { 2105 sk = iso_get_sock(&hdev->bdaddr, BDADDR_ANY, 2106 BT_LISTEN, NULL, NULL); 2107 } 2108 2109 done: 2110 if (!sk) 2111 return 0; 2112 2113 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) 2114 *flags |= HCI_PROTO_DEFER; 2115 2116 sock_put(sk); 2117 2118 return HCI_LM_ACCEPT; 2119 } 2120 2121 static void iso_connect_cfm(struct hci_conn *hcon, __u8 status) 2122 { 2123 if (hcon->type != ISO_LINK) { 2124 if (hcon->type != LE_LINK) 2125 return; 2126 2127 /* Check if LE link has failed */ 2128 if (status) { 2129 struct hci_link *link, *t; 2130 2131 list_for_each_entry_safe(link, t, &hcon->link_list, 2132 list) 2133 iso_conn_del(link->conn, bt_to_errno(status)); 2134 2135 return; 2136 } 2137 2138 /* Create CIS if pending */ 2139 hci_le_create_cis_pending(hcon->hdev); 2140 return; 2141 } 2142 2143 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); 2144 2145 /* Similar to the success case, if HCI_CONN_BIG_SYNC_FAILED or 2146 * HCI_CONN_PA_SYNC_FAILED is set, queue the failed connection 2147 * into the accept queue of the listening socket and wake up 2148 * userspace, to inform the user about the event. 2149 */ 2150 if (!status || test_bit(HCI_CONN_BIG_SYNC_FAILED, &hcon->flags) || 2151 test_bit(HCI_CONN_PA_SYNC_FAILED, &hcon->flags)) { 2152 struct iso_conn *conn; 2153 2154 conn = iso_conn_add(hcon); 2155 if (conn) 2156 iso_conn_ready(conn); 2157 } else { 2158 iso_conn_del(hcon, bt_to_errno(status)); 2159 } 2160 } 2161 2162 static void iso_disconn_cfm(struct hci_conn *hcon, __u8 reason) 2163 { 2164 if (hcon->type != ISO_LINK) 2165 return; 2166 2167 BT_DBG("hcon %p reason %d", hcon, reason); 2168 2169 iso_conn_del(hcon, bt_to_errno(reason)); 2170 } 2171 2172 void iso_recv(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 2173 { 2174 struct iso_conn *conn = hcon->iso_data; 2175 __u16 pb, ts, len; 2176 2177 if (!conn) 2178 goto drop; 2179 2180 pb = hci_iso_flags_pb(flags); 2181 ts = hci_iso_flags_ts(flags); 2182 2183 BT_DBG("conn %p len %d pb 0x%x ts 0x%x", conn, skb->len, pb, ts); 2184 2185 switch (pb) { 2186 case ISO_START: 2187 case ISO_SINGLE: 2188 if (conn->rx_len) { 2189 BT_ERR("Unexpected start frame (len %d)", skb->len); 2190 kfree_skb(conn->rx_skb); 2191 conn->rx_skb = NULL; 2192 conn->rx_len = 0; 2193 } 2194 2195 if (ts) { 2196 struct hci_iso_ts_data_hdr *hdr; 2197 2198 /* TODO: add timestamp to the packet? */ 2199 hdr = skb_pull_data(skb, HCI_ISO_TS_DATA_HDR_SIZE); 2200 if (!hdr) { 2201 BT_ERR("Frame is too short (len %d)", skb->len); 2202 goto drop; 2203 } 2204 2205 len = __le16_to_cpu(hdr->slen); 2206 } else { 2207 struct hci_iso_data_hdr *hdr; 2208 2209 hdr = skb_pull_data(skb, HCI_ISO_DATA_HDR_SIZE); 2210 if (!hdr) { 2211 BT_ERR("Frame is too short (len %d)", skb->len); 2212 goto drop; 2213 } 2214 2215 len = __le16_to_cpu(hdr->slen); 2216 } 2217 2218 flags = hci_iso_data_flags(len); 2219 len = hci_iso_data_len(len); 2220 2221 BT_DBG("Start: total len %d, frag len %d flags 0x%4.4x", len, 2222 skb->len, flags); 2223 2224 if (len == skb->len) { 2225 /* Complete frame received */ 2226 hci_skb_pkt_status(skb) = flags & 0x03; 2227 iso_recv_frame(conn, skb); 2228 return; 2229 } 2230 2231 if (pb == ISO_SINGLE) { 2232 BT_ERR("Frame malformed (len %d, expected len %d)", 2233 skb->len, len); 2234 goto drop; 2235 } 2236 2237 if (skb->len > len) { 2238 BT_ERR("Frame is too long (len %d, expected len %d)", 2239 skb->len, len); 2240 goto drop; 2241 } 2242 2243 /* Allocate skb for the complete frame (with header) */ 2244 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); 2245 if (!conn->rx_skb) 2246 goto drop; 2247 2248 hci_skb_pkt_status(conn->rx_skb) = flags & 0x03; 2249 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 2250 skb->len); 2251 conn->rx_len = len - skb->len; 2252 break; 2253 2254 case ISO_CONT: 2255 BT_DBG("Cont: frag len %d (expecting %d)", skb->len, 2256 conn->rx_len); 2257 2258 if (!conn->rx_len) { 2259 BT_ERR("Unexpected continuation frame (len %d)", 2260 skb->len); 2261 goto drop; 2262 } 2263 2264 if (skb->len > conn->rx_len) { 2265 BT_ERR("Fragment is too long (len %d, expected %d)", 2266 skb->len, conn->rx_len); 2267 kfree_skb(conn->rx_skb); 2268 conn->rx_skb = NULL; 2269 conn->rx_len = 0; 2270 goto drop; 2271 } 2272 2273 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 2274 skb->len); 2275 conn->rx_len -= skb->len; 2276 return; 2277 2278 case ISO_END: 2279 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 2280 skb->len); 2281 conn->rx_len -= skb->len; 2282 2283 if (!conn->rx_len) { 2284 struct sk_buff *rx_skb = conn->rx_skb; 2285 2286 /* Complete frame received. iso_recv_frame 2287 * takes ownership of the skb so set the global 2288 * rx_skb pointer to NULL first. 2289 */ 2290 conn->rx_skb = NULL; 2291 iso_recv_frame(conn, rx_skb); 2292 } 2293 break; 2294 } 2295 2296 drop: 2297 kfree_skb(skb); 2298 } 2299 2300 static struct hci_cb iso_cb = { 2301 .name = "ISO", 2302 .connect_cfm = iso_connect_cfm, 2303 .disconn_cfm = iso_disconn_cfm, 2304 }; 2305 2306 static int iso_debugfs_show(struct seq_file *f, void *p) 2307 { 2308 struct sock *sk; 2309 2310 read_lock(&iso_sk_list.lock); 2311 2312 sk_for_each(sk, &iso_sk_list.head) { 2313 seq_printf(f, "%pMR %pMR %d\n", &iso_pi(sk)->src, 2314 &iso_pi(sk)->dst, sk->sk_state); 2315 } 2316 2317 read_unlock(&iso_sk_list.lock); 2318 2319 return 0; 2320 } 2321 2322 DEFINE_SHOW_ATTRIBUTE(iso_debugfs); 2323 2324 static struct dentry *iso_debugfs; 2325 2326 static const struct proto_ops iso_sock_ops = { 2327 .family = PF_BLUETOOTH, 2328 .owner = THIS_MODULE, 2329 .release = iso_sock_release, 2330 .bind = iso_sock_bind, 2331 .connect = iso_sock_connect, 2332 .listen = iso_sock_listen, 2333 .accept = iso_sock_accept, 2334 .getname = iso_sock_getname, 2335 .sendmsg = iso_sock_sendmsg, 2336 .recvmsg = iso_sock_recvmsg, 2337 .poll = bt_sock_poll, 2338 .ioctl = bt_sock_ioctl, 2339 .mmap = sock_no_mmap, 2340 .socketpair = sock_no_socketpair, 2341 .shutdown = iso_sock_shutdown, 2342 .setsockopt = iso_sock_setsockopt, 2343 .getsockopt = iso_sock_getsockopt 2344 }; 2345 2346 static const struct net_proto_family iso_sock_family_ops = { 2347 .family = PF_BLUETOOTH, 2348 .owner = THIS_MODULE, 2349 .create = iso_sock_create, 2350 }; 2351 2352 static bool iso_inited; 2353 2354 bool iso_enabled(void) 2355 { 2356 return iso_inited; 2357 } 2358 2359 int iso_init(void) 2360 { 2361 int err; 2362 2363 BUILD_BUG_ON(sizeof(struct sockaddr_iso) > sizeof(struct sockaddr)); 2364 2365 if (iso_inited) 2366 return -EALREADY; 2367 2368 err = proto_register(&iso_proto, 0); 2369 if (err < 0) 2370 return err; 2371 2372 err = bt_sock_register(BTPROTO_ISO, &iso_sock_family_ops); 2373 if (err < 0) { 2374 BT_ERR("ISO socket registration failed"); 2375 goto error; 2376 } 2377 2378 err = bt_procfs_init(&init_net, "iso", &iso_sk_list, NULL); 2379 if (err < 0) { 2380 BT_ERR("Failed to create ISO proc file"); 2381 bt_sock_unregister(BTPROTO_ISO); 2382 goto error; 2383 } 2384 2385 BT_INFO("ISO socket layer initialized"); 2386 2387 hci_register_cb(&iso_cb); 2388 2389 if (!IS_ERR_OR_NULL(bt_debugfs)) 2390 iso_debugfs = debugfs_create_file("iso", 0444, bt_debugfs, 2391 NULL, &iso_debugfs_fops); 2392 2393 iso_inited = true; 2394 2395 return 0; 2396 2397 error: 2398 proto_unregister(&iso_proto); 2399 return err; 2400 } 2401 2402 int iso_exit(void) 2403 { 2404 if (!iso_inited) 2405 return -EALREADY; 2406 2407 bt_procfs_cleanup(&init_net, "iso"); 2408 2409 debugfs_remove(iso_debugfs); 2410 iso_debugfs = NULL; 2411 2412 hci_unregister_cb(&iso_cb); 2413 2414 bt_sock_unregister(BTPROTO_ISO); 2415 2416 proto_unregister(&iso_proto); 2417 2418 iso_inited = false; 2419 2420 return 0; 2421 } 2422