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