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 memset(&cinfo
, 0, sizeof(cinfo
));
417 cinfo
.hci_handle
= chan
->conn
->hcon
->handle
;
418 memcpy(cinfo
.dev_class
, chan
->conn
->hcon
->dev_class
, 3);
420 len
= min_t(unsigned int, len
, sizeof(cinfo
));
421 if (copy_to_user(optval
, (char *) &cinfo
, len
))
435 static int l2cap_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
437 struct sock
*sk
= sock
->sk
;
438 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
439 struct bt_security sec
;
444 if (level
== SOL_L2CAP
)
445 return l2cap_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
447 if (level
!= SOL_BLUETOOTH
)
450 if (get_user(len
, optlen
))
457 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
458 && sk
->sk_type
!= SOCK_RAW
) {
463 sec
.level
= chan
->sec_level
;
465 len
= min_t(unsigned int, len
, sizeof(sec
));
466 if (copy_to_user(optval
, (char *) &sec
, len
))
472 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
477 if (put_user(bt_sk(sk
)->defer_setup
, (u32 __user
*) optval
))
483 if (put_user(chan
->flushable
, (u32 __user
*) optval
))
497 static int l2cap_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
499 struct sock
*sk
= sock
->sk
;
500 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
501 struct l2cap_options opts
;
511 if (sk
->sk_state
== BT_CONNECTED
) {
516 opts
.imtu
= chan
->imtu
;
517 opts
.omtu
= chan
->omtu
;
518 opts
.flush_to
= chan
->flush_to
;
519 opts
.mode
= chan
->mode
;
520 opts
.fcs
= chan
->fcs
;
521 opts
.max_tx
= chan
->max_tx
;
522 opts
.txwin_size
= (__u16
)chan
->tx_win
;
524 len
= min_t(unsigned int, sizeof(opts
), optlen
);
525 if (copy_from_user((char *) &opts
, optval
, len
)) {
530 if (opts
.txwin_size
> L2CAP_DEFAULT_TX_WINDOW
) {
535 chan
->mode
= opts
.mode
;
536 switch (chan
->mode
) {
537 case L2CAP_MODE_BASIC
:
538 chan
->conf_state
&= ~L2CAP_CONF_STATE2_DEVICE
;
540 case L2CAP_MODE_ERTM
:
541 case L2CAP_MODE_STREAMING
:
550 chan
->imtu
= opts
.imtu
;
551 chan
->omtu
= opts
.omtu
;
552 chan
->fcs
= opts
.fcs
;
553 chan
->max_tx
= opts
.max_tx
;
554 chan
->tx_win
= (__u8
)opts
.txwin_size
;
558 if (get_user(opt
, (u32 __user
*) optval
)) {
563 if (opt
& L2CAP_LM_AUTH
)
564 chan
->sec_level
= BT_SECURITY_LOW
;
565 if (opt
& L2CAP_LM_ENCRYPT
)
566 chan
->sec_level
= BT_SECURITY_MEDIUM
;
567 if (opt
& L2CAP_LM_SECURE
)
568 chan
->sec_level
= BT_SECURITY_HIGH
;
570 chan
->role_switch
= (opt
& L2CAP_LM_MASTER
);
571 chan
->force_reliable
= (opt
& L2CAP_LM_RELIABLE
);
583 static int l2cap_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
585 struct sock
*sk
= sock
->sk
;
586 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
587 struct bt_security sec
;
593 if (level
== SOL_L2CAP
)
594 return l2cap_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
596 if (level
!= SOL_BLUETOOTH
)
603 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
604 && sk
->sk_type
!= SOCK_RAW
) {
609 sec
.level
= BT_SECURITY_LOW
;
611 len
= min_t(unsigned int, sizeof(sec
), optlen
);
612 if (copy_from_user((char *) &sec
, optval
, len
)) {
617 if (sec
.level
< BT_SECURITY_LOW
||
618 sec
.level
> BT_SECURITY_HIGH
) {
623 chan
->sec_level
= sec
.level
;
627 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
632 if (get_user(opt
, (u32 __user
*) optval
)) {
637 bt_sk(sk
)->defer_setup
= opt
;
641 if (get_user(opt
, (u32 __user
*) optval
)) {
646 if (opt
> BT_FLUSHABLE_ON
) {
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
)) {
661 chan
->flushable
= opt
;
673 static int l2cap_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
)
675 struct sock
*sk
= sock
->sk
;
676 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
681 BT_DBG("sock %p, sk %p", sock
, sk
);
683 err
= sock_error(sk
);
687 if (msg
->msg_flags
& MSG_OOB
)
692 if (sk
->sk_state
!= BT_CONNECTED
) {
697 /* Connectionless channel */
698 if (sk
->sk_type
== SOCK_DGRAM
) {
699 skb
= l2cap_create_connless_pdu(chan
, msg
, len
);
703 l2cap_do_send(chan
, skb
);
709 switch (chan
->mode
) {
710 case L2CAP_MODE_BASIC
:
711 /* Check outgoing MTU */
712 if (len
> chan
->omtu
) {
717 /* Create a basic PDU */
718 skb
= l2cap_create_basic_pdu(chan
, msg
, len
);
724 l2cap_do_send(chan
, skb
);
728 case L2CAP_MODE_ERTM
:
729 case L2CAP_MODE_STREAMING
:
730 /* Entire SDU fits into one PDU */
731 if (len
<= chan
->remote_mps
) {
732 control
= L2CAP_SDU_UNSEGMENTED
;
733 skb
= l2cap_create_iframe_pdu(chan
, msg
, len
, control
,
739 __skb_queue_tail(&chan
->tx_q
, skb
);
741 if (chan
->tx_send_head
== NULL
)
742 chan
->tx_send_head
= skb
;
745 /* Segment SDU into multiples PDUs */
746 err
= l2cap_sar_segment_sdu(chan
, msg
, len
);
751 if (chan
->mode
== L2CAP_MODE_STREAMING
) {
752 l2cap_streaming_send(chan
);
757 if ((chan
->conn_state
& L2CAP_CONN_REMOTE_BUSY
) &&
758 (chan
->conn_state
& L2CAP_CONN_WAIT_F
)) {
762 err
= l2cap_ertm_send(chan
);
769 BT_DBG("bad state %1.1x", chan
->mode
);
778 static int l2cap_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
, int flags
)
780 struct sock
*sk
= sock
->sk
;
784 if (sk
->sk_state
== BT_CONNECT2
&& bt_sk(sk
)->defer_setup
) {
785 sk
->sk_state
= BT_CONFIG
;
787 __l2cap_connect_rsp_defer(l2cap_pi(sk
)->chan
);
794 if (sock
->type
== SOCK_STREAM
)
795 return bt_sock_stream_recvmsg(iocb
, sock
, msg
, len
, flags
);
797 return bt_sock_recvmsg(iocb
, sock
, msg
, len
, flags
);
800 /* Kill socket (only if zapped and orphan)
801 * Must be called on unlocked socket.
803 void l2cap_sock_kill(struct sock
*sk
)
805 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
808 BT_DBG("sk %p state %d", sk
, sk
->sk_state
);
810 /* Kill poor orphan */
812 l2cap_chan_destroy(l2cap_pi(sk
)->chan
);
813 sock_set_flag(sk
, SOCK_DEAD
);
817 /* Must be called on unlocked socket. */
818 static void l2cap_sock_close(struct sock
*sk
)
820 l2cap_sock_clear_timer(sk
);
822 __l2cap_sock_close(sk
, ECONNRESET
);
827 static void l2cap_sock_cleanup_listen(struct sock
*parent
)
831 BT_DBG("parent %p", parent
);
833 /* Close not yet accepted channels */
834 while ((sk
= bt_accept_dequeue(parent
, NULL
)))
835 l2cap_sock_close(sk
);
837 parent
->sk_state
= BT_CLOSED
;
838 sock_set_flag(parent
, SOCK_ZAPPED
);
841 void __l2cap_sock_close(struct sock
*sk
, int reason
)
843 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
844 struct l2cap_conn
*conn
= chan
->conn
;
846 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
848 switch (sk
->sk_state
) {
850 l2cap_sock_cleanup_listen(sk
);
855 if ((sk
->sk_type
== SOCK_SEQPACKET
||
856 sk
->sk_type
== SOCK_STREAM
) &&
857 conn
->hcon
->type
== ACL_LINK
) {
858 l2cap_sock_set_timer(sk
, sk
->sk_sndtimeo
);
859 l2cap_send_disconn_req(conn
, chan
, reason
);
861 l2cap_chan_del(chan
, reason
);
865 if ((sk
->sk_type
== SOCK_SEQPACKET
||
866 sk
->sk_type
== SOCK_STREAM
) &&
867 conn
->hcon
->type
== ACL_LINK
) {
868 struct l2cap_conn_rsp rsp
;
871 if (bt_sk(sk
)->defer_setup
)
872 result
= L2CAP_CR_SEC_BLOCK
;
874 result
= L2CAP_CR_BAD_PSM
;
876 rsp
.scid
= cpu_to_le16(chan
->dcid
);
877 rsp
.dcid
= cpu_to_le16(chan
->scid
);
878 rsp
.result
= cpu_to_le16(result
);
879 rsp
.status
= cpu_to_le16(L2CAP_CS_NO_INFO
);
880 l2cap_send_cmd(conn
, chan
->ident
, L2CAP_CONN_RSP
,
884 l2cap_chan_del(chan
, reason
);
889 l2cap_chan_del(chan
, reason
);
893 sock_set_flag(sk
, SOCK_ZAPPED
);
898 static int l2cap_sock_shutdown(struct socket
*sock
, int how
)
900 struct sock
*sk
= sock
->sk
;
901 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
904 BT_DBG("sock %p, sk %p", sock
, sk
);
910 if (!sk
->sk_shutdown
) {
911 if (chan
->mode
== L2CAP_MODE_ERTM
)
912 err
= __l2cap_wait_ack(sk
);
914 sk
->sk_shutdown
= SHUTDOWN_MASK
;
915 l2cap_sock_clear_timer(sk
);
916 __l2cap_sock_close(sk
, 0);
918 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
919 err
= bt_sock_wait_state(sk
, BT_CLOSED
,
923 if (!err
&& sk
->sk_err
)
930 static int l2cap_sock_release(struct socket
*sock
)
932 struct sock
*sk
= sock
->sk
;
935 BT_DBG("sock %p, sk %p", sock
, sk
);
940 err
= l2cap_sock_shutdown(sock
, 2);
947 static void l2cap_sock_destruct(struct sock
*sk
)
951 skb_queue_purge(&sk
->sk_receive_queue
);
952 skb_queue_purge(&sk
->sk_write_queue
);
955 void l2cap_sock_init(struct sock
*sk
, struct sock
*parent
)
957 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
958 struct l2cap_chan
*chan
= pi
->chan
;
963 struct l2cap_chan
*pchan
= l2cap_pi(parent
)->chan
;
965 sk
->sk_type
= parent
->sk_type
;
966 bt_sk(sk
)->defer_setup
= bt_sk(parent
)->defer_setup
;
968 chan
->imtu
= pchan
->imtu
;
969 chan
->omtu
= pchan
->omtu
;
970 chan
->conf_state
= pchan
->conf_state
;
971 chan
->mode
= pchan
->mode
;
972 chan
->fcs
= pchan
->fcs
;
973 chan
->max_tx
= pchan
->max_tx
;
974 chan
->tx_win
= pchan
->tx_win
;
975 chan
->sec_level
= pchan
->sec_level
;
976 chan
->role_switch
= pchan
->role_switch
;
977 chan
->force_reliable
= pchan
->force_reliable
;
978 chan
->flushable
= pchan
->flushable
;
980 chan
->imtu
= L2CAP_DEFAULT_MTU
;
982 if (!disable_ertm
&& sk
->sk_type
== SOCK_STREAM
) {
983 chan
->mode
= L2CAP_MODE_ERTM
;
984 chan
->conf_state
|= L2CAP_CONF_STATE2_DEVICE
;
986 chan
->mode
= L2CAP_MODE_BASIC
;
988 chan
->max_tx
= L2CAP_DEFAULT_MAX_TX
;
989 chan
->fcs
= L2CAP_FCS_CRC16
;
990 chan
->tx_win
= L2CAP_DEFAULT_TX_WINDOW
;
991 chan
->sec_level
= BT_SECURITY_LOW
;
992 chan
->role_switch
= 0;
993 chan
->force_reliable
= 0;
994 chan
->flushable
= BT_FLUSHABLE_OFF
;
997 /* Default config options */
998 chan
->flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
1001 static struct proto l2cap_proto
= {
1003 .owner
= THIS_MODULE
,
1004 .obj_size
= sizeof(struct l2cap_pinfo
)
1007 struct sock
*l2cap_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
1011 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &l2cap_proto
);
1015 sock_init_data(sock
, sk
);
1016 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
1018 sk
->sk_destruct
= l2cap_sock_destruct
;
1019 sk
->sk_sndtimeo
= msecs_to_jiffies(L2CAP_CONN_TIMEOUT
);
1021 sock_reset_flag(sk
, SOCK_ZAPPED
);
1023 sk
->sk_protocol
= proto
;
1024 sk
->sk_state
= BT_OPEN
;
1026 setup_timer(&sk
->sk_timer
, l2cap_sock_timeout
, (unsigned long) sk
);
1031 static int l2cap_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
1035 struct l2cap_chan
*chan
;
1037 BT_DBG("sock %p", sock
);
1039 sock
->state
= SS_UNCONNECTED
;
1041 if (sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
&&
1042 sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
)
1043 return -ESOCKTNOSUPPORT
;
1045 if (sock
->type
== SOCK_RAW
&& !kern
&& !capable(CAP_NET_RAW
))
1048 sock
->ops
= &l2cap_sock_ops
;
1050 sk
= l2cap_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
1054 chan
= l2cap_chan_create(sk
);
1056 l2cap_sock_kill(sk
);
1060 l2cap_pi(sk
)->chan
= chan
;
1062 l2cap_sock_init(sk
, NULL
);
1066 static const struct proto_ops l2cap_sock_ops
= {
1067 .family
= PF_BLUETOOTH
,
1068 .owner
= THIS_MODULE
,
1069 .release
= l2cap_sock_release
,
1070 .bind
= l2cap_sock_bind
,
1071 .connect
= l2cap_sock_connect
,
1072 .listen
= l2cap_sock_listen
,
1073 .accept
= l2cap_sock_accept
,
1074 .getname
= l2cap_sock_getname
,
1075 .sendmsg
= l2cap_sock_sendmsg
,
1076 .recvmsg
= l2cap_sock_recvmsg
,
1077 .poll
= bt_sock_poll
,
1078 .ioctl
= bt_sock_ioctl
,
1079 .mmap
= sock_no_mmap
,
1080 .socketpair
= sock_no_socketpair
,
1081 .shutdown
= l2cap_sock_shutdown
,
1082 .setsockopt
= l2cap_sock_setsockopt
,
1083 .getsockopt
= l2cap_sock_getsockopt
1086 static const struct net_proto_family l2cap_sock_family_ops
= {
1087 .family
= PF_BLUETOOTH
,
1088 .owner
= THIS_MODULE
,
1089 .create
= l2cap_sock_create
,
1092 int __init
l2cap_init_sockets(void)
1096 err
= proto_register(&l2cap_proto
, 0);
1100 err
= bt_sock_register(BTPROTO_L2CAP
, &l2cap_sock_family_ops
);
1104 BT_INFO("L2CAP socket layer initialized");
1109 BT_ERR("L2CAP socket registration failed");
1110 proto_unregister(&l2cap_proto
);
1114 void l2cap_cleanup_sockets(void)
1116 if (bt_sock_unregister(BTPROTO_L2CAP
) < 0)
1117 BT_ERR("L2CAP socket unregistration failed");
1119 proto_unregister(&l2cap_proto
);