2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland
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/signal.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/pkt_sched.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
47 struct sock sk
; /* must be first member */
50 struct caif_connect_request conn_req
;
51 struct mutex readlock
;
52 struct dentry
*debugfs_socket_dir
;
53 int headroom
, tailroom
, maxframe
;
56 static int rx_flow_is_on(struct caifsock
*cf_sk
)
58 return test_bit(RX_FLOW_ON_BIT
,
59 (void *) &cf_sk
->flow_state
);
62 static int tx_flow_is_on(struct caifsock
*cf_sk
)
64 return test_bit(TX_FLOW_ON_BIT
,
65 (void *) &cf_sk
->flow_state
);
68 static void set_rx_flow_off(struct caifsock
*cf_sk
)
70 clear_bit(RX_FLOW_ON_BIT
,
71 (void *) &cf_sk
->flow_state
);
74 static void set_rx_flow_on(struct caifsock
*cf_sk
)
76 set_bit(RX_FLOW_ON_BIT
,
77 (void *) &cf_sk
->flow_state
);
80 static void set_tx_flow_off(struct caifsock
*cf_sk
)
82 clear_bit(TX_FLOW_ON_BIT
,
83 (void *) &cf_sk
->flow_state
);
86 static void set_tx_flow_on(struct caifsock
*cf_sk
)
88 set_bit(TX_FLOW_ON_BIT
,
89 (void *) &cf_sk
->flow_state
);
92 static void caif_read_lock(struct sock
*sk
)
94 struct caifsock
*cf_sk
;
95 cf_sk
= container_of(sk
, struct caifsock
, sk
);
96 mutex_lock(&cf_sk
->readlock
);
99 static void caif_read_unlock(struct sock
*sk
)
101 struct caifsock
*cf_sk
;
102 cf_sk
= container_of(sk
, struct caifsock
, sk
);
103 mutex_unlock(&cf_sk
->readlock
);
106 static int sk_rcvbuf_lowwater(struct caifsock
*cf_sk
)
108 /* A quarter of full buffer is used a low water mark */
109 return cf_sk
->sk
.sk_rcvbuf
/ 4;
112 static void caif_flow_ctrl(struct sock
*sk
, int mode
)
114 struct caifsock
*cf_sk
;
115 cf_sk
= container_of(sk
, struct caifsock
, sk
);
116 if (cf_sk
->layer
.dn
&& cf_sk
->layer
.dn
->modemcmd
)
117 cf_sk
->layer
.dn
->modemcmd(cf_sk
->layer
.dn
, mode
);
121 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
122 * not dropped, but CAIF is sending flow off instead.
124 static void caif_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
128 struct sk_buff_head
*list
= &sk
->sk_receive_queue
;
129 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
132 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
133 (unsigned int)sk
->sk_rcvbuf
&& rx_flow_is_on(cf_sk
)) {
134 net_dbg_ratelimited("sending flow OFF (queue len = %d %d)\n",
135 atomic_read(&cf_sk
->sk
.sk_rmem_alloc
),
136 sk_rcvbuf_lowwater(cf_sk
));
137 set_rx_flow_off(cf_sk
);
138 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_OFF_REQ
);
141 err
= sk_filter(sk
, skb
);
145 if (!sk_rmem_schedule(sk
, skb
, skb
->truesize
) && rx_flow_is_on(cf_sk
)) {
146 set_rx_flow_off(cf_sk
);
147 net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
148 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_OFF_REQ
);
151 skb_set_owner_r(skb
, sk
);
152 spin_lock_irqsave(&list
->lock
, flags
);
153 queued
= !sock_flag(sk
, SOCK_DEAD
);
155 __skb_queue_tail(list
, skb
);
156 spin_unlock_irqrestore(&list
->lock
, flags
);
159 sk
->sk_data_ready(sk
);
164 /* Packet Receive Callback function called from CAIF Stack */
165 static int caif_sktrecv_cb(struct cflayer
*layr
, struct cfpkt
*pkt
)
167 struct caifsock
*cf_sk
;
170 cf_sk
= container_of(layr
, struct caifsock
, layer
);
171 skb
= cfpkt_tonative(pkt
);
173 if (unlikely(cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
)) {
177 caif_queue_rcv_skb(&cf_sk
->sk
, skb
);
181 static void cfsk_hold(struct cflayer
*layr
)
183 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
184 sock_hold(&cf_sk
->sk
);
187 static void cfsk_put(struct cflayer
*layr
)
189 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
190 sock_put(&cf_sk
->sk
);
193 /* Packet Control Callback function called from CAIF */
194 static void caif_ctrl_cb(struct cflayer
*layr
,
195 enum caif_ctrlcmd flow
,
198 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
200 case CAIF_CTRLCMD_FLOW_ON_IND
:
201 /* OK from modem to start sending again */
202 set_tx_flow_on(cf_sk
);
203 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
206 case CAIF_CTRLCMD_FLOW_OFF_IND
:
207 /* Modem asks us to shut up */
208 set_tx_flow_off(cf_sk
);
209 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
212 case CAIF_CTRLCMD_INIT_RSP
:
213 /* We're now connected */
214 caif_client_register_refcnt(&cf_sk
->layer
,
215 cfsk_hold
, cfsk_put
);
216 cf_sk
->sk
.sk_state
= CAIF_CONNECTED
;
217 set_tx_flow_on(cf_sk
);
218 cf_sk
->sk
.sk_shutdown
= 0;
219 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
222 case CAIF_CTRLCMD_DEINIT_RSP
:
223 /* We're now disconnected */
224 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
225 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
228 case CAIF_CTRLCMD_INIT_FAIL_RSP
:
229 /* Connect request failed */
230 cf_sk
->sk
.sk_err
= ECONNREFUSED
;
231 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
232 cf_sk
->sk
.sk_shutdown
= SHUTDOWN_MASK
;
234 * Socket "standards" seems to require POLLOUT to
235 * be set at connect failure.
237 set_tx_flow_on(cf_sk
);
238 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
241 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
:
242 /* Modem has closed this connection, or device is down. */
243 cf_sk
->sk
.sk_shutdown
= SHUTDOWN_MASK
;
244 cf_sk
->sk
.sk_err
= ECONNRESET
;
245 set_rx_flow_on(cf_sk
);
246 cf_sk
->sk
.sk_error_report(&cf_sk
->sk
);
250 pr_debug("Unexpected flow command %d\n", flow
);
254 static void caif_check_flow_release(struct sock
*sk
)
256 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
258 if (rx_flow_is_on(cf_sk
))
261 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk_rcvbuf_lowwater(cf_sk
)) {
262 set_rx_flow_on(cf_sk
);
263 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_ON_REQ
);
268 * Copied from unix_dgram_recvmsg, but removed credit checks,
269 * changed locking, address handling and added MSG_TRUNC.
271 static int caif_seqpkt_recvmsg(struct socket
*sock
, struct msghdr
*m
,
272 size_t len
, int flags
)
275 struct sock
*sk
= sock
->sk
;
284 skb
= skb_recv_datagram(sk
, flags
, 0 , &ret
);
289 m
->msg_flags
|= MSG_TRUNC
;
293 ret
= skb_copy_datagram_msg(skb
, 0, m
, copylen
);
297 ret
= (flags
& MSG_TRUNC
) ? skb
->len
: copylen
;
299 skb_free_datagram(sk
, skb
);
300 caif_check_flow_release(sk
);
308 /* Copied from unix_stream_wait_data, identical except for lock call. */
309 static long caif_stream_data_wait(struct sock
*sk
, long timeo
)
315 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
317 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
319 sk
->sk_state
!= CAIF_CONNECTED
||
320 sock_flag(sk
, SOCK_DEAD
) ||
321 (sk
->sk_shutdown
& RCV_SHUTDOWN
) ||
322 signal_pending(current
) ||
326 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
328 timeo
= schedule_timeout(timeo
);
331 if (sock_flag(sk
, SOCK_DEAD
))
334 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
337 finish_wait(sk_sleep(sk
), &wait
);
344 * Copied from unix_stream_recvmsg, but removed credit checks,
345 * changed locking calls, changed address handling.
347 static int caif_stream_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
348 size_t size
, int flags
)
350 struct sock
*sk
= sock
->sk
;
361 * Lock the socket to prevent queue disordering
362 * while sleeps in memcpy_tomsg
365 if (sk
->sk_state
== CAIF_CONNECTING
)
369 target
= sock_rcvlowat(sk
, flags
&MSG_WAITALL
, size
);
370 timeo
= sock_rcvtimeo(sk
, flags
&MSG_DONTWAIT
);
377 if (sock_flag(sk
, SOCK_DEAD
)) {
381 skb
= skb_dequeue(&sk
->sk_receive_queue
);
382 caif_check_flow_release(sk
);
385 if (copied
>= target
)
388 * POSIX 1003.1g mandates this order.
390 err
= sock_error(sk
);
394 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
398 if (sk
->sk_state
!= CAIF_CONNECTED
)
400 if (sock_flag(sk
, SOCK_DEAD
))
409 caif_read_unlock(sk
);
411 timeo
= caif_stream_data_wait(sk
, timeo
);
413 if (signal_pending(current
)) {
414 err
= sock_intr_errno(timeo
);
424 chunk
= min_t(unsigned int, skb
->len
, size
);
425 if (memcpy_to_msg(msg
, skb
->data
, chunk
)) {
426 skb_queue_head(&sk
->sk_receive_queue
, skb
);
434 /* Mark read part of skb as used */
435 if (!(flags
& MSG_PEEK
)) {
436 skb_pull(skb
, chunk
);
438 /* put the skb back if we didn't use it up. */
440 skb_queue_head(&sk
->sk_receive_queue
, skb
);
447 * It is questionable, see note in unix_dgram_recvmsg.
449 /* put message back and return */
450 skb_queue_head(&sk
->sk_receive_queue
, skb
);
454 caif_read_unlock(sk
);
457 return copied
? : err
;
461 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
462 * CAIF flow-on and sock_writable.
464 static long caif_wait_for_flow_on(struct caifsock
*cf_sk
,
465 int wait_writeable
, long timeo
, int *err
)
467 struct sock
*sk
= &cf_sk
->sk
;
471 if (tx_flow_is_on(cf_sk
) &&
472 (!wait_writeable
|| sock_writeable(&cf_sk
->sk
)))
478 if (signal_pending(current
))
480 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
482 if (sk
->sk_shutdown
& SHUTDOWN_MASK
)
488 if (cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
)
490 timeo
= schedule_timeout(timeo
);
492 finish_wait(sk_sleep(sk
), &wait
);
497 * Transmit a SKB. The device may temporarily request re-transmission
498 * by returning EAGAIN.
500 static int transmit_skb(struct sk_buff
*skb
, struct caifsock
*cf_sk
,
501 int noblock
, long timeo
)
505 pkt
= cfpkt_fromnative(CAIF_DIR_OUT
, skb
);
506 memset(skb
->cb
, 0, sizeof(struct caif_payload_info
));
507 cfpkt_set_prio(pkt
, cf_sk
->sk
.sk_priority
);
509 if (cf_sk
->layer
.dn
== NULL
) {
514 return cf_sk
->layer
.dn
->transmit(cf_sk
->layer
.dn
, pkt
);
517 /* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
518 static int caif_seqpkt_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
521 struct sock
*sk
= sock
->sk
;
522 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
525 struct sk_buff
*skb
= NULL
;
529 ret
= sock_error(sk
);
534 if (msg
->msg_flags
&MSG_OOB
)
538 if (msg
->msg_namelen
)
542 if (unlikely(msg
->msg_iter
.iov
->iov_base
== NULL
))
544 noblock
= msg
->msg_flags
& MSG_DONTWAIT
;
546 timeo
= sock_sndtimeo(sk
, noblock
);
547 timeo
= caif_wait_for_flow_on(container_of(sk
, struct caifsock
, sk
),
553 if (cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
||
554 sock_flag(sk
, SOCK_DEAD
) ||
555 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
558 /* Error if trying to write more than maximum frame size. */
560 if (len
> cf_sk
->maxframe
&& cf_sk
->sk
.sk_protocol
!= CAIFPROTO_RFM
)
563 buffer_size
= len
+ cf_sk
->headroom
+ cf_sk
->tailroom
;
566 skb
= sock_alloc_send_skb(sk
, buffer_size
, noblock
, &ret
);
568 if (!skb
|| skb_tailroom(skb
) < buffer_size
)
571 skb_reserve(skb
, cf_sk
->headroom
);
573 ret
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
577 ret
= transmit_skb(skb
, cf_sk
, noblock
, timeo
);
579 /* skb is already freed */
589 * Copied from unix_stream_sendmsg and adapted to CAIF:
590 * Changed removed permission handling and added waiting for flow on
591 * and other minor adaptations.
593 static int caif_stream_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
596 struct sock
*sk
= sock
->sk
;
597 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
604 if (unlikely(msg
->msg_flags
&MSG_OOB
))
607 if (unlikely(msg
->msg_namelen
))
610 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
611 timeo
= caif_wait_for_flow_on(cf_sk
, 1, timeo
, &err
);
613 if (unlikely(sk
->sk_shutdown
& SEND_SHUTDOWN
))
620 if (size
> cf_sk
->maxframe
)
621 size
= cf_sk
->maxframe
;
623 /* If size is more than half of sndbuf, chop up message */
624 if (size
> ((sk
->sk_sndbuf
>> 1) - 64))
625 size
= (sk
->sk_sndbuf
>> 1) - 64;
627 if (size
> SKB_MAX_ALLOC
)
628 size
= SKB_MAX_ALLOC
;
630 skb
= sock_alloc_send_skb(sk
,
631 size
+ cf_sk
->headroom
+
633 msg
->msg_flags
&MSG_DONTWAIT
,
638 skb_reserve(skb
, cf_sk
->headroom
);
640 * If you pass two values to the sock_alloc_send_skb
641 * it tries to grab the large buffer with GFP_NOFS
642 * (which can fail easily), and if it fails grab the
643 * fallback size buffer which is under a page and will
646 size
= min_t(int, size
, skb_tailroom(skb
));
648 err
= memcpy_from_msg(skb_put(skb
, size
), msg
, size
);
653 err
= transmit_skb(skb
, cf_sk
,
654 msg
->msg_flags
&MSG_DONTWAIT
, timeo
);
656 /* skb is already freed */
665 if (sent
== 0 && !(msg
->msg_flags
&MSG_NOSIGNAL
))
666 send_sig(SIGPIPE
, current
, 0);
672 static int setsockopt(struct socket
*sock
,
673 int lvl
, int opt
, char __user
*ov
, unsigned int ol
)
675 struct sock
*sk
= sock
->sk
;
676 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
679 if (cf_sk
->sk
.sk_socket
->state
!= SS_UNCONNECTED
)
683 case CAIFSO_LINK_SELECT
:
684 if (ol
< sizeof(int))
688 if (copy_from_user(&linksel
, ov
, sizeof(int)))
690 lock_sock(&(cf_sk
->sk
));
691 cf_sk
->conn_req
.link_selector
= linksel
;
692 release_sock(&cf_sk
->sk
);
695 case CAIFSO_REQ_PARAM
:
698 if (cf_sk
->sk
.sk_protocol
!= CAIFPROTO_UTIL
)
700 lock_sock(&(cf_sk
->sk
));
701 if (ol
> sizeof(cf_sk
->conn_req
.param
.data
) ||
702 copy_from_user(&cf_sk
->conn_req
.param
.data
, ov
, ol
)) {
703 release_sock(&cf_sk
->sk
);
706 cf_sk
->conn_req
.param
.size
= ol
;
707 release_sock(&cf_sk
->sk
);
721 * caif_connect() - Connect a CAIF Socket
722 * Copied and modified af_irda.c:irda_connect().
724 * Note : by consulting "errno", the user space caller may learn the cause
725 * of the failure. Most of them are visible in the function, others may come
726 * from subroutines called and are listed here :
727 * o -EAFNOSUPPORT: bad socket family or type.
728 * o -ESOCKTNOSUPPORT: bad socket type or protocol
729 * o -EINVAL: bad socket address, or CAIF link type
730 * o -ECONNREFUSED: remote end refused the connection.
731 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
732 * o -EISCONN: already connected.
733 * o -ETIMEDOUT: Connection timed out (send timeout)
734 * o -ENODEV: No link layer to send request
735 * o -ECONNRESET: Received Shutdown indication or lost link layer
736 * o -ENOMEM: Out of memory
739 * o sk_state: holds the CAIF_* protocol state, it's updated by
741 * o sock->state: holds the SS_* socket state and is updated by connect and
744 static int caif_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
745 int addr_len
, int flags
)
747 struct sock
*sk
= sock
->sk
;
748 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
751 int ifindex
, headroom
, tailroom
;
753 struct net_device
*dev
;
758 if (addr_len
< offsetofend(struct sockaddr
, sa_family
))
762 if (uaddr
->sa_family
!= AF_CAIF
)
765 switch (sock
->state
) {
767 /* Normal case, a fresh connect */
768 caif_assert(sk
->sk_state
== CAIF_DISCONNECTED
);
771 switch (sk
->sk_state
) {
773 sock
->state
= SS_CONNECTED
;
776 case CAIF_DISCONNECTED
:
777 /* Reconnect allowed */
779 case CAIF_CONNECTING
:
781 if (flags
& O_NONBLOCK
)
787 caif_assert(sk
->sk_state
== CAIF_CONNECTED
||
788 sk
->sk_state
== CAIF_DISCONNECTED
);
789 if (sk
->sk_shutdown
& SHUTDOWN_MASK
) {
790 /* Allow re-connect after SHUTDOWN_IND */
791 caif_disconnect_client(sock_net(sk
), &cf_sk
->layer
);
792 caif_free_client(&cf_sk
->layer
);
795 /* No reconnect on a seqpacket socket */
798 case SS_DISCONNECTING
:
800 caif_assert(1); /*Should never happen */
803 sk
->sk_state
= CAIF_DISCONNECTED
;
804 sock
->state
= SS_UNCONNECTED
;
805 sk_stream_kill_queues(&cf_sk
->sk
);
808 if (addr_len
!= sizeof(struct sockaddr_caif
))
811 memcpy(&cf_sk
->conn_req
.sockaddr
, uaddr
,
812 sizeof(struct sockaddr_caif
));
814 /* Move to connecting socket, start sending Connect Requests */
815 sock
->state
= SS_CONNECTING
;
816 sk
->sk_state
= CAIF_CONNECTING
;
818 /* Check priority value comming from socket */
819 /* if priority value is out of range it will be ajusted */
820 if (cf_sk
->sk
.sk_priority
> CAIF_PRIO_MAX
)
821 cf_sk
->conn_req
.priority
= CAIF_PRIO_MAX
;
822 else if (cf_sk
->sk
.sk_priority
< CAIF_PRIO_MIN
)
823 cf_sk
->conn_req
.priority
= CAIF_PRIO_MIN
;
825 cf_sk
->conn_req
.priority
= cf_sk
->sk
.sk_priority
;
827 /*ifindex = id of the interface.*/
828 cf_sk
->conn_req
.ifindex
= cf_sk
->sk
.sk_bound_dev_if
;
830 cf_sk
->layer
.receive
= caif_sktrecv_cb
;
832 err
= caif_connect_client(sock_net(sk
), &cf_sk
->conn_req
,
833 &cf_sk
->layer
, &ifindex
, &headroom
, &tailroom
);
836 cf_sk
->sk
.sk_socket
->state
= SS_UNCONNECTED
;
837 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
843 dev
= dev_get_by_index_rcu(sock_net(sk
), ifindex
);
848 cf_sk
->headroom
= LL_RESERVED_SPACE_EXTRA(dev
, headroom
);
852 cf_sk
->tailroom
= tailroom
;
853 cf_sk
->maxframe
= mtu
- (headroom
+ tailroom
);
854 if (cf_sk
->maxframe
< 1) {
855 pr_warn("CAIF Interface MTU too small (%d)\n", dev
->mtu
);
863 if (sk
->sk_state
!= CAIF_CONNECTED
&& (flags
& O_NONBLOCK
))
866 timeo
= sock_sndtimeo(sk
, flags
& O_NONBLOCK
);
870 timeo
= wait_event_interruptible_timeout(*sk_sleep(sk
),
871 sk
->sk_state
!= CAIF_CONNECTING
,
875 goto out
; /* -ERESTARTSYS */
878 if (timeo
== 0 && sk
->sk_state
!= CAIF_CONNECTED
)
880 if (sk
->sk_state
!= CAIF_CONNECTED
) {
881 sock
->state
= SS_UNCONNECTED
;
882 err
= sock_error(sk
);
887 sock
->state
= SS_CONNECTED
;
895 * caif_release() - Disconnect a CAIF Socket
896 * Copied and modified af_irda.c:irda_release().
898 static int caif_release(struct socket
*sock
)
900 struct sock
*sk
= sock
->sk
;
901 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
906 set_tx_flow_off(cf_sk
);
909 * Ensure that packets are not queued after this point in time.
910 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
911 * this ensures no packets when sock is dead.
913 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
914 sock_set_flag(sk
, SOCK_DEAD
);
915 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
918 WARN_ON(IS_ERR(cf_sk
->debugfs_socket_dir
));
919 debugfs_remove_recursive(cf_sk
->debugfs_socket_dir
);
921 lock_sock(&(cf_sk
->sk
));
922 sk
->sk_state
= CAIF_DISCONNECTED
;
923 sk
->sk_shutdown
= SHUTDOWN_MASK
;
925 caif_disconnect_client(sock_net(sk
), &cf_sk
->layer
);
926 cf_sk
->sk
.sk_socket
->state
= SS_DISCONNECTING
;
927 wake_up_interruptible_poll(sk_sleep(sk
), EPOLLERR
|EPOLLHUP
);
930 sk_stream_kill_queues(&cf_sk
->sk
);
936 /* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
937 static __poll_t
caif_poll(struct file
*file
,
938 struct socket
*sock
, poll_table
*wait
)
940 struct sock
*sk
= sock
->sk
;
942 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
944 sock_poll_wait(file
, sock
, wait
);
947 /* exceptional events? */
950 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
952 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
956 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
957 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
958 mask
|= EPOLLIN
| EPOLLRDNORM
;
961 * we set writable also when the other side has shut down the
962 * connection. This prevents stuck sockets.
964 if (sock_writeable(sk
) && tx_flow_is_on(cf_sk
))
965 mask
|= EPOLLOUT
| EPOLLWRNORM
| EPOLLWRBAND
;
970 static const struct proto_ops caif_seqpacket_ops
= {
972 .owner
= THIS_MODULE
,
973 .release
= caif_release
,
974 .bind
= sock_no_bind
,
975 .connect
= caif_connect
,
976 .socketpair
= sock_no_socketpair
,
977 .accept
= sock_no_accept
,
978 .getname
= sock_no_getname
,
980 .ioctl
= sock_no_ioctl
,
981 .listen
= sock_no_listen
,
982 .shutdown
= sock_no_shutdown
,
983 .setsockopt
= setsockopt
,
984 .getsockopt
= sock_no_getsockopt
,
985 .sendmsg
= caif_seqpkt_sendmsg
,
986 .recvmsg
= caif_seqpkt_recvmsg
,
987 .mmap
= sock_no_mmap
,
988 .sendpage
= sock_no_sendpage
,
991 static const struct proto_ops caif_stream_ops
= {
993 .owner
= THIS_MODULE
,
994 .release
= caif_release
,
995 .bind
= sock_no_bind
,
996 .connect
= caif_connect
,
997 .socketpair
= sock_no_socketpair
,
998 .accept
= sock_no_accept
,
999 .getname
= sock_no_getname
,
1001 .ioctl
= sock_no_ioctl
,
1002 .listen
= sock_no_listen
,
1003 .shutdown
= sock_no_shutdown
,
1004 .setsockopt
= setsockopt
,
1005 .getsockopt
= sock_no_getsockopt
,
1006 .sendmsg
= caif_stream_sendmsg
,
1007 .recvmsg
= caif_stream_recvmsg
,
1008 .mmap
= sock_no_mmap
,
1009 .sendpage
= sock_no_sendpage
,
1012 /* This function is called when a socket is finally destroyed. */
1013 static void caif_sock_destructor(struct sock
*sk
)
1015 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
1016 caif_assert(!refcount_read(&sk
->sk_wmem_alloc
));
1017 caif_assert(sk_unhashed(sk
));
1018 caif_assert(!sk
->sk_socket
);
1019 if (!sock_flag(sk
, SOCK_DEAD
)) {
1020 pr_debug("Attempt to release alive CAIF socket: %p\n", sk
);
1023 sk_stream_kill_queues(&cf_sk
->sk
);
1024 caif_free_client(&cf_sk
->layer
);
1027 static int caif_create(struct net
*net
, struct socket
*sock
, int protocol
,
1030 struct sock
*sk
= NULL
;
1031 struct caifsock
*cf_sk
= NULL
;
1032 static struct proto prot
= {.name
= "PF_CAIF",
1033 .owner
= THIS_MODULE
,
1034 .obj_size
= sizeof(struct caifsock
),
1035 .useroffset
= offsetof(struct caifsock
, conn_req
.param
),
1036 .usersize
= sizeof_field(struct caifsock
, conn_req
.param
)
1039 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_NET_ADMIN
))
1042 * The sock->type specifies the socket type to use.
1043 * The CAIF socket is a packet stream in the sense
1044 * that it is packet based. CAIF trusts the reliability
1045 * of the link, no resending is implemented.
1047 if (sock
->type
== SOCK_SEQPACKET
)
1048 sock
->ops
= &caif_seqpacket_ops
;
1049 else if (sock
->type
== SOCK_STREAM
)
1050 sock
->ops
= &caif_stream_ops
;
1052 return -ESOCKTNOSUPPORT
;
1054 if (protocol
< 0 || protocol
>= CAIFPROTO_MAX
)
1055 return -EPROTONOSUPPORT
;
1057 * Set the socket state to unconnected. The socket state
1058 * is really not used at all in the net/core or socket.c but the
1059 * initialization makes sure that sock->state is not uninitialized.
1061 sk
= sk_alloc(net
, PF_CAIF
, GFP_KERNEL
, &prot
, kern
);
1065 cf_sk
= container_of(sk
, struct caifsock
, sk
);
1067 /* Store the protocol */
1068 sk
->sk_protocol
= (unsigned char) protocol
;
1070 /* Initialize default priority for well-known cases */
1073 sk
->sk_priority
= TC_PRIO_CONTROL
;
1076 sk
->sk_priority
= TC_PRIO_INTERACTIVE_BULK
;
1079 sk
->sk_priority
= TC_PRIO_BESTEFFORT
;
1083 * Lock in order to try to stop someone from opening the socket
1086 lock_sock(&(cf_sk
->sk
));
1088 /* Initialize the nozero default sock structure data. */
1089 sock_init_data(sock
, sk
);
1090 sk
->sk_destruct
= caif_sock_destructor
;
1092 mutex_init(&cf_sk
->readlock
); /* single task reading lock */
1093 cf_sk
->layer
.ctrlcmd
= caif_ctrl_cb
;
1094 cf_sk
->sk
.sk_socket
->state
= SS_UNCONNECTED
;
1095 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
1097 set_tx_flow_off(cf_sk
);
1098 set_rx_flow_on(cf_sk
);
1100 /* Set default options on configuration */
1101 cf_sk
->conn_req
.link_selector
= CAIF_LINK_LOW_LATENCY
;
1102 cf_sk
->conn_req
.protocol
= protocol
;
1103 release_sock(&cf_sk
->sk
);
1108 static const struct net_proto_family caif_family_ops
= {
1110 .create
= caif_create
,
1111 .owner
= THIS_MODULE
,
1114 static int __init
caif_sktinit_module(void)
1116 return sock_register(&caif_family_ops
);
1119 static void __exit
caif_sktexit_module(void)
1121 sock_unregister(PF_CAIF
);
1123 module_init(caif_sktinit_module
);
1124 module_exit(caif_sktexit_module
);