1 // SPDX-License-Identifier: GPL-2.0
3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2022 Intel Corporation
6 * Copyright 2023-2024 NXP
9 #include <linux/module.h>
10 #include <linux/debugfs.h>
11 #include <linux/seq_file.h>
12 #include <linux/sched/signal.h>
14 #include <net/bluetooth/bluetooth.h>
15 #include <net/bluetooth/hci_core.h>
16 #include <net/bluetooth/iso.h>
19 static const struct proto_ops iso_sock_ops;
21 static struct bt_sock_list iso_sk_list = {
22 .lock = __RW_LOCK_UNLOCKED(iso_sk_list.lock)
25 /* ---- ISO connections ---- */
27 struct hci_conn *hcon;
29 /* @lock: spinlock protecting changes to iso_conn fields */
33 struct delayed_work timeout_work;
35 struct sk_buff *rx_skb;
40 #define iso_conn_lock(c) spin_lock(&(c)->lock)
41 #define iso_conn_unlock(c) spin_unlock(&(c)->lock)
43 static void iso_sock_close(struct sock *sk);
44 static void iso_sock_kill(struct sock *sk);
46 /* ----- ISO socket info ----- */
47 #define iso_pi(sk) ((struct iso_pinfo *)sk)
49 #define EIR_SERVICE_DATA_LENGTH 4
50 #define BASE_MAX_LENGTH (HCI_MAX_PER_AD_LENGTH - EIR_SERVICE_DATA_LENGTH)
51 #define EIR_BAA_SERVICE_UUID 0x1851
53 /* iso_pinfo flags values */
68 __u8 bc_bis[ISO_MAX_NUM_BIS];
71 struct bt_iso_qos qos;
74 __u8 base[BASE_MAX_LENGTH];
75 struct iso_conn *conn;
78 static struct bt_iso_qos default_qos;
80 static bool check_ucast_qos(struct bt_iso_qos *qos);
81 static bool check_bcast_qos(struct bt_iso_qos *qos);
82 static bool iso_match_sid(struct sock *sk, void *data);
83 static bool iso_match_sync_handle(struct sock *sk, void *data);
84 static void iso_sock_disconn(struct sock *sk);
86 typedef bool (*iso_sock_match_t)(struct sock *sk, void *data);
88 static struct sock *iso_get_sock_listen(bdaddr_t *src, bdaddr_t *dst,
89 iso_sock_match_t match, void *data);
91 /* ---- ISO timers ---- */
92 #define ISO_CONN_TIMEOUT (HZ * 40)
93 #define ISO_DISCONN_TIMEOUT (HZ * 2)
95 static void iso_sock_timeout(struct work_struct *work)
97 struct iso_conn *conn = container_of(work, struct iso_conn,
105 iso_conn_unlock(conn);
110 BT_DBG("sock %p state %d", sk, sk->sk_state);
113 sk->sk_err = ETIMEDOUT;
114 sk->sk_state_change(sk);
119 static void iso_sock_set_timer(struct sock *sk, long timeout)
121 if (!iso_pi(sk)->conn)
124 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
125 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
126 schedule_delayed_work(&iso_pi(sk)->conn->timeout_work, timeout);
129 static void iso_sock_clear_timer(struct sock *sk)
131 if (!iso_pi(sk)->conn)
134 BT_DBG("sock %p state %d", sk, sk->sk_state);
135 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
138 /* ---- ISO connections ---- */
139 static struct iso_conn *iso_conn_add(struct hci_conn *hcon)
141 struct iso_conn *conn = hcon->iso_data;
149 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
153 spin_lock_init(&conn->lock);
154 INIT_DELAYED_WORK(&conn->timeout_work, iso_sock_timeout);
156 hcon->iso_data = conn;
160 BT_DBG("hcon %p conn %p", hcon, conn);
165 /* Delete channel. Must be called on the locked socket. */
166 static void iso_chan_del(struct sock *sk, int err)
168 struct iso_conn *conn;
171 conn = iso_pi(sk)->conn;
173 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
178 iso_pi(sk)->conn = NULL;
179 iso_conn_unlock(conn);
182 hci_conn_drop(conn->hcon);
185 sk->sk_state = BT_CLOSED;
188 parent = bt_sk(sk)->parent;
190 bt_accept_unlink(sk);
191 parent->sk_data_ready(parent);
193 sk->sk_state_change(sk);
196 sock_set_flag(sk, SOCK_ZAPPED);
199 static bool iso_match_conn_sync_handle(struct sock *sk, void *data)
201 struct hci_conn *hcon = data;
203 if (test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags))
206 return hcon->sync_handle == iso_pi(sk)->sync_handle;
209 static void iso_conn_del(struct hci_conn *hcon, int err)
211 struct iso_conn *conn = hcon->iso_data;
218 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
225 iso_conn_unlock(conn);
230 /* While a PA sync hcon is in the process of closing,
231 * mark parent socket with a flag, so that any residual
232 * BIGInfo adv reports that arrive before PA sync is
233 * terminated are not processed anymore.
235 if (test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags)) {
236 parent = iso_get_sock_listen(&hcon->src,
238 iso_match_conn_sync_handle,
242 set_bit(BT_SK_PA_SYNC_TERM,
243 &iso_pi(parent)->flags);
248 iso_sock_clear_timer(sk);
249 iso_chan_del(sk, err);
254 /* Ensure no more work items will run before freeing conn. */
255 cancel_delayed_work_sync(&conn->timeout_work);
257 hcon->iso_data = NULL;
261 static int __iso_chan_add(struct iso_conn *conn, struct sock *sk,
264 BT_DBG("conn %p", conn);
266 if (iso_pi(sk)->conn == conn && conn->sk == sk)
270 BT_ERR("conn->sk already set");
274 iso_pi(sk)->conn = conn;
278 bt_accept_enqueue(parent, sk, true);
283 static int iso_chan_add(struct iso_conn *conn, struct sock *sk,
289 err = __iso_chan_add(conn, sk, parent);
290 iso_conn_unlock(conn);
295 static inline u8 le_addr_type(u8 bdaddr_type)
297 if (bdaddr_type == BDADDR_LE_PUBLIC)
298 return ADDR_LE_DEV_PUBLIC;
300 return ADDR_LE_DEV_RANDOM;
303 static int iso_connect_bis(struct sock *sk)
305 struct iso_conn *conn;
306 struct hci_conn *hcon;
307 struct hci_dev *hdev;
310 BT_DBG("%pMR", &iso_pi(sk)->src);
312 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
313 iso_pi(sk)->src_type);
315 return -EHOSTUNREACH;
319 if (!bis_capable(hdev)) {
324 /* Fail if user set invalid QoS */
325 if (iso_pi(sk)->qos_user_set && !check_bcast_qos(&iso_pi(sk)->qos)) {
326 iso_pi(sk)->qos = default_qos;
331 /* Fail if out PHYs are marked as disabled */
332 if (!iso_pi(sk)->qos.bcast.out.phy) {
337 /* Just bind if DEFER_SETUP has been set */
338 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
339 hcon = hci_bind_bis(hdev, &iso_pi(sk)->dst,
340 &iso_pi(sk)->qos, iso_pi(sk)->base_len,
347 hcon = hci_connect_bis(hdev, &iso_pi(sk)->dst,
348 le_addr_type(iso_pi(sk)->dst_type),
349 &iso_pi(sk)->qos, iso_pi(sk)->base_len,
357 conn = iso_conn_add(hcon);
366 err = iso_chan_add(conn, sk, NULL);
372 /* Update source addr of the socket */
373 bacpy(&iso_pi(sk)->src, &hcon->src);
375 if (hcon->state == BT_CONNECTED) {
376 iso_sock_clear_timer(sk);
377 sk->sk_state = BT_CONNECTED;
378 } else if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
379 iso_sock_clear_timer(sk);
380 sk->sk_state = BT_CONNECT;
382 sk->sk_state = BT_CONNECT;
383 iso_sock_set_timer(sk, sk->sk_sndtimeo);
389 hci_dev_unlock(hdev);
394 static int iso_connect_cis(struct sock *sk)
396 struct iso_conn *conn;
397 struct hci_conn *hcon;
398 struct hci_dev *hdev;
401 BT_DBG("%pMR -> %pMR", &iso_pi(sk)->src, &iso_pi(sk)->dst);
403 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
404 iso_pi(sk)->src_type);
406 return -EHOSTUNREACH;
410 if (!cis_central_capable(hdev)) {
415 /* Fail if user set invalid QoS */
416 if (iso_pi(sk)->qos_user_set && !check_ucast_qos(&iso_pi(sk)->qos)) {
417 iso_pi(sk)->qos = default_qos;
422 /* Fail if either PHYs are marked as disabled */
423 if (!iso_pi(sk)->qos.ucast.in.phy && !iso_pi(sk)->qos.ucast.out.phy) {
428 /* Just bind if DEFER_SETUP has been set */
429 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
430 hcon = hci_bind_cis(hdev, &iso_pi(sk)->dst,
431 le_addr_type(iso_pi(sk)->dst_type),
438 hcon = hci_connect_cis(hdev, &iso_pi(sk)->dst,
439 le_addr_type(iso_pi(sk)->dst_type),
447 conn = iso_conn_add(hcon);
456 err = iso_chan_add(conn, sk, NULL);
462 /* Update source addr of the socket */
463 bacpy(&iso_pi(sk)->src, &hcon->src);
465 if (hcon->state == BT_CONNECTED) {
466 iso_sock_clear_timer(sk);
467 sk->sk_state = BT_CONNECTED;
468 } else if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
469 iso_sock_clear_timer(sk);
470 sk->sk_state = BT_CONNECT;
472 sk->sk_state = BT_CONNECT;
473 iso_sock_set_timer(sk, sk->sk_sndtimeo);
479 hci_dev_unlock(hdev);
484 static struct bt_iso_qos *iso_sock_get_qos(struct sock *sk)
486 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONNECT2)
487 return &iso_pi(sk)->conn->hcon->iso_qos;
489 return &iso_pi(sk)->qos;
492 static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
494 struct iso_conn *conn = iso_pi(sk)->conn;
495 struct bt_iso_qos *qos = iso_sock_get_qos(sk);
496 struct hci_iso_data_hdr *hdr;
499 BT_DBG("sk %p len %d", sk, skb->len);
501 if (skb->len > qos->ucast.out.sdu)
506 /* Push ISO data header */
507 hdr = skb_push(skb, HCI_ISO_DATA_HDR_SIZE);
508 hdr->sn = cpu_to_le16(conn->tx_sn++);
509 hdr->slen = cpu_to_le16(hci_iso_data_len_pack(len,
510 HCI_ISO_STATUS_VALID));
512 if (sk->sk_state == BT_CONNECTED)
513 hci_send_iso(conn->hcon, skb);
520 static void iso_recv_frame(struct iso_conn *conn, struct sk_buff *skb)
526 iso_conn_unlock(conn);
531 BT_DBG("sk %p len %d", sk, skb->len);
533 if (sk->sk_state != BT_CONNECTED)
536 if (!sock_queue_rcv_skb(sk, skb))
543 /* -------- Socket interface ---------- */
544 static struct sock *__iso_get_sock_listen_by_addr(bdaddr_t *src, bdaddr_t *dst)
548 sk_for_each(sk, &iso_sk_list.head) {
549 if (sk->sk_state != BT_LISTEN)
552 if (bacmp(&iso_pi(sk)->dst, dst))
555 if (!bacmp(&iso_pi(sk)->src, src))
562 static struct sock *__iso_get_sock_listen_by_sid(bdaddr_t *ba, bdaddr_t *bc,
567 sk_for_each(sk, &iso_sk_list.head) {
568 if (sk->sk_state != BT_LISTEN)
571 if (bacmp(&iso_pi(sk)->src, ba))
574 if (bacmp(&iso_pi(sk)->dst, bc))
577 if (iso_pi(sk)->bc_sid == sid)
584 /* Find socket listening:
585 * source bdaddr (Unicast)
586 * destination bdaddr (Broadcast only)
587 * match func - pass NULL to ignore
588 * match func data - pass -1 to ignore
589 * Returns closest match.
591 static struct sock *iso_get_sock_listen(bdaddr_t *src, bdaddr_t *dst,
592 iso_sock_match_t match, void *data)
594 struct sock *sk = NULL, *sk1 = NULL;
596 read_lock(&iso_sk_list.lock);
598 sk_for_each(sk, &iso_sk_list.head) {
599 if (sk->sk_state != BT_LISTEN)
602 /* Match Broadcast destination */
603 if (bacmp(dst, BDADDR_ANY) && bacmp(&iso_pi(sk)->dst, dst))
606 /* Use Match function if provided */
607 if (match && !match(sk, data))
611 if (!bacmp(&iso_pi(sk)->src, src)) {
617 if (!bacmp(&iso_pi(sk)->src, BDADDR_ANY)) {
629 read_unlock(&iso_sk_list.lock);
631 return sk ? sk : sk1;
634 static struct sock *iso_get_sock_big(struct sock *match_sk, bdaddr_t *src,
635 bdaddr_t *dst, uint8_t big)
637 struct sock *sk = NULL;
639 read_lock(&iso_sk_list.lock);
641 sk_for_each(sk, &iso_sk_list.head) {
645 /* Look for sockets that have already been
646 * connected to the BIG
648 if (sk->sk_state != BT_CONNECTED &&
649 sk->sk_state != BT_CONNECT)
652 /* Match Broadcast destination */
653 if (bacmp(&iso_pi(sk)->dst, dst))
656 /* Match BIG handle */
657 if (iso_pi(sk)->qos.bcast.big != big)
660 /* Match source address */
661 if (bacmp(&iso_pi(sk)->src, src))
668 read_unlock(&iso_sk_list.lock);
673 static void iso_sock_destruct(struct sock *sk)
677 skb_queue_purge(&sk->sk_receive_queue);
678 skb_queue_purge(&sk->sk_write_queue);
681 static void iso_sock_cleanup_listen(struct sock *parent)
685 BT_DBG("parent %p", parent);
687 /* Close not yet accepted channels */
688 while ((sk = bt_accept_dequeue(parent, NULL))) {
693 /* If listening socket has a hcon, properly disconnect it */
694 if (iso_pi(parent)->conn && iso_pi(parent)->conn->hcon) {
695 iso_sock_disconn(parent);
699 parent->sk_state = BT_CLOSED;
700 sock_set_flag(parent, SOCK_ZAPPED);
703 /* Kill socket (only if zapped and orphan)
704 * Must be called on unlocked socket.
706 static void iso_sock_kill(struct sock *sk)
708 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||
709 sock_flag(sk, SOCK_DEAD))
712 BT_DBG("sk %p state %d", sk, sk->sk_state);
714 /* Kill poor orphan */
715 bt_sock_unlink(&iso_sk_list, sk);
716 sock_set_flag(sk, SOCK_DEAD);
720 static void iso_sock_disconn(struct sock *sk)
723 struct hci_conn *hcon = iso_pi(sk)->conn->hcon;
725 if (test_bit(HCI_CONN_BIG_CREATED, &hcon->flags)) {
726 bis_sk = iso_get_sock_big(sk, &iso_pi(sk)->src,
728 iso_pi(sk)->qos.bcast.big);
730 /* If there are any other connected sockets for the
731 * same BIG, just delete the sk and leave the bis
732 * hcon active, in case later rebinding is needed.
735 hcon->state = BT_OPEN;
736 iso_pi(sk)->conn->hcon = NULL;
737 iso_sock_clear_timer(sk);
738 iso_chan_del(sk, bt_to_errno(hcon->abort_reason));
744 sk->sk_state = BT_DISCONN;
745 iso_sock_set_timer(sk, ISO_DISCONN_TIMEOUT);
746 iso_conn_lock(iso_pi(sk)->conn);
747 hci_conn_drop(iso_pi(sk)->conn->hcon);
748 iso_pi(sk)->conn->hcon = NULL;
749 iso_conn_unlock(iso_pi(sk)->conn);
752 static void __iso_sock_close(struct sock *sk)
754 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
756 switch (sk->sk_state) {
758 iso_sock_cleanup_listen(sk);
764 if (iso_pi(sk)->conn->hcon)
765 iso_sock_disconn(sk);
767 iso_chan_del(sk, ECONNRESET);
771 if (iso_pi(sk)->conn->hcon &&
772 (test_bit(HCI_CONN_PA_SYNC, &iso_pi(sk)->conn->hcon->flags) ||
773 test_bit(HCI_CONN_PA_SYNC_FAILED, &iso_pi(sk)->conn->hcon->flags)))
774 iso_sock_disconn(sk);
776 iso_chan_del(sk, ECONNRESET);
779 iso_chan_del(sk, ECONNRESET);
783 sock_set_flag(sk, SOCK_ZAPPED);
788 /* Must be called on unlocked socket. */
789 static void iso_sock_close(struct sock *sk)
791 iso_sock_clear_timer(sk);
793 __iso_sock_close(sk);
798 static void iso_sock_init(struct sock *sk, struct sock *parent)
803 sk->sk_type = parent->sk_type;
804 bt_sk(sk)->flags = bt_sk(parent)->flags;
805 security_sk_clone(parent, sk);
809 static struct proto iso_proto = {
811 .owner = THIS_MODULE,
812 .obj_size = sizeof(struct iso_pinfo)
815 #define DEFAULT_IO_QOS \
817 .interval = 10000u, \
820 .phy = BT_ISO_PHY_2M, \
824 static struct bt_iso_qos default_qos = {
826 .big = BT_ISO_QOS_BIG_UNSET,
827 .bis = BT_ISO_QOS_BIS_UNSET,
831 .in = DEFAULT_IO_QOS,
832 .out = DEFAULT_IO_QOS,
837 .sync_timeout = BT_ISO_SYNC_TIMEOUT,
838 .sync_cte_type = 0x00,
840 .timeout = BT_ISO_SYNC_TIMEOUT,
844 static struct sock *iso_sock_alloc(struct net *net, struct socket *sock,
845 int proto, gfp_t prio, int kern)
849 sk = bt_sock_alloc(net, sock, &iso_proto, proto, prio, kern);
853 sk->sk_destruct = iso_sock_destruct;
854 sk->sk_sndtimeo = ISO_CONN_TIMEOUT;
856 /* Set address type as public as default src address is BDADDR_ANY */
857 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC;
859 iso_pi(sk)->qos = default_qos;
861 bt_sock_link(&iso_sk_list, sk);
865 static int iso_sock_create(struct net *net, struct socket *sock, int protocol,
870 BT_DBG("sock %p", sock);
872 sock->state = SS_UNCONNECTED;
874 if (sock->type != SOCK_SEQPACKET)
875 return -ESOCKTNOSUPPORT;
877 sock->ops = &iso_sock_ops;
879 sk = iso_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
883 iso_sock_init(sk, NULL);
887 static int iso_sock_bind_bc(struct socket *sock, struct sockaddr *addr,
890 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
891 struct sock *sk = sock->sk;
894 BT_DBG("sk %p bc_sid %u bc_num_bis %u", sk, sa->iso_bc->bc_sid,
895 sa->iso_bc->bc_num_bis);
897 if (addr_len != sizeof(*sa) + sizeof(*sa->iso_bc))
900 bacpy(&iso_pi(sk)->dst, &sa->iso_bc->bc_bdaddr);
902 /* Check if the address type is of LE type */
903 if (!bdaddr_type_is_le(sa->iso_bc->bc_bdaddr_type))
906 iso_pi(sk)->dst_type = sa->iso_bc->bc_bdaddr_type;
907 iso_pi(sk)->sync_handle = -1;
909 if (sa->iso_bc->bc_sid > 0x0f)
912 iso_pi(sk)->bc_sid = sa->iso_bc->bc_sid;
914 if (sa->iso_bc->bc_num_bis > ISO_MAX_NUM_BIS)
917 iso_pi(sk)->bc_num_bis = sa->iso_bc->bc_num_bis;
919 for (i = 0; i < iso_pi(sk)->bc_num_bis; i++)
920 if (sa->iso_bc->bc_bis[i] < 0x01 ||
921 sa->iso_bc->bc_bis[i] > 0x1f)
924 memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis,
925 iso_pi(sk)->bc_num_bis);
930 static int iso_sock_bind_pa_sk(struct sock *sk, struct sockaddr_iso *sa,
935 if (sk->sk_type != SOCK_SEQPACKET) {
940 if (addr_len != sizeof(*sa) + sizeof(*sa->iso_bc)) {
945 if (sa->iso_bc->bc_num_bis > ISO_MAX_NUM_BIS) {
950 iso_pi(sk)->bc_num_bis = sa->iso_bc->bc_num_bis;
952 for (int i = 0; i < iso_pi(sk)->bc_num_bis; i++)
953 if (sa->iso_bc->bc_bis[i] < 0x01 ||
954 sa->iso_bc->bc_bis[i] > 0x1f) {
959 memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis,
960 iso_pi(sk)->bc_num_bis);
966 static int iso_sock_bind(struct socket *sock, struct sockaddr *addr,
969 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
970 struct sock *sk = sock->sk;
973 BT_DBG("sk %p %pMR type %u", sk, &sa->iso_bdaddr, sa->iso_bdaddr_type);
975 if (!addr || addr_len < sizeof(struct sockaddr_iso) ||
976 addr->sa_family != AF_BLUETOOTH)
981 /* Allow the user to bind a PA sync socket to a number
982 * of BISes to sync to.
984 if (sk->sk_state == BT_CONNECT2 &&
985 test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags)) {
986 err = iso_sock_bind_pa_sk(sk, sa, addr_len);
990 if (sk->sk_state != BT_OPEN) {
995 if (sk->sk_type != SOCK_SEQPACKET) {
1000 /* Check if the address type is of LE type */
1001 if (!bdaddr_type_is_le(sa->iso_bdaddr_type)) {
1006 bacpy(&iso_pi(sk)->src, &sa->iso_bdaddr);
1007 iso_pi(sk)->src_type = sa->iso_bdaddr_type;
1009 /* Check for Broadcast address */
1010 if (addr_len > sizeof(*sa)) {
1011 err = iso_sock_bind_bc(sock, addr, addr_len);
1016 sk->sk_state = BT_BOUND;
1023 static int iso_sock_connect(struct socket *sock, struct sockaddr *addr,
1024 int alen, int flags)
1026 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
1027 struct sock *sk = sock->sk;
1030 BT_DBG("sk %p", sk);
1032 if (alen < sizeof(struct sockaddr_iso) ||
1033 addr->sa_family != AF_BLUETOOTH)
1036 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
1039 if (sk->sk_type != SOCK_SEQPACKET)
1042 /* Check if the address type is of LE type */
1043 if (!bdaddr_type_is_le(sa->iso_bdaddr_type))
1048 bacpy(&iso_pi(sk)->dst, &sa->iso_bdaddr);
1049 iso_pi(sk)->dst_type = sa->iso_bdaddr_type;
1053 if (bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
1054 err = iso_connect_cis(sk);
1056 err = iso_connect_bis(sk);
1063 if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
1064 err = bt_sock_wait_state(sk, BT_CONNECTED,
1065 sock_sndtimeo(sk, flags & O_NONBLOCK));
1072 static int iso_listen_bis(struct sock *sk)
1074 struct hci_dev *hdev;
1076 struct iso_conn *conn;
1077 struct hci_conn *hcon;
1079 BT_DBG("%pMR -> %pMR (SID 0x%2.2x)", &iso_pi(sk)->src,
1080 &iso_pi(sk)->dst, iso_pi(sk)->bc_sid);
1082 write_lock(&iso_sk_list.lock);
1084 if (__iso_get_sock_listen_by_sid(&iso_pi(sk)->src, &iso_pi(sk)->dst,
1085 iso_pi(sk)->bc_sid))
1088 write_unlock(&iso_sk_list.lock);
1093 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
1094 iso_pi(sk)->src_type);
1096 return -EHOSTUNREACH;
1100 /* Fail if user set invalid QoS */
1101 if (iso_pi(sk)->qos_user_set && !check_bcast_qos(&iso_pi(sk)->qos)) {
1102 iso_pi(sk)->qos = default_qos;
1107 hcon = hci_pa_create_sync(hdev, &iso_pi(sk)->dst,
1108 le_addr_type(iso_pi(sk)->dst_type),
1109 iso_pi(sk)->bc_sid, &iso_pi(sk)->qos);
1111 err = PTR_ERR(hcon);
1115 conn = iso_conn_add(hcon);
1117 hci_conn_drop(hcon);
1122 err = iso_chan_add(conn, sk, NULL);
1124 hci_conn_drop(hcon);
1131 hci_dev_unlock(hdev);
1135 static int iso_listen_cis(struct sock *sk)
1139 BT_DBG("%pMR", &iso_pi(sk)->src);
1141 write_lock(&iso_sk_list.lock);
1143 if (__iso_get_sock_listen_by_addr(&iso_pi(sk)->src, &iso_pi(sk)->dst))
1146 write_unlock(&iso_sk_list.lock);
1151 static int iso_sock_listen(struct socket *sock, int backlog)
1153 struct sock *sk = sock->sk;
1156 BT_DBG("sk %p backlog %d", sk, backlog);
1160 if (sk->sk_state != BT_BOUND) {
1165 if (sk->sk_type != SOCK_SEQPACKET) {
1170 if (!bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
1171 err = iso_listen_cis(sk);
1173 err = iso_listen_bis(sk);
1178 sk->sk_max_ack_backlog = backlog;
1179 sk->sk_ack_backlog = 0;
1181 sk->sk_state = BT_LISTEN;
1188 static int iso_sock_accept(struct socket *sock, struct socket *newsock,
1189 int flags, bool kern)
1191 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1192 struct sock *sk = sock->sk, *ch;
1198 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1200 BT_DBG("sk %p timeo %ld", sk, timeo);
1202 /* Wait for an incoming connection. (wake-one). */
1203 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1205 if (sk->sk_state != BT_LISTEN) {
1210 ch = bt_accept_dequeue(sk, newsock);
1219 if (signal_pending(current)) {
1220 err = sock_intr_errno(timeo);
1226 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1229 remove_wait_queue(sk_sleep(sk), &wait);
1234 newsock->state = SS_CONNECTED;
1236 BT_DBG("new socket %p", ch);
1243 static int iso_sock_getname(struct socket *sock, struct sockaddr *addr,
1246 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
1247 struct sock *sk = sock->sk;
1249 BT_DBG("sock %p, sk %p", sock, sk);
1251 addr->sa_family = AF_BLUETOOTH;
1254 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->dst);
1255 sa->iso_bdaddr_type = iso_pi(sk)->dst_type;
1257 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->src);
1258 sa->iso_bdaddr_type = iso_pi(sk)->src_type;
1261 return sizeof(struct sockaddr_iso);
1264 static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1267 struct sock *sk = sock->sk;
1268 struct sk_buff *skb, **frag;
1272 BT_DBG("sock %p, sk %p", sock, sk);
1274 err = sock_error(sk);
1278 if (msg->msg_flags & MSG_OOB)
1283 if (sk->sk_state != BT_CONNECTED) {
1288 mtu = iso_pi(sk)->conn->hcon->mtu;
1292 skb = bt_skb_sendmsg(sk, msg, len, mtu, HCI_ISO_DATA_HDR_SIZE, 0);
1294 return PTR_ERR(skb);
1298 BT_DBG("skb %p len %d", sk, skb->len);
1300 /* Continuation fragments */
1301 frag = &skb_shinfo(skb)->frag_list;
1303 struct sk_buff *tmp;
1305 tmp = bt_skb_sendmsg(sk, msg, len, mtu, 0, 0);
1308 return PTR_ERR(tmp);
1315 skb->len += tmp->len;
1316 skb->data_len += tmp->len;
1318 BT_DBG("frag %p len %d", *frag, tmp->len);
1320 frag = &(*frag)->next;
1325 if (sk->sk_state == BT_CONNECTED)
1326 err = iso_send_frame(sk, skb);
1337 static void iso_conn_defer_accept(struct hci_conn *conn)
1339 struct hci_cp_le_accept_cis cp;
1340 struct hci_dev *hdev = conn->hdev;
1342 BT_DBG("conn %p", conn);
1344 conn->state = BT_CONFIG;
1346 cp.handle = cpu_to_le16(conn->handle);
1348 hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp);
1351 static void iso_conn_big_sync(struct sock *sk)
1354 struct hci_dev *hdev;
1356 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
1357 iso_pi(sk)->src_type);
1362 if (!test_and_set_bit(BT_SK_BIG_SYNC, &iso_pi(sk)->flags)) {
1363 err = hci_le_big_create_sync(hdev, iso_pi(sk)->conn->hcon,
1365 iso_pi(sk)->sync_handle,
1366 iso_pi(sk)->bc_num_bis,
1367 iso_pi(sk)->bc_bis);
1369 bt_dev_err(hdev, "hci_le_big_create_sync: %d",
1374 static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1375 size_t len, int flags)
1377 struct sock *sk = sock->sk;
1378 struct iso_pinfo *pi = iso_pi(sk);
1380 BT_DBG("sk %p", sk);
1382 if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
1384 switch (sk->sk_state) {
1386 if (pi->conn->hcon &&
1387 test_bit(HCI_CONN_PA_SYNC, &pi->conn->hcon->flags)) {
1388 iso_conn_big_sync(sk);
1389 sk->sk_state = BT_LISTEN;
1391 iso_conn_defer_accept(pi->conn->hcon);
1392 sk->sk_state = BT_CONFIG;
1398 return iso_connect_cis(sk);
1405 return bt_sock_recvmsg(sock, msg, len, flags);
1408 static bool check_io_qos(struct bt_iso_io_qos *qos)
1410 /* If no PHY is enable SDU must be 0 */
1411 if (!qos->phy && qos->sdu)
1414 if (qos->interval && (qos->interval < 0xff || qos->interval > 0xfffff))
1417 if (qos->latency && (qos->latency < 0x05 || qos->latency > 0xfa0))
1420 if (qos->phy > BT_ISO_PHY_ANY)
1426 static bool check_ucast_qos(struct bt_iso_qos *qos)
1428 if (qos->ucast.cig > 0xef && qos->ucast.cig != BT_ISO_QOS_CIG_UNSET)
1431 if (qos->ucast.cis > 0xef && qos->ucast.cis != BT_ISO_QOS_CIS_UNSET)
1434 if (qos->ucast.sca > 0x07)
1437 if (qos->ucast.packing > 0x01)
1440 if (qos->ucast.framing > 0x01)
1443 if (!check_io_qos(&qos->ucast.in))
1446 if (!check_io_qos(&qos->ucast.out))
1452 static bool check_bcast_qos(struct bt_iso_qos *qos)
1454 if (!qos->bcast.sync_factor)
1455 qos->bcast.sync_factor = 0x01;
1457 if (qos->bcast.packing > 0x01)
1460 if (qos->bcast.framing > 0x01)
1463 if (!check_io_qos(&qos->bcast.in))
1466 if (!check_io_qos(&qos->bcast.out))
1469 if (qos->bcast.encryption > 0x01)
1472 if (qos->bcast.options > 0x07)
1475 if (qos->bcast.skip > 0x01f3)
1478 if (!qos->bcast.sync_timeout)
1479 qos->bcast.sync_timeout = BT_ISO_SYNC_TIMEOUT;
1481 if (qos->bcast.sync_timeout < 0x000a || qos->bcast.sync_timeout > 0x4000)
1484 if (qos->bcast.sync_cte_type > 0x1f)
1487 if (qos->bcast.mse > 0x1f)
1490 if (!qos->bcast.timeout)
1491 qos->bcast.sync_timeout = BT_ISO_SYNC_TIMEOUT;
1493 if (qos->bcast.timeout < 0x000a || qos->bcast.timeout > 0x4000)
1499 static int iso_sock_setsockopt(struct socket *sock, int level, int optname,
1500 sockptr_t optval, unsigned int optlen)
1502 struct sock *sk = sock->sk;
1504 struct bt_iso_qos qos = default_qos;
1507 BT_DBG("sk %p", sk);
1512 case BT_DEFER_SETUP:
1513 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1518 err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, optlen);
1523 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1525 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1529 err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, optlen);
1534 set_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags);
1536 clear_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags);
1540 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1541 sk->sk_state != BT_CONNECT2) {
1546 err = bt_copy_from_sockptr(&qos, sizeof(qos), optval, optlen);
1550 iso_pi(sk)->qos = qos;
1551 iso_pi(sk)->qos_user_set = true;
1556 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1557 sk->sk_state != BT_CONNECT2) {
1562 if (optlen > sizeof(iso_pi(sk)->base)) {
1567 err = bt_copy_from_sockptr(iso_pi(sk)->base, optlen, optval,
1572 iso_pi(sk)->base_len = optlen;
1585 static int iso_sock_getsockopt(struct socket *sock, int level, int optname,
1586 char __user *optval, int __user *optlen)
1588 struct sock *sk = sock->sk;
1590 struct bt_iso_qos *qos;
1594 BT_DBG("sk %p", sk);
1596 if (get_user(len, optlen))
1602 case BT_DEFER_SETUP:
1603 if (sk->sk_state == BT_CONNECTED) {
1608 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1609 (u32 __user *)optval))
1615 if (put_user(test_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags),
1616 (int __user *)optval))
1621 qos = iso_sock_get_qos(sk);
1623 len = min_t(unsigned int, len, sizeof(*qos));
1624 if (copy_to_user(optval, qos, len))
1630 if (sk->sk_state == BT_CONNECTED &&
1631 !bacmp(&iso_pi(sk)->dst, BDADDR_ANY)) {
1632 base_len = iso_pi(sk)->conn->hcon->le_per_adv_data_len;
1633 base = iso_pi(sk)->conn->hcon->le_per_adv_data;
1635 base_len = iso_pi(sk)->base_len;
1636 base = iso_pi(sk)->base;
1639 len = min_t(unsigned int, len, base_len);
1640 if (copy_to_user(optval, base, len))
1642 if (put_user(len, optlen))
1656 static int iso_sock_shutdown(struct socket *sock, int how)
1658 struct sock *sk = sock->sk;
1661 BT_DBG("sock %p, sk %p, how %d", sock, sk, how);
1671 if (sk->sk_shutdown & RCV_SHUTDOWN)
1673 sk->sk_shutdown |= RCV_SHUTDOWN;
1676 if (sk->sk_shutdown & SEND_SHUTDOWN)
1678 sk->sk_shutdown |= SEND_SHUTDOWN;
1681 if (sk->sk_shutdown & SHUTDOWN_MASK)
1683 sk->sk_shutdown |= SHUTDOWN_MASK;
1687 iso_sock_clear_timer(sk);
1688 __iso_sock_close(sk);
1690 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1691 !(current->flags & PF_EXITING))
1692 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1701 static int iso_sock_release(struct socket *sock)
1703 struct sock *sk = sock->sk;
1706 BT_DBG("sock %p, sk %p", sock, sk);
1713 if (sock_flag(sk, SOCK_LINGER) && READ_ONCE(sk->sk_lingertime) &&
1714 !(current->flags & PF_EXITING)) {
1716 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1725 static void iso_sock_ready(struct sock *sk)
1727 BT_DBG("sk %p", sk);
1733 iso_sock_clear_timer(sk);
1734 sk->sk_state = BT_CONNECTED;
1735 sk->sk_state_change(sk);
1739 struct iso_list_data {
1740 struct hci_conn *hcon;
1744 static bool iso_match_big(struct sock *sk, void *data)
1746 struct hci_evt_le_big_sync_estabilished *ev = data;
1748 return ev->handle == iso_pi(sk)->qos.bcast.big;
1751 static bool iso_match_pa_sync_flag(struct sock *sk, void *data)
1753 return test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags);
1756 static void iso_conn_ready(struct iso_conn *conn)
1758 struct sock *parent = NULL;
1759 struct sock *sk = conn->sk;
1760 struct hci_ev_le_big_sync_estabilished *ev = NULL;
1761 struct hci_ev_le_pa_sync_established *ev2 = NULL;
1762 struct hci_evt_le_big_info_adv_report *ev3 = NULL;
1763 struct hci_conn *hcon;
1765 BT_DBG("conn %p", conn);
1768 iso_sock_ready(conn->sk);
1774 if (test_bit(HCI_CONN_BIG_SYNC, &hcon->flags) ||
1775 test_bit(HCI_CONN_BIG_SYNC_FAILED, &hcon->flags)) {
1776 ev = hci_recv_event_data(hcon->hdev,
1777 HCI_EVT_LE_BIG_SYNC_ESTABILISHED);
1779 /* Get reference to PA sync parent socket, if it exists */
1780 parent = iso_get_sock_listen(&hcon->src,
1782 iso_match_pa_sync_flag, NULL);
1784 parent = iso_get_sock_listen(&hcon->src,
1787 } else if (test_bit(HCI_CONN_PA_SYNC_FAILED, &hcon->flags)) {
1788 ev2 = hci_recv_event_data(hcon->hdev,
1789 HCI_EV_LE_PA_SYNC_ESTABLISHED);
1791 parent = iso_get_sock_listen(&hcon->src,
1793 iso_match_sid, ev2);
1794 } else if (test_bit(HCI_CONN_PA_SYNC, &hcon->flags)) {
1795 ev3 = hci_recv_event_data(hcon->hdev,
1796 HCI_EVT_LE_BIG_INFO_ADV_REPORT);
1798 parent = iso_get_sock_listen(&hcon->src,
1800 iso_match_sync_handle, ev3);
1804 parent = iso_get_sock_listen(&hcon->src,
1805 BDADDR_ANY, NULL, NULL);
1812 sk = iso_sock_alloc(sock_net(parent), NULL,
1813 BTPROTO_ISO, GFP_ATOMIC, 0);
1815 release_sock(parent);
1819 iso_sock_init(sk, parent);
1821 bacpy(&iso_pi(sk)->src, &hcon->src);
1823 /* Convert from HCI to three-value type */
1824 if (hcon->src_type == ADDR_LE_DEV_PUBLIC)
1825 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC;
1827 iso_pi(sk)->src_type = BDADDR_LE_RANDOM;
1829 /* If hcon has no destination address (BDADDR_ANY) it means it
1830 * was created by HCI_EV_LE_BIG_SYNC_ESTABILISHED or
1831 * HCI_EV_LE_PA_SYNC_ESTABLISHED so we need to initialize using
1832 * the parent socket destination address.
1834 if (!bacmp(&hcon->dst, BDADDR_ANY)) {
1835 bacpy(&hcon->dst, &iso_pi(parent)->dst);
1836 hcon->dst_type = iso_pi(parent)->dst_type;
1837 hcon->sync_handle = iso_pi(parent)->sync_handle;
1841 iso_pi(sk)->qos = iso_pi(parent)->qos;
1842 iso_pi(sk)->qos.bcast.encryption = ev3->encryption;
1843 hcon->iso_qos = iso_pi(sk)->qos;
1844 iso_pi(sk)->bc_num_bis = iso_pi(parent)->bc_num_bis;
1845 memcpy(iso_pi(sk)->bc_bis, iso_pi(parent)->bc_bis, ISO_MAX_NUM_BIS);
1846 set_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags);
1849 bacpy(&iso_pi(sk)->dst, &hcon->dst);
1850 iso_pi(sk)->dst_type = hcon->dst_type;
1851 iso_pi(sk)->sync_handle = iso_pi(parent)->sync_handle;
1852 memcpy(iso_pi(sk)->base, iso_pi(parent)->base, iso_pi(parent)->base_len);
1853 iso_pi(sk)->base_len = iso_pi(parent)->base_len;
1855 hci_conn_hold(hcon);
1856 iso_chan_add(conn, sk, parent);
1858 if ((ev && ((struct hci_evt_le_big_sync_estabilished *)ev)->status) ||
1859 (ev2 && ev2->status)) {
1860 /* Trigger error signal on child socket */
1861 sk->sk_err = ECONNREFUSED;
1862 sk->sk_error_report(sk);
1865 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1866 sk->sk_state = BT_CONNECT2;
1868 sk->sk_state = BT_CONNECTED;
1870 /* Wake up parent */
1871 parent->sk_data_ready(parent);
1873 release_sock(parent);
1878 static bool iso_match_sid(struct sock *sk, void *data)
1880 struct hci_ev_le_pa_sync_established *ev = data;
1882 return ev->sid == iso_pi(sk)->bc_sid;
1885 static bool iso_match_sync_handle(struct sock *sk, void *data)
1887 struct hci_evt_le_big_info_adv_report *ev = data;
1889 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle;
1892 static bool iso_match_sync_handle_pa_report(struct sock *sk, void *data)
1894 struct hci_ev_le_per_adv_report *ev = data;
1896 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle;
1899 /* ----- ISO interface with lower layer (HCI) ----- */
1901 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1903 struct hci_ev_le_pa_sync_established *ev1;
1904 struct hci_evt_le_big_info_adv_report *ev2;
1905 struct hci_ev_le_per_adv_report *ev3;
1908 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
1910 /* Broadcast receiver requires handling of some events before it can
1911 * proceed to establishing a BIG sync:
1913 * 1. HCI_EV_LE_PA_SYNC_ESTABLISHED: The socket may specify a specific
1914 * SID to listen to and once sync is estabilished its handle needs to
1915 * be stored in iso_pi(sk)->sync_handle so it can be matched once
1916 * receiving the BIG Info.
1917 * 2. HCI_EVT_LE_BIG_INFO_ADV_REPORT: When connect_ind is triggered by a
1918 * a BIG Info it attempts to check if there any listening socket with
1919 * the same sync_handle and if it does then attempt to create a sync.
1920 * 3. HCI_EV_LE_PER_ADV_REPORT: When a PA report is received, it is stored
1921 * in iso_pi(sk)->base so it can be passed up to user, in the case of a
1924 ev1 = hci_recv_event_data(hdev, HCI_EV_LE_PA_SYNC_ESTABLISHED);
1926 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr, iso_match_sid,
1928 if (sk && !ev1->status)
1929 iso_pi(sk)->sync_handle = le16_to_cpu(ev1->handle);
1934 ev2 = hci_recv_event_data(hdev, HCI_EVT_LE_BIG_INFO_ADV_REPORT);
1936 /* Try to get PA sync listening socket, if it exists */
1937 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr,
1938 iso_match_pa_sync_flag, NULL);
1941 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr,
1942 iso_match_sync_handle, ev2);
1944 /* If PA Sync is in process of terminating,
1945 * do not handle any more BIGInfo adv reports.
1948 if (sk && test_bit(BT_SK_PA_SYNC_TERM,
1949 &iso_pi(sk)->flags))
1956 if (ev2->num_bis < iso_pi(sk)->bc_num_bis)
1957 iso_pi(sk)->bc_num_bis = ev2->num_bis;
1959 if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags) &&
1960 !test_and_set_bit(BT_SK_BIG_SYNC, &iso_pi(sk)->flags)) {
1961 err = hci_le_big_create_sync(hdev, NULL,
1963 iso_pi(sk)->sync_handle,
1964 iso_pi(sk)->bc_num_bis,
1965 iso_pi(sk)->bc_bis);
1967 bt_dev_err(hdev, "hci_le_big_create_sync: %d",
1976 ev3 = hci_recv_event_data(hdev, HCI_EV_LE_PER_ADV_REPORT);
1978 size_t base_len = 0;
1980 struct hci_conn *hcon;
1982 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr,
1983 iso_match_sync_handle_pa_report, ev3);
1987 hcon = iso_pi(sk)->conn->hcon;
1991 if (ev3->data_status == LE_PA_DATA_TRUNCATED) {
1992 /* The controller was unable to retrieve PA data. */
1993 memset(hcon->le_per_adv_data, 0,
1994 HCI_MAX_PER_AD_TOT_LEN);
1995 hcon->le_per_adv_data_len = 0;
1996 hcon->le_per_adv_data_offset = 0;
2000 if (hcon->le_per_adv_data_offset + ev3->length >
2001 HCI_MAX_PER_AD_TOT_LEN)
2004 memcpy(hcon->le_per_adv_data + hcon->le_per_adv_data_offset,
2005 ev3->data, ev3->length);
2006 hcon->le_per_adv_data_offset += ev3->length;
2008 if (ev3->data_status == LE_PA_DATA_COMPLETE) {
2009 /* All PA data has been received. */
2010 hcon->le_per_adv_data_len =
2011 hcon->le_per_adv_data_offset;
2012 hcon->le_per_adv_data_offset = 0;
2015 base = eir_get_service_data(hcon->le_per_adv_data,
2016 hcon->le_per_adv_data_len,
2017 EIR_BAA_SERVICE_UUID,
2020 if (!base || base_len > BASE_MAX_LENGTH)
2023 memcpy(iso_pi(sk)->base, base, base_len);
2024 iso_pi(sk)->base_len = base_len;
2026 /* This is a PA data fragment. Keep pa_data_len set to 0
2027 * until all data has been reassembled.
2029 hcon->le_per_adv_data_len = 0;
2032 sk = iso_get_sock_listen(&hdev->bdaddr, BDADDR_ANY, NULL, NULL);
2039 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
2040 *flags |= HCI_PROTO_DEFER;
2044 return HCI_LM_ACCEPT;
2047 static void iso_connect_cfm(struct hci_conn *hcon, __u8 status)
2049 if (hcon->type != ISO_LINK) {
2050 if (hcon->type != LE_LINK)
2053 /* Check if LE link has failed */
2055 struct hci_link *link, *t;
2057 list_for_each_entry_safe(link, t, &hcon->link_list,
2059 iso_conn_del(link->conn, bt_to_errno(status));
2064 /* Create CIS if pending */
2065 hci_le_create_cis_pending(hcon->hdev);
2069 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
2071 /* Similar to the success case, if HCI_CONN_BIG_SYNC_FAILED or
2072 * HCI_CONN_PA_SYNC_FAILED is set, queue the failed connection
2073 * into the accept queue of the listening socket and wake up
2074 * userspace, to inform the user about the event.
2076 if (!status || test_bit(HCI_CONN_BIG_SYNC_FAILED, &hcon->flags) ||
2077 test_bit(HCI_CONN_PA_SYNC_FAILED, &hcon->flags)) {
2078 struct iso_conn *conn;
2080 conn = iso_conn_add(hcon);
2082 iso_conn_ready(conn);
2084 iso_conn_del(hcon, bt_to_errno(status));
2088 static void iso_disconn_cfm(struct hci_conn *hcon, __u8 reason)
2090 if (hcon->type != ISO_LINK)
2093 BT_DBG("hcon %p reason %d", hcon, reason);
2095 iso_conn_del(hcon, bt_to_errno(reason));
2098 void iso_recv(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
2100 struct iso_conn *conn = hcon->iso_data;
2106 pb = hci_iso_flags_pb(flags);
2107 ts = hci_iso_flags_ts(flags);
2109 BT_DBG("conn %p len %d pb 0x%x ts 0x%x", conn, skb->len, pb, ts);
2115 BT_ERR("Unexpected start frame (len %d)", skb->len);
2116 kfree_skb(conn->rx_skb);
2117 conn->rx_skb = NULL;
2122 struct hci_iso_ts_data_hdr *hdr;
2124 /* TODO: add timestamp to the packet? */
2125 hdr = skb_pull_data(skb, HCI_ISO_TS_DATA_HDR_SIZE);
2127 BT_ERR("Frame is too short (len %d)", skb->len);
2131 len = __le16_to_cpu(hdr->slen);
2133 struct hci_iso_data_hdr *hdr;
2135 hdr = skb_pull_data(skb, HCI_ISO_DATA_HDR_SIZE);
2137 BT_ERR("Frame is too short (len %d)", skb->len);
2141 len = __le16_to_cpu(hdr->slen);
2144 flags = hci_iso_data_flags(len);
2145 len = hci_iso_data_len(len);
2147 BT_DBG("Start: total len %d, frag len %d flags 0x%4.4x", len,
2150 if (len == skb->len) {
2151 /* Complete frame received */
2152 hci_skb_pkt_status(skb) = flags & 0x03;
2153 iso_recv_frame(conn, skb);
2157 if (pb == ISO_SINGLE) {
2158 BT_ERR("Frame malformed (len %d, expected len %d)",
2163 if (skb->len > len) {
2164 BT_ERR("Frame is too long (len %d, expected len %d)",
2169 /* Allocate skb for the complete frame (with header) */
2170 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
2174 hci_skb_pkt_status(conn->rx_skb) = flags & 0x03;
2175 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
2177 conn->rx_len = len - skb->len;
2181 BT_DBG("Cont: frag len %d (expecting %d)", skb->len,
2184 if (!conn->rx_len) {
2185 BT_ERR("Unexpected continuation frame (len %d)",
2190 if (skb->len > conn->rx_len) {
2191 BT_ERR("Fragment is too long (len %d, expected %d)",
2192 skb->len, conn->rx_len);
2193 kfree_skb(conn->rx_skb);
2194 conn->rx_skb = NULL;
2199 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
2201 conn->rx_len -= skb->len;
2205 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
2207 conn->rx_len -= skb->len;
2209 if (!conn->rx_len) {
2210 struct sk_buff *rx_skb = conn->rx_skb;
2212 /* Complete frame received. iso_recv_frame
2213 * takes ownership of the skb so set the global
2214 * rx_skb pointer to NULL first.
2216 conn->rx_skb = NULL;
2217 iso_recv_frame(conn, rx_skb);
2226 static struct hci_cb iso_cb = {
2228 .connect_cfm = iso_connect_cfm,
2229 .disconn_cfm = iso_disconn_cfm,
2232 static int iso_debugfs_show(struct seq_file *f, void *p)
2236 read_lock(&iso_sk_list.lock);
2238 sk_for_each(sk, &iso_sk_list.head) {
2239 seq_printf(f, "%pMR %pMR %d\n", &iso_pi(sk)->src,
2240 &iso_pi(sk)->dst, sk->sk_state);
2243 read_unlock(&iso_sk_list.lock);
2248 DEFINE_SHOW_ATTRIBUTE(iso_debugfs);
2250 static struct dentry *iso_debugfs;
2252 static const struct proto_ops iso_sock_ops = {
2253 .family = PF_BLUETOOTH,
2254 .owner = THIS_MODULE,
2255 .release = iso_sock_release,
2256 .bind = iso_sock_bind,
2257 .connect = iso_sock_connect,
2258 .listen = iso_sock_listen,
2259 .accept = iso_sock_accept,
2260 .getname = iso_sock_getname,
2261 .sendmsg = iso_sock_sendmsg,
2262 .recvmsg = iso_sock_recvmsg,
2263 .poll = bt_sock_poll,
2264 .ioctl = bt_sock_ioctl,
2265 .mmap = sock_no_mmap,
2266 .socketpair = sock_no_socketpair,
2267 .shutdown = iso_sock_shutdown,
2268 .setsockopt = iso_sock_setsockopt,
2269 .getsockopt = iso_sock_getsockopt
2272 static const struct net_proto_family iso_sock_family_ops = {
2273 .family = PF_BLUETOOTH,
2274 .owner = THIS_MODULE,
2275 .create = iso_sock_create,
2278 static bool iso_inited;
2280 bool iso_enabled(void)
2289 BUILD_BUG_ON(sizeof(struct sockaddr_iso) > sizeof(struct sockaddr));
2294 err = proto_register(&iso_proto, 0);
2298 err = bt_sock_register(BTPROTO_ISO, &iso_sock_family_ops);
2300 BT_ERR("ISO socket registration failed");
2304 err = bt_procfs_init(&init_net, "iso", &iso_sk_list, NULL);
2306 BT_ERR("Failed to create ISO proc file");
2307 bt_sock_unregister(BTPROTO_ISO);
2311 BT_INFO("ISO socket layer initialized");
2313 hci_register_cb(&iso_cb);
2315 if (IS_ERR_OR_NULL(bt_debugfs))
2319 iso_debugfs = debugfs_create_file("iso", 0444, bt_debugfs,
2320 NULL, &iso_debugfs_fops);
2328 proto_unregister(&iso_proto);
2337 bt_procfs_cleanup(&init_net, "iso");
2339 debugfs_remove(iso_debugfs);
2342 hci_unregister_cb(&iso_cb);
2344 bt_sock_unregister(BTPROTO_ISO);
2346 proto_unregister(&iso_proto);