Home | History | Annotate | Line # | Download | only in netinet
tcp_output.c revision 1.35
      1 /*	$NetBSD: tcp_output.c,v 1.35 1998/04/29 03:44:12 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 /*
    113  * Knob to enable Congestion Window Monitoring.
    114  */
    115 #ifdef TCP_CWM
    116 int	tcp_cwm = 1;
    117 #else
    118 int	tcp_cwm = 0;
    119 #endif
    120 
    121 static __inline void tcp_segsize __P((struct tcpcb *, int *, int *));
    122 static __inline void
    123 tcp_segsize(tp, txsegsizep, rxsegsizep)
    124 	struct tcpcb *tp;
    125 	int *txsegsizep, *rxsegsizep;
    126 {
    127 	struct inpcb *inp = tp->t_inpcb;
    128 	struct rtentry *rt;
    129 	struct ifnet *ifp;
    130 	int size;
    131 
    132 	if ((rt = in_pcbrtentry(inp)) == NULL) {
    133 		size = tcp_mssdflt;
    134 		goto out;
    135 	}
    136 
    137 	ifp = rt->rt_ifp;
    138 
    139 	if (rt->rt_rmx.rmx_mtu != 0)
    140 		size = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
    141 	else if (ip_mtudisc || in_localaddr(inp->inp_faddr) ||
    142 		 ifp->if_flags & IFF_LOOPBACK)
    143 		size = ifp->if_mtu - sizeof(struct tcpiphdr);
    144 	else
    145 		size = tcp_mssdflt;
    146 	size -= (tcp_optlen(tp) + ip_optlen(tp->t_inpcb));
    147 
    148  out:
    149 	*txsegsizep = min(tp->t_peermss, size);
    150 	*rxsegsizep = min(tp->t_ourmss, size);
    151 
    152 	if (*txsegsizep != tp->t_segsz) {
    153 		/*
    154 		 * If the new segment size is larger, we don't want to
    155 		 * mess up the congestion window, but if it is smaller
    156 		 * we'll have to reduce the congestion window to ensure
    157 		 * that we don't get into trouble with initial windows
    158 		 * and the rest.  In any case, if the segment size
    159 		 * has changed, chances are the path has, too, and
    160 		 * our congestion window will be different.
    161 		 */
    162 		if (*txsegsizep < tp->t_segsz) {
    163 			tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
    164 					   * *txsegsizep, *txsegsizep);
    165 			tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
    166 						* *txsegsizep, *txsegsizep);
    167 		}
    168 		tp->t_segsz = *txsegsizep;
    169 	}
    170 }
    171 
    172 /*
    173  * Tcp output routine: figure out what should be sent and send it.
    174  */
    175 int
    176 tcp_output(tp)
    177 	register struct tcpcb *tp;
    178 {
    179 	register struct socket *so = tp->t_inpcb->inp_socket;
    180 	register long len, win;
    181 	int off, flags, error;
    182 	register struct mbuf *m;
    183 	register struct tcpiphdr *ti;
    184 	u_char opt[MAX_TCPOPTLEN];
    185 	unsigned optlen, hdrlen;
    186 	int idle, sendalot, txsegsize, rxsegsize;
    187 
    188 	tcp_segsize(tp, &txsegsize, &rxsegsize);
    189 
    190 	idle = (tp->snd_max == tp->snd_una);
    191 
    192 	if (tcp_cwm) {
    193 		/*
    194 		 * Hughes/Touch/Heidemann Congestion Window Monitoring.
    195 		 * Count the number of packets currently pending
    196 		 * acknowledgement, and limit our congestion window
    197 		 * to the initial congestion window plus that count.
    198 		 * This prevents bursting once all pending packets have
    199 		 * been acknowledged (i.e. transmission is idle).
    200 		 */
    201 		tp->snd_cwnd = min(tp->snd_cwnd,
    202 		    TCP_INITIAL_WINDOW(tcp_init_win, txsegsize) +
    203 		    (tp->snd_nxt - tp->snd_una));
    204 	} else {
    205 		if (idle && tp->t_idle >= tp->t_rxtcur) {
    206 			/*
    207 			 * We have been idle for "a while" and no acks are
    208 			 * expected to clock out any data we send --
    209 			 * slow start to get ack "clock" running again.
    210 			 */
    211 			tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win,
    212 			    txsegsize);
    213 		}
    214 	}
    215 
    216 again:
    217 	/*
    218 	 * Determine length of data that should be transmitted, and
    219 	 * flags that should be used.  If there is some data or critical
    220 	 * controls (SYN, RST) to send, then transmit; otherwise,
    221 	 * investigate further.
    222 	 */
    223 	sendalot = 0;
    224 	off = tp->snd_nxt - tp->snd_una;
    225 	win = min(tp->snd_wnd, tp->snd_cwnd);
    226 
    227 	flags = tcp_outflags[tp->t_state];
    228 	/*
    229 	 * If in persist timeout with window of 0, send 1 byte.
    230 	 * Otherwise, if window is small but nonzero
    231 	 * and timer expired, we will send what we can
    232 	 * and go to transmit state.
    233 	 */
    234 	if (tp->t_force) {
    235 		if (win == 0) {
    236 			/*
    237 			 * If we still have some data to send, then
    238 			 * clear the FIN bit.  Usually this would
    239 			 * happen below when it realizes that we
    240 			 * aren't sending all the data.  However,
    241 			 * if we have exactly 1 byte of unset data,
    242 			 * then it won't clear the FIN bit below,
    243 			 * and if we are in persist state, we wind
    244 			 * up sending the packet without recording
    245 			 * that we sent the FIN bit.
    246 			 *
    247 			 * We can't just blindly clear the FIN bit,
    248 			 * because if we don't have any more data
    249 			 * to send then the probe will be the FIN
    250 			 * itself.
    251 			 */
    252 			if (off < so->so_snd.sb_cc)
    253 				flags &= ~TH_FIN;
    254 			win = 1;
    255 		} else {
    256 			tp->t_timer[TCPT_PERSIST] = 0;
    257 			tp->t_rxtshift = 0;
    258 		}
    259 	}
    260 
    261 	if (win < so->so_snd.sb_cc) {
    262 		len = win - off;
    263 		flags &= ~TH_FIN;
    264 	} else
    265 		len = so->so_snd.sb_cc - off;
    266 
    267 	if (len < 0) {
    268 		/*
    269 		 * If FIN has been sent but not acked,
    270 		 * but we haven't been called to retransmit,
    271 		 * len will be -1.  Otherwise, window shrank
    272 		 * after we sent into it.  If window shrank to 0,
    273 		 * cancel pending retransmit, pull snd_nxt back
    274 		 * to (closed) window, and set the persist timer
    275 		 * if it isn't already going.  If the window didn't
    276 		 * close completely, just wait for an ACK.
    277 		 */
    278 		len = 0;
    279 		if (win == 0) {
    280 			tp->t_timer[TCPT_REXMT] = 0;
    281 			tp->t_rxtshift = 0;
    282 			tp->snd_nxt = tp->snd_una;
    283 			if (tp->t_timer[TCPT_PERSIST] == 0)
    284 				tcp_setpersist(tp);
    285 		}
    286 	}
    287 	if (len > txsegsize) {
    288 		len = txsegsize;
    289 		flags &= ~TH_FIN;
    290 		sendalot = 1;
    291 	}
    292 
    293 	win = sbspace(&so->so_rcv);
    294 
    295 	/*
    296 	 * Sender silly window avoidance.  If connection is idle
    297 	 * and can send all data, a maximum segment,
    298 	 * at least a maximum default-size segment do it,
    299 	 * or are forced, do it; otherwise don't bother.
    300 	 * If peer's buffer is tiny, then send
    301 	 * when window is at least half open.
    302 	 * If retransmitting (possibly after persist timer forced us
    303 	 * to send into a small window), then must resend.
    304 	 */
    305 	if (len) {
    306 		if (len == txsegsize)
    307 			goto send;
    308 		if ((idle || tp->t_flags & TF_NODELAY) &&
    309 		    len + off >= so->so_snd.sb_cc)
    310 			goto send;
    311 		if (tp->t_force)
    312 			goto send;
    313 		if (len >= tp->max_sndwnd / 2)
    314 			goto send;
    315 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
    316 			goto send;
    317 	}
    318 
    319 	/*
    320 	 * Compare available window to amount of window known to peer
    321 	 * (as advertised window less next expected input).  If the
    322 	 * difference is at least twice the size of the largest segment
    323 	 * we expect to receive (i.e. two segments) or at least 50% of
    324 	 * the maximum possible window, then want to send a window update
    325 	 * to peer.
    326 	 */
    327 	if (win > 0) {
    328 		/*
    329 		 * "adv" is the amount we can increase the window,
    330 		 * taking into account that we are limited by
    331 		 * TCP_MAXWIN << tp->rcv_scale.
    332 		 */
    333 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
    334 			(tp->rcv_adv - tp->rcv_nxt);
    335 
    336 		if (adv >= (long) (2 * rxsegsize))
    337 			goto send;
    338 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
    339 			goto send;
    340 	}
    341 
    342 	/*
    343 	 * Send if we owe peer an ACK.
    344 	 */
    345 	if (tp->t_flags & TF_ACKNOW)
    346 		goto send;
    347 	if (flags & (TH_SYN|TH_RST))
    348 		goto send;
    349 	if (SEQ_GT(tp->snd_up, tp->snd_una))
    350 		goto send;
    351 	/*
    352 	 * If our state indicates that FIN should be sent
    353 	 * and we have not yet done so, or we're retransmitting the FIN,
    354 	 * then we need to send.
    355 	 */
    356 	if (flags & TH_FIN &&
    357 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
    358 		goto send;
    359 
    360 	/*
    361 	 * TCP window updates are not reliable, rather a polling protocol
    362 	 * using ``persist'' packets is used to insure receipt of window
    363 	 * updates.  The three ``states'' for the output side are:
    364 	 *	idle			not doing retransmits or persists
    365 	 *	persisting		to move a small or zero window
    366 	 *	(re)transmitting	and thereby not persisting
    367 	 *
    368 	 * tp->t_timer[TCPT_PERSIST]
    369 	 *	is set when we are in persist state.
    370 	 * tp->t_force
    371 	 *	is set when we are called to send a persist packet.
    372 	 * tp->t_timer[TCPT_REXMT]
    373 	 *	is set when we are retransmitting
    374 	 * The output side is idle when both timers are zero.
    375 	 *
    376 	 * If send window is too small, there is data to transmit, and no
    377 	 * retransmit or persist is pending, then go to persist state.
    378 	 * If nothing happens soon, send when timer expires:
    379 	 * if window is nonzero, transmit what we can,
    380 	 * otherwise force out a byte.
    381 	 */
    382 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
    383 	    tp->t_timer[TCPT_PERSIST] == 0) {
    384 		tp->t_rxtshift = 0;
    385 		tcp_setpersist(tp);
    386 	}
    387 
    388 	/*
    389 	 * No reason to send a segment, just return.
    390 	 */
    391 	return (0);
    392 
    393 send:
    394 	/*
    395 	 * Before ESTABLISHED, force sending of initial options
    396 	 * unless TCP set not to do any options.
    397 	 * NOTE: we assume that the IP/TCP header plus TCP options
    398 	 * always fit in a single mbuf, leaving room for a maximum
    399 	 * link header, i.e.
    400 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
    401 	 */
    402 	optlen = 0;
    403 	hdrlen = sizeof (struct tcpiphdr);
    404 	if (flags & TH_SYN) {
    405 		struct rtentry *rt = in_pcbrtentry(tp->t_inpcb);
    406 
    407 		tp->snd_nxt = tp->iss;
    408 		tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
    409 						    rt->rt_ifp : NULL);
    410 		if ((tp->t_flags & TF_NOOPT) == 0) {
    411 			opt[0] = TCPOPT_MAXSEG;
    412 			opt[1] = 4;
    413 			opt[2] = (tp->t_ourmss >> 8) & 0xff;
    414 			opt[3] = tp->t_ourmss & 0xff;
    415 			optlen = 4;
    416 
    417 			if ((tp->t_flags & TF_REQ_SCALE) &&
    418 			    ((flags & TH_ACK) == 0 ||
    419 			    (tp->t_flags & TF_RCVD_SCALE))) {
    420 				*((u_int32_t *) (opt + optlen)) = htonl(
    421 					TCPOPT_NOP << 24 |
    422 					TCPOPT_WINDOW << 16 |
    423 					TCPOLEN_WINDOW << 8 |
    424 					tp->request_r_scale);
    425 				optlen += 4;
    426 			}
    427 		}
    428  	}
    429 
    430  	/*
    431 	 * Send a timestamp and echo-reply if this is a SYN and our side
    432 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
    433 	 * and our peer have sent timestamps in our SYN's.
    434  	 */
    435  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
    436  	     (flags & TH_RST) == 0 &&
    437  	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
    438 	     (tp->t_flags & TF_RCVD_TSTMP))) {
    439 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
    440 
    441  		/* Form timestamp option as shown in appendix A of RFC 1323. */
    442  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
    443  		*lp++ = htonl(tcp_now);
    444  		*lp   = htonl(tp->ts_recent);
    445  		optlen += TCPOLEN_TSTAMP_APPA;
    446  	}
    447 
    448  	hdrlen += optlen;
    449 
    450 #ifdef DIAGNOSTIC
    451 	if (len > txsegsize)
    452 		panic("tcp data to be sent is larger than segment");
    453  	if (max_linkhdr + hdrlen > MHLEN)
    454 		panic("tcphdr too big");
    455 #endif
    456 
    457 	/*
    458 	 * Grab a header mbuf, attaching a copy of data to
    459 	 * be transmitted, and initialize the header from
    460 	 * the template for sends on this connection.
    461 	 */
    462 	if (len) {
    463 		if (tp->t_force && len == 1)
    464 			tcpstat.tcps_sndprobe++;
    465 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
    466 			tcpstat.tcps_sndrexmitpack++;
    467 			tcpstat.tcps_sndrexmitbyte += len;
    468 		} else {
    469 			tcpstat.tcps_sndpack++;
    470 			tcpstat.tcps_sndbyte += len;
    471 		}
    472 #ifdef notyet
    473 		if ((m = m_copypack(so->so_snd.sb_mb, off,
    474 		    (int)len, max_linkhdr + hdrlen)) == 0) {
    475 			error = ENOBUFS;
    476 			goto out;
    477 		}
    478 		/*
    479 		 * m_copypack left space for our hdr; use it.
    480 		 */
    481 		m->m_len += hdrlen;
    482 		m->m_data -= hdrlen;
    483 #else
    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 		if (len <= MHLEN - hdrlen - max_linkhdr) {
    492 			m_copydata(so->so_snd.sb_mb, off, (int) len,
    493 			    mtod(m, caddr_t) + hdrlen);
    494 			m->m_len += len;
    495 		} else {
    496 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
    497 			if (m->m_next == 0) {
    498 				(void) m_freem(m);
    499 				error = ENOBUFS;
    500 				goto out;
    501 			}
    502 		}
    503 #endif
    504 		/*
    505 		 * If we're sending everything we've got, set PUSH.
    506 		 * (This will keep happy those implementations which only
    507 		 * give data to the user when a buffer fills or
    508 		 * a PUSH comes in.)
    509 		 */
    510 		if (off + len == so->so_snd.sb_cc)
    511 			flags |= TH_PUSH;
    512 	} else {
    513 		if (tp->t_flags & TF_ACKNOW)
    514 			tcpstat.tcps_sndacks++;
    515 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
    516 			tcpstat.tcps_sndctrl++;
    517 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
    518 			tcpstat.tcps_sndurg++;
    519 		else
    520 			tcpstat.tcps_sndwinup++;
    521 
    522 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    523 		if (m == NULL) {
    524 			error = ENOBUFS;
    525 			goto out;
    526 		}
    527 		m->m_data += max_linkhdr;
    528 		m->m_len = hdrlen;
    529 	}
    530 	m->m_pkthdr.rcvif = (struct ifnet *)0;
    531 	ti = mtod(m, struct tcpiphdr *);
    532 	if (tp->t_template == 0)
    533 		panic("tcp_output");
    534 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
    535 
    536 	/*
    537 	 * Fill in fields, remembering maximum advertised
    538 	 * window for use in delaying messages about window sizes.
    539 	 * If resending a FIN, be sure not to use a new sequence number.
    540 	 */
    541 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
    542 	    tp->snd_nxt == tp->snd_max)
    543 		tp->snd_nxt--;
    544 	/*
    545 	 * If we are doing retransmissions, then snd_nxt will
    546 	 * not reflect the first unsent octet.  For ACK only
    547 	 * packets, we do not want the sequence number of the
    548 	 * retransmitted packet, we want the sequence number
    549 	 * of the next unsent octet.  So, if there is no data
    550 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
    551 	 * when filling in ti_seq.  But if we are in persist
    552 	 * state, snd_max might reflect one byte beyond the
    553 	 * right edge of the window, so use snd_nxt in that
    554 	 * case, since we know we aren't doing a retransmission.
    555 	 * (retransmit and persist are mutually exclusive...)
    556 	 */
    557 	if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
    558 		ti->ti_seq = htonl(tp->snd_nxt);
    559 	else
    560 		ti->ti_seq = htonl(tp->snd_max);
    561 	ti->ti_ack = htonl(tp->rcv_nxt);
    562 	if (optlen) {
    563 		bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
    564 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
    565 	}
    566 	ti->ti_flags = flags;
    567 	/*
    568 	 * Calculate receive window.  Don't shrink window,
    569 	 * but avoid silly window syndrome.
    570 	 */
    571 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
    572 		win = 0;
    573 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
    574 		win = (long)TCP_MAXWIN << tp->rcv_scale;
    575 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
    576 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
    577 	ti->ti_win = htons((u_int16_t) (win>>tp->rcv_scale));
    578 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
    579 		u_int32_t urp = tp->snd_up - tp->snd_nxt;
    580 		if (urp > IP_MAXPACKET)
    581 			urp = IP_MAXPACKET;
    582 		ti->ti_urp = htons((u_int16_t)urp);
    583 		ti->ti_flags |= TH_URG;
    584 	} else
    585 		/*
    586 		 * If no urgent pointer to send, then we pull
    587 		 * the urgent pointer to the left edge of the send window
    588 		 * so that it doesn't drift into the send window on sequence
    589 		 * number wraparound.
    590 		 */
    591 		tp->snd_up = tp->snd_una;		/* drag it along */
    592 
    593 	/*
    594 	 * Put TCP length in extended header, and then
    595 	 * checksum extended header and data.
    596 	 */
    597 	if (len + optlen)
    598 		ti->ti_len = htons((u_int16_t)(sizeof (struct tcphdr) +
    599 		    optlen + len));
    600 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
    601 
    602 	/*
    603 	 * In transmit state, time the transmission and arrange for
    604 	 * the retransmit.  In persist state, just set snd_max.
    605 	 */
    606 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
    607 		tcp_seq startseq = tp->snd_nxt;
    608 
    609 		/*
    610 		 * Advance snd_nxt over sequence space of this segment.
    611 		 */
    612 		if (flags & (TH_SYN|TH_FIN)) {
    613 			if (flags & TH_SYN)
    614 				tp->snd_nxt++;
    615 			if (flags & TH_FIN) {
    616 				tp->snd_nxt++;
    617 				tp->t_flags |= TF_SENTFIN;
    618 			}
    619 		}
    620 		tp->snd_nxt += len;
    621 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
    622 			tp->snd_max = tp->snd_nxt;
    623 			/*
    624 			 * Time this transmission if not a retransmission and
    625 			 * not currently timing anything.
    626 			 */
    627 			if (tp->t_rtt == 0) {
    628 				tp->t_rtt = 1;
    629 				tp->t_rtseq = startseq;
    630 				tcpstat.tcps_segstimed++;
    631 			}
    632 		}
    633 
    634 		/*
    635 		 * Set retransmit timer if not currently set,
    636 		 * and not doing an ack or a keep-alive probe.
    637 		 * Initial value for retransmit timer is smoothed
    638 		 * round-trip time + 2 * round-trip time variance.
    639 		 * Initialize shift counter which is used for backoff
    640 		 * of retransmit time.
    641 		 */
    642 		if (tp->t_timer[TCPT_REXMT] == 0 &&
    643 		    tp->snd_nxt != tp->snd_una) {
    644 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
    645 			if (tp->t_timer[TCPT_PERSIST]) {
    646 				tp->t_timer[TCPT_PERSIST] = 0;
    647 				tp->t_rxtshift = 0;
    648 			}
    649 		}
    650 	} else
    651 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
    652 			tp->snd_max = tp->snd_nxt + len;
    653 
    654 	/*
    655 	 * Trace.
    656 	 */
    657 	if (so->so_options & SO_DEBUG)
    658 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
    659 
    660 	/*
    661 	 * Fill in IP length and desired time to live and
    662 	 * send to IP level.  There should be a better way
    663 	 * to handle ttl and tos; we could keep them in
    664 	 * the template, but need a way to checksum without them.
    665 	 */
    666 	m->m_pkthdr.len = hdrlen + len;
    667 #ifdef TUBA
    668 	if (tp->t_tuba_pcb)
    669 		error = tuba_output(m, tp);
    670 	else
    671 #endif
    672     {
    673 	struct rtentry *rt;
    674 
    675 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
    676 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
    677 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
    678 
    679 	if (ip_mtudisc && (rt = in_pcbrtentry(tp->t_inpcb)) != 0 &&
    680 	    (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
    681 		((struct ip *)ti)->ip_off |= IP_DF;
    682 
    683 #if BSD >= 43
    684 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
    685 	    so->so_options & SO_DONTROUTE, 0);
    686 #else
    687 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
    688 	    so->so_options & SO_DONTROUTE);
    689 #endif
    690     }
    691 	if (error) {
    692 out:
    693 		if (error == ENOBUFS) {
    694 			tcp_quench(tp->t_inpcb, 0);
    695 			return (0);
    696 		}
    697 		if ((error == EHOSTUNREACH || error == ENETDOWN)
    698 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
    699 			tp->t_softerror = error;
    700 			return (0);
    701 		}
    702 		return (error);
    703 	}
    704 	tcpstat.tcps_sndtotal++;
    705 	if (tp->t_flags & TF_DELACK)
    706 		tcpstat.tcps_delack++;
    707 
    708 	/*
    709 	 * Data sent (as far as we can tell).
    710 	 * If this advertises a larger window than any other segment,
    711 	 * then remember the size of the advertised window.
    712 	 * Any pending ACK has now been sent.
    713 	 */
    714 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
    715 		tp->rcv_adv = tp->rcv_nxt + win;
    716 	tp->last_ack_sent = tp->rcv_nxt;
    717 	tp->t_flags &= ~TF_ACKNOW;
    718 	TCP_CLEAR_DELACK(tp);
    719 	if (sendalot)
    720 		goto again;
    721 	return (0);
    722 }
    723 
    724 void
    725 tcp_setpersist(tp)
    726 	register struct tcpcb *tp;
    727 {
    728 	register int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
    729 
    730 	if (tp->t_timer[TCPT_REXMT])
    731 		panic("tcp_output REXMT");
    732 	/*
    733 	 * Start/restart persistance timer.
    734 	 */
    735 	if (t < tp->t_rttmin)
    736 		t = tp->t_rttmin;
    737 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
    738 	    t * tcp_backoff[tp->t_rxtshift],
    739 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
    740 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
    741 		tp->t_rxtshift++;
    742 }
    743