blob: 4f4eb6c2ff41145700af88a47a245c5bc3265fdb [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 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
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.
19
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.
23*/
24
25/* Bluetooth SCO sockets. */
26
27#include <linux/module.h>
28
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/debugfs.h>
42#include <linux/seq_file.h>
43#include <linux/list.h>
44#include <linux/security.h>
45#include <net/sock.h>
46
47#include <linux/uaccess.h>
48
49#include <net/bluetooth/bluetooth.h>
50#include <net/bluetooth/hci_core.h>
51#include <net/bluetooth/sco.h>
52
53static bool disable_esco;
54
55static const struct proto_ops sco_sock_ops;
56
57static struct bt_sock_list sco_sk_list = {
58 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
59};
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
97/* ---- SCO connections ---- */
98static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
99{
100 struct hci_dev *hdev = hcon->hdev;
101 struct sco_conn *conn = hcon->sco_data;
102
103 if (conn || status)
104 return conn;
105
106 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
107 if (!conn)
108 return NULL;
109
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);
124
125 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{
139 struct sco_conn *conn = hcon->sco_data;
140 struct sock *sk;
141
142 if (!conn)
143 return 0;
144
145 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
146
147 /* Kill socket */
148 sk = sco_chan_get(conn);
149 if (sk) {
150 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);
167 if (conn->sk)
168 err = -EBUSY;
169 else
170 __sco_chan_add(conn, sk, parent);
171
172 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 __u16 pkt_type = sco_pi(sk)->pkt_type;
181 struct sco_conn *conn;
182 struct hci_conn *hcon;
183 struct hci_dev *hdev;
184 int err, type;
185
186 BT_DBG("%s -> %s", batostr(src), batostr(dst));
187
188 hdev = hci_get_route(dst, src);
189 if (!hdev)
190 return -EHOSTUNREACH;
191
192 hci_dev_lock(hdev);
193
194 if (lmp_esco_capable(hdev) && !disable_esco)
195 type = ESCO_LINK;
196 else {
197 type = SCO_LINK;
198 pkt_type &= SCO_ESCO_MASK;
199 }
200
201 hcon = hci_connect(hdev, type, pkt_type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
202 if (IS_ERR(hcon)) {
203 err = PTR_ERR(hcon);
204 goto done;
205 }
206
207 conn = sco_conn_add(hcon, 0);
208 if (!conn) {
209 hci_conn_put(hcon);
210 err = -ENOMEM;
211 goto done;
212 }
213
214 /* Update source addr of the socket */
215 bacpy(src, conn->src);
216
217 err = sco_chan_add(conn, sk, NULL);
218 if (err)
219 goto done;
220
221 if (hcon->state == BT_CONNECTED) {
222 sco_sock_clear_timer(sk);
223 sk->sk_state = BT_CONNECTED;
224 } else {
225 sk->sk_state = BT_CONNECT;
226 sco_sock_set_timer(sk, sk->sk_sndtimeo);
227 }
228
229done:
230 hci_dev_unlock(hdev);
231 hci_dev_put(hdev);
232 return err;
233}
234
235static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
236{
237 struct sco_conn *conn = sco_pi(sk)->conn;
238 struct sk_buff *skb;
239 int err, count;
240
241 /* Check outgoing MTU */
242 if (len > conn->mtu)
243 return -EINVAL;
244
245 BT_DBG("sk %p len %d", sk, len);
246
247 count = min_t(unsigned int, conn->mtu, len);
248 skb = bt_skb_send_alloc(sk, count,
249 msg->msg_flags & MSG_DONTWAIT, &err);
250 if (!skb)
251 return err;
252
253 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
254 kfree_skb(skb);
255 return -EFAULT;
256 }
257
258 hci_send_sco(conn->hcon, skb);
259
260 return count;
261}
262
263static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
264{
265 struct sock *sk = sco_chan_get(conn);
266
267 if (!sk)
268 goto drop;
269
270 BT_DBG("sk %p len %d", sk, skb->len);
271
272 if (sk->sk_state != BT_CONNECTED)
273 goto drop;
274
275 if (!sock_queue_rcv_skb(sk, skb))
276 return;
277
278drop:
279 kfree_skb(skb);
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
364static void __sco_sock_close(struct sock *sk)
365{
366 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
367
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 if (sco_pi(sk)->conn) {
376 sk->sk_state = BT_DISCONN;
377 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
378 hci_conn_put(sco_pi(sk)->conn->hcon);
379 sco_pi(sk)->conn->hcon = NULL;
380 } else
381 sco_chan_del(sk, ECONNRESET);
382 break;
383
384 case BT_CONNECT2:
385 case BT_CONNECT:
386 case BT_DISCONN:
387 sco_chan_del(sk, ECONNRESET);
388 break;
389
390 default:
391 sock_set_flag(sk, SOCK_ZAPPED);
392 break;
393 }
394}
395
396/* Must be called on unlocked socket. */
397static void sco_sock_close(struct sock *sk)
398{
399 sco_sock_clear_timer(sk);
400 lock_sock(sk);
401 __sco_sock_close(sk);
402 release_sock(sk);
403 sco_sock_kill(sk);
404}
405
406static void sco_sock_init(struct sock *sk, struct sock *parent)
407{
408 BT_DBG("sk %p", sk);
409
410 if (parent) {
411 sk->sk_type = parent->sk_type;
412 security_sk_clone(parent, sk);
413 }
414}
415
416static struct proto sco_proto = {
417 .name = "SCO",
418 .owner = THIS_MODULE,
419 .obj_size = sizeof(struct sco_pinfo)
420};
421
422static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
423{
424 struct sock *sk;
425
426 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
427 if (!sk)
428 return NULL;
429
430 sock_init_data(sock, sk);
431 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
432
433 sk->sk_destruct = sco_sock_destruct;
434 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
435
436 sock_reset_flag(sk, SOCK_ZAPPED);
437
438 sk->sk_protocol = proto;
439 sk->sk_state = BT_OPEN;
440
441 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
442
443 bt_sock_link(&sco_sk_list, sk);
444 return sk;
445}
446
447static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
448 int kern)
449{
450 struct sock *sk;
451
452 BT_DBG("sock %p", sock);
453
454 sock->state = SS_UNCONNECTED;
455
456 if (sock->type != SOCK_SEQPACKET)
457 return -ESOCKTNOSUPPORT;
458
459 sock->ops = &sco_sock_ops;
460
461 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
462 if (!sk)
463 return -ENOMEM;
464
465 sco_sock_init(sk, NULL);
466 return 0;
467}
468
469static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
470{
471 struct sockaddr_sco sa;
472 struct sock *sk = sock->sk;
473 bdaddr_t *src = &sa.sco_bdaddr;
474 int len, err = 0;
475
476 BT_DBG("sk %p %s", sk, batostr(&sa.sco_bdaddr));
477
478 if (!addr || addr->sa_family != AF_BLUETOOTH)
479 return -EINVAL;
480
481 memset(&sa, 0, sizeof(sa));
482 len = min_t(unsigned int, sizeof(sa), alen);
483 memcpy(&sa, addr, len);
484
485 lock_sock(sk);
486
487 if (sk->sk_state != BT_OPEN) {
488 err = -EBADFD;
489 goto done;
490 }
491
492 write_lock(&sco_sk_list.lock);
493
494 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
495 err = -EADDRINUSE;
496 } else {
497 /* Save source address */
498 bacpy(&bt_sk(sk)->src, &sa.sco_bdaddr);
499 sco_pi(sk)->pkt_type = sa.sco_pkt_type;
500 sk->sk_state = BT_BOUND;
501 }
502
503 write_unlock(&sco_sk_list.lock);
504
505done:
506 release_sock(sk);
507 return err;
508}
509
510static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
511{
512 struct sock *sk = sock->sk;
513 struct sockaddr_sco sa;
514 int len, err = 0;
515
516 BT_DBG("sk %p", sk);
517
518 if (!addr || addr->sa_family != AF_BLUETOOTH)
519 return -EINVAL;
520
521 memset(&sa, 0, sizeof(sa));
522 len = min_t(unsigned int, sizeof(sa), alen);
523 memcpy(&sa, addr, len);
524
525 lock_sock(sk);
526
527 if (sk->sk_type != SOCK_SEQPACKET) {
528 err = -EINVAL;
529 goto done;
530 }
531
532 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
533 err = -EBADFD;
534 goto done;
535 }
536
537 /* Set destination address and psm */
538 bacpy(&bt_sk(sk)->dst, &sa.sco_bdaddr);
539 sco_pi(sk)->pkt_type = sa.sco_pkt_type;
540
541 err = sco_connect(sk);
542 if (err)
543 goto done;
544
545 err = bt_sock_wait_state(sk, BT_CONNECTED,
546 sock_sndtimeo(sk, flags & O_NONBLOCK));
547
548done:
549 release_sock(sk);
550 return err;
551}
552
553static int sco_sock_listen(struct socket *sock, int backlog)
554{
555 struct sock *sk = sock->sk;
556 int err = 0;
557
558 BT_DBG("sk %p backlog %d", sk, backlog);
559
560 lock_sock(sk);
561
562 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
563 err = -EBADFD;
564 goto done;
565 }
566
567 sk->sk_max_ack_backlog = backlog;
568 sk->sk_ack_backlog = 0;
569 sk->sk_state = BT_LISTEN;
570
571done:
572 release_sock(sk);
573 return err;
574}
575
576static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
577{
578 DECLARE_WAITQUEUE(wait, current);
579 struct sock *sk = sock->sk, *ch;
580 long timeo;
581 int err = 0;
582
583 lock_sock(sk);
584
585 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
586
587 BT_DBG("sk %p timeo %ld", sk, timeo);
588
589 /* Wait for an incoming connection. (wake-one). */
590 add_wait_queue_exclusive(sk_sleep(sk), &wait);
591 while (1) {
592 set_current_state(TASK_INTERRUPTIBLE);
593
594 if (sk->sk_state != BT_LISTEN) {
595 err = -EBADFD;
596 break;
597 }
598
599 ch = bt_accept_dequeue(sk, newsock);
600 if (ch)
601 break;
602
603 if (!timeo) {
604 err = -EAGAIN;
605 break;
606 }
607
608 if (signal_pending(current)) {
609 err = sock_intr_errno(timeo);
610 break;
611 }
612
613 release_sock(sk);
614 timeo = schedule_timeout(timeo);
615 lock_sock(sk);
616 }
617 __set_current_state(TASK_RUNNING);
618 remove_wait_queue(sk_sleep(sk), &wait);
619
620 if (err)
621 goto done;
622
623 newsock->state = SS_CONNECTED;
624
625 BT_DBG("new socket %p", ch);
626
627done:
628 release_sock(sk);
629 return err;
630}
631
632static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
633{
634 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
635 struct sock *sk = sock->sk;
636
637 BT_DBG("sock %p, sk %p", sock, sk);
638
639 addr->sa_family = AF_BLUETOOTH;
640 *len = sizeof(struct sockaddr_sco);
641
642 if (peer)
643 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
644 else
645 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
646 sa->sco_pkt_type = sco_pi(sk)->pkt_type;
647
648 return 0;
649}
650
651static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
652 struct msghdr *msg, size_t len)
653{
654 struct sock *sk = sock->sk;
655 int err;
656
657 BT_DBG("sock %p, sk %p", sock, sk);
658
659 err = sock_error(sk);
660 if (err)
661 return err;
662
663 if (msg->msg_flags & MSG_OOB)
664 return -EOPNOTSUPP;
665
666 lock_sock(sk);
667
668 if (sk->sk_state == BT_CONNECTED)
669 err = sco_send_frame(sk, msg, len);
670 else
671 err = -ENOTCONN;
672
673 release_sock(sk);
674 return err;
675}
676
677static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
678{
679 struct sock *sk = sock->sk;
680 int err = 0;
681
682 BT_DBG("sk %p", sk);
683
684 lock_sock(sk);
685
686 switch (optname) {
687 default:
688 err = -ENOPROTOOPT;
689 break;
690 }
691
692 release_sock(sk);
693 return err;
694}
695
696static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
697{
698 struct sock *sk = sock->sk;
699 struct sco_options opts;
700 struct sco_conninfo cinfo;
701 int len, err = 0;
702
703 BT_DBG("sk %p", sk);
704
705 if (get_user(len, optlen))
706 return -EFAULT;
707
708 lock_sock(sk);
709
710 switch (optname) {
711 case SCO_OPTIONS:
712 if (sk->sk_state != BT_CONNECTED) {
713 err = -ENOTCONN;
714 break;
715 }
716
717 opts.mtu = sco_pi(sk)->conn->mtu;
718
719 BT_DBG("mtu %d", opts.mtu);
720
721 len = min_t(unsigned int, len, sizeof(opts));
722 if (copy_to_user(optval, (char *)&opts, len))
723 err = -EFAULT;
724
725 break;
726
727 case SCO_CONNINFO:
728 if (sk->sk_state != BT_CONNECTED) {
729 err = -ENOTCONN;
730 break;
731 }
732
733 memset(&cinfo, 0, sizeof(cinfo));
734 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
735 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
736
737 len = min_t(unsigned int, len, sizeof(cinfo));
738 if (copy_to_user(optval, (char *)&cinfo, len))
739 err = -EFAULT;
740
741 break;
742
743 default:
744 err = -ENOPROTOOPT;
745 break;
746 }
747
748 release_sock(sk);
749 return err;
750}
751
752static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
753{
754 struct sock *sk = sock->sk;
755 int len, err = 0;
756
757 BT_DBG("sk %p", sk);
758
759 if (level == SOL_SCO)
760 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
761
762 if (get_user(len, optlen))
763 return -EFAULT;
764
765 lock_sock(sk);
766
767 switch (optname) {
768 default:
769 err = -ENOPROTOOPT;
770 break;
771 }
772
773 release_sock(sk);
774 return err;
775}
776
777static int sco_sock_shutdown(struct socket *sock, int how)
778{
779 struct sock *sk = sock->sk;
780 int err = 0;
781
782 BT_DBG("sock %p, sk %p", sock, sk);
783
784 if (!sk)
785 return 0;
786
787 lock_sock(sk);
788 if (!sk->sk_shutdown) {
789 sk->sk_shutdown = SHUTDOWN_MASK;
790 sco_sock_clear_timer(sk);
791 __sco_sock_close(sk);
792
793 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
794 err = bt_sock_wait_state(sk, BT_CLOSED,
795 sk->sk_lingertime);
796 }
797 release_sock(sk);
798 return err;
799}
800
801static int sco_sock_release(struct socket *sock)
802{
803 struct sock *sk = sock->sk;
804 int err = 0;
805
806 BT_DBG("sock %p, sk %p", sock, sk);
807
808 if (!sk)
809 return 0;
810
811 sco_sock_close(sk);
812
813 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
814 lock_sock(sk);
815 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
816 release_sock(sk);
817 }
818
819 sock_orphan(sk);
820 sco_sock_kill(sk);
821 return err;
822}
823
824static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
825{
826 BT_DBG("conn %p", conn);
827
828 sco_pi(sk)->conn = conn;
829 conn->sk = sk;
830
831 if (parent)
832 bt_accept_enqueue(parent, sk);
833}
834
835/* Delete channel.
836 * Must be called on the locked socket. */
837static void sco_chan_del(struct sock *sk, int err)
838{
839 struct sco_conn *conn;
840
841 conn = sco_pi(sk)->conn;
842
843 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
844
845 if (conn) {
846 sco_conn_lock(conn);
847 conn->sk = NULL;
848 sco_pi(sk)->conn = NULL;
849 sco_conn_unlock(conn);
850
851 if (conn->hcon)
852 hci_conn_put(conn->hcon);
853 }
854
855 sk->sk_state = BT_CLOSED;
856 sk->sk_err = err;
857 sk->sk_state_change(sk);
858
859 sock_set_flag(sk, SOCK_ZAPPED);
860}
861
862static void sco_conn_ready(struct sco_conn *conn)
863{
864 struct sock *parent;
865 struct sock *sk = conn->sk;
866
867 BT_DBG("conn %p", conn);
868
869 sco_conn_lock(conn);
870
871 if (sk) {
872 sco_sock_clear_timer(sk);
873 bh_lock_sock(sk);
874 sk->sk_state = BT_CONNECTED;
875 sk->sk_state_change(sk);
876 bh_unlock_sock(sk);
877 } else {
878 parent = sco_get_sock_listen(conn->src);
879 if (!parent)
880 goto done;
881
882 bh_lock_sock(parent);
883
884 sk = sco_sock_alloc(sock_net(parent), NULL,
885 BTPROTO_SCO, GFP_ATOMIC);
886 if (!sk) {
887 bh_unlock_sock(parent);
888 goto done;
889 }
890
891 sco_sock_init(sk, parent);
892
893 bacpy(&bt_sk(sk)->src, conn->src);
894 bacpy(&bt_sk(sk)->dst, conn->dst);
895
896 hci_conn_hold(conn->hcon);
897 __sco_chan_add(conn, sk, parent);
898
899 sk->sk_state = BT_CONNECTED;
900
901 /* Wake up parent */
902 parent->sk_data_ready(parent, 1);
903
904 bh_unlock_sock(parent);
905 }
906
907done:
908 sco_conn_unlock(conn);
909}
910
911/* ----- SCO interface with lower layer (HCI) ----- */
912int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
913{
914 register struct sock *sk;
915 struct hlist_node *node;
916 int lm = 0;
917
918 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
919
920 /* Find listening sockets */
921 read_lock(&sco_sk_list.lock);
922 sk_for_each(sk, node, &sco_sk_list.head) {
923 if (sk->sk_state != BT_LISTEN)
924 continue;
925
926 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
927 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
928 lm |= HCI_LM_ACCEPT;
929 break;
930 }
931 }
932 read_unlock(&sco_sk_list.lock);
933
934 return lm;
935}
936
937int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
938{
939 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
940 if (!status) {
941 struct sco_conn *conn;
942
943 conn = sco_conn_add(hcon, status);
944 if (conn)
945 sco_conn_ready(conn);
946 } else
947 sco_conn_del(hcon, bt_to_errno(status));
948
949 return 0;
950}
951
952int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
953{
954 BT_DBG("hcon %p reason %d", hcon, reason);
955
956 sco_conn_del(hcon, bt_to_errno(reason));
957 return 0;
958}
959
960int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
961{
962 struct sco_conn *conn = hcon->sco_data;
963
964 if (!conn)
965 goto drop;
966
967 BT_DBG("conn %p len %d", conn, skb->len);
968
969 if (skb->len) {
970 sco_recv_frame(conn, skb);
971 return 0;
972 }
973
974drop:
975 kfree_skb(skb);
976 return 0;
977}
978
979static int sco_debugfs_show(struct seq_file *f, void *p)
980{
981 struct sock *sk;
982 struct hlist_node *node;
983
984 read_lock(&sco_sk_list.lock);
985
986 sk_for_each(sk, node, &sco_sk_list.head) {
987 seq_printf(f, "%s %s %d\n", batostr(&bt_sk(sk)->src),
988 batostr(&bt_sk(sk)->dst), sk->sk_state);
989 }
990
991 read_unlock(&sco_sk_list.lock);
992
993 return 0;
994}
995
996static int sco_debugfs_open(struct inode *inode, struct file *file)
997{
998 return single_open(file, sco_debugfs_show, inode->i_private);
999}
1000
1001static const struct file_operations sco_debugfs_fops = {
1002 .open = sco_debugfs_open,
1003 .read = seq_read,
1004 .llseek = seq_lseek,
1005 .release = single_release,
1006};
1007
1008static struct dentry *sco_debugfs;
1009
1010static const struct proto_ops sco_sock_ops = {
1011 .family = PF_BLUETOOTH,
1012 .owner = THIS_MODULE,
1013 .release = sco_sock_release,
1014 .bind = sco_sock_bind,
1015 .connect = sco_sock_connect,
1016 .listen = sco_sock_listen,
1017 .accept = sco_sock_accept,
1018 .getname = sco_sock_getname,
1019 .sendmsg = sco_sock_sendmsg,
1020 .recvmsg = bt_sock_recvmsg,
1021 .poll = bt_sock_poll,
1022 .ioctl = bt_sock_ioctl,
1023 .mmap = sock_no_mmap,
1024 .socketpair = sock_no_socketpair,
1025 .shutdown = sco_sock_shutdown,
1026 .setsockopt = sco_sock_setsockopt,
1027 .getsockopt = sco_sock_getsockopt
1028};
1029
1030static const struct net_proto_family sco_sock_family_ops = {
1031 .family = PF_BLUETOOTH,
1032 .owner = THIS_MODULE,
1033 .create = sco_sock_create,
1034};
1035
1036int __init sco_init(void)
1037{
1038 int err;
1039
1040 err = proto_register(&sco_proto, 0);
1041 if (err < 0)
1042 return err;
1043
1044 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1045 if (err < 0) {
1046 BT_ERR("SCO socket registration failed");
1047 goto error;
1048 }
1049
1050 if (bt_debugfs) {
1051 sco_debugfs = debugfs_create_file("sco", 0444,
1052 bt_debugfs, NULL, &sco_debugfs_fops);
1053 if (!sco_debugfs)
1054 BT_ERR("Failed to create SCO debug file");
1055 }
1056
1057 BT_INFO("SCO socket layer initialized");
1058
1059 return 0;
1060
1061error:
1062 proto_unregister(&sco_proto);
1063 return err;
1064}
1065
1066void __exit sco_exit(void)
1067{
1068 debugfs_remove(sco_debugfs);
1069
1070 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1071 BT_ERR("SCO socket unregistration failed");
1072
1073 proto_unregister(&sco_proto);
1074}
1075
1076module_param(disable_esco, bool, 0644);
1077MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");