Home | History | Annotate | Line # | Download | only in netinet
tcp_output.c revision 1.179
      1 /*	$NetBSD: tcp_output.c,v 1.179 2014/11/10 18:52:51 maxv 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, 2005, 2006 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  * This code is derived from software contributed to The NetBSD Foundation
     80  * by Charles M. Hannum.
     81  * This code is derived from software contributed to The NetBSD Foundation
     82  * by Rui Paulo.
     83  *
     84  * Redistribution and use in source and binary forms, with or without
     85  * modification, are permitted provided that the following conditions
     86  * are met:
     87  * 1. Redistributions of source code must retain the above copyright
     88  *    notice, this list of conditions and the following disclaimer.
     89  * 2. Redistributions in binary form must reproduce the above copyright
     90  *    notice, this list of conditions and the following disclaimer in the
     91  *    documentation and/or other materials provided with the distribution.
     92  *
     93  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     94  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     95  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     96  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     97  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     98  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     99  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    100  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    101  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    103  * POSSIBILITY OF SUCH DAMAGE.
    104  */
    105 
    106 /*
    107  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
    108  *	The Regents of the University of California.  All rights reserved.
    109  *
    110  * Redistribution and use in source and binary forms, with or without
    111  * modification, are permitted provided that the following conditions
    112  * are met:
    113  * 1. Redistributions of source code must retain the above copyright
    114  *    notice, this list of conditions and the following disclaimer.
    115  * 2. Redistributions in binary form must reproduce the above copyright
    116  *    notice, this list of conditions and the following disclaimer in the
    117  *    documentation and/or other materials provided with the distribution.
    118  * 3. Neither the name of the University nor the names of its contributors
    119  *    may be used to endorse or promote products derived from this software
    120  *    without specific prior written permission.
    121  *
    122  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    123  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    124  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    125  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    126  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    127  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    128  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    129  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    130  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    131  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    132  * SUCH DAMAGE.
    133  *
    134  *	@(#)tcp_output.c	8.4 (Berkeley) 5/24/95
    135  */
    136 
    137 #include <sys/cdefs.h>
    138 __KERNEL_RCSID(0, "$NetBSD: tcp_output.c,v 1.179 2014/11/10 18:52:51 maxv Exp $");
    139 
    140 #include "opt_inet.h"
    141 #include "opt_ipsec.h"
    142 #include "opt_tcp_debug.h"
    143 
    144 #include <sys/param.h>
    145 #include <sys/systm.h>
    146 #include <sys/mbuf.h>
    147 #include <sys/protosw.h>
    148 #include <sys/socket.h>
    149 #include <sys/socketvar.h>
    150 #include <sys/errno.h>
    151 #include <sys/domain.h>
    152 #include <sys/kernel.h>
    153 #ifdef TCP_SIGNATURE
    154 #include <sys/md5.h>
    155 #endif
    156 
    157 #include <net/if.h>
    158 #include <net/route.h>
    159 
    160 #include <netinet/in.h>
    161 #include <netinet/in_systm.h>
    162 #include <netinet/ip.h>
    163 #include <netinet/in_pcb.h>
    164 #include <netinet/ip_var.h>
    165 
    166 #ifdef INET6
    167 #ifndef INET
    168 #include <netinet/in.h>
    169 #endif
    170 #include <netinet/ip6.h>
    171 #include <netinet6/in6_var.h>
    172 #include <netinet6/ip6_var.h>
    173 #include <netinet6/in6_pcb.h>
    174 #include <netinet6/nd6.h>
    175 #endif
    176 
    177 #ifdef IPSEC
    178 #include <netipsec/ipsec.h>
    179 #include <netipsec/key.h>
    180 #ifdef INET6
    181 #include <netipsec/ipsec6.h>
    182 #endif
    183 #endif	/* IPSEC*/
    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/tcp_private.h>
    192 #include <netinet/tcp_congctl.h>
    193 #include <netinet/tcpip.h>
    194 #include <netinet/tcp_debug.h>
    195 #include <netinet/in_offload.h>
    196 #include <netinet6/in6_offload.h>
    197 
    198 #ifdef notyet
    199 extern struct mbuf *m_copypack();
    200 #endif
    201 
    202 /*
    203  * Knob to enable Congestion Window Monitoring, and control
    204  * the burst size it allows.  Default burst is 4 packets, per
    205  * the Internet draft.
    206  */
    207 int	tcp_cwm = 0;
    208 int	tcp_cwm_burstsize = 4;
    209 
    210 int	tcp_do_autosndbuf = 1;
    211 int	tcp_autosndbuf_inc = 8 * 1024;
    212 int	tcp_autosndbuf_max = 256 * 1024;
    213 
    214 #ifdef TCP_OUTPUT_COUNTERS
    215 #include <sys/device.h>
    216 
    217 extern struct evcnt tcp_output_bigheader;
    218 extern struct evcnt tcp_output_predict_hit;
    219 extern struct evcnt tcp_output_predict_miss;
    220 extern struct evcnt tcp_output_copysmall;
    221 extern struct evcnt tcp_output_copybig;
    222 extern struct evcnt tcp_output_refbig;
    223 
    224 #define	TCP_OUTPUT_COUNTER_INCR(ev)	(ev)->ev_count++
    225 #else
    226 
    227 #define	TCP_OUTPUT_COUNTER_INCR(ev)	/* nothing */
    228 
    229 #endif /* TCP_OUTPUT_COUNTERS */
    230 
    231 static
    232 #ifndef GPROF
    233 inline
    234 #endif
    235 int
    236 tcp_segsize(struct tcpcb *tp, int *txsegsizep, int *rxsegsizep,
    237     bool *alwaysfragp)
    238 {
    239 #ifdef INET
    240 	struct inpcb *inp = tp->t_inpcb;
    241 #endif
    242 #ifdef INET6
    243 	struct in6pcb *in6p = tp->t_in6pcb;
    244 #endif
    245 	struct socket *so = NULL;
    246 	struct rtentry *rt;
    247 	struct ifnet *ifp;
    248 	int size;
    249 	int hdrlen;
    250 	int optlen;
    251 
    252 	*alwaysfragp = false;
    253 
    254 #ifdef DIAGNOSTIC
    255 	if (tp->t_inpcb && tp->t_in6pcb)
    256 		panic("tcp_segsize: both t_inpcb and t_in6pcb are set");
    257 #endif
    258 	switch (tp->t_family) {
    259 #ifdef INET
    260 	case AF_INET:
    261 		hdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
    262 		break;
    263 #endif
    264 #ifdef INET6
    265 	case AF_INET6:
    266 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
    267 		break;
    268 #endif
    269 	default:
    270 		size = tcp_mssdflt;
    271 		goto out;
    272 	}
    273 
    274 	rt = NULL;
    275 #ifdef INET
    276 	if (inp) {
    277 		rt = in_pcbrtentry(inp);
    278 		so = inp->inp_socket;
    279 	}
    280 #endif
    281 #ifdef INET6
    282 	if (in6p) {
    283 		rt = in6_pcbrtentry(in6p);
    284 		so = in6p->in6p_socket;
    285 	}
    286 #endif
    287 	if (rt == NULL) {
    288 		size = tcp_mssdflt;
    289 		goto out;
    290 	}
    291 
    292 	ifp = rt->rt_ifp;
    293 
    294 	size = tcp_mssdflt;
    295 	if (tp->t_mtudisc && rt->rt_rmx.rmx_mtu != 0) {
    296 #ifdef INET6
    297 		if (in6p && rt->rt_rmx.rmx_mtu < IPV6_MMTU) {
    298 			/*
    299 			 * RFC2460 section 5, last paragraph: if path MTU is
    300 			 * smaller than 1280, use 1280 as packet size and
    301 			 * attach fragment header.
    302 			 */
    303 			size = IPV6_MMTU - hdrlen - sizeof(struct ip6_frag);
    304 			*alwaysfragp = true;
    305 		} else
    306 			size = rt->rt_rmx.rmx_mtu - hdrlen;
    307 #else
    308 		size = rt->rt_rmx.rmx_mtu - hdrlen;
    309 #endif
    310 	} else if (ifp->if_flags & IFF_LOOPBACK)
    311 		size = ifp->if_mtu - hdrlen;
    312 #ifdef INET
    313 	else if (inp && tp->t_mtudisc)
    314 		size = ifp->if_mtu - hdrlen;
    315 	else if (inp && in_localaddr(inp->inp_faddr))
    316 		size = ifp->if_mtu - hdrlen;
    317 #endif
    318 #ifdef INET6
    319 	else if (in6p) {
    320 #ifdef INET
    321 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
    322 			/* mapped addr case */
    323 			struct in_addr d;
    324 			bcopy(&in6p->in6p_faddr.s6_addr32[3], &d, sizeof(d));
    325 			if (tp->t_mtudisc || in_localaddr(d))
    326 				size = ifp->if_mtu - hdrlen;
    327 		} else
    328 #endif
    329 		{
    330 			/*
    331 			 * for IPv6, path MTU discovery is always turned on,
    332 			 * or the node must use packet size <= 1280.
    333 			 */
    334 			size = tp->t_mtudisc ? IN6_LINKMTU(ifp) : IPV6_MMTU;
    335 			size -= hdrlen;
    336 		}
    337 	}
    338 #endif
    339  out:
    340 	/*
    341 	 * Now we must make room for whatever extra TCP/IP options are in
    342 	 * the packet.
    343 	 */
    344 	optlen = tcp_optlen(tp);
    345 
    346 	/*
    347 	 * XXX tp->t_ourmss should have the right size, but without this code
    348 	 * fragmentation will occur... need more investigation
    349 	 */
    350 #ifdef INET
    351 	if (inp) {
    352 #if defined(IPSEC)
    353 		if (ipsec_used &&
    354 		    !IPSEC_PCB_SKIP_IPSEC(inp->inp_sp, IPSEC_DIR_OUTBOUND))
    355 			optlen += ipsec4_hdrsiz_tcp(tp);
    356 #endif
    357 		optlen += ip_optlen(inp);
    358 	}
    359 #endif
    360 #ifdef INET6
    361 #ifdef INET
    362 	if (in6p && tp->t_family == AF_INET) {
    363 #if defined(IPSEC)
    364 		if (ipsec_used &&
    365 		    !IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
    366 			optlen += ipsec4_hdrsiz_tcp(tp);
    367 #endif
    368 		/* XXX size -= ip_optlen(in6p); */
    369 	} else
    370 #endif
    371 	if (in6p && tp->t_family == AF_INET6) {
    372 #if defined(IPSEC)
    373 		if (ipsec_used &&
    374 		    !IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
    375 			optlen += ipsec6_hdrsiz_tcp(tp);
    376 #endif
    377 		optlen += ip6_optlen(in6p);
    378 	}
    379 #endif
    380 	size -= optlen;
    381 
    382 	/* there may not be any room for data if mtu is too small */
    383 	if (size < 0)
    384 		return (EMSGSIZE);
    385 
    386 	/*
    387 	 * *rxsegsizep holds *estimated* inbound segment size (estimation
    388 	 * assumes that path MTU is the same for both ways).  this is only
    389 	 * for silly window avoidance, do not use the value for other purposes.
    390 	 *
    391 	 * ipseclen is subtracted from both sides, this may not be right.
    392 	 * I'm not quite sure about this (could someone comment).
    393 	 */
    394 	*txsegsizep = min(tp->t_peermss - optlen, size);
    395 	/*
    396 	 * Never send more than half a buffer full.  This insures that we can
    397 	 * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and
    398 	 * therefore acks will never be delayed unless we run out of data to
    399 	 * transmit.
    400 	 */
    401 	if (so)
    402 		*txsegsizep = min(so->so_snd.sb_hiwat >> 1, *txsegsizep);
    403 	*rxsegsizep = min(tp->t_ourmss - optlen, size);
    404 
    405 	if (*txsegsizep != tp->t_segsz) {
    406 		/*
    407 		 * If the new segment size is larger, we don't want to
    408 		 * mess up the congestion window, but if it is smaller
    409 		 * we'll have to reduce the congestion window to ensure
    410 		 * that we don't get into trouble with initial windows
    411 		 * and the rest.  In any case, if the segment size
    412 		 * has changed, chances are the path has, too, and
    413 		 * our congestion window will be different.
    414 		 */
    415 		if (*txsegsizep < tp->t_segsz) {
    416 			tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
    417 					   * *txsegsizep, *txsegsizep);
    418 			tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
    419 						* *txsegsizep, *txsegsizep);
    420 		}
    421 		tp->t_segsz = *txsegsizep;
    422 	}
    423 
    424 	return (0);
    425 }
    426 
    427 static
    428 #ifndef GPROF
    429 inline
    430 #endif
    431 int
    432 tcp_build_datapkt(struct tcpcb *tp, struct socket *so, int off,
    433     long len, int hdrlen, struct mbuf **mp)
    434 {
    435 	struct mbuf *m, *m0;
    436 	uint64_t *tcps;
    437 
    438 	tcps = TCP_STAT_GETREF();
    439 	if (tp->t_force && len == 1)
    440 		tcps[TCP_STAT_SNDPROBE]++;
    441 	else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
    442 		tcps[TCP_STAT_SNDREXMITPACK]++;
    443 		tcps[TCP_STAT_SNDREXMITBYTE] += len;
    444 	} else {
    445 		tcps[TCP_STAT_SNDPACK]++;
    446 		tcps[TCP_STAT_SNDBYTE] += len;
    447 	}
    448 	TCP_STAT_PUTREF();
    449 #ifdef notyet
    450 	if ((m = m_copypack(so->so_snd.sb_mb, off,
    451 	    (int)len, max_linkhdr + hdrlen)) == 0)
    452 		return (ENOBUFS);
    453 	/*
    454 	 * m_copypack left space for our hdr; use it.
    455 	 */
    456 	m->m_len += hdrlen;
    457 	m->m_data -= hdrlen;
    458 #else
    459 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
    460 	if (__predict_false(m == NULL))
    461 		return (ENOBUFS);
    462 	MCLAIM(m, &tcp_tx_mowner);
    463 
    464 	/*
    465 	 * XXX Because other code assumes headers will fit in
    466 	 * XXX one header mbuf.
    467 	 *
    468 	 * (This code should almost *never* be run.)
    469 	 */
    470 	if (__predict_false((max_linkhdr + hdrlen) > MHLEN)) {
    471 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_bigheader);
    472 		MCLGET(m, M_DONTWAIT);
    473 		if ((m->m_flags & M_EXT) == 0) {
    474 			m_freem(m);
    475 			return (ENOBUFS);
    476 		}
    477 	}
    478 
    479 	m->m_data += max_linkhdr;
    480 	m->m_len = hdrlen;
    481 
    482 	/*
    483 	 * To avoid traversing the whole sb_mb chain for correct
    484 	 * data to send, remember last sent mbuf, its offset and
    485 	 * the sent size.  When called the next time, see if the
    486 	 * data to send is directly following the previous transfer.
    487 	 * This is important for large TCP windows.
    488 	 */
    489 	if (off == 0 || tp->t_lastm == NULL ||
    490 	    (tp->t_lastoff + tp->t_lastlen) != off) {
    491 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_miss);
    492 		/*
    493 		 * Either a new packet or a retransmit.
    494 		 * Start from the beginning.
    495 		 */
    496 		tp->t_lastm = so->so_snd.sb_mb;
    497 		tp->t_inoff = off;
    498 	} else {
    499 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_hit);
    500 		tp->t_inoff += tp->t_lastlen;
    501 	}
    502 
    503 	/* Traverse forward to next packet */
    504 	while (tp->t_inoff > 0) {
    505 		if (tp->t_lastm == NULL)
    506 			panic("tp->t_lastm == NULL");
    507 		if (tp->t_inoff < tp->t_lastm->m_len)
    508 			break;
    509 		tp->t_inoff -= tp->t_lastm->m_len;
    510 		tp->t_lastm = tp->t_lastm->m_next;
    511 	}
    512 
    513 	tp->t_lastoff = off;
    514 	tp->t_lastlen = len;
    515 	m0 = tp->t_lastm;
    516 	off = tp->t_inoff;
    517 
    518 	if (len <= M_TRAILINGSPACE(m)) {
    519 		m_copydata(m0, off, (int) len, mtod(m, char *) + hdrlen);
    520 		m->m_len += len;
    521 		TCP_OUTPUT_COUNTER_INCR(&tcp_output_copysmall);
    522 	} else {
    523 		m->m_next = m_copym(m0, off, (int) len, M_DONTWAIT);
    524 		if (m->m_next == NULL) {
    525 			m_freem(m);
    526 			return (ENOBUFS);
    527 		}
    528 #ifdef TCP_OUTPUT_COUNTERS
    529 		if (m->m_next->m_flags & M_EXT)
    530 			TCP_OUTPUT_COUNTER_INCR(&tcp_output_refbig);
    531 		else
    532 			TCP_OUTPUT_COUNTER_INCR(&tcp_output_copybig);
    533 #endif /* TCP_OUTPUT_COUNTERS */
    534 	}
    535 #endif
    536 
    537 	*mp = m;
    538 	return (0);
    539 }
    540 
    541 /*
    542  * Tcp output routine: figure out what should be sent and send it.
    543  */
    544 int
    545 tcp_output(struct tcpcb *tp)
    546 {
    547 	struct rtentry *rt;
    548 	struct socket *so;
    549 	struct route *ro;
    550 	long len, win;
    551 	int off, flags, error;
    552 	struct mbuf *m;
    553 	struct ip *ip;
    554 #ifdef INET6
    555 	struct ip6_hdr *ip6;
    556 #endif
    557 	struct tcphdr *th;
    558 	u_char opt[MAX_TCPOPTLEN];
    559 #define OPT_FITS(more)	((optlen + (more)) < sizeof(opt))
    560 	unsigned optlen, hdrlen, packetlen;
    561 	unsigned int sack_numblks;
    562 	int idle, sendalot, txsegsize, rxsegsize;
    563 	int txsegsize_nosack;
    564 	int maxburst = TCP_MAXBURST;
    565 	int af;		/* address family on the wire */
    566 	int iphdrlen;
    567 	int has_tso4, has_tso6;
    568 	int has_tso, use_tso;
    569 	bool alwaysfrag;
    570 	int sack_rxmit;
    571 	int sack_bytes_rxmt;
    572 	int ecn_tos;
    573 	struct sackhole *p;
    574 #ifdef TCP_SIGNATURE
    575 	int sigoff = 0;
    576 #endif
    577 	uint64_t *tcps;
    578 
    579 #ifdef DIAGNOSTIC
    580 	if (tp->t_inpcb && tp->t_in6pcb)
    581 		panic("tcp_output: both t_inpcb and t_in6pcb are set");
    582 #endif
    583 	so = NULL;
    584 	ro = NULL;
    585 	if (tp->t_inpcb) {
    586 		so = tp->t_inpcb->inp_socket;
    587 		ro = &tp->t_inpcb->inp_route;
    588 	}
    589 #ifdef INET6
    590 	else if (tp->t_in6pcb) {
    591 		so = tp->t_in6pcb->in6p_socket;
    592 		ro = &tp->t_in6pcb->in6p_route;
    593 	}
    594 #endif
    595 
    596 	switch (af = tp->t_family) {
    597 #ifdef INET
    598 	case AF_INET:
    599 		if (tp->t_inpcb)
    600 			break;
    601 #ifdef INET6
    602 		/* mapped addr case */
    603 		if (tp->t_in6pcb)
    604 			break;
    605 #endif
    606 		return (EINVAL);
    607 #endif
    608 #ifdef INET6
    609 	case AF_INET6:
    610 		if (tp->t_in6pcb)
    611 			break;
    612 		return (EINVAL);
    613 #endif
    614 	default:
    615 		return (EAFNOSUPPORT);
    616 	}
    617 
    618 	if (tcp_segsize(tp, &txsegsize, &rxsegsize, &alwaysfrag))
    619 		return (EMSGSIZE);
    620 
    621 	idle = (tp->snd_max == tp->snd_una);
    622 
    623 	/*
    624 	 * Determine if we can use TCP segmentation offload:
    625 	 * - If we're using IPv4
    626 	 * - If there is not an IPsec policy that prevents it
    627 	 * - If the interface can do it
    628 	 */
    629 	has_tso4 = has_tso6 = false;
    630 #if defined(INET)
    631 	has_tso4 = tp->t_inpcb != NULL &&
    632 #if defined(IPSEC)
    633 	    (!ipsec_used || IPSEC_PCB_SKIP_IPSEC(tp->t_inpcb->inp_sp,
    634 	    IPSEC_DIR_OUTBOUND)) &&
    635 #endif
    636 	    (rt = rtcache_validate(&tp->t_inpcb->inp_route)) != NULL &&
    637 	    (rt->rt_ifp->if_capenable & IFCAP_TSOv4) != 0;
    638 #endif /* defined(INET) */
    639 #if defined(INET6)
    640 	has_tso6 = tp->t_in6pcb != NULL &&
    641 #if defined(IPSEC)
    642 	    (!ipsec_used || IPSEC_PCB_SKIP_IPSEC(tp->t_in6pcb->in6p_sp,
    643 	    IPSEC_DIR_OUTBOUND)) &&
    644 #endif
    645 	    (rt = rtcache_validate(&tp->t_in6pcb->in6p_route)) != NULL &&
    646 	    (rt->rt_ifp->if_capenable & IFCAP_TSOv6) != 0;
    647 #endif /* defined(INET6) */
    648 	has_tso = (has_tso4 || has_tso6) && !alwaysfrag;
    649 
    650 	/*
    651 	 * Restart Window computation.  From draft-floyd-incr-init-win-03:
    652 	 *
    653 	 *	Optionally, a TCP MAY set the restart window to the
    654 	 *	minimum of the value used for the initial window and
    655 	 *	the current value of cwnd (in other words, using a
    656 	 *	larger value for the restart window should never increase
    657 	 *	the size of cwnd).
    658 	 */
    659 	if (tcp_cwm) {
    660 		/*
    661 		 * Hughes/Touch/Heidemann Congestion Window Monitoring.
    662 		 * Count the number of packets currently pending
    663 		 * acknowledgement, and limit our congestion window
    664 		 * to a pre-determined allowed burst size plus that count.
    665 		 * This prevents bursting once all pending packets have
    666 		 * been acknowledged (i.e. transmission is idle).
    667 		 *
    668 		 * XXX Link this to Initial Window?
    669 		 */
    670 		tp->snd_cwnd = min(tp->snd_cwnd,
    671 		    (tcp_cwm_burstsize * txsegsize) +
    672 		    (tp->snd_nxt - tp->snd_una));
    673 	} else {
    674 		if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) {
    675 			/*
    676 			 * We have been idle for "a while" and no acks are
    677 			 * expected to clock out any data we send --
    678 			 * slow start to get ack "clock" running again.
    679 			 */
    680 			int ss = tcp_init_win;
    681 #ifdef INET
    682 			if (tp->t_inpcb &&
    683 			    in_localaddr(tp->t_inpcb->inp_faddr))
    684 				ss = tcp_init_win_local;
    685 #endif
    686 #ifdef INET6
    687 			if (tp->t_in6pcb &&
    688 			    in6_localaddr(&tp->t_in6pcb->in6p_faddr))
    689 				ss = tcp_init_win_local;
    690 #endif
    691 			tp->snd_cwnd = min(tp->snd_cwnd,
    692 			    TCP_INITIAL_WINDOW(ss, txsegsize));
    693 		}
    694 	}
    695 
    696 	txsegsize_nosack = txsegsize;
    697 again:
    698 	ecn_tos = 0;
    699 	use_tso = has_tso;
    700 	if ((tp->t_flags & (TF_ECN_SND_CWR|TF_ECN_SND_ECE)) != 0) {
    701 		/* don't duplicate CWR/ECE. */
    702 		use_tso = 0;
    703 	}
    704 	TCP_REASS_LOCK(tp);
    705 	sack_numblks = tcp_sack_numblks(tp);
    706 	if (sack_numblks) {
    707 		int sackoptlen;
    708 
    709 		sackoptlen = TCP_SACK_OPTLEN(sack_numblks);
    710 		if (sackoptlen > txsegsize_nosack) {
    711 			sack_numblks = 0; /* give up SACK */
    712 			txsegsize = txsegsize_nosack;
    713 		} else {
    714 			if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
    715 				/* don't duplicate D-SACK. */
    716 				use_tso = 0;
    717 			}
    718 			txsegsize = txsegsize_nosack - sackoptlen;
    719 		}
    720 	} else {
    721 		txsegsize = txsegsize_nosack;
    722 	}
    723 
    724 	/*
    725 	 * Determine length of data that should be transmitted, and
    726 	 * flags that should be used.  If there is some data or critical
    727 	 * controls (SYN, RST) to send, then transmit; otherwise,
    728 	 * investigate further.
    729 	 *
    730 	 * Readjust SACK information to avoid resending duplicate data.
    731 	 */
    732 	if (TCP_SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max))
    733 		tcp_sack_adjust(tp);
    734 	sendalot = 0;
    735 	off = tp->snd_nxt - tp->snd_una;
    736 	win = min(tp->snd_wnd, tp->snd_cwnd);
    737 
    738 	flags = tcp_outflags[tp->t_state];
    739 
    740 	/*
    741 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
    742 	 * to send out new data (when sendalot is 1), bypass this function.
    743 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
    744 	 * we're replacing a (future) new transmission with a retransmission
    745 	 * now, and we previously incremented snd_cwnd in tcp_input().
    746 	 */
    747 	/*
    748 	 * Still in sack recovery , reset rxmit flag to zero.
    749 	 */
    750 	sack_rxmit = 0;
    751 	sack_bytes_rxmt = 0;
    752 	len = 0;
    753 	p = NULL;
    754 	do {
    755 		long cwin;
    756 		if (!TCP_SACK_ENABLED(tp))
    757 			break;
    758 		if (tp->t_partialacks < 0)
    759 			break;
    760 		p = tcp_sack_output(tp, &sack_bytes_rxmt);
    761 		if (p == NULL)
    762 			break;
    763 
    764 		cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
    765 		if (cwin < 0)
    766 			cwin = 0;
    767 		/* Do not retransmit SACK segments beyond snd_recover */
    768 		if (SEQ_GT(p->end, tp->snd_recover)) {
    769 			/*
    770 			 * (At least) part of sack hole extends beyond
    771 			 * snd_recover. Check to see if we can rexmit data
    772 			 * for this hole.
    773 			 */
    774 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
    775 				/*
    776 				 * Can't rexmit any more data for this hole.
    777 				 * That data will be rexmitted in the next
    778 				 * sack recovery episode, when snd_recover
    779 				 * moves past p->rxmit.
    780 				 */
    781 				p = NULL;
    782 				break;
    783 			}
    784 			/* Can rexmit part of the current hole */
    785 			len = ((long)ulmin(cwin, tp->snd_recover - p->rxmit));
    786 		} else
    787 			len = ((long)ulmin(cwin, p->end - p->rxmit));
    788 		off = p->rxmit - tp->snd_una;
    789 		if (off + len > so->so_snd.sb_cc) {
    790 			/* 1 for TH_FIN */
    791 			KASSERT(off + len == so->so_snd.sb_cc + 1);
    792 			KASSERT(p->rxmit + len == tp->snd_max);
    793 			len = so->so_snd.sb_cc - off;
    794 		}
    795 		if (len > 0) {
    796 			sack_rxmit = 1;
    797 			sendalot = 1;
    798 		}
    799 	} while (/*CONSTCOND*/0);
    800 
    801 	/*
    802 	 * If in persist timeout with window of 0, send 1 byte.
    803 	 * Otherwise, if window is small but nonzero
    804 	 * and timer expired, we will send what we can
    805 	 * and go to transmit state.
    806 	 */
    807 	if (tp->t_force) {
    808 		if (win == 0) {
    809 			/*
    810 			 * If we still have some data to send, then
    811 			 * clear the FIN bit.  Usually this would
    812 			 * happen below when it realizes that we
    813 			 * aren't sending all the data.  However,
    814 			 * if we have exactly 1 byte of unset data,
    815 			 * then it won't clear the FIN bit below,
    816 			 * and if we are in persist state, we wind
    817 			 * up sending the packet without recording
    818 			 * that we sent the FIN bit.
    819 			 *
    820 			 * We can't just blindly clear the FIN bit,
    821 			 * because if we don't have any more data
    822 			 * to send then the probe will be the FIN
    823 			 * itself.
    824 			 */
    825 			if (off < so->so_snd.sb_cc)
    826 				flags &= ~TH_FIN;
    827 			win = 1;
    828 		} else {
    829 			TCP_TIMER_DISARM(tp, TCPT_PERSIST);
    830 			tp->t_rxtshift = 0;
    831 		}
    832 	}
    833 
    834 	if (sack_rxmit == 0) {
    835 		if (TCP_SACK_ENABLED(tp) && tp->t_partialacks >= 0) {
    836 			long cwin;
    837 
    838 			/*
    839 			 * We are inside of a SACK recovery episode and are
    840 			 * sending new data, having retransmitted all the
    841 			 * data possible in the scoreboard.
    842 			 */
    843 			if (tp->snd_wnd < so->so_snd.sb_cc) {
    844 				len = tp->snd_wnd - off;
    845 				flags &= ~TH_FIN;
    846 			} else {
    847 				len = so->so_snd.sb_cc - off;
    848 			}
    849 
    850 			/*
    851 			 * From FreeBSD:
    852 			 *  Don't remove this (len > 0) check !
    853 			 *  We explicitly check for len > 0 here (although it
    854 			 *  isn't really necessary), to work around a gcc
    855 			 *  optimization issue - to force gcc to compute
    856 			 *  len above. Without this check, the computation
    857 			 *  of len is bungled by the optimizer.
    858 			 */
    859 			if (len > 0) {
    860 				cwin = tp->snd_cwnd -
    861 				    (tp->snd_nxt - tp->sack_newdata) -
    862 				    sack_bytes_rxmt;
    863 				if (cwin < 0)
    864 					cwin = 0;
    865 				if (cwin < len) {
    866 					len = cwin;
    867 					flags &= ~TH_FIN;
    868 				}
    869 			}
    870 		} else if (win < so->so_snd.sb_cc) {
    871 			len = win - off;
    872 			flags &= ~TH_FIN;
    873 		} else {
    874 			len = so->so_snd.sb_cc - off;
    875 		}
    876 	}
    877 
    878 	if (len < 0) {
    879 		/*
    880 		 * If FIN has been sent but not acked,
    881 		 * but we haven't been called to retransmit,
    882 		 * len will be -1.  Otherwise, window shrank
    883 		 * after we sent into it.  If window shrank to 0,
    884 		 * cancel pending retransmit, pull snd_nxt back
    885 		 * to (closed) window, and set the persist timer
    886 		 * if it isn't already going.  If the window didn't
    887 		 * close completely, just wait for an ACK.
    888 		 *
    889 		 * If we have a pending FIN, either it has already been
    890 		 * transmitted or it is outside the window, so drop it.
    891 		 * If the FIN has been transmitted, but this is not a
    892 		 * retransmission, then len must be -1.  Therefore we also
    893 		 * prevent here the sending of `gratuitous FINs'.  This
    894 		 * eliminates the need to check for that case below (e.g.
    895 		 * to back up snd_nxt before the FIN so that the sequence
    896 		 * number is correct).
    897 		 */
    898 		len = 0;
    899 		flags &= ~TH_FIN;
    900 		if (win == 0) {
    901 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
    902 			tp->t_rxtshift = 0;
    903 			tp->snd_nxt = tp->snd_una;
    904 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
    905 				tcp_setpersist(tp);
    906 		}
    907 	}
    908 
    909 	/*
    910 	 * Automatic sizing enables the performance of large buffers
    911 	 * and most of the efficiency of small ones by only allocating
    912 	 * space when it is needed.
    913 	 *
    914 	 * The criteria to step up the send buffer one notch are:
    915 	 *  1. receive window of remote host is larger than send buffer
    916 	 *     (with a fudge factor of 5/4th);
    917 	 *  2. send buffer is filled to 7/8th with data (so we actually
    918 	 *     have data to make use of it);
    919 	 *  3. send buffer fill has not hit maximal automatic size;
    920 	 *  4. our send window (slow start and cogestion controlled) is
    921 	 *     larger than sent but unacknowledged data in send buffer.
    922 	 *
    923 	 * The remote host receive window scaling factor may limit the
    924 	 * growing of the send buffer before it reaches its allowed
    925 	 * maximum.
    926 	 *
    927 	 * It scales directly with slow start or congestion window
    928 	 * and does at most one step per received ACK.  This fast
    929 	 * scaling has the drawback of growing the send buffer beyond
    930 	 * what is strictly necessary to make full use of a given
    931 	 * delay*bandwith product.  However testing has shown this not
    932 	 * to be much of an problem.  At worst we are trading wasting
    933 	 * of available bandwith (the non-use of it) for wasting some
    934 	 * socket buffer memory.
    935 	 *
    936 	 * TODO: Shrink send buffer during idle periods together
    937 	 * with congestion window.  Requires another timer.
    938 	 */
    939 	if (tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
    940 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
    941 		    so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
    942 		    so->so_snd.sb_cc < tcp_autosndbuf_max &&
    943 		    win >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
    944 			if (!sbreserve(&so->so_snd,
    945 			    min(so->so_snd.sb_hiwat + tcp_autosndbuf_inc,
    946 			     tcp_autosndbuf_max), so))
    947 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
    948 		}
    949 	}
    950 
    951 	if (len > txsegsize) {
    952 		if (use_tso) {
    953 			/*
    954 			 * Truncate TSO transfers to IP_MAXPACKET, and make
    955 			 * sure that we send equal size transfers down the
    956 			 * stack (rather than big-small-big-small-...).
    957 			 */
    958 #ifdef INET6
    959 			CTASSERT(IPV6_MAXPACKET == IP_MAXPACKET);
    960 #endif
    961 			len = (min(len, IP_MAXPACKET) / txsegsize) * txsegsize;
    962 			if (len <= txsegsize) {
    963 				use_tso = 0;
    964 			}
    965 		} else
    966 			len = txsegsize;
    967 		flags &= ~TH_FIN;
    968 		sendalot = 1;
    969 	} else
    970 		use_tso = 0;
    971 	if (sack_rxmit) {
    972 		if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
    973 			flags &= ~TH_FIN;
    974 	}
    975 
    976 	win = sbspace(&so->so_rcv);
    977 
    978 	/*
    979 	 * Sender silly window avoidance.  If connection is idle
    980 	 * and can send all data, a maximum segment,
    981 	 * at least a maximum default-size segment do it,
    982 	 * or are forced, do it; otherwise don't bother.
    983 	 * If peer's buffer is tiny, then send
    984 	 * when window is at least half open.
    985 	 * If retransmitting (possibly after persist timer forced us
    986 	 * to send into a small window), then must resend.
    987 	 */
    988 	if (len) {
    989 		if (len >= txsegsize)
    990 			goto send;
    991 		if ((so->so_state & SS_MORETOCOME) == 0 &&
    992 		    ((idle || tp->t_flags & TF_NODELAY) &&
    993 		     len + off >= so->so_snd.sb_cc))
    994 			goto send;
    995 		if (tp->t_force)
    996 			goto send;
    997 		if (len >= tp->max_sndwnd / 2)
    998 			goto send;
    999 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
   1000 			goto send;
   1001 		if (sack_rxmit)
   1002 			goto send;
   1003 	}
   1004 
   1005 	/*
   1006 	 * Compare available window to amount of window known to peer
   1007 	 * (as advertised window less next expected input).  If the
   1008 	 * difference is at least twice the size of the largest segment
   1009 	 * we expect to receive (i.e. two segments) or at least 50% of
   1010 	 * the maximum possible window, then want to send a window update
   1011 	 * to peer.
   1012 	 */
   1013 	if (win > 0) {
   1014 		/*
   1015 		 * "adv" is the amount we can increase the window,
   1016 		 * taking into account that we are limited by
   1017 		 * TCP_MAXWIN << tp->rcv_scale.
   1018 		 */
   1019 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
   1020 			(tp->rcv_adv - tp->rcv_nxt);
   1021 
   1022 		if (adv >= (long) (2 * rxsegsize))
   1023 			goto send;
   1024 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
   1025 			goto send;
   1026 	}
   1027 
   1028 	/*
   1029 	 * Send if we owe peer an ACK.
   1030 	 */
   1031 	if (tp->t_flags & TF_ACKNOW)
   1032 		goto send;
   1033 	if (flags & (TH_SYN|TH_FIN|TH_RST))
   1034 		goto send;
   1035 	if (SEQ_GT(tp->snd_up, tp->snd_una))
   1036 		goto send;
   1037 	/*
   1038 	 * In SACK, it is possible for tcp_output to fail to send a segment
   1039 	 * after the retransmission timer has been turned off.  Make sure
   1040 	 * that the retransmission timer is set.
   1041 	 */
   1042 	if (TCP_SACK_ENABLED(tp) && SEQ_GT(tp->snd_max, tp->snd_una) &&
   1043 	    !TCP_TIMER_ISARMED(tp, TCPT_REXMT) &&
   1044 	    !TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
   1045 		TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   1046 		goto just_return;
   1047 	}
   1048 
   1049 	/*
   1050 	 * TCP window updates are not reliable, rather a polling protocol
   1051 	 * using ``persist'' packets is used to insure receipt of window
   1052 	 * updates.  The three ``states'' for the output side are:
   1053 	 *	idle			not doing retransmits or persists
   1054 	 *	persisting		to move a small or zero window
   1055 	 *	(re)transmitting	and thereby not persisting
   1056 	 *
   1057 	 * tp->t_timer[TCPT_PERSIST]
   1058 	 *	is set when we are in persist state.
   1059 	 * tp->t_force
   1060 	 *	is set when we are called to send a persist packet.
   1061 	 * tp->t_timer[TCPT_REXMT]
   1062 	 *	is set when we are retransmitting
   1063 	 * The output side is idle when both timers are zero.
   1064 	 *
   1065 	 * If send window is too small, there is data to transmit, and no
   1066 	 * retransmit or persist is pending, then go to persist state.
   1067 	 * If nothing happens soon, send when timer expires:
   1068 	 * if window is nonzero, transmit what we can,
   1069 	 * otherwise force out a byte.
   1070 	 */
   1071 	if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
   1072 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
   1073 		tp->t_rxtshift = 0;
   1074 		tcp_setpersist(tp);
   1075 	}
   1076 
   1077 	/*
   1078 	 * No reason to send a segment, just return.
   1079 	 */
   1080 just_return:
   1081 	TCP_REASS_UNLOCK(tp);
   1082 	return (0);
   1083 
   1084 send:
   1085 	/*
   1086 	 * Before ESTABLISHED, force sending of initial options
   1087 	 * unless TCP set not to do any options.
   1088 	 * NOTE: we assume that the IP/TCP header plus TCP options
   1089 	 * always fit in a single mbuf, leaving room for a maximum
   1090 	 * link header, i.e.
   1091 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
   1092 	 */
   1093 	optlen = 0;
   1094 	switch (af) {
   1095 #ifdef INET
   1096 	case AF_INET:
   1097 		iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
   1098 		break;
   1099 #endif
   1100 #ifdef INET6
   1101 	case AF_INET6:
   1102 		iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
   1103 		break;
   1104 #endif
   1105 	default:	/*pacify gcc*/
   1106 		iphdrlen = 0;
   1107 		break;
   1108 	}
   1109 	hdrlen = iphdrlen;
   1110 	if (flags & TH_SYN) {
   1111 		struct rtentry *synrt;
   1112 
   1113 		synrt = NULL;
   1114 #ifdef INET
   1115 		if (tp->t_inpcb)
   1116 			synrt = in_pcbrtentry(tp->t_inpcb);
   1117 #endif
   1118 #ifdef INET6
   1119 		if (tp->t_in6pcb)
   1120 			synrt = in6_pcbrtentry(tp->t_in6pcb);
   1121 #endif
   1122 
   1123 		tp->snd_nxt = tp->iss;
   1124 		tp->t_ourmss = tcp_mss_to_advertise(synrt != NULL ?
   1125 						    synrt->rt_ifp : NULL, af);
   1126 		if ((tp->t_flags & TF_NOOPT) == 0 && OPT_FITS(4)) {
   1127 			opt[0] = TCPOPT_MAXSEG;
   1128 			opt[1] = 4;
   1129 			opt[2] = (tp->t_ourmss >> 8) & 0xff;
   1130 			opt[3] = tp->t_ourmss & 0xff;
   1131 			optlen = 4;
   1132 
   1133 			if ((tp->t_flags & TF_REQ_SCALE) &&
   1134 			    ((flags & TH_ACK) == 0 ||
   1135 			    (tp->t_flags & TF_RCVD_SCALE)) &&
   1136 			    OPT_FITS(4)) {
   1137 				*((u_int32_t *) (opt + optlen)) = htonl(
   1138 					TCPOPT_NOP << 24 |
   1139 					TCPOPT_WINDOW << 16 |
   1140 					TCPOLEN_WINDOW << 8 |
   1141 					tp->request_r_scale);
   1142 				optlen += 4;
   1143 			}
   1144 			if (tcp_do_sack && OPT_FITS(4)) {
   1145 				u_int8_t *cp = (u_int8_t *)(opt + optlen);
   1146 
   1147 				cp[0] = TCPOPT_SACK_PERMITTED;
   1148 				cp[1] = 2;
   1149 				cp[2] = TCPOPT_NOP;
   1150 				cp[3] = TCPOPT_NOP;
   1151 				optlen += 4;
   1152 			}
   1153 		}
   1154 	}
   1155 
   1156 	/*
   1157 	 * Send a timestamp and echo-reply if this is a SYN and our side
   1158 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
   1159 	 * and our peer have sent timestamps in our SYN's.
   1160 	 */
   1161 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
   1162 	     (flags & TH_RST) == 0 &&
   1163 	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
   1164 	     (tp->t_flags & TF_RCVD_TSTMP)) && OPT_FITS(TCPOLEN_TSTAMP_APPA)) {
   1165 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
   1166 
   1167 		/* Form timestamp option as shown in appendix A of RFC 1323. */
   1168 		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
   1169 		*lp++ = htonl(TCP_TIMESTAMP(tp));
   1170 		*lp   = htonl(tp->ts_recent);
   1171 		optlen += TCPOLEN_TSTAMP_APPA;
   1172 
   1173 		/* Set receive buffer autosizing timestamp. */
   1174 		if (tp->rfbuf_ts == 0 && (so->so_rcv.sb_flags & SB_AUTOSIZE))
   1175 			tp->rfbuf_ts = TCP_TIMESTAMP(tp);
   1176 	}
   1177 
   1178 	/*
   1179 	 * Tack on the SACK block if it is necessary.
   1180 	 */
   1181 	if (sack_numblks) {
   1182 		int sack_len;
   1183 		u_char *bp = (u_char *)(opt + optlen);
   1184 		u_int32_t *lp = (u_int32_t *)(bp + 4);
   1185 		struct ipqent *tiqe;
   1186 
   1187 		sack_len = sack_numblks * 8 + 2;
   1188 		if (OPT_FITS(sack_len + 2)) {
   1189 			bp[0] = TCPOPT_NOP;
   1190 			bp[1] = TCPOPT_NOP;
   1191 			bp[2] = TCPOPT_SACK;
   1192 			bp[3] = sack_len;
   1193 			if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
   1194 				sack_numblks--;
   1195 				*lp++ = htonl(tp->rcv_dsack_block.left);
   1196 				*lp++ = htonl(tp->rcv_dsack_block.right);
   1197 				tp->rcv_sack_flags &= ~TCPSACK_HAVED;
   1198 			}
   1199 			for (tiqe = TAILQ_FIRST(&tp->timeq);
   1200 			    sack_numblks > 0;
   1201 			    tiqe = TAILQ_NEXT(tiqe, ipqe_timeq)) {
   1202 				KASSERT(tiqe != NULL);
   1203 				sack_numblks--;
   1204 				*lp++ = htonl(tiqe->ipqe_seq);
   1205 				*lp++ = htonl(tiqe->ipqe_seq + tiqe->ipqe_len +
   1206 				    ((tiqe->ipqe_flags & TH_FIN) != 0 ? 1 : 0));
   1207 			}
   1208 			optlen += sack_len + 2;
   1209 		}
   1210 	}
   1211 	TCP_REASS_UNLOCK(tp);
   1212 
   1213 #ifdef TCP_SIGNATURE
   1214 	if ((tp->t_flags & TF_SIGNATURE) && OPT_FITS(TCPOLEN_SIGNATURE + 2)) {
   1215 		u_char *bp;
   1216 		/*
   1217 		 * Initialize TCP-MD5 option (RFC2385)
   1218 		 */
   1219 		bp = (u_char *)opt + optlen;
   1220 		*bp++ = TCPOPT_SIGNATURE;
   1221 		*bp++ = TCPOLEN_SIGNATURE;
   1222 		sigoff = optlen + 2;
   1223 		memset(bp, 0, TCP_SIGLEN);
   1224 		bp += TCP_SIGLEN;
   1225 		optlen += TCPOLEN_SIGNATURE;
   1226 		/*
   1227 		 * Terminate options list and maintain 32-bit alignment.
   1228  		 */
   1229 		*bp++ = TCPOPT_NOP;
   1230 		*bp++ = TCPOPT_EOL;
   1231  		optlen += 2;
   1232  	}
   1233 #endif /* TCP_SIGNATURE */
   1234 
   1235 	hdrlen += optlen;
   1236 
   1237 #ifdef DIAGNOSTIC
   1238 	if (!use_tso && len > txsegsize)
   1239 		panic("tcp data to be sent is larger than segment");
   1240 	else if (use_tso && len > IP_MAXPACKET)
   1241 		panic("tcp data to be sent is larger than max TSO size");
   1242 	if (max_linkhdr + hdrlen > MCLBYTES)
   1243 		panic("tcphdr too big");
   1244 #endif
   1245 
   1246 	/*
   1247 	 * Grab a header mbuf, attaching a copy of data to
   1248 	 * be transmitted, and initialize the header from
   1249 	 * the template for sends on this connection.
   1250 	 */
   1251 	if (len) {
   1252 		error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
   1253 		if (error)
   1254 			goto out;
   1255 		/*
   1256 		 * If we're sending everything we've got, set PUSH.
   1257 		 * (This will keep happy those implementations which only
   1258 		 * give data to the user when a buffer fills or
   1259 		 * a PUSH comes in.)
   1260 		 */
   1261 		if (off + len == so->so_snd.sb_cc)
   1262 			flags |= TH_PUSH;
   1263 	} else {
   1264 		tcps = TCP_STAT_GETREF();
   1265 		if (tp->t_flags & TF_ACKNOW)
   1266 			tcps[TCP_STAT_SNDACKS]++;
   1267 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
   1268 			tcps[TCP_STAT_SNDCTRL]++;
   1269 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
   1270 			tcps[TCP_STAT_SNDURG]++;
   1271 		else
   1272 			tcps[TCP_STAT_SNDWINUP]++;
   1273 		TCP_STAT_PUTREF();
   1274 
   1275 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
   1276 		if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
   1277 			MCLGET(m, M_DONTWAIT);
   1278 			if ((m->m_flags & M_EXT) == 0) {
   1279 				m_freem(m);
   1280 				m = NULL;
   1281 			}
   1282 		}
   1283 		if (m == NULL) {
   1284 			error = ENOBUFS;
   1285 			goto out;
   1286 		}
   1287 		MCLAIM(m, &tcp_tx_mowner);
   1288 		m->m_data += max_linkhdr;
   1289 		m->m_len = hdrlen;
   1290 	}
   1291 	m->m_pkthdr.rcvif = NULL;
   1292 	switch (af) {
   1293 #ifdef INET
   1294 	case AF_INET:
   1295 		ip = mtod(m, struct ip *);
   1296 #ifdef INET6
   1297 		ip6 = NULL;
   1298 #endif
   1299 		th = (struct tcphdr *)(ip + 1);
   1300 		break;
   1301 #endif
   1302 #ifdef INET6
   1303 	case AF_INET6:
   1304 		ip = NULL;
   1305 		ip6 = mtod(m, struct ip6_hdr *);
   1306 		th = (struct tcphdr *)(ip6 + 1);
   1307 		break;
   1308 #endif
   1309 	default:	/*pacify gcc*/
   1310 		ip = NULL;
   1311 #ifdef INET6
   1312 		ip6 = NULL;
   1313 #endif
   1314 		th = NULL;
   1315 		break;
   1316 	}
   1317 	if (tp->t_template == 0)
   1318 		panic("tcp_output");
   1319 	if (tp->t_template->m_len < iphdrlen)
   1320 		panic("tcp_output");
   1321 	bcopy(mtod(tp->t_template, void *), mtod(m, void *), iphdrlen);
   1322 
   1323 	/*
   1324 	 * If we are starting a connection, send ECN setup
   1325 	 * SYN packet. If we are on a retransmit, we may
   1326 	 * resend those bits a number of times as per
   1327 	 * RFC 3168.
   1328 	 */
   1329 	if (tp->t_state == TCPS_SYN_SENT && tcp_do_ecn) {
   1330 		if (tp->t_flags & TF_SYN_REXMT) {
   1331 			if (tp->t_ecn_retries--)
   1332 				flags |= TH_ECE|TH_CWR;
   1333 		} else {
   1334 			flags |= TH_ECE|TH_CWR;
   1335 			tp->t_ecn_retries = tcp_ecn_maxretries;
   1336 		}
   1337 	}
   1338 
   1339 	if (TCP_ECN_ALLOWED(tp)) {
   1340 		/*
   1341 		 * If the peer has ECN, mark data packets
   1342 		 * ECN capable. Ignore pure ack packets, retransmissions
   1343 		 * and window probes.
   1344 		 */
   1345 		if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
   1346 		    !(tp->t_force && len == 1)) {
   1347 			ecn_tos = IPTOS_ECN_ECT0;
   1348 			TCP_STATINC(TCP_STAT_ECN_ECT);
   1349 		}
   1350 
   1351 		/*
   1352 		 * Reply with proper ECN notifications.
   1353 		 */
   1354 		if (tp->t_flags & TF_ECN_SND_CWR) {
   1355 			flags |= TH_CWR;
   1356 			tp->t_flags &= ~TF_ECN_SND_CWR;
   1357 		}
   1358 		if (tp->t_flags & TF_ECN_SND_ECE) {
   1359 			flags |= TH_ECE;
   1360 		}
   1361 	}
   1362 
   1363 
   1364 	/*
   1365 	 * If we are doing retransmissions, then snd_nxt will
   1366 	 * not reflect the first unsent octet.  For ACK only
   1367 	 * packets, we do not want the sequence number of the
   1368 	 * retransmitted packet, we want the sequence number
   1369 	 * of the next unsent octet.  So, if there is no data
   1370 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
   1371 	 * when filling in ti_seq.  But if we are in persist
   1372 	 * state, snd_max might reflect one byte beyond the
   1373 	 * right edge of the window, so use snd_nxt in that
   1374 	 * case, since we know we aren't doing a retransmission.
   1375 	 * (retransmit and persist are mutually exclusive...)
   1376 	 */
   1377 	if (TCP_SACK_ENABLED(tp) && sack_rxmit) {
   1378 		th->th_seq = htonl(p->rxmit);
   1379 		p->rxmit += len;
   1380 	} else {
   1381 		if (len || (flags & (TH_SYN|TH_FIN)) ||
   1382 		    TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
   1383 			th->th_seq = htonl(tp->snd_nxt);
   1384 		else
   1385 			th->th_seq = htonl(tp->snd_max);
   1386 	}
   1387 	th->th_ack = htonl(tp->rcv_nxt);
   1388 	if (optlen) {
   1389 		bcopy((void *)opt, (void *)(th + 1), optlen);
   1390 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
   1391 	}
   1392 	th->th_flags = flags;
   1393 	/*
   1394 	 * Calculate receive window.  Don't shrink window,
   1395 	 * but avoid silly window syndrome.
   1396 	 */
   1397 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
   1398 		win = 0;
   1399 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
   1400 		win = (long)TCP_MAXWIN << tp->rcv_scale;
   1401 	if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt))
   1402 		win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt);
   1403 	th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
   1404 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
   1405 		u_int32_t urp = tp->snd_up - tp->snd_nxt;
   1406 		if (urp > IP_MAXPACKET)
   1407 			urp = IP_MAXPACKET;
   1408 		th->th_urp = htons((u_int16_t)urp);
   1409 		th->th_flags |= TH_URG;
   1410 	} else
   1411 		/*
   1412 		 * If no urgent pointer to send, then we pull
   1413 		 * the urgent pointer to the left edge of the send window
   1414 		 * so that it doesn't drift into the send window on sequence
   1415 		 * number wraparound.
   1416 		 */
   1417 		tp->snd_up = tp->snd_una;		/* drag it along */
   1418 
   1419 #ifdef TCP_SIGNATURE
   1420 	if (sigoff && (tp->t_flags & TF_SIGNATURE)) {
   1421 		struct secasvar *sav;
   1422 		u_int8_t *sigp;
   1423 
   1424 		sav = tcp_signature_getsav(m, th);
   1425 
   1426 		if (sav == NULL) {
   1427 			if (m)
   1428 				m_freem(m);
   1429 			return (EPERM);
   1430 		}
   1431 
   1432 		m->m_pkthdr.len = hdrlen + len;
   1433 		sigp = (char *)th + sizeof(*th) + sigoff;
   1434 		tcp_signature(m, th, (char *)th - mtod(m, char *), sav, sigp);
   1435 
   1436 		key_sa_recordxfer(sav, m);
   1437 		KEY_FREESAV(&sav);
   1438 	}
   1439 #endif
   1440 
   1441 	/*
   1442 	 * Set ourselves up to be checksummed just before the packet
   1443 	 * hits the wire.
   1444 	 */
   1445 	switch (af) {
   1446 #ifdef INET
   1447 	case AF_INET:
   1448 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
   1449 		if (use_tso) {
   1450 			m->m_pkthdr.segsz = txsegsize;
   1451 			m->m_pkthdr.csum_flags = M_CSUM_TSOv4;
   1452 		} else {
   1453 			m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
   1454 			if (len + optlen) {
   1455 				/* Fixup the pseudo-header checksum. */
   1456 				/* XXXJRT Not IP Jumbogram safe. */
   1457 				th->th_sum = in_cksum_addword(th->th_sum,
   1458 				    htons((u_int16_t) (len + optlen)));
   1459 			}
   1460 		}
   1461 		break;
   1462 #endif
   1463 #ifdef INET6
   1464 	case AF_INET6:
   1465 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
   1466 		if (use_tso) {
   1467 			m->m_pkthdr.segsz = txsegsize;
   1468 			m->m_pkthdr.csum_flags = M_CSUM_TSOv6;
   1469 		} else {
   1470 			m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
   1471 			if (len + optlen) {
   1472 				/* Fixup the pseudo-header checksum. */
   1473 				/* XXXJRT: Not IPv6 Jumbogram safe. */
   1474 				th->th_sum = in_cksum_addword(th->th_sum,
   1475 				    htons((u_int16_t) (len + optlen)));
   1476 			}
   1477 		}
   1478 		break;
   1479 #endif
   1480 	}
   1481 
   1482 	/*
   1483 	 * In transmit state, time the transmission and arrange for
   1484 	 * the retransmit.  In persist state, just set snd_max.
   1485 	 */
   1486 	if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
   1487 		tcp_seq startseq = tp->snd_nxt;
   1488 
   1489 		/*
   1490 		 * Advance snd_nxt over sequence space of this segment.
   1491 		 * There are no states in which we send both a SYN and a FIN,
   1492 		 * so we collapse the tests for these flags.
   1493 		 */
   1494 		if (flags & (TH_SYN|TH_FIN))
   1495 			tp->snd_nxt++;
   1496 		if (sack_rxmit)
   1497 			goto timer;
   1498 		tp->snd_nxt += len;
   1499 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
   1500 			tp->snd_max = tp->snd_nxt;
   1501 			/*
   1502 			 * Time this transmission if not a retransmission and
   1503 			 * not currently timing anything.
   1504 			 */
   1505 			if (tp->t_rtttime == 0) {
   1506 				tp->t_rtttime = tcp_now;
   1507 				tp->t_rtseq = startseq;
   1508 				TCP_STATINC(TCP_STAT_SEGSTIMED);
   1509 			}
   1510 		}
   1511 
   1512 		/*
   1513 		 * Set retransmit timer if not currently set,
   1514 		 * and not doing an ack or a keep-alive probe.
   1515 		 * Initial value for retransmit timer is smoothed
   1516 		 * round-trip time + 2 * round-trip time variance.
   1517 		 * Initialize shift counter which is used for backoff
   1518 		 * of retransmit time.
   1519 		 */
   1520 timer:
   1521 		if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
   1522 			((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
   1523 		    tp->snd_nxt != tp->snd_una)) {
   1524 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
   1525 				TCP_TIMER_DISARM(tp, TCPT_PERSIST);
   1526 				tp->t_rxtshift = 0;
   1527 			}
   1528 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   1529 		}
   1530 	} else
   1531 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
   1532 			tp->snd_max = tp->snd_nxt + len;
   1533 
   1534 #ifdef TCP_DEBUG
   1535 	/*
   1536 	 * Trace.
   1537 	 */
   1538 	if (so->so_options & SO_DEBUG)
   1539 		tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
   1540 #endif
   1541 
   1542 	/*
   1543 	 * Fill in IP length and desired time to live and
   1544 	 * send to IP level.  There should be a better way
   1545 	 * to handle ttl and tos; we could keep them in
   1546 	 * the template, but need a way to checksum without them.
   1547 	 */
   1548 	m->m_pkthdr.len = hdrlen + len;
   1549 
   1550 	switch (af) {
   1551 #ifdef INET
   1552 	case AF_INET:
   1553 		ip->ip_len = htons(m->m_pkthdr.len);
   1554 		packetlen = m->m_pkthdr.len;
   1555 		if (tp->t_inpcb) {
   1556 			ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
   1557 			ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos | ecn_tos;
   1558 		}
   1559 #ifdef INET6
   1560 		else if (tp->t_in6pcb) {
   1561 			ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
   1562 			ip->ip_tos = ecn_tos;	/*XXX*/
   1563 		}
   1564 #endif
   1565 		break;
   1566 #endif
   1567 #ifdef INET6
   1568 	case AF_INET6:
   1569 		packetlen = m->m_pkthdr.len;
   1570 		ip6->ip6_nxt = IPPROTO_TCP;
   1571 		if (tp->t_in6pcb) {
   1572 			/*
   1573 			 * we separately set hoplimit for every segment, since
   1574 			 * the user might want to change the value via
   1575 			 * setsockopt. Also, desired default hop limit might
   1576 			 * be changed via Neighbor Discovery.
   1577 			 */
   1578 			ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
   1579 				(rt = rtcache_validate(ro)) != NULL ? rt->rt_ifp
   1580 				                                    : NULL);
   1581 		}
   1582 		ip6->ip6_flow |= htonl(ecn_tos << 20);
   1583 		/* ip6->ip6_flow = ??? (from template) */
   1584 		/* ip6_plen will be filled in ip6_output(). */
   1585 		break;
   1586 #endif
   1587 	default:	/*pacify gcc*/
   1588 		packetlen = 0;
   1589 		break;
   1590 	}
   1591 
   1592 	switch (af) {
   1593 #ifdef INET
   1594 	case AF_INET:
   1595 	    {
   1596 		struct mbuf *opts;
   1597 
   1598 		if (tp->t_inpcb)
   1599 			opts = tp->t_inpcb->inp_options;
   1600 		else
   1601 			opts = NULL;
   1602 		error = ip_output(m, opts, ro,
   1603 			(tp->t_mtudisc ? IP_MTUDISC : 0) |
   1604 			(so->so_options & SO_DONTROUTE), NULL, so);
   1605 		break;
   1606 	    }
   1607 #endif
   1608 #ifdef INET6
   1609 	case AF_INET6:
   1610 	    {
   1611 		struct ip6_pktopts *opts;
   1612 
   1613 		if (tp->t_in6pcb)
   1614 			opts = tp->t_in6pcb->in6p_outputopts;
   1615 		else
   1616 			opts = NULL;
   1617 		error = ip6_output(m, opts, ro, so->so_options & SO_DONTROUTE,
   1618 			NULL, so, NULL);
   1619 		break;
   1620 	    }
   1621 #endif
   1622 	default:
   1623 		error = EAFNOSUPPORT;
   1624 		break;
   1625 	}
   1626 	if (error) {
   1627 out:
   1628 		if (error == ENOBUFS) {
   1629 			TCP_STATINC(TCP_STAT_SELFQUENCH);
   1630 #ifdef INET
   1631 			if (tp->t_inpcb)
   1632 				tcp_quench(tp->t_inpcb, 0);
   1633 #endif
   1634 #ifdef INET6
   1635 			if (tp->t_in6pcb)
   1636 				tcp6_quench(tp->t_in6pcb, 0);
   1637 #endif
   1638 			error = 0;
   1639 		} else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
   1640 		    TCPS_HAVERCVDSYN(tp->t_state)) {
   1641 			tp->t_softerror = error;
   1642 			error = 0;
   1643 		}
   1644 
   1645 		/* Back out the seqence number advance. */
   1646 		if (sack_rxmit)
   1647 			p->rxmit -= len;
   1648 
   1649 		/* Restart the delayed ACK timer, if necessary. */
   1650 		if (tp->t_flags & TF_DELACK)
   1651 			TCP_RESTART_DELACK(tp);
   1652 
   1653 		return (error);
   1654 	}
   1655 
   1656 	if (packetlen > tp->t_pmtud_mtu_sent)
   1657 		tp->t_pmtud_mtu_sent = packetlen;
   1658 
   1659 	tcps = TCP_STAT_GETREF();
   1660 	tcps[TCP_STAT_SNDTOTAL]++;
   1661 	if (tp->t_flags & TF_DELACK)
   1662 		tcps[TCP_STAT_DELACK]++;
   1663 	TCP_STAT_PUTREF();
   1664 
   1665 	/*
   1666 	 * Data sent (as far as we can tell).
   1667 	 * If this advertises a larger window than any other segment,
   1668 	 * then remember the size of the advertised window.
   1669 	 * Any pending ACK has now been sent.
   1670 	 */
   1671 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
   1672 		tp->rcv_adv = tp->rcv_nxt + win;
   1673 	tp->last_ack_sent = tp->rcv_nxt;
   1674 	tp->t_flags &= ~TF_ACKNOW;
   1675 	TCP_CLEAR_DELACK(tp);
   1676 #ifdef DIAGNOSTIC
   1677 	if (maxburst < 0)
   1678 		printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
   1679 #endif
   1680 	if (sendalot && (tp->t_congctl == &tcp_reno_ctl || --maxburst))
   1681 		goto again;
   1682 	return (0);
   1683 }
   1684 
   1685 void
   1686 tcp_setpersist(struct tcpcb *tp)
   1687 {
   1688 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
   1689 	int nticks;
   1690 
   1691 	if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
   1692 		panic("tcp_output REXMT");
   1693 	/*
   1694 	 * Start/restart persistance timer.
   1695 	 */
   1696 	if (t < tp->t_rttmin)
   1697 		t = tp->t_rttmin;
   1698 	TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
   1699 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
   1700 	TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
   1701 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
   1702 		tp->t_rxtshift++;
   1703 }
   1704