blob: 1d9c36509d7bc524c6df622f419cda2984a95612 [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.
6
7 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 2 as
11 published by the Free Software Foundation;
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
16 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
17 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
18 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21
22 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
23 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
24 SOFTWARE IS DISCLAIMED.
25*/
26
27/* Bluetooth L2CAP sockets. */
28
29#include <net/bluetooth/bluetooth.h>
Gustavo F. Padovan33575df2011-02-04 02:48:48 -020030#include <net/bluetooth/hci_core.h>
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020031#include <net/bluetooth/l2cap.h>
32
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030033static const struct proto_ops l2cap_sock_ops;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -030034static void l2cap_sock_init(struct sock *sk, struct sock *parent);
35static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio);
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030036
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020037static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
38{
39 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -030040 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020041 struct sockaddr_l2 la;
42 int len, err = 0;
43
44 BT_DBG("sk %p", sk);
45
46 if (!addr || addr->sa_family != AF_BLUETOOTH)
47 return -EINVAL;
48
49 memset(&la, 0, sizeof(la));
50 len = min_t(unsigned int, sizeof(la), alen);
51 memcpy(&la, addr, len);
52
Ville Tervob62f3282011-02-10 22:38:50 -030053 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020054 return -EINVAL;
55
56 lock_sock(sk);
57
58 if (sk->sk_state != BT_OPEN) {
59 err = -EBADFD;
60 goto done;
61 }
62
63 if (la.l2_psm) {
64 __u16 psm = __le16_to_cpu(la.l2_psm);
65
66 /* PSM must be odd and lsb of upper byte must be 0 */
67 if ((psm & 0x0101) != 0x0001) {
68 err = -EINVAL;
69 goto done;
70 }
71
72 /* Restrict usage of well-known PSMs */
73 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
74 err = -EACCES;
75 goto done;
76 }
77 }
78
Ville Tervob62f3282011-02-10 22:38:50 -030079 if (la.l2_cid)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030080 err = l2cap_add_scid(chan, la.l2_cid);
81 else
82 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
Ville Tervob62f3282011-02-10 22:38:50 -030083
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030084 if (err < 0)
85 goto done;
86
87 if (__le16_to_cpu(la.l2_psm) == 0x0001 ||
88 __le16_to_cpu(la.l2_psm) == 0x0003)
89 chan->sec_level = BT_SECURITY_SDP;
90
91 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -030092
93 chan->state = BT_BOUND;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030094 sk->sk_state = BT_BOUND;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020095
96done:
97 release_sock(sk);
98 return err;
99}
100
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200101static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
102{
103 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300104 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200105 struct sockaddr_l2 la;
106 int len, err = 0;
107
108 BT_DBG("sk %p", sk);
109
110 if (!addr || alen < sizeof(addr->sa_family) ||
111 addr->sa_family != AF_BLUETOOTH)
112 return -EINVAL;
113
114 memset(&la, 0, sizeof(la));
115 len = min_t(unsigned int, sizeof(la), alen);
116 memcpy(&la, addr, len);
117
Ville Tervoacd7d372011-02-10 22:38:49 -0300118 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200119 return -EINVAL;
120
121 lock_sock(sk);
122
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300123 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED
Ville Tervoacd7d372011-02-10 22:38:49 -0300124 && !(la.l2_psm || la.l2_cid)) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200125 err = -EINVAL;
126 goto done;
127 }
128
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300129 switch (chan->mode) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200130 case L2CAP_MODE_BASIC:
131 break;
132 case L2CAP_MODE_ERTM:
133 case L2CAP_MODE_STREAMING:
134 if (!disable_ertm)
135 break;
136 /* fall through */
137 default:
138 err = -ENOTSUPP;
139 goto done;
140 }
141
142 switch (sk->sk_state) {
143 case BT_CONNECT:
144 case BT_CONNECT2:
145 case BT_CONFIG:
146 /* Already connecting */
147 goto wait;
148
149 case BT_CONNECTED:
150 /* Already connected */
151 err = -EISCONN;
152 goto done;
153
154 case BT_OPEN:
155 case BT_BOUND:
156 /* Can connect */
157 break;
158
159 default:
160 err = -EBADFD;
161 goto done;
162 }
163
164 /* PSM must be odd and lsb of upper byte must be 0 */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300165 if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 && !la.l2_cid &&
166 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200167 err = -EINVAL;
168 goto done;
169 }
170
171 /* Set destination address and psm */
172 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300173 chan->psm = la.l2_psm;
174 chan->dcid = la.l2_cid;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200175
Gustavo F. Padovan77a74c72011-04-12 18:17:14 -0300176 err = l2cap_chan_connect(l2cap_pi(sk)->chan);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200177 if (err)
178 goto done;
179
180wait:
181 err = bt_sock_wait_state(sk, BT_CONNECTED,
182 sock_sndtimeo(sk, flags & O_NONBLOCK));
183done:
184 release_sock(sk);
185 return err;
186}
187
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200188static int l2cap_sock_listen(struct socket *sock, int backlog)
189{
190 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300191 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200192 int err = 0;
193
194 BT_DBG("sk %p backlog %d", sk, backlog);
195
196 lock_sock(sk);
197
198 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
199 || sk->sk_state != BT_BOUND) {
200 err = -EBADFD;
201 goto done;
202 }
203
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300204 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200205 case L2CAP_MODE_BASIC:
206 break;
207 case L2CAP_MODE_ERTM:
208 case L2CAP_MODE_STREAMING:
209 if (!disable_ertm)
210 break;
211 /* fall through */
212 default:
213 err = -ENOTSUPP;
214 goto done;
215 }
216
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200217 sk->sk_max_ack_backlog = backlog;
218 sk->sk_ack_backlog = 0;
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300219
220 chan->state = BT_LISTEN;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200221 sk->sk_state = BT_LISTEN;
222
223done:
224 release_sock(sk);
225 return err;
226}
227
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200228static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
229{
230 DECLARE_WAITQUEUE(wait, current);
231 struct sock *sk = sock->sk, *nsk;
232 long timeo;
233 int err = 0;
234
235 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
236
237 if (sk->sk_state != BT_LISTEN) {
238 err = -EBADFD;
239 goto done;
240 }
241
242 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
243
244 BT_DBG("sk %p timeo %ld", sk, timeo);
245
246 /* Wait for an incoming connection. (wake-one). */
247 add_wait_queue_exclusive(sk_sleep(sk), &wait);
248 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
249 set_current_state(TASK_INTERRUPTIBLE);
250 if (!timeo) {
251 err = -EAGAIN;
252 break;
253 }
254
255 release_sock(sk);
256 timeo = schedule_timeout(timeo);
257 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
258
259 if (sk->sk_state != BT_LISTEN) {
260 err = -EBADFD;
261 break;
262 }
263
264 if (signal_pending(current)) {
265 err = sock_intr_errno(timeo);
266 break;
267 }
268 }
269 set_current_state(TASK_RUNNING);
270 remove_wait_queue(sk_sleep(sk), &wait);
271
272 if (err)
273 goto done;
274
275 newsock->state = SS_CONNECTED;
276
277 BT_DBG("new socket %p", nsk);
278
279done:
280 release_sock(sk);
281 return err;
282}
283
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200284static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
285{
286 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
287 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300288 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200289
290 BT_DBG("sock %p, sk %p", sock, sk);
291
292 addr->sa_family = AF_BLUETOOTH;
293 *len = sizeof(struct sockaddr_l2);
294
295 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300296 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200297 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300298 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200299 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300300 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200301 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300302 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200303 }
304
305 return 0;
306}
307
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200308static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
309{
310 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300311 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200312 struct l2cap_options opts;
313 struct l2cap_conninfo cinfo;
314 int len, err = 0;
315 u32 opt;
316
317 BT_DBG("sk %p", sk);
318
319 if (get_user(len, optlen))
320 return -EFAULT;
321
322 lock_sock(sk);
323
324 switch (optname) {
325 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300326 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300327 opts.imtu = chan->imtu;
328 opts.omtu = chan->omtu;
329 opts.flush_to = chan->flush_to;
330 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300331 opts.fcs = chan->fcs;
332 opts.max_tx = chan->max_tx;
333 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200334
335 len = min_t(unsigned int, len, sizeof(opts));
336 if (copy_to_user(optval, (char *) &opts, len))
337 err = -EFAULT;
338
339 break;
340
341 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300342 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200343 case BT_SECURITY_LOW:
344 opt = L2CAP_LM_AUTH;
345 break;
346 case BT_SECURITY_MEDIUM:
347 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
348 break;
349 case BT_SECURITY_HIGH:
350 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
351 L2CAP_LM_SECURE;
352 break;
353 default:
354 opt = 0;
355 break;
356 }
357
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300358 if (chan->role_switch)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200359 opt |= L2CAP_LM_MASTER;
360
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300361 if (chan->force_reliable)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200362 opt |= L2CAP_LM_RELIABLE;
363
364 if (put_user(opt, (u32 __user *) optval))
365 err = -EFAULT;
366 break;
367
368 case L2CAP_CONNINFO:
369 if (sk->sk_state != BT_CONNECTED &&
370 !(sk->sk_state == BT_CONNECT2 &&
371 bt_sk(sk)->defer_setup)) {
372 err = -ENOTCONN;
373 break;
374 }
375
Filip Palian8d03e972011-05-12 19:32:46 +0200376 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300377 cinfo.hci_handle = chan->conn->hcon->handle;
378 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200379
380 len = min_t(unsigned int, len, sizeof(cinfo));
381 if (copy_to_user(optval, (char *) &cinfo, len))
382 err = -EFAULT;
383
384 break;
385
386 default:
387 err = -ENOPROTOOPT;
388 break;
389 }
390
391 release_sock(sk);
392 return err;
393}
394
395static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
396{
397 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300398 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200399 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700400 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200401 int len, err = 0;
402
403 BT_DBG("sk %p", sk);
404
405 if (level == SOL_L2CAP)
406 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
407
408 if (level != SOL_BLUETOOTH)
409 return -ENOPROTOOPT;
410
411 if (get_user(len, optlen))
412 return -EFAULT;
413
414 lock_sock(sk);
415
416 switch (optname) {
417 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300418 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
419 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200420 err = -EINVAL;
421 break;
422 }
423
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300424 sec.level = chan->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200425
426 len = min_t(unsigned int, len, sizeof(sec));
427 if (copy_to_user(optval, (char *) &sec, len))
428 err = -EFAULT;
429
430 break;
431
432 case BT_DEFER_SETUP:
433 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
434 err = -EINVAL;
435 break;
436 }
437
438 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
439 err = -EFAULT;
440
441 break;
442
443 case BT_FLUSHABLE:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300444 if (put_user(chan->flushable, (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200445 err = -EFAULT;
446
447 break;
448
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700449 case BT_POWER:
450 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
451 && sk->sk_type != SOCK_RAW) {
452 err = -EINVAL;
453 break;
454 }
455
456 pwr.force_active = chan->force_active;
457
458 len = min_t(unsigned int, len, sizeof(pwr));
459 if (copy_to_user(optval, (char *) &pwr, len))
460 err = -EFAULT;
461
462 break;
463
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200464 default:
465 err = -ENOPROTOOPT;
466 break;
467 }
468
469 release_sock(sk);
470 return err;
471}
472
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200473static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
474{
475 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300476 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200477 struct l2cap_options opts;
478 int len, err = 0;
479 u32 opt;
480
481 BT_DBG("sk %p", sk);
482
483 lock_sock(sk);
484
485 switch (optname) {
486 case L2CAP_OPTIONS:
487 if (sk->sk_state == BT_CONNECTED) {
488 err = -EINVAL;
489 break;
490 }
491
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300492 opts.imtu = chan->imtu;
493 opts.omtu = chan->omtu;
494 opts.flush_to = chan->flush_to;
495 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300496 opts.fcs = chan->fcs;
497 opts.max_tx = chan->max_tx;
498 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200499
500 len = min_t(unsigned int, sizeof(opts), optlen);
501 if (copy_from_user((char *) &opts, optval, len)) {
502 err = -EFAULT;
503 break;
504 }
505
506 if (opts.txwin_size > L2CAP_DEFAULT_TX_WINDOW) {
507 err = -EINVAL;
508 break;
509 }
510
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300511 chan->mode = opts.mode;
512 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200513 case L2CAP_MODE_BASIC:
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300514 chan->conf_state &= ~L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200515 break;
516 case L2CAP_MODE_ERTM:
517 case L2CAP_MODE_STREAMING:
518 if (!disable_ertm)
519 break;
520 /* fall through */
521 default:
522 err = -EINVAL;
523 break;
524 }
525
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300526 chan->imtu = opts.imtu;
527 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300528 chan->fcs = opts.fcs;
529 chan->max_tx = opts.max_tx;
530 chan->tx_win = (__u8)opts.txwin_size;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200531 break;
532
533 case L2CAP_LM:
534 if (get_user(opt, (u32 __user *) optval)) {
535 err = -EFAULT;
536 break;
537 }
538
539 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300540 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200541 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300542 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200543 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300544 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200545
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300546 chan->role_switch = (opt & L2CAP_LM_MASTER);
547 chan->force_reliable = (opt & L2CAP_LM_RELIABLE);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200548 break;
549
550 default:
551 err = -ENOPROTOOPT;
552 break;
553 }
554
555 release_sock(sk);
556 return err;
557}
558
559static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
560{
561 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300562 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200563 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700564 struct bt_power pwr;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200565 int len, err = 0;
566 u32 opt;
567
568 BT_DBG("sk %p", sk);
569
570 if (level == SOL_L2CAP)
571 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
572
573 if (level != SOL_BLUETOOTH)
574 return -ENOPROTOOPT;
575
576 lock_sock(sk);
577
578 switch (optname) {
579 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300580 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
581 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200582 err = -EINVAL;
583 break;
584 }
585
586 sec.level = BT_SECURITY_LOW;
587
588 len = min_t(unsigned int, sizeof(sec), optlen);
589 if (copy_from_user((char *) &sec, optval, len)) {
590 err = -EFAULT;
591 break;
592 }
593
594 if (sec.level < BT_SECURITY_LOW ||
595 sec.level > BT_SECURITY_HIGH) {
596 err = -EINVAL;
597 break;
598 }
599
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300600 chan->sec_level = sec.level;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200601 break;
602
603 case BT_DEFER_SETUP:
604 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
605 err = -EINVAL;
606 break;
607 }
608
609 if (get_user(opt, (u32 __user *) optval)) {
610 err = -EFAULT;
611 break;
612 }
613
614 bt_sk(sk)->defer_setup = opt;
615 break;
616
617 case BT_FLUSHABLE:
618 if (get_user(opt, (u32 __user *) optval)) {
619 err = -EFAULT;
620 break;
621 }
622
623 if (opt > BT_FLUSHABLE_ON) {
624 err = -EINVAL;
625 break;
626 }
627
628 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300629 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300630 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200631 No Flush support in the LM */
632 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
633 err = -EINVAL;
634 break;
635 }
636 }
637
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300638 chan->flushable = opt;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200639 break;
640
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700641 case BT_POWER:
642 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
643 chan->chan_type != L2CAP_CHAN_RAW) {
644 err = -EINVAL;
645 break;
646 }
647
648 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
649
650 len = min_t(unsigned int, sizeof(pwr), optlen);
651 if (copy_from_user((char *) &pwr, optval, len)) {
652 err = -EFAULT;
653 break;
654 }
655 chan->force_active = pwr.force_active;
656 break;
657
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200658 default:
659 err = -ENOPROTOOPT;
660 break;
661 }
662
663 release_sock(sk);
664 return err;
665}
666
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200667static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
668{
669 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300670 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200671 int err;
672
673 BT_DBG("sock %p, sk %p", sock, sk);
674
675 err = sock_error(sk);
676 if (err)
677 return err;
678
679 if (msg->msg_flags & MSG_OOB)
680 return -EOPNOTSUPP;
681
682 lock_sock(sk);
683
684 if (sk->sk_state != BT_CONNECTED) {
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300685 release_sock(sk);
686 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200687 }
688
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300689 err = l2cap_chan_send(chan, msg, len);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200690
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200691 release_sock(sk);
692 return err;
693}
694
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200695static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
696{
697 struct sock *sk = sock->sk;
698
699 lock_sock(sk);
700
701 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300702 sk->sk_state = BT_CONFIG;
703
704 __l2cap_connect_rsp_defer(l2cap_pi(sk)->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200705 release_sock(sk);
706 return 0;
707 }
708
709 release_sock(sk);
710
711 if (sock->type == SOCK_STREAM)
712 return bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
713
714 return bt_sock_recvmsg(iocb, sock, msg, len, flags);
715}
716
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200717/* Kill socket (only if zapped and orphan)
718 * Must be called on unlocked socket.
719 */
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300720static void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200721{
722 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
723 return;
724
725 BT_DBG("sk %p state %d", sk, sk->sk_state);
726
727 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300728
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300729 l2cap_chan_destroy(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200730 sock_set_flag(sk, SOCK_DEAD);
731 sock_put(sk);
732}
733
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200734static int l2cap_sock_shutdown(struct socket *sock, int how)
735{
736 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300737 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200738 int err = 0;
739
740 BT_DBG("sock %p, sk %p", sock, sk);
741
742 if (!sk)
743 return 0;
744
745 lock_sock(sk);
746 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300747 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200748 err = __l2cap_wait_ack(sk);
749
750 sk->sk_shutdown = SHUTDOWN_MASK;
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300751 l2cap_chan_close(chan, 0);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200752
753 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
754 err = bt_sock_wait_state(sk, BT_CLOSED,
755 sk->sk_lingertime);
756 }
757
758 if (!err && sk->sk_err)
759 err = -sk->sk_err;
760
761 release_sock(sk);
762 return err;
763}
764
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200765static int l2cap_sock_release(struct socket *sock)
766{
767 struct sock *sk = sock->sk;
768 int err;
769
770 BT_DBG("sock %p, sk %p", sock, sk);
771
772 if (!sk)
773 return 0;
774
775 err = l2cap_sock_shutdown(sock, 2);
776
777 sock_orphan(sk);
778 l2cap_sock_kill(sk);
779 return err;
780}
781
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300782static struct l2cap_chan *l2cap_sock_new_connection_cb(void *data)
783{
784 struct sock *sk, *parent = data;
785
786 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
787 GFP_ATOMIC);
788 if (!sk)
789 return NULL;
790
791 l2cap_sock_init(sk, parent);
792
793 return l2cap_pi(sk)->chan;
794}
795
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300796static int l2cap_sock_recv_cb(void *data, struct sk_buff *skb)
797{
798 struct sock *sk = data;
799
800 return sock_queue_rcv_skb(sk, skb);
801}
802
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300803static void l2cap_sock_close_cb(void *data)
804{
805 struct sock *sk = data;
806
807 l2cap_sock_kill(sk);
808}
809
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300810static void l2cap_sock_state_change_cb(void *data, int state)
811{
812 struct sock *sk = data;
813
814 sk->sk_state = state;
815}
816
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300817static struct l2cap_ops l2cap_chan_ops = {
818 .name = "L2CAP Socket Interface",
819 .new_connection = l2cap_sock_new_connection_cb,
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300820 .recv = l2cap_sock_recv_cb,
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300821 .close = l2cap_sock_close_cb,
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300822 .state_change = l2cap_sock_state_change_cb,
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300823};
824
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200825static void l2cap_sock_destruct(struct sock *sk)
826{
827 BT_DBG("sk %p", sk);
828
829 skb_queue_purge(&sk->sk_receive_queue);
830 skb_queue_purge(&sk->sk_write_queue);
831}
832
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300833static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200834{
835 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300836 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200837
838 BT_DBG("sk %p", sk);
839
840 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300841 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
842
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200843 sk->sk_type = parent->sk_type;
844 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
845
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300846 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300847 chan->imtu = pchan->imtu;
848 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300849 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300850 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300851 chan->fcs = pchan->fcs;
852 chan->max_tx = pchan->max_tx;
853 chan->tx_win = pchan->tx_win;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300854 chan->sec_level = pchan->sec_level;
855 chan->role_switch = pchan->role_switch;
856 chan->force_reliable = pchan->force_reliable;
857 chan->flushable = pchan->flushable;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700858 chan->force_active = pchan->force_active;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200859 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300860
861 switch (sk->sk_type) {
862 case SOCK_RAW:
863 chan->chan_type = L2CAP_CHAN_RAW;
864 break;
865 case SOCK_DGRAM:
866 chan->chan_type = L2CAP_CHAN_CONN_LESS;
867 break;
868 case SOCK_SEQPACKET:
869 case SOCK_STREAM:
870 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
871 break;
872 }
873
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300874 chan->imtu = L2CAP_DEFAULT_MTU;
875 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200876 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300877 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300878 chan->conf_state |= L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200879 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300880 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200881 }
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300882 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
883 chan->fcs = L2CAP_FCS_CRC16;
884 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300885 chan->sec_level = BT_SECURITY_LOW;
886 chan->role_switch = 0;
887 chan->force_reliable = 0;
888 chan->flushable = BT_FLUSHABLE_OFF;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700889 chan->force_active = BT_POWER_FORCE_ACTIVE_ON;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300890
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200891 }
892
893 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300894 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300895
896 chan->data = sk;
897 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200898}
899
900static struct proto l2cap_proto = {
901 .name = "L2CAP",
902 .owner = THIS_MODULE,
903 .obj_size = sizeof(struct l2cap_pinfo)
904};
905
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300906static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200907{
908 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -0300909 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200910
911 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
912 if (!sk)
913 return NULL;
914
915 sock_init_data(sock, sk);
916 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
917
918 sk->sk_destruct = l2cap_sock_destruct;
919 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
920
921 sock_reset_flag(sk, SOCK_ZAPPED);
922
923 sk->sk_protocol = proto;
924 sk->sk_state = BT_OPEN;
925
Gustavo F. Padovandc50a062011-05-16 16:42:01 -0300926 chan = l2cap_chan_create(sk);
927 if (!chan) {
928 l2cap_sock_kill(sk);
929 return NULL;
930 }
931
932 l2cap_pi(sk)->chan = chan;
933
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200934 return sk;
935}
936
937static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
938 int kern)
939{
940 struct sock *sk;
941
942 BT_DBG("sock %p", sock);
943
944 sock->state = SS_UNCONNECTED;
945
946 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
947 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
948 return -ESOCKTNOSUPPORT;
949
950 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
951 return -EPERM;
952
953 sock->ops = &l2cap_sock_ops;
954
955 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
956 if (!sk)
957 return -ENOMEM;
958
959 l2cap_sock_init(sk, NULL);
960 return 0;
961}
962
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -0300963static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -0200964 .family = PF_BLUETOOTH,
965 .owner = THIS_MODULE,
966 .release = l2cap_sock_release,
967 .bind = l2cap_sock_bind,
968 .connect = l2cap_sock_connect,
969 .listen = l2cap_sock_listen,
970 .accept = l2cap_sock_accept,
971 .getname = l2cap_sock_getname,
972 .sendmsg = l2cap_sock_sendmsg,
973 .recvmsg = l2cap_sock_recvmsg,
974 .poll = bt_sock_poll,
975 .ioctl = bt_sock_ioctl,
976 .mmap = sock_no_mmap,
977 .socketpair = sock_no_socketpair,
978 .shutdown = l2cap_sock_shutdown,
979 .setsockopt = l2cap_sock_setsockopt,
980 .getsockopt = l2cap_sock_getsockopt
981};
982
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200983static const struct net_proto_family l2cap_sock_family_ops = {
984 .family = PF_BLUETOOTH,
985 .owner = THIS_MODULE,
986 .create = l2cap_sock_create,
987};
988
989int __init l2cap_init_sockets(void)
990{
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -0300991 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200992
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -0300993 err = proto_register(&l2cap_proto, 0);
994 if (err < 0)
995 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200996
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -0300997 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
998 if (err < 0)
999 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001000
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001001 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001002
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001003 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001004
1005error:
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001006 BT_ERR("L2CAP socket registration failed");
1007 proto_unregister(&l2cap_proto);
1008 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001009}
1010
1011void l2cap_cleanup_sockets(void)
1012{
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001013 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1014 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001015
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001016 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001017}