xref: /csrg-svn/sys/netinet/tcp_output.c (revision 69640)
123191Smckusick /*
2*69640Skarels  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
363218Sbostic  *	The Regents of the University of California.  All rights reserved.
423191Smckusick  *
544486Sbostic  * %sccs.include.redist.c%
632788Sbostic  *
7*69640Skarels  *	@(#)tcp_output.c	8.4 (Berkeley) 05/24/95
823191Smckusick  */
94677Swnj 
1056531Sbostic #include <sys/param.h>
1156531Sbostic #include <sys/systm.h>
1256531Sbostic #include <sys/malloc.h>
1356531Sbostic #include <sys/mbuf.h>
1456531Sbostic #include <sys/protosw.h>
1556531Sbostic #include <sys/socket.h>
1656531Sbostic #include <sys/socketvar.h>
1756531Sbostic #include <sys/errno.h>
1810895Ssam 
1956531Sbostic #include <net/route.h>
2010895Ssam 
2156531Sbostic #include <netinet/in.h>
2256531Sbostic #include <netinet/in_systm.h>
2356531Sbostic #include <netinet/ip.h>
2456531Sbostic #include <netinet/in_pcb.h>
2556531Sbostic #include <netinet/ip_var.h>
2656531Sbostic #include <netinet/tcp.h>
275088Swnj #define	TCPOUTFLAGS
2856531Sbostic #include <netinet/tcp_fsm.h>
2956531Sbostic #include <netinet/tcp_seq.h>
3056531Sbostic #include <netinet/tcp_timer.h>
3156531Sbostic #include <netinet/tcp_var.h>
3256531Sbostic #include <netinet/tcpip.h>
3356531Sbostic #include <netinet/tcp_debug.h>
344677Swnj 
3544380Skarels #ifdef notyet
3644380Skarels extern struct mbuf *m_copypack();
3744380Skarels #endif
3844380Skarels 
395441Swnj 
4057433Sandrew #define MAX_TCPOPTLEN	32	/* max # bytes that go in options */
4157433Sandrew 
425441Swnj /*
435245Sroot  * Tcp output routine: figure out what should be sent and send it.
444678Swnj  */
4561335Sbostic int
tcp_output(tp)465075Swnj tcp_output(tp)
475075Swnj 	register struct tcpcb *tp;
484678Swnj {
495075Swnj 	register struct socket *so = tp->t_inpcb->inp_socket;
5037317Skarels 	register long len, win;
5125940Skarels 	int off, flags, error;
525075Swnj 	register struct mbuf *m;
535075Swnj 	register struct tcpiphdr *ti;
5457433Sandrew 	u_char opt[MAX_TCPOPTLEN];
5544380Skarels 	unsigned optlen, hdrlen;
5625940Skarels 	int idle, sendalot;
574678Swnj 
585075Swnj 	/*
596279Swnj 	 * Determine length of data that should be transmitted,
605088Swnj 	 * and flags that will be used.
615088Swnj 	 * If there is some data or critical controls (SYN, RST)
625088Swnj 	 * to send, then transmit; otherwise, investigate further.
635075Swnj 	 */
6425940Skarels 	idle = (tp->snd_max == tp->snd_una);
6544380Skarels 	if (idle && tp->t_idle >= tp->t_rxtcur)
6644380Skarels 		/*
6744380Skarels 		 * We have been idle for "a while" and no acks are
6844380Skarels 		 * expected to clock out any data we send --
6944380Skarels 		 * slow start to get ack "clock" running again.
7044380Skarels 		 */
7144380Skarels 		tp->snd_cwnd = tp->t_maxseg;
727125Swnj again:
737125Swnj 	sendalot = 0;
745075Swnj 	off = tp->snd_nxt - tp->snd_una;
7537317Skarels 	win = min(tp->snd_wnd, tp->snd_cwnd);
7626834Skarels 
7765262Smckusick 	flags = tcp_outflags[tp->t_state];
7821116Skarels 	/*
7921116Skarels 	 * If in persist timeout with window of 0, send 1 byte.
8025940Skarels 	 * Otherwise, if window is small but nonzero
8125940Skarels 	 * and timer expired, we will send what we can
8225940Skarels 	 * and go to transmit state.
8321116Skarels 	 */
8421116Skarels 	if (tp->t_force) {
8565262Smckusick 		if (win == 0) {
8665262Smckusick 			/*
8765262Smckusick 			 * If we still have some data to send, then
8865262Smckusick 			 * clear the FIN bit.  Usually this would
8965262Smckusick 			 * happen below when it realizes that we
9065262Smckusick 			 * aren't sending all the data.  However,
9165262Smckusick 			 * if we have exactly 1 byte of unset data,
9265262Smckusick 			 * then it won't clear the FIN bit below,
9365262Smckusick 			 * and if we are in persist state, we wind
9465262Smckusick 			 * up sending the packet without recording
9565262Smckusick 			 * that we sent the FIN bit.
9665262Smckusick 			 *
9765262Smckusick 			 * We can't just blindly clear the FIN bit,
9865262Smckusick 			 * because if we don't have any more data
9965262Smckusick 			 * to send then the probe will be the FIN
10065262Smckusick 			 * itself.
10165262Smckusick 			 */
10265262Smckusick 			if (off < so->so_snd.sb_cc)
10365262Smckusick 				flags &= ~TH_FIN;
10421116Skarels 			win = 1;
10565262Smckusick 		} else {
10621116Skarels 			tp->t_timer[TCPT_PERSIST] = 0;
10721116Skarels 			tp->t_rxtshift = 0;
10821116Skarels 		}
10921116Skarels 	}
11025940Skarels 
11137317Skarels 	len = min(so->so_snd.sb_cc, win) - off;
11225940Skarels 
11326834Skarels 	if (len < 0) {
11426834Skarels 		/*
11527048Skarels 		 * If FIN has been sent but not acked,
11627048Skarels 		 * but we haven't been called to retransmit,
11732785Skarels 		 * len will be -1.  Otherwise, window shrank
11832785Skarels 		 * after we sent into it.  If window shrank to 0,
11932785Skarels 		 * cancel pending retransmit and pull snd_nxt
12032785Skarels 		 * back to (closed) window.  We will enter persist
12132785Skarels 		 * state below.  If the window didn't close completely,
12227048Skarels 		 * just wait for an ACK.
12326834Skarels 		 */
12432785Skarels 		len = 0;
12532785Skarels 		if (win == 0) {
12627067Skarels 			tp->t_timer[TCPT_REXMT] = 0;
12727067Skarels 			tp->snd_nxt = tp->snd_una;
12832785Skarels 		}
12926834Skarels 	}
13027067Skarels 	if (len > tp->t_maxseg) {
13127067Skarels 		len = tp->t_maxseg;
13231442Skarels 		sendalot = 1;
13327067Skarels 	}
13429795Skarels 	if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
13529795Skarels 		flags &= ~TH_FIN;
13644380Skarels 
13726834Skarels 	win = sbspace(&so->so_rcv);
13826834Skarels 
13925940Skarels 	/*
14017318Skarels 	 * Sender silly window avoidance.  If connection is idle
14117318Skarels 	 * and can send all data, a maximum segment,
14217318Skarels 	 * at least a maximum default-size segment do it,
1436279Swnj 	 * or are forced, do it; otherwise don't bother.
14425261Skarels 	 * If peer's buffer is tiny, then send
14525261Skarels 	 * when window is at least half open.
14621116Skarels 	 * If retransmitting (possibly after persist timer forced us
14721116Skarels 	 * to send into a small window), then must resend.
1486279Swnj 	 */
1496279Swnj 	if (len) {
15031725Skarels 		if (len == tp->t_maxseg)
1516279Swnj 			goto send;
15225940Skarels 		if ((idle || tp->t_flags & TF_NODELAY) &&
15325940Skarels 		    len + off >= so->so_snd.sb_cc)
1546279Swnj 			goto send;
1556279Swnj 		if (tp->t_force)
1566279Swnj 			goto send;
15725261Skarels 		if (len >= tp->max_sndwnd / 2)
15825261Skarels 			goto send;
15921116Skarels 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
16021116Skarels 			goto send;
16126834Skarels 	}
1626279Swnj 
1635441Swnj 	/*
16425940Skarels 	 * Compare available window to amount of window
16525940Skarels 	 * known to peer (as advertised window less
16632785Skarels 	 * next expected input).  If the difference is at least two
16740685Skarels 	 * max size segments, or at least 50% of the maximum possible
16832785Skarels 	 * window, then want to send a window update to peer.
1695075Swnj 	 */
17032034Skarels 	if (win > 0) {
17157433Sandrew 		/*
17257433Sandrew 		 * "adv" is the amount we can increase the window,
17357433Sandrew 		 * taking into account that we are limited by
17457433Sandrew 		 * TCP_MAXWIN << tp->rcv_scale.
17557433Sandrew 		 */
17657433Sandrew 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
17757433Sandrew 			(tp->rcv_adv - tp->rcv_nxt);
1784678Swnj 
17945169Skarels 		if (adv >= (long) (2 * tp->t_maxseg))
18032785Skarels 			goto send;
18145169Skarels 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
18232034Skarels 			goto send;
18332034Skarels 	}
18432034Skarels 
1855075Swnj 	/*
18644380Skarels 	 * Send if we owe peer an ACK.
18744380Skarels 	 */
18844380Skarels 	if (tp->t_flags & TF_ACKNOW)
18944380Skarels 		goto send;
19044380Skarels 	if (flags & (TH_SYN|TH_RST))
19144380Skarels 		goto send;
19244380Skarels 	if (SEQ_GT(tp->snd_up, tp->snd_una))
19344380Skarels 		goto send;
19444380Skarels 	/*
19544380Skarels 	 * If our state indicates that FIN should be sent
19644380Skarels 	 * and we have not yet done so, or we're retransmitting the FIN,
19744380Skarels 	 * then we need to send.
19844380Skarels 	 */
19944380Skarels 	if (flags & TH_FIN &&
20044380Skarels 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
20144380Skarels 		goto send;
20244380Skarels 
20344380Skarels 	/*
2047125Swnj 	 * TCP window updates are not reliable, rather a polling protocol
2057125Swnj 	 * using ``persist'' packets is used to insure receipt of window
2067125Swnj 	 * updates.  The three ``states'' for the output side are:
2077125Swnj 	 *	idle			not doing retransmits or persists
20825940Skarels 	 *	persisting		to move a small or zero window
2097125Swnj 	 *	(re)transmitting	and thereby not persisting
2107125Swnj 	 *
2117125Swnj 	 * tp->t_timer[TCPT_PERSIST]
2127125Swnj 	 *	is set when we are in persist state.
2137125Swnj 	 * tp->t_force
2147125Swnj 	 *	is set when we are called to send a persist packet.
2157125Swnj 	 * tp->t_timer[TCPT_REXMT]
2167125Swnj 	 *	is set when we are retransmitting
2177125Swnj 	 * The output side is idle when both timers are zero.
2187125Swnj 	 *
21921116Skarels 	 * If send window is too small, there is data to transmit, and no
22021116Skarels 	 * retransmit or persist is pending, then go to persist state.
22121116Skarels 	 * If nothing happens soon, send when timer expires:
22221116Skarels 	 * if window is nonzero, transmit what we can,
22321116Skarels 	 * otherwise force out a byte.
2247125Swnj 	 */
22521116Skarels 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
22621116Skarels 	    tp->t_timer[TCPT_PERSIST] == 0) {
2277125Swnj 		tp->t_rxtshift = 0;
2287125Swnj 		tcp_setpersist(tp);
2297125Swnj 	}
2307125Swnj 
2317125Swnj 	/*
2325075Swnj 	 * No reason to send a segment, just return.
2335075Swnj 	 */
2345110Swnj 	return (0);
2354678Swnj 
2365075Swnj send:
2375075Swnj 	/*
23844380Skarels 	 * Before ESTABLISHED, force sending of initial options
23944380Skarels 	 * unless TCP set not to do any options.
24044380Skarels 	 * NOTE: we assume that the IP/TCP header plus TCP options
24144380Skarels 	 * always fit in a single mbuf, leaving room for a maximum
24244380Skarels 	 * link header, i.e.
24344380Skarels 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
24444380Skarels 	 */
24544380Skarels 	optlen = 0;
24644380Skarels 	hdrlen = sizeof (struct tcpiphdr);
24759038Skarels 	if (flags & TH_SYN) {
24859038Skarels 		tp->snd_nxt = tp->iss;
24959038Skarels 		if ((tp->t_flags & TF_NOOPT) == 0) {
25059038Skarels 			u_short mss;
25152515Storek 
25259038Skarels 			opt[0] = TCPOPT_MAXSEG;
25359038Skarels 			opt[1] = 4;
25459038Skarels 			mss = htons((u_short) tcp_mss(tp, 0));
25559038Skarels 			bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss));
25659038Skarels 			optlen = 4;
25759038Skarels 
25859038Skarels 			if ((tp->t_flags & TF_REQ_SCALE) &&
25959038Skarels 			    ((flags & TH_ACK) == 0 ||
26059038Skarels 			    (tp->t_flags & TF_RCVD_SCALE))) {
26159038Skarels 				*((u_long *) (opt + optlen)) = htonl(
26259038Skarels 					TCPOPT_NOP << 24 |
26359038Skarels 					TCPOPT_WINDOW << 16 |
26459038Skarels 					TCPOLEN_WINDOW << 8 |
26559038Skarels 					tp->request_r_scale);
26659038Skarels 				optlen += 4;
26759038Skarels 			}
26859038Skarels 		}
26957433Sandrew  	}
27057433Sandrew 
27157433Sandrew  	/*
27257433Sandrew 	 * Send a timestamp and echo-reply if this is a SYN and our side
27357433Sandrew 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
27457433Sandrew 	 * and our peer have sent timestamps in our SYN's.
27557433Sandrew  	 */
27657433Sandrew  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
27757433Sandrew  	     (flags & TH_RST) == 0 &&
27857433Sandrew  	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
27957433Sandrew 	     (tp->t_flags & TF_RCVD_TSTMP))) {
28057433Sandrew 		u_long *lp = (u_long *)(opt + optlen);
28157433Sandrew 
28257433Sandrew  		/* Form timestamp option as shown in appendix A of RFC 1323. */
28357433Sandrew  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
28457433Sandrew  		*lp++ = htonl(tcp_now);
28557433Sandrew  		*lp   = htonl(tp->ts_recent);
28657433Sandrew  		optlen += TCPOLEN_TSTAMP_APPA;
28757433Sandrew  	}
28857666Sandrew 
28957666Sandrew  	hdrlen += optlen;
29057433Sandrew 
29157433Sandrew 	/*
29257433Sandrew 	 * Adjust data length if insertion of options will
29357433Sandrew 	 * bump the packet length beyond the t_maxseg length.
29457433Sandrew 	 */
295*69640Skarels 	if (len > tp->t_maxseg - optlen) {
29657433Sandrew 		len = tp->t_maxseg - optlen;
29757433Sandrew 		sendalot = 1;
298*69640Skarels 		flags &= ~TH_FIN;
29957433Sandrew 	 }
30057433Sandrew 
30157433Sandrew 
30244380Skarels #ifdef DIAGNOSTIC
30357433Sandrew  	if (max_linkhdr + hdrlen > MHLEN)
30457433Sandrew 		panic("tcphdr too big");
30544380Skarels #endif
30644380Skarels 
30744380Skarels 	/*
3085075Swnj 	 * Grab a header mbuf, attaching a copy of data to
3095075Swnj 	 * be transmitted, and initialize the header from
3105075Swnj 	 * the template for sends on this connection.
3115075Swnj 	 */
3125075Swnj 	if (len) {
31331442Skarels 		if (tp->t_force && len == 1)
31431442Skarels 			tcpstat.tcps_sndprobe++;
31531442Skarels 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
31631442Skarels 			tcpstat.tcps_sndrexmitpack++;
31731442Skarels 			tcpstat.tcps_sndrexmitbyte += len;
31831442Skarels 		} else {
31931442Skarels 			tcpstat.tcps_sndpack++;
32031442Skarels 			tcpstat.tcps_sndbyte += len;
32131442Skarels 		}
32244380Skarels #ifdef notyet
32344380Skarels 		if ((m = m_copypack(so->so_snd.sb_mb, off,
32444380Skarels 		    (int)len, max_linkhdr + hdrlen)) == 0) {
32544380Skarels 			error = ENOBUFS;
32644380Skarels 			goto out;
32744380Skarels 		}
32844380Skarels 		/*
32944380Skarels 		 * m_copypack left space for our hdr; use it.
33044380Skarels 		 */
33144380Skarels 		m->m_len += hdrlen;
33244380Skarels 		m->m_data -= hdrlen;
33344380Skarels #else
33444380Skarels 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
33544380Skarels 		if (m == NULL) {
33644380Skarels 			error = ENOBUFS;
33744380Skarels 			goto out;
33844380Skarels 		}
33944380Skarels 		m->m_data += max_linkhdr;
34044380Skarels 		m->m_len = hdrlen;
34144380Skarels 		if (len <= MHLEN - hdrlen - max_linkhdr) {
34237317Skarels 			m_copydata(so->so_snd.sb_mb, off, (int) len,
34344380Skarels 			    mtod(m, caddr_t) + hdrlen);
34437317Skarels 			m->m_len += len;
34537317Skarels 		} else {
34637317Skarels 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
347*69640Skarels 			if (m->m_next == 0) {
348*69640Skarels 				(void) m_free(m);
349*69640Skarels 				error = ENOBUFS;
350*69640Skarels 				goto out;
351*69640Skarels 			}
35237317Skarels 		}
35344380Skarels #endif
35444380Skarels 		/*
35544380Skarels 		 * If we're sending everything we've got, set PUSH.
35644380Skarels 		 * (This will keep happy those implementations which only
35744380Skarels 		 * give data to the user when a buffer fills or
35844380Skarels 		 * a PUSH comes in.)
35944380Skarels 		 */
36044380Skarels 		if (off + len == so->so_snd.sb_cc)
36144380Skarels 			flags |= TH_PUSH;
36244380Skarels 	} else {
36344380Skarels 		if (tp->t_flags & TF_ACKNOW)
36444380Skarels 			tcpstat.tcps_sndacks++;
36544380Skarels 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
36644380Skarels 			tcpstat.tcps_sndctrl++;
36744380Skarels 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
36844380Skarels 			tcpstat.tcps_sndurg++;
36944380Skarels 		else
37044380Skarels 			tcpstat.tcps_sndwinup++;
37131442Skarels 
37244380Skarels 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
37344380Skarels 		if (m == NULL) {
37444380Skarels 			error = ENOBUFS;
37544380Skarels 			goto out;
37644380Skarels 		}
37744380Skarels 		m->m_data += max_linkhdr;
37844380Skarels 		m->m_len = hdrlen;
37944380Skarels 	}
38044380Skarels 	m->m_pkthdr.rcvif = (struct ifnet *)0;
38144380Skarels 	ti = mtod(m, struct tcpiphdr *);
3825075Swnj 	if (tp->t_template == 0)
3835075Swnj 		panic("tcp_output");
3845110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
3855075Swnj 
3865075Swnj 	/*
3875075Swnj 	 * Fill in fields, remembering maximum advertised
3885075Swnj 	 * window for use in delaying messages about window sizes.
38927067Skarels 	 * If resending a FIN, be sure not to use a new sequence number.
3905075Swnj 	 */
39131725Skarels 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
39231725Skarels 	    tp->snd_nxt == tp->snd_max)
39327067Skarels 		tp->snd_nxt--;
39464175Smckusick 	/*
39564175Smckusick 	 * If we are doing retransmissions, then snd_nxt will
39664175Smckusick 	 * not reflect the first unsent octet.  For ACK only
39764175Smckusick 	 * packets, we do not want the sequence number of the
39864175Smckusick 	 * retransmitted packet, we want the sequence number
39964175Smckusick 	 * of the next unsent octet.  So, if there is no data
40064175Smckusick 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
40164175Smckusick 	 * when filling in ti_seq.  But if we are in persist
40264175Smckusick 	 * state, snd_max might reflect one byte beyond the
40364175Smckusick 	 * right edge of the window, so use snd_nxt in that
40464175Smckusick 	 * case, since we know we aren't doing a retransmission.
40564175Smckusick 	 * (retransmit and persist are mutually exclusive...)
40664175Smckusick 	 */
40764175Smckusick 	if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
40864175Smckusick 		ti->ti_seq = htonl(tp->snd_nxt);
40964175Smckusick 	else
41064175Smckusick 		ti->ti_seq = htonl(tp->snd_max);
41125940Skarels 	ti->ti_ack = htonl(tp->rcv_nxt);
41244380Skarels 	if (optlen) {
41344380Skarels 		bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
4145441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
4155088Swnj 	}
4165088Swnj 	ti->ti_flags = flags;
41725940Skarels 	/*
41825940Skarels 	 * Calculate receive window.  Don't shrink window,
41925940Skarels 	 * but avoid silly window syndrome.
42025940Skarels 	 */
42133783Skarels 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
42225940Skarels 		win = 0;
42357433Sandrew 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
42457433Sandrew 		win = (long)TCP_MAXWIN << tp->rcv_scale;
42536777Skarels 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
42636777Skarels 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
42757433Sandrew 	ti->ti_win = htons((u_short) (win>>tp->rcv_scale));
4285088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
42926387Skarels 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
4305075Swnj 		ti->ti_flags |= TH_URG;
4315075Swnj 	} else
4325075Swnj 		/*
4335075Swnj 		 * If no urgent pointer to send, then we pull
4345075Swnj 		 * the urgent pointer to the left edge of the send window
4355075Swnj 		 * so that it doesn't drift into the send window on sequence
4365075Swnj 		 * number wraparound.
4375075Swnj 		 */
4385088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
4395075Swnj 
4405075Swnj 	/*
4415075Swnj 	 * Put TCP length in extended header, and then
4425075Swnj 	 * checksum extended header and data.
4435075Swnj 	 */
44425940Skarels 	if (len + optlen)
44544380Skarels 		ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
44625940Skarels 		    optlen + len));
44744380Skarels 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
4485075Swnj 
4495075Swnj 	/*
4507125Swnj 	 * In transmit state, time the transmission and arrange for
45121116Skarels 	 * the retransmit.  In persist state, just set snd_max.
4525088Swnj 	 */
45321116Skarels 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
45431758Skarels 		tcp_seq startseq = tp->snd_nxt;
45531758Skarels 
4567125Swnj 		/*
4577146Swnj 		 * Advance snd_nxt over sequence space of this segment.
4587125Swnj 		 */
45944380Skarels 		if (flags & (TH_SYN|TH_FIN)) {
46044380Skarels 			if (flags & TH_SYN)
46144380Skarels 				tp->snd_nxt++;
46244380Skarels 			if (flags & TH_FIN) {
46344380Skarels 				tp->snd_nxt++;
46444380Skarels 				tp->t_flags |= TF_SENTFIN;
46544380Skarels 			}
46627067Skarels 		}
4677125Swnj 		tp->snd_nxt += len;
46831758Skarels 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
4697149Swnj 			tp->snd_max = tp->snd_nxt;
47031758Skarels 			/*
47131758Skarels 			 * Time this transmission if not a retransmission and
47231758Skarels 			 * not currently timing anything.
47331758Skarels 			 */
47431758Skarels 			if (tp->t_rtt == 0) {
47531758Skarels 				tp->t_rtt = 1;
47631758Skarels 				tp->t_rtseq = startseq;
47731758Skarels 				tcpstat.tcps_segstimed++;
47831758Skarels 			}
47931758Skarels 		}
4805088Swnj 
4817125Swnj 		/*
48221116Skarels 		 * Set retransmit timer if not currently set,
48326443Skarels 		 * and not doing an ack or a keep-alive probe.
48431726Skarels 		 * Initial value for retransmit timer is smoothed
48531726Skarels 		 * round-trip time + 2 * round-trip time variance.
48626834Skarels 		 * Initialize shift counter which is used for backoff
48726834Skarels 		 * of retransmit time.
4887125Swnj 		 */
4897125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
4907125Swnj 		    tp->snd_nxt != tp->snd_una) {
49132034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
49232034Skarels 			if (tp->t_timer[TCPT_PERSIST]) {
49332034Skarels 				tp->t_timer[TCPT_PERSIST] = 0;
49432034Skarels 				tp->t_rxtshift = 0;
49532034Skarels 			}
4967125Swnj 		}
49726443Skarels 	} else
49825940Skarels 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
49925940Skarels 			tp->snd_max = tp->snd_nxt + len;
5005163Swnj 
5015163Swnj 	/*
5025268Sroot 	 * Trace.
5035268Sroot 	 */
5047146Swnj 	if (so->so_options & SO_DEBUG)
5055268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
5065268Sroot 
5075268Sroot 	/*
5085075Swnj 	 * Fill in IP length and desired time to live and
50944380Skarels 	 * send to IP level.  There should be a better way
51044380Skarels 	 * to handle ttl and tos; we could keep them in
51144380Skarels 	 * the template, but need a way to checksum without them.
5125075Swnj 	 */
51344380Skarels 	m->m_pkthdr.len = hdrlen + len;
51457947Ssklower #ifdef TUBA
51557947Ssklower 	if (tp->t_tuba_pcb)
51657947Ssklower 		error = tuba_output(m, tp);
51757947Ssklower 	else
51857947Ssklower #endif
51957947Ssklower     {
52044380Skarels 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
52144380Skarels 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
52244380Skarels 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
52344380Skarels #if BSD >= 43
52426059Skarels 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
52557433Sandrew 	    so->so_options & SO_DONTROUTE, 0);
52644380Skarels #else
52744380Skarels 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
52844380Skarels 	    so->so_options & SO_DONTROUTE);
52944380Skarels #endif
53057947Ssklower     }
53133446Skarels 	if (error) {
53244380Skarels out:
53333446Skarels 		if (error == ENOBUFS) {
53461335Sbostic 			tcp_quench(tp->t_inpcb, 0);
53533446Skarels 			return (0);
53633446Skarels 		}
53744380Skarels 		if ((error == EHOSTUNREACH || error == ENETDOWN)
53844380Skarels 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
53944380Skarels 			tp->t_softerror = error;
54044380Skarels 			return (0);
54144380Skarels 		}
5426505Ssam 		return (error);
54333446Skarels 	}
54431442Skarels 	tcpstat.tcps_sndtotal++;
5455075Swnj 
5465075Swnj 	/*
5475075Swnj 	 * Data sent (as far as we can tell).
5485075Swnj 	 * If this advertises a larger window than any other segment,
5495245Sroot 	 * then remember the size of the advertised window.
55025940Skarels 	 * Any pending ACK has now been sent.
5515075Swnj 	 */
5525252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
5535075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
55457433Sandrew 	tp->last_ack_sent = tp->rcv_nxt;
5555088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
55625940Skarels 	if (sendalot)
5577125Swnj 		goto again;
5586505Ssam 	return (0);
5594677Swnj }
5607125Swnj 
56161335Sbostic void
tcp_setpersist(tp)5627125Swnj tcp_setpersist(tp)
5637125Swnj 	register struct tcpcb *tp;
5647125Swnj {
56531726Skarels 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
5667125Swnj 
5677125Swnj 	if (tp->t_timer[TCPT_REXMT])
5687125Swnj 		panic("tcp_output REXMT");
5697125Swnj 	/*
5707125Swnj 	 * Start/restart persistance timer.
5717125Swnj 	 */
5727125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
57331726Skarels 	    t * tcp_backoff[tp->t_rxtshift],
57431725Skarels 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
57531725Skarels 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
57631725Skarels 		tp->t_rxtshift++;
5777125Swnj }
578