~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/bluetooth/sco.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  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 #include <linux/debugfs.h>
 29 #include <linux/seq_file.h>
 30 
 31 #include <net/bluetooth/bluetooth.h>
 32 #include <net/bluetooth/hci_core.h>
 33 #include <net/bluetooth/sco.h>
 34 
 35 static bool disable_esco;
 36 
 37 static const struct proto_ops sco_sock_ops;
 38 
 39 static struct bt_sock_list sco_sk_list = {
 40         .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
 41 };
 42 
 43 /* ---- SCO connections ---- */
 44 struct sco_conn {
 45         struct hci_conn *hcon;
 46 
 47         spinlock_t      lock;
 48         struct sock     *sk;
 49 
 50         unsigned int    mtu;
 51 };
 52 
 53 #define sco_conn_lock(c)        spin_lock(&c->lock);
 54 #define sco_conn_unlock(c)      spin_unlock(&c->lock);
 55 
 56 static void sco_sock_close(struct sock *sk);
 57 static void sco_sock_kill(struct sock *sk);
 58 
 59 /* ----- SCO socket info ----- */
 60 #define sco_pi(sk) ((struct sco_pinfo *) sk)
 61 
 62 struct sco_pinfo {
 63         struct bt_sock  bt;
 64         bdaddr_t        src;
 65         bdaddr_t        dst;
 66         __u32           flags;
 67         __u16           setting;
 68         struct sco_conn *conn;
 69 };
 70 
 71 /* ---- SCO timers ---- */
 72 #define SCO_CONN_TIMEOUT        (HZ * 40)
 73 #define SCO_DISCONN_TIMEOUT     (HZ * 2)
 74 
 75 static void sco_sock_timeout(unsigned long arg)
 76 {
 77         struct sock *sk = (struct sock *) arg;
 78 
 79         BT_DBG("sock %p state %d", sk, sk->sk_state);
 80 
 81         bh_lock_sock(sk);
 82         sk->sk_err = ETIMEDOUT;
 83         sk->sk_state_change(sk);
 84         bh_unlock_sock(sk);
 85 
 86         sco_sock_kill(sk);
 87         sock_put(sk);
 88 }
 89 
 90 static void sco_sock_set_timer(struct sock *sk, long timeout)
 91 {
 92         BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
 93         sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
 94 }
 95 
 96 static void sco_sock_clear_timer(struct sock *sk)
 97 {
 98         BT_DBG("sock %p state %d", sk, sk->sk_state);
 99         sk_stop_timer(sk, &sk->sk_timer);
100 }
101 
102 /* ---- SCO connections ---- */
103 static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
104 {
105         struct hci_dev *hdev = hcon->hdev;
106         struct sco_conn *conn = hcon->sco_data;
107 
108         if (conn)
109                 return conn;
110 
111         conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL);
112         if (!conn)
113                 return NULL;
114 
115         spin_lock_init(&conn->lock);
116 
117         hcon->sco_data = conn;
118         conn->hcon = hcon;
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);
126 
127         return conn;
128 }
129 
130 /* Delete channel.
131  * Must be called on the locked socket. */
132 static void sco_chan_del(struct sock *sk, int err)
133 {
134         struct sco_conn *conn;
135 
136         conn = sco_pi(sk)->conn;
137 
138         BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
139 
140         if (conn) {
141                 sco_conn_lock(conn);
142                 conn->sk = NULL;
143                 sco_pi(sk)->conn = NULL;
144                 sco_conn_unlock(conn);
145 
146                 if (conn->hcon)
147                         hci_conn_drop(conn->hcon);
148         }
149 
150         sk->sk_state = BT_CLOSED;
151         sk->sk_err   = err;
152         sk->sk_state_change(sk);
153 
154         sock_set_flag(sk, SOCK_ZAPPED);
155 }
156 
157 static void sco_conn_del(struct hci_conn *hcon, int err)
158 {
159         struct sco_conn *conn = hcon->sco_data;
160         struct sock *sk;
161 
162         if (!conn)
163                 return;
164 
165         BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
166 
167         /* Kill socket */
168         sco_conn_lock(conn);
169         sk = conn->sk;
170         sco_conn_unlock(conn);
171 
172         if (sk) {
173                 bh_lock_sock(sk);
174                 sco_sock_clear_timer(sk);
175                 sco_chan_del(sk, err);
176                 bh_unlock_sock(sk);
177                 sco_sock_kill(sk);
178         }
179 
180         hcon->sco_data = NULL;
181         kfree(conn);
182 }
183 
184 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
185 {
186         BT_DBG("conn %p", conn);
187 
188         sco_pi(sk)->conn = conn;
189         conn->sk = sk;
190 
191         if (parent)
192                 bt_accept_enqueue(parent, sk);
193 }
194 
195 static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
196                         struct sock *parent)
197 {
198         int err = 0;
199 
200         sco_conn_lock(conn);
201         if (conn->sk)
202                 err = -EBUSY;
203         else
204                 __sco_chan_add(conn, sk, parent);
205 
206         sco_conn_unlock(conn);
207         return err;
208 }
209 
210 static int sco_connect(struct sock *sk)
211 {
212         struct sco_conn *conn;
213         struct hci_conn *hcon;
214         struct hci_dev  *hdev;
215         int err, type;
216 
217         BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
218 
219         hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src);
220         if (!hdev)
221                 return -EHOSTUNREACH;
222 
223         hci_dev_lock(hdev);
224 
225         if (lmp_esco_capable(hdev) && !disable_esco)
226                 type = ESCO_LINK;
227         else
228                 type = SCO_LINK;
229 
230         if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
231             (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev))) {
232                 err = -EOPNOTSUPP;
233                 goto done;
234         }
235 
236         hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
237                                sco_pi(sk)->setting);
238         if (IS_ERR(hcon)) {
239                 err = PTR_ERR(hcon);
240                 goto done;
241         }
242 
243         conn = sco_conn_add(hcon);
244         if (!conn) {
245                 hci_conn_drop(hcon);
246                 err = -ENOMEM;
247                 goto done;
248         }
249 
250         /* Update source addr of the socket */
251         bacpy(&sco_pi(sk)->src, &hcon->src);
252 
253         err = sco_chan_add(conn, sk, NULL);
254         if (err)
255                 goto done;
256 
257         if (hcon->state == BT_CONNECTED) {
258                 sco_sock_clear_timer(sk);
259                 sk->sk_state = BT_CONNECTED;
260         } else {
261                 sk->sk_state = BT_CONNECT;
262                 sco_sock_set_timer(sk, sk->sk_sndtimeo);
263         }
264 
265 done:
266         hci_dev_unlock(hdev);
267         hci_dev_put(hdev);
268         return err;
269 }
270 
271 static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
272 {
273         struct sco_conn *conn = sco_pi(sk)->conn;
274         struct sk_buff *skb;
275         int err;
276 
277         /* Check outgoing MTU */
278         if (len > conn->mtu)
279                 return -EINVAL;
280 
281         BT_DBG("sk %p len %d", sk, len);
282 
283         skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
284         if (!skb)
285                 return err;
286 
287         if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
288                 kfree_skb(skb);
289                 return -EFAULT;
290         }
291 
292         hci_send_sco(conn->hcon, skb);
293 
294         return len;
295 }
296 
297 static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
298 {
299         struct sock *sk;
300 
301         sco_conn_lock(conn);
302         sk = conn->sk;
303         sco_conn_unlock(conn);
304 
305         if (!sk)
306                 goto drop;
307 
308         BT_DBG("sk %p len %d", sk, skb->len);
309 
310         if (sk->sk_state != BT_CONNECTED)
311                 goto drop;
312 
313         if (!sock_queue_rcv_skb(sk, skb))
314                 return;
315 
316 drop:
317         kfree_skb(skb);
318 }
319 
320 /* -------- Socket interface ---------- */
321 static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
322 {
323         struct sock *sk;
324 
325         sk_for_each(sk, &sco_sk_list.head) {
326                 if (sk->sk_state != BT_LISTEN)
327                         continue;
328 
329                 if (!bacmp(&sco_pi(sk)->src, ba))
330                         return sk;
331         }
332 
333         return NULL;
334 }
335 
336 /* Find socket listening on source bdaddr.
337  * Returns closest match.
338  */
339 static struct sock *sco_get_sock_listen(bdaddr_t *src)
340 {
341         struct sock *sk = NULL, *sk1 = NULL;
342 
343         read_lock(&sco_sk_list.lock);
344 
345         sk_for_each(sk, &sco_sk_list.head) {
346                 if (sk->sk_state != BT_LISTEN)
347                         continue;
348 
349                 /* Exact match. */
350                 if (!bacmp(&sco_pi(sk)->src, src))
351                         break;
352 
353                 /* Closest match */
354                 if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
355                         sk1 = sk;
356         }
357 
358         read_unlock(&sco_sk_list.lock);
359 
360         return sk ? sk : sk1;
361 }
362 
363 static void sco_sock_destruct(struct sock *sk)
364 {
365         BT_DBG("sk %p", sk);
366 
367         skb_queue_purge(&sk->sk_receive_queue);
368         skb_queue_purge(&sk->sk_write_queue);
369 }
370 
371 static void sco_sock_cleanup_listen(struct sock *parent)
372 {
373         struct sock *sk;
374 
375         BT_DBG("parent %p", parent);
376 
377         /* Close not yet accepted channels */
378         while ((sk = bt_accept_dequeue(parent, NULL))) {
379                 sco_sock_close(sk);
380                 sco_sock_kill(sk);
381         }
382 
383         parent->sk_state  = BT_CLOSED;
384         sock_set_flag(parent, SOCK_ZAPPED);
385 }
386 
387 /* Kill socket (only if zapped and orphan)
388  * Must be called on unlocked socket.
389  */
390 static void sco_sock_kill(struct sock *sk)
391 {
392         if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
393                 return;
394 
395         BT_DBG("sk %p state %d", sk, sk->sk_state);
396 
397         /* Kill poor orphan */
398         bt_sock_unlink(&sco_sk_list, sk);
399         sock_set_flag(sk, SOCK_DEAD);
400         sock_put(sk);
401 }
402 
403 static void __sco_sock_close(struct sock *sk)
404 {
405         BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
406 
407         switch (sk->sk_state) {
408         case BT_LISTEN:
409                 sco_sock_cleanup_listen(sk);
410                 break;
411 
412         case BT_CONNECTED:
413         case BT_CONFIG:
414                 if (sco_pi(sk)->conn->hcon) {
415                         sk->sk_state = BT_DISCONN;
416                         sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
417                         hci_conn_drop(sco_pi(sk)->conn->hcon);
418                         sco_pi(sk)->conn->hcon = NULL;
419                 } else
420                         sco_chan_del(sk, ECONNRESET);
421                 break;
422 
423         case BT_CONNECT2:
424         case BT_CONNECT:
425         case BT_DISCONN:
426                 sco_chan_del(sk, ECONNRESET);
427                 break;
428 
429         default:
430                 sock_set_flag(sk, SOCK_ZAPPED);
431                 break;
432         }
433 }
434 
435 /* Must be called on unlocked socket. */
436 static void sco_sock_close(struct sock *sk)
437 {
438         sco_sock_clear_timer(sk);
439         lock_sock(sk);
440         __sco_sock_close(sk);
441         release_sock(sk);
442         sco_sock_kill(sk);
443 }
444 
445 static void sco_sock_init(struct sock *sk, struct sock *parent)
446 {
447         BT_DBG("sk %p", sk);
448 
449         if (parent) {
450                 sk->sk_type = parent->sk_type;
451                 bt_sk(sk)->flags = bt_sk(parent)->flags;
452                 security_sk_clone(parent, sk);
453         }
454 }
455 
456 static struct proto sco_proto = {
457         .name           = "SCO",
458         .owner          = THIS_MODULE,
459         .obj_size       = sizeof(struct sco_pinfo)
460 };
461 
462 static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio, int kern)
463 {
464         struct sock *sk;
465 
466         sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, kern);
467         if (!sk)
468                 return NULL;
469 
470         sock_init_data(sock, sk);
471         INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
472 
473         sk->sk_destruct = sco_sock_destruct;
474         sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
475 
476         sock_reset_flag(sk, SOCK_ZAPPED);
477 
478         sk->sk_protocol = proto;
479         sk->sk_state    = BT_OPEN;
480 
481         sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
482 
483         setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
484 
485         bt_sock_link(&sco_sk_list, sk);
486         return sk;
487 }
488 
489 static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
490                            int kern)
491 {
492         struct sock *sk;
493 
494         BT_DBG("sock %p", sock);
495 
496         sock->state = SS_UNCONNECTED;
497 
498         if (sock->type != SOCK_SEQPACKET)
499                 return -ESOCKTNOSUPPORT;
500 
501         sock->ops = &sco_sock_ops;
502 
503         sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
504         if (!sk)
505                 return -ENOMEM;
506 
507         sco_sock_init(sk, NULL);
508         return 0;
509 }
510 
511 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
512 {
513         struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
514         struct sock *sk = sock->sk;
515         int err = 0;
516 
517         BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
518 
519         if (!addr || addr->sa_family != AF_BLUETOOTH)
520                 return -EINVAL;
521 
522         if (addr_len < sizeof(struct sockaddr_sco))
523                 return -EINVAL;
524 
525         lock_sock(sk);
526 
527         if (sk->sk_state != BT_OPEN) {
528                 err = -EBADFD;
529                 goto done;
530         }
531 
532         if (sk->sk_type != SOCK_SEQPACKET) {
533                 err = -EINVAL;
534                 goto done;
535         }
536 
537         bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
538 
539         sk->sk_state = BT_BOUND;
540 
541 done:
542         release_sock(sk);
543         return err;
544 }
545 
546 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
547 {
548         struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
549         struct sock *sk = sock->sk;
550         int err;
551 
552         BT_DBG("sk %p", sk);
553 
554         if (alen < sizeof(struct sockaddr_sco) ||
555             addr->sa_family != AF_BLUETOOTH)
556                 return -EINVAL;
557 
558         if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
559                 return -EBADFD;
560 
561         if (sk->sk_type != SOCK_SEQPACKET)
562                 return -EINVAL;
563 
564         lock_sock(sk);
565 
566         /* Set destination address and psm */
567         bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
568 
569         err = sco_connect(sk);
570         if (err)
571                 goto done;
572 
573         err = bt_sock_wait_state(sk, BT_CONNECTED,
574                                  sock_sndtimeo(sk, flags & O_NONBLOCK));
575 
576 done:
577         release_sock(sk);
578         return err;
579 }
580 
581 static int sco_sock_listen(struct socket *sock, int backlog)
582 {
583         struct sock *sk = sock->sk;
584         bdaddr_t *src = &sco_pi(sk)->src;
585         int err = 0;
586 
587         BT_DBG("sk %p backlog %d", sk, backlog);
588 
589         lock_sock(sk);
590 
591         if (sk->sk_state != BT_BOUND) {
592                 err = -EBADFD;
593                 goto done;
594         }
595 
596         if (sk->sk_type != SOCK_SEQPACKET) {
597                 err = -EINVAL;
598                 goto done;
599         }
600 
601         write_lock(&sco_sk_list.lock);
602 
603         if (__sco_get_sock_listen_by_addr(src)) {
604                 err = -EADDRINUSE;
605                 goto unlock;
606         }
607 
608         sk->sk_max_ack_backlog = backlog;
609         sk->sk_ack_backlog = 0;
610 
611         sk->sk_state = BT_LISTEN;
612 
613 unlock:
614         write_unlock(&sco_sk_list.lock);
615 
616 done:
617         release_sock(sk);
618         return err;
619 }
620 
621 static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
622 {
623         DEFINE_WAIT_FUNC(wait, woken_wake_function);
624         struct sock *sk = sock->sk, *ch;
625         long timeo;
626         int err = 0;
627 
628         lock_sock(sk);
629 
630         timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
631 
632         BT_DBG("sk %p timeo %ld", sk, timeo);
633 
634         /* Wait for an incoming connection. (wake-one). */
635         add_wait_queue_exclusive(sk_sleep(sk), &wait);
636         while (1) {
637                 if (sk->sk_state != BT_LISTEN) {
638                         err = -EBADFD;
639                         break;
640                 }
641 
642                 ch = bt_accept_dequeue(sk, newsock);
643                 if (ch)
644                         break;
645 
646                 if (!timeo) {
647                         err = -EAGAIN;
648                         break;
649                 }
650 
651                 if (signal_pending(current)) {
652                         err = sock_intr_errno(timeo);
653                         break;
654                 }
655 
656                 release_sock(sk);
657 
658                 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
659                 lock_sock(sk);
660         }
661         remove_wait_queue(sk_sleep(sk), &wait);
662 
663         if (err)
664                 goto done;
665 
666         newsock->state = SS_CONNECTED;
667 
668         BT_DBG("new socket %p", ch);
669 
670 done:
671         release_sock(sk);
672         return err;
673 }
674 
675 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
676 {
677         struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
678         struct sock *sk = sock->sk;
679 
680         BT_DBG("sock %p, sk %p", sock, sk);
681 
682         addr->sa_family = AF_BLUETOOTH;
683         *len = sizeof(struct sockaddr_sco);
684 
685         if (peer)
686                 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
687         else
688                 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
689 
690         return 0;
691 }
692 
693 static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
694                             size_t len)
695 {
696         struct sock *sk = sock->sk;
697         int err;
698 
699         BT_DBG("sock %p, sk %p", sock, sk);
700 
701         err = sock_error(sk);
702         if (err)
703                 return err;
704 
705         if (msg->msg_flags & MSG_OOB)
706                 return -EOPNOTSUPP;
707 
708         lock_sock(sk);
709 
710         if (sk->sk_state == BT_CONNECTED)
711                 err = sco_send_frame(sk, msg, len);
712         else
713                 err = -ENOTCONN;
714 
715         release_sock(sk);
716         return err;
717 }
718 
719 static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
720 {
721         struct hci_dev *hdev = conn->hdev;
722 
723         BT_DBG("conn %p", conn);
724 
725         conn->state = BT_CONFIG;
726 
727         if (!lmp_esco_capable(hdev)) {
728                 struct hci_cp_accept_conn_req cp;
729 
730                 bacpy(&cp.bdaddr, &conn->dst);
731                 cp.role = 0x00; /* Ignored */
732 
733                 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
734         } else {
735                 struct hci_cp_accept_sync_conn_req cp;
736 
737                 bacpy(&cp.bdaddr, &conn->dst);
738                 cp.pkt_type = cpu_to_le16(conn->pkt_type);
739 
740                 cp.tx_bandwidth   = cpu_to_le32(0x00001f40);
741                 cp.rx_bandwidth   = cpu_to_le32(0x00001f40);
742                 cp.content_format = cpu_to_le16(setting);
743 
744                 switch (setting & SCO_AIRMODE_MASK) {
745                 case SCO_AIRMODE_TRANSP:
746                         if (conn->pkt_type & ESCO_2EV3)
747                                 cp.max_latency = cpu_to_le16(0x0008);
748                         else
749                                 cp.max_latency = cpu_to_le16(0x000D);
750                         cp.retrans_effort = 0x02;
751                         break;
752                 case SCO_AIRMODE_CVSD:
753                         cp.max_latency = cpu_to_le16(0xffff);
754                         cp.retrans_effort = 0xff;
755                         break;
756                 }
757 
758                 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
759                              sizeof(cp), &cp);
760         }
761 }
762 
763 static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
764                             size_t len, int flags)
765 {
766         struct sock *sk = sock->sk;
767         struct sco_pinfo *pi = sco_pi(sk);
768 
769         lock_sock(sk);
770 
771         if (sk->sk_state == BT_CONNECT2 &&
772             test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
773                 sco_conn_defer_accept(pi->conn->hcon, pi->setting);
774                 sk->sk_state = BT_CONFIG;
775 
776                 release_sock(sk);
777                 return 0;
778         }
779 
780         release_sock(sk);
781 
782         return bt_sock_recvmsg(sock, msg, len, flags);
783 }
784 
785 static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
786 {
787         struct sock *sk = sock->sk;
788         int len, err = 0;
789         struct bt_voice voice;
790         u32 opt;
791 
792         BT_DBG("sk %p", sk);
793 
794         lock_sock(sk);
795 
796         switch (optname) {
797 
798         case BT_DEFER_SETUP:
799                 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
800                         err = -EINVAL;
801                         break;
802                 }
803 
804                 if (get_user(opt, (u32 __user *) optval)) {
805                         err = -EFAULT;
806                         break;
807                 }
808 
809                 if (opt)
810                         set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
811                 else
812                         clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
813                 break;
814 
815         case BT_VOICE:
816                 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
817                     sk->sk_state != BT_CONNECT2) {
818                         err = -EINVAL;
819                         break;
820                 }
821 
822                 voice.setting = sco_pi(sk)->setting;
823 
824                 len = min_t(unsigned int, sizeof(voice), optlen);
825                 if (copy_from_user((char *) &voice, optval, len)) {
826                         err = -EFAULT;
827                         break;
828                 }
829 
830                 /* Explicitly check for these values */
831                 if (voice.setting != BT_VOICE_TRANSPARENT &&
832                     voice.setting != BT_VOICE_CVSD_16BIT) {
833                         err = -EINVAL;
834                         break;
835                 }
836 
837                 sco_pi(sk)->setting = voice.setting;
838                 break;
839 
840         default:
841                 err = -ENOPROTOOPT;
842                 break;
843         }
844 
845         release_sock(sk);
846         return err;
847 }
848 
849 static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
850 {
851         struct sock *sk = sock->sk;
852         struct sco_options opts;
853         struct sco_conninfo cinfo;
854         int len, err = 0;
855 
856         BT_DBG("sk %p", sk);
857 
858         if (get_user(len, optlen))
859                 return -EFAULT;
860 
861         lock_sock(sk);
862 
863         switch (optname) {
864         case SCO_OPTIONS:
865                 if (sk->sk_state != BT_CONNECTED &&
866                     !(sk->sk_state == BT_CONNECT2 &&
867                       test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
868                         err = -ENOTCONN;
869                         break;
870                 }
871 
872                 opts.mtu = sco_pi(sk)->conn->mtu;
873 
874                 BT_DBG("mtu %d", opts.mtu);
875 
876                 len = min_t(unsigned int, len, sizeof(opts));
877                 if (copy_to_user(optval, (char *)&opts, len))
878                         err = -EFAULT;
879 
880                 break;
881 
882         case SCO_CONNINFO:
883                 if (sk->sk_state != BT_CONNECTED &&
884                     !(sk->sk_state == BT_CONNECT2 &&
885                       test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
886                         err = -ENOTCONN;
887                         break;
888                 }
889 
890                 memset(&cinfo, 0, sizeof(cinfo));
891                 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
892                 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
893 
894                 len = min_t(unsigned int, len, sizeof(cinfo));
895                 if (copy_to_user(optval, (char *)&cinfo, len))
896                         err = -EFAULT;
897 
898                 break;
899 
900         default:
901                 err = -ENOPROTOOPT;
902                 break;
903         }
904 
905         release_sock(sk);
906         return err;
907 }
908 
909 static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
910 {
911         struct sock *sk = sock->sk;
912         int len, err = 0;
913         struct bt_voice voice;
914 
915         BT_DBG("sk %p", sk);
916 
917         if (level == SOL_SCO)
918                 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
919 
920         if (get_user(len, optlen))
921                 return -EFAULT;
922 
923         lock_sock(sk);
924 
925         switch (optname) {
926 
927         case BT_DEFER_SETUP:
928                 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
929                         err = -EINVAL;
930                         break;
931                 }
932 
933                 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
934                              (u32 __user *) optval))
935                         err = -EFAULT;
936 
937                 break;
938 
939         case BT_VOICE:
940                 voice.setting = sco_pi(sk)->setting;
941 
942                 len = min_t(unsigned int, len, sizeof(voice));
943                 if (copy_to_user(optval, (char *)&voice, len))
944                         err = -EFAULT;
945 
946                 break;
947 
948         default:
949                 err = -ENOPROTOOPT;
950                 break;
951         }
952 
953         release_sock(sk);
954         return err;
955 }
956 
957 static int sco_sock_shutdown(struct socket *sock, int how)
958 {
959         struct sock *sk = sock->sk;
960         int err = 0;
961 
962         BT_DBG("sock %p, sk %p", sock, sk);
963 
964         if (!sk)
965                 return 0;
966 
967         lock_sock(sk);
968         if (!sk->sk_shutdown) {
969                 sk->sk_shutdown = SHUTDOWN_MASK;
970                 sco_sock_clear_timer(sk);
971                 __sco_sock_close(sk);
972 
973                 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
974                     !(current->flags & PF_EXITING))
975                         err = bt_sock_wait_state(sk, BT_CLOSED,
976                                                  sk->sk_lingertime);
977         }
978         release_sock(sk);
979         return err;
980 }
981 
982 static int sco_sock_release(struct socket *sock)
983 {
984         struct sock *sk = sock->sk;
985         int err = 0;
986 
987         BT_DBG("sock %p, sk %p", sock, sk);
988 
989         if (!sk)
990                 return 0;
991 
992         sco_sock_close(sk);
993 
994         if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
995             !(current->flags & PF_EXITING)) {
996                 lock_sock(sk);
997                 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
998                 release_sock(sk);
999         }
1000 
1001         sock_orphan(sk);
1002         sco_sock_kill(sk);
1003         return err;
1004 }
1005 
1006 static void sco_conn_ready(struct sco_conn *conn)
1007 {
1008         struct sock *parent;
1009         struct sock *sk = conn->sk;
1010 
1011         BT_DBG("conn %p", conn);
1012 
1013         if (sk) {
1014                 sco_sock_clear_timer(sk);
1015                 bh_lock_sock(sk);
1016                 sk->sk_state = BT_CONNECTED;
1017                 sk->sk_state_change(sk);
1018                 bh_unlock_sock(sk);
1019         } else {
1020                 sco_conn_lock(conn);
1021 
1022                 parent = sco_get_sock_listen(&conn->hcon->src);
1023                 if (!parent) {
1024                         sco_conn_unlock(conn);
1025                         return;
1026                 }
1027 
1028                 bh_lock_sock(parent);
1029 
1030                 sk = sco_sock_alloc(sock_net(parent), NULL,
1031                                     BTPROTO_SCO, GFP_ATOMIC, 0);
1032                 if (!sk) {
1033                         bh_unlock_sock(parent);
1034                         sco_conn_unlock(conn);
1035                         return;
1036                 }
1037 
1038                 sco_sock_init(sk, parent);
1039 
1040                 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1041                 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
1042 
1043                 hci_conn_hold(conn->hcon);
1044                 __sco_chan_add(conn, sk, parent);
1045 
1046                 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1047                         sk->sk_state = BT_CONNECT2;
1048                 else
1049                         sk->sk_state = BT_CONNECTED;
1050 
1051                 /* Wake up parent */
1052                 parent->sk_data_ready(parent);
1053 
1054                 bh_unlock_sock(parent);
1055 
1056                 sco_conn_unlock(conn);
1057         }
1058 }
1059 
1060 /* ----- SCO interface with lower layer (HCI) ----- */
1061 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1062 {
1063         struct sock *sk;
1064         int lm = 0;
1065 
1066         BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
1067 
1068         /* Find listening sockets */
1069         read_lock(&sco_sk_list.lock);
1070         sk_for_each(sk, &sco_sk_list.head) {
1071                 if (sk->sk_state != BT_LISTEN)
1072                         continue;
1073 
1074                 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1075                     !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
1076                         lm |= HCI_LM_ACCEPT;
1077 
1078                         if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1079                                 *flags |= HCI_PROTO_DEFER;
1080                         break;
1081                 }
1082         }
1083         read_unlock(&sco_sk_list.lock);
1084 
1085         return lm;
1086 }
1087 
1088 static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
1089 {
1090         if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1091                 return;
1092 
1093         BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
1094 
1095         if (!status) {
1096                 struct sco_conn *conn;
1097 
1098                 conn = sco_conn_add(hcon);
1099                 if (conn)
1100                         sco_conn_ready(conn);
1101         } else
1102                 sco_conn_del(hcon, bt_to_errno(status));
1103 }
1104 
1105 static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1106 {
1107         if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1108                 return;
1109 
1110         BT_DBG("hcon %p reason %d", hcon, reason);
1111 
1112         sco_conn_del(hcon, bt_to_errno(reason));
1113 }
1114 
1115 void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
1116 {
1117         struct sco_conn *conn = hcon->sco_data;
1118 
1119         if (!conn)
1120                 goto drop;
1121 
1122         BT_DBG("conn %p len %d", conn, skb->len);
1123 
1124         if (skb->len) {
1125                 sco_recv_frame(conn, skb);
1126                 return;
1127         }
1128 
1129 drop:
1130         kfree_skb(skb);
1131 }
1132 
1133 static struct hci_cb sco_cb = {
1134         .name           = "SCO",
1135         .connect_cfm    = sco_connect_cfm,
1136         .disconn_cfm    = sco_disconn_cfm,
1137 };
1138 
1139 static int sco_debugfs_show(struct seq_file *f, void *p)
1140 {
1141         struct sock *sk;
1142 
1143         read_lock(&sco_sk_list.lock);
1144 
1145         sk_for_each(sk, &sco_sk_list.head) {
1146                 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1147                            &sco_pi(sk)->dst, sk->sk_state);
1148         }
1149 
1150         read_unlock(&sco_sk_list.lock);
1151 
1152         return 0;
1153 }
1154 
1155 static int sco_debugfs_open(struct inode *inode, struct file *file)
1156 {
1157         return single_open(file, sco_debugfs_show, inode->i_private);
1158 }
1159 
1160 static const struct file_operations sco_debugfs_fops = {
1161         .open           = sco_debugfs_open,
1162         .read           = seq_read,
1163         .llseek         = seq_lseek,
1164         .release        = single_release,
1165 };
1166 
1167 static struct dentry *sco_debugfs;
1168 
1169 static const struct proto_ops sco_sock_ops = {
1170         .family         = PF_BLUETOOTH,
1171         .owner          = THIS_MODULE,
1172         .release        = sco_sock_release,
1173         .bind           = sco_sock_bind,
1174         .connect        = sco_sock_connect,
1175         .listen         = sco_sock_listen,
1176         .accept         = sco_sock_accept,
1177         .getname        = sco_sock_getname,
1178         .sendmsg        = sco_sock_sendmsg,
1179         .recvmsg        = sco_sock_recvmsg,
1180         .poll           = bt_sock_poll,
1181         .ioctl          = bt_sock_ioctl,
1182         .mmap           = sock_no_mmap,
1183         .socketpair     = sock_no_socketpair,
1184         .shutdown       = sco_sock_shutdown,
1185         .setsockopt     = sco_sock_setsockopt,
1186         .getsockopt     = sco_sock_getsockopt
1187 };
1188 
1189 static const struct net_proto_family sco_sock_family_ops = {
1190         .family = PF_BLUETOOTH,
1191         .owner  = THIS_MODULE,
1192         .create = sco_sock_create,
1193 };
1194 
1195 int __init sco_init(void)
1196 {
1197         int err;
1198 
1199         BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr));
1200 
1201         err = proto_register(&sco_proto, 0);
1202         if (err < 0)
1203                 return err;
1204 
1205         err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1206         if (err < 0) {
1207                 BT_ERR("SCO socket registration failed");
1208                 goto error;
1209         }
1210 
1211         err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
1212         if (err < 0) {
1213                 BT_ERR("Failed to create SCO proc file");
1214                 bt_sock_unregister(BTPROTO_SCO);
1215                 goto error;
1216         }
1217 
1218         BT_INFO("SCO socket layer initialized");
1219 
1220         hci_register_cb(&sco_cb);
1221 
1222         if (IS_ERR_OR_NULL(bt_debugfs))
1223                 return 0;
1224 
1225         sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1226                                           NULL, &sco_debugfs_fops);
1227 
1228         return 0;
1229 
1230 error:
1231         proto_unregister(&sco_proto);
1232         return err;
1233 }
1234 
1235 void sco_exit(void)
1236 {
1237         bt_procfs_cleanup(&init_net, "sco");
1238 
1239         debugfs_remove(sco_debugfs);
1240 
1241         hci_unregister_cb(&sco_cb);
1242 
1243         bt_sock_unregister(BTPROTO_SCO);
1244 
1245         proto_unregister(&sco_proto);
1246 }
1247 
1248 module_param(disable_esco, bool, 0644);
1249 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");
1250 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp