blob: 4c3621b5e0aa2344196767bdeb6be407918732b4 [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>
28
29#include <linux/types.h>
30#include <linux/errno.h>
31#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/sched.h>
33#include <linux/slab.h>
34#include <linux/poll.h>
35#include <linux/fcntl.h>
36#include <linux/init.h>
37#include <linux/interrupt.h>
38#include <linux/socket.h>
39#include <linux/skbuff.h>
Marcel Holtmannbe9d1222005-11-08 09:57:38 -080040#include <linux/device.h>
Marcel Holtmannaef7d972010-03-21 05:27:45 +010041#include <linux/debugfs.h>
42#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/list.h>
44#include <net/sock.h>
45
46#include <asm/system.h>
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +020047#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <net/bluetooth/bluetooth.h>
50#include <net/bluetooth/hci_core.h>
51#include <net/bluetooth/sco.h>
52
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +020053static int disable_esco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080055static const struct proto_ops sco_sock_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static struct bt_sock_list sco_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070058 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
61static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
62static void sco_chan_del(struct sock *sk, int err);
63
64static int sco_conn_del(struct hci_conn *conn, int err);
65
66static void sco_sock_close(struct sock *sk);
67static void sco_sock_kill(struct sock *sk);
68
69/* ---- SCO timers ---- */
70static void sco_sock_timeout(unsigned long arg)
71{
72 struct sock *sk = (struct sock *) arg;
73
74 BT_DBG("sock %p state %d", sk, sk->sk_state);
75
76 bh_lock_sock(sk);
77 sk->sk_err = ETIMEDOUT;
78 sk->sk_state_change(sk);
79 bh_unlock_sock(sk);
80
81 sco_sock_kill(sk);
82 sock_put(sk);
83}
84
85static void sco_sock_set_timer(struct sock *sk, long timeout)
86{
87 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
88 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
89}
90
91static void sco_sock_clear_timer(struct sock *sk)
92{
93 BT_DBG("sock %p state %d", sk, sk->sk_state);
94 sk_stop_timer(sk, &sk->sk_timer);
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* ---- SCO connections ---- */
98static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
99{
100 struct hci_dev *hdev = hcon->hdev;
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200101 struct sco_conn *conn = hcon->sco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200103 if (conn || status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return conn;
105
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200106 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
107 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 spin_lock_init(&conn->lock);
111
112 hcon->sco_data = conn;
113 conn->hcon = hcon;
114
115 conn->src = &hdev->bdaddr;
116 conn->dst = &hcon->dst;
117
118 if (hdev->sco_mtu > 0)
119 conn->mtu = hdev->sco_mtu;
120 else
121 conn->mtu = 60;
122
123 BT_DBG("hcon %p conn %p", hcon, conn);
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return conn;
126}
127
128static inline struct sock *sco_chan_get(struct sco_conn *conn)
129{
130 struct sock *sk = NULL;
131 sco_conn_lock(conn);
132 sk = conn->sk;
133 sco_conn_unlock(conn);
134 return sk;
135}
136
137static int sco_conn_del(struct hci_conn *hcon, int err)
138{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200139 struct sco_conn *conn = hcon->sco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct sock *sk;
141
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200142 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return 0;
144
145 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
146
147 /* Kill socket */
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200148 sk = sco_chan_get(conn);
149 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 bh_lock_sock(sk);
151 sco_sock_clear_timer(sk);
152 sco_chan_del(sk, err);
153 bh_unlock_sock(sk);
154 sco_sock_kill(sk);
155 }
156
157 hcon->sco_data = NULL;
158 kfree(conn);
159 return 0;
160}
161
162static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
163{
164 int err = 0;
165
166 sco_conn_lock(conn);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300167 if (conn->sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 err = -EBUSY;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300169 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 __sco_chan_add(conn, sk, parent);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 sco_conn_unlock(conn);
173 return err;
174}
175
176static int sco_connect(struct sock *sk)
177{
178 bdaddr_t *src = &bt_sk(sk)->src;
179 bdaddr_t *dst = &bt_sk(sk)->dst;
180 struct sco_conn *conn;
181 struct hci_conn *hcon;
182 struct hci_dev *hdev;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200183 int err, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 BT_DBG("%s -> %s", batostr(src), batostr(dst));
186
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200187 hdev = hci_get_route(dst, src);
188 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -EHOSTUNREACH;
190
191 hci_dev_lock_bh(hdev);
192
Marcel Holtmann7cb127d2008-07-14 20:13:53 +0200193 if (lmp_esco_capable(hdev) && !disable_esco)
194 type = ESCO_LINK;
195 else
196 type = SCO_LINK;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200197
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100198 hcon = hci_connect(hdev, type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
Ville Tervo30e76272011-02-22 16:10:53 -0300199 if (IS_ERR(hcon)) {
200 err = PTR_ERR(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto done;
Ville Tervo30e76272011-02-22 16:10:53 -0300202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 conn = sco_conn_add(hcon, 0);
205 if (!conn) {
206 hci_conn_put(hcon);
Ville Tervo30e76272011-02-22 16:10:53 -0300207 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 goto done;
209 }
210
211 /* Update source addr of the socket */
212 bacpy(src, conn->src);
213
214 err = sco_chan_add(conn, sk, NULL);
215 if (err)
216 goto done;
217
218 if (hcon->state == BT_CONNECTED) {
219 sco_sock_clear_timer(sk);
220 sk->sk_state = BT_CONNECTED;
221 } else {
222 sk->sk_state = BT_CONNECT;
223 sco_sock_set_timer(sk, sk->sk_sndtimeo);
224 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226done:
227 hci_dev_unlock_bh(hdev);
228 hci_dev_put(hdev);
229 return err;
230}
231
232static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
233{
234 struct sco_conn *conn = sco_pi(sk)->conn;
235 struct sk_buff *skb;
236 int err, count;
237
238 /* Check outgoing MTU */
239 if (len > conn->mtu)
240 return -EINVAL;
241
242 BT_DBG("sk %p len %d", sk, len);
243
244 count = min_t(unsigned int, conn->mtu, len);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300245 skb = bt_skb_send_alloc(sk, count,
246 msg->msg_flags & MSG_DONTWAIT, &err);
247 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return err;
249
250 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300251 kfree_skb(skb);
252 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -0300255 hci_send_sco(conn->hcon, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
261{
262 struct sock *sk = sco_chan_get(conn);
263
264 if (!sk)
265 goto drop;
266
267 BT_DBG("sk %p len %d", sk, skb->len);
268
269 if (sk->sk_state != BT_CONNECTED)
270 goto drop;
271
272 if (!sock_queue_rcv_skb(sk, skb))
273 return;
274
275drop:
276 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/* -------- Socket interface ---------- */
280static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
281{
282 struct sock *sk;
283 struct hlist_node *node;
284
285 sk_for_each(sk, node, &sco_sk_list.head)
286 if (!bacmp(&bt_sk(sk)->src, ba))
287 goto found;
288 sk = NULL;
289found:
290 return sk;
291}
292
293/* Find socket listening on source bdaddr.
294 * Returns closest match.
295 */
296static struct sock *sco_get_sock_listen(bdaddr_t *src)
297{
298 struct sock *sk = NULL, *sk1 = NULL;
299 struct hlist_node *node;
300
301 read_lock(&sco_sk_list.lock);
302
303 sk_for_each(sk, node, &sco_sk_list.head) {
304 if (sk->sk_state != BT_LISTEN)
305 continue;
306
307 /* Exact match. */
308 if (!bacmp(&bt_sk(sk)->src, src))
309 break;
310
311 /* Closest match */
312 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
313 sk1 = sk;
314 }
315
316 read_unlock(&sco_sk_list.lock);
317
318 return node ? sk : sk1;
319}
320
321static void sco_sock_destruct(struct sock *sk)
322{
323 BT_DBG("sk %p", sk);
324
325 skb_queue_purge(&sk->sk_receive_queue);
326 skb_queue_purge(&sk->sk_write_queue);
327}
328
329static void sco_sock_cleanup_listen(struct sock *parent)
330{
331 struct sock *sk;
332
333 BT_DBG("parent %p", parent);
334
335 /* Close not yet accepted channels */
336 while ((sk = bt_accept_dequeue(parent, NULL))) {
337 sco_sock_close(sk);
338 sco_sock_kill(sk);
339 }
340
341 parent->sk_state = BT_CLOSED;
342 sock_set_flag(parent, SOCK_ZAPPED);
343}
344
345/* Kill socket (only if zapped and orphan)
346 * Must be called on unlocked socket.
347 */
348static void sco_sock_kill(struct sock *sk)
349{
350 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
351 return;
352
353 BT_DBG("sk %p state %d", sk, sk->sk_state);
354
355 /* Kill poor orphan */
356 bt_sock_unlink(&sco_sk_list, sk);
357 sock_set_flag(sk, SOCK_DEAD);
358 sock_put(sk);
359}
360
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200361static void __sco_sock_close(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200363 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 switch (sk->sk_state) {
366 case BT_LISTEN:
367 sco_sock_cleanup_listen(sk);
368 break;
369
370 case BT_CONNECTED:
371 case BT_CONFIG:
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300372 if (sco_pi(sk)->conn) {
373 sk->sk_state = BT_DISCONN;
374 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
375 hci_conn_put(sco_pi(sk)->conn->hcon);
376 sco_pi(sk)->conn->hcon = NULL;
377 } else
378 sco_chan_del(sk, ECONNRESET);
379 break;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 case BT_CONNECT:
382 case BT_DISCONN:
383 sco_chan_del(sk, ECONNRESET);
384 break;
385
386 default:
387 sock_set_flag(sk, SOCK_ZAPPED);
388 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700389 }
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200390}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200392/* Must be called on unlocked socket. */
393static void sco_sock_close(struct sock *sk)
394{
395 sco_sock_clear_timer(sk);
396 lock_sock(sk);
397 __sco_sock_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 sco_sock_kill(sk);
400}
401
402static void sco_sock_init(struct sock *sk, struct sock *parent)
403{
404 BT_DBG("sk %p", sk);
405
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900406 if (parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 sk->sk_type = parent->sk_type;
408}
409
410static struct proto sco_proto = {
411 .name = "SCO",
412 .owner = THIS_MODULE,
413 .obj_size = sizeof(struct sco_pinfo)
414};
415
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700416static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct sock *sk;
419
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700420 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!sk)
422 return NULL;
423
424 sock_init_data(sock, sk);
425 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
426
427 sk->sk_destruct = sco_sock_destruct;
428 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
429
430 sock_reset_flag(sk, SOCK_ZAPPED);
431
432 sk->sk_protocol = proto;
433 sk->sk_state = BT_OPEN;
434
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800435 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 bt_sock_link(&sco_sk_list, sk);
438 return sk;
439}
440
Eric Paris3f378b62009-11-05 22:18:14 -0800441static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
442 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct sock *sk;
445
446 BT_DBG("sock %p", sock);
447
448 sock->state = SS_UNCONNECTED;
449
450 if (sock->type != SOCK_SEQPACKET)
451 return -ESOCKTNOSUPPORT;
452
453 sock->ops = &sco_sock_ops;
454
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700455 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200456 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return -ENOMEM;
458
459 sco_sock_init(sk, NULL);
460 return 0;
461}
462
463static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
464{
465 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
466 struct sock *sk = sock->sk;
467 bdaddr_t *src = &sa->sco_bdaddr;
468 int err = 0;
469
470 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
471
472 if (!addr || addr->sa_family != AF_BLUETOOTH)
473 return -EINVAL;
474
475 lock_sock(sk);
476
477 if (sk->sk_state != BT_OPEN) {
478 err = -EBADFD;
479 goto done;
480 }
481
482 write_lock_bh(&sco_sk_list.lock);
483
484 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
485 err = -EADDRINUSE;
486 } else {
487 /* Save source address */
488 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
489 sk->sk_state = BT_BOUND;
490 }
491
492 write_unlock_bh(&sco_sk_list.lock);
493
494done:
495 release_sock(sk);
496 return err;
497}
498
499static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
500{
501 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
502 struct sock *sk = sock->sk;
503 int err = 0;
504
505
506 BT_DBG("sk %p", sk);
507
Changli Gao6503d962010-03-31 22:58:26 +0000508 if (alen < sizeof(struct sockaddr_sco) ||
509 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return -EINVAL;
511
512 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
513 return -EBADFD;
514
515 if (sk->sk_type != SOCK_SEQPACKET)
516 return -EINVAL;
517
518 lock_sock(sk);
519
520 /* Set destination address and psm */
521 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
522
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200523 err = sco_connect(sk);
524 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto done;
526
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900527 err = bt_sock_wait_state(sk, BT_CONNECTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 sock_sndtimeo(sk, flags & O_NONBLOCK));
529
530done:
531 release_sock(sk);
532 return err;
533}
534
535static int sco_sock_listen(struct socket *sock, int backlog)
536{
537 struct sock *sk = sock->sk;
538 int err = 0;
539
540 BT_DBG("sk %p backlog %d", sk, backlog);
541
542 lock_sock(sk);
543
544 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
545 err = -EBADFD;
546 goto done;
547 }
548
549 sk->sk_max_ack_backlog = backlog;
550 sk->sk_ack_backlog = 0;
551 sk->sk_state = BT_LISTEN;
552
553done:
554 release_sock(sk);
555 return err;
556}
557
558static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
559{
560 DECLARE_WAITQUEUE(wait, current);
561 struct sock *sk = sock->sk, *ch;
562 long timeo;
563 int err = 0;
564
565 lock_sock(sk);
566
567 if (sk->sk_state != BT_LISTEN) {
568 err = -EBADFD;
569 goto done;
570 }
571
572 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
573
574 BT_DBG("sk %p timeo %ld", sk, timeo);
575
576 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000577 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 while (!(ch = bt_accept_dequeue(sk, newsock))) {
579 set_current_state(TASK_INTERRUPTIBLE);
580 if (!timeo) {
581 err = -EAGAIN;
582 break;
583 }
584
585 release_sock(sk);
586 timeo = schedule_timeout(timeo);
587 lock_sock(sk);
588
589 if (sk->sk_state != BT_LISTEN) {
590 err = -EBADFD;
591 break;
592 }
593
594 if (signal_pending(current)) {
595 err = sock_intr_errno(timeo);
596 break;
597 }
598 }
599 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000600 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 if (err)
603 goto done;
604
605 newsock->state = SS_CONNECTED;
606
607 BT_DBG("new socket %p", ch);
608
609done:
610 release_sock(sk);
611 return err;
612}
613
614static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
615{
616 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
617 struct sock *sk = sock->sk;
618
619 BT_DBG("sock %p, sk %p", sock, sk);
620
621 addr->sa_family = AF_BLUETOOTH;
622 *len = sizeof(struct sockaddr_sco);
623
624 if (peer)
625 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
626 else
627 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
628
629 return 0;
630}
631
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900632static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct msghdr *msg, size_t len)
634{
635 struct sock *sk = sock->sk;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300636 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 BT_DBG("sock %p, sk %p", sock, sk);
639
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800640 err = sock_error(sk);
641 if (err)
642 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (msg->msg_flags & MSG_OOB)
645 return -EOPNOTSUPP;
646
647 lock_sock(sk);
648
649 if (sk->sk_state == BT_CONNECTED)
650 err = sco_send_frame(sk, msg, len);
651 else
652 err = -ENOTCONN;
653
654 release_sock(sk);
655 return err;
656}
657
David S. Millerb7058842009-09-30 16:12:20 -0700658static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct sock *sk = sock->sk;
661 int err = 0;
662
663 BT_DBG("sk %p", sk);
664
665 lock_sock(sk);
666
667 switch (optname) {
668 default:
669 err = -ENOPROTOOPT;
670 break;
671 }
672
673 release_sock(sk);
674 return err;
675}
676
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100677static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct sock *sk = sock->sk;
680 struct sco_options opts;
681 struct sco_conninfo cinfo;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900682 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 BT_DBG("sk %p", sk);
685
686 if (get_user(len, optlen))
687 return -EFAULT;
688
689 lock_sock(sk);
690
691 switch (optname) {
692 case SCO_OPTIONS:
693 if (sk->sk_state != BT_CONNECTED) {
694 err = -ENOTCONN;
695 break;
696 }
697
698 opts.mtu = sco_pi(sk)->conn->mtu;
699
700 BT_DBG("mtu %d", opts.mtu);
701
702 len = min_t(unsigned int, len, sizeof(opts));
703 if (copy_to_user(optval, (char *)&opts, len))
704 err = -EFAULT;
705
706 break;
707
708 case SCO_CONNINFO:
709 if (sk->sk_state != BT_CONNECTED) {
710 err = -ENOTCONN;
711 break;
712 }
713
Vasiliy Kulikovc4c896e2011-02-14 13:54:26 +0300714 memset(&cinfo, 0, sizeof(cinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
716 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
717
718 len = min_t(unsigned int, len, sizeof(cinfo));
719 if (copy_to_user(optval, (char *)&cinfo, len))
720 err = -EFAULT;
721
722 break;
723
724 default:
725 err = -ENOPROTOOPT;
726 break;
727 }
728
729 release_sock(sk);
730 return err;
731}
732
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100733static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
734{
735 struct sock *sk = sock->sk;
736 int len, err = 0;
737
738 BT_DBG("sk %p", sk);
739
740 if (level == SOL_SCO)
741 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
742
743 if (get_user(len, optlen))
744 return -EFAULT;
745
746 lock_sock(sk);
747
748 switch (optname) {
749 default:
750 err = -ENOPROTOOPT;
751 break;
752 }
753
754 release_sock(sk);
755 return err;
756}
757
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200758static int sco_sock_shutdown(struct socket *sock, int how)
759{
760 struct sock *sk = sock->sk;
761 int err = 0;
762
763 BT_DBG("sock %p, sk %p", sock, sk);
764
765 if (!sk)
766 return 0;
767
768 lock_sock(sk);
769 if (!sk->sk_shutdown) {
770 sk->sk_shutdown = SHUTDOWN_MASK;
771 sco_sock_clear_timer(sk);
772 __sco_sock_close(sk);
773
774 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
775 err = bt_sock_wait_state(sk, BT_CLOSED,
776 sk->sk_lingertime);
777 }
778 release_sock(sk);
779 return err;
780}
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782static int sco_sock_release(struct socket *sock)
783{
784 struct sock *sk = sock->sk;
785 int err = 0;
786
787 BT_DBG("sock %p, sk %p", sock, sk);
788
789 if (!sk)
790 return 0;
791
792 sco_sock_close(sk);
793
794 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
795 lock_sock(sk);
796 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
797 release_sock(sk);
798 }
799
800 sock_orphan(sk);
801 sco_sock_kill(sk);
802 return err;
803}
804
805static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
806{
807 BT_DBG("conn %p", conn);
808
809 sco_pi(sk)->conn = conn;
810 conn->sk = sk;
811
812 if (parent)
813 bt_accept_enqueue(parent, sk);
814}
815
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900816/* Delete channel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * Must be called on the locked socket. */
818static void sco_chan_del(struct sock *sk, int err)
819{
820 struct sco_conn *conn;
821
822 conn = sco_pi(sk)->conn;
823
824 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
825
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900826 if (conn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 sco_conn_lock(conn);
828 conn->sk = NULL;
829 sco_pi(sk)->conn = NULL;
830 sco_conn_unlock(conn);
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300831
832 if (conn->hcon)
833 hci_conn_put(conn->hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
836 sk->sk_state = BT_CLOSED;
837 sk->sk_err = err;
838 sk->sk_state_change(sk);
839
840 sock_set_flag(sk, SOCK_ZAPPED);
841}
842
843static void sco_conn_ready(struct sco_conn *conn)
844{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200845 struct sock *parent;
846 struct sock *sk = conn->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 BT_DBG("conn %p", conn);
849
850 sco_conn_lock(conn);
851
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200852 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 sco_sock_clear_timer(sk);
854 bh_lock_sock(sk);
855 sk->sk_state = BT_CONNECTED;
856 sk->sk_state_change(sk);
857 bh_unlock_sock(sk);
858 } else {
859 parent = sco_get_sock_listen(conn->src);
860 if (!parent)
861 goto done;
862
863 bh_lock_sock(parent);
864
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300865 sk = sco_sock_alloc(sock_net(parent), NULL,
866 BTPROTO_SCO, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (!sk) {
868 bh_unlock_sock(parent);
869 goto done;
870 }
871
872 sco_sock_init(sk, parent);
873
874 bacpy(&bt_sk(sk)->src, conn->src);
875 bacpy(&bt_sk(sk)->dst, conn->dst);
876
877 hci_conn_hold(conn->hcon);
878 __sco_chan_add(conn, sk, parent);
879
880 sk->sk_state = BT_CONNECTED;
881
882 /* Wake up parent */
883 parent->sk_data_ready(parent, 1);
884
885 bh_unlock_sock(parent);
886 }
887
888done:
889 sco_conn_unlock(conn);
890}
891
892/* ----- SCO interface with lower layer (HCI) ----- */
893static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
894{
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100895 register struct sock *sk;
896 struct hlist_node *node;
897 int lm = 0;
898
899 if (type != SCO_LINK && type != ESCO_LINK)
Gustavo F. Padovanc89ad732010-11-01 19:08:50 +0000900 return -EINVAL;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
903
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100904 /* Find listening sockets */
905 read_lock(&sco_sk_list.lock);
906 sk_for_each(sk, node, &sco_sk_list.head) {
907 if (sk->sk_state != BT_LISTEN)
908 continue;
909
910 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
911 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
912 lm |= HCI_LM_ACCEPT;
913 break;
914 }
915 }
916 read_unlock(&sco_sk_list.lock);
917
918 return lm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
922{
923 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
924
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200925 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
Gustavo F. Padovanc89ad732010-11-01 19:08:50 +0000926 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (!status) {
929 struct sco_conn *conn;
930
931 conn = sco_conn_add(hcon, status);
932 if (conn)
933 sco_conn_ready(conn);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900934 } else
Joe Perchese1750722011-06-29 18:18:29 -0700935 sco_conn_del(hcon, bt_to_errno(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 return 0;
938}
939
Marcel Holtmann2950f212009-02-12 14:02:50 +0100940static int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 BT_DBG("hcon %p reason %d", hcon, reason);
943
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200944 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
Gustavo F. Padovanc89ad732010-11-01 19:08:50 +0000945 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Joe Perchese1750722011-06-29 18:18:29 -0700947 sco_conn_del(hcon, bt_to_errno(reason));
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
950}
951
952static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
953{
954 struct sco_conn *conn = hcon->sco_data;
955
956 if (!conn)
957 goto drop;
958
959 BT_DBG("conn %p len %d", conn, skb->len);
960
961 if (skb->len) {
962 sco_recv_frame(conn, skb);
963 return 0;
964 }
965
966drop:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900967 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return 0;
969}
970
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100971static int sco_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct sock *sk;
974 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 read_lock_bh(&sco_sk_list.lock);
977
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800978 sk_for_each(sk, node, &sco_sk_list.head) {
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100979 seq_printf(f, "%s %s %d\n", batostr(&bt_sk(sk)->src),
980 batostr(&bt_sk(sk)->dst), sk->sk_state);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 read_unlock_bh(&sco_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800984
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100988static int sco_debugfs_open(struct inode *inode, struct file *file)
989{
990 return single_open(file, sco_debugfs_show, inode->i_private);
991}
992
993static const struct file_operations sco_debugfs_fops = {
994 .open = sco_debugfs_open,
995 .read = seq_read,
996 .llseek = seq_lseek,
997 .release = single_release,
998};
999
1000static struct dentry *sco_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001002static const struct proto_ops sco_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 .family = PF_BLUETOOTH,
1004 .owner = THIS_MODULE,
1005 .release = sco_sock_release,
1006 .bind = sco_sock_bind,
1007 .connect = sco_sock_connect,
1008 .listen = sco_sock_listen,
1009 .accept = sco_sock_accept,
1010 .getname = sco_sock_getname,
1011 .sendmsg = sco_sock_sendmsg,
1012 .recvmsg = bt_sock_recvmsg,
1013 .poll = bt_sock_poll,
Marcel Holtmann3241ad82008-07-14 20:13:50 +02001014 .ioctl = bt_sock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 .mmap = sock_no_mmap,
1016 .socketpair = sock_no_socketpair,
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +02001017 .shutdown = sco_sock_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 .setsockopt = sco_sock_setsockopt,
1019 .getsockopt = sco_sock_getsockopt
1020};
1021
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001022static const struct net_proto_family sco_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 .family = PF_BLUETOOTH,
1024 .owner = THIS_MODULE,
1025 .create = sco_sock_create,
1026};
1027
1028static struct hci_proto sco_hci_proto = {
1029 .name = "SCO",
1030 .id = HCI_PROTO_SCO,
1031 .connect_ind = sco_connect_ind,
1032 .connect_cfm = sco_connect_cfm,
Marcel Holtmann2950f212009-02-12 14:02:50 +01001033 .disconn_cfm = sco_disconn_cfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 .recv_scodata = sco_recv_scodata
1035};
1036
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001037int __init sco_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 int err;
1040
1041 err = proto_register(&sco_proto, 0);
1042 if (err < 0)
1043 return err;
1044
1045 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1046 if (err < 0) {
1047 BT_ERR("SCO socket registration failed");
1048 goto error;
1049 }
1050
1051 err = hci_register_proto(&sco_hci_proto);
1052 if (err < 0) {
1053 BT_ERR("SCO protocol registration failed");
1054 bt_sock_unregister(BTPROTO_SCO);
1055 goto error;
1056 }
1057
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001058 if (bt_debugfs) {
1059 sco_debugfs = debugfs_create_file("sco", 0444,
1060 bt_debugfs, NULL, &sco_debugfs_fops);
1061 if (!sco_debugfs)
1062 BT_ERR("Failed to create SCO debug file");
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 BT_INFO("SCO socket layer initialized");
1066
1067 return 0;
1068
1069error:
1070 proto_unregister(&sco_proto);
1071 return err;
1072}
1073
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001074void __exit sco_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001076 debugfs_remove(sco_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1079 BT_ERR("SCO socket unregistration failed");
1080
1081 if (hci_unregister_proto(&sco_hci_proto) < 0)
1082 BT_ERR("SCO protocol unregistration failed");
1083
1084 proto_unregister(&sco_proto);
1085}
1086
Marcel Holtmann7cb127d2008-07-14 20:13:53 +02001087module_param(disable_esco, bool, 0644);
1088MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");