blob: 4b4e0201ebbede39744e04f9a92c51c841a426ca [file] [log] [blame]
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4 Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
5 Copyright (C) 2010 Google Inc.
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>
32
Gustavo F. Padovan6ddc0482011-02-04 03:23:31 -020033/* ---- L2CAP timers ---- */
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020034static void l2cap_sock_timeout(unsigned long arg)
35{
36 struct sock *sk = (struct sock *) arg;
37 int reason;
38
39 BT_DBG("sock %p state %d", sk, sk->sk_state);
40
41 bh_lock_sock(sk);
42
43 if (sock_owned_by_user(sk)) {
44 /* sk is owned by user. Try again later */
45 l2cap_sock_set_timer(sk, HZ / 5);
46 bh_unlock_sock(sk);
47 sock_put(sk);
48 return;
49 }
50
51 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG)
52 reason = ECONNREFUSED;
53 else if (sk->sk_state == BT_CONNECT &&
54 l2cap_pi(sk)->sec_level != BT_SECURITY_SDP)
55 reason = ECONNREFUSED;
56 else
57 reason = ETIMEDOUT;
58
59 __l2cap_sock_close(sk, reason);
60
61 bh_unlock_sock(sk);
62
63 l2cap_sock_kill(sk);
64 sock_put(sk);
65}
66
Gustavo F. Padovan6ddc0482011-02-04 03:23:31 -020067void l2cap_sock_set_timer(struct sock *sk, long timeout)
68{
69 BT_DBG("sk %p state %d timeout %ld", sk, sk->sk_state, timeout);
70 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
71}
72
73void l2cap_sock_clear_timer(struct sock *sk)
74{
75 BT_DBG("sock %p state %d", sk, sk->sk_state);
76 sk_stop_timer(sk, &sk->sk_timer);
77}
78
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -020079static struct sock *__l2cap_get_sock_by_addr(__le16 psm, bdaddr_t *src)
80{
81 struct sock *sk;
82 struct hlist_node *node;
83 sk_for_each(sk, node, &l2cap_sk_list.head)
84 if (l2cap_pi(sk)->sport == psm && !bacmp(&bt_sk(sk)->src, src))
85 goto found;
86 sk = NULL;
87found:
88 return sk;
89}
90
91static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
92{
93 struct sock *sk = sock->sk;
94 struct sockaddr_l2 la;
95 int len, err = 0;
96
97 BT_DBG("sk %p", sk);
98
99 if (!addr || addr->sa_family != AF_BLUETOOTH)
100 return -EINVAL;
101
102 memset(&la, 0, sizeof(la));
103 len = min_t(unsigned int, sizeof(la), alen);
104 memcpy(&la, addr, len);
105
106 if (la.l2_cid)
107 return -EINVAL;
108
109 lock_sock(sk);
110
111 if (sk->sk_state != BT_OPEN) {
112 err = -EBADFD;
113 goto done;
114 }
115
116 if (la.l2_psm) {
117 __u16 psm = __le16_to_cpu(la.l2_psm);
118
119 /* PSM must be odd and lsb of upper byte must be 0 */
120 if ((psm & 0x0101) != 0x0001) {
121 err = -EINVAL;
122 goto done;
123 }
124
125 /* Restrict usage of well-known PSMs */
126 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
127 err = -EACCES;
128 goto done;
129 }
130 }
131
132 write_lock_bh(&l2cap_sk_list.lock);
133
134 if (la.l2_psm && __l2cap_get_sock_by_addr(la.l2_psm, &la.l2_bdaddr)) {
135 err = -EADDRINUSE;
136 } else {
137 /* Save source address */
138 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
139 l2cap_pi(sk)->psm = la.l2_psm;
140 l2cap_pi(sk)->sport = la.l2_psm;
141 sk->sk_state = BT_BOUND;
142
143 if (__le16_to_cpu(la.l2_psm) == 0x0001 ||
144 __le16_to_cpu(la.l2_psm) == 0x0003)
145 l2cap_pi(sk)->sec_level = BT_SECURITY_SDP;
146 }
147
148 write_unlock_bh(&l2cap_sk_list.lock);
149
150done:
151 release_sock(sk);
152 return err;
153}
154
Gustavo F. Padovan4e34c502011-02-04 02:56:13 -0200155static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
156{
157 struct sock *sk = sock->sk;
158 struct sockaddr_l2 la;
159 int len, err = 0;
160
161 BT_DBG("sk %p", sk);
162
163 if (!addr || alen < sizeof(addr->sa_family) ||
164 addr->sa_family != AF_BLUETOOTH)
165 return -EINVAL;
166
167 memset(&la, 0, sizeof(la));
168 len = min_t(unsigned int, sizeof(la), alen);
169 memcpy(&la, addr, len);
170
171 if (la.l2_cid)
172 return -EINVAL;
173
174 lock_sock(sk);
175
176 if ((sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM)
177 && !la.l2_psm) {
178 err = -EINVAL;
179 goto done;
180 }
181
182 switch (l2cap_pi(sk)->mode) {
183 case L2CAP_MODE_BASIC:
184 break;
185 case L2CAP_MODE_ERTM:
186 case L2CAP_MODE_STREAMING:
187 if (!disable_ertm)
188 break;
189 /* fall through */
190 default:
191 err = -ENOTSUPP;
192 goto done;
193 }
194
195 switch (sk->sk_state) {
196 case BT_CONNECT:
197 case BT_CONNECT2:
198 case BT_CONFIG:
199 /* Already connecting */
200 goto wait;
201
202 case BT_CONNECTED:
203 /* Already connected */
204 err = -EISCONN;
205 goto done;
206
207 case BT_OPEN:
208 case BT_BOUND:
209 /* Can connect */
210 break;
211
212 default:
213 err = -EBADFD;
214 goto done;
215 }
216
217 /* PSM must be odd and lsb of upper byte must be 0 */
218 if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 &&
219 sk->sk_type != SOCK_RAW) {
220 err = -EINVAL;
221 goto done;
222 }
223
224 /* Set destination address and psm */
225 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
226 l2cap_pi(sk)->psm = la.l2_psm;
227
228 err = l2cap_do_connect(sk);
229 if (err)
230 goto done;
231
232wait:
233 err = bt_sock_wait_state(sk, BT_CONNECTED,
234 sock_sndtimeo(sk, flags & O_NONBLOCK));
235done:
236 release_sock(sk);
237 return err;
238}
239
Gustavo F. Padovanaf6bcd82011-02-04 02:40:28 -0200240static int l2cap_sock_listen(struct socket *sock, int backlog)
241{
242 struct sock *sk = sock->sk;
243 int err = 0;
244
245 BT_DBG("sk %p backlog %d", sk, backlog);
246
247 lock_sock(sk);
248
249 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
250 || sk->sk_state != BT_BOUND) {
251 err = -EBADFD;
252 goto done;
253 }
254
255 switch (l2cap_pi(sk)->mode) {
256 case L2CAP_MODE_BASIC:
257 break;
258 case L2CAP_MODE_ERTM:
259 case L2CAP_MODE_STREAMING:
260 if (!disable_ertm)
261 break;
262 /* fall through */
263 default:
264 err = -ENOTSUPP;
265 goto done;
266 }
267
268 if (!l2cap_pi(sk)->psm) {
269 bdaddr_t *src = &bt_sk(sk)->src;
270 u16 psm;
271
272 err = -EINVAL;
273
274 write_lock_bh(&l2cap_sk_list.lock);
275
276 for (psm = 0x1001; psm < 0x1100; psm += 2)
277 if (!__l2cap_get_sock_by_addr(cpu_to_le16(psm), src)) {
278 l2cap_pi(sk)->psm = cpu_to_le16(psm);
279 l2cap_pi(sk)->sport = cpu_to_le16(psm);
280 err = 0;
281 break;
282 }
283
284 write_unlock_bh(&l2cap_sk_list.lock);
285
286 if (err < 0)
287 goto done;
288 }
289
290 sk->sk_max_ack_backlog = backlog;
291 sk->sk_ack_backlog = 0;
292 sk->sk_state = BT_LISTEN;
293
294done:
295 release_sock(sk);
296 return err;
297}
298
Gustavo F. Padovanc47b7c72011-02-04 02:42:23 -0200299static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
300{
301 DECLARE_WAITQUEUE(wait, current);
302 struct sock *sk = sock->sk, *nsk;
303 long timeo;
304 int err = 0;
305
306 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
307
308 if (sk->sk_state != BT_LISTEN) {
309 err = -EBADFD;
310 goto done;
311 }
312
313 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
314
315 BT_DBG("sk %p timeo %ld", sk, timeo);
316
317 /* Wait for an incoming connection. (wake-one). */
318 add_wait_queue_exclusive(sk_sleep(sk), &wait);
319 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
320 set_current_state(TASK_INTERRUPTIBLE);
321 if (!timeo) {
322 err = -EAGAIN;
323 break;
324 }
325
326 release_sock(sk);
327 timeo = schedule_timeout(timeo);
328 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
329
330 if (sk->sk_state != BT_LISTEN) {
331 err = -EBADFD;
332 break;
333 }
334
335 if (signal_pending(current)) {
336 err = sock_intr_errno(timeo);
337 break;
338 }
339 }
340 set_current_state(TASK_RUNNING);
341 remove_wait_queue(sk_sleep(sk), &wait);
342
343 if (err)
344 goto done;
345
346 newsock->state = SS_CONNECTED;
347
348 BT_DBG("new socket %p", nsk);
349
350done:
351 release_sock(sk);
352 return err;
353}
354
Gustavo F. Padovand7175d52011-02-04 02:43:46 -0200355static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
356{
357 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
358 struct sock *sk = sock->sk;
359
360 BT_DBG("sock %p, sk %p", sock, sk);
361
362 addr->sa_family = AF_BLUETOOTH;
363 *len = sizeof(struct sockaddr_l2);
364
365 if (peer) {
366 la->l2_psm = l2cap_pi(sk)->psm;
367 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
368 la->l2_cid = cpu_to_le16(l2cap_pi(sk)->dcid);
369 } else {
370 la->l2_psm = l2cap_pi(sk)->sport;
371 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
372 la->l2_cid = cpu_to_le16(l2cap_pi(sk)->scid);
373 }
374
375 return 0;
376}
377
Gustavo F. Padovan99f48082011-02-04 02:52:55 -0200378static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
379{
380 struct sock *sk = sock->sk;
381 struct l2cap_options opts;
382 struct l2cap_conninfo cinfo;
383 int len, err = 0;
384 u32 opt;
385
386 BT_DBG("sk %p", sk);
387
388 if (get_user(len, optlen))
389 return -EFAULT;
390
391 lock_sock(sk);
392
393 switch (optname) {
394 case L2CAP_OPTIONS:
395 opts.imtu = l2cap_pi(sk)->imtu;
396 opts.omtu = l2cap_pi(sk)->omtu;
397 opts.flush_to = l2cap_pi(sk)->flush_to;
398 opts.mode = l2cap_pi(sk)->mode;
399 opts.fcs = l2cap_pi(sk)->fcs;
400 opts.max_tx = l2cap_pi(sk)->max_tx;
401 opts.txwin_size = (__u16)l2cap_pi(sk)->tx_win;
402
403 len = min_t(unsigned int, len, sizeof(opts));
404 if (copy_to_user(optval, (char *) &opts, len))
405 err = -EFAULT;
406
407 break;
408
409 case L2CAP_LM:
410 switch (l2cap_pi(sk)->sec_level) {
411 case BT_SECURITY_LOW:
412 opt = L2CAP_LM_AUTH;
413 break;
414 case BT_SECURITY_MEDIUM:
415 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
416 break;
417 case BT_SECURITY_HIGH:
418 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
419 L2CAP_LM_SECURE;
420 break;
421 default:
422 opt = 0;
423 break;
424 }
425
426 if (l2cap_pi(sk)->role_switch)
427 opt |= L2CAP_LM_MASTER;
428
429 if (l2cap_pi(sk)->force_reliable)
430 opt |= L2CAP_LM_RELIABLE;
431
432 if (put_user(opt, (u32 __user *) optval))
433 err = -EFAULT;
434 break;
435
436 case L2CAP_CONNINFO:
437 if (sk->sk_state != BT_CONNECTED &&
438 !(sk->sk_state == BT_CONNECT2 &&
439 bt_sk(sk)->defer_setup)) {
440 err = -ENOTCONN;
441 break;
442 }
443
444 cinfo.hci_handle = l2cap_pi(sk)->conn->hcon->handle;
445 memcpy(cinfo.dev_class, l2cap_pi(sk)->conn->hcon->dev_class, 3);
446
447 len = min_t(unsigned int, len, sizeof(cinfo));
448 if (copy_to_user(optval, (char *) &cinfo, len))
449 err = -EFAULT;
450
451 break;
452
453 default:
454 err = -ENOPROTOOPT;
455 break;
456 }
457
458 release_sock(sk);
459 return err;
460}
461
462static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
463{
464 struct sock *sk = sock->sk;
465 struct bt_security sec;
466 int len, err = 0;
467
468 BT_DBG("sk %p", sk);
469
470 if (level == SOL_L2CAP)
471 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
472
473 if (level != SOL_BLUETOOTH)
474 return -ENOPROTOOPT;
475
476 if (get_user(len, optlen))
477 return -EFAULT;
478
479 lock_sock(sk);
480
481 switch (optname) {
482 case BT_SECURITY:
483 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
484 && sk->sk_type != SOCK_RAW) {
485 err = -EINVAL;
486 break;
487 }
488
489 sec.level = l2cap_pi(sk)->sec_level;
490
491 len = min_t(unsigned int, len, sizeof(sec));
492 if (copy_to_user(optval, (char *) &sec, len))
493 err = -EFAULT;
494
495 break;
496
497 case BT_DEFER_SETUP:
498 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
499 err = -EINVAL;
500 break;
501 }
502
503 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
504 err = -EFAULT;
505
506 break;
507
508 case BT_FLUSHABLE:
509 if (put_user(l2cap_pi(sk)->flushable, (u32 __user *) optval))
510 err = -EFAULT;
511
512 break;
513
514 default:
515 err = -ENOPROTOOPT;
516 break;
517 }
518
519 release_sock(sk);
520 return err;
521}
522
Gustavo F. Padovan33575df2011-02-04 02:48:48 -0200523static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
524{
525 struct sock *sk = sock->sk;
526 struct l2cap_options opts;
527 int len, err = 0;
528 u32 opt;
529
530 BT_DBG("sk %p", sk);
531
532 lock_sock(sk);
533
534 switch (optname) {
535 case L2CAP_OPTIONS:
536 if (sk->sk_state == BT_CONNECTED) {
537 err = -EINVAL;
538 break;
539 }
540
541 opts.imtu = l2cap_pi(sk)->imtu;
542 opts.omtu = l2cap_pi(sk)->omtu;
543 opts.flush_to = l2cap_pi(sk)->flush_to;
544 opts.mode = l2cap_pi(sk)->mode;
545 opts.fcs = l2cap_pi(sk)->fcs;
546 opts.max_tx = l2cap_pi(sk)->max_tx;
547 opts.txwin_size = (__u16)l2cap_pi(sk)->tx_win;
548
549 len = min_t(unsigned int, sizeof(opts), optlen);
550 if (copy_from_user((char *) &opts, optval, len)) {
551 err = -EFAULT;
552 break;
553 }
554
555 if (opts.txwin_size > L2CAP_DEFAULT_TX_WINDOW) {
556 err = -EINVAL;
557 break;
558 }
559
560 l2cap_pi(sk)->mode = opts.mode;
561 switch (l2cap_pi(sk)->mode) {
562 case L2CAP_MODE_BASIC:
563 l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_STATE2_DEVICE;
564 break;
565 case L2CAP_MODE_ERTM:
566 case L2CAP_MODE_STREAMING:
567 if (!disable_ertm)
568 break;
569 /* fall through */
570 default:
571 err = -EINVAL;
572 break;
573 }
574
575 l2cap_pi(sk)->imtu = opts.imtu;
576 l2cap_pi(sk)->omtu = opts.omtu;
577 l2cap_pi(sk)->fcs = opts.fcs;
578 l2cap_pi(sk)->max_tx = opts.max_tx;
579 l2cap_pi(sk)->tx_win = (__u8)opts.txwin_size;
580 break;
581
582 case L2CAP_LM:
583 if (get_user(opt, (u32 __user *) optval)) {
584 err = -EFAULT;
585 break;
586 }
587
588 if (opt & L2CAP_LM_AUTH)
589 l2cap_pi(sk)->sec_level = BT_SECURITY_LOW;
590 if (opt & L2CAP_LM_ENCRYPT)
591 l2cap_pi(sk)->sec_level = BT_SECURITY_MEDIUM;
592 if (opt & L2CAP_LM_SECURE)
593 l2cap_pi(sk)->sec_level = BT_SECURITY_HIGH;
594
595 l2cap_pi(sk)->role_switch = (opt & L2CAP_LM_MASTER);
596 l2cap_pi(sk)->force_reliable = (opt & L2CAP_LM_RELIABLE);
597 break;
598
599 default:
600 err = -ENOPROTOOPT;
601 break;
602 }
603
604 release_sock(sk);
605 return err;
606}
607
608static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
609{
610 struct sock *sk = sock->sk;
611 struct bt_security sec;
612 int len, err = 0;
613 u32 opt;
614
615 BT_DBG("sk %p", sk);
616
617 if (level == SOL_L2CAP)
618 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
619
620 if (level != SOL_BLUETOOTH)
621 return -ENOPROTOOPT;
622
623 lock_sock(sk);
624
625 switch (optname) {
626 case BT_SECURITY:
627 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
628 && sk->sk_type != SOCK_RAW) {
629 err = -EINVAL;
630 break;
631 }
632
633 sec.level = BT_SECURITY_LOW;
634
635 len = min_t(unsigned int, sizeof(sec), optlen);
636 if (copy_from_user((char *) &sec, optval, len)) {
637 err = -EFAULT;
638 break;
639 }
640
641 if (sec.level < BT_SECURITY_LOW ||
642 sec.level > BT_SECURITY_HIGH) {
643 err = -EINVAL;
644 break;
645 }
646
647 l2cap_pi(sk)->sec_level = sec.level;
648 break;
649
650 case BT_DEFER_SETUP:
651 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
652 err = -EINVAL;
653 break;
654 }
655
656 if (get_user(opt, (u32 __user *) optval)) {
657 err = -EFAULT;
658 break;
659 }
660
661 bt_sk(sk)->defer_setup = opt;
662 break;
663
664 case BT_FLUSHABLE:
665 if (get_user(opt, (u32 __user *) optval)) {
666 err = -EFAULT;
667 break;
668 }
669
670 if (opt > BT_FLUSHABLE_ON) {
671 err = -EINVAL;
672 break;
673 }
674
675 if (opt == BT_FLUSHABLE_OFF) {
676 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
677 /* proceed futher only when we have l2cap_conn and
678 No Flush support in the LM */
679 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
680 err = -EINVAL;
681 break;
682 }
683 }
684
685 l2cap_pi(sk)->flushable = opt;
686 break;
687
688 default:
689 err = -ENOPROTOOPT;
690 break;
691 }
692
693 release_sock(sk);
694 return err;
695}
696
Gustavo F. Padovanfd83ccd2011-02-04 03:20:52 -0200697static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
698{
699 struct sock *sk = sock->sk;
700 struct l2cap_pinfo *pi = l2cap_pi(sk);
701 struct sk_buff *skb;
702 u16 control;
703 int err;
704
705 BT_DBG("sock %p, sk %p", sock, sk);
706
707 err = sock_error(sk);
708 if (err)
709 return err;
710
711 if (msg->msg_flags & MSG_OOB)
712 return -EOPNOTSUPP;
713
714 lock_sock(sk);
715
716 if (sk->sk_state != BT_CONNECTED) {
717 err = -ENOTCONN;
718 goto done;
719 }
720
721 /* Connectionless channel */
722 if (sk->sk_type == SOCK_DGRAM) {
723 skb = l2cap_create_connless_pdu(sk, msg, len);
724 if (IS_ERR(skb)) {
725 err = PTR_ERR(skb);
726 } else {
727 l2cap_do_send(sk, skb);
728 err = len;
729 }
730 goto done;
731 }
732
733 switch (pi->mode) {
734 case L2CAP_MODE_BASIC:
735 /* Check outgoing MTU */
736 if (len > pi->omtu) {
737 err = -EMSGSIZE;
738 goto done;
739 }
740
741 /* Create a basic PDU */
742 skb = l2cap_create_basic_pdu(sk, msg, len);
743 if (IS_ERR(skb)) {
744 err = PTR_ERR(skb);
745 goto done;
746 }
747
748 l2cap_do_send(sk, skb);
749 err = len;
750 break;
751
752 case L2CAP_MODE_ERTM:
753 case L2CAP_MODE_STREAMING:
754 /* Entire SDU fits into one PDU */
755 if (len <= pi->remote_mps) {
756 control = L2CAP_SDU_UNSEGMENTED;
757 skb = l2cap_create_iframe_pdu(sk, msg, len, control, 0);
758 if (IS_ERR(skb)) {
759 err = PTR_ERR(skb);
760 goto done;
761 }
762 __skb_queue_tail(TX_QUEUE(sk), skb);
763
764 if (sk->sk_send_head == NULL)
765 sk->sk_send_head = skb;
766
767 } else {
768 /* Segment SDU into multiples PDUs */
769 err = l2cap_sar_segment_sdu(sk, msg, len);
770 if (err < 0)
771 goto done;
772 }
773
774 if (pi->mode == L2CAP_MODE_STREAMING) {
775 l2cap_streaming_send(sk);
776 } else {
777 if ((pi->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
778 (pi->conn_state & L2CAP_CONN_WAIT_F)) {
779 err = len;
780 break;
781 }
782 err = l2cap_ertm_send(sk);
783 }
784
785 if (err >= 0)
786 err = len;
787 break;
788
789 default:
790 BT_DBG("bad state %1.1x", pi->mode);
791 err = -EBADFD;
792 }
793
794done:
795 release_sock(sk);
796 return err;
797}
798
Gustavo F. Padovan68983252011-02-04 03:02:31 -0200799static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
800{
801 struct sock *sk = sock->sk;
802
803 lock_sock(sk);
804
805 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
806 struct l2cap_conn_rsp rsp;
807 struct l2cap_conn *conn = l2cap_pi(sk)->conn;
808 u8 buf[128];
809
810 sk->sk_state = BT_CONFIG;
811
812 rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
813 rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
814 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
815 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
816 l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident,
817 L2CAP_CONN_RSP, sizeof(rsp), &rsp);
818
819 if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) {
820 release_sock(sk);
821 return 0;
822 }
823
824 l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
825 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
826 l2cap_build_conf_req(sk, buf), buf);
827 l2cap_pi(sk)->num_conf_req++;
828
829 release_sock(sk);
830 return 0;
831 }
832
833 release_sock(sk);
834
835 if (sock->type == SOCK_STREAM)
836 return bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
837
838 return bt_sock_recvmsg(iocb, sock, msg, len, flags);
839}
840
Gustavo F. Padovan05fc1572011-02-04 03:26:01 -0200841/* Kill socket (only if zapped and orphan)
842 * Must be called on unlocked socket.
843 */
844void l2cap_sock_kill(struct sock *sk)
845{
846 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
847 return;
848
849 BT_DBG("sk %p state %d", sk, sk->sk_state);
850
851 /* Kill poor orphan */
852 bt_sock_unlink(&l2cap_sk_list, sk);
853 sock_set_flag(sk, SOCK_DEAD);
854 sock_put(sk);
855}
856
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -0200857static int l2cap_sock_shutdown(struct socket *sock, int how)
858{
859 struct sock *sk = sock->sk;
860 int err = 0;
861
862 BT_DBG("sock %p, sk %p", sock, sk);
863
864 if (!sk)
865 return 0;
866
867 lock_sock(sk);
868 if (!sk->sk_shutdown) {
869 if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM)
870 err = __l2cap_wait_ack(sk);
871
872 sk->sk_shutdown = SHUTDOWN_MASK;
873 l2cap_sock_clear_timer(sk);
874 __l2cap_sock_close(sk, 0);
875
876 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
877 err = bt_sock_wait_state(sk, BT_CLOSED,
878 sk->sk_lingertime);
879 }
880
881 if (!err && sk->sk_err)
882 err = -sk->sk_err;
883
884 release_sock(sk);
885 return err;
886}
887
Gustavo F. Padovan554f05b2011-02-04 02:36:42 -0200888static int l2cap_sock_release(struct socket *sock)
889{
890 struct sock *sk = sock->sk;
891 int err;
892
893 BT_DBG("sock %p, sk %p", sock, sk);
894
895 if (!sk)
896 return 0;
897
898 err = l2cap_sock_shutdown(sock, 2);
899
900 sock_orphan(sk);
901 l2cap_sock_kill(sk);
902 return err;
903}
904
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -0200905static void l2cap_sock_destruct(struct sock *sk)
906{
907 BT_DBG("sk %p", sk);
908
909 skb_queue_purge(&sk->sk_receive_queue);
910 skb_queue_purge(&sk->sk_write_queue);
911}
912
913void l2cap_sock_init(struct sock *sk, struct sock *parent)
914{
915 struct l2cap_pinfo *pi = l2cap_pi(sk);
916
917 BT_DBG("sk %p", sk);
918
919 if (parent) {
920 sk->sk_type = parent->sk_type;
921 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
922
923 pi->imtu = l2cap_pi(parent)->imtu;
924 pi->omtu = l2cap_pi(parent)->omtu;
925 pi->conf_state = l2cap_pi(parent)->conf_state;
926 pi->mode = l2cap_pi(parent)->mode;
927 pi->fcs = l2cap_pi(parent)->fcs;
928 pi->max_tx = l2cap_pi(parent)->max_tx;
929 pi->tx_win = l2cap_pi(parent)->tx_win;
930 pi->sec_level = l2cap_pi(parent)->sec_level;
931 pi->role_switch = l2cap_pi(parent)->role_switch;
932 pi->force_reliable = l2cap_pi(parent)->force_reliable;
933 pi->flushable = l2cap_pi(parent)->flushable;
934 } else {
935 pi->imtu = L2CAP_DEFAULT_MTU;
936 pi->omtu = 0;
937 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
938 pi->mode = L2CAP_MODE_ERTM;
939 pi->conf_state |= L2CAP_CONF_STATE2_DEVICE;
940 } else {
941 pi->mode = L2CAP_MODE_BASIC;
942 }
943 pi->max_tx = L2CAP_DEFAULT_MAX_TX;
944 pi->fcs = L2CAP_FCS_CRC16;
945 pi->tx_win = L2CAP_DEFAULT_TX_WINDOW;
946 pi->sec_level = BT_SECURITY_LOW;
947 pi->role_switch = 0;
948 pi->force_reliable = 0;
949 pi->flushable = BT_FLUSHABLE_OFF;
950 }
951
952 /* Default config options */
953 pi->conf_len = 0;
954 pi->flush_to = L2CAP_DEFAULT_FLUSH_TO;
955 skb_queue_head_init(TX_QUEUE(sk));
956 skb_queue_head_init(SREJ_QUEUE(sk));
957 skb_queue_head_init(BUSY_QUEUE(sk));
958 INIT_LIST_HEAD(SREJ_LIST(sk));
959}
960
961static struct proto l2cap_proto = {
962 .name = "L2CAP",
963 .owner = THIS_MODULE,
964 .obj_size = sizeof(struct l2cap_pinfo)
965};
966
967struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
968{
969 struct sock *sk;
970
971 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
972 if (!sk)
973 return NULL;
974
975 sock_init_data(sock, sk);
976 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
977
978 sk->sk_destruct = l2cap_sock_destruct;
979 sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT);
980
981 sock_reset_flag(sk, SOCK_ZAPPED);
982
983 sk->sk_protocol = proto;
984 sk->sk_state = BT_OPEN;
985
986 setup_timer(&sk->sk_timer, l2cap_sock_timeout, (unsigned long) sk);
987
988 bt_sock_link(&l2cap_sk_list, sk);
989 return sk;
990}
991
992static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
993 int kern)
994{
995 struct sock *sk;
996
997 BT_DBG("sock %p", sock);
998
999 sock->state = SS_UNCONNECTED;
1000
1001 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1002 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1003 return -ESOCKTNOSUPPORT;
1004
1005 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1006 return -EPERM;
1007
1008 sock->ops = &l2cap_sock_ops;
1009
1010 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1011 if (!sk)
1012 return -ENOMEM;
1013
1014 l2cap_sock_init(sk, NULL);
1015 return 0;
1016}
1017
Gustavo F. Padovan65390582011-02-04 02:33:56 -02001018const struct proto_ops l2cap_sock_ops = {
1019 .family = PF_BLUETOOTH,
1020 .owner = THIS_MODULE,
1021 .release = l2cap_sock_release,
1022 .bind = l2cap_sock_bind,
1023 .connect = l2cap_sock_connect,
1024 .listen = l2cap_sock_listen,
1025 .accept = l2cap_sock_accept,
1026 .getname = l2cap_sock_getname,
1027 .sendmsg = l2cap_sock_sendmsg,
1028 .recvmsg = l2cap_sock_recvmsg,
1029 .poll = bt_sock_poll,
1030 .ioctl = bt_sock_ioctl,
1031 .mmap = sock_no_mmap,
1032 .socketpair = sock_no_socketpair,
1033 .shutdown = l2cap_sock_shutdown,
1034 .setsockopt = l2cap_sock_setsockopt,
1035 .getsockopt = l2cap_sock_getsockopt
1036};
1037
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02001038static const struct net_proto_family l2cap_sock_family_ops = {
1039 .family = PF_BLUETOOTH,
1040 .owner = THIS_MODULE,
1041 .create = l2cap_sock_create,
1042};
1043
1044int __init l2cap_init_sockets(void)
1045{
1046 int err;
1047
1048 err = proto_register(&l2cap_proto, 0);
1049 if (err < 0)
1050 return err;
1051
1052 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1053 if (err < 0)
1054 goto error;
1055
1056 BT_INFO("L2CAP socket layer initialized");
1057
1058 return 0;
1059
1060error:
1061 BT_ERR("L2CAP socket registration failed");
1062 proto_unregister(&l2cap_proto);
1063 return err;
1064}
1065
1066void l2cap_cleanup_sockets(void)
1067{
1068 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1069 BT_ERR("L2CAP socket unregistration failed");
1070
1071 proto_unregister(&l2cap_proto);
1072}