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