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 <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
31 #include <net/bluetooth/l2cap.h>
33 static const struct proto_ops l2cap_sock_ops
;
35 /* ---- L2CAP timers ---- */
36 static void l2cap_sock_timeout(unsigned long arg
)
38 struct sock
*sk
= (struct sock
*) arg
;
41 BT_DBG("sock %p state %d", sk
, sk
->sk_state
);
45 if (sock_owned_by_user(sk
)) {
46 /* sk is owned by user. Try again later */
47 l2cap_sock_set_timer(sk
, HZ
/ 5);
53 if (sk
->sk_state
== BT_CONNECTED
|| sk
->sk_state
== BT_CONFIG
)
54 reason
= ECONNREFUSED
;
55 else if (sk
->sk_state
== BT_CONNECT
&&
56 l2cap_pi(sk
)->chan
->sec_level
!= BT_SECURITY_SDP
)
57 reason
= ECONNREFUSED
;
61 __l2cap_sock_close(sk
, reason
);
69 void l2cap_sock_set_timer(struct sock
*sk
, long timeout
)
71 BT_DBG("sk %p state %d timeout %ld", sk
, sk
->sk_state
, timeout
);
72 sk_reset_timer(sk
, &sk
->sk_timer
, jiffies
+ timeout
);
75 void l2cap_sock_clear_timer(struct sock
*sk
)
77 BT_DBG("sock %p state %d", sk
, sk
->sk_state
);
78 sk_stop_timer(sk
, &sk
->sk_timer
);
81 static int l2cap_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int alen
)
83 struct sock
*sk
= sock
->sk
;
84 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
85 struct sockaddr_l2 la
;
90 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
93 memset(&la
, 0, sizeof(la
));
94 len
= min_t(unsigned int, sizeof(la
), alen
);
95 memcpy(&la
, addr
, len
);
97 if (la
.l2_cid
&& la
.l2_psm
)
102 if (sk
->sk_state
!= BT_OPEN
) {
108 __u16 psm
= __le16_to_cpu(la
.l2_psm
);
110 /* PSM must be odd and lsb of upper byte must be 0 */
111 if ((psm
& 0x0101) != 0x0001) {
116 /* Restrict usage of well-known PSMs */
117 if (psm
< 0x1001 && !capable(CAP_NET_BIND_SERVICE
)) {
124 err
= l2cap_add_scid(chan
, la
.l2_cid
);
126 err
= l2cap_add_psm(chan
, &la
.l2_bdaddr
, la
.l2_psm
);
131 if (__le16_to_cpu(la
.l2_psm
) == 0x0001 ||
132 __le16_to_cpu(la
.l2_psm
) == 0x0003)
133 chan
->sec_level
= BT_SECURITY_SDP
;
135 bacpy(&bt_sk(sk
)->src
, &la
.l2_bdaddr
);
136 sk
->sk_state
= BT_BOUND
;
143 static int l2cap_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
145 struct sock
*sk
= sock
->sk
;
146 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
147 struct sockaddr_l2 la
;
152 if (!addr
|| alen
< sizeof(addr
->sa_family
) ||
153 addr
->sa_family
!= AF_BLUETOOTH
)
156 memset(&la
, 0, sizeof(la
));
157 len
= min_t(unsigned int, sizeof(la
), alen
);
158 memcpy(&la
, addr
, len
);
160 if (la
.l2_cid
&& la
.l2_psm
)
165 if ((sk
->sk_type
== SOCK_SEQPACKET
|| sk
->sk_type
== SOCK_STREAM
)
166 && !(la
.l2_psm
|| la
.l2_cid
)) {
171 switch (chan
->mode
) {
172 case L2CAP_MODE_BASIC
:
174 case L2CAP_MODE_ERTM
:
175 case L2CAP_MODE_STREAMING
:
184 switch (sk
->sk_state
) {
188 /* Already connecting */
192 /* Already connected */
206 /* PSM must be odd and lsb of upper byte must be 0 */
207 if ((__le16_to_cpu(la
.l2_psm
) & 0x0101) != 0x0001 &&
208 sk
->sk_type
!= SOCK_RAW
&& !la
.l2_cid
) {
213 /* Set destination address and psm */
214 bacpy(&bt_sk(sk
)->dst
, &la
.l2_bdaddr
);
215 chan
->psm
= la
.l2_psm
;
216 chan
->dcid
= la
.l2_cid
;
218 err
= l2cap_chan_connect(l2cap_pi(sk
)->chan
);
223 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
224 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
230 static int l2cap_sock_listen(struct socket
*sock
, int backlog
)
232 struct sock
*sk
= sock
->sk
;
233 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
236 BT_DBG("sk %p backlog %d", sk
, backlog
);
240 if ((sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
)
241 || sk
->sk_state
!= BT_BOUND
) {
246 switch (chan
->mode
) {
247 case L2CAP_MODE_BASIC
:
249 case L2CAP_MODE_ERTM
:
250 case L2CAP_MODE_STREAMING
:
259 sk
->sk_max_ack_backlog
= backlog
;
260 sk
->sk_ack_backlog
= 0;
261 sk
->sk_state
= BT_LISTEN
;
268 static int l2cap_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
270 DECLARE_WAITQUEUE(wait
, current
);
271 struct sock
*sk
= sock
->sk
, *nsk
;
275 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
277 if (sk
->sk_state
!= BT_LISTEN
) {
282 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
284 BT_DBG("sk %p timeo %ld", sk
, timeo
);
286 /* Wait for an incoming connection. (wake-one). */
287 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
288 while (!(nsk
= bt_accept_dequeue(sk
, newsock
))) {
289 set_current_state(TASK_INTERRUPTIBLE
);
296 timeo
= schedule_timeout(timeo
);
297 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
299 if (sk
->sk_state
!= BT_LISTEN
) {
304 if (signal_pending(current
)) {
305 err
= sock_intr_errno(timeo
);
309 set_current_state(TASK_RUNNING
);
310 remove_wait_queue(sk_sleep(sk
), &wait
);
315 newsock
->state
= SS_CONNECTED
;
317 BT_DBG("new socket %p", nsk
);
324 static int l2cap_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
326 struct sockaddr_l2
*la
= (struct sockaddr_l2
*) addr
;
327 struct sock
*sk
= sock
->sk
;
328 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
330 BT_DBG("sock %p, sk %p", sock
, sk
);
332 addr
->sa_family
= AF_BLUETOOTH
;
333 *len
= sizeof(struct sockaddr_l2
);
336 la
->l2_psm
= chan
->psm
;
337 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->dst
);
338 la
->l2_cid
= cpu_to_le16(chan
->dcid
);
340 la
->l2_psm
= chan
->sport
;
341 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->src
);
342 la
->l2_cid
= cpu_to_le16(chan
->scid
);
348 static int l2cap_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
350 struct sock
*sk
= sock
->sk
;
351 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
352 struct l2cap_options opts
;
353 struct l2cap_conninfo cinfo
;
359 if (get_user(len
, optlen
))
366 memset(&opts
, 0, sizeof(opts
));
367 opts
.imtu
= chan
->imtu
;
368 opts
.omtu
= chan
->omtu
;
369 opts
.flush_to
= chan
->flush_to
;
370 opts
.mode
= chan
->mode
;
371 opts
.fcs
= chan
->fcs
;
372 opts
.max_tx
= chan
->max_tx
;
373 opts
.txwin_size
= (__u16
)chan
->tx_win
;
375 len
= min_t(unsigned int, len
, sizeof(opts
));
376 if (copy_to_user(optval
, (char *) &opts
, len
))
382 switch (chan
->sec_level
) {
383 case BT_SECURITY_LOW
:
386 case BT_SECURITY_MEDIUM
:
387 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
;
389 case BT_SECURITY_HIGH
:
390 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
|
398 if (chan
->role_switch
)
399 opt
|= L2CAP_LM_MASTER
;
401 if (chan
->force_reliable
)
402 opt
|= L2CAP_LM_RELIABLE
;
404 if (put_user(opt
, (u32 __user
*) optval
))
409 if (sk
->sk_state
!= BT_CONNECTED
&&
410 !(sk
->sk_state
== BT_CONNECT2
&&
411 bt_sk(sk
)->defer_setup
)) {
416 cinfo
.hci_handle
= chan
->conn
->hcon
->handle
;
417 memcpy(cinfo
.dev_class
, chan
->conn
->hcon
->dev_class
, 3);
419 len
= min_t(unsigned int, len
, sizeof(cinfo
));
420 if (copy_to_user(optval
, (char *) &cinfo
, len
))
434 static int l2cap_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
436 struct sock
*sk
= sock
->sk
;
437 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
438 struct bt_security sec
;
443 if (level
== SOL_L2CAP
)
444 return l2cap_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
446 if (level
!= SOL_BLUETOOTH
)
449 if (get_user(len
, optlen
))
456 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
457 && sk
->sk_type
!= SOCK_RAW
) {
462 sec
.level
= chan
->sec_level
;
464 len
= min_t(unsigned int, len
, sizeof(sec
));
465 if (copy_to_user(optval
, (char *) &sec
, len
))
471 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
476 if (put_user(bt_sk(sk
)->defer_setup
, (u32 __user
*) optval
))
482 if (put_user(chan
->flushable
, (u32 __user
*) optval
))
496 static int l2cap_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
498 struct sock
*sk
= sock
->sk
;
499 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
500 struct l2cap_options opts
;
510 if (sk
->sk_state
== BT_CONNECTED
) {
515 opts
.imtu
= chan
->imtu
;
516 opts
.omtu
= chan
->omtu
;
517 opts
.flush_to
= chan
->flush_to
;
518 opts
.mode
= chan
->mode
;
519 opts
.fcs
= chan
->fcs
;
520 opts
.max_tx
= chan
->max_tx
;
521 opts
.txwin_size
= (__u16
)chan
->tx_win
;
523 len
= min_t(unsigned int, sizeof(opts
), optlen
);
524 if (copy_from_user((char *) &opts
, optval
, len
)) {
529 if (opts
.txwin_size
> L2CAP_DEFAULT_TX_WINDOW
) {
534 chan
->mode
= opts
.mode
;
535 switch (chan
->mode
) {
536 case L2CAP_MODE_BASIC
:
537 chan
->conf_state
&= ~L2CAP_CONF_STATE2_DEVICE
;
539 case L2CAP_MODE_ERTM
:
540 case L2CAP_MODE_STREAMING
:
549 chan
->imtu
= opts
.imtu
;
550 chan
->omtu
= opts
.omtu
;
551 chan
->fcs
= opts
.fcs
;
552 chan
->max_tx
= opts
.max_tx
;
553 chan
->tx_win
= (__u8
)opts
.txwin_size
;
557 if (get_user(opt
, (u32 __user
*) optval
)) {
562 if (opt
& L2CAP_LM_AUTH
)
563 chan
->sec_level
= BT_SECURITY_LOW
;
564 if (opt
& L2CAP_LM_ENCRYPT
)
565 chan
->sec_level
= BT_SECURITY_MEDIUM
;
566 if (opt
& L2CAP_LM_SECURE
)
567 chan
->sec_level
= BT_SECURITY_HIGH
;
569 chan
->role_switch
= (opt
& L2CAP_LM_MASTER
);
570 chan
->force_reliable
= (opt
& L2CAP_LM_RELIABLE
);
582 static int l2cap_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
584 struct sock
*sk
= sock
->sk
;
585 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
586 struct bt_security sec
;
592 if (level
== SOL_L2CAP
)
593 return l2cap_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
595 if (level
!= SOL_BLUETOOTH
)
602 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
603 && sk
->sk_type
!= SOCK_RAW
) {
608 sec
.level
= BT_SECURITY_LOW
;
610 len
= min_t(unsigned int, sizeof(sec
), optlen
);
611 if (copy_from_user((char *) &sec
, optval
, len
)) {
616 if (sec
.level
< BT_SECURITY_LOW
||
617 sec
.level
> BT_SECURITY_HIGH
) {
622 chan
->sec_level
= sec
.level
;
626 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
631 if (get_user(opt
, (u32 __user
*) optval
)) {
636 bt_sk(sk
)->defer_setup
= opt
;
640 if (get_user(opt
, (u32 __user
*) optval
)) {
645 if (opt
> BT_FLUSHABLE_ON
) {
650 if (opt
== BT_FLUSHABLE_OFF
) {
651 struct l2cap_conn
*conn
= chan
->conn
;
652 /* proceed further only when we have l2cap_conn and
653 No Flush support in the LM */
654 if (!conn
|| !lmp_no_flush_capable(conn
->hcon
->hdev
)) {
660 chan
->flushable
= opt
;
672 static int l2cap_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
)
674 struct sock
*sk
= sock
->sk
;
675 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
680 BT_DBG("sock %p, sk %p", sock
, sk
);
682 err
= sock_error(sk
);
686 if (msg
->msg_flags
& MSG_OOB
)
691 if (sk
->sk_state
!= BT_CONNECTED
) {
696 /* Connectionless channel */
697 if (sk
->sk_type
== SOCK_DGRAM
) {
698 skb
= l2cap_create_connless_pdu(chan
, msg
, len
);
702 l2cap_do_send(chan
, skb
);
708 switch (chan
->mode
) {
709 case L2CAP_MODE_BASIC
:
710 /* Check outgoing MTU */
711 if (len
> chan
->omtu
) {
716 /* Create a basic PDU */
717 skb
= l2cap_create_basic_pdu(chan
, msg
, len
);
723 l2cap_do_send(chan
, skb
);
727 case L2CAP_MODE_ERTM
:
728 case L2CAP_MODE_STREAMING
:
729 /* Entire SDU fits into one PDU */
730 if (len
<= chan
->remote_mps
) {
731 control
= L2CAP_SDU_UNSEGMENTED
;
732 skb
= l2cap_create_iframe_pdu(chan
, msg
, len
, control
,
738 __skb_queue_tail(&chan
->tx_q
, skb
);
740 if (chan
->tx_send_head
== NULL
)
741 chan
->tx_send_head
= skb
;
744 /* Segment SDU into multiples PDUs */
745 err
= l2cap_sar_segment_sdu(chan
, msg
, len
);
750 if (chan
->mode
== L2CAP_MODE_STREAMING
) {
751 l2cap_streaming_send(chan
);
756 if ((chan
->conn_state
& L2CAP_CONN_REMOTE_BUSY
) &&
757 (chan
->conn_state
& L2CAP_CONN_WAIT_F
)) {
761 err
= l2cap_ertm_send(chan
);
768 BT_DBG("bad state %1.1x", chan
->mode
);
777 static int l2cap_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
, int flags
)
779 struct sock
*sk
= sock
->sk
;
783 if (sk
->sk_state
== BT_CONNECT2
&& bt_sk(sk
)->defer_setup
) {
784 sk
->sk_state
= BT_CONFIG
;
786 __l2cap_connect_rsp_defer(l2cap_pi(sk
)->chan
);
793 if (sock
->type
== SOCK_STREAM
)
794 return bt_sock_stream_recvmsg(iocb
, sock
, msg
, len
, flags
);
796 return bt_sock_recvmsg(iocb
, sock
, msg
, len
, flags
);
799 /* Kill socket (only if zapped and orphan)
800 * Must be called on unlocked socket.
802 void l2cap_sock_kill(struct sock
*sk
)
804 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
807 BT_DBG("sk %p state %d", sk
, sk
->sk_state
);
809 /* Kill poor orphan */
811 l2cap_chan_destroy(l2cap_pi(sk
)->chan
);
812 sock_set_flag(sk
, SOCK_DEAD
);
816 /* Must be called on unlocked socket. */
817 static void l2cap_sock_close(struct sock
*sk
)
819 l2cap_sock_clear_timer(sk
);
821 __l2cap_sock_close(sk
, ECONNRESET
);
826 static void l2cap_sock_cleanup_listen(struct sock
*parent
)
830 BT_DBG("parent %p", parent
);
832 /* Close not yet accepted channels */
833 while ((sk
= bt_accept_dequeue(parent
, NULL
)))
834 l2cap_sock_close(sk
);
836 parent
->sk_state
= BT_CLOSED
;
837 sock_set_flag(parent
, SOCK_ZAPPED
);
840 void __l2cap_sock_close(struct sock
*sk
, int reason
)
842 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
843 struct l2cap_conn
*conn
= chan
->conn
;
845 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
847 switch (sk
->sk_state
) {
849 l2cap_sock_cleanup_listen(sk
);
854 if ((sk
->sk_type
== SOCK_SEQPACKET
||
855 sk
->sk_type
== SOCK_STREAM
) &&
856 conn
->hcon
->type
== ACL_LINK
) {
857 l2cap_sock_set_timer(sk
, sk
->sk_sndtimeo
);
858 l2cap_send_disconn_req(conn
, chan
, reason
);
860 l2cap_chan_del(chan
, reason
);
864 if ((sk
->sk_type
== SOCK_SEQPACKET
||
865 sk
->sk_type
== SOCK_STREAM
) &&
866 conn
->hcon
->type
== ACL_LINK
) {
867 struct l2cap_conn_rsp rsp
;
870 if (bt_sk(sk
)->defer_setup
)
871 result
= L2CAP_CR_SEC_BLOCK
;
873 result
= L2CAP_CR_BAD_PSM
;
875 rsp
.scid
= cpu_to_le16(chan
->dcid
);
876 rsp
.dcid
= cpu_to_le16(chan
->scid
);
877 rsp
.result
= cpu_to_le16(result
);
878 rsp
.status
= cpu_to_le16(L2CAP_CS_NO_INFO
);
879 l2cap_send_cmd(conn
, chan
->ident
, L2CAP_CONN_RSP
,
883 l2cap_chan_del(chan
, reason
);
888 l2cap_chan_del(chan
, reason
);
892 sock_set_flag(sk
, SOCK_ZAPPED
);
897 static int l2cap_sock_shutdown(struct socket
*sock
, int how
)
899 struct sock
*sk
= sock
->sk
;
900 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
903 BT_DBG("sock %p, sk %p", sock
, sk
);
909 if (!sk
->sk_shutdown
) {
910 if (chan
->mode
== L2CAP_MODE_ERTM
)
911 err
= __l2cap_wait_ack(sk
);
913 sk
->sk_shutdown
= SHUTDOWN_MASK
;
914 l2cap_sock_clear_timer(sk
);
915 __l2cap_sock_close(sk
, 0);
917 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
918 err
= bt_sock_wait_state(sk
, BT_CLOSED
,
922 if (!err
&& sk
->sk_err
)
929 static int l2cap_sock_release(struct socket
*sock
)
931 struct sock
*sk
= sock
->sk
;
934 BT_DBG("sock %p, sk %p", sock
, sk
);
939 err
= l2cap_sock_shutdown(sock
, 2);
946 static void l2cap_sock_destruct(struct sock
*sk
)
950 skb_queue_purge(&sk
->sk_receive_queue
);
951 skb_queue_purge(&sk
->sk_write_queue
);
954 void l2cap_sock_init(struct sock
*sk
, struct sock
*parent
)
956 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
957 struct l2cap_chan
*chan
= pi
->chan
;
962 struct l2cap_chan
*pchan
= l2cap_pi(parent
)->chan
;
964 sk
->sk_type
= parent
->sk_type
;
965 bt_sk(sk
)->defer_setup
= bt_sk(parent
)->defer_setup
;
967 chan
->imtu
= pchan
->imtu
;
968 chan
->omtu
= pchan
->omtu
;
969 chan
->conf_state
= pchan
->conf_state
;
970 chan
->mode
= pchan
->mode
;
971 chan
->fcs
= pchan
->fcs
;
972 chan
->max_tx
= pchan
->max_tx
;
973 chan
->tx_win
= pchan
->tx_win
;
974 chan
->sec_level
= pchan
->sec_level
;
975 chan
->role_switch
= pchan
->role_switch
;
976 chan
->force_reliable
= pchan
->force_reliable
;
977 chan
->flushable
= pchan
->flushable
;
979 chan
->imtu
= L2CAP_DEFAULT_MTU
;
981 if (!disable_ertm
&& sk
->sk_type
== SOCK_STREAM
) {
982 chan
->mode
= L2CAP_MODE_ERTM
;
983 chan
->conf_state
|= L2CAP_CONF_STATE2_DEVICE
;
985 chan
->mode
= L2CAP_MODE_BASIC
;
987 chan
->max_tx
= L2CAP_DEFAULT_MAX_TX
;
988 chan
->fcs
= L2CAP_FCS_CRC16
;
989 chan
->tx_win
= L2CAP_DEFAULT_TX_WINDOW
;
990 chan
->sec_level
= BT_SECURITY_LOW
;
991 chan
->role_switch
= 0;
992 chan
->force_reliable
= 0;
993 chan
->flushable
= BT_FLUSHABLE_OFF
;
996 /* Default config options */
997 chan
->flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
1000 static struct proto l2cap_proto
= {
1002 .owner
= THIS_MODULE
,
1003 .obj_size
= sizeof(struct l2cap_pinfo
)
1006 struct sock
*l2cap_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
1010 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &l2cap_proto
);
1014 sock_init_data(sock
, sk
);
1015 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
1017 sk
->sk_destruct
= l2cap_sock_destruct
;
1018 sk
->sk_sndtimeo
= msecs_to_jiffies(L2CAP_CONN_TIMEOUT
);
1020 sock_reset_flag(sk
, SOCK_ZAPPED
);
1022 sk
->sk_protocol
= proto
;
1023 sk
->sk_state
= BT_OPEN
;
1025 setup_timer(&sk
->sk_timer
, l2cap_sock_timeout
, (unsigned long) sk
);
1030 static int l2cap_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
1034 struct l2cap_chan
*chan
;
1036 BT_DBG("sock %p", sock
);
1038 sock
->state
= SS_UNCONNECTED
;
1040 if (sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
&&
1041 sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
)
1042 return -ESOCKTNOSUPPORT
;
1044 if (sock
->type
== SOCK_RAW
&& !kern
&& !capable(CAP_NET_RAW
))
1047 sock
->ops
= &l2cap_sock_ops
;
1049 sk
= l2cap_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
1053 chan
= l2cap_chan_create(sk
);
1055 l2cap_sock_kill(sk
);
1059 l2cap_pi(sk
)->chan
= chan
;
1061 l2cap_sock_init(sk
, NULL
);
1065 static const struct proto_ops l2cap_sock_ops
= {
1066 .family
= PF_BLUETOOTH
,
1067 .owner
= THIS_MODULE
,
1068 .release
= l2cap_sock_release
,
1069 .bind
= l2cap_sock_bind
,
1070 .connect
= l2cap_sock_connect
,
1071 .listen
= l2cap_sock_listen
,
1072 .accept
= l2cap_sock_accept
,
1073 .getname
= l2cap_sock_getname
,
1074 .sendmsg
= l2cap_sock_sendmsg
,
1075 .recvmsg
= l2cap_sock_recvmsg
,
1076 .poll
= bt_sock_poll
,
1077 .ioctl
= bt_sock_ioctl
,
1078 .mmap
= sock_no_mmap
,
1079 .socketpair
= sock_no_socketpair
,
1080 .shutdown
= l2cap_sock_shutdown
,
1081 .setsockopt
= l2cap_sock_setsockopt
,
1082 .getsockopt
= l2cap_sock_getsockopt
1085 static const struct net_proto_family l2cap_sock_family_ops
= {
1086 .family
= PF_BLUETOOTH
,
1087 .owner
= THIS_MODULE
,
1088 .create
= l2cap_sock_create
,
1091 int __init
l2cap_init_sockets(void)
1095 err
= proto_register(&l2cap_proto
, 0);
1099 err
= bt_sock_register(BTPROTO_L2CAP
, &l2cap_sock_family_ops
);
1103 BT_INFO("L2CAP socket layer initialized");
1108 BT_ERR("L2CAP socket registration failed");
1109 proto_unregister(&l2cap_proto
);
1113 void l2cap_cleanup_sockets(void)
1115 if (bt_sock_unregister(BTPROTO_L2CAP
) < 0)
1116 BT_ERR("L2CAP socket unregistration failed");
1118 proto_unregister(&l2cap_proto
);