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