blob: d5093b853b05306f89cd93e68a0209a114554a73 [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
256 addr->sa_family = AF_BLUETOOTH;
257 *len = sizeof(struct sockaddr_l2);
258
259 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300260 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200261 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300262 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200263 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300264 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200265 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300266 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200267 }
268
269 return 0;
270}
271
Gustavo Padovan2d792812012-10-06 10:07:01 +0100272static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
273 char __user *optval, int __user *optlen)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200274{
275 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300276 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200277 struct l2cap_options opts;
278 struct l2cap_conninfo cinfo;
279 int len, err = 0;
280 u32 opt;
281
282 BT_DBG("sk %p", sk);
283
284 if (get_user(len, optlen))
285 return -EFAULT;
286
287 lock_sock(sk);
288
289 switch (optname) {
290 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300291 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300292 opts.imtu = chan->imtu;
293 opts.omtu = chan->omtu;
294 opts.flush_to = chan->flush_to;
295 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300296 opts.fcs = chan->fcs;
297 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300298 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200299
300 len = min_t(unsigned int, len, sizeof(opts));
301 if (copy_to_user(optval, (char *) &opts, len))
302 err = -EFAULT;
303
304 break;
305
306 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300307 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200308 case BT_SECURITY_LOW:
309 opt = L2CAP_LM_AUTH;
310 break;
311 case BT_SECURITY_MEDIUM:
312 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
313 break;
314 case BT_SECURITY_HIGH:
315 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
Gustavo Padovan2d792812012-10-06 10:07:01 +0100316 L2CAP_LM_SECURE;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200317 break;
318 default:
319 opt = 0;
320 break;
321 }
322
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300323 if (test_bit(FLAG_ROLE_SWITCH, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200324 opt |= L2CAP_LM_MASTER;
325
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300326 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200327 opt |= L2CAP_LM_RELIABLE;
328
329 if (put_user(opt, (u32 __user *) optval))
330 err = -EFAULT;
331 break;
332
333 case L2CAP_CONNINFO:
334 if (sk->sk_state != BT_CONNECTED &&
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300335 !(sk->sk_state == BT_CONNECT2 &&
336 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200337 err = -ENOTCONN;
338 break;
339 }
340
Filip Palian8d03e972011-05-12 19:32:46 +0200341 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300342 cinfo.hci_handle = chan->conn->hcon->handle;
343 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200344
345 len = min_t(unsigned int, len, sizeof(cinfo));
346 if (copy_to_user(optval, (char *) &cinfo, len))
347 err = -EFAULT;
348
349 break;
350
351 default:
352 err = -ENOPROTOOPT;
353 break;
354 }
355
356 release_sock(sk);
357 return err;
358}
359
Gustavo Padovan2d792812012-10-06 10:07:01 +0100360static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
361 char __user *optval, int __user *optlen)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200362{
363 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300364 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200365 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700366 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200367 int len, err = 0;
368
369 BT_DBG("sk %p", sk);
370
371 if (level == SOL_L2CAP)
372 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
373
374 if (level != SOL_BLUETOOTH)
375 return -ENOPROTOOPT;
376
377 if (get_user(len, optlen))
378 return -EFAULT;
379
380 lock_sock(sk);
381
382 switch (optname) {
383 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300384 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100385 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200386 err = -EINVAL;
387 break;
388 }
389
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300390 memset(&sec, 0, sizeof(sec));
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300391 if (chan->conn) {
Gustavo Padovanc6585a42012-05-07 03:07:26 -0300392 sec.level = chan->conn->hcon->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200393
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300394 if (sk->sk_state == BT_CONNECTED)
395 sec.key_size = chan->conn->hcon->enc_key_size;
396 } else {
397 sec.level = chan->sec_level;
398 }
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300399
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200400 len = min_t(unsigned int, len, sizeof(sec));
401 if (copy_to_user(optval, (char *) &sec, len))
402 err = -EFAULT;
403
404 break;
405
406 case BT_DEFER_SETUP:
407 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
408 err = -EINVAL;
409 break;
410 }
411
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300412 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
413 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200414 err = -EFAULT;
415
416 break;
417
418 case BT_FLUSHABLE:
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300419 if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags),
Gustavo Padovan2d792812012-10-06 10:07:01 +0100420 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200421 err = -EFAULT;
422
423 break;
424
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700425 case BT_POWER:
426 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
Gustavo Padovan2d792812012-10-06 10:07:01 +0100427 && sk->sk_type != SOCK_RAW) {
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700428 err = -EINVAL;
429 break;
430 }
431
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300432 pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700433
434 len = min_t(unsigned int, len, sizeof(pwr));
435 if (copy_to_user(optval, (char *) &pwr, len))
436 err = -EFAULT;
437
438 break;
439
Mat Martineau2ea66482011-11-02 16:18:30 -0700440 case BT_CHANNEL_POLICY:
441 if (!enable_hs) {
442 err = -ENOPROTOOPT;
443 break;
444 }
445
446 if (put_user(chan->chan_policy, (u32 __user *) optval))
447 err = -EFAULT;
448 break;
449
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200450 default:
451 err = -ENOPROTOOPT;
452 break;
453 }
454
455 release_sock(sk);
456 return err;
457}
458
Andre Guedes682877c2012-05-31 17:01:34 -0300459static bool l2cap_valid_mtu(struct l2cap_chan *chan, u16 mtu)
460{
461 switch (chan->scid) {
462 case L2CAP_CID_LE_DATA:
Andre Guedes8c3a4f02012-05-31 17:01:35 -0300463 if (mtu < L2CAP_LE_MIN_MTU)
Andre Guedes682877c2012-05-31 17:01:34 -0300464 return false;
465 break;
466
467 default:
468 if (mtu < L2CAP_DEFAULT_MIN_MTU)
469 return false;
470 }
471
472 return true;
473}
474
Gustavo Padovan2d792812012-10-06 10:07:01 +0100475static int l2cap_sock_setsockopt_old(struct socket *sock, int optname,
476 char __user *optval, unsigned int optlen)
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200477{
478 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300479 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200480 struct l2cap_options opts;
481 int len, err = 0;
482 u32 opt;
483
484 BT_DBG("sk %p", sk);
485
486 lock_sock(sk);
487
488 switch (optname) {
489 case L2CAP_OPTIONS:
490 if (sk->sk_state == BT_CONNECTED) {
491 err = -EINVAL;
492 break;
493 }
494
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300495 opts.imtu = chan->imtu;
496 opts.omtu = chan->omtu;
497 opts.flush_to = chan->flush_to;
498 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300499 opts.fcs = chan->fcs;
500 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300501 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200502
503 len = min_t(unsigned int, sizeof(opts), optlen);
504 if (copy_from_user((char *) &opts, optval, len)) {
505 err = -EFAULT;
506 break;
507 }
508
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300509 if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200510 err = -EINVAL;
511 break;
512 }
513
Andre Guedes682877c2012-05-31 17:01:34 -0300514 if (!l2cap_valid_mtu(chan, opts.imtu)) {
515 err = -EINVAL;
516 break;
517 }
518
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300519 chan->mode = opts.mode;
520 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200521 case L2CAP_MODE_BASIC:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -0300522 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200523 break;
524 case L2CAP_MODE_ERTM:
525 case L2CAP_MODE_STREAMING:
526 if (!disable_ertm)
527 break;
528 /* fall through */
529 default:
530 err = -EINVAL;
531 break;
532 }
533
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300534 chan->imtu = opts.imtu;
535 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300536 chan->fcs = opts.fcs;
537 chan->max_tx = opts.max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300538 chan->tx_win = opts.txwin_size;
Andrei Emeltchenko12d59782012-10-10 17:38:26 +0300539 chan->flush_to = opts.flush_to;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200540 break;
541
542 case L2CAP_LM:
543 if (get_user(opt, (u32 __user *) optval)) {
544 err = -EFAULT;
545 break;
546 }
547
548 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300549 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200550 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300551 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200552 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300553 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200554
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300555 if (opt & L2CAP_LM_MASTER)
556 set_bit(FLAG_ROLE_SWITCH, &chan->flags);
557 else
558 clear_bit(FLAG_ROLE_SWITCH, &chan->flags);
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300559
560 if (opt & L2CAP_LM_RELIABLE)
561 set_bit(FLAG_FORCE_RELIABLE, &chan->flags);
562 else
563 clear_bit(FLAG_FORCE_RELIABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200564 break;
565
566 default:
567 err = -ENOPROTOOPT;
568 break;
569 }
570
571 release_sock(sk);
572 return err;
573}
574
Gustavo Padovan2d792812012-10-06 10:07:01 +0100575static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
576 char __user *optval, unsigned int optlen)
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200577{
578 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300579 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200580 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700581 struct bt_power pwr;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300582 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200583 int len, err = 0;
584 u32 opt;
585
586 BT_DBG("sk %p", sk);
587
588 if (level == SOL_L2CAP)
589 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
590
591 if (level != SOL_BLUETOOTH)
592 return -ENOPROTOOPT;
593
594 lock_sock(sk);
595
596 switch (optname) {
597 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300598 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100599 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200600 err = -EINVAL;
601 break;
602 }
603
604 sec.level = BT_SECURITY_LOW;
605
606 len = min_t(unsigned int, sizeof(sec), optlen);
607 if (copy_from_user((char *) &sec, optval, len)) {
608 err = -EFAULT;
609 break;
610 }
611
612 if (sec.level < BT_SECURITY_LOW ||
Gustavo Padovan2d792812012-10-06 10:07:01 +0100613 sec.level > BT_SECURITY_HIGH) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200614 err = -EINVAL;
615 break;
616 }
617
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300618 chan->sec_level = sec.level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300619
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200620 if (!chan->conn)
621 break;
622
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300623 conn = chan->conn;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200624
625 /*change security for LE channels */
626 if (chan->scid == L2CAP_CID_LE_DATA) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300627 if (!conn->hcon->out) {
628 err = -EINVAL;
629 break;
630 }
631
Vinicius Costa Gomescc110922012-08-23 21:32:43 -0300632 if (smp_conn_security(conn->hcon, sec.level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300633 break;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300634 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200635 chan->state = BT_CONFIG;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200636
Gustavo Padovana7d77232012-05-13 03:20:07 -0300637 /* or for ACL link */
638 } else if ((sk->sk_state == BT_CONNECT2 &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100639 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) ||
Gustavo Padovana7d77232012-05-13 03:20:07 -0300640 sk->sk_state == BT_CONNECTED) {
641 if (!l2cap_chan_check_security(chan))
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300642 set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
Gustavo Padovana7d77232012-05-13 03:20:07 -0300643 else
644 sk->sk_state_change(sk);
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200645 } else {
646 err = -EINVAL;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300647 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200648 break;
649
650 case BT_DEFER_SETUP:
651 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
652 err = -EINVAL;
653 break;
654 }
655
656 if (get_user(opt, (u32 __user *) optval)) {
657 err = -EFAULT;
658 break;
659 }
660
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300661 if (opt)
662 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
663 else
664 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200665 break;
666
667 case BT_FLUSHABLE:
668 if (get_user(opt, (u32 __user *) optval)) {
669 err = -EFAULT;
670 break;
671 }
672
673 if (opt > BT_FLUSHABLE_ON) {
674 err = -EINVAL;
675 break;
676 }
677
678 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300679 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300680 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200681 No Flush support in the LM */
682 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
683 err = -EINVAL;
684 break;
685 }
686 }
687
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300688 if (opt)
689 set_bit(FLAG_FLUSHABLE, &chan->flags);
690 else
691 clear_bit(FLAG_FLUSHABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200692 break;
693
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700694 case BT_POWER:
695 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100696 chan->chan_type != L2CAP_CHAN_RAW) {
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700697 err = -EINVAL;
698 break;
699 }
700
701 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
702
703 len = min_t(unsigned int, sizeof(pwr), optlen);
704 if (copy_from_user((char *) &pwr, optval, len)) {
705 err = -EFAULT;
706 break;
707 }
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300708
709 if (pwr.force_active)
710 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
711 else
712 clear_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700713 break;
714
Mat Martineau2ea66482011-11-02 16:18:30 -0700715 case BT_CHANNEL_POLICY:
716 if (!enable_hs) {
717 err = -ENOPROTOOPT;
718 break;
719 }
720
721 if (get_user(opt, (u32 __user *) optval)) {
722 err = -EFAULT;
723 break;
724 }
725
726 if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) {
727 err = -EINVAL;
728 break;
729 }
730
731 if (chan->mode != L2CAP_MODE_ERTM &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100732 chan->mode != L2CAP_MODE_STREAMING) {
Mat Martineau2ea66482011-11-02 16:18:30 -0700733 err = -EOPNOTSUPP;
734 break;
735 }
736
737 chan->chan_policy = (u8) opt;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200738 break;
739
740 default:
741 err = -ENOPROTOOPT;
742 break;
743 }
744
745 release_sock(sk);
746 return err;
747}
748
Gustavo Padovan2d792812012-10-06 10:07:01 +0100749static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
750 struct msghdr *msg, size_t len)
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200751{
752 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300753 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200754 int err;
755
756 BT_DBG("sock %p, sk %p", sock, sk);
757
758 err = sock_error(sk);
759 if (err)
760 return err;
761
762 if (msg->msg_flags & MSG_OOB)
763 return -EOPNOTSUPP;
764
Mat Martineaua6a55682012-05-04 14:20:31 -0700765 if (sk->sk_state != BT_CONNECTED)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300766 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200767
Mat Martineaua6a55682012-05-04 14:20:31 -0700768 l2cap_chan_lock(chan);
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200769 err = l2cap_chan_send(chan, msg, len, sk->sk_priority);
Mat Martineaua6a55682012-05-04 14:20:31 -0700770 l2cap_chan_unlock(chan);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200771
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200772 return err;
773}
774
Gustavo Padovan2d792812012-10-06 10:07:01 +0100775static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
776 struct msghdr *msg, size_t len, int flags)
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200777{
778 struct sock *sk = sock->sk;
Mat Martineaue3281402011-07-07 09:39:02 -0700779 struct l2cap_pinfo *pi = l2cap_pi(sk);
780 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200781
782 lock_sock(sk);
783
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300784 if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
785 &bt_sk(sk)->flags)) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300786 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200787 pi->chan->state = BT_CONFIG;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300788
Mat Martineaue3281402011-07-07 09:39:02 -0700789 __l2cap_connect_rsp_defer(pi->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200790 release_sock(sk);
791 return 0;
792 }
793
794 release_sock(sk);
795
796 if (sock->type == SOCK_STREAM)
Mat Martineaue3281402011-07-07 09:39:02 -0700797 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
798 else
799 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200800
Mat Martineaue3281402011-07-07 09:39:02 -0700801 if (pi->chan->mode != L2CAP_MODE_ERTM)
802 return err;
803
804 /* Attempt to put pending rx data in the socket buffer */
805
806 lock_sock(sk);
807
808 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
809 goto done;
810
811 if (pi->rx_busy_skb) {
812 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
813 pi->rx_busy_skb = NULL;
814 else
815 goto done;
816 }
817
818 /* Restore data flow when half of the receive buffer is
819 * available. This avoids resending large numbers of
820 * frames.
821 */
822 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
823 l2cap_chan_busy(pi->chan, 0);
824
825done:
826 release_sock(sk);
827 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200828}
829
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200830/* Kill socket (only if zapped and orphan)
831 * Must be called on unlocked socket.
832 */
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300833static void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200834{
835 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
836 return;
837
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200838 BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200839
840 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300841
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530842 l2cap_chan_put(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200843 sock_set_flag(sk, SOCK_DEAD);
844 sock_put(sk);
845}
846
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200847static int l2cap_sock_shutdown(struct socket *sock, int how)
848{
849 struct sock *sk = sock->sk;
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200850 struct l2cap_chan *chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200851 struct l2cap_conn *conn;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200852 int err = 0;
853
854 BT_DBG("sock %p, sk %p", sock, sk);
855
856 if (!sk)
857 return 0;
858
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200859 chan = l2cap_pi(sk)->chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200860 conn = chan->conn;
861
862 if (conn)
863 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200864
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200865 l2cap_chan_lock(chan);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200866 lock_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200867
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200868 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300869 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200870 err = __l2cap_wait_ack(sk);
871
872 sk->sk_shutdown = SHUTDOWN_MASK;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200873
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200874 release_sock(sk);
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300875 l2cap_chan_close(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200876 lock_sock(sk);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200877
878 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
879 err = bt_sock_wait_state(sk, BT_CLOSED,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100880 sk->sk_lingertime);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200881 }
882
883 if (!err && sk->sk_err)
884 err = -sk->sk_err;
885
886 release_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200887 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200888
889 if (conn)
890 mutex_unlock(&conn->chan_lock);
891
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200892 return err;
893}
894
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200895static int l2cap_sock_release(struct socket *sock)
896{
897 struct sock *sk = sock->sk;
898 int err;
899
900 BT_DBG("sock %p, sk %p", sock, sk);
901
902 if (!sk)
903 return 0;
904
Masatake YAMATO5b28d952012-07-26 01:29:25 +0900905 bt_sock_unlink(&l2cap_sk_list, sk);
906
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200907 err = l2cap_sock_shutdown(sock, 2);
908
909 sock_orphan(sk);
910 l2cap_sock_kill(sk);
911 return err;
912}
913
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -0300914static void l2cap_sock_cleanup_listen(struct sock *parent)
915{
916 struct sock *sk;
917
918 BT_DBG("parent %p", parent);
919
920 /* Close not yet accepted channels */
921 while ((sk = bt_accept_dequeue(parent, NULL))) {
922 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
923
924 l2cap_chan_lock(chan);
925 __clear_chan_timer(chan);
926 l2cap_chan_close(chan, ECONNRESET);
927 l2cap_chan_unlock(chan);
928
929 l2cap_sock_kill(sk);
930 }
931}
932
Gustavo Padovan80b98022012-05-27 22:27:51 -0300933static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300934{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300935 struct sock *sk, *parent = chan->data;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300936
Gustavo Padovan53826692012-05-27 22:27:55 -0300937 /* Check for backlog size */
938 if (sk_acceptq_is_full(parent)) {
939 BT_DBG("backlog full %d", parent->sk_ack_backlog);
940 return NULL;
941 }
942
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300943 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100944 GFP_ATOMIC);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300945 if (!sk)
946 return NULL;
947
Octavian Purdilad22015a2012-01-22 00:28:34 +0200948 bt_sock_reclassify_lock(sk, BTPROTO_L2CAP);
949
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300950 l2cap_sock_init(sk, parent);
951
Gustavo Padovan644912e2012-10-12 19:35:23 +0800952 bt_accept_enqueue(parent, sk);
953
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300954 return l2cap_pi(sk)->chan;
955}
956
Gustavo Padovan80b98022012-05-27 22:27:51 -0300957static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300958{
Mat Martineaue3281402011-07-07 09:39:02 -0700959 int err;
Gustavo Padovan80b98022012-05-27 22:27:51 -0300960 struct sock *sk = chan->data;
Mat Martineaue3281402011-07-07 09:39:02 -0700961 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300962
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200963 lock_sock(sk);
964
965 if (pi->rx_busy_skb) {
966 err = -ENOMEM;
967 goto done;
968 }
Mat Martineaue3281402011-07-07 09:39:02 -0700969
970 err = sock_queue_rcv_skb(sk, skb);
971
972 /* For ERTM, handle one skb that doesn't fit into the recv
973 * buffer. This is important to do because the data frames
974 * have already been acked, so the skb cannot be discarded.
975 *
976 * Notify the l2cap core that the buffer is full, so the
977 * LOCAL_BUSY state is entered and no more frames are
978 * acked and reassembled until there is buffer space
979 * available.
980 */
981 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
982 pi->rx_busy_skb = skb;
983 l2cap_chan_busy(pi->chan, 1);
984 err = 0;
985 }
986
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200987done:
988 release_sock(sk);
989
Mat Martineaue3281402011-07-07 09:39:02 -0700990 return err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300991}
992
Gustavo Padovan80b98022012-05-27 22:27:51 -0300993static void l2cap_sock_close_cb(struct l2cap_chan *chan)
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300994{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300995 struct sock *sk = chan->data;
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300996
997 l2cap_sock_kill(sk);
998}
999
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001000static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
1001{
1002 struct sock *sk = chan->data;
1003 struct sock *parent;
1004
1005 lock_sock(sk);
1006
1007 parent = bt_sk(sk)->parent;
1008
1009 sock_set_flag(sk, SOCK_ZAPPED);
1010
1011 switch (chan->state) {
1012 case BT_OPEN:
1013 case BT_BOUND:
1014 case BT_CLOSED:
1015 break;
1016 case BT_LISTEN:
1017 l2cap_sock_cleanup_listen(sk);
1018 sk->sk_state = BT_CLOSED;
1019 chan->state = BT_CLOSED;
1020
1021 break;
1022 default:
1023 sk->sk_state = BT_CLOSED;
1024 chan->state = BT_CLOSED;
1025
1026 sk->sk_err = err;
1027
1028 if (parent) {
1029 bt_accept_unlink(sk);
1030 parent->sk_data_ready(parent, 0);
1031 } else {
1032 sk->sk_state_change(sk);
1033 }
1034
1035 break;
1036 }
1037
1038 release_sock(sk);
1039}
1040
Gustavo Padovan80b98022012-05-27 22:27:51 -03001041static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001042{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001043 struct sock *sk = chan->data;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001044
1045 sk->sk_state = state;
1046}
1047
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001048static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
Gustavo Padovan90338942012-04-06 20:15:47 -03001049 unsigned long len, int nb)
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001050{
Gustavo Padovan90338942012-04-06 20:15:47 -03001051 struct sk_buff *skb;
1052 int err;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001053
Mat Martineaua6a55682012-05-04 14:20:31 -07001054 l2cap_chan_unlock(chan);
Gustavo Padovan90338942012-04-06 20:15:47 -03001055 skb = bt_skb_send_alloc(chan->sk, len, nb, &err);
Mat Martineaua6a55682012-05-04 14:20:31 -07001056 l2cap_chan_lock(chan);
1057
Gustavo Padovan90338942012-04-06 20:15:47 -03001058 if (!skb)
1059 return ERR_PTR(err);
1060
1061 return skb;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001062}
1063
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001064static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
1065{
1066 struct sock *sk = chan->data;
1067 struct sock *parent;
1068
1069 lock_sock(sk);
1070
1071 parent = bt_sk(sk)->parent;
1072
1073 BT_DBG("sk %p, parent %p", sk, parent);
1074
1075 sk->sk_state = BT_CONNECTED;
1076 sk->sk_state_change(sk);
1077
1078 if (parent)
1079 parent->sk_data_ready(parent, 0);
1080
1081 release_sock(sk);
1082}
1083
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001084static struct l2cap_ops l2cap_chan_ops = {
1085 .name = "L2CAP Socket Interface",
1086 .new_connection = l2cap_sock_new_connection_cb,
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001087 .recv = l2cap_sock_recv_cb,
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001088 .close = l2cap_sock_close_cb,
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001089 .teardown = l2cap_sock_teardown_cb,
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001090 .state_change = l2cap_sock_state_change_cb,
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001091 .ready = l2cap_sock_ready_cb,
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001092 .alloc_skb = l2cap_sock_alloc_skb_cb,
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001093};
1094
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001095static void l2cap_sock_destruct(struct sock *sk)
1096{
1097 BT_DBG("sk %p", sk);
1098
Sasha Levin23d3a862012-10-08 16:48:32 -04001099 if (l2cap_pi(sk)->chan)
1100 l2cap_chan_put(l2cap_pi(sk)->chan);
Mat Martineaue3281402011-07-07 09:39:02 -07001101 if (l2cap_pi(sk)->rx_busy_skb) {
1102 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
1103 l2cap_pi(sk)->rx_busy_skb = NULL;
1104 }
1105
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001106 skb_queue_purge(&sk->sk_receive_queue);
1107 skb_queue_purge(&sk->sk_write_queue);
1108}
1109
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001110static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001111{
1112 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001113 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001114
1115 BT_DBG("sk %p", sk);
1116
1117 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001118 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
1119
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001120 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -03001121 bt_sk(sk)->flags = bt_sk(parent)->flags;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001122
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001123 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001124 chan->imtu = pchan->imtu;
1125 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001126 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001127 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001128 chan->fcs = pchan->fcs;
1129 chan->max_tx = pchan->max_tx;
1130 chan->tx_win = pchan->tx_win;
Andrei Emeltchenko6b3c7102011-11-02 09:57:10 +02001131 chan->tx_win_max = pchan->tx_win_max;
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001132 chan->sec_level = pchan->sec_level;
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +03001133 chan->flags = pchan->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +00001134
1135 security_sk_clone(parent, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001136 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001137
1138 switch (sk->sk_type) {
1139 case SOCK_RAW:
1140 chan->chan_type = L2CAP_CHAN_RAW;
1141 break;
1142 case SOCK_DGRAM:
1143 chan->chan_type = L2CAP_CHAN_CONN_LESS;
1144 break;
1145 case SOCK_SEQPACKET:
1146 case SOCK_STREAM:
1147 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
1148 break;
1149 }
1150
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001151 chan->imtu = L2CAP_DEFAULT_MTU;
1152 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001153 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001154 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001155 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001156 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001157 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001158 }
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +03001159
1160 l2cap_chan_set_defaults(chan);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001161 }
1162
1163 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001164 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001165
1166 chan->data = sk;
1167 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001168}
1169
1170static struct proto l2cap_proto = {
1171 .name = "L2CAP",
1172 .owner = THIS_MODULE,
1173 .obj_size = sizeof(struct l2cap_pinfo)
1174};
1175
Gustavo Padovan2d792812012-10-06 10:07:01 +01001176static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
1177 int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001178{
1179 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001180 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001181
1182 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1183 if (!sk)
1184 return NULL;
1185
1186 sock_init_data(sock, sk);
1187 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1188
1189 sk->sk_destruct = l2cap_sock_destruct;
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08001190 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001191
1192 sock_reset_flag(sk, SOCK_ZAPPED);
1193
1194 sk->sk_protocol = proto;
1195 sk->sk_state = BT_OPEN;
1196
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001197 chan = l2cap_chan_create();
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001198 if (!chan) {
Jaganath Kanakkassery49dfbb92012-07-19 12:54:04 +05301199 sk_free(sk);
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001200 return NULL;
1201 }
1202
Mat Martineau61d6ef32012-04-27 16:50:50 -07001203 l2cap_chan_hold(chan);
1204
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001205 chan->sk = sk;
1206
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001207 l2cap_pi(sk)->chan = chan;
1208
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001209 return sk;
1210}
1211
1212static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1213 int kern)
1214{
1215 struct sock *sk;
1216
1217 BT_DBG("sock %p", sock);
1218
1219 sock->state = SS_UNCONNECTED;
1220
1221 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01001222 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001223 return -ESOCKTNOSUPPORT;
1224
1225 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1226 return -EPERM;
1227
1228 sock->ops = &l2cap_sock_ops;
1229
1230 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1231 if (!sk)
1232 return -ENOMEM;
1233
1234 l2cap_sock_init(sk, NULL);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001235 bt_sock_link(&l2cap_sk_list, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001236 return 0;
1237}
1238
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -03001239static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001240 .family = PF_BLUETOOTH,
1241 .owner = THIS_MODULE,
1242 .release = l2cap_sock_release,
1243 .bind = l2cap_sock_bind,
1244 .connect = l2cap_sock_connect,
1245 .listen = l2cap_sock_listen,
1246 .accept = l2cap_sock_accept,
1247 .getname = l2cap_sock_getname,
1248 .sendmsg = l2cap_sock_sendmsg,
1249 .recvmsg = l2cap_sock_recvmsg,
1250 .poll = bt_sock_poll,
1251 .ioctl = bt_sock_ioctl,
1252 .mmap = sock_no_mmap,
1253 .socketpair = sock_no_socketpair,
1254 .shutdown = l2cap_sock_shutdown,
1255 .setsockopt = l2cap_sock_setsockopt,
1256 .getsockopt = l2cap_sock_getsockopt
1257};
1258
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001259static const struct net_proto_family l2cap_sock_family_ops = {
1260 .family = PF_BLUETOOTH,
1261 .owner = THIS_MODULE,
1262 .create = l2cap_sock_create,
1263};
1264
1265int __init l2cap_init_sockets(void)
1266{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001267 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001268
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001269 err = proto_register(&l2cap_proto, 0);
1270 if (err < 0)
1271 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001272
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001273 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001274 if (err < 0) {
1275 BT_ERR("L2CAP socket registration failed");
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001276 goto error;
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001277 }
1278
Gustavo Padovan2d792812012-10-06 10:07:01 +01001279 err = bt_procfs_init(THIS_MODULE, &init_net, "l2cap", &l2cap_sk_list,
1280 NULL);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001281 if (err < 0) {
1282 BT_ERR("Failed to create L2CAP proc file");
1283 bt_sock_unregister(BTPROTO_L2CAP);
1284 goto error;
1285 }
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001286
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001287 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001288
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001289 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001290
1291error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001292 proto_unregister(&l2cap_proto);
1293 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001294}
1295
1296void l2cap_cleanup_sockets(void)
1297{
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001298 bt_procfs_cleanup(&init_net, "l2cap");
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001299 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1300 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001301
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001302 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001303}