YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
| 4 | |
| 5 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth SCO sockets. */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 28 | #include <linux/debugfs.h> |
| 29 | #include <linux/seq_file.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 30 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #include <net/bluetooth/bluetooth.h> |
| 33 | #include <net/bluetooth/hci_core.h> |
| 34 | #include <net/bluetooth/sco.h> |
| 35 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 36 | static bool disable_esco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 38 | static const struct proto_ops sco_sock_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | static struct bt_sock_list sco_sk_list = { |
Robert P. J. Day | d5fb296 | 2008-03-28 16:17:38 -0700 | [diff] [blame] | 41 | .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Marcel Holtmann | fc8f525 | 2014-07-11 06:19:42 +0200 | [diff] [blame] | 44 | /* ---- SCO connections ---- */ |
| 45 | struct sco_conn { |
| 46 | struct hci_conn *hcon; |
| 47 | |
| 48 | spinlock_t lock; |
| 49 | struct sock *sk; |
| 50 | |
Desmond Cheong Zhi Xi | ba316be | 2021-08-10 12:14:05 +0800 | [diff] [blame] | 51 | struct delayed_work timeout_work; |
| 52 | |
Marcel Holtmann | fc8f525 | 2014-07-11 06:19:42 +0200 | [diff] [blame] | 53 | unsigned int mtu; |
| 54 | }; |
| 55 | |
Meng Yu | 0f90d32 | 2021-03-24 10:03:25 +0800 | [diff] [blame] | 56 | #define sco_conn_lock(c) spin_lock(&c->lock) |
| 57 | #define sco_conn_unlock(c) spin_unlock(&c->lock) |
Marcel Holtmann | fc8f525 | 2014-07-11 06:19:42 +0200 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | static void sco_sock_close(struct sock *sk); |
| 60 | static void sco_sock_kill(struct sock *sk); |
| 61 | |
Marcel Holtmann | 2a0dccb | 2014-07-11 06:19:41 +0200 | [diff] [blame] | 62 | /* ----- SCO socket info ----- */ |
| 63 | #define sco_pi(sk) ((struct sco_pinfo *) sk) |
| 64 | |
| 65 | struct sco_pinfo { |
| 66 | struct bt_sock bt; |
| 67 | bdaddr_t src; |
| 68 | bdaddr_t dst; |
| 69 | __u32 flags; |
| 70 | __u16 setting; |
Alain Michaud | 00398e1 | 2020-06-11 19:50:41 +0000 | [diff] [blame] | 71 | __u8 cmsg_mask; |
Kiran K | f687340 | 2021-09-07 15:42:42 +0530 | [diff] [blame] | 72 | struct bt_codec codec; |
Marcel Holtmann | 2a0dccb | 2014-07-11 06:19:41 +0200 | [diff] [blame] | 73 | struct sco_conn *conn; |
| 74 | }; |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* ---- SCO timers ---- */ |
Marcel Holtmann | 068d69e | 2014-07-11 06:19:44 +0200 | [diff] [blame] | 77 | #define SCO_CONN_TIMEOUT (HZ * 40) |
| 78 | #define SCO_DISCONN_TIMEOUT (HZ * 2) |
| 79 | |
Desmond Cheong Zhi Xi | ba316be | 2021-08-10 12:14:05 +0800 | [diff] [blame] | 80 | static void sco_sock_timeout(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Desmond Cheong Zhi Xi | ba316be | 2021-08-10 12:14:05 +0800 | [diff] [blame] | 82 | struct sco_conn *conn = container_of(work, struct sco_conn, |
| 83 | timeout_work.work); |
| 84 | struct sock *sk; |
| 85 | |
| 86 | sco_conn_lock(conn); |
| 87 | sk = conn->sk; |
| 88 | if (sk) |
| 89 | sock_hold(sk); |
| 90 | sco_conn_unlock(conn); |
| 91 | |
| 92 | if (!sk) |
| 93 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 96 | |
Desmond Cheong Zhi Xi | 27c24fd | 2021-08-10 12:14:07 +0800 | [diff] [blame] | 97 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | sk->sk_err = ETIMEDOUT; |
| 99 | sk->sk_state_change(sk); |
Desmond Cheong Zhi Xi | 27c24fd | 2021-08-10 12:14:07 +0800 | [diff] [blame] | 100 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | sock_put(sk); |
| 102 | } |
| 103 | |
| 104 | static void sco_sock_set_timer(struct sock *sk, long timeout) |
| 105 | { |
Desmond Cheong Zhi Xi | ba316be | 2021-08-10 12:14:05 +0800 | [diff] [blame] | 106 | if (!sco_pi(sk)->conn) |
| 107 | return; |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout); |
Desmond Cheong Zhi Xi | ba316be | 2021-08-10 12:14:05 +0800 | [diff] [blame] | 110 | cancel_delayed_work(&sco_pi(sk)->conn->timeout_work); |
| 111 | schedule_delayed_work(&sco_pi(sk)->conn->timeout_work, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static void sco_sock_clear_timer(struct sock *sk) |
| 115 | { |
Desmond Cheong Zhi Xi | ba316be | 2021-08-10 12:14:05 +0800 | [diff] [blame] | 116 | if (!sco_pi(sk)->conn) |
| 117 | return; |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
Desmond Cheong Zhi Xi | ba316be | 2021-08-10 12:14:05 +0800 | [diff] [blame] | 120 | cancel_delayed_work(&sco_pi(sk)->conn->timeout_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /* ---- SCO connections ---- */ |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 124 | static struct sco_conn *sco_conn_add(struct hci_conn *hcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | struct hci_dev *hdev = hcon->hdev; |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 127 | struct sco_conn *conn = hcon->sco_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 129 | if (conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | return conn; |
| 131 | |
Claudio Takahasi | c10cc5a | 2013-04-11 11:35:45 -0300 | [diff] [blame] | 132 | conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL); |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 133 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | spin_lock_init(&conn->lock); |
Desmond Cheong Zhi Xi | 49d8a56 | 2021-09-02 23:13:06 -0400 | [diff] [blame] | 137 | INIT_DELAYED_WORK(&conn->timeout_work, sco_sock_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | hcon->sco_data = conn; |
| 140 | conn->hcon = hcon; |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | if (hdev->sco_mtu > 0) |
| 143 | conn->mtu = hdev->sco_mtu; |
| 144 | else |
| 145 | conn->mtu = 60; |
| 146 | |
| 147 | BT_DBG("hcon %p conn %p", hcon, conn); |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | return conn; |
| 150 | } |
| 151 | |
Marcel Holtmann | e03ab51 | 2014-07-13 19:54:49 +0200 | [diff] [blame] | 152 | /* Delete channel. |
| 153 | * Must be called on the locked socket. */ |
| 154 | static void sco_chan_del(struct sock *sk, int err) |
| 155 | { |
| 156 | struct sco_conn *conn; |
| 157 | |
| 158 | conn = sco_pi(sk)->conn; |
| 159 | |
| 160 | BT_DBG("sk %p, conn %p, err %d", sk, conn, err); |
| 161 | |
| 162 | if (conn) { |
| 163 | sco_conn_lock(conn); |
| 164 | conn->sk = NULL; |
| 165 | sco_pi(sk)->conn = NULL; |
| 166 | sco_conn_unlock(conn); |
| 167 | |
| 168 | if (conn->hcon) |
| 169 | hci_conn_drop(conn->hcon); |
| 170 | } |
| 171 | |
| 172 | sk->sk_state = BT_CLOSED; |
| 173 | sk->sk_err = err; |
| 174 | sk->sk_state_change(sk); |
| 175 | |
| 176 | sock_set_flag(sk, SOCK_ZAPPED); |
| 177 | } |
| 178 | |
Nicholas Krause | df94536 | 2015-08-18 21:23:01 -0400 | [diff] [blame] | 179 | static void sco_conn_del(struct hci_conn *hcon, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 181 | struct sco_conn *conn = hcon->sco_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | struct sock *sk; |
| 183 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 184 | if (!conn) |
Nicholas Krause | df94536 | 2015-08-18 21:23:01 -0400 | [diff] [blame] | 185 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 188 | |
| 189 | /* Kill socket */ |
Marcel Holtmann | eb5a4de | 2014-07-14 01:30:15 +0200 | [diff] [blame] | 190 | sco_conn_lock(conn); |
| 191 | sk = conn->sk; |
Desmond Cheong Zhi Xi | f4712fa | 2021-09-02 23:13:05 -0400 | [diff] [blame] | 192 | if (sk) |
| 193 | sock_hold(sk); |
Marcel Holtmann | eb5a4de | 2014-07-14 01:30:15 +0200 | [diff] [blame] | 194 | sco_conn_unlock(conn); |
| 195 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 196 | if (sk) { |
Desmond Cheong Zhi Xi | 27c24fd | 2021-08-10 12:14:07 +0800 | [diff] [blame] | 197 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | sco_sock_clear_timer(sk); |
| 199 | sco_chan_del(sk, err); |
Desmond Cheong Zhi Xi | 27c24fd | 2021-08-10 12:14:07 +0800 | [diff] [blame] | 200 | release_sock(sk); |
Kuba Pawlak | 75e34f5 | 2015-10-05 18:44:15 +0200 | [diff] [blame] | 201 | sock_put(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Desmond Cheong Zhi Xi | 49d8a56 | 2021-09-02 23:13:06 -0400 | [diff] [blame] | 204 | /* Ensure no more work items will run before freeing conn. */ |
| 205 | cancel_delayed_work_sync(&conn->timeout_work); |
| 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | hcon->sco_data = NULL; |
| 208 | kfree(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 211 | static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, |
| 212 | struct sock *parent) |
Marcel Holtmann | 015b01c | 2014-07-13 19:54:48 +0200 | [diff] [blame] | 213 | { |
| 214 | BT_DBG("conn %p", conn); |
| 215 | |
| 216 | sco_pi(sk)->conn = conn; |
| 217 | conn->sk = sk; |
| 218 | |
| 219 | if (parent) |
Matthias Kaehlcke | c4f5627 | 2019-01-02 16:11:20 -0800 | [diff] [blame] | 220 | bt_accept_enqueue(parent, sk, true); |
Marcel Holtmann | 015b01c | 2014-07-13 19:54:48 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 223 | static int sco_chan_add(struct sco_conn *conn, struct sock *sk, |
| 224 | struct sock *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
| 226 | int err = 0; |
| 227 | |
| 228 | sco_conn_lock(conn); |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 229 | if (conn->sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | err = -EBUSY; |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 231 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | __sco_chan_add(conn, sk, parent); |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | sco_conn_unlock(conn); |
| 235 | return err; |
| 236 | } |
| 237 | |
Desmond Cheong Zhi Xi | 734bc5f | 2021-08-10 12:14:06 +0800 | [diff] [blame] | 238 | static int sco_connect(struct hci_dev *hdev, struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | struct sco_conn *conn; |
| 241 | struct hci_conn *hcon; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 242 | int err, type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 244 | BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 246 | if (lmp_esco_capable(hdev) && !disable_esco) |
| 247 | type = ESCO_LINK; |
| 248 | else |
| 249 | type = SCO_LINK; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 250 | |
Frédéric Dalleau | 79dc008 | 2013-08-19 14:24:01 +0200 | [diff] [blame] | 251 | if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT && |
Desmond Cheong Zhi Xi | 734bc5f | 2021-08-10 12:14:06 +0800 | [diff] [blame] | 252 | (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev))) |
| 253 | return -EOPNOTSUPP; |
Frédéric Dalleau | 79dc008 | 2013-08-19 14:24:01 +0200 | [diff] [blame] | 254 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 255 | hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst, |
Kiran K | b2af264 | 2021-09-07 15:42:43 +0530 | [diff] [blame] | 256 | sco_pi(sk)->setting, &sco_pi(sk)->codec); |
Desmond Cheong Zhi Xi | 734bc5f | 2021-08-10 12:14:06 +0800 | [diff] [blame] | 257 | if (IS_ERR(hcon)) |
| 258 | return PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 260 | conn = sco_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | if (!conn) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 262 | hci_conn_drop(hcon); |
Desmond Cheong Zhi Xi | 734bc5f | 2021-08-10 12:14:06 +0800 | [diff] [blame] | 263 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /* Update source addr of the socket */ |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 267 | bacpy(&sco_pi(sk)->src, &hcon->src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | err = sco_chan_add(conn, sk, NULL); |
| 270 | if (err) |
Desmond Cheong Zhi Xi | 734bc5f | 2021-08-10 12:14:06 +0800 | [diff] [blame] | 271 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
| 273 | if (hcon->state == BT_CONNECTED) { |
| 274 | sco_sock_clear_timer(sk); |
| 275 | sk->sk_state = BT_CONNECTED; |
| 276 | } else { |
| 277 | sk->sk_state = BT_CONNECT; |
| 278 | sco_sock_set_timer(sk, sk->sk_sndtimeo); |
| 279 | } |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return err; |
| 282 | } |
| 283 | |
Luiz Augusto von Dentz | 0771cbb | 2021-09-03 15:27:31 -0700 | [diff] [blame] | 284 | static int sco_send_frame(struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
| 286 | struct sco_conn *conn = sco_pi(sk)->conn; |
Luiz Augusto von Dentz | 037ce00 | 2021-09-16 13:10:49 -0700 | [diff] [blame] | 287 | int len = skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | /* Check outgoing MTU */ |
Luiz Augusto von Dentz | 037ce00 | 2021-09-16 13:10:49 -0700 | [diff] [blame] | 290 | if (len > conn->mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | return -EINVAL; |
| 292 | |
Luiz Augusto von Dentz | 037ce00 | 2021-09-16 13:10:49 -0700 | [diff] [blame] | 293 | BT_DBG("sk %p len %d", sk, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Gustavo F. Padovan | 0d861d8 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 295 | hci_send_sco(conn->hcon, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
Luiz Augusto von Dentz | 037ce00 | 2021-09-16 13:10:49 -0700 | [diff] [blame] | 297 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 300 | static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Marcel Holtmann | eb5a4de | 2014-07-14 01:30:15 +0200 | [diff] [blame] | 302 | struct sock *sk; |
| 303 | |
| 304 | sco_conn_lock(conn); |
| 305 | sk = conn->sk; |
| 306 | sco_conn_unlock(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
| 308 | if (!sk) |
| 309 | goto drop; |
| 310 | |
Kai Ye | 79dbeafe | 2021-06-03 15:41:04 +0800 | [diff] [blame] | 311 | BT_DBG("sk %p len %u", sk, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
| 313 | if (sk->sk_state != BT_CONNECTED) |
| 314 | goto drop; |
| 315 | |
| 316 | if (!sock_queue_rcv_skb(sk, skb)) |
| 317 | return; |
| 318 | |
| 319 | drop: |
| 320 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* -------- Socket interface ---------- */ |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 324 | static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 326 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 328 | sk_for_each(sk, &sco_sk_list.head) { |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 329 | if (sk->sk_state != BT_LISTEN) |
| 330 | continue; |
| 331 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 332 | if (!bacmp(&sco_pi(sk)->src, ba)) |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 333 | return sk; |
| 334 | } |
| 335 | |
| 336 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* Find socket listening on source bdaddr. |
| 340 | * Returns closest match. |
| 341 | */ |
| 342 | static struct sock *sco_get_sock_listen(bdaddr_t *src) |
| 343 | { |
| 344 | struct sock *sk = NULL, *sk1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | read_lock(&sco_sk_list.lock); |
| 347 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 348 | sk_for_each(sk, &sco_sk_list.head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | if (sk->sk_state != BT_LISTEN) |
| 350 | continue; |
| 351 | |
| 352 | /* Exact match. */ |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 353 | if (!bacmp(&sco_pi(sk)->src, src)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | break; |
| 355 | |
| 356 | /* Closest match */ |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 357 | if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | sk1 = sk; |
| 359 | } |
| 360 | |
| 361 | read_unlock(&sco_sk_list.lock); |
| 362 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 363 | return sk ? sk : sk1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static void sco_sock_destruct(struct sock *sk) |
| 367 | { |
| 368 | BT_DBG("sk %p", sk); |
| 369 | |
| 370 | skb_queue_purge(&sk->sk_receive_queue); |
| 371 | skb_queue_purge(&sk->sk_write_queue); |
| 372 | } |
| 373 | |
| 374 | static void sco_sock_cleanup_listen(struct sock *parent) |
| 375 | { |
| 376 | struct sock *sk; |
| 377 | |
| 378 | BT_DBG("parent %p", parent); |
| 379 | |
| 380 | /* Close not yet accepted channels */ |
| 381 | while ((sk = bt_accept_dequeue(parent, NULL))) { |
| 382 | sco_sock_close(sk); |
| 383 | sco_sock_kill(sk); |
| 384 | } |
| 385 | |
| 386 | parent->sk_state = BT_CLOSED; |
| 387 | sock_set_flag(parent, SOCK_ZAPPED); |
| 388 | } |
| 389 | |
| 390 | /* Kill socket (only if zapped and orphan) |
| 391 | * Must be called on unlocked socket. |
| 392 | */ |
| 393 | static void sco_sock_kill(struct sock *sk) |
| 394 | { |
Desmond Cheong Zhi Xi | e1dee2c | 2021-08-10 12:14:10 +0800 | [diff] [blame] | 395 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | return; |
| 397 | |
| 398 | BT_DBG("sk %p state %d", sk, sk->sk_state); |
| 399 | |
| 400 | /* Kill poor orphan */ |
| 401 | bt_sock_unlink(&sco_sk_list, sk); |
| 402 | sock_set_flag(sk, SOCK_DEAD); |
| 403 | sock_put(sk); |
| 404 | } |
| 405 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 406 | static void __sco_sock_close(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 408 | BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
| 410 | switch (sk->sk_state) { |
| 411 | case BT_LISTEN: |
| 412 | sco_sock_cleanup_listen(sk); |
| 413 | break; |
| 414 | |
| 415 | case BT_CONNECTED: |
| 416 | case BT_CONFIG: |
Gustavo Padovan | b7e98b5 | 2013-01-03 19:59:28 -0200 | [diff] [blame] | 417 | if (sco_pi(sk)->conn->hcon) { |
Luiz Augusto von Dentz | 4a77708 | 2011-05-12 11:13:15 +0300 | [diff] [blame] | 418 | sk->sk_state = BT_DISCONN; |
| 419 | sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT); |
Kuba Pawlak | 435c513 | 2015-10-05 18:44:16 +0200 | [diff] [blame] | 420 | sco_conn_lock(sco_pi(sk)->conn); |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 421 | hci_conn_drop(sco_pi(sk)->conn->hcon); |
Luiz Augusto von Dentz | 4a77708 | 2011-05-12 11:13:15 +0300 | [diff] [blame] | 422 | sco_pi(sk)->conn->hcon = NULL; |
Kuba Pawlak | 435c513 | 2015-10-05 18:44:16 +0200 | [diff] [blame] | 423 | sco_conn_unlock(sco_pi(sk)->conn); |
Luiz Augusto von Dentz | 4a77708 | 2011-05-12 11:13:15 +0300 | [diff] [blame] | 424 | } else |
| 425 | sco_chan_del(sk, ECONNRESET); |
| 426 | break; |
| 427 | |
Vinicius Costa Gomes | eb20ff9 | 2013-03-13 19:46:20 -0300 | [diff] [blame] | 428 | case BT_CONNECT2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | case BT_CONNECT: |
| 430 | case BT_DISCONN: |
| 431 | sco_chan_del(sk, ECONNRESET); |
| 432 | break; |
| 433 | |
| 434 | default: |
| 435 | sock_set_flag(sk, SOCK_ZAPPED); |
| 436 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 437 | } |
Kiran K | f687340 | 2021-09-07 15:42:42 +0530 | [diff] [blame] | 438 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 441 | /* Must be called on unlocked socket. */ |
| 442 | static void sco_sock_close(struct sock *sk) |
| 443 | { |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 444 | lock_sock(sk); |
Desmond Cheong Zhi Xi | 3f2c89f | 2021-08-10 12:14:08 +0800 | [diff] [blame] | 445 | sco_sock_clear_timer(sk); |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 446 | __sco_sock_close(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Alain Michaud | 00398e1 | 2020-06-11 19:50:41 +0000 | [diff] [blame] | 450 | static void sco_skb_put_cmsg(struct sk_buff *skb, struct msghdr *msg, |
| 451 | struct sock *sk) |
| 452 | { |
| 453 | if (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS) |
| 454 | put_cmsg(msg, SOL_BLUETOOTH, BT_SCM_PKT_STATUS, |
| 455 | sizeof(bt_cb(skb)->sco.pkt_status), |
| 456 | &bt_cb(skb)->sco.pkt_status); |
| 457 | } |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | static void sco_sock_init(struct sock *sk, struct sock *parent) |
| 460 | { |
| 461 | BT_DBG("sk %p", sk); |
| 462 | |
Paul Moore | 6230c9b | 2011-10-07 09:40:59 +0000 | [diff] [blame] | 463 | if (parent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | sk->sk_type = parent->sk_type; |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 465 | bt_sk(sk)->flags = bt_sk(parent)->flags; |
Paul Moore | 6230c9b | 2011-10-07 09:40:59 +0000 | [diff] [blame] | 466 | security_sk_clone(parent, sk); |
Alain Michaud | 00398e1 | 2020-06-11 19:50:41 +0000 | [diff] [blame] | 467 | } else { |
| 468 | bt_sk(sk)->skb_put_cmsg = sco_skb_put_cmsg; |
Paul Moore | 6230c9b | 2011-10-07 09:40:59 +0000 | [diff] [blame] | 469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static struct proto sco_proto = { |
| 473 | .name = "SCO", |
| 474 | .owner = THIS_MODULE, |
| 475 | .obj_size = sizeof(struct sco_pinfo) |
| 476 | }; |
| 477 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 478 | static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, |
| 479 | int proto, gfp_t prio, int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | { |
| 481 | struct sock *sk; |
| 482 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 483 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, kern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | if (!sk) |
| 485 | return NULL; |
| 486 | |
| 487 | sock_init_data(sock, sk); |
| 488 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 489 | |
| 490 | sk->sk_destruct = sco_sock_destruct; |
| 491 | sk->sk_sndtimeo = SCO_CONN_TIMEOUT; |
| 492 | |
| 493 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 494 | |
| 495 | sk->sk_protocol = proto; |
| 496 | sk->sk_state = BT_OPEN; |
| 497 | |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 498 | sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT; |
Kiran K | f687340 | 2021-09-07 15:42:42 +0530 | [diff] [blame] | 499 | sco_pi(sk)->codec.id = BT_CODEC_CVSD; |
| 500 | sco_pi(sk)->codec.cid = 0xffff; |
| 501 | sco_pi(sk)->codec.vid = 0xffff; |
| 502 | sco_pi(sk)->codec.data_path = 0x00; |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | bt_sock_link(&sco_sk_list, sk); |
| 505 | return sk; |
| 506 | } |
| 507 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 508 | static int sco_sock_create(struct net *net, struct socket *sock, int protocol, |
| 509 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { |
| 511 | struct sock *sk; |
| 512 | |
| 513 | BT_DBG("sock %p", sock); |
| 514 | |
| 515 | sock->state = SS_UNCONNECTED; |
| 516 | |
| 517 | if (sock->type != SOCK_SEQPACKET) |
| 518 | return -ESOCKTNOSUPPORT; |
| 519 | |
| 520 | sock->ops = &sco_sock_ops; |
| 521 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 522 | sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern); |
Marcel Holtmann | 74da626 | 2006-10-15 17:31:14 +0200 | [diff] [blame] | 523 | if (!sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | return -ENOMEM; |
| 525 | |
| 526 | sco_sock_init(sk, NULL); |
| 527 | return 0; |
| 528 | } |
| 529 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 530 | static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, |
| 531 | int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
| 533 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 534 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | int err = 0; |
| 536 | |
Mateusz Jurczyk | d2ecfa7 | 2017-06-29 14:04:59 +0200 | [diff] [blame] | 537 | if (!addr || addr_len < sizeof(struct sockaddr_sco) || |
| 538 | addr->sa_family != AF_BLUETOOTH) |
David S. Miller | 5233252 | 2015-12-15 15:39:08 -0500 | [diff] [blame] | 539 | return -EINVAL; |
| 540 | |
Tetsuo Handa | bd7d46d | 2019-04-12 19:54:33 +0900 | [diff] [blame] | 541 | BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr); |
| 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | lock_sock(sk); |
| 544 | |
| 545 | if (sk->sk_state != BT_OPEN) { |
| 546 | err = -EBADFD; |
| 547 | goto done; |
| 548 | } |
| 549 | |
Marcel Holtmann | 8ed21f7 | 2012-04-19 13:43:53 +0200 | [diff] [blame] | 550 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 551 | err = -EINVAL; |
| 552 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 555 | bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr); |
Marcel Holtmann | 8ed21f7 | 2012-04-19 13:43:53 +0200 | [diff] [blame] | 556 | |
| 557 | sk->sk_state = BT_BOUND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | done: |
| 560 | release_sock(sk); |
| 561 | return err; |
| 562 | } |
| 563 | |
| 564 | static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) |
| 565 | { |
| 566 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 567 | struct sock *sk = sock->sk; |
Desmond Cheong Zhi Xi | 734bc5f | 2021-08-10 12:14:06 +0800 | [diff] [blame] | 568 | struct hci_dev *hdev; |
Claudio Takahasi | 92f185c | 2013-04-11 11:35:46 -0300 | [diff] [blame] | 569 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
| 571 | BT_DBG("sk %p", sk); |
| 572 | |
Changli Gao | 6503d96 | 2010-03-31 22:58:26 +0000 | [diff] [blame] | 573 | if (alen < sizeof(struct sockaddr_sco) || |
| 574 | addr->sa_family != AF_BLUETOOTH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | return -EINVAL; |
| 576 | |
| 577 | if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) |
| 578 | return -EBADFD; |
| 579 | |
| 580 | if (sk->sk_type != SOCK_SEQPACKET) |
| 581 | return -EINVAL; |
| 582 | |
Desmond Cheong Zhi Xi | 734bc5f | 2021-08-10 12:14:06 +0800 | [diff] [blame] | 583 | hdev = hci_get_route(&sa->sco_bdaddr, &sco_pi(sk)->src, BDADDR_BREDR); |
| 584 | if (!hdev) |
| 585 | return -EHOSTUNREACH; |
| 586 | hci_dev_lock(hdev); |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | lock_sock(sk); |
| 589 | |
| 590 | /* Set destination address and psm */ |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 591 | bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
Desmond Cheong Zhi Xi | 734bc5f | 2021-08-10 12:14:06 +0800 | [diff] [blame] | 593 | err = sco_connect(hdev, sk); |
| 594 | hci_dev_unlock(hdev); |
| 595 | hci_dev_put(hdev); |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 596 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | goto done; |
| 598 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 599 | err = bt_sock_wait_state(sk, BT_CONNECTED, |
Gustavo Padovan | be7c2b9 | 2012-05-17 00:36:21 -0300 | [diff] [blame] | 600 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
| 602 | done: |
| 603 | release_sock(sk); |
| 604 | return err; |
| 605 | } |
| 606 | |
| 607 | static int sco_sock_listen(struct socket *sock, int backlog) |
| 608 | { |
| 609 | struct sock *sk = sock->sk; |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 610 | bdaddr_t *src = &sco_pi(sk)->src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | int err = 0; |
| 612 | |
| 613 | BT_DBG("sk %p backlog %d", sk, backlog); |
| 614 | |
| 615 | lock_sock(sk); |
| 616 | |
Marcel Holtmann | 7d5d775 | 2012-04-19 13:43:52 +0200 | [diff] [blame] | 617 | if (sk->sk_state != BT_BOUND) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | err = -EBADFD; |
| 619 | goto done; |
| 620 | } |
| 621 | |
Marcel Holtmann | 7d5d775 | 2012-04-19 13:43:52 +0200 | [diff] [blame] | 622 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 623 | err = -EINVAL; |
| 624 | goto done; |
| 625 | } |
| 626 | |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 627 | write_lock(&sco_sk_list.lock); |
| 628 | |
| 629 | if (__sco_get_sock_listen_by_addr(src)) { |
| 630 | err = -EADDRINUSE; |
| 631 | goto unlock; |
| 632 | } |
| 633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | sk->sk_max_ack_backlog = backlog; |
| 635 | sk->sk_ack_backlog = 0; |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | sk->sk_state = BT_LISTEN; |
| 638 | |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 639 | unlock: |
| 640 | write_unlock(&sco_sk_list.lock); |
| 641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | done: |
| 643 | release_sock(sk); |
| 644 | return err; |
| 645 | } |
| 646 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 647 | static int sco_sock_accept(struct socket *sock, struct socket *newsock, |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 648 | int flags, bool kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | { |
Peter Hurley | dfb2fae | 2015-01-23 12:16:53 -0500 | [diff] [blame] | 650 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | struct sock *sk = sock->sk, *ch; |
| 652 | long timeo; |
| 653 | int err = 0; |
| 654 | |
| 655 | lock_sock(sk); |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 658 | |
| 659 | BT_DBG("sk %p timeo %ld", sk, timeo); |
| 660 | |
| 661 | /* Wait for an incoming connection. (wake-one). */ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 662 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 663 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | if (sk->sk_state != BT_LISTEN) { |
| 665 | err = -EBADFD; |
| 666 | break; |
| 667 | } |
| 668 | |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 669 | ch = bt_accept_dequeue(sk, newsock); |
| 670 | if (ch) |
| 671 | break; |
| 672 | |
| 673 | if (!timeo) { |
| 674 | err = -EAGAIN; |
| 675 | break; |
| 676 | } |
| 677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (signal_pending(current)) { |
| 679 | err = sock_intr_errno(timeo); |
| 680 | break; |
| 681 | } |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 682 | |
| 683 | release_sock(sk); |
Peter Hurley | dfb2fae | 2015-01-23 12:16:53 -0500 | [diff] [blame] | 684 | |
| 685 | timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo); |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 686 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 688 | remove_wait_queue(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
| 690 | if (err) |
| 691 | goto done; |
| 692 | |
| 693 | newsock->state = SS_CONNECTED; |
| 694 | |
| 695 | BT_DBG("new socket %p", ch); |
| 696 | |
| 697 | done: |
| 698 | release_sock(sk); |
| 699 | return err; |
| 700 | } |
| 701 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 702 | static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 703 | int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | { |
| 705 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 706 | struct sock *sk = sock->sk; |
| 707 | |
| 708 | BT_DBG("sock %p, sk %p", sock, sk); |
| 709 | |
| 710 | addr->sa_family = AF_BLUETOOTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | if (peer) |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 713 | bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | else |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 715 | bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 717 | return sizeof(struct sockaddr_sco); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 720 | static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg, |
| 721 | size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | { |
| 723 | struct sock *sk = sock->sk; |
Luiz Augusto von Dentz | 0771cbb | 2021-09-03 15:27:31 -0700 | [diff] [blame] | 724 | struct sk_buff *skb; |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 725 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
| 727 | BT_DBG("sock %p, sk %p", sock, sk); |
| 728 | |
Benjamin LaHaise | c1cbe4b | 2005-12-13 23:22:19 -0800 | [diff] [blame] | 729 | err = sock_error(sk); |
| 730 | if (err) |
| 731 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
| 733 | if (msg->msg_flags & MSG_OOB) |
| 734 | return -EOPNOTSUPP; |
| 735 | |
Luiz Augusto von Dentz | 0771cbb | 2021-09-03 15:27:31 -0700 | [diff] [blame] | 736 | skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0); |
Luiz Augusto von Dentz | 266191aa | 2021-09-16 13:10:48 -0700 | [diff] [blame] | 737 | if (IS_ERR(skb)) |
Luiz Augusto von Dentz | 0771cbb | 2021-09-03 15:27:31 -0700 | [diff] [blame] | 738 | return PTR_ERR(skb); |
Takashi Iwai | 99c23da | 2021-08-28 18:18:18 +0200 | [diff] [blame] | 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | lock_sock(sk); |
| 741 | |
| 742 | if (sk->sk_state == BT_CONNECTED) |
Luiz Augusto von Dentz | 0771cbb | 2021-09-03 15:27:31 -0700 | [diff] [blame] | 743 | err = sco_send_frame(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | else |
| 745 | err = -ENOTCONN; |
| 746 | |
| 747 | release_sock(sk); |
Luiz Augusto von Dentz | 037ce00 | 2021-09-16 13:10:49 -0700 | [diff] [blame] | 748 | |
| 749 | if (err < 0) |
Luiz Augusto von Dentz | 0771cbb | 2021-09-03 15:27:31 -0700 | [diff] [blame] | 750 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | return err; |
| 752 | } |
| 753 | |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 754 | static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting) |
Frédéric Dalleau | fa5513b | 2013-04-16 17:28:58 +0200 | [diff] [blame] | 755 | { |
| 756 | struct hci_dev *hdev = conn->hdev; |
| 757 | |
| 758 | BT_DBG("conn %p", conn); |
| 759 | |
| 760 | conn->state = BT_CONFIG; |
| 761 | |
| 762 | if (!lmp_esco_capable(hdev)) { |
| 763 | struct hci_cp_accept_conn_req cp; |
| 764 | |
| 765 | bacpy(&cp.bdaddr, &conn->dst); |
Frédéric Dalleau | 33f2404 | 2013-08-19 14:23:55 +0200 | [diff] [blame] | 766 | cp.role = 0x00; /* Ignored */ |
Frédéric Dalleau | fa5513b | 2013-04-16 17:28:58 +0200 | [diff] [blame] | 767 | |
| 768 | hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp); |
| 769 | } else { |
| 770 | struct hci_cp_accept_sync_conn_req cp; |
| 771 | |
| 772 | bacpy(&cp.bdaddr, &conn->dst); |
| 773 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
| 774 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 775 | cp.tx_bandwidth = cpu_to_le32(0x00001f40); |
| 776 | cp.rx_bandwidth = cpu_to_le32(0x00001f40); |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 777 | cp.content_format = cpu_to_le16(setting); |
| 778 | |
| 779 | switch (setting & SCO_AIRMODE_MASK) { |
| 780 | case SCO_AIRMODE_TRANSP: |
| 781 | if (conn->pkt_type & ESCO_2EV3) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 782 | cp.max_latency = cpu_to_le16(0x0008); |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 783 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 784 | cp.max_latency = cpu_to_le16(0x000D); |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 785 | cp.retrans_effort = 0x02; |
| 786 | break; |
| 787 | case SCO_AIRMODE_CVSD: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 788 | cp.max_latency = cpu_to_le16(0xffff); |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 789 | cp.retrans_effort = 0xff; |
| 790 | break; |
Dan Carpenter | 59da0b3 | 2021-06-25 18:00:09 +0300 | [diff] [blame] | 791 | default: |
| 792 | /* use CVSD settings as fallback */ |
| 793 | cp.max_latency = cpu_to_le16(0xffff); |
| 794 | cp.retrans_effort = 0xff; |
| 795 | break; |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 796 | } |
Frédéric Dalleau | fa5513b | 2013-04-16 17:28:58 +0200 | [diff] [blame] | 797 | |
| 798 | hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, |
| 799 | sizeof(cp), &cp); |
| 800 | } |
| 801 | } |
| 802 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 803 | static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg, |
| 804 | size_t len, int flags) |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 805 | { |
| 806 | struct sock *sk = sock->sk; |
| 807 | struct sco_pinfo *pi = sco_pi(sk); |
| 808 | |
| 809 | lock_sock(sk); |
| 810 | |
| 811 | if (sk->sk_state == BT_CONNECT2 && |
| 812 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 813 | sco_conn_defer_accept(pi->conn->hcon, pi->setting); |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 814 | sk->sk_state = BT_CONFIG; |
| 815 | |
| 816 | release_sock(sk); |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | release_sock(sk); |
| 821 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 822 | return bt_sock_recvmsg(sock, msg, len, flags); |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 823 | } |
| 824 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 825 | static int sco_sock_setsockopt(struct socket *sock, int level, int optname, |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 826 | sockptr_t optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | { |
| 828 | struct sock *sk = sock->sk; |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 829 | int len, err = 0; |
| 830 | struct bt_voice voice; |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 831 | u32 opt; |
Kiran K | f687340 | 2021-09-07 15:42:42 +0530 | [diff] [blame] | 832 | struct bt_codecs *codecs; |
| 833 | struct hci_dev *hdev; |
| 834 | __u8 buffer[255]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | BT_DBG("sk %p", sk); |
| 837 | |
| 838 | lock_sock(sk); |
| 839 | |
| 840 | switch (optname) { |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 841 | |
| 842 | case BT_DEFER_SETUP: |
| 843 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 844 | err = -EINVAL; |
| 845 | break; |
| 846 | } |
| 847 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 848 | if (copy_from_sockptr(&opt, optval, sizeof(u32))) { |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 849 | err = -EFAULT; |
| 850 | break; |
| 851 | } |
| 852 | |
| 853 | if (opt) |
| 854 | set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
| 855 | else |
| 856 | clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
| 857 | break; |
| 858 | |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 859 | case BT_VOICE: |
| 860 | if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND && |
| 861 | sk->sk_state != BT_CONNECT2) { |
| 862 | err = -EINVAL; |
| 863 | break; |
| 864 | } |
| 865 | |
| 866 | voice.setting = sco_pi(sk)->setting; |
| 867 | |
| 868 | len = min_t(unsigned int, sizeof(voice), optlen); |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 869 | if (copy_from_sockptr(&voice, optval, len)) { |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 870 | err = -EFAULT; |
| 871 | break; |
| 872 | } |
| 873 | |
| 874 | /* Explicitly check for these values */ |
| 875 | if (voice.setting != BT_VOICE_TRANSPARENT && |
| 876 | voice.setting != BT_VOICE_CVSD_16BIT) { |
| 877 | err = -EINVAL; |
| 878 | break; |
| 879 | } |
| 880 | |
| 881 | sco_pi(sk)->setting = voice.setting; |
Kiran K | b2af264 | 2021-09-07 15:42:43 +0530 | [diff] [blame] | 882 | hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src, |
| 883 | BDADDR_BREDR); |
| 884 | if (!hdev) { |
| 885 | err = -EBADFD; |
| 886 | break; |
| 887 | } |
| 888 | if (enhanced_sco_capable(hdev) && |
| 889 | voice.setting == BT_VOICE_TRANSPARENT) |
| 890 | sco_pi(sk)->codec.id = BT_CODEC_TRANSPARENT; |
| 891 | hci_dev_put(hdev); |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 892 | break; |
| 893 | |
Alain Michaud | 00398e1 | 2020-06-11 19:50:41 +0000 | [diff] [blame] | 894 | case BT_PKT_STATUS: |
David S. Miller | 83a33b2 | 2020-07-31 16:15:03 -0700 | [diff] [blame] | 895 | if (copy_from_sockptr(&opt, optval, sizeof(u32))) { |
Alain Michaud | 00398e1 | 2020-06-11 19:50:41 +0000 | [diff] [blame] | 896 | err = -EFAULT; |
| 897 | break; |
| 898 | } |
| 899 | |
| 900 | if (opt) |
| 901 | sco_pi(sk)->cmsg_mask |= SCO_CMSG_PKT_STATUS; |
| 902 | else |
| 903 | sco_pi(sk)->cmsg_mask &= SCO_CMSG_PKT_STATUS; |
| 904 | break; |
| 905 | |
Kiran K | f687340 | 2021-09-07 15:42:42 +0530 | [diff] [blame] | 906 | case BT_CODEC: |
| 907 | if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND && |
| 908 | sk->sk_state != BT_CONNECT2) { |
| 909 | err = -EINVAL; |
| 910 | break; |
| 911 | } |
| 912 | |
| 913 | hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src, |
| 914 | BDADDR_BREDR); |
| 915 | if (!hdev) { |
| 916 | err = -EBADFD; |
| 917 | break; |
| 918 | } |
| 919 | |
| 920 | if (!hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)) { |
| 921 | hci_dev_put(hdev); |
| 922 | err = -EOPNOTSUPP; |
| 923 | break; |
| 924 | } |
| 925 | |
| 926 | if (!hdev->get_data_path_id) { |
| 927 | hci_dev_put(hdev); |
| 928 | err = -EOPNOTSUPP; |
| 929 | break; |
| 930 | } |
| 931 | |
| 932 | if (optlen < sizeof(struct bt_codecs) || |
| 933 | optlen > sizeof(buffer)) { |
| 934 | hci_dev_put(hdev); |
| 935 | err = -EINVAL; |
| 936 | break; |
| 937 | } |
| 938 | |
| 939 | if (copy_from_sockptr(buffer, optval, optlen)) { |
| 940 | hci_dev_put(hdev); |
| 941 | err = -EFAULT; |
| 942 | break; |
| 943 | } |
| 944 | |
| 945 | codecs = (void *)buffer; |
| 946 | |
| 947 | if (codecs->num_codecs > 1) { |
| 948 | hci_dev_put(hdev); |
| 949 | err = -EINVAL; |
| 950 | break; |
| 951 | } |
| 952 | |
| 953 | sco_pi(sk)->codec = codecs->codecs[0]; |
| 954 | hci_dev_put(hdev); |
| 955 | break; |
| 956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | default: |
| 958 | err = -ENOPROTOOPT; |
| 959 | break; |
| 960 | } |
| 961 | |
| 962 | release_sock(sk); |
| 963 | return err; |
| 964 | } |
| 965 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 966 | static int sco_sock_getsockopt_old(struct socket *sock, int optname, |
| 967 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | { |
| 969 | struct sock *sk = sock->sk; |
| 970 | struct sco_options opts; |
| 971 | struct sco_conninfo cinfo; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 972 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | |
| 974 | BT_DBG("sk %p", sk); |
| 975 | |
| 976 | if (get_user(len, optlen)) |
| 977 | return -EFAULT; |
| 978 | |
| 979 | lock_sock(sk); |
| 980 | |
| 981 | switch (optname) { |
| 982 | case SCO_OPTIONS: |
Johan Hedberg | 9d225d2 | 2013-08-08 14:53:56 +0300 | [diff] [blame] | 983 | if (sk->sk_state != BT_CONNECTED && |
| 984 | !(sk->sk_state == BT_CONNECT2 && |
| 985 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | err = -ENOTCONN; |
| 987 | break; |
| 988 | } |
| 989 | |
| 990 | opts.mtu = sco_pi(sk)->conn->mtu; |
| 991 | |
Kai Ye | 79dbeafe | 2021-06-03 15:41:04 +0800 | [diff] [blame] | 992 | BT_DBG("mtu %u", opts.mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
| 994 | len = min_t(unsigned int, len, sizeof(opts)); |
| 995 | if (copy_to_user(optval, (char *)&opts, len)) |
| 996 | err = -EFAULT; |
| 997 | |
| 998 | break; |
| 999 | |
| 1000 | case SCO_CONNINFO: |
Johan Hedberg | 9d225d2 | 2013-08-08 14:53:56 +0300 | [diff] [blame] | 1001 | if (sk->sk_state != BT_CONNECTED && |
| 1002 | !(sk->sk_state == BT_CONNECT2 && |
| 1003 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | err = -ENOTCONN; |
| 1005 | break; |
| 1006 | } |
| 1007 | |
Vasiliy Kulikov | c4c896e | 2011-02-14 13:54:26 +0300 | [diff] [blame] | 1008 | memset(&cinfo, 0, sizeof(cinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle; |
| 1010 | memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3); |
| 1011 | |
| 1012 | len = min_t(unsigned int, len, sizeof(cinfo)); |
| 1013 | if (copy_to_user(optval, (char *)&cinfo, len)) |
| 1014 | err = -EFAULT; |
| 1015 | |
| 1016 | break; |
| 1017 | |
| 1018 | default: |
| 1019 | err = -ENOPROTOOPT; |
| 1020 | break; |
| 1021 | } |
| 1022 | |
| 1023 | release_sock(sk); |
| 1024 | return err; |
| 1025 | } |
| 1026 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 1027 | static int sco_sock_getsockopt(struct socket *sock, int level, int optname, |
| 1028 | char __user *optval, int __user *optlen) |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 1029 | { |
| 1030 | struct sock *sk = sock->sk; |
| 1031 | int len, err = 0; |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 1032 | struct bt_voice voice; |
Luiz Augusto von Dentz | eab2404 | 2020-02-14 10:08:57 -0800 | [diff] [blame] | 1033 | u32 phys; |
Alain Michaud | 00398e1 | 2020-06-11 19:50:41 +0000 | [diff] [blame] | 1034 | int pkt_status; |
Kiran K | 248733e | 2021-09-07 15:42:40 +0530 | [diff] [blame] | 1035 | int buf_len; |
| 1036 | struct codec_list *c; |
| 1037 | u8 num_codecs, i, __user *ptr; |
| 1038 | struct hci_dev *hdev; |
| 1039 | struct hci_codec_caps *caps; |
| 1040 | struct bt_codec codec; |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 1041 | |
| 1042 | BT_DBG("sk %p", sk); |
| 1043 | |
| 1044 | if (level == SOL_SCO) |
| 1045 | return sco_sock_getsockopt_old(sock, optname, optval, optlen); |
| 1046 | |
| 1047 | if (get_user(len, optlen)) |
| 1048 | return -EFAULT; |
| 1049 | |
| 1050 | lock_sock(sk); |
| 1051 | |
| 1052 | switch (optname) { |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 1053 | |
| 1054 | case BT_DEFER_SETUP: |
| 1055 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 1056 | err = -EINVAL; |
| 1057 | break; |
| 1058 | } |
| 1059 | |
| 1060 | if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags), |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 1061 | (u32 __user *)optval)) |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 1062 | err = -EFAULT; |
| 1063 | |
| 1064 | break; |
| 1065 | |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 1066 | case BT_VOICE: |
| 1067 | voice.setting = sco_pi(sk)->setting; |
| 1068 | |
| 1069 | len = min_t(unsigned int, len, sizeof(voice)); |
| 1070 | if (copy_to_user(optval, (char *)&voice, len)) |
| 1071 | err = -EFAULT; |
| 1072 | |
| 1073 | break; |
| 1074 | |
Luiz Augusto von Dentz | eab2404 | 2020-02-14 10:08:57 -0800 | [diff] [blame] | 1075 | case BT_PHY: |
Luiz Augusto von Dentz | a2a8b0b | 2020-02-18 10:33:20 -0800 | [diff] [blame] | 1076 | if (sk->sk_state != BT_CONNECTED) { |
Luiz Augusto von Dentz | eab2404 | 2020-02-14 10:08:57 -0800 | [diff] [blame] | 1077 | err = -ENOTCONN; |
| 1078 | break; |
| 1079 | } |
| 1080 | |
| 1081 | phys = hci_conn_get_phy(sco_pi(sk)->conn->hcon); |
| 1082 | |
| 1083 | if (put_user(phys, (u32 __user *) optval)) |
| 1084 | err = -EFAULT; |
| 1085 | break; |
| 1086 | |
Alain Michaud | 00398e1 | 2020-06-11 19:50:41 +0000 | [diff] [blame] | 1087 | case BT_PKT_STATUS: |
| 1088 | pkt_status = (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS); |
| 1089 | |
| 1090 | if (put_user(pkt_status, (int __user *)optval)) |
| 1091 | err = -EFAULT; |
| 1092 | break; |
| 1093 | |
Joseph Hwang | 0fc1a72 | 2020-09-10 14:04:02 +0800 | [diff] [blame] | 1094 | case BT_SNDMTU: |
| 1095 | case BT_RCVMTU: |
Wei Yongjun | f6b8c6b | 2020-11-16 21:24:21 +0800 | [diff] [blame] | 1096 | if (sk->sk_state != BT_CONNECTED) { |
| 1097 | err = -ENOTCONN; |
| 1098 | break; |
| 1099 | } |
| 1100 | |
Joseph Hwang | 0fc1a72 | 2020-09-10 14:04:02 +0800 | [diff] [blame] | 1101 | if (put_user(sco_pi(sk)->conn->mtu, (u32 __user *)optval)) |
| 1102 | err = -EFAULT; |
| 1103 | break; |
| 1104 | |
Kiran K | 248733e | 2021-09-07 15:42:40 +0530 | [diff] [blame] | 1105 | case BT_CODEC: |
| 1106 | num_codecs = 0; |
| 1107 | buf_len = 0; |
| 1108 | |
| 1109 | hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src, BDADDR_BREDR); |
| 1110 | if (!hdev) { |
| 1111 | err = -EBADFD; |
| 1112 | break; |
| 1113 | } |
| 1114 | |
| 1115 | if (!hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)) { |
| 1116 | hci_dev_put(hdev); |
| 1117 | err = -EOPNOTSUPP; |
| 1118 | break; |
| 1119 | } |
| 1120 | |
| 1121 | if (!hdev->get_data_path_id) { |
| 1122 | hci_dev_put(hdev); |
| 1123 | err = -EOPNOTSUPP; |
| 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | /* find total buffer size required to copy codec + caps */ |
| 1128 | hci_dev_lock(hdev); |
| 1129 | list_for_each_entry(c, &hdev->local_codecs, list) { |
| 1130 | if (c->transport != HCI_TRANSPORT_SCO_ESCO) |
| 1131 | continue; |
| 1132 | num_codecs++; |
| 1133 | for (i = 0, caps = c->caps; i < c->num_caps; i++) { |
| 1134 | buf_len += 1 + caps->len; |
| 1135 | caps = (void *)&caps->data[caps->len]; |
| 1136 | } |
| 1137 | buf_len += sizeof(struct bt_codec); |
| 1138 | } |
| 1139 | hci_dev_unlock(hdev); |
| 1140 | |
| 1141 | buf_len += sizeof(struct bt_codecs); |
| 1142 | if (buf_len > len) { |
| 1143 | hci_dev_put(hdev); |
| 1144 | err = -ENOBUFS; |
| 1145 | break; |
| 1146 | } |
| 1147 | ptr = optval; |
| 1148 | |
| 1149 | if (put_user(num_codecs, ptr)) { |
| 1150 | hci_dev_put(hdev); |
| 1151 | err = -EFAULT; |
| 1152 | break; |
| 1153 | } |
| 1154 | ptr += sizeof(num_codecs); |
| 1155 | |
| 1156 | /* Iterate all the codecs supported over SCO and populate |
| 1157 | * codec data |
| 1158 | */ |
| 1159 | hci_dev_lock(hdev); |
| 1160 | list_for_each_entry(c, &hdev->local_codecs, list) { |
| 1161 | if (c->transport != HCI_TRANSPORT_SCO_ESCO) |
| 1162 | continue; |
| 1163 | |
| 1164 | codec.id = c->id; |
| 1165 | codec.cid = c->cid; |
| 1166 | codec.vid = c->vid; |
| 1167 | err = hdev->get_data_path_id(hdev, &codec.data_path); |
| 1168 | if (err < 0) |
| 1169 | break; |
| 1170 | codec.num_caps = c->num_caps; |
| 1171 | if (copy_to_user(ptr, &codec, sizeof(codec))) { |
| 1172 | err = -EFAULT; |
| 1173 | break; |
| 1174 | } |
| 1175 | ptr += sizeof(codec); |
| 1176 | |
| 1177 | /* find codec capabilities data length */ |
| 1178 | len = 0; |
| 1179 | for (i = 0, caps = c->caps; i < c->num_caps; i++) { |
| 1180 | len += 1 + caps->len; |
| 1181 | caps = (void *)&caps->data[caps->len]; |
| 1182 | } |
| 1183 | |
| 1184 | /* copy codec capabilities data */ |
| 1185 | if (len && copy_to_user(ptr, c->caps, len)) { |
| 1186 | err = -EFAULT; |
| 1187 | break; |
| 1188 | } |
| 1189 | ptr += len; |
| 1190 | } |
| 1191 | |
| 1192 | if (!err && put_user(buf_len, optlen)) |
| 1193 | err = -EFAULT; |
| 1194 | |
| 1195 | hci_dev_unlock(hdev); |
| 1196 | hci_dev_put(hdev); |
| 1197 | |
| 1198 | break; |
| 1199 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 1200 | default: |
| 1201 | err = -ENOPROTOOPT; |
| 1202 | break; |
| 1203 | } |
| 1204 | |
| 1205 | release_sock(sk); |
| 1206 | return err; |
| 1207 | } |
| 1208 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 1209 | static int sco_sock_shutdown(struct socket *sock, int how) |
| 1210 | { |
| 1211 | struct sock *sk = sock->sk; |
| 1212 | int err = 0; |
| 1213 | |
| 1214 | BT_DBG("sock %p, sk %p", sock, sk); |
| 1215 | |
| 1216 | if (!sk) |
| 1217 | return 0; |
| 1218 | |
Kuba Pawlak | 1da5537 | 2015-10-05 18:44:17 +0200 | [diff] [blame] | 1219 | sock_hold(sk); |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 1220 | lock_sock(sk); |
Kuba Pawlak | 1da5537 | 2015-10-05 18:44:17 +0200 | [diff] [blame] | 1221 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 1222 | if (!sk->sk_shutdown) { |
| 1223 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 1224 | sco_sock_clear_timer(sk); |
| 1225 | __sco_sock_close(sk); |
| 1226 | |
Vladimir Davydov | 093facf | 2014-07-15 12:25:28 +0400 | [diff] [blame] | 1227 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime && |
| 1228 | !(current->flags & PF_EXITING)) |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 1229 | err = bt_sock_wait_state(sk, BT_CLOSED, |
Gustavo Padovan | be7c2b9 | 2012-05-17 00:36:21 -0300 | [diff] [blame] | 1230 | sk->sk_lingertime); |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 1231 | } |
Kuba Pawlak | 1da5537 | 2015-10-05 18:44:17 +0200 | [diff] [blame] | 1232 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 1233 | release_sock(sk); |
Kuba Pawlak | 1da5537 | 2015-10-05 18:44:17 +0200 | [diff] [blame] | 1234 | sock_put(sk); |
| 1235 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 1236 | return err; |
| 1237 | } |
| 1238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | static int sco_sock_release(struct socket *sock) |
| 1240 | { |
| 1241 | struct sock *sk = sock->sk; |
| 1242 | int err = 0; |
| 1243 | |
| 1244 | BT_DBG("sock %p, sk %p", sock, sk); |
| 1245 | |
| 1246 | if (!sk) |
| 1247 | return 0; |
| 1248 | |
| 1249 | sco_sock_close(sk); |
| 1250 | |
Vladimir Davydov | 093facf | 2014-07-15 12:25:28 +0400 | [diff] [blame] | 1251 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime && |
| 1252 | !(current->flags & PF_EXITING)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | lock_sock(sk); |
| 1254 | err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime); |
| 1255 | release_sock(sk); |
| 1256 | } |
| 1257 | |
| 1258 | sock_orphan(sk); |
| 1259 | sco_sock_kill(sk); |
| 1260 | return err; |
| 1261 | } |
| 1262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | static void sco_conn_ready(struct sco_conn *conn) |
| 1264 | { |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 1265 | struct sock *parent; |
| 1266 | struct sock *sk = conn->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
| 1268 | BT_DBG("conn %p", conn); |
| 1269 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 1270 | if (sk) { |
Desmond Cheong Zhi Xi | 27c24fd | 2021-08-10 12:14:07 +0800 | [diff] [blame] | 1271 | lock_sock(sk); |
Desmond Cheong Zhi Xi | 3f2c89f | 2021-08-10 12:14:08 +0800 | [diff] [blame] | 1272 | sco_sock_clear_timer(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | sk->sk_state = BT_CONNECTED; |
| 1274 | sk->sk_state_change(sk); |
Desmond Cheong Zhi Xi | 27c24fd | 2021-08-10 12:14:07 +0800 | [diff] [blame] | 1275 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | } else { |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 1277 | sco_conn_lock(conn); |
| 1278 | |
Kuba Pawlak | 2c501cd | 2015-10-26 16:17:14 +0000 | [diff] [blame] | 1279 | if (!conn->hcon) { |
| 1280 | sco_conn_unlock(conn); |
| 1281 | return; |
| 1282 | } |
| 1283 | |
Marcel Holtmann | 041987c | 2013-10-13 10:15:22 -0700 | [diff] [blame] | 1284 | parent = sco_get_sock_listen(&conn->hcon->src); |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 1285 | if (!parent) { |
| 1286 | sco_conn_unlock(conn); |
| 1287 | return; |
| 1288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | |
Desmond Cheong Zhi Xi | 27c24fd | 2021-08-10 12:14:07 +0800 | [diff] [blame] | 1290 | lock_sock(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 1292 | sk = sco_sock_alloc(sock_net(parent), NULL, |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 1293 | BTPROTO_SCO, GFP_ATOMIC, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | if (!sk) { |
Desmond Cheong Zhi Xi | 27c24fd | 2021-08-10 12:14:07 +0800 | [diff] [blame] | 1295 | release_sock(parent); |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 1296 | sco_conn_unlock(conn); |
| 1297 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | sco_sock_init(sk, parent); |
| 1301 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 1302 | bacpy(&sco_pi(sk)->src, &conn->hcon->src); |
| 1303 | bacpy(&sco_pi(sk)->dst, &conn->hcon->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
| 1305 | hci_conn_hold(conn->hcon); |
| 1306 | __sco_chan_add(conn, sk, parent); |
| 1307 | |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 1308 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) |
| 1309 | sk->sk_state = BT_CONNECT2; |
| 1310 | else |
| 1311 | sk->sk_state = BT_CONNECTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | |
| 1313 | /* Wake up parent */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 1314 | parent->sk_data_ready(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
Desmond Cheong Zhi Xi | 27c24fd | 2021-08-10 12:14:07 +0800 | [diff] [blame] | 1316 | release_sock(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 1318 | sco_conn_unlock(conn); |
| 1319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | /* ----- SCO interface with lower layer (HCI) ----- */ |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 1323 | int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | { |
Gustavo Padovan | fc5fef6 | 2012-05-23 04:04:19 -0300 | [diff] [blame] | 1325 | struct sock *sk; |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1326 | int lm = 0; |
| 1327 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1328 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1330 | /* Find listening sockets */ |
| 1331 | read_lock(&sco_sk_list.lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1332 | sk_for_each(sk, &sco_sk_list.head) { |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1333 | if (sk->sk_state != BT_LISTEN) |
| 1334 | continue; |
| 1335 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 1336 | if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) || |
| 1337 | !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) { |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1338 | lm |= HCI_LM_ACCEPT; |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 1339 | |
| 1340 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) |
| 1341 | *flags |= HCI_PROTO_DEFER; |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1342 | break; |
| 1343 | } |
| 1344 | } |
| 1345 | read_unlock(&sco_sk_list.lock); |
| 1346 | |
| 1347 | return lm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1350 | static void sco_connect_cfm(struct hci_conn *hcon, __u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | { |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1352 | if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK) |
| 1353 | return; |
| 1354 | |
Kai Ye | 79dbeafe | 2021-06-03 15:41:04 +0800 | [diff] [blame] | 1355 | BT_DBG("hcon %p bdaddr %pMR status %u", hcon, &hcon->dst, status); |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | if (!status) { |
| 1358 | struct sco_conn *conn; |
| 1359 | |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 1360 | conn = sco_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | if (conn) |
| 1362 | sco_conn_ready(conn); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1363 | } else |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 1364 | sco_conn_del(hcon, bt_to_errno(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Johan Hedberg | 3a6d576 | 2015-02-18 14:53:58 +0200 | [diff] [blame] | 1367 | static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | { |
Johan Hedberg | 3a6d576 | 2015-02-18 14:53:58 +0200 | [diff] [blame] | 1369 | if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK) |
| 1370 | return; |
| 1371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 1373 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 1374 | sco_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
Arron Wang | 9b4c333 | 2015-06-09 17:47:22 +0800 | [diff] [blame] | 1377 | void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | { |
| 1379 | struct sco_conn *conn = hcon->sco_data; |
| 1380 | |
| 1381 | if (!conn) |
| 1382 | goto drop; |
| 1383 | |
Kai Ye | 79dbeafe | 2021-06-03 15:41:04 +0800 | [diff] [blame] | 1384 | BT_DBG("conn %p len %u", conn, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | |
| 1386 | if (skb->len) { |
| 1387 | sco_recv_frame(conn, skb); |
Arron Wang | 9b4c333 | 2015-06-09 17:47:22 +0800 | [diff] [blame] | 1388 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | drop: |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1392 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1395 | static struct hci_cb sco_cb = { |
| 1396 | .name = "SCO", |
| 1397 | .connect_cfm = sco_connect_cfm, |
Johan Hedberg | 3a6d576 | 2015-02-18 14:53:58 +0200 | [diff] [blame] | 1398 | .disconn_cfm = sco_disconn_cfm, |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1399 | }; |
| 1400 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1401 | static int sco_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | { |
| 1403 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
Gustavo F. Padovan | ee65d19 | 2011-12-27 15:28:46 -0200 | [diff] [blame] | 1405 | read_lock(&sco_sk_list.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1407 | sk_for_each(sk, &sco_sk_list.head) { |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 1408 | seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src, |
| 1409 | &sco_pi(sk)->dst, sk->sk_state); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 1410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | |
Gustavo F. Padovan | ee65d19 | 2011-12-27 15:28:46 -0200 | [diff] [blame] | 1412 | read_unlock(&sco_sk_list.lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 1413 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1414 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Yangtao Li | 8e2924e | 2018-11-05 09:56:19 -0500 | [diff] [blame] | 1417 | DEFINE_SHOW_ATTRIBUTE(sco_debugfs); |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1418 | |
| 1419 | static struct dentry *sco_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1421 | static const struct proto_ops sco_sock_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | .family = PF_BLUETOOTH, |
| 1423 | .owner = THIS_MODULE, |
| 1424 | .release = sco_sock_release, |
| 1425 | .bind = sco_sock_bind, |
| 1426 | .connect = sco_sock_connect, |
| 1427 | .listen = sco_sock_listen, |
| 1428 | .accept = sco_sock_accept, |
| 1429 | .getname = sco_sock_getname, |
| 1430 | .sendmsg = sco_sock_sendmsg, |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 1431 | .recvmsg = sco_sock_recvmsg, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1432 | .poll = bt_sock_poll, |
Marcel Holtmann | 3241ad8 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 1433 | .ioctl = bt_sock_ioctl, |
Arnd Bergmann | c7cbdbf | 2019-04-17 22:51:48 +0200 | [diff] [blame] | 1434 | .gettstamp = sock_gettstamp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | .mmap = sock_no_mmap, |
| 1436 | .socketpair = sock_no_socketpair, |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 1437 | .shutdown = sco_sock_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | .setsockopt = sco_sock_setsockopt, |
| 1439 | .getsockopt = sco_sock_getsockopt |
| 1440 | }; |
| 1441 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 1442 | static const struct net_proto_family sco_sock_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | .family = PF_BLUETOOTH, |
| 1444 | .owner = THIS_MODULE, |
| 1445 | .create = sco_sock_create, |
| 1446 | }; |
| 1447 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 1448 | int __init sco_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | { |
| 1450 | int err; |
| 1451 | |
Marcel Holtmann | 15762fa | 2015-01-11 15:21:06 -0800 | [diff] [blame] | 1452 | BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr)); |
| 1453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | err = proto_register(&sco_proto, 0); |
| 1455 | if (err < 0) |
| 1456 | return err; |
| 1457 | |
| 1458 | err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops); |
| 1459 | if (err < 0) { |
| 1460 | BT_ERR("SCO socket registration failed"); |
| 1461 | goto error; |
| 1462 | } |
| 1463 | |
Al Viro | b031661 | 2013-04-04 19:14:33 -0400 | [diff] [blame] | 1464 | err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL); |
Masatake YAMATO | de9b921 | 2012-07-26 01:30:12 +0900 | [diff] [blame] | 1465 | if (err < 0) { |
| 1466 | BT_ERR("Failed to create SCO proc file"); |
| 1467 | bt_sock_unregister(BTPROTO_SCO); |
| 1468 | goto error; |
| 1469 | } |
| 1470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | BT_INFO("SCO socket layer initialized"); |
| 1472 | |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1473 | hci_register_cb(&sco_cb); |
| 1474 | |
Marcel Holtmann | 1120e4b | 2013-10-17 17:24:16 -0700 | [diff] [blame] | 1475 | if (IS_ERR_OR_NULL(bt_debugfs)) |
| 1476 | return 0; |
| 1477 | |
| 1478 | sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs, |
| 1479 | NULL, &sco_debugfs_fops); |
| 1480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | return 0; |
| 1482 | |
| 1483 | error: |
| 1484 | proto_unregister(&sco_proto); |
| 1485 | return err; |
| 1486 | } |
| 1487 | |
Alexander Aring | 0402d9f2 | 2015-03-07 20:52:28 +0100 | [diff] [blame] | 1488 | void sco_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | { |
Masatake YAMATO | de9b921 | 2012-07-26 01:30:12 +0900 | [diff] [blame] | 1490 | bt_procfs_cleanup(&init_net, "sco"); |
| 1491 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1492 | debugfs_remove(sco_debugfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1494 | hci_unregister_cb(&sco_cb); |
| 1495 | |
David Herrmann | 5e9d7f8 | 2013-02-24 19:36:51 +0100 | [diff] [blame] | 1496 | bt_sock_unregister(BTPROTO_SCO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | proto_unregister(&sco_proto); |
| 1499 | } |
| 1500 | |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 1501 | module_param(disable_esco, bool, 0644); |
| 1502 | MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation"); |