xref: /csrg-svn/sys/netinet/tcp_output.c (revision 37317)
123191Smckusick /*
2*37317Skarels  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
332788Sbostic  * All rights reserved.
423191Smckusick  *
532788Sbostic  * Redistribution and use in source and binary forms are permitted
634855Sbostic  * provided that the above copyright notice and this paragraph are
734855Sbostic  * duplicated in all such forms and that any documentation,
834855Sbostic  * advertising materials, and other materials related to such
934855Sbostic  * distribution and use acknowledge that the software was developed
1034855Sbostic  * by the University of California, Berkeley.  The name of the
1134855Sbostic  * University may not be used to endorse or promote products derived
1234855Sbostic  * from this software without specific prior written permission.
1334855Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434855Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534855Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1632788Sbostic  *
17*37317Skarels  *	@(#)tcp_output.c	7.18 (Berkeley) 04/08/89
1823191Smckusick  */
194677Swnj 
2017063Sbloom #include "param.h"
2117063Sbloom #include "systm.h"
22*37317Skarels #include "malloc.h"
2317063Sbloom #include "mbuf.h"
2417063Sbloom #include "protosw.h"
2517063Sbloom #include "socket.h"
2617063Sbloom #include "socketvar.h"
2717063Sbloom #include "errno.h"
2810895Ssam 
2910895Ssam #include "../net/route.h"
3010895Ssam 
3117063Sbloom #include "in.h"
3217063Sbloom #include "in_pcb.h"
3317063Sbloom #include "in_systm.h"
3417063Sbloom #include "ip.h"
3517063Sbloom #include "ip_var.h"
3617063Sbloom #include "tcp.h"
375088Swnj #define	TCPOUTFLAGS
3817063Sbloom #include "tcp_fsm.h"
3917063Sbloom #include "tcp_seq.h"
4017063Sbloom #include "tcp_timer.h"
4117063Sbloom #include "tcp_var.h"
4217063Sbloom #include "tcpip.h"
4317063Sbloom #include "tcp_debug.h"
444677Swnj 
454678Swnj /*
468314Sroot  * Initial options.
475441Swnj  */
485441Swnj u_char	tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
495441Swnj 
505441Swnj /*
515245Sroot  * Tcp output routine: figure out what should be sent and send it.
524678Swnj  */
535075Swnj tcp_output(tp)
545075Swnj 	register struct tcpcb *tp;
554678Swnj {
565075Swnj 	register struct socket *so = tp->t_inpcb->inp_socket;
57*37317Skarels 	register long len, win;
585075Swnj 	struct mbuf *m0;
5925940Skarels 	int off, flags, error;
605075Swnj 	register struct mbuf *m;
615075Swnj 	register struct tcpiphdr *ti;
625441Swnj 	u_char *opt;
635441Swnj 	unsigned optlen = 0;
6425940Skarels 	int idle, sendalot;
654678Swnj 
665075Swnj 	/*
676279Swnj 	 * Determine length of data that should be transmitted,
685088Swnj 	 * and flags that will be used.
695088Swnj 	 * If there is some data or critical controls (SYN, RST)
705088Swnj 	 * to send, then transmit; otherwise, investigate further.
715075Swnj 	 */
7225940Skarels 	idle = (tp->snd_max == tp->snd_una);
737125Swnj again:
747125Swnj 	sendalot = 0;
755075Swnj 	off = tp->snd_nxt - tp->snd_una;
76*37317Skarels 	win = min(tp->snd_wnd, tp->snd_cwnd);
7726834Skarels 
7821116Skarels 	/*
7921116Skarels 	 * If in persist timeout with window of 0, send 1 byte.
8025940Skarels 	 * Otherwise, if window is small but nonzero
8125940Skarels 	 * and timer expired, we will send what we can
8225940Skarels 	 * and go to transmit state.
8321116Skarels 	 */
8421116Skarels 	if (tp->t_force) {
8526834Skarels 		if (win == 0)
8621116Skarels 			win = 1;
8721116Skarels 		else {
8821116Skarels 			tp->t_timer[TCPT_PERSIST] = 0;
8921116Skarels 			tp->t_rxtshift = 0;
9021116Skarels 		}
9121116Skarels 	}
9225940Skarels 
93*37317Skarels 	len = min(so->so_snd.sb_cc, win) - off;
945088Swnj 	flags = tcp_outflags[tp->t_state];
9525940Skarels 
9626834Skarels 	if (len < 0) {
9726834Skarels 		/*
9827048Skarels 		 * If FIN has been sent but not acked,
9927048Skarels 		 * but we haven't been called to retransmit,
10032785Skarels 		 * len will be -1.  Otherwise, window shrank
10132785Skarels 		 * after we sent into it.  If window shrank to 0,
10232785Skarels 		 * cancel pending retransmit and pull snd_nxt
10332785Skarels 		 * back to (closed) window.  We will enter persist
10432785Skarels 		 * state below.  If the window didn't close completely,
10527048Skarels 		 * just wait for an ACK.
10626834Skarels 		 */
10732785Skarels 		len = 0;
10832785Skarels 		if (win == 0) {
10927067Skarels 			tp->t_timer[TCPT_REXMT] = 0;
11027067Skarels 			tp->snd_nxt = tp->snd_una;
11132785Skarels 		}
11226834Skarels 	}
11327067Skarels 	if (len > tp->t_maxseg) {
11427067Skarels 		len = tp->t_maxseg;
11531442Skarels 		sendalot = 1;
11627067Skarels 	}
11729795Skarels 	if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
11829795Skarels 		flags &= ~TH_FIN;
11926834Skarels 	win = sbspace(&so->so_rcv);
12026834Skarels 
12127067Skarels 
12225940Skarels 	/*
12327067Skarels 	 * If our state indicates that FIN should be sent
12427067Skarels 	 * and we have not yet done so, or we're retransmitting the FIN,
12527067Skarels 	 * then we need to send.
12627067Skarels 	 */
12727067Skarels 	if (flags & TH_FIN &&
12827067Skarels 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
12927067Skarels 		goto send;
13027067Skarels 	/*
13125940Skarels 	 * Send if we owe peer an ACK.
13225940Skarels 	 */
13326834Skarels 	if (tp->t_flags & TF_ACKNOW)
13425940Skarels 		goto send;
13527067Skarels 	if (flags & (TH_SYN|TH_RST))
13626834Skarels 		goto send;
1376279Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_una))
1386279Swnj 		goto send;
1394678Swnj 
1405075Swnj 	/*
14117318Skarels 	 * Sender silly window avoidance.  If connection is idle
14217318Skarels 	 * and can send all data, a maximum segment,
14317318Skarels 	 * at least a maximum default-size segment do it,
1446279Swnj 	 * or are forced, do it; otherwise don't bother.
14525261Skarels 	 * If peer's buffer is tiny, then send
14625261Skarels 	 * when window is at least half open.
14721116Skarels 	 * If retransmitting (possibly after persist timer forced us
14821116Skarels 	 * to send into a small window), then must resend.
1496279Swnj 	 */
1506279Swnj 	if (len) {
15131725Skarels 		if (len == tp->t_maxseg)
1526279Swnj 			goto send;
15325940Skarels 		if ((idle || tp->t_flags & TF_NODELAY) &&
15425940Skarels 		    len + off >= so->so_snd.sb_cc)
1556279Swnj 			goto send;
1566279Swnj 		if (tp->t_force)
1576279Swnj 			goto send;
15825261Skarels 		if (len >= tp->max_sndwnd / 2)
15925261Skarels 			goto send;
16021116Skarels 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
16121116Skarels 			goto send;
16226834Skarels 	}
1636279Swnj 
1645441Swnj 	/*
16525940Skarels 	 * Compare available window to amount of window
16625940Skarels 	 * known to peer (as advertised window less
16732785Skarels 	 * next expected input).  If the difference is at least two
16832785Skarels 	 * max size segments or at least 35% of the maximum possible
16932785Skarels 	 * window, then want to send a window update to peer.
1705075Swnj 	 */
17132034Skarels 	if (win > 0) {
17232034Skarels 		int adv = win - (tp->rcv_adv - tp->rcv_nxt);
1734678Swnj 
17432785Skarels 		if (so->so_rcv.sb_cc == 0 && adv >= 2 * tp->t_maxseg)
17532785Skarels 			goto send;
17632034Skarels 		if (100 * adv / so->so_rcv.sb_hiwat >= 35)
17732034Skarels 			goto send;
17832034Skarels 	}
17932034Skarels 
1805075Swnj 	/*
1817125Swnj 	 * TCP window updates are not reliable, rather a polling protocol
1827125Swnj 	 * using ``persist'' packets is used to insure receipt of window
1837125Swnj 	 * updates.  The three ``states'' for the output side are:
1847125Swnj 	 *	idle			not doing retransmits or persists
18525940Skarels 	 *	persisting		to move a small or zero window
1867125Swnj 	 *	(re)transmitting	and thereby not persisting
1877125Swnj 	 *
1887125Swnj 	 * tp->t_timer[TCPT_PERSIST]
1897125Swnj 	 *	is set when we are in persist state.
1907125Swnj 	 * tp->t_force
1917125Swnj 	 *	is set when we are called to send a persist packet.
1927125Swnj 	 * tp->t_timer[TCPT_REXMT]
1937125Swnj 	 *	is set when we are retransmitting
1947125Swnj 	 * The output side is idle when both timers are zero.
1957125Swnj 	 *
19621116Skarels 	 * If send window is too small, there is data to transmit, and no
19721116Skarels 	 * retransmit or persist is pending, then go to persist state.
19821116Skarels 	 * If nothing happens soon, send when timer expires:
19921116Skarels 	 * if window is nonzero, transmit what we can,
20021116Skarels 	 * otherwise force out a byte.
2017125Swnj 	 */
20221116Skarels 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
20321116Skarels 	    tp->t_timer[TCPT_PERSIST] == 0) {
2047125Swnj 		tp->t_rxtshift = 0;
2057125Swnj 		tcp_setpersist(tp);
2067125Swnj 	}
2077125Swnj 
2087125Swnj 	/*
2095075Swnj 	 * No reason to send a segment, just return.
2105075Swnj 	 */
2115110Swnj 	return (0);
2124678Swnj 
2135075Swnj send:
2145075Swnj 	/*
2155075Swnj 	 * Grab a header mbuf, attaching a copy of data to
2165075Swnj 	 * be transmitted, and initialize the header from
2175075Swnj 	 * the template for sends on this connection.
2185075Swnj 	 */
219*37317Skarels 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
22011720Ssam 	if (m == NULL)
2216505Ssam 		return (ENOBUFS);
222*37317Skarels 	m->m_data += max_linkhdr;
2234885Swnj 	m->m_len = sizeof (struct tcpiphdr);
224*37317Skarels 	m->m_pkthdr.rcvif = (struct ifnet *)0;
225*37317Skarels 	ti = mtod(m, struct tcpiphdr *);
2265075Swnj 	if (len) {
22731442Skarels 		if (tp->t_force && len == 1)
22831442Skarels 			tcpstat.tcps_sndprobe++;
22931442Skarels 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
23031442Skarels 			tcpstat.tcps_sndrexmitpack++;
23131442Skarels 			tcpstat.tcps_sndrexmitbyte += len;
23231442Skarels 		} else {
23331442Skarels 			tcpstat.tcps_sndpack++;
23431442Skarels 			tcpstat.tcps_sndbyte += len;
23531442Skarels 		}
236*37317Skarels 		if (len <= MHLEN - sizeof (struct tcpiphdr) - max_linkhdr) {
237*37317Skarels 			m_copydata(so->so_snd.sb_mb, off, (int) len,
238*37317Skarels 			    mtod(m, caddr_t) + sizeof(struct tcpiphdr));
239*37317Skarels 			m->m_len += len;
240*37317Skarels 		} else {
241*37317Skarels 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
242*37317Skarels 			if (m->m_next == 0)
243*37317Skarels 				len = 0;
244*37317Skarels 		}
24531442Skarels 	} else if (tp->t_flags & TF_ACKNOW)
24631442Skarels 		tcpstat.tcps_sndacks++;
24731442Skarels 	else if (flags & (TH_SYN|TH_FIN|TH_RST))
24831442Skarels 		tcpstat.tcps_sndctrl++;
24931442Skarels 	else if (SEQ_GT(tp->snd_up, tp->snd_una))
25031442Skarels 		tcpstat.tcps_sndurg++;
25131442Skarels 	else
25231442Skarels 		tcpstat.tcps_sndwinup++;
25331442Skarels 
2545075Swnj 	if (tp->t_template == 0)
2555075Swnj 		panic("tcp_output");
2565110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
2575075Swnj 
2585075Swnj 	/*
2595075Swnj 	 * Fill in fields, remembering maximum advertised
2605075Swnj 	 * window for use in delaying messages about window sizes.
26127067Skarels 	 * If resending a FIN, be sure not to use a new sequence number.
2625075Swnj 	 */
26331725Skarels 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
26431725Skarels 	    tp->snd_nxt == tp->snd_max)
26527067Skarels 		tp->snd_nxt--;
26625940Skarels 	ti->ti_seq = htonl(tp->snd_nxt);
26725940Skarels 	ti->ti_ack = htonl(tp->rcv_nxt);
2685441Swnj 	/*
2695441Swnj 	 * Before ESTABLISHED, force sending of initial options
2705441Swnj 	 * unless TCP set to not do any options.
2715441Swnj 	 */
27225940Skarels 	opt = NULL;
27332373Skarels 	if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) {
27426387Skarels 		u_short mss;
27517273Skarels 
276*37317Skarels 		mss = min(so->so_rcv.sb_hiwat / 2, tcp_mss(tp));
27725940Skarels 		if (mss > IP_MSS - sizeof(struct tcpiphdr)) {
27825940Skarels 			opt = tcp_initopt;
27925940Skarels 			optlen = sizeof (tcp_initopt);
28025940Skarels 			*(u_short *)(opt + 2) = htons(mss);
28125940Skarels 		}
2825441Swnj 	}
2838314Sroot 	if (opt) {
2845110Swnj 		m0 = m->m_next;
2859643Ssam 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
2865088Swnj 		if (m->m_next == 0) {
2875088Swnj 			(void) m_free(m);
2885441Swnj 			m_freem(m0);
2896505Ssam 			return (ENOBUFS);
2905088Swnj 		}
2915088Swnj 		m->m_next->m_next = m0;
2925441Swnj 		m0 = m->m_next;
2935441Swnj 		m0->m_len = optlen;
2946162Ssam 		bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
2955441Swnj 		opt = (u_char *)(mtod(m0, caddr_t) + optlen);
2965441Swnj 		while (m0->m_len & 0x3) {
2975441Swnj 			*opt++ = TCPOPT_EOL;
2985441Swnj 			m0->m_len++;
2995441Swnj 		}
3005441Swnj 		optlen = m0->m_len;
3015441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
3025088Swnj 	}
3035088Swnj 	ti->ti_flags = flags;
30425940Skarels 	/*
30525940Skarels 	 * Calculate receive window.  Don't shrink window,
30625940Skarels 	 * but avoid silly window syndrome.
30725940Skarels 	 */
30833783Skarels 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
30925940Skarels 		win = 0;
310*37317Skarels 	if (win > IP_MAXPACKET)
311*37317Skarels 		win = IP_MAXPACKET;
31236777Skarels 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
31336777Skarels 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
31425940Skarels 	ti->ti_win = htons((u_short)win);
3155088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
31626387Skarels 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
3175075Swnj 		ti->ti_flags |= TH_URG;
3185075Swnj 	} else
3195075Swnj 		/*
3205075Swnj 		 * If no urgent pointer to send, then we pull
3215075Swnj 		 * the urgent pointer to the left edge of the send window
3225075Swnj 		 * so that it doesn't drift into the send window on sequence
3235075Swnj 		 * number wraparound.
3245075Swnj 		 */
3255088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
3267644Sroot 	/*
3277644Sroot 	 * If anything to send and we can send it all, set PUSH.
3287644Sroot 	 * (This will keep happy those implementations which only
32910143Ssam 	 * give data to the user when a buffer fills or a PUSH comes in.)
3307644Sroot 	 */
3317644Sroot 	if (len && off+len == so->so_snd.sb_cc)
3327644Sroot 		ti->ti_flags |= TH_PUSH;
3335075Swnj 
3345075Swnj 	/*
3355075Swnj 	 * Put TCP length in extended header, and then
3365075Swnj 	 * checksum extended header and data.
3375075Swnj 	 */
33825940Skarels 	if (len + optlen)
33925940Skarels 		ti->ti_len = htons((u_short)(sizeof(struct tcphdr) +
34025940Skarels 		    optlen + len));
34134500Skarels 	ti->ti_sum = in_cksum(m,
34234500Skarels 	    (int)(sizeof (struct tcpiphdr) + (int)optlen + len));
3435075Swnj 
3445075Swnj 	/*
3457125Swnj 	 * In transmit state, time the transmission and arrange for
34621116Skarels 	 * the retransmit.  In persist state, just set snd_max.
3475088Swnj 	 */
34821116Skarels 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
34931758Skarels 		tcp_seq startseq = tp->snd_nxt;
35031758Skarels 
3517125Swnj 		/*
3527146Swnj 		 * Advance snd_nxt over sequence space of this segment.
3537125Swnj 		 */
35427067Skarels 		if (flags & TH_SYN)
3557125Swnj 			tp->snd_nxt++;
35627067Skarels 		if (flags & TH_FIN) {
35727067Skarels 			tp->snd_nxt++;
35827067Skarels 			tp->t_flags |= TF_SENTFIN;
35927067Skarels 		}
3607125Swnj 		tp->snd_nxt += len;
36131758Skarels 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
3627149Swnj 			tp->snd_max = tp->snd_nxt;
36331758Skarels 			/*
36431758Skarels 			 * Time this transmission if not a retransmission and
36531758Skarels 			 * not currently timing anything.
36631758Skarels 			 */
36731758Skarels 			if (tp->t_rtt == 0) {
36831758Skarels 				tp->t_rtt = 1;
36931758Skarels 				tp->t_rtseq = startseq;
37031758Skarels 				tcpstat.tcps_segstimed++;
37131758Skarels 			}
37231758Skarels 		}
3735088Swnj 
3747125Swnj 		/*
37521116Skarels 		 * Set retransmit timer if not currently set,
37626443Skarels 		 * and not doing an ack or a keep-alive probe.
37731726Skarels 		 * Initial value for retransmit timer is smoothed
37831726Skarels 		 * round-trip time + 2 * round-trip time variance.
37926834Skarels 		 * Initialize shift counter which is used for backoff
38026834Skarels 		 * of retransmit time.
3817125Swnj 		 */
3827125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
3837125Swnj 		    tp->snd_nxt != tp->snd_una) {
38432034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
38532034Skarels 			if (tp->t_timer[TCPT_PERSIST]) {
38632034Skarels 				tp->t_timer[TCPT_PERSIST] = 0;
38732034Skarels 				tp->t_rxtshift = 0;
38832034Skarels 			}
3897125Swnj 		}
39026443Skarels 	} else
39125940Skarels 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
39225940Skarels 			tp->snd_max = tp->snd_nxt + len;
3935163Swnj 
3945163Swnj 	/*
3955268Sroot 	 * Trace.
3965268Sroot 	 */
3977146Swnj 	if (so->so_options & SO_DEBUG)
3985268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
3995268Sroot 
4005268Sroot 	/*
4015075Swnj 	 * Fill in IP length and desired time to live and
4025075Swnj 	 * send to IP level.
4035075Swnj 	 */
4045441Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
405*37317Skarels 	if (m->m_flags & M_PKTHDR)
406*37317Skarels 		m->m_pkthdr.len = ((struct ip *)ti)->ip_len;
4075075Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
40826059Skarels 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
40926059Skarels 	    so->so_options & SO_DONTROUTE);
41033446Skarels 	if (error) {
41133446Skarels 		if (error == ENOBUFS) {
41233446Skarels 			tcp_quench(tp->t_inpcb);
41333446Skarels 			return (0);
41433446Skarels 		}
4156505Ssam 		return (error);
41633446Skarels 	}
41731442Skarels 	tcpstat.tcps_sndtotal++;
4185075Swnj 
4195075Swnj 	/*
4205075Swnj 	 * Data sent (as far as we can tell).
4215075Swnj 	 * If this advertises a larger window than any other segment,
4225245Sroot 	 * then remember the size of the advertised window.
42325940Skarels 	 * Any pending ACK has now been sent.
4245075Swnj 	 */
4255252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
4265075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
4275088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
42825940Skarels 	if (sendalot)
4297125Swnj 		goto again;
4306505Ssam 	return (0);
4314677Swnj }
4327125Swnj 
4337125Swnj tcp_setpersist(tp)
4347125Swnj 	register struct tcpcb *tp;
4357125Swnj {
43631726Skarels 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
4377125Swnj 
4387125Swnj 	if (tp->t_timer[TCPT_REXMT])
4397125Swnj 		panic("tcp_output REXMT");
4407125Swnj 	/*
4417125Swnj 	 * Start/restart persistance timer.
4427125Swnj 	 */
4437125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
44431726Skarels 	    t * tcp_backoff[tp->t_rxtshift],
44531725Skarels 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
44631725Skarels 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
44731725Skarels 		tp->t_rxtshift++;
4487125Swnj }
449