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