blob: a69aa345597e77d2cd34cdf443702acccbb4b318 [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth SCO sockets. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28
29#include <linux/types.h>
30#include <linux/errno.h>
31#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/sched.h>
33#include <linux/slab.h>
34#include <linux/poll.h>
35#include <linux/fcntl.h>
36#include <linux/init.h>
37#include <linux/interrupt.h>
38#include <linux/socket.h>
39#include <linux/skbuff.h>
Marcel Holtmannbe9d1222005-11-08 09:57:38 -080040#include <linux/device.h>
Marcel Holtmannaef7d972010-03-21 05:27:45 +010041#include <linux/debugfs.h>
42#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/list.h>
Paul Moore6230c9b2011-10-07 09:40:59 +000044#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/sock.h>
46
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +020047#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <net/bluetooth/bluetooth.h>
50#include <net/bluetooth/hci_core.h>
51#include <net/bluetooth/sco.h>
52
Rusty Russelleb939922011-12-19 14:08:01 +000053static bool disable_esco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080055static const struct proto_ops sco_sock_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static struct bt_sock_list sco_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070058 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void sco_sock_close(struct sock *sk);
65static void sco_sock_kill(struct sock *sk);
66
67/* ---- SCO timers ---- */
68static void sco_sock_timeout(unsigned long arg)
69{
70 struct sock *sk = (struct sock *) arg;
71
72 BT_DBG("sock %p state %d", sk, sk->sk_state);
73
74 bh_lock_sock(sk);
75 sk->sk_err = ETIMEDOUT;
76 sk->sk_state_change(sk);
77 bh_unlock_sock(sk);
78
79 sco_sock_kill(sk);
80 sock_put(sk);
81}
82
83static void sco_sock_set_timer(struct sock *sk, long timeout)
84{
85 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
86 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
87}
88
89static void sco_sock_clear_timer(struct sock *sk)
90{
91 BT_DBG("sock %p state %d", sk, sk->sk_state);
92 sk_stop_timer(sk, &sk->sk_timer);
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* ---- SCO connections ---- */
96static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
97{
98 struct hci_dev *hdev = hcon->hdev;
Marcel Holtmann25ea6db2006-07-06 15:40:09 +020099 struct sco_conn *conn = hcon->sco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200101 if (conn || status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return conn;
103
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200104 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
105 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 spin_lock_init(&conn->lock);
109
110 hcon->sco_data = conn;
111 conn->hcon = hcon;
112
113 conn->src = &hdev->bdaddr;
114 conn->dst = &hcon->dst;
115
116 if (hdev->sco_mtu > 0)
117 conn->mtu = hdev->sco_mtu;
118 else
119 conn->mtu = 60;
120
121 BT_DBG("hcon %p conn %p", hcon, conn);
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return conn;
124}
125
126static inline struct sock *sco_chan_get(struct sco_conn *conn)
127{
128 struct sock *sk = NULL;
129 sco_conn_lock(conn);
130 sk = conn->sk;
131 sco_conn_unlock(conn);
132 return sk;
133}
134
135static int sco_conn_del(struct hci_conn *hcon, int err)
136{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200137 struct sco_conn *conn = hcon->sco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct sock *sk;
139
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200140 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return 0;
142
143 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
144
145 /* Kill socket */
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200146 sk = sco_chan_get(conn);
147 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 bh_lock_sock(sk);
149 sco_sock_clear_timer(sk);
150 sco_chan_del(sk, err);
151 bh_unlock_sock(sk);
152 sco_sock_kill(sk);
153 }
154
155 hcon->sco_data = NULL;
156 kfree(conn);
157 return 0;
158}
159
160static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
161{
162 int err = 0;
163
164 sco_conn_lock(conn);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300165 if (conn->sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 err = -EBUSY;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300167 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 __sco_chan_add(conn, sk, parent);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 sco_conn_unlock(conn);
171 return err;
172}
173
174static int sco_connect(struct sock *sk)
175{
176 bdaddr_t *src = &bt_sk(sk)->src;
177 bdaddr_t *dst = &bt_sk(sk)->dst;
178 struct sco_conn *conn;
179 struct hci_conn *hcon;
180 struct hci_dev *hdev;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200181 int err, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 BT_DBG("%s -> %s", batostr(src), batostr(dst));
184
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200185 hdev = hci_get_route(dst, src);
186 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EHOSTUNREACH;
188
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300189 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Marcel Holtmann7cb127d2008-07-14 20:13:53 +0200191 if (lmp_esco_capable(hdev) && !disable_esco)
192 type = ESCO_LINK;
193 else
194 type = SCO_LINK;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200195
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100196 hcon = hci_connect(hdev, type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
Ville Tervo30e76272011-02-22 16:10:53 -0300197 if (IS_ERR(hcon)) {
198 err = PTR_ERR(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto done;
Ville Tervo30e76272011-02-22 16:10:53 -0300200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 conn = sco_conn_add(hcon, 0);
203 if (!conn) {
204 hci_conn_put(hcon);
Ville Tervo30e76272011-02-22 16:10:53 -0300205 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 goto done;
207 }
208
209 /* Update source addr of the socket */
210 bacpy(src, conn->src);
211
212 err = sco_chan_add(conn, sk, NULL);
213 if (err)
214 goto done;
215
216 if (hcon->state == BT_CONNECTED) {
217 sco_sock_clear_timer(sk);
218 sk->sk_state = BT_CONNECTED;
219 } else {
220 sk->sk_state = BT_CONNECT;
221 sco_sock_set_timer(sk, sk->sk_sndtimeo);
222 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300225 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 hci_dev_put(hdev);
227 return err;
228}
229
230static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
231{
232 struct sco_conn *conn = sco_pi(sk)->conn;
233 struct sk_buff *skb;
Mikel Astiz088ce082012-04-11 08:48:48 +0200234 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /* Check outgoing MTU */
237 if (len > conn->mtu)
238 return -EINVAL;
239
240 BT_DBG("sk %p len %d", sk, len);
241
Mikel Astiz088ce082012-04-11 08:48:48 +0200242 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300243 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return err;
245
Mikel Astiz088ce082012-04-11 08:48:48 +0200246 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300247 kfree_skb(skb);
248 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -0300251 hci_send_sco(conn->hcon, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Mikel Astiz088ce082012-04-11 08:48:48 +0200253 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
257{
258 struct sock *sk = sco_chan_get(conn);
259
260 if (!sk)
261 goto drop;
262
263 BT_DBG("sk %p len %d", sk, skb->len);
264
265 if (sk->sk_state != BT_CONNECTED)
266 goto drop;
267
268 if (!sock_queue_rcv_skb(sk, skb))
269 return;
270
271drop:
272 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/* -------- Socket interface ---------- */
276static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
277{
278 struct sock *sk;
279 struct hlist_node *node;
280
281 sk_for_each(sk, node, &sco_sk_list.head)
282 if (!bacmp(&bt_sk(sk)->src, ba))
283 goto found;
284 sk = NULL;
285found:
286 return sk;
287}
288
289/* Find socket listening on source bdaddr.
290 * Returns closest match.
291 */
292static struct sock *sco_get_sock_listen(bdaddr_t *src)
293{
294 struct sock *sk = NULL, *sk1 = NULL;
295 struct hlist_node *node;
296
297 read_lock(&sco_sk_list.lock);
298
299 sk_for_each(sk, node, &sco_sk_list.head) {
300 if (sk->sk_state != BT_LISTEN)
301 continue;
302
303 /* Exact match. */
304 if (!bacmp(&bt_sk(sk)->src, src))
305 break;
306
307 /* Closest match */
308 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
309 sk1 = sk;
310 }
311
312 read_unlock(&sco_sk_list.lock);
313
314 return node ? sk : sk1;
315}
316
317static void sco_sock_destruct(struct sock *sk)
318{
319 BT_DBG("sk %p", sk);
320
321 skb_queue_purge(&sk->sk_receive_queue);
322 skb_queue_purge(&sk->sk_write_queue);
323}
324
325static void sco_sock_cleanup_listen(struct sock *parent)
326{
327 struct sock *sk;
328
329 BT_DBG("parent %p", parent);
330
331 /* Close not yet accepted channels */
332 while ((sk = bt_accept_dequeue(parent, NULL))) {
333 sco_sock_close(sk);
334 sco_sock_kill(sk);
335 }
336
337 parent->sk_state = BT_CLOSED;
338 sock_set_flag(parent, SOCK_ZAPPED);
339}
340
341/* Kill socket (only if zapped and orphan)
342 * Must be called on unlocked socket.
343 */
344static void sco_sock_kill(struct sock *sk)
345{
346 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
347 return;
348
349 BT_DBG("sk %p state %d", sk, sk->sk_state);
350
351 /* Kill poor orphan */
352 bt_sock_unlink(&sco_sk_list, sk);
353 sock_set_flag(sk, SOCK_DEAD);
354 sock_put(sk);
355}
356
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200357static void __sco_sock_close(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200359 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 switch (sk->sk_state) {
362 case BT_LISTEN:
363 sco_sock_cleanup_listen(sk);
364 break;
365
366 case BT_CONNECTED:
367 case BT_CONFIG:
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300368 if (sco_pi(sk)->conn) {
369 sk->sk_state = BT_DISCONN;
370 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
371 hci_conn_put(sco_pi(sk)->conn->hcon);
372 sco_pi(sk)->conn->hcon = NULL;
373 } else
374 sco_chan_del(sk, ECONNRESET);
375 break;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 case BT_CONNECT:
378 case BT_DISCONN:
379 sco_chan_del(sk, ECONNRESET);
380 break;
381
382 default:
383 sock_set_flag(sk, SOCK_ZAPPED);
384 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700385 }
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200388/* Must be called on unlocked socket. */
389static void sco_sock_close(struct sock *sk)
390{
391 sco_sock_clear_timer(sk);
392 lock_sock(sk);
393 __sco_sock_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sco_sock_kill(sk);
396}
397
398static void sco_sock_init(struct sock *sk, struct sock *parent)
399{
400 BT_DBG("sk %p", sk);
401
Paul Moore6230c9b2011-10-07 09:40:59 +0000402 if (parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 sk->sk_type = parent->sk_type;
Paul Moore6230c9b2011-10-07 09:40:59 +0000404 security_sk_clone(parent, sk);
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408static struct proto sco_proto = {
409 .name = "SCO",
410 .owner = THIS_MODULE,
411 .obj_size = sizeof(struct sco_pinfo)
412};
413
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700414static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct sock *sk;
417
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700418 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!sk)
420 return NULL;
421
422 sock_init_data(sock, sk);
423 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
424
425 sk->sk_destruct = sco_sock_destruct;
426 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
427
428 sock_reset_flag(sk, SOCK_ZAPPED);
429
430 sk->sk_protocol = proto;
431 sk->sk_state = BT_OPEN;
432
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800433 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 bt_sock_link(&sco_sk_list, sk);
436 return sk;
437}
438
Eric Paris3f378b62009-11-05 22:18:14 -0800439static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
440 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct sock *sk;
443
444 BT_DBG("sock %p", sock);
445
446 sock->state = SS_UNCONNECTED;
447
448 if (sock->type != SOCK_SEQPACKET)
449 return -ESOCKTNOSUPPORT;
450
451 sock->ops = &sco_sock_ops;
452
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700453 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200454 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return -ENOMEM;
456
457 sco_sock_init(sk, NULL);
458 return 0;
459}
460
461static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
462{
463 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
464 struct sock *sk = sock->sk;
465 bdaddr_t *src = &sa->sco_bdaddr;
466 int err = 0;
467
468 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
469
470 if (!addr || addr->sa_family != AF_BLUETOOTH)
471 return -EINVAL;
472
473 lock_sock(sk);
474
475 if (sk->sk_state != BT_OPEN) {
476 err = -EBADFD;
477 goto done;
478 }
479
Gustavo F. Padovanee65d192011-12-27 15:28:46 -0200480 write_lock(&sco_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
483 err = -EADDRINUSE;
484 } else {
485 /* Save source address */
486 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
487 sk->sk_state = BT_BOUND;
488 }
489
Gustavo F. Padovanee65d192011-12-27 15:28:46 -0200490 write_unlock(&sco_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492done:
493 release_sock(sk);
494 return err;
495}
496
497static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
498{
499 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
500 struct sock *sk = sock->sk;
501 int err = 0;
502
503
504 BT_DBG("sk %p", sk);
505
Changli Gao6503d962010-03-31 22:58:26 +0000506 if (alen < sizeof(struct sockaddr_sco) ||
507 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return -EINVAL;
509
510 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
511 return -EBADFD;
512
513 if (sk->sk_type != SOCK_SEQPACKET)
514 return -EINVAL;
515
516 lock_sock(sk);
517
518 /* Set destination address and psm */
519 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
520
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200521 err = sco_connect(sk);
522 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto done;
524
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900525 err = bt_sock_wait_state(sk, BT_CONNECTED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 sock_sndtimeo(sk, flags & O_NONBLOCK));
527
528done:
529 release_sock(sk);
530 return err;
531}
532
533static int sco_sock_listen(struct socket *sock, int backlog)
534{
535 struct sock *sk = sock->sk;
536 int err = 0;
537
538 BT_DBG("sk %p backlog %d", sk, backlog);
539
540 lock_sock(sk);
541
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200542 if (sk->sk_state != BT_BOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 err = -EBADFD;
544 goto done;
545 }
546
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200547 if (sk->sk_type != SOCK_SEQPACKET) {
548 err = -EINVAL;
549 goto done;
550 }
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 sk->sk_max_ack_backlog = backlog;
553 sk->sk_ack_backlog = 0;
554 sk->sk_state = BT_LISTEN;
555
556done:
557 release_sock(sk);
558 return err;
559}
560
561static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
562{
563 DECLARE_WAITQUEUE(wait, current);
564 struct sock *sk = sock->sk, *ch;
565 long timeo;
566 int err = 0;
567
568 lock_sock(sk);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
571
572 BT_DBG("sk %p timeo %ld", sk, timeo);
573
574 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000575 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurley552b0d32011-07-24 00:11:01 -0400576 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (sk->sk_state != BT_LISTEN) {
580 err = -EBADFD;
581 break;
582 }
583
Peter Hurley552b0d32011-07-24 00:11:01 -0400584 ch = bt_accept_dequeue(sk, newsock);
585 if (ch)
586 break;
587
588 if (!timeo) {
589 err = -EAGAIN;
590 break;
591 }
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (signal_pending(current)) {
594 err = sock_intr_errno(timeo);
595 break;
596 }
Peter Hurley552b0d32011-07-24 00:11:01 -0400597
598 release_sock(sk);
599 timeo = schedule_timeout(timeo);
600 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Peter Hurley552b0d32011-07-24 00:11:01 -0400602 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000603 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (err)
606 goto done;
607
608 newsock->state = SS_CONNECTED;
609
610 BT_DBG("new socket %p", ch);
611
612done:
613 release_sock(sk);
614 return err;
615}
616
617static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
618{
619 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
620 struct sock *sk = sock->sk;
621
622 BT_DBG("sock %p, sk %p", sock, sk);
623
624 addr->sa_family = AF_BLUETOOTH;
625 *len = sizeof(struct sockaddr_sco);
626
627 if (peer)
628 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
629 else
630 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
631
632 return 0;
633}
634
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900635static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct msghdr *msg, size_t len)
637{
638 struct sock *sk = sock->sk;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300639 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 BT_DBG("sock %p, sk %p", sock, sk);
642
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800643 err = sock_error(sk);
644 if (err)
645 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if (msg->msg_flags & MSG_OOB)
648 return -EOPNOTSUPP;
649
650 lock_sock(sk);
651
652 if (sk->sk_state == BT_CONNECTED)
653 err = sco_send_frame(sk, msg, len);
654 else
655 err = -ENOTCONN;
656
657 release_sock(sk);
658 return err;
659}
660
David S. Millerb7058842009-09-30 16:12:20 -0700661static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct sock *sk = sock->sk;
664 int err = 0;
665
666 BT_DBG("sk %p", sk);
667
668 lock_sock(sk);
669
670 switch (optname) {
671 default:
672 err = -ENOPROTOOPT;
673 break;
674 }
675
676 release_sock(sk);
677 return err;
678}
679
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100680static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct sock *sk = sock->sk;
683 struct sco_options opts;
684 struct sco_conninfo cinfo;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900685 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 BT_DBG("sk %p", sk);
688
689 if (get_user(len, optlen))
690 return -EFAULT;
691
692 lock_sock(sk);
693
694 switch (optname) {
695 case SCO_OPTIONS:
696 if (sk->sk_state != BT_CONNECTED) {
697 err = -ENOTCONN;
698 break;
699 }
700
701 opts.mtu = sco_pi(sk)->conn->mtu;
702
703 BT_DBG("mtu %d", opts.mtu);
704
705 len = min_t(unsigned int, len, sizeof(opts));
706 if (copy_to_user(optval, (char *)&opts, len))
707 err = -EFAULT;
708
709 break;
710
711 case SCO_CONNINFO:
712 if (sk->sk_state != BT_CONNECTED) {
713 err = -ENOTCONN;
714 break;
715 }
716
Vasiliy Kulikovc4c896e2011-02-14 13:54:26 +0300717 memset(&cinfo, 0, sizeof(cinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
719 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
720
721 len = min_t(unsigned int, len, sizeof(cinfo));
722 if (copy_to_user(optval, (char *)&cinfo, len))
723 err = -EFAULT;
724
725 break;
726
727 default:
728 err = -ENOPROTOOPT;
729 break;
730 }
731
732 release_sock(sk);
733 return err;
734}
735
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100736static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
737{
738 struct sock *sk = sock->sk;
739 int len, err = 0;
740
741 BT_DBG("sk %p", sk);
742
743 if (level == SOL_SCO)
744 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
745
746 if (get_user(len, optlen))
747 return -EFAULT;
748
749 lock_sock(sk);
750
751 switch (optname) {
752 default:
753 err = -ENOPROTOOPT;
754 break;
755 }
756
757 release_sock(sk);
758 return err;
759}
760
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200761static int sco_sock_shutdown(struct socket *sock, int how)
762{
763 struct sock *sk = sock->sk;
764 int err = 0;
765
766 BT_DBG("sock %p, sk %p", sock, sk);
767
768 if (!sk)
769 return 0;
770
771 lock_sock(sk);
772 if (!sk->sk_shutdown) {
773 sk->sk_shutdown = SHUTDOWN_MASK;
774 sco_sock_clear_timer(sk);
775 __sco_sock_close(sk);
776
777 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
778 err = bt_sock_wait_state(sk, BT_CLOSED,
779 sk->sk_lingertime);
780 }
781 release_sock(sk);
782 return err;
783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static int sco_sock_release(struct socket *sock)
786{
787 struct sock *sk = sock->sk;
788 int err = 0;
789
790 BT_DBG("sock %p, sk %p", sock, sk);
791
792 if (!sk)
793 return 0;
794
795 sco_sock_close(sk);
796
797 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
798 lock_sock(sk);
799 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
800 release_sock(sk);
801 }
802
803 sock_orphan(sk);
804 sco_sock_kill(sk);
805 return err;
806}
807
808static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
809{
810 BT_DBG("conn %p", conn);
811
812 sco_pi(sk)->conn = conn;
813 conn->sk = sk;
814
815 if (parent)
816 bt_accept_enqueue(parent, sk);
817}
818
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900819/* Delete channel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * Must be called on the locked socket. */
821static void sco_chan_del(struct sock *sk, int err)
822{
823 struct sco_conn *conn;
824
825 conn = sco_pi(sk)->conn;
826
827 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
828
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900829 if (conn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 sco_conn_lock(conn);
831 conn->sk = NULL;
832 sco_pi(sk)->conn = NULL;
833 sco_conn_unlock(conn);
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300834
835 if (conn->hcon)
836 hci_conn_put(conn->hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
839 sk->sk_state = BT_CLOSED;
840 sk->sk_err = err;
841 sk->sk_state_change(sk);
842
843 sock_set_flag(sk, SOCK_ZAPPED);
844}
845
846static void sco_conn_ready(struct sco_conn *conn)
847{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200848 struct sock *parent;
849 struct sock *sk = conn->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 BT_DBG("conn %p", conn);
852
853 sco_conn_lock(conn);
854
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200855 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 sco_sock_clear_timer(sk);
857 bh_lock_sock(sk);
858 sk->sk_state = BT_CONNECTED;
859 sk->sk_state_change(sk);
860 bh_unlock_sock(sk);
861 } else {
862 parent = sco_get_sock_listen(conn->src);
863 if (!parent)
864 goto done;
865
866 bh_lock_sock(parent);
867
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300868 sk = sco_sock_alloc(sock_net(parent), NULL,
869 BTPROTO_SCO, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!sk) {
871 bh_unlock_sock(parent);
872 goto done;
873 }
874
875 sco_sock_init(sk, parent);
876
877 bacpy(&bt_sk(sk)->src, conn->src);
878 bacpy(&bt_sk(sk)->dst, conn->dst);
879
880 hci_conn_hold(conn->hcon);
881 __sco_chan_add(conn, sk, parent);
882
883 sk->sk_state = BT_CONNECTED;
884
885 /* Wake up parent */
886 parent->sk_data_ready(parent, 1);
887
888 bh_unlock_sock(parent);
889 }
890
891done:
892 sco_conn_unlock(conn);
893}
894
895/* ----- SCO interface with lower layer (HCI) ----- */
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200896int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100898 register struct sock *sk;
899 struct hlist_node *node;
900 int lm = 0;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
903
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100904 /* Find listening sockets */
905 read_lock(&sco_sk_list.lock);
906 sk_for_each(sk, node, &sco_sk_list.head) {
907 if (sk->sk_state != BT_LISTEN)
908 continue;
909
910 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
911 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
912 lm |= HCI_LM_ACCEPT;
913 break;
914 }
915 }
916 read_unlock(&sco_sk_list.lock);
917
918 return lm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200921int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (!status) {
925 struct sco_conn *conn;
926
927 conn = sco_conn_add(hcon, status);
928 if (conn)
929 sco_conn_ready(conn);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900930 } else
Joe Perchese1750722011-06-29 18:18:29 -0700931 sco_conn_del(hcon, bt_to_errno(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 return 0;
934}
935
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200936int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 BT_DBG("hcon %p reason %d", hcon, reason);
939
Joe Perchese1750722011-06-29 18:18:29 -0700940 sco_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return 0;
942}
943
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200944int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 struct sco_conn *conn = hcon->sco_data;
947
948 if (!conn)
949 goto drop;
950
951 BT_DBG("conn %p len %d", conn, skb->len);
952
953 if (skb->len) {
954 sco_recv_frame(conn, skb);
955 return 0;
956 }
957
958drop:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900959 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 0;
961}
962
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100963static int sco_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 struct sock *sk;
966 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Gustavo F. Padovanee65d192011-12-27 15:28:46 -0200968 read_lock(&sco_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800970 sk_for_each(sk, node, &sco_sk_list.head) {
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100971 seq_printf(f, "%s %s %d\n", batostr(&bt_sk(sk)->src),
972 batostr(&bt_sk(sk)->dst), sk->sk_state);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Gustavo F. Padovanee65d192011-12-27 15:28:46 -0200975 read_unlock(&sco_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800976
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100980static int sco_debugfs_open(struct inode *inode, struct file *file)
981{
982 return single_open(file, sco_debugfs_show, inode->i_private);
983}
984
985static const struct file_operations sco_debugfs_fops = {
986 .open = sco_debugfs_open,
987 .read = seq_read,
988 .llseek = seq_lseek,
989 .release = single_release,
990};
991
992static struct dentry *sco_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800994static const struct proto_ops sco_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 .family = PF_BLUETOOTH,
996 .owner = THIS_MODULE,
997 .release = sco_sock_release,
998 .bind = sco_sock_bind,
999 .connect = sco_sock_connect,
1000 .listen = sco_sock_listen,
1001 .accept = sco_sock_accept,
1002 .getname = sco_sock_getname,
1003 .sendmsg = sco_sock_sendmsg,
1004 .recvmsg = bt_sock_recvmsg,
1005 .poll = bt_sock_poll,
Marcel Holtmann3241ad82008-07-14 20:13:50 +02001006 .ioctl = bt_sock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 .mmap = sock_no_mmap,
1008 .socketpair = sock_no_socketpair,
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +02001009 .shutdown = sco_sock_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 .setsockopt = sco_sock_setsockopt,
1011 .getsockopt = sco_sock_getsockopt
1012};
1013
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001014static const struct net_proto_family sco_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 .family = PF_BLUETOOTH,
1016 .owner = THIS_MODULE,
1017 .create = sco_sock_create,
1018};
1019
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001020int __init sco_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 int err;
1023
1024 err = proto_register(&sco_proto, 0);
1025 if (err < 0)
1026 return err;
1027
1028 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1029 if (err < 0) {
1030 BT_ERR("SCO socket registration failed");
1031 goto error;
1032 }
1033
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001034 if (bt_debugfs) {
1035 sco_debugfs = debugfs_create_file("sco", 0444,
1036 bt_debugfs, NULL, &sco_debugfs_fops);
1037 if (!sco_debugfs)
1038 BT_ERR("Failed to create SCO debug file");
1039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 BT_INFO("SCO socket layer initialized");
1042
1043 return 0;
1044
1045error:
1046 proto_unregister(&sco_proto);
1047 return err;
1048}
1049
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001050void __exit sco_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001052 debugfs_remove(sco_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1055 BT_ERR("SCO socket unregistration failed");
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 proto_unregister(&sco_proto);
1058}
1059
Marcel Holtmann7cb127d2008-07-14 20:13:53 +02001060module_param(disable_esco, bool, 0644);
1061MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");