^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) BlueZ - Bluetooth protocol stack for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Copyright (C) 2000-2001 Qualcomm Incorporated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) it under the terms of the GNU General Public License version 2 as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) published by the Free Software Foundation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) SOFTWARE IS DISCLAIMED.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* Bluetooth SCO sockets. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <net/bluetooth/bluetooth.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <net/bluetooth/hci_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <net/bluetooth/sco.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static bool disable_esco;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static const struct proto_ops sco_sock_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static struct bt_sock_list sco_sk_list = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* ---- SCO connections ---- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct sco_conn {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct hci_conn *hcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct delayed_work timeout_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define sco_conn_lock(c) spin_lock(&c->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define sco_conn_unlock(c) spin_unlock(&c->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static void sco_sock_close(struct sock *sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static void sco_sock_kill(struct sock *sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* ----- SCO socket info ----- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define sco_pi(sk) ((struct sco_pinfo *) sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct sco_pinfo {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct bt_sock bt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) bdaddr_t src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) bdaddr_t dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) __u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) __u16 setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) __u8 cmsg_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct sco_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* ---- SCO timers ---- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define SCO_CONN_TIMEOUT (HZ * 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define SCO_DISCONN_TIMEOUT (HZ * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static void sco_sock_timeout(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct sco_conn *conn = container_of(work, struct sco_conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) timeout_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) sco_conn_lock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) sk = conn->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) sco_conn_unlock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) BT_DBG("sock %p state %d", sk, sk->sk_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) sk->sk_err = ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) sk->sk_state_change(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static void sco_sock_set_timer(struct sock *sk, long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!sco_pi(sk)->conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) schedule_delayed_work(&sco_pi(sk)->conn->timeout_work, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static void sco_sock_clear_timer(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (!sco_pi(sk)->conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) BT_DBG("sock %p state %d", sk, sk->sk_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* ---- SCO connections ---- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct hci_dev *hdev = hcon->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct sco_conn *conn = hcon->sco_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) spin_lock_init(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) INIT_DELAYED_WORK(&conn->timeout_work, sco_sock_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) hcon->sco_data = conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) conn->hcon = hcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (hdev->sco_mtu > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) conn->mtu = hdev->sco_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) conn->mtu = 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) BT_DBG("hcon %p conn %p", hcon, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* Delete channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * Must be called on the locked socket. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static void sco_chan_del(struct sock *sk, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct sco_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) conn = sco_pi(sk)->conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) sco_conn_lock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) conn->sk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) sco_pi(sk)->conn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) sco_conn_unlock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (conn->hcon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) hci_conn_drop(conn->hcon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) sk->sk_state = BT_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) sk->sk_err = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) sk->sk_state_change(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) sock_set_flag(sk, SOCK_ZAPPED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static void sco_conn_del(struct hci_conn *hcon, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct sco_conn *conn = hcon->sco_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* Kill socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) sco_conn_lock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) sk = conn->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) sco_conn_unlock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) sco_sock_clear_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) sco_chan_del(sk, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* Ensure no more work items will run before freeing conn. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) cancel_delayed_work_sync(&conn->timeout_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) hcon->sco_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) kfree(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static void __sco_chan_add(struct sco_conn *conn, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct sock *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) BT_DBG("conn %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) sco_pi(sk)->conn = conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) conn->sk = sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) bt_accept_enqueue(parent, sk, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct sock *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) sco_conn_lock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (conn->sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) __sco_chan_add(conn, sk, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) sco_conn_unlock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static int sco_connect(struct hci_dev *hdev, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) struct sco_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct hci_conn *hcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int err, type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (lmp_esco_capable(hdev) && !disable_esco)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) type = ESCO_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) type = SCO_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) sco_pi(sk)->setting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (IS_ERR(hcon))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return PTR_ERR(hcon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) conn = sco_conn_add(hcon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (!conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) hci_conn_drop(hcon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /* Update source addr of the socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) bacpy(&sco_pi(sk)->src, &hcon->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) err = sco_chan_add(conn, sk, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (hcon->state == BT_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) sco_sock_clear_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) sk->sk_state = BT_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) sk->sk_state = BT_CONNECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) sco_sock_set_timer(sk, sk->sk_sndtimeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int sco_send_frame(struct sock *sk, void *buf, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) unsigned int msg_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct sco_conn *conn = sco_pi(sk)->conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /* Check outgoing MTU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (len > conn->mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) BT_DBG("sk %p len %d", sk, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) skb = bt_skb_send_alloc(sk, len, msg_flags & MSG_DONTWAIT, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) memcpy(skb_put(skb, len), buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) hci_send_sco(conn->hcon, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) sco_conn_lock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) sk = conn->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) sco_conn_unlock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) BT_DBG("sk %p len %d", sk, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (sk->sk_state != BT_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (!sock_queue_rcv_skb(sk, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /* -------- Socket interface ---------- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) sk_for_each(sk, &sco_sk_list.head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (sk->sk_state != BT_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (!bacmp(&sco_pi(sk)->src, ba))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /* Find socket listening on source bdaddr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * Returns closest match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static struct sock *sco_get_sock_listen(bdaddr_t *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct sock *sk = NULL, *sk1 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) read_lock(&sco_sk_list.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) sk_for_each(sk, &sco_sk_list.head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (sk->sk_state != BT_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /* Exact match. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (!bacmp(&sco_pi(sk)->src, src))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /* Closest match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) sk1 = sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) read_unlock(&sco_sk_list.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return sk ? sk : sk1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static void sco_sock_destruct(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) BT_DBG("sk %p", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) skb_queue_purge(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) skb_queue_purge(&sk->sk_write_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static void sco_sock_cleanup_listen(struct sock *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) BT_DBG("parent %p", parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /* Close not yet accepted channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) while ((sk = bt_accept_dequeue(parent, NULL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) sco_sock_close(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) sco_sock_kill(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) parent->sk_state = BT_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) sock_set_flag(parent, SOCK_ZAPPED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) /* Kill socket (only if zapped and orphan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * Must be called on unlocked socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) static void sco_sock_kill(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) BT_DBG("sk %p state %d", sk, sk->sk_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /* Kill poor orphan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) bt_sock_unlink(&sco_sk_list, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) sock_set_flag(sk, SOCK_DEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static void __sco_sock_close(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) switch (sk->sk_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) case BT_LISTEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) sco_sock_cleanup_listen(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) case BT_CONNECTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) case BT_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (sco_pi(sk)->conn->hcon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) sk->sk_state = BT_DISCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) sco_conn_lock(sco_pi(sk)->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) hci_conn_drop(sco_pi(sk)->conn->hcon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) sco_pi(sk)->conn->hcon = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) sco_conn_unlock(sco_pi(sk)->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) sco_chan_del(sk, ECONNRESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) case BT_CONNECT2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) case BT_CONNECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) case BT_DISCONN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) sco_chan_del(sk, ECONNRESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) sock_set_flag(sk, SOCK_ZAPPED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) /* Must be called on unlocked socket. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static void sco_sock_close(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) sco_sock_clear_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) __sco_sock_close(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static void sco_skb_put_cmsg(struct sk_buff *skb, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) put_cmsg(msg, SOL_BLUETOOTH, BT_SCM_PKT_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) sizeof(bt_cb(skb)->sco.pkt_status),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) &bt_cb(skb)->sco.pkt_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) static void sco_sock_init(struct sock *sk, struct sock *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) BT_DBG("sk %p", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) sk->sk_type = parent->sk_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) bt_sk(sk)->flags = bt_sk(parent)->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) security_sk_clone(parent, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) bt_sk(sk)->skb_put_cmsg = sco_skb_put_cmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static struct proto sco_proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) .name = "SCO",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) .obj_size = sizeof(struct sco_pinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static struct sock *sco_sock_alloc(struct net *net, struct socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int proto, gfp_t prio, int kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) sock_init_data(sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) sk->sk_destruct = sco_sock_destruct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) sock_reset_flag(sk, SOCK_ZAPPED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) sk->sk_protocol = proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) sk->sk_state = BT_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) bt_sock_link(&sco_sk_list, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) return sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) int kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) BT_DBG("sock %p", sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) sock->state = SS_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (sock->type != SOCK_SEQPACKET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) return -ESOCKTNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) sock->ops = &sco_sock_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) sco_sock_init(sk, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static int sco_sock_bind(struct socket *sock, struct sockaddr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) int addr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (!addr || addr_len < sizeof(struct sockaddr_sco) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) addr->sa_family != AF_BLUETOOTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (sk->sk_state != BT_OPEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) err = -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (sk->sk_type != SOCK_SEQPACKET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) sk->sk_state = BT_BOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) struct hci_dev *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) BT_DBG("sk %p", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (alen < sizeof(struct sockaddr_sco) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) addr->sa_family != AF_BLUETOOTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) return -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (sk->sk_type != SOCK_SEQPACKET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) hdev = hci_get_route(&sa->sco_bdaddr, &sco_pi(sk)->src, BDADDR_BREDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (!hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return -EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) hci_dev_lock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) /* Set destination address and psm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) err = sco_connect(hdev, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) hci_dev_unlock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) hci_dev_put(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) err = bt_sock_wait_state(sk, BT_CONNECTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) sock_sndtimeo(sk, flags & O_NONBLOCK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) static int sco_sock_listen(struct socket *sock, int backlog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) bdaddr_t *src = &sco_pi(sk)->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) BT_DBG("sk %p backlog %d", sk, backlog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if (sk->sk_state != BT_BOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) err = -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) if (sk->sk_type != SOCK_SEQPACKET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) write_lock(&sco_sk_list.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (__sco_get_sock_listen_by_addr(src)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) err = -EADDRINUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) sk->sk_max_ack_backlog = backlog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) sk->sk_ack_backlog = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) sk->sk_state = BT_LISTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) write_unlock(&sco_sk_list.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) static int sco_sock_accept(struct socket *sock, struct socket *newsock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) int flags, bool kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) DEFINE_WAIT_FUNC(wait, woken_wake_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) struct sock *sk = sock->sk, *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) long timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) BT_DBG("sk %p timeo %ld", sk, timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) /* Wait for an incoming connection. (wake-one). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) add_wait_queue_exclusive(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (sk->sk_state != BT_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) err = -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) ch = bt_accept_dequeue(sk, newsock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) if (ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (!timeo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) err = sock_intr_errno(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) remove_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) newsock->state = SS_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) BT_DBG("new socket %p", ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) static int sco_sock_getname(struct socket *sock, struct sockaddr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) int peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) BT_DBG("sock %p, sk %p", sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) addr->sa_family = AF_BLUETOOTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) if (peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) return sizeof(struct sockaddr_sco);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) BT_DBG("sock %p, sk %p", sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) err = sock_error(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (msg->msg_flags & MSG_OOB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) buf = kmalloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) if (memcpy_from_msg(buf, msg, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (sk->sk_state == BT_CONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) err = sco_send_frame(sk, buf, len, msg->msg_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) err = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) struct hci_dev *hdev = conn->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) BT_DBG("conn %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) conn->state = BT_CONFIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) if (!lmp_esco_capable(hdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) struct hci_cp_accept_conn_req cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) bacpy(&cp.bdaddr, &conn->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) cp.role = 0x00; /* Ignored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) struct hci_cp_accept_sync_conn_req cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) bacpy(&cp.bdaddr, &conn->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) cp.pkt_type = cpu_to_le16(conn->pkt_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) cp.tx_bandwidth = cpu_to_le32(0x00001f40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) cp.rx_bandwidth = cpu_to_le32(0x00001f40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) cp.content_format = cpu_to_le16(setting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) switch (setting & SCO_AIRMODE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) case SCO_AIRMODE_TRANSP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) if (conn->pkt_type & ESCO_2EV3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) cp.max_latency = cpu_to_le16(0x0008);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) cp.max_latency = cpu_to_le16(0x000D);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) cp.retrans_effort = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) case SCO_AIRMODE_CVSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) cp.max_latency = cpu_to_le16(0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) cp.retrans_effort = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) /* use CVSD settings as fallback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) cp.max_latency = cpu_to_le16(0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) cp.retrans_effort = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) sizeof(cp), &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) size_t len, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) struct sco_pinfo *pi = sco_pi(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (sk->sk_state == BT_CONNECT2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) sco_conn_defer_accept(pi->conn->hcon, pi->setting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) sk->sk_state = BT_CONFIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) return bt_sock_recvmsg(sock, msg, len, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) static int sco_sock_setsockopt(struct socket *sock, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) sockptr_t optval, unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) int len, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) struct bt_voice voice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) u32 opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) BT_DBG("sk %p", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) case BT_DEFER_SETUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) if (opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) case BT_VOICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) sk->sk_state != BT_CONNECT2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) voice.setting = sco_pi(sk)->setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) len = min_t(unsigned int, sizeof(voice), optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) if (copy_from_sockptr(&voice, optval, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) /* Explicitly check for these values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (voice.setting != BT_VOICE_TRANSPARENT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) voice.setting != BT_VOICE_CVSD_16BIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) sco_pi(sk)->setting = voice.setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) case BT_PKT_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) sco_pi(sk)->cmsg_mask |= SCO_CMSG_PKT_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) sco_pi(sk)->cmsg_mask &= SCO_CMSG_PKT_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) err = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) static int sco_sock_getsockopt_old(struct socket *sock, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) char __user *optval, int __user *optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) struct sco_options opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) struct sco_conninfo cinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) int len, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) BT_DBG("sk %p", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (get_user(len, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) case SCO_OPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (sk->sk_state != BT_CONNECTED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) !(sk->sk_state == BT_CONNECT2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) err = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) opts.mtu = sco_pi(sk)->conn->mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) BT_DBG("mtu %d", opts.mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) len = min_t(unsigned int, len, sizeof(opts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) if (copy_to_user(optval, (char *)&opts, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) case SCO_CONNINFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (sk->sk_state != BT_CONNECTED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) !(sk->sk_state == BT_CONNECT2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) err = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) memset(&cinfo, 0, sizeof(cinfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) len = min_t(unsigned int, len, sizeof(cinfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) if (copy_to_user(optval, (char *)&cinfo, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) err = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) char __user *optval, int __user *optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) int len, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) struct bt_voice voice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) u32 phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) int pkt_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) BT_DBG("sk %p", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) if (level == SOL_SCO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) return sco_sock_getsockopt_old(sock, optname, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) if (get_user(len, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) case BT_DEFER_SETUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) (u32 __user *)optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) case BT_VOICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) voice.setting = sco_pi(sk)->setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) len = min_t(unsigned int, len, sizeof(voice));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) if (copy_to_user(optval, (char *)&voice, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) case BT_PHY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) if (sk->sk_state != BT_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) err = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) phys = hci_conn_get_phy(sco_pi(sk)->conn->hcon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) if (put_user(phys, (u32 __user *) optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) case BT_PKT_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) pkt_status = (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (put_user(pkt_status, (int __user *)optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) case BT_SNDMTU:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) case BT_RCVMTU:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) if (sk->sk_state != BT_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) err = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) if (put_user(sco_pi(sk)->conn->mtu, (u32 __user *)optval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) err = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) static int sco_sock_shutdown(struct socket *sock, int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) BT_DBG("sock %p, sk %p", sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) if (!sk->sk_shutdown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) sk->sk_shutdown = SHUTDOWN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) sco_sock_clear_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) __sco_sock_close(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) !(current->flags & PF_EXITING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) err = bt_sock_wait_state(sk, BT_CLOSED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) sk->sk_lingertime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) static int sco_sock_release(struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) BT_DBG("sock %p, sk %p", sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) sco_sock_close(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) !(current->flags & PF_EXITING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) sock_orphan(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) sco_sock_kill(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) static void sco_conn_ready(struct sco_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) struct sock *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) struct sock *sk = conn->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) BT_DBG("conn %p", conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) if (sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) sco_sock_clear_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) sk->sk_state = BT_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) sk->sk_state_change(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) sco_conn_lock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) if (!conn->hcon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) sco_conn_unlock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) parent = sco_get_sock_listen(&conn->hcon->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) if (!parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) sco_conn_unlock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) bh_lock_sock(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) sk = sco_sock_alloc(sock_net(parent), NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) BTPROTO_SCO, GFP_ATOMIC, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) if (!sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) bh_unlock_sock(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) sco_conn_unlock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) sco_sock_init(sk, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) bacpy(&sco_pi(sk)->src, &conn->hcon->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) hci_conn_hold(conn->hcon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) __sco_chan_add(conn, sk, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) sk->sk_state = BT_CONNECT2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) sk->sk_state = BT_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) /* Wake up parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) parent->sk_data_ready(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) bh_unlock_sock(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) sco_conn_unlock(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) /* ----- SCO interface with lower layer (HCI) ----- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) int lm = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) /* Find listening sockets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) read_lock(&sco_sk_list.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) sk_for_each(sk, &sco_sk_list.head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) if (sk->sk_state != BT_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) lm |= HCI_LM_ACCEPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) *flags |= HCI_PROTO_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) read_unlock(&sco_sk_list.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) return lm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) if (!status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) struct sco_conn *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) conn = sco_conn_add(hcon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) sco_conn_ready(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) sco_conn_del(hcon, bt_to_errno(status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) BT_DBG("hcon %p reason %d", hcon, reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) sco_conn_del(hcon, bt_to_errno(reason));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) struct sco_conn *conn = hcon->sco_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) BT_DBG("conn %p len %d", conn, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) if (skb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) sco_recv_frame(conn, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) static struct hci_cb sco_cb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) .name = "SCO",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) .connect_cfm = sco_connect_cfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) .disconn_cfm = sco_disconn_cfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) static int sco_debugfs_show(struct seq_file *f, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) read_lock(&sco_sk_list.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) sk_for_each(sk, &sco_sk_list.head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) &sco_pi(sk)->dst, sk->sk_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) read_unlock(&sco_sk_list.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) DEFINE_SHOW_ATTRIBUTE(sco_debugfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) static struct dentry *sco_debugfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) static const struct proto_ops sco_sock_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) .family = PF_BLUETOOTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) .release = sco_sock_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) .bind = sco_sock_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) .connect = sco_sock_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) .listen = sco_sock_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) .accept = sco_sock_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) .getname = sco_sock_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) .sendmsg = sco_sock_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) .recvmsg = sco_sock_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) .poll = bt_sock_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) .ioctl = bt_sock_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) .gettstamp = sock_gettstamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) .mmap = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) .shutdown = sco_sock_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) .setsockopt = sco_sock_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) .getsockopt = sco_sock_getsockopt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) static const struct net_proto_family sco_sock_family_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) .family = PF_BLUETOOTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) .create = sco_sock_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) int __init sco_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) err = proto_register(&sco_proto, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) BT_ERR("SCO socket registration failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) BT_ERR("Failed to create SCO proc file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) bt_sock_unregister(BTPROTO_SCO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) BT_INFO("SCO socket layer initialized");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) hci_register_cb(&sco_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) if (IS_ERR_OR_NULL(bt_debugfs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) NULL, &sco_debugfs_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) proto_unregister(&sco_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) void sco_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) bt_procfs_cleanup(&init_net, "sco");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) debugfs_remove(sco_debugfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) hci_unregister_cb(&sco_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) bt_sock_unregister(BTPROTO_SCO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) proto_unregister(&sco_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) module_param(disable_esco, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");