blob: 7ee9e4ab00f882f827f7b63361dc82a24ca72a23 [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 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 Hideaki8e87d142007-02-09 23:24:33 +090015 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 Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth SCO sockets. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
Marcel Holtmannaef7d972010-03-21 05:27:45 +010028#include <linux/debugfs.h>
29#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <net/bluetooth/bluetooth.h>
32#include <net/bluetooth/hci_core.h>
33#include <net/bluetooth/sco.h>
34
Rusty Russelleb939922011-12-19 14:08:01 +000035static bool disable_esco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080037static const struct proto_ops sco_sock_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static struct bt_sock_list sco_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070040 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
Marcel Holtmannfc8f5252014-07-11 06:19:42 +020043/* ---- SCO connections ---- */
44struct 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 Torvalds1da177e2005-04-16 15:20:36 -070056static void sco_sock_close(struct sock *sk);
57static void sco_sock_kill(struct sock *sk);
58
Marcel Holtmann2a0dccb2014-07-11 06:19:41 +020059/* ----- SCO socket info ----- */
60#define sco_pi(sk) ((struct sco_pinfo *) sk)
61
62struct 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 Torvalds1da177e2005-04-16 15:20:36 -070071/* ---- SCO timers ---- */
Marcel Holtmann068d69e2014-07-11 06:19:44 +020072#define SCO_CONN_TIMEOUT (HZ * 40)
73#define SCO_DISCONN_TIMEOUT (HZ * 2)
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static 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
90static 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
96static 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 Torvalds1da177e2005-04-16 15:20:36 -0700102/* ---- SCO connections ---- */
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +0200103static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 struct hci_dev *hdev = hcon->hdev;
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200106 struct sco_conn *conn = hcon->sco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +0200108 if (conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return conn;
110
Claudio Takahasic10cc5a2013-04-11 11:35:45 -0300111 conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL);
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200112 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 spin_lock_init(&conn->lock);
116
117 hcon->sco_data = conn;
118 conn->hcon = hcon;
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 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 Holtmann25ea6db2006-07-06 15:40:09 +0200126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return conn;
128}
129
Marcel Holtmanne03ab512014-07-13 19:54:49 +0200130/* Delete channel.
131 * Must be called on the locked socket. */
132static void sco_chan_del(struct sock *sk, int err)
133{
134 struct sco_conn *conn;
135
136 conn = sco_pi(sk)->conn;
137
138 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
139
140 if (conn) {
141 sco_conn_lock(conn);
142 conn->sk = NULL;
143 sco_pi(sk)->conn = NULL;
144 sco_conn_unlock(conn);
145
146 if (conn->hcon)
147 hci_conn_drop(conn->hcon);
148 }
149
150 sk->sk_state = BT_CLOSED;
151 sk->sk_err = err;
152 sk->sk_state_change(sk);
153
154 sock_set_flag(sk, SOCK_ZAPPED);
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static int sco_conn_del(struct hci_conn *hcon, int err)
158{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200159 struct sco_conn *conn = hcon->sco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct sock *sk;
161
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200162 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return 0;
164
165 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
166
167 /* Kill socket */
Marcel Holtmanneb5a4de2014-07-14 01:30:15 +0200168 sco_conn_lock(conn);
169 sk = conn->sk;
170 sco_conn_unlock(conn);
171
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200172 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 bh_lock_sock(sk);
174 sco_sock_clear_timer(sk);
175 sco_chan_del(sk, err);
176 bh_unlock_sock(sk);
177 sco_sock_kill(sk);
178 }
179
180 hcon->sco_data = NULL;
181 kfree(conn);
182 return 0;
183}
184
Marcel Holtmann015b01c2014-07-13 19:54:48 +0200185static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
186{
187 BT_DBG("conn %p", conn);
188
189 sco_pi(sk)->conn = conn;
190 conn->sk = sk;
191
192 if (parent)
193 bt_accept_enqueue(parent, sk);
194}
195
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300196static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
197 struct sock *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 int err = 0;
200
201 sco_conn_lock(conn);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300202 if (conn->sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 err = -EBUSY;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300204 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 __sco_chan_add(conn, sk, parent);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 sco_conn_unlock(conn);
208 return err;
209}
210
211static int sco_connect(struct sock *sk)
212{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct sco_conn *conn;
214 struct hci_conn *hcon;
215 struct hci_dev *hdev;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200216 int err, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Marcel Holtmanneea96362013-10-13 10:34:01 -0700218 BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Marcel Holtmanneea96362013-10-13 10:34:01 -0700220 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src);
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200221 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -EHOSTUNREACH;
223
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300224 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Marcel Holtmann7cb127d2008-07-14 20:13:53 +0200226 if (lmp_esco_capable(hdev) && !disable_esco)
227 type = ESCO_LINK;
228 else
229 type = SCO_LINK;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200230
Frédéric Dalleau79dc0082013-08-19 14:24:01 +0200231 if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
232 (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev))) {
233 err = -EOPNOTSUPP;
234 goto done;
235 }
236
Marcel Holtmanneea96362013-10-13 10:34:01 -0700237 hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
Marcel Holtmann041987c2013-10-13 10:15:22 -0700238 sco_pi(sk)->setting);
Ville Tervo30e76272011-02-22 16:10:53 -0300239 if (IS_ERR(hcon)) {
240 err = PTR_ERR(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto done;
Ville Tervo30e76272011-02-22 16:10:53 -0300242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +0200244 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (!conn) {
David Herrmann76a68ba2013-04-06 20:28:37 +0200246 hci_conn_drop(hcon);
Ville Tervo30e76272011-02-22 16:10:53 -0300247 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto done;
249 }
250
251 /* Update source addr of the socket */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700252 bacpy(&sco_pi(sk)->src, &hcon->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 err = sco_chan_add(conn, sk, NULL);
255 if (err)
256 goto done;
257
258 if (hcon->state == BT_CONNECTED) {
259 sco_sock_clear_timer(sk);
260 sk->sk_state = BT_CONNECTED;
261 } else {
262 sk->sk_state = BT_CONNECT;
263 sco_sock_set_timer(sk, sk->sk_sndtimeo);
264 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300267 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 hci_dev_put(hdev);
269 return err;
270}
271
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300272static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct sco_conn *conn = sco_pi(sk)->conn;
275 struct sk_buff *skb;
Mikel Astiz088ce082012-04-11 08:48:48 +0200276 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /* Check outgoing MTU */
279 if (len > conn->mtu)
280 return -EINVAL;
281
282 BT_DBG("sk %p len %d", sk, len);
283
Mikel Astiz088ce082012-04-11 08:48:48 +0200284 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300285 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return err;
287
Mikel Astiz088ce082012-04-11 08:48:48 +0200288 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300289 kfree_skb(skb);
290 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -0300293 hci_send_sco(conn->hcon, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Mikel Astiz088ce082012-04-11 08:48:48 +0200295 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300298static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Marcel Holtmanneb5a4de2014-07-14 01:30:15 +0200300 struct sock *sk;
301
302 sco_conn_lock(conn);
303 sk = conn->sk;
304 sco_conn_unlock(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (!sk)
307 goto drop;
308
309 BT_DBG("sk %p len %d", sk, skb->len);
310
311 if (sk->sk_state != BT_CONNECTED)
312 goto drop;
313
314 if (!sock_queue_rcv_skb(sk, skb))
315 return;
316
317drop:
318 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/* -------- Socket interface ---------- */
Marcel Holtmannfb334052012-04-19 14:37:58 +0200322static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Marcel Holtmannfb334052012-04-19 14:37:58 +0200324 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Sasha Levinb67bfe02013-02-27 17:06:00 -0800326 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmannfb334052012-04-19 14:37:58 +0200327 if (sk->sk_state != BT_LISTEN)
328 continue;
329
Marcel Holtmanneea96362013-10-13 10:34:01 -0700330 if (!bacmp(&sco_pi(sk)->src, ba))
Marcel Holtmannfb334052012-04-19 14:37:58 +0200331 return sk;
332 }
333
334 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337/* Find socket listening on source bdaddr.
338 * Returns closest match.
339 */
340static struct sock *sco_get_sock_listen(bdaddr_t *src)
341{
342 struct sock *sk = NULL, *sk1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 read_lock(&sco_sk_list.lock);
345
Sasha Levinb67bfe02013-02-27 17:06:00 -0800346 sk_for_each(sk, &sco_sk_list.head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (sk->sk_state != BT_LISTEN)
348 continue;
349
350 /* Exact match. */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700351 if (!bacmp(&sco_pi(sk)->src, src))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
353
354 /* Closest match */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700355 if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 sk1 = sk;
357 }
358
359 read_unlock(&sco_sk_list.lock);
360
Sasha Levinb67bfe02013-02-27 17:06:00 -0800361 return sk ? sk : sk1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364static void sco_sock_destruct(struct sock *sk)
365{
366 BT_DBG("sk %p", sk);
367
368 skb_queue_purge(&sk->sk_receive_queue);
369 skb_queue_purge(&sk->sk_write_queue);
370}
371
372static void sco_sock_cleanup_listen(struct sock *parent)
373{
374 struct sock *sk;
375
376 BT_DBG("parent %p", parent);
377
378 /* Close not yet accepted channels */
379 while ((sk = bt_accept_dequeue(parent, NULL))) {
380 sco_sock_close(sk);
381 sco_sock_kill(sk);
382 }
383
384 parent->sk_state = BT_CLOSED;
385 sock_set_flag(parent, SOCK_ZAPPED);
386}
387
388/* Kill socket (only if zapped and orphan)
389 * Must be called on unlocked socket.
390 */
391static void sco_sock_kill(struct sock *sk)
392{
393 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
394 return;
395
396 BT_DBG("sk %p state %d", sk, sk->sk_state);
397
398 /* Kill poor orphan */
399 bt_sock_unlink(&sco_sk_list, sk);
400 sock_set_flag(sk, SOCK_DEAD);
401 sock_put(sk);
402}
403
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200404static void __sco_sock_close(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200406 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 switch (sk->sk_state) {
409 case BT_LISTEN:
410 sco_sock_cleanup_listen(sk);
411 break;
412
413 case BT_CONNECTED:
414 case BT_CONFIG:
Gustavo Padovanb7e98b52013-01-03 19:59:28 -0200415 if (sco_pi(sk)->conn->hcon) {
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300416 sk->sk_state = BT_DISCONN;
417 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
David Herrmann76a68ba2013-04-06 20:28:37 +0200418 hci_conn_drop(sco_pi(sk)->conn->hcon);
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300419 sco_pi(sk)->conn->hcon = NULL;
420 } else
421 sco_chan_del(sk, ECONNRESET);
422 break;
423
Vinicius Costa Gomeseb20ff92013-03-13 19:46:20 -0300424 case BT_CONNECT2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 case BT_CONNECT:
426 case BT_DISCONN:
427 sco_chan_del(sk, ECONNRESET);
428 break;
429
430 default:
431 sock_set_flag(sk, SOCK_ZAPPED);
432 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700433 }
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200436/* Must be called on unlocked socket. */
437static void sco_sock_close(struct sock *sk)
438{
439 sco_sock_clear_timer(sk);
440 lock_sock(sk);
441 __sco_sock_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sco_sock_kill(sk);
444}
445
446static void sco_sock_init(struct sock *sk, struct sock *parent)
447{
448 BT_DBG("sk %p", sk);
449
Paul Moore6230c9b2011-10-07 09:40:59 +0000450 if (parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 sk->sk_type = parent->sk_type;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100452 bt_sk(sk)->flags = bt_sk(parent)->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +0000453 security_sk_clone(parent, sk);
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457static struct proto sco_proto = {
458 .name = "SCO",
459 .owner = THIS_MODULE,
460 .obj_size = sizeof(struct sco_pinfo)
461};
462
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700463static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct sock *sk;
466
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700467 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!sk)
469 return NULL;
470
471 sock_init_data(sock, sk);
472 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
473
474 sk->sk_destruct = sco_sock_destruct;
475 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
476
477 sock_reset_flag(sk, SOCK_ZAPPED);
478
479 sk->sk_protocol = proto;
480 sk->sk_state = BT_OPEN;
481
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200482 sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
483
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800484 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 bt_sock_link(&sco_sk_list, sk);
487 return sk;
488}
489
Eric Paris3f378b62009-11-05 22:18:14 -0800490static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
491 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct sock *sk;
494
495 BT_DBG("sock %p", sock);
496
497 sock->state = SS_UNCONNECTED;
498
499 if (sock->type != SOCK_SEQPACKET)
500 return -ESOCKTNOSUPPORT;
501
502 sock->ops = &sco_sock_ops;
503
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700504 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200505 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return -ENOMEM;
507
508 sco_sock_init(sk, NULL);
509 return 0;
510}
511
512static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
513{
514 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
515 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 int err = 0;
517
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300518 BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (!addr || addr->sa_family != AF_BLUETOOTH)
521 return -EINVAL;
522
523 lock_sock(sk);
524
525 if (sk->sk_state != BT_OPEN) {
526 err = -EBADFD;
527 goto done;
528 }
529
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200530 if (sk->sk_type != SOCK_SEQPACKET) {
531 err = -EINVAL;
532 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
Marcel Holtmanneea96362013-10-13 10:34:01 -0700535 bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200536
537 sk->sk_state = BT_BOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539done:
540 release_sock(sk);
541 return err;
542}
543
544static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
545{
546 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
547 struct sock *sk = sock->sk;
Claudio Takahasi92f185c2013-04-11 11:35:46 -0300548 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 BT_DBG("sk %p", sk);
551
Changli Gao6503d962010-03-31 22:58:26 +0000552 if (alen < sizeof(struct sockaddr_sco) ||
553 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return -EINVAL;
555
556 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
557 return -EBADFD;
558
559 if (sk->sk_type != SOCK_SEQPACKET)
560 return -EINVAL;
561
562 lock_sock(sk);
563
564 /* Set destination address and psm */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700565 bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200567 err = sco_connect(sk);
568 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto done;
570
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900571 err = bt_sock_wait_state(sk, BT_CONNECTED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300572 sock_sndtimeo(sk, flags & O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574done:
575 release_sock(sk);
576 return err;
577}
578
579static int sco_sock_listen(struct socket *sock, int backlog)
580{
581 struct sock *sk = sock->sk;
Marcel Holtmanneea96362013-10-13 10:34:01 -0700582 bdaddr_t *src = &sco_pi(sk)->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int err = 0;
584
585 BT_DBG("sk %p backlog %d", sk, backlog);
586
587 lock_sock(sk);
588
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200589 if (sk->sk_state != BT_BOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 err = -EBADFD;
591 goto done;
592 }
593
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200594 if (sk->sk_type != SOCK_SEQPACKET) {
595 err = -EINVAL;
596 goto done;
597 }
598
Marcel Holtmannfb334052012-04-19 14:37:58 +0200599 write_lock(&sco_sk_list.lock);
600
601 if (__sco_get_sock_listen_by_addr(src)) {
602 err = -EADDRINUSE;
603 goto unlock;
604 }
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 sk->sk_max_ack_backlog = backlog;
607 sk->sk_ack_backlog = 0;
Marcel Holtmannfb334052012-04-19 14:37:58 +0200608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 sk->sk_state = BT_LISTEN;
610
Marcel Holtmannfb334052012-04-19 14:37:58 +0200611unlock:
612 write_unlock(&sco_sk_list.lock);
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614done:
615 release_sock(sk);
616 return err;
617}
618
619static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
620{
621 DECLARE_WAITQUEUE(wait, current);
622 struct sock *sk = sock->sk, *ch;
623 long timeo;
624 int err = 0;
625
626 lock_sock(sk);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
629
630 BT_DBG("sk %p timeo %ld", sk, timeo);
631
632 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000633 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurley552b0d32011-07-24 00:11:01 -0400634 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (sk->sk_state != BT_LISTEN) {
638 err = -EBADFD;
639 break;
640 }
641
Peter Hurley552b0d32011-07-24 00:11:01 -0400642 ch = bt_accept_dequeue(sk, newsock);
643 if (ch)
644 break;
645
646 if (!timeo) {
647 err = -EAGAIN;
648 break;
649 }
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (signal_pending(current)) {
652 err = sock_intr_errno(timeo);
653 break;
654 }
Peter Hurley552b0d32011-07-24 00:11:01 -0400655
656 release_sock(sk);
657 timeo = schedule_timeout(timeo);
658 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Peter Hurley552b0d32011-07-24 00:11:01 -0400660 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000661 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (err)
664 goto done;
665
666 newsock->state = SS_CONNECTED;
667
668 BT_DBG("new socket %p", ch);
669
670done:
671 release_sock(sk);
672 return err;
673}
674
675static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
676{
677 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
678 struct sock *sk = sock->sk;
679
680 BT_DBG("sock %p, sk %p", sock, sk);
681
682 addr->sa_family = AF_BLUETOOTH;
683 *len = sizeof(struct sockaddr_sco);
684
685 if (peer)
Marcel Holtmanneea96362013-10-13 10:34:01 -0700686 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 else
Marcel Holtmanneea96362013-10-13 10:34:01 -0700688 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 return 0;
691}
692
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900693static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct msghdr *msg, size_t len)
695{
696 struct sock *sk = sock->sk;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300697 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 BT_DBG("sock %p, sk %p", sock, sk);
700
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800701 err = sock_error(sk);
702 if (err)
703 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (msg->msg_flags & MSG_OOB)
706 return -EOPNOTSUPP;
707
708 lock_sock(sk);
709
710 if (sk->sk_state == BT_CONNECTED)
711 err = sco_send_frame(sk, msg, len);
712 else
713 err = -ENOTCONN;
714
715 release_sock(sk);
716 return err;
717}
718
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200719static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200720{
721 struct hci_dev *hdev = conn->hdev;
722
723 BT_DBG("conn %p", conn);
724
725 conn->state = BT_CONFIG;
726
727 if (!lmp_esco_capable(hdev)) {
728 struct hci_cp_accept_conn_req cp;
729
730 bacpy(&cp.bdaddr, &conn->dst);
Frédéric Dalleau33f24042013-08-19 14:23:55 +0200731 cp.role = 0x00; /* Ignored */
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200732
733 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
734 } else {
735 struct hci_cp_accept_sync_conn_req cp;
736
737 bacpy(&cp.bdaddr, &conn->dst);
738 cp.pkt_type = cpu_to_le16(conn->pkt_type);
739
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700740 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
741 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200742 cp.content_format = cpu_to_le16(setting);
743
744 switch (setting & SCO_AIRMODE_MASK) {
745 case SCO_AIRMODE_TRANSP:
746 if (conn->pkt_type & ESCO_2EV3)
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700747 cp.max_latency = cpu_to_le16(0x0008);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200748 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700749 cp.max_latency = cpu_to_le16(0x000D);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200750 cp.retrans_effort = 0x02;
751 break;
752 case SCO_AIRMODE_CVSD:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700753 cp.max_latency = cpu_to_le16(0xffff);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200754 cp.retrans_effort = 0xff;
755 break;
756 }
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200757
758 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
759 sizeof(cp), &cp);
760 }
761}
762
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100763static int sco_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
764 struct msghdr *msg, size_t len, int flags)
765{
766 struct sock *sk = sock->sk;
767 struct sco_pinfo *pi = sco_pi(sk);
768
769 lock_sock(sk);
770
771 if (sk->sk_state == BT_CONNECT2 &&
772 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200773 sco_conn_defer_accept(pi->conn->hcon, pi->setting);
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100774 sk->sk_state = BT_CONFIG;
775
776 release_sock(sk);
777 return 0;
778 }
779
780 release_sock(sk);
781
782 return bt_sock_recvmsg(iocb, sock, msg, len, flags);
783}
784
David S. Millerb7058842009-09-30 16:12:20 -0700785static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct sock *sk = sock->sk;
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200788 int len, err = 0;
789 struct bt_voice voice;
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100790 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 BT_DBG("sk %p", sk);
793
794 lock_sock(sk);
795
796 switch (optname) {
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100797
798 case BT_DEFER_SETUP:
799 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
800 err = -EINVAL;
801 break;
802 }
803
804 if (get_user(opt, (u32 __user *) optval)) {
805 err = -EFAULT;
806 break;
807 }
808
809 if (opt)
810 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
811 else
812 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
813 break;
814
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200815 case BT_VOICE:
816 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
817 sk->sk_state != BT_CONNECT2) {
818 err = -EINVAL;
819 break;
820 }
821
822 voice.setting = sco_pi(sk)->setting;
823
824 len = min_t(unsigned int, sizeof(voice), optlen);
825 if (copy_from_user((char *) &voice, optval, len)) {
826 err = -EFAULT;
827 break;
828 }
829
830 /* Explicitly check for these values */
831 if (voice.setting != BT_VOICE_TRANSPARENT &&
832 voice.setting != BT_VOICE_CVSD_16BIT) {
833 err = -EINVAL;
834 break;
835 }
836
837 sco_pi(sk)->setting = voice.setting;
838 break;
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 default:
841 err = -ENOPROTOOPT;
842 break;
843 }
844
845 release_sock(sk);
846 return err;
847}
848
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100849static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 struct sock *sk = sock->sk;
852 struct sco_options opts;
853 struct sco_conninfo cinfo;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900854 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 BT_DBG("sk %p", sk);
857
858 if (get_user(len, optlen))
859 return -EFAULT;
860
861 lock_sock(sk);
862
863 switch (optname) {
864 case SCO_OPTIONS:
Johan Hedberg9d225d22013-08-08 14:53:56 +0300865 if (sk->sk_state != BT_CONNECTED &&
866 !(sk->sk_state == BT_CONNECT2 &&
867 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 err = -ENOTCONN;
869 break;
870 }
871
872 opts.mtu = sco_pi(sk)->conn->mtu;
873
874 BT_DBG("mtu %d", opts.mtu);
875
876 len = min_t(unsigned int, len, sizeof(opts));
877 if (copy_to_user(optval, (char *)&opts, len))
878 err = -EFAULT;
879
880 break;
881
882 case SCO_CONNINFO:
Johan Hedberg9d225d22013-08-08 14:53:56 +0300883 if (sk->sk_state != BT_CONNECTED &&
884 !(sk->sk_state == BT_CONNECT2 &&
885 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 err = -ENOTCONN;
887 break;
888 }
889
Vasiliy Kulikovc4c896e2011-02-14 13:54:26 +0300890 memset(&cinfo, 0, sizeof(cinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
892 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
893
894 len = min_t(unsigned int, len, sizeof(cinfo));
895 if (copy_to_user(optval, (char *)&cinfo, len))
896 err = -EFAULT;
897
898 break;
899
900 default:
901 err = -ENOPROTOOPT;
902 break;
903 }
904
905 release_sock(sk);
906 return err;
907}
908
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100909static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
910{
911 struct sock *sk = sock->sk;
912 int len, err = 0;
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200913 struct bt_voice voice;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100914
915 BT_DBG("sk %p", sk);
916
917 if (level == SOL_SCO)
918 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
919
920 if (get_user(len, optlen))
921 return -EFAULT;
922
923 lock_sock(sk);
924
925 switch (optname) {
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100926
927 case BT_DEFER_SETUP:
928 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
929 err = -EINVAL;
930 break;
931 }
932
933 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
934 (u32 __user *) optval))
935 err = -EFAULT;
936
937 break;
938
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200939 case BT_VOICE:
940 voice.setting = sco_pi(sk)->setting;
941
942 len = min_t(unsigned int, len, sizeof(voice));
943 if (copy_to_user(optval, (char *)&voice, len))
944 err = -EFAULT;
945
946 break;
947
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100948 default:
949 err = -ENOPROTOOPT;
950 break;
951 }
952
953 release_sock(sk);
954 return err;
955}
956
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200957static int sco_sock_shutdown(struct socket *sock, int how)
958{
959 struct sock *sk = sock->sk;
960 int err = 0;
961
962 BT_DBG("sock %p, sk %p", sock, sk);
963
964 if (!sk)
965 return 0;
966
967 lock_sock(sk);
968 if (!sk->sk_shutdown) {
969 sk->sk_shutdown = SHUTDOWN_MASK;
970 sco_sock_clear_timer(sk);
971 __sco_sock_close(sk);
972
Vladimir Davydov093facf2014-07-15 12:25:28 +0400973 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
974 !(current->flags & PF_EXITING))
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200975 err = bt_sock_wait_state(sk, BT_CLOSED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300976 sk->sk_lingertime);
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200977 }
978 release_sock(sk);
979 return err;
980}
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982static int sco_sock_release(struct socket *sock)
983{
984 struct sock *sk = sock->sk;
985 int err = 0;
986
987 BT_DBG("sock %p, sk %p", sock, sk);
988
989 if (!sk)
990 return 0;
991
992 sco_sock_close(sk);
993
Vladimir Davydov093facf2014-07-15 12:25:28 +0400994 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
995 !(current->flags & PF_EXITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 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 Torvalds1da177e2005-04-16 15:20:36 -07001006static void sco_conn_ready(struct sco_conn *conn)
1007{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +02001008 struct sock *parent;
1009 struct sock *sk = conn->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 BT_DBG("conn %p", conn);
1012
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +02001013 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 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 Guedes40528082013-01-29 19:59:56 -03001020 sco_conn_lock(conn);
1021
Marcel Holtmann041987c2013-10-13 10:15:22 -07001022 parent = sco_get_sock_listen(&conn->hcon->src);
Andre Guedes40528082013-01-29 19:59:56 -03001023 if (!parent) {
1024 sco_conn_unlock(conn);
1025 return;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 bh_lock_sock(parent);
1029
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -03001030 sk = sco_sock_alloc(sock_net(parent), NULL,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -03001031 BTPROTO_SCO, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!sk) {
1033 bh_unlock_sock(parent);
Andre Guedes40528082013-01-29 19:59:56 -03001034 sco_conn_unlock(conn);
1035 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037
1038 sco_sock_init(sk, parent);
1039
Marcel Holtmanneea96362013-10-13 10:34:01 -07001040 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1041 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 hci_conn_hold(conn->hcon);
1044 __sco_chan_add(conn, sk, parent);
1045
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001046 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 Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 /* Wake up parent */
David S. Miller676d2362014-04-11 16:15:36 -04001052 parent->sk_data_ready(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 bh_unlock_sock(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Andre Guedes40528082013-01-29 19:59:56 -03001056 sco_conn_unlock(conn);
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
1060/* ----- SCO interface with lower layer (HCI) ----- */
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001061int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Gustavo Padovanfc5fef62012-05-23 04:04:19 -03001063 struct sock *sk;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001064 int lm = 0;
1065
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001066 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001068 /* Find listening sockets */
1069 read_lock(&sco_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001070 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001071 if (sk->sk_state != BT_LISTEN)
1072 continue;
1073
Marcel Holtmanneea96362013-10-13 10:34:01 -07001074 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1075 !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001076 lm |= HCI_LM_ACCEPT;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001077
1078 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1079 *flags |= HCI_PROTO_DEFER;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001080 break;
1081 }
1082 }
1083 read_unlock(&sco_sk_list.lock);
1084
1085 return lm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03001088void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001090 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!status) {
1092 struct sco_conn *conn;
1093
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +02001094 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (conn)
1096 sco_conn_ready(conn);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001097 } else
Joe Perchese1750722011-06-29 18:18:29 -07001098 sco_conn_del(hcon, bt_to_errno(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03001101void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 BT_DBG("hcon %p reason %d", hcon, reason);
1104
Joe Perchese1750722011-06-29 18:18:29 -07001105 sco_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Ulisses Furquim686ebf22011-12-21 10:11:33 -02001108int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
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
1122drop:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001123 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return 0;
1125}
1126
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001127static int sco_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Gustavo F. Padovanee65d192011-12-27 15:28:46 -02001131 read_lock(&sco_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Sasha Levinb67bfe02013-02-27 17:06:00 -08001133 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmanneea96362013-10-13 10:34:01 -07001134 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1135 &sco_pi(sk)->dst, sk->sk_state);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Gustavo F. Padovanee65d192011-12-27 15:28:46 -02001138 read_unlock(&sco_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001139
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001143static int sco_debugfs_open(struct inode *inode, struct file *file)
1144{
1145 return single_open(file, sco_debugfs_show, inode->i_private);
1146}
1147
1148static 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
1155static struct dentry *sco_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001157static const struct proto_ops sco_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 .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 Dalleau20714bf2012-11-21 10:51:12 +01001167 .recvmsg = sco_sock_recvmsg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 .poll = bt_sock_poll,
Marcel Holtmann3241ad82008-07-14 20:13:50 +02001169 .ioctl = bt_sock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 .mmap = sock_no_mmap,
1171 .socketpair = sock_no_socketpair,
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +02001172 .shutdown = sco_sock_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 .setsockopt = sco_sock_setsockopt,
1174 .getsockopt = sco_sock_getsockopt
1175};
1176
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001177static const struct net_proto_family sco_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 .family = PF_BLUETOOTH,
1179 .owner = THIS_MODULE,
1180 .create = sco_sock_create,
1181};
1182
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001183int __init sco_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
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 Virob0316612013-04-04 19:14:33 -04001197 err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
Masatake YAMATOde9b9212012-07-26 01:30:12 +09001198 if (err < 0) {
1199 BT_ERR("Failed to create SCO proc file");
1200 bt_sock_unregister(BTPROTO_SCO);
1201 goto error;
1202 }
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 BT_INFO("SCO socket layer initialized");
1205
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07001206 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 Torvalds1da177e2005-04-16 15:20:36 -07001212 return 0;
1213
1214error:
1215 proto_unregister(&sco_proto);
1216 return err;
1217}
1218
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001219void __exit sco_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Masatake YAMATOde9b9212012-07-26 01:30:12 +09001221 bt_procfs_cleanup(&init_net, "sco");
1222
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001223 debugfs_remove(sco_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
David Herrmann5e9d7f82013-02-24 19:36:51 +01001225 bt_sock_unregister(BTPROTO_SCO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 proto_unregister(&sco_proto);
1228}
1229
Marcel Holtmann7cb127d2008-07-14 20:13:53 +02001230module_param(disable_esco, bool, 0644);
1231MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");