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