Merge tag 'char-misc-5.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6-block.git] / net / dccp / timer.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
7c657876
ACM
2/*
3 * net/dccp/timer.c
8109b02b 4 *
7c657876
ACM
5 * An implementation of the DCCP protocol
6 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7c657876
ACM
7 */
8
7c657876
ACM
9#include <linux/dccp.h>
10#include <linux/skbuff.h>
bc3b2d7f 11#include <linux/export.h>
7c657876
ACM
12
13#include "dccp.h"
14
2e2e9e92
GR
15/* 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
7c657876
ACM
20static void dccp_write_err(struct sock *sk)
21{
22 sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
23 sk->sk_error_report(sk);
24
017487d7 25 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
7c657876 26 dccp_done(sk);
aa62d76b 27 __DCCP_INC_STATS(DCCP_MIB_ABORTONTIMEOUT);
7c657876
ACM
28}
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)
b6c6712a 38 dst_negative_advice(sk);
2e2e9e92
GR
39 retry_until = icsk->icsk_syn_retries ?
40 : sysctl_dccp_request_retries;
7c657876 41 } else {
2e2e9e92 42 if (icsk->icsk_retransmits >= sysctl_dccp_retries1) {
7690af3f
ACM
43 /* NOTE. draft-ietf-tcpimpl-pmtud-01.txt requires pmtu
44 black hole detection. :-(
7c657876
ACM
45
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
c9eaf173 60 Golden words :-).
7c657876
ACM
61 */
62
b6c6712a 63 dst_negative_advice(sk);
7c657876
ACM
64 }
65
2e2e9e92 66 retry_until = sysctl_dccp_retries2;
7c657876
ACM
67 /*
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
7c657876
ACM
80/*
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
8109b02b 87 /*
7c657876
ACM
88 * More than than 4MSL (8 minutes) has passed, a RESET(aborted) was
89 * sent, no need to retransmit, this sock is dead.
90 */
91 if (dccp_write_timeout(sk))
59435444 92 return;
7c657876
ACM
93
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)
aa62d76b 99 __DCCP_INC_STATS(DCCP_MIB_TIMEOUTS);
7c657876 100
59435444 101 if (dccp_retransmit_skb(sk) != 0) {
7c657876
ACM
102 /*
103 * Retransmission failed because of local congestion,
104 * do not backoff.
105 */
59435444 106 if (--icsk->icsk_retransmits == 0)
7c657876
ACM
107 icsk->icsk_retransmits = 1;
108 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
109 min(icsk->icsk_rto,
110 TCP_RESOURCE_PROBE_INTERVAL),
7690af3f 111 DCCP_RTO_MAX);
59435444 112 return;
7c657876
ACM
113 }
114
115 icsk->icsk_backoff++;
7c657876
ACM
116
117 icsk->icsk_rto = min(icsk->icsk_rto << 1, DCCP_RTO_MAX);
7690af3f
ACM
118 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto,
119 DCCP_RTO_MAX);
2e2e9e92 120 if (icsk->icsk_retransmits > sysctl_dccp_retries1)
7c657876 121 __sk_dst_reset(sk);
7c657876
ACM
122}
123
59f379f9 124static void dccp_write_timer(struct timer_list *t)
7c657876 125{
59f379f9
KC
126 struct inet_connection_sock *icsk =
127 from_timer(icsk, t, icsk_retransmit_timer);
128 struct sock *sk = &icsk->icsk_inet.sk;
7c657876
ACM
129 int event = 0;
130
131 bh_lock_sock(sk);
132 if (sock_owned_by_user(sk)) {
133 /* Try again later */
7690af3f
ACM
134 sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
135 jiffies + (HZ / 20));
7c657876
ACM
136 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)) {
7690af3f
ACM
143 sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
144 icsk->icsk_timeout);
7c657876
ACM
145 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
59f379f9 161static void dccp_keepalive_timer(struct timer_list *t)
7c657876 162{
59f379f9 163 struct sock *sk = from_timer(sk, t, sk_timer);
7c657876 164
fa76ce73 165 pr_err("dccp should not use a keepalive timer !\n");
7c657876
ACM
166 sock_put(sk);
167}
4ed800d0
GR
168
169/* This is the same as tcp_delack_timer, sans prequeue & mem_reclaim stuff */
59f379f9 170static void dccp_delack_timer(struct timer_list *t)
4ed800d0 171{
59f379f9
KC
172 struct inet_connection_sock *icsk =
173 from_timer(icsk, t, icsk_delack_timer);
174 struct sock *sk = &icsk->icsk_inet.sk;
4ed800d0
GR
175
176 bh_lock_sock(sk);
177 if (sock_owned_by_user(sk)) {
178 /* Try again later. */
179 icsk->icsk_ack.blocked = 1;
02a1d6e7 180 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
4ed800d0
GR
181 sk_reset_timer(sk, &icsk->icsk_delack_timer,
182 jiffies + TCP_DELACK_MIN);
183 goto out;
184 }
185
186 if (sk->sk_state == DCCP_CLOSED ||
187 !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
188 goto out;
189 if (time_after(icsk->icsk_ack.timeout, jiffies)) {
190 sk_reset_timer(sk, &icsk->icsk_delack_timer,
191 icsk->icsk_ack.timeout);
192 goto out;
193 }
194
195 icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
196
197 if (inet_csk_ack_scheduled(sk)) {
31954cd8 198 if (!inet_csk_in_pingpong_mode(sk)) {
4ed800d0
GR
199 /* Delayed ACK missed: inflate ATO. */
200 icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1,
201 icsk->icsk_rto);
202 } else {
203 /* Delayed ACK missed: leave pingpong mode and
204 * deflate ATO.
205 */
31954cd8 206 inet_csk_exit_pingpong_mode(sk);
4ed800d0
GR
207 icsk->icsk_ack.ato = TCP_ATO_MIN;
208 }
209 dccp_send_ack(sk);
02a1d6e7 210 __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
4ed800d0
GR
211 }
212out:
213 bh_unlock_sock(sk);
214 sock_put(sk);
215}
216
dc841e30
GR
217/**
218 * dccp_write_xmitlet - Workhorse for CCID packet dequeueing interface
219 * See the comments above %ccid_dequeueing_decision for supported modes.
220 */
221static void dccp_write_xmitlet(unsigned long data)
aabb601b
GR
222{
223 struct sock *sk = (struct sock *)data;
aabb601b
GR
224
225 bh_lock_sock(sk);
226 if (sock_owned_by_user(sk))
dc841e30 227 sk_reset_timer(sk, &dccp_sk(sk)->dccps_xmit_timer, jiffies + 1);
aabb601b 228 else
b1fcf55e 229 dccp_write_xmit(sk);
aabb601b 230 bh_unlock_sock(sk);
a8d7aa17 231 sock_put(sk);
aabb601b
GR
232}
233
839a6094 234static void dccp_write_xmit_timer(struct timer_list *t)
aabb601b 235{
839a6094
KC
236 struct dccp_sock *dp = from_timer(dp, t, dccps_xmit_timer);
237 struct sock *sk = &dp->dccps_inet_connection.icsk_inet.sk;
238
239 dccp_write_xmitlet((unsigned long)sk);
aabb601b
GR
240}
241
4ed800d0
GR
242void dccp_init_xmit_timers(struct sock *sk)
243{
dc841e30
GR
244 struct dccp_sock *dp = dccp_sk(sk);
245
246 tasklet_init(&dp->dccps_xmitlet, dccp_write_xmitlet, (unsigned long)sk);
839a6094 247 timer_setup(&dp->dccps_xmit_timer, dccp_write_xmit_timer, 0);
4ed800d0
GR
248 inet_csk_init_xmit_timers(sk, &dccp_write_timer, &dccp_delack_timer,
249 &dccp_keepalive_timer);
250}
4c70f383
GR
251
252static ktime_t dccp_timestamp_seed;
253/**
254 * dccp_timestamp - 10s of microseconds time source
255 * Returns the number of 10s of microseconds since loading DCCP. This is native
256 * DCCP time difference format (RFC 4340, sec. 13).
257 * Please note: This will wrap around about circa every 11.9 hours.
258 */
259u32 dccp_timestamp(void)
260{
5c4a43b0 261 u64 delta = (u64)ktime_us_delta(ktime_get_real(), dccp_timestamp_seed);
4c70f383
GR
262
263 do_div(delta, 10);
264 return delta;
265}
266EXPORT_SYMBOL_GPL(dccp_timestamp);
267
268void __init dccp_timestamping_init(void)
269{
270 dccp_timestamp_seed = ktime_get_real();
271}