1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson AB 2010
4 * Author: Sjur Brendeland
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
9 #include <linux/filter.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/sched/signal.h>
14 #include <linux/spinlock.h>
15 #include <linux/mutex.h>
16 #include <linux/list.h>
17 #include <linux/wait.h>
18 #include <linux/poll.h>
19 #include <linux/tcp.h>
20 #include <linux/uaccess.h>
21 #include <linux/debugfs.h>
22 #include <linux/caif/caif_socket.h>
23 #include <linux/pkt_sched.h>
25 #include <net/tcp_states.h>
26 #include <net/caif/caif_layer.h>
27 #include <net/caif/caif_dev.h>
28 #include <net/caif/cfpkt.h>
30 MODULE_DESCRIPTION("ST-Ericsson CAIF modem protocol socket support (AF_CAIF)");
31 MODULE_LICENSE("GPL");
32 MODULE_ALIAS_NETPROTO(AF_CAIF
);
35 * CAIF state is re-using the TCP socket states.
36 * caif_states stored in sk_state reflect the state as reported by
37 * the CAIF stack, while sk_socket->state is the state of the socket.
40 CAIF_CONNECTED
= TCP_ESTABLISHED
,
41 CAIF_CONNECTING
= TCP_SYN_SENT
,
42 CAIF_DISCONNECTED
= TCP_CLOSE
45 #define TX_FLOW_ON_BIT 1
46 #define RX_FLOW_ON_BIT 2
49 struct sock sk
; /* must be first member */
51 unsigned long flow_state
;
52 struct caif_connect_request conn_req
;
53 struct mutex readlock
;
54 struct dentry
*debugfs_socket_dir
;
55 int headroom
, tailroom
, maxframe
;
58 static int rx_flow_is_on(struct caifsock
*cf_sk
)
60 return test_bit(RX_FLOW_ON_BIT
, &cf_sk
->flow_state
);
63 static int tx_flow_is_on(struct caifsock
*cf_sk
)
65 return test_bit(TX_FLOW_ON_BIT
, &cf_sk
->flow_state
);
68 static void set_rx_flow_off(struct caifsock
*cf_sk
)
70 clear_bit(RX_FLOW_ON_BIT
, &cf_sk
->flow_state
);
73 static void set_rx_flow_on(struct caifsock
*cf_sk
)
75 set_bit(RX_FLOW_ON_BIT
, &cf_sk
->flow_state
);
78 static void set_tx_flow_off(struct caifsock
*cf_sk
)
80 clear_bit(TX_FLOW_ON_BIT
, &cf_sk
->flow_state
);
83 static void set_tx_flow_on(struct caifsock
*cf_sk
)
85 set_bit(TX_FLOW_ON_BIT
, &cf_sk
->flow_state
);
88 static void caif_read_lock(struct sock
*sk
)
90 struct caifsock
*cf_sk
;
91 cf_sk
= container_of(sk
, struct caifsock
, sk
);
92 mutex_lock(&cf_sk
->readlock
);
95 static void caif_read_unlock(struct sock
*sk
)
97 struct caifsock
*cf_sk
;
98 cf_sk
= container_of(sk
, struct caifsock
, sk
);
99 mutex_unlock(&cf_sk
->readlock
);
102 static int sk_rcvbuf_lowwater(struct caifsock
*cf_sk
)
104 /* A quarter of full buffer is used a low water mark */
105 return cf_sk
->sk
.sk_rcvbuf
/ 4;
108 static void caif_flow_ctrl(struct sock
*sk
, int mode
)
110 struct caifsock
*cf_sk
;
111 cf_sk
= container_of(sk
, struct caifsock
, sk
);
112 if (cf_sk
->layer
.dn
&& cf_sk
->layer
.dn
->modemcmd
)
113 cf_sk
->layer
.dn
->modemcmd(cf_sk
->layer
.dn
, mode
);
117 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
118 * not dropped, but CAIF is sending flow off instead.
120 static void caif_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
124 struct sk_buff_head
*list
= &sk
->sk_receive_queue
;
125 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
128 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
129 (unsigned int)sk
->sk_rcvbuf
&& rx_flow_is_on(cf_sk
)) {
130 net_dbg_ratelimited("sending flow OFF (queue len = %d %d)\n",
131 atomic_read(&cf_sk
->sk
.sk_rmem_alloc
),
132 sk_rcvbuf_lowwater(cf_sk
));
133 set_rx_flow_off(cf_sk
);
134 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_OFF_REQ
);
137 err
= sk_filter(sk
, skb
);
141 if (!sk_rmem_schedule(sk
, skb
, skb
->truesize
) && rx_flow_is_on(cf_sk
)) {
142 set_rx_flow_off(cf_sk
);
143 net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
144 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_OFF_REQ
);
147 skb_set_owner_r(skb
, sk
);
148 spin_lock_irqsave(&list
->lock
, flags
);
149 queued
= !sock_flag(sk
, SOCK_DEAD
);
151 __skb_queue_tail(list
, skb
);
152 spin_unlock_irqrestore(&list
->lock
, flags
);
155 sk
->sk_data_ready(sk
);
160 /* Packet Receive Callback function called from CAIF Stack */
161 static int caif_sktrecv_cb(struct cflayer
*layr
, struct cfpkt
*pkt
)
163 struct caifsock
*cf_sk
;
166 cf_sk
= container_of(layr
, struct caifsock
, layer
);
167 skb
= cfpkt_tonative(pkt
);
169 if (unlikely(cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
)) {
173 caif_queue_rcv_skb(&cf_sk
->sk
, skb
);
177 static void cfsk_hold(struct cflayer
*layr
)
179 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
180 sock_hold(&cf_sk
->sk
);
183 static void cfsk_put(struct cflayer
*layr
)
185 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
186 sock_put(&cf_sk
->sk
);
189 /* Packet Control Callback function called from CAIF */
190 static void caif_ctrl_cb(struct cflayer
*layr
,
191 enum caif_ctrlcmd flow
,
194 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
196 case CAIF_CTRLCMD_FLOW_ON_IND
:
197 /* OK from modem to start sending again */
198 set_tx_flow_on(cf_sk
);
199 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
202 case CAIF_CTRLCMD_FLOW_OFF_IND
:
203 /* Modem asks us to shut up */
204 set_tx_flow_off(cf_sk
);
205 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
208 case CAIF_CTRLCMD_INIT_RSP
:
209 /* We're now connected */
210 caif_client_register_refcnt(&cf_sk
->layer
,
211 cfsk_hold
, cfsk_put
);
212 cf_sk
->sk
.sk_state
= CAIF_CONNECTED
;
213 set_tx_flow_on(cf_sk
);
214 cf_sk
->sk
.sk_shutdown
= 0;
215 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
218 case CAIF_CTRLCMD_DEINIT_RSP
:
219 /* We're now disconnected */
220 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
221 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
224 case CAIF_CTRLCMD_INIT_FAIL_RSP
:
225 /* Connect request failed */
226 cf_sk
->sk
.sk_err
= ECONNREFUSED
;
227 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
228 cf_sk
->sk
.sk_shutdown
= SHUTDOWN_MASK
;
230 * Socket "standards" seems to require POLLOUT to
231 * be set at connect failure.
233 set_tx_flow_on(cf_sk
);
234 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
237 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
:
238 /* Modem has closed this connection, or device is down. */
239 cf_sk
->sk
.sk_shutdown
= SHUTDOWN_MASK
;
240 cf_sk
->sk
.sk_err
= ECONNRESET
;
241 set_rx_flow_on(cf_sk
);
242 sk_error_report(&cf_sk
->sk
);
246 pr_debug("Unexpected flow command %d\n", flow
);
250 static void caif_check_flow_release(struct sock
*sk
)
252 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
254 if (rx_flow_is_on(cf_sk
))
257 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk_rcvbuf_lowwater(cf_sk
)) {
258 set_rx_flow_on(cf_sk
);
259 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_ON_REQ
);
264 * Copied from unix_dgram_recvmsg, but removed credit checks,
265 * changed locking, address handling and added MSG_TRUNC.
267 static int caif_seqpkt_recvmsg(struct socket
*sock
, struct msghdr
*m
,
268 size_t len
, int flags
)
271 struct sock
*sk
= sock
->sk
;
280 skb
= skb_recv_datagram(sk
, flags
, &ret
);
285 m
->msg_flags
|= MSG_TRUNC
;
289 ret
= skb_copy_datagram_msg(skb
, 0, m
, copylen
);
293 ret
= (flags
& MSG_TRUNC
) ? skb
->len
: copylen
;
295 skb_free_datagram(sk
, skb
);
296 caif_check_flow_release(sk
);
304 /* Copied from unix_stream_wait_data, identical except for lock call. */
305 static long caif_stream_data_wait(struct sock
*sk
, long timeo
)
311 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
313 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
315 sk
->sk_state
!= CAIF_CONNECTED
||
316 sock_flag(sk
, SOCK_DEAD
) ||
317 (sk
->sk_shutdown
& RCV_SHUTDOWN
) ||
318 signal_pending(current
) ||
322 sk_set_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
324 timeo
= schedule_timeout(timeo
);
327 if (sock_flag(sk
, SOCK_DEAD
))
330 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA
, sk
);
333 finish_wait(sk_sleep(sk
), &wait
);
340 * Copied from unix_stream_recvmsg, but removed credit checks,
341 * changed locking calls, changed address handling.
343 static int caif_stream_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
344 size_t size
, int flags
)
346 struct sock
*sk
= sock
->sk
;
357 * Lock the socket to prevent queue disordering
358 * while sleeps in memcpy_tomsg
361 if (sk
->sk_state
== CAIF_CONNECTING
)
365 target
= sock_rcvlowat(sk
, flags
&MSG_WAITALL
, size
);
366 timeo
= sock_rcvtimeo(sk
, flags
&MSG_DONTWAIT
);
373 if (sock_flag(sk
, SOCK_DEAD
)) {
377 skb
= skb_dequeue(&sk
->sk_receive_queue
);
378 caif_check_flow_release(sk
);
381 if (copied
>= target
)
384 * POSIX 1003.1g mandates this order.
386 err
= sock_error(sk
);
390 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
394 if (sk
->sk_state
!= CAIF_CONNECTED
)
396 if (sock_flag(sk
, SOCK_DEAD
))
405 caif_read_unlock(sk
);
407 timeo
= caif_stream_data_wait(sk
, timeo
);
409 if (signal_pending(current
)) {
410 err
= sock_intr_errno(timeo
);
420 chunk
= min_t(unsigned int, skb
->len
, size
);
421 if (memcpy_to_msg(msg
, skb
->data
, chunk
)) {
422 skb_queue_head(&sk
->sk_receive_queue
, skb
);
430 /* Mark read part of skb as used */
431 if (!(flags
& MSG_PEEK
)) {
432 skb_pull(skb
, chunk
);
434 /* put the skb back if we didn't use it up. */
436 skb_queue_head(&sk
->sk_receive_queue
, skb
);
443 * It is questionable, see note in unix_dgram_recvmsg.
445 /* put message back and return */
446 skb_queue_head(&sk
->sk_receive_queue
, skb
);
450 caif_read_unlock(sk
);
453 return copied
? : err
;
457 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
458 * CAIF flow-on and sock_writable.
460 static long caif_wait_for_flow_on(struct caifsock
*cf_sk
,
461 int wait_writeable
, long timeo
, int *err
)
463 struct sock
*sk
= &cf_sk
->sk
;
467 if (tx_flow_is_on(cf_sk
) &&
468 (!wait_writeable
|| sock_writeable(&cf_sk
->sk
)))
474 if (signal_pending(current
))
476 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
478 if (sk
->sk_shutdown
& SHUTDOWN_MASK
)
484 if (cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
)
486 timeo
= schedule_timeout(timeo
);
488 finish_wait(sk_sleep(sk
), &wait
);
493 * Transmit a SKB. The device may temporarily request re-transmission
494 * by returning EAGAIN.
496 static int transmit_skb(struct sk_buff
*skb
, struct caifsock
*cf_sk
,
497 int noblock
, long timeo
)
501 pkt
= cfpkt_fromnative(CAIF_DIR_OUT
, skb
);
502 memset(skb
->cb
, 0, sizeof(struct caif_payload_info
));
503 cfpkt_set_prio(pkt
, cf_sk
->sk
.sk_priority
);
505 if (cf_sk
->layer
.dn
== NULL
) {
510 return cf_sk
->layer
.dn
->transmit(cf_sk
->layer
.dn
, pkt
);
513 /* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
514 static int caif_seqpkt_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
517 struct sock
*sk
= sock
->sk
;
518 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
521 struct sk_buff
*skb
= NULL
;
525 ret
= sock_error(sk
);
530 if (msg
->msg_flags
&MSG_OOB
)
534 if (msg
->msg_namelen
)
537 noblock
= msg
->msg_flags
& MSG_DONTWAIT
;
539 timeo
= sock_sndtimeo(sk
, noblock
);
540 timeo
= caif_wait_for_flow_on(container_of(sk
, struct caifsock
, sk
),
546 if (cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
||
547 sock_flag(sk
, SOCK_DEAD
) ||
548 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
551 /* Error if trying to write more than maximum frame size. */
553 if (len
> cf_sk
->maxframe
&& cf_sk
->sk
.sk_protocol
!= CAIFPROTO_RFM
)
556 buffer_size
= len
+ cf_sk
->headroom
+ cf_sk
->tailroom
;
559 skb
= sock_alloc_send_skb(sk
, buffer_size
, noblock
, &ret
);
561 if (!skb
|| skb_tailroom(skb
) < buffer_size
)
564 skb_reserve(skb
, cf_sk
->headroom
);
566 ret
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
570 ret
= transmit_skb(skb
, cf_sk
, noblock
, timeo
);
572 /* skb is already freed */
582 * Copied from unix_stream_sendmsg and adapted to CAIF:
583 * Changed removed permission handling and added waiting for flow on
584 * and other minor adaptations.
586 static int caif_stream_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
589 struct sock
*sk
= sock
->sk
;
590 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
597 if (unlikely(msg
->msg_flags
&MSG_OOB
))
600 if (unlikely(msg
->msg_namelen
))
603 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
604 timeo
= caif_wait_for_flow_on(cf_sk
, 1, timeo
, &err
);
606 if (unlikely(sk
->sk_shutdown
& SEND_SHUTDOWN
))
613 if (size
> cf_sk
->maxframe
)
614 size
= cf_sk
->maxframe
;
616 /* If size is more than half of sndbuf, chop up message */
617 if (size
> ((sk
->sk_sndbuf
>> 1) - 64))
618 size
= (sk
->sk_sndbuf
>> 1) - 64;
620 if (size
> SKB_MAX_ALLOC
)
621 size
= SKB_MAX_ALLOC
;
623 skb
= sock_alloc_send_skb(sk
,
624 size
+ cf_sk
->headroom
+
626 msg
->msg_flags
&MSG_DONTWAIT
,
631 skb_reserve(skb
, cf_sk
->headroom
);
633 * If you pass two values to the sock_alloc_send_skb
634 * it tries to grab the large buffer with GFP_NOFS
635 * (which can fail easily), and if it fails grab the
636 * fallback size buffer which is under a page and will
639 size
= min_t(int, size
, skb_tailroom(skb
));
641 err
= memcpy_from_msg(skb_put(skb
, size
), msg
, size
);
646 err
= transmit_skb(skb
, cf_sk
,
647 msg
->msg_flags
&MSG_DONTWAIT
, timeo
);
649 /* skb is already freed */
658 if (sent
== 0 && !(msg
->msg_flags
&MSG_NOSIGNAL
))
659 send_sig(SIGPIPE
, current
, 0);
665 static int setsockopt(struct socket
*sock
, int lvl
, int opt
, sockptr_t ov
,
668 struct sock
*sk
= sock
->sk
;
669 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
672 if (cf_sk
->sk
.sk_socket
->state
!= SS_UNCONNECTED
)
676 case CAIFSO_LINK_SELECT
:
677 if (ol
< sizeof(int))
681 if (copy_from_sockptr(&linksel
, ov
, sizeof(int)))
683 lock_sock(&(cf_sk
->sk
));
684 cf_sk
->conn_req
.link_selector
= linksel
;
685 release_sock(&cf_sk
->sk
);
688 case CAIFSO_REQ_PARAM
:
691 if (cf_sk
->sk
.sk_protocol
!= CAIFPROTO_UTIL
)
693 lock_sock(&(cf_sk
->sk
));
694 if (ol
> sizeof(cf_sk
->conn_req
.param
.data
) ||
695 copy_from_sockptr(&cf_sk
->conn_req
.param
.data
, ov
, ol
)) {
696 release_sock(&cf_sk
->sk
);
699 cf_sk
->conn_req
.param
.size
= ol
;
700 release_sock(&cf_sk
->sk
);
714 * caif_connect() - Connect a CAIF Socket
715 * Copied and modified af_irda.c:irda_connect().
717 * Note : by consulting "errno", the user space caller may learn the cause
718 * of the failure. Most of them are visible in the function, others may come
719 * from subroutines called and are listed here :
720 * o -EAFNOSUPPORT: bad socket family or type.
721 * o -ESOCKTNOSUPPORT: bad socket type or protocol
722 * o -EINVAL: bad socket address, or CAIF link type
723 * o -ECONNREFUSED: remote end refused the connection.
724 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
725 * o -EISCONN: already connected.
726 * o -ETIMEDOUT: Connection timed out (send timeout)
727 * o -ENODEV: No link layer to send request
728 * o -ECONNRESET: Received Shutdown indication or lost link layer
729 * o -ENOMEM: Out of memory
732 * o sk_state: holds the CAIF_* protocol state, it's updated by
734 * o sock->state: holds the SS_* socket state and is updated by connect and
737 static int caif_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
738 int addr_len
, int flags
)
740 struct sock
*sk
= sock
->sk
;
741 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
744 int ifindex
, headroom
, tailroom
;
746 struct net_device
*dev
;
751 if (addr_len
< offsetofend(struct sockaddr
, sa_family
))
755 if (uaddr
->sa_family
!= AF_CAIF
)
758 switch (sock
->state
) {
760 /* Normal case, a fresh connect */
761 caif_assert(sk
->sk_state
== CAIF_DISCONNECTED
);
764 switch (sk
->sk_state
) {
766 sock
->state
= SS_CONNECTED
;
769 case CAIF_DISCONNECTED
:
770 /* Reconnect allowed */
772 case CAIF_CONNECTING
:
774 if (flags
& O_NONBLOCK
)
780 caif_assert(sk
->sk_state
== CAIF_CONNECTED
||
781 sk
->sk_state
== CAIF_DISCONNECTED
);
782 if (sk
->sk_shutdown
& SHUTDOWN_MASK
) {
783 /* Allow re-connect after SHUTDOWN_IND */
784 caif_disconnect_client(sock_net(sk
), &cf_sk
->layer
);
785 caif_free_client(&cf_sk
->layer
);
788 /* No reconnect on a seqpacket socket */
791 case SS_DISCONNECTING
:
793 caif_assert(1); /*Should never happen */
796 sk
->sk_state
= CAIF_DISCONNECTED
;
797 sock
->state
= SS_UNCONNECTED
;
798 sk_stream_kill_queues(&cf_sk
->sk
);
801 if (addr_len
!= sizeof(struct sockaddr_caif
))
804 memcpy(&cf_sk
->conn_req
.sockaddr
, uaddr
,
805 sizeof(struct sockaddr_caif
));
807 /* Move to connecting socket, start sending Connect Requests */
808 sock
->state
= SS_CONNECTING
;
809 sk
->sk_state
= CAIF_CONNECTING
;
811 /* Check priority value comming from socket */
812 /* if priority value is out of range it will be ajusted */
813 if (cf_sk
->sk
.sk_priority
> CAIF_PRIO_MAX
)
814 cf_sk
->conn_req
.priority
= CAIF_PRIO_MAX
;
815 else if (cf_sk
->sk
.sk_priority
< CAIF_PRIO_MIN
)
816 cf_sk
->conn_req
.priority
= CAIF_PRIO_MIN
;
818 cf_sk
->conn_req
.priority
= cf_sk
->sk
.sk_priority
;
820 /*ifindex = id of the interface.*/
821 cf_sk
->conn_req
.ifindex
= cf_sk
->sk
.sk_bound_dev_if
;
823 cf_sk
->layer
.receive
= caif_sktrecv_cb
;
825 err
= caif_connect_client(sock_net(sk
), &cf_sk
->conn_req
,
826 &cf_sk
->layer
, &ifindex
, &headroom
, &tailroom
);
829 cf_sk
->sk
.sk_socket
->state
= SS_UNCONNECTED
;
830 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
836 dev
= dev_get_by_index_rcu(sock_net(sk
), ifindex
);
841 cf_sk
->headroom
= LL_RESERVED_SPACE_EXTRA(dev
, headroom
);
845 cf_sk
->tailroom
= tailroom
;
846 cf_sk
->maxframe
= mtu
- (headroom
+ tailroom
);
847 if (cf_sk
->maxframe
< 1) {
848 pr_warn("CAIF Interface MTU too small (%d)\n", dev
->mtu
);
856 if (sk
->sk_state
!= CAIF_CONNECTED
&& (flags
& O_NONBLOCK
))
859 timeo
= sock_sndtimeo(sk
, flags
& O_NONBLOCK
);
863 timeo
= wait_event_interruptible_timeout(*sk_sleep(sk
),
864 sk
->sk_state
!= CAIF_CONNECTING
,
868 goto out
; /* -ERESTARTSYS */
871 if (timeo
== 0 && sk
->sk_state
!= CAIF_CONNECTED
)
873 if (sk
->sk_state
!= CAIF_CONNECTED
) {
874 sock
->state
= SS_UNCONNECTED
;
875 err
= sock_error(sk
);
880 sock
->state
= SS_CONNECTED
;
888 * caif_release() - Disconnect a CAIF Socket
889 * Copied and modified af_irda.c:irda_release().
891 static int caif_release(struct socket
*sock
)
893 struct sock
*sk
= sock
->sk
;
894 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
899 set_tx_flow_off(cf_sk
);
902 * Ensure that packets are not queued after this point in time.
903 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
904 * this ensures no packets when sock is dead.
906 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
907 sock_set_flag(sk
, SOCK_DEAD
);
908 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
911 WARN_ON(IS_ERR(cf_sk
->debugfs_socket_dir
));
912 debugfs_remove_recursive(cf_sk
->debugfs_socket_dir
);
914 lock_sock(&(cf_sk
->sk
));
915 sk
->sk_state
= CAIF_DISCONNECTED
;
916 sk
->sk_shutdown
= SHUTDOWN_MASK
;
918 caif_disconnect_client(sock_net(sk
), &cf_sk
->layer
);
919 cf_sk
->sk
.sk_socket
->state
= SS_DISCONNECTING
;
920 wake_up_interruptible_poll(sk_sleep(sk
), EPOLLERR
|EPOLLHUP
);
923 sk_stream_kill_queues(&cf_sk
->sk
);
929 /* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
930 static __poll_t
caif_poll(struct file
*file
,
931 struct socket
*sock
, poll_table
*wait
)
933 struct sock
*sk
= sock
->sk
;
935 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
937 sock_poll_wait(file
, sock
, wait
);
940 /* exceptional events? */
943 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
945 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
949 if (!skb_queue_empty_lockless(&sk
->sk_receive_queue
) ||
950 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
951 mask
|= EPOLLIN
| EPOLLRDNORM
;
954 * we set writable also when the other side has shut down the
955 * connection. This prevents stuck sockets.
957 if (sock_writeable(sk
) && tx_flow_is_on(cf_sk
))
958 mask
|= EPOLLOUT
| EPOLLWRNORM
| EPOLLWRBAND
;
963 static const struct proto_ops caif_seqpacket_ops
= {
965 .owner
= THIS_MODULE
,
966 .release
= caif_release
,
967 .bind
= sock_no_bind
,
968 .connect
= caif_connect
,
969 .socketpair
= sock_no_socketpair
,
970 .accept
= sock_no_accept
,
971 .getname
= sock_no_getname
,
973 .ioctl
= sock_no_ioctl
,
974 .listen
= sock_no_listen
,
975 .shutdown
= sock_no_shutdown
,
976 .setsockopt
= setsockopt
,
977 .sendmsg
= caif_seqpkt_sendmsg
,
978 .recvmsg
= caif_seqpkt_recvmsg
,
979 .mmap
= sock_no_mmap
,
982 static const struct proto_ops caif_stream_ops
= {
984 .owner
= THIS_MODULE
,
985 .release
= caif_release
,
986 .bind
= sock_no_bind
,
987 .connect
= caif_connect
,
988 .socketpair
= sock_no_socketpair
,
989 .accept
= sock_no_accept
,
990 .getname
= sock_no_getname
,
992 .ioctl
= sock_no_ioctl
,
993 .listen
= sock_no_listen
,
994 .shutdown
= sock_no_shutdown
,
995 .setsockopt
= setsockopt
,
996 .sendmsg
= caif_stream_sendmsg
,
997 .recvmsg
= caif_stream_recvmsg
,
998 .mmap
= sock_no_mmap
,
1001 /* This function is called when a socket is finally destroyed. */
1002 static void caif_sock_destructor(struct sock
*sk
)
1004 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
1005 caif_assert(!refcount_read(&sk
->sk_wmem_alloc
));
1006 caif_assert(sk_unhashed(sk
));
1007 caif_assert(!sk
->sk_socket
);
1008 if (!sock_flag(sk
, SOCK_DEAD
)) {
1009 pr_debug("Attempt to release alive CAIF socket: %p\n", sk
);
1012 sk_stream_kill_queues(&cf_sk
->sk
);
1013 WARN_ON_ONCE(sk
->sk_forward_alloc
);
1014 caif_free_client(&cf_sk
->layer
);
1017 static int caif_create(struct net
*net
, struct socket
*sock
, int protocol
,
1020 struct sock
*sk
= NULL
;
1021 struct caifsock
*cf_sk
= NULL
;
1022 static struct proto prot
= {.name
= "PF_CAIF",
1023 .owner
= THIS_MODULE
,
1024 .obj_size
= sizeof(struct caifsock
),
1025 .useroffset
= offsetof(struct caifsock
, conn_req
.param
),
1026 .usersize
= sizeof_field(struct caifsock
, conn_req
.param
)
1029 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_NET_ADMIN
))
1032 * The sock->type specifies the socket type to use.
1033 * The CAIF socket is a packet stream in the sense
1034 * that it is packet based. CAIF trusts the reliability
1035 * of the link, no resending is implemented.
1037 if (sock
->type
== SOCK_SEQPACKET
)
1038 sock
->ops
= &caif_seqpacket_ops
;
1039 else if (sock
->type
== SOCK_STREAM
)
1040 sock
->ops
= &caif_stream_ops
;
1042 return -ESOCKTNOSUPPORT
;
1044 if (protocol
< 0 || protocol
>= CAIFPROTO_MAX
)
1045 return -EPROTONOSUPPORT
;
1047 * Set the socket state to unconnected. The socket state
1048 * is really not used at all in the net/core or socket.c but the
1049 * initialization makes sure that sock->state is not uninitialized.
1051 sk
= sk_alloc(net
, PF_CAIF
, GFP_KERNEL
, &prot
, kern
);
1055 cf_sk
= container_of(sk
, struct caifsock
, sk
);
1057 /* Store the protocol */
1058 sk
->sk_protocol
= (unsigned char) protocol
;
1060 /* Initialize default priority for well-known cases */
1063 sk
->sk_priority
= TC_PRIO_CONTROL
;
1066 sk
->sk_priority
= TC_PRIO_INTERACTIVE_BULK
;
1069 sk
->sk_priority
= TC_PRIO_BESTEFFORT
;
1073 * Lock in order to try to stop someone from opening the socket
1076 lock_sock(&(cf_sk
->sk
));
1078 /* Initialize the nozero default sock structure data. */
1079 sock_init_data(sock
, sk
);
1080 sk
->sk_destruct
= caif_sock_destructor
;
1082 mutex_init(&cf_sk
->readlock
); /* single task reading lock */
1083 cf_sk
->layer
.ctrlcmd
= caif_ctrl_cb
;
1084 cf_sk
->sk
.sk_socket
->state
= SS_UNCONNECTED
;
1085 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
1087 set_tx_flow_off(cf_sk
);
1088 set_rx_flow_on(cf_sk
);
1090 /* Set default options on configuration */
1091 cf_sk
->conn_req
.link_selector
= CAIF_LINK_LOW_LATENCY
;
1092 cf_sk
->conn_req
.protocol
= protocol
;
1093 release_sock(&cf_sk
->sk
);
1098 static const struct net_proto_family caif_family_ops
= {
1100 .create
= caif_create
,
1101 .owner
= THIS_MODULE
,
1104 static int __init
caif_sktinit_module(void)
1106 return sock_register(&caif_family_ops
);
1109 static void __exit
caif_sktexit_module(void)
1111 sock_unregister(PF_CAIF
);
1113 module_init(caif_sktinit_module
);
1114 module_exit(caif_sktexit_module
);