xref: /csrg-svn/sys/netinet/tcp_output.c (revision 61335)
123191Smckusick /*
244380Skarels  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
332788Sbostic  * All rights reserved.
423191Smckusick  *
544486Sbostic  * %sccs.include.redist.c%
632788Sbostic  *
7*61335Sbostic  *	@(#)tcp_output.c	7.29 (Berkeley) 06/04/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  */
45*61335Sbostic 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 
7721116Skarels 	/*
7821116Skarels 	 * If in persist timeout with window of 0, send 1 byte.
7925940Skarels 	 * Otherwise, if window is small but nonzero
8025940Skarels 	 * and timer expired, we will send what we can
8125940Skarels 	 * and go to transmit state.
8221116Skarels 	 */
8321116Skarels 	if (tp->t_force) {
8426834Skarels 		if (win == 0)
8521116Skarels 			win = 1;
8621116Skarels 		else {
8721116Skarels 			tp->t_timer[TCPT_PERSIST] = 0;
8821116Skarels 			tp->t_rxtshift = 0;
8921116Skarels 		}
9021116Skarels 	}
9125940Skarels 
9244380Skarels 	flags = tcp_outflags[tp->t_state];
9337317Skarels 	len = min(so->so_snd.sb_cc, win) - off;
9425940Skarels 
9526834Skarels 	if (len < 0) {
9626834Skarels 		/*
9727048Skarels 		 * If FIN has been sent but not acked,
9827048Skarels 		 * but we haven't been called to retransmit,
9932785Skarels 		 * len will be -1.  Otherwise, window shrank
10032785Skarels 		 * after we sent into it.  If window shrank to 0,
10132785Skarels 		 * cancel pending retransmit and pull snd_nxt
10232785Skarels 		 * back to (closed) window.  We will enter persist
10332785Skarels 		 * state below.  If the window didn't close completely,
10427048Skarels 		 * just wait for an ACK.
10526834Skarels 		 */
10632785Skarels 		len = 0;
10732785Skarels 		if (win == 0) {
10827067Skarels 			tp->t_timer[TCPT_REXMT] = 0;
10927067Skarels 			tp->snd_nxt = tp->snd_una;
11032785Skarels 		}
11126834Skarels 	}
11227067Skarels 	if (len > tp->t_maxseg) {
11327067Skarels 		len = tp->t_maxseg;
11431442Skarels 		sendalot = 1;
11527067Skarels 	}
11629795Skarels 	if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
11729795Skarels 		flags &= ~TH_FIN;
11844380Skarels 
11926834Skarels 	win = sbspace(&so->so_rcv);
12026834Skarels 
12125940Skarels 	/*
12217318Skarels 	 * Sender silly window avoidance.  If connection is idle
12317318Skarels 	 * and can send all data, a maximum segment,
12417318Skarels 	 * at least a maximum default-size segment do it,
1256279Swnj 	 * or are forced, do it; otherwise don't bother.
12625261Skarels 	 * If peer's buffer is tiny, then send
12725261Skarels 	 * when window is at least half open.
12821116Skarels 	 * If retransmitting (possibly after persist timer forced us
12921116Skarels 	 * to send into a small window), then must resend.
1306279Swnj 	 */
1316279Swnj 	if (len) {
13231725Skarels 		if (len == tp->t_maxseg)
1336279Swnj 			goto send;
13425940Skarels 		if ((idle || tp->t_flags & TF_NODELAY) &&
13525940Skarels 		    len + off >= so->so_snd.sb_cc)
1366279Swnj 			goto send;
1376279Swnj 		if (tp->t_force)
1386279Swnj 			goto send;
13925261Skarels 		if (len >= tp->max_sndwnd / 2)
14025261Skarels 			goto send;
14121116Skarels 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
14221116Skarels 			goto send;
14326834Skarels 	}
1446279Swnj 
1455441Swnj 	/*
14625940Skarels 	 * Compare available window to amount of window
14725940Skarels 	 * known to peer (as advertised window less
14832785Skarels 	 * next expected input).  If the difference is at least two
14940685Skarels 	 * max size segments, or at least 50% of the maximum possible
15032785Skarels 	 * window, then want to send a window update to peer.
1515075Swnj 	 */
15232034Skarels 	if (win > 0) {
15357433Sandrew 		/*
15457433Sandrew 		 * "adv" is the amount we can increase the window,
15557433Sandrew 		 * taking into account that we are limited by
15657433Sandrew 		 * TCP_MAXWIN << tp->rcv_scale.
15757433Sandrew 		 */
15857433Sandrew 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
15957433Sandrew 			(tp->rcv_adv - tp->rcv_nxt);
1604678Swnj 
16145169Skarels 		if (adv >= (long) (2 * tp->t_maxseg))
16232785Skarels 			goto send;
16345169Skarels 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
16432034Skarels 			goto send;
16532034Skarels 	}
16632034Skarels 
1675075Swnj 	/*
16844380Skarels 	 * Send if we owe peer an ACK.
16944380Skarels 	 */
17044380Skarels 	if (tp->t_flags & TF_ACKNOW)
17144380Skarels 		goto send;
17244380Skarels 	if (flags & (TH_SYN|TH_RST))
17344380Skarels 		goto send;
17444380Skarels 	if (SEQ_GT(tp->snd_up, tp->snd_una))
17544380Skarels 		goto send;
17644380Skarels 	/*
17744380Skarels 	 * If our state indicates that FIN should be sent
17844380Skarels 	 * and we have not yet done so, or we're retransmitting the FIN,
17944380Skarels 	 * then we need to send.
18044380Skarels 	 */
18144380Skarels 	if (flags & TH_FIN &&
18244380Skarels 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
18344380Skarels 		goto send;
18444380Skarels 
18544380Skarels 	/*
1867125Swnj 	 * TCP window updates are not reliable, rather a polling protocol
1877125Swnj 	 * using ``persist'' packets is used to insure receipt of window
1887125Swnj 	 * updates.  The three ``states'' for the output side are:
1897125Swnj 	 *	idle			not doing retransmits or persists
19025940Skarels 	 *	persisting		to move a small or zero window
1917125Swnj 	 *	(re)transmitting	and thereby not persisting
1927125Swnj 	 *
1937125Swnj 	 * tp->t_timer[TCPT_PERSIST]
1947125Swnj 	 *	is set when we are in persist state.
1957125Swnj 	 * tp->t_force
1967125Swnj 	 *	is set when we are called to send a persist packet.
1977125Swnj 	 * tp->t_timer[TCPT_REXMT]
1987125Swnj 	 *	is set when we are retransmitting
1997125Swnj 	 * The output side is idle when both timers are zero.
2007125Swnj 	 *
20121116Skarels 	 * If send window is too small, there is data to transmit, and no
20221116Skarels 	 * retransmit or persist is pending, then go to persist state.
20321116Skarels 	 * If nothing happens soon, send when timer expires:
20421116Skarels 	 * if window is nonzero, transmit what we can,
20521116Skarels 	 * otherwise force out a byte.
2067125Swnj 	 */
20721116Skarels 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
20821116Skarels 	    tp->t_timer[TCPT_PERSIST] == 0) {
2097125Swnj 		tp->t_rxtshift = 0;
2107125Swnj 		tcp_setpersist(tp);
2117125Swnj 	}
2127125Swnj 
2137125Swnj 	/*
2145075Swnj 	 * No reason to send a segment, just return.
2155075Swnj 	 */
2165110Swnj 	return (0);
2174678Swnj 
2185075Swnj send:
2195075Swnj 	/*
22044380Skarels 	 * Before ESTABLISHED, force sending of initial options
22144380Skarels 	 * unless TCP set not to do any options.
22244380Skarels 	 * NOTE: we assume that the IP/TCP header plus TCP options
22344380Skarels 	 * always fit in a single mbuf, leaving room for a maximum
22444380Skarels 	 * link header, i.e.
22544380Skarels 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
22644380Skarels 	 */
22744380Skarels 	optlen = 0;
22844380Skarels 	hdrlen = sizeof (struct tcpiphdr);
22959038Skarels 	if (flags & TH_SYN) {
23059038Skarels 		tp->snd_nxt = tp->iss;
23159038Skarels 		if ((tp->t_flags & TF_NOOPT) == 0) {
23259038Skarels 			u_short mss;
23352515Storek 
23459038Skarels 			opt[0] = TCPOPT_MAXSEG;
23559038Skarels 			opt[1] = 4;
23659038Skarels 			mss = htons((u_short) tcp_mss(tp, 0));
23759038Skarels 			bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss));
23859038Skarels 			optlen = 4;
23959038Skarels 
24059038Skarels 			if ((tp->t_flags & TF_REQ_SCALE) &&
24159038Skarels 			    ((flags & TH_ACK) == 0 ||
24259038Skarels 			    (tp->t_flags & TF_RCVD_SCALE))) {
24359038Skarels 				*((u_long *) (opt + optlen)) = htonl(
24459038Skarels 					TCPOPT_NOP << 24 |
24559038Skarels 					TCPOPT_WINDOW << 16 |
24659038Skarels 					TCPOLEN_WINDOW << 8 |
24759038Skarels 					tp->request_r_scale);
24859038Skarels 				optlen += 4;
24959038Skarels 			}
25059038Skarels 		}
25157433Sandrew  	}
25257433Sandrew 
25357433Sandrew  	/*
25457433Sandrew 	 * Send a timestamp and echo-reply if this is a SYN and our side
25557433Sandrew 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
25657433Sandrew 	 * and our peer have sent timestamps in our SYN's.
25757433Sandrew  	 */
25857433Sandrew  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
25957433Sandrew  	     (flags & TH_RST) == 0 &&
26057433Sandrew  	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
26157433Sandrew 	     (tp->t_flags & TF_RCVD_TSTMP))) {
26257433Sandrew 		u_long *lp = (u_long *)(opt + optlen);
26357433Sandrew 
26457433Sandrew  		/* Form timestamp option as shown in appendix A of RFC 1323. */
26557433Sandrew  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
26657433Sandrew  		*lp++ = htonl(tcp_now);
26757433Sandrew  		*lp   = htonl(tp->ts_recent);
26857433Sandrew  		optlen += TCPOLEN_TSTAMP_APPA;
26957433Sandrew  	}
27057666Sandrew 
27157666Sandrew  	hdrlen += optlen;
27257433Sandrew 
27357433Sandrew 	/*
27457433Sandrew 	 * Adjust data length if insertion of options will
27557433Sandrew 	 * bump the packet length beyond the t_maxseg length.
27657433Sandrew 	 */
27757433Sandrew 	 if (len > tp->t_maxseg - optlen) {
27857433Sandrew 		len = tp->t_maxseg - optlen;
27957433Sandrew 		sendalot = 1;
28057433Sandrew 	 }
28157433Sandrew 
28257433Sandrew 
28344380Skarels #ifdef DIAGNOSTIC
28457433Sandrew  	if (max_linkhdr + hdrlen > MHLEN)
28557433Sandrew 		panic("tcphdr too big");
28644380Skarels #endif
28744380Skarels 
28844380Skarels 	/*
2895075Swnj 	 * Grab a header mbuf, attaching a copy of data to
2905075Swnj 	 * be transmitted, and initialize the header from
2915075Swnj 	 * the template for sends on this connection.
2925075Swnj 	 */
2935075Swnj 	if (len) {
29431442Skarels 		if (tp->t_force && len == 1)
29531442Skarels 			tcpstat.tcps_sndprobe++;
29631442Skarels 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
29731442Skarels 			tcpstat.tcps_sndrexmitpack++;
29831442Skarels 			tcpstat.tcps_sndrexmitbyte += len;
29931442Skarels 		} else {
30031442Skarels 			tcpstat.tcps_sndpack++;
30131442Skarels 			tcpstat.tcps_sndbyte += len;
30231442Skarels 		}
30344380Skarels #ifdef notyet
30444380Skarels 		if ((m = m_copypack(so->so_snd.sb_mb, off,
30544380Skarels 		    (int)len, max_linkhdr + hdrlen)) == 0) {
30644380Skarels 			error = ENOBUFS;
30744380Skarels 			goto out;
30844380Skarels 		}
30944380Skarels 		/*
31044380Skarels 		 * m_copypack left space for our hdr; use it.
31144380Skarels 		 */
31244380Skarels 		m->m_len += hdrlen;
31344380Skarels 		m->m_data -= hdrlen;
31444380Skarels #else
31544380Skarels 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
31644380Skarels 		if (m == NULL) {
31744380Skarels 			error = ENOBUFS;
31844380Skarels 			goto out;
31944380Skarels 		}
32044380Skarels 		m->m_data += max_linkhdr;
32144380Skarels 		m->m_len = hdrlen;
32244380Skarels 		if (len <= MHLEN - hdrlen - max_linkhdr) {
32337317Skarels 			m_copydata(so->so_snd.sb_mb, off, (int) len,
32444380Skarels 			    mtod(m, caddr_t) + hdrlen);
32537317Skarels 			m->m_len += len;
32637317Skarels 		} else {
32737317Skarels 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
32837317Skarels 			if (m->m_next == 0)
32937317Skarels 				len = 0;
33037317Skarels 		}
33144380Skarels #endif
33244380Skarels 		/*
33344380Skarels 		 * If we're sending everything we've got, set PUSH.
33444380Skarels 		 * (This will keep happy those implementations which only
33544380Skarels 		 * give data to the user when a buffer fills or
33644380Skarels 		 * a PUSH comes in.)
33744380Skarels 		 */
33844380Skarels 		if (off + len == so->so_snd.sb_cc)
33944380Skarels 			flags |= TH_PUSH;
34044380Skarels 	} else {
34144380Skarels 		if (tp->t_flags & TF_ACKNOW)
34244380Skarels 			tcpstat.tcps_sndacks++;
34344380Skarels 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
34444380Skarels 			tcpstat.tcps_sndctrl++;
34544380Skarels 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
34644380Skarels 			tcpstat.tcps_sndurg++;
34744380Skarels 		else
34844380Skarels 			tcpstat.tcps_sndwinup++;
34931442Skarels 
35044380Skarels 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
35144380Skarels 		if (m == NULL) {
35244380Skarels 			error = ENOBUFS;
35344380Skarels 			goto out;
35444380Skarels 		}
35544380Skarels 		m->m_data += max_linkhdr;
35644380Skarels 		m->m_len = hdrlen;
35744380Skarels 	}
35844380Skarels 	m->m_pkthdr.rcvif = (struct ifnet *)0;
35944380Skarels 	ti = mtod(m, struct tcpiphdr *);
3605075Swnj 	if (tp->t_template == 0)
3615075Swnj 		panic("tcp_output");
3625110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
3635075Swnj 
3645075Swnj 	/*
3655075Swnj 	 * Fill in fields, remembering maximum advertised
3665075Swnj 	 * window for use in delaying messages about window sizes.
36727067Skarels 	 * If resending a FIN, be sure not to use a new sequence number.
3685075Swnj 	 */
36931725Skarels 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
37031725Skarels 	    tp->snd_nxt == tp->snd_max)
37127067Skarels 		tp->snd_nxt--;
37225940Skarels 	ti->ti_seq = htonl(tp->snd_nxt);
37325940Skarels 	ti->ti_ack = htonl(tp->rcv_nxt);
37444380Skarels 	if (optlen) {
37544380Skarels 		bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
3765441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
3775088Swnj 	}
3785088Swnj 	ti->ti_flags = flags;
37925940Skarels 	/*
38025940Skarels 	 * Calculate receive window.  Don't shrink window,
38125940Skarels 	 * but avoid silly window syndrome.
38225940Skarels 	 */
38333783Skarels 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
38425940Skarels 		win = 0;
38557433Sandrew 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
38657433Sandrew 		win = (long)TCP_MAXWIN << tp->rcv_scale;
38736777Skarels 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
38836777Skarels 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
38957433Sandrew 	ti->ti_win = htons((u_short) (win>>tp->rcv_scale));
3905088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
39126387Skarels 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
3925075Swnj 		ti->ti_flags |= TH_URG;
3935075Swnj 	} else
3945075Swnj 		/*
3955075Swnj 		 * If no urgent pointer to send, then we pull
3965075Swnj 		 * the urgent pointer to the left edge of the send window
3975075Swnj 		 * so that it doesn't drift into the send window on sequence
3985075Swnj 		 * number wraparound.
3995075Swnj 		 */
4005088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
4015075Swnj 
4025075Swnj 	/*
4035075Swnj 	 * Put TCP length in extended header, and then
4045075Swnj 	 * checksum extended header and data.
4055075Swnj 	 */
40625940Skarels 	if (len + optlen)
40744380Skarels 		ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
40825940Skarels 		    optlen + len));
40944380Skarels 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
4105075Swnj 
4115075Swnj 	/*
4127125Swnj 	 * In transmit state, time the transmission and arrange for
41321116Skarels 	 * the retransmit.  In persist state, just set snd_max.
4145088Swnj 	 */
41521116Skarels 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
41631758Skarels 		tcp_seq startseq = tp->snd_nxt;
41731758Skarels 
4187125Swnj 		/*
4197146Swnj 		 * Advance snd_nxt over sequence space of this segment.
4207125Swnj 		 */
42144380Skarels 		if (flags & (TH_SYN|TH_FIN)) {
42244380Skarels 			if (flags & TH_SYN)
42344380Skarels 				tp->snd_nxt++;
42444380Skarels 			if (flags & TH_FIN) {
42544380Skarels 				tp->snd_nxt++;
42644380Skarels 				tp->t_flags |= TF_SENTFIN;
42744380Skarels 			}
42827067Skarels 		}
4297125Swnj 		tp->snd_nxt += len;
43031758Skarels 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
4317149Swnj 			tp->snd_max = tp->snd_nxt;
43231758Skarels 			/*
43331758Skarels 			 * Time this transmission if not a retransmission and
43431758Skarels 			 * not currently timing anything.
43531758Skarels 			 */
43631758Skarels 			if (tp->t_rtt == 0) {
43731758Skarels 				tp->t_rtt = 1;
43831758Skarels 				tp->t_rtseq = startseq;
43931758Skarels 				tcpstat.tcps_segstimed++;
44031758Skarels 			}
44131758Skarels 		}
4425088Swnj 
4437125Swnj 		/*
44421116Skarels 		 * Set retransmit timer if not currently set,
44526443Skarels 		 * and not doing an ack or a keep-alive probe.
44631726Skarels 		 * Initial value for retransmit timer is smoothed
44731726Skarels 		 * round-trip time + 2 * round-trip time variance.
44826834Skarels 		 * Initialize shift counter which is used for backoff
44926834Skarels 		 * of retransmit time.
4507125Swnj 		 */
4517125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
4527125Swnj 		    tp->snd_nxt != tp->snd_una) {
45332034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
45432034Skarels 			if (tp->t_timer[TCPT_PERSIST]) {
45532034Skarels 				tp->t_timer[TCPT_PERSIST] = 0;
45632034Skarels 				tp->t_rxtshift = 0;
45732034Skarels 			}
4587125Swnj 		}
45926443Skarels 	} else
46025940Skarels 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
46125940Skarels 			tp->snd_max = tp->snd_nxt + len;
4625163Swnj 
4635163Swnj 	/*
4645268Sroot 	 * Trace.
4655268Sroot 	 */
4667146Swnj 	if (so->so_options & SO_DEBUG)
4675268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
4685268Sroot 
4695268Sroot 	/*
4705075Swnj 	 * Fill in IP length and desired time to live and
47144380Skarels 	 * send to IP level.  There should be a better way
47244380Skarels 	 * to handle ttl and tos; we could keep them in
47344380Skarels 	 * the template, but need a way to checksum without them.
4745075Swnj 	 */
47544380Skarels 	m->m_pkthdr.len = hdrlen + len;
47657947Ssklower #ifdef TUBA
47757947Ssklower 	if (tp->t_tuba_pcb)
47857947Ssklower 		error = tuba_output(m, tp);
47957947Ssklower 	else
48057947Ssklower #endif
48157947Ssklower     {
48244380Skarels 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
48344380Skarels 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
48444380Skarels 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
48544380Skarels #if BSD >= 43
48626059Skarels 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
48757433Sandrew 	    so->so_options & SO_DONTROUTE, 0);
48844380Skarels #else
48944380Skarels 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
49044380Skarels 	    so->so_options & SO_DONTROUTE);
49144380Skarels #endif
49257947Ssklower     }
49333446Skarels 	if (error) {
49444380Skarels out:
49533446Skarels 		if (error == ENOBUFS) {
496*61335Sbostic 			tcp_quench(tp->t_inpcb, 0);
49733446Skarels 			return (0);
49833446Skarels 		}
49944380Skarels 		if ((error == EHOSTUNREACH || error == ENETDOWN)
50044380Skarels 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
50144380Skarels 			tp->t_softerror = error;
50244380Skarels 			return (0);
50344380Skarels 		}
5046505Ssam 		return (error);
50533446Skarels 	}
50631442Skarels 	tcpstat.tcps_sndtotal++;
5075075Swnj 
5085075Swnj 	/*
5095075Swnj 	 * Data sent (as far as we can tell).
5105075Swnj 	 * If this advertises a larger window than any other segment,
5115245Sroot 	 * then remember the size of the advertised window.
51225940Skarels 	 * Any pending ACK has now been sent.
5135075Swnj 	 */
5145252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
5155075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
51657433Sandrew 	tp->last_ack_sent = tp->rcv_nxt;
5175088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
51825940Skarels 	if (sendalot)
5197125Swnj 		goto again;
5206505Ssam 	return (0);
5214677Swnj }
5227125Swnj 
523*61335Sbostic void
5247125Swnj tcp_setpersist(tp)
5257125Swnj 	register struct tcpcb *tp;
5267125Swnj {
52731726Skarels 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
5287125Swnj 
5297125Swnj 	if (tp->t_timer[TCPT_REXMT])
5307125Swnj 		panic("tcp_output REXMT");
5317125Swnj 	/*
5327125Swnj 	 * Start/restart persistance timer.
5337125Swnj 	 */
5347125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
53531726Skarels 	    t * tcp_backoff[tp->t_rxtshift],
53631725Skarels 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
53731725Skarels 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
53831725Skarels 		tp->t_rxtshift++;
5397125Swnj }
540