blob: 4050edeaf78bd86909d45df55cdd781b7710e5f7 [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);
92 sk->sk_state = BT_BOUND;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020093
94done:
95 release_sock(sk);
96 return err;
97}
98
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -020099static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
100{
101 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300102 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200103 struct sockaddr_l2 la;
104 int len, err = 0;
105
106 BT_DBG("sk %p", sk);
107
108 if (!addr || alen < sizeof(addr->sa_family) ||
109 addr->sa_family != AF_BLUETOOTH)
110 return -EINVAL;
111
112 memset(&la, 0, sizeof(la));
113 len = min_t(unsigned int, sizeof(la), alen);
114 memcpy(&la, addr, len);
115
Ville Tervoacd7d372011-02-10 22:38:49 -0300116 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200117 return -EINVAL;
118
119 lock_sock(sk);
120
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300121 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED
Ville Tervoacd7d372011-02-10 22:38:49 -0300122 && !(la.l2_psm || la.l2_cid)) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200123 err = -EINVAL;
124 goto done;
125 }
126
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300127 switch (chan->mode) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200128 case L2CAP_MODE_BASIC:
129 break;
130 case L2CAP_MODE_ERTM:
131 case L2CAP_MODE_STREAMING:
132 if (!disable_ertm)
133 break;
134 /* fall through */
135 default:
136 err = -ENOTSUPP;
137 goto done;
138 }
139
140 switch (sk->sk_state) {
141 case BT_CONNECT:
142 case BT_CONNECT2:
143 case BT_CONFIG:
144 /* Already connecting */
145 goto wait;
146
147 case BT_CONNECTED:
148 /* Already connected */
149 err = -EISCONN;
150 goto done;
151
152 case BT_OPEN:
153 case BT_BOUND:
154 /* Can connect */
155 break;
156
157 default:
158 err = -EBADFD;
159 goto done;
160 }
161
162 /* PSM must be odd and lsb of upper byte must be 0 */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300163 if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 && !la.l2_cid &&
164 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200165 err = -EINVAL;
166 goto done;
167 }
168
169 /* Set destination address and psm */
170 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300171 chan->psm = la.l2_psm;
172 chan->dcid = la.l2_cid;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200173
Gustavo F. Padovan77a74c72011-04-12 18:17:14 -0300174 err = l2cap_chan_connect(l2cap_pi(sk)->chan);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200175 if (err)
176 goto done;
177
178wait:
179 err = bt_sock_wait_state(sk, BT_CONNECTED,
180 sock_sndtimeo(sk, flags & O_NONBLOCK));
181done:
182 release_sock(sk);
183 return err;
184}
185
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200186static int l2cap_sock_listen(struct socket *sock, int backlog)
187{
188 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300189 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200190 int err = 0;
191
192 BT_DBG("sk %p backlog %d", sk, backlog);
193
194 lock_sock(sk);
195
196 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
197 || sk->sk_state != BT_BOUND) {
198 err = -EBADFD;
199 goto done;
200 }
201
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300202 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200203 case L2CAP_MODE_BASIC:
204 break;
205 case L2CAP_MODE_ERTM:
206 case L2CAP_MODE_STREAMING:
207 if (!disable_ertm)
208 break;
209 /* fall through */
210 default:
211 err = -ENOTSUPP;
212 goto done;
213 }
214
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200215 sk->sk_max_ack_backlog = backlog;
216 sk->sk_ack_backlog = 0;
217 sk->sk_state = BT_LISTEN;
218
219done:
220 release_sock(sk);
221 return err;
222}
223
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200224static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
225{
226 DECLARE_WAITQUEUE(wait, current);
227 struct sock *sk = sock->sk, *nsk;
228 long timeo;
229 int err = 0;
230
231 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
232
233 if (sk->sk_state != BT_LISTEN) {
234 err = -EBADFD;
235 goto done;
236 }
237
238 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
239
240 BT_DBG("sk %p timeo %ld", sk, timeo);
241
242 /* Wait for an incoming connection. (wake-one). */
243 add_wait_queue_exclusive(sk_sleep(sk), &wait);
244 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
245 set_current_state(TASK_INTERRUPTIBLE);
246 if (!timeo) {
247 err = -EAGAIN;
248 break;
249 }
250
251 release_sock(sk);
252 timeo = schedule_timeout(timeo);
253 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
254
255 if (sk->sk_state != BT_LISTEN) {
256 err = -EBADFD;
257 break;
258 }
259
260 if (signal_pending(current)) {
261 err = sock_intr_errno(timeo);
262 break;
263 }
264 }
265 set_current_state(TASK_RUNNING);
266 remove_wait_queue(sk_sleep(sk), &wait);
267
268 if (err)
269 goto done;
270
271 newsock->state = SS_CONNECTED;
272
273 BT_DBG("new socket %p", nsk);
274
275done:
276 release_sock(sk);
277 return err;
278}
279
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200280static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
281{
282 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
283 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300284 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200285
286 BT_DBG("sock %p, sk %p", sock, sk);
287
288 addr->sa_family = AF_BLUETOOTH;
289 *len = sizeof(struct sockaddr_l2);
290
291 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300292 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200293 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300294 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200295 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300296 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200297 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300298 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200299 }
300
301 return 0;
302}
303
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200304static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
305{
306 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300307 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200308 struct l2cap_options opts;
309 struct l2cap_conninfo cinfo;
310 int len, err = 0;
311 u32 opt;
312
313 BT_DBG("sk %p", sk);
314
315 if (get_user(len, optlen))
316 return -EFAULT;
317
318 lock_sock(sk);
319
320 switch (optname) {
321 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300322 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300323 opts.imtu = chan->imtu;
324 opts.omtu = chan->omtu;
325 opts.flush_to = chan->flush_to;
326 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300327 opts.fcs = chan->fcs;
328 opts.max_tx = chan->max_tx;
329 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200330
331 len = min_t(unsigned int, len, sizeof(opts));
332 if (copy_to_user(optval, (char *) &opts, len))
333 err = -EFAULT;
334
335 break;
336
337 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300338 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200339 case BT_SECURITY_LOW:
340 opt = L2CAP_LM_AUTH;
341 break;
342 case BT_SECURITY_MEDIUM:
343 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
344 break;
345 case BT_SECURITY_HIGH:
346 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
347 L2CAP_LM_SECURE;
348 break;
349 default:
350 opt = 0;
351 break;
352 }
353
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300354 if (chan->role_switch)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200355 opt |= L2CAP_LM_MASTER;
356
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300357 if (chan->force_reliable)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200358 opt |= L2CAP_LM_RELIABLE;
359
360 if (put_user(opt, (u32 __user *) optval))
361 err = -EFAULT;
362 break;
363
364 case L2CAP_CONNINFO:
365 if (sk->sk_state != BT_CONNECTED &&
366 !(sk->sk_state == BT_CONNECT2 &&
367 bt_sk(sk)->defer_setup)) {
368 err = -ENOTCONN;
369 break;
370 }
371
Filip Palian8d03e972011-05-12 19:32:46 +0200372 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300373 cinfo.hci_handle = chan->conn->hcon->handle;
374 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200375
376 len = min_t(unsigned int, len, sizeof(cinfo));
377 if (copy_to_user(optval, (char *) &cinfo, len))
378 err = -EFAULT;
379
380 break;
381
382 default:
383 err = -ENOPROTOOPT;
384 break;
385 }
386
387 release_sock(sk);
388 return err;
389}
390
391static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
392{
393 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300394 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200395 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700396 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200397 int len, err = 0;
398
399 BT_DBG("sk %p", sk);
400
401 if (level == SOL_L2CAP)
402 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
403
404 if (level != SOL_BLUETOOTH)
405 return -ENOPROTOOPT;
406
407 if (get_user(len, optlen))
408 return -EFAULT;
409
410 lock_sock(sk);
411
412 switch (optname) {
413 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300414 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
415 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200416 err = -EINVAL;
417 break;
418 }
419
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300420 sec.level = chan->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200421
422 len = min_t(unsigned int, len, sizeof(sec));
423 if (copy_to_user(optval, (char *) &sec, len))
424 err = -EFAULT;
425
426 break;
427
428 case BT_DEFER_SETUP:
429 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
430 err = -EINVAL;
431 break;
432 }
433
434 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
435 err = -EFAULT;
436
437 break;
438
439 case BT_FLUSHABLE:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300440 if (put_user(chan->flushable, (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200441 err = -EFAULT;
442
443 break;
444
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700445 case BT_POWER:
446 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
447 && sk->sk_type != SOCK_RAW) {
448 err = -EINVAL;
449 break;
450 }
451
452 pwr.force_active = chan->force_active;
453
454 len = min_t(unsigned int, len, sizeof(pwr));
455 if (copy_to_user(optval, (char *) &pwr, len))
456 err = -EFAULT;
457
458 break;
459
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200460 default:
461 err = -ENOPROTOOPT;
462 break;
463 }
464
465 release_sock(sk);
466 return err;
467}
468
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200469static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
470{
471 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300472 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200473 struct l2cap_options opts;
474 int len, err = 0;
475 u32 opt;
476
477 BT_DBG("sk %p", sk);
478
479 lock_sock(sk);
480
481 switch (optname) {
482 case L2CAP_OPTIONS:
483 if (sk->sk_state == BT_CONNECTED) {
484 err = -EINVAL;
485 break;
486 }
487
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300488 opts.imtu = chan->imtu;
489 opts.omtu = chan->omtu;
490 opts.flush_to = chan->flush_to;
491 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300492 opts.fcs = chan->fcs;
493 opts.max_tx = chan->max_tx;
494 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200495
496 len = min_t(unsigned int, sizeof(opts), optlen);
497 if (copy_from_user((char *) &opts, optval, len)) {
498 err = -EFAULT;
499 break;
500 }
501
502 if (opts.txwin_size > L2CAP_DEFAULT_TX_WINDOW) {
503 err = -EINVAL;
504 break;
505 }
506
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300507 chan->mode = opts.mode;
508 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200509 case L2CAP_MODE_BASIC:
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300510 chan->conf_state &= ~L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200511 break;
512 case L2CAP_MODE_ERTM:
513 case L2CAP_MODE_STREAMING:
514 if (!disable_ertm)
515 break;
516 /* fall through */
517 default:
518 err = -EINVAL;
519 break;
520 }
521
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300522 chan->imtu = opts.imtu;
523 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300524 chan->fcs = opts.fcs;
525 chan->max_tx = opts.max_tx;
526 chan->tx_win = (__u8)opts.txwin_size;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200527 break;
528
529 case L2CAP_LM:
530 if (get_user(opt, (u32 __user *) optval)) {
531 err = -EFAULT;
532 break;
533 }
534
535 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300536 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200537 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300538 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200539 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300540 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200541
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300542 chan->role_switch = (opt & L2CAP_LM_MASTER);
543 chan->force_reliable = (opt & L2CAP_LM_RELIABLE);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200544 break;
545
546 default:
547 err = -ENOPROTOOPT;
548 break;
549 }
550
551 release_sock(sk);
552 return err;
553}
554
555static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
556{
557 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300558 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200559 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700560 struct bt_power pwr;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200561 int len, err = 0;
562 u32 opt;
563
564 BT_DBG("sk %p", sk);
565
566 if (level == SOL_L2CAP)
567 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
568
569 if (level != SOL_BLUETOOTH)
570 return -ENOPROTOOPT;
571
572 lock_sock(sk);
573
574 switch (optname) {
575 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300576 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
577 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200578 err = -EINVAL;
579 break;
580 }
581
582 sec.level = BT_SECURITY_LOW;
583
584 len = min_t(unsigned int, sizeof(sec), optlen);
585 if (copy_from_user((char *) &sec, optval, len)) {
586 err = -EFAULT;
587 break;
588 }
589
590 if (sec.level < BT_SECURITY_LOW ||
591 sec.level > BT_SECURITY_HIGH) {
592 err = -EINVAL;
593 break;
594 }
595
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300596 chan->sec_level = sec.level;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200597 break;
598
599 case BT_DEFER_SETUP:
600 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
601 err = -EINVAL;
602 break;
603 }
604
605 if (get_user(opt, (u32 __user *) optval)) {
606 err = -EFAULT;
607 break;
608 }
609
610 bt_sk(sk)->defer_setup = opt;
611 break;
612
613 case BT_FLUSHABLE:
614 if (get_user(opt, (u32 __user *) optval)) {
615 err = -EFAULT;
616 break;
617 }
618
619 if (opt > BT_FLUSHABLE_ON) {
620 err = -EINVAL;
621 break;
622 }
623
624 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300625 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300626 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200627 No Flush support in the LM */
628 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
629 err = -EINVAL;
630 break;
631 }
632 }
633
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300634 chan->flushable = opt;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200635 break;
636
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700637 case BT_POWER:
638 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
639 chan->chan_type != L2CAP_CHAN_RAW) {
640 err = -EINVAL;
641 break;
642 }
643
644 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
645
646 len = min_t(unsigned int, sizeof(pwr), optlen);
647 if (copy_from_user((char *) &pwr, optval, len)) {
648 err = -EFAULT;
649 break;
650 }
651 chan->force_active = pwr.force_active;
652 break;
653
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200654 default:
655 err = -ENOPROTOOPT;
656 break;
657 }
658
659 release_sock(sk);
660 return err;
661}
662
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200663static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
664{
665 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300666 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200667 int err;
668
669 BT_DBG("sock %p, sk %p", sock, sk);
670
671 err = sock_error(sk);
672 if (err)
673 return err;
674
675 if (msg->msg_flags & MSG_OOB)
676 return -EOPNOTSUPP;
677
678 lock_sock(sk);
679
680 if (sk->sk_state != BT_CONNECTED) {
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300681 release_sock(sk);
682 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200683 }
684
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300685 err = l2cap_chan_send(chan, msg, len);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200686
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200687 release_sock(sk);
688 return err;
689}
690
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200691static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
692{
693 struct sock *sk = sock->sk;
694
695 lock_sock(sk);
696
697 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300698 sk->sk_state = BT_CONFIG;
699
700 __l2cap_connect_rsp_defer(l2cap_pi(sk)->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200701 release_sock(sk);
702 return 0;
703 }
704
705 release_sock(sk);
706
707 if (sock->type == SOCK_STREAM)
708 return bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
709
710 return bt_sock_recvmsg(iocb, sock, msg, len, flags);
711}
712
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200713/* Kill socket (only if zapped and orphan)
714 * Must be called on unlocked socket.
715 */
716void l2cap_sock_kill(struct sock *sk)
717{
718 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
719 return;
720
721 BT_DBG("sk %p state %d", sk, sk->sk_state);
722
723 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300724
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300725 l2cap_chan_destroy(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200726 sock_set_flag(sk, SOCK_DEAD);
727 sock_put(sk);
728}
729
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200730static int l2cap_sock_shutdown(struct socket *sock, int how)
731{
732 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300733 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200734 int err = 0;
735
736 BT_DBG("sock %p, sk %p", sock, sk);
737
738 if (!sk)
739 return 0;
740
741 lock_sock(sk);
742 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300743 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200744 err = __l2cap_wait_ack(sk);
745
746 sk->sk_shutdown = SHUTDOWN_MASK;
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300747 l2cap_chan_close(chan, 0);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200748
749 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
750 err = bt_sock_wait_state(sk, BT_CLOSED,
751 sk->sk_lingertime);
752 }
753
754 if (!err && sk->sk_err)
755 err = -sk->sk_err;
756
757 release_sock(sk);
758 return err;
759}
760
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200761static int l2cap_sock_release(struct socket *sock)
762{
763 struct sock *sk = sock->sk;
764 int err;
765
766 BT_DBG("sock %p, sk %p", sock, sk);
767
768 if (!sk)
769 return 0;
770
771 err = l2cap_sock_shutdown(sock, 2);
772
773 sock_orphan(sk);
774 l2cap_sock_kill(sk);
775 return err;
776}
777
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300778static struct l2cap_chan *l2cap_sock_new_connection_cb(void *data)
779{
780 struct sock *sk, *parent = data;
781
782 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
783 GFP_ATOMIC);
784 if (!sk)
785 return NULL;
786
787 l2cap_sock_init(sk, parent);
788
789 return l2cap_pi(sk)->chan;
790}
791
792static struct l2cap_ops l2cap_chan_ops = {
793 .name = "L2CAP Socket Interface",
794 .new_connection = l2cap_sock_new_connection_cb,
795};
796
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200797static void l2cap_sock_destruct(struct sock *sk)
798{
799 BT_DBG("sk %p", sk);
800
801 skb_queue_purge(&sk->sk_receive_queue);
802 skb_queue_purge(&sk->sk_write_queue);
803}
804
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300805static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200806{
807 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300808 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200809
810 BT_DBG("sk %p", sk);
811
812 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300813 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
814
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200815 sk->sk_type = parent->sk_type;
816 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
817
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300818 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300819 chan->imtu = pchan->imtu;
820 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300821 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300822 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300823 chan->fcs = pchan->fcs;
824 chan->max_tx = pchan->max_tx;
825 chan->tx_win = pchan->tx_win;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300826 chan->sec_level = pchan->sec_level;
827 chan->role_switch = pchan->role_switch;
828 chan->force_reliable = pchan->force_reliable;
829 chan->flushable = pchan->flushable;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700830 chan->force_active = pchan->force_active;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200831 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300832
833 switch (sk->sk_type) {
834 case SOCK_RAW:
835 chan->chan_type = L2CAP_CHAN_RAW;
836 break;
837 case SOCK_DGRAM:
838 chan->chan_type = L2CAP_CHAN_CONN_LESS;
839 break;
840 case SOCK_SEQPACKET:
841 case SOCK_STREAM:
842 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
843 break;
844 }
845
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300846 chan->imtu = L2CAP_DEFAULT_MTU;
847 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200848 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300849 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300850 chan->conf_state |= L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200851 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300852 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200853 }
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300854 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
855 chan->fcs = L2CAP_FCS_CRC16;
856 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300857 chan->sec_level = BT_SECURITY_LOW;
858 chan->role_switch = 0;
859 chan->force_reliable = 0;
860 chan->flushable = BT_FLUSHABLE_OFF;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700861 chan->force_active = BT_POWER_FORCE_ACTIVE_ON;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300862
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200863 }
864
865 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300866 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300867
868 chan->data = sk;
869 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200870}
871
872static struct proto l2cap_proto = {
873 .name = "L2CAP",
874 .owner = THIS_MODULE,
875 .obj_size = sizeof(struct l2cap_pinfo)
876};
877
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300878static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200879{
880 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -0300881 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200882
883 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
884 if (!sk)
885 return NULL;
886
887 sock_init_data(sock, sk);
888 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
889
890 sk->sk_destruct = l2cap_sock_destruct;
891 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
892
893 sock_reset_flag(sk, SOCK_ZAPPED);
894
895 sk->sk_protocol = proto;
896 sk->sk_state = BT_OPEN;
897
Gustavo F. Padovandc50a062011-05-16 16:42:01 -0300898 chan = l2cap_chan_create(sk);
899 if (!chan) {
900 l2cap_sock_kill(sk);
901 return NULL;
902 }
903
904 l2cap_pi(sk)->chan = chan;
905
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200906 return sk;
907}
908
909static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
910 int kern)
911{
912 struct sock *sk;
913
914 BT_DBG("sock %p", sock);
915
916 sock->state = SS_UNCONNECTED;
917
918 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
919 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
920 return -ESOCKTNOSUPPORT;
921
922 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
923 return -EPERM;
924
925 sock->ops = &l2cap_sock_ops;
926
927 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
928 if (!sk)
929 return -ENOMEM;
930
931 l2cap_sock_init(sk, NULL);
932 return 0;
933}
934
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -0300935static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -0200936 .family = PF_BLUETOOTH,
937 .owner = THIS_MODULE,
938 .release = l2cap_sock_release,
939 .bind = l2cap_sock_bind,
940 .connect = l2cap_sock_connect,
941 .listen = l2cap_sock_listen,
942 .accept = l2cap_sock_accept,
943 .getname = l2cap_sock_getname,
944 .sendmsg = l2cap_sock_sendmsg,
945 .recvmsg = l2cap_sock_recvmsg,
946 .poll = bt_sock_poll,
947 .ioctl = bt_sock_ioctl,
948 .mmap = sock_no_mmap,
949 .socketpair = sock_no_socketpair,
950 .shutdown = l2cap_sock_shutdown,
951 .setsockopt = l2cap_sock_setsockopt,
952 .getsockopt = l2cap_sock_getsockopt
953};
954
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200955static const struct net_proto_family l2cap_sock_family_ops = {
956 .family = PF_BLUETOOTH,
957 .owner = THIS_MODULE,
958 .create = l2cap_sock_create,
959};
960
961int __init l2cap_init_sockets(void)
962{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300963 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200964
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300965 err = proto_register(&l2cap_proto, 0);
966 if (err < 0)
967 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200968
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300969 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
970 if (err < 0)
971 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200972
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300973 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200974
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300975 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200976
977error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300978 BT_ERR("L2CAP socket registration failed");
979 proto_unregister(&l2cap_proto);
980 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200981}
982
983void l2cap_cleanup_sockets(void)
984{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300985 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
986 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200987
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300988 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200989}