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
;
50 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
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
)
62 if (sk
->sk_state
!= BT_OPEN
) {
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) {
76 /* Restrict usage of well-known PSMs */
77 if (psm
< 0x1001 && !capable(CAP_NET_BIND_SERVICE
)) {
84 err
= l2cap_add_scid(chan
, la
.l2_cid
);
86 err
= l2cap_add_psm(chan
, &la
.l2_bdaddr
, la
.l2_psm
);
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
;
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
;
114 if (!addr
|| alen
< sizeof(addr
->sa_family
) ||
115 addr
->sa_family
!= AF_BLUETOOTH
)
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
)
127 if (chan
->chan_type
== L2CAP_CHAN_CONN_ORIENTED
128 && !(la
.l2_psm
|| la
.l2_cid
)) {
133 switch (chan
->mode
) {
134 case L2CAP_MODE_BASIC
:
136 case L2CAP_MODE_ERTM
:
137 case L2CAP_MODE_STREAMING
:
146 switch (sk
->sk_state
) {
150 /* Already connecting */
154 /* Already connected */
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
) {
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
);
185 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
186 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
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
;
198 BT_DBG("sk %p backlog %d", sk
, backlog
);
202 if ((sock
->type
!= SOCK_SEQPACKET
&& sock
->type
!= SOCK_STREAM
)
203 || sk
->sk_state
!= BT_BOUND
) {
208 switch (chan
->mode
) {
209 case L2CAP_MODE_BASIC
:
211 case L2CAP_MODE_ERTM
:
212 case L2CAP_MODE_STREAMING
:
221 sk
->sk_max_ack_backlog
= backlog
;
222 sk
->sk_ack_backlog
= 0;
224 chan
->state
= BT_LISTEN
;
225 sk
->sk_state
= BT_LISTEN
;
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
;
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
);
248 set_current_state(TASK_INTERRUPTIBLE
);
250 if (sk
->sk_state
!= BT_LISTEN
) {
255 nsk
= bt_accept_dequeue(sk
, newsock
);
264 if (signal_pending(current
)) {
265 err
= sock_intr_errno(timeo
);
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
);
279 newsock
->state
= SS_CONNECTED
;
281 BT_DBG("new socket %p", nsk
);
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
);
300 la
->l2_psm
= chan
->psm
;
301 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->dst
);
302 la
->l2_cid
= cpu_to_le16(chan
->dcid
);
304 la
->l2_psm
= chan
->sport
;
305 bacpy(&la
->l2_bdaddr
, &bt_sk(sk
)->src
);
306 la
->l2_cid
= cpu_to_le16(chan
->scid
);
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
;
323 if (get_user(len
, optlen
))
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
))
346 switch (chan
->sec_level
) {
347 case BT_SECURITY_LOW
:
350 case BT_SECURITY_MEDIUM
:
351 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
;
353 case BT_SECURITY_HIGH
:
354 opt
= L2CAP_LM_AUTH
| L2CAP_LM_ENCRYPT
|
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
))
373 if (sk
->sk_state
!= BT_CONNECTED
&&
374 !(sk
->sk_state
== BT_CONNECT2
&&
375 bt_sk(sk
)->defer_setup
)) {
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
))
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
;
409 if (level
== SOL_L2CAP
)
410 return l2cap_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
412 if (level
!= SOL_BLUETOOTH
)
415 if (get_user(len
, optlen
))
422 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
&&
423 chan
->chan_type
!= L2CAP_CHAN_RAW
) {
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
))
441 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
446 if (put_user(bt_sk(sk
)->defer_setup
, (u32 __user
*) optval
))
452 if (put_user(chan
->flushable
, (u32 __user
*) optval
))
458 if (sk
->sk_type
!= SOCK_SEQPACKET
&& sk
->sk_type
!= SOCK_STREAM
459 && sk
->sk_type
!= SOCK_RAW
) {
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
))
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
;
495 if (sk
->sk_state
== BT_CONNECTED
) {
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
)) {
514 if (opts
.txwin_size
> L2CAP_DEFAULT_TX_WINDOW
) {
519 chan
->mode
= opts
.mode
;
520 switch (chan
->mode
) {
521 case L2CAP_MODE_BASIC
:
522 clear_bit(CONF_STATE2_DEVICE
, &chan
->conf_state
);
524 case L2CAP_MODE_ERTM
:
525 case L2CAP_MODE_STREAMING
:
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
;
542 if (get_user(opt
, (u32 __user
*) optval
)) {
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
);
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
;
573 struct l2cap_conn
*conn
;
579 if (level
== SOL_L2CAP
)
580 return l2cap_sock_setsockopt_old(sock
, optname
, optval
, optlen
);
582 if (level
!= SOL_BLUETOOTH
)
589 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
&&
590 chan
->chan_type
!= L2CAP_CHAN_RAW
) {
595 sec
.level
= BT_SECURITY_LOW
;
597 len
= min_t(unsigned int, sizeof(sec
), optlen
);
598 if (copy_from_user((char *) &sec
, optval
, len
)) {
603 if (sec
.level
< BT_SECURITY_LOW
||
604 sec
.level
> BT_SECURITY_HIGH
) {
609 chan
->sec_level
= sec
.level
;
612 if (conn
&& chan
->scid
== L2CAP_CID_LE_DATA
) {
613 if (!conn
->hcon
->out
) {
618 if (smp_conn_security(conn
, sec
.level
))
622 sk
->sk_state
= BT_CONFIG
;
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
;
665 if (chan
->chan_type
!= L2CAP_CHAN_CONN_ORIENTED
&&
666 chan
->chan_type
!= L2CAP_CHAN_RAW
) {
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
)) {
678 chan
->force_active
= pwr
.force_active
;
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
;
696 BT_DBG("sock %p, sk %p", sock
, sk
);
698 err
= sock_error(sk
);
702 if (msg
->msg_flags
& MSG_OOB
)
707 if (sk
->sk_state
!= BT_CONNECTED
) {
712 err
= l2cap_chan_send(chan
, msg
, len
);
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
);
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
);
736 if (sock
->type
== SOCK_STREAM
)
737 err
= bt_sock_stream_recvmsg(iocb
, sock
, msg
, len
, flags
);
739 err
= bt_sock_recvmsg(iocb
, sock
, msg
, len
, flags
);
741 if (pi
->chan
->mode
!= L2CAP_MODE_ERTM
)
744 /* Attempt to put pending rx data in the socket buffer */
748 if (!test_bit(CONN_LOCAL_BUSY
, &pi
->chan
->conn_state
))
751 if (pi
->rx_busy_skb
) {
752 if (!sock_queue_rcv_skb(sk
, pi
->rx_busy_skb
))
753 pi
->rx_busy_skb
= NULL
;
758 /* Restore data flow when half of the receive buffer is
759 * available. This avoids resending large numbers of
762 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
>> 1)
763 l2cap_chan_busy(pi
->chan
, 0);
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
)
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
);
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
;
793 BT_DBG("sock %p, sk %p", 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
,
811 if (!err
&& sk
->sk_err
)
818 static int l2cap_sock_release(struct socket
*sock
)
820 struct sock
*sk
= sock
->sk
;
823 BT_DBG("sock %p, sk %p", sock
, sk
);
828 err
= l2cap_sock_shutdown(sock
, 2);
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
,
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
)
852 struct sock
*sk
= data
;
853 struct l2cap_pinfo
*pi
= l2cap_pi(sk
);
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
869 if (err
< 0 && pi
->chan
->mode
== L2CAP_MODE_ERTM
) {
870 pi
->rx_busy_skb
= skb
;
871 l2cap_chan_busy(pi
->chan
, 1);
878 static void l2cap_sock_close_cb(void *data
)
880 struct sock
*sk
= data
;
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
)
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
;
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
);
943 switch (sk
->sk_type
) {
945 chan
->chan_type
= L2CAP_CHAN_RAW
;
948 chan
->chan_type
= L2CAP_CHAN_CONN_LESS
;
952 chan
->chan_type
= L2CAP_CHAN_CONN_ORIENTED
;
956 chan
->imtu
= L2CAP_DEFAULT_MTU
;
958 if (!disable_ertm
&& sk
->sk_type
== SOCK_STREAM
) {
959 chan
->mode
= L2CAP_MODE_ERTM
;
960 set_bit(CONF_STATE2_DEVICE
, &chan
->conf_state
);
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
;
979 chan
->ops
= &l2cap_chan_ops
;
982 static struct proto l2cap_proto
= {
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
)
991 struct l2cap_chan
*chan
;
993 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &l2cap_proto
);
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
);
1010 l2cap_sock_kill(sk
);
1014 l2cap_pi(sk
)->chan
= chan
;
1019 static int l2cap_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
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
))
1035 sock
->ops
= &l2cap_sock_ops
;
1037 sk
= l2cap_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
1041 l2cap_sock_init(sk
, NULL
);
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)
1075 err
= proto_register(&l2cap_proto
, 0);
1079 err
= bt_sock_register(BTPROTO_L2CAP
, &l2cap_sock_family_ops
);
1083 BT_INFO("L2CAP socket layer initialized");
1088 BT_ERR("L2CAP socket registration failed");
1089 proto_unregister(&l2cap_proto
);
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
);