blob: 5563023001c65310decf1690ddb62ea16df542cf [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
248 addr->sa_family = AF_BLUETOOTH;
249 *len = sizeof(struct sockaddr_l2);
250
251 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300252 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200253 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300254 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200255 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300256 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200257 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300258 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200259 }
260
261 return 0;
262}
263
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200264static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
265{
266 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300267 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200268 struct l2cap_options opts;
269 struct l2cap_conninfo cinfo;
270 int len, err = 0;
271 u32 opt;
272
273 BT_DBG("sk %p", sk);
274
275 if (get_user(len, optlen))
276 return -EFAULT;
277
278 lock_sock(sk);
279
280 switch (optname) {
281 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300282 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300283 opts.imtu = chan->imtu;
284 opts.omtu = chan->omtu;
285 opts.flush_to = chan->flush_to;
286 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300287 opts.fcs = chan->fcs;
288 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300289 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200290
291 len = min_t(unsigned int, len, sizeof(opts));
292 if (copy_to_user(optval, (char *) &opts, len))
293 err = -EFAULT;
294
295 break;
296
297 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300298 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200299 case BT_SECURITY_LOW:
300 opt = L2CAP_LM_AUTH;
301 break;
302 case BT_SECURITY_MEDIUM:
303 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
304 break;
305 case BT_SECURITY_HIGH:
306 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
307 L2CAP_LM_SECURE;
308 break;
309 default:
310 opt = 0;
311 break;
312 }
313
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300314 if (test_bit(FLAG_ROLE_SWITCH, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200315 opt |= L2CAP_LM_MASTER;
316
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300317 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200318 opt |= L2CAP_LM_RELIABLE;
319
320 if (put_user(opt, (u32 __user *) optval))
321 err = -EFAULT;
322 break;
323
324 case L2CAP_CONNINFO:
325 if (sk->sk_state != BT_CONNECTED &&
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300326 !(sk->sk_state == BT_CONNECT2 &&
327 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200328 err = -ENOTCONN;
329 break;
330 }
331
Filip Palian8d03e972011-05-12 19:32:46 +0200332 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300333 cinfo.hci_handle = chan->conn->hcon->handle;
334 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200335
336 len = min_t(unsigned int, len, sizeof(cinfo));
337 if (copy_to_user(optval, (char *) &cinfo, len))
338 err = -EFAULT;
339
340 break;
341
342 default:
343 err = -ENOPROTOOPT;
344 break;
345 }
346
347 release_sock(sk);
348 return err;
349}
350
351static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
352{
353 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300354 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200355 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700356 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200357 int len, err = 0;
358
359 BT_DBG("sk %p", sk);
360
361 if (level == SOL_L2CAP)
362 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
363
364 if (level != SOL_BLUETOOTH)
365 return -ENOPROTOOPT;
366
367 if (get_user(len, optlen))
368 return -EFAULT;
369
370 lock_sock(sk);
371
372 switch (optname) {
373 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300374 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
375 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200376 err = -EINVAL;
377 break;
378 }
379
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300380 memset(&sec, 0, sizeof(sec));
Gustavo Padovanc6585a42012-05-07 03:07:26 -0300381 if (chan->conn)
382 sec.level = chan->conn->hcon->sec_level;
383 else
384 sec.level = chan->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200385
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300386 if (sk->sk_state == BT_CONNECTED)
387 sec.key_size = chan->conn->hcon->enc_key_size;
388
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200389 len = min_t(unsigned int, len, sizeof(sec));
390 if (copy_to_user(optval, (char *) &sec, len))
391 err = -EFAULT;
392
393 break;
394
395 case BT_DEFER_SETUP:
396 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
397 err = -EINVAL;
398 break;
399 }
400
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300401 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
402 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200403 err = -EFAULT;
404
405 break;
406
407 case BT_FLUSHABLE:
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300408 if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags),
409 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200410 err = -EFAULT;
411
412 break;
413
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700414 case BT_POWER:
415 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
416 && sk->sk_type != SOCK_RAW) {
417 err = -EINVAL;
418 break;
419 }
420
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300421 pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700422
423 len = min_t(unsigned int, len, sizeof(pwr));
424 if (copy_to_user(optval, (char *) &pwr, len))
425 err = -EFAULT;
426
427 break;
428
Mat Martineau2ea66482011-11-02 16:18:30 -0700429 case BT_CHANNEL_POLICY:
430 if (!enable_hs) {
431 err = -ENOPROTOOPT;
432 break;
433 }
434
435 if (put_user(chan->chan_policy, (u32 __user *) optval))
436 err = -EFAULT;
437 break;
438
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200439 default:
440 err = -ENOPROTOOPT;
441 break;
442 }
443
444 release_sock(sk);
445 return err;
446}
447
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200448static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
449{
450 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300451 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200452 struct l2cap_options opts;
453 int len, err = 0;
454 u32 opt;
455
456 BT_DBG("sk %p", sk);
457
458 lock_sock(sk);
459
460 switch (optname) {
461 case L2CAP_OPTIONS:
462 if (sk->sk_state == BT_CONNECTED) {
463 err = -EINVAL;
464 break;
465 }
466
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300467 opts.imtu = chan->imtu;
468 opts.omtu = chan->omtu;
469 opts.flush_to = chan->flush_to;
470 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300471 opts.fcs = chan->fcs;
472 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300473 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200474
475 len = min_t(unsigned int, sizeof(opts), optlen);
476 if (copy_from_user((char *) &opts, optval, len)) {
477 err = -EFAULT;
478 break;
479 }
480
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300481 if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200482 err = -EINVAL;
483 break;
484 }
485
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300486 chan->mode = opts.mode;
487 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200488 case L2CAP_MODE_BASIC:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -0300489 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200490 break;
491 case L2CAP_MODE_ERTM:
492 case L2CAP_MODE_STREAMING:
493 if (!disable_ertm)
494 break;
495 /* fall through */
496 default:
497 err = -EINVAL;
498 break;
499 }
500
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300501 chan->imtu = opts.imtu;
502 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300503 chan->fcs = opts.fcs;
504 chan->max_tx = opts.max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300505 chan->tx_win = opts.txwin_size;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200506 break;
507
508 case L2CAP_LM:
509 if (get_user(opt, (u32 __user *) optval)) {
510 err = -EFAULT;
511 break;
512 }
513
514 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300515 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200516 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300517 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200518 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300519 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200520
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300521 if (opt & L2CAP_LM_MASTER)
522 set_bit(FLAG_ROLE_SWITCH, &chan->flags);
523 else
524 clear_bit(FLAG_ROLE_SWITCH, &chan->flags);
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300525
526 if (opt & L2CAP_LM_RELIABLE)
527 set_bit(FLAG_FORCE_RELIABLE, &chan->flags);
528 else
529 clear_bit(FLAG_FORCE_RELIABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200530 break;
531
532 default:
533 err = -ENOPROTOOPT;
534 break;
535 }
536
537 release_sock(sk);
538 return err;
539}
540
541static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
542{
543 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300544 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200545 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700546 struct bt_power pwr;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300547 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200548 int len, err = 0;
549 u32 opt;
550
551 BT_DBG("sk %p", sk);
552
553 if (level == SOL_L2CAP)
554 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
555
556 if (level != SOL_BLUETOOTH)
557 return -ENOPROTOOPT;
558
559 lock_sock(sk);
560
561 switch (optname) {
562 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300563 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
564 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200565 err = -EINVAL;
566 break;
567 }
568
569 sec.level = BT_SECURITY_LOW;
570
571 len = min_t(unsigned int, sizeof(sec), optlen);
572 if (copy_from_user((char *) &sec, optval, len)) {
573 err = -EFAULT;
574 break;
575 }
576
577 if (sec.level < BT_SECURITY_LOW ||
578 sec.level > BT_SECURITY_HIGH) {
579 err = -EINVAL;
580 break;
581 }
582
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300583 chan->sec_level = sec.level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300584
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200585 if (!chan->conn)
586 break;
587
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300588 conn = chan->conn;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200589
590 /*change security for LE channels */
591 if (chan->scid == L2CAP_CID_LE_DATA) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300592 if (!conn->hcon->out) {
593 err = -EINVAL;
594 break;
595 }
596
597 if (smp_conn_security(conn, sec.level))
598 break;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300599 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200600 chan->state = BT_CONFIG;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200601
Gustavo Padovana7d77232012-05-13 03:20:07 -0300602 /* or for ACL link */
603 } else if ((sk->sk_state == BT_CONNECT2 &&
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300604 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) ||
Gustavo Padovana7d77232012-05-13 03:20:07 -0300605 sk->sk_state == BT_CONNECTED) {
606 if (!l2cap_chan_check_security(chan))
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300607 set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
Gustavo Padovana7d77232012-05-13 03:20:07 -0300608 else
609 sk->sk_state_change(sk);
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200610 } else {
611 err = -EINVAL;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300612 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200613 break;
614
615 case BT_DEFER_SETUP:
616 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
617 err = -EINVAL;
618 break;
619 }
620
621 if (get_user(opt, (u32 __user *) optval)) {
622 err = -EFAULT;
623 break;
624 }
625
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300626 if (opt)
627 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
628 else
629 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200630 break;
631
632 case BT_FLUSHABLE:
633 if (get_user(opt, (u32 __user *) optval)) {
634 err = -EFAULT;
635 break;
636 }
637
638 if (opt > BT_FLUSHABLE_ON) {
639 err = -EINVAL;
640 break;
641 }
642
643 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300644 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300645 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200646 No Flush support in the LM */
647 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
648 err = -EINVAL;
649 break;
650 }
651 }
652
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300653 if (opt)
654 set_bit(FLAG_FLUSHABLE, &chan->flags);
655 else
656 clear_bit(FLAG_FLUSHABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200657 break;
658
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700659 case BT_POWER:
660 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
661 chan->chan_type != L2CAP_CHAN_RAW) {
662 err = -EINVAL;
663 break;
664 }
665
666 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
667
668 len = min_t(unsigned int, sizeof(pwr), optlen);
669 if (copy_from_user((char *) &pwr, optval, len)) {
670 err = -EFAULT;
671 break;
672 }
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300673
674 if (pwr.force_active)
675 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
676 else
677 clear_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700678 break;
679
Mat Martineau2ea66482011-11-02 16:18:30 -0700680 case BT_CHANNEL_POLICY:
681 if (!enable_hs) {
682 err = -ENOPROTOOPT;
683 break;
684 }
685
686 if (get_user(opt, (u32 __user *) optval)) {
687 err = -EFAULT;
688 break;
689 }
690
691 if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) {
692 err = -EINVAL;
693 break;
694 }
695
696 if (chan->mode != L2CAP_MODE_ERTM &&
697 chan->mode != L2CAP_MODE_STREAMING) {
698 err = -EOPNOTSUPP;
699 break;
700 }
701
702 chan->chan_policy = (u8) opt;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200703 break;
704
705 default:
706 err = -ENOPROTOOPT;
707 break;
708 }
709
710 release_sock(sk);
711 return err;
712}
713
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200714static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
715{
716 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300717 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200718 int err;
719
720 BT_DBG("sock %p, sk %p", sock, sk);
721
722 err = sock_error(sk);
723 if (err)
724 return err;
725
726 if (msg->msg_flags & MSG_OOB)
727 return -EOPNOTSUPP;
728
Mat Martineaua6a55682012-05-04 14:20:31 -0700729 if (sk->sk_state != BT_CONNECTED)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300730 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200731
Mat Martineaua6a55682012-05-04 14:20:31 -0700732 l2cap_chan_lock(chan);
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200733 err = l2cap_chan_send(chan, msg, len, sk->sk_priority);
Mat Martineaua6a55682012-05-04 14:20:31 -0700734 l2cap_chan_unlock(chan);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200735
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200736 return err;
737}
738
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200739static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
740{
741 struct sock *sk = sock->sk;
Mat Martineaue3281402011-07-07 09:39:02 -0700742 struct l2cap_pinfo *pi = l2cap_pi(sk);
743 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200744
745 lock_sock(sk);
746
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300747 if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
748 &bt_sk(sk)->flags)) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300749 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200750 pi->chan->state = BT_CONFIG;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300751
Mat Martineaue3281402011-07-07 09:39:02 -0700752 __l2cap_connect_rsp_defer(pi->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200753 release_sock(sk);
754 return 0;
755 }
756
757 release_sock(sk);
758
759 if (sock->type == SOCK_STREAM)
Mat Martineaue3281402011-07-07 09:39:02 -0700760 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
761 else
762 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200763
Mat Martineaue3281402011-07-07 09:39:02 -0700764 if (pi->chan->mode != L2CAP_MODE_ERTM)
765 return err;
766
767 /* Attempt to put pending rx data in the socket buffer */
768
769 lock_sock(sk);
770
771 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
772 goto done;
773
774 if (pi->rx_busy_skb) {
775 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
776 pi->rx_busy_skb = NULL;
777 else
778 goto done;
779 }
780
781 /* Restore data flow when half of the receive buffer is
782 * available. This avoids resending large numbers of
783 * frames.
784 */
785 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
786 l2cap_chan_busy(pi->chan, 0);
787
788done:
789 release_sock(sk);
790 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200791}
792
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200793/* Kill socket (only if zapped and orphan)
794 * Must be called on unlocked socket.
795 */
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300796static void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200797{
798 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
799 return;
800
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200801 BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200802
803 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300804
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300805 l2cap_chan_destroy(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200806 sock_set_flag(sk, SOCK_DEAD);
807 sock_put(sk);
808}
809
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200810static int l2cap_sock_shutdown(struct socket *sock, int how)
811{
812 struct sock *sk = sock->sk;
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200813 struct l2cap_chan *chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200814 struct l2cap_conn *conn;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200815 int err = 0;
816
817 BT_DBG("sock %p, sk %p", sock, sk);
818
819 if (!sk)
820 return 0;
821
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200822 chan = l2cap_pi(sk)->chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200823 conn = chan->conn;
824
825 if (conn)
826 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200827
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200828 l2cap_chan_lock(chan);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200829 lock_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200830
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200831 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300832 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200833 err = __l2cap_wait_ack(sk);
834
835 sk->sk_shutdown = SHUTDOWN_MASK;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200836
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200837 release_sock(sk);
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300838 l2cap_chan_close(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200839 lock_sock(sk);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200840
841 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
842 err = bt_sock_wait_state(sk, BT_CLOSED,
843 sk->sk_lingertime);
844 }
845
846 if (!err && sk->sk_err)
847 err = -sk->sk_err;
848
849 release_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200850 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200851
852 if (conn)
853 mutex_unlock(&conn->chan_lock);
854
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200855 return err;
856}
857
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200858static int l2cap_sock_release(struct socket *sock)
859{
860 struct sock *sk = sock->sk;
861 int err;
862
863 BT_DBG("sock %p, sk %p", sock, sk);
864
865 if (!sk)
866 return 0;
867
868 err = l2cap_sock_shutdown(sock, 2);
869
870 sock_orphan(sk);
871 l2cap_sock_kill(sk);
872 return err;
873}
874
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -0300875static void l2cap_sock_cleanup_listen(struct sock *parent)
876{
877 struct sock *sk;
878
879 BT_DBG("parent %p", parent);
880
881 /* Close not yet accepted channels */
882 while ((sk = bt_accept_dequeue(parent, NULL))) {
883 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
884
885 l2cap_chan_lock(chan);
886 __clear_chan_timer(chan);
887 l2cap_chan_close(chan, ECONNRESET);
888 l2cap_chan_unlock(chan);
889
890 l2cap_sock_kill(sk);
891 }
892}
893
Gustavo Padovan80b98022012-05-27 22:27:51 -0300894static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300895{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300896 struct sock *sk, *parent = chan->data;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300897
898 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
899 GFP_ATOMIC);
900 if (!sk)
901 return NULL;
902
Octavian Purdilad22015a2012-01-22 00:28:34 +0200903 bt_sock_reclassify_lock(sk, BTPROTO_L2CAP);
904
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300905 l2cap_sock_init(sk, parent);
906
907 return l2cap_pi(sk)->chan;
908}
909
Gustavo Padovan80b98022012-05-27 22:27:51 -0300910static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300911{
Mat Martineaue3281402011-07-07 09:39:02 -0700912 int err;
Gustavo Padovan80b98022012-05-27 22:27:51 -0300913 struct sock *sk = chan->data;
Mat Martineaue3281402011-07-07 09:39:02 -0700914 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300915
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200916 lock_sock(sk);
917
918 if (pi->rx_busy_skb) {
919 err = -ENOMEM;
920 goto done;
921 }
Mat Martineaue3281402011-07-07 09:39:02 -0700922
923 err = sock_queue_rcv_skb(sk, skb);
924
925 /* For ERTM, handle one skb that doesn't fit into the recv
926 * buffer. This is important to do because the data frames
927 * have already been acked, so the skb cannot be discarded.
928 *
929 * Notify the l2cap core that the buffer is full, so the
930 * LOCAL_BUSY state is entered and no more frames are
931 * acked and reassembled until there is buffer space
932 * available.
933 */
934 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
935 pi->rx_busy_skb = skb;
936 l2cap_chan_busy(pi->chan, 1);
937 err = 0;
938 }
939
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200940done:
941 release_sock(sk);
942
Mat Martineaue3281402011-07-07 09:39:02 -0700943 return err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300944}
945
Gustavo Padovan80b98022012-05-27 22:27:51 -0300946static void l2cap_sock_close_cb(struct l2cap_chan *chan)
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300947{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300948 struct sock *sk = chan->data;
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300949
950 l2cap_sock_kill(sk);
951}
952
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -0300953static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
954{
955 struct sock *sk = chan->data;
956 struct sock *parent;
957
958 lock_sock(sk);
959
960 parent = bt_sk(sk)->parent;
961
962 sock_set_flag(sk, SOCK_ZAPPED);
963
964 switch (chan->state) {
965 case BT_OPEN:
966 case BT_BOUND:
967 case BT_CLOSED:
968 break;
969 case BT_LISTEN:
970 l2cap_sock_cleanup_listen(sk);
971 sk->sk_state = BT_CLOSED;
972 chan->state = BT_CLOSED;
973
974 break;
975 default:
976 sk->sk_state = BT_CLOSED;
977 chan->state = BT_CLOSED;
978
979 sk->sk_err = err;
980
981 if (parent) {
982 bt_accept_unlink(sk);
983 parent->sk_data_ready(parent, 0);
984 } else {
985 sk->sk_state_change(sk);
986 }
987
988 break;
989 }
990
991 release_sock(sk);
992}
993
Gustavo Padovan80b98022012-05-27 22:27:51 -0300994static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300995{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300996 struct sock *sk = chan->data;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300997
998 sk->sk_state = state;
999}
1000
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001001static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
Gustavo Padovan90338942012-04-06 20:15:47 -03001002 unsigned long len, int nb)
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001003{
Gustavo Padovan90338942012-04-06 20:15:47 -03001004 struct sk_buff *skb;
1005 int err;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001006
Mat Martineaua6a55682012-05-04 14:20:31 -07001007 l2cap_chan_unlock(chan);
Gustavo Padovan90338942012-04-06 20:15:47 -03001008 skb = bt_skb_send_alloc(chan->sk, len, nb, &err);
Mat Martineaua6a55682012-05-04 14:20:31 -07001009 l2cap_chan_lock(chan);
1010
Gustavo Padovan90338942012-04-06 20:15:47 -03001011 if (!skb)
1012 return ERR_PTR(err);
1013
1014 return skb;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001015}
1016
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001017static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
1018{
1019 struct sock *sk = chan->data;
1020 struct sock *parent;
1021
1022 lock_sock(sk);
1023
1024 parent = bt_sk(sk)->parent;
1025
1026 BT_DBG("sk %p, parent %p", sk, parent);
1027
1028 sk->sk_state = BT_CONNECTED;
1029 sk->sk_state_change(sk);
1030
1031 if (parent)
1032 parent->sk_data_ready(parent, 0);
1033
1034 release_sock(sk);
1035}
1036
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001037static struct l2cap_ops l2cap_chan_ops = {
1038 .name = "L2CAP Socket Interface",
1039 .new_connection = l2cap_sock_new_connection_cb,
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001040 .recv = l2cap_sock_recv_cb,
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001041 .close = l2cap_sock_close_cb,
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001042 .teardown = l2cap_sock_teardown_cb,
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001043 .state_change = l2cap_sock_state_change_cb,
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001044 .ready = l2cap_sock_ready_cb,
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001045 .alloc_skb = l2cap_sock_alloc_skb_cb,
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001046};
1047
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001048static void l2cap_sock_destruct(struct sock *sk)
1049{
1050 BT_DBG("sk %p", sk);
1051
Mat Martineau61d6ef32012-04-27 16:50:50 -07001052 l2cap_chan_put(l2cap_pi(sk)->chan);
Mat Martineaue3281402011-07-07 09:39:02 -07001053 if (l2cap_pi(sk)->rx_busy_skb) {
1054 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
1055 l2cap_pi(sk)->rx_busy_skb = NULL;
1056 }
1057
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001058 skb_queue_purge(&sk->sk_receive_queue);
1059 skb_queue_purge(&sk->sk_write_queue);
1060}
1061
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001062static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001063{
1064 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001065 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001066
1067 BT_DBG("sk %p", sk);
1068
1069 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001070 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
1071
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001072 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -03001073 bt_sk(sk)->flags = bt_sk(parent)->flags;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001074
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001075 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001076 chan->imtu = pchan->imtu;
1077 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001078 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001079 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001080 chan->fcs = pchan->fcs;
1081 chan->max_tx = pchan->max_tx;
1082 chan->tx_win = pchan->tx_win;
Andrei Emeltchenko6b3c7102011-11-02 09:57:10 +02001083 chan->tx_win_max = pchan->tx_win_max;
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001084 chan->sec_level = pchan->sec_level;
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +03001085 chan->flags = pchan->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +00001086
1087 security_sk_clone(parent, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001088 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001089
1090 switch (sk->sk_type) {
1091 case SOCK_RAW:
1092 chan->chan_type = L2CAP_CHAN_RAW;
1093 break;
1094 case SOCK_DGRAM:
1095 chan->chan_type = L2CAP_CHAN_CONN_LESS;
1096 break;
1097 case SOCK_SEQPACKET:
1098 case SOCK_STREAM:
1099 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
1100 break;
1101 }
1102
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001103 chan->imtu = L2CAP_DEFAULT_MTU;
1104 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001105 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001106 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001107 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001108 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001109 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001110 }
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +03001111
1112 l2cap_chan_set_defaults(chan);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001113 }
1114
1115 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001116 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001117
1118 chan->data = sk;
1119 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001120}
1121
1122static struct proto l2cap_proto = {
1123 .name = "L2CAP",
1124 .owner = THIS_MODULE,
1125 .obj_size = sizeof(struct l2cap_pinfo)
1126};
1127
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001128static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001129{
1130 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001131 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001132
1133 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1134 if (!sk)
1135 return NULL;
1136
1137 sock_init_data(sock, sk);
1138 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1139
1140 sk->sk_destruct = l2cap_sock_destruct;
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08001141 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001142
1143 sock_reset_flag(sk, SOCK_ZAPPED);
1144
1145 sk->sk_protocol = proto;
1146 sk->sk_state = BT_OPEN;
1147
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001148 chan = l2cap_chan_create();
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001149 if (!chan) {
1150 l2cap_sock_kill(sk);
1151 return NULL;
1152 }
1153
Mat Martineau61d6ef32012-04-27 16:50:50 -07001154 l2cap_chan_hold(chan);
1155
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001156 chan->sk = sk;
1157
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001158 l2cap_pi(sk)->chan = chan;
1159
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001160 return sk;
1161}
1162
1163static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1164 int kern)
1165{
1166 struct sock *sk;
1167
1168 BT_DBG("sock %p", sock);
1169
1170 sock->state = SS_UNCONNECTED;
1171
1172 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1173 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1174 return -ESOCKTNOSUPPORT;
1175
1176 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1177 return -EPERM;
1178
1179 sock->ops = &l2cap_sock_ops;
1180
1181 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1182 if (!sk)
1183 return -ENOMEM;
1184
1185 l2cap_sock_init(sk, NULL);
1186 return 0;
1187}
1188
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -03001189static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001190 .family = PF_BLUETOOTH,
1191 .owner = THIS_MODULE,
1192 .release = l2cap_sock_release,
1193 .bind = l2cap_sock_bind,
1194 .connect = l2cap_sock_connect,
1195 .listen = l2cap_sock_listen,
1196 .accept = l2cap_sock_accept,
1197 .getname = l2cap_sock_getname,
1198 .sendmsg = l2cap_sock_sendmsg,
1199 .recvmsg = l2cap_sock_recvmsg,
1200 .poll = bt_sock_poll,
1201 .ioctl = bt_sock_ioctl,
1202 .mmap = sock_no_mmap,
1203 .socketpair = sock_no_socketpair,
1204 .shutdown = l2cap_sock_shutdown,
1205 .setsockopt = l2cap_sock_setsockopt,
1206 .getsockopt = l2cap_sock_getsockopt
1207};
1208
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001209static const struct net_proto_family l2cap_sock_family_ops = {
1210 .family = PF_BLUETOOTH,
1211 .owner = THIS_MODULE,
1212 .create = l2cap_sock_create,
1213};
1214
1215int __init l2cap_init_sockets(void)
1216{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001217 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001218
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001219 err = proto_register(&l2cap_proto, 0);
1220 if (err < 0)
1221 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001222
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001223 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1224 if (err < 0)
1225 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001226
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001227 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001228
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001229 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001230
1231error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001232 BT_ERR("L2CAP socket registration failed");
1233 proto_unregister(&l2cap_proto);
1234 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001235}
1236
1237void l2cap_cleanup_sockets(void)
1238{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001239 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1240 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001241
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001242 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001243}