blob: d0927d1fdadaba4887dbac40064c4328541b9b00 [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>
47#include <asm/uaccess.h>
48
49#include <net/bluetooth/bluetooth.h>
50#include <net/bluetooth/hci_core.h>
51#include <net/bluetooth/sco.h>
52
Marcel Holtmann7cb127d2008-07-14 20:13:53 +020053#define VERSION "0.6"
54
55static int disable_esco = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080057static const struct proto_ops sco_sock_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59static struct bt_sock_list sco_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070060 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
64static void sco_chan_del(struct sock *sk, int err);
65
66static int sco_conn_del(struct hci_conn *conn, int err);
67
68static void sco_sock_close(struct sock *sk);
69static void sco_sock_kill(struct sock *sk);
70
71/* ---- SCO timers ---- */
72static void sco_sock_timeout(unsigned long arg)
73{
74 struct sock *sk = (struct sock *) arg;
75
76 BT_DBG("sock %p state %d", sk, sk->sk_state);
77
78 bh_lock_sock(sk);
79 sk->sk_err = ETIMEDOUT;
80 sk->sk_state_change(sk);
81 bh_unlock_sock(sk);
82
83 sco_sock_kill(sk);
84 sock_put(sk);
85}
86
87static void sco_sock_set_timer(struct sock *sk, long timeout)
88{
89 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
90 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
91}
92
93static void sco_sock_clear_timer(struct sock *sk)
94{
95 BT_DBG("sock %p state %d", sk, sk->sk_state);
96 sk_stop_timer(sk, &sk->sk_timer);
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* ---- SCO connections ---- */
100static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
101{
102 struct hci_dev *hdev = hcon->hdev;
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200103 struct sco_conn *conn = hcon->sco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200105 if (conn || status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return conn;
107
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200108 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
109 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 spin_lock_init(&conn->lock);
113
114 hcon->sco_data = conn;
115 conn->hcon = hcon;
116
117 conn->src = &hdev->bdaddr;
118 conn->dst = &hcon->dst;
119
120 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
130static inline struct sock *sco_chan_get(struct sco_conn *conn)
131{
132 struct sock *sk = NULL;
133 sco_conn_lock(conn);
134 sk = conn->sk;
135 sco_conn_unlock(conn);
136 return sk;
137}
138
139static int sco_conn_del(struct hci_conn *hcon, int err)
140{
141 struct sco_conn *conn;
142 struct sock *sk;
143
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900144 if (!(conn = hcon->sco_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return 0;
146
147 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
148
149 /* Kill socket */
150 if ((sk = sco_chan_get(conn))) {
151 bh_lock_sock(sk);
152 sco_sock_clear_timer(sk);
153 sco_chan_del(sk, err);
154 bh_unlock_sock(sk);
155 sco_sock_kill(sk);
156 }
157
158 hcon->sco_data = NULL;
159 kfree(conn);
160 return 0;
161}
162
163static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
164{
165 int err = 0;
166
167 sco_conn_lock(conn);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300168 if (conn->sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 err = -EBUSY;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300170 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 __sco_chan_add(conn, sk, parent);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 sco_conn_unlock(conn);
174 return err;
175}
176
177static int sco_connect(struct sock *sk)
178{
179 bdaddr_t *src = &bt_sk(sk)->src;
180 bdaddr_t *dst = &bt_sk(sk)->dst;
181 struct sco_conn *conn;
182 struct hci_conn *hcon;
183 struct hci_dev *hdev;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200184 int err, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 BT_DBG("%s -> %s", batostr(src), batostr(dst));
187
188 if (!(hdev = hci_get_route(dst, src)))
189 return -EHOSTUNREACH;
190
191 hci_dev_lock_bh(hdev);
192
193 err = -ENOMEM;
194
Marcel Holtmann7cb127d2008-07-14 20:13:53 +0200195 if (lmp_esco_capable(hdev) && !disable_esco)
196 type = ESCO_LINK;
197 else
198 type = SCO_LINK;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200199
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100200 hcon = hci_connect(hdev, type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (!hcon)
202 goto done;
203
204 conn = sco_conn_add(hcon, 0);
205 if (!conn) {
206 hci_conn_put(hcon);
207 goto done;
208 }
209
210 /* Update source addr of the socket */
211 bacpy(src, conn->src);
212
213 err = sco_chan_add(conn, sk, NULL);
214 if (err)
215 goto done;
216
217 if (hcon->state == BT_CONNECTED) {
218 sco_sock_clear_timer(sk);
219 sk->sk_state = BT_CONNECTED;
220 } else {
221 sk->sk_state = BT_CONNECT;
222 sco_sock_set_timer(sk, sk->sk_sndtimeo);
223 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225done:
226 hci_dev_unlock_bh(hdev);
227 hci_dev_put(hdev);
228 return err;
229}
230
231static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
232{
233 struct sco_conn *conn = sco_pi(sk)->conn;
234 struct sk_buff *skb;
235 int err, count;
236
237 /* Check outgoing MTU */
238 if (len > conn->mtu)
239 return -EINVAL;
240
241 BT_DBG("sk %p len %d", sk, len);
242
243 count = min_t(unsigned int, conn->mtu, len);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300244 skb = bt_skb_send_alloc(sk, count,
245 msg->msg_flags & MSG_DONTWAIT, &err);
246 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return err;
248
249 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300250 kfree_skb(skb);
251 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -0300254 hci_send_sco(conn->hcon, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
260{
261 struct sock *sk = sco_chan_get(conn);
262
263 if (!sk)
264 goto drop;
265
266 BT_DBG("sk %p len %d", sk, skb->len);
267
268 if (sk->sk_state != BT_CONNECTED)
269 goto drop;
270
271 if (!sock_queue_rcv_skb(sk, skb))
272 return;
273
274drop:
275 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/* -------- Socket interface ---------- */
279static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
280{
281 struct sock *sk;
282 struct hlist_node *node;
283
284 sk_for_each(sk, node, &sco_sk_list.head)
285 if (!bacmp(&bt_sk(sk)->src, ba))
286 goto found;
287 sk = NULL;
288found:
289 return sk;
290}
291
292/* Find socket listening on source bdaddr.
293 * Returns closest match.
294 */
295static struct sock *sco_get_sock_listen(bdaddr_t *src)
296{
297 struct sock *sk = NULL, *sk1 = NULL;
298 struct hlist_node *node;
299
300 read_lock(&sco_sk_list.lock);
301
302 sk_for_each(sk, node, &sco_sk_list.head) {
303 if (sk->sk_state != BT_LISTEN)
304 continue;
305
306 /* Exact match. */
307 if (!bacmp(&bt_sk(sk)->src, src))
308 break;
309
310 /* Closest match */
311 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
312 sk1 = sk;
313 }
314
315 read_unlock(&sco_sk_list.lock);
316
317 return node ? sk : sk1;
318}
319
320static void sco_sock_destruct(struct sock *sk)
321{
322 BT_DBG("sk %p", sk);
323
324 skb_queue_purge(&sk->sk_receive_queue);
325 skb_queue_purge(&sk->sk_write_queue);
326}
327
328static void sco_sock_cleanup_listen(struct sock *parent)
329{
330 struct sock *sk;
331
332 BT_DBG("parent %p", parent);
333
334 /* Close not yet accepted channels */
335 while ((sk = bt_accept_dequeue(parent, NULL))) {
336 sco_sock_close(sk);
337 sco_sock_kill(sk);
338 }
339
340 parent->sk_state = BT_CLOSED;
341 sock_set_flag(parent, SOCK_ZAPPED);
342}
343
344/* Kill socket (only if zapped and orphan)
345 * Must be called on unlocked socket.
346 */
347static void sco_sock_kill(struct sock *sk)
348{
349 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
350 return;
351
352 BT_DBG("sk %p state %d", sk, sk->sk_state);
353
354 /* Kill poor orphan */
355 bt_sock_unlink(&sco_sk_list, sk);
356 sock_set_flag(sk, SOCK_DEAD);
357 sock_put(sk);
358}
359
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200360static void __sco_sock_close(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200362 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 switch (sk->sk_state) {
365 case BT_LISTEN:
366 sco_sock_cleanup_listen(sk);
367 break;
368
369 case BT_CONNECTED:
370 case BT_CONFIG:
371 case BT_CONNECT:
372 case BT_DISCONN:
373 sco_chan_del(sk, ECONNRESET);
374 break;
375
376 default:
377 sock_set_flag(sk, SOCK_ZAPPED);
378 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700379 }
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200380}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200382/* Must be called on unlocked socket. */
383static void sco_sock_close(struct sock *sk)
384{
385 sco_sock_clear_timer(sk);
386 lock_sock(sk);
387 __sco_sock_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 sco_sock_kill(sk);
390}
391
392static void sco_sock_init(struct sock *sk, struct sock *parent)
393{
394 BT_DBG("sk %p", sk);
395
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900396 if (parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 sk->sk_type = parent->sk_type;
398}
399
400static struct proto sco_proto = {
401 .name = "SCO",
402 .owner = THIS_MODULE,
403 .obj_size = sizeof(struct sco_pinfo)
404};
405
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700406static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 struct sock *sk;
409
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700410 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!sk)
412 return NULL;
413
414 sock_init_data(sock, sk);
415 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
416
417 sk->sk_destruct = sco_sock_destruct;
418 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
419
420 sock_reset_flag(sk, SOCK_ZAPPED);
421
422 sk->sk_protocol = proto;
423 sk->sk_state = BT_OPEN;
424
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800425 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 bt_sock_link(&sco_sk_list, sk);
428 return sk;
429}
430
Eric Paris3f378b62009-11-05 22:18:14 -0800431static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
432 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct sock *sk;
435
436 BT_DBG("sock %p", sock);
437
438 sock->state = SS_UNCONNECTED;
439
440 if (sock->type != SOCK_SEQPACKET)
441 return -ESOCKTNOSUPPORT;
442
443 sock->ops = &sco_sock_ops;
444
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700445 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200446 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -ENOMEM;
448
449 sco_sock_init(sk, NULL);
450 return 0;
451}
452
453static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
454{
455 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
456 struct sock *sk = sock->sk;
457 bdaddr_t *src = &sa->sco_bdaddr;
458 int err = 0;
459
460 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
461
462 if (!addr || addr->sa_family != AF_BLUETOOTH)
463 return -EINVAL;
464
465 lock_sock(sk);
466
467 if (sk->sk_state != BT_OPEN) {
468 err = -EBADFD;
469 goto done;
470 }
471
472 write_lock_bh(&sco_sk_list.lock);
473
474 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
475 err = -EADDRINUSE;
476 } else {
477 /* Save source address */
478 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
479 sk->sk_state = BT_BOUND;
480 }
481
482 write_unlock_bh(&sco_sk_list.lock);
483
484done:
485 release_sock(sk);
486 return err;
487}
488
489static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
490{
491 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
492 struct sock *sk = sock->sk;
493 int err = 0;
494
495
496 BT_DBG("sk %p", sk);
497
Changli Gao6503d962010-03-31 22:58:26 +0000498 if (alen < sizeof(struct sockaddr_sco) ||
499 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return -EINVAL;
501
502 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
503 return -EBADFD;
504
505 if (sk->sk_type != SOCK_SEQPACKET)
506 return -EINVAL;
507
508 lock_sock(sk);
509
510 /* Set destination address and psm */
511 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
512
513 if ((err = sco_connect(sk)))
514 goto done;
515
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900516 err = bt_sock_wait_state(sk, BT_CONNECTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 sock_sndtimeo(sk, flags & O_NONBLOCK));
518
519done:
520 release_sock(sk);
521 return err;
522}
523
524static int sco_sock_listen(struct socket *sock, int backlog)
525{
526 struct sock *sk = sock->sk;
527 int err = 0;
528
529 BT_DBG("sk %p backlog %d", sk, backlog);
530
531 lock_sock(sk);
532
533 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
534 err = -EBADFD;
535 goto done;
536 }
537
538 sk->sk_max_ack_backlog = backlog;
539 sk->sk_ack_backlog = 0;
540 sk->sk_state = BT_LISTEN;
541
542done:
543 release_sock(sk);
544 return err;
545}
546
547static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
548{
549 DECLARE_WAITQUEUE(wait, current);
550 struct sock *sk = sock->sk, *ch;
551 long timeo;
552 int err = 0;
553
554 lock_sock(sk);
555
556 if (sk->sk_state != BT_LISTEN) {
557 err = -EBADFD;
558 goto done;
559 }
560
561 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
562
563 BT_DBG("sk %p timeo %ld", sk, timeo);
564
565 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000566 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 while (!(ch = bt_accept_dequeue(sk, newsock))) {
568 set_current_state(TASK_INTERRUPTIBLE);
569 if (!timeo) {
570 err = -EAGAIN;
571 break;
572 }
573
574 release_sock(sk);
575 timeo = schedule_timeout(timeo);
576 lock_sock(sk);
577
578 if (sk->sk_state != BT_LISTEN) {
579 err = -EBADFD;
580 break;
581 }
582
583 if (signal_pending(current)) {
584 err = sock_intr_errno(timeo);
585 break;
586 }
587 }
588 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000589 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if (err)
592 goto done;
593
594 newsock->state = SS_CONNECTED;
595
596 BT_DBG("new socket %p", ch);
597
598done:
599 release_sock(sk);
600 return err;
601}
602
603static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
604{
605 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
606 struct sock *sk = sock->sk;
607
608 BT_DBG("sock %p, sk %p", sock, sk);
609
610 addr->sa_family = AF_BLUETOOTH;
611 *len = sizeof(struct sockaddr_sco);
612
613 if (peer)
614 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
615 else
616 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
617
618 return 0;
619}
620
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900621static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct msghdr *msg, size_t len)
623{
624 struct sock *sk = sock->sk;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300625 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 BT_DBG("sock %p, sk %p", sock, sk);
628
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800629 err = sock_error(sk);
630 if (err)
631 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (msg->msg_flags & MSG_OOB)
634 return -EOPNOTSUPP;
635
636 lock_sock(sk);
637
638 if (sk->sk_state == BT_CONNECTED)
639 err = sco_send_frame(sk, msg, len);
640 else
641 err = -ENOTCONN;
642
643 release_sock(sk);
644 return err;
645}
646
David S. Millerb7058842009-09-30 16:12:20 -0700647static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct sock *sk = sock->sk;
650 int err = 0;
651
652 BT_DBG("sk %p", sk);
653
654 lock_sock(sk);
655
656 switch (optname) {
657 default:
658 err = -ENOPROTOOPT;
659 break;
660 }
661
662 release_sock(sk);
663 return err;
664}
665
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100666static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct sock *sk = sock->sk;
669 struct sco_options opts;
670 struct sco_conninfo cinfo;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900671 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 BT_DBG("sk %p", sk);
674
675 if (get_user(len, optlen))
676 return -EFAULT;
677
678 lock_sock(sk);
679
680 switch (optname) {
681 case SCO_OPTIONS:
682 if (sk->sk_state != BT_CONNECTED) {
683 err = -ENOTCONN;
684 break;
685 }
686
687 opts.mtu = sco_pi(sk)->conn->mtu;
688
689 BT_DBG("mtu %d", opts.mtu);
690
691 len = min_t(unsigned int, len, sizeof(opts));
692 if (copy_to_user(optval, (char *)&opts, len))
693 err = -EFAULT;
694
695 break;
696
697 case SCO_CONNINFO:
698 if (sk->sk_state != BT_CONNECTED) {
699 err = -ENOTCONN;
700 break;
701 }
702
703 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
704 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
705
706 len = min_t(unsigned int, len, sizeof(cinfo));
707 if (copy_to_user(optval, (char *)&cinfo, len))
708 err = -EFAULT;
709
710 break;
711
712 default:
713 err = -ENOPROTOOPT;
714 break;
715 }
716
717 release_sock(sk);
718 return err;
719}
720
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100721static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
722{
723 struct sock *sk = sock->sk;
724 int len, err = 0;
725
726 BT_DBG("sk %p", sk);
727
728 if (level == SOL_SCO)
729 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
730
731 if (get_user(len, optlen))
732 return -EFAULT;
733
734 lock_sock(sk);
735
736 switch (optname) {
737 default:
738 err = -ENOPROTOOPT;
739 break;
740 }
741
742 release_sock(sk);
743 return err;
744}
745
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200746static int sco_sock_shutdown(struct socket *sock, int how)
747{
748 struct sock *sk = sock->sk;
749 int err = 0;
750
751 BT_DBG("sock %p, sk %p", sock, sk);
752
753 if (!sk)
754 return 0;
755
756 lock_sock(sk);
757 if (!sk->sk_shutdown) {
758 sk->sk_shutdown = SHUTDOWN_MASK;
759 sco_sock_clear_timer(sk);
760 __sco_sock_close(sk);
761
762 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
763 err = bt_sock_wait_state(sk, BT_CLOSED,
764 sk->sk_lingertime);
765 }
766 release_sock(sk);
767 return err;
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static int sco_sock_release(struct socket *sock)
771{
772 struct sock *sk = sock->sk;
773 int err = 0;
774
775 BT_DBG("sock %p, sk %p", sock, sk);
776
777 if (!sk)
778 return 0;
779
780 sco_sock_close(sk);
781
782 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
783 lock_sock(sk);
784 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
785 release_sock(sk);
786 }
787
788 sock_orphan(sk);
789 sco_sock_kill(sk);
790 return err;
791}
792
793static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
794{
795 BT_DBG("conn %p", conn);
796
797 sco_pi(sk)->conn = conn;
798 conn->sk = sk;
799
800 if (parent)
801 bt_accept_enqueue(parent, sk);
802}
803
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900804/* Delete channel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * Must be called on the locked socket. */
806static void sco_chan_del(struct sock *sk, int err)
807{
808 struct sco_conn *conn;
809
810 conn = sco_pi(sk)->conn;
811
812 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
813
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900814 if (conn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 sco_conn_lock(conn);
816 conn->sk = NULL;
817 sco_pi(sk)->conn = NULL;
818 sco_conn_unlock(conn);
819 hci_conn_put(conn->hcon);
820 }
821
822 sk->sk_state = BT_CLOSED;
823 sk->sk_err = err;
824 sk->sk_state_change(sk);
825
826 sock_set_flag(sk, SOCK_ZAPPED);
827}
828
829static void sco_conn_ready(struct sco_conn *conn)
830{
831 struct sock *parent, *sk;
832
833 BT_DBG("conn %p", conn);
834
835 sco_conn_lock(conn);
836
837 if ((sk = conn->sk)) {
838 sco_sock_clear_timer(sk);
839 bh_lock_sock(sk);
840 sk->sk_state = BT_CONNECTED;
841 sk->sk_state_change(sk);
842 bh_unlock_sock(sk);
843 } else {
844 parent = sco_get_sock_listen(conn->src);
845 if (!parent)
846 goto done;
847
848 bh_lock_sock(parent);
849
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300850 sk = sco_sock_alloc(sock_net(parent), NULL,
851 BTPROTO_SCO, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (!sk) {
853 bh_unlock_sock(parent);
854 goto done;
855 }
856
857 sco_sock_init(sk, parent);
858
859 bacpy(&bt_sk(sk)->src, conn->src);
860 bacpy(&bt_sk(sk)->dst, conn->dst);
861
862 hci_conn_hold(conn->hcon);
863 __sco_chan_add(conn, sk, parent);
864
865 sk->sk_state = BT_CONNECTED;
866
867 /* Wake up parent */
868 parent->sk_data_ready(parent, 1);
869
870 bh_unlock_sock(parent);
871 }
872
873done:
874 sco_conn_unlock(conn);
875}
876
877/* ----- SCO interface with lower layer (HCI) ----- */
878static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
879{
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100880 register struct sock *sk;
881 struct hlist_node *node;
882 int lm = 0;
883
884 if (type != SCO_LINK && type != ESCO_LINK)
885 return 0;
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
888
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100889 /* Find listening sockets */
890 read_lock(&sco_sk_list.lock);
891 sk_for_each(sk, node, &sco_sk_list.head) {
892 if (sk->sk_state != BT_LISTEN)
893 continue;
894
895 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
896 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
897 lm |= HCI_LM_ACCEPT;
898 break;
899 }
900 }
901 read_unlock(&sco_sk_list.lock);
902
903 return lm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
907{
908 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
909
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200910 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return 0;
912
913 if (!status) {
914 struct sco_conn *conn;
915
916 conn = sco_conn_add(hcon, status);
917 if (conn)
918 sco_conn_ready(conn);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900919 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 sco_conn_del(hcon, bt_err(status));
921
922 return 0;
923}
924
Marcel Holtmann2950f212009-02-12 14:02:50 +0100925static int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 BT_DBG("hcon %p reason %d", hcon, reason);
928
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200929 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return 0;
931
932 sco_conn_del(hcon, bt_err(reason));
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 0;
935}
936
937static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
938{
939 struct sco_conn *conn = hcon->sco_data;
940
941 if (!conn)
942 goto drop;
943
944 BT_DBG("conn %p len %d", conn, skb->len);
945
946 if (skb->len) {
947 sco_recv_frame(conn, skb);
948 return 0;
949 }
950
951drop:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900952 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
954}
955
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100956static int sco_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 struct sock *sk;
959 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 read_lock_bh(&sco_sk_list.lock);
962
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800963 sk_for_each(sk, node, &sco_sk_list.head) {
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100964 seq_printf(f, "%s %s %d\n", batostr(&bt_sk(sk)->src),
965 batostr(&bt_sk(sk)->dst), sk->sk_state);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 read_unlock_bh(&sco_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800969
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100973static int sco_debugfs_open(struct inode *inode, struct file *file)
974{
975 return single_open(file, sco_debugfs_show, inode->i_private);
976}
977
978static const struct file_operations sco_debugfs_fops = {
979 .open = sco_debugfs_open,
980 .read = seq_read,
981 .llseek = seq_lseek,
982 .release = single_release,
983};
984
985static struct dentry *sco_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800987static const struct proto_ops sco_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 .family = PF_BLUETOOTH,
989 .owner = THIS_MODULE,
990 .release = sco_sock_release,
991 .bind = sco_sock_bind,
992 .connect = sco_sock_connect,
993 .listen = sco_sock_listen,
994 .accept = sco_sock_accept,
995 .getname = sco_sock_getname,
996 .sendmsg = sco_sock_sendmsg,
997 .recvmsg = bt_sock_recvmsg,
998 .poll = bt_sock_poll,
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200999 .ioctl = bt_sock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 .mmap = sock_no_mmap,
1001 .socketpair = sock_no_socketpair,
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +02001002 .shutdown = sco_sock_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 .setsockopt = sco_sock_setsockopt,
1004 .getsockopt = sco_sock_getsockopt
1005};
1006
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001007static const struct net_proto_family sco_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 .family = PF_BLUETOOTH,
1009 .owner = THIS_MODULE,
1010 .create = sco_sock_create,
1011};
1012
1013static struct hci_proto sco_hci_proto = {
1014 .name = "SCO",
1015 .id = HCI_PROTO_SCO,
1016 .connect_ind = sco_connect_ind,
1017 .connect_cfm = sco_connect_cfm,
Marcel Holtmann2950f212009-02-12 14:02:50 +01001018 .disconn_cfm = sco_disconn_cfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 .recv_scodata = sco_recv_scodata
1020};
1021
1022static int __init sco_init(void)
1023{
1024 int err;
1025
1026 err = proto_register(&sco_proto, 0);
1027 if (err < 0)
1028 return err;
1029
1030 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1031 if (err < 0) {
1032 BT_ERR("SCO socket registration failed");
1033 goto error;
1034 }
1035
1036 err = hci_register_proto(&sco_hci_proto);
1037 if (err < 0) {
1038 BT_ERR("SCO protocol registration failed");
1039 bt_sock_unregister(BTPROTO_SCO);
1040 goto error;
1041 }
1042
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001043 if (bt_debugfs) {
1044 sco_debugfs = debugfs_create_file("sco", 0444,
1045 bt_debugfs, NULL, &sco_debugfs_fops);
1046 if (!sco_debugfs)
1047 BT_ERR("Failed to create SCO debug file");
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 BT_INFO("SCO (Voice Link) ver %s", VERSION);
1051 BT_INFO("SCO socket layer initialized");
1052
1053 return 0;
1054
1055error:
1056 proto_unregister(&sco_proto);
1057 return err;
1058}
1059
1060static void __exit sco_exit(void)
1061{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001062 debugfs_remove(sco_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1065 BT_ERR("SCO socket unregistration failed");
1066
1067 if (hci_unregister_proto(&sco_hci_proto) < 0)
1068 BT_ERR("SCO protocol unregistration failed");
1069
1070 proto_unregister(&sco_proto);
1071}
1072
1073module_init(sco_init);
1074module_exit(sco_exit);
1075
Marcel Holtmann7cb127d2008-07-14 20:13:53 +02001076module_param(disable_esco, bool, 0644);
1077MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");
1078
Marcel Holtmann63fbd242008-08-18 13:23:53 +02001079MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1081MODULE_VERSION(VERSION);
1082MODULE_LICENSE("GPL");
1083MODULE_ALIAS("bt-proto-2");