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