blob: e21c53b08da4b69d1954891ff91850bedbeb66ff [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) {
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 Holtmann015b01c2014-07-13 19:54:48 +0200186static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
187{
188 BT_DBG("conn %p", conn);
189
190 sco_pi(sk)->conn = conn;
191 conn->sk = sk;
192
193 if (parent)
194 bt_accept_enqueue(parent, sk);
195}
196
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300197static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
198 struct sock *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 int err = 0;
201
202 sco_conn_lock(conn);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300203 if (conn->sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 err = -EBUSY;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300205 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 __sco_chan_add(conn, sk, parent);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 sco_conn_unlock(conn);
209 return err;
210}
211
212static int sco_connect(struct sock *sk)
213{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct sco_conn *conn;
215 struct hci_conn *hcon;
216 struct hci_dev *hdev;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200217 int err, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Marcel Holtmanneea96362013-10-13 10:34:01 -0700219 BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Marcel Holtmanneea96362013-10-13 10:34:01 -0700221 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src);
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200222 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -EHOSTUNREACH;
224
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300225 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Marcel Holtmann7cb127d2008-07-14 20:13:53 +0200227 if (lmp_esco_capable(hdev) && !disable_esco)
228 type = ESCO_LINK;
229 else
230 type = SCO_LINK;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200231
Frédéric Dalleau79dc0082013-08-19 14:24:01 +0200232 if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
233 (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev))) {
234 err = -EOPNOTSUPP;
235 goto done;
236 }
237
Marcel Holtmanneea96362013-10-13 10:34:01 -0700238 hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
Marcel Holtmann041987c2013-10-13 10:15:22 -0700239 sco_pi(sk)->setting);
Ville Tervo30e76272011-02-22 16:10:53 -0300240 if (IS_ERR(hcon)) {
241 err = PTR_ERR(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto done;
Ville Tervo30e76272011-02-22 16:10:53 -0300243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +0200245 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!conn) {
David Herrmann76a68ba2013-04-06 20:28:37 +0200247 hci_conn_drop(hcon);
Ville Tervo30e76272011-02-22 16:10:53 -0300248 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 goto done;
250 }
251
252 /* Update source addr of the socket */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700253 bacpy(&sco_pi(sk)->src, &hcon->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 err = sco_chan_add(conn, sk, NULL);
256 if (err)
257 goto done;
258
259 if (hcon->state == BT_CONNECTED) {
260 sco_sock_clear_timer(sk);
261 sk->sk_state = BT_CONNECTED;
262 } else {
263 sk->sk_state = BT_CONNECT;
264 sco_sock_set_timer(sk, sk->sk_sndtimeo);
265 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300268 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 hci_dev_put(hdev);
270 return err;
271}
272
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300273static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct sco_conn *conn = sco_pi(sk)->conn;
276 struct sk_buff *skb;
Mikel Astiz088ce082012-04-11 08:48:48 +0200277 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* Check outgoing MTU */
280 if (len > conn->mtu)
281 return -EINVAL;
282
283 BT_DBG("sk %p len %d", sk, len);
284
Mikel Astiz088ce082012-04-11 08:48:48 +0200285 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300286 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return err;
288
Al Viro6ce8e9c2014-04-06 21:25:44 -0400289 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300290 kfree_skb(skb);
291 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -0300294 hci_send_sco(conn->hcon, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Mikel Astiz088ce082012-04-11 08:48:48 +0200296 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300299static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Marcel Holtmanneb5a4de2014-07-14 01:30:15 +0200301 struct sock *sk;
302
303 sco_conn_lock(conn);
304 sk = conn->sk;
305 sco_conn_unlock(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (!sk)
308 goto drop;
309
310 BT_DBG("sk %p len %d", sk, skb->len);
311
312 if (sk->sk_state != BT_CONNECTED)
313 goto drop;
314
315 if (!sock_queue_rcv_skb(sk, skb))
316 return;
317
318drop:
319 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322/* -------- Socket interface ---------- */
Marcel Holtmannfb334052012-04-19 14:37:58 +0200323static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Marcel Holtmannfb334052012-04-19 14:37:58 +0200325 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Sasha Levinb67bfe02013-02-27 17:06:00 -0800327 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmannfb334052012-04-19 14:37:58 +0200328 if (sk->sk_state != BT_LISTEN)
329 continue;
330
Marcel Holtmanneea96362013-10-13 10:34:01 -0700331 if (!bacmp(&sco_pi(sk)->src, ba))
Marcel Holtmannfb334052012-04-19 14:37:58 +0200332 return sk;
333 }
334
335 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338/* Find socket listening on source bdaddr.
339 * Returns closest match.
340 */
341static struct sock *sco_get_sock_listen(bdaddr_t *src)
342{
343 struct sock *sk = NULL, *sk1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 read_lock(&sco_sk_list.lock);
346
Sasha Levinb67bfe02013-02-27 17:06:00 -0800347 sk_for_each(sk, &sco_sk_list.head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (sk->sk_state != BT_LISTEN)
349 continue;
350
351 /* Exact match. */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700352 if (!bacmp(&sco_pi(sk)->src, src))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354
355 /* Closest match */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700356 if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 sk1 = sk;
358 }
359
360 read_unlock(&sco_sk_list.lock);
361
Sasha Levinb67bfe02013-02-27 17:06:00 -0800362 return sk ? sk : sk1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365static void sco_sock_destruct(struct sock *sk)
366{
367 BT_DBG("sk %p", sk);
368
369 skb_queue_purge(&sk->sk_receive_queue);
370 skb_queue_purge(&sk->sk_write_queue);
371}
372
373static void sco_sock_cleanup_listen(struct sock *parent)
374{
375 struct sock *sk;
376
377 BT_DBG("parent %p", parent);
378
379 /* Close not yet accepted channels */
380 while ((sk = bt_accept_dequeue(parent, NULL))) {
381 sco_sock_close(sk);
382 sco_sock_kill(sk);
383 }
384
385 parent->sk_state = BT_CLOSED;
386 sock_set_flag(parent, SOCK_ZAPPED);
387}
388
389/* Kill socket (only if zapped and orphan)
390 * Must be called on unlocked socket.
391 */
392static void sco_sock_kill(struct sock *sk)
393{
394 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
395 return;
396
397 BT_DBG("sk %p state %d", sk, sk->sk_state);
398
399 /* Kill poor orphan */
400 bt_sock_unlink(&sco_sk_list, sk);
401 sock_set_flag(sk, SOCK_DEAD);
402 sock_put(sk);
403}
404
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200405static void __sco_sock_close(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200407 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 switch (sk->sk_state) {
410 case BT_LISTEN:
411 sco_sock_cleanup_listen(sk);
412 break;
413
414 case BT_CONNECTED:
415 case BT_CONFIG:
Gustavo Padovanb7e98b52013-01-03 19:59:28 -0200416 if (sco_pi(sk)->conn->hcon) {
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300417 sk->sk_state = BT_DISCONN;
418 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
David Herrmann76a68ba2013-04-06 20:28:37 +0200419 hci_conn_drop(sco_pi(sk)->conn->hcon);
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300420 sco_pi(sk)->conn->hcon = NULL;
421 } else
422 sco_chan_del(sk, ECONNRESET);
423 break;
424
Vinicius Costa Gomeseb20ff92013-03-13 19:46:20 -0300425 case BT_CONNECT2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 case BT_CONNECT:
427 case BT_DISCONN:
428 sco_chan_del(sk, ECONNRESET);
429 break;
430
431 default:
432 sock_set_flag(sk, SOCK_ZAPPED);
433 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700434 }
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200435}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200437/* Must be called on unlocked socket. */
438static void sco_sock_close(struct sock *sk)
439{
440 sco_sock_clear_timer(sk);
441 lock_sock(sk);
442 __sco_sock_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 sco_sock_kill(sk);
445}
446
447static void sco_sock_init(struct sock *sk, struct sock *parent)
448{
449 BT_DBG("sk %p", sk);
450
Paul Moore6230c9b2011-10-07 09:40:59 +0000451 if (parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 sk->sk_type = parent->sk_type;
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +0100453 bt_sk(sk)->flags = bt_sk(parent)->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +0000454 security_sk_clone(parent, sk);
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458static struct proto sco_proto = {
459 .name = "SCO",
460 .owner = THIS_MODULE,
461 .obj_size = sizeof(struct sco_pinfo)
462};
463
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500464static 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 -0700465{
466 struct sock *sk;
467
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500468 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (!sk)
470 return NULL;
471
472 sock_init_data(sock, sk);
473 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
474
475 sk->sk_destruct = sco_sock_destruct;
476 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
477
478 sock_reset_flag(sk, SOCK_ZAPPED);
479
480 sk->sk_protocol = proto;
481 sk->sk_state = BT_OPEN;
482
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200483 sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
484
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800485 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 bt_sock_link(&sco_sk_list, sk);
488 return sk;
489}
490
Eric Paris3f378b62009-11-05 22:18:14 -0800491static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
492 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct sock *sk;
495
496 BT_DBG("sock %p", sock);
497
498 sock->state = SS_UNCONNECTED;
499
500 if (sock->type != SOCK_SEQPACKET)
501 return -ESOCKTNOSUPPORT;
502
503 sock->ops = &sco_sock_ops;
504
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500505 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200506 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return -ENOMEM;
508
509 sco_sock_init(sk, NULL);
510 return 0;
511}
512
513static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
514{
515 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
516 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int err = 0;
518
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300519 BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 if (!addr || addr->sa_family != AF_BLUETOOTH)
522 return -EINVAL;
523
524 lock_sock(sk);
525
526 if (sk->sk_state != BT_OPEN) {
527 err = -EBADFD;
528 goto done;
529 }
530
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200531 if (sk->sk_type != SOCK_SEQPACKET) {
532 err = -EINVAL;
533 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
Marcel Holtmanneea96362013-10-13 10:34:01 -0700536 bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200537
538 sk->sk_state = BT_BOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540done:
541 release_sock(sk);
542 return err;
543}
544
545static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
546{
547 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
548 struct sock *sk = sock->sk;
Claudio Takahasi92f185c2013-04-11 11:35:46 -0300549 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 BT_DBG("sk %p", sk);
552
Changli Gao6503d962010-03-31 22:58:26 +0000553 if (alen < sizeof(struct sockaddr_sco) ||
554 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return -EINVAL;
556
557 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
558 return -EBADFD;
559
560 if (sk->sk_type != SOCK_SEQPACKET)
561 return -EINVAL;
562
563 lock_sock(sk);
564
565 /* Set destination address and psm */
Marcel Holtmanneea96362013-10-13 10:34:01 -0700566 bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200568 err = sco_connect(sk);
569 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto done;
571
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900572 err = bt_sock_wait_state(sk, BT_CONNECTED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300573 sock_sndtimeo(sk, flags & O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575done:
576 release_sock(sk);
577 return err;
578}
579
580static int sco_sock_listen(struct socket *sock, int backlog)
581{
582 struct sock *sk = sock->sk;
Marcel Holtmanneea96362013-10-13 10:34:01 -0700583 bdaddr_t *src = &sco_pi(sk)->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int err = 0;
585
586 BT_DBG("sk %p backlog %d", sk, backlog);
587
588 lock_sock(sk);
589
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200590 if (sk->sk_state != BT_BOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 err = -EBADFD;
592 goto done;
593 }
594
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200595 if (sk->sk_type != SOCK_SEQPACKET) {
596 err = -EINVAL;
597 goto done;
598 }
599
Marcel Holtmannfb334052012-04-19 14:37:58 +0200600 write_lock(&sco_sk_list.lock);
601
602 if (__sco_get_sock_listen_by_addr(src)) {
603 err = -EADDRINUSE;
604 goto unlock;
605 }
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 sk->sk_max_ack_backlog = backlog;
608 sk->sk_ack_backlog = 0;
Marcel Holtmannfb334052012-04-19 14:37:58 +0200609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 sk->sk_state = BT_LISTEN;
611
Marcel Holtmannfb334052012-04-19 14:37:58 +0200612unlock:
613 write_unlock(&sco_sk_list.lock);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615done:
616 release_sock(sk);
617 return err;
618}
619
620static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
621{
Peter Hurleydfb2fae2015-01-23 12:16:53 -0500622 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct sock *sk = sock->sk, *ch;
624 long timeo;
625 int err = 0;
626
627 lock_sock(sk);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
630
631 BT_DBG("sk %p timeo %ld", sk, timeo);
632
633 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000634 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurley552b0d32011-07-24 00:11:01 -0400635 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (sk->sk_state != BT_LISTEN) {
637 err = -EBADFD;
638 break;
639 }
640
Peter Hurley552b0d32011-07-24 00:11:01 -0400641 ch = bt_accept_dequeue(sk, newsock);
642 if (ch)
643 break;
644
645 if (!timeo) {
646 err = -EAGAIN;
647 break;
648 }
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (signal_pending(current)) {
651 err = sock_intr_errno(timeo);
652 break;
653 }
Peter Hurley552b0d32011-07-24 00:11:01 -0400654
655 release_sock(sk);
Peter Hurleydfb2fae2015-01-23 12:16:53 -0500656
657 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
Peter Hurley552b0d32011-07-24 00:11:01 -0400658 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000660 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (err)
663 goto done;
664
665 newsock->state = SS_CONNECTED;
666
667 BT_DBG("new socket %p", ch);
668
669done:
670 release_sock(sk);
671 return err;
672}
673
674static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
675{
676 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
677 struct sock *sk = sock->sk;
678
679 BT_DBG("sock %p, sk %p", sock, sk);
680
681 addr->sa_family = AF_BLUETOOTH;
682 *len = sizeof(struct sockaddr_sco);
683
684 if (peer)
Marcel Holtmanneea96362013-10-13 10:34:01 -0700685 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 else
Marcel Holtmanneea96362013-10-13 10:34:01 -0700687 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 return 0;
690}
691
Ying Xue1b784142015-03-02 15:37:48 +0800692static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
693 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 struct sock *sk = sock->sk;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300696 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 BT_DBG("sock %p, sk %p", sock, sk);
699
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800700 err = sock_error(sk);
701 if (err)
702 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (msg->msg_flags & MSG_OOB)
705 return -EOPNOTSUPP;
706
707 lock_sock(sk);
708
709 if (sk->sk_state == BT_CONNECTED)
710 err = sco_send_frame(sk, msg, len);
711 else
712 err = -ENOTCONN;
713
714 release_sock(sk);
715 return err;
716}
717
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200718static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200719{
720 struct hci_dev *hdev = conn->hdev;
721
722 BT_DBG("conn %p", conn);
723
724 conn->state = BT_CONFIG;
725
726 if (!lmp_esco_capable(hdev)) {
727 struct hci_cp_accept_conn_req cp;
728
729 bacpy(&cp.bdaddr, &conn->dst);
Frédéric Dalleau33f24042013-08-19 14:23:55 +0200730 cp.role = 0x00; /* Ignored */
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200731
732 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
733 } else {
734 struct hci_cp_accept_sync_conn_req cp;
735
736 bacpy(&cp.bdaddr, &conn->dst);
737 cp.pkt_type = cpu_to_le16(conn->pkt_type);
738
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700739 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
740 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200741 cp.content_format = cpu_to_le16(setting);
742
743 switch (setting & SCO_AIRMODE_MASK) {
744 case SCO_AIRMODE_TRANSP:
745 if (conn->pkt_type & ESCO_2EV3)
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700746 cp.max_latency = cpu_to_le16(0x0008);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200747 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700748 cp.max_latency = cpu_to_le16(0x000D);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200749 cp.retrans_effort = 0x02;
750 break;
751 case SCO_AIRMODE_CVSD:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700752 cp.max_latency = cpu_to_le16(0xffff);
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200753 cp.retrans_effort = 0xff;
754 break;
755 }
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200756
757 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
758 sizeof(cp), &cp);
759 }
760}
761
Ying Xue1b784142015-03-02 15:37:48 +0800762static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
763 size_t len, int flags)
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +0100764{
765 struct sock *sk = sock->sk;
766 struct sco_pinfo *pi = sco_pi(sk);
767
768 lock_sock(sk);
769
770 if (sk->sk_state == BT_CONNECT2 &&
771 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
Frédéric Dalleau2f69a822013-08-19 14:23:58 +0200772 sco_conn_defer_accept(pi->conn->hcon, pi->setting);
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +0100773 sk->sk_state = BT_CONFIG;
774
775 release_sock(sk);
776 return 0;
777 }
778
779 release_sock(sk);
780
Ying Xue1b784142015-03-02 15:37:48 +0800781 return bt_sock_recvmsg(sock, msg, len, flags);
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +0100782}
783
David S. Millerb7058842009-09-30 16:12:20 -0700784static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct sock *sk = sock->sk;
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200787 int len, err = 0;
788 struct bt_voice voice;
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100789 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 BT_DBG("sk %p", sk);
792
793 lock_sock(sk);
794
795 switch (optname) {
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100796
797 case BT_DEFER_SETUP:
798 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
799 err = -EINVAL;
800 break;
801 }
802
803 if (get_user(opt, (u32 __user *) optval)) {
804 err = -EFAULT;
805 break;
806 }
807
808 if (opt)
809 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
810 else
811 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
812 break;
813
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200814 case BT_VOICE:
815 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
816 sk->sk_state != BT_CONNECT2) {
817 err = -EINVAL;
818 break;
819 }
820
821 voice.setting = sco_pi(sk)->setting;
822
823 len = min_t(unsigned int, sizeof(voice), optlen);
824 if (copy_from_user((char *) &voice, optval, len)) {
825 err = -EFAULT;
826 break;
827 }
828
829 /* Explicitly check for these values */
830 if (voice.setting != BT_VOICE_TRANSPARENT &&
831 voice.setting != BT_VOICE_CVSD_16BIT) {
832 err = -EINVAL;
833 break;
834 }
835
836 sco_pi(sk)->setting = voice.setting;
837 break;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 default:
840 err = -ENOPROTOOPT;
841 break;
842 }
843
844 release_sock(sk);
845 return err;
846}
847
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100848static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct sock *sk = sock->sk;
851 struct sco_options opts;
852 struct sco_conninfo cinfo;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900853 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 BT_DBG("sk %p", sk);
856
857 if (get_user(len, optlen))
858 return -EFAULT;
859
860 lock_sock(sk);
861
862 switch (optname) {
863 case SCO_OPTIONS:
Johan Hedberg9d225d22013-08-08 14:53:56 +0300864 if (sk->sk_state != BT_CONNECTED &&
865 !(sk->sk_state == BT_CONNECT2 &&
866 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 err = -ENOTCONN;
868 break;
869 }
870
871 opts.mtu = sco_pi(sk)->conn->mtu;
872
873 BT_DBG("mtu %d", opts.mtu);
874
875 len = min_t(unsigned int, len, sizeof(opts));
876 if (copy_to_user(optval, (char *)&opts, len))
877 err = -EFAULT;
878
879 break;
880
881 case SCO_CONNINFO:
Johan Hedberg9d225d22013-08-08 14:53:56 +0300882 if (sk->sk_state != BT_CONNECTED &&
883 !(sk->sk_state == BT_CONNECT2 &&
884 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 err = -ENOTCONN;
886 break;
887 }
888
Vasiliy Kulikovc4c896e2011-02-14 13:54:26 +0300889 memset(&cinfo, 0, sizeof(cinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
891 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
892
893 len = min_t(unsigned int, len, sizeof(cinfo));
894 if (copy_to_user(optval, (char *)&cinfo, len))
895 err = -EFAULT;
896
897 break;
898
899 default:
900 err = -ENOPROTOOPT;
901 break;
902 }
903
904 release_sock(sk);
905 return err;
906}
907
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100908static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
909{
910 struct sock *sk = sock->sk;
911 int len, err = 0;
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200912 struct bt_voice voice;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100913
914 BT_DBG("sk %p", sk);
915
916 if (level == SOL_SCO)
917 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
918
919 if (get_user(len, optlen))
920 return -EFAULT;
921
922 lock_sock(sk);
923
924 switch (optname) {
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100925
926 case BT_DEFER_SETUP:
927 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
928 err = -EINVAL;
929 break;
930 }
931
932 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
933 (u32 __user *) optval))
934 err = -EFAULT;
935
936 break;
937
Frédéric Dalleauad10b1a2013-08-19 14:23:56 +0200938 case BT_VOICE:
939 voice.setting = sco_pi(sk)->setting;
940
941 len = min_t(unsigned int, len, sizeof(voice));
942 if (copy_to_user(optval, (char *)&voice, len))
943 err = -EFAULT;
944
945 break;
946
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100947 default:
948 err = -ENOPROTOOPT;
949 break;
950 }
951
952 release_sock(sk);
953 return err;
954}
955
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200956static int sco_sock_shutdown(struct socket *sock, int how)
957{
958 struct sock *sk = sock->sk;
959 int err = 0;
960
961 BT_DBG("sock %p, sk %p", sock, sk);
962
963 if (!sk)
964 return 0;
965
966 lock_sock(sk);
967 if (!sk->sk_shutdown) {
968 sk->sk_shutdown = SHUTDOWN_MASK;
969 sco_sock_clear_timer(sk);
970 __sco_sock_close(sk);
971
Vladimir Davydov093facf2014-07-15 12:25:28 +0400972 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
973 !(current->flags & PF_EXITING))
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200974 err = bt_sock_wait_state(sk, BT_CLOSED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300975 sk->sk_lingertime);
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200976 }
977 release_sock(sk);
978 return err;
979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981static int sco_sock_release(struct socket *sock)
982{
983 struct sock *sk = sock->sk;
984 int err = 0;
985
986 BT_DBG("sock %p, sk %p", sock, sk);
987
988 if (!sk)
989 return 0;
990
991 sco_sock_close(sk);
992
Vladimir Davydov093facf2014-07-15 12:25:28 +0400993 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
994 !(current->flags & PF_EXITING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 lock_sock(sk);
996 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
997 release_sock(sk);
998 }
999
1000 sock_orphan(sk);
1001 sco_sock_kill(sk);
1002 return err;
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005static void sco_conn_ready(struct sco_conn *conn)
1006{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +02001007 struct sock *parent;
1008 struct sock *sk = conn->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 BT_DBG("conn %p", conn);
1011
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +02001012 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 sco_sock_clear_timer(sk);
1014 bh_lock_sock(sk);
1015 sk->sk_state = BT_CONNECTED;
1016 sk->sk_state_change(sk);
1017 bh_unlock_sock(sk);
1018 } else {
Andre Guedes40528082013-01-29 19:59:56 -03001019 sco_conn_lock(conn);
1020
Marcel Holtmann041987c2013-10-13 10:15:22 -07001021 parent = sco_get_sock_listen(&conn->hcon->src);
Andre Guedes40528082013-01-29 19:59:56 -03001022 if (!parent) {
1023 sco_conn_unlock(conn);
1024 return;
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 bh_lock_sock(parent);
1028
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -03001029 sk = sco_sock_alloc(sock_net(parent), NULL,
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001030 BTPROTO_SCO, GFP_ATOMIC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (!sk) {
1032 bh_unlock_sock(parent);
Andre Guedes40528082013-01-29 19:59:56 -03001033 sco_conn_unlock(conn);
1034 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
1037 sco_sock_init(sk, parent);
1038
Marcel Holtmanneea96362013-10-13 10:34:01 -07001039 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1040 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 hci_conn_hold(conn->hcon);
1043 __sco_chan_add(conn, sk, parent);
1044
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01001045 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1046 sk->sk_state = BT_CONNECT2;
1047 else
1048 sk->sk_state = BT_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /* Wake up parent */
David S. Miller676d2362014-04-11 16:15:36 -04001051 parent->sk_data_ready(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 bh_unlock_sock(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Andre Guedes40528082013-01-29 19:59:56 -03001055 sco_conn_unlock(conn);
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059/* ----- SCO interface with lower layer (HCI) ----- */
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01001060int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Gustavo Padovanfc5fef62012-05-23 04:04:19 -03001062 struct sock *sk;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001063 int lm = 0;
1064
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001065 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001067 /* Find listening sockets */
1068 read_lock(&sco_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001069 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001070 if (sk->sk_state != BT_LISTEN)
1071 continue;
1072
Marcel Holtmanneea96362013-10-13 10:34:01 -07001073 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1074 !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001075 lm |= HCI_LM_ACCEPT;
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01001076
1077 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1078 *flags |= HCI_PROTO_DEFER;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001079 break;
1080 }
1081 }
1082 read_unlock(&sco_sk_list.lock);
1083
1084 return lm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
Johan Hedberg539c4962015-02-18 14:53:57 +02001087static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Johan Hedberg539c4962015-02-18 14:53:57 +02001089 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1090 return;
1091
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001092 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Johan Hedberg539c4962015-02-18 14:53:57 +02001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!status) {
1095 struct sco_conn *conn;
1096
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +02001097 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (conn)
1099 sco_conn_ready(conn);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001100 } else
Joe Perchese1750722011-06-29 18:18:29 -07001101 sco_conn_del(hcon, bt_to_errno(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
Johan Hedberg3a6d5762015-02-18 14:53:58 +02001104static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Johan Hedberg3a6d5762015-02-18 14:53:58 +02001106 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1107 return;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 BT_DBG("hcon %p reason %d", hcon, reason);
1110
Joe Perchese1750722011-06-29 18:18:29 -07001111 sco_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
Arron Wang9b4c3332015-06-09 17:47:22 +08001114void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 struct sco_conn *conn = hcon->sco_data;
1117
1118 if (!conn)
1119 goto drop;
1120
1121 BT_DBG("conn %p len %d", conn, skb->len);
1122
1123 if (skb->len) {
1124 sco_recv_frame(conn, skb);
Arron Wang9b4c3332015-06-09 17:47:22 +08001125 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128drop:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001129 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Johan Hedberg539c4962015-02-18 14:53:57 +02001132static struct hci_cb sco_cb = {
1133 .name = "SCO",
1134 .connect_cfm = sco_connect_cfm,
Johan Hedberg3a6d5762015-02-18 14:53:58 +02001135 .disconn_cfm = sco_disconn_cfm,
Johan Hedberg539c4962015-02-18 14:53:57 +02001136};
1137
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001138static int sco_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Gustavo F. Padovanee65d192011-12-27 15:28:46 -02001142 read_lock(&sco_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Sasha Levinb67bfe02013-02-27 17:06:00 -08001144 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmanneea96362013-10-13 10:34:01 -07001145 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1146 &sco_pi(sk)->dst, sk->sk_state);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Gustavo F. Padovanee65d192011-12-27 15:28:46 -02001149 read_unlock(&sco_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001150
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001154static int sco_debugfs_open(struct inode *inode, struct file *file)
1155{
1156 return single_open(file, sco_debugfs_show, inode->i_private);
1157}
1158
1159static const struct file_operations sco_debugfs_fops = {
1160 .open = sco_debugfs_open,
1161 .read = seq_read,
1162 .llseek = seq_lseek,
1163 .release = single_release,
1164};
1165
1166static struct dentry *sco_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001168static const struct proto_ops sco_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 .family = PF_BLUETOOTH,
1170 .owner = THIS_MODULE,
1171 .release = sco_sock_release,
1172 .bind = sco_sock_bind,
1173 .connect = sco_sock_connect,
1174 .listen = sco_sock_listen,
1175 .accept = sco_sock_accept,
1176 .getname = sco_sock_getname,
1177 .sendmsg = sco_sock_sendmsg,
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01001178 .recvmsg = sco_sock_recvmsg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 .poll = bt_sock_poll,
Marcel Holtmann3241ad82008-07-14 20:13:50 +02001180 .ioctl = bt_sock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 .mmap = sock_no_mmap,
1182 .socketpair = sock_no_socketpair,
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +02001183 .shutdown = sco_sock_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 .setsockopt = sco_sock_setsockopt,
1185 .getsockopt = sco_sock_getsockopt
1186};
1187
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001188static const struct net_proto_family sco_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 .family = PF_BLUETOOTH,
1190 .owner = THIS_MODULE,
1191 .create = sco_sock_create,
1192};
1193
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001194int __init sco_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 int err;
1197
Marcel Holtmann15762fa2015-01-11 15:21:06 -08001198 BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr));
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 err = proto_register(&sco_proto, 0);
1201 if (err < 0)
1202 return err;
1203
1204 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1205 if (err < 0) {
1206 BT_ERR("SCO socket registration failed");
1207 goto error;
1208 }
1209
Al Virob0316612013-04-04 19:14:33 -04001210 err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
Masatake YAMATOde9b9212012-07-26 01:30:12 +09001211 if (err < 0) {
1212 BT_ERR("Failed to create SCO proc file");
1213 bt_sock_unregister(BTPROTO_SCO);
1214 goto error;
1215 }
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 BT_INFO("SCO socket layer initialized");
1218
Johan Hedberg539c4962015-02-18 14:53:57 +02001219 hci_register_cb(&sco_cb);
1220
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07001221 if (IS_ERR_OR_NULL(bt_debugfs))
1222 return 0;
1223
1224 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1225 NULL, &sco_debugfs_fops);
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return 0;
1228
1229error:
1230 proto_unregister(&sco_proto);
1231 return err;
1232}
1233
Alexander Aring0402d9f22015-03-07 20:52:28 +01001234void sco_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Masatake YAMATOde9b9212012-07-26 01:30:12 +09001236 bt_procfs_cleanup(&init_net, "sco");
1237
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001238 debugfs_remove(sco_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Johan Hedberg539c4962015-02-18 14:53:57 +02001240 hci_unregister_cb(&sco_cb);
1241
David Herrmann5e9d7f82013-02-24 19:36:51 +01001242 bt_sock_unregister(BTPROTO_SCO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 proto_unregister(&sco_proto);
1245}
1246
Marcel Holtmann7cb127d2008-07-14 20:13:53 +02001247module_param(disable_esco, bool, 0644);
1248MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");