blob: 0ef5a78a889f8328fbc2489a8994f21ce4202870 [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>
Marcel Holtmannaef7d972010-03-21 05:27:45 +010028#include <linux/debugfs.h>
29#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <net/bluetooth/bluetooth.h>
32#include <net/bluetooth/hci_core.h>
33#include <net/bluetooth/sco.h>
34
Rusty Russelleb939922011-12-19 14:08:01 +000035static bool disable_esco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080037static const struct proto_ops sco_sock_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static struct bt_sock_list sco_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070040 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
43static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
44static void sco_chan_del(struct sock *sk, int err);
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static void sco_sock_close(struct sock *sk);
47static void sco_sock_kill(struct sock *sk);
48
49/* ---- SCO timers ---- */
50static void sco_sock_timeout(unsigned long arg)
51{
52 struct sock *sk = (struct sock *) arg;
53
54 BT_DBG("sock %p state %d", sk, sk->sk_state);
55
56 bh_lock_sock(sk);
57 sk->sk_err = ETIMEDOUT;
58 sk->sk_state_change(sk);
59 bh_unlock_sock(sk);
60
61 sco_sock_kill(sk);
62 sock_put(sk);
63}
64
65static void sco_sock_set_timer(struct sock *sk, long timeout)
66{
67 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
68 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
69}
70
71static void sco_sock_clear_timer(struct sock *sk)
72{
73 BT_DBG("sock %p state %d", sk, sk->sk_state);
74 sk_stop_timer(sk, &sk->sk_timer);
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* ---- SCO connections ---- */
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +020078static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 struct hci_dev *hdev = hcon->hdev;
Marcel Holtmann25ea6db2006-07-06 15:40:09 +020081 struct sco_conn *conn = hcon->sco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +020083 if (conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return conn;
85
Marcel Holtmann25ea6db2006-07-06 15:40:09 +020086 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
87 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 spin_lock_init(&conn->lock);
91
92 hcon->sco_data = conn;
93 conn->hcon = hcon;
94
95 conn->src = &hdev->bdaddr;
96 conn->dst = &hcon->dst;
97
98 if (hdev->sco_mtu > 0)
99 conn->mtu = hdev->sco_mtu;
100 else
101 conn->mtu = 60;
102
103 BT_DBG("hcon %p conn %p", hcon, conn);
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return conn;
106}
107
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300108static struct sock *sco_chan_get(struct sco_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct sock *sk = NULL;
111 sco_conn_lock(conn);
112 sk = conn->sk;
113 sco_conn_unlock(conn);
114 return sk;
115}
116
117static int sco_conn_del(struct hci_conn *hcon, int err)
118{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200119 struct sco_conn *conn = hcon->sco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct sock *sk;
121
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200122 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124
125 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
126
127 /* Kill socket */
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200128 sk = sco_chan_get(conn);
129 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 bh_lock_sock(sk);
131 sco_sock_clear_timer(sk);
132 sco_chan_del(sk, err);
133 bh_unlock_sock(sk);
134 sco_sock_kill(sk);
135 }
136
137 hcon->sco_data = NULL;
138 kfree(conn);
139 return 0;
140}
141
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300142static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
143 struct sock *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int err = 0;
146
147 sco_conn_lock(conn);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300148 if (conn->sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 err = -EBUSY;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300150 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 __sco_chan_add(conn, sk, parent);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 sco_conn_unlock(conn);
154 return err;
155}
156
157static int sco_connect(struct sock *sk)
158{
159 bdaddr_t *src = &bt_sk(sk)->src;
160 bdaddr_t *dst = &bt_sk(sk)->dst;
161 struct sco_conn *conn;
162 struct hci_conn *hcon;
163 struct hci_dev *hdev;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200164 int err, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 BT_DBG("%s -> %s", batostr(src), batostr(dst));
167
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200168 hdev = hci_get_route(dst, src);
169 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -EHOSTUNREACH;
171
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300172 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Marcel Holtmann7cb127d2008-07-14 20:13:53 +0200174 if (lmp_esco_capable(hdev) && !disable_esco)
175 type = ESCO_LINK;
176 else
177 type = SCO_LINK;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200178
Andre Guedesb12f62c2012-04-24 21:02:54 -0300179 hcon = hci_connect(hdev, type, dst, BDADDR_BREDR, BT_SECURITY_LOW,
180 HCI_AT_NO_BONDING);
Ville Tervo30e76272011-02-22 16:10:53 -0300181 if (IS_ERR(hcon)) {
182 err = PTR_ERR(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 goto done;
Ville Tervo30e76272011-02-22 16:10:53 -0300184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +0200186 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (!conn) {
188 hci_conn_put(hcon);
Ville Tervo30e76272011-02-22 16:10:53 -0300189 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto done;
191 }
192
193 /* Update source addr of the socket */
194 bacpy(src, conn->src);
195
196 err = sco_chan_add(conn, sk, NULL);
197 if (err)
198 goto done;
199
200 if (hcon->state == BT_CONNECTED) {
201 sco_sock_clear_timer(sk);
202 sk->sk_state = BT_CONNECTED;
203 } else {
204 sk->sk_state = BT_CONNECT;
205 sco_sock_set_timer(sk, sk->sk_sndtimeo);
206 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300209 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 hci_dev_put(hdev);
211 return err;
212}
213
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300214static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 struct sco_conn *conn = sco_pi(sk)->conn;
217 struct sk_buff *skb;
Mikel Astiz088ce082012-04-11 08:48:48 +0200218 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* Check outgoing MTU */
221 if (len > conn->mtu)
222 return -EINVAL;
223
224 BT_DBG("sk %p len %d", sk, len);
225
Mikel Astiz088ce082012-04-11 08:48:48 +0200226 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300227 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return err;
229
Mikel Astiz088ce082012-04-11 08:48:48 +0200230 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300231 kfree_skb(skb);
232 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -0300235 hci_send_sco(conn->hcon, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Mikel Astiz088ce082012-04-11 08:48:48 +0200237 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300240static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct sock *sk = sco_chan_get(conn);
243
244 if (!sk)
245 goto drop;
246
247 BT_DBG("sk %p len %d", sk, skb->len);
248
249 if (sk->sk_state != BT_CONNECTED)
250 goto drop;
251
252 if (!sock_queue_rcv_skb(sk, skb))
253 return;
254
255drop:
256 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259/* -------- Socket interface ---------- */
Marcel Holtmannfb334052012-04-19 14:37:58 +0200260static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct hlist_node *node;
Marcel Holtmannfb334052012-04-19 14:37:58 +0200263 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Marcel Holtmannfb334052012-04-19 14:37:58 +0200265 sk_for_each(sk, node, &sco_sk_list.head) {
266 if (sk->sk_state != BT_LISTEN)
267 continue;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!bacmp(&bt_sk(sk)->src, ba))
Marcel Holtmannfb334052012-04-19 14:37:58 +0200270 return sk;
271 }
272
273 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/* Find socket listening on source bdaddr.
277 * Returns closest match.
278 */
279static struct sock *sco_get_sock_listen(bdaddr_t *src)
280{
281 struct sock *sk = NULL, *sk1 = NULL;
282 struct hlist_node *node;
283
284 read_lock(&sco_sk_list.lock);
285
286 sk_for_each(sk, node, &sco_sk_list.head) {
287 if (sk->sk_state != BT_LISTEN)
288 continue;
289
290 /* Exact match. */
291 if (!bacmp(&bt_sk(sk)->src, src))
292 break;
293
294 /* Closest match */
295 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
296 sk1 = sk;
297 }
298
299 read_unlock(&sco_sk_list.lock);
300
301 return node ? sk : sk1;
302}
303
304static void sco_sock_destruct(struct sock *sk)
305{
306 BT_DBG("sk %p", sk);
307
308 skb_queue_purge(&sk->sk_receive_queue);
309 skb_queue_purge(&sk->sk_write_queue);
310}
311
312static void sco_sock_cleanup_listen(struct sock *parent)
313{
314 struct sock *sk;
315
316 BT_DBG("parent %p", parent);
317
318 /* Close not yet accepted channels */
319 while ((sk = bt_accept_dequeue(parent, NULL))) {
320 sco_sock_close(sk);
321 sco_sock_kill(sk);
322 }
323
324 parent->sk_state = BT_CLOSED;
325 sock_set_flag(parent, SOCK_ZAPPED);
326}
327
328/* Kill socket (only if zapped and orphan)
329 * Must be called on unlocked socket.
330 */
331static void sco_sock_kill(struct sock *sk)
332{
333 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
334 return;
335
336 BT_DBG("sk %p state %d", sk, sk->sk_state);
337
338 /* Kill poor orphan */
339 bt_sock_unlink(&sco_sk_list, sk);
340 sock_set_flag(sk, SOCK_DEAD);
341 sock_put(sk);
342}
343
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200344static void __sco_sock_close(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200346 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 switch (sk->sk_state) {
349 case BT_LISTEN:
350 sco_sock_cleanup_listen(sk);
351 break;
352
353 case BT_CONNECTED:
354 case BT_CONFIG:
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300355 if (sco_pi(sk)->conn) {
356 sk->sk_state = BT_DISCONN;
357 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
358 hci_conn_put(sco_pi(sk)->conn->hcon);
359 sco_pi(sk)->conn->hcon = NULL;
360 } else
361 sco_chan_del(sk, ECONNRESET);
362 break;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 case BT_CONNECT:
365 case BT_DISCONN:
366 sco_chan_del(sk, ECONNRESET);
367 break;
368
369 default:
370 sock_set_flag(sk, SOCK_ZAPPED);
371 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700372 }
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200373}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200375/* Must be called on unlocked socket. */
376static void sco_sock_close(struct sock *sk)
377{
378 sco_sock_clear_timer(sk);
379 lock_sock(sk);
380 __sco_sock_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 sco_sock_kill(sk);
383}
384
385static void sco_sock_init(struct sock *sk, struct sock *parent)
386{
387 BT_DBG("sk %p", sk);
388
Paul Moore6230c9b2011-10-07 09:40:59 +0000389 if (parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 sk->sk_type = parent->sk_type;
Paul Moore6230c9b2011-10-07 09:40:59 +0000391 security_sk_clone(parent, sk);
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395static struct proto sco_proto = {
396 .name = "SCO",
397 .owner = THIS_MODULE,
398 .obj_size = sizeof(struct sco_pinfo)
399};
400
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700401static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct sock *sk;
404
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700405 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!sk)
407 return NULL;
408
409 sock_init_data(sock, sk);
410 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
411
412 sk->sk_destruct = sco_sock_destruct;
413 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
414
415 sock_reset_flag(sk, SOCK_ZAPPED);
416
417 sk->sk_protocol = proto;
418 sk->sk_state = BT_OPEN;
419
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800420 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 bt_sock_link(&sco_sk_list, sk);
423 return sk;
424}
425
Eric Paris3f378b62009-11-05 22:18:14 -0800426static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
427 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct sock *sk;
430
431 BT_DBG("sock %p", sock);
432
433 sock->state = SS_UNCONNECTED;
434
435 if (sock->type != SOCK_SEQPACKET)
436 return -ESOCKTNOSUPPORT;
437
438 sock->ops = &sco_sock_ops;
439
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700440 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200441 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -ENOMEM;
443
444 sco_sock_init(sk, NULL);
445 return 0;
446}
447
448static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
449{
450 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
451 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int err = 0;
453
454 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
455
456 if (!addr || addr->sa_family != AF_BLUETOOTH)
457 return -EINVAL;
458
459 lock_sock(sk);
460
461 if (sk->sk_state != BT_OPEN) {
462 err = -EBADFD;
463 goto done;
464 }
465
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200466 if (sk->sk_type != SOCK_SEQPACKET) {
467 err = -EINVAL;
468 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200471 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
472
473 sk->sk_state = BT_BOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475done:
476 release_sock(sk);
477 return err;
478}
479
480static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
481{
482 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
483 struct sock *sk = sock->sk;
484 int err = 0;
485
486
487 BT_DBG("sk %p", sk);
488
Changli Gao6503d962010-03-31 22:58:26 +0000489 if (alen < sizeof(struct sockaddr_sco) ||
490 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EINVAL;
492
493 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
494 return -EBADFD;
495
496 if (sk->sk_type != SOCK_SEQPACKET)
497 return -EINVAL;
498
499 lock_sock(sk);
500
501 /* Set destination address and psm */
502 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
503
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200504 err = sco_connect(sk);
505 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto done;
507
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900508 err = bt_sock_wait_state(sk, BT_CONNECTED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300509 sock_sndtimeo(sk, flags & O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511done:
512 release_sock(sk);
513 return err;
514}
515
516static int sco_sock_listen(struct socket *sock, int backlog)
517{
518 struct sock *sk = sock->sk;
Marcel Holtmannfb334052012-04-19 14:37:58 +0200519 bdaddr_t *src = &bt_sk(sk)->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int err = 0;
521
522 BT_DBG("sk %p backlog %d", sk, backlog);
523
524 lock_sock(sk);
525
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200526 if (sk->sk_state != BT_BOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 err = -EBADFD;
528 goto done;
529 }
530
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200531 if (sk->sk_type != SOCK_SEQPACKET) {
532 err = -EINVAL;
533 goto done;
534 }
535
Marcel Holtmannfb334052012-04-19 14:37:58 +0200536 write_lock(&sco_sk_list.lock);
537
538 if (__sco_get_sock_listen_by_addr(src)) {
539 err = -EADDRINUSE;
540 goto unlock;
541 }
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 sk->sk_max_ack_backlog = backlog;
544 sk->sk_ack_backlog = 0;
Marcel Holtmannfb334052012-04-19 14:37:58 +0200545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 sk->sk_state = BT_LISTEN;
547
Marcel Holtmannfb334052012-04-19 14:37:58 +0200548unlock:
549 write_unlock(&sco_sk_list.lock);
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551done:
552 release_sock(sk);
553 return err;
554}
555
556static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
557{
558 DECLARE_WAITQUEUE(wait, current);
559 struct sock *sk = sock->sk, *ch;
560 long timeo;
561 int err = 0;
562
563 lock_sock(sk);
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
566
567 BT_DBG("sk %p timeo %ld", sk, timeo);
568
569 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000570 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurley552b0d32011-07-24 00:11:01 -0400571 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (sk->sk_state != BT_LISTEN) {
575 err = -EBADFD;
576 break;
577 }
578
Peter Hurley552b0d32011-07-24 00:11:01 -0400579 ch = bt_accept_dequeue(sk, newsock);
580 if (ch)
581 break;
582
583 if (!timeo) {
584 err = -EAGAIN;
585 break;
586 }
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (signal_pending(current)) {
589 err = sock_intr_errno(timeo);
590 break;
591 }
Peter Hurley552b0d32011-07-24 00:11:01 -0400592
593 release_sock(sk);
594 timeo = schedule_timeout(timeo);
595 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Peter Hurley552b0d32011-07-24 00:11:01 -0400597 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000598 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (err)
601 goto done;
602
603 newsock->state = SS_CONNECTED;
604
605 BT_DBG("new socket %p", ch);
606
607done:
608 release_sock(sk);
609 return err;
610}
611
612static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
613{
614 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
615 struct sock *sk = sock->sk;
616
617 BT_DBG("sock %p, sk %p", sock, sk);
618
619 addr->sa_family = AF_BLUETOOTH;
620 *len = sizeof(struct sockaddr_sco);
621
622 if (peer)
623 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
624 else
625 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
626
627 return 0;
628}
629
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900630static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct msghdr *msg, size_t len)
632{
633 struct sock *sk = sock->sk;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300634 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 BT_DBG("sock %p, sk %p", sock, sk);
637
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800638 err = sock_error(sk);
639 if (err)
640 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (msg->msg_flags & MSG_OOB)
643 return -EOPNOTSUPP;
644
645 lock_sock(sk);
646
647 if (sk->sk_state == BT_CONNECTED)
648 err = sco_send_frame(sk, msg, len);
649 else
650 err = -ENOTCONN;
651
652 release_sock(sk);
653 return err;
654}
655
David S. Millerb7058842009-09-30 16:12:20 -0700656static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 struct sock *sk = sock->sk;
659 int err = 0;
660
661 BT_DBG("sk %p", sk);
662
663 lock_sock(sk);
664
665 switch (optname) {
666 default:
667 err = -ENOPROTOOPT;
668 break;
669 }
670
671 release_sock(sk);
672 return err;
673}
674
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100675static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct sock *sk = sock->sk;
678 struct sco_options opts;
679 struct sco_conninfo cinfo;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900680 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 BT_DBG("sk %p", sk);
683
684 if (get_user(len, optlen))
685 return -EFAULT;
686
687 lock_sock(sk);
688
689 switch (optname) {
690 case SCO_OPTIONS:
691 if (sk->sk_state != BT_CONNECTED) {
692 err = -ENOTCONN;
693 break;
694 }
695
696 opts.mtu = sco_pi(sk)->conn->mtu;
697
698 BT_DBG("mtu %d", opts.mtu);
699
700 len = min_t(unsigned int, len, sizeof(opts));
701 if (copy_to_user(optval, (char *)&opts, len))
702 err = -EFAULT;
703
704 break;
705
706 case SCO_CONNINFO:
707 if (sk->sk_state != BT_CONNECTED) {
708 err = -ENOTCONN;
709 break;
710 }
711
Vasiliy Kulikovc4c896e2011-02-14 13:54:26 +0300712 memset(&cinfo, 0, sizeof(cinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
714 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
715
716 len = min_t(unsigned int, len, sizeof(cinfo));
717 if (copy_to_user(optval, (char *)&cinfo, len))
718 err = -EFAULT;
719
720 break;
721
722 default:
723 err = -ENOPROTOOPT;
724 break;
725 }
726
727 release_sock(sk);
728 return err;
729}
730
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100731static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
732{
733 struct sock *sk = sock->sk;
734 int len, err = 0;
735
736 BT_DBG("sk %p", sk);
737
738 if (level == SOL_SCO)
739 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
740
741 if (get_user(len, optlen))
742 return -EFAULT;
743
744 lock_sock(sk);
745
746 switch (optname) {
747 default:
748 err = -ENOPROTOOPT;
749 break;
750 }
751
752 release_sock(sk);
753 return err;
754}
755
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200756static int sco_sock_shutdown(struct socket *sock, int how)
757{
758 struct sock *sk = sock->sk;
759 int err = 0;
760
761 BT_DBG("sock %p, sk %p", sock, sk);
762
763 if (!sk)
764 return 0;
765
766 lock_sock(sk);
767 if (!sk->sk_shutdown) {
768 sk->sk_shutdown = SHUTDOWN_MASK;
769 sco_sock_clear_timer(sk);
770 __sco_sock_close(sk);
771
772 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
773 err = bt_sock_wait_state(sk, BT_CLOSED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300774 sk->sk_lingertime);
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200775 }
776 release_sock(sk);
777 return err;
778}
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780static int sco_sock_release(struct socket *sock)
781{
782 struct sock *sk = sock->sk;
783 int err = 0;
784
785 BT_DBG("sock %p, sk %p", sock, sk);
786
787 if (!sk)
788 return 0;
789
790 sco_sock_close(sk);
791
792 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
793 lock_sock(sk);
794 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
795 release_sock(sk);
796 }
797
798 sock_orphan(sk);
799 sco_sock_kill(sk);
800 return err;
801}
802
803static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
804{
805 BT_DBG("conn %p", conn);
806
807 sco_pi(sk)->conn = conn;
808 conn->sk = sk;
809
810 if (parent)
811 bt_accept_enqueue(parent, sk);
812}
813
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900814/* Delete channel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * Must be called on the locked socket. */
816static void sco_chan_del(struct sock *sk, int err)
817{
818 struct sco_conn *conn;
819
820 conn = sco_pi(sk)->conn;
821
822 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
823
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900824 if (conn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 sco_conn_lock(conn);
826 conn->sk = NULL;
827 sco_pi(sk)->conn = NULL;
828 sco_conn_unlock(conn);
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300829
830 if (conn->hcon)
831 hci_conn_put(conn->hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
834 sk->sk_state = BT_CLOSED;
835 sk->sk_err = err;
836 sk->sk_state_change(sk);
837
838 sock_set_flag(sk, SOCK_ZAPPED);
839}
840
841static void sco_conn_ready(struct sco_conn *conn)
842{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200843 struct sock *parent;
844 struct sock *sk = conn->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 BT_DBG("conn %p", conn);
847
848 sco_conn_lock(conn);
849
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200850 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 sco_sock_clear_timer(sk);
852 bh_lock_sock(sk);
853 sk->sk_state = BT_CONNECTED;
854 sk->sk_state_change(sk);
855 bh_unlock_sock(sk);
856 } else {
857 parent = sco_get_sock_listen(conn->src);
858 if (!parent)
859 goto done;
860
861 bh_lock_sock(parent);
862
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300863 sk = sco_sock_alloc(sock_net(parent), NULL,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300864 BTPROTO_SCO, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (!sk) {
866 bh_unlock_sock(parent);
867 goto done;
868 }
869
870 sco_sock_init(sk, parent);
871
872 bacpy(&bt_sk(sk)->src, conn->src);
873 bacpy(&bt_sk(sk)->dst, conn->dst);
874
875 hci_conn_hold(conn->hcon);
876 __sco_chan_add(conn, sk, parent);
877
878 sk->sk_state = BT_CONNECTED;
879
880 /* Wake up parent */
881 parent->sk_data_ready(parent, 1);
882
883 bh_unlock_sock(parent);
884 }
885
886done:
887 sco_conn_unlock(conn);
888}
889
890/* ----- SCO interface with lower layer (HCI) ----- */
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200891int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Gustavo Padovanfc5fef62012-05-23 04:04:19 -0300893 struct sock *sk;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100894 struct hlist_node *node;
895 int lm = 0;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
898
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100899 /* Find listening sockets */
900 read_lock(&sco_sk_list.lock);
901 sk_for_each(sk, node, &sco_sk_list.head) {
902 if (sk->sk_state != BT_LISTEN)
903 continue;
904
905 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300906 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100907 lm |= HCI_LM_ACCEPT;
908 break;
909 }
910 }
911 read_unlock(&sco_sk_list.lock);
912
913 return lm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Andrei Emeltchenko9e664632012-07-24 16:06:15 +0300916void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (!status) {
920 struct sco_conn *conn;
921
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +0200922 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (conn)
924 sco_conn_ready(conn);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900925 } else
Joe Perchese1750722011-06-29 18:18:29 -0700926 sco_conn_del(hcon, bt_to_errno(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Andrei Emeltchenko9e664632012-07-24 16:06:15 +0300929void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 BT_DBG("hcon %p reason %d", hcon, reason);
932
Joe Perchese1750722011-06-29 18:18:29 -0700933 sco_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200936int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 struct sco_conn *conn = hcon->sco_data;
939
940 if (!conn)
941 goto drop;
942
943 BT_DBG("conn %p len %d", conn, skb->len);
944
945 if (skb->len) {
946 sco_recv_frame(conn, skb);
947 return 0;
948 }
949
950drop:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900951 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
953}
954
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100955static int sco_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 struct sock *sk;
958 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Gustavo F. Padovanee65d192011-12-27 15:28:46 -0200960 read_lock(&sco_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800962 sk_for_each(sk, node, &sco_sk_list.head) {
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100963 seq_printf(f, "%s %s %d\n", batostr(&bt_sk(sk)->src),
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300964 batostr(&bt_sk(sk)->dst), sk->sk_state);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Gustavo F. Padovanee65d192011-12-27 15:28:46 -0200967 read_unlock(&sco_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800968
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100972static int sco_debugfs_open(struct inode *inode, struct file *file)
973{
974 return single_open(file, sco_debugfs_show, inode->i_private);
975}
976
977static const struct file_operations sco_debugfs_fops = {
978 .open = sco_debugfs_open,
979 .read = seq_read,
980 .llseek = seq_lseek,
981 .release = single_release,
982};
983
984static struct dentry *sco_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800986static const struct proto_ops sco_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 .family = PF_BLUETOOTH,
988 .owner = THIS_MODULE,
989 .release = sco_sock_release,
990 .bind = sco_sock_bind,
991 .connect = sco_sock_connect,
992 .listen = sco_sock_listen,
993 .accept = sco_sock_accept,
994 .getname = sco_sock_getname,
995 .sendmsg = sco_sock_sendmsg,
996 .recvmsg = bt_sock_recvmsg,
997 .poll = bt_sock_poll,
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200998 .ioctl = bt_sock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 .mmap = sock_no_mmap,
1000 .socketpair = sock_no_socketpair,
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +02001001 .shutdown = sco_sock_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 .setsockopt = sco_sock_setsockopt,
1003 .getsockopt = sco_sock_getsockopt
1004};
1005
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001006static const struct net_proto_family sco_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 .family = PF_BLUETOOTH,
1008 .owner = THIS_MODULE,
1009 .create = sco_sock_create,
1010};
1011
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001012int __init sco_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 int err;
1015
1016 err = proto_register(&sco_proto, 0);
1017 if (err < 0)
1018 return err;
1019
1020 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1021 if (err < 0) {
1022 BT_ERR("SCO socket registration failed");
1023 goto error;
1024 }
1025
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001026 if (bt_debugfs) {
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -03001027 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1028 NULL, &sco_debugfs_fops);
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001029 if (!sco_debugfs)
1030 BT_ERR("Failed to create SCO debug file");
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 BT_INFO("SCO socket layer initialized");
1034
1035 return 0;
1036
1037error:
1038 proto_unregister(&sco_proto);
1039 return err;
1040}
1041
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001042void __exit sco_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001044 debugfs_remove(sco_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1047 BT_ERR("SCO socket unregistration failed");
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 proto_unregister(&sco_proto);
1050}
1051
Marcel Holtmann7cb127d2008-07-14 20:13:53 +02001052module_param(disable_esco, bool, 0644);
1053MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");