blob: 6a4a2f0eab7e6be11e616b6011ba09481057678b [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
Vinicius Costa Gomes89daa342011-07-08 18:31:46 -0300508 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;
Vinicius Costa Gomes89daa342011-07-08 18:31:46 -0300510
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200511 len = min_t(unsigned int, len, sizeof(sec));
512 if (copy_to_user(optval, (char *) &sec, len))
513 err = -EFAULT;
514
515 break;
516
517 case BT_DEFER_SETUP:
518 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
519 err = -EINVAL;
520 break;
521 }
522
523 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
524 err = -EFAULT;
525
526 break;
527
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700528 case BT_POWER:
529 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
530 && sk->sk_type != SOCK_RAW) {
531 err = -EINVAL;
532 break;
533 }
534
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 pwr.force_active = l2cap_pi(sk)->force_active;
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700536
537 len = min_t(unsigned int, len, sizeof(pwr));
538 if (copy_to_user(optval, (char *) &pwr, len))
539 err = -EFAULT;
540
541 break;
542
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 case BT_AMP_POLICY:
544 if (put_user(l2cap_pi(sk)->amp_pref, (u32 __user *) optval))
545 err = -EFAULT;
546 break;
547
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200548 default:
549 err = -ENOPROTOOPT;
550 break;
551 }
552
553 release_sock(sk);
554 return err;
555}
556
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200557static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
558{
559 struct sock *sk = sock->sk;
560 struct l2cap_options opts;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 int len, le_sock, err = 0;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200562 u32 opt;
563
564 BT_DBG("sk %p", sk);
565
566 lock_sock(sk);
567
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568 le_sock = l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA;
569
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200570 switch (optname) {
571 case L2CAP_OPTIONS:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572 if (sk->sk_state == BT_CONNECTED && !le_sock) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200573 err = -EINVAL;
574 break;
575 }
576
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577 opts.imtu = l2cap_pi(sk)->imtu;
578 opts.omtu = l2cap_pi(sk)->omtu;
579 opts.flush_to = l2cap_pi(sk)->flush_to;
580 opts.mode = l2cap_pi(sk)->mode;
581 opts.fcs = l2cap_pi(sk)->fcs;
582 opts.max_tx = l2cap_pi(sk)->max_tx;
583 opts.txwin_size = l2cap_pi(sk)->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200584
585 len = min_t(unsigned int, sizeof(opts), optlen);
586 if (copy_from_user((char *) &opts, optval, len)) {
587 err = -EFAULT;
588 break;
589 }
590
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 if ((opts.imtu || opts.omtu) && le_sock &&
592 (sk->sk_state == BT_CONNECTED)) {
593 if (opts.imtu >= L2CAP_LE_DEFAULT_MTU)
594 l2cap_pi(sk)->imtu = opts.imtu;
595 if (opts.omtu >= L2CAP_LE_DEFAULT_MTU)
596 l2cap_pi(sk)->omtu = opts.omtu;
597 if (opts.imtu < L2CAP_LE_DEFAULT_MTU ||
598 opts.omtu < L2CAP_LE_DEFAULT_MTU)
599 err = -EINVAL;
600 break;
601 }
602
603 if (opts.txwin_size < 1 ||
604 opts.txwin_size > L2CAP_TX_WIN_MAX_EXTENDED) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200605 err = -EINVAL;
606 break;
607 }
608
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609 l2cap_pi(sk)->mode = opts.mode;
610 switch (l2cap_pi(sk)->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200611 case L2CAP_MODE_BASIC:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612 l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200613 break;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200614 case L2CAP_MODE_STREAMING:
Mat Martineau0ac14362012-02-03 16:24:42 -0800615 if (!disable_ertm) {
616 /* No fallback to ERTM or Basic mode */
617 l2cap_pi(sk)->conf_state |=
618 L2CAP_CONF_STATE2_DEVICE;
619 break;
620 }
621 err = -EINVAL;
622 break;
623 case L2CAP_MODE_ERTM:
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200624 if (!disable_ertm)
625 break;
626 /* fall through */
627 default:
628 err = -EINVAL;
629 break;
630 }
631
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 l2cap_pi(sk)->imtu = opts.imtu;
633 l2cap_pi(sk)->omtu = opts.omtu;
634 l2cap_pi(sk)->fcs = opts.fcs;
635 l2cap_pi(sk)->max_tx = opts.max_tx;
636 l2cap_pi(sk)->tx_win = opts.txwin_size;
Srinivas Krovvidi10734192011-12-29 07:29:11 +0530637 l2cap_pi(sk)->flush_to = opts.flush_to;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200638 break;
639
640 case L2CAP_LM:
641 if (get_user(opt, (u32 __user *) optval)) {
642 err = -EFAULT;
643 break;
644 }
645
646 if (opt & L2CAP_LM_AUTH)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647 l2cap_pi(sk)->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200648 if (opt & L2CAP_LM_ENCRYPT)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 l2cap_pi(sk)->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200650 if (opt & L2CAP_LM_SECURE)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 l2cap_pi(sk)->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200652
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 l2cap_pi(sk)->role_switch = (opt & L2CAP_LM_MASTER);
654 l2cap_pi(sk)->force_reliable = (opt & L2CAP_LM_RELIABLE);
655 l2cap_pi(sk)->flushable = (opt & L2CAP_LM_FLUSHABLE);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200656 break;
657
658 default:
659 err = -ENOPROTOOPT;
660 break;
661 }
662
663 release_sock(sk);
664 return err;
665}
666
667static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
668{
669 struct sock *sk = sock->sk;
670 struct bt_security sec;
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700671 struct bt_power pwr;
Vinicius Costa Gomesa5474a82011-01-26 21:42:57 -0300672 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200673 int len, err = 0;
674 u32 opt;
675
676 BT_DBG("sk %p", sk);
677
678 if (level == SOL_L2CAP)
679 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
680
681 if (level != SOL_BLUETOOTH)
682 return -ENOPROTOOPT;
683
684 lock_sock(sk);
685
686 switch (optname) {
687 case BT_SECURITY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700688 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
689 && sk->sk_type != SOCK_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200690 err = -EINVAL;
691 break;
692 }
693
694 sec.level = BT_SECURITY_LOW;
695
696 len = min_t(unsigned int, sizeof(sec), optlen);
697 if (copy_from_user((char *) &sec, optval, len)) {
698 err = -EFAULT;
699 break;
700 }
701
702 if (sec.level < BT_SECURITY_LOW ||
703 sec.level > BT_SECURITY_HIGH) {
704 err = -EINVAL;
705 break;
706 }
707
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708 l2cap_pi(sk)->sec_level = sec.level;
Vinicius Costa Gomesa5474a82011-01-26 21:42:57 -0300709
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 conn = l2cap_pi(sk)->conn;
711 if (conn && l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA) {
Vinicius Costa Gomesa5474a82011-01-26 21:42:57 -0300712 if (!conn->hcon->out) {
713 err = -EINVAL;
714 break;
715 }
716
717 if (smp_conn_security(conn, sec.level))
718 break;
719
720 err = 0;
721 sk->sk_state = BT_CONFIG;
722 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200723 break;
724
725 case BT_DEFER_SETUP:
726 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
727 err = -EINVAL;
728 break;
729 }
730
731 if (get_user(opt, (u32 __user *) optval)) {
732 err = -EFAULT;
733 break;
734 }
735
736 bt_sk(sk)->defer_setup = opt;
737 break;
738
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700739 case BT_POWER:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
741 && sk->sk_type != SOCK_RAW) {
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700742 err = -EINVAL;
743 break;
744 }
745
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746 pwr.force_active = 1;
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700747
748 len = min_t(unsigned int, sizeof(pwr), optlen);
749 if (copy_from_user((char *) &pwr, optval, len)) {
750 err = -EFAULT;
751 break;
752 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 l2cap_pi(sk)->force_active = pwr.force_active;
754 break;
755
756 case BT_AMP_POLICY:
757 if (get_user(opt, (u32 __user *) optval)) {
758 err = -EFAULT;
759 break;
760 }
761
Mat Martineauf00ffac2011-10-19 15:07:32 -0700762 if ((opt > BT_AMP_POLICY_PREFER_AMP) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763 ((l2cap_pi(sk)->mode != L2CAP_MODE_ERTM) &&
764 (l2cap_pi(sk)->mode != L2CAP_MODE_STREAMING))) {
765 err = -EINVAL;
766 break;
767 }
768
769 l2cap_pi(sk)->amp_pref = (u8) opt;
770 BT_DBG("BT_AMP_POLICY now %d", opt);
771
772 if ((sk->sk_state == BT_CONNECTED) &&
Peter Krystadc446d212011-09-20 15:35:50 -0700773 (l2cap_pi(sk)->amp_move_role == L2CAP_AMP_MOVE_NONE))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 l2cap_amp_move_init(sk);
775
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700776 break;
777
Srinivas Krovvidi10734192011-12-29 07:29:11 +0530778 case BT_FLUSHABLE:
779 if (get_user(opt, (u32 __user *) optval)) {
780 err = -EFAULT;
781 break;
782 }
783 l2cap_pi(sk)->flushable = opt;
784
785 break;
786
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200787 default:
788 err = -ENOPROTOOPT;
789 break;
790 }
791
792 release_sock(sk);
793 return err;
794}
795
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200796static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
797{
798 struct sock *sk = sock->sk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799 struct l2cap_pinfo *pi = l2cap_pi(sk);
800 struct sk_buff *skb;
801 struct sk_buff_head seg_queue;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200802 int err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803 u8 amp_id;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200804
805 BT_DBG("sock %p, sk %p", sock, sk);
806
807 err = sock_error(sk);
808 if (err)
809 return err;
810
811 if (msg->msg_flags & MSG_OOB)
812 return -EOPNOTSUPP;
813
814 lock_sock(sk);
815
816 if (sk->sk_state != BT_CONNECTED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817 err = -ENOTCONN;
818 goto done;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200819 }
820
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700821 /* Connectionless channel */
822 if (sk->sk_type == SOCK_DGRAM) {
823 skb = l2cap_create_connless_pdu(sk, msg, len);
824 if (IS_ERR(skb)) {
825 err = PTR_ERR(skb);
826 } else {
827 l2cap_do_send(sk, skb);
828 err = len;
829 }
830 goto done;
831 }
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200832
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 switch (pi->mode) {
834 case L2CAP_MODE_BASIC:
835 /* Check outgoing MTU */
836 if (len > pi->omtu) {
837 err = -EMSGSIZE;
838 goto done;
839 }
840
841 /* Create a basic PDU */
842 skb = l2cap_create_basic_pdu(sk, msg, len);
843 if (IS_ERR(skb)) {
844 err = PTR_ERR(skb);
845 goto done;
846 }
847
848 l2cap_do_send(sk, skb);
849 err = len;
850 break;
851
852 case L2CAP_MODE_ERTM:
853 case L2CAP_MODE_STREAMING:
854
855 /* Check outgoing MTU */
856 if (len > pi->omtu) {
857 err = -EMSGSIZE;
858 goto done;
859 }
860
861 __skb_queue_head_init(&seg_queue);
862
863 /* Do segmentation before calling in to the state machine,
864 * since it's possible to block while waiting for memory
865 * allocation.
866 */
867 amp_id = pi->amp_id;
868 err = l2cap_segment_sdu(sk, &seg_queue, msg, len, 0);
869
870 /* The socket lock is released while segmenting, so check
871 * that the socket is still connected
872 */
873 if (sk->sk_state != BT_CONNECTED) {
874 __skb_queue_purge(&seg_queue);
875 err = -ENOTCONN;
876 }
877
878 if (err) {
879 BT_DBG("Error %d, sk_sndbuf %d, sk_wmem_alloc %d",
880 err, sk->sk_sndbuf,
881 atomic_read(&sk->sk_wmem_alloc));
882 break;
883 }
884
885 if (pi->amp_id != amp_id) {
886 /* Channel moved while unlocked. Resegment. */
887 err = l2cap_resegment_queue(sk, &seg_queue);
888
889 if (err)
890 break;
891 }
892
893 if (pi->mode != L2CAP_MODE_STREAMING)
894 err = l2cap_ertm_tx(sk, 0, &seg_queue,
895 L2CAP_ERTM_EVENT_DATA_REQUEST);
896 else
897 err = l2cap_strm_tx(sk, &seg_queue);
898 if (!err)
899 err = len;
900
901 /* If the skbs were not queued for sending, they'll still be in
902 * seg_queue and need to be purged.
903 */
904 __skb_queue_purge(&seg_queue);
905 break;
906
907 default:
908 BT_DBG("bad state %1.1x", pi->mode);
909 err = -EBADFD;
910 }
911
912done:
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200913 release_sock(sk);
914 return err;
915}
916
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200917static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
918{
919 struct sock *sk = sock->sk;
Mat Martineauc0675b82011-07-07 09:39:02 -0700920 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200921
922 lock_sock(sk);
923
924 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 struct l2cap_conn_rsp rsp;
926 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
927 u8 buf[128];
928
929 if (l2cap_pi(sk)->amp_id) {
930 /* Physical link must be brought up before connection
931 * completes.
932 */
933 amp_accept_physical(conn, l2cap_pi(sk)->amp_id, sk);
934 release_sock(sk);
935 return 0;
936 }
937
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300938 sk->sk_state = BT_CONFIG;
939
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
941 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
942 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
943 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
944 l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident,
945 L2CAP_CONN_RSP, sizeof(rsp), &rsp);
946
947 if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) {
948 release_sock(sk);
949 return 0;
950 }
951
952 l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
953 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
954 l2cap_build_conf_req(sk, buf), buf);
955 l2cap_pi(sk)->num_conf_req++;
956
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200957 release_sock(sk);
958 return 0;
959 }
960
961 release_sock(sk);
962
963 if (sock->type == SOCK_STREAM)
Mat Martineauc0675b82011-07-07 09:39:02 -0700964 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
965 else
966 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200967
Mat Martineau28eb3fa2012-02-09 16:06:12 -0800968 if (err >= 0)
969 l2cap_ertm_recv_done(sk);
Mat Martineauc0675b82011-07-07 09:39:02 -0700970
Mat Martineauc0675b82011-07-07 09:39:02 -0700971 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200972}
973
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200974/* Kill socket (only if zapped and orphan)
975 * Must be called on unlocked socket.
976 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700977void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200978{
979 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
980 return;
981
982 BT_DBG("sk %p state %d", sk, sk->sk_state);
983
984 /* Kill poor orphan */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985 bt_sock_unlink(&l2cap_sk_list, sk);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200986 sock_set_flag(sk, SOCK_DEAD);
987 sock_put(sk);
988}
989
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990/* Must be called on unlocked socket. */
991static void l2cap_sock_close(struct sock *sk)
992{
993 l2cap_sock_clear_timer(sk);
994 lock_sock(sk);
995 __l2cap_sock_close(sk, ECONNRESET);
996 release_sock(sk);
997 l2cap_sock_kill(sk);
998}
999
1000static void l2cap_sock_cleanup_listen(struct sock *parent)
1001{
1002 struct sock *sk;
1003
1004 BT_DBG("parent %p", parent);
1005
1006 /* Close not yet accepted channels */
1007 while ((sk = bt_accept_dequeue(parent, NULL)))
1008 l2cap_sock_close(sk);
1009
1010 parent->sk_state = BT_CLOSED;
1011 sock_set_flag(parent, SOCK_ZAPPED);
1012}
1013
1014void __l2cap_sock_close(struct sock *sk, int reason)
1015{
1016 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
1017
1018 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
1019
1020 switch (sk->sk_state) {
1021 case BT_LISTEN:
1022 l2cap_sock_cleanup_listen(sk);
1023 break;
1024
1025 case BT_CONNECTED:
1026 case BT_CONFIG:
1027 if ((sk->sk_type == SOCK_SEQPACKET ||
1028 sk->sk_type == SOCK_STREAM) &&
1029 conn->hcon->type == ACL_LINK) {
1030 l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
1031 l2cap_send_disconn_req(conn, sk, reason);
1032 } else
1033 l2cap_chan_del(sk, reason);
1034 break;
1035
1036 case BT_CONNECT2:
1037 if ((sk->sk_type == SOCK_SEQPACKET ||
1038 sk->sk_type == SOCK_STREAM) &&
1039 conn->hcon->type == ACL_LINK) {
1040 struct l2cap_conn_rsp rsp;
1041 __u16 result;
1042
1043 if (bt_sk(sk)->defer_setup)
1044 result = L2CAP_CR_SEC_BLOCK;
1045 else
1046 result = L2CAP_CR_BAD_PSM;
1047 sk->sk_state = BT_DISCONN;
1048
1049 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
1050 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
1051 rsp.result = cpu_to_le16(result);
1052 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
1053 l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
1054 L2CAP_CONN_RSP, sizeof(rsp), &rsp);
1055 }
1056
1057 l2cap_chan_del(sk, reason);
1058 break;
1059
1060 case BT_CONNECT:
1061 case BT_DISCONN:
1062 l2cap_chan_del(sk, reason);
1063 break;
1064
1065 default:
1066 sock_set_flag(sk, SOCK_ZAPPED);
1067 break;
1068 }
1069}
1070
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001071static int l2cap_sock_shutdown(struct socket *sock, int how)
1072{
1073 struct sock *sk = sock->sk;
1074 int err = 0;
1075
1076 BT_DBG("sock %p, sk %p", sock, sk);
1077
1078 if (!sk)
1079 return 0;
1080
1081 lock_sock(sk);
1082 if (!sk->sk_shutdown) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083
1084 if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) {
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001085 err = __l2cap_wait_ack(sk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086 l2cap_ertm_shutdown(sk);
1087 }
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001088
1089 sk->sk_shutdown = SHUTDOWN_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 l2cap_sock_clear_timer(sk);
1091 __l2cap_sock_close(sk, 0);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001092
1093 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
1094 err = bt_sock_wait_state(sk, BT_CLOSED,
1095 sk->sk_lingertime);
1096 }
1097
1098 if (!err && sk->sk_err)
1099 err = -sk->sk_err;
1100
1101 release_sock(sk);
1102 return err;
1103}
1104
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -02001105static int l2cap_sock_release(struct socket *sock)
1106{
1107 struct sock *sk = sock->sk;
Brian Gix20de7cf2012-02-02 14:56:51 -08001108 struct sock *srv_sk = NULL;
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -02001109 int err;
1110
1111 BT_DBG("sock %p, sk %p", sock, sk);
1112
1113 if (!sk)
1114 return 0;
1115
Brian Gix20de7cf2012-02-02 14:56:51 -08001116 /* If this is an ATT Client socket, find the matching Server */
1117 if (l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA && !l2cap_pi(sk)->incoming)
1118 srv_sk = l2cap_find_sock_by_fixed_cid_and_dir(L2CAP_CID_LE_DATA,
1119 &bt_sk(sk)->src, &bt_sk(sk)->dst, 1);
1120
1121 /* If server socket found, request tear down */
1122 BT_DBG("client:%p server:%p", sk, srv_sk);
1123 if (srv_sk)
1124 l2cap_sock_set_timer(srv_sk, 1);
1125
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -02001126 err = l2cap_sock_shutdown(sock, 2);
1127
1128 sock_orphan(sk);
1129 l2cap_sock_kill(sk);
1130 return err;
1131}
1132
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001133static void l2cap_sock_destruct(struct sock *sk)
1134{
1135 BT_DBG("sk %p", sk);
1136
1137 skb_queue_purge(&sk->sk_receive_queue);
1138 skb_queue_purge(&sk->sk_write_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001139
1140 l2cap_ertm_destruct(sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001141}
1142
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143static void set_default_config(struct l2cap_conf_prm *conf_prm)
1144{
1145 conf_prm->fcs = L2CAP_FCS_CRC16;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001146 conf_prm->flush_to = L2CAP_DEFAULT_FLUSH_TO;
1147}
1148
1149void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001150{
1151 struct l2cap_pinfo *pi = l2cap_pi(sk);
1152
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153 BT_DBG("sk %p parent %p", sk, parent);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001154
1155 if (parent) {
1156 sk->sk_type = parent->sk_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157 sk->sk_rcvbuf = parent->sk_rcvbuf;
1158 sk->sk_sndbuf = parent->sk_sndbuf;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001159 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
1160
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161 pi->imtu = l2cap_pi(parent)->imtu;
1162 pi->omtu = l2cap_pi(parent)->omtu;
1163 pi->conf_state = l2cap_pi(parent)->conf_state;
1164 pi->mode = l2cap_pi(parent)->mode;
1165 pi->fcs = l2cap_pi(parent)->fcs;
1166 pi->max_tx = l2cap_pi(parent)->max_tx;
1167 pi->tx_win = l2cap_pi(parent)->tx_win;
1168 pi->sec_level = l2cap_pi(parent)->sec_level;
1169 pi->role_switch = l2cap_pi(parent)->role_switch;
1170 pi->force_reliable = l2cap_pi(parent)->force_reliable;
1171 pi->flushable = l2cap_pi(parent)->flushable;
1172 pi->force_active = l2cap_pi(parent)->force_active;
1173 pi->amp_pref = l2cap_pi(parent)->amp_pref;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001174 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175 pi->imtu = L2CAP_DEFAULT_MTU;
1176 pi->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001177 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 pi->mode = L2CAP_MODE_ERTM;
1179 pi->conf_state |= L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001180 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001181 pi->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001182 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183 pi->reconf_state = L2CAP_RECONF_NONE;
1184 pi->max_tx = L2CAP_DEFAULT_MAX_TX;
1185 pi->fcs = L2CAP_FCS_CRC16;
1186 pi->tx_win = L2CAP_DEFAULT_TX_WINDOW;
1187 pi->sec_level = BT_SECURITY_LOW;
1188 pi->role_switch = 0;
1189 pi->force_reliable = 0;
1190 pi->flushable = 0;
1191 pi->force_active = 1;
1192 pi->amp_pref = BT_AMP_POLICY_REQUIRE_BR_EDR;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001193 }
1194
1195 /* Default config options */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196 sk->sk_backlog_rcv = l2cap_data_channel;
1197 pi->ampcon = NULL;
1198 pi->ampchan = NULL;
1199 pi->conf_len = 0;
1200 pi->flush_to = L2CAP_DEFAULT_FLUSH_TO;
1201 pi->scid = 0;
1202 pi->dcid = 0;
1203 pi->tx_win_max = L2CAP_TX_WIN_MAX_ENHANCED;
1204 pi->extended_control = 0;
Gustavo F. Padovanb090c4b2011-05-16 17:24:37 -03001205
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001206 pi->local_conf.fcs = pi->fcs;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 pi->local_conf.flush_to = pi->flush_to;
1208
1209 set_default_config(&pi->remote_conf);
1210
1211 skb_queue_head_init(TX_QUEUE(sk));
1212 skb_queue_head_init(SREJ_QUEUE(sk));
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001213}
1214
1215static struct proto l2cap_proto = {
1216 .name = "L2CAP",
1217 .owner = THIS_MODULE,
1218 .obj_size = sizeof(struct l2cap_pinfo)
1219};
1220
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001222{
1223 struct sock *sk;
1224
1225 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1226 if (!sk)
1227 return NULL;
1228
1229 sock_init_data(sock, sk);
1230 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1231
1232 sk->sk_destruct = l2cap_sock_destruct;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001234
1235 sock_reset_flag(sk, SOCK_ZAPPED);
1236
1237 sk->sk_protocol = proto;
1238 sk->sk_state = BT_OPEN;
1239
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001240 setup_timer(&sk->sk_timer, l2cap_sock_timeout, (unsigned long) sk);
Gustavo F. Padovanb87bf5b2011-05-16 16:42:01 -03001241
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242 bt_sock_link(&l2cap_sk_list, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001243 return sk;
1244}
1245
1246static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1247 int kern)
1248{
1249 struct sock *sk;
1250
1251 BT_DBG("sock %p", sock);
1252
1253 sock->state = SS_UNCONNECTED;
1254
1255 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1256 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1257 return -ESOCKTNOSUPPORT;
1258
1259 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1260 return -EPERM;
1261
1262 sock->ops = &l2cap_sock_ops;
1263
1264 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1265 if (!sk)
1266 return -ENOMEM;
1267
1268 l2cap_sock_init(sk, NULL);
1269 return 0;
1270}
1271
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001272const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001273 .family = PF_BLUETOOTH,
1274 .owner = THIS_MODULE,
1275 .release = l2cap_sock_release,
1276 .bind = l2cap_sock_bind,
1277 .connect = l2cap_sock_connect,
1278 .listen = l2cap_sock_listen,
1279 .accept = l2cap_sock_accept,
1280 .getname = l2cap_sock_getname,
1281 .sendmsg = l2cap_sock_sendmsg,
1282 .recvmsg = l2cap_sock_recvmsg,
1283 .poll = bt_sock_poll,
1284 .ioctl = bt_sock_ioctl,
1285 .mmap = sock_no_mmap,
1286 .socketpair = sock_no_socketpair,
1287 .shutdown = l2cap_sock_shutdown,
1288 .setsockopt = l2cap_sock_setsockopt,
1289 .getsockopt = l2cap_sock_getsockopt
1290};
1291
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001292static const struct net_proto_family l2cap_sock_family_ops = {
1293 .family = PF_BLUETOOTH,
1294 .owner = THIS_MODULE,
1295 .create = l2cap_sock_create,
1296};
1297
1298int __init l2cap_init_sockets(void)
1299{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001300 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001301
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001302 err = proto_register(&l2cap_proto, 0);
1303 if (err < 0)
1304 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001305
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001306 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1307 if (err < 0)
1308 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001309
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001310 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001311
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001312 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001313
1314error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001315 BT_ERR("L2CAP socket registration failed");
1316 proto_unregister(&l2cap_proto);
1317 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001318}
1319
1320void l2cap_cleanup_sockets(void)
1321{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001322 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1323 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001324
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001325 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001326}