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