xref: /csrg-svn/sys/netinet/tcp_output.c (revision 59038)
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*59038Skarels  *	@(#)tcp_output.c	7.28 (Berkeley) 04/10/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  */
455075Swnj tcp_output(tp)
465075Swnj 	register struct tcpcb *tp;
474678Swnj {
485075Swnj 	register struct socket *so = tp->t_inpcb->inp_socket;
4937317Skarels 	register long len, win;
5025940Skarels 	int off, flags, error;
515075Swnj 	register struct mbuf *m;
525075Swnj 	register struct tcpiphdr *ti;
5357433Sandrew 	u_char opt[MAX_TCPOPTLEN];
5444380Skarels 	unsigned optlen, hdrlen;
5525940Skarels 	int idle, sendalot;
564678Swnj 
575075Swnj 	/*
586279Swnj 	 * Determine length of data that should be transmitted,
595088Swnj 	 * and flags that will be used.
605088Swnj 	 * If there is some data or critical controls (SYN, RST)
615088Swnj 	 * to send, then transmit; otherwise, investigate further.
625075Swnj 	 */
6325940Skarels 	idle = (tp->snd_max == tp->snd_una);
6444380Skarels 	if (idle && tp->t_idle >= tp->t_rxtcur)
6544380Skarels 		/*
6644380Skarels 		 * We have been idle for "a while" and no acks are
6744380Skarels 		 * expected to clock out any data we send --
6844380Skarels 		 * slow start to get ack "clock" running again.
6944380Skarels 		 */
7044380Skarels 		tp->snd_cwnd = tp->t_maxseg;
717125Swnj again:
727125Swnj 	sendalot = 0;
735075Swnj 	off = tp->snd_nxt - tp->snd_una;
7437317Skarels 	win = min(tp->snd_wnd, tp->snd_cwnd);
7526834Skarels 
7621116Skarels 	/*
7721116Skarels 	 * If in persist timeout with window of 0, send 1 byte.
7825940Skarels 	 * Otherwise, if window is small but nonzero
7925940Skarels 	 * and timer expired, we will send what we can
8025940Skarels 	 * and go to transmit state.
8121116Skarels 	 */
8221116Skarels 	if (tp->t_force) {
8326834Skarels 		if (win == 0)
8421116Skarels 			win = 1;
8521116Skarels 		else {
8621116Skarels 			tp->t_timer[TCPT_PERSIST] = 0;
8721116Skarels 			tp->t_rxtshift = 0;
8821116Skarels 		}
8921116Skarels 	}
9025940Skarels 
9144380Skarels 	flags = tcp_outflags[tp->t_state];
9237317Skarels 	len = min(so->so_snd.sb_cc, win) - off;
9325940Skarels 
9426834Skarels 	if (len < 0) {
9526834Skarels 		/*
9627048Skarels 		 * If FIN has been sent but not acked,
9727048Skarels 		 * but we haven't been called to retransmit,
9832785Skarels 		 * len will be -1.  Otherwise, window shrank
9932785Skarels 		 * after we sent into it.  If window shrank to 0,
10032785Skarels 		 * cancel pending retransmit and pull snd_nxt
10132785Skarels 		 * back to (closed) window.  We will enter persist
10232785Skarels 		 * state below.  If the window didn't close completely,
10327048Skarels 		 * just wait for an ACK.
10426834Skarels 		 */
10532785Skarels 		len = 0;
10632785Skarels 		if (win == 0) {
10727067Skarels 			tp->t_timer[TCPT_REXMT] = 0;
10827067Skarels 			tp->snd_nxt = tp->snd_una;
10932785Skarels 		}
11026834Skarels 	}
11127067Skarels 	if (len > tp->t_maxseg) {
11227067Skarels 		len = tp->t_maxseg;
11331442Skarels 		sendalot = 1;
11427067Skarels 	}
11529795Skarels 	if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
11629795Skarels 		flags &= ~TH_FIN;
11744380Skarels 
11826834Skarels 	win = sbspace(&so->so_rcv);
11926834Skarels 
12025940Skarels 	/*
12117318Skarels 	 * Sender silly window avoidance.  If connection is idle
12217318Skarels 	 * and can send all data, a maximum segment,
12317318Skarels 	 * at least a maximum default-size segment do it,
1246279Swnj 	 * or are forced, do it; otherwise don't bother.
12525261Skarels 	 * If peer's buffer is tiny, then send
12625261Skarels 	 * when window is at least half open.
12721116Skarels 	 * If retransmitting (possibly after persist timer forced us
12821116Skarels 	 * to send into a small window), then must resend.
1296279Swnj 	 */
1306279Swnj 	if (len) {
13131725Skarels 		if (len == tp->t_maxseg)
1326279Swnj 			goto send;
13325940Skarels 		if ((idle || tp->t_flags & TF_NODELAY) &&
13425940Skarels 		    len + off >= so->so_snd.sb_cc)
1356279Swnj 			goto send;
1366279Swnj 		if (tp->t_force)
1376279Swnj 			goto send;
13825261Skarels 		if (len >= tp->max_sndwnd / 2)
13925261Skarels 			goto send;
14021116Skarels 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
14121116Skarels 			goto send;
14226834Skarels 	}
1436279Swnj 
1445441Swnj 	/*
14525940Skarels 	 * Compare available window to amount of window
14625940Skarels 	 * known to peer (as advertised window less
14732785Skarels 	 * next expected input).  If the difference is at least two
14840685Skarels 	 * max size segments, or at least 50% of the maximum possible
14932785Skarels 	 * window, then want to send a window update to peer.
1505075Swnj 	 */
15132034Skarels 	if (win > 0) {
15257433Sandrew 		/*
15357433Sandrew 		 * "adv" is the amount we can increase the window,
15457433Sandrew 		 * taking into account that we are limited by
15557433Sandrew 		 * TCP_MAXWIN << tp->rcv_scale.
15657433Sandrew 		 */
15757433Sandrew 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
15857433Sandrew 			(tp->rcv_adv - tp->rcv_nxt);
1594678Swnj 
16045169Skarels 		if (adv >= (long) (2 * tp->t_maxseg))
16132785Skarels 			goto send;
16245169Skarels 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
16332034Skarels 			goto send;
16432034Skarels 	}
16532034Skarels 
1665075Swnj 	/*
16744380Skarels 	 * Send if we owe peer an ACK.
16844380Skarels 	 */
16944380Skarels 	if (tp->t_flags & TF_ACKNOW)
17044380Skarels 		goto send;
17144380Skarels 	if (flags & (TH_SYN|TH_RST))
17244380Skarels 		goto send;
17344380Skarels 	if (SEQ_GT(tp->snd_up, tp->snd_una))
17444380Skarels 		goto send;
17544380Skarels 	/*
17644380Skarels 	 * If our state indicates that FIN should be sent
17744380Skarels 	 * and we have not yet done so, or we're retransmitting the FIN,
17844380Skarels 	 * then we need to send.
17944380Skarels 	 */
18044380Skarels 	if (flags & TH_FIN &&
18144380Skarels 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
18244380Skarels 		goto send;
18344380Skarels 
18444380Skarels 	/*
1857125Swnj 	 * TCP window updates are not reliable, rather a polling protocol
1867125Swnj 	 * using ``persist'' packets is used to insure receipt of window
1877125Swnj 	 * updates.  The three ``states'' for the output side are:
1887125Swnj 	 *	idle			not doing retransmits or persists
18925940Skarels 	 *	persisting		to move a small or zero window
1907125Swnj 	 *	(re)transmitting	and thereby not persisting
1917125Swnj 	 *
1927125Swnj 	 * tp->t_timer[TCPT_PERSIST]
1937125Swnj 	 *	is set when we are in persist state.
1947125Swnj 	 * tp->t_force
1957125Swnj 	 *	is set when we are called to send a persist packet.
1967125Swnj 	 * tp->t_timer[TCPT_REXMT]
1977125Swnj 	 *	is set when we are retransmitting
1987125Swnj 	 * The output side is idle when both timers are zero.
1997125Swnj 	 *
20021116Skarels 	 * If send window is too small, there is data to transmit, and no
20121116Skarels 	 * retransmit or persist is pending, then go to persist state.
20221116Skarels 	 * If nothing happens soon, send when timer expires:
20321116Skarels 	 * if window is nonzero, transmit what we can,
20421116Skarels 	 * otherwise force out a byte.
2057125Swnj 	 */
20621116Skarels 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
20721116Skarels 	    tp->t_timer[TCPT_PERSIST] == 0) {
2087125Swnj 		tp->t_rxtshift = 0;
2097125Swnj 		tcp_setpersist(tp);
2107125Swnj 	}
2117125Swnj 
2127125Swnj 	/*
2135075Swnj 	 * No reason to send a segment, just return.
2145075Swnj 	 */
2155110Swnj 	return (0);
2164678Swnj 
2175075Swnj send:
2185075Swnj 	/*
21944380Skarels 	 * Before ESTABLISHED, force sending of initial options
22044380Skarels 	 * unless TCP set not to do any options.
22144380Skarels 	 * NOTE: we assume that the IP/TCP header plus TCP options
22244380Skarels 	 * always fit in a single mbuf, leaving room for a maximum
22344380Skarels 	 * link header, i.e.
22444380Skarels 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
22544380Skarels 	 */
22644380Skarels 	optlen = 0;
22744380Skarels 	hdrlen = sizeof (struct tcpiphdr);
228*59038Skarels 	if (flags & TH_SYN) {
229*59038Skarels 		tp->snd_nxt = tp->iss;
230*59038Skarels 		if ((tp->t_flags & TF_NOOPT) == 0) {
231*59038Skarels 			u_short mss;
23252515Storek 
233*59038Skarels 			opt[0] = TCPOPT_MAXSEG;
234*59038Skarels 			opt[1] = 4;
235*59038Skarels 			mss = htons((u_short) tcp_mss(tp, 0));
236*59038Skarels 			bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss));
237*59038Skarels 			optlen = 4;
238*59038Skarels 
239*59038Skarels 			if ((tp->t_flags & TF_REQ_SCALE) &&
240*59038Skarels 			    ((flags & TH_ACK) == 0 ||
241*59038Skarels 			    (tp->t_flags & TF_RCVD_SCALE))) {
242*59038Skarels 				*((u_long *) (opt + optlen)) = htonl(
243*59038Skarels 					TCPOPT_NOP << 24 |
244*59038Skarels 					TCPOPT_WINDOW << 16 |
245*59038Skarels 					TCPOLEN_WINDOW << 8 |
246*59038Skarels 					tp->request_r_scale);
247*59038Skarels 				optlen += 4;
248*59038Skarels 			}
249*59038Skarels 		}
25057433Sandrew  	}
25157433Sandrew 
25257433Sandrew  	/*
25357433Sandrew 	 * Send a timestamp and echo-reply if this is a SYN and our side
25457433Sandrew 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
25557433Sandrew 	 * and our peer have sent timestamps in our SYN's.
25657433Sandrew  	 */
25757433Sandrew  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
25857433Sandrew  	     (flags & TH_RST) == 0 &&
25957433Sandrew  	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
26057433Sandrew 	     (tp->t_flags & TF_RCVD_TSTMP))) {
26157433Sandrew 		u_long *lp = (u_long *)(opt + optlen);
26257433Sandrew 
26357433Sandrew  		/* Form timestamp option as shown in appendix A of RFC 1323. */
26457433Sandrew  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
26557433Sandrew  		*lp++ = htonl(tcp_now);
26657433Sandrew  		*lp   = htonl(tp->ts_recent);
26757433Sandrew  		optlen += TCPOLEN_TSTAMP_APPA;
26857433Sandrew  	}
26957666Sandrew 
27057666Sandrew  	hdrlen += optlen;
27157433Sandrew 
27257433Sandrew 	/*
27357433Sandrew 	 * Adjust data length if insertion of options will
27457433Sandrew 	 * bump the packet length beyond the t_maxseg length.
27557433Sandrew 	 */
27657433Sandrew 	 if (len > tp->t_maxseg - optlen) {
27757433Sandrew 		len = tp->t_maxseg - optlen;
27857433Sandrew 		sendalot = 1;
27957433Sandrew 	 }
28057433Sandrew 
28157433Sandrew 
28244380Skarels #ifdef DIAGNOSTIC
28357433Sandrew  	if (max_linkhdr + hdrlen > MHLEN)
28457433Sandrew 		panic("tcphdr too big");
28544380Skarels #endif
28644380Skarels 
28744380Skarels 	/*
2885075Swnj 	 * Grab a header mbuf, attaching a copy of data to
2895075Swnj 	 * be transmitted, and initialize the header from
2905075Swnj 	 * the template for sends on this connection.
2915075Swnj 	 */
2925075Swnj 	if (len) {
29331442Skarels 		if (tp->t_force && len == 1)
29431442Skarels 			tcpstat.tcps_sndprobe++;
29531442Skarels 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
29631442Skarels 			tcpstat.tcps_sndrexmitpack++;
29731442Skarels 			tcpstat.tcps_sndrexmitbyte += len;
29831442Skarels 		} else {
29931442Skarels 			tcpstat.tcps_sndpack++;
30031442Skarels 			tcpstat.tcps_sndbyte += len;
30131442Skarels 		}
30244380Skarels #ifdef notyet
30344380Skarels 		if ((m = m_copypack(so->so_snd.sb_mb, off,
30444380Skarels 		    (int)len, max_linkhdr + hdrlen)) == 0) {
30544380Skarels 			error = ENOBUFS;
30644380Skarels 			goto out;
30744380Skarels 		}
30844380Skarels 		/*
30944380Skarels 		 * m_copypack left space for our hdr; use it.
31044380Skarels 		 */
31144380Skarels 		m->m_len += hdrlen;
31244380Skarels 		m->m_data -= hdrlen;
31344380Skarels #else
31444380Skarels 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
31544380Skarels 		if (m == NULL) {
31644380Skarels 			error = ENOBUFS;
31744380Skarels 			goto out;
31844380Skarels 		}
31944380Skarels 		m->m_data += max_linkhdr;
32044380Skarels 		m->m_len = hdrlen;
32144380Skarels 		if (len <= MHLEN - hdrlen - max_linkhdr) {
32237317Skarels 			m_copydata(so->so_snd.sb_mb, off, (int) len,
32344380Skarels 			    mtod(m, caddr_t) + hdrlen);
32437317Skarels 			m->m_len += len;
32537317Skarels 		} else {
32637317Skarels 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
32737317Skarels 			if (m->m_next == 0)
32837317Skarels 				len = 0;
32937317Skarels 		}
33044380Skarels #endif
33144380Skarels 		/*
33244380Skarels 		 * If we're sending everything we've got, set PUSH.
33344380Skarels 		 * (This will keep happy those implementations which only
33444380Skarels 		 * give data to the user when a buffer fills or
33544380Skarels 		 * a PUSH comes in.)
33644380Skarels 		 */
33744380Skarels 		if (off + len == so->so_snd.sb_cc)
33844380Skarels 			flags |= TH_PUSH;
33944380Skarels 	} else {
34044380Skarels 		if (tp->t_flags & TF_ACKNOW)
34144380Skarels 			tcpstat.tcps_sndacks++;
34244380Skarels 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
34344380Skarels 			tcpstat.tcps_sndctrl++;
34444380Skarels 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
34544380Skarels 			tcpstat.tcps_sndurg++;
34644380Skarels 		else
34744380Skarels 			tcpstat.tcps_sndwinup++;
34831442Skarels 
34944380Skarels 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
35044380Skarels 		if (m == NULL) {
35144380Skarels 			error = ENOBUFS;
35244380Skarels 			goto out;
35344380Skarels 		}
35444380Skarels 		m->m_data += max_linkhdr;
35544380Skarels 		m->m_len = hdrlen;
35644380Skarels 	}
35744380Skarels 	m->m_pkthdr.rcvif = (struct ifnet *)0;
35844380Skarels 	ti = mtod(m, struct tcpiphdr *);
3595075Swnj 	if (tp->t_template == 0)
3605075Swnj 		panic("tcp_output");
3615110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
3625075Swnj 
3635075Swnj 	/*
3645075Swnj 	 * Fill in fields, remembering maximum advertised
3655075Swnj 	 * window for use in delaying messages about window sizes.
36627067Skarels 	 * If resending a FIN, be sure not to use a new sequence number.
3675075Swnj 	 */
36831725Skarels 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
36931725Skarels 	    tp->snd_nxt == tp->snd_max)
37027067Skarels 		tp->snd_nxt--;
37125940Skarels 	ti->ti_seq = htonl(tp->snd_nxt);
37225940Skarels 	ti->ti_ack = htonl(tp->rcv_nxt);
37344380Skarels 	if (optlen) {
37444380Skarels 		bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
3755441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
3765088Swnj 	}
3775088Swnj 	ti->ti_flags = flags;
37825940Skarels 	/*
37925940Skarels 	 * Calculate receive window.  Don't shrink window,
38025940Skarels 	 * but avoid silly window syndrome.
38125940Skarels 	 */
38233783Skarels 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
38325940Skarels 		win = 0;
38457433Sandrew 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
38557433Sandrew 		win = (long)TCP_MAXWIN << tp->rcv_scale;
38636777Skarels 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
38736777Skarels 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
38857433Sandrew 	ti->ti_win = htons((u_short) (win>>tp->rcv_scale));
3895088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
39026387Skarels 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
3915075Swnj 		ti->ti_flags |= TH_URG;
3925075Swnj 	} else
3935075Swnj 		/*
3945075Swnj 		 * If no urgent pointer to send, then we pull
3955075Swnj 		 * the urgent pointer to the left edge of the send window
3965075Swnj 		 * so that it doesn't drift into the send window on sequence
3975075Swnj 		 * number wraparound.
3985075Swnj 		 */
3995088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
4005075Swnj 
4015075Swnj 	/*
4025075Swnj 	 * Put TCP length in extended header, and then
4035075Swnj 	 * checksum extended header and data.
4045075Swnj 	 */
40525940Skarels 	if (len + optlen)
40644380Skarels 		ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
40725940Skarels 		    optlen + len));
40844380Skarels 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
4095075Swnj 
4105075Swnj 	/*
4117125Swnj 	 * In transmit state, time the transmission and arrange for
41221116Skarels 	 * the retransmit.  In persist state, just set snd_max.
4135088Swnj 	 */
41421116Skarels 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
41531758Skarels 		tcp_seq startseq = tp->snd_nxt;
41631758Skarels 
4177125Swnj 		/*
4187146Swnj 		 * Advance snd_nxt over sequence space of this segment.
4197125Swnj 		 */
42044380Skarels 		if (flags & (TH_SYN|TH_FIN)) {
42144380Skarels 			if (flags & TH_SYN)
42244380Skarels 				tp->snd_nxt++;
42344380Skarels 			if (flags & TH_FIN) {
42444380Skarels 				tp->snd_nxt++;
42544380Skarels 				tp->t_flags |= TF_SENTFIN;
42644380Skarels 			}
42727067Skarels 		}
4287125Swnj 		tp->snd_nxt += len;
42931758Skarels 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
4307149Swnj 			tp->snd_max = tp->snd_nxt;
43131758Skarels 			/*
43231758Skarels 			 * Time this transmission if not a retransmission and
43331758Skarels 			 * not currently timing anything.
43431758Skarels 			 */
43531758Skarels 			if (tp->t_rtt == 0) {
43631758Skarels 				tp->t_rtt = 1;
43731758Skarels 				tp->t_rtseq = startseq;
43831758Skarels 				tcpstat.tcps_segstimed++;
43931758Skarels 			}
44031758Skarels 		}
4415088Swnj 
4427125Swnj 		/*
44321116Skarels 		 * Set retransmit timer if not currently set,
44426443Skarels 		 * and not doing an ack or a keep-alive probe.
44531726Skarels 		 * Initial value for retransmit timer is smoothed
44631726Skarels 		 * round-trip time + 2 * round-trip time variance.
44726834Skarels 		 * Initialize shift counter which is used for backoff
44826834Skarels 		 * of retransmit time.
4497125Swnj 		 */
4507125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
4517125Swnj 		    tp->snd_nxt != tp->snd_una) {
45232034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
45332034Skarels 			if (tp->t_timer[TCPT_PERSIST]) {
45432034Skarels 				tp->t_timer[TCPT_PERSIST] = 0;
45532034Skarels 				tp->t_rxtshift = 0;
45632034Skarels 			}
4577125Swnj 		}
45826443Skarels 	} else
45925940Skarels 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
46025940Skarels 			tp->snd_max = tp->snd_nxt + len;
4615163Swnj 
4625163Swnj 	/*
4635268Sroot 	 * Trace.
4645268Sroot 	 */
4657146Swnj 	if (so->so_options & SO_DEBUG)
4665268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
4675268Sroot 
4685268Sroot 	/*
4695075Swnj 	 * Fill in IP length and desired time to live and
47044380Skarels 	 * send to IP level.  There should be a better way
47144380Skarels 	 * to handle ttl and tos; we could keep them in
47244380Skarels 	 * the template, but need a way to checksum without them.
4735075Swnj 	 */
47444380Skarels 	m->m_pkthdr.len = hdrlen + len;
47557947Ssklower #ifdef TUBA
47657947Ssklower 	if (tp->t_tuba_pcb)
47757947Ssklower 		error = tuba_output(m, tp);
47857947Ssklower 	else
47957947Ssklower #endif
48057947Ssklower     {
48144380Skarels 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
48244380Skarels 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
48344380Skarels 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
48444380Skarels #if BSD >= 43
48526059Skarels 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
48657433Sandrew 	    so->so_options & SO_DONTROUTE, 0);
48744380Skarels #else
48844380Skarels 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
48944380Skarels 	    so->so_options & SO_DONTROUTE);
49044380Skarels #endif
49157947Ssklower     }
49233446Skarels 	if (error) {
49344380Skarels out:
49433446Skarels 		if (error == ENOBUFS) {
49533446Skarels 			tcp_quench(tp->t_inpcb);
49633446Skarels 			return (0);
49733446Skarels 		}
49844380Skarels 		if ((error == EHOSTUNREACH || error == ENETDOWN)
49944380Skarels 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
50044380Skarels 			tp->t_softerror = error;
50144380Skarels 			return (0);
50244380Skarels 		}
5036505Ssam 		return (error);
50433446Skarels 	}
50531442Skarels 	tcpstat.tcps_sndtotal++;
5065075Swnj 
5075075Swnj 	/*
5085075Swnj 	 * Data sent (as far as we can tell).
5095075Swnj 	 * If this advertises a larger window than any other segment,
5105245Sroot 	 * then remember the size of the advertised window.
51125940Skarels 	 * Any pending ACK has now been sent.
5125075Swnj 	 */
5135252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
5145075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
51557433Sandrew 	tp->last_ack_sent = tp->rcv_nxt;
5165088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
51725940Skarels 	if (sendalot)
5187125Swnj 		goto again;
5196505Ssam 	return (0);
5204677Swnj }
5217125Swnj 
5227125Swnj tcp_setpersist(tp)
5237125Swnj 	register struct tcpcb *tp;
5247125Swnj {
52531726Skarels 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
5267125Swnj 
5277125Swnj 	if (tp->t_timer[TCPT_REXMT])
5287125Swnj 		panic("tcp_output REXMT");
5297125Swnj 	/*
5307125Swnj 	 * Start/restart persistance timer.
5317125Swnj 	 */
5327125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
53331726Skarels 	    t * tcp_backoff[tp->t_rxtshift],
53431725Skarels 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
53531725Skarels 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
53631725Skarels 		tp->t_rxtshift++;
5377125Swnj }
538