blob: 867a06576d8a1b6a4bc1bd4db503e43d32610567 [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
Gustavo Padovan8c520a52012-05-23 04:04:22 -030028#include <linux/export.h>
Marcel Holtmannaef7d972010-03-21 05:27:45 +010029#include <linux/debugfs.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/l2cap.h>
34#include <net/bluetooth/rfcomm.h>
35
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080036static const struct proto_ops rfcomm_sock_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38static struct bt_sock_list rfcomm_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070039 .lock = __RW_LOCK_UNLOCKED(rfcomm_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
42static void rfcomm_sock_close(struct sock *sk);
43static void rfcomm_sock_kill(struct sock *sk);
44
45/* ---- DLC callbacks ----
46 *
47 * called under rfcomm_dlc_lock()
48 */
49static void rfcomm_sk_data_ready(struct rfcomm_dlc *d, struct sk_buff *skb)
50{
51 struct sock *sk = d->owner;
52 if (!sk)
53 return;
54
55 atomic_add(skb->len, &sk->sk_rmem_alloc);
56 skb_queue_tail(&sk->sk_receive_queue, skb);
57 sk->sk_data_ready(sk, skb->len);
58
59 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
60 rfcomm_dlc_throttle(d);
61}
62
63static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
64{
65 struct sock *sk = d->owner, *parent;
Gustavo F. Padovanfad003b2010-08-14 00:48:07 -030066 unsigned long flags;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (!sk)
69 return;
70
71 BT_DBG("dlc %p state %ld err %d", d, d->state, err);
72
Gustavo F. Padovanfad003b2010-08-14 00:48:07 -030073 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 bh_lock_sock(sk);
75
76 if (err)
77 sk->sk_err = err;
78
79 sk->sk_state = d->state;
80
81 parent = bt_sk(sk)->parent;
82 if (parent) {
83 if (d->state == BT_CLOSED) {
84 sock_set_flag(sk, SOCK_ZAPPED);
85 bt_accept_unlink(sk);
86 }
87 parent->sk_data_ready(parent, 0);
88 } else {
89 if (d->state == BT_CONNECTED)
90 rfcomm_session_getaddr(d->session, &bt_sk(sk)->src, NULL);
91 sk->sk_state_change(sk);
92 }
93
94 bh_unlock_sock(sk);
Gustavo F. Padovanfad003b2010-08-14 00:48:07 -030095 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 if (parent && sock_flag(sk, SOCK_ZAPPED)) {
98 /* We have to drop DLC lock here, otherwise
99 * rfcomm_sock_destruct() will dead lock. */
100 rfcomm_dlc_unlock(d);
101 rfcomm_sock_kill(sk);
102 rfcomm_dlc_lock(d);
103 }
104}
105
106/* ---- Socket functions ---- */
107static struct sock *__rfcomm_get_sock_by_addr(u8 channel, bdaddr_t *src)
108{
109 struct sock *sk = NULL;
110 struct hlist_node *node;
111
112 sk_for_each(sk, node, &rfcomm_sk_list.head) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900113 if (rfcomm_pi(sk)->channel == channel &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 !bacmp(&bt_sk(sk)->src, src))
115 break;
116 }
117
118 return node ? sk : NULL;
119}
120
121/* Find socket with channel and source bdaddr.
122 * Returns closest match.
123 */
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000124static struct sock *rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct sock *sk = NULL, *sk1 = NULL;
127 struct hlist_node *node;
128
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000129 read_lock(&rfcomm_sk_list.lock);
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 sk_for_each(sk, node, &rfcomm_sk_list.head) {
132 if (state && sk->sk_state != state)
133 continue;
134
135 if (rfcomm_pi(sk)->channel == channel) {
136 /* Exact match. */
137 if (!bacmp(&bt_sk(sk)->src, src))
138 break;
139
140 /* Closest match */
141 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
142 sk1 = sk;
143 }
144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 read_unlock(&rfcomm_sk_list.lock);
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000147
148 return node ? sk : sk1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151static void rfcomm_sock_destruct(struct sock *sk)
152{
153 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
154
155 BT_DBG("sk %p dlc %p", sk, d);
156
157 skb_queue_purge(&sk->sk_receive_queue);
158 skb_queue_purge(&sk->sk_write_queue);
159
160 rfcomm_dlc_lock(d);
161 rfcomm_pi(sk)->dlc = NULL;
162
163 /* Detach DLC if it's owned by this socket */
164 if (d->owner == sk)
165 d->owner = NULL;
166 rfcomm_dlc_unlock(d);
167
168 rfcomm_dlc_put(d);
169}
170
171static void rfcomm_sock_cleanup_listen(struct sock *parent)
172{
173 struct sock *sk;
174
175 BT_DBG("parent %p", parent);
176
177 /* Close not yet accepted dlcs */
178 while ((sk = bt_accept_dequeue(parent, NULL))) {
179 rfcomm_sock_close(sk);
180 rfcomm_sock_kill(sk);
181 }
182
183 parent->sk_state = BT_CLOSED;
184 sock_set_flag(parent, SOCK_ZAPPED);
185}
186
187/* Kill socket (only if zapped and orphan)
188 * Must be called on unlocked socket.
189 */
190static void rfcomm_sock_kill(struct sock *sk)
191{
192 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
193 return;
194
195 BT_DBG("sk %p state %d refcnt %d", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
196
197 /* Kill poor orphan */
198 bt_sock_unlink(&rfcomm_sk_list, sk);
199 sock_set_flag(sk, SOCK_DEAD);
200 sock_put(sk);
201}
202
203static void __rfcomm_sock_close(struct sock *sk)
204{
205 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
206
207 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
208
209 switch (sk->sk_state) {
210 case BT_LISTEN:
211 rfcomm_sock_cleanup_listen(sk);
212 break;
213
214 case BT_CONNECT:
215 case BT_CONNECT2:
216 case BT_CONFIG:
217 case BT_CONNECTED:
218 rfcomm_dlc_close(d, 0);
219
220 default:
221 sock_set_flag(sk, SOCK_ZAPPED);
222 break;
223 }
224}
225
226/* Close socket.
227 * Must be called on unlocked socket.
228 */
229static void rfcomm_sock_close(struct sock *sk)
230{
231 lock_sock(sk);
232 __rfcomm_sock_close(sk);
233 release_sock(sk);
234}
235
236static void rfcomm_sock_init(struct sock *sk, struct sock *parent)
237{
238 struct rfcomm_pinfo *pi = rfcomm_pi(sk);
239
240 BT_DBG("sk %p", sk);
241
242 if (parent) {
243 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300244 pi->dlc->defer_setup = test_bit(BT_SK_DEFER_SETUP,
245 &bt_sk(parent)->flags);
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100246
247 pi->sec_level = rfcomm_pi(parent)->sec_level;
248 pi->role_switch = rfcomm_pi(parent)->role_switch;
Paul Moore6230c9b2011-10-07 09:40:59 +0000249
250 security_sk_clone(parent, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 } else {
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100252 pi->dlc->defer_setup = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100253
254 pi->sec_level = BT_SECURITY_LOW;
255 pi->role_switch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100258 pi->dlc->sec_level = pi->sec_level;
259 pi->dlc->role_switch = pi->role_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262static struct proto rfcomm_proto = {
263 .name = "RFCOMM",
264 .owner = THIS_MODULE,
265 .obj_size = sizeof(struct rfcomm_pinfo)
266};
267
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700268static struct sock *rfcomm_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct rfcomm_dlc *d;
271 struct sock *sk;
272
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700273 sk = sk_alloc(net, PF_BLUETOOTH, prio, &rfcomm_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (!sk)
275 return NULL;
276
277 sock_init_data(sock, sk);
278 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
279
280 d = rfcomm_dlc_alloc(prio);
281 if (!d) {
282 sk_free(sk);
283 return NULL;
284 }
285
286 d->data_ready = rfcomm_sk_data_ready;
287 d->state_change = rfcomm_sk_state_change;
288
289 rfcomm_pi(sk)->dlc = d;
290 d->owner = sk;
291
292 sk->sk_destruct = rfcomm_sock_destruct;
293 sk->sk_sndtimeo = RFCOMM_CONN_TIMEOUT;
294
Marcel Holtmann77db1982008-07-14 20:13:45 +0200295 sk->sk_sndbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
296 sk->sk_rcvbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 sock_reset_flag(sk, SOCK_ZAPPED);
299
300 sk->sk_protocol = proto;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200301 sk->sk_state = BT_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 bt_sock_link(&rfcomm_sk_list, sk);
304
305 BT_DBG("sk %p", sk);
306 return sk;
307}
308
Eric Paris3f378b62009-11-05 22:18:14 -0800309static int rfcomm_sock_create(struct net *net, struct socket *sock,
310 int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct sock *sk;
313
314 BT_DBG("sock %p", sock);
315
316 sock->state = SS_UNCONNECTED;
317
318 if (sock->type != SOCK_STREAM && sock->type != SOCK_RAW)
319 return -ESOCKTNOSUPPORT;
320
321 sock->ops = &rfcomm_sock_ops;
322
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700323 sk = rfcomm_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200324 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return -ENOMEM;
326
327 rfcomm_sock_init(sk, NULL);
328 return 0;
329}
330
331static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
332{
333 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
334 struct sock *sk = sock->sk;
335 int err = 0;
336
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300337 BT_DBG("sk %p %pMR", sk, &sa->rc_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (!addr || addr->sa_family != AF_BLUETOOTH)
340 return -EINVAL;
341
342 lock_sock(sk);
343
344 if (sk->sk_state != BT_OPEN) {
345 err = -EBADFD;
346 goto done;
347 }
348
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200349 if (sk->sk_type != SOCK_STREAM) {
350 err = -EINVAL;
351 goto done;
352 }
353
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200354 write_lock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if (sa->rc_channel && __rfcomm_get_sock_by_addr(sa->rc_channel, &sa->rc_bdaddr)) {
357 err = -EADDRINUSE;
358 } else {
359 /* Save source address */
360 bacpy(&bt_sk(sk)->src, &sa->rc_bdaddr);
361 rfcomm_pi(sk)->channel = sa->rc_channel;
362 sk->sk_state = BT_BOUND;
363 }
364
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200365 write_unlock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367done:
368 release_sock(sk);
369 return err;
370}
371
372static int rfcomm_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
373{
374 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
375 struct sock *sk = sock->sk;
376 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
377 int err = 0;
378
379 BT_DBG("sk %p", sk);
380
Changli Gao6503d962010-03-31 22:58:26 +0000381 if (alen < sizeof(struct sockaddr_rc) ||
382 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return -EINVAL;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 lock_sock(sk);
386
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200387 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
388 err = -EBADFD;
389 goto done;
390 }
391
392 if (sk->sk_type != SOCK_STREAM) {
393 err = -EINVAL;
394 goto done;
395 }
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 sk->sk_state = BT_CONNECT;
398 bacpy(&bt_sk(sk)->dst, &sa->rc_bdaddr);
399 rfcomm_pi(sk)->channel = sa->rc_channel;
400
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100401 d->sec_level = rfcomm_pi(sk)->sec_level;
402 d->role_switch = rfcomm_pi(sk)->role_switch;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 err = rfcomm_dlc_open(d, &bt_sk(sk)->src, &sa->rc_bdaddr, sa->rc_channel);
405 if (!err)
406 err = bt_sock_wait_state(sk, BT_CONNECTED,
407 sock_sndtimeo(sk, flags & O_NONBLOCK));
408
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200409done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 release_sock(sk);
411 return err;
412}
413
414static int rfcomm_sock_listen(struct socket *sock, int backlog)
415{
416 struct sock *sk = sock->sk;
417 int err = 0;
418
419 BT_DBG("sk %p backlog %d", sk, backlog);
420
421 lock_sock(sk);
422
423 if (sk->sk_state != BT_BOUND) {
424 err = -EBADFD;
425 goto done;
426 }
427
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200428 if (sk->sk_type != SOCK_STREAM) {
429 err = -EINVAL;
430 goto done;
431 }
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (!rfcomm_pi(sk)->channel) {
434 bdaddr_t *src = &bt_sk(sk)->src;
435 u8 channel;
436
437 err = -EINVAL;
438
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200439 write_lock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 for (channel = 1; channel < 31; channel++)
442 if (!__rfcomm_get_sock_by_addr(channel, src)) {
443 rfcomm_pi(sk)->channel = channel;
444 err = 0;
445 break;
446 }
447
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200448 write_unlock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (err < 0)
451 goto done;
452 }
453
454 sk->sk_max_ack_backlog = backlog;
455 sk->sk_ack_backlog = 0;
456 sk->sk_state = BT_LISTEN;
457
458done:
459 release_sock(sk);
460 return err;
461}
462
463static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int flags)
464{
465 DECLARE_WAITQUEUE(wait, current);
466 struct sock *sk = sock->sk, *nsk;
467 long timeo;
468 int err = 0;
469
470 lock_sock(sk);
471
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200472 if (sk->sk_type != SOCK_STREAM) {
473 err = -EINVAL;
474 goto done;
475 }
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
478
479 BT_DBG("sk %p timeo %ld", sk, timeo);
480
481 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000482 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurley950e2d52011-07-24 00:10:41 -0400483 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (sk->sk_state != BT_LISTEN) {
487 err = -EBADFD;
488 break;
489 }
490
Peter Hurley950e2d52011-07-24 00:10:41 -0400491 nsk = bt_accept_dequeue(sk, newsock);
492 if (nsk)
493 break;
494
495 if (!timeo) {
496 err = -EAGAIN;
497 break;
498 }
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (signal_pending(current)) {
501 err = sock_intr_errno(timeo);
502 break;
503 }
Peter Hurley950e2d52011-07-24 00:10:41 -0400504
505 release_sock(sk);
506 timeo = schedule_timeout(timeo);
507 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Peter Hurley950e2d52011-07-24 00:10:41 -0400509 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000510 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 if (err)
513 goto done;
514
515 newsock->state = SS_CONNECTED;
516
517 BT_DBG("new socket %p", nsk);
518
519done:
520 release_sock(sk);
521 return err;
522}
523
524static int rfcomm_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
525{
526 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
527 struct sock *sk = sock->sk;
528
529 BT_DBG("sock %p, sk %p", sock, sk);
530
531 sa->rc_family = AF_BLUETOOTH;
532 sa->rc_channel = rfcomm_pi(sk)->channel;
533 if (peer)
534 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->dst);
535 else
536 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->src);
537
538 *len = sizeof(struct sockaddr_rc);
539 return 0;
540}
541
542static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
543 struct msghdr *msg, size_t len)
544{
545 struct sock *sk = sock->sk;
546 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
547 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int sent = 0;
549
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100550 if (test_bit(RFCOMM_DEFER_SETUP, &d->flags))
551 return -ENOTCONN;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (msg->msg_flags & MSG_OOB)
554 return -EOPNOTSUPP;
555
556 if (sk->sk_shutdown & SEND_SHUTDOWN)
557 return -EPIPE;
558
559 BT_DBG("sock %p, sk %p", sock, sk);
560
561 lock_sock(sk);
562
563 while (len) {
564 size_t size = min_t(size_t, len, d->mtu);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100565 int err;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 skb = sock_alloc_send_skb(sk, size + RFCOMM_SKB_RESERVE,
568 msg->msg_flags & MSG_DONTWAIT, &err);
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100569 if (!skb) {
570 if (sent == 0)
571 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
575
576 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
577 if (err) {
578 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100579 if (sent == 0)
580 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
582 }
583
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200584 skb->priority = sk->sk_priority;
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 err = rfcomm_dlc_send(d, skb);
587 if (err < 0) {
588 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100589 if (sent == 0)
590 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 break;
592 }
593
594 sent += size;
595 len -= size;
596 }
597
598 release_sock(sk);
599
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100600 return sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603static int rfcomm_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
604 struct msghdr *msg, size_t size, int flags)
605{
606 struct sock *sk = sock->sk;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100607 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700608 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100610 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
611 rfcomm_dlc_accept(d);
612 return 0;
613 }
614
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700615 len = bt_sock_stream_recvmsg(iocb, sock, msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 lock_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700618 if (!(flags & MSG_PEEK) && len > 0)
619 atomic_sub(len, &sk->sk_rmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (atomic_read(&sk->sk_rmem_alloc) <= (sk->sk_rcvbuf >> 2))
622 rfcomm_dlc_unthrottle(rfcomm_pi(sk)->dlc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 release_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700624
625 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
David S. Millerb7058842009-09-30 16:12:20 -0700628static int rfcomm_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct sock *sk = sock->sk;
631 int err = 0;
632 u32 opt;
633
634 BT_DBG("sk %p", sk);
635
636 lock_sock(sk);
637
638 switch (optname) {
639 case RFCOMM_LM:
640 if (get_user(opt, (u32 __user *) optval)) {
641 err = -EFAULT;
642 break;
643 }
644
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100645 if (opt & RFCOMM_LM_AUTH)
646 rfcomm_pi(sk)->sec_level = BT_SECURITY_LOW;
647 if (opt & RFCOMM_LM_ENCRYPT)
648 rfcomm_pi(sk)->sec_level = BT_SECURITY_MEDIUM;
649 if (opt & RFCOMM_LM_SECURE)
650 rfcomm_pi(sk)->sec_level = BT_SECURITY_HIGH;
651
652 rfcomm_pi(sk)->role_switch = (opt & RFCOMM_LM_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 break;
654
655 default:
656 err = -ENOPROTOOPT;
657 break;
658 }
659
660 release_sock(sk);
661 return err;
662}
663
David S. Millerb7058842009-09-30 16:12:20 -0700664static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100665{
666 struct sock *sk = sock->sk;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100667 struct bt_security sec;
Stephen Boydd0fad892011-05-12 16:50:09 -0700668 int err = 0;
669 size_t len;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100670 u32 opt;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100671
672 BT_DBG("sk %p", sk);
673
674 if (level == SOL_RFCOMM)
675 return rfcomm_sock_setsockopt_old(sock, optname, optval, optlen);
676
Marcel Holtmann0588d942009-01-16 10:06:13 +0100677 if (level != SOL_BLUETOOTH)
678 return -ENOPROTOOPT;
679
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100680 lock_sock(sk);
681
682 switch (optname) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100683 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +0100684 if (sk->sk_type != SOCK_STREAM) {
685 err = -EINVAL;
686 break;
687 }
688
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100689 sec.level = BT_SECURITY_LOW;
690
691 len = min_t(unsigned int, sizeof(sec), optlen);
692 if (copy_from_user((char *) &sec, optval, len)) {
693 err = -EFAULT;
694 break;
695 }
696
697 if (sec.level > BT_SECURITY_HIGH) {
698 err = -EINVAL;
699 break;
700 }
701
702 rfcomm_pi(sk)->sec_level = sec.level;
703 break;
704
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100705 case BT_DEFER_SETUP:
706 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
707 err = -EINVAL;
708 break;
709 }
710
711 if (get_user(opt, (u32 __user *) optval)) {
712 err = -EFAULT;
713 break;
714 }
715
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300716 if (opt)
717 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
718 else
719 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
720
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100721 break;
722
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100723 default:
724 err = -ENOPROTOOPT;
725 break;
726 }
727
728 release_sock(sk);
729 return err;
730}
731
732static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct rfcomm_conninfo cinfo;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300736 struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int len, err = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100738 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 BT_DBG("sk %p", sk);
741
742 if (get_user(len, optlen))
743 return -EFAULT;
744
745 lock_sock(sk);
746
747 switch (optname) {
748 case RFCOMM_LM:
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100749 switch (rfcomm_pi(sk)->sec_level) {
750 case BT_SECURITY_LOW:
751 opt = RFCOMM_LM_AUTH;
752 break;
753 case BT_SECURITY_MEDIUM:
754 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT;
755 break;
756 case BT_SECURITY_HIGH:
757 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT |
758 RFCOMM_LM_SECURE;
759 break;
760 default:
761 opt = 0;
762 break;
763 }
764
765 if (rfcomm_pi(sk)->role_switch)
766 opt |= RFCOMM_LM_MASTER;
767
768 if (put_user(opt, (u32 __user *) optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 err = -EFAULT;
770 break;
771
772 case RFCOMM_CONNINFO:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100773 if (sk->sk_state != BT_CONNECTED &&
774 !rfcomm_pi(sk)->dlc->defer_setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 err = -ENOTCONN;
776 break;
777 }
778
Filip Palian8d03e972011-05-12 19:32:46 +0200779 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300780 cinfo.hci_handle = conn->hcon->handle;
781 memcpy(cinfo.dev_class, conn->hcon->dev_class, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 len = min_t(unsigned int, len, sizeof(cinfo));
784 if (copy_to_user(optval, (char *) &cinfo, len))
785 err = -EFAULT;
786
787 break;
788
789 default:
790 err = -ENOPROTOOPT;
791 break;
792 }
793
794 release_sock(sk);
795 return err;
796}
797
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100798static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
799{
800 struct sock *sk = sock->sk;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100801 struct bt_security sec;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100802 int len, err = 0;
803
804 BT_DBG("sk %p", sk);
805
806 if (level == SOL_RFCOMM)
807 return rfcomm_sock_getsockopt_old(sock, optname, optval, optlen);
808
Marcel Holtmann0588d942009-01-16 10:06:13 +0100809 if (level != SOL_BLUETOOTH)
810 return -ENOPROTOOPT;
811
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100812 if (get_user(len, optlen))
813 return -EFAULT;
814
815 lock_sock(sk);
816
817 switch (optname) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100818 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +0100819 if (sk->sk_type != SOCK_STREAM) {
820 err = -EINVAL;
821 break;
822 }
823
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100824 sec.level = rfcomm_pi(sk)->sec_level;
825
826 len = min_t(unsigned int, len, sizeof(sec));
827 if (copy_to_user(optval, (char *) &sec, len))
828 err = -EFAULT;
829
830 break;
831
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100832 case BT_DEFER_SETUP:
833 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
834 err = -EINVAL;
835 break;
836 }
837
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300838 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
839 (u32 __user *) optval))
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100840 err = -EFAULT;
841
842 break;
843
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100844 default:
845 err = -ENOPROTOOPT;
846 break;
847 }
848
849 release_sock(sk);
850 return err;
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853static int rfcomm_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
854{
David S. Millere19caae2008-12-09 01:04:27 -0800855 struct sock *sk __maybe_unused = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 int err;
857
David S. Millere19caae2008-12-09 01:04:27 -0800858 BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200860 err = bt_sock_ioctl(sock, cmd, arg);
861
862 if (err == -ENOIOCTLCMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863#ifdef CONFIG_BT_RFCOMM_TTY
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200864 lock_sock(sk);
865 err = rfcomm_dev_ioctl(sk, cmd, (void __user *) arg);
866 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867#else
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200868 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869#endif
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return err;
873}
874
875static int rfcomm_sock_shutdown(struct socket *sock, int how)
876{
877 struct sock *sk = sock->sk;
878 int err = 0;
879
880 BT_DBG("sock %p, sk %p", sock, sk);
881
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200882 if (!sk)
883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 lock_sock(sk);
886 if (!sk->sk_shutdown) {
887 sk->sk_shutdown = SHUTDOWN_MASK;
888 __rfcomm_sock_close(sk);
889
890 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
891 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
892 }
893 release_sock(sk);
894 return err;
895}
896
897static int rfcomm_sock_release(struct socket *sock)
898{
899 struct sock *sk = sock->sk;
900 int err;
901
902 BT_DBG("sock %p, sk %p", sock, sk);
903
904 if (!sk)
905 return 0;
906
907 err = rfcomm_sock_shutdown(sock, 2);
908
909 sock_orphan(sk);
910 rfcomm_sock_kill(sk);
911 return err;
912}
913
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900914/* ---- RFCOMM core layer callbacks ----
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 *
916 * called under rfcomm_lock()
917 */
918int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d)
919{
920 struct sock *sk, *parent;
921 bdaddr_t src, dst;
922 int result = 0;
923
924 BT_DBG("session %p channel %d", s, channel);
925
926 rfcomm_session_getaddr(s, &src, &dst);
927
928 /* Check if we have socket listening on channel */
929 parent = rfcomm_get_sock_by_channel(BT_LISTEN, channel, &src);
930 if (!parent)
931 return 0;
932
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000933 bh_lock_sock(parent);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* Check for backlog size */
936 if (sk_acceptq_is_full(parent)) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900937 BT_DBG("backlog full %d", parent->sk_ack_backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 goto done;
939 }
940
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900941 sk = rfcomm_sock_alloc(sock_net(parent), NULL, BTPROTO_RFCOMM, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!sk)
943 goto done;
944
Octavian Purdilab5a30dd2012-01-22 00:28:34 +0200945 bt_sock_reclassify_lock(sk, BTPROTO_RFCOMM);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 rfcomm_sock_init(sk, parent);
948 bacpy(&bt_sk(sk)->src, &src);
949 bacpy(&bt_sk(sk)->dst, &dst);
950 rfcomm_pi(sk)->channel = channel;
951
952 sk->sk_state = BT_CONFIG;
953 bt_accept_enqueue(parent, sk);
954
955 /* Accept connection and return socket DLC */
956 *d = rfcomm_pi(sk)->dlc;
957 result = 1;
958
959done:
960 bh_unlock_sock(parent);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100961
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300962 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100963 parent->sk_state_change(parent);
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return result;
966}
967
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100968static int rfcomm_sock_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 struct sock *sk;
971 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200973 read_lock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800975 sk_for_each(sk, node, &rfcomm_sk_list.head) {
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +0300976 seq_printf(f, "%pMR %pMR %d %d\n",
977 &bt_sk(sk)->src, &bt_sk(sk)->dst,
978 sk->sk_state, rfcomm_pi(sk)->channel);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200981 read_unlock(&rfcomm_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800982
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100986static int rfcomm_sock_debugfs_open(struct inode *inode, struct file *file)
987{
988 return single_open(file, rfcomm_sock_debugfs_show, inode->i_private);
989}
990
991static const struct file_operations rfcomm_sock_debugfs_fops = {
992 .open = rfcomm_sock_debugfs_open,
993 .read = seq_read,
994 .llseek = seq_lseek,
995 .release = single_release,
996};
997
998static struct dentry *rfcomm_sock_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001000static const struct proto_ops rfcomm_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 .family = PF_BLUETOOTH,
1002 .owner = THIS_MODULE,
1003 .release = rfcomm_sock_release,
1004 .bind = rfcomm_sock_bind,
1005 .connect = rfcomm_sock_connect,
1006 .listen = rfcomm_sock_listen,
1007 .accept = rfcomm_sock_accept,
1008 .getname = rfcomm_sock_getname,
1009 .sendmsg = rfcomm_sock_sendmsg,
1010 .recvmsg = rfcomm_sock_recvmsg,
1011 .shutdown = rfcomm_sock_shutdown,
1012 .setsockopt = rfcomm_sock_setsockopt,
1013 .getsockopt = rfcomm_sock_getsockopt,
1014 .ioctl = rfcomm_sock_ioctl,
1015 .poll = bt_sock_poll,
1016 .socketpair = sock_no_socketpair,
1017 .mmap = sock_no_mmap
1018};
1019
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001020static const struct net_proto_family rfcomm_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 .family = PF_BLUETOOTH,
1022 .owner = THIS_MODULE,
1023 .create = rfcomm_sock_create
1024};
1025
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001026int __init rfcomm_init_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 int err;
1029
1030 err = proto_register(&rfcomm_proto, 0);
1031 if (err < 0)
1032 return err;
1033
1034 err = bt_sock_register(BTPROTO_RFCOMM, &rfcomm_sock_family_ops);
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001035 if (err < 0) {
1036 BT_ERR("RFCOMM socket layer registration failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 goto error;
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001038 }
1039
1040 err = bt_procfs_init(THIS_MODULE, &init_net, "rfcomm", &rfcomm_sk_list, NULL);
1041 if (err < 0) {
1042 BT_ERR("Failed to create RFCOMM proc file");
1043 bt_sock_unregister(BTPROTO_RFCOMM);
1044 goto error;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001047 if (bt_debugfs) {
1048 rfcomm_sock_debugfs = debugfs_create_file("rfcomm", 0444,
1049 bt_debugfs, NULL, &rfcomm_sock_debugfs_fops);
1050 if (!rfcomm_sock_debugfs)
1051 BT_ERR("Failed to create RFCOMM debug file");
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 BT_INFO("RFCOMM socket layer initialized");
1055
1056 return 0;
1057
1058error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 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{
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001065 bt_procfs_cleanup(&init_net, "rfcomm");
1066
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001067 debugfs_remove(rfcomm_sock_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 if (bt_sock_unregister(BTPROTO_RFCOMM) < 0)
1070 BT_ERR("RFCOMM socket layer unregistration failed");
1071
1072 proto_unregister(&rfcomm_proto);
1073}