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