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