blob: 0098af80b21327045c99a38771ed8c95da2544c2 [file] [log] [blame]
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4 Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
5 Copyright (C) 2010 Google Inc.
Gustavo F. Padovan590051d2011-12-18 13:39:33 -02006 Copyright (C) 2011 ProFUSION Embedded Systems
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007
8 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License version 2 as
12 published by the Free Software Foundation;
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
17 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
18 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
24 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
25 SOFTWARE IS DISCLAIMED.
26*/
27
28/* Bluetooth L2CAP sockets. */
29
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040030#include <linux/export.h>
Paul Moore6230c9b2011-10-07 09:40:59 +000031
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020032#include <net/bluetooth/bluetooth.h>
Gustavo F. Padovan33575df2011-02-04 02:48:48 -020033#include <net/bluetooth/hci_core.h>
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020034#include <net/bluetooth/l2cap.h>
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -030035#include <net/bluetooth/smp.h>
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020036
Masatake YAMATO5b28d952012-07-26 01:29:25 +090037static struct bt_sock_list l2cap_sk_list = {
38 .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock)
39};
40
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030041static const struct proto_ops l2cap_sock_ops;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -030042static void l2cap_sock_init(struct sock *sk, struct sock *parent);
Gustavo Padovan2d792812012-10-06 10:07:01 +010043static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
44 int proto, gfp_t prio);
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030045
David Herrmannb3916db2013-04-05 14:57:34 +020046bool l2cap_is_socket(struct socket *sock)
47{
48 return sock && sock->ops == &l2cap_sock_ops;
49}
50EXPORT_SYMBOL(l2cap_is_socket);
51
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020052static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
53{
54 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -030055 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020056 struct sockaddr_l2 la;
57 int len, err = 0;
58
59 BT_DBG("sk %p", sk);
60
61 if (!addr || addr->sa_family != AF_BLUETOOTH)
62 return -EINVAL;
63
64 memset(&la, 0, sizeof(la));
65 len = min_t(unsigned int, sizeof(la), alen);
66 memcpy(&la, addr, len);
67
Ville Tervob62f3282011-02-10 22:38:50 -030068 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020069 return -EINVAL;
70
71 lock_sock(sk);
72
73 if (sk->sk_state != BT_OPEN) {
74 err = -EBADFD;
75 goto done;
76 }
77
78 if (la.l2_psm) {
79 __u16 psm = __le16_to_cpu(la.l2_psm);
80
81 /* PSM must be odd and lsb of upper byte must be 0 */
82 if ((psm & 0x0101) != 0x0001) {
83 err = -EINVAL;
84 goto done;
85 }
86
87 /* Restrict usage of well-known PSMs */
88 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
89 err = -EACCES;
90 goto done;
91 }
92 }
93
Ville Tervob62f3282011-02-10 22:38:50 -030094 if (la.l2_cid)
Santosh Nayak6e4aff12012-03-01 22:46:36 +053095 err = l2cap_add_scid(chan, __le16_to_cpu(la.l2_cid));
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030096 else
97 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
Ville Tervob62f3282011-02-10 22:38:50 -030098
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030099 if (err < 0)
100 goto done;
101
Andrei Emeltchenko2983fd62012-05-24 15:42:50 +0300102 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP ||
103 __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300104 chan->sec_level = BT_SECURITY_SDP;
105
106 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300107
108 chan->state = BT_BOUND;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300109 sk->sk_state = BT_BOUND;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200110
111done:
112 release_sock(sk);
113 return err;
114}
115
Gustavo Padovan2d792812012-10-06 10:07:01 +0100116static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr,
117 int alen, int flags)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200118{
119 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300120 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200121 struct sockaddr_l2 la;
122 int len, err = 0;
123
124 BT_DBG("sk %p", sk);
125
126 if (!addr || alen < sizeof(addr->sa_family) ||
127 addr->sa_family != AF_BLUETOOTH)
128 return -EINVAL;
129
130 memset(&la, 0, sizeof(la));
131 len = min_t(unsigned int, sizeof(la), alen);
132 memcpy(&la, addr, len);
133
Ville Tervoacd7d372011-02-10 22:38:49 -0300134 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200135 return -EINVAL;
136
Santosh Nayak6e4aff12012-03-01 22:46:36 +0530137 err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid),
Andre Guedes8e9f9892012-04-24 21:02:55 -0300138 &la.l2_bdaddr, la.l2_bdaddr_type);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200139 if (err)
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200140 return err;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200141
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200142 lock_sock(sk);
143
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200144 err = bt_sock_wait_state(sk, BT_CONNECTED,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100145 sock_sndtimeo(sk, flags & O_NONBLOCK));
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200146
147 release_sock(sk);
148
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200149 return err;
150}
151
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200152static int l2cap_sock_listen(struct socket *sock, int backlog)
153{
154 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300155 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200156 int err = 0;
157
158 BT_DBG("sk %p backlog %d", sk, backlog);
159
160 lock_sock(sk);
161
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200162 if (sk->sk_state != BT_BOUND) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200163 err = -EBADFD;
164 goto done;
165 }
166
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200167 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) {
168 err = -EINVAL;
169 goto done;
170 }
171
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300172 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200173 case L2CAP_MODE_BASIC:
174 break;
175 case L2CAP_MODE_ERTM:
176 case L2CAP_MODE_STREAMING:
177 if (!disable_ertm)
178 break;
179 /* fall through */
180 default:
181 err = -ENOTSUPP;
182 goto done;
183 }
184
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200185 sk->sk_max_ack_backlog = backlog;
186 sk->sk_ack_backlog = 0;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300187
188 chan->state = BT_LISTEN;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200189 sk->sk_state = BT_LISTEN;
190
191done:
192 release_sock(sk);
193 return err;
194}
195
Gustavo Padovan2d792812012-10-06 10:07:01 +0100196static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
197 int flags)
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200198{
199 DECLARE_WAITQUEUE(wait, current);
200 struct sock *sk = sock->sk, *nsk;
201 long timeo;
202 int err = 0;
203
204 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
205
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200206 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
207
208 BT_DBG("sk %p timeo %ld", sk, timeo);
209
210 /* Wait for an incoming connection. (wake-one). */
211 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400212 while (1) {
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200213 set_current_state(TASK_INTERRUPTIBLE);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200214
215 if (sk->sk_state != BT_LISTEN) {
216 err = -EBADFD;
217 break;
218 }
219
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400220 nsk = bt_accept_dequeue(sk, newsock);
221 if (nsk)
222 break;
223
224 if (!timeo) {
225 err = -EAGAIN;
226 break;
227 }
228
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200229 if (signal_pending(current)) {
230 err = sock_intr_errno(timeo);
231 break;
232 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400233
234 release_sock(sk);
235 timeo = schedule_timeout(timeo);
236 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200237 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400238 __set_current_state(TASK_RUNNING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200239 remove_wait_queue(sk_sleep(sk), &wait);
240
241 if (err)
242 goto done;
243
244 newsock->state = SS_CONNECTED;
245
246 BT_DBG("new socket %p", nsk);
247
248done:
249 release_sock(sk);
250 return err;
251}
252
Gustavo Padovan2d792812012-10-06 10:07:01 +0100253static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr,
254 int *len, int peer)
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200255{
256 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
257 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300258 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200259
260 BT_DBG("sock %p, sk %p", sock, sk);
261
Mathias Krause792039c2012-08-15 11:31:51 +0000262 memset(la, 0, sizeof(struct sockaddr_l2));
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200263 addr->sa_family = AF_BLUETOOTH;
264 *len = sizeof(struct sockaddr_l2);
265
266 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300267 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200268 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300269 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200270 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300271 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200272 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300273 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200274 }
275
276 return 0;
277}
278
Gustavo Padovan2d792812012-10-06 10:07:01 +0100279static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
280 char __user *optval, int __user *optlen)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200281{
282 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300283 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200284 struct l2cap_options opts;
285 struct l2cap_conninfo cinfo;
286 int len, err = 0;
287 u32 opt;
288
289 BT_DBG("sk %p", sk);
290
291 if (get_user(len, optlen))
292 return -EFAULT;
293
294 lock_sock(sk);
295
296 switch (optname) {
297 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300298 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300299 opts.imtu = chan->imtu;
300 opts.omtu = chan->omtu;
301 opts.flush_to = chan->flush_to;
302 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300303 opts.fcs = chan->fcs;
304 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300305 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200306
307 len = min_t(unsigned int, len, sizeof(opts));
308 if (copy_to_user(optval, (char *) &opts, len))
309 err = -EFAULT;
310
311 break;
312
313 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300314 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200315 case BT_SECURITY_LOW:
316 opt = L2CAP_LM_AUTH;
317 break;
318 case BT_SECURITY_MEDIUM:
319 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
320 break;
321 case BT_SECURITY_HIGH:
322 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
Gustavo Padovan2d792812012-10-06 10:07:01 +0100323 L2CAP_LM_SECURE;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200324 break;
325 default:
326 opt = 0;
327 break;
328 }
329
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300330 if (test_bit(FLAG_ROLE_SWITCH, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200331 opt |= L2CAP_LM_MASTER;
332
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300333 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200334 opt |= L2CAP_LM_RELIABLE;
335
336 if (put_user(opt, (u32 __user *) optval))
337 err = -EFAULT;
338 break;
339
340 case L2CAP_CONNINFO:
341 if (sk->sk_state != BT_CONNECTED &&
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300342 !(sk->sk_state == BT_CONNECT2 &&
343 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200344 err = -ENOTCONN;
345 break;
346 }
347
Filip Palian8d03e972011-05-12 19:32:46 +0200348 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300349 cinfo.hci_handle = chan->conn->hcon->handle;
350 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200351
352 len = min_t(unsigned int, len, sizeof(cinfo));
353 if (copy_to_user(optval, (char *) &cinfo, len))
354 err = -EFAULT;
355
356 break;
357
358 default:
359 err = -ENOPROTOOPT;
360 break;
361 }
362
363 release_sock(sk);
364 return err;
365}
366
Gustavo Padovan2d792812012-10-06 10:07:01 +0100367static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
368 char __user *optval, int __user *optlen)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200369{
370 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300371 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200372 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700373 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200374 int len, err = 0;
375
376 BT_DBG("sk %p", sk);
377
378 if (level == SOL_L2CAP)
379 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
380
381 if (level != SOL_BLUETOOTH)
382 return -ENOPROTOOPT;
383
384 if (get_user(len, optlen))
385 return -EFAULT;
386
387 lock_sock(sk);
388
389 switch (optname) {
390 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300391 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100392 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200393 err = -EINVAL;
394 break;
395 }
396
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300397 memset(&sec, 0, sizeof(sec));
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300398 if (chan->conn) {
Gustavo Padovanc6585a42012-05-07 03:07:26 -0300399 sec.level = chan->conn->hcon->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200400
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300401 if (sk->sk_state == BT_CONNECTED)
402 sec.key_size = chan->conn->hcon->enc_key_size;
403 } else {
404 sec.level = chan->sec_level;
405 }
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300406
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200407 len = min_t(unsigned int, len, sizeof(sec));
408 if (copy_to_user(optval, (char *) &sec, len))
409 err = -EFAULT;
410
411 break;
412
413 case BT_DEFER_SETUP:
414 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
415 err = -EINVAL;
416 break;
417 }
418
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300419 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
420 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200421 err = -EFAULT;
422
423 break;
424
425 case BT_FLUSHABLE:
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300426 if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags),
Gustavo Padovan2d792812012-10-06 10:07:01 +0100427 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200428 err = -EFAULT;
429
430 break;
431
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700432 case BT_POWER:
433 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
Gustavo Padovan2d792812012-10-06 10:07:01 +0100434 && sk->sk_type != SOCK_RAW) {
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700435 err = -EINVAL;
436 break;
437 }
438
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300439 pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700440
441 len = min_t(unsigned int, len, sizeof(pwr));
442 if (copy_to_user(optval, (char *) &pwr, len))
443 err = -EFAULT;
444
445 break;
446
Mat Martineau2ea66482011-11-02 16:18:30 -0700447 case BT_CHANNEL_POLICY:
448 if (!enable_hs) {
449 err = -ENOPROTOOPT;
450 break;
451 }
452
453 if (put_user(chan->chan_policy, (u32 __user *) optval))
454 err = -EFAULT;
455 break;
456
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200457 default:
458 err = -ENOPROTOOPT;
459 break;
460 }
461
462 release_sock(sk);
463 return err;
464}
465
Andre Guedes682877c2012-05-31 17:01:34 -0300466static bool l2cap_valid_mtu(struct l2cap_chan *chan, u16 mtu)
467{
468 switch (chan->scid) {
Johan Hedberg073d1cf2013-04-29 19:35:35 +0300469 case L2CAP_CID_ATT:
Andre Guedes8c3a4f02012-05-31 17:01:35 -0300470 if (mtu < L2CAP_LE_MIN_MTU)
Andre Guedes682877c2012-05-31 17:01:34 -0300471 return false;
472 break;
473
474 default:
475 if (mtu < L2CAP_DEFAULT_MIN_MTU)
476 return false;
477 }
478
479 return true;
480}
481
Gustavo Padovan2d792812012-10-06 10:07:01 +0100482static int l2cap_sock_setsockopt_old(struct socket *sock, int optname,
483 char __user *optval, unsigned int optlen)
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200484{
485 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300486 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200487 struct l2cap_options opts;
488 int len, err = 0;
489 u32 opt;
490
491 BT_DBG("sk %p", sk);
492
493 lock_sock(sk);
494
495 switch (optname) {
496 case L2CAP_OPTIONS:
497 if (sk->sk_state == BT_CONNECTED) {
498 err = -EINVAL;
499 break;
500 }
501
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300502 opts.imtu = chan->imtu;
503 opts.omtu = chan->omtu;
504 opts.flush_to = chan->flush_to;
505 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300506 opts.fcs = chan->fcs;
507 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300508 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200509
510 len = min_t(unsigned int, sizeof(opts), optlen);
511 if (copy_from_user((char *) &opts, optval, len)) {
512 err = -EFAULT;
513 break;
514 }
515
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300516 if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200517 err = -EINVAL;
518 break;
519 }
520
Andre Guedes682877c2012-05-31 17:01:34 -0300521 if (!l2cap_valid_mtu(chan, opts.imtu)) {
522 err = -EINVAL;
523 break;
524 }
525
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300526 chan->mode = opts.mode;
527 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200528 case L2CAP_MODE_BASIC:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -0300529 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200530 break;
531 case L2CAP_MODE_ERTM:
532 case L2CAP_MODE_STREAMING:
533 if (!disable_ertm)
534 break;
535 /* fall through */
536 default:
537 err = -EINVAL;
538 break;
539 }
540
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300541 chan->imtu = opts.imtu;
542 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300543 chan->fcs = opts.fcs;
544 chan->max_tx = opts.max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300545 chan->tx_win = opts.txwin_size;
Andrei Emeltchenko12d59782012-10-10 17:38:26 +0300546 chan->flush_to = opts.flush_to;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200547 break;
548
549 case L2CAP_LM:
550 if (get_user(opt, (u32 __user *) optval)) {
551 err = -EFAULT;
552 break;
553 }
554
555 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300556 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200557 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300558 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200559 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300560 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200561
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300562 if (opt & L2CAP_LM_MASTER)
563 set_bit(FLAG_ROLE_SWITCH, &chan->flags);
564 else
565 clear_bit(FLAG_ROLE_SWITCH, &chan->flags);
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300566
567 if (opt & L2CAP_LM_RELIABLE)
568 set_bit(FLAG_FORCE_RELIABLE, &chan->flags);
569 else
570 clear_bit(FLAG_FORCE_RELIABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200571 break;
572
573 default:
574 err = -ENOPROTOOPT;
575 break;
576 }
577
578 release_sock(sk);
579 return err;
580}
581
Gustavo Padovan2d792812012-10-06 10:07:01 +0100582static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
583 char __user *optval, unsigned int optlen)
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200584{
585 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300586 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200587 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700588 struct bt_power pwr;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300589 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200590 int len, err = 0;
591 u32 opt;
592
593 BT_DBG("sk %p", sk);
594
595 if (level == SOL_L2CAP)
596 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
597
598 if (level != SOL_BLUETOOTH)
599 return -ENOPROTOOPT;
600
601 lock_sock(sk);
602
603 switch (optname) {
604 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300605 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100606 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200607 err = -EINVAL;
608 break;
609 }
610
611 sec.level = BT_SECURITY_LOW;
612
613 len = min_t(unsigned int, sizeof(sec), optlen);
614 if (copy_from_user((char *) &sec, optval, len)) {
615 err = -EFAULT;
616 break;
617 }
618
619 if (sec.level < BT_SECURITY_LOW ||
Gustavo Padovan2d792812012-10-06 10:07:01 +0100620 sec.level > BT_SECURITY_HIGH) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200621 err = -EINVAL;
622 break;
623 }
624
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300625 chan->sec_level = sec.level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300626
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200627 if (!chan->conn)
628 break;
629
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300630 conn = chan->conn;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200631
632 /*change security for LE channels */
Johan Hedberg073d1cf2013-04-29 19:35:35 +0300633 if (chan->scid == L2CAP_CID_ATT) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300634 if (!conn->hcon->out) {
635 err = -EINVAL;
636 break;
637 }
638
Vinicius Costa Gomescc110922012-08-23 21:32:43 -0300639 if (smp_conn_security(conn->hcon, sec.level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300640 break;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300641 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200642 chan->state = BT_CONFIG;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200643
Gustavo Padovana7d77232012-05-13 03:20:07 -0300644 /* or for ACL link */
645 } else if ((sk->sk_state == BT_CONNECT2 &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100646 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) ||
Gustavo Padovana7d77232012-05-13 03:20:07 -0300647 sk->sk_state == BT_CONNECTED) {
648 if (!l2cap_chan_check_security(chan))
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300649 set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
Gustavo Padovana7d77232012-05-13 03:20:07 -0300650 else
651 sk->sk_state_change(sk);
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200652 } else {
653 err = -EINVAL;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300654 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200655 break;
656
657 case BT_DEFER_SETUP:
658 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
659 err = -EINVAL;
660 break;
661 }
662
663 if (get_user(opt, (u32 __user *) optval)) {
664 err = -EFAULT;
665 break;
666 }
667
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300668 if (opt)
669 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
670 else
671 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200672 break;
673
674 case BT_FLUSHABLE:
675 if (get_user(opt, (u32 __user *) optval)) {
676 err = -EFAULT;
677 break;
678 }
679
680 if (opt > BT_FLUSHABLE_ON) {
681 err = -EINVAL;
682 break;
683 }
684
685 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300686 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300687 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200688 No Flush support in the LM */
689 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
690 err = -EINVAL;
691 break;
692 }
693 }
694
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300695 if (opt)
696 set_bit(FLAG_FLUSHABLE, &chan->flags);
697 else
698 clear_bit(FLAG_FLUSHABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200699 break;
700
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700701 case BT_POWER:
702 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100703 chan->chan_type != L2CAP_CHAN_RAW) {
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700704 err = -EINVAL;
705 break;
706 }
707
708 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
709
710 len = min_t(unsigned int, sizeof(pwr), optlen);
711 if (copy_from_user((char *) &pwr, optval, len)) {
712 err = -EFAULT;
713 break;
714 }
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300715
716 if (pwr.force_active)
717 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
718 else
719 clear_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700720 break;
721
Mat Martineau2ea66482011-11-02 16:18:30 -0700722 case BT_CHANNEL_POLICY:
723 if (!enable_hs) {
724 err = -ENOPROTOOPT;
725 break;
726 }
727
728 if (get_user(opt, (u32 __user *) optval)) {
729 err = -EFAULT;
730 break;
731 }
732
733 if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) {
734 err = -EINVAL;
735 break;
736 }
737
738 if (chan->mode != L2CAP_MODE_ERTM &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100739 chan->mode != L2CAP_MODE_STREAMING) {
Mat Martineau2ea66482011-11-02 16:18:30 -0700740 err = -EOPNOTSUPP;
741 break;
742 }
743
744 chan->chan_policy = (u8) opt;
Mat Martineau3f7a56c2012-10-23 15:24:23 -0700745
746 if (sk->sk_state == BT_CONNECTED &&
747 chan->move_role == L2CAP_MOVE_ROLE_NONE)
748 l2cap_move_start(chan);
749
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200750 break;
751
752 default:
753 err = -ENOPROTOOPT;
754 break;
755 }
756
757 release_sock(sk);
758 return err;
759}
760
Gustavo Padovan2d792812012-10-06 10:07:01 +0100761static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
762 struct msghdr *msg, size_t len)
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200763{
764 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300765 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200766 int err;
767
768 BT_DBG("sock %p, sk %p", sock, sk);
769
770 err = sock_error(sk);
771 if (err)
772 return err;
773
774 if (msg->msg_flags & MSG_OOB)
775 return -EOPNOTSUPP;
776
Mat Martineaua6a55682012-05-04 14:20:31 -0700777 if (sk->sk_state != BT_CONNECTED)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300778 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200779
Mat Martineaua6a55682012-05-04 14:20:31 -0700780 l2cap_chan_lock(chan);
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200781 err = l2cap_chan_send(chan, msg, len, sk->sk_priority);
Mat Martineaua6a55682012-05-04 14:20:31 -0700782 l2cap_chan_unlock(chan);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200783
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200784 return err;
785}
786
Gustavo Padovan2d792812012-10-06 10:07:01 +0100787static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
788 struct msghdr *msg, size_t len, int flags)
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200789{
790 struct sock *sk = sock->sk;
Mat Martineaue3281402011-07-07 09:39:02 -0700791 struct l2cap_pinfo *pi = l2cap_pi(sk);
792 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200793
794 lock_sock(sk);
795
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300796 if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
797 &bt_sk(sk)->flags)) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300798 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200799 pi->chan->state = BT_CONFIG;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300800
Mat Martineaue3281402011-07-07 09:39:02 -0700801 __l2cap_connect_rsp_defer(pi->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200802 release_sock(sk);
803 return 0;
804 }
805
806 release_sock(sk);
807
808 if (sock->type == SOCK_STREAM)
Mat Martineaue3281402011-07-07 09:39:02 -0700809 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
810 else
811 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200812
Mat Martineaue3281402011-07-07 09:39:02 -0700813 if (pi->chan->mode != L2CAP_MODE_ERTM)
814 return err;
815
816 /* Attempt to put pending rx data in the socket buffer */
817
818 lock_sock(sk);
819
820 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
821 goto done;
822
823 if (pi->rx_busy_skb) {
824 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
825 pi->rx_busy_skb = NULL;
826 else
827 goto done;
828 }
829
830 /* Restore data flow when half of the receive buffer is
831 * available. This avoids resending large numbers of
832 * frames.
833 */
834 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
835 l2cap_chan_busy(pi->chan, 0);
836
837done:
838 release_sock(sk);
839 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200840}
841
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200842/* Kill socket (only if zapped and orphan)
843 * Must be called on unlocked socket.
844 */
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300845static void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200846{
847 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
848 return;
849
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200850 BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200851
852 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300853
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530854 l2cap_chan_put(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200855 sock_set_flag(sk, SOCK_DEAD);
856 sock_put(sk);
857}
858
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200859static int l2cap_sock_shutdown(struct socket *sock, int how)
860{
861 struct sock *sk = sock->sk;
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200862 struct l2cap_chan *chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200863 struct l2cap_conn *conn;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200864 int err = 0;
865
866 BT_DBG("sock %p, sk %p", sock, sk);
867
868 if (!sk)
869 return 0;
870
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200871 chan = l2cap_pi(sk)->chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200872 conn = chan->conn;
873
874 if (conn)
875 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200876
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200877 l2cap_chan_lock(chan);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200878 lock_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200879
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200880 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300881 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200882 err = __l2cap_wait_ack(sk);
883
884 sk->sk_shutdown = SHUTDOWN_MASK;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200885
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200886 release_sock(sk);
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300887 l2cap_chan_close(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200888 lock_sock(sk);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200889
890 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
891 err = bt_sock_wait_state(sk, BT_CLOSED,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100892 sk->sk_lingertime);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200893 }
894
895 if (!err && sk->sk_err)
896 err = -sk->sk_err;
897
898 release_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200899 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200900
901 if (conn)
902 mutex_unlock(&conn->chan_lock);
903
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200904 return err;
905}
906
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200907static int l2cap_sock_release(struct socket *sock)
908{
909 struct sock *sk = sock->sk;
910 int err;
911
912 BT_DBG("sock %p, sk %p", sock, sk);
913
914 if (!sk)
915 return 0;
916
Masatake YAMATO5b28d952012-07-26 01:29:25 +0900917 bt_sock_unlink(&l2cap_sk_list, sk);
918
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200919 err = l2cap_sock_shutdown(sock, 2);
920
921 sock_orphan(sk);
922 l2cap_sock_kill(sk);
923 return err;
924}
925
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -0300926static void l2cap_sock_cleanup_listen(struct sock *parent)
927{
928 struct sock *sk;
929
930 BT_DBG("parent %p", parent);
931
932 /* Close not yet accepted channels */
933 while ((sk = bt_accept_dequeue(parent, NULL))) {
934 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
935
936 l2cap_chan_lock(chan);
937 __clear_chan_timer(chan);
938 l2cap_chan_close(chan, ECONNRESET);
939 l2cap_chan_unlock(chan);
940
941 l2cap_sock_kill(sk);
942 }
943}
944
Gustavo Padovan80b98022012-05-27 22:27:51 -0300945static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300946{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300947 struct sock *sk, *parent = chan->data;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300948
Gustavo Padovan53826692012-05-27 22:27:55 -0300949 /* Check for backlog size */
950 if (sk_acceptq_is_full(parent)) {
951 BT_DBG("backlog full %d", parent->sk_ack_backlog);
952 return NULL;
953 }
954
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300955 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100956 GFP_ATOMIC);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300957 if (!sk)
958 return NULL;
959
Octavian Purdilad22015a2012-01-22 00:28:34 +0200960 bt_sock_reclassify_lock(sk, BTPROTO_L2CAP);
961
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300962 l2cap_sock_init(sk, parent);
963
Gustavo Padovan644912e2012-10-12 19:35:23 +0800964 bt_accept_enqueue(parent, sk);
965
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300966 return l2cap_pi(sk)->chan;
967}
968
Gustavo Padovan80b98022012-05-27 22:27:51 -0300969static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300970{
Mat Martineaue3281402011-07-07 09:39:02 -0700971 int err;
Gustavo Padovan80b98022012-05-27 22:27:51 -0300972 struct sock *sk = chan->data;
Mat Martineaue3281402011-07-07 09:39:02 -0700973 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300974
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200975 lock_sock(sk);
976
977 if (pi->rx_busy_skb) {
978 err = -ENOMEM;
979 goto done;
980 }
Mat Martineaue3281402011-07-07 09:39:02 -0700981
982 err = sock_queue_rcv_skb(sk, skb);
983
984 /* For ERTM, handle one skb that doesn't fit into the recv
985 * buffer. This is important to do because the data frames
986 * have already been acked, so the skb cannot be discarded.
987 *
988 * Notify the l2cap core that the buffer is full, so the
989 * LOCAL_BUSY state is entered and no more frames are
990 * acked and reassembled until there is buffer space
991 * available.
992 */
993 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
994 pi->rx_busy_skb = skb;
995 l2cap_chan_busy(pi->chan, 1);
996 err = 0;
997 }
998
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200999done:
1000 release_sock(sk);
1001
Mat Martineaue3281402011-07-07 09:39:02 -07001002 return err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001003}
1004
Gustavo Padovan80b98022012-05-27 22:27:51 -03001005static void l2cap_sock_close_cb(struct l2cap_chan *chan)
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001006{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001007 struct sock *sk = chan->data;
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001008
1009 l2cap_sock_kill(sk);
1010}
1011
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001012static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
1013{
1014 struct sock *sk = chan->data;
1015 struct sock *parent;
1016
1017 lock_sock(sk);
1018
1019 parent = bt_sk(sk)->parent;
1020
1021 sock_set_flag(sk, SOCK_ZAPPED);
1022
1023 switch (chan->state) {
1024 case BT_OPEN:
1025 case BT_BOUND:
1026 case BT_CLOSED:
1027 break;
1028 case BT_LISTEN:
1029 l2cap_sock_cleanup_listen(sk);
1030 sk->sk_state = BT_CLOSED;
1031 chan->state = BT_CLOSED;
1032
1033 break;
1034 default:
1035 sk->sk_state = BT_CLOSED;
1036 chan->state = BT_CLOSED;
1037
1038 sk->sk_err = err;
1039
1040 if (parent) {
1041 bt_accept_unlink(sk);
1042 parent->sk_data_ready(parent, 0);
1043 } else {
1044 sk->sk_state_change(sk);
1045 }
1046
1047 break;
1048 }
1049
1050 release_sock(sk);
1051}
1052
Gustavo Padovan80b98022012-05-27 22:27:51 -03001053static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001054{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001055 struct sock *sk = chan->data;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001056
1057 sk->sk_state = state;
1058}
1059
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001060static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
Gustavo Padovan90338942012-04-06 20:15:47 -03001061 unsigned long len, int nb)
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001062{
Gustavo Padovan90338942012-04-06 20:15:47 -03001063 struct sk_buff *skb;
1064 int err;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001065
Mat Martineaua6a55682012-05-04 14:20:31 -07001066 l2cap_chan_unlock(chan);
Gustavo Padovan90338942012-04-06 20:15:47 -03001067 skb = bt_skb_send_alloc(chan->sk, len, nb, &err);
Mat Martineaua6a55682012-05-04 14:20:31 -07001068 l2cap_chan_lock(chan);
1069
Gustavo Padovan90338942012-04-06 20:15:47 -03001070 if (!skb)
1071 return ERR_PTR(err);
1072
1073 return skb;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001074}
1075
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001076static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
1077{
1078 struct sock *sk = chan->data;
1079 struct sock *parent;
1080
1081 lock_sock(sk);
1082
1083 parent = bt_sk(sk)->parent;
1084
1085 BT_DBG("sk %p, parent %p", sk, parent);
1086
1087 sk->sk_state = BT_CONNECTED;
1088 sk->sk_state_change(sk);
1089
1090 if (parent)
1091 parent->sk_data_ready(parent, 0);
1092
1093 release_sock(sk);
1094}
1095
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001096static void l2cap_sock_defer_cb(struct l2cap_chan *chan)
1097{
1098 struct sock *sk = chan->data;
1099 struct sock *parent = bt_sk(sk)->parent;
1100
1101 if (parent)
1102 parent->sk_data_ready(parent, 0);
1103}
1104
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001105static struct l2cap_ops l2cap_chan_ops = {
1106 .name = "L2CAP Socket Interface",
1107 .new_connection = l2cap_sock_new_connection_cb,
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001108 .recv = l2cap_sock_recv_cb,
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001109 .close = l2cap_sock_close_cb,
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001110 .teardown = l2cap_sock_teardown_cb,
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001111 .state_change = l2cap_sock_state_change_cb,
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001112 .ready = l2cap_sock_ready_cb,
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001113 .defer = l2cap_sock_defer_cb,
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001114 .alloc_skb = l2cap_sock_alloc_skb_cb,
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001115};
1116
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001117static void l2cap_sock_destruct(struct sock *sk)
1118{
1119 BT_DBG("sk %p", sk);
1120
Sasha Levin23d3a862012-10-08 16:48:32 -04001121 if (l2cap_pi(sk)->chan)
1122 l2cap_chan_put(l2cap_pi(sk)->chan);
Mat Martineaue3281402011-07-07 09:39:02 -07001123 if (l2cap_pi(sk)->rx_busy_skb) {
1124 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
1125 l2cap_pi(sk)->rx_busy_skb = NULL;
1126 }
1127
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001128 skb_queue_purge(&sk->sk_receive_queue);
1129 skb_queue_purge(&sk->sk_write_queue);
1130}
1131
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001132static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001133{
1134 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001135 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001136
1137 BT_DBG("sk %p", sk);
1138
1139 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001140 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
1141
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001142 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -03001143 bt_sk(sk)->flags = bt_sk(parent)->flags;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001144
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001145 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001146 chan->imtu = pchan->imtu;
1147 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001148 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001149 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001150 chan->fcs = pchan->fcs;
1151 chan->max_tx = pchan->max_tx;
1152 chan->tx_win = pchan->tx_win;
Andrei Emeltchenko6b3c7102011-11-02 09:57:10 +02001153 chan->tx_win_max = pchan->tx_win_max;
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001154 chan->sec_level = pchan->sec_level;
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +03001155 chan->flags = pchan->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +00001156
1157 security_sk_clone(parent, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001158 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001159
1160 switch (sk->sk_type) {
1161 case SOCK_RAW:
1162 chan->chan_type = L2CAP_CHAN_RAW;
1163 break;
1164 case SOCK_DGRAM:
1165 chan->chan_type = L2CAP_CHAN_CONN_LESS;
1166 break;
1167 case SOCK_SEQPACKET:
1168 case SOCK_STREAM:
1169 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
1170 break;
1171 }
1172
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001173 chan->imtu = L2CAP_DEFAULT_MTU;
1174 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001175 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001176 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001177 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001178 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001179 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001180 }
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +03001181
1182 l2cap_chan_set_defaults(chan);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001183 }
1184
1185 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001186 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001187
1188 chan->data = sk;
1189 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001190}
1191
1192static struct proto l2cap_proto = {
1193 .name = "L2CAP",
1194 .owner = THIS_MODULE,
1195 .obj_size = sizeof(struct l2cap_pinfo)
1196};
1197
Gustavo Padovan2d792812012-10-06 10:07:01 +01001198static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
1199 int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001200{
1201 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001202 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001203
1204 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1205 if (!sk)
1206 return NULL;
1207
1208 sock_init_data(sock, sk);
1209 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1210
1211 sk->sk_destruct = l2cap_sock_destruct;
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08001212 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001213
1214 sock_reset_flag(sk, SOCK_ZAPPED);
1215
1216 sk->sk_protocol = proto;
1217 sk->sk_state = BT_OPEN;
1218
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001219 chan = l2cap_chan_create();
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001220 if (!chan) {
Jaganath Kanakkassery49dfbb92012-07-19 12:54:04 +05301221 sk_free(sk);
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001222 return NULL;
1223 }
1224
Mat Martineau61d6ef32012-04-27 16:50:50 -07001225 l2cap_chan_hold(chan);
1226
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001227 chan->sk = sk;
1228
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001229 l2cap_pi(sk)->chan = chan;
1230
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001231 return sk;
1232}
1233
1234static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1235 int kern)
1236{
1237 struct sock *sk;
1238
1239 BT_DBG("sock %p", sock);
1240
1241 sock->state = SS_UNCONNECTED;
1242
1243 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01001244 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001245 return -ESOCKTNOSUPPORT;
1246
1247 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1248 return -EPERM;
1249
1250 sock->ops = &l2cap_sock_ops;
1251
1252 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1253 if (!sk)
1254 return -ENOMEM;
1255
1256 l2cap_sock_init(sk, NULL);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001257 bt_sock_link(&l2cap_sk_list, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001258 return 0;
1259}
1260
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -03001261static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001262 .family = PF_BLUETOOTH,
1263 .owner = THIS_MODULE,
1264 .release = l2cap_sock_release,
1265 .bind = l2cap_sock_bind,
1266 .connect = l2cap_sock_connect,
1267 .listen = l2cap_sock_listen,
1268 .accept = l2cap_sock_accept,
1269 .getname = l2cap_sock_getname,
1270 .sendmsg = l2cap_sock_sendmsg,
1271 .recvmsg = l2cap_sock_recvmsg,
1272 .poll = bt_sock_poll,
1273 .ioctl = bt_sock_ioctl,
1274 .mmap = sock_no_mmap,
1275 .socketpair = sock_no_socketpair,
1276 .shutdown = l2cap_sock_shutdown,
1277 .setsockopt = l2cap_sock_setsockopt,
1278 .getsockopt = l2cap_sock_getsockopt
1279};
1280
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001281static const struct net_proto_family l2cap_sock_family_ops = {
1282 .family = PF_BLUETOOTH,
1283 .owner = THIS_MODULE,
1284 .create = l2cap_sock_create,
1285};
1286
1287int __init l2cap_init_sockets(void)
1288{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001289 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001290
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001291 err = proto_register(&l2cap_proto, 0);
1292 if (err < 0)
1293 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001294
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001295 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001296 if (err < 0) {
1297 BT_ERR("L2CAP socket registration failed");
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001298 goto error;
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001299 }
1300
Al Virob0316612013-04-04 19:14:33 -04001301 err = bt_procfs_init(&init_net, "l2cap", &l2cap_sk_list,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001302 NULL);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001303 if (err < 0) {
1304 BT_ERR("Failed to create L2CAP proc file");
1305 bt_sock_unregister(BTPROTO_L2CAP);
1306 goto error;
1307 }
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001308
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001309 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001310
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001311 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001312
1313error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001314 proto_unregister(&l2cap_proto);
1315 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001316}
1317
1318void l2cap_cleanup_sockets(void)
1319{
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001320 bt_procfs_cleanup(&init_net, "l2cap");
David Herrmann5e9d7f82013-02-24 19:36:51 +01001321 bt_sock_unregister(BTPROTO_L2CAP);
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001322 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001323}