^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * INET An implementation of the TCP/IP protocol suite for the LINUX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * operating system. INET is implemented using the BSD Socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * interface as the means of communication with the user level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Implementation of the Transmission Control Protocol(TCP).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Authors: Ross Biro
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Mark Evans, <evansmp@uhura.aston.ac.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Corey Minyard <wf-rch!minyard@relay.EU.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Florian La Roche, <flla@stud.uni-sb.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Linus Torvalds, <torvalds@cs.helsinki.fi>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Alan Cox, <gw4pts@gw4pts.ampr.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Matthew Dillon, <dillon@apollo.west.oic.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Jorge Cwik, <jorge@laser.satlink.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static u32 tcp_clamp_rto_to_user_timeout(const struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u32 elapsed, start_ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) s32 remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) start_ts = tcp_sk(sk)->retrans_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (!icsk->icsk_user_timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return icsk->icsk_rto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) elapsed = tcp_time_stamp(tcp_sk(sk)) - start_ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) remaining = icsk->icsk_user_timeout - elapsed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (remaining <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return 1; /* user timeout has passed; fire ASAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return min_t(u32, icsk->icsk_rto, msecs_to_jiffies(remaining));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u32 tcp_clamp_probe0_to_user_timeout(const struct sock *sk, u32 when)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) u32 remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) s32 elapsed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (!icsk->icsk_user_timeout || !icsk->icsk_probes_tstamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return when;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) elapsed = tcp_jiffies32 - icsk->icsk_probes_tstamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (unlikely(elapsed < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) elapsed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) remaining = msecs_to_jiffies(icsk->icsk_user_timeout) - elapsed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) remaining = max_t(u32, remaining, TCP_TIMEOUT_MIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return min_t(u32, remaining, when);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * tcp_write_err() - close socket and save error info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @sk: The socket the error has appeared on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Returns: Nothing (void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static void tcp_write_err(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) sk->sk_error_report(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) tcp_write_queue_purge(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) tcp_done(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * tcp_out_of_resources() - Close socket if out of resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @sk: pointer to current socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * @do_reset: send a last packet with reset flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Do not allow orphaned sockets to eat all our resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * This is direct violation of TCP specs, but it is required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * to prevent DoS attacks. It is called when a retransmission timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * or zero probe timeout occurs on orphaned socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * Also close if our net namespace is exiting; in that case there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * hope of ever communicating again since all netns interfaces are already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * down (or about to be down), and we need to release our dst references,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * which have been moved to the netns loopback interface, so the namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * can finish exiting. This condition is only possible if we are a kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * socket, as those do not hold references to the namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * Criteria is still not confirmed experimentally and may change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * We kill the socket, if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * 1. If number of orphaned sockets exceeds an administratively configured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * 2. If we have strong memory pressure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * 3. If our net namespace is exiting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int tcp_out_of_resources(struct sock *sk, bool do_reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int shift = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* If peer does not open window for long time, or did not transmit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * anything for long time, penalize it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if ((s32)(tcp_jiffies32 - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) shift++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* If some dubious ICMP arrived, penalize even more. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (sk->sk_err_soft)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) shift++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (tcp_check_oom(sk, shift)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* Catch exceptional cases, when connection requires reset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * 1. Last segment was sent recently. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if ((s32)(tcp_jiffies32 - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* 2. Window is closed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) (!tp->snd_wnd && !tp->packets_out))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) do_reset = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (do_reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) tcp_send_active_reset(sk, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) tcp_done(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (!check_net(sock_net(sk))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* Not possible to send reset; just close */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) tcp_done(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * tcp_orphan_retries() - Returns maximal number of retries on an orphaned socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * @sk: Pointer to the current socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * @alive: bool, socket alive state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int tcp_orphan_retries(struct sock *sk, bool alive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int retries = sock_net(sk)->ipv4.sysctl_tcp_orphan_retries; /* May be zero. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* We know from an ICMP that something is wrong. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (sk->sk_err_soft && !alive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* However, if socket sent something recently, select some safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * number of retries. 8 corresponds to >100 seconds with minimal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * RTO of 200msec. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (retries == 0 && alive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) retries = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return retries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) const struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int mss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /* Black hole detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (!net->ipv4.sysctl_tcp_mtu_probing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!icsk->icsk_mtup.enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) icsk->icsk_mtup.enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) icsk->icsk_mtup.probe_timestamp = tcp_jiffies32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) mss = min(net->ipv4.sysctl_tcp_base_mss, mss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) mss = max(mss, net->ipv4.sysctl_tcp_mtu_probe_floor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) mss = max(mss, net->ipv4.sysctl_tcp_min_snd_mss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static unsigned int tcp_model_timeout(struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) unsigned int boundary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) unsigned int rto_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) unsigned int linear_backoff_thresh, timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) linear_backoff_thresh = ilog2(TCP_RTO_MAX / rto_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (boundary <= linear_backoff_thresh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) timeout = ((2 << boundary) - 1) * rto_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) timeout = ((2 << linear_backoff_thresh) - 1) * rto_base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) (boundary - linear_backoff_thresh) * TCP_RTO_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return jiffies_to_msecs(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * retransmits_timed_out() - returns true if this connection has timed out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @sk: The current socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * @boundary: max number of retransmissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * @timeout: A custom timeout value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * If set to 0 the default timeout is calculated and used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * Using TCP_RTO_MIN and the number of unsuccessful retransmits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * The default "timeout" value this function can calculate and use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * is equivalent to the timeout of a TCP Connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * after "boundary" unsuccessful, exponentially backed-off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * retransmissions with an initial RTO of TCP_RTO_MIN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static bool retransmits_timed_out(struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) unsigned int boundary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) unsigned int start_ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (!inet_csk(sk)->icsk_retransmits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) start_ts = tcp_sk(sk)->retrans_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (likely(timeout == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) unsigned int rto_base = TCP_RTO_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) rto_base = tcp_timeout_init(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) timeout = tcp_model_timeout(sk, boundary, rto_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return (s32)(tcp_time_stamp(tcp_sk(sk)) - start_ts - timeout) >= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* A write timeout has occurred. Process the after effects. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static int tcp_write_timeout(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) bool expired = false, do_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int retry_until;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (icsk->icsk_retransmits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) __dst_negative_advice(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) retry_until = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) expired = icsk->icsk_retransmits >= retry_until;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /* Black hole detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) tcp_mtu_probing(icsk, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) __dst_negative_advice(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) retry_until = net->ipv4.sysctl_tcp_retries2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (sock_flag(sk, SOCK_DEAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) const bool alive = icsk->icsk_rto < TCP_RTO_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) retry_until = tcp_orphan_retries(sk, alive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) do_reset = alive ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) !retransmits_timed_out(sk, retry_until, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (tcp_out_of_resources(sk, do_reset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (!expired)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) expired = retransmits_timed_out(sk, retry_until,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) icsk->icsk_user_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) tcp_fastopen_active_detect_blackhole(sk, expired);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RTO_CB_FLAG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) tcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RTO_CB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) icsk->icsk_retransmits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) icsk->icsk_rto, (int)expired);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (expired) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /* Has it gone just too far? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) tcp_write_err(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (sk_rethink_txhash(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) tp->timeout_rehash++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPTIMEOUTREHASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /* Called with BH disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) void tcp_delack_timer_handler(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) sk_mem_reclaim_partial(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (time_after(icsk->icsk_ack.timeout, jiffies)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (inet_csk_ack_scheduled(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (!inet_csk_in_pingpong_mode(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /* Delayed ACK missed: inflate ATO. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /* Delayed ACK missed: leave pingpong mode and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * deflate ATO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) inet_csk_exit_pingpong_mode(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) icsk->icsk_ack.ato = TCP_ATO_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) tcp_mstamp_refresh(tcp_sk(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) tcp_send_ack(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (tcp_under_memory_pressure(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) sk_mem_reclaim(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * tcp_delack_timer() - The TCP delayed ACK timeout handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * @t: Pointer to the timer. (gets casted to struct sock *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * This function gets (indirectly) called when the kernel timer for a TCP packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * of this socket expires. Calls tcp_delack_timer_handler() to do the actual work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * Returns: Nothing (void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static void tcp_delack_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct inet_connection_sock *icsk =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) from_timer(icsk, t, icsk_delack_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct sock *sk = &icsk->icsk_inet.sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (!sock_owned_by_user(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) tcp_delack_timer_handler(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /* deleguate our work to tcp_release_cb() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &sk->sk_tsq_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static void tcp_probe_timer(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct sk_buff *skb = tcp_send_head(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) int max_probes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (tp->packets_out || !skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) icsk->icsk_probes_out = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) icsk->icsk_probes_tstamp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) /* RFC 1122 4.2.2.17 requires the sender to stay open indefinitely as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * long as the receiver continues to respond probes. We support this by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * default and reset icsk_probes_out with incoming ACKs. But if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * socket is orphaned or the user specifies TCP_USER_TIMEOUT, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * kill the socket when the retry count and the time exceeds the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * corresponding system limit. We also implement similar policy when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * we use RTO to probe window in tcp_retransmit_timer().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (!icsk->icsk_probes_tstamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) icsk->icsk_probes_tstamp = tcp_jiffies32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) else if (icsk->icsk_user_timeout &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) (s32)(tcp_jiffies32 - icsk->icsk_probes_tstamp) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) msecs_to_jiffies(icsk->icsk_user_timeout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) max_probes = sock_net(sk)->ipv4.sysctl_tcp_retries2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (sock_flag(sk, SOCK_DEAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) max_probes = tcp_orphan_retries(sk, alive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (!alive && icsk->icsk_backoff >= max_probes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (tcp_out_of_resources(sk, true))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (icsk->icsk_probes_out >= max_probes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) abort: tcp_write_err(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /* Only send another probe if we didn't close things up. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) tcp_send_probe0(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * Timer for Fast Open socket to retransmit SYNACK. Note that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * sk here is the child socket, not the parent (listener) socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static void tcp_fastopen_synack_timer(struct sock *sk, struct request_sock *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) int max_retries = icsk->icsk_syn_retries ? :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) sock_net(sk)->ipv4.sysctl_tcp_synack_retries + 1; /* add one more retry for fastopen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) req->rsk_ops->syn_ack_timeout(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (req->num_timeout >= max_retries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) tcp_write_err(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) /* Lower cwnd after certain SYNACK timeout like tcp_init_transfer() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (icsk->icsk_retransmits == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) tcp_enter_loss(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) /* XXX (TFO) - Unlike regular SYN-ACK retransmit, we ignore error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * returned from rtx_syn_ack() to make it more persistent like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * regular retransmit because if the child socket has been accepted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * it's not good to give up too easily.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) inet_rtx_syn_ack(sk, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) req->num_timeout++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) icsk->icsk_retransmits++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (!tp->retrans_stamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) tp->retrans_stamp = tcp_time_stamp(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * tcp_retransmit_timer() - The TCP retransmit timeout handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * @sk: Pointer to the current socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * This function gets called when the kernel timer for a TCP packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * of this socket expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * It handles retransmission, timer adjustment and other necesarry measures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * Returns: Nothing (void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) void tcp_retransmit_timer(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct request_sock *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) req = rcu_dereference_protected(tp->fastopen_rsk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) lockdep_sock_is_held(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) sk->sk_state != TCP_FIN_WAIT1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) tcp_fastopen_synack_timer(sk, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /* Before we receive ACK to our SYN-ACK don't retransmit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * anything else (e.g., data or FIN segments).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (!tp->packets_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) skb = tcp_rtx_queue_head(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (WARN_ON_ONCE(!skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) tp->tlp_high_seq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /* Receiver dastardly shrinks window. Our retransmits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * become zero probes, but we should not timeout this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * connection. If the socket is an orphan, time it out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * we cannot allow such beasts to hang infinitely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (sk->sk_family == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) &inet->inet_daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) ntohs(inet->inet_dport),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) inet->inet_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) tp->snd_una, tp->snd_nxt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) else if (sk->sk_family == AF_INET6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) &sk->sk_v6_daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) ntohs(inet->inet_dport),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) inet->inet_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) tp->snd_una, tp->snd_nxt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) tcp_write_err(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) tcp_enter_loss(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) tcp_retransmit_skb(sk, skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) __sk_dst_reset(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) goto out_reset_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPTIMEOUTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (tcp_write_timeout(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (icsk->icsk_retransmits == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) int mib_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (icsk->icsk_ca_state == TCP_CA_Recovery) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (tcp_is_sack(tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) mib_idx = LINUX_MIB_TCPLOSSFAILURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) tp->sacked_out) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (tcp_is_sack(tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) mib_idx = LINUX_MIB_TCPSACKFAILURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) mib_idx = LINUX_MIB_TCPRENOFAILURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (mib_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) __NET_INC_STATS(sock_net(sk), mib_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) tcp_enter_loss(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) icsk->icsk_retransmits++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /* Retransmission failed because of local congestion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * Let senders fight for local resources conservatively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) TCP_RESOURCE_PROBE_INTERVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) TCP_RTO_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /* Increase the timeout each time we retransmit. Note that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * we do not increase the rtt estimate. rto is initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * that doubling rto each time is the least we can get away with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * In KA9Q, Karn uses this for the first few times, and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * goes to quadratic. netBSD doubles, but only goes up to *64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * and clamps at 1 to 64 sec afterwards. Note that 120 sec is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * defined in the protocol as the maximum possible RTT. I guess
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * we'll have to use something other than TCP to talk to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * University of Mars.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * PAWS allows us longer timeouts and large windows, so once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * implemented ftp to mars will work nicely. We will have to fix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * the 120 second clamps though!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) icsk->icsk_backoff++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) out_reset_timer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * used to reset timer, set to 0. Recalculate 'icsk_rto' as this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * might be increased if the stream oscillates between thin and thick,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * thus the old value might already be too high compared to the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * set by 'tcp_set_rto' in tcp_input.c which resets the rto without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * exponential backoff behaviour to avoid continue hammering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * linear-timeout retransmissions into a black hole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (sk->sk_state == TCP_ESTABLISHED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) (tp->thin_lto || net->ipv4.sysctl_tcp_thin_linear_timeouts) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) tcp_stream_is_thin(tp) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) icsk->icsk_backoff = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) /* Use normal (exponential) backoff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) tcp_clamp_rto_to_user_timeout(sk), TCP_RTO_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1 + 1, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) __sk_dst_reset(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) out:;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) /* Called with bottom-half processing disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) Called by tcp_write_timer() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) void tcp_write_timer_handler(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) int event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) !icsk->icsk_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (time_after(icsk->icsk_timeout, jiffies)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) tcp_mstamp_refresh(tcp_sk(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) event = icsk->icsk_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) case ICSK_TIME_REO_TIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) tcp_rack_reo_timeout(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) case ICSK_TIME_LOSS_PROBE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) tcp_send_loss_probe(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) case ICSK_TIME_RETRANS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) icsk->icsk_pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) tcp_retransmit_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) case ICSK_TIME_PROBE0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) icsk->icsk_pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) tcp_probe_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) sk_mem_reclaim(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) static void tcp_write_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) struct inet_connection_sock *icsk =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) from_timer(icsk, t, icsk_retransmit_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) struct sock *sk = &icsk->icsk_inet.sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (!sock_owned_by_user(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) tcp_write_timer_handler(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) /* delegate our work to tcp_release_cb() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &sk->sk_tsq_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) void tcp_syn_ack_timeout(const struct request_sock *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) struct net *net = read_pnet(&inet_rsk(req)->ireq_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) __NET_INC_STATS(net, LINUX_MIB_TCPTIMEOUTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) EXPORT_SYMBOL(tcp_syn_ack_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) void tcp_set_keepalive(struct sock *sk, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (val && !sock_flag(sk, SOCK_KEEPOPEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) else if (!val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) inet_csk_delete_keepalive_timer(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) EXPORT_SYMBOL_GPL(tcp_set_keepalive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) static void tcp_keepalive_timer (struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) struct sock *sk = from_timer(sk, t, sk_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) u32 elapsed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) /* Only process if socket is not in use. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (sock_owned_by_user(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) /* Try again later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) inet_csk_reset_keepalive_timer (sk, HZ/20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (sk->sk_state == TCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) pr_err("Hmm... keepalive on a LISTEN ???\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) tcp_mstamp_refresh(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (tp->linger2 >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (tmo > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) tcp_send_active_reset(sk, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) goto death;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (!sock_flag(sk, SOCK_KEEPOPEN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) elapsed = keepalive_time_when(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) /* It is alive without keepalive 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (tp->packets_out || !tcp_write_queue_empty(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) goto resched;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) elapsed = keepalive_time_elapsed(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) if (elapsed >= keepalive_time_when(tp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) /* If the TCP_USER_TIMEOUT option is enabled, use that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * to determine when to timeout instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) if ((icsk->icsk_user_timeout != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) elapsed >= msecs_to_jiffies(icsk->icsk_user_timeout) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) icsk->icsk_probes_out > 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) (icsk->icsk_user_timeout == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) icsk->icsk_probes_out >= keepalive_probes(tp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) tcp_send_active_reset(sk, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) tcp_write_err(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (tcp_write_wakeup(sk, LINUX_MIB_TCPKEEPALIVE) <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) icsk->icsk_probes_out++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) elapsed = keepalive_intvl_when(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) /* If keepalive was lost due to local congestion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * try harder.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) elapsed = TCP_RESOURCE_PROBE_INTERVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) /* It is tp->rcv_tstamp + keepalive_time_when(tp) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) elapsed = keepalive_time_when(tp) - elapsed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) sk_mem_reclaim(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) resched:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) inet_csk_reset_keepalive_timer (sk, elapsed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) death:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) tcp_done(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) static enum hrtimer_restart tcp_compressed_ack_kick(struct hrtimer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) struct tcp_sock *tp = container_of(timer, struct tcp_sock, compressed_ack_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) struct sock *sk = (struct sock *)tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) if (!sock_owned_by_user(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (tp->compressed_ack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) /* Since we have to send one ack finally,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * substract one from tp->compressed_ack to keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * LINUX_MIB_TCPACKCOMPRESSED accurate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) tp->compressed_ack--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) tcp_send_ack(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) &sk->sk_tsq_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) void tcp_init_xmit_timers(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) &tcp_keepalive_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) hrtimer_init(&tcp_sk(sk)->pacing_timer, CLOCK_MONOTONIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) HRTIMER_MODE_ABS_PINNED_SOFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) tcp_sk(sk)->pacing_timer.function = tcp_pace_kick;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) hrtimer_init(&tcp_sk(sk)->compressed_ack_timer, CLOCK_MONOTONIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) HRTIMER_MODE_REL_PINNED_SOFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) tcp_sk(sk)->compressed_ack_timer.function = tcp_compressed_ack_kick;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) }