blob: b79fb7561836026a56a8117615c4b7a1312053c3 [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
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300370 cinfo.hci_handle = chan->conn->hcon->handle;
371 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200372
373 len = min_t(unsigned int, len, sizeof(cinfo));
374 if (copy_to_user(optval, (char *) &cinfo, len))
375 err = -EFAULT;
376
377 break;
378
379 default:
380 err = -ENOPROTOOPT;
381 break;
382 }
383
384 release_sock(sk);
385 return err;
386}
387
388static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
389{
390 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300391 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200392 struct bt_security sec;
393 int len, err = 0;
394
395 BT_DBG("sk %p", sk);
396
397 if (level == SOL_L2CAP)
398 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
399
400 if (level != SOL_BLUETOOTH)
401 return -ENOPROTOOPT;
402
403 if (get_user(len, optlen))
404 return -EFAULT;
405
406 lock_sock(sk);
407
408 switch (optname) {
409 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300410 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
411 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200412 err = -EINVAL;
413 break;
414 }
415
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300416 sec.level = chan->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200417
418 len = min_t(unsigned int, len, sizeof(sec));
419 if (copy_to_user(optval, (char *) &sec, len))
420 err = -EFAULT;
421
422 break;
423
424 case BT_DEFER_SETUP:
425 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
426 err = -EINVAL;
427 break;
428 }
429
430 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
431 err = -EFAULT;
432
433 break;
434
435 case BT_FLUSHABLE:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300436 if (put_user(chan->flushable, (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200437 err = -EFAULT;
438
439 break;
440
441 default:
442 err = -ENOPROTOOPT;
443 break;
444 }
445
446 release_sock(sk);
447 return err;
448}
449
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200450static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
451{
452 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300453 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200454 struct l2cap_options opts;
455 int len, err = 0;
456 u32 opt;
457
458 BT_DBG("sk %p", sk);
459
460 lock_sock(sk);
461
462 switch (optname) {
463 case L2CAP_OPTIONS:
464 if (sk->sk_state == BT_CONNECTED) {
465 err = -EINVAL;
466 break;
467 }
468
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300469 opts.imtu = chan->imtu;
470 opts.omtu = chan->omtu;
471 opts.flush_to = chan->flush_to;
472 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300473 opts.fcs = chan->fcs;
474 opts.max_tx = chan->max_tx;
475 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200476
477 len = min_t(unsigned int, sizeof(opts), optlen);
478 if (copy_from_user((char *) &opts, optval, len)) {
479 err = -EFAULT;
480 break;
481 }
482
483 if (opts.txwin_size > L2CAP_DEFAULT_TX_WINDOW) {
484 err = -EINVAL;
485 break;
486 }
487
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300488 chan->mode = opts.mode;
489 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200490 case L2CAP_MODE_BASIC:
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300491 chan->conf_state &= ~L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200492 break;
493 case L2CAP_MODE_ERTM:
494 case L2CAP_MODE_STREAMING:
495 if (!disable_ertm)
496 break;
497 /* fall through */
498 default:
499 err = -EINVAL;
500 break;
501 }
502
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300503 chan->imtu = opts.imtu;
504 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300505 chan->fcs = opts.fcs;
506 chan->max_tx = opts.max_tx;
507 chan->tx_win = (__u8)opts.txwin_size;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200508 break;
509
510 case L2CAP_LM:
511 if (get_user(opt, (u32 __user *) optval)) {
512 err = -EFAULT;
513 break;
514 }
515
516 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300517 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200518 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300519 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200520 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300521 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200522
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300523 chan->role_switch = (opt & L2CAP_LM_MASTER);
524 chan->force_reliable = (opt & L2CAP_LM_RELIABLE);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200525 break;
526
527 default:
528 err = -ENOPROTOOPT;
529 break;
530 }
531
532 release_sock(sk);
533 return err;
534}
535
536static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
537{
538 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300539 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200540 struct bt_security sec;
541 int len, err = 0;
542 u32 opt;
543
544 BT_DBG("sk %p", sk);
545
546 if (level == SOL_L2CAP)
547 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
548
549 if (level != SOL_BLUETOOTH)
550 return -ENOPROTOOPT;
551
552 lock_sock(sk);
553
554 switch (optname) {
555 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300556 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
557 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200558 err = -EINVAL;
559 break;
560 }
561
562 sec.level = BT_SECURITY_LOW;
563
564 len = min_t(unsigned int, sizeof(sec), optlen);
565 if (copy_from_user((char *) &sec, optval, len)) {
566 err = -EFAULT;
567 break;
568 }
569
570 if (sec.level < BT_SECURITY_LOW ||
571 sec.level > BT_SECURITY_HIGH) {
572 err = -EINVAL;
573 break;
574 }
575
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300576 chan->sec_level = sec.level;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200577 break;
578
579 case BT_DEFER_SETUP:
580 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
581 err = -EINVAL;
582 break;
583 }
584
585 if (get_user(opt, (u32 __user *) optval)) {
586 err = -EFAULT;
587 break;
588 }
589
590 bt_sk(sk)->defer_setup = opt;
591 break;
592
593 case BT_FLUSHABLE:
594 if (get_user(opt, (u32 __user *) optval)) {
595 err = -EFAULT;
596 break;
597 }
598
599 if (opt > BT_FLUSHABLE_ON) {
600 err = -EINVAL;
601 break;
602 }
603
604 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300605 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300606 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200607 No Flush support in the LM */
608 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
609 err = -EINVAL;
610 break;
611 }
612 }
613
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300614 chan->flushable = opt;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200615 break;
616
617 default:
618 err = -ENOPROTOOPT;
619 break;
620 }
621
622 release_sock(sk);
623 return err;
624}
625
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200626static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
627{
628 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300629 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200630 int err;
631
632 BT_DBG("sock %p, sk %p", sock, sk);
633
634 err = sock_error(sk);
635 if (err)
636 return err;
637
638 if (msg->msg_flags & MSG_OOB)
639 return -EOPNOTSUPP;
640
641 lock_sock(sk);
642
643 if (sk->sk_state != BT_CONNECTED) {
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300644 release_sock(sk);
645 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200646 }
647
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300648 err = l2cap_chan_send(chan, msg, len);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200649
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200650 release_sock(sk);
651 return err;
652}
653
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200654static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
655{
656 struct sock *sk = sock->sk;
657
658 lock_sock(sk);
659
660 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300661 sk->sk_state = BT_CONFIG;
662
663 __l2cap_connect_rsp_defer(l2cap_pi(sk)->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200664 release_sock(sk);
665 return 0;
666 }
667
668 release_sock(sk);
669
670 if (sock->type == SOCK_STREAM)
671 return bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
672
673 return bt_sock_recvmsg(iocb, sock, msg, len, flags);
674}
675
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200676/* Kill socket (only if zapped and orphan)
677 * Must be called on unlocked socket.
678 */
679void l2cap_sock_kill(struct sock *sk)
680{
681 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
682 return;
683
684 BT_DBG("sk %p state %d", sk, sk->sk_state);
685
686 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300687
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300688 l2cap_chan_destroy(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200689 sock_set_flag(sk, SOCK_DEAD);
690 sock_put(sk);
691}
692
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200693static int l2cap_sock_shutdown(struct socket *sock, int how)
694{
695 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300696 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200697 int err = 0;
698
699 BT_DBG("sock %p, sk %p", sock, sk);
700
701 if (!sk)
702 return 0;
703
704 lock_sock(sk);
705 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300706 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200707 err = __l2cap_wait_ack(sk);
708
709 sk->sk_shutdown = SHUTDOWN_MASK;
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300710 l2cap_chan_close(chan, 0);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200711
712 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
713 err = bt_sock_wait_state(sk, BT_CLOSED,
714 sk->sk_lingertime);
715 }
716
717 if (!err && sk->sk_err)
718 err = -sk->sk_err;
719
720 release_sock(sk);
721 return err;
722}
723
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200724static int l2cap_sock_release(struct socket *sock)
725{
726 struct sock *sk = sock->sk;
727 int err;
728
729 BT_DBG("sock %p, sk %p", sock, sk);
730
731 if (!sk)
732 return 0;
733
734 err = l2cap_sock_shutdown(sock, 2);
735
736 sock_orphan(sk);
737 l2cap_sock_kill(sk);
738 return err;
739}
740
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200741static void l2cap_sock_destruct(struct sock *sk)
742{
743 BT_DBG("sk %p", sk);
744
745 skb_queue_purge(&sk->sk_receive_queue);
746 skb_queue_purge(&sk->sk_write_queue);
747}
748
749void l2cap_sock_init(struct sock *sk, struct sock *parent)
750{
751 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300752 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200753
754 BT_DBG("sk %p", sk);
755
756 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300757 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
758
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200759 sk->sk_type = parent->sk_type;
760 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
761
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300762 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300763 chan->imtu = pchan->imtu;
764 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300765 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300766 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300767 chan->fcs = pchan->fcs;
768 chan->max_tx = pchan->max_tx;
769 chan->tx_win = pchan->tx_win;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300770 chan->sec_level = pchan->sec_level;
771 chan->role_switch = pchan->role_switch;
772 chan->force_reliable = pchan->force_reliable;
773 chan->flushable = pchan->flushable;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200774 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300775
776 switch (sk->sk_type) {
777 case SOCK_RAW:
778 chan->chan_type = L2CAP_CHAN_RAW;
779 break;
780 case SOCK_DGRAM:
781 chan->chan_type = L2CAP_CHAN_CONN_LESS;
782 break;
783 case SOCK_SEQPACKET:
784 case SOCK_STREAM:
785 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
786 break;
787 }
788
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300789 chan->imtu = L2CAP_DEFAULT_MTU;
790 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200791 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300792 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300793 chan->conf_state |= L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200794 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300795 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200796 }
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300797 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
798 chan->fcs = L2CAP_FCS_CRC16;
799 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300800 chan->sec_level = BT_SECURITY_LOW;
801 chan->role_switch = 0;
802 chan->force_reliable = 0;
803 chan->flushable = BT_FLUSHABLE_OFF;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200804 }
805
806 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300807 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200808}
809
810static struct proto l2cap_proto = {
811 .name = "L2CAP",
812 .owner = THIS_MODULE,
813 .obj_size = sizeof(struct l2cap_pinfo)
814};
815
816struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
817{
818 struct sock *sk;
819
820 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
821 if (!sk)
822 return NULL;
823
824 sock_init_data(sock, sk);
825 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
826
827 sk->sk_destruct = l2cap_sock_destruct;
828 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
829
830 sock_reset_flag(sk, SOCK_ZAPPED);
831
832 sk->sk_protocol = proto;
833 sk->sk_state = BT_OPEN;
834
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200835 return sk;
836}
837
838static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
839 int kern)
840{
841 struct sock *sk;
Gustavo F. Padovan5d41ce12011-04-08 15:40:02 -0300842 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200843
844 BT_DBG("sock %p", sock);
845
846 sock->state = SS_UNCONNECTED;
847
848 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
849 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
850 return -ESOCKTNOSUPPORT;
851
852 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
853 return -EPERM;
854
855 sock->ops = &l2cap_sock_ops;
856
857 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
858 if (!sk)
859 return -ENOMEM;
860
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300861 chan = l2cap_chan_create(sk);
Gustavo F. Padovan5d41ce12011-04-08 15:40:02 -0300862 if (!chan) {
863 l2cap_sock_kill(sk);
864 return -ENOMEM;
865 }
866
867 l2cap_pi(sk)->chan = chan;
868
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200869 l2cap_sock_init(sk, NULL);
870 return 0;
871}
872
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -0300873static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -0200874 .family = PF_BLUETOOTH,
875 .owner = THIS_MODULE,
876 .release = l2cap_sock_release,
877 .bind = l2cap_sock_bind,
878 .connect = l2cap_sock_connect,
879 .listen = l2cap_sock_listen,
880 .accept = l2cap_sock_accept,
881 .getname = l2cap_sock_getname,
882 .sendmsg = l2cap_sock_sendmsg,
883 .recvmsg = l2cap_sock_recvmsg,
884 .poll = bt_sock_poll,
885 .ioctl = bt_sock_ioctl,
886 .mmap = sock_no_mmap,
887 .socketpair = sock_no_socketpair,
888 .shutdown = l2cap_sock_shutdown,
889 .setsockopt = l2cap_sock_setsockopt,
890 .getsockopt = l2cap_sock_getsockopt
891};
892
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200893static const struct net_proto_family l2cap_sock_family_ops = {
894 .family = PF_BLUETOOTH,
895 .owner = THIS_MODULE,
896 .create = l2cap_sock_create,
897};
898
899int __init l2cap_init_sockets(void)
900{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300901 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200902
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300903 err = proto_register(&l2cap_proto, 0);
904 if (err < 0)
905 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200906
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300907 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
908 if (err < 0)
909 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200910
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300911 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200912
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300913 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200914
915error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300916 BT_ERR("L2CAP socket registration failed");
917 proto_unregister(&l2cap_proto);
918 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200919}
920
921void l2cap_cleanup_sockets(void)
922{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300923 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
924 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200925
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -0300926 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200927}