Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.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
ab32ea5d
BH
25int sysctl_tcp_syn_retries __read_mostly = TCP_SYN_RETRIES;
26int sysctl_tcp_synack_retries __read_mostly = TCP_SYNACK_RETRIES;
27int sysctl_tcp_keepalive_time __read_mostly = TCP_KEEPALIVE_TIME;
28int sysctl_tcp_keepalive_probes __read_mostly = TCP_KEEPALIVE_PROBES;
29int sysctl_tcp_keepalive_intvl __read_mostly = TCP_KEEPALIVE_INTVL;
30int sysctl_tcp_retries1 __read_mostly = TCP_RETR1;
31int sysctl_tcp_retries2 __read_mostly = TCP_RETR2;
32int sysctl_tcp_orphan_retries __read_mostly;
36e31b0a 33int sysctl_tcp_thin_linear_timeouts __read_mostly;
6fa3eb70
S
34int sysctl_tcp_rto_min __read_mostly = TCP_RTO_MIN;
35EXPORT_SYMBOL(sysctl_tcp_rto_min);
36int sysctl_tcp_rto_max __read_mostly = TCP_RTO_MAX;
37EXPORT_SYMBOL(sysctl_tcp_rto_max);
1da177e4 38
1da177e4
LT
39static void tcp_write_err(struct sock *sk)
40{
41 sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
42 sk->sk_error_report(sk);
43
44 tcp_done(sk);
de0744af 45 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
1da177e4
LT
46}
47
48/* Do not allow orphaned sockets to eat all our resources.
49 * This is direct violation of TCP specs, but it is required
50 * to prevent DoS attacks. It is called when a retransmission timeout
51 * or zero probe timeout occurs on orphaned socket.
52 *
caa20d9a 53 * Criteria is still not confirmed experimentally and may change.
1da177e4
LT
54 * We kill the socket, if:
55 * 1. If number of orphaned sockets exceeds an administratively configured
56 * limit.
57 * 2. If we have strong memory pressure.
58 */
59static int tcp_out_of_resources(struct sock *sk, int do_reset)
60{
61 struct tcp_sock *tp = tcp_sk(sk);
ad1af0fe 62 int shift = 0;
1da177e4 63
e905a9ed 64 /* If peer does not open window for long time, or did not transmit
1da177e4 65 * anything for long time, penalize it. */
6fa3eb70 66 if ((s32)(tcp_time_stamp - tp->lsndtime) > 2*sysctl_tcp_rto_max || !do_reset)
ad1af0fe 67 shift++;
1da177e4
LT
68
69 /* If some dubious ICMP arrived, penalize even more. */
70 if (sk->sk_err_soft)
ad1af0fe 71 shift++;
1da177e4 72
efcdbf24 73 if (tcp_check_oom(sk, shift)) {
1da177e4
LT
74 /* Catch exceptional cases, when connection requires reset.
75 * 1. Last segment was sent recently. */
76 if ((s32)(tcp_time_stamp - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||
77 /* 2. Window is closed. */
78 (!tp->snd_wnd && !tp->packets_out))
79 do_reset = 1;
80 if (do_reset)
81 tcp_send_active_reset(sk, GFP_ATOMIC);
82 tcp_done(sk);
de0744af 83 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
1da177e4
LT
84 return 1;
85 }
86 return 0;
87}
88
89/* Calculate maximal number or retries on an orphaned socket. */
90static int tcp_orphan_retries(struct sock *sk, int alive)
91{
92 int retries = sysctl_tcp_orphan_retries; /* May be zero. */
93
94 /* We know from an ICMP that something is wrong. */
95 if (sk->sk_err_soft && !alive)
96 retries = 0;
97
98 /* However, if socket sent something recently, select some safe
99 * number of retries. 8 corresponds to >100 seconds with minimal
100 * RTO of 200msec. */
101 if (retries == 0 && alive)
102 retries = 8;
103 return retries;
104}
105
ce55dd36
ED
106static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
107{
ce55dd36
ED
108 /* Black hole detection */
109 if (sysctl_tcp_mtu_probing) {
110 if (!icsk->icsk_mtup.enabled) {
111 icsk->icsk_mtup.enabled = 1;
112 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
113 } else {
114 struct tcp_sock *tp = tcp_sk(sk);
829942c1
DM
115 int mss;
116
8beb5c5f 117 mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1;
ce55dd36
ED
118 mss = min(sysctl_tcp_base_mss, mss);
119 mss = max(mss, 68 - tp->tcp_header_len);
120 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
121 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
122 }
123 }
124}
125
2f7de571 126/* This function calculates a "timeout" which is equivalent to the timeout of a
3ad2f3fb 127 * TCP connection after "boundary" unsuccessful, exponentially backed-off
4d22f7d3
DL
128 * retransmissions with an initial RTO of TCP_RTO_MIN or TCP_TIMEOUT_INIT if
129 * syn_set flag is set.
2f7de571
DL
130 */
131static bool retransmits_timed_out(struct sock *sk,
dca43c75 132 unsigned int boundary,
21a180cd 133 unsigned int timeout,
4d22f7d3 134 bool syn_set)
2f7de571 135{
dca43c75 136 unsigned int linear_backoff_thresh, start_ts;
6fa3eb70 137 unsigned int rto_base = syn_set ? TCP_TIMEOUT_INIT : sysctl_tcp_rto_min;
2f7de571
DL
138
139 if (!inet_csk(sk)->icsk_retransmits)
140 return false;
141
142 if (unlikely(!tcp_sk(sk)->retrans_stamp))
143 start_ts = TCP_SKB_CB(tcp_write_queue_head(sk))->when;
144 else
145 start_ts = tcp_sk(sk)->retrans_stamp;
146
dca43c75 147 if (likely(timeout == 0)) {
6fa3eb70 148 linear_backoff_thresh = ilog2(sysctl_tcp_rto_max/rto_base);
2f7de571 149
dca43c75 150 if (boundary <= linear_backoff_thresh)
21a180cd 151 timeout = ((2 << boundary) - 1) * rto_base;
dca43c75 152 else
21a180cd 153 timeout = ((2 << linear_backoff_thresh) - 1) * rto_base +
6fa3eb70 154 (boundary - linear_backoff_thresh) * sysctl_tcp_rto_max;
dca43c75 155 }
2f7de571
DL
156 return (tcp_time_stamp - start_ts) >= timeout;
157}
158
1da177e4
LT
159/* A write timeout has occurred. Process the after effects. */
160static int tcp_write_timeout(struct sock *sk)
161{
5d424d5a 162 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 163 int retry_until;
3db1cd5c 164 bool do_reset, syn_set = false;
1da177e4
LT
165
166 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
463c84b9 167 if (icsk->icsk_retransmits)
b6c6712a 168 dst_negative_advice(sk);
463c84b9 169 retry_until = icsk->icsk_syn_retries ? : sysctl_tcp_syn_retries;
3db1cd5c 170 syn_set = true;
1da177e4 171 } else {
21a180cd 172 if (retransmits_timed_out(sk, sysctl_tcp_retries1, 0, 0)) {
5d424d5a 173 /* Black hole detection */
ce55dd36 174 tcp_mtu_probing(icsk, sk);
1da177e4 175
b6c6712a 176 dst_negative_advice(sk);
1da177e4
LT
177 }
178
179 retry_until = sysctl_tcp_retries2;
180 if (sock_flag(sk, SOCK_DEAD)) {
6fa3eb70 181 const int alive = (icsk->icsk_rto < sysctl_tcp_rto_max);
e905a9ed 182
1da177e4 183 retry_until = tcp_orphan_retries(sk, alive);
6fa12c85 184 do_reset = alive ||
21a180cd 185 !retransmits_timed_out(sk, retry_until, 0, 0);
1da177e4 186
6fa12c85 187 if (tcp_out_of_resources(sk, do_reset))
1da177e4
LT
188 return 1;
189 }
190 }
191
dca43c75 192 if (retransmits_timed_out(sk, retry_until,
21a180cd 193 syn_set ? 0 : icsk->icsk_user_timeout, syn_set)) {
1da177e4
LT
194 /* Has it gone just too far? */
195 tcp_write_err(sk);
196 return 1;
197 }
198 return 0;
199}
200
6f458dfb 201void tcp_delack_timer_handler(struct sock *sk)
1da177e4 202{
1da177e4 203 struct tcp_sock *tp = tcp_sk(sk);
463c84b9 204 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 205
9993e7d3 206 sk_mem_reclaim_partial(sk);
1da177e4 207
9b4c2e72
ED
208 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
209 !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
1da177e4
LT
210 goto out;
211
463c84b9
ACM
212 if (time_after(icsk->icsk_ack.timeout, jiffies)) {
213 sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
1da177e4
LT
214 goto out;
215 }
463c84b9 216 icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
1da177e4 217
b03efcfb 218 if (!skb_queue_empty(&tp->ucopy.prequeue)) {
1da177e4
LT
219 struct sk_buff *skb;
220
de0744af 221 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSCHEDULERFAILED);
1da177e4
LT
222
223 while ((skb = __skb_dequeue(&tp->ucopy.prequeue)) != NULL)
c57943a1 224 sk_backlog_rcv(sk, skb);
1da177e4
LT
225
226 tp->ucopy.memory = 0;
227 }
228
463c84b9
ACM
229 if (inet_csk_ack_scheduled(sk)) {
230 if (!icsk->icsk_ack.pingpong) {
1da177e4 231 /* Delayed ACK missed: inflate ATO. */
463c84b9 232 icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto);
1da177e4
LT
233 } else {
234 /* Delayed ACK missed: leave pingpong mode and
235 * deflate ATO.
236 */
463c84b9
ACM
237 icsk->icsk_ack.pingpong = 0;
238 icsk->icsk_ack.ato = TCP_ATO_MIN;
1da177e4
LT
239 }
240 tcp_send_ack(sk);
de0744af 241 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKS);
1da177e4 242 }
1da177e4
LT
243
244out:
180d8cd9 245 if (sk_under_memory_pressure(sk))
3ab224be 246 sk_mem_reclaim(sk);
6f458dfb
ED
247}
248
249static void tcp_delack_timer(unsigned long data)
250{
251 struct sock *sk = (struct sock *)data;
252
253 bh_lock_sock(sk);
254 if (!sock_owned_by_user(sk)) {
255 tcp_delack_timer_handler(sk);
256 } else {
257 inet_csk(sk)->icsk_ack.blocked = 1;
258 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
259 /* deleguate our work to tcp_release_cb() */
144d56e9
ED
260 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &tcp_sk(sk)->tsq_flags))
261 sock_hold(sk);
6f458dfb 262 }
1da177e4
LT
263 bh_unlock_sock(sk);
264 sock_put(sk);
265}
266
267static void tcp_probe_timer(struct sock *sk)
268{
6687e988 269 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4
LT
270 struct tcp_sock *tp = tcp_sk(sk);
271 int max_probes;
272
fe067e8a 273 if (tp->packets_out || !tcp_send_head(sk)) {
6687e988 274 icsk->icsk_probes_out = 0;
1da177e4
LT
275 return;
276 }
277
278 /* *WARNING* RFC 1122 forbids this
279 *
280 * It doesn't AFAIK, because we kill the retransmit timer -AK
281 *
282 * FIXME: We ought not to do it, Solaris 2.5 actually has fixing
283 * this behaviour in Solaris down as a bug fix. [AC]
284 *
6687e988 285 * Let me to explain. icsk_probes_out is zeroed by incoming ACKs
1da177e4
LT
286 * even if they advertise zero window. Hence, connection is killed only
287 * if we received no ACKs for normal connection timeout. It is not killed
288 * only because window stays zero for some time, window may be zero
289 * until armageddon and even later. We are in full accordance
290 * with RFCs, only probe timer combines both retransmission timeout
291 * and probe timeout in one bottle. --ANK
292 */
293 max_probes = sysctl_tcp_retries2;
294
295 if (sock_flag(sk, SOCK_DEAD)) {
6fa3eb70 296 const int alive = ((icsk->icsk_rto << icsk->icsk_backoff) < sysctl_tcp_rto_max);
e905a9ed 297
1da177e4
LT
298 max_probes = tcp_orphan_retries(sk, alive);
299
6687e988 300 if (tcp_out_of_resources(sk, alive || icsk->icsk_probes_out <= max_probes))
1da177e4
LT
301 return;
302 }
303
6687e988 304 if (icsk->icsk_probes_out > max_probes) {
1da177e4
LT
305 tcp_write_err(sk);
306 } else {
307 /* Only send another probe if we didn't close things up. */
308 tcp_send_probe0(sk);
309 }
310}
311
8336886f
JC
312/*
313 * Timer for Fast Open socket to retransmit SYNACK. Note that the
314 * sk here is the child socket, not the parent (listener) socket.
315 */
316static void tcp_fastopen_synack_timer(struct sock *sk)
317{
318 struct inet_connection_sock *icsk = inet_csk(sk);
319 int max_retries = icsk->icsk_syn_retries ? :
320 sysctl_tcp_synack_retries + 1; /* add one more retry for fastopen */
321 struct request_sock *req;
322
323 req = tcp_sk(sk)->fastopen_rsk;
324 req->rsk_ops->syn_ack_timeout(sk, req);
325
e6c022a4 326 if (req->num_timeout >= max_retries) {
8336886f
JC
327 tcp_write_err(sk);
328 return;
329 }
330 /* XXX (TFO) - Unlike regular SYN-ACK retransmit, we ignore error
331 * returned from rtx_syn_ack() to make it more persistent like
332 * regular retransmit because if the child socket has been accepted
333 * it's not good to give up too easily.
334 */
e6c022a4
ED
335 inet_rtx_syn_ack(sk, req);
336 req->num_timeout++;
8336886f 337 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
6fa3eb70 338 TCP_TIMEOUT_INIT << req->num_timeout, sysctl_tcp_rto_max);
8336886f
JC
339}
340
1da177e4
LT
341/*
342 * The TCP retransmit timer.
343 */
344
f1ecd5d9 345void tcp_retransmit_timer(struct sock *sk)
1da177e4
LT
346{
347 struct tcp_sock *tp = tcp_sk(sk);
463c84b9 348 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 349
8336886f 350 if (tp->fastopen_rsk) {
37561f68
JC
351 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
352 sk->sk_state != TCP_FIN_WAIT1);
8336886f
JC
353 tcp_fastopen_synack_timer(sk);
354 /* Before we receive ACK to our SYN-ACK don't retransmit
355 * anything else (e.g., data or FIN segments).
356 */
357 return;
358 }
1da177e4
LT
359 if (!tp->packets_out)
360 goto out;
361
547b792c 362 WARN_ON(tcp_write_queue_empty(sk));
1da177e4 363
9b717a8d
ND
364 tp->tlp_high_seq = 0;
365
1da177e4
LT
366 if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
367 !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
368 /* Receiver dastardly shrinks window. Our retransmits
369 * become zero probes, but we should not timeout this
370 * connection. If the socket is an orphan, time it out,
371 * we cannot allow such beasts to hang infinitely.
372 */
569508c9
YH
373 struct inet_sock *inet = inet_sk(sk);
374 if (sk->sk_family == AF_INET) {
afd46503
JP
375 LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n"),
376 &inet->inet_daddr,
377 ntohs(inet->inet_dport), inet->inet_num,
378 tp->snd_una, tp->snd_nxt);
1da177e4 379 }
dfd56b8b 380#if IS_ENABLED(CONFIG_IPV6)
569508c9
YH
381 else if (sk->sk_family == AF_INET6) {
382 struct ipv6_pinfo *np = inet6_sk(sk);
afd46503
JP
383 LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n"),
384 &np->daddr,
385 ntohs(inet->inet_dport), inet->inet_num,
386 tp->snd_una, tp->snd_nxt);
569508c9 387 }
1da177e4 388#endif
6fa3eb70 389 if (tcp_time_stamp - tp->rcv_tstamp > sysctl_tcp_rto_max) {
1da177e4
LT
390 tcp_write_err(sk);
391 goto out;
392 }
393 tcp_enter_loss(sk, 0);
fe067e8a 394 tcp_retransmit_skb(sk, tcp_write_queue_head(sk));
1da177e4
LT
395 __sk_dst_reset(sk);
396 goto out_reset_timer;
397 }
398
399 if (tcp_write_timeout(sk))
400 goto out;
401
463c84b9 402 if (icsk->icsk_retransmits == 0) {
40b215e5
PE
403 int mib_idx;
404
c60ce4e2 405 if (icsk->icsk_ca_state == TCP_CA_Recovery) {
bc079e9e
IJ
406 if (tcp_is_sack(tp))
407 mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
408 else
409 mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
6687e988 410 } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
40b215e5 411 mib_idx = LINUX_MIB_TCPLOSSFAILURES;
c60ce4e2
IJ
412 } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
413 tp->sacked_out) {
414 if (tcp_is_sack(tp))
415 mib_idx = LINUX_MIB_TCPSACKFAILURES;
416 else
417 mib_idx = LINUX_MIB_TCPRENOFAILURES;
1da177e4 418 } else {
40b215e5 419 mib_idx = LINUX_MIB_TCPTIMEOUTS;
1da177e4 420 }
de0744af 421 NET_INC_STATS_BH(sock_net(sk), mib_idx);
1da177e4
LT
422 }
423
6fa3eb70
S
424 if (icsk->icsk_MMSRB == 1)
425 {
426 #ifdef CONFIG_MTK_NET_LOGGING
427 printk(KERN_DEBUG "[mtk_net][mmspb] tcp_retransmit_timer enter loss\n");
428 #endif
429 }
9b44190d 430 tcp_enter_loss(sk, 0);
1da177e4 431
fe067e8a 432 if (tcp_retransmit_skb(sk, tcp_write_queue_head(sk)) > 0) {
1da177e4
LT
433 /* Retransmission failed because of local congestion,
434 * do not backoff.
435 */
463c84b9
ACM
436 if (!icsk->icsk_retransmits)
437 icsk->icsk_retransmits = 1;
438 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
3f421baa 439 min(icsk->icsk_rto, TCP_RESOURCE_PROBE_INTERVAL),
6fa3eb70 440 sysctl_tcp_rto_max);
1da177e4
LT
441 goto out;
442 }
443
444 /* Increase the timeout each time we retransmit. Note that
445 * we do not increase the rtt estimate. rto is initialized
446 * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests
447 * that doubling rto each time is the least we can get away with.
448 * In KA9Q, Karn uses this for the first few times, and then
449 * goes to quadratic. netBSD doubles, but only goes up to *64,
450 * and clamps at 1 to 64 sec afterwards. Note that 120 sec is
451 * defined in the protocol as the maximum possible RTT. I guess
452 * we'll have to use something other than TCP to talk to the
453 * University of Mars.
454 *
455 * PAWS allows us longer timeouts and large windows, so once
456 * implemented ftp to mars will work nicely. We will have to fix
457 * the 120 second clamps though!
458 */
463c84b9
ACM
459 icsk->icsk_backoff++;
460 icsk->icsk_retransmits++;
1da177e4
LT
461
462out_reset_timer:
36e31b0a
AP
463 /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is
464 * used to reset timer, set to 0. Recalculate 'icsk_rto' as this
465 * might be increased if the stream oscillates between thin and thick,
466 * thus the old value might already be too high compared to the value
467 * set by 'tcp_set_rto' in tcp_input.c which resets the rto without
468 * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating
469 * exponential backoff behaviour to avoid continue hammering
470 * linear-timeout retransmissions into a black hole
471 */
472 if (sk->sk_state == TCP_ESTABLISHED &&
473 (tp->thin_lto || sysctl_tcp_thin_linear_timeouts) &&
474 tcp_stream_is_thin(tp) &&
475 icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
476 icsk->icsk_backoff = 0;
6fa3eb70 477 icsk->icsk_rto = min_t(unsigned int, __tcp_set_rto(tp), sysctl_tcp_rto_max);
36e31b0a
AP
478 } else {
479 /* Use normal (exponential) backoff */
6fa3eb70 480 icsk->icsk_rto = min_t(unsigned int, icsk->icsk_rto << 1, sysctl_tcp_rto_max);
36e31b0a 481 }
6fa3eb70 482 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, sysctl_tcp_rto_max);
21a180cd 483 if (retransmits_timed_out(sk, sysctl_tcp_retries1 + 1, 0, 0))
1da177e4
LT
484 __sk_dst_reset(sk);
485
486out:;
487}
488
6f458dfb 489void tcp_write_timer_handler(struct sock *sk)
1da177e4 490{
463c84b9 491 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4
LT
492 int event;
493
9b4c2e72
ED
494 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
495 !icsk->icsk_pending)
1da177e4 496 goto out;
6fa3eb70
S
497 if (icsk->icsk_MMSRB != 1)
498 {
463c84b9
ACM
499 if (time_after(icsk->icsk_timeout, jiffies)) {
500 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
1da177e4
LT
501 goto out;
502 }
6fa3eb70 503 }
463c84b9 504 event = icsk->icsk_pending;
1da177e4
LT
505
506 switch (event) {
6ba8a3b1
ND
507 case ICSK_TIME_EARLY_RETRANS:
508 tcp_resume_early_retransmit(sk);
509 break;
510 case ICSK_TIME_LOSS_PROBE:
511 tcp_send_loss_probe(sk);
512 break;
463c84b9 513 case ICSK_TIME_RETRANS:
6ba8a3b1 514 icsk->icsk_pending = 0;
1da177e4
LT
515 tcp_retransmit_timer(sk);
516 break;
463c84b9 517 case ICSK_TIME_PROBE0:
6ba8a3b1 518 icsk->icsk_pending = 0;
1da177e4
LT
519 tcp_probe_timer(sk);
520 break;
521 }
1da177e4
LT
522
523out:
3ab224be 524 sk_mem_reclaim(sk);
6f458dfb
ED
525}
526
527static void tcp_write_timer(unsigned long data)
528{
529 struct sock *sk = (struct sock *)data;
530
531 bh_lock_sock(sk);
532 if (!sock_owned_by_user(sk)) {
533 tcp_write_timer_handler(sk);
534 } else {
6fa3eb70
S
535
536 //if (icsk->icsk_MMSRB == 1)
537 //printk("[mmspb] tcp_write_timer user owner sock\n");
538
6f458dfb 539 /* deleguate our work to tcp_release_cb() */
144d56e9
ED
540 if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &tcp_sk(sk)->tsq_flags))
541 sock_hold(sk);
6f458dfb 542 }
1da177e4
LT
543 bh_unlock_sock(sk);
544 sock_put(sk);
545}
546
295f7324
ACM
547/*
548 * Timer for listening sockets
549 */
550
551static void tcp_synack_timer(struct sock *sk)
552{
a019d6fe 553 inet_csk_reqsk_queue_prune(sk, TCP_SYNQ_INTERVAL,
6fa3eb70 554 TCP_TIMEOUT_INIT, sysctl_tcp_rto_max);
1da177e4
LT
555}
556
72659ecc
OP
557void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req)
558{
559 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPTIMEOUTS);
560}
561EXPORT_SYMBOL(tcp_syn_ack_timeout);
562
1da177e4
LT
563void tcp_set_keepalive(struct sock *sk, int val)
564{
565 if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
566 return;
567
568 if (val && !sock_flag(sk, SOCK_KEEPOPEN))
463c84b9 569 inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk)));
1da177e4 570 else if (!val)
463c84b9 571 inet_csk_delete_keepalive_timer(sk);
1da177e4
LT
572}
573
574
575static void tcp_keepalive_timer (unsigned long data)
576{
577 struct sock *sk = (struct sock *) data;
6687e988 578 struct inet_connection_sock *icsk = inet_csk(sk);
1da177e4 579 struct tcp_sock *tp = tcp_sk(sk);
6c37e5de 580 u32 elapsed;
1da177e4
LT
581
582 /* Only process if socket is not in use. */
583 bh_lock_sock(sk);
584 if (sock_owned_by_user(sk)) {
e905a9ed 585 /* Try again later. */
463c84b9 586 inet_csk_reset_keepalive_timer (sk, HZ/20);
1da177e4
LT
587 goto out;
588 }
589
590 if (sk->sk_state == TCP_LISTEN) {
591 tcp_synack_timer(sk);
592 goto out;
593 }
594
595 if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) {
596 if (tp->linger2 >= 0) {
463c84b9 597 const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN;
1da177e4
LT
598
599 if (tmo > 0) {
600 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
601 goto out;
602 }
603 }
604 tcp_send_active_reset(sk, GFP_ATOMIC);
605 goto death;
606 }
607
608 if (!sock_flag(sk, SOCK_KEEPOPEN) || sk->sk_state == TCP_CLOSE)
609 goto out;
610
611 elapsed = keepalive_time_when(tp);
612
613 /* It is alive without keepalive 8) */
fe067e8a 614 if (tp->packets_out || tcp_send_head(sk))
1da177e4
LT
615 goto resched;
616
6c37e5de 617 elapsed = keepalive_time_elapsed(tp);
1da177e4
LT
618
619 if (elapsed >= keepalive_time_when(tp)) {
dca43c75
JC
620 /* If the TCP_USER_TIMEOUT option is enabled, use that
621 * to determine when to timeout instead.
622 */
623 if ((icsk->icsk_user_timeout != 0 &&
624 elapsed >= icsk->icsk_user_timeout &&
625 icsk->icsk_probes_out > 0) ||
626 (icsk->icsk_user_timeout == 0 &&
627 icsk->icsk_probes_out >= keepalive_probes(tp))) {
1da177e4
LT
628 tcp_send_active_reset(sk, GFP_ATOMIC);
629 tcp_write_err(sk);
630 goto out;
631 }
632 if (tcp_write_wakeup(sk) <= 0) {
6687e988 633 icsk->icsk_probes_out++;
1da177e4
LT
634 elapsed = keepalive_intvl_when(tp);
635 } else {
636 /* If keepalive was lost due to local congestion,
637 * try harder.
638 */
639 elapsed = TCP_RESOURCE_PROBE_INTERVAL;
640 }
641 } else {
642 /* It is tp->rcv_tstamp + keepalive_time_when(tp) */
643 elapsed = keepalive_time_when(tp) - elapsed;
644 }
645
3ab224be 646 sk_mem_reclaim(sk);
1da177e4
LT
647
648resched:
463c84b9 649 inet_csk_reset_keepalive_timer (sk, elapsed);
1da177e4
LT
650 goto out;
651
e905a9ed 652death:
1da177e4
LT
653 tcp_done(sk);
654
655out:
656 bh_unlock_sock(sk);
657 sock_put(sk);
658}
6f458dfb
ED
659
660void tcp_init_xmit_timers(struct sock *sk)
661{
662 inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
663 &tcp_keepalive_timer);
664}
665EXPORT_SYMBOL(tcp_init_xmit_timers);