blob: f315c8d0e43b594486cc3dd1be1433387601e7c3 [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
Nicholas Krausedf945362015-08-18 21:23:01 -0400157static void sco_conn_del(struct hci_conn *hcon, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
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)
Nicholas Krausedf945362015-08-18 21:23:01 -0400163 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Marcel Holtmann015b01c2014-07-13 19:54:48 +0200184static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
185{
186 BT_DBG("conn %p", conn);
187
188 sco_pi(sk)->conn = conn;
189 conn->sk = sk;
190
191 if (parent)
192 bt_accept_enqueue(parent, sk);
193}
194
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300195static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
196 struct sock *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 int err = 0;
199
200 sco_conn_lock(conn);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300201 if (conn->sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 err = -EBUSY;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300203 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 __sco_chan_add(conn, sk, parent);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 sco_conn_unlock(conn);
207 return err;
208}
209
210static int sco_connect(struct sock *sk)
211{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 struct sco_conn *conn;
213 struct hci_conn *hcon;
214 struct hci_dev *hdev;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200215 int err, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Marcel Holtmanneea96362013-10-13 10:34:01 -0700217 BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Marcel Holtmanneea96362013-10-13 10:34:01 -0700219 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src);
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200220 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -EHOSTUNREACH;
222
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300223 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Marcel Holtmann7cb127d2008-07-14 20:13:53 +0200225 if (lmp_esco_capable(hdev) && !disable_esco)
226 type = ESCO_LINK;
227 else
228 type = SCO_LINK;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200229
Frédéric Dalleau79dc0082013-08-19 14:24:01 +0200230 if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
231 (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev))) {
232 err = -EOPNOTSUPP;
233 goto done;
234 }
235
Marcel Holtmanneea96362013-10-13 10:34:01 -0700236 hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
Marcel Holtmann041987c2013-10-13 10:15:22 -0700237 sco_pi(sk)->setting);
Ville Tervo30e76272011-02-22 16:10:53 -0300238 if (IS_ERR(hcon)) {
239 err = PTR_ERR(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto done;
Ville Tervo30e76272011-02-22 16:10:53 -0300241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +0200243 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (!conn) {
David Herrmann76a68ba2013-04-06 20:28:37 +0200245 hci_conn_drop(hcon);
Ville Tervo30e76272011-02-22 16:10:53 -0300246 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto done;
248 }
249
250 /* Update source addr of the socket */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700251 bacpy(&sco_pi(sk)->src, &hcon->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 err = sco_chan_add(conn, sk, NULL);
254 if (err)
255 goto done;
256
257 if (hcon->state == BT_CONNECTED) {
258 sco_sock_clear_timer(sk);
259 sk->sk_state = BT_CONNECTED;
260 } else {
261 sk->sk_state = BT_CONNECT;
262 sco_sock_set_timer(sk, sk->sk_sndtimeo);
263 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300266 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 hci_dev_put(hdev);
268 return err;
269}
270
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300271static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct sco_conn *conn = sco_pi(sk)->conn;
274 struct sk_buff *skb;
Mikel Astiz088ce082012-04-11 08:48:48 +0200275 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /* Check outgoing MTU */
278 if (len > conn->mtu)
279 return -EINVAL;
280
281 BT_DBG("sk %p len %d", sk, len);
282
Mikel Astiz088ce082012-04-11 08:48:48 +0200283 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300284 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return err;
286
Al Viro6ce8e9c2014-04-06 21:25:44 -0400287 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300288 kfree_skb(skb);
289 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -0300292 hci_send_sco(conn->hcon, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Mikel Astiz088ce082012-04-11 08:48:48 +0200294 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300297static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Marcel Holtmanneb5a4de2014-07-14 01:30:15 +0200299 struct sock *sk;
300
301 sco_conn_lock(conn);
302 sk = conn->sk;
303 sco_conn_unlock(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (!sk)
306 goto drop;
307
308 BT_DBG("sk %p len %d", sk, skb->len);
309
310 if (sk->sk_state != BT_CONNECTED)
311 goto drop;
312
313 if (!sock_queue_rcv_skb(sk, skb))
314 return;
315
316drop:
317 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320/* -------- Socket interface ---------- */
Marcel Holtmannfb334052012-04-19 14:37:58 +0200321static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Marcel Holtmannfb334052012-04-19 14:37:58 +0200323 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Sasha Levinb67bfe02013-02-27 17:06:00 -0800325 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmannfb334052012-04-19 14:37:58 +0200326 if (sk->sk_state != BT_LISTEN)
327 continue;
328
Marcel Holtmanneea96362013-10-13 10:34:01 -0700329 if (!bacmp(&sco_pi(sk)->src, ba))
Marcel Holtmannfb334052012-04-19 14:37:58 +0200330 return sk;
331 }
332
333 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336/* Find socket listening on source bdaddr.
337 * Returns closest match.
338 */
339static struct sock *sco_get_sock_listen(bdaddr_t *src)
340{
341 struct sock *sk = NULL, *sk1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 read_lock(&sco_sk_list.lock);
344
Sasha Levinb67bfe02013-02-27 17:06:00 -0800345 sk_for_each(sk, &sco_sk_list.head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (sk->sk_state != BT_LISTEN)
347 continue;
348
349 /* Exact match. */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700350 if (!bacmp(&sco_pi(sk)->src, src))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
352
353 /* Closest match */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700354 if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 sk1 = sk;
356 }
357
358 read_unlock(&sco_sk_list.lock);
359
Sasha Levinb67bfe02013-02-27 17:06:00 -0800360 return sk ? sk : sk1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363static void sco_sock_destruct(struct sock *sk)
364{
365 BT_DBG("sk %p", sk);
366
367 skb_queue_purge(&sk->sk_receive_queue);
368 skb_queue_purge(&sk->sk_write_queue);
369}
370
371static void sco_sock_cleanup_listen(struct sock *parent)
372{
373 struct sock *sk;
374
375 BT_DBG("parent %p", parent);
376
377 /* Close not yet accepted channels */
378 while ((sk = bt_accept_dequeue(parent, NULL))) {
379 sco_sock_close(sk);
380 sco_sock_kill(sk);
381 }
382
383 parent->sk_state = BT_CLOSED;
384 sock_set_flag(parent, SOCK_ZAPPED);
385}
386
387/* Kill socket (only if zapped and orphan)
388 * Must be called on unlocked socket.
389 */
390static void sco_sock_kill(struct sock *sk)
391{
392 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
393 return;
394
395 BT_DBG("sk %p state %d", sk, sk->sk_state);
396
397 /* Kill poor orphan */
398 bt_sock_unlink(&sco_sk_list, sk);
399 sock_set_flag(sk, SOCK_DEAD);
400 sock_put(sk);
401}
402
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200403static void __sco_sock_close(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200405 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 switch (sk->sk_state) {
408 case BT_LISTEN:
409 sco_sock_cleanup_listen(sk);
410 break;
411
412 case BT_CONNECTED:
413 case BT_CONFIG:
Gustavo Padovanb7e98b52013-01-03 19:59:28 -0200414 if (sco_pi(sk)->conn->hcon) {
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300415 sk->sk_state = BT_DISCONN;
416 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
David Herrmann76a68ba2013-04-06 20:28:37 +0200417 hci_conn_drop(sco_pi(sk)->conn->hcon);
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300418 sco_pi(sk)->conn->hcon = NULL;
419 } else
420 sco_chan_del(sk, ECONNRESET);
421 break;
422
Vinicius Costa Gomeseb20ff92013-03-13 19:46:20 -0300423 case BT_CONNECT2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 case BT_CONNECT:
425 case BT_DISCONN:
426 sco_chan_del(sk, ECONNRESET);
427 break;
428
429 default:
430 sock_set_flag(sk, SOCK_ZAPPED);
431 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700432 }
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200435/* Must be called on unlocked socket. */
436static void sco_sock_close(struct sock *sk)
437{
438 sco_sock_clear_timer(sk);
439 lock_sock(sk);
440 __sco_sock_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 sco_sock_kill(sk);
443}
444
445static void sco_sock_init(struct sock *sk, struct sock *parent)
446{
447 BT_DBG("sk %p", sk);
448
Paul Moore6230c9b2011-10-07 09:40:59 +0000449 if (parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 sk->sk_type = parent->sk_type;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100451 bt_sk(sk)->flags = bt_sk(parent)->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +0000452 security_sk_clone(parent, sk);
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456static struct proto sco_proto = {
457 .name = "SCO",
458 .owner = THIS_MODULE,
459 .obj_size = sizeof(struct sco_pinfo)
460};
461
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500462static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct sock *sk;
465
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500466 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!sk)
468 return NULL;
469
470 sock_init_data(sock, sk);
471 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
472
473 sk->sk_destruct = sco_sock_destruct;
474 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
475
476 sock_reset_flag(sk, SOCK_ZAPPED);
477
478 sk->sk_protocol = proto;
479 sk->sk_state = BT_OPEN;
480
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200481 sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
482
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800483 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 bt_sock_link(&sco_sk_list, sk);
486 return sk;
487}
488
Eric Paris3f378b62009-11-05 22:18:14 -0800489static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
490 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct sock *sk;
493
494 BT_DBG("sock %p", sock);
495
496 sock->state = SS_UNCONNECTED;
497
498 if (sock->type != SOCK_SEQPACKET)
499 return -ESOCKTNOSUPPORT;
500
501 sock->ops = &sco_sock_ops;
502
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500503 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200504 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -ENOMEM;
506
507 sco_sock_init(sk, NULL);
508 return 0;
509}
510
511static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
512{
513 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
514 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int err = 0;
516
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300517 BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (!addr || addr->sa_family != AF_BLUETOOTH)
520 return -EINVAL;
521
522 lock_sock(sk);
523
524 if (sk->sk_state != BT_OPEN) {
525 err = -EBADFD;
526 goto done;
527 }
528
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200529 if (sk->sk_type != SOCK_SEQPACKET) {
530 err = -EINVAL;
531 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
Marcel Holtmanneea96362013-10-13 10:34:01 -0700534 bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200535
536 sk->sk_state = BT_BOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538done:
539 release_sock(sk);
540 return err;
541}
542
543static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
544{
545 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
546 struct sock *sk = sock->sk;
Claudio Takahasi92f185c2013-04-11 11:35:46 -0300547 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 BT_DBG("sk %p", sk);
550
Changli Gao6503d962010-03-31 22:58:26 +0000551 if (alen < sizeof(struct sockaddr_sco) ||
552 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return -EINVAL;
554
555 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
556 return -EBADFD;
557
558 if (sk->sk_type != SOCK_SEQPACKET)
559 return -EINVAL;
560
561 lock_sock(sk);
562
563 /* Set destination address and psm */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700564 bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200566 err = sco_connect(sk);
567 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto done;
569
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900570 err = bt_sock_wait_state(sk, BT_CONNECTED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300571 sock_sndtimeo(sk, flags & O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573done:
574 release_sock(sk);
575 return err;
576}
577
578static int sco_sock_listen(struct socket *sock, int backlog)
579{
580 struct sock *sk = sock->sk;
Marcel Holtmanneea96362013-10-13 10:34:01 -0700581 bdaddr_t *src = &sco_pi(sk)->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int err = 0;
583
584 BT_DBG("sk %p backlog %d", sk, backlog);
585
586 lock_sock(sk);
587
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200588 if (sk->sk_state != BT_BOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 err = -EBADFD;
590 goto done;
591 }
592
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200593 if (sk->sk_type != SOCK_SEQPACKET) {
594 err = -EINVAL;
595 goto done;
596 }
597
Marcel Holtmannfb334052012-04-19 14:37:58 +0200598 write_lock(&sco_sk_list.lock);
599
600 if (__sco_get_sock_listen_by_addr(src)) {
601 err = -EADDRINUSE;
602 goto unlock;
603 }
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 sk->sk_max_ack_backlog = backlog;
606 sk->sk_ack_backlog = 0;
Marcel Holtmannfb334052012-04-19 14:37:58 +0200607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 sk->sk_state = BT_LISTEN;
609
Marcel Holtmannfb334052012-04-19 14:37:58 +0200610unlock:
611 write_unlock(&sco_sk_list.lock);
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613done:
614 release_sock(sk);
615 return err;
616}
617
618static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
619{
Peter Hurleydfb2fae2015-01-23 12:16:53 -0500620 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct sock *sk = sock->sk, *ch;
622 long timeo;
623 int err = 0;
624
625 lock_sock(sk);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
628
629 BT_DBG("sk %p timeo %ld", sk, timeo);
630
631 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000632 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurley552b0d32011-07-24 00:11:01 -0400633 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (sk->sk_state != BT_LISTEN) {
635 err = -EBADFD;
636 break;
637 }
638
Peter Hurley552b0d32011-07-24 00:11:01 -0400639 ch = bt_accept_dequeue(sk, newsock);
640 if (ch)
641 break;
642
643 if (!timeo) {
644 err = -EAGAIN;
645 break;
646 }
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (signal_pending(current)) {
649 err = sock_intr_errno(timeo);
650 break;
651 }
Peter Hurley552b0d32011-07-24 00:11:01 -0400652
653 release_sock(sk);
Peter Hurleydfb2fae2015-01-23 12:16:53 -0500654
655 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
Peter Hurley552b0d32011-07-24 00:11:01 -0400656 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000658 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (err)
661 goto done;
662
663 newsock->state = SS_CONNECTED;
664
665 BT_DBG("new socket %p", ch);
666
667done:
668 release_sock(sk);
669 return err;
670}
671
672static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
673{
674 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
675 struct sock *sk = sock->sk;
676
677 BT_DBG("sock %p, sk %p", sock, sk);
678
679 addr->sa_family = AF_BLUETOOTH;
680 *len = sizeof(struct sockaddr_sco);
681
682 if (peer)
Marcel Holtmanneea96362013-10-13 10:34:01 -0700683 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 else
Marcel Holtmanneea96362013-10-13 10:34:01 -0700685 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 return 0;
688}
689
Ying Xue1b784142015-03-02 15:37:48 +0800690static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
691 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 struct sock *sk = sock->sk;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300694 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 BT_DBG("sock %p, sk %p", sock, sk);
697
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800698 err = sock_error(sk);
699 if (err)
700 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 if (msg->msg_flags & MSG_OOB)
703 return -EOPNOTSUPP;
704
705 lock_sock(sk);
706
707 if (sk->sk_state == BT_CONNECTED)
708 err = sco_send_frame(sk, msg, len);
709 else
710 err = -ENOTCONN;
711
712 release_sock(sk);
713 return err;
714}
715
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200716static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200717{
718 struct hci_dev *hdev = conn->hdev;
719
720 BT_DBG("conn %p", conn);
721
722 conn->state = BT_CONFIG;
723
724 if (!lmp_esco_capable(hdev)) {
725 struct hci_cp_accept_conn_req cp;
726
727 bacpy(&cp.bdaddr, &conn->dst);
Frédéric Dalleau33f24042013-08-19 14:23:55 +0200728 cp.role = 0x00; /* Ignored */
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200729
730 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
731 } else {
732 struct hci_cp_accept_sync_conn_req cp;
733
734 bacpy(&cp.bdaddr, &conn->dst);
735 cp.pkt_type = cpu_to_le16(conn->pkt_type);
736
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700737 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
738 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200739 cp.content_format = cpu_to_le16(setting);
740
741 switch (setting & SCO_AIRMODE_MASK) {
742 case SCO_AIRMODE_TRANSP:
743 if (conn->pkt_type & ESCO_2EV3)
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700744 cp.max_latency = cpu_to_le16(0x0008);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200745 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700746 cp.max_latency = cpu_to_le16(0x000D);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200747 cp.retrans_effort = 0x02;
748 break;
749 case SCO_AIRMODE_CVSD:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700750 cp.max_latency = cpu_to_le16(0xffff);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200751 cp.retrans_effort = 0xff;
752 break;
753 }
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200754
755 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
756 sizeof(cp), &cp);
757 }
758}
759
Ying Xue1b784142015-03-02 15:37:48 +0800760static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
761 size_t len, int flags)
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100762{
763 struct sock *sk = sock->sk;
764 struct sco_pinfo *pi = sco_pi(sk);
765
766 lock_sock(sk);
767
768 if (sk->sk_state == BT_CONNECT2 &&
769 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200770 sco_conn_defer_accept(pi->conn->hcon, pi->setting);
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100771 sk->sk_state = BT_CONFIG;
772
773 release_sock(sk);
774 return 0;
775 }
776
777 release_sock(sk);
778
Ying Xue1b784142015-03-02 15:37:48 +0800779 return bt_sock_recvmsg(sock, msg, len, flags);
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100780}
781
David S. Millerb7058842009-09-30 16:12:20 -0700782static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct sock *sk = sock->sk;
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200785 int len, err = 0;
786 struct bt_voice voice;
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100787 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 BT_DBG("sk %p", sk);
790
791 lock_sock(sk);
792
793 switch (optname) {
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100794
795 case BT_DEFER_SETUP:
796 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
797 err = -EINVAL;
798 break;
799 }
800
801 if (get_user(opt, (u32 __user *) optval)) {
802 err = -EFAULT;
803 break;
804 }
805
806 if (opt)
807 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
808 else
809 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
810 break;
811
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200812 case BT_VOICE:
813 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
814 sk->sk_state != BT_CONNECT2) {
815 err = -EINVAL;
816 break;
817 }
818
819 voice.setting = sco_pi(sk)->setting;
820
821 len = min_t(unsigned int, sizeof(voice), optlen);
822 if (copy_from_user((char *) &voice, optval, len)) {
823 err = -EFAULT;
824 break;
825 }
826
827 /* Explicitly check for these values */
828 if (voice.setting != BT_VOICE_TRANSPARENT &&
829 voice.setting != BT_VOICE_CVSD_16BIT) {
830 err = -EINVAL;
831 break;
832 }
833
834 sco_pi(sk)->setting = voice.setting;
835 break;
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 default:
838 err = -ENOPROTOOPT;
839 break;
840 }
841
842 release_sock(sk);
843 return err;
844}
845
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100846static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct sock *sk = sock->sk;
849 struct sco_options opts;
850 struct sco_conninfo cinfo;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900851 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 BT_DBG("sk %p", sk);
854
855 if (get_user(len, optlen))
856 return -EFAULT;
857
858 lock_sock(sk);
859
860 switch (optname) {
861 case SCO_OPTIONS:
Johan Hedberg9d225d22013-08-08 14:53:56 +0300862 if (sk->sk_state != BT_CONNECTED &&
863 !(sk->sk_state == BT_CONNECT2 &&
864 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 err = -ENOTCONN;
866 break;
867 }
868
869 opts.mtu = sco_pi(sk)->conn->mtu;
870
871 BT_DBG("mtu %d", opts.mtu);
872
873 len = min_t(unsigned int, len, sizeof(opts));
874 if (copy_to_user(optval, (char *)&opts, len))
875 err = -EFAULT;
876
877 break;
878
879 case SCO_CONNINFO:
Johan Hedberg9d225d22013-08-08 14:53:56 +0300880 if (sk->sk_state != BT_CONNECTED &&
881 !(sk->sk_state == BT_CONNECT2 &&
882 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 err = -ENOTCONN;
884 break;
885 }
886
Vasiliy Kulikovc4c896e2011-02-14 13:54:26 +0300887 memset(&cinfo, 0, sizeof(cinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
889 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
890
891 len = min_t(unsigned int, len, sizeof(cinfo));
892 if (copy_to_user(optval, (char *)&cinfo, len))
893 err = -EFAULT;
894
895 break;
896
897 default:
898 err = -ENOPROTOOPT;
899 break;
900 }
901
902 release_sock(sk);
903 return err;
904}
905
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100906static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
907{
908 struct sock *sk = sock->sk;
909 int len, err = 0;
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200910 struct bt_voice voice;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100911
912 BT_DBG("sk %p", sk);
913
914 if (level == SOL_SCO)
915 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
916
917 if (get_user(len, optlen))
918 return -EFAULT;
919
920 lock_sock(sk);
921
922 switch (optname) {
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100923
924 case BT_DEFER_SETUP:
925 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
926 err = -EINVAL;
927 break;
928 }
929
930 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
931 (u32 __user *) optval))
932 err = -EFAULT;
933
934 break;
935
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200936 case BT_VOICE:
937 voice.setting = sco_pi(sk)->setting;
938
939 len = min_t(unsigned int, len, sizeof(voice));
940 if (copy_to_user(optval, (char *)&voice, len))
941 err = -EFAULT;
942
943 break;
944
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100945 default:
946 err = -ENOPROTOOPT;
947 break;
948 }
949
950 release_sock(sk);
951 return err;
952}
953
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200954static int sco_sock_shutdown(struct socket *sock, int how)
955{
956 struct sock *sk = sock->sk;
957 int err = 0;
958
959 BT_DBG("sock %p, sk %p", sock, sk);
960
961 if (!sk)
962 return 0;
963
964 lock_sock(sk);
965 if (!sk->sk_shutdown) {
966 sk->sk_shutdown = SHUTDOWN_MASK;
967 sco_sock_clear_timer(sk);
968 __sco_sock_close(sk);
969
Vladimir Davydov093facf2014-07-15 12:25:28 +0400970 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
971 !(current->flags & PF_EXITING))
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200972 err = bt_sock_wait_state(sk, BT_CLOSED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300973 sk->sk_lingertime);
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200974 }
975 release_sock(sk);
976 return err;
977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979static int sco_sock_release(struct socket *sock)
980{
981 struct sock *sk = sock->sk;
982 int err = 0;
983
984 BT_DBG("sock %p, sk %p", sock, sk);
985
986 if (!sk)
987 return 0;
988
989 sco_sock_close(sk);
990
Vladimir Davydov093facf2014-07-15 12:25:28 +0400991 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
992 !(current->flags & PF_EXITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 lock_sock(sk);
994 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
995 release_sock(sk);
996 }
997
998 sock_orphan(sk);
999 sco_sock_kill(sk);
1000 return err;
1001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003static void sco_conn_ready(struct sco_conn *conn)
1004{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +02001005 struct sock *parent;
1006 struct sock *sk = conn->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 BT_DBG("conn %p", conn);
1009
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +02001010 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 sco_sock_clear_timer(sk);
1012 bh_lock_sock(sk);
1013 sk->sk_state = BT_CONNECTED;
1014 sk->sk_state_change(sk);
1015 bh_unlock_sock(sk);
1016 } else {
Andre Guedes40528082013-01-29 19:59:56 -03001017 sco_conn_lock(conn);
1018
Marcel Holtmann041987c2013-10-13 10:15:22 -07001019 parent = sco_get_sock_listen(&conn->hcon->src);
Andre Guedes40528082013-01-29 19:59:56 -03001020 if (!parent) {
1021 sco_conn_unlock(conn);
1022 return;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 bh_lock_sock(parent);
1026
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -03001027 sk = sco_sock_alloc(sock_net(parent), NULL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001028 BTPROTO_SCO, GFP_ATOMIC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (!sk) {
1030 bh_unlock_sock(parent);
Andre Guedes40528082013-01-29 19:59:56 -03001031 sco_conn_unlock(conn);
1032 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034
1035 sco_sock_init(sk, parent);
1036
Marcel Holtmanneea96362013-10-13 10:34:01 -07001037 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1038 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 hci_conn_hold(conn->hcon);
1041 __sco_chan_add(conn, sk, parent);
1042
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001043 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1044 sk->sk_state = BT_CONNECT2;
1045 else
1046 sk->sk_state = BT_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /* Wake up parent */
David S. Miller676d2362014-04-11 16:15:36 -04001049 parent->sk_data_ready(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 bh_unlock_sock(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Andre Guedes40528082013-01-29 19:59:56 -03001053 sco_conn_unlock(conn);
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057/* ----- SCO interface with lower layer (HCI) ----- */
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001058int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Gustavo Padovanfc5fef62012-05-23 04:04:19 -03001060 struct sock *sk;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001061 int lm = 0;
1062
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001063 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001065 /* Find listening sockets */
1066 read_lock(&sco_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001067 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001068 if (sk->sk_state != BT_LISTEN)
1069 continue;
1070
Marcel Holtmanneea96362013-10-13 10:34:01 -07001071 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1072 !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001073 lm |= HCI_LM_ACCEPT;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001074
1075 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1076 *flags |= HCI_PROTO_DEFER;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001077 break;
1078 }
1079 }
1080 read_unlock(&sco_sk_list.lock);
1081
1082 return lm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Johan Hedberg539c4962015-02-18 14:53:57 +02001085static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Johan Hedberg539c4962015-02-18 14:53:57 +02001087 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1088 return;
1089
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001090 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Johan Hedberg539c4962015-02-18 14:53:57 +02001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (!status) {
1093 struct sco_conn *conn;
1094
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +02001095 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (conn)
1097 sco_conn_ready(conn);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001098 } else
Joe Perchese1750722011-06-29 18:18:29 -07001099 sco_conn_del(hcon, bt_to_errno(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
Johan Hedberg3a6d5762015-02-18 14:53:58 +02001102static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Johan Hedberg3a6d5762015-02-18 14:53:58 +02001104 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1105 return;
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 BT_DBG("hcon %p reason %d", hcon, reason);
1108
Joe Perchese1750722011-06-29 18:18:29 -07001109 sco_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Arron Wang9b4c3332015-06-09 17:47:22 +08001112void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct sco_conn *conn = hcon->sco_data;
1115
1116 if (!conn)
1117 goto drop;
1118
1119 BT_DBG("conn %p len %d", conn, skb->len);
1120
1121 if (skb->len) {
1122 sco_recv_frame(conn, skb);
Arron Wang9b4c3332015-06-09 17:47:22 +08001123 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
1126drop:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001127 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Johan Hedberg539c4962015-02-18 14:53:57 +02001130static struct hci_cb sco_cb = {
1131 .name = "SCO",
1132 .connect_cfm = sco_connect_cfm,
Johan Hedberg3a6d5762015-02-18 14:53:58 +02001133 .disconn_cfm = sco_disconn_cfm,
Johan Hedberg539c4962015-02-18 14:53:57 +02001134};
1135
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001136static int sco_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Gustavo F. Padovanee65d192011-12-27 15:28:46 -02001140 read_lock(&sco_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Sasha Levinb67bfe02013-02-27 17:06:00 -08001142 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmanneea96362013-10-13 10:34:01 -07001143 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1144 &sco_pi(sk)->dst, sk->sk_state);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Gustavo F. Padovanee65d192011-12-27 15:28:46 -02001147 read_unlock(&sco_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001148
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001152static int sco_debugfs_open(struct inode *inode, struct file *file)
1153{
1154 return single_open(file, sco_debugfs_show, inode->i_private);
1155}
1156
1157static const struct file_operations sco_debugfs_fops = {
1158 .open = sco_debugfs_open,
1159 .read = seq_read,
1160 .llseek = seq_lseek,
1161 .release = single_release,
1162};
1163
1164static struct dentry *sco_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001166static const struct proto_ops sco_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 .family = PF_BLUETOOTH,
1168 .owner = THIS_MODULE,
1169 .release = sco_sock_release,
1170 .bind = sco_sock_bind,
1171 .connect = sco_sock_connect,
1172 .listen = sco_sock_listen,
1173 .accept = sco_sock_accept,
1174 .getname = sco_sock_getname,
1175 .sendmsg = sco_sock_sendmsg,
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001176 .recvmsg = sco_sock_recvmsg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 .poll = bt_sock_poll,
Marcel Holtmann3241ad82008-07-14 20:13:50 +02001178 .ioctl = bt_sock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 .mmap = sock_no_mmap,
1180 .socketpair = sock_no_socketpair,
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +02001181 .shutdown = sco_sock_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 .setsockopt = sco_sock_setsockopt,
1183 .getsockopt = sco_sock_getsockopt
1184};
1185
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001186static const struct net_proto_family sco_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 .family = PF_BLUETOOTH,
1188 .owner = THIS_MODULE,
1189 .create = sco_sock_create,
1190};
1191
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001192int __init sco_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 int err;
1195
Marcel Holtmann15762fa2015-01-11 15:21:06 -08001196 BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr));
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 err = proto_register(&sco_proto, 0);
1199 if (err < 0)
1200 return err;
1201
1202 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1203 if (err < 0) {
1204 BT_ERR("SCO socket registration failed");
1205 goto error;
1206 }
1207
Al Virob0316612013-04-04 19:14:33 -04001208 err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
Masatake YAMATOde9b9212012-07-26 01:30:12 +09001209 if (err < 0) {
1210 BT_ERR("Failed to create SCO proc file");
1211 bt_sock_unregister(BTPROTO_SCO);
1212 goto error;
1213 }
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 BT_INFO("SCO socket layer initialized");
1216
Johan Hedberg539c4962015-02-18 14:53:57 +02001217 hci_register_cb(&sco_cb);
1218
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07001219 if (IS_ERR_OR_NULL(bt_debugfs))
1220 return 0;
1221
1222 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1223 NULL, &sco_debugfs_fops);
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return 0;
1226
1227error:
1228 proto_unregister(&sco_proto);
1229 return err;
1230}
1231
Alexander Aring0402d9f22015-03-07 20:52:28 +01001232void sco_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Masatake YAMATOde9b9212012-07-26 01:30:12 +09001234 bt_procfs_cleanup(&init_net, "sco");
1235
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001236 debugfs_remove(sco_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Johan Hedberg539c4962015-02-18 14:53:57 +02001238 hci_unregister_cb(&sco_cb);
1239
David Herrmann5e9d7f82013-02-24 19:36:51 +01001240 bt_sock_unregister(BTPROTO_SCO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 proto_unregister(&sco_proto);
1243}
1244
Marcel Holtmann7cb127d2008-07-14 20:13:53 +02001245module_param(disable_esco, bool, 0644);
1246MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");