blob: 2f4fd5751a46811923d284d52b047ea86558c916 [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;
34
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020035static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
36{
37 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -030038 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020039 struct sockaddr_l2 la;
40 int len, err = 0;
41
42 BT_DBG("sk %p", sk);
43
44 if (!addr || addr->sa_family != AF_BLUETOOTH)
45 return -EINVAL;
46
47 memset(&la, 0, sizeof(la));
48 len = min_t(unsigned int, sizeof(la), alen);
49 memcpy(&la, addr, len);
50
Ville Tervob62f3282011-02-10 22:38:50 -030051 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020052 return -EINVAL;
53
54 lock_sock(sk);
55
56 if (sk->sk_state != BT_OPEN) {
57 err = -EBADFD;
58 goto done;
59 }
60
61 if (la.l2_psm) {
62 __u16 psm = __le16_to_cpu(la.l2_psm);
63
64 /* PSM must be odd and lsb of upper byte must be 0 */
65 if ((psm & 0x0101) != 0x0001) {
66 err = -EINVAL;
67 goto done;
68 }
69
70 /* Restrict usage of well-known PSMs */
71 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
72 err = -EACCES;
73 goto done;
74 }
75 }
76
Ville Tervob62f3282011-02-10 22:38:50 -030077 if (la.l2_cid)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030078 err = l2cap_add_scid(chan, la.l2_cid);
79 else
80 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
Ville Tervob62f3282011-02-10 22:38:50 -030081
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030082 if (err < 0)
83 goto done;
84
85 if (__le16_to_cpu(la.l2_psm) == 0x0001 ||
86 __le16_to_cpu(la.l2_psm) == 0x0003)
87 chan->sec_level = BT_SECURITY_SDP;
88
89 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
90 sk->sk_state = BT_BOUND;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020091
92done:
93 release_sock(sk);
94 return err;
95}
96
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -020097static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
98{
99 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300100 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200101 struct sockaddr_l2 la;
102 int len, err = 0;
103
104 BT_DBG("sk %p", sk);
105
106 if (!addr || alen < sizeof(addr->sa_family) ||
107 addr->sa_family != AF_BLUETOOTH)
108 return -EINVAL;
109
110 memset(&la, 0, sizeof(la));
111 len = min_t(unsigned int, sizeof(la), alen);
112 memcpy(&la, addr, len);
113
Ville Tervoacd7d372011-02-10 22:38:49 -0300114 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200115 return -EINVAL;
116
117 lock_sock(sk);
118
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300119 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED
Ville Tervoacd7d372011-02-10 22:38:49 -0300120 && !(la.l2_psm || la.l2_cid)) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200121 err = -EINVAL;
122 goto done;
123 }
124
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300125 switch (chan->mode) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200126 case L2CAP_MODE_BASIC:
127 break;
128 case L2CAP_MODE_ERTM:
129 case L2CAP_MODE_STREAMING:
130 if (!disable_ertm)
131 break;
132 /* fall through */
133 default:
134 err = -ENOTSUPP;
135 goto done;
136 }
137
138 switch (sk->sk_state) {
139 case BT_CONNECT:
140 case BT_CONNECT2:
141 case BT_CONFIG:
142 /* Already connecting */
143 goto wait;
144
145 case BT_CONNECTED:
146 /* Already connected */
147 err = -EISCONN;
148 goto done;
149
150 case BT_OPEN:
151 case BT_BOUND:
152 /* Can connect */
153 break;
154
155 default:
156 err = -EBADFD;
157 goto done;
158 }
159
160 /* PSM must be odd and lsb of upper byte must be 0 */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300161 if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 && !la.l2_cid &&
162 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200163 err = -EINVAL;
164 goto done;
165 }
166
167 /* Set destination address and psm */
168 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300169 chan->psm = la.l2_psm;
170 chan->dcid = la.l2_cid;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200171
Gustavo F. Padovan77a74c72011-04-12 18:17:14 -0300172 err = l2cap_chan_connect(l2cap_pi(sk)->chan);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200173 if (err)
174 goto done;
175
176wait:
177 err = bt_sock_wait_state(sk, BT_CONNECTED,
178 sock_sndtimeo(sk, flags & O_NONBLOCK));
179done:
180 release_sock(sk);
181 return err;
182}
183
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200184static int l2cap_sock_listen(struct socket *sock, int backlog)
185{
186 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300187 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200188 int err = 0;
189
190 BT_DBG("sk %p backlog %d", sk, backlog);
191
192 lock_sock(sk);
193
194 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
195 || sk->sk_state != BT_BOUND) {
196 err = -EBADFD;
197 goto done;
198 }
199
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300200 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200201 case L2CAP_MODE_BASIC:
202 break;
203 case L2CAP_MODE_ERTM:
204 case L2CAP_MODE_STREAMING:
205 if (!disable_ertm)
206 break;
207 /* fall through */
208 default:
209 err = -ENOTSUPP;
210 goto done;
211 }
212
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200213 sk->sk_max_ack_backlog = backlog;
214 sk->sk_ack_backlog = 0;
215 sk->sk_state = BT_LISTEN;
216
217done:
218 release_sock(sk);
219 return err;
220}
221
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200222static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
223{
224 DECLARE_WAITQUEUE(wait, current);
225 struct sock *sk = sock->sk, *nsk;
226 long timeo;
227 int err = 0;
228
229 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
230
231 if (sk->sk_state != BT_LISTEN) {
232 err = -EBADFD;
233 goto done;
234 }
235
236 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
237
238 BT_DBG("sk %p timeo %ld", sk, timeo);
239
240 /* Wait for an incoming connection. (wake-one). */
241 add_wait_queue_exclusive(sk_sleep(sk), &wait);
242 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
243 set_current_state(TASK_INTERRUPTIBLE);
244 if (!timeo) {
245 err = -EAGAIN;
246 break;
247 }
248
249 release_sock(sk);
250 timeo = schedule_timeout(timeo);
251 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
252
253 if (sk->sk_state != BT_LISTEN) {
254 err = -EBADFD;
255 break;
256 }
257
258 if (signal_pending(current)) {
259 err = sock_intr_errno(timeo);
260 break;
261 }
262 }
263 set_current_state(TASK_RUNNING);
264 remove_wait_queue(sk_sleep(sk), &wait);
265
266 if (err)
267 goto done;
268
269 newsock->state = SS_CONNECTED;
270
271 BT_DBG("new socket %p", nsk);
272
273done:
274 release_sock(sk);
275 return err;
276}
277
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200278static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
279{
280 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
281 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300282 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200283
284 BT_DBG("sock %p, sk %p", sock, sk);
285
286 addr->sa_family = AF_BLUETOOTH;
287 *len = sizeof(struct sockaddr_l2);
288
289 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300290 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200291 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300292 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200293 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300294 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200295 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300296 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200297 }
298
299 return 0;
300}
301
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200302static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
303{
304 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300305 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200306 struct l2cap_options opts;
307 struct l2cap_conninfo cinfo;
308 int len, err = 0;
309 u32 opt;
310
311 BT_DBG("sk %p", sk);
312
313 if (get_user(len, optlen))
314 return -EFAULT;
315
316 lock_sock(sk);
317
318 switch (optname) {
319 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300320 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300321 opts.imtu = chan->imtu;
322 opts.omtu = chan->omtu;
323 opts.flush_to = chan->flush_to;
324 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300325 opts.fcs = chan->fcs;
326 opts.max_tx = chan->max_tx;
327 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200328
329 len = min_t(unsigned int, len, sizeof(opts));
330 if (copy_to_user(optval, (char *) &opts, len))
331 err = -EFAULT;
332
333 break;
334
335 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300336 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200337 case BT_SECURITY_LOW:
338 opt = L2CAP_LM_AUTH;
339 break;
340 case BT_SECURITY_MEDIUM:
341 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
342 break;
343 case BT_SECURITY_HIGH:
344 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
345 L2CAP_LM_SECURE;
346 break;
347 default:
348 opt = 0;
349 break;
350 }
351
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300352 if (chan->role_switch)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200353 opt |= L2CAP_LM_MASTER;
354
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300355 if (chan->force_reliable)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200356 opt |= L2CAP_LM_RELIABLE;
357
358 if (put_user(opt, (u32 __user *) optval))
359 err = -EFAULT;
360 break;
361
362 case L2CAP_CONNINFO:
363 if (sk->sk_state != BT_CONNECTED &&
364 !(sk->sk_state == BT_CONNECT2 &&
365 bt_sk(sk)->defer_setup)) {
366 err = -ENOTCONN;
367 break;
368 }
369
Filip Palian8d03e972011-05-12 19:32:46 +0200370 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300371 cinfo.hci_handle = chan->conn->hcon->handle;
372 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200373
374 len = min_t(unsigned int, len, sizeof(cinfo));
375 if (copy_to_user(optval, (char *) &cinfo, len))
376 err = -EFAULT;
377
378 break;
379
380 default:
381 err = -ENOPROTOOPT;
382 break;
383 }
384
385 release_sock(sk);
386 return err;
387}
388
389static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
390{
391 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300392 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200393 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700394 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200395 int len, err = 0;
396
397 BT_DBG("sk %p", sk);
398
399 if (level == SOL_L2CAP)
400 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
401
402 if (level != SOL_BLUETOOTH)
403 return -ENOPROTOOPT;
404
405 if (get_user(len, optlen))
406 return -EFAULT;
407
408 lock_sock(sk);
409
410 switch (optname) {
411 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300412 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
413 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200414 err = -EINVAL;
415 break;
416 }
417
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300418 sec.level = chan->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200419
420 len = min_t(unsigned int, len, sizeof(sec));
421 if (copy_to_user(optval, (char *) &sec, len))
422 err = -EFAULT;
423
424 break;
425
426 case BT_DEFER_SETUP:
427 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
428 err = -EINVAL;
429 break;
430 }
431
432 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
433 err = -EFAULT;
434
435 break;
436
437 case BT_FLUSHABLE:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300438 if (put_user(chan->flushable, (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200439 err = -EFAULT;
440
441 break;
442
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700443 case BT_POWER:
444 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
445 && sk->sk_type != SOCK_RAW) {
446 err = -EINVAL;
447 break;
448 }
449
450 pwr.force_active = chan->force_active;
451
452 len = min_t(unsigned int, len, sizeof(pwr));
453 if (copy_to_user(optval, (char *) &pwr, len))
454 err = -EFAULT;
455
456 break;
457
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200458 default:
459 err = -ENOPROTOOPT;
460 break;
461 }
462
463 release_sock(sk);
464 return err;
465}
466
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200467static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
468{
469 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300470 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200471 struct l2cap_options opts;
472 int len, err = 0;
473 u32 opt;
474
475 BT_DBG("sk %p", sk);
476
477 lock_sock(sk);
478
479 switch (optname) {
480 case L2CAP_OPTIONS:
481 if (sk->sk_state == BT_CONNECTED) {
482 err = -EINVAL;
483 break;
484 }
485
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300486 opts.imtu = chan->imtu;
487 opts.omtu = chan->omtu;
488 opts.flush_to = chan->flush_to;
489 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300490 opts.fcs = chan->fcs;
491 opts.max_tx = chan->max_tx;
492 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200493
494 len = min_t(unsigned int, sizeof(opts), optlen);
495 if (copy_from_user((char *) &opts, optval, len)) {
496 err = -EFAULT;
497 break;
498 }
499
500 if (opts.txwin_size > L2CAP_DEFAULT_TX_WINDOW) {
501 err = -EINVAL;
502 break;
503 }
504
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300505 chan->mode = opts.mode;
506 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200507 case L2CAP_MODE_BASIC:
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300508 chan->conf_state &= ~L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200509 break;
510 case L2CAP_MODE_ERTM:
511 case L2CAP_MODE_STREAMING:
512 if (!disable_ertm)
513 break;
514 /* fall through */
515 default:
516 err = -EINVAL;
517 break;
518 }
519
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300520 chan->imtu = opts.imtu;
521 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300522 chan->fcs = opts.fcs;
523 chan->max_tx = opts.max_tx;
524 chan->tx_win = (__u8)opts.txwin_size;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200525 break;
526
527 case L2CAP_LM:
528 if (get_user(opt, (u32 __user *) optval)) {
529 err = -EFAULT;
530 break;
531 }
532
533 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300534 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200535 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300536 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200537 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300538 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200539
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300540 chan->role_switch = (opt & L2CAP_LM_MASTER);
541 chan->force_reliable = (opt & L2CAP_LM_RELIABLE);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200542 break;
543
544 default:
545 err = -ENOPROTOOPT;
546 break;
547 }
548
549 release_sock(sk);
550 return err;
551}
552
553static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
554{
555 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300556 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200557 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700558 struct bt_power pwr;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200559 int len, err = 0;
560 u32 opt;
561
562 BT_DBG("sk %p", sk);
563
564 if (level == SOL_L2CAP)
565 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
566
567 if (level != SOL_BLUETOOTH)
568 return -ENOPROTOOPT;
569
570 lock_sock(sk);
571
572 switch (optname) {
573 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300574 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
575 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200576 err = -EINVAL;
577 break;
578 }
579
580 sec.level = BT_SECURITY_LOW;
581
582 len = min_t(unsigned int, sizeof(sec), optlen);
583 if (copy_from_user((char *) &sec, optval, len)) {
584 err = -EFAULT;
585 break;
586 }
587
588 if (sec.level < BT_SECURITY_LOW ||
589 sec.level > BT_SECURITY_HIGH) {
590 err = -EINVAL;
591 break;
592 }
593
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300594 chan->sec_level = sec.level;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200595 break;
596
597 case BT_DEFER_SETUP:
598 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
599 err = -EINVAL;
600 break;
601 }
602
603 if (get_user(opt, (u32 __user *) optval)) {
604 err = -EFAULT;
605 break;
606 }
607
608 bt_sk(sk)->defer_setup = opt;
609 break;
610
611 case BT_FLUSHABLE:
612 if (get_user(opt, (u32 __user *) optval)) {
613 err = -EFAULT;
614 break;
615 }
616
617 if (opt > BT_FLUSHABLE_ON) {
618 err = -EINVAL;
619 break;
620 }
621
622 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300623 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300624 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200625 No Flush support in the LM */
626 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
627 err = -EINVAL;
628 break;
629 }
630 }
631
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300632 chan->flushable = opt;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200633 break;
634
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700635 case BT_POWER:
636 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
637 chan->chan_type != L2CAP_CHAN_RAW) {
638 err = -EINVAL;
639 break;
640 }
641
642 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
643
644 len = min_t(unsigned int, sizeof(pwr), optlen);
645 if (copy_from_user((char *) &pwr, optval, len)) {
646 err = -EFAULT;
647 break;
648 }
649 chan->force_active = pwr.force_active;
650 break;
651
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200652 default:
653 err = -ENOPROTOOPT;
654 break;
655 }
656
657 release_sock(sk);
658 return err;
659}
660
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200661static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
662{
663 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300664 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200665 int err;
666
667 BT_DBG("sock %p, sk %p", sock, sk);
668
669 err = sock_error(sk);
670 if (err)
671 return err;
672
673 if (msg->msg_flags & MSG_OOB)
674 return -EOPNOTSUPP;
675
676 lock_sock(sk);
677
678 if (sk->sk_state != BT_CONNECTED) {
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300679 release_sock(sk);
680 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200681 }
682
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300683 err = l2cap_chan_send(chan, msg, len);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200684
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200685 release_sock(sk);
686 return err;
687}
688
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200689static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
690{
691 struct sock *sk = sock->sk;
692
693 lock_sock(sk);
694
695 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300696 sk->sk_state = BT_CONFIG;
697
698 __l2cap_connect_rsp_defer(l2cap_pi(sk)->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200699 release_sock(sk);
700 return 0;
701 }
702
703 release_sock(sk);
704
705 if (sock->type == SOCK_STREAM)
706 return bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
707
708 return bt_sock_recvmsg(iocb, sock, msg, len, flags);
709}
710
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200711/* Kill socket (only if zapped and orphan)
712 * Must be called on unlocked socket.
713 */
714void l2cap_sock_kill(struct sock *sk)
715{
716 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
717 return;
718
719 BT_DBG("sk %p state %d", sk, sk->sk_state);
720
721 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300722
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300723 l2cap_chan_destroy(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200724 sock_set_flag(sk, SOCK_DEAD);
725 sock_put(sk);
726}
727
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200728static int l2cap_sock_shutdown(struct socket *sock, int how)
729{
730 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300731 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200732 int err = 0;
733
734 BT_DBG("sock %p, sk %p", sock, sk);
735
736 if (!sk)
737 return 0;
738
739 lock_sock(sk);
740 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300741 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200742 err = __l2cap_wait_ack(sk);
743
744 sk->sk_shutdown = SHUTDOWN_MASK;
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300745 l2cap_chan_close(chan, 0);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200746
747 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
748 err = bt_sock_wait_state(sk, BT_CLOSED,
749 sk->sk_lingertime);
750 }
751
752 if (!err && sk->sk_err)
753 err = -sk->sk_err;
754
755 release_sock(sk);
756 return err;
757}
758
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200759static int l2cap_sock_release(struct socket *sock)
760{
761 struct sock *sk = sock->sk;
762 int err;
763
764 BT_DBG("sock %p, sk %p", sock, sk);
765
766 if (!sk)
767 return 0;
768
769 err = l2cap_sock_shutdown(sock, 2);
770
771 sock_orphan(sk);
772 l2cap_sock_kill(sk);
773 return err;
774}
775
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200776static void l2cap_sock_destruct(struct sock *sk)
777{
778 BT_DBG("sk %p", sk);
779
780 skb_queue_purge(&sk->sk_receive_queue);
781 skb_queue_purge(&sk->sk_write_queue);
782}
783
784void l2cap_sock_init(struct sock *sk, struct sock *parent)
785{
786 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300787 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200788
789 BT_DBG("sk %p", sk);
790
791 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300792 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
793
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200794 sk->sk_type = parent->sk_type;
795 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
796
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300797 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300798 chan->imtu = pchan->imtu;
799 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300800 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300801 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300802 chan->fcs = pchan->fcs;
803 chan->max_tx = pchan->max_tx;
804 chan->tx_win = pchan->tx_win;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300805 chan->sec_level = pchan->sec_level;
806 chan->role_switch = pchan->role_switch;
807 chan->force_reliable = pchan->force_reliable;
808 chan->flushable = pchan->flushable;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700809 chan->force_active = pchan->force_active;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200810 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300811
812 switch (sk->sk_type) {
813 case SOCK_RAW:
814 chan->chan_type = L2CAP_CHAN_RAW;
815 break;
816 case SOCK_DGRAM:
817 chan->chan_type = L2CAP_CHAN_CONN_LESS;
818 break;
819 case SOCK_SEQPACKET:
820 case SOCK_STREAM:
821 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
822 break;
823 }
824
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300825 chan->imtu = L2CAP_DEFAULT_MTU;
826 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200827 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300828 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300829 chan->conf_state |= L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200830 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300831 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200832 }
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300833 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
834 chan->fcs = L2CAP_FCS_CRC16;
835 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300836 chan->sec_level = BT_SECURITY_LOW;
837 chan->role_switch = 0;
838 chan->force_reliable = 0;
839 chan->flushable = BT_FLUSHABLE_OFF;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700840 chan->force_active = BT_POWER_FORCE_ACTIVE_ON;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200841 }
842
843 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300844 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200845}
846
847static struct proto l2cap_proto = {
848 .name = "L2CAP",
849 .owner = THIS_MODULE,
850 .obj_size = sizeof(struct l2cap_pinfo)
851};
852
853struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
854{
855 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -0300856 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200857
858 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
859 if (!sk)
860 return NULL;
861
862 sock_init_data(sock, sk);
863 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
864
865 sk->sk_destruct = l2cap_sock_destruct;
866 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
867
868 sock_reset_flag(sk, SOCK_ZAPPED);
869
870 sk->sk_protocol = proto;
871 sk->sk_state = BT_OPEN;
872
Gustavo F. Padovandc50a062011-05-16 16:42:01 -0300873 chan = l2cap_chan_create(sk);
874 if (!chan) {
875 l2cap_sock_kill(sk);
876 return NULL;
877 }
878
879 l2cap_pi(sk)->chan = chan;
880
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200881 return sk;
882}
883
884static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
885 int kern)
886{
887 struct sock *sk;
888
889 BT_DBG("sock %p", sock);
890
891 sock->state = SS_UNCONNECTED;
892
893 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
894 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
895 return -ESOCKTNOSUPPORT;
896
897 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
898 return -EPERM;
899
900 sock->ops = &l2cap_sock_ops;
901
902 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
903 if (!sk)
904 return -ENOMEM;
905
906 l2cap_sock_init(sk, NULL);
907 return 0;
908}
909
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -0300910static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -0200911 .family = PF_BLUETOOTH,
912 .owner = THIS_MODULE,
913 .release = l2cap_sock_release,
914 .bind = l2cap_sock_bind,
915 .connect = l2cap_sock_connect,
916 .listen = l2cap_sock_listen,
917 .accept = l2cap_sock_accept,
918 .getname = l2cap_sock_getname,
919 .sendmsg = l2cap_sock_sendmsg,
920 .recvmsg = l2cap_sock_recvmsg,
921 .poll = bt_sock_poll,
922 .ioctl = bt_sock_ioctl,
923 .mmap = sock_no_mmap,
924 .socketpair = sock_no_socketpair,
925 .shutdown = l2cap_sock_shutdown,
926 .setsockopt = l2cap_sock_setsockopt,
927 .getsockopt = l2cap_sock_getsockopt
928};
929
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200930static const struct net_proto_family l2cap_sock_family_ops = {
931 .family = PF_BLUETOOTH,
932 .owner = THIS_MODULE,
933 .create = l2cap_sock_create,
934};
935
936int __init l2cap_init_sockets(void)
937{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300938 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200939
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300940 err = proto_register(&l2cap_proto, 0);
941 if (err < 0)
942 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200943
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300944 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
945 if (err < 0)
946 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200947
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300948 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200949
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300950 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200951
952error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300953 BT_ERR("L2CAP socket registration failed");
954 proto_unregister(&l2cap_proto);
955 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200956}
957
958void l2cap_cleanup_sockets(void)
959{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300960 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
961 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200962
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300963 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200964}