blob: ac3c650cb234f9985ddf0b54924db9000c4586c3 [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010030#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <net/bluetooth/bluetooth.h>
33#include <net/bluetooth/hci_core.h>
34#include <net/bluetooth/l2cap.h>
35#include <net/bluetooth/rfcomm.h>
36
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080037static const struct proto_ops rfcomm_sock_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static struct bt_sock_list rfcomm_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070040 .lock = __RW_LOCK_UNLOCKED(rfcomm_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
43static void rfcomm_sock_close(struct sock *sk);
44static void rfcomm_sock_kill(struct sock *sk);
45
46/* ---- DLC callbacks ----
47 *
48 * called under rfcomm_dlc_lock()
49 */
50static void rfcomm_sk_data_ready(struct rfcomm_dlc *d, struct sk_buff *skb)
51{
52 struct sock *sk = d->owner;
53 if (!sk)
54 return;
55
56 atomic_add(skb->len, &sk->sk_rmem_alloc);
57 skb_queue_tail(&sk->sk_receive_queue, skb);
David S. Miller676d2362014-04-11 16:15:36 -040058 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
61 rfcomm_dlc_throttle(d);
62}
63
64static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
65{
66 struct sock *sk = d->owner, *parent;
Gustavo F. Padovanfad003b2010-08-14 00:48:07 -030067 unsigned long flags;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (!sk)
70 return;
71
72 BT_DBG("dlc %p state %ld err %d", d, d->state, err);
73
Gustavo F. Padovanfad003b2010-08-14 00:48:07 -030074 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 bh_lock_sock(sk);
76
77 if (err)
78 sk->sk_err = err;
79
80 sk->sk_state = d->state;
81
82 parent = bt_sk(sk)->parent;
83 if (parent) {
84 if (d->state == BT_CLOSED) {
85 sock_set_flag(sk, SOCK_ZAPPED);
86 bt_accept_unlink(sk);
87 }
David S. Miller676d2362014-04-11 16:15:36 -040088 parent->sk_data_ready(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 } else {
90 if (d->state == BT_CONNECTED)
Marcel Holtmann94a86df2013-10-13 10:34:02 -070091 rfcomm_session_getaddr(d->session,
92 &rfcomm_pi(sk)->src, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 sk->sk_state_change(sk);
94 }
95
96 bh_unlock_sock(sk);
Gustavo F. Padovanfad003b2010-08-14 00:48:07 -030097 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 if (parent && sock_flag(sk, SOCK_ZAPPED)) {
100 /* We have to drop DLC lock here, otherwise
101 * rfcomm_sock_destruct() will dead lock. */
102 rfcomm_dlc_unlock(d);
103 rfcomm_sock_kill(sk);
104 rfcomm_dlc_lock(d);
105 }
106}
107
108/* ---- Socket functions ---- */
Andrzej Kaczmarekb1765e72014-02-20 16:42:01 +0100109static struct sock *__rfcomm_get_listen_sock_by_addr(u8 channel, bdaddr_t *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct sock *sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Sasha Levinb67bfe02013-02-27 17:06:00 -0800113 sk_for_each(sk, &rfcomm_sk_list.head) {
Andrzej Kaczmarekb1765e72014-02-20 16:42:01 +0100114 if (rfcomm_pi(sk)->channel != channel)
115 continue;
116
117 if (bacmp(&rfcomm_pi(sk)->src, src))
118 continue;
119
120 if (sk->sk_state == BT_BOUND || sk->sk_state == BT_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 break;
122 }
123
Sasha Levinb67bfe02013-02-27 17:06:00 -0800124 return sk ? sk : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127/* Find socket with channel and source bdaddr.
128 * Returns closest match.
129 */
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000130static struct sock *rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct sock *sk = NULL, *sk1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000134 read_lock(&rfcomm_sk_list.lock);
135
Sasha Levinb67bfe02013-02-27 17:06:00 -0800136 sk_for_each(sk, &rfcomm_sk_list.head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (state && sk->sk_state != state)
138 continue;
139
140 if (rfcomm_pi(sk)->channel == channel) {
141 /* Exact match. */
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700142 if (!bacmp(&rfcomm_pi(sk)->src, src))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 break;
144
145 /* Closest match */
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700146 if (!bacmp(&rfcomm_pi(sk)->src, BDADDR_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 sk1 = sk;
148 }
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 read_unlock(&rfcomm_sk_list.lock);
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000152
Sasha Levinb67bfe02013-02-27 17:06:00 -0800153 return sk ? sk : sk1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156static void rfcomm_sock_destruct(struct sock *sk)
157{
158 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
159
160 BT_DBG("sk %p dlc %p", sk, d);
161
162 skb_queue_purge(&sk->sk_receive_queue);
163 skb_queue_purge(&sk->sk_write_queue);
164
165 rfcomm_dlc_lock(d);
166 rfcomm_pi(sk)->dlc = NULL;
167
168 /* Detach DLC if it's owned by this socket */
169 if (d->owner == sk)
170 d->owner = NULL;
171 rfcomm_dlc_unlock(d);
172
173 rfcomm_dlc_put(d);
174}
175
176static void rfcomm_sock_cleanup_listen(struct sock *parent)
177{
178 struct sock *sk;
179
180 BT_DBG("parent %p", parent);
181
182 /* Close not yet accepted dlcs */
183 while ((sk = bt_accept_dequeue(parent, NULL))) {
184 rfcomm_sock_close(sk);
185 rfcomm_sock_kill(sk);
186 }
187
188 parent->sk_state = BT_CLOSED;
189 sock_set_flag(parent, SOCK_ZAPPED);
190}
191
192/* Kill socket (only if zapped and orphan)
193 * Must be called on unlocked socket.
194 */
195static void rfcomm_sock_kill(struct sock *sk)
196{
197 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
198 return;
199
200 BT_DBG("sk %p state %d refcnt %d", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
201
202 /* Kill poor orphan */
203 bt_sock_unlink(&rfcomm_sk_list, sk);
204 sock_set_flag(sk, SOCK_DEAD);
205 sock_put(sk);
206}
207
208static void __rfcomm_sock_close(struct sock *sk)
209{
210 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
211
212 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
213
214 switch (sk->sk_state) {
215 case BT_LISTEN:
216 rfcomm_sock_cleanup_listen(sk);
217 break;
218
219 case BT_CONNECT:
220 case BT_CONNECT2:
221 case BT_CONFIG:
222 case BT_CONNECTED:
223 rfcomm_dlc_close(d, 0);
224
225 default:
226 sock_set_flag(sk, SOCK_ZAPPED);
227 break;
228 }
229}
230
231/* Close socket.
232 * Must be called on unlocked socket.
233 */
234static void rfcomm_sock_close(struct sock *sk)
235{
236 lock_sock(sk);
237 __rfcomm_sock_close(sk);
238 release_sock(sk);
239}
240
241static void rfcomm_sock_init(struct sock *sk, struct sock *parent)
242{
243 struct rfcomm_pinfo *pi = rfcomm_pi(sk);
244
245 BT_DBG("sk %p", sk);
246
247 if (parent) {
248 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300249 pi->dlc->defer_setup = test_bit(BT_SK_DEFER_SETUP,
250 &bt_sk(parent)->flags);
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100251
252 pi->sec_level = rfcomm_pi(parent)->sec_level;
253 pi->role_switch = rfcomm_pi(parent)->role_switch;
Paul Moore6230c9b2011-10-07 09:40:59 +0000254
255 security_sk_clone(parent, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 } else {
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100257 pi->dlc->defer_setup = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100258
259 pi->sec_level = BT_SECURITY_LOW;
260 pi->role_switch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100263 pi->dlc->sec_level = pi->sec_level;
264 pi->dlc->role_switch = pi->role_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267static struct proto rfcomm_proto = {
268 .name = "RFCOMM",
269 .owner = THIS_MODULE,
270 .obj_size = sizeof(struct rfcomm_pinfo)
271};
272
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500273static struct sock *rfcomm_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct rfcomm_dlc *d;
276 struct sock *sk;
277
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500278 sk = sk_alloc(net, PF_BLUETOOTH, prio, &rfcomm_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!sk)
280 return NULL;
281
282 sock_init_data(sock, sk);
283 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
284
285 d = rfcomm_dlc_alloc(prio);
286 if (!d) {
287 sk_free(sk);
288 return NULL;
289 }
290
291 d->data_ready = rfcomm_sk_data_ready;
292 d->state_change = rfcomm_sk_state_change;
293
294 rfcomm_pi(sk)->dlc = d;
295 d->owner = sk;
296
297 sk->sk_destruct = rfcomm_sock_destruct;
298 sk->sk_sndtimeo = RFCOMM_CONN_TIMEOUT;
299
Marcel Holtmann77db1982008-07-14 20:13:45 +0200300 sk->sk_sndbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
301 sk->sk_rcvbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 sock_reset_flag(sk, SOCK_ZAPPED);
304
305 sk->sk_protocol = proto;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200306 sk->sk_state = BT_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 bt_sock_link(&rfcomm_sk_list, sk);
309
310 BT_DBG("sk %p", sk);
311 return sk;
312}
313
Eric Paris3f378b62009-11-05 22:18:14 -0800314static int rfcomm_sock_create(struct net *net, struct socket *sock,
315 int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 struct sock *sk;
318
319 BT_DBG("sock %p", sock);
320
321 sock->state = SS_UNCONNECTED;
322
323 if (sock->type != SOCK_STREAM && sock->type != SOCK_RAW)
324 return -ESOCKTNOSUPPORT;
325
326 sock->ops = &rfcomm_sock_ops;
327
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500328 sk = rfcomm_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200329 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -ENOMEM;
331
332 rfcomm_sock_init(sk, NULL);
333 return 0;
334}
335
336static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
337{
Jaganath Kanakkassery951b6a02015-05-14 12:58:08 +0530338 struct sockaddr_rc sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct sock *sk = sock->sk;
Jaganath Kanakkassery951b6a02015-05-14 12:58:08 +0530340 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (!addr || addr->sa_family != AF_BLUETOOTH)
343 return -EINVAL;
344
Jaganath Kanakkassery951b6a02015-05-14 12:58:08 +0530345 memset(&sa, 0, sizeof(sa));
346 len = min_t(unsigned int, sizeof(sa), addr_len);
347 memcpy(&sa, addr, len);
348
349 BT_DBG("sk %p %pMR", sk, &sa.rc_bdaddr);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 lock_sock(sk);
352
353 if (sk->sk_state != BT_OPEN) {
354 err = -EBADFD;
355 goto done;
356 }
357
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200358 if (sk->sk_type != SOCK_STREAM) {
359 err = -EINVAL;
360 goto done;
361 }
362
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200363 write_lock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Jaganath Kanakkassery951b6a02015-05-14 12:58:08 +0530365 if (sa.rc_channel &&
366 __rfcomm_get_listen_sock_by_addr(sa.rc_channel, &sa.rc_bdaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 err = -EADDRINUSE;
368 } else {
369 /* Save source address */
Jaganath Kanakkassery951b6a02015-05-14 12:58:08 +0530370 bacpy(&rfcomm_pi(sk)->src, &sa.rc_bdaddr);
371 rfcomm_pi(sk)->channel = sa.rc_channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 sk->sk_state = BT_BOUND;
373 }
374
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200375 write_unlock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377done:
378 release_sock(sk);
379 return err;
380}
381
382static int rfcomm_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
383{
384 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
385 struct sock *sk = sock->sk;
386 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
387 int err = 0;
388
389 BT_DBG("sk %p", sk);
390
Changli Gao6503d962010-03-31 22:58:26 +0000391 if (alen < sizeof(struct sockaddr_rc) ||
392 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -EINVAL;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 lock_sock(sk);
396
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200397 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
398 err = -EBADFD;
399 goto done;
400 }
401
402 if (sk->sk_type != SOCK_STREAM) {
403 err = -EINVAL;
404 goto done;
405 }
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 sk->sk_state = BT_CONNECT;
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700408 bacpy(&rfcomm_pi(sk)->dst, &sa->rc_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 rfcomm_pi(sk)->channel = sa->rc_channel;
410
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100411 d->sec_level = rfcomm_pi(sk)->sec_level;
412 d->role_switch = rfcomm_pi(sk)->role_switch;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200413
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700414 err = rfcomm_dlc_open(d, &rfcomm_pi(sk)->src, &sa->rc_bdaddr,
415 sa->rc_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!err)
417 err = bt_sock_wait_state(sk, BT_CONNECTED,
418 sock_sndtimeo(sk, flags & O_NONBLOCK));
419
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200420done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 release_sock(sk);
422 return err;
423}
424
425static int rfcomm_sock_listen(struct socket *sock, int backlog)
426{
427 struct sock *sk = sock->sk;
428 int err = 0;
429
430 BT_DBG("sk %p backlog %d", sk, backlog);
431
432 lock_sock(sk);
433
434 if (sk->sk_state != BT_BOUND) {
435 err = -EBADFD;
436 goto done;
437 }
438
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200439 if (sk->sk_type != SOCK_STREAM) {
440 err = -EINVAL;
441 goto done;
442 }
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (!rfcomm_pi(sk)->channel) {
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700445 bdaddr_t *src = &rfcomm_pi(sk)->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 u8 channel;
447
448 err = -EINVAL;
449
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200450 write_lock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 for (channel = 1; channel < 31; channel++)
Andrzej Kaczmarekb1765e72014-02-20 16:42:01 +0100453 if (!__rfcomm_get_listen_sock_by_addr(channel, src)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 rfcomm_pi(sk)->channel = channel;
455 err = 0;
456 break;
457 }
458
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -0200459 write_unlock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (err < 0)
462 goto done;
463 }
464
465 sk->sk_max_ack_backlog = backlog;
466 sk->sk_ack_backlog = 0;
467 sk->sk_state = BT_LISTEN;
468
469done:
470 release_sock(sk);
471 return err;
472}
473
David Howellscdfbabf2017-03-09 08:09:05 +0000474static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int flags,
475 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Peter Hurleydfb2fae2015-01-23 12:16:53 -0500477 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct sock *sk = sock->sk, *nsk;
479 long timeo;
480 int err = 0;
481
Gustavo Padovandc2a0e22012-11-20 23:25:54 -0200482 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Marcel Holtmann354d28d2005-09-13 01:32:31 +0200484 if (sk->sk_type != SOCK_STREAM) {
485 err = -EINVAL;
486 goto done;
487 }
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
490
491 BT_DBG("sk %p timeo %ld", sk, timeo);
492
493 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000494 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurley950e2d52011-07-24 00:10:41 -0400495 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (sk->sk_state != BT_LISTEN) {
497 err = -EBADFD;
498 break;
499 }
500
Peter Hurley950e2d52011-07-24 00:10:41 -0400501 nsk = bt_accept_dequeue(sk, newsock);
502 if (nsk)
503 break;
504
505 if (!timeo) {
506 err = -EAGAIN;
507 break;
508 }
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (signal_pending(current)) {
511 err = sock_intr_errno(timeo);
512 break;
513 }
Peter Hurley950e2d52011-07-24 00:10:41 -0400514
515 release_sock(sk);
Peter Hurleydfb2fae2015-01-23 12:16:53 -0500516
517 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
518
Gustavo Padovandc2a0e22012-11-20 23:25:54 -0200519 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000521 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if (err)
524 goto done;
525
526 newsock->state = SS_CONNECTED;
527
528 BT_DBG("new socket %p", nsk);
529
530done:
531 release_sock(sk);
532 return err;
533}
534
535static int rfcomm_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
536{
537 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
538 struct sock *sk = sock->sk;
539
540 BT_DBG("sock %p, sk %p", sock, sk);
541
Johan Hedberge8b1ab92014-03-26 15:49:18 +0200542 if (peer && sk->sk_state != BT_CONNECTED &&
543 sk->sk_state != BT_CONNECT && sk->sk_state != BT_CONNECT2)
Johan Hedberg35364c92014-01-28 15:16:50 -0800544 return -ENOTCONN;
545
Mathias Krause9344a972012-08-15 11:31:50 +0000546 memset(sa, 0, sizeof(*sa));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 sa->rc_family = AF_BLUETOOTH;
548 sa->rc_channel = rfcomm_pi(sk)->channel;
549 if (peer)
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700550 bacpy(&sa->rc_bdaddr, &rfcomm_pi(sk)->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 else
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700552 bacpy(&sa->rc_bdaddr, &rfcomm_pi(sk)->src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 *len = sizeof(struct sockaddr_rc);
555 return 0;
556}
557
Ying Xue1b784142015-03-02 15:37:48 +0800558static int rfcomm_sock_sendmsg(struct socket *sock, struct msghdr *msg,
559 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct sock *sk = sock->sk;
562 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
563 struct sk_buff *skb;
Johan Hedberge793dcf2013-09-16 13:05:19 +0300564 int sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100566 if (test_bit(RFCOMM_DEFER_SETUP, &d->flags))
567 return -ENOTCONN;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (msg->msg_flags & MSG_OOB)
570 return -EOPNOTSUPP;
571
572 if (sk->sk_shutdown & SEND_SHUTDOWN)
573 return -EPIPE;
574
575 BT_DBG("sock %p, sk %p", sock, sk);
576
577 lock_sock(sk);
578
Johan Hedberge793dcf2013-09-16 13:05:19 +0300579 sent = bt_sock_wait_ready(sk, msg->msg_flags);
580 if (sent)
581 goto done;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 while (len) {
584 size_t size = min_t(size_t, len, d->mtu);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100585 int err;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 skb = sock_alloc_send_skb(sk, size + RFCOMM_SKB_RESERVE,
588 msg->msg_flags & MSG_DONTWAIT, &err);
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100589 if (!skb) {
590 if (sent == 0)
591 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
Victor Shcherbatyuk91aa35a2009-01-15 21:52:12 +0100593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
595
Al Viro6ce8e9c2014-04-06 21:25:44 -0400596 err = memcpy_from_msg(skb_put(skb, size), msg, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (err) {
598 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100599 if (sent == 0)
600 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 break;
602 }
603
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200604 skb->priority = sk->sk_priority;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 err = rfcomm_dlc_send(d, skb);
607 if (err < 0) {
608 kfree_skb(skb);
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100609 if (sent == 0)
610 sent = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 break;
612 }
613
614 sent += size;
615 len -= size;
616 }
617
Johan Hedberge793dcf2013-09-16 13:05:19 +0300618done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 release_sock(sk);
620
Marcel Holtmann4d6a2182007-01-08 02:16:31 +0100621 return sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Ying Xue1b784142015-03-02 15:37:48 +0800624static int rfcomm_sock_recvmsg(struct socket *sock, struct msghdr *msg,
625 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct sock *sk = sock->sk;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100628 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700629 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100631 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
632 rfcomm_dlc_accept(d);
633 return 0;
634 }
635
Ying Xue1b784142015-03-02 15:37:48 +0800636 len = bt_sock_stream_recvmsg(sock, msg, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 lock_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700639 if (!(flags & MSG_PEEK) && len > 0)
640 atomic_sub(len, &sk->sk_rmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (atomic_read(&sk->sk_rmem_alloc) <= (sk->sk_rcvbuf >> 2))
643 rfcomm_dlc_unthrottle(rfcomm_pi(sk)->dlc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 release_sock(sk);
Mat Martineau3d7d01d2010-09-08 10:05:28 -0700645
646 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
David S. Millerb7058842009-09-30 16:12:20 -0700649static int rfcomm_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 struct sock *sk = sock->sk;
652 int err = 0;
653 u32 opt;
654
655 BT_DBG("sk %p", sk);
656
657 lock_sock(sk);
658
659 switch (optname) {
660 case RFCOMM_LM:
661 if (get_user(opt, (u32 __user *) optval)) {
662 err = -EFAULT;
663 break;
664 }
665
Marcel Holtmann2c068e02014-01-15 22:37:41 -0800666 if (opt & RFCOMM_LM_FIPS) {
667 err = -EINVAL;
668 break;
669 }
670
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100671 if (opt & RFCOMM_LM_AUTH)
672 rfcomm_pi(sk)->sec_level = BT_SECURITY_LOW;
673 if (opt & RFCOMM_LM_ENCRYPT)
674 rfcomm_pi(sk)->sec_level = BT_SECURITY_MEDIUM;
675 if (opt & RFCOMM_LM_SECURE)
676 rfcomm_pi(sk)->sec_level = BT_SECURITY_HIGH;
677
678 rfcomm_pi(sk)->role_switch = (opt & RFCOMM_LM_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 break;
680
681 default:
682 err = -ENOPROTOOPT;
683 break;
684 }
685
686 release_sock(sk);
687 return err;
688}
689
David S. Millerb7058842009-09-30 16:12:20 -0700690static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100691{
692 struct sock *sk = sock->sk;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100693 struct bt_security sec;
Stephen Boydd0fad892011-05-12 16:50:09 -0700694 int err = 0;
695 size_t len;
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100696 u32 opt;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100697
698 BT_DBG("sk %p", sk);
699
700 if (level == SOL_RFCOMM)
701 return rfcomm_sock_setsockopt_old(sock, optname, optval, optlen);
702
Marcel Holtmann0588d942009-01-16 10:06:13 +0100703 if (level != SOL_BLUETOOTH)
704 return -ENOPROTOOPT;
705
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100706 lock_sock(sk);
707
708 switch (optname) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100709 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +0100710 if (sk->sk_type != SOCK_STREAM) {
711 err = -EINVAL;
712 break;
713 }
714
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100715 sec.level = BT_SECURITY_LOW;
716
717 len = min_t(unsigned int, sizeof(sec), optlen);
718 if (copy_from_user((char *) &sec, optval, len)) {
719 err = -EFAULT;
720 break;
721 }
722
723 if (sec.level > BT_SECURITY_HIGH) {
724 err = -EINVAL;
725 break;
726 }
727
728 rfcomm_pi(sk)->sec_level = sec.level;
729 break;
730
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100731 case BT_DEFER_SETUP:
732 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
733 err = -EINVAL;
734 break;
735 }
736
737 if (get_user(opt, (u32 __user *) optval)) {
738 err = -EFAULT;
739 break;
740 }
741
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300742 if (opt)
743 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
744 else
745 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
746
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100747 break;
748
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100749 default:
750 err = -ENOPROTOOPT;
751 break;
752 }
753
754 release_sock(sk);
755 return err;
756}
757
758static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 struct sock *sk = sock->sk;
Marcel Holtmann60c7a3c92013-11-02 02:36:31 -0700761 struct sock *l2cap_sk;
762 struct l2cap_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct rfcomm_conninfo cinfo;
764 int len, err = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100765 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 BT_DBG("sk %p", sk);
768
769 if (get_user(len, optlen))
770 return -EFAULT;
771
772 lock_sock(sk);
773
774 switch (optname) {
775 case RFCOMM_LM:
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100776 switch (rfcomm_pi(sk)->sec_level) {
777 case BT_SECURITY_LOW:
778 opt = RFCOMM_LM_AUTH;
779 break;
780 case BT_SECURITY_MEDIUM:
781 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT;
782 break;
783 case BT_SECURITY_HIGH:
784 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT |
Marcel Holtmann2c068e02014-01-15 22:37:41 -0800785 RFCOMM_LM_SECURE;
786 break;
787 case BT_SECURITY_FIPS:
788 opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT |
789 RFCOMM_LM_SECURE | RFCOMM_LM_FIPS;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100790 break;
791 default:
792 opt = 0;
793 break;
794 }
795
796 if (rfcomm_pi(sk)->role_switch)
797 opt |= RFCOMM_LM_MASTER;
798
799 if (put_user(opt, (u32 __user *) optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 err = -EFAULT;
Marcel Holtmann2c068e02014-01-15 22:37:41 -0800801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 break;
803
804 case RFCOMM_CONNINFO:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100805 if (sk->sk_state != BT_CONNECTED &&
806 !rfcomm_pi(sk)->dlc->defer_setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 err = -ENOTCONN;
808 break;
809 }
810
Marcel Holtmann60c7a3c92013-11-02 02:36:31 -0700811 l2cap_sk = rfcomm_pi(sk)->dlc->session->sock->sk;
812 conn = l2cap_pi(l2cap_sk)->chan->conn;
813
Filip Palian8d03e972011-05-12 19:32:46 +0200814 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300815 cinfo.hci_handle = conn->hcon->handle;
816 memcpy(cinfo.dev_class, conn->hcon->dev_class, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 len = min_t(unsigned int, len, sizeof(cinfo));
819 if (copy_to_user(optval, (char *) &cinfo, len))
820 err = -EFAULT;
821
822 break;
823
824 default:
825 err = -ENOPROTOOPT;
826 break;
827 }
828
829 release_sock(sk);
830 return err;
831}
832
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100833static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
834{
835 struct sock *sk = sock->sk;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100836 struct bt_security sec;
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100837 int len, err = 0;
838
839 BT_DBG("sk %p", sk);
840
841 if (level == SOL_RFCOMM)
842 return rfcomm_sock_getsockopt_old(sock, optname, optval, optlen);
843
Marcel Holtmann0588d942009-01-16 10:06:13 +0100844 if (level != SOL_BLUETOOTH)
845 return -ENOPROTOOPT;
846
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100847 if (get_user(len, optlen))
848 return -EFAULT;
849
850 lock_sock(sk);
851
852 switch (optname) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100853 case BT_SECURITY:
Marcel Holtmann0588d942009-01-16 10:06:13 +0100854 if (sk->sk_type != SOCK_STREAM) {
855 err = -EINVAL;
856 break;
857 }
858
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100859 sec.level = rfcomm_pi(sk)->sec_level;
Mathias Krause9ad2de42012-08-15 11:31:48 +0000860 sec.key_size = 0;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100861
862 len = min_t(unsigned int, len, sizeof(sec));
863 if (copy_to_user(optval, (char *) &sec, len))
864 err = -EFAULT;
865
866 break;
867
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100868 case BT_DEFER_SETUP:
869 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
870 err = -EINVAL;
871 break;
872 }
873
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300874 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
875 (u32 __user *) optval))
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100876 err = -EFAULT;
877
878 break;
879
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100880 default:
881 err = -ENOPROTOOPT;
882 break;
883 }
884
885 release_sock(sk);
886 return err;
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889static int rfcomm_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
890{
David S. Millere19caae2008-12-09 01:04:27 -0800891 struct sock *sk __maybe_unused = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 int err;
893
David S. Millere19caae2008-12-09 01:04:27 -0800894 BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200896 err = bt_sock_ioctl(sock, cmd, arg);
897
898 if (err == -ENOIOCTLCMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#ifdef CONFIG_BT_RFCOMM_TTY
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200900 lock_sock(sk);
901 err = rfcomm_dev_ioctl(sk, cmd, (void __user *) arg);
902 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903#else
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200904 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#endif
Marcel Holtmann3241ad82008-07-14 20:13:50 +0200906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return err;
909}
910
911static int rfcomm_sock_shutdown(struct socket *sock, int how)
912{
913 struct sock *sk = sock->sk;
914 int err = 0;
915
916 BT_DBG("sock %p, sk %p", sock, sk);
917
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +0200918 if (!sk)
919 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 lock_sock(sk);
922 if (!sk->sk_shutdown) {
923 sk->sk_shutdown = SHUTDOWN_MASK;
924 __rfcomm_sock_close(sk);
925
Vladimir Davydov093facf2014-07-15 12:25:28 +0400926 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
927 !(current->flags & PF_EXITING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
929 }
930 release_sock(sk);
931 return err;
932}
933
934static int rfcomm_sock_release(struct socket *sock)
935{
936 struct sock *sk = sock->sk;
937 int err;
938
939 BT_DBG("sock %p, sk %p", sock, sk);
940
941 if (!sk)
942 return 0;
943
944 err = rfcomm_sock_shutdown(sock, 2);
945
946 sock_orphan(sk);
947 rfcomm_sock_kill(sk);
948 return err;
949}
950
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900951/* ---- RFCOMM core layer callbacks ----
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *
953 * called under rfcomm_lock()
954 */
955int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d)
956{
957 struct sock *sk, *parent;
958 bdaddr_t src, dst;
959 int result = 0;
960
961 BT_DBG("session %p channel %d", s, channel);
962
963 rfcomm_session_getaddr(s, &src, &dst);
964
965 /* Check if we have socket listening on channel */
966 parent = rfcomm_get_sock_by_channel(BT_LISTEN, channel, &src);
967 if (!parent)
968 return 0;
969
Gustavo F. Padovaneeb36652010-11-01 18:43:53 +0000970 bh_lock_sock(parent);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Check for backlog size */
973 if (sk_acceptq_is_full(parent)) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900974 BT_DBG("backlog full %d", parent->sk_ack_backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 goto done;
976 }
977
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500978 sk = rfcomm_sock_alloc(sock_net(parent), NULL, BTPROTO_RFCOMM, GFP_ATOMIC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!sk)
980 goto done;
981
Octavian Purdilab5a30dd2012-01-22 00:28:34 +0200982 bt_sock_reclassify_lock(sk, BTPROTO_RFCOMM);
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 rfcomm_sock_init(sk, parent);
Marcel Holtmann94a86df2013-10-13 10:34:02 -0700985 bacpy(&rfcomm_pi(sk)->src, &src);
986 bacpy(&rfcomm_pi(sk)->dst, &dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 rfcomm_pi(sk)->channel = channel;
988
989 sk->sk_state = BT_CONFIG;
990 bt_accept_enqueue(parent, sk);
991
992 /* Accept connection and return socket DLC */
993 *d = rfcomm_pi(sk)->dlc;
994 result = 1;
995
996done:
997 bh_unlock_sock(parent);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100998
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300999 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001000 parent->sk_state_change(parent);
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return result;
1003}
1004
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001005static int rfcomm_sock_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -02001009 read_lock(&rfcomm_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Sasha Levinb67bfe02013-02-27 17:06:00 -08001011 sk_for_each(sk, &rfcomm_sk_list.head) {
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03001012 seq_printf(f, "%pMR %pMR %d %d\n",
Marcel Holtmann94a86df2013-10-13 10:34:02 -07001013 &rfcomm_pi(sk)->src, &rfcomm_pi(sk)->dst,
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03001014 sk->sk_state, rfcomm_pi(sk)->channel);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Gustavo F. Padovan95ca83f2011-12-27 15:28:44 -02001017 read_unlock(&rfcomm_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001018
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001022static int rfcomm_sock_debugfs_open(struct inode *inode, struct file *file)
1023{
1024 return single_open(file, rfcomm_sock_debugfs_show, inode->i_private);
1025}
1026
1027static const struct file_operations rfcomm_sock_debugfs_fops = {
1028 .open = rfcomm_sock_debugfs_open,
1029 .read = seq_read,
1030 .llseek = seq_lseek,
1031 .release = single_release,
1032};
1033
1034static struct dentry *rfcomm_sock_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001036static const struct proto_ops rfcomm_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 .family = PF_BLUETOOTH,
1038 .owner = THIS_MODULE,
1039 .release = rfcomm_sock_release,
1040 .bind = rfcomm_sock_bind,
1041 .connect = rfcomm_sock_connect,
1042 .listen = rfcomm_sock_listen,
1043 .accept = rfcomm_sock_accept,
1044 .getname = rfcomm_sock_getname,
1045 .sendmsg = rfcomm_sock_sendmsg,
1046 .recvmsg = rfcomm_sock_recvmsg,
1047 .shutdown = rfcomm_sock_shutdown,
1048 .setsockopt = rfcomm_sock_setsockopt,
1049 .getsockopt = rfcomm_sock_getsockopt,
1050 .ioctl = rfcomm_sock_ioctl,
1051 .poll = bt_sock_poll,
1052 .socketpair = sock_no_socketpair,
1053 .mmap = sock_no_mmap
1054};
1055
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001056static const struct net_proto_family rfcomm_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 .family = PF_BLUETOOTH,
1058 .owner = THIS_MODULE,
1059 .create = rfcomm_sock_create
1060};
1061
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001062int __init rfcomm_init_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 int err;
1065
Marcel Holtmann74b3fb82015-01-11 15:18:20 -08001066 BUILD_BUG_ON(sizeof(struct sockaddr_rc) > sizeof(struct sockaddr));
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 err = proto_register(&rfcomm_proto, 0);
1069 if (err < 0)
1070 return err;
1071
1072 err = bt_sock_register(BTPROTO_RFCOMM, &rfcomm_sock_family_ops);
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001073 if (err < 0) {
1074 BT_ERR("RFCOMM socket layer registration failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto error;
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001076 }
1077
Al Virob0316612013-04-04 19:14:33 -04001078 err = bt_procfs_init(&init_net, "rfcomm", &rfcomm_sk_list, NULL);
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001079 if (err < 0) {
1080 BT_ERR("Failed to create RFCOMM proc file");
1081 bt_sock_unregister(BTPROTO_RFCOMM);
1082 goto error;
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 BT_INFO("RFCOMM socket layer initialized");
1086
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07001087 if (IS_ERR_OR_NULL(bt_debugfs))
1088 return 0;
1089
1090 rfcomm_sock_debugfs = debugfs_create_file("rfcomm", 0444,
1091 bt_debugfs, NULL,
1092 &rfcomm_sock_debugfs_fops);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return 0;
1095
1096error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 proto_unregister(&rfcomm_proto);
1098 return err;
1099}
1100
Gustavo F. Padovan2f8362a2010-07-24 02:04:45 -03001101void __exit rfcomm_cleanup_sockets(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Masatake YAMATOc6f5df162012-07-26 01:29:49 +09001103 bt_procfs_cleanup(&init_net, "rfcomm");
1104
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001105 debugfs_remove(rfcomm_sock_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
David Herrmann5e9d7f82013-02-24 19:36:51 +01001107 bt_sock_unregister(BTPROTO_RFCOMM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 proto_unregister(&rfcomm_proto);
1110}