blob: 3125ce670c2f241f446daae17a37fbcc5f485574 [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{
Marcel Holtmannc4297e82015-10-26 02:08:38 +010077 struct sock *sk = (struct sock *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
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) {
Kuba Pawlak75e34f52015-10-05 18:44:15 +0200173 sock_hold(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 bh_lock_sock(sk);
175 sco_sock_clear_timer(sk);
176 sco_chan_del(sk, err);
177 bh_unlock_sock(sk);
178 sco_sock_kill(sk);
Kuba Pawlak75e34f52015-10-05 18:44:15 +0200179 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
182 hcon->sco_data = NULL;
183 kfree(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Marcel Holtmannc4297e82015-10-26 02:08:38 +0100186static void __sco_chan_add(struct sco_conn *conn, struct sock *sk,
187 struct sock *parent)
Marcel Holtmann015b01c2014-07-13 19:54:48 +0200188{
189 BT_DBG("conn %p", conn);
190
191 sco_pi(sk)->conn = conn;
192 conn->sk = sk;
193
194 if (parent)
195 bt_accept_enqueue(parent, sk);
196}
197
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300198static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
199 struct sock *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 int err = 0;
202
203 sco_conn_lock(conn);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300204 if (conn->sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 err = -EBUSY;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300206 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 __sco_chan_add(conn, sk, parent);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 sco_conn_unlock(conn);
210 return err;
211}
212
213static int sco_connect(struct sock *sk)
214{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct sco_conn *conn;
216 struct hci_conn *hcon;
217 struct hci_dev *hdev;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200218 int err, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Marcel Holtmanneea96362013-10-13 10:34:01 -0700220 BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Johan Hedberg39385cb2016-11-12 17:03:07 +0200222 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src, BDADDR_BREDR);
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200223 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EHOSTUNREACH;
225
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300226 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Marcel Holtmann7cb127d2008-07-14 20:13:53 +0200228 if (lmp_esco_capable(hdev) && !disable_esco)
229 type = ESCO_LINK;
230 else
231 type = SCO_LINK;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200232
Frédéric Dalleau79dc0082013-08-19 14:24:01 +0200233 if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
234 (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev))) {
235 err = -EOPNOTSUPP;
236 goto done;
237 }
238
Marcel Holtmanneea96362013-10-13 10:34:01 -0700239 hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
Marcel Holtmann041987c2013-10-13 10:15:22 -0700240 sco_pi(sk)->setting);
Ville Tervo30e76272011-02-22 16:10:53 -0300241 if (IS_ERR(hcon)) {
242 err = PTR_ERR(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto done;
Ville Tervo30e76272011-02-22 16:10:53 -0300244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +0200246 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!conn) {
David Herrmann76a68ba2013-04-06 20:28:37 +0200248 hci_conn_drop(hcon);
Ville Tervo30e76272011-02-22 16:10:53 -0300249 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto done;
251 }
252
253 /* Update source addr of the socket */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700254 bacpy(&sco_pi(sk)->src, &hcon->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 err = sco_chan_add(conn, sk, NULL);
257 if (err)
258 goto done;
259
260 if (hcon->state == BT_CONNECTED) {
261 sco_sock_clear_timer(sk);
262 sk->sk_state = BT_CONNECTED;
263 } else {
264 sk->sk_state = BT_CONNECT;
265 sco_sock_set_timer(sk, sk->sk_sndtimeo);
266 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300269 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 hci_dev_put(hdev);
271 return err;
272}
273
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300274static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct sco_conn *conn = sco_pi(sk)->conn;
277 struct sk_buff *skb;
Mikel Astiz088ce082012-04-11 08:48:48 +0200278 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* Check outgoing MTU */
281 if (len > conn->mtu)
282 return -EINVAL;
283
284 BT_DBG("sk %p len %d", sk, len);
285
Mikel Astiz088ce082012-04-11 08:48:48 +0200286 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300287 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return err;
289
Al Viro6ce8e9c2014-04-06 21:25:44 -0400290 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300291 kfree_skb(skb);
292 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -0300295 hci_send_sco(conn->hcon, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Mikel Astiz088ce082012-04-11 08:48:48 +0200297 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300300static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Marcel Holtmanneb5a4de2014-07-14 01:30:15 +0200302 struct sock *sk;
303
304 sco_conn_lock(conn);
305 sk = conn->sk;
306 sco_conn_unlock(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
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
319drop:
320 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323/* -------- Socket interface ---------- */
Marcel Holtmannfb334052012-04-19 14:37:58 +0200324static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Marcel Holtmannfb334052012-04-19 14:37:58 +0200326 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Sasha Levinb67bfe02013-02-27 17:06:00 -0800328 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmannfb334052012-04-19 14:37:58 +0200329 if (sk->sk_state != BT_LISTEN)
330 continue;
331
Marcel Holtmanneea96362013-10-13 10:34:01 -0700332 if (!bacmp(&sco_pi(sk)->src, ba))
Marcel Holtmannfb334052012-04-19 14:37:58 +0200333 return sk;
334 }
335
336 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339/* Find socket listening on source bdaddr.
340 * Returns closest match.
341 */
342static struct sock *sco_get_sock_listen(bdaddr_t *src)
343{
344 struct sock *sk = NULL, *sk1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 read_lock(&sco_sk_list.lock);
347
Sasha Levinb67bfe02013-02-27 17:06:00 -0800348 sk_for_each(sk, &sco_sk_list.head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (sk->sk_state != BT_LISTEN)
350 continue;
351
352 /* Exact match. */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700353 if (!bacmp(&sco_pi(sk)->src, src))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 break;
355
356 /* Closest match */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700357 if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 sk1 = sk;
359 }
360
361 read_unlock(&sco_sk_list.lock);
362
Sasha Levinb67bfe02013-02-27 17:06:00 -0800363 return sk ? sk : sk1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366static 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
374static 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 */
393static 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 Holtmannfd0b3ff2009-06-16 00:01:49 +0200406static void __sco_sock_close(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200408 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
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 Padovanb7e98b52013-01-03 19:59:28 -0200417 if (sco_pi(sk)->conn->hcon) {
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300418 sk->sk_state = BT_DISCONN;
419 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
Kuba Pawlak435c5132015-10-05 18:44:16 +0200420 sco_conn_lock(sco_pi(sk)->conn);
David Herrmann76a68ba2013-04-06 20:28:37 +0200421 hci_conn_drop(sco_pi(sk)->conn->hcon);
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300422 sco_pi(sk)->conn->hcon = NULL;
Kuba Pawlak435c5132015-10-05 18:44:16 +0200423 sco_conn_unlock(sco_pi(sk)->conn);
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300424 } else
425 sco_chan_del(sk, ECONNRESET);
426 break;
427
Vinicius Costa Gomeseb20ff92013-03-13 19:46:20 -0300428 case BT_CONNECT2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 case BT_CONNECT:
430 case BT_DISCONN:
431 sco_chan_del(sk, ECONNRESET);
432 break;
433
434 default:
435 sock_set_flag(sk, SOCK_ZAPPED);
436 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700437 }
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200438}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200440/* Must be called on unlocked socket. */
441static void sco_sock_close(struct sock *sk)
442{
443 sco_sock_clear_timer(sk);
444 lock_sock(sk);
445 __sco_sock_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 sco_sock_kill(sk);
448}
449
450static void sco_sock_init(struct sock *sk, struct sock *parent)
451{
452 BT_DBG("sk %p", sk);
453
Paul Moore6230c9b2011-10-07 09:40:59 +0000454 if (parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 sk->sk_type = parent->sk_type;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100456 bt_sk(sk)->flags = bt_sk(parent)->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +0000457 security_sk_clone(parent, sk);
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461static struct proto sco_proto = {
462 .name = "SCO",
463 .owner = THIS_MODULE,
464 .obj_size = sizeof(struct sco_pinfo)
465};
466
Marcel Holtmannc4297e82015-10-26 02:08:38 +0100467static struct sock *sco_sock_alloc(struct net *net, struct socket *sock,
468 int proto, gfp_t prio, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct sock *sk;
471
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500472 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!sk)
474 return NULL;
475
476 sock_init_data(sock, sk);
477 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
478
479 sk->sk_destruct = sco_sock_destruct;
480 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
481
482 sock_reset_flag(sk, SOCK_ZAPPED);
483
484 sk->sk_protocol = proto;
485 sk->sk_state = BT_OPEN;
486
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200487 sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
488
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800489 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 bt_sock_link(&sco_sk_list, sk);
492 return sk;
493}
494
Eric Paris3f378b62009-11-05 22:18:14 -0800495static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
496 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct sock *sk;
499
500 BT_DBG("sock %p", sock);
501
502 sock->state = SS_UNCONNECTED;
503
504 if (sock->type != SOCK_SEQPACKET)
505 return -ESOCKTNOSUPPORT;
506
507 sock->ops = &sco_sock_ops;
508
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500509 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200510 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return -ENOMEM;
512
513 sco_sock_init(sk, NULL);
514 return 0;
515}
516
Marcel Holtmannc4297e82015-10-26 02:08:38 +0100517static int sco_sock_bind(struct socket *sock, struct sockaddr *addr,
518 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
521 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int err = 0;
523
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300524 BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (!addr || addr->sa_family != AF_BLUETOOTH)
527 return -EINVAL;
528
David S. Miller52332522015-12-15 15:39:08 -0500529 if (addr_len < sizeof(struct sockaddr_sco))
530 return -EINVAL;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 lock_sock(sk);
533
534 if (sk->sk_state != BT_OPEN) {
535 err = -EBADFD;
536 goto done;
537 }
538
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200539 if (sk->sk_type != SOCK_SEQPACKET) {
540 err = -EINVAL;
541 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
Marcel Holtmanneea96362013-10-13 10:34:01 -0700544 bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200545
546 sk->sk_state = BT_BOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548done:
549 release_sock(sk);
550 return err;
551}
552
553static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
554{
555 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
556 struct sock *sk = sock->sk;
Claudio Takahasi92f185c2013-04-11 11:35:46 -0300557 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 BT_DBG("sk %p", sk);
560
Changli Gao6503d962010-03-31 22:58:26 +0000561 if (alen < sizeof(struct sockaddr_sco) ||
562 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return -EINVAL;
564
565 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
566 return -EBADFD;
567
568 if (sk->sk_type != SOCK_SEQPACKET)
569 return -EINVAL;
570
571 lock_sock(sk);
572
573 /* Set destination address and psm */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700574 bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200576 err = sco_connect(sk);
577 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto done;
579
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900580 err = bt_sock_wait_state(sk, BT_CONNECTED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300581 sock_sndtimeo(sk, flags & O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583done:
584 release_sock(sk);
585 return err;
586}
587
588static int sco_sock_listen(struct socket *sock, int backlog)
589{
590 struct sock *sk = sock->sk;
Marcel Holtmanneea96362013-10-13 10:34:01 -0700591 bdaddr_t *src = &sco_pi(sk)->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int err = 0;
593
594 BT_DBG("sk %p backlog %d", sk, backlog);
595
596 lock_sock(sk);
597
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200598 if (sk->sk_state != BT_BOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 err = -EBADFD;
600 goto done;
601 }
602
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200603 if (sk->sk_type != SOCK_SEQPACKET) {
604 err = -EINVAL;
605 goto done;
606 }
607
Marcel Holtmannfb334052012-04-19 14:37:58 +0200608 write_lock(&sco_sk_list.lock);
609
610 if (__sco_get_sock_listen_by_addr(src)) {
611 err = -EADDRINUSE;
612 goto unlock;
613 }
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 sk->sk_max_ack_backlog = backlog;
616 sk->sk_ack_backlog = 0;
Marcel Holtmannfb334052012-04-19 14:37:58 +0200617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 sk->sk_state = BT_LISTEN;
619
Marcel Holtmannfb334052012-04-19 14:37:58 +0200620unlock:
621 write_unlock(&sco_sk_list.lock);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623done:
624 release_sock(sk);
625 return err;
626}
627
Marcel Holtmannc4297e82015-10-26 02:08:38 +0100628static int sco_sock_accept(struct socket *sock, struct socket *newsock,
629 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Peter Hurleydfb2fae2015-01-23 12:16:53 -0500631 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct sock *sk = sock->sk, *ch;
633 long timeo;
634 int err = 0;
635
636 lock_sock(sk);
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
639
640 BT_DBG("sk %p timeo %ld", sk, timeo);
641
642 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000643 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurley552b0d32011-07-24 00:11:01 -0400644 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (sk->sk_state != BT_LISTEN) {
646 err = -EBADFD;
647 break;
648 }
649
Peter Hurley552b0d32011-07-24 00:11:01 -0400650 ch = bt_accept_dequeue(sk, newsock);
651 if (ch)
652 break;
653
654 if (!timeo) {
655 err = -EAGAIN;
656 break;
657 }
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (signal_pending(current)) {
660 err = sock_intr_errno(timeo);
661 break;
662 }
Peter Hurley552b0d32011-07-24 00:11:01 -0400663
664 release_sock(sk);
Peter Hurleydfb2fae2015-01-23 12:16:53 -0500665
666 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
Peter Hurley552b0d32011-07-24 00:11:01 -0400667 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000669 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (err)
672 goto done;
673
674 newsock->state = SS_CONNECTED;
675
676 BT_DBG("new socket %p", ch);
677
678done:
679 release_sock(sk);
680 return err;
681}
682
Marcel Holtmannc4297e82015-10-26 02:08:38 +0100683static int sco_sock_getname(struct socket *sock, struct sockaddr *addr,
684 int *len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
687 struct sock *sk = sock->sk;
688
689 BT_DBG("sock %p, sk %p", sock, sk);
690
691 addr->sa_family = AF_BLUETOOTH;
692 *len = sizeof(struct sockaddr_sco);
693
694 if (peer)
Marcel Holtmanneea96362013-10-13 10:34:01 -0700695 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 else
Marcel Holtmanneea96362013-10-13 10:34:01 -0700697 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 return 0;
700}
701
Ying Xue1b784142015-03-02 15:37:48 +0800702static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
703 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct sock *sk = sock->sk;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300706 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 BT_DBG("sock %p, sk %p", sock, sk);
709
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800710 err = sock_error(sk);
711 if (err)
712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 if (msg->msg_flags & MSG_OOB)
715 return -EOPNOTSUPP;
716
717 lock_sock(sk);
718
719 if (sk->sk_state == BT_CONNECTED)
720 err = sco_send_frame(sk, msg, len);
721 else
722 err = -ENOTCONN;
723
724 release_sock(sk);
725 return err;
726}
727
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200728static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200729{
730 struct hci_dev *hdev = conn->hdev;
731
732 BT_DBG("conn %p", conn);
733
734 conn->state = BT_CONFIG;
735
736 if (!lmp_esco_capable(hdev)) {
737 struct hci_cp_accept_conn_req cp;
738
739 bacpy(&cp.bdaddr, &conn->dst);
Frédéric Dalleau33f24042013-08-19 14:23:55 +0200740 cp.role = 0x00; /* Ignored */
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200741
742 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
743 } else {
744 struct hci_cp_accept_sync_conn_req cp;
745
746 bacpy(&cp.bdaddr, &conn->dst);
747 cp.pkt_type = cpu_to_le16(conn->pkt_type);
748
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700749 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
750 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200751 cp.content_format = cpu_to_le16(setting);
752
753 switch (setting & SCO_AIRMODE_MASK) {
754 case SCO_AIRMODE_TRANSP:
755 if (conn->pkt_type & ESCO_2EV3)
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700756 cp.max_latency = cpu_to_le16(0x0008);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200757 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700758 cp.max_latency = cpu_to_le16(0x000D);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200759 cp.retrans_effort = 0x02;
760 break;
761 case SCO_AIRMODE_CVSD:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700762 cp.max_latency = cpu_to_le16(0xffff);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200763 cp.retrans_effort = 0xff;
764 break;
765 }
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200766
767 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
768 sizeof(cp), &cp);
769 }
770}
771
Ying Xue1b784142015-03-02 15:37:48 +0800772static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
773 size_t len, int flags)
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100774{
775 struct sock *sk = sock->sk;
776 struct sco_pinfo *pi = sco_pi(sk);
777
778 lock_sock(sk);
779
780 if (sk->sk_state == BT_CONNECT2 &&
781 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200782 sco_conn_defer_accept(pi->conn->hcon, pi->setting);
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100783 sk->sk_state = BT_CONFIG;
784
785 release_sock(sk);
786 return 0;
787 }
788
789 release_sock(sk);
790
Ying Xue1b784142015-03-02 15:37:48 +0800791 return bt_sock_recvmsg(sock, msg, len, flags);
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100792}
793
Marcel Holtmannc4297e82015-10-26 02:08:38 +0100794static int sco_sock_setsockopt(struct socket *sock, int level, int optname,
795 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct sock *sk = sock->sk;
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200798 int len, err = 0;
799 struct bt_voice voice;
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100800 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 BT_DBG("sk %p", sk);
803
804 lock_sock(sk);
805
806 switch (optname) {
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100807
808 case BT_DEFER_SETUP:
809 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
810 err = -EINVAL;
811 break;
812 }
813
814 if (get_user(opt, (u32 __user *) optval)) {
815 err = -EFAULT;
816 break;
817 }
818
819 if (opt)
820 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
821 else
822 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
823 break;
824
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200825 case BT_VOICE:
826 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
827 sk->sk_state != BT_CONNECT2) {
828 err = -EINVAL;
829 break;
830 }
831
832 voice.setting = sco_pi(sk)->setting;
833
834 len = min_t(unsigned int, sizeof(voice), optlen);
Marcel Holtmannc4297e82015-10-26 02:08:38 +0100835 if (copy_from_user((char *)&voice, optval, len)) {
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200836 err = -EFAULT;
837 break;
838 }
839
840 /* Explicitly check for these values */
841 if (voice.setting != BT_VOICE_TRANSPARENT &&
842 voice.setting != BT_VOICE_CVSD_16BIT) {
843 err = -EINVAL;
844 break;
845 }
846
847 sco_pi(sk)->setting = voice.setting;
848 break;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 default:
851 err = -ENOPROTOOPT;
852 break;
853 }
854
855 release_sock(sk);
856 return err;
857}
858
Marcel Holtmannc4297e82015-10-26 02:08:38 +0100859static int sco_sock_getsockopt_old(struct socket *sock, int optname,
860 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 struct sock *sk = sock->sk;
863 struct sco_options opts;
864 struct sco_conninfo cinfo;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900865 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 BT_DBG("sk %p", sk);
868
869 if (get_user(len, optlen))
870 return -EFAULT;
871
872 lock_sock(sk);
873
874 switch (optname) {
875 case SCO_OPTIONS:
Johan Hedberg9d225d22013-08-08 14:53:56 +0300876 if (sk->sk_state != BT_CONNECTED &&
877 !(sk->sk_state == BT_CONNECT2 &&
878 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 err = -ENOTCONN;
880 break;
881 }
882
883 opts.mtu = sco_pi(sk)->conn->mtu;
884
885 BT_DBG("mtu %d", opts.mtu);
886
887 len = min_t(unsigned int, len, sizeof(opts));
888 if (copy_to_user(optval, (char *)&opts, len))
889 err = -EFAULT;
890
891 break;
892
893 case SCO_CONNINFO:
Johan Hedberg9d225d22013-08-08 14:53:56 +0300894 if (sk->sk_state != BT_CONNECTED &&
895 !(sk->sk_state == BT_CONNECT2 &&
896 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 err = -ENOTCONN;
898 break;
899 }
900
Vasiliy Kulikovc4c896e2011-02-14 13:54:26 +0300901 memset(&cinfo, 0, sizeof(cinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
903 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
904
905 len = min_t(unsigned int, len, sizeof(cinfo));
906 if (copy_to_user(optval, (char *)&cinfo, len))
907 err = -EFAULT;
908
909 break;
910
911 default:
912 err = -ENOPROTOOPT;
913 break;
914 }
915
916 release_sock(sk);
917 return err;
918}
919
Marcel Holtmannc4297e82015-10-26 02:08:38 +0100920static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
921 char __user *optval, int __user *optlen)
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100922{
923 struct sock *sk = sock->sk;
924 int len, err = 0;
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200925 struct bt_voice voice;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100926
927 BT_DBG("sk %p", sk);
928
929 if (level == SOL_SCO)
930 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
931
932 if (get_user(len, optlen))
933 return -EFAULT;
934
935 lock_sock(sk);
936
937 switch (optname) {
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100938
939 case BT_DEFER_SETUP:
940 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
941 err = -EINVAL;
942 break;
943 }
944
945 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
Marcel Holtmannc4297e82015-10-26 02:08:38 +0100946 (u32 __user *)optval))
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100947 err = -EFAULT;
948
949 break;
950
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200951 case BT_VOICE:
952 voice.setting = sco_pi(sk)->setting;
953
954 len = min_t(unsigned int, len, sizeof(voice));
955 if (copy_to_user(optval, (char *)&voice, len))
956 err = -EFAULT;
957
958 break;
959
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100960 default:
961 err = -ENOPROTOOPT;
962 break;
963 }
964
965 release_sock(sk);
966 return err;
967}
968
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200969static int sco_sock_shutdown(struct socket *sock, int how)
970{
971 struct sock *sk = sock->sk;
972 int err = 0;
973
974 BT_DBG("sock %p, sk %p", sock, sk);
975
976 if (!sk)
977 return 0;
978
Kuba Pawlak1da55372015-10-05 18:44:17 +0200979 sock_hold(sk);
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200980 lock_sock(sk);
Kuba Pawlak1da55372015-10-05 18:44:17 +0200981
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200982 if (!sk->sk_shutdown) {
983 sk->sk_shutdown = SHUTDOWN_MASK;
984 sco_sock_clear_timer(sk);
985 __sco_sock_close(sk);
986
Vladimir Davydov093facf2014-07-15 12:25:28 +0400987 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
988 !(current->flags & PF_EXITING))
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200989 err = bt_sock_wait_state(sk, BT_CLOSED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300990 sk->sk_lingertime);
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200991 }
Kuba Pawlak1da55372015-10-05 18:44:17 +0200992
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200993 release_sock(sk);
Kuba Pawlak1da55372015-10-05 18:44:17 +0200994 sock_put(sk);
995
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200996 return err;
997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999static int sco_sock_release(struct socket *sock)
1000{
1001 struct sock *sk = sock->sk;
1002 int err = 0;
1003
1004 BT_DBG("sock %p, sk %p", sock, sk);
1005
1006 if (!sk)
1007 return 0;
1008
1009 sco_sock_close(sk);
1010
Vladimir Davydov093facf2014-07-15 12:25:28 +04001011 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1012 !(current->flags & PF_EXITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 lock_sock(sk);
1014 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1015 release_sock(sk);
1016 }
1017
1018 sock_orphan(sk);
1019 sco_sock_kill(sk);
1020 return err;
1021}
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023static void sco_conn_ready(struct sco_conn *conn)
1024{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +02001025 struct sock *parent;
1026 struct sock *sk = conn->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 BT_DBG("conn %p", conn);
1029
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +02001030 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 sco_sock_clear_timer(sk);
1032 bh_lock_sock(sk);
1033 sk->sk_state = BT_CONNECTED;
1034 sk->sk_state_change(sk);
1035 bh_unlock_sock(sk);
1036 } else {
Andre Guedes40528082013-01-29 19:59:56 -03001037 sco_conn_lock(conn);
1038
Kuba Pawlak2c501cd2015-10-26 16:17:14 +00001039 if (!conn->hcon) {
1040 sco_conn_unlock(conn);
1041 return;
1042 }
1043
Marcel Holtmann041987c2013-10-13 10:15:22 -07001044 parent = sco_get_sock_listen(&conn->hcon->src);
Andre Guedes40528082013-01-29 19:59:56 -03001045 if (!parent) {
1046 sco_conn_unlock(conn);
1047 return;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 bh_lock_sock(parent);
1051
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -03001052 sk = sco_sock_alloc(sock_net(parent), NULL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001053 BTPROTO_SCO, GFP_ATOMIC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!sk) {
1055 bh_unlock_sock(parent);
Andre Guedes40528082013-01-29 19:59:56 -03001056 sco_conn_unlock(conn);
1057 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
1060 sco_sock_init(sk, parent);
1061
Marcel Holtmanneea96362013-10-13 10:34:01 -07001062 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1063 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 hci_conn_hold(conn->hcon);
1066 __sco_chan_add(conn, sk, parent);
1067
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001068 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1069 sk->sk_state = BT_CONNECT2;
1070 else
1071 sk->sk_state = BT_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 /* Wake up parent */
David S. Miller676d2362014-04-11 16:15:36 -04001074 parent->sk_data_ready(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 bh_unlock_sock(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Andre Guedes40528082013-01-29 19:59:56 -03001078 sco_conn_unlock(conn);
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082/* ----- SCO interface with lower layer (HCI) ----- */
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001083int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Gustavo Padovanfc5fef62012-05-23 04:04:19 -03001085 struct sock *sk;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001086 int lm = 0;
1087
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001088 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001090 /* Find listening sockets */
1091 read_lock(&sco_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001092 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001093 if (sk->sk_state != BT_LISTEN)
1094 continue;
1095
Marcel Holtmanneea96362013-10-13 10:34:01 -07001096 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1097 !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001098 lm |= HCI_LM_ACCEPT;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001099
1100 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1101 *flags |= HCI_PROTO_DEFER;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001102 break;
1103 }
1104 }
1105 read_unlock(&sco_sk_list.lock);
1106
1107 return lm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Johan Hedberg539c4962015-02-18 14:53:57 +02001110static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Johan Hedberg539c4962015-02-18 14:53:57 +02001112 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1113 return;
1114
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001115 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Johan Hedberg539c4962015-02-18 14:53:57 +02001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!status) {
1118 struct sco_conn *conn;
1119
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +02001120 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (conn)
1122 sco_conn_ready(conn);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001123 } else
Joe Perchese1750722011-06-29 18:18:29 -07001124 sco_conn_del(hcon, bt_to_errno(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Johan Hedberg3a6d5762015-02-18 14:53:58 +02001127static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Johan Hedberg3a6d5762015-02-18 14:53:58 +02001129 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1130 return;
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 BT_DBG("hcon %p reason %d", hcon, reason);
1133
Joe Perchese1750722011-06-29 18:18:29 -07001134 sco_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
Arron Wang9b4c3332015-06-09 17:47:22 +08001137void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 struct sco_conn *conn = hcon->sco_data;
1140
1141 if (!conn)
1142 goto drop;
1143
1144 BT_DBG("conn %p len %d", conn, skb->len);
1145
1146 if (skb->len) {
1147 sco_recv_frame(conn, skb);
Arron Wang9b4c3332015-06-09 17:47:22 +08001148 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150
1151drop:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001152 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Johan Hedberg539c4962015-02-18 14:53:57 +02001155static struct hci_cb sco_cb = {
1156 .name = "SCO",
1157 .connect_cfm = sco_connect_cfm,
Johan Hedberg3a6d5762015-02-18 14:53:58 +02001158 .disconn_cfm = sco_disconn_cfm,
Johan Hedberg539c4962015-02-18 14:53:57 +02001159};
1160
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001161static int sco_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Gustavo F. Padovanee65d192011-12-27 15:28:46 -02001165 read_lock(&sco_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Sasha Levinb67bfe02013-02-27 17:06:00 -08001167 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmanneea96362013-10-13 10:34:01 -07001168 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1169 &sco_pi(sk)->dst, sk->sk_state);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Gustavo F. Padovanee65d192011-12-27 15:28:46 -02001172 read_unlock(&sco_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001173
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001177static int sco_debugfs_open(struct inode *inode, struct file *file)
1178{
1179 return single_open(file, sco_debugfs_show, inode->i_private);
1180}
1181
1182static const struct file_operations sco_debugfs_fops = {
1183 .open = sco_debugfs_open,
1184 .read = seq_read,
1185 .llseek = seq_lseek,
1186 .release = single_release,
1187};
1188
1189static struct dentry *sco_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001191static const struct proto_ops sco_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 .family = PF_BLUETOOTH,
1193 .owner = THIS_MODULE,
1194 .release = sco_sock_release,
1195 .bind = sco_sock_bind,
1196 .connect = sco_sock_connect,
1197 .listen = sco_sock_listen,
1198 .accept = sco_sock_accept,
1199 .getname = sco_sock_getname,
1200 .sendmsg = sco_sock_sendmsg,
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001201 .recvmsg = sco_sock_recvmsg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 .poll = bt_sock_poll,
Marcel Holtmann3241ad82008-07-14 20:13:50 +02001203 .ioctl = bt_sock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 .mmap = sock_no_mmap,
1205 .socketpair = sock_no_socketpair,
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +02001206 .shutdown = sco_sock_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 .setsockopt = sco_sock_setsockopt,
1208 .getsockopt = sco_sock_getsockopt
1209};
1210
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001211static const struct net_proto_family sco_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 .family = PF_BLUETOOTH,
1213 .owner = THIS_MODULE,
1214 .create = sco_sock_create,
1215};
1216
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001217int __init sco_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 int err;
1220
Marcel Holtmann15762fa2015-01-11 15:21:06 -08001221 BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr));
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 err = proto_register(&sco_proto, 0);
1224 if (err < 0)
1225 return err;
1226
1227 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1228 if (err < 0) {
1229 BT_ERR("SCO socket registration failed");
1230 goto error;
1231 }
1232
Al Virob0316612013-04-04 19:14:33 -04001233 err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
Masatake YAMATOde9b9212012-07-26 01:30:12 +09001234 if (err < 0) {
1235 BT_ERR("Failed to create SCO proc file");
1236 bt_sock_unregister(BTPROTO_SCO);
1237 goto error;
1238 }
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 BT_INFO("SCO socket layer initialized");
1241
Johan Hedberg539c4962015-02-18 14:53:57 +02001242 hci_register_cb(&sco_cb);
1243
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07001244 if (IS_ERR_OR_NULL(bt_debugfs))
1245 return 0;
1246
1247 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1248 NULL, &sco_debugfs_fops);
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251
1252error:
1253 proto_unregister(&sco_proto);
1254 return err;
1255}
1256
Alexander Aring0402d9f22015-03-07 20:52:28 +01001257void sco_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Masatake YAMATOde9b9212012-07-26 01:30:12 +09001259 bt_procfs_cleanup(&init_net, "sco");
1260
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001261 debugfs_remove(sco_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Johan Hedberg539c4962015-02-18 14:53:57 +02001263 hci_unregister_cb(&sco_cb);
1264
David Herrmann5e9d7f82013-02-24 19:36:51 +01001265 bt_sock_unregister(BTPROTO_SCO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 proto_unregister(&sco_proto);
1268}
1269
Marcel Holtmann7cb127d2008-07-14 20:13:53 +02001270module_param(disable_esco, bool, 0644);
1271MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");