blob: 95498d553b0098a58994cf0dc63c8e6f30c2b941 [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>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070035
36#include "smp.h"
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020037
Masatake YAMATO5b28d952012-07-26 01:29:25 +090038static struct bt_sock_list l2cap_sk_list = {
39 .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock)
40};
41
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030042static const struct proto_ops l2cap_sock_ops;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -030043static void l2cap_sock_init(struct sock *sk, struct sock *parent);
Gustavo Padovan2d792812012-10-06 10:07:01 +010044static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
45 int proto, gfp_t prio);
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030046
David Herrmannb3916db2013-04-05 14:57:34 +020047bool l2cap_is_socket(struct socket *sock)
48{
49 return sock && sock->ops == &l2cap_sock_ops;
50}
51EXPORT_SYMBOL(l2cap_is_socket);
52
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020053static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
54{
55 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -030056 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020057 struct sockaddr_l2 la;
58 int len, err = 0;
59
60 BT_DBG("sk %p", sk);
61
62 if (!addr || addr->sa_family != AF_BLUETOOTH)
63 return -EINVAL;
64
65 memset(&la, 0, sizeof(la));
66 len = min_t(unsigned int, sizeof(la), alen);
67 memcpy(&la, addr, len);
68
Ville Tervob62f3282011-02-10 22:38:50 -030069 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020070 return -EINVAL;
71
Johan Hedberg80c1a2e2013-10-14 21:17:52 +030072 if (!bdaddr_type_is_valid(la.l2_bdaddr_type))
73 return -EINVAL;
74
Johan Hedbergbfe46552013-10-16 17:13:26 +030075 if (bdaddr_type_is_le(la.l2_bdaddr_type)) {
76 /* Connection oriented channels are not supported on LE */
77 if (la.l2_psm)
78 return -EINVAL;
79 /* We only allow ATT user space socket */
80 if (la.l2_cid != L2CAP_CID_ATT)
81 return -EINVAL;
82 }
83
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020084 lock_sock(sk);
85
86 if (sk->sk_state != BT_OPEN) {
87 err = -EBADFD;
88 goto done;
89 }
90
91 if (la.l2_psm) {
92 __u16 psm = __le16_to_cpu(la.l2_psm);
93
94 /* PSM must be odd and lsb of upper byte must be 0 */
95 if ((psm & 0x0101) != 0x0001) {
96 err = -EINVAL;
97 goto done;
98 }
99
100 /* Restrict usage of well-known PSMs */
101 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
102 err = -EACCES;
103 goto done;
104 }
105 }
106
Ville Tervob62f3282011-02-10 22:38:50 -0300107 if (la.l2_cid)
Santosh Nayak6e4aff12012-03-01 22:46:36 +0530108 err = l2cap_add_scid(chan, __le16_to_cpu(la.l2_cid));
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300109 else
110 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
Ville Tervob62f3282011-02-10 22:38:50 -0300111
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300112 if (err < 0)
113 goto done;
114
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700115 switch (chan->chan_type) {
Marcel Holtmann3124b842013-10-12 07:19:32 -0700116 case L2CAP_CHAN_CONN_LESS:
117 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_3DSP)
118 chan->sec_level = BT_SECURITY_SDP;
119 break;
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700120 case L2CAP_CHAN_CONN_ORIENTED:
121 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP ||
122 __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM)
123 chan->sec_level = BT_SECURITY_SDP;
124 break;
125 }
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300126
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700127 bacpy(&chan->src, &la.l2_bdaddr);
Marcel Holtmann4f1654e2013-10-13 08:50:41 -0700128 chan->src_type = la.l2_bdaddr_type;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300129
130 chan->state = BT_BOUND;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300131 sk->sk_state = BT_BOUND;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200132
133done:
134 release_sock(sk);
135 return err;
136}
137
Gustavo Padovan2d792812012-10-06 10:07:01 +0100138static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr,
139 int alen, int flags)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200140{
141 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300142 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200143 struct sockaddr_l2 la;
144 int len, err = 0;
145
146 BT_DBG("sk %p", sk);
147
148 if (!addr || alen < sizeof(addr->sa_family) ||
149 addr->sa_family != AF_BLUETOOTH)
150 return -EINVAL;
151
152 memset(&la, 0, sizeof(la));
153 len = min_t(unsigned int, sizeof(la), alen);
154 memcpy(&la, addr, len);
155
Ville Tervoacd7d372011-02-10 22:38:49 -0300156 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200157 return -EINVAL;
158
Johan Hedberg80c1a2e2013-10-14 21:17:52 +0300159 if (!bdaddr_type_is_valid(la.l2_bdaddr_type))
160 return -EINVAL;
161
Johan Hedberg1f209382013-10-14 21:17:53 +0300162 if (chan->src_type == BDADDR_BREDR && la.l2_bdaddr_type != BDADDR_BREDR)
163 return -EINVAL;
164
165 if (chan->src_type != BDADDR_BREDR && la.l2_bdaddr_type == BDADDR_BREDR)
166 return -EINVAL;
167
Johan Hedbergbfe46552013-10-16 17:13:26 +0300168 if (bdaddr_type_is_le(la.l2_bdaddr_type)) {
169 /* Connection oriented channels are not supported on LE */
170 if (la.l2_psm)
171 return -EINVAL;
172 /* We only allow ATT user space socket */
173 if (la.l2_cid != L2CAP_CID_ATT)
174 return -EINVAL;
175 }
176
Santosh Nayak6e4aff12012-03-01 22:46:36 +0530177 err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid),
Andre Guedes8e9f9892012-04-24 21:02:55 -0300178 &la.l2_bdaddr, la.l2_bdaddr_type);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200179 if (err)
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200180 return err;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200181
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200182 lock_sock(sk);
183
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200184 err = bt_sock_wait_state(sk, BT_CONNECTED,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100185 sock_sndtimeo(sk, flags & O_NONBLOCK));
Andrei Emeltchenkob3fb6112012-02-22 17:11:57 +0200186
187 release_sock(sk);
188
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200189 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
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200202 if (sk->sk_state != BT_BOUND) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200203 err = -EBADFD;
204 goto done;
205 }
206
Marcel Holtmann6b3af732012-04-19 13:43:51 +0200207 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) {
208 err = -EINVAL;
209 goto done;
210 }
211
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300212 switch (chan->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200213 case L2CAP_MODE_BASIC:
214 break;
215 case L2CAP_MODE_ERTM:
216 case L2CAP_MODE_STREAMING:
217 if (!disable_ertm)
218 break;
219 /* fall through */
220 default:
221 err = -ENOTSUPP;
222 goto done;
223 }
224
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200225 sk->sk_max_ack_backlog = backlog;
226 sk->sk_ack_backlog = 0;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300227
228 chan->state = BT_LISTEN;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200229 sk->sk_state = BT_LISTEN;
230
231done:
232 release_sock(sk);
233 return err;
234}
235
Gustavo Padovan2d792812012-10-06 10:07:01 +0100236static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
237 int flags)
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200238{
239 DECLARE_WAITQUEUE(wait, current);
240 struct sock *sk = sock->sk, *nsk;
241 long timeo;
242 int err = 0;
243
244 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
245
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200246 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
247
248 BT_DBG("sk %p timeo %ld", sk, timeo);
249
250 /* Wait for an incoming connection. (wake-one). */
251 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400252 while (1) {
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200253 set_current_state(TASK_INTERRUPTIBLE);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200254
255 if (sk->sk_state != BT_LISTEN) {
256 err = -EBADFD;
257 break;
258 }
259
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400260 nsk = bt_accept_dequeue(sk, newsock);
261 if (nsk)
262 break;
263
264 if (!timeo) {
265 err = -EAGAIN;
266 break;
267 }
268
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200269 if (signal_pending(current)) {
270 err = sock_intr_errno(timeo);
271 break;
272 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400273
274 release_sock(sk);
275 timeo = schedule_timeout(timeo);
276 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200277 }
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400278 __set_current_state(TASK_RUNNING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200279 remove_wait_queue(sk_sleep(sk), &wait);
280
281 if (err)
282 goto done;
283
284 newsock->state = SS_CONNECTED;
285
286 BT_DBG("new socket %p", nsk);
287
288done:
289 release_sock(sk);
290 return err;
291}
292
Gustavo Padovan2d792812012-10-06 10:07:01 +0100293static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr,
294 int *len, int peer)
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200295{
296 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
297 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300298 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200299
300 BT_DBG("sock %p, sk %p", sock, sk);
301
Mathias Krause792039c2012-08-15 11:31:51 +0000302 memset(la, 0, sizeof(struct sockaddr_l2));
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200303 addr->sa_family = AF_BLUETOOTH;
304 *len = sizeof(struct sockaddr_l2);
305
306 if (peer) {
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300307 la->l2_psm = chan->psm;
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700308 bacpy(&la->l2_bdaddr, &chan->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300309 la->l2_cid = cpu_to_le16(chan->dcid);
Marcel Holtmann4f1654e2013-10-13 08:50:41 -0700310 la->l2_bdaddr_type = chan->dst_type;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200311 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300312 la->l2_psm = chan->sport;
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700313 bacpy(&la->l2_bdaddr, &chan->src);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300314 la->l2_cid = cpu_to_le16(chan->scid);
Marcel Holtmann4f1654e2013-10-13 08:50:41 -0700315 la->l2_bdaddr_type = chan->src_type;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200316 }
317
318 return 0;
319}
320
Gustavo Padovan2d792812012-10-06 10:07:01 +0100321static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
322 char __user *optval, int __user *optlen)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200323{
324 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300325 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200326 struct l2cap_options opts;
327 struct l2cap_conninfo cinfo;
328 int len, err = 0;
329 u32 opt;
330
331 BT_DBG("sk %p", sk);
332
333 if (get_user(len, optlen))
334 return -EFAULT;
335
336 lock_sock(sk);
337
338 switch (optname) {
339 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300340 memset(&opts, 0, sizeof(opts));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300341 opts.imtu = chan->imtu;
342 opts.omtu = chan->omtu;
343 opts.flush_to = chan->flush_to;
344 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300345 opts.fcs = chan->fcs;
346 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300347 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200348
349 len = min_t(unsigned int, len, sizeof(opts));
350 if (copy_to_user(optval, (char *) &opts, len))
351 err = -EFAULT;
352
353 break;
354
355 case L2CAP_LM:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300356 switch (chan->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200357 case BT_SECURITY_LOW:
358 opt = L2CAP_LM_AUTH;
359 break;
360 case BT_SECURITY_MEDIUM:
361 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
362 break;
363 case BT_SECURITY_HIGH:
364 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
Gustavo Padovan2d792812012-10-06 10:07:01 +0100365 L2CAP_LM_SECURE;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200366 break;
367 default:
368 opt = 0;
369 break;
370 }
371
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300372 if (test_bit(FLAG_ROLE_SWITCH, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200373 opt |= L2CAP_LM_MASTER;
374
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300375 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200376 opt |= L2CAP_LM_RELIABLE;
377
378 if (put_user(opt, (u32 __user *) optval))
379 err = -EFAULT;
380 break;
381
382 case L2CAP_CONNINFO:
383 if (sk->sk_state != BT_CONNECTED &&
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300384 !(sk->sk_state == BT_CONNECT2 &&
385 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200386 err = -ENOTCONN;
387 break;
388 }
389
Filip Palian8d03e972011-05-12 19:32:46 +0200390 memset(&cinfo, 0, sizeof(cinfo));
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300391 cinfo.hci_handle = chan->conn->hcon->handle;
392 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200393
394 len = min_t(unsigned int, len, sizeof(cinfo));
395 if (copy_to_user(optval, (char *) &cinfo, len))
396 err = -EFAULT;
397
398 break;
399
400 default:
401 err = -ENOPROTOOPT;
402 break;
403 }
404
405 release_sock(sk);
406 return err;
407}
408
Gustavo Padovan2d792812012-10-06 10:07:01 +0100409static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
410 char __user *optval, int __user *optlen)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200411{
412 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300413 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200414 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700415 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200416 int len, err = 0;
417
418 BT_DBG("sk %p", sk);
419
420 if (level == SOL_L2CAP)
421 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
422
423 if (level != SOL_BLUETOOTH)
424 return -ENOPROTOOPT;
425
426 if (get_user(len, optlen))
427 return -EFAULT;
428
429 lock_sock(sk);
430
431 switch (optname) {
432 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300433 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100434 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200435 err = -EINVAL;
436 break;
437 }
438
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300439 memset(&sec, 0, sizeof(sec));
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300440 if (chan->conn) {
Gustavo Padovanc6585a42012-05-07 03:07:26 -0300441 sec.level = chan->conn->hcon->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200442
Andrei Emeltchenko85e34362012-10-05 16:56:54 +0300443 if (sk->sk_state == BT_CONNECTED)
444 sec.key_size = chan->conn->hcon->enc_key_size;
445 } else {
446 sec.level = chan->sec_level;
447 }
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300448
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200449 len = min_t(unsigned int, len, sizeof(sec));
450 if (copy_to_user(optval, (char *) &sec, len))
451 err = -EFAULT;
452
453 break;
454
455 case BT_DEFER_SETUP:
456 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
457 err = -EINVAL;
458 break;
459 }
460
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300461 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
462 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200463 err = -EFAULT;
464
465 break;
466
467 case BT_FLUSHABLE:
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300468 if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags),
Gustavo Padovan2d792812012-10-06 10:07:01 +0100469 (u32 __user *) optval))
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200470 err = -EFAULT;
471
472 break;
473
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700474 case BT_POWER:
475 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
Gustavo Padovan2d792812012-10-06 10:07:01 +0100476 && sk->sk_type != SOCK_RAW) {
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700477 err = -EINVAL;
478 break;
479 }
480
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300481 pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700482
483 len = min_t(unsigned int, len, sizeof(pwr));
484 if (copy_to_user(optval, (char *) &pwr, len))
485 err = -EFAULT;
486
487 break;
488
Mat Martineau2ea66482011-11-02 16:18:30 -0700489 case BT_CHANNEL_POLICY:
Mat Martineau2ea66482011-11-02 16:18:30 -0700490 if (put_user(chan->chan_policy, (u32 __user *) optval))
491 err = -EFAULT;
492 break;
493
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200494 default:
495 err = -ENOPROTOOPT;
496 break;
497 }
498
499 release_sock(sk);
500 return err;
501}
502
Andre Guedes682877c2012-05-31 17:01:34 -0300503static bool l2cap_valid_mtu(struct l2cap_chan *chan, u16 mtu)
504{
505 switch (chan->scid) {
Johan Hedberg073d1cf2013-04-29 19:35:35 +0300506 case L2CAP_CID_ATT:
Andre Guedes8c3a4f02012-05-31 17:01:35 -0300507 if (mtu < L2CAP_LE_MIN_MTU)
Andre Guedes682877c2012-05-31 17:01:34 -0300508 return false;
509 break;
510
511 default:
512 if (mtu < L2CAP_DEFAULT_MIN_MTU)
513 return false;
514 }
515
516 return true;
517}
518
Gustavo Padovan2d792812012-10-06 10:07:01 +0100519static int l2cap_sock_setsockopt_old(struct socket *sock, int optname,
520 char __user *optval, unsigned int optlen)
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200521{
522 struct sock *sk = sock->sk;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -0300523 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200524 struct l2cap_options opts;
525 int len, err = 0;
526 u32 opt;
527
528 BT_DBG("sk %p", sk);
529
530 lock_sock(sk);
531
532 switch (optname) {
533 case L2CAP_OPTIONS:
534 if (sk->sk_state == BT_CONNECTED) {
535 err = -EINVAL;
536 break;
537 }
538
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300539 opts.imtu = chan->imtu;
540 opts.omtu = chan->omtu;
541 opts.flush_to = chan->flush_to;
542 opts.mode = chan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300543 opts.fcs = chan->fcs;
544 opts.max_tx = chan->max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300545 opts.txwin_size = chan->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200546
547 len = min_t(unsigned int, sizeof(opts), optlen);
548 if (copy_from_user((char *) &opts, optval, len)) {
549 err = -EFAULT;
550 break;
551 }
552
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300553 if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200554 err = -EINVAL;
555 break;
556 }
557
Andre Guedes682877c2012-05-31 17:01:34 -0300558 if (!l2cap_valid_mtu(chan, opts.imtu)) {
559 err = -EINVAL;
560 break;
561 }
562
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300563 chan->mode = opts.mode;
564 switch (chan->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200565 case L2CAP_MODE_BASIC:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -0300566 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200567 break;
568 case L2CAP_MODE_ERTM:
569 case L2CAP_MODE_STREAMING:
570 if (!disable_ertm)
571 break;
572 /* fall through */
573 default:
574 err = -EINVAL;
575 break;
576 }
577
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300578 chan->imtu = opts.imtu;
579 chan->omtu = opts.omtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300580 chan->fcs = opts.fcs;
581 chan->max_tx = opts.max_tx;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +0300582 chan->tx_win = opts.txwin_size;
Andrei Emeltchenko12d59782012-10-10 17:38:26 +0300583 chan->flush_to = opts.flush_to;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200584 break;
585
586 case L2CAP_LM:
587 if (get_user(opt, (u32 __user *) optval)) {
588 err = -EFAULT;
589 break;
590 }
591
592 if (opt & L2CAP_LM_AUTH)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300593 chan->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200594 if (opt & L2CAP_LM_ENCRYPT)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300595 chan->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200596 if (opt & L2CAP_LM_SECURE)
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300597 chan->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200598
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +0300599 if (opt & L2CAP_LM_MASTER)
600 set_bit(FLAG_ROLE_SWITCH, &chan->flags);
601 else
602 clear_bit(FLAG_ROLE_SWITCH, &chan->flags);
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +0300603
604 if (opt & L2CAP_LM_RELIABLE)
605 set_bit(FLAG_FORCE_RELIABLE, &chan->flags);
606 else
607 clear_bit(FLAG_FORCE_RELIABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200608 break;
609
610 default:
611 err = -ENOPROTOOPT;
612 break;
613 }
614
615 release_sock(sk);
616 return err;
617}
618
Gustavo Padovan2d792812012-10-06 10:07:01 +0100619static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
620 char __user *optval, unsigned int optlen)
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200621{
622 struct sock *sk = sock->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300623 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200624 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700625 struct bt_power pwr;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300626 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200627 int len, err = 0;
628 u32 opt;
629
630 BT_DBG("sk %p", sk);
631
632 if (level == SOL_L2CAP)
633 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
634
635 if (level != SOL_BLUETOOTH)
636 return -ENOPROTOOPT;
637
638 lock_sock(sk);
639
640 switch (optname) {
641 case BT_SECURITY:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300642 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100643 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200644 err = -EINVAL;
645 break;
646 }
647
648 sec.level = BT_SECURITY_LOW;
649
650 len = min_t(unsigned int, sizeof(sec), optlen);
651 if (copy_from_user((char *) &sec, optval, len)) {
652 err = -EFAULT;
653 break;
654 }
655
656 if (sec.level < BT_SECURITY_LOW ||
Gustavo Padovan2d792812012-10-06 10:07:01 +0100657 sec.level > BT_SECURITY_HIGH) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200658 err = -EINVAL;
659 break;
660 }
661
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300662 chan->sec_level = sec.level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300663
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200664 if (!chan->conn)
665 break;
666
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300667 conn = chan->conn;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200668
669 /*change security for LE channels */
Johan Hedberg073d1cf2013-04-29 19:35:35 +0300670 if (chan->scid == L2CAP_CID_ATT) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300671 if (!conn->hcon->out) {
672 err = -EINVAL;
673 break;
674 }
675
Vinicius Costa Gomescc110922012-08-23 21:32:43 -0300676 if (smp_conn_security(conn->hcon, sec.level))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300677 break;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300678 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200679 chan->state = BT_CONFIG;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200680
Gustavo Padovana7d77232012-05-13 03:20:07 -0300681 /* or for ACL link */
682 } else if ((sk->sk_state == BT_CONNECT2 &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100683 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) ||
Gustavo Padovana7d77232012-05-13 03:20:07 -0300684 sk->sk_state == BT_CONNECTED) {
685 if (!l2cap_chan_check_security(chan))
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300686 set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
Gustavo Padovana7d77232012-05-13 03:20:07 -0300687 else
688 sk->sk_state_change(sk);
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200689 } else {
690 err = -EINVAL;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300691 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200692 break;
693
694 case BT_DEFER_SETUP:
695 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
696 err = -EINVAL;
697 break;
698 }
699
700 if (get_user(opt, (u32 __user *) optval)) {
701 err = -EFAULT;
702 break;
703 }
704
Marcel Holtmannbdc25782013-10-14 02:45:34 -0700705 if (opt) {
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300706 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
Marcel Holtmannbdc25782013-10-14 02:45:34 -0700707 set_bit(FLAG_DEFER_SETUP, &chan->flags);
708 } else {
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300709 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
Marcel Holtmannbdc25782013-10-14 02:45:34 -0700710 clear_bit(FLAG_DEFER_SETUP, &chan->flags);
711 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200712 break;
713
714 case BT_FLUSHABLE:
715 if (get_user(opt, (u32 __user *) optval)) {
716 err = -EFAULT;
717 break;
718 }
719
720 if (opt > BT_FLUSHABLE_ON) {
721 err = -EINVAL;
722 break;
723 }
724
725 if (opt == BT_FLUSHABLE_OFF) {
Johannes Bergc1f23a22013-10-07 18:19:16 +0200726 conn = chan->conn;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300727 /* proceed further only when we have l2cap_conn and
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200728 No Flush support in the LM */
729 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
730 err = -EINVAL;
731 break;
732 }
733 }
734
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +0300735 if (opt)
736 set_bit(FLAG_FLUSHABLE, &chan->flags);
737 else
738 clear_bit(FLAG_FLUSHABLE, &chan->flags);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200739 break;
740
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700741 case BT_POWER:
742 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100743 chan->chan_type != L2CAP_CHAN_RAW) {
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700744 err = -EINVAL;
745 break;
746 }
747
748 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
749
750 len = min_t(unsigned int, sizeof(pwr), optlen);
751 if (copy_from_user((char *) &pwr, optval, len)) {
752 err = -EFAULT;
753 break;
754 }
Andrei Emeltchenko15770b12011-10-11 14:04:33 +0300755
756 if (pwr.force_active)
757 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
758 else
759 clear_bit(FLAG_FORCE_ACTIVE, &chan->flags);
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700760 break;
761
Mat Martineau2ea66482011-11-02 16:18:30 -0700762 case BT_CHANNEL_POLICY:
Mat Martineau2ea66482011-11-02 16:18:30 -0700763 if (get_user(opt, (u32 __user *) optval)) {
764 err = -EFAULT;
765 break;
766 }
767
768 if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) {
769 err = -EINVAL;
770 break;
771 }
772
773 if (chan->mode != L2CAP_MODE_ERTM &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100774 chan->mode != L2CAP_MODE_STREAMING) {
Mat Martineau2ea66482011-11-02 16:18:30 -0700775 err = -EOPNOTSUPP;
776 break;
777 }
778
779 chan->chan_policy = (u8) opt;
Mat Martineau3f7a56c2012-10-23 15:24:23 -0700780
781 if (sk->sk_state == BT_CONNECTED &&
782 chan->move_role == L2CAP_MOVE_ROLE_NONE)
783 l2cap_move_start(chan);
784
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200785 break;
786
787 default:
788 err = -ENOPROTOOPT;
789 break;
790 }
791
792 release_sock(sk);
793 return err;
794}
795
Gustavo Padovan2d792812012-10-06 10:07:01 +0100796static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
797 struct msghdr *msg, size_t len)
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200798{
799 struct sock *sk = sock->sk;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300800 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200801 int err;
802
803 BT_DBG("sock %p, sk %p", sock, sk);
804
805 err = sock_error(sk);
806 if (err)
807 return err;
808
809 if (msg->msg_flags & MSG_OOB)
810 return -EOPNOTSUPP;
811
Mat Martineaua6a55682012-05-04 14:20:31 -0700812 if (sk->sk_state != BT_CONNECTED)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -0300813 return -ENOTCONN;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200814
Johan Hedberge793dcf2013-09-16 13:05:19 +0300815 lock_sock(sk);
816 err = bt_sock_wait_ready(sk, msg->msg_flags);
817 release_sock(sk);
818 if (err)
819 return err;
820
Mat Martineaua6a55682012-05-04 14:20:31 -0700821 l2cap_chan_lock(chan);
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200822 err = l2cap_chan_send(chan, msg, len, sk->sk_priority);
Mat Martineaua6a55682012-05-04 14:20:31 -0700823 l2cap_chan_unlock(chan);
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200824
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200825 return err;
826}
827
Gustavo Padovan2d792812012-10-06 10:07:01 +0100828static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
829 struct msghdr *msg, size_t len, int flags)
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200830{
831 struct sock *sk = sock->sk;
Mat Martineaue3281402011-07-07 09:39:02 -0700832 struct l2cap_pinfo *pi = l2cap_pi(sk);
833 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200834
835 lock_sock(sk);
836
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300837 if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
838 &bt_sk(sk)->flags)) {
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300839 sk->sk_state = BT_CONFIG;
Gustavo F. Padovan3542b8542011-12-28 13:54:17 -0200840 pi->chan->state = BT_CONFIG;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300841
Mat Martineaue3281402011-07-07 09:39:02 -0700842 __l2cap_connect_rsp_defer(pi->chan);
Johan Hedberg970871b2013-09-25 13:26:05 +0300843 err = 0;
844 goto done;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200845 }
846
847 release_sock(sk);
848
849 if (sock->type == SOCK_STREAM)
Mat Martineaue3281402011-07-07 09:39:02 -0700850 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
851 else
852 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200853
Mat Martineaue3281402011-07-07 09:39:02 -0700854 if (pi->chan->mode != L2CAP_MODE_ERTM)
855 return err;
856
857 /* Attempt to put pending rx data in the socket buffer */
858
859 lock_sock(sk);
860
861 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
862 goto done;
863
864 if (pi->rx_busy_skb) {
865 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
866 pi->rx_busy_skb = NULL;
867 else
868 goto done;
869 }
870
871 /* Restore data flow when half of the receive buffer is
872 * available. This avoids resending large numbers of
873 * frames.
874 */
875 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
876 l2cap_chan_busy(pi->chan, 0);
877
878done:
879 release_sock(sk);
880 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200881}
882
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200883/* Kill socket (only if zapped and orphan)
884 * Must be called on unlocked socket.
885 */
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -0300886static void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200887{
888 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
889 return;
890
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200891 BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200892
893 /* Kill poor orphan */
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300894
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530895 l2cap_chan_put(l2cap_pi(sk)->chan);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200896 sock_set_flag(sk, SOCK_DEAD);
897 sock_put(sk);
898}
899
Gustavo Padovandc253062013-10-15 19:24:49 -0300900static int __l2cap_wait_ack(struct sock *sk)
901{
902 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
903 DECLARE_WAITQUEUE(wait, current);
904 int err = 0;
905 int timeo = HZ/5;
906
907 add_wait_queue(sk_sleep(sk), &wait);
908 set_current_state(TASK_INTERRUPTIBLE);
909 while (chan->unacked_frames > 0 && chan->conn) {
910 if (!timeo)
911 timeo = HZ/5;
912
913 if (signal_pending(current)) {
914 err = sock_intr_errno(timeo);
915 break;
916 }
917
918 release_sock(sk);
919 timeo = schedule_timeout(timeo);
920 lock_sock(sk);
921 set_current_state(TASK_INTERRUPTIBLE);
922
923 err = sock_error(sk);
924 if (err)
925 break;
926 }
927 set_current_state(TASK_RUNNING);
928 remove_wait_queue(sk_sleep(sk), &wait);
929 return err;
930}
931
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200932static int l2cap_sock_shutdown(struct socket *sock, int how)
933{
934 struct sock *sk = sock->sk;
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200935 struct l2cap_chan *chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200936 struct l2cap_conn *conn;
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200937 int err = 0;
938
939 BT_DBG("sock %p, sk %p", sock, sk);
940
941 if (!sk)
942 return 0;
943
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200944 chan = l2cap_pi(sk)->chan;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200945 conn = chan->conn;
946
947 if (conn)
948 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko7ddb6e02012-02-14 15:12:57 +0200949
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200950 l2cap_chan_lock(chan);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200951 lock_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200952
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200953 if (!sk->sk_shutdown) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300954 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200955 err = __l2cap_wait_ack(sk);
956
957 sk->sk_shutdown = SHUTDOWN_MASK;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200958
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200959 release_sock(sk);
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300960 l2cap_chan_close(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200961 lock_sock(sk);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200962
963 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
964 err = bt_sock_wait_state(sk, BT_CLOSED,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100965 sk->sk_lingertime);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200966 }
967
968 if (!err && sk->sk_err)
969 err = -sk->sk_err;
970
971 release_sock(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200972 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200973
974 if (conn)
975 mutex_unlock(&conn->chan_lock);
976
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200977 return err;
978}
979
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200980static int l2cap_sock_release(struct socket *sock)
981{
982 struct sock *sk = sock->sk;
983 int err;
984
985 BT_DBG("sock %p, sk %p", sock, sk);
986
987 if (!sk)
988 return 0;
989
Masatake YAMATO5b28d952012-07-26 01:29:25 +0900990 bt_sock_unlink(&l2cap_sk_list, sk);
991
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200992 err = l2cap_sock_shutdown(sock, 2);
993
994 sock_orphan(sk);
995 l2cap_sock_kill(sk);
996 return err;
997}
998
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -0300999static void l2cap_sock_cleanup_listen(struct sock *parent)
1000{
1001 struct sock *sk;
1002
1003 BT_DBG("parent %p", parent);
1004
1005 /* Close not yet accepted channels */
1006 while ((sk = bt_accept_dequeue(parent, NULL))) {
1007 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
1008
1009 l2cap_chan_lock(chan);
1010 __clear_chan_timer(chan);
1011 l2cap_chan_close(chan, ECONNRESET);
1012 l2cap_chan_unlock(chan);
1013
1014 l2cap_sock_kill(sk);
1015 }
1016}
1017
Gustavo Padovan80b98022012-05-27 22:27:51 -03001018static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001019{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001020 struct sock *sk, *parent = chan->data;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001021
Gustavo Padovan53826692012-05-27 22:27:55 -03001022 /* Check for backlog size */
1023 if (sk_acceptq_is_full(parent)) {
1024 BT_DBG("backlog full %d", parent->sk_ack_backlog);
1025 return NULL;
1026 }
1027
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001028 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001029 GFP_ATOMIC);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001030 if (!sk)
1031 return NULL;
1032
Octavian Purdilad22015a2012-01-22 00:28:34 +02001033 bt_sock_reclassify_lock(sk, BTPROTO_L2CAP);
1034
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001035 l2cap_sock_init(sk, parent);
1036
Gustavo Padovan644912e2012-10-12 19:35:23 +08001037 bt_accept_enqueue(parent, sk);
1038
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001039 return l2cap_pi(sk)->chan;
1040}
1041
Gustavo Padovan80b98022012-05-27 22:27:51 -03001042static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001043{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001044 struct sock *sk = chan->data;
Marcel Holtmann84b34d92013-10-13 11:36:07 -07001045 int err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001046
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001047 lock_sock(sk);
1048
Marcel Holtmann84b34d92013-10-13 11:36:07 -07001049 if (l2cap_pi(sk)->rx_busy_skb) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001050 err = -ENOMEM;
1051 goto done;
1052 }
Mat Martineaue3281402011-07-07 09:39:02 -07001053
1054 err = sock_queue_rcv_skb(sk, skb);
1055
1056 /* For ERTM, handle one skb that doesn't fit into the recv
1057 * buffer. This is important to do because the data frames
1058 * have already been acked, so the skb cannot be discarded.
1059 *
1060 * Notify the l2cap core that the buffer is full, so the
1061 * LOCAL_BUSY state is entered and no more frames are
1062 * acked and reassembled until there is buffer space
1063 * available.
1064 */
Marcel Holtmann84b34d92013-10-13 11:36:07 -07001065 if (err < 0 && chan->mode == L2CAP_MODE_ERTM) {
1066 l2cap_pi(sk)->rx_busy_skb = skb;
1067 l2cap_chan_busy(chan, 1);
Mat Martineaue3281402011-07-07 09:39:02 -07001068 err = 0;
1069 }
1070
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001071done:
1072 release_sock(sk);
1073
Mat Martineaue3281402011-07-07 09:39:02 -07001074 return err;
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001075}
1076
Gustavo Padovan80b98022012-05-27 22:27:51 -03001077static void l2cap_sock_close_cb(struct l2cap_chan *chan)
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001078{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001079 struct sock *sk = chan->data;
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001080
1081 l2cap_sock_kill(sk);
1082}
1083
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001084static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
1085{
1086 struct sock *sk = chan->data;
1087 struct sock *parent;
1088
1089 lock_sock(sk);
1090
1091 parent = bt_sk(sk)->parent;
1092
1093 sock_set_flag(sk, SOCK_ZAPPED);
1094
1095 switch (chan->state) {
1096 case BT_OPEN:
1097 case BT_BOUND:
1098 case BT_CLOSED:
1099 break;
1100 case BT_LISTEN:
1101 l2cap_sock_cleanup_listen(sk);
1102 sk->sk_state = BT_CLOSED;
1103 chan->state = BT_CLOSED;
1104
1105 break;
1106 default:
1107 sk->sk_state = BT_CLOSED;
1108 chan->state = BT_CLOSED;
1109
1110 sk->sk_err = err;
1111
1112 if (parent) {
1113 bt_accept_unlink(sk);
1114 parent->sk_data_ready(parent, 0);
1115 } else {
1116 sk->sk_state_change(sk);
1117 }
1118
1119 break;
1120 }
1121
1122 release_sock(sk);
1123}
1124
Gustavo Padovan53f52122013-10-15 19:24:45 -03001125static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state,
1126 int err)
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001127{
Gustavo Padovan80b98022012-05-27 22:27:51 -03001128 struct sock *sk = chan->data;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001129
1130 sk->sk_state = state;
Gustavo Padovan53f52122013-10-15 19:24:45 -03001131
1132 if (err)
1133 sk->sk_err = err;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001134}
1135
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001136static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
Gustavo Padovan90338942012-04-06 20:15:47 -03001137 unsigned long len, int nb)
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001138{
Gustavo Padovan90338942012-04-06 20:15:47 -03001139 struct sk_buff *skb;
1140 int err;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001141
Mat Martineaua6a55682012-05-04 14:20:31 -07001142 l2cap_chan_unlock(chan);
Gustavo Padovan90338942012-04-06 20:15:47 -03001143 skb = bt_skb_send_alloc(chan->sk, len, nb, &err);
Mat Martineaua6a55682012-05-04 14:20:31 -07001144 l2cap_chan_lock(chan);
1145
Gustavo Padovan90338942012-04-06 20:15:47 -03001146 if (!skb)
1147 return ERR_PTR(err);
1148
1149 return skb;
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001150}
1151
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001152static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
1153{
1154 struct sock *sk = chan->data;
1155 struct sock *parent;
1156
1157 lock_sock(sk);
1158
1159 parent = bt_sk(sk)->parent;
1160
1161 BT_DBG("sk %p, parent %p", sk, parent);
1162
1163 sk->sk_state = BT_CONNECTED;
1164 sk->sk_state_change(sk);
1165
1166 if (parent)
1167 parent->sk_data_ready(parent, 0);
1168
1169 release_sock(sk);
1170}
1171
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001172static void l2cap_sock_defer_cb(struct l2cap_chan *chan)
1173{
1174 struct sock *sk = chan->data;
1175 struct sock *parent = bt_sk(sk)->parent;
1176
1177 if (parent)
1178 parent->sk_data_ready(parent, 0);
1179}
1180
Marcel Holtmannd97c8992013-10-14 02:53:54 -07001181static void l2cap_sock_resume_cb(struct l2cap_chan *chan)
1182{
1183 struct sock *sk = chan->data;
1184
1185 clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
1186 sk->sk_state_change(sk);
1187}
1188
Gustavo Padovan5ec1bbe2013-10-15 19:24:48 -03001189static void l2cap_sock_set_shutdown_cb(struct l2cap_chan *chan)
1190{
1191 struct sock *sk = chan->data;
1192
1193 lock_sock(sk);
1194 sk->sk_shutdown = SHUTDOWN_MASK;
1195 release_sock(sk);
1196}
1197
Gustavo Padovan8d836d72013-10-15 19:24:47 -03001198static long l2cap_sock_get_sndtimeo_cb(struct l2cap_chan *chan)
1199{
1200 struct sock *sk = chan->data;
1201
1202 return sk->sk_sndtimeo;
1203}
1204
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001205static struct l2cap_ops l2cap_chan_ops = {
1206 .name = "L2CAP Socket Interface",
1207 .new_connection = l2cap_sock_new_connection_cb,
Gustavo F. Padovan23070492011-05-16 17:57:22 -03001208 .recv = l2cap_sock_recv_cb,
Gustavo F. Padovanba3bd0e2011-05-16 18:23:24 -03001209 .close = l2cap_sock_close_cb,
Andrei Emeltchenkoc0df7f62012-05-27 22:27:52 -03001210 .teardown = l2cap_sock_teardown_cb,
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001211 .state_change = l2cap_sock_state_change_cb,
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001212 .ready = l2cap_sock_ready_cb,
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001213 .defer = l2cap_sock_defer_cb,
Marcel Holtmannd97c8992013-10-14 02:53:54 -07001214 .resume = l2cap_sock_resume_cb,
Gustavo Padovan5ec1bbe2013-10-15 19:24:48 -03001215 .set_shutdown = l2cap_sock_set_shutdown_cb,
Gustavo Padovan8d836d72013-10-15 19:24:47 -03001216 .get_sndtimeo = l2cap_sock_get_sndtimeo_cb,
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02001217 .alloc_skb = l2cap_sock_alloc_skb_cb,
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001218};
1219
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001220static void l2cap_sock_destruct(struct sock *sk)
1221{
1222 BT_DBG("sk %p", sk);
1223
Sasha Levin23d3a862012-10-08 16:48:32 -04001224 if (l2cap_pi(sk)->chan)
1225 l2cap_chan_put(l2cap_pi(sk)->chan);
Marcel Holtmann84b34d92013-10-13 11:36:07 -07001226
Mat Martineaue3281402011-07-07 09:39:02 -07001227 if (l2cap_pi(sk)->rx_busy_skb) {
1228 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
1229 l2cap_pi(sk)->rx_busy_skb = NULL;
1230 }
1231
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001232 skb_queue_purge(&sk->sk_receive_queue);
1233 skb_queue_purge(&sk->sk_write_queue);
1234}
1235
Marcel Holtmann2edf8702013-10-13 12:55:29 -07001236static void l2cap_skb_msg_name(struct sk_buff *skb, void *msg_name,
1237 int *msg_namelen)
1238{
1239 struct sockaddr_l2 *la = (struct sockaddr_l2 *) msg_name;
1240
1241 memset(la, 0, sizeof(struct sockaddr_l2));
1242 la->l2_family = AF_BLUETOOTH;
1243 la->l2_psm = bt_cb(skb)->psm;
1244 bacpy(&la->l2_bdaddr, &bt_cb(skb)->bdaddr);
1245
1246 *msg_namelen = sizeof(struct sockaddr_l2);
1247}
1248
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001249static void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001250{
Marcel Holtmann84b34d92013-10-13 11:36:07 -07001251 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001252
1253 BT_DBG("sk %p", sk);
1254
1255 if (parent) {
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001256 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
1257
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001258 sk->sk_type = parent->sk_type;
Gustavo Padovanc5daa682012-05-16 12:17:10 -03001259 bt_sk(sk)->flags = bt_sk(parent)->flags;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001260
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001261 chan->chan_type = pchan->chan_type;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001262 chan->imtu = pchan->imtu;
1263 chan->omtu = pchan->omtu;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001264 chan->conf_state = pchan->conf_state;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001265 chan->mode = pchan->mode;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001266 chan->fcs = pchan->fcs;
1267 chan->max_tx = pchan->max_tx;
1268 chan->tx_win = pchan->tx_win;
Andrei Emeltchenko6b3c7102011-11-02 09:57:10 +02001269 chan->tx_win_max = pchan->tx_win_max;
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001270 chan->sec_level = pchan->sec_level;
Andrei Emeltchenkod57b0e82011-10-11 14:04:31 +03001271 chan->flags = pchan->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +00001272
1273 security_sk_clone(parent, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001274 } else {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001275 switch (sk->sk_type) {
1276 case SOCK_RAW:
1277 chan->chan_type = L2CAP_CHAN_RAW;
1278 break;
1279 case SOCK_DGRAM:
1280 chan->chan_type = L2CAP_CHAN_CONN_LESS;
Marcel Holtmann2edf8702013-10-13 12:55:29 -07001281 bt_sk(sk)->skb_msg_name = l2cap_skb_msg_name;
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001282 break;
1283 case SOCK_SEQPACKET:
1284 case SOCK_STREAM:
1285 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
1286 break;
1287 }
1288
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001289 chan->imtu = L2CAP_DEFAULT_MTU;
1290 chan->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001291 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001292 chan->mode = L2CAP_MODE_ERTM;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001293 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001294 } else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001295 chan->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001296 }
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +03001297
1298 l2cap_chan_set_defaults(chan);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001299 }
1300
1301 /* Default config options */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001302 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001303
1304 chan->data = sk;
1305 chan->ops = &l2cap_chan_ops;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001306}
1307
1308static struct proto l2cap_proto = {
1309 .name = "L2CAP",
1310 .owner = THIS_MODULE,
1311 .obj_size = sizeof(struct l2cap_pinfo)
1312};
1313
Gustavo Padovan2d792812012-10-06 10:07:01 +01001314static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
1315 int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001316{
1317 struct sock *sk;
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001318 struct l2cap_chan *chan;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001319
1320 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1321 if (!sk)
1322 return NULL;
1323
1324 sock_init_data(sock, sk);
1325 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1326
1327 sk->sk_destruct = l2cap_sock_destruct;
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08001328 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001329
1330 sock_reset_flag(sk, SOCK_ZAPPED);
1331
1332 sk->sk_protocol = proto;
1333 sk->sk_state = BT_OPEN;
1334
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001335 chan = l2cap_chan_create();
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001336 if (!chan) {
Jaganath Kanakkassery49dfbb92012-07-19 12:54:04 +05301337 sk_free(sk);
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001338 return NULL;
1339 }
1340
Mat Martineau61d6ef32012-04-27 16:50:50 -07001341 l2cap_chan_hold(chan);
1342
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -03001343 chan->sk = sk;
1344
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001345 l2cap_pi(sk)->chan = chan;
1346
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001347 return sk;
1348}
1349
1350static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1351 int kern)
1352{
1353 struct sock *sk;
1354
1355 BT_DBG("sock %p", sock);
1356
1357 sock->state = SS_UNCONNECTED;
1358
1359 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01001360 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001361 return -ESOCKTNOSUPPORT;
1362
1363 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1364 return -EPERM;
1365
1366 sock->ops = &l2cap_sock_ops;
1367
1368 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1369 if (!sk)
1370 return -ENOMEM;
1371
1372 l2cap_sock_init(sk, NULL);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001373 bt_sock_link(&l2cap_sk_list, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001374 return 0;
1375}
1376
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -03001377static const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001378 .family = PF_BLUETOOTH,
1379 .owner = THIS_MODULE,
1380 .release = l2cap_sock_release,
1381 .bind = l2cap_sock_bind,
1382 .connect = l2cap_sock_connect,
1383 .listen = l2cap_sock_listen,
1384 .accept = l2cap_sock_accept,
1385 .getname = l2cap_sock_getname,
1386 .sendmsg = l2cap_sock_sendmsg,
1387 .recvmsg = l2cap_sock_recvmsg,
1388 .poll = bt_sock_poll,
1389 .ioctl = bt_sock_ioctl,
1390 .mmap = sock_no_mmap,
1391 .socketpair = sock_no_socketpair,
1392 .shutdown = l2cap_sock_shutdown,
1393 .setsockopt = l2cap_sock_setsockopt,
1394 .getsockopt = l2cap_sock_getsockopt
1395};
1396
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001397static const struct net_proto_family l2cap_sock_family_ops = {
1398 .family = PF_BLUETOOTH,
1399 .owner = THIS_MODULE,
1400 .create = l2cap_sock_create,
1401};
1402
1403int __init l2cap_init_sockets(void)
1404{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001405 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001406
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001407 err = proto_register(&l2cap_proto, 0);
1408 if (err < 0)
1409 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001410
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001411 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001412 if (err < 0) {
1413 BT_ERR("L2CAP socket registration failed");
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001414 goto error;
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001415 }
1416
Al Virob0316612013-04-04 19:14:33 -04001417 err = bt_procfs_init(&init_net, "l2cap", &l2cap_sk_list,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001418 NULL);
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001419 if (err < 0) {
1420 BT_ERR("Failed to create L2CAP proc file");
1421 bt_sock_unregister(BTPROTO_L2CAP);
1422 goto error;
1423 }
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001424
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001425 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001426
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001427 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001428
1429error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001430 proto_unregister(&l2cap_proto);
1431 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001432}
1433
1434void l2cap_cleanup_sockets(void)
1435{
Masatake YAMATO5b28d952012-07-26 01:29:25 +09001436 bt_procfs_cleanup(&init_net, "l2cap");
David Herrmann5e9d7f82013-02-24 19:36:51 +01001437 bt_sock_unregister(BTPROTO_L2CAP);
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001438 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001439}