blob: 5c406d3136f74fc206380968a0a98c3c4b742474 [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
Paul Moore6230c9b2011-10-07 09:40:59 +000029#include <linux/security.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040030#include <linux/export.h>
Paul Moore6230c9b2011-10-07 09:40:59 +000031
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020032#include <net/bluetooth/bluetooth.h>
Gustavo F. Padovan33575df2011-02-04 02:48:48 -020033#include <net/bluetooth/hci_core.h>
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020034#include <net/bluetooth/l2cap.h>
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -030035#include <net/bluetooth/smp.h>
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020036
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030037static const struct proto_ops l2cap_sock_ops;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -030038static void l2cap_sock_init(struct sock *sk, struct sock *parent);
39static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio);
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030040
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020041static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
42{
43 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -030044 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020045 struct sockaddr_l2 la;
46 int len, err = 0;
47
48 BT_DBG("sk %p", sk);
49
50 if (!addr || addr->sa_family != AF_BLUETOOTH)
51 return -EINVAL;
52
53 memset(&la, 0, sizeof(la));
54 len = min_t(unsigned int, sizeof(la), alen);
55 memcpy(&la, addr, len);
56
Ville Tervob62f3282011-02-10 22:38:50 -030057 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020058 return -EINVAL;
59
60 lock_sock(sk);
61
62 if (sk->sk_state != BT_OPEN) {
63 err = -EBADFD;
64 goto done;
65 }
66
67 if (la.l2_psm) {
68 __u16 psm = __le16_to_cpu(la.l2_psm);
69
70 /* PSM must be odd and lsb of upper byte must be 0 */
71 if ((psm & 0x0101) != 0x0001) {
72 err = -EINVAL;
73 goto done;
74 }
75
76 /* Restrict usage of well-known PSMs */
77 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
78 err = -EACCES;
79 goto done;
80 }
81 }
82
Ville Tervob62f3282011-02-10 22:38:50 -030083 if (la.l2_cid)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030084 err = l2cap_add_scid(chan, la.l2_cid);
85 else
86 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
Ville Tervob62f3282011-02-10 22:38:50 -030087
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030088 if (err < 0)
89 goto done;
90
91 if (__le16_to_cpu(la.l2_psm) == 0x0001 ||
92 __le16_to_cpu(la.l2_psm) == 0x0003)
93 chan->sec_level = BT_SECURITY_SDP;
94
95 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -030096
97 chan->state = BT_BOUND;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030098 sk->sk_state = BT_BOUND;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020099
100done:
101 release_sock(sk);
102 return err;
103}
104
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200105static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
106{
107 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300108 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200109 struct sockaddr_l2 la;
110 int len, err = 0;
111
112 BT_DBG("sk %p", sk);
113
114 if (!addr || alen < sizeof(addr->sa_family) ||
115 addr->sa_family != AF_BLUETOOTH)
116 return -EINVAL;
117
118 memset(&la, 0, sizeof(la));
119 len = min_t(unsigned int, sizeof(la), alen);
120 memcpy(&la, addr, len);
121
Ville Tervoacd7d372011-02-10 22:38:49 -0300122 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200123 return -EINVAL;
124
125 lock_sock(sk);
126
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300127 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED
Ville Tervoacd7d372011-02-10 22:38:49 -0300128 && !(la.l2_psm || la.l2_cid)) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200129 err = -EINVAL;
130 goto done;
131 }
132
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300133 switch (chan->mode) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200134 case L2CAP_MODE_BASIC:
135 break;
136 case L2CAP_MODE_ERTM:
137 case L2CAP_MODE_STREAMING:
138 if (!disable_ertm)
139 break;
140 /* fall through */
141 default:
142 err = -ENOTSUPP;
143 goto done;
144 }
145
146 switch (sk->sk_state) {
147 case BT_CONNECT:
148 case BT_CONNECT2:
149 case BT_CONFIG:
150 /* Already connecting */
151 goto wait;
152
153 case BT_CONNECTED:
154 /* Already connected */
155 err = -EISCONN;
156 goto done;
157
158 case BT_OPEN:
159 case BT_BOUND:
160 /* Can connect */
161 break;
162
163 default:
164 err = -EBADFD;
165 goto done;
166 }
167
168 /* PSM must be odd and lsb of upper byte must be 0 */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300169 if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 && !la.l2_cid &&
170 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200171 err = -EINVAL;
172 goto done;
173 }
174
175 /* Set destination address and psm */
176 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300177 chan->psm = la.l2_psm;
178 chan->dcid = la.l2_cid;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200179
Gustavo F. Padovan77a74c72011-04-12 18:17:14 -0300180 err = l2cap_chan_connect(l2cap_pi(sk)->chan);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200181 if (err)
182 goto done;
183
184wait:
185 err = bt_sock_wait_state(sk, BT_CONNECTED,
186 sock_sndtimeo(sk, flags & O_NONBLOCK));
187done:
188 release_sock(sk);
189 return err;
190}
191
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200192static int l2cap_sock_listen(struct socket *sock, int backlog)
193{
194 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300195 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200196 int err = 0;
197
198 BT_DBG("sk %p backlog %d", sk, backlog);
199
200 lock_sock(sk);
201
202 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
203 || sk->sk_state != BT_BOUND) {
204 err = -EBADFD;
205 goto done;
206 }
207
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300208 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200209 case L2CAP_MODE_BASIC:
210 break;
211 case L2CAP_MODE_ERTM:
212 case L2CAP_MODE_STREAMING:
213 if (!disable_ertm)
214 break;
215 /* fall through */
216 default:
217 err = -ENOTSUPP;
218 goto done;
219 }
220
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200221 sk->sk_max_ack_backlog = backlog;
222 sk->sk_ack_backlog = 0;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300223
224 chan->state = BT_LISTEN;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200225 sk->sk_state = BT_LISTEN;
226
227done:
228 release_sock(sk);
229 return err;
230}
231
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200232static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
233{
234 DECLARE_WAITQUEUE(wait, current);
235 struct sock *sk = sock->sk, *nsk;
236 long timeo;
237 int err = 0;
238
239 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
240
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200241 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
242
243 BT_DBG("sk %p timeo %ld", sk, timeo);
244
245 /* Wait for an incoming connection. (wake-one). */
246 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400247 while (1) {
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200248 set_current_state(TASK_INTERRUPTIBLE);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200249
250 if (sk->sk_state != BT_LISTEN) {
251 err = -EBADFD;
252 break;
253 }
254
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400255 nsk = bt_accept_dequeue(sk, newsock);
256 if (nsk)
257 break;
258
259 if (!timeo) {
260 err = -EAGAIN;
261 break;
262 }
263
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200264 if (signal_pending(current)) {
265 err = sock_intr_errno(timeo);
266 break;
267 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400268
269 release_sock(sk);
270 timeo = schedule_timeout(timeo);
271 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200272 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400273 __set_current_state(TASK_RUNNING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200274 remove_wait_queue(sk_sleep(sk), &wait);
275
276 if (err)
277 goto done;
278
279 newsock->state = SS_CONNECTED;
280
281 BT_DBG("new socket %p", nsk);
282
283done:
284 release_sock(sk);
285 return err;
286}
287
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200288static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
289{
290 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
291 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300292 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200293
294 BT_DBG("sock %p, sk %p", sock, sk);
295
296 addr->sa_family = AF_BLUETOOTH;
297 *len = sizeof(struct sockaddr_l2);
298
299 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300300 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200301 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300302 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200303 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300304 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200305 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300306 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200307 }
308
309 return 0;
310}
311
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200312static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
313{
314 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300315 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200316 struct l2cap_options opts;
317 struct l2cap_conninfo cinfo;
318 int len, err = 0;
319 u32 opt;
320
321 BT_DBG("sk %p", sk);
322
323 if (get_user(len, optlen))
324 return -EFAULT;
325
326 lock_sock(sk);
327
328 switch (optname) {
329 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300330 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300331 opts.imtu = chan->imtu;
332 opts.omtu = chan->omtu;
333 opts.flush_to = chan->flush_to;
334 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300335 opts.fcs = chan->fcs;
336 opts.max_tx = chan->max_tx;
337 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200338
339 len = min_t(unsigned int, len, sizeof(opts));
340 if (copy_to_user(optval, (char *) &opts, len))
341 err = -EFAULT;
342
343 break;
344
345 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300346 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200347 case BT_SECURITY_LOW:
348 opt = L2CAP_LM_AUTH;
349 break;
350 case BT_SECURITY_MEDIUM:
351 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
352 break;
353 case BT_SECURITY_HIGH:
354 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
355 L2CAP_LM_SECURE;
356 break;
357 default:
358 opt = 0;
359 break;
360 }
361
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300362 if (chan->role_switch)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200363 opt |= L2CAP_LM_MASTER;
364
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300365 if (chan->force_reliable)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200366 opt |= L2CAP_LM_RELIABLE;
367
368 if (put_user(opt, (u32 __user *) optval))
369 err = -EFAULT;
370 break;
371
372 case L2CAP_CONNINFO:
373 if (sk->sk_state != BT_CONNECTED &&
374 !(sk->sk_state == BT_CONNECT2 &&
375 bt_sk(sk)->defer_setup)) {
376 err = -ENOTCONN;
377 break;
378 }
379
Filip Palian8d03e972011-05-12 19:32:46 +0200380 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300381 cinfo.hci_handle = chan->conn->hcon->handle;
382 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200383
384 len = min_t(unsigned int, len, sizeof(cinfo));
385 if (copy_to_user(optval, (char *) &cinfo, len))
386 err = -EFAULT;
387
388 break;
389
390 default:
391 err = -ENOPROTOOPT;
392 break;
393 }
394
395 release_sock(sk);
396 return err;
397}
398
399static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
400{
401 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300402 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200403 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700404 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200405 int len, err = 0;
406
407 BT_DBG("sk %p", sk);
408
409 if (level == SOL_L2CAP)
410 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
411
412 if (level != SOL_BLUETOOTH)
413 return -ENOPROTOOPT;
414
415 if (get_user(len, optlen))
416 return -EFAULT;
417
418 lock_sock(sk);
419
420 switch (optname) {
421 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300422 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
423 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200424 err = -EINVAL;
425 break;
426 }
427
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300428 memset(&sec, 0, sizeof(sec));
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300429 sec.level = chan->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200430
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300431 if (sk->sk_state == BT_CONNECTED)
432 sec.key_size = chan->conn->hcon->enc_key_size;
433
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200434 len = min_t(unsigned int, len, sizeof(sec));
435 if (copy_to_user(optval, (char *) &sec, len))
436 err = -EFAULT;
437
438 break;
439
440 case BT_DEFER_SETUP:
441 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
442 err = -EINVAL;
443 break;
444 }
445
446 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
447 err = -EFAULT;
448
449 break;
450
451 case BT_FLUSHABLE:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300452 if (put_user(chan->flushable, (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200453 err = -EFAULT;
454
455 break;
456
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700457 case BT_POWER:
458 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
459 && sk->sk_type != SOCK_RAW) {
460 err = -EINVAL;
461 break;
462 }
463
464 pwr.force_active = chan->force_active;
465
466 len = min_t(unsigned int, len, sizeof(pwr));
467 if (copy_to_user(optval, (char *) &pwr, len))
468 err = -EFAULT;
469
470 break;
471
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200472 default:
473 err = -ENOPROTOOPT;
474 break;
475 }
476
477 release_sock(sk);
478 return err;
479}
480
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200481static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
482{
483 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300484 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200485 struct l2cap_options opts;
486 int len, err = 0;
487 u32 opt;
488
489 BT_DBG("sk %p", sk);
490
491 lock_sock(sk);
492
493 switch (optname) {
494 case L2CAP_OPTIONS:
495 if (sk->sk_state == BT_CONNECTED) {
496 err = -EINVAL;
497 break;
498 }
499
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300500 opts.imtu = chan->imtu;
501 opts.omtu = chan->omtu;
502 opts.flush_to = chan->flush_to;
503 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300504 opts.fcs = chan->fcs;
505 opts.max_tx = chan->max_tx;
506 opts.txwin_size = (__u16)chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200507
508 len = min_t(unsigned int, sizeof(opts), optlen);
509 if (copy_from_user((char *) &opts, optval, len)) {
510 err = -EFAULT;
511 break;
512 }
513
514 if (opts.txwin_size > L2CAP_DEFAULT_TX_WINDOW) {
515 err = -EINVAL;
516 break;
517 }
518
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300519 chan->mode = opts.mode;
520 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200521 case L2CAP_MODE_BASIC:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -0300522 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200523 break;
524 case L2CAP_MODE_ERTM:
525 case L2CAP_MODE_STREAMING:
526 if (!disable_ertm)
527 break;
528 /* fall through */
529 default:
530 err = -EINVAL;
531 break;
532 }
533
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300534 chan->imtu = opts.imtu;
535 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300536 chan->fcs = opts.fcs;
537 chan->max_tx = opts.max_tx;
538 chan->tx_win = (__u8)opts.txwin_size;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200539 break;
540
541 case L2CAP_LM:
542 if (get_user(opt, (u32 __user *) optval)) {
543 err = -EFAULT;
544 break;
545 }
546
547 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300548 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200549 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300550 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200551 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300552 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200553
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300554 chan->role_switch = (opt & L2CAP_LM_MASTER);
555 chan->force_reliable = (opt & L2CAP_LM_RELIABLE);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200556 break;
557
558 default:
559 err = -ENOPROTOOPT;
560 break;
561 }
562
563 release_sock(sk);
564 return err;
565}
566
567static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
568{
569 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300570 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200571 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700572 struct bt_power pwr;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300573 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200574 int len, err = 0;
575 u32 opt;
576
577 BT_DBG("sk %p", sk);
578
579 if (level == SOL_L2CAP)
580 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
581
582 if (level != SOL_BLUETOOTH)
583 return -ENOPROTOOPT;
584
585 lock_sock(sk);
586
587 switch (optname) {
588 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300589 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
590 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200591 err = -EINVAL;
592 break;
593 }
594
595 sec.level = BT_SECURITY_LOW;
596
597 len = min_t(unsigned int, sizeof(sec), optlen);
598 if (copy_from_user((char *) &sec, optval, len)) {
599 err = -EFAULT;
600 break;
601 }
602
603 if (sec.level < BT_SECURITY_LOW ||
604 sec.level > BT_SECURITY_HIGH) {
605 err = -EINVAL;
606 break;
607 }
608
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300609 chan->sec_level = sec.level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300610
611 conn = chan->conn;
612 if (conn && chan->scid == L2CAP_CID_LE_DATA) {
613 if (!conn->hcon->out) {
614 err = -EINVAL;
615 break;
616 }
617
618 if (smp_conn_security(conn, sec.level))
619 break;
620
621 err = 0;
622 sk->sk_state = BT_CONFIG;
623 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200624 break;
625
626 case BT_DEFER_SETUP:
627 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
628 err = -EINVAL;
629 break;
630 }
631
632 if (get_user(opt, (u32 __user *) optval)) {
633 err = -EFAULT;
634 break;
635 }
636
637 bt_sk(sk)->defer_setup = opt;
638 break;
639
640 case BT_FLUSHABLE:
641 if (get_user(opt, (u32 __user *) optval)) {
642 err = -EFAULT;
643 break;
644 }
645
646 if (opt > BT_FLUSHABLE_ON) {
647 err = -EINVAL;
648 break;
649 }
650
651 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300652 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300653 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200654 No Flush support in the LM */
655 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
656 err = -EINVAL;
657 break;
658 }
659 }
660
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300661 chan->flushable = opt;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200662 break;
663
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700664 case BT_POWER:
665 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
666 chan->chan_type != L2CAP_CHAN_RAW) {
667 err = -EINVAL;
668 break;
669 }
670
671 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
672
673 len = min_t(unsigned int, sizeof(pwr), optlen);
674 if (copy_from_user((char *) &pwr, optval, len)) {
675 err = -EFAULT;
676 break;
677 }
678 chan->force_active = pwr.force_active;
679 break;
680
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200681 default:
682 err = -ENOPROTOOPT;
683 break;
684 }
685
686 release_sock(sk);
687 return err;
688}
689
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200690static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
691{
692 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300693 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200694 int err;
695
696 BT_DBG("sock %p, sk %p", sock, sk);
697
698 err = sock_error(sk);
699 if (err)
700 return err;
701
702 if (msg->msg_flags & MSG_OOB)
703 return -EOPNOTSUPP;
704
705 lock_sock(sk);
706
707 if (sk->sk_state != BT_CONNECTED) {
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300708 release_sock(sk);
709 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200710 }
711
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300712 err = l2cap_chan_send(chan, msg, len);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200713
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200714 release_sock(sk);
715 return err;
716}
717
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200718static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
719{
720 struct sock *sk = sock->sk;
Mat Martineaue3281402011-07-07 09:39:02 -0700721 struct l2cap_pinfo *pi = l2cap_pi(sk);
722 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200723
724 lock_sock(sk);
725
726 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300727 sk->sk_state = BT_CONFIG;
728
Mat Martineaue3281402011-07-07 09:39:02 -0700729 __l2cap_connect_rsp_defer(pi->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200730 release_sock(sk);
731 return 0;
732 }
733
734 release_sock(sk);
735
736 if (sock->type == SOCK_STREAM)
Mat Martineaue3281402011-07-07 09:39:02 -0700737 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
738 else
739 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200740
Mat Martineaue3281402011-07-07 09:39:02 -0700741 if (pi->chan->mode != L2CAP_MODE_ERTM)
742 return err;
743
744 /* Attempt to put pending rx data in the socket buffer */
745
746 lock_sock(sk);
747
748 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
749 goto done;
750
751 if (pi->rx_busy_skb) {
752 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
753 pi->rx_busy_skb = NULL;
754 else
755 goto done;
756 }
757
758 /* Restore data flow when half of the receive buffer is
759 * available. This avoids resending large numbers of
760 * frames.
761 */
762 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
763 l2cap_chan_busy(pi->chan, 0);
764
765done:
766 release_sock(sk);
767 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200768}
769
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200770/* Kill socket (only if zapped and orphan)
771 * Must be called on unlocked socket.
772 */
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300773static void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200774{
775 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
776 return;
777
778 BT_DBG("sk %p state %d", sk, sk->sk_state);
779
780 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300781
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300782 l2cap_chan_destroy(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200783 sock_set_flag(sk, SOCK_DEAD);
784 sock_put(sk);
785}
786
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200787static int l2cap_sock_shutdown(struct socket *sock, int how)
788{
789 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300790 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200791 int err = 0;
792
793 BT_DBG("sock %p, sk %p", sock, sk);
794
795 if (!sk)
796 return 0;
797
798 lock_sock(sk);
799 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300800 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200801 err = __l2cap_wait_ack(sk);
802
803 sk->sk_shutdown = SHUTDOWN_MASK;
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300804 l2cap_chan_close(chan, 0);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200805
806 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
807 err = bt_sock_wait_state(sk, BT_CLOSED,
808 sk->sk_lingertime);
809 }
810
811 if (!err && sk->sk_err)
812 err = -sk->sk_err;
813
814 release_sock(sk);
815 return err;
816}
817
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200818static int l2cap_sock_release(struct socket *sock)
819{
820 struct sock *sk = sock->sk;
821 int err;
822
823 BT_DBG("sock %p, sk %p", sock, sk);
824
825 if (!sk)
826 return 0;
827
828 err = l2cap_sock_shutdown(sock, 2);
829
830 sock_orphan(sk);
831 l2cap_sock_kill(sk);
832 return err;
833}
834
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300835static struct l2cap_chan *l2cap_sock_new_connection_cb(void *data)
836{
837 struct sock *sk, *parent = data;
838
839 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
840 GFP_ATOMIC);
841 if (!sk)
842 return NULL;
843
844 l2cap_sock_init(sk, parent);
845
846 return l2cap_pi(sk)->chan;
847}
848
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300849static int l2cap_sock_recv_cb(void *data, struct sk_buff *skb)
850{
Mat Martineaue3281402011-07-07 09:39:02 -0700851 int err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300852 struct sock *sk = data;
Mat Martineaue3281402011-07-07 09:39:02 -0700853 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300854
Mat Martineaue3281402011-07-07 09:39:02 -0700855 if (pi->rx_busy_skb)
856 return -ENOMEM;
857
858 err = sock_queue_rcv_skb(sk, skb);
859
860 /* For ERTM, handle one skb that doesn't fit into the recv
861 * buffer. This is important to do because the data frames
862 * have already been acked, so the skb cannot be discarded.
863 *
864 * Notify the l2cap core that the buffer is full, so the
865 * LOCAL_BUSY state is entered and no more frames are
866 * acked and reassembled until there is buffer space
867 * available.
868 */
869 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
870 pi->rx_busy_skb = skb;
871 l2cap_chan_busy(pi->chan, 1);
872 err = 0;
873 }
874
875 return err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300876}
877
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300878static void l2cap_sock_close_cb(void *data)
879{
880 struct sock *sk = data;
881
882 l2cap_sock_kill(sk);
883}
884
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300885static void l2cap_sock_state_change_cb(void *data, int state)
886{
887 struct sock *sk = data;
888
889 sk->sk_state = state;
890}
891
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300892static struct l2cap_ops l2cap_chan_ops = {
893 .name = "L2CAP Socket Interface",
894 .new_connection = l2cap_sock_new_connection_cb,
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300895 .recv = l2cap_sock_recv_cb,
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300896 .close = l2cap_sock_close_cb,
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300897 .state_change = l2cap_sock_state_change_cb,
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300898};
899
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200900static void l2cap_sock_destruct(struct sock *sk)
901{
902 BT_DBG("sk %p", sk);
903
Mat Martineaue3281402011-07-07 09:39:02 -0700904 if (l2cap_pi(sk)->rx_busy_skb) {
905 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
906 l2cap_pi(sk)->rx_busy_skb = NULL;
907 }
908
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200909 skb_queue_purge(&sk->sk_receive_queue);
910 skb_queue_purge(&sk->sk_write_queue);
911}
912
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300913static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200914{
915 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300916 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200917
918 BT_DBG("sk %p", sk);
919
920 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300921 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
922
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200923 sk->sk_type = parent->sk_type;
924 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
925
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300926 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300927 chan->imtu = pchan->imtu;
928 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300929 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300930 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300931 chan->fcs = pchan->fcs;
932 chan->max_tx = pchan->max_tx;
933 chan->tx_win = pchan->tx_win;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300934 chan->sec_level = pchan->sec_level;
935 chan->role_switch = pchan->role_switch;
936 chan->force_reliable = pchan->force_reliable;
937 chan->flushable = pchan->flushable;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700938 chan->force_active = pchan->force_active;
Paul Moore6230c9b2011-10-07 09:40:59 +0000939
940 security_sk_clone(parent, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200941 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300942
943 switch (sk->sk_type) {
944 case SOCK_RAW:
945 chan->chan_type = L2CAP_CHAN_RAW;
946 break;
947 case SOCK_DGRAM:
948 chan->chan_type = L2CAP_CHAN_CONN_LESS;
949 break;
950 case SOCK_SEQPACKET:
951 case SOCK_STREAM:
952 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
953 break;
954 }
955
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300956 chan->imtu = L2CAP_DEFAULT_MTU;
957 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200958 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300959 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -0300960 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200961 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300962 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200963 }
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300964 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
965 chan->fcs = L2CAP_FCS_CRC16;
966 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300967 chan->sec_level = BT_SECURITY_LOW;
968 chan->role_switch = 0;
969 chan->force_reliable = 0;
970 chan->flushable = BT_FLUSHABLE_OFF;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700971 chan->force_active = BT_POWER_FORCE_ACTIVE_ON;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300972
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200973 }
974
975 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300976 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300977
978 chan->data = sk;
979 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200980}
981
982static struct proto l2cap_proto = {
983 .name = "L2CAP",
984 .owner = THIS_MODULE,
985 .obj_size = sizeof(struct l2cap_pinfo)
986};
987
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300988static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200989{
990 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -0300991 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200992
993 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
994 if (!sk)
995 return NULL;
996
997 sock_init_data(sock, sk);
998 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
999
1000 sk->sk_destruct = l2cap_sock_destruct;
Chen Ganir6be6b112011-07-28 15:42:09 +03001001 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001002
1003 sock_reset_flag(sk, SOCK_ZAPPED);
1004
1005 sk->sk_protocol = proto;
1006 sk->sk_state = BT_OPEN;
1007
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001008 chan = l2cap_chan_create(sk);
1009 if (!chan) {
1010 l2cap_sock_kill(sk);
1011 return NULL;
1012 }
1013
1014 l2cap_pi(sk)->chan = chan;
1015
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001016 return sk;
1017}
1018
1019static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1020 int kern)
1021{
1022 struct sock *sk;
1023
1024 BT_DBG("sock %p", sock);
1025
1026 sock->state = SS_UNCONNECTED;
1027
1028 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1029 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1030 return -ESOCKTNOSUPPORT;
1031
1032 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1033 return -EPERM;
1034
1035 sock->ops = &l2cap_sock_ops;
1036
1037 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1038 if (!sk)
1039 return -ENOMEM;
1040
1041 l2cap_sock_init(sk, NULL);
1042 return 0;
1043}
1044
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -03001045static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001046 .family = PF_BLUETOOTH,
1047 .owner = THIS_MODULE,
1048 .release = l2cap_sock_release,
1049 .bind = l2cap_sock_bind,
1050 .connect = l2cap_sock_connect,
1051 .listen = l2cap_sock_listen,
1052 .accept = l2cap_sock_accept,
1053 .getname = l2cap_sock_getname,
1054 .sendmsg = l2cap_sock_sendmsg,
1055 .recvmsg = l2cap_sock_recvmsg,
1056 .poll = bt_sock_poll,
1057 .ioctl = bt_sock_ioctl,
1058 .mmap = sock_no_mmap,
1059 .socketpair = sock_no_socketpair,
1060 .shutdown = l2cap_sock_shutdown,
1061 .setsockopt = l2cap_sock_setsockopt,
1062 .getsockopt = l2cap_sock_getsockopt
1063};
1064
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001065static const struct net_proto_family l2cap_sock_family_ops = {
1066 .family = PF_BLUETOOTH,
1067 .owner = THIS_MODULE,
1068 .create = l2cap_sock_create,
1069};
1070
1071int __init l2cap_init_sockets(void)
1072{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001073 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001074
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001075 err = proto_register(&l2cap_proto, 0);
1076 if (err < 0)
1077 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001078
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001079 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1080 if (err < 0)
1081 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001082
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001083 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001084
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001085 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001086
1087error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001088 BT_ERR("L2CAP socket registration failed");
1089 proto_unregister(&l2cap_proto);
1090 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001091}
1092
1093void l2cap_cleanup_sockets(void)
1094{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001095 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1096 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001097
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001098 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001099}