2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/spinlock.h>
14 #include <linux/mutex.h>
15 #include <linux/list.h>
16 #include <linux/wait.h>
17 #include <linux/poll.h>
18 #include <linux/tcp.h>
19 #include <linux/uaccess.h>
20 #include <linux/debugfs.h>
21 #include <linux/caif/caif_socket.h>
22 #include <asm/atomic.h>
24 #include <net/tcp_states.h>
25 #include <net/caif/caif_layer.h>
26 #include <net/caif/caif_dev.h>
27 #include <net/caif/cfpkt.h>
29 MODULE_LICENSE("GPL");
30 MODULE_ALIAS_NETPROTO(AF_CAIF
);
33 * CAIF state is re-using the TCP socket states.
34 * caif_states stored in sk_state reflect the state as reported by
35 * the CAIF stack, while sk_socket->state is the state of the socket.
38 CAIF_CONNECTED
= TCP_ESTABLISHED
,
39 CAIF_CONNECTING
= TCP_SYN_SENT
,
40 CAIF_DISCONNECTED
= TCP_CLOSE
43 #define TX_FLOW_ON_BIT 1
44 #define RX_FLOW_ON_BIT 2
46 static struct dentry
*debugfsdir
;
48 #ifdef CONFIG_DEBUG_FS
49 struct debug_fs_counter
{
50 atomic_t caif_nr_socks
;
51 atomic_t num_connect_req
;
52 atomic_t num_connect_resp
;
53 atomic_t num_connect_fail_resp
;
54 atomic_t num_disconnect
;
55 atomic_t num_remote_shutdown_ind
;
56 atomic_t num_tx_flow_off_ind
;
57 atomic_t num_tx_flow_on_ind
;
58 atomic_t num_rx_flow_off
;
59 atomic_t num_rx_flow_on
;
61 static struct debug_fs_counter cnt
;
62 #define dbfs_atomic_inc(v) atomic_inc(v)
63 #define dbfs_atomic_dec(v) atomic_dec(v)
65 #define dbfs_atomic_inc(v)
66 #define dbfs_atomic_dec(v)
70 struct sock sk
; /* must be first member */
72 char name
[CAIF_LAYER_NAME_SZ
]; /* Used for debugging */
74 struct caif_connect_request conn_req
;
75 struct mutex readlock
;
76 struct dentry
*debugfs_socket_dir
;
77 int headroom
, tailroom
, maxframe
;
80 static int rx_flow_is_on(struct caifsock
*cf_sk
)
82 return test_bit(RX_FLOW_ON_BIT
,
83 (void *) &cf_sk
->flow_state
);
86 static int tx_flow_is_on(struct caifsock
*cf_sk
)
88 return test_bit(TX_FLOW_ON_BIT
,
89 (void *) &cf_sk
->flow_state
);
92 static void set_rx_flow_off(struct caifsock
*cf_sk
)
94 clear_bit(RX_FLOW_ON_BIT
,
95 (void *) &cf_sk
->flow_state
);
98 static void set_rx_flow_on(struct caifsock
*cf_sk
)
100 set_bit(RX_FLOW_ON_BIT
,
101 (void *) &cf_sk
->flow_state
);
104 static void set_tx_flow_off(struct caifsock
*cf_sk
)
106 clear_bit(TX_FLOW_ON_BIT
,
107 (void *) &cf_sk
->flow_state
);
110 static void set_tx_flow_on(struct caifsock
*cf_sk
)
112 set_bit(TX_FLOW_ON_BIT
,
113 (void *) &cf_sk
->flow_state
);
116 static void caif_read_lock(struct sock
*sk
)
118 struct caifsock
*cf_sk
;
119 cf_sk
= container_of(sk
, struct caifsock
, sk
);
120 mutex_lock(&cf_sk
->readlock
);
123 static void caif_read_unlock(struct sock
*sk
)
125 struct caifsock
*cf_sk
;
126 cf_sk
= container_of(sk
, struct caifsock
, sk
);
127 mutex_unlock(&cf_sk
->readlock
);
130 static int sk_rcvbuf_lowwater(struct caifsock
*cf_sk
)
132 /* A quarter of full buffer is used a low water mark */
133 return cf_sk
->sk
.sk_rcvbuf
/ 4;
136 static void caif_flow_ctrl(struct sock
*sk
, int mode
)
138 struct caifsock
*cf_sk
;
139 cf_sk
= container_of(sk
, struct caifsock
, sk
);
140 if (cf_sk
->layer
.dn
&& cf_sk
->layer
.dn
->modemcmd
)
141 cf_sk
->layer
.dn
->modemcmd(cf_sk
->layer
.dn
, mode
);
145 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
146 * not dropped, but CAIF is sending flow off instead.
148 static int caif_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
153 struct sk_buff_head
*list
= &sk
->sk_receive_queue
;
154 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
156 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
157 (unsigned)sk
->sk_rcvbuf
&& rx_flow_is_on(cf_sk
)) {
158 pr_debug("sending flow OFF (queue len = %d %d)\n",
159 atomic_read(&cf_sk
->sk
.sk_rmem_alloc
),
160 sk_rcvbuf_lowwater(cf_sk
));
161 set_rx_flow_off(cf_sk
);
162 dbfs_atomic_inc(&cnt
.num_rx_flow_off
);
163 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_OFF_REQ
);
166 err
= sk_filter(sk
, skb
);
169 if (!sk_rmem_schedule(sk
, skb
->truesize
) && rx_flow_is_on(cf_sk
)) {
170 set_rx_flow_off(cf_sk
);
171 pr_debug("sending flow OFF due to rmem_schedule\n");
172 dbfs_atomic_inc(&cnt
.num_rx_flow_off
);
173 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_OFF_REQ
);
176 skb_set_owner_r(skb
, sk
);
177 /* Cache the SKB length before we tack it onto the receive
178 * queue. Once it is added it no longer belongs to us and
179 * may be freed by other threads of control pulling packets
183 spin_lock_irqsave(&list
->lock
, flags
);
184 if (!sock_flag(sk
, SOCK_DEAD
))
185 __skb_queue_tail(list
, skb
);
186 spin_unlock_irqrestore(&list
->lock
, flags
);
188 if (!sock_flag(sk
, SOCK_DEAD
))
189 sk
->sk_data_ready(sk
, skb_len
);
195 /* Packet Receive Callback function called from CAIF Stack */
196 static int caif_sktrecv_cb(struct cflayer
*layr
, struct cfpkt
*pkt
)
198 struct caifsock
*cf_sk
;
201 cf_sk
= container_of(layr
, struct caifsock
, layer
);
202 skb
= cfpkt_tonative(pkt
);
204 if (unlikely(cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
)) {
208 caif_queue_rcv_skb(&cf_sk
->sk
, skb
);
212 /* Packet Control Callback function called from CAIF */
213 static void caif_ctrl_cb(struct cflayer
*layr
,
214 enum caif_ctrlcmd flow
,
217 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
219 case CAIF_CTRLCMD_FLOW_ON_IND
:
220 /* OK from modem to start sending again */
221 dbfs_atomic_inc(&cnt
.num_tx_flow_on_ind
);
222 set_tx_flow_on(cf_sk
);
223 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
226 case CAIF_CTRLCMD_FLOW_OFF_IND
:
227 /* Modem asks us to shut up */
228 dbfs_atomic_inc(&cnt
.num_tx_flow_off_ind
);
229 set_tx_flow_off(cf_sk
);
230 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
233 case CAIF_CTRLCMD_INIT_RSP
:
234 /* We're now connected */
235 dbfs_atomic_inc(&cnt
.num_connect_resp
);
236 cf_sk
->sk
.sk_state
= CAIF_CONNECTED
;
237 set_tx_flow_on(cf_sk
);
238 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
241 case CAIF_CTRLCMD_DEINIT_RSP
:
242 /* We're now disconnected */
243 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
244 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
245 cfcnfg_release_adap_layer(&cf_sk
->layer
);
248 case CAIF_CTRLCMD_INIT_FAIL_RSP
:
249 /* Connect request failed */
250 dbfs_atomic_inc(&cnt
.num_connect_fail_resp
);
251 cf_sk
->sk
.sk_err
= ECONNREFUSED
;
252 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
253 cf_sk
->sk
.sk_shutdown
= SHUTDOWN_MASK
;
255 * Socket "standards" seems to require POLLOUT to
256 * be set at connect failure.
258 set_tx_flow_on(cf_sk
);
259 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
262 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
:
263 /* Modem has closed this connection, or device is down. */
264 dbfs_atomic_inc(&cnt
.num_remote_shutdown_ind
);
265 cf_sk
->sk
.sk_shutdown
= SHUTDOWN_MASK
;
266 cf_sk
->sk
.sk_err
= ECONNRESET
;
267 set_rx_flow_on(cf_sk
);
268 cf_sk
->sk
.sk_error_report(&cf_sk
->sk
);
272 pr_debug("Unexpected flow command %d\n", flow
);
276 static void caif_check_flow_release(struct sock
*sk
)
278 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
280 if (rx_flow_is_on(cf_sk
))
283 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk_rcvbuf_lowwater(cf_sk
)) {
284 dbfs_atomic_inc(&cnt
.num_rx_flow_on
);
285 set_rx_flow_on(cf_sk
);
286 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_ON_REQ
);
291 * Copied from unix_dgram_recvmsg, but removed credit checks,
292 * changed locking, address handling and added MSG_TRUNC.
294 static int caif_seqpkt_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
295 struct msghdr
*m
, size_t len
, int flags
)
298 struct sock
*sk
= sock
->sk
;
304 if (m
->msg_flags
&MSG_OOB
)
307 skb
= skb_recv_datagram(sk
, flags
, 0 , &ret
);
312 m
->msg_flags
|= MSG_TRUNC
;
316 ret
= skb_copy_datagram_iovec(skb
, 0, m
->msg_iov
, copylen
);
320 ret
= (flags
& MSG_TRUNC
) ? skb
->len
: copylen
;
322 skb_free_datagram(sk
, skb
);
323 caif_check_flow_release(sk
);
331 /* Copied from unix_stream_wait_data, identical except for lock call. */
332 static long caif_stream_data_wait(struct sock
*sk
, long timeo
)
338 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
340 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
342 sk
->sk_state
!= CAIF_CONNECTED
||
343 sock_flag(sk
, SOCK_DEAD
) ||
344 (sk
->sk_shutdown
& RCV_SHUTDOWN
) ||
345 signal_pending(current
) ||
349 set_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
351 timeo
= schedule_timeout(timeo
);
353 clear_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
356 finish_wait(sk_sleep(sk
), &wait
);
363 * Copied from unix_stream_recvmsg, but removed credit checks,
364 * changed locking calls, changed address handling.
366 static int caif_stream_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
367 struct msghdr
*msg
, size_t size
,
370 struct sock
*sk
= sock
->sk
;
380 msg
->msg_namelen
= 0;
383 * Lock the socket to prevent queue disordering
384 * while sleeps in memcpy_tomsg
387 if (sk
->sk_state
== CAIF_CONNECTING
)
391 target
= sock_rcvlowat(sk
, flags
&MSG_WAITALL
, size
);
392 timeo
= sock_rcvtimeo(sk
, flags
&MSG_DONTWAIT
);
399 skb
= skb_dequeue(&sk
->sk_receive_queue
);
400 caif_check_flow_release(sk
);
403 if (copied
>= target
)
406 * POSIX 1003.1g mandates this order.
408 err
= sock_error(sk
);
412 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
416 if (sk
->sk_state
!= CAIF_CONNECTED
)
418 if (sock_flag(sk
, SOCK_DEAD
))
427 caif_read_unlock(sk
);
429 timeo
= caif_stream_data_wait(sk
, timeo
);
431 if (signal_pending(current
)) {
432 err
= sock_intr_errno(timeo
);
442 chunk
= min_t(unsigned int, skb
->len
, size
);
443 if (memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) {
444 skb_queue_head(&sk
->sk_receive_queue
, skb
);
452 /* Mark read part of skb as used */
453 if (!(flags
& MSG_PEEK
)) {
454 skb_pull(skb
, chunk
);
456 /* put the skb back if we didn't use it up. */
458 skb_queue_head(&sk
->sk_receive_queue
, skb
);
465 * It is questionable, see note in unix_dgram_recvmsg.
467 /* put message back and return */
468 skb_queue_head(&sk
->sk_receive_queue
, skb
);
472 caif_read_unlock(sk
);
475 return copied
? : err
;
479 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
480 * CAIF flow-on and sock_writable.
482 static long caif_wait_for_flow_on(struct caifsock
*cf_sk
,
483 int wait_writeable
, long timeo
, int *err
)
485 struct sock
*sk
= &cf_sk
->sk
;
489 if (tx_flow_is_on(cf_sk
) &&
490 (!wait_writeable
|| sock_writeable(&cf_sk
->sk
)))
496 if (signal_pending(current
))
498 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
500 if (sk
->sk_shutdown
& SHUTDOWN_MASK
)
506 if (cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
)
508 timeo
= schedule_timeout(timeo
);
510 finish_wait(sk_sleep(sk
), &wait
);
515 * Transmit a SKB. The device may temporarily request re-transmission
516 * by returning EAGAIN.
518 static int transmit_skb(struct sk_buff
*skb
, struct caifsock
*cf_sk
,
519 int noblock
, long timeo
)
522 int ret
, loopcnt
= 0;
524 pkt
= cfpkt_fromnative(CAIF_DIR_OUT
, skb
);
525 memset(cfpkt_info(pkt
), 0, sizeof(struct caif_payload_info
));
530 /* Slight paranoia, probably not needed. */
531 if (unlikely(loopcnt
++ > 1000)) {
532 pr_warn("transmit retries failed, error = %d\n", ret
);
536 if (cf_sk
->layer
.dn
!= NULL
)
537 ret
= cf_sk
->layer
.dn
->transmit(cf_sk
->layer
.dn
, pkt
);
538 if (likely(ret
>= 0))
540 /* if transmit return -EAGAIN, then retry */
541 if (noblock
&& ret
== -EAGAIN
)
543 timeo
= caif_wait_for_flow_on(cf_sk
, 0, timeo
, &ret
);
544 if (signal_pending(current
)) {
545 ret
= sock_intr_errno(timeo
);
550 if (cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
||
551 sock_flag(&cf_sk
->sk
, SOCK_DEAD
) ||
552 (cf_sk
->sk
.sk_shutdown
& RCV_SHUTDOWN
)) {
554 cf_sk
->sk
.sk_err
= EPIPE
;
557 } while (ret
== -EAGAIN
);
561 /* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
562 static int caif_seqpkt_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
563 struct msghdr
*msg
, size_t len
)
565 struct sock
*sk
= sock
->sk
;
566 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
569 struct sk_buff
*skb
= NULL
;
573 ret
= sock_error(sk
);
578 if (msg
->msg_flags
&MSG_OOB
)
582 if (msg
->msg_namelen
)
586 if (unlikely(msg
->msg_iov
->iov_base
== NULL
))
588 noblock
= msg
->msg_flags
& MSG_DONTWAIT
;
590 timeo
= sock_sndtimeo(sk
, noblock
);
591 timeo
= caif_wait_for_flow_on(container_of(sk
, struct caifsock
, sk
),
597 if (cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
||
598 sock_flag(sk
, SOCK_DEAD
) ||
599 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
602 /* Error if trying to write more than maximum frame size. */
604 if (len
> cf_sk
->maxframe
&& cf_sk
->sk
.sk_protocol
!= CAIFPROTO_RFM
)
607 buffer_size
= len
+ cf_sk
->headroom
+ cf_sk
->tailroom
;
610 skb
= sock_alloc_send_skb(sk
, buffer_size
, noblock
, &ret
);
612 if (!skb
|| skb_tailroom(skb
) < buffer_size
)
615 skb_reserve(skb
, cf_sk
->headroom
);
617 ret
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
621 ret
= transmit_skb(skb
, cf_sk
, noblock
, timeo
);
631 * Copied from unix_stream_sendmsg and adapted to CAIF:
632 * Changed removed permission handling and added waiting for flow on
633 * and other minor adaptations.
635 static int caif_stream_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
636 struct msghdr
*msg
, size_t len
)
638 struct sock
*sk
= sock
->sk
;
639 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
646 if (unlikely(msg
->msg_flags
&MSG_OOB
))
649 if (unlikely(msg
->msg_namelen
))
652 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
653 timeo
= caif_wait_for_flow_on(cf_sk
, 1, timeo
, &err
);
655 if (unlikely(sk
->sk_shutdown
& SEND_SHUTDOWN
))
662 if (size
> cf_sk
->maxframe
)
663 size
= cf_sk
->maxframe
;
665 /* If size is more than half of sndbuf, chop up message */
666 if (size
> ((sk
->sk_sndbuf
>> 1) - 64))
667 size
= (sk
->sk_sndbuf
>> 1) - 64;
669 if (size
> SKB_MAX_ALLOC
)
670 size
= SKB_MAX_ALLOC
;
672 skb
= sock_alloc_send_skb(sk
,
673 size
+ cf_sk
->headroom
+
675 msg
->msg_flags
&MSG_DONTWAIT
,
680 skb_reserve(skb
, cf_sk
->headroom
);
682 * If you pass two values to the sock_alloc_send_skb
683 * it tries to grab the large buffer with GFP_NOFS
684 * (which can fail easily), and if it fails grab the
685 * fallback size buffer which is under a page and will
688 size
= min_t(int, size
, skb_tailroom(skb
));
690 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
695 err
= transmit_skb(skb
, cf_sk
,
696 msg
->msg_flags
&MSG_DONTWAIT
, timeo
);
707 if (sent
== 0 && !(msg
->msg_flags
&MSG_NOSIGNAL
))
708 send_sig(SIGPIPE
, current
, 0);
714 static int setsockopt(struct socket
*sock
,
715 int lvl
, int opt
, char __user
*ov
, unsigned int ol
)
717 struct sock
*sk
= sock
->sk
;
718 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
721 if (cf_sk
->sk
.sk_socket
->state
!= SS_UNCONNECTED
)
725 case CAIFSO_LINK_SELECT
:
726 if (ol
< sizeof(int))
730 if (copy_from_user(&linksel
, ov
, sizeof(int)))
732 lock_sock(&(cf_sk
->sk
));
733 cf_sk
->conn_req
.link_selector
= linksel
;
734 release_sock(&cf_sk
->sk
);
737 case CAIFSO_REQ_PARAM
:
740 if (cf_sk
->sk
.sk_protocol
!= CAIFPROTO_UTIL
)
742 lock_sock(&(cf_sk
->sk
));
743 if (ol
> sizeof(cf_sk
->conn_req
.param
.data
) ||
744 copy_from_user(&cf_sk
->conn_req
.param
.data
, ov
, ol
)) {
745 release_sock(&cf_sk
->sk
);
748 cf_sk
->conn_req
.param
.size
= ol
;
749 release_sock(&cf_sk
->sk
);
763 * caif_connect() - Connect a CAIF Socket
764 * Copied and modified af_irda.c:irda_connect().
766 * Note : by consulting "errno", the user space caller may learn the cause
767 * of the failure. Most of them are visible in the function, others may come
768 * from subroutines called and are listed here :
769 * o -EAFNOSUPPORT: bad socket family or type.
770 * o -ESOCKTNOSUPPORT: bad socket type or protocol
771 * o -EINVAL: bad socket address, or CAIF link type
772 * o -ECONNREFUSED: remote end refused the connection.
773 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
774 * o -EISCONN: already connected.
775 * o -ETIMEDOUT: Connection timed out (send timeout)
776 * o -ENODEV: No link layer to send request
777 * o -ECONNRESET: Received Shutdown indication or lost link layer
778 * o -ENOMEM: Out of memory
781 * o sk_state: holds the CAIF_* protocol state, it's updated by
783 * o sock->state: holds the SS_* socket state and is updated by connect and
786 static int caif_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
787 int addr_len
, int flags
)
789 struct sock
*sk
= sock
->sk
;
790 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
793 int ifindex
, headroom
, tailroom
;
795 struct net_device
*dev
;
800 if (uaddr
->sa_family
!= AF_CAIF
)
803 switch (sock
->state
) {
805 /* Normal case, a fresh connect */
806 caif_assert(sk
->sk_state
== CAIF_DISCONNECTED
);
809 switch (sk
->sk_state
) {
811 sock
->state
= SS_CONNECTED
;
814 case CAIF_DISCONNECTED
:
815 /* Reconnect allowed */
817 case CAIF_CONNECTING
:
819 if (flags
& O_NONBLOCK
)
825 caif_assert(sk
->sk_state
== CAIF_CONNECTED
||
826 sk
->sk_state
== CAIF_DISCONNECTED
);
827 if (sk
->sk_shutdown
& SHUTDOWN_MASK
) {
828 /* Allow re-connect after SHUTDOWN_IND */
829 caif_disconnect_client(&cf_sk
->layer
);
832 /* No reconnect on a seqpacket socket */
835 case SS_DISCONNECTING
:
837 caif_assert(1); /*Should never happen */
840 sk
->sk_state
= CAIF_DISCONNECTED
;
841 sock
->state
= SS_UNCONNECTED
;
842 sk_stream_kill_queues(&cf_sk
->sk
);
845 if (addr_len
!= sizeof(struct sockaddr_caif
))
848 memcpy(&cf_sk
->conn_req
.sockaddr
, uaddr
,
849 sizeof(struct sockaddr_caif
));
851 /* Move to connecting socket, start sending Connect Requests */
852 sock
->state
= SS_CONNECTING
;
853 sk
->sk_state
= CAIF_CONNECTING
;
855 /* Check priority value comming from socket */
856 /* if priority value is out of range it will be ajusted */
857 if (cf_sk
->sk
.sk_priority
> CAIF_PRIO_MAX
)
858 cf_sk
->conn_req
.priority
= CAIF_PRIO_MAX
;
859 else if (cf_sk
->sk
.sk_priority
< CAIF_PRIO_MIN
)
860 cf_sk
->conn_req
.priority
= CAIF_PRIO_MIN
;
862 cf_sk
->conn_req
.priority
= cf_sk
->sk
.sk_priority
;
864 /*ifindex = id of the interface.*/
865 cf_sk
->conn_req
.ifindex
= cf_sk
->sk
.sk_bound_dev_if
;
867 dbfs_atomic_inc(&cnt
.num_connect_req
);
868 cf_sk
->layer
.receive
= caif_sktrecv_cb
;
869 err
= caif_connect_client(&cf_sk
->conn_req
,
870 &cf_sk
->layer
, &ifindex
, &headroom
, &tailroom
);
872 cf_sk
->sk
.sk_socket
->state
= SS_UNCONNECTED
;
873 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
879 dev
= dev_get_by_index_rcu(sock_net(sk
), ifindex
);
884 cf_sk
->headroom
= LL_RESERVED_SPACE_EXTRA(dev
, headroom
);
888 cf_sk
->tailroom
= tailroom
;
889 cf_sk
->maxframe
= mtu
- (headroom
+ tailroom
);
890 if (cf_sk
->maxframe
< 1) {
891 pr_warn("CAIF Interface MTU too small (%d)\n", dev
->mtu
);
899 if (sk
->sk_state
!= CAIF_CONNECTED
&& (flags
& O_NONBLOCK
))
902 timeo
= sock_sndtimeo(sk
, flags
& O_NONBLOCK
);
906 timeo
= wait_event_interruptible_timeout(*sk_sleep(sk
),
907 sk
->sk_state
!= CAIF_CONNECTING
,
911 goto out
; /* -ERESTARTSYS */
914 if (timeo
== 0 && sk
->sk_state
!= CAIF_CONNECTED
)
916 if (sk
->sk_state
!= CAIF_CONNECTED
) {
917 sock
->state
= SS_UNCONNECTED
;
918 err
= sock_error(sk
);
923 sock
->state
= SS_CONNECTED
;
931 * caif_release() - Disconnect a CAIF Socket
932 * Copied and modified af_irda.c:irda_release().
934 static int caif_release(struct socket
*sock
)
936 struct sock
*sk
= sock
->sk
;
937 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
943 set_tx_flow_off(cf_sk
);
946 * Ensure that packets are not queued after this point in time.
947 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
948 * this ensures no packets when sock is dead.
950 spin_lock(&sk
->sk_receive_queue
.lock
);
951 sock_set_flag(sk
, SOCK_DEAD
);
952 spin_unlock(&sk
->sk_receive_queue
.lock
);
955 dbfs_atomic_inc(&cnt
.num_disconnect
);
957 if (cf_sk
->debugfs_socket_dir
!= NULL
)
958 debugfs_remove_recursive(cf_sk
->debugfs_socket_dir
);
960 lock_sock(&(cf_sk
->sk
));
961 sk
->sk_state
= CAIF_DISCONNECTED
;
962 sk
->sk_shutdown
= SHUTDOWN_MASK
;
964 if (cf_sk
->sk
.sk_socket
->state
== SS_CONNECTED
||
965 cf_sk
->sk
.sk_socket
->state
== SS_CONNECTING
)
966 res
= caif_disconnect_client(&cf_sk
->layer
);
968 cf_sk
->sk
.sk_socket
->state
= SS_DISCONNECTING
;
969 wake_up_interruptible_poll(sk_sleep(sk
), POLLERR
|POLLHUP
);
972 cf_sk
->layer
.dn
= NULL
;
973 sk_stream_kill_queues(&cf_sk
->sk
);
979 /* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
980 static unsigned int caif_poll(struct file
*file
,
981 struct socket
*sock
, poll_table
*wait
)
983 struct sock
*sk
= sock
->sk
;
985 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
987 sock_poll_wait(file
, sk_sleep(sk
), wait
);
990 /* exceptional events? */
993 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
995 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
999 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1000 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
1001 mask
|= POLLIN
| POLLRDNORM
;
1004 * we set writable also when the other side has shut down the
1005 * connection. This prevents stuck sockets.
1007 if (sock_writeable(sk
) && tx_flow_is_on(cf_sk
))
1008 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
1013 static const struct proto_ops caif_seqpacket_ops
= {
1015 .owner
= THIS_MODULE
,
1016 .release
= caif_release
,
1017 .bind
= sock_no_bind
,
1018 .connect
= caif_connect
,
1019 .socketpair
= sock_no_socketpair
,
1020 .accept
= sock_no_accept
,
1021 .getname
= sock_no_getname
,
1023 .ioctl
= sock_no_ioctl
,
1024 .listen
= sock_no_listen
,
1025 .shutdown
= sock_no_shutdown
,
1026 .setsockopt
= setsockopt
,
1027 .getsockopt
= sock_no_getsockopt
,
1028 .sendmsg
= caif_seqpkt_sendmsg
,
1029 .recvmsg
= caif_seqpkt_recvmsg
,
1030 .mmap
= sock_no_mmap
,
1031 .sendpage
= sock_no_sendpage
,
1034 static const struct proto_ops caif_stream_ops
= {
1036 .owner
= THIS_MODULE
,
1037 .release
= caif_release
,
1038 .bind
= sock_no_bind
,
1039 .connect
= caif_connect
,
1040 .socketpair
= sock_no_socketpair
,
1041 .accept
= sock_no_accept
,
1042 .getname
= sock_no_getname
,
1044 .ioctl
= sock_no_ioctl
,
1045 .listen
= sock_no_listen
,
1046 .shutdown
= sock_no_shutdown
,
1047 .setsockopt
= setsockopt
,
1048 .getsockopt
= sock_no_getsockopt
,
1049 .sendmsg
= caif_stream_sendmsg
,
1050 .recvmsg
= caif_stream_recvmsg
,
1051 .mmap
= sock_no_mmap
,
1052 .sendpage
= sock_no_sendpage
,
1055 /* This function is called when a socket is finally destroyed. */
1056 static void caif_sock_destructor(struct sock
*sk
)
1058 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
1059 caif_assert(!atomic_read(&sk
->sk_wmem_alloc
));
1060 caif_assert(sk_unhashed(sk
));
1061 caif_assert(!sk
->sk_socket
);
1062 if (!sock_flag(sk
, SOCK_DEAD
)) {
1063 pr_info("Attempt to release alive CAIF socket: %p\n", sk
);
1066 sk_stream_kill_queues(&cf_sk
->sk
);
1067 dbfs_atomic_dec(&cnt
.caif_nr_socks
);
1070 static int caif_create(struct net
*net
, struct socket
*sock
, int protocol
,
1073 struct sock
*sk
= NULL
;
1074 struct caifsock
*cf_sk
= NULL
;
1075 static struct proto prot
= {.name
= "PF_CAIF",
1076 .owner
= THIS_MODULE
,
1077 .obj_size
= sizeof(struct caifsock
),
1080 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_NET_ADMIN
))
1083 * The sock->type specifies the socket type to use.
1084 * The CAIF socket is a packet stream in the sense
1085 * that it is packet based. CAIF trusts the reliability
1086 * of the link, no resending is implemented.
1088 if (sock
->type
== SOCK_SEQPACKET
)
1089 sock
->ops
= &caif_seqpacket_ops
;
1090 else if (sock
->type
== SOCK_STREAM
)
1091 sock
->ops
= &caif_stream_ops
;
1093 return -ESOCKTNOSUPPORT
;
1095 if (protocol
< 0 || protocol
>= CAIFPROTO_MAX
)
1096 return -EPROTONOSUPPORT
;
1098 * Set the socket state to unconnected. The socket state
1099 * is really not used at all in the net/core or socket.c but the
1100 * initialization makes sure that sock->state is not uninitialized.
1102 sk
= sk_alloc(net
, PF_CAIF
, GFP_KERNEL
, &prot
);
1106 cf_sk
= container_of(sk
, struct caifsock
, sk
);
1108 /* Store the protocol */
1109 sk
->sk_protocol
= (unsigned char) protocol
;
1112 * Lock in order to try to stop someone from opening the socket
1115 lock_sock(&(cf_sk
->sk
));
1117 /* Initialize the nozero default sock structure data. */
1118 sock_init_data(sock
, sk
);
1119 sk
->sk_destruct
= caif_sock_destructor
;
1121 mutex_init(&cf_sk
->readlock
); /* single task reading lock */
1122 cf_sk
->layer
.ctrlcmd
= caif_ctrl_cb
;
1123 cf_sk
->sk
.sk_socket
->state
= SS_UNCONNECTED
;
1124 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
1126 set_tx_flow_off(cf_sk
);
1127 set_rx_flow_on(cf_sk
);
1129 /* Set default options on configuration */
1130 cf_sk
->sk
.sk_priority
= CAIF_PRIO_NORMAL
;
1131 cf_sk
->conn_req
.link_selector
= CAIF_LINK_LOW_LATENCY
;
1132 cf_sk
->conn_req
.protocol
= protocol
;
1133 /* Increase the number of sockets created. */
1134 dbfs_atomic_inc(&cnt
.caif_nr_socks
);
1135 #ifdef CONFIG_DEBUG_FS
1136 if (!IS_ERR(debugfsdir
)) {
1137 /* Fill in some information concerning the misc socket. */
1138 snprintf(cf_sk
->name
, sizeof(cf_sk
->name
), "cfsk%d",
1139 atomic_read(&cnt
.caif_nr_socks
));
1141 cf_sk
->debugfs_socket_dir
=
1142 debugfs_create_dir(cf_sk
->name
, debugfsdir
);
1143 debugfs_create_u32("sk_state", S_IRUSR
| S_IWUSR
,
1144 cf_sk
->debugfs_socket_dir
,
1145 (u32
*) &cf_sk
->sk
.sk_state
);
1146 debugfs_create_u32("flow_state", S_IRUSR
| S_IWUSR
,
1147 cf_sk
->debugfs_socket_dir
, &cf_sk
->flow_state
);
1148 debugfs_create_u32("sk_rmem_alloc", S_IRUSR
| S_IWUSR
,
1149 cf_sk
->debugfs_socket_dir
,
1150 (u32
*) &cf_sk
->sk
.sk_rmem_alloc
);
1151 debugfs_create_u32("sk_wmem_alloc", S_IRUSR
| S_IWUSR
,
1152 cf_sk
->debugfs_socket_dir
,
1153 (u32
*) &cf_sk
->sk
.sk_wmem_alloc
);
1154 debugfs_create_u32("identity", S_IRUSR
| S_IWUSR
,
1155 cf_sk
->debugfs_socket_dir
,
1156 (u32
*) &cf_sk
->layer
.id
);
1159 release_sock(&cf_sk
->sk
);
1164 static struct net_proto_family caif_family_ops
= {
1166 .create
= caif_create
,
1167 .owner
= THIS_MODULE
,
1170 static int af_caif_init(void)
1172 int err
= sock_register(&caif_family_ops
);
1178 static int __init
caif_sktinit_module(void)
1180 #ifdef CONFIG_DEBUG_FS
1181 debugfsdir
= debugfs_create_dir("caif_sk", NULL
);
1182 if (!IS_ERR(debugfsdir
)) {
1183 debugfs_create_u32("num_sockets", S_IRUSR
| S_IWUSR
,
1185 (u32
*) &cnt
.caif_nr_socks
);
1186 debugfs_create_u32("num_connect_req", S_IRUSR
| S_IWUSR
,
1188 (u32
*) &cnt
.num_connect_req
);
1189 debugfs_create_u32("num_connect_resp", S_IRUSR
| S_IWUSR
,
1191 (u32
*) &cnt
.num_connect_resp
);
1192 debugfs_create_u32("num_connect_fail_resp", S_IRUSR
| S_IWUSR
,
1194 (u32
*) &cnt
.num_connect_fail_resp
);
1195 debugfs_create_u32("num_disconnect", S_IRUSR
| S_IWUSR
,
1197 (u32
*) &cnt
.num_disconnect
);
1198 debugfs_create_u32("num_remote_shutdown_ind",
1199 S_IRUSR
| S_IWUSR
, debugfsdir
,
1200 (u32
*) &cnt
.num_remote_shutdown_ind
);
1201 debugfs_create_u32("num_tx_flow_off_ind", S_IRUSR
| S_IWUSR
,
1203 (u32
*) &cnt
.num_tx_flow_off_ind
);
1204 debugfs_create_u32("num_tx_flow_on_ind", S_IRUSR
| S_IWUSR
,
1206 (u32
*) &cnt
.num_tx_flow_on_ind
);
1207 debugfs_create_u32("num_rx_flow_off", S_IRUSR
| S_IWUSR
,
1209 (u32
*) &cnt
.num_rx_flow_off
);
1210 debugfs_create_u32("num_rx_flow_on", S_IRUSR
| S_IWUSR
,
1212 (u32
*) &cnt
.num_rx_flow_on
);
1215 return af_caif_init();
1218 static void __exit
caif_sktexit_module(void)
1220 sock_unregister(PF_CAIF
);
1221 if (debugfsdir
!= NULL
)
1222 debugfs_remove_recursive(debugfsdir
);
1224 module_init(caif_sktinit_module
);
1225 module_exit(caif_sktexit_module
);