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 25b701a99eSJon Maxwell static u32 tcp_clamp_rto_to_user_timeout(const struct sock *sk) 26b701a99eSJon Maxwell { 27b701a99eSJon Maxwell struct inet_connection_sock *icsk = inet_csk(sk); 28b701a99eSJon Maxwell u32 elapsed, start_ts; 299efdda4eSEric Dumazet s32 remaining; 30b701a99eSJon Maxwell 317ae18975SYuchung Cheng start_ts = tcp_sk(sk)->retrans_stamp; 327ae18975SYuchung Cheng if (!icsk->icsk_user_timeout) 33b701a99eSJon Maxwell return icsk->icsk_rto; 34b701a99eSJon Maxwell elapsed = tcp_time_stamp(tcp_sk(sk)) - start_ts; 359efdda4eSEric Dumazet remaining = icsk->icsk_user_timeout - elapsed; 369efdda4eSEric Dumazet if (remaining <= 0) 37b701a99eSJon Maxwell return 1; /* user timeout has passed; fire ASAP */ 389efdda4eSEric Dumazet 399efdda4eSEric Dumazet return min_t(u32, icsk->icsk_rto, msecs_to_jiffies(remaining)); 40b701a99eSJon Maxwell } 41b701a99eSJon Maxwell 42c380d37eSRichard Sailer /** 43c380d37eSRichard Sailer * tcp_write_err() - close socket and save error info 44c380d37eSRichard Sailer * @sk: The socket the error has appeared on. 45c380d37eSRichard Sailer * 46c380d37eSRichard Sailer * Returns: Nothing (void) 47c380d37eSRichard Sailer */ 48c380d37eSRichard Sailer 491da177e4SLinus Torvalds static void tcp_write_err(struct sock *sk) 501da177e4SLinus Torvalds { 511da177e4SLinus Torvalds sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT; 521da177e4SLinus Torvalds sk->sk_error_report(sk); 531da177e4SLinus Torvalds 54e05836acSSoheil Hassas Yeganeh tcp_write_queue_purge(sk); 551da177e4SLinus Torvalds tcp_done(sk); 5602a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT); 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds 59c380d37eSRichard Sailer /** 60c380d37eSRichard Sailer * tcp_out_of_resources() - Close socket if out of resources 61c380d37eSRichard Sailer * @sk: pointer to current socket 62c380d37eSRichard Sailer * @do_reset: send a last packet with reset flag 63c380d37eSRichard Sailer * 64c380d37eSRichard Sailer * Do not allow orphaned sockets to eat all our resources. 651da177e4SLinus Torvalds * This is direct violation of TCP specs, but it is required 661da177e4SLinus Torvalds * to prevent DoS attacks. It is called when a retransmission timeout 671da177e4SLinus Torvalds * or zero probe timeout occurs on orphaned socket. 681da177e4SLinus Torvalds * 694ee806d5SDan Streetman * Also close if our net namespace is exiting; in that case there is no 704ee806d5SDan Streetman * hope of ever communicating again since all netns interfaces are already 714ee806d5SDan Streetman * down (or about to be down), and we need to release our dst references, 724ee806d5SDan Streetman * which have been moved to the netns loopback interface, so the namespace 734ee806d5SDan Streetman * can finish exiting. This condition is only possible if we are a kernel 744ee806d5SDan Streetman * socket, as those do not hold references to the namespace. 754ee806d5SDan Streetman * 76caa20d9aSStephen Hemminger * Criteria is still not confirmed experimentally and may change. 771da177e4SLinus Torvalds * We kill the socket, if: 781da177e4SLinus Torvalds * 1. If number of orphaned sockets exceeds an administratively configured 791da177e4SLinus Torvalds * limit. 801da177e4SLinus Torvalds * 2. If we have strong memory pressure. 814ee806d5SDan Streetman * 3. If our net namespace is exiting. 821da177e4SLinus Torvalds */ 83b248230cSYuchung Cheng static int tcp_out_of_resources(struct sock *sk, bool do_reset) 841da177e4SLinus Torvalds { 851da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 86ad1af0feSDavid S. Miller int shift = 0; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* If peer does not open window for long time, or did not transmit 891da177e4SLinus Torvalds * anything for long time, penalize it. */ 90d635fbe2SEric Dumazet if ((s32)(tcp_jiffies32 - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset) 91ad1af0feSDavid S. Miller shift++; 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* If some dubious ICMP arrived, penalize even more. */ 941da177e4SLinus Torvalds if (sk->sk_err_soft) 95ad1af0feSDavid S. Miller shift++; 961da177e4SLinus Torvalds 97efcdbf24SArun Sharma if (tcp_check_oom(sk, shift)) { 981da177e4SLinus Torvalds /* Catch exceptional cases, when connection requires reset. 991da177e4SLinus Torvalds * 1. Last segment was sent recently. */ 100d635fbe2SEric Dumazet if ((s32)(tcp_jiffies32 - tp->lsndtime) <= TCP_TIMEWAIT_LEN || 1011da177e4SLinus Torvalds /* 2. Window is closed. */ 1021da177e4SLinus Torvalds (!tp->snd_wnd && !tp->packets_out)) 103b248230cSYuchung Cheng do_reset = true; 1041da177e4SLinus Torvalds if (do_reset) 1051da177e4SLinus Torvalds tcp_send_active_reset(sk, GFP_ATOMIC); 1061da177e4SLinus Torvalds tcp_done(sk); 10702a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY); 1081da177e4SLinus Torvalds return 1; 1091da177e4SLinus Torvalds } 1104ee806d5SDan Streetman 1114ee806d5SDan Streetman if (!check_net(sock_net(sk))) { 1124ee806d5SDan Streetman /* Not possible to send reset; just close */ 1134ee806d5SDan Streetman tcp_done(sk); 1144ee806d5SDan Streetman return 1; 1154ee806d5SDan Streetman } 1164ee806d5SDan Streetman 1171da177e4SLinus Torvalds return 0; 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 120c380d37eSRichard Sailer /** 121c380d37eSRichard Sailer * tcp_orphan_retries() - Returns maximal number of retries on an orphaned socket 122c380d37eSRichard Sailer * @sk: Pointer to the current socket. 123c380d37eSRichard Sailer * @alive: bool, socket alive state 124c380d37eSRichard Sailer */ 1257533ce30SRichard Sailer static int tcp_orphan_retries(struct sock *sk, bool alive) 1261da177e4SLinus Torvalds { 127c402d9beSNikolay Borisov int retries = sock_net(sk)->ipv4.sysctl_tcp_orphan_retries; /* May be zero. */ 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* We know from an ICMP that something is wrong. */ 1301da177e4SLinus Torvalds if (sk->sk_err_soft && !alive) 1311da177e4SLinus Torvalds retries = 0; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds /* However, if socket sent something recently, select some safe 1341da177e4SLinus Torvalds * number of retries. 8 corresponds to >100 seconds with minimal 1351da177e4SLinus Torvalds * RTO of 200msec. */ 1361da177e4SLinus Torvalds if (retries == 0 && alive) 1371da177e4SLinus Torvalds retries = 8; 1381da177e4SLinus Torvalds return retries; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 141ce55dd36SEric Dumazet static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk) 142ce55dd36SEric Dumazet { 143d0f36847SEric Dumazet const struct net *net = sock_net(sk); 144d0f36847SEric Dumazet int mss; 145b0f9ca53SFan Du 146ce55dd36SEric Dumazet /* Black hole detection */ 147d0f36847SEric Dumazet if (!net->ipv4.sysctl_tcp_mtu_probing) 148d0f36847SEric Dumazet return; 149d0f36847SEric Dumazet 150ce55dd36SEric Dumazet if (!icsk->icsk_mtup.enabled) { 151ce55dd36SEric Dumazet icsk->icsk_mtup.enabled = 1; 152c74df29aSEric Dumazet icsk->icsk_mtup.probe_timestamp = tcp_jiffies32; 153ce55dd36SEric Dumazet } else { 1548beb5c5fSEric Dumazet mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1; 155b0f9ca53SFan Du mss = min(net->ipv4.sysctl_tcp_base_mss, mss); 156d0f36847SEric Dumazet mss = max(mss, 68 - tcp_sk(sk)->tcp_header_len); 157ce55dd36SEric Dumazet icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss); 158d0f36847SEric Dumazet } 159ce55dd36SEric Dumazet tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 160ce55dd36SEric Dumazet } 161ce55dd36SEric Dumazet 16201a523b0SYuchung Cheng static unsigned int tcp_model_timeout(struct sock *sk, 16301a523b0SYuchung Cheng unsigned int boundary, 16401a523b0SYuchung Cheng unsigned int rto_base) 16501a523b0SYuchung Cheng { 16601a523b0SYuchung Cheng unsigned int linear_backoff_thresh, timeout; 167c380d37eSRichard Sailer 16801a523b0SYuchung Cheng linear_backoff_thresh = ilog2(TCP_RTO_MAX / rto_base); 16901a523b0SYuchung Cheng if (boundary <= linear_backoff_thresh) 17001a523b0SYuchung Cheng timeout = ((2 << boundary) - 1) * rto_base; 17101a523b0SYuchung Cheng else 17201a523b0SYuchung Cheng timeout = ((2 << linear_backoff_thresh) - 1) * rto_base + 17301a523b0SYuchung Cheng (boundary - linear_backoff_thresh) * TCP_RTO_MAX; 17401a523b0SYuchung Cheng return jiffies_to_msecs(timeout); 17501a523b0SYuchung Cheng } 176c380d37eSRichard Sailer /** 177c380d37eSRichard Sailer * retransmits_timed_out() - returns true if this connection has timed out 178c380d37eSRichard Sailer * @sk: The current socket 179c380d37eSRichard Sailer * @boundary: max number of retransmissions 180c380d37eSRichard Sailer * @timeout: A custom timeout value. 181c380d37eSRichard Sailer * If set to 0 the default timeout is calculated and used. 182c380d37eSRichard Sailer * Using TCP_RTO_MIN and the number of unsuccessful retransmits. 183c380d37eSRichard Sailer * 184c380d37eSRichard Sailer * The default "timeout" value this function can calculate and use 185c380d37eSRichard Sailer * is equivalent to the timeout of a TCP Connection 186c380d37eSRichard Sailer * after "boundary" unsuccessful, exponentially backed-off 187ce682ef6SEric Dumazet * retransmissions with an initial RTO of TCP_RTO_MIN. 1882f7de571SDamian Lukowski */ 1892f7de571SDamian Lukowski static bool retransmits_timed_out(struct sock *sk, 190dca43c75SJerry Chu unsigned int boundary, 191ce682ef6SEric Dumazet unsigned int timeout) 1922f7de571SDamian Lukowski { 19301a523b0SYuchung Cheng unsigned int start_ts; 1942f7de571SDamian Lukowski 1952f7de571SDamian Lukowski if (!inet_csk(sk)->icsk_retransmits) 1962f7de571SDamian Lukowski return false; 1972f7de571SDamian Lukowski 1987ae18975SYuchung Cheng start_ts = tcp_sk(sk)->retrans_stamp; 19901a523b0SYuchung Cheng if (likely(timeout == 0)) 20001a523b0SYuchung Cheng timeout = tcp_model_timeout(sk, boundary, TCP_RTO_MIN); 2012f7de571SDamian Lukowski 2029efdda4eSEric Dumazet return (s32)(tcp_time_stamp(tcp_sk(sk)) - start_ts - timeout) >= 0; 2032f7de571SDamian Lukowski } 2042f7de571SDamian Lukowski 2051da177e4SLinus Torvalds /* A write timeout has occurred. Process the after effects. */ 2061da177e4SLinus Torvalds static int tcp_write_timeout(struct sock *sk) 2071da177e4SLinus Torvalds { 2085d424d5aSJohn Heffner struct inet_connection_sock *icsk = inet_csk(sk); 209c968601dSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 2106fa25166SNikolay Borisov struct net *net = sock_net(sk); 211ce682ef6SEric Dumazet bool expired, do_reset; 2121da177e4SLinus Torvalds int retry_until; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { 215c968601dSYuchung Cheng if (icsk->icsk_retransmits) { 216b6c6712aSEric Dumazet dst_negative_advice(sk); 217c5715b8fSYuchung Cheng } else { 2183acf3ec3SLawrence Brakmo sk_rethink_txhash(sk); 219c968601dSYuchung Cheng } 2206fa25166SNikolay Borisov retry_until = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries; 221ce682ef6SEric Dumazet expired = icsk->icsk_retransmits >= retry_until; 2221da177e4SLinus Torvalds } else { 223ce682ef6SEric Dumazet if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1, 0)) { 2245d424d5aSJohn Heffner /* Black hole detection */ 225ce55dd36SEric Dumazet tcp_mtu_probing(icsk, sk); 2261da177e4SLinus Torvalds 227b6c6712aSEric Dumazet dst_negative_advice(sk); 2283acf3ec3SLawrence Brakmo } else { 2293acf3ec3SLawrence Brakmo sk_rethink_txhash(sk); 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 232c6214a97SNikolay Borisov retry_until = net->ipv4.sysctl_tcp_retries2; 2331da177e4SLinus Torvalds if (sock_flag(sk, SOCK_DEAD)) { 2347533ce30SRichard Sailer const bool alive = icsk->icsk_rto < TCP_RTO_MAX; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds retry_until = tcp_orphan_retries(sk, alive); 2376fa12c85SDamian Lukowski do_reset = alive || 238ce682ef6SEric Dumazet !retransmits_timed_out(sk, retry_until, 0); 2391da177e4SLinus Torvalds 2406fa12c85SDamian Lukowski if (tcp_out_of_resources(sk, do_reset)) 2411da177e4SLinus Torvalds return 1; 2421da177e4SLinus Torvalds } 243ce682ef6SEric Dumazet expired = retransmits_timed_out(sk, retry_until, 244ce682ef6SEric Dumazet icsk->icsk_user_timeout); 2451da177e4SLinus Torvalds } 2467268586bSYuchung Cheng tcp_fastopen_active_detect_blackhole(sk, expired); 247f89013f6SLawrence Brakmo 248f89013f6SLawrence Brakmo if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RTO_CB_FLAG)) 249f89013f6SLawrence Brakmo tcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RTO_CB, 250f89013f6SLawrence Brakmo icsk->icsk_retransmits, 251f89013f6SLawrence Brakmo icsk->icsk_rto, (int)expired); 252f89013f6SLawrence Brakmo 253ce682ef6SEric Dumazet if (expired) { 2541da177e4SLinus Torvalds /* Has it gone just too far? */ 2551da177e4SLinus Torvalds tcp_write_err(sk); 2561da177e4SLinus Torvalds return 1; 2571da177e4SLinus Torvalds } 258f89013f6SLawrence Brakmo 2591da177e4SLinus Torvalds return 0; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 262c10d9310SEric Dumazet /* Called with BH disabled */ 2636f458dfbSEric Dumazet void tcp_delack_timer_handler(struct sock *sk) 2641da177e4SLinus Torvalds { 265463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2661da177e4SLinus Torvalds 2679993e7d3SDavid S. Miller sk_mem_reclaim_partial(sk); 2681da177e4SLinus Torvalds 26902b2faafSEric Dumazet if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) || 27002b2faafSEric Dumazet !(icsk->icsk_ack.pending & ICSK_ACK_TIMER)) 2711da177e4SLinus Torvalds goto out; 2721da177e4SLinus Torvalds 273463c84b9SArnaldo Carvalho de Melo if (time_after(icsk->icsk_ack.timeout, jiffies)) { 274463c84b9SArnaldo Carvalho de Melo sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout); 2751da177e4SLinus Torvalds goto out; 2761da177e4SLinus Torvalds } 277463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER; 2781da177e4SLinus Torvalds 279463c84b9SArnaldo Carvalho de Melo if (inet_csk_ack_scheduled(sk)) { 28031954cd8SWei Wang if (!inet_csk_in_pingpong_mode(sk)) { 2811da177e4SLinus Torvalds /* Delayed ACK missed: inflate ATO. */ 282463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto); 2831da177e4SLinus Torvalds } else { 2841da177e4SLinus Torvalds /* Delayed ACK missed: leave pingpong mode and 2851da177e4SLinus Torvalds * deflate ATO. 2861da177e4SLinus Torvalds */ 28731954cd8SWei Wang inet_csk_exit_pingpong_mode(sk); 288463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = TCP_ATO_MIN; 2891da177e4SLinus Torvalds } 2904688eb7cSEric Dumazet tcp_mstamp_refresh(tcp_sk(sk)); 2911da177e4SLinus Torvalds tcp_send_ack(sk); 29202a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS); 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds out: 296b8da51ebSEric Dumazet if (tcp_under_memory_pressure(sk)) 2973ab224beSHideo Aoki sk_mem_reclaim(sk); 2986f458dfbSEric Dumazet } 2996f458dfbSEric Dumazet 300c380d37eSRichard Sailer 301c380d37eSRichard Sailer /** 302c380d37eSRichard Sailer * tcp_delack_timer() - The TCP delayed ACK timeout handler 303c380d37eSRichard Sailer * @data: Pointer to the current socket. (gets casted to struct sock *) 304c380d37eSRichard Sailer * 305c380d37eSRichard Sailer * This function gets (indirectly) called when the kernel timer for a TCP packet 306c380d37eSRichard Sailer * of this socket expires. Calls tcp_delack_timer_handler() to do the actual work. 307c380d37eSRichard Sailer * 308c380d37eSRichard Sailer * Returns: Nothing (void) 309c380d37eSRichard Sailer */ 31059f379f9SKees Cook static void tcp_delack_timer(struct timer_list *t) 3116f458dfbSEric Dumazet { 31259f379f9SKees Cook struct inet_connection_sock *icsk = 31359f379f9SKees Cook from_timer(icsk, t, icsk_delack_timer); 31459f379f9SKees Cook struct sock *sk = &icsk->icsk_inet.sk; 3156f458dfbSEric Dumazet 3166f458dfbSEric Dumazet bh_lock_sock(sk); 3176f458dfbSEric Dumazet if (!sock_owned_by_user(sk)) { 3186f458dfbSEric Dumazet tcp_delack_timer_handler(sk); 3196f458dfbSEric Dumazet } else { 32059f379f9SKees Cook icsk->icsk_ack.blocked = 1; 32102a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED); 3226f458dfbSEric Dumazet /* deleguate our work to tcp_release_cb() */ 3237aa5470cSEric Dumazet if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &sk->sk_tsq_flags)) 324144d56e9SEric Dumazet sock_hold(sk); 3256f458dfbSEric Dumazet } 3261da177e4SLinus Torvalds bh_unlock_sock(sk); 3271da177e4SLinus Torvalds sock_put(sk); 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds static void tcp_probe_timer(struct sock *sk) 3311da177e4SLinus Torvalds { 3326687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 33375c119afSEric Dumazet struct sk_buff *skb = tcp_send_head(sk); 3341da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 3351da177e4SLinus Torvalds int max_probes; 3361da177e4SLinus Torvalds 33775c119afSEric Dumazet if (tp->packets_out || !skb) { 3386687e988SArnaldo Carvalho de Melo icsk->icsk_probes_out = 0; 3391da177e4SLinus Torvalds return; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 342b248230cSYuchung Cheng /* RFC 1122 4.2.2.17 requires the sender to stay open indefinitely as 343b248230cSYuchung Cheng * long as the receiver continues to respond probes. We support this by 344b248230cSYuchung Cheng * default and reset icsk_probes_out with incoming ACKs. But if the 345b248230cSYuchung Cheng * socket is orphaned or the user specifies TCP_USER_TIMEOUT, we 346b248230cSYuchung Cheng * kill the socket when the retry count and the time exceeds the 347b248230cSYuchung Cheng * corresponding system limit. We also implement similar policy when 348b248230cSYuchung Cheng * we use RTO to probe window in tcp_retransmit_timer(). 3491da177e4SLinus Torvalds */ 3509721e709SYuchung Cheng if (icsk->icsk_user_timeout) { 3519721e709SYuchung Cheng u32 elapsed = tcp_model_timeout(sk, icsk->icsk_probes_out, 3529721e709SYuchung Cheng tcp_probe0_base(sk)); 3539721e709SYuchung Cheng 3549721e709SYuchung Cheng if (elapsed >= icsk->icsk_user_timeout) 355b248230cSYuchung Cheng goto abort; 3569721e709SYuchung Cheng } 3571da177e4SLinus Torvalds 358c6214a97SNikolay Borisov max_probes = sock_net(sk)->ipv4.sysctl_tcp_retries2; 3591da177e4SLinus Torvalds if (sock_flag(sk, SOCK_DEAD)) { 3607533ce30SRichard Sailer const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX; 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds max_probes = tcp_orphan_retries(sk, alive); 363b248230cSYuchung Cheng if (!alive && icsk->icsk_backoff >= max_probes) 364b248230cSYuchung Cheng goto abort; 365b248230cSYuchung Cheng if (tcp_out_of_resources(sk, true)) 3661da177e4SLinus Torvalds return; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 3693976535aSYuchung Cheng if (icsk->icsk_probes_out >= max_probes) { 370b248230cSYuchung Cheng abort: tcp_write_err(sk); 3711da177e4SLinus Torvalds } else { 3721da177e4SLinus Torvalds /* Only send another probe if we didn't close things up. */ 3731da177e4SLinus Torvalds tcp_send_probe0(sk); 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds /* 3788336886fSJerry Chu * Timer for Fast Open socket to retransmit SYNACK. Note that the 3798336886fSJerry Chu * sk here is the child socket, not the parent (listener) socket. 3808336886fSJerry Chu */ 3818336886fSJerry Chu static void tcp_fastopen_synack_timer(struct sock *sk) 3828336886fSJerry Chu { 3838336886fSJerry Chu struct inet_connection_sock *icsk = inet_csk(sk); 3848336886fSJerry Chu int max_retries = icsk->icsk_syn_retries ? : 3857c083ecbSNikolay Borisov sock_net(sk)->ipv4.sysctl_tcp_synack_retries + 1; /* add one more retry for fastopen */ 386c7d13c8fSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 3878336886fSJerry Chu struct request_sock *req; 3888336886fSJerry Chu 3898336886fSJerry Chu req = tcp_sk(sk)->fastopen_rsk; 39042cb80a2SEric Dumazet req->rsk_ops->syn_ack_timeout(req); 3918336886fSJerry Chu 392e6c022a4SEric Dumazet if (req->num_timeout >= max_retries) { 3938336886fSJerry Chu tcp_write_err(sk); 3948336886fSJerry Chu return; 3958336886fSJerry Chu } 396*8c3cfe19SYuchung Cheng /* Lower cwnd after certain SYNACK timeout like tcp_init_transfer() */ 397*8c3cfe19SYuchung Cheng if (icsk->icsk_retransmits == 1) 398*8c3cfe19SYuchung Cheng tcp_enter_loss(sk); 3998336886fSJerry Chu /* XXX (TFO) - Unlike regular SYN-ACK retransmit, we ignore error 4008336886fSJerry Chu * returned from rtx_syn_ack() to make it more persistent like 4018336886fSJerry Chu * regular retransmit because if the child socket has been accepted 4028336886fSJerry Chu * it's not good to give up too easily. 4038336886fSJerry Chu */ 404e6c022a4SEric Dumazet inet_rtx_syn_ack(sk, req); 405e6c022a4SEric Dumazet req->num_timeout++; 4067e32b443SYuchung Cheng icsk->icsk_retransmits++; 407c7d13c8fSYuchung Cheng if (!tp->retrans_stamp) 408c7d13c8fSYuchung Cheng tp->retrans_stamp = tcp_time_stamp(tp); 4098336886fSJerry Chu inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 410e6c022a4SEric Dumazet TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX); 4118336886fSJerry Chu } 4128336886fSJerry Chu 4131da177e4SLinus Torvalds 414c380d37eSRichard Sailer /** 415c380d37eSRichard Sailer * tcp_retransmit_timer() - The TCP retransmit timeout handler 416c380d37eSRichard Sailer * @sk: Pointer to the current socket. 417c380d37eSRichard Sailer * 418c380d37eSRichard Sailer * This function gets called when the kernel timer for a TCP packet 419c380d37eSRichard Sailer * of this socket expires. 420c380d37eSRichard Sailer * 421c380d37eSRichard Sailer * It handles retransmission, timer adjustment and other necesarry measures. 422c380d37eSRichard Sailer * 423c380d37eSRichard Sailer * Returns: Nothing (void) 424c380d37eSRichard Sailer */ 425f1ecd5d9SDamian Lukowski void tcp_retransmit_timer(struct sock *sk) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 428ae5c3f40SNikolay Borisov struct net *net = sock_net(sk); 429463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 4301da177e4SLinus Torvalds 4318336886fSJerry Chu if (tp->fastopen_rsk) { 43237561f68SJerry Chu WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV && 4338336886fSJerry Chu sk->sk_state != TCP_FIN_WAIT1); 4348336886fSJerry Chu tcp_fastopen_synack_timer(sk); 4358336886fSJerry Chu /* Before we receive ACK to our SYN-ACK don't retransmit 4368336886fSJerry Chu * anything else (e.g., data or FIN segments). 4378336886fSJerry Chu */ 4388336886fSJerry Chu return; 4398336886fSJerry Chu } 44088f8598dSYuchung Cheng if (!tp->packets_out || WARN_ON_ONCE(tcp_rtx_queue_empty(sk))) 44188f8598dSYuchung Cheng return; 4421da177e4SLinus Torvalds 4439b717a8dSNandita Dukkipati tp->tlp_high_seq = 0; 4449b717a8dSNandita Dukkipati 4451da177e4SLinus Torvalds if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) && 4461da177e4SLinus Torvalds !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) { 4471da177e4SLinus Torvalds /* Receiver dastardly shrinks window. Our retransmits 4481da177e4SLinus Torvalds * become zero probes, but we should not timeout this 4491da177e4SLinus Torvalds * connection. If the socket is an orphan, time it out, 4501da177e4SLinus Torvalds * we cannot allow such beasts to hang infinitely. 4511da177e4SLinus Torvalds */ 4521da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 453569508c9SYOSHIFUJI Hideaki if (sk->sk_family == AF_INET) { 454ba7a46f1SJoe Perches net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n", 455afd46503SJoe Perches &inet->inet_daddr, 456ba7a46f1SJoe Perches ntohs(inet->inet_dport), 457ba7a46f1SJoe Perches inet->inet_num, 458afd46503SJoe Perches tp->snd_una, tp->snd_nxt); 4591da177e4SLinus Torvalds } 460dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 461569508c9SYOSHIFUJI Hideaki else if (sk->sk_family == AF_INET6) { 462ba7a46f1SJoe Perches net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n", 463efe4208fSEric Dumazet &sk->sk_v6_daddr, 464ba7a46f1SJoe Perches ntohs(inet->inet_dport), 465ba7a46f1SJoe Perches inet->inet_num, 466afd46503SJoe Perches tp->snd_una, tp->snd_nxt); 467569508c9SYOSHIFUJI Hideaki } 468569508c9SYOSHIFUJI Hideaki #endif 46970eabf0eSEric Dumazet if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) { 4701da177e4SLinus Torvalds tcp_write_err(sk); 4711da177e4SLinus Torvalds goto out; 4721da177e4SLinus Torvalds } 4735ae344c9SNeal Cardwell tcp_enter_loss(sk); 47475c119afSEric Dumazet tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1); 4751da177e4SLinus Torvalds __sk_dst_reset(sk); 4761da177e4SLinus Torvalds goto out_reset_timer; 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds 479e1561fe2SYuchung Cheng __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPTIMEOUTS); 4801da177e4SLinus Torvalds if (tcp_write_timeout(sk)) 4811da177e4SLinus Torvalds goto out; 4821da177e4SLinus Torvalds 483463c84b9SArnaldo Carvalho de Melo if (icsk->icsk_retransmits == 0) { 484e1561fe2SYuchung Cheng int mib_idx = 0; 48540b215e5SPavel Emelyanov 486c60ce4e2SIlpo Järvinen if (icsk->icsk_ca_state == TCP_CA_Recovery) { 487bc079e9eSIlpo Järvinen if (tcp_is_sack(tp)) 488bc079e9eSIlpo Järvinen mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL; 489bc079e9eSIlpo Järvinen else 490bc079e9eSIlpo Järvinen mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL; 4916687e988SArnaldo Carvalho de Melo } else if (icsk->icsk_ca_state == TCP_CA_Loss) { 49240b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPLOSSFAILURES; 493c60ce4e2SIlpo Järvinen } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) || 494c60ce4e2SIlpo Järvinen tp->sacked_out) { 495c60ce4e2SIlpo Järvinen if (tcp_is_sack(tp)) 496c60ce4e2SIlpo Järvinen mib_idx = LINUX_MIB_TCPSACKFAILURES; 497c60ce4e2SIlpo Järvinen else 498c60ce4e2SIlpo Järvinen mib_idx = LINUX_MIB_TCPRENOFAILURES; 4991da177e4SLinus Torvalds } 500e1561fe2SYuchung Cheng if (mib_idx) 50102a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), mib_idx); 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 5045ae344c9SNeal Cardwell tcp_enter_loss(sk); 5051da177e4SLinus Torvalds 506590d2026SYuchung Cheng icsk->icsk_retransmits++; 50775c119afSEric Dumazet if (tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1) > 0) { 5081da177e4SLinus Torvalds /* Retransmission failed because of local congestion, 509590d2026SYuchung Cheng * Let senders fight for local resources conservatively. 5101da177e4SLinus Torvalds */ 511463c84b9SArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 512590d2026SYuchung Cheng TCP_RESOURCE_PROBE_INTERVAL, 5133f421baaSArnaldo Carvalho de Melo TCP_RTO_MAX); 5141da177e4SLinus Torvalds goto out; 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* Increase the timeout each time we retransmit. Note that 5181da177e4SLinus Torvalds * we do not increase the rtt estimate. rto is initialized 5191da177e4SLinus Torvalds * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests 5201da177e4SLinus Torvalds * that doubling rto each time is the least we can get away with. 5211da177e4SLinus Torvalds * In KA9Q, Karn uses this for the first few times, and then 5221da177e4SLinus Torvalds * goes to quadratic. netBSD doubles, but only goes up to *64, 5231da177e4SLinus Torvalds * and clamps at 1 to 64 sec afterwards. Note that 120 sec is 5241da177e4SLinus Torvalds * defined in the protocol as the maximum possible RTT. I guess 5251da177e4SLinus Torvalds * we'll have to use something other than TCP to talk to the 5261da177e4SLinus Torvalds * University of Mars. 5271da177e4SLinus Torvalds * 5281da177e4SLinus Torvalds * PAWS allows us longer timeouts and large windows, so once 5291da177e4SLinus Torvalds * implemented ftp to mars will work nicely. We will have to fix 5301da177e4SLinus Torvalds * the 120 second clamps though! 5311da177e4SLinus Torvalds */ 532463c84b9SArnaldo Carvalho de Melo icsk->icsk_backoff++; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds out_reset_timer: 53536e31b0aSAndreas Petlund /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is 53636e31b0aSAndreas Petlund * used to reset timer, set to 0. Recalculate 'icsk_rto' as this 53736e31b0aSAndreas Petlund * might be increased if the stream oscillates between thin and thick, 53836e31b0aSAndreas Petlund * thus the old value might already be too high compared to the value 53936e31b0aSAndreas Petlund * set by 'tcp_set_rto' in tcp_input.c which resets the rto without 54036e31b0aSAndreas Petlund * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating 54136e31b0aSAndreas Petlund * exponential backoff behaviour to avoid continue hammering 54236e31b0aSAndreas Petlund * linear-timeout retransmissions into a black hole 54336e31b0aSAndreas Petlund */ 54436e31b0aSAndreas Petlund if (sk->sk_state == TCP_ESTABLISHED && 5452c04ac8aSEric Dumazet (tp->thin_lto || net->ipv4.sysctl_tcp_thin_linear_timeouts) && 54636e31b0aSAndreas Petlund tcp_stream_is_thin(tp) && 54736e31b0aSAndreas Petlund icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) { 54836e31b0aSAndreas Petlund icsk->icsk_backoff = 0; 54936e31b0aSAndreas Petlund icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX); 55036e31b0aSAndreas Petlund } else { 55136e31b0aSAndreas Petlund /* Use normal (exponential) backoff */ 552463c84b9SArnaldo Carvalho de Melo icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX); 55336e31b0aSAndreas Petlund } 554b701a99eSJon Maxwell inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 555b701a99eSJon Maxwell tcp_clamp_rto_to_user_timeout(sk), TCP_RTO_MAX); 556ce682ef6SEric Dumazet if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1 + 1, 0)) 5571da177e4SLinus Torvalds __sk_dst_reset(sk); 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds out:; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 562c380d37eSRichard Sailer /* Called with bottom-half processing disabled. 563c380d37eSRichard Sailer Called by tcp_write_timer() */ 5646f458dfbSEric Dumazet void tcp_write_timer_handler(struct sock *sk) 5651da177e4SLinus Torvalds { 566463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 5671da177e4SLinus Torvalds int event; 5681da177e4SLinus Torvalds 56902b2faafSEric Dumazet if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) || 57002b2faafSEric Dumazet !icsk->icsk_pending) 5711da177e4SLinus Torvalds goto out; 5721da177e4SLinus Torvalds 573463c84b9SArnaldo Carvalho de Melo if (time_after(icsk->icsk_timeout, jiffies)) { 574463c84b9SArnaldo Carvalho de Melo sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout); 5751da177e4SLinus Torvalds goto out; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5789a568de4SEric Dumazet tcp_mstamp_refresh(tcp_sk(sk)); 579463c84b9SArnaldo Carvalho de Melo event = icsk->icsk_pending; 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds switch (event) { 58257dde7f7SYuchung Cheng case ICSK_TIME_REO_TIMEOUT: 58357dde7f7SYuchung Cheng tcp_rack_reo_timeout(sk); 58457dde7f7SYuchung Cheng break; 5856ba8a3b1SNandita Dukkipati case ICSK_TIME_LOSS_PROBE: 5866ba8a3b1SNandita Dukkipati tcp_send_loss_probe(sk); 5876ba8a3b1SNandita Dukkipati break; 588463c84b9SArnaldo Carvalho de Melo case ICSK_TIME_RETRANS: 5896ba8a3b1SNandita Dukkipati icsk->icsk_pending = 0; 5901da177e4SLinus Torvalds tcp_retransmit_timer(sk); 5911da177e4SLinus Torvalds break; 592463c84b9SArnaldo Carvalho de Melo case ICSK_TIME_PROBE0: 5936ba8a3b1SNandita Dukkipati icsk->icsk_pending = 0; 5941da177e4SLinus Torvalds tcp_probe_timer(sk); 5951da177e4SLinus Torvalds break; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds out: 5993ab224beSHideo Aoki sk_mem_reclaim(sk); 6006f458dfbSEric Dumazet } 6016f458dfbSEric Dumazet 60259f379f9SKees Cook static void tcp_write_timer(struct timer_list *t) 6036f458dfbSEric Dumazet { 60459f379f9SKees Cook struct inet_connection_sock *icsk = 60559f379f9SKees Cook from_timer(icsk, t, icsk_retransmit_timer); 60659f379f9SKees Cook struct sock *sk = &icsk->icsk_inet.sk; 6076f458dfbSEric Dumazet 6086f458dfbSEric Dumazet bh_lock_sock(sk); 6096f458dfbSEric Dumazet if (!sock_owned_by_user(sk)) { 6106f458dfbSEric Dumazet tcp_write_timer_handler(sk); 6116f458dfbSEric Dumazet } else { 612c380d37eSRichard Sailer /* delegate our work to tcp_release_cb() */ 6137aa5470cSEric Dumazet if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &sk->sk_tsq_flags)) 614144d56e9SEric Dumazet sock_hold(sk); 6156f458dfbSEric Dumazet } 6161da177e4SLinus Torvalds bh_unlock_sock(sk); 6171da177e4SLinus Torvalds sock_put(sk); 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds 62042cb80a2SEric Dumazet void tcp_syn_ack_timeout(const struct request_sock *req) 62172659eccSOctavian Purdila { 62242cb80a2SEric Dumazet struct net *net = read_pnet(&inet_rsk(req)->ireq_net); 62342cb80a2SEric Dumazet 62402a1d6e7SEric Dumazet __NET_INC_STATS(net, LINUX_MIB_TCPTIMEOUTS); 62572659eccSOctavian Purdila } 62672659eccSOctavian Purdila EXPORT_SYMBOL(tcp_syn_ack_timeout); 62772659eccSOctavian Purdila 6281da177e4SLinus Torvalds void tcp_set_keepalive(struct sock *sk, int val) 6291da177e4SLinus Torvalds { 6301da177e4SLinus Torvalds if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) 6311da177e4SLinus Torvalds return; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds if (val && !sock_flag(sk, SOCK_KEEPOPEN)) 634463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk))); 6351da177e4SLinus Torvalds else if (!val) 636463c84b9SArnaldo Carvalho de Melo inet_csk_delete_keepalive_timer(sk); 6371da177e4SLinus Torvalds } 6384b9d07a4SUrsula Braun EXPORT_SYMBOL_GPL(tcp_set_keepalive); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds 64159f379f9SKees Cook static void tcp_keepalive_timer (struct timer_list *t) 6421da177e4SLinus Torvalds { 64359f379f9SKees Cook struct sock *sk = from_timer(sk, t, sk_timer); 6446687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 6451da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 6466c37e5deSFlavio Leitner u32 elapsed; 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds /* Only process if socket is not in use. */ 6491da177e4SLinus Torvalds bh_lock_sock(sk); 6501da177e4SLinus Torvalds if (sock_owned_by_user(sk)) { 6511da177e4SLinus Torvalds /* Try again later. */ 652463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer (sk, HZ/20); 6531da177e4SLinus Torvalds goto out; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds if (sk->sk_state == TCP_LISTEN) { 657fa76ce73SEric Dumazet pr_err("Hmm... keepalive on a LISTEN ???\n"); 6581da177e4SLinus Torvalds goto out; 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds 6614688eb7cSEric Dumazet tcp_mstamp_refresh(tp); 6621da177e4SLinus Torvalds if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) { 6631da177e4SLinus Torvalds if (tp->linger2 >= 0) { 664463c84b9SArnaldo Carvalho de Melo const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds if (tmo > 0) { 6671da177e4SLinus Torvalds tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); 6681da177e4SLinus Torvalds goto out; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds tcp_send_active_reset(sk, GFP_ATOMIC); 6721da177e4SLinus Torvalds goto death; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6752dda6400SEric Dumazet if (!sock_flag(sk, SOCK_KEEPOPEN) || 6762dda6400SEric Dumazet ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT))) 6771da177e4SLinus Torvalds goto out; 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds elapsed = keepalive_time_when(tp); 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds /* It is alive without keepalive 8) */ 68275c119afSEric Dumazet if (tp->packets_out || !tcp_write_queue_empty(sk)) 6831da177e4SLinus Torvalds goto resched; 6841da177e4SLinus Torvalds 6856c37e5deSFlavio Leitner elapsed = keepalive_time_elapsed(tp); 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds if (elapsed >= keepalive_time_when(tp)) { 688dca43c75SJerry Chu /* If the TCP_USER_TIMEOUT option is enabled, use that 689dca43c75SJerry Chu * to determine when to timeout instead. 690dca43c75SJerry Chu */ 691dca43c75SJerry Chu if ((icsk->icsk_user_timeout != 0 && 6929bcc66e1SJon Maxwell elapsed >= msecs_to_jiffies(icsk->icsk_user_timeout) && 693dca43c75SJerry Chu icsk->icsk_probes_out > 0) || 694dca43c75SJerry Chu (icsk->icsk_user_timeout == 0 && 695dca43c75SJerry Chu icsk->icsk_probes_out >= keepalive_probes(tp))) { 6961da177e4SLinus Torvalds tcp_send_active_reset(sk, GFP_ATOMIC); 6971da177e4SLinus Torvalds tcp_write_err(sk); 6981da177e4SLinus Torvalds goto out; 6991da177e4SLinus Torvalds } 700e520af48SEric Dumazet if (tcp_write_wakeup(sk, LINUX_MIB_TCPKEEPALIVE) <= 0) { 7016687e988SArnaldo Carvalho de Melo icsk->icsk_probes_out++; 7021da177e4SLinus Torvalds elapsed = keepalive_intvl_when(tp); 7031da177e4SLinus Torvalds } else { 7041da177e4SLinus Torvalds /* If keepalive was lost due to local congestion, 7051da177e4SLinus Torvalds * try harder. 7061da177e4SLinus Torvalds */ 7071da177e4SLinus Torvalds elapsed = TCP_RESOURCE_PROBE_INTERVAL; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds } else { 7101da177e4SLinus Torvalds /* It is tp->rcv_tstamp + keepalive_time_when(tp) */ 7111da177e4SLinus Torvalds elapsed = keepalive_time_when(tp) - elapsed; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7143ab224beSHideo Aoki sk_mem_reclaim(sk); 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds resched: 717463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer (sk, elapsed); 7181da177e4SLinus Torvalds goto out; 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds death: 7211da177e4SLinus Torvalds tcp_done(sk); 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds out: 7241da177e4SLinus Torvalds bh_unlock_sock(sk); 7251da177e4SLinus Torvalds sock_put(sk); 7261da177e4SLinus Torvalds } 7276f458dfbSEric Dumazet 7285d9f4262SEric Dumazet static enum hrtimer_restart tcp_compressed_ack_kick(struct hrtimer *timer) 7295d9f4262SEric Dumazet { 7305d9f4262SEric Dumazet struct tcp_sock *tp = container_of(timer, struct tcp_sock, compressed_ack_timer); 7315d9f4262SEric Dumazet struct sock *sk = (struct sock *)tp; 7325d9f4262SEric Dumazet 7335d9f4262SEric Dumazet bh_lock_sock(sk); 7345d9f4262SEric Dumazet if (!sock_owned_by_user(sk)) { 73586de5921SEric Dumazet if (tp->compressed_ack > TCP_FASTRETRANS_THRESH) 7365d9f4262SEric Dumazet tcp_send_ack(sk); 7375d9f4262SEric Dumazet } else { 7385d9f4262SEric Dumazet if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, 7395d9f4262SEric Dumazet &sk->sk_tsq_flags)) 7405d9f4262SEric Dumazet sock_hold(sk); 7415d9f4262SEric Dumazet } 7425d9f4262SEric Dumazet bh_unlock_sock(sk); 7435d9f4262SEric Dumazet 7445d9f4262SEric Dumazet sock_put(sk); 7455d9f4262SEric Dumazet 7465d9f4262SEric Dumazet return HRTIMER_NORESTART; 7475d9f4262SEric Dumazet } 7485d9f4262SEric Dumazet 7496f458dfbSEric Dumazet void tcp_init_xmit_timers(struct sock *sk) 7506f458dfbSEric Dumazet { 7516f458dfbSEric Dumazet inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer, 7526f458dfbSEric Dumazet &tcp_keepalive_timer); 753fb420d5dSEric Dumazet hrtimer_init(&tcp_sk(sk)->pacing_timer, CLOCK_MONOTONIC, 75473a6bab5SEric Dumazet HRTIMER_MODE_ABS_PINNED_SOFT); 755218af599SEric Dumazet tcp_sk(sk)->pacing_timer.function = tcp_pace_kick; 7565d9f4262SEric Dumazet 7575d9f4262SEric Dumazet hrtimer_init(&tcp_sk(sk)->compressed_ack_timer, CLOCK_MONOTONIC, 7585d9f4262SEric Dumazet HRTIMER_MODE_REL_PINNED_SOFT); 7595d9f4262SEric Dumazet tcp_sk(sk)->compressed_ack_timer.function = tcp_compressed_ack_kick; 7606f458dfbSEric Dumazet } 761