blob: 761f86866485ca4643c13977e0e7c2e69f19d3f4 [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
Steve Mucklef132c6c2012-06-06 18:30:57 -070029#include <linux/interrupt.h>
30#include <linux/module.h>
Paul Moore6230c9b2011-10-07 09:40:59 +000031
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020032#include <net/bluetooth/bluetooth.h>
Gustavo F. Padovan33575df2011-02-04 02:48:48 -020033#include <net/bluetooth/hci_core.h>
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020034#include <net/bluetooth/l2cap.h>
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -030035#include <net/bluetooth/smp.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <net/bluetooth/amp.h>
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020037
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038/* ---- L2CAP timers ---- */
39static void l2cap_sock_timeout(unsigned long arg)
40{
41 struct sock *sk = (struct sock *) arg;
42 int reason;
43
44 BT_DBG("sock %p state %d", sk, sk->sk_state);
45
46 bh_lock_sock(sk);
47
48 if (sock_owned_by_user(sk)) {
49 /* sk is owned by user. Try again later */
50 l2cap_sock_set_timer(sk, HZ / 5);
51 bh_unlock_sock(sk);
52 sock_put(sk);
53 return;
54 }
55
56 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG)
57 reason = ECONNREFUSED;
58 else if (sk->sk_state == BT_CONNECT &&
59 l2cap_pi(sk)->sec_level != BT_SECURITY_SDP)
60 reason = ECONNREFUSED;
61 else
62 reason = ETIMEDOUT;
63
64 __l2cap_sock_close(sk, reason);
65
66 bh_unlock_sock(sk);
67
68 l2cap_sock_kill(sk);
69 sock_put(sk);
70}
71
72void l2cap_sock_set_timer(struct sock *sk, long timeout)
73{
74 BT_DBG("sk %p state %d timeout %ld", sk, sk->sk_state, timeout);
75 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
76}
77
78void l2cap_sock_clear_timer(struct sock *sk)
79{
80 BT_DBG("sock %p state %d", sk, sk->sk_state);
81 sk_stop_timer(sk, &sk->sk_timer);
82}
83
Brian Gixa94b6122012-02-23 16:07:10 -080084int l2cap_sock_le_params_valid(struct bt_le_params *le_params)
85{
86 if (!le_params || le_params->latency > BT_LE_LATENCY_MAX ||
87 le_params->scan_window > BT_LE_SCAN_WINDOW_MAX ||
88 le_params->scan_interval < BT_LE_SCAN_INTERVAL_MIN ||
89 le_params->scan_window > le_params->scan_interval ||
90 le_params->interval_min < BT_LE_CONN_INTERVAL_MIN ||
91 le_params->interval_max > BT_LE_CONN_INTERVAL_MAX ||
92 le_params->interval_min > le_params->interval_max ||
93 le_params->supervision_timeout < BT_LE_SUP_TO_MIN ||
94 le_params->supervision_timeout > BT_LE_SUP_TO_MAX) {
95 return 0;
96 }
97
98 return 1;
99}
100
Archana Ramachandran95319af2012-08-22 11:46:03 -0700101int l2cap_sock_le_conn_update_params_valid(struct bt_le_params *le_params)
102{
103 if (!le_params || le_params->latency > BT_LE_LATENCY_MAX ||
104 le_params->interval_min < BT_LE_CONN_INTERVAL_MIN ||
105 le_params->interval_max > BT_LE_CONN_INTERVAL_MAX ||
106 le_params->interval_min > le_params->interval_max ||
107 le_params->supervision_timeout < BT_LE_SUP_TO_MIN ||
108 le_params->supervision_timeout > BT_LE_SUP_TO_MAX) {
109 return 0;
110 }
111
112 return 1;
113}
114
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115static struct sock *__l2cap_get_sock_by_addr(__le16 psm, bdaddr_t *src)
116{
117 struct sock *sk;
118 struct hlist_node *node;
119 sk_for_each(sk, node, &l2cap_sk_list.head)
120 if (l2cap_pi(sk)->sport == psm && !bacmp(&bt_sk(sk)->src, src))
121 goto found;
122 sk = NULL;
123found:
124 return sk;
125}
Gustavo F. Padovancf2f90f2011-04-27 18:40:39 -0300126
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200127static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
128{
129 struct sock *sk = sock->sk;
130 struct sockaddr_l2 la;
131 int len, err = 0;
132
133 BT_DBG("sk %p", sk);
134
135 if (!addr || addr->sa_family != AF_BLUETOOTH)
136 return -EINVAL;
137
138 memset(&la, 0, sizeof(la));
139 len = min_t(unsigned int, sizeof(la), alen);
140 memcpy(&la, addr, len);
141
Ville Tervob62f3282011-02-10 22:38:50 -0300142 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200143 return -EINVAL;
144
145 lock_sock(sk);
146
147 if (sk->sk_state != BT_OPEN) {
148 err = -EBADFD;
149 goto done;
150 }
151
152 if (la.l2_psm) {
153 __u16 psm = __le16_to_cpu(la.l2_psm);
154
155 /* PSM must be odd and lsb of upper byte must be 0 */
156 if ((psm & 0x0101) != 0x0001) {
157 err = -EINVAL;
158 goto done;
159 }
160
161 /* Restrict usage of well-known PSMs */
162 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
163 err = -EACCES;
164 goto done;
165 }
166 }
167
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 write_lock_bh(&l2cap_sk_list.lock);
169
170 if (la.l2_psm && __l2cap_get_sock_by_addr(la.l2_psm, &la.l2_bdaddr)) {
171 err = -EADDRINUSE;
172 } else {
173 /* Save source address */
174 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
175 l2cap_pi(sk)->psm = la.l2_psm;
176 l2cap_pi(sk)->sport = la.l2_psm;
177 sk->sk_state = BT_BOUND;
178
179 if (__le16_to_cpu(la.l2_psm) == 0x0001 ||
180 __le16_to_cpu(la.l2_psm) == 0x0003)
181 l2cap_pi(sk)->sec_level = BT_SECURITY_SDP;
182 }
183
Ville Tervob62f3282011-02-10 22:38:50 -0300184 if (la.l2_cid)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185 l2cap_pi(sk)->scid = la.l2_cid;
Ville Tervob62f3282011-02-10 22:38:50 -0300186
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187 write_unlock_bh(&l2cap_sk_list.lock);
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200188
189done:
190 release_sock(sk);
191 return err;
192}
193
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200194static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
195{
196 struct sock *sk = sock->sk;
197 struct sockaddr_l2 la;
198 int len, err = 0;
199
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200 BT_DBG("sk %p type %d mode %d state %d", sk, sk->sk_type,
201 l2cap_pi(sk)->mode, sk->sk_state);
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200202
203 if (!addr || alen < sizeof(addr->sa_family) ||
204 addr->sa_family != AF_BLUETOOTH)
205 return -EINVAL;
206
207 memset(&la, 0, sizeof(la));
208 len = min_t(unsigned int, sizeof(la), alen);
209 memcpy(&la, addr, len);
210
Ville Tervoacd7d372011-02-10 22:38:49 -0300211 if (la.l2_cid && la.l2_psm)
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200212 return -EINVAL;
213
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200214 lock_sock(sk);
215
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 if ((sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM)
217 && !(la.l2_psm || la.l2_cid || l2cap_pi(sk)->fixed_channel)) {
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200218 err = -EINVAL;
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200219 goto done;
220 }
221
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222 switch (l2cap_pi(sk)->mode) {
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200223 case L2CAP_MODE_BASIC:
224 break;
225 case L2CAP_MODE_ERTM:
226 case L2CAP_MODE_STREAMING:
227 if (!disable_ertm)
228 break;
229 /* fall through */
230 default:
231 err = -ENOTSUPP;
232 goto done;
233 }
234
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200235 switch (sk->sk_state) {
236 case BT_CONNECT:
237 case BT_CONNECT2:
238 case BT_CONFIG:
239 /* Already connecting */
240 goto wait;
241
242 case BT_CONNECTED:
243 /* Already connected */
244 err = -EISCONN;
245 goto done;
246
247 case BT_OPEN:
248 case BT_BOUND:
249 /* Can connect */
250 break;
251
252 default:
253 err = -EBADFD;
254 goto done;
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200255 }
256
257 /* PSM must be odd and lsb of upper byte must be 0 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 &&
259 !l2cap_pi(sk)->fixed_channel &&
260 sk->sk_type != SOCK_RAW && !la.l2_cid) {
261 BT_DBG("Bad PSM 0x%x", (int)__le16_to_cpu(la.l2_psm));
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200262 err = -EINVAL;
263 goto done;
264 }
265
266 /* Set destination address and psm */
267 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268 l2cap_pi(sk)->psm = la.l2_psm;
269 l2cap_pi(sk)->dcid = la.l2_cid;
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200270
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271 err = l2cap_do_connect(sk);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200272 if (err)
273 goto done;
274
275wait:
276 err = bt_sock_wait_state(sk, BT_CONNECTED,
277 sock_sndtimeo(sk, flags & O_NONBLOCK));
278done:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 if (err)
280 BT_ERR("failed %d", err);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200281 release_sock(sk);
282 return err;
283}
284
285static int l2cap_sock_listen(struct socket *sock, int backlog)
286{
287 struct sock *sk = sock->sk;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200288 int err = 0;
289
290 BT_DBG("sk %p backlog %d", sk, backlog);
291
292 lock_sock(sk);
293
294 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
295 || sk->sk_state != BT_BOUND) {
296 err = -EBADFD;
297 goto done;
298 }
299
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 switch (l2cap_pi(sk)->mode) {
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200301 case L2CAP_MODE_BASIC:
302 break;
303 case L2CAP_MODE_ERTM:
304 case L2CAP_MODE_STREAMING:
305 if (!disable_ertm)
306 break;
307 /* fall through */
308 default:
309 err = -ENOTSUPP;
310 goto done;
311 }
312
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 if (!l2cap_pi(sk)->psm && !l2cap_pi(sk)->scid) {
314 bdaddr_t *src = &bt_sk(sk)->src;
315 u16 psm;
316
317 err = -EINVAL;
318
319 write_lock_bh(&l2cap_sk_list.lock);
320
321 for (psm = 0x1001; psm < 0x1100; psm += 2)
322 if (!__l2cap_get_sock_by_addr(cpu_to_le16(psm), src)) {
323 l2cap_pi(sk)->psm = cpu_to_le16(psm);
324 l2cap_pi(sk)->sport = cpu_to_le16(psm);
325 err = 0;
326 break;
327 }
328
329 write_unlock_bh(&l2cap_sk_list.lock);
330
331 if (err < 0)
332 goto done;
333 }
334
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200335 sk->sk_max_ack_backlog = backlog;
336 sk->sk_ack_backlog = 0;
337 sk->sk_state = BT_LISTEN;
338
Gustavo F. Padovan65390582011-02-04 02:33:56 -0200339done:
340 release_sock(sk);
341 return err;
342}
343
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200344static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
345{
346 DECLARE_WAITQUEUE(wait, current);
347 struct sock *sk = sock->sk, *nsk;
348 long timeo;
349 int err = 0;
350
351 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
352
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 if (sk->sk_state != BT_LISTEN) {
354 err = -EBADFD;
355 goto done;
356 }
357
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200358 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
359
360 BT_DBG("sk %p timeo %ld", sk, timeo);
361
362 /* Wait for an incoming connection. (wake-one). */
363 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200365 set_current_state(TASK_INTERRUPTIBLE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366 if (!timeo) {
367 err = -EAGAIN;
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200368 break;
369 }
370
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371 release_sock(sk);
372 timeo = schedule_timeout(timeo);
373 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400374
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375 if (sk->sk_state != BT_LISTEN) {
376 err = -EBADFD;
Peter Hurleyf9a3c202011-07-24 00:10:52 -0400377 break;
378 }
379
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200380 if (signal_pending(current)) {
381 err = sock_intr_errno(timeo);
382 break;
383 }
384 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385 set_current_state(TASK_RUNNING);
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200386 remove_wait_queue(sk_sleep(sk), &wait);
387
388 if (err)
389 goto done;
390
391 newsock->state = SS_CONNECTED;
392
393 BT_DBG("new socket %p", nsk);
394
395done:
396 release_sock(sk);
397 return err;
398}
399
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200400static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
401{
402 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
403 struct sock *sk = sock->sk;
404
405 BT_DBG("sock %p, sk %p", sock, sk);
406
407 addr->sa_family = AF_BLUETOOTH;
408 *len = sizeof(struct sockaddr_l2);
409
410 if (peer) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411 la->l2_psm = l2cap_pi(sk)->psm;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200412 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413 la->l2_cid = cpu_to_le16(l2cap_pi(sk)->dcid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200414 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415 la->l2_psm = l2cap_pi(sk)->sport;
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200416 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417 la->l2_cid = cpu_to_le16(l2cap_pi(sk)->scid);
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200418 }
419
420 return 0;
421}
422
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200423static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
424{
425 struct sock *sk = sock->sk;
426 struct l2cap_options opts;
427 struct l2cap_conninfo cinfo;
428 int len, err = 0;
429 u32 opt;
430
431 BT_DBG("sk %p", sk);
432
433 if (get_user(len, optlen))
434 return -EFAULT;
435
436 lock_sock(sk);
437
438 switch (optname) {
439 case L2CAP_OPTIONS:
Vasiliy Kulikove3fb5922011-02-10 20:59:42 +0300440 memset(&opts, 0, sizeof(opts));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441 opts.imtu = l2cap_pi(sk)->imtu;
442 opts.omtu = l2cap_pi(sk)->omtu;
443 opts.flush_to = l2cap_pi(sk)->flush_to;
444 opts.mode = l2cap_pi(sk)->mode;
445 opts.fcs = l2cap_pi(sk)->fcs;
446 opts.max_tx = l2cap_pi(sk)->max_tx;
447 opts.txwin_size = l2cap_pi(sk)->tx_win;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200448
449 len = min_t(unsigned int, len, sizeof(opts));
450 if (copy_to_user(optval, (char *) &opts, len))
451 err = -EFAULT;
452
453 break;
454
455 case L2CAP_LM:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456 switch (l2cap_pi(sk)->sec_level) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200457 case BT_SECURITY_LOW:
458 opt = L2CAP_LM_AUTH;
459 break;
460 case BT_SECURITY_MEDIUM:
461 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
462 break;
463 case BT_SECURITY_HIGH:
464 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
465 L2CAP_LM_SECURE;
466 break;
467 default:
468 opt = 0;
469 break;
470 }
471
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472 if (l2cap_pi(sk)->role_switch)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200473 opt |= L2CAP_LM_MASTER;
474
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 if (l2cap_pi(sk)->force_reliable)
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200476 opt |= L2CAP_LM_RELIABLE;
477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 if (l2cap_pi(sk)->flushable)
479 opt |= L2CAP_LM_FLUSHABLE;
480
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200481 if (put_user(opt, (u32 __user *) optval))
482 err = -EFAULT;
483 break;
484
485 case L2CAP_CONNINFO:
486 if (sk->sk_state != BT_CONNECTED &&
487 !(sk->sk_state == BT_CONNECT2 &&
488 bt_sk(sk)->defer_setup)) {
489 err = -ENOTCONN;
490 break;
491 }
492
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 cinfo.hci_handle = l2cap_pi(sk)->conn->hcon->handle;
494 memcpy(cinfo.dev_class, l2cap_pi(sk)->conn->hcon->dev_class, 3);
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200495
496 len = min_t(unsigned int, len, sizeof(cinfo));
497 if (copy_to_user(optval, (char *) &cinfo, len))
498 err = -EFAULT;
499
500 break;
501
502 default:
503 err = -ENOPROTOOPT;
504 break;
505 }
506
507 release_sock(sk);
508 return err;
509}
510
511static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
512{
513 struct sock *sk = sock->sk;
514 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700515 struct bt_power pwr;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200516 int len, err = 0;
517
518 BT_DBG("sk %p", sk);
519
520 if (level == SOL_L2CAP)
521 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
522
523 if (level != SOL_BLUETOOTH)
524 return -ENOPROTOOPT;
525
526 if (get_user(len, optlen))
527 return -EFAULT;
528
529 lock_sock(sk);
530
531 switch (optname) {
532 case BT_SECURITY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
534 && sk->sk_type != SOCK_RAW) {
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200535 err = -EINVAL;
536 break;
537 }
538
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300539 memset(&sec, 0, sizeof(sec));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540 sec.level = l2cap_pi(sk)->sec_level;
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200541
Brian Gix27a795c2012-04-19 11:05:06 -0700542 if (sk->sk_state == BT_CONNECTED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 sec.key_size = l2cap_pi(sk)->conn->hcon->enc_key_size;
Brian Gix27a795c2012-04-19 11:05:06 -0700544 sec.level = l2cap_pi(sk)->conn->hcon->sec_level;
545 }
Vinicius Costa Gomes8f360112011-07-08 18:31:46 -0300546
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200547 len = min_t(unsigned int, len, sizeof(sec));
548 if (copy_to_user(optval, (char *) &sec, len))
549 err = -EFAULT;
550
551 break;
552
553 case BT_DEFER_SETUP:
554 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
555 err = -EINVAL;
556 break;
557 }
558
559 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
560 err = -EFAULT;
561
562 break;
563
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700564 case BT_POWER:
565 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
566 && sk->sk_type != SOCK_RAW) {
567 err = -EINVAL;
568 break;
569 }
570
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571 pwr.force_active = l2cap_pi(sk)->force_active;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700572
573 len = min_t(unsigned int, len, sizeof(pwr));
574 if (copy_to_user(optval, (char *) &pwr, len))
575 err = -EFAULT;
576
577 break;
578
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579 case BT_AMP_POLICY:
580 if (put_user(l2cap_pi(sk)->amp_pref, (u32 __user *) optval))
581 err = -EFAULT;
582 break;
583
Brian Gixa94b6122012-02-23 16:07:10 -0800584 case BT_LE_PARAMS:
585 if (l2cap_pi(sk)->scid != L2CAP_CID_LE_DATA) {
586 err = -EINVAL;
Mat Martineau2ea66482011-11-02 16:18:30 -0700587 break;
588 }
589
Brian Gixa94b6122012-02-23 16:07:10 -0800590 if (copy_to_user(optval, (char *) &bt_sk(sk)->le_params,
591 sizeof(bt_sk(sk)->le_params)))
Mat Martineau2ea66482011-11-02 16:18:30 -0700592 err = -EFAULT;
593 break;
594
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200595 default:
596 err = -ENOPROTOOPT;
597 break;
598 }
599
600 release_sock(sk);
601 return err;
602}
603
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200604static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
605{
606 struct sock *sk = sock->sk;
607 struct l2cap_options opts;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608 int len, le_sock, err = 0;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200609 u32 opt;
610
611 BT_DBG("sk %p", sk);
612
613 lock_sock(sk);
614
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615 le_sock = l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA;
616
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200617 switch (optname) {
618 case L2CAP_OPTIONS:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619 if (sk->sk_state == BT_CONNECTED && !le_sock) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200620 err = -EINVAL;
621 break;
622 }
623
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624 opts.imtu = l2cap_pi(sk)->imtu;
625 opts.omtu = l2cap_pi(sk)->omtu;
626 opts.flush_to = l2cap_pi(sk)->flush_to;
627 opts.mode = l2cap_pi(sk)->mode;
628 opts.fcs = l2cap_pi(sk)->fcs;
629 opts.max_tx = l2cap_pi(sk)->max_tx;
630 opts.txwin_size = l2cap_pi(sk)->tx_win;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200631
632 len = min_t(unsigned int, sizeof(opts), optlen);
633 if (copy_from_user((char *) &opts, optval, len)) {
634 err = -EFAULT;
635 break;
636 }
637
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638 if ((opts.imtu || opts.omtu) && le_sock &&
639 (sk->sk_state == BT_CONNECTED)) {
640 if (opts.imtu >= L2CAP_LE_DEFAULT_MTU)
641 l2cap_pi(sk)->imtu = opts.imtu;
642 if (opts.omtu >= L2CAP_LE_DEFAULT_MTU)
643 l2cap_pi(sk)->omtu = opts.omtu;
644 if (opts.imtu < L2CAP_LE_DEFAULT_MTU ||
645 opts.omtu < L2CAP_LE_DEFAULT_MTU)
646 err = -EINVAL;
647 break;
648 }
649
650 if (opts.txwin_size < 1 ||
651 opts.txwin_size > L2CAP_TX_WIN_MAX_EXTENDED) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200652 err = -EINVAL;
653 break;
654 }
655
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 l2cap_pi(sk)->mode = opts.mode;
657 switch (l2cap_pi(sk)->mode) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200658 case L2CAP_MODE_BASIC:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659 l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200660 break;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200661 case L2CAP_MODE_STREAMING:
Mat Martineau0ac14362012-02-03 16:24:42 -0800662 if (!disable_ertm) {
663 /* No fallback to ERTM or Basic mode */
664 l2cap_pi(sk)->conf_state |=
665 L2CAP_CONF_STATE2_DEVICE;
666 break;
667 }
668 err = -EINVAL;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200669 break;
670 case L2CAP_MODE_ERTM:
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200671 if (!disable_ertm)
672 break;
673 /* fall through */
674 default:
675 err = -EINVAL;
676 break;
677 }
678
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679 l2cap_pi(sk)->imtu = opts.imtu;
680 l2cap_pi(sk)->omtu = opts.omtu;
681 l2cap_pi(sk)->fcs = opts.fcs;
682 l2cap_pi(sk)->max_tx = opts.max_tx;
683 l2cap_pi(sk)->tx_win = opts.txwin_size;
Srinivas Krovvidi10734192011-12-29 07:29:11 +0530684 l2cap_pi(sk)->flush_to = opts.flush_to;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200685 break;
686
687 case L2CAP_LM:
688 if (get_user(opt, (u32 __user *) optval)) {
689 err = -EFAULT;
690 break;
691 }
692
693 if (opt & L2CAP_LM_AUTH)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694 l2cap_pi(sk)->sec_level = BT_SECURITY_LOW;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200695 if (opt & L2CAP_LM_ENCRYPT)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700696 l2cap_pi(sk)->sec_level = BT_SECURITY_MEDIUM;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200697 if (opt & L2CAP_LM_SECURE)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698 l2cap_pi(sk)->sec_level = BT_SECURITY_HIGH;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200699
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 l2cap_pi(sk)->role_switch = (opt & L2CAP_LM_MASTER);
701 l2cap_pi(sk)->force_reliable = (opt & L2CAP_LM_RELIABLE);
702 l2cap_pi(sk)->flushable = (opt & L2CAP_LM_FLUSHABLE);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200703 break;
704
705 default:
706 err = -ENOPROTOOPT;
707 break;
708 }
709
710 release_sock(sk);
711 return err;
712}
713
714static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
715{
716 struct sock *sk = sock->sk;
717 struct bt_security sec;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700718 struct bt_power pwr;
Brian Gixa94b6122012-02-23 16:07:10 -0800719 struct bt_le_params le_params;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300720 struct l2cap_conn *conn;
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200721 int len, err = 0;
722 u32 opt;
723
724 BT_DBG("sk %p", sk);
725
726 if (level == SOL_L2CAP)
727 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
728
729 if (level != SOL_BLUETOOTH)
730 return -ENOPROTOOPT;
731
732 lock_sock(sk);
733
734 switch (optname) {
735 case BT_SECURITY:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
737 && sk->sk_type != SOCK_RAW) {
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200738 err = -EINVAL;
739 break;
740 }
741
742 sec.level = BT_SECURITY_LOW;
743
744 len = min_t(unsigned int, sizeof(sec), optlen);
745 if (copy_from_user((char *) &sec, optval, len)) {
746 err = -EFAULT;
747 break;
748 }
749
750 if (sec.level < BT_SECURITY_LOW ||
751 sec.level > BT_SECURITY_HIGH) {
752 err = -EINVAL;
753 break;
754 }
755
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756 l2cap_pi(sk)->sec_level = sec.level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300757
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758 conn = l2cap_pi(sk)->conn;
759 if (conn && l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300760 if (!conn->hcon->out) {
761 err = -EINVAL;
762 break;
763 }
764
765 if (smp_conn_security(conn, sec.level))
766 break;
Gustavo F. Padovan0bee1d62011-11-05 19:58:31 -0200767
Vinicius Costa Gomesa5474a82011-01-26 21:42:57 -0300768 err = 0;
769 sk->sk_state = BT_CONFIG;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -0300770 }
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200771 break;
772
773 case BT_DEFER_SETUP:
774 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
775 err = -EINVAL;
776 break;
777 }
778
779 if (get_user(opt, (u32 __user *) optval)) {
780 err = -EFAULT;
781 break;
782 }
783
784 bt_sk(sk)->defer_setup = opt;
785 break;
786
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700787 case BT_POWER:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
789 && sk->sk_type != SOCK_RAW) {
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700790 err = -EINVAL;
791 break;
792 }
793
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794 pwr.force_active = 1;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700795
796 len = min_t(unsigned int, sizeof(pwr), optlen);
797 if (copy_from_user((char *) &pwr, optval, len)) {
798 err = -EFAULT;
799 break;
800 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801 l2cap_pi(sk)->force_active = pwr.force_active;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700802 break;
803
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 case BT_AMP_POLICY:
Mat Martineau2ea66482011-11-02 16:18:30 -0700805 if (get_user(opt, (u32 __user *) optval)) {
806 err = -EFAULT;
807 break;
808 }
809
Mat Martineauf00ffac2011-10-19 15:07:32 -0700810 if ((opt > BT_AMP_POLICY_PREFER_AMP) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 ((l2cap_pi(sk)->mode != L2CAP_MODE_ERTM) &&
812 (l2cap_pi(sk)->mode != L2CAP_MODE_STREAMING))) {
Mat Martineau2ea66482011-11-02 16:18:30 -0700813 err = -EINVAL;
814 break;
815 }
816
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817 l2cap_pi(sk)->amp_pref = (u8) opt;
818 BT_DBG("BT_AMP_POLICY now %d", opt);
819
820 if ((sk->sk_state == BT_CONNECTED) &&
Peter Krystadc446d212011-09-20 15:35:50 -0700821 (l2cap_pi(sk)->amp_move_role == L2CAP_AMP_MOVE_NONE))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822 l2cap_amp_move_init(sk);
823
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700824 break;
825
Srinivas Krovvidi10734192011-12-29 07:29:11 +0530826 case BT_FLUSHABLE:
827 if (get_user(opt, (u32 __user *) optval)) {
828 err = -EFAULT;
829 break;
830 }
831 l2cap_pi(sk)->flushable = opt;
832
833 break;
834
Brian Gixa94b6122012-02-23 16:07:10 -0800835 case BT_LE_PARAMS:
836 if (l2cap_pi(sk)->scid != L2CAP_CID_LE_DATA) {
837 err = -EINVAL;
Mat Martineau2ea66482011-11-02 16:18:30 -0700838 break;
839 }
840
Brian Gixa94b6122012-02-23 16:07:10 -0800841 if (copy_from_user((char *) &le_params, optval,
842 sizeof(struct bt_le_params))) {
843 err = -EFAULT;
844 break;
845 }
846
847 conn = l2cap_pi(sk)->conn;
848 if (!conn || !conn->hcon ||
849 l2cap_pi(sk)->scid != L2CAP_CID_LE_DATA) {
850 memcpy(&bt_sk(sk)->le_params, &le_params,
851 sizeof(le_params));
852 break;
853 }
854
855 if (!conn->hcon->out ||
Archana Ramachandran95319af2012-08-22 11:46:03 -0700856 !l2cap_sock_le_conn_update_params_valid(
857 &le_params)) {
Brian Gixa94b6122012-02-23 16:07:10 -0800858 err = -EINVAL;
859 break;
860 }
861
862 memcpy(&bt_sk(sk)->le_params, &le_params, sizeof(le_params));
863
864 hci_le_conn_update(conn->hcon,
865 le_params.interval_min,
866 le_params.interval_max,
867 le_params.latency,
868 le_params.supervision_timeout);
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200869 break;
870
871 default:
872 err = -ENOPROTOOPT;
873 break;
874 }
875
876 release_sock(sk);
877 return err;
878}
879
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200880static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
881{
882 struct sock *sk = sock->sk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883 struct l2cap_pinfo *pi = l2cap_pi(sk);
884 struct sk_buff *skb;
885 struct sk_buff_head seg_queue;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200886 int err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887 u8 amp_id;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200888
889 BT_DBG("sock %p, sk %p", sock, sk);
890
891 err = sock_error(sk);
892 if (err)
893 return err;
894
895 if (msg->msg_flags & MSG_OOB)
896 return -EOPNOTSUPP;
897
898 lock_sock(sk);
899
900 if (sk->sk_state != BT_CONNECTED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 err = -ENOTCONN;
902 goto done;
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200903 }
904
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905 /* Connectionless channel */
906 if (sk->sk_type == SOCK_DGRAM) {
907 skb = l2cap_create_connless_pdu(sk, msg, len);
908 if (IS_ERR(skb)) {
909 err = PTR_ERR(skb);
910 } else {
911 l2cap_do_send(sk, skb);
912 err = len;
913 }
914 goto done;
915 }
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200916
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917 switch (pi->mode) {
918 case L2CAP_MODE_BASIC:
919 /* Check outgoing MTU */
920 if (len > pi->omtu) {
921 err = -EMSGSIZE;
922 goto done;
923 }
924
925 /* Create a basic PDU */
926 skb = l2cap_create_basic_pdu(sk, msg, len);
927 if (IS_ERR(skb)) {
928 err = PTR_ERR(skb);
929 goto done;
930 }
931
932 l2cap_do_send(sk, skb);
933 err = len;
934 break;
935
936 case L2CAP_MODE_ERTM:
937 case L2CAP_MODE_STREAMING:
938
939 /* Check outgoing MTU */
940 if (len > pi->omtu) {
941 err = -EMSGSIZE;
942 goto done;
943 }
944
945 __skb_queue_head_init(&seg_queue);
946
947 /* Do segmentation before calling in to the state machine,
948 * since it's possible to block while waiting for memory
949 * allocation.
950 */
951 amp_id = pi->amp_id;
952 err = l2cap_segment_sdu(sk, &seg_queue, msg, len, 0);
953
954 /* The socket lock is released while segmenting, so check
955 * that the socket is still connected
956 */
957 if (sk->sk_state != BT_CONNECTED) {
958 __skb_queue_purge(&seg_queue);
959 err = -ENOTCONN;
960 }
961
962 if (err) {
963 BT_DBG("Error %d, sk_sndbuf %d, sk_wmem_alloc %d",
964 err, sk->sk_sndbuf,
965 atomic_read(&sk->sk_wmem_alloc));
966 break;
967 }
968
969 if (pi->amp_id != amp_id) {
970 /* Channel moved while unlocked. Resegment. */
971 err = l2cap_resegment_queue(sk, &seg_queue);
972
973 if (err)
974 break;
975 }
976
977 if (pi->mode != L2CAP_MODE_STREAMING)
978 err = l2cap_ertm_tx(sk, 0, &seg_queue,
979 L2CAP_ERTM_EVENT_DATA_REQUEST);
980 else
981 err = l2cap_strm_tx(sk, &seg_queue);
982 if (!err)
983 err = len;
984
985 /* If the skbs were not queued for sending, they'll still be in
986 * seg_queue and need to be purged.
987 */
988 __skb_queue_purge(&seg_queue);
989 break;
990
991 default:
992 BT_DBG("bad state %1.1x", pi->mode);
993 err = -EBADFD;
994 }
995
996done:
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200997 release_sock(sk);
998 return err;
999}
1000
Gustavo F. Padovan68983252011-02-04 03:02:31 -02001001static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
1002{
1003 struct sock *sk = sock->sk;
Mat Martineaue3281402011-07-07 09:39:02 -07001004 int err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -02001005
1006 lock_sock(sk);
1007
1008 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009 struct l2cap_conn_rsp rsp;
1010 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
1011 u8 buf[128];
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001012
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 if (l2cap_pi(sk)->amp_id) {
1014 /* Physical link must be brought up before connection
1015 * completes.
1016 */
1017 amp_accept_physical(conn, l2cap_pi(sk)->amp_id, sk);
1018 release_sock(sk);
1019 return 0;
1020 }
1021
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001022 sk->sk_state = BT_CONFIG;
1023
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
1025 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
1026 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
1027 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
1028 l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident,
1029 L2CAP_CONN_RSP, sizeof(rsp), &rsp);
1030
1031 if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) {
1032 release_sock(sk);
1033 return 0;
1034 }
1035
1036 l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
1037 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
1038 l2cap_build_conf_req(sk, buf), buf);
1039 l2cap_pi(sk)->num_conf_req++;
1040
Gustavo F. Padovan68983252011-02-04 03:02:31 -02001041 release_sock(sk);
1042 return 0;
1043 }
1044
1045 release_sock(sk);
1046
1047 if (sock->type == SOCK_STREAM)
Mat Martineaue3281402011-07-07 09:39:02 -07001048 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
1049 else
1050 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
Gustavo F. Padovan68983252011-02-04 03:02:31 -02001051
Mat Martineau28eb3fa2012-02-09 16:06:12 -08001052 if (err >= 0)
1053 l2cap_ertm_recv_done(sk);
Mat Martineaue3281402011-07-07 09:39:02 -07001054
Mat Martineaue3281402011-07-07 09:39:02 -07001055 return err;
Gustavo F. Padovan68983252011-02-04 03:02:31 -02001056}
1057
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -02001058/* Kill socket (only if zapped and orphan)
1059 * Must be called on unlocked socket.
1060 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061void l2cap_sock_kill(struct sock *sk)
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -02001062{
1063 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
1064 return;
1065
1066 BT_DBG("sk %p state %d", sk, sk->sk_state);
1067
1068 /* Kill poor orphan */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069 bt_sock_unlink(&l2cap_sk_list, sk);
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -02001070 sock_set_flag(sk, SOCK_DEAD);
1071 sock_put(sk);
1072}
1073
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074/* Must be called on unlocked socket. */
1075static void l2cap_sock_close(struct sock *sk)
1076{
1077 l2cap_sock_clear_timer(sk);
1078 lock_sock(sk);
1079 __l2cap_sock_close(sk, ECONNRESET);
1080 release_sock(sk);
1081 l2cap_sock_kill(sk);
1082}
1083
1084static void l2cap_sock_cleanup_listen(struct sock *parent)
1085{
1086 struct sock *sk;
1087
1088 BT_DBG("parent %p", parent);
1089
1090 /* Close not yet accepted channels */
1091 while ((sk = bt_accept_dequeue(parent, NULL)))
1092 l2cap_sock_close(sk);
1093
1094 parent->sk_state = BT_CLOSED;
1095 sock_set_flag(parent, SOCK_ZAPPED);
1096}
1097
1098void __l2cap_sock_close(struct sock *sk, int reason)
1099{
1100 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
1101
1102 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
1103
1104 switch (sk->sk_state) {
1105 case BT_LISTEN:
1106 l2cap_sock_cleanup_listen(sk);
1107 break;
1108
1109 case BT_CONNECTED:
1110 case BT_CONFIG:
1111 if ((sk->sk_type == SOCK_SEQPACKET ||
1112 sk->sk_type == SOCK_STREAM) &&
1113 conn->hcon->type == ACL_LINK) {
1114 l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
1115 l2cap_send_disconn_req(conn, sk, reason);
1116 } else
1117 l2cap_chan_del(sk, reason);
1118 break;
1119
1120 case BT_CONNECT2:
1121 if ((sk->sk_type == SOCK_SEQPACKET ||
1122 sk->sk_type == SOCK_STREAM) &&
1123 conn->hcon->type == ACL_LINK) {
1124 struct l2cap_conn_rsp rsp;
1125 __u16 result;
1126
1127 if (bt_sk(sk)->defer_setup)
1128 result = L2CAP_CR_SEC_BLOCK;
1129 else
1130 result = L2CAP_CR_BAD_PSM;
1131 sk->sk_state = BT_DISCONN;
1132
1133 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
1134 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
1135 rsp.result = cpu_to_le16(result);
1136 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
1137 l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
1138 L2CAP_CONN_RSP, sizeof(rsp), &rsp);
1139 }
1140
1141 l2cap_chan_del(sk, reason);
1142 break;
1143
1144 case BT_CONNECT:
1145 case BT_DISCONN:
1146 l2cap_chan_del(sk, reason);
1147 break;
1148
1149 default:
1150 sock_set_flag(sk, SOCK_ZAPPED);
1151 break;
1152 }
1153}
1154
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001155static int l2cap_sock_shutdown(struct socket *sock, int how)
1156{
1157 struct sock *sk = sock->sk;
1158 int err = 0;
1159
1160 BT_DBG("sock %p, sk %p", sock, sk);
1161
1162 if (!sk)
1163 return 0;
1164
1165 lock_sock(sk);
1166 if (!sk->sk_shutdown) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167
1168 if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) {
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001169 err = __l2cap_wait_ack(sk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001170 l2cap_ertm_shutdown(sk);
1171 }
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001172
1173 sk->sk_shutdown = SHUTDOWN_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174 l2cap_sock_clear_timer(sk);
1175 __l2cap_sock_close(sk, 0);
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001176
1177 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
1178 err = bt_sock_wait_state(sk, BT_CLOSED,
1179 sk->sk_lingertime);
1180 }
1181
1182 if (!err && sk->sk_err)
1183 err = -sk->sk_err;
1184
1185 release_sock(sk);
1186 return err;
1187}
1188
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001189static int l2cap_sock_release(struct socket *sock)
1190{
1191 struct sock *sk = sock->sk;
Brian Gixbcdb7e72012-06-05 13:35:39 -07001192 struct sock *sk2 = NULL;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001193 int err;
1194
1195 BT_DBG("sock %p, sk %p", sock, sk);
1196
1197 if (!sk)
1198 return 0;
1199
Brian Gixbcdb7e72012-06-05 13:35:39 -07001200 /* If this is an ATT socket, find it's matching server/client */
1201 if (l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA)
1202 sk2 = l2cap_find_sock_by_fixed_cid_and_dir(L2CAP_CID_LE_DATA,
1203 &bt_sk(sk)->src, &bt_sk(sk)->dst,
1204 l2cap_pi(sk)->incoming ? 0 : 1);
Brian Gix20de7cf2012-02-02 14:56:51 -08001205
Brian Gixbcdb7e72012-06-05 13:35:39 -07001206 /* If matching socket found, request tear down */
1207 BT_DBG("sock:%p companion:%p", sk, sk2);
1208 if (sk2)
1209 l2cap_sock_set_timer(sk2, 1);
Brian Gix20de7cf2012-02-02 14:56:51 -08001210
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001211 err = l2cap_sock_shutdown(sock, 2);
1212
1213 sock_orphan(sk);
1214 l2cap_sock_kill(sk);
1215 return err;
1216}
1217
1218static void l2cap_sock_destruct(struct sock *sk)
1219{
1220 BT_DBG("sk %p", sk);
1221
1222 skb_queue_purge(&sk->sk_receive_queue);
1223 skb_queue_purge(&sk->sk_write_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001224
1225 l2cap_ertm_destruct(sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001226}
1227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228static void set_default_config(struct l2cap_conf_prm *conf_prm)
1229{
1230 conf_prm->fcs = L2CAP_FCS_CRC16;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231 conf_prm->flush_to = L2CAP_DEFAULT_FLUSH_TO;
1232}
1233
1234void l2cap_sock_init(struct sock *sk, struct sock *parent)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001235{
1236 struct l2cap_pinfo *pi = l2cap_pi(sk);
1237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001238 BT_DBG("sk %p parent %p", sk, parent);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001239
1240 if (parent) {
1241 sk->sk_type = parent->sk_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242 sk->sk_rcvbuf = parent->sk_rcvbuf;
1243 sk->sk_sndbuf = parent->sk_sndbuf;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001244 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
1245
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246 pi->imtu = l2cap_pi(parent)->imtu;
1247 pi->omtu = l2cap_pi(parent)->omtu;
1248 pi->conf_state = l2cap_pi(parent)->conf_state;
1249 pi->mode = l2cap_pi(parent)->mode;
1250 pi->fcs = l2cap_pi(parent)->fcs;
1251 pi->max_tx = l2cap_pi(parent)->max_tx;
1252 pi->tx_win = l2cap_pi(parent)->tx_win;
1253 pi->sec_level = l2cap_pi(parent)->sec_level;
1254 pi->role_switch = l2cap_pi(parent)->role_switch;
1255 pi->force_reliable = l2cap_pi(parent)->force_reliable;
1256 pi->flushable = l2cap_pi(parent)->flushable;
1257 pi->force_active = l2cap_pi(parent)->force_active;
1258 pi->amp_pref = l2cap_pi(parent)->amp_pref;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001259 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260 pi->imtu = L2CAP_DEFAULT_MTU;
1261 pi->omtu = 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001262 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263 pi->mode = L2CAP_MODE_ERTM;
1264 pi->conf_state |= L2CAP_CONF_STATE2_DEVICE;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001265 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001266 pi->mode = L2CAP_MODE_BASIC;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001267 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001268 pi->reconf_state = L2CAP_RECONF_NONE;
1269 pi->max_tx = L2CAP_DEFAULT_MAX_TX;
1270 pi->fcs = L2CAP_FCS_CRC16;
1271 pi->tx_win = L2CAP_DEFAULT_TX_WINDOW;
1272 pi->sec_level = BT_SECURITY_LOW;
1273 pi->role_switch = 0;
1274 pi->force_reliable = 0;
1275 pi->flushable = 0;
1276 pi->force_active = 1;
1277 pi->amp_pref = BT_AMP_POLICY_REQUIRE_BR_EDR;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001278 }
1279
1280 /* Default config options */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281 sk->sk_backlog_rcv = l2cap_data_channel;
1282 pi->ampcon = NULL;
1283 pi->ampchan = NULL;
1284 pi->conf_len = 0;
1285 pi->flush_to = L2CAP_DEFAULT_FLUSH_TO;
1286 pi->scid = 0;
1287 pi->dcid = 0;
1288 pi->tx_win_max = L2CAP_TX_WIN_MAX_ENHANCED;
Mat Martineauda318242012-06-22 11:36:18 -07001289 pi->ack_win = pi->tx_win;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290 pi->extended_control = 0;
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001291
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001292 pi->local_conf.fcs = pi->fcs;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293 pi->local_conf.flush_to = pi->flush_to;
1294
1295 set_default_config(&pi->remote_conf);
1296
1297 skb_queue_head_init(TX_QUEUE(sk));
1298 skb_queue_head_init(SREJ_QUEUE(sk));
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001299}
1300
1301static struct proto l2cap_proto = {
1302 .name = "L2CAP",
1303 .owner = THIS_MODULE,
1304 .obj_size = sizeof(struct l2cap_pinfo)
1305};
1306
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001307struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001308{
1309 struct sock *sk;
1310
1311 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1312 if (!sk)
1313 return NULL;
1314
1315 sock_init_data(sock, sk);
1316 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1317
1318 sk->sk_destruct = l2cap_sock_destruct;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001319 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001320
1321 sock_reset_flag(sk, SOCK_ZAPPED);
1322
1323 sk->sk_protocol = proto;
1324 sk->sk_state = BT_OPEN;
1325
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001326 setup_timer(&sk->sk_timer, l2cap_sock_timeout, (unsigned long) sk);
Gustavo F. Padovandc50a062011-05-16 16:42:01 -03001327
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001328 bt_sock_link(&l2cap_sk_list, sk);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001329 return sk;
1330}
1331
1332static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1333 int kern)
1334{
1335 struct sock *sk;
1336
1337 BT_DBG("sock %p", sock);
1338
1339 sock->state = SS_UNCONNECTED;
1340
1341 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1342 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1343 return -ESOCKTNOSUPPORT;
1344
1345 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1346 return -EPERM;
1347
1348 sock->ops = &l2cap_sock_ops;
1349
1350 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1351 if (!sk)
1352 return -ENOMEM;
1353
1354 l2cap_sock_init(sk, NULL);
1355 return 0;
1356}
1357
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001358const struct proto_ops l2cap_sock_ops = {
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001359 .family = PF_BLUETOOTH,
1360 .owner = THIS_MODULE,
1361 .release = l2cap_sock_release,
1362 .bind = l2cap_sock_bind,
1363 .connect = l2cap_sock_connect,
1364 .listen = l2cap_sock_listen,
1365 .accept = l2cap_sock_accept,
1366 .getname = l2cap_sock_getname,
1367 .sendmsg = l2cap_sock_sendmsg,
1368 .recvmsg = l2cap_sock_recvmsg,
1369 .poll = bt_sock_poll,
1370 .ioctl = bt_sock_ioctl,
1371 .mmap = sock_no_mmap,
1372 .socketpair = sock_no_socketpair,
1373 .shutdown = l2cap_sock_shutdown,
1374 .setsockopt = l2cap_sock_setsockopt,
1375 .getsockopt = l2cap_sock_getsockopt
1376};
1377
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001378static const struct net_proto_family l2cap_sock_family_ops = {
1379 .family = PF_BLUETOOTH,
1380 .owner = THIS_MODULE,
1381 .create = l2cap_sock_create,
1382};
1383
1384int __init l2cap_init_sockets(void)
1385{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001386 int err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001387
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001388 err = proto_register(&l2cap_proto, 0);
1389 if (err < 0)
1390 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001391
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001392 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1393 if (err < 0)
1394 goto error;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001395
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001396 BT_INFO("L2CAP socket layer initialized");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001397
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001398 return 0;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001399
1400error:
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001401 BT_ERR("L2CAP socket registration failed");
1402 proto_unregister(&l2cap_proto);
1403 return err;
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001404}
1405
1406void l2cap_cleanup_sockets(void)
1407{
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001408 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1409 BT_ERR("L2CAP socket unregistration failed");
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001410
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03001411 proto_unregister(&l2cap_proto);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001412}