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 */ 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 */ 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 */ 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 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 116 /** 117 * sk_stream_wait_memory - Wait for more memory for a socket 118 * @sk: socket to wait for memory 119 * @timeo_p: for how long 120 */ 121 int sk_stream_wait_memory(struct sock *sk, long *timeo_p) 122 { 123 int ret, err = 0; 124 long vm_wait = 0; 125 long current_timeo = *timeo_p; 126 DEFINE_WAIT_FUNC(wait, woken_wake_function); 127 128 if (sk_stream_memory_free(sk)) 129 current_timeo = vm_wait = get_random_u32_below(HZ / 5) + 2; 130 131 add_wait_queue(sk_sleep(sk), &wait); 132 133 while (1) { 134 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 135 136 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) 137 goto do_error; 138 if (!*timeo_p) 139 goto do_eagain; 140 if (signal_pending(current)) 141 goto do_interrupted; 142 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); 143 if (sk_stream_memory_free(sk) && !vm_wait) 144 break; 145 146 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 147 sk->sk_write_pending++; 148 ret = sk_wait_event(sk, ¤t_timeo, READ_ONCE(sk->sk_err) || 149 (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN) || 150 (sk_stream_memory_free(sk) && !vm_wait), 151 &wait); 152 sk->sk_write_pending--; 153 if (ret < 0) 154 goto do_error; 155 156 if (vm_wait) { 157 vm_wait -= current_timeo; 158 current_timeo = *timeo_p; 159 if (current_timeo != MAX_SCHEDULE_TIMEOUT && 160 (current_timeo -= vm_wait) < 0) 161 current_timeo = 0; 162 vm_wait = 0; 163 } 164 *timeo_p = current_timeo; 165 } 166 out: 167 if (!sock_flag(sk, SOCK_DEAD)) 168 remove_wait_queue(sk_sleep(sk), &wait); 169 return err; 170 171 do_error: 172 err = -EPIPE; 173 goto out; 174 do_eagain: 175 /* Make sure that whenever EAGAIN is returned, EPOLLOUT event can 176 * be generated later. 177 * When TCP receives ACK packets that make room, tcp_check_space() 178 * only calls tcp_new_space() if SOCK_NOSPACE is set. 179 */ 180 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 181 err = -EAGAIN; 182 goto out; 183 do_interrupted: 184 err = sock_intr_errno(*timeo_p); 185 goto out; 186 } 187 EXPORT_SYMBOL(sk_stream_wait_memory); 188 189 int sk_stream_error(struct sock *sk, int flags, int err) 190 { 191 if (err == -EPIPE) 192 err = sock_error(sk) ? : -EPIPE; 193 if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) 194 send_sig(SIGPIPE, current, 0); 195 return err; 196 } 197 EXPORT_SYMBOL(sk_stream_error); 198 199 void sk_stream_kill_queues(struct sock *sk) 200 { 201 /* First the read buffer. */ 202 __skb_queue_purge(&sk->sk_receive_queue); 203 204 /* Next, the error queue. 205 * We need to use queue lock, because other threads might 206 * add packets to the queue without socket lock being held. 207 */ 208 skb_queue_purge(&sk->sk_error_queue); 209 210 /* Next, the write queue. */ 211 WARN_ON_ONCE(!skb_queue_empty(&sk->sk_write_queue)); 212 213 /* Account for returned memory. */ 214 sk_mem_reclaim_final(sk); 215 216 WARN_ON_ONCE(sk->sk_wmem_queued); 217 218 /* It is _impossible_ for the backlog to contain anything 219 * when we get here. All user references to this socket 220 * have gone away, only the net layer knows can touch it. 221 */ 222 } 223 EXPORT_SYMBOL(sk_stream_kill_queues); 224