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