blob: af467ce2cf2be070b08e4558aa13268ca52c0e52 [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.
Gustavo F. Padovan590051d2011-12-18 13:39:33 -02006 Copyright (C) 2011 ProFUSION Embedded Systems
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007
8 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License version 2 as
12 published by the Free Software Foundation;
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
17 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
18 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
24 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
25 SOFTWARE IS DISCLAIMED.
26*/
27
28/* Bluetooth L2CAP sockets. */
29
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
Masatake YAMATO5b28d952012-07-26 01:29:25 +090037static struct bt_sock_list l2cap_sk_list = {
38 .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock)
39};
40
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030041static const struct proto_ops l2cap_sock_ops;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -030042static void l2cap_sock_init(struct sock *sk, struct sock *parent);
43static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio);
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030044
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020045static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
46{
47 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -030048 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020049 struct sockaddr_l2 la;
50 int len, err = 0;
51
52 BT_DBG("sk %p", sk);
53
54 if (!addr || addr->sa_family != AF_BLUETOOTH)
55 return -EINVAL;
56
57 memset(&la, 0, sizeof(la));
58 len = min_t(unsigned int, sizeof(la), alen);
59 memcpy(&la, addr, len);
60
Ville Tervob62f3282011-02-10 22:38:50 -030061 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020062 return -EINVAL;
63
64 lock_sock(sk);
65
66 if (sk->sk_state != BT_OPEN) {
67 err = -EBADFD;
68 goto done;
69 }
70
71 if (la.l2_psm) {
72 __u16 psm = __le16_to_cpu(la.l2_psm);
73
74 /* PSM must be odd and lsb of upper byte must be 0 */
75 if ((psm & 0x0101) != 0x0001) {
76 err = -EINVAL;
77 goto done;
78 }
79
80 /* Restrict usage of well-known PSMs */
81 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
82 err = -EACCES;
83 goto done;
84 }
85 }
86
Ville Tervob62f3282011-02-10 22:38:50 -030087 if (la.l2_cid)
Santosh Nayak6e4aff12012-03-01 22:46:36 +053088 err = l2cap_add_scid(chan, __le16_to_cpu(la.l2_cid));
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030089 else
90 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
Ville Tervob62f3282011-02-10 22:38:50 -030091
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030092 if (err < 0)
93 goto done;
94
Andrei Emeltchenko2983fd62012-05-24 15:42:50 +030095 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP ||
96 __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -030097 chan->sec_level = BT_SECURITY_SDP;
98
99 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300100
101 chan->state = BT_BOUND;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300102 sk->sk_state = BT_BOUND;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200103
104done:
105 release_sock(sk);
106 return err;
107}
108
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200109static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
110{
111 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300112 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200113 struct sockaddr_l2 la;
114 int len, err = 0;
115
116 BT_DBG("sk %p", sk);
117
118 if (!addr || alen < sizeof(addr->sa_family) ||
119 addr->sa_family != AF_BLUETOOTH)
120 return -EINVAL;
121
122 memset(&la, 0, sizeof(la));
123 len = min_t(unsigned int, sizeof(la), alen);
124 memcpy(&la, addr, len);
125
Ville Tervoacd7d372011-02-10 22:38:49 -0300126 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200127 return -EINVAL;
128
Santosh Nayak6e4aff12012-03-01 22:46:36 +0530129 err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid),
Andre Guedes8e9f9892012-04-24 21:02:55 -0300130 &la.l2_bdaddr, la.l2_bdaddr_type);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200131 if (err)
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200132 return err;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200133
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200134 lock_sock(sk);
135
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200136 err = bt_sock_wait_state(sk, BT_CONNECTED,
137 sock_sndtimeo(sk, flags & O_NONBLOCK));
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200138
139 release_sock(sk);
140
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200141 return err;
142}
143
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200144static int l2cap_sock_listen(struct socket *sock, int backlog)
145{
146 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300147 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200148 int err = 0;
149
150 BT_DBG("sk %p backlog %d", sk, backlog);
151
152 lock_sock(sk);
153
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200154 if (sk->sk_state != BT_BOUND) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200155 err = -EBADFD;
156 goto done;
157 }
158
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200159 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) {
160 err = -EINVAL;
161 goto done;
162 }
163
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300164 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200165 case L2CAP_MODE_BASIC:
166 break;
167 case L2CAP_MODE_ERTM:
168 case L2CAP_MODE_STREAMING:
169 if (!disable_ertm)
170 break;
171 /* fall through */
172 default:
173 err = -ENOTSUPP;
174 goto done;
175 }
176
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200177 sk->sk_max_ack_backlog = backlog;
178 sk->sk_ack_backlog = 0;
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300179
180 chan->state = BT_LISTEN;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200181 sk->sk_state = BT_LISTEN;
182
183done:
184 release_sock(sk);
185 return err;
186}
187
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200188static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
189{
190 DECLARE_WAITQUEUE(wait, current);
191 struct sock *sk = sock->sk, *nsk;
192 long timeo;
193 int err = 0;
194
195 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
196
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200197 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
198
199 BT_DBG("sk %p timeo %ld", sk, timeo);
200
201 /* Wait for an incoming connection. (wake-one). */
202 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400203 while (1) {
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200204 set_current_state(TASK_INTERRUPTIBLE);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200205
206 if (sk->sk_state != BT_LISTEN) {
207 err = -EBADFD;
208 break;
209 }
210
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400211 nsk = bt_accept_dequeue(sk, newsock);
212 if (nsk)
213 break;
214
215 if (!timeo) {
216 err = -EAGAIN;
217 break;
218 }
219
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200220 if (signal_pending(current)) {
221 err = sock_intr_errno(timeo);
222 break;
223 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400224
225 release_sock(sk);
226 timeo = schedule_timeout(timeo);
227 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200228 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400229 __set_current_state(TASK_RUNNING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200230 remove_wait_queue(sk_sleep(sk), &wait);
231
232 if (err)
233 goto done;
234
235 newsock->state = SS_CONNECTED;
236
237 BT_DBG("new socket %p", nsk);
238
239done:
240 release_sock(sk);
241 return err;
242}
243
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200244static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
245{
246 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
247 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300248 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200249
250 BT_DBG("sock %p, sk %p", sock, sk);
251
252 addr->sa_family = AF_BLUETOOTH;
253 *len = sizeof(struct sockaddr_l2);
254
255 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300256 la->l2_psm = chan->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200257 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300258 la->l2_cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200259 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300260 la->l2_psm = chan->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200261 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300262 la->l2_cid = cpu_to_le16(chan->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200263 }
264
265 return 0;
266}
267
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200268static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
269{
270 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300271 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200272 struct l2cap_options opts;
273 struct l2cap_conninfo cinfo;
274 int len, err = 0;
275 u32 opt;
276
277 BT_DBG("sk %p", sk);
278
279 if (get_user(len, optlen))
280 return -EFAULT;
281
282 lock_sock(sk);
283
284 switch (optname) {
285 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300286 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300287 opts.imtu = chan->imtu;
288 opts.omtu = chan->omtu;
289 opts.flush_to = chan->flush_to;
290 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300291 opts.fcs = chan->fcs;
292 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300293 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200294
295 len = min_t(unsigned int, len, sizeof(opts));
296 if (copy_to_user(optval, (char *) &opts, len))
297 err = -EFAULT;
298
299 break;
300
301 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300302 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200303 case BT_SECURITY_LOW:
304 opt = L2CAP_LM_AUTH;
305 break;
306 case BT_SECURITY_MEDIUM:
307 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
308 break;
309 case BT_SECURITY_HIGH:
310 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
311 L2CAP_LM_SECURE;
312 break;
313 default:
314 opt = 0;
315 break;
316 }
317
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300318 if (test_bit(FLAG_ROLE_SWITCH, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200319 opt |= L2CAP_LM_MASTER;
320
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300321 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200322 opt |= L2CAP_LM_RELIABLE;
323
324 if (put_user(opt, (u32 __user *) optval))
325 err = -EFAULT;
326 break;
327
328 case L2CAP_CONNINFO:
329 if (sk->sk_state != BT_CONNECTED &&
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300330 !(sk->sk_state == BT_CONNECT2 &&
331 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200332 err = -ENOTCONN;
333 break;
334 }
335
Filip Palian8d03e972011-05-12 19:32:46 +0200336 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300337 cinfo.hci_handle = chan->conn->hcon->handle;
338 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200339
340 len = min_t(unsigned int, len, sizeof(cinfo));
341 if (copy_to_user(optval, (char *) &cinfo, len))
342 err = -EFAULT;
343
344 break;
345
346 default:
347 err = -ENOPROTOOPT;
348 break;
349 }
350
351 release_sock(sk);
352 return err;
353}
354
355static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
356{
357 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300358 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200359 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700360 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200361 int len, err = 0;
362
363 BT_DBG("sk %p", sk);
364
365 if (level == SOL_L2CAP)
366 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
367
368 if (level != SOL_BLUETOOTH)
369 return -ENOPROTOOPT;
370
371 if (get_user(len, optlen))
372 return -EFAULT;
373
374 lock_sock(sk);
375
376 switch (optname) {
377 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300378 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
379 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200380 err = -EINVAL;
381 break;
382 }
383
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300384 memset(&sec, 0, sizeof(sec));
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300385 if (chan->conn) {
Gustavo Padovanc6585a42012-05-07 03:07:26 -0300386 sec.level = chan->conn->hcon->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200387
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300388 if (sk->sk_state == BT_CONNECTED)
389 sec.key_size = chan->conn->hcon->enc_key_size;
390 } else {
391 sec.level = chan->sec_level;
392 }
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300393
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200394 len = min_t(unsigned int, len, sizeof(sec));
395 if (copy_to_user(optval, (char *) &sec, len))
396 err = -EFAULT;
397
398 break;
399
400 case BT_DEFER_SETUP:
401 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
402 err = -EINVAL;
403 break;
404 }
405
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300406 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
407 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200408 err = -EFAULT;
409
410 break;
411
412 case BT_FLUSHABLE:
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300413 if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags),
414 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200415 err = -EFAULT;
416
417 break;
418
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700419 case BT_POWER:
420 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
421 && sk->sk_type != SOCK_RAW) {
422 err = -EINVAL;
423 break;
424 }
425
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300426 pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700427
428 len = min_t(unsigned int, len, sizeof(pwr));
429 if (copy_to_user(optval, (char *) &pwr, len))
430 err = -EFAULT;
431
432 break;
433
Mat Martineau2ea66482011-11-02 16:18:30 -0700434 case BT_CHANNEL_POLICY:
435 if (!enable_hs) {
436 err = -ENOPROTOOPT;
437 break;
438 }
439
440 if (put_user(chan->chan_policy, (u32 __user *) optval))
441 err = -EFAULT;
442 break;
443
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200444 default:
445 err = -ENOPROTOOPT;
446 break;
447 }
448
449 release_sock(sk);
450 return err;
451}
452
Andre Guedes682877c2012-05-31 17:01:34 -0300453static bool l2cap_valid_mtu(struct l2cap_chan *chan, u16 mtu)
454{
455 switch (chan->scid) {
456 case L2CAP_CID_LE_DATA:
Andre Guedes8c3a4f02012-05-31 17:01:35 -0300457 if (mtu < L2CAP_LE_MIN_MTU)
Andre Guedes682877c2012-05-31 17:01:34 -0300458 return false;
459 break;
460
461 default:
462 if (mtu < L2CAP_DEFAULT_MIN_MTU)
463 return false;
464 }
465
466 return true;
467}
468
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200469static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
470{
471 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300472 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200473 struct l2cap_options opts;
474 int len, err = 0;
475 u32 opt;
476
477 BT_DBG("sk %p", sk);
478
479 lock_sock(sk);
480
481 switch (optname) {
482 case L2CAP_OPTIONS:
483 if (sk->sk_state == BT_CONNECTED) {
484 err = -EINVAL;
485 break;
486 }
487
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300488 opts.imtu = chan->imtu;
489 opts.omtu = chan->omtu;
490 opts.flush_to = chan->flush_to;
491 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300492 opts.fcs = chan->fcs;
493 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300494 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200495
496 len = min_t(unsigned int, sizeof(opts), optlen);
497 if (copy_from_user((char *) &opts, optval, len)) {
498 err = -EFAULT;
499 break;
500 }
501
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300502 if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200503 err = -EINVAL;
504 break;
505 }
506
Andre Guedes682877c2012-05-31 17:01:34 -0300507 if (!l2cap_valid_mtu(chan, opts.imtu)) {
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;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300531 chan->tx_win = 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
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300547 if (opt & L2CAP_LM_MASTER)
548 set_bit(FLAG_ROLE_SWITCH, &chan->flags);
549 else
550 clear_bit(FLAG_ROLE_SWITCH, &chan->flags);
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300551
552 if (opt & L2CAP_LM_RELIABLE)
553 set_bit(FLAG_FORCE_RELIABLE, &chan->flags);
554 else
555 clear_bit(FLAG_FORCE_RELIABLE, &chan->flags);
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
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200611 if (!chan->conn)
612 break;
613
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300614 conn = chan->conn;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200615
616 /*change security for LE channels */
617 if (chan->scid == L2CAP_CID_LE_DATA) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300618 if (!conn->hcon->out) {
619 err = -EINVAL;
620 break;
621 }
622
Vinicius Costa Gomescc110922012-08-23 21:32:43 -0300623 if (smp_conn_security(conn->hcon, sec.level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300624 break;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300625 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200626 chan->state = BT_CONFIG;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200627
Gustavo Padovana7d77232012-05-13 03:20:07 -0300628 /* or for ACL link */
629 } else if ((sk->sk_state == BT_CONNECT2 &&
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300630 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) ||
Gustavo Padovana7d77232012-05-13 03:20:07 -0300631 sk->sk_state == BT_CONNECTED) {
632 if (!l2cap_chan_check_security(chan))
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300633 set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
Gustavo Padovana7d77232012-05-13 03:20:07 -0300634 else
635 sk->sk_state_change(sk);
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200636 } else {
637 err = -EINVAL;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300638 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200639 break;
640
641 case BT_DEFER_SETUP:
642 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
643 err = -EINVAL;
644 break;
645 }
646
647 if (get_user(opt, (u32 __user *) optval)) {
648 err = -EFAULT;
649 break;
650 }
651
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300652 if (opt)
653 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
654 else
655 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200656 break;
657
658 case BT_FLUSHABLE:
659 if (get_user(opt, (u32 __user *) optval)) {
660 err = -EFAULT;
661 break;
662 }
663
664 if (opt > BT_FLUSHABLE_ON) {
665 err = -EINVAL;
666 break;
667 }
668
669 if (opt == BT_FLUSHABLE_OFF) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300670 struct l2cap_conn *conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300671 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200672 No Flush support in the LM */
673 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
674 err = -EINVAL;
675 break;
676 }
677 }
678
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300679 if (opt)
680 set_bit(FLAG_FLUSHABLE, &chan->flags);
681 else
682 clear_bit(FLAG_FLUSHABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200683 break;
684
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700685 case BT_POWER:
686 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
687 chan->chan_type != L2CAP_CHAN_RAW) {
688 err = -EINVAL;
689 break;
690 }
691
692 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
693
694 len = min_t(unsigned int, sizeof(pwr), optlen);
695 if (copy_from_user((char *) &pwr, optval, len)) {
696 err = -EFAULT;
697 break;
698 }
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300699
700 if (pwr.force_active)
701 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
702 else
703 clear_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700704 break;
705
Mat Martineau2ea66482011-11-02 16:18:30 -0700706 case BT_CHANNEL_POLICY:
707 if (!enable_hs) {
708 err = -ENOPROTOOPT;
709 break;
710 }
711
712 if (get_user(opt, (u32 __user *) optval)) {
713 err = -EFAULT;
714 break;
715 }
716
717 if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) {
718 err = -EINVAL;
719 break;
720 }
721
722 if (chan->mode != L2CAP_MODE_ERTM &&
723 chan->mode != L2CAP_MODE_STREAMING) {
724 err = -EOPNOTSUPP;
725 break;
726 }
727
728 chan->chan_policy = (u8) opt;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200729 break;
730
731 default:
732 err = -ENOPROTOOPT;
733 break;
734 }
735
736 release_sock(sk);
737 return err;
738}
739
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200740static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
741{
742 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300743 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200744 int err;
745
746 BT_DBG("sock %p, sk %p", sock, sk);
747
748 err = sock_error(sk);
749 if (err)
750 return err;
751
752 if (msg->msg_flags & MSG_OOB)
753 return -EOPNOTSUPP;
754
Mat Martineaua6a55682012-05-04 14:20:31 -0700755 if (sk->sk_state != BT_CONNECTED)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300756 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200757
Mat Martineaua6a55682012-05-04 14:20:31 -0700758 l2cap_chan_lock(chan);
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200759 err = l2cap_chan_send(chan, msg, len, sk->sk_priority);
Mat Martineaua6a55682012-05-04 14:20:31 -0700760 l2cap_chan_unlock(chan);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200761
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200762 return err;
763}
764
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200765static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
766{
767 struct sock *sk = sock->sk;
Mat Martineaue3281402011-07-07 09:39:02 -0700768 struct l2cap_pinfo *pi = l2cap_pi(sk);
769 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200770
771 lock_sock(sk);
772
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300773 if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
774 &bt_sk(sk)->flags)) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300775 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200776 pi->chan->state = BT_CONFIG;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300777
Mat Martineaue3281402011-07-07 09:39:02 -0700778 __l2cap_connect_rsp_defer(pi->chan);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200779 release_sock(sk);
780 return 0;
781 }
782
783 release_sock(sk);
784
785 if (sock->type == SOCK_STREAM)
Mat Martineaue3281402011-07-07 09:39:02 -0700786 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
787 else
788 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200789
Mat Martineaue3281402011-07-07 09:39:02 -0700790 if (pi->chan->mode != L2CAP_MODE_ERTM)
791 return err;
792
793 /* Attempt to put pending rx data in the socket buffer */
794
795 lock_sock(sk);
796
797 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
798 goto done;
799
800 if (pi->rx_busy_skb) {
801 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
802 pi->rx_busy_skb = NULL;
803 else
804 goto done;
805 }
806
807 /* Restore data flow when half of the receive buffer is
808 * available. This avoids resending large numbers of
809 * frames.
810 */
811 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
812 l2cap_chan_busy(pi->chan, 0);
813
814done:
815 release_sock(sk);
816 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200817}
818
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200819/* Kill socket (only if zapped and orphan)
820 * Must be called on unlocked socket.
821 */
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300822static void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200823{
824 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
825 return;
826
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200827 BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200828
829 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300830
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530831 l2cap_chan_put(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200832 sock_set_flag(sk, SOCK_DEAD);
833 sock_put(sk);
834}
835
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200836static int l2cap_sock_shutdown(struct socket *sock, int how)
837{
838 struct sock *sk = sock->sk;
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200839 struct l2cap_chan *chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200840 struct l2cap_conn *conn;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200841 int err = 0;
842
843 BT_DBG("sock %p, sk %p", sock, sk);
844
845 if (!sk)
846 return 0;
847
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200848 chan = l2cap_pi(sk)->chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200849 conn = chan->conn;
850
851 if (conn)
852 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200853
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200854 l2cap_chan_lock(chan);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200855 lock_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200856
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200857 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300858 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200859 err = __l2cap_wait_ack(sk);
860
861 sk->sk_shutdown = SHUTDOWN_MASK;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200862
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200863 release_sock(sk);
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300864 l2cap_chan_close(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200865 lock_sock(sk);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200866
867 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
868 err = bt_sock_wait_state(sk, BT_CLOSED,
869 sk->sk_lingertime);
870 }
871
872 if (!err && sk->sk_err)
873 err = -sk->sk_err;
874
875 release_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200876 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200877
878 if (conn)
879 mutex_unlock(&conn->chan_lock);
880
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200881 return err;
882}
883
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200884static int l2cap_sock_release(struct socket *sock)
885{
886 struct sock *sk = sock->sk;
887 int err;
888
889 BT_DBG("sock %p, sk %p", sock, sk);
890
891 if (!sk)
892 return 0;
893
Masatake YAMATO5b28d952012-07-26 01:29:25 +0900894 bt_sock_unlink(&l2cap_sk_list, sk);
895
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200896 err = l2cap_sock_shutdown(sock, 2);
897
898 sock_orphan(sk);
899 l2cap_sock_kill(sk);
900 return err;
901}
902
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -0300903static void l2cap_sock_cleanup_listen(struct sock *parent)
904{
905 struct sock *sk;
906
907 BT_DBG("parent %p", parent);
908
909 /* Close not yet accepted channels */
910 while ((sk = bt_accept_dequeue(parent, NULL))) {
911 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
912
913 l2cap_chan_lock(chan);
914 __clear_chan_timer(chan);
915 l2cap_chan_close(chan, ECONNRESET);
916 l2cap_chan_unlock(chan);
917
918 l2cap_sock_kill(sk);
919 }
920}
921
Gustavo Padovan80b98022012-05-27 22:27:51 -0300922static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300923{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300924 struct sock *sk, *parent = chan->data;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300925
Gustavo Padovan53826692012-05-27 22:27:55 -0300926 /* Check for backlog size */
927 if (sk_acceptq_is_full(parent)) {
928 BT_DBG("backlog full %d", parent->sk_ack_backlog);
929 return NULL;
930 }
931
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300932 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
933 GFP_ATOMIC);
934 if (!sk)
935 return NULL;
936
Octavian Purdilad22015a2012-01-22 00:28:34 +0200937 bt_sock_reclassify_lock(sk, BTPROTO_L2CAP);
938
Gustavo F. Padovan80808e42011-05-16 17:24:37 -0300939 l2cap_sock_init(sk, parent);
940
941 return l2cap_pi(sk)->chan;
942}
943
Gustavo Padovan80b98022012-05-27 22:27:51 -0300944static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300945{
Mat Martineaue3281402011-07-07 09:39:02 -0700946 int err;
Gustavo Padovan80b98022012-05-27 22:27:51 -0300947 struct sock *sk = chan->data;
Mat Martineaue3281402011-07-07 09:39:02 -0700948 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300949
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200950 lock_sock(sk);
951
952 if (pi->rx_busy_skb) {
953 err = -ENOMEM;
954 goto done;
955 }
Mat Martineaue3281402011-07-07 09:39:02 -0700956
957 err = sock_queue_rcv_skb(sk, skb);
958
959 /* For ERTM, handle one skb that doesn't fit into the recv
960 * buffer. This is important to do because the data frames
961 * have already been acked, so the skb cannot be discarded.
962 *
963 * Notify the l2cap core that the buffer is full, so the
964 * LOCAL_BUSY state is entered and no more frames are
965 * acked and reassembled until there is buffer space
966 * available.
967 */
968 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
969 pi->rx_busy_skb = skb;
970 l2cap_chan_busy(pi->chan, 1);
971 err = 0;
972 }
973
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200974done:
975 release_sock(sk);
976
Mat Martineaue3281402011-07-07 09:39:02 -0700977 return err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -0300978}
979
Gustavo Padovan80b98022012-05-27 22:27:51 -0300980static void l2cap_sock_close_cb(struct l2cap_chan *chan)
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300981{
Gustavo Padovan80b98022012-05-27 22:27:51 -0300982 struct sock *sk = chan->data;
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300983
984 l2cap_sock_kill(sk);
985}
986
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -0300987static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
988{
989 struct sock *sk = chan->data;
990 struct sock *parent;
991
992 lock_sock(sk);
993
994 parent = bt_sk(sk)->parent;
995
996 sock_set_flag(sk, SOCK_ZAPPED);
997
998 switch (chan->state) {
999 case BT_OPEN:
1000 case BT_BOUND:
1001 case BT_CLOSED:
1002 break;
1003 case BT_LISTEN:
1004 l2cap_sock_cleanup_listen(sk);
1005 sk->sk_state = BT_CLOSED;
1006 chan->state = BT_CLOSED;
1007
1008 break;
1009 default:
1010 sk->sk_state = BT_CLOSED;
1011 chan->state = BT_CLOSED;
1012
1013 sk->sk_err = err;
1014
1015 if (parent) {
1016 bt_accept_unlink(sk);
1017 parent->sk_data_ready(parent, 0);
1018 } else {
1019 sk->sk_state_change(sk);
1020 }
1021
1022 break;
1023 }
1024
1025 release_sock(sk);
1026}
1027
Gustavo Padovan80b98022012-05-27 22:27:51 -03001028static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001029{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001030 struct sock *sk = chan->data;
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001031
1032 sk->sk_state = state;
1033}
1034
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001035static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
Gustavo Padovan90338942012-04-06 20:15:47 -03001036 unsigned long len, int nb)
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001037{
Gustavo Padovan90338942012-04-06 20:15:47 -03001038 struct sk_buff *skb;
1039 int err;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001040
Mat Martineaua6a55682012-05-04 14:20:31 -07001041 l2cap_chan_unlock(chan);
Gustavo Padovan90338942012-04-06 20:15:47 -03001042 skb = bt_skb_send_alloc(chan->sk, len, nb, &err);
Mat Martineaua6a55682012-05-04 14:20:31 -07001043 l2cap_chan_lock(chan);
1044
Gustavo Padovan90338942012-04-06 20:15:47 -03001045 if (!skb)
1046 return ERR_PTR(err);
1047
1048 return skb;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001049}
1050
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001051static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
1052{
1053 struct sock *sk = chan->data;
1054 struct sock *parent;
1055
1056 lock_sock(sk);
1057
1058 parent = bt_sk(sk)->parent;
1059
1060 BT_DBG("sk %p, parent %p", sk, parent);
1061
1062 sk->sk_state = BT_CONNECTED;
1063 sk->sk_state_change(sk);
1064
1065 if (parent)
1066 parent->sk_data_ready(parent, 0);
1067
1068 release_sock(sk);
1069}
1070
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001071static struct l2cap_ops l2cap_chan_ops = {
1072 .name = "L2CAP Socket Interface",
1073 .new_connection = l2cap_sock_new_connection_cb,
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001074 .recv = l2cap_sock_recv_cb,
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001075 .close = l2cap_sock_close_cb,
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001076 .teardown = l2cap_sock_teardown_cb,
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001077 .state_change = l2cap_sock_state_change_cb,
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001078 .ready = l2cap_sock_ready_cb,
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001079 .alloc_skb = l2cap_sock_alloc_skb_cb,
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001080};
1081
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001082static void l2cap_sock_destruct(struct sock *sk)
1083{
1084 BT_DBG("sk %p", sk);
1085
Sasha Levin23d3a862012-10-08 16:48:32 -04001086 if (l2cap_pi(sk)->chan)
1087 l2cap_chan_put(l2cap_pi(sk)->chan);
Mat Martineaue3281402011-07-07 09:39:02 -07001088 if (l2cap_pi(sk)->rx_busy_skb) {
1089 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
1090 l2cap_pi(sk)->rx_busy_skb = NULL;
1091 }
1092
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001093 skb_queue_purge(&sk->sk_receive_queue);
1094 skb_queue_purge(&sk->sk_write_queue);
1095}
1096
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001097static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001098{
1099 struct l2cap_pinfo *pi = l2cap_pi(sk);
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001100 struct l2cap_chan *chan = pi->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001101
1102 BT_DBG("sk %p", sk);
1103
1104 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001105 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
1106
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001107 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -03001108 bt_sk(sk)->flags = bt_sk(parent)->flags;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001109
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001110 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001111 chan->imtu = pchan->imtu;
1112 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001113 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001114 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001115 chan->fcs = pchan->fcs;
1116 chan->max_tx = pchan->max_tx;
1117 chan->tx_win = pchan->tx_win;
Andrei Emeltchenko6b3c7102011-11-02 09:57:10 +02001118 chan->tx_win_max = pchan->tx_win_max;
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001119 chan->sec_level = pchan->sec_level;
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +03001120 chan->flags = pchan->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +00001121
1122 security_sk_clone(parent, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001123 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001124
1125 switch (sk->sk_type) {
1126 case SOCK_RAW:
1127 chan->chan_type = L2CAP_CHAN_RAW;
1128 break;
1129 case SOCK_DGRAM:
1130 chan->chan_type = L2CAP_CHAN_CONN_LESS;
1131 break;
1132 case SOCK_SEQPACKET:
1133 case SOCK_STREAM:
1134 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
1135 break;
1136 }
1137
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001138 chan->imtu = L2CAP_DEFAULT_MTU;
1139 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001140 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001141 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001142 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001143 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001144 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001145 }
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +03001146
1147 l2cap_chan_set_defaults(chan);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001148 }
1149
1150 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001151 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001152
1153 chan->data = sk;
1154 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001155}
1156
1157static struct proto l2cap_proto = {
1158 .name = "L2CAP",
1159 .owner = THIS_MODULE,
1160 .obj_size = sizeof(struct l2cap_pinfo)
1161};
1162
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001163static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001164{
1165 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001166 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001167
1168 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1169 if (!sk)
1170 return NULL;
1171
1172 sock_init_data(sock, sk);
1173 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1174
1175 sk->sk_destruct = l2cap_sock_destruct;
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08001176 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001177
1178 sock_reset_flag(sk, SOCK_ZAPPED);
1179
1180 sk->sk_protocol = proto;
1181 sk->sk_state = BT_OPEN;
1182
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001183 chan = l2cap_chan_create();
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001184 if (!chan) {
Jaganath Kanakkassery49dfbb92012-07-19 12:54:04 +05301185 sk_free(sk);
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001186 return NULL;
1187 }
1188
Mat Martineau61d6ef32012-04-27 16:50:50 -07001189 l2cap_chan_hold(chan);
1190
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001191 chan->sk = sk;
1192
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001193 l2cap_pi(sk)->chan = chan;
1194
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001195 return sk;
1196}
1197
1198static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1199 int kern)
1200{
1201 struct sock *sk;
1202
1203 BT_DBG("sock %p", sock);
1204
1205 sock->state = SS_UNCONNECTED;
1206
1207 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1208 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1209 return -ESOCKTNOSUPPORT;
1210
1211 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1212 return -EPERM;
1213
1214 sock->ops = &l2cap_sock_ops;
1215
1216 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1217 if (!sk)
1218 return -ENOMEM;
1219
1220 l2cap_sock_init(sk, NULL);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001221 bt_sock_link(&l2cap_sk_list, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001222 return 0;
1223}
1224
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -03001225static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001226 .family = PF_BLUETOOTH,
1227 .owner = THIS_MODULE,
1228 .release = l2cap_sock_release,
1229 .bind = l2cap_sock_bind,
1230 .connect = l2cap_sock_connect,
1231 .listen = l2cap_sock_listen,
1232 .accept = l2cap_sock_accept,
1233 .getname = l2cap_sock_getname,
1234 .sendmsg = l2cap_sock_sendmsg,
1235 .recvmsg = l2cap_sock_recvmsg,
1236 .poll = bt_sock_poll,
1237 .ioctl = bt_sock_ioctl,
1238 .mmap = sock_no_mmap,
1239 .socketpair = sock_no_socketpair,
1240 .shutdown = l2cap_sock_shutdown,
1241 .setsockopt = l2cap_sock_setsockopt,
1242 .getsockopt = l2cap_sock_getsockopt
1243};
1244
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001245static const struct net_proto_family l2cap_sock_family_ops = {
1246 .family = PF_BLUETOOTH,
1247 .owner = THIS_MODULE,
1248 .create = l2cap_sock_create,
1249};
1250
1251int __init l2cap_init_sockets(void)
1252{
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001253 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001254
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001255 err = proto_register(&l2cap_proto, 0);
1256 if (err < 0)
1257 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001258
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001259 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001260 if (err < 0) {
1261 BT_ERR("L2CAP socket registration failed");
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001262 goto error;
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001263 }
1264
1265 err = bt_procfs_init(THIS_MODULE, &init_net, "l2cap", &l2cap_sk_list, NULL);
1266 if (err < 0) {
1267 BT_ERR("Failed to create L2CAP proc file");
1268 bt_sock_unregister(BTPROTO_L2CAP);
1269 goto error;
1270 }
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001271
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001272 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001273
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001274 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001275
1276error:
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001277 proto_unregister(&l2cap_proto);
1278 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001279}
1280
1281void l2cap_cleanup_sockets(void)
1282{
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001283 bt_procfs_cleanup(&init_net, "l2cap");
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001284 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1285 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001286
Gustavo F. Padovane2174ca42011-02-17 19:16:55 -03001287 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001288}