blob: 8eb1e16bf2e204098ba94a199642b9ad170fe33f [file] [log] [blame]
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001/*
2 BlueZ - Bluetooth protocol stack for Linux
Mat Martineau966dcfa2011-12-12 10:45:31 -08003 Copyright (c) 2000-2001, 2011-2012 Code Aurora Forum. All rights reserved.
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02004 Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
5 Copyright (C) 2010 Google Inc.
6
7 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 2 as
11 published by the Free Software Foundation;
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
16 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
17 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
18 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21
22 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
23 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
24 SOFTWARE IS DISCLAIMED.
25*/
26
27/* Bluetooth L2CAP sockets. */
28
29#include <net/bluetooth/bluetooth.h>
Gustavo F. Padovan33575df2011-02-04 02:48:48 -020030#include <net/bluetooth/hci_core.h>
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020031#include <net/bluetooth/l2cap.h>
Vinicius Costa Gomesa5474a82011-01-26 21:42:57 -030032#include <net/bluetooth/smp.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <net/bluetooth/amp.h>
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020034
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035/* ---- L2CAP timers ---- */
36static void l2cap_sock_timeout(unsigned long arg)
37{
38 struct sock *sk = (struct sock *) arg;
39 int reason;
40
41 BT_DBG("sock %p state %d", sk, sk->sk_state);
42
43 bh_lock_sock(sk);
44
45 if (sock_owned_by_user(sk)) {
46 /* sk is owned by user. Try again later */
47 l2cap_sock_set_timer(sk, HZ / 5);
48 bh_unlock_sock(sk);
49 sock_put(sk);
50 return;
51 }
52
53 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG)
54 reason = ECONNREFUSED;
55 else if (sk->sk_state == BT_CONNECT &&
56 l2cap_pi(sk)->sec_level != BT_SECURITY_SDP)
57 reason = ECONNREFUSED;
58 else
59 reason = ETIMEDOUT;
60
61 __l2cap_sock_close(sk, reason);
62
63 bh_unlock_sock(sk);
64
65 l2cap_sock_kill(sk);
66 sock_put(sk);
67}
68
69void l2cap_sock_set_timer(struct sock *sk, long timeout)
70{
71 BT_DBG("sk %p state %d timeout %ld", sk, sk->sk_state, timeout);
72 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
73}
74
75void l2cap_sock_clear_timer(struct sock *sk)
76{
77 BT_DBG("sock %p state %d", sk, sk->sk_state);
78 sk_stop_timer(sk, &sk->sk_timer);
79}
80
81static struct sock *__l2cap_get_sock_by_addr(__le16 psm, bdaddr_t *src)
82{
83 struct sock *sk;
84 struct hlist_node *node;
85 sk_for_each(sk, node, &l2cap_sk_list.head)
86 if (l2cap_pi(sk)->sport == psm && !bacmp(&bt_sk(sk)->src, src))
87 goto found;
88 sk = NULL;
89found:
90 return sk;
91}
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -030092
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020093static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
94{
95 struct sock *sk = sock->sk;
96 struct sockaddr_l2 la;
97 int len, err = 0;
98
99 BT_DBG("sk %p", sk);
100
101 if (!addr || addr->sa_family != AF_BLUETOOTH)
102 return -EINVAL;
103
104 memset(&la, 0, sizeof(la));
105 len = min_t(unsigned int, sizeof(la), alen);
106 memcpy(&la, addr, len);
107
Ville Tervob62f3282011-02-10 22:38:50 -0300108 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200109 return -EINVAL;
110
111 lock_sock(sk);
112
113 if (sk->sk_state != BT_OPEN) {
114 err = -EBADFD;
115 goto done;
116 }
117
118 if (la.l2_psm) {
119 __u16 psm = __le16_to_cpu(la.l2_psm);
120
121 /* PSM must be odd and lsb of upper byte must be 0 */
122 if ((psm & 0x0101) != 0x0001) {
123 err = -EINVAL;
124 goto done;
125 }
126
127 /* Restrict usage of well-known PSMs */
128 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
129 err = -EACCES;
130 goto done;
131 }
132 }
133
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134 write_lock_bh(&l2cap_sk_list.lock);
135
136 if (la.l2_psm && __l2cap_get_sock_by_addr(la.l2_psm, &la.l2_bdaddr)) {
137 err = -EADDRINUSE;
138 } else {
139 /* Save source address */
140 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
141 l2cap_pi(sk)->psm = la.l2_psm;
142 l2cap_pi(sk)->sport = la.l2_psm;
143 sk->sk_state = BT_BOUND;
144
145 if (__le16_to_cpu(la.l2_psm) == 0x0001 ||
146 __le16_to_cpu(la.l2_psm) == 0x0003)
147 l2cap_pi(sk)->sec_level = BT_SECURITY_SDP;
148 }
149
Ville Tervob62f3282011-02-10 22:38:50 -0300150 if (la.l2_cid)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151 l2cap_pi(sk)->scid = la.l2_cid;
Ville Tervob62f3282011-02-10 22:38:50 -0300152
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153 write_unlock_bh(&l2cap_sk_list.lock);
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200154
155done:
156 release_sock(sk);
157 return err;
158}
159
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200160static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
161{
162 struct sock *sk = sock->sk;
163 struct sockaddr_l2 la;
164 int len, err = 0;
165
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 BT_DBG("sk %p type %d mode %d state %d", sk, sk->sk_type,
167 l2cap_pi(sk)->mode, sk->sk_state);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200168
169 if (!addr || alen < sizeof(addr->sa_family) ||
170 addr->sa_family != AF_BLUETOOTH)
171 return -EINVAL;
172
173 memset(&la, 0, sizeof(la));
174 len = min_t(unsigned int, sizeof(la), alen);
175 memcpy(&la, addr, len);
176
Ville Tervoacd7d372011-02-10 22:38:49 -0300177 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200178 return -EINVAL;
179
180 lock_sock(sk);
181
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182 if ((sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM)
183 && !(la.l2_psm || la.l2_cid || l2cap_pi(sk)->fixed_channel)) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200184 err = -EINVAL;
185 goto done;
186 }
187
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188 switch (l2cap_pi(sk)->mode) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200189 case L2CAP_MODE_BASIC:
190 break;
191 case L2CAP_MODE_ERTM:
192 case L2CAP_MODE_STREAMING:
193 if (!disable_ertm)
194 break;
195 /* fall through */
196 default:
197 err = -ENOTSUPP;
198 goto done;
199 }
200
201 switch (sk->sk_state) {
202 case BT_CONNECT:
203 case BT_CONNECT2:
204 case BT_CONFIG:
205 /* Already connecting */
206 goto wait;
207
208 case BT_CONNECTED:
209 /* Already connected */
210 err = -EISCONN;
211 goto done;
212
213 case BT_OPEN:
214 case BT_BOUND:
215 /* Can connect */
216 break;
217
218 default:
219 err = -EBADFD;
220 goto done;
221 }
222
223 /* PSM must be odd and lsb of upper byte must be 0 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 &&
225 !l2cap_pi(sk)->fixed_channel &&
226 sk->sk_type != SOCK_RAW && !la.l2_cid) {
227 BT_DBG("Bad PSM 0x%x", (int)__le16_to_cpu(la.l2_psm));
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200228 err = -EINVAL;
229 goto done;
230 }
231
232 /* Set destination address and psm */
233 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 l2cap_pi(sk)->psm = la.l2_psm;
235 l2cap_pi(sk)->dcid = la.l2_cid;
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200236
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237 err = l2cap_do_connect(sk);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200238 if (err)
239 goto done;
240
241wait:
242 err = bt_sock_wait_state(sk, BT_CONNECTED,
243 sock_sndtimeo(sk, flags & O_NONBLOCK));
244done:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245 if (err)
246 BT_ERR("failed %d", err);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200247 release_sock(sk);
248 return err;
249}
250
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200251static int l2cap_sock_listen(struct socket *sock, int backlog)
252{
253 struct sock *sk = sock->sk;
254 int err = 0;
255
256 BT_DBG("sk %p backlog %d", sk, backlog);
257
258 lock_sock(sk);
259
260 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
261 || sk->sk_state != BT_BOUND) {
262 err = -EBADFD;
263 goto done;
264 }
265
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 switch (l2cap_pi(sk)->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200267 case L2CAP_MODE_BASIC:
268 break;
269 case L2CAP_MODE_ERTM:
270 case L2CAP_MODE_STREAMING:
271 if (!disable_ertm)
272 break;
273 /* fall through */
274 default:
275 err = -ENOTSUPP;
276 goto done;
277 }
278
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 if (!l2cap_pi(sk)->psm && !l2cap_pi(sk)->scid) {
280 bdaddr_t *src = &bt_sk(sk)->src;
281 u16 psm;
282
283 err = -EINVAL;
284
285 write_lock_bh(&l2cap_sk_list.lock);
286
287 for (psm = 0x1001; psm < 0x1100; psm += 2)
288 if (!__l2cap_get_sock_by_addr(cpu_to_le16(psm), src)) {
289 l2cap_pi(sk)->psm = cpu_to_le16(psm);
290 l2cap_pi(sk)->sport = cpu_to_le16(psm);
291 err = 0;
292 break;
293 }
294
295 write_unlock_bh(&l2cap_sk_list.lock);
296
297 if (err < 0)
298 goto done;
299 }
300
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200301 sk->sk_max_ack_backlog = backlog;
302 sk->sk_ack_backlog = 0;
303 sk->sk_state = BT_LISTEN;
304
305done:
306 release_sock(sk);
307 return err;
308}
309
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200310static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
311{
312 DECLARE_WAITQUEUE(wait, current);
313 struct sock *sk = sock->sk, *nsk;
314 long timeo;
315 int err = 0;
316
317 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
318
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319 if (sk->sk_state != BT_LISTEN) {
320 err = -EBADFD;
321 goto done;
322 }
323
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200324 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
325
326 BT_DBG("sk %p timeo %ld", sk, timeo);
327
328 /* Wait for an incoming connection. (wake-one). */
329 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700330 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200331 set_current_state(TASK_INTERRUPTIBLE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332 if (!timeo) {
333 err = -EAGAIN;
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200334 break;
335 }
336
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 release_sock(sk);
338 timeo = schedule_timeout(timeo);
339 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Peter Hurley4e20b5e2011-07-24 00:10:52 -0400340
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341 if (sk->sk_state != BT_LISTEN) {
342 err = -EBADFD;
Peter Hurley4e20b5e2011-07-24 00:10:52 -0400343 break;
344 }
345
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200346 if (signal_pending(current)) {
347 err = sock_intr_errno(timeo);
348 break;
349 }
350 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351 set_current_state(TASK_RUNNING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200352 remove_wait_queue(sk_sleep(sk), &wait);
353
354 if (err)
355 goto done;
356
357 newsock->state = SS_CONNECTED;
358
359 BT_DBG("new socket %p", nsk);
360
361done:
362 release_sock(sk);
363 return err;
364}
365
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200366static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
367{
368 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
369 struct sock *sk = sock->sk;
370
371 BT_DBG("sock %p, sk %p", sock, sk);
372
373 addr->sa_family = AF_BLUETOOTH;
374 *len = sizeof(struct sockaddr_l2);
375
376 if (peer) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377 la->l2_psm = l2cap_pi(sk)->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200378 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379 la->l2_cid = cpu_to_le16(l2cap_pi(sk)->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200380 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 la->l2_psm = l2cap_pi(sk)->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200382 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383 la->l2_cid = cpu_to_le16(l2cap_pi(sk)->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200384 }
385
386 return 0;
387}
388
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200389static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
390{
391 struct sock *sk = sock->sk;
392 struct l2cap_options opts;
393 struct l2cap_conninfo cinfo;
394 int len, err = 0;
395 u32 opt;
396
397 BT_DBG("sk %p", sk);
398
399 if (get_user(len, optlen))
400 return -EFAULT;
401
402 lock_sock(sk);
403
404 switch (optname) {
405 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300406 memset(&opts, 0, sizeof(opts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407 opts.imtu = l2cap_pi(sk)->imtu;
408 opts.omtu = l2cap_pi(sk)->omtu;
409 opts.flush_to = l2cap_pi(sk)->flush_to;
410 opts.mode = l2cap_pi(sk)->mode;
411 opts.fcs = l2cap_pi(sk)->fcs;
412 opts.max_tx = l2cap_pi(sk)->max_tx;
413 opts.txwin_size = l2cap_pi(sk)->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200414
415 len = min_t(unsigned int, len, sizeof(opts));
416 if (copy_to_user(optval, (char *) &opts, len))
417 err = -EFAULT;
418
419 break;
420
421 case L2CAP_LM:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422 switch (l2cap_pi(sk)->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200423 case BT_SECURITY_LOW:
424 opt = L2CAP_LM_AUTH;
425 break;
426 case BT_SECURITY_MEDIUM:
427 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
428 break;
429 case BT_SECURITY_HIGH:
430 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
431 L2CAP_LM_SECURE;
432 break;
433 default:
434 opt = 0;
435 break;
436 }
437
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438 if (l2cap_pi(sk)->role_switch)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200439 opt |= L2CAP_LM_MASTER;
440
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441 if (l2cap_pi(sk)->force_reliable)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200442 opt |= L2CAP_LM_RELIABLE;
443
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444 if (l2cap_pi(sk)->flushable)
445 opt |= L2CAP_LM_FLUSHABLE;
446
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200447 if (put_user(opt, (u32 __user *) optval))
448 err = -EFAULT;
449 break;
450
451 case L2CAP_CONNINFO:
452 if (sk->sk_state != BT_CONNECTED &&
453 !(sk->sk_state == BT_CONNECT2 &&
454 bt_sk(sk)->defer_setup)) {
455 err = -ENOTCONN;
456 break;
457 }
458
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 cinfo.hci_handle = l2cap_pi(sk)->conn->hcon->handle;
460 memcpy(cinfo.dev_class, l2cap_pi(sk)->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200461
462 len = min_t(unsigned int, len, sizeof(cinfo));
463 if (copy_to_user(optval, (char *) &cinfo, len))
464 err = -EFAULT;
465
466 break;
467
468 default:
469 err = -ENOPROTOOPT;
470 break;
471 }
472
473 release_sock(sk);
474 return err;
475}
476
477static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
478{
479 struct sock *sk = sock->sk;
480 struct bt_security sec;
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700481 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200482 int len, err = 0;
483
484 BT_DBG("sk %p", sk);
485
486 if (level == SOL_L2CAP)
487 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
488
489 if (level != SOL_BLUETOOTH)
490 return -ENOPROTOOPT;
491
492 if (get_user(len, optlen))
493 return -EFAULT;
494
495 lock_sock(sk);
496
497 switch (optname) {
498 case BT_SECURITY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
500 && sk->sk_type != SOCK_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200501 err = -EINVAL;
502 break;
503 }
504
Vinicius Costa Gomes89daa342011-07-08 18:31:46 -0300505 memset(&sec, 0, sizeof(sec));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 sec.level = l2cap_pi(sk)->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200507
Brian Gix27a795c2012-04-19 11:05:06 -0700508 if (sk->sk_state == BT_CONNECTED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 sec.key_size = l2cap_pi(sk)->conn->hcon->enc_key_size;
Brian Gix27a795c2012-04-19 11:05:06 -0700510 sec.level = l2cap_pi(sk)->conn->hcon->sec_level;
511 }
Vinicius Costa Gomes89daa342011-07-08 18:31:46 -0300512
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200513 len = min_t(unsigned int, len, sizeof(sec));
514 if (copy_to_user(optval, (char *) &sec, len))
515 err = -EFAULT;
516
517 break;
518
519 case BT_DEFER_SETUP:
520 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
521 err = -EINVAL;
522 break;
523 }
524
525 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
526 err = -EFAULT;
527
528 break;
529
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700530 case BT_POWER:
531 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
532 && sk->sk_type != SOCK_RAW) {
533 err = -EINVAL;
534 break;
535 }
536
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537 pwr.force_active = l2cap_pi(sk)->force_active;
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700538
539 len = min_t(unsigned int, len, sizeof(pwr));
540 if (copy_to_user(optval, (char *) &pwr, len))
541 err = -EFAULT;
542
543 break;
544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 case BT_AMP_POLICY:
546 if (put_user(l2cap_pi(sk)->amp_pref, (u32 __user *) optval))
547 err = -EFAULT;
548 break;
549
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200550 default:
551 err = -ENOPROTOOPT;
552 break;
553 }
554
555 release_sock(sk);
556 return err;
557}
558
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200559static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
560{
561 struct sock *sk = sock->sk;
562 struct l2cap_options opts;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563 int len, le_sock, err = 0;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200564 u32 opt;
565
566 BT_DBG("sk %p", sk);
567
568 lock_sock(sk);
569
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 le_sock = l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA;
571
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200572 switch (optname) {
573 case L2CAP_OPTIONS:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574 if (sk->sk_state == BT_CONNECTED && !le_sock) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200575 err = -EINVAL;
576 break;
577 }
578
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579 opts.imtu = l2cap_pi(sk)->imtu;
580 opts.omtu = l2cap_pi(sk)->omtu;
581 opts.flush_to = l2cap_pi(sk)->flush_to;
582 opts.mode = l2cap_pi(sk)->mode;
583 opts.fcs = l2cap_pi(sk)->fcs;
584 opts.max_tx = l2cap_pi(sk)->max_tx;
585 opts.txwin_size = l2cap_pi(sk)->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200586
587 len = min_t(unsigned int, sizeof(opts), optlen);
588 if (copy_from_user((char *) &opts, optval, len)) {
589 err = -EFAULT;
590 break;
591 }
592
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593 if ((opts.imtu || opts.omtu) && le_sock &&
594 (sk->sk_state == BT_CONNECTED)) {
595 if (opts.imtu >= L2CAP_LE_DEFAULT_MTU)
596 l2cap_pi(sk)->imtu = opts.imtu;
597 if (opts.omtu >= L2CAP_LE_DEFAULT_MTU)
598 l2cap_pi(sk)->omtu = opts.omtu;
599 if (opts.imtu < L2CAP_LE_DEFAULT_MTU ||
600 opts.omtu < L2CAP_LE_DEFAULT_MTU)
601 err = -EINVAL;
602 break;
603 }
604
605 if (opts.txwin_size < 1 ||
606 opts.txwin_size > L2CAP_TX_WIN_MAX_EXTENDED) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200607 err = -EINVAL;
608 break;
609 }
610
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611 l2cap_pi(sk)->mode = opts.mode;
612 switch (l2cap_pi(sk)->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200613 case L2CAP_MODE_BASIC:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614 l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200615 break;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200616 case L2CAP_MODE_STREAMING:
Mat Martineau0ac14362012-02-03 16:24:42 -0800617 if (!disable_ertm) {
618 /* No fallback to ERTM or Basic mode */
619 l2cap_pi(sk)->conf_state |=
620 L2CAP_CONF_STATE2_DEVICE;
621 break;
622 }
623 err = -EINVAL;
624 break;
625 case L2CAP_MODE_ERTM:
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200626 if (!disable_ertm)
627 break;
628 /* fall through */
629 default:
630 err = -EINVAL;
631 break;
632 }
633
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634 l2cap_pi(sk)->imtu = opts.imtu;
635 l2cap_pi(sk)->omtu = opts.omtu;
636 l2cap_pi(sk)->fcs = opts.fcs;
637 l2cap_pi(sk)->max_tx = opts.max_tx;
638 l2cap_pi(sk)->tx_win = opts.txwin_size;
Srinivas Krovvidi10734192011-12-29 07:29:11 +0530639 l2cap_pi(sk)->flush_to = opts.flush_to;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200640 break;
641
642 case L2CAP_LM:
643 if (get_user(opt, (u32 __user *) optval)) {
644 err = -EFAULT;
645 break;
646 }
647
648 if (opt & L2CAP_LM_AUTH)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 l2cap_pi(sk)->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200650 if (opt & L2CAP_LM_ENCRYPT)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 l2cap_pi(sk)->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200652 if (opt & L2CAP_LM_SECURE)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 l2cap_pi(sk)->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200654
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 l2cap_pi(sk)->role_switch = (opt & L2CAP_LM_MASTER);
656 l2cap_pi(sk)->force_reliable = (opt & L2CAP_LM_RELIABLE);
657 l2cap_pi(sk)->flushable = (opt & L2CAP_LM_FLUSHABLE);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200658 break;
659
660 default:
661 err = -ENOPROTOOPT;
662 break;
663 }
664
665 release_sock(sk);
666 return err;
667}
668
669static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
670{
671 struct sock *sk = sock->sk;
672 struct bt_security sec;
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700673 struct bt_power pwr;
Vinicius Costa Gomesa5474a82011-01-26 21:42:57 -0300674 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200675 int len, err = 0;
676 u32 opt;
677
678 BT_DBG("sk %p", sk);
679
680 if (level == SOL_L2CAP)
681 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
682
683 if (level != SOL_BLUETOOTH)
684 return -ENOPROTOOPT;
685
686 lock_sock(sk);
687
688 switch (optname) {
689 case BT_SECURITY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
691 && sk->sk_type != SOCK_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200692 err = -EINVAL;
693 break;
694 }
695
696 sec.level = BT_SECURITY_LOW;
697
698 len = min_t(unsigned int, sizeof(sec), optlen);
699 if (copy_from_user((char *) &sec, optval, len)) {
700 err = -EFAULT;
701 break;
702 }
703
704 if (sec.level < BT_SECURITY_LOW ||
705 sec.level > BT_SECURITY_HIGH) {
706 err = -EINVAL;
707 break;
708 }
709
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 l2cap_pi(sk)->sec_level = sec.level;
Vinicius Costa Gomesa5474a82011-01-26 21:42:57 -0300711
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712 conn = l2cap_pi(sk)->conn;
713 if (conn && l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA) {
Vinicius Costa Gomesa5474a82011-01-26 21:42:57 -0300714 if (!conn->hcon->out) {
715 err = -EINVAL;
716 break;
717 }
718
719 if (smp_conn_security(conn, sec.level))
720 break;
721
722 err = 0;
723 sk->sk_state = BT_CONFIG;
724 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200725 break;
726
727 case BT_DEFER_SETUP:
728 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
729 err = -EINVAL;
730 break;
731 }
732
733 if (get_user(opt, (u32 __user *) optval)) {
734 err = -EFAULT;
735 break;
736 }
737
738 bt_sk(sk)->defer_setup = opt;
739 break;
740
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700741 case BT_POWER:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
743 && sk->sk_type != SOCK_RAW) {
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700744 err = -EINVAL;
745 break;
746 }
747
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 pwr.force_active = 1;
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700749
750 len = min_t(unsigned int, sizeof(pwr), optlen);
751 if (copy_from_user((char *) &pwr, optval, len)) {
752 err = -EFAULT;
753 break;
754 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755 l2cap_pi(sk)->force_active = pwr.force_active;
756 break;
757
758 case BT_AMP_POLICY:
759 if (get_user(opt, (u32 __user *) optval)) {
760 err = -EFAULT;
761 break;
762 }
763
Mat Martineauf00ffac2011-10-19 15:07:32 -0700764 if ((opt > BT_AMP_POLICY_PREFER_AMP) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 ((l2cap_pi(sk)->mode != L2CAP_MODE_ERTM) &&
766 (l2cap_pi(sk)->mode != L2CAP_MODE_STREAMING))) {
767 err = -EINVAL;
768 break;
769 }
770
771 l2cap_pi(sk)->amp_pref = (u8) opt;
772 BT_DBG("BT_AMP_POLICY now %d", opt);
773
774 if ((sk->sk_state == BT_CONNECTED) &&
Peter Krystadc446d212011-09-20 15:35:50 -0700775 (l2cap_pi(sk)->amp_move_role == L2CAP_AMP_MOVE_NONE))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 l2cap_amp_move_init(sk);
777
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700778 break;
779
Srinivas Krovvidi10734192011-12-29 07:29:11 +0530780 case BT_FLUSHABLE:
781 if (get_user(opt, (u32 __user *) optval)) {
782 err = -EFAULT;
783 break;
784 }
785 l2cap_pi(sk)->flushable = opt;
786
787 break;
788
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200789 default:
790 err = -ENOPROTOOPT;
791 break;
792 }
793
794 release_sock(sk);
795 return err;
796}
797
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200798static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
799{
800 struct sock *sk = sock->sk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801 struct l2cap_pinfo *pi = l2cap_pi(sk);
802 struct sk_buff *skb;
803 struct sk_buff_head seg_queue;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200804 int err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805 u8 amp_id;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200806
807 BT_DBG("sock %p, sk %p", sock, sk);
808
809 err = sock_error(sk);
810 if (err)
811 return err;
812
813 if (msg->msg_flags & MSG_OOB)
814 return -EOPNOTSUPP;
815
816 lock_sock(sk);
817
818 if (sk->sk_state != BT_CONNECTED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 err = -ENOTCONN;
820 goto done;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200821 }
822
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 /* Connectionless channel */
824 if (sk->sk_type == SOCK_DGRAM) {
825 skb = l2cap_create_connless_pdu(sk, msg, len);
826 if (IS_ERR(skb)) {
827 err = PTR_ERR(skb);
828 } else {
829 l2cap_do_send(sk, skb);
830 err = len;
831 }
832 goto done;
833 }
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200834
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835 switch (pi->mode) {
836 case L2CAP_MODE_BASIC:
837 /* Check outgoing MTU */
838 if (len > pi->omtu) {
839 err = -EMSGSIZE;
840 goto done;
841 }
842
843 /* Create a basic PDU */
844 skb = l2cap_create_basic_pdu(sk, msg, len);
845 if (IS_ERR(skb)) {
846 err = PTR_ERR(skb);
847 goto done;
848 }
849
850 l2cap_do_send(sk, skb);
851 err = len;
852 break;
853
854 case L2CAP_MODE_ERTM:
855 case L2CAP_MODE_STREAMING:
856
857 /* Check outgoing MTU */
858 if (len > pi->omtu) {
859 err = -EMSGSIZE;
860 goto done;
861 }
862
863 __skb_queue_head_init(&seg_queue);
864
865 /* Do segmentation before calling in to the state machine,
866 * since it's possible to block while waiting for memory
867 * allocation.
868 */
869 amp_id = pi->amp_id;
870 err = l2cap_segment_sdu(sk, &seg_queue, msg, len, 0);
871
872 /* The socket lock is released while segmenting, so check
873 * that the socket is still connected
874 */
875 if (sk->sk_state != BT_CONNECTED) {
876 __skb_queue_purge(&seg_queue);
877 err = -ENOTCONN;
878 }
879
880 if (err) {
881 BT_DBG("Error %d, sk_sndbuf %d, sk_wmem_alloc %d",
882 err, sk->sk_sndbuf,
883 atomic_read(&sk->sk_wmem_alloc));
884 break;
885 }
886
887 if (pi->amp_id != amp_id) {
888 /* Channel moved while unlocked. Resegment. */
889 err = l2cap_resegment_queue(sk, &seg_queue);
890
891 if (err)
892 break;
893 }
894
895 if (pi->mode != L2CAP_MODE_STREAMING)
896 err = l2cap_ertm_tx(sk, 0, &seg_queue,
897 L2CAP_ERTM_EVENT_DATA_REQUEST);
898 else
899 err = l2cap_strm_tx(sk, &seg_queue);
900 if (!err)
901 err = len;
902
903 /* If the skbs were not queued for sending, they'll still be in
904 * seg_queue and need to be purged.
905 */
906 __skb_queue_purge(&seg_queue);
907 break;
908
909 default:
910 BT_DBG("bad state %1.1x", pi->mode);
911 err = -EBADFD;
912 }
913
914done:
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200915 release_sock(sk);
916 return err;
917}
918
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200919static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
920{
921 struct sock *sk = sock->sk;
Mat Martineauc0675b82011-07-07 09:39:02 -0700922 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200923
924 lock_sock(sk);
925
926 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927 struct l2cap_conn_rsp rsp;
928 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
929 u8 buf[128];
930
931 if (l2cap_pi(sk)->amp_id) {
932 /* Physical link must be brought up before connection
933 * completes.
934 */
935 amp_accept_physical(conn, l2cap_pi(sk)->amp_id, sk);
936 release_sock(sk);
937 return 0;
938 }
939
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300940 sk->sk_state = BT_CONFIG;
941
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
943 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
944 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
945 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
946 l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident,
947 L2CAP_CONN_RSP, sizeof(rsp), &rsp);
948
949 if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) {
950 release_sock(sk);
951 return 0;
952 }
953
954 l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
955 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
956 l2cap_build_conf_req(sk, buf), buf);
957 l2cap_pi(sk)->num_conf_req++;
958
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200959 release_sock(sk);
960 return 0;
961 }
962
963 release_sock(sk);
964
965 if (sock->type == SOCK_STREAM)
Mat Martineauc0675b82011-07-07 09:39:02 -0700966 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
967 else
968 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200969
Mat Martineau28eb3fa2012-02-09 16:06:12 -0800970 if (err >= 0)
971 l2cap_ertm_recv_done(sk);
Mat Martineauc0675b82011-07-07 09:39:02 -0700972
Mat Martineauc0675b82011-07-07 09:39:02 -0700973 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200974}
975
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200976/* Kill socket (only if zapped and orphan)
977 * Must be called on unlocked socket.
978 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700979void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200980{
981 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
982 return;
983
984 BT_DBG("sk %p state %d", sk, sk->sk_state);
985
986 /* Kill poor orphan */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700987 bt_sock_unlink(&l2cap_sk_list, sk);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200988 sock_set_flag(sk, SOCK_DEAD);
989 sock_put(sk);
990}
991
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992/* Must be called on unlocked socket. */
993static void l2cap_sock_close(struct sock *sk)
994{
995 l2cap_sock_clear_timer(sk);
996 lock_sock(sk);
997 __l2cap_sock_close(sk, ECONNRESET);
998 release_sock(sk);
999 l2cap_sock_kill(sk);
1000}
1001
1002static void l2cap_sock_cleanup_listen(struct sock *parent)
1003{
1004 struct sock *sk;
1005
1006 BT_DBG("parent %p", parent);
1007
1008 /* Close not yet accepted channels */
1009 while ((sk = bt_accept_dequeue(parent, NULL)))
1010 l2cap_sock_close(sk);
1011
1012 parent->sk_state = BT_CLOSED;
1013 sock_set_flag(parent, SOCK_ZAPPED);
1014}
1015
1016void __l2cap_sock_close(struct sock *sk, int reason)
1017{
1018 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
1019
1020 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
1021
1022 switch (sk->sk_state) {
1023 case BT_LISTEN:
1024 l2cap_sock_cleanup_listen(sk);
1025 break;
1026
1027 case BT_CONNECTED:
1028 case BT_CONFIG:
1029 if ((sk->sk_type == SOCK_SEQPACKET ||
1030 sk->sk_type == SOCK_STREAM) &&
1031 conn->hcon->type == ACL_LINK) {
1032 l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
1033 l2cap_send_disconn_req(conn, sk, reason);
1034 } else
1035 l2cap_chan_del(sk, reason);
1036 break;
1037
1038 case BT_CONNECT2:
1039 if ((sk->sk_type == SOCK_SEQPACKET ||
1040 sk->sk_type == SOCK_STREAM) &&
1041 conn->hcon->type == ACL_LINK) {
1042 struct l2cap_conn_rsp rsp;
1043 __u16 result;
1044
1045 if (bt_sk(sk)->defer_setup)
1046 result = L2CAP_CR_SEC_BLOCK;
1047 else
1048 result = L2CAP_CR_BAD_PSM;
1049 sk->sk_state = BT_DISCONN;
1050
1051 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
1052 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
1053 rsp.result = cpu_to_le16(result);
1054 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
1055 l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
1056 L2CAP_CONN_RSP, sizeof(rsp), &rsp);
1057 }
1058
1059 l2cap_chan_del(sk, reason);
1060 break;
1061
1062 case BT_CONNECT:
1063 case BT_DISCONN:
1064 l2cap_chan_del(sk, reason);
1065 break;
1066
1067 default:
1068 sock_set_flag(sk, SOCK_ZAPPED);
1069 break;
1070 }
1071}
1072
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001073static int l2cap_sock_shutdown(struct socket *sock, int how)
1074{
1075 struct sock *sk = sock->sk;
1076 int err = 0;
1077
1078 BT_DBG("sock %p, sk %p", sock, sk);
1079
1080 if (!sk)
1081 return 0;
1082
1083 lock_sock(sk);
1084 if (!sk->sk_shutdown) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085
1086 if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) {
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001087 err = __l2cap_wait_ack(sk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088 l2cap_ertm_shutdown(sk);
1089 }
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001090
1091 sk->sk_shutdown = SHUTDOWN_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 l2cap_sock_clear_timer(sk);
1093 __l2cap_sock_close(sk, 0);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001094
1095 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
1096 err = bt_sock_wait_state(sk, BT_CLOSED,
1097 sk->sk_lingertime);
1098 }
1099
1100 if (!err && sk->sk_err)
1101 err = -sk->sk_err;
1102
1103 release_sock(sk);
1104 return err;
1105}
1106
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -02001107static int l2cap_sock_release(struct socket *sock)
1108{
1109 struct sock *sk = sock->sk;
Brian Gix20de7cf2012-02-02 14:56:51 -08001110 struct sock *srv_sk = NULL;
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -02001111 int err;
1112
1113 BT_DBG("sock %p, sk %p", sock, sk);
1114
1115 if (!sk)
1116 return 0;
1117
Brian Gix20de7cf2012-02-02 14:56:51 -08001118 /* If this is an ATT Client socket, find the matching Server */
1119 if (l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA && !l2cap_pi(sk)->incoming)
1120 srv_sk = l2cap_find_sock_by_fixed_cid_and_dir(L2CAP_CID_LE_DATA,
1121 &bt_sk(sk)->src, &bt_sk(sk)->dst, 1);
1122
1123 /* If server socket found, request tear down */
1124 BT_DBG("client:%p server:%p", sk, srv_sk);
1125 if (srv_sk)
1126 l2cap_sock_set_timer(srv_sk, 1);
1127
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -02001128 err = l2cap_sock_shutdown(sock, 2);
1129
1130 sock_orphan(sk);
1131 l2cap_sock_kill(sk);
1132 return err;
1133}
1134
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001135static void l2cap_sock_destruct(struct sock *sk)
1136{
1137 BT_DBG("sk %p", sk);
1138
1139 skb_queue_purge(&sk->sk_receive_queue);
1140 skb_queue_purge(&sk->sk_write_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001141
1142 l2cap_ertm_destruct(sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001143}
1144
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001145static void set_default_config(struct l2cap_conf_prm *conf_prm)
1146{
1147 conf_prm->fcs = L2CAP_FCS_CRC16;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148 conf_prm->flush_to = L2CAP_DEFAULT_FLUSH_TO;
1149}
1150
1151void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001152{
1153 struct l2cap_pinfo *pi = l2cap_pi(sk);
1154
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001155 BT_DBG("sk %p parent %p", sk, parent);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001156
1157 if (parent) {
1158 sk->sk_type = parent->sk_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159 sk->sk_rcvbuf = parent->sk_rcvbuf;
1160 sk->sk_sndbuf = parent->sk_sndbuf;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001161 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
1162
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001163 pi->imtu = l2cap_pi(parent)->imtu;
1164 pi->omtu = l2cap_pi(parent)->omtu;
1165 pi->conf_state = l2cap_pi(parent)->conf_state;
1166 pi->mode = l2cap_pi(parent)->mode;
1167 pi->fcs = l2cap_pi(parent)->fcs;
1168 pi->max_tx = l2cap_pi(parent)->max_tx;
1169 pi->tx_win = l2cap_pi(parent)->tx_win;
1170 pi->sec_level = l2cap_pi(parent)->sec_level;
1171 pi->role_switch = l2cap_pi(parent)->role_switch;
1172 pi->force_reliable = l2cap_pi(parent)->force_reliable;
1173 pi->flushable = l2cap_pi(parent)->flushable;
1174 pi->force_active = l2cap_pi(parent)->force_active;
1175 pi->amp_pref = l2cap_pi(parent)->amp_pref;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001176 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 pi->imtu = L2CAP_DEFAULT_MTU;
1178 pi->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001179 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001180 pi->mode = L2CAP_MODE_ERTM;
1181 pi->conf_state |= L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001182 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183 pi->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001184 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001185 pi->reconf_state = L2CAP_RECONF_NONE;
1186 pi->max_tx = L2CAP_DEFAULT_MAX_TX;
1187 pi->fcs = L2CAP_FCS_CRC16;
1188 pi->tx_win = L2CAP_DEFAULT_TX_WINDOW;
1189 pi->sec_level = BT_SECURITY_LOW;
1190 pi->role_switch = 0;
1191 pi->force_reliable = 0;
1192 pi->flushable = 0;
1193 pi->force_active = 1;
1194 pi->amp_pref = BT_AMP_POLICY_REQUIRE_BR_EDR;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001195 }
1196
1197 /* Default config options */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198 sk->sk_backlog_rcv = l2cap_data_channel;
1199 pi->ampcon = NULL;
1200 pi->ampchan = NULL;
1201 pi->conf_len = 0;
1202 pi->flush_to = L2CAP_DEFAULT_FLUSH_TO;
1203 pi->scid = 0;
1204 pi->dcid = 0;
1205 pi->tx_win_max = L2CAP_TX_WIN_MAX_ENHANCED;
1206 pi->extended_control = 0;
Gustavo F. Padovanb090c4b2011-05-16 17:24:37 -03001207
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208 pi->local_conf.fcs = pi->fcs;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 pi->local_conf.flush_to = pi->flush_to;
1210
1211 set_default_config(&pi->remote_conf);
1212
1213 skb_queue_head_init(TX_QUEUE(sk));
1214 skb_queue_head_init(SREJ_QUEUE(sk));
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001215}
1216
1217static struct proto l2cap_proto = {
1218 .name = "L2CAP",
1219 .owner = THIS_MODULE,
1220 .obj_size = sizeof(struct l2cap_pinfo)
1221};
1222
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001224{
1225 struct sock *sk;
1226
1227 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1228 if (!sk)
1229 return NULL;
1230
1231 sock_init_data(sock, sk);
1232 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1233
1234 sk->sk_destruct = l2cap_sock_destruct;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001235 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001236
1237 sock_reset_flag(sk, SOCK_ZAPPED);
1238
1239 sk->sk_protocol = proto;
1240 sk->sk_state = BT_OPEN;
1241
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242 setup_timer(&sk->sk_timer, l2cap_sock_timeout, (unsigned long) sk);
Gustavo F. Padovanb87bf5b2011-05-16 16:42:01 -03001243
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001244 bt_sock_link(&l2cap_sk_list, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001245 return sk;
1246}
1247
1248static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1249 int kern)
1250{
1251 struct sock *sk;
1252
1253 BT_DBG("sock %p", sock);
1254
1255 sock->state = SS_UNCONNECTED;
1256
1257 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1258 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1259 return -ESOCKTNOSUPPORT;
1260
1261 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1262 return -EPERM;
1263
1264 sock->ops = &l2cap_sock_ops;
1265
1266 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1267 if (!sk)
1268 return -ENOMEM;
1269
1270 l2cap_sock_init(sk, NULL);
1271 return 0;
1272}
1273
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001274const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001275 .family = PF_BLUETOOTH,
1276 .owner = THIS_MODULE,
1277 .release = l2cap_sock_release,
1278 .bind = l2cap_sock_bind,
1279 .connect = l2cap_sock_connect,
1280 .listen = l2cap_sock_listen,
1281 .accept = l2cap_sock_accept,
1282 .getname = l2cap_sock_getname,
1283 .sendmsg = l2cap_sock_sendmsg,
1284 .recvmsg = l2cap_sock_recvmsg,
1285 .poll = bt_sock_poll,
1286 .ioctl = bt_sock_ioctl,
1287 .mmap = sock_no_mmap,
1288 .socketpair = sock_no_socketpair,
1289 .shutdown = l2cap_sock_shutdown,
1290 .setsockopt = l2cap_sock_setsockopt,
1291 .getsockopt = l2cap_sock_getsockopt
1292};
1293
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001294static const struct net_proto_family l2cap_sock_family_ops = {
1295 .family = PF_BLUETOOTH,
1296 .owner = THIS_MODULE,
1297 .create = l2cap_sock_create,
1298};
1299
1300int __init l2cap_init_sockets(void)
1301{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001302 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001303
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001304 err = proto_register(&l2cap_proto, 0);
1305 if (err < 0)
1306 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001307
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001308 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1309 if (err < 0)
1310 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001311
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001312 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001313
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001314 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001315
1316error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001317 BT_ERR("L2CAP socket registration failed");
1318 proto_unregister(&l2cap_proto);
1319 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001320}
1321
1322void l2cap_cleanup_sockets(void)
1323{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001324 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1325 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001326
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001327 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001328}