blob: 482722bbc7a052c67053a95ad357463a5bc641bf [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>
Marcel Holtmannaef7d972010-03-21 05:27:45 +010043#include <linux/debugfs.h>
44#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/sock.h>
46
47#include <asm/system.h>
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +020048#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <net/bluetooth/bluetooth.h>
51#include <net/bluetooth/hci_core.h>
52#include <net/bluetooth/l2cap.h>
53#include <net/bluetooth/rfcomm.h>
54
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080055static const struct proto_ops rfcomm_sock_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static struct bt_sock_list rfcomm_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070058 .lock = __RW_LOCK_UNLOCKED(rfcomm_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
61static void rfcomm_sock_close(struct sock *sk);
62static void rfcomm_sock_kill(struct sock *sk);
63
64/* ---- DLC callbacks ----
65 *
66 * called under rfcomm_dlc_lock()
67 */
68static void rfcomm_sk_data_ready(struct rfcomm_dlc *d, struct sk_buff *skb)
69{
70 struct sock *sk = d->owner;
71 if (!sk)
72 return;
73
74 atomic_add(skb->len, &sk->sk_rmem_alloc);
75 skb_queue_tail(&sk->sk_receive_queue, skb);
76 sk->sk_data_ready(sk, skb->len);
77
78 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
79 rfcomm_dlc_throttle(d);
80}
81
82static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
83{
84 struct sock *sk = d->owner, *parent;
Gustavo F. Padovanfad003b2010-08-14 00:48:07 -030085 unsigned long flags;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (!sk)
88 return;
89
90 BT_DBG("dlc %p state %ld err %d", d, d->state, err);
91
Gustavo F. Padovanfad003b2010-08-14 00:48:07 -030092 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 bh_lock_sock(sk);
94
95 if (err)
96 sk->sk_err = err;
97
98 sk->sk_state = d->state;
99
100 parent = bt_sk(sk)->parent;
101 if (parent) {
102 if (d->state == BT_CLOSED) {
103 sock_set_flag(sk, SOCK_ZAPPED);
104 bt_accept_unlink(sk);
105 }
106 parent->sk_data_ready(parent, 0);
107 } else {
108 if (d->state == BT_CONNECTED)
109 rfcomm_session_getaddr(d->session, &bt_sk(sk)->src, NULL);
110 sk->sk_state_change(sk);
111 }
112
113 bh_unlock_sock(sk);
Gustavo F. Padovanfad003b2010-08-14 00:48:07 -0300114 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (parent && sock_flag(sk, SOCK_ZAPPED)) {
117 /* We have to drop DLC lock here, otherwise
118 * rfcomm_sock_destruct() will dead lock. */
119 rfcomm_dlc_unlock(d);
120 rfcomm_sock_kill(sk);
121 rfcomm_dlc_lock(d);
122 }
123}
124
125/* ---- Socket functions ---- */
126static struct sock *__rfcomm_get_sock_by_addr(u8 channel, bdaddr_t *src)
127{
128 struct sock *sk = NULL;
129 struct hlist_node *node;
130
131 sk_for_each(sk, node, &rfcomm_sk_list.head) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900132 if (rfcomm_pi(sk)->channel == channel &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 !bacmp(&bt_sk(sk)->src, src))
134 break;
135 }
136
137 return node ? sk : NULL;
138}
139
140/* Find socket with channel and source bdaddr.
141 * Returns closest match.
142 */
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000143static struct sock *rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct sock *sk = NULL, *sk1 = NULL;
146 struct hlist_node *node;
147
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000148 read_lock(&rfcomm_sk_list.lock);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 sk_for_each(sk, node, &rfcomm_sk_list.head) {
151 if (state && sk->sk_state != state)
152 continue;
153
154 if (rfcomm_pi(sk)->channel == channel) {
155 /* Exact match. */
156 if (!bacmp(&bt_sk(sk)->src, src))
157 break;
158
159 /* Closest match */
160 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
161 sk1 = sk;
162 }
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 read_unlock(&rfcomm_sk_list.lock);
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000166
167 return node ? sk : sk1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170static void rfcomm_sock_destruct(struct sock *sk)
171{
172 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
173
174 BT_DBG("sk %p dlc %p", sk, d);
175
176 skb_queue_purge(&sk->sk_receive_queue);
177 skb_queue_purge(&sk->sk_write_queue);
178
179 rfcomm_dlc_lock(d);
180 rfcomm_pi(sk)->dlc = NULL;
181
182 /* Detach DLC if it's owned by this socket */
183 if (d->owner == sk)
184 d->owner = NULL;
185 rfcomm_dlc_unlock(d);
186
187 rfcomm_dlc_put(d);
188}
189
190static void rfcomm_sock_cleanup_listen(struct sock *parent)
191{
192 struct sock *sk;
193
194 BT_DBG("parent %p", parent);
195
196 /* Close not yet accepted dlcs */
197 while ((sk = bt_accept_dequeue(parent, NULL))) {
198 rfcomm_sock_close(sk);
199 rfcomm_sock_kill(sk);
200 }
201
202 parent->sk_state = BT_CLOSED;
203 sock_set_flag(parent, SOCK_ZAPPED);
204}
205
206/* Kill socket (only if zapped and orphan)
207 * Must be called on unlocked socket.
208 */
209static void rfcomm_sock_kill(struct sock *sk)
210{
211 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
212 return;
213
214 BT_DBG("sk %p state %d refcnt %d", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
215
216 /* Kill poor orphan */
217 bt_sock_unlink(&rfcomm_sk_list, sk);
218 sock_set_flag(sk, SOCK_DEAD);
219 sock_put(sk);
220}
221
222static void __rfcomm_sock_close(struct sock *sk)
223{
224 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
225
226 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
227
228 switch (sk->sk_state) {
229 case BT_LISTEN:
230 rfcomm_sock_cleanup_listen(sk);
231 break;
232
233 case BT_CONNECT:
234 case BT_CONNECT2:
235 case BT_CONFIG:
236 case BT_CONNECTED:
237 rfcomm_dlc_close(d, 0);
238
239 default:
240 sock_set_flag(sk, SOCK_ZAPPED);
241 break;
242 }
243}
244
245/* Close socket.
246 * Must be called on unlocked socket.
247 */
248static void rfcomm_sock_close(struct sock *sk)
249{
250 lock_sock(sk);
251 __rfcomm_sock_close(sk);
252 release_sock(sk);
253}
254
255static void rfcomm_sock_init(struct sock *sk, struct sock *parent)
256{
257 struct rfcomm_pinfo *pi = rfcomm_pi(sk);
258
259 BT_DBG("sk %p", sk);
260
261 if (parent) {
262 sk->sk_type = parent->sk_type;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100263 pi->dlc->defer_setup = bt_sk(parent)->defer_setup;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100264
265 pi->sec_level = rfcomm_pi(parent)->sec_level;
266 pi->role_switch = rfcomm_pi(parent)->role_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 } else {
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100268 pi->dlc->defer_setup = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100269
270 pi->sec_level = BT_SECURITY_LOW;
271 pi->role_switch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100274 pi->dlc->sec_level = pi->sec_level;
275 pi->dlc->role_switch = pi->role_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278static struct proto rfcomm_proto = {
279 .name = "RFCOMM",
280 .owner = THIS_MODULE,
281 .obj_size = sizeof(struct rfcomm_pinfo)
282};
283
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700284static struct sock *rfcomm_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 struct rfcomm_dlc *d;
287 struct sock *sk;
288
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700289 sk = sk_alloc(net, PF_BLUETOOTH, prio, &rfcomm_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (!sk)
291 return NULL;
292
293 sock_init_data(sock, sk);
294 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
295
296 d = rfcomm_dlc_alloc(prio);
297 if (!d) {
298 sk_free(sk);
299 return NULL;
300 }
301
302 d->data_ready = rfcomm_sk_data_ready;
303 d->state_change = rfcomm_sk_state_change;
304
305 rfcomm_pi(sk)->dlc = d;
306 d->owner = sk;
307
308 sk->sk_destruct = rfcomm_sock_destruct;
309 sk->sk_sndtimeo = RFCOMM_CONN_TIMEOUT;
310
Marcel Holtmann77db1982008-07-14 20:13:45 +0200311 sk->sk_sndbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
312 sk->sk_rcvbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 sock_reset_flag(sk, SOCK_ZAPPED);
315
316 sk->sk_protocol = proto;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200317 sk->sk_state = BT_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 bt_sock_link(&rfcomm_sk_list, sk);
320
321 BT_DBG("sk %p", sk);
322 return sk;
323}
324
Eric Paris3f378b62009-11-05 22:18:14 -0800325static int rfcomm_sock_create(struct net *net, struct socket *sock,
326 int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 struct sock *sk;
329
330 BT_DBG("sock %p", sock);
331
332 sock->state = SS_UNCONNECTED;
333
334 if (sock->type != SOCK_STREAM && sock->type != SOCK_RAW)
335 return -ESOCKTNOSUPPORT;
336
337 sock->ops = &rfcomm_sock_ops;
338
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700339 sk = rfcomm_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200340 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -ENOMEM;
342
343 rfcomm_sock_init(sk, NULL);
344 return 0;
345}
346
347static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
348{
349 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
350 struct sock *sk = sock->sk;
351 int err = 0;
352
353 BT_DBG("sk %p %s", sk, batostr(&sa->rc_bdaddr));
354
355 if (!addr || addr->sa_family != AF_BLUETOOTH)
356 return -EINVAL;
357
358 lock_sock(sk);
359
360 if (sk->sk_state != BT_OPEN) {
361 err = -EBADFD;
362 goto done;
363 }
364
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200365 if (sk->sk_type != SOCK_STREAM) {
366 err = -EINVAL;
367 goto done;
368 }
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 write_lock_bh(&rfcomm_sk_list.lock);
371
372 if (sa->rc_channel && __rfcomm_get_sock_by_addr(sa->rc_channel, &sa->rc_bdaddr)) {
373 err = -EADDRINUSE;
374 } else {
375 /* Save source address */
376 bacpy(&bt_sk(sk)->src, &sa->rc_bdaddr);
377 rfcomm_pi(sk)->channel = sa->rc_channel;
378 sk->sk_state = BT_BOUND;
379 }
380
381 write_unlock_bh(&rfcomm_sk_list.lock);
382
383done:
384 release_sock(sk);
385 return err;
386}
387
388static int rfcomm_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
389{
390 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
391 struct sock *sk = sock->sk;
392 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
393 int err = 0;
394
395 BT_DBG("sk %p", sk);
396
Changli Gao6503d962010-03-31 22:58:26 +0000397 if (alen < sizeof(struct sockaddr_rc) ||
398 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return -EINVAL;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 lock_sock(sk);
402
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200403 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
404 err = -EBADFD;
405 goto done;
406 }
407
408 if (sk->sk_type != SOCK_STREAM) {
409 err = -EINVAL;
410 goto done;
411 }
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 sk->sk_state = BT_CONNECT;
414 bacpy(&bt_sk(sk)->dst, &sa->rc_bdaddr);
415 rfcomm_pi(sk)->channel = sa->rc_channel;
416
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100417 d->sec_level = rfcomm_pi(sk)->sec_level;
418 d->role_switch = rfcomm_pi(sk)->role_switch;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 err = rfcomm_dlc_open(d, &bt_sk(sk)->src, &sa->rc_bdaddr, sa->rc_channel);
421 if (!err)
422 err = bt_sock_wait_state(sk, BT_CONNECTED,
423 sock_sndtimeo(sk, flags & O_NONBLOCK));
424
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200425done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 release_sock(sk);
427 return err;
428}
429
430static int rfcomm_sock_listen(struct socket *sock, int backlog)
431{
432 struct sock *sk = sock->sk;
433 int err = 0;
434
435 BT_DBG("sk %p backlog %d", sk, backlog);
436
437 lock_sock(sk);
438
439 if (sk->sk_state != BT_BOUND) {
440 err = -EBADFD;
441 goto done;
442 }
443
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200444 if (sk->sk_type != SOCK_STREAM) {
445 err = -EINVAL;
446 goto done;
447 }
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (!rfcomm_pi(sk)->channel) {
450 bdaddr_t *src = &bt_sk(sk)->src;
451 u8 channel;
452
453 err = -EINVAL;
454
455 write_lock_bh(&rfcomm_sk_list.lock);
456
457 for (channel = 1; channel < 31; channel++)
458 if (!__rfcomm_get_sock_by_addr(channel, src)) {
459 rfcomm_pi(sk)->channel = channel;
460 err = 0;
461 break;
462 }
463
464 write_unlock_bh(&rfcomm_sk_list.lock);
465
466 if (err < 0)
467 goto done;
468 }
469
470 sk->sk_max_ack_backlog = backlog;
471 sk->sk_ack_backlog = 0;
472 sk->sk_state = BT_LISTEN;
473
474done:
475 release_sock(sk);
476 return err;
477}
478
479static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int flags)
480{
481 DECLARE_WAITQUEUE(wait, current);
482 struct sock *sk = sock->sk, *nsk;
483 long timeo;
484 int err = 0;
485
486 lock_sock(sk);
487
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200488 if (sk->sk_type != SOCK_STREAM) {
489 err = -EINVAL;
490 goto done;
491 }
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
494
495 BT_DBG("sk %p timeo %ld", sk, timeo);
496
497 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000498 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurley950e2d52011-07-24 00:10:41 -0400499 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (sk->sk_state != BT_LISTEN) {
503 err = -EBADFD;
504 break;
505 }
506
Peter Hurley950e2d52011-07-24 00:10:41 -0400507 nsk = bt_accept_dequeue(sk, newsock);
508 if (nsk)
509 break;
510
511 if (!timeo) {
512 err = -EAGAIN;
513 break;
514 }
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (signal_pending(current)) {
517 err = sock_intr_errno(timeo);
518 break;
519 }
Peter Hurley950e2d52011-07-24 00:10:41 -0400520
521 release_sock(sk);
522 timeo = schedule_timeout(timeo);
523 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Peter Hurley950e2d52011-07-24 00:10:41 -0400525 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000526 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 if (err)
529 goto done;
530
531 newsock->state = SS_CONNECTED;
532
533 BT_DBG("new socket %p", nsk);
534
535done:
536 release_sock(sk);
537 return err;
538}
539
540static int rfcomm_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
541{
542 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
543 struct sock *sk = sock->sk;
544
545 BT_DBG("sock %p, sk %p", sock, sk);
546
547 sa->rc_family = AF_BLUETOOTH;
548 sa->rc_channel = rfcomm_pi(sk)->channel;
549 if (peer)
550 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->dst);
551 else
552 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->src);
553
554 *len = sizeof(struct sockaddr_rc);
555 return 0;
556}
557
558static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
559 struct msghdr *msg, size_t len)
560{
561 struct sock *sk = sock->sk;
562 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
563 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int sent = 0;
565
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100566 if (test_bit(RFCOMM_DEFER_SETUP, &d->flags))
567 return -ENOTCONN;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (msg->msg_flags & MSG_OOB)
570 return -EOPNOTSUPP;
571
572 if (sk->sk_shutdown & SEND_SHUTDOWN)
573 return -EPIPE;
574
575 BT_DBG("sock %p, sk %p", sock, sk);
576
577 lock_sock(sk);
578
579 while (len) {
580 size_t size = min_t(size_t, len, d->mtu);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100581 int err;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 skb = sock_alloc_send_skb(sk, size + RFCOMM_SKB_RESERVE,
584 msg->msg_flags & MSG_DONTWAIT, &err);
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100585 if (!skb) {
586 if (sent == 0)
587 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 break;
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
591
592 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
593 if (err) {
594 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100595 if (sent == 0)
596 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 break;
598 }
599
600 err = rfcomm_dlc_send(d, skb);
601 if (err < 0) {
602 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100603 if (sent == 0)
604 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
606 }
607
608 sent += size;
609 len -= size;
610 }
611
612 release_sock(sk);
613
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100614 return sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617static int rfcomm_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
618 struct msghdr *msg, size_t size, int flags)
619{
620 struct sock *sk = sock->sk;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100621 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700622 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100624 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
625 rfcomm_dlc_accept(d);
626 return 0;
627 }
628
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700629 len = bt_sock_stream_recvmsg(iocb, sock, msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 lock_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700632 if (!(flags & MSG_PEEK) && len > 0)
633 atomic_sub(len, &sk->sk_rmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (atomic_read(&sk->sk_rmem_alloc) <= (sk->sk_rcvbuf >> 2))
636 rfcomm_dlc_unthrottle(rfcomm_pi(sk)->dlc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 release_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700638
639 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
David S. Millerb7058842009-09-30 16:12:20 -0700642static int rfcomm_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct sock *sk = sock->sk;
645 int err = 0;
646 u32 opt;
647
648 BT_DBG("sk %p", sk);
649
650 lock_sock(sk);
651
652 switch (optname) {
653 case RFCOMM_LM:
654 if (get_user(opt, (u32 __user *) optval)) {
655 err = -EFAULT;
656 break;
657 }
658
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100659 if (opt & RFCOMM_LM_AUTH)
660 rfcomm_pi(sk)->sec_level = BT_SECURITY_LOW;
661 if (opt & RFCOMM_LM_ENCRYPT)
662 rfcomm_pi(sk)->sec_level = BT_SECURITY_MEDIUM;
663 if (opt & RFCOMM_LM_SECURE)
664 rfcomm_pi(sk)->sec_level = BT_SECURITY_HIGH;
665
666 rfcomm_pi(sk)->role_switch = (opt & RFCOMM_LM_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 break;
668
669 default:
670 err = -ENOPROTOOPT;
671 break;
672 }
673
674 release_sock(sk);
675 return err;
676}
677
David S. Millerb7058842009-09-30 16:12:20 -0700678static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100679{
680 struct sock *sk = sock->sk;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100681 struct bt_security sec;
Stephen Boydd0fad892011-05-12 16:50:09 -0700682 int err = 0;
683 size_t len;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100684 u32 opt;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100685
686 BT_DBG("sk %p", sk);
687
688 if (level == SOL_RFCOMM)
689 return rfcomm_sock_setsockopt_old(sock, optname, optval, optlen);
690
Marcel Holtmann0588d942009-01-16 10:06:13 +0100691 if (level != SOL_BLUETOOTH)
692 return -ENOPROTOOPT;
693
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100694 lock_sock(sk);
695
696 switch (optname) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100697 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +0100698 if (sk->sk_type != SOCK_STREAM) {
699 err = -EINVAL;
700 break;
701 }
702
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100703 sec.level = BT_SECURITY_LOW;
704
705 len = min_t(unsigned int, sizeof(sec), optlen);
706 if (copy_from_user((char *) &sec, optval, len)) {
707 err = -EFAULT;
708 break;
709 }
710
711 if (sec.level > BT_SECURITY_HIGH) {
712 err = -EINVAL;
713 break;
714 }
715
716 rfcomm_pi(sk)->sec_level = sec.level;
717 break;
718
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100719 case BT_DEFER_SETUP:
720 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
721 err = -EINVAL;
722 break;
723 }
724
725 if (get_user(opt, (u32 __user *) optval)) {
726 err = -EFAULT;
727 break;
728 }
729
730 bt_sk(sk)->defer_setup = opt;
731 break;
732
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100733 default:
734 err = -ENOPROTOOPT;
735 break;
736 }
737
738 release_sock(sk);
739 return err;
740}
741
742static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct rfcomm_conninfo cinfo;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300746 struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int len, err = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100748 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 BT_DBG("sk %p", sk);
751
752 if (get_user(len, optlen))
753 return -EFAULT;
754
755 lock_sock(sk);
756
757 switch (optname) {
758 case RFCOMM_LM:
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100759 switch (rfcomm_pi(sk)->sec_level) {
760 case BT_SECURITY_LOW:
761 opt = RFCOMM_LM_AUTH;
762 break;
763 case BT_SECURITY_MEDIUM:
764 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT;
765 break;
766 case BT_SECURITY_HIGH:
767 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT |
768 RFCOMM_LM_SECURE;
769 break;
770 default:
771 opt = 0;
772 break;
773 }
774
775 if (rfcomm_pi(sk)->role_switch)
776 opt |= RFCOMM_LM_MASTER;
777
778 if (put_user(opt, (u32 __user *) optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 err = -EFAULT;
780 break;
781
782 case RFCOMM_CONNINFO:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100783 if (sk->sk_state != BT_CONNECTED &&
784 !rfcomm_pi(sk)->dlc->defer_setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 err = -ENOTCONN;
786 break;
787 }
788
Filip Palian8d03e972011-05-12 19:32:46 +0200789 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300790 cinfo.hci_handle = conn->hcon->handle;
791 memcpy(cinfo.dev_class, conn->hcon->dev_class, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 len = min_t(unsigned int, len, sizeof(cinfo));
794 if (copy_to_user(optval, (char *) &cinfo, len))
795 err = -EFAULT;
796
797 break;
798
799 default:
800 err = -ENOPROTOOPT;
801 break;
802 }
803
804 release_sock(sk);
805 return err;
806}
807
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100808static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
809{
810 struct sock *sk = sock->sk;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100811 struct bt_security sec;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100812 int len, err = 0;
813
814 BT_DBG("sk %p", sk);
815
816 if (level == SOL_RFCOMM)
817 return rfcomm_sock_getsockopt_old(sock, optname, optval, optlen);
818
Marcel Holtmann0588d942009-01-16 10:06:13 +0100819 if (level != SOL_BLUETOOTH)
820 return -ENOPROTOOPT;
821
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100822 if (get_user(len, optlen))
823 return -EFAULT;
824
825 lock_sock(sk);
826
827 switch (optname) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100828 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +0100829 if (sk->sk_type != SOCK_STREAM) {
830 err = -EINVAL;
831 break;
832 }
833
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100834 sec.level = rfcomm_pi(sk)->sec_level;
835
836 len = min_t(unsigned int, len, sizeof(sec));
837 if (copy_to_user(optval, (char *) &sec, len))
838 err = -EFAULT;
839
840 break;
841
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100842 case BT_DEFER_SETUP:
843 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
844 err = -EINVAL;
845 break;
846 }
847
848 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
849 err = -EFAULT;
850
851 break;
852
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100853 default:
854 err = -ENOPROTOOPT;
855 break;
856 }
857
858 release_sock(sk);
859 return err;
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862static int rfcomm_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
863{
David S. Millere19caae2008-12-09 01:04:27 -0800864 struct sock *sk __maybe_unused = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 int err;
866
David S. Millere19caae2008-12-09 01:04:27 -0800867 BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200869 err = bt_sock_ioctl(sock, cmd, arg);
870
871 if (err == -ENOIOCTLCMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872#ifdef CONFIG_BT_RFCOMM_TTY
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200873 lock_sock(sk);
874 err = rfcomm_dev_ioctl(sk, cmd, (void __user *) arg);
875 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876#else
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200877 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878#endif
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return err;
882}
883
884static int rfcomm_sock_shutdown(struct socket *sock, int how)
885{
886 struct sock *sk = sock->sk;
887 int err = 0;
888
889 BT_DBG("sock %p, sk %p", sock, sk);
890
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200891 if (!sk)
892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 lock_sock(sk);
895 if (!sk->sk_shutdown) {
896 sk->sk_shutdown = SHUTDOWN_MASK;
897 __rfcomm_sock_close(sk);
898
899 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
900 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
901 }
902 release_sock(sk);
903 return err;
904}
905
906static int rfcomm_sock_release(struct socket *sock)
907{
908 struct sock *sk = sock->sk;
909 int err;
910
911 BT_DBG("sock %p, sk %p", sock, sk);
912
913 if (!sk)
914 return 0;
915
916 err = rfcomm_sock_shutdown(sock, 2);
917
918 sock_orphan(sk);
919 rfcomm_sock_kill(sk);
920 return err;
921}
922
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900923/* ---- RFCOMM core layer callbacks ----
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 *
925 * called under rfcomm_lock()
926 */
927int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d)
928{
929 struct sock *sk, *parent;
930 bdaddr_t src, dst;
931 int result = 0;
932
933 BT_DBG("session %p channel %d", s, channel);
934
935 rfcomm_session_getaddr(s, &src, &dst);
936
937 /* Check if we have socket listening on channel */
938 parent = rfcomm_get_sock_by_channel(BT_LISTEN, channel, &src);
939 if (!parent)
940 return 0;
941
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000942 bh_lock_sock(parent);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* Check for backlog size */
945 if (sk_acceptq_is_full(parent)) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900946 BT_DBG("backlog full %d", parent->sk_ack_backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto done;
948 }
949
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900950 sk = rfcomm_sock_alloc(sock_net(parent), NULL, BTPROTO_RFCOMM, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (!sk)
952 goto done;
953
954 rfcomm_sock_init(sk, parent);
955 bacpy(&bt_sk(sk)->src, &src);
956 bacpy(&bt_sk(sk)->dst, &dst);
957 rfcomm_pi(sk)->channel = channel;
958
959 sk->sk_state = BT_CONFIG;
960 bt_accept_enqueue(parent, sk);
961
962 /* Accept connection and return socket DLC */
963 *d = rfcomm_pi(sk)->dlc;
964 result = 1;
965
966done:
967 bh_unlock_sock(parent);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100968
969 if (bt_sk(parent)->defer_setup)
970 parent->sk_state_change(parent);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return result;
973}
974
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100975static int rfcomm_sock_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 struct sock *sk;
978 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 read_lock_bh(&rfcomm_sk_list.lock);
981
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800982 sk_for_each(sk, node, &rfcomm_sk_list.head) {
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100983 seq_printf(f, "%s %s %d %d\n",
984 batostr(&bt_sk(sk)->src),
985 batostr(&bt_sk(sk)->dst),
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800986 sk->sk_state, rfcomm_pi(sk)->channel);
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 read_unlock_bh(&rfcomm_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800990
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100994static int rfcomm_sock_debugfs_open(struct inode *inode, struct file *file)
995{
996 return single_open(file, rfcomm_sock_debugfs_show, inode->i_private);
997}
998
999static const struct file_operations rfcomm_sock_debugfs_fops = {
1000 .open = rfcomm_sock_debugfs_open,
1001 .read = seq_read,
1002 .llseek = seq_lseek,
1003 .release = single_release,
1004};
1005
1006static struct dentry *rfcomm_sock_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001008static const struct proto_ops rfcomm_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 .family = PF_BLUETOOTH,
1010 .owner = THIS_MODULE,
1011 .release = rfcomm_sock_release,
1012 .bind = rfcomm_sock_bind,
1013 .connect = rfcomm_sock_connect,
1014 .listen = rfcomm_sock_listen,
1015 .accept = rfcomm_sock_accept,
1016 .getname = rfcomm_sock_getname,
1017 .sendmsg = rfcomm_sock_sendmsg,
1018 .recvmsg = rfcomm_sock_recvmsg,
1019 .shutdown = rfcomm_sock_shutdown,
1020 .setsockopt = rfcomm_sock_setsockopt,
1021 .getsockopt = rfcomm_sock_getsockopt,
1022 .ioctl = rfcomm_sock_ioctl,
1023 .poll = bt_sock_poll,
1024 .socketpair = sock_no_socketpair,
1025 .mmap = sock_no_mmap
1026};
1027
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001028static const struct net_proto_family rfcomm_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 .family = PF_BLUETOOTH,
1030 .owner = THIS_MODULE,
1031 .create = rfcomm_sock_create
1032};
1033
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001034int __init rfcomm_init_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 int err;
1037
1038 err = proto_register(&rfcomm_proto, 0);
1039 if (err < 0)
1040 return err;
1041
1042 err = bt_sock_register(BTPROTO_RFCOMM, &rfcomm_sock_family_ops);
1043 if (err < 0)
1044 goto error;
1045
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001046 if (bt_debugfs) {
1047 rfcomm_sock_debugfs = debugfs_create_file("rfcomm", 0444,
1048 bt_debugfs, NULL, &rfcomm_sock_debugfs_fops);
1049 if (!rfcomm_sock_debugfs)
1050 BT_ERR("Failed to create RFCOMM debug file");
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 BT_INFO("RFCOMM socket layer initialized");
1054
1055 return 0;
1056
1057error:
1058 BT_ERR("RFCOMM socket layer registration failed");
1059 proto_unregister(&rfcomm_proto);
1060 return err;
1061}
1062
Gustavo F. Padovan2f8362a2010-07-24 02:04:45 -03001063void __exit rfcomm_cleanup_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001065 debugfs_remove(rfcomm_sock_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 if (bt_sock_unregister(BTPROTO_RFCOMM) < 0)
1068 BT_ERR("RFCOMM socket layer unregistration failed");
1069
1070 proto_unregister(&rfcomm_proto);
1071}