Home | History | Annotate | Line # | Download | only in netinet
tcp_output.c revision 1.105
      1 /*	$NetBSD: tcp_output.c,v 1.105 2003/10/24 10:25:40 ragge 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.105 2003/10/24 10:25:40 ragge 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_lastm == NULL ||
    455 		(tp->t_lastoff + tp->t_lastlen) != off
    456 #else
    457 	    1
    458 #endif
    459 	   )
    460 	{
    461 #ifdef FAST_MBSEARCH
    462 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_miss);
    463 #endif
    464 		/*
    465 		 * Either a new packet or a retransmit.
    466 		 * Start from the beginning.
    467 		 */
    468 		tp->t_lastm = so->so_snd.sb_mb;
    469 		tp->t_inoff = off;
    470 	} else {
    471 #ifdef FAST_MBSEARCH
    472 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_hit);
    473 #endif
    474 		tp->t_inoff += tp->t_lastlen;
    475 	}
    476 
    477 	/* Traverse forward to next packet */
    478 	while (tp->t_inoff > 0) {
    479 		if (tp->t_lastm == NULL)
    480 			panic("tp->t_lastm == NULL");
    481 		if (tp->t_inoff < tp->t_lastm->m_len)
    482 			break;
    483 		tp->t_inoff -= tp->t_lastm->m_len;
    484 		tp->t_lastm = tp->t_lastm->m_next;
    485 	}
    486 
    487 	tp->t_lastoff = off;
    488 	tp->t_lastlen = len;
    489 	m0 = tp->t_lastm;
    490 	off = tp->t_inoff;
    491 
    492 	if (len <= M_TRAILINGSPACE(m)) {
    493 		m_copydata(m0, off, (int) len, mtod(m, caddr_t) + hdrlen);
    494 		m->m_len += len;
    495 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_copysmall);
    496 	} else {
    497 		m->m_next = m_copy(m0, off, (int) len);
    498 		if (m->m_next == NULL) {
    499 			m_freem(m);
    500 			return (ENOBUFS);
    501 		}
    502 #ifdef TCP_OUTPUT_COUNTERS
    503 		if (m->m_next->m_flags & M_EXT)
    504 			TCP_OUTPUT_COUNTER_INCR(&tcp_output_refbig);
    505 		else
    506 			TCP_OUTPUT_COUNTER_INCR(&tcp_output_copybig);
    507 #endif /* TCP_OUTPUT_COUNTERS */
    508 	}
    509 #endif
    510 
    511 	*mp = m;
    512 	return (0);
    513 }
    514 
    515 /*
    516  * Tcp output routine: figure out what should be sent and send it.
    517  */
    518 int
    519 tcp_output(tp)
    520 	struct tcpcb *tp;
    521 {
    522 	struct socket *so;
    523 	struct route *ro;
    524 	long len, win;
    525 	int off, flags, error;
    526 	struct mbuf *m;
    527 	struct ip *ip;
    528 #ifdef INET6
    529 	struct ip6_hdr *ip6;
    530 #endif
    531 	struct tcphdr *th;
    532 	u_char opt[MAX_TCPOPTLEN];
    533 	unsigned optlen, hdrlen;
    534 	int idle, sendalot, txsegsize, rxsegsize;
    535 	int maxburst = TCP_MAXBURST;
    536 	int af;		/* address family on the wire */
    537 	int iphdrlen;
    538 
    539 #ifdef DIAGNOSTIC
    540 	if (tp->t_inpcb && tp->t_in6pcb)
    541 		panic("tcp_output: both t_inpcb and t_in6pcb are set");
    542 #endif
    543 	so = NULL;
    544 	ro = NULL;
    545 	if (tp->t_inpcb) {
    546 		so = tp->t_inpcb->inp_socket;
    547 		ro = &tp->t_inpcb->inp_route;
    548 	}
    549 #ifdef INET6
    550 	else if (tp->t_in6pcb) {
    551 		so = tp->t_in6pcb->in6p_socket;
    552 		ro = (struct route *)&tp->t_in6pcb->in6p_route;
    553 	}
    554 #endif
    555 
    556 	switch (af = tp->t_family) {
    557 #ifdef INET
    558 	case AF_INET:
    559 		if (tp->t_inpcb)
    560 			break;
    561 #ifdef INET6
    562 		/* mapped addr case */
    563 		if (tp->t_in6pcb)
    564 			break;
    565 #endif
    566 		return EINVAL;
    567 #endif
    568 #ifdef INET6
    569 	case AF_INET6:
    570 		if (tp->t_in6pcb)
    571 			break;
    572 		return EINVAL;
    573 #endif
    574 	default:
    575 		return EAFNOSUPPORT;
    576 	}
    577 
    578 	tcp_segsize(tp, &txsegsize, &rxsegsize);
    579 
    580 	idle = (tp->snd_max == tp->snd_una);
    581 
    582 	/*
    583 	 * Restart Window computation.  From draft-floyd-incr-init-win-03:
    584 	 *
    585 	 *	Optionally, a TCP MAY set the restart window to the
    586 	 *	minimum of the value used for the initial window and
    587 	 *	the current value of cwnd (in other words, using a
    588 	 *	larger value for the restart window should never increase
    589 	 *	the size of cwnd).
    590 	 */
    591 	if (tcp_cwm) {
    592 		/*
    593 		 * Hughes/Touch/Heidemann Congestion Window Monitoring.
    594 		 * Count the number of packets currently pending
    595 		 * acknowledgement, and limit our congestion window
    596 		 * to a pre-determined allowed burst size plus that count.
    597 		 * This prevents bursting once all pending packets have
    598 		 * been acknowledged (i.e. transmission is idle).
    599 		 *
    600 		 * XXX Link this to Initial Window?
    601 		 */
    602 		tp->snd_cwnd = min(tp->snd_cwnd,
    603 		    (tcp_cwm_burstsize * txsegsize) +
    604 		    (tp->snd_nxt - tp->snd_una));
    605 	} else {
    606 		if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) {
    607 			/*
    608 			 * We have been idle for "a while" and no acks are
    609 			 * expected to clock out any data we send --
    610 			 * slow start to get ack "clock" running again.
    611 			 */
    612 			int ss = tcp_init_win;
    613 #ifdef INET
    614 			if (tp->t_inpcb &&
    615 			    in_localaddr(tp->t_inpcb->inp_faddr))
    616 				ss = tcp_init_win_local;
    617 #endif
    618 #ifdef INET6
    619 			if (tp->t_in6pcb &&
    620 			    in6_localaddr(&tp->t_in6pcb->in6p_faddr))
    621 				ss = tcp_init_win_local;
    622 #endif
    623 			tp->snd_cwnd = min(tp->snd_cwnd,
    624 			    TCP_INITIAL_WINDOW(ss, txsegsize));
    625 		}
    626 	}
    627 
    628 again:
    629 	/*
    630 	 * Determine length of data that should be transmitted, and
    631 	 * flags that should be used.  If there is some data or critical
    632 	 * controls (SYN, RST) to send, then transmit; otherwise,
    633 	 * investigate further.
    634 	 */
    635 	sendalot = 0;
    636 	off = tp->snd_nxt - tp->snd_una;
    637 	win = min(tp->snd_wnd, tp->snd_cwnd);
    638 
    639 	flags = tcp_outflags[tp->t_state];
    640 	/*
    641 	 * If in persist timeout with window of 0, send 1 byte.
    642 	 * Otherwise, if window is small but nonzero
    643 	 * and timer expired, we will send what we can
    644 	 * and go to transmit state.
    645 	 */
    646 	if (tp->t_force) {
    647 		if (win == 0) {
    648 			/*
    649 			 * If we still have some data to send, then
    650 			 * clear the FIN bit.  Usually this would
    651 			 * happen below when it realizes that we
    652 			 * aren't sending all the data.  However,
    653 			 * if we have exactly 1 byte of unset data,
    654 			 * then it won't clear the FIN bit below,
    655 			 * and if we are in persist state, we wind
    656 			 * up sending the packet without recording
    657 			 * that we sent the FIN bit.
    658 			 *
    659 			 * We can't just blindly clear the FIN bit,
    660 			 * because if we don't have any more data
    661 			 * to send then the probe will be the FIN
    662 			 * itself.
    663 			 */
    664 			if (off < so->so_snd.sb_cc)
    665 				flags &= ~TH_FIN;
    666 			win = 1;
    667 		} else {
    668 			TCP_TIMER_DISARM(tp, TCPT_PERSIST);
    669 			tp->t_rxtshift = 0;
    670 		}
    671 	}
    672 
    673 	if (win < so->so_snd.sb_cc) {
    674 		len = win - off;
    675 		flags &= ~TH_FIN;
    676 	} else
    677 		len = so->so_snd.sb_cc - off;
    678 
    679 	if (len < 0) {
    680 		/*
    681 		 * If FIN has been sent but not acked,
    682 		 * but we haven't been called to retransmit,
    683 		 * len will be -1.  Otherwise, window shrank
    684 		 * after we sent into it.  If window shrank to 0,
    685 		 * cancel pending retransmit, pull snd_nxt back
    686 		 * to (closed) window, and set the persist timer
    687 		 * if it isn't already going.  If the window didn't
    688 		 * close completely, just wait for an ACK.
    689 		 *
    690 		 * If we have a pending FIN, either it has already been
    691 		 * transmitted or it is outside the window, so drop it.
    692 		 * If the FIN has been transmitted, but this is not a
    693 		 * retransmission, then len must be -1.  Therefore we also
    694 		 * prevent here the sending of `gratuitous FINs'.  This
    695 		 * eliminates the need to check for that case below (e.g.
    696 		 * to back up snd_nxt before the FIN so that the sequence
    697 		 * number is correct).
    698 		 */
    699 		len = 0;
    700 		flags &= ~TH_FIN;
    701 		if (win == 0) {
    702 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
    703 			tp->t_rxtshift = 0;
    704 			tp->snd_nxt = tp->snd_una;
    705 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
    706 				tcp_setpersist(tp);
    707 		}
    708 	}
    709 	if (len > txsegsize) {
    710 		len = txsegsize;
    711 		flags &= ~TH_FIN;
    712 		sendalot = 1;
    713 	}
    714 
    715 	win = sbspace(&so->so_rcv);
    716 
    717 	/*
    718 	 * Sender silly window avoidance.  If connection is idle
    719 	 * and can send all data, a maximum segment,
    720 	 * at least a maximum default-size segment do it,
    721 	 * or are forced, do it; otherwise don't bother.
    722 	 * If peer's buffer is tiny, then send
    723 	 * when window is at least half open.
    724 	 * If retransmitting (possibly after persist timer forced us
    725 	 * to send into a small window), then must resend.
    726 	 */
    727 	if (len) {
    728 		if (len == txsegsize)
    729 			goto send;
    730 		if ((so->so_state & SS_MORETOCOME) == 0 &&
    731 		    ((idle || tp->t_flags & TF_NODELAY) &&
    732 		     len + off >= so->so_snd.sb_cc))
    733 			goto send;
    734 		if (tp->t_force)
    735 			goto send;
    736 		if (len >= tp->max_sndwnd / 2)
    737 			goto send;
    738 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
    739 			goto send;
    740 	}
    741 
    742 	/*
    743 	 * Compare available window to amount of window known to peer
    744 	 * (as advertised window less next expected input).  If the
    745 	 * difference is at least twice the size of the largest segment
    746 	 * we expect to receive (i.e. two segments) or at least 50% of
    747 	 * the maximum possible window, then want to send a window update
    748 	 * to peer.
    749 	 */
    750 	if (win > 0) {
    751 		/*
    752 		 * "adv" is the amount we can increase the window,
    753 		 * taking into account that we are limited by
    754 		 * TCP_MAXWIN << tp->rcv_scale.
    755 		 */
    756 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
    757 			(tp->rcv_adv - tp->rcv_nxt);
    758 
    759 		if (adv >= (long) (2 * rxsegsize))
    760 			goto send;
    761 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
    762 			goto send;
    763 	}
    764 
    765 	/*
    766 	 * Send if we owe peer an ACK.
    767 	 */
    768 	if (tp->t_flags & TF_ACKNOW)
    769 		goto send;
    770 	if (flags & (TH_SYN|TH_FIN|TH_RST))
    771 		goto send;
    772 	if (SEQ_GT(tp->snd_up, tp->snd_una))
    773 		goto send;
    774 
    775 	/*
    776 	 * TCP window updates are not reliable, rather a polling protocol
    777 	 * using ``persist'' packets is used to insure receipt of window
    778 	 * updates.  The three ``states'' for the output side are:
    779 	 *	idle			not doing retransmits or persists
    780 	 *	persisting		to move a small or zero window
    781 	 *	(re)transmitting	and thereby not persisting
    782 	 *
    783 	 * tp->t_timer[TCPT_PERSIST]
    784 	 *	is set when we are in persist state.
    785 	 * tp->t_force
    786 	 *	is set when we are called to send a persist packet.
    787 	 * tp->t_timer[TCPT_REXMT]
    788 	 *	is set when we are retransmitting
    789 	 * The output side is idle when both timers are zero.
    790 	 *
    791 	 * If send window is too small, there is data to transmit, and no
    792 	 * retransmit or persist is pending, then go to persist state.
    793 	 * If nothing happens soon, send when timer expires:
    794 	 * if window is nonzero, transmit what we can,
    795 	 * otherwise force out a byte.
    796 	 */
    797 	if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
    798 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
    799 		tp->t_rxtshift = 0;
    800 		tcp_setpersist(tp);
    801 	}
    802 
    803 	/*
    804 	 * No reason to send a segment, just return.
    805 	 */
    806 	return (0);
    807 
    808 send:
    809 	/*
    810 	 * Before ESTABLISHED, force sending of initial options
    811 	 * unless TCP set not to do any options.
    812 	 * NOTE: we assume that the IP/TCP header plus TCP options
    813 	 * always fit in a single mbuf, leaving room for a maximum
    814 	 * link header, i.e.
    815 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
    816 	 */
    817 	optlen = 0;
    818 	switch (af) {
    819 #ifdef INET
    820 	case AF_INET:
    821 		iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
    822 		break;
    823 #endif
    824 #ifdef INET6
    825 	case AF_INET6:
    826 		iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
    827 		break;
    828 #endif
    829 	default:	/*pacify gcc*/
    830 		iphdrlen = 0;
    831 		break;
    832 	}
    833 	hdrlen = iphdrlen;
    834 	if (flags & TH_SYN) {
    835 		struct rtentry *rt;
    836 
    837 		rt = NULL;
    838 #ifdef INET
    839 		if (tp->t_inpcb)
    840 			rt = in_pcbrtentry(tp->t_inpcb);
    841 #endif
    842 #ifdef INET6
    843 		if (tp->t_in6pcb)
    844 			rt = in6_pcbrtentry(tp->t_in6pcb);
    845 #endif
    846 
    847 		tp->snd_nxt = tp->iss;
    848 		tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
    849 						    rt->rt_ifp : NULL, af);
    850 		if ((tp->t_flags & TF_NOOPT) == 0) {
    851 			opt[0] = TCPOPT_MAXSEG;
    852 			opt[1] = 4;
    853 			opt[2] = (tp->t_ourmss >> 8) & 0xff;
    854 			opt[3] = tp->t_ourmss & 0xff;
    855 			optlen = 4;
    856 
    857 			if ((tp->t_flags & TF_REQ_SCALE) &&
    858 			    ((flags & TH_ACK) == 0 ||
    859 			    (tp->t_flags & TF_RCVD_SCALE))) {
    860 				*((u_int32_t *) (opt + optlen)) = htonl(
    861 					TCPOPT_NOP << 24 |
    862 					TCPOPT_WINDOW << 16 |
    863 					TCPOLEN_WINDOW << 8 |
    864 					tp->request_r_scale);
    865 				optlen += 4;
    866 			}
    867 		}
    868 	}
    869 
    870 	/*
    871 	 * Send a timestamp and echo-reply if this is a SYN and our side
    872 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
    873 	 * and our peer have sent timestamps in our SYN's.
    874 	 */
    875 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
    876 	     (flags & TH_RST) == 0 &&
    877 	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
    878 	     (tp->t_flags & TF_RCVD_TSTMP))) {
    879 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
    880 
    881 		/* Form timestamp option as shown in appendix A of RFC 1323. */
    882 		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
    883 		*lp++ = htonl(TCP_TIMESTAMP(tp));
    884 		*lp   = htonl(tp->ts_recent);
    885 		optlen += TCPOLEN_TSTAMP_APPA;
    886 	}
    887 
    888 	hdrlen += optlen;
    889 
    890 #ifdef DIAGNOSTIC
    891 	if (len > txsegsize)
    892 		panic("tcp data to be sent is larger than segment");
    893 	if (max_linkhdr + hdrlen > MCLBYTES)
    894 		panic("tcphdr too big");
    895 #endif
    896 
    897 	/*
    898 	 * Grab a header mbuf, attaching a copy of data to
    899 	 * be transmitted, and initialize the header from
    900 	 * the template for sends on this connection.
    901 	 */
    902 	if (len) {
    903 		error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
    904 		if (error)
    905 			goto out;
    906 		/*
    907 		 * If we're sending everything we've got, set PUSH.
    908 		 * (This will keep happy those implementations which only
    909 		 * give data to the user when a buffer fills or
    910 		 * a PUSH comes in.)
    911 		 */
    912 		if (off + len == so->so_snd.sb_cc)
    913 			flags |= TH_PUSH;
    914 	} else {
    915 		if (tp->t_flags & TF_ACKNOW)
    916 			tcpstat.tcps_sndacks++;
    917 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
    918 			tcpstat.tcps_sndctrl++;
    919 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
    920 			tcpstat.tcps_sndurg++;
    921 		else
    922 			tcpstat.tcps_sndwinup++;
    923 
    924 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    925 		if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
    926 			MCLGET(m, M_DONTWAIT);
    927 			if ((m->m_flags & M_EXT) == 0) {
    928 				m_freem(m);
    929 				m = NULL;
    930 			}
    931 		}
    932 		if (m == NULL) {
    933 			error = ENOBUFS;
    934 			goto out;
    935 		}
    936 		MCLAIM(m, &tcp_tx_mowner);
    937 		m->m_data += max_linkhdr;
    938 		m->m_len = hdrlen;
    939 	}
    940 	m->m_pkthdr.rcvif = (struct ifnet *)0;
    941 	switch (af) {
    942 #ifdef INET
    943 	case AF_INET:
    944 		ip = mtod(m, struct ip *);
    945 #ifdef INET6
    946 		ip6 = NULL;
    947 #endif
    948 		th = (struct tcphdr *)(ip + 1);
    949 		break;
    950 #endif
    951 #ifdef INET6
    952 	case AF_INET6:
    953 		ip = NULL;
    954 		ip6 = mtod(m, struct ip6_hdr *);
    955 		th = (struct tcphdr *)(ip6 + 1);
    956 		break;
    957 #endif
    958 	default:	/*pacify gcc*/
    959 		ip = NULL;
    960 #ifdef INET6
    961 		ip6 = NULL;
    962 #endif
    963 		th = NULL;
    964 		break;
    965 	}
    966 	if (tp->t_template == 0)
    967 		panic("tcp_output");
    968 	if (tp->t_template->m_len < iphdrlen)
    969 		panic("tcp_output");
    970 	bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), iphdrlen);
    971 
    972 	/*
    973 	 * If we are doing retransmissions, then snd_nxt will
    974 	 * not reflect the first unsent octet.  For ACK only
    975 	 * packets, we do not want the sequence number of the
    976 	 * retransmitted packet, we want the sequence number
    977 	 * of the next unsent octet.  So, if there is no data
    978 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
    979 	 * when filling in ti_seq.  But if we are in persist
    980 	 * state, snd_max might reflect one byte beyond the
    981 	 * right edge of the window, so use snd_nxt in that
    982 	 * case, since we know we aren't doing a retransmission.
    983 	 * (retransmit and persist are mutually exclusive...)
    984 	 */
    985 	if (len || (flags & (TH_SYN|TH_FIN)) ||
    986 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
    987 		th->th_seq = htonl(tp->snd_nxt);
    988 	else
    989 		th->th_seq = htonl(tp->snd_max);
    990 	th->th_ack = htonl(tp->rcv_nxt);
    991 	if (optlen) {
    992 		bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen);
    993 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
    994 	}
    995 	th->th_flags = flags;
    996 	/*
    997 	 * Calculate receive window.  Don't shrink window,
    998 	 * but avoid silly window syndrome.
    999 	 */
   1000 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
   1001 		win = 0;
   1002 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
   1003 		win = (long)TCP_MAXWIN << tp->rcv_scale;
   1004 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
   1005 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
   1006 	th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
   1007 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
   1008 		u_int32_t urp = tp->snd_up - tp->snd_nxt;
   1009 		if (urp > IP_MAXPACKET)
   1010 			urp = IP_MAXPACKET;
   1011 		th->th_urp = htons((u_int16_t)urp);
   1012 		th->th_flags |= TH_URG;
   1013 	} else
   1014 		/*
   1015 		 * If no urgent pointer to send, then we pull
   1016 		 * the urgent pointer to the left edge of the send window
   1017 		 * so that it doesn't drift into the send window on sequence
   1018 		 * number wraparound.
   1019 		 */
   1020 		tp->snd_up = tp->snd_una;		/* drag it along */
   1021 
   1022 	/*
   1023 	 * Set ourselves up to be checksummed just before the packet
   1024 	 * hits the wire.
   1025 	 */
   1026 	switch (af) {
   1027 #ifdef INET
   1028 	case AF_INET:
   1029 		m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
   1030 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
   1031 		if (len + optlen) {
   1032 			/* Fixup the pseudo-header checksum. */
   1033 			/* XXXJRT Not IP Jumbogram safe. */
   1034 			th->th_sum = in_cksum_addword(th->th_sum,
   1035 			    htons((u_int16_t) (len + optlen)));
   1036 		}
   1037 		break;
   1038 #endif
   1039 #ifdef INET6
   1040 	case AF_INET6:
   1041 		/*
   1042 		 * XXX Actually delaying the checksum is Hard
   1043 		 * XXX (well, maybe not for Itojun, but it is
   1044 		 * XXX for me), but we can still take advantage
   1045 		 * XXX of the cached pseudo-header checksum.
   1046 		 */
   1047 		/* equals to hdrlen + len */
   1048 		m->m_pkthdr.len = sizeof(struct ip6_hdr)
   1049 			+ sizeof(struct tcphdr) + optlen + len;
   1050 #ifdef notyet
   1051 		m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
   1052 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
   1053 #endif
   1054 		if (len + optlen) {
   1055 			/* Fixup the pseudo-header checksum. */
   1056 			/* XXXJRT: Not IPv6 Jumbogram safe. */
   1057 			th->th_sum = in_cksum_addword(th->th_sum,
   1058 			    htons((u_int16_t) (len + optlen)));
   1059 		}
   1060 #ifndef notyet
   1061 		th->th_sum = in6_cksum(m, 0, sizeof(struct ip6_hdr),
   1062 		    sizeof(struct tcphdr) + optlen + len);
   1063 #endif
   1064 		break;
   1065 #endif
   1066 	}
   1067 
   1068 	/*
   1069 	 * In transmit state, time the transmission and arrange for
   1070 	 * the retransmit.  In persist state, just set snd_max.
   1071 	 */
   1072 	if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
   1073 		tcp_seq startseq = tp->snd_nxt;
   1074 
   1075 		/*
   1076 		 * Advance snd_nxt over sequence space of this segment.
   1077 		 * There are no states in which we send both a SYN and a FIN,
   1078 		 * so we collapse the tests for these flags.
   1079 		 */
   1080 		if (flags & (TH_SYN|TH_FIN))
   1081 			tp->snd_nxt++;
   1082 		tp->snd_nxt += len;
   1083 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
   1084 			tp->snd_max = tp->snd_nxt;
   1085 			/*
   1086 			 * Time this transmission if not a retransmission and
   1087 			 * not currently timing anything.
   1088 			 */
   1089 			if (tp->t_rtttime == 0) {
   1090 				tp->t_rtttime = tcp_now;
   1091 				tp->t_rtseq = startseq;
   1092 				tcpstat.tcps_segstimed++;
   1093 			}
   1094 		}
   1095 
   1096 		/*
   1097 		 * Set retransmit timer if not currently set,
   1098 		 * and not doing an ack or a keep-alive probe.
   1099 		 * Initial value for retransmit timer is smoothed
   1100 		 * round-trip time + 2 * round-trip time variance.
   1101 		 * Initialize shift counter which is used for backoff
   1102 		 * of retransmit time.
   1103 		 */
   1104 		if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
   1105 		    tp->snd_nxt != tp->snd_una) {
   1106 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   1107 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
   1108 				TCP_TIMER_DISARM(tp, TCPT_PERSIST);
   1109 				tp->t_rxtshift = 0;
   1110 			}
   1111 		}
   1112 	} else
   1113 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
   1114 			tp->snd_max = tp->snd_nxt + len;
   1115 
   1116 #ifdef TCP_DEBUG
   1117 	/*
   1118 	 * Trace.
   1119 	 */
   1120 	if (so->so_options & SO_DEBUG)
   1121 		tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
   1122 #endif
   1123 
   1124 	/*
   1125 	 * Fill in IP length and desired time to live and
   1126 	 * send to IP level.  There should be a better way
   1127 	 * to handle ttl and tos; we could keep them in
   1128 	 * the template, but need a way to checksum without them.
   1129 	 */
   1130 	m->m_pkthdr.len = hdrlen + len;
   1131 
   1132 	switch (af) {
   1133 #ifdef INET
   1134 	case AF_INET:
   1135 		ip->ip_len = htons(m->m_pkthdr.len);
   1136 		if (tp->t_inpcb) {
   1137 			ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
   1138 			ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
   1139 		}
   1140 #ifdef INET6
   1141 		else if (tp->t_in6pcb) {
   1142 			ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
   1143 			ip->ip_tos = 0;	/*XXX*/
   1144 		}
   1145 #endif
   1146 		break;
   1147 #endif
   1148 #ifdef INET6
   1149 	case AF_INET6:
   1150 		ip6->ip6_nxt = IPPROTO_TCP;
   1151 		if (tp->t_in6pcb) {
   1152 			/*
   1153 			 * we separately set hoplimit for every segment, since
   1154 			 * the user might want to change the value via
   1155 			 * setsockopt. Also, desired default hop limit might
   1156 			 * be changed via Neighbor Discovery.
   1157 			 */
   1158 			ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
   1159 				ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
   1160 		}
   1161 		/* ip6->ip6_flow = ??? */
   1162 		/* ip6_plen will be filled in ip6_output(). */
   1163 		break;
   1164 #endif
   1165 	}
   1166 
   1167 	switch (af) {
   1168 #ifdef INET
   1169 	case AF_INET:
   1170 	    {
   1171 		struct mbuf *opts;
   1172 
   1173 		if (tp->t_inpcb)
   1174 			opts = tp->t_inpcb->inp_options;
   1175 		else
   1176 			opts = NULL;
   1177 		error = ip_output(m, opts, ro,
   1178 			(tp->t_mtudisc ? IP_MTUDISC : 0) |
   1179 			(so->so_options & SO_DONTROUTE),
   1180 			(struct ip_moptions *)0, so);
   1181 		break;
   1182 	    }
   1183 #endif
   1184 #ifdef INET6
   1185 	case AF_INET6:
   1186 	    {
   1187 		struct ip6_pktopts *opts;
   1188 
   1189 		if (tp->t_in6pcb)
   1190 			opts = tp->t_in6pcb->in6p_outputopts;
   1191 		else
   1192 			opts = NULL;
   1193 		error = ip6_output(m, opts, (struct route_in6 *)ro,
   1194 			so->so_options & SO_DONTROUTE,
   1195 			(struct ip6_moptions *)0, so, NULL);
   1196 		break;
   1197 	    }
   1198 #endif
   1199 	default:
   1200 		error = EAFNOSUPPORT;
   1201 		break;
   1202 	}
   1203 	if (error) {
   1204 out:
   1205 		if (error == ENOBUFS) {
   1206 			tcpstat.tcps_selfquench++;
   1207 #ifdef INET
   1208 			if (tp->t_inpcb)
   1209 				tcp_quench(tp->t_inpcb, 0);
   1210 #endif
   1211 #ifdef INET6
   1212 			if (tp->t_in6pcb)
   1213 				tcp6_quench(tp->t_in6pcb, 0);
   1214 #endif
   1215 			error = 0;
   1216 		} else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
   1217 		    TCPS_HAVERCVDSYN(tp->t_state)) {
   1218 			tp->t_softerror = error;
   1219 			error = 0;
   1220 		}
   1221 
   1222 		/* Restart the delayed ACK timer, if necessary. */
   1223 		if (tp->t_flags & TF_DELACK)
   1224 			TCP_RESTART_DELACK(tp);
   1225 
   1226 		return (error);
   1227 	}
   1228 	tcpstat.tcps_sndtotal++;
   1229 	if (tp->t_flags & TF_DELACK)
   1230 		tcpstat.tcps_delack++;
   1231 
   1232 	/*
   1233 	 * Data sent (as far as we can tell).
   1234 	 * If this advertises a larger window than any other segment,
   1235 	 * then remember the size of the advertised window.
   1236 	 * Any pending ACK has now been sent.
   1237 	 */
   1238 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
   1239 		tp->rcv_adv = tp->rcv_nxt + win;
   1240 	tp->last_ack_sent = tp->rcv_nxt;
   1241 	tp->t_flags &= ~TF_ACKNOW;
   1242 	TCP_CLEAR_DELACK(tp);
   1243 #ifdef DIAGNOSTIC
   1244 	if (maxburst < 0)
   1245 		printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
   1246 #endif
   1247 	if (sendalot && (!tcp_do_newreno || --maxburst))
   1248 		goto again;
   1249 	return (0);
   1250 }
   1251 
   1252 void
   1253 tcp_setpersist(tp)
   1254 	struct tcpcb *tp;
   1255 {
   1256 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
   1257 	int nticks;
   1258 
   1259 	if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
   1260 		panic("tcp_output REXMT");
   1261 	/*
   1262 	 * Start/restart persistance timer.
   1263 	 */
   1264 	if (t < tp->t_rttmin)
   1265 		t = tp->t_rttmin;
   1266 	TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
   1267 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
   1268 	TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
   1269 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
   1270 		tp->t_rxtshift++;
   1271 }
   1272