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> |
| 15 | |
| 16 | #include "dccp.h" |
| 17 | |
Gerrit Renker | 2e2e9e9 | 2006-11-13 13:23:52 -0200 | [diff] [blame] | 18 | /* sysctl variables governing numbers of retransmission attempts */ |
| 19 | int sysctl_dccp_request_retries __read_mostly = TCP_SYN_RETRIES; |
| 20 | int sysctl_dccp_retries1 __read_mostly = TCP_RETR1; |
| 21 | int sysctl_dccp_retries2 __read_mostly = TCP_RETR2; |
| 22 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 23 | static void dccp_write_err(struct sock *sk) |
| 24 | { |
| 25 | sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT; |
| 26 | sk->sk_error_report(sk); |
| 27 | |
Arnaldo Carvalho de Melo | 017487d | 2006-03-20 19:25:24 -0800 | [diff] [blame] | 28 | dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 29 | dccp_done(sk); |
| 30 | DCCP_INC_STATS_BH(DCCP_MIB_ABORTONTIMEOUT); |
| 31 | } |
| 32 | |
| 33 | /* A write timeout has occurred. Process the after effects. */ |
| 34 | static int dccp_write_timeout(struct sock *sk) |
| 35 | { |
| 36 | const struct inet_connection_sock *icsk = inet_csk(sk); |
| 37 | int retry_until; |
| 38 | |
| 39 | if (sk->sk_state == DCCP_REQUESTING || sk->sk_state == DCCP_PARTOPEN) { |
| 40 | if (icsk->icsk_retransmits != 0) |
| 41 | dst_negative_advice(&sk->sk_dst_cache); |
Gerrit Renker | 2e2e9e9 | 2006-11-13 13:23:52 -0200 | [diff] [blame] | 42 | retry_until = icsk->icsk_syn_retries ? |
| 43 | : sysctl_dccp_request_retries; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 44 | } else { |
Gerrit Renker | 2e2e9e9 | 2006-11-13 13:23:52 -0200 | [diff] [blame] | 45 | if (icsk->icsk_retransmits >= sysctl_dccp_retries1) { |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 46 | /* NOTE. draft-ietf-tcpimpl-pmtud-01.txt requires pmtu |
| 47 | black hole detection. :-( |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 48 | |
| 49 | It is place to make it. It is not made. I do not want |
| 50 | to make it. It is disguisting. It does not work in any |
| 51 | case. Let me to cite the same draft, which requires for |
| 52 | us to implement this: |
| 53 | |
| 54 | "The one security concern raised by this memo is that ICMP black holes |
| 55 | are often caused by over-zealous security administrators who block |
| 56 | all ICMP messages. It is vitally important that those who design and |
| 57 | deploy security systems understand the impact of strict filtering on |
| 58 | upper-layer protocols. The safest web site in the world is worthless |
| 59 | if most TCP implementations cannot transfer data from it. It would |
| 60 | be far nicer to have all of the black holes fixed rather than fixing |
| 61 | all of the TCP implementations." |
| 62 | |
YOSHIFUJI Hideaki | c9eaf17 | 2007-02-09 23:24:38 +0900 | [diff] [blame] | 63 | Golden words :-). |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 64 | */ |
| 65 | |
| 66 | dst_negative_advice(&sk->sk_dst_cache); |
| 67 | } |
| 68 | |
Gerrit Renker | 2e2e9e9 | 2006-11-13 13:23:52 -0200 | [diff] [blame] | 69 | retry_until = sysctl_dccp_retries2; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 70 | /* |
| 71 | * FIXME: see tcp_write_timout and tcp_out_of_resources |
| 72 | */ |
| 73 | } |
| 74 | |
| 75 | if (icsk->icsk_retransmits >= retry_until) { |
| 76 | /* Has it gone just too far? */ |
| 77 | dccp_write_err(sk); |
| 78 | return 1; |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 83 | /* |
| 84 | * The DCCP retransmit timer. |
| 85 | */ |
| 86 | static void dccp_retransmit_timer(struct sock *sk) |
| 87 | { |
| 88 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 89 | |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 90 | /* retransmit timer is used for feature negotiation throughout |
| 91 | * connection. In this case, no packet is re-transmitted, but rather an |
Gerrit Renker | 08a29e4 | 2006-11-13 13:07:51 -0200 | [diff] [blame] | 92 | * ack is generated and pending changes are placed into its options. |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 93 | */ |
| 94 | if (sk->sk_send_head == NULL) { |
| 95 | dccp_pr_debug("feat negotiation retransmit timeout %p\n", sk); |
| 96 | if (sk->sk_state == DCCP_OPEN) |
| 97 | dccp_send_ack(sk); |
| 98 | goto backoff; |
| 99 | } |
| 100 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 101 | /* |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 102 | * More than than 4MSL (8 minutes) has passed, a RESET(aborted) was |
| 103 | * sent, no need to retransmit, this sock is dead. |
| 104 | */ |
| 105 | if (dccp_write_timeout(sk)) |
Gerrit Renker | 5943544 | 2008-07-26 11:59:09 +0100 | [diff] [blame] | 106 | return; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * We want to know the number of packets retransmitted, not the |
| 110 | * total number of retransmissions of clones of original packets. |
| 111 | */ |
| 112 | if (icsk->icsk_retransmits == 0) |
| 113 | DCCP_INC_STATS_BH(DCCP_MIB_TIMEOUTS); |
| 114 | |
Gerrit Renker | 5943544 | 2008-07-26 11:59:09 +0100 | [diff] [blame] | 115 | if (dccp_retransmit_skb(sk) != 0) { |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 116 | /* |
| 117 | * Retransmission failed because of local congestion, |
| 118 | * do not backoff. |
| 119 | */ |
Gerrit Renker | 5943544 | 2008-07-26 11:59:09 +0100 | [diff] [blame] | 120 | if (--icsk->icsk_retransmits == 0) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 121 | icsk->icsk_retransmits = 1; |
| 122 | inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, |
| 123 | min(icsk->icsk_rto, |
| 124 | TCP_RESOURCE_PROBE_INTERVAL), |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 125 | DCCP_RTO_MAX); |
Gerrit Renker | 5943544 | 2008-07-26 11:59:09 +0100 | [diff] [blame] | 126 | return; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 129 | backoff: |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 130 | icsk->icsk_backoff++; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 131 | |
| 132 | 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] | 133 | inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, |
| 134 | DCCP_RTO_MAX); |
Gerrit Renker | 2e2e9e9 | 2006-11-13 13:23:52 -0200 | [diff] [blame] | 135 | if (icsk->icsk_retransmits > sysctl_dccp_retries1) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 136 | __sk_dst_reset(sk); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static void dccp_write_timer(unsigned long data) |
| 140 | { |
| 141 | struct sock *sk = (struct sock *)data; |
| 142 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 143 | int event = 0; |
| 144 | |
| 145 | bh_lock_sock(sk); |
| 146 | if (sock_owned_by_user(sk)) { |
| 147 | /* Try again later */ |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 148 | sk_reset_timer(sk, &icsk->icsk_retransmit_timer, |
| 149 | jiffies + (HZ / 20)); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 150 | goto out; |
| 151 | } |
| 152 | |
| 153 | if (sk->sk_state == DCCP_CLOSED || !icsk->icsk_pending) |
| 154 | goto out; |
| 155 | |
| 156 | if (time_after(icsk->icsk_timeout, jiffies)) { |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 157 | sk_reset_timer(sk, &icsk->icsk_retransmit_timer, |
| 158 | icsk->icsk_timeout); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 159 | goto out; |
| 160 | } |
| 161 | |
| 162 | event = icsk->icsk_pending; |
| 163 | icsk->icsk_pending = 0; |
| 164 | |
| 165 | switch (event) { |
| 166 | case ICSK_TIME_RETRANS: |
| 167 | dccp_retransmit_timer(sk); |
| 168 | break; |
| 169 | } |
| 170 | out: |
| 171 | bh_unlock_sock(sk); |
| 172 | sock_put(sk); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Timer for listening sockets |
| 177 | */ |
| 178 | static void dccp_response_timer(struct sock *sk) |
| 179 | { |
Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 180 | inet_csk_reqsk_queue_prune(sk, TCP_SYNQ_INTERVAL, DCCP_TIMEOUT_INIT, |
| 181 | DCCP_RTO_MAX); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static void dccp_keepalive_timer(unsigned long data) |
| 185 | { |
| 186 | struct sock *sk = (struct sock *)data; |
| 187 | |
| 188 | /* Only process if socket is not in use. */ |
| 189 | bh_lock_sock(sk); |
| 190 | if (sock_owned_by_user(sk)) { |
Arnaldo Carvalho de Melo | 8109b02 | 2006-12-10 16:01:18 -0200 | [diff] [blame] | 191 | /* Try again later. */ |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 192 | inet_csk_reset_keepalive_timer(sk, HZ / 20); |
| 193 | goto out; |
| 194 | } |
| 195 | |
| 196 | if (sk->sk_state == DCCP_LISTEN) { |
| 197 | dccp_response_timer(sk); |
| 198 | goto out; |
| 199 | } |
| 200 | out: |
| 201 | bh_unlock_sock(sk); |
| 202 | sock_put(sk); |
| 203 | } |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 204 | |
| 205 | /* This is the same as tcp_delack_timer, sans prequeue & mem_reclaim stuff */ |
| 206 | static void dccp_delack_timer(unsigned long data) |
| 207 | { |
| 208 | struct sock *sk = (struct sock *)data; |
| 209 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 210 | |
| 211 | bh_lock_sock(sk); |
| 212 | if (sock_owned_by_user(sk)) { |
| 213 | /* Try again later. */ |
| 214 | icsk->icsk_ack.blocked = 1; |
Pavel Emelyanov | de0744a | 2008-07-16 20:31:16 -0700 | [diff] [blame] | 215 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED); |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 216 | sk_reset_timer(sk, &icsk->icsk_delack_timer, |
| 217 | jiffies + TCP_DELACK_MIN); |
| 218 | goto out; |
| 219 | } |
| 220 | |
| 221 | if (sk->sk_state == DCCP_CLOSED || |
| 222 | !(icsk->icsk_ack.pending & ICSK_ACK_TIMER)) |
| 223 | goto out; |
| 224 | if (time_after(icsk->icsk_ack.timeout, jiffies)) { |
| 225 | sk_reset_timer(sk, &icsk->icsk_delack_timer, |
| 226 | icsk->icsk_ack.timeout); |
| 227 | goto out; |
| 228 | } |
| 229 | |
| 230 | icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER; |
| 231 | |
| 232 | if (inet_csk_ack_scheduled(sk)) { |
| 233 | if (!icsk->icsk_ack.pingpong) { |
| 234 | /* Delayed ACK missed: inflate ATO. */ |
| 235 | icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, |
| 236 | icsk->icsk_rto); |
| 237 | } else { |
| 238 | /* Delayed ACK missed: leave pingpong mode and |
| 239 | * deflate ATO. |
| 240 | */ |
| 241 | icsk->icsk_ack.pingpong = 0; |
| 242 | icsk->icsk_ack.ato = TCP_ATO_MIN; |
| 243 | } |
| 244 | dccp_send_ack(sk); |
Pavel Emelyanov | de0744a | 2008-07-16 20:31:16 -0700 | [diff] [blame] | 245 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKS); |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 246 | } |
| 247 | out: |
| 248 | bh_unlock_sock(sk); |
| 249 | sock_put(sk); |
| 250 | } |
| 251 | |
Gerrit Renker | aabb601 | 2007-03-09 13:47:58 -0800 | [diff] [blame] | 252 | /* Transmit-delay timer: used by the CCIDs to delay actual send time */ |
Adrian Bunk | c93a882 | 2007-03-24 21:01:31 -0700 | [diff] [blame] | 253 | static void dccp_write_xmit_timer(unsigned long data) |
Gerrit Renker | aabb601 | 2007-03-09 13:47:58 -0800 | [diff] [blame] | 254 | { |
| 255 | struct sock *sk = (struct sock *)data; |
| 256 | struct dccp_sock *dp = dccp_sk(sk); |
| 257 | |
| 258 | bh_lock_sock(sk); |
| 259 | if (sock_owned_by_user(sk)) |
| 260 | sk_reset_timer(sk, &dp->dccps_xmit_timer, jiffies+1); |
| 261 | else |
| 262 | dccp_write_xmit(sk, 0); |
| 263 | bh_unlock_sock(sk); |
| 264 | sock_put(sk); |
| 265 | } |
| 266 | |
| 267 | static void dccp_init_write_xmit_timer(struct sock *sk) |
| 268 | { |
| 269 | struct dccp_sock *dp = dccp_sk(sk); |
| 270 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 271 | setup_timer(&dp->dccps_xmit_timer, dccp_write_xmit_timer, |
| 272 | (unsigned long)sk); |
Gerrit Renker | aabb601 | 2007-03-09 13:47:58 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 275 | void dccp_init_xmit_timers(struct sock *sk) |
| 276 | { |
Gerrit Renker | aabb601 | 2007-03-09 13:47:58 -0800 | [diff] [blame] | 277 | dccp_init_write_xmit_timer(sk); |
Gerrit Renker | 4ed800d | 2006-11-13 13:26:51 -0200 | [diff] [blame] | 278 | inet_csk_init_xmit_timers(sk, &dccp_write_timer, &dccp_delack_timer, |
| 279 | &dccp_keepalive_timer); |
| 280 | } |
Gerrit Renker | 4c70f38 | 2007-09-25 22:40:13 -0700 | [diff] [blame] | 281 | |
| 282 | static ktime_t dccp_timestamp_seed; |
| 283 | /** |
| 284 | * dccp_timestamp - 10s of microseconds time source |
| 285 | * Returns the number of 10s of microseconds since loading DCCP. This is native |
| 286 | * DCCP time difference format (RFC 4340, sec. 13). |
| 287 | * Please note: This will wrap around about circa every 11.9 hours. |
| 288 | */ |
| 289 | u32 dccp_timestamp(void) |
| 290 | { |
| 291 | s64 delta = ktime_us_delta(ktime_get_real(), dccp_timestamp_seed); |
| 292 | |
| 293 | do_div(delta, 10); |
| 294 | return delta; |
| 295 | } |
| 296 | EXPORT_SYMBOL_GPL(dccp_timestamp); |
| 297 | |
| 298 | void __init dccp_timestamping_init(void) |
| 299 | { |
| 300 | dccp_timestamp_seed = ktime_get_real(); |
| 301 | } |