Home | History | Annotate | Line # | Download | only in netinet
tcp_output.c revision 1.63.2.8
      1 /*	$NetBSD: tcp_output.c,v 1.63.2.8 2002/04/01 07:48:44 nathanw 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   1.1 (NRL) 17 January 1995
     34  *
     35  * NRL grants permission for redistribution and use in source and binary
     36  * forms, with or without modification, of the software and documentation
     37  * created at NRL provided that the following conditions are met:
     38  *
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgements:
     46  *      This product includes software developed by the University of
     47  *      California, Berkeley and its contributors.
     48  *      This product includes software developed at the Information
     49  *      Technology Division, US Naval Research Laboratory.
     50  * 4. Neither the name of the NRL nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
     55  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     57  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
     58  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     59  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     60  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     61  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     62  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     63  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     64  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     65  *
     66  * The views and conclusions contained in the software and documentation
     67  * are those of the authors and should not be interpreted as representing
     68  * official policies, either expressed or implied, of the US Naval
     69  * Research Laboratory (NRL).
     70  */
     71 
     72 /*-
     73  * Copyright (c) 1997, 1998, 2001 The NetBSD Foundation, Inc.
     74  * All rights reserved.
     75  *
     76  * This code is derived from software contributed to The NetBSD Foundation
     77  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     78  * Facility, NASA Ames Research Center.
     79  *
     80  * Redistribution and use in source and binary forms, with or without
     81  * modification, are permitted provided that the following conditions
     82  * are met:
     83  * 1. Redistributions of source code must retain the above copyright
     84  *    notice, this list of conditions and the following disclaimer.
     85  * 2. Redistributions in binary form must reproduce the above copyright
     86  *    notice, this list of conditions and the following disclaimer in the
     87  *    documentation and/or other materials provided with the distribution.
     88  * 3. All advertising materials mentioning features or use of this software
     89  *    must display the following acknowledgement:
     90  *	This product includes software developed by the NetBSD
     91  *	Foundation, Inc. and its contributors.
     92  * 4. Neither the name of The NetBSD Foundation nor the names of its
     93  *    contributors may be used to endorse or promote products derived
     94  *    from this software without specific prior written permission.
     95  *
     96  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     97  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     98  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     99  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
    100  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    101  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    102  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    103  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    104  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    105  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    106  * POSSIBILITY OF SUCH DAMAGE.
    107  */
    108 
    109 /*
    110  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
    111  *	The Regents of the University of California.  All rights reserved.
    112  *
    113  * Redistribution and use in source and binary forms, with or without
    114  * modification, are permitted provided that the following conditions
    115  * are met:
    116  * 1. Redistributions of source code must retain the above copyright
    117  *    notice, this list of conditions and the following disclaimer.
    118  * 2. Redistributions in binary form must reproduce the above copyright
    119  *    notice, this list of conditions and the following disclaimer in the
    120  *    documentation and/or other materials provided with the distribution.
    121  * 3. All advertising materials mentioning features or use of this software
    122  *    must display the following acknowledgement:
    123  *	This product includes software developed by the University of
    124  *	California, Berkeley and its contributors.
    125  * 4. Neither the name of the University nor the names of its contributors
    126  *    may be used to endorse or promote products derived from this software
    127  *    without specific prior written permission.
    128  *
    129  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    130  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    131  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    132  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    133  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    134  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    135  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    136  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    137  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    138  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    139  * SUCH DAMAGE.
    140  *
    141  *	@(#)tcp_output.c	8.4 (Berkeley) 5/24/95
    142  */
    143 
    144 #include <sys/cdefs.h>
    145 __KERNEL_RCSID(0, "$NetBSD: tcp_output.c,v 1.63.2.8 2002/04/01 07:48:44 nathanw Exp $");
    146 
    147 #include "opt_inet.h"
    148 #include "opt_ipsec.h"
    149 #include "opt_tcp_debug.h"
    150 
    151 #include <sys/param.h>
    152 #include <sys/systm.h>
    153 #include <sys/malloc.h>
    154 #include <sys/mbuf.h>
    155 #include <sys/protosw.h>
    156 #include <sys/socket.h>
    157 #include <sys/socketvar.h>
    158 #include <sys/errno.h>
    159 #include <sys/domain.h>
    160 #include <sys/kernel.h>
    161 
    162 #include <net/if.h>
    163 #include <net/route.h>
    164 
    165 #include <netinet/in.h>
    166 #include <netinet/in_systm.h>
    167 #include <netinet/ip.h>
    168 #include <netinet/in_pcb.h>
    169 #include <netinet/ip_var.h>
    170 
    171 #ifdef INET6
    172 #ifndef INET
    173 #include <netinet/in.h>
    174 #endif
    175 #include <netinet/ip6.h>
    176 #include <netinet6/in6_pcb.h>
    177 #include <netinet6/ip6_var.h>
    178 #endif
    179 
    180 #include <netinet/tcp.h>
    181 #define	TCPOUTFLAGS
    182 #include <netinet/tcp_fsm.h>
    183 #include <netinet/tcp_seq.h>
    184 #include <netinet/tcp_timer.h>
    185 #include <netinet/tcp_var.h>
    186 #include <netinet/tcpip.h>
    187 #include <netinet/tcp_debug.h>
    188 
    189 #ifdef notyet
    190 extern struct mbuf *m_copypack();
    191 #endif
    192 
    193 #define MAX_TCPOPTLEN	32	/* max # bytes that go in options */
    194 
    195 /*
    196  * Knob to enable Congestion Window Monitoring, and control the
    197  * the burst size it allows.  Default burst is 4 packets, per
    198  * the Internet draft.
    199  */
    200 int	tcp_cwm = 1;
    201 int	tcp_cwm_burstsize = 4;
    202 
    203 static
    204 #ifndef GPROF
    205 __inline
    206 #endif
    207 void
    208 tcp_segsize(struct tcpcb *tp, int *txsegsizep, int *rxsegsizep)
    209 {
    210 #ifdef INET
    211 	struct inpcb *inp = tp->t_inpcb;
    212 #endif
    213 #ifdef INET6
    214 	struct in6pcb *in6p = tp->t_in6pcb;
    215 #endif
    216 	struct rtentry *rt;
    217 	struct ifnet *ifp;
    218 	int size;
    219 	int iphlen;
    220 	int optlen;
    221 
    222 #ifdef DIAGNOSTIC
    223 	if (tp->t_inpcb && tp->t_in6pcb)
    224 		panic("tcp_segsize: both t_inpcb and t_in6pcb are set");
    225 #endif
    226 	switch (tp->t_family) {
    227 #ifdef INET
    228 	case AF_INET:
    229 		iphlen = sizeof(struct ip);
    230 		break;
    231 #endif
    232 #ifdef INET6
    233 	case AF_INET6:
    234 		iphlen = sizeof(struct ip6_hdr);
    235 		break;
    236 #endif
    237 	default:
    238 		size = tcp_mssdflt;
    239 		goto out;
    240 	}
    241 
    242 	rt = NULL;
    243 #ifdef INET
    244 	if (inp)
    245 		rt = in_pcbrtentry(inp);
    246 #endif
    247 #ifdef INET6
    248 	if (in6p)
    249 		rt = in6_pcbrtentry(in6p);
    250 #endif
    251 	if (rt == NULL) {
    252 		size = tcp_mssdflt;
    253 		goto out;
    254 	}
    255 
    256 	ifp = rt->rt_ifp;
    257 
    258 	size = tcp_mssdflt;
    259 	if (rt->rt_rmx.rmx_mtu != 0)
    260 		size = rt->rt_rmx.rmx_mtu - iphlen - sizeof(struct tcphdr);
    261 	else if (ifp->if_flags & IFF_LOOPBACK)
    262 		size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    263 #ifdef INET
    264 	else if (inp && ip_mtudisc)
    265 		size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    266 	else if (inp && in_localaddr(inp->inp_faddr))
    267 		size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    268 #endif
    269 #ifdef INET6
    270 	else if (in6p) {
    271 #ifdef INET
    272 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
    273 			/* mapped addr case */
    274 			struct in_addr d;
    275 			bcopy(&in6p->in6p_faddr.s6_addr32[3], &d, sizeof(d));
    276 			if (ip_mtudisc || in_localaddr(d))
    277 				size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    278 		} else
    279 #endif
    280 		{
    281 			/*
    282 			 * for IPv6, path MTU discovery is always turned on,
    283 			 * or the node must use packet size <= 1280.
    284 			 */
    285 			size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
    286 		}
    287 	}
    288 #endif
    289  out:
    290 	/*
    291 	 * Now we must make room for whatever extra TCP/IP options are in
    292 	 * the packet.
    293 	 */
    294 	optlen = tcp_optlen(tp);
    295 
    296 	/*
    297 	 * XXX tp->t_ourmss should have the right size, but without this code
    298 	 * fragmentation will occur... need more investigation
    299 	 */
    300 #ifdef INET
    301 	if (inp) {
    302 #ifdef IPSEC
    303 		optlen += ipsec4_hdrsiz_tcp(tp);
    304 #endif
    305 		optlen += ip_optlen(inp);
    306 	}
    307 #endif
    308 #ifdef INET6
    309 #ifdef INET
    310 	if (in6p && tp->t_family == AF_INET) {
    311 #ifdef IPSEC
    312 		optlen += ipsec4_hdrsiz_tcp(tp);
    313 #endif
    314 		/* XXX size -= ip_optlen(in6p); */
    315 	} else
    316 #endif
    317 	if (in6p && tp->t_family == AF_INET6) {
    318 #ifdef IPSEC
    319 		optlen += ipsec6_hdrsiz_tcp(tp);
    320 #endif
    321 		optlen += ip6_optlen(in6p);
    322 	}
    323 #endif
    324 	size -= optlen;
    325 
    326 	/*
    327 	 * *rxsegsizep holds *estimated* inbound segment size (estimation
    328 	 * assumes that path MTU is the same for both ways).  this is only
    329 	 * for silly window avoidance, do not use the value for other purposes.
    330 	 *
    331 	 * ipseclen is subtracted from both sides, this may not be right.
    332 	 * I'm not quite sure about this (could someone comment).
    333 	 */
    334 	*txsegsizep = min(tp->t_peermss - optlen, size);
    335 	*rxsegsizep = min(tp->t_ourmss - optlen, size);
    336 
    337 	if (*txsegsizep != tp->t_segsz) {
    338 		/*
    339 		 * If the new segment size is larger, we don't want to
    340 		 * mess up the congestion window, but if it is smaller
    341 		 * we'll have to reduce the congestion window to ensure
    342 		 * that we don't get into trouble with initial windows
    343 		 * and the rest.  In any case, if the segment size
    344 		 * has changed, chances are the path has, too, and
    345 		 * our congestion window will be different.
    346 		 */
    347 		if (*txsegsizep < tp->t_segsz) {
    348 			tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
    349 					   * *txsegsizep, *txsegsizep);
    350 			tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
    351 						* *txsegsizep, *txsegsizep);
    352 		}
    353 		tp->t_segsz = *txsegsizep;
    354 	}
    355 }
    356 
    357 static
    358 #ifndef GPROF
    359 __inline
    360 #endif
    361 int
    362 tcp_build_datapkt(struct tcpcb *tp, struct socket *so, int off,
    363     long len, int hdrlen, struct mbuf **mp)
    364 {
    365 	struct mbuf *m;
    366 
    367 	if (tp->t_force && len == 1)
    368 		tcpstat.tcps_sndprobe++;
    369 	else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
    370 		tcpstat.tcps_sndrexmitpack++;
    371 		tcpstat.tcps_sndrexmitbyte += len;
    372 	} else {
    373 		tcpstat.tcps_sndpack++;
    374 		tcpstat.tcps_sndbyte += len;
    375 	}
    376 #ifdef notyet
    377 	if ((m = m_copypack(so->so_snd.sb_mb, off,
    378 	    (int)len, max_linkhdr + hdrlen)) == 0)
    379 		return (ENOBUFS);
    380 	/*
    381 	 * m_copypack left space for our hdr; use it.
    382 	 */
    383 	m->m_len += hdrlen;
    384 	m->m_data -= hdrlen;
    385 #else
    386 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
    387 	if (m != NULL &&
    388 	    (max_linkhdr + hdrlen > MHLEN ||
    389 	     max_linkhdr + hdrlen + len <= MCLBYTES)) {
    390 		MCLGET(m, M_DONTWAIT);
    391 		if ((m->m_flags & M_EXT) == 0) {
    392 			m_freem(m);
    393 			m = NULL;
    394 		}
    395 	}
    396 	if (m == NULL)
    397 		return (ENOBUFS);
    398 	m->m_data += max_linkhdr;
    399 	m->m_len = hdrlen;
    400 	if (len <= M_TRAILINGSPACE(m)) {
    401 		m_copydata(so->so_snd.sb_mb, off, (int) len,
    402 		    mtod(m, caddr_t) + hdrlen);
    403 		m->m_len += len;
    404 	} else {
    405 		m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
    406 		if (m->m_next == NULL) {
    407 			m_freem(m);
    408 			return (ENOBUFS);
    409 		}
    410 	}
    411 #endif
    412 
    413 	*mp = m;
    414 	return (0);
    415 }
    416 
    417 /*
    418  * Tcp output routine: figure out what should be sent and send it.
    419  */
    420 int
    421 tcp_output(tp)
    422 	struct tcpcb *tp;
    423 {
    424 	struct socket *so;
    425 	struct route *ro;
    426 	long len, win;
    427 	int off, flags, error;
    428 	struct mbuf *m;
    429 	struct ip *ip;
    430 #ifdef INET6
    431 	struct ip6_hdr *ip6;
    432 #endif
    433 	struct tcphdr *th;
    434 	u_char opt[MAX_TCPOPTLEN];
    435 	unsigned optlen, hdrlen;
    436 	int idle, sendalot, txsegsize, rxsegsize;
    437 	int maxburst = TCP_MAXBURST;
    438 	int af;		/* address family on the wire */
    439 	int iphdrlen;
    440 
    441 #ifdef DIAGNOSTIC
    442 	if (tp->t_inpcb && tp->t_in6pcb)
    443 		panic("tcp_output: both t_inpcb and t_in6pcb are set");
    444 #endif
    445 	so = NULL;
    446 	ro = NULL;
    447 	if (tp->t_inpcb) {
    448 		so = tp->t_inpcb->inp_socket;
    449 		ro = &tp->t_inpcb->inp_route;
    450 	}
    451 #ifdef INET6
    452 	else if (tp->t_in6pcb) {
    453 		so = tp->t_in6pcb->in6p_socket;
    454 		ro = (struct route *)&tp->t_in6pcb->in6p_route;
    455 	}
    456 #endif
    457 
    458 	switch (af = tp->t_family) {
    459 #ifdef INET
    460 	case AF_INET:
    461 		if (tp->t_inpcb)
    462 			break;
    463 #ifdef INET6
    464 		/* mapped addr case */
    465 		if (tp->t_in6pcb)
    466 			break;
    467 #endif
    468 		return EINVAL;
    469 #endif
    470 #ifdef INET6
    471 	case AF_INET6:
    472 		if (tp->t_in6pcb)
    473 			break;
    474 		return EINVAL;
    475 #endif
    476 	default:
    477 		return EAFNOSUPPORT;
    478 	}
    479 
    480 	tcp_segsize(tp, &txsegsize, &rxsegsize);
    481 
    482 	idle = (tp->snd_max == tp->snd_una);
    483 
    484 	/*
    485 	 * Restart Window computation.  From draft-floyd-incr-init-win-03:
    486 	 *
    487 	 *	Optionally, a TCP MAY set the restart window to the
    488 	 *	minimum of the value used for the initial window and
    489 	 *	the current value of cwnd (in other words, using a
    490 	 *	larger value for the restart window should never increase
    491 	 *	the size of cwnd).
    492 	 */
    493 	if (tcp_cwm) {
    494 		/*
    495 		 * Hughes/Touch/Heidemann Congestion Window Monitoring.
    496 		 * Count the number of packets currently pending
    497 		 * acknowledgement, and limit our congestion window
    498 		 * to a pre-determined allowed burst size plus that count.
    499 		 * This prevents bursting once all pending packets have
    500 		 * been acknowledged (i.e. transmission is idle).
    501 		 *
    502 		 * XXX Link this to Initial Window?
    503 		 */
    504 		tp->snd_cwnd = min(tp->snd_cwnd,
    505 		    (tcp_cwm_burstsize * txsegsize) +
    506 		    (tp->snd_nxt - tp->snd_una));
    507 	} else {
    508 		if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) {
    509 			/*
    510 			 * We have been idle for "a while" and no acks are
    511 			 * expected to clock out any data we send --
    512 			 * slow start to get ack "clock" running again.
    513 			 */
    514 			tp->snd_cwnd = min(tp->snd_cwnd,
    515 			    TCP_INITIAL_WINDOW(tcp_init_win, txsegsize));
    516 		}
    517 	}
    518 
    519 again:
    520 	/*
    521 	 * Determine length of data that should be transmitted, and
    522 	 * flags that should be used.  If there is some data or critical
    523 	 * controls (SYN, RST) to send, then transmit; otherwise,
    524 	 * investigate further.
    525 	 */
    526 	sendalot = 0;
    527 	off = tp->snd_nxt - tp->snd_una;
    528 	win = min(tp->snd_wnd, tp->snd_cwnd);
    529 
    530 	flags = tcp_outflags[tp->t_state];
    531 	/*
    532 	 * If in persist timeout with window of 0, send 1 byte.
    533 	 * Otherwise, if window is small but nonzero
    534 	 * and timer expired, we will send what we can
    535 	 * and go to transmit state.
    536 	 */
    537 	if (tp->t_force) {
    538 		if (win == 0) {
    539 			/*
    540 			 * If we still have some data to send, then
    541 			 * clear the FIN bit.  Usually this would
    542 			 * happen below when it realizes that we
    543 			 * aren't sending all the data.  However,
    544 			 * if we have exactly 1 byte of unset data,
    545 			 * then it won't clear the FIN bit below,
    546 			 * and if we are in persist state, we wind
    547 			 * up sending the packet without recording
    548 			 * that we sent the FIN bit.
    549 			 *
    550 			 * We can't just blindly clear the FIN bit,
    551 			 * because if we don't have any more data
    552 			 * to send then the probe will be the FIN
    553 			 * itself.
    554 			 */
    555 			if (off < so->so_snd.sb_cc)
    556 				flags &= ~TH_FIN;
    557 			win = 1;
    558 		} else {
    559 			TCP_TIMER_DISARM(tp, TCPT_PERSIST);
    560 			tp->t_rxtshift = 0;
    561 		}
    562 	}
    563 
    564 	if (win < so->so_snd.sb_cc) {
    565 		len = win - off;
    566 		flags &= ~TH_FIN;
    567 	} else
    568 		len = so->so_snd.sb_cc - off;
    569 
    570 	if (len < 0) {
    571 		/*
    572 		 * If FIN has been sent but not acked,
    573 		 * but we haven't been called to retransmit,
    574 		 * len will be -1.  Otherwise, window shrank
    575 		 * after we sent into it.  If window shrank to 0,
    576 		 * cancel pending retransmit, pull snd_nxt back
    577 		 * to (closed) window, and set the persist timer
    578 		 * if it isn't already going.  If the window didn't
    579 		 * close completely, just wait for an ACK.
    580 		 *
    581 		 * If we have a pending FIN, either it has already been
    582 		 * transmitted or it is outside the window, so drop it.
    583 		 * If the FIN has been transmitted, but this is not a
    584 		 * retransmission, then len must be -1.  Therefore we also
    585 		 * prevent here the sending of `gratuitous FINs'.  This
    586 		 * eliminates the need to check for that case below (e.g.
    587 		 * to back up snd_nxt before the FIN so that the sequence
    588 		 * number is correct).
    589 		 */
    590 		len = 0;
    591 		flags &= ~TH_FIN;
    592 		if (win == 0) {
    593 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
    594 			tp->t_rxtshift = 0;
    595 			tp->snd_nxt = tp->snd_una;
    596 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
    597 				tcp_setpersist(tp);
    598 		}
    599 	}
    600 	if (len > txsegsize) {
    601 		len = txsegsize;
    602 		flags &= ~TH_FIN;
    603 		sendalot = 1;
    604 	}
    605 
    606 	win = sbspace(&so->so_rcv);
    607 
    608 	/*
    609 	 * Sender silly window avoidance.  If connection is idle
    610 	 * and can send all data, a maximum segment,
    611 	 * at least a maximum default-size segment do it,
    612 	 * or are forced, do it; otherwise don't bother.
    613 	 * If peer's buffer is tiny, then send
    614 	 * when window is at least half open.
    615 	 * If retransmitting (possibly after persist timer forced us
    616 	 * to send into a small window), then must resend.
    617 	 */
    618 	if (len) {
    619 		if (len == txsegsize)
    620 			goto send;
    621 		if ((so->so_state & SS_MORETOCOME) == 0 &&
    622 		    ((idle || tp->t_flags & TF_NODELAY) &&
    623 		     len + off >= so->so_snd.sb_cc))
    624 			goto send;
    625 		if (tp->t_force)
    626 			goto send;
    627 		if (len >= tp->max_sndwnd / 2)
    628 			goto send;
    629 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
    630 			goto send;
    631 	}
    632 
    633 	/*
    634 	 * Compare available window to amount of window known to peer
    635 	 * (as advertised window less next expected input).  If the
    636 	 * difference is at least twice the size of the largest segment
    637 	 * we expect to receive (i.e. two segments) or at least 50% of
    638 	 * the maximum possible window, then want to send a window update
    639 	 * to peer.
    640 	 */
    641 	if (win > 0) {
    642 		/*
    643 		 * "adv" is the amount we can increase the window,
    644 		 * taking into account that we are limited by
    645 		 * TCP_MAXWIN << tp->rcv_scale.
    646 		 */
    647 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
    648 			(tp->rcv_adv - tp->rcv_nxt);
    649 
    650 		if (adv >= (long) (2 * rxsegsize))
    651 			goto send;
    652 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
    653 			goto send;
    654 	}
    655 
    656 	/*
    657 	 * Send if we owe peer an ACK.
    658 	 */
    659 	if (tp->t_flags & TF_ACKNOW)
    660 		goto send;
    661 	if (flags & (TH_SYN|TH_FIN|TH_RST))
    662 		goto send;
    663 	if (SEQ_GT(tp->snd_up, tp->snd_una))
    664 		goto send;
    665 
    666 	/*
    667 	 * TCP window updates are not reliable, rather a polling protocol
    668 	 * using ``persist'' packets is used to insure receipt of window
    669 	 * updates.  The three ``states'' for the output side are:
    670 	 *	idle			not doing retransmits or persists
    671 	 *	persisting		to move a small or zero window
    672 	 *	(re)transmitting	and thereby not persisting
    673 	 *
    674 	 * tp->t_timer[TCPT_PERSIST]
    675 	 *	is set when we are in persist state.
    676 	 * tp->t_force
    677 	 *	is set when we are called to send a persist packet.
    678 	 * tp->t_timer[TCPT_REXMT]
    679 	 *	is set when we are retransmitting
    680 	 * The output side is idle when both timers are zero.
    681 	 *
    682 	 * If send window is too small, there is data to transmit, and no
    683 	 * retransmit or persist is pending, then go to persist state.
    684 	 * If nothing happens soon, send when timer expires:
    685 	 * if window is nonzero, transmit what we can,
    686 	 * otherwise force out a byte.
    687 	 */
    688 	if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
    689 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
    690 		tp->t_rxtshift = 0;
    691 		tcp_setpersist(tp);
    692 	}
    693 
    694 	/*
    695 	 * No reason to send a segment, just return.
    696 	 */
    697 	return (0);
    698 
    699 send:
    700 	/*
    701 	 * Before ESTABLISHED, force sending of initial options
    702 	 * unless TCP set not to do any options.
    703 	 * NOTE: we assume that the IP/TCP header plus TCP options
    704 	 * always fit in a single mbuf, leaving room for a maximum
    705 	 * link header, i.e.
    706 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
    707 	 */
    708 	optlen = 0;
    709 	switch (af) {
    710 #ifdef INET
    711 	case AF_INET:
    712 		iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
    713 		break;
    714 #endif
    715 #ifdef INET6
    716 	case AF_INET6:
    717 		iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
    718 		break;
    719 #endif
    720 	default:	/*pacify gcc*/
    721 		iphdrlen = 0;
    722 		break;
    723 	}
    724 	hdrlen = iphdrlen;
    725 	if (flags & TH_SYN) {
    726 		struct rtentry *rt;
    727 
    728 		rt = NULL;
    729 #ifdef INET
    730 		if (tp->t_inpcb)
    731 			rt = in_pcbrtentry(tp->t_inpcb);
    732 #endif
    733 #ifdef INET6
    734 		if (tp->t_in6pcb)
    735 			rt = in6_pcbrtentry(tp->t_in6pcb);
    736 #endif
    737 
    738 		tp->snd_nxt = tp->iss;
    739 		tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
    740 						    rt->rt_ifp : NULL, af);
    741 		if ((tp->t_flags & TF_NOOPT) == 0) {
    742 			opt[0] = TCPOPT_MAXSEG;
    743 			opt[1] = 4;
    744 			opt[2] = (tp->t_ourmss >> 8) & 0xff;
    745 			opt[3] = tp->t_ourmss & 0xff;
    746 			optlen = 4;
    747 
    748 			if ((tp->t_flags & TF_REQ_SCALE) &&
    749 			    ((flags & TH_ACK) == 0 ||
    750 			    (tp->t_flags & TF_RCVD_SCALE))) {
    751 				*((u_int32_t *) (opt + optlen)) = htonl(
    752 					TCPOPT_NOP << 24 |
    753 					TCPOPT_WINDOW << 16 |
    754 					TCPOLEN_WINDOW << 8 |
    755 					tp->request_r_scale);
    756 				optlen += 4;
    757 			}
    758 		}
    759  	}
    760 
    761  	/*
    762 	 * Send a timestamp and echo-reply if this is a SYN and our side
    763 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
    764 	 * and our peer have sent timestamps in our SYN's.
    765  	 */
    766  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
    767  	     (flags & TH_RST) == 0 &&
    768  	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
    769 	     (tp->t_flags & TF_RCVD_TSTMP))) {
    770 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
    771 
    772  		/* Form timestamp option as shown in appendix A of RFC 1323. */
    773  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
    774  		*lp++ = htonl(TCP_TIMESTAMP(tp));
    775  		*lp   = htonl(tp->ts_recent);
    776  		optlen += TCPOLEN_TSTAMP_APPA;
    777  	}
    778 
    779  	hdrlen += optlen;
    780 
    781 #ifdef DIAGNOSTIC
    782 	if (len > txsegsize)
    783 		panic("tcp data to be sent is larger than segment");
    784  	if (max_linkhdr + hdrlen > MCLBYTES)
    785 		panic("tcphdr too big");
    786 #endif
    787 
    788 	/*
    789 	 * Grab a header mbuf, attaching a copy of data to
    790 	 * be transmitted, and initialize the header from
    791 	 * the template for sends on this connection.
    792 	 */
    793 	if (len) {
    794 		error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
    795 		if (error)
    796 			goto out;
    797 		/*
    798 		 * If we're sending everything we've got, set PUSH.
    799 		 * (This will keep happy those implementations which only
    800 		 * give data to the user when a buffer fills or
    801 		 * a PUSH comes in.)
    802 		 */
    803 		if (off + len == so->so_snd.sb_cc)
    804 			flags |= TH_PUSH;
    805 	} else {
    806 		if (tp->t_flags & TF_ACKNOW)
    807 			tcpstat.tcps_sndacks++;
    808 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
    809 			tcpstat.tcps_sndctrl++;
    810 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
    811 			tcpstat.tcps_sndurg++;
    812 		else
    813 			tcpstat.tcps_sndwinup++;
    814 
    815 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    816 		if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
    817 			MCLGET(m, M_DONTWAIT);
    818 			if ((m->m_flags & M_EXT) == 0) {
    819 				m_freem(m);
    820 				m = NULL;
    821 			}
    822 		}
    823 		if (m == NULL) {
    824 			error = ENOBUFS;
    825 			goto out;
    826 		}
    827 		m->m_data += max_linkhdr;
    828 		m->m_len = hdrlen;
    829 	}
    830 	m->m_pkthdr.rcvif = (struct ifnet *)0;
    831 	switch (af) {
    832 #ifdef INET
    833 	case AF_INET:
    834 		ip = mtod(m, struct ip *);
    835 #ifdef INET6
    836 		ip6 = NULL;
    837 #endif
    838 		th = (struct tcphdr *)(ip + 1);
    839 		break;
    840 #endif
    841 #ifdef INET6
    842 	case AF_INET6:
    843 		ip = NULL;
    844 		ip6 = mtod(m, struct ip6_hdr *);
    845 		th = (struct tcphdr *)(ip6 + 1);
    846 		break;
    847 #endif
    848 	default:	/*pacify gcc*/
    849 		ip = NULL;
    850 #ifdef INET6
    851 		ip6 = NULL;
    852 #endif
    853 		th = NULL;
    854 		break;
    855 	}
    856 	if (tp->t_template == 0)
    857 		panic("tcp_output");
    858 	if (tp->t_template->m_len < iphdrlen)
    859 		panic("tcp_output");
    860 	bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), iphdrlen);
    861 
    862 	/*
    863 	 * If we are doing retransmissions, then snd_nxt will
    864 	 * not reflect the first unsent octet.  For ACK only
    865 	 * packets, we do not want the sequence number of the
    866 	 * retransmitted packet, we want the sequence number
    867 	 * of the next unsent octet.  So, if there is no data
    868 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
    869 	 * when filling in ti_seq.  But if we are in persist
    870 	 * state, snd_max might reflect one byte beyond the
    871 	 * right edge of the window, so use snd_nxt in that
    872 	 * case, since we know we aren't doing a retransmission.
    873 	 * (retransmit and persist are mutually exclusive...)
    874 	 */
    875 	if (len || (flags & (TH_SYN|TH_FIN)) ||
    876 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
    877 		th->th_seq = htonl(tp->snd_nxt);
    878 	else
    879 		th->th_seq = htonl(tp->snd_max);
    880 	th->th_ack = htonl(tp->rcv_nxt);
    881 	if (optlen) {
    882 		bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen);
    883 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
    884 	}
    885 	th->th_flags = flags;
    886 	/*
    887 	 * Calculate receive window.  Don't shrink window,
    888 	 * but avoid silly window syndrome.
    889 	 */
    890 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
    891 		win = 0;
    892 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
    893 		win = (long)TCP_MAXWIN << tp->rcv_scale;
    894 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
    895 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
    896 	th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
    897 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
    898 		u_int32_t urp = tp->snd_up - tp->snd_nxt;
    899 		if (urp > IP_MAXPACKET)
    900 			urp = IP_MAXPACKET;
    901 		th->th_urp = htons((u_int16_t)urp);
    902 		th->th_flags |= TH_URG;
    903 	} else
    904 		/*
    905 		 * If no urgent pointer to send, then we pull
    906 		 * the urgent pointer to the left edge of the send window
    907 		 * so that it doesn't drift into the send window on sequence
    908 		 * number wraparound.
    909 		 */
    910 		tp->snd_up = tp->snd_una;		/* drag it along */
    911 
    912 	/*
    913 	 * Set ourselves up to be checksummed just before the packet
    914 	 * hits the wire.
    915 	 */
    916 	switch (af) {
    917 #ifdef INET
    918 	case AF_INET:
    919 		m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
    920 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
    921 		if (len + optlen) {
    922 			/* Fixup the pseudo-header checksum. */
    923 			/* XXXJRT Not IP Jumbogram safe. */
    924 			th->th_sum = in_cksum_addword(th->th_sum,
    925 			    htons((u_int16_t) (len + optlen)));
    926 		}
    927 		break;
    928 #endif
    929 #ifdef INET6
    930 	case AF_INET6:
    931 		/*
    932 		 * XXX Actually delaying the checksum is Hard
    933 		 * XXX (well, maybe not for Itojun, but it is
    934 		 * XXX for me), but we can still take advantage
    935 		 * XXX of the cached pseudo-header checksum.
    936 		 */
    937 		/* equals to hdrlen + len */
    938 		m->m_pkthdr.len = sizeof(struct ip6_hdr)
    939 			+ sizeof(struct tcphdr) + optlen + len;
    940 #ifdef notyet
    941 		m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
    942 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
    943 #endif
    944 		if (len + optlen) {
    945 			/* Fixup the pseudo-header checksum. */
    946 			/* XXXJRT: Not IPv6 Jumbogram safe. */
    947 			th->th_sum = in_cksum_addword(th->th_sum,
    948 			    htons((u_int16_t) (len + optlen)));
    949 		}
    950 #ifndef notyet
    951 		th->th_sum = in6_cksum(m, 0, sizeof(struct ip6_hdr),
    952 		    sizeof(struct tcphdr) + optlen + len);
    953 #endif
    954 		break;
    955 #endif
    956 	}
    957 
    958 	/*
    959 	 * In transmit state, time the transmission and arrange for
    960 	 * the retransmit.  In persist state, just set snd_max.
    961 	 */
    962 	if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
    963 		tcp_seq startseq = tp->snd_nxt;
    964 
    965 		/*
    966 		 * Advance snd_nxt over sequence space of this segment.
    967 		 * There are no states in which we send both a SYN and a FIN,
    968 		 * so we collapse the tests for these flags.
    969 		 */
    970 		if (flags & (TH_SYN|TH_FIN))
    971 			tp->snd_nxt++;
    972 		tp->snd_nxt += len;
    973 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
    974 			tp->snd_max = tp->snd_nxt;
    975 			/*
    976 			 * Time this transmission if not a retransmission and
    977 			 * not currently timing anything.
    978 			 */
    979 			if (tp->t_rtttime == 0) {
    980 				tp->t_rtttime = tcp_now;
    981 				tp->t_rtseq = startseq;
    982 				tcpstat.tcps_segstimed++;
    983 			}
    984 		}
    985 
    986 		/*
    987 		 * Set retransmit timer if not currently set,
    988 		 * and not doing an ack or a keep-alive probe.
    989 		 * Initial value for retransmit timer is smoothed
    990 		 * round-trip time + 2 * round-trip time variance.
    991 		 * Initialize shift counter which is used for backoff
    992 		 * of retransmit time.
    993 		 */
    994 		if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
    995 		    tp->snd_nxt != tp->snd_una) {
    996 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
    997 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
    998 				TCP_TIMER_DISARM(tp, TCPT_PERSIST);
    999 				tp->t_rxtshift = 0;
   1000 			}
   1001 		}
   1002 	} else
   1003 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
   1004 			tp->snd_max = tp->snd_nxt + len;
   1005 
   1006 #ifdef TCP_DEBUG
   1007 	/*
   1008 	 * Trace.
   1009 	 */
   1010 	if (so->so_options & SO_DEBUG) {
   1011 		/*
   1012 		 * need to recover version # field, which was overwritten
   1013 		 * on ip_cksum computation.
   1014 		 */
   1015 		struct ip *sip;
   1016 		sip = mtod(m, struct ip *);
   1017 		switch (af) {
   1018 #ifdef INET
   1019 		case AF_INET:
   1020 			sip->ip_v = 4;
   1021 			break;
   1022 #endif
   1023 #ifdef INET6
   1024 		case AF_INET6:
   1025 			sip->ip_v = 6;
   1026 			break;
   1027 #endif
   1028 		}
   1029 		tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
   1030 	}
   1031 #endif
   1032 
   1033 	/*
   1034 	 * Fill in IP length and desired time to live and
   1035 	 * send to IP level.  There should be a better way
   1036 	 * to handle ttl and tos; we could keep them in
   1037 	 * the template, but need a way to checksum without them.
   1038 	 */
   1039 	m->m_pkthdr.len = hdrlen + len;
   1040 
   1041 	switch (af) {
   1042 #ifdef INET
   1043 	case AF_INET:
   1044 		ip->ip_len = m->m_pkthdr.len;
   1045 		if (tp->t_inpcb) {
   1046 			ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
   1047 			ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
   1048 		}
   1049 #ifdef INET6
   1050 		else if (tp->t_in6pcb) {
   1051 			ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
   1052 			ip->ip_tos = 0;	/*XXX*/
   1053 		}
   1054 #endif
   1055 		break;
   1056 #endif
   1057 #ifdef INET6
   1058 	case AF_INET6:
   1059 		ip6->ip6_nxt = IPPROTO_TCP;
   1060 		if (tp->t_in6pcb) {
   1061 			/*
   1062 			 * we separately set hoplimit for every segment, since
   1063 			 * the user might want to change the value via
   1064 			 * setsockopt. Also, desired default hop limit might
   1065 			 * be changed via Neighbor Discovery.
   1066 			 */
   1067 			ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
   1068 				ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
   1069 		}
   1070 		/* ip6->ip6_flow = ??? */
   1071 		/* ip6_plen will be filled in ip6_output(). */
   1072 		break;
   1073 #endif
   1074 	}
   1075 
   1076 #ifdef IPSEC
   1077 	if (ipsec_setsocket(m, so) != 0) {
   1078 		m_freem(m);
   1079 		error = ENOBUFS;
   1080 		goto out;
   1081 	}
   1082 #endif /*IPSEC*/
   1083 
   1084 	switch (af) {
   1085 #ifdef INET
   1086 	case AF_INET:
   1087 	    {
   1088 		struct mbuf *opts;
   1089 
   1090 		if (tp->t_inpcb)
   1091 			opts = tp->t_inpcb->inp_options;
   1092 		else
   1093 			opts = NULL;
   1094 		error = ip_output(m, opts, ro,
   1095 			(ip_mtudisc ? IP_MTUDISC : 0) |
   1096 			(so->so_options & SO_DONTROUTE),
   1097 			0);
   1098 		break;
   1099 	    }
   1100 #endif
   1101 #ifdef INET6
   1102 	case AF_INET6:
   1103 	    {
   1104 		struct ip6_pktopts *opts;
   1105 
   1106 		if (tp->t_in6pcb)
   1107 			opts = tp->t_in6pcb->in6p_outputopts;
   1108 		else
   1109 			opts = NULL;
   1110 		error = ip6_output(m, opts, (struct route_in6 *)ro,
   1111 			so->so_options & SO_DONTROUTE, 0, NULL);
   1112 		break;
   1113 	    }
   1114 #endif
   1115 	default:
   1116 		error = EAFNOSUPPORT;
   1117 		break;
   1118 	}
   1119 	if (error) {
   1120 out:
   1121 		if (error == ENOBUFS) {
   1122 			tcpstat.tcps_selfquench++;
   1123 #ifdef INET
   1124 			if (tp->t_inpcb)
   1125 				tcp_quench(tp->t_inpcb, 0);
   1126 #endif
   1127 #ifdef INET6
   1128 			if (tp->t_in6pcb)
   1129 				tcp6_quench(tp->t_in6pcb, 0);
   1130 #endif
   1131 			error = 0;
   1132 		} else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
   1133 		    TCPS_HAVERCVDSYN(tp->t_state)) {
   1134 			tp->t_softerror = error;
   1135 			error = 0;
   1136 		}
   1137 
   1138 		/* Restart the delayed ACK timer, if necessary. */
   1139 		if (tp->t_flags & TF_DELACK)
   1140 			TCP_RESTART_DELACK(tp);
   1141 
   1142 		return (error);
   1143 	}
   1144 	tcpstat.tcps_sndtotal++;
   1145 	if (tp->t_flags & TF_DELACK)
   1146 		tcpstat.tcps_delack++;
   1147 
   1148 	/*
   1149 	 * Data sent (as far as we can tell).
   1150 	 * If this advertises a larger window than any other segment,
   1151 	 * then remember the size of the advertised window.
   1152 	 * Any pending ACK has now been sent.
   1153 	 */
   1154 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
   1155 		tp->rcv_adv = tp->rcv_nxt + win;
   1156 	tp->last_ack_sent = tp->rcv_nxt;
   1157 	tp->t_flags &= ~TF_ACKNOW;
   1158 	TCP_CLEAR_DELACK(tp);
   1159 #ifdef DIAGNOSTIC
   1160 	if (maxburst < 0)
   1161 		printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
   1162 #endif
   1163 	if (sendalot && (!tcp_do_newreno || --maxburst))
   1164 		goto again;
   1165 	return (0);
   1166 }
   1167 
   1168 void
   1169 tcp_setpersist(tp)
   1170 	struct tcpcb *tp;
   1171 {
   1172 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
   1173 	int nticks;
   1174 
   1175 	if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
   1176 		panic("tcp_output REXMT");
   1177 	/*
   1178 	 * Start/restart persistance timer.
   1179 	 */
   1180 	if (t < tp->t_rttmin)
   1181 		t = tp->t_rttmin;
   1182 	TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
   1183 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
   1184 	TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
   1185 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
   1186 		tp->t_rxtshift++;
   1187 }
   1188