blob: 146b614d10ed1ed72b521165eec3907ce9fbd0f1 [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>
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -030032#include <net/bluetooth/smp.h>
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020033
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030034static const struct proto_ops l2cap_sock_ops;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -030035static void l2cap_sock_init(struct sock *sk, struct sock *parent);
36static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio);
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030037
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020038static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
39{
40 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -030041 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020042 struct sockaddr_l2 la;
43 int len, err = 0;
44
45 BT_DBG("sk %p", sk);
46
47 if (!addr || addr->sa_family != AF_BLUETOOTH)
48 return -EINVAL;
49
50 memset(&la, 0, sizeof(la));
51 len = min_t(unsigned int, sizeof(la), alen);
52 memcpy(&la, addr, len);
53
Ville Tervob62f3282011-02-10 22:38:50 -030054 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020055 return -EINVAL;
56
57 lock_sock(sk);
58
59 if (sk->sk_state != BT_OPEN) {
60 err = -EBADFD;
61 goto done;
62 }
63
64 if (la.l2_psm) {
65 __u16 psm = __le16_to_cpu(la.l2_psm);
66
67 /* PSM must be odd and lsb of upper byte must be 0 */
68 if ((psm & 0x0101) != 0x0001) {
69 err = -EINVAL;
70 goto done;
71 }
72
73 /* Restrict usage of well-known PSMs */
74 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
75 err = -EACCES;
76 goto done;
77 }
78 }
79
Ville Tervob62f3282011-02-10 22:38:50 -030080 if (la.l2_cid)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030081 err = l2cap_add_scid(chan, la.l2_cid);
82 else
83 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
Ville Tervob62f3282011-02-10 22:38:50 -030084
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030085 if (err < 0)
86 goto done;
87
88 if (__le16_to_cpu(la.l2_psm) == 0x0001 ||
89 __le16_to_cpu(la.l2_psm) == 0x0003)
90 chan->sec_level = BT_SECURITY_SDP;
91
92 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -030093
94 chan->state = BT_BOUND;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030095 sk->sk_state = BT_BOUND;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020096
97done:
98 release_sock(sk);
99 return err;
100}
101
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200102static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
103{
104 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300105 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200106 struct sockaddr_l2 la;
107 int len, err = 0;
108
109 BT_DBG("sk %p", sk);
110
111 if (!addr || alen < sizeof(addr->sa_family) ||
112 addr->sa_family != AF_BLUETOOTH)
113 return -EINVAL;
114
115 memset(&la, 0, sizeof(la));
116 len = min_t(unsigned int, sizeof(la), alen);
117 memcpy(&la, addr, len);
118
Ville Tervoacd7d372011-02-10 22:38:49 -0300119 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200120 return -EINVAL;
121
122 lock_sock(sk);
123
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300124 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED
Ville Tervoacd7d372011-02-10 22:38:49 -0300125 && !(la.l2_psm || la.l2_cid)) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200126 err = -EINVAL;
127 goto done;
128 }
129
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300130 switch (chan->mode) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200131 case L2CAP_MODE_BASIC:
132 break;
133 case L2CAP_MODE_ERTM:
134 case L2CAP_MODE_STREAMING:
135 if (!disable_ertm)
136 break;
137 /* fall through */
138 default:
139 err = -ENOTSUPP;
140 goto done;
141 }
142
143 switch (sk->sk_state) {
144 case BT_CONNECT:
145 case BT_CONNECT2:
146 case BT_CONFIG:
147 /* Already connecting */
148 goto wait;
149
150 case BT_CONNECTED:
151 /* Already connected */
152 err = -EISCONN;
153 goto done;
154
155 case BT_OPEN:
156 case BT_BOUND:
157 /* Can connect */
158 break;
159
160 default:
161 err = -EBADFD;
162 goto done;
163 }
164
165 /* PSM must be odd and lsb of upper byte must be 0 */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300166 if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 && !la.l2_cid &&
167 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200168 err = -EINVAL;
169 goto done;
170 }
171
172 /* Set destination address and psm */
173 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300174 chan->psm = la.l2_psm;
175 chan->dcid = la.l2_cid;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200176
Gustavo F. Padovan77a74c72011-04-12 18:17:14 -0300177 err = l2cap_chan_connect(l2cap_pi(sk)->chan);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200178 if (err)
179 goto done;
180
181wait:
182 err = bt_sock_wait_state(sk, BT_CONNECTED,
183 sock_sndtimeo(sk, flags & O_NONBLOCK));
184done:
185 release_sock(sk);
186 return err;
187}
188
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200189static int l2cap_sock_listen(struct socket *sock, int backlog)
190{
191 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300192 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200193 int err = 0;
194
195 BT_DBG("sk %p backlog %d", sk, backlog);
196
197 lock_sock(sk);
198
199 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
200 || sk->sk_state != BT_BOUND) {
201 err = -EBADFD;
202 goto done;
203 }
204
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300205 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200206 case L2CAP_MODE_BASIC:
207 break;
208 case L2CAP_MODE_ERTM:
209 case L2CAP_MODE_STREAMING:
210 if (!disable_ertm)
211 break;
212 /* fall through */
213 default:
214 err = -ENOTSUPP;
215 goto done;
216 }
217
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200218 sk->sk_max_ack_backlog = backlog;
219 sk->sk_ack_backlog = 0;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300220
221 chan->state = BT_LISTEN;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200222 sk->sk_state = BT_LISTEN;
223
224done:
225 release_sock(sk);
226 return err;
227}
228
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200229static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
230{
231 DECLARE_WAITQUEUE(wait, current);
232 struct sock *sk = sock->sk, *nsk;
233 long timeo;
234 int err = 0;
235
236 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
237
238 if (sk->sk_state != BT_LISTEN) {
239 err = -EBADFD;
240 goto done;
241 }
242
243 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
244
245 BT_DBG("sk %p timeo %ld", sk, timeo);
246
247 /* Wait for an incoming connection. (wake-one). */
248 add_wait_queue_exclusive(sk_sleep(sk), &wait);
249 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
250 set_current_state(TASK_INTERRUPTIBLE);
251 if (!timeo) {
252 err = -EAGAIN;
253 break;
254 }
255
256 release_sock(sk);
257 timeo = schedule_timeout(timeo);
258 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
259
260 if (sk->sk_state != BT_LISTEN) {
261 err = -EBADFD;
262 break;
263 }
264
265 if (signal_pending(current)) {
266 err = sock_intr_errno(timeo);
267 break;
268 }
269 }
270 set_current_state(TASK_RUNNING);
271 remove_wait_queue(sk_sleep(sk), &wait);
272
273 if (err)
274 goto done;
275
276 newsock->state = SS_CONNECTED;
277
278 BT_DBG("new socket %p", nsk);
279
280done:
281 release_sock(sk);
282 return err;
283}
284
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200285static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
286{
287 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
288 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300289 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200290
291 BT_DBG("sock %p, sk %p", sock, sk);
292
293 addr->sa_family = AF_BLUETOOTH;
294 *len = sizeof(struct sockaddr_l2);
295
296 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300297 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200298 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300299 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200300 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300301 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200302 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300303 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200304 }
305
306 return 0;
307}
308
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200309static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
310{
311 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300312 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200313 struct l2cap_options opts;
314 struct l2cap_conninfo cinfo;
315 int len, err = 0;
316 u32 opt;
317
318 BT_DBG("sk %p", sk);
319
320 if (get_user(len, optlen))
321 return -EFAULT;
322
323 lock_sock(sk);
324
325 switch (optname) {
326 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300327 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300328 opts.imtu = chan->imtu;
329 opts.omtu = chan->omtu;
330 opts.flush_to = chan->flush_to;
331 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300332 opts.fcs = chan->fcs;
333 opts.max_tx = chan->max_tx;
334 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200335
336 len = min_t(unsigned int, len, sizeof(opts));
337 if (copy_to_user(optval, (char *) &opts, len))
338 err = -EFAULT;
339
340 break;
341
342 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300343 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200344 case BT_SECURITY_LOW:
345 opt = L2CAP_LM_AUTH;
346 break;
347 case BT_SECURITY_MEDIUM:
348 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
349 break;
350 case BT_SECURITY_HIGH:
351 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
352 L2CAP_LM_SECURE;
353 break;
354 default:
355 opt = 0;
356 break;
357 }
358
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300359 if (chan->role_switch)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200360 opt |= L2CAP_LM_MASTER;
361
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300362 if (chan->force_reliable)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200363 opt |= L2CAP_LM_RELIABLE;
364
365 if (put_user(opt, (u32 __user *) optval))
366 err = -EFAULT;
367 break;
368
369 case L2CAP_CONNINFO:
370 if (sk->sk_state != BT_CONNECTED &&
371 !(sk->sk_state == BT_CONNECT2 &&
372 bt_sk(sk)->defer_setup)) {
373 err = -ENOTCONN;
374 break;
375 }
376
Filip Palian8d03e972011-05-12 19:32:46 +0200377 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300378 cinfo.hci_handle = chan->conn->hcon->handle;
379 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200380
381 len = min_t(unsigned int, len, sizeof(cinfo));
382 if (copy_to_user(optval, (char *) &cinfo, len))
383 err = -EFAULT;
384
385 break;
386
387 default:
388 err = -ENOPROTOOPT;
389 break;
390 }
391
392 release_sock(sk);
393 return err;
394}
395
396static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
397{
398 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300399 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200400 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700401 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200402 int len, err = 0;
403
404 BT_DBG("sk %p", sk);
405
406 if (level == SOL_L2CAP)
407 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
408
409 if (level != SOL_BLUETOOTH)
410 return -ENOPROTOOPT;
411
412 if (get_user(len, optlen))
413 return -EFAULT;
414
415 lock_sock(sk);
416
417 switch (optname) {
418 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300419 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
420 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200421 err = -EINVAL;
422 break;
423 }
424
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300425 sec.level = chan->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200426
427 len = min_t(unsigned int, len, sizeof(sec));
428 if (copy_to_user(optval, (char *) &sec, len))
429 err = -EFAULT;
430
431 break;
432
433 case BT_DEFER_SETUP:
434 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
435 err = -EINVAL;
436 break;
437 }
438
439 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
440 err = -EFAULT;
441
442 break;
443
444 case BT_FLUSHABLE:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300445 if (put_user(chan->flushable, (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200446 err = -EFAULT;
447
448 break;
449
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700450 case BT_POWER:
451 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
452 && sk->sk_type != SOCK_RAW) {
453 err = -EINVAL;
454 break;
455 }
456
457 pwr.force_active = chan->force_active;
458
459 len = min_t(unsigned int, len, sizeof(pwr));
460 if (copy_to_user(optval, (char *) &pwr, len))
461 err = -EFAULT;
462
463 break;
464
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200465 default:
466 err = -ENOPROTOOPT;
467 break;
468 }
469
470 release_sock(sk);
471 return err;
472}
473
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200474static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
475{
476 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300477 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200478 struct l2cap_options opts;
479 int len, err = 0;
480 u32 opt;
481
482 BT_DBG("sk %p", sk);
483
484 lock_sock(sk);
485
486 switch (optname) {
487 case L2CAP_OPTIONS:
488 if (sk->sk_state == BT_CONNECTED) {
489 err = -EINVAL;
490 break;
491 }
492
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300493 opts.imtu = chan->imtu;
494 opts.omtu = chan->omtu;
495 opts.flush_to = chan->flush_to;
496 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300497 opts.fcs = chan->fcs;
498 opts.max_tx = chan->max_tx;
499 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200500
501 len = min_t(unsigned int, sizeof(opts), optlen);
502 if (copy_from_user((char *) &opts, optval, len)) {
503 err = -EFAULT;
504 break;
505 }
506
507 if (opts.txwin_size > L2CAP_DEFAULT_TX_WINDOW) {
508 err = -EINVAL;
509 break;
510 }
511
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300512 chan->mode = opts.mode;
513 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200514 case L2CAP_MODE_BASIC:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -0300515 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200516 break;
517 case L2CAP_MODE_ERTM:
518 case L2CAP_MODE_STREAMING:
519 if (!disable_ertm)
520 break;
521 /* fall through */
522 default:
523 err = -EINVAL;
524 break;
525 }
526
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300527 chan->imtu = opts.imtu;
528 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300529 chan->fcs = opts.fcs;
530 chan->max_tx = opts.max_tx;
531 chan->tx_win = (__u8)opts.txwin_size;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200532 break;
533
534 case L2CAP_LM:
535 if (get_user(opt, (u32 __user *) optval)) {
536 err = -EFAULT;
537 break;
538 }
539
540 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300541 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200542 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300543 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200544 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300545 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200546
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300547 chan->role_switch = (opt & L2CAP_LM_MASTER);
548 chan->force_reliable = (opt & L2CAP_LM_RELIABLE);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200549 break;
550
551 default:
552 err = -ENOPROTOOPT;
553 break;
554 }
555
556 release_sock(sk);
557 return err;
558}
559
560static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
561{
562 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300563 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200564 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700565 struct bt_power pwr;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300566 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200567 int len, err = 0;
568 u32 opt;
569
570 BT_DBG("sk %p", sk);
571
572 if (level == SOL_L2CAP)
573 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
574
575 if (level != SOL_BLUETOOTH)
576 return -ENOPROTOOPT;
577
578 lock_sock(sk);
579
580 switch (optname) {
581 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300582 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
583 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200584 err = -EINVAL;
585 break;
586 }
587
588 sec.level = BT_SECURITY_LOW;
589
590 len = min_t(unsigned int, sizeof(sec), optlen);
591 if (copy_from_user((char *) &sec, optval, len)) {
592 err = -EFAULT;
593 break;
594 }
595
596 if (sec.level < BT_SECURITY_LOW ||
597 sec.level > BT_SECURITY_HIGH) {
598 err = -EINVAL;
599 break;
600 }
601
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300602 chan->sec_level = sec.level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300603
604 conn = chan->conn;
605 if (conn && chan->scid == L2CAP_CID_LE_DATA) {
606 if (!conn->hcon->out) {
607 err = -EINVAL;
608 break;
609 }
610
611 if (smp_conn_security(conn, sec.level))
612 break;
613
614 err = 0;
615 sk->sk_state = BT_CONFIG;
616 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200617 break;
618
619 case BT_DEFER_SETUP:
620 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
621 err = -EINVAL;
622 break;
623 }
624
625 if (get_user(opt, (u32 __user *) optval)) {
626 err = -EFAULT;
627 break;
628 }
629
630 bt_sk(sk)->defer_setup = opt;
631 break;
632
633 case BT_FLUSHABLE:
634 if (get_user(opt, (u32 __user *) optval)) {
635 err = -EFAULT;
636 break;
637 }
638
639 if (opt > BT_FLUSHABLE_ON) {
640 err = -EINVAL;
641 break;
642 }
643
644 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300645 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300646 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200647 No Flush support in the LM */
648 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
649 err = -EINVAL;
650 break;
651 }
652 }
653
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300654 chan->flushable = opt;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200655 break;
656
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700657 case BT_POWER:
658 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
659 chan->chan_type != L2CAP_CHAN_RAW) {
660 err = -EINVAL;
661 break;
662 }
663
664 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
665
666 len = min_t(unsigned int, sizeof(pwr), optlen);
667 if (copy_from_user((char *) &pwr, optval, len)) {
668 err = -EFAULT;
669 break;
670 }
671 chan->force_active = pwr.force_active;
672 break;
673
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200674 default:
675 err = -ENOPROTOOPT;
676 break;
677 }
678
679 release_sock(sk);
680 return err;
681}
682
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200683static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
684{
685 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300686 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200687 int err;
688
689 BT_DBG("sock %p, sk %p", sock, sk);
690
691 err = sock_error(sk);
692 if (err)
693 return err;
694
695 if (msg->msg_flags & MSG_OOB)
696 return -EOPNOTSUPP;
697
698 lock_sock(sk);
699
700 if (sk->sk_state != BT_CONNECTED) {
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300701 release_sock(sk);
702 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200703 }
704
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300705 err = l2cap_chan_send(chan, msg, len);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200706
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200707 release_sock(sk);
708 return err;
709}
710
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200711static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
712{
713 struct sock *sk = sock->sk;
Mat Martineaue3281402011-07-07 09:39:02 -0700714 struct l2cap_pinfo *pi = l2cap_pi(sk);
715 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200716
717 lock_sock(sk);
718
719 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300720 sk->sk_state = BT_CONFIG;
721
Mat Martineaue3281402011-07-07 09:39:02 -0700722 __l2cap_connect_rsp_defer(pi->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200723 release_sock(sk);
724 return 0;
725 }
726
727 release_sock(sk);
728
729 if (sock->type == SOCK_STREAM)
Mat Martineaue3281402011-07-07 09:39:02 -0700730 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
731 else
732 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200733
Mat Martineaue3281402011-07-07 09:39:02 -0700734 if (pi->chan->mode != L2CAP_MODE_ERTM)
735 return err;
736
737 /* Attempt to put pending rx data in the socket buffer */
738
739 lock_sock(sk);
740
741 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
742 goto done;
743
744 if (pi->rx_busy_skb) {
745 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
746 pi->rx_busy_skb = NULL;
747 else
748 goto done;
749 }
750
751 /* Restore data flow when half of the receive buffer is
752 * available. This avoids resending large numbers of
753 * frames.
754 */
755 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
756 l2cap_chan_busy(pi->chan, 0);
757
758done:
759 release_sock(sk);
760 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200761}
762
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200763/* Kill socket (only if zapped and orphan)
764 * Must be called on unlocked socket.
765 */
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300766static void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200767{
768 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
769 return;
770
771 BT_DBG("sk %p state %d", sk, sk->sk_state);
772
773 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300774
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300775 l2cap_chan_destroy(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200776 sock_set_flag(sk, SOCK_DEAD);
777 sock_put(sk);
778}
779
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200780static int l2cap_sock_shutdown(struct socket *sock, int how)
781{
782 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300783 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200784 int err = 0;
785
786 BT_DBG("sock %p, sk %p", sock, sk);
787
788 if (!sk)
789 return 0;
790
791 lock_sock(sk);
792 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300793 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200794 err = __l2cap_wait_ack(sk);
795
796 sk->sk_shutdown = SHUTDOWN_MASK;
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300797 l2cap_chan_close(chan, 0);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200798
799 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
800 err = bt_sock_wait_state(sk, BT_CLOSED,
801 sk->sk_lingertime);
802 }
803
804 if (!err && sk->sk_err)
805 err = -sk->sk_err;
806
807 release_sock(sk);
808 return err;
809}
810
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200811static int l2cap_sock_release(struct socket *sock)
812{
813 struct sock *sk = sock->sk;
814 int err;
815
816 BT_DBG("sock %p, sk %p", sock, sk);
817
818 if (!sk)
819 return 0;
820
821 err = l2cap_sock_shutdown(sock, 2);
822
823 sock_orphan(sk);
824 l2cap_sock_kill(sk);
825 return err;
826}
827
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300828static struct l2cap_chan *l2cap_sock_new_connection_cb(void *data)
829{
830 struct sock *sk, *parent = data;
831
832 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
833 GFP_ATOMIC);
834 if (!sk)
835 return NULL;
836
837 l2cap_sock_init(sk, parent);
838
839 return l2cap_pi(sk)->chan;
840}
841
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300842static int l2cap_sock_recv_cb(void *data, struct sk_buff *skb)
843{
Mat Martineaue3281402011-07-07 09:39:02 -0700844 int err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300845 struct sock *sk = data;
Mat Martineaue3281402011-07-07 09:39:02 -0700846 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300847
Mat Martineaue3281402011-07-07 09:39:02 -0700848 if (pi->rx_busy_skb)
849 return -ENOMEM;
850
851 err = sock_queue_rcv_skb(sk, skb);
852
853 /* For ERTM, handle one skb that doesn't fit into the recv
854 * buffer. This is important to do because the data frames
855 * have already been acked, so the skb cannot be discarded.
856 *
857 * Notify the l2cap core that the buffer is full, so the
858 * LOCAL_BUSY state is entered and no more frames are
859 * acked and reassembled until there is buffer space
860 * available.
861 */
862 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
863 pi->rx_busy_skb = skb;
864 l2cap_chan_busy(pi->chan, 1);
865 err = 0;
866 }
867
868 return err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300869}
870
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300871static void l2cap_sock_close_cb(void *data)
872{
873 struct sock *sk = data;
874
875 l2cap_sock_kill(sk);
876}
877
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300878static void l2cap_sock_state_change_cb(void *data, int state)
879{
880 struct sock *sk = data;
881
882 sk->sk_state = state;
883}
884
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300885static struct l2cap_ops l2cap_chan_ops = {
886 .name = "L2CAP Socket Interface",
887 .new_connection = l2cap_sock_new_connection_cb,
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300888 .recv = l2cap_sock_recv_cb,
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300889 .close = l2cap_sock_close_cb,
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300890 .state_change = l2cap_sock_state_change_cb,
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300891};
892
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200893static void l2cap_sock_destruct(struct sock *sk)
894{
895 BT_DBG("sk %p", sk);
896
Mat Martineaue3281402011-07-07 09:39:02 -0700897 if (l2cap_pi(sk)->rx_busy_skb) {
898 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
899 l2cap_pi(sk)->rx_busy_skb = NULL;
900 }
901
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200902 skb_queue_purge(&sk->sk_receive_queue);
903 skb_queue_purge(&sk->sk_write_queue);
904}
905
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300906static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200907{
908 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300909 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200910
911 BT_DBG("sk %p", sk);
912
913 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300914 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
915
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200916 sk->sk_type = parent->sk_type;
917 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
918
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300919 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300920 chan->imtu = pchan->imtu;
921 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300922 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300923 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300924 chan->fcs = pchan->fcs;
925 chan->max_tx = pchan->max_tx;
926 chan->tx_win = pchan->tx_win;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300927 chan->sec_level = pchan->sec_level;
928 chan->role_switch = pchan->role_switch;
929 chan->force_reliable = pchan->force_reliable;
930 chan->flushable = pchan->flushable;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700931 chan->force_active = pchan->force_active;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200932 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300933
934 switch (sk->sk_type) {
935 case SOCK_RAW:
936 chan->chan_type = L2CAP_CHAN_RAW;
937 break;
938 case SOCK_DGRAM:
939 chan->chan_type = L2CAP_CHAN_CONN_LESS;
940 break;
941 case SOCK_SEQPACKET:
942 case SOCK_STREAM:
943 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
944 break;
945 }
946
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300947 chan->imtu = L2CAP_DEFAULT_MTU;
948 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200949 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300950 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -0300951 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200952 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300953 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200954 }
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300955 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
956 chan->fcs = L2CAP_FCS_CRC16;
957 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300958 chan->sec_level = BT_SECURITY_LOW;
959 chan->role_switch = 0;
960 chan->force_reliable = 0;
961 chan->flushable = BT_FLUSHABLE_OFF;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700962 chan->force_active = BT_POWER_FORCE_ACTIVE_ON;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300963
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200964 }
965
966 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300967 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300968
969 chan->data = sk;
970 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200971}
972
973static struct proto l2cap_proto = {
974 .name = "L2CAP",
975 .owner = THIS_MODULE,
976 .obj_size = sizeof(struct l2cap_pinfo)
977};
978
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300979static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200980{
981 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -0300982 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200983
984 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
985 if (!sk)
986 return NULL;
987
988 sock_init_data(sock, sk);
989 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
990
991 sk->sk_destruct = l2cap_sock_destruct;
992 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
993
994 sock_reset_flag(sk, SOCK_ZAPPED);
995
996 sk->sk_protocol = proto;
997 sk->sk_state = BT_OPEN;
998
Gustavo F. Padovandc50a062011-05-16 16:42:01 -0300999 chan = l2cap_chan_create(sk);
1000 if (!chan) {
1001 l2cap_sock_kill(sk);
1002 return NULL;
1003 }
1004
1005 l2cap_pi(sk)->chan = chan;
1006
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001007 return sk;
1008}
1009
1010static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1011 int kern)
1012{
1013 struct sock *sk;
1014
1015 BT_DBG("sock %p", sock);
1016
1017 sock->state = SS_UNCONNECTED;
1018
1019 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1020 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1021 return -ESOCKTNOSUPPORT;
1022
1023 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1024 return -EPERM;
1025
1026 sock->ops = &l2cap_sock_ops;
1027
1028 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1029 if (!sk)
1030 return -ENOMEM;
1031
1032 l2cap_sock_init(sk, NULL);
1033 return 0;
1034}
1035
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -03001036static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001037 .family = PF_BLUETOOTH,
1038 .owner = THIS_MODULE,
1039 .release = l2cap_sock_release,
1040 .bind = l2cap_sock_bind,
1041 .connect = l2cap_sock_connect,
1042 .listen = l2cap_sock_listen,
1043 .accept = l2cap_sock_accept,
1044 .getname = l2cap_sock_getname,
1045 .sendmsg = l2cap_sock_sendmsg,
1046 .recvmsg = l2cap_sock_recvmsg,
1047 .poll = bt_sock_poll,
1048 .ioctl = bt_sock_ioctl,
1049 .mmap = sock_no_mmap,
1050 .socketpair = sock_no_socketpair,
1051 .shutdown = l2cap_sock_shutdown,
1052 .setsockopt = l2cap_sock_setsockopt,
1053 .getsockopt = l2cap_sock_getsockopt
1054};
1055
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001056static const struct net_proto_family l2cap_sock_family_ops = {
1057 .family = PF_BLUETOOTH,
1058 .owner = THIS_MODULE,
1059 .create = l2cap_sock_create,
1060};
1061
1062int __init l2cap_init_sockets(void)
1063{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001064 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001065
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001066 err = proto_register(&l2cap_proto, 0);
1067 if (err < 0)
1068 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001069
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001070 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1071 if (err < 0)
1072 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001073
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001074 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001075
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001076 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001077
1078error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001079 BT_ERR("L2CAP socket registration failed");
1080 proto_unregister(&l2cap_proto);
1081 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001082}
1083
1084void l2cap_cleanup_sockets(void)
1085{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001086 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1087 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001088
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001089 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001090}