xref: /csrg-svn/sys/netinet/tcp_output.c (revision 65262)
123191Smckusick /*
263218Sbostic  * Copyright (c) 1982, 1986, 1988, 1990, 1993
363218Sbostic  *	The Regents of the University of California.  All rights reserved.
423191Smckusick  *
544486Sbostic  * %sccs.include.redist.c%
632788Sbostic  *
7*65262Smckusick  *	@(#)tcp_output.c	8.3 (Berkeley) 12/30/93
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
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 
77*65262Smckusick 	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) {
85*65262Smckusick 		if (win == 0) {
86*65262Smckusick 			/*
87*65262Smckusick 			 * If we still have some data to send, then
88*65262Smckusick 			 * clear the FIN bit.  Usually this would
89*65262Smckusick 			 * happen below when it realizes that we
90*65262Smckusick 			 * aren't sending all the data.  However,
91*65262Smckusick 			 * if we have exactly 1 byte of unset data,
92*65262Smckusick 			 * then it won't clear the FIN bit below,
93*65262Smckusick 			 * and if we are in persist state, we wind
94*65262Smckusick 			 * up sending the packet without recording
95*65262Smckusick 			 * that we sent the FIN bit.
96*65262Smckusick 			 *
97*65262Smckusick 			 * We can't just blindly clear the FIN bit,
98*65262Smckusick 			 * because if we don't have any more data
99*65262Smckusick 			 * to send then the probe will be the FIN
100*65262Smckusick 			 * itself.
101*65262Smckusick 			 */
102*65262Smckusick 			if (off < so->so_snd.sb_cc)
103*65262Smckusick 				flags &= ~TH_FIN;
10421116Skarels 			win = 1;
105*65262Smckusick 		} 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 	 */
29557433Sandrew 	 if (len > tp->t_maxseg - optlen) {
29657433Sandrew 		len = tp->t_maxseg - optlen;
29757433Sandrew 		sendalot = 1;
29857433Sandrew 	 }
29957433Sandrew 
30057433Sandrew 
30144380Skarels #ifdef DIAGNOSTIC
30257433Sandrew  	if (max_linkhdr + hdrlen > MHLEN)
30357433Sandrew 		panic("tcphdr too big");
30444380Skarels #endif
30544380Skarels 
30644380Skarels 	/*
3075075Swnj 	 * Grab a header mbuf, attaching a copy of data to
3085075Swnj 	 * be transmitted, and initialize the header from
3095075Swnj 	 * the template for sends on this connection.
3105075Swnj 	 */
3115075Swnj 	if (len) {
31231442Skarels 		if (tp->t_force && len == 1)
31331442Skarels 			tcpstat.tcps_sndprobe++;
31431442Skarels 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
31531442Skarels 			tcpstat.tcps_sndrexmitpack++;
31631442Skarels 			tcpstat.tcps_sndrexmitbyte += len;
31731442Skarels 		} else {
31831442Skarels 			tcpstat.tcps_sndpack++;
31931442Skarels 			tcpstat.tcps_sndbyte += len;
32031442Skarels 		}
32144380Skarels #ifdef notyet
32244380Skarels 		if ((m = m_copypack(so->so_snd.sb_mb, off,
32344380Skarels 		    (int)len, max_linkhdr + hdrlen)) == 0) {
32444380Skarels 			error = ENOBUFS;
32544380Skarels 			goto out;
32644380Skarels 		}
32744380Skarels 		/*
32844380Skarels 		 * m_copypack left space for our hdr; use it.
32944380Skarels 		 */
33044380Skarels 		m->m_len += hdrlen;
33144380Skarels 		m->m_data -= hdrlen;
33244380Skarels #else
33344380Skarels 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
33444380Skarels 		if (m == NULL) {
33544380Skarels 			error = ENOBUFS;
33644380Skarels 			goto out;
33744380Skarels 		}
33844380Skarels 		m->m_data += max_linkhdr;
33944380Skarels 		m->m_len = hdrlen;
34044380Skarels 		if (len <= MHLEN - hdrlen - max_linkhdr) {
34137317Skarels 			m_copydata(so->so_snd.sb_mb, off, (int) len,
34244380Skarels 			    mtod(m, caddr_t) + hdrlen);
34337317Skarels 			m->m_len += len;
34437317Skarels 		} else {
34537317Skarels 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
34637317Skarels 			if (m->m_next == 0)
34737317Skarels 				len = 0;
34837317Skarels 		}
34944380Skarels #endif
35044380Skarels 		/*
35144380Skarels 		 * If we're sending everything we've got, set PUSH.
35244380Skarels 		 * (This will keep happy those implementations which only
35344380Skarels 		 * give data to the user when a buffer fills or
35444380Skarels 		 * a PUSH comes in.)
35544380Skarels 		 */
35644380Skarels 		if (off + len == so->so_snd.sb_cc)
35744380Skarels 			flags |= TH_PUSH;
35844380Skarels 	} else {
35944380Skarels 		if (tp->t_flags & TF_ACKNOW)
36044380Skarels 			tcpstat.tcps_sndacks++;
36144380Skarels 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
36244380Skarels 			tcpstat.tcps_sndctrl++;
36344380Skarels 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
36444380Skarels 			tcpstat.tcps_sndurg++;
36544380Skarels 		else
36644380Skarels 			tcpstat.tcps_sndwinup++;
36731442Skarels 
36844380Skarels 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
36944380Skarels 		if (m == NULL) {
37044380Skarels 			error = ENOBUFS;
37144380Skarels 			goto out;
37244380Skarels 		}
37344380Skarels 		m->m_data += max_linkhdr;
37444380Skarels 		m->m_len = hdrlen;
37544380Skarels 	}
37644380Skarels 	m->m_pkthdr.rcvif = (struct ifnet *)0;
37744380Skarels 	ti = mtod(m, struct tcpiphdr *);
3785075Swnj 	if (tp->t_template == 0)
3795075Swnj 		panic("tcp_output");
3805110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
3815075Swnj 
3825075Swnj 	/*
3835075Swnj 	 * Fill in fields, remembering maximum advertised
3845075Swnj 	 * window for use in delaying messages about window sizes.
38527067Skarels 	 * If resending a FIN, be sure not to use a new sequence number.
3865075Swnj 	 */
38731725Skarels 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
38831725Skarels 	    tp->snd_nxt == tp->snd_max)
38927067Skarels 		tp->snd_nxt--;
39064175Smckusick 	/*
39164175Smckusick 	 * If we are doing retransmissions, then snd_nxt will
39264175Smckusick 	 * not reflect the first unsent octet.  For ACK only
39364175Smckusick 	 * packets, we do not want the sequence number of the
39464175Smckusick 	 * retransmitted packet, we want the sequence number
39564175Smckusick 	 * of the next unsent octet.  So, if there is no data
39664175Smckusick 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
39764175Smckusick 	 * when filling in ti_seq.  But if we are in persist
39864175Smckusick 	 * state, snd_max might reflect one byte beyond the
39964175Smckusick 	 * right edge of the window, so use snd_nxt in that
40064175Smckusick 	 * case, since we know we aren't doing a retransmission.
40164175Smckusick 	 * (retransmit and persist are mutually exclusive...)
40264175Smckusick 	 */
40364175Smckusick 	if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
40464175Smckusick 		ti->ti_seq = htonl(tp->snd_nxt);
40564175Smckusick 	else
40664175Smckusick 		ti->ti_seq = htonl(tp->snd_max);
40725940Skarels 	ti->ti_ack = htonl(tp->rcv_nxt);
40844380Skarels 	if (optlen) {
40944380Skarels 		bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
4105441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
4115088Swnj 	}
4125088Swnj 	ti->ti_flags = flags;
41325940Skarels 	/*
41425940Skarels 	 * Calculate receive window.  Don't shrink window,
41525940Skarels 	 * but avoid silly window syndrome.
41625940Skarels 	 */
41733783Skarels 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
41825940Skarels 		win = 0;
41957433Sandrew 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
42057433Sandrew 		win = (long)TCP_MAXWIN << tp->rcv_scale;
42136777Skarels 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
42236777Skarels 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
42357433Sandrew 	ti->ti_win = htons((u_short) (win>>tp->rcv_scale));
4245088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
42526387Skarels 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
4265075Swnj 		ti->ti_flags |= TH_URG;
4275075Swnj 	} else
4285075Swnj 		/*
4295075Swnj 		 * If no urgent pointer to send, then we pull
4305075Swnj 		 * the urgent pointer to the left edge of the send window
4315075Swnj 		 * so that it doesn't drift into the send window on sequence
4325075Swnj 		 * number wraparound.
4335075Swnj 		 */
4345088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
4355075Swnj 
4365075Swnj 	/*
4375075Swnj 	 * Put TCP length in extended header, and then
4385075Swnj 	 * checksum extended header and data.
4395075Swnj 	 */
44025940Skarels 	if (len + optlen)
44144380Skarels 		ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
44225940Skarels 		    optlen + len));
44344380Skarels 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
4445075Swnj 
4455075Swnj 	/*
4467125Swnj 	 * In transmit state, time the transmission and arrange for
44721116Skarels 	 * the retransmit.  In persist state, just set snd_max.
4485088Swnj 	 */
44921116Skarels 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
45031758Skarels 		tcp_seq startseq = tp->snd_nxt;
45131758Skarels 
4527125Swnj 		/*
4537146Swnj 		 * Advance snd_nxt over sequence space of this segment.
4547125Swnj 		 */
45544380Skarels 		if (flags & (TH_SYN|TH_FIN)) {
45644380Skarels 			if (flags & TH_SYN)
45744380Skarels 				tp->snd_nxt++;
45844380Skarels 			if (flags & TH_FIN) {
45944380Skarels 				tp->snd_nxt++;
46044380Skarels 				tp->t_flags |= TF_SENTFIN;
46144380Skarels 			}
46227067Skarels 		}
4637125Swnj 		tp->snd_nxt += len;
46431758Skarels 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
4657149Swnj 			tp->snd_max = tp->snd_nxt;
46631758Skarels 			/*
46731758Skarels 			 * Time this transmission if not a retransmission and
46831758Skarels 			 * not currently timing anything.
46931758Skarels 			 */
47031758Skarels 			if (tp->t_rtt == 0) {
47131758Skarels 				tp->t_rtt = 1;
47231758Skarels 				tp->t_rtseq = startseq;
47331758Skarels 				tcpstat.tcps_segstimed++;
47431758Skarels 			}
47531758Skarels 		}
4765088Swnj 
4777125Swnj 		/*
47821116Skarels 		 * Set retransmit timer if not currently set,
47926443Skarels 		 * and not doing an ack or a keep-alive probe.
48031726Skarels 		 * Initial value for retransmit timer is smoothed
48131726Skarels 		 * round-trip time + 2 * round-trip time variance.
48226834Skarels 		 * Initialize shift counter which is used for backoff
48326834Skarels 		 * of retransmit time.
4847125Swnj 		 */
4857125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
4867125Swnj 		    tp->snd_nxt != tp->snd_una) {
48732034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
48832034Skarels 			if (tp->t_timer[TCPT_PERSIST]) {
48932034Skarels 				tp->t_timer[TCPT_PERSIST] = 0;
49032034Skarels 				tp->t_rxtshift = 0;
49132034Skarels 			}
4927125Swnj 		}
49326443Skarels 	} else
49425940Skarels 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
49525940Skarels 			tp->snd_max = tp->snd_nxt + len;
4965163Swnj 
4975163Swnj 	/*
4985268Sroot 	 * Trace.
4995268Sroot 	 */
5007146Swnj 	if (so->so_options & SO_DEBUG)
5015268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
5025268Sroot 
5035268Sroot 	/*
5045075Swnj 	 * Fill in IP length and desired time to live and
50544380Skarels 	 * send to IP level.  There should be a better way
50644380Skarels 	 * to handle ttl and tos; we could keep them in
50744380Skarels 	 * the template, but need a way to checksum without them.
5085075Swnj 	 */
50944380Skarels 	m->m_pkthdr.len = hdrlen + len;
51057947Ssklower #ifdef TUBA
51157947Ssklower 	if (tp->t_tuba_pcb)
51257947Ssklower 		error = tuba_output(m, tp);
51357947Ssklower 	else
51457947Ssklower #endif
51557947Ssklower     {
51644380Skarels 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
51744380Skarels 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
51844380Skarels 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
51944380Skarels #if BSD >= 43
52026059Skarels 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
52157433Sandrew 	    so->so_options & SO_DONTROUTE, 0);
52244380Skarels #else
52344380Skarels 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
52444380Skarels 	    so->so_options & SO_DONTROUTE);
52544380Skarels #endif
52657947Ssklower     }
52733446Skarels 	if (error) {
52844380Skarels out:
52933446Skarels 		if (error == ENOBUFS) {
53061335Sbostic 			tcp_quench(tp->t_inpcb, 0);
53133446Skarels 			return (0);
53233446Skarels 		}
53344380Skarels 		if ((error == EHOSTUNREACH || error == ENETDOWN)
53444380Skarels 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
53544380Skarels 			tp->t_softerror = error;
53644380Skarels 			return (0);
53744380Skarels 		}
5386505Ssam 		return (error);
53933446Skarels 	}
54031442Skarels 	tcpstat.tcps_sndtotal++;
5415075Swnj 
5425075Swnj 	/*
5435075Swnj 	 * Data sent (as far as we can tell).
5445075Swnj 	 * If this advertises a larger window than any other segment,
5455245Sroot 	 * then remember the size of the advertised window.
54625940Skarels 	 * Any pending ACK has now been sent.
5475075Swnj 	 */
5485252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
5495075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
55057433Sandrew 	tp->last_ack_sent = tp->rcv_nxt;
5515088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
55225940Skarels 	if (sendalot)
5537125Swnj 		goto again;
5546505Ssam 	return (0);
5554677Swnj }
5567125Swnj 
55761335Sbostic void
5587125Swnj tcp_setpersist(tp)
5597125Swnj 	register struct tcpcb *tp;
5607125Swnj {
56131726Skarels 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
5627125Swnj 
5637125Swnj 	if (tp->t_timer[TCPT_REXMT])
5647125Swnj 		panic("tcp_output REXMT");
5657125Swnj 	/*
5667125Swnj 	 * Start/restart persistance timer.
5677125Swnj 	 */
5687125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
56931726Skarels 	    t * tcp_backoff[tp->t_rxtshift],
57031725Skarels 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
57131725Skarels 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
57231725Skarels 		tp->t_rxtshift++;
5737125Swnj }
574