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 /*
26  * BlueZ SCO sockets.
27  *
28  * $Id: sco.c,v 1.4 2002/07/22 20:32:54 maxk Exp $
29  */
30 #define VERSION "0.3"
31 
32 #include <linux/config.h>
33 #include <linux/module.h>
34 
35 #include <linux/types.h>
36 #include <linux/errno.h>
37 #include <linux/kernel.h>
38 #include <linux/major.h>
39 #include <linux/sched.h>
40 #include <linux/slab.h>
41 #include <linux/poll.h>
42 #include <linux/fcntl.h>
43 #include <linux/init.h>
44 #include <linux/skbuff.h>
45 #include <linux/interrupt.h>
46 #include <linux/socket.h>
47 #include <linux/skbuff.h>
48 #include <linux/proc_fs.h>
49 #include <linux/list.h>
50 #include <net/sock.h>
51 
52 #include <asm/system.h>
53 #include <asm/uaccess.h>
54 
55 #include <net/bluetooth/bluetooth.h>
56 #include <net/bluetooth/hci_core.h>
57 #include <net/bluetooth/sco.h>
58 
59 #ifndef SCO_DEBUG
60 #undef  BT_DBG
61 #define BT_DBG( A... )
62 #endif
63 
64 static struct proto_ops sco_sock_ops;
65 
66 static struct bluez_sock_list sco_sk_list = {
67 	lock: RW_LOCK_UNLOCKED
68 };
69 
70 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
71 static void sco_chan_del(struct sock *sk, int err);
72 
73 static int  sco_conn_del(struct hci_conn *conn, int err);
74 
75 static void sco_sock_close(struct sock *sk);
76 static void sco_sock_kill(struct sock *sk);
77 
78 /* ----- SCO timers ------ */
sco_sock_timeout(unsigned long arg)79 static void sco_sock_timeout(unsigned long arg)
80 {
81 	struct sock *sk = (struct sock *) arg;
82 
83 	BT_DBG("sock %p state %d", sk, sk->state);
84 
85 	bh_lock_sock(sk);
86 	sk->err = ETIMEDOUT;
87 	sk->state_change(sk);
88 	bh_unlock_sock(sk);
89 
90 	sco_sock_kill(sk);
91 	sock_put(sk);
92 }
93 
sco_sock_set_timer(struct sock * sk,long timeout)94 static void sco_sock_set_timer(struct sock *sk, long timeout)
95 {
96 	BT_DBG("sock %p state %d timeout %ld", sk, sk->state, timeout);
97 
98 	if (!mod_timer(&sk->timer, jiffies + timeout))
99 		sock_hold(sk);
100 }
101 
sco_sock_clear_timer(struct sock * sk)102 static void sco_sock_clear_timer(struct sock *sk)
103 {
104 	BT_DBG("sock %p state %d", sk, sk->state);
105 
106 	if (timer_pending(&sk->timer) && del_timer(&sk->timer))
107 		__sock_put(sk);
108 }
109 
sco_sock_init_timer(struct sock * sk)110 static void sco_sock_init_timer(struct sock *sk)
111 {
112 	init_timer(&sk->timer);
113 	sk->timer.function = sco_sock_timeout;
114 	sk->timer.data = (unsigned long)sk;
115 }
116 
117 /* -------- SCO connections --------- */
sco_conn_add(struct hci_conn * hcon,__u8 status)118 static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
119 {
120 	struct hci_dev *hdev = hcon->hdev;
121 	struct sco_conn *conn;
122 
123 	if ((conn = hcon->sco_data))
124 		return conn;
125 
126 	if (status)
127 		return conn;
128 
129 	if (!(conn = kmalloc(sizeof(struct sco_conn), GFP_ATOMIC)))
130 		return NULL;
131 	memset(conn, 0, sizeof(struct sco_conn));
132 
133 	spin_lock_init(&conn->lock);
134 
135 	hcon->sco_data = conn;
136 	conn->hcon = hcon;
137 
138 	conn->src = &hdev->bdaddr;
139 	conn->dst = &hcon->dst;
140 
141 	if (hdev->sco_mtu > 0)
142 		conn->mtu = hdev->sco_mtu;
143 	else
144 		conn->mtu = 60;
145 
146 	BT_DBG("hcon %p conn %p", hcon, conn);
147 
148 	MOD_INC_USE_COUNT;
149 	return conn;
150 }
151 
sco_chan_get(struct sco_conn * conn)152 static inline struct sock * sco_chan_get(struct sco_conn *conn)
153 {
154 	struct sock *sk = NULL;
155 	sco_conn_lock(conn);
156 	sk = conn->sk;
157 	sco_conn_unlock(conn);
158 	return sk;
159 }
160 
sco_conn_del(struct hci_conn * hcon,int err)161 static int sco_conn_del(struct hci_conn *hcon, int err)
162 {
163 	struct sco_conn *conn;
164 	struct sock *sk;
165 
166 	if (!(conn = hcon->sco_data))
167 		return 0;
168 
169 	BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
170 
171 	/* Kill socket */
172 	if ((sk = sco_chan_get(conn))) {
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 	MOD_DEC_USE_COUNT;
184 	return 0;
185 }
186 
sco_chan_add(struct sco_conn * conn,struct sock * sk,struct sock * parent)187 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
188 {
189 	int err = 0;
190 
191 	sco_conn_lock(conn);
192 	if (conn->sk) {
193 		err = -EBUSY;
194 	} else {
195 		__sco_chan_add(conn, sk, parent);
196 	}
197 	sco_conn_unlock(conn);
198 	return err;
199 }
200 
sco_connect(struct sock * sk)201 int sco_connect(struct sock *sk)
202 {
203 	bdaddr_t *src = &bluez_pi(sk)->src;
204 	bdaddr_t *dst = &bluez_pi(sk)->dst;
205 	struct sco_conn *conn;
206 	struct hci_conn *hcon;
207 	struct hci_dev  *hdev;
208 	int err = 0;
209 
210 	BT_DBG("%s -> %s", batostr(src), batostr(dst));
211 
212 	if (!(hdev = hci_get_route(dst, src)))
213 		return -EHOSTUNREACH;
214 
215 	hci_dev_lock_bh(hdev);
216 
217 	err = -ENOMEM;
218 
219 	hcon = hci_connect(hdev, SCO_LINK, dst);
220 	if (!hcon)
221 		goto done;
222 
223 	conn = sco_conn_add(hcon, 0);
224 	if (!conn) {
225 		hci_conn_put(hcon);
226 		goto done;
227 	}
228 
229 	/* Update source addr of the socket */
230 	bacpy(src, conn->src);
231 
232 	err = sco_chan_add(conn, sk, NULL);
233 	if (err)
234 		goto done;
235 
236 	if (hcon->state == BT_CONNECTED) {
237 		sco_sock_clear_timer(sk);
238 		sk->state = BT_CONNECTED;
239 	} else {
240 		sk->state = BT_CONNECT;
241 		sco_sock_set_timer(sk, sk->sndtimeo);
242 	}
243 done:
244 	hci_dev_unlock_bh(hdev);
245 	hci_dev_put(hdev);
246 	return err;
247 }
248 
sco_send_frame(struct sock * sk,struct msghdr * msg,int len)249 static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
250 {
251 	struct sco_conn *conn = sco_pi(sk)->conn;
252 	struct sk_buff *skb;
253 	int err, count;
254 
255 	/* Check outgoing MTU */
256 	if (len > conn->mtu)
257 		return -EINVAL;
258 
259 	BT_DBG("sk %p len %d", sk, len);
260 
261 	count = MIN(conn->mtu, len);
262 	if (!(skb = bluez_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err)))
263 		return err;
264 
265 	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
266 		err = -EFAULT;
267 		goto fail;
268 	}
269 
270 	if ((err = hci_send_sco(conn->hcon, skb)) < 0)
271 		goto fail;
272 
273 	return count;
274 
275 fail:
276 	kfree_skb(skb);
277 	return err;
278 }
279 
sco_recv_frame(struct sco_conn * conn,struct sk_buff * skb)280 static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
281 {
282 	struct sock *sk = sco_chan_get(conn);
283 
284 	if (!sk)
285 		goto drop;
286 
287 	BT_DBG("sk %p len %d", sk, skb->len);
288 
289 	if (sk->state != BT_CONNECTED)
290 		goto drop;
291 
292 	if (!sock_queue_rcv_skb(sk, skb))
293 		return;
294 
295 drop:
296 	kfree_skb(skb);
297 	return;
298 }
299 
300 /* -------- Socket interface ---------- */
__sco_get_sock_by_addr(bdaddr_t * ba)301 static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
302 {
303 	struct sock *sk;
304 
305 	for (sk = sco_sk_list.head; sk; sk = sk->next) {
306 		if (!bacmp(&bluez_pi(sk)->src, ba))
307 			break;
308 	}
309 
310 	return sk;
311 }
312 
313 /* Find socket listening on source bdaddr.
314  * Returns closest match.
315  */
sco_get_sock_listen(bdaddr_t * src)316 static struct sock *sco_get_sock_listen(bdaddr_t *src)
317 {
318 	struct sock *sk, *sk1 = NULL;
319 
320 	read_lock(&sco_sk_list.lock);
321 
322 	for (sk = sco_sk_list.head; sk; sk = sk->next) {
323 		if (sk->state != BT_LISTEN)
324 			continue;
325 
326 		/* Exact match. */
327 		if (!bacmp(&bluez_pi(sk)->src, src))
328 			break;
329 
330 		/* Closest match */
331 		if (!bacmp(&bluez_pi(sk)->src, BDADDR_ANY))
332 			sk1 = sk;
333 	}
334 
335 	read_unlock(&sco_sk_list.lock);
336 
337 	return sk ? sk : sk1;
338 }
339 
sco_sock_destruct(struct sock * sk)340 static void sco_sock_destruct(struct sock *sk)
341 {
342 	BT_DBG("sk %p", sk);
343 
344 	skb_queue_purge(&sk->receive_queue);
345 	skb_queue_purge(&sk->write_queue);
346 
347 	MOD_DEC_USE_COUNT;
348 }
349 
sco_sock_cleanup_listen(struct sock * parent)350 static void sco_sock_cleanup_listen(struct sock *parent)
351 {
352 	struct sock *sk;
353 
354 	BT_DBG("parent %p", parent);
355 
356 	/* Close not yet accepted channels */
357 	while ((sk = bluez_accept_dequeue(parent, NULL))) {
358 		sco_sock_close(sk);
359 		sco_sock_kill(sk);
360 	}
361 
362 	parent->state  = BT_CLOSED;
363 	parent->zapped = 1;
364 }
365 
366 /* Kill socket (only if zapped and orphan)
367  * Must be called on unlocked socket.
368  */
sco_sock_kill(struct sock * sk)369 static void sco_sock_kill(struct sock *sk)
370 {
371 	if (!sk->zapped || sk->socket)
372 		return;
373 
374 	BT_DBG("sk %p state %d", sk, sk->state);
375 
376 	/* Kill poor orphan */
377 	bluez_sock_unlink(&sco_sk_list, sk);
378 	sk->dead = 1;
379 	sock_put(sk);
380 }
381 
382 /* Close socket.
383  * Must be called on unlocked socket.
384  */
sco_sock_close(struct sock * sk)385 static void sco_sock_close(struct sock *sk)
386 {
387 	struct sco_conn *conn;
388 
389 	sco_sock_clear_timer(sk);
390 
391 	lock_sock(sk);
392 
393 	conn = sco_pi(sk)->conn;
394 
395 	BT_DBG("sk %p state %d conn %p socket %p", sk, sk->state, conn, sk->socket);
396 
397 	switch (sk->state) {
398 	case BT_LISTEN:
399 		sco_sock_cleanup_listen(sk);
400 		break;
401 
402 	case BT_CONNECTED:
403 	case BT_CONFIG:
404 	case BT_CONNECT:
405 	case BT_DISCONN:
406 		sco_chan_del(sk, ECONNRESET);
407 		break;
408 
409 	default:
410 		sk->zapped = 1;
411 		break;
412 	};
413 
414 	release_sock(sk);
415 }
416 
sco_sock_init(struct sock * sk,struct sock * parent)417 static void sco_sock_init(struct sock *sk, struct sock *parent)
418 {
419 	BT_DBG("sk %p", sk);
420 
421 	if (parent)
422 		sk->type = parent->type;
423 }
424 
sco_sock_alloc(struct socket * sock,int proto,int prio)425 static struct sock *sco_sock_alloc(struct socket *sock, int proto, int prio)
426 {
427 	struct sock *sk;
428 
429 	if (!(sk = sk_alloc(PF_BLUETOOTH, prio, 1)))
430 		return NULL;
431 
432 	bluez_sock_init(sock, sk);
433 
434 	sk->zapped   = 0;
435 
436 	sk->destruct = sco_sock_destruct;
437 	sk->sndtimeo = SCO_CONN_TIMEOUT;
438 
439 	sk->protocol = proto;
440 	sk->state    = BT_OPEN;
441 
442 	sco_sock_init_timer(sk);
443 
444 	bluez_sock_link(&sco_sk_list, sk);
445 
446 	MOD_INC_USE_COUNT;
447 	return sk;
448 }
449 
sco_sock_create(struct socket * sock,int protocol)450 static int sco_sock_create(struct socket *sock, int protocol)
451 {
452 	struct sock *sk;
453 
454 	BT_DBG("sock %p", sock);
455 
456 	sock->state = SS_UNCONNECTED;
457 
458 	if (sock->type != SOCK_SEQPACKET)
459 		return -ESOCKTNOSUPPORT;
460 
461 	sock->ops = &sco_sock_ops;
462 
463 	if (!(sk = sco_sock_alloc(sock, protocol, GFP_KERNEL)))
464 		return -ENOMEM;
465 
466 	sco_sock_init(sk, NULL);
467 	return 0;
468 }
469 
sco_sock_bind(struct socket * sock,struct sockaddr * addr,int addr_len)470 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
471 {
472 	struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
473 	struct sock *sk = sock->sk;
474 	bdaddr_t *src = &sa->sco_bdaddr;
475 	int err = 0;
476 
477 	BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
478 
479 	if (!addr || addr->sa_family != AF_BLUETOOTH)
480 		return -EINVAL;
481 
482 	lock_sock(sk);
483 
484 	if (sk->state != BT_OPEN) {
485 		err = -EBADFD;
486 		goto done;
487 	}
488 
489 	write_lock_bh(&sco_sk_list.lock);
490 
491 	if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
492 		err = -EADDRINUSE;
493 	} else {
494 		/* Save source address */
495 		bacpy(&bluez_pi(sk)->src, &sa->sco_bdaddr);
496 		sk->state = BT_BOUND;
497 	}
498 
499 	write_unlock_bh(&sco_sk_list.lock);
500 
501 done:
502 	release_sock(sk);
503 
504 	return err;
505 }
506 
sco_sock_connect(struct socket * sock,struct sockaddr * addr,int alen,int flags)507 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
508 {
509 	struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
510 	struct sock *sk = sock->sk;
511 	int err = 0;
512 
513 
514 	BT_DBG("sk %p", sk);
515 
516 	if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_sco))
517 		return -EINVAL;
518 
519 	if (sk->state != BT_OPEN && sk->state != BT_BOUND)
520 		return -EBADFD;
521 
522 	if (sk->type != SOCK_SEQPACKET)
523 		return -EINVAL;
524 
525 	lock_sock(sk);
526 
527 	/* Set destination address and psm */
528 	bacpy(&bluez_pi(sk)->dst, &sa->sco_bdaddr);
529 
530 	if ((err = sco_connect(sk)))
531 		goto done;
532 
533 	err = bluez_sock_wait_state(sk, BT_CONNECTED,
534 			sock_sndtimeo(sk, flags & O_NONBLOCK));
535 
536 done:
537 	release_sock(sk);
538 	return err;
539 }
540 
sco_sock_listen(struct socket * sock,int backlog)541 int sco_sock_listen(struct socket *sock, int backlog)
542 {
543 	struct sock *sk = sock->sk;
544 	int err = 0;
545 
546 	BT_DBG("sk %p backlog %d", sk, backlog);
547 
548 	lock_sock(sk);
549 
550 	if (sk->state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
551 		err = -EBADFD;
552 		goto done;
553 	}
554 
555 	sk->max_ack_backlog = backlog;
556 	sk->ack_backlog = 0;
557 	sk->state = BT_LISTEN;
558 
559 done:
560 	release_sock(sk);
561 	return err;
562 }
563 
sco_sock_accept(struct socket * sock,struct socket * newsock,int flags)564 int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
565 {
566 	DECLARE_WAITQUEUE(wait, current);
567 	struct sock *sk = sock->sk, *ch;
568 	long timeo;
569 	int err = 0;
570 
571 	lock_sock(sk);
572 
573 	if (sk->state != BT_LISTEN) {
574 		err = -EBADFD;
575 		goto done;
576 	}
577 
578 	timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
579 
580 	BT_DBG("sk %p timeo %ld", sk, timeo);
581 
582 	/* Wait for an incoming connection. (wake-one). */
583 	add_wait_queue_exclusive(sk->sleep, &wait);
584 	while (!(ch = bluez_accept_dequeue(sk, newsock))) {
585 		set_current_state(TASK_INTERRUPTIBLE);
586 		if (!timeo) {
587 			err = -EAGAIN;
588 			break;
589 		}
590 
591 		release_sock(sk);
592 		timeo = schedule_timeout(timeo);
593 		lock_sock(sk);
594 
595 		if (sk->state != BT_LISTEN) {
596 			err = -EBADFD;
597 			break;
598 		}
599 
600 		if (signal_pending(current)) {
601 			err = sock_intr_errno(timeo);
602 			break;
603 		}
604 	}
605 	set_current_state(TASK_RUNNING);
606 	remove_wait_queue(sk->sleep, &wait);
607 
608 	if (err)
609 		goto done;
610 
611 	newsock->state = SS_CONNECTED;
612 
613 	BT_DBG("new socket %p", ch);
614 
615 done:
616 	release_sock(sk);
617 	return err;
618 }
619 
sco_sock_getname(struct socket * sock,struct sockaddr * addr,int * len,int peer)620 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
621 {
622 	struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
623 	struct sock *sk = sock->sk;
624 
625 	BT_DBG("sock %p, sk %p", sock, sk);
626 
627 	addr->sa_family = AF_BLUETOOTH;
628 	*len = sizeof(struct sockaddr_sco);
629 
630 	if (peer)
631 		bacpy(&sa->sco_bdaddr, &bluez_pi(sk)->dst);
632 	else
633 		bacpy(&sa->sco_bdaddr, &bluez_pi(sk)->src);
634 
635 	return 0;
636 }
637 
sco_sock_sendmsg(struct socket * sock,struct msghdr * msg,int len,struct scm_cookie * scm)638 static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
639 {
640 	struct sock *sk = sock->sk;
641 	int err = 0;
642 
643 	BT_DBG("sock %p, sk %p", sock, sk);
644 
645 	if (sk->err)
646 		return sock_error(sk);
647 
648 	if (msg->msg_flags & MSG_OOB)
649 		return -EOPNOTSUPP;
650 
651 	lock_sock(sk);
652 
653 	if (sk->state == BT_CONNECTED)
654 		err = sco_send_frame(sk, msg, len);
655 	else
656 		err = -ENOTCONN;
657 
658 	release_sock(sk);
659 	return err;
660 }
661 
sco_sock_setsockopt(struct socket * sock,int level,int optname,char * optval,int optlen)662 int sco_sock_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
663 {
664 	struct sock *sk = sock->sk;
665 	int err = 0;
666 
667 	BT_DBG("sk %p", sk);
668 
669 	lock_sock(sk);
670 
671 	switch (optname) {
672 	default:
673 		err = -ENOPROTOOPT;
674 		break;
675 	};
676 
677 	release_sock(sk);
678 	return err;
679 }
680 
sco_sock_getsockopt(struct socket * sock,int level,int optname,char * optval,int * optlen)681 int sco_sock_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
682 {
683 	struct sock *sk = sock->sk;
684 	struct sco_options opts;
685 	struct sco_conninfo cinfo;
686 	int len, err = 0;
687 
688 	BT_DBG("sk %p", sk);
689 
690 	if (get_user(len, optlen))
691 		return -EFAULT;
692 
693 	lock_sock(sk);
694 
695 	switch (optname) {
696 	case SCO_OPTIONS:
697 		if (sk->state != BT_CONNECTED) {
698 			err = -ENOTCONN;
699 			break;
700 		}
701 
702 		opts.mtu = sco_pi(sk)->conn->mtu;
703 
704 		BT_DBG("mtu %d", opts.mtu);
705 
706 		len = MIN(len, sizeof(opts));
707 		if (copy_to_user(optval, (char *)&opts, len))
708 			err = -EFAULT;
709 
710 		break;
711 
712 	case SCO_CONNINFO:
713 		if (sk->state != BT_CONNECTED) {
714 			err = -ENOTCONN;
715 			break;
716 		}
717 
718 		cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
719 
720 		len = MIN(len, sizeof(cinfo));
721 		if (copy_to_user(optval, (char *)&cinfo, len))
722 			err = -EFAULT;
723 
724 		break;
725 
726 	default:
727 		err = -ENOPROTOOPT;
728 		break;
729 	};
730 
731 	release_sock(sk);
732 	return err;
733 }
734 
sco_sock_release(struct socket * sock)735 static int sco_sock_release(struct socket *sock)
736 {
737 	struct sock *sk = sock->sk;
738 	int err = 0;
739 
740 	BT_DBG("sock %p, sk %p", sock, sk);
741 
742 	if (!sk)
743 		return 0;
744 
745 	sco_sock_close(sk);
746 	if (sk->linger) {
747 		lock_sock(sk);
748 		err = bluez_sock_wait_state(sk, BT_CLOSED, sk->lingertime);
749 		release_sock(sk);
750 	}
751 
752 	sock_orphan(sk);
753 	sco_sock_kill(sk);
754 	return err;
755 }
756 
__sco_chan_add(struct sco_conn * conn,struct sock * sk,struct sock * parent)757 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
758 {
759 	BT_DBG("conn %p", conn);
760 
761 	sco_pi(sk)->conn = conn;
762 	conn->sk = sk;
763 
764 	if (parent)
765 		bluez_accept_enqueue(parent, sk);
766 }
767 
768 /* Delete channel.
769  * Must be called on the locked socket. */
sco_chan_del(struct sock * sk,int err)770 static void sco_chan_del(struct sock *sk, int err)
771 {
772 	struct sco_conn *conn;
773 
774 	conn = sco_pi(sk)->conn;
775 
776 	BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
777 
778 	if (conn) {
779 		sco_conn_lock(conn);
780 		conn->sk = NULL;
781 		sco_pi(sk)->conn = NULL;
782 		sco_conn_unlock(conn);
783 		hci_conn_put(conn->hcon);
784 	}
785 
786 	sk->state = BT_CLOSED;
787 	sk->err   = err;
788 	sk->state_change(sk);
789 
790 	sk->zapped = 1;
791 }
792 
sco_conn_ready(struct sco_conn * conn)793 static void sco_conn_ready(struct sco_conn *conn)
794 {
795 	struct sock *parent, *sk;
796 
797 	BT_DBG("conn %p", conn);
798 
799 	sco_conn_lock(conn);
800 
801 	if ((sk = conn->sk)) {
802 		sco_sock_clear_timer(sk);
803 		bh_lock_sock(sk);
804 		sk->state = BT_CONNECTED;
805 		sk->state_change(sk);
806 		bh_unlock_sock(sk);
807 	} else {
808 		parent = sco_get_sock_listen(conn->src);
809 		if (!parent)
810 			goto done;
811 
812 		bh_lock_sock(parent);
813 
814 		sk = sco_sock_alloc(NULL, BTPROTO_SCO, GFP_ATOMIC);
815 		if (!sk) {
816 			bh_unlock_sock(parent);
817                 	goto done;
818 		}
819 
820 		sco_sock_init(sk, parent);
821 
822 		bacpy(&bluez_pi(sk)->src, conn->src);
823 		bacpy(&bluez_pi(sk)->dst, conn->dst);
824 
825 		hci_conn_hold(conn->hcon);
826         	__sco_chan_add(conn, sk, parent);
827 
828         	sk->state = BT_CONNECTED;
829 
830 		/* Wake up parent */
831 		parent->data_ready(parent, 1);
832 
833         	bh_unlock_sock(parent);
834 	}
835 
836 done:
837 	sco_conn_unlock(conn);
838 }
839 
840 /* ----- SCO interface with lower layer (HCI) ----- */
sco_connect_ind(struct hci_dev * hdev,bdaddr_t * bdaddr,__u8 type)841 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
842 {
843 	BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
844 
845 	/* Always accept connection */
846 	return HCI_LM_ACCEPT;
847 }
848 
sco_connect_cfm(struct hci_conn * hcon,__u8 status)849 int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
850 {
851 	BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
852 
853 	if (hcon->type != SCO_LINK)
854 		return 0;
855 
856 	if (!status) {
857 		struct sco_conn *conn;
858 
859 		conn = sco_conn_add(hcon, status);
860 		if (conn)
861 			sco_conn_ready(conn);
862 	} else
863 		sco_conn_del(hcon, bterr(status));
864 
865 	return 0;
866 }
867 
sco_disconn_ind(struct hci_conn * hcon,__u8 reason)868 int sco_disconn_ind(struct hci_conn *hcon, __u8 reason)
869 {
870 	BT_DBG("hcon %p reason %d", hcon, reason);
871 
872 	if (hcon->type != SCO_LINK)
873 		return 0;
874 
875 	sco_conn_del(hcon, bterr(reason));
876 	return 0;
877 }
878 
sco_recv_scodata(struct hci_conn * hcon,struct sk_buff * skb)879 int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
880 {
881 	struct sco_conn *conn = hcon->sco_data;
882 
883 	if (!conn)
884 		goto drop;
885 
886 	BT_DBG("conn %p len %d", conn, skb->len);
887 
888 	if (skb->len) {
889 		sco_recv_frame(conn, skb);
890 		return 0;
891 	}
892 
893 drop:
894 	kfree_skb(skb);
895 	return 0;
896 }
897 
898 /* ----- Proc fs support ------ */
sco_sock_dump(char * buf,struct bluez_sock_list * list)899 static int sco_sock_dump(char *buf, struct bluez_sock_list *list)
900 {
901 	struct sco_pinfo *pi;
902 	struct sock *sk;
903 	char *ptr = buf;
904 
905 	write_lock_bh(&list->lock);
906 
907 	for (sk = list->head; sk; sk = sk->next) {
908 		pi = sco_pi(sk);
909 		ptr += sprintf(ptr, "%s %s %d\n",
910 				batostr(&bluez_pi(sk)->src), batostr(&bluez_pi(sk)->dst),
911 				sk->state);
912 	}
913 
914 	write_unlock_bh(&list->lock);
915 
916 	ptr += sprintf(ptr, "\n");
917 
918 	return ptr - buf;
919 }
920 
sco_read_proc(char * buf,char ** start,off_t offset,int count,int * eof,void * priv)921 static int sco_read_proc(char *buf, char **start, off_t offset, int count, int *eof, void *priv)
922 {
923 	char *ptr = buf;
924 	int len;
925 
926 	BT_DBG("count %d, offset %ld", count, offset);
927 
928 	ptr += sco_sock_dump(ptr, &sco_sk_list);
929 	len  = ptr - buf;
930 
931 	if (len <= count + offset)
932 		*eof = 1;
933 
934 	*start = buf + offset;
935 	len -= offset;
936 
937 	if (len > count)
938 		len = count;
939 	if (len < 0)
940 		len = 0;
941 
942 	return len;
943 }
944 
945 static struct proto_ops sco_sock_ops = {
946 	family:		PF_BLUETOOTH,
947 	release:	sco_sock_release,
948 	bind:		sco_sock_bind,
949 	connect:	sco_sock_connect,
950 	listen:		sco_sock_listen,
951 	accept:		sco_sock_accept,
952 	getname:	sco_sock_getname,
953 	sendmsg:	sco_sock_sendmsg,
954 	recvmsg:	bluez_sock_recvmsg,
955 	poll:		bluez_sock_poll,
956 	socketpair:	sock_no_socketpair,
957 	ioctl:		sock_no_ioctl,
958 	shutdown:	sock_no_shutdown,
959 	setsockopt:	sco_sock_setsockopt,
960 	getsockopt:	sco_sock_getsockopt,
961 	mmap:		sock_no_mmap
962 };
963 
964 static struct net_proto_family sco_sock_family_ops = {
965 	family:		PF_BLUETOOTH,
966 	create:		sco_sock_create
967 };
968 
969 static struct hci_proto sco_hci_proto = {
970 	name:		"SCO",
971 	id:		HCI_PROTO_SCO,
972 	connect_ind:	sco_connect_ind,
973 	connect_cfm:	sco_connect_cfm,
974 	disconn_ind:	sco_disconn_ind,
975 	recv_scodata:	sco_recv_scodata,
976 };
977 
sco_init(void)978 int __init sco_init(void)
979 {
980 	int err;
981 
982 	if ((err = bluez_sock_register(BTPROTO_SCO, &sco_sock_family_ops))) {
983 		BT_ERR("Can't register SCO socket layer");
984 		return err;
985 	}
986 
987 	if ((err = hci_register_proto(&sco_hci_proto))) {
988 		BT_ERR("Can't register SCO protocol");
989 		return err;
990 	}
991 
992 	create_proc_read_entry("bluetooth/sco", 0, 0, sco_read_proc, NULL);
993 
994 	BT_INFO("BlueZ SCO ver %s Copyright (C) 2000,2001 Qualcomm Inc", VERSION);
995 	BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
996 	return 0;
997 }
998 
sco_cleanup(void)999 void sco_cleanup(void)
1000 {
1001 	int err;
1002 
1003 	remove_proc_entry("bluetooth/sco", NULL);
1004 
1005 	/* Unregister socket, protocol and notifier */
1006 	if ((err = bluez_sock_unregister(BTPROTO_SCO)))
1007 		BT_ERR("Can't unregister SCO socket layer %d", err);
1008 
1009 	if ((err = hci_unregister_proto(&sco_hci_proto)))
1010 		BT_ERR("Can't unregister SCO protocol %d", err);
1011 }
1012 
1013 module_init(sco_init);
1014 module_exit(sco_cleanup);
1015 
1016 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
1017 MODULE_DESCRIPTION("BlueZ SCO ver " VERSION);
1018 MODULE_LICENSE("GPL");
1019