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