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