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