11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Implementation of the Transmission Control Protocol(TCP). 71da177e4SLinus Torvalds * 802c30a84SJesper Juhl * Authors: Ross Biro 91da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 101da177e4SLinus Torvalds * Mark Evans, <evansmp@uhura.aston.ac.uk> 111da177e4SLinus Torvalds * Corey Minyard <wf-rch!minyard@relay.EU.net> 121da177e4SLinus Torvalds * Florian La Roche, <flla@stud.uni-sb.de> 131da177e4SLinus Torvalds * Charles Hedrick, <hedrick@klinzhai.rutgers.edu> 141da177e4SLinus Torvalds * Linus Torvalds, <torvalds@cs.helsinki.fi> 151da177e4SLinus Torvalds * Alan Cox, <gw4pts@gw4pts.ampr.org> 161da177e4SLinus Torvalds * Matthew Dillon, <dillon@apollo.west.oic.com> 171da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 181da177e4SLinus Torvalds * Jorge Cwik, <jorge@laser.satlink.net> 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include <linux/module.h> 225a0e3ad6STejun Heo #include <linux/gfp.h> 231da177e4SLinus Torvalds #include <net/tcp.h> 241da177e4SLinus Torvalds 25ab32ea5dSBrian Haley int sysctl_tcp_syn_retries __read_mostly = TCP_SYN_RETRIES; 26ab32ea5dSBrian Haley int sysctl_tcp_synack_retries __read_mostly = TCP_SYNACK_RETRIES; 27ab32ea5dSBrian Haley int sysctl_tcp_keepalive_time __read_mostly = TCP_KEEPALIVE_TIME; 28ab32ea5dSBrian Haley int sysctl_tcp_keepalive_probes __read_mostly = TCP_KEEPALIVE_PROBES; 29ab32ea5dSBrian Haley int sysctl_tcp_keepalive_intvl __read_mostly = TCP_KEEPALIVE_INTVL; 30ab32ea5dSBrian Haley int sysctl_tcp_retries1 __read_mostly = TCP_RETR1; 31ab32ea5dSBrian Haley int sysctl_tcp_retries2 __read_mostly = TCP_RETR2; 32ab32ea5dSBrian Haley int sysctl_tcp_orphan_retries __read_mostly; 3336e31b0aSAndreas Petlund int sysctl_tcp_thin_linear_timeouts __read_mostly; 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds static void tcp_write_err(struct sock *sk) 361da177e4SLinus Torvalds { 371da177e4SLinus Torvalds sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT; 381da177e4SLinus Torvalds sk->sk_error_report(sk); 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds tcp_done(sk); 41de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT); 421da177e4SLinus Torvalds } 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds /* Do not allow orphaned sockets to eat all our resources. 451da177e4SLinus Torvalds * This is direct violation of TCP specs, but it is required 461da177e4SLinus Torvalds * to prevent DoS attacks. It is called when a retransmission timeout 471da177e4SLinus Torvalds * or zero probe timeout occurs on orphaned socket. 481da177e4SLinus Torvalds * 49caa20d9aSStephen Hemminger * Criteria is still not confirmed experimentally and may change. 501da177e4SLinus Torvalds * We kill the socket, if: 511da177e4SLinus Torvalds * 1. If number of orphaned sockets exceeds an administratively configured 521da177e4SLinus Torvalds * limit. 531da177e4SLinus Torvalds * 2. If we have strong memory pressure. 541da177e4SLinus Torvalds */ 55b248230cSYuchung Cheng static int tcp_out_of_resources(struct sock *sk, bool do_reset) 561da177e4SLinus Torvalds { 571da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 58ad1af0feSDavid S. Miller int shift = 0; 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds /* If peer does not open window for long time, or did not transmit 611da177e4SLinus Torvalds * anything for long time, penalize it. */ 621da177e4SLinus Torvalds if ((s32)(tcp_time_stamp - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset) 63ad1af0feSDavid S. Miller shift++; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* If some dubious ICMP arrived, penalize even more. */ 661da177e4SLinus Torvalds if (sk->sk_err_soft) 67ad1af0feSDavid S. Miller shift++; 681da177e4SLinus Torvalds 69efcdbf24SArun Sharma if (tcp_check_oom(sk, shift)) { 701da177e4SLinus Torvalds /* Catch exceptional cases, when connection requires reset. 711da177e4SLinus Torvalds * 1. Last segment was sent recently. */ 721da177e4SLinus Torvalds if ((s32)(tcp_time_stamp - tp->lsndtime) <= TCP_TIMEWAIT_LEN || 731da177e4SLinus Torvalds /* 2. Window is closed. */ 741da177e4SLinus Torvalds (!tp->snd_wnd && !tp->packets_out)) 75b248230cSYuchung Cheng do_reset = true; 761da177e4SLinus Torvalds if (do_reset) 771da177e4SLinus Torvalds tcp_send_active_reset(sk, GFP_ATOMIC); 781da177e4SLinus Torvalds tcp_done(sk); 79de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY); 801da177e4SLinus Torvalds return 1; 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds return 0; 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* Calculate maximal number or retries on an orphaned socket. */ 861da177e4SLinus Torvalds static int tcp_orphan_retries(struct sock *sk, int alive) 871da177e4SLinus Torvalds { 881da177e4SLinus Torvalds int retries = sysctl_tcp_orphan_retries; /* May be zero. */ 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* We know from an ICMP that something is wrong. */ 911da177e4SLinus Torvalds if (sk->sk_err_soft && !alive) 921da177e4SLinus Torvalds retries = 0; 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds /* However, if socket sent something recently, select some safe 951da177e4SLinus Torvalds * number of retries. 8 corresponds to >100 seconds with minimal 961da177e4SLinus Torvalds * RTO of 200msec. */ 971da177e4SLinus Torvalds if (retries == 0 && alive) 981da177e4SLinus Torvalds retries = 8; 991da177e4SLinus Torvalds return retries; 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 102ce55dd36SEric Dumazet static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk) 103ce55dd36SEric Dumazet { 104*b0f9ca53SFan Du struct net *net = sock_net(sk); 105*b0f9ca53SFan Du 106ce55dd36SEric Dumazet /* Black hole detection */ 107*b0f9ca53SFan Du if (net->ipv4.sysctl_tcp_mtu_probing) { 108ce55dd36SEric Dumazet if (!icsk->icsk_mtup.enabled) { 109ce55dd36SEric Dumazet icsk->icsk_mtup.enabled = 1; 110ce55dd36SEric Dumazet tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 111ce55dd36SEric Dumazet } else { 112*b0f9ca53SFan Du struct net *net = sock_net(sk); 113ce55dd36SEric Dumazet struct tcp_sock *tp = tcp_sk(sk); 114829942c1SDavid S. Miller int mss; 115829942c1SDavid S. Miller 1168beb5c5fSEric Dumazet mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1; 117*b0f9ca53SFan Du mss = min(net->ipv4.sysctl_tcp_base_mss, mss); 118ce55dd36SEric Dumazet mss = max(mss, 68 - tp->tcp_header_len); 119ce55dd36SEric Dumazet icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss); 120ce55dd36SEric Dumazet tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 121ce55dd36SEric Dumazet } 122ce55dd36SEric Dumazet } 123ce55dd36SEric Dumazet } 124ce55dd36SEric Dumazet 1252f7de571SDamian Lukowski /* This function calculates a "timeout" which is equivalent to the timeout of a 1263ad2f3fbSDaniel Mack * TCP connection after "boundary" unsuccessful, exponentially backed-off 1274d22f7d3SDamian Lukowski * retransmissions with an initial RTO of TCP_RTO_MIN or TCP_TIMEOUT_INIT if 1284d22f7d3SDamian Lukowski * syn_set flag is set. 1292f7de571SDamian Lukowski */ 1302f7de571SDamian Lukowski static bool retransmits_timed_out(struct sock *sk, 131dca43c75SJerry Chu unsigned int boundary, 13221a180cdSDavid S. Miller unsigned int timeout, 1334d22f7d3SDamian Lukowski bool syn_set) 1342f7de571SDamian Lukowski { 135dca43c75SJerry Chu unsigned int linear_backoff_thresh, start_ts; 1364d22f7d3SDamian Lukowski unsigned int rto_base = syn_set ? TCP_TIMEOUT_INIT : TCP_RTO_MIN; 1372f7de571SDamian Lukowski 1382f7de571SDamian Lukowski if (!inet_csk(sk)->icsk_retransmits) 1392f7de571SDamian Lukowski return false; 1402f7de571SDamian Lukowski 1412f7de571SDamian Lukowski start_ts = tcp_sk(sk)->retrans_stamp; 1427faee5c0SEric Dumazet if (unlikely(!start_ts)) 1437faee5c0SEric Dumazet start_ts = tcp_skb_timestamp(tcp_write_queue_head(sk)); 1442f7de571SDamian Lukowski 145dca43c75SJerry Chu if (likely(timeout == 0)) { 1464d22f7d3SDamian Lukowski linear_backoff_thresh = ilog2(TCP_RTO_MAX/rto_base); 1472f7de571SDamian Lukowski 1482f7de571SDamian Lukowski if (boundary <= linear_backoff_thresh) 1494d22f7d3SDamian Lukowski timeout = ((2 << boundary) - 1) * rto_base; 1502f7de571SDamian Lukowski else 1514d22f7d3SDamian Lukowski timeout = ((2 << linear_backoff_thresh) - 1) * rto_base + 1522f7de571SDamian Lukowski (boundary - linear_backoff_thresh) * TCP_RTO_MAX; 153dca43c75SJerry Chu } 1542f7de571SDamian Lukowski return (tcp_time_stamp - start_ts) >= timeout; 1552f7de571SDamian Lukowski } 1562f7de571SDamian Lukowski 1571da177e4SLinus Torvalds /* A write timeout has occurred. Process the after effects. */ 1581da177e4SLinus Torvalds static int tcp_write_timeout(struct sock *sk) 1591da177e4SLinus Torvalds { 1605d424d5aSJohn Heffner struct inet_connection_sock *icsk = inet_csk(sk); 161c968601dSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 1621da177e4SLinus Torvalds int retry_until; 1633db1cd5cSRusty Russell bool do_reset, syn_set = false; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { 166c968601dSYuchung Cheng if (icsk->icsk_retransmits) { 167b6c6712aSEric Dumazet dst_negative_advice(sk); 168c968601dSYuchung Cheng if (tp->syn_fastopen || tp->syn_data) 169c968601dSYuchung Cheng tcp_fastopen_cache_set(sk, 0, NULL, true); 170f19c29e3SYuchung Cheng if (tp->syn_data) 171f19c29e3SYuchung Cheng NET_INC_STATS_BH(sock_net(sk), 172f19c29e3SYuchung Cheng LINUX_MIB_TCPFASTOPENACTIVEFAIL); 173c968601dSYuchung Cheng } 174463c84b9SArnaldo Carvalho de Melo retry_until = icsk->icsk_syn_retries ? : sysctl_tcp_syn_retries; 1753db1cd5cSRusty Russell syn_set = true; 1761da177e4SLinus Torvalds } else { 17721a180cdSDavid S. Miller if (retransmits_timed_out(sk, sysctl_tcp_retries1, 0, 0)) { 1785d424d5aSJohn Heffner /* Black hole detection */ 179ce55dd36SEric Dumazet tcp_mtu_probing(icsk, sk); 1801da177e4SLinus Torvalds 181b6c6712aSEric Dumazet dst_negative_advice(sk); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds retry_until = sysctl_tcp_retries2; 1851da177e4SLinus Torvalds if (sock_flag(sk, SOCK_DEAD)) { 186fcdd1cf4SEric Dumazet const int alive = icsk->icsk_rto < TCP_RTO_MAX; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds retry_until = tcp_orphan_retries(sk, alive); 1896fa12c85SDamian Lukowski do_reset = alive || 19021a180cdSDavid S. Miller !retransmits_timed_out(sk, retry_until, 0, 0); 1911da177e4SLinus Torvalds 1926fa12c85SDamian Lukowski if (tcp_out_of_resources(sk, do_reset)) 1931da177e4SLinus Torvalds return 1; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 197dca43c75SJerry Chu if (retransmits_timed_out(sk, retry_until, 19821a180cdSDavid S. Miller syn_set ? 0 : icsk->icsk_user_timeout, syn_set)) { 1991da177e4SLinus Torvalds /* Has it gone just too far? */ 2001da177e4SLinus Torvalds tcp_write_err(sk); 2011da177e4SLinus Torvalds return 1; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds return 0; 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2066f458dfbSEric Dumazet void tcp_delack_timer_handler(struct sock *sk) 2071da177e4SLinus Torvalds { 2081da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 209463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2101da177e4SLinus Torvalds 2119993e7d3SDavid S. Miller sk_mem_reclaim_partial(sk); 2121da177e4SLinus Torvalds 213463c84b9SArnaldo Carvalho de Melo if (sk->sk_state == TCP_CLOSE || !(icsk->icsk_ack.pending & ICSK_ACK_TIMER)) 2141da177e4SLinus Torvalds goto out; 2151da177e4SLinus Torvalds 216463c84b9SArnaldo Carvalho de Melo if (time_after(icsk->icsk_ack.timeout, jiffies)) { 217463c84b9SArnaldo Carvalho de Melo sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout); 2181da177e4SLinus Torvalds goto out; 2191da177e4SLinus Torvalds } 220463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER; 2211da177e4SLinus Torvalds 222b03efcfbSDavid S. Miller if (!skb_queue_empty(&tp->ucopy.prequeue)) { 2231da177e4SLinus Torvalds struct sk_buff *skb; 2241da177e4SLinus Torvalds 225de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSCHEDULERFAILED); 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds while ((skb = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) 228c57943a1SPeter Zijlstra sk_backlog_rcv(sk, skb); 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds tp->ucopy.memory = 0; 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds 233463c84b9SArnaldo Carvalho de Melo if (inet_csk_ack_scheduled(sk)) { 234463c84b9SArnaldo Carvalho de Melo if (!icsk->icsk_ack.pingpong) { 2351da177e4SLinus Torvalds /* Delayed ACK missed: inflate ATO. */ 236463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto); 2371da177e4SLinus Torvalds } else { 2381da177e4SLinus Torvalds /* Delayed ACK missed: leave pingpong mode and 2391da177e4SLinus Torvalds * deflate ATO. 2401da177e4SLinus Torvalds */ 241463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.pingpong = 0; 242463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = TCP_ATO_MIN; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds tcp_send_ack(sk); 245de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKS); 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds out: 249180d8cd9SGlauber Costa if (sk_under_memory_pressure(sk)) 2503ab224beSHideo Aoki sk_mem_reclaim(sk); 2516f458dfbSEric Dumazet } 2526f458dfbSEric Dumazet 2536f458dfbSEric Dumazet static void tcp_delack_timer(unsigned long data) 2546f458dfbSEric Dumazet { 2556f458dfbSEric Dumazet struct sock *sk = (struct sock *)data; 2566f458dfbSEric Dumazet 2576f458dfbSEric Dumazet bh_lock_sock(sk); 2586f458dfbSEric Dumazet if (!sock_owned_by_user(sk)) { 2596f458dfbSEric Dumazet tcp_delack_timer_handler(sk); 2606f458dfbSEric Dumazet } else { 2616f458dfbSEric Dumazet inet_csk(sk)->icsk_ack.blocked = 1; 2626f458dfbSEric Dumazet NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED); 2636f458dfbSEric Dumazet /* deleguate our work to tcp_release_cb() */ 264144d56e9SEric Dumazet if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &tcp_sk(sk)->tsq_flags)) 265144d56e9SEric Dumazet sock_hold(sk); 2666f458dfbSEric Dumazet } 2671da177e4SLinus Torvalds bh_unlock_sock(sk); 2681da177e4SLinus Torvalds sock_put(sk); 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds static void tcp_probe_timer(struct sock *sk) 2721da177e4SLinus Torvalds { 2736687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2741da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 2751da177e4SLinus Torvalds int max_probes; 276b248230cSYuchung Cheng u32 start_ts; 2771da177e4SLinus Torvalds 278fe067e8aSDavid S. Miller if (tp->packets_out || !tcp_send_head(sk)) { 2796687e988SArnaldo Carvalho de Melo icsk->icsk_probes_out = 0; 2801da177e4SLinus Torvalds return; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 283b248230cSYuchung Cheng /* RFC 1122 4.2.2.17 requires the sender to stay open indefinitely as 284b248230cSYuchung Cheng * long as the receiver continues to respond probes. We support this by 285b248230cSYuchung Cheng * default and reset icsk_probes_out with incoming ACKs. But if the 286b248230cSYuchung Cheng * socket is orphaned or the user specifies TCP_USER_TIMEOUT, we 287b248230cSYuchung Cheng * kill the socket when the retry count and the time exceeds the 288b248230cSYuchung Cheng * corresponding system limit. We also implement similar policy when 289b248230cSYuchung Cheng * we use RTO to probe window in tcp_retransmit_timer(). 2901da177e4SLinus Torvalds */ 291b248230cSYuchung Cheng start_ts = tcp_skb_timestamp(tcp_send_head(sk)); 292b248230cSYuchung Cheng if (!start_ts) 293b248230cSYuchung Cheng skb_mstamp_get(&tcp_send_head(sk)->skb_mstamp); 294b248230cSYuchung Cheng else if (icsk->icsk_user_timeout && 295b248230cSYuchung Cheng (s32)(tcp_time_stamp - start_ts) > icsk->icsk_user_timeout) 296b248230cSYuchung Cheng goto abort; 2971da177e4SLinus Torvalds 298b248230cSYuchung Cheng max_probes = sysctl_tcp_retries2; 2991da177e4SLinus Torvalds if (sock_flag(sk, SOCK_DEAD)) { 300fcdd1cf4SEric Dumazet const int alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds max_probes = tcp_orphan_retries(sk, alive); 303b248230cSYuchung Cheng if (!alive && icsk->icsk_backoff >= max_probes) 304b248230cSYuchung Cheng goto abort; 305b248230cSYuchung Cheng if (tcp_out_of_resources(sk, true)) 3061da177e4SLinus Torvalds return; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 3096687e988SArnaldo Carvalho de Melo if (icsk->icsk_probes_out > max_probes) { 310b248230cSYuchung Cheng abort: tcp_write_err(sk); 3111da177e4SLinus Torvalds } else { 3121da177e4SLinus Torvalds /* Only send another probe if we didn't close things up. */ 3131da177e4SLinus Torvalds tcp_send_probe0(sk); 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /* 3188336886fSJerry Chu * Timer for Fast Open socket to retransmit SYNACK. Note that the 3198336886fSJerry Chu * sk here is the child socket, not the parent (listener) socket. 3208336886fSJerry Chu */ 3218336886fSJerry Chu static void tcp_fastopen_synack_timer(struct sock *sk) 3228336886fSJerry Chu { 3238336886fSJerry Chu struct inet_connection_sock *icsk = inet_csk(sk); 3248336886fSJerry Chu int max_retries = icsk->icsk_syn_retries ? : 3258336886fSJerry Chu sysctl_tcp_synack_retries + 1; /* add one more retry for fastopen */ 3268336886fSJerry Chu struct request_sock *req; 3278336886fSJerry Chu 3288336886fSJerry Chu req = tcp_sk(sk)->fastopen_rsk; 3298336886fSJerry Chu req->rsk_ops->syn_ack_timeout(sk, req); 3308336886fSJerry Chu 331e6c022a4SEric Dumazet if (req->num_timeout >= max_retries) { 3328336886fSJerry Chu tcp_write_err(sk); 3338336886fSJerry Chu return; 3348336886fSJerry Chu } 3358336886fSJerry Chu /* XXX (TFO) - Unlike regular SYN-ACK retransmit, we ignore error 3368336886fSJerry Chu * returned from rtx_syn_ack() to make it more persistent like 3378336886fSJerry Chu * regular retransmit because if the child socket has been accepted 3388336886fSJerry Chu * it's not good to give up too easily. 3398336886fSJerry Chu */ 340e6c022a4SEric Dumazet inet_rtx_syn_ack(sk, req); 341e6c022a4SEric Dumazet req->num_timeout++; 3428336886fSJerry Chu inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 343e6c022a4SEric Dumazet TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX); 3448336886fSJerry Chu } 3458336886fSJerry Chu 3468336886fSJerry Chu /* 3471da177e4SLinus Torvalds * The TCP retransmit timer. 3481da177e4SLinus Torvalds */ 3491da177e4SLinus Torvalds 350f1ecd5d9SDamian Lukowski void tcp_retransmit_timer(struct sock *sk) 3511da177e4SLinus Torvalds { 3521da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 353463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 3541da177e4SLinus Torvalds 3558336886fSJerry Chu if (tp->fastopen_rsk) { 35637561f68SJerry Chu WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV && 3578336886fSJerry Chu sk->sk_state != TCP_FIN_WAIT1); 3588336886fSJerry Chu tcp_fastopen_synack_timer(sk); 3598336886fSJerry Chu /* Before we receive ACK to our SYN-ACK don't retransmit 3608336886fSJerry Chu * anything else (e.g., data or FIN segments). 3618336886fSJerry Chu */ 3628336886fSJerry Chu return; 3638336886fSJerry Chu } 3641da177e4SLinus Torvalds if (!tp->packets_out) 3651da177e4SLinus Torvalds goto out; 3661da177e4SLinus Torvalds 367547b792cSIlpo Järvinen WARN_ON(tcp_write_queue_empty(sk)); 3681da177e4SLinus Torvalds 3699b717a8dSNandita Dukkipati tp->tlp_high_seq = 0; 3709b717a8dSNandita Dukkipati 3711da177e4SLinus Torvalds if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) && 3721da177e4SLinus Torvalds !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) { 3731da177e4SLinus Torvalds /* Receiver dastardly shrinks window. Our retransmits 3741da177e4SLinus Torvalds * become zero probes, but we should not timeout this 3751da177e4SLinus Torvalds * connection. If the socket is an orphan, time it out, 3761da177e4SLinus Torvalds * we cannot allow such beasts to hang infinitely. 3771da177e4SLinus Torvalds */ 3781da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 379569508c9SYOSHIFUJI Hideaki if (sk->sk_family == AF_INET) { 380ba7a46f1SJoe Perches net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n", 381afd46503SJoe Perches &inet->inet_daddr, 382ba7a46f1SJoe Perches ntohs(inet->inet_dport), 383ba7a46f1SJoe Perches inet->inet_num, 384afd46503SJoe Perches tp->snd_una, tp->snd_nxt); 3851da177e4SLinus Torvalds } 386dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 387569508c9SYOSHIFUJI Hideaki else if (sk->sk_family == AF_INET6) { 388ba7a46f1SJoe Perches net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n", 389efe4208fSEric Dumazet &sk->sk_v6_daddr, 390ba7a46f1SJoe Perches ntohs(inet->inet_dport), 391ba7a46f1SJoe Perches inet->inet_num, 392afd46503SJoe Perches tp->snd_una, tp->snd_nxt); 393569508c9SYOSHIFUJI Hideaki } 394569508c9SYOSHIFUJI Hideaki #endif 3951da177e4SLinus Torvalds if (tcp_time_stamp - tp->rcv_tstamp > TCP_RTO_MAX) { 3961da177e4SLinus Torvalds tcp_write_err(sk); 3971da177e4SLinus Torvalds goto out; 3981da177e4SLinus Torvalds } 3995ae344c9SNeal Cardwell tcp_enter_loss(sk); 400fe067e8aSDavid S. Miller tcp_retransmit_skb(sk, tcp_write_queue_head(sk)); 4011da177e4SLinus Torvalds __sk_dst_reset(sk); 4021da177e4SLinus Torvalds goto out_reset_timer; 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds if (tcp_write_timeout(sk)) 4061da177e4SLinus Torvalds goto out; 4071da177e4SLinus Torvalds 408463c84b9SArnaldo Carvalho de Melo if (icsk->icsk_retransmits == 0) { 40940b215e5SPavel Emelyanov int mib_idx; 41040b215e5SPavel Emelyanov 411c60ce4e2SIlpo Järvinen if (icsk->icsk_ca_state == TCP_CA_Recovery) { 412bc079e9eSIlpo Järvinen if (tcp_is_sack(tp)) 413bc079e9eSIlpo Järvinen mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL; 414bc079e9eSIlpo Järvinen else 415bc079e9eSIlpo Järvinen mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL; 4166687e988SArnaldo Carvalho de Melo } else if (icsk->icsk_ca_state == TCP_CA_Loss) { 41740b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPLOSSFAILURES; 418c60ce4e2SIlpo Järvinen } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) || 419c60ce4e2SIlpo Järvinen tp->sacked_out) { 420c60ce4e2SIlpo Järvinen if (tcp_is_sack(tp)) 421c60ce4e2SIlpo Järvinen mib_idx = LINUX_MIB_TCPSACKFAILURES; 422c60ce4e2SIlpo Järvinen else 423c60ce4e2SIlpo Järvinen mib_idx = LINUX_MIB_TCPRENOFAILURES; 4241da177e4SLinus Torvalds } else { 42540b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPTIMEOUTS; 4261da177e4SLinus Torvalds } 427de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), mib_idx); 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds 4305ae344c9SNeal Cardwell tcp_enter_loss(sk); 4311da177e4SLinus Torvalds 432fe067e8aSDavid S. Miller if (tcp_retransmit_skb(sk, tcp_write_queue_head(sk)) > 0) { 4331da177e4SLinus Torvalds /* Retransmission failed because of local congestion, 4341da177e4SLinus Torvalds * do not backoff. 4351da177e4SLinus Torvalds */ 436463c84b9SArnaldo Carvalho de Melo if (!icsk->icsk_retransmits) 437463c84b9SArnaldo Carvalho de Melo icsk->icsk_retransmits = 1; 438463c84b9SArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 4393f421baaSArnaldo Carvalho de Melo min(icsk->icsk_rto, TCP_RESOURCE_PROBE_INTERVAL), 4403f421baaSArnaldo Carvalho de Melo TCP_RTO_MAX); 4411da177e4SLinus Torvalds goto out; 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /* Increase the timeout each time we retransmit. Note that 4451da177e4SLinus Torvalds * we do not increase the rtt estimate. rto is initialized 4461da177e4SLinus Torvalds * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests 4471da177e4SLinus Torvalds * that doubling rto each time is the least we can get away with. 4481da177e4SLinus Torvalds * In KA9Q, Karn uses this for the first few times, and then 4491da177e4SLinus Torvalds * goes to quadratic. netBSD doubles, but only goes up to *64, 4501da177e4SLinus Torvalds * and clamps at 1 to 64 sec afterwards. Note that 120 sec is 4511da177e4SLinus Torvalds * defined in the protocol as the maximum possible RTT. I guess 4521da177e4SLinus Torvalds * we'll have to use something other than TCP to talk to the 4531da177e4SLinus Torvalds * University of Mars. 4541da177e4SLinus Torvalds * 4551da177e4SLinus Torvalds * PAWS allows us longer timeouts and large windows, so once 4561da177e4SLinus Torvalds * implemented ftp to mars will work nicely. We will have to fix 4571da177e4SLinus Torvalds * the 120 second clamps though! 4581da177e4SLinus Torvalds */ 459463c84b9SArnaldo Carvalho de Melo icsk->icsk_backoff++; 460463c84b9SArnaldo Carvalho de Melo icsk->icsk_retransmits++; 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds out_reset_timer: 46336e31b0aSAndreas Petlund /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is 46436e31b0aSAndreas Petlund * used to reset timer, set to 0. Recalculate 'icsk_rto' as this 46536e31b0aSAndreas Petlund * might be increased if the stream oscillates between thin and thick, 46636e31b0aSAndreas Petlund * thus the old value might already be too high compared to the value 46736e31b0aSAndreas Petlund * set by 'tcp_set_rto' in tcp_input.c which resets the rto without 46836e31b0aSAndreas Petlund * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating 46936e31b0aSAndreas Petlund * exponential backoff behaviour to avoid continue hammering 47036e31b0aSAndreas Petlund * linear-timeout retransmissions into a black hole 47136e31b0aSAndreas Petlund */ 47236e31b0aSAndreas Petlund if (sk->sk_state == TCP_ESTABLISHED && 47336e31b0aSAndreas Petlund (tp->thin_lto || sysctl_tcp_thin_linear_timeouts) && 47436e31b0aSAndreas Petlund tcp_stream_is_thin(tp) && 47536e31b0aSAndreas Petlund icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) { 47636e31b0aSAndreas Petlund icsk->icsk_backoff = 0; 47736e31b0aSAndreas Petlund icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX); 47836e31b0aSAndreas Petlund } else { 47936e31b0aSAndreas Petlund /* Use normal (exponential) backoff */ 480463c84b9SArnaldo Carvalho de Melo icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX); 48136e31b0aSAndreas Petlund } 4823f421baaSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX); 48321a180cdSDavid S. Miller if (retransmits_timed_out(sk, sysctl_tcp_retries1 + 1, 0, 0)) 4841da177e4SLinus Torvalds __sk_dst_reset(sk); 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds out:; 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 4896f458dfbSEric Dumazet void tcp_write_timer_handler(struct sock *sk) 4901da177e4SLinus Torvalds { 491463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 4921da177e4SLinus Torvalds int event; 4931da177e4SLinus Torvalds 494463c84b9SArnaldo Carvalho de Melo if (sk->sk_state == TCP_CLOSE || !icsk->icsk_pending) 4951da177e4SLinus Torvalds goto out; 4961da177e4SLinus Torvalds 497463c84b9SArnaldo Carvalho de Melo if (time_after(icsk->icsk_timeout, jiffies)) { 498463c84b9SArnaldo Carvalho de Melo sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout); 4991da177e4SLinus Torvalds goto out; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 502463c84b9SArnaldo Carvalho de Melo event = icsk->icsk_pending; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds switch (event) { 5056ba8a3b1SNandita Dukkipati case ICSK_TIME_EARLY_RETRANS: 5066ba8a3b1SNandita Dukkipati tcp_resume_early_retransmit(sk); 5076ba8a3b1SNandita Dukkipati break; 5086ba8a3b1SNandita Dukkipati case ICSK_TIME_LOSS_PROBE: 5096ba8a3b1SNandita Dukkipati tcp_send_loss_probe(sk); 5106ba8a3b1SNandita Dukkipati break; 511463c84b9SArnaldo Carvalho de Melo case ICSK_TIME_RETRANS: 5126ba8a3b1SNandita Dukkipati icsk->icsk_pending = 0; 5131da177e4SLinus Torvalds tcp_retransmit_timer(sk); 5141da177e4SLinus Torvalds break; 515463c84b9SArnaldo Carvalho de Melo case ICSK_TIME_PROBE0: 5166ba8a3b1SNandita Dukkipati icsk->icsk_pending = 0; 5171da177e4SLinus Torvalds tcp_probe_timer(sk); 5181da177e4SLinus Torvalds break; 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds out: 5223ab224beSHideo Aoki sk_mem_reclaim(sk); 5236f458dfbSEric Dumazet } 5246f458dfbSEric Dumazet 5256f458dfbSEric Dumazet static void tcp_write_timer(unsigned long data) 5266f458dfbSEric Dumazet { 5276f458dfbSEric Dumazet struct sock *sk = (struct sock *)data; 5286f458dfbSEric Dumazet 5296f458dfbSEric Dumazet bh_lock_sock(sk); 5306f458dfbSEric Dumazet if (!sock_owned_by_user(sk)) { 5316f458dfbSEric Dumazet tcp_write_timer_handler(sk); 5326f458dfbSEric Dumazet } else { 5336f458dfbSEric Dumazet /* deleguate our work to tcp_release_cb() */ 534144d56e9SEric Dumazet if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &tcp_sk(sk)->tsq_flags)) 535144d56e9SEric Dumazet sock_hold(sk); 5366f458dfbSEric Dumazet } 5371da177e4SLinus Torvalds bh_unlock_sock(sk); 5381da177e4SLinus Torvalds sock_put(sk); 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 541295f7324SArnaldo Carvalho de Melo /* 542295f7324SArnaldo Carvalho de Melo * Timer for listening sockets 543295f7324SArnaldo Carvalho de Melo */ 544295f7324SArnaldo Carvalho de Melo 545295f7324SArnaldo Carvalho de Melo static void tcp_synack_timer(struct sock *sk) 546295f7324SArnaldo Carvalho de Melo { 547a019d6feSArnaldo Carvalho de Melo inet_csk_reqsk_queue_prune(sk, TCP_SYNQ_INTERVAL, 548a019d6feSArnaldo Carvalho de Melo TCP_TIMEOUT_INIT, TCP_RTO_MAX); 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 55172659eccSOctavian Purdila void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req) 55272659eccSOctavian Purdila { 55372659eccSOctavian Purdila NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPTIMEOUTS); 55472659eccSOctavian Purdila } 55572659eccSOctavian Purdila EXPORT_SYMBOL(tcp_syn_ack_timeout); 55672659eccSOctavian Purdila 5571da177e4SLinus Torvalds void tcp_set_keepalive(struct sock *sk, int val) 5581da177e4SLinus Torvalds { 5591da177e4SLinus Torvalds if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) 5601da177e4SLinus Torvalds return; 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds if (val && !sock_flag(sk, SOCK_KEEPOPEN)) 563463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk))); 5641da177e4SLinus Torvalds else if (!val) 565463c84b9SArnaldo Carvalho de Melo inet_csk_delete_keepalive_timer(sk); 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds static void tcp_keepalive_timer (unsigned long data) 5701da177e4SLinus Torvalds { 5711da177e4SLinus Torvalds struct sock *sk = (struct sock *) data; 5726687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 5731da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 5746c37e5deSFlavio Leitner u32 elapsed; 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds /* Only process if socket is not in use. */ 5771da177e4SLinus Torvalds bh_lock_sock(sk); 5781da177e4SLinus Torvalds if (sock_owned_by_user(sk)) { 5791da177e4SLinus Torvalds /* Try again later. */ 580463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer (sk, HZ/20); 5811da177e4SLinus Torvalds goto out; 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds if (sk->sk_state == TCP_LISTEN) { 5851da177e4SLinus Torvalds tcp_synack_timer(sk); 5861da177e4SLinus Torvalds goto out; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) { 5901da177e4SLinus Torvalds if (tp->linger2 >= 0) { 591463c84b9SArnaldo Carvalho de Melo const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds if (tmo > 0) { 5941da177e4SLinus Torvalds tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); 5951da177e4SLinus Torvalds goto out; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds tcp_send_active_reset(sk, GFP_ATOMIC); 5991da177e4SLinus Torvalds goto death; 6001da177e4SLinus Torvalds } 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_KEEPOPEN) || sk->sk_state == TCP_CLOSE) 6031da177e4SLinus Torvalds goto out; 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds elapsed = keepalive_time_when(tp); 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds /* It is alive without keepalive 8) */ 608fe067e8aSDavid S. Miller if (tp->packets_out || tcp_send_head(sk)) 6091da177e4SLinus Torvalds goto resched; 6101da177e4SLinus Torvalds 6116c37e5deSFlavio Leitner elapsed = keepalive_time_elapsed(tp); 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds if (elapsed >= keepalive_time_when(tp)) { 614dca43c75SJerry Chu /* If the TCP_USER_TIMEOUT option is enabled, use that 615dca43c75SJerry Chu * to determine when to timeout instead. 616dca43c75SJerry Chu */ 617dca43c75SJerry Chu if ((icsk->icsk_user_timeout != 0 && 618dca43c75SJerry Chu elapsed >= icsk->icsk_user_timeout && 619dca43c75SJerry Chu icsk->icsk_probes_out > 0) || 620dca43c75SJerry Chu (icsk->icsk_user_timeout == 0 && 621dca43c75SJerry Chu icsk->icsk_probes_out >= keepalive_probes(tp))) { 6221da177e4SLinus Torvalds tcp_send_active_reset(sk, GFP_ATOMIC); 6231da177e4SLinus Torvalds tcp_write_err(sk); 6241da177e4SLinus Torvalds goto out; 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds if (tcp_write_wakeup(sk) <= 0) { 6276687e988SArnaldo Carvalho de Melo icsk->icsk_probes_out++; 6281da177e4SLinus Torvalds elapsed = keepalive_intvl_when(tp); 6291da177e4SLinus Torvalds } else { 6301da177e4SLinus Torvalds /* If keepalive was lost due to local congestion, 6311da177e4SLinus Torvalds * try harder. 6321da177e4SLinus Torvalds */ 6331da177e4SLinus Torvalds elapsed = TCP_RESOURCE_PROBE_INTERVAL; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds } else { 6361da177e4SLinus Torvalds /* It is tp->rcv_tstamp + keepalive_time_when(tp) */ 6371da177e4SLinus Torvalds elapsed = keepalive_time_when(tp) - elapsed; 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 6403ab224beSHideo Aoki sk_mem_reclaim(sk); 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds resched: 643463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer (sk, elapsed); 6441da177e4SLinus Torvalds goto out; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds death: 6471da177e4SLinus Torvalds tcp_done(sk); 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds out: 6501da177e4SLinus Torvalds bh_unlock_sock(sk); 6511da177e4SLinus Torvalds sock_put(sk); 6521da177e4SLinus Torvalds } 6536f458dfbSEric Dumazet 6546f458dfbSEric Dumazet void tcp_init_xmit_timers(struct sock *sk) 6556f458dfbSEric Dumazet { 6566f458dfbSEric Dumazet inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer, 6576f458dfbSEric Dumazet &tcp_keepalive_timer); 6586f458dfbSEric Dumazet } 6596f458dfbSEric Dumazet EXPORT_SYMBOL(tcp_init_xmit_timers); 660