blob: 34bbe1c5e389500f080e15b30c194e95ea36f189 [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
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030037static const struct proto_ops l2cap_sock_ops;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -030038static void l2cap_sock_init(struct sock *sk, struct sock *parent);
39static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio);
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030040
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020041static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
42{
43 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -030044 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020045 struct sockaddr_l2 la;
46 int len, err = 0;
47
48 BT_DBG("sk %p", sk);
49
50 if (!addr || addr->sa_family != AF_BLUETOOTH)
51 return -EINVAL;
52
53 memset(&la, 0, sizeof(la));
54 len = min_t(unsigned int, sizeof(la), alen);
55 memcpy(&la, addr, len);
56
Ville Tervob62f3282011-02-10 22:38:50 -030057 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020058 return -EINVAL;
59
60 lock_sock(sk);
61
62 if (sk->sk_state != BT_OPEN) {
63 err = -EBADFD;
64 goto done;
65 }
66
67 if (la.l2_psm) {
68 __u16 psm = __le16_to_cpu(la.l2_psm);
69
70 /* PSM must be odd and lsb of upper byte must be 0 */
71 if ((psm & 0x0101) != 0x0001) {
72 err = -EINVAL;
73 goto done;
74 }
75
76 /* Restrict usage of well-known PSMs */
77 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
78 err = -EACCES;
79 goto done;
80 }
81 }
82
Ville Tervob62f3282011-02-10 22:38:50 -030083 if (la.l2_cid)
Santosh Nayak6e4aff12012-03-01 22:46:36 +053084 err = l2cap_add_scid(chan, __le16_to_cpu(la.l2_cid));
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030085 else
86 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
Ville Tervob62f3282011-02-10 22:38:50 -030087
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030088 if (err < 0)
89 goto done;
90
Andrei Emeltchenko2983fd62012-05-24 15:42:50 +030091 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP ||
92 __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030093 chan->sec_level = BT_SECURITY_SDP;
94
95 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -030096
97 chan->state = BT_BOUND;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030098 sk->sk_state = BT_BOUND;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020099
100done:
101 release_sock(sk);
102 return err;
103}
104
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200105static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
106{
107 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300108 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200109 struct sockaddr_l2 la;
110 int len, err = 0;
111
112 BT_DBG("sk %p", sk);
113
114 if (!addr || alen < sizeof(addr->sa_family) ||
115 addr->sa_family != AF_BLUETOOTH)
116 return -EINVAL;
117
118 memset(&la, 0, sizeof(la));
119 len = min_t(unsigned int, sizeof(la), alen);
120 memcpy(&la, addr, len);
121
Ville Tervoacd7d372011-02-10 22:38:49 -0300122 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200123 return -EINVAL;
124
Santosh Nayak6e4aff12012-03-01 22:46:36 +0530125 err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid),
Andre Guedes8e9f9892012-04-24 21:02:55 -0300126 &la.l2_bdaddr, la.l2_bdaddr_type);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200127 if (err)
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200128 return err;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200129
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200130 lock_sock(sk);
131
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200132 err = bt_sock_wait_state(sk, BT_CONNECTED,
133 sock_sndtimeo(sk, flags & O_NONBLOCK));
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200134
135 release_sock(sk);
136
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200137 return err;
138}
139
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200140static int l2cap_sock_listen(struct socket *sock, int backlog)
141{
142 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300143 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200144 int err = 0;
145
146 BT_DBG("sk %p backlog %d", sk, backlog);
147
148 lock_sock(sk);
149
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200150 if (sk->sk_state != BT_BOUND) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200151 err = -EBADFD;
152 goto done;
153 }
154
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200155 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) {
156 err = -EINVAL;
157 goto done;
158 }
159
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300160 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200161 case L2CAP_MODE_BASIC:
162 break;
163 case L2CAP_MODE_ERTM:
164 case L2CAP_MODE_STREAMING:
165 if (!disable_ertm)
166 break;
167 /* fall through */
168 default:
169 err = -ENOTSUPP;
170 goto done;
171 }
172
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200173 sk->sk_max_ack_backlog = backlog;
174 sk->sk_ack_backlog = 0;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300175
176 chan->state = BT_LISTEN;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200177 sk->sk_state = BT_LISTEN;
178
179done:
180 release_sock(sk);
181 return err;
182}
183
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200184static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
185{
186 DECLARE_WAITQUEUE(wait, current);
187 struct sock *sk = sock->sk, *nsk;
188 long timeo;
189 int err = 0;
190
191 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
192
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200193 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
194
195 BT_DBG("sk %p timeo %ld", sk, timeo);
196
197 /* Wait for an incoming connection. (wake-one). */
198 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400199 while (1) {
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200200 set_current_state(TASK_INTERRUPTIBLE);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200201
202 if (sk->sk_state != BT_LISTEN) {
203 err = -EBADFD;
204 break;
205 }
206
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400207 nsk = bt_accept_dequeue(sk, newsock);
208 if (nsk)
209 break;
210
211 if (!timeo) {
212 err = -EAGAIN;
213 break;
214 }
215
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200216 if (signal_pending(current)) {
217 err = sock_intr_errno(timeo);
218 break;
219 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400220
221 release_sock(sk);
222 timeo = schedule_timeout(timeo);
223 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200224 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400225 __set_current_state(TASK_RUNNING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200226 remove_wait_queue(sk_sleep(sk), &wait);
227
228 if (err)
229 goto done;
230
231 newsock->state = SS_CONNECTED;
232
233 BT_DBG("new socket %p", nsk);
234
235done:
236 release_sock(sk);
237 return err;
238}
239
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200240static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
241{
242 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
243 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300244 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200245
246 BT_DBG("sock %p, sk %p", sock, sk);
247
Mathias Krause792039c2012-08-15 11:31:51 +0000248 memset(la, 0, sizeof(struct sockaddr_l2));
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200249 addr->sa_family = AF_BLUETOOTH;
250 *len = sizeof(struct sockaddr_l2);
251
252 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300253 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200254 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300255 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200256 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300257 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200258 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300259 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200260 }
261
262 return 0;
263}
264
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200265static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
266{
267 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300268 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200269 struct l2cap_options opts;
270 struct l2cap_conninfo cinfo;
271 int len, err = 0;
272 u32 opt;
273
274 BT_DBG("sk %p", sk);
275
276 if (get_user(len, optlen))
277 return -EFAULT;
278
279 lock_sock(sk);
280
281 switch (optname) {
282 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300283 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300284 opts.imtu = chan->imtu;
285 opts.omtu = chan->omtu;
286 opts.flush_to = chan->flush_to;
287 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300288 opts.fcs = chan->fcs;
289 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300290 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200291
292 len = min_t(unsigned int, len, sizeof(opts));
293 if (copy_to_user(optval, (char *) &opts, len))
294 err = -EFAULT;
295
296 break;
297
298 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300299 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200300 case BT_SECURITY_LOW:
301 opt = L2CAP_LM_AUTH;
302 break;
303 case BT_SECURITY_MEDIUM:
304 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
305 break;
306 case BT_SECURITY_HIGH:
307 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
308 L2CAP_LM_SECURE;
309 break;
310 default:
311 opt = 0;
312 break;
313 }
314
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300315 if (test_bit(FLAG_ROLE_SWITCH, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200316 opt |= L2CAP_LM_MASTER;
317
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300318 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200319 opt |= L2CAP_LM_RELIABLE;
320
321 if (put_user(opt, (u32 __user *) optval))
322 err = -EFAULT;
323 break;
324
325 case L2CAP_CONNINFO:
326 if (sk->sk_state != BT_CONNECTED &&
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300327 !(sk->sk_state == BT_CONNECT2 &&
328 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200329 err = -ENOTCONN;
330 break;
331 }
332
Filip Palian8d03e972011-05-12 19:32:46 +0200333 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300334 cinfo.hci_handle = chan->conn->hcon->handle;
335 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200336
337 len = min_t(unsigned int, len, sizeof(cinfo));
338 if (copy_to_user(optval, (char *) &cinfo, len))
339 err = -EFAULT;
340
341 break;
342
343 default:
344 err = -ENOPROTOOPT;
345 break;
346 }
347
348 release_sock(sk);
349 return err;
350}
351
352static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
353{
354 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300355 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200356 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700357 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200358 int len, err = 0;
359
360 BT_DBG("sk %p", sk);
361
362 if (level == SOL_L2CAP)
363 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
364
365 if (level != SOL_BLUETOOTH)
366 return -ENOPROTOOPT;
367
368 if (get_user(len, optlen))
369 return -EFAULT;
370
371 lock_sock(sk);
372
373 switch (optname) {
374 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300375 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
376 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200377 err = -EINVAL;
378 break;
379 }
380
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300381 memset(&sec, 0, sizeof(sec));
Gustavo Padovanc6585a42012-05-07 03:07:26 -0300382 if (chan->conn)
383 sec.level = chan->conn->hcon->sec_level;
384 else
385 sec.level = chan->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200386
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300387 if (sk->sk_state == BT_CONNECTED)
388 sec.key_size = chan->conn->hcon->enc_key_size;
389
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200390 len = min_t(unsigned int, len, sizeof(sec));
391 if (copy_to_user(optval, (char *) &sec, len))
392 err = -EFAULT;
393
394 break;
395
396 case BT_DEFER_SETUP:
397 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
398 err = -EINVAL;
399 break;
400 }
401
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300402 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
403 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200404 err = -EFAULT;
405
406 break;
407
408 case BT_FLUSHABLE:
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300409 if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags),
410 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200411 err = -EFAULT;
412
413 break;
414
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700415 case BT_POWER:
416 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
417 && sk->sk_type != SOCK_RAW) {
418 err = -EINVAL;
419 break;
420 }
421
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300422 pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700423
424 len = min_t(unsigned int, len, sizeof(pwr));
425 if (copy_to_user(optval, (char *) &pwr, len))
426 err = -EFAULT;
427
428 break;
429
Mat Martineau2ea66482011-11-02 16:18:30 -0700430 case BT_CHANNEL_POLICY:
431 if (!enable_hs) {
432 err = -ENOPROTOOPT;
433 break;
434 }
435
436 if (put_user(chan->chan_policy, (u32 __user *) optval))
437 err = -EFAULT;
438 break;
439
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200440 default:
441 err = -ENOPROTOOPT;
442 break;
443 }
444
445 release_sock(sk);
446 return err;
447}
448
Andre Guedes682877c2012-05-31 17:01:34 -0300449static bool l2cap_valid_mtu(struct l2cap_chan *chan, u16 mtu)
450{
451 switch (chan->scid) {
452 case L2CAP_CID_LE_DATA:
Andre Guedes8c3a4f02012-05-31 17:01:35 -0300453 if (mtu < L2CAP_LE_MIN_MTU)
Andre Guedes682877c2012-05-31 17:01:34 -0300454 return false;
455 break;
456
457 default:
458 if (mtu < L2CAP_DEFAULT_MIN_MTU)
459 return false;
460 }
461
462 return true;
463}
464
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200465static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
466{
467 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300468 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200469 struct l2cap_options opts;
470 int len, err = 0;
471 u32 opt;
472
473 BT_DBG("sk %p", sk);
474
475 lock_sock(sk);
476
477 switch (optname) {
478 case L2CAP_OPTIONS:
479 if (sk->sk_state == BT_CONNECTED) {
480 err = -EINVAL;
481 break;
482 }
483
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300484 opts.imtu = chan->imtu;
485 opts.omtu = chan->omtu;
486 opts.flush_to = chan->flush_to;
487 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300488 opts.fcs = chan->fcs;
489 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300490 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200491
492 len = min_t(unsigned int, sizeof(opts), optlen);
493 if (copy_from_user((char *) &opts, optval, len)) {
494 err = -EFAULT;
495 break;
496 }
497
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300498 if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200499 err = -EINVAL;
500 break;
501 }
502
Andre Guedes682877c2012-05-31 17:01:34 -0300503 if (!l2cap_valid_mtu(chan, opts.imtu)) {
504 err = -EINVAL;
505 break;
506 }
507
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300508 chan->mode = opts.mode;
509 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200510 case L2CAP_MODE_BASIC:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -0300511 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200512 break;
513 case L2CAP_MODE_ERTM:
514 case L2CAP_MODE_STREAMING:
515 if (!disable_ertm)
516 break;
517 /* fall through */
518 default:
519 err = -EINVAL;
520 break;
521 }
522
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300523 chan->imtu = opts.imtu;
524 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300525 chan->fcs = opts.fcs;
526 chan->max_tx = opts.max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300527 chan->tx_win = opts.txwin_size;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200528 break;
529
530 case L2CAP_LM:
531 if (get_user(opt, (u32 __user *) optval)) {
532 err = -EFAULT;
533 break;
534 }
535
536 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300537 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200538 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300539 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200540 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300541 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200542
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300543 if (opt & L2CAP_LM_MASTER)
544 set_bit(FLAG_ROLE_SWITCH, &chan->flags);
545 else
546 clear_bit(FLAG_ROLE_SWITCH, &chan->flags);
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300547
548 if (opt & L2CAP_LM_RELIABLE)
549 set_bit(FLAG_FORCE_RELIABLE, &chan->flags);
550 else
551 clear_bit(FLAG_FORCE_RELIABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200552 break;
553
554 default:
555 err = -ENOPROTOOPT;
556 break;
557 }
558
559 release_sock(sk);
560 return err;
561}
562
563static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
564{
565 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300566 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200567 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700568 struct bt_power pwr;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300569 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200570 int len, err = 0;
571 u32 opt;
572
573 BT_DBG("sk %p", sk);
574
575 if (level == SOL_L2CAP)
576 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
577
578 if (level != SOL_BLUETOOTH)
579 return -ENOPROTOOPT;
580
581 lock_sock(sk);
582
583 switch (optname) {
584 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300585 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
586 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200587 err = -EINVAL;
588 break;
589 }
590
591 sec.level = BT_SECURITY_LOW;
592
593 len = min_t(unsigned int, sizeof(sec), optlen);
594 if (copy_from_user((char *) &sec, optval, len)) {
595 err = -EFAULT;
596 break;
597 }
598
599 if (sec.level < BT_SECURITY_LOW ||
600 sec.level > BT_SECURITY_HIGH) {
601 err = -EINVAL;
602 break;
603 }
604
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300605 chan->sec_level = sec.level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300606
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200607 if (!chan->conn)
608 break;
609
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300610 conn = chan->conn;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200611
612 /*change security for LE channels */
613 if (chan->scid == L2CAP_CID_LE_DATA) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300614 if (!conn->hcon->out) {
615 err = -EINVAL;
616 break;
617 }
618
Vinicius Costa Gomescc110922012-08-23 21:32:43 -0300619 if (smp_conn_security(conn->hcon, sec.level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300620 break;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300621 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200622 chan->state = BT_CONFIG;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200623
Gustavo Padovana7d77232012-05-13 03:20:07 -0300624 /* or for ACL link */
625 } else if ((sk->sk_state == BT_CONNECT2 &&
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300626 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) ||
Gustavo Padovana7d77232012-05-13 03:20:07 -0300627 sk->sk_state == BT_CONNECTED) {
628 if (!l2cap_chan_check_security(chan))
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300629 set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
Gustavo Padovana7d77232012-05-13 03:20:07 -0300630 else
631 sk->sk_state_change(sk);
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200632 } else {
633 err = -EINVAL;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300634 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200635 break;
636
637 case BT_DEFER_SETUP:
638 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
639 err = -EINVAL;
640 break;
641 }
642
643 if (get_user(opt, (u32 __user *) optval)) {
644 err = -EFAULT;
645 break;
646 }
647
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300648 if (opt)
649 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
650 else
651 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200652 break;
653
654 case BT_FLUSHABLE:
655 if (get_user(opt, (u32 __user *) optval)) {
656 err = -EFAULT;
657 break;
658 }
659
660 if (opt > BT_FLUSHABLE_ON) {
661 err = -EINVAL;
662 break;
663 }
664
665 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300666 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300667 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200668 No Flush support in the LM */
669 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
670 err = -EINVAL;
671 break;
672 }
673 }
674
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300675 if (opt)
676 set_bit(FLAG_FLUSHABLE, &chan->flags);
677 else
678 clear_bit(FLAG_FLUSHABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200679 break;
680
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700681 case BT_POWER:
682 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
683 chan->chan_type != L2CAP_CHAN_RAW) {
684 err = -EINVAL;
685 break;
686 }
687
688 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
689
690 len = min_t(unsigned int, sizeof(pwr), optlen);
691 if (copy_from_user((char *) &pwr, optval, len)) {
692 err = -EFAULT;
693 break;
694 }
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300695
696 if (pwr.force_active)
697 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
698 else
699 clear_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700700 break;
701
Mat Martineau2ea66482011-11-02 16:18:30 -0700702 case BT_CHANNEL_POLICY:
703 if (!enable_hs) {
704 err = -ENOPROTOOPT;
705 break;
706 }
707
708 if (get_user(opt, (u32 __user *) optval)) {
709 err = -EFAULT;
710 break;
711 }
712
713 if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) {
714 err = -EINVAL;
715 break;
716 }
717
718 if (chan->mode != L2CAP_MODE_ERTM &&
719 chan->mode != L2CAP_MODE_STREAMING) {
720 err = -EOPNOTSUPP;
721 break;
722 }
723
724 chan->chan_policy = (u8) opt;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200725 break;
726
727 default:
728 err = -ENOPROTOOPT;
729 break;
730 }
731
732 release_sock(sk);
733 return err;
734}
735
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200736static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
737{
738 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300739 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200740 int err;
741
742 BT_DBG("sock %p, sk %p", sock, sk);
743
744 err = sock_error(sk);
745 if (err)
746 return err;
747
748 if (msg->msg_flags & MSG_OOB)
749 return -EOPNOTSUPP;
750
Mat Martineaua6a55682012-05-04 14:20:31 -0700751 if (sk->sk_state != BT_CONNECTED)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300752 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200753
Mat Martineaua6a55682012-05-04 14:20:31 -0700754 l2cap_chan_lock(chan);
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200755 err = l2cap_chan_send(chan, msg, len, sk->sk_priority);
Mat Martineaua6a55682012-05-04 14:20:31 -0700756 l2cap_chan_unlock(chan);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200757
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200758 return err;
759}
760
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200761static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
762{
763 struct sock *sk = sock->sk;
Mat Martineaue3281402011-07-07 09:39:02 -0700764 struct l2cap_pinfo *pi = l2cap_pi(sk);
765 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200766
767 lock_sock(sk);
768
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300769 if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
770 &bt_sk(sk)->flags)) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300771 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200772 pi->chan->state = BT_CONFIG;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300773
Mat Martineaue3281402011-07-07 09:39:02 -0700774 __l2cap_connect_rsp_defer(pi->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200775 release_sock(sk);
776 return 0;
777 }
778
779 release_sock(sk);
780
781 if (sock->type == SOCK_STREAM)
Mat Martineaue3281402011-07-07 09:39:02 -0700782 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
783 else
784 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200785
Mat Martineaue3281402011-07-07 09:39:02 -0700786 if (pi->chan->mode != L2CAP_MODE_ERTM)
787 return err;
788
789 /* Attempt to put pending rx data in the socket buffer */
790
791 lock_sock(sk);
792
793 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
794 goto done;
795
796 if (pi->rx_busy_skb) {
797 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
798 pi->rx_busy_skb = NULL;
799 else
800 goto done;
801 }
802
803 /* Restore data flow when half of the receive buffer is
804 * available. This avoids resending large numbers of
805 * frames.
806 */
807 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
808 l2cap_chan_busy(pi->chan, 0);
809
810done:
811 release_sock(sk);
812 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200813}
814
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200815/* Kill socket (only if zapped and orphan)
816 * Must be called on unlocked socket.
817 */
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300818static void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200819{
820 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
821 return;
822
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200823 BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200824
825 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300826
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300827 l2cap_chan_destroy(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200828 sock_set_flag(sk, SOCK_DEAD);
829 sock_put(sk);
830}
831
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200832static int l2cap_sock_shutdown(struct socket *sock, int how)
833{
834 struct sock *sk = sock->sk;
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200835 struct l2cap_chan *chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200836 struct l2cap_conn *conn;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200837 int err = 0;
838
839 BT_DBG("sock %p, sk %p", sock, sk);
840
841 if (!sk)
842 return 0;
843
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200844 chan = l2cap_pi(sk)->chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200845 conn = chan->conn;
846
847 if (conn)
848 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200849
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200850 l2cap_chan_lock(chan);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200851 lock_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200852
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200853 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300854 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200855 err = __l2cap_wait_ack(sk);
856
857 sk->sk_shutdown = SHUTDOWN_MASK;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200858
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200859 release_sock(sk);
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300860 l2cap_chan_close(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200861 lock_sock(sk);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200862
863 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
864 err = bt_sock_wait_state(sk, BT_CLOSED,
865 sk->sk_lingertime);
866 }
867
868 if (!err && sk->sk_err)
869 err = -sk->sk_err;
870
871 release_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200872 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200873
874 if (conn)
875 mutex_unlock(&conn->chan_lock);
876
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200877 return err;
878}
879
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200880static int l2cap_sock_release(struct socket *sock)
881{
882 struct sock *sk = sock->sk;
883 int err;
884
885 BT_DBG("sock %p, sk %p", sock, sk);
886
887 if (!sk)
888 return 0;
889
890 err = l2cap_sock_shutdown(sock, 2);
891
892 sock_orphan(sk);
893 l2cap_sock_kill(sk);
894 return err;
895}
896
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -0300897static void l2cap_sock_cleanup_listen(struct sock *parent)
898{
899 struct sock *sk;
900
901 BT_DBG("parent %p", parent);
902
903 /* Close not yet accepted channels */
904 while ((sk = bt_accept_dequeue(parent, NULL))) {
905 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
906
907 l2cap_chan_lock(chan);
908 __clear_chan_timer(chan);
909 l2cap_chan_close(chan, ECONNRESET);
910 l2cap_chan_unlock(chan);
911
912 l2cap_sock_kill(sk);
913 }
914}
915
Gustavo Padovan80b98022012-05-27 22:27:51 -0300916static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300917{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300918 struct sock *sk, *parent = chan->data;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300919
Gustavo Padovan53826692012-05-27 22:27:55 -0300920 /* Check for backlog size */
921 if (sk_acceptq_is_full(parent)) {
922 BT_DBG("backlog full %d", parent->sk_ack_backlog);
923 return NULL;
924 }
925
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300926 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
927 GFP_ATOMIC);
928 if (!sk)
929 return NULL;
930
Octavian Purdilad22015a2012-01-22 00:28:34 +0200931 bt_sock_reclassify_lock(sk, BTPROTO_L2CAP);
932
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300933 l2cap_sock_init(sk, parent);
934
935 return l2cap_pi(sk)->chan;
936}
937
Gustavo Padovan80b98022012-05-27 22:27:51 -0300938static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300939{
Mat Martineaue3281402011-07-07 09:39:02 -0700940 int err;
Gustavo Padovan80b98022012-05-27 22:27:51 -0300941 struct sock *sk = chan->data;
Mat Martineaue3281402011-07-07 09:39:02 -0700942 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300943
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200944 lock_sock(sk);
945
946 if (pi->rx_busy_skb) {
947 err = -ENOMEM;
948 goto done;
949 }
Mat Martineaue3281402011-07-07 09:39:02 -0700950
951 err = sock_queue_rcv_skb(sk, skb);
952
953 /* For ERTM, handle one skb that doesn't fit into the recv
954 * buffer. This is important to do because the data frames
955 * have already been acked, so the skb cannot be discarded.
956 *
957 * Notify the l2cap core that the buffer is full, so the
958 * LOCAL_BUSY state is entered and no more frames are
959 * acked and reassembled until there is buffer space
960 * available.
961 */
962 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
963 pi->rx_busy_skb = skb;
964 l2cap_chan_busy(pi->chan, 1);
965 err = 0;
966 }
967
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200968done:
969 release_sock(sk);
970
Mat Martineaue3281402011-07-07 09:39:02 -0700971 return err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300972}
973
Gustavo Padovan80b98022012-05-27 22:27:51 -0300974static void l2cap_sock_close_cb(struct l2cap_chan *chan)
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300975{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300976 struct sock *sk = chan->data;
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300977
978 l2cap_sock_kill(sk);
979}
980
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -0300981static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
982{
983 struct sock *sk = chan->data;
984 struct sock *parent;
985
986 lock_sock(sk);
987
988 parent = bt_sk(sk)->parent;
989
990 sock_set_flag(sk, SOCK_ZAPPED);
991
992 switch (chan->state) {
993 case BT_OPEN:
994 case BT_BOUND:
995 case BT_CLOSED:
996 break;
997 case BT_LISTEN:
998 l2cap_sock_cleanup_listen(sk);
999 sk->sk_state = BT_CLOSED;
1000 chan->state = BT_CLOSED;
1001
1002 break;
1003 default:
1004 sk->sk_state = BT_CLOSED;
1005 chan->state = BT_CLOSED;
1006
1007 sk->sk_err = err;
1008
1009 if (parent) {
1010 bt_accept_unlink(sk);
1011 parent->sk_data_ready(parent, 0);
1012 } else {
1013 sk->sk_state_change(sk);
1014 }
1015
1016 break;
1017 }
1018
1019 release_sock(sk);
1020}
1021
Gustavo Padovan80b98022012-05-27 22:27:51 -03001022static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001023{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001024 struct sock *sk = chan->data;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001025
1026 sk->sk_state = state;
1027}
1028
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001029static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
Gustavo Padovan90338942012-04-06 20:15:47 -03001030 unsigned long len, int nb)
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001031{
Gustavo Padovan90338942012-04-06 20:15:47 -03001032 struct sk_buff *skb;
1033 int err;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001034
Mat Martineaua6a55682012-05-04 14:20:31 -07001035 l2cap_chan_unlock(chan);
Gustavo Padovan90338942012-04-06 20:15:47 -03001036 skb = bt_skb_send_alloc(chan->sk, len, nb, &err);
Mat Martineaua6a55682012-05-04 14:20:31 -07001037 l2cap_chan_lock(chan);
1038
Gustavo Padovan90338942012-04-06 20:15:47 -03001039 if (!skb)
1040 return ERR_PTR(err);
1041
1042 return skb;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001043}
1044
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001045static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
1046{
1047 struct sock *sk = chan->data;
1048 struct sock *parent;
1049
1050 lock_sock(sk);
1051
1052 parent = bt_sk(sk)->parent;
1053
1054 BT_DBG("sk %p, parent %p", sk, parent);
1055
1056 sk->sk_state = BT_CONNECTED;
1057 sk->sk_state_change(sk);
1058
1059 if (parent)
1060 parent->sk_data_ready(parent, 0);
1061
1062 release_sock(sk);
1063}
1064
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001065static struct l2cap_ops l2cap_chan_ops = {
1066 .name = "L2CAP Socket Interface",
1067 .new_connection = l2cap_sock_new_connection_cb,
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001068 .recv = l2cap_sock_recv_cb,
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001069 .close = l2cap_sock_close_cb,
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001070 .teardown = l2cap_sock_teardown_cb,
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001071 .state_change = l2cap_sock_state_change_cb,
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001072 .ready = l2cap_sock_ready_cb,
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001073 .alloc_skb = l2cap_sock_alloc_skb_cb,
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001074};
1075
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001076static void l2cap_sock_destruct(struct sock *sk)
1077{
1078 BT_DBG("sk %p", sk);
1079
Mat Martineau61d6ef32012-04-27 16:50:50 -07001080 l2cap_chan_put(l2cap_pi(sk)->chan);
Mat Martineaue3281402011-07-07 09:39:02 -07001081 if (l2cap_pi(sk)->rx_busy_skb) {
1082 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
1083 l2cap_pi(sk)->rx_busy_skb = NULL;
1084 }
1085
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001086 skb_queue_purge(&sk->sk_receive_queue);
1087 skb_queue_purge(&sk->sk_write_queue);
1088}
1089
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001090static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001091{
1092 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001093 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001094
1095 BT_DBG("sk %p", sk);
1096
1097 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001098 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
1099
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001100 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -03001101 bt_sk(sk)->flags = bt_sk(parent)->flags;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001102
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001103 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001104 chan->imtu = pchan->imtu;
1105 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001106 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001107 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001108 chan->fcs = pchan->fcs;
1109 chan->max_tx = pchan->max_tx;
1110 chan->tx_win = pchan->tx_win;
Andrei Emeltchenko6b3c7102011-11-02 09:57:10 +02001111 chan->tx_win_max = pchan->tx_win_max;
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001112 chan->sec_level = pchan->sec_level;
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +03001113 chan->flags = pchan->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +00001114
1115 security_sk_clone(parent, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001116 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001117
1118 switch (sk->sk_type) {
1119 case SOCK_RAW:
1120 chan->chan_type = L2CAP_CHAN_RAW;
1121 break;
1122 case SOCK_DGRAM:
1123 chan->chan_type = L2CAP_CHAN_CONN_LESS;
1124 break;
1125 case SOCK_SEQPACKET:
1126 case SOCK_STREAM:
1127 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
1128 break;
1129 }
1130
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001131 chan->imtu = L2CAP_DEFAULT_MTU;
1132 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001133 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001134 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001135 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001136 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001137 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001138 }
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +03001139
1140 l2cap_chan_set_defaults(chan);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001141 }
1142
1143 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001144 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001145
1146 chan->data = sk;
1147 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001148}
1149
1150static struct proto l2cap_proto = {
1151 .name = "L2CAP",
1152 .owner = THIS_MODULE,
1153 .obj_size = sizeof(struct l2cap_pinfo)
1154};
1155
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001156static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001157{
1158 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001159 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001160
1161 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1162 if (!sk)
1163 return NULL;
1164
1165 sock_init_data(sock, sk);
1166 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1167
1168 sk->sk_destruct = l2cap_sock_destruct;
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08001169 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001170
1171 sock_reset_flag(sk, SOCK_ZAPPED);
1172
1173 sk->sk_protocol = proto;
1174 sk->sk_state = BT_OPEN;
1175
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001176 chan = l2cap_chan_create();
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001177 if (!chan) {
Jaganath Kanakkassery49dfbb92012-07-19 12:54:04 +05301178 sk_free(sk);
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001179 return NULL;
1180 }
1181
Mat Martineau61d6ef32012-04-27 16:50:50 -07001182 l2cap_chan_hold(chan);
1183
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001184 chan->sk = sk;
1185
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001186 l2cap_pi(sk)->chan = chan;
1187
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001188 return sk;
1189}
1190
1191static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1192 int kern)
1193{
1194 struct sock *sk;
1195
1196 BT_DBG("sock %p", sock);
1197
1198 sock->state = SS_UNCONNECTED;
1199
1200 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1201 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1202 return -ESOCKTNOSUPPORT;
1203
1204 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1205 return -EPERM;
1206
1207 sock->ops = &l2cap_sock_ops;
1208
1209 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1210 if (!sk)
1211 return -ENOMEM;
1212
1213 l2cap_sock_init(sk, NULL);
1214 return 0;
1215}
1216
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -03001217static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001218 .family = PF_BLUETOOTH,
1219 .owner = THIS_MODULE,
1220 .release = l2cap_sock_release,
1221 .bind = l2cap_sock_bind,
1222 .connect = l2cap_sock_connect,
1223 .listen = l2cap_sock_listen,
1224 .accept = l2cap_sock_accept,
1225 .getname = l2cap_sock_getname,
1226 .sendmsg = l2cap_sock_sendmsg,
1227 .recvmsg = l2cap_sock_recvmsg,
1228 .poll = bt_sock_poll,
1229 .ioctl = bt_sock_ioctl,
1230 .mmap = sock_no_mmap,
1231 .socketpair = sock_no_socketpair,
1232 .shutdown = l2cap_sock_shutdown,
1233 .setsockopt = l2cap_sock_setsockopt,
1234 .getsockopt = l2cap_sock_getsockopt
1235};
1236
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001237static const struct net_proto_family l2cap_sock_family_ops = {
1238 .family = PF_BLUETOOTH,
1239 .owner = THIS_MODULE,
1240 .create = l2cap_sock_create,
1241};
1242
1243int __init l2cap_init_sockets(void)
1244{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001245 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001246
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001247 err = proto_register(&l2cap_proto, 0);
1248 if (err < 0)
1249 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001250
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001251 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1252 if (err < 0)
1253 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001254
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001255 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001256
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001257 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001258
1259error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001260 BT_ERR("L2CAP socket registration failed");
1261 proto_unregister(&l2cap_proto);
1262 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001263}
1264
1265void l2cap_cleanup_sockets(void)
1266{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001267 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1268 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001269
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001270 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001271}