blob: 3c2d3e4aa2f58a271bea6632451edd21b92668a9 [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)
Marcel Holtmann94a86df2013-10-13 10:34:02 -070090 rfcomm_session_getaddr(d->session,
91 &rfcomm_pi(sk)->src, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 sk->sk_state_change(sk);
93 }
94
95 bh_unlock_sock(sk);
Gustavo F. Padovanfad003b2010-08-14 00:48:07 -030096 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 if (parent && sock_flag(sk, SOCK_ZAPPED)) {
99 /* We have to drop DLC lock here, otherwise
100 * rfcomm_sock_destruct() will dead lock. */
101 rfcomm_dlc_unlock(d);
102 rfcomm_sock_kill(sk);
103 rfcomm_dlc_lock(d);
104 }
105}
106
107/* ---- Socket functions ---- */
108static struct sock *__rfcomm_get_sock_by_addr(u8 channel, bdaddr_t *src)
109{
110 struct sock *sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Sasha Levinb67bfe02013-02-27 17:06:00 -0800112 sk_for_each(sk, &rfcomm_sk_list.head) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900113 if (rfcomm_pi(sk)->channel == channel &&
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700114 !bacmp(&rfcomm_pi(sk)->src, src))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 break;
116 }
117
Sasha Levinb67bfe02013-02-27 17:06:00 -0800118 return sk ? sk : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000128 read_lock(&rfcomm_sk_list.lock);
129
Sasha Levinb67bfe02013-02-27 17:06:00 -0800130 sk_for_each(sk, &rfcomm_sk_list.head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (state && sk->sk_state != state)
132 continue;
133
134 if (rfcomm_pi(sk)->channel == channel) {
135 /* Exact match. */
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700136 if (!bacmp(&rfcomm_pi(sk)->src, src))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 break;
138
139 /* Closest match */
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700140 if (!bacmp(&rfcomm_pi(sk)->src, BDADDR_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 sk1 = sk;
142 }
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 read_unlock(&rfcomm_sk_list.lock);
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000146
Sasha Levinb67bfe02013-02-27 17:06:00 -0800147 return sk ? sk : sk1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150static void rfcomm_sock_destruct(struct sock *sk)
151{
152 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
153
154 BT_DBG("sk %p dlc %p", sk, d);
155
156 skb_queue_purge(&sk->sk_receive_queue);
157 skb_queue_purge(&sk->sk_write_queue);
158
159 rfcomm_dlc_lock(d);
160 rfcomm_pi(sk)->dlc = NULL;
161
162 /* Detach DLC if it's owned by this socket */
163 if (d->owner == sk)
164 d->owner = NULL;
165 rfcomm_dlc_unlock(d);
166
167 rfcomm_dlc_put(d);
168}
169
170static void rfcomm_sock_cleanup_listen(struct sock *parent)
171{
172 struct sock *sk;
173
174 BT_DBG("parent %p", parent);
175
176 /* Close not yet accepted dlcs */
177 while ((sk = bt_accept_dequeue(parent, NULL))) {
178 rfcomm_sock_close(sk);
179 rfcomm_sock_kill(sk);
180 }
181
182 parent->sk_state = BT_CLOSED;
183 sock_set_flag(parent, SOCK_ZAPPED);
184}
185
186/* Kill socket (only if zapped and orphan)
187 * Must be called on unlocked socket.
188 */
189static void rfcomm_sock_kill(struct sock *sk)
190{
191 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
192 return;
193
194 BT_DBG("sk %p state %d refcnt %d", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
195
196 /* Kill poor orphan */
197 bt_sock_unlink(&rfcomm_sk_list, sk);
198 sock_set_flag(sk, SOCK_DEAD);
199 sock_put(sk);
200}
201
202static void __rfcomm_sock_close(struct sock *sk)
203{
204 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
205
206 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
207
208 switch (sk->sk_state) {
209 case BT_LISTEN:
210 rfcomm_sock_cleanup_listen(sk);
211 break;
212
213 case BT_CONNECT:
214 case BT_CONNECT2:
215 case BT_CONFIG:
216 case BT_CONNECTED:
217 rfcomm_dlc_close(d, 0);
218
219 default:
220 sock_set_flag(sk, SOCK_ZAPPED);
221 break;
222 }
223}
224
225/* Close socket.
226 * Must be called on unlocked socket.
227 */
228static void rfcomm_sock_close(struct sock *sk)
229{
230 lock_sock(sk);
231 __rfcomm_sock_close(sk);
232 release_sock(sk);
233}
234
235static void rfcomm_sock_init(struct sock *sk, struct sock *parent)
236{
237 struct rfcomm_pinfo *pi = rfcomm_pi(sk);
238
239 BT_DBG("sk %p", sk);
240
241 if (parent) {
242 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300243 pi->dlc->defer_setup = test_bit(BT_SK_DEFER_SETUP,
244 &bt_sk(parent)->flags);
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100245
246 pi->sec_level = rfcomm_pi(parent)->sec_level;
247 pi->role_switch = rfcomm_pi(parent)->role_switch;
Paul Moore6230c9b2011-10-07 09:40:59 +0000248
249 security_sk_clone(parent, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 } else {
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100251 pi->dlc->defer_setup = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100252
253 pi->sec_level = BT_SECURITY_LOW;
254 pi->role_switch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100257 pi->dlc->sec_level = pi->sec_level;
258 pi->dlc->role_switch = pi->role_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261static struct proto rfcomm_proto = {
262 .name = "RFCOMM",
263 .owner = THIS_MODULE,
264 .obj_size = sizeof(struct rfcomm_pinfo)
265};
266
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700267static struct sock *rfcomm_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct rfcomm_dlc *d;
270 struct sock *sk;
271
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700272 sk = sk_alloc(net, PF_BLUETOOTH, prio, &rfcomm_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!sk)
274 return NULL;
275
276 sock_init_data(sock, sk);
277 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
278
279 d = rfcomm_dlc_alloc(prio);
280 if (!d) {
281 sk_free(sk);
282 return NULL;
283 }
284
285 d->data_ready = rfcomm_sk_data_ready;
286 d->state_change = rfcomm_sk_state_change;
287
288 rfcomm_pi(sk)->dlc = d;
289 d->owner = sk;
290
291 sk->sk_destruct = rfcomm_sock_destruct;
292 sk->sk_sndtimeo = RFCOMM_CONN_TIMEOUT;
293
Marcel Holtmann77db1982008-07-14 20:13:45 +0200294 sk->sk_sndbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
295 sk->sk_rcvbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 sock_reset_flag(sk, SOCK_ZAPPED);
298
299 sk->sk_protocol = proto;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200300 sk->sk_state = BT_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 bt_sock_link(&rfcomm_sk_list, sk);
303
304 BT_DBG("sk %p", sk);
305 return sk;
306}
307
Eric Paris3f378b62009-11-05 22:18:14 -0800308static int rfcomm_sock_create(struct net *net, struct socket *sock,
309 int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct sock *sk;
312
313 BT_DBG("sock %p", sock);
314
315 sock->state = SS_UNCONNECTED;
316
317 if (sock->type != SOCK_STREAM && sock->type != SOCK_RAW)
318 return -ESOCKTNOSUPPORT;
319
320 sock->ops = &rfcomm_sock_ops;
321
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700322 sk = rfcomm_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200323 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return -ENOMEM;
325
326 rfcomm_sock_init(sk, NULL);
327 return 0;
328}
329
330static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
331{
332 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
333 struct sock *sk = sock->sk;
334 int err = 0;
335
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300336 BT_DBG("sk %p %pMR", sk, &sa->rc_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (!addr || addr->sa_family != AF_BLUETOOTH)
339 return -EINVAL;
340
341 lock_sock(sk);
342
343 if (sk->sk_state != BT_OPEN) {
344 err = -EBADFD;
345 goto done;
346 }
347
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200348 if (sk->sk_type != SOCK_STREAM) {
349 err = -EINVAL;
350 goto done;
351 }
352
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200353 write_lock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (sa->rc_channel && __rfcomm_get_sock_by_addr(sa->rc_channel, &sa->rc_bdaddr)) {
356 err = -EADDRINUSE;
357 } else {
358 /* Save source address */
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700359 bacpy(&rfcomm_pi(sk)->src, &sa->rc_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 rfcomm_pi(sk)->channel = sa->rc_channel;
361 sk->sk_state = BT_BOUND;
362 }
363
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200364 write_unlock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366done:
367 release_sock(sk);
368 return err;
369}
370
371static int rfcomm_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
372{
373 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
374 struct sock *sk = sock->sk;
375 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
376 int err = 0;
377
378 BT_DBG("sk %p", sk);
379
Changli Gao6503d962010-03-31 22:58:26 +0000380 if (alen < sizeof(struct sockaddr_rc) ||
381 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -EINVAL;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 lock_sock(sk);
385
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200386 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
387 err = -EBADFD;
388 goto done;
389 }
390
391 if (sk->sk_type != SOCK_STREAM) {
392 err = -EINVAL;
393 goto done;
394 }
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 sk->sk_state = BT_CONNECT;
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700397 bacpy(&rfcomm_pi(sk)->dst, &sa->rc_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 rfcomm_pi(sk)->channel = sa->rc_channel;
399
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100400 d->sec_level = rfcomm_pi(sk)->sec_level;
401 d->role_switch = rfcomm_pi(sk)->role_switch;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200402
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700403 err = rfcomm_dlc_open(d, &rfcomm_pi(sk)->src, &sa->rc_bdaddr,
404 sa->rc_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 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) {
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700434 bdaddr_t *src = &rfcomm_pi(sk)->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 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
Gustavo Padovandc2a0e22012-11-20 23:25:54 -0200470 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
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);
Gustavo Padovandc2a0e22012-11-20 23:25:54 -0200507 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
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
Mathias Krause9344a972012-08-15 11:31:50 +0000531 memset(sa, 0, sizeof(*sa));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 sa->rc_family = AF_BLUETOOTH;
533 sa->rc_channel = rfcomm_pi(sk)->channel;
534 if (peer)
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700535 bacpy(&sa->rc_bdaddr, &rfcomm_pi(sk)->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 else
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700537 bacpy(&sa->rc_bdaddr, &rfcomm_pi(sk)->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 *len = sizeof(struct sockaddr_rc);
540 return 0;
541}
542
543static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
544 struct msghdr *msg, size_t len)
545{
546 struct sock *sk = sock->sk;
547 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
548 struct sk_buff *skb;
Johan Hedberge793dcf2013-09-16 13:05:19 +0300549 int sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100551 if (test_bit(RFCOMM_DEFER_SETUP, &d->flags))
552 return -ENOTCONN;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (msg->msg_flags & MSG_OOB)
555 return -EOPNOTSUPP;
556
557 if (sk->sk_shutdown & SEND_SHUTDOWN)
558 return -EPIPE;
559
560 BT_DBG("sock %p, sk %p", sock, sk);
561
562 lock_sock(sk);
563
Johan Hedberge793dcf2013-09-16 13:05:19 +0300564 sent = bt_sock_wait_ready(sk, msg->msg_flags);
565 if (sent)
566 goto done;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 while (len) {
569 size_t size = min_t(size_t, len, d->mtu);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100570 int err;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 skb = sock_alloc_send_skb(sk, size + RFCOMM_SKB_RESERVE,
573 msg->msg_flags & MSG_DONTWAIT, &err);
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100574 if (!skb) {
575 if (sent == 0)
576 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 break;
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
580
581 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
582 if (err) {
583 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100584 if (sent == 0)
585 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 break;
587 }
588
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200589 skb->priority = sk->sk_priority;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 err = rfcomm_dlc_send(d, skb);
592 if (err < 0) {
593 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100594 if (sent == 0)
595 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 break;
597 }
598
599 sent += size;
600 len -= size;
601 }
602
Johan Hedberge793dcf2013-09-16 13:05:19 +0300603done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 release_sock(sk);
605
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100606 return sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609static int rfcomm_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
610 struct msghdr *msg, size_t size, int flags)
611{
612 struct sock *sk = sock->sk;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100613 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700614 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100616 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
617 rfcomm_dlc_accept(d);
618 return 0;
619 }
620
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700621 len = bt_sock_stream_recvmsg(iocb, sock, msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 lock_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700624 if (!(flags & MSG_PEEK) && len > 0)
625 atomic_sub(len, &sk->sk_rmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (atomic_read(&sk->sk_rmem_alloc) <= (sk->sk_rcvbuf >> 2))
628 rfcomm_dlc_unthrottle(rfcomm_pi(sk)->dlc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 release_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700630
631 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
David S. Millerb7058842009-09-30 16:12:20 -0700634static int rfcomm_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct sock *sk = sock->sk;
637 int err = 0;
638 u32 opt;
639
640 BT_DBG("sk %p", sk);
641
642 lock_sock(sk);
643
644 switch (optname) {
645 case RFCOMM_LM:
646 if (get_user(opt, (u32 __user *) optval)) {
647 err = -EFAULT;
648 break;
649 }
650
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100651 if (opt & RFCOMM_LM_AUTH)
652 rfcomm_pi(sk)->sec_level = BT_SECURITY_LOW;
653 if (opt & RFCOMM_LM_ENCRYPT)
654 rfcomm_pi(sk)->sec_level = BT_SECURITY_MEDIUM;
655 if (opt & RFCOMM_LM_SECURE)
656 rfcomm_pi(sk)->sec_level = BT_SECURITY_HIGH;
657
658 rfcomm_pi(sk)->role_switch = (opt & RFCOMM_LM_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 break;
660
661 default:
662 err = -ENOPROTOOPT;
663 break;
664 }
665
666 release_sock(sk);
667 return err;
668}
669
David S. Millerb7058842009-09-30 16:12:20 -0700670static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100671{
672 struct sock *sk = sock->sk;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100673 struct bt_security sec;
Stephen Boydd0fad892011-05-12 16:50:09 -0700674 int err = 0;
675 size_t len;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100676 u32 opt;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100677
678 BT_DBG("sk %p", sk);
679
680 if (level == SOL_RFCOMM)
681 return rfcomm_sock_setsockopt_old(sock, optname, optval, optlen);
682
Marcel Holtmann0588d942009-01-16 10:06:13 +0100683 if (level != SOL_BLUETOOTH)
684 return -ENOPROTOOPT;
685
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100686 lock_sock(sk);
687
688 switch (optname) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100689 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +0100690 if (sk->sk_type != SOCK_STREAM) {
691 err = -EINVAL;
692 break;
693 }
694
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100695 sec.level = BT_SECURITY_LOW;
696
697 len = min_t(unsigned int, sizeof(sec), optlen);
698 if (copy_from_user((char *) &sec, optval, len)) {
699 err = -EFAULT;
700 break;
701 }
702
703 if (sec.level > BT_SECURITY_HIGH) {
704 err = -EINVAL;
705 break;
706 }
707
708 rfcomm_pi(sk)->sec_level = sec.level;
709 break;
710
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100711 case BT_DEFER_SETUP:
712 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
713 err = -EINVAL;
714 break;
715 }
716
717 if (get_user(opt, (u32 __user *) optval)) {
718 err = -EFAULT;
719 break;
720 }
721
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300722 if (opt)
723 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
724 else
725 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
726
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100727 break;
728
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100729 default:
730 err = -ENOPROTOOPT;
731 break;
732 }
733
734 release_sock(sk);
735 return err;
736}
737
738static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 struct sock *sk = sock->sk;
Marcel Holtmann60c7a3c92013-11-02 02:36:31 -0700741 struct sock *l2cap_sk;
742 struct l2cap_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct rfcomm_conninfo cinfo;
744 int len, err = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100745 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 BT_DBG("sk %p", sk);
748
749 if (get_user(len, optlen))
750 return -EFAULT;
751
752 lock_sock(sk);
753
754 switch (optname) {
755 case RFCOMM_LM:
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100756 switch (rfcomm_pi(sk)->sec_level) {
757 case BT_SECURITY_LOW:
758 opt = RFCOMM_LM_AUTH;
759 break;
760 case BT_SECURITY_MEDIUM:
761 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT;
762 break;
763 case BT_SECURITY_HIGH:
764 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT |
765 RFCOMM_LM_SECURE;
766 break;
767 default:
768 opt = 0;
769 break;
770 }
771
772 if (rfcomm_pi(sk)->role_switch)
773 opt |= RFCOMM_LM_MASTER;
774
775 if (put_user(opt, (u32 __user *) optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 err = -EFAULT;
777 break;
778
779 case RFCOMM_CONNINFO:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100780 if (sk->sk_state != BT_CONNECTED &&
781 !rfcomm_pi(sk)->dlc->defer_setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 err = -ENOTCONN;
783 break;
784 }
785
Marcel Holtmann60c7a3c92013-11-02 02:36:31 -0700786 l2cap_sk = rfcomm_pi(sk)->dlc->session->sock->sk;
787 conn = l2cap_pi(l2cap_sk)->chan->conn;
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;
Mathias Krause9ad2de42012-08-15 11:31:48 +0000835 sec.key_size = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100836
837 len = min_t(unsigned int, len, sizeof(sec));
838 if (copy_to_user(optval, (char *) &sec, len))
839 err = -EFAULT;
840
841 break;
842
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100843 case BT_DEFER_SETUP:
844 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
845 err = -EINVAL;
846 break;
847 }
848
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300849 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
850 (u32 __user *) optval))
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100851 err = -EFAULT;
852
853 break;
854
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100855 default:
856 err = -ENOPROTOOPT;
857 break;
858 }
859
860 release_sock(sk);
861 return err;
862}
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864static int rfcomm_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
865{
David S. Millere19caae2008-12-09 01:04:27 -0800866 struct sock *sk __maybe_unused = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int err;
868
David S. Millere19caae2008-12-09 01:04:27 -0800869 BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200871 err = bt_sock_ioctl(sock, cmd, arg);
872
873 if (err == -ENOIOCTLCMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874#ifdef CONFIG_BT_RFCOMM_TTY
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200875 lock_sock(sk);
876 err = rfcomm_dev_ioctl(sk, cmd, (void __user *) arg);
877 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878#else
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200879 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880#endif
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return err;
884}
885
886static int rfcomm_sock_shutdown(struct socket *sock, int how)
887{
888 struct sock *sk = sock->sk;
889 int err = 0;
890
891 BT_DBG("sock %p, sk %p", sock, sk);
892
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200893 if (!sk)
894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 lock_sock(sk);
897 if (!sk->sk_shutdown) {
898 sk->sk_shutdown = SHUTDOWN_MASK;
899 __rfcomm_sock_close(sk);
900
901 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
902 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
903 }
904 release_sock(sk);
905 return err;
906}
907
908static int rfcomm_sock_release(struct socket *sock)
909{
910 struct sock *sk = sock->sk;
911 int err;
912
913 BT_DBG("sock %p, sk %p", sock, sk);
914
915 if (!sk)
916 return 0;
917
918 err = rfcomm_sock_shutdown(sock, 2);
919
920 sock_orphan(sk);
921 rfcomm_sock_kill(sk);
922 return err;
923}
924
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900925/* ---- RFCOMM core layer callbacks ----
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
927 * called under rfcomm_lock()
928 */
929int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d)
930{
931 struct sock *sk, *parent;
932 bdaddr_t src, dst;
933 int result = 0;
934
935 BT_DBG("session %p channel %d", s, channel);
936
937 rfcomm_session_getaddr(s, &src, &dst);
938
939 /* Check if we have socket listening on channel */
940 parent = rfcomm_get_sock_by_channel(BT_LISTEN, channel, &src);
941 if (!parent)
942 return 0;
943
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000944 bh_lock_sock(parent);
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /* Check for backlog size */
947 if (sk_acceptq_is_full(parent)) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900948 BT_DBG("backlog full %d", parent->sk_ack_backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 goto done;
950 }
951
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900952 sk = rfcomm_sock_alloc(sock_net(parent), NULL, BTPROTO_RFCOMM, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (!sk)
954 goto done;
955
Octavian Purdilab5a30dd2012-01-22 00:28:34 +0200956 bt_sock_reclassify_lock(sk, BTPROTO_RFCOMM);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 rfcomm_sock_init(sk, parent);
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700959 bacpy(&rfcomm_pi(sk)->src, &src);
960 bacpy(&rfcomm_pi(sk)->dst, &dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 rfcomm_pi(sk)->channel = channel;
962
963 sk->sk_state = BT_CONFIG;
964 bt_accept_enqueue(parent, sk);
965
966 /* Accept connection and return socket DLC */
967 *d = rfcomm_pi(sk)->dlc;
968 result = 1;
969
970done:
971 bh_unlock_sock(parent);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100972
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300973 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100974 parent->sk_state_change(parent);
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return result;
977}
978
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100979static int rfcomm_sock_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200983 read_lock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Sasha Levinb67bfe02013-02-27 17:06:00 -0800985 sk_for_each(sk, &rfcomm_sk_list.head) {
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +0300986 seq_printf(f, "%pMR %pMR %d %d\n",
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700987 &rfcomm_pi(sk)->src, &rfcomm_pi(sk)->dst,
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +0300988 sk->sk_state, rfcomm_pi(sk)->channel);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200991 read_unlock(&rfcomm_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800992
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100993 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100996static int rfcomm_sock_debugfs_open(struct inode *inode, struct file *file)
997{
998 return single_open(file, rfcomm_sock_debugfs_show, inode->i_private);
999}
1000
1001static const struct file_operations rfcomm_sock_debugfs_fops = {
1002 .open = rfcomm_sock_debugfs_open,
1003 .read = seq_read,
1004 .llseek = seq_lseek,
1005 .release = single_release,
1006};
1007
1008static struct dentry *rfcomm_sock_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001010static const struct proto_ops rfcomm_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 .family = PF_BLUETOOTH,
1012 .owner = THIS_MODULE,
1013 .release = rfcomm_sock_release,
1014 .bind = rfcomm_sock_bind,
1015 .connect = rfcomm_sock_connect,
1016 .listen = rfcomm_sock_listen,
1017 .accept = rfcomm_sock_accept,
1018 .getname = rfcomm_sock_getname,
1019 .sendmsg = rfcomm_sock_sendmsg,
1020 .recvmsg = rfcomm_sock_recvmsg,
1021 .shutdown = rfcomm_sock_shutdown,
1022 .setsockopt = rfcomm_sock_setsockopt,
1023 .getsockopt = rfcomm_sock_getsockopt,
1024 .ioctl = rfcomm_sock_ioctl,
1025 .poll = bt_sock_poll,
1026 .socketpair = sock_no_socketpair,
1027 .mmap = sock_no_mmap
1028};
1029
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001030static const struct net_proto_family rfcomm_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 .family = PF_BLUETOOTH,
1032 .owner = THIS_MODULE,
1033 .create = rfcomm_sock_create
1034};
1035
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001036int __init rfcomm_init_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 int err;
1039
1040 err = proto_register(&rfcomm_proto, 0);
1041 if (err < 0)
1042 return err;
1043
1044 err = bt_sock_register(BTPROTO_RFCOMM, &rfcomm_sock_family_ops);
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001045 if (err < 0) {
1046 BT_ERR("RFCOMM socket layer registration failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto error;
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001048 }
1049
Al Virob0316612013-04-04 19:14:33 -04001050 err = bt_procfs_init(&init_net, "rfcomm", &rfcomm_sk_list, NULL);
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001051 if (err < 0) {
1052 BT_ERR("Failed to create RFCOMM proc file");
1053 bt_sock_unregister(BTPROTO_RFCOMM);
1054 goto error;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 BT_INFO("RFCOMM socket layer initialized");
1058
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07001059 if (IS_ERR_OR_NULL(bt_debugfs))
1060 return 0;
1061
1062 rfcomm_sock_debugfs = debugfs_create_file("rfcomm", 0444,
1063 bt_debugfs, NULL,
1064 &rfcomm_sock_debugfs_fops);
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return 0;
1067
1068error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 proto_unregister(&rfcomm_proto);
1070 return err;
1071}
1072
Gustavo F. Padovan2f8362a2010-07-24 02:04:45 -03001073void __exit rfcomm_cleanup_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001075 bt_procfs_cleanup(&init_net, "rfcomm");
1076
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001077 debugfs_remove(rfcomm_sock_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
David Herrmann5e9d7f82013-02-24 19:36:51 +01001079 bt_sock_unregister(BTPROTO_RFCOMM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 proto_unregister(&rfcomm_proto);
1082}