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