[28449] | 1 | /* $Id: tcp_timer.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * NAT - TCP timers.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
[1] | 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[28449] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[28449] | 26 | */
|
---|
| 27 |
|
---|
| 28 | /*
|
---|
| 29 | * This code is based on:
|
---|
| 30 | *
|
---|
[1] | 31 | * Copyright (c) 1982, 1986, 1988, 1990, 1993
|
---|
[14470] | 32 | * The Regents of the University of California. All rights reserved.
|
---|
[1] | 33 | *
|
---|
| 34 | * Redistribution and use in source and binary forms, with or without
|
---|
| 35 | * modification, are permitted provided that the following conditions
|
---|
| 36 | * are met:
|
---|
| 37 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 38 | * notice, this list of conditions and the following disclaimer.
|
---|
| 39 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 40 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 41 | * documentation and/or other materials provided with the distribution.
|
---|
[95573] | 42 | * 3. Neither the name of the University nor the names of its contributors
|
---|
[1] | 43 | * may be used to endorse or promote products derived from this software
|
---|
| 44 | * without specific prior written permission.
|
---|
| 45 | *
|
---|
| 46 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 47 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 48 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 49 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
| 50 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 51 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 52 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 53 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 54 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 55 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 56 | * SUCH DAMAGE.
|
---|
| 57 | *
|
---|
[14470] | 58 | * @(#)tcp_timer.c 8.1 (Berkeley) 6/10/93
|
---|
[1] | 59 | * tcp_timer.c,v 1.2 1994/08/02 07:49:10 davidg Exp
|
---|
| 60 | */
|
---|
| 61 |
|
---|
| 62 | #include <slirp.h>
|
---|
| 63 |
|
---|
| 64 |
|
---|
[41840] | 65 | static struct tcpcb *tcp_timers(PNATState pData, register struct tcpcb *tp, int timer);
|
---|
[1] | 66 | /*
|
---|
| 67 | * Fast timeout routine for processing delayed acks
|
---|
| 68 | */
|
---|
| 69 | void
|
---|
[1033] | 70 | tcp_fasttimo(PNATState pData)
|
---|
[1] | 71 | {
|
---|
[16443] | 72 | register struct socket *so, *so_next;
|
---|
[14964] | 73 | register struct tcpcb *tp;
|
---|
[1] | 74 |
|
---|
[37936] | 75 | LogFlowFuncEnter();
|
---|
[1033] | 76 |
|
---|
[14964] | 77 | so = tcb.so_next;
|
---|
| 78 | if (so)
|
---|
[17191] | 79 | QSOCKET_FOREACH (so, so_next, tcp)
|
---|
[16443] | 80 | /* { */
|
---|
[14964] | 81 | if ( (tp = (struct tcpcb *)so->so_tcpcb)
|
---|
| 82 | && (tp->t_flags & TF_DELACK))
|
---|
| 83 | {
|
---|
| 84 | tp->t_flags &= ~TF_DELACK;
|
---|
| 85 | tp->t_flags |= TF_ACKNOW;
|
---|
| 86 | tcpstat.tcps_delack++;
|
---|
[16443] | 87 | TCP_OUTPUT(pData, tp);
|
---|
[14964] | 88 | }
|
---|
[16443] | 89 | LOOP_LABEL(tcp, so, so_next);
|
---|
| 90 | }
|
---|
[1] | 91 | }
|
---|
| 92 |
|
---|
| 93 | /*
|
---|
| 94 | * Tcp protocol timeout routine called every 500 ms.
|
---|
| 95 | * Updates the timers in all active tcb's and
|
---|
| 96 | * causes finite state machine actions if timers expire.
|
---|
| 97 | */
|
---|
| 98 | void
|
---|
[1033] | 99 | tcp_slowtimo(PNATState pData)
|
---|
[1] | 100 | {
|
---|
[14964] | 101 | register struct socket *ip, *ipnxt;
|
---|
| 102 | register struct tcpcb *tp;
|
---|
| 103 | register int i;
|
---|
[1] | 104 |
|
---|
[37936] | 105 | LogFlowFuncEnter();
|
---|
[1033] | 106 |
|
---|
[14964] | 107 | /*
|
---|
| 108 | * Search through tcb's and update active timers.
|
---|
| 109 | */
|
---|
| 110 | ip = tcb.so_next;
|
---|
| 111 | if (ip == 0)
|
---|
| 112 | return;
|
---|
[16443] | 113 | QSOCKET_FOREACH(ip, ipnxt, tcp)
|
---|
[23369] | 114 | /* { */
|
---|
[14964] | 115 | ipnxt = ip->so_next;
|
---|
| 116 | tp = sototcpcb(ip);
|
---|
| 117 | if (tp == 0)
|
---|
[16443] | 118 | CONTINUE(tcp);
|
---|
[14964] | 119 | for (i = 0; i < TCPT_NTIMERS; i++)
|
---|
| 120 | {
|
---|
| 121 | if (tp->t_timer[i] && --tp->t_timer[i] == 0)
|
---|
| 122 | {
|
---|
[17191] | 123 | tcp_timers(pData, tp, i);
|
---|
[14964] | 124 | if (ipnxt->so_prev != ip)
|
---|
| 125 | goto tpgone;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | tp->t_idle++;
|
---|
| 129 | if (tp->t_rtt)
|
---|
| 130 | tp->t_rtt++;
|
---|
[1] | 131 | tpgone:
|
---|
[14964] | 132 | ;
|
---|
[16443] | 133 | LOOP_LABEL(tcp, ip, ipnxt);
|
---|
[14964] | 134 | }
|
---|
| 135 | tcp_iss += TCP_ISSINCR / PR_SLOWHZ; /* increment iss */
|
---|
[1] | 136 | #ifdef TCP_COMPAT_42
|
---|
[14964] | 137 | if ((int)tcp_iss < 0)
|
---|
| 138 | tcp_iss = 0; /* XXX */
|
---|
[1] | 139 | #endif
|
---|
[14964] | 140 | tcp_now++; /* for timestamps */
|
---|
[1] | 141 | }
|
---|
| 142 |
|
---|
| 143 | /*
|
---|
| 144 | * Cancel all timers for TCP tp.
|
---|
| 145 | */
|
---|
| 146 | void
|
---|
[14964] | 147 | tcp_canceltimers(struct tcpcb *tp)
|
---|
[1] | 148 | {
|
---|
[14964] | 149 | register int i;
|
---|
[1] | 150 |
|
---|
[14964] | 151 | for (i = 0; i < TCPT_NTIMERS; i++)
|
---|
| 152 | tp->t_timer[i] = 0;
|
---|
[1] | 153 | }
|
---|
| 154 |
|
---|
[14964] | 155 | const int tcp_backoff[TCP_MAXRXTSHIFT + 1] =
|
---|
| 156 | {
|
---|
| 157 | 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64
|
---|
| 158 | };
|
---|
[1] | 159 |
|
---|
| 160 | /*
|
---|
| 161 | * TCP timer processing.
|
---|
| 162 | */
|
---|
[41840] | 163 | static struct tcpcb *
|
---|
[1033] | 164 | tcp_timers(PNATState pData, register struct tcpcb *tp, int timer)
|
---|
[1] | 165 | {
|
---|
[14964] | 166 | register int rexmt;
|
---|
[63668] | 167 | int fUninitializedTemplate = 0;
|
---|
[1033] | 168 |
|
---|
[37936] | 169 | LogFlowFunc(("ENTER: tp:%R[tcpcb793], timer:%d\n", tp, timer));
|
---|
[63668] | 170 | fUninitializedTemplate = RT_BOOL(( tp->t_template.ti_src.s_addr == INADDR_ANY
|
---|
[41411] | 171 | || tp->t_template.ti_dst.s_addr == INADDR_ANY));
|
---|
[63668] | 172 | if (fUninitializedTemplate)
|
---|
[41408] | 173 | {
|
---|
| 174 | tp = tcp_drop(pData, tp, 0);
|
---|
| 175 | return tp;
|
---|
| 176 | }
|
---|
[1033] | 177 |
|
---|
[14964] | 178 | switch (timer)
|
---|
| 179 | {
|
---|
[14470] | 180 | /*
|
---|
| 181 | * 2 MSL timeout in shutdown went off. If we're closed but
|
---|
| 182 | * still waiting for peer to close and connection has been idle
|
---|
| 183 | * too long, or if 2MSL time is up from TIME_WAIT, delete connection
|
---|
| 184 | * control block. Otherwise, check again in a bit.
|
---|
| 185 | */
|
---|
| 186 | case TCPT_2MSL:
|
---|
[14964] | 187 | if (tp->t_state != TCPS_TIME_WAIT &&
|
---|
[14470] | 188 | tp->t_idle <= tcp_maxidle)
|
---|
[14964] | 189 | tp->t_timer[TCPT_2MSL] = tcp_keepintvl;
|
---|
| 190 | else
|
---|
| 191 | tp = tcp_close(pData, tp);
|
---|
| 192 | break;
|
---|
[1] | 193 |
|
---|
[14470] | 194 | /*
|
---|
| 195 | * Retransmission timer went off. Message has not
|
---|
| 196 | * been acked within retransmit interval. Back off
|
---|
| 197 | * to a longer retransmit interval and retransmit one segment.
|
---|
| 198 | */
|
---|
| 199 | case TCPT_REXMT:
|
---|
[22249] | 200 | STAM_COUNTER_INC(&pData->StatTCP_retransmit);
|
---|
[14964] | 201 | /*
|
---|
| 202 | * XXX If a packet has timed out, then remove all the queued
|
---|
| 203 | * packets for that session.
|
---|
| 204 | */
|
---|
| 205 | if (++tp->t_rxtshift > TCP_MAXRXTSHIFT)
|
---|
| 206 | {
|
---|
[14470] | 207 | /*
|
---|
[14964] | 208 | * This is a hack to suit our terminal server here at the uni of canberra
|
---|
| 209 | * since they have trouble with zeroes... It usually lets them through
|
---|
| 210 | * unharmed, but under some conditions, it'll eat the zeros. If we
|
---|
| 211 | * keep retransmitting it, it'll keep eating the zeroes, so we keep
|
---|
| 212 | * retransmitting, and eventually the connection dies...
|
---|
| 213 | * (this only happens on incoming data)
|
---|
| 214 | *
|
---|
| 215 | * So, if we were gonna drop the connection from too many retransmits,
|
---|
| 216 | * don't... instead halve the t_maxseg, which might break up the NULLs and
|
---|
| 217 | * let them through
|
---|
| 218 | *
|
---|
| 219 | * *sigh*
|
---|
[14470] | 220 | */
|
---|
[14964] | 221 | tp->t_maxseg >>= 1;
|
---|
| 222 | if (tp->t_maxseg < 32)
|
---|
| 223 | {
|
---|
| 224 | /*
|
---|
| 225 | * We tried our best, now the connection must die!
|
---|
| 226 | */
|
---|
| 227 | tp->t_rxtshift = TCP_MAXRXTSHIFT;
|
---|
| 228 | tcpstat.tcps_timeoutdrop++;
|
---|
| 229 | tp = tcp_drop(pData, tp, tp->t_softerror);
|
---|
| 230 | /* tp->t_softerror : ETIMEDOUT); */ /* XXX */
|
---|
| 231 | return (tp); /* XXX */
|
---|
| 232 | }
|
---|
[1033] | 233 |
|
---|
[14470] | 234 | /*
|
---|
[14964] | 235 | * Set rxtshift to 6, which is still at the maximum
|
---|
| 236 | * backoff time
|
---|
[14470] | 237 | */
|
---|
[14964] | 238 | tp->t_rxtshift = 6;
|
---|
| 239 | }
|
---|
| 240 | tcpstat.tcps_rexmttimeo++;
|
---|
| 241 | rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
|
---|
| 242 | TCPT_RANGESET(tp->t_rxtcur, rexmt,
|
---|
| 243 | (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
|
---|
| 244 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
|
---|
| 245 | /*
|
---|
| 246 | * If losing, let the lower level know and try for
|
---|
| 247 | * a better route. Also, if we backed off this far,
|
---|
| 248 | * our srtt estimate is probably bogus. Clobber it
|
---|
| 249 | * so we'll take the next rtt measurement as our srtt;
|
---|
| 250 | * move the current srtt into rttvar to keep the current
|
---|
| 251 | * retransmit times until then.
|
---|
| 252 | */
|
---|
| 253 | if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4)
|
---|
| 254 | {
|
---|
| 255 | /* in_losing(tp->t_inpcb); */
|
---|
| 256 | tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
|
---|
| 257 | tp->t_srtt = 0;
|
---|
| 258 | }
|
---|
| 259 | tp->snd_nxt = tp->snd_una;
|
---|
| 260 | /*
|
---|
| 261 | * If timing a segment in this window, stop the timer.
|
---|
| 262 | */
|
---|
| 263 | tp->t_rtt = 0;
|
---|
| 264 | /*
|
---|
| 265 | * Close the congestion window down to one segment
|
---|
| 266 | * (we'll open it by one segment for each ack we get).
|
---|
| 267 | * Since we probably have a window's worth of unacked
|
---|
| 268 | * data accumulated, this "slow start" keeps us from
|
---|
| 269 | * dumping all that data as back-to-back packets (which
|
---|
| 270 | * might overwhelm an intermediate gateway).
|
---|
| 271 | *
|
---|
| 272 | * There are two phases to the opening: Initially we
|
---|
| 273 | * open by one mss on each ack. This makes the window
|
---|
| 274 | * size increase exponentially with time. If the
|
---|
| 275 | * window is larger than the path can handle, this
|
---|
| 276 | * exponential growth results in dropped packet(s)
|
---|
| 277 | * almost immediately. To get more time between
|
---|
| 278 | * drops but still "push" the network to take advantage
|
---|
| 279 | * of improving conditions, we switch from exponential
|
---|
| 280 | * to linear window opening at some threshold size.
|
---|
| 281 | * For a threshold, we use half the current window
|
---|
| 282 | * size, truncated to a multiple of the mss.
|
---|
| 283 | *
|
---|
| 284 | * (the minimum cwnd that will give us exponential
|
---|
| 285 | * growth is 2 mss. We don't allow the threshold
|
---|
| 286 | * to go below this.)
|
---|
| 287 | */
|
---|
| 288 | {
|
---|
[14470] | 289 | u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
|
---|
| 290 | if (win < 2)
|
---|
[14964] | 291 | win = 2;
|
---|
[14470] | 292 | tp->snd_cwnd = tp->t_maxseg;
|
---|
| 293 | tp->snd_ssthresh = win * tp->t_maxseg;
|
---|
| 294 | tp->t_dupacks = 0;
|
---|
[14964] | 295 | }
|
---|
| 296 | (void) tcp_output(pData, tp);
|
---|
| 297 | break;
|
---|
[1] | 298 |
|
---|
[14470] | 299 | /*
|
---|
| 300 | * Persistence timer into zero window.
|
---|
| 301 | * Force a byte to be output, if possible.
|
---|
| 302 | */
|
---|
| 303 | case TCPT_PERSIST:
|
---|
[14964] | 304 | tcpstat.tcps_persisttimeo++;
|
---|
| 305 | tcp_setpersist(tp);
|
---|
| 306 | tp->t_force = 1;
|
---|
| 307 | (void) tcp_output(pData, tp);
|
---|
| 308 | tp->t_force = 0;
|
---|
| 309 | break;
|
---|
[1] | 310 |
|
---|
[14470] | 311 | /*
|
---|
| 312 | * Keep-alive timer went off; send something
|
---|
| 313 | * or drop connection if idle for too long.
|
---|
| 314 | */
|
---|
| 315 | case TCPT_KEEP:
|
---|
[14964] | 316 | tcpstat.tcps_keeptimeo++;
|
---|
| 317 | if (tp->t_state < TCPS_ESTABLISHED)
|
---|
| 318 | goto dropit;
|
---|
| 319 | /* if (tp->t_socket->so_options & SO_KEEPALIVE && */
|
---|
[15845] | 320 | if ((so_options) && tp->t_state <= TCPS_CLOSE_WAIT)
|
---|
| 321 | {
|
---|
[14964] | 322 | if (tp->t_idle >= tcp_keepidle + tcp_maxidle)
|
---|
| 323 | goto dropit;
|
---|
| 324 | /*
|
---|
| 325 | * Send a packet designed to force a response
|
---|
| 326 | * if the peer is up and reachable:
|
---|
| 327 | * either an ACK if the connection is still alive,
|
---|
| 328 | * or an RST if the peer has closed the connection
|
---|
| 329 | * due to timeout or reboot.
|
---|
| 330 | * Using sequence number tp->snd_una-1
|
---|
| 331 | * causes the transmitted zero-length segment
|
---|
| 332 | * to lie outside the receive window;
|
---|
| 333 | * by the protocol spec, this requires the
|
---|
| 334 | * correspondent TCP to respond.
|
---|
| 335 | */
|
---|
| 336 | tcpstat.tcps_keepprobe++;
|
---|
[1] | 337 | #ifdef TCP_COMPAT_42
|
---|
[14964] | 338 | /*
|
---|
| 339 | * The keepalive packet must have nonzero length
|
---|
| 340 | * to get a 4.2 host to respond.
|
---|
| 341 | */
|
---|
| 342 | tcp_respond(tp, &tp->t_template, (struct mbuf *)NULL,
|
---|
[14470] | 343 | tp->rcv_nxt - 1, tp->snd_una - 1, 0);
|
---|
[1] | 344 | #else
|
---|
[14964] | 345 | tcp_respond(pData, tp, &tp->t_template, (struct mbuf *)NULL,
|
---|
| 346 | tp->rcv_nxt, tp->snd_una - 1, 0);
|
---|
[1] | 347 | #endif
|
---|
[14964] | 348 | tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
|
---|
| 349 | }
|
---|
| 350 | else
|
---|
| 351 | tp->t_timer[TCPT_KEEP] = tcp_keepidle;
|
---|
| 352 | break;
|
---|
[1] | 353 |
|
---|
[14470] | 354 | dropit:
|
---|
[14964] | 355 | tcpstat.tcps_keepdrops++;
|
---|
| 356 | tp = tcp_drop(pData, tp, 0); /* ETIMEDOUT); */
|
---|
| 357 | break;
|
---|
| 358 | }
|
---|
[1] | 359 |
|
---|
[14964] | 360 | return tp;
|
---|
[1] | 361 | }
|
---|