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