blob: 55d483067f49e506f1159755dd732a09362f147d [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{
Teow Wan Yeea9467512016-09-23 15:59:50 +0800349 struct sockaddr_rc sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct sock *sk = sock->sk;
Teow Wan Yeea9467512016-09-23 15:59:50 +0800351
352 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!addr || addr->sa_family != AF_BLUETOOTH)
354 return -EINVAL;
355
Teow Wan Yeea9467512016-09-23 15:59:50 +0800356 memset(&sa, 0, sizeof(sa));
357 len = min_t(unsigned int, sizeof(sa), addr_len);
358 memcpy(&sa, addr, len);
359
360 BT_DBG("sk %p %pMR", sk, &sa.rc_bdaddr);
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 lock_sock(sk);
363
364 if (sk->sk_state != BT_OPEN) {
365 err = -EBADFD;
366 goto done;
367 }
368
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200369 if (sk->sk_type != SOCK_STREAM) {
370 err = -EINVAL;
371 goto done;
372 }
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 write_lock_bh(&rfcomm_sk_list.lock);
Teow Wan Yeea9467512016-09-23 15:59:50 +0800375 if (sa.rc_channel && __rfcomm_get_sock_by_addr(sa.rc_channel, &sa.rc_bdaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 err = -EADDRINUSE;
377 } else {
378 /* Save source address */
Teow Wan Yeea9467512016-09-23 15:59:50 +0800379 bacpy(&bt_sk(sk)->src, &sa.rc_bdaddr);
380 rfcomm_pi(sk)->channel = sa.rc_channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 sk->sk_state = BT_BOUND;
382 }
383
384 write_unlock_bh(&rfcomm_sk_list.lock);
385
386done:
387 release_sock(sk);
388 return err;
389}
390
391static int rfcomm_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
392{
393 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
394 struct sock *sk = sock->sk;
395 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
396 int err = 0;
397
398 BT_DBG("sk %p", sk);
399
Changli Gao6503d962010-03-31 22:58:26 +0000400 if (alen < sizeof(struct sockaddr_rc) ||
401 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return -EINVAL;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 lock_sock(sk);
405
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200406 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
407 err = -EBADFD;
408 goto done;
409 }
410
411 if (sk->sk_type != SOCK_STREAM) {
412 err = -EINVAL;
413 goto done;
414 }
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 sk->sk_state = BT_CONNECT;
417 bacpy(&bt_sk(sk)->dst, &sa->rc_bdaddr);
418 rfcomm_pi(sk)->channel = sa->rc_channel;
419
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100420 d->sec_level = rfcomm_pi(sk)->sec_level;
421 d->role_switch = rfcomm_pi(sk)->role_switch;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 err = rfcomm_dlc_open(d, &bt_sk(sk)->src, &sa->rc_bdaddr, sa->rc_channel);
424 if (!err)
425 err = bt_sock_wait_state(sk, BT_CONNECTED,
426 sock_sndtimeo(sk, flags & O_NONBLOCK));
427
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200428done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 release_sock(sk);
430 return err;
431}
432
433static int rfcomm_sock_listen(struct socket *sock, int backlog)
434{
435 struct sock *sk = sock->sk;
436 int err = 0;
437
438 BT_DBG("sk %p backlog %d", sk, backlog);
439
440 lock_sock(sk);
441
442 if (sk->sk_state != BT_BOUND) {
443 err = -EBADFD;
444 goto done;
445 }
446
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200447 if (sk->sk_type != SOCK_STREAM) {
448 err = -EINVAL;
449 goto done;
450 }
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!rfcomm_pi(sk)->channel) {
453 bdaddr_t *src = &bt_sk(sk)->src;
454 u8 channel;
455
456 err = -EINVAL;
457
458 write_lock_bh(&rfcomm_sk_list.lock);
459
460 for (channel = 1; channel < 31; channel++)
461 if (!__rfcomm_get_sock_by_addr(channel, src)) {
462 rfcomm_pi(sk)->channel = channel;
463 err = 0;
464 break;
465 }
466
467 write_unlock_bh(&rfcomm_sk_list.lock);
468
469 if (err < 0)
470 goto done;
471 }
472
473 sk->sk_max_ack_backlog = backlog;
474 sk->sk_ack_backlog = 0;
475 sk->sk_state = BT_LISTEN;
476
477done:
478 release_sock(sk);
479 return err;
480}
481
482static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int flags)
483{
484 DECLARE_WAITQUEUE(wait, current);
485 struct sock *sk = sock->sk, *nsk;
486 long timeo;
487 int err = 0;
488
489 lock_sock(sk);
490
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491 if (sk->sk_state != BT_LISTEN) {
492 err = -EBADFD;
493 goto done;
494 }
495
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200496 if (sk->sk_type != SOCK_STREAM) {
497 err = -EINVAL;
498 goto done;
499 }
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
502
503 BT_DBG("sk %p timeo %ld", sk, timeo);
504
505 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000506 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 set_current_state(TASK_INTERRUPTIBLE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 if (!timeo) {
510 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 break;
512 }
513
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 release_sock(sk);
515 timeo = schedule_timeout(timeo);
516 lock_sock(sk);
Peter Hurleye7a40bf2011-07-24 00:10:41 -0400517
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518 if (sk->sk_state != BT_LISTEN) {
519 err = -EBADFD;
Peter Hurleye7a40bf2011-07-24 00:10:41 -0400520 break;
521 }
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (signal_pending(current)) {
524 err = sock_intr_errno(timeo);
525 break;
526 }
527 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000529 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (err)
532 goto done;
533
534 newsock->state = SS_CONNECTED;
535
536 BT_DBG("new socket %p", nsk);
537
538done:
539 release_sock(sk);
540 return err;
541}
542
543static int rfcomm_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
544{
545 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
546 struct sock *sk = sock->sk;
547
548 BT_DBG("sock %p, sk %p", sock, sk);
549
550 sa->rc_family = AF_BLUETOOTH;
551 sa->rc_channel = rfcomm_pi(sk)->channel;
552 if (peer)
553 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->dst);
554 else
555 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->src);
556
557 *len = sizeof(struct sockaddr_rc);
558 return 0;
559}
560
561static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
562 struct msghdr *msg, size_t len)
563{
564 struct sock *sk = sock->sk;
565 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
566 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int sent = 0;
568
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100569 if (test_bit(RFCOMM_DEFER_SETUP, &d->flags))
570 return -ENOTCONN;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (msg->msg_flags & MSG_OOB)
573 return -EOPNOTSUPP;
574
575 if (sk->sk_shutdown & SEND_SHUTDOWN)
576 return -EPIPE;
577
578 BT_DBG("sock %p, sk %p", sock, sk);
579
580 lock_sock(sk);
581
582 while (len) {
583 size_t size = min_t(size_t, len, d->mtu);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100584 int err;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 skb = sock_alloc_send_skb(sk, size + RFCOMM_SKB_RESERVE,
587 msg->msg_flags & MSG_DONTWAIT, &err);
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100588 if (!skb) {
589 if (sent == 0)
590 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 break;
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
594
595 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
596 if (err) {
597 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100598 if (sent == 0)
599 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
601 }
602
603 err = rfcomm_dlc_send(d, skb);
604 if (err < 0) {
605 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100606 if (sent == 0)
607 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 break;
609 }
610
611 sent += size;
612 len -= size;
613 }
614
615 release_sock(sk);
616
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100617 return sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620static int rfcomm_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
621 struct msghdr *msg, size_t size, int flags)
622{
623 struct sock *sk = sock->sk;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100624 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700625 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100627 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
628 rfcomm_dlc_accept(d);
629 return 0;
630 }
631
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700632 len = bt_sock_stream_recvmsg(iocb, sock, msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 lock_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700635 if (!(flags & MSG_PEEK) && len > 0)
636 atomic_sub(len, &sk->sk_rmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (atomic_read(&sk->sk_rmem_alloc) <= (sk->sk_rcvbuf >> 2))
639 rfcomm_dlc_unthrottle(rfcomm_pi(sk)->dlc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 release_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700641
642 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
David S. Millerb7058842009-09-30 16:12:20 -0700645static int rfcomm_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct sock *sk = sock->sk;
648 int err = 0;
649 u32 opt;
650
651 BT_DBG("sk %p", sk);
652
653 lock_sock(sk);
654
655 switch (optname) {
656 case RFCOMM_LM:
657 if (get_user(opt, (u32 __user *) optval)) {
658 err = -EFAULT;
659 break;
660 }
661
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100662 if (opt & RFCOMM_LM_AUTH)
663 rfcomm_pi(sk)->sec_level = BT_SECURITY_LOW;
664 if (opt & RFCOMM_LM_ENCRYPT)
665 rfcomm_pi(sk)->sec_level = BT_SECURITY_MEDIUM;
666 if (opt & RFCOMM_LM_SECURE)
667 rfcomm_pi(sk)->sec_level = BT_SECURITY_HIGH;
668
669 rfcomm_pi(sk)->role_switch = (opt & RFCOMM_LM_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 break;
671
672 default:
673 err = -ENOPROTOOPT;
674 break;
675 }
676
677 release_sock(sk);
678 return err;
679}
680
David S. Millerb7058842009-09-30 16:12:20 -0700681static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100682{
683 struct sock *sk = sock->sk;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100684 struct bt_security sec;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685 int len, err = 0;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100686 u32 opt;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100687
688 BT_DBG("sk %p", sk);
689
690 if (level == SOL_RFCOMM)
691 return rfcomm_sock_setsockopt_old(sock, optname, optval, optlen);
692
Marcel Holtmann0588d942009-01-16 10:06:13 +0100693 if (level != SOL_BLUETOOTH)
694 return -ENOPROTOOPT;
695
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100696 lock_sock(sk);
697
698 switch (optname) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100699 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +0100700 if (sk->sk_type != SOCK_STREAM) {
701 err = -EINVAL;
702 break;
703 }
704
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100705 sec.level = BT_SECURITY_LOW;
706
707 len = min_t(unsigned int, sizeof(sec), optlen);
708 if (copy_from_user((char *) &sec, optval, len)) {
709 err = -EFAULT;
710 break;
711 }
712
Bhakthavatsala Raghavendrab03b5702013-02-12 19:44:47 +0530713 if (sec.level > BT_SECURITY_VERY_HIGH) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100714 err = -EINVAL;
715 break;
716 }
717
718 rfcomm_pi(sk)->sec_level = sec.level;
Bhakthavatsala Raghavendrab03b5702013-02-12 19:44:47 +0530719 BT_DBG("set to %d", sec.level);
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100720 break;
721
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100722 case BT_DEFER_SETUP:
723 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
724 err = -EINVAL;
725 break;
726 }
727
728 if (get_user(opt, (u32 __user *) optval)) {
729 err = -EFAULT;
730 break;
731 }
732
733 bt_sk(sk)->defer_setup = opt;
734 break;
735
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100736 default:
737 err = -ENOPROTOOPT;
738 break;
739 }
740
741 release_sock(sk);
742 return err;
743}
744
745static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct sock *sk = sock->sk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 struct sock *l2cap_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct rfcomm_conninfo cinfo;
750 int len, err = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100751 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 BT_DBG("sk %p", sk);
754
755 if (get_user(len, optlen))
756 return -EFAULT;
757
758 lock_sock(sk);
759
760 switch (optname) {
761 case RFCOMM_LM:
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100762 switch (rfcomm_pi(sk)->sec_level) {
763 case BT_SECURITY_LOW:
764 opt = RFCOMM_LM_AUTH;
765 break;
766 case BT_SECURITY_MEDIUM:
767 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT;
768 break;
769 case BT_SECURITY_HIGH:
Bhakthavatsala Raghavendrab03b5702013-02-12 19:44:47 +0530770 case BT_SECURITY_VERY_HIGH:
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100771 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT |
772 RFCOMM_LM_SECURE;
773 break;
774 default:
775 opt = 0;
776 break;
777 }
778
779 if (rfcomm_pi(sk)->role_switch)
780 opt |= RFCOMM_LM_MASTER;
781
782 if (put_user(opt, (u32 __user *) optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 err = -EFAULT;
784 break;
785
786 case RFCOMM_CONNINFO:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100787 if (sk->sk_state != BT_CONNECTED &&
788 !rfcomm_pi(sk)->dlc->defer_setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 err = -ENOTCONN;
790 break;
791 }
792
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793 l2cap_sk = rfcomm_pi(sk)->dlc->session->sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795 cinfo.hci_handle = l2cap_pi(l2cap_sk)->conn->hcon->handle;
796 memcpy(cinfo.dev_class, l2cap_pi(l2cap_sk)->conn->hcon->dev_class, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
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;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100816 struct bt_security sec;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100817 int len, err = 0;
818
819 BT_DBG("sk %p", sk);
820
821 if (level == SOL_RFCOMM)
822 return rfcomm_sock_getsockopt_old(sock, optname, optval, optlen);
823
Marcel Holtmann0588d942009-01-16 10:06:13 +0100824 if (level != SOL_BLUETOOTH)
825 return -ENOPROTOOPT;
826
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100827 if (get_user(len, optlen))
828 return -EFAULT;
829
830 lock_sock(sk);
831
832 switch (optname) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100833 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +0100834 if (sk->sk_type != SOCK_STREAM) {
835 err = -EINVAL;
836 break;
837 }
838
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100839 sec.level = rfcomm_pi(sk)->sec_level;
840
841 len = min_t(unsigned int, len, sizeof(sec));
842 if (copy_to_user(optval, (char *) &sec, len))
843 err = -EFAULT;
844
845 break;
846
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100847 case BT_DEFER_SETUP:
848 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
849 err = -EINVAL;
850 break;
851 }
852
853 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
854 err = -EFAULT;
855
856 break;
857
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100858 default:
859 err = -ENOPROTOOPT;
860 break;
861 }
862
863 release_sock(sk);
864 return err;
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867static int rfcomm_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
868{
David S. Millere19caae2008-12-09 01:04:27 -0800869 struct sock *sk __maybe_unused = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 int err;
871
David S. Millere19caae2008-12-09 01:04:27 -0800872 BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200874 err = bt_sock_ioctl(sock, cmd, arg);
875
876 if (err == -ENOIOCTLCMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877#ifdef CONFIG_BT_RFCOMM_TTY
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200878 lock_sock(sk);
879 err = rfcomm_dev_ioctl(sk, cmd, (void __user *) arg);
880 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881#else
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200882 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883#endif
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return err;
887}
888
889static int rfcomm_sock_shutdown(struct socket *sock, int how)
890{
891 struct sock *sk = sock->sk;
892 int err = 0;
893
894 BT_DBG("sock %p, sk %p", sock, sk);
895
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200896 if (!sk)
897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 lock_sock(sk);
900 if (!sk->sk_shutdown) {
901 sk->sk_shutdown = SHUTDOWN_MASK;
902 __rfcomm_sock_close(sk);
903
904 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
905 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
906 }
907 release_sock(sk);
908 return err;
909}
910
911static int rfcomm_sock_release(struct socket *sock)
912{
913 struct sock *sk = sock->sk;
914 int err;
915
916 BT_DBG("sock %p, sk %p", sock, sk);
917
918 if (!sk)
919 return 0;
920
921 err = rfcomm_sock_shutdown(sock, 2);
922
923 sock_orphan(sk);
924 rfcomm_sock_kill(sk);
925 return err;
926}
927
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900928/* ---- RFCOMM core layer callbacks ----
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
930 * called under rfcomm_lock()
931 */
932int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d)
933{
934 struct sock *sk, *parent;
935 bdaddr_t src, dst;
936 int result = 0;
937
938 BT_DBG("session %p channel %d", s, channel);
939
940 rfcomm_session_getaddr(s, &src, &dst);
941
942 /* Check if we have socket listening on channel */
943 parent = rfcomm_get_sock_by_channel(BT_LISTEN, channel, &src);
944 if (!parent)
945 return 0;
946
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000947 bh_lock_sock(parent);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /* Check for backlog size */
950 if (sk_acceptq_is_full(parent)) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900951 BT_DBG("backlog full %d", parent->sk_ack_backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 goto done;
953 }
954
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900955 sk = rfcomm_sock_alloc(sock_net(parent), NULL, BTPROTO_RFCOMM, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (!sk)
957 goto done;
958
959 rfcomm_sock_init(sk, parent);
960 bacpy(&bt_sk(sk)->src, &src);
961 bacpy(&bt_sk(sk)->dst, &dst);
962 rfcomm_pi(sk)->channel = channel;
963
964 sk->sk_state = BT_CONFIG;
965 bt_accept_enqueue(parent, sk);
966
967 /* Accept connection and return socket DLC */
968 *d = rfcomm_pi(sk)->dlc;
969 result = 1;
970
971done:
972 bh_unlock_sock(parent);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100973
974 if (bt_sk(parent)->defer_setup)
975 parent->sk_state_change(parent);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return result;
978}
979
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100980static int rfcomm_sock_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 struct sock *sk;
983 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 read_lock_bh(&rfcomm_sk_list.lock);
986
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800987 sk_for_each(sk, node, &rfcomm_sk_list.head) {
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100988 seq_printf(f, "%s %s %d %d\n",
989 batostr(&bt_sk(sk)->src),
990 batostr(&bt_sk(sk)->dst),
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800991 sk->sk_state, rfcomm_pi(sk)->channel);
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 read_unlock_bh(&rfcomm_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800995
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100999static int rfcomm_sock_debugfs_open(struct inode *inode, struct file *file)
1000{
1001 return single_open(file, rfcomm_sock_debugfs_show, inode->i_private);
1002}
1003
1004static const struct file_operations rfcomm_sock_debugfs_fops = {
1005 .open = rfcomm_sock_debugfs_open,
1006 .read = seq_read,
1007 .llseek = seq_lseek,
1008 .release = single_release,
1009};
1010
1011static struct dentry *rfcomm_sock_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001013static const struct proto_ops rfcomm_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 .family = PF_BLUETOOTH,
1015 .owner = THIS_MODULE,
1016 .release = rfcomm_sock_release,
1017 .bind = rfcomm_sock_bind,
1018 .connect = rfcomm_sock_connect,
1019 .listen = rfcomm_sock_listen,
1020 .accept = rfcomm_sock_accept,
1021 .getname = rfcomm_sock_getname,
1022 .sendmsg = rfcomm_sock_sendmsg,
1023 .recvmsg = rfcomm_sock_recvmsg,
1024 .shutdown = rfcomm_sock_shutdown,
1025 .setsockopt = rfcomm_sock_setsockopt,
1026 .getsockopt = rfcomm_sock_getsockopt,
1027 .ioctl = rfcomm_sock_ioctl,
1028 .poll = bt_sock_poll,
1029 .socketpair = sock_no_socketpair,
1030 .mmap = sock_no_mmap
1031};
1032
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001033static const struct net_proto_family rfcomm_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 .family = PF_BLUETOOTH,
1035 .owner = THIS_MODULE,
1036 .create = rfcomm_sock_create
1037};
1038
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001039int __init rfcomm_init_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 int err;
1042
1043 err = proto_register(&rfcomm_proto, 0);
1044 if (err < 0)
1045 return err;
1046
1047 err = bt_sock_register(BTPROTO_RFCOMM, &rfcomm_sock_family_ops);
1048 if (err < 0)
1049 goto error;
1050
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001051 if (bt_debugfs) {
1052 rfcomm_sock_debugfs = debugfs_create_file("rfcomm", 0444,
1053 bt_debugfs, NULL, &rfcomm_sock_debugfs_fops);
1054 if (!rfcomm_sock_debugfs)
1055 BT_ERR("Failed to create RFCOMM debug file");
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 BT_INFO("RFCOMM socket layer initialized");
1059
1060 return 0;
1061
1062error:
1063 BT_ERR("RFCOMM socket layer registration failed");
1064 proto_unregister(&rfcomm_proto);
1065 return err;
1066}
1067
Gustavo F. Padovan2f8362a2010-07-24 02:04:45 -03001068void __exit rfcomm_cleanup_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001070 debugfs_remove(rfcomm_sock_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 if (bt_sock_unregister(BTPROTO_RFCOMM) < 0)
1073 BT_ERR("RFCOMM socket layer unregistration failed");
1074
1075 proto_unregister(&rfcomm_proto);
1076}