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