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 <linux/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 caif_sock_create
;
52 atomic_t num_connect_req
;
53 atomic_t num_connect_resp
;
54 atomic_t num_connect_fail_resp
;
55 atomic_t num_disconnect
;
56 atomic_t num_remote_shutdown_ind
;
57 atomic_t num_tx_flow_off_ind
;
58 atomic_t num_tx_flow_on_ind
;
59 atomic_t num_rx_flow_off
;
60 atomic_t num_rx_flow_on
;
62 static struct debug_fs_counter cnt
;
63 #define dbfs_atomic_inc(v) atomic_inc_return(v)
64 #define dbfs_atomic_dec(v) atomic_dec_return(v)
66 #define dbfs_atomic_inc(v) 0
67 #define dbfs_atomic_dec(v) 0
71 struct sock sk
; /* must be first member */
73 char name
[CAIF_LAYER_NAME_SZ
]; /* Used for debugging */
75 struct caif_connect_request conn_req
;
76 struct mutex readlock
;
77 struct dentry
*debugfs_socket_dir
;
78 int headroom
, tailroom
, maxframe
;
81 static int rx_flow_is_on(struct caifsock
*cf_sk
)
83 return test_bit(RX_FLOW_ON_BIT
,
84 (void *) &cf_sk
->flow_state
);
87 static int tx_flow_is_on(struct caifsock
*cf_sk
)
89 return test_bit(TX_FLOW_ON_BIT
,
90 (void *) &cf_sk
->flow_state
);
93 static void set_rx_flow_off(struct caifsock
*cf_sk
)
95 clear_bit(RX_FLOW_ON_BIT
,
96 (void *) &cf_sk
->flow_state
);
99 static void set_rx_flow_on(struct caifsock
*cf_sk
)
101 set_bit(RX_FLOW_ON_BIT
,
102 (void *) &cf_sk
->flow_state
);
105 static void set_tx_flow_off(struct caifsock
*cf_sk
)
107 clear_bit(TX_FLOW_ON_BIT
,
108 (void *) &cf_sk
->flow_state
);
111 static void set_tx_flow_on(struct caifsock
*cf_sk
)
113 set_bit(TX_FLOW_ON_BIT
,
114 (void *) &cf_sk
->flow_state
);
117 static void caif_read_lock(struct sock
*sk
)
119 struct caifsock
*cf_sk
;
120 cf_sk
= container_of(sk
, struct caifsock
, sk
);
121 mutex_lock(&cf_sk
->readlock
);
124 static void caif_read_unlock(struct sock
*sk
)
126 struct caifsock
*cf_sk
;
127 cf_sk
= container_of(sk
, struct caifsock
, sk
);
128 mutex_unlock(&cf_sk
->readlock
);
131 static int sk_rcvbuf_lowwater(struct caifsock
*cf_sk
)
133 /* A quarter of full buffer is used a low water mark */
134 return cf_sk
->sk
.sk_rcvbuf
/ 4;
137 static void caif_flow_ctrl(struct sock
*sk
, int mode
)
139 struct caifsock
*cf_sk
;
140 cf_sk
= container_of(sk
, struct caifsock
, sk
);
141 if (cf_sk
->layer
.dn
&& cf_sk
->layer
.dn
->modemcmd
)
142 cf_sk
->layer
.dn
->modemcmd(cf_sk
->layer
.dn
, mode
);
146 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
147 * not dropped, but CAIF is sending flow off instead.
149 static int caif_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
154 struct sk_buff_head
*list
= &sk
->sk_receive_queue
;
155 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
157 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
158 (unsigned)sk
->sk_rcvbuf
&& rx_flow_is_on(cf_sk
)) {
160 pr_debug("sending flow OFF (queue len = %d %d)\n",
161 atomic_read(&cf_sk
->sk
.sk_rmem_alloc
),
162 sk_rcvbuf_lowwater(cf_sk
));
163 set_rx_flow_off(cf_sk
);
164 dbfs_atomic_inc(&cnt
.num_rx_flow_off
);
165 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_OFF_REQ
);
168 err
= sk_filter(sk
, skb
);
171 if (!sk_rmem_schedule(sk
, skb
->truesize
) && rx_flow_is_on(cf_sk
)) {
172 set_rx_flow_off(cf_sk
);
174 pr_debug("sending flow OFF due to rmem_schedule\n");
175 dbfs_atomic_inc(&cnt
.num_rx_flow_off
);
176 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_OFF_REQ
);
179 skb_set_owner_r(skb
, sk
);
180 /* Cache the SKB length before we tack it onto the receive
181 * queue. Once it is added it no longer belongs to us and
182 * may be freed by other threads of control pulling packets
186 spin_lock_irqsave(&list
->lock
, flags
);
187 if (!sock_flag(sk
, SOCK_DEAD
))
188 __skb_queue_tail(list
, skb
);
189 spin_unlock_irqrestore(&list
->lock
, flags
);
191 if (!sock_flag(sk
, SOCK_DEAD
))
192 sk
->sk_data_ready(sk
, skb_len
);
198 /* Packet Receive Callback function called from CAIF Stack */
199 static int caif_sktrecv_cb(struct cflayer
*layr
, struct cfpkt
*pkt
)
201 struct caifsock
*cf_sk
;
204 cf_sk
= container_of(layr
, struct caifsock
, layer
);
205 skb
= cfpkt_tonative(pkt
);
207 if (unlikely(cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
)) {
211 caif_queue_rcv_skb(&cf_sk
->sk
, skb
);
215 static void cfsk_hold(struct cflayer
*layr
)
217 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
218 sock_hold(&cf_sk
->sk
);
221 static void cfsk_put(struct cflayer
*layr
)
223 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
224 sock_put(&cf_sk
->sk
);
227 /* Packet Control Callback function called from CAIF */
228 static void caif_ctrl_cb(struct cflayer
*layr
,
229 enum caif_ctrlcmd flow
,
232 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
234 case CAIF_CTRLCMD_FLOW_ON_IND
:
235 /* OK from modem to start sending again */
236 dbfs_atomic_inc(&cnt
.num_tx_flow_on_ind
);
237 set_tx_flow_on(cf_sk
);
238 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
241 case CAIF_CTRLCMD_FLOW_OFF_IND
:
242 /* Modem asks us to shut up */
243 dbfs_atomic_inc(&cnt
.num_tx_flow_off_ind
);
244 set_tx_flow_off(cf_sk
);
245 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
248 case CAIF_CTRLCMD_INIT_RSP
:
249 /* We're now connected */
250 caif_client_register_refcnt(&cf_sk
->layer
,
251 cfsk_hold
, cfsk_put
);
252 dbfs_atomic_inc(&cnt
.num_connect_resp
);
253 cf_sk
->sk
.sk_state
= CAIF_CONNECTED
;
254 set_tx_flow_on(cf_sk
);
255 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
258 case CAIF_CTRLCMD_DEINIT_RSP
:
259 /* We're now disconnected */
260 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
261 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
264 case CAIF_CTRLCMD_INIT_FAIL_RSP
:
265 /* Connect request failed */
266 dbfs_atomic_inc(&cnt
.num_connect_fail_resp
);
267 cf_sk
->sk
.sk_err
= ECONNREFUSED
;
268 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
269 cf_sk
->sk
.sk_shutdown
= SHUTDOWN_MASK
;
271 * Socket "standards" seems to require POLLOUT to
272 * be set at connect failure.
274 set_tx_flow_on(cf_sk
);
275 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
278 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
:
279 /* Modem has closed this connection, or device is down. */
280 dbfs_atomic_inc(&cnt
.num_remote_shutdown_ind
);
281 cf_sk
->sk
.sk_shutdown
= SHUTDOWN_MASK
;
282 cf_sk
->sk
.sk_err
= ECONNRESET
;
283 set_rx_flow_on(cf_sk
);
284 cf_sk
->sk
.sk_error_report(&cf_sk
->sk
);
288 pr_debug("Unexpected flow command %d\n", flow
);
292 static void caif_check_flow_release(struct sock
*sk
)
294 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
296 if (rx_flow_is_on(cf_sk
))
299 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk_rcvbuf_lowwater(cf_sk
)) {
300 dbfs_atomic_inc(&cnt
.num_rx_flow_on
);
301 set_rx_flow_on(cf_sk
);
302 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_ON_REQ
);
307 * Copied from unix_dgram_recvmsg, but removed credit checks,
308 * changed locking, address handling and added MSG_TRUNC.
310 static int caif_seqpkt_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
311 struct msghdr
*m
, size_t len
, int flags
)
314 struct sock
*sk
= sock
->sk
;
320 if (m
->msg_flags
&MSG_OOB
)
323 skb
= skb_recv_datagram(sk
, flags
, 0 , &ret
);
328 m
->msg_flags
|= MSG_TRUNC
;
332 ret
= skb_copy_datagram_iovec(skb
, 0, m
->msg_iov
, copylen
);
336 ret
= (flags
& MSG_TRUNC
) ? skb
->len
: copylen
;
338 skb_free_datagram(sk
, skb
);
339 caif_check_flow_release(sk
);
347 /* Copied from unix_stream_wait_data, identical except for lock call. */
348 static long caif_stream_data_wait(struct sock
*sk
, long timeo
)
354 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
356 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
358 sk
->sk_state
!= CAIF_CONNECTED
||
359 sock_flag(sk
, SOCK_DEAD
) ||
360 (sk
->sk_shutdown
& RCV_SHUTDOWN
) ||
361 signal_pending(current
) ||
365 set_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
367 timeo
= schedule_timeout(timeo
);
369 clear_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
372 finish_wait(sk_sleep(sk
), &wait
);
379 * Copied from unix_stream_recvmsg, but removed credit checks,
380 * changed locking calls, changed address handling.
382 static int caif_stream_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
383 struct msghdr
*msg
, size_t size
,
386 struct sock
*sk
= sock
->sk
;
396 msg
->msg_namelen
= 0;
399 * Lock the socket to prevent queue disordering
400 * while sleeps in memcpy_tomsg
403 if (sk
->sk_state
== CAIF_CONNECTING
)
407 target
= sock_rcvlowat(sk
, flags
&MSG_WAITALL
, size
);
408 timeo
= sock_rcvtimeo(sk
, flags
&MSG_DONTWAIT
);
415 skb
= skb_dequeue(&sk
->sk_receive_queue
);
416 caif_check_flow_release(sk
);
419 if (copied
>= target
)
422 * POSIX 1003.1g mandates this order.
424 err
= sock_error(sk
);
428 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
432 if (sk
->sk_state
!= CAIF_CONNECTED
)
434 if (sock_flag(sk
, SOCK_DEAD
))
443 caif_read_unlock(sk
);
445 timeo
= caif_stream_data_wait(sk
, timeo
);
447 if (signal_pending(current
)) {
448 err
= sock_intr_errno(timeo
);
458 chunk
= min_t(unsigned int, skb
->len
, size
);
459 if (memcpy_toiovec(msg
->msg_iov
, skb
->data
, chunk
)) {
460 skb_queue_head(&sk
->sk_receive_queue
, skb
);
468 /* Mark read part of skb as used */
469 if (!(flags
& MSG_PEEK
)) {
470 skb_pull(skb
, chunk
);
472 /* put the skb back if we didn't use it up. */
474 skb_queue_head(&sk
->sk_receive_queue
, skb
);
481 * It is questionable, see note in unix_dgram_recvmsg.
483 /* put message back and return */
484 skb_queue_head(&sk
->sk_receive_queue
, skb
);
488 caif_read_unlock(sk
);
491 return copied
? : err
;
495 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
496 * CAIF flow-on and sock_writable.
498 static long caif_wait_for_flow_on(struct caifsock
*cf_sk
,
499 int wait_writeable
, long timeo
, int *err
)
501 struct sock
*sk
= &cf_sk
->sk
;
505 if (tx_flow_is_on(cf_sk
) &&
506 (!wait_writeable
|| sock_writeable(&cf_sk
->sk
)))
512 if (signal_pending(current
))
514 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
516 if (sk
->sk_shutdown
& SHUTDOWN_MASK
)
522 if (cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
)
524 timeo
= schedule_timeout(timeo
);
526 finish_wait(sk_sleep(sk
), &wait
);
531 * Transmit a SKB. The device may temporarily request re-transmission
532 * by returning EAGAIN.
534 static int transmit_skb(struct sk_buff
*skb
, struct caifsock
*cf_sk
,
535 int noblock
, long timeo
)
539 pkt
= cfpkt_fromnative(CAIF_DIR_OUT
, skb
);
540 memset(skb
->cb
, 0, sizeof(struct caif_payload_info
));
542 if (cf_sk
->layer
.dn
== NULL
) {
547 return cf_sk
->layer
.dn
->transmit(cf_sk
->layer
.dn
, pkt
);
550 /* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
551 static int caif_seqpkt_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
552 struct msghdr
*msg
, size_t len
)
554 struct sock
*sk
= sock
->sk
;
555 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
558 struct sk_buff
*skb
= NULL
;
562 ret
= sock_error(sk
);
567 if (msg
->msg_flags
&MSG_OOB
)
571 if (msg
->msg_namelen
)
575 if (unlikely(msg
->msg_iov
->iov_base
== NULL
))
577 noblock
= msg
->msg_flags
& MSG_DONTWAIT
;
579 timeo
= sock_sndtimeo(sk
, noblock
);
580 timeo
= caif_wait_for_flow_on(container_of(sk
, struct caifsock
, sk
),
586 if (cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
||
587 sock_flag(sk
, SOCK_DEAD
) ||
588 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
591 /* Error if trying to write more than maximum frame size. */
593 if (len
> cf_sk
->maxframe
&& cf_sk
->sk
.sk_protocol
!= CAIFPROTO_RFM
)
596 buffer_size
= len
+ cf_sk
->headroom
+ cf_sk
->tailroom
;
599 skb
= sock_alloc_send_skb(sk
, buffer_size
, noblock
, &ret
);
601 if (!skb
|| skb_tailroom(skb
) < buffer_size
)
604 skb_reserve(skb
, cf_sk
->headroom
);
606 ret
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
610 ret
= transmit_skb(skb
, cf_sk
, noblock
, timeo
);
612 /* skb is already freed */
622 * Copied from unix_stream_sendmsg and adapted to CAIF:
623 * Changed removed permission handling and added waiting for flow on
624 * and other minor adaptations.
626 static int caif_stream_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
627 struct msghdr
*msg
, size_t len
)
629 struct sock
*sk
= sock
->sk
;
630 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
637 if (unlikely(msg
->msg_flags
&MSG_OOB
))
640 if (unlikely(msg
->msg_namelen
))
643 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
644 timeo
= caif_wait_for_flow_on(cf_sk
, 1, timeo
, &err
);
646 if (unlikely(sk
->sk_shutdown
& SEND_SHUTDOWN
))
653 if (size
> cf_sk
->maxframe
)
654 size
= cf_sk
->maxframe
;
656 /* If size is more than half of sndbuf, chop up message */
657 if (size
> ((sk
->sk_sndbuf
>> 1) - 64))
658 size
= (sk
->sk_sndbuf
>> 1) - 64;
660 if (size
> SKB_MAX_ALLOC
)
661 size
= SKB_MAX_ALLOC
;
663 skb
= sock_alloc_send_skb(sk
,
664 size
+ cf_sk
->headroom
+
666 msg
->msg_flags
&MSG_DONTWAIT
,
671 skb_reserve(skb
, cf_sk
->headroom
);
673 * If you pass two values to the sock_alloc_send_skb
674 * it tries to grab the large buffer with GFP_NOFS
675 * (which can fail easily), and if it fails grab the
676 * fallback size buffer which is under a page and will
679 size
= min_t(int, size
, skb_tailroom(skb
));
681 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
686 err
= transmit_skb(skb
, cf_sk
,
687 msg
->msg_flags
&MSG_DONTWAIT
, timeo
);
689 /* skb is already freed */
698 if (sent
== 0 && !(msg
->msg_flags
&MSG_NOSIGNAL
))
699 send_sig(SIGPIPE
, current
, 0);
705 static int setsockopt(struct socket
*sock
,
706 int lvl
, int opt
, char __user
*ov
, unsigned int ol
)
708 struct sock
*sk
= sock
->sk
;
709 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
712 if (cf_sk
->sk
.sk_socket
->state
!= SS_UNCONNECTED
)
716 case CAIFSO_LINK_SELECT
:
717 if (ol
< sizeof(int))
721 if (copy_from_user(&linksel
, ov
, sizeof(int)))
723 lock_sock(&(cf_sk
->sk
));
724 cf_sk
->conn_req
.link_selector
= linksel
;
725 release_sock(&cf_sk
->sk
);
728 case CAIFSO_REQ_PARAM
:
731 if (cf_sk
->sk
.sk_protocol
!= CAIFPROTO_UTIL
)
733 lock_sock(&(cf_sk
->sk
));
734 if (ol
> sizeof(cf_sk
->conn_req
.param
.data
) ||
735 copy_from_user(&cf_sk
->conn_req
.param
.data
, ov
, ol
)) {
736 release_sock(&cf_sk
->sk
);
739 cf_sk
->conn_req
.param
.size
= ol
;
740 release_sock(&cf_sk
->sk
);
754 * caif_connect() - Connect a CAIF Socket
755 * Copied and modified af_irda.c:irda_connect().
757 * Note : by consulting "errno", the user space caller may learn the cause
758 * of the failure. Most of them are visible in the function, others may come
759 * from subroutines called and are listed here :
760 * o -EAFNOSUPPORT: bad socket family or type.
761 * o -ESOCKTNOSUPPORT: bad socket type or protocol
762 * o -EINVAL: bad socket address, or CAIF link type
763 * o -ECONNREFUSED: remote end refused the connection.
764 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
765 * o -EISCONN: already connected.
766 * o -ETIMEDOUT: Connection timed out (send timeout)
767 * o -ENODEV: No link layer to send request
768 * o -ECONNRESET: Received Shutdown indication or lost link layer
769 * o -ENOMEM: Out of memory
772 * o sk_state: holds the CAIF_* protocol state, it's updated by
774 * o sock->state: holds the SS_* socket state and is updated by connect and
777 static int caif_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
778 int addr_len
, int flags
)
780 struct sock
*sk
= sock
->sk
;
781 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
784 int ifindex
, headroom
, tailroom
;
786 struct net_device
*dev
;
791 if (uaddr
->sa_family
!= AF_CAIF
)
794 switch (sock
->state
) {
796 /* Normal case, a fresh connect */
797 caif_assert(sk
->sk_state
== CAIF_DISCONNECTED
);
800 switch (sk
->sk_state
) {
802 sock
->state
= SS_CONNECTED
;
805 case CAIF_DISCONNECTED
:
806 /* Reconnect allowed */
808 case CAIF_CONNECTING
:
810 if (flags
& O_NONBLOCK
)
816 caif_assert(sk
->sk_state
== CAIF_CONNECTED
||
817 sk
->sk_state
== CAIF_DISCONNECTED
);
818 if (sk
->sk_shutdown
& SHUTDOWN_MASK
) {
819 /* Allow re-connect after SHUTDOWN_IND */
820 caif_disconnect_client(sock_net(sk
), &cf_sk
->layer
);
821 caif_free_client(&cf_sk
->layer
);
824 /* No reconnect on a seqpacket socket */
827 case SS_DISCONNECTING
:
829 caif_assert(1); /*Should never happen */
832 sk
->sk_state
= CAIF_DISCONNECTED
;
833 sock
->state
= SS_UNCONNECTED
;
834 sk_stream_kill_queues(&cf_sk
->sk
);
837 if (addr_len
!= sizeof(struct sockaddr_caif
))
840 memcpy(&cf_sk
->conn_req
.sockaddr
, uaddr
,
841 sizeof(struct sockaddr_caif
));
843 /* Move to connecting socket, start sending Connect Requests */
844 sock
->state
= SS_CONNECTING
;
845 sk
->sk_state
= CAIF_CONNECTING
;
847 /* Check priority value comming from socket */
848 /* if priority value is out of range it will be ajusted */
849 if (cf_sk
->sk
.sk_priority
> CAIF_PRIO_MAX
)
850 cf_sk
->conn_req
.priority
= CAIF_PRIO_MAX
;
851 else if (cf_sk
->sk
.sk_priority
< CAIF_PRIO_MIN
)
852 cf_sk
->conn_req
.priority
= CAIF_PRIO_MIN
;
854 cf_sk
->conn_req
.priority
= cf_sk
->sk
.sk_priority
;
856 /*ifindex = id of the interface.*/
857 cf_sk
->conn_req
.ifindex
= cf_sk
->sk
.sk_bound_dev_if
;
859 dbfs_atomic_inc(&cnt
.num_connect_req
);
860 cf_sk
->layer
.receive
= caif_sktrecv_cb
;
862 err
= caif_connect_client(sock_net(sk
), &cf_sk
->conn_req
,
863 &cf_sk
->layer
, &ifindex
, &headroom
, &tailroom
);
866 cf_sk
->sk
.sk_socket
->state
= SS_UNCONNECTED
;
867 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
873 dev
= dev_get_by_index_rcu(sock_net(sk
), ifindex
);
878 cf_sk
->headroom
= LL_RESERVED_SPACE_EXTRA(dev
, headroom
);
882 cf_sk
->tailroom
= tailroom
;
883 cf_sk
->maxframe
= mtu
- (headroom
+ tailroom
);
884 if (cf_sk
->maxframe
< 1) {
885 pr_warn("CAIF Interface MTU too small (%d)\n", dev
->mtu
);
893 if (sk
->sk_state
!= CAIF_CONNECTED
&& (flags
& O_NONBLOCK
))
896 timeo
= sock_sndtimeo(sk
, flags
& O_NONBLOCK
);
900 timeo
= wait_event_interruptible_timeout(*sk_sleep(sk
),
901 sk
->sk_state
!= CAIF_CONNECTING
,
905 goto out
; /* -ERESTARTSYS */
908 if (timeo
== 0 && sk
->sk_state
!= CAIF_CONNECTED
)
910 if (sk
->sk_state
!= CAIF_CONNECTED
) {
911 sock
->state
= SS_UNCONNECTED
;
912 err
= sock_error(sk
);
917 sock
->state
= SS_CONNECTED
;
925 * caif_release() - Disconnect a CAIF Socket
926 * Copied and modified af_irda.c:irda_release().
928 static int caif_release(struct socket
*sock
)
930 struct sock
*sk
= sock
->sk
;
931 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
936 set_tx_flow_off(cf_sk
);
939 * Ensure that packets are not queued after this point in time.
940 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
941 * this ensures no packets when sock is dead.
943 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
944 sock_set_flag(sk
, SOCK_DEAD
);
945 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
948 dbfs_atomic_inc(&cnt
.num_disconnect
);
950 WARN_ON(IS_ERR(cf_sk
->debugfs_socket_dir
));
951 if (cf_sk
->debugfs_socket_dir
!= NULL
)
952 debugfs_remove_recursive(cf_sk
->debugfs_socket_dir
);
954 lock_sock(&(cf_sk
->sk
));
955 sk
->sk_state
= CAIF_DISCONNECTED
;
956 sk
->sk_shutdown
= SHUTDOWN_MASK
;
958 caif_disconnect_client(sock_net(sk
), &cf_sk
->layer
);
959 cf_sk
->sk
.sk_socket
->state
= SS_DISCONNECTING
;
960 wake_up_interruptible_poll(sk_sleep(sk
), POLLERR
|POLLHUP
);
963 sk_stream_kill_queues(&cf_sk
->sk
);
969 /* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
970 static unsigned int caif_poll(struct file
*file
,
971 struct socket
*sock
, poll_table
*wait
)
973 struct sock
*sk
= sock
->sk
;
975 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
977 sock_poll_wait(file
, sk_sleep(sk
), wait
);
980 /* exceptional events? */
983 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
985 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
989 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
990 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
991 mask
|= POLLIN
| POLLRDNORM
;
994 * we set writable also when the other side has shut down the
995 * connection. This prevents stuck sockets.
997 if (sock_writeable(sk
) && tx_flow_is_on(cf_sk
))
998 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
1003 static const struct proto_ops caif_seqpacket_ops
= {
1005 .owner
= THIS_MODULE
,
1006 .release
= caif_release
,
1007 .bind
= sock_no_bind
,
1008 .connect
= caif_connect
,
1009 .socketpair
= sock_no_socketpair
,
1010 .accept
= sock_no_accept
,
1011 .getname
= sock_no_getname
,
1013 .ioctl
= sock_no_ioctl
,
1014 .listen
= sock_no_listen
,
1015 .shutdown
= sock_no_shutdown
,
1016 .setsockopt
= setsockopt
,
1017 .getsockopt
= sock_no_getsockopt
,
1018 .sendmsg
= caif_seqpkt_sendmsg
,
1019 .recvmsg
= caif_seqpkt_recvmsg
,
1020 .mmap
= sock_no_mmap
,
1021 .sendpage
= sock_no_sendpage
,
1024 static const struct proto_ops caif_stream_ops
= {
1026 .owner
= THIS_MODULE
,
1027 .release
= caif_release
,
1028 .bind
= sock_no_bind
,
1029 .connect
= caif_connect
,
1030 .socketpair
= sock_no_socketpair
,
1031 .accept
= sock_no_accept
,
1032 .getname
= sock_no_getname
,
1034 .ioctl
= sock_no_ioctl
,
1035 .listen
= sock_no_listen
,
1036 .shutdown
= sock_no_shutdown
,
1037 .setsockopt
= setsockopt
,
1038 .getsockopt
= sock_no_getsockopt
,
1039 .sendmsg
= caif_stream_sendmsg
,
1040 .recvmsg
= caif_stream_recvmsg
,
1041 .mmap
= sock_no_mmap
,
1042 .sendpage
= sock_no_sendpage
,
1045 /* This function is called when a socket is finally destroyed. */
1046 static void caif_sock_destructor(struct sock
*sk
)
1048 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
1049 caif_assert(!atomic_read(&sk
->sk_wmem_alloc
));
1050 caif_assert(sk_unhashed(sk
));
1051 caif_assert(!sk
->sk_socket
);
1052 if (!sock_flag(sk
, SOCK_DEAD
)) {
1053 pr_debug("Attempt to release alive CAIF socket: %p\n", sk
);
1056 sk_stream_kill_queues(&cf_sk
->sk
);
1057 dbfs_atomic_dec(&cnt
.caif_nr_socks
);
1058 caif_free_client(&cf_sk
->layer
);
1061 static int caif_create(struct net
*net
, struct socket
*sock
, int protocol
,
1065 struct sock
*sk
= NULL
;
1066 struct caifsock
*cf_sk
= NULL
;
1067 static struct proto prot
= {.name
= "PF_CAIF",
1068 .owner
= THIS_MODULE
,
1069 .obj_size
= sizeof(struct caifsock
),
1072 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_NET_ADMIN
))
1075 * The sock->type specifies the socket type to use.
1076 * The CAIF socket is a packet stream in the sense
1077 * that it is packet based. CAIF trusts the reliability
1078 * of the link, no resending is implemented.
1080 if (sock
->type
== SOCK_SEQPACKET
)
1081 sock
->ops
= &caif_seqpacket_ops
;
1082 else if (sock
->type
== SOCK_STREAM
)
1083 sock
->ops
= &caif_stream_ops
;
1085 return -ESOCKTNOSUPPORT
;
1087 if (protocol
< 0 || protocol
>= CAIFPROTO_MAX
)
1088 return -EPROTONOSUPPORT
;
1090 * Set the socket state to unconnected. The socket state
1091 * is really not used at all in the net/core or socket.c but the
1092 * initialization makes sure that sock->state is not uninitialized.
1094 sk
= sk_alloc(net
, PF_CAIF
, GFP_KERNEL
, &prot
);
1098 cf_sk
= container_of(sk
, struct caifsock
, sk
);
1100 /* Store the protocol */
1101 sk
->sk_protocol
= (unsigned char) protocol
;
1104 * Lock in order to try to stop someone from opening the socket
1107 lock_sock(&(cf_sk
->sk
));
1109 /* Initialize the nozero default sock structure data. */
1110 sock_init_data(sock
, sk
);
1111 sk
->sk_destruct
= caif_sock_destructor
;
1113 mutex_init(&cf_sk
->readlock
); /* single task reading lock */
1114 cf_sk
->layer
.ctrlcmd
= caif_ctrl_cb
;
1115 cf_sk
->sk
.sk_socket
->state
= SS_UNCONNECTED
;
1116 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
1118 set_tx_flow_off(cf_sk
);
1119 set_rx_flow_on(cf_sk
);
1121 /* Set default options on configuration */
1122 cf_sk
->sk
.sk_priority
= CAIF_PRIO_NORMAL
;
1123 cf_sk
->conn_req
.link_selector
= CAIF_LINK_LOW_LATENCY
;
1124 cf_sk
->conn_req
.protocol
= protocol
;
1125 /* Increase the number of sockets created. */
1126 dbfs_atomic_inc(&cnt
.caif_nr_socks
);
1127 num
= dbfs_atomic_inc(&cnt
.caif_sock_create
);
1128 #ifdef CONFIG_DEBUG_FS
1129 if (!IS_ERR(debugfsdir
)) {
1131 /* Fill in some information concerning the misc socket. */
1132 snprintf(cf_sk
->name
, sizeof(cf_sk
->name
), "cfsk%d", num
);
1134 cf_sk
->debugfs_socket_dir
=
1135 debugfs_create_dir(cf_sk
->name
, debugfsdir
);
1137 debugfs_create_u32("sk_state", S_IRUSR
| S_IWUSR
,
1138 cf_sk
->debugfs_socket_dir
,
1139 (u32
*) &cf_sk
->sk
.sk_state
);
1140 debugfs_create_u32("flow_state", S_IRUSR
| S_IWUSR
,
1141 cf_sk
->debugfs_socket_dir
, &cf_sk
->flow_state
);
1142 debugfs_create_u32("sk_rmem_alloc", S_IRUSR
| S_IWUSR
,
1143 cf_sk
->debugfs_socket_dir
,
1144 (u32
*) &cf_sk
->sk
.sk_rmem_alloc
);
1145 debugfs_create_u32("sk_wmem_alloc", S_IRUSR
| S_IWUSR
,
1146 cf_sk
->debugfs_socket_dir
,
1147 (u32
*) &cf_sk
->sk
.sk_wmem_alloc
);
1148 debugfs_create_u32("identity", S_IRUSR
| S_IWUSR
,
1149 cf_sk
->debugfs_socket_dir
,
1150 (u32
*) &cf_sk
->layer
.id
);
1153 release_sock(&cf_sk
->sk
);
1158 static struct net_proto_family caif_family_ops
= {
1160 .create
= caif_create
,
1161 .owner
= THIS_MODULE
,
1164 static int af_caif_init(void)
1166 int err
= sock_register(&caif_family_ops
);
1172 static int __init
caif_sktinit_module(void)
1174 #ifdef CONFIG_DEBUG_FS
1175 debugfsdir
= debugfs_create_dir("caif_sk", NULL
);
1176 if (!IS_ERR(debugfsdir
)) {
1177 debugfs_create_u32("num_sockets", S_IRUSR
| S_IWUSR
,
1179 (u32
*) &cnt
.caif_nr_socks
);
1180 debugfs_create_u32("num_create", S_IRUSR
| S_IWUSR
,
1182 (u32
*) &cnt
.caif_sock_create
);
1183 debugfs_create_u32("num_connect_req", S_IRUSR
| S_IWUSR
,
1185 (u32
*) &cnt
.num_connect_req
);
1186 debugfs_create_u32("num_connect_resp", S_IRUSR
| S_IWUSR
,
1188 (u32
*) &cnt
.num_connect_resp
);
1189 debugfs_create_u32("num_connect_fail_resp", S_IRUSR
| S_IWUSR
,
1191 (u32
*) &cnt
.num_connect_fail_resp
);
1192 debugfs_create_u32("num_disconnect", S_IRUSR
| S_IWUSR
,
1194 (u32
*) &cnt
.num_disconnect
);
1195 debugfs_create_u32("num_remote_shutdown_ind",
1196 S_IRUSR
| S_IWUSR
, debugfsdir
,
1197 (u32
*) &cnt
.num_remote_shutdown_ind
);
1198 debugfs_create_u32("num_tx_flow_off_ind", S_IRUSR
| S_IWUSR
,
1200 (u32
*) &cnt
.num_tx_flow_off_ind
);
1201 debugfs_create_u32("num_tx_flow_on_ind", S_IRUSR
| S_IWUSR
,
1203 (u32
*) &cnt
.num_tx_flow_on_ind
);
1204 debugfs_create_u32("num_rx_flow_off", S_IRUSR
| S_IWUSR
,
1206 (u32
*) &cnt
.num_rx_flow_off
);
1207 debugfs_create_u32("num_rx_flow_on", S_IRUSR
| S_IWUSR
,
1209 (u32
*) &cnt
.num_rx_flow_on
);
1212 return af_caif_init();
1215 static void __exit
caif_sktexit_module(void)
1217 sock_unregister(PF_CAIF
);
1218 if (debugfsdir
!= NULL
)
1219 debugfs_remove_recursive(debugfsdir
);
1221 module_init(caif_sktinit_module
);
1222 module_exit(caif_sktexit_module
);