1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/module.h>
22#include <linux/gfp.h>
23#include <net/tcp.h>
24
25
26
27
28
29
30
31
32static void tcp_write_err(struct sock *sk)
33{
34 sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
35 sk->sk_error_report(sk);
36
37 tcp_done(sk);
38 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
39}
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65static 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
71
72 if ((s32)(tcp_jiffies32 - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
73 shift++;
74
75
76 if (sk->sk_err_soft)
77 shift++;
78
79 if (tcp_check_oom(sk, shift)) {
80
81
82 if ((s32)(tcp_jiffies32 - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||
83
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
95 tcp_done(sk);
96 return 1;
97 }
98
99 return 0;
100}
101
102
103
104
105
106
107static int tcp_orphan_retries(struct sock *sk, bool alive)
108{
109 int retries = sock_net(sk)->ipv4.sysctl_tcp_orphan_retries;
110
111
112 if (sk->sk_err_soft && !alive)
113 retries = 0;
114
115
116
117
118 if (retries == 0 && alive)
119 retries = 8;
120 return retries;
121}
122
123static 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
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
147
148
149
150
151
152
153
154
155
156
157
158static 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
190static 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 if (tp->syn_fastopen || tp->syn_data)
202 tcp_fastopen_cache_set(sk, 0, NULL, true, 0);
203 if (tp->syn_data && icsk->icsk_retransmits == 1)
204 NET_INC_STATS(sock_net(sk),
205 LINUX_MIB_TCPFASTOPENACTIVEFAIL);
206 } else if (!tp->syn_data && !tp->syn_fastopen) {
207 sk_rethink_txhash(sk);
208 }
209 retry_until = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
210 expired = icsk->icsk_retransmits >= retry_until;
211 } else {
212 if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1, 0)) {
213
214
215
216
217
218 if (tp->syn_data_acked) {
219 tcp_fastopen_cache_set(sk, 0, NULL, true, 0);
220 if (icsk->icsk_retransmits == net->ipv4.sysctl_tcp_retries1)
221 NET_INC_STATS(sock_net(sk),
222 LINUX_MIB_TCPFASTOPENACTIVEFAIL);
223 }
224
225 tcp_mtu_probing(icsk, sk);
226
227 dst_negative_advice(sk);
228 } else {
229 sk_rethink_txhash(sk);
230 }
231
232 retry_until = net->ipv4.sysctl_tcp_retries2;
233 if (sock_flag(sk, SOCK_DEAD)) {
234 const bool alive = icsk->icsk_rto < TCP_RTO_MAX;
235
236 retry_until = tcp_orphan_retries(sk, alive);
237 do_reset = alive ||
238 !retransmits_timed_out(sk, retry_until, 0);
239
240 if (tcp_out_of_resources(sk, do_reset))
241 return 1;
242 }
243 expired = retransmits_timed_out(sk, retry_until,
244 icsk->icsk_user_timeout);
245 }
246 if (expired) {
247
248 tcp_write_err(sk);
249 return 1;
250 }
251 return 0;
252}
253
254
255void tcp_delack_timer_handler(struct sock *sk)
256{
257 struct inet_connection_sock *icsk = inet_csk(sk);
258
259 sk_mem_reclaim_partial(sk);
260
261 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
262 !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
263 goto out;
264
265 if (time_after(icsk->icsk_ack.timeout, jiffies)) {
266 sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
267 goto out;
268 }
269 icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
270
271 if (inet_csk_ack_scheduled(sk)) {
272 if (!icsk->icsk_ack.pingpong) {
273
274 icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto);
275 } else {
276
277
278
279 icsk->icsk_ack.pingpong = 0;
280 icsk->icsk_ack.ato = TCP_ATO_MIN;
281 }
282 tcp_mstamp_refresh(tcp_sk(sk));
283 tcp_send_ack(sk);
284 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
285 }
286
287out:
288 if (tcp_under_memory_pressure(sk))
289 sk_mem_reclaim(sk);
290}
291
292
293
294
295
296
297
298
299
300
301
302static void tcp_delack_timer(struct timer_list *t)
303{
304 struct inet_connection_sock *icsk =
305 from_timer(icsk, t, icsk_delack_timer);
306 struct sock *sk = &icsk->icsk_inet.sk;
307
308 bh_lock_sock(sk);
309 if (!sock_owned_by_user(sk)) {
310 tcp_delack_timer_handler(sk);
311 } else {
312 icsk->icsk_ack.blocked = 1;
313 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
314
315 if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &sk->sk_tsq_flags))
316 sock_hold(sk);
317 }
318 bh_unlock_sock(sk);
319 sock_put(sk);
320}
321
322static void tcp_probe_timer(struct sock *sk)
323{
324 struct inet_connection_sock *icsk = inet_csk(sk);
325 struct sk_buff *skb = tcp_send_head(sk);
326 struct tcp_sock *tp = tcp_sk(sk);
327 int max_probes;
328 u32 start_ts;
329
330 if (tp->packets_out || !skb) {
331 icsk->icsk_probes_out = 0;
332 return;
333 }
334
335
336
337
338
339
340
341
342
343 start_ts = tcp_skb_timestamp(skb);
344 if (!start_ts)
345 skb->skb_mstamp = tp->tcp_mstamp;
346 else if (icsk->icsk_user_timeout &&
347 (s32)(tcp_time_stamp(tp) - start_ts) >
348 jiffies_to_msecs(icsk->icsk_user_timeout))
349 goto abort;
350
351 max_probes = sock_net(sk)->ipv4.sysctl_tcp_retries2;
352 if (sock_flag(sk, SOCK_DEAD)) {
353 const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX;
354
355 max_probes = tcp_orphan_retries(sk, alive);
356 if (!alive && icsk->icsk_backoff >= max_probes)
357 goto abort;
358 if (tcp_out_of_resources(sk, true))
359 return;
360 }
361
362 if (icsk->icsk_probes_out > max_probes) {
363abort: tcp_write_err(sk);
364 } else {
365
366 tcp_send_probe0(sk);
367 }
368}
369
370
371
372
373
374static void tcp_fastopen_synack_timer(struct sock *sk)
375{
376 struct inet_connection_sock *icsk = inet_csk(sk);
377 int max_retries = icsk->icsk_syn_retries ? :
378 sock_net(sk)->ipv4.sysctl_tcp_synack_retries + 1;
379 struct request_sock *req;
380
381 req = tcp_sk(sk)->fastopen_rsk;
382 req->rsk_ops->syn_ack_timeout(req);
383
384 if (req->num_timeout >= max_retries) {
385 tcp_write_err(sk);
386 return;
387 }
388
389
390
391
392
393 inet_rtx_syn_ack(sk, req);
394 req->num_timeout++;
395 icsk->icsk_retransmits++;
396 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
397 TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
398}
399
400
401
402
403
404
405
406
407
408
409
410
411
412void tcp_retransmit_timer(struct sock *sk)
413{
414 struct tcp_sock *tp = tcp_sk(sk);
415 struct net *net = sock_net(sk);
416 struct inet_connection_sock *icsk = inet_csk(sk);
417
418 if (tp->fastopen_rsk) {
419 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
420 sk->sk_state != TCP_FIN_WAIT1);
421 tcp_fastopen_synack_timer(sk);
422
423
424
425 return;
426 }
427 if (!tp->packets_out)
428 goto out;
429
430 WARN_ON(tcp_rtx_queue_empty(sk));
431
432 tp->tlp_high_seq = 0;
433
434 if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
435 !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
436
437
438
439
440
441 struct inet_sock *inet = inet_sk(sk);
442 if (sk->sk_family == AF_INET) {
443 net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
444 &inet->inet_daddr,
445 ntohs(inet->inet_dport),
446 inet->inet_num,
447 tp->snd_una, tp->snd_nxt);
448 }
449#if IS_ENABLED(CONFIG_IPV6)
450 else if (sk->sk_family == AF_INET6) {
451 net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
452 &sk->sk_v6_daddr,
453 ntohs(inet->inet_dport),
454 inet->inet_num,
455 tp->snd_una, tp->snd_nxt);
456 }
457#endif
458 if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) {
459 tcp_write_err(sk);
460 goto out;
461 }
462 tcp_enter_loss(sk);
463 tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1);
464 __sk_dst_reset(sk);
465 goto out_reset_timer;
466 }
467
468 if (tcp_write_timeout(sk))
469 goto out;
470
471 if (icsk->icsk_retransmits == 0) {
472 int mib_idx;
473
474 if (icsk->icsk_ca_state == TCP_CA_Recovery) {
475 if (tcp_is_sack(tp))
476 mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
477 else
478 mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
479 } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
480 mib_idx = LINUX_MIB_TCPLOSSFAILURES;
481 } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
482 tp->sacked_out) {
483 if (tcp_is_sack(tp))
484 mib_idx = LINUX_MIB_TCPSACKFAILURES;
485 else
486 mib_idx = LINUX_MIB_TCPRENOFAILURES;
487 } else {
488 mib_idx = LINUX_MIB_TCPTIMEOUTS;
489 }
490 __NET_INC_STATS(sock_net(sk), mib_idx);
491 }
492
493 tcp_enter_loss(sk);
494
495 if (tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1) > 0) {
496
497
498
499 if (!icsk->icsk_retransmits)
500 icsk->icsk_retransmits = 1;
501 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
502 min(icsk->icsk_rto, TCP_RESOURCE_PROBE_INTERVAL),
503 TCP_RTO_MAX);
504 goto out;
505 }
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522 icsk->icsk_backoff++;
523 icsk->icsk_retransmits++;
524
525out_reset_timer:
526
527
528
529
530
531
532
533
534
535 if (sk->sk_state == TCP_ESTABLISHED &&
536 (tp->thin_lto || net->ipv4.sysctl_tcp_thin_linear_timeouts) &&
537 tcp_stream_is_thin(tp) &&
538 icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
539 icsk->icsk_backoff = 0;
540 icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX);
541 } else {
542
543 icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
544 }
545 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX);
546 if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1 + 1, 0))
547 __sk_dst_reset(sk);
548
549out:;
550}
551
552
553
554void tcp_write_timer_handler(struct sock *sk)
555{
556 struct inet_connection_sock *icsk = inet_csk(sk);
557 int event;
558
559 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
560 !icsk->icsk_pending)
561 goto out;
562
563 if (time_after(icsk->icsk_timeout, jiffies)) {
564 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
565 goto out;
566 }
567
568 tcp_mstamp_refresh(tcp_sk(sk));
569 event = icsk->icsk_pending;
570
571 switch (event) {
572 case ICSK_TIME_REO_TIMEOUT:
573 tcp_rack_reo_timeout(sk);
574 break;
575 case ICSK_TIME_LOSS_PROBE:
576 tcp_send_loss_probe(sk);
577 break;
578 case ICSK_TIME_RETRANS:
579 icsk->icsk_pending = 0;
580 tcp_retransmit_timer(sk);
581 break;
582 case ICSK_TIME_PROBE0:
583 icsk->icsk_pending = 0;
584 tcp_probe_timer(sk);
585 break;
586 }
587
588out:
589 sk_mem_reclaim(sk);
590}
591
592static void tcp_write_timer(struct timer_list *t)
593{
594 struct inet_connection_sock *icsk =
595 from_timer(icsk, t, icsk_retransmit_timer);
596 struct sock *sk = &icsk->icsk_inet.sk;
597
598 bh_lock_sock(sk);
599 if (!sock_owned_by_user(sk)) {
600 tcp_write_timer_handler(sk);
601 } else {
602
603 if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &sk->sk_tsq_flags))
604 sock_hold(sk);
605 }
606 bh_unlock_sock(sk);
607 sock_put(sk);
608}
609
610void tcp_syn_ack_timeout(const struct request_sock *req)
611{
612 struct net *net = read_pnet(&inet_rsk(req)->ireq_net);
613
614 __NET_INC_STATS(net, LINUX_MIB_TCPTIMEOUTS);
615}
616EXPORT_SYMBOL(tcp_syn_ack_timeout);
617
618void tcp_set_keepalive(struct sock *sk, int val)
619{
620 if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
621 return;
622
623 if (val && !sock_flag(sk, SOCK_KEEPOPEN))
624 inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk)));
625 else if (!val)
626 inet_csk_delete_keepalive_timer(sk);
627}
628EXPORT_SYMBOL_GPL(tcp_set_keepalive);
629
630
631static void tcp_keepalive_timer (struct timer_list *t)
632{
633 struct sock *sk = from_timer(sk, t, sk_timer);
634 struct inet_connection_sock *icsk = inet_csk(sk);
635 struct tcp_sock *tp = tcp_sk(sk);
636 u32 elapsed;
637
638
639 bh_lock_sock(sk);
640 if (sock_owned_by_user(sk)) {
641
642 inet_csk_reset_keepalive_timer (sk, HZ/20);
643 goto out;
644 }
645
646 if (sk->sk_state == TCP_LISTEN) {
647 pr_err("Hmm... keepalive on a LISTEN ???\n");
648 goto out;
649 }
650
651 tcp_mstamp_refresh(tp);
652 if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) {
653 if (tp->linger2 >= 0) {
654 const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN;
655
656 if (tmo > 0) {
657 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
658 goto out;
659 }
660 }
661 tcp_send_active_reset(sk, GFP_ATOMIC);
662 goto death;
663 }
664
665 if (!sock_flag(sk, SOCK_KEEPOPEN) ||
666 ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)))
667 goto out;
668
669 elapsed = keepalive_time_when(tp);
670
671
672 if (tp->packets_out || !tcp_write_queue_empty(sk))
673 goto resched;
674
675 elapsed = keepalive_time_elapsed(tp);
676
677 if (elapsed >= keepalive_time_when(tp)) {
678
679
680
681 if ((icsk->icsk_user_timeout != 0 &&
682 elapsed >= icsk->icsk_user_timeout &&
683 icsk->icsk_probes_out > 0) ||
684 (icsk->icsk_user_timeout == 0 &&
685 icsk->icsk_probes_out >= keepalive_probes(tp))) {
686 tcp_send_active_reset(sk, GFP_ATOMIC);
687 tcp_write_err(sk);
688 goto out;
689 }
690 if (tcp_write_wakeup(sk, LINUX_MIB_TCPKEEPALIVE) <= 0) {
691 icsk->icsk_probes_out++;
692 elapsed = keepalive_intvl_when(tp);
693 } else {
694
695
696
697 elapsed = TCP_RESOURCE_PROBE_INTERVAL;
698 }
699 } else {
700
701 elapsed = keepalive_time_when(tp) - elapsed;
702 }
703
704 sk_mem_reclaim(sk);
705
706resched:
707 inet_csk_reset_keepalive_timer (sk, elapsed);
708 goto out;
709
710death:
711 tcp_done(sk);
712
713out:
714 bh_unlock_sock(sk);
715 sock_put(sk);
716}
717
718void tcp_init_xmit_timers(struct sock *sk)
719{
720 inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
721 &tcp_keepalive_timer);
722 hrtimer_init(&tcp_sk(sk)->pacing_timer, CLOCK_MONOTONIC,
723 HRTIMER_MODE_ABS_PINNED);
724 tcp_sk(sk)->pacing_timer.function = tcp_pace_kick;
725}
726