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 162*01a523b0SYuchung Cheng static unsigned int tcp_model_timeout(struct sock *sk, 163*01a523b0SYuchung Cheng unsigned int boundary, 164*01a523b0SYuchung Cheng unsigned int rto_base) 165*01a523b0SYuchung Cheng { 166*01a523b0SYuchung Cheng unsigned int linear_backoff_thresh, timeout; 167c380d37eSRichard Sailer 168*01a523b0SYuchung Cheng linear_backoff_thresh = ilog2(TCP_RTO_MAX / rto_base); 169*01a523b0SYuchung Cheng if (boundary <= linear_backoff_thresh) 170*01a523b0SYuchung Cheng timeout = ((2 << boundary) - 1) * rto_base; 171*01a523b0SYuchung Cheng else 172*01a523b0SYuchung Cheng timeout = ((2 << linear_backoff_thresh) - 1) * rto_base + 173*01a523b0SYuchung Cheng (boundary - linear_backoff_thresh) * TCP_RTO_MAX; 174*01a523b0SYuchung Cheng return jiffies_to_msecs(timeout); 175*01a523b0SYuchung 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 { 193*01a523b0SYuchung 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; 199*01a523b0SYuchung Cheng if (likely(timeout == 0)) 200*01a523b0SYuchung 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)) { 280463c84b9SArnaldo Carvalho de Melo if (!icsk->icsk_ack.pingpong) { 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 */ 287463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.pingpong = 0; 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; 336b248230cSYuchung Cheng u32 start_ts; 3371da177e4SLinus Torvalds 33875c119afSEric Dumazet if (tp->packets_out || !skb) { 3396687e988SArnaldo Carvalho de Melo icsk->icsk_probes_out = 0; 3401da177e4SLinus Torvalds return; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 343b248230cSYuchung Cheng /* RFC 1122 4.2.2.17 requires the sender to stay open indefinitely as 344b248230cSYuchung Cheng * long as the receiver continues to respond probes. We support this by 345b248230cSYuchung Cheng * default and reset icsk_probes_out with incoming ACKs. But if the 346b248230cSYuchung Cheng * socket is orphaned or the user specifies TCP_USER_TIMEOUT, we 347b248230cSYuchung Cheng * kill the socket when the retry count and the time exceeds the 348b248230cSYuchung Cheng * corresponding system limit. We also implement similar policy when 349b248230cSYuchung Cheng * we use RTO to probe window in tcp_retransmit_timer(). 3501da177e4SLinus Torvalds */ 35175c119afSEric Dumazet start_ts = tcp_skb_timestamp(skb); 352b248230cSYuchung Cheng if (!start_ts) 3535f6188a8SEric Dumazet skb->skb_mstamp_ns = tp->tcp_clock_cache; 354b248230cSYuchung Cheng else if (icsk->icsk_user_timeout && 3559bcc66e1SJon Maxwell (s32)(tcp_time_stamp(tp) - start_ts) > icsk->icsk_user_timeout) 356b248230cSYuchung Cheng goto abort; 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 } 3968336886fSJerry Chu /* XXX (TFO) - Unlike regular SYN-ACK retransmit, we ignore error 3978336886fSJerry Chu * returned from rtx_syn_ack() to make it more persistent like 3988336886fSJerry Chu * regular retransmit because if the child socket has been accepted 3998336886fSJerry Chu * it's not good to give up too easily. 4008336886fSJerry Chu */ 401e6c022a4SEric Dumazet inet_rtx_syn_ack(sk, req); 402e6c022a4SEric Dumazet req->num_timeout++; 4037e32b443SYuchung Cheng icsk->icsk_retransmits++; 404c7d13c8fSYuchung Cheng if (!tp->retrans_stamp) 405c7d13c8fSYuchung Cheng tp->retrans_stamp = tcp_time_stamp(tp); 4068336886fSJerry Chu inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 407e6c022a4SEric Dumazet TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX); 4088336886fSJerry Chu } 4098336886fSJerry Chu 4101da177e4SLinus Torvalds 411c380d37eSRichard Sailer /** 412c380d37eSRichard Sailer * tcp_retransmit_timer() - The TCP retransmit timeout handler 413c380d37eSRichard Sailer * @sk: Pointer to the current socket. 414c380d37eSRichard Sailer * 415c380d37eSRichard Sailer * This function gets called when the kernel timer for a TCP packet 416c380d37eSRichard Sailer * of this socket expires. 417c380d37eSRichard Sailer * 418c380d37eSRichard Sailer * It handles retransmission, timer adjustment and other necesarry measures. 419c380d37eSRichard Sailer * 420c380d37eSRichard Sailer * Returns: Nothing (void) 421c380d37eSRichard Sailer */ 422f1ecd5d9SDamian Lukowski void tcp_retransmit_timer(struct sock *sk) 4231da177e4SLinus Torvalds { 4241da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 425ae5c3f40SNikolay Borisov struct net *net = sock_net(sk); 426463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 4271da177e4SLinus Torvalds 4288336886fSJerry Chu if (tp->fastopen_rsk) { 42937561f68SJerry Chu WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV && 4308336886fSJerry Chu sk->sk_state != TCP_FIN_WAIT1); 4318336886fSJerry Chu tcp_fastopen_synack_timer(sk); 4328336886fSJerry Chu /* Before we receive ACK to our SYN-ACK don't retransmit 4338336886fSJerry Chu * anything else (e.g., data or FIN segments). 4348336886fSJerry Chu */ 4358336886fSJerry Chu return; 4368336886fSJerry Chu } 43788f8598dSYuchung Cheng if (!tp->packets_out || WARN_ON_ONCE(tcp_rtx_queue_empty(sk))) 43888f8598dSYuchung Cheng return; 4391da177e4SLinus Torvalds 4409b717a8dSNandita Dukkipati tp->tlp_high_seq = 0; 4419b717a8dSNandita Dukkipati 4421da177e4SLinus Torvalds if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) && 4431da177e4SLinus Torvalds !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) { 4441da177e4SLinus Torvalds /* Receiver dastardly shrinks window. Our retransmits 4451da177e4SLinus Torvalds * become zero probes, but we should not timeout this 4461da177e4SLinus Torvalds * connection. If the socket is an orphan, time it out, 4471da177e4SLinus Torvalds * we cannot allow such beasts to hang infinitely. 4481da177e4SLinus Torvalds */ 4491da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 450569508c9SYOSHIFUJI Hideaki if (sk->sk_family == AF_INET) { 451ba7a46f1SJoe Perches net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n", 452afd46503SJoe Perches &inet->inet_daddr, 453ba7a46f1SJoe Perches ntohs(inet->inet_dport), 454ba7a46f1SJoe Perches inet->inet_num, 455afd46503SJoe Perches tp->snd_una, tp->snd_nxt); 4561da177e4SLinus Torvalds } 457dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 458569508c9SYOSHIFUJI Hideaki else if (sk->sk_family == AF_INET6) { 459ba7a46f1SJoe Perches net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n", 460efe4208fSEric Dumazet &sk->sk_v6_daddr, 461ba7a46f1SJoe Perches ntohs(inet->inet_dport), 462ba7a46f1SJoe Perches inet->inet_num, 463afd46503SJoe Perches tp->snd_una, tp->snd_nxt); 464569508c9SYOSHIFUJI Hideaki } 465569508c9SYOSHIFUJI Hideaki #endif 46670eabf0eSEric Dumazet if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) { 4671da177e4SLinus Torvalds tcp_write_err(sk); 4681da177e4SLinus Torvalds goto out; 4691da177e4SLinus Torvalds } 4705ae344c9SNeal Cardwell tcp_enter_loss(sk); 47175c119afSEric Dumazet tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1); 4721da177e4SLinus Torvalds __sk_dst_reset(sk); 4731da177e4SLinus Torvalds goto out_reset_timer; 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 476e1561fe2SYuchung Cheng __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPTIMEOUTS); 4771da177e4SLinus Torvalds if (tcp_write_timeout(sk)) 4781da177e4SLinus Torvalds goto out; 4791da177e4SLinus Torvalds 480463c84b9SArnaldo Carvalho de Melo if (icsk->icsk_retransmits == 0) { 481e1561fe2SYuchung Cheng int mib_idx = 0; 48240b215e5SPavel Emelyanov 483c60ce4e2SIlpo Järvinen if (icsk->icsk_ca_state == TCP_CA_Recovery) { 484bc079e9eSIlpo Järvinen if (tcp_is_sack(tp)) 485bc079e9eSIlpo Järvinen mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL; 486bc079e9eSIlpo Järvinen else 487bc079e9eSIlpo Järvinen mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL; 4886687e988SArnaldo Carvalho de Melo } else if (icsk->icsk_ca_state == TCP_CA_Loss) { 48940b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPLOSSFAILURES; 490c60ce4e2SIlpo Järvinen } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) || 491c60ce4e2SIlpo Järvinen tp->sacked_out) { 492c60ce4e2SIlpo Järvinen if (tcp_is_sack(tp)) 493c60ce4e2SIlpo Järvinen mib_idx = LINUX_MIB_TCPSACKFAILURES; 494c60ce4e2SIlpo Järvinen else 495c60ce4e2SIlpo Järvinen mib_idx = LINUX_MIB_TCPRENOFAILURES; 4961da177e4SLinus Torvalds } 497e1561fe2SYuchung Cheng if (mib_idx) 49802a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), mib_idx); 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds 5015ae344c9SNeal Cardwell tcp_enter_loss(sk); 5021da177e4SLinus Torvalds 50375c119afSEric Dumazet if (tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1) > 0) { 5041da177e4SLinus Torvalds /* Retransmission failed because of local congestion, 5051da177e4SLinus Torvalds * do not backoff. 5061da177e4SLinus Torvalds */ 507463c84b9SArnaldo Carvalho de Melo if (!icsk->icsk_retransmits) 508463c84b9SArnaldo Carvalho de Melo icsk->icsk_retransmits = 1; 509463c84b9SArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 5103f421baaSArnaldo Carvalho de Melo min(icsk->icsk_rto, TCP_RESOURCE_PROBE_INTERVAL), 5113f421baaSArnaldo Carvalho de Melo TCP_RTO_MAX); 5121da177e4SLinus Torvalds goto out; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /* Increase the timeout each time we retransmit. Note that 5161da177e4SLinus Torvalds * we do not increase the rtt estimate. rto is initialized 5171da177e4SLinus Torvalds * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests 5181da177e4SLinus Torvalds * that doubling rto each time is the least we can get away with. 5191da177e4SLinus Torvalds * In KA9Q, Karn uses this for the first few times, and then 5201da177e4SLinus Torvalds * goes to quadratic. netBSD doubles, but only goes up to *64, 5211da177e4SLinus Torvalds * and clamps at 1 to 64 sec afterwards. Note that 120 sec is 5221da177e4SLinus Torvalds * defined in the protocol as the maximum possible RTT. I guess 5231da177e4SLinus Torvalds * we'll have to use something other than TCP to talk to the 5241da177e4SLinus Torvalds * University of Mars. 5251da177e4SLinus Torvalds * 5261da177e4SLinus Torvalds * PAWS allows us longer timeouts and large windows, so once 5271da177e4SLinus Torvalds * implemented ftp to mars will work nicely. We will have to fix 5281da177e4SLinus Torvalds * the 120 second clamps though! 5291da177e4SLinus Torvalds */ 530463c84b9SArnaldo Carvalho de Melo icsk->icsk_backoff++; 531463c84b9SArnaldo Carvalho de Melo icsk->icsk_retransmits++; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds out_reset_timer: 53436e31b0aSAndreas Petlund /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is 53536e31b0aSAndreas Petlund * used to reset timer, set to 0. Recalculate 'icsk_rto' as this 53636e31b0aSAndreas Petlund * might be increased if the stream oscillates between thin and thick, 53736e31b0aSAndreas Petlund * thus the old value might already be too high compared to the value 53836e31b0aSAndreas Petlund * set by 'tcp_set_rto' in tcp_input.c which resets the rto without 53936e31b0aSAndreas Petlund * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating 54036e31b0aSAndreas Petlund * exponential backoff behaviour to avoid continue hammering 54136e31b0aSAndreas Petlund * linear-timeout retransmissions into a black hole 54236e31b0aSAndreas Petlund */ 54336e31b0aSAndreas Petlund if (sk->sk_state == TCP_ESTABLISHED && 5442c04ac8aSEric Dumazet (tp->thin_lto || net->ipv4.sysctl_tcp_thin_linear_timeouts) && 54536e31b0aSAndreas Petlund tcp_stream_is_thin(tp) && 54636e31b0aSAndreas Petlund icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) { 54736e31b0aSAndreas Petlund icsk->icsk_backoff = 0; 54836e31b0aSAndreas Petlund icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX); 54936e31b0aSAndreas Petlund } else { 55036e31b0aSAndreas Petlund /* Use normal (exponential) backoff */ 551463c84b9SArnaldo Carvalho de Melo icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX); 55236e31b0aSAndreas Petlund } 553b701a99eSJon Maxwell inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 554b701a99eSJon Maxwell tcp_clamp_rto_to_user_timeout(sk), TCP_RTO_MAX); 555ce682ef6SEric Dumazet if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1 + 1, 0)) 5561da177e4SLinus Torvalds __sk_dst_reset(sk); 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds out:; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 561c380d37eSRichard Sailer /* Called with bottom-half processing disabled. 562c380d37eSRichard Sailer Called by tcp_write_timer() */ 5636f458dfbSEric Dumazet void tcp_write_timer_handler(struct sock *sk) 5641da177e4SLinus Torvalds { 565463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 5661da177e4SLinus Torvalds int event; 5671da177e4SLinus Torvalds 56802b2faafSEric Dumazet if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) || 56902b2faafSEric Dumazet !icsk->icsk_pending) 5701da177e4SLinus Torvalds goto out; 5711da177e4SLinus Torvalds 572463c84b9SArnaldo Carvalho de Melo if (time_after(icsk->icsk_timeout, jiffies)) { 573463c84b9SArnaldo Carvalho de Melo sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout); 5741da177e4SLinus Torvalds goto out; 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5779a568de4SEric Dumazet tcp_mstamp_refresh(tcp_sk(sk)); 578463c84b9SArnaldo Carvalho de Melo event = icsk->icsk_pending; 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds switch (event) { 58157dde7f7SYuchung Cheng case ICSK_TIME_REO_TIMEOUT: 58257dde7f7SYuchung Cheng tcp_rack_reo_timeout(sk); 58357dde7f7SYuchung Cheng break; 5846ba8a3b1SNandita Dukkipati case ICSK_TIME_LOSS_PROBE: 5856ba8a3b1SNandita Dukkipati tcp_send_loss_probe(sk); 5866ba8a3b1SNandita Dukkipati break; 587463c84b9SArnaldo Carvalho de Melo case ICSK_TIME_RETRANS: 5886ba8a3b1SNandita Dukkipati icsk->icsk_pending = 0; 5891da177e4SLinus Torvalds tcp_retransmit_timer(sk); 5901da177e4SLinus Torvalds break; 591463c84b9SArnaldo Carvalho de Melo case ICSK_TIME_PROBE0: 5926ba8a3b1SNandita Dukkipati icsk->icsk_pending = 0; 5931da177e4SLinus Torvalds tcp_probe_timer(sk); 5941da177e4SLinus Torvalds break; 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds out: 5983ab224beSHideo Aoki sk_mem_reclaim(sk); 5996f458dfbSEric Dumazet } 6006f458dfbSEric Dumazet 60159f379f9SKees Cook static void tcp_write_timer(struct timer_list *t) 6026f458dfbSEric Dumazet { 60359f379f9SKees Cook struct inet_connection_sock *icsk = 60459f379f9SKees Cook from_timer(icsk, t, icsk_retransmit_timer); 60559f379f9SKees Cook struct sock *sk = &icsk->icsk_inet.sk; 6066f458dfbSEric Dumazet 6076f458dfbSEric Dumazet bh_lock_sock(sk); 6086f458dfbSEric Dumazet if (!sock_owned_by_user(sk)) { 6096f458dfbSEric Dumazet tcp_write_timer_handler(sk); 6106f458dfbSEric Dumazet } else { 611c380d37eSRichard Sailer /* delegate our work to tcp_release_cb() */ 6127aa5470cSEric Dumazet if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &sk->sk_tsq_flags)) 613144d56e9SEric Dumazet sock_hold(sk); 6146f458dfbSEric Dumazet } 6151da177e4SLinus Torvalds bh_unlock_sock(sk); 6161da177e4SLinus Torvalds sock_put(sk); 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 61942cb80a2SEric Dumazet void tcp_syn_ack_timeout(const struct request_sock *req) 62072659eccSOctavian Purdila { 62142cb80a2SEric Dumazet struct net *net = read_pnet(&inet_rsk(req)->ireq_net); 62242cb80a2SEric Dumazet 62302a1d6e7SEric Dumazet __NET_INC_STATS(net, LINUX_MIB_TCPTIMEOUTS); 62472659eccSOctavian Purdila } 62572659eccSOctavian Purdila EXPORT_SYMBOL(tcp_syn_ack_timeout); 62672659eccSOctavian Purdila 6271da177e4SLinus Torvalds void tcp_set_keepalive(struct sock *sk, int val) 6281da177e4SLinus Torvalds { 6291da177e4SLinus Torvalds if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) 6301da177e4SLinus Torvalds return; 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds if (val && !sock_flag(sk, SOCK_KEEPOPEN)) 633463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk))); 6341da177e4SLinus Torvalds else if (!val) 635463c84b9SArnaldo Carvalho de Melo inet_csk_delete_keepalive_timer(sk); 6361da177e4SLinus Torvalds } 6374b9d07a4SUrsula Braun EXPORT_SYMBOL_GPL(tcp_set_keepalive); 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds 64059f379f9SKees Cook static void tcp_keepalive_timer (struct timer_list *t) 6411da177e4SLinus Torvalds { 64259f379f9SKees Cook struct sock *sk = from_timer(sk, t, sk_timer); 6436687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 6441da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 6456c37e5deSFlavio Leitner u32 elapsed; 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* Only process if socket is not in use. */ 6481da177e4SLinus Torvalds bh_lock_sock(sk); 6491da177e4SLinus Torvalds if (sock_owned_by_user(sk)) { 6501da177e4SLinus Torvalds /* Try again later. */ 651463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer (sk, HZ/20); 6521da177e4SLinus Torvalds goto out; 6531da177e4SLinus Torvalds } 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds if (sk->sk_state == TCP_LISTEN) { 656fa76ce73SEric Dumazet pr_err("Hmm... keepalive on a LISTEN ???\n"); 6571da177e4SLinus Torvalds goto out; 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 6604688eb7cSEric Dumazet tcp_mstamp_refresh(tp); 6611da177e4SLinus Torvalds if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) { 6621da177e4SLinus Torvalds if (tp->linger2 >= 0) { 663463c84b9SArnaldo Carvalho de Melo const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds if (tmo > 0) { 6661da177e4SLinus Torvalds tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); 6671da177e4SLinus Torvalds goto out; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds tcp_send_active_reset(sk, GFP_ATOMIC); 6711da177e4SLinus Torvalds goto death; 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 6742dda6400SEric Dumazet if (!sock_flag(sk, SOCK_KEEPOPEN) || 6752dda6400SEric Dumazet ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT))) 6761da177e4SLinus Torvalds goto out; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds elapsed = keepalive_time_when(tp); 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds /* It is alive without keepalive 8) */ 68175c119afSEric Dumazet if (tp->packets_out || !tcp_write_queue_empty(sk)) 6821da177e4SLinus Torvalds goto resched; 6831da177e4SLinus Torvalds 6846c37e5deSFlavio Leitner elapsed = keepalive_time_elapsed(tp); 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds if (elapsed >= keepalive_time_when(tp)) { 687dca43c75SJerry Chu /* If the TCP_USER_TIMEOUT option is enabled, use that 688dca43c75SJerry Chu * to determine when to timeout instead. 689dca43c75SJerry Chu */ 690dca43c75SJerry Chu if ((icsk->icsk_user_timeout != 0 && 6919bcc66e1SJon Maxwell elapsed >= msecs_to_jiffies(icsk->icsk_user_timeout) && 692dca43c75SJerry Chu icsk->icsk_probes_out > 0) || 693dca43c75SJerry Chu (icsk->icsk_user_timeout == 0 && 694dca43c75SJerry Chu icsk->icsk_probes_out >= keepalive_probes(tp))) { 6951da177e4SLinus Torvalds tcp_send_active_reset(sk, GFP_ATOMIC); 6961da177e4SLinus Torvalds tcp_write_err(sk); 6971da177e4SLinus Torvalds goto out; 6981da177e4SLinus Torvalds } 699e520af48SEric Dumazet if (tcp_write_wakeup(sk, LINUX_MIB_TCPKEEPALIVE) <= 0) { 7006687e988SArnaldo Carvalho de Melo icsk->icsk_probes_out++; 7011da177e4SLinus Torvalds elapsed = keepalive_intvl_when(tp); 7021da177e4SLinus Torvalds } else { 7031da177e4SLinus Torvalds /* If keepalive was lost due to local congestion, 7041da177e4SLinus Torvalds * try harder. 7051da177e4SLinus Torvalds */ 7061da177e4SLinus Torvalds elapsed = TCP_RESOURCE_PROBE_INTERVAL; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds } else { 7091da177e4SLinus Torvalds /* It is tp->rcv_tstamp + keepalive_time_when(tp) */ 7101da177e4SLinus Torvalds elapsed = keepalive_time_when(tp) - elapsed; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 7133ab224beSHideo Aoki sk_mem_reclaim(sk); 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds resched: 716463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer (sk, elapsed); 7171da177e4SLinus Torvalds goto out; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds death: 7201da177e4SLinus Torvalds tcp_done(sk); 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds out: 7231da177e4SLinus Torvalds bh_unlock_sock(sk); 7241da177e4SLinus Torvalds sock_put(sk); 7251da177e4SLinus Torvalds } 7266f458dfbSEric Dumazet 7275d9f4262SEric Dumazet static enum hrtimer_restart tcp_compressed_ack_kick(struct hrtimer *timer) 7285d9f4262SEric Dumazet { 7295d9f4262SEric Dumazet struct tcp_sock *tp = container_of(timer, struct tcp_sock, compressed_ack_timer); 7305d9f4262SEric Dumazet struct sock *sk = (struct sock *)tp; 7315d9f4262SEric Dumazet 7325d9f4262SEric Dumazet bh_lock_sock(sk); 7335d9f4262SEric Dumazet if (!sock_owned_by_user(sk)) { 73486de5921SEric Dumazet if (tp->compressed_ack > TCP_FASTRETRANS_THRESH) 7355d9f4262SEric Dumazet tcp_send_ack(sk); 7365d9f4262SEric Dumazet } else { 7375d9f4262SEric Dumazet if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, 7385d9f4262SEric Dumazet &sk->sk_tsq_flags)) 7395d9f4262SEric Dumazet sock_hold(sk); 7405d9f4262SEric Dumazet } 7415d9f4262SEric Dumazet bh_unlock_sock(sk); 7425d9f4262SEric Dumazet 7435d9f4262SEric Dumazet sock_put(sk); 7445d9f4262SEric Dumazet 7455d9f4262SEric Dumazet return HRTIMER_NORESTART; 7465d9f4262SEric Dumazet } 7475d9f4262SEric Dumazet 7486f458dfbSEric Dumazet void tcp_init_xmit_timers(struct sock *sk) 7496f458dfbSEric Dumazet { 7506f458dfbSEric Dumazet inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer, 7516f458dfbSEric Dumazet &tcp_keepalive_timer); 752fb420d5dSEric Dumazet hrtimer_init(&tcp_sk(sk)->pacing_timer, CLOCK_MONOTONIC, 75373a6bab5SEric Dumazet HRTIMER_MODE_ABS_PINNED_SOFT); 754218af599SEric Dumazet tcp_sk(sk)->pacing_timer.function = tcp_pace_kick; 7555d9f4262SEric Dumazet 7565d9f4262SEric Dumazet hrtimer_init(&tcp_sk(sk)->compressed_ack_timer, CLOCK_MONOTONIC, 7575d9f4262SEric Dumazet HRTIMER_MODE_REL_PINNED_SOFT); 7585d9f4262SEric Dumazet tcp_sk(sk)->compressed_ack_timer.function = tcp_compressed_ack_kick; 7596f458dfbSEric Dumazet } 760