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