]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv4/tcp_timer.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / net / ipv4 / tcp_timer.c
CommitLineData
1da177e4
LT
1/*
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 *
02c30a84 8 * Authors: Ross Biro
1da177e4
LT
9 * 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>
5a0e3ad6 22#include <linux/gfp.h>
1da177e4
LT
23#include <net/tcp.h>
24
c380d37e
RS
25/**
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
1da177e4
LT
32static void tcp_write_err(struct sock *sk)
33{
34 sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
35 sk->sk_error_report(sk);
36
3fbaf486 37 tcp_write_queue_purge(sk);
1da177e4 38 tcp_done(sk);
02a1d6e7 39 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
1da177e4
LT
40}
41
c380d37e
RS
42/**
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
1da177e4 46 *
c380d37e
RS
47 * 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 *
4ee806d5
DS
52 * 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 *
c380d37e
RS
59 * 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.
4ee806d5 64 * 3. If our net namespace is exiting.
1da177e4 65 */
b248230c 66static int tcp_out_of_resources(struct sock *sk, bool do_reset)
1da177e4
LT
67{
68 struct tcp_sock *tp = tcp_sk(sk);
ad1af0fe 69 int shift = 0;
1da177e4 70
e905a9ed 71 /* If peer does not open window for long time, or did not transmit
1da177e4 72 * anything for long time, penalize it. */
d635fbe2 73 if ((s32)(tcp_jiffies32 - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
ad1af0fe 74 shift++;
1da177e4
LT
75
76 /* If some dubious ICMP arrived, penalize even more. */
77 if (sk->sk_err_soft)
ad1af0fe 78 shift++;
1da177e4 79
efcdbf24 80 if (tcp_check_oom(sk, shift)) {
1da177e4
LT
81 /* Catch exceptional cases, when connection requires reset.
82 * 1. Last segment was sent recently. */
d635fbe2 83 if ((s32)(tcp_jiffies32 - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||
1da177e4
LT
84 /* 2. Window is closed. */
85 (!tp->snd_wnd && !tp->packets_out))
b248230c 86 do_reset = true;
1da177e4
LT
87 if (do_reset)
88 tcp_send_active_reset(sk, GFP_ATOMIC);
89 tcp_done(sk);
02a1d6e7 90 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
1da177e4
LT
91 return 1;
92 }
4ee806d5
DS
93
94 if (!check_net(sock_net(sk))) {
95 /* Not possible to send reset; just close */
96 tcp_done(sk);
97 return 1;
98 }
99
1da177e4
LT
100 return 0;
101}
102
c380d37e
RS
103/**
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 */
7533ce30 108static int tcp_orphan_retries(struct sock *sk, bool alive)
1da177e4 109{
c402d9be 110 int retries = sock_net(sk)->ipv4.sysctl_tcp_orphan_retries; /* May be zero. */
1da177e4
LT
111
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
ce55dd36
ED
124static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
125{
d0f36847
ED
126 const struct net *net = sock_net(sk);
127 int mss;
b0f9ca53 128
ce55dd36 129 /* Black hole detection */
d0f36847
ED
130 if (!net->ipv4.sysctl_tcp_mtu_probing)
131 return;
132
133 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);
524951b6 140 mss = max(mss, net->ipv4.sysctl_tcp_min_snd_mss);
d0f36847 141 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
ce55dd36 142 }
d0f36847 143 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
ce55dd36
ED
144}
145
c380d37e
RS
146
147/**
148 * retransmits_timed_out() - returns true if this connection has timed out
149 * @sk: The current socket
150 * @boundary: max number of retransmissions
151 * @timeout: A custom timeout value.
152 * If set to 0 the default timeout is calculated and used.
153 * Using TCP_RTO_MIN and the number of unsuccessful retransmits.
c380d37e
RS
154 *
155 * The default "timeout" value this function can calculate and use
156 * is equivalent to the timeout of a TCP Connection
157 * after "boundary" unsuccessful, exponentially backed-off
ce682ef6 158 * retransmissions with an initial RTO of TCP_RTO_MIN.
2f7de571
DL
159 */
160static bool retransmits_timed_out(struct sock *sk,
dca43c75 161 unsigned int boundary,
ce682ef6 162 unsigned int timeout)
2f7de571 163{
ce682ef6 164 const unsigned int rto_base = TCP_RTO_MIN;
9a568de4 165 unsigned int linear_backoff_thresh, start_ts;
2f7de571
DL
166
167 if (!inet_csk(sk)->icsk_retransmits)
168 return false;
169
7faee5c0 170 start_ts = tcp_sk(sk)->retrans_stamp;
75c119af
ED
171 if (unlikely(!start_ts)) {
172 struct sk_buff *head = tcp_rtx_queue_head(sk);
173
174 if (!head)
175 return false;
176 start_ts = tcp_skb_timestamp(head);
177 }
2f7de571 178
dca43c75 179 if (likely(timeout == 0)) {
21a180cd 180 linear_backoff_thresh = ilog2(TCP_RTO_MAX/rto_base);
2f7de571 181
dca43c75 182 if (boundary <= linear_backoff_thresh)
21a180cd 183 timeout = ((2 << boundary) - 1) * rto_base;
dca43c75 184 else
21a180cd 185 timeout = ((2 << linear_backoff_thresh) - 1) * rto_base +
dca43c75
JC
186 (boundary - linear_backoff_thresh) * TCP_RTO_MAX;
187 }
9a568de4 188 return (tcp_time_stamp(tcp_sk(sk)) - start_ts) >= jiffies_to_msecs(timeout);
2f7de571
DL
189}
190
1da177e4
LT
191/* A write timeout has occurred. Process the after effects. */
192static int tcp_write_timeout(struct sock *sk)
193{
5d424d5a 194 struct inet_connection_sock *icsk = inet_csk(sk);
c968601d 195 struct tcp_sock *tp = tcp_sk(sk);
6fa25166 196 struct net *net = sock_net(sk);
ce682ef6 197 bool expired, do_reset;
1da177e4
LT
198 int retry_until;
199
200 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
c968601d 201 if (icsk->icsk_retransmits) {
b6c6712a 202 dst_negative_advice(sk);
c968601d 203 if (tp->syn_fastopen || tp->syn_data)
2646c831 204 tcp_fastopen_cache_set(sk, 0, NULL, true, 0);
dd52bc2b 205 if (tp->syn_data && icsk->icsk_retransmits == 1)
c10d9310
ED
206 NET_INC_STATS(sock_net(sk),
207 LINUX_MIB_TCPFASTOPENACTIVEFAIL);
3acf3ec3
LB
208 } else if (!tp->syn_data && !tp->syn_fastopen) {
209 sk_rethink_txhash(sk);
c968601d 210 }
6fa25166 211 retry_until = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
ce682ef6 212 expired = icsk->icsk_retransmits >= retry_until;
1da177e4 213 } else {
ce682ef6 214 if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1, 0)) {
0e45f4da
YC
215 /* Some middle-boxes may black-hole Fast Open _after_
216 * the handshake. Therefore we conservatively disable
59450f8d
WW
217 * Fast Open on this path on recurring timeouts after
218 * successful Fast Open.
0e45f4da 219 */
59450f8d 220 if (tp->syn_data_acked) {
0e45f4da 221 tcp_fastopen_cache_set(sk, 0, NULL, true, 0);
ae5c3f40 222 if (icsk->icsk_retransmits == net->ipv4.sysctl_tcp_retries1)
c10d9310
ED
223 NET_INC_STATS(sock_net(sk),
224 LINUX_MIB_TCPFASTOPENACTIVEFAIL);
0e45f4da 225 }
5d424d5a 226 /* Black hole detection */
ce55dd36 227 tcp_mtu_probing(icsk, sk);
1da177e4 228
b6c6712a 229 dst_negative_advice(sk);
3acf3ec3
LB
230 } else {
231 sk_rethink_txhash(sk);
1da177e4
LT
232 }
233
c6214a97 234 retry_until = net->ipv4.sysctl_tcp_retries2;
1da177e4 235 if (sock_flag(sk, SOCK_DEAD)) {
7533ce30 236 const bool alive = icsk->icsk_rto < TCP_RTO_MAX;
e905a9ed 237
1da177e4 238 retry_until = tcp_orphan_retries(sk, alive);
6fa12c85 239 do_reset = alive ||
ce682ef6 240 !retransmits_timed_out(sk, retry_until, 0);
1da177e4 241
6fa12c85 242 if (tcp_out_of_resources(sk, do_reset))
1da177e4
LT
243 return 1;
244 }
ce682ef6
ED
245 expired = retransmits_timed_out(sk, retry_until,
246 icsk->icsk_user_timeout);
1da177e4 247 }
ce682ef6 248 if (expired) {
1da177e4
LT
249 /* Has it gone just too far? */
250 tcp_write_err(sk);
251 return 1;
252 }
253 return 0;
254}
255
c10d9310 256/* Called with BH disabled */
6f458dfb 257void tcp_delack_timer_handler(struct sock *sk)
1da177e4 258{
463c84b9 259 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 260
9993e7d3 261 sk_mem_reclaim_partial(sk);
1da177e4 262
02b2faaf
ED
263 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
264 !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
1da177e4
LT
265 goto out;
266
463c84b9
ACM
267 if (time_after(icsk->icsk_ack.timeout, jiffies)) {
268 sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
1da177e4
LT
269 goto out;
270 }
463c84b9 271 icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
1da177e4 272
463c84b9
ACM
273 if (inet_csk_ack_scheduled(sk)) {
274 if (!icsk->icsk_ack.pingpong) {
1da177e4 275 /* Delayed ACK missed: inflate ATO. */
463c84b9 276 icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto);
1da177e4
LT
277 } else {
278 /* Delayed ACK missed: leave pingpong mode and
279 * deflate ATO.
280 */
463c84b9
ACM
281 icsk->icsk_ack.pingpong = 0;
282 icsk->icsk_ack.ato = TCP_ATO_MIN;
1da177e4 283 }
4688eb7c 284 tcp_mstamp_refresh(tcp_sk(sk));
1da177e4 285 tcp_send_ack(sk);
02a1d6e7 286 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
1da177e4 287 }
1da177e4
LT
288
289out:
b8da51eb 290 if (tcp_under_memory_pressure(sk))
3ab224be 291 sk_mem_reclaim(sk);
6f458dfb
ED
292}
293
c380d37e
RS
294
295/**
296 * tcp_delack_timer() - The TCP delayed ACK timeout handler
297 * @data: Pointer to the current socket. (gets casted to struct sock *)
298 *
299 * This function gets (indirectly) called when the kernel timer for a TCP packet
300 * of this socket expires. Calls tcp_delack_timer_handler() to do the actual work.
301 *
302 * Returns: Nothing (void)
303 */
59f379f9 304static void tcp_delack_timer(struct timer_list *t)
6f458dfb 305{
59f379f9
KC
306 struct inet_connection_sock *icsk =
307 from_timer(icsk, t, icsk_delack_timer);
308 struct sock *sk = &icsk->icsk_inet.sk;
6f458dfb
ED
309
310 bh_lock_sock(sk);
311 if (!sock_owned_by_user(sk)) {
312 tcp_delack_timer_handler(sk);
313 } else {
59f379f9 314 icsk->icsk_ack.blocked = 1;
02a1d6e7 315 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
6f458dfb 316 /* deleguate our work to tcp_release_cb() */
7aa5470c 317 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &sk->sk_tsq_flags))
144d56e9 318 sock_hold(sk);
6f458dfb 319 }
1da177e4
LT
320 bh_unlock_sock(sk);
321 sock_put(sk);
322}
323
324static void tcp_probe_timer(struct sock *sk)
325{
6687e988 326 struct inet_connection_sock *icsk = inet_csk(sk);
75c119af 327 struct sk_buff *skb = tcp_send_head(sk);
1da177e4
LT
328 struct tcp_sock *tp = tcp_sk(sk);
329 int max_probes;
b248230c 330 u32 start_ts;
1da177e4 331
75c119af 332 if (tp->packets_out || !skb) {
6687e988 333 icsk->icsk_probes_out = 0;
1da177e4
LT
334 return;
335 }
336
b248230c
YC
337 /* RFC 1122 4.2.2.17 requires the sender to stay open indefinitely as
338 * long as the receiver continues to respond probes. We support this by
339 * default and reset icsk_probes_out with incoming ACKs. But if the
340 * socket is orphaned or the user specifies TCP_USER_TIMEOUT, we
341 * kill the socket when the retry count and the time exceeds the
342 * corresponding system limit. We also implement similar policy when
343 * we use RTO to probe window in tcp_retransmit_timer().
1da177e4 344 */
75c119af 345 start_ts = tcp_skb_timestamp(skb);
b248230c 346 if (!start_ts)
75c119af 347 skb->skb_mstamp = tp->tcp_mstamp;
b248230c 348 else if (icsk->icsk_user_timeout &&
4ab68879
ED
349 (s32)(tcp_time_stamp(tp) - start_ts) >
350 jiffies_to_msecs(icsk->icsk_user_timeout))
b248230c 351 goto abort;
1da177e4 352
c6214a97 353 max_probes = sock_net(sk)->ipv4.sysctl_tcp_retries2;
1da177e4 354 if (sock_flag(sk, SOCK_DEAD)) {
7533ce30 355 const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX;
e905a9ed 356
1da177e4 357 max_probes = tcp_orphan_retries(sk, alive);
b248230c
YC
358 if (!alive && icsk->icsk_backoff >= max_probes)
359 goto abort;
360 if (tcp_out_of_resources(sk, true))
1da177e4
LT
361 return;
362 }
363
b87709fa 364 if (icsk->icsk_probes_out >= max_probes) {
b248230c 365abort: tcp_write_err(sk);
1da177e4
LT
366 } else {
367 /* Only send another probe if we didn't close things up. */
368 tcp_send_probe0(sk);
369 }
370}
371
8336886f
JC
372/*
373 * Timer for Fast Open socket to retransmit SYNACK. Note that the
374 * sk here is the child socket, not the parent (listener) socket.
375 */
376static void tcp_fastopen_synack_timer(struct sock *sk)
377{
378 struct inet_connection_sock *icsk = inet_csk(sk);
379 int max_retries = icsk->icsk_syn_retries ? :
7c083ecb 380 sock_net(sk)->ipv4.sysctl_tcp_synack_retries + 1; /* add one more retry for fastopen */
8336886f
JC
381 struct request_sock *req;
382
383 req = tcp_sk(sk)->fastopen_rsk;
42cb80a2 384 req->rsk_ops->syn_ack_timeout(req);
8336886f 385
e6c022a4 386 if (req->num_timeout >= max_retries) {
8336886f
JC
387 tcp_write_err(sk);
388 return;
389 }
390 /* XXX (TFO) - Unlike regular SYN-ACK retransmit, we ignore error
391 * returned from rtx_syn_ack() to make it more persistent like
392 * regular retransmit because if the child socket has been accepted
393 * it's not good to give up too easily.
394 */
e6c022a4
ED
395 inet_rtx_syn_ack(sk, req);
396 req->num_timeout++;
7e32b443 397 icsk->icsk_retransmits++;
8336886f 398 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
e6c022a4 399 TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
8336886f
JC
400}
401
1da177e4 402
c380d37e
RS
403/**
404 * tcp_retransmit_timer() - The TCP retransmit timeout handler
405 * @sk: Pointer to the current socket.
406 *
407 * This function gets called when the kernel timer for a TCP packet
408 * of this socket expires.
409 *
410 * It handles retransmission, timer adjustment and other necesarry measures.
411 *
412 * Returns: Nothing (void)
413 */
f1ecd5d9 414void tcp_retransmit_timer(struct sock *sk)
1da177e4
LT
415{
416 struct tcp_sock *tp = tcp_sk(sk);
ae5c3f40 417 struct net *net = sock_net(sk);
463c84b9 418 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 419
8336886f 420 if (tp->fastopen_rsk) {
37561f68
JC
421 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
422 sk->sk_state != TCP_FIN_WAIT1);
8336886f
JC
423 tcp_fastopen_synack_timer(sk);
424 /* Before we receive ACK to our SYN-ACK don't retransmit
425 * anything else (e.g., data or FIN segments).
426 */
427 return;
428 }
52a5ecd0
YC
429 if (!tp->packets_out || WARN_ON_ONCE(tcp_rtx_queue_empty(sk)))
430 return;
1da177e4 431
9b717a8d
ND
432 tp->tlp_high_seq = 0;
433
1da177e4
LT
434 if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
435 !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
436 /* Receiver dastardly shrinks window. Our retransmits
437 * become zero probes, but we should not timeout this
438 * connection. If the socket is an orphan, time it out,
439 * we cannot allow such beasts to hang infinitely.
440 */
569508c9
YH
441 struct inet_sock *inet = inet_sk(sk);
442 if (sk->sk_family == AF_INET) {
ba7a46f1
JP
443 net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
444 &inet->inet_daddr,
445 ntohs(inet->inet_dport),
446 inet->inet_num,
447 tp->snd_una, tp->snd_nxt);
1da177e4 448 }
dfd56b8b 449#if IS_ENABLED(CONFIG_IPV6)
569508c9 450 else if (sk->sk_family == AF_INET6) {
ba7a46f1
JP
451 net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
452 &sk->sk_v6_daddr,
453 ntohs(inet->inet_dport),
454 inet->inet_num,
455 tp->snd_una, tp->snd_nxt);
569508c9 456 }
1da177e4 457#endif
70eabf0e 458 if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) {
1da177e4
LT
459 tcp_write_err(sk);
460 goto out;
461 }
5ae344c9 462 tcp_enter_loss(sk);
75c119af 463 tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1);
1da177e4
LT
464 __sk_dst_reset(sk);
465 goto out_reset_timer;
466 }
467
3a0274cf 468 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPTIMEOUTS);
1da177e4
LT
469 if (tcp_write_timeout(sk))
470 goto out;
471
463c84b9 472 if (icsk->icsk_retransmits == 0) {
3a0274cf 473 int mib_idx = 0;
40b215e5 474
c60ce4e2 475 if (icsk->icsk_ca_state == TCP_CA_Recovery) {
bc079e9e
IJ
476 if (tcp_is_sack(tp))
477 mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
478 else
479 mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
6687e988 480 } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
40b215e5 481 mib_idx = LINUX_MIB_TCPLOSSFAILURES;
c60ce4e2
IJ
482 } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
483 tp->sacked_out) {
484 if (tcp_is_sack(tp))
485 mib_idx = LINUX_MIB_TCPSACKFAILURES;
486 else
487 mib_idx = LINUX_MIB_TCPRENOFAILURES;
1da177e4 488 }
3a0274cf
YC
489 if (mib_idx)
490 __NET_INC_STATS(sock_net(sk), mib_idx);
1da177e4
LT
491 }
492
5ae344c9 493 tcp_enter_loss(sk);
1da177e4 494
75c119af 495 if (tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1) > 0) {
1da177e4
LT
496 /* Retransmission failed because of local congestion,
497 * do not backoff.
498 */
463c84b9
ACM
499 if (!icsk->icsk_retransmits)
500 icsk->icsk_retransmits = 1;
501 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
3f421baa
ACM
502 min(icsk->icsk_rto, TCP_RESOURCE_PROBE_INTERVAL),
503 TCP_RTO_MAX);
1da177e4
LT
504 goto out;
505 }
506
507 /* Increase the timeout each time we retransmit. Note that
508 * we do not increase the rtt estimate. rto is initialized
509 * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests
510 * that doubling rto each time is the least we can get away with.
511 * In KA9Q, Karn uses this for the first few times, and then
512 * goes to quadratic. netBSD doubles, but only goes up to *64,
513 * and clamps at 1 to 64 sec afterwards. Note that 120 sec is
514 * defined in the protocol as the maximum possible RTT. I guess
515 * we'll have to use something other than TCP to talk to the
516 * University of Mars.
517 *
518 * PAWS allows us longer timeouts and large windows, so once
519 * implemented ftp to mars will work nicely. We will have to fix
520 * the 120 second clamps though!
521 */
463c84b9
ACM
522 icsk->icsk_backoff++;
523 icsk->icsk_retransmits++;
1da177e4
LT
524
525out_reset_timer:
36e31b0a
AP
526 /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is
527 * used to reset timer, set to 0. Recalculate 'icsk_rto' as this
528 * might be increased if the stream oscillates between thin and thick,
529 * thus the old value might already be too high compared to the value
530 * set by 'tcp_set_rto' in tcp_input.c which resets the rto without
531 * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating
532 * exponential backoff behaviour to avoid continue hammering
533 * linear-timeout retransmissions into a black hole
534 */
535 if (sk->sk_state == TCP_ESTABLISHED &&
2c04ac8a 536 (tp->thin_lto || net->ipv4.sysctl_tcp_thin_linear_timeouts) &&
36e31b0a
AP
537 tcp_stream_is_thin(tp) &&
538 icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
539 icsk->icsk_backoff = 0;
540 icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX);
541 } else {
542 /* Use normal (exponential) backoff */
543 icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
544 }
3f421baa 545 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX);
ce682ef6 546 if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1 + 1, 0))
1da177e4
LT
547 __sk_dst_reset(sk);
548
549out:;
550}
551
c380d37e
RS
552/* Called with bottom-half processing disabled.
553 Called by tcp_write_timer() */
6f458dfb 554void tcp_write_timer_handler(struct sock *sk)
1da177e4 555{
463c84b9 556 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4
LT
557 int event;
558
02b2faaf
ED
559 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
560 !icsk->icsk_pending)
1da177e4
LT
561 goto out;
562
463c84b9
ACM
563 if (time_after(icsk->icsk_timeout, jiffies)) {
564 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
1da177e4
LT
565 goto out;
566 }
567
9a568de4 568 tcp_mstamp_refresh(tcp_sk(sk));
463c84b9 569 event = icsk->icsk_pending;
1da177e4
LT
570
571 switch (event) {
57dde7f7
YC
572 case ICSK_TIME_REO_TIMEOUT:
573 tcp_rack_reo_timeout(sk);
574 break;
6ba8a3b1
ND
575 case ICSK_TIME_LOSS_PROBE:
576 tcp_send_loss_probe(sk);
577 break;
463c84b9 578 case ICSK_TIME_RETRANS:
6ba8a3b1 579 icsk->icsk_pending = 0;
1da177e4
LT
580 tcp_retransmit_timer(sk);
581 break;
463c84b9 582 case ICSK_TIME_PROBE0:
6ba8a3b1 583 icsk->icsk_pending = 0;
1da177e4
LT
584 tcp_probe_timer(sk);
585 break;
586 }
1da177e4
LT
587
588out:
3ab224be 589 sk_mem_reclaim(sk);
6f458dfb
ED
590}
591
59f379f9 592static void tcp_write_timer(struct timer_list *t)
6f458dfb 593{
59f379f9
KC
594 struct inet_connection_sock *icsk =
595 from_timer(icsk, t, icsk_retransmit_timer);
596 struct sock *sk = &icsk->icsk_inet.sk;
6f458dfb
ED
597
598 bh_lock_sock(sk);
599 if (!sock_owned_by_user(sk)) {
600 tcp_write_timer_handler(sk);
601 } else {
c380d37e 602 /* delegate our work to tcp_release_cb() */
7aa5470c 603 if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &sk->sk_tsq_flags))
144d56e9 604 sock_hold(sk);
6f458dfb 605 }
1da177e4
LT
606 bh_unlock_sock(sk);
607 sock_put(sk);
608}
609
42cb80a2 610void tcp_syn_ack_timeout(const struct request_sock *req)
72659ecc 611{
42cb80a2
ED
612 struct net *net = read_pnet(&inet_rsk(req)->ireq_net);
613
02a1d6e7 614 __NET_INC_STATS(net, LINUX_MIB_TCPTIMEOUTS);
72659ecc
OP
615}
616EXPORT_SYMBOL(tcp_syn_ack_timeout);
617
1da177e4
LT
618void tcp_set_keepalive(struct sock *sk, int val)
619{
620 if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
621 return;
622
623 if (val && !sock_flag(sk, SOCK_KEEPOPEN))
463c84b9 624 inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk)));
1da177e4 625 else if (!val)
463c84b9 626 inet_csk_delete_keepalive_timer(sk);
1da177e4 627}
4b9d07a4 628EXPORT_SYMBOL_GPL(tcp_set_keepalive);
1da177e4
LT
629
630
59f379f9 631static void tcp_keepalive_timer (struct timer_list *t)
1da177e4 632{
59f379f9 633 struct sock *sk = from_timer(sk, t, sk_timer);
6687e988 634 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 635 struct tcp_sock *tp = tcp_sk(sk);
6c37e5de 636 u32 elapsed;
1da177e4
LT
637
638 /* Only process if socket is not in use. */
639 bh_lock_sock(sk);
640 if (sock_owned_by_user(sk)) {
e905a9ed 641 /* Try again later. */
463c84b9 642 inet_csk_reset_keepalive_timer (sk, HZ/20);
1da177e4
LT
643 goto out;
644 }
645
646 if (sk->sk_state == TCP_LISTEN) {
fa76ce73 647 pr_err("Hmm... keepalive on a LISTEN ???\n");
1da177e4
LT
648 goto out;
649 }
650
4688eb7c 651 tcp_mstamp_refresh(tp);
1da177e4
LT
652 if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) {
653 if (tp->linger2 >= 0) {
463c84b9 654 const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN;
1da177e4
LT
655
656 if (tmo > 0) {
657 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
658 goto out;
659 }
660 }
661 tcp_send_active_reset(sk, GFP_ATOMIC);
662 goto death;
663 }
664
2dda6400
ED
665 if (!sock_flag(sk, SOCK_KEEPOPEN) ||
666 ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)))
1da177e4
LT
667 goto out;
668
669 elapsed = keepalive_time_when(tp);
670
671 /* It is alive without keepalive 8) */
75c119af 672 if (tp->packets_out || !tcp_write_queue_empty(sk))
1da177e4
LT
673 goto resched;
674
6c37e5de 675 elapsed = keepalive_time_elapsed(tp);
1da177e4
LT
676
677 if (elapsed >= keepalive_time_when(tp)) {
dca43c75
JC
678 /* If the TCP_USER_TIMEOUT option is enabled, use that
679 * to determine when to timeout instead.
680 */
681 if ((icsk->icsk_user_timeout != 0 &&
682 elapsed >= icsk->icsk_user_timeout &&
683 icsk->icsk_probes_out > 0) ||
684 (icsk->icsk_user_timeout == 0 &&
685 icsk->icsk_probes_out >= keepalive_probes(tp))) {
1da177e4
LT
686 tcp_send_active_reset(sk, GFP_ATOMIC);
687 tcp_write_err(sk);
688 goto out;
689 }
e520af48 690 if (tcp_write_wakeup(sk, LINUX_MIB_TCPKEEPALIVE) <= 0) {
6687e988 691 icsk->icsk_probes_out++;
1da177e4
LT
692 elapsed = keepalive_intvl_when(tp);
693 } else {
694 /* If keepalive was lost due to local congestion,
695 * try harder.
696 */
697 elapsed = TCP_RESOURCE_PROBE_INTERVAL;
698 }
699 } else {
700 /* It is tp->rcv_tstamp + keepalive_time_when(tp) */
701 elapsed = keepalive_time_when(tp) - elapsed;
702 }
703
3ab224be 704 sk_mem_reclaim(sk);
1da177e4
LT
705
706resched:
463c84b9 707 inet_csk_reset_keepalive_timer (sk, elapsed);
1da177e4
LT
708 goto out;
709
e905a9ed 710death:
1da177e4
LT
711 tcp_done(sk);
712
713out:
714 bh_unlock_sock(sk);
715 sock_put(sk);
716}
6f458dfb
ED
717
718void tcp_init_xmit_timers(struct sock *sk)
719{
720 inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
721 &tcp_keepalive_timer);
218af599
ED
722 hrtimer_init(&tcp_sk(sk)->pacing_timer, CLOCK_MONOTONIC,
723 HRTIMER_MODE_ABS_PINNED);
724 tcp_sk(sk)->pacing_timer.function = tcp_pace_kick;
6f458dfb 725}