xref: /linux/net/core/stream.c (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *     SUCS NET3:
4  *
5  *     Generic stream handling routines. These are generic for most
6  *     protocols. Even IP. Tonight 8-).
7  *     This is used because TCP, LLC (others too) layer all have mostly
8  *     identical sendmsg() and recvmsg() code.
9  *     So we (will) share it here.
10  *
11  *     Authors:        Arnaldo Carvalho de Melo <acme@conectiva.com.br>
12  *                     (from old tcp.c code)
13  *                     Alan Cox <alan@lxorguk.ukuu.org.uk> (Borrowed comments 8-))
14  */
15 
16 #include <linux/module.h>
17 #include <linux/sched/signal.h>
18 #include <linux/net.h>
19 #include <linux/signal.h>
20 #include <linux/tcp.h>
21 #include <linux/wait.h>
22 #include <net/sock.h>
23 
24 /**
25  * sk_stream_write_space - stream socket write_space callback.
26  * @sk: pointer to the socket structure
27  *
28  * This function is invoked when there's space available in the socket's
29  * send buffer for writing. It first checks if the socket is writable,
30  * clears the SOCK_NOSPACE flag indicating that memory for writing
31  * is now available, wakes up any processes waiting for write operations
32  * and sends asynchronous notifications if needed.
33  */
sk_stream_write_space(struct sock * sk)34 void sk_stream_write_space(struct sock *sk)
35 {
36 	struct socket *sock = sk->sk_socket;
37 	struct socket_wq *wq;
38 
39 	if (__sk_stream_is_writeable(sk, 1) && sock) {
40 		clear_bit(SOCK_NOSPACE, &sock->flags);
41 
42 		rcu_read_lock();
43 		wq = rcu_dereference(sk->sk_wq);
44 		if (skwq_has_sleeper(wq))
45 			wake_up_interruptible_poll(&wq->wait, EPOLLOUT |
46 						EPOLLWRNORM | EPOLLWRBAND);
47 		if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN))
48 			sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
49 		rcu_read_unlock();
50 	}
51 }
52 
53 /**
54  * sk_stream_wait_connect - Wait for a socket to get into the connected state
55  * @sk: sock to wait on
56  * @timeo_p: for how long to wait
57  *
58  * Must be called with the socket locked.
59  */
sk_stream_wait_connect(struct sock * sk,long * timeo_p)60 int sk_stream_wait_connect(struct sock *sk, long *timeo_p)
61 {
62 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
63 	struct task_struct *tsk = current;
64 	int done;
65 
66 	do {
67 		int err = sock_error(sk);
68 		if (err)
69 			return err;
70 		if ((1 << sk->sk_state) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV))
71 			return -EPIPE;
72 		if (!*timeo_p)
73 			return -EAGAIN;
74 		if (signal_pending(tsk))
75 			return sock_intr_errno(*timeo_p);
76 
77 		add_wait_queue(sk_sleep(sk), &wait);
78 		sk->sk_write_pending++;
79 		done = sk_wait_event(sk, timeo_p,
80 				     !READ_ONCE(sk->sk_err) &&
81 				     !((1 << READ_ONCE(sk->sk_state)) &
82 				       ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)), &wait);
83 		remove_wait_queue(sk_sleep(sk), &wait);
84 		sk->sk_write_pending--;
85 	} while (!done);
86 	return done < 0 ? done : 0;
87 }
88 EXPORT_SYMBOL(sk_stream_wait_connect);
89 
90 /**
91  * sk_stream_closing - Return 1 if we still have things to send in our buffers.
92  * @sk: socket to verify
93  */
sk_stream_closing(const struct sock * sk)94 static int sk_stream_closing(const struct sock *sk)
95 {
96 	return (1 << READ_ONCE(sk->sk_state)) &
97 	       (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK);
98 }
99 
sk_stream_wait_close(struct sock * sk,long timeout)100 void sk_stream_wait_close(struct sock *sk, long timeout)
101 {
102 	if (timeout) {
103 		DEFINE_WAIT_FUNC(wait, woken_wake_function);
104 
105 		add_wait_queue(sk_sleep(sk), &wait);
106 
107 		do {
108 			if (sk_wait_event(sk, &timeout, !sk_stream_closing(sk), &wait))
109 				break;
110 		} while (!signal_pending(current) && timeout);
111 
112 		remove_wait_queue(sk_sleep(sk), &wait);
113 	}
114 }
115 EXPORT_SYMBOL(sk_stream_wait_close);
116 
117 /**
118  * sk_stream_wait_memory - Wait for more memory for a socket
119  * @sk: socket to wait for memory
120  * @timeo_p: for how long
121  */
sk_stream_wait_memory(struct sock * sk,long * timeo_p)122 int sk_stream_wait_memory(struct sock *sk, long *timeo_p)
123 {
124 	int ret, err = 0;
125 	long vm_wait = 0;
126 	long current_timeo = *timeo_p;
127 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
128 
129 	if (sk_stream_memory_free(sk))
130 		current_timeo = vm_wait = get_random_u32_below(HZ / 5) + 2;
131 
132 	add_wait_queue(sk_sleep(sk), &wait);
133 
134 	while (1) {
135 		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
136 
137 		if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
138 			goto do_error;
139 		if (!*timeo_p)
140 			goto do_eagain;
141 		if (signal_pending(current))
142 			goto do_interrupted;
143 		sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
144 		if (sk_stream_memory_free(sk) && !vm_wait)
145 			break;
146 
147 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
148 		sk->sk_write_pending++;
149 		ret = sk_wait_event(sk, &current_timeo, READ_ONCE(sk->sk_err) ||
150 				    (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN) ||
151 				    (sk_stream_memory_free(sk) && !vm_wait),
152 				    &wait);
153 		sk->sk_write_pending--;
154 		if (ret < 0)
155 			goto do_error;
156 
157 		if (vm_wait) {
158 			vm_wait -= current_timeo;
159 			current_timeo = *timeo_p;
160 			if (current_timeo != MAX_SCHEDULE_TIMEOUT &&
161 			    (current_timeo -= vm_wait) < 0)
162 				current_timeo = 0;
163 			vm_wait = 0;
164 		}
165 		*timeo_p = current_timeo;
166 	}
167 out:
168 	if (!sock_flag(sk, SOCK_DEAD))
169 		remove_wait_queue(sk_sleep(sk), &wait);
170 	return err;
171 
172 do_error:
173 	err = -EPIPE;
174 	goto out;
175 do_eagain:
176 	/* Make sure that whenever EAGAIN is returned, EPOLLOUT event can
177 	 * be generated later.
178 	 * When TCP receives ACK packets that make room, tcp_check_space()
179 	 * only calls tcp_new_space() if SOCK_NOSPACE is set.
180 	 */
181 	set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
182 	err = -EAGAIN;
183 	goto out;
184 do_interrupted:
185 	err = sock_intr_errno(*timeo_p);
186 	goto out;
187 }
188 EXPORT_SYMBOL(sk_stream_wait_memory);
189 
sk_stream_error(struct sock * sk,int flags,int err)190 int sk_stream_error(struct sock *sk, int flags, int err)
191 {
192 	if (err == -EPIPE)
193 		err = sock_error(sk) ? : -EPIPE;
194 	if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
195 		send_sig(SIGPIPE, current, 0);
196 	return err;
197 }
198 EXPORT_SYMBOL(sk_stream_error);
199 
sk_stream_kill_queues(struct sock * sk)200 void sk_stream_kill_queues(struct sock *sk)
201 {
202 	/* First the read buffer. */
203 	__skb_queue_purge(&sk->sk_receive_queue);
204 
205 	/* Next, the error queue.
206 	 * We need to use queue lock, because other threads might
207 	 * add packets to the queue without socket lock being held.
208 	 */
209 	skb_queue_purge(&sk->sk_error_queue);
210 
211 	/* Next, the write queue. */
212 	WARN_ON_ONCE(!skb_queue_empty(&sk->sk_write_queue));
213 
214 	/* Account for returned memory. */
215 	sk_mem_reclaim_final(sk);
216 
217 	WARN_ON_ONCE(sk->sk_wmem_queued);
218 
219 	/* It is _impossible_ for the backlog to contain anything
220 	 * when we get here.  All user references to this socket
221 	 * have gone away, only the net layer knows can touch it.
222 	 */
223 }
224 EXPORT_SYMBOL(sk_stream_kill_queues);
225