Home | History | Annotate | Line # | Download | only in netinet
tcp_output.c revision 1.30
      1 /*	$NetBSD: tcp_output.c,v 1.30 1998/03/19 22:29:34 kml Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
      9  * Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)tcp_output.c	8.4 (Berkeley) 5/24/95
     73  */
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/malloc.h>
     78 #include <sys/mbuf.h>
     79 #include <sys/protosw.h>
     80 #include <sys/socket.h>
     81 #include <sys/socketvar.h>
     82 #include <sys/errno.h>
     83 
     84 #include <net/if.h>
     85 #include <net/route.h>
     86 
     87 #include <netinet/in.h>
     88 #include <netinet/in_systm.h>
     89 #include <netinet/ip.h>
     90 #include <netinet/in_pcb.h>
     91 #include <netinet/ip_var.h>
     92 #include <netinet/tcp.h>
     93 #define	TCPOUTFLAGS
     94 #include <netinet/tcp_fsm.h>
     95 #include <netinet/tcp_seq.h>
     96 #include <netinet/tcp_timer.h>
     97 #include <netinet/tcp_var.h>
     98 #include <netinet/tcpip.h>
     99 #include <netinet/tcp_debug.h>
    100 
    101 #ifdef TUBA
    102 #include <netiso/iso.h>
    103 #include <netiso/tuba_table.h>
    104 #endif
    105 
    106 #ifdef notyet
    107 extern struct mbuf *m_copypack();
    108 #endif
    109 
    110 #define MAX_TCPOPTLEN	32	/* max # bytes that go in options */
    111 
    112 static __inline void tcp_segsize __P((struct tcpcb *, int *, int *));
    113 static __inline void
    114 tcp_segsize(tp, txsegsizep, rxsegsizep)
    115 	struct tcpcb *tp;
    116 	int *txsegsizep, *rxsegsizep;
    117 {
    118 	struct inpcb *inp = tp->t_inpcb;
    119 	struct rtentry *rt;
    120 	struct ifnet *ifp;
    121 	int size;
    122 
    123 	if ((rt = in_pcbrtentry(inp)) == NULL) {
    124 		size = tcp_mssdflt;
    125 		goto out;
    126 	}
    127 
    128 	ifp = rt->rt_ifp;
    129 
    130 	if (rt->rt_rmx.rmx_mtu != 0)
    131 		size = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
    132 	else if (ip_mtudisc || in_localaddr(inp->inp_faddr) ||
    133 		 ifp->if_flags & IFF_LOOPBACK)
    134 		size = ifp->if_mtu - sizeof(struct tcpiphdr);
    135 	else
    136 		size = tcp_mssdflt;
    137 	size -= tcp_optlen(tp);
    138 
    139  out:
    140 	*txsegsizep = min(tp->t_peermss, size);
    141 	*rxsegsizep = min(tp->t_ourmss, size);
    142 
    143 	if (*txsegsizep != tp->t_segsz) {
    144 	        /*
    145 		 * XXX:  should we check to make sure these are all
    146 		 *       at least txsegsize in length, now?
    147 		 */
    148 		tp->snd_cwnd = (tp->snd_cwnd / tp->t_segsz) * *txsegsizep;
    149 		tp->snd_ssthresh = (tp->snd_ssthresh / tp->t_segsz) *
    150 		    *txsegsizep;
    151 		tp->t_segsz = *txsegsizep;
    152 	}
    153 }
    154 
    155 /*
    156  * Tcp output routine: figure out what should be sent and send it.
    157  */
    158 int
    159 tcp_output(tp)
    160 	register struct tcpcb *tp;
    161 {
    162 	register struct socket *so = tp->t_inpcb->inp_socket;
    163 	register long len, win;
    164 	int off, flags, error;
    165 	register struct mbuf *m;
    166 	register struct tcpiphdr *ti;
    167 	u_char opt[MAX_TCPOPTLEN];
    168 	unsigned optlen, hdrlen;
    169 	int idle, sendalot, txsegsize, rxsegsize;
    170 
    171 	tcp_segsize(tp, &txsegsize, &rxsegsize);
    172 
    173 	/*
    174 	 * Determine length of data that should be transmitted,
    175 	 * and flags that will be used.
    176 	 * If there is some data or critical controls (SYN, RST)
    177 	 * to send, then transmit; otherwise, investigate further.
    178 	 */
    179 	idle = (tp->snd_max == tp->snd_una);
    180 	if (idle && tp->t_idle >= tp->t_rxtcur)
    181 		/*
    182 		 * We have been idle for "a while" and no acks are
    183 		 * expected to clock out any data we send --
    184 		 * slow start to get ack "clock" running again.
    185 		 */
    186 		tp->snd_cwnd = TCP_INITIAL_WINDOW(txsegsize);
    187 again:
    188 	sendalot = 0;
    189 	off = tp->snd_nxt - tp->snd_una;
    190 	win = min(tp->snd_wnd, tp->snd_cwnd);
    191 
    192 	flags = tcp_outflags[tp->t_state];
    193 	/*
    194 	 * If in persist timeout with window of 0, send 1 byte.
    195 	 * Otherwise, if window is small but nonzero
    196 	 * and timer expired, we will send what we can
    197 	 * and go to transmit state.
    198 	 */
    199 	if (tp->t_force) {
    200 		if (win == 0) {
    201 			/*
    202 			 * If we still have some data to send, then
    203 			 * clear the FIN bit.  Usually this would
    204 			 * happen below when it realizes that we
    205 			 * aren't sending all the data.  However,
    206 			 * if we have exactly 1 byte of unset data,
    207 			 * then it won't clear the FIN bit below,
    208 			 * and if we are in persist state, we wind
    209 			 * up sending the packet without recording
    210 			 * that we sent the FIN bit.
    211 			 *
    212 			 * We can't just blindly clear the FIN bit,
    213 			 * because if we don't have any more data
    214 			 * to send then the probe will be the FIN
    215 			 * itself.
    216 			 */
    217 			if (off < so->so_snd.sb_cc)
    218 				flags &= ~TH_FIN;
    219 			win = 1;
    220 		} else {
    221 			tp->t_timer[TCPT_PERSIST] = 0;
    222 			tp->t_rxtshift = 0;
    223 		}
    224 	}
    225 
    226 	if (win < so->so_snd.sb_cc) {
    227 		len = win - off;
    228 		flags &= ~TH_FIN;
    229 	} else
    230 		len = so->so_snd.sb_cc - off;
    231 
    232 	if (len < 0) {
    233 		/*
    234 		 * If FIN has been sent but not acked,
    235 		 * but we haven't been called to retransmit,
    236 		 * len will be -1.  Otherwise, window shrank
    237 		 * after we sent into it.  If window shrank to 0,
    238 		 * cancel pending retransmit, pull snd_nxt back
    239 		 * to (closed) window, and set the persist timer
    240 		 * if it isn't already going.  If the window didn't
    241 		 * close completely, just wait for an ACK.
    242 		 */
    243 		len = 0;
    244 		if (win == 0) {
    245 			tp->t_timer[TCPT_REXMT] = 0;
    246 			tp->t_rxtshift = 0;
    247 			tp->snd_nxt = tp->snd_una;
    248 			if (tp->t_timer[TCPT_PERSIST] == 0)
    249 				tcp_setpersist(tp);
    250 		}
    251 	}
    252 	if (len > txsegsize) {
    253 		len = txsegsize;
    254 		flags &= ~TH_FIN;
    255 		sendalot = 1;
    256 	}
    257 
    258 	win = sbspace(&so->so_rcv);
    259 
    260 	/*
    261 	 * Sender silly window avoidance.  If connection is idle
    262 	 * and can send all data, a maximum segment,
    263 	 * at least a maximum default-size segment do it,
    264 	 * or are forced, do it; otherwise don't bother.
    265 	 * If peer's buffer is tiny, then send
    266 	 * when window is at least half open.
    267 	 * If retransmitting (possibly after persist timer forced us
    268 	 * to send into a small window), then must resend.
    269 	 */
    270 	if (len) {
    271 		if (len == txsegsize)
    272 			goto send;
    273 		if ((idle || tp->t_flags & TF_NODELAY) &&
    274 		    len + off >= so->so_snd.sb_cc)
    275 			goto send;
    276 		if (tp->t_force)
    277 			goto send;
    278 		if (len >= tp->max_sndwnd / 2)
    279 			goto send;
    280 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
    281 			goto send;
    282 	}
    283 
    284 	/*
    285 	 * Compare available window to amount of window known to peer
    286 	 * (as advertised window less next expected input).  If the
    287 	 * difference is at least twice the size of the largest segment
    288 	 * we expect to receive (i.e. two segments) or at least 50% of
    289 	 * the maximum possible window, then want to send a window update
    290 	 * to peer.
    291 	 */
    292 	if (win > 0) {
    293 		/*
    294 		 * "adv" is the amount we can increase the window,
    295 		 * taking into account that we are limited by
    296 		 * TCP_MAXWIN << tp->rcv_scale.
    297 		 */
    298 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
    299 			(tp->rcv_adv - tp->rcv_nxt);
    300 
    301 		if (adv >= (long) (2 * rxsegsize))
    302 			goto send;
    303 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
    304 			goto send;
    305 	}
    306 
    307 	/*
    308 	 * Send if we owe peer an ACK.
    309 	 */
    310 	if (tp->t_flags & TF_ACKNOW)
    311 		goto send;
    312 	if (flags & (TH_SYN|TH_RST))
    313 		goto send;
    314 	if (SEQ_GT(tp->snd_up, tp->snd_una))
    315 		goto send;
    316 	/*
    317 	 * If our state indicates that FIN should be sent
    318 	 * and we have not yet done so, or we're retransmitting the FIN,
    319 	 * then we need to send.
    320 	 */
    321 	if (flags & TH_FIN &&
    322 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
    323 		goto send;
    324 
    325 	/*
    326 	 * TCP window updates are not reliable, rather a polling protocol
    327 	 * using ``persist'' packets is used to insure receipt of window
    328 	 * updates.  The three ``states'' for the output side are:
    329 	 *	idle			not doing retransmits or persists
    330 	 *	persisting		to move a small or zero window
    331 	 *	(re)transmitting	and thereby not persisting
    332 	 *
    333 	 * tp->t_timer[TCPT_PERSIST]
    334 	 *	is set when we are in persist state.
    335 	 * tp->t_force
    336 	 *	is set when we are called to send a persist packet.
    337 	 * tp->t_timer[TCPT_REXMT]
    338 	 *	is set when we are retransmitting
    339 	 * The output side is idle when both timers are zero.
    340 	 *
    341 	 * If send window is too small, there is data to transmit, and no
    342 	 * retransmit or persist is pending, then go to persist state.
    343 	 * If nothing happens soon, send when timer expires:
    344 	 * if window is nonzero, transmit what we can,
    345 	 * otherwise force out a byte.
    346 	 */
    347 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
    348 	    tp->t_timer[TCPT_PERSIST] == 0) {
    349 		tp->t_rxtshift = 0;
    350 		tcp_setpersist(tp);
    351 	}
    352 
    353 	/*
    354 	 * No reason to send a segment, just return.
    355 	 */
    356 	return (0);
    357 
    358 send:
    359 	/*
    360 	 * Before ESTABLISHED, force sending of initial options
    361 	 * unless TCP set not to do any options.
    362 	 * NOTE: we assume that the IP/TCP header plus TCP options
    363 	 * always fit in a single mbuf, leaving room for a maximum
    364 	 * link header, i.e.
    365 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
    366 	 */
    367 	optlen = 0;
    368 	hdrlen = sizeof (struct tcpiphdr);
    369 	if (flags & TH_SYN) {
    370 		tp->snd_nxt = tp->iss;
    371 		tp->t_ourmss = tcp_mss_to_advertise(tp);
    372 		if ((tp->t_flags & TF_NOOPT) == 0) {
    373 			opt[0] = TCPOPT_MAXSEG;
    374 			opt[1] = 4;
    375 			opt[2] = (tp->t_ourmss >> 8) & 0xff;
    376 			opt[3] = tp->t_ourmss & 0xff;
    377 			optlen = 4;
    378 
    379 			if ((tp->t_flags & TF_REQ_SCALE) &&
    380 			    ((flags & TH_ACK) == 0 ||
    381 			    (tp->t_flags & TF_RCVD_SCALE))) {
    382 				*((u_int32_t *) (opt + optlen)) = htonl(
    383 					TCPOPT_NOP << 24 |
    384 					TCPOPT_WINDOW << 16 |
    385 					TCPOLEN_WINDOW << 8 |
    386 					tp->request_r_scale);
    387 				optlen += 4;
    388 			}
    389 		}
    390  	}
    391 
    392  	/*
    393 	 * Send a timestamp and echo-reply if this is a SYN and our side
    394 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
    395 	 * and our peer have sent timestamps in our SYN's.
    396  	 */
    397  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
    398  	     (flags & TH_RST) == 0 &&
    399  	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
    400 	     (tp->t_flags & TF_RCVD_TSTMP))) {
    401 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
    402 
    403  		/* Form timestamp option as shown in appendix A of RFC 1323. */
    404  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
    405  		*lp++ = htonl(tcp_now);
    406  		*lp   = htonl(tp->ts_recent);
    407  		optlen += TCPOLEN_TSTAMP_APPA;
    408  	}
    409 
    410  	hdrlen += optlen;
    411 
    412 #ifdef DIAGNOSTIC
    413 	if (len > txsegsize)
    414 		panic("tcp data to be sent is larger than segment");
    415  	if (max_linkhdr + hdrlen > MHLEN)
    416 		panic("tcphdr too big");
    417 #endif
    418 
    419 	/*
    420 	 * Grab a header mbuf, attaching a copy of data to
    421 	 * be transmitted, and initialize the header from
    422 	 * the template for sends on this connection.
    423 	 */
    424 	if (len) {
    425 		if (tp->t_force && len == 1)
    426 			tcpstat.tcps_sndprobe++;
    427 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
    428 			tcpstat.tcps_sndrexmitpack++;
    429 			tcpstat.tcps_sndrexmitbyte += len;
    430 		} else {
    431 			tcpstat.tcps_sndpack++;
    432 			tcpstat.tcps_sndbyte += len;
    433 		}
    434 #ifdef notyet
    435 		if ((m = m_copypack(so->so_snd.sb_mb, off,
    436 		    (int)len, max_linkhdr + hdrlen)) == 0) {
    437 			error = ENOBUFS;
    438 			goto out;
    439 		}
    440 		/*
    441 		 * m_copypack left space for our hdr; use it.
    442 		 */
    443 		m->m_len += hdrlen;
    444 		m->m_data -= hdrlen;
    445 #else
    446 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    447 		if (m == NULL) {
    448 			error = ENOBUFS;
    449 			goto out;
    450 		}
    451 		m->m_data += max_linkhdr;
    452 		m->m_len = hdrlen;
    453 		if (len <= MHLEN - hdrlen - max_linkhdr) {
    454 			m_copydata(so->so_snd.sb_mb, off, (int) len,
    455 			    mtod(m, caddr_t) + hdrlen);
    456 			m->m_len += len;
    457 		} else {
    458 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
    459 			if (m->m_next == 0) {
    460 				(void) m_freem(m);
    461 				error = ENOBUFS;
    462 				goto out;
    463 			}
    464 		}
    465 #endif
    466 		/*
    467 		 * If we're sending everything we've got, set PUSH.
    468 		 * (This will keep happy those implementations which only
    469 		 * give data to the user when a buffer fills or
    470 		 * a PUSH comes in.)
    471 		 */
    472 		if (off + len == so->so_snd.sb_cc)
    473 			flags |= TH_PUSH;
    474 	} else {
    475 		if (tp->t_flags & TF_ACKNOW)
    476 			tcpstat.tcps_sndacks++;
    477 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
    478 			tcpstat.tcps_sndctrl++;
    479 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
    480 			tcpstat.tcps_sndurg++;
    481 		else
    482 			tcpstat.tcps_sndwinup++;
    483 
    484 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    485 		if (m == NULL) {
    486 			error = ENOBUFS;
    487 			goto out;
    488 		}
    489 		m->m_data += max_linkhdr;
    490 		m->m_len = hdrlen;
    491 	}
    492 	m->m_pkthdr.rcvif = (struct ifnet *)0;
    493 	ti = mtod(m, struct tcpiphdr *);
    494 	if (tp->t_template == 0)
    495 		panic("tcp_output");
    496 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
    497 
    498 	/*
    499 	 * Fill in fields, remembering maximum advertised
    500 	 * window for use in delaying messages about window sizes.
    501 	 * If resending a FIN, be sure not to use a new sequence number.
    502 	 */
    503 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
    504 	    tp->snd_nxt == tp->snd_max)
    505 		tp->snd_nxt--;
    506 	/*
    507 	 * If we are doing retransmissions, then snd_nxt will
    508 	 * not reflect the first unsent octet.  For ACK only
    509 	 * packets, we do not want the sequence number of the
    510 	 * retransmitted packet, we want the sequence number
    511 	 * of the next unsent octet.  So, if there is no data
    512 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
    513 	 * when filling in ti_seq.  But if we are in persist
    514 	 * state, snd_max might reflect one byte beyond the
    515 	 * right edge of the window, so use snd_nxt in that
    516 	 * case, since we know we aren't doing a retransmission.
    517 	 * (retransmit and persist are mutually exclusive...)
    518 	 */
    519 	if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
    520 		ti->ti_seq = htonl(tp->snd_nxt);
    521 	else
    522 		ti->ti_seq = htonl(tp->snd_max);
    523 	ti->ti_ack = htonl(tp->rcv_nxt);
    524 	if (optlen) {
    525 		bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
    526 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
    527 	}
    528 	ti->ti_flags = flags;
    529 	/*
    530 	 * Calculate receive window.  Don't shrink window,
    531 	 * but avoid silly window syndrome.
    532 	 */
    533 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
    534 		win = 0;
    535 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
    536 		win = (long)TCP_MAXWIN << tp->rcv_scale;
    537 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
    538 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
    539 	ti->ti_win = htons((u_int16_t) (win>>tp->rcv_scale));
    540 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
    541 		u_int32_t urp = tp->snd_up - tp->snd_nxt;
    542 		if (urp > IP_MAXPACKET)
    543 			urp = IP_MAXPACKET;
    544 		ti->ti_urp = htons((u_int16_t)urp);
    545 		ti->ti_flags |= TH_URG;
    546 	} else
    547 		/*
    548 		 * If no urgent pointer to send, then we pull
    549 		 * the urgent pointer to the left edge of the send window
    550 		 * so that it doesn't drift into the send window on sequence
    551 		 * number wraparound.
    552 		 */
    553 		tp->snd_up = tp->snd_una;		/* drag it along */
    554 
    555 	/*
    556 	 * Put TCP length in extended header, and then
    557 	 * checksum extended header and data.
    558 	 */
    559 	if (len + optlen)
    560 		ti->ti_len = htons((u_int16_t)(sizeof (struct tcphdr) +
    561 		    optlen + len));
    562 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
    563 
    564 	/*
    565 	 * In transmit state, time the transmission and arrange for
    566 	 * the retransmit.  In persist state, just set snd_max.
    567 	 */
    568 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
    569 		tcp_seq startseq = tp->snd_nxt;
    570 
    571 		/*
    572 		 * Advance snd_nxt over sequence space of this segment.
    573 		 */
    574 		if (flags & (TH_SYN|TH_FIN)) {
    575 			if (flags & TH_SYN)
    576 				tp->snd_nxt++;
    577 			if (flags & TH_FIN) {
    578 				tp->snd_nxt++;
    579 				tp->t_flags |= TF_SENTFIN;
    580 			}
    581 		}
    582 		tp->snd_nxt += len;
    583 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
    584 			tp->snd_max = tp->snd_nxt;
    585 			/*
    586 			 * Time this transmission if not a retransmission and
    587 			 * not currently timing anything.
    588 			 */
    589 			if (tp->t_rtt == 0) {
    590 				tp->t_rtt = 1;
    591 				tp->t_rtseq = startseq;
    592 				tcpstat.tcps_segstimed++;
    593 			}
    594 		}
    595 
    596 		/*
    597 		 * Set retransmit timer if not currently set,
    598 		 * and not doing an ack or a keep-alive probe.
    599 		 * Initial value for retransmit timer is smoothed
    600 		 * round-trip time + 2 * round-trip time variance.
    601 		 * Initialize shift counter which is used for backoff
    602 		 * of retransmit time.
    603 		 */
    604 		if (tp->t_timer[TCPT_REXMT] == 0 &&
    605 		    tp->snd_nxt != tp->snd_una) {
    606 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
    607 			if (tp->t_timer[TCPT_PERSIST]) {
    608 				tp->t_timer[TCPT_PERSIST] = 0;
    609 				tp->t_rxtshift = 0;
    610 			}
    611 		}
    612 	} else
    613 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
    614 			tp->snd_max = tp->snd_nxt + len;
    615 
    616 	/*
    617 	 * Trace.
    618 	 */
    619 	if (so->so_options & SO_DEBUG)
    620 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
    621 
    622 	/*
    623 	 * Fill in IP length and desired time to live and
    624 	 * send to IP level.  There should be a better way
    625 	 * to handle ttl and tos; we could keep them in
    626 	 * the template, but need a way to checksum without them.
    627 	 */
    628 	m->m_pkthdr.len = hdrlen + len;
    629 #ifdef TUBA
    630 	if (tp->t_tuba_pcb)
    631 		error = tuba_output(m, tp);
    632 	else
    633 #endif
    634     {
    635 	struct rtentry *rt;
    636 
    637 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
    638 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
    639 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
    640 
    641 	if (ip_mtudisc && (rt = in_pcbrtentry(tp->t_inpcb)) != 0 &&
    642 	    (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
    643 		((struct ip *)ti)->ip_off |= IP_DF;
    644 
    645 #if BSD >= 43
    646 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
    647 	    so->so_options & SO_DONTROUTE, 0);
    648 #else
    649 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
    650 	    so->so_options & SO_DONTROUTE);
    651 #endif
    652     }
    653 	if (error) {
    654 out:
    655 		if (error == ENOBUFS) {
    656 			tcp_quench(tp->t_inpcb, 0);
    657 			return (0);
    658 		}
    659 		if ((error == EHOSTUNREACH || error == ENETDOWN)
    660 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
    661 			tp->t_softerror = error;
    662 			return (0);
    663 		}
    664 		return (error);
    665 	}
    666 	tcpstat.tcps_sndtotal++;
    667 	if (tp->t_flags & TF_DELACK)
    668 		tcpstat.tcps_delack++;
    669 
    670 	/*
    671 	 * Data sent (as far as we can tell).
    672 	 * If this advertises a larger window than any other segment,
    673 	 * then remember the size of the advertised window.
    674 	 * Any pending ACK has now been sent.
    675 	 */
    676 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
    677 		tp->rcv_adv = tp->rcv_nxt + win;
    678 	tp->last_ack_sent = tp->rcv_nxt;
    679 	tp->t_flags &= ~TF_ACKNOW;
    680 	TCP_CLEAR_DELACK(tp);
    681 	if (sendalot)
    682 		goto again;
    683 	return (0);
    684 }
    685 
    686 void
    687 tcp_setpersist(tp)
    688 	register struct tcpcb *tp;
    689 {
    690 	register int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
    691 
    692 	if (tp->t_timer[TCPT_REXMT])
    693 		panic("tcp_output REXMT");
    694 	/*
    695 	 * Start/restart persistance timer.
    696 	 */
    697 	if (t < tp->t_rttmin)
    698 		t = tp->t_rttmin;
    699 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
    700 	    t * tcp_backoff[tp->t_rxtshift],
    701 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
    702 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
    703 		tp->t_rxtshift++;
    704 }
    705