blob: 27a3b37acd2efea080d1d52e9f4097046e61f05f [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07002/*
3 * net/dccp/timer.c
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -02004 *
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07005 * An implementation of the DCCP protocol
6 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07007 */
8
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07009#include <linux/dccp.h>
10#include <linux/skbuff.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040011#include <linux/export.h>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070012
13#include "dccp.h"
14
Gerrit Renker2e2e9e92006-11-13 13:23:52 -020015/* sysctl variables governing numbers of retransmission attempts */
16int sysctl_dccp_request_retries __read_mostly = TCP_SYN_RETRIES;
17int sysctl_dccp_retries1 __read_mostly = TCP_RETR1;
18int sysctl_dccp_retries2 __read_mostly = TCP_RETR2;
19
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070020static void dccp_write_err(struct sock *sk)
21{
22 sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
Alexander Aringe3ae2362021-06-27 18:48:21 -040023 sk_error_report(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070024
Arnaldo Carvalho de Melo017487d2006-03-20 19:25:24 -080025 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070026 dccp_done(sk);
Eric Dumazetaa62d762016-04-27 16:44:28 -070027 __DCCP_INC_STATS(DCCP_MIB_ABORTONTIMEOUT);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070028}
29
30/* A write timeout has occurred. Process the after effects. */
31static int dccp_write_timeout(struct sock *sk)
32{
33 const struct inet_connection_sock *icsk = inet_csk(sk);
34 int retry_until;
35
36 if (sk->sk_state == DCCP_REQUESTING || sk->sk_state == DCCP_PARTOPEN) {
37 if (icsk->icsk_retransmits != 0)
Eric Dumazetb6c67122010-04-08 23:03:29 +000038 dst_negative_advice(sk);
Gerrit Renker2e2e9e92006-11-13 13:23:52 -020039 retry_until = icsk->icsk_syn_retries ?
40 : sysctl_dccp_request_retries;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070041 } else {
Gerrit Renker2e2e9e92006-11-13 13:23:52 -020042 if (icsk->icsk_retransmits >= sysctl_dccp_retries1) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030043 /* NOTE. draft-ietf-tcpimpl-pmtud-01.txt requires pmtu
44 black hole detection. :-(
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070045
46 It is place to make it. It is not made. I do not want
47 to make it. It is disguisting. It does not work in any
48 case. Let me to cite the same draft, which requires for
49 us to implement this:
50
51 "The one security concern raised by this memo is that ICMP black holes
52 are often caused by over-zealous security administrators who block
53 all ICMP messages. It is vitally important that those who design and
54 deploy security systems understand the impact of strict filtering on
55 upper-layer protocols. The safest web site in the world is worthless
56 if most TCP implementations cannot transfer data from it. It would
57 be far nicer to have all of the black holes fixed rather than fixing
58 all of the TCP implementations."
59
YOSHIFUJI Hideakic9eaf172007-02-09 23:24:38 +090060 Golden words :-).
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070061 */
62
Eric Dumazetb6c67122010-04-08 23:03:29 +000063 dst_negative_advice(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070064 }
65
Gerrit Renker2e2e9e92006-11-13 13:23:52 -020066 retry_until = sysctl_dccp_retries2;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070067 /*
68 * FIXME: see tcp_write_timout and tcp_out_of_resources
69 */
70 }
71
72 if (icsk->icsk_retransmits >= retry_until) {
73 /* Has it gone just too far? */
74 dccp_write_err(sk);
75 return 1;
76 }
77 return 0;
78}
79
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070080/*
81 * The DCCP retransmit timer.
82 */
83static void dccp_retransmit_timer(struct sock *sk)
84{
85 struct inet_connection_sock *icsk = inet_csk(sk);
86
87 /*
Randy Dunlap54633522020-08-22 18:07:13 -070088 * More than 4MSL (8 minutes) has passed, a RESET(aborted) was
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070089 * sent, no need to retransmit, this sock is dead.
90 */
91 if (dccp_write_timeout(sk))
Gerrit Renker59435442008-07-26 11:59:09 +010092 return;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070093
94 /*
95 * We want to know the number of packets retransmitted, not the
96 * total number of retransmissions of clones of original packets.
97 */
98 if (icsk->icsk_retransmits == 0)
Eric Dumazetaa62d762016-04-27 16:44:28 -070099 __DCCP_INC_STATS(DCCP_MIB_TIMEOUTS);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700100
Gerrit Renker59435442008-07-26 11:59:09 +0100101 if (dccp_retransmit_skb(sk) != 0) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700102 /*
103 * Retransmission failed because of local congestion,
104 * do not backoff.
105 */
Gerrit Renker59435442008-07-26 11:59:09 +0100106 if (--icsk->icsk_retransmits == 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700107 icsk->icsk_retransmits = 1;
108 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
109 min(icsk->icsk_rto,
110 TCP_RESOURCE_PROBE_INTERVAL),
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300111 DCCP_RTO_MAX);
Gerrit Renker59435442008-07-26 11:59:09 +0100112 return;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700113 }
114
115 icsk->icsk_backoff++;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700116
117 icsk->icsk_rto = min(icsk->icsk_rto << 1, DCCP_RTO_MAX);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300118 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto,
119 DCCP_RTO_MAX);
Gerrit Renker2e2e9e92006-11-13 13:23:52 -0200120 if (icsk->icsk_retransmits > sysctl_dccp_retries1)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700121 __sk_dst_reset(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700122}
123
Kees Cook59f379f2017-10-16 17:29:19 -0700124static void dccp_write_timer(struct timer_list *t)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700125{
Kees Cook59f379f2017-10-16 17:29:19 -0700126 struct inet_connection_sock *icsk =
127 from_timer(icsk, t, icsk_retransmit_timer);
128 struct sock *sk = &icsk->icsk_inet.sk;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700129 int event = 0;
130
131 bh_lock_sock(sk);
132 if (sock_owned_by_user(sk)) {
133 /* Try again later */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300134 sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
135 jiffies + (HZ / 20));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700136 goto out;
137 }
138
139 if (sk->sk_state == DCCP_CLOSED || !icsk->icsk_pending)
140 goto out;
141
142 if (time_after(icsk->icsk_timeout, jiffies)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300143 sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
144 icsk->icsk_timeout);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700145 goto out;
146 }
147
148 event = icsk->icsk_pending;
149 icsk->icsk_pending = 0;
150
151 switch (event) {
152 case ICSK_TIME_RETRANS:
153 dccp_retransmit_timer(sk);
154 break;
155 }
156out:
157 bh_unlock_sock(sk);
158 sock_put(sk);
159}
160
Kees Cook59f379f2017-10-16 17:29:19 -0700161static void dccp_keepalive_timer(struct timer_list *t)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700162{
Kees Cook59f379f2017-10-16 17:29:19 -0700163 struct sock *sk = from_timer(sk, t, sk_timer);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700164
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700165 pr_err("dccp should not use a keepalive timer !\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700166 sock_put(sk);
167}
Gerrit Renker4ed800d2006-11-13 13:26:51 -0200168
169/* This is the same as tcp_delack_timer, sans prequeue & mem_reclaim stuff */
Kees Cook59f379f2017-10-16 17:29:19 -0700170static void dccp_delack_timer(struct timer_list *t)
Gerrit Renker4ed800d2006-11-13 13:26:51 -0200171{
Kees Cook59f379f2017-10-16 17:29:19 -0700172 struct inet_connection_sock *icsk =
173 from_timer(icsk, t, icsk_delack_timer);
174 struct sock *sk = &icsk->icsk_inet.sk;
Gerrit Renker4ed800d2006-11-13 13:26:51 -0200175
176 bh_lock_sock(sk);
177 if (sock_owned_by_user(sk)) {
178 /* Try again later. */
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700179 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
Gerrit Renker4ed800d2006-11-13 13:26:51 -0200180 sk_reset_timer(sk, &icsk->icsk_delack_timer,
181 jiffies + TCP_DELACK_MIN);
182 goto out;
183 }
184
185 if (sk->sk_state == DCCP_CLOSED ||
186 !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
187 goto out;
188 if (time_after(icsk->icsk_ack.timeout, jiffies)) {
189 sk_reset_timer(sk, &icsk->icsk_delack_timer,
190 icsk->icsk_ack.timeout);
191 goto out;
192 }
193
194 icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
195
196 if (inet_csk_ack_scheduled(sk)) {
Wei Wang31954cd2019-01-25 10:53:19 -0800197 if (!inet_csk_in_pingpong_mode(sk)) {
Gerrit Renker4ed800d2006-11-13 13:26:51 -0200198 /* Delayed ACK missed: inflate ATO. */
199 icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1,
200 icsk->icsk_rto);
201 } else {
202 /* Delayed ACK missed: leave pingpong mode and
203 * deflate ATO.
204 */
Wei Wang31954cd2019-01-25 10:53:19 -0800205 inet_csk_exit_pingpong_mode(sk);
Gerrit Renker4ed800d2006-11-13 13:26:51 -0200206 icsk->icsk_ack.ato = TCP_ATO_MIN;
207 }
208 dccp_send_ack(sk);
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700209 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
Gerrit Renker4ed800d2006-11-13 13:26:51 -0200210 }
211out:
212 bh_unlock_sock(sk);
213 sock_put(sk);
214}
215
Gerrit Renkerdc841e32010-10-27 19:16:26 +0000216/**
217 * dccp_write_xmitlet - Workhorse for CCID packet dequeueing interface
Allen Paisfccf2902020-11-03 14:48:16 +0530218 * @t: pointer to the tasklet associated with this handler
Andrew Lunnd0b11012020-07-13 01:15:00 +0200219 *
Gerrit Renkerdc841e32010-10-27 19:16:26 +0000220 * See the comments above %ccid_dequeueing_decision for supported modes.
221 */
Allen Paisfccf2902020-11-03 14:48:16 +0530222static void dccp_write_xmitlet(struct tasklet_struct *t)
Gerrit Renkeraabb6012007-03-09 13:47:58 -0800223{
Allen Paisfccf2902020-11-03 14:48:16 +0530224 struct dccp_sock *dp = from_tasklet(dp, t, dccps_xmitlet);
225 struct sock *sk = &dp->dccps_inet_connection.icsk_inet.sk;
Gerrit Renkeraabb6012007-03-09 13:47:58 -0800226
227 bh_lock_sock(sk);
228 if (sock_owned_by_user(sk))
Gerrit Renkerdc841e32010-10-27 19:16:26 +0000229 sk_reset_timer(sk, &dccp_sk(sk)->dccps_xmit_timer, jiffies + 1);
Gerrit Renkeraabb6012007-03-09 13:47:58 -0800230 else
Gerrit Renkerb1fcf552010-10-27 19:16:27 +0000231 dccp_write_xmit(sk);
Gerrit Renkeraabb6012007-03-09 13:47:58 -0800232 bh_unlock_sock(sk);
Eric Dumazeta8d7aa12018-05-03 09:39:20 -0700233 sock_put(sk);
Gerrit Renkeraabb6012007-03-09 13:47:58 -0800234}
235
Kees Cook839a6092017-10-24 01:46:09 -0700236static void dccp_write_xmit_timer(struct timer_list *t)
Gerrit Renkeraabb6012007-03-09 13:47:58 -0800237{
Kees Cook839a6092017-10-24 01:46:09 -0700238 struct dccp_sock *dp = from_timer(dp, t, dccps_xmit_timer);
Kees Cook839a6092017-10-24 01:46:09 -0700239
Allen Paisfccf2902020-11-03 14:48:16 +0530240 dccp_write_xmitlet(&dp->dccps_xmitlet);
Gerrit Renkeraabb6012007-03-09 13:47:58 -0800241}
242
Gerrit Renker4ed800d2006-11-13 13:26:51 -0200243void dccp_init_xmit_timers(struct sock *sk)
244{
Gerrit Renkerdc841e32010-10-27 19:16:26 +0000245 struct dccp_sock *dp = dccp_sk(sk);
246
Allen Paisfccf2902020-11-03 14:48:16 +0530247 tasklet_setup(&dp->dccps_xmitlet, dccp_write_xmitlet);
Kees Cook839a6092017-10-24 01:46:09 -0700248 timer_setup(&dp->dccps_xmit_timer, dccp_write_xmit_timer, 0);
Gerrit Renker4ed800d2006-11-13 13:26:51 -0200249 inet_csk_init_xmit_timers(sk, &dccp_write_timer, &dccp_delack_timer,
250 &dccp_keepalive_timer);
251}
Gerrit Renker4c70f382007-09-25 22:40:13 -0700252
253static ktime_t dccp_timestamp_seed;
254/**
255 * dccp_timestamp - 10s of microseconds time source
256 * Returns the number of 10s of microseconds since loading DCCP. This is native
257 * DCCP time difference format (RFC 4340, sec. 13).
258 * Please note: This will wrap around about circa every 11.9 hours.
259 */
260u32 dccp_timestamp(void)
261{
Chen Gang5c4a43b2014-05-21 08:19:34 +0800262 u64 delta = (u64)ktime_us_delta(ktime_get_real(), dccp_timestamp_seed);
Gerrit Renker4c70f382007-09-25 22:40:13 -0700263
264 do_div(delta, 10);
265 return delta;
266}
267EXPORT_SYMBOL_GPL(dccp_timestamp);
268
269void __init dccp_timestamping_init(void)
270{
271 dccp_timestamp_seed = ktime_get_real();
272}