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