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