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