blob: 9fe80d228c191816b2a1bca6d8ba4f158edbf7d8 [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>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070035
36#include "smp.h"
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020037
Masatake YAMATO5b28d952012-07-26 01:29:25 +090038static struct bt_sock_list l2cap_sk_list = {
39 .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock)
40};
41
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030042static const struct proto_ops l2cap_sock_ops;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -030043static void l2cap_sock_init(struct sock *sk, struct sock *parent);
Gustavo Padovan2d792812012-10-06 10:07:01 +010044static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
45 int proto, gfp_t prio);
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030046
David Herrmannb3916db2013-04-05 14:57:34 +020047bool l2cap_is_socket(struct socket *sock)
48{
49 return sock && sock->ops == &l2cap_sock_ops;
50}
51EXPORT_SYMBOL(l2cap_is_socket);
52
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020053static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
54{
55 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -030056 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020057 struct sockaddr_l2 la;
58 int len, err = 0;
59
60 BT_DBG("sk %p", sk);
61
62 if (!addr || addr->sa_family != AF_BLUETOOTH)
63 return -EINVAL;
64
65 memset(&la, 0, sizeof(la));
66 len = min_t(unsigned int, sizeof(la), alen);
67 memcpy(&la, addr, len);
68
Ville Tervob62f3282011-02-10 22:38:50 -030069 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020070 return -EINVAL;
71
72 lock_sock(sk);
73
74 if (sk->sk_state != BT_OPEN) {
75 err = -EBADFD;
76 goto done;
77 }
78
79 if (la.l2_psm) {
80 __u16 psm = __le16_to_cpu(la.l2_psm);
81
82 /* PSM must be odd and lsb of upper byte must be 0 */
83 if ((psm & 0x0101) != 0x0001) {
84 err = -EINVAL;
85 goto done;
86 }
87
88 /* Restrict usage of well-known PSMs */
89 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
90 err = -EACCES;
91 goto done;
92 }
93 }
94
Ville Tervob62f3282011-02-10 22:38:50 -030095 if (la.l2_cid)
Santosh Nayak6e4aff12012-03-01 22:46:36 +053096 err = l2cap_add_scid(chan, __le16_to_cpu(la.l2_cid));
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030097 else
98 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
Ville Tervob62f3282011-02-10 22:38:50 -030099
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300100 if (err < 0)
101 goto done;
102
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700103 switch (chan->chan_type) {
Marcel Holtmann3124b842013-10-12 07:19:32 -0700104 case L2CAP_CHAN_CONN_LESS:
105 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_3DSP)
106 chan->sec_level = BT_SECURITY_SDP;
107 break;
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700108 case L2CAP_CHAN_CONN_ORIENTED:
109 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP ||
110 __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM)
111 chan->sec_level = BT_SECURITY_SDP;
112 break;
113 }
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300114
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700115 bacpy(&chan->src, &la.l2_bdaddr);
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300116
117 chan->state = BT_BOUND;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300118 sk->sk_state = BT_BOUND;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200119
120done:
121 release_sock(sk);
122 return err;
123}
124
Gustavo Padovan2d792812012-10-06 10:07:01 +0100125static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr,
126 int alen, int flags)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200127{
128 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300129 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200130 struct sockaddr_l2 la;
131 int len, err = 0;
132
133 BT_DBG("sk %p", sk);
134
135 if (!addr || alen < sizeof(addr->sa_family) ||
136 addr->sa_family != AF_BLUETOOTH)
137 return -EINVAL;
138
139 memset(&la, 0, sizeof(la));
140 len = min_t(unsigned int, sizeof(la), alen);
141 memcpy(&la, addr, len);
142
Ville Tervoacd7d372011-02-10 22:38:49 -0300143 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200144 return -EINVAL;
145
Santosh Nayak6e4aff12012-03-01 22:46:36 +0530146 err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid),
Andre Guedes8e9f9892012-04-24 21:02:55 -0300147 &la.l2_bdaddr, la.l2_bdaddr_type);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200148 if (err)
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200149 return err;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200150
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200151 lock_sock(sk);
152
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200153 err = bt_sock_wait_state(sk, BT_CONNECTED,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100154 sock_sndtimeo(sk, flags & O_NONBLOCK));
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200155
156 release_sock(sk);
157
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200158 return err;
159}
160
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200161static int l2cap_sock_listen(struct socket *sock, int backlog)
162{
163 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300164 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200165 int err = 0;
166
167 BT_DBG("sk %p backlog %d", sk, backlog);
168
169 lock_sock(sk);
170
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200171 if (sk->sk_state != BT_BOUND) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200172 err = -EBADFD;
173 goto done;
174 }
175
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200176 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) {
177 err = -EINVAL;
178 goto done;
179 }
180
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300181 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200182 case L2CAP_MODE_BASIC:
183 break;
184 case L2CAP_MODE_ERTM:
185 case L2CAP_MODE_STREAMING:
186 if (!disable_ertm)
187 break;
188 /* fall through */
189 default:
190 err = -ENOTSUPP;
191 goto done;
192 }
193
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200194 sk->sk_max_ack_backlog = backlog;
195 sk->sk_ack_backlog = 0;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300196
197 chan->state = BT_LISTEN;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200198 sk->sk_state = BT_LISTEN;
199
200done:
201 release_sock(sk);
202 return err;
203}
204
Gustavo Padovan2d792812012-10-06 10:07:01 +0100205static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
206 int flags)
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200207{
208 DECLARE_WAITQUEUE(wait, current);
209 struct sock *sk = sock->sk, *nsk;
210 long timeo;
211 int err = 0;
212
213 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
214
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200215 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
216
217 BT_DBG("sk %p timeo %ld", sk, timeo);
218
219 /* Wait for an incoming connection. (wake-one). */
220 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400221 while (1) {
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200222 set_current_state(TASK_INTERRUPTIBLE);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200223
224 if (sk->sk_state != BT_LISTEN) {
225 err = -EBADFD;
226 break;
227 }
228
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400229 nsk = bt_accept_dequeue(sk, newsock);
230 if (nsk)
231 break;
232
233 if (!timeo) {
234 err = -EAGAIN;
235 break;
236 }
237
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200238 if (signal_pending(current)) {
239 err = sock_intr_errno(timeo);
240 break;
241 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400242
243 release_sock(sk);
244 timeo = schedule_timeout(timeo);
245 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200246 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400247 __set_current_state(TASK_RUNNING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200248 remove_wait_queue(sk_sleep(sk), &wait);
249
250 if (err)
251 goto done;
252
253 newsock->state = SS_CONNECTED;
254
255 BT_DBG("new socket %p", nsk);
256
257done:
258 release_sock(sk);
259 return err;
260}
261
Gustavo Padovan2d792812012-10-06 10:07:01 +0100262static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr,
263 int *len, int peer)
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200264{
265 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
266 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300267 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200268
269 BT_DBG("sock %p, sk %p", sock, sk);
270
Mathias Krause792039c2012-08-15 11:31:51 +0000271 memset(la, 0, sizeof(struct sockaddr_l2));
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200272 addr->sa_family = AF_BLUETOOTH;
273 *len = sizeof(struct sockaddr_l2);
274
275 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300276 la->l2_psm = chan->psm;
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700277 bacpy(&la->l2_bdaddr, &chan->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300278 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200279 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300280 la->l2_psm = chan->sport;
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700281 bacpy(&la->l2_bdaddr, &chan->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300282 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200283 }
284
285 return 0;
286}
287
Gustavo Padovan2d792812012-10-06 10:07:01 +0100288static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
289 char __user *optval, int __user *optlen)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200290{
291 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300292 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200293 struct l2cap_options opts;
294 struct l2cap_conninfo cinfo;
295 int len, err = 0;
296 u32 opt;
297
298 BT_DBG("sk %p", sk);
299
300 if (get_user(len, optlen))
301 return -EFAULT;
302
303 lock_sock(sk);
304
305 switch (optname) {
306 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300307 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300308 opts.imtu = chan->imtu;
309 opts.omtu = chan->omtu;
310 opts.flush_to = chan->flush_to;
311 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300312 opts.fcs = chan->fcs;
313 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300314 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200315
316 len = min_t(unsigned int, len, sizeof(opts));
317 if (copy_to_user(optval, (char *) &opts, len))
318 err = -EFAULT;
319
320 break;
321
322 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300323 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200324 case BT_SECURITY_LOW:
325 opt = L2CAP_LM_AUTH;
326 break;
327 case BT_SECURITY_MEDIUM:
328 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
329 break;
330 case BT_SECURITY_HIGH:
331 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
Gustavo Padovan2d792812012-10-06 10:07:01 +0100332 L2CAP_LM_SECURE;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200333 break;
334 default:
335 opt = 0;
336 break;
337 }
338
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300339 if (test_bit(FLAG_ROLE_SWITCH, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200340 opt |= L2CAP_LM_MASTER;
341
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300342 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200343 opt |= L2CAP_LM_RELIABLE;
344
345 if (put_user(opt, (u32 __user *) optval))
346 err = -EFAULT;
347 break;
348
349 case L2CAP_CONNINFO:
350 if (sk->sk_state != BT_CONNECTED &&
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300351 !(sk->sk_state == BT_CONNECT2 &&
352 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200353 err = -ENOTCONN;
354 break;
355 }
356
Filip Palian8d03e972011-05-12 19:32:46 +0200357 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300358 cinfo.hci_handle = chan->conn->hcon->handle;
359 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200360
361 len = min_t(unsigned int, len, sizeof(cinfo));
362 if (copy_to_user(optval, (char *) &cinfo, len))
363 err = -EFAULT;
364
365 break;
366
367 default:
368 err = -ENOPROTOOPT;
369 break;
370 }
371
372 release_sock(sk);
373 return err;
374}
375
Gustavo Padovan2d792812012-10-06 10:07:01 +0100376static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
377 char __user *optval, int __user *optlen)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200378{
379 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300380 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200381 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700382 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200383 int len, err = 0;
384
385 BT_DBG("sk %p", sk);
386
387 if (level == SOL_L2CAP)
388 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
389
390 if (level != SOL_BLUETOOTH)
391 return -ENOPROTOOPT;
392
393 if (get_user(len, optlen))
394 return -EFAULT;
395
396 lock_sock(sk);
397
398 switch (optname) {
399 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300400 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100401 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200402 err = -EINVAL;
403 break;
404 }
405
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300406 memset(&sec, 0, sizeof(sec));
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300407 if (chan->conn) {
Gustavo Padovanc6585a42012-05-07 03:07:26 -0300408 sec.level = chan->conn->hcon->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200409
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300410 if (sk->sk_state == BT_CONNECTED)
411 sec.key_size = chan->conn->hcon->enc_key_size;
412 } else {
413 sec.level = chan->sec_level;
414 }
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300415
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200416 len = min_t(unsigned int, len, sizeof(sec));
417 if (copy_to_user(optval, (char *) &sec, len))
418 err = -EFAULT;
419
420 break;
421
422 case BT_DEFER_SETUP:
423 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
424 err = -EINVAL;
425 break;
426 }
427
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300428 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
429 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200430 err = -EFAULT;
431
432 break;
433
434 case BT_FLUSHABLE:
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300435 if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags),
Gustavo Padovan2d792812012-10-06 10:07:01 +0100436 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200437 err = -EFAULT;
438
439 break;
440
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700441 case BT_POWER:
442 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
Gustavo Padovan2d792812012-10-06 10:07:01 +0100443 && sk->sk_type != SOCK_RAW) {
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700444 err = -EINVAL;
445 break;
446 }
447
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300448 pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700449
450 len = min_t(unsigned int, len, sizeof(pwr));
451 if (copy_to_user(optval, (char *) &pwr, len))
452 err = -EFAULT;
453
454 break;
455
Mat Martineau2ea66482011-11-02 16:18:30 -0700456 case BT_CHANNEL_POLICY:
Mat Martineau2ea66482011-11-02 16:18:30 -0700457 if (put_user(chan->chan_policy, (u32 __user *) optval))
458 err = -EFAULT;
459 break;
460
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200461 default:
462 err = -ENOPROTOOPT;
463 break;
464 }
465
466 release_sock(sk);
467 return err;
468}
469
Andre Guedes682877c2012-05-31 17:01:34 -0300470static bool l2cap_valid_mtu(struct l2cap_chan *chan, u16 mtu)
471{
472 switch (chan->scid) {
Johan Hedberg073d1cf2013-04-29 19:35:35 +0300473 case L2CAP_CID_ATT:
Andre Guedes8c3a4f02012-05-31 17:01:35 -0300474 if (mtu < L2CAP_LE_MIN_MTU)
Andre Guedes682877c2012-05-31 17:01:34 -0300475 return false;
476 break;
477
478 default:
479 if (mtu < L2CAP_DEFAULT_MIN_MTU)
480 return false;
481 }
482
483 return true;
484}
485
Gustavo Padovan2d792812012-10-06 10:07:01 +0100486static int l2cap_sock_setsockopt_old(struct socket *sock, int optname,
487 char __user *optval, unsigned int optlen)
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200488{
489 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300490 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200491 struct l2cap_options opts;
492 int len, err = 0;
493 u32 opt;
494
495 BT_DBG("sk %p", sk);
496
497 lock_sock(sk);
498
499 switch (optname) {
500 case L2CAP_OPTIONS:
501 if (sk->sk_state == BT_CONNECTED) {
502 err = -EINVAL;
503 break;
504 }
505
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300506 opts.imtu = chan->imtu;
507 opts.omtu = chan->omtu;
508 opts.flush_to = chan->flush_to;
509 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300510 opts.fcs = chan->fcs;
511 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300512 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200513
514 len = min_t(unsigned int, sizeof(opts), optlen);
515 if (copy_from_user((char *) &opts, optval, len)) {
516 err = -EFAULT;
517 break;
518 }
519
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300520 if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200521 err = -EINVAL;
522 break;
523 }
524
Andre Guedes682877c2012-05-31 17:01:34 -0300525 if (!l2cap_valid_mtu(chan, opts.imtu)) {
526 err = -EINVAL;
527 break;
528 }
529
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300530 chan->mode = opts.mode;
531 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200532 case L2CAP_MODE_BASIC:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -0300533 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200534 break;
535 case L2CAP_MODE_ERTM:
536 case L2CAP_MODE_STREAMING:
537 if (!disable_ertm)
538 break;
539 /* fall through */
540 default:
541 err = -EINVAL;
542 break;
543 }
544
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300545 chan->imtu = opts.imtu;
546 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300547 chan->fcs = opts.fcs;
548 chan->max_tx = opts.max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300549 chan->tx_win = opts.txwin_size;
Andrei Emeltchenko12d59782012-10-10 17:38:26 +0300550 chan->flush_to = opts.flush_to;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200551 break;
552
553 case L2CAP_LM:
554 if (get_user(opt, (u32 __user *) optval)) {
555 err = -EFAULT;
556 break;
557 }
558
559 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300560 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200561 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300562 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200563 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300564 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200565
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300566 if (opt & L2CAP_LM_MASTER)
567 set_bit(FLAG_ROLE_SWITCH, &chan->flags);
568 else
569 clear_bit(FLAG_ROLE_SWITCH, &chan->flags);
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300570
571 if (opt & L2CAP_LM_RELIABLE)
572 set_bit(FLAG_FORCE_RELIABLE, &chan->flags);
573 else
574 clear_bit(FLAG_FORCE_RELIABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200575 break;
576
577 default:
578 err = -ENOPROTOOPT;
579 break;
580 }
581
582 release_sock(sk);
583 return err;
584}
585
Gustavo Padovan2d792812012-10-06 10:07:01 +0100586static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
587 char __user *optval, unsigned int optlen)
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200588{
589 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300590 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200591 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700592 struct bt_power pwr;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300593 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200594 int len, err = 0;
595 u32 opt;
596
597 BT_DBG("sk %p", sk);
598
599 if (level == SOL_L2CAP)
600 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
601
602 if (level != SOL_BLUETOOTH)
603 return -ENOPROTOOPT;
604
605 lock_sock(sk);
606
607 switch (optname) {
608 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300609 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100610 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200611 err = -EINVAL;
612 break;
613 }
614
615 sec.level = BT_SECURITY_LOW;
616
617 len = min_t(unsigned int, sizeof(sec), optlen);
618 if (copy_from_user((char *) &sec, optval, len)) {
619 err = -EFAULT;
620 break;
621 }
622
623 if (sec.level < BT_SECURITY_LOW ||
Gustavo Padovan2d792812012-10-06 10:07:01 +0100624 sec.level > BT_SECURITY_HIGH) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200625 err = -EINVAL;
626 break;
627 }
628
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300629 chan->sec_level = sec.level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300630
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200631 if (!chan->conn)
632 break;
633
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300634 conn = chan->conn;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200635
636 /*change security for LE channels */
Johan Hedberg073d1cf2013-04-29 19:35:35 +0300637 if (chan->scid == L2CAP_CID_ATT) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300638 if (!conn->hcon->out) {
639 err = -EINVAL;
640 break;
641 }
642
Vinicius Costa Gomescc110922012-08-23 21:32:43 -0300643 if (smp_conn_security(conn->hcon, sec.level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300644 break;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300645 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200646 chan->state = BT_CONFIG;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200647
Gustavo Padovana7d77232012-05-13 03:20:07 -0300648 /* or for ACL link */
649 } else if ((sk->sk_state == BT_CONNECT2 &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100650 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) ||
Gustavo Padovana7d77232012-05-13 03:20:07 -0300651 sk->sk_state == BT_CONNECTED) {
652 if (!l2cap_chan_check_security(chan))
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300653 set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
Gustavo Padovana7d77232012-05-13 03:20:07 -0300654 else
655 sk->sk_state_change(sk);
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200656 } else {
657 err = -EINVAL;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300658 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200659 break;
660
661 case BT_DEFER_SETUP:
662 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
663 err = -EINVAL;
664 break;
665 }
666
667 if (get_user(opt, (u32 __user *) optval)) {
668 err = -EFAULT;
669 break;
670 }
671
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300672 if (opt)
673 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
674 else
675 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200676 break;
677
678 case BT_FLUSHABLE:
679 if (get_user(opt, (u32 __user *) optval)) {
680 err = -EFAULT;
681 break;
682 }
683
684 if (opt > BT_FLUSHABLE_ON) {
685 err = -EINVAL;
686 break;
687 }
688
689 if (opt == BT_FLUSHABLE_OFF) {
Johannes Bergc1f23a22013-10-07 18:19:16 +0200690 conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300691 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200692 No Flush support in the LM */
693 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
694 err = -EINVAL;
695 break;
696 }
697 }
698
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300699 if (opt)
700 set_bit(FLAG_FLUSHABLE, &chan->flags);
701 else
702 clear_bit(FLAG_FLUSHABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200703 break;
704
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700705 case BT_POWER:
706 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100707 chan->chan_type != L2CAP_CHAN_RAW) {
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700708 err = -EINVAL;
709 break;
710 }
711
712 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
713
714 len = min_t(unsigned int, sizeof(pwr), optlen);
715 if (copy_from_user((char *) &pwr, optval, len)) {
716 err = -EFAULT;
717 break;
718 }
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300719
720 if (pwr.force_active)
721 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
722 else
723 clear_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700724 break;
725
Mat Martineau2ea66482011-11-02 16:18:30 -0700726 case BT_CHANNEL_POLICY:
Mat Martineau2ea66482011-11-02 16:18:30 -0700727 if (get_user(opt, (u32 __user *) optval)) {
728 err = -EFAULT;
729 break;
730 }
731
732 if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) {
733 err = -EINVAL;
734 break;
735 }
736
737 if (chan->mode != L2CAP_MODE_ERTM &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100738 chan->mode != L2CAP_MODE_STREAMING) {
Mat Martineau2ea66482011-11-02 16:18:30 -0700739 err = -EOPNOTSUPP;
740 break;
741 }
742
743 chan->chan_policy = (u8) opt;
Mat Martineau3f7a56c2012-10-23 15:24:23 -0700744
745 if (sk->sk_state == BT_CONNECTED &&
746 chan->move_role == L2CAP_MOVE_ROLE_NONE)
747 l2cap_move_start(chan);
748
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200749 break;
750
751 default:
752 err = -ENOPROTOOPT;
753 break;
754 }
755
756 release_sock(sk);
757 return err;
758}
759
Gustavo Padovan2d792812012-10-06 10:07:01 +0100760static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
761 struct msghdr *msg, size_t len)
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200762{
763 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300764 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200765 int err;
766
767 BT_DBG("sock %p, sk %p", sock, sk);
768
769 err = sock_error(sk);
770 if (err)
771 return err;
772
773 if (msg->msg_flags & MSG_OOB)
774 return -EOPNOTSUPP;
775
Mat Martineaua6a55682012-05-04 14:20:31 -0700776 if (sk->sk_state != BT_CONNECTED)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300777 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200778
Johan Hedberge793dcf2013-09-16 13:05:19 +0300779 lock_sock(sk);
780 err = bt_sock_wait_ready(sk, msg->msg_flags);
781 release_sock(sk);
782 if (err)
783 return err;
784
Mat Martineaua6a55682012-05-04 14:20:31 -0700785 l2cap_chan_lock(chan);
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200786 err = l2cap_chan_send(chan, msg, len, sk->sk_priority);
Mat Martineaua6a55682012-05-04 14:20:31 -0700787 l2cap_chan_unlock(chan);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200788
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200789 return err;
790}
791
Gustavo Padovan2d792812012-10-06 10:07:01 +0100792static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
793 struct msghdr *msg, size_t len, int flags)
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200794{
795 struct sock *sk = sock->sk;
Mat Martineaue3281402011-07-07 09:39:02 -0700796 struct l2cap_pinfo *pi = l2cap_pi(sk);
797 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200798
799 lock_sock(sk);
800
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300801 if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
802 &bt_sk(sk)->flags)) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300803 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200804 pi->chan->state = BT_CONFIG;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300805
Mat Martineaue3281402011-07-07 09:39:02 -0700806 __l2cap_connect_rsp_defer(pi->chan);
Johan Hedberg970871b2013-09-25 13:26:05 +0300807 err = 0;
808 goto done;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200809 }
810
811 release_sock(sk);
812
813 if (sock->type == SOCK_STREAM)
Mat Martineaue3281402011-07-07 09:39:02 -0700814 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
815 else
816 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200817
Mat Martineaue3281402011-07-07 09:39:02 -0700818 if (pi->chan->mode != L2CAP_MODE_ERTM)
819 return err;
820
821 /* Attempt to put pending rx data in the socket buffer */
822
823 lock_sock(sk);
824
825 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
826 goto done;
827
828 if (pi->rx_busy_skb) {
829 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
830 pi->rx_busy_skb = NULL;
831 else
832 goto done;
833 }
834
835 /* Restore data flow when half of the receive buffer is
836 * available. This avoids resending large numbers of
837 * frames.
838 */
839 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
840 l2cap_chan_busy(pi->chan, 0);
841
842done:
843 release_sock(sk);
844 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200845}
846
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200847/* Kill socket (only if zapped and orphan)
848 * Must be called on unlocked socket.
849 */
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300850static void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200851{
852 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
853 return;
854
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200855 BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200856
857 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300858
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530859 l2cap_chan_put(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200860 sock_set_flag(sk, SOCK_DEAD);
861 sock_put(sk);
862}
863
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200864static int l2cap_sock_shutdown(struct socket *sock, int how)
865{
866 struct sock *sk = sock->sk;
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200867 struct l2cap_chan *chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200868 struct l2cap_conn *conn;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200869 int err = 0;
870
871 BT_DBG("sock %p, sk %p", sock, sk);
872
873 if (!sk)
874 return 0;
875
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200876 chan = l2cap_pi(sk)->chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200877 conn = chan->conn;
878
879 if (conn)
880 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200881
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200882 l2cap_chan_lock(chan);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200883 lock_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200884
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200885 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300886 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200887 err = __l2cap_wait_ack(sk);
888
889 sk->sk_shutdown = SHUTDOWN_MASK;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200890
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200891 release_sock(sk);
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300892 l2cap_chan_close(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200893 lock_sock(sk);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200894
895 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
896 err = bt_sock_wait_state(sk, BT_CLOSED,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100897 sk->sk_lingertime);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200898 }
899
900 if (!err && sk->sk_err)
901 err = -sk->sk_err;
902
903 release_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200904 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200905
906 if (conn)
907 mutex_unlock(&conn->chan_lock);
908
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200909 return err;
910}
911
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200912static int l2cap_sock_release(struct socket *sock)
913{
914 struct sock *sk = sock->sk;
915 int err;
916
917 BT_DBG("sock %p, sk %p", sock, sk);
918
919 if (!sk)
920 return 0;
921
Masatake YAMATO5b28d952012-07-26 01:29:25 +0900922 bt_sock_unlink(&l2cap_sk_list, sk);
923
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200924 err = l2cap_sock_shutdown(sock, 2);
925
926 sock_orphan(sk);
927 l2cap_sock_kill(sk);
928 return err;
929}
930
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -0300931static void l2cap_sock_cleanup_listen(struct sock *parent)
932{
933 struct sock *sk;
934
935 BT_DBG("parent %p", parent);
936
937 /* Close not yet accepted channels */
938 while ((sk = bt_accept_dequeue(parent, NULL))) {
939 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
940
941 l2cap_chan_lock(chan);
942 __clear_chan_timer(chan);
943 l2cap_chan_close(chan, ECONNRESET);
944 l2cap_chan_unlock(chan);
945
946 l2cap_sock_kill(sk);
947 }
948}
949
Gustavo Padovan80b98022012-05-27 22:27:51 -0300950static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300951{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300952 struct sock *sk, *parent = chan->data;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300953
Gustavo Padovan53826692012-05-27 22:27:55 -0300954 /* Check for backlog size */
955 if (sk_acceptq_is_full(parent)) {
956 BT_DBG("backlog full %d", parent->sk_ack_backlog);
957 return NULL;
958 }
959
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300960 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100961 GFP_ATOMIC);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300962 if (!sk)
963 return NULL;
964
Octavian Purdilad22015a2012-01-22 00:28:34 +0200965 bt_sock_reclassify_lock(sk, BTPROTO_L2CAP);
966
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300967 l2cap_sock_init(sk, parent);
968
Gustavo Padovan644912e2012-10-12 19:35:23 +0800969 bt_accept_enqueue(parent, sk);
970
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300971 return l2cap_pi(sk)->chan;
972}
973
Gustavo Padovan80b98022012-05-27 22:27:51 -0300974static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300975{
Mat Martineaue3281402011-07-07 09:39:02 -0700976 int err;
Gustavo Padovan80b98022012-05-27 22:27:51 -0300977 struct sock *sk = chan->data;
Mat Martineaue3281402011-07-07 09:39:02 -0700978 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300979
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200980 lock_sock(sk);
981
982 if (pi->rx_busy_skb) {
983 err = -ENOMEM;
984 goto done;
985 }
Mat Martineaue3281402011-07-07 09:39:02 -0700986
987 err = sock_queue_rcv_skb(sk, skb);
988
989 /* For ERTM, handle one skb that doesn't fit into the recv
990 * buffer. This is important to do because the data frames
991 * have already been acked, so the skb cannot be discarded.
992 *
993 * Notify the l2cap core that the buffer is full, so the
994 * LOCAL_BUSY state is entered and no more frames are
995 * acked and reassembled until there is buffer space
996 * available.
997 */
998 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
999 pi->rx_busy_skb = skb;
1000 l2cap_chan_busy(pi->chan, 1);
1001 err = 0;
1002 }
1003
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001004done:
1005 release_sock(sk);
1006
Mat Martineaue3281402011-07-07 09:39:02 -07001007 return err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001008}
1009
Gustavo Padovan80b98022012-05-27 22:27:51 -03001010static void l2cap_sock_close_cb(struct l2cap_chan *chan)
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001011{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001012 struct sock *sk = chan->data;
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001013
1014 l2cap_sock_kill(sk);
1015}
1016
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001017static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
1018{
1019 struct sock *sk = chan->data;
1020 struct sock *parent;
1021
1022 lock_sock(sk);
1023
1024 parent = bt_sk(sk)->parent;
1025
1026 sock_set_flag(sk, SOCK_ZAPPED);
1027
1028 switch (chan->state) {
1029 case BT_OPEN:
1030 case BT_BOUND:
1031 case BT_CLOSED:
1032 break;
1033 case BT_LISTEN:
1034 l2cap_sock_cleanup_listen(sk);
1035 sk->sk_state = BT_CLOSED;
1036 chan->state = BT_CLOSED;
1037
1038 break;
1039 default:
1040 sk->sk_state = BT_CLOSED;
1041 chan->state = BT_CLOSED;
1042
1043 sk->sk_err = err;
1044
1045 if (parent) {
1046 bt_accept_unlink(sk);
1047 parent->sk_data_ready(parent, 0);
1048 } else {
1049 sk->sk_state_change(sk);
1050 }
1051
1052 break;
1053 }
1054
1055 release_sock(sk);
1056}
1057
Gustavo Padovan80b98022012-05-27 22:27:51 -03001058static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001059{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001060 struct sock *sk = chan->data;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001061
1062 sk->sk_state = state;
1063}
1064
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001065static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
Gustavo Padovan90338942012-04-06 20:15:47 -03001066 unsigned long len, int nb)
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001067{
Gustavo Padovan90338942012-04-06 20:15:47 -03001068 struct sk_buff *skb;
1069 int err;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001070
Mat Martineaua6a55682012-05-04 14:20:31 -07001071 l2cap_chan_unlock(chan);
Gustavo Padovan90338942012-04-06 20:15:47 -03001072 skb = bt_skb_send_alloc(chan->sk, len, nb, &err);
Mat Martineaua6a55682012-05-04 14:20:31 -07001073 l2cap_chan_lock(chan);
1074
Gustavo Padovan90338942012-04-06 20:15:47 -03001075 if (!skb)
1076 return ERR_PTR(err);
1077
1078 return skb;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001079}
1080
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001081static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
1082{
1083 struct sock *sk = chan->data;
1084 struct sock *parent;
1085
1086 lock_sock(sk);
1087
1088 parent = bt_sk(sk)->parent;
1089
1090 BT_DBG("sk %p, parent %p", sk, parent);
1091
1092 sk->sk_state = BT_CONNECTED;
1093 sk->sk_state_change(sk);
1094
1095 if (parent)
1096 parent->sk_data_ready(parent, 0);
1097
1098 release_sock(sk);
1099}
1100
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001101static void l2cap_sock_defer_cb(struct l2cap_chan *chan)
1102{
1103 struct sock *sk = chan->data;
1104 struct sock *parent = bt_sk(sk)->parent;
1105
1106 if (parent)
1107 parent->sk_data_ready(parent, 0);
1108}
1109
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001110static struct l2cap_ops l2cap_chan_ops = {
1111 .name = "L2CAP Socket Interface",
1112 .new_connection = l2cap_sock_new_connection_cb,
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001113 .recv = l2cap_sock_recv_cb,
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001114 .close = l2cap_sock_close_cb,
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001115 .teardown = l2cap_sock_teardown_cb,
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001116 .state_change = l2cap_sock_state_change_cb,
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001117 .ready = l2cap_sock_ready_cb,
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001118 .defer = l2cap_sock_defer_cb,
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001119 .alloc_skb = l2cap_sock_alloc_skb_cb,
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001120};
1121
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001122static void l2cap_sock_destruct(struct sock *sk)
1123{
1124 BT_DBG("sk %p", sk);
1125
Sasha Levin23d3a862012-10-08 16:48:32 -04001126 if (l2cap_pi(sk)->chan)
1127 l2cap_chan_put(l2cap_pi(sk)->chan);
Mat Martineaue3281402011-07-07 09:39:02 -07001128 if (l2cap_pi(sk)->rx_busy_skb) {
1129 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
1130 l2cap_pi(sk)->rx_busy_skb = NULL;
1131 }
1132
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001133 skb_queue_purge(&sk->sk_receive_queue);
1134 skb_queue_purge(&sk->sk_write_queue);
1135}
1136
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001137static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001138{
1139 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001140 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001141
1142 BT_DBG("sk %p", sk);
1143
1144 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001145 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
1146
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001147 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -03001148 bt_sk(sk)->flags = bt_sk(parent)->flags;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001149
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001150 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001151 chan->imtu = pchan->imtu;
1152 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001153 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001154 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001155 chan->fcs = pchan->fcs;
1156 chan->max_tx = pchan->max_tx;
1157 chan->tx_win = pchan->tx_win;
Andrei Emeltchenko6b3c7102011-11-02 09:57:10 +02001158 chan->tx_win_max = pchan->tx_win_max;
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001159 chan->sec_level = pchan->sec_level;
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +03001160 chan->flags = pchan->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +00001161
1162 security_sk_clone(parent, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001163 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001164
1165 switch (sk->sk_type) {
1166 case SOCK_RAW:
1167 chan->chan_type = L2CAP_CHAN_RAW;
1168 break;
1169 case SOCK_DGRAM:
1170 chan->chan_type = L2CAP_CHAN_CONN_LESS;
1171 break;
1172 case SOCK_SEQPACKET:
1173 case SOCK_STREAM:
1174 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
1175 break;
1176 }
1177
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001178 chan->imtu = L2CAP_DEFAULT_MTU;
1179 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001180 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001181 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001182 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001183 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001184 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001185 }
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +03001186
1187 l2cap_chan_set_defaults(chan);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001188 }
1189
1190 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001191 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001192
1193 chan->data = sk;
1194 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001195}
1196
1197static struct proto l2cap_proto = {
1198 .name = "L2CAP",
1199 .owner = THIS_MODULE,
1200 .obj_size = sizeof(struct l2cap_pinfo)
1201};
1202
Gustavo Padovan2d792812012-10-06 10:07:01 +01001203static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
1204 int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001205{
1206 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001207 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001208
1209 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1210 if (!sk)
1211 return NULL;
1212
1213 sock_init_data(sock, sk);
1214 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1215
1216 sk->sk_destruct = l2cap_sock_destruct;
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08001217 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001218
1219 sock_reset_flag(sk, SOCK_ZAPPED);
1220
1221 sk->sk_protocol = proto;
1222 sk->sk_state = BT_OPEN;
1223
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001224 chan = l2cap_chan_create();
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001225 if (!chan) {
Jaganath Kanakkassery49dfbb92012-07-19 12:54:04 +05301226 sk_free(sk);
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001227 return NULL;
1228 }
1229
Mat Martineau61d6ef32012-04-27 16:50:50 -07001230 l2cap_chan_hold(chan);
1231
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001232 chan->sk = sk;
1233
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001234 l2cap_pi(sk)->chan = chan;
1235
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001236 return sk;
1237}
1238
1239static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1240 int kern)
1241{
1242 struct sock *sk;
1243
1244 BT_DBG("sock %p", sock);
1245
1246 sock->state = SS_UNCONNECTED;
1247
1248 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01001249 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001250 return -ESOCKTNOSUPPORT;
1251
1252 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1253 return -EPERM;
1254
1255 sock->ops = &l2cap_sock_ops;
1256
1257 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1258 if (!sk)
1259 return -ENOMEM;
1260
1261 l2cap_sock_init(sk, NULL);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001262 bt_sock_link(&l2cap_sk_list, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001263 return 0;
1264}
1265
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -03001266static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001267 .family = PF_BLUETOOTH,
1268 .owner = THIS_MODULE,
1269 .release = l2cap_sock_release,
1270 .bind = l2cap_sock_bind,
1271 .connect = l2cap_sock_connect,
1272 .listen = l2cap_sock_listen,
1273 .accept = l2cap_sock_accept,
1274 .getname = l2cap_sock_getname,
1275 .sendmsg = l2cap_sock_sendmsg,
1276 .recvmsg = l2cap_sock_recvmsg,
1277 .poll = bt_sock_poll,
1278 .ioctl = bt_sock_ioctl,
1279 .mmap = sock_no_mmap,
1280 .socketpair = sock_no_socketpair,
1281 .shutdown = l2cap_sock_shutdown,
1282 .setsockopt = l2cap_sock_setsockopt,
1283 .getsockopt = l2cap_sock_getsockopt
1284};
1285
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001286static const struct net_proto_family l2cap_sock_family_ops = {
1287 .family = PF_BLUETOOTH,
1288 .owner = THIS_MODULE,
1289 .create = l2cap_sock_create,
1290};
1291
1292int __init l2cap_init_sockets(void)
1293{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001294 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001295
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001296 err = proto_register(&l2cap_proto, 0);
1297 if (err < 0)
1298 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001299
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001300 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001301 if (err < 0) {
1302 BT_ERR("L2CAP socket registration failed");
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001303 goto error;
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001304 }
1305
Al Virob0316612013-04-04 19:14:33 -04001306 err = bt_procfs_init(&init_net, "l2cap", &l2cap_sk_list,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001307 NULL);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001308 if (err < 0) {
1309 BT_ERR("Failed to create L2CAP proc file");
1310 bt_sock_unregister(BTPROTO_L2CAP);
1311 goto error;
1312 }
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001313
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001314 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001315
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001316 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001317
1318error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001319 proto_unregister(&l2cap_proto);
1320 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001321}
1322
1323void l2cap_cleanup_sockets(void)
1324{
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001325 bt_procfs_cleanup(&init_net, "l2cap");
David Herrmann5e9d7f82013-02-24 19:36:51 +01001326 bt_sock_unregister(BTPROTO_L2CAP);
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001327 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001328}