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 len, 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 if (copy_from_sockptr(&opt, optval, sizeof(u32))) { 1368 err = -EFAULT; 1369 break; 1370 } 1371 1372 if (opt) 1373 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); 1374 else 1375 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); 1376 break; 1377 1378 case BT_PKT_STATUS: 1379 if (copy_from_sockptr(&opt, optval, sizeof(u32))) { 1380 err = -EFAULT; 1381 break; 1382 } 1383 1384 if (opt) 1385 set_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags); 1386 else 1387 clear_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags); 1388 break; 1389 1390 case BT_ISO_QOS: 1391 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND && 1392 sk->sk_state != BT_CONNECT2) { 1393 err = -EINVAL; 1394 break; 1395 } 1396 1397 len = min_t(unsigned int, sizeof(qos), optlen); 1398 1399 if (copy_from_sockptr(&qos, optval, len)) { 1400 err = -EFAULT; 1401 break; 1402 } 1403 1404 if (len == sizeof(qos.ucast) && !check_ucast_qos(&qos)) { 1405 err = -EINVAL; 1406 break; 1407 } 1408 1409 iso_pi(sk)->qos = qos; 1410 iso_pi(sk)->qos_user_set = true; 1411 1412 break; 1413 1414 case BT_ISO_BASE: 1415 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND && 1416 sk->sk_state != BT_CONNECT2) { 1417 err = -EINVAL; 1418 break; 1419 } 1420 1421 if (optlen > sizeof(iso_pi(sk)->base)) { 1422 err = -EOVERFLOW; 1423 break; 1424 } 1425 1426 len = min_t(unsigned int, sizeof(iso_pi(sk)->base), optlen); 1427 1428 if (copy_from_sockptr(iso_pi(sk)->base, optval, len)) { 1429 err = -EFAULT; 1430 break; 1431 } 1432 1433 iso_pi(sk)->base_len = len; 1434 1435 break; 1436 1437 default: 1438 err = -ENOPROTOOPT; 1439 break; 1440 } 1441 1442 release_sock(sk); 1443 return err; 1444 } 1445 1446 static int iso_sock_getsockopt(struct socket *sock, int level, int optname, 1447 char __user *optval, int __user *optlen) 1448 { 1449 struct sock *sk = sock->sk; 1450 int len, err = 0; 1451 struct bt_iso_qos *qos; 1452 u8 base_len; 1453 u8 *base; 1454 1455 BT_DBG("sk %p", sk); 1456 1457 if (get_user(len, optlen)) 1458 return -EFAULT; 1459 1460 lock_sock(sk); 1461 1462 switch (optname) { 1463 case BT_DEFER_SETUP: 1464 if (sk->sk_state == BT_CONNECTED) { 1465 err = -EINVAL; 1466 break; 1467 } 1468 1469 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags), 1470 (u32 __user *)optval)) 1471 err = -EFAULT; 1472 1473 break; 1474 1475 case BT_PKT_STATUS: 1476 if (put_user(test_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags), 1477 (int __user *)optval)) 1478 err = -EFAULT; 1479 break; 1480 1481 case BT_ISO_QOS: 1482 qos = iso_sock_get_qos(sk); 1483 1484 len = min_t(unsigned int, len, sizeof(*qos)); 1485 if (copy_to_user(optval, qos, len)) 1486 err = -EFAULT; 1487 1488 break; 1489 1490 case BT_ISO_BASE: 1491 if (sk->sk_state == BT_CONNECTED && 1492 !bacmp(&iso_pi(sk)->dst, BDADDR_ANY)) { 1493 base_len = iso_pi(sk)->conn->hcon->le_per_adv_data_len; 1494 base = iso_pi(sk)->conn->hcon->le_per_adv_data; 1495 } else { 1496 base_len = iso_pi(sk)->base_len; 1497 base = iso_pi(sk)->base; 1498 } 1499 1500 len = min_t(unsigned int, len, base_len); 1501 if (copy_to_user(optval, base, len)) 1502 err = -EFAULT; 1503 1504 break; 1505 1506 default: 1507 err = -ENOPROTOOPT; 1508 break; 1509 } 1510 1511 release_sock(sk); 1512 return err; 1513 } 1514 1515 static int iso_sock_shutdown(struct socket *sock, int how) 1516 { 1517 struct sock *sk = sock->sk; 1518 int err = 0; 1519 1520 BT_DBG("sock %p, sk %p, how %d", sock, sk, how); 1521 1522 if (!sk) 1523 return 0; 1524 1525 sock_hold(sk); 1526 lock_sock(sk); 1527 1528 switch (how) { 1529 case SHUT_RD: 1530 if (sk->sk_shutdown & RCV_SHUTDOWN) 1531 goto unlock; 1532 sk->sk_shutdown |= RCV_SHUTDOWN; 1533 break; 1534 case SHUT_WR: 1535 if (sk->sk_shutdown & SEND_SHUTDOWN) 1536 goto unlock; 1537 sk->sk_shutdown |= SEND_SHUTDOWN; 1538 break; 1539 case SHUT_RDWR: 1540 if (sk->sk_shutdown & SHUTDOWN_MASK) 1541 goto unlock; 1542 sk->sk_shutdown |= SHUTDOWN_MASK; 1543 break; 1544 } 1545 1546 iso_sock_clear_timer(sk); 1547 __iso_sock_close(sk); 1548 1549 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime && 1550 !(current->flags & PF_EXITING)) 1551 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime); 1552 1553 unlock: 1554 release_sock(sk); 1555 sock_put(sk); 1556 1557 return err; 1558 } 1559 1560 static int iso_sock_release(struct socket *sock) 1561 { 1562 struct sock *sk = sock->sk; 1563 int err = 0; 1564 1565 BT_DBG("sock %p, sk %p", sock, sk); 1566 1567 if (!sk) 1568 return 0; 1569 1570 iso_sock_close(sk); 1571 1572 if (sock_flag(sk, SOCK_LINGER) && READ_ONCE(sk->sk_lingertime) && 1573 !(current->flags & PF_EXITING)) { 1574 lock_sock(sk); 1575 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime); 1576 release_sock(sk); 1577 } 1578 1579 sock_orphan(sk); 1580 iso_sock_kill(sk); 1581 return err; 1582 } 1583 1584 static void iso_sock_ready(struct sock *sk) 1585 { 1586 BT_DBG("sk %p", sk); 1587 1588 if (!sk) 1589 return; 1590 1591 lock_sock(sk); 1592 iso_sock_clear_timer(sk); 1593 sk->sk_state = BT_CONNECTED; 1594 sk->sk_state_change(sk); 1595 release_sock(sk); 1596 } 1597 1598 struct iso_list_data { 1599 struct hci_conn *hcon; 1600 int count; 1601 }; 1602 1603 static bool iso_match_big(struct sock *sk, void *data) 1604 { 1605 struct hci_evt_le_big_sync_estabilished *ev = data; 1606 1607 return ev->handle == iso_pi(sk)->qos.bcast.big; 1608 } 1609 1610 static bool iso_match_pa_sync_flag(struct sock *sk, void *data) 1611 { 1612 return test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags); 1613 } 1614 1615 static void iso_conn_ready(struct iso_conn *conn) 1616 { 1617 struct sock *parent = NULL; 1618 struct sock *sk = conn->sk; 1619 struct hci_ev_le_big_sync_estabilished *ev = NULL; 1620 struct hci_ev_le_pa_sync_established *ev2 = NULL; 1621 struct hci_evt_le_big_info_adv_report *ev3 = NULL; 1622 struct hci_conn *hcon; 1623 1624 BT_DBG("conn %p", conn); 1625 1626 if (sk) { 1627 iso_sock_ready(conn->sk); 1628 } else { 1629 hcon = conn->hcon; 1630 if (!hcon) 1631 return; 1632 1633 if (test_bit(HCI_CONN_BIG_SYNC, &hcon->flags) || 1634 test_bit(HCI_CONN_BIG_SYNC_FAILED, &hcon->flags)) { 1635 ev = hci_recv_event_data(hcon->hdev, 1636 HCI_EVT_LE_BIG_SYNC_ESTABILISHED); 1637 1638 /* Get reference to PA sync parent socket, if it exists */ 1639 parent = iso_get_sock_listen(&hcon->src, 1640 &hcon->dst, 1641 iso_match_pa_sync_flag, NULL); 1642 if (!parent && ev) 1643 parent = iso_get_sock_listen(&hcon->src, 1644 &hcon->dst, 1645 iso_match_big, ev); 1646 } else if (test_bit(HCI_CONN_PA_SYNC_FAILED, &hcon->flags)) { 1647 ev2 = hci_recv_event_data(hcon->hdev, 1648 HCI_EV_LE_PA_SYNC_ESTABLISHED); 1649 if (ev2) 1650 parent = iso_get_sock_listen(&hcon->src, 1651 &hcon->dst, 1652 iso_match_sid, ev2); 1653 } else if (test_bit(HCI_CONN_PA_SYNC, &hcon->flags)) { 1654 ev3 = hci_recv_event_data(hcon->hdev, 1655 HCI_EVT_LE_BIG_INFO_ADV_REPORT); 1656 if (ev3) 1657 parent = iso_get_sock_listen(&hcon->src, 1658 &hcon->dst, 1659 iso_match_sync_handle, ev3); 1660 } 1661 1662 if (!parent) 1663 parent = iso_get_sock_listen(&hcon->src, 1664 BDADDR_ANY, NULL, NULL); 1665 1666 if (!parent) 1667 return; 1668 1669 lock_sock(parent); 1670 1671 sk = iso_sock_alloc(sock_net(parent), NULL, 1672 BTPROTO_ISO, GFP_ATOMIC, 0); 1673 if (!sk) { 1674 release_sock(parent); 1675 return; 1676 } 1677 1678 iso_sock_init(sk, parent); 1679 1680 bacpy(&iso_pi(sk)->src, &hcon->src); 1681 1682 /* Convert from HCI to three-value type */ 1683 if (hcon->src_type == ADDR_LE_DEV_PUBLIC) 1684 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC; 1685 else 1686 iso_pi(sk)->src_type = BDADDR_LE_RANDOM; 1687 1688 /* If hcon has no destination address (BDADDR_ANY) it means it 1689 * was created by HCI_EV_LE_BIG_SYNC_ESTABILISHED or 1690 * HCI_EV_LE_PA_SYNC_ESTABLISHED so we need to initialize using 1691 * the parent socket destination address. 1692 */ 1693 if (!bacmp(&hcon->dst, BDADDR_ANY)) { 1694 bacpy(&hcon->dst, &iso_pi(parent)->dst); 1695 hcon->dst_type = iso_pi(parent)->dst_type; 1696 hcon->sync_handle = iso_pi(parent)->sync_handle; 1697 } 1698 1699 if (ev3) { 1700 iso_pi(sk)->qos = iso_pi(parent)->qos; 1701 iso_pi(sk)->qos.bcast.encryption = ev3->encryption; 1702 hcon->iso_qos = iso_pi(sk)->qos; 1703 iso_pi(sk)->bc_num_bis = iso_pi(parent)->bc_num_bis; 1704 memcpy(iso_pi(sk)->bc_bis, iso_pi(parent)->bc_bis, ISO_MAX_NUM_BIS); 1705 set_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags); 1706 } 1707 1708 bacpy(&iso_pi(sk)->dst, &hcon->dst); 1709 iso_pi(sk)->dst_type = hcon->dst_type; 1710 iso_pi(sk)->sync_handle = iso_pi(parent)->sync_handle; 1711 memcpy(iso_pi(sk)->base, iso_pi(parent)->base, iso_pi(parent)->base_len); 1712 iso_pi(sk)->base_len = iso_pi(parent)->base_len; 1713 1714 hci_conn_hold(hcon); 1715 iso_chan_add(conn, sk, parent); 1716 1717 if ((ev && ((struct hci_evt_le_big_sync_estabilished *)ev)->status) || 1718 (ev2 && ev2->status)) { 1719 /* Trigger error signal on child socket */ 1720 sk->sk_err = ECONNREFUSED; 1721 sk->sk_error_report(sk); 1722 } 1723 1724 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) 1725 sk->sk_state = BT_CONNECT2; 1726 else 1727 sk->sk_state = BT_CONNECTED; 1728 1729 /* Wake up parent */ 1730 parent->sk_data_ready(parent); 1731 1732 release_sock(parent); 1733 } 1734 } 1735 1736 static bool iso_match_sid(struct sock *sk, void *data) 1737 { 1738 struct hci_ev_le_pa_sync_established *ev = data; 1739 1740 return ev->sid == iso_pi(sk)->bc_sid; 1741 } 1742 1743 static bool iso_match_sync_handle(struct sock *sk, void *data) 1744 { 1745 struct hci_evt_le_big_info_adv_report *ev = data; 1746 1747 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle; 1748 } 1749 1750 static bool iso_match_sync_handle_pa_report(struct sock *sk, void *data) 1751 { 1752 struct hci_ev_le_per_adv_report *ev = data; 1753 1754 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle; 1755 } 1756 1757 /* ----- ISO interface with lower layer (HCI) ----- */ 1758 1759 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags) 1760 { 1761 struct hci_ev_le_pa_sync_established *ev1; 1762 struct hci_evt_le_big_info_adv_report *ev2; 1763 struct hci_ev_le_per_adv_report *ev3; 1764 struct sock *sk; 1765 int lm = 0; 1766 1767 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); 1768 1769 /* Broadcast receiver requires handling of some events before it can 1770 * proceed to establishing a BIG sync: 1771 * 1772 * 1. HCI_EV_LE_PA_SYNC_ESTABLISHED: The socket may specify a specific 1773 * SID to listen to and once sync is estabilished its handle needs to 1774 * be stored in iso_pi(sk)->sync_handle so it can be matched once 1775 * receiving the BIG Info. 1776 * 2. HCI_EVT_LE_BIG_INFO_ADV_REPORT: When connect_ind is triggered by a 1777 * a BIG Info it attempts to check if there any listening socket with 1778 * the same sync_handle and if it does then attempt to create a sync. 1779 * 3. HCI_EV_LE_PER_ADV_REPORT: When a PA report is received, it is stored 1780 * in iso_pi(sk)->base so it can be passed up to user, in the case of a 1781 * broadcast sink. 1782 */ 1783 ev1 = hci_recv_event_data(hdev, HCI_EV_LE_PA_SYNC_ESTABLISHED); 1784 if (ev1) { 1785 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr, iso_match_sid, 1786 ev1); 1787 if (sk && !ev1->status) 1788 iso_pi(sk)->sync_handle = le16_to_cpu(ev1->handle); 1789 1790 goto done; 1791 } 1792 1793 ev2 = hci_recv_event_data(hdev, HCI_EVT_LE_BIG_INFO_ADV_REPORT); 1794 if (ev2) { 1795 /* Try to get PA sync listening socket, if it exists */ 1796 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr, 1797 iso_match_pa_sync_flag, NULL); 1798 1799 if (!sk) { 1800 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr, 1801 iso_match_sync_handle, ev2); 1802 1803 /* If PA Sync is in process of terminating, 1804 * do not handle any more BIGInfo adv reports. 1805 */ 1806 1807 if (sk && test_bit(BT_SK_PA_SYNC_TERM, 1808 &iso_pi(sk)->flags)) 1809 return lm; 1810 } 1811 1812 if (sk) { 1813 int err; 1814 1815 if (ev2->num_bis < iso_pi(sk)->bc_num_bis) 1816 iso_pi(sk)->bc_num_bis = ev2->num_bis; 1817 1818 if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags) && 1819 !test_and_set_bit(BT_SK_BIG_SYNC, &iso_pi(sk)->flags)) { 1820 err = hci_le_big_create_sync(hdev, NULL, 1821 &iso_pi(sk)->qos, 1822 iso_pi(sk)->sync_handle, 1823 iso_pi(sk)->bc_num_bis, 1824 iso_pi(sk)->bc_bis); 1825 if (err) { 1826 bt_dev_err(hdev, "hci_le_big_create_sync: %d", 1827 err); 1828 sk = NULL; 1829 } 1830 } 1831 } 1832 } 1833 1834 ev3 = hci_recv_event_data(hdev, HCI_EV_LE_PER_ADV_REPORT); 1835 if (ev3) { 1836 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr, 1837 iso_match_sync_handle_pa_report, ev3); 1838 1839 if (sk) { 1840 memcpy(iso_pi(sk)->base, ev3->data, ev3->length); 1841 iso_pi(sk)->base_len = ev3->length; 1842 } 1843 } else { 1844 sk = iso_get_sock_listen(&hdev->bdaddr, BDADDR_ANY, NULL, NULL); 1845 } 1846 1847 done: 1848 if (!sk) 1849 return lm; 1850 1851 lm |= HCI_LM_ACCEPT; 1852 1853 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) 1854 *flags |= HCI_PROTO_DEFER; 1855 1856 return lm; 1857 } 1858 1859 static void iso_connect_cfm(struct hci_conn *hcon, __u8 status) 1860 { 1861 if (hcon->type != ISO_LINK) { 1862 if (hcon->type != LE_LINK) 1863 return; 1864 1865 /* Check if LE link has failed */ 1866 if (status) { 1867 struct hci_link *link, *t; 1868 1869 list_for_each_entry_safe(link, t, &hcon->link_list, 1870 list) 1871 iso_conn_del(link->conn, bt_to_errno(status)); 1872 1873 return; 1874 } 1875 1876 /* Create CIS if pending */ 1877 hci_le_create_cis_pending(hcon->hdev); 1878 return; 1879 } 1880 1881 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); 1882 1883 /* Similar to the success case, if HCI_CONN_BIG_SYNC_FAILED or 1884 * HCI_CONN_PA_SYNC_FAILED is set, queue the failed connection 1885 * into the accept queue of the listening socket and wake up 1886 * userspace, to inform the user about the event. 1887 */ 1888 if (!status || test_bit(HCI_CONN_BIG_SYNC_FAILED, &hcon->flags) || 1889 test_bit(HCI_CONN_PA_SYNC_FAILED, &hcon->flags)) { 1890 struct iso_conn *conn; 1891 1892 conn = iso_conn_add(hcon); 1893 if (conn) 1894 iso_conn_ready(conn); 1895 } else { 1896 iso_conn_del(hcon, bt_to_errno(status)); 1897 } 1898 } 1899 1900 static void iso_disconn_cfm(struct hci_conn *hcon, __u8 reason) 1901 { 1902 if (hcon->type != ISO_LINK) 1903 return; 1904 1905 BT_DBG("hcon %p reason %d", hcon, reason); 1906 1907 iso_conn_del(hcon, bt_to_errno(reason)); 1908 } 1909 1910 void iso_recv(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 1911 { 1912 struct iso_conn *conn = hcon->iso_data; 1913 __u16 pb, ts, len; 1914 1915 if (!conn) 1916 goto drop; 1917 1918 pb = hci_iso_flags_pb(flags); 1919 ts = hci_iso_flags_ts(flags); 1920 1921 BT_DBG("conn %p len %d pb 0x%x ts 0x%x", conn, skb->len, pb, ts); 1922 1923 switch (pb) { 1924 case ISO_START: 1925 case ISO_SINGLE: 1926 if (conn->rx_len) { 1927 BT_ERR("Unexpected start frame (len %d)", skb->len); 1928 kfree_skb(conn->rx_skb); 1929 conn->rx_skb = NULL; 1930 conn->rx_len = 0; 1931 } 1932 1933 if (ts) { 1934 struct hci_iso_ts_data_hdr *hdr; 1935 1936 /* TODO: add timestamp to the packet? */ 1937 hdr = skb_pull_data(skb, HCI_ISO_TS_DATA_HDR_SIZE); 1938 if (!hdr) { 1939 BT_ERR("Frame is too short (len %d)", skb->len); 1940 goto drop; 1941 } 1942 1943 len = __le16_to_cpu(hdr->slen); 1944 } else { 1945 struct hci_iso_data_hdr *hdr; 1946 1947 hdr = skb_pull_data(skb, HCI_ISO_DATA_HDR_SIZE); 1948 if (!hdr) { 1949 BT_ERR("Frame is too short (len %d)", skb->len); 1950 goto drop; 1951 } 1952 1953 len = __le16_to_cpu(hdr->slen); 1954 } 1955 1956 flags = hci_iso_data_flags(len); 1957 len = hci_iso_data_len(len); 1958 1959 BT_DBG("Start: total len %d, frag len %d flags 0x%4.4x", len, 1960 skb->len, flags); 1961 1962 if (len == skb->len) { 1963 /* Complete frame received */ 1964 hci_skb_pkt_status(skb) = flags & 0x03; 1965 iso_recv_frame(conn, skb); 1966 return; 1967 } 1968 1969 if (pb == ISO_SINGLE) { 1970 BT_ERR("Frame malformed (len %d, expected len %d)", 1971 skb->len, len); 1972 goto drop; 1973 } 1974 1975 if (skb->len > len) { 1976 BT_ERR("Frame is too long (len %d, expected len %d)", 1977 skb->len, len); 1978 goto drop; 1979 } 1980 1981 /* Allocate skb for the complete frame (with header) */ 1982 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); 1983 if (!conn->rx_skb) 1984 goto drop; 1985 1986 hci_skb_pkt_status(conn->rx_skb) = flags & 0x03; 1987 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 1988 skb->len); 1989 conn->rx_len = len - skb->len; 1990 break; 1991 1992 case ISO_CONT: 1993 BT_DBG("Cont: frag len %d (expecting %d)", skb->len, 1994 conn->rx_len); 1995 1996 if (!conn->rx_len) { 1997 BT_ERR("Unexpected continuation frame (len %d)", 1998 skb->len); 1999 goto drop; 2000 } 2001 2002 if (skb->len > conn->rx_len) { 2003 BT_ERR("Fragment is too long (len %d, expected %d)", 2004 skb->len, conn->rx_len); 2005 kfree_skb(conn->rx_skb); 2006 conn->rx_skb = NULL; 2007 conn->rx_len = 0; 2008 goto drop; 2009 } 2010 2011 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 2012 skb->len); 2013 conn->rx_len -= skb->len; 2014 return; 2015 2016 case ISO_END: 2017 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 2018 skb->len); 2019 conn->rx_len -= skb->len; 2020 2021 if (!conn->rx_len) { 2022 struct sk_buff *rx_skb = conn->rx_skb; 2023 2024 /* Complete frame received. iso_recv_frame 2025 * takes ownership of the skb so set the global 2026 * rx_skb pointer to NULL first. 2027 */ 2028 conn->rx_skb = NULL; 2029 iso_recv_frame(conn, rx_skb); 2030 } 2031 break; 2032 } 2033 2034 drop: 2035 kfree_skb(skb); 2036 } 2037 2038 static struct hci_cb iso_cb = { 2039 .name = "ISO", 2040 .connect_cfm = iso_connect_cfm, 2041 .disconn_cfm = iso_disconn_cfm, 2042 }; 2043 2044 static int iso_debugfs_show(struct seq_file *f, void *p) 2045 { 2046 struct sock *sk; 2047 2048 read_lock(&iso_sk_list.lock); 2049 2050 sk_for_each(sk, &iso_sk_list.head) { 2051 seq_printf(f, "%pMR %pMR %d\n", &iso_pi(sk)->src, 2052 &iso_pi(sk)->dst, sk->sk_state); 2053 } 2054 2055 read_unlock(&iso_sk_list.lock); 2056 2057 return 0; 2058 } 2059 2060 DEFINE_SHOW_ATTRIBUTE(iso_debugfs); 2061 2062 static struct dentry *iso_debugfs; 2063 2064 static const struct proto_ops iso_sock_ops = { 2065 .family = PF_BLUETOOTH, 2066 .owner = THIS_MODULE, 2067 .release = iso_sock_release, 2068 .bind = iso_sock_bind, 2069 .connect = iso_sock_connect, 2070 .listen = iso_sock_listen, 2071 .accept = iso_sock_accept, 2072 .getname = iso_sock_getname, 2073 .sendmsg = iso_sock_sendmsg, 2074 .recvmsg = iso_sock_recvmsg, 2075 .poll = bt_sock_poll, 2076 .ioctl = bt_sock_ioctl, 2077 .mmap = sock_no_mmap, 2078 .socketpair = sock_no_socketpair, 2079 .shutdown = iso_sock_shutdown, 2080 .setsockopt = iso_sock_setsockopt, 2081 .getsockopt = iso_sock_getsockopt 2082 }; 2083 2084 static const struct net_proto_family iso_sock_family_ops = { 2085 .family = PF_BLUETOOTH, 2086 .owner = THIS_MODULE, 2087 .create = iso_sock_create, 2088 }; 2089 2090 static bool iso_inited; 2091 2092 bool iso_enabled(void) 2093 { 2094 return iso_inited; 2095 } 2096 2097 int iso_init(void) 2098 { 2099 int err; 2100 2101 BUILD_BUG_ON(sizeof(struct sockaddr_iso) > sizeof(struct sockaddr)); 2102 2103 if (iso_inited) 2104 return -EALREADY; 2105 2106 err = proto_register(&iso_proto, 0); 2107 if (err < 0) 2108 return err; 2109 2110 err = bt_sock_register(BTPROTO_ISO, &iso_sock_family_ops); 2111 if (err < 0) { 2112 BT_ERR("ISO socket registration failed"); 2113 goto error; 2114 } 2115 2116 err = bt_procfs_init(&init_net, "iso", &iso_sk_list, NULL); 2117 if (err < 0) { 2118 BT_ERR("Failed to create ISO proc file"); 2119 bt_sock_unregister(BTPROTO_ISO); 2120 goto error; 2121 } 2122 2123 BT_INFO("ISO socket layer initialized"); 2124 2125 hci_register_cb(&iso_cb); 2126 2127 if (IS_ERR_OR_NULL(bt_debugfs)) 2128 return 0; 2129 2130 if (!iso_debugfs) { 2131 iso_debugfs = debugfs_create_file("iso", 0444, bt_debugfs, 2132 NULL, &iso_debugfs_fops); 2133 } 2134 2135 iso_inited = true; 2136 2137 return 0; 2138 2139 error: 2140 proto_unregister(&iso_proto); 2141 return err; 2142 } 2143 2144 int iso_exit(void) 2145 { 2146 if (!iso_inited) 2147 return -EALREADY; 2148 2149 bt_procfs_cleanup(&init_net, "iso"); 2150 2151 debugfs_remove(iso_debugfs); 2152 iso_debugfs = NULL; 2153 2154 hci_unregister_cb(&iso_cb); 2155 2156 bt_sock_unregister(BTPROTO_ISO); 2157 2158 proto_unregister(&iso_proto); 2159 2160 iso_inited = false; 2161 2162 return 0; 2163 } 2164