blob: c024e715512fdff242616c4ac403fa8315b13289 [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 ---- */
Andrzej Kaczmarekb1765e72014-02-20 16:42:01 +0100108static struct sock *__rfcomm_get_listen_sock_by_addr(u8 channel, bdaddr_t *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
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) {
Andrzej Kaczmarekb1765e72014-02-20 16:42:01 +0100113 if (rfcomm_pi(sk)->channel != channel)
114 continue;
115
116 if (bacmp(&rfcomm_pi(sk)->src, src))
117 continue;
118
119 if (sk->sk_state == BT_BOUND || sk->sk_state == BT_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 break;
121 }
122
Sasha Levinb67bfe02013-02-27 17:06:00 -0800123 return sk ? sk : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126/* Find socket with channel and source bdaddr.
127 * Returns closest match.
128 */
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000129static struct sock *rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct sock *sk = NULL, *sk1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000133 read_lock(&rfcomm_sk_list.lock);
134
Sasha Levinb67bfe02013-02-27 17:06:00 -0800135 sk_for_each(sk, &rfcomm_sk_list.head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (state && sk->sk_state != state)
137 continue;
138
139 if (rfcomm_pi(sk)->channel == channel) {
140 /* Exact match. */
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700141 if (!bacmp(&rfcomm_pi(sk)->src, src))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 break;
143
144 /* Closest match */
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700145 if (!bacmp(&rfcomm_pi(sk)->src, BDADDR_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 sk1 = sk;
147 }
148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 read_unlock(&rfcomm_sk_list.lock);
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000151
Sasha Levinb67bfe02013-02-27 17:06:00 -0800152 return sk ? sk : sk1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155static void rfcomm_sock_destruct(struct sock *sk)
156{
157 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
158
159 BT_DBG("sk %p dlc %p", sk, d);
160
161 skb_queue_purge(&sk->sk_receive_queue);
162 skb_queue_purge(&sk->sk_write_queue);
163
164 rfcomm_dlc_lock(d);
165 rfcomm_pi(sk)->dlc = NULL;
166
167 /* Detach DLC if it's owned by this socket */
168 if (d->owner == sk)
169 d->owner = NULL;
170 rfcomm_dlc_unlock(d);
171
172 rfcomm_dlc_put(d);
173}
174
175static void rfcomm_sock_cleanup_listen(struct sock *parent)
176{
177 struct sock *sk;
178
179 BT_DBG("parent %p", parent);
180
181 /* Close not yet accepted dlcs */
182 while ((sk = bt_accept_dequeue(parent, NULL))) {
183 rfcomm_sock_close(sk);
184 rfcomm_sock_kill(sk);
185 }
186
187 parent->sk_state = BT_CLOSED;
188 sock_set_flag(parent, SOCK_ZAPPED);
189}
190
191/* Kill socket (only if zapped and orphan)
192 * Must be called on unlocked socket.
193 */
194static void rfcomm_sock_kill(struct sock *sk)
195{
196 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
197 return;
198
199 BT_DBG("sk %p state %d refcnt %d", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
200
201 /* Kill poor orphan */
202 bt_sock_unlink(&rfcomm_sk_list, sk);
203 sock_set_flag(sk, SOCK_DEAD);
204 sock_put(sk);
205}
206
207static void __rfcomm_sock_close(struct sock *sk)
208{
209 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
210
211 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
212
213 switch (sk->sk_state) {
214 case BT_LISTEN:
215 rfcomm_sock_cleanup_listen(sk);
216 break;
217
218 case BT_CONNECT:
219 case BT_CONNECT2:
220 case BT_CONFIG:
221 case BT_CONNECTED:
222 rfcomm_dlc_close(d, 0);
223
224 default:
225 sock_set_flag(sk, SOCK_ZAPPED);
226 break;
227 }
228}
229
230/* Close socket.
231 * Must be called on unlocked socket.
232 */
233static void rfcomm_sock_close(struct sock *sk)
234{
235 lock_sock(sk);
236 __rfcomm_sock_close(sk);
237 release_sock(sk);
238}
239
240static void rfcomm_sock_init(struct sock *sk, struct sock *parent)
241{
242 struct rfcomm_pinfo *pi = rfcomm_pi(sk);
243
244 BT_DBG("sk %p", sk);
245
246 if (parent) {
247 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300248 pi->dlc->defer_setup = test_bit(BT_SK_DEFER_SETUP,
249 &bt_sk(parent)->flags);
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100250
251 pi->sec_level = rfcomm_pi(parent)->sec_level;
252 pi->role_switch = rfcomm_pi(parent)->role_switch;
Paul Moore6230c9b2011-10-07 09:40:59 +0000253
254 security_sk_clone(parent, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 } else {
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100256 pi->dlc->defer_setup = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100257
258 pi->sec_level = BT_SECURITY_LOW;
259 pi->role_switch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100262 pi->dlc->sec_level = pi->sec_level;
263 pi->dlc->role_switch = pi->role_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266static struct proto rfcomm_proto = {
267 .name = "RFCOMM",
268 .owner = THIS_MODULE,
269 .obj_size = sizeof(struct rfcomm_pinfo)
270};
271
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700272static struct sock *rfcomm_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct rfcomm_dlc *d;
275 struct sock *sk;
276
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700277 sk = sk_alloc(net, PF_BLUETOOTH, prio, &rfcomm_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!sk)
279 return NULL;
280
281 sock_init_data(sock, sk);
282 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
283
284 d = rfcomm_dlc_alloc(prio);
285 if (!d) {
286 sk_free(sk);
287 return NULL;
288 }
289
290 d->data_ready = rfcomm_sk_data_ready;
291 d->state_change = rfcomm_sk_state_change;
292
293 rfcomm_pi(sk)->dlc = d;
294 d->owner = sk;
295
296 sk->sk_destruct = rfcomm_sock_destruct;
297 sk->sk_sndtimeo = RFCOMM_CONN_TIMEOUT;
298
Marcel Holtmann77db1982008-07-14 20:13:45 +0200299 sk->sk_sndbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
300 sk->sk_rcvbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 sock_reset_flag(sk, SOCK_ZAPPED);
303
304 sk->sk_protocol = proto;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200305 sk->sk_state = BT_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 bt_sock_link(&rfcomm_sk_list, sk);
308
309 BT_DBG("sk %p", sk);
310 return sk;
311}
312
Eric Paris3f378b62009-11-05 22:18:14 -0800313static int rfcomm_sock_create(struct net *net, struct socket *sock,
314 int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct sock *sk;
317
318 BT_DBG("sock %p", sock);
319
320 sock->state = SS_UNCONNECTED;
321
322 if (sock->type != SOCK_STREAM && sock->type != SOCK_RAW)
323 return -ESOCKTNOSUPPORT;
324
325 sock->ops = &rfcomm_sock_ops;
326
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700327 sk = rfcomm_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200328 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return -ENOMEM;
330
331 rfcomm_sock_init(sk, NULL);
332 return 0;
333}
334
335static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
336{
337 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
338 struct sock *sk = sock->sk;
Andrzej Kaczmarekb1765e72014-02-20 16:42:01 +0100339 int chan = sa->rc_channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int err = 0;
341
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300342 BT_DBG("sk %p %pMR", sk, &sa->rc_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (!addr || addr->sa_family != AF_BLUETOOTH)
345 return -EINVAL;
346
347 lock_sock(sk);
348
349 if (sk->sk_state != BT_OPEN) {
350 err = -EBADFD;
351 goto done;
352 }
353
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200354 if (sk->sk_type != SOCK_STREAM) {
355 err = -EINVAL;
356 goto done;
357 }
358
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200359 write_lock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Andrzej Kaczmarekb1765e72014-02-20 16:42:01 +0100361 if (chan && __rfcomm_get_listen_sock_by_addr(chan, &sa->rc_bdaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 err = -EADDRINUSE;
363 } else {
364 /* Save source address */
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700365 bacpy(&rfcomm_pi(sk)->src, &sa->rc_bdaddr);
Andrzej Kaczmarekb1765e72014-02-20 16:42:01 +0100366 rfcomm_pi(sk)->channel = chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 sk->sk_state = BT_BOUND;
368 }
369
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200370 write_unlock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372done:
373 release_sock(sk);
374 return err;
375}
376
377static int rfcomm_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
378{
379 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
380 struct sock *sk = sock->sk;
381 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
382 int err = 0;
383
384 BT_DBG("sk %p", sk);
385
Changli Gao6503d962010-03-31 22:58:26 +0000386 if (alen < sizeof(struct sockaddr_rc) ||
387 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return -EINVAL;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 lock_sock(sk);
391
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200392 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
393 err = -EBADFD;
394 goto done;
395 }
396
397 if (sk->sk_type != SOCK_STREAM) {
398 err = -EINVAL;
399 goto done;
400 }
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 sk->sk_state = BT_CONNECT;
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700403 bacpy(&rfcomm_pi(sk)->dst, &sa->rc_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 rfcomm_pi(sk)->channel = sa->rc_channel;
405
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100406 d->sec_level = rfcomm_pi(sk)->sec_level;
407 d->role_switch = rfcomm_pi(sk)->role_switch;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200408
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700409 err = rfcomm_dlc_open(d, &rfcomm_pi(sk)->src, &sa->rc_bdaddr,
410 sa->rc_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!err)
412 err = bt_sock_wait_state(sk, BT_CONNECTED,
413 sock_sndtimeo(sk, flags & O_NONBLOCK));
414
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200415done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 release_sock(sk);
417 return err;
418}
419
420static int rfcomm_sock_listen(struct socket *sock, int backlog)
421{
422 struct sock *sk = sock->sk;
423 int err = 0;
424
425 BT_DBG("sk %p backlog %d", sk, backlog);
426
427 lock_sock(sk);
428
429 if (sk->sk_state != BT_BOUND) {
430 err = -EBADFD;
431 goto done;
432 }
433
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200434 if (sk->sk_type != SOCK_STREAM) {
435 err = -EINVAL;
436 goto done;
437 }
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (!rfcomm_pi(sk)->channel) {
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700440 bdaddr_t *src = &rfcomm_pi(sk)->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 u8 channel;
442
443 err = -EINVAL;
444
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200445 write_lock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 for (channel = 1; channel < 31; channel++)
Andrzej Kaczmarekb1765e72014-02-20 16:42:01 +0100448 if (!__rfcomm_get_listen_sock_by_addr(channel, src)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 rfcomm_pi(sk)->channel = channel;
450 err = 0;
451 break;
452 }
453
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200454 write_unlock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (err < 0)
457 goto done;
458 }
459
460 sk->sk_max_ack_backlog = backlog;
461 sk->sk_ack_backlog = 0;
462 sk->sk_state = BT_LISTEN;
463
464done:
465 release_sock(sk);
466 return err;
467}
468
469static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int flags)
470{
471 DECLARE_WAITQUEUE(wait, current);
472 struct sock *sk = sock->sk, *nsk;
473 long timeo;
474 int err = 0;
475
Gustavo Padovandc2a0e22012-11-20 23:25:54 -0200476 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200478 if (sk->sk_type != SOCK_STREAM) {
479 err = -EINVAL;
480 goto done;
481 }
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
484
485 BT_DBG("sk %p timeo %ld", sk, timeo);
486
487 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000488 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurley950e2d52011-07-24 00:10:41 -0400489 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (sk->sk_state != BT_LISTEN) {
493 err = -EBADFD;
494 break;
495 }
496
Peter Hurley950e2d52011-07-24 00:10:41 -0400497 nsk = bt_accept_dequeue(sk, newsock);
498 if (nsk)
499 break;
500
501 if (!timeo) {
502 err = -EAGAIN;
503 break;
504 }
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (signal_pending(current)) {
507 err = sock_intr_errno(timeo);
508 break;
509 }
Peter Hurley950e2d52011-07-24 00:10:41 -0400510
511 release_sock(sk);
512 timeo = schedule_timeout(timeo);
Gustavo Padovandc2a0e22012-11-20 23:25:54 -0200513 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Peter Hurley950e2d52011-07-24 00:10:41 -0400515 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000516 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (err)
519 goto done;
520
521 newsock->state = SS_CONNECTED;
522
523 BT_DBG("new socket %p", nsk);
524
525done:
526 release_sock(sk);
527 return err;
528}
529
530static int rfcomm_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
531{
532 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
533 struct sock *sk = sock->sk;
534
535 BT_DBG("sock %p, sk %p", sock, sk);
536
Johan Hedberg35364c92014-01-28 15:16:50 -0800537 if (peer && sk->sk_state != BT_CONNECTED)
538 return -ENOTCONN;
539
Mathias Krause9344a972012-08-15 11:31:50 +0000540 memset(sa, 0, sizeof(*sa));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 sa->rc_family = AF_BLUETOOTH;
542 sa->rc_channel = rfcomm_pi(sk)->channel;
543 if (peer)
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700544 bacpy(&sa->rc_bdaddr, &rfcomm_pi(sk)->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 else
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700546 bacpy(&sa->rc_bdaddr, &rfcomm_pi(sk)->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 *len = sizeof(struct sockaddr_rc);
549 return 0;
550}
551
552static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
553 struct msghdr *msg, size_t len)
554{
555 struct sock *sk = sock->sk;
556 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
557 struct sk_buff *skb;
Johan Hedberge793dcf2013-09-16 13:05:19 +0300558 int sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100560 if (test_bit(RFCOMM_DEFER_SETUP, &d->flags))
561 return -ENOTCONN;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (msg->msg_flags & MSG_OOB)
564 return -EOPNOTSUPP;
565
566 if (sk->sk_shutdown & SEND_SHUTDOWN)
567 return -EPIPE;
568
569 BT_DBG("sock %p, sk %p", sock, sk);
570
571 lock_sock(sk);
572
Johan Hedberge793dcf2013-09-16 13:05:19 +0300573 sent = bt_sock_wait_ready(sk, msg->msg_flags);
574 if (sent)
575 goto done;
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 while (len) {
578 size_t size = min_t(size_t, len, d->mtu);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100579 int err;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 skb = sock_alloc_send_skb(sk, size + RFCOMM_SKB_RESERVE,
582 msg->msg_flags & MSG_DONTWAIT, &err);
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100583 if (!skb) {
584 if (sent == 0)
585 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 break;
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
589
590 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
591 if (err) {
592 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100593 if (sent == 0)
594 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
596 }
597
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200598 skb->priority = sk->sk_priority;
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 err = rfcomm_dlc_send(d, skb);
601 if (err < 0) {
602 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100603 if (sent == 0)
604 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
606 }
607
608 sent += size;
609 len -= size;
610 }
611
Johan Hedberge793dcf2013-09-16 13:05:19 +0300612done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 release_sock(sk);
614
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100615 return sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618static int rfcomm_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
619 struct msghdr *msg, size_t size, int flags)
620{
621 struct sock *sk = sock->sk;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100622 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700623 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100625 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
626 rfcomm_dlc_accept(d);
627 return 0;
628 }
629
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700630 len = bt_sock_stream_recvmsg(iocb, sock, msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 lock_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700633 if (!(flags & MSG_PEEK) && len > 0)
634 atomic_sub(len, &sk->sk_rmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (atomic_read(&sk->sk_rmem_alloc) <= (sk->sk_rcvbuf >> 2))
637 rfcomm_dlc_unthrottle(rfcomm_pi(sk)->dlc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 release_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700639
640 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
David S. Millerb7058842009-09-30 16:12:20 -0700643static int rfcomm_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 struct sock *sk = sock->sk;
646 int err = 0;
647 u32 opt;
648
649 BT_DBG("sk %p", sk);
650
651 lock_sock(sk);
652
653 switch (optname) {
654 case RFCOMM_LM:
655 if (get_user(opt, (u32 __user *) optval)) {
656 err = -EFAULT;
657 break;
658 }
659
Marcel Holtmann2c068e02014-01-15 22:37:41 -0800660 if (opt & RFCOMM_LM_FIPS) {
661 err = -EINVAL;
662 break;
663 }
664
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100665 if (opt & RFCOMM_LM_AUTH)
666 rfcomm_pi(sk)->sec_level = BT_SECURITY_LOW;
667 if (opt & RFCOMM_LM_ENCRYPT)
668 rfcomm_pi(sk)->sec_level = BT_SECURITY_MEDIUM;
669 if (opt & RFCOMM_LM_SECURE)
670 rfcomm_pi(sk)->sec_level = BT_SECURITY_HIGH;
671
672 rfcomm_pi(sk)->role_switch = (opt & RFCOMM_LM_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 break;
674
675 default:
676 err = -ENOPROTOOPT;
677 break;
678 }
679
680 release_sock(sk);
681 return err;
682}
683
David S. Millerb7058842009-09-30 16:12:20 -0700684static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100685{
686 struct sock *sk = sock->sk;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100687 struct bt_security sec;
Stephen Boydd0fad892011-05-12 16:50:09 -0700688 int err = 0;
689 size_t len;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100690 u32 opt;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100691
692 BT_DBG("sk %p", sk);
693
694 if (level == SOL_RFCOMM)
695 return rfcomm_sock_setsockopt_old(sock, optname, optval, optlen);
696
Marcel Holtmann0588d942009-01-16 10:06:13 +0100697 if (level != SOL_BLUETOOTH)
698 return -ENOPROTOOPT;
699
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100700 lock_sock(sk);
701
702 switch (optname) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100703 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +0100704 if (sk->sk_type != SOCK_STREAM) {
705 err = -EINVAL;
706 break;
707 }
708
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100709 sec.level = BT_SECURITY_LOW;
710
711 len = min_t(unsigned int, sizeof(sec), optlen);
712 if (copy_from_user((char *) &sec, optval, len)) {
713 err = -EFAULT;
714 break;
715 }
716
717 if (sec.level > BT_SECURITY_HIGH) {
718 err = -EINVAL;
719 break;
720 }
721
722 rfcomm_pi(sk)->sec_level = sec.level;
723 break;
724
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100725 case BT_DEFER_SETUP:
726 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
727 err = -EINVAL;
728 break;
729 }
730
731 if (get_user(opt, (u32 __user *) optval)) {
732 err = -EFAULT;
733 break;
734 }
735
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300736 if (opt)
737 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
738 else
739 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
740
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100741 break;
742
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100743 default:
744 err = -ENOPROTOOPT;
745 break;
746 }
747
748 release_sock(sk);
749 return err;
750}
751
752static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct sock *sk = sock->sk;
Marcel Holtmann60c7a3c92013-11-02 02:36:31 -0700755 struct sock *l2cap_sk;
756 struct l2cap_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct rfcomm_conninfo cinfo;
758 int len, err = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100759 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 BT_DBG("sk %p", sk);
762
763 if (get_user(len, optlen))
764 return -EFAULT;
765
766 lock_sock(sk);
767
768 switch (optname) {
769 case RFCOMM_LM:
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100770 switch (rfcomm_pi(sk)->sec_level) {
771 case BT_SECURITY_LOW:
772 opt = RFCOMM_LM_AUTH;
773 break;
774 case BT_SECURITY_MEDIUM:
775 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT;
776 break;
777 case BT_SECURITY_HIGH:
778 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT |
Marcel Holtmann2c068e02014-01-15 22:37:41 -0800779 RFCOMM_LM_SECURE;
780 break;
781 case BT_SECURITY_FIPS:
782 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT |
783 RFCOMM_LM_SECURE | RFCOMM_LM_FIPS;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100784 break;
785 default:
786 opt = 0;
787 break;
788 }
789
790 if (rfcomm_pi(sk)->role_switch)
791 opt |= RFCOMM_LM_MASTER;
792
793 if (put_user(opt, (u32 __user *) optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 err = -EFAULT;
Marcel Holtmann2c068e02014-01-15 22:37:41 -0800795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 break;
797
798 case RFCOMM_CONNINFO:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100799 if (sk->sk_state != BT_CONNECTED &&
800 !rfcomm_pi(sk)->dlc->defer_setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 err = -ENOTCONN;
802 break;
803 }
804
Marcel Holtmann60c7a3c92013-11-02 02:36:31 -0700805 l2cap_sk = rfcomm_pi(sk)->dlc->session->sock->sk;
806 conn = l2cap_pi(l2cap_sk)->chan->conn;
807
Filip Palian8d03e972011-05-12 19:32:46 +0200808 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300809 cinfo.hci_handle = conn->hcon->handle;
810 memcpy(cinfo.dev_class, conn->hcon->dev_class, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 len = min_t(unsigned int, len, sizeof(cinfo));
813 if (copy_to_user(optval, (char *) &cinfo, len))
814 err = -EFAULT;
815
816 break;
817
818 default:
819 err = -ENOPROTOOPT;
820 break;
821 }
822
823 release_sock(sk);
824 return err;
825}
826
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100827static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
828{
829 struct sock *sk = sock->sk;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100830 struct bt_security sec;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100831 int len, err = 0;
832
833 BT_DBG("sk %p", sk);
834
835 if (level == SOL_RFCOMM)
836 return rfcomm_sock_getsockopt_old(sock, optname, optval, optlen);
837
Marcel Holtmann0588d942009-01-16 10:06:13 +0100838 if (level != SOL_BLUETOOTH)
839 return -ENOPROTOOPT;
840
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100841 if (get_user(len, optlen))
842 return -EFAULT;
843
844 lock_sock(sk);
845
846 switch (optname) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100847 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +0100848 if (sk->sk_type != SOCK_STREAM) {
849 err = -EINVAL;
850 break;
851 }
852
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100853 sec.level = rfcomm_pi(sk)->sec_level;
Mathias Krause9ad2de42012-08-15 11:31:48 +0000854 sec.key_size = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100855
856 len = min_t(unsigned int, len, sizeof(sec));
857 if (copy_to_user(optval, (char *) &sec, len))
858 err = -EFAULT;
859
860 break;
861
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100862 case BT_DEFER_SETUP:
863 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
864 err = -EINVAL;
865 break;
866 }
867
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300868 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
869 (u32 __user *) optval))
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100870 err = -EFAULT;
871
872 break;
873
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100874 default:
875 err = -ENOPROTOOPT;
876 break;
877 }
878
879 release_sock(sk);
880 return err;
881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static int rfcomm_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
884{
David S. Millere19caae2008-12-09 01:04:27 -0800885 struct sock *sk __maybe_unused = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 int err;
887
David S. Millere19caae2008-12-09 01:04:27 -0800888 BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200890 err = bt_sock_ioctl(sock, cmd, arg);
891
892 if (err == -ENOIOCTLCMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#ifdef CONFIG_BT_RFCOMM_TTY
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200894 lock_sock(sk);
895 err = rfcomm_dev_ioctl(sk, cmd, (void __user *) arg);
896 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897#else
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200898 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#endif
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return err;
903}
904
905static int rfcomm_sock_shutdown(struct socket *sock, int how)
906{
907 struct sock *sk = sock->sk;
908 int err = 0;
909
910 BT_DBG("sock %p, sk %p", sock, sk);
911
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200912 if (!sk)
913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 lock_sock(sk);
916 if (!sk->sk_shutdown) {
917 sk->sk_shutdown = SHUTDOWN_MASK;
918 __rfcomm_sock_close(sk);
919
920 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
921 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
922 }
923 release_sock(sk);
924 return err;
925}
926
927static int rfcomm_sock_release(struct socket *sock)
928{
929 struct sock *sk = sock->sk;
930 int err;
931
932 BT_DBG("sock %p, sk %p", sock, sk);
933
934 if (!sk)
935 return 0;
936
937 err = rfcomm_sock_shutdown(sock, 2);
938
939 sock_orphan(sk);
940 rfcomm_sock_kill(sk);
941 return err;
942}
943
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900944/* ---- RFCOMM core layer callbacks ----
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 *
946 * called under rfcomm_lock()
947 */
948int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d)
949{
950 struct sock *sk, *parent;
951 bdaddr_t src, dst;
952 int result = 0;
953
954 BT_DBG("session %p channel %d", s, channel);
955
956 rfcomm_session_getaddr(s, &src, &dst);
957
958 /* Check if we have socket listening on channel */
959 parent = rfcomm_get_sock_by_channel(BT_LISTEN, channel, &src);
960 if (!parent)
961 return 0;
962
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000963 bh_lock_sock(parent);
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* Check for backlog size */
966 if (sk_acceptq_is_full(parent)) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900967 BT_DBG("backlog full %d", parent->sk_ack_backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 goto done;
969 }
970
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900971 sk = rfcomm_sock_alloc(sock_net(parent), NULL, BTPROTO_RFCOMM, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!sk)
973 goto done;
974
Octavian Purdilab5a30dd2012-01-22 00:28:34 +0200975 bt_sock_reclassify_lock(sk, BTPROTO_RFCOMM);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 rfcomm_sock_init(sk, parent);
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700978 bacpy(&rfcomm_pi(sk)->src, &src);
979 bacpy(&rfcomm_pi(sk)->dst, &dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 rfcomm_pi(sk)->channel = channel;
981
982 sk->sk_state = BT_CONFIG;
983 bt_accept_enqueue(parent, sk);
984
985 /* Accept connection and return socket DLC */
986 *d = rfcomm_pi(sk)->dlc;
987 result = 1;
988
989done:
990 bh_unlock_sock(parent);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100991
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300992 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100993 parent->sk_state_change(parent);
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return result;
996}
997
Marcel Holtmannaef7d972010-03-21 05:27:45 +0100998static int rfcomm_sock_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -02001002 read_lock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Sasha Levinb67bfe02013-02-27 17:06:00 -08001004 sk_for_each(sk, &rfcomm_sk_list.head) {
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03001005 seq_printf(f, "%pMR %pMR %d %d\n",
Marcel Holtmann94a86df2013-10-13 10:34:02 -07001006 &rfcomm_pi(sk)->src, &rfcomm_pi(sk)->dst,
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03001007 sk->sk_state, rfcomm_pi(sk)->channel);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -02001010 read_unlock(&rfcomm_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001011
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001015static int rfcomm_sock_debugfs_open(struct inode *inode, struct file *file)
1016{
1017 return single_open(file, rfcomm_sock_debugfs_show, inode->i_private);
1018}
1019
1020static const struct file_operations rfcomm_sock_debugfs_fops = {
1021 .open = rfcomm_sock_debugfs_open,
1022 .read = seq_read,
1023 .llseek = seq_lseek,
1024 .release = single_release,
1025};
1026
1027static struct dentry *rfcomm_sock_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001029static const struct proto_ops rfcomm_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 .family = PF_BLUETOOTH,
1031 .owner = THIS_MODULE,
1032 .release = rfcomm_sock_release,
1033 .bind = rfcomm_sock_bind,
1034 .connect = rfcomm_sock_connect,
1035 .listen = rfcomm_sock_listen,
1036 .accept = rfcomm_sock_accept,
1037 .getname = rfcomm_sock_getname,
1038 .sendmsg = rfcomm_sock_sendmsg,
1039 .recvmsg = rfcomm_sock_recvmsg,
1040 .shutdown = rfcomm_sock_shutdown,
1041 .setsockopt = rfcomm_sock_setsockopt,
1042 .getsockopt = rfcomm_sock_getsockopt,
1043 .ioctl = rfcomm_sock_ioctl,
1044 .poll = bt_sock_poll,
1045 .socketpair = sock_no_socketpair,
1046 .mmap = sock_no_mmap
1047};
1048
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001049static const struct net_proto_family rfcomm_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 .family = PF_BLUETOOTH,
1051 .owner = THIS_MODULE,
1052 .create = rfcomm_sock_create
1053};
1054
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001055int __init rfcomm_init_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 int err;
1058
1059 err = proto_register(&rfcomm_proto, 0);
1060 if (err < 0)
1061 return err;
1062
1063 err = bt_sock_register(BTPROTO_RFCOMM, &rfcomm_sock_family_ops);
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001064 if (err < 0) {
1065 BT_ERR("RFCOMM socket layer registration failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 goto error;
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001067 }
1068
Al Virob0316612013-04-04 19:14:33 -04001069 err = bt_procfs_init(&init_net, "rfcomm", &rfcomm_sk_list, NULL);
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001070 if (err < 0) {
1071 BT_ERR("Failed to create RFCOMM proc file");
1072 bt_sock_unregister(BTPROTO_RFCOMM);
1073 goto error;
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 BT_INFO("RFCOMM socket layer initialized");
1077
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07001078 if (IS_ERR_OR_NULL(bt_debugfs))
1079 return 0;
1080
1081 rfcomm_sock_debugfs = debugfs_create_file("rfcomm", 0444,
1082 bt_debugfs, NULL,
1083 &rfcomm_sock_debugfs_fops);
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return 0;
1086
1087error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 proto_unregister(&rfcomm_proto);
1089 return err;
1090}
1091
Gustavo F. Padovan2f8362a2010-07-24 02:04:45 -03001092void __exit rfcomm_cleanup_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001094 bt_procfs_cleanup(&init_net, "rfcomm");
1095
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001096 debugfs_remove(rfcomm_sock_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
David Herrmann5e9d7f82013-02-24 19:36:51 +01001098 bt_sock_unregister(BTPROTO_RFCOMM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 proto_unregister(&rfcomm_proto);
1101}