Home | History | Annotate | Line # | Download | only in netinet
tcp_timer.c revision 1.72
      1 /*	$NetBSD: tcp_timer.c,v 1.72 2005/07/19 17:00:02 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.72 2005/07/19 17:00:02 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/tcpip.h>
    142 #ifdef TCP_DEBUG
    143 #include <netinet/tcp_debug.h>
    144 #endif
    145 
    146 /*
    147  * Various tunable timer parameters.  These are initialized in tcp_init(),
    148  * unless they are patched.
    149  */
    150 int	tcp_keepidle = 0;
    151 int	tcp_keepintvl = 0;
    152 int	tcp_keepcnt = 0;		/* max idle probes */
    153 int	tcp_maxpersistidle = 0;		/* max idle time in persist */
    154 int	tcp_maxidle;			/* computed in tcp_slowtimo() */
    155 
    156 /*
    157  * Time to delay the ACK.  This is initialized in tcp_init(), unless
    158  * its patched.
    159  */
    160 int	tcp_delack_ticks = 0;
    161 
    162 void	tcp_timer_rexmt(void *);
    163 void	tcp_timer_persist(void *);
    164 void	tcp_timer_keep(void *);
    165 void	tcp_timer_2msl(void *);
    166 
    167 const tcp_timer_func_t tcp_timer_funcs[TCPT_NTIMERS] = {
    168 	tcp_timer_rexmt,
    169 	tcp_timer_persist,
    170 	tcp_timer_keep,
    171 	tcp_timer_2msl,
    172 };
    173 
    174 /*
    175  * Timer state initialization, called from tcp_init().
    176  */
    177 void
    178 tcp_timer_init(void)
    179 {
    180 
    181 	if (tcp_keepidle == 0)
    182 		tcp_keepidle = TCPTV_KEEP_IDLE;
    183 
    184 	if (tcp_keepintvl == 0)
    185 		tcp_keepintvl = TCPTV_KEEPINTVL;
    186 
    187 	if (tcp_keepcnt == 0)
    188 		tcp_keepcnt = TCPTV_KEEPCNT;
    189 
    190 	if (tcp_maxpersistidle == 0)
    191 		tcp_maxpersistidle = TCPTV_KEEP_IDLE;
    192 
    193 	if (tcp_delack_ticks == 0)
    194 		tcp_delack_ticks = TCP_DELACK_TICKS;
    195 }
    196 
    197 /*
    198  * Return how many timers are currently being invoked.
    199  */
    200 int
    201 tcp_timers_invoking(struct tcpcb *tp)
    202 {
    203 	int i;
    204 	int count = 0;
    205 
    206 	for (i = 0; i < TCPT_NTIMERS; i++)
    207 		if (callout_invoking(&tp->t_timer[i]))
    208 			count++;
    209 	if (callout_invoking(&tp->t_delack_ch))
    210 		count++;
    211 
    212 	return count;
    213 }
    214 
    215 /*
    216  * Callout to process delayed ACKs for a TCPCB.
    217  */
    218 void
    219 tcp_delack(void *arg)
    220 {
    221 	struct tcpcb *tp = arg;
    222 	int s;
    223 
    224 	/*
    225 	 * If tcp_output() wasn't able to transmit the ACK
    226 	 * for whatever reason, it will restart the delayed
    227 	 * ACK callout.
    228 	 */
    229 
    230 	s = splsoftnet();
    231 	callout_ack(&tp->t_delack_ch);
    232 	if (tcp_isdead(tp)) {
    233 		splx(s);
    234 		return;
    235 	}
    236 
    237 	tp->t_flags |= TF_ACKNOW;
    238 	(void) tcp_output(tp);
    239 	splx(s);
    240 }
    241 
    242 /*
    243  * Tcp protocol timeout routine called every 500 ms.
    244  * Updates the timers in all active tcb's and
    245  * causes finite state machine actions if timers expire.
    246  */
    247 void
    248 tcp_slowtimo(void)
    249 {
    250 	int s;
    251 
    252 	s = splsoftnet();
    253 	tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
    254 	tcp_iss_seq += TCP_ISSINCR;			/* increment iss */
    255 	tcp_now++;					/* for timestamps */
    256 	splx(s);
    257 }
    258 
    259 /*
    260  * Cancel all timers for TCP tp.
    261  */
    262 void
    263 tcp_canceltimers(struct tcpcb *tp)
    264 {
    265 	int i;
    266 
    267 	for (i = 0; i < TCPT_NTIMERS; i++)
    268 		TCP_TIMER_DISARM(tp, i);
    269 }
    270 
    271 const int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
    272     { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
    273 
    274 const int	tcp_totbackoff = 511;	/* sum of tcp_backoff[] */
    275 
    276 /*
    277  * TCP timer processing.
    278  */
    279 
    280 void
    281 tcp_timer_rexmt(void *arg)
    282 {
    283 	struct tcpcb *tp = arg;
    284 	uint32_t rto;
    285 	int s;
    286 #ifdef TCP_DEBUG
    287 	struct socket *so = NULL;
    288 	short ostate;
    289 #endif
    290 
    291 	s = splsoftnet();
    292 	callout_ack(&tp->t_timer[TCPT_REXMT]);
    293 	if (tcp_isdead(tp)) {
    294 		splx(s);
    295 		return;
    296 	}
    297 
    298 	if ((tp->t_flags & TF_PMTUD_PEND) && tp->t_inpcb &&
    299 	    SEQ_GEQ(tp->t_pmtud_th_seq, tp->snd_una) &&
    300 	    SEQ_LT(tp->t_pmtud_th_seq, (int)(tp->snd_una + tp->t_ourmss))) {
    301 		extern struct sockaddr_in icmpsrc;
    302 		struct icmp icmp;
    303 
    304 		tp->t_flags &= ~TF_PMTUD_PEND;
    305 
    306 		/* XXX create fake icmp message with relevant entries */
    307 		icmp.icmp_nextmtu = tp->t_pmtud_nextmtu;
    308 		icmp.icmp_ip.ip_len = tp->t_pmtud_ip_len;
    309 		icmp.icmp_ip.ip_hl = tp->t_pmtud_ip_hl;
    310 		icmpsrc.sin_addr = tp->t_inpcb->inp_faddr;
    311 		icmp_mtudisc(&icmp, icmpsrc.sin_addr);
    312 
    313 		/*
    314 		 * Notify all connections to the same peer about
    315 		 * new mss and trigger retransmit.
    316 		 */
    317 		in_pcbnotifyall(&tcbtable, icmpsrc.sin_addr, EMSGSIZE,
    318 		    tcp_mtudisc);
    319  		splx(s);
    320  		return;
    321  	}
    322 #ifdef TCP_DEBUG
    323 #ifdef INET
    324 	if (tp->t_inpcb)
    325 		so = tp->t_inpcb->inp_socket;
    326 #endif
    327 #ifdef INET6
    328 	if (tp->t_in6pcb)
    329 		so = tp->t_in6pcb->in6p_socket;
    330 #endif
    331 	ostate = tp->t_state;
    332 #endif /* TCP_DEBUG */
    333 
    334 	/*
    335 	 * Clear the SACK scoreboard, reset FACK estimate.
    336 	 */
    337 	tcp_free_sackholes(tp);
    338 	tp->snd_fack = tp->snd_una;
    339 
    340 	/*
    341 	 * Retransmission timer went off.  Message has not
    342 	 * been acked within retransmit interval.  Back off
    343 	 * to a longer retransmit interval and retransmit one segment.
    344 	 */
    345 
    346 	if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {
    347 		tp->t_rxtshift = TCP_MAXRXTSHIFT;
    348 		tcpstat.tcps_timeoutdrop++;
    349 		tp = tcp_drop(tp, tp->t_softerror ?
    350 		    tp->t_softerror : ETIMEDOUT);
    351 		goto out;
    352 	}
    353 	tcpstat.tcps_rexmttimeo++;
    354 	rto = TCP_REXMTVAL(tp);
    355 	if (rto < tp->t_rttmin)
    356 		rto = tp->t_rttmin;
    357 	TCPT_RANGESET(tp->t_rxtcur, rto * tcp_backoff[tp->t_rxtshift],
    358 	    tp->t_rttmin, TCPTV_REXMTMAX);
    359 	TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
    360 
    361 	/*
    362 	 * If we are losing and we are trying path MTU discovery,
    363 	 * try turning it off.  This will avoid black holes in
    364 	 * the network which suppress or fail to send "packet
    365 	 * too big" ICMP messages.  We should ideally do
    366 	 * lots more sophisticated searching to find the right
    367 	 * value here...
    368 	 */
    369 	if (tp->t_mtudisc && tp->t_rxtshift > TCP_MAXRXTSHIFT / 6) {
    370 		tcpstat.tcps_pmtublackhole++;
    371 
    372 #ifdef INET
    373 		/* try turning PMTUD off */
    374 		if (tp->t_inpcb)
    375 			tp->t_mtudisc = 0;
    376 #endif
    377 #ifdef INET6
    378 		/* try using IPv6 minimum MTU */
    379 		if (tp->t_in6pcb)
    380 			tp->t_mtudisc = 0;
    381 #endif
    382 
    383 		/* XXX: more sophisticated Black hole recovery code? */
    384 	}
    385 
    386 	/*
    387 	 * If losing, let the lower level know and try for
    388 	 * a better route.  Also, if we backed off this far,
    389 	 * our srtt estimate is probably bogus.  Clobber it
    390 	 * so we'll take the next rtt measurement as our srtt;
    391 	 * move the current srtt into rttvar to keep the current
    392 	 * retransmit times until then.
    393 	 */
    394 	if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {
    395 #ifdef INET
    396 		if (tp->t_inpcb)
    397 			in_losing(tp->t_inpcb);
    398 #endif
    399 #ifdef INET6
    400 		if (tp->t_in6pcb)
    401 			in6_losing(tp->t_in6pcb);
    402 #endif
    403 		tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);
    404 		tp->t_srtt = 0;
    405 	}
    406 	tp->snd_nxt = tp->snd_una;
    407 	tp->snd_high = tp->snd_max;
    408 	/*
    409 	 * If timing a segment in this window, stop the timer.
    410 	 */
    411 	tp->t_rtttime = 0;
    412 	/*
    413 	 * Remember if we are retransmitting a SYN, because if
    414 	 * we do, set the initial congestion window must be set
    415 	 * to 1 segment.
    416 	 */
    417 	if (tp->t_state == TCPS_SYN_SENT)
    418 		tp->t_flags |= TF_SYN_REXMT;
    419 	/*
    420 	 * Close the congestion window down to one segment
    421 	 * (we'll open it by one segment for each ack we get).
    422 	 * Since we probably have a window's worth of unacked
    423 	 * data accumulated, this "slow start" keeps us from
    424 	 * dumping all that data as back-to-back packets (which
    425 	 * might overwhelm an intermediate gateway).
    426 	 *
    427 	 * There are two phases to the opening: Initially we
    428 	 * open by one mss on each ack.  This makes the window
    429 	 * size increase exponentially with time.  If the
    430 	 * window is larger than the path can handle, this
    431 	 * exponential growth results in dropped packet(s)
    432 	 * almost immediately.  To get more time between
    433 	 * drops but still "push" the network to take advantage
    434 	 * of improving conditions, we switch from exponential
    435 	 * to linear window opening at some threshhold size.
    436 	 * For a threshhold, we use half the current window
    437 	 * size, truncated to a multiple of the mss.
    438 	 *
    439 	 * (the minimum cwnd that will give us exponential
    440 	 * growth is 2 mss.  We don't allow the threshhold
    441 	 * to go below this.)
    442 	 */
    443 	{
    444 	u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_segsz;
    445 	if (win < 2)
    446 		win = 2;
    447 	/* Loss Window MUST be one segment. */
    448 	tp->snd_cwnd = tp->t_segsz;
    449 	tp->snd_ssthresh = win * tp->t_segsz;
    450 	tp->t_partialacks = -1;
    451 	tp->t_dupacks = 0;
    452 	}
    453 	(void) tcp_output(tp);
    454 
    455  out:
    456 #ifdef TCP_DEBUG
    457 	if (tp && so->so_options & SO_DEBUG)
    458 		tcp_trace(TA_USER, ostate, tp, NULL,
    459 		    PRU_SLOWTIMO | (TCPT_REXMT << 8));
    460 #endif
    461 	splx(s);
    462 }
    463 
    464 void
    465 tcp_timer_persist(void *arg)
    466 {
    467 	struct tcpcb *tp = arg;
    468 	uint32_t rto;
    469 	int s;
    470 #ifdef TCP_DEBUG
    471 	struct socket *so = NULL;
    472 	short ostate;
    473 #endif
    474 
    475 	s = splsoftnet();
    476 	callout_ack(&tp->t_timer[TCPT_PERSIST]);
    477 	if (tcp_isdead(tp)) {
    478 		splx(s);
    479 		return;
    480 	}
    481 
    482 #ifdef TCP_DEBUG
    483 #ifdef INET
    484 	if (tp->t_inpcb)
    485 		so = tp->t_inpcb->inp_socket;
    486 #endif
    487 #ifdef INET6
    488 	if (tp->t_in6pcb)
    489 		so = tp->t_in6pcb->in6p_socket;
    490 #endif
    491 
    492 	ostate = tp->t_state;
    493 #endif /* TCP_DEBUG */
    494 
    495 	/*
    496 	 * Persistance timer into zero window.
    497 	 * Force a byte to be output, if possible.
    498 	 */
    499 
    500 	/*
    501 	 * Hack: if the peer is dead/unreachable, we do not
    502 	 * time out if the window is closed.  After a full
    503 	 * backoff, drop the connection if the idle time
    504 	 * (no responses to probes) reaches the maximum
    505 	 * backoff that we would use if retransmitting.
    506 	 */
    507 	rto = TCP_REXMTVAL(tp);
    508 	if (rto < tp->t_rttmin)
    509 		rto = tp->t_rttmin;
    510 	if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&
    511 	    ((tcp_now - tp->t_rcvtime) >= tcp_maxpersistidle ||
    512 	    (tcp_now - tp->t_rcvtime) >= rto * tcp_totbackoff)) {
    513 		tcpstat.tcps_persistdrops++;
    514 		tp = tcp_drop(tp, ETIMEDOUT);
    515 		goto out;
    516 	}
    517 	tcpstat.tcps_persisttimeo++;
    518 	tcp_setpersist(tp);
    519 	tp->t_force = 1;
    520 	(void) tcp_output(tp);
    521 	tp->t_force = 0;
    522 
    523  out:
    524 #ifdef TCP_DEBUG
    525 	if (tp && so->so_options & SO_DEBUG)
    526 		tcp_trace(TA_USER, ostate, tp, NULL,
    527 		    PRU_SLOWTIMO | (TCPT_PERSIST << 8));
    528 #endif
    529 	splx(s);
    530 }
    531 
    532 void
    533 tcp_timer_keep(void *arg)
    534 {
    535 	struct tcpcb *tp = arg;
    536 	struct socket *so = NULL;	/* Quell compiler warning */
    537 	int s;
    538 #ifdef TCP_DEBUG
    539 	short ostate;
    540 #endif
    541 
    542 	s = splsoftnet();
    543 	callout_ack(&tp->t_timer[TCPT_KEEP]);
    544 	if (tcp_isdead(tp)) {
    545 		splx(s);
    546 		return;
    547 	}
    548 
    549 #ifdef TCP_DEBUG
    550 	ostate = tp->t_state;
    551 #endif /* TCP_DEBUG */
    552 
    553 	/*
    554 	 * Keep-alive timer went off; send something
    555 	 * or drop connection if idle for too long.
    556 	 */
    557 
    558 	tcpstat.tcps_keeptimeo++;
    559 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    560 		goto dropit;
    561 #ifdef INET
    562 	if (tp->t_inpcb)
    563 		so = tp->t_inpcb->inp_socket;
    564 #endif
    565 #ifdef INET6
    566 	if (tp->t_in6pcb)
    567 		so = tp->t_in6pcb->in6p_socket;
    568 #endif
    569 	if (so->so_options & SO_KEEPALIVE &&
    570 	    tp->t_state <= TCPS_CLOSE_WAIT) {
    571 	    	if ((tcp_maxidle > 0) &&
    572 		    ((tcp_now - tp->t_rcvtime) >=
    573 		     tcp_keepidle + tcp_maxidle))
    574 			goto dropit;
    575 		/*
    576 		 * Send a packet designed to force a response
    577 		 * if the peer is up and reachable:
    578 		 * either an ACK if the connection is still alive,
    579 		 * or an RST if the peer has closed the connection
    580 		 * due to timeout or reboot.
    581 		 * Using sequence number tp->snd_una-1
    582 		 * causes the transmitted zero-length segment
    583 		 * to lie outside the receive window;
    584 		 * by the protocol spec, this requires the
    585 		 * correspondent TCP to respond.
    586 		 */
    587 		tcpstat.tcps_keepprobe++;
    588 		if (tcp_compat_42) {
    589 			/*
    590 			 * The keepalive packet must have nonzero
    591 			 * length to get a 4.2 host to respond.
    592 			 */
    593 			(void)tcp_respond(tp, tp->t_template,
    594 			    (struct mbuf *)NULL, NULL, tp->rcv_nxt - 1,
    595 			    tp->snd_una - 1, 0);
    596 		} else {
    597 			(void)tcp_respond(tp, tp->t_template,
    598 			    (struct mbuf *)NULL, NULL, tp->rcv_nxt,
    599 			    tp->snd_una - 1, 0);
    600 		}
    601 		TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepintvl);
    602 	} else
    603 		TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
    604 
    605 #ifdef TCP_DEBUG
    606 	if (tp && so->so_options & SO_DEBUG)
    607 		tcp_trace(TA_USER, ostate, tp, NULL,
    608 		    PRU_SLOWTIMO | (TCPT_KEEP << 8));
    609 #endif
    610 	splx(s);
    611 	return;
    612 
    613  dropit:
    614 	tcpstat.tcps_keepdrops++;
    615 	(void) tcp_drop(tp, ETIMEDOUT);
    616 	splx(s);
    617 }
    618 
    619 void
    620 tcp_timer_2msl(void *arg)
    621 {
    622 	struct tcpcb *tp = arg;
    623 	int s;
    624 #ifdef TCP_DEBUG
    625 	struct socket *so = NULL;
    626 	short ostate;
    627 #endif
    628 
    629 	s = splsoftnet();
    630 	callout_ack(&tp->t_timer[TCPT_2MSL]);
    631 	if (tcp_isdead(tp)) {
    632 		splx(s);
    633 		return;
    634 	}
    635 
    636 	/*
    637 	 * 2 MSL timeout went off, clear the SACK scoreboard, reset
    638 	 * the FACK estimate.
    639 	 */
    640 	tcp_free_sackholes(tp);
    641 	tp->snd_fack = tp->snd_una;
    642 
    643 #ifdef TCP_DEBUG
    644 #ifdef INET
    645 	if (tp->t_inpcb)
    646 		so = tp->t_inpcb->inp_socket;
    647 #endif
    648 #ifdef INET6
    649 	if (tp->t_in6pcb)
    650 		so = tp->t_in6pcb->in6p_socket;
    651 #endif
    652 
    653 	ostate = tp->t_state;
    654 #endif /* TCP_DEBUG */
    655 
    656 	/*
    657 	 * 2 MSL timeout in shutdown went off.  If we're closed but
    658 	 * still waiting for peer to close and connection has been idle
    659 	 * too long, or if 2MSL time is up from TIME_WAIT, delete connection
    660 	 * control block.  Otherwise, check again in a bit.
    661 	 */
    662 	if (tp->t_state != TCPS_TIME_WAIT &&
    663 	    ((tcp_maxidle == 0) || ((tcp_now - tp->t_rcvtime) <= tcp_maxidle)))
    664 		TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_keepintvl);
    665 	else
    666 		tp = tcp_close(tp);
    667 
    668 #ifdef TCP_DEBUG
    669 	if (tp && so->so_options & SO_DEBUG)
    670 		tcp_trace(TA_USER, ostate, tp, NULL,
    671 		    PRU_SLOWTIMO | (TCPT_2MSL << 8));
    672 #endif
    673 	splx(s);
    674 }
    675