Home | History | Annotate | Line # | Download | only in netinet
tcp_timer.c revision 1.77
      1 /*	$NetBSD: tcp_timer.c,v 1.77 2007/06/20 15:29:18 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1997, 1998, 2001, 2005 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     38  * Facility, NASA Ames Research Center.
     39  * This code is derived from software contributed to The NetBSD Foundation
     40  * by Charles M. Hannum.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the NetBSD
     53  *	Foundation, Inc. and its contributors.
     54  * 4. Neither the name of The NetBSD Foundation nor the names of its
     55  *    contributors may be used to endorse or promote products derived
     56  *    from this software without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     59  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     60  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     61  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     62  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     63  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     64  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     65  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     66  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     67  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     68  * POSSIBILITY OF SUCH DAMAGE.
     69  */
     70 
     71 /*
     72  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
     73  *	The Regents of the University of California.  All rights reserved.
     74  *
     75  * Redistribution and use in source and binary forms, with or without
     76  * modification, are permitted provided that the following conditions
     77  * are met:
     78  * 1. Redistributions of source code must retain the above copyright
     79  *    notice, this list of conditions and the following disclaimer.
     80  * 2. Redistributions in binary form must reproduce the above copyright
     81  *    notice, this list of conditions and the following disclaimer in the
     82  *    documentation and/or other materials provided with the distribution.
     83  * 3. Neither the name of the University nor the names of its contributors
     84  *    may be used to endorse or promote products derived from this software
     85  *    without specific prior written permission.
     86  *
     87  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     88  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     89  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     90  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     91  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     92  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     93  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     94  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     95  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     96  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     97  * SUCH DAMAGE.
     98  *
     99  *	@(#)tcp_timer.c	8.2 (Berkeley) 5/24/95
    100  */
    101 
    102 #include <sys/cdefs.h>
    103 __KERNEL_RCSID(0, "$NetBSD: tcp_timer.c,v 1.77 2007/06/20 15:29:18 christos Exp $");
    104 
    105 #include "opt_inet.h"
    106 #include "opt_tcp_debug.h"
    107 
    108 #include <sys/param.h>
    109 #include <sys/systm.h>
    110 #include <sys/malloc.h>
    111 #include <sys/mbuf.h>
    112 #include <sys/socket.h>
    113 #include <sys/socketvar.h>
    114 #include <sys/protosw.h>
    115 #include <sys/errno.h>
    116 #include <sys/kernel.h>
    117 
    118 #include <net/if.h>
    119 #include <net/route.h>
    120 
    121 #include <netinet/in.h>
    122 #include <netinet/in_systm.h>
    123 #include <netinet/ip.h>
    124 #include <netinet/in_pcb.h>
    125 #include <netinet/ip_var.h>
    126 #include <netinet/ip_icmp.h>
    127 
    128 #ifdef INET6
    129 #ifndef INET
    130 #include <netinet/in.h>
    131 #endif
    132 #include <netinet/ip6.h>
    133 #include <netinet6/in6_pcb.h>
    134 #endif
    135 
    136 #include <netinet/tcp.h>
    137 #include <netinet/tcp_fsm.h>
    138 #include <netinet/tcp_seq.h>
    139 #include <netinet/tcp_timer.h>
    140 #include <netinet/tcp_var.h>
    141 #include <netinet/tcp_congctl.h>
    142 #include <netinet/tcpip.h>
    143 #ifdef TCP_DEBUG
    144 #include <netinet/tcp_debug.h>
    145 #endif
    146 
    147 /*
    148  * Various tunable timer parameters.  These are initialized in tcp_init(),
    149  * unless they are patched.
    150  */
    151 u_int	tcp_keepinit = 0;
    152 u_int	tcp_keepidle = 0;
    153 u_int	tcp_keepintvl = 0;
    154 u_int	tcp_keepcnt = 0;		/* max idle probes */
    155 
    156 int	tcp_maxpersistidle = 0;		/* max idle time in persist */
    157 
    158 /*
    159  * Time to delay the ACK.  This is initialized in tcp_init(), unless
    160  * its patched.
    161  */
    162 int	tcp_delack_ticks = 0;
    163 
    164 void	tcp_timer_rexmt(void *);
    165 void	tcp_timer_persist(void *);
    166 void	tcp_timer_keep(void *);
    167 void	tcp_timer_2msl(void *);
    168 
    169 const tcp_timer_func_t tcp_timer_funcs[TCPT_NTIMERS] = {
    170 	tcp_timer_rexmt,
    171 	tcp_timer_persist,
    172 	tcp_timer_keep,
    173 	tcp_timer_2msl,
    174 };
    175 
    176 /*
    177  * Timer state initialization, called from tcp_init().
    178  */
    179 void
    180 tcp_timer_init(void)
    181 {
    182 
    183 	if (tcp_keepinit == 0)
    184 		tcp_keepinit = TCPTV_KEEP_INIT;
    185 
    186 	if (tcp_keepidle == 0)
    187 		tcp_keepidle = TCPTV_KEEP_IDLE;
    188 
    189 	if (tcp_keepintvl == 0)
    190 		tcp_keepintvl = TCPTV_KEEPINTVL;
    191 
    192 	if (tcp_keepcnt == 0)
    193 		tcp_keepcnt = TCPTV_KEEPCNT;
    194 
    195 	if (tcp_maxpersistidle == 0)
    196 		tcp_maxpersistidle = TCPTV_KEEP_IDLE;
    197 
    198 	if (tcp_delack_ticks == 0)
    199 		tcp_delack_ticks = TCP_DELACK_TICKS;
    200 }
    201 
    202 /*
    203  * Return how many timers are currently being invoked.
    204  */
    205 int
    206 tcp_timers_invoking(struct tcpcb *tp)
    207 {
    208 	int i;
    209 	int count = 0;
    210 
    211 	for (i = 0; i < TCPT_NTIMERS; i++)
    212 		if (callout_invoking(&tp->t_timer[i]))
    213 			count++;
    214 	if (callout_invoking(&tp->t_delack_ch))
    215 		count++;
    216 
    217 	return count;
    218 }
    219 
    220 /*
    221  * Callout to process delayed ACKs for a TCPCB.
    222  */
    223 void
    224 tcp_delack(void *arg)
    225 {
    226 	struct tcpcb *tp = arg;
    227 	int s;
    228 
    229 	/*
    230 	 * If tcp_output() wasn't able to transmit the ACK
    231 	 * for whatever reason, it will restart the delayed
    232 	 * ACK callout.
    233 	 */
    234 
    235 	s = splsoftnet();
    236 	callout_ack(&tp->t_delack_ch);
    237 	if (tcp_isdead(tp)) {
    238 		splx(s);
    239 		return;
    240 	}
    241 
    242 	tp->t_flags |= TF_ACKNOW;
    243 	(void) tcp_output(tp);
    244 	splx(s);
    245 }
    246 
    247 /*
    248  * Tcp protocol timeout routine called every 500 ms.
    249  * Updates the timers in all active tcb's and
    250  * causes finite state machine actions if timers expire.
    251  */
    252 void
    253 tcp_slowtimo(void)
    254 {
    255 	int s;
    256 
    257 	s = splsoftnet();
    258 	tcp_iss_seq += TCP_ISSINCR;			/* increment iss */
    259 	tcp_now++;					/* for timestamps */
    260 	splx(s);
    261 }
    262 
    263 /*
    264  * Cancel all timers for TCP tp.
    265  */
    266 void
    267 tcp_canceltimers(struct tcpcb *tp)
    268 {
    269 	int i;
    270 
    271 	for (i = 0; i < TCPT_NTIMERS; i++)
    272 		TCP_TIMER_DISARM(tp, i);
    273 }
    274 
    275 const int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
    276     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
    277 
    278 const int	tcp_totbackoff = 511;	/* sum of tcp_backoff[] */
    279 
    280 /*
    281  * TCP timer processing.
    282  */
    283 
    284 void
    285 tcp_timer_rexmt(void *arg)
    286 {
    287 	struct tcpcb *tp = arg;
    288 	uint32_t rto;
    289 	int s;
    290 #ifdef TCP_DEBUG
    291 	struct socket *so = NULL;
    292 	short ostate;
    293 #endif
    294 
    295 	s = splsoftnet();
    296 	callout_ack(&tp->t_timer[TCPT_REXMT]);
    297 	if (tcp_isdead(tp)) {
    298 		splx(s);
    299 		return;
    300 	}
    301 
    302 	if ((tp->t_flags & TF_PMTUD_PEND) && tp->t_inpcb &&
    303 	    SEQ_GEQ(tp->t_pmtud_th_seq, tp->snd_una) &&
    304 	    SEQ_LT(tp->t_pmtud_th_seq, (int)(tp->snd_una + tp->t_ourmss))) {
    305 		extern struct sockaddr_in icmpsrc;
    306 		struct icmp icmp;
    307 
    308 		tp->t_flags &= ~TF_PMTUD_PEND;
    309 
    310 		/* XXX create fake icmp message with relevant entries */
    311 		icmp.icmp_nextmtu = tp->t_pmtud_nextmtu;
    312 		icmp.icmp_ip.ip_len = tp->t_pmtud_ip_len;
    313 		icmp.icmp_ip.ip_hl = tp->t_pmtud_ip_hl;
    314 		icmpsrc.sin_addr = tp->t_inpcb->inp_faddr;
    315 		icmp_mtudisc(&icmp, icmpsrc.sin_addr);
    316 
    317 		/*
    318 		 * Notify all connections to the same peer about
    319 		 * new mss and trigger retransmit.
    320 		 */
    321 		in_pcbnotifyall(&tcbtable, icmpsrc.sin_addr, EMSGSIZE,
    322 		    tcp_mtudisc);
    323  		splx(s);
    324  		return;
    325  	}
    326 #ifdef TCP_DEBUG
    327 #ifdef INET
    328 	if (tp->t_inpcb)
    329 		so = tp->t_inpcb->inp_socket;
    330 #endif
    331 #ifdef INET6
    332 	if (tp->t_in6pcb)
    333 		so = tp->t_in6pcb->in6p_socket;
    334 #endif
    335 	ostate = tp->t_state;
    336 #endif /* TCP_DEBUG */
    337 
    338 	/*
    339 	 * Clear the SACK scoreboard, reset FACK estimate.
    340 	 */
    341 	tcp_free_sackholes(tp);
    342 	tp->snd_fack = tp->snd_una;
    343 
    344 	/*
    345 	 * Retransmission timer went off.  Message has not
    346 	 * been acked within retransmit interval.  Back off
    347 	 * to a longer retransmit interval and retransmit one segment.
    348 	 */
    349 
    350 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
    351 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
    352 		tcpstat.tcps_timeoutdrop++;
    353 		tp = tcp_drop(tp, tp->t_softerror ?
    354 		    tp->t_softerror : ETIMEDOUT);
    355 		goto out;
    356 	}
    357 	tcpstat.tcps_rexmttimeo++;
    358 	rto = TCP_REXMTVAL(tp);
    359 	if (rto < tp->t_rttmin)
    360 		rto = tp->t_rttmin;
    361 	TCPT_RANGESET(tp->t_rxtcur, rto * tcp_backoff[tp->t_rxtshift],
    362 	    tp->t_rttmin, TCPTV_REXMTMAX);
    363 	TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
    364 
    365 	/*
    366 	 * If we are losing and we are trying path MTU discovery,
    367 	 * try turning it off.  This will avoid black holes in
    368 	 * the network which suppress or fail to send "packet
    369 	 * too big" ICMP messages.  We should ideally do
    370 	 * lots more sophisticated searching to find the right
    371 	 * value here...
    372 	 */
    373 	if (tp->t_mtudisc && tp->t_rxtshift > TCP_MAXRXTSHIFT / 6) {
    374 		tcpstat.tcps_pmtublackhole++;
    375 
    376 #ifdef INET
    377 		/* try turning PMTUD off */
    378 		if (tp->t_inpcb)
    379 			tp->t_mtudisc = 0;
    380 #endif
    381 #ifdef INET6
    382 		/* try using IPv6 minimum MTU */
    383 		if (tp->t_in6pcb)
    384 			tp->t_mtudisc = 0;
    385 #endif
    386 
    387 		/* XXX: more sophisticated Black hole recovery code? */
    388 	}
    389 
    390 	/*
    391 	 * If losing, let the lower level know and try for
    392 	 * a better route.  Also, if we backed off this far,
    393 	 * our srtt estimate is probably bogus.  Clobber it
    394 	 * so we'll take the next rtt measurement as our srtt;
    395 	 * move the current srtt into rttvar to keep the current
    396 	 * retransmit times until then.
    397 	 */
    398 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
    399 #ifdef INET
    400 		if (tp->t_inpcb)
    401 			in_losing(tp->t_inpcb);
    402 #endif
    403 #ifdef INET6
    404 		if (tp->t_in6pcb)
    405 			in6_losing(tp->t_in6pcb);
    406 #endif
    407 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
    408 		tp->t_srtt = 0;
    409 	}
    410 	tp->snd_nxt = tp->snd_una;
    411 	tp->snd_high = tp->snd_max;
    412 	/*
    413 	 * If timing a segment in this window, stop the timer.
    414 	 */
    415 	tp->t_rtttime = 0;
    416 	/*
    417 	 * Remember if we are retransmitting a SYN, because if
    418 	 * we do, set the initial congestion window must be set
    419 	 * to 1 segment.
    420 	 */
    421 	if (tp->t_state == TCPS_SYN_SENT)
    422 		tp->t_flags |= TF_SYN_REXMT;
    423 
    424 	/*
    425 	 * Adjust congestion control parameters.
    426 	 */
    427 	tp->t_congctl->slow_retransmit(tp);
    428 
    429 	(void) tcp_output(tp);
    430 
    431  out:
    432 #ifdef TCP_DEBUG
    433 	if (tp && so->so_options & SO_DEBUG)
    434 		tcp_trace(TA_USER, ostate, tp, NULL,
    435 		    PRU_SLOWTIMO | (TCPT_REXMT << 8));
    436 #endif
    437 	splx(s);
    438 }
    439 
    440 void
    441 tcp_timer_persist(void *arg)
    442 {
    443 	struct tcpcb *tp = arg;
    444 	uint32_t rto;
    445 	int s;
    446 #ifdef TCP_DEBUG
    447 	struct socket *so = NULL;
    448 	short ostate;
    449 #endif
    450 
    451 	s = splsoftnet();
    452 	callout_ack(&tp->t_timer[TCPT_PERSIST]);
    453 	if (tcp_isdead(tp)) {
    454 		splx(s);
    455 		return;
    456 	}
    457 
    458 #ifdef TCP_DEBUG
    459 #ifdef INET
    460 	if (tp->t_inpcb)
    461 		so = tp->t_inpcb->inp_socket;
    462 #endif
    463 #ifdef INET6
    464 	if (tp->t_in6pcb)
    465 		so = tp->t_in6pcb->in6p_socket;
    466 #endif
    467 
    468 	ostate = tp->t_state;
    469 #endif /* TCP_DEBUG */
    470 
    471 	/*
    472 	 * Persistance timer into zero window.
    473 	 * Force a byte to be output, if possible.
    474 	 */
    475 
    476 	/*
    477 	 * Hack: if the peer is dead/unreachable, we do not
    478 	 * time out if the window is closed.  After a full
    479 	 * backoff, drop the connection if the idle time
    480 	 * (no responses to probes) reaches the maximum
    481 	 * backoff that we would use if retransmitting.
    482 	 */
    483 	rto = TCP_REXMTVAL(tp);
    484 	if (rto < tp->t_rttmin)
    485 		rto = tp->t_rttmin;
    486 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
    487 	    ((tcp_now - tp->t_rcvtime) >= tcp_maxpersistidle ||
    488 	    (tcp_now - tp->t_rcvtime) >= rto * tcp_totbackoff)) {
    489 		tcpstat.tcps_persistdrops++;
    490 		tp = tcp_drop(tp, ETIMEDOUT);
    491 		goto out;
    492 	}
    493 	tcpstat.tcps_persisttimeo++;
    494 	tcp_setpersist(tp);
    495 	tp->t_force = 1;
    496 	(void) tcp_output(tp);
    497 	tp->t_force = 0;
    498 
    499  out:
    500 #ifdef TCP_DEBUG
    501 	if (tp && so->so_options & SO_DEBUG)
    502 		tcp_trace(TA_USER, ostate, tp, NULL,
    503 		    PRU_SLOWTIMO | (TCPT_PERSIST << 8));
    504 #endif
    505 	splx(s);
    506 }
    507 
    508 void
    509 tcp_timer_keep(void *arg)
    510 {
    511 	struct tcpcb *tp = arg;
    512 	struct socket *so = NULL;	/* Quell compiler warning */
    513 	int s;
    514 #ifdef TCP_DEBUG
    515 	short ostate;
    516 #endif
    517 
    518 	s = splsoftnet();
    519 	callout_ack(&tp->t_timer[TCPT_KEEP]);
    520 	if (tcp_isdead(tp)) {
    521 		splx(s);
    522 		return;
    523 	}
    524 
    525 #ifdef TCP_DEBUG
    526 	ostate = tp->t_state;
    527 #endif /* TCP_DEBUG */
    528 
    529 	/*
    530 	 * Keep-alive timer went off; send something
    531 	 * or drop connection if idle for too long.
    532 	 */
    533 
    534 	tcpstat.tcps_keeptimeo++;
    535 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    536 		goto dropit;
    537 #ifdef INET
    538 	if (tp->t_inpcb)
    539 		so = tp->t_inpcb->inp_socket;
    540 #endif
    541 #ifdef INET6
    542 	if (tp->t_in6pcb)
    543 		so = tp->t_in6pcb->in6p_socket;
    544 #endif
    545 	KASSERT(so != NULL);
    546 	if (so->so_options & SO_KEEPALIVE &&
    547 	    tp->t_state <= TCPS_CLOSE_WAIT) {
    548 	    	if ((tp->t_maxidle > 0) &&
    549 		    ((tcp_now - tp->t_rcvtime) >=
    550 		     tp->t_keepidle + tp->t_maxidle))
    551 			goto dropit;
    552 		/*
    553 		 * Send a packet designed to force a response
    554 		 * if the peer is up and reachable:
    555 		 * either an ACK if the connection is still alive,
    556 		 * or an RST if the peer has closed the connection
    557 		 * due to timeout or reboot.
    558 		 * Using sequence number tp->snd_una-1
    559 		 * causes the transmitted zero-length segment
    560 		 * to lie outside the receive window;
    561 		 * by the protocol spec, this requires the
    562 		 * correspondent TCP to respond.
    563 		 */
    564 		tcpstat.tcps_keepprobe++;
    565 		if (tcp_compat_42) {
    566 			/*
    567 			 * The keepalive packet must have nonzero
    568 			 * length to get a 4.2 host to respond.
    569 			 */
    570 			(void)tcp_respond(tp, tp->t_template,
    571 			    (struct mbuf *)NULL, NULL, tp->rcv_nxt - 1,
    572 			    tp->snd_una - 1, 0);
    573 		} else {
    574 			(void)tcp_respond(tp, tp->t_template,
    575 			    (struct mbuf *)NULL, NULL, tp->rcv_nxt,
    576 			    tp->snd_una - 1, 0);
    577 		}
    578 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepintvl);
    579 	} else
    580 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle);
    581 
    582 #ifdef TCP_DEBUG
    583 	if (tp && so->so_options & SO_DEBUG)
    584 		tcp_trace(TA_USER, ostate, tp, NULL,
    585 		    PRU_SLOWTIMO | (TCPT_KEEP << 8));
    586 #endif
    587 	splx(s);
    588 	return;
    589 
    590  dropit:
    591 	tcpstat.tcps_keepdrops++;
    592 	(void) tcp_drop(tp, ETIMEDOUT);
    593 	splx(s);
    594 }
    595 
    596 void
    597 tcp_timer_2msl(void *arg)
    598 {
    599 	struct tcpcb *tp = arg;
    600 	int s;
    601 #ifdef TCP_DEBUG
    602 	struct socket *so = NULL;
    603 	short ostate;
    604 #endif
    605 
    606 	s = splsoftnet();
    607 	callout_ack(&tp->t_timer[TCPT_2MSL]);
    608 	if (tcp_isdead(tp)) {
    609 		splx(s);
    610 		return;
    611 	}
    612 
    613 	/*
    614 	 * 2 MSL timeout went off, clear the SACK scoreboard, reset
    615 	 * the FACK estimate.
    616 	 */
    617 	tcp_free_sackholes(tp);
    618 	tp->snd_fack = tp->snd_una;
    619 
    620 #ifdef TCP_DEBUG
    621 #ifdef INET
    622 	if (tp->t_inpcb)
    623 		so = tp->t_inpcb->inp_socket;
    624 #endif
    625 #ifdef INET6
    626 	if (tp->t_in6pcb)
    627 		so = tp->t_in6pcb->in6p_socket;
    628 #endif
    629 
    630 	ostate = tp->t_state;
    631 #endif /* TCP_DEBUG */
    632 
    633 	/*
    634 	 * 2 MSL timeout in shutdown went off.  If we're closed but
    635 	 * still waiting for peer to close and connection has been idle
    636 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
    637 	 * control block.  Otherwise, check again in a bit.
    638 	 */
    639 	if (tp->t_state != TCPS_TIME_WAIT &&
    640 	    ((tp->t_maxidle == 0) ||
    641 	    ((tcp_now - tp->t_rcvtime) <= tp->t_maxidle)))
    642 	    TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_keepintvl);
    643 	else
    644 		tp = tcp_close(tp);
    645 
    646 #ifdef TCP_DEBUG
    647 	if (tp && so->so_options & SO_DEBUG)
    648 		tcp_trace(TA_USER, ostate, tp, NULL,
    649 		    PRU_SLOWTIMO | (TCPT_2MSL << 8));
    650 #endif
    651 	splx(s);
    652 }
    653