blob: ca6b2ad1c3fc02a6bcbf16a034aa253e05b1a456 [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);
168 if (conn->sk) {
169 err = -EBUSY;
170 } else {
171 __sco_chan_add(conn, sk, parent);
172 }
173 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);
244 if (!(skb = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err)))
245 return err;
246
247 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
248 err = -EFAULT;
249 goto fail;
250 }
251
252 if ((err = hci_send_sco(conn->hcon, skb)) < 0)
Eric Sesterhenncdee5752006-04-05 22:28:14 -0700253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 return count;
256
257fail:
258 kfree_skb(skb);
259 return err;
260}
261
262static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
263{
264 struct sock *sk = sco_chan_get(conn);
265
266 if (!sk)
267 goto drop;
268
269 BT_DBG("sk %p len %d", sk, skb->len);
270
271 if (sk->sk_state != BT_CONNECTED)
272 goto drop;
273
274 if (!sock_queue_rcv_skb(sk, skb))
275 return;
276
277drop:
278 kfree_skb(skb);
279 return;
280}
281
282/* -------- Socket interface ---------- */
283static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
284{
285 struct sock *sk;
286 struct hlist_node *node;
287
288 sk_for_each(sk, node, &sco_sk_list.head)
289 if (!bacmp(&bt_sk(sk)->src, ba))
290 goto found;
291 sk = NULL;
292found:
293 return sk;
294}
295
296/* Find socket listening on source bdaddr.
297 * Returns closest match.
298 */
299static struct sock *sco_get_sock_listen(bdaddr_t *src)
300{
301 struct sock *sk = NULL, *sk1 = NULL;
302 struct hlist_node *node;
303
304 read_lock(&sco_sk_list.lock);
305
306 sk_for_each(sk, node, &sco_sk_list.head) {
307 if (sk->sk_state != BT_LISTEN)
308 continue;
309
310 /* Exact match. */
311 if (!bacmp(&bt_sk(sk)->src, src))
312 break;
313
314 /* Closest match */
315 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
316 sk1 = sk;
317 }
318
319 read_unlock(&sco_sk_list.lock);
320
321 return node ? sk : sk1;
322}
323
324static void sco_sock_destruct(struct sock *sk)
325{
326 BT_DBG("sk %p", sk);
327
328 skb_queue_purge(&sk->sk_receive_queue);
329 skb_queue_purge(&sk->sk_write_queue);
330}
331
332static void sco_sock_cleanup_listen(struct sock *parent)
333{
334 struct sock *sk;
335
336 BT_DBG("parent %p", parent);
337
338 /* Close not yet accepted channels */
339 while ((sk = bt_accept_dequeue(parent, NULL))) {
340 sco_sock_close(sk);
341 sco_sock_kill(sk);
342 }
343
344 parent->sk_state = BT_CLOSED;
345 sock_set_flag(parent, SOCK_ZAPPED);
346}
347
348/* Kill socket (only if zapped and orphan)
349 * Must be called on unlocked socket.
350 */
351static void sco_sock_kill(struct sock *sk)
352{
353 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
354 return;
355
356 BT_DBG("sk %p state %d", sk, sk->sk_state);
357
358 /* Kill poor orphan */
359 bt_sock_unlink(&sco_sk_list, sk);
360 sock_set_flag(sk, SOCK_DEAD);
361 sock_put(sk);
362}
363
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200364static void __sco_sock_close(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200366 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 switch (sk->sk_state) {
369 case BT_LISTEN:
370 sco_sock_cleanup_listen(sk);
371 break;
372
373 case BT_CONNECTED:
374 case BT_CONFIG:
375 case BT_CONNECT:
376 case BT_DISCONN:
377 sco_chan_del(sk, ECONNRESET);
378 break;
379
380 default:
381 sock_set_flag(sk, SOCK_ZAPPED);
382 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700383 }
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200384}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200386/* Must be called on unlocked socket. */
387static void sco_sock_close(struct sock *sk)
388{
389 sco_sock_clear_timer(sk);
390 lock_sock(sk);
391 __sco_sock_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 sco_sock_kill(sk);
394}
395
396static void sco_sock_init(struct sock *sk, struct sock *parent)
397{
398 BT_DBG("sk %p", sk);
399
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900400 if (parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 sk->sk_type = parent->sk_type;
402}
403
404static struct proto sco_proto = {
405 .name = "SCO",
406 .owner = THIS_MODULE,
407 .obj_size = sizeof(struct sco_pinfo)
408};
409
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700410static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 struct sock *sk;
413
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700414 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (!sk)
416 return NULL;
417
418 sock_init_data(sock, sk);
419 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
420
421 sk->sk_destruct = sco_sock_destruct;
422 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
423
424 sock_reset_flag(sk, SOCK_ZAPPED);
425
426 sk->sk_protocol = proto;
427 sk->sk_state = BT_OPEN;
428
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800429 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 bt_sock_link(&sco_sk_list, sk);
432 return sk;
433}
434
Eric Paris3f378b62009-11-05 22:18:14 -0800435static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
436 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 struct sock *sk;
439
440 BT_DBG("sock %p", sock);
441
442 sock->state = SS_UNCONNECTED;
443
444 if (sock->type != SOCK_SEQPACKET)
445 return -ESOCKTNOSUPPORT;
446
447 sock->ops = &sco_sock_ops;
448
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700449 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200450 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return -ENOMEM;
452
453 sco_sock_init(sk, NULL);
454 return 0;
455}
456
457static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
458{
459 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
460 struct sock *sk = sock->sk;
461 bdaddr_t *src = &sa->sco_bdaddr;
462 int err = 0;
463
464 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
465
466 if (!addr || addr->sa_family != AF_BLUETOOTH)
467 return -EINVAL;
468
469 lock_sock(sk);
470
471 if (sk->sk_state != BT_OPEN) {
472 err = -EBADFD;
473 goto done;
474 }
475
476 write_lock_bh(&sco_sk_list.lock);
477
478 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
479 err = -EADDRINUSE;
480 } else {
481 /* Save source address */
482 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
483 sk->sk_state = BT_BOUND;
484 }
485
486 write_unlock_bh(&sco_sk_list.lock);
487
488done:
489 release_sock(sk);
490 return err;
491}
492
493static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
494{
495 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
496 struct sock *sk = sock->sk;
497 int err = 0;
498
499
500 BT_DBG("sk %p", sk);
501
Changli Gao6503d962010-03-31 22:58:26 +0000502 if (alen < sizeof(struct sockaddr_sco) ||
503 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -EINVAL;
505
506 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
507 return -EBADFD;
508
509 if (sk->sk_type != SOCK_SEQPACKET)
510 return -EINVAL;
511
512 lock_sock(sk);
513
514 /* Set destination address and psm */
515 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
516
517 if ((err = sco_connect(sk)))
518 goto done;
519
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900520 err = bt_sock_wait_state(sk, BT_CONNECTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 sock_sndtimeo(sk, flags & O_NONBLOCK));
522
523done:
524 release_sock(sk);
525 return err;
526}
527
528static int sco_sock_listen(struct socket *sock, int backlog)
529{
530 struct sock *sk = sock->sk;
531 int err = 0;
532
533 BT_DBG("sk %p backlog %d", sk, backlog);
534
535 lock_sock(sk);
536
537 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
538 err = -EBADFD;
539 goto done;
540 }
541
542 sk->sk_max_ack_backlog = backlog;
543 sk->sk_ack_backlog = 0;
544 sk->sk_state = BT_LISTEN;
545
546done:
547 release_sock(sk);
548 return err;
549}
550
551static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
552{
553 DECLARE_WAITQUEUE(wait, current);
554 struct sock *sk = sock->sk, *ch;
555 long timeo;
556 int err = 0;
557
558 lock_sock(sk);
559
560 if (sk->sk_state != BT_LISTEN) {
561 err = -EBADFD;
562 goto done;
563 }
564
565 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
566
567 BT_DBG("sk %p timeo %ld", sk, timeo);
568
569 /* Wait for an incoming connection. (wake-one). */
570 add_wait_queue_exclusive(sk->sk_sleep, &wait);
571 while (!(ch = bt_accept_dequeue(sk, newsock))) {
572 set_current_state(TASK_INTERRUPTIBLE);
573 if (!timeo) {
574 err = -EAGAIN;
575 break;
576 }
577
578 release_sock(sk);
579 timeo = schedule_timeout(timeo);
580 lock_sock(sk);
581
582 if (sk->sk_state != BT_LISTEN) {
583 err = -EBADFD;
584 break;
585 }
586
587 if (signal_pending(current)) {
588 err = sock_intr_errno(timeo);
589 break;
590 }
591 }
592 set_current_state(TASK_RUNNING);
593 remove_wait_queue(sk->sk_sleep, &wait);
594
595 if (err)
596 goto done;
597
598 newsock->state = SS_CONNECTED;
599
600 BT_DBG("new socket %p", ch);
601
602done:
603 release_sock(sk);
604 return err;
605}
606
607static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
608{
609 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
610 struct sock *sk = sock->sk;
611
612 BT_DBG("sock %p, sk %p", sock, sk);
613
614 addr->sa_family = AF_BLUETOOTH;
615 *len = sizeof(struct sockaddr_sco);
616
617 if (peer)
618 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
619 else
620 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
621
622 return 0;
623}
624
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900625static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct msghdr *msg, size_t len)
627{
628 struct sock *sk = sock->sk;
629 int err = 0;
630
631 BT_DBG("sock %p, sk %p", sock, sk);
632
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800633 err = sock_error(sk);
634 if (err)
635 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (msg->msg_flags & MSG_OOB)
638 return -EOPNOTSUPP;
639
640 lock_sock(sk);
641
642 if (sk->sk_state == BT_CONNECTED)
643 err = sco_send_frame(sk, msg, len);
644 else
645 err = -ENOTCONN;
646
647 release_sock(sk);
648 return err;
649}
650
David S. Millerb7058842009-09-30 16:12:20 -0700651static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 struct sock *sk = sock->sk;
654 int err = 0;
655
656 BT_DBG("sk %p", sk);
657
658 lock_sock(sk);
659
660 switch (optname) {
661 default:
662 err = -ENOPROTOOPT;
663 break;
664 }
665
666 release_sock(sk);
667 return err;
668}
669
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100670static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct sock *sk = sock->sk;
673 struct sco_options opts;
674 struct sco_conninfo cinfo;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900675 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 BT_DBG("sk %p", sk);
678
679 if (get_user(len, optlen))
680 return -EFAULT;
681
682 lock_sock(sk);
683
684 switch (optname) {
685 case SCO_OPTIONS:
686 if (sk->sk_state != BT_CONNECTED) {
687 err = -ENOTCONN;
688 break;
689 }
690
691 opts.mtu = sco_pi(sk)->conn->mtu;
692
693 BT_DBG("mtu %d", opts.mtu);
694
695 len = min_t(unsigned int, len, sizeof(opts));
696 if (copy_to_user(optval, (char *)&opts, len))
697 err = -EFAULT;
698
699 break;
700
701 case SCO_CONNINFO:
702 if (sk->sk_state != BT_CONNECTED) {
703 err = -ENOTCONN;
704 break;
705 }
706
707 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
708 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
709
710 len = min_t(unsigned int, len, sizeof(cinfo));
711 if (copy_to_user(optval, (char *)&cinfo, len))
712 err = -EFAULT;
713
714 break;
715
716 default:
717 err = -ENOPROTOOPT;
718 break;
719 }
720
721 release_sock(sk);
722 return err;
723}
724
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100725static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
726{
727 struct sock *sk = sock->sk;
728 int len, err = 0;
729
730 BT_DBG("sk %p", sk);
731
732 if (level == SOL_SCO)
733 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
734
735 if (get_user(len, optlen))
736 return -EFAULT;
737
738 lock_sock(sk);
739
740 switch (optname) {
741 default:
742 err = -ENOPROTOOPT;
743 break;
744 }
745
746 release_sock(sk);
747 return err;
748}
749
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200750static int sco_sock_shutdown(struct socket *sock, int how)
751{
752 struct sock *sk = sock->sk;
753 int err = 0;
754
755 BT_DBG("sock %p, sk %p", sock, sk);
756
757 if (!sk)
758 return 0;
759
760 lock_sock(sk);
761 if (!sk->sk_shutdown) {
762 sk->sk_shutdown = SHUTDOWN_MASK;
763 sco_sock_clear_timer(sk);
764 __sco_sock_close(sk);
765
766 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
767 err = bt_sock_wait_state(sk, BT_CLOSED,
768 sk->sk_lingertime);
769 }
770 release_sock(sk);
771 return err;
772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static int sco_sock_release(struct socket *sock)
775{
776 struct sock *sk = sock->sk;
777 int err = 0;
778
779 BT_DBG("sock %p, sk %p", sock, sk);
780
781 if (!sk)
782 return 0;
783
784 sco_sock_close(sk);
785
786 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
787 lock_sock(sk);
788 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
789 release_sock(sk);
790 }
791
792 sock_orphan(sk);
793 sco_sock_kill(sk);
794 return err;
795}
796
797static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
798{
799 BT_DBG("conn %p", conn);
800
801 sco_pi(sk)->conn = conn;
802 conn->sk = sk;
803
804 if (parent)
805 bt_accept_enqueue(parent, sk);
806}
807
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900808/* Delete channel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * Must be called on the locked socket. */
810static void sco_chan_del(struct sock *sk, int err)
811{
812 struct sco_conn *conn;
813
814 conn = sco_pi(sk)->conn;
815
816 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
817
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900818 if (conn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 sco_conn_lock(conn);
820 conn->sk = NULL;
821 sco_pi(sk)->conn = NULL;
822 sco_conn_unlock(conn);
823 hci_conn_put(conn->hcon);
824 }
825
826 sk->sk_state = BT_CLOSED;
827 sk->sk_err = err;
828 sk->sk_state_change(sk);
829
830 sock_set_flag(sk, SOCK_ZAPPED);
831}
832
833static void sco_conn_ready(struct sco_conn *conn)
834{
835 struct sock *parent, *sk;
836
837 BT_DBG("conn %p", conn);
838
839 sco_conn_lock(conn);
840
841 if ((sk = conn->sk)) {
842 sco_sock_clear_timer(sk);
843 bh_lock_sock(sk);
844 sk->sk_state = BT_CONNECTED;
845 sk->sk_state_change(sk);
846 bh_unlock_sock(sk);
847 } else {
848 parent = sco_get_sock_listen(conn->src);
849 if (!parent)
850 goto done;
851
852 bh_lock_sock(parent);
853
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900854 sk = sco_sock_alloc(sock_net(parent), NULL, BTPROTO_SCO, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!sk) {
856 bh_unlock_sock(parent);
857 goto done;
858 }
859
860 sco_sock_init(sk, parent);
861
862 bacpy(&bt_sk(sk)->src, conn->src);
863 bacpy(&bt_sk(sk)->dst, conn->dst);
864
865 hci_conn_hold(conn->hcon);
866 __sco_chan_add(conn, sk, parent);
867
868 sk->sk_state = BT_CONNECTED;
869
870 /* Wake up parent */
871 parent->sk_data_ready(parent, 1);
872
873 bh_unlock_sock(parent);
874 }
875
876done:
877 sco_conn_unlock(conn);
878}
879
880/* ----- SCO interface with lower layer (HCI) ----- */
881static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
882{
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100883 register struct sock *sk;
884 struct hlist_node *node;
885 int lm = 0;
886
887 if (type != SCO_LINK && type != ESCO_LINK)
888 return 0;
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
891
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100892 /* Find listening sockets */
893 read_lock(&sco_sk_list.lock);
894 sk_for_each(sk, node, &sco_sk_list.head) {
895 if (sk->sk_state != BT_LISTEN)
896 continue;
897
898 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
899 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
900 lm |= HCI_LM_ACCEPT;
901 break;
902 }
903 }
904 read_unlock(&sco_sk_list.lock);
905
906 return lm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
910{
911 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
912
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200913 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return 0;
915
916 if (!status) {
917 struct sco_conn *conn;
918
919 conn = sco_conn_add(hcon, status);
920 if (conn)
921 sco_conn_ready(conn);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900922 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 sco_conn_del(hcon, bt_err(status));
924
925 return 0;
926}
927
Marcel Holtmann2950f212009-02-12 14:02:50 +0100928static int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 BT_DBG("hcon %p reason %d", hcon, reason);
931
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200932 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 0;
934
935 sco_conn_del(hcon, bt_err(reason));
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
938}
939
940static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
941{
942 struct sco_conn *conn = hcon->sco_data;
943
944 if (!conn)
945 goto drop;
946
947 BT_DBG("conn %p len %d", conn, skb->len);
948
949 if (skb->len) {
950 sco_recv_frame(conn, skb);
951 return 0;
952 }
953
954drop:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900955 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
957}
958
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100959static int sco_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 struct sock *sk;
962 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 read_lock_bh(&sco_sk_list.lock);
965
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800966 sk_for_each(sk, node, &sco_sk_list.head) {
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100967 seq_printf(f, "%s %s %d\n", batostr(&bt_sk(sk)->src),
968 batostr(&bt_sk(sk)->dst), sk->sk_state);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 read_unlock_bh(&sco_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800972
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100973 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100976static int sco_debugfs_open(struct inode *inode, struct file *file)
977{
978 return single_open(file, sco_debugfs_show, inode->i_private);
979}
980
981static const struct file_operations sco_debugfs_fops = {
982 .open = sco_debugfs_open,
983 .read = seq_read,
984 .llseek = seq_lseek,
985 .release = single_release,
986};
987
988static struct dentry *sco_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800990static const struct proto_ops sco_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 .family = PF_BLUETOOTH,
992 .owner = THIS_MODULE,
993 .release = sco_sock_release,
994 .bind = sco_sock_bind,
995 .connect = sco_sock_connect,
996 .listen = sco_sock_listen,
997 .accept = sco_sock_accept,
998 .getname = sco_sock_getname,
999 .sendmsg = sco_sock_sendmsg,
1000 .recvmsg = bt_sock_recvmsg,
1001 .poll = bt_sock_poll,
Marcel Holtmann3241ad82008-07-14 20:13:50 +02001002 .ioctl = bt_sock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 .mmap = sock_no_mmap,
1004 .socketpair = sock_no_socketpair,
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +02001005 .shutdown = sco_sock_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 .setsockopt = sco_sock_setsockopt,
1007 .getsockopt = sco_sock_getsockopt
1008};
1009
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001010static const struct net_proto_family sco_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 .family = PF_BLUETOOTH,
1012 .owner = THIS_MODULE,
1013 .create = sco_sock_create,
1014};
1015
1016static struct hci_proto sco_hci_proto = {
1017 .name = "SCO",
1018 .id = HCI_PROTO_SCO,
1019 .connect_ind = sco_connect_ind,
1020 .connect_cfm = sco_connect_cfm,
Marcel Holtmann2950f212009-02-12 14:02:50 +01001021 .disconn_cfm = sco_disconn_cfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 .recv_scodata = sco_recv_scodata
1023};
1024
1025static int __init sco_init(void)
1026{
1027 int err;
1028
1029 err = proto_register(&sco_proto, 0);
1030 if (err < 0)
1031 return err;
1032
1033 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1034 if (err < 0) {
1035 BT_ERR("SCO socket registration failed");
1036 goto error;
1037 }
1038
1039 err = hci_register_proto(&sco_hci_proto);
1040 if (err < 0) {
1041 BT_ERR("SCO protocol registration failed");
1042 bt_sock_unregister(BTPROTO_SCO);
1043 goto error;
1044 }
1045
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001046 if (bt_debugfs) {
1047 sco_debugfs = debugfs_create_file("sco", 0444,
1048 bt_debugfs, NULL, &sco_debugfs_fops);
1049 if (!sco_debugfs)
1050 BT_ERR("Failed to create SCO debug file");
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 BT_INFO("SCO (Voice Link) ver %s", VERSION);
1054 BT_INFO("SCO socket layer initialized");
1055
1056 return 0;
1057
1058error:
1059 proto_unregister(&sco_proto);
1060 return err;
1061}
1062
1063static void __exit sco_exit(void)
1064{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001065 debugfs_remove(sco_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1068 BT_ERR("SCO socket unregistration failed");
1069
1070 if (hci_unregister_proto(&sco_hci_proto) < 0)
1071 BT_ERR("SCO protocol unregistration failed");
1072
1073 proto_unregister(&sco_proto);
1074}
1075
1076module_init(sco_init);
1077module_exit(sco_exit);
1078
Marcel Holtmann7cb127d2008-07-14 20:13:53 +02001079module_param(disable_esco, bool, 0644);
1080MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");
1081
Marcel Holtmann63fbd242008-08-18 13:23:53 +02001082MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1084MODULE_VERSION(VERSION);
1085MODULE_LICENSE("GPL");
1086MODULE_ALIAS("bt-proto-2");