blob: 65dd7133d72bf92e3d4a1b3e190aa14ecac107ce [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 RFCOMM implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation;
9
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090014 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090019 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 SOFTWARE IS DISCLAIMED.
22*/
23
24/*
25 * RFCOMM sockets.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29
30#include <linux/types.h>
31#include <linux/errno.h>
32#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/sched.h>
34#include <linux/slab.h>
35#include <linux/poll.h>
36#include <linux/fcntl.h>
37#include <linux/init.h>
38#include <linux/interrupt.h>
39#include <linux/socket.h>
40#include <linux/skbuff.h>
41#include <linux/list.h>
Marcel Holtmannbe9d1222005-11-08 09:57:38 -080042#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <net/sock.h>
44
45#include <asm/system.h>
46#include <asm/uaccess.h>
47
48#include <net/bluetooth/bluetooth.h>
49#include <net/bluetooth/hci_core.h>
50#include <net/bluetooth/l2cap.h>
51#include <net/bluetooth/rfcomm.h>
52
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080053static const struct proto_ops rfcomm_sock_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55static struct bt_sock_list rfcomm_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070056 .lock = __RW_LOCK_UNLOCKED(rfcomm_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59static void rfcomm_sock_close(struct sock *sk);
60static void rfcomm_sock_kill(struct sock *sk);
61
62/* ---- DLC callbacks ----
63 *
64 * called under rfcomm_dlc_lock()
65 */
66static void rfcomm_sk_data_ready(struct rfcomm_dlc *d, struct sk_buff *skb)
67{
68 struct sock *sk = d->owner;
69 if (!sk)
70 return;
71
72 atomic_add(skb->len, &sk->sk_rmem_alloc);
73 skb_queue_tail(&sk->sk_receive_queue, skb);
74 sk->sk_data_ready(sk, skb->len);
75
76 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
77 rfcomm_dlc_throttle(d);
78}
79
80static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
81{
82 struct sock *sk = d->owner, *parent;
83 if (!sk)
84 return;
85
86 BT_DBG("dlc %p state %ld err %d", d, d->state, err);
87
88 bh_lock_sock(sk);
89
90 if (err)
91 sk->sk_err = err;
92
93 sk->sk_state = d->state;
94
95 parent = bt_sk(sk)->parent;
96 if (parent) {
97 if (d->state == BT_CLOSED) {
98 sock_set_flag(sk, SOCK_ZAPPED);
99 bt_accept_unlink(sk);
100 }
101 parent->sk_data_ready(parent, 0);
102 } else {
103 if (d->state == BT_CONNECTED)
104 rfcomm_session_getaddr(d->session, &bt_sk(sk)->src, NULL);
105 sk->sk_state_change(sk);
106 }
107
108 bh_unlock_sock(sk);
109
110 if (parent && sock_flag(sk, SOCK_ZAPPED)) {
111 /* We have to drop DLC lock here, otherwise
112 * rfcomm_sock_destruct() will dead lock. */
113 rfcomm_dlc_unlock(d);
114 rfcomm_sock_kill(sk);
115 rfcomm_dlc_lock(d);
116 }
117}
118
119/* ---- Socket functions ---- */
120static struct sock *__rfcomm_get_sock_by_addr(u8 channel, bdaddr_t *src)
121{
122 struct sock *sk = NULL;
123 struct hlist_node *node;
124
125 sk_for_each(sk, node, &rfcomm_sk_list.head) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900126 if (rfcomm_pi(sk)->channel == channel &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 !bacmp(&bt_sk(sk)->src, src))
128 break;
129 }
130
131 return node ? sk : NULL;
132}
133
134/* Find socket with channel and source bdaddr.
135 * Returns closest match.
136 */
137static struct sock *__rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *src)
138{
139 struct sock *sk = NULL, *sk1 = NULL;
140 struct hlist_node *node;
141
142 sk_for_each(sk, node, &rfcomm_sk_list.head) {
143 if (state && sk->sk_state != state)
144 continue;
145
146 if (rfcomm_pi(sk)->channel == channel) {
147 /* Exact match. */
148 if (!bacmp(&bt_sk(sk)->src, src))
149 break;
150
151 /* Closest match */
152 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
153 sk1 = sk;
154 }
155 }
156 return node ? sk : sk1;
157}
158
159/* Find socket with given address (channel, src).
160 * Returns locked socket */
161static inline struct sock *rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *src)
162{
163 struct sock *s;
164 read_lock(&rfcomm_sk_list.lock);
165 s = __rfcomm_get_sock_by_channel(state, channel, src);
166 if (s) bh_lock_sock(s);
167 read_unlock(&rfcomm_sk_list.lock);
168 return s;
169}
170
171static void rfcomm_sock_destruct(struct sock *sk)
172{
173 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
174
175 BT_DBG("sk %p dlc %p", sk, d);
176
177 skb_queue_purge(&sk->sk_receive_queue);
178 skb_queue_purge(&sk->sk_write_queue);
179
180 rfcomm_dlc_lock(d);
181 rfcomm_pi(sk)->dlc = NULL;
182
183 /* Detach DLC if it's owned by this socket */
184 if (d->owner == sk)
185 d->owner = NULL;
186 rfcomm_dlc_unlock(d);
187
188 rfcomm_dlc_put(d);
189}
190
191static void rfcomm_sock_cleanup_listen(struct sock *parent)
192{
193 struct sock *sk;
194
195 BT_DBG("parent %p", parent);
196
197 /* Close not yet accepted dlcs */
198 while ((sk = bt_accept_dequeue(parent, NULL))) {
199 rfcomm_sock_close(sk);
200 rfcomm_sock_kill(sk);
201 }
202
203 parent->sk_state = BT_CLOSED;
204 sock_set_flag(parent, SOCK_ZAPPED);
205}
206
207/* Kill socket (only if zapped and orphan)
208 * Must be called on unlocked socket.
209 */
210static void rfcomm_sock_kill(struct sock *sk)
211{
212 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
213 return;
214
215 BT_DBG("sk %p state %d refcnt %d", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
216
217 /* Kill poor orphan */
218 bt_sock_unlink(&rfcomm_sk_list, sk);
219 sock_set_flag(sk, SOCK_DEAD);
220 sock_put(sk);
221}
222
223static void __rfcomm_sock_close(struct sock *sk)
224{
225 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
226
227 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
228
229 switch (sk->sk_state) {
230 case BT_LISTEN:
231 rfcomm_sock_cleanup_listen(sk);
232 break;
233
234 case BT_CONNECT:
235 case BT_CONNECT2:
236 case BT_CONFIG:
237 case BT_CONNECTED:
238 rfcomm_dlc_close(d, 0);
239
240 default:
241 sock_set_flag(sk, SOCK_ZAPPED);
242 break;
243 }
244}
245
246/* Close socket.
247 * Must be called on unlocked socket.
248 */
249static void rfcomm_sock_close(struct sock *sk)
250{
251 lock_sock(sk);
252 __rfcomm_sock_close(sk);
253 release_sock(sk);
254}
255
256static void rfcomm_sock_init(struct sock *sk, struct sock *parent)
257{
258 struct rfcomm_pinfo *pi = rfcomm_pi(sk);
259
260 BT_DBG("sk %p", sk);
261
262 if (parent) {
263 sk->sk_type = parent->sk_type;
264 pi->link_mode = rfcomm_pi(parent)->link_mode;
265 } else {
266 pi->link_mode = 0;
267 }
268
269 pi->dlc->link_mode = pi->link_mode;
270}
271
272static struct proto rfcomm_proto = {
273 .name = "RFCOMM",
274 .owner = THIS_MODULE,
275 .obj_size = sizeof(struct rfcomm_pinfo)
276};
277
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700278static struct sock *rfcomm_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct rfcomm_dlc *d;
281 struct sock *sk;
282
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700283 sk = sk_alloc(net, PF_BLUETOOTH, prio, &rfcomm_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!sk)
285 return NULL;
286
287 sock_init_data(sock, sk);
288 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
289
290 d = rfcomm_dlc_alloc(prio);
291 if (!d) {
292 sk_free(sk);
293 return NULL;
294 }
295
296 d->data_ready = rfcomm_sk_data_ready;
297 d->state_change = rfcomm_sk_state_change;
298
299 rfcomm_pi(sk)->dlc = d;
300 d->owner = sk;
301
302 sk->sk_destruct = rfcomm_sock_destruct;
303 sk->sk_sndtimeo = RFCOMM_CONN_TIMEOUT;
304
Marcel Holtmann77db1982008-07-14 20:13:45 +0200305 sk->sk_sndbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
306 sk->sk_rcvbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 sock_reset_flag(sk, SOCK_ZAPPED);
309
310 sk->sk_protocol = proto;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200311 sk->sk_state = BT_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 bt_sock_link(&rfcomm_sk_list, sk);
314
315 BT_DBG("sk %p", sk);
316 return sk;
317}
318
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700319static int rfcomm_sock_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 struct sock *sk;
322
323 BT_DBG("sock %p", sock);
324
325 sock->state = SS_UNCONNECTED;
326
327 if (sock->type != SOCK_STREAM && sock->type != SOCK_RAW)
328 return -ESOCKTNOSUPPORT;
329
330 sock->ops = &rfcomm_sock_ops;
331
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700332 sk = rfcomm_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200333 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -ENOMEM;
335
336 rfcomm_sock_init(sk, NULL);
337 return 0;
338}
339
340static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
341{
342 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
343 struct sock *sk = sock->sk;
344 int err = 0;
345
346 BT_DBG("sk %p %s", sk, batostr(&sa->rc_bdaddr));
347
348 if (!addr || addr->sa_family != AF_BLUETOOTH)
349 return -EINVAL;
350
351 lock_sock(sk);
352
353 if (sk->sk_state != BT_OPEN) {
354 err = -EBADFD;
355 goto done;
356 }
357
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200358 if (sk->sk_type != SOCK_STREAM) {
359 err = -EINVAL;
360 goto done;
361 }
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 write_lock_bh(&rfcomm_sk_list.lock);
364
365 if (sa->rc_channel && __rfcomm_get_sock_by_addr(sa->rc_channel, &sa->rc_bdaddr)) {
366 err = -EADDRINUSE;
367 } else {
368 /* Save source address */
369 bacpy(&bt_sk(sk)->src, &sa->rc_bdaddr);
370 rfcomm_pi(sk)->channel = sa->rc_channel;
371 sk->sk_state = BT_BOUND;
372 }
373
374 write_unlock_bh(&rfcomm_sk_list.lock);
375
376done:
377 release_sock(sk);
378 return err;
379}
380
381static int rfcomm_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
382{
383 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
384 struct sock *sk = sock->sk;
385 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
386 int err = 0;
387
388 BT_DBG("sk %p", sk);
389
390 if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_rc))
391 return -EINVAL;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 lock_sock(sk);
394
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200395 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
396 err = -EBADFD;
397 goto done;
398 }
399
400 if (sk->sk_type != SOCK_STREAM) {
401 err = -EINVAL;
402 goto done;
403 }
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 sk->sk_state = BT_CONNECT;
406 bacpy(&bt_sk(sk)->dst, &sa->rc_bdaddr);
407 rfcomm_pi(sk)->channel = sa->rc_channel;
408
Marcel Holtmann77db1982008-07-14 20:13:45 +0200409 d->link_mode = rfcomm_pi(sk)->link_mode;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 err = rfcomm_dlc_open(d, &bt_sk(sk)->src, &sa->rc_bdaddr, sa->rc_channel);
412 if (!err)
413 err = bt_sock_wait_state(sk, BT_CONNECTED,
414 sock_sndtimeo(sk, flags & O_NONBLOCK));
415
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200416done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 release_sock(sk);
418 return err;
419}
420
421static int rfcomm_sock_listen(struct socket *sock, int backlog)
422{
423 struct sock *sk = sock->sk;
424 int err = 0;
425
426 BT_DBG("sk %p backlog %d", sk, backlog);
427
428 lock_sock(sk);
429
430 if (sk->sk_state != BT_BOUND) {
431 err = -EBADFD;
432 goto done;
433 }
434
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200435 if (sk->sk_type != SOCK_STREAM) {
436 err = -EINVAL;
437 goto done;
438 }
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!rfcomm_pi(sk)->channel) {
441 bdaddr_t *src = &bt_sk(sk)->src;
442 u8 channel;
443
444 err = -EINVAL;
445
446 write_lock_bh(&rfcomm_sk_list.lock);
447
448 for (channel = 1; channel < 31; channel++)
449 if (!__rfcomm_get_sock_by_addr(channel, src)) {
450 rfcomm_pi(sk)->channel = channel;
451 err = 0;
452 break;
453 }
454
455 write_unlock_bh(&rfcomm_sk_list.lock);
456
457 if (err < 0)
458 goto done;
459 }
460
461 sk->sk_max_ack_backlog = backlog;
462 sk->sk_ack_backlog = 0;
463 sk->sk_state = BT_LISTEN;
464
465done:
466 release_sock(sk);
467 return err;
468}
469
470static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int flags)
471{
472 DECLARE_WAITQUEUE(wait, current);
473 struct sock *sk = sock->sk, *nsk;
474 long timeo;
475 int err = 0;
476
477 lock_sock(sk);
478
479 if (sk->sk_state != BT_LISTEN) {
480 err = -EBADFD;
481 goto done;
482 }
483
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200484 if (sk->sk_type != SOCK_STREAM) {
485 err = -EINVAL;
486 goto done;
487 }
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
490
491 BT_DBG("sk %p timeo %ld", sk, timeo);
492
493 /* Wait for an incoming connection. (wake-one). */
494 add_wait_queue_exclusive(sk->sk_sleep, &wait);
495 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
496 set_current_state(TASK_INTERRUPTIBLE);
497 if (!timeo) {
498 err = -EAGAIN;
499 break;
500 }
501
502 release_sock(sk);
503 timeo = schedule_timeout(timeo);
504 lock_sock(sk);
505
506 if (sk->sk_state != BT_LISTEN) {
507 err = -EBADFD;
508 break;
509 }
510
511 if (signal_pending(current)) {
512 err = sock_intr_errno(timeo);
513 break;
514 }
515 }
516 set_current_state(TASK_RUNNING);
517 remove_wait_queue(sk->sk_sleep, &wait);
518
519 if (err)
520 goto done;
521
522 newsock->state = SS_CONNECTED;
523
524 BT_DBG("new socket %p", nsk);
525
526done:
527 release_sock(sk);
528 return err;
529}
530
531static int rfcomm_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
532{
533 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
534 struct sock *sk = sock->sk;
535
536 BT_DBG("sock %p, sk %p", sock, sk);
537
538 sa->rc_family = AF_BLUETOOTH;
539 sa->rc_channel = rfcomm_pi(sk)->channel;
540 if (peer)
541 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->dst);
542 else
543 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->src);
544
545 *len = sizeof(struct sockaddr_rc);
546 return 0;
547}
548
549static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
550 struct msghdr *msg, size_t len)
551{
552 struct sock *sk = sock->sk;
553 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
554 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int sent = 0;
556
557 if (msg->msg_flags & MSG_OOB)
558 return -EOPNOTSUPP;
559
560 if (sk->sk_shutdown & SEND_SHUTDOWN)
561 return -EPIPE;
562
563 BT_DBG("sock %p, sk %p", sock, sk);
564
565 lock_sock(sk);
566
567 while (len) {
568 size_t size = min_t(size_t, len, d->mtu);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100569 int err;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 skb = sock_alloc_send_skb(sk, size + RFCOMM_SKB_RESERVE,
572 msg->msg_flags & MSG_DONTWAIT, &err);
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100573 if (!skb) {
574 if (sent == 0)
575 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 break;
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
579
580 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
581 if (err) {
582 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100583 if (sent == 0)
584 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
586 }
587
588 err = rfcomm_dlc_send(d, skb);
589 if (err < 0) {
590 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100591 if (sent == 0)
592 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 break;
594 }
595
596 sent += size;
597 len -= size;
598 }
599
600 release_sock(sk);
601
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100602 return sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605static long rfcomm_sock_data_wait(struct sock *sk, long timeo)
606{
607 DECLARE_WAITQUEUE(wait, current);
608
609 add_wait_queue(sk->sk_sleep, &wait);
610 for (;;) {
611 set_current_state(TASK_INTERRUPTIBLE);
612
David S. Millerb03efcf2005-07-08 14:57:23 -0700613 if (!skb_queue_empty(&sk->sk_receive_queue) ||
614 sk->sk_err ||
615 (sk->sk_shutdown & RCV_SHUTDOWN) ||
616 signal_pending(current) ||
617 !timeo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
619
620 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
621 release_sock(sk);
622 timeo = schedule_timeout(timeo);
623 lock_sock(sk);
624 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
625 }
626
627 __set_current_state(TASK_RUNNING);
628 remove_wait_queue(sk->sk_sleep, &wait);
629 return timeo;
630}
631
632static int rfcomm_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
633 struct msghdr *msg, size_t size, int flags)
634{
635 struct sock *sk = sock->sk;
636 int err = 0;
637 size_t target, copied = 0;
638 long timeo;
639
640 if (flags & MSG_OOB)
641 return -EOPNOTSUPP;
642
643 msg->msg_namelen = 0;
644
Marcel Holtmanna418b892008-11-30 12:17:28 +0100645 BT_DBG("sk %p size %zu", sk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 lock_sock(sk);
648
649 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
650 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
651
652 do {
653 struct sk_buff *skb;
654 int chunk;
655
656 skb = skb_dequeue(&sk->sk_receive_queue);
657 if (!skb) {
658 if (copied >= target)
659 break;
660
661 if ((err = sock_error(sk)) != 0)
662 break;
663 if (sk->sk_shutdown & RCV_SHUTDOWN)
664 break;
665
666 err = -EAGAIN;
667 if (!timeo)
668 break;
669
670 timeo = rfcomm_sock_data_wait(sk, timeo);
671
672 if (signal_pending(current)) {
673 err = sock_intr_errno(timeo);
674 goto out;
675 }
676 continue;
677 }
678
679 chunk = min_t(unsigned int, skb->len, size);
680 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
681 skb_queue_head(&sk->sk_receive_queue, skb);
682 if (!copied)
683 copied = -EFAULT;
684 break;
685 }
686 copied += chunk;
687 size -= chunk;
688
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200689 sock_recv_timestamp(msg, sk, skb);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!(flags & MSG_PEEK)) {
692 atomic_sub(chunk, &sk->sk_rmem_alloc);
693
694 skb_pull(skb, chunk);
695 if (skb->len) {
696 skb_queue_head(&sk->sk_receive_queue, skb);
697 break;
698 }
699 kfree_skb(skb);
700
701 } else {
702 /* put message back and return */
703 skb_queue_head(&sk->sk_receive_queue, skb);
704 break;
705 }
706 } while (size);
707
708out:
709 if (atomic_read(&sk->sk_rmem_alloc) <= (sk->sk_rcvbuf >> 2))
710 rfcomm_dlc_unthrottle(rfcomm_pi(sk)->dlc);
711
712 release_sock(sk);
713 return copied ? : err;
714}
715
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100716static int rfcomm_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct sock *sk = sock->sk;
719 int err = 0;
720 u32 opt;
721
722 BT_DBG("sk %p", sk);
723
724 lock_sock(sk);
725
726 switch (optname) {
727 case RFCOMM_LM:
728 if (get_user(opt, (u32 __user *) optval)) {
729 err = -EFAULT;
730 break;
731 }
732
733 rfcomm_pi(sk)->link_mode = opt;
734 break;
735
736 default:
737 err = -ENOPROTOOPT;
738 break;
739 }
740
741 release_sock(sk);
742 return err;
743}
744
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100745static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
746{
747 struct sock *sk = sock->sk;
748 int err = 0;
749
750 BT_DBG("sk %p", sk);
751
752 if (level == SOL_RFCOMM)
753 return rfcomm_sock_setsockopt_old(sock, optname, optval, optlen);
754
755 lock_sock(sk);
756
757 switch (optname) {
758 default:
759 err = -ENOPROTOOPT;
760 break;
761 }
762
763 release_sock(sk);
764 return err;
765}
766
767static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct sock *sk = sock->sk;
770 struct sock *l2cap_sk;
771 struct rfcomm_conninfo cinfo;
772 int len, err = 0;
773
774 BT_DBG("sk %p", sk);
775
776 if (get_user(len, optlen))
777 return -EFAULT;
778
779 lock_sock(sk);
780
781 switch (optname) {
782 case RFCOMM_LM:
783 if (put_user(rfcomm_pi(sk)->link_mode, (u32 __user *) optval))
784 err = -EFAULT;
785 break;
786
787 case RFCOMM_CONNINFO:
788 if (sk->sk_state != BT_CONNECTED) {
789 err = -ENOTCONN;
790 break;
791 }
792
793 l2cap_sk = rfcomm_pi(sk)->dlc->session->sock->sk;
794
795 cinfo.hci_handle = l2cap_pi(l2cap_sk)->conn->hcon->handle;
796 memcpy(cinfo.dev_class, l2cap_pi(l2cap_sk)->conn->hcon->dev_class, 3);
797
798 len = min_t(unsigned int, len, sizeof(cinfo));
799 if (copy_to_user(optval, (char *) &cinfo, len))
800 err = -EFAULT;
801
802 break;
803
804 default:
805 err = -ENOPROTOOPT;
806 break;
807 }
808
809 release_sock(sk);
810 return err;
811}
812
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100813static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
814{
815 struct sock *sk = sock->sk;
816 int len, err = 0;
817
818 BT_DBG("sk %p", sk);
819
820 if (level == SOL_RFCOMM)
821 return rfcomm_sock_getsockopt_old(sock, optname, optval, optlen);
822
823 if (get_user(len, optlen))
824 return -EFAULT;
825
826 lock_sock(sk);
827
828 switch (optname) {
829 default:
830 err = -ENOPROTOOPT;
831 break;
832 }
833
834 release_sock(sk);
835 return err;
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838static int rfcomm_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
839{
David S. Millere19caae2008-12-09 01:04:27 -0800840 struct sock *sk __maybe_unused = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 int err;
842
David S. Millere19caae2008-12-09 01:04:27 -0800843 BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200845 err = bt_sock_ioctl(sock, cmd, arg);
846
847 if (err == -ENOIOCTLCMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848#ifdef CONFIG_BT_RFCOMM_TTY
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200849 lock_sock(sk);
850 err = rfcomm_dev_ioctl(sk, cmd, (void __user *) arg);
851 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852#else
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200853 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854#endif
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return err;
858}
859
860static int rfcomm_sock_shutdown(struct socket *sock, int how)
861{
862 struct sock *sk = sock->sk;
863 int err = 0;
864
865 BT_DBG("sock %p, sk %p", sock, sk);
866
867 if (!sk) return 0;
868
869 lock_sock(sk);
870 if (!sk->sk_shutdown) {
871 sk->sk_shutdown = SHUTDOWN_MASK;
872 __rfcomm_sock_close(sk);
873
874 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
875 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
876 }
877 release_sock(sk);
878 return err;
879}
880
881static int rfcomm_sock_release(struct socket *sock)
882{
883 struct sock *sk = sock->sk;
884 int err;
885
886 BT_DBG("sock %p, sk %p", sock, sk);
887
888 if (!sk)
889 return 0;
890
891 err = rfcomm_sock_shutdown(sock, 2);
892
893 sock_orphan(sk);
894 rfcomm_sock_kill(sk);
895 return err;
896}
897
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900898/* ---- RFCOMM core layer callbacks ----
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 *
900 * called under rfcomm_lock()
901 */
902int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d)
903{
904 struct sock *sk, *parent;
905 bdaddr_t src, dst;
906 int result = 0;
907
908 BT_DBG("session %p channel %d", s, channel);
909
910 rfcomm_session_getaddr(s, &src, &dst);
911
912 /* Check if we have socket listening on channel */
913 parent = rfcomm_get_sock_by_channel(BT_LISTEN, channel, &src);
914 if (!parent)
915 return 0;
916
917 /* Check for backlog size */
918 if (sk_acceptq_is_full(parent)) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900919 BT_DBG("backlog full %d", parent->sk_ack_backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 goto done;
921 }
922
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900923 sk = rfcomm_sock_alloc(sock_net(parent), NULL, BTPROTO_RFCOMM, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (!sk)
925 goto done;
926
927 rfcomm_sock_init(sk, parent);
928 bacpy(&bt_sk(sk)->src, &src);
929 bacpy(&bt_sk(sk)->dst, &dst);
930 rfcomm_pi(sk)->channel = channel;
931
932 sk->sk_state = BT_CONFIG;
933 bt_accept_enqueue(parent, sk);
934
935 /* Accept connection and return socket DLC */
936 *d = rfcomm_pi(sk)->dlc;
937 result = 1;
938
939done:
940 bh_unlock_sock(parent);
941 return result;
942}
943
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800944static ssize_t rfcomm_sock_sysfs_show(struct class *dev, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 struct sock *sk;
947 struct hlist_node *node;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800948 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 read_lock_bh(&rfcomm_sk_list.lock);
951
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800952 sk_for_each(sk, node, &rfcomm_sk_list.head) {
953 str += sprintf(str, "%s %s %d %d\n",
954 batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
955 sk->sk_state, rfcomm_pi(sk)->channel);
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 read_unlock_bh(&rfcomm_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800959
960 return (str - buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800963static CLASS_ATTR(rfcomm, S_IRUGO, rfcomm_sock_sysfs_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800965static const struct proto_ops rfcomm_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 .family = PF_BLUETOOTH,
967 .owner = THIS_MODULE,
968 .release = rfcomm_sock_release,
969 .bind = rfcomm_sock_bind,
970 .connect = rfcomm_sock_connect,
971 .listen = rfcomm_sock_listen,
972 .accept = rfcomm_sock_accept,
973 .getname = rfcomm_sock_getname,
974 .sendmsg = rfcomm_sock_sendmsg,
975 .recvmsg = rfcomm_sock_recvmsg,
976 .shutdown = rfcomm_sock_shutdown,
977 .setsockopt = rfcomm_sock_setsockopt,
978 .getsockopt = rfcomm_sock_getsockopt,
979 .ioctl = rfcomm_sock_ioctl,
980 .poll = bt_sock_poll,
981 .socketpair = sock_no_socketpair,
982 .mmap = sock_no_mmap
983};
984
985static struct net_proto_family rfcomm_sock_family_ops = {
986 .family = PF_BLUETOOTH,
987 .owner = THIS_MODULE,
988 .create = rfcomm_sock_create
989};
990
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800991int __init rfcomm_init_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 int err;
994
995 err = proto_register(&rfcomm_proto, 0);
996 if (err < 0)
997 return err;
998
999 err = bt_sock_register(BTPROTO_RFCOMM, &rfcomm_sock_family_ops);
1000 if (err < 0)
1001 goto error;
1002
Marcel Holtmanndf5c37e2006-10-15 17:30:45 +02001003 if (class_create_file(bt_class, &class_attr_rfcomm) < 0)
1004 BT_ERR("Failed to create RFCOMM info file");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 BT_INFO("RFCOMM socket layer initialized");
1007
1008 return 0;
1009
1010error:
1011 BT_ERR("RFCOMM socket layer registration failed");
1012 proto_unregister(&rfcomm_proto);
1013 return err;
1014}
1015
1016void __exit rfcomm_cleanup_sockets(void)
1017{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +02001018 class_remove_file(bt_class, &class_attr_rfcomm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 if (bt_sock_unregister(BTPROTO_RFCOMM) < 0)
1021 BT_ERR("RFCOMM socket layer unregistration failed");
1022
1023 proto_unregister(&rfcomm_proto);
1024}