Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/dccp/timer.c |
Arnaldo Carvalho de Melo | 8109b02 | 2006-12-10 16:01:18 -0200 | [diff] [blame] | 3 | * |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 4 | * An implementation of the DCCP protocol |
| 5 | * Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 13 | #include <linux/dccp.h> |
| 14 | #include <linux/skbuff.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 15 | #include <linux/export.h> |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 16 | |
| 17 | #include "dccp.h" |
| 18 | |
Gerrit Renker | 2e2e9e9 | 2006-11-13 13:23:52 -0200 | [diff] [blame] | 19 | /* sysctl variables governing numbers of retransmission attempts */ |
| 20 | int sysctl_dccp_request_retries __read_mostly = TCP_SYN_RETRIES; |
| 21 | int sysctl_dccp_retries1 __read_mostly = TCP_RETR1; |
| 22 | int sysctl_dccp_retries2 __read_mostly = TCP_RETR2; |
| 23 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 24 | static void dccp_write_err(struct sock *sk) |
| 25 | { |
| 26 | sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT; |
| 27 | sk->sk_error_report(sk); |
| 28 | |
Arnaldo Carvalho de Melo | 017487d | 2006-03-20 19:25:24 -0800 | [diff] [blame] | 29 | dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 30 | dccp_done(sk); |
Eric Dumazet | aa62d76 | 2016-04-27 16:44:28 -0700 | [diff] [blame] | 31 | __DCCP_INC_STATS(DCCP_MIB_ABORTONTIMEOUT); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* A write timeout has occurred. Process the after effects. */ |
| 35 | static int dccp_write_timeout(struct sock *sk) |
| 36 | { |
| 37 | const struct inet_connection_sock *icsk = inet_csk(sk); |
| 38 | int retry_until; |
| 39 | |
| 40 | if (sk->sk_state == DCCP_REQUESTING || sk->sk_state == DCCP_PARTOPEN) { |
| 41 | if (icsk->icsk_retransmits != 0) |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 42 | dst_negative_advice(sk); |
Gerrit Renker | 2e2e9e9 | 2006-11-13 13:23:52 -0200 | [diff] [blame] | 43 | retry_until = icsk->icsk_syn_retries ? |
| 44 | : sysctl_dccp_request_retries; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 45 | } else { |
Gerrit Renker | 2e2e9e9 | 2006-11-13 13:23:52 -0200 | [diff] [blame] | 46 | if (icsk->icsk_retransmits >= sysctl_dccp_retries1) { |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 47 | /* NOTE. draft-ietf-tcpimpl-pmtud-01.txt requires pmtu |
| 48 | black hole detection. :-( |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 49 | |
| 50 | It is place to make it. It is not made. I do not want |
| 51 | to make it. It is disguisting. It does not work in any |
| 52 | case. Let me to cite the same draft, which requires for |
| 53 | us to implement this: |
| 54 | |
| 55 | "The one security concern raised by this memo is that ICMP black holes |
| 56 | are often caused by over-zealous security administrators who block |
| 57 | all ICMP messages. It is vitally important that those who design and |
| 58 | deploy security systems understand the impact of strict filtering on |
| 59 | upper-layer protocols. The safest web site in the world is worthless |
| 60 | if most TCP implementations cannot transfer data from it. It would |
| 61 | be far nicer to have all of the black holes fixed rather than fixing |
| 62 | all of the TCP implementations." |
| 63 | |
YOSHIFUJI Hideaki | c9eaf17 | 2007-02-09 23:24:38 +0900 | [diff] [blame] | 64 | Golden words :-). |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 65 | */ |
| 66 | |
Eric Dumazet | b6c6712 | 2010-04-08 23:03:29 +0000 | [diff] [blame] | 67 | dst_negative_advice(sk); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Gerrit Renker | 2e2e9e9 | 2006-11-13 13:23:52 -0200 | [diff] [blame] | 70 | retry_until = sysctl_dccp_retries2; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 71 | /* |
| 72 | * FIXME: see tcp_write_timout and tcp_out_of_resources |
| 73 | */ |
| 74 | } |
| 75 | |
| 76 | if (icsk->icsk_retransmits >= retry_until) { |
| 77 | /* Has it gone just too far? */ |
| 78 | dccp_write_err(sk); |
| 79 | return 1; |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 84 | /* |
| 85 | * The DCCP retransmit timer. |
| 86 | */ |
| 87 | static void dccp_retransmit_timer(struct sock *sk) |
| 88 | { |
| 89 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 90 | |
| 91 | /* |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 92 | * More than than 4MSL (8 minutes) has passed, a RESET(aborted) was |
| 93 | * sent, no need to retransmit, this sock is dead. |
| 94 | */ |
| 95 | if (dccp_write_timeout(sk)) |
Gerrit Renker | 5943544 | 2008-07-26 11:59:09 +0100 | [diff] [blame] | 96 | return; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * We want to know the number of packets retransmitted, not the |
| 100 | * total number of retransmissions of clones of original packets. |
| 101 | */ |
| 102 | if (icsk->icsk_retransmits == 0) |
Eric Dumazet | aa62d76 | 2016-04-27 16:44:28 -0700 | [diff] [blame] | 103 | __DCCP_INC_STATS(DCCP_MIB_TIMEOUTS); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 104 | |
Gerrit Renker | 5943544 | 2008-07-26 11:59:09 +0100 | [diff] [blame] | 105 | if (dccp_retransmit_skb(sk) != 0) { |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 106 | /* |
| 107 | * Retransmission failed because of local congestion, |
| 108 | * do not backoff. |
| 109 | */ |
Gerrit Renker | 5943544 | 2008-07-26 11:59:09 +0100 | [diff] [blame] | 110 | if (--icsk->icsk_retransmits == 0) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 111 | icsk->icsk_retransmits = 1; |
| 112 | inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, |
| 113 | min(icsk->icsk_rto, |
| 114 | TCP_RESOURCE_PROBE_INTERVAL), |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 115 | DCCP_RTO_MAX); |
Gerrit Renker | 5943544 | 2008-07-26 11:59:09 +0100 | [diff] [blame] | 116 | return; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | icsk->icsk_backoff++; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 120 | |
| 121 | icsk->icsk_rto = min(icsk->icsk_rto << 1, DCCP_RTO_MAX); |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 122 | inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, |
| 123 | DCCP_RTO_MAX); |
Gerrit Renker | 2e2e9e9 | 2006-11-13 13:23:52 -0200 | [diff] [blame] | 124 | if (icsk->icsk_retransmits > sysctl_dccp_retries1) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 125 | __sk_dst_reset(sk); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Kees Cook | 59f379f | 2017-10-16 17:29:19 -0700 | [diff] [blame] | 128 | static void dccp_write_timer(struct timer_list *t) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 129 | { |
Kees Cook | 59f379f | 2017-10-16 17:29:19 -0700 | [diff] [blame] | 130 | struct inet_connection_sock *icsk = |
| 131 | from_timer(icsk, t, icsk_retransmit_timer); |
| 132 | struct sock *sk = &icsk->icsk_inet.sk; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 133 | int event = 0; |
| 134 | |
| 135 | bh_lock_sock(sk); |
| 136 | if (sock_owned_by_user(sk)) { |
| 137 | /* Try again later */ |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 138 | sk_reset_timer(sk, &icsk->icsk_retransmit_timer, |
| 139 | jiffies + (HZ / 20)); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 140 | goto out; |
| 141 | } |
| 142 | |
| 143 | if (sk->sk_state == DCCP_CLOSED || !icsk->icsk_pending) |
| 144 | goto out; |
| 145 | |
| 146 | if (time_after(icsk->icsk_timeout, jiffies)) { |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 147 | sk_reset_timer(sk, &icsk->icsk_retransmit_timer, |
| 148 | icsk->icsk_timeout); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 149 | goto out; |
| 150 | } |
| 151 | |
| 152 | event = icsk->icsk_pending; |
| 153 | icsk->icsk_pending = 0; |
| 154 | |
| 155 | switch (event) { |
| 156 | case ICSK_TIME_RETRANS: |
| 157 | dccp_retransmit_timer(sk); |
| 158 | break; |
| 159 | } |
| 160 | out: |
| 161 | bh_unlock_sock(sk); |
| 162 | sock_put(sk); |
| 163 | } |
| 164 | |
Kees Cook | 59f379f | 2017-10-16 17:29:19 -0700 | [diff] [blame] | 165 | static void dccp_keepalive_timer(struct timer_list *t) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 166 | { |
Kees Cook | 59f379f | 2017-10-16 17:29:19 -0700 | [diff] [blame] | 167 | struct sock *sk = from_timer(sk, t, sk_timer); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 168 | |
Eric Dumazet | fa76ce73 | 2015-03-19 19:04:20 -0700 | [diff] [blame] | 169 | pr_err("dccp should not use a keepalive timer !\n"); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 170 | sock_put(sk); |
| 171 | } |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 172 | |
| 173 | /* This is the same as tcp_delack_timer, sans prequeue & mem_reclaim stuff */ |
Kees Cook | 59f379f | 2017-10-16 17:29:19 -0700 | [diff] [blame] | 174 | static void dccp_delack_timer(struct timer_list *t) |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 175 | { |
Kees Cook | 59f379f | 2017-10-16 17:29:19 -0700 | [diff] [blame] | 176 | struct inet_connection_sock *icsk = |
| 177 | from_timer(icsk, t, icsk_delack_timer); |
| 178 | struct sock *sk = &icsk->icsk_inet.sk; |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 179 | |
| 180 | bh_lock_sock(sk); |
| 181 | if (sock_owned_by_user(sk)) { |
| 182 | /* Try again later. */ |
| 183 | icsk->icsk_ack.blocked = 1; |
Eric Dumazet | 02a1d6e | 2016-04-27 16:44:39 -0700 | [diff] [blame] | 184 | __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED); |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 185 | sk_reset_timer(sk, &icsk->icsk_delack_timer, |
| 186 | jiffies + TCP_DELACK_MIN); |
| 187 | goto out; |
| 188 | } |
| 189 | |
| 190 | if (sk->sk_state == DCCP_CLOSED || |
| 191 | !(icsk->icsk_ack.pending & ICSK_ACK_TIMER)) |
| 192 | goto out; |
| 193 | if (time_after(icsk->icsk_ack.timeout, jiffies)) { |
| 194 | sk_reset_timer(sk, &icsk->icsk_delack_timer, |
| 195 | icsk->icsk_ack.timeout); |
| 196 | goto out; |
| 197 | } |
| 198 | |
| 199 | icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER; |
| 200 | |
| 201 | if (inet_csk_ack_scheduled(sk)) { |
| 202 | if (!icsk->icsk_ack.pingpong) { |
| 203 | /* Delayed ACK missed: inflate ATO. */ |
| 204 | icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, |
| 205 | icsk->icsk_rto); |
| 206 | } else { |
| 207 | /* Delayed ACK missed: leave pingpong mode and |
| 208 | * deflate ATO. |
| 209 | */ |
| 210 | icsk->icsk_ack.pingpong = 0; |
| 211 | icsk->icsk_ack.ato = TCP_ATO_MIN; |
| 212 | } |
| 213 | dccp_send_ack(sk); |
Eric Dumazet | 02a1d6e | 2016-04-27 16:44:39 -0700 | [diff] [blame] | 214 | __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS); |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 215 | } |
| 216 | out: |
| 217 | bh_unlock_sock(sk); |
| 218 | sock_put(sk); |
| 219 | } |
| 220 | |
Gerrit Renker | dc841e3 | 2010-10-27 19:16:26 +0000 | [diff] [blame] | 221 | /** |
| 222 | * dccp_write_xmitlet - Workhorse for CCID packet dequeueing interface |
| 223 | * See the comments above %ccid_dequeueing_decision for supported modes. |
| 224 | */ |
| 225 | static void dccp_write_xmitlet(unsigned long data) |
Gerrit Renker | aabb601 | 2007-03-09 13:47:58 -0800 | [diff] [blame] | 226 | { |
| 227 | struct sock *sk = (struct sock *)data; |
Gerrit Renker | aabb601 | 2007-03-09 13:47:58 -0800 | [diff] [blame] | 228 | |
| 229 | bh_lock_sock(sk); |
| 230 | if (sock_owned_by_user(sk)) |
Gerrit Renker | dc841e3 | 2010-10-27 19:16:26 +0000 | [diff] [blame] | 231 | sk_reset_timer(sk, &dccp_sk(sk)->dccps_xmit_timer, jiffies + 1); |
Gerrit Renker | aabb601 | 2007-03-09 13:47:58 -0800 | [diff] [blame] | 232 | else |
Gerrit Renker | b1fcf55 | 2010-10-27 19:16:27 +0000 | [diff] [blame] | 233 | dccp_write_xmit(sk); |
Gerrit Renker | aabb601 | 2007-03-09 13:47:58 -0800 | [diff] [blame] | 234 | bh_unlock_sock(sk); |
Eric Dumazet | a8d7aa1 | 2018-05-03 09:39:20 -0700 | [diff] [blame] | 235 | sock_put(sk); |
Gerrit Renker | aabb601 | 2007-03-09 13:47:58 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Kees Cook | 839a609 | 2017-10-24 01:46:09 -0700 | [diff] [blame] | 238 | static void dccp_write_xmit_timer(struct timer_list *t) |
Gerrit Renker | aabb601 | 2007-03-09 13:47:58 -0800 | [diff] [blame] | 239 | { |
Kees Cook | 839a609 | 2017-10-24 01:46:09 -0700 | [diff] [blame] | 240 | struct dccp_sock *dp = from_timer(dp, t, dccps_xmit_timer); |
| 241 | struct sock *sk = &dp->dccps_inet_connection.icsk_inet.sk; |
| 242 | |
| 243 | dccp_write_xmitlet((unsigned long)sk); |
Gerrit Renker | aabb601 | 2007-03-09 13:47:58 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 246 | void dccp_init_xmit_timers(struct sock *sk) |
| 247 | { |
Gerrit Renker | dc841e3 | 2010-10-27 19:16:26 +0000 | [diff] [blame] | 248 | struct dccp_sock *dp = dccp_sk(sk); |
| 249 | |
| 250 | tasklet_init(&dp->dccps_xmitlet, dccp_write_xmitlet, (unsigned long)sk); |
Kees Cook | 839a609 | 2017-10-24 01:46:09 -0700 | [diff] [blame] | 251 | timer_setup(&dp->dccps_xmit_timer, dccp_write_xmit_timer, 0); |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 252 | inet_csk_init_xmit_timers(sk, &dccp_write_timer, &dccp_delack_timer, |
| 253 | &dccp_keepalive_timer); |
| 254 | } |
Gerrit Renker | 4c70f38 | 2007-09-25 22:40:13 -0700 | [diff] [blame] | 255 | |
| 256 | static ktime_t dccp_timestamp_seed; |
| 257 | /** |
| 258 | * dccp_timestamp - 10s of microseconds time source |
| 259 | * Returns the number of 10s of microseconds since loading DCCP. This is native |
| 260 | * DCCP time difference format (RFC 4340, sec. 13). |
| 261 | * Please note: This will wrap around about circa every 11.9 hours. |
| 262 | */ |
| 263 | u32 dccp_timestamp(void) |
| 264 | { |
Chen Gang | 5c4a43b | 2014-05-21 08:19:34 +0800 | [diff] [blame] | 265 | u64 delta = (u64)ktime_us_delta(ktime_get_real(), dccp_timestamp_seed); |
Gerrit Renker | 4c70f38 | 2007-09-25 22:40:13 -0700 | [diff] [blame] | 266 | |
| 267 | do_div(delta, 10); |
| 268 | return delta; |
| 269 | } |
| 270 | EXPORT_SYMBOL_GPL(dccp_timestamp); |
| 271 | |
| 272 | void __init dccp_timestamping_init(void) |
| 273 | { |
| 274 | dccp_timestamp_seed = ktime_get_real(); |
| 275 | } |