blob: 92bdf64fffae3a5be291ca419eb21276b4c8cbae [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
Richard Sailerc380d372016-07-16 04:04:34 +020025/**
26 * tcp_write_err() - close socket and save error info
27 * @sk: The socket the error has appeared on.
28 *
29 * Returns: Nothing (void)
30 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static void tcp_write_err(struct sock *sk)
33{
34 sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
35 sk->sk_error_report(sk);
36
Soheil Hassas Yeganehe05836a2018-03-06 17:15:12 -050037 tcp_write_queue_purge(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 tcp_done(sk);
Eric Dumazet02a1d6e2016-04-27 16:44:39 -070039 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Richard Sailerc380d372016-07-16 04:04:34 +020042/**
43 * tcp_out_of_resources() - Close socket if out of resources
44 * @sk: pointer to current socket
45 * @do_reset: send a last packet with reset flag
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 *
Richard Sailerc380d372016-07-16 04:04:34 +020047 * Do not allow orphaned sockets to eat all our resources.
48 * This is direct violation of TCP specs, but it is required
49 * to prevent DoS attacks. It is called when a retransmission timeout
50 * or zero probe timeout occurs on orphaned socket.
51 *
Dan Streetman4ee806d2018-01-18 16:14:26 -050052 * Also close if our net namespace is exiting; in that case there is no
53 * hope of ever communicating again since all netns interfaces are already
54 * down (or about to be down), and we need to release our dst references,
55 * which have been moved to the netns loopback interface, so the namespace
56 * can finish exiting. This condition is only possible if we are a kernel
57 * socket, as those do not hold references to the namespace.
58 *
Richard Sailerc380d372016-07-16 04:04:34 +020059 * Criteria is still not confirmed experimentally and may change.
60 * We kill the socket, if:
61 * 1. If number of orphaned sockets exceeds an administratively configured
62 * limit.
63 * 2. If we have strong memory pressure.
Dan Streetman4ee806d2018-01-18 16:14:26 -050064 * 3. If our net namespace is exiting.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Yuchung Chengb2482302014-09-29 13:20:38 -070066static int tcp_out_of_resources(struct sock *sk, bool do_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerad1af0f2010-08-25 02:27:49 -070069 int shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090071 /* If peer does not open window for long time, or did not transmit
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * anything for long time, penalize it. */
Eric Dumazetd635fbe2017-05-16 14:00:03 -070073 if ((s32)(tcp_jiffies32 - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
David S. Millerad1af0f2010-08-25 02:27:49 -070074 shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 /* If some dubious ICMP arrived, penalize even more. */
77 if (sk->sk_err_soft)
David S. Millerad1af0f2010-08-25 02:27:49 -070078 shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Arun Sharmaefcdbf22012-01-30 14:16:06 -080080 if (tcp_check_oom(sk, shift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /* Catch exceptional cases, when connection requires reset.
82 * 1. Last segment was sent recently. */
Eric Dumazetd635fbe2017-05-16 14:00:03 -070083 if ((s32)(tcp_jiffies32 - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 /* 2. Window is closed. */
85 (!tp->snd_wnd && !tp->packets_out))
Yuchung Chengb2482302014-09-29 13:20:38 -070086 do_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (do_reset)
88 tcp_send_active_reset(sk, GFP_ATOMIC);
89 tcp_done(sk);
Eric Dumazet02a1d6e2016-04-27 16:44:39 -070090 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 1;
92 }
Dan Streetman4ee806d2018-01-18 16:14:26 -050093
94 if (!check_net(sock_net(sk))) {
95 /* Not possible to send reset; just close */
96 tcp_done(sk);
97 return 1;
98 }
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101}
102
Richard Sailerc380d372016-07-16 04:04:34 +0200103/**
104 * tcp_orphan_retries() - Returns maximal number of retries on an orphaned socket
105 * @sk: Pointer to the current socket.
106 * @alive: bool, socket alive state
107 */
Richard Sailer7533ce32015-10-09 02:41:37 +0200108static int tcp_orphan_retries(struct sock *sk, bool alive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Nikolay Borisovc402d9b2016-02-03 09:46:55 +0200110 int retries = sock_net(sk)->ipv4.sysctl_tcp_orphan_retries; /* May be zero. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /* We know from an ICMP that something is wrong. */
113 if (sk->sk_err_soft && !alive)
114 retries = 0;
115
116 /* However, if socket sent something recently, select some safe
117 * number of retries. 8 corresponds to >100 seconds with minimal
118 * RTO of 200msec. */
119 if (retries == 0 && alive)
120 retries = 8;
121 return retries;
122}
123
Eric Dumazetce55dd32007-12-21 01:50:43 -0800124static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
125{
Eric Dumazetd0f36842017-11-03 06:09:17 -0700126 const struct net *net = sock_net(sk);
127 int mss;
Fan Dub0f9ca52015-02-10 09:53:16 +0800128
Eric Dumazetce55dd32007-12-21 01:50:43 -0800129 /* Black hole detection */
Eric Dumazetd0f36842017-11-03 06:09:17 -0700130 if (!net->ipv4.sysctl_tcp_mtu_probing)
131 return;
David S. Miller829942c2007-12-21 04:29:16 -0800132
Eric Dumazetd0f36842017-11-03 06:09:17 -0700133 if (!icsk->icsk_mtup.enabled) {
134 icsk->icsk_mtup.enabled = 1;
135 icsk->icsk_mtup.probe_timestamp = tcp_jiffies32;
136 } else {
137 mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1;
138 mss = min(net->ipv4.sysctl_tcp_base_mss, mss);
139 mss = max(mss, 68 - tcp_sk(sk)->tcp_header_len);
140 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
Eric Dumazetce55dd32007-12-21 01:50:43 -0800141 }
Eric Dumazetd0f36842017-11-03 06:09:17 -0700142 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetce55dd32007-12-21 01:50:43 -0800143}
144
Richard Sailerc380d372016-07-16 04:04:34 +0200145
146/**
147 * retransmits_timed_out() - returns true if this connection has timed out
148 * @sk: The current socket
149 * @boundary: max number of retransmissions
150 * @timeout: A custom timeout value.
151 * If set to 0 the default timeout is calculated and used.
152 * Using TCP_RTO_MIN and the number of unsuccessful retransmits.
Richard Sailerc380d372016-07-16 04:04:34 +0200153 *
154 * The default "timeout" value this function can calculate and use
155 * is equivalent to the timeout of a TCP Connection
156 * after "boundary" unsuccessful, exponentially backed-off
Eric Dumazetce682ef2017-05-23 12:38:35 -0700157 * retransmissions with an initial RTO of TCP_RTO_MIN.
Damian Lukowski2f7de572009-12-07 06:06:16 +0000158 */
159static bool retransmits_timed_out(struct sock *sk,
Jerry Chudca43c72010-08-27 19:13:28 +0000160 unsigned int boundary,
Eric Dumazetce682ef2017-05-23 12:38:35 -0700161 unsigned int timeout)
Damian Lukowski2f7de572009-12-07 06:06:16 +0000162{
Eric Dumazetce682ef2017-05-23 12:38:35 -0700163 const unsigned int rto_base = TCP_RTO_MIN;
Eric Dumazet9a568de2017-05-16 14:00:14 -0700164 unsigned int linear_backoff_thresh, start_ts;
Damian Lukowski2f7de572009-12-07 06:06:16 +0000165
166 if (!inet_csk(sk)->icsk_retransmits)
167 return false;
168
Eric Dumazet7faee5c2014-09-05 15:33:33 -0700169 start_ts = tcp_sk(sk)->retrans_stamp;
Eric Dumazet75c119a2017-10-05 22:21:27 -0700170 if (unlikely(!start_ts)) {
171 struct sk_buff *head = tcp_rtx_queue_head(sk);
172
173 if (!head)
174 return false;
175 start_ts = tcp_skb_timestamp(head);
176 }
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);
Lawrence Brakmo3acf3ec2016-09-27 19:03:37 -0700202 } else if (!tp->syn_data && !tp->syn_fastopen) {
203 sk_rethink_txhash(sk);
Yuchung Chengc9686012013-10-29 10:09:05 -0700204 }
Nikolay Borisov6fa25162016-02-03 09:46:49 +0200205 retry_until = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
Eric Dumazetce682ef2017-05-23 12:38:35 -0700206 expired = icsk->icsk_retransmits >= retry_until;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 } else {
Eric Dumazetce682ef2017-05-23 12:38:35 -0700208 if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1, 0)) {
John Heffner5d424d52006-03-20 17:53:41 -0800209 /* Black hole detection */
Eric Dumazetce55dd32007-12-21 01:50:43 -0800210 tcp_mtu_probing(icsk, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Eric Dumazetb6c67122010-04-08 23:03:29 +0000212 dst_negative_advice(sk);
Lawrence Brakmo3acf3ec2016-09-27 19:03:37 -0700213 } else {
214 sk_rethink_txhash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Nikolay Borisovc6214a92016-02-03 09:46:54 +0200217 retry_until = net->ipv4.sysctl_tcp_retries2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (sock_flag(sk, SOCK_DEAD)) {
Richard Sailer7533ce32015-10-09 02:41:37 +0200219 const bool alive = icsk->icsk_rto < TCP_RTO_MAX;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 retry_until = tcp_orphan_retries(sk, alive);
Damian Lukowski6fa12c82009-08-26 00:16:34 +0000222 do_reset = alive ||
Eric Dumazetce682ef2017-05-23 12:38:35 -0700223 !retransmits_timed_out(sk, retry_until, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Damian Lukowski6fa12c82009-08-26 00:16:34 +0000225 if (tcp_out_of_resources(sk, do_reset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return 1;
227 }
Eric Dumazetce682ef2017-05-23 12:38:35 -0700228 expired = retransmits_timed_out(sk, retry_until,
229 icsk->icsk_user_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Yuchung Cheng72685862017-12-12 13:10:40 -0800231 tcp_fastopen_active_detect_blackhole(sk, expired);
Lawrence Brakmof89013f2018-01-25 16:14:11 -0800232
233 if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RTO_CB_FLAG))
234 tcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RTO_CB,
235 icsk->icsk_retransmits,
236 icsk->icsk_rto, (int)expired);
237
Eric Dumazetce682ef2017-05-23 12:38:35 -0700238 if (expired) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* Has it gone just too far? */
240 tcp_write_err(sk);
241 return 1;
242 }
Lawrence Brakmof89013f2018-01-25 16:14:11 -0800243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245}
246
Eric Dumazetc10d9312016-04-29 14:16:47 -0700247/* Called with BH disabled */
Eric Dumazet6f458df2012-07-20 05:45:50 +0000248void tcp_delack_timer_handler(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700250 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David S. Miller9993e7d2008-01-10 21:56:38 -0800252 sk_mem_reclaim_partial(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Eric Dumazet02b2faa2017-03-03 14:08:21 -0800254 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
255 !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 goto out;
257
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700258 if (time_after(icsk->icsk_ack.timeout, jiffies)) {
259 sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto out;
261 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700262 icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700264 if (inet_csk_ack_scheduled(sk)) {
265 if (!icsk->icsk_ack.pingpong) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* Delayed ACK missed: inflate ATO. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700267 icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 } else {
269 /* Delayed ACK missed: leave pingpong mode and
270 * deflate ATO.
271 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700272 icsk->icsk_ack.pingpong = 0;
273 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Eric Dumazet4688eb72017-12-12 18:22:52 -0800275 tcp_mstamp_refresh(tcp_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 tcp_send_ack(sk);
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700277 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280out:
Eric Dumazetb8da51e2015-05-15 12:39:27 -0700281 if (tcp_under_memory_pressure(sk))
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800282 sk_mem_reclaim(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000283}
284
Richard Sailerc380d372016-07-16 04:04:34 +0200285
286/**
287 * tcp_delack_timer() - The TCP delayed ACK timeout handler
288 * @data: Pointer to the current socket. (gets casted to struct sock *)
289 *
290 * This function gets (indirectly) called when the kernel timer for a TCP packet
291 * of this socket expires. Calls tcp_delack_timer_handler() to do the actual work.
292 *
293 * Returns: Nothing (void)
294 */
Kees Cook59f379f2017-10-16 17:29:19 -0700295static void tcp_delack_timer(struct timer_list *t)
Eric Dumazet6f458df2012-07-20 05:45:50 +0000296{
Kees Cook59f379f2017-10-16 17:29:19 -0700297 struct inet_connection_sock *icsk =
298 from_timer(icsk, t, icsk_delack_timer);
299 struct sock *sk = &icsk->icsk_inet.sk;
Eric Dumazet6f458df2012-07-20 05:45:50 +0000300
301 bh_lock_sock(sk);
302 if (!sock_owned_by_user(sk)) {
303 tcp_delack_timer_handler(sk);
304 } else {
Kees Cook59f379f2017-10-16 17:29:19 -0700305 icsk->icsk_ack.blocked = 1;
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700306 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000307 /* deleguate our work to tcp_release_cb() */
Eric Dumazet7aa54702016-12-03 11:14:57 -0800308 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &sk->sk_tsq_flags))
Eric Dumazet144d56e2012-08-20 00:22:46 +0000309 sock_hold(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 bh_unlock_sock(sk);
312 sock_put(sk);
313}
314
315static void tcp_probe_timer(struct sock *sk)
316{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300317 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet75c119a2017-10-05 22:21:27 -0700318 struct sk_buff *skb = tcp_send_head(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct tcp_sock *tp = tcp_sk(sk);
320 int max_probes;
Yuchung Chengb2482302014-09-29 13:20:38 -0700321 u32 start_ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Eric Dumazet75c119a2017-10-05 22:21:27 -0700323 if (tp->packets_out || !skb) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300324 icsk->icsk_probes_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return;
326 }
327
Yuchung Chengb2482302014-09-29 13:20:38 -0700328 /* RFC 1122 4.2.2.17 requires the sender to stay open indefinitely as
329 * long as the receiver continues to respond probes. We support this by
330 * default and reset icsk_probes_out with incoming ACKs. But if the
331 * socket is orphaned or the user specifies TCP_USER_TIMEOUT, we
332 * kill the socket when the retry count and the time exceeds the
333 * corresponding system limit. We also implement similar policy when
334 * we use RTO to probe window in tcp_retransmit_timer().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
Eric Dumazet75c119a2017-10-05 22:21:27 -0700336 start_ts = tcp_skb_timestamp(skb);
Yuchung Chengb2482302014-09-29 13:20:38 -0700337 if (!start_ts)
Eric Dumazet75c119a2017-10-05 22:21:27 -0700338 skb->skb_mstamp = tp->tcp_mstamp;
Yuchung Chengb2482302014-09-29 13:20:38 -0700339 else if (icsk->icsk_user_timeout &&
Eric Dumazet4ab68872017-05-21 10:39:00 -0700340 (s32)(tcp_time_stamp(tp) - start_ts) >
341 jiffies_to_msecs(icsk->icsk_user_timeout))
Yuchung Chengb2482302014-09-29 13:20:38 -0700342 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Nikolay Borisovc6214a92016-02-03 09:46:54 +0200344 max_probes = sock_net(sk)->ipv4.sysctl_tcp_retries2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (sock_flag(sk, SOCK_DEAD)) {
Richard Sailer7533ce32015-10-09 02:41:37 +0200346 const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 max_probes = tcp_orphan_retries(sk, alive);
Yuchung Chengb2482302014-09-29 13:20:38 -0700349 if (!alive && icsk->icsk_backoff >= max_probes)
350 goto abort;
351 if (tcp_out_of_resources(sk, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return;
353 }
354
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300355 if (icsk->icsk_probes_out > max_probes) {
Yuchung Chengb2482302014-09-29 13:20:38 -0700356abort: tcp_write_err(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 } else {
358 /* Only send another probe if we didn't close things up. */
359 tcp_send_probe0(sk);
360 }
361}
362
363/*
Jerry Chu83368862012-08-31 12:29:12 +0000364 * Timer for Fast Open socket to retransmit SYNACK. Note that the
365 * sk here is the child socket, not the parent (listener) socket.
366 */
367static void tcp_fastopen_synack_timer(struct sock *sk)
368{
369 struct inet_connection_sock *icsk = inet_csk(sk);
370 int max_retries = icsk->icsk_syn_retries ? :
Nikolay Borisov7c083ec2016-02-03 09:46:50 +0200371 sock_net(sk)->ipv4.sysctl_tcp_synack_retries + 1; /* add one more retry for fastopen */
Jerry Chu83368862012-08-31 12:29:12 +0000372 struct request_sock *req;
373
374 req = tcp_sk(sk)->fastopen_rsk;
Eric Dumazet42cb80a2015-03-22 10:22:19 -0700375 req->rsk_ops->syn_ack_timeout(req);
Jerry Chu83368862012-08-31 12:29:12 +0000376
Eric Dumazete6c022a2012-10-27 23:16:46 +0000377 if (req->num_timeout >= max_retries) {
Jerry Chu83368862012-08-31 12:29:12 +0000378 tcp_write_err(sk);
379 return;
380 }
381 /* XXX (TFO) - Unlike regular SYN-ACK retransmit, we ignore error
382 * returned from rtx_syn_ack() to make it more persistent like
383 * regular retransmit because if the child socket has been accepted
384 * it's not good to give up too easily.
385 */
Eric Dumazete6c022a2012-10-27 23:16:46 +0000386 inet_rtx_syn_ack(sk, req);
387 req->num_timeout++;
Yuchung Cheng7e32b442016-09-21 16:16:15 -0700388 icsk->icsk_retransmits++;
Jerry Chu83368862012-08-31 12:29:12 +0000389 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
Eric Dumazete6c022a2012-10-27 23:16:46 +0000390 TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
Jerry Chu83368862012-08-31 12:29:12 +0000391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Richard Sailerc380d372016-07-16 04:04:34 +0200394/**
395 * tcp_retransmit_timer() - The TCP retransmit timeout handler
396 * @sk: Pointer to the current socket.
397 *
398 * This function gets called when the kernel timer for a TCP packet
399 * of this socket expires.
400 *
401 * It handles retransmission, timer adjustment and other necesarry measures.
402 *
403 * Returns: Nothing (void)
404 */
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000405void tcp_retransmit_timer(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisovae5c3f42016-02-03 09:46:53 +0200408 struct net *net = sock_net(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700409 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Jerry Chu83368862012-08-31 12:29:12 +0000411 if (tp->fastopen_rsk) {
Jerry Chu37561f62012-10-22 11:26:36 +0000412 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
413 sk->sk_state != TCP_FIN_WAIT1);
Jerry Chu83368862012-08-31 12:29:12 +0000414 tcp_fastopen_synack_timer(sk);
415 /* Before we receive ACK to our SYN-ACK don't retransmit
416 * anything else (e.g., data or FIN segments).
417 */
418 return;
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!tp->packets_out)
421 goto out;
422
Eric Dumazet75c119a2017-10-05 22:21:27 -0700423 WARN_ON(tcp_rtx_queue_empty(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Nandita Dukkipati9b717a82013-03-11 10:00:44 +0000425 tp->tlp_high_seq = 0;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
428 !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
429 /* Receiver dastardly shrinks window. Our retransmits
430 * become zero probes, but we should not timeout this
431 * connection. If the socket is an orphan, time it out,
432 * we cannot allow such beasts to hang infinitely.
433 */
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700434 struct inet_sock *inet = inet_sk(sk);
435 if (sk->sk_family == AF_INET) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800436 net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
437 &inet->inet_daddr,
438 ntohs(inet->inet_dport),
439 inet->inet_num,
440 tp->snd_una, tp->snd_nxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000442#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700443 else if (sk->sk_family == AF_INET6) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800444 net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
445 &sk->sk_v6_daddr,
446 ntohs(inet->inet_dport),
447 inet->inet_num,
448 tp->snd_una, tp->snd_nxt);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700449 }
450#endif
Eric Dumazet70eabf02017-05-16 14:00:07 -0700451 if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 tcp_write_err(sk);
453 goto out;
454 }
Neal Cardwell5ae344c2014-08-04 19:12:29 -0400455 tcp_enter_loss(sk);
Eric Dumazet75c119a2017-10-05 22:21:27 -0700456 tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 __sk_dst_reset(sk);
458 goto out_reset_timer;
459 }
460
461 if (tcp_write_timeout(sk))
462 goto out;
463
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700464 if (icsk->icsk_retransmits == 0) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700465 int mib_idx;
466
Ilpo Järvinenc60ce4e2010-10-14 01:52:09 +0000467 if (icsk->icsk_ca_state == TCP_CA_Recovery) {
Ilpo Järvinenbc079e92009-02-28 04:44:34 +0000468 if (tcp_is_sack(tp))
469 mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
470 else
471 mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300472 } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700473 mib_idx = LINUX_MIB_TCPLOSSFAILURES;
Ilpo Järvinenc60ce4e2010-10-14 01:52:09 +0000474 } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
475 tp->sacked_out) {
476 if (tcp_is_sack(tp))
477 mib_idx = LINUX_MIB_TCPSACKFAILURES;
478 else
479 mib_idx = LINUX_MIB_TCPRENOFAILURES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 } else {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700481 mib_idx = LINUX_MIB_TCPTIMEOUTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700483 __NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
Neal Cardwell5ae344c2014-08-04 19:12:29 -0400486 tcp_enter_loss(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Eric Dumazet75c119a2017-10-05 22:21:27 -0700488 if (tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* Retransmission failed because of local congestion,
490 * do not backoff.
491 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700492 if (!icsk->icsk_retransmits)
493 icsk->icsk_retransmits = 1;
494 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700495 min(icsk->icsk_rto, TCP_RESOURCE_PROBE_INTERVAL),
496 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 goto out;
498 }
499
500 /* Increase the timeout each time we retransmit. Note that
501 * we do not increase the rtt estimate. rto is initialized
502 * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests
503 * that doubling rto each time is the least we can get away with.
504 * In KA9Q, Karn uses this for the first few times, and then
505 * goes to quadratic. netBSD doubles, but only goes up to *64,
506 * and clamps at 1 to 64 sec afterwards. Note that 120 sec is
507 * defined in the protocol as the maximum possible RTT. I guess
508 * we'll have to use something other than TCP to talk to the
509 * University of Mars.
510 *
511 * PAWS allows us longer timeouts and large windows, so once
512 * implemented ftp to mars will work nicely. We will have to fix
513 * the 120 second clamps though!
514 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700515 icsk->icsk_backoff++;
516 icsk->icsk_retransmits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518out_reset_timer:
Andreas Petlund36e31b0a2010-02-18 02:47:01 +0000519 /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is
520 * used to reset timer, set to 0. Recalculate 'icsk_rto' as this
521 * might be increased if the stream oscillates between thin and thick,
522 * thus the old value might already be too high compared to the value
523 * set by 'tcp_set_rto' in tcp_input.c which resets the rto without
524 * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating
525 * exponential backoff behaviour to avoid continue hammering
526 * linear-timeout retransmissions into a black hole
527 */
528 if (sk->sk_state == TCP_ESTABLISHED &&
Eric Dumazet2c04ac82017-10-26 21:54:58 -0700529 (tp->thin_lto || net->ipv4.sysctl_tcp_thin_linear_timeouts) &&
Andreas Petlund36e31b0a2010-02-18 02:47:01 +0000530 tcp_stream_is_thin(tp) &&
531 icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
532 icsk->icsk_backoff = 0;
533 icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX);
534 } else {
535 /* Use normal (exponential) backoff */
536 icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
537 }
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700538 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX);
Eric Dumazetce682ef2017-05-23 12:38:35 -0700539 if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1 + 1, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 __sk_dst_reset(sk);
541
542out:;
543}
544
Richard Sailerc380d372016-07-16 04:04:34 +0200545/* Called with bottom-half processing disabled.
546 Called by tcp_write_timer() */
Eric Dumazet6f458df2012-07-20 05:45:50 +0000547void tcp_write_timer_handler(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700549 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 int event;
551
Eric Dumazet02b2faa2017-03-03 14:08:21 -0800552 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
553 !icsk->icsk_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto out;
555
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700556 if (time_after(icsk->icsk_timeout, jiffies)) {
557 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto out;
559 }
560
Eric Dumazet9a568de2017-05-16 14:00:14 -0700561 tcp_mstamp_refresh(tcp_sk(sk));
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700562 event = icsk->icsk_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 switch (event) {
Yuchung Cheng57dde7f2017-01-12 22:11:33 -0800565 case ICSK_TIME_REO_TIMEOUT:
566 tcp_rack_reo_timeout(sk);
567 break;
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000568 case ICSK_TIME_LOSS_PROBE:
569 tcp_send_loss_probe(sk);
570 break;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700571 case ICSK_TIME_RETRANS:
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000572 icsk->icsk_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 tcp_retransmit_timer(sk);
574 break;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700575 case ICSK_TIME_PROBE0:
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000576 icsk->icsk_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 tcp_probe_timer(sk);
578 break;
579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581out:
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800582 sk_mem_reclaim(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000583}
584
Kees Cook59f379f2017-10-16 17:29:19 -0700585static void tcp_write_timer(struct timer_list *t)
Eric Dumazet6f458df2012-07-20 05:45:50 +0000586{
Kees Cook59f379f2017-10-16 17:29:19 -0700587 struct inet_connection_sock *icsk =
588 from_timer(icsk, t, icsk_retransmit_timer);
589 struct sock *sk = &icsk->icsk_inet.sk;
Eric Dumazet6f458df2012-07-20 05:45:50 +0000590
591 bh_lock_sock(sk);
592 if (!sock_owned_by_user(sk)) {
593 tcp_write_timer_handler(sk);
594 } else {
Richard Sailerc380d372016-07-16 04:04:34 +0200595 /* delegate our work to tcp_release_cb() */
Eric Dumazet7aa54702016-12-03 11:14:57 -0800596 if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &sk->sk_tsq_flags))
Eric Dumazet144d56e2012-08-20 00:22:46 +0000597 sock_hold(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 bh_unlock_sock(sk);
600 sock_put(sk);
601}
602
Eric Dumazet42cb80a2015-03-22 10:22:19 -0700603void tcp_syn_ack_timeout(const struct request_sock *req)
Octavian Purdila72659ec2010-01-17 19:09:39 -0800604{
Eric Dumazet42cb80a2015-03-22 10:22:19 -0700605 struct net *net = read_pnet(&inet_rsk(req)->ireq_net);
606
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700607 __NET_INC_STATS(net, LINUX_MIB_TCPTIMEOUTS);
Octavian Purdila72659ec2010-01-17 19:09:39 -0800608}
609EXPORT_SYMBOL(tcp_syn_ack_timeout);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611void tcp_set_keepalive(struct sock *sk, int val)
612{
613 if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
614 return;
615
616 if (val && !sock_flag(sk, SOCK_KEEPOPEN))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700617 inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 else if (!val)
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700619 inet_csk_delete_keepalive_timer(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
Ursula Braun4b9d07a2017-01-09 16:55:12 +0100621EXPORT_SYMBOL_GPL(tcp_set_keepalive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623
Kees Cook59f379f2017-10-16 17:29:19 -0700624static void tcp_keepalive_timer (struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Kees Cook59f379f2017-10-16 17:29:19 -0700626 struct sock *sk = from_timer(sk, t, sk_timer);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300627 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct tcp_sock *tp = tcp_sk(sk);
Flavio Leitner6c37e5d2010-04-26 18:33:27 +0000629 u32 elapsed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 /* Only process if socket is not in use. */
632 bh_lock_sock(sk);
633 if (sock_owned_by_user(sk)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900634 /* Try again later. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700635 inet_csk_reset_keepalive_timer (sk, HZ/20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 goto out;
637 }
638
639 if (sk->sk_state == TCP_LISTEN) {
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700640 pr_err("Hmm... keepalive on a LISTEN ???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 goto out;
642 }
643
Eric Dumazet4688eb72017-12-12 18:22:52 -0800644 tcp_mstamp_refresh(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) {
646 if (tp->linger2 >= 0) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700647 const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 if (tmo > 0) {
650 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
651 goto out;
652 }
653 }
654 tcp_send_active_reset(sk, GFP_ATOMIC);
655 goto death;
656 }
657
Eric Dumazet2dda6402017-08-02 23:10:46 -0700658 if (!sock_flag(sk, SOCK_KEEPOPEN) ||
659 ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 goto out;
661
662 elapsed = keepalive_time_when(tp);
663
664 /* It is alive without keepalive 8) */
Eric Dumazet75c119a2017-10-05 22:21:27 -0700665 if (tp->packets_out || !tcp_write_queue_empty(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 goto resched;
667
Flavio Leitner6c37e5d2010-04-26 18:33:27 +0000668 elapsed = keepalive_time_elapsed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 if (elapsed >= keepalive_time_when(tp)) {
Jerry Chudca43c72010-08-27 19:13:28 +0000671 /* If the TCP_USER_TIMEOUT option is enabled, use that
672 * to determine when to timeout instead.
673 */
674 if ((icsk->icsk_user_timeout != 0 &&
675 elapsed >= icsk->icsk_user_timeout &&
676 icsk->icsk_probes_out > 0) ||
677 (icsk->icsk_user_timeout == 0 &&
678 icsk->icsk_probes_out >= keepalive_probes(tp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 tcp_send_active_reset(sk, GFP_ATOMIC);
680 tcp_write_err(sk);
681 goto out;
682 }
Eric Dumazete520af42015-05-06 14:26:25 -0700683 if (tcp_write_wakeup(sk, LINUX_MIB_TCPKEEPALIVE) <= 0) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300684 icsk->icsk_probes_out++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 elapsed = keepalive_intvl_when(tp);
686 } else {
687 /* If keepalive was lost due to local congestion,
688 * try harder.
689 */
690 elapsed = TCP_RESOURCE_PROBE_INTERVAL;
691 }
692 } else {
693 /* It is tp->rcv_tstamp + keepalive_time_when(tp) */
694 elapsed = keepalive_time_when(tp) - elapsed;
695 }
696
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800697 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699resched:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700700 inet_csk_reset_keepalive_timer (sk, elapsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 goto out;
702
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900703death:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 tcp_done(sk);
705
706out:
707 bh_unlock_sock(sk);
708 sock_put(sk);
709}
Eric Dumazet6f458df2012-07-20 05:45:50 +0000710
711void tcp_init_xmit_timers(struct sock *sk)
712{
713 inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
714 &tcp_keepalive_timer);
Eric Dumazet218af592017-05-16 04:24:36 -0700715 hrtimer_init(&tcp_sk(sk)->pacing_timer, CLOCK_MONOTONIC,
Eric Dumazet73a6bab2018-05-10 14:59:43 -0700716 HRTIMER_MODE_ABS_PINNED_SOFT);
Eric Dumazet218af592017-05-16 04:24:36 -0700717 tcp_sk(sk)->pacing_timer.function = tcp_pace_kick;
Eric Dumazet6f458df2012-07-20 05:45:50 +0000718}