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)
59 __u8 bc_bis[ISO_MAX_NUM_BIS];
62 struct bt_iso_qos qos;
65 __u8 base[BASE_MAX_LENGTH];
66 struct iso_conn *conn;
69 static struct bt_iso_qos default_qos;
71 static bool check_ucast_qos(struct bt_iso_qos *qos);
72 static bool check_bcast_qos(struct bt_iso_qos *qos);
74 /* ---- ISO timers ---- */
75 #define ISO_CONN_TIMEOUT (HZ * 40)
76 #define ISO_DISCONN_TIMEOUT (HZ * 2)
78 static void iso_sock_timeout(struct work_struct *work)
80 struct iso_conn *conn = container_of(work, struct iso_conn,
88 iso_conn_unlock(conn);
93 BT_DBG("sock %p state %d", sk, sk->sk_state);
96 sk->sk_err = ETIMEDOUT;
97 sk->sk_state_change(sk);
102 static void iso_sock_set_timer(struct sock *sk, long timeout)
104 if (!iso_pi(sk)->conn)
107 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
108 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
109 schedule_delayed_work(&iso_pi(sk)->conn->timeout_work, timeout);
112 static void iso_sock_clear_timer(struct sock *sk)
114 if (!iso_pi(sk)->conn)
117 BT_DBG("sock %p state %d", sk, sk->sk_state);
118 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
121 /* ---- ISO connections ---- */
122 static struct iso_conn *iso_conn_add(struct hci_conn *hcon)
124 struct iso_conn *conn = hcon->iso_data;
129 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
133 spin_lock_init(&conn->lock);
134 INIT_DELAYED_WORK(&conn->timeout_work, iso_sock_timeout);
136 hcon->iso_data = conn;
140 BT_DBG("hcon %p conn %p", hcon, conn);
145 /* Delete channel. Must be called on the locked socket. */
146 static void iso_chan_del(struct sock *sk, int err)
148 struct iso_conn *conn;
151 conn = iso_pi(sk)->conn;
153 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
158 iso_pi(sk)->conn = NULL;
159 iso_conn_unlock(conn);
162 hci_conn_drop(conn->hcon);
165 sk->sk_state = BT_CLOSED;
168 parent = bt_sk(sk)->parent;
170 bt_accept_unlink(sk);
171 parent->sk_data_ready(parent);
173 sk->sk_state_change(sk);
176 sock_set_flag(sk, SOCK_ZAPPED);
179 static void iso_conn_del(struct hci_conn *hcon, int err)
181 struct iso_conn *conn = hcon->iso_data;
187 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
194 iso_conn_unlock(conn);
198 iso_sock_clear_timer(sk);
199 iso_chan_del(sk, err);
204 /* Ensure no more work items will run before freeing conn. */
205 cancel_delayed_work_sync(&conn->timeout_work);
207 hcon->iso_data = NULL;
211 static int __iso_chan_add(struct iso_conn *conn, struct sock *sk,
214 BT_DBG("conn %p", conn);
216 if (iso_pi(sk)->conn == conn && conn->sk == sk)
220 BT_ERR("conn->sk already set");
224 iso_pi(sk)->conn = conn;
228 bt_accept_enqueue(parent, sk, true);
233 static int iso_chan_add(struct iso_conn *conn, struct sock *sk,
239 err = __iso_chan_add(conn, sk, parent);
240 iso_conn_unlock(conn);
245 static inline u8 le_addr_type(u8 bdaddr_type)
247 if (bdaddr_type == BDADDR_LE_PUBLIC)
248 return ADDR_LE_DEV_PUBLIC;
250 return ADDR_LE_DEV_RANDOM;
253 static int iso_connect_bis(struct sock *sk)
255 struct iso_conn *conn;
256 struct hci_conn *hcon;
257 struct hci_dev *hdev;
260 BT_DBG("%pMR", &iso_pi(sk)->src);
262 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
263 iso_pi(sk)->src_type);
265 return -EHOSTUNREACH;
269 if (!bis_capable(hdev)) {
274 /* Fail if user set invalid QoS */
275 if (iso_pi(sk)->qos_user_set && !check_bcast_qos(&iso_pi(sk)->qos)) {
276 iso_pi(sk)->qos = default_qos;
281 /* Fail if out PHYs are marked as disabled */
282 if (!iso_pi(sk)->qos.bcast.out.phy) {
287 hcon = hci_connect_bis(hdev, &iso_pi(sk)->dst,
288 le_addr_type(iso_pi(sk)->dst_type),
289 &iso_pi(sk)->qos, iso_pi(sk)->base_len,
296 conn = iso_conn_add(hcon);
303 hci_dev_unlock(hdev);
306 err = iso_chan_add(conn, sk, NULL);
312 /* Update source addr of the socket */
313 bacpy(&iso_pi(sk)->src, &hcon->src);
315 if (hcon->state == BT_CONNECTED) {
316 iso_sock_clear_timer(sk);
317 sk->sk_state = BT_CONNECTED;
319 sk->sk_state = BT_CONNECT;
320 iso_sock_set_timer(sk, sk->sk_sndtimeo);
327 hci_dev_unlock(hdev);
332 static int iso_connect_cis(struct sock *sk)
334 struct iso_conn *conn;
335 struct hci_conn *hcon;
336 struct hci_dev *hdev;
339 BT_DBG("%pMR -> %pMR", &iso_pi(sk)->src, &iso_pi(sk)->dst);
341 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
342 iso_pi(sk)->src_type);
344 return -EHOSTUNREACH;
348 if (!cis_central_capable(hdev)) {
353 /* Fail if user set invalid QoS */
354 if (iso_pi(sk)->qos_user_set && !check_ucast_qos(&iso_pi(sk)->qos)) {
355 iso_pi(sk)->qos = default_qos;
360 /* Fail if either PHYs are marked as disabled */
361 if (!iso_pi(sk)->qos.ucast.in.phy && !iso_pi(sk)->qos.ucast.out.phy) {
366 /* Just bind if DEFER_SETUP has been set */
367 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
368 hcon = hci_bind_cis(hdev, &iso_pi(sk)->dst,
369 le_addr_type(iso_pi(sk)->dst_type),
376 hcon = hci_connect_cis(hdev, &iso_pi(sk)->dst,
377 le_addr_type(iso_pi(sk)->dst_type),
385 conn = iso_conn_add(hcon);
392 hci_dev_unlock(hdev);
395 err = iso_chan_add(conn, sk, NULL);
401 /* Update source addr of the socket */
402 bacpy(&iso_pi(sk)->src, &hcon->src);
404 if (hcon->state == BT_CONNECTED) {
405 iso_sock_clear_timer(sk);
406 sk->sk_state = BT_CONNECTED;
407 } else if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
408 iso_sock_clear_timer(sk);
409 sk->sk_state = BT_CONNECT;
411 sk->sk_state = BT_CONNECT;
412 iso_sock_set_timer(sk, sk->sk_sndtimeo);
419 hci_dev_unlock(hdev);
424 static struct bt_iso_qos *iso_sock_get_qos(struct sock *sk)
426 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONNECT2)
427 return &iso_pi(sk)->conn->hcon->iso_qos;
429 return &iso_pi(sk)->qos;
432 static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
434 struct iso_conn *conn = iso_pi(sk)->conn;
435 struct bt_iso_qos *qos = iso_sock_get_qos(sk);
436 struct hci_iso_data_hdr *hdr;
439 BT_DBG("sk %p len %d", sk, skb->len);
441 if (skb->len > qos->ucast.out.sdu)
446 /* Push ISO data header */
447 hdr = skb_push(skb, HCI_ISO_DATA_HDR_SIZE);
448 hdr->sn = cpu_to_le16(conn->tx_sn++);
449 hdr->slen = cpu_to_le16(hci_iso_data_len_pack(len,
450 HCI_ISO_STATUS_VALID));
452 if (sk->sk_state == BT_CONNECTED)
453 hci_send_iso(conn->hcon, skb);
460 static void iso_recv_frame(struct iso_conn *conn, struct sk_buff *skb)
466 iso_conn_unlock(conn);
471 BT_DBG("sk %p len %d", sk, skb->len);
473 if (sk->sk_state != BT_CONNECTED)
476 if (!sock_queue_rcv_skb(sk, skb))
483 /* -------- Socket interface ---------- */
484 static struct sock *__iso_get_sock_listen_by_addr(bdaddr_t *ba)
488 sk_for_each(sk, &iso_sk_list.head) {
489 if (sk->sk_state != BT_LISTEN)
492 if (!bacmp(&iso_pi(sk)->src, ba))
499 static struct sock *__iso_get_sock_listen_by_sid(bdaddr_t *ba, bdaddr_t *bc,
504 sk_for_each(sk, &iso_sk_list.head) {
505 if (sk->sk_state != BT_LISTEN)
508 if (bacmp(&iso_pi(sk)->src, ba))
511 if (bacmp(&iso_pi(sk)->dst, bc))
514 if (iso_pi(sk)->bc_sid == sid)
521 typedef bool (*iso_sock_match_t)(struct sock *sk, void *data);
523 /* Find socket listening:
524 * source bdaddr (Unicast)
525 * destination bdaddr (Broadcast only)
526 * match func - pass NULL to ignore
527 * match func data - pass -1 to ignore
528 * Returns closest match.
530 static struct sock *iso_get_sock_listen(bdaddr_t *src, bdaddr_t *dst,
531 iso_sock_match_t match, void *data)
533 struct sock *sk = NULL, *sk1 = NULL;
535 read_lock(&iso_sk_list.lock);
537 sk_for_each(sk, &iso_sk_list.head) {
538 if (sk->sk_state != BT_LISTEN)
541 /* Match Broadcast destination */
542 if (bacmp(dst, BDADDR_ANY) && bacmp(&iso_pi(sk)->dst, dst))
545 /* Use Match function if provided */
546 if (match && !match(sk, data))
550 if (!bacmp(&iso_pi(sk)->src, src))
554 if (!bacmp(&iso_pi(sk)->src, BDADDR_ANY))
558 read_unlock(&iso_sk_list.lock);
560 return sk ? sk : sk1;
563 static void iso_sock_destruct(struct sock *sk)
567 skb_queue_purge(&sk->sk_receive_queue);
568 skb_queue_purge(&sk->sk_write_queue);
571 static void iso_sock_cleanup_listen(struct sock *parent)
575 BT_DBG("parent %p", parent);
577 /* Close not yet accepted channels */
578 while ((sk = bt_accept_dequeue(parent, NULL))) {
583 parent->sk_state = BT_CLOSED;
584 sock_set_flag(parent, SOCK_ZAPPED);
587 /* Kill socket (only if zapped and orphan)
588 * Must be called on unlocked socket.
590 static void iso_sock_kill(struct sock *sk)
592 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||
593 sock_flag(sk, SOCK_DEAD))
596 BT_DBG("sk %p state %d", sk, sk->sk_state);
598 /* Kill poor orphan */
599 bt_sock_unlink(&iso_sk_list, sk);
600 sock_set_flag(sk, SOCK_DEAD);
604 static void iso_conn_defer_reject(struct hci_conn *conn)
606 struct hci_cp_le_reject_cis cp;
608 BT_DBG("conn %p", conn);
610 memset(&cp, 0, sizeof(cp));
611 cp.handle = cpu_to_le16(conn->handle);
612 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
613 hci_send_cmd(conn->hdev, HCI_OP_LE_REJECT_CIS, sizeof(cp), &cp);
616 static void __iso_sock_close(struct sock *sk)
618 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
620 switch (sk->sk_state) {
622 iso_sock_cleanup_listen(sk);
627 if (iso_pi(sk)->conn->hcon) {
628 sk->sk_state = BT_DISCONN;
629 iso_sock_set_timer(sk, ISO_DISCONN_TIMEOUT);
630 iso_conn_lock(iso_pi(sk)->conn);
631 hci_conn_drop(iso_pi(sk)->conn->hcon);
632 iso_pi(sk)->conn->hcon = NULL;
633 iso_conn_unlock(iso_pi(sk)->conn);
635 iso_chan_del(sk, ECONNRESET);
640 if (iso_pi(sk)->conn->hcon)
641 iso_conn_defer_reject(iso_pi(sk)->conn->hcon);
642 iso_chan_del(sk, ECONNRESET);
645 /* In case of DEFER_SETUP the hcon would be bound to CIG which
646 * needs to be removed so just call hci_conn_del so the cleanup
647 * callback do what is needed.
649 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags) &&
650 iso_pi(sk)->conn->hcon) {
651 hci_conn_del(iso_pi(sk)->conn->hcon);
652 iso_pi(sk)->conn->hcon = NULL;
655 iso_chan_del(sk, ECONNRESET);
658 iso_chan_del(sk, ECONNRESET);
662 sock_set_flag(sk, SOCK_ZAPPED);
667 /* Must be called on unlocked socket. */
668 static void iso_sock_close(struct sock *sk)
670 iso_sock_clear_timer(sk);
672 __iso_sock_close(sk);
677 static void iso_sock_init(struct sock *sk, struct sock *parent)
682 sk->sk_type = parent->sk_type;
683 bt_sk(sk)->flags = bt_sk(parent)->flags;
684 security_sk_clone(parent, sk);
688 static struct proto iso_proto = {
690 .owner = THIS_MODULE,
691 .obj_size = sizeof(struct iso_pinfo)
694 #define DEFAULT_IO_QOS \
696 .interval = 10000u, \
699 .phy = BT_ISO_PHY_2M, \
703 static struct bt_iso_qos default_qos = {
705 .big = BT_ISO_QOS_BIG_UNSET,
706 .bis = BT_ISO_QOS_BIS_UNSET,
707 .sync_interval = 0x00,
710 .in = DEFAULT_IO_QOS,
711 .out = DEFAULT_IO_QOS,
716 .sync_timeout = 0x4000,
717 .sync_cte_type = 0x00,
723 static struct sock *iso_sock_alloc(struct net *net, struct socket *sock,
724 int proto, gfp_t prio, int kern)
728 sk = sk_alloc(net, PF_BLUETOOTH, prio, &iso_proto, kern);
732 sock_init_data(sock, sk);
733 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
735 sk->sk_destruct = iso_sock_destruct;
736 sk->sk_sndtimeo = ISO_CONN_TIMEOUT;
738 sock_reset_flag(sk, SOCK_ZAPPED);
740 sk->sk_protocol = proto;
741 sk->sk_state = BT_OPEN;
743 /* Set address type as public as default src address is BDADDR_ANY */
744 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC;
746 iso_pi(sk)->qos = default_qos;
748 bt_sock_link(&iso_sk_list, sk);
752 static int iso_sock_create(struct net *net, struct socket *sock, int protocol,
757 BT_DBG("sock %p", sock);
759 sock->state = SS_UNCONNECTED;
761 if (sock->type != SOCK_SEQPACKET)
762 return -ESOCKTNOSUPPORT;
764 sock->ops = &iso_sock_ops;
766 sk = iso_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
770 iso_sock_init(sk, NULL);
774 static int iso_sock_bind_bc(struct socket *sock, struct sockaddr *addr,
777 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
778 struct sock *sk = sock->sk;
781 BT_DBG("sk %p bc_sid %u bc_num_bis %u", sk, sa->iso_bc->bc_sid,
782 sa->iso_bc->bc_num_bis);
784 if (addr_len > sizeof(*sa) + sizeof(*sa->iso_bc) ||
785 sa->iso_bc->bc_num_bis < 0x01 || sa->iso_bc->bc_num_bis > 0x1f)
788 bacpy(&iso_pi(sk)->dst, &sa->iso_bc->bc_bdaddr);
789 iso_pi(sk)->dst_type = sa->iso_bc->bc_bdaddr_type;
790 iso_pi(sk)->sync_handle = -1;
791 iso_pi(sk)->bc_sid = sa->iso_bc->bc_sid;
792 iso_pi(sk)->bc_num_bis = sa->iso_bc->bc_num_bis;
794 for (i = 0; i < iso_pi(sk)->bc_num_bis; i++) {
795 if (sa->iso_bc->bc_bis[i] < 0x01 ||
796 sa->iso_bc->bc_bis[i] > 0x1f)
799 memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis,
800 iso_pi(sk)->bc_num_bis);
806 static int iso_sock_bind(struct socket *sock, struct sockaddr *addr,
809 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
810 struct sock *sk = sock->sk;
813 BT_DBG("sk %p %pMR type %u", sk, &sa->iso_bdaddr, sa->iso_bdaddr_type);
815 if (!addr || addr_len < sizeof(struct sockaddr_iso) ||
816 addr->sa_family != AF_BLUETOOTH)
821 if (sk->sk_state != BT_OPEN) {
826 if (sk->sk_type != SOCK_SEQPACKET) {
831 /* Check if the address type is of LE type */
832 if (!bdaddr_type_is_le(sa->iso_bdaddr_type)) {
837 bacpy(&iso_pi(sk)->src, &sa->iso_bdaddr);
838 iso_pi(sk)->src_type = sa->iso_bdaddr_type;
840 /* Check for Broadcast address */
841 if (addr_len > sizeof(*sa)) {
842 err = iso_sock_bind_bc(sock, addr, addr_len);
847 sk->sk_state = BT_BOUND;
854 static int iso_sock_connect(struct socket *sock, struct sockaddr *addr,
857 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
858 struct sock *sk = sock->sk;
863 if (alen < sizeof(struct sockaddr_iso) ||
864 addr->sa_family != AF_BLUETOOTH)
867 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
870 if (sk->sk_type != SOCK_SEQPACKET)
873 /* Check if the address type is of LE type */
874 if (!bdaddr_type_is_le(sa->iso_bdaddr_type))
879 bacpy(&iso_pi(sk)->dst, &sa->iso_bdaddr);
880 iso_pi(sk)->dst_type = sa->iso_bdaddr_type;
884 if (bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
885 err = iso_connect_cis(sk);
887 err = iso_connect_bis(sk);
894 if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
895 err = bt_sock_wait_state(sk, BT_CONNECTED,
896 sock_sndtimeo(sk, flags & O_NONBLOCK));
903 static int iso_listen_bis(struct sock *sk)
905 struct hci_dev *hdev;
908 BT_DBG("%pMR -> %pMR (SID 0x%2.2x)", &iso_pi(sk)->src,
909 &iso_pi(sk)->dst, iso_pi(sk)->bc_sid);
911 write_lock(&iso_sk_list.lock);
913 if (__iso_get_sock_listen_by_sid(&iso_pi(sk)->src, &iso_pi(sk)->dst,
917 write_unlock(&iso_sk_list.lock);
922 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
923 iso_pi(sk)->src_type);
925 return -EHOSTUNREACH;
927 /* Fail if user set invalid QoS */
928 if (iso_pi(sk)->qos_user_set && !check_bcast_qos(&iso_pi(sk)->qos)) {
929 iso_pi(sk)->qos = default_qos;
933 err = hci_pa_create_sync(hdev, &iso_pi(sk)->dst,
934 le_addr_type(iso_pi(sk)->dst_type),
935 iso_pi(sk)->bc_sid, &iso_pi(sk)->qos);
942 static int iso_listen_cis(struct sock *sk)
946 BT_DBG("%pMR", &iso_pi(sk)->src);
948 write_lock(&iso_sk_list.lock);
950 if (__iso_get_sock_listen_by_addr(&iso_pi(sk)->src))
953 write_unlock(&iso_sk_list.lock);
958 static int iso_sock_listen(struct socket *sock, int backlog)
960 struct sock *sk = sock->sk;
963 BT_DBG("sk %p backlog %d", sk, backlog);
967 if (sk->sk_state != BT_BOUND) {
972 if (sk->sk_type != SOCK_SEQPACKET) {
977 if (!bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
978 err = iso_listen_cis(sk);
980 err = iso_listen_bis(sk);
985 sk->sk_max_ack_backlog = backlog;
986 sk->sk_ack_backlog = 0;
988 sk->sk_state = BT_LISTEN;
995 static int iso_sock_accept(struct socket *sock, struct socket *newsock,
996 int flags, bool kern)
998 DEFINE_WAIT_FUNC(wait, woken_wake_function);
999 struct sock *sk = sock->sk, *ch;
1005 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1007 BT_DBG("sk %p timeo %ld", sk, timeo);
1009 /* Wait for an incoming connection. (wake-one). */
1010 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1012 if (sk->sk_state != BT_LISTEN) {
1017 ch = bt_accept_dequeue(sk, newsock);
1026 if (signal_pending(current)) {
1027 err = sock_intr_errno(timeo);
1033 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1036 remove_wait_queue(sk_sleep(sk), &wait);
1041 newsock->state = SS_CONNECTED;
1043 BT_DBG("new socket %p", ch);
1050 static int iso_sock_getname(struct socket *sock, struct sockaddr *addr,
1053 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
1054 struct sock *sk = sock->sk;
1056 BT_DBG("sock %p, sk %p", sock, sk);
1058 addr->sa_family = AF_BLUETOOTH;
1061 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->dst);
1062 sa->iso_bdaddr_type = iso_pi(sk)->dst_type;
1064 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->src);
1065 sa->iso_bdaddr_type = iso_pi(sk)->src_type;
1068 return sizeof(struct sockaddr_iso);
1071 static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1074 struct sock *sk = sock->sk;
1075 struct iso_conn *conn = iso_pi(sk)->conn;
1076 struct sk_buff *skb, **frag;
1079 BT_DBG("sock %p, sk %p", sock, sk);
1081 err = sock_error(sk);
1085 if (msg->msg_flags & MSG_OOB)
1088 if (sk->sk_state != BT_CONNECTED)
1091 skb = bt_skb_sendmsg(sk, msg, len, conn->hcon->hdev->iso_mtu,
1092 HCI_ISO_DATA_HDR_SIZE, 0);
1094 return PTR_ERR(skb);
1098 BT_DBG("skb %p len %d", sk, skb->len);
1100 /* Continuation fragments */
1101 frag = &skb_shinfo(skb)->frag_list;
1103 struct sk_buff *tmp;
1105 tmp = bt_skb_sendmsg(sk, msg, len, conn->hcon->hdev->iso_mtu,
1109 return PTR_ERR(tmp);
1116 skb->len += tmp->len;
1117 skb->data_len += tmp->len;
1119 BT_DBG("frag %p len %d", *frag, tmp->len);
1121 frag = &(*frag)->next;
1126 if (sk->sk_state == BT_CONNECTED)
1127 err = iso_send_frame(sk, skb);
1138 static void iso_conn_defer_accept(struct hci_conn *conn)
1140 struct hci_cp_le_accept_cis cp;
1141 struct hci_dev *hdev = conn->hdev;
1143 BT_DBG("conn %p", conn);
1145 conn->state = BT_CONFIG;
1147 cp.handle = cpu_to_le16(conn->handle);
1149 hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp);
1152 static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1153 size_t len, int flags)
1155 struct sock *sk = sock->sk;
1156 struct iso_pinfo *pi = iso_pi(sk);
1158 BT_DBG("sk %p", sk);
1160 if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
1161 switch (sk->sk_state) {
1164 iso_conn_defer_accept(pi->conn->hcon);
1165 sk->sk_state = BT_CONFIG;
1169 return iso_connect_cis(sk);
1173 return bt_sock_recvmsg(sock, msg, len, flags);
1176 static bool check_io_qos(struct bt_iso_io_qos *qos)
1178 /* If no PHY is enable SDU must be 0 */
1179 if (!qos->phy && qos->sdu)
1182 if (qos->interval && (qos->interval < 0xff || qos->interval > 0xfffff))
1185 if (qos->latency && (qos->latency < 0x05 || qos->latency > 0xfa0))
1188 if (qos->phy > BT_ISO_PHY_ANY)
1194 static bool check_ucast_qos(struct bt_iso_qos *qos)
1196 if (qos->ucast.sca > 0x07)
1199 if (qos->ucast.packing > 0x01)
1202 if (qos->ucast.framing > 0x01)
1205 if (!check_io_qos(&qos->ucast.in))
1208 if (!check_io_qos(&qos->ucast.out))
1214 static bool check_bcast_qos(struct bt_iso_qos *qos)
1216 if (qos->bcast.sync_interval > 0x07)
1219 if (qos->bcast.packing > 0x01)
1222 if (qos->bcast.framing > 0x01)
1225 if (!check_io_qos(&qos->bcast.in))
1228 if (!check_io_qos(&qos->bcast.out))
1231 if (qos->bcast.encryption > 0x01)
1234 if (qos->bcast.options > 0x07)
1237 if (qos->bcast.skip > 0x01f3)
1240 if (qos->bcast.sync_timeout < 0x000a || qos->bcast.sync_timeout > 0x4000)
1243 if (qos->bcast.sync_cte_type > 0x1f)
1246 if (qos->bcast.mse > 0x1f)
1249 if (qos->bcast.timeout < 0x000a || qos->bcast.timeout > 0x4000)
1255 static int iso_sock_setsockopt(struct socket *sock, int level, int optname,
1256 sockptr_t optval, unsigned int optlen)
1258 struct sock *sk = sock->sk;
1260 struct bt_iso_qos qos = default_qos;
1263 BT_DBG("sk %p", sk);
1268 case BT_DEFER_SETUP:
1269 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1274 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
1280 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1282 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1286 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1287 sk->sk_state != BT_CONNECT2) {
1292 len = min_t(unsigned int, sizeof(qos), optlen);
1294 if (copy_from_sockptr(&qos, optval, len)) {
1299 if (len == sizeof(qos.ucast) && !check_ucast_qos(&qos)) {
1304 iso_pi(sk)->qos = qos;
1305 iso_pi(sk)->qos_user_set = true;
1310 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1311 sk->sk_state != BT_CONNECT2) {
1316 if (optlen > sizeof(iso_pi(sk)->base)) {
1321 len = min_t(unsigned int, sizeof(iso_pi(sk)->base), optlen);
1323 if (copy_from_sockptr(iso_pi(sk)->base, optval, len)) {
1328 iso_pi(sk)->base_len = len;
1341 static int iso_sock_getsockopt(struct socket *sock, int level, int optname,
1342 char __user *optval, int __user *optlen)
1344 struct sock *sk = sock->sk;
1346 struct bt_iso_qos *qos;
1350 BT_DBG("sk %p", sk);
1352 if (get_user(len, optlen))
1358 case BT_DEFER_SETUP:
1359 if (sk->sk_state == BT_CONNECTED) {
1364 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1365 (u32 __user *)optval))
1371 qos = iso_sock_get_qos(sk);
1373 len = min_t(unsigned int, len, sizeof(*qos));
1374 if (copy_to_user(optval, qos, len))
1380 if (sk->sk_state == BT_CONNECTED) {
1381 base_len = iso_pi(sk)->conn->hcon->le_per_adv_data_len;
1382 base = iso_pi(sk)->conn->hcon->le_per_adv_data;
1384 base_len = iso_pi(sk)->base_len;
1385 base = iso_pi(sk)->base;
1388 len = min_t(unsigned int, len, base_len);
1389 if (copy_to_user(optval, base, len))
1403 static int iso_sock_shutdown(struct socket *sock, int how)
1405 struct sock *sk = sock->sk;
1408 BT_DBG("sock %p, sk %p, how %d", sock, sk, how);
1418 if (sk->sk_shutdown & RCV_SHUTDOWN)
1420 sk->sk_shutdown |= RCV_SHUTDOWN;
1423 if (sk->sk_shutdown & SEND_SHUTDOWN)
1425 sk->sk_shutdown |= SEND_SHUTDOWN;
1428 if (sk->sk_shutdown & SHUTDOWN_MASK)
1430 sk->sk_shutdown |= SHUTDOWN_MASK;
1434 iso_sock_clear_timer(sk);
1435 __iso_sock_close(sk);
1437 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1438 !(current->flags & PF_EXITING))
1439 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1448 static int iso_sock_release(struct socket *sock)
1450 struct sock *sk = sock->sk;
1453 BT_DBG("sock %p, sk %p", sock, sk);
1460 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1461 !(current->flags & PF_EXITING)) {
1463 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1472 static void iso_sock_ready(struct sock *sk)
1474 BT_DBG("sk %p", sk);
1480 iso_sock_clear_timer(sk);
1481 sk->sk_state = BT_CONNECTED;
1482 sk->sk_state_change(sk);
1486 struct iso_list_data {
1487 struct hci_conn *hcon;
1491 static bool iso_match_big(struct sock *sk, void *data)
1493 struct hci_evt_le_big_sync_estabilished *ev = data;
1495 return ev->handle == iso_pi(sk)->qos.bcast.big;
1498 static void iso_conn_ready(struct iso_conn *conn)
1500 struct sock *parent;
1501 struct sock *sk = conn->sk;
1502 struct hci_ev_le_big_sync_estabilished *ev;
1503 struct hci_conn *hcon;
1505 BT_DBG("conn %p", conn);
1508 iso_sock_ready(conn->sk);
1514 ev = hci_recv_event_data(hcon->hdev,
1515 HCI_EVT_LE_BIG_SYNC_ESTABILISHED);
1517 parent = iso_get_sock_listen(&hcon->src,
1521 parent = iso_get_sock_listen(&hcon->src,
1522 BDADDR_ANY, NULL, NULL);
1529 sk = iso_sock_alloc(sock_net(parent), NULL,
1530 BTPROTO_ISO, GFP_ATOMIC, 0);
1532 release_sock(parent);
1536 iso_sock_init(sk, parent);
1538 bacpy(&iso_pi(sk)->src, &hcon->src);
1539 iso_pi(sk)->src_type = hcon->src_type;
1541 /* If hcon has no destination address (BDADDR_ANY) it means it
1542 * was created by HCI_EV_LE_BIG_SYNC_ESTABILISHED so we need to
1543 * initialize using the parent socket destination address.
1545 if (!bacmp(&hcon->dst, BDADDR_ANY)) {
1546 bacpy(&hcon->dst, &iso_pi(parent)->dst);
1547 hcon->dst_type = iso_pi(parent)->dst_type;
1548 hcon->sync_handle = iso_pi(parent)->sync_handle;
1551 bacpy(&iso_pi(sk)->dst, &hcon->dst);
1552 iso_pi(sk)->dst_type = hcon->dst_type;
1554 hci_conn_hold(hcon);
1555 iso_chan_add(conn, sk, parent);
1557 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1558 sk->sk_state = BT_CONNECT2;
1560 sk->sk_state = BT_CONNECTED;
1562 /* Wake up parent */
1563 parent->sk_data_ready(parent);
1565 release_sock(parent);
1569 static bool iso_match_sid(struct sock *sk, void *data)
1571 struct hci_ev_le_pa_sync_established *ev = data;
1573 return ev->sid == iso_pi(sk)->bc_sid;
1576 static bool iso_match_sync_handle(struct sock *sk, void *data)
1578 struct hci_evt_le_big_info_adv_report *ev = data;
1580 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle;
1583 /* ----- ISO interface with lower layer (HCI) ----- */
1585 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1587 struct hci_ev_le_pa_sync_established *ev1;
1588 struct hci_evt_le_big_info_adv_report *ev2;
1592 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
1594 /* Broadcast receiver requires handling of some events before it can
1595 * proceed to establishing a BIG sync:
1597 * 1. HCI_EV_LE_PA_SYNC_ESTABLISHED: The socket may specify a specific
1598 * SID to listen to and once sync is estabilished its handle needs to
1599 * be stored in iso_pi(sk)->sync_handle so it can be matched once
1600 * receiving the BIG Info.
1601 * 2. HCI_EVT_LE_BIG_INFO_ADV_REPORT: When connect_ind is triggered by a
1602 * a BIG Info it attempts to check if there any listening socket with
1603 * the same sync_handle and if it does then attempt to create a sync.
1605 ev1 = hci_recv_event_data(hdev, HCI_EV_LE_PA_SYNC_ESTABLISHED);
1607 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr, iso_match_sid,
1610 iso_pi(sk)->sync_handle = le16_to_cpu(ev1->handle);
1615 ev2 = hci_recv_event_data(hdev, HCI_EVT_LE_BIG_INFO_ADV_REPORT);
1617 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr,
1618 iso_match_sync_handle, ev2);
1622 if (ev2->num_bis < iso_pi(sk)->bc_num_bis)
1623 iso_pi(sk)->bc_num_bis = ev2->num_bis;
1625 err = hci_le_big_create_sync(hdev,
1627 iso_pi(sk)->sync_handle,
1628 iso_pi(sk)->bc_num_bis,
1629 iso_pi(sk)->bc_bis);
1631 bt_dev_err(hdev, "hci_le_big_create_sync: %d",
1637 sk = iso_get_sock_listen(&hdev->bdaddr, BDADDR_ANY, NULL, NULL);
1644 lm |= HCI_LM_ACCEPT;
1646 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1647 *flags |= HCI_PROTO_DEFER;
1652 static void iso_connect_cfm(struct hci_conn *hcon, __u8 status)
1654 if (hcon->type != ISO_LINK) {
1655 if (hcon->type != LE_LINK)
1658 /* Check if LE link has failed */
1660 struct hci_link *link, *t;
1662 list_for_each_entry_safe(link, t, &hcon->link_list,
1664 iso_conn_del(link->conn, bt_to_errno(status));
1669 /* Create CIS if pending */
1670 hci_le_create_cis(hcon);
1674 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
1677 struct iso_conn *conn;
1679 conn = iso_conn_add(hcon);
1681 iso_conn_ready(conn);
1683 iso_conn_del(hcon, bt_to_errno(status));
1687 static void iso_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1689 if (hcon->type != ISO_LINK)
1692 BT_DBG("hcon %p reason %d", hcon, reason);
1694 iso_conn_del(hcon, bt_to_errno(reason));
1697 void iso_recv(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
1699 struct iso_conn *conn = hcon->iso_data;
1705 pb = hci_iso_flags_pb(flags);
1706 ts = hci_iso_flags_ts(flags);
1708 BT_DBG("conn %p len %d pb 0x%x ts 0x%x", conn, skb->len, pb, ts);
1714 BT_ERR("Unexpected start frame (len %d)", skb->len);
1715 kfree_skb(conn->rx_skb);
1716 conn->rx_skb = NULL;
1721 struct hci_iso_ts_data_hdr *hdr;
1723 /* TODO: add timestamp to the packet? */
1724 hdr = skb_pull_data(skb, HCI_ISO_TS_DATA_HDR_SIZE);
1726 BT_ERR("Frame is too short (len %d)", skb->len);
1730 len = __le16_to_cpu(hdr->slen);
1732 struct hci_iso_data_hdr *hdr;
1734 hdr = skb_pull_data(skb, HCI_ISO_DATA_HDR_SIZE);
1736 BT_ERR("Frame is too short (len %d)", skb->len);
1740 len = __le16_to_cpu(hdr->slen);
1743 flags = hci_iso_data_flags(len);
1744 len = hci_iso_data_len(len);
1746 BT_DBG("Start: total len %d, frag len %d flags 0x%4.4x", len,
1749 if (len == skb->len) {
1750 /* Complete frame received */
1751 iso_recv_frame(conn, skb);
1755 if (pb == ISO_SINGLE) {
1756 BT_ERR("Frame malformed (len %d, expected len %d)",
1761 if (skb->len > len) {
1762 BT_ERR("Frame is too long (len %d, expected len %d)",
1767 /* Allocate skb for the complete frame (with header) */
1768 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
1772 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1774 conn->rx_len = len - skb->len;
1778 BT_DBG("Cont: frag len %d (expecting %d)", skb->len,
1781 if (!conn->rx_len) {
1782 BT_ERR("Unexpected continuation frame (len %d)",
1787 if (skb->len > conn->rx_len) {
1788 BT_ERR("Fragment is too long (len %d, expected %d)",
1789 skb->len, conn->rx_len);
1790 kfree_skb(conn->rx_skb);
1791 conn->rx_skb = NULL;
1796 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1798 conn->rx_len -= skb->len;
1802 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1804 conn->rx_len -= skb->len;
1806 if (!conn->rx_len) {
1807 struct sk_buff *rx_skb = conn->rx_skb;
1809 /* Complete frame received. iso_recv_frame
1810 * takes ownership of the skb so set the global
1811 * rx_skb pointer to NULL first.
1813 conn->rx_skb = NULL;
1814 iso_recv_frame(conn, rx_skb);
1823 static struct hci_cb iso_cb = {
1825 .connect_cfm = iso_connect_cfm,
1826 .disconn_cfm = iso_disconn_cfm,
1829 static int iso_debugfs_show(struct seq_file *f, void *p)
1833 read_lock(&iso_sk_list.lock);
1835 sk_for_each(sk, &iso_sk_list.head) {
1836 seq_printf(f, "%pMR %pMR %d\n", &iso_pi(sk)->src,
1837 &iso_pi(sk)->dst, sk->sk_state);
1840 read_unlock(&iso_sk_list.lock);
1845 DEFINE_SHOW_ATTRIBUTE(iso_debugfs);
1847 static struct dentry *iso_debugfs;
1849 static const struct proto_ops iso_sock_ops = {
1850 .family = PF_BLUETOOTH,
1851 .owner = THIS_MODULE,
1852 .release = iso_sock_release,
1853 .bind = iso_sock_bind,
1854 .connect = iso_sock_connect,
1855 .listen = iso_sock_listen,
1856 .accept = iso_sock_accept,
1857 .getname = iso_sock_getname,
1858 .sendmsg = iso_sock_sendmsg,
1859 .recvmsg = iso_sock_recvmsg,
1860 .poll = bt_sock_poll,
1861 .ioctl = bt_sock_ioctl,
1862 .mmap = sock_no_mmap,
1863 .socketpair = sock_no_socketpair,
1864 .shutdown = iso_sock_shutdown,
1865 .setsockopt = iso_sock_setsockopt,
1866 .getsockopt = iso_sock_getsockopt
1869 static const struct net_proto_family iso_sock_family_ops = {
1870 .family = PF_BLUETOOTH,
1871 .owner = THIS_MODULE,
1872 .create = iso_sock_create,
1875 static bool iso_inited;
1877 bool iso_enabled(void)
1886 BUILD_BUG_ON(sizeof(struct sockaddr_iso) > sizeof(struct sockaddr));
1891 err = proto_register(&iso_proto, 0);
1895 err = bt_sock_register(BTPROTO_ISO, &iso_sock_family_ops);
1897 BT_ERR("ISO socket registration failed");
1901 err = bt_procfs_init(&init_net, "iso", &iso_sk_list, NULL);
1903 BT_ERR("Failed to create ISO proc file");
1904 bt_sock_unregister(BTPROTO_ISO);
1908 BT_INFO("ISO socket layer initialized");
1910 hci_register_cb(&iso_cb);
1912 if (IS_ERR_OR_NULL(bt_debugfs))
1916 iso_debugfs = debugfs_create_file("iso", 0444, bt_debugfs,
1917 NULL, &iso_debugfs_fops);
1925 proto_unregister(&iso_proto);
1934 bt_procfs_cleanup(&init_net, "iso");
1936 debugfs_remove(iso_debugfs);
1939 hci_unregister_cb(&iso_cb);
1941 bt_sock_unregister(BTPROTO_ISO);
1943 proto_unregister(&iso_proto);