blob: 1bcfb8422fdcf55096e9b4131e07eab63d5c365c [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
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020046static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
47{
48 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -030049 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020050 struct sockaddr_l2 la;
51 int len, err = 0;
52
53 BT_DBG("sk %p", sk);
54
55 if (!addr || addr->sa_family != AF_BLUETOOTH)
56 return -EINVAL;
57
58 memset(&la, 0, sizeof(la));
59 len = min_t(unsigned int, sizeof(la), alen);
60 memcpy(&la, addr, len);
61
Ville Tervob62f3282011-02-10 22:38:50 -030062 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020063 return -EINVAL;
64
65 lock_sock(sk);
66
67 if (sk->sk_state != BT_OPEN) {
68 err = -EBADFD;
69 goto done;
70 }
71
72 if (la.l2_psm) {
73 __u16 psm = __le16_to_cpu(la.l2_psm);
74
75 /* PSM must be odd and lsb of upper byte must be 0 */
76 if ((psm & 0x0101) != 0x0001) {
77 err = -EINVAL;
78 goto done;
79 }
80
81 /* Restrict usage of well-known PSMs */
82 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
83 err = -EACCES;
84 goto done;
85 }
86 }
87
Ville Tervob62f3282011-02-10 22:38:50 -030088 if (la.l2_cid)
Santosh Nayak6e4aff12012-03-01 22:46:36 +053089 err = l2cap_add_scid(chan, __le16_to_cpu(la.l2_cid));
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030090 else
91 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
Ville Tervob62f3282011-02-10 22:38:50 -030092
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030093 if (err < 0)
94 goto done;
95
Andrei Emeltchenko2983fd62012-05-24 15:42:50 +030096 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP ||
97 __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030098 chan->sec_level = BT_SECURITY_SDP;
99
100 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300101
102 chan->state = BT_BOUND;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300103 sk->sk_state = BT_BOUND;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200104
105done:
106 release_sock(sk);
107 return err;
108}
109
Gustavo Padovan2d792812012-10-06 10:07:01 +0100110static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr,
111 int alen, int flags)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200112{
113 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300114 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200115 struct sockaddr_l2 la;
116 int len, err = 0;
117
118 BT_DBG("sk %p", sk);
119
120 if (!addr || alen < sizeof(addr->sa_family) ||
121 addr->sa_family != AF_BLUETOOTH)
122 return -EINVAL;
123
124 memset(&la, 0, sizeof(la));
125 len = min_t(unsigned int, sizeof(la), alen);
126 memcpy(&la, addr, len);
127
Ville Tervoacd7d372011-02-10 22:38:49 -0300128 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200129 return -EINVAL;
130
Santosh Nayak6e4aff12012-03-01 22:46:36 +0530131 err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid),
Andre Guedes8e9f9892012-04-24 21:02:55 -0300132 &la.l2_bdaddr, la.l2_bdaddr_type);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200133 if (err)
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200134 return err;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200135
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200136 lock_sock(sk);
137
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200138 err = bt_sock_wait_state(sk, BT_CONNECTED,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100139 sock_sndtimeo(sk, flags & O_NONBLOCK));
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200140
141 release_sock(sk);
142
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200143 return err;
144}
145
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200146static int l2cap_sock_listen(struct socket *sock, int backlog)
147{
148 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300149 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200150 int err = 0;
151
152 BT_DBG("sk %p backlog %d", sk, backlog);
153
154 lock_sock(sk);
155
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200156 if (sk->sk_state != BT_BOUND) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200157 err = -EBADFD;
158 goto done;
159 }
160
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200161 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) {
162 err = -EINVAL;
163 goto done;
164 }
165
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300166 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200167 case L2CAP_MODE_BASIC:
168 break;
169 case L2CAP_MODE_ERTM:
170 case L2CAP_MODE_STREAMING:
171 if (!disable_ertm)
172 break;
173 /* fall through */
174 default:
175 err = -ENOTSUPP;
176 goto done;
177 }
178
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200179 sk->sk_max_ack_backlog = backlog;
180 sk->sk_ack_backlog = 0;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300181
182 chan->state = BT_LISTEN;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200183 sk->sk_state = BT_LISTEN;
184
185done:
186 release_sock(sk);
187 return err;
188}
189
Gustavo Padovan2d792812012-10-06 10:07:01 +0100190static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
191 int flags)
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200192{
193 DECLARE_WAITQUEUE(wait, current);
194 struct sock *sk = sock->sk, *nsk;
195 long timeo;
196 int err = 0;
197
198 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
199
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200200 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
201
202 BT_DBG("sk %p timeo %ld", sk, timeo);
203
204 /* Wait for an incoming connection. (wake-one). */
205 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400206 while (1) {
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200207 set_current_state(TASK_INTERRUPTIBLE);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200208
209 if (sk->sk_state != BT_LISTEN) {
210 err = -EBADFD;
211 break;
212 }
213
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400214 nsk = bt_accept_dequeue(sk, newsock);
215 if (nsk)
216 break;
217
218 if (!timeo) {
219 err = -EAGAIN;
220 break;
221 }
222
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200223 if (signal_pending(current)) {
224 err = sock_intr_errno(timeo);
225 break;
226 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400227
228 release_sock(sk);
229 timeo = schedule_timeout(timeo);
230 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200231 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400232 __set_current_state(TASK_RUNNING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200233 remove_wait_queue(sk_sleep(sk), &wait);
234
235 if (err)
236 goto done;
237
238 newsock->state = SS_CONNECTED;
239
240 BT_DBG("new socket %p", nsk);
241
242done:
243 release_sock(sk);
244 return err;
245}
246
Gustavo Padovan2d792812012-10-06 10:07:01 +0100247static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr,
248 int *len, int peer)
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200249{
250 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
251 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300252 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200253
254 BT_DBG("sock %p, sk %p", sock, sk);
255
Mathias Krause792039c2012-08-15 11:31:51 +0000256 memset(la, 0, sizeof(struct sockaddr_l2));
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200257 addr->sa_family = AF_BLUETOOTH;
258 *len = sizeof(struct sockaddr_l2);
259
260 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300261 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200262 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300263 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200264 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300265 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200266 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300267 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200268 }
269
270 return 0;
271}
272
Gustavo Padovan2d792812012-10-06 10:07:01 +0100273static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
274 char __user *optval, int __user *optlen)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200275{
276 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300277 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200278 struct l2cap_options opts;
279 struct l2cap_conninfo cinfo;
280 int len, err = 0;
281 u32 opt;
282
283 BT_DBG("sk %p", sk);
284
285 if (get_user(len, optlen))
286 return -EFAULT;
287
288 lock_sock(sk);
289
290 switch (optname) {
291 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300292 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300293 opts.imtu = chan->imtu;
294 opts.omtu = chan->omtu;
295 opts.flush_to = chan->flush_to;
296 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300297 opts.fcs = chan->fcs;
298 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300299 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200300
301 len = min_t(unsigned int, len, sizeof(opts));
302 if (copy_to_user(optval, (char *) &opts, len))
303 err = -EFAULT;
304
305 break;
306
307 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300308 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200309 case BT_SECURITY_LOW:
310 opt = L2CAP_LM_AUTH;
311 break;
312 case BT_SECURITY_MEDIUM:
313 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
314 break;
315 case BT_SECURITY_HIGH:
316 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
Gustavo Padovan2d792812012-10-06 10:07:01 +0100317 L2CAP_LM_SECURE;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200318 break;
319 default:
320 opt = 0;
321 break;
322 }
323
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300324 if (test_bit(FLAG_ROLE_SWITCH, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200325 opt |= L2CAP_LM_MASTER;
326
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300327 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200328 opt |= L2CAP_LM_RELIABLE;
329
330 if (put_user(opt, (u32 __user *) optval))
331 err = -EFAULT;
332 break;
333
334 case L2CAP_CONNINFO:
335 if (sk->sk_state != BT_CONNECTED &&
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300336 !(sk->sk_state == BT_CONNECT2 &&
337 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200338 err = -ENOTCONN;
339 break;
340 }
341
Filip Palian8d03e972011-05-12 19:32:46 +0200342 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300343 cinfo.hci_handle = chan->conn->hcon->handle;
344 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200345
346 len = min_t(unsigned int, len, sizeof(cinfo));
347 if (copy_to_user(optval, (char *) &cinfo, len))
348 err = -EFAULT;
349
350 break;
351
352 default:
353 err = -ENOPROTOOPT;
354 break;
355 }
356
357 release_sock(sk);
358 return err;
359}
360
Gustavo Padovan2d792812012-10-06 10:07:01 +0100361static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
362 char __user *optval, int __user *optlen)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200363{
364 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300365 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200366 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700367 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200368 int len, err = 0;
369
370 BT_DBG("sk %p", sk);
371
372 if (level == SOL_L2CAP)
373 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
374
375 if (level != SOL_BLUETOOTH)
376 return -ENOPROTOOPT;
377
378 if (get_user(len, optlen))
379 return -EFAULT;
380
381 lock_sock(sk);
382
383 switch (optname) {
384 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300385 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100386 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200387 err = -EINVAL;
388 break;
389 }
390
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300391 memset(&sec, 0, sizeof(sec));
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300392 if (chan->conn) {
Gustavo Padovanc6585a42012-05-07 03:07:26 -0300393 sec.level = chan->conn->hcon->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200394
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300395 if (sk->sk_state == BT_CONNECTED)
396 sec.key_size = chan->conn->hcon->enc_key_size;
397 } else {
398 sec.level = chan->sec_level;
399 }
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300400
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200401 len = min_t(unsigned int, len, sizeof(sec));
402 if (copy_to_user(optval, (char *) &sec, len))
403 err = -EFAULT;
404
405 break;
406
407 case BT_DEFER_SETUP:
408 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
409 err = -EINVAL;
410 break;
411 }
412
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300413 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
414 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200415 err = -EFAULT;
416
417 break;
418
419 case BT_FLUSHABLE:
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300420 if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags),
Gustavo Padovan2d792812012-10-06 10:07:01 +0100421 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200422 err = -EFAULT;
423
424 break;
425
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700426 case BT_POWER:
427 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
Gustavo Padovan2d792812012-10-06 10:07:01 +0100428 && sk->sk_type != SOCK_RAW) {
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700429 err = -EINVAL;
430 break;
431 }
432
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300433 pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700434
435 len = min_t(unsigned int, len, sizeof(pwr));
436 if (copy_to_user(optval, (char *) &pwr, len))
437 err = -EFAULT;
438
439 break;
440
Mat Martineau2ea66482011-11-02 16:18:30 -0700441 case BT_CHANNEL_POLICY:
442 if (!enable_hs) {
443 err = -ENOPROTOOPT;
444 break;
445 }
446
447 if (put_user(chan->chan_policy, (u32 __user *) optval))
448 err = -EFAULT;
449 break;
450
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200451 default:
452 err = -ENOPROTOOPT;
453 break;
454 }
455
456 release_sock(sk);
457 return err;
458}
459
Andre Guedes682877c2012-05-31 17:01:34 -0300460static bool l2cap_valid_mtu(struct l2cap_chan *chan, u16 mtu)
461{
462 switch (chan->scid) {
463 case L2CAP_CID_LE_DATA:
Andre Guedes8c3a4f02012-05-31 17:01:35 -0300464 if (mtu < L2CAP_LE_MIN_MTU)
Andre Guedes682877c2012-05-31 17:01:34 -0300465 return false;
466 break;
467
468 default:
469 if (mtu < L2CAP_DEFAULT_MIN_MTU)
470 return false;
471 }
472
473 return true;
474}
475
Gustavo Padovan2d792812012-10-06 10:07:01 +0100476static int l2cap_sock_setsockopt_old(struct socket *sock, int optname,
477 char __user *optval, unsigned int optlen)
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200478{
479 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300480 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200481 struct l2cap_options opts;
482 int len, err = 0;
483 u32 opt;
484
485 BT_DBG("sk %p", sk);
486
487 lock_sock(sk);
488
489 switch (optname) {
490 case L2CAP_OPTIONS:
491 if (sk->sk_state == BT_CONNECTED) {
492 err = -EINVAL;
493 break;
494 }
495
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300496 opts.imtu = chan->imtu;
497 opts.omtu = chan->omtu;
498 opts.flush_to = chan->flush_to;
499 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300500 opts.fcs = chan->fcs;
501 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300502 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200503
504 len = min_t(unsigned int, sizeof(opts), optlen);
505 if (copy_from_user((char *) &opts, optval, len)) {
506 err = -EFAULT;
507 break;
508 }
509
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300510 if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200511 err = -EINVAL;
512 break;
513 }
514
Andre Guedes682877c2012-05-31 17:01:34 -0300515 if (!l2cap_valid_mtu(chan, opts.imtu)) {
516 err = -EINVAL;
517 break;
518 }
519
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300520 chan->mode = opts.mode;
521 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200522 case L2CAP_MODE_BASIC:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -0300523 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200524 break;
525 case L2CAP_MODE_ERTM:
526 case L2CAP_MODE_STREAMING:
527 if (!disable_ertm)
528 break;
529 /* fall through */
530 default:
531 err = -EINVAL;
532 break;
533 }
534
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300535 chan->imtu = opts.imtu;
536 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300537 chan->fcs = opts.fcs;
538 chan->max_tx = opts.max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300539 chan->tx_win = opts.txwin_size;
Andrei Emeltchenko12d59782012-10-10 17:38:26 +0300540 chan->flush_to = opts.flush_to;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200541 break;
542
543 case L2CAP_LM:
544 if (get_user(opt, (u32 __user *) optval)) {
545 err = -EFAULT;
546 break;
547 }
548
549 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300550 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200551 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300552 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200553 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300554 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200555
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300556 if (opt & L2CAP_LM_MASTER)
557 set_bit(FLAG_ROLE_SWITCH, &chan->flags);
558 else
559 clear_bit(FLAG_ROLE_SWITCH, &chan->flags);
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300560
561 if (opt & L2CAP_LM_RELIABLE)
562 set_bit(FLAG_FORCE_RELIABLE, &chan->flags);
563 else
564 clear_bit(FLAG_FORCE_RELIABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200565 break;
566
567 default:
568 err = -ENOPROTOOPT;
569 break;
570 }
571
572 release_sock(sk);
573 return err;
574}
575
Gustavo Padovan2d792812012-10-06 10:07:01 +0100576static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
577 char __user *optval, unsigned int optlen)
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200578{
579 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300580 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200581 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700582 struct bt_power pwr;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300583 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200584 int len, err = 0;
585 u32 opt;
586
587 BT_DBG("sk %p", sk);
588
589 if (level == SOL_L2CAP)
590 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
591
592 if (level != SOL_BLUETOOTH)
593 return -ENOPROTOOPT;
594
595 lock_sock(sk);
596
597 switch (optname) {
598 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300599 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100600 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200601 err = -EINVAL;
602 break;
603 }
604
605 sec.level = BT_SECURITY_LOW;
606
607 len = min_t(unsigned int, sizeof(sec), optlen);
608 if (copy_from_user((char *) &sec, optval, len)) {
609 err = -EFAULT;
610 break;
611 }
612
613 if (sec.level < BT_SECURITY_LOW ||
Gustavo Padovan2d792812012-10-06 10:07:01 +0100614 sec.level > BT_SECURITY_HIGH) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200615 err = -EINVAL;
616 break;
617 }
618
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300619 chan->sec_level = sec.level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300620
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200621 if (!chan->conn)
622 break;
623
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300624 conn = chan->conn;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200625
626 /*change security for LE channels */
627 if (chan->scid == L2CAP_CID_LE_DATA) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300628 if (!conn->hcon->out) {
629 err = -EINVAL;
630 break;
631 }
632
Vinicius Costa Gomescc110922012-08-23 21:32:43 -0300633 if (smp_conn_security(conn->hcon, sec.level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300634 break;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300635 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200636 chan->state = BT_CONFIG;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200637
Gustavo Padovana7d77232012-05-13 03:20:07 -0300638 /* or for ACL link */
639 } else if ((sk->sk_state == BT_CONNECT2 &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100640 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) ||
Gustavo Padovana7d77232012-05-13 03:20:07 -0300641 sk->sk_state == BT_CONNECTED) {
642 if (!l2cap_chan_check_security(chan))
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300643 set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
Gustavo Padovana7d77232012-05-13 03:20:07 -0300644 else
645 sk->sk_state_change(sk);
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200646 } else {
647 err = -EINVAL;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300648 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200649 break;
650
651 case BT_DEFER_SETUP:
652 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
653 err = -EINVAL;
654 break;
655 }
656
657 if (get_user(opt, (u32 __user *) optval)) {
658 err = -EFAULT;
659 break;
660 }
661
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300662 if (opt)
663 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
664 else
665 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200666 break;
667
668 case BT_FLUSHABLE:
669 if (get_user(opt, (u32 __user *) optval)) {
670 err = -EFAULT;
671 break;
672 }
673
674 if (opt > BT_FLUSHABLE_ON) {
675 err = -EINVAL;
676 break;
677 }
678
679 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300680 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300681 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200682 No Flush support in the LM */
683 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
684 err = -EINVAL;
685 break;
686 }
687 }
688
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300689 if (opt)
690 set_bit(FLAG_FLUSHABLE, &chan->flags);
691 else
692 clear_bit(FLAG_FLUSHABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200693 break;
694
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700695 case BT_POWER:
696 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100697 chan->chan_type != L2CAP_CHAN_RAW) {
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700698 err = -EINVAL;
699 break;
700 }
701
702 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
703
704 len = min_t(unsigned int, sizeof(pwr), optlen);
705 if (copy_from_user((char *) &pwr, optval, len)) {
706 err = -EFAULT;
707 break;
708 }
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300709
710 if (pwr.force_active)
711 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
712 else
713 clear_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700714 break;
715
Mat Martineau2ea66482011-11-02 16:18:30 -0700716 case BT_CHANNEL_POLICY:
717 if (!enable_hs) {
718 err = -ENOPROTOOPT;
719 break;
720 }
721
722 if (get_user(opt, (u32 __user *) optval)) {
723 err = -EFAULT;
724 break;
725 }
726
727 if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) {
728 err = -EINVAL;
729 break;
730 }
731
732 if (chan->mode != L2CAP_MODE_ERTM &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100733 chan->mode != L2CAP_MODE_STREAMING) {
Mat Martineau2ea66482011-11-02 16:18:30 -0700734 err = -EOPNOTSUPP;
735 break;
736 }
737
738 chan->chan_policy = (u8) opt;
Mat Martineau3f7a56c2012-10-23 15:24:23 -0700739
740 if (sk->sk_state == BT_CONNECTED &&
741 chan->move_role == L2CAP_MOVE_ROLE_NONE)
742 l2cap_move_start(chan);
743
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200744 break;
745
746 default:
747 err = -ENOPROTOOPT;
748 break;
749 }
750
751 release_sock(sk);
752 return err;
753}
754
Gustavo Padovan2d792812012-10-06 10:07:01 +0100755static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
756 struct msghdr *msg, size_t len)
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200757{
758 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300759 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200760 int err;
761
762 BT_DBG("sock %p, sk %p", sock, sk);
763
764 err = sock_error(sk);
765 if (err)
766 return err;
767
768 if (msg->msg_flags & MSG_OOB)
769 return -EOPNOTSUPP;
770
Mat Martineaua6a55682012-05-04 14:20:31 -0700771 if (sk->sk_state != BT_CONNECTED)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300772 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200773
Mat Martineaua6a55682012-05-04 14:20:31 -0700774 l2cap_chan_lock(chan);
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200775 err = l2cap_chan_send(chan, msg, len, sk->sk_priority);
Mat Martineaua6a55682012-05-04 14:20:31 -0700776 l2cap_chan_unlock(chan);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200777
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200778 return err;
779}
780
Gustavo Padovan2d792812012-10-06 10:07:01 +0100781static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
782 struct msghdr *msg, size_t len, int flags)
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200783{
784 struct sock *sk = sock->sk;
Mat Martineaue3281402011-07-07 09:39:02 -0700785 struct l2cap_pinfo *pi = l2cap_pi(sk);
786 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200787
788 lock_sock(sk);
789
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300790 if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
791 &bt_sk(sk)->flags)) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300792 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200793 pi->chan->state = BT_CONFIG;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300794
Mat Martineaue3281402011-07-07 09:39:02 -0700795 __l2cap_connect_rsp_defer(pi->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200796 release_sock(sk);
797 return 0;
798 }
799
800 release_sock(sk);
801
802 if (sock->type == SOCK_STREAM)
Mat Martineaue3281402011-07-07 09:39:02 -0700803 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
804 else
805 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200806
Mat Martineaue3281402011-07-07 09:39:02 -0700807 if (pi->chan->mode != L2CAP_MODE_ERTM)
808 return err;
809
810 /* Attempt to put pending rx data in the socket buffer */
811
812 lock_sock(sk);
813
814 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
815 goto done;
816
817 if (pi->rx_busy_skb) {
818 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
819 pi->rx_busy_skb = NULL;
820 else
821 goto done;
822 }
823
824 /* Restore data flow when half of the receive buffer is
825 * available. This avoids resending large numbers of
826 * frames.
827 */
828 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
829 l2cap_chan_busy(pi->chan, 0);
830
831done:
832 release_sock(sk);
833 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200834}
835
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200836/* Kill socket (only if zapped and orphan)
837 * Must be called on unlocked socket.
838 */
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300839static void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200840{
841 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
842 return;
843
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200844 BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200845
846 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300847
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530848 l2cap_chan_put(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200849 sock_set_flag(sk, SOCK_DEAD);
850 sock_put(sk);
851}
852
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200853static int l2cap_sock_shutdown(struct socket *sock, int how)
854{
855 struct sock *sk = sock->sk;
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200856 struct l2cap_chan *chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200857 struct l2cap_conn *conn;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200858 int err = 0;
859
860 BT_DBG("sock %p, sk %p", sock, sk);
861
862 if (!sk)
863 return 0;
864
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200865 chan = l2cap_pi(sk)->chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200866 conn = chan->conn;
867
868 if (conn)
869 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200870
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200871 l2cap_chan_lock(chan);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200872 lock_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200873
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200874 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300875 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200876 err = __l2cap_wait_ack(sk);
877
878 sk->sk_shutdown = SHUTDOWN_MASK;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200879
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200880 release_sock(sk);
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300881 l2cap_chan_close(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200882 lock_sock(sk);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200883
884 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
885 err = bt_sock_wait_state(sk, BT_CLOSED,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100886 sk->sk_lingertime);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200887 }
888
889 if (!err && sk->sk_err)
890 err = -sk->sk_err;
891
892 release_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200893 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200894
895 if (conn)
896 mutex_unlock(&conn->chan_lock);
897
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200898 return err;
899}
900
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200901static int l2cap_sock_release(struct socket *sock)
902{
903 struct sock *sk = sock->sk;
904 int err;
905
906 BT_DBG("sock %p, sk %p", sock, sk);
907
908 if (!sk)
909 return 0;
910
Masatake YAMATO5b28d952012-07-26 01:29:25 +0900911 bt_sock_unlink(&l2cap_sk_list, sk);
912
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200913 err = l2cap_sock_shutdown(sock, 2);
914
915 sock_orphan(sk);
916 l2cap_sock_kill(sk);
917 return err;
918}
919
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -0300920static void l2cap_sock_cleanup_listen(struct sock *parent)
921{
922 struct sock *sk;
923
924 BT_DBG("parent %p", parent);
925
926 /* Close not yet accepted channels */
927 while ((sk = bt_accept_dequeue(parent, NULL))) {
928 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
929
930 l2cap_chan_lock(chan);
931 __clear_chan_timer(chan);
932 l2cap_chan_close(chan, ECONNRESET);
933 l2cap_chan_unlock(chan);
934
935 l2cap_sock_kill(sk);
936 }
937}
938
Gustavo Padovan80b98022012-05-27 22:27:51 -0300939static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300940{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300941 struct sock *sk, *parent = chan->data;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300942
Gustavo Padovan53826692012-05-27 22:27:55 -0300943 /* Check for backlog size */
944 if (sk_acceptq_is_full(parent)) {
945 BT_DBG("backlog full %d", parent->sk_ack_backlog);
946 return NULL;
947 }
948
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300949 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100950 GFP_ATOMIC);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300951 if (!sk)
952 return NULL;
953
Octavian Purdilad22015a2012-01-22 00:28:34 +0200954 bt_sock_reclassify_lock(sk, BTPROTO_L2CAP);
955
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300956 l2cap_sock_init(sk, parent);
957
Gustavo Padovan644912e2012-10-12 19:35:23 +0800958 bt_accept_enqueue(parent, sk);
959
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300960 return l2cap_pi(sk)->chan;
961}
962
Gustavo Padovan80b98022012-05-27 22:27:51 -0300963static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300964{
Mat Martineaue3281402011-07-07 09:39:02 -0700965 int err;
Gustavo Padovan80b98022012-05-27 22:27:51 -0300966 struct sock *sk = chan->data;
Mat Martineaue3281402011-07-07 09:39:02 -0700967 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300968
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200969 lock_sock(sk);
970
971 if (pi->rx_busy_skb) {
972 err = -ENOMEM;
973 goto done;
974 }
Mat Martineaue3281402011-07-07 09:39:02 -0700975
976 err = sock_queue_rcv_skb(sk, skb);
977
978 /* For ERTM, handle one skb that doesn't fit into the recv
979 * buffer. This is important to do because the data frames
980 * have already been acked, so the skb cannot be discarded.
981 *
982 * Notify the l2cap core that the buffer is full, so the
983 * LOCAL_BUSY state is entered and no more frames are
984 * acked and reassembled until there is buffer space
985 * available.
986 */
987 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
988 pi->rx_busy_skb = skb;
989 l2cap_chan_busy(pi->chan, 1);
990 err = 0;
991 }
992
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200993done:
994 release_sock(sk);
995
Mat Martineaue3281402011-07-07 09:39:02 -0700996 return err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300997}
998
Gustavo Padovan80b98022012-05-27 22:27:51 -0300999static void l2cap_sock_close_cb(struct l2cap_chan *chan)
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001000{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001001 struct sock *sk = chan->data;
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001002
1003 l2cap_sock_kill(sk);
1004}
1005
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001006static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
1007{
1008 struct sock *sk = chan->data;
1009 struct sock *parent;
1010
1011 lock_sock(sk);
1012
1013 parent = bt_sk(sk)->parent;
1014
1015 sock_set_flag(sk, SOCK_ZAPPED);
1016
1017 switch (chan->state) {
1018 case BT_OPEN:
1019 case BT_BOUND:
1020 case BT_CLOSED:
1021 break;
1022 case BT_LISTEN:
1023 l2cap_sock_cleanup_listen(sk);
1024 sk->sk_state = BT_CLOSED;
1025 chan->state = BT_CLOSED;
1026
1027 break;
1028 default:
1029 sk->sk_state = BT_CLOSED;
1030 chan->state = BT_CLOSED;
1031
1032 sk->sk_err = err;
1033
1034 if (parent) {
1035 bt_accept_unlink(sk);
1036 parent->sk_data_ready(parent, 0);
1037 } else {
1038 sk->sk_state_change(sk);
1039 }
1040
1041 break;
1042 }
1043
1044 release_sock(sk);
1045}
1046
Gustavo Padovan80b98022012-05-27 22:27:51 -03001047static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001048{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001049 struct sock *sk = chan->data;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001050
1051 sk->sk_state = state;
1052}
1053
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001054static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
Gustavo Padovan90338942012-04-06 20:15:47 -03001055 unsigned long len, int nb)
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001056{
Gustavo Padovan90338942012-04-06 20:15:47 -03001057 struct sk_buff *skb;
1058 int err;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001059
Mat Martineaua6a55682012-05-04 14:20:31 -07001060 l2cap_chan_unlock(chan);
Gustavo Padovan90338942012-04-06 20:15:47 -03001061 skb = bt_skb_send_alloc(chan->sk, len, nb, &err);
Mat Martineaua6a55682012-05-04 14:20:31 -07001062 l2cap_chan_lock(chan);
1063
Gustavo Padovan90338942012-04-06 20:15:47 -03001064 if (!skb)
1065 return ERR_PTR(err);
1066
1067 return skb;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001068}
1069
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001070static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
1071{
1072 struct sock *sk = chan->data;
1073 struct sock *parent;
1074
1075 lock_sock(sk);
1076
1077 parent = bt_sk(sk)->parent;
1078
1079 BT_DBG("sk %p, parent %p", sk, parent);
1080
1081 sk->sk_state = BT_CONNECTED;
1082 sk->sk_state_change(sk);
1083
1084 if (parent)
1085 parent->sk_data_ready(parent, 0);
1086
1087 release_sock(sk);
1088}
1089
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001090static void l2cap_sock_defer_cb(struct l2cap_chan *chan)
1091{
1092 struct sock *sk = chan->data;
1093 struct sock *parent = bt_sk(sk)->parent;
1094
1095 if (parent)
1096 parent->sk_data_ready(parent, 0);
1097}
1098
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001099static struct l2cap_ops l2cap_chan_ops = {
1100 .name = "L2CAP Socket Interface",
1101 .new_connection = l2cap_sock_new_connection_cb,
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001102 .recv = l2cap_sock_recv_cb,
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001103 .close = l2cap_sock_close_cb,
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001104 .teardown = l2cap_sock_teardown_cb,
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001105 .state_change = l2cap_sock_state_change_cb,
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001106 .ready = l2cap_sock_ready_cb,
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001107 .defer = l2cap_sock_defer_cb,
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001108 .alloc_skb = l2cap_sock_alloc_skb_cb,
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001109};
1110
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001111static void l2cap_sock_destruct(struct sock *sk)
1112{
1113 BT_DBG("sk %p", sk);
1114
Sasha Levin23d3a862012-10-08 16:48:32 -04001115 if (l2cap_pi(sk)->chan)
1116 l2cap_chan_put(l2cap_pi(sk)->chan);
Mat Martineaue3281402011-07-07 09:39:02 -07001117 if (l2cap_pi(sk)->rx_busy_skb) {
1118 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
1119 l2cap_pi(sk)->rx_busy_skb = NULL;
1120 }
1121
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001122 skb_queue_purge(&sk->sk_receive_queue);
1123 skb_queue_purge(&sk->sk_write_queue);
1124}
1125
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001126static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001127{
1128 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001129 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001130
1131 BT_DBG("sk %p", sk);
1132
1133 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001134 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
1135
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001136 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -03001137 bt_sk(sk)->flags = bt_sk(parent)->flags;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001138
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001139 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001140 chan->imtu = pchan->imtu;
1141 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001142 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001143 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001144 chan->fcs = pchan->fcs;
1145 chan->max_tx = pchan->max_tx;
1146 chan->tx_win = pchan->tx_win;
Andrei Emeltchenko6b3c7102011-11-02 09:57:10 +02001147 chan->tx_win_max = pchan->tx_win_max;
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001148 chan->sec_level = pchan->sec_level;
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +03001149 chan->flags = pchan->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +00001150
1151 security_sk_clone(parent, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001152 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001153
1154 switch (sk->sk_type) {
1155 case SOCK_RAW:
1156 chan->chan_type = L2CAP_CHAN_RAW;
1157 break;
1158 case SOCK_DGRAM:
1159 chan->chan_type = L2CAP_CHAN_CONN_LESS;
1160 break;
1161 case SOCK_SEQPACKET:
1162 case SOCK_STREAM:
1163 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
1164 break;
1165 }
1166
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001167 chan->imtu = L2CAP_DEFAULT_MTU;
1168 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001169 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001170 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001171 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001172 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001173 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001174 }
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +03001175
1176 l2cap_chan_set_defaults(chan);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001177 }
1178
1179 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001180 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001181
1182 chan->data = sk;
1183 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001184}
1185
1186static struct proto l2cap_proto = {
1187 .name = "L2CAP",
1188 .owner = THIS_MODULE,
1189 .obj_size = sizeof(struct l2cap_pinfo)
1190};
1191
Gustavo Padovan2d792812012-10-06 10:07:01 +01001192static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
1193 int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001194{
1195 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001196 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001197
1198 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1199 if (!sk)
1200 return NULL;
1201
1202 sock_init_data(sock, sk);
1203 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1204
1205 sk->sk_destruct = l2cap_sock_destruct;
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08001206 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001207
1208 sock_reset_flag(sk, SOCK_ZAPPED);
1209
1210 sk->sk_protocol = proto;
1211 sk->sk_state = BT_OPEN;
1212
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001213 chan = l2cap_chan_create();
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001214 if (!chan) {
Jaganath Kanakkassery49dfbb92012-07-19 12:54:04 +05301215 sk_free(sk);
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001216 return NULL;
1217 }
1218
Mat Martineau61d6ef32012-04-27 16:50:50 -07001219 l2cap_chan_hold(chan);
1220
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001221 chan->sk = sk;
1222
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001223 l2cap_pi(sk)->chan = chan;
1224
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001225 return sk;
1226}
1227
1228static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1229 int kern)
1230{
1231 struct sock *sk;
1232
1233 BT_DBG("sock %p", sock);
1234
1235 sock->state = SS_UNCONNECTED;
1236
1237 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01001238 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001239 return -ESOCKTNOSUPPORT;
1240
1241 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1242 return -EPERM;
1243
1244 sock->ops = &l2cap_sock_ops;
1245
1246 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1247 if (!sk)
1248 return -ENOMEM;
1249
1250 l2cap_sock_init(sk, NULL);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001251 bt_sock_link(&l2cap_sk_list, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001252 return 0;
1253}
1254
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -03001255static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001256 .family = PF_BLUETOOTH,
1257 .owner = THIS_MODULE,
1258 .release = l2cap_sock_release,
1259 .bind = l2cap_sock_bind,
1260 .connect = l2cap_sock_connect,
1261 .listen = l2cap_sock_listen,
1262 .accept = l2cap_sock_accept,
1263 .getname = l2cap_sock_getname,
1264 .sendmsg = l2cap_sock_sendmsg,
1265 .recvmsg = l2cap_sock_recvmsg,
1266 .poll = bt_sock_poll,
1267 .ioctl = bt_sock_ioctl,
1268 .mmap = sock_no_mmap,
1269 .socketpair = sock_no_socketpair,
1270 .shutdown = l2cap_sock_shutdown,
1271 .setsockopt = l2cap_sock_setsockopt,
1272 .getsockopt = l2cap_sock_getsockopt
1273};
1274
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001275static const struct net_proto_family l2cap_sock_family_ops = {
1276 .family = PF_BLUETOOTH,
1277 .owner = THIS_MODULE,
1278 .create = l2cap_sock_create,
1279};
1280
1281int __init l2cap_init_sockets(void)
1282{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001283 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001284
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001285 err = proto_register(&l2cap_proto, 0);
1286 if (err < 0)
1287 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001288
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001289 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001290 if (err < 0) {
1291 BT_ERR("L2CAP socket registration failed");
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001292 goto error;
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001293 }
1294
Gustavo Padovan2d792812012-10-06 10:07:01 +01001295 err = bt_procfs_init(THIS_MODULE, &init_net, "l2cap", &l2cap_sk_list,
1296 NULL);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001297 if (err < 0) {
1298 BT_ERR("Failed to create L2CAP proc file");
1299 bt_sock_unregister(BTPROTO_L2CAP);
1300 goto error;
1301 }
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001302
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001303 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001304
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001305 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001306
1307error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001308 proto_unregister(&l2cap_proto);
1309 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001310}
1311
1312void l2cap_cleanup_sockets(void)
1313{
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001314 bt_procfs_cleanup(&init_net, "l2cap");
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001315 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1316 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001317
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001318 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001319}