xref: /openbsd-src/sys/netinet/tcp_timer.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /*	$OpenBSD: tcp_timer.c,v 1.46 2011/07/06 23:44:20 sthen Exp $	*/
2 /*	$NetBSD: tcp_timer.c,v 1.14 1996/02/13 23:44:09 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1988, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)tcp_timer.c	8.1 (Berkeley) 6/10/93
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/protosw.h>
41 #include <sys/kernel.h>
42 #include <sys/pool.h>
43 
44 #include <net/route.h>
45 
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/in_pcb.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/tcp.h>
52 #include <netinet/tcp_fsm.h>
53 #include <netinet/tcp_timer.h>
54 #include <netinet/tcp_var.h>
55 #include <netinet/ip_icmp.h>
56 #include <netinet/tcp_seq.h>
57 
58 int	tcp_always_keepalive;
59 int	tcp_keepidle;
60 int	tcp_keepintvl;
61 int	tcp_maxpersistidle;	/* max idle time in persist */
62 int	tcp_maxidle;
63 
64 /*
65  * Time to delay the ACK.  This is initialized in tcp_init(), unless
66  * its patched.
67  */
68 int	tcp_delack_ticks;
69 
70 void	tcp_timer_rexmt(void *);
71 void	tcp_timer_persist(void *);
72 void	tcp_timer_keep(void *);
73 void	tcp_timer_2msl(void *);
74 
75 const tcp_timer_func_t tcp_timer_funcs[TCPT_NTIMERS] = {
76 	tcp_timer_rexmt,
77 	tcp_timer_persist,
78 	tcp_timer_keep,
79 	tcp_timer_2msl,
80 };
81 
82 /*
83  * Timer state initialization, called from tcp_init().
84  */
85 void
86 tcp_timer_init(void)
87 {
88 
89 	if (tcp_keepidle == 0)
90 		tcp_keepidle = TCPTV_KEEP_IDLE;
91 
92 	if (tcp_keepintvl == 0)
93 		tcp_keepintvl = TCPTV_KEEPINTVL;
94 
95 	if (tcp_maxpersistidle == 0)
96 		tcp_maxpersistidle = TCPTV_KEEP_IDLE;
97 
98 	if (tcp_delack_ticks == 0)
99 		tcp_delack_ticks = TCP_DELACK_TICKS;
100 }
101 
102 /*
103  * Callout to process delayed ACKs for a TCPCB.
104  */
105 void
106 tcp_delack(void *arg)
107 {
108 	struct tcpcb *tp = arg;
109 	int s;
110 
111 	/*
112 	 * If tcp_output() wasn't able to transmit the ACK
113 	 * for whatever reason, it will restart the delayed
114 	 * ACK callout.
115 	 */
116 
117 	s = splsoftnet();
118 	if (tp->t_flags & TF_DEAD) {
119 		splx(s);
120 		return;
121 	}
122 	tp->t_flags |= TF_ACKNOW;
123 	(void) tcp_output(tp);
124 	splx(s);
125 }
126 
127 /*
128  * Tcp protocol timeout routine called every 500 ms.
129  * Updates the timers in all active tcb's and
130  * causes finite state machine actions if timers expire.
131  */
132 void
133 tcp_slowtimo()
134 {
135 	int s;
136 
137 	s = splsoftnet();
138 	tcp_maxidle = TCPTV_KEEPCNT * tcp_keepintvl;
139 	tcp_iss += TCP_ISSINCR2/PR_SLOWHZ;		/* increment iss */
140 	tcp_now++;					/* for timestamps */
141 	splx(s);
142 }
143 
144 /*
145  * Cancel all timers for TCP tp.
146  */
147 void
148 tcp_canceltimers(tp)
149 	struct tcpcb *tp;
150 {
151 	int i;
152 
153 	for (i = 0; i < TCPT_NTIMERS; i++)
154 		TCP_TIMER_DISARM(tp, i);
155 }
156 
157 int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
158     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
159 
160 int tcp_totbackoff = 511;	/* sum of tcp_backoff[] */
161 
162 /*
163  * TCP timer processing.
164  */
165 
166 #ifdef TCP_SACK
167 void	tcp_timer_freesack(struct tcpcb *);
168 
169 void
170 tcp_timer_freesack(struct tcpcb *tp)
171 {
172 	struct sackhole *p, *q;
173 	/*
174 	 * Free SACK holes for 2MSL and REXMT timers.
175 	 */
176 	q = tp->snd_holes;
177 	while (q != NULL) {
178 		p = q;
179 		q = q->next;
180 		pool_put(&sackhl_pool, p);
181 	}
182 	tp->snd_holes = 0;
183 #ifdef TCP_FACK
184 	tp->snd_fack = tp->snd_una;
185 	tp->retran_data = 0;
186 	tp->snd_awnd = 0;
187 #endif /* TCP_FACK */
188 }
189 #endif /* TCP_SACK */
190 
191 void
192 tcp_timer_rexmt(void *arg)
193 {
194 	struct tcpcb *tp = arg;
195 	uint32_t rto;
196 	int s;
197 
198 	s = splsoftnet();
199 	if (tp->t_flags & TF_DEAD) {
200 		splx(s);
201 		return;
202 	}
203 
204 	if ((tp->t_flags & TF_PMTUD_PEND) && tp->t_inpcb &&
205 	    SEQ_GEQ(tp->t_pmtud_th_seq, tp->snd_una) &&
206 	    SEQ_LT(tp->t_pmtud_th_seq, (int)(tp->snd_una + tp->t_maxseg))) {
207 		extern struct sockaddr_in icmpsrc;
208 		struct icmp icmp;
209 
210 		tp->t_flags &= ~TF_PMTUD_PEND;
211 
212 		/* XXX create fake icmp message with relevant entries */
213 		icmp.icmp_nextmtu = tp->t_pmtud_nextmtu;
214 		icmp.icmp_ip.ip_len = tp->t_pmtud_ip_len;
215 		icmp.icmp_ip.ip_hl = tp->t_pmtud_ip_hl;
216 		icmpsrc.sin_addr = tp->t_inpcb->inp_faddr;
217 		icmp_mtudisc(&icmp, tp->t_inpcb->inp_rtableid);
218 
219 		/*
220 		 * Notify all connections to the same peer about
221 		 * new mss and trigger retransmit.
222 		 */
223 		in_pcbnotifyall(&tcbtable, sintosa(&icmpsrc),
224 		    tp->t_inpcb->inp_rtableid, EMSGSIZE, tcp_mtudisc);
225 		splx(s);
226 		return;
227 	}
228 
229 #ifdef TCP_SACK
230 	tcp_timer_freesack(tp);
231 #endif
232 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
233 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
234 		tcpstat.tcps_timeoutdrop++;
235 		(void)tcp_drop(tp, tp->t_softerror ?
236 		    tp->t_softerror : ETIMEDOUT);
237 		goto out;
238 	}
239 	tcpstat.tcps_rexmttimeo++;
240 	rto = TCP_REXMTVAL(tp);
241 	if (rto < tp->t_rttmin)
242 		rto = tp->t_rttmin;
243 	TCPT_RANGESET(tp->t_rxtcur,
244 	    rto * tcp_backoff[tp->t_rxtshift],
245 	    tp->t_rttmin, TCPTV_REXMTMAX);
246 	TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
247 
248 	/*
249 	 * If we are losing and we are trying path MTU discovery,
250 	 * try turning it off.  This will avoid black holes in
251 	 * the network which suppress or fail to send "packet
252 	 * too big" ICMP messages.  We should ideally do
253 	 * lots more sophisticated searching to find the right
254 	 * value here...
255 	 */
256 	if (ip_mtudisc && tp->t_inpcb &&
257 	    TCPS_HAVEESTABLISHED(tp->t_state) &&
258 	    tp->t_rxtshift > TCP_MAXRXTSHIFT / 6) {
259 		struct inpcb *inp = tp->t_inpcb;
260 		struct rtentry *rt = NULL;
261 		struct sockaddr_in sin;
262 
263 		/* No data to send means path mtu is not a problem */
264 		if (!inp->inp_socket->so_snd.sb_cc)
265 			goto leave;
266 
267 		rt = in_pcbrtentry(inp);
268 		/* Check if path MTU discovery is disabled already */
269 		if (rt && (rt->rt_flags & RTF_HOST) &&
270 		    (rt->rt_rmx.rmx_locks & RTV_MTU))
271 			goto leave;
272 
273 		rt = NULL;
274 		switch(tp->pf) {
275 #ifdef INET6
276 		case PF_INET6:
277 			/*
278 			 * We can not turn off path MTU for IPv6.
279 			 * Do nothing for now, maybe lower to
280 			 * minimum MTU.
281 			 */
282 			break;
283 #endif
284 		case PF_INET:
285 			bzero(&sin, sizeof(struct sockaddr_in));
286 			sin.sin_family = AF_INET;
287 			sin.sin_len = sizeof(struct sockaddr_in);
288 			sin.sin_addr = inp->inp_faddr;
289 			rt = icmp_mtudisc_clone(sintosa(&sin),
290 			    inp->inp_rtableid);
291 			break;
292 		}
293 		if (rt != NULL) {
294 			/* Disable path MTU discovery */
295 			if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
296 				rt->rt_rmx.rmx_locks |= RTV_MTU;
297 				in_rtchange(inp, 0);
298 			}
299 
300 			rtfree(rt);
301 		}
302 	leave:
303 		;
304 	}
305 
306 	/*
307 	 * If losing, let the lower level know and try for
308 	 * a better route.  Also, if we backed off this far,
309 	 * our srtt estimate is probably bogus.  Clobber it
310 	 * so we'll take the next rtt measurement as our srtt;
311 	 * move the current srtt into rttvar to keep the current
312 	 * retransmit times until then.
313 	 */
314 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
315 		in_losing(tp->t_inpcb);
316 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
317 		tp->t_srtt = 0;
318 	}
319 	tp->snd_nxt = tp->snd_una;
320 #if defined(TCP_SACK)
321 	/*
322 	 * Note:  We overload snd_last to function also as the
323 	 * snd_last variable described in RFC 2582
324 	 */
325 	tp->snd_last = tp->snd_max;
326 #endif /* TCP_SACK */
327 	/*
328 	 * If timing a segment in this window, stop the timer.
329 	 */
330 	tp->t_rtttime = 0;
331 #ifdef TCP_ECN
332 	/*
333 	 * if ECN is enabled, there might be a broken firewall which
334 	 * blocks ecn packets.  fall back to non-ecn.
335 	 */
336 	if ((tp->t_state == TCPS_SYN_SENT || tp->t_state == TCPS_SYN_RECEIVED)
337 	    && tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN))
338 		tp->t_flags |= TF_DISABLE_ECN;
339 #endif
340 	/*
341 	 * Close the congestion window down to one segment
342 	 * (we'll open it by one segment for each ack we get).
343 	 * Since we probably have a window's worth of unacked
344 	 * data accumulated, this "slow start" keeps us from
345 	 * dumping all that data as back-to-back packets (which
346 	 * might overwhelm an intermediate gateway).
347 	 *
348 	 * There are two phases to the opening: Initially we
349 	 * open by one mss on each ack.  This makes the window
350 	 * size increase exponentially with time.  If the
351 	 * window is larger than the path can handle, this
352 	 * exponential growth results in dropped packet(s)
353 	 * almost immediately.  To get more time between
354 	 * drops but still "push" the network to take advantage
355 	 * of improving conditions, we switch from exponential
356 	 * to linear window opening at some threshold size.
357 	 * For a threshold, we use half the current window
358 	 * size, truncated to a multiple of the mss.
359 	 *
360 	 * (the minimum cwnd that will give us exponential
361 	 * growth is 2 mss.  We don't allow the threshold
362 	 * to go below this.)
363 	 */
364 	{
365 		u_long win = ulmin(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
366 		if (win < 2)
367 			win = 2;
368 		tp->snd_cwnd = tp->t_maxseg;
369 		tp->snd_ssthresh = win * tp->t_maxseg;
370 		tp->t_dupacks = 0;
371 #ifdef TCP_ECN
372 		tp->snd_last = tp->snd_max;
373 		tp->t_flags |= TF_SEND_CWR;
374 #endif
375 #if 1 /* TCP_ECN */
376 		tcpstat.tcps_cwr_timeout++;
377 #endif
378 	}
379 	(void) tcp_output(tp);
380 
381  out:
382 	splx(s);
383 }
384 
385 void
386 tcp_timer_persist(void *arg)
387 {
388 	struct tcpcb *tp = arg;
389 	uint32_t rto;
390 	int s;
391 
392 	s = splsoftnet();
393 	if ((tp->t_flags & TF_DEAD) ||
394             TCP_TIMER_ISARMED(tp, TCPT_REXMT)) {
395 		splx(s);
396 		return;
397 	}
398 	tcpstat.tcps_persisttimeo++;
399 	/*
400 	 * Hack: if the peer is dead/unreachable, we do not
401 	 * time out if the window is closed.  After a full
402 	 * backoff, drop the connection if the idle time
403 	 * (no responses to probes) reaches the maximum
404 	 * backoff that we would use if retransmitting.
405 	 */
406 	rto = TCP_REXMTVAL(tp);
407 	if (rto < tp->t_rttmin)
408 		rto = tp->t_rttmin;
409 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
410 	    ((tcp_now - tp->t_rcvtime) >= tcp_maxpersistidle ||
411 	    (tcp_now - tp->t_rcvtime) >= rto * tcp_totbackoff)) {
412 		tcpstat.tcps_persistdrop++;
413 		tp = tcp_drop(tp, ETIMEDOUT);
414 		goto out;
415 	}
416 	tcp_setpersist(tp);
417 	tp->t_force = 1;
418 	(void) tcp_output(tp);
419 	tp->t_force = 0;
420  out:
421 	splx(s);
422 }
423 
424 void
425 tcp_timer_keep(void *arg)
426 {
427 	struct tcpcb *tp = arg;
428 	int s;
429 
430 	s = splsoftnet();
431 	if (tp->t_flags & TF_DEAD) {
432 		splx(s);
433 		return;
434 	}
435 
436 	tcpstat.tcps_keeptimeo++;
437 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
438 		goto dropit;
439 	if ((tcp_always_keepalive ||
440 	    tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) &&
441 	    tp->t_state <= TCPS_CLOSING) {
442 		if ((tcp_maxidle > 0) &&
443 		    ((tcp_now - tp->t_rcvtime) >= tcp_keepidle + tcp_maxidle))
444 			goto dropit;
445 		/*
446 		 * Send a packet designed to force a response
447 		 * if the peer is up and reachable:
448 		 * either an ACK if the connection is still alive,
449 		 * or an RST if the peer has closed the connection
450 		 * due to timeout or reboot.
451 		 * Using sequence number tp->snd_una-1
452 		 * causes the transmitted zero-length segment
453 		 * to lie outside the receive window;
454 		 * by the protocol spec, this requires the
455 		 * correspondent TCP to respond.
456 		 */
457 		tcpstat.tcps_keepprobe++;
458 		tcp_respond(tp, mtod(tp->t_template, caddr_t),
459 		    NULL, tp->rcv_nxt, tp->snd_una - 1, 0, 0);
460 		TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepintvl);
461 	} else
462 		TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
463 
464 	splx(s);
465 	return;
466 
467  dropit:
468 	tcpstat.tcps_keepdrops++;
469 	tp = tcp_drop(tp, ETIMEDOUT);
470 
471 	splx(s);
472 }
473 
474 void
475 tcp_timer_2msl(void *arg)
476 {
477 	struct tcpcb *tp = arg;
478 	int s;
479 
480 	s = splsoftnet();
481 	if (tp->t_flags & TF_DEAD) {
482 		splx(s);
483 		return;
484 	}
485 
486 #ifdef TCP_SACK
487 	tcp_timer_freesack(tp);
488 #endif
489 
490 	if (tp->t_state != TCPS_TIME_WAIT &&
491 	    ((tcp_maxidle == 0) || ((tcp_now - tp->t_rcvtime) <= tcp_maxidle)))
492 		TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_keepintvl);
493 	else
494 		tp = tcp_close(tp);
495 
496 	splx(s);
497 }
498