xref: /csrg-svn/sys/netinet/tcp_output.c (revision 57666)
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*57666Sandrew  *	@(#)tcp_output.c	7.26 (Berkeley) 01/22/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);
22844380Skarels 	if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) {
22952515Storek 		u_short mss;
23052515Storek 
23157433Sandrew  		opt[0] = TCPOPT_MAXSEG;
23257433Sandrew  		opt[1] = 4;
23352515Storek 		mss = htons((u_short) tcp_mss(tp, 0));
23452515Storek 		bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss));
23557433Sandrew  		optlen = 4;
23657433Sandrew 
23757433Sandrew  		if ((tp->t_flags & TF_REQ_SCALE) &&
23857433Sandrew  		    ((flags & TH_ACK) == 0 || (tp->t_flags & TF_RCVD_SCALE))) {
23957433Sandrew  			*((u_long *) (opt + optlen)) = htonl(
24057433Sandrew  				TCPOPT_NOP<<24 |
24157433Sandrew  				TCPOPT_WINDOW<<16 |
24257433Sandrew  				TCPOLEN_WINDOW<<8 |
24357433Sandrew  				tp->request_r_scale);
24457433Sandrew  			optlen += 4;
24557433Sandrew  		}
24657433Sandrew  	}
24757433Sandrew 
24857433Sandrew  	/*
24957433Sandrew 	 * Send a timestamp and echo-reply if this is a SYN and our side
25057433Sandrew 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
25157433Sandrew 	 * and our peer have sent timestamps in our SYN's.
25257433Sandrew  	 */
25357433Sandrew  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
25457433Sandrew  	     (flags & TH_RST) == 0 &&
25557433Sandrew  	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
25657433Sandrew 	     (tp->t_flags & TF_RCVD_TSTMP))) {
25757433Sandrew 		u_long *lp = (u_long *)(opt + optlen);
25857433Sandrew 
25957433Sandrew  		/* Form timestamp option as shown in appendix A of RFC 1323. */
26057433Sandrew  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
26157433Sandrew  		*lp++ = htonl(tcp_now);
26257433Sandrew  		*lp   = htonl(tp->ts_recent);
26357433Sandrew  		optlen += TCPOLEN_TSTAMP_APPA;
26457433Sandrew  	}
265*57666Sandrew 
266*57666Sandrew  	hdrlen += optlen;
26757433Sandrew 
26857433Sandrew 	/*
26957433Sandrew 	 * Adjust data length if insertion of options will
27057433Sandrew 	 * bump the packet length beyond the t_maxseg length.
27157433Sandrew 	 */
27257433Sandrew 	 if (len > tp->t_maxseg - optlen) {
27357433Sandrew 		len = tp->t_maxseg - optlen;
27457433Sandrew 		sendalot = 1;
27557433Sandrew 	 }
27657433Sandrew 
27757433Sandrew 
27844380Skarels #ifdef DIAGNOSTIC
27957433Sandrew  	if (max_linkhdr + hdrlen > MHLEN)
28057433Sandrew 		panic("tcphdr too big");
28144380Skarels #endif
28244380Skarels 
28344380Skarels 	/*
2845075Swnj 	 * Grab a header mbuf, attaching a copy of data to
2855075Swnj 	 * be transmitted, and initialize the header from
2865075Swnj 	 * the template for sends on this connection.
2875075Swnj 	 */
2885075Swnj 	if (len) {
28931442Skarels 		if (tp->t_force && len == 1)
29031442Skarels 			tcpstat.tcps_sndprobe++;
29131442Skarels 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
29231442Skarels 			tcpstat.tcps_sndrexmitpack++;
29331442Skarels 			tcpstat.tcps_sndrexmitbyte += len;
29431442Skarels 		} else {
29531442Skarels 			tcpstat.tcps_sndpack++;
29631442Skarels 			tcpstat.tcps_sndbyte += len;
29731442Skarels 		}
29844380Skarels #ifdef notyet
29944380Skarels 		if ((m = m_copypack(so->so_snd.sb_mb, off,
30044380Skarels 		    (int)len, max_linkhdr + hdrlen)) == 0) {
30144380Skarels 			error = ENOBUFS;
30244380Skarels 			goto out;
30344380Skarels 		}
30444380Skarels 		/*
30544380Skarels 		 * m_copypack left space for our hdr; use it.
30644380Skarels 		 */
30744380Skarels 		m->m_len += hdrlen;
30844380Skarels 		m->m_data -= hdrlen;
30944380Skarels #else
31044380Skarels 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
31144380Skarels 		if (m == NULL) {
31244380Skarels 			error = ENOBUFS;
31344380Skarels 			goto out;
31444380Skarels 		}
31544380Skarels 		m->m_data += max_linkhdr;
31644380Skarels 		m->m_len = hdrlen;
31744380Skarels 		if (len <= MHLEN - hdrlen - max_linkhdr) {
31837317Skarels 			m_copydata(so->so_snd.sb_mb, off, (int) len,
31944380Skarels 			    mtod(m, caddr_t) + hdrlen);
32037317Skarels 			m->m_len += len;
32137317Skarels 		} else {
32237317Skarels 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
32337317Skarels 			if (m->m_next == 0)
32437317Skarels 				len = 0;
32537317Skarels 		}
32644380Skarels #endif
32744380Skarels 		/*
32844380Skarels 		 * If we're sending everything we've got, set PUSH.
32944380Skarels 		 * (This will keep happy those implementations which only
33044380Skarels 		 * give data to the user when a buffer fills or
33144380Skarels 		 * a PUSH comes in.)
33244380Skarels 		 */
33344380Skarels 		if (off + len == so->so_snd.sb_cc)
33444380Skarels 			flags |= TH_PUSH;
33544380Skarels 	} else {
33644380Skarels 		if (tp->t_flags & TF_ACKNOW)
33744380Skarels 			tcpstat.tcps_sndacks++;
33844380Skarels 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
33944380Skarels 			tcpstat.tcps_sndctrl++;
34044380Skarels 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
34144380Skarels 			tcpstat.tcps_sndurg++;
34244380Skarels 		else
34344380Skarels 			tcpstat.tcps_sndwinup++;
34431442Skarels 
34544380Skarels 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
34644380Skarels 		if (m == NULL) {
34744380Skarels 			error = ENOBUFS;
34844380Skarels 			goto out;
34944380Skarels 		}
35044380Skarels 		m->m_data += max_linkhdr;
35144380Skarels 		m->m_len = hdrlen;
35244380Skarels 	}
35344380Skarels 	m->m_pkthdr.rcvif = (struct ifnet *)0;
35444380Skarels 	ti = mtod(m, struct tcpiphdr *);
3555075Swnj 	if (tp->t_template == 0)
3565075Swnj 		panic("tcp_output");
3575110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
3585075Swnj 
3595075Swnj 	/*
3605075Swnj 	 * Fill in fields, remembering maximum advertised
3615075Swnj 	 * window for use in delaying messages about window sizes.
36227067Skarels 	 * If resending a FIN, be sure not to use a new sequence number.
3635075Swnj 	 */
36431725Skarels 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
36531725Skarels 	    tp->snd_nxt == tp->snd_max)
36627067Skarels 		tp->snd_nxt--;
36725940Skarels 	ti->ti_seq = htonl(tp->snd_nxt);
36825940Skarels 	ti->ti_ack = htonl(tp->rcv_nxt);
36944380Skarels 	if (optlen) {
37044380Skarels 		bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
3715441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
3725088Swnj 	}
3735088Swnj 	ti->ti_flags = flags;
37425940Skarels 	/*
37525940Skarels 	 * Calculate receive window.  Don't shrink window,
37625940Skarels 	 * but avoid silly window syndrome.
37725940Skarels 	 */
37833783Skarels 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
37925940Skarels 		win = 0;
38057433Sandrew 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
38157433Sandrew 		win = (long)TCP_MAXWIN << tp->rcv_scale;
38236777Skarels 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
38336777Skarels 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
38457433Sandrew 	ti->ti_win = htons((u_short) (win>>tp->rcv_scale));
3855088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
38626387Skarels 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
3875075Swnj 		ti->ti_flags |= TH_URG;
3885075Swnj 	} else
3895075Swnj 		/*
3905075Swnj 		 * If no urgent pointer to send, then we pull
3915075Swnj 		 * the urgent pointer to the left edge of the send window
3925075Swnj 		 * so that it doesn't drift into the send window on sequence
3935075Swnj 		 * number wraparound.
3945075Swnj 		 */
3955088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
3965075Swnj 
3975075Swnj 	/*
3985075Swnj 	 * Put TCP length in extended header, and then
3995075Swnj 	 * checksum extended header and data.
4005075Swnj 	 */
40125940Skarels 	if (len + optlen)
40244380Skarels 		ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
40325940Skarels 		    optlen + len));
40444380Skarels 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
4055075Swnj 
4065075Swnj 	/*
4077125Swnj 	 * In transmit state, time the transmission and arrange for
40821116Skarels 	 * the retransmit.  In persist state, just set snd_max.
4095088Swnj 	 */
41021116Skarels 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
41131758Skarels 		tcp_seq startseq = tp->snd_nxt;
41231758Skarels 
4137125Swnj 		/*
4147146Swnj 		 * Advance snd_nxt over sequence space of this segment.
4157125Swnj 		 */
41644380Skarels 		if (flags & (TH_SYN|TH_FIN)) {
41744380Skarels 			if (flags & TH_SYN)
41844380Skarels 				tp->snd_nxt++;
41944380Skarels 			if (flags & TH_FIN) {
42044380Skarels 				tp->snd_nxt++;
42144380Skarels 				tp->t_flags |= TF_SENTFIN;
42244380Skarels 			}
42327067Skarels 		}
4247125Swnj 		tp->snd_nxt += len;
42531758Skarels 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
4267149Swnj 			tp->snd_max = tp->snd_nxt;
42731758Skarels 			/*
42831758Skarels 			 * Time this transmission if not a retransmission and
42931758Skarels 			 * not currently timing anything.
43031758Skarels 			 */
43131758Skarels 			if (tp->t_rtt == 0) {
43231758Skarels 				tp->t_rtt = 1;
43331758Skarels 				tp->t_rtseq = startseq;
43431758Skarels 				tcpstat.tcps_segstimed++;
43531758Skarels 			}
43631758Skarels 		}
4375088Swnj 
4387125Swnj 		/*
43921116Skarels 		 * Set retransmit timer if not currently set,
44026443Skarels 		 * and not doing an ack or a keep-alive probe.
44131726Skarels 		 * Initial value for retransmit timer is smoothed
44231726Skarels 		 * round-trip time + 2 * round-trip time variance.
44326834Skarels 		 * Initialize shift counter which is used for backoff
44426834Skarels 		 * of retransmit time.
4457125Swnj 		 */
4467125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
4477125Swnj 		    tp->snd_nxt != tp->snd_una) {
44832034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
44932034Skarels 			if (tp->t_timer[TCPT_PERSIST]) {
45032034Skarels 				tp->t_timer[TCPT_PERSIST] = 0;
45132034Skarels 				tp->t_rxtshift = 0;
45232034Skarels 			}
4537125Swnj 		}
45426443Skarels 	} else
45525940Skarels 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
45625940Skarels 			tp->snd_max = tp->snd_nxt + len;
4575163Swnj 
4585163Swnj 	/*
4595268Sroot 	 * Trace.
4605268Sroot 	 */
4617146Swnj 	if (so->so_options & SO_DEBUG)
4625268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
4635268Sroot 
4645268Sroot 	/*
4655075Swnj 	 * Fill in IP length and desired time to live and
46644380Skarels 	 * send to IP level.  There should be a better way
46744380Skarels 	 * to handle ttl and tos; we could keep them in
46844380Skarels 	 * the template, but need a way to checksum without them.
4695075Swnj 	 */
47044380Skarels 	m->m_pkthdr.len = hdrlen + len;
47144380Skarels 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
47244380Skarels 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
47344380Skarels 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
47444380Skarels #if BSD >= 43
47526059Skarels 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
47657433Sandrew 	    so->so_options & SO_DONTROUTE, 0);
47744380Skarels #else
47844380Skarels 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
47944380Skarels 	    so->so_options & SO_DONTROUTE);
48044380Skarels #endif
48133446Skarels 	if (error) {
48244380Skarels out:
48333446Skarels 		if (error == ENOBUFS) {
48433446Skarels 			tcp_quench(tp->t_inpcb);
48533446Skarels 			return (0);
48633446Skarels 		}
48744380Skarels 		if ((error == EHOSTUNREACH || error == ENETDOWN)
48844380Skarels 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
48944380Skarels 			tp->t_softerror = error;
49044380Skarels 			return (0);
49144380Skarels 		}
4926505Ssam 		return (error);
49333446Skarels 	}
49431442Skarels 	tcpstat.tcps_sndtotal++;
4955075Swnj 
4965075Swnj 	/*
4975075Swnj 	 * Data sent (as far as we can tell).
4985075Swnj 	 * If this advertises a larger window than any other segment,
4995245Sroot 	 * then remember the size of the advertised window.
50025940Skarels 	 * Any pending ACK has now been sent.
5015075Swnj 	 */
5025252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
5035075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
50457433Sandrew 	tp->last_ack_sent = tp->rcv_nxt;
5055088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
50625940Skarels 	if (sendalot)
5077125Swnj 		goto again;
5086505Ssam 	return (0);
5094677Swnj }
5107125Swnj 
5117125Swnj tcp_setpersist(tp)
5127125Swnj 	register struct tcpcb *tp;
5137125Swnj {
51431726Skarels 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
5157125Swnj 
5167125Swnj 	if (tp->t_timer[TCPT_REXMT])
5177125Swnj 		panic("tcp_output REXMT");
5187125Swnj 	/*
5197125Swnj 	 * Start/restart persistance timer.
5207125Swnj 	 */
5217125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
52231726Skarels 	    t * tcp_backoff[tp->t_rxtshift],
52331725Skarels 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
52431725Skarels 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
52531725Skarels 		tp->t_rxtshift++;
5267125Swnj }
527