xref: /csrg-svn/sys/netinet/tcp_output.c (revision 7125)
1*7125Swnj /*	tcp_output.c	4.39	82/06/08	*/
24677Swnj 
34677Swnj #include "../h/param.h"
44677Swnj #include "../h/systm.h"
54677Swnj #include "../h/mbuf.h"
65163Swnj #include "../h/protosw.h"
74677Swnj #include "../h/socket.h"
84804Swnj #include "../h/socketvar.h"
95088Swnj #include "../net/in.h"
106352Ssam #include "../net/route.h"
115088Swnj #include "../net/in_pcb.h"
125088Swnj #include "../net/in_systm.h"
134804Swnj #include "../net/ip.h"
144900Swnj #include "../net/ip_var.h"
154804Swnj #include "../net/tcp.h"
165088Swnj #define	TCPOUTFLAGS
175088Swnj #include "../net/tcp_fsm.h"
185088Swnj #include "../net/tcp_seq.h"
195088Swnj #include "../net/tcp_timer.h"
204804Swnj #include "../net/tcp_var.h"
215088Swnj #include "../net/tcpip.h"
225268Sroot #include "../net/tcp_debug.h"
236505Ssam #include <errno.h>
244677Swnj 
255245Sroot char *tcpstates[]; /* XXX */
265441Swnj 
274678Swnj /*
285441Swnj  * Initial options: indicate max segment length 1/2 of space
295441Swnj  * allocated for receive; if TCPTRUEOOB is defined, indicate
305441Swnj  * willingness to do true out-of-band.
315441Swnj  */
325441Swnj #ifndef TCPTRUEOOB
335441Swnj u_char	tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
345441Swnj #else
355441Swnj u_char	tcp_initopt[6] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, TCPOPT_WILLOOB, 2 };
365441Swnj #endif
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;
455075Swnj 	register int len;
465075Swnj 	struct mbuf *m0;
476505Ssam 	int off, flags, win, error;
485075Swnj 	register struct mbuf *m;
495075Swnj 	register struct tcpiphdr *ti;
505441Swnj 	u_char *opt;
515441Swnj 	unsigned optlen = 0;
52*7125Swnj 	int sendalot;
534678Swnj 
545075Swnj COUNT(TCP_OUTPUT);
554678Swnj 
565075Swnj 	/*
576279Swnj 	 * Determine length of data that should be transmitted,
585088Swnj 	 * and flags that will be used.
595088Swnj 	 * If there is some data or critical controls (SYN, RST)
605088Swnj 	 * to send, then transmit; otherwise, investigate further.
615075Swnj 	 */
62*7125Swnj again:
63*7125Swnj 	sendalot = 0;
645075Swnj 	off = tp->snd_nxt - tp->snd_una;
655163Swnj 	len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off;
665285Sroot 	if (len < 0)
676505Ssam 		return (0);	/* ??? */	/* past FIN */
68*7125Swnj 	if (len > tp->t_maxseg) {
695088Swnj 		len = tp->t_maxseg;
70*7125Swnj 		sendalot = 1;
71*7125Swnj 	}
726279Swnj 
735088Swnj 	flags = tcp_outflags[tp->t_state];
745299Sroot 	if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc)
755163Swnj 		flags &= ~TH_FIN;
766279Swnj 	if (flags & (TH_SYN|TH_RST|TH_FIN))
775075Swnj 		goto send;
786279Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_una))
796279Swnj 		goto send;
804678Swnj 
815075Swnj 	/*
826279Swnj 	 * Sender silly window avoidance.  If can send all data,
836279Swnj 	 * a maximum segment, at least 1/4 of window do it,
846279Swnj 	 * or are forced, do it; otherwise don't bother.
856279Swnj 	 */
866279Swnj 	if (len) {
876279Swnj 		if (len == tp->t_maxseg || off+len >= so->so_snd.sb_cc)
886279Swnj 			goto send;
896279Swnj 		if (len * 4 >= tp->snd_wnd)		/* a lot */
906279Swnj 			goto send;
916279Swnj 		if (tp->t_force)
926279Swnj 			goto send;
936279Swnj 	}
946279Swnj 
956279Swnj 	/*
965285Sroot 	 * Send if we owe peer an ACK.
975075Swnj 	 */
985441Swnj 	if (tp->t_flags&TF_ACKNOW)
995075Swnj 		goto send;
1004678Swnj 
1015441Swnj #ifdef TCPTRUEOOB
1025075Swnj 	/*
1035441Swnj 	 * Send if an out of band data or ack should be transmitted.
1045441Swnj 	 */
1055441Swnj 	if (tp->t_oobflags&(TCPOOB_OWEACK|TCPOOB_NEEDACK)))
1065441Swnj 		goto send;
1075441Swnj #endif
1085441Swnj 
1095441Swnj 	/*
1105075Swnj 	 * Calculate available window in i, and also amount
1115075Swnj 	 * of window known to peer (as advertised window less
1125075Swnj 	 * next expected input.)  If this is 35% or more of the
1135075Swnj 	 * maximum possible window, then want to send a segment to peer.
1145075Swnj 	 */
1155088Swnj 	win = sbspace(&so->so_rcv);
1165088Swnj 	if (win > 0 &&
1175088Swnj 	    ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
1185075Swnj 		goto send;
1194678Swnj 
1205075Swnj 	/*
121*7125Swnj 	 * TCP window updates are not reliable, rather a polling protocol
122*7125Swnj 	 * using ``persist'' packets is used to insure receipt of window
123*7125Swnj 	 * updates.  The three ``states'' for the output side are:
124*7125Swnj 	 *	idle			not doing retransmits or persists
125*7125Swnj 	 *	persisting		to move a zero window
126*7125Swnj 	 *	(re)transmitting	and thereby not persisting
127*7125Swnj 	 *
128*7125Swnj 	 * tp->t_timer[TCPT_PERSIST]
129*7125Swnj 	 *	is set when we are in persist state.
130*7125Swnj 	 * tp->t_force
131*7125Swnj 	 *	is set when we are called to send a persist packet.
132*7125Swnj 	 * tp->t_timer[TCPT_REXMT]
133*7125Swnj 	 *	is set when we are retransmitting
134*7125Swnj 	 * The output side is idle when both timers are zero.
135*7125Swnj 	 *
136*7125Swnj 	 * If send window is closed, there is data to transmit, and no
137*7125Swnj 	 * retransmit or persist is pending, then go to persist state,
138*7125Swnj 	 * arranging to force out a byte to get more current window information
139*7125Swnj 	 * if nothing happens soon.
140*7125Swnj 	 */
141*7125Swnj 	if (tp->snd_wnd == 0 && so->so_snd.sb_cc &&
142*7125Swnj 	    tp->t_timer[TCPT_REXMT] == 0 && tp->t_timer[TCPT_PERSIST] == 0) {
143*7125Swnj 		tp->t_rxtshift = 0;
144*7125Swnj 		tcp_setpersist(tp);
145*7125Swnj 	}
146*7125Swnj 
147*7125Swnj 	/*
1485075Swnj 	 * No reason to send a segment, just return.
1495075Swnj 	 */
1505110Swnj 	return (0);
1514678Swnj 
1525075Swnj send:
1535075Swnj 	/*
1545075Swnj 	 * Grab a header mbuf, attaching a copy of data to
1555075Swnj 	 * be transmitted, and initialize the header from
1565075Swnj 	 * the template for sends on this connection.
1575075Swnj 	 */
1584677Swnj 	MGET(m, 0);
1594677Swnj 	if (m == 0)
1606505Ssam 		return (ENOBUFS);
1615245Sroot 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
1624885Swnj 	m->m_len = sizeof (struct tcpiphdr);
1635075Swnj 	if (len) {
1645075Swnj 		m->m_next = m_copy(so->so_snd.sb_mb, off, len);
1655075Swnj 		if (m->m_next == 0)
1665075Swnj 			len = 0;
1675075Swnj 	}
1685075Swnj 	ti = mtod(m, struct tcpiphdr *);
1695075Swnj 	if (tp->t_template == 0)
1705075Swnj 		panic("tcp_output");
1715110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
1725075Swnj 
1735075Swnj 	/*
1745075Swnj 	 * Fill in fields, remembering maximum advertised
1755075Swnj 	 * window for use in delaying messages about window sizes.
1765075Swnj 	 */
1775245Sroot 	ti->ti_seq = tp->snd_nxt;
1785245Sroot 	ti->ti_ack = tp->rcv_nxt;
1795245Sroot #if vax
1805245Sroot 	ti->ti_seq = htonl(ti->ti_seq);
1815245Sroot 	ti->ti_ack = htonl(ti->ti_ack);
1825245Sroot #endif
1835441Swnj 	/*
1845441Swnj 	 * Before ESTABLISHED, force sending of initial options
1855441Swnj 	 * unless TCP set to not do any options.
1865441Swnj 	 */
1875441Swnj 	if (tp->t_state < TCPS_ESTABLISHED) {
1885441Swnj 		if (tp->t_flags&TF_NOOPT)
1895441Swnj 			goto noopt;
1905441Swnj 		opt = tcp_initopt;
1915441Swnj 		optlen = sizeof (tcp_initopt);
1925441Swnj 		*(u_short *)(opt + 2) = so->so_rcv.sb_hiwat / 2;
1935441Swnj #if vax
1945441Swnj 		*(u_short *)(opt + 2) = htons(*(u_short *)(opt + 2));
1955441Swnj #endif
1965441Swnj 	} else {
1975441Swnj 		if (tp->t_tcpopt == 0)
1985441Swnj 			goto noopt;
1995441Swnj 		opt = mtod(tp->t_tcpopt, u_char *);
2005441Swnj 		optlen = tp->t_tcpopt->m_len;
2015441Swnj 	}
2025441Swnj #ifndef TCPTRUEOOB
2035441Swnj 	if (opt)
2045441Swnj #else
2055441Swnj 	if (opt || (tp->t_oobflags&(TCPOOB_OWEACK|TCPOOB_NEEDACK)))
2065441Swnj #endif
2075441Swnj 	{
2085110Swnj 		m0 = m->m_next;
2095584Sroot 		m->m_next = m_get(M_DONTWAIT);
2105088Swnj 		if (m->m_next == 0) {
2115088Swnj 			(void) m_free(m);
2125441Swnj 			m_freem(m0);
2136505Ssam 			return (ENOBUFS);
2145088Swnj 		}
2155088Swnj 		m->m_next->m_next = m0;
2165441Swnj 		m0 = m->m_next;
2175441Swnj 		m0->m_off = MMINOFF;
2185441Swnj 		m0->m_len = optlen;
2196162Ssam 		bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
2205441Swnj 		opt = (u_char *)(mtod(m0, caddr_t) + optlen);
2215441Swnj #ifdef TCPTRUEOOB
2225441Swnj 		if (tp->t_oobflags&TCPOOB_OWEACK) {
2235441Swnj printf("tp %x send OOBACK for %x\n", tp->t_iobseq);
2245441Swnj 			*opt++ = TCPOPT_OOBACK;
2255441Swnj 			*opt++ = 3;
2265441Swnj 			*opt++ = tp->t_iobseq;
2275441Swnj 			m0->m_len += 3;
2285441Swnj 			tp->t_oobflags &= ~TCPOOB_OWEACK;
2295441Swnj 			/* sender should rexmt oob to force ack repeat */
2305441Swnj 		}
2315441Swnj 		if (tp->t_oobflags&TCPOOB_NEEDACK) {
2325441Swnj printf("tp %x send OOBDATA seq %x data %x\n", tp->t_oobseq, tp->t_oobc);
2335441Swnj 			*opt++ = TCPOPT_OOBDATA;
2345548Swnj 			*opt++ = 8;
2355441Swnj 			*opt++ = tp->t_oobseq;
2365441Swnj 			*opt++ = tp->t_oobc;
2375548Swnj 			*(tcp_seq *)opt = tp->t_oobmark - tp->snd_nxt;
2385548Swnj #ifdef vax
2395548Swnj 			*(tcp_seq *)opt = htonl((unsigned)*(tcp_seq *)opt);
2405548Swnj #endif
2415548Swnj 			m0->m_len += 8;
2425441Swnj 			TCPT_RANGESET(tp->t_timer[TCPT_OOBREXMT],
2435441Swnj 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
2445441Swnj 		}
2455441Swnj #endif
2465441Swnj 		while (m0->m_len & 0x3) {
2475441Swnj 			*opt++ = TCPOPT_EOL;
2485441Swnj 			m0->m_len++;
2495441Swnj 		}
2505441Swnj 		optlen = m0->m_len;
2515441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
2525088Swnj 	}
2535441Swnj noopt:
2545088Swnj 	ti->ti_flags = flags;
2555075Swnj 	win = sbspace(&so->so_rcv);
2566279Swnj 	if (win < so->so_rcv.sb_hiwat / 4)	/* avoid silly window */
2576279Swnj 		win = 0;
2585075Swnj 	if (win > 0)
2596279Swnj #if vax
2605110Swnj 		ti->ti_win = htons((u_short)win);
2616279Swnj #else
2626279Swnj 		ti->ti_win = win;
2636279Swnj #endif
2645088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
2655420Swnj 		ti->ti_urp = tp->snd_up - tp->snd_nxt;
2665420Swnj #if vax
2675420Swnj 		ti->ti_urp = htons(ti->ti_urp);
2685420Swnj #endif
2695075Swnj 		ti->ti_flags |= TH_URG;
2705075Swnj 	} else
2715075Swnj 		/*
2725075Swnj 		 * If no urgent pointer to send, then we pull
2735075Swnj 		 * the urgent pointer to the left edge of the send window
2745075Swnj 		 * so that it doesn't drift into the send window on sequence
2755075Swnj 		 * number wraparound.
2765075Swnj 		 */
2775088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
2785088Swnj 	/* PUSH */
2795075Swnj 
2805075Swnj 	/*
2815075Swnj 	 * Put TCP length in extended header, and then
2825075Swnj 	 * checksum extended header and data.
2835075Swnj 	 */
2845441Swnj 	if (len + optlen) {
2855441Swnj 		ti->ti_len = sizeof (struct tcphdr) + optlen + len;
2865441Swnj #if vax
2875441Swnj 		ti->ti_len = htons((u_short)ti->ti_len);
2885441Swnj #endif
2895441Swnj 	}
2906162Ssam 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len);
2915075Swnj 
2925075Swnj 	/*
293*7125Swnj 	 * In transmit state, time the transmission and arrange for
294*7125Swnj 	 * the retransmit.  In persist state, reset persist time for
295*7125Swnj 	 * next persist.
2965088Swnj 	 */
297*7125Swnj 	if (tp->t_force == 0) {
298*7125Swnj 		/*
299*7125Swnj 		 * Advance snd_nxt over sequence space of this segment
300*7125Swnj 		 */
301*7125Swnj 		if (flags & (TH_SYN|TH_FIN))
302*7125Swnj 			tp->snd_nxt++;
303*7125Swnj 		tp->snd_nxt += len;
3045088Swnj 
305*7125Swnj 		/*
306*7125Swnj 		 * Time this transmission if not a retransmission and
307*7125Swnj 		 * not currently timing anything.
308*7125Swnj 		 */
309*7125Swnj 		if (SEQ_GT(tp->snd_nxt, tp->snd_max) && tp->t_rtt == 0) {
310*7125Swnj 			tp->t_rtt = 1;
311*7125Swnj 			tp->t_rtseq = tp->snd_nxt - len;
312*7125Swnj 		}
3135088Swnj 
314*7125Swnj 		/*
315*7125Swnj 		 * Set retransmit timer if not currently set.
316*7125Swnj 		 * Initial value for retransmit timer to tcp_beta*tp->t_srtt.
317*7125Swnj 		 * Initialize shift counter which is used for exponential
318*7125Swnj 		 * backoff of retransmit time.
319*7125Swnj 		 */
320*7125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
321*7125Swnj 		    tp->snd_nxt != tp->snd_una) {
322*7125Swnj 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
323*7125Swnj 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
324*7125Swnj 			tp->t_rtt = 0;
325*7125Swnj 			tp->t_rxtshift = 0;
326*7125Swnj 		}
327*7125Swnj 		tp->t_timer[TCPT_PERSIST] = 0;
328*7125Swnj 	} else
329*7125Swnj 		tcp_setpersist(tp);
3305163Swnj 
3315163Swnj 	/*
3325268Sroot 	 * Trace.
3335268Sroot 	 */
3345268Sroot 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
3355268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
3365268Sroot 
3375268Sroot 	/*
3385075Swnj 	 * Fill in IP length and desired time to live and
3395075Swnj 	 * send to IP level.
3405075Swnj 	 */
3415441Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
3425075Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
3436505Ssam 	if (error = ip_output(m, tp->t_ipopt, &tp->t_inpcb->inp_route, 0))
3446505Ssam 		return (error);
3455075Swnj 
3465075Swnj 	/*
3475075Swnj 	 * Data sent (as far as we can tell).
3485075Swnj 	 * If this advertises a larger window than any other segment,
3495245Sroot 	 * then remember the size of the advertised window.
3505088Swnj 	 * Drop send for purpose of ACK requirements.
3515075Swnj 	 */
3525252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
3535075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
3545088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
3555245Sroot 	if (SEQ_GT(tp->snd_nxt, tp->snd_max))
3565245Sroot 		tp->snd_max = tp->snd_nxt;
357*7125Swnj 	if (sendalot && tp->t_force == 0)
358*7125Swnj 		goto again;
3596505Ssam 	return (0);
3604677Swnj }
361*7125Swnj 
362*7125Swnj tcp_setpersist(tp)
363*7125Swnj 	register struct tcpcb *tp;
364*7125Swnj {
365*7125Swnj 
366*7125Swnj 	if (tp->t_timer[TCPT_REXMT])
367*7125Swnj 		panic("tcp_output REXMT");
368*7125Swnj 	/*
369*7125Swnj 	 * Start/restart persistance timer.
370*7125Swnj 	 */
371*7125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
372*7125Swnj 	    ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift,
373*7125Swnj 	    TCPTV_PERSMIN, TCPTV_MAX);
374*7125Swnj 	tp->t_rxtshift++;
375*7125Swnj 	if (tp->t_rxtshift >= TCP_MAXRXTSHIFT)
376*7125Swnj 		tp->t_rxtshift = 0;
377*7125Swnj }
378