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