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>
32 #include <net/bluetooth/smp.h>
34 static const struct proto_ops l2cap_sock_ops
;
35 static void l2cap_sock_init(struct sock
*sk
, struct sock
*parent
);
36 static struct sock
*l2cap_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
);
38 static int l2cap_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int alen
)
40 struct sock
*sk
= sock
->sk
;
41 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
42 struct sockaddr_l2 la
;
47 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
50 memset(&la
, 0, sizeof(la
));
51 len
= min_t(unsigned int, sizeof(la
), alen
);
52 memcpy(&la
, addr
, len
);
54 if (la
.l2_cid
&& la
.l2_psm
)
59 if (sk
->sk_state
!= BT_OPEN
) {
65 __u16 psm
= __le16_to_cpu(la
.l2_psm
);
67 /* PSM must be odd and lsb of upper byte must be 0 */
68 if ((psm
& 0x0101) != 0x0001) {
73 /* Restrict usage of well-known PSMs */
74 if (psm
< 0x1001 && !capable(CAP_NET_BIND_SERVICE
)) {
81 err
= l2cap_add_scid(chan
, la
.l2_cid
);
83 err
= l2cap_add_psm(chan
, &la
.l2_bdaddr
, la
.l2_psm
);
88 if (__le16_to_cpu(la
.l2_psm
) == 0x0001 ||
89 __le16_to_cpu(la
.l2_psm
) == 0x0003)
90 chan
->sec_level
= BT_SECURITY_SDP
;
92 bacpy(&bt_sk(sk
)->src
, &la
.l2_bdaddr
);
94 chan
->state
= BT_BOUND
;
95 sk
->sk_state
= BT_BOUND
;
102 static int l2cap_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
104 struct sock
*sk
= sock
->sk
;
105 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
106 struct sockaddr_l2 la
;
111 if (!addr
|| alen
< sizeof(addr
->sa_family
) ||
112 addr
->sa_family
!= AF_BLUETOOTH
)
115 memset(&la
, 0, sizeof(la
));
116 len
= min_t(unsigned int, sizeof(la
), alen
);
117 memcpy(&la
, addr
, len
);
119 if (la
.l2_cid
&& la
.l2_psm
)
124 if (chan
->chan_type
== L2CAP_CHAN_CONN_ORIENTED
125 && !(la
.l2_psm
|| la
.l2_cid
)) {
130 switch (chan
->mode
) {
131 case L2CAP_MODE_BASIC
:
133 case L2CAP_MODE_ERTM
:
134 case L2CAP_MODE_STREAMING
:
143 switch (sk
->sk_state
) {
147 /* Already connecting */
151 /* Already connected */
165 /* PSM must be odd and lsb of upper byte must be 0 */
166 if ((__le16_to_cpu(la
.l2_psm
) & 0x0101) != 0x0001 && !la
.l2_cid
&&
167 chan
->chan_type
!= L2CAP_CHAN_RAW
) {
172 /* Set destination address and psm */
173 bacpy(&bt_sk(sk
)->dst
, &la
.l2_bdaddr
);
174 chan
->psm
= la
.l2_psm
;
175 chan
->dcid
= la
.l2_cid
;
177 err
= l2cap_chan_connect(l2cap_pi(sk
)->chan
);
182 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
183 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
189 static int l2cap_sock_listen(struct socket
*sock
, int backlog
)
191 struct sock
*sk
= sock
->sk
;
192 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
195 BT_DBG("sk %p backlog %d", sk
, backlog
);
199 if ((sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
)
200 || sk
->sk_state
!= BT_BOUND
) {
205 switch (chan
->mode
) {
206 case L2CAP_MODE_BASIC
:
208 case L2CAP_MODE_ERTM
:
209 case L2CAP_MODE_STREAMING
:
218 sk
->sk_max_ack_backlog
= backlog
;
219 sk
->sk_ack_backlog
= 0;
221 chan
->state
= BT_LISTEN
;
222 sk
->sk_state
= BT_LISTEN
;
229 static int l2cap_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
231 DECLARE_WAITQUEUE(wait
, current
);
232 struct sock
*sk
= sock
->sk
, *nsk
;
236 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
238 if (sk
->sk_state
!= BT_LISTEN
) {
243 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
245 BT_DBG("sk %p timeo %ld", sk
, timeo
);
247 /* Wait for an incoming connection. (wake-one). */
248 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
249 while (!(nsk
= bt_accept_dequeue(sk
, newsock
))) {
250 set_current_state(TASK_INTERRUPTIBLE
);
257 timeo
= schedule_timeout(timeo
);
258 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
260 if (sk
->sk_state
!= BT_LISTEN
) {
265 if (signal_pending(current
)) {
266 err
= sock_intr_errno(timeo
);
270 set_current_state(TASK_RUNNING
);
271 remove_wait_queue(sk_sleep(sk
), &wait
);
276 newsock
->state
= SS_CONNECTED
;
278 BT_DBG("new socket %p", nsk
);
285 static int l2cap_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
287 struct sockaddr_l2
*la
= (struct sockaddr_l2
*) addr
;
288 struct sock
*sk
= sock
->sk
;
289 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
291 BT_DBG("sock %p, sk %p", sock
, sk
);
293 addr
->sa_family
= AF_BLUETOOTH
;
294 *len
= sizeof(struct sockaddr_l2
);
297 la
->l2_psm
= chan
->psm
;
298 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->dst
);
299 la
->l2_cid
= cpu_to_le16(chan
->dcid
);
301 la
->l2_psm
= chan
->sport
;
302 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->src
);
303 la
->l2_cid
= cpu_to_le16(chan
->scid
);
309 static int l2cap_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
311 struct sock
*sk
= sock
->sk
;
312 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
313 struct l2cap_options opts
;
314 struct l2cap_conninfo cinfo
;
320 if (get_user(len
, optlen
))
327 memset(&opts
, 0, sizeof(opts
));
328 opts
.imtu
= chan
->imtu
;
329 opts
.omtu
= chan
->omtu
;
330 opts
.flush_to
= chan
->flush_to
;
331 opts
.mode
= chan
->mode
;
332 opts
.fcs
= chan
->fcs
;
333 opts
.max_tx
= chan
->max_tx
;
334 opts
.txwin_size
= (__u16
)chan
->tx_win
;
336 len
= min_t(unsigned int, len
, sizeof(opts
));
337 if (copy_to_user(optval
, (char *) &opts
, len
))
343 switch (chan
->sec_level
) {
344 case BT_SECURITY_LOW
:
347 case BT_SECURITY_MEDIUM
:
348 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
;
350 case BT_SECURITY_HIGH
:
351 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
|
359 if (chan
->role_switch
)
360 opt
|= L2CAP_LM_MASTER
;
362 if (chan
->force_reliable
)
363 opt
|= L2CAP_LM_RELIABLE
;
365 if (put_user(opt
, (u32 __user
*) optval
))
370 if (sk
->sk_state
!= BT_CONNECTED
&&
371 !(sk
->sk_state
== BT_CONNECT2
&&
372 bt_sk(sk
)->defer_setup
)) {
377 memset(&cinfo
, 0, sizeof(cinfo
));
378 cinfo
.hci_handle
= chan
->conn
->hcon
->handle
;
379 memcpy(cinfo
.dev_class
, chan
->conn
->hcon
->dev_class
, 3);
381 len
= min_t(unsigned int, len
, sizeof(cinfo
));
382 if (copy_to_user(optval
, (char *) &cinfo
, len
))
396 static int l2cap_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
398 struct sock
*sk
= sock
->sk
;
399 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
400 struct bt_security sec
;
406 if (level
== SOL_L2CAP
)
407 return l2cap_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
409 if (level
!= SOL_BLUETOOTH
)
412 if (get_user(len
, optlen
))
419 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
&&
420 chan
->chan_type
!= L2CAP_CHAN_RAW
) {
425 memset(&sec
, 0, sizeof(sec
));
426 sec
.level
= chan
->sec_level
;
428 if (sk
->sk_state
== BT_CONNECTED
)
429 sec
.key_size
= chan
->conn
->hcon
->enc_key_size
;
431 len
= min_t(unsigned int, len
, sizeof(sec
));
432 if (copy_to_user(optval
, (char *) &sec
, len
))
438 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
443 if (put_user(bt_sk(sk
)->defer_setup
, (u32 __user
*) optval
))
449 if (put_user(chan
->flushable
, (u32 __user
*) optval
))
455 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
456 && sk
->sk_type
!= SOCK_RAW
) {
461 pwr
.force_active
= chan
->force_active
;
463 len
= min_t(unsigned int, len
, sizeof(pwr
));
464 if (copy_to_user(optval
, (char *) &pwr
, len
))
478 static int l2cap_sock_setsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, unsigned int optlen
)
480 struct sock
*sk
= sock
->sk
;
481 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
482 struct l2cap_options opts
;
492 if (sk
->sk_state
== BT_CONNECTED
) {
497 opts
.imtu
= chan
->imtu
;
498 opts
.omtu
= chan
->omtu
;
499 opts
.flush_to
= chan
->flush_to
;
500 opts
.mode
= chan
->mode
;
501 opts
.fcs
= chan
->fcs
;
502 opts
.max_tx
= chan
->max_tx
;
503 opts
.txwin_size
= (__u16
)chan
->tx_win
;
505 len
= min_t(unsigned int, sizeof(opts
), optlen
);
506 if (copy_from_user((char *) &opts
, optval
, len
)) {
511 if (opts
.txwin_size
> L2CAP_DEFAULT_TX_WINDOW
) {
516 chan
->mode
= opts
.mode
;
517 switch (chan
->mode
) {
518 case L2CAP_MODE_BASIC
:
519 clear_bit(CONF_STATE2_DEVICE
, &chan
->conf_state
);
521 case L2CAP_MODE_ERTM
:
522 case L2CAP_MODE_STREAMING
:
531 chan
->imtu
= opts
.imtu
;
532 chan
->omtu
= opts
.omtu
;
533 chan
->fcs
= opts
.fcs
;
534 chan
->max_tx
= opts
.max_tx
;
535 chan
->tx_win
= (__u8
)opts
.txwin_size
;
539 if (get_user(opt
, (u32 __user
*) optval
)) {
544 if (opt
& L2CAP_LM_AUTH
)
545 chan
->sec_level
= BT_SECURITY_LOW
;
546 if (opt
& L2CAP_LM_ENCRYPT
)
547 chan
->sec_level
= BT_SECURITY_MEDIUM
;
548 if (opt
& L2CAP_LM_SECURE
)
549 chan
->sec_level
= BT_SECURITY_HIGH
;
551 chan
->role_switch
= (opt
& L2CAP_LM_MASTER
);
552 chan
->force_reliable
= (opt
& L2CAP_LM_RELIABLE
);
564 static int l2cap_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
566 struct sock
*sk
= sock
->sk
;
567 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
568 struct bt_security sec
;
570 struct l2cap_conn
*conn
;
576 if (level
== SOL_L2CAP
)
577 return l2cap_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
579 if (level
!= SOL_BLUETOOTH
)
586 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
&&
587 chan
->chan_type
!= L2CAP_CHAN_RAW
) {
592 sec
.level
= BT_SECURITY_LOW
;
594 len
= min_t(unsigned int, sizeof(sec
), optlen
);
595 if (copy_from_user((char *) &sec
, optval
, len
)) {
600 if (sec
.level
< BT_SECURITY_LOW
||
601 sec
.level
> BT_SECURITY_HIGH
) {
606 chan
->sec_level
= sec
.level
;
609 if (conn
&& chan
->scid
== L2CAP_CID_LE_DATA
) {
610 if (!conn
->hcon
->out
) {
615 if (smp_conn_security(conn
, sec
.level
))
619 sk
->sk_state
= BT_CONFIG
;
624 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
629 if (get_user(opt
, (u32 __user
*) optval
)) {
634 bt_sk(sk
)->defer_setup
= opt
;
638 if (get_user(opt
, (u32 __user
*) optval
)) {
643 if (opt
> BT_FLUSHABLE_ON
) {
648 if (opt
== BT_FLUSHABLE_OFF
) {
649 struct l2cap_conn
*conn
= chan
->conn
;
650 /* proceed further only when we have l2cap_conn and
651 No Flush support in the LM */
652 if (!conn
|| !lmp_no_flush_capable(conn
->hcon
->hdev
)) {
658 chan
->flushable
= opt
;
662 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
&&
663 chan
->chan_type
!= L2CAP_CHAN_RAW
) {
668 pwr
.force_active
= BT_POWER_FORCE_ACTIVE_ON
;
670 len
= min_t(unsigned int, sizeof(pwr
), optlen
);
671 if (copy_from_user((char *) &pwr
, optval
, len
)) {
675 chan
->force_active
= pwr
.force_active
;
687 static int l2cap_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
)
689 struct sock
*sk
= sock
->sk
;
690 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
693 BT_DBG("sock %p, sk %p", sock
, sk
);
695 err
= sock_error(sk
);
699 if (msg
->msg_flags
& MSG_OOB
)
704 if (sk
->sk_state
!= BT_CONNECTED
) {
709 err
= l2cap_chan_send(chan
, msg
, len
);
715 static int l2cap_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*msg
, size_t len
, int flags
)
717 struct sock
*sk
= sock
->sk
;
718 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
723 if (sk
->sk_state
== BT_CONNECT2
&& bt_sk(sk
)->defer_setup
) {
724 sk
->sk_state
= BT_CONFIG
;
726 __l2cap_connect_rsp_defer(pi
->chan
);
733 if (sock
->type
== SOCK_STREAM
)
734 err
= bt_sock_stream_recvmsg(iocb
, sock
, msg
, len
, flags
);
736 err
= bt_sock_recvmsg(iocb
, sock
, msg
, len
, flags
);
738 if (pi
->chan
->mode
!= L2CAP_MODE_ERTM
)
741 /* Attempt to put pending rx data in the socket buffer */
745 if (!test_bit(CONN_LOCAL_BUSY
, &pi
->chan
->conn_state
))
748 if (pi
->rx_busy_skb
) {
749 if (!sock_queue_rcv_skb(sk
, pi
->rx_busy_skb
))
750 pi
->rx_busy_skb
= NULL
;
755 /* Restore data flow when half of the receive buffer is
756 * available. This avoids resending large numbers of
759 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
>> 1)
760 l2cap_chan_busy(pi
->chan
, 0);
767 /* Kill socket (only if zapped and orphan)
768 * Must be called on unlocked socket.
770 static void l2cap_sock_kill(struct sock
*sk
)
772 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
775 BT_DBG("sk %p state %d", sk
, sk
->sk_state
);
777 /* Kill poor orphan */
779 l2cap_chan_destroy(l2cap_pi(sk
)->chan
);
780 sock_set_flag(sk
, SOCK_DEAD
);
784 static int l2cap_sock_shutdown(struct socket
*sock
, int how
)
786 struct sock
*sk
= sock
->sk
;
787 struct l2cap_chan
*chan
= l2cap_pi(sk
)->chan
;
790 BT_DBG("sock %p, sk %p", sock
, sk
);
796 if (!sk
->sk_shutdown
) {
797 if (chan
->mode
== L2CAP_MODE_ERTM
)
798 err
= __l2cap_wait_ack(sk
);
800 sk
->sk_shutdown
= SHUTDOWN_MASK
;
801 l2cap_chan_close(chan
, 0);
803 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
804 err
= bt_sock_wait_state(sk
, BT_CLOSED
,
808 if (!err
&& sk
->sk_err
)
815 static int l2cap_sock_release(struct socket
*sock
)
817 struct sock
*sk
= sock
->sk
;
820 BT_DBG("sock %p, sk %p", sock
, sk
);
825 err
= l2cap_sock_shutdown(sock
, 2);
832 static struct l2cap_chan
*l2cap_sock_new_connection_cb(void *data
)
834 struct sock
*sk
, *parent
= data
;
836 sk
= l2cap_sock_alloc(sock_net(parent
), NULL
, BTPROTO_L2CAP
,
841 l2cap_sock_init(sk
, parent
);
843 return l2cap_pi(sk
)->chan
;
846 static int l2cap_sock_recv_cb(void *data
, struct sk_buff
*skb
)
849 struct sock
*sk
= data
;
850 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
855 err
= sock_queue_rcv_skb(sk
, skb
);
857 /* For ERTM, handle one skb that doesn't fit into the recv
858 * buffer. This is important to do because the data frames
859 * have already been acked, so the skb cannot be discarded.
861 * Notify the l2cap core that the buffer is full, so the
862 * LOCAL_BUSY state is entered and no more frames are
863 * acked and reassembled until there is buffer space
866 if (err
< 0 && pi
->chan
->mode
== L2CAP_MODE_ERTM
) {
867 pi
->rx_busy_skb
= skb
;
868 l2cap_chan_busy(pi
->chan
, 1);
875 static void l2cap_sock_close_cb(void *data
)
877 struct sock
*sk
= data
;
882 static void l2cap_sock_state_change_cb(void *data
, int state
)
884 struct sock
*sk
= data
;
886 sk
->sk_state
= state
;
889 static struct l2cap_ops l2cap_chan_ops
= {
890 .name
= "L2CAP Socket Interface",
891 .new_connection
= l2cap_sock_new_connection_cb
,
892 .recv
= l2cap_sock_recv_cb
,
893 .close
= l2cap_sock_close_cb
,
894 .state_change
= l2cap_sock_state_change_cb
,
897 static void l2cap_sock_destruct(struct sock
*sk
)
901 if (l2cap_pi(sk
)->rx_busy_skb
) {
902 kfree_skb(l2cap_pi(sk
)->rx_busy_skb
);
903 l2cap_pi(sk
)->rx_busy_skb
= NULL
;
906 skb_queue_purge(&sk
->sk_receive_queue
);
907 skb_queue_purge(&sk
->sk_write_queue
);
910 static void l2cap_sock_init(struct sock
*sk
, struct sock
*parent
)
912 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
913 struct l2cap_chan
*chan
= pi
->chan
;
918 struct l2cap_chan
*pchan
= l2cap_pi(parent
)->chan
;
920 sk
->sk_type
= parent
->sk_type
;
921 bt_sk(sk
)->defer_setup
= bt_sk(parent
)->defer_setup
;
923 chan
->chan_type
= pchan
->chan_type
;
924 chan
->imtu
= pchan
->imtu
;
925 chan
->omtu
= pchan
->omtu
;
926 chan
->conf_state
= pchan
->conf_state
;
927 chan
->mode
= pchan
->mode
;
928 chan
->fcs
= pchan
->fcs
;
929 chan
->max_tx
= pchan
->max_tx
;
930 chan
->tx_win
= pchan
->tx_win
;
931 chan
->sec_level
= pchan
->sec_level
;
932 chan
->role_switch
= pchan
->role_switch
;
933 chan
->force_reliable
= pchan
->force_reliable
;
934 chan
->flushable
= pchan
->flushable
;
935 chan
->force_active
= pchan
->force_active
;
938 switch (sk
->sk_type
) {
940 chan
->chan_type
= L2CAP_CHAN_RAW
;
943 chan
->chan_type
= L2CAP_CHAN_CONN_LESS
;
947 chan
->chan_type
= L2CAP_CHAN_CONN_ORIENTED
;
951 chan
->imtu
= L2CAP_DEFAULT_MTU
;
953 if (!disable_ertm
&& sk
->sk_type
== SOCK_STREAM
) {
954 chan
->mode
= L2CAP_MODE_ERTM
;
955 set_bit(CONF_STATE2_DEVICE
, &chan
->conf_state
);
957 chan
->mode
= L2CAP_MODE_BASIC
;
959 chan
->max_tx
= L2CAP_DEFAULT_MAX_TX
;
960 chan
->fcs
= L2CAP_FCS_CRC16
;
961 chan
->tx_win
= L2CAP_DEFAULT_TX_WINDOW
;
962 chan
->sec_level
= BT_SECURITY_LOW
;
963 chan
->role_switch
= 0;
964 chan
->force_reliable
= 0;
965 chan
->flushable
= BT_FLUSHABLE_OFF
;
966 chan
->force_active
= BT_POWER_FORCE_ACTIVE_ON
;
970 /* Default config options */
971 chan
->flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
974 chan
->ops
= &l2cap_chan_ops
;
977 static struct proto l2cap_proto
= {
979 .owner
= THIS_MODULE
,
980 .obj_size
= sizeof(struct l2cap_pinfo
)
983 static struct sock
*l2cap_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
986 struct l2cap_chan
*chan
;
988 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &l2cap_proto
);
992 sock_init_data(sock
, sk
);
993 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
995 sk
->sk_destruct
= l2cap_sock_destruct
;
996 sk
->sk_sndtimeo
= msecs_to_jiffies(L2CAP_CONN_TIMEOUT
);
998 sock_reset_flag(sk
, SOCK_ZAPPED
);
1000 sk
->sk_protocol
= proto
;
1001 sk
->sk_state
= BT_OPEN
;
1003 chan
= l2cap_chan_create(sk
);
1005 l2cap_sock_kill(sk
);
1009 l2cap_pi(sk
)->chan
= chan
;
1014 static int l2cap_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
1019 BT_DBG("sock %p", sock
);
1021 sock
->state
= SS_UNCONNECTED
;
1023 if (sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
&&
1024 sock
->type
!= SOCK_DGRAM
&& sock
->type
!= SOCK_RAW
)
1025 return -ESOCKTNOSUPPORT
;
1027 if (sock
->type
== SOCK_RAW
&& !kern
&& !capable(CAP_NET_RAW
))
1030 sock
->ops
= &l2cap_sock_ops
;
1032 sk
= l2cap_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
1036 l2cap_sock_init(sk
, NULL
);
1040 static const struct proto_ops l2cap_sock_ops
= {
1041 .family
= PF_BLUETOOTH
,
1042 .owner
= THIS_MODULE
,
1043 .release
= l2cap_sock_release
,
1044 .bind
= l2cap_sock_bind
,
1045 .connect
= l2cap_sock_connect
,
1046 .listen
= l2cap_sock_listen
,
1047 .accept
= l2cap_sock_accept
,
1048 .getname
= l2cap_sock_getname
,
1049 .sendmsg
= l2cap_sock_sendmsg
,
1050 .recvmsg
= l2cap_sock_recvmsg
,
1051 .poll
= bt_sock_poll
,
1052 .ioctl
= bt_sock_ioctl
,
1053 .mmap
= sock_no_mmap
,
1054 .socketpair
= sock_no_socketpair
,
1055 .shutdown
= l2cap_sock_shutdown
,
1056 .setsockopt
= l2cap_sock_setsockopt
,
1057 .getsockopt
= l2cap_sock_getsockopt
1060 static const struct net_proto_family l2cap_sock_family_ops
= {
1061 .family
= PF_BLUETOOTH
,
1062 .owner
= THIS_MODULE
,
1063 .create
= l2cap_sock_create
,
1066 int __init
l2cap_init_sockets(void)
1070 err
= proto_register(&l2cap_proto
, 0);
1074 err
= bt_sock_register(BTPROTO_L2CAP
, &l2cap_sock_family_ops
);
1078 BT_INFO("L2CAP socket layer initialized");
1083 BT_ERR("L2CAP socket registration failed");
1084 proto_unregister(&l2cap_proto
);
1088 void l2cap_cleanup_sockets(void)
1090 if (bt_sock_unregister(BTPROTO_L2CAP
) < 0)
1091 BT_ERR("L2CAP socket unregistration failed");
1093 proto_unregister(&l2cap_proto
);