xref: /csrg-svn/sys/netinet/tcp_output.c (revision 25940)
123191Smckusick /*
223191Smckusick  * Copyright (c) 1982 Regents of the University of California.
323191Smckusick  * All rights reserved.  The Berkeley software License Agreement
423191Smckusick  * specifies the terms and conditions for redistribution.
523191Smckusick  *
6*25940Skarels  *	@(#)tcp_output.c	6.12 (Berkeley) 01/22/86
723191Smckusick  */
84677Swnj 
917063Sbloom #include "param.h"
1017063Sbloom #include "systm.h"
1117063Sbloom #include "mbuf.h"
1217063Sbloom #include "protosw.h"
1317063Sbloom #include "socket.h"
1417063Sbloom #include "socketvar.h"
1517063Sbloom #include "errno.h"
1610895Ssam 
1710895Ssam #include "../net/route.h"
1810895Ssam 
1917063Sbloom #include "in.h"
2017063Sbloom #include "in_pcb.h"
2117063Sbloom #include "in_systm.h"
2217063Sbloom #include "ip.h"
2317063Sbloom #include "ip_var.h"
2417063Sbloom #include "tcp.h"
255088Swnj #define	TCPOUTFLAGS
2617063Sbloom #include "tcp_fsm.h"
2717063Sbloom #include "tcp_seq.h"
2817063Sbloom #include "tcp_timer.h"
2917063Sbloom #include "tcp_var.h"
3017063Sbloom #include "tcpip.h"
3117063Sbloom #include "tcp_debug.h"
324677Swnj 
334678Swnj /*
348314Sroot  * Initial options.
355441Swnj  */
365441Swnj u_char	tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
375441Swnj 
385441Swnj /*
395245Sroot  * Tcp output routine: figure out what should be sent and send it.
404678Swnj  */
415075Swnj tcp_output(tp)
425075Swnj 	register struct tcpcb *tp;
434678Swnj {
445075Swnj 	register struct socket *so = tp->t_inpcb->inp_socket;
45*25940Skarels 	register int len, win;
465075Swnj 	struct mbuf *m0;
47*25940Skarels 	int off, flags, error;
485075Swnj 	register struct mbuf *m;
495075Swnj 	register struct tcpiphdr *ti;
505441Swnj 	u_char *opt;
515441Swnj 	unsigned optlen = 0;
52*25940Skarels 	int idle, sendalot;
534678Swnj 
545075Swnj 	/*
556279Swnj 	 * Determine length of data that should be transmitted,
565088Swnj 	 * and flags that will be used.
575088Swnj 	 * If there is some data or critical controls (SYN, RST)
585088Swnj 	 * to send, then transmit; otherwise, investigate further.
595075Swnj 	 */
60*25940Skarels 	idle = (tp->snd_max == tp->snd_una);
617125Swnj again:
627125Swnj 	sendalot = 0;
635075Swnj 	off = tp->snd_nxt - tp->snd_una;
6421116Skarels 	win = MIN(tp->snd_wnd, tp->snd_cwnd);
6521116Skarels 	/*
6621116Skarels 	 * If in persist timeout with window of 0, send 1 byte.
67*25940Skarels 	 * Otherwise, if window is small but nonzero
68*25940Skarels 	 * and timer expired, we will send what we can
69*25940Skarels 	 * and go to transmit state.
7021116Skarels 	 */
7121116Skarels 	if (tp->t_force) {
7221116Skarels 		if (win == 0)
7321116Skarels 			win = 1;
7421116Skarels 		else {
7521116Skarels 			tp->t_timer[TCPT_PERSIST] = 0;
7621116Skarels 			tp->t_rxtshift = 0;
7721116Skarels 		}
7821116Skarels 	}
79*25940Skarels 
8017361Skarels 	len = MIN(so->so_snd.sb_cc, win) - off;
815285Sroot 	if (len < 0)
826505Ssam 		return (0);	/* ??? */	/* past FIN */
837125Swnj 	if (len > tp->t_maxseg) {
845088Swnj 		len = tp->t_maxseg;
8517318Skarels 		/*
8621116Skarels 		 * Don't send more than one segment if retransmitting
8721116Skarels 		 * (or persisting, but then we shouldn't be here).
8817318Skarels 		 */
8917361Skarels 		if (tp->t_rxtshift == 0)
9017318Skarels 			sendalot = 1;
917125Swnj 	}
926279Swnj 
9321116Skarels 	win = sbspace(&so->so_rcv);
945088Swnj 	flags = tcp_outflags[tp->t_state];
955299Sroot 	if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc)
965163Swnj 		flags &= ~TH_FIN;
976279Swnj 	if (flags & (TH_SYN|TH_RST|TH_FIN))
985075Swnj 		goto send;
99*25940Skarels 
100*25940Skarels 	/*
101*25940Skarels 	 * Send if we owe peer an ACK.
102*25940Skarels 	 */
103*25940Skarels 	if (tp->t_flags&TF_ACKNOW)
104*25940Skarels 		goto send;
1056279Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_una))
1066279Swnj 		goto send;
1074678Swnj 
1085075Swnj 	/*
10917318Skarels 	 * Sender silly window avoidance.  If connection is idle
11017318Skarels 	 * and can send all data, a maximum segment,
11117318Skarels 	 * at least a maximum default-size segment do it,
1126279Swnj 	 * or are forced, do it; otherwise don't bother.
11325261Skarels 	 * If peer's buffer is tiny, then send
11425261Skarels 	 * when window is at least half open.
11521116Skarels 	 * If retransmitting (possibly after persist timer forced us
11621116Skarels 	 * to send into a small window), then must resend.
1176279Swnj 	 */
1186279Swnj 	if (len) {
119*25940Skarels 		if (len == tp->t_maxseg || len >= TCP_MSS)	/* a lot */
1206279Swnj 			goto send;
121*25940Skarels 		if ((idle || tp->t_flags & TF_NODELAY) &&
122*25940Skarels 		    len + off >= so->so_snd.sb_cc)
1236279Swnj 			goto send;
1246279Swnj 		if (tp->t_force)
1256279Swnj 			goto send;
12625261Skarels 		if (len >= tp->max_sndwnd / 2)
12725261Skarels 			goto send;
12821116Skarels 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
12921116Skarels 			goto send;
130*25940Skarels 	} else
131*25940Skarels 		/*
132*25940Skarels 		 * If window shrank after we sent into it,
133*25940Skarels 		 * cancel pending retransmit.  We will enter
134*25940Skarels 		 * persist state below.
135*25940Skarels 		 */
136*25940Skarels 		if (off == 0 && SEQ_LT(tp->snd_nxt, tp->snd_max))
137*25940Skarels 			tp->t_timer[TCPT_REXMT] = 0;
1386279Swnj 
1394678Swnj 
1405441Swnj 	/*
141*25940Skarels 	 * Compare available window to amount of window
142*25940Skarels 	 * known to peer (as advertised window less
14317361Skarels 	 * next expected input.)  If the difference is 35% or more of the
14417361Skarels 	 * maximum possible window, then want to send a window update to peer.
1455075Swnj 	 */
1465088Swnj 	if (win > 0 &&
1475088Swnj 	    ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
1485075Swnj 		goto send;
1494678Swnj 
1505075Swnj 	/*
1517125Swnj 	 * TCP window updates are not reliable, rather a polling protocol
1527125Swnj 	 * using ``persist'' packets is used to insure receipt of window
1537125Swnj 	 * updates.  The three ``states'' for the output side are:
1547125Swnj 	 *	idle			not doing retransmits or persists
155*25940Skarels 	 *	persisting		to move a small or zero window
1567125Swnj 	 *	(re)transmitting	and thereby not persisting
1577125Swnj 	 *
1587125Swnj 	 * tp->t_timer[TCPT_PERSIST]
1597125Swnj 	 *	is set when we are in persist state.
1607125Swnj 	 * tp->t_force
1617125Swnj 	 *	is set when we are called to send a persist packet.
1627125Swnj 	 * tp->t_timer[TCPT_REXMT]
1637125Swnj 	 *	is set when we are retransmitting
1647125Swnj 	 * The output side is idle when both timers are zero.
1657125Swnj 	 *
16621116Skarels 	 * If send window is too small, there is data to transmit, and no
16721116Skarels 	 * retransmit or persist is pending, then go to persist state.
16821116Skarels 	 * If nothing happens soon, send when timer expires:
16921116Skarels 	 * if window is nonzero, transmit what we can,
17021116Skarels 	 * otherwise force out a byte.
1717125Swnj 	 */
17221116Skarels 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
17321116Skarels 	    tp->t_timer[TCPT_PERSIST] == 0) {
1747125Swnj 		tp->t_rxtshift = 0;
1757125Swnj 		tcp_setpersist(tp);
1767125Swnj 	}
1777125Swnj 
1787125Swnj 	/*
1795075Swnj 	 * No reason to send a segment, just return.
1805075Swnj 	 */
1815110Swnj 	return (0);
1824678Swnj 
1835075Swnj send:
1845075Swnj 	/*
1855075Swnj 	 * Grab a header mbuf, attaching a copy of data to
1865075Swnj 	 * be transmitted, and initialize the header from
1875075Swnj 	 * the template for sends on this connection.
1885075Swnj 	 */
18911720Ssam 	MGET(m, M_DONTWAIT, MT_HEADER);
19011720Ssam 	if (m == NULL)
1916505Ssam 		return (ENOBUFS);
1925245Sroot 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
1934885Swnj 	m->m_len = sizeof (struct tcpiphdr);
1945075Swnj 	if (len) {
1955075Swnj 		m->m_next = m_copy(so->so_snd.sb_mb, off, len);
1965075Swnj 		if (m->m_next == 0)
1975075Swnj 			len = 0;
1985075Swnj 	}
1995075Swnj 	ti = mtod(m, struct tcpiphdr *);
2005075Swnj 	if (tp->t_template == 0)
2015075Swnj 		panic("tcp_output");
2025110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
2035075Swnj 
2045075Swnj 	/*
2055075Swnj 	 * Fill in fields, remembering maximum advertised
2065075Swnj 	 * window for use in delaying messages about window sizes.
2075075Swnj 	 */
208*25940Skarels 	ti->ti_seq = htonl(tp->snd_nxt);
209*25940Skarels 	ti->ti_ack = htonl(tp->rcv_nxt);
2105441Swnj 	/*
2115441Swnj 	 * Before ESTABLISHED, force sending of initial options
2125441Swnj 	 * unless TCP set to not do any options.
2135441Swnj 	 */
214*25940Skarels 	opt = NULL;
215*25940Skarels 	if (tp->t_state < TCPS_ESTABLISHED && (tp->t_flags & TF_NOOPT) == 0) {
21617273Skarels 		int mss;
21717273Skarels 
21817273Skarels 		mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp));
219*25940Skarels 		if (mss > IP_MSS - sizeof(struct tcpiphdr)) {
220*25940Skarels 			opt = tcp_initopt;
221*25940Skarels 			optlen = sizeof (tcp_initopt);
222*25940Skarels 			*(u_short *)(opt + 2) = htons(mss);
223*25940Skarels 		}
224*25940Skarels 	} else if (tp->t_tcpopt) {
2255441Swnj 		opt = mtod(tp->t_tcpopt, u_char *);
2265441Swnj 		optlen = tp->t_tcpopt->m_len;
2275441Swnj 	}
2288314Sroot 	if (opt) {
2295110Swnj 		m0 = m->m_next;
2309643Ssam 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
2315088Swnj 		if (m->m_next == 0) {
2325088Swnj 			(void) m_free(m);
2335441Swnj 			m_freem(m0);
2346505Ssam 			return (ENOBUFS);
2355088Swnj 		}
2365088Swnj 		m->m_next->m_next = m0;
2375441Swnj 		m0 = m->m_next;
2385441Swnj 		m0->m_len = optlen;
2396162Ssam 		bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
2405441Swnj 		opt = (u_char *)(mtod(m0, caddr_t) + optlen);
2415441Swnj 		while (m0->m_len & 0x3) {
2425441Swnj 			*opt++ = TCPOPT_EOL;
2435441Swnj 			m0->m_len++;
2445441Swnj 		}
2455441Swnj 		optlen = m0->m_len;
2465441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
2475088Swnj 	}
2485088Swnj 	ti->ti_flags = flags;
249*25940Skarels 	/*
250*25940Skarels 	 * Calculate receive window.  Don't shrink window,
251*25940Skarels 	 * but avoid silly window syndrome.
252*25940Skarels 	 */
253*25940Skarels 	if (win < so->so_rcv.sb_hiwat / 4 && win < tp->t_maxseg)
254*25940Skarels 		win = 0;
255*25940Skarels 	if (win < (int)(tp->rcv_adv - tp->rcv_nxt))
256*25940Skarels 		win = (int)(tp->rcv_adv - tp->rcv_nxt);
257*25940Skarels 	ti->ti_win = htons((u_short)win);
2585088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
259*25940Skarels 		ti->ti_urp = htons(tp->snd_up - tp->snd_nxt);
2605075Swnj 		ti->ti_flags |= TH_URG;
2615075Swnj 	} else
2625075Swnj 		/*
2635075Swnj 		 * If no urgent pointer to send, then we pull
2645075Swnj 		 * the urgent pointer to the left edge of the send window
2655075Swnj 		 * so that it doesn't drift into the send window on sequence
2665075Swnj 		 * number wraparound.
2675075Swnj 		 */
2685088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
2697644Sroot 	/*
2707644Sroot 	 * If anything to send and we can send it all, set PUSH.
2717644Sroot 	 * (This will keep happy those implementations which only
27210143Ssam 	 * give data to the user when a buffer fills or a PUSH comes in.)
2737644Sroot 	 */
2747644Sroot 	if (len && off+len == so->so_snd.sb_cc)
2757644Sroot 		ti->ti_flags |= TH_PUSH;
2765075Swnj 
2775075Swnj 	/*
2785075Swnj 	 * Put TCP length in extended header, and then
2795075Swnj 	 * checksum extended header and data.
2805075Swnj 	 */
281*25940Skarels 	if (len + optlen)
282*25940Skarels 		ti->ti_len = htons((u_short)(sizeof(struct tcphdr) +
283*25940Skarels 		    optlen + len));
2846162Ssam 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len);
2855075Swnj 
2865075Swnj 	/*
2877125Swnj 	 * In transmit state, time the transmission and arrange for
28821116Skarels 	 * the retransmit.  In persist state, just set snd_max.
2895088Swnj 	 */
29021116Skarels 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
2917125Swnj 		/*
2927146Swnj 		 * Advance snd_nxt over sequence space of this segment.
2937125Swnj 		 */
2947125Swnj 		if (flags & (TH_SYN|TH_FIN))
2957125Swnj 			tp->snd_nxt++;
2967125Swnj 		tp->snd_nxt += len;
29715385Ssam 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
2987149Swnj 			tp->snd_max = tp->snd_nxt;
29915385Ssam 			/*
30015385Ssam 			 * Time this transmission if not a retransmission and
30115385Ssam 			 * not currently timing anything.
30215385Ssam 			 */
30315385Ssam 			if (tp->t_rtt == 0) {
30415385Ssam 				tp->t_rtt = 1;
30515385Ssam 				tp->t_rtseq = tp->snd_nxt - len;
30615385Ssam 			}
3077125Swnj 		}
3085088Swnj 
3097125Swnj 		/*
31021116Skarels 		 * Set retransmit timer if not currently set,
31121116Skarels 		 * and not doing a keep-alive probe.
312*25940Skarels 		 * Initial value for retransmit timer is tcp_beta*tp->t_srtt.
3137125Swnj 		 * Initialize shift counter which is used for exponential
3147125Swnj 		 * backoff of retransmit time.
3157125Swnj 		 */
3167125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
3177125Swnj 		    tp->snd_nxt != tp->snd_una) {
3187125Swnj 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
3197125Swnj 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
3207125Swnj 			tp->t_rxtshift = 0;
3217125Swnj 		}
3227125Swnj 		tp->t_timer[TCPT_PERSIST] = 0;
3237149Swnj 	} else {
324*25940Skarels 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
325*25940Skarels 			tp->snd_max = tp->snd_nxt + len;
3267146Swnj 	}
3275163Swnj 
3285163Swnj 	/*
3295268Sroot 	 * Trace.
3305268Sroot 	 */
3317146Swnj 	if (so->so_options & SO_DEBUG)
3325268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
3335268Sroot 
3345268Sroot 	/*
3355075Swnj 	 * Fill in IP length and desired time to live and
3365075Swnj 	 * send to IP level.
3375075Swnj 	 */
3385441Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
3395075Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
34012418Ssam 	if (so->so_options & SO_DONTROUTE)
34112765Ssam 		error =
34224817Skarels 		   ip_output(m, tp->t_inpcb->inp_options, (struct route *)0,
34324817Skarels 			IP_ROUTETOIF);
34412418Ssam 	else
34524817Skarels 		error = ip_output(m, tp->t_inpcb->inp_options,
34624817Skarels 		    &tp->t_inpcb->inp_route, 0);
34712418Ssam 	if (error)
3486505Ssam 		return (error);
3495075Swnj 
3505075Swnj 	/*
3515075Swnj 	 * Data sent (as far as we can tell).
3525075Swnj 	 * If this advertises a larger window than any other segment,
3535245Sroot 	 * then remember the size of the advertised window.
354*25940Skarels 	 * Any pending ACK has now been sent.
3555075Swnj 	 */
3565252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
3575075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
3585088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
359*25940Skarels 	if (sendalot)
3607125Swnj 		goto again;
3616505Ssam 	return (0);
3624677Swnj }
3637125Swnj 
3647125Swnj tcp_setpersist(tp)
3657125Swnj 	register struct tcpcb *tp;
3667125Swnj {
3677125Swnj 
3687125Swnj 	if (tp->t_timer[TCPT_REXMT])
3697125Swnj 		panic("tcp_output REXMT");
3707125Swnj 	/*
3717125Swnj 	 * Start/restart persistance timer.
3727125Swnj 	 */
3737125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
3747125Swnj 	    ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift,
3757125Swnj 	    TCPTV_PERSMIN, TCPTV_MAX);
3767125Swnj 	tp->t_rxtshift++;
3777125Swnj 	if (tp->t_rxtshift >= TCP_MAXRXTSHIFT)
3787125Swnj 		tp->t_rxtshift = 0;
3797125Swnj }
380