blob: a845b7692c1b0d0cce8f68345db8d4f3fa812c68 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Implementation of the Transmission Control Protocol(TCP).
7 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Mark Evans, <evansmp@uhura.aston.ac.uk>
11 * Corey Minyard <wf-rch!minyard@relay.EU.net>
12 * Florian La Roche, <flla@stud.uni-sb.de>
13 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
14 * Linus Torvalds, <torvalds@cs.helsinki.fi>
15 * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 * Matthew Dillon, <dillon@apollo.west.oic.com>
17 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
18 * Jorge Cwik, <jorge@laser.satlink.net>
19 */
20
21#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/tcp.h>
24
Andreas Petlund36e31b0a2010-02-18 02:47:01 +000025int sysctl_tcp_thin_linear_timeouts __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Richard Sailerc380d372016-07-16 04:04:34 +020027/**
28 * tcp_write_err() - close socket and save error info
29 * @sk: The socket the error has appeared on.
30 *
31 * Returns: Nothing (void)
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static void tcp_write_err(struct sock *sk)
35{
36 sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
37 sk->sk_error_report(sk);
38
Soheil Hassas Yeganehe44c1732018-03-06 17:15:12 -050039 tcp_write_queue_purge(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 tcp_done(sk);
Eric Dumazet02a1d6e2016-04-27 16:44:39 -070041 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Richard Sailerc380d372016-07-16 04:04:34 +020044/**
45 * tcp_out_of_resources() - Close socket if out of resources
46 * @sk: pointer to current socket
47 * @do_reset: send a last packet with reset flag
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 *
Richard Sailerc380d372016-07-16 04:04:34 +020049 * Do not allow orphaned sockets to eat all our resources.
50 * This is direct violation of TCP specs, but it is required
51 * to prevent DoS attacks. It is called when a retransmission timeout
52 * or zero probe timeout occurs on orphaned socket.
53 *
Dan Streetman32e57f82018-01-18 16:14:26 -050054 * Also close if our net namespace is exiting; in that case there is no
55 * hope of ever communicating again since all netns interfaces are already
56 * down (or about to be down), and we need to release our dst references,
57 * which have been moved to the netns loopback interface, so the namespace
58 * can finish exiting. This condition is only possible if we are a kernel
59 * socket, as those do not hold references to the namespace.
60 *
Richard Sailerc380d372016-07-16 04:04:34 +020061 * Criteria is still not confirmed experimentally and may change.
62 * We kill the socket, if:
63 * 1. If number of orphaned sockets exceeds an administratively configured
64 * limit.
65 * 2. If we have strong memory pressure.
Dan Streetman32e57f82018-01-18 16:14:26 -050066 * 3. If our net namespace is exiting.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
Yuchung Chengb2482302014-09-29 13:20:38 -070068static int tcp_out_of_resources(struct sock *sk, bool do_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerad1af0f2010-08-25 02:27:49 -070071 int shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090073 /* If peer does not open window for long time, or did not transmit
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * anything for long time, penalize it. */
Eric Dumazetd635fbe2017-05-16 14:00:03 -070075 if ((s32)(tcp_jiffies32 - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
David S. Millerad1af0f2010-08-25 02:27:49 -070076 shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 /* If some dubious ICMP arrived, penalize even more. */
79 if (sk->sk_err_soft)
David S. Millerad1af0f2010-08-25 02:27:49 -070080 shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Arun Sharmaefcdbf22012-01-30 14:16:06 -080082 if (tcp_check_oom(sk, shift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /* Catch exceptional cases, when connection requires reset.
84 * 1. Last segment was sent recently. */
Eric Dumazetd635fbe2017-05-16 14:00:03 -070085 if ((s32)(tcp_jiffies32 - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 /* 2. Window is closed. */
87 (!tp->snd_wnd && !tp->packets_out))
Yuchung Chengb2482302014-09-29 13:20:38 -070088 do_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (do_reset)
90 tcp_send_active_reset(sk, GFP_ATOMIC);
91 tcp_done(sk);
Eric Dumazet02a1d6e2016-04-27 16:44:39 -070092 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 1;
94 }
Dan Streetman32e57f82018-01-18 16:14:26 -050095
96 if (!check_net(sock_net(sk))) {
97 /* Not possible to send reset; just close */
98 tcp_done(sk);
99 return 1;
100 }
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
103}
104
Richard Sailerc380d372016-07-16 04:04:34 +0200105/**
106 * tcp_orphan_retries() - Returns maximal number of retries on an orphaned socket
107 * @sk: Pointer to the current socket.
108 * @alive: bool, socket alive state
109 */
Richard Sailer7533ce32015-10-09 02:41:37 +0200110static int tcp_orphan_retries(struct sock *sk, bool alive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Nikolay Borisovc402d9b2016-02-03 09:46:55 +0200112 int retries = sock_net(sk)->ipv4.sysctl_tcp_orphan_retries; /* May be zero. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 /* We know from an ICMP that something is wrong. */
115 if (sk->sk_err_soft && !alive)
116 retries = 0;
117
118 /* However, if socket sent something recently, select some safe
119 * number of retries. 8 corresponds to >100 seconds with minimal
120 * RTO of 200msec. */
121 if (retries == 0 && alive)
122 retries = 8;
123 return retries;
124}
125
Eric Dumazetce55dd32007-12-21 01:50:43 -0800126static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
127{
Fan Dub0f9ca52015-02-10 09:53:16 +0800128 struct net *net = sock_net(sk);
129
Eric Dumazetce55dd32007-12-21 01:50:43 -0800130 /* Black hole detection */
Fan Dub0f9ca52015-02-10 09:53:16 +0800131 if (net->ipv4.sysctl_tcp_mtu_probing) {
Eric Dumazetce55dd32007-12-21 01:50:43 -0800132 if (!icsk->icsk_mtup.enabled) {
133 icsk->icsk_mtup.enabled = 1;
Eric Dumazetc74df292017-05-16 14:00:08 -0700134 icsk->icsk_mtup.probe_timestamp = tcp_jiffies32;
Eric Dumazetce55dd32007-12-21 01:50:43 -0800135 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
136 } else {
Fan Dub0f9ca52015-02-10 09:53:16 +0800137 struct net *net = sock_net(sk);
Eric Dumazetce55dd32007-12-21 01:50:43 -0800138 struct tcp_sock *tp = tcp_sk(sk);
David S. Miller829942c2007-12-21 04:29:16 -0800139 int mss;
140
Eric Dumazet8beb5c52007-12-21 05:58:29 -0800141 mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1;
Fan Dub0f9ca52015-02-10 09:53:16 +0800142 mss = min(net->ipv4.sysctl_tcp_base_mss, mss);
Eric Dumazetce55dd32007-12-21 01:50:43 -0800143 mss = max(mss, 68 - tp->tcp_header_len);
144 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
145 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
146 }
147 }
148}
149
Richard Sailerc380d372016-07-16 04:04:34 +0200150
151/**
152 * retransmits_timed_out() - returns true if this connection has timed out
153 * @sk: The current socket
154 * @boundary: max number of retransmissions
155 * @timeout: A custom timeout value.
156 * If set to 0 the default timeout is calculated and used.
157 * Using TCP_RTO_MIN and the number of unsuccessful retransmits.
Richard Sailerc380d372016-07-16 04:04:34 +0200158 *
159 * The default "timeout" value this function can calculate and use
160 * is equivalent to the timeout of a TCP Connection
161 * after "boundary" unsuccessful, exponentially backed-off
Eric Dumazetce682ef2017-05-23 12:38:35 -0700162 * retransmissions with an initial RTO of TCP_RTO_MIN.
Damian Lukowski2f7de572009-12-07 06:06:16 +0000163 */
164static bool retransmits_timed_out(struct sock *sk,
Jerry Chudca43c72010-08-27 19:13:28 +0000165 unsigned int boundary,
Eric Dumazetce682ef2017-05-23 12:38:35 -0700166 unsigned int timeout)
Damian Lukowski2f7de572009-12-07 06:06:16 +0000167{
Eric Dumazetce682ef2017-05-23 12:38:35 -0700168 const unsigned int rto_base = TCP_RTO_MIN;
Eric Dumazet9a568de2017-05-16 14:00:14 -0700169 unsigned int linear_backoff_thresh, start_ts;
Damian Lukowski2f7de572009-12-07 06:06:16 +0000170
171 if (!inet_csk(sk)->icsk_retransmits)
172 return false;
173
Eric Dumazet7faee5c2014-09-05 15:33:33 -0700174 start_ts = tcp_sk(sk)->retrans_stamp;
175 if (unlikely(!start_ts))
176 start_ts = tcp_skb_timestamp(tcp_write_queue_head(sk));
Damian Lukowski2f7de572009-12-07 06:06:16 +0000177
Jerry Chudca43c72010-08-27 19:13:28 +0000178 if (likely(timeout == 0)) {
David S. Miller21a180c2010-10-04 11:56:38 -0700179 linear_backoff_thresh = ilog2(TCP_RTO_MAX/rto_base);
Damian Lukowski2f7de572009-12-07 06:06:16 +0000180
Jerry Chudca43c72010-08-27 19:13:28 +0000181 if (boundary <= linear_backoff_thresh)
David S. Miller21a180c2010-10-04 11:56:38 -0700182 timeout = ((2 << boundary) - 1) * rto_base;
Jerry Chudca43c72010-08-27 19:13:28 +0000183 else
David S. Miller21a180c2010-10-04 11:56:38 -0700184 timeout = ((2 << linear_backoff_thresh) - 1) * rto_base +
Jerry Chudca43c72010-08-27 19:13:28 +0000185 (boundary - linear_backoff_thresh) * TCP_RTO_MAX;
186 }
Eric Dumazet9a568de2017-05-16 14:00:14 -0700187 return (tcp_time_stamp(tcp_sk(sk)) - start_ts) >= jiffies_to_msecs(timeout);
Damian Lukowski2f7de572009-12-07 06:06:16 +0000188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/* A write timeout has occurred. Process the after effects. */
191static int tcp_write_timeout(struct sock *sk)
192{
John Heffner5d424d52006-03-20 17:53:41 -0800193 struct inet_connection_sock *icsk = inet_csk(sk);
Yuchung Chengc9686012013-10-29 10:09:05 -0700194 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov6fa25162016-02-03 09:46:49 +0200195 struct net *net = sock_net(sk);
Eric Dumazetce682ef2017-05-23 12:38:35 -0700196 bool expired, do_reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int retry_until;
198
199 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
Yuchung Chengc9686012013-10-29 10:09:05 -0700200 if (icsk->icsk_retransmits) {
Eric Dumazetb6c67122010-04-08 23:03:29 +0000201 dst_negative_advice(sk);
Yuchung Chengc9686012013-10-29 10:09:05 -0700202 if (tp->syn_fastopen || tp->syn_data)
Daniel Lee2646c832015-04-06 14:37:27 -0700203 tcp_fastopen_cache_set(sk, 0, NULL, true, 0);
Yuchung Chengdd52bc22015-11-18 18:17:31 -0800204 if (tp->syn_data && icsk->icsk_retransmits == 1)
Eric Dumazetc10d9312016-04-29 14:16:47 -0700205 NET_INC_STATS(sock_net(sk),
206 LINUX_MIB_TCPFASTOPENACTIVEFAIL);
Lawrence Brakmo3acf3ec2016-09-27 19:03:37 -0700207 } else if (!tp->syn_data && !tp->syn_fastopen) {
208 sk_rethink_txhash(sk);
Yuchung Chengc9686012013-10-29 10:09:05 -0700209 }
Nikolay Borisov6fa25162016-02-03 09:46:49 +0200210 retry_until = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
Eric Dumazetce682ef2017-05-23 12:38:35 -0700211 expired = icsk->icsk_retransmits >= retry_until;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 } else {
Eric Dumazetce682ef2017-05-23 12:38:35 -0700213 if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1, 0)) {
Yuchung Cheng0e45f4d2015-11-18 18:17:30 -0800214 /* Some middle-boxes may black-hole Fast Open _after_
215 * the handshake. Therefore we conservatively disable
Wei Wang59450f82017-04-20 14:45:48 -0700216 * Fast Open on this path on recurring timeouts after
217 * successful Fast Open.
Yuchung Cheng0e45f4d2015-11-18 18:17:30 -0800218 */
Wei Wang59450f82017-04-20 14:45:48 -0700219 if (tp->syn_data_acked) {
Yuchung Cheng0e45f4d2015-11-18 18:17:30 -0800220 tcp_fastopen_cache_set(sk, 0, NULL, true, 0);
Nikolay Borisovae5c3f42016-02-03 09:46:53 +0200221 if (icsk->icsk_retransmits == net->ipv4.sysctl_tcp_retries1)
Eric Dumazetc10d9312016-04-29 14:16:47 -0700222 NET_INC_STATS(sock_net(sk),
223 LINUX_MIB_TCPFASTOPENACTIVEFAIL);
Yuchung Cheng0e45f4d2015-11-18 18:17:30 -0800224 }
John Heffner5d424d52006-03-20 17:53:41 -0800225 /* Black hole detection */
Eric Dumazetce55dd32007-12-21 01:50:43 -0800226 tcp_mtu_probing(icsk, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Eric Dumazetb6c67122010-04-08 23:03:29 +0000228 dst_negative_advice(sk);
Lawrence Brakmo3acf3ec2016-09-27 19:03:37 -0700229 } else {
230 sk_rethink_txhash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
Nikolay Borisovc6214a92016-02-03 09:46:54 +0200233 retry_until = net->ipv4.sysctl_tcp_retries2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (sock_flag(sk, SOCK_DEAD)) {
Richard Sailer7533ce32015-10-09 02:41:37 +0200235 const bool alive = icsk->icsk_rto < TCP_RTO_MAX;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 retry_until = tcp_orphan_retries(sk, alive);
Damian Lukowski6fa12c82009-08-26 00:16:34 +0000238 do_reset = alive ||
Eric Dumazetce682ef2017-05-23 12:38:35 -0700239 !retransmits_timed_out(sk, retry_until, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Damian Lukowski6fa12c82009-08-26 00:16:34 +0000241 if (tcp_out_of_resources(sk, do_reset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 1;
243 }
Eric Dumazetce682ef2017-05-23 12:38:35 -0700244 expired = retransmits_timed_out(sk, retry_until,
245 icsk->icsk_user_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Eric Dumazetce682ef2017-05-23 12:38:35 -0700247 if (expired) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* Has it gone just too far? */
249 tcp_write_err(sk);
250 return 1;
251 }
252 return 0;
253}
254
Eric Dumazetc10d9312016-04-29 14:16:47 -0700255/* Called with BH disabled */
Eric Dumazet6f458df2012-07-20 05:45:50 +0000256void tcp_delack_timer_handler(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700258 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
David S. Miller9993e7d2008-01-10 21:56:38 -0800260 sk_mem_reclaim_partial(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Eric Dumazet02b2faa2017-03-03 14:08:21 -0800262 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
263 !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto out;
265
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700266 if (time_after(icsk->icsk_ack.timeout, jiffies)) {
267 sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto out;
269 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700270 icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700272 if (inet_csk_ack_scheduled(sk)) {
273 if (!icsk->icsk_ack.pingpong) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* Delayed ACK missed: inflate ATO. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700275 icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 } else {
277 /* Delayed ACK missed: leave pingpong mode and
278 * deflate ATO.
279 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700280 icsk->icsk_ack.pingpong = 0;
281 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Eric Dumazet55043192017-12-12 18:22:52 -0800283 tcp_mstamp_refresh(tcp_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 tcp_send_ack(sk);
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700285 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288out:
Eric Dumazetb8da51e2015-05-15 12:39:27 -0700289 if (tcp_under_memory_pressure(sk))
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800290 sk_mem_reclaim(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000291}
292
Richard Sailerc380d372016-07-16 04:04:34 +0200293
294/**
295 * tcp_delack_timer() - The TCP delayed ACK timeout handler
296 * @data: Pointer to the current socket. (gets casted to struct sock *)
297 *
298 * This function gets (indirectly) called when the kernel timer for a TCP packet
299 * of this socket expires. Calls tcp_delack_timer_handler() to do the actual work.
300 *
301 * Returns: Nothing (void)
302 */
Eric Dumazet6f458df2012-07-20 05:45:50 +0000303static void tcp_delack_timer(unsigned long data)
304{
305 struct sock *sk = (struct sock *)data;
306
307 bh_lock_sock(sk);
308 if (!sock_owned_by_user(sk)) {
309 tcp_delack_timer_handler(sk);
310 } else {
311 inet_csk(sk)->icsk_ack.blocked = 1;
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700312 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000313 /* deleguate our work to tcp_release_cb() */
Eric Dumazet7aa54702016-12-03 11:14:57 -0800314 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &sk->sk_tsq_flags))
Eric Dumazet144d56e2012-08-20 00:22:46 +0000315 sock_hold(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 bh_unlock_sock(sk);
318 sock_put(sk);
319}
320
321static void tcp_probe_timer(struct sock *sk)
322{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300323 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct tcp_sock *tp = tcp_sk(sk);
325 int max_probes;
Yuchung Chengb2482302014-09-29 13:20:38 -0700326 u32 start_ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
David S. Millerfe067e82007-03-07 12:12:44 -0800328 if (tp->packets_out || !tcp_send_head(sk)) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300329 icsk->icsk_probes_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return;
331 }
332
Yuchung Chengb2482302014-09-29 13:20:38 -0700333 /* RFC 1122 4.2.2.17 requires the sender to stay open indefinitely as
334 * long as the receiver continues to respond probes. We support this by
335 * default and reset icsk_probes_out with incoming ACKs. But if the
336 * socket is orphaned or the user specifies TCP_USER_TIMEOUT, we
337 * kill the socket when the retry count and the time exceeds the
338 * corresponding system limit. We also implement similar policy when
339 * we use RTO to probe window in tcp_retransmit_timer().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
Yuchung Chengb2482302014-09-29 13:20:38 -0700341 start_ts = tcp_skb_timestamp(tcp_send_head(sk));
342 if (!start_ts)
Eric Dumazet385e2072017-05-16 14:00:00 -0700343 tcp_send_head(sk)->skb_mstamp = tp->tcp_mstamp;
Yuchung Chengb2482302014-09-29 13:20:38 -0700344 else if (icsk->icsk_user_timeout &&
Eric Dumazet4ab68872017-05-21 10:39:00 -0700345 (s32)(tcp_time_stamp(tp) - start_ts) >
346 jiffies_to_msecs(icsk->icsk_user_timeout))
Yuchung Chengb2482302014-09-29 13:20:38 -0700347 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Nikolay Borisovc6214a92016-02-03 09:46:54 +0200349 max_probes = sock_net(sk)->ipv4.sysctl_tcp_retries2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (sock_flag(sk, SOCK_DEAD)) {
Richard Sailer7533ce32015-10-09 02:41:37 +0200351 const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 max_probes = tcp_orphan_retries(sk, alive);
Yuchung Chengb2482302014-09-29 13:20:38 -0700354 if (!alive && icsk->icsk_backoff >= max_probes)
355 goto abort;
356 if (tcp_out_of_resources(sk, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return;
358 }
359
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300360 if (icsk->icsk_probes_out > max_probes) {
Yuchung Chengb2482302014-09-29 13:20:38 -0700361abort: tcp_write_err(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 } else {
363 /* Only send another probe if we didn't close things up. */
364 tcp_send_probe0(sk);
365 }
366}
367
368/*
Jerry Chu83368862012-08-31 12:29:12 +0000369 * Timer for Fast Open socket to retransmit SYNACK. Note that the
370 * sk here is the child socket, not the parent (listener) socket.
371 */
372static void tcp_fastopen_synack_timer(struct sock *sk)
373{
374 struct inet_connection_sock *icsk = inet_csk(sk);
375 int max_retries = icsk->icsk_syn_retries ? :
Nikolay Borisov7c083ec2016-02-03 09:46:50 +0200376 sock_net(sk)->ipv4.sysctl_tcp_synack_retries + 1; /* add one more retry for fastopen */
Jerry Chu83368862012-08-31 12:29:12 +0000377 struct request_sock *req;
378
379 req = tcp_sk(sk)->fastopen_rsk;
Eric Dumazet42cb80a2015-03-22 10:22:19 -0700380 req->rsk_ops->syn_ack_timeout(req);
Jerry Chu83368862012-08-31 12:29:12 +0000381
Eric Dumazete6c022a2012-10-27 23:16:46 +0000382 if (req->num_timeout >= max_retries) {
Jerry Chu83368862012-08-31 12:29:12 +0000383 tcp_write_err(sk);
384 return;
385 }
386 /* XXX (TFO) - Unlike regular SYN-ACK retransmit, we ignore error
387 * returned from rtx_syn_ack() to make it more persistent like
388 * regular retransmit because if the child socket has been accepted
389 * it's not good to give up too easily.
390 */
Eric Dumazete6c022a2012-10-27 23:16:46 +0000391 inet_rtx_syn_ack(sk, req);
392 req->num_timeout++;
Yuchung Cheng7e32b442016-09-21 16:16:15 -0700393 icsk->icsk_retransmits++;
Jerry Chu83368862012-08-31 12:29:12 +0000394 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
Eric Dumazete6c022a2012-10-27 23:16:46 +0000395 TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
Jerry Chu83368862012-08-31 12:29:12 +0000396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Richard Sailerc380d372016-07-16 04:04:34 +0200399/**
400 * tcp_retransmit_timer() - The TCP retransmit timeout handler
401 * @sk: Pointer to the current socket.
402 *
403 * This function gets called when the kernel timer for a TCP packet
404 * of this socket expires.
405 *
406 * It handles retransmission, timer adjustment and other necesarry measures.
407 *
408 * Returns: Nothing (void)
409 */
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000410void tcp_retransmit_timer(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisovae5c3f42016-02-03 09:46:53 +0200413 struct net *net = sock_net(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700414 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Jerry Chu83368862012-08-31 12:29:12 +0000416 if (tp->fastopen_rsk) {
Jerry Chu37561f62012-10-22 11:26:36 +0000417 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
418 sk->sk_state != TCP_FIN_WAIT1);
Jerry Chu83368862012-08-31 12:29:12 +0000419 tcp_fastopen_synack_timer(sk);
420 /* Before we receive ACK to our SYN-ACK don't retransmit
421 * anything else (e.g., data or FIN segments).
422 */
423 return;
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (!tp->packets_out)
426 goto out;
427
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700428 WARN_ON(tcp_write_queue_empty(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Nandita Dukkipati9b717a82013-03-11 10:00:44 +0000430 tp->tlp_high_seq = 0;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
433 !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
434 /* Receiver dastardly shrinks window. Our retransmits
435 * become zero probes, but we should not timeout this
436 * connection. If the socket is an orphan, time it out,
437 * we cannot allow such beasts to hang infinitely.
438 */
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700439 struct inet_sock *inet = inet_sk(sk);
440 if (sk->sk_family == AF_INET) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800441 net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
442 &inet->inet_daddr,
443 ntohs(inet->inet_dport),
444 inet->inet_num,
445 tp->snd_una, tp->snd_nxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000447#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700448 else if (sk->sk_family == AF_INET6) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800449 net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
450 &sk->sk_v6_daddr,
451 ntohs(inet->inet_dport),
452 inet->inet_num,
453 tp->snd_una, tp->snd_nxt);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700454 }
455#endif
Eric Dumazet70eabf02017-05-16 14:00:07 -0700456 if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 tcp_write_err(sk);
458 goto out;
459 }
Neal Cardwell5ae344c2014-08-04 19:12:29 -0400460 tcp_enter_loss(sk);
Eric Dumazet10d3be52016-04-21 10:55:23 -0700461 tcp_retransmit_skb(sk, tcp_write_queue_head(sk), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 __sk_dst_reset(sk);
463 goto out_reset_timer;
464 }
465
466 if (tcp_write_timeout(sk))
467 goto out;
468
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700469 if (icsk->icsk_retransmits == 0) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700470 int mib_idx;
471
Ilpo Järvinenc60ce4e2010-10-14 01:52:09 +0000472 if (icsk->icsk_ca_state == TCP_CA_Recovery) {
Ilpo Järvinenbc079e92009-02-28 04:44:34 +0000473 if (tcp_is_sack(tp))
474 mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
475 else
476 mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300477 } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700478 mib_idx = LINUX_MIB_TCPLOSSFAILURES;
Ilpo Järvinenc60ce4e2010-10-14 01:52:09 +0000479 } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
480 tp->sacked_out) {
481 if (tcp_is_sack(tp))
482 mib_idx = LINUX_MIB_TCPSACKFAILURES;
483 else
484 mib_idx = LINUX_MIB_TCPRENOFAILURES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } else {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700486 mib_idx = LINUX_MIB_TCPTIMEOUTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700488 __NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
Neal Cardwell5ae344c2014-08-04 19:12:29 -0400491 tcp_enter_loss(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Eric Dumazet10d3be52016-04-21 10:55:23 -0700493 if (tcp_retransmit_skb(sk, tcp_write_queue_head(sk), 1) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* Retransmission failed because of local congestion,
495 * do not backoff.
496 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700497 if (!icsk->icsk_retransmits)
498 icsk->icsk_retransmits = 1;
499 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700500 min(icsk->icsk_rto, TCP_RESOURCE_PROBE_INTERVAL),
501 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto out;
503 }
504
505 /* Increase the timeout each time we retransmit. Note that
506 * we do not increase the rtt estimate. rto is initialized
507 * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests
508 * that doubling rto each time is the least we can get away with.
509 * In KA9Q, Karn uses this for the first few times, and then
510 * goes to quadratic. netBSD doubles, but only goes up to *64,
511 * and clamps at 1 to 64 sec afterwards. Note that 120 sec is
512 * defined in the protocol as the maximum possible RTT. I guess
513 * we'll have to use something other than TCP to talk to the
514 * University of Mars.
515 *
516 * PAWS allows us longer timeouts and large windows, so once
517 * implemented ftp to mars will work nicely. We will have to fix
518 * the 120 second clamps though!
519 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700520 icsk->icsk_backoff++;
521 icsk->icsk_retransmits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523out_reset_timer:
Andreas Petlund36e31b0a2010-02-18 02:47:01 +0000524 /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is
525 * used to reset timer, set to 0. Recalculate 'icsk_rto' as this
526 * might be increased if the stream oscillates between thin and thick,
527 * thus the old value might already be too high compared to the value
528 * set by 'tcp_set_rto' in tcp_input.c which resets the rto without
529 * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating
530 * exponential backoff behaviour to avoid continue hammering
531 * linear-timeout retransmissions into a black hole
532 */
533 if (sk->sk_state == TCP_ESTABLISHED &&
534 (tp->thin_lto || sysctl_tcp_thin_linear_timeouts) &&
535 tcp_stream_is_thin(tp) &&
536 icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
537 icsk->icsk_backoff = 0;
538 icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX);
539 } else {
540 /* Use normal (exponential) backoff */
541 icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
542 }
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700543 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX);
Eric Dumazetce682ef2017-05-23 12:38:35 -0700544 if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1 + 1, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 __sk_dst_reset(sk);
546
547out:;
548}
549
Richard Sailerc380d372016-07-16 04:04:34 +0200550/* Called with bottom-half processing disabled.
551 Called by tcp_write_timer() */
Eric Dumazet6f458df2012-07-20 05:45:50 +0000552void tcp_write_timer_handler(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700554 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int event;
556
Eric Dumazet02b2faa2017-03-03 14:08:21 -0800557 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
558 !icsk->icsk_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto out;
560
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700561 if (time_after(icsk->icsk_timeout, jiffies)) {
562 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto out;
564 }
565
Eric Dumazet9a568de2017-05-16 14:00:14 -0700566 tcp_mstamp_refresh(tcp_sk(sk));
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700567 event = icsk->icsk_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 switch (event) {
Yuchung Cheng57dde7f2017-01-12 22:11:33 -0800570 case ICSK_TIME_REO_TIMEOUT:
571 tcp_rack_reo_timeout(sk);
572 break;
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000573 case ICSK_TIME_LOSS_PROBE:
574 tcp_send_loss_probe(sk);
575 break;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700576 case ICSK_TIME_RETRANS:
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000577 icsk->icsk_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 tcp_retransmit_timer(sk);
579 break;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700580 case ICSK_TIME_PROBE0:
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000581 icsk->icsk_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 tcp_probe_timer(sk);
583 break;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586out:
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800587 sk_mem_reclaim(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000588}
589
590static void tcp_write_timer(unsigned long data)
591{
592 struct sock *sk = (struct sock *)data;
593
594 bh_lock_sock(sk);
595 if (!sock_owned_by_user(sk)) {
596 tcp_write_timer_handler(sk);
597 } else {
Richard Sailerc380d372016-07-16 04:04:34 +0200598 /* delegate our work to tcp_release_cb() */
Eric Dumazet7aa54702016-12-03 11:14:57 -0800599 if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &sk->sk_tsq_flags))
Eric Dumazet144d56e2012-08-20 00:22:46 +0000600 sock_hold(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 bh_unlock_sock(sk);
603 sock_put(sk);
604}
605
Eric Dumazet42cb80a2015-03-22 10:22:19 -0700606void tcp_syn_ack_timeout(const struct request_sock *req)
Octavian Purdila72659ec2010-01-17 19:09:39 -0800607{
Eric Dumazet42cb80a2015-03-22 10:22:19 -0700608 struct net *net = read_pnet(&inet_rsk(req)->ireq_net);
609
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700610 __NET_INC_STATS(net, LINUX_MIB_TCPTIMEOUTS);
Octavian Purdila72659ec2010-01-17 19:09:39 -0800611}
612EXPORT_SYMBOL(tcp_syn_ack_timeout);
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614void tcp_set_keepalive(struct sock *sk, int val)
615{
616 if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
617 return;
618
619 if (val && !sock_flag(sk, SOCK_KEEPOPEN))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700620 inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 else if (!val)
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700622 inet_csk_delete_keepalive_timer(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
Ursula Braun4b9d07a2017-01-09 16:55:12 +0100624EXPORT_SYMBOL_GPL(tcp_set_keepalive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626
627static void tcp_keepalive_timer (unsigned long data)
628{
629 struct sock *sk = (struct sock *) data;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300630 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct tcp_sock *tp = tcp_sk(sk);
Flavio Leitner6c37e5d2010-04-26 18:33:27 +0000632 u32 elapsed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* Only process if socket is not in use. */
635 bh_lock_sock(sk);
636 if (sock_owned_by_user(sk)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900637 /* Try again later. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700638 inet_csk_reset_keepalive_timer (sk, HZ/20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto out;
640 }
641
642 if (sk->sk_state == TCP_LISTEN) {
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700643 pr_err("Hmm... keepalive on a LISTEN ???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto out;
645 }
646
Eric Dumazet55043192017-12-12 18:22:52 -0800647 tcp_mstamp_refresh(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) {
649 if (tp->linger2 >= 0) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700650 const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (tmo > 0) {
653 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
654 goto out;
655 }
656 }
657 tcp_send_active_reset(sk, GFP_ATOMIC);
658 goto death;
659 }
660
Eric Dumazet2dda6402017-08-02 23:10:46 -0700661 if (!sock_flag(sk, SOCK_KEEPOPEN) ||
662 ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto out;
664
665 elapsed = keepalive_time_when(tp);
666
667 /* It is alive without keepalive 8) */
David S. Millerfe067e82007-03-07 12:12:44 -0800668 if (tp->packets_out || tcp_send_head(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto resched;
670
Flavio Leitner6c37e5d2010-04-26 18:33:27 +0000671 elapsed = keepalive_time_elapsed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (elapsed >= keepalive_time_when(tp)) {
Jerry Chudca43c72010-08-27 19:13:28 +0000674 /* If the TCP_USER_TIMEOUT option is enabled, use that
675 * to determine when to timeout instead.
676 */
677 if ((icsk->icsk_user_timeout != 0 &&
678 elapsed >= icsk->icsk_user_timeout &&
679 icsk->icsk_probes_out > 0) ||
680 (icsk->icsk_user_timeout == 0 &&
681 icsk->icsk_probes_out >= keepalive_probes(tp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 tcp_send_active_reset(sk, GFP_ATOMIC);
683 tcp_write_err(sk);
684 goto out;
685 }
Eric Dumazete520af42015-05-06 14:26:25 -0700686 if (tcp_write_wakeup(sk, LINUX_MIB_TCPKEEPALIVE) <= 0) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300687 icsk->icsk_probes_out++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 elapsed = keepalive_intvl_when(tp);
689 } else {
690 /* If keepalive was lost due to local congestion,
691 * try harder.
692 */
693 elapsed = TCP_RESOURCE_PROBE_INTERVAL;
694 }
695 } else {
696 /* It is tp->rcv_tstamp + keepalive_time_when(tp) */
697 elapsed = keepalive_time_when(tp) - elapsed;
698 }
699
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800700 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702resched:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700703 inet_csk_reset_keepalive_timer (sk, elapsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 goto out;
705
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900706death:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 tcp_done(sk);
708
709out:
710 bh_unlock_sock(sk);
711 sock_put(sk);
712}
Eric Dumazet6f458df2012-07-20 05:45:50 +0000713
714void tcp_init_xmit_timers(struct sock *sk)
715{
716 inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
717 &tcp_keepalive_timer);
Eric Dumazet218af592017-05-16 04:24:36 -0700718 hrtimer_init(&tcp_sk(sk)->pacing_timer, CLOCK_MONOTONIC,
719 HRTIMER_MODE_ABS_PINNED);
720 tcp_sk(sk)->pacing_timer.function = tcp_pace_kick;
Eric Dumazet6f458df2012-07-20 05:45:50 +0000721}