1 // SPDX-License-Identifier: GPL-2.0
3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2022 Intel Corporation
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>
18 static const struct proto_ops iso_sock_ops;
20 static struct bt_sock_list iso_sk_list = {
21 .lock = __RW_LOCK_UNLOCKED(iso_sk_list.lock)
24 /* ---- ISO connections ---- */
26 struct hci_conn *hcon;
28 /* @lock: spinlock protecting changes to iso_conn fields */
32 struct delayed_work timeout_work;
34 struct sk_buff *rx_skb;
39 #define iso_conn_lock(c) spin_lock(&(c)->lock)
40 #define iso_conn_unlock(c) spin_unlock(&(c)->lock)
42 static void iso_sock_close(struct sock *sk);
43 static void iso_sock_kill(struct sock *sk);
45 /* ----- ISO socket info ----- */
46 #define iso_pi(sk) ((struct iso_pinfo *)sk)
48 #define EIR_SERVICE_DATA_LENGTH 4
49 #define BASE_MAX_LENGTH (HCI_MAX_PER_AD_LENGTH - EIR_SERVICE_DATA_LENGTH)
51 /* iso_pinfo flags values */
66 __u8 bc_bis[ISO_MAX_NUM_BIS];
69 struct bt_iso_qos qos;
72 __u8 base[BASE_MAX_LENGTH];
73 struct iso_conn *conn;
76 static struct bt_iso_qos default_qos;
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);
84 typedef bool (*iso_sock_match_t)(struct sock *sk, void *data);
86 static struct sock *iso_get_sock_listen(bdaddr_t *src, bdaddr_t *dst,
87 iso_sock_match_t match, void *data);
89 /* ---- ISO timers ---- */
90 #define ISO_CONN_TIMEOUT (HZ * 40)
91 #define ISO_DISCONN_TIMEOUT (HZ * 2)
93 static void iso_sock_timeout(struct work_struct *work)
95 struct iso_conn *conn = container_of(work, struct iso_conn,
103 iso_conn_unlock(conn);
108 BT_DBG("sock %p state %d", sk, sk->sk_state);
111 sk->sk_err = ETIMEDOUT;
112 sk->sk_state_change(sk);
117 static void iso_sock_set_timer(struct sock *sk, long timeout)
119 if (!iso_pi(sk)->conn)
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);
127 static void iso_sock_clear_timer(struct sock *sk)
129 if (!iso_pi(sk)->conn)
132 BT_DBG("sock %p state %d", sk, sk->sk_state);
133 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
136 /* ---- ISO connections ---- */
137 static struct iso_conn *iso_conn_add(struct hci_conn *hcon)
139 struct iso_conn *conn = hcon->iso_data;
147 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
151 spin_lock_init(&conn->lock);
152 INIT_DELAYED_WORK(&conn->timeout_work, iso_sock_timeout);
154 hcon->iso_data = conn;
158 BT_DBG("hcon %p conn %p", hcon, conn);
163 /* Delete channel. Must be called on the locked socket. */
164 static void iso_chan_del(struct sock *sk, int err)
166 struct iso_conn *conn;
169 conn = iso_pi(sk)->conn;
171 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
176 iso_pi(sk)->conn = NULL;
177 iso_conn_unlock(conn);
180 hci_conn_drop(conn->hcon);
183 sk->sk_state = BT_CLOSED;
186 parent = bt_sk(sk)->parent;
188 bt_accept_unlink(sk);
189 parent->sk_data_ready(parent);
191 sk->sk_state_change(sk);
194 sock_set_flag(sk, SOCK_ZAPPED);
197 static bool iso_match_conn_sync_handle(struct sock *sk, void *data)
199 struct hci_conn *hcon = data;
201 if (test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags))
204 return hcon->sync_handle == iso_pi(sk)->sync_handle;
207 static void iso_conn_del(struct hci_conn *hcon, int err)
209 struct iso_conn *conn = hcon->iso_data;
216 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
223 iso_conn_unlock(conn);
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.
233 if (test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags)) {
234 parent = iso_get_sock_listen(&hcon->src,
236 iso_match_conn_sync_handle,
240 set_bit(BT_SK_PA_SYNC_TERM,
241 &iso_pi(parent)->flags);
246 iso_sock_clear_timer(sk);
247 iso_chan_del(sk, err);
252 /* Ensure no more work items will run before freeing conn. */
253 cancel_delayed_work_sync(&conn->timeout_work);
255 hcon->iso_data = NULL;
259 static int __iso_chan_add(struct iso_conn *conn, struct sock *sk,
262 BT_DBG("conn %p", conn);
264 if (iso_pi(sk)->conn == conn && conn->sk == sk)
268 BT_ERR("conn->sk already set");
272 iso_pi(sk)->conn = conn;
276 bt_accept_enqueue(parent, sk, true);
281 static int iso_chan_add(struct iso_conn *conn, struct sock *sk,
287 err = __iso_chan_add(conn, sk, parent);
288 iso_conn_unlock(conn);
293 static inline u8 le_addr_type(u8 bdaddr_type)
295 if (bdaddr_type == BDADDR_LE_PUBLIC)
296 return ADDR_LE_DEV_PUBLIC;
298 return ADDR_LE_DEV_RANDOM;
301 static int iso_connect_bis(struct sock *sk)
303 struct iso_conn *conn;
304 struct hci_conn *hcon;
305 struct hci_dev *hdev;
308 BT_DBG("%pMR", &iso_pi(sk)->src);
310 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
311 iso_pi(sk)->src_type);
313 return -EHOSTUNREACH;
317 if (!bis_capable(hdev)) {
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;
329 /* Fail if out PHYs are marked as disabled */
330 if (!iso_pi(sk)->qos.bcast.out.phy) {
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,
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,
355 conn = iso_conn_add(hcon);
364 err = iso_chan_add(conn, sk, NULL);
370 /* Update source addr of the socket */
371 bacpy(&iso_pi(sk)->src, &hcon->src);
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;
380 sk->sk_state = BT_CONNECT;
381 iso_sock_set_timer(sk, sk->sk_sndtimeo);
387 hci_dev_unlock(hdev);
392 static int iso_connect_cis(struct sock *sk)
394 struct iso_conn *conn;
395 struct hci_conn *hcon;
396 struct hci_dev *hdev;
399 BT_DBG("%pMR -> %pMR", &iso_pi(sk)->src, &iso_pi(sk)->dst);
401 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
402 iso_pi(sk)->src_type);
404 return -EHOSTUNREACH;
408 if (!cis_central_capable(hdev)) {
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;
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) {
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),
436 hcon = hci_connect_cis(hdev, &iso_pi(sk)->dst,
437 le_addr_type(iso_pi(sk)->dst_type),
445 conn = iso_conn_add(hcon);
454 err = iso_chan_add(conn, sk, NULL);
460 /* Update source addr of the socket */
461 bacpy(&iso_pi(sk)->src, &hcon->src);
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;
470 sk->sk_state = BT_CONNECT;
471 iso_sock_set_timer(sk, sk->sk_sndtimeo);
477 hci_dev_unlock(hdev);
482 static struct bt_iso_qos *iso_sock_get_qos(struct sock *sk)
484 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONNECT2)
485 return &iso_pi(sk)->conn->hcon->iso_qos;
487 return &iso_pi(sk)->qos;
490 static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
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;
497 BT_DBG("sk %p len %d", sk, skb->len);
499 if (skb->len > qos->ucast.out.sdu)
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));
510 if (sk->sk_state == BT_CONNECTED)
511 hci_send_iso(conn->hcon, skb);
518 static void iso_recv_frame(struct iso_conn *conn, struct sk_buff *skb)
524 iso_conn_unlock(conn);
529 BT_DBG("sk %p len %d", sk, skb->len);
531 if (sk->sk_state != BT_CONNECTED)
534 if (!sock_queue_rcv_skb(sk, skb))
541 /* -------- Socket interface ---------- */
542 static struct sock *__iso_get_sock_listen_by_addr(bdaddr_t *src, bdaddr_t *dst)
546 sk_for_each(sk, &iso_sk_list.head) {
547 if (sk->sk_state != BT_LISTEN)
550 if (bacmp(&iso_pi(sk)->dst, dst))
553 if (!bacmp(&iso_pi(sk)->src, src))
560 static struct sock *__iso_get_sock_listen_by_sid(bdaddr_t *ba, bdaddr_t *bc,
565 sk_for_each(sk, &iso_sk_list.head) {
566 if (sk->sk_state != BT_LISTEN)
569 if (bacmp(&iso_pi(sk)->src, ba))
572 if (bacmp(&iso_pi(sk)->dst, bc))
575 if (iso_pi(sk)->bc_sid == sid)
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.
589 static struct sock *iso_get_sock_listen(bdaddr_t *src, bdaddr_t *dst,
590 iso_sock_match_t match, void *data)
592 struct sock *sk = NULL, *sk1 = NULL;
594 read_lock(&iso_sk_list.lock);
596 sk_for_each(sk, &iso_sk_list.head) {
597 if (sk->sk_state != BT_LISTEN)
600 /* Match Broadcast destination */
601 if (bacmp(dst, BDADDR_ANY) && bacmp(&iso_pi(sk)->dst, dst))
604 /* Use Match function if provided */
605 if (match && !match(sk, data))
609 if (!bacmp(&iso_pi(sk)->src, src))
613 if (!bacmp(&iso_pi(sk)->src, BDADDR_ANY))
617 read_unlock(&iso_sk_list.lock);
619 return sk ? sk : sk1;
622 static void iso_sock_destruct(struct sock *sk)
626 skb_queue_purge(&sk->sk_receive_queue);
627 skb_queue_purge(&sk->sk_write_queue);
630 static void iso_sock_cleanup_listen(struct sock *parent)
634 BT_DBG("parent %p", parent);
636 /* Close not yet accepted channels */
637 while ((sk = bt_accept_dequeue(parent, NULL))) {
642 /* If listening socket stands for a PA sync connection,
643 * properly disconnect the hcon and socket.
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);
651 parent->sk_state = BT_CLOSED;
652 sock_set_flag(parent, SOCK_ZAPPED);
655 /* Kill socket (only if zapped and orphan)
656 * Must be called on unlocked socket.
658 static void iso_sock_kill(struct sock *sk)
660 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||
661 sock_flag(sk, SOCK_DEAD))
664 BT_DBG("sk %p state %d", sk, sk->sk_state);
666 /* Kill poor orphan */
667 bt_sock_unlink(&iso_sk_list, sk);
668 sock_set_flag(sk, SOCK_DEAD);
672 static void iso_sock_disconn(struct sock *sk)
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);
682 static void __iso_sock_close(struct sock *sk)
684 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
686 switch (sk->sk_state) {
688 iso_sock_cleanup_listen(sk);
694 if (iso_pi(sk)->conn->hcon)
695 iso_sock_disconn(sk);
697 iso_chan_del(sk, ECONNRESET);
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);
706 iso_chan_del(sk, ECONNRESET);
709 iso_chan_del(sk, ECONNRESET);
713 sock_set_flag(sk, SOCK_ZAPPED);
718 /* Must be called on unlocked socket. */
719 static void iso_sock_close(struct sock *sk)
721 iso_sock_clear_timer(sk);
723 __iso_sock_close(sk);
728 static void iso_sock_init(struct sock *sk, struct sock *parent)
733 sk->sk_type = parent->sk_type;
734 bt_sk(sk)->flags = bt_sk(parent)->flags;
735 security_sk_clone(parent, sk);
739 static struct proto iso_proto = {
741 .owner = THIS_MODULE,
742 .obj_size = sizeof(struct iso_pinfo)
745 #define DEFAULT_IO_QOS \
747 .interval = 10000u, \
750 .phy = BT_ISO_PHY_2M, \
754 static struct bt_iso_qos default_qos = {
756 .big = BT_ISO_QOS_BIG_UNSET,
757 .bis = BT_ISO_QOS_BIS_UNSET,
761 .in = DEFAULT_IO_QOS,
762 .out = DEFAULT_IO_QOS,
767 .sync_timeout = 0x4000,
768 .sync_cte_type = 0x00,
774 static struct sock *iso_sock_alloc(struct net *net, struct socket *sock,
775 int proto, gfp_t prio, int kern)
779 sk = bt_sock_alloc(net, sock, &iso_proto, proto, prio, kern);
783 sk->sk_destruct = iso_sock_destruct;
784 sk->sk_sndtimeo = ISO_CONN_TIMEOUT;
786 /* Set address type as public as default src address is BDADDR_ANY */
787 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC;
789 iso_pi(sk)->qos = default_qos;
791 bt_sock_link(&iso_sk_list, sk);
795 static int iso_sock_create(struct net *net, struct socket *sock, int protocol,
800 BT_DBG("sock %p", sock);
802 sock->state = SS_UNCONNECTED;
804 if (sock->type != SOCK_SEQPACKET)
805 return -ESOCKTNOSUPPORT;
807 sock->ops = &iso_sock_ops;
809 sk = iso_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
813 iso_sock_init(sk, NULL);
817 static int iso_sock_bind_bc(struct socket *sock, struct sockaddr *addr,
820 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
821 struct sock *sk = sock->sk;
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);
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)
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;
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)
842 memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis,
843 iso_pi(sk)->bc_num_bis);
849 static int iso_sock_bind(struct socket *sock, struct sockaddr *addr,
852 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
853 struct sock *sk = sock->sk;
856 BT_DBG("sk %p %pMR type %u", sk, &sa->iso_bdaddr, sa->iso_bdaddr_type);
858 if (!addr || addr_len < sizeof(struct sockaddr_iso) ||
859 addr->sa_family != AF_BLUETOOTH)
864 if (sk->sk_state != BT_OPEN) {
869 if (sk->sk_type != SOCK_SEQPACKET) {
874 /* Check if the address type is of LE type */
875 if (!bdaddr_type_is_le(sa->iso_bdaddr_type)) {
880 bacpy(&iso_pi(sk)->src, &sa->iso_bdaddr);
881 iso_pi(sk)->src_type = sa->iso_bdaddr_type;
883 /* Check for Broadcast address */
884 if (addr_len > sizeof(*sa)) {
885 err = iso_sock_bind_bc(sock, addr, addr_len);
890 sk->sk_state = BT_BOUND;
897 static int iso_sock_connect(struct socket *sock, struct sockaddr *addr,
900 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
901 struct sock *sk = sock->sk;
906 if (alen < sizeof(struct sockaddr_iso) ||
907 addr->sa_family != AF_BLUETOOTH)
910 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
913 if (sk->sk_type != SOCK_SEQPACKET)
916 /* Check if the address type is of LE type */
917 if (!bdaddr_type_is_le(sa->iso_bdaddr_type))
922 bacpy(&iso_pi(sk)->dst, &sa->iso_bdaddr);
923 iso_pi(sk)->dst_type = sa->iso_bdaddr_type;
927 if (bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
928 err = iso_connect_cis(sk);
930 err = iso_connect_bis(sk);
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));
946 static int iso_listen_bis(struct sock *sk)
948 struct hci_dev *hdev;
951 BT_DBG("%pMR -> %pMR (SID 0x%2.2x)", &iso_pi(sk)->src,
952 &iso_pi(sk)->dst, iso_pi(sk)->bc_sid);
954 write_lock(&iso_sk_list.lock);
956 if (__iso_get_sock_listen_by_sid(&iso_pi(sk)->src, &iso_pi(sk)->dst,
960 write_unlock(&iso_sk_list.lock);
965 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
966 iso_pi(sk)->src_type);
968 return -EHOSTUNREACH;
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;
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);
985 static int iso_listen_cis(struct sock *sk)
989 BT_DBG("%pMR", &iso_pi(sk)->src);
991 write_lock(&iso_sk_list.lock);
993 if (__iso_get_sock_listen_by_addr(&iso_pi(sk)->src, &iso_pi(sk)->dst))
996 write_unlock(&iso_sk_list.lock);
1001 static int iso_sock_listen(struct socket *sock, int backlog)
1003 struct sock *sk = sock->sk;
1006 BT_DBG("sk %p backlog %d", sk, backlog);
1010 if (sk->sk_state != BT_BOUND) {
1015 if (sk->sk_type != SOCK_SEQPACKET) {
1020 if (!bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
1021 err = iso_listen_cis(sk);
1023 err = iso_listen_bis(sk);
1028 sk->sk_max_ack_backlog = backlog;
1029 sk->sk_ack_backlog = 0;
1031 sk->sk_state = BT_LISTEN;
1038 static int iso_sock_accept(struct socket *sock, struct socket *newsock,
1039 int flags, bool kern)
1041 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1042 struct sock *sk = sock->sk, *ch;
1048 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1050 BT_DBG("sk %p timeo %ld", sk, timeo);
1052 /* Wait for an incoming connection. (wake-one). */
1053 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1055 if (sk->sk_state != BT_LISTEN) {
1060 ch = bt_accept_dequeue(sk, newsock);
1069 if (signal_pending(current)) {
1070 err = sock_intr_errno(timeo);
1076 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1079 remove_wait_queue(sk_sleep(sk), &wait);
1084 newsock->state = SS_CONNECTED;
1086 BT_DBG("new socket %p", ch);
1093 static int iso_sock_getname(struct socket *sock, struct sockaddr *addr,
1096 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
1097 struct sock *sk = sock->sk;
1099 BT_DBG("sock %p, sk %p", sock, sk);
1101 addr->sa_family = AF_BLUETOOTH;
1104 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->dst);
1105 sa->iso_bdaddr_type = iso_pi(sk)->dst_type;
1107 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->src);
1108 sa->iso_bdaddr_type = iso_pi(sk)->src_type;
1111 return sizeof(struct sockaddr_iso);
1114 static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1117 struct sock *sk = sock->sk;
1118 struct sk_buff *skb, **frag;
1122 BT_DBG("sock %p, sk %p", sock, sk);
1124 err = sock_error(sk);
1128 if (msg->msg_flags & MSG_OOB)
1133 if (sk->sk_state != BT_CONNECTED) {
1138 mtu = iso_pi(sk)->conn->hcon->hdev->iso_mtu;
1142 skb = bt_skb_sendmsg(sk, msg, len, mtu, HCI_ISO_DATA_HDR_SIZE, 0);
1144 return PTR_ERR(skb);
1148 BT_DBG("skb %p len %d", sk, skb->len);
1150 /* Continuation fragments */
1151 frag = &skb_shinfo(skb)->frag_list;
1153 struct sk_buff *tmp;
1155 tmp = bt_skb_sendmsg(sk, msg, len, mtu, 0, 0);
1158 return PTR_ERR(tmp);
1165 skb->len += tmp->len;
1166 skb->data_len += tmp->len;
1168 BT_DBG("frag %p len %d", *frag, tmp->len);
1170 frag = &(*frag)->next;
1175 if (sk->sk_state == BT_CONNECTED)
1176 err = iso_send_frame(sk, skb);
1187 static void iso_conn_defer_accept(struct hci_conn *conn)
1189 struct hci_cp_le_accept_cis cp;
1190 struct hci_dev *hdev = conn->hdev;
1192 BT_DBG("conn %p", conn);
1194 conn->state = BT_CONFIG;
1196 cp.handle = cpu_to_le16(conn->handle);
1198 hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp);
1201 static void iso_conn_big_sync(struct sock *sk)
1204 struct hci_dev *hdev;
1206 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
1207 iso_pi(sk)->src_type);
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,
1215 iso_pi(sk)->sync_handle,
1216 iso_pi(sk)->bc_num_bis,
1217 iso_pi(sk)->bc_bis);
1219 bt_dev_err(hdev, "hci_le_big_create_sync: %d",
1224 static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1225 size_t len, int flags)
1227 struct sock *sk = sock->sk;
1228 struct iso_pinfo *pi = iso_pi(sk);
1230 BT_DBG("sk %p", sk);
1232 if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
1234 switch (sk->sk_state) {
1236 if (pi->conn->hcon &&
1237 test_bit(HCI_CONN_PA_SYNC, &pi->conn->hcon->flags)) {
1238 iso_conn_big_sync(sk);
1239 sk->sk_state = BT_LISTEN;
1241 iso_conn_defer_accept(pi->conn->hcon);
1242 sk->sk_state = BT_CONFIG;
1248 return iso_connect_cis(sk);
1255 return bt_sock_recvmsg(sock, msg, len, flags);
1258 static bool check_io_qos(struct bt_iso_io_qos *qos)
1260 /* If no PHY is enable SDU must be 0 */
1261 if (!qos->phy && qos->sdu)
1264 if (qos->interval && (qos->interval < 0xff || qos->interval > 0xfffff))
1267 if (qos->latency && (qos->latency < 0x05 || qos->latency > 0xfa0))
1270 if (qos->phy > BT_ISO_PHY_ANY)
1276 static bool check_ucast_qos(struct bt_iso_qos *qos)
1278 if (qos->ucast.cig > 0xef && qos->ucast.cig != BT_ISO_QOS_CIG_UNSET)
1281 if (qos->ucast.cis > 0xef && qos->ucast.cis != BT_ISO_QOS_CIS_UNSET)
1284 if (qos->ucast.sca > 0x07)
1287 if (qos->ucast.packing > 0x01)
1290 if (qos->ucast.framing > 0x01)
1293 if (!check_io_qos(&qos->ucast.in))
1296 if (!check_io_qos(&qos->ucast.out))
1302 static bool check_bcast_qos(struct bt_iso_qos *qos)
1304 if (qos->bcast.sync_factor == 0x00)
1307 if (qos->bcast.packing > 0x01)
1310 if (qos->bcast.framing > 0x01)
1313 if (!check_io_qos(&qos->bcast.in))
1316 if (!check_io_qos(&qos->bcast.out))
1319 if (qos->bcast.encryption > 0x01)
1322 if (qos->bcast.options > 0x07)
1325 if (qos->bcast.skip > 0x01f3)
1328 if (qos->bcast.sync_timeout < 0x000a || qos->bcast.sync_timeout > 0x4000)
1331 if (qos->bcast.sync_cte_type > 0x1f)
1334 if (qos->bcast.mse > 0x1f)
1337 if (qos->bcast.timeout < 0x000a || qos->bcast.timeout > 0x4000)
1343 static int iso_sock_setsockopt(struct socket *sock, int level, int optname,
1344 sockptr_t optval, unsigned int optlen)
1346 struct sock *sk = sock->sk;
1348 struct bt_iso_qos qos = default_qos;
1351 BT_DBG("sk %p", sk);
1356 case BT_DEFER_SETUP:
1357 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1362 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
1368 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1370 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1374 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
1380 set_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags);
1382 clear_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags);
1386 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1387 sk->sk_state != BT_CONNECT2) {
1392 len = min_t(unsigned int, sizeof(qos), optlen);
1394 if (copy_from_sockptr(&qos, optval, len)) {
1399 if (len == sizeof(qos.ucast) && !check_ucast_qos(&qos)) {
1404 iso_pi(sk)->qos = qos;
1405 iso_pi(sk)->qos_user_set = true;
1410 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1411 sk->sk_state != BT_CONNECT2) {
1416 if (optlen > sizeof(iso_pi(sk)->base)) {
1421 len = min_t(unsigned int, sizeof(iso_pi(sk)->base), optlen);
1423 if (copy_from_sockptr(iso_pi(sk)->base, optval, len)) {
1428 iso_pi(sk)->base_len = len;
1441 static int iso_sock_getsockopt(struct socket *sock, int level, int optname,
1442 char __user *optval, int __user *optlen)
1444 struct sock *sk = sock->sk;
1446 struct bt_iso_qos *qos;
1450 BT_DBG("sk %p", sk);
1452 if (get_user(len, optlen))
1458 case BT_DEFER_SETUP:
1459 if (sk->sk_state == BT_CONNECTED) {
1464 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1465 (u32 __user *)optval))
1471 if (put_user(test_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags),
1472 (int __user *)optval))
1477 qos = iso_sock_get_qos(sk);
1479 len = min_t(unsigned int, len, sizeof(*qos));
1480 if (copy_to_user(optval, qos, len))
1486 if (sk->sk_state == BT_CONNECTED &&
1487 !bacmp(&iso_pi(sk)->dst, BDADDR_ANY)) {
1488 base_len = iso_pi(sk)->conn->hcon->le_per_adv_data_len;
1489 base = iso_pi(sk)->conn->hcon->le_per_adv_data;
1491 base_len = iso_pi(sk)->base_len;
1492 base = iso_pi(sk)->base;
1495 len = min_t(unsigned int, len, base_len);
1496 if (copy_to_user(optval, base, len))
1510 static int iso_sock_shutdown(struct socket *sock, int how)
1512 struct sock *sk = sock->sk;
1515 BT_DBG("sock %p, sk %p, how %d", sock, sk, how);
1525 if (sk->sk_shutdown & RCV_SHUTDOWN)
1527 sk->sk_shutdown |= RCV_SHUTDOWN;
1530 if (sk->sk_shutdown & SEND_SHUTDOWN)
1532 sk->sk_shutdown |= SEND_SHUTDOWN;
1535 if (sk->sk_shutdown & SHUTDOWN_MASK)
1537 sk->sk_shutdown |= SHUTDOWN_MASK;
1541 iso_sock_clear_timer(sk);
1542 __iso_sock_close(sk);
1544 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1545 !(current->flags & PF_EXITING))
1546 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1555 static int iso_sock_release(struct socket *sock)
1557 struct sock *sk = sock->sk;
1560 BT_DBG("sock %p, sk %p", sock, sk);
1567 if (sock_flag(sk, SOCK_LINGER) && READ_ONCE(sk->sk_lingertime) &&
1568 !(current->flags & PF_EXITING)) {
1570 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1579 static void iso_sock_ready(struct sock *sk)
1581 BT_DBG("sk %p", sk);
1587 iso_sock_clear_timer(sk);
1588 sk->sk_state = BT_CONNECTED;
1589 sk->sk_state_change(sk);
1593 struct iso_list_data {
1594 struct hci_conn *hcon;
1598 static bool iso_match_big(struct sock *sk, void *data)
1600 struct hci_evt_le_big_sync_estabilished *ev = data;
1602 return ev->handle == iso_pi(sk)->qos.bcast.big;
1605 static bool iso_match_pa_sync_flag(struct sock *sk, void *data)
1607 return test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags);
1610 static void iso_conn_ready(struct iso_conn *conn)
1612 struct sock *parent = NULL;
1613 struct sock *sk = conn->sk;
1614 struct hci_ev_le_big_sync_estabilished *ev = NULL;
1615 struct hci_ev_le_pa_sync_established *ev2 = NULL;
1616 struct hci_evt_le_big_info_adv_report *ev3 = NULL;
1617 struct hci_conn *hcon;
1619 BT_DBG("conn %p", conn);
1622 iso_sock_ready(conn->sk);
1628 if (test_bit(HCI_CONN_BIG_SYNC, &hcon->flags) ||
1629 test_bit(HCI_CONN_BIG_SYNC_FAILED, &hcon->flags)) {
1630 ev = hci_recv_event_data(hcon->hdev,
1631 HCI_EVT_LE_BIG_SYNC_ESTABILISHED);
1633 /* Get reference to PA sync parent socket, if it exists */
1634 parent = iso_get_sock_listen(&hcon->src,
1636 iso_match_pa_sync_flag, NULL);
1638 parent = iso_get_sock_listen(&hcon->src,
1641 } else if (test_bit(HCI_CONN_PA_SYNC_FAILED, &hcon->flags)) {
1642 ev2 = hci_recv_event_data(hcon->hdev,
1643 HCI_EV_LE_PA_SYNC_ESTABLISHED);
1645 parent = iso_get_sock_listen(&hcon->src,
1647 iso_match_sid, ev2);
1648 } else if (test_bit(HCI_CONN_PA_SYNC, &hcon->flags)) {
1649 ev3 = hci_recv_event_data(hcon->hdev,
1650 HCI_EVT_LE_BIG_INFO_ADV_REPORT);
1652 parent = iso_get_sock_listen(&hcon->src,
1654 iso_match_sync_handle, ev3);
1658 parent = iso_get_sock_listen(&hcon->src,
1659 BDADDR_ANY, NULL, NULL);
1666 sk = iso_sock_alloc(sock_net(parent), NULL,
1667 BTPROTO_ISO, GFP_ATOMIC, 0);
1669 release_sock(parent);
1673 iso_sock_init(sk, parent);
1675 bacpy(&iso_pi(sk)->src, &hcon->src);
1677 /* Convert from HCI to three-value type */
1678 if (hcon->src_type == ADDR_LE_DEV_PUBLIC)
1679 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC;
1681 iso_pi(sk)->src_type = BDADDR_LE_RANDOM;
1683 /* If hcon has no destination address (BDADDR_ANY) it means it
1684 * was created by HCI_EV_LE_BIG_SYNC_ESTABILISHED or
1685 * HCI_EV_LE_PA_SYNC_ESTABLISHED so we need to initialize using
1686 * the parent socket destination address.
1688 if (!bacmp(&hcon->dst, BDADDR_ANY)) {
1689 bacpy(&hcon->dst, &iso_pi(parent)->dst);
1690 hcon->dst_type = iso_pi(parent)->dst_type;
1691 hcon->sync_handle = iso_pi(parent)->sync_handle;
1695 iso_pi(sk)->qos = iso_pi(parent)->qos;
1696 iso_pi(sk)->qos.bcast.encryption = ev3->encryption;
1697 hcon->iso_qos = iso_pi(sk)->qos;
1698 iso_pi(sk)->bc_num_bis = iso_pi(parent)->bc_num_bis;
1699 memcpy(iso_pi(sk)->bc_bis, iso_pi(parent)->bc_bis, ISO_MAX_NUM_BIS);
1700 set_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags);
1703 bacpy(&iso_pi(sk)->dst, &hcon->dst);
1704 iso_pi(sk)->dst_type = hcon->dst_type;
1705 iso_pi(sk)->sync_handle = iso_pi(parent)->sync_handle;
1706 memcpy(iso_pi(sk)->base, iso_pi(parent)->base, iso_pi(parent)->base_len);
1707 iso_pi(sk)->base_len = iso_pi(parent)->base_len;
1709 hci_conn_hold(hcon);
1710 iso_chan_add(conn, sk, parent);
1712 if ((ev && ((struct hci_evt_le_big_sync_estabilished *)ev)->status) ||
1713 (ev2 && ev2->status)) {
1714 /* Trigger error signal on child socket */
1715 sk->sk_err = ECONNREFUSED;
1716 sk->sk_error_report(sk);
1719 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1720 sk->sk_state = BT_CONNECT2;
1722 sk->sk_state = BT_CONNECTED;
1724 /* Wake up parent */
1725 parent->sk_data_ready(parent);
1727 release_sock(parent);
1731 static bool iso_match_sid(struct sock *sk, void *data)
1733 struct hci_ev_le_pa_sync_established *ev = data;
1735 return ev->sid == iso_pi(sk)->bc_sid;
1738 static bool iso_match_sync_handle(struct sock *sk, void *data)
1740 struct hci_evt_le_big_info_adv_report *ev = data;
1742 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle;
1745 static bool iso_match_sync_handle_pa_report(struct sock *sk, void *data)
1747 struct hci_ev_le_per_adv_report *ev = data;
1749 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle;
1752 /* ----- ISO interface with lower layer (HCI) ----- */
1754 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1756 struct hci_ev_le_pa_sync_established *ev1;
1757 struct hci_evt_le_big_info_adv_report *ev2;
1758 struct hci_ev_le_per_adv_report *ev3;
1762 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
1764 /* Broadcast receiver requires handling of some events before it can
1765 * proceed to establishing a BIG sync:
1767 * 1. HCI_EV_LE_PA_SYNC_ESTABLISHED: The socket may specify a specific
1768 * SID to listen to and once sync is estabilished its handle needs to
1769 * be stored in iso_pi(sk)->sync_handle so it can be matched once
1770 * receiving the BIG Info.
1771 * 2. HCI_EVT_LE_BIG_INFO_ADV_REPORT: When connect_ind is triggered by a
1772 * a BIG Info it attempts to check if there any listening socket with
1773 * the same sync_handle and if it does then attempt to create a sync.
1774 * 3. HCI_EV_LE_PER_ADV_REPORT: When a PA report is received, it is stored
1775 * in iso_pi(sk)->base so it can be passed up to user, in the case of a
1778 ev1 = hci_recv_event_data(hdev, HCI_EV_LE_PA_SYNC_ESTABLISHED);
1780 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr, iso_match_sid,
1782 if (sk && !ev1->status)
1783 iso_pi(sk)->sync_handle = le16_to_cpu(ev1->handle);
1788 ev2 = hci_recv_event_data(hdev, HCI_EVT_LE_BIG_INFO_ADV_REPORT);
1790 /* Try to get PA sync listening socket, if it exists */
1791 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr,
1792 iso_match_pa_sync_flag, NULL);
1795 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr,
1796 iso_match_sync_handle, ev2);
1798 /* If PA Sync is in process of terminating,
1799 * do not handle any more BIGInfo adv reports.
1802 if (sk && test_bit(BT_SK_PA_SYNC_TERM,
1803 &iso_pi(sk)->flags))
1810 if (ev2->num_bis < iso_pi(sk)->bc_num_bis)
1811 iso_pi(sk)->bc_num_bis = ev2->num_bis;
1813 if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags) &&
1814 !test_and_set_bit(BT_SK_BIG_SYNC, &iso_pi(sk)->flags)) {
1815 err = hci_le_big_create_sync(hdev, NULL,
1817 iso_pi(sk)->sync_handle,
1818 iso_pi(sk)->bc_num_bis,
1819 iso_pi(sk)->bc_bis);
1821 bt_dev_err(hdev, "hci_le_big_create_sync: %d",
1829 ev3 = hci_recv_event_data(hdev, HCI_EV_LE_PER_ADV_REPORT);
1831 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr,
1832 iso_match_sync_handle_pa_report, ev3);
1835 memcpy(iso_pi(sk)->base, ev3->data, ev3->length);
1836 iso_pi(sk)->base_len = ev3->length;
1839 sk = iso_get_sock_listen(&hdev->bdaddr, BDADDR_ANY, NULL, NULL);
1846 lm |= HCI_LM_ACCEPT;
1848 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1849 *flags |= HCI_PROTO_DEFER;
1854 static void iso_connect_cfm(struct hci_conn *hcon, __u8 status)
1856 if (hcon->type != ISO_LINK) {
1857 if (hcon->type != LE_LINK)
1860 /* Check if LE link has failed */
1862 struct hci_link *link, *t;
1864 list_for_each_entry_safe(link, t, &hcon->link_list,
1866 iso_conn_del(link->conn, bt_to_errno(status));
1871 /* Create CIS if pending */
1872 hci_le_create_cis_pending(hcon->hdev);
1876 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
1878 /* Similar to the success case, if HCI_CONN_BIG_SYNC_FAILED or
1879 * HCI_CONN_PA_SYNC_FAILED is set, queue the failed connection
1880 * into the accept queue of the listening socket and wake up
1881 * userspace, to inform the user about the event.
1883 if (!status || test_bit(HCI_CONN_BIG_SYNC_FAILED, &hcon->flags) ||
1884 test_bit(HCI_CONN_PA_SYNC_FAILED, &hcon->flags)) {
1885 struct iso_conn *conn;
1887 conn = iso_conn_add(hcon);
1889 iso_conn_ready(conn);
1891 iso_conn_del(hcon, bt_to_errno(status));
1895 static void iso_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1897 if (hcon->type != ISO_LINK)
1900 BT_DBG("hcon %p reason %d", hcon, reason);
1902 iso_conn_del(hcon, bt_to_errno(reason));
1905 void iso_recv(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
1907 struct iso_conn *conn = hcon->iso_data;
1913 pb = hci_iso_flags_pb(flags);
1914 ts = hci_iso_flags_ts(flags);
1916 BT_DBG("conn %p len %d pb 0x%x ts 0x%x", conn, skb->len, pb, ts);
1922 BT_ERR("Unexpected start frame (len %d)", skb->len);
1923 kfree_skb(conn->rx_skb);
1924 conn->rx_skb = NULL;
1929 struct hci_iso_ts_data_hdr *hdr;
1931 /* TODO: add timestamp to the packet? */
1932 hdr = skb_pull_data(skb, HCI_ISO_TS_DATA_HDR_SIZE);
1934 BT_ERR("Frame is too short (len %d)", skb->len);
1938 len = __le16_to_cpu(hdr->slen);
1940 struct hci_iso_data_hdr *hdr;
1942 hdr = skb_pull_data(skb, HCI_ISO_DATA_HDR_SIZE);
1944 BT_ERR("Frame is too short (len %d)", skb->len);
1948 len = __le16_to_cpu(hdr->slen);
1951 flags = hci_iso_data_flags(len);
1952 len = hci_iso_data_len(len);
1954 BT_DBG("Start: total len %d, frag len %d flags 0x%4.4x", len,
1957 if (len == skb->len) {
1958 /* Complete frame received */
1959 hci_skb_pkt_status(skb) = flags & 0x03;
1960 iso_recv_frame(conn, skb);
1964 if (pb == ISO_SINGLE) {
1965 BT_ERR("Frame malformed (len %d, expected len %d)",
1970 if (skb->len > len) {
1971 BT_ERR("Frame is too long (len %d, expected len %d)",
1976 /* Allocate skb for the complete frame (with header) */
1977 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
1981 hci_skb_pkt_status(conn->rx_skb) = flags & 0x03;
1982 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1984 conn->rx_len = len - skb->len;
1988 BT_DBG("Cont: frag len %d (expecting %d)", skb->len,
1991 if (!conn->rx_len) {
1992 BT_ERR("Unexpected continuation frame (len %d)",
1997 if (skb->len > conn->rx_len) {
1998 BT_ERR("Fragment is too long (len %d, expected %d)",
1999 skb->len, conn->rx_len);
2000 kfree_skb(conn->rx_skb);
2001 conn->rx_skb = NULL;
2006 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
2008 conn->rx_len -= skb->len;
2012 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
2014 conn->rx_len -= skb->len;
2016 if (!conn->rx_len) {
2017 struct sk_buff *rx_skb = conn->rx_skb;
2019 /* Complete frame received. iso_recv_frame
2020 * takes ownership of the skb so set the global
2021 * rx_skb pointer to NULL first.
2023 conn->rx_skb = NULL;
2024 iso_recv_frame(conn, rx_skb);
2033 static struct hci_cb iso_cb = {
2035 .connect_cfm = iso_connect_cfm,
2036 .disconn_cfm = iso_disconn_cfm,
2039 static int iso_debugfs_show(struct seq_file *f, void *p)
2043 read_lock(&iso_sk_list.lock);
2045 sk_for_each(sk, &iso_sk_list.head) {
2046 seq_printf(f, "%pMR %pMR %d\n", &iso_pi(sk)->src,
2047 &iso_pi(sk)->dst, sk->sk_state);
2050 read_unlock(&iso_sk_list.lock);
2055 DEFINE_SHOW_ATTRIBUTE(iso_debugfs);
2057 static struct dentry *iso_debugfs;
2059 static const struct proto_ops iso_sock_ops = {
2060 .family = PF_BLUETOOTH,
2061 .owner = THIS_MODULE,
2062 .release = iso_sock_release,
2063 .bind = iso_sock_bind,
2064 .connect = iso_sock_connect,
2065 .listen = iso_sock_listen,
2066 .accept = iso_sock_accept,
2067 .getname = iso_sock_getname,
2068 .sendmsg = iso_sock_sendmsg,
2069 .recvmsg = iso_sock_recvmsg,
2070 .poll = bt_sock_poll,
2071 .ioctl = bt_sock_ioctl,
2072 .mmap = sock_no_mmap,
2073 .socketpair = sock_no_socketpair,
2074 .shutdown = iso_sock_shutdown,
2075 .setsockopt = iso_sock_setsockopt,
2076 .getsockopt = iso_sock_getsockopt
2079 static const struct net_proto_family iso_sock_family_ops = {
2080 .family = PF_BLUETOOTH,
2081 .owner = THIS_MODULE,
2082 .create = iso_sock_create,
2085 static bool iso_inited;
2087 bool iso_enabled(void)
2096 BUILD_BUG_ON(sizeof(struct sockaddr_iso) > sizeof(struct sockaddr));
2101 err = proto_register(&iso_proto, 0);
2105 err = bt_sock_register(BTPROTO_ISO, &iso_sock_family_ops);
2107 BT_ERR("ISO socket registration failed");
2111 err = bt_procfs_init(&init_net, "iso", &iso_sk_list, NULL);
2113 BT_ERR("Failed to create ISO proc file");
2114 bt_sock_unregister(BTPROTO_ISO);
2118 BT_INFO("ISO socket layer initialized");
2120 hci_register_cb(&iso_cb);
2122 if (IS_ERR_OR_NULL(bt_debugfs))
2126 iso_debugfs = debugfs_create_file("iso", 0444, bt_debugfs,
2127 NULL, &iso_debugfs_fops);
2135 proto_unregister(&iso_proto);
2144 bt_procfs_cleanup(&init_net, "iso");
2146 debugfs_remove(iso_debugfs);
2149 hci_unregister_cb(&iso_cb);
2151 bt_sock_unregister(BTPROTO_ISO);
2153 proto_unregister(&iso_proto);