xref: /csrg-svn/sys/netinet/tcp_output.c (revision 36777)
123191Smckusick /*
229150Smckusick  * Copyright (c) 1982, 1986 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*36777Skarels  *	@(#)tcp_output.c	7.13.1.4 (Berkeley) 02/15/89
1823191Smckusick  */
194677Swnj 
2017063Sbloom #include "param.h"
2117063Sbloom #include "systm.h"
2217063Sbloom #include "mbuf.h"
2317063Sbloom #include "protosw.h"
2417063Sbloom #include "socket.h"
2517063Sbloom #include "socketvar.h"
2617063Sbloom #include "errno.h"
2710895Ssam 
2810895Ssam #include "../net/route.h"
2910895Ssam 
3017063Sbloom #include "in.h"
3117063Sbloom #include "in_pcb.h"
3217063Sbloom #include "in_systm.h"
3317063Sbloom #include "ip.h"
3417063Sbloom #include "ip_var.h"
3517063Sbloom #include "tcp.h"
365088Swnj #define	TCPOUTFLAGS
3717063Sbloom #include "tcp_fsm.h"
3817063Sbloom #include "tcp_seq.h"
3917063Sbloom #include "tcp_timer.h"
4017063Sbloom #include "tcp_var.h"
4117063Sbloom #include "tcpip.h"
4217063Sbloom #include "tcp_debug.h"
434677Swnj 
444678Swnj /*
458314Sroot  * Initial options.
465441Swnj  */
475441Swnj u_char	tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
485441Swnj 
495441Swnj /*
505245Sroot  * Tcp output routine: figure out what should be sent and send it.
514678Swnj  */
525075Swnj tcp_output(tp)
535075Swnj 	register struct tcpcb *tp;
544678Swnj {
555075Swnj 	register struct socket *so = tp->t_inpcb->inp_socket;
56*36777Skarels 	register int len, win;
575075Swnj 	struct mbuf *m0;
5825940Skarels 	int off, flags, error;
595075Swnj 	register struct mbuf *m;
605075Swnj 	register struct tcpiphdr *ti;
615441Swnj 	u_char *opt;
625441Swnj 	unsigned optlen = 0;
6325940Skarels 	int idle, sendalot;
644678Swnj 
655075Swnj 	/*
666279Swnj 	 * Determine length of data that should be transmitted,
675088Swnj 	 * and flags that will be used.
685088Swnj 	 * If there is some data or critical controls (SYN, RST)
695088Swnj 	 * to send, then transmit; otherwise, investigate further.
705075Swnj 	 */
7125940Skarels 	idle = (tp->snd_max == tp->snd_una);
727125Swnj again:
737125Swnj 	sendalot = 0;
745075Swnj 	off = tp->snd_nxt - tp->snd_una;
7521116Skarels 	win = MIN(tp->snd_wnd, tp->snd_cwnd);
7626834Skarels 
7721116Skarels 	/*
7821116Skarels 	 * If in persist timeout with window of 0, send 1 byte.
7925940Skarels 	 * Otherwise, if window is small but nonzero
8025940Skarels 	 * and timer expired, we will send what we can
8125940Skarels 	 * and go to transmit state.
8221116Skarels 	 */
8321116Skarels 	if (tp->t_force) {
8426834Skarels 		if (win == 0)
8521116Skarels 			win = 1;
8621116Skarels 		else {
8721116Skarels 			tp->t_timer[TCPT_PERSIST] = 0;
8821116Skarels 			tp->t_rxtshift = 0;
8921116Skarels 		}
9021116Skarels 	}
9125940Skarels 
9217361Skarels 	len = MIN(so->so_snd.sb_cc, win) - off;
935088Swnj 	flags = tcp_outflags[tp->t_state];
9425940Skarels 
9526834Skarels 	if (len < 0) {
9626834Skarels 		/*
9727048Skarels 		 * If FIN has been sent but not acked,
9827048Skarels 		 * but we haven't been called to retransmit,
9932785Skarels 		 * len will be -1.  Otherwise, window shrank
10032785Skarels 		 * after we sent into it.  If window shrank to 0,
10132785Skarels 		 * cancel pending retransmit and pull snd_nxt
10232785Skarels 		 * back to (closed) window.  We will enter persist
10332785Skarels 		 * state below.  If the window didn't close completely,
10427048Skarels 		 * just wait for an ACK.
10526834Skarels 		 */
10632785Skarels 		len = 0;
10732785Skarels 		if (win == 0) {
10827067Skarels 			tp->t_timer[TCPT_REXMT] = 0;
10927067Skarels 			tp->snd_nxt = tp->snd_una;
11032785Skarels 		}
11126834Skarels 	}
11227067Skarels 	if (len > tp->t_maxseg) {
11327067Skarels 		len = tp->t_maxseg;
11431442Skarels 		sendalot = 1;
11527067Skarels 	}
11629795Skarels 	if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
11729795Skarels 		flags &= ~TH_FIN;
11826834Skarels 	win = sbspace(&so->so_rcv);
11926834Skarels 
12027067Skarels 
12125940Skarels 	/*
12227067Skarels 	 * If our state indicates that FIN should be sent
12327067Skarels 	 * and we have not yet done so, or we're retransmitting the FIN,
12427067Skarels 	 * then we need to send.
12527067Skarels 	 */
12627067Skarels 	if (flags & TH_FIN &&
12727067Skarels 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
12827067Skarels 		goto send;
12927067Skarels 	/*
13025940Skarels 	 * Send if we owe peer an ACK.
13125940Skarels 	 */
13226834Skarels 	if (tp->t_flags & TF_ACKNOW)
13325940Skarels 		goto send;
13427067Skarels 	if (flags & (TH_SYN|TH_RST))
13526834Skarels 		goto send;
1366279Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_una))
1376279Swnj 		goto send;
1384678Swnj 
1395075Swnj 	/*
14017318Skarels 	 * Sender silly window avoidance.  If connection is idle
14117318Skarels 	 * and can send all data, a maximum segment,
14217318Skarels 	 * at least a maximum default-size segment do it,
1436279Swnj 	 * or are forced, do it; otherwise don't bother.
14425261Skarels 	 * If peer's buffer is tiny, then send
14525261Skarels 	 * when window is at least half open.
14621116Skarels 	 * If retransmitting (possibly after persist timer forced us
14721116Skarels 	 * to send into a small window), then must resend.
1486279Swnj 	 */
1496279Swnj 	if (len) {
15031725Skarels 		if (len == tp->t_maxseg)
1516279Swnj 			goto send;
15225940Skarels 		if ((idle || tp->t_flags & TF_NODELAY) &&
15325940Skarels 		    len + off >= so->so_snd.sb_cc)
1546279Swnj 			goto send;
1556279Swnj 		if (tp->t_force)
1566279Swnj 			goto send;
15725261Skarels 		if (len >= tp->max_sndwnd / 2)
15825261Skarels 			goto send;
15921116Skarels 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
16021116Skarels 			goto send;
16126834Skarels 	}
1626279Swnj 
1635441Swnj 	/*
16425940Skarels 	 * Compare available window to amount of window
16525940Skarels 	 * known to peer (as advertised window less
16632785Skarels 	 * next expected input).  If the difference is at least two
16732785Skarels 	 * max size segments or at least 35% of the maximum possible
16832785Skarels 	 * window, then want to send a window update to peer.
1695075Swnj 	 */
17032034Skarels 	if (win > 0) {
17132034Skarels 		int adv = win - (tp->rcv_adv - tp->rcv_nxt);
1724678Swnj 
17332785Skarels 		if (so->so_rcv.sb_cc == 0 && adv >= 2 * tp->t_maxseg)
17432785Skarels 			goto send;
17532034Skarels 		if (100 * adv / so->so_rcv.sb_hiwat >= 35)
17632034Skarels 			goto send;
17732034Skarels 	}
17832034Skarels 
1795075Swnj 	/*
1807125Swnj 	 * TCP window updates are not reliable, rather a polling protocol
1817125Swnj 	 * using ``persist'' packets is used to insure receipt of window
1827125Swnj 	 * updates.  The three ``states'' for the output side are:
1837125Swnj 	 *	idle			not doing retransmits or persists
18425940Skarels 	 *	persisting		to move a small or zero window
1857125Swnj 	 *	(re)transmitting	and thereby not persisting
1867125Swnj 	 *
1877125Swnj 	 * tp->t_timer[TCPT_PERSIST]
1887125Swnj 	 *	is set when we are in persist state.
1897125Swnj 	 * tp->t_force
1907125Swnj 	 *	is set when we are called to send a persist packet.
1917125Swnj 	 * tp->t_timer[TCPT_REXMT]
1927125Swnj 	 *	is set when we are retransmitting
1937125Swnj 	 * The output side is idle when both timers are zero.
1947125Swnj 	 *
19521116Skarels 	 * If send window is too small, there is data to transmit, and no
19621116Skarels 	 * retransmit or persist is pending, then go to persist state.
19721116Skarels 	 * If nothing happens soon, send when timer expires:
19821116Skarels 	 * if window is nonzero, transmit what we can,
19921116Skarels 	 * otherwise force out a byte.
2007125Swnj 	 */
20121116Skarels 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
20221116Skarels 	    tp->t_timer[TCPT_PERSIST] == 0) {
2037125Swnj 		tp->t_rxtshift = 0;
2047125Swnj 		tcp_setpersist(tp);
2057125Swnj 	}
2067125Swnj 
2077125Swnj 	/*
2085075Swnj 	 * No reason to send a segment, just return.
2095075Swnj 	 */
2105110Swnj 	return (0);
2114678Swnj 
2125075Swnj send:
2135075Swnj 	/*
2145075Swnj 	 * Grab a header mbuf, attaching a copy of data to
2155075Swnj 	 * be transmitted, and initialize the header from
2165075Swnj 	 * the template for sends on this connection.
2175075Swnj 	 */
21811720Ssam 	MGET(m, M_DONTWAIT, MT_HEADER);
21911720Ssam 	if (m == NULL)
2206505Ssam 		return (ENOBUFS);
221*36777Skarels 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
2224885Swnj 	m->m_len = sizeof (struct tcpiphdr);
2235075Swnj 	if (len) {
22431442Skarels 		if (tp->t_force && len == 1)
22531442Skarels 			tcpstat.tcps_sndprobe++;
22631442Skarels 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
22731442Skarels 			tcpstat.tcps_sndrexmitpack++;
22831442Skarels 			tcpstat.tcps_sndrexmitbyte += len;
22931442Skarels 		} else {
23031442Skarels 			tcpstat.tcps_sndpack++;
23131442Skarels 			tcpstat.tcps_sndbyte += len;
23231442Skarels 		}
233*36777Skarels 		m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
234*36777Skarels 		if (m->m_next == 0)
235*36777Skarels 			len = 0;
23631442Skarels 	} else if (tp->t_flags & TF_ACKNOW)
23731442Skarels 		tcpstat.tcps_sndacks++;
23831442Skarels 	else if (flags & (TH_SYN|TH_FIN|TH_RST))
23931442Skarels 		tcpstat.tcps_sndctrl++;
24031442Skarels 	else if (SEQ_GT(tp->snd_up, tp->snd_una))
24131442Skarels 		tcpstat.tcps_sndurg++;
24231442Skarels 	else
24331442Skarels 		tcpstat.tcps_sndwinup++;
24431442Skarels 
245*36777Skarels 	ti = mtod(m, struct tcpiphdr *);
2465075Swnj 	if (tp->t_template == 0)
2475075Swnj 		panic("tcp_output");
2485110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
2495075Swnj 
2505075Swnj 	/*
2515075Swnj 	 * Fill in fields, remembering maximum advertised
2525075Swnj 	 * window for use in delaying messages about window sizes.
25327067Skarels 	 * If resending a FIN, be sure not to use a new sequence number.
2545075Swnj 	 */
25531725Skarels 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
25631725Skarels 	    tp->snd_nxt == tp->snd_max)
25727067Skarels 		tp->snd_nxt--;
25825940Skarels 	ti->ti_seq = htonl(tp->snd_nxt);
25925940Skarels 	ti->ti_ack = htonl(tp->rcv_nxt);
2605441Swnj 	/*
2615441Swnj 	 * Before ESTABLISHED, force sending of initial options
2625441Swnj 	 * unless TCP set to not do any options.
2635441Swnj 	 */
26425940Skarels 	opt = NULL;
26532373Skarels 	if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) {
26626387Skarels 		u_short mss;
26717273Skarels 
26817273Skarels 		mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp));
26925940Skarels 		if (mss > IP_MSS - sizeof(struct tcpiphdr)) {
27025940Skarels 			opt = tcp_initopt;
27125940Skarels 			optlen = sizeof (tcp_initopt);
27225940Skarels 			*(u_short *)(opt + 2) = htons(mss);
27325940Skarels 		}
2745441Swnj 	}
2758314Sroot 	if (opt) {
2765110Swnj 		m0 = m->m_next;
2779643Ssam 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
2785088Swnj 		if (m->m_next == 0) {
2795088Swnj 			(void) m_free(m);
2805441Swnj 			m_freem(m0);
2816505Ssam 			return (ENOBUFS);
2825088Swnj 		}
2835088Swnj 		m->m_next->m_next = m0;
2845441Swnj 		m0 = m->m_next;
2855441Swnj 		m0->m_len = optlen;
2866162Ssam 		bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
2875441Swnj 		opt = (u_char *)(mtod(m0, caddr_t) + optlen);
2885441Swnj 		while (m0->m_len & 0x3) {
2895441Swnj 			*opt++ = TCPOPT_EOL;
2905441Swnj 			m0->m_len++;
2915441Swnj 		}
2925441Swnj 		optlen = m0->m_len;
2935441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
2945088Swnj 	}
2955088Swnj 	ti->ti_flags = flags;
29625940Skarels 	/*
29725940Skarels 	 * Calculate receive window.  Don't shrink window,
29825940Skarels 	 * but avoid silly window syndrome.
29925940Skarels 	 */
30033783Skarels 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
30125940Skarels 		win = 0;
302*36777Skarels 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
303*36777Skarels 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
30434500Skarels 	if (win > IP_MAXPACKET)
30534500Skarels 		win = IP_MAXPACKET;
30625940Skarels 	ti->ti_win = htons((u_short)win);
3075088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
30826387Skarels 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
3095075Swnj 		ti->ti_flags |= TH_URG;
3105075Swnj 	} else
3115075Swnj 		/*
3125075Swnj 		 * If no urgent pointer to send, then we pull
3135075Swnj 		 * the urgent pointer to the left edge of the send window
3145075Swnj 		 * so that it doesn't drift into the send window on sequence
3155075Swnj 		 * number wraparound.
3165075Swnj 		 */
3175088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
3187644Sroot 	/*
3197644Sroot 	 * If anything to send and we can send it all, set PUSH.
3207644Sroot 	 * (This will keep happy those implementations which only
32110143Ssam 	 * give data to the user when a buffer fills or a PUSH comes in.)
3227644Sroot 	 */
3237644Sroot 	if (len && off+len == so->so_snd.sb_cc)
3247644Sroot 		ti->ti_flags |= TH_PUSH;
3255075Swnj 
3265075Swnj 	/*
3275075Swnj 	 * Put TCP length in extended header, and then
3285075Swnj 	 * checksum extended header and data.
3295075Swnj 	 */
33025940Skarels 	if (len + optlen)
33125940Skarels 		ti->ti_len = htons((u_short)(sizeof(struct tcphdr) +
33225940Skarels 		    optlen + len));
33334500Skarels 	ti->ti_sum = in_cksum(m,
33434500Skarels 	    (int)(sizeof (struct tcpiphdr) + (int)optlen + len));
3355075Swnj 
3365075Swnj 	/*
3377125Swnj 	 * In transmit state, time the transmission and arrange for
33821116Skarels 	 * the retransmit.  In persist state, just set snd_max.
3395088Swnj 	 */
34021116Skarels 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
34131758Skarels 		tcp_seq startseq = tp->snd_nxt;
34231758Skarels 
3437125Swnj 		/*
3447146Swnj 		 * Advance snd_nxt over sequence space of this segment.
3457125Swnj 		 */
34627067Skarels 		if (flags & TH_SYN)
3477125Swnj 			tp->snd_nxt++;
34827067Skarels 		if (flags & TH_FIN) {
34927067Skarels 			tp->snd_nxt++;
35027067Skarels 			tp->t_flags |= TF_SENTFIN;
35127067Skarels 		}
3527125Swnj 		tp->snd_nxt += len;
35331758Skarels 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
3547149Swnj 			tp->snd_max = tp->snd_nxt;
35531758Skarels 			/*
35631758Skarels 			 * Time this transmission if not a retransmission and
35731758Skarels 			 * not currently timing anything.
35831758Skarels 			 */
35931758Skarels 			if (tp->t_rtt == 0) {
36031758Skarels 				tp->t_rtt = 1;
36131758Skarels 				tp->t_rtseq = startseq;
36231758Skarels 				tcpstat.tcps_segstimed++;
36331758Skarels 			}
36431758Skarels 		}
3655088Swnj 
3667125Swnj 		/*
36721116Skarels 		 * Set retransmit timer if not currently set,
36826443Skarels 		 * and not doing an ack or a keep-alive probe.
36931726Skarels 		 * Initial value for retransmit timer is smoothed
37031726Skarels 		 * round-trip time + 2 * round-trip time variance.
37126834Skarels 		 * Initialize shift counter which is used for backoff
37226834Skarels 		 * of retransmit time.
3737125Swnj 		 */
3747125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
3757125Swnj 		    tp->snd_nxt != tp->snd_una) {
37632034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
37732034Skarels 			if (tp->t_timer[TCPT_PERSIST]) {
37832034Skarels 				tp->t_timer[TCPT_PERSIST] = 0;
37932034Skarels 				tp->t_rxtshift = 0;
38032034Skarels 			}
3817125Swnj 		}
38226443Skarels 	} else
38325940Skarels 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
38425940Skarels 			tp->snd_max = tp->snd_nxt + len;
3855163Swnj 
3865163Swnj 	/*
3875268Sroot 	 * Trace.
3885268Sroot 	 */
3897146Swnj 	if (so->so_options & SO_DEBUG)
3905268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
3915268Sroot 
3925268Sroot 	/*
3935075Swnj 	 * Fill in IP length and desired time to live and
3945075Swnj 	 * send to IP level.
3955075Swnj 	 */
3965441Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
3975075Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
398*36777Skarels #if BSD>=43
39926059Skarels 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
40026059Skarels 	    so->so_options & SO_DONTROUTE);
401*36777Skarels #else
402*36777Skarels 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
403*36777Skarels 			  so->so_options & SO_DONTROUTE);
404*36777Skarels #endif
40533446Skarels 	if (error) {
40633446Skarels 		if (error == ENOBUFS) {
40733446Skarels 			tcp_quench(tp->t_inpcb);
40833446Skarels 			return (0);
40933446Skarels 		}
4106505Ssam 		return (error);
41133446Skarels 	}
41231442Skarels 	tcpstat.tcps_sndtotal++;
4135075Swnj 
4145075Swnj 	/*
4155075Swnj 	 * Data sent (as far as we can tell).
4165075Swnj 	 * If this advertises a larger window than any other segment,
4175245Sroot 	 * then remember the size of the advertised window.
41825940Skarels 	 * Any pending ACK has now been sent.
4195075Swnj 	 */
4205252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
4215075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
4225088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
42325940Skarels 	if (sendalot)
4247125Swnj 		goto again;
4256505Ssam 	return (0);
4264677Swnj }
4277125Swnj 
4287125Swnj tcp_setpersist(tp)
4297125Swnj 	register struct tcpcb *tp;
4307125Swnj {
43131726Skarels 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
4327125Swnj 
4337125Swnj 	if (tp->t_timer[TCPT_REXMT])
4347125Swnj 		panic("tcp_output REXMT");
4357125Swnj 	/*
4367125Swnj 	 * Start/restart persistance timer.
4377125Swnj 	 */
4387125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
43931726Skarels 	    t * tcp_backoff[tp->t_rxtshift],
44031725Skarels 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
44131725Skarels 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
44231725Skarels 		tp->t_rxtshift++;
4437125Swnj }
444