1 // SPDX-License-Identifier: GPL-2.0
3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2022 Intel Corporation
8 #include <linux/module.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/sched/signal.h>
13 #include <net/bluetooth/bluetooth.h>
14 #include <net/bluetooth/hci_core.h>
15 #include <net/bluetooth/iso.h>
17 static const struct proto_ops iso_sock_ops;
19 static struct bt_sock_list iso_sk_list = {
20 .lock = __RW_LOCK_UNLOCKED(iso_sk_list.lock)
23 /* ---- ISO connections ---- */
25 struct hci_conn *hcon;
27 /* @lock: spinlock protecting changes to iso_conn fields */
31 struct delayed_work timeout_work;
33 struct sk_buff *rx_skb;
38 #define iso_conn_lock(c) spin_lock(&(c)->lock)
39 #define iso_conn_unlock(c) spin_unlock(&(c)->lock)
41 static void iso_sock_close(struct sock *sk);
42 static void iso_sock_kill(struct sock *sk);
44 /* ----- ISO socket info ----- */
45 #define iso_pi(sk) ((struct iso_pinfo *)sk)
47 #define EIR_SERVICE_DATA_LENGTH 4
48 #define BASE_MAX_LENGTH (HCI_MAX_PER_AD_LENGTH - EIR_SERVICE_DATA_LENGTH)
58 __u8 bc_bis[ISO_MAX_NUM_BIS];
61 struct bt_iso_qos qos;
63 __u8 base[BASE_MAX_LENGTH];
64 struct iso_conn *conn;
67 /* ---- ISO timers ---- */
68 #define ISO_CONN_TIMEOUT (HZ * 40)
69 #define ISO_DISCONN_TIMEOUT (HZ * 2)
71 static void iso_sock_timeout(struct work_struct *work)
73 struct iso_conn *conn = container_of(work, struct iso_conn,
81 iso_conn_unlock(conn);
86 BT_DBG("sock %p state %d", sk, sk->sk_state);
89 sk->sk_err = ETIMEDOUT;
90 sk->sk_state_change(sk);
95 static void iso_sock_set_timer(struct sock *sk, long timeout)
97 if (!iso_pi(sk)->conn)
100 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
101 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
102 schedule_delayed_work(&iso_pi(sk)->conn->timeout_work, timeout);
105 static void iso_sock_clear_timer(struct sock *sk)
107 if (!iso_pi(sk)->conn)
110 BT_DBG("sock %p state %d", sk, sk->sk_state);
111 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
114 /* ---- ISO connections ---- */
115 static struct iso_conn *iso_conn_add(struct hci_conn *hcon)
117 struct iso_conn *conn = hcon->iso_data;
122 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
126 spin_lock_init(&conn->lock);
127 INIT_DELAYED_WORK(&conn->timeout_work, iso_sock_timeout);
129 hcon->iso_data = conn;
133 BT_DBG("hcon %p conn %p", hcon, conn);
138 /* Delete channel. Must be called on the locked socket. */
139 static void iso_chan_del(struct sock *sk, int err)
141 struct iso_conn *conn;
144 conn = iso_pi(sk)->conn;
146 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
151 iso_pi(sk)->conn = NULL;
152 iso_conn_unlock(conn);
155 hci_conn_drop(conn->hcon);
158 sk->sk_state = BT_CLOSED;
161 parent = bt_sk(sk)->parent;
163 bt_accept_unlink(sk);
164 parent->sk_data_ready(parent);
166 sk->sk_state_change(sk);
169 sock_set_flag(sk, SOCK_ZAPPED);
172 static void iso_conn_del(struct hci_conn *hcon, int err)
174 struct iso_conn *conn = hcon->iso_data;
180 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
187 iso_conn_unlock(conn);
191 iso_sock_clear_timer(sk);
192 iso_chan_del(sk, err);
197 /* Ensure no more work items will run before freeing conn. */
198 cancel_delayed_work_sync(&conn->timeout_work);
200 hcon->iso_data = NULL;
204 static int __iso_chan_add(struct iso_conn *conn, struct sock *sk,
207 BT_DBG("conn %p", conn);
209 if (iso_pi(sk)->conn == conn && conn->sk == sk)
213 BT_ERR("conn->sk already set");
217 iso_pi(sk)->conn = conn;
221 bt_accept_enqueue(parent, sk, true);
226 static int iso_chan_add(struct iso_conn *conn, struct sock *sk,
232 err = __iso_chan_add(conn, sk, parent);
233 iso_conn_unlock(conn);
238 static inline u8 le_addr_type(u8 bdaddr_type)
240 if (bdaddr_type == BDADDR_LE_PUBLIC)
241 return ADDR_LE_DEV_PUBLIC;
243 return ADDR_LE_DEV_RANDOM;
246 static int iso_connect_bis(struct sock *sk)
248 struct iso_conn *conn;
249 struct hci_conn *hcon;
250 struct hci_dev *hdev;
253 BT_DBG("%pMR", &iso_pi(sk)->src);
255 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
256 iso_pi(sk)->src_type);
258 return -EHOSTUNREACH;
262 if (!bis_capable(hdev)) {
267 /* Fail if out PHYs are marked as disabled */
268 if (!iso_pi(sk)->qos.out.phy) {
273 hcon = hci_connect_bis(hdev, &iso_pi(sk)->dst,
274 le_addr_type(iso_pi(sk)->dst_type),
275 &iso_pi(sk)->qos, iso_pi(sk)->base_len,
282 conn = iso_conn_add(hcon);
289 hci_dev_unlock(hdev);
294 /* Update source addr of the socket */
295 bacpy(&iso_pi(sk)->src, &hcon->src);
297 err = iso_chan_add(conn, sk, NULL);
301 if (hcon->state == BT_CONNECTED) {
302 iso_sock_clear_timer(sk);
303 sk->sk_state = BT_CONNECTED;
305 sk->sk_state = BT_CONNECT;
306 iso_sock_set_timer(sk, sk->sk_sndtimeo);
314 hci_dev_unlock(hdev);
319 static int iso_connect_cis(struct sock *sk)
321 struct iso_conn *conn;
322 struct hci_conn *hcon;
323 struct hci_dev *hdev;
326 BT_DBG("%pMR -> %pMR", &iso_pi(sk)->src, &iso_pi(sk)->dst);
328 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
329 iso_pi(sk)->src_type);
331 return -EHOSTUNREACH;
335 if (!cis_central_capable(hdev)) {
340 /* Fail if either PHYs are marked as disabled */
341 if (!iso_pi(sk)->qos.in.phy && !iso_pi(sk)->qos.out.phy) {
346 /* Just bind if DEFER_SETUP has been set */
347 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
348 hcon = hci_bind_cis(hdev, &iso_pi(sk)->dst,
349 le_addr_type(iso_pi(sk)->dst_type),
356 hcon = hci_connect_cis(hdev, &iso_pi(sk)->dst,
357 le_addr_type(iso_pi(sk)->dst_type),
365 conn = iso_conn_add(hcon);
372 hci_dev_unlock(hdev);
377 /* Update source addr of the socket */
378 bacpy(&iso_pi(sk)->src, &hcon->src);
380 err = iso_chan_add(conn, sk, NULL);
384 if (hcon->state == BT_CONNECTED) {
385 iso_sock_clear_timer(sk);
386 sk->sk_state = BT_CONNECTED;
387 } else if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
388 iso_sock_clear_timer(sk);
389 sk->sk_state = BT_CONNECT;
391 sk->sk_state = BT_CONNECT;
392 iso_sock_set_timer(sk, sk->sk_sndtimeo);
400 hci_dev_unlock(hdev);
405 static struct bt_iso_qos *iso_sock_get_qos(struct sock *sk)
407 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONNECT2)
408 return &iso_pi(sk)->conn->hcon->iso_qos;
410 return &iso_pi(sk)->qos;
413 static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
415 struct iso_conn *conn = iso_pi(sk)->conn;
416 struct bt_iso_qos *qos = iso_sock_get_qos(sk);
417 struct hci_iso_data_hdr *hdr;
420 BT_DBG("sk %p len %d", sk, skb->len);
422 if (skb->len > qos->out.sdu)
427 /* Push ISO data header */
428 hdr = skb_push(skb, HCI_ISO_DATA_HDR_SIZE);
429 hdr->sn = cpu_to_le16(conn->tx_sn++);
430 hdr->slen = cpu_to_le16(hci_iso_data_len_pack(len,
431 HCI_ISO_STATUS_VALID));
433 if (sk->sk_state == BT_CONNECTED)
434 hci_send_iso(conn->hcon, skb);
441 static void iso_recv_frame(struct iso_conn *conn, struct sk_buff *skb)
447 iso_conn_unlock(conn);
452 BT_DBG("sk %p len %d", sk, skb->len);
454 if (sk->sk_state != BT_CONNECTED)
457 if (!sock_queue_rcv_skb(sk, skb))
464 /* -------- Socket interface ---------- */
465 static struct sock *__iso_get_sock_listen_by_addr(bdaddr_t *ba)
469 sk_for_each(sk, &iso_sk_list.head) {
470 if (sk->sk_state != BT_LISTEN)
473 if (!bacmp(&iso_pi(sk)->src, ba))
480 static struct sock *__iso_get_sock_listen_by_sid(bdaddr_t *ba, bdaddr_t *bc,
485 sk_for_each(sk, &iso_sk_list.head) {
486 if (sk->sk_state != BT_LISTEN)
489 if (bacmp(&iso_pi(sk)->src, ba))
492 if (bacmp(&iso_pi(sk)->dst, bc))
495 if (iso_pi(sk)->bc_sid == sid)
502 typedef bool (*iso_sock_match_t)(struct sock *sk, void *data);
504 /* Find socket listening:
505 * source bdaddr (Unicast)
506 * destination bdaddr (Broadcast only)
507 * match func - pass NULL to ignore
508 * match func data - pass -1 to ignore
509 * Returns closest match.
511 static struct sock *iso_get_sock_listen(bdaddr_t *src, bdaddr_t *dst,
512 iso_sock_match_t match, void *data)
514 struct sock *sk = NULL, *sk1 = NULL;
516 read_lock(&iso_sk_list.lock);
518 sk_for_each(sk, &iso_sk_list.head) {
519 if (sk->sk_state != BT_LISTEN)
522 /* Match Broadcast destination */
523 if (bacmp(dst, BDADDR_ANY) && bacmp(&iso_pi(sk)->dst, dst))
526 /* Use Match function if provided */
527 if (match && !match(sk, data))
531 if (!bacmp(&iso_pi(sk)->src, src))
535 if (!bacmp(&iso_pi(sk)->src, BDADDR_ANY))
539 read_unlock(&iso_sk_list.lock);
541 return sk ? sk : sk1;
544 static void iso_sock_destruct(struct sock *sk)
548 skb_queue_purge(&sk->sk_receive_queue);
549 skb_queue_purge(&sk->sk_write_queue);
552 static void iso_sock_cleanup_listen(struct sock *parent)
556 BT_DBG("parent %p", parent);
558 /* Close not yet accepted channels */
559 while ((sk = bt_accept_dequeue(parent, NULL))) {
564 parent->sk_state = BT_CLOSED;
565 sock_set_flag(parent, SOCK_ZAPPED);
568 /* Kill socket (only if zapped and orphan)
569 * Must be called on unlocked socket.
571 static void iso_sock_kill(struct sock *sk)
573 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||
574 sock_flag(sk, SOCK_DEAD))
577 BT_DBG("sk %p state %d", sk, sk->sk_state);
579 /* Kill poor orphan */
580 bt_sock_unlink(&iso_sk_list, sk);
581 sock_set_flag(sk, SOCK_DEAD);
585 static void iso_conn_defer_reject(struct hci_conn *conn)
587 struct hci_cp_le_reject_cis cp;
589 BT_DBG("conn %p", conn);
591 memset(&cp, 0, sizeof(cp));
592 cp.handle = cpu_to_le16(conn->handle);
593 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
594 hci_send_cmd(conn->hdev, HCI_OP_LE_REJECT_CIS, sizeof(cp), &cp);
597 static void __iso_sock_close(struct sock *sk)
599 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
601 switch (sk->sk_state) {
603 iso_sock_cleanup_listen(sk);
608 if (iso_pi(sk)->conn->hcon) {
609 sk->sk_state = BT_DISCONN;
610 iso_sock_set_timer(sk, ISO_DISCONN_TIMEOUT);
611 iso_conn_lock(iso_pi(sk)->conn);
612 hci_conn_drop(iso_pi(sk)->conn->hcon);
613 iso_pi(sk)->conn->hcon = NULL;
614 iso_conn_unlock(iso_pi(sk)->conn);
616 iso_chan_del(sk, ECONNRESET);
621 if (iso_pi(sk)->conn->hcon)
622 iso_conn_defer_reject(iso_pi(sk)->conn->hcon);
623 iso_chan_del(sk, ECONNRESET);
626 /* In case of DEFER_SETUP the hcon would be bound to CIG which
627 * needs to be removed so just call hci_conn_del so the cleanup
628 * callback do what is needed.
630 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags) &&
631 iso_pi(sk)->conn->hcon) {
632 hci_conn_del(iso_pi(sk)->conn->hcon);
633 iso_pi(sk)->conn->hcon = NULL;
636 iso_chan_del(sk, ECONNRESET);
639 iso_chan_del(sk, ECONNRESET);
643 sock_set_flag(sk, SOCK_ZAPPED);
648 /* Must be called on unlocked socket. */
649 static void iso_sock_close(struct sock *sk)
651 iso_sock_clear_timer(sk);
653 __iso_sock_close(sk);
658 static void iso_sock_init(struct sock *sk, struct sock *parent)
663 sk->sk_type = parent->sk_type;
664 bt_sk(sk)->flags = bt_sk(parent)->flags;
665 security_sk_clone(parent, sk);
669 static struct proto iso_proto = {
671 .owner = THIS_MODULE,
672 .obj_size = sizeof(struct iso_pinfo)
675 #define DEFAULT_IO_QOS \
677 .interval = 10000u, \
680 .phy = BT_ISO_PHY_2M, \
684 static struct bt_iso_qos default_qos = {
685 .cig = BT_ISO_QOS_CIG_UNSET,
686 .cis = BT_ISO_QOS_CIS_UNSET,
690 .in = DEFAULT_IO_QOS,
691 .out = DEFAULT_IO_QOS,
694 static struct sock *iso_sock_alloc(struct net *net, struct socket *sock,
695 int proto, gfp_t prio, int kern)
699 sk = sk_alloc(net, PF_BLUETOOTH, prio, &iso_proto, kern);
703 sock_init_data(sock, sk);
704 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
706 sk->sk_destruct = iso_sock_destruct;
707 sk->sk_sndtimeo = ISO_CONN_TIMEOUT;
709 sock_reset_flag(sk, SOCK_ZAPPED);
711 sk->sk_protocol = proto;
712 sk->sk_state = BT_OPEN;
714 /* Set address type as public as default src address is BDADDR_ANY */
715 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC;
717 iso_pi(sk)->qos = default_qos;
719 bt_sock_link(&iso_sk_list, sk);
723 static int iso_sock_create(struct net *net, struct socket *sock, int protocol,
728 BT_DBG("sock %p", sock);
730 sock->state = SS_UNCONNECTED;
732 if (sock->type != SOCK_SEQPACKET)
733 return -ESOCKTNOSUPPORT;
735 sock->ops = &iso_sock_ops;
737 sk = iso_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
741 iso_sock_init(sk, NULL);
745 static int iso_sock_bind_bc(struct socket *sock, struct sockaddr *addr,
748 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
749 struct sock *sk = sock->sk;
752 BT_DBG("sk %p bc_sid %u bc_num_bis %u", sk, sa->iso_bc->bc_sid,
753 sa->iso_bc->bc_num_bis);
755 if (addr_len > sizeof(*sa) + sizeof(*sa->iso_bc) ||
756 sa->iso_bc->bc_num_bis < 0x01 || sa->iso_bc->bc_num_bis > 0x1f)
759 bacpy(&iso_pi(sk)->dst, &sa->iso_bc->bc_bdaddr);
760 iso_pi(sk)->dst_type = sa->iso_bc->bc_bdaddr_type;
761 iso_pi(sk)->sync_handle = -1;
762 iso_pi(sk)->bc_sid = sa->iso_bc->bc_sid;
763 iso_pi(sk)->bc_num_bis = sa->iso_bc->bc_num_bis;
765 for (i = 0; i < iso_pi(sk)->bc_num_bis; i++) {
766 if (sa->iso_bc->bc_bis[i] < 0x01 ||
767 sa->iso_bc->bc_bis[i] > 0x1f)
770 memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis,
771 iso_pi(sk)->bc_num_bis);
777 static int iso_sock_bind(struct socket *sock, struct sockaddr *addr,
780 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
781 struct sock *sk = sock->sk;
784 BT_DBG("sk %p %pMR type %u", sk, &sa->iso_bdaddr, sa->iso_bdaddr_type);
786 if (!addr || addr_len < sizeof(struct sockaddr_iso) ||
787 addr->sa_family != AF_BLUETOOTH)
792 if (sk->sk_state != BT_OPEN) {
797 if (sk->sk_type != SOCK_SEQPACKET) {
802 /* Check if the address type is of LE type */
803 if (!bdaddr_type_is_le(sa->iso_bdaddr_type)) {
808 bacpy(&iso_pi(sk)->src, &sa->iso_bdaddr);
809 iso_pi(sk)->src_type = sa->iso_bdaddr_type;
811 /* Check for Broadcast address */
812 if (addr_len > sizeof(*sa)) {
813 err = iso_sock_bind_bc(sock, addr, addr_len);
818 sk->sk_state = BT_BOUND;
825 static int iso_sock_connect(struct socket *sock, struct sockaddr *addr,
828 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
829 struct sock *sk = sock->sk;
834 if (alen < sizeof(struct sockaddr_iso) ||
835 addr->sa_family != AF_BLUETOOTH)
838 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
841 if (sk->sk_type != SOCK_SEQPACKET)
844 /* Check if the address type is of LE type */
845 if (!bdaddr_type_is_le(sa->iso_bdaddr_type))
850 bacpy(&iso_pi(sk)->dst, &sa->iso_bdaddr);
851 iso_pi(sk)->dst_type = sa->iso_bdaddr_type;
855 if (bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
856 err = iso_connect_cis(sk);
858 err = iso_connect_bis(sk);
865 if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
866 err = bt_sock_wait_state(sk, BT_CONNECTED,
867 sock_sndtimeo(sk, flags & O_NONBLOCK));
874 static int iso_listen_bis(struct sock *sk)
876 struct hci_dev *hdev;
879 BT_DBG("%pMR -> %pMR (SID 0x%2.2x)", &iso_pi(sk)->src,
880 &iso_pi(sk)->dst, iso_pi(sk)->bc_sid);
882 write_lock(&iso_sk_list.lock);
884 if (__iso_get_sock_listen_by_sid(&iso_pi(sk)->src, &iso_pi(sk)->dst,
888 write_unlock(&iso_sk_list.lock);
893 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
894 iso_pi(sk)->src_type);
896 return -EHOSTUNREACH;
900 err = hci_pa_create_sync(hdev, &iso_pi(sk)->dst,
901 le_addr_type(iso_pi(sk)->dst_type),
904 hci_dev_unlock(hdev);
910 static int iso_listen_cis(struct sock *sk)
914 BT_DBG("%pMR", &iso_pi(sk)->src);
916 write_lock(&iso_sk_list.lock);
918 if (__iso_get_sock_listen_by_addr(&iso_pi(sk)->src))
921 write_unlock(&iso_sk_list.lock);
926 static int iso_sock_listen(struct socket *sock, int backlog)
928 struct sock *sk = sock->sk;
931 BT_DBG("sk %p backlog %d", sk, backlog);
935 if (sk->sk_state != BT_BOUND) {
940 if (sk->sk_type != SOCK_SEQPACKET) {
945 if (!bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
946 err = iso_listen_cis(sk);
948 err = iso_listen_bis(sk);
953 sk->sk_max_ack_backlog = backlog;
954 sk->sk_ack_backlog = 0;
956 sk->sk_state = BT_LISTEN;
963 static int iso_sock_accept(struct socket *sock, struct socket *newsock,
964 int flags, bool kern)
966 DEFINE_WAIT_FUNC(wait, woken_wake_function);
967 struct sock *sk = sock->sk, *ch;
973 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
975 BT_DBG("sk %p timeo %ld", sk, timeo);
977 /* Wait for an incoming connection. (wake-one). */
978 add_wait_queue_exclusive(sk_sleep(sk), &wait);
980 if (sk->sk_state != BT_LISTEN) {
985 ch = bt_accept_dequeue(sk, newsock);
994 if (signal_pending(current)) {
995 err = sock_intr_errno(timeo);
1001 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1004 remove_wait_queue(sk_sleep(sk), &wait);
1009 newsock->state = SS_CONNECTED;
1011 BT_DBG("new socket %p", ch);
1018 static int iso_sock_getname(struct socket *sock, struct sockaddr *addr,
1021 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
1022 struct sock *sk = sock->sk;
1024 BT_DBG("sock %p, sk %p", sock, sk);
1026 addr->sa_family = AF_BLUETOOTH;
1029 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->dst);
1030 sa->iso_bdaddr_type = iso_pi(sk)->dst_type;
1032 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->src);
1033 sa->iso_bdaddr_type = iso_pi(sk)->src_type;
1036 return sizeof(struct sockaddr_iso);
1039 static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1042 struct sock *sk = sock->sk;
1043 struct iso_conn *conn = iso_pi(sk)->conn;
1044 struct sk_buff *skb, **frag;
1047 BT_DBG("sock %p, sk %p", sock, sk);
1049 err = sock_error(sk);
1053 if (msg->msg_flags & MSG_OOB)
1056 if (sk->sk_state != BT_CONNECTED)
1059 skb = bt_skb_sendmsg(sk, msg, len, conn->hcon->hdev->iso_mtu,
1060 HCI_ISO_DATA_HDR_SIZE, 0);
1062 return PTR_ERR(skb);
1066 BT_DBG("skb %p len %d", sk, skb->len);
1068 /* Continuation fragments */
1069 frag = &skb_shinfo(skb)->frag_list;
1071 struct sk_buff *tmp;
1073 tmp = bt_skb_sendmsg(sk, msg, len, conn->hcon->hdev->iso_mtu,
1077 return PTR_ERR(tmp);
1084 skb->len += tmp->len;
1085 skb->data_len += tmp->len;
1087 BT_DBG("frag %p len %d", *frag, tmp->len);
1089 frag = &(*frag)->next;
1094 if (sk->sk_state == BT_CONNECTED)
1095 err = iso_send_frame(sk, skb);
1106 static void iso_conn_defer_accept(struct hci_conn *conn)
1108 struct hci_cp_le_accept_cis cp;
1109 struct hci_dev *hdev = conn->hdev;
1111 BT_DBG("conn %p", conn);
1113 conn->state = BT_CONFIG;
1115 cp.handle = cpu_to_le16(conn->handle);
1117 hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp);
1120 static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1121 size_t len, int flags)
1123 struct sock *sk = sock->sk;
1124 struct iso_pinfo *pi = iso_pi(sk);
1126 BT_DBG("sk %p", sk);
1128 if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
1129 switch (sk->sk_state) {
1132 iso_conn_defer_accept(pi->conn->hcon);
1133 sk->sk_state = BT_CONFIG;
1137 return iso_connect_cis(sk);
1141 return bt_sock_recvmsg(sock, msg, len, flags);
1144 static bool check_io_qos(struct bt_iso_io_qos *qos)
1146 /* If no PHY is enable SDU must be 0 */
1147 if (!qos->phy && qos->sdu)
1150 if (qos->interval && (qos->interval < 0xff || qos->interval > 0xfffff))
1153 if (qos->latency && (qos->latency < 0x05 || qos->latency > 0xfa0))
1156 if (qos->phy > BT_ISO_PHY_ANY)
1162 static bool check_qos(struct bt_iso_qos *qos)
1164 if (qos->sca > 0x07)
1167 if (qos->packing > 0x01)
1170 if (qos->framing > 0x01)
1173 if (!check_io_qos(&qos->in))
1176 if (!check_io_qos(&qos->out))
1182 static int iso_sock_setsockopt(struct socket *sock, int level, int optname,
1183 sockptr_t optval, unsigned int optlen)
1185 struct sock *sk = sock->sk;
1187 struct bt_iso_qos qos;
1190 BT_DBG("sk %p", sk);
1195 case BT_DEFER_SETUP:
1196 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1201 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
1207 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1209 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1213 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1214 sk->sk_state != BT_CONNECT2) {
1219 len = min_t(unsigned int, sizeof(qos), optlen);
1220 if (len != sizeof(qos)) {
1225 memset(&qos, 0, sizeof(qos));
1227 if (copy_from_sockptr(&qos, optval, len)) {
1232 if (!check_qos(&qos)) {
1237 iso_pi(sk)->qos = qos;
1242 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1243 sk->sk_state != BT_CONNECT2) {
1248 if (optlen > sizeof(iso_pi(sk)->base)) {
1253 len = min_t(unsigned int, sizeof(iso_pi(sk)->base), optlen);
1255 if (copy_from_sockptr(iso_pi(sk)->base, optval, len)) {
1260 iso_pi(sk)->base_len = len;
1273 static int iso_sock_getsockopt(struct socket *sock, int level, int optname,
1274 char __user *optval, int __user *optlen)
1276 struct sock *sk = sock->sk;
1278 struct bt_iso_qos *qos;
1282 BT_DBG("sk %p", sk);
1284 if (get_user(len, optlen))
1290 case BT_DEFER_SETUP:
1291 if (sk->sk_state == BT_CONNECTED) {
1296 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1297 (u32 __user *)optval))
1303 qos = iso_sock_get_qos(sk);
1305 len = min_t(unsigned int, len, sizeof(*qos));
1306 if (copy_to_user(optval, qos, len))
1312 if (sk->sk_state == BT_CONNECTED) {
1313 base_len = iso_pi(sk)->conn->hcon->le_per_adv_data_len;
1314 base = iso_pi(sk)->conn->hcon->le_per_adv_data;
1316 base_len = iso_pi(sk)->base_len;
1317 base = iso_pi(sk)->base;
1320 len = min_t(unsigned int, len, base_len);
1321 if (copy_to_user(optval, base, len))
1335 static int iso_sock_shutdown(struct socket *sock, int how)
1337 struct sock *sk = sock->sk;
1340 BT_DBG("sock %p, sk %p, how %d", sock, sk, how);
1350 if (sk->sk_shutdown & RCV_SHUTDOWN)
1352 sk->sk_shutdown |= RCV_SHUTDOWN;
1355 if (sk->sk_shutdown & SEND_SHUTDOWN)
1357 sk->sk_shutdown |= SEND_SHUTDOWN;
1360 if (sk->sk_shutdown & SHUTDOWN_MASK)
1362 sk->sk_shutdown |= SHUTDOWN_MASK;
1366 iso_sock_clear_timer(sk);
1367 __iso_sock_close(sk);
1369 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1370 !(current->flags & PF_EXITING))
1371 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1380 static int iso_sock_release(struct socket *sock)
1382 struct sock *sk = sock->sk;
1385 BT_DBG("sock %p, sk %p", sock, sk);
1392 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1393 !(current->flags & PF_EXITING)) {
1395 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1404 static void iso_sock_ready(struct sock *sk)
1406 BT_DBG("sk %p", sk);
1412 iso_sock_clear_timer(sk);
1413 sk->sk_state = BT_CONNECTED;
1414 sk->sk_state_change(sk);
1418 struct iso_list_data {
1419 struct hci_conn *hcon;
1423 static bool iso_match_big(struct sock *sk, void *data)
1425 struct hci_evt_le_big_sync_estabilished *ev = data;
1427 return ev->handle == iso_pi(sk)->qos.big;
1430 static void iso_conn_ready(struct iso_conn *conn)
1432 struct sock *parent;
1433 struct sock *sk = conn->sk;
1434 struct hci_ev_le_big_sync_estabilished *ev;
1436 BT_DBG("conn %p", conn);
1439 iso_sock_ready(conn->sk);
1441 iso_conn_lock(conn);
1444 iso_conn_unlock(conn);
1448 ev = hci_recv_event_data(conn->hcon->hdev,
1449 HCI_EVT_LE_BIG_SYNC_ESTABILISHED);
1451 parent = iso_get_sock_listen(&conn->hcon->src,
1455 parent = iso_get_sock_listen(&conn->hcon->src,
1456 BDADDR_ANY, NULL, NULL);
1459 iso_conn_unlock(conn);
1465 sk = iso_sock_alloc(sock_net(parent), NULL,
1466 BTPROTO_ISO, GFP_ATOMIC, 0);
1468 release_sock(parent);
1469 iso_conn_unlock(conn);
1473 iso_sock_init(sk, parent);
1475 bacpy(&iso_pi(sk)->src, &conn->hcon->src);
1476 iso_pi(sk)->src_type = conn->hcon->src_type;
1478 /* If hcon has no destination address (BDADDR_ANY) it means it
1479 * was created by HCI_EV_LE_BIG_SYNC_ESTABILISHED so we need to
1480 * initialize using the parent socket destination address.
1482 if (!bacmp(&conn->hcon->dst, BDADDR_ANY)) {
1483 bacpy(&conn->hcon->dst, &iso_pi(parent)->dst);
1484 conn->hcon->dst_type = iso_pi(parent)->dst_type;
1485 conn->hcon->sync_handle = iso_pi(parent)->sync_handle;
1488 bacpy(&iso_pi(sk)->dst, &conn->hcon->dst);
1489 iso_pi(sk)->dst_type = conn->hcon->dst_type;
1491 hci_conn_hold(conn->hcon);
1492 __iso_chan_add(conn, sk, parent);
1494 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1495 sk->sk_state = BT_CONNECT2;
1497 sk->sk_state = BT_CONNECTED;
1499 /* Wake up parent */
1500 parent->sk_data_ready(parent);
1502 release_sock(parent);
1504 iso_conn_unlock(conn);
1508 static bool iso_match_sid(struct sock *sk, void *data)
1510 struct hci_ev_le_pa_sync_established *ev = data;
1512 return ev->sid == iso_pi(sk)->bc_sid;
1515 static bool iso_match_sync_handle(struct sock *sk, void *data)
1517 struct hci_evt_le_big_info_adv_report *ev = data;
1519 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle;
1522 /* ----- ISO interface with lower layer (HCI) ----- */
1524 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1526 struct hci_ev_le_pa_sync_established *ev1;
1527 struct hci_evt_le_big_info_adv_report *ev2;
1531 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
1533 /* Broadcast receiver requires handling of some events before it can
1534 * proceed to establishing a BIG sync:
1536 * 1. HCI_EV_LE_PA_SYNC_ESTABLISHED: The socket may specify a specific
1537 * SID to listen to and once sync is estabilished its handle needs to
1538 * be stored in iso_pi(sk)->sync_handle so it can be matched once
1539 * receiving the BIG Info.
1540 * 2. HCI_EVT_LE_BIG_INFO_ADV_REPORT: When connect_ind is triggered by a
1541 * a BIG Info it attempts to check if there any listening socket with
1542 * the same sync_handle and if it does then attempt to create a sync.
1544 ev1 = hci_recv_event_data(hdev, HCI_EV_LE_PA_SYNC_ESTABLISHED);
1546 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr, iso_match_sid,
1549 iso_pi(sk)->sync_handle = le16_to_cpu(ev1->handle);
1554 ev2 = hci_recv_event_data(hdev, HCI_EVT_LE_BIG_INFO_ADV_REPORT);
1556 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr,
1557 iso_match_sync_handle, ev2);
1561 if (ev2->num_bis < iso_pi(sk)->bc_num_bis)
1562 iso_pi(sk)->bc_num_bis = ev2->num_bis;
1564 err = hci_le_big_create_sync(hdev,
1566 iso_pi(sk)->sync_handle,
1567 iso_pi(sk)->bc_num_bis,
1568 iso_pi(sk)->bc_bis);
1570 bt_dev_err(hdev, "hci_le_big_create_sync: %d",
1576 sk = iso_get_sock_listen(&hdev->bdaddr, BDADDR_ANY, NULL, NULL);
1583 lm |= HCI_LM_ACCEPT;
1585 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1586 *flags |= HCI_PROTO_DEFER;
1591 static void iso_connect_cfm(struct hci_conn *hcon, __u8 status)
1593 if (hcon->type != ISO_LINK) {
1594 if (hcon->type != LE_LINK)
1597 /* Check if LE link has failed */
1600 iso_conn_del(hcon->link, bt_to_errno(status));
1604 /* Create CIS if pending */
1605 hci_le_create_cis(hcon);
1609 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
1612 struct iso_conn *conn;
1614 conn = iso_conn_add(hcon);
1616 iso_conn_ready(conn);
1618 iso_conn_del(hcon, bt_to_errno(status));
1622 static void iso_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1624 if (hcon->type != ISO_LINK)
1627 BT_DBG("hcon %p reason %d", hcon, reason);
1629 iso_conn_del(hcon, bt_to_errno(reason));
1632 void iso_recv(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
1634 struct iso_conn *conn = hcon->iso_data;
1635 struct hci_iso_data_hdr *hdr;
1641 pb = hci_iso_flags_pb(flags);
1642 ts = hci_iso_flags_ts(flags);
1644 BT_DBG("conn %p len %d pb 0x%x ts 0x%x", conn, skb->len, pb, ts);
1650 BT_ERR("Unexpected start frame (len %d)", skb->len);
1651 kfree_skb(conn->rx_skb);
1652 conn->rx_skb = NULL;
1657 /* TODO: add timestamp to the packet? */
1658 hdr = skb_pull_data(skb, HCI_ISO_TS_DATA_HDR_SIZE);
1660 BT_ERR("Frame is too short (len %d)", skb->len);
1665 hdr = skb_pull_data(skb, HCI_ISO_DATA_HDR_SIZE);
1667 BT_ERR("Frame is too short (len %d)", skb->len);
1672 len = __le16_to_cpu(hdr->slen);
1673 flags = hci_iso_data_flags(len);
1674 len = hci_iso_data_len(len);
1676 BT_DBG("Start: total len %d, frag len %d flags 0x%4.4x", len,
1679 if (len == skb->len) {
1680 /* Complete frame received */
1681 iso_recv_frame(conn, skb);
1685 if (pb == ISO_SINGLE) {
1686 BT_ERR("Frame malformed (len %d, expected len %d)",
1691 if (skb->len > len) {
1692 BT_ERR("Frame is too long (len %d, expected len %d)",
1697 /* Allocate skb for the complete frame (with header) */
1698 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
1702 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1704 conn->rx_len = len - skb->len;
1708 BT_DBG("Cont: frag len %d (expecting %d)", skb->len,
1711 if (!conn->rx_len) {
1712 BT_ERR("Unexpected continuation frame (len %d)",
1717 if (skb->len > conn->rx_len) {
1718 BT_ERR("Fragment is too long (len %d, expected %d)",
1719 skb->len, conn->rx_len);
1720 kfree_skb(conn->rx_skb);
1721 conn->rx_skb = NULL;
1726 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1728 conn->rx_len -= skb->len;
1732 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1734 conn->rx_len -= skb->len;
1736 if (!conn->rx_len) {
1737 struct sk_buff *rx_skb = conn->rx_skb;
1739 /* Complete frame received. iso_recv_frame
1740 * takes ownership of the skb so set the global
1741 * rx_skb pointer to NULL first.
1743 conn->rx_skb = NULL;
1744 iso_recv_frame(conn, rx_skb);
1753 static struct hci_cb iso_cb = {
1755 .connect_cfm = iso_connect_cfm,
1756 .disconn_cfm = iso_disconn_cfm,
1759 static int iso_debugfs_show(struct seq_file *f, void *p)
1763 read_lock(&iso_sk_list.lock);
1765 sk_for_each(sk, &iso_sk_list.head) {
1766 seq_printf(f, "%pMR %pMR %d\n", &iso_pi(sk)->src,
1767 &iso_pi(sk)->dst, sk->sk_state);
1770 read_unlock(&iso_sk_list.lock);
1775 DEFINE_SHOW_ATTRIBUTE(iso_debugfs);
1777 static struct dentry *iso_debugfs;
1779 static const struct proto_ops iso_sock_ops = {
1780 .family = PF_BLUETOOTH,
1781 .owner = THIS_MODULE,
1782 .release = iso_sock_release,
1783 .bind = iso_sock_bind,
1784 .connect = iso_sock_connect,
1785 .listen = iso_sock_listen,
1786 .accept = iso_sock_accept,
1787 .getname = iso_sock_getname,
1788 .sendmsg = iso_sock_sendmsg,
1789 .recvmsg = iso_sock_recvmsg,
1790 .poll = bt_sock_poll,
1791 .ioctl = bt_sock_ioctl,
1792 .mmap = sock_no_mmap,
1793 .socketpair = sock_no_socketpair,
1794 .shutdown = iso_sock_shutdown,
1795 .setsockopt = iso_sock_setsockopt,
1796 .getsockopt = iso_sock_getsockopt
1799 static const struct net_proto_family iso_sock_family_ops = {
1800 .family = PF_BLUETOOTH,
1801 .owner = THIS_MODULE,
1802 .create = iso_sock_create,
1805 static bool iso_inited;
1807 bool iso_enabled(void)
1816 BUILD_BUG_ON(sizeof(struct sockaddr_iso) > sizeof(struct sockaddr));
1821 err = proto_register(&iso_proto, 0);
1825 err = bt_sock_register(BTPROTO_ISO, &iso_sock_family_ops);
1827 BT_ERR("ISO socket registration failed");
1831 err = bt_procfs_init(&init_net, "iso", &iso_sk_list, NULL);
1833 BT_ERR("Failed to create ISO proc file");
1834 bt_sock_unregister(BTPROTO_ISO);
1838 BT_INFO("ISO socket layer initialized");
1840 hci_register_cb(&iso_cb);
1842 if (IS_ERR_OR_NULL(bt_debugfs))
1846 iso_debugfs = debugfs_create_file("iso", 0444, bt_debugfs,
1847 NULL, &iso_debugfs_fops);
1855 proto_unregister(&iso_proto);
1864 bt_procfs_cleanup(&init_net, "iso");
1866 debugfs_remove(iso_debugfs);
1869 hci_unregister_cb(&iso_cb);
1871 bt_sock_unregister(BTPROTO_ISO);
1873 proto_unregister(&iso_proto);