1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (c) 1982, 1986, 1988, 1990, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	@(#)tcp_timer.c	8.1 (Berkeley) 6/10/93
31  * tcp_timer.c,v 1.2 1994/08/02 07:49:10 davidg Exp
32  */
33 
34 #include "slirp.h"
35 
36 static struct tcpcb *tcp_timers(register struct tcpcb *tp, int timer);
37 
38 /*
39  * Fast timeout routine for processing delayed acks
40  */
tcp_fasttimo(Slirp * slirp)41 void tcp_fasttimo(Slirp *slirp)
42 {
43     register struct socket *so;
44     register struct tcpcb *tp;
45 
46     DEBUG_CALL("tcp_fasttimo");
47 
48     so = slirp->tcb.so_next;
49     if (so)
50         for (; so != &slirp->tcb; so = so->so_next)
51             if ((tp = (struct tcpcb *)so->so_tcpcb) &&
52                 (tp->t_flags & TF_DELACK)) {
53                 tp->t_flags &= ~TF_DELACK;
54                 tp->t_flags |= TF_ACKNOW;
55                 tcp_output(tp);
56             }
57 }
58 
59 /*
60  * Tcp protocol timeout routine called every 500 ms.
61  * Updates the timers in all active tcb's and
62  * causes finite state machine actions if timers expire.
63  */
tcp_slowtimo(Slirp * slirp)64 void tcp_slowtimo(Slirp *slirp)
65 {
66     register struct socket *ip, *ipnxt;
67     register struct tcpcb *tp;
68     register int i;
69 
70     DEBUG_CALL("tcp_slowtimo");
71 
72     /*
73      * Search through tcb's and update active timers.
74      */
75     ip = slirp->tcb.so_next;
76     if (ip == NULL) {
77         return;
78     }
79     for (; ip != &slirp->tcb; ip = ipnxt) {
80         ipnxt = ip->so_next;
81         tp = sototcpcb(ip);
82         if (tp == NULL) {
83             continue;
84         }
85         for (i = 0; i < TCPT_NTIMERS; i++) {
86             if (tp->t_timer[i] && --tp->t_timer[i] == 0) {
87                 tcp_timers(tp, i);
88                 if (ipnxt->so_prev != ip)
89                     goto tpgone;
90             }
91         }
92         tp->t_idle++;
93         if (tp->t_rtt)
94             tp->t_rtt++;
95     tpgone:;
96     }
97     slirp->tcp_iss += TCP_ISSINCR / PR_SLOWHZ; /* increment iss */
98     slirp->tcp_now++; /* for timestamps */
99 }
100 
101 /*
102  * Cancel all timers for TCP tp.
103  */
tcp_canceltimers(struct tcpcb * tp)104 void tcp_canceltimers(struct tcpcb *tp)
105 {
106     register int i;
107 
108     for (i = 0; i < TCPT_NTIMERS; i++)
109         tp->t_timer[i] = 0;
110 }
111 
112 const int tcp_backoff[TCP_MAXRXTSHIFT + 1] = { 1,  2,  4,  8,  16, 32, 64,
113                                                64, 64, 64, 64, 64, 64 };
114 
115 /*
116  * TCP timer processing.
117  */
tcp_timers(register struct tcpcb * tp,int timer)118 static struct tcpcb *tcp_timers(register struct tcpcb *tp, int timer)
119 {
120     register int rexmt;
121 
122     DEBUG_CALL("tcp_timers");
123 
124     switch (timer) {
125     /*
126      * 2 MSL timeout in shutdown went off.  If we're closed but
127      * still waiting for peer to close and connection has been idle
128      * too long, or if 2MSL time is up from TIME_WAIT, delete connection
129      * control block.  Otherwise, check again in a bit.
130      */
131     case TCPT_2MSL:
132         if (tp->t_state != TCPS_TIME_WAIT && tp->t_idle <= TCP_MAXIDLE)
133             tp->t_timer[TCPT_2MSL] = TCPTV_KEEPINTVL;
134         else
135             tp = tcp_close(tp);
136         break;
137 
138     /*
139      * Retransmission timer went off.  Message has not
140      * been acked within retransmit interval.  Back off
141      * to a longer retransmit interval and retransmit one segment.
142      */
143     case TCPT_REXMT:
144 
145         /*
146          * XXXXX If a packet has timed out, then remove all the queued
147          * packets for that session.
148          */
149 
150         if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
151             /*
152              * This is a hack to suit our terminal server here at the uni of
153              * canberra since they have trouble with zeroes... It usually lets
154              * them through unharmed, but under some conditions, it'll eat the
155              * zeros.  If we keep retransmitting it, it'll keep eating the
156              * zeroes, so we keep retransmitting, and eventually the connection
157              * dies... (this only happens on incoming data)
158              *
159              * So, if we were gonna drop the connection from too many
160              * retransmits, don't... instead halve the t_maxseg, which might
161              * break up the NULLs and let them through
162              *
163              * *sigh*
164              */
165 
166             tp->t_maxseg >>= 1;
167             if (tp->t_maxseg < 32) {
168                 /*
169                  * We tried our best, now the connection must die!
170                  */
171                 tp->t_rxtshift = TCP_MAXRXTSHIFT;
172                 tp = tcp_drop(tp, tp->t_softerror);
173                 /* tp->t_softerror : ETIMEDOUT); */ /* XXX */
174                 return (tp); /* XXX */
175             }
176 
177             /*
178              * Set rxtshift to 6, which is still at the maximum
179              * backoff time
180              */
181             tp->t_rxtshift = 6;
182         }
183         rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
184         TCPT_RANGESET(tp->t_rxtcur, rexmt, (short)tp->t_rttmin,
185                       TCPTV_REXMTMAX); /* XXX */
186         tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
187         /*
188          * If losing, let the lower level know and try for
189          * a better route.  Also, if we backed off this far,
190          * our srtt estimate is probably bogus.  Clobber it
191          * so we'll take the next rtt measurement as our srtt;
192          * move the current srtt into rttvar to keep the current
193          * retransmit times until then.
194          */
195         if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
196             tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
197             tp->t_srtt = 0;
198         }
199         tp->snd_nxt = tp->snd_una;
200         /*
201          * If timing a segment in this window, stop the timer.
202          */
203         tp->t_rtt = 0;
204         /*
205          * Close the congestion window down to one segment
206          * (we'll open it by one segment for each ack we get).
207          * Since we probably have a window's worth of unacked
208          * data accumulated, this "slow start" keeps us from
209          * dumping all that data as back-to-back packets (which
210          * might overwhelm an intermediate gateway).
211          *
212          * There are two phases to the opening: Initially we
213          * open by one mss on each ack.  This makes the window
214          * size increase exponentially with time.  If the
215          * window is larger than the path can handle, this
216          * exponential growth results in dropped packet(s)
217          * almost immediately.  To get more time between
218          * drops but still "push" the network to take advantage
219          * of improving conditions, we switch from exponential
220          * to linear window opening at some threshold size.
221          * For a threshold, we use half the current window
222          * size, truncated to a multiple of the mss.
223          *
224          * (the minimum cwnd that will give us exponential
225          * growth is 2 mss.  We don't allow the threshold
226          * to go below this.)
227          */
228         {
229             unsigned win = MIN(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
230             if (win < 2)
231                 win = 2;
232             tp->snd_cwnd = tp->t_maxseg;
233             tp->snd_ssthresh = win * tp->t_maxseg;
234             tp->t_dupacks = 0;
235         }
236         tcp_output(tp);
237         break;
238 
239     /*
240      * Persistence timer into zero window.
241      * Force a byte to be output, if possible.
242      */
243     case TCPT_PERSIST:
244         tcp_setpersist(tp);
245         tp->t_force = 1;
246         tcp_output(tp);
247         tp->t_force = 0;
248         break;
249 
250     /*
251      * Keep-alive timer went off; send something
252      * or drop connection if idle for too long.
253      */
254     case TCPT_KEEP:
255         if (tp->t_state < TCPS_ESTABLISHED)
256             goto dropit;
257 
258         if (slirp_do_keepalive && tp->t_state <= TCPS_CLOSE_WAIT) {
259             if (tp->t_idle >= TCPTV_KEEP_IDLE + TCP_MAXIDLE)
260                 goto dropit;
261             /*
262              * Send a packet designed to force a response
263              * if the peer is up and reachable:
264              * either an ACK if the connection is still alive,
265              * or an RST if the peer has closed the connection
266              * due to timeout or reboot.
267              * Using sequence number tp->snd_una-1
268              * causes the transmitted zero-length segment
269              * to lie outside the receive window;
270              * by the protocol spec, this requires the
271              * correspondent TCP to respond.
272              */
273             tcp_respond(tp, &tp->t_template, (struct mbuf *)NULL, tp->rcv_nxt,
274                         tp->snd_una - 1, 0, tp->t_socket->so_ffamily);
275             tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL;
276         } else
277             tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE;
278         break;
279 
280     dropit:
281         tp = tcp_drop(tp, 0);
282         break;
283     }
284 
285     return (tp);
286 }
287