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, iso_pi(sk)->dst_type,
274 &iso_pi(sk)->qos, iso_pi(sk)->base_len,
281 conn = iso_conn_add(hcon);
288 /* Update source addr of the socket */
289 bacpy(&iso_pi(sk)->src, &hcon->src);
291 err = iso_chan_add(conn, sk, NULL);
295 if (hcon->state == BT_CONNECTED) {
296 iso_sock_clear_timer(sk);
297 sk->sk_state = BT_CONNECTED;
299 sk->sk_state = BT_CONNECT;
300 iso_sock_set_timer(sk, sk->sk_sndtimeo);
304 hci_dev_unlock(hdev);
309 static int iso_connect_cis(struct sock *sk)
311 struct iso_conn *conn;
312 struct hci_conn *hcon;
313 struct hci_dev *hdev;
316 BT_DBG("%pMR -> %pMR", &iso_pi(sk)->src, &iso_pi(sk)->dst);
318 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
319 iso_pi(sk)->src_type);
321 return -EHOSTUNREACH;
325 if (!cis_central_capable(hdev)) {
330 /* Fail if either PHYs are marked as disabled */
331 if (!iso_pi(sk)->qos.in.phy && !iso_pi(sk)->qos.out.phy) {
336 /* Just bind if DEFER_SETUP has been set */
337 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
338 hcon = hci_bind_cis(hdev, &iso_pi(sk)->dst,
339 le_addr_type(iso_pi(sk)->dst_type),
346 hcon = hci_connect_cis(hdev, &iso_pi(sk)->dst,
347 le_addr_type(iso_pi(sk)->dst_type),
355 conn = iso_conn_add(hcon);
362 /* Update source addr of the socket */
363 bacpy(&iso_pi(sk)->src, &hcon->src);
365 err = iso_chan_add(conn, sk, NULL);
369 if (hcon->state == BT_CONNECTED) {
370 iso_sock_clear_timer(sk);
371 sk->sk_state = BT_CONNECTED;
372 } else if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
373 iso_sock_clear_timer(sk);
374 sk->sk_state = BT_CONNECT;
376 sk->sk_state = BT_CONNECT;
377 iso_sock_set_timer(sk, sk->sk_sndtimeo);
381 hci_dev_unlock(hdev);
386 static struct bt_iso_qos *iso_sock_get_qos(struct sock *sk)
388 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONNECT2)
389 return &iso_pi(sk)->conn->hcon->iso_qos;
391 return &iso_pi(sk)->qos;
394 static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
396 struct iso_conn *conn = iso_pi(sk)->conn;
397 struct bt_iso_qos *qos = iso_sock_get_qos(sk);
398 struct hci_iso_data_hdr *hdr;
401 BT_DBG("sk %p len %d", sk, skb->len);
403 if (skb->len > qos->out.sdu)
408 /* Push ISO data header */
409 hdr = skb_push(skb, HCI_ISO_DATA_HDR_SIZE);
410 hdr->sn = cpu_to_le16(conn->tx_sn++);
411 hdr->slen = cpu_to_le16(hci_iso_data_len_pack(len,
412 HCI_ISO_STATUS_VALID));
414 if (sk->sk_state == BT_CONNECTED)
415 hci_send_iso(conn->hcon, skb);
422 static void iso_recv_frame(struct iso_conn *conn, struct sk_buff *skb)
428 iso_conn_unlock(conn);
433 BT_DBG("sk %p len %d", sk, skb->len);
435 if (sk->sk_state != BT_CONNECTED)
438 if (!sock_queue_rcv_skb(sk, skb))
445 /* -------- Socket interface ---------- */
446 static struct sock *__iso_get_sock_listen_by_addr(bdaddr_t *ba)
450 sk_for_each(sk, &iso_sk_list.head) {
451 if (sk->sk_state != BT_LISTEN)
454 if (!bacmp(&iso_pi(sk)->src, ba))
461 static struct sock *__iso_get_sock_listen_by_sid(bdaddr_t *ba, bdaddr_t *bc,
466 sk_for_each(sk, &iso_sk_list.head) {
467 if (sk->sk_state != BT_LISTEN)
470 if (bacmp(&iso_pi(sk)->src, ba))
473 if (bacmp(&iso_pi(sk)->dst, bc))
476 if (iso_pi(sk)->bc_sid == sid)
483 typedef bool (*iso_sock_match_t)(struct sock *sk, void *data);
485 /* Find socket listening:
486 * source bdaddr (Unicast)
487 * destination bdaddr (Broadcast only)
488 * match func - pass NULL to ignore
489 * match func data - pass -1 to ignore
490 * Returns closest match.
492 static struct sock *iso_get_sock_listen(bdaddr_t *src, bdaddr_t *dst,
493 iso_sock_match_t match, void *data)
495 struct sock *sk = NULL, *sk1 = NULL;
497 read_lock(&iso_sk_list.lock);
499 sk_for_each(sk, &iso_sk_list.head) {
500 if (sk->sk_state != BT_LISTEN)
503 /* Match Broadcast destination */
504 if (bacmp(dst, BDADDR_ANY) && bacmp(&iso_pi(sk)->dst, dst))
507 /* Use Match function if provided */
508 if (match && !match(sk, data))
512 if (!bacmp(&iso_pi(sk)->src, src))
516 if (!bacmp(&iso_pi(sk)->src, BDADDR_ANY))
520 read_unlock(&iso_sk_list.lock);
522 return sk ? sk : sk1;
525 static void iso_sock_destruct(struct sock *sk)
529 skb_queue_purge(&sk->sk_receive_queue);
530 skb_queue_purge(&sk->sk_write_queue);
533 static void iso_sock_cleanup_listen(struct sock *parent)
537 BT_DBG("parent %p", parent);
539 /* Close not yet accepted channels */
540 while ((sk = bt_accept_dequeue(parent, NULL))) {
545 parent->sk_state = BT_CLOSED;
546 sock_set_flag(parent, SOCK_ZAPPED);
549 /* Kill socket (only if zapped and orphan)
550 * Must be called on unlocked socket.
552 static void iso_sock_kill(struct sock *sk)
554 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||
555 sock_flag(sk, SOCK_DEAD))
558 BT_DBG("sk %p state %d", sk, sk->sk_state);
560 /* Kill poor orphan */
561 bt_sock_unlink(&iso_sk_list, sk);
562 sock_set_flag(sk, SOCK_DEAD);
566 static void iso_conn_defer_reject(struct hci_conn *conn)
568 struct hci_cp_le_reject_cis cp;
570 BT_DBG("conn %p", conn);
572 memset(&cp, 0, sizeof(cp));
573 cp.handle = cpu_to_le16(conn->handle);
574 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
575 hci_send_cmd(conn->hdev, HCI_OP_LE_REJECT_CIS, sizeof(cp), &cp);
578 static void __iso_sock_close(struct sock *sk)
580 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
582 switch (sk->sk_state) {
584 iso_sock_cleanup_listen(sk);
589 if (iso_pi(sk)->conn->hcon) {
590 sk->sk_state = BT_DISCONN;
591 iso_sock_set_timer(sk, ISO_DISCONN_TIMEOUT);
592 iso_conn_lock(iso_pi(sk)->conn);
593 hci_conn_drop(iso_pi(sk)->conn->hcon);
594 iso_pi(sk)->conn->hcon = NULL;
595 iso_conn_unlock(iso_pi(sk)->conn);
597 iso_chan_del(sk, ECONNRESET);
602 if (iso_pi(sk)->conn->hcon)
603 iso_conn_defer_reject(iso_pi(sk)->conn->hcon);
604 iso_chan_del(sk, ECONNRESET);
607 /* In case of DEFER_SETUP the hcon would be bound to CIG which
608 * needs to be removed so just call hci_conn_del so the cleanup
609 * callback do what is needed.
611 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags) &&
612 iso_pi(sk)->conn->hcon) {
613 hci_conn_del(iso_pi(sk)->conn->hcon);
614 iso_pi(sk)->conn->hcon = NULL;
617 iso_chan_del(sk, ECONNRESET);
620 iso_chan_del(sk, ECONNRESET);
624 sock_set_flag(sk, SOCK_ZAPPED);
629 /* Must be called on unlocked socket. */
630 static void iso_sock_close(struct sock *sk)
632 iso_sock_clear_timer(sk);
634 __iso_sock_close(sk);
639 static void iso_sock_init(struct sock *sk, struct sock *parent)
644 sk->sk_type = parent->sk_type;
645 bt_sk(sk)->flags = bt_sk(parent)->flags;
646 security_sk_clone(parent, sk);
650 static struct proto iso_proto = {
652 .owner = THIS_MODULE,
653 .obj_size = sizeof(struct iso_pinfo)
656 #define DEFAULT_IO_QOS \
658 .interval = 10000u, \
661 .phy = BT_ISO_PHY_2M, \
665 static struct bt_iso_qos default_qos = {
666 .cig = BT_ISO_QOS_CIG_UNSET,
667 .cis = BT_ISO_QOS_CIS_UNSET,
671 .in = DEFAULT_IO_QOS,
672 .out = DEFAULT_IO_QOS,
675 static struct sock *iso_sock_alloc(struct net *net, struct socket *sock,
676 int proto, gfp_t prio, int kern)
680 sk = sk_alloc(net, PF_BLUETOOTH, prio, &iso_proto, kern);
684 sock_init_data(sock, sk);
685 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
687 sk->sk_destruct = iso_sock_destruct;
688 sk->sk_sndtimeo = ISO_CONN_TIMEOUT;
690 sock_reset_flag(sk, SOCK_ZAPPED);
692 sk->sk_protocol = proto;
693 sk->sk_state = BT_OPEN;
695 /* Set address type as public as default src address is BDADDR_ANY */
696 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC;
698 iso_pi(sk)->qos = default_qos;
700 bt_sock_link(&iso_sk_list, sk);
704 static int iso_sock_create(struct net *net, struct socket *sock, int protocol,
709 BT_DBG("sock %p", sock);
711 sock->state = SS_UNCONNECTED;
713 if (sock->type != SOCK_SEQPACKET)
714 return -ESOCKTNOSUPPORT;
716 sock->ops = &iso_sock_ops;
718 sk = iso_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
722 iso_sock_init(sk, NULL);
726 static int iso_sock_bind_bc(struct socket *sock, struct sockaddr *addr,
729 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
730 struct sock *sk = sock->sk;
733 BT_DBG("sk %p bc_sid %u bc_num_bis %u", sk, sa->iso_bc->bc_sid,
734 sa->iso_bc->bc_num_bis);
736 if (addr_len > sizeof(*sa) + sizeof(*sa->iso_bc) ||
737 sa->iso_bc->bc_num_bis < 0x01 || sa->iso_bc->bc_num_bis > 0x1f)
740 bacpy(&iso_pi(sk)->dst, &sa->iso_bc->bc_bdaddr);
741 iso_pi(sk)->dst_type = sa->iso_bc->bc_bdaddr_type;
742 iso_pi(sk)->sync_handle = -1;
743 iso_pi(sk)->bc_sid = sa->iso_bc->bc_sid;
744 iso_pi(sk)->bc_num_bis = sa->iso_bc->bc_num_bis;
746 for (i = 0; i < iso_pi(sk)->bc_num_bis; i++) {
747 if (sa->iso_bc->bc_bis[i] < 0x01 ||
748 sa->iso_bc->bc_bis[i] > 0x1f)
751 memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis,
752 iso_pi(sk)->bc_num_bis);
758 static int iso_sock_bind(struct socket *sock, struct sockaddr *addr,
761 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
762 struct sock *sk = sock->sk;
765 BT_DBG("sk %p %pMR type %u", sk, &sa->iso_bdaddr, sa->iso_bdaddr_type);
767 if (!addr || addr_len < sizeof(struct sockaddr_iso) ||
768 addr->sa_family != AF_BLUETOOTH)
773 if (sk->sk_state != BT_OPEN) {
778 if (sk->sk_type != SOCK_SEQPACKET) {
783 /* Check if the address type is of LE type */
784 if (!bdaddr_type_is_le(sa->iso_bdaddr_type)) {
789 bacpy(&iso_pi(sk)->src, &sa->iso_bdaddr);
790 iso_pi(sk)->src_type = sa->iso_bdaddr_type;
792 /* Check for Broadcast address */
793 if (addr_len > sizeof(*sa)) {
794 err = iso_sock_bind_bc(sock, addr, addr_len);
799 sk->sk_state = BT_BOUND;
806 static int iso_sock_connect(struct socket *sock, struct sockaddr *addr,
809 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
810 struct sock *sk = sock->sk;
815 if (alen < sizeof(struct sockaddr_iso) ||
816 addr->sa_family != AF_BLUETOOTH)
819 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
822 if (sk->sk_type != SOCK_SEQPACKET)
825 /* Check if the address type is of LE type */
826 if (!bdaddr_type_is_le(sa->iso_bdaddr_type))
831 bacpy(&iso_pi(sk)->dst, &sa->iso_bdaddr);
832 iso_pi(sk)->dst_type = sa->iso_bdaddr_type;
834 if (bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
835 err = iso_connect_cis(sk);
837 err = iso_connect_bis(sk);
842 if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
843 err = bt_sock_wait_state(sk, BT_CONNECTED,
844 sock_sndtimeo(sk, flags & O_NONBLOCK));
852 static int iso_listen_bis(struct sock *sk)
854 struct hci_dev *hdev;
857 BT_DBG("%pMR -> %pMR (SID 0x%2.2x)", &iso_pi(sk)->src,
858 &iso_pi(sk)->dst, iso_pi(sk)->bc_sid);
860 write_lock(&iso_sk_list.lock);
862 if (__iso_get_sock_listen_by_sid(&iso_pi(sk)->src, &iso_pi(sk)->dst,
866 write_unlock(&iso_sk_list.lock);
871 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
872 iso_pi(sk)->src_type);
874 return -EHOSTUNREACH;
878 err = hci_pa_create_sync(hdev, &iso_pi(sk)->dst, iso_pi(sk)->dst_type,
881 hci_dev_unlock(hdev);
887 static int iso_listen_cis(struct sock *sk)
891 BT_DBG("%pMR", &iso_pi(sk)->src);
893 write_lock(&iso_sk_list.lock);
895 if (__iso_get_sock_listen_by_addr(&iso_pi(sk)->src))
898 write_unlock(&iso_sk_list.lock);
903 static int iso_sock_listen(struct socket *sock, int backlog)
905 struct sock *sk = sock->sk;
908 BT_DBG("sk %p backlog %d", sk, backlog);
912 if (sk->sk_state != BT_BOUND) {
917 if (sk->sk_type != SOCK_SEQPACKET) {
922 if (!bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
923 err = iso_listen_cis(sk);
925 err = iso_listen_bis(sk);
930 sk->sk_max_ack_backlog = backlog;
931 sk->sk_ack_backlog = 0;
933 sk->sk_state = BT_LISTEN;
940 static int iso_sock_accept(struct socket *sock, struct socket *newsock,
941 int flags, bool kern)
943 DEFINE_WAIT_FUNC(wait, woken_wake_function);
944 struct sock *sk = sock->sk, *ch;
950 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
952 BT_DBG("sk %p timeo %ld", sk, timeo);
954 /* Wait for an incoming connection. (wake-one). */
955 add_wait_queue_exclusive(sk_sleep(sk), &wait);
957 if (sk->sk_state != BT_LISTEN) {
962 ch = bt_accept_dequeue(sk, newsock);
971 if (signal_pending(current)) {
972 err = sock_intr_errno(timeo);
978 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
981 remove_wait_queue(sk_sleep(sk), &wait);
986 newsock->state = SS_CONNECTED;
988 BT_DBG("new socket %p", ch);
995 static int iso_sock_getname(struct socket *sock, struct sockaddr *addr,
998 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
999 struct sock *sk = sock->sk;
1001 BT_DBG("sock %p, sk %p", sock, sk);
1003 addr->sa_family = AF_BLUETOOTH;
1006 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->dst);
1007 sa->iso_bdaddr_type = iso_pi(sk)->dst_type;
1009 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->src);
1010 sa->iso_bdaddr_type = iso_pi(sk)->src_type;
1013 return sizeof(struct sockaddr_iso);
1016 static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1019 struct sock *sk = sock->sk;
1020 struct iso_conn *conn = iso_pi(sk)->conn;
1021 struct sk_buff *skb, **frag;
1024 BT_DBG("sock %p, sk %p", sock, sk);
1026 err = sock_error(sk);
1030 if (msg->msg_flags & MSG_OOB)
1033 if (sk->sk_state != BT_CONNECTED)
1036 skb = bt_skb_sendmsg(sk, msg, len, conn->hcon->hdev->iso_mtu,
1037 HCI_ISO_DATA_HDR_SIZE, 0);
1039 return PTR_ERR(skb);
1043 BT_DBG("skb %p len %d", sk, skb->len);
1045 /* Continuation fragments */
1046 frag = &skb_shinfo(skb)->frag_list;
1048 struct sk_buff *tmp;
1050 tmp = bt_skb_sendmsg(sk, msg, len, conn->hcon->hdev->iso_mtu,
1054 return PTR_ERR(tmp);
1061 skb->len += tmp->len;
1062 skb->data_len += tmp->len;
1064 BT_DBG("frag %p len %d", *frag, tmp->len);
1066 frag = &(*frag)->next;
1071 if (sk->sk_state == BT_CONNECTED)
1072 err = iso_send_frame(sk, skb);
1083 static void iso_conn_defer_accept(struct hci_conn *conn)
1085 struct hci_cp_le_accept_cis cp;
1086 struct hci_dev *hdev = conn->hdev;
1088 BT_DBG("conn %p", conn);
1090 conn->state = BT_CONFIG;
1092 cp.handle = cpu_to_le16(conn->handle);
1094 hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp);
1097 static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1098 size_t len, int flags)
1100 struct sock *sk = sock->sk;
1101 struct iso_pinfo *pi = iso_pi(sk);
1104 BT_DBG("sk %p", sk);
1108 if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
1109 switch (sk->sk_state) {
1111 iso_conn_defer_accept(pi->conn->hcon);
1112 sk->sk_state = BT_CONFIG;
1116 err = iso_connect_cis(sk);
1124 return bt_sock_recvmsg(sock, msg, len, flags);
1127 static bool check_io_qos(struct bt_iso_io_qos *qos)
1129 /* If no PHY is enable SDU must be 0 */
1130 if (!qos->phy && qos->sdu)
1133 if (qos->interval && (qos->interval < 0xff || qos->interval > 0xfffff))
1136 if (qos->latency && (qos->latency < 0x05 || qos->latency > 0xfa0))
1139 if (qos->phy > BT_ISO_PHY_ANY)
1145 static bool check_qos(struct bt_iso_qos *qos)
1147 if (qos->sca > 0x07)
1150 if (qos->packing > 0x01)
1153 if (qos->framing > 0x01)
1156 if (!check_io_qos(&qos->in))
1159 if (!check_io_qos(&qos->out))
1165 static int iso_sock_setsockopt(struct socket *sock, int level, int optname,
1166 sockptr_t optval, unsigned int optlen)
1168 struct sock *sk = sock->sk;
1170 struct bt_iso_qos qos;
1173 BT_DBG("sk %p", sk);
1178 case BT_DEFER_SETUP:
1179 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1184 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
1190 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1192 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1196 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1197 sk->sk_state != BT_CONNECT2) {
1202 len = min_t(unsigned int, sizeof(qos), optlen);
1203 if (len != sizeof(qos)) {
1208 memset(&qos, 0, sizeof(qos));
1210 if (copy_from_sockptr(&qos, optval, len)) {
1215 if (!check_qos(&qos)) {
1220 iso_pi(sk)->qos = qos;
1225 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1226 sk->sk_state != BT_CONNECT2) {
1231 if (optlen > sizeof(iso_pi(sk)->base)) {
1236 len = min_t(unsigned int, sizeof(iso_pi(sk)->base), optlen);
1238 if (copy_from_sockptr(iso_pi(sk)->base, optval, len)) {
1243 iso_pi(sk)->base_len = len;
1256 static int iso_sock_getsockopt(struct socket *sock, int level, int optname,
1257 char __user *optval, int __user *optlen)
1259 struct sock *sk = sock->sk;
1261 struct bt_iso_qos *qos;
1265 BT_DBG("sk %p", sk);
1267 if (get_user(len, optlen))
1273 case BT_DEFER_SETUP:
1274 if (sk->sk_state == BT_CONNECTED) {
1279 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1280 (u32 __user *)optval))
1286 qos = iso_sock_get_qos(sk);
1288 len = min_t(unsigned int, len, sizeof(*qos));
1289 if (copy_to_user(optval, qos, len))
1295 if (sk->sk_state == BT_CONNECTED) {
1296 base_len = iso_pi(sk)->conn->hcon->le_per_adv_data_len;
1297 base = iso_pi(sk)->conn->hcon->le_per_adv_data;
1299 base_len = iso_pi(sk)->base_len;
1300 base = iso_pi(sk)->base;
1303 len = min_t(unsigned int, len, base_len);
1304 if (copy_to_user(optval, base, len))
1318 static int iso_sock_shutdown(struct socket *sock, int how)
1320 struct sock *sk = sock->sk;
1323 BT_DBG("sock %p, sk %p, how %d", sock, sk, how);
1333 if (sk->sk_shutdown & RCV_SHUTDOWN)
1335 sk->sk_shutdown |= RCV_SHUTDOWN;
1338 if (sk->sk_shutdown & SEND_SHUTDOWN)
1340 sk->sk_shutdown |= SEND_SHUTDOWN;
1343 if (sk->sk_shutdown & SHUTDOWN_MASK)
1345 sk->sk_shutdown |= SHUTDOWN_MASK;
1349 iso_sock_clear_timer(sk);
1350 __iso_sock_close(sk);
1352 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1353 !(current->flags & PF_EXITING))
1354 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1363 static int iso_sock_release(struct socket *sock)
1365 struct sock *sk = sock->sk;
1368 BT_DBG("sock %p, sk %p", sock, sk);
1375 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1376 !(current->flags & PF_EXITING)) {
1378 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1387 static void iso_sock_ready(struct sock *sk)
1389 BT_DBG("sk %p", sk);
1395 iso_sock_clear_timer(sk);
1396 sk->sk_state = BT_CONNECTED;
1397 sk->sk_state_change(sk);
1401 struct iso_list_data {
1402 struct hci_conn *hcon;
1406 static bool iso_match_big(struct sock *sk, void *data)
1408 struct hci_evt_le_big_sync_estabilished *ev = data;
1410 return ev->handle == iso_pi(sk)->qos.big;
1413 static void iso_conn_ready(struct iso_conn *conn)
1415 struct sock *parent;
1416 struct sock *sk = conn->sk;
1417 struct hci_ev_le_big_sync_estabilished *ev;
1419 BT_DBG("conn %p", conn);
1422 iso_sock_ready(conn->sk);
1424 iso_conn_lock(conn);
1427 iso_conn_unlock(conn);
1431 ev = hci_recv_event_data(conn->hcon->hdev,
1432 HCI_EVT_LE_BIG_SYNC_ESTABILISHED);
1434 parent = iso_get_sock_listen(&conn->hcon->src,
1438 parent = iso_get_sock_listen(&conn->hcon->src,
1439 BDADDR_ANY, NULL, NULL);
1442 iso_conn_unlock(conn);
1448 sk = iso_sock_alloc(sock_net(parent), NULL,
1449 BTPROTO_ISO, GFP_ATOMIC, 0);
1451 release_sock(parent);
1452 iso_conn_unlock(conn);
1456 iso_sock_init(sk, parent);
1458 bacpy(&iso_pi(sk)->src, &conn->hcon->src);
1459 iso_pi(sk)->src_type = conn->hcon->src_type;
1461 /* If hcon has no destination address (BDADDR_ANY) it means it
1462 * was created by HCI_EV_LE_BIG_SYNC_ESTABILISHED so we need to
1463 * initialize using the parent socket destination address.
1465 if (!bacmp(&conn->hcon->dst, BDADDR_ANY)) {
1466 bacpy(&conn->hcon->dst, &iso_pi(parent)->dst);
1467 conn->hcon->dst_type = iso_pi(parent)->dst_type;
1468 conn->hcon->sync_handle = iso_pi(parent)->sync_handle;
1471 bacpy(&iso_pi(sk)->dst, &conn->hcon->dst);
1472 iso_pi(sk)->dst_type = conn->hcon->dst_type;
1474 hci_conn_hold(conn->hcon);
1475 __iso_chan_add(conn, sk, parent);
1477 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1478 sk->sk_state = BT_CONNECT2;
1480 sk->sk_state = BT_CONNECTED;
1482 /* Wake up parent */
1483 parent->sk_data_ready(parent);
1485 release_sock(parent);
1487 iso_conn_unlock(conn);
1491 static bool iso_match_sid(struct sock *sk, void *data)
1493 struct hci_ev_le_pa_sync_established *ev = data;
1495 return ev->sid == iso_pi(sk)->bc_sid;
1498 static bool iso_match_sync_handle(struct sock *sk, void *data)
1500 struct hci_evt_le_big_info_adv_report *ev = data;
1502 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle;
1505 /* ----- ISO interface with lower layer (HCI) ----- */
1507 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1509 struct hci_ev_le_pa_sync_established *ev1;
1510 struct hci_evt_le_big_info_adv_report *ev2;
1514 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
1516 /* Broadcast receiver requires handling of some events before it can
1517 * proceed to establishing a BIG sync:
1519 * 1. HCI_EV_LE_PA_SYNC_ESTABLISHED: The socket may specify a specific
1520 * SID to listen to and once sync is estabilished its handle needs to
1521 * be stored in iso_pi(sk)->sync_handle so it can be matched once
1522 * receiving the BIG Info.
1523 * 2. HCI_EVT_LE_BIG_INFO_ADV_REPORT: When connect_ind is triggered by a
1524 * a BIG Info it attempts to check if there any listening socket with
1525 * the same sync_handle and if it does then attempt to create a sync.
1527 ev1 = hci_recv_event_data(hdev, HCI_EV_LE_PA_SYNC_ESTABLISHED);
1529 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr, iso_match_sid,
1532 iso_pi(sk)->sync_handle = le16_to_cpu(ev1->handle);
1537 ev2 = hci_recv_event_data(hdev, HCI_EVT_LE_BIG_INFO_ADV_REPORT);
1539 sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr,
1540 iso_match_sync_handle, ev2);
1544 if (ev2->num_bis < iso_pi(sk)->bc_num_bis)
1545 iso_pi(sk)->bc_num_bis = ev2->num_bis;
1547 err = hci_le_big_create_sync(hdev,
1549 iso_pi(sk)->sync_handle,
1550 iso_pi(sk)->bc_num_bis,
1551 iso_pi(sk)->bc_bis);
1553 bt_dev_err(hdev, "hci_le_big_create_sync: %d",
1559 sk = iso_get_sock_listen(&hdev->bdaddr, BDADDR_ANY, NULL, NULL);
1566 lm |= HCI_LM_ACCEPT;
1568 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1569 *flags |= HCI_PROTO_DEFER;
1574 static void iso_connect_cfm(struct hci_conn *hcon, __u8 status)
1576 if (hcon->type != ISO_LINK) {
1577 if (hcon->type != LE_LINK)
1580 /* Check if LE link has failed */
1583 iso_conn_del(hcon->link, bt_to_errno(status));
1587 /* Create CIS if pending */
1588 hci_le_create_cis(hcon);
1592 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
1595 struct iso_conn *conn;
1597 conn = iso_conn_add(hcon);
1599 iso_conn_ready(conn);
1601 iso_conn_del(hcon, bt_to_errno(status));
1605 static void iso_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1607 if (hcon->type != ISO_LINK)
1610 BT_DBG("hcon %p reason %d", hcon, reason);
1612 iso_conn_del(hcon, bt_to_errno(reason));
1615 void iso_recv(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
1617 struct iso_conn *conn = hcon->iso_data;
1618 struct hci_iso_data_hdr *hdr;
1624 pb = hci_iso_flags_pb(flags);
1625 ts = hci_iso_flags_ts(flags);
1627 BT_DBG("conn %p len %d pb 0x%x ts 0x%x", conn, skb->len, pb, ts);
1633 BT_ERR("Unexpected start frame (len %d)", skb->len);
1634 kfree_skb(conn->rx_skb);
1635 conn->rx_skb = NULL;
1640 /* TODO: add timestamp to the packet? */
1641 hdr = skb_pull_data(skb, HCI_ISO_TS_DATA_HDR_SIZE);
1643 BT_ERR("Frame is too short (len %d)", skb->len);
1648 hdr = skb_pull_data(skb, HCI_ISO_DATA_HDR_SIZE);
1650 BT_ERR("Frame is too short (len %d)", skb->len);
1655 len = __le16_to_cpu(hdr->slen);
1656 flags = hci_iso_data_flags(len);
1657 len = hci_iso_data_len(len);
1659 BT_DBG("Start: total len %d, frag len %d flags 0x%4.4x", len,
1662 if (len == skb->len) {
1663 /* Complete frame received */
1664 iso_recv_frame(conn, skb);
1668 if (pb == ISO_SINGLE) {
1669 BT_ERR("Frame malformed (len %d, expected len %d)",
1674 if (skb->len > len) {
1675 BT_ERR("Frame is too long (len %d, expected len %d)",
1680 /* Allocate skb for the complete frame (with header) */
1681 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
1685 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1687 conn->rx_len = len - skb->len;
1691 BT_DBG("Cont: frag len %d (expecting %d)", skb->len,
1694 if (!conn->rx_len) {
1695 BT_ERR("Unexpected continuation frame (len %d)",
1700 if (skb->len > conn->rx_len) {
1701 BT_ERR("Fragment is too long (len %d, expected %d)",
1702 skb->len, conn->rx_len);
1703 kfree_skb(conn->rx_skb);
1704 conn->rx_skb = NULL;
1709 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1711 conn->rx_len -= skb->len;
1715 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1717 conn->rx_len -= skb->len;
1719 if (!conn->rx_len) {
1720 struct sk_buff *rx_skb = conn->rx_skb;
1722 /* Complete frame received. iso_recv_frame
1723 * takes ownership of the skb so set the global
1724 * rx_skb pointer to NULL first.
1726 conn->rx_skb = NULL;
1727 iso_recv_frame(conn, rx_skb);
1736 static struct hci_cb iso_cb = {
1738 .connect_cfm = iso_connect_cfm,
1739 .disconn_cfm = iso_disconn_cfm,
1742 static int iso_debugfs_show(struct seq_file *f, void *p)
1746 read_lock(&iso_sk_list.lock);
1748 sk_for_each(sk, &iso_sk_list.head) {
1749 seq_printf(f, "%pMR %pMR %d\n", &iso_pi(sk)->src,
1750 &iso_pi(sk)->dst, sk->sk_state);
1753 read_unlock(&iso_sk_list.lock);
1758 DEFINE_SHOW_ATTRIBUTE(iso_debugfs);
1760 static struct dentry *iso_debugfs;
1762 static const struct proto_ops iso_sock_ops = {
1763 .family = PF_BLUETOOTH,
1764 .owner = THIS_MODULE,
1765 .release = iso_sock_release,
1766 .bind = iso_sock_bind,
1767 .connect = iso_sock_connect,
1768 .listen = iso_sock_listen,
1769 .accept = iso_sock_accept,
1770 .getname = iso_sock_getname,
1771 .sendmsg = iso_sock_sendmsg,
1772 .recvmsg = iso_sock_recvmsg,
1773 .poll = bt_sock_poll,
1774 .ioctl = bt_sock_ioctl,
1775 .mmap = sock_no_mmap,
1776 .socketpair = sock_no_socketpair,
1777 .shutdown = iso_sock_shutdown,
1778 .setsockopt = iso_sock_setsockopt,
1779 .getsockopt = iso_sock_getsockopt
1782 static const struct net_proto_family iso_sock_family_ops = {
1783 .family = PF_BLUETOOTH,
1784 .owner = THIS_MODULE,
1785 .create = iso_sock_create,
1788 static bool iso_inited;
1790 bool iso_enabled(void)
1799 BUILD_BUG_ON(sizeof(struct sockaddr_iso) > sizeof(struct sockaddr));
1804 err = proto_register(&iso_proto, 0);
1808 err = bt_sock_register(BTPROTO_ISO, &iso_sock_family_ops);
1810 BT_ERR("ISO socket registration failed");
1814 err = bt_procfs_init(&init_net, "iso", &iso_sk_list, NULL);
1816 BT_ERR("Failed to create ISO proc file");
1817 bt_sock_unregister(BTPROTO_ISO);
1821 BT_INFO("ISO socket layer initialized");
1823 hci_register_cb(&iso_cb);
1825 if (IS_ERR_OR_NULL(bt_debugfs))
1829 iso_debugfs = debugfs_create_file("iso", 0444, bt_debugfs,
1830 NULL, &iso_debugfs_fops);
1838 proto_unregister(&iso_proto);
1847 bt_procfs_cleanup(&init_net, "iso");
1849 debugfs_remove(iso_debugfs);
1852 hci_unregister_cb(&iso_cb);
1854 bt_sock_unregister(BTPROTO_ISO);
1856 proto_unregister(&iso_proto);