18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * SUCS NET3: 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Generic stream handling routines. These are generic for most 68c2ecf20Sopenharmony_ci * protocols. Even IP. Tonight 8-). 78c2ecf20Sopenharmony_ci * This is used because TCP, LLC (others too) layer all have mostly 88c2ecf20Sopenharmony_ci * identical sendmsg() and recvmsg() code. 98c2ecf20Sopenharmony_ci * So we (will) share it here. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br> 128c2ecf20Sopenharmony_ci * (from old tcp.c code) 138c2ecf20Sopenharmony_ci * Alan Cox <alan@lxorguk.ukuu.org.uk> (Borrowed comments 8-)) 148c2ecf20Sopenharmony_ci */ 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci#include <linux/sched/signal.h> 188c2ecf20Sopenharmony_ci#include <linux/net.h> 198c2ecf20Sopenharmony_ci#include <linux/signal.h> 208c2ecf20Sopenharmony_ci#include <linux/tcp.h> 218c2ecf20Sopenharmony_ci#include <linux/wait.h> 228c2ecf20Sopenharmony_ci#include <net/sock.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci/** 258c2ecf20Sopenharmony_ci * sk_stream_write_space - stream socket write_space callback. 268c2ecf20Sopenharmony_ci * @sk: socket 278c2ecf20Sopenharmony_ci * 288c2ecf20Sopenharmony_ci * FIXME: write proper description 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_civoid sk_stream_write_space(struct sock *sk) 318c2ecf20Sopenharmony_ci{ 328c2ecf20Sopenharmony_ci struct socket *sock = sk->sk_socket; 338c2ecf20Sopenharmony_ci struct socket_wq *wq; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci if (__sk_stream_is_writeable(sk, 1) && sock) { 368c2ecf20Sopenharmony_ci clear_bit(SOCK_NOSPACE, &sock->flags); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci rcu_read_lock(); 398c2ecf20Sopenharmony_ci wq = rcu_dereference(sk->sk_wq); 408c2ecf20Sopenharmony_ci if (skwq_has_sleeper(wq)) 418c2ecf20Sopenharmony_ci wake_up_interruptible_poll(&wq->wait, EPOLLOUT | 428c2ecf20Sopenharmony_ci EPOLLWRNORM | EPOLLWRBAND); 438c2ecf20Sopenharmony_ci if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN)) 448c2ecf20Sopenharmony_ci sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT); 458c2ecf20Sopenharmony_ci rcu_read_unlock(); 468c2ecf20Sopenharmony_ci } 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci/** 508c2ecf20Sopenharmony_ci * sk_stream_wait_connect - Wait for a socket to get into the connected state 518c2ecf20Sopenharmony_ci * @sk: sock to wait on 528c2ecf20Sopenharmony_ci * @timeo_p: for how long to wait 538c2ecf20Sopenharmony_ci * 548c2ecf20Sopenharmony_ci * Must be called with the socket locked. 558c2ecf20Sopenharmony_ci */ 568c2ecf20Sopenharmony_ciint sk_stream_wait_connect(struct sock *sk, long *timeo_p) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci DEFINE_WAIT_FUNC(wait, woken_wake_function); 598c2ecf20Sopenharmony_ci struct task_struct *tsk = current; 608c2ecf20Sopenharmony_ci int done; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci do { 638c2ecf20Sopenharmony_ci int err = sock_error(sk); 648c2ecf20Sopenharmony_ci if (err) 658c2ecf20Sopenharmony_ci return err; 668c2ecf20Sopenharmony_ci if ((1 << sk->sk_state) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV)) 678c2ecf20Sopenharmony_ci return -EPIPE; 688c2ecf20Sopenharmony_ci if (!*timeo_p) 698c2ecf20Sopenharmony_ci return -EAGAIN; 708c2ecf20Sopenharmony_ci if (signal_pending(tsk)) 718c2ecf20Sopenharmony_ci return sock_intr_errno(*timeo_p); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci add_wait_queue(sk_sleep(sk), &wait); 748c2ecf20Sopenharmony_ci sk->sk_write_pending++; 758c2ecf20Sopenharmony_ci done = sk_wait_event(sk, timeo_p, 768c2ecf20Sopenharmony_ci !READ_ONCE(sk->sk_err) && 778c2ecf20Sopenharmony_ci !((1 << READ_ONCE(sk->sk_state)) & 788c2ecf20Sopenharmony_ci ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)), &wait); 798c2ecf20Sopenharmony_ci remove_wait_queue(sk_sleep(sk), &wait); 808c2ecf20Sopenharmony_ci sk->sk_write_pending--; 818c2ecf20Sopenharmony_ci } while (!done); 828c2ecf20Sopenharmony_ci return 0; 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sk_stream_wait_connect); 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci/** 878c2ecf20Sopenharmony_ci * sk_stream_closing - Return 1 if we still have things to send in our buffers. 888c2ecf20Sopenharmony_ci * @sk: socket to verify 898c2ecf20Sopenharmony_ci */ 908c2ecf20Sopenharmony_cistatic int sk_stream_closing(const struct sock *sk) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci return (1 << READ_ONCE(sk->sk_state)) & 938c2ecf20Sopenharmony_ci (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK); 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_civoid sk_stream_wait_close(struct sock *sk, long timeout) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci if (timeout) { 998c2ecf20Sopenharmony_ci DEFINE_WAIT_FUNC(wait, woken_wake_function); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci add_wait_queue(sk_sleep(sk), &wait); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci do { 1048c2ecf20Sopenharmony_ci if (sk_wait_event(sk, &timeout, !sk_stream_closing(sk), &wait)) 1058c2ecf20Sopenharmony_ci break; 1068c2ecf20Sopenharmony_ci } while (!signal_pending(current) && timeout); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci remove_wait_queue(sk_sleep(sk), &wait); 1098c2ecf20Sopenharmony_ci } 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sk_stream_wait_close); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci/** 1148c2ecf20Sopenharmony_ci * sk_stream_wait_memory - Wait for more memory for a socket 1158c2ecf20Sopenharmony_ci * @sk: socket to wait for memory 1168c2ecf20Sopenharmony_ci * @timeo_p: for how long 1178c2ecf20Sopenharmony_ci */ 1188c2ecf20Sopenharmony_ciint sk_stream_wait_memory(struct sock *sk, long *timeo_p) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci int err = 0; 1218c2ecf20Sopenharmony_ci long vm_wait = 0; 1228c2ecf20Sopenharmony_ci long current_timeo = *timeo_p; 1238c2ecf20Sopenharmony_ci DEFINE_WAIT_FUNC(wait, woken_wake_function); 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci if (sk_stream_memory_free(sk)) 1268c2ecf20Sopenharmony_ci current_timeo = vm_wait = (prandom_u32() % (HZ / 5)) + 2; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci add_wait_queue(sk_sleep(sk), &wait); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci while (1) { 1318c2ecf20Sopenharmony_ci sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) 1348c2ecf20Sopenharmony_ci goto do_error; 1358c2ecf20Sopenharmony_ci if (!*timeo_p) 1368c2ecf20Sopenharmony_ci goto do_eagain; 1378c2ecf20Sopenharmony_ci if (signal_pending(current)) 1388c2ecf20Sopenharmony_ci goto do_interrupted; 1398c2ecf20Sopenharmony_ci sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); 1408c2ecf20Sopenharmony_ci if (sk_stream_memory_free(sk) && !vm_wait) 1418c2ecf20Sopenharmony_ci break; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1448c2ecf20Sopenharmony_ci sk->sk_write_pending++; 1458c2ecf20Sopenharmony_ci sk_wait_event(sk, ¤t_timeo, READ_ONCE(sk->sk_err) || 1468c2ecf20Sopenharmony_ci (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN) || 1478c2ecf20Sopenharmony_ci (sk_stream_memory_free(sk) && 1488c2ecf20Sopenharmony_ci !vm_wait), &wait); 1498c2ecf20Sopenharmony_ci sk->sk_write_pending--; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci if (vm_wait) { 1528c2ecf20Sopenharmony_ci vm_wait -= current_timeo; 1538c2ecf20Sopenharmony_ci current_timeo = *timeo_p; 1548c2ecf20Sopenharmony_ci if (current_timeo != MAX_SCHEDULE_TIMEOUT && 1558c2ecf20Sopenharmony_ci (current_timeo -= vm_wait) < 0) 1568c2ecf20Sopenharmony_ci current_timeo = 0; 1578c2ecf20Sopenharmony_ci vm_wait = 0; 1588c2ecf20Sopenharmony_ci } 1598c2ecf20Sopenharmony_ci *timeo_p = current_timeo; 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ciout: 1628c2ecf20Sopenharmony_ci if (!sock_flag(sk, SOCK_DEAD)) 1638c2ecf20Sopenharmony_ci remove_wait_queue(sk_sleep(sk), &wait); 1648c2ecf20Sopenharmony_ci return err; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_cido_error: 1678c2ecf20Sopenharmony_ci err = -EPIPE; 1688c2ecf20Sopenharmony_ci goto out; 1698c2ecf20Sopenharmony_cido_eagain: 1708c2ecf20Sopenharmony_ci /* Make sure that whenever EAGAIN is returned, EPOLLOUT event can 1718c2ecf20Sopenharmony_ci * be generated later. 1728c2ecf20Sopenharmony_ci * When TCP receives ACK packets that make room, tcp_check_space() 1738c2ecf20Sopenharmony_ci * only calls tcp_new_space() if SOCK_NOSPACE is set. 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_ci set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1768c2ecf20Sopenharmony_ci err = -EAGAIN; 1778c2ecf20Sopenharmony_ci goto out; 1788c2ecf20Sopenharmony_cido_interrupted: 1798c2ecf20Sopenharmony_ci err = sock_intr_errno(*timeo_p); 1808c2ecf20Sopenharmony_ci goto out; 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sk_stream_wait_memory); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ciint sk_stream_error(struct sock *sk, int flags, int err) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci if (err == -EPIPE) 1878c2ecf20Sopenharmony_ci err = sock_error(sk) ? : -EPIPE; 1888c2ecf20Sopenharmony_ci if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) 1898c2ecf20Sopenharmony_ci send_sig(SIGPIPE, current, 0); 1908c2ecf20Sopenharmony_ci return err; 1918c2ecf20Sopenharmony_ci} 1928c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sk_stream_error); 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_civoid sk_stream_kill_queues(struct sock *sk) 1958c2ecf20Sopenharmony_ci{ 1968c2ecf20Sopenharmony_ci /* First the read buffer. */ 1978c2ecf20Sopenharmony_ci __skb_queue_purge(&sk->sk_receive_queue); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci /* Next, the error queue. 2008c2ecf20Sopenharmony_ci * We need to use queue lock, because other threads might 2018c2ecf20Sopenharmony_ci * add packets to the queue without socket lock being held. 2028c2ecf20Sopenharmony_ci */ 2038c2ecf20Sopenharmony_ci skb_queue_purge(&sk->sk_error_queue); 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci /* Next, the write queue. */ 2068c2ecf20Sopenharmony_ci WARN_ON(!skb_queue_empty(&sk->sk_write_queue)); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci /* Account for returned memory. */ 2098c2ecf20Sopenharmony_ci sk_mem_reclaim(sk); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci WARN_ON(sk->sk_wmem_queued); 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci /* It is _impossible_ for the backlog to contain anything 2148c2ecf20Sopenharmony_ci * when we get here. All user references to this socket 2158c2ecf20Sopenharmony_ci * have gone away, only the net layer knows can touch it. 2168c2ecf20Sopenharmony_ci */ 2178c2ecf20Sopenharmony_ci} 2188c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sk_stream_kill_queues); 219