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 | |
| 51 | unsigned int mtu; |
| 52 | }; |
| 53 | |
| 54 | #define sco_conn_lock(c) spin_lock(&c->lock); |
| 55 | #define sco_conn_unlock(c) spin_unlock(&c->lock); |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static void sco_sock_close(struct sock *sk); |
| 58 | static void sco_sock_kill(struct sock *sk); |
| 59 | |
Marcel Holtmann | 2a0dccb | 2014-07-11 06:19:41 +0200 | [diff] [blame] | 60 | /* ----- SCO socket info ----- */ |
| 61 | #define sco_pi(sk) ((struct sco_pinfo *) sk) |
| 62 | |
| 63 | struct sco_pinfo { |
| 64 | struct bt_sock bt; |
| 65 | bdaddr_t src; |
| 66 | bdaddr_t dst; |
| 67 | __u32 flags; |
| 68 | __u16 setting; |
| 69 | struct sco_conn *conn; |
| 70 | }; |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* ---- SCO timers ---- */ |
Marcel Holtmann | 068d69e | 2014-07-11 06:19:44 +0200 | [diff] [blame] | 73 | #define SCO_CONN_TIMEOUT (HZ * 40) |
| 74 | #define SCO_DISCONN_TIMEOUT (HZ * 2) |
| 75 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 76 | static void sco_sock_timeout(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 78 | struct sock *sk = from_timer(sk, t, sk_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 81 | |
| 82 | bh_lock_sock(sk); |
| 83 | sk->sk_err = ETIMEDOUT; |
| 84 | sk->sk_state_change(sk); |
| 85 | bh_unlock_sock(sk); |
| 86 | |
| 87 | sco_sock_kill(sk); |
| 88 | sock_put(sk); |
| 89 | } |
| 90 | |
| 91 | static void sco_sock_set_timer(struct sock *sk, long timeout) |
| 92 | { |
| 93 | BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout); |
| 94 | sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout); |
| 95 | } |
| 96 | |
| 97 | static void sco_sock_clear_timer(struct sock *sk) |
| 98 | { |
| 99 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 100 | sk_stop_timer(sk, &sk->sk_timer); |
| 101 | } |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* ---- SCO connections ---- */ |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 104 | static struct sco_conn *sco_conn_add(struct hci_conn *hcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | struct hci_dev *hdev = hcon->hdev; |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 107 | struct sco_conn *conn = hcon->sco_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 109 | if (conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return conn; |
| 111 | |
Claudio Takahasi | c10cc5a | 2013-04-11 11:35:45 -0300 | [diff] [blame] | 112 | conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL); |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 113 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | spin_lock_init(&conn->lock); |
| 117 | |
| 118 | hcon->sco_data = conn; |
| 119 | conn->hcon = hcon; |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | if (hdev->sco_mtu > 0) |
| 122 | conn->mtu = hdev->sco_mtu; |
| 123 | else |
| 124 | conn->mtu = 60; |
| 125 | |
| 126 | BT_DBG("hcon %p conn %p", hcon, conn); |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | return conn; |
| 129 | } |
| 130 | |
Marcel Holtmann | e03ab51 | 2014-07-13 19:54:49 +0200 | [diff] [blame] | 131 | /* Delete channel. |
| 132 | * Must be called on the locked socket. */ |
| 133 | static void sco_chan_del(struct sock *sk, int err) |
| 134 | { |
| 135 | struct sco_conn *conn; |
| 136 | |
| 137 | conn = sco_pi(sk)->conn; |
| 138 | |
| 139 | BT_DBG("sk %p, conn %p, err %d", sk, conn, err); |
| 140 | |
| 141 | if (conn) { |
| 142 | sco_conn_lock(conn); |
| 143 | conn->sk = NULL; |
| 144 | sco_pi(sk)->conn = NULL; |
| 145 | sco_conn_unlock(conn); |
| 146 | |
| 147 | if (conn->hcon) |
| 148 | hci_conn_drop(conn->hcon); |
| 149 | } |
| 150 | |
| 151 | sk->sk_state = BT_CLOSED; |
| 152 | sk->sk_err = err; |
| 153 | sk->sk_state_change(sk); |
| 154 | |
| 155 | sock_set_flag(sk, SOCK_ZAPPED); |
| 156 | } |
| 157 | |
Nicholas Krause | df94536 | 2015-08-18 21:23:01 -0400 | [diff] [blame] | 158 | static void sco_conn_del(struct hci_conn *hcon, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 160 | struct sco_conn *conn = hcon->sco_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | struct sock *sk; |
| 162 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 163 | if (!conn) |
Nicholas Krause | df94536 | 2015-08-18 21:23:01 -0400 | [diff] [blame] | 164 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 167 | |
| 168 | /* Kill socket */ |
Marcel Holtmann | eb5a4de | 2014-07-14 01:30:15 +0200 | [diff] [blame] | 169 | sco_conn_lock(conn); |
| 170 | sk = conn->sk; |
| 171 | sco_conn_unlock(conn); |
| 172 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 173 | if (sk) { |
Kuba Pawlak | 75e34f5 | 2015-10-05 18:44:15 +0200 | [diff] [blame] | 174 | sock_hold(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | bh_lock_sock(sk); |
| 176 | sco_sock_clear_timer(sk); |
| 177 | sco_chan_del(sk, err); |
| 178 | bh_unlock_sock(sk); |
| 179 | sco_sock_kill(sk); |
Kuba Pawlak | 75e34f5 | 2015-10-05 18:44:15 +0200 | [diff] [blame] | 180 | sock_put(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | hcon->sco_data = NULL; |
| 184 | kfree(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 187 | static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, |
| 188 | struct sock *parent) |
Marcel Holtmann | 015b01c | 2014-07-13 19:54:48 +0200 | [diff] [blame] | 189 | { |
| 190 | BT_DBG("conn %p", conn); |
| 191 | |
| 192 | sco_pi(sk)->conn = conn; |
| 193 | conn->sk = sk; |
| 194 | |
| 195 | if (parent) |
Matthias Kaehlcke | c4f5627 | 2019-01-02 16:11:20 -0800 | [diff] [blame] | 196 | bt_accept_enqueue(parent, sk, true); |
Marcel Holtmann | 015b01c | 2014-07-13 19:54:48 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 199 | static int sco_chan_add(struct sco_conn *conn, struct sock *sk, |
| 200 | struct sock *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
| 202 | int err = 0; |
| 203 | |
| 204 | sco_conn_lock(conn); |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 205 | if (conn->sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | err = -EBUSY; |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 207 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | __sco_chan_add(conn, sk, parent); |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | sco_conn_unlock(conn); |
| 211 | return err; |
| 212 | } |
| 213 | |
| 214 | static int sco_connect(struct sock *sk) |
| 215 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | struct sco_conn *conn; |
| 217 | struct hci_conn *hcon; |
| 218 | struct hci_dev *hdev; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 219 | int err, type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 221 | BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Johan Hedberg | 39385cb | 2016-11-12 17:03:07 +0200 | [diff] [blame] | 223 | hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src, BDADDR_BREDR); |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 224 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | return -EHOSTUNREACH; |
| 226 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 227 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 229 | if (lmp_esco_capable(hdev) && !disable_esco) |
| 230 | type = ESCO_LINK; |
| 231 | else |
| 232 | type = SCO_LINK; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 233 | |
Frédéric Dalleau | 79dc008 | 2013-08-19 14:24:01 +0200 | [diff] [blame] | 234 | if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT && |
| 235 | (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev))) { |
| 236 | err = -EOPNOTSUPP; |
| 237 | goto done; |
| 238 | } |
| 239 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 240 | hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst, |
Marcel Holtmann | 041987c | 2013-10-13 10:15:22 -0700 | [diff] [blame] | 241 | sco_pi(sk)->setting); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 242 | if (IS_ERR(hcon)) { |
| 243 | err = PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | goto done; |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 247 | conn = sco_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | if (!conn) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 249 | hci_conn_drop(hcon); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 250 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | goto done; |
| 252 | } |
| 253 | |
| 254 | /* Update source addr of the socket */ |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 255 | bacpy(&sco_pi(sk)->src, &hcon->src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | err = sco_chan_add(conn, sk, NULL); |
| 258 | if (err) |
| 259 | goto done; |
| 260 | |
| 261 | if (hcon->state == BT_CONNECTED) { |
| 262 | sco_sock_clear_timer(sk); |
| 263 | sk->sk_state = BT_CONNECTED; |
| 264 | } else { |
| 265 | sk->sk_state = BT_CONNECT; |
| 266 | sco_sock_set_timer(sk, sk->sk_sndtimeo); |
| 267 | } |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 270 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | hci_dev_put(hdev); |
| 272 | return err; |
| 273 | } |
| 274 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 275 | static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | struct sco_conn *conn = sco_pi(sk)->conn; |
| 278 | struct sk_buff *skb; |
Mikel Astiz | 088ce08 | 2012-04-11 08:48:48 +0200 | [diff] [blame] | 279 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
| 281 | /* Check outgoing MTU */ |
| 282 | if (len > conn->mtu) |
| 283 | return -EINVAL; |
| 284 | |
| 285 | BT_DBG("sk %p len %d", sk, len); |
| 286 | |
Mikel Astiz | 088ce08 | 2012-04-11 08:48:48 +0200 | [diff] [blame] | 287 | skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err); |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 288 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | return err; |
| 290 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 291 | if (memcpy_from_msg(skb_put(skb, len), msg, len)) { |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 292 | kfree_skb(skb); |
| 293 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Gustavo F. Padovan | 0d861d8 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 296 | hci_send_sco(conn->hcon, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Mikel Astiz | 088ce08 | 2012-04-11 08:48:48 +0200 | [diff] [blame] | 298 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 301 | 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] | 302 | { |
Marcel Holtmann | eb5a4de | 2014-07-14 01:30:15 +0200 | [diff] [blame] | 303 | struct sock *sk; |
| 304 | |
| 305 | sco_conn_lock(conn); |
| 306 | sk = conn->sk; |
| 307 | sco_conn_unlock(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | if (!sk) |
| 310 | goto drop; |
| 311 | |
| 312 | BT_DBG("sk %p len %d", sk, skb->len); |
| 313 | |
| 314 | if (sk->sk_state != BT_CONNECTED) |
| 315 | goto drop; |
| 316 | |
| 317 | if (!sock_queue_rcv_skb(sk, skb)) |
| 318 | return; |
| 319 | |
| 320 | drop: |
| 321 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* -------- Socket interface ---------- */ |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 325 | static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 327 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 329 | sk_for_each(sk, &sco_sk_list.head) { |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 330 | if (sk->sk_state != BT_LISTEN) |
| 331 | continue; |
| 332 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 333 | if (!bacmp(&sco_pi(sk)->src, ba)) |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 334 | return sk; |
| 335 | } |
| 336 | |
| 337 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* Find socket listening on source bdaddr. |
| 341 | * Returns closest match. |
| 342 | */ |
| 343 | static struct sock *sco_get_sock_listen(bdaddr_t *src) |
| 344 | { |
| 345 | struct sock *sk = NULL, *sk1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
| 347 | read_lock(&sco_sk_list.lock); |
| 348 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 349 | sk_for_each(sk, &sco_sk_list.head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if (sk->sk_state != BT_LISTEN) |
| 351 | continue; |
| 352 | |
| 353 | /* Exact match. */ |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 354 | if (!bacmp(&sco_pi(sk)->src, src)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | break; |
| 356 | |
| 357 | /* Closest match */ |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 358 | if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | sk1 = sk; |
| 360 | } |
| 361 | |
| 362 | read_unlock(&sco_sk_list.lock); |
| 363 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 364 | return sk ? sk : sk1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static void sco_sock_destruct(struct sock *sk) |
| 368 | { |
| 369 | BT_DBG("sk %p", sk); |
| 370 | |
| 371 | skb_queue_purge(&sk->sk_receive_queue); |
| 372 | skb_queue_purge(&sk->sk_write_queue); |
| 373 | } |
| 374 | |
| 375 | static void sco_sock_cleanup_listen(struct sock *parent) |
| 376 | { |
| 377 | struct sock *sk; |
| 378 | |
| 379 | BT_DBG("parent %p", parent); |
| 380 | |
| 381 | /* Close not yet accepted channels */ |
| 382 | while ((sk = bt_accept_dequeue(parent, NULL))) { |
| 383 | sco_sock_close(sk); |
| 384 | sco_sock_kill(sk); |
| 385 | } |
| 386 | |
| 387 | parent->sk_state = BT_CLOSED; |
| 388 | sock_set_flag(parent, SOCK_ZAPPED); |
| 389 | } |
| 390 | |
| 391 | /* Kill socket (only if zapped and orphan) |
| 392 | * Must be called on unlocked socket. |
| 393 | */ |
| 394 | static void sco_sock_kill(struct sock *sk) |
| 395 | { |
Sudip Mukherjee | 4e1a720 | 2018-07-15 20:36:50 +0100 | [diff] [blame] | 396 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket || |
| 397 | sock_flag(sk, SOCK_DEAD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | return; |
| 399 | |
| 400 | BT_DBG("sk %p state %d", sk, sk->sk_state); |
| 401 | |
| 402 | /* Kill poor orphan */ |
| 403 | bt_sock_unlink(&sco_sk_list, sk); |
| 404 | sock_set_flag(sk, SOCK_DEAD); |
| 405 | sock_put(sk); |
| 406 | } |
| 407 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 408 | static void __sco_sock_close(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 410 | 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] | 411 | |
| 412 | switch (sk->sk_state) { |
| 413 | case BT_LISTEN: |
| 414 | sco_sock_cleanup_listen(sk); |
| 415 | break; |
| 416 | |
| 417 | case BT_CONNECTED: |
| 418 | case BT_CONFIG: |
Gustavo Padovan | b7e98b5 | 2013-01-03 19:59:28 -0200 | [diff] [blame] | 419 | if (sco_pi(sk)->conn->hcon) { |
Luiz Augusto von Dentz | 4a77708 | 2011-05-12 11:13:15 +0300 | [diff] [blame] | 420 | sk->sk_state = BT_DISCONN; |
| 421 | sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT); |
Kuba Pawlak | 435c513 | 2015-10-05 18:44:16 +0200 | [diff] [blame] | 422 | sco_conn_lock(sco_pi(sk)->conn); |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 423 | hci_conn_drop(sco_pi(sk)->conn->hcon); |
Luiz Augusto von Dentz | 4a77708 | 2011-05-12 11:13:15 +0300 | [diff] [blame] | 424 | sco_pi(sk)->conn->hcon = NULL; |
Kuba Pawlak | 435c513 | 2015-10-05 18:44:16 +0200 | [diff] [blame] | 425 | sco_conn_unlock(sco_pi(sk)->conn); |
Luiz Augusto von Dentz | 4a77708 | 2011-05-12 11:13:15 +0300 | [diff] [blame] | 426 | } else |
| 427 | sco_chan_del(sk, ECONNRESET); |
| 428 | break; |
| 429 | |
Vinicius Costa Gomes | eb20ff9 | 2013-03-13 19:46:20 -0300 | [diff] [blame] | 430 | case BT_CONNECT2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | case BT_CONNECT: |
| 432 | case BT_DISCONN: |
| 433 | sco_chan_del(sk, ECONNRESET); |
| 434 | break; |
| 435 | |
| 436 | default: |
| 437 | sock_set_flag(sk, SOCK_ZAPPED); |
| 438 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 439 | } |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 442 | /* Must be called on unlocked socket. */ |
| 443 | static void sco_sock_close(struct sock *sk) |
| 444 | { |
| 445 | sco_sock_clear_timer(sk); |
| 446 | lock_sock(sk); |
| 447 | __sco_sock_close(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | sco_sock_kill(sk); |
| 450 | } |
| 451 | |
| 452 | static void sco_sock_init(struct sock *sk, struct sock *parent) |
| 453 | { |
| 454 | BT_DBG("sk %p", sk); |
| 455 | |
Paul Moore | 6230c9b | 2011-10-07 09:40:59 +0000 | [diff] [blame] | 456 | if (parent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | sk->sk_type = parent->sk_type; |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 458 | bt_sk(sk)->flags = bt_sk(parent)->flags; |
Paul Moore | 6230c9b | 2011-10-07 09:40:59 +0000 | [diff] [blame] | 459 | security_sk_clone(parent, sk); |
| 460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static struct proto sco_proto = { |
| 464 | .name = "SCO", |
| 465 | .owner = THIS_MODULE, |
| 466 | .obj_size = sizeof(struct sco_pinfo) |
| 467 | }; |
| 468 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 469 | static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, |
| 470 | int proto, gfp_t prio, int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
| 472 | struct sock *sk; |
| 473 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 474 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, kern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | if (!sk) |
| 476 | return NULL; |
| 477 | |
| 478 | sock_init_data(sock, sk); |
| 479 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 480 | |
| 481 | sk->sk_destruct = sco_sock_destruct; |
| 482 | sk->sk_sndtimeo = SCO_CONN_TIMEOUT; |
| 483 | |
| 484 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 485 | |
| 486 | sk->sk_protocol = proto; |
| 487 | sk->sk_state = BT_OPEN; |
| 488 | |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 489 | sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT; |
| 490 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 491 | timer_setup(&sk->sk_timer, sco_sock_timeout, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
| 493 | bt_sock_link(&sco_sk_list, sk); |
| 494 | return sk; |
| 495 | } |
| 496 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 497 | static int sco_sock_create(struct net *net, struct socket *sock, int protocol, |
| 498 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
| 500 | struct sock *sk; |
| 501 | |
| 502 | BT_DBG("sock %p", sock); |
| 503 | |
| 504 | sock->state = SS_UNCONNECTED; |
| 505 | |
| 506 | if (sock->type != SOCK_SEQPACKET) |
| 507 | return -ESOCKTNOSUPPORT; |
| 508 | |
| 509 | sock->ops = &sco_sock_ops; |
| 510 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 511 | sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern); |
Marcel Holtmann | 74da626 | 2006-10-15 17:31:14 +0200 | [diff] [blame] | 512 | if (!sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | return -ENOMEM; |
| 514 | |
| 515 | sco_sock_init(sk, NULL); |
| 516 | return 0; |
| 517 | } |
| 518 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 519 | static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, |
| 520 | int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | { |
| 522 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 523 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | int err = 0; |
| 525 | |
Mateusz Jurczyk | d2ecfa7 | 2017-06-29 14:04:59 +0200 | [diff] [blame] | 526 | if (!addr || addr_len < sizeof(struct sockaddr_sco) || |
| 527 | addr->sa_family != AF_BLUETOOTH) |
David S. Miller | 5233252 | 2015-12-15 15:39:08 -0500 | [diff] [blame] | 528 | return -EINVAL; |
| 529 | |
Tetsuo Handa | bd7d46d | 2019-04-12 19:54:33 +0900 | [diff] [blame] | 530 | BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr); |
| 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | lock_sock(sk); |
| 533 | |
| 534 | if (sk->sk_state != BT_OPEN) { |
| 535 | err = -EBADFD; |
| 536 | goto done; |
| 537 | } |
| 538 | |
Marcel Holtmann | 8ed21f7 | 2012-04-19 13:43:53 +0200 | [diff] [blame] | 539 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 540 | err = -EINVAL; |
| 541 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 544 | bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr); |
Marcel Holtmann | 8ed21f7 | 2012-04-19 13:43:53 +0200 | [diff] [blame] | 545 | |
| 546 | sk->sk_state = BT_BOUND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | done: |
| 549 | release_sock(sk); |
| 550 | return err; |
| 551 | } |
| 552 | |
| 553 | static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) |
| 554 | { |
| 555 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 556 | struct sock *sk = sock->sk; |
Claudio Takahasi | 92f185c | 2013-04-11 11:35:46 -0300 | [diff] [blame] | 557 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | BT_DBG("sk %p", sk); |
| 560 | |
Changli Gao | 6503d96 | 2010-03-31 22:58:26 +0000 | [diff] [blame] | 561 | if (alen < sizeof(struct sockaddr_sco) || |
| 562 | addr->sa_family != AF_BLUETOOTH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | return -EINVAL; |
| 564 | |
| 565 | if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) |
| 566 | return -EBADFD; |
| 567 | |
| 568 | if (sk->sk_type != SOCK_SEQPACKET) |
| 569 | return -EINVAL; |
| 570 | |
| 571 | lock_sock(sk); |
| 572 | |
| 573 | /* Set destination address and psm */ |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 574 | bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 576 | err = sco_connect(sk); |
| 577 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | goto done; |
| 579 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 580 | err = bt_sock_wait_state(sk, BT_CONNECTED, |
Gustavo Padovan | be7c2b9 | 2012-05-17 00:36:21 -0300 | [diff] [blame] | 581 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
| 583 | done: |
| 584 | release_sock(sk); |
| 585 | return err; |
| 586 | } |
| 587 | |
| 588 | static int sco_sock_listen(struct socket *sock, int backlog) |
| 589 | { |
| 590 | struct sock *sk = sock->sk; |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 591 | bdaddr_t *src = &sco_pi(sk)->src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | int err = 0; |
| 593 | |
| 594 | BT_DBG("sk %p backlog %d", sk, backlog); |
| 595 | |
| 596 | lock_sock(sk); |
| 597 | |
Marcel Holtmann | 7d5d775 | 2012-04-19 13:43:52 +0200 | [diff] [blame] | 598 | if (sk->sk_state != BT_BOUND) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | err = -EBADFD; |
| 600 | goto done; |
| 601 | } |
| 602 | |
Marcel Holtmann | 7d5d775 | 2012-04-19 13:43:52 +0200 | [diff] [blame] | 603 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 604 | err = -EINVAL; |
| 605 | goto done; |
| 606 | } |
| 607 | |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 608 | write_lock(&sco_sk_list.lock); |
| 609 | |
| 610 | if (__sco_get_sock_listen_by_addr(src)) { |
| 611 | err = -EADDRINUSE; |
| 612 | goto unlock; |
| 613 | } |
| 614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | sk->sk_max_ack_backlog = backlog; |
| 616 | sk->sk_ack_backlog = 0; |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | sk->sk_state = BT_LISTEN; |
| 619 | |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 620 | unlock: |
| 621 | write_unlock(&sco_sk_list.lock); |
| 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | done: |
| 624 | release_sock(sk); |
| 625 | return err; |
| 626 | } |
| 627 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 628 | static int sco_sock_accept(struct socket *sock, struct socket *newsock, |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 629 | int flags, bool kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | { |
Peter Hurley | dfb2fae | 2015-01-23 12:16:53 -0500 | [diff] [blame] | 631 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | struct sock *sk = sock->sk, *ch; |
| 633 | long timeo; |
| 634 | int err = 0; |
| 635 | |
| 636 | lock_sock(sk); |
| 637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 639 | |
| 640 | BT_DBG("sk %p timeo %ld", sk, timeo); |
| 641 | |
| 642 | /* Wait for an incoming connection. (wake-one). */ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 643 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 644 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if (sk->sk_state != BT_LISTEN) { |
| 646 | err = -EBADFD; |
| 647 | break; |
| 648 | } |
| 649 | |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 650 | ch = bt_accept_dequeue(sk, newsock); |
| 651 | if (ch) |
| 652 | break; |
| 653 | |
| 654 | if (!timeo) { |
| 655 | err = -EAGAIN; |
| 656 | break; |
| 657 | } |
| 658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | if (signal_pending(current)) { |
| 660 | err = sock_intr_errno(timeo); |
| 661 | break; |
| 662 | } |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 663 | |
| 664 | release_sock(sk); |
Peter Hurley | dfb2fae | 2015-01-23 12:16:53 -0500 | [diff] [blame] | 665 | |
| 666 | timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo); |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 667 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 669 | remove_wait_queue(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
| 671 | if (err) |
| 672 | goto done; |
| 673 | |
| 674 | newsock->state = SS_CONNECTED; |
| 675 | |
| 676 | BT_DBG("new socket %p", ch); |
| 677 | |
| 678 | done: |
| 679 | release_sock(sk); |
| 680 | return err; |
| 681 | } |
| 682 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 683 | static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 684 | int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { |
| 686 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 687 | struct sock *sk = sock->sk; |
| 688 | |
| 689 | BT_DBG("sock %p, sk %p", sock, sk); |
| 690 | |
| 691 | addr->sa_family = AF_BLUETOOTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
| 693 | if (peer) |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 694 | bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | else |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 696 | bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 698 | return sizeof(struct sockaddr_sco); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 701 | static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg, |
| 702 | size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
| 704 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 705 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
| 707 | BT_DBG("sock %p, sk %p", sock, sk); |
| 708 | |
Benjamin LaHaise | c1cbe4b | 2005-12-13 23:22:19 -0800 | [diff] [blame] | 709 | err = sock_error(sk); |
| 710 | if (err) |
| 711 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
| 713 | if (msg->msg_flags & MSG_OOB) |
| 714 | return -EOPNOTSUPP; |
| 715 | |
| 716 | lock_sock(sk); |
| 717 | |
| 718 | if (sk->sk_state == BT_CONNECTED) |
| 719 | err = sco_send_frame(sk, msg, len); |
| 720 | else |
| 721 | err = -ENOTCONN; |
| 722 | |
| 723 | release_sock(sk); |
| 724 | return err; |
| 725 | } |
| 726 | |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 727 | 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] | 728 | { |
| 729 | struct hci_dev *hdev = conn->hdev; |
| 730 | |
| 731 | BT_DBG("conn %p", conn); |
| 732 | |
| 733 | conn->state = BT_CONFIG; |
| 734 | |
| 735 | if (!lmp_esco_capable(hdev)) { |
| 736 | struct hci_cp_accept_conn_req cp; |
| 737 | |
| 738 | bacpy(&cp.bdaddr, &conn->dst); |
Frédéric Dalleau | 33f2404 | 2013-08-19 14:23:55 +0200 | [diff] [blame] | 739 | cp.role = 0x00; /* Ignored */ |
Frédéric Dalleau | fa5513b | 2013-04-16 17:28:58 +0200 | [diff] [blame] | 740 | |
| 741 | hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp); |
| 742 | } else { |
| 743 | struct hci_cp_accept_sync_conn_req cp; |
| 744 | |
| 745 | bacpy(&cp.bdaddr, &conn->dst); |
| 746 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
| 747 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 748 | cp.tx_bandwidth = cpu_to_le32(0x00001f40); |
| 749 | cp.rx_bandwidth = cpu_to_le32(0x00001f40); |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 750 | cp.content_format = cpu_to_le16(setting); |
| 751 | |
| 752 | switch (setting & SCO_AIRMODE_MASK) { |
| 753 | case SCO_AIRMODE_TRANSP: |
| 754 | if (conn->pkt_type & ESCO_2EV3) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 755 | cp.max_latency = cpu_to_le16(0x0008); |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 756 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 757 | cp.max_latency = cpu_to_le16(0x000D); |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 758 | cp.retrans_effort = 0x02; |
| 759 | break; |
| 760 | case SCO_AIRMODE_CVSD: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 761 | cp.max_latency = cpu_to_le16(0xffff); |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 762 | cp.retrans_effort = 0xff; |
| 763 | break; |
| 764 | } |
Frédéric Dalleau | fa5513b | 2013-04-16 17:28:58 +0200 | [diff] [blame] | 765 | |
| 766 | hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, |
| 767 | sizeof(cp), &cp); |
| 768 | } |
| 769 | } |
| 770 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 771 | static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg, |
| 772 | size_t len, int flags) |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 773 | { |
| 774 | struct sock *sk = sock->sk; |
| 775 | struct sco_pinfo *pi = sco_pi(sk); |
| 776 | |
| 777 | lock_sock(sk); |
| 778 | |
| 779 | if (sk->sk_state == BT_CONNECT2 && |
| 780 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { |
Frédéric Dalleau | 2f69a82 | 2013-08-19 14:23:58 +0200 | [diff] [blame] | 781 | sco_conn_defer_accept(pi->conn->hcon, pi->setting); |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 782 | sk->sk_state = BT_CONFIG; |
| 783 | |
| 784 | release_sock(sk); |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | release_sock(sk); |
| 789 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 790 | return bt_sock_recvmsg(sock, msg, len, flags); |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 793 | static int sco_sock_setsockopt(struct socket *sock, int level, int optname, |
| 794 | char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | { |
| 796 | struct sock *sk = sock->sk; |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 797 | int len, err = 0; |
| 798 | struct bt_voice voice; |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 799 | u32 opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
| 801 | BT_DBG("sk %p", sk); |
| 802 | |
| 803 | lock_sock(sk); |
| 804 | |
| 805 | switch (optname) { |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 806 | |
| 807 | case BT_DEFER_SETUP: |
| 808 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 809 | err = -EINVAL; |
| 810 | break; |
| 811 | } |
| 812 | |
| 813 | if (get_user(opt, (u32 __user *) optval)) { |
| 814 | err = -EFAULT; |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | if (opt) |
| 819 | set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
| 820 | else |
| 821 | clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
| 822 | break; |
| 823 | |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 824 | case BT_VOICE: |
| 825 | if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND && |
| 826 | sk->sk_state != BT_CONNECT2) { |
| 827 | err = -EINVAL; |
| 828 | break; |
| 829 | } |
| 830 | |
| 831 | voice.setting = sco_pi(sk)->setting; |
| 832 | |
| 833 | len = min_t(unsigned int, sizeof(voice), optlen); |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 834 | if (copy_from_user((char *)&voice, optval, len)) { |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 835 | err = -EFAULT; |
| 836 | break; |
| 837 | } |
| 838 | |
| 839 | /* Explicitly check for these values */ |
| 840 | if (voice.setting != BT_VOICE_TRANSPARENT && |
| 841 | voice.setting != BT_VOICE_CVSD_16BIT) { |
| 842 | err = -EINVAL; |
| 843 | break; |
| 844 | } |
| 845 | |
| 846 | sco_pi(sk)->setting = voice.setting; |
| 847 | break; |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | default: |
| 850 | err = -ENOPROTOOPT; |
| 851 | break; |
| 852 | } |
| 853 | |
| 854 | release_sock(sk); |
| 855 | return err; |
| 856 | } |
| 857 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 858 | static int sco_sock_getsockopt_old(struct socket *sock, int optname, |
| 859 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
| 861 | struct sock *sk = sock->sk; |
| 862 | struct sco_options opts; |
| 863 | struct sco_conninfo cinfo; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 864 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
| 866 | BT_DBG("sk %p", sk); |
| 867 | |
| 868 | if (get_user(len, optlen)) |
| 869 | return -EFAULT; |
| 870 | |
| 871 | lock_sock(sk); |
| 872 | |
| 873 | switch (optname) { |
| 874 | case SCO_OPTIONS: |
Johan Hedberg | 9d225d2 | 2013-08-08 14:53:56 +0300 | [diff] [blame] | 875 | if (sk->sk_state != BT_CONNECTED && |
| 876 | !(sk->sk_state == BT_CONNECT2 && |
| 877 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | err = -ENOTCONN; |
| 879 | break; |
| 880 | } |
| 881 | |
| 882 | opts.mtu = sco_pi(sk)->conn->mtu; |
| 883 | |
| 884 | BT_DBG("mtu %d", opts.mtu); |
| 885 | |
| 886 | len = min_t(unsigned int, len, sizeof(opts)); |
| 887 | if (copy_to_user(optval, (char *)&opts, len)) |
| 888 | err = -EFAULT; |
| 889 | |
| 890 | break; |
| 891 | |
| 892 | case SCO_CONNINFO: |
Johan Hedberg | 9d225d2 | 2013-08-08 14:53:56 +0300 | [diff] [blame] | 893 | if (sk->sk_state != BT_CONNECTED && |
| 894 | !(sk->sk_state == BT_CONNECT2 && |
| 895 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | err = -ENOTCONN; |
| 897 | break; |
| 898 | } |
| 899 | |
Vasiliy Kulikov | c4c896e | 2011-02-14 13:54:26 +0300 | [diff] [blame] | 900 | memset(&cinfo, 0, sizeof(cinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle; |
| 902 | memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3); |
| 903 | |
| 904 | len = min_t(unsigned int, len, sizeof(cinfo)); |
| 905 | if (copy_to_user(optval, (char *)&cinfo, len)) |
| 906 | err = -EFAULT; |
| 907 | |
| 908 | break; |
| 909 | |
| 910 | default: |
| 911 | err = -ENOPROTOOPT; |
| 912 | break; |
| 913 | } |
| 914 | |
| 915 | release_sock(sk); |
| 916 | return err; |
| 917 | } |
| 918 | |
Marcel Holtmann | c4297e8 | 2015-10-26 02:08:38 +0100 | [diff] [blame] | 919 | static int sco_sock_getsockopt(struct socket *sock, int level, int optname, |
| 920 | char __user *optval, int __user *optlen) |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 921 | { |
| 922 | struct sock *sk = sock->sk; |
| 923 | int len, err = 0; |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 924 | struct bt_voice voice; |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 925 | |
| 926 | BT_DBG("sk %p", sk); |
| 927 | |
| 928 | if (level == SOL_SCO) |
| 929 | return sco_sock_getsockopt_old(sock, optname, optval, optlen); |
| 930 | |
| 931 | if (get_user(len, optlen)) |
| 932 | return -EFAULT; |
| 933 | |
| 934 | lock_sock(sk); |
| 935 | |
| 936 | switch (optname) { |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 937 | |
| 938 | case BT_DEFER_SETUP: |
| 939 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 940 | err = -EINVAL; |
| 941 | break; |
| 942 | } |
| 943 | |
| 944 | 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] | 945 | (u32 __user *)optval)) |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 946 | err = -EFAULT; |
| 947 | |
| 948 | break; |
| 949 | |
Frédéric Dalleau | ad10b1a | 2013-08-19 14:23:56 +0200 | [diff] [blame] | 950 | case BT_VOICE: |
| 951 | voice.setting = sco_pi(sk)->setting; |
| 952 | |
| 953 | len = min_t(unsigned int, len, sizeof(voice)); |
| 954 | if (copy_to_user(optval, (char *)&voice, len)) |
| 955 | err = -EFAULT; |
| 956 | |
| 957 | break; |
| 958 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 959 | default: |
| 960 | err = -ENOPROTOOPT; |
| 961 | break; |
| 962 | } |
| 963 | |
| 964 | release_sock(sk); |
| 965 | return err; |
| 966 | } |
| 967 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 968 | static int sco_sock_shutdown(struct socket *sock, int how) |
| 969 | { |
| 970 | struct sock *sk = sock->sk; |
| 971 | int err = 0; |
| 972 | |
| 973 | BT_DBG("sock %p, sk %p", sock, sk); |
| 974 | |
| 975 | if (!sk) |
| 976 | return 0; |
| 977 | |
Kuba Pawlak | 1da5537 | 2015-10-05 18:44:17 +0200 | [diff] [blame] | 978 | sock_hold(sk); |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 979 | lock_sock(sk); |
Kuba Pawlak | 1da5537 | 2015-10-05 18:44:17 +0200 | [diff] [blame] | 980 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 981 | if (!sk->sk_shutdown) { |
| 982 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 983 | sco_sock_clear_timer(sk); |
| 984 | __sco_sock_close(sk); |
| 985 | |
Vladimir Davydov | 093facf | 2014-07-15 12:25:28 +0400 | [diff] [blame] | 986 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime && |
| 987 | !(current->flags & PF_EXITING)) |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 988 | err = bt_sock_wait_state(sk, BT_CLOSED, |
Gustavo Padovan | be7c2b9 | 2012-05-17 00:36:21 -0300 | [diff] [blame] | 989 | sk->sk_lingertime); |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 990 | } |
Kuba Pawlak | 1da5537 | 2015-10-05 18:44:17 +0200 | [diff] [blame] | 991 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 992 | release_sock(sk); |
Kuba Pawlak | 1da5537 | 2015-10-05 18:44:17 +0200 | [diff] [blame] | 993 | sock_put(sk); |
| 994 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 995 | return err; |
| 996 | } |
| 997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | static int sco_sock_release(struct socket *sock) |
| 999 | { |
| 1000 | struct sock *sk = sock->sk; |
| 1001 | int err = 0; |
| 1002 | |
| 1003 | BT_DBG("sock %p, sk %p", sock, sk); |
| 1004 | |
| 1005 | if (!sk) |
| 1006 | return 0; |
| 1007 | |
| 1008 | sco_sock_close(sk); |
| 1009 | |
Vladimir Davydov | 093facf | 2014-07-15 12:25:28 +0400 | [diff] [blame] | 1010 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime && |
| 1011 | !(current->flags & PF_EXITING)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | lock_sock(sk); |
| 1013 | err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime); |
| 1014 | release_sock(sk); |
| 1015 | } |
| 1016 | |
| 1017 | sock_orphan(sk); |
| 1018 | sco_sock_kill(sk); |
| 1019 | return err; |
| 1020 | } |
| 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | static void sco_conn_ready(struct sco_conn *conn) |
| 1023 | { |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 1024 | struct sock *parent; |
| 1025 | struct sock *sk = conn->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | |
| 1027 | BT_DBG("conn %p", conn); |
| 1028 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 1029 | if (sk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | sco_sock_clear_timer(sk); |
| 1031 | bh_lock_sock(sk); |
| 1032 | sk->sk_state = BT_CONNECTED; |
| 1033 | sk->sk_state_change(sk); |
| 1034 | bh_unlock_sock(sk); |
| 1035 | } else { |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 1036 | sco_conn_lock(conn); |
| 1037 | |
Kuba Pawlak | 2c501cd | 2015-10-26 16:17:14 +0000 | [diff] [blame] | 1038 | if (!conn->hcon) { |
| 1039 | sco_conn_unlock(conn); |
| 1040 | return; |
| 1041 | } |
| 1042 | |
Marcel Holtmann | 041987c | 2013-10-13 10:15:22 -0700 | [diff] [blame] | 1043 | parent = sco_get_sock_listen(&conn->hcon->src); |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 1044 | if (!parent) { |
| 1045 | sco_conn_unlock(conn); |
| 1046 | return; |
| 1047 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
| 1049 | bh_lock_sock(parent); |
| 1050 | |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 1051 | sk = sco_sock_alloc(sock_net(parent), NULL, |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 1052 | BTPROTO_SCO, GFP_ATOMIC, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | if (!sk) { |
| 1054 | bh_unlock_sock(parent); |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 1055 | sco_conn_unlock(conn); |
| 1056 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | sco_sock_init(sk, parent); |
| 1060 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 1061 | bacpy(&sco_pi(sk)->src, &conn->hcon->src); |
| 1062 | bacpy(&sco_pi(sk)->dst, &conn->hcon->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
| 1064 | hci_conn_hold(conn->hcon); |
| 1065 | __sco_chan_add(conn, sk, parent); |
| 1066 | |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 1067 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) |
| 1068 | sk->sk_state = BT_CONNECT2; |
| 1069 | else |
| 1070 | sk->sk_state = BT_CONNECTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | |
| 1072 | /* Wake up parent */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 1073 | parent->sk_data_ready(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
| 1075 | bh_unlock_sock(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 1077 | sco_conn_unlock(conn); |
| 1078 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /* ----- SCO interface with lower layer (HCI) ----- */ |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 1082 | 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] | 1083 | { |
Gustavo Padovan | fc5fef6 | 2012-05-23 04:04:19 -0300 | [diff] [blame] | 1084 | struct sock *sk; |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1085 | int lm = 0; |
| 1086 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1087 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1089 | /* Find listening sockets */ |
| 1090 | read_lock(&sco_sk_list.lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1091 | sk_for_each(sk, &sco_sk_list.head) { |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1092 | if (sk->sk_state != BT_LISTEN) |
| 1093 | continue; |
| 1094 | |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 1095 | if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) || |
| 1096 | !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) { |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1097 | lm |= HCI_LM_ACCEPT; |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 1098 | |
| 1099 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) |
| 1100 | *flags |= HCI_PROTO_DEFER; |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1101 | break; |
| 1102 | } |
| 1103 | } |
| 1104 | read_unlock(&sco_sk_list.lock); |
| 1105 | |
| 1106 | return lm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1109 | static void sco_connect_cfm(struct hci_conn *hcon, __u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | { |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1111 | if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK) |
| 1112 | return; |
| 1113 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1114 | BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | if (!status) { |
| 1117 | struct sco_conn *conn; |
| 1118 | |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 1119 | conn = sco_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | if (conn) |
| 1121 | sco_conn_ready(conn); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1122 | } else |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 1123 | sco_conn_del(hcon, bt_to_errno(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Johan Hedberg | 3a6d576 | 2015-02-18 14:53:58 +0200 | [diff] [blame] | 1126 | static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | { |
Johan Hedberg | 3a6d576 | 2015-02-18 14:53:58 +0200 | [diff] [blame] | 1128 | if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK) |
| 1129 | return; |
| 1130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 1132 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 1133 | sco_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Arron Wang | 9b4c333 | 2015-06-09 17:47:22 +0800 | [diff] [blame] | 1136 | void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | { |
| 1138 | struct sco_conn *conn = hcon->sco_data; |
| 1139 | |
| 1140 | if (!conn) |
| 1141 | goto drop; |
| 1142 | |
| 1143 | BT_DBG("conn %p len %d", conn, skb->len); |
| 1144 | |
| 1145 | if (skb->len) { |
| 1146 | sco_recv_frame(conn, skb); |
Arron Wang | 9b4c333 | 2015-06-09 17:47:22 +0800 | [diff] [blame] | 1147 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | drop: |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1151 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1154 | static struct hci_cb sco_cb = { |
| 1155 | .name = "SCO", |
| 1156 | .connect_cfm = sco_connect_cfm, |
Johan Hedberg | 3a6d576 | 2015-02-18 14:53:58 +0200 | [diff] [blame] | 1157 | .disconn_cfm = sco_disconn_cfm, |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1158 | }; |
| 1159 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1160 | static int sco_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | { |
| 1162 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | |
Gustavo F. Padovan | ee65d19 | 2011-12-27 15:28:46 -0200 | [diff] [blame] | 1164 | read_lock(&sco_sk_list.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1166 | sk_for_each(sk, &sco_sk_list.head) { |
Marcel Holtmann | eea9636 | 2013-10-13 10:34:01 -0700 | [diff] [blame] | 1167 | seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src, |
| 1168 | &sco_pi(sk)->dst, sk->sk_state); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 1169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | |
Gustavo F. Padovan | ee65d19 | 2011-12-27 15:28:46 -0200 | [diff] [blame] | 1171 | read_unlock(&sco_sk_list.lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 1172 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1173 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Yangtao Li | 8e2924e | 2018-11-05 09:56:19 -0500 | [diff] [blame] | 1176 | DEFINE_SHOW_ATTRIBUTE(sco_debugfs); |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1177 | |
| 1178 | static struct dentry *sco_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1180 | static const struct proto_ops sco_sock_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | .family = PF_BLUETOOTH, |
| 1182 | .owner = THIS_MODULE, |
| 1183 | .release = sco_sock_release, |
| 1184 | .bind = sco_sock_bind, |
| 1185 | .connect = sco_sock_connect, |
| 1186 | .listen = sco_sock_listen, |
| 1187 | .accept = sco_sock_accept, |
| 1188 | .getname = sco_sock_getname, |
| 1189 | .sendmsg = sco_sock_sendmsg, |
Frédéric Dalleau | 20714bfe | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 1190 | .recvmsg = sco_sock_recvmsg, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1191 | .poll = bt_sock_poll, |
Marcel Holtmann | 3241ad8 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 1192 | .ioctl = bt_sock_ioctl, |
Arnd Bergmann | c7cbdbf | 2019-04-17 22:51:48 +0200 | [diff] [blame] | 1193 | .gettstamp = sock_gettstamp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | .mmap = sock_no_mmap, |
| 1195 | .socketpair = sock_no_socketpair, |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 1196 | .shutdown = sco_sock_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | .setsockopt = sco_sock_setsockopt, |
| 1198 | .getsockopt = sco_sock_getsockopt |
| 1199 | }; |
| 1200 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 1201 | static const struct net_proto_family sco_sock_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | .family = PF_BLUETOOTH, |
| 1203 | .owner = THIS_MODULE, |
| 1204 | .create = sco_sock_create, |
| 1205 | }; |
| 1206 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 1207 | int __init sco_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | { |
| 1209 | int err; |
| 1210 | |
Marcel Holtmann | 15762fa | 2015-01-11 15:21:06 -0800 | [diff] [blame] | 1211 | BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr)); |
| 1212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | err = proto_register(&sco_proto, 0); |
| 1214 | if (err < 0) |
| 1215 | return err; |
| 1216 | |
| 1217 | err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops); |
| 1218 | if (err < 0) { |
| 1219 | BT_ERR("SCO socket registration failed"); |
| 1220 | goto error; |
| 1221 | } |
| 1222 | |
Al Viro | b031661 | 2013-04-04 19:14:33 -0400 | [diff] [blame] | 1223 | err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL); |
Masatake YAMATO | de9b921 | 2012-07-26 01:30:12 +0900 | [diff] [blame] | 1224 | if (err < 0) { |
| 1225 | BT_ERR("Failed to create SCO proc file"); |
| 1226 | bt_sock_unregister(BTPROTO_SCO); |
| 1227 | goto error; |
| 1228 | } |
| 1229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | BT_INFO("SCO socket layer initialized"); |
| 1231 | |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1232 | hci_register_cb(&sco_cb); |
| 1233 | |
Marcel Holtmann | 1120e4b | 2013-10-17 17:24:16 -0700 | [diff] [blame] | 1234 | if (IS_ERR_OR_NULL(bt_debugfs)) |
| 1235 | return 0; |
| 1236 | |
| 1237 | sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs, |
| 1238 | NULL, &sco_debugfs_fops); |
| 1239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | return 0; |
| 1241 | |
| 1242 | error: |
| 1243 | proto_unregister(&sco_proto); |
| 1244 | return err; |
| 1245 | } |
| 1246 | |
Alexander Aring | 0402d9f2 | 2015-03-07 20:52:28 +0100 | [diff] [blame] | 1247 | void sco_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | { |
Masatake YAMATO | de9b921 | 2012-07-26 01:30:12 +0900 | [diff] [blame] | 1249 | bt_procfs_cleanup(&init_net, "sco"); |
| 1250 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1251 | debugfs_remove(sco_debugfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 1253 | hci_unregister_cb(&sco_cb); |
| 1254 | |
David Herrmann | 5e9d7f8 | 2013-02-24 19:36:51 +0100 | [diff] [blame] | 1255 | bt_sock_unregister(BTPROTO_SCO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | proto_unregister(&sco_proto); |
| 1258 | } |
| 1259 | |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 1260 | module_param(disable_esco, bool, 0644); |
| 1261 | MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation"); |