blob: 0e23ebdf7c8f17170aa20397c528bdd5c6e17c3d [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. Padovan6ddc0482011-02-04 03:23:31 -020033/* ---- L2CAP timers ---- */
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020034static void l2cap_sock_timeout(unsigned long arg)
35{
36 struct sock *sk = (struct sock *) arg;
37 int reason;
38
39 BT_DBG("sock %p state %d", sk, sk->sk_state);
40
41 bh_lock_sock(sk);
42
43 if (sock_owned_by_user(sk)) {
44 /* sk is owned by user. Try again later */
45 l2cap_sock_set_timer(sk, HZ / 5);
46 bh_unlock_sock(sk);
47 sock_put(sk);
48 return;
49 }
50
51 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG)
52 reason = ECONNREFUSED;
53 else if (sk->sk_state == BT_CONNECT &&
Gustavo F. Padovan43434782011-04-12 18:31:57 -030054 l2cap_pi(sk)->chan->sec_level != BT_SECURITY_SDP)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020055 reason = ECONNREFUSED;
56 else
57 reason = ETIMEDOUT;
58
59 __l2cap_sock_close(sk, reason);
60
61 bh_unlock_sock(sk);
62
63 l2cap_sock_kill(sk);
64 sock_put(sk);
65}
66
Gustavo F. Padovan6ddc0482011-02-04 03:23:31 -020067void l2cap_sock_set_timer(struct sock *sk, long timeout)
68{
69 BT_DBG("sk %p state %d timeout %ld", sk, sk->sk_state, timeout);
70 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
71}
72
73void l2cap_sock_clear_timer(struct sock *sk)
74{
75 BT_DBG("sock %p state %d", sk, sk->sk_state);
76 sk_stop_timer(sk, &sk->sk_timer);
77}
78
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020079static struct sock *__l2cap_get_sock_by_addr(__le16 psm, bdaddr_t *src)
80{
81 struct sock *sk;
82 struct hlist_node *node;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -030083 sk_for_each(sk, node, &l2cap_sk_list.head) {
84 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
85
86 if (chan->sport == psm && !bacmp(&bt_sk(sk)->src, src))
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020087 goto found;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -030088 }
89
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020090 sk = NULL;
91found:
92 return sk;
93}
94
95static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
96{
97 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -030098 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020099 struct sockaddr_l2 la;
100 int len, err = 0;
101
102 BT_DBG("sk %p", sk);
103
104 if (!addr || addr->sa_family != AF_BLUETOOTH)
105 return -EINVAL;
106
107 memset(&la, 0, sizeof(la));
108 len = min_t(unsigned int, sizeof(la), alen);
109 memcpy(&la, addr, len);
110
Ville Tervob62f3282011-02-10 22:38:50 -0300111 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200112 return -EINVAL;
113
114 lock_sock(sk);
115
116 if (sk->sk_state != BT_OPEN) {
117 err = -EBADFD;
118 goto done;
119 }
120
121 if (la.l2_psm) {
122 __u16 psm = __le16_to_cpu(la.l2_psm);
123
124 /* PSM must be odd and lsb of upper byte must be 0 */
125 if ((psm & 0x0101) != 0x0001) {
126 err = -EINVAL;
127 goto done;
128 }
129
130 /* Restrict usage of well-known PSMs */
131 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
132 err = -EACCES;
133 goto done;
134 }
135 }
136
137 write_lock_bh(&l2cap_sk_list.lock);
138
139 if (la.l2_psm && __l2cap_get_sock_by_addr(la.l2_psm, &la.l2_bdaddr)) {
140 err = -EADDRINUSE;
141 } else {
142 /* Save source address */
143 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300144 chan->psm = la.l2_psm;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300145 chan->sport = la.l2_psm;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200146 sk->sk_state = BT_BOUND;
147
148 if (__le16_to_cpu(la.l2_psm) == 0x0001 ||
149 __le16_to_cpu(la.l2_psm) == 0x0003)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300150 chan->sec_level = BT_SECURITY_SDP;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200151 }
152
Ville Tervob62f3282011-02-10 22:38:50 -0300153 if (la.l2_cid)
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300154 chan->scid = la.l2_cid;
Ville Tervob62f3282011-02-10 22:38:50 -0300155
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200156 write_unlock_bh(&l2cap_sk_list.lock);
157
158done:
159 release_sock(sk);
160 return err;
161}
162
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200163static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
164{
165 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300166 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200167 struct sockaddr_l2 la;
168 int len, err = 0;
169
170 BT_DBG("sk %p", sk);
171
172 if (!addr || alen < sizeof(addr->sa_family) ||
173 addr->sa_family != AF_BLUETOOTH)
174 return -EINVAL;
175
176 memset(&la, 0, sizeof(la));
177 len = min_t(unsigned int, sizeof(la), alen);
178 memcpy(&la, addr, len);
179
Ville Tervoacd7d372011-02-10 22:38:49 -0300180 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200181 return -EINVAL;
182
183 lock_sock(sk);
184
185 if ((sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM)
Ville Tervoacd7d372011-02-10 22:38:49 -0300186 && !(la.l2_psm || la.l2_cid)) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200187 err = -EINVAL;
188 goto done;
189 }
190
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300191 switch (chan->mode) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200192 case L2CAP_MODE_BASIC:
193 break;
194 case L2CAP_MODE_ERTM:
195 case L2CAP_MODE_STREAMING:
196 if (!disable_ertm)
197 break;
198 /* fall through */
199 default:
200 err = -ENOTSUPP;
201 goto done;
202 }
203
204 switch (sk->sk_state) {
205 case BT_CONNECT:
206 case BT_CONNECT2:
207 case BT_CONFIG:
208 /* Already connecting */
209 goto wait;
210
211 case BT_CONNECTED:
212 /* Already connected */
213 err = -EISCONN;
214 goto done;
215
216 case BT_OPEN:
217 case BT_BOUND:
218 /* Can connect */
219 break;
220
221 default:
222 err = -EBADFD;
223 goto done;
224 }
225
226 /* PSM must be odd and lsb of upper byte must be 0 */
227 if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 &&
Ville Tervoacd7d372011-02-10 22:38:49 -0300228 sk->sk_type != SOCK_RAW && !la.l2_cid) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200229 err = -EINVAL;
230 goto done;
231 }
232
233 /* Set destination address and psm */
234 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300235 chan->psm = la.l2_psm;
236 chan->dcid = la.l2_cid;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200237
Gustavo F. Padovan77a74c72011-04-12 18:17:14 -0300238 err = l2cap_chan_connect(l2cap_pi(sk)->chan);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200239 if (err)
240 goto done;
241
242wait:
243 err = bt_sock_wait_state(sk, BT_CONNECTED,
244 sock_sndtimeo(sk, flags & O_NONBLOCK));
245done:
246 release_sock(sk);
247 return err;
248}
249
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200250static int l2cap_sock_listen(struct socket *sock, int backlog)
251{
252 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300253 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200254 int err = 0;
255
256 BT_DBG("sk %p backlog %d", sk, backlog);
257
258 lock_sock(sk);
259
260 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
261 || sk->sk_state != BT_BOUND) {
262 err = -EBADFD;
263 goto done;
264 }
265
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300266 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200267 case L2CAP_MODE_BASIC:
268 break;
269 case L2CAP_MODE_ERTM:
270 case L2CAP_MODE_STREAMING:
271 if (!disable_ertm)
272 break;
273 /* fall through */
274 default:
275 err = -ENOTSUPP;
276 goto done;
277 }
278
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300279 if (!chan->psm && !chan->scid) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200280 bdaddr_t *src = &bt_sk(sk)->src;
281 u16 psm;
282
283 err = -EINVAL;
284
285 write_lock_bh(&l2cap_sk_list.lock);
286
287 for (psm = 0x1001; psm < 0x1100; psm += 2)
288 if (!__l2cap_get_sock_by_addr(cpu_to_le16(psm), src)) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300289 chan->psm = cpu_to_le16(psm);
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300290 chan->sport = cpu_to_le16(psm);
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200291 err = 0;
292 break;
293 }
294
295 write_unlock_bh(&l2cap_sk_list.lock);
296
297 if (err < 0)
298 goto done;
299 }
300
301 sk->sk_max_ack_backlog = backlog;
302 sk->sk_ack_backlog = 0;
303 sk->sk_state = BT_LISTEN;
304
305done:
306 release_sock(sk);
307 return err;
308}
309
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200310static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
311{
312 DECLARE_WAITQUEUE(wait, current);
313 struct sock *sk = sock->sk, *nsk;
314 long timeo;
315 int err = 0;
316
317 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
318
319 if (sk->sk_state != BT_LISTEN) {
320 err = -EBADFD;
321 goto done;
322 }
323
324 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
325
326 BT_DBG("sk %p timeo %ld", sk, timeo);
327
328 /* Wait for an incoming connection. (wake-one). */
329 add_wait_queue_exclusive(sk_sleep(sk), &wait);
330 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
331 set_current_state(TASK_INTERRUPTIBLE);
332 if (!timeo) {
333 err = -EAGAIN;
334 break;
335 }
336
337 release_sock(sk);
338 timeo = schedule_timeout(timeo);
339 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
340
341 if (sk->sk_state != BT_LISTEN) {
342 err = -EBADFD;
343 break;
344 }
345
346 if (signal_pending(current)) {
347 err = sock_intr_errno(timeo);
348 break;
349 }
350 }
351 set_current_state(TASK_RUNNING);
352 remove_wait_queue(sk_sleep(sk), &wait);
353
354 if (err)
355 goto done;
356
357 newsock->state = SS_CONNECTED;
358
359 BT_DBG("new socket %p", nsk);
360
361done:
362 release_sock(sk);
363 return err;
364}
365
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200366static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
367{
368 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
369 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300370 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200371
372 BT_DBG("sock %p, sk %p", sock, sk);
373
374 addr->sa_family = AF_BLUETOOTH;
375 *len = sizeof(struct sockaddr_l2);
376
377 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300378 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200379 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300380 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200381 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300382 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200383 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300384 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200385 }
386
387 return 0;
388}
389
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200390static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
391{
392 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300393 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200394 struct l2cap_options opts;
395 struct l2cap_conninfo cinfo;
396 int len, err = 0;
397 u32 opt;
398
399 BT_DBG("sk %p", sk);
400
401 if (get_user(len, optlen))
402 return -EFAULT;
403
404 lock_sock(sk);
405
406 switch (optname) {
407 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300408 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300409 opts.imtu = chan->imtu;
410 opts.omtu = chan->omtu;
411 opts.flush_to = chan->flush_to;
412 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300413 opts.fcs = chan->fcs;
414 opts.max_tx = chan->max_tx;
415 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200416
417 len = min_t(unsigned int, len, sizeof(opts));
418 if (copy_to_user(optval, (char *) &opts, len))
419 err = -EFAULT;
420
421 break;
422
423 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300424 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200425 case BT_SECURITY_LOW:
426 opt = L2CAP_LM_AUTH;
427 break;
428 case BT_SECURITY_MEDIUM:
429 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
430 break;
431 case BT_SECURITY_HIGH:
432 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
433 L2CAP_LM_SECURE;
434 break;
435 default:
436 opt = 0;
437 break;
438 }
439
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300440 if (chan->role_switch)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200441 opt |= L2CAP_LM_MASTER;
442
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300443 if (chan->force_reliable)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200444 opt |= L2CAP_LM_RELIABLE;
445
446 if (put_user(opt, (u32 __user *) optval))
447 err = -EFAULT;
448 break;
449
450 case L2CAP_CONNINFO:
451 if (sk->sk_state != BT_CONNECTED &&
452 !(sk->sk_state == BT_CONNECT2 &&
453 bt_sk(sk)->defer_setup)) {
454 err = -ENOTCONN;
455 break;
456 }
457
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300458 cinfo.hci_handle = chan->conn->hcon->handle;
459 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200460
461 len = min_t(unsigned int, len, sizeof(cinfo));
462 if (copy_to_user(optval, (char *) &cinfo, len))
463 err = -EFAULT;
464
465 break;
466
467 default:
468 err = -ENOPROTOOPT;
469 break;
470 }
471
472 release_sock(sk);
473 return err;
474}
475
476static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
477{
478 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300479 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200480 struct bt_security sec;
481 int len, err = 0;
482
483 BT_DBG("sk %p", sk);
484
485 if (level == SOL_L2CAP)
486 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
487
488 if (level != SOL_BLUETOOTH)
489 return -ENOPROTOOPT;
490
491 if (get_user(len, optlen))
492 return -EFAULT;
493
494 lock_sock(sk);
495
496 switch (optname) {
497 case BT_SECURITY:
498 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
499 && sk->sk_type != SOCK_RAW) {
500 err = -EINVAL;
501 break;
502 }
503
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300504 sec.level = chan->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200505
506 len = min_t(unsigned int, len, sizeof(sec));
507 if (copy_to_user(optval, (char *) &sec, len))
508 err = -EFAULT;
509
510 break;
511
512 case BT_DEFER_SETUP:
513 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
514 err = -EINVAL;
515 break;
516 }
517
518 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
519 err = -EFAULT;
520
521 break;
522
523 case BT_FLUSHABLE:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300524 if (put_user(chan->flushable, (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200525 err = -EFAULT;
526
527 break;
528
529 default:
530 err = -ENOPROTOOPT;
531 break;
532 }
533
534 release_sock(sk);
535 return err;
536}
537
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200538static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
539{
540 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300541 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200542 struct l2cap_options opts;
543 int len, err = 0;
544 u32 opt;
545
546 BT_DBG("sk %p", sk);
547
548 lock_sock(sk);
549
550 switch (optname) {
551 case L2CAP_OPTIONS:
552 if (sk->sk_state == BT_CONNECTED) {
553 err = -EINVAL;
554 break;
555 }
556
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300557 opts.imtu = chan->imtu;
558 opts.omtu = chan->omtu;
559 opts.flush_to = chan->flush_to;
560 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300561 opts.fcs = chan->fcs;
562 opts.max_tx = chan->max_tx;
563 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200564
565 len = min_t(unsigned int, sizeof(opts), optlen);
566 if (copy_from_user((char *) &opts, optval, len)) {
567 err = -EFAULT;
568 break;
569 }
570
571 if (opts.txwin_size > L2CAP_DEFAULT_TX_WINDOW) {
572 err = -EINVAL;
573 break;
574 }
575
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300576 chan->mode = opts.mode;
577 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200578 case L2CAP_MODE_BASIC:
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300579 chan->conf_state &= ~L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200580 break;
581 case L2CAP_MODE_ERTM:
582 case L2CAP_MODE_STREAMING:
583 if (!disable_ertm)
584 break;
585 /* fall through */
586 default:
587 err = -EINVAL;
588 break;
589 }
590
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300591 chan->imtu = opts.imtu;
592 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300593 chan->fcs = opts.fcs;
594 chan->max_tx = opts.max_tx;
595 chan->tx_win = (__u8)opts.txwin_size;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200596 break;
597
598 case L2CAP_LM:
599 if (get_user(opt, (u32 __user *) optval)) {
600 err = -EFAULT;
601 break;
602 }
603
604 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300605 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200606 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300607 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200608 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300609 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200610
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300611 chan->role_switch = (opt & L2CAP_LM_MASTER);
612 chan->force_reliable = (opt & L2CAP_LM_RELIABLE);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200613 break;
614
615 default:
616 err = -ENOPROTOOPT;
617 break;
618 }
619
620 release_sock(sk);
621 return err;
622}
623
624static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
625{
626 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300627 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200628 struct bt_security sec;
629 int len, err = 0;
630 u32 opt;
631
632 BT_DBG("sk %p", sk);
633
634 if (level == SOL_L2CAP)
635 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
636
637 if (level != SOL_BLUETOOTH)
638 return -ENOPROTOOPT;
639
640 lock_sock(sk);
641
642 switch (optname) {
643 case BT_SECURITY:
644 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
645 && sk->sk_type != SOCK_RAW) {
646 err = -EINVAL;
647 break;
648 }
649
650 sec.level = BT_SECURITY_LOW;
651
652 len = min_t(unsigned int, sizeof(sec), optlen);
653 if (copy_from_user((char *) &sec, optval, len)) {
654 err = -EFAULT;
655 break;
656 }
657
658 if (sec.level < BT_SECURITY_LOW ||
659 sec.level > BT_SECURITY_HIGH) {
660 err = -EINVAL;
661 break;
662 }
663
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300664 chan->sec_level = sec.level;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200665 break;
666
667 case BT_DEFER_SETUP:
668 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
669 err = -EINVAL;
670 break;
671 }
672
673 if (get_user(opt, (u32 __user *) optval)) {
674 err = -EFAULT;
675 break;
676 }
677
678 bt_sk(sk)->defer_setup = opt;
679 break;
680
681 case BT_FLUSHABLE:
682 if (get_user(opt, (u32 __user *) optval)) {
683 err = -EFAULT;
684 break;
685 }
686
687 if (opt > BT_FLUSHABLE_ON) {
688 err = -EINVAL;
689 break;
690 }
691
692 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300693 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200694 /* proceed futher only when we have l2cap_conn and
695 No Flush support in the LM */
696 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
697 err = -EINVAL;
698 break;
699 }
700 }
701
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300702 chan->flushable = opt;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200703 break;
704
705 default:
706 err = -ENOPROTOOPT;
707 break;
708 }
709
710 release_sock(sk);
711 return err;
712}
713
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200714static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
715{
716 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300717 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200718 struct sk_buff *skb;
719 u16 control;
720 int err;
721
722 BT_DBG("sock %p, sk %p", sock, sk);
723
724 err = sock_error(sk);
725 if (err)
726 return err;
727
728 if (msg->msg_flags & MSG_OOB)
729 return -EOPNOTSUPP;
730
731 lock_sock(sk);
732
733 if (sk->sk_state != BT_CONNECTED) {
734 err = -ENOTCONN;
735 goto done;
736 }
737
738 /* Connectionless channel */
739 if (sk->sk_type == SOCK_DGRAM) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300740 skb = l2cap_create_connless_pdu(chan, msg, len);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200741 if (IS_ERR(skb)) {
742 err = PTR_ERR(skb);
743 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300744 l2cap_do_send(chan, skb);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200745 err = len;
746 }
747 goto done;
748 }
749
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300750 switch (chan->mode) {
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200751 case L2CAP_MODE_BASIC:
752 /* Check outgoing MTU */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300753 if (len > chan->omtu) {
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200754 err = -EMSGSIZE;
755 goto done;
756 }
757
758 /* Create a basic PDU */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300759 skb = l2cap_create_basic_pdu(chan, msg, len);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200760 if (IS_ERR(skb)) {
761 err = PTR_ERR(skb);
762 goto done;
763 }
764
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300765 l2cap_do_send(chan, skb);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200766 err = len;
767 break;
768
769 case L2CAP_MODE_ERTM:
770 case L2CAP_MODE_STREAMING:
771 /* Entire SDU fits into one PDU */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300772 if (len <= chan->remote_mps) {
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200773 control = L2CAP_SDU_UNSEGMENTED;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300774 skb = l2cap_create_iframe_pdu(chan, msg, len, control,
775 0);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200776 if (IS_ERR(skb)) {
777 err = PTR_ERR(skb);
778 goto done;
779 }
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300780 __skb_queue_tail(&chan->tx_q, skb);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200781
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300782 if (chan->tx_send_head == NULL)
783 chan->tx_send_head = skb;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200784
785 } else {
786 /* Segment SDU into multiples PDUs */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300787 err = l2cap_sar_segment_sdu(chan, msg, len);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200788 if (err < 0)
789 goto done;
790 }
791
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300792 if (chan->mode == L2CAP_MODE_STREAMING) {
793 l2cap_streaming_send(chan);
Gustavo F. Padovan525cd182011-03-25 19:43:39 -0300794 err = len;
795 break;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200796 }
797
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300798 if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
799 (chan->conn_state & L2CAP_CONN_WAIT_F)) {
Gustavo F. Padovan525cd182011-03-25 19:43:39 -0300800 err = len;
801 break;
802 }
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300803 err = l2cap_ertm_send(chan);
Gustavo F. Padovan525cd182011-03-25 19:43:39 -0300804
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200805 if (err >= 0)
806 err = len;
807 break;
808
809 default:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300810 BT_DBG("bad state %1.1x", chan->mode);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200811 err = -EBADFD;
812 }
813
814done:
815 release_sock(sk);
816 return err;
817}
818
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200819static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
820{
821 struct sock *sk = sock->sk;
822
823 lock_sock(sk);
824
825 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300826 sk->sk_state = BT_CONFIG;
827
828 __l2cap_connect_rsp_defer(l2cap_pi(sk)->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200829 release_sock(sk);
830 return 0;
831 }
832
833 release_sock(sk);
834
835 if (sock->type == SOCK_STREAM)
836 return bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
837
838 return bt_sock_recvmsg(iocb, sock, msg, len, flags);
839}
840
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200841/* Kill socket (only if zapped and orphan)
842 * Must be called on unlocked socket.
843 */
844void l2cap_sock_kill(struct sock *sk)
845{
846 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
847 return;
848
849 BT_DBG("sk %p state %d", sk, sk->sk_state);
850
851 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300852
853 l2cap_chan_free(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200854 bt_sock_unlink(&l2cap_sk_list, sk);
855 sock_set_flag(sk, SOCK_DEAD);
856 sock_put(sk);
857}
858
Gustavo F. Padovan6de07022011-02-04 03:35:20 -0200859/* Must be called on unlocked socket. */
860static void l2cap_sock_close(struct sock *sk)
861{
862 l2cap_sock_clear_timer(sk);
863 lock_sock(sk);
864 __l2cap_sock_close(sk, ECONNRESET);
865 release_sock(sk);
866 l2cap_sock_kill(sk);
867}
868
869static void l2cap_sock_cleanup_listen(struct sock *parent)
870{
871 struct sock *sk;
872
873 BT_DBG("parent %p", parent);
874
875 /* Close not yet accepted channels */
876 while ((sk = bt_accept_dequeue(parent, NULL)))
877 l2cap_sock_close(sk);
878
879 parent->sk_state = BT_CLOSED;
880 sock_set_flag(parent, SOCK_ZAPPED);
881}
882
883void __l2cap_sock_close(struct sock *sk, int reason)
884{
Gustavo F. Padovane92c8e72011-04-01 00:53:45 -0300885 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300886 struct l2cap_conn *conn = chan->conn;
Ville Tervo5589fa92011-02-10 22:38:51 -0300887
Gustavo F. Padovan6de07022011-02-04 03:35:20 -0200888 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
889
890 switch (sk->sk_state) {
891 case BT_LISTEN:
892 l2cap_sock_cleanup_listen(sk);
893 break;
894
895 case BT_CONNECTED:
896 case BT_CONFIG:
Ville Tervo5589fa92011-02-10 22:38:51 -0300897 if ((sk->sk_type == SOCK_SEQPACKET ||
898 sk->sk_type == SOCK_STREAM) &&
899 conn->hcon->type == ACL_LINK) {
Gustavo F. Padovan6de07022011-02-04 03:35:20 -0200900 l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
Gustavo F. Padovane92c8e72011-04-01 00:53:45 -0300901 l2cap_send_disconn_req(conn, chan, reason);
Gustavo F. Padovan6de07022011-02-04 03:35:20 -0200902 } else
Gustavo F. Padovane92c8e72011-04-01 00:53:45 -0300903 l2cap_chan_del(chan, reason);
Gustavo F. Padovan6de07022011-02-04 03:35:20 -0200904 break;
905
906 case BT_CONNECT2:
Ville Tervo5589fa92011-02-10 22:38:51 -0300907 if ((sk->sk_type == SOCK_SEQPACKET ||
908 sk->sk_type == SOCK_STREAM) &&
909 conn->hcon->type == ACL_LINK) {
Gustavo F. Padovan6de07022011-02-04 03:35:20 -0200910 struct l2cap_conn_rsp rsp;
911 __u16 result;
912
913 if (bt_sk(sk)->defer_setup)
914 result = L2CAP_CR_SEC_BLOCK;
915 else
916 result = L2CAP_CR_BAD_PSM;
917
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300918 rsp.scid = cpu_to_le16(chan->dcid);
919 rsp.dcid = cpu_to_le16(chan->scid);
Gustavo F. Padovan6de07022011-02-04 03:35:20 -0200920 rsp.result = cpu_to_le16(result);
921 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Gustavo F. Padovane92c8e72011-04-01 00:53:45 -0300922 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
923 sizeof(rsp), &rsp);
Andrei Emeltchenko34bd0272011-03-24 17:16:08 +0200924 }
925
Gustavo F. Padovane92c8e72011-04-01 00:53:45 -0300926 l2cap_chan_del(chan, reason);
Gustavo F. Padovan6de07022011-02-04 03:35:20 -0200927 break;
928
929 case BT_CONNECT:
930 case BT_DISCONN:
Gustavo F. Padovane92c8e72011-04-01 00:53:45 -0300931 l2cap_chan_del(chan, reason);
Gustavo F. Padovan6de07022011-02-04 03:35:20 -0200932 break;
933
934 default:
935 sock_set_flag(sk, SOCK_ZAPPED);
936 break;
937 }
938}
939
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200940static int l2cap_sock_shutdown(struct socket *sock, int how)
941{
942 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300943 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200944 int err = 0;
945
946 BT_DBG("sock %p, sk %p", sock, sk);
947
948 if (!sk)
949 return 0;
950
951 lock_sock(sk);
952 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300953 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200954 err = __l2cap_wait_ack(sk);
955
956 sk->sk_shutdown = SHUTDOWN_MASK;
957 l2cap_sock_clear_timer(sk);
958 __l2cap_sock_close(sk, 0);
959
960 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
961 err = bt_sock_wait_state(sk, BT_CLOSED,
962 sk->sk_lingertime);
963 }
964
965 if (!err && sk->sk_err)
966 err = -sk->sk_err;
967
968 release_sock(sk);
969 return err;
970}
971
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200972static int l2cap_sock_release(struct socket *sock)
973{
974 struct sock *sk = sock->sk;
975 int err;
976
977 BT_DBG("sock %p, sk %p", sock, sk);
978
979 if (!sk)
980 return 0;
981
982 err = l2cap_sock_shutdown(sock, 2);
983
984 sock_orphan(sk);
985 l2cap_sock_kill(sk);
986 return err;
987}
988
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200989static void l2cap_sock_destruct(struct sock *sk)
990{
991 BT_DBG("sk %p", sk);
992
993 skb_queue_purge(&sk->sk_receive_queue);
994 skb_queue_purge(&sk->sk_write_queue);
995}
996
997void l2cap_sock_init(struct sock *sk, struct sock *parent)
998{
999 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001000 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001001
1002 BT_DBG("sk %p", sk);
1003
1004 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001005 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
1006
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001007 sk->sk_type = parent->sk_type;
1008 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
1009
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001010 chan->imtu = pchan->imtu;
1011 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001012 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001013 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001014 chan->fcs = pchan->fcs;
1015 chan->max_tx = pchan->max_tx;
1016 chan->tx_win = pchan->tx_win;
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001017 chan->sec_level = pchan->sec_level;
1018 chan->role_switch = pchan->role_switch;
1019 chan->force_reliable = pchan->force_reliable;
1020 chan->flushable = pchan->flushable;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001021 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001022 chan->imtu = L2CAP_DEFAULT_MTU;
1023 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001024 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001025 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001026 chan->conf_state |= L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001027 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001028 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001029 }
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001030 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
1031 chan->fcs = L2CAP_FCS_CRC16;
1032 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001033 chan->sec_level = BT_SECURITY_LOW;
1034 chan->role_switch = 0;
1035 chan->force_reliable = 0;
1036 chan->flushable = BT_FLUSHABLE_OFF;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001037 }
1038
1039 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001040 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001041}
1042
1043static struct proto l2cap_proto = {
1044 .name = "L2CAP",
1045 .owner = THIS_MODULE,
1046 .obj_size = sizeof(struct l2cap_pinfo)
1047};
1048
1049struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
1050{
1051 struct sock *sk;
1052
1053 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1054 if (!sk)
1055 return NULL;
1056
1057 sock_init_data(sock, sk);
1058 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1059
1060 sk->sk_destruct = l2cap_sock_destruct;
1061 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
1062
1063 sock_reset_flag(sk, SOCK_ZAPPED);
1064
1065 sk->sk_protocol = proto;
1066 sk->sk_state = BT_OPEN;
1067
1068 setup_timer(&sk->sk_timer, l2cap_sock_timeout, (unsigned long) sk);
1069
1070 bt_sock_link(&l2cap_sk_list, sk);
1071 return sk;
1072}
1073
1074static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1075 int kern)
1076{
1077 struct sock *sk;
Gustavo F. Padovan5d41ce12011-04-08 15:40:02 -03001078 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001079
1080 BT_DBG("sock %p", sock);
1081
1082 sock->state = SS_UNCONNECTED;
1083
1084 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1085 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1086 return -ESOCKTNOSUPPORT;
1087
1088 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1089 return -EPERM;
1090
1091 sock->ops = &l2cap_sock_ops;
1092
1093 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1094 if (!sk)
1095 return -ENOMEM;
1096
Gustavo F. Padovan5d41ce12011-04-08 15:40:02 -03001097 chan = l2cap_chan_alloc(sk);
1098 if (!chan) {
1099 l2cap_sock_kill(sk);
1100 return -ENOMEM;
1101 }
1102
1103 l2cap_pi(sk)->chan = chan;
1104
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001105 l2cap_sock_init(sk, NULL);
1106 return 0;
1107}
1108
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001109const struct proto_ops l2cap_sock_ops = {
1110 .family = PF_BLUETOOTH,
1111 .owner = THIS_MODULE,
1112 .release = l2cap_sock_release,
1113 .bind = l2cap_sock_bind,
1114 .connect = l2cap_sock_connect,
1115 .listen = l2cap_sock_listen,
1116 .accept = l2cap_sock_accept,
1117 .getname = l2cap_sock_getname,
1118 .sendmsg = l2cap_sock_sendmsg,
1119 .recvmsg = l2cap_sock_recvmsg,
1120 .poll = bt_sock_poll,
1121 .ioctl = bt_sock_ioctl,
1122 .mmap = sock_no_mmap,
1123 .socketpair = sock_no_socketpair,
1124 .shutdown = l2cap_sock_shutdown,
1125 .setsockopt = l2cap_sock_setsockopt,
1126 .getsockopt = l2cap_sock_getsockopt
1127};
1128
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001129static const struct net_proto_family l2cap_sock_family_ops = {
1130 .family = PF_BLUETOOTH,
1131 .owner = THIS_MODULE,
1132 .create = l2cap_sock_create,
1133};
1134
1135int __init l2cap_init_sockets(void)
1136{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001137 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001138
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001139 err = proto_register(&l2cap_proto, 0);
1140 if (err < 0)
1141 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001142
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001143 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1144 if (err < 0)
1145 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001146
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001147 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001148
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001149 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001150
1151error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001152 BT_ERR("L2CAP socket registration failed");
1153 proto_unregister(&l2cap_proto);
1154 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001155}
1156
1157void l2cap_cleanup_sockets(void)
1158{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001159 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1160 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001161
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001162 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001163}