jbd: Issue cache flush after checkpointing
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / bluetooth / l2cap_sock.c
blob5c406d3136f74fc206380968a0a98c3c4b742474
1 /*
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.
7 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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;
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.
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.
27 /* Bluetooth L2CAP sockets. */
29 #include <linux/security.h>
30 #include <linux/export.h>
32 #include <net/bluetooth/bluetooth.h>
33 #include <net/bluetooth/hci_core.h>
34 #include <net/bluetooth/l2cap.h>
35 #include <net/bluetooth/smp.h>
37 static const struct proto_ops l2cap_sock_ops;
38 static void l2cap_sock_init(struct sock *sk, struct sock *parent);
39 static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio);
41 static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
43 struct sock *sk = sock->sk;
44 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
45 struct sockaddr_l2 la;
46 int len, err = 0;
48 BT_DBG("sk %p", sk);
50 if (!addr || addr->sa_family != AF_BLUETOOTH)
51 return -EINVAL;
53 memset(&la, 0, sizeof(la));
54 len = min_t(unsigned int, sizeof(la), alen);
55 memcpy(&la, addr, len);
57 if (la.l2_cid && la.l2_psm)
58 return -EINVAL;
60 lock_sock(sk);
62 if (sk->sk_state != BT_OPEN) {
63 err = -EBADFD;
64 goto done;
67 if (la.l2_psm) {
68 __u16 psm = __le16_to_cpu(la.l2_psm);
70 /* PSM must be odd and lsb of upper byte must be 0 */
71 if ((psm & 0x0101) != 0x0001) {
72 err = -EINVAL;
73 goto done;
76 /* Restrict usage of well-known PSMs */
77 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
78 err = -EACCES;
79 goto done;
83 if (la.l2_cid)
84 err = l2cap_add_scid(chan, la.l2_cid);
85 else
86 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
88 if (err < 0)
89 goto done;
91 if (__le16_to_cpu(la.l2_psm) == 0x0001 ||
92 __le16_to_cpu(la.l2_psm) == 0x0003)
93 chan->sec_level = BT_SECURITY_SDP;
95 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
97 chan->state = BT_BOUND;
98 sk->sk_state = BT_BOUND;
100 done:
101 release_sock(sk);
102 return err;
105 static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
107 struct sock *sk = sock->sk;
108 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
109 struct sockaddr_l2 la;
110 int len, err = 0;
112 BT_DBG("sk %p", sk);
114 if (!addr || alen < sizeof(addr->sa_family) ||
115 addr->sa_family != AF_BLUETOOTH)
116 return -EINVAL;
118 memset(&la, 0, sizeof(la));
119 len = min_t(unsigned int, sizeof(la), alen);
120 memcpy(&la, addr, len);
122 if (la.l2_cid && la.l2_psm)
123 return -EINVAL;
125 lock_sock(sk);
127 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED
128 && !(la.l2_psm || la.l2_cid)) {
129 err = -EINVAL;
130 goto done;
133 switch (chan->mode) {
134 case L2CAP_MODE_BASIC:
135 break;
136 case L2CAP_MODE_ERTM:
137 case L2CAP_MODE_STREAMING:
138 if (!disable_ertm)
139 break;
140 /* fall through */
141 default:
142 err = -ENOTSUPP;
143 goto done;
146 switch (sk->sk_state) {
147 case BT_CONNECT:
148 case BT_CONNECT2:
149 case BT_CONFIG:
150 /* Already connecting */
151 goto wait;
153 case BT_CONNECTED:
154 /* Already connected */
155 err = -EISCONN;
156 goto done;
158 case BT_OPEN:
159 case BT_BOUND:
160 /* Can connect */
161 break;
163 default:
164 err = -EBADFD;
165 goto done;
168 /* PSM must be odd and lsb of upper byte must be 0 */
169 if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 && !la.l2_cid &&
170 chan->chan_type != L2CAP_CHAN_RAW) {
171 err = -EINVAL;
172 goto done;
175 /* Set destination address and psm */
176 bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr);
177 chan->psm = la.l2_psm;
178 chan->dcid = la.l2_cid;
180 err = l2cap_chan_connect(l2cap_pi(sk)->chan);
181 if (err)
182 goto done;
184 wait:
185 err = bt_sock_wait_state(sk, BT_CONNECTED,
186 sock_sndtimeo(sk, flags & O_NONBLOCK));
187 done:
188 release_sock(sk);
189 return err;
192 static int l2cap_sock_listen(struct socket *sock, int backlog)
194 struct sock *sk = sock->sk;
195 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
196 int err = 0;
198 BT_DBG("sk %p backlog %d", sk, backlog);
200 lock_sock(sk);
202 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
203 || sk->sk_state != BT_BOUND) {
204 err = -EBADFD;
205 goto done;
208 switch (chan->mode) {
209 case L2CAP_MODE_BASIC:
210 break;
211 case L2CAP_MODE_ERTM:
212 case L2CAP_MODE_STREAMING:
213 if (!disable_ertm)
214 break;
215 /* fall through */
216 default:
217 err = -ENOTSUPP;
218 goto done;
221 sk->sk_max_ack_backlog = backlog;
222 sk->sk_ack_backlog = 0;
224 chan->state = BT_LISTEN;
225 sk->sk_state = BT_LISTEN;
227 done:
228 release_sock(sk);
229 return err;
232 static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
234 DECLARE_WAITQUEUE(wait, current);
235 struct sock *sk = sock->sk, *nsk;
236 long timeo;
237 int err = 0;
239 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
241 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
243 BT_DBG("sk %p timeo %ld", sk, timeo);
245 /* Wait for an incoming connection. (wake-one). */
246 add_wait_queue_exclusive(sk_sleep(sk), &wait);
247 while (1) {
248 set_current_state(TASK_INTERRUPTIBLE);
250 if (sk->sk_state != BT_LISTEN) {
251 err = -EBADFD;
252 break;
255 nsk = bt_accept_dequeue(sk, newsock);
256 if (nsk)
257 break;
259 if (!timeo) {
260 err = -EAGAIN;
261 break;
264 if (signal_pending(current)) {
265 err = sock_intr_errno(timeo);
266 break;
269 release_sock(sk);
270 timeo = schedule_timeout(timeo);
271 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
273 __set_current_state(TASK_RUNNING);
274 remove_wait_queue(sk_sleep(sk), &wait);
276 if (err)
277 goto done;
279 newsock->state = SS_CONNECTED;
281 BT_DBG("new socket %p", nsk);
283 done:
284 release_sock(sk);
285 return err;
288 static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
290 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
291 struct sock *sk = sock->sk;
292 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
294 BT_DBG("sock %p, sk %p", sock, sk);
296 addr->sa_family = AF_BLUETOOTH;
297 *len = sizeof(struct sockaddr_l2);
299 if (peer) {
300 la->l2_psm = chan->psm;
301 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
302 la->l2_cid = cpu_to_le16(chan->dcid);
303 } else {
304 la->l2_psm = chan->sport;
305 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
306 la->l2_cid = cpu_to_le16(chan->scid);
309 return 0;
312 static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
314 struct sock *sk = sock->sk;
315 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
316 struct l2cap_options opts;
317 struct l2cap_conninfo cinfo;
318 int len, err = 0;
319 u32 opt;
321 BT_DBG("sk %p", sk);
323 if (get_user(len, optlen))
324 return -EFAULT;
326 lock_sock(sk);
328 switch (optname) {
329 case L2CAP_OPTIONS:
330 memset(&opts, 0, sizeof(opts));
331 opts.imtu = chan->imtu;
332 opts.omtu = chan->omtu;
333 opts.flush_to = chan->flush_to;
334 opts.mode = chan->mode;
335 opts.fcs = chan->fcs;
336 opts.max_tx = chan->max_tx;
337 opts.txwin_size = (__u16)chan->tx_win;
339 len = min_t(unsigned int, len, sizeof(opts));
340 if (copy_to_user(optval, (char *) &opts, len))
341 err = -EFAULT;
343 break;
345 case L2CAP_LM:
346 switch (chan->sec_level) {
347 case BT_SECURITY_LOW:
348 opt = L2CAP_LM_AUTH;
349 break;
350 case BT_SECURITY_MEDIUM:
351 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
352 break;
353 case BT_SECURITY_HIGH:
354 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
355 L2CAP_LM_SECURE;
356 break;
357 default:
358 opt = 0;
359 break;
362 if (chan->role_switch)
363 opt |= L2CAP_LM_MASTER;
365 if (chan->force_reliable)
366 opt |= L2CAP_LM_RELIABLE;
368 if (put_user(opt, (u32 __user *) optval))
369 err = -EFAULT;
370 break;
372 case L2CAP_CONNINFO:
373 if (sk->sk_state != BT_CONNECTED &&
374 !(sk->sk_state == BT_CONNECT2 &&
375 bt_sk(sk)->defer_setup)) {
376 err = -ENOTCONN;
377 break;
380 memset(&cinfo, 0, sizeof(cinfo));
381 cinfo.hci_handle = chan->conn->hcon->handle;
382 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
384 len = min_t(unsigned int, len, sizeof(cinfo));
385 if (copy_to_user(optval, (char *) &cinfo, len))
386 err = -EFAULT;
388 break;
390 default:
391 err = -ENOPROTOOPT;
392 break;
395 release_sock(sk);
396 return err;
399 static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
401 struct sock *sk = sock->sk;
402 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
403 struct bt_security sec;
404 struct bt_power pwr;
405 int len, err = 0;
407 BT_DBG("sk %p", sk);
409 if (level == SOL_L2CAP)
410 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
412 if (level != SOL_BLUETOOTH)
413 return -ENOPROTOOPT;
415 if (get_user(len, optlen))
416 return -EFAULT;
418 lock_sock(sk);
420 switch (optname) {
421 case BT_SECURITY:
422 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
423 chan->chan_type != L2CAP_CHAN_RAW) {
424 err = -EINVAL;
425 break;
428 memset(&sec, 0, sizeof(sec));
429 sec.level = chan->sec_level;
431 if (sk->sk_state == BT_CONNECTED)
432 sec.key_size = chan->conn->hcon->enc_key_size;
434 len = min_t(unsigned int, len, sizeof(sec));
435 if (copy_to_user(optval, (char *) &sec, len))
436 err = -EFAULT;
438 break;
440 case BT_DEFER_SETUP:
441 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
442 err = -EINVAL;
443 break;
446 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
447 err = -EFAULT;
449 break;
451 case BT_FLUSHABLE:
452 if (put_user(chan->flushable, (u32 __user *) optval))
453 err = -EFAULT;
455 break;
457 case BT_POWER:
458 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
459 && sk->sk_type != SOCK_RAW) {
460 err = -EINVAL;
461 break;
464 pwr.force_active = chan->force_active;
466 len = min_t(unsigned int, len, sizeof(pwr));
467 if (copy_to_user(optval, (char *) &pwr, len))
468 err = -EFAULT;
470 break;
472 default:
473 err = -ENOPROTOOPT;
474 break;
477 release_sock(sk);
478 return err;
481 static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
483 struct sock *sk = sock->sk;
484 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
485 struct l2cap_options opts;
486 int len, err = 0;
487 u32 opt;
489 BT_DBG("sk %p", sk);
491 lock_sock(sk);
493 switch (optname) {
494 case L2CAP_OPTIONS:
495 if (sk->sk_state == BT_CONNECTED) {
496 err = -EINVAL;
497 break;
500 opts.imtu = chan->imtu;
501 opts.omtu = chan->omtu;
502 opts.flush_to = chan->flush_to;
503 opts.mode = chan->mode;
504 opts.fcs = chan->fcs;
505 opts.max_tx = chan->max_tx;
506 opts.txwin_size = (__u16)chan->tx_win;
508 len = min_t(unsigned int, sizeof(opts), optlen);
509 if (copy_from_user((char *) &opts, optval, len)) {
510 err = -EFAULT;
511 break;
514 if (opts.txwin_size > L2CAP_DEFAULT_TX_WINDOW) {
515 err = -EINVAL;
516 break;
519 chan->mode = opts.mode;
520 switch (chan->mode) {
521 case L2CAP_MODE_BASIC:
522 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
523 break;
524 case L2CAP_MODE_ERTM:
525 case L2CAP_MODE_STREAMING:
526 if (!disable_ertm)
527 break;
528 /* fall through */
529 default:
530 err = -EINVAL;
531 break;
534 chan->imtu = opts.imtu;
535 chan->omtu = opts.omtu;
536 chan->fcs = opts.fcs;
537 chan->max_tx = opts.max_tx;
538 chan->tx_win = (__u8)opts.txwin_size;
539 break;
541 case L2CAP_LM:
542 if (get_user(opt, (u32 __user *) optval)) {
543 err = -EFAULT;
544 break;
547 if (opt & L2CAP_LM_AUTH)
548 chan->sec_level = BT_SECURITY_LOW;
549 if (opt & L2CAP_LM_ENCRYPT)
550 chan->sec_level = BT_SECURITY_MEDIUM;
551 if (opt & L2CAP_LM_SECURE)
552 chan->sec_level = BT_SECURITY_HIGH;
554 chan->role_switch = (opt & L2CAP_LM_MASTER);
555 chan->force_reliable = (opt & L2CAP_LM_RELIABLE);
556 break;
558 default:
559 err = -ENOPROTOOPT;
560 break;
563 release_sock(sk);
564 return err;
567 static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
569 struct sock *sk = sock->sk;
570 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
571 struct bt_security sec;
572 struct bt_power pwr;
573 struct l2cap_conn *conn;
574 int len, err = 0;
575 u32 opt;
577 BT_DBG("sk %p", sk);
579 if (level == SOL_L2CAP)
580 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
582 if (level != SOL_BLUETOOTH)
583 return -ENOPROTOOPT;
585 lock_sock(sk);
587 switch (optname) {
588 case BT_SECURITY:
589 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
590 chan->chan_type != L2CAP_CHAN_RAW) {
591 err = -EINVAL;
592 break;
595 sec.level = BT_SECURITY_LOW;
597 len = min_t(unsigned int, sizeof(sec), optlen);
598 if (copy_from_user((char *) &sec, optval, len)) {
599 err = -EFAULT;
600 break;
603 if (sec.level < BT_SECURITY_LOW ||
604 sec.level > BT_SECURITY_HIGH) {
605 err = -EINVAL;
606 break;
609 chan->sec_level = sec.level;
611 conn = chan->conn;
612 if (conn && chan->scid == L2CAP_CID_LE_DATA) {
613 if (!conn->hcon->out) {
614 err = -EINVAL;
615 break;
618 if (smp_conn_security(conn, sec.level))
619 break;
621 err = 0;
622 sk->sk_state = BT_CONFIG;
624 break;
626 case BT_DEFER_SETUP:
627 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
628 err = -EINVAL;
629 break;
632 if (get_user(opt, (u32 __user *) optval)) {
633 err = -EFAULT;
634 break;
637 bt_sk(sk)->defer_setup = opt;
638 break;
640 case BT_FLUSHABLE:
641 if (get_user(opt, (u32 __user *) optval)) {
642 err = -EFAULT;
643 break;
646 if (opt > BT_FLUSHABLE_ON) {
647 err = -EINVAL;
648 break;
651 if (opt == BT_FLUSHABLE_OFF) {
652 struct l2cap_conn *conn = chan->conn;
653 /* proceed further only when we have l2cap_conn and
654 No Flush support in the LM */
655 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
656 err = -EINVAL;
657 break;
661 chan->flushable = opt;
662 break;
664 case BT_POWER:
665 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
666 chan->chan_type != L2CAP_CHAN_RAW) {
667 err = -EINVAL;
668 break;
671 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
673 len = min_t(unsigned int, sizeof(pwr), optlen);
674 if (copy_from_user((char *) &pwr, optval, len)) {
675 err = -EFAULT;
676 break;
678 chan->force_active = pwr.force_active;
679 break;
681 default:
682 err = -ENOPROTOOPT;
683 break;
686 release_sock(sk);
687 return err;
690 static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
692 struct sock *sk = sock->sk;
693 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
694 int err;
696 BT_DBG("sock %p, sk %p", sock, sk);
698 err = sock_error(sk);
699 if (err)
700 return err;
702 if (msg->msg_flags & MSG_OOB)
703 return -EOPNOTSUPP;
705 lock_sock(sk);
707 if (sk->sk_state != BT_CONNECTED) {
708 release_sock(sk);
709 return -ENOTCONN;
712 err = l2cap_chan_send(chan, msg, len);
714 release_sock(sk);
715 return err;
718 static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
720 struct sock *sk = sock->sk;
721 struct l2cap_pinfo *pi = l2cap_pi(sk);
722 int err;
724 lock_sock(sk);
726 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
727 sk->sk_state = BT_CONFIG;
729 __l2cap_connect_rsp_defer(pi->chan);
730 release_sock(sk);
731 return 0;
734 release_sock(sk);
736 if (sock->type == SOCK_STREAM)
737 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
738 else
739 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
741 if (pi->chan->mode != L2CAP_MODE_ERTM)
742 return err;
744 /* Attempt to put pending rx data in the socket buffer */
746 lock_sock(sk);
748 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
749 goto done;
751 if (pi->rx_busy_skb) {
752 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
753 pi->rx_busy_skb = NULL;
754 else
755 goto done;
758 /* Restore data flow when half of the receive buffer is
759 * available. This avoids resending large numbers of
760 * frames.
762 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
763 l2cap_chan_busy(pi->chan, 0);
765 done:
766 release_sock(sk);
767 return err;
770 /* Kill socket (only if zapped and orphan)
771 * Must be called on unlocked socket.
773 static void l2cap_sock_kill(struct sock *sk)
775 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
776 return;
778 BT_DBG("sk %p state %d", sk, sk->sk_state);
780 /* Kill poor orphan */
782 l2cap_chan_destroy(l2cap_pi(sk)->chan);
783 sock_set_flag(sk, SOCK_DEAD);
784 sock_put(sk);
787 static int l2cap_sock_shutdown(struct socket *sock, int how)
789 struct sock *sk = sock->sk;
790 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
791 int err = 0;
793 BT_DBG("sock %p, sk %p", sock, sk);
795 if (!sk)
796 return 0;
798 lock_sock(sk);
799 if (!sk->sk_shutdown) {
800 if (chan->mode == L2CAP_MODE_ERTM)
801 err = __l2cap_wait_ack(sk);
803 sk->sk_shutdown = SHUTDOWN_MASK;
804 l2cap_chan_close(chan, 0);
806 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
807 err = bt_sock_wait_state(sk, BT_CLOSED,
808 sk->sk_lingertime);
811 if (!err && sk->sk_err)
812 err = -sk->sk_err;
814 release_sock(sk);
815 return err;
818 static int l2cap_sock_release(struct socket *sock)
820 struct sock *sk = sock->sk;
821 int err;
823 BT_DBG("sock %p, sk %p", sock, sk);
825 if (!sk)
826 return 0;
828 err = l2cap_sock_shutdown(sock, 2);
830 sock_orphan(sk);
831 l2cap_sock_kill(sk);
832 return err;
835 static struct l2cap_chan *l2cap_sock_new_connection_cb(void *data)
837 struct sock *sk, *parent = data;
839 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
840 GFP_ATOMIC);
841 if (!sk)
842 return NULL;
844 l2cap_sock_init(sk, parent);
846 return l2cap_pi(sk)->chan;
849 static int l2cap_sock_recv_cb(void *data, struct sk_buff *skb)
851 int err;
852 struct sock *sk = data;
853 struct l2cap_pinfo *pi = l2cap_pi(sk);
855 if (pi->rx_busy_skb)
856 return -ENOMEM;
858 err = sock_queue_rcv_skb(sk, skb);
860 /* For ERTM, handle one skb that doesn't fit into the recv
861 * buffer. This is important to do because the data frames
862 * have already been acked, so the skb cannot be discarded.
864 * Notify the l2cap core that the buffer is full, so the
865 * LOCAL_BUSY state is entered and no more frames are
866 * acked and reassembled until there is buffer space
867 * available.
869 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
870 pi->rx_busy_skb = skb;
871 l2cap_chan_busy(pi->chan, 1);
872 err = 0;
875 return err;
878 static void l2cap_sock_close_cb(void *data)
880 struct sock *sk = data;
882 l2cap_sock_kill(sk);
885 static void l2cap_sock_state_change_cb(void *data, int state)
887 struct sock *sk = data;
889 sk->sk_state = state;
892 static struct l2cap_ops l2cap_chan_ops = {
893 .name = "L2CAP Socket Interface",
894 .new_connection = l2cap_sock_new_connection_cb,
895 .recv = l2cap_sock_recv_cb,
896 .close = l2cap_sock_close_cb,
897 .state_change = l2cap_sock_state_change_cb,
900 static void l2cap_sock_destruct(struct sock *sk)
902 BT_DBG("sk %p", sk);
904 if (l2cap_pi(sk)->rx_busy_skb) {
905 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
906 l2cap_pi(sk)->rx_busy_skb = NULL;
909 skb_queue_purge(&sk->sk_receive_queue);
910 skb_queue_purge(&sk->sk_write_queue);
913 static void l2cap_sock_init(struct sock *sk, struct sock *parent)
915 struct l2cap_pinfo *pi = l2cap_pi(sk);
916 struct l2cap_chan *chan = pi->chan;
918 BT_DBG("sk %p", sk);
920 if (parent) {
921 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
923 sk->sk_type = parent->sk_type;
924 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
926 chan->chan_type = pchan->chan_type;
927 chan->imtu = pchan->imtu;
928 chan->omtu = pchan->omtu;
929 chan->conf_state = pchan->conf_state;
930 chan->mode = pchan->mode;
931 chan->fcs = pchan->fcs;
932 chan->max_tx = pchan->max_tx;
933 chan->tx_win = pchan->tx_win;
934 chan->sec_level = pchan->sec_level;
935 chan->role_switch = pchan->role_switch;
936 chan->force_reliable = pchan->force_reliable;
937 chan->flushable = pchan->flushable;
938 chan->force_active = pchan->force_active;
940 security_sk_clone(parent, sk);
941 } else {
943 switch (sk->sk_type) {
944 case SOCK_RAW:
945 chan->chan_type = L2CAP_CHAN_RAW;
946 break;
947 case SOCK_DGRAM:
948 chan->chan_type = L2CAP_CHAN_CONN_LESS;
949 break;
950 case SOCK_SEQPACKET:
951 case SOCK_STREAM:
952 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
953 break;
956 chan->imtu = L2CAP_DEFAULT_MTU;
957 chan->omtu = 0;
958 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
959 chan->mode = L2CAP_MODE_ERTM;
960 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
961 } else {
962 chan->mode = L2CAP_MODE_BASIC;
964 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
965 chan->fcs = L2CAP_FCS_CRC16;
966 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
967 chan->sec_level = BT_SECURITY_LOW;
968 chan->role_switch = 0;
969 chan->force_reliable = 0;
970 chan->flushable = BT_FLUSHABLE_OFF;
971 chan->force_active = BT_POWER_FORCE_ACTIVE_ON;
975 /* Default config options */
976 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
978 chan->data = sk;
979 chan->ops = &l2cap_chan_ops;
982 static struct proto l2cap_proto = {
983 .name = "L2CAP",
984 .owner = THIS_MODULE,
985 .obj_size = sizeof(struct l2cap_pinfo)
988 static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
990 struct sock *sk;
991 struct l2cap_chan *chan;
993 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
994 if (!sk)
995 return NULL;
997 sock_init_data(sock, sk);
998 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1000 sk->sk_destruct = l2cap_sock_destruct;
1001 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
1003 sock_reset_flag(sk, SOCK_ZAPPED);
1005 sk->sk_protocol = proto;
1006 sk->sk_state = BT_OPEN;
1008 chan = l2cap_chan_create(sk);
1009 if (!chan) {
1010 l2cap_sock_kill(sk);
1011 return NULL;
1014 l2cap_pi(sk)->chan = chan;
1016 return sk;
1019 static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1020 int kern)
1022 struct sock *sk;
1024 BT_DBG("sock %p", sock);
1026 sock->state = SS_UNCONNECTED;
1028 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1029 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1030 return -ESOCKTNOSUPPORT;
1032 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1033 return -EPERM;
1035 sock->ops = &l2cap_sock_ops;
1037 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1038 if (!sk)
1039 return -ENOMEM;
1041 l2cap_sock_init(sk, NULL);
1042 return 0;
1045 static const struct proto_ops l2cap_sock_ops = {
1046 .family = PF_BLUETOOTH,
1047 .owner = THIS_MODULE,
1048 .release = l2cap_sock_release,
1049 .bind = l2cap_sock_bind,
1050 .connect = l2cap_sock_connect,
1051 .listen = l2cap_sock_listen,
1052 .accept = l2cap_sock_accept,
1053 .getname = l2cap_sock_getname,
1054 .sendmsg = l2cap_sock_sendmsg,
1055 .recvmsg = l2cap_sock_recvmsg,
1056 .poll = bt_sock_poll,
1057 .ioctl = bt_sock_ioctl,
1058 .mmap = sock_no_mmap,
1059 .socketpair = sock_no_socketpair,
1060 .shutdown = l2cap_sock_shutdown,
1061 .setsockopt = l2cap_sock_setsockopt,
1062 .getsockopt = l2cap_sock_getsockopt
1065 static const struct net_proto_family l2cap_sock_family_ops = {
1066 .family = PF_BLUETOOTH,
1067 .owner = THIS_MODULE,
1068 .create = l2cap_sock_create,
1071 int __init l2cap_init_sockets(void)
1073 int err;
1075 err = proto_register(&l2cap_proto, 0);
1076 if (err < 0)
1077 return err;
1079 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1080 if (err < 0)
1081 goto error;
1083 BT_INFO("L2CAP socket layer initialized");
1085 return 0;
1087 error:
1088 BT_ERR("L2CAP socket registration failed");
1089 proto_unregister(&l2cap_proto);
1090 return err;
1093 void l2cap_cleanup_sockets(void)
1095 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1096 BT_ERR("L2CAP socket unregistration failed");
1098 proto_unregister(&l2cap_proto);