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