blob: 091c53925e4da6b2b154d166682a0ac0aefd7ecb [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
Wei Yongjun55477202018-07-25 06:06:07 +000025static u32 tcp_retransmit_stamp(const struct sock *sk)
Jon Maxwella7fa3772018-07-19 11:14:43 +100026{
27 u32 start_ts = tcp_sk(sk)->retrans_stamp;
28
29 if (unlikely(!start_ts)) {
30 struct sk_buff *head = tcp_rtx_queue_head(sk);
31
32 if (!head)
33 return 0;
34 start_ts = tcp_skb_timestamp(head);
35 }
36 return start_ts;
37}
38
Jon Maxwellb701a992018-07-19 11:14:44 +100039static u32 tcp_clamp_rto_to_user_timeout(const struct sock *sk)
40{
41 struct inet_connection_sock *icsk = inet_csk(sk);
42 u32 elapsed, start_ts;
Eric Dumazet9efdda42018-11-24 09:12:24 -080043 s32 remaining;
Jon Maxwellb701a992018-07-19 11:14:44 +100044
45 start_ts = tcp_retransmit_stamp(sk);
46 if (!icsk->icsk_user_timeout || !start_ts)
47 return icsk->icsk_rto;
48 elapsed = tcp_time_stamp(tcp_sk(sk)) - start_ts;
Eric Dumazet9efdda42018-11-24 09:12:24 -080049 remaining = icsk->icsk_user_timeout - elapsed;
50 if (remaining <= 0)
Jon Maxwellb701a992018-07-19 11:14:44 +100051 return 1; /* user timeout has passed; fire ASAP */
Eric Dumazet9efdda42018-11-24 09:12:24 -080052
53 return min_t(u32, icsk->icsk_rto, msecs_to_jiffies(remaining));
Jon Maxwellb701a992018-07-19 11:14:44 +100054}
55
Richard Sailerc380d372016-07-16 04:04:34 +020056/**
57 * tcp_write_err() - close socket and save error info
58 * @sk: The socket the error has appeared on.
59 *
60 * Returns: Nothing (void)
61 */
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void tcp_write_err(struct sock *sk)
64{
65 sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
66 sk->sk_error_report(sk);
67
Soheil Hassas Yeganehe05836a2018-03-06 17:15:12 -050068 tcp_write_queue_purge(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 tcp_done(sk);
Eric Dumazet02a1d6e2016-04-27 16:44:39 -070070 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Richard Sailerc380d372016-07-16 04:04:34 +020073/**
74 * tcp_out_of_resources() - Close socket if out of resources
75 * @sk: pointer to current socket
76 * @do_reset: send a last packet with reset flag
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
Richard Sailerc380d372016-07-16 04:04:34 +020078 * Do not allow orphaned sockets to eat all our resources.
79 * This is direct violation of TCP specs, but it is required
80 * to prevent DoS attacks. It is called when a retransmission timeout
81 * or zero probe timeout occurs on orphaned socket.
82 *
Dan Streetman4ee806d2018-01-18 16:14:26 -050083 * Also close if our net namespace is exiting; in that case there is no
84 * hope of ever communicating again since all netns interfaces are already
85 * down (or about to be down), and we need to release our dst references,
86 * which have been moved to the netns loopback interface, so the namespace
87 * can finish exiting. This condition is only possible if we are a kernel
88 * socket, as those do not hold references to the namespace.
89 *
Richard Sailerc380d372016-07-16 04:04:34 +020090 * Criteria is still not confirmed experimentally and may change.
91 * We kill the socket, if:
92 * 1. If number of orphaned sockets exceeds an administratively configured
93 * limit.
94 * 2. If we have strong memory pressure.
Dan Streetman4ee806d2018-01-18 16:14:26 -050095 * 3. If our net namespace is exiting.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Yuchung Chengb2482302014-09-29 13:20:38 -070097static int tcp_out_of_resources(struct sock *sk, bool do_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerad1af0f2010-08-25 02:27:49 -0700100 int shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900102 /* If peer does not open window for long time, or did not transmit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * anything for long time, penalize it. */
Eric Dumazetd635fbe2017-05-16 14:00:03 -0700104 if ((s32)(tcp_jiffies32 - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
David S. Millerad1af0f2010-08-25 02:27:49 -0700105 shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /* If some dubious ICMP arrived, penalize even more. */
108 if (sk->sk_err_soft)
David S. Millerad1af0f2010-08-25 02:27:49 -0700109 shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Arun Sharmaefcdbf22012-01-30 14:16:06 -0800111 if (tcp_check_oom(sk, shift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* Catch exceptional cases, when connection requires reset.
113 * 1. Last segment was sent recently. */
Eric Dumazetd635fbe2017-05-16 14:00:03 -0700114 if ((s32)(tcp_jiffies32 - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /* 2. Window is closed. */
116 (!tp->snd_wnd && !tp->packets_out))
Yuchung Chengb2482302014-09-29 13:20:38 -0700117 do_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (do_reset)
119 tcp_send_active_reset(sk, GFP_ATOMIC);
120 tcp_done(sk);
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700121 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return 1;
123 }
Dan Streetman4ee806d2018-01-18 16:14:26 -0500124
125 if (!check_net(sock_net(sk))) {
126 /* Not possible to send reset; just close */
127 tcp_done(sk);
128 return 1;
129 }
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return 0;
132}
133
Richard Sailerc380d372016-07-16 04:04:34 +0200134/**
135 * tcp_orphan_retries() - Returns maximal number of retries on an orphaned socket
136 * @sk: Pointer to the current socket.
137 * @alive: bool, socket alive state
138 */
Richard Sailer7533ce32015-10-09 02:41:37 +0200139static int tcp_orphan_retries(struct sock *sk, bool alive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Nikolay Borisovc402d9b2016-02-03 09:46:55 +0200141 int retries = sock_net(sk)->ipv4.sysctl_tcp_orphan_retries; /* May be zero. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 /* We know from an ICMP that something is wrong. */
144 if (sk->sk_err_soft && !alive)
145 retries = 0;
146
147 /* However, if socket sent something recently, select some safe
148 * number of retries. 8 corresponds to >100 seconds with minimal
149 * RTO of 200msec. */
150 if (retries == 0 && alive)
151 retries = 8;
152 return retries;
153}
154
Eric Dumazetce55dd32007-12-21 01:50:43 -0800155static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
156{
Eric Dumazetd0f36842017-11-03 06:09:17 -0700157 const struct net *net = sock_net(sk);
158 int mss;
Fan Dub0f9ca52015-02-10 09:53:16 +0800159
Eric Dumazetce55dd32007-12-21 01:50:43 -0800160 /* Black hole detection */
Eric Dumazetd0f36842017-11-03 06:09:17 -0700161 if (!net->ipv4.sysctl_tcp_mtu_probing)
162 return;
David S. Miller829942c2007-12-21 04:29:16 -0800163
Eric Dumazetd0f36842017-11-03 06:09:17 -0700164 if (!icsk->icsk_mtup.enabled) {
165 icsk->icsk_mtup.enabled = 1;
166 icsk->icsk_mtup.probe_timestamp = tcp_jiffies32;
167 } else {
168 mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1;
169 mss = min(net->ipv4.sysctl_tcp_base_mss, mss);
170 mss = max(mss, 68 - tcp_sk(sk)->tcp_header_len);
171 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
Eric Dumazetce55dd32007-12-21 01:50:43 -0800172 }
Eric Dumazetd0f36842017-11-03 06:09:17 -0700173 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetce55dd32007-12-21 01:50:43 -0800174}
175
Richard Sailerc380d372016-07-16 04:04:34 +0200176
177/**
178 * retransmits_timed_out() - returns true if this connection has timed out
179 * @sk: The current socket
180 * @boundary: max number of retransmissions
181 * @timeout: A custom timeout value.
182 * If set to 0 the default timeout is calculated and used.
183 * Using TCP_RTO_MIN and the number of unsuccessful retransmits.
Richard Sailerc380d372016-07-16 04:04:34 +0200184 *
185 * The default "timeout" value this function can calculate and use
186 * is equivalent to the timeout of a TCP Connection
187 * after "boundary" unsuccessful, exponentially backed-off
Eric Dumazetce682ef2017-05-23 12:38:35 -0700188 * retransmissions with an initial RTO of TCP_RTO_MIN.
Damian Lukowski2f7de572009-12-07 06:06:16 +0000189 */
190static bool retransmits_timed_out(struct sock *sk,
Jerry Chudca43c72010-08-27 19:13:28 +0000191 unsigned int boundary,
Eric Dumazetce682ef2017-05-23 12:38:35 -0700192 unsigned int timeout)
Damian Lukowski2f7de572009-12-07 06:06:16 +0000193{
Eric Dumazetce682ef2017-05-23 12:38:35 -0700194 const unsigned int rto_base = TCP_RTO_MIN;
Eric Dumazet9a568de2017-05-16 14:00:14 -0700195 unsigned int linear_backoff_thresh, start_ts;
Damian Lukowski2f7de572009-12-07 06:06:16 +0000196
197 if (!inet_csk(sk)->icsk_retransmits)
198 return false;
199
Jon Maxwella7fa3772018-07-19 11:14:43 +1000200 start_ts = tcp_retransmit_stamp(sk);
201 if (!start_ts)
202 return false;
Damian Lukowski2f7de572009-12-07 06:06:16 +0000203
Jerry Chudca43c72010-08-27 19:13:28 +0000204 if (likely(timeout == 0)) {
David S. Miller21a180c2010-10-04 11:56:38 -0700205 linear_backoff_thresh = ilog2(TCP_RTO_MAX/rto_base);
Damian Lukowski2f7de572009-12-07 06:06:16 +0000206
Jerry Chudca43c72010-08-27 19:13:28 +0000207 if (boundary <= linear_backoff_thresh)
David S. Miller21a180c2010-10-04 11:56:38 -0700208 timeout = ((2 << boundary) - 1) * rto_base;
Jerry Chudca43c72010-08-27 19:13:28 +0000209 else
David S. Miller21a180c2010-10-04 11:56:38 -0700210 timeout = ((2 << linear_backoff_thresh) - 1) * rto_base +
Jerry Chudca43c72010-08-27 19:13:28 +0000211 (boundary - linear_backoff_thresh) * TCP_RTO_MAX;
Jon Maxwell9bcc66e2018-07-19 11:14:42 +1000212 timeout = jiffies_to_msecs(timeout);
Jerry Chudca43c72010-08-27 19:13:28 +0000213 }
Eric Dumazet9efdda42018-11-24 09:12:24 -0800214 return (s32)(tcp_time_stamp(tcp_sk(sk)) - start_ts - timeout) >= 0;
Damian Lukowski2f7de572009-12-07 06:06:16 +0000215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/* A write timeout has occurred. Process the after effects. */
218static int tcp_write_timeout(struct sock *sk)
219{
John Heffner5d424d52006-03-20 17:53:41 -0800220 struct inet_connection_sock *icsk = inet_csk(sk);
Yuchung Chengc9686012013-10-29 10:09:05 -0700221 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov6fa25162016-02-03 09:46:49 +0200222 struct net *net = sock_net(sk);
Eric Dumazetce682ef2017-05-23 12:38:35 -0700223 bool expired, do_reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 int retry_until;
225
226 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
Yuchung Chengc9686012013-10-29 10:09:05 -0700227 if (icsk->icsk_retransmits) {
Eric Dumazetb6c67122010-04-08 23:03:29 +0000228 dst_negative_advice(sk);
Lawrence Brakmo3acf3ec2016-09-27 19:03:37 -0700229 } else if (!tp->syn_data && !tp->syn_fastopen) {
230 sk_rethink_txhash(sk);
Yuchung Chengc9686012013-10-29 10:09:05 -0700231 }
Nikolay Borisov6fa25162016-02-03 09:46:49 +0200232 retry_until = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
Eric Dumazetce682ef2017-05-23 12:38:35 -0700233 expired = icsk->icsk_retransmits >= retry_until;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 } else {
Eric Dumazetce682ef2017-05-23 12:38:35 -0700235 if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1, 0)) {
John Heffner5d424d52006-03-20 17:53:41 -0800236 /* Black hole detection */
Eric Dumazetce55dd32007-12-21 01:50:43 -0800237 tcp_mtu_probing(icsk, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Eric Dumazetb6c67122010-04-08 23:03:29 +0000239 dst_negative_advice(sk);
Lawrence Brakmo3acf3ec2016-09-27 19:03:37 -0700240 } else {
241 sk_rethink_txhash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Nikolay Borisovc6214a92016-02-03 09:46:54 +0200244 retry_until = net->ipv4.sysctl_tcp_retries2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (sock_flag(sk, SOCK_DEAD)) {
Richard Sailer7533ce32015-10-09 02:41:37 +0200246 const bool alive = icsk->icsk_rto < TCP_RTO_MAX;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 retry_until = tcp_orphan_retries(sk, alive);
Damian Lukowski6fa12c82009-08-26 00:16:34 +0000249 do_reset = alive ||
Eric Dumazetce682ef2017-05-23 12:38:35 -0700250 !retransmits_timed_out(sk, retry_until, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Damian Lukowski6fa12c82009-08-26 00:16:34 +0000252 if (tcp_out_of_resources(sk, do_reset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 1;
254 }
Eric Dumazetce682ef2017-05-23 12:38:35 -0700255 expired = retransmits_timed_out(sk, retry_until,
256 icsk->icsk_user_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Yuchung Cheng72685862017-12-12 13:10:40 -0800258 tcp_fastopen_active_detect_blackhole(sk, expired);
Lawrence Brakmof89013f2018-01-25 16:14:11 -0800259
260 if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RTO_CB_FLAG))
261 tcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RTO_CB,
262 icsk->icsk_retransmits,
263 icsk->icsk_rto, (int)expired);
264
Eric Dumazetce682ef2017-05-23 12:38:35 -0700265 if (expired) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* Has it gone just too far? */
267 tcp_write_err(sk);
268 return 1;
269 }
Lawrence Brakmof89013f2018-01-25 16:14:11 -0800270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
272}
273
Eric Dumazetc10d9312016-04-29 14:16:47 -0700274/* Called with BH disabled */
Eric Dumazet6f458df2012-07-20 05:45:50 +0000275void tcp_delack_timer_handler(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700277 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
David S. Miller9993e7d2008-01-10 21:56:38 -0800279 sk_mem_reclaim_partial(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Eric Dumazet02b2faa2017-03-03 14:08:21 -0800281 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
282 !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 goto out;
284
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700285 if (time_after(icsk->icsk_ack.timeout, jiffies)) {
286 sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 goto out;
288 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700289 icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700291 if (inet_csk_ack_scheduled(sk)) {
292 if (!icsk->icsk_ack.pingpong) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* Delayed ACK missed: inflate ATO. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700294 icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 } else {
296 /* Delayed ACK missed: leave pingpong mode and
297 * deflate ATO.
298 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700299 icsk->icsk_ack.pingpong = 0;
300 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
Eric Dumazet4688eb72017-12-12 18:22:52 -0800302 tcp_mstamp_refresh(tcp_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 tcp_send_ack(sk);
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700304 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307out:
Eric Dumazetb8da51e2015-05-15 12:39:27 -0700308 if (tcp_under_memory_pressure(sk))
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800309 sk_mem_reclaim(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000310}
311
Richard Sailerc380d372016-07-16 04:04:34 +0200312
313/**
314 * tcp_delack_timer() - The TCP delayed ACK timeout handler
315 * @data: Pointer to the current socket. (gets casted to struct sock *)
316 *
317 * This function gets (indirectly) called when the kernel timer for a TCP packet
318 * of this socket expires. Calls tcp_delack_timer_handler() to do the actual work.
319 *
320 * Returns: Nothing (void)
321 */
Kees Cook59f379f2017-10-16 17:29:19 -0700322static void tcp_delack_timer(struct timer_list *t)
Eric Dumazet6f458df2012-07-20 05:45:50 +0000323{
Kees Cook59f379f2017-10-16 17:29:19 -0700324 struct inet_connection_sock *icsk =
325 from_timer(icsk, t, icsk_delack_timer);
326 struct sock *sk = &icsk->icsk_inet.sk;
Eric Dumazet6f458df2012-07-20 05:45:50 +0000327
328 bh_lock_sock(sk);
329 if (!sock_owned_by_user(sk)) {
330 tcp_delack_timer_handler(sk);
331 } else {
Kees Cook59f379f2017-10-16 17:29:19 -0700332 icsk->icsk_ack.blocked = 1;
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700333 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000334 /* deleguate our work to tcp_release_cb() */
Eric Dumazet7aa54702016-12-03 11:14:57 -0800335 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &sk->sk_tsq_flags))
Eric Dumazet144d56e2012-08-20 00:22:46 +0000336 sock_hold(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 bh_unlock_sock(sk);
339 sock_put(sk);
340}
341
342static void tcp_probe_timer(struct sock *sk)
343{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300344 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet75c119a2017-10-05 22:21:27 -0700345 struct sk_buff *skb = tcp_send_head(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct tcp_sock *tp = tcp_sk(sk);
347 int max_probes;
Yuchung Chengb2482302014-09-29 13:20:38 -0700348 u32 start_ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Eric Dumazet75c119a2017-10-05 22:21:27 -0700350 if (tp->packets_out || !skb) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300351 icsk->icsk_probes_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return;
353 }
354
Yuchung Chengb2482302014-09-29 13:20:38 -0700355 /* RFC 1122 4.2.2.17 requires the sender to stay open indefinitely as
356 * long as the receiver continues to respond probes. We support this by
357 * default and reset icsk_probes_out with incoming ACKs. But if the
358 * socket is orphaned or the user specifies TCP_USER_TIMEOUT, we
359 * kill the socket when the retry count and the time exceeds the
360 * corresponding system limit. We also implement similar policy when
361 * we use RTO to probe window in tcp_retransmit_timer().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
Eric Dumazet75c119a2017-10-05 22:21:27 -0700363 start_ts = tcp_skb_timestamp(skb);
Yuchung Chengb2482302014-09-29 13:20:38 -0700364 if (!start_ts)
Eric Dumazet5f6188a2018-10-15 09:37:52 -0700365 skb->skb_mstamp_ns = tp->tcp_clock_cache;
Yuchung Chengb2482302014-09-29 13:20:38 -0700366 else if (icsk->icsk_user_timeout &&
Jon Maxwell9bcc66e2018-07-19 11:14:42 +1000367 (s32)(tcp_time_stamp(tp) - start_ts) > icsk->icsk_user_timeout)
Yuchung Chengb2482302014-09-29 13:20:38 -0700368 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Nikolay Borisovc6214a92016-02-03 09:46:54 +0200370 max_probes = sock_net(sk)->ipv4.sysctl_tcp_retries2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (sock_flag(sk, SOCK_DEAD)) {
Richard Sailer7533ce32015-10-09 02:41:37 +0200372 const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 max_probes = tcp_orphan_retries(sk, alive);
Yuchung Chengb2482302014-09-29 13:20:38 -0700375 if (!alive && icsk->icsk_backoff >= max_probes)
376 goto abort;
377 if (tcp_out_of_resources(sk, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return;
379 }
380
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300381 if (icsk->icsk_probes_out > max_probes) {
Yuchung Chengb2482302014-09-29 13:20:38 -0700382abort: tcp_write_err(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 } else {
384 /* Only send another probe if we didn't close things up. */
385 tcp_send_probe0(sk);
386 }
387}
388
389/*
Jerry Chu83368862012-08-31 12:29:12 +0000390 * Timer for Fast Open socket to retransmit SYNACK. Note that the
391 * sk here is the child socket, not the parent (listener) socket.
392 */
393static void tcp_fastopen_synack_timer(struct sock *sk)
394{
395 struct inet_connection_sock *icsk = inet_csk(sk);
396 int max_retries = icsk->icsk_syn_retries ? :
Nikolay Borisov7c083ec2016-02-03 09:46:50 +0200397 sock_net(sk)->ipv4.sysctl_tcp_synack_retries + 1; /* add one more retry for fastopen */
Jerry Chu83368862012-08-31 12:29:12 +0000398 struct request_sock *req;
399
400 req = tcp_sk(sk)->fastopen_rsk;
Eric Dumazet42cb80a2015-03-22 10:22:19 -0700401 req->rsk_ops->syn_ack_timeout(req);
Jerry Chu83368862012-08-31 12:29:12 +0000402
Eric Dumazete6c022a2012-10-27 23:16:46 +0000403 if (req->num_timeout >= max_retries) {
Jerry Chu83368862012-08-31 12:29:12 +0000404 tcp_write_err(sk);
405 return;
406 }
407 /* XXX (TFO) - Unlike regular SYN-ACK retransmit, we ignore error
408 * returned from rtx_syn_ack() to make it more persistent like
409 * regular retransmit because if the child socket has been accepted
410 * it's not good to give up too easily.
411 */
Eric Dumazete6c022a2012-10-27 23:16:46 +0000412 inet_rtx_syn_ack(sk, req);
413 req->num_timeout++;
Yuchung Cheng7e32b442016-09-21 16:16:15 -0700414 icsk->icsk_retransmits++;
Jerry Chu83368862012-08-31 12:29:12 +0000415 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
Eric Dumazete6c022a2012-10-27 23:16:46 +0000416 TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
Jerry Chu83368862012-08-31 12:29:12 +0000417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Richard Sailerc380d372016-07-16 04:04:34 +0200420/**
421 * tcp_retransmit_timer() - The TCP retransmit timeout handler
422 * @sk: Pointer to the current socket.
423 *
424 * This function gets called when the kernel timer for a TCP packet
425 * of this socket expires.
426 *
427 * It handles retransmission, timer adjustment and other necesarry measures.
428 *
429 * Returns: Nothing (void)
430 */
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000431void tcp_retransmit_timer(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisovae5c3f42016-02-03 09:46:53 +0200434 struct net *net = sock_net(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700435 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jerry Chu83368862012-08-31 12:29:12 +0000437 if (tp->fastopen_rsk) {
Jerry Chu37561f62012-10-22 11:26:36 +0000438 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
439 sk->sk_state != TCP_FIN_WAIT1);
Jerry Chu83368862012-08-31 12:29:12 +0000440 tcp_fastopen_synack_timer(sk);
441 /* Before we receive ACK to our SYN-ACK don't retransmit
442 * anything else (e.g., data or FIN segments).
443 */
444 return;
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (!tp->packets_out)
447 goto out;
448
Eric Dumazet75c119a2017-10-05 22:21:27 -0700449 WARN_ON(tcp_rtx_queue_empty(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Nandita Dukkipati9b717a82013-03-11 10:00:44 +0000451 tp->tlp_high_seq = 0;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
454 !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
455 /* Receiver dastardly shrinks window. Our retransmits
456 * become zero probes, but we should not timeout this
457 * connection. If the socket is an orphan, time it out,
458 * we cannot allow such beasts to hang infinitely.
459 */
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700460 struct inet_sock *inet = inet_sk(sk);
461 if (sk->sk_family == AF_INET) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800462 net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
463 &inet->inet_daddr,
464 ntohs(inet->inet_dport),
465 inet->inet_num,
466 tp->snd_una, tp->snd_nxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000468#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700469 else if (sk->sk_family == AF_INET6) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800470 net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
471 &sk->sk_v6_daddr,
472 ntohs(inet->inet_dport),
473 inet->inet_num,
474 tp->snd_una, tp->snd_nxt);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700475 }
476#endif
Eric Dumazet70eabf02017-05-16 14:00:07 -0700477 if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 tcp_write_err(sk);
479 goto out;
480 }
Neal Cardwell5ae344c2014-08-04 19:12:29 -0400481 tcp_enter_loss(sk);
Eric Dumazet75c119a2017-10-05 22:21:27 -0700482 tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 __sk_dst_reset(sk);
484 goto out_reset_timer;
485 }
486
487 if (tcp_write_timeout(sk))
488 goto out;
489
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700490 if (icsk->icsk_retransmits == 0) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700491 int mib_idx;
492
Ilpo Järvinenc60ce4e2010-10-14 01:52:09 +0000493 if (icsk->icsk_ca_state == TCP_CA_Recovery) {
Ilpo Järvinenbc079e92009-02-28 04:44:34 +0000494 if (tcp_is_sack(tp))
495 mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
496 else
497 mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300498 } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700499 mib_idx = LINUX_MIB_TCPLOSSFAILURES;
Ilpo Järvinenc60ce4e2010-10-14 01:52:09 +0000500 } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
501 tp->sacked_out) {
502 if (tcp_is_sack(tp))
503 mib_idx = LINUX_MIB_TCPSACKFAILURES;
504 else
505 mib_idx = LINUX_MIB_TCPRENOFAILURES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 } else {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700507 mib_idx = LINUX_MIB_TCPTIMEOUTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700509 __NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
Neal Cardwell5ae344c2014-08-04 19:12:29 -0400512 tcp_enter_loss(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Eric Dumazet75c119a2017-10-05 22:21:27 -0700514 if (tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* Retransmission failed because of local congestion,
516 * do not backoff.
517 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700518 if (!icsk->icsk_retransmits)
519 icsk->icsk_retransmits = 1;
520 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700521 min(icsk->icsk_rto, TCP_RESOURCE_PROBE_INTERVAL),
522 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto out;
524 }
525
526 /* Increase the timeout each time we retransmit. Note that
527 * we do not increase the rtt estimate. rto is initialized
528 * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests
529 * that doubling rto each time is the least we can get away with.
530 * In KA9Q, Karn uses this for the first few times, and then
531 * goes to quadratic. netBSD doubles, but only goes up to *64,
532 * and clamps at 1 to 64 sec afterwards. Note that 120 sec is
533 * defined in the protocol as the maximum possible RTT. I guess
534 * we'll have to use something other than TCP to talk to the
535 * University of Mars.
536 *
537 * PAWS allows us longer timeouts and large windows, so once
538 * implemented ftp to mars will work nicely. We will have to fix
539 * the 120 second clamps though!
540 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700541 icsk->icsk_backoff++;
542 icsk->icsk_retransmits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544out_reset_timer:
Andreas Petlund36e31b0a2010-02-18 02:47:01 +0000545 /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is
546 * used to reset timer, set to 0. Recalculate 'icsk_rto' as this
547 * might be increased if the stream oscillates between thin and thick,
548 * thus the old value might already be too high compared to the value
549 * set by 'tcp_set_rto' in tcp_input.c which resets the rto without
550 * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating
551 * exponential backoff behaviour to avoid continue hammering
552 * linear-timeout retransmissions into a black hole
553 */
554 if (sk->sk_state == TCP_ESTABLISHED &&
Eric Dumazet2c04ac82017-10-26 21:54:58 -0700555 (tp->thin_lto || net->ipv4.sysctl_tcp_thin_linear_timeouts) &&
Andreas Petlund36e31b0a2010-02-18 02:47:01 +0000556 tcp_stream_is_thin(tp) &&
557 icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
558 icsk->icsk_backoff = 0;
559 icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX);
560 } else {
561 /* Use normal (exponential) backoff */
562 icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
563 }
Jon Maxwellb701a992018-07-19 11:14:44 +1000564 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
565 tcp_clamp_rto_to_user_timeout(sk), TCP_RTO_MAX);
Eric Dumazetce682ef2017-05-23 12:38:35 -0700566 if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1 + 1, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 __sk_dst_reset(sk);
568
569out:;
570}
571
Richard Sailerc380d372016-07-16 04:04:34 +0200572/* Called with bottom-half processing disabled.
573 Called by tcp_write_timer() */
Eric Dumazet6f458df2012-07-20 05:45:50 +0000574void tcp_write_timer_handler(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700576 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int event;
578
Eric Dumazet02b2faa2017-03-03 14:08:21 -0800579 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
580 !icsk->icsk_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto out;
582
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700583 if (time_after(icsk->icsk_timeout, jiffies)) {
584 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto out;
586 }
587
Eric Dumazet9a568de2017-05-16 14:00:14 -0700588 tcp_mstamp_refresh(tcp_sk(sk));
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700589 event = icsk->icsk_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 switch (event) {
Yuchung Cheng57dde7f2017-01-12 22:11:33 -0800592 case ICSK_TIME_REO_TIMEOUT:
593 tcp_rack_reo_timeout(sk);
594 break;
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000595 case ICSK_TIME_LOSS_PROBE:
596 tcp_send_loss_probe(sk);
597 break;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700598 case ICSK_TIME_RETRANS:
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000599 icsk->icsk_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 tcp_retransmit_timer(sk);
601 break;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700602 case ICSK_TIME_PROBE0:
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000603 icsk->icsk_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 tcp_probe_timer(sk);
605 break;
606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608out:
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800609 sk_mem_reclaim(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000610}
611
Kees Cook59f379f2017-10-16 17:29:19 -0700612static void tcp_write_timer(struct timer_list *t)
Eric Dumazet6f458df2012-07-20 05:45:50 +0000613{
Kees Cook59f379f2017-10-16 17:29:19 -0700614 struct inet_connection_sock *icsk =
615 from_timer(icsk, t, icsk_retransmit_timer);
616 struct sock *sk = &icsk->icsk_inet.sk;
Eric Dumazet6f458df2012-07-20 05:45:50 +0000617
618 bh_lock_sock(sk);
619 if (!sock_owned_by_user(sk)) {
620 tcp_write_timer_handler(sk);
621 } else {
Richard Sailerc380d372016-07-16 04:04:34 +0200622 /* delegate our work to tcp_release_cb() */
Eric Dumazet7aa54702016-12-03 11:14:57 -0800623 if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &sk->sk_tsq_flags))
Eric Dumazet144d56e2012-08-20 00:22:46 +0000624 sock_hold(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 bh_unlock_sock(sk);
627 sock_put(sk);
628}
629
Eric Dumazet42cb80a2015-03-22 10:22:19 -0700630void tcp_syn_ack_timeout(const struct request_sock *req)
Octavian Purdila72659ec2010-01-17 19:09:39 -0800631{
Eric Dumazet42cb80a2015-03-22 10:22:19 -0700632 struct net *net = read_pnet(&inet_rsk(req)->ireq_net);
633
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700634 __NET_INC_STATS(net, LINUX_MIB_TCPTIMEOUTS);
Octavian Purdila72659ec2010-01-17 19:09:39 -0800635}
636EXPORT_SYMBOL(tcp_syn_ack_timeout);
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638void tcp_set_keepalive(struct sock *sk, int val)
639{
640 if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
641 return;
642
643 if (val && !sock_flag(sk, SOCK_KEEPOPEN))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700644 inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 else if (!val)
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700646 inet_csk_delete_keepalive_timer(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
Ursula Braun4b9d07a2017-01-09 16:55:12 +0100648EXPORT_SYMBOL_GPL(tcp_set_keepalive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650
Kees Cook59f379f2017-10-16 17:29:19 -0700651static void tcp_keepalive_timer (struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Kees Cook59f379f2017-10-16 17:29:19 -0700653 struct sock *sk = from_timer(sk, t, sk_timer);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300654 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct tcp_sock *tp = tcp_sk(sk);
Flavio Leitner6c37e5d2010-04-26 18:33:27 +0000656 u32 elapsed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* Only process if socket is not in use. */
659 bh_lock_sock(sk);
660 if (sock_owned_by_user(sk)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900661 /* Try again later. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700662 inet_csk_reset_keepalive_timer (sk, HZ/20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto out;
664 }
665
666 if (sk->sk_state == TCP_LISTEN) {
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700667 pr_err("Hmm... keepalive on a LISTEN ???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goto out;
669 }
670
Eric Dumazet4688eb72017-12-12 18:22:52 -0800671 tcp_mstamp_refresh(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) {
673 if (tp->linger2 >= 0) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700674 const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 if (tmo > 0) {
677 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
678 goto out;
679 }
680 }
681 tcp_send_active_reset(sk, GFP_ATOMIC);
682 goto death;
683 }
684
Eric Dumazet2dda6402017-08-02 23:10:46 -0700685 if (!sock_flag(sk, SOCK_KEEPOPEN) ||
686 ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto out;
688
689 elapsed = keepalive_time_when(tp);
690
691 /* It is alive without keepalive 8) */
Eric Dumazet75c119a2017-10-05 22:21:27 -0700692 if (tp->packets_out || !tcp_write_queue_empty(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 goto resched;
694
Flavio Leitner6c37e5d2010-04-26 18:33:27 +0000695 elapsed = keepalive_time_elapsed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (elapsed >= keepalive_time_when(tp)) {
Jerry Chudca43c72010-08-27 19:13:28 +0000698 /* If the TCP_USER_TIMEOUT option is enabled, use that
699 * to determine when to timeout instead.
700 */
701 if ((icsk->icsk_user_timeout != 0 &&
Jon Maxwell9bcc66e2018-07-19 11:14:42 +1000702 elapsed >= msecs_to_jiffies(icsk->icsk_user_timeout) &&
Jerry Chudca43c72010-08-27 19:13:28 +0000703 icsk->icsk_probes_out > 0) ||
704 (icsk->icsk_user_timeout == 0 &&
705 icsk->icsk_probes_out >= keepalive_probes(tp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 tcp_send_active_reset(sk, GFP_ATOMIC);
707 tcp_write_err(sk);
708 goto out;
709 }
Eric Dumazete520af42015-05-06 14:26:25 -0700710 if (tcp_write_wakeup(sk, LINUX_MIB_TCPKEEPALIVE) <= 0) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300711 icsk->icsk_probes_out++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 elapsed = keepalive_intvl_when(tp);
713 } else {
714 /* If keepalive was lost due to local congestion,
715 * try harder.
716 */
717 elapsed = TCP_RESOURCE_PROBE_INTERVAL;
718 }
719 } else {
720 /* It is tp->rcv_tstamp + keepalive_time_when(tp) */
721 elapsed = keepalive_time_when(tp) - elapsed;
722 }
723
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800724 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726resched:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700727 inet_csk_reset_keepalive_timer (sk, elapsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto out;
729
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900730death:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 tcp_done(sk);
732
733out:
734 bh_unlock_sock(sk);
735 sock_put(sk);
736}
Eric Dumazet6f458df2012-07-20 05:45:50 +0000737
Eric Dumazet5d9f4262018-05-17 14:47:26 -0700738static enum hrtimer_restart tcp_compressed_ack_kick(struct hrtimer *timer)
739{
740 struct tcp_sock *tp = container_of(timer, struct tcp_sock, compressed_ack_timer);
741 struct sock *sk = (struct sock *)tp;
742
743 bh_lock_sock(sk);
744 if (!sock_owned_by_user(sk)) {
Eric Dumazet86de5922018-11-20 05:53:59 -0800745 if (tp->compressed_ack > TCP_FASTRETRANS_THRESH)
Eric Dumazet5d9f4262018-05-17 14:47:26 -0700746 tcp_send_ack(sk);
747 } else {
748 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED,
749 &sk->sk_tsq_flags))
750 sock_hold(sk);
751 }
752 bh_unlock_sock(sk);
753
754 sock_put(sk);
755
756 return HRTIMER_NORESTART;
757}
758
Eric Dumazet6f458df2012-07-20 05:45:50 +0000759void tcp_init_xmit_timers(struct sock *sk)
760{
761 inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
762 &tcp_keepalive_timer);
Eric Dumazetfb420d52018-09-28 10:28:44 -0700763 hrtimer_init(&tcp_sk(sk)->pacing_timer, CLOCK_MONOTONIC,
Eric Dumazet73a6bab2018-05-10 14:59:43 -0700764 HRTIMER_MODE_ABS_PINNED_SOFT);
Eric Dumazet218af592017-05-16 04:24:36 -0700765 tcp_sk(sk)->pacing_timer.function = tcp_pace_kick;
Eric Dumazet5d9f4262018-05-17 14:47:26 -0700766
767 hrtimer_init(&tcp_sk(sk)->compressed_ack_timer, CLOCK_MONOTONIC,
768 HRTIMER_MODE_REL_PINNED_SOFT);
769 tcp_sk(sk)->compressed_ack_timer.function = tcp_compressed_ack_kick;
Eric Dumazet6f458df2012-07-20 05:45:50 +0000770}