Bluetooth: sco: fix information leak to userspace
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / bluetooth / sco.c
blobdc009794118dec6b2352118510305a1e27f72b22
1 /*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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;
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
15 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
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22 SOFTWARE IS DISCLAIMED.
25 /* Bluetooth SCO sockets. */
27 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/errno.h>
31 #include <linux/kernel.h>
32 #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>
40 #include <linux/device.h>
41 #include <linux/list.h>
42 #include <net/sock.h>
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
47 #include <net/bluetooth/bluetooth.h>
48 #include <net/bluetooth/hci_core.h>
49 #include <net/bluetooth/sco.h>
51 #define VERSION "0.6"
53 static int disable_esco = 0;
55 static const struct proto_ops sco_sock_ops;
57 static struct bt_sock_list sco_sk_list = {
58 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
61 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
62 static void sco_chan_del(struct sock *sk, int err);
64 static int sco_conn_del(struct hci_conn *conn, int err);
66 static void sco_sock_close(struct sock *sk);
67 static void sco_sock_kill(struct sock *sk);
69 /* ---- SCO timers ---- */
70 static void sco_sock_timeout(unsigned long arg)
72 struct sock *sk = (struct sock *) arg;
74 BT_DBG("sock %p state %d", sk, sk->sk_state);
76 bh_lock_sock(sk);
77 sk->sk_err = ETIMEDOUT;
78 sk->sk_state_change(sk);
79 bh_unlock_sock(sk);
81 sco_sock_kill(sk);
82 sock_put(sk);
85 static void sco_sock_set_timer(struct sock *sk, long timeout)
87 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
88 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
91 static void sco_sock_clear_timer(struct sock *sk)
93 BT_DBG("sock %p state %d", sk, sk->sk_state);
94 sk_stop_timer(sk, &sk->sk_timer);
97 /* ---- SCO connections ---- */
98 static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
100 struct hci_dev *hdev = hcon->hdev;
101 struct sco_conn *conn = hcon->sco_data;
103 if (conn || status)
104 return conn;
106 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
107 if (!conn)
108 return NULL;
110 spin_lock_init(&conn->lock);
112 hcon->sco_data = conn;
113 conn->hcon = hcon;
115 conn->src = &hdev->bdaddr;
116 conn->dst = &hcon->dst;
118 if (hdev->sco_mtu > 0)
119 conn->mtu = hdev->sco_mtu;
120 else
121 conn->mtu = 60;
123 BT_DBG("hcon %p conn %p", hcon, conn);
125 return conn;
128 static inline struct sock *sco_chan_get(struct sco_conn *conn)
130 struct sock *sk = NULL;
131 sco_conn_lock(conn);
132 sk = conn->sk;
133 sco_conn_unlock(conn);
134 return sk;
137 static int sco_conn_del(struct hci_conn *hcon, int err)
139 struct sco_conn *conn;
140 struct sock *sk;
142 if (!(conn = hcon->sco_data))
143 return 0;
145 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
147 /* Kill socket */
148 if ((sk = sco_chan_get(conn))) {
149 bh_lock_sock(sk);
150 sco_sock_clear_timer(sk);
151 sco_chan_del(sk, err);
152 bh_unlock_sock(sk);
153 sco_sock_kill(sk);
156 hcon->sco_data = NULL;
157 kfree(conn);
158 return 0;
161 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
163 int err = 0;
165 sco_conn_lock(conn);
166 if (conn->sk) {
167 err = -EBUSY;
168 } else {
169 __sco_chan_add(conn, sk, parent);
171 sco_conn_unlock(conn);
172 return err;
175 static int sco_connect(struct sock *sk)
177 bdaddr_t *src = &bt_sk(sk)->src;
178 bdaddr_t *dst = &bt_sk(sk)->dst;
179 struct sco_conn *conn;
180 struct hci_conn *hcon;
181 struct hci_dev *hdev;
182 int err, type;
184 BT_DBG("%s -> %s", batostr(src), batostr(dst));
186 if (!(hdev = hci_get_route(dst, src)))
187 return -EHOSTUNREACH;
189 hci_dev_lock_bh(hdev);
191 err = -ENOMEM;
193 if (lmp_esco_capable(hdev) && !disable_esco)
194 type = ESCO_LINK;
195 else
196 type = SCO_LINK;
198 hcon = hci_connect(hdev, type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
199 if (!hcon)
200 goto done;
202 conn = sco_conn_add(hcon, 0);
203 if (!conn) {
204 hci_conn_put(hcon);
205 goto done;
208 /* Update source addr of the socket */
209 bacpy(src, conn->src);
211 err = sco_chan_add(conn, sk, NULL);
212 if (err)
213 goto done;
215 if (hcon->state == BT_CONNECTED) {
216 sco_sock_clear_timer(sk);
217 sk->sk_state = BT_CONNECTED;
218 } else {
219 sk->sk_state = BT_CONNECT;
220 sco_sock_set_timer(sk, sk->sk_sndtimeo);
223 done:
224 hci_dev_unlock_bh(hdev);
225 hci_dev_put(hdev);
226 return err;
229 static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
231 struct sco_conn *conn = sco_pi(sk)->conn;
232 struct sk_buff *skb;
233 int err, count;
235 /* Check outgoing MTU */
236 if (len > conn->mtu)
237 return -EINVAL;
239 BT_DBG("sk %p len %d", sk, len);
241 count = min_t(unsigned int, conn->mtu, len);
242 if (!(skb = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err)))
243 return err;
245 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
246 err = -EFAULT;
247 goto fail;
250 if ((err = hci_send_sco(conn->hcon, skb)) < 0)
251 return err;
253 return count;
255 fail:
256 kfree_skb(skb);
257 return err;
260 static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
262 struct sock *sk = sco_chan_get(conn);
264 if (!sk)
265 goto drop;
267 BT_DBG("sk %p len %d", sk, skb->len);
269 if (sk->sk_state != BT_CONNECTED)
270 goto drop;
272 if (!sock_queue_rcv_skb(sk, skb))
273 return;
275 drop:
276 kfree_skb(skb);
277 return;
280 /* -------- Socket interface ---------- */
281 static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
283 struct sock *sk;
284 struct hlist_node *node;
286 sk_for_each(sk, node, &sco_sk_list.head)
287 if (!bacmp(&bt_sk(sk)->src, ba))
288 goto found;
289 sk = NULL;
290 found:
291 return sk;
294 /* Find socket listening on source bdaddr.
295 * Returns closest match.
297 static struct sock *sco_get_sock_listen(bdaddr_t *src)
299 struct sock *sk = NULL, *sk1 = NULL;
300 struct hlist_node *node;
302 read_lock(&sco_sk_list.lock);
304 sk_for_each(sk, node, &sco_sk_list.head) {
305 if (sk->sk_state != BT_LISTEN)
306 continue;
308 /* Exact match. */
309 if (!bacmp(&bt_sk(sk)->src, src))
310 break;
312 /* Closest match */
313 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
314 sk1 = sk;
317 read_unlock(&sco_sk_list.lock);
319 return node ? sk : sk1;
322 static void sco_sock_destruct(struct sock *sk)
324 BT_DBG("sk %p", sk);
326 skb_queue_purge(&sk->sk_receive_queue);
327 skb_queue_purge(&sk->sk_write_queue);
330 static void sco_sock_cleanup_listen(struct sock *parent)
332 struct sock *sk;
334 BT_DBG("parent %p", parent);
336 /* Close not yet accepted channels */
337 while ((sk = bt_accept_dequeue(parent, NULL))) {
338 sco_sock_close(sk);
339 sco_sock_kill(sk);
342 parent->sk_state = BT_CLOSED;
343 sock_set_flag(parent, SOCK_ZAPPED);
346 /* Kill socket (only if zapped and orphan)
347 * Must be called on unlocked socket.
349 static void sco_sock_kill(struct sock *sk)
351 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
352 return;
354 BT_DBG("sk %p state %d", sk, sk->sk_state);
356 /* Kill poor orphan */
357 bt_sock_unlink(&sco_sk_list, sk);
358 sock_set_flag(sk, SOCK_DEAD);
359 sock_put(sk);
362 static void __sco_sock_close(struct sock *sk)
364 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
366 switch (sk->sk_state) {
367 case BT_LISTEN:
368 sco_sock_cleanup_listen(sk);
369 break;
371 case BT_CONNECTED:
372 case BT_CONFIG:
373 case BT_CONNECT:
374 case BT_DISCONN:
375 sco_chan_del(sk, ECONNRESET);
376 break;
378 default:
379 sock_set_flag(sk, SOCK_ZAPPED);
380 break;
384 /* Must be called on unlocked socket. */
385 static void sco_sock_close(struct sock *sk)
387 sco_sock_clear_timer(sk);
388 lock_sock(sk);
389 __sco_sock_close(sk);
390 release_sock(sk);
391 sco_sock_kill(sk);
394 static void sco_sock_init(struct sock *sk, struct sock *parent)
396 BT_DBG("sk %p", sk);
398 if (parent)
399 sk->sk_type = parent->sk_type;
402 static struct proto sco_proto = {
403 .name = "SCO",
404 .owner = THIS_MODULE,
405 .obj_size = sizeof(struct sco_pinfo)
408 static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
410 struct sock *sk;
412 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
413 if (!sk)
414 return NULL;
416 sock_init_data(sock, sk);
417 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
419 sk->sk_destruct = sco_sock_destruct;
420 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
422 sock_reset_flag(sk, SOCK_ZAPPED);
424 sk->sk_protocol = proto;
425 sk->sk_state = BT_OPEN;
427 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
429 bt_sock_link(&sco_sk_list, sk);
430 return sk;
433 static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
434 int kern)
436 struct sock *sk;
438 BT_DBG("sock %p", sock);
440 sock->state = SS_UNCONNECTED;
442 if (sock->type != SOCK_SEQPACKET)
443 return -ESOCKTNOSUPPORT;
445 sock->ops = &sco_sock_ops;
447 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
448 if (!sk)
449 return -ENOMEM;
451 sco_sock_init(sk, NULL);
452 return 0;
455 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
457 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
458 struct sock *sk = sock->sk;
459 bdaddr_t *src = &sa->sco_bdaddr;
460 int err = 0;
462 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
464 if (!addr || addr->sa_family != AF_BLUETOOTH)
465 return -EINVAL;
467 lock_sock(sk);
469 if (sk->sk_state != BT_OPEN) {
470 err = -EBADFD;
471 goto done;
474 write_lock_bh(&sco_sk_list.lock);
476 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
477 err = -EADDRINUSE;
478 } else {
479 /* Save source address */
480 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
481 sk->sk_state = BT_BOUND;
484 write_unlock_bh(&sco_sk_list.lock);
486 done:
487 release_sock(sk);
488 return err;
491 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
493 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
494 struct sock *sk = sock->sk;
495 int err = 0;
498 BT_DBG("sk %p", sk);
500 if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_sco))
501 return -EINVAL;
503 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
504 return -EBADFD;
506 if (sk->sk_type != SOCK_SEQPACKET)
507 return -EINVAL;
509 lock_sock(sk);
511 /* Set destination address and psm */
512 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
514 if ((err = sco_connect(sk)))
515 goto done;
517 err = bt_sock_wait_state(sk, BT_CONNECTED,
518 sock_sndtimeo(sk, flags & O_NONBLOCK));
520 done:
521 release_sock(sk);
522 return err;
525 static int sco_sock_listen(struct socket *sock, int backlog)
527 struct sock *sk = sock->sk;
528 int err = 0;
530 BT_DBG("sk %p backlog %d", sk, backlog);
532 lock_sock(sk);
534 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
535 err = -EBADFD;
536 goto done;
539 sk->sk_max_ack_backlog = backlog;
540 sk->sk_ack_backlog = 0;
541 sk->sk_state = BT_LISTEN;
543 done:
544 release_sock(sk);
545 return err;
548 static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
550 DECLARE_WAITQUEUE(wait, current);
551 struct sock *sk = sock->sk, *ch;
552 long timeo;
553 int err = 0;
555 lock_sock(sk);
557 if (sk->sk_state != BT_LISTEN) {
558 err = -EBADFD;
559 goto done;
562 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
564 BT_DBG("sk %p timeo %ld", sk, timeo);
566 /* Wait for an incoming connection. (wake-one). */
567 add_wait_queue_exclusive(sk->sk_sleep, &wait);
568 while (!(ch = bt_accept_dequeue(sk, newsock))) {
569 set_current_state(TASK_INTERRUPTIBLE);
570 if (!timeo) {
571 err = -EAGAIN;
572 break;
575 release_sock(sk);
576 timeo = schedule_timeout(timeo);
577 lock_sock(sk);
579 if (sk->sk_state != BT_LISTEN) {
580 err = -EBADFD;
581 break;
584 if (signal_pending(current)) {
585 err = sock_intr_errno(timeo);
586 break;
589 set_current_state(TASK_RUNNING);
590 remove_wait_queue(sk->sk_sleep, &wait);
592 if (err)
593 goto done;
595 newsock->state = SS_CONNECTED;
597 BT_DBG("new socket %p", ch);
599 done:
600 release_sock(sk);
601 return err;
604 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
606 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
607 struct sock *sk = sock->sk;
609 BT_DBG("sock %p, sk %p", sock, sk);
611 addr->sa_family = AF_BLUETOOTH;
612 *len = sizeof(struct sockaddr_sco);
614 if (peer)
615 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
616 else
617 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
619 return 0;
622 static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
623 struct msghdr *msg, size_t len)
625 struct sock *sk = sock->sk;
626 int err = 0;
628 BT_DBG("sock %p, sk %p", sock, sk);
630 err = sock_error(sk);
631 if (err)
632 return err;
634 if (msg->msg_flags & MSG_OOB)
635 return -EOPNOTSUPP;
637 lock_sock(sk);
639 if (sk->sk_state == BT_CONNECTED)
640 err = sco_send_frame(sk, msg, len);
641 else
642 err = -ENOTCONN;
644 release_sock(sk);
645 return err;
648 static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
650 struct sock *sk = sock->sk;
651 int err = 0;
653 BT_DBG("sk %p", sk);
655 lock_sock(sk);
657 switch (optname) {
658 default:
659 err = -ENOPROTOOPT;
660 break;
663 release_sock(sk);
664 return err;
667 static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
669 struct sock *sk = sock->sk;
670 struct sco_options opts;
671 struct sco_conninfo cinfo;
672 int len, err = 0;
674 BT_DBG("sk %p", sk);
676 if (get_user(len, optlen))
677 return -EFAULT;
679 lock_sock(sk);
681 switch (optname) {
682 case SCO_OPTIONS:
683 if (sk->sk_state != BT_CONNECTED) {
684 err = -ENOTCONN;
685 break;
688 opts.mtu = sco_pi(sk)->conn->mtu;
690 BT_DBG("mtu %d", opts.mtu);
692 len = min_t(unsigned int, len, sizeof(opts));
693 if (copy_to_user(optval, (char *)&opts, len))
694 err = -EFAULT;
696 break;
698 case SCO_CONNINFO:
699 if (sk->sk_state != BT_CONNECTED) {
700 err = -ENOTCONN;
701 break;
704 memset(&cinfo, 0, sizeof(cinfo));
705 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
706 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
708 len = min_t(unsigned int, len, sizeof(cinfo));
709 if (copy_to_user(optval, (char *)&cinfo, len))
710 err = -EFAULT;
712 break;
714 default:
715 err = -ENOPROTOOPT;
716 break;
719 release_sock(sk);
720 return err;
723 static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
725 struct sock *sk = sock->sk;
726 int len, err = 0;
728 BT_DBG("sk %p", sk);
730 if (level == SOL_SCO)
731 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
733 if (get_user(len, optlen))
734 return -EFAULT;
736 lock_sock(sk);
738 switch (optname) {
739 default:
740 err = -ENOPROTOOPT;
741 break;
744 release_sock(sk);
745 return err;
748 static int sco_sock_shutdown(struct socket *sock, int how)
750 struct sock *sk = sock->sk;
751 int err = 0;
753 BT_DBG("sock %p, sk %p", sock, sk);
755 if (!sk)
756 return 0;
758 lock_sock(sk);
759 if (!sk->sk_shutdown) {
760 sk->sk_shutdown = SHUTDOWN_MASK;
761 sco_sock_clear_timer(sk);
762 __sco_sock_close(sk);
764 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
765 err = bt_sock_wait_state(sk, BT_CLOSED,
766 sk->sk_lingertime);
768 release_sock(sk);
769 return err;
772 static int sco_sock_release(struct socket *sock)
774 struct sock *sk = sock->sk;
775 int err = 0;
777 BT_DBG("sock %p, sk %p", sock, sk);
779 if (!sk)
780 return 0;
782 sco_sock_close(sk);
784 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
785 lock_sock(sk);
786 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
787 release_sock(sk);
790 sock_orphan(sk);
791 sco_sock_kill(sk);
792 return err;
795 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
797 BT_DBG("conn %p", conn);
799 sco_pi(sk)->conn = conn;
800 conn->sk = sk;
802 if (parent)
803 bt_accept_enqueue(parent, sk);
806 /* Delete channel.
807 * Must be called on the locked socket. */
808 static void sco_chan_del(struct sock *sk, int err)
810 struct sco_conn *conn;
812 conn = sco_pi(sk)->conn;
814 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
816 if (conn) {
817 sco_conn_lock(conn);
818 conn->sk = NULL;
819 sco_pi(sk)->conn = NULL;
820 sco_conn_unlock(conn);
821 hci_conn_put(conn->hcon);
824 sk->sk_state = BT_CLOSED;
825 sk->sk_err = err;
826 sk->sk_state_change(sk);
828 sock_set_flag(sk, SOCK_ZAPPED);
831 static void sco_conn_ready(struct sco_conn *conn)
833 struct sock *parent, *sk;
835 BT_DBG("conn %p", conn);
837 sco_conn_lock(conn);
839 if ((sk = conn->sk)) {
840 sco_sock_clear_timer(sk);
841 bh_lock_sock(sk);
842 sk->sk_state = BT_CONNECTED;
843 sk->sk_state_change(sk);
844 bh_unlock_sock(sk);
845 } else {
846 parent = sco_get_sock_listen(conn->src);
847 if (!parent)
848 goto done;
850 bh_lock_sock(parent);
852 sk = sco_sock_alloc(sock_net(parent), NULL, BTPROTO_SCO, GFP_ATOMIC);
853 if (!sk) {
854 bh_unlock_sock(parent);
855 goto done;
858 sco_sock_init(sk, parent);
860 bacpy(&bt_sk(sk)->src, conn->src);
861 bacpy(&bt_sk(sk)->dst, conn->dst);
863 hci_conn_hold(conn->hcon);
864 __sco_chan_add(conn, sk, parent);
866 sk->sk_state = BT_CONNECTED;
868 /* Wake up parent */
869 parent->sk_data_ready(parent, 1);
871 bh_unlock_sock(parent);
874 done:
875 sco_conn_unlock(conn);
878 /* ----- SCO interface with lower layer (HCI) ----- */
879 static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
881 register struct sock *sk;
882 struct hlist_node *node;
883 int lm = 0;
885 if (type != SCO_LINK && type != ESCO_LINK)
886 return 0;
888 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
890 /* Find listening sockets */
891 read_lock(&sco_sk_list.lock);
892 sk_for_each(sk, node, &sco_sk_list.head) {
893 if (sk->sk_state != BT_LISTEN)
894 continue;
896 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
897 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
898 lm |= HCI_LM_ACCEPT;
899 break;
902 read_unlock(&sco_sk_list.lock);
904 return lm;
907 static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
909 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
911 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
912 return 0;
914 if (!status) {
915 struct sco_conn *conn;
917 conn = sco_conn_add(hcon, status);
918 if (conn)
919 sco_conn_ready(conn);
920 } else
921 sco_conn_del(hcon, bt_err(status));
923 return 0;
926 static int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
928 BT_DBG("hcon %p reason %d", hcon, reason);
930 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
931 return 0;
933 sco_conn_del(hcon, bt_err(reason));
935 return 0;
938 static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
940 struct sco_conn *conn = hcon->sco_data;
942 if (!conn)
943 goto drop;
945 BT_DBG("conn %p len %d", conn, skb->len);
947 if (skb->len) {
948 sco_recv_frame(conn, skb);
949 return 0;
952 drop:
953 kfree_skb(skb);
954 return 0;
957 static ssize_t sco_sysfs_show(struct class *dev, char *buf)
959 struct sock *sk;
960 struct hlist_node *node;
961 char *str = buf;
962 int size = PAGE_SIZE;
964 read_lock_bh(&sco_sk_list.lock);
966 sk_for_each(sk, node, &sco_sk_list.head) {
967 int len;
969 len = snprintf(str, size, "%s %s %d\n",
970 batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
971 sk->sk_state);
973 size -= len;
974 if (size <= 0)
975 break;
977 str += len;
980 read_unlock_bh(&sco_sk_list.lock);
982 return (str - buf);
985 static CLASS_ATTR(sco, S_IRUGO, sco_sysfs_show, NULL);
987 static const struct proto_ops sco_sock_ops = {
988 .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,
999 .ioctl = bt_sock_ioctl,
1000 .mmap = sock_no_mmap,
1001 .socketpair = sock_no_socketpair,
1002 .shutdown = sco_sock_shutdown,
1003 .setsockopt = sco_sock_setsockopt,
1004 .getsockopt = sco_sock_getsockopt
1007 static const struct net_proto_family sco_sock_family_ops = {
1008 .family = PF_BLUETOOTH,
1009 .owner = THIS_MODULE,
1010 .create = sco_sock_create,
1013 static 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,
1018 .disconn_cfm = sco_disconn_cfm,
1019 .recv_scodata = sco_recv_scodata
1022 static int __init sco_init(void)
1024 int err;
1026 err = proto_register(&sco_proto, 0);
1027 if (err < 0)
1028 return err;
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;
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;
1043 if (class_create_file(bt_class, &class_attr_sco) < 0)
1044 BT_ERR("Failed to create SCO info file");
1046 BT_INFO("SCO (Voice Link) ver %s", VERSION);
1047 BT_INFO("SCO socket layer initialized");
1049 return 0;
1051 error:
1052 proto_unregister(&sco_proto);
1053 return err;
1056 static void __exit sco_exit(void)
1058 class_remove_file(bt_class, &class_attr_sco);
1060 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1061 BT_ERR("SCO socket unregistration failed");
1063 if (hci_unregister_proto(&sco_hci_proto) < 0)
1064 BT_ERR("SCO protocol unregistration failed");
1066 proto_unregister(&sco_proto);
1069 module_init(sco_init);
1070 module_exit(sco_exit);
1072 module_param(disable_esco, bool, 0644);
1073 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");
1075 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1076 MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1077 MODULE_VERSION(VERSION);
1078 MODULE_LICENSE("GPL");
1079 MODULE_ALIAS("bt-proto-2");