xref: /csrg-svn/sys/netinet/tcp_output.c (revision 17273)
1*17273Skarels /*	tcp_output.c	6.5	84/10/19	*/
24677Swnj 
317063Sbloom #include "param.h"
417063Sbloom #include "systm.h"
517063Sbloom #include "mbuf.h"
617063Sbloom #include "protosw.h"
717063Sbloom #include "socket.h"
817063Sbloom #include "socketvar.h"
917063Sbloom #include "errno.h"
1010895Ssam 
1110895Ssam #include "../net/route.h"
1210895Ssam 
1317063Sbloom #include "in.h"
1417063Sbloom #include "in_pcb.h"
1517063Sbloom #include "in_systm.h"
1617063Sbloom #include "ip.h"
1717063Sbloom #include "ip_var.h"
1817063Sbloom #include "tcp.h"
195088Swnj #define	TCPOUTFLAGS
2017063Sbloom #include "tcp_fsm.h"
2117063Sbloom #include "tcp_seq.h"
2217063Sbloom #include "tcp_timer.h"
2317063Sbloom #include "tcp_var.h"
2417063Sbloom #include "tcpip.h"
2517063Sbloom #include "tcp_debug.h"
264677Swnj 
274678Swnj /*
288314Sroot  * Initial options.
295441Swnj  */
305441Swnj u_char	tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
315441Swnj 
325441Swnj /*
335245Sroot  * Tcp output routine: figure out what should be sent and send it.
344678Swnj  */
355075Swnj tcp_output(tp)
365075Swnj 	register struct tcpcb *tp;
374678Swnj {
385075Swnj 	register struct socket *so = tp->t_inpcb->inp_socket;
395075Swnj 	register int len;
405075Swnj 	struct mbuf *m0;
416505Ssam 	int off, flags, win, error;
425075Swnj 	register struct mbuf *m;
435075Swnj 	register struct tcpiphdr *ti;
445441Swnj 	u_char *opt;
455441Swnj 	unsigned optlen = 0;
467125Swnj 	int sendalot;
474678Swnj 
485075Swnj 	/*
496279Swnj 	 * Determine length of data that should be transmitted,
505088Swnj 	 * and flags that will be used.
515088Swnj 	 * If there is some data or critical controls (SYN, RST)
525088Swnj 	 * to send, then transmit; otherwise, investigate further.
535075Swnj 	 */
547125Swnj again:
557125Swnj 	sendalot = 0;
565075Swnj 	off = tp->snd_nxt - tp->snd_una;
575163Swnj 	len = MIN(so->so_snd.sb_cc, tp->snd_wnd+tp->t_force) - off;
585285Sroot 	if (len < 0)
596505Ssam 		return (0);	/* ??? */	/* past FIN */
607125Swnj 	if (len > tp->t_maxseg) {
615088Swnj 		len = tp->t_maxseg;
627125Swnj 		sendalot = 1;
637125Swnj 	}
646279Swnj 
655088Swnj 	flags = tcp_outflags[tp->t_state];
665299Sroot 	if (tp->snd_nxt + len < tp->snd_una + so->so_snd.sb_cc)
675163Swnj 		flags &= ~TH_FIN;
686279Swnj 	if (flags & (TH_SYN|TH_RST|TH_FIN))
695075Swnj 		goto send;
706279Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_una))
716279Swnj 		goto send;
724678Swnj 
735075Swnj 	/*
746279Swnj 	 * Sender silly window avoidance.  If can send all data,
756279Swnj 	 * a maximum segment, at least 1/4 of window do it,
766279Swnj 	 * or are forced, do it; otherwise don't bother.
776279Swnj 	 */
786279Swnj 	if (len) {
796279Swnj 		if (len == tp->t_maxseg || off+len >= so->so_snd.sb_cc)
806279Swnj 			goto send;
816279Swnj 		if (len * 4 >= tp->snd_wnd)		/* a lot */
826279Swnj 			goto send;
836279Swnj 		if (tp->t_force)
846279Swnj 			goto send;
856279Swnj 	}
866279Swnj 
876279Swnj 	/*
885285Sroot 	 * Send if we owe peer an ACK.
895075Swnj 	 */
905441Swnj 	if (tp->t_flags&TF_ACKNOW)
915075Swnj 		goto send;
924678Swnj 
935441Swnj 
945441Swnj 	/*
955075Swnj 	 * Calculate available window in i, and also amount
965075Swnj 	 * of window known to peer (as advertised window less
975075Swnj 	 * next expected input.)  If this is 35% or more of the
985075Swnj 	 * maximum possible window, then want to send a segment to peer.
995075Swnj 	 */
1005088Swnj 	win = sbspace(&so->so_rcv);
1015088Swnj 	if (win > 0 &&
1025088Swnj 	    ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
1035075Swnj 		goto send;
1044678Swnj 
1055075Swnj 	/*
1067125Swnj 	 * TCP window updates are not reliable, rather a polling protocol
1077125Swnj 	 * using ``persist'' packets is used to insure receipt of window
1087125Swnj 	 * updates.  The three ``states'' for the output side are:
1097125Swnj 	 *	idle			not doing retransmits or persists
1107125Swnj 	 *	persisting		to move a zero window
1117125Swnj 	 *	(re)transmitting	and thereby not persisting
1127125Swnj 	 *
1137125Swnj 	 * tp->t_timer[TCPT_PERSIST]
1147125Swnj 	 *	is set when we are in persist state.
1157125Swnj 	 * tp->t_force
1167125Swnj 	 *	is set when we are called to send a persist packet.
1177125Swnj 	 * tp->t_timer[TCPT_REXMT]
1187125Swnj 	 *	is set when we are retransmitting
1197125Swnj 	 * The output side is idle when both timers are zero.
1207125Swnj 	 *
1217125Swnj 	 * If send window is closed, there is data to transmit, and no
1227125Swnj 	 * retransmit or persist is pending, then go to persist state,
1237125Swnj 	 * arranging to force out a byte to get more current window information
1247125Swnj 	 * if nothing happens soon.
1257125Swnj 	 */
1267125Swnj 	if (tp->snd_wnd == 0 && so->so_snd.sb_cc &&
1277125Swnj 	    tp->t_timer[TCPT_REXMT] == 0 && tp->t_timer[TCPT_PERSIST] == 0) {
1287125Swnj 		tp->t_rxtshift = 0;
1297125Swnj 		tcp_setpersist(tp);
1307125Swnj 	}
1317125Swnj 
1327125Swnj 	/*
1335075Swnj 	 * No reason to send a segment, just return.
1345075Swnj 	 */
1355110Swnj 	return (0);
1364678Swnj 
1375075Swnj send:
1385075Swnj 	/*
1395075Swnj 	 * Grab a header mbuf, attaching a copy of data to
1405075Swnj 	 * be transmitted, and initialize the header from
1415075Swnj 	 * the template for sends on this connection.
1425075Swnj 	 */
14311720Ssam 	MGET(m, M_DONTWAIT, MT_HEADER);
14411720Ssam 	if (m == NULL)
1456505Ssam 		return (ENOBUFS);
1465245Sroot 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
1474885Swnj 	m->m_len = sizeof (struct tcpiphdr);
1485075Swnj 	if (len) {
1495075Swnj 		m->m_next = m_copy(so->so_snd.sb_mb, off, len);
1505075Swnj 		if (m->m_next == 0)
1515075Swnj 			len = 0;
1525075Swnj 	}
1535075Swnj 	ti = mtod(m, struct tcpiphdr *);
1545075Swnj 	if (tp->t_template == 0)
1555075Swnj 		panic("tcp_output");
1565110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
1575075Swnj 
1585075Swnj 	/*
1595075Swnj 	 * Fill in fields, remembering maximum advertised
1605075Swnj 	 * window for use in delaying messages about window sizes.
1615075Swnj 	 */
1625245Sroot 	ti->ti_seq = tp->snd_nxt;
1635245Sroot 	ti->ti_ack = tp->rcv_nxt;
1645245Sroot 	ti->ti_seq = htonl(ti->ti_seq);
1655245Sroot 	ti->ti_ack = htonl(ti->ti_ack);
1665441Swnj 	/*
1675441Swnj 	 * Before ESTABLISHED, force sending of initial options
1685441Swnj 	 * unless TCP set to not do any options.
1695441Swnj 	 */
1705441Swnj 	if (tp->t_state < TCPS_ESTABLISHED) {
171*17273Skarels 		int mss;
172*17273Skarels 
1735441Swnj 		if (tp->t_flags&TF_NOOPT)
1745441Swnj 			goto noopt;
175*17273Skarels 		mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp));
176*17273Skarels 		if (mss <= IP_MSS - sizeof(struct tcpiphdr))
177*17273Skarels 			goto noopt;
1785441Swnj 		opt = tcp_initopt;
1795441Swnj 		optlen = sizeof (tcp_initopt);
180*17273Skarels 		*(u_short *)(opt + 2) = htons(mss);
1815441Swnj 	} else {
1825441Swnj 		if (tp->t_tcpopt == 0)
1835441Swnj 			goto noopt;
1845441Swnj 		opt = mtod(tp->t_tcpopt, u_char *);
1855441Swnj 		optlen = tp->t_tcpopt->m_len;
1865441Swnj 	}
1878314Sroot 	if (opt) {
1885110Swnj 		m0 = m->m_next;
1899643Ssam 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
1905088Swnj 		if (m->m_next == 0) {
1915088Swnj 			(void) m_free(m);
1925441Swnj 			m_freem(m0);
1936505Ssam 			return (ENOBUFS);
1945088Swnj 		}
1955088Swnj 		m->m_next->m_next = m0;
1965441Swnj 		m0 = m->m_next;
1975441Swnj 		m0->m_len = optlen;
1986162Ssam 		bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
1995441Swnj 		opt = (u_char *)(mtod(m0, caddr_t) + optlen);
2005441Swnj 		while (m0->m_len & 0x3) {
2015441Swnj 			*opt++ = TCPOPT_EOL;
2025441Swnj 			m0->m_len++;
2035441Swnj 		}
2045441Swnj 		optlen = m0->m_len;
2055441Swnj 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
2065088Swnj 	}
2075441Swnj noopt:
2085088Swnj 	ti->ti_flags = flags;
2095075Swnj 	win = sbspace(&so->so_rcv);
2106279Swnj 	if (win < so->so_rcv.sb_hiwat / 4)	/* avoid silly window */
2116279Swnj 		win = 0;
2125075Swnj 	if (win > 0)
2135110Swnj 		ti->ti_win = htons((u_short)win);
2145088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
2155420Swnj 		ti->ti_urp = tp->snd_up - tp->snd_nxt;
2165420Swnj 		ti->ti_urp = htons(ti->ti_urp);
2175075Swnj 		ti->ti_flags |= TH_URG;
2185075Swnj 	} else
2195075Swnj 		/*
2205075Swnj 		 * If no urgent pointer to send, then we pull
2215075Swnj 		 * the urgent pointer to the left edge of the send window
2225075Swnj 		 * so that it doesn't drift into the send window on sequence
2235075Swnj 		 * number wraparound.
2245075Swnj 		 */
2255088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
2267644Sroot 	/*
2277644Sroot 	 * If anything to send and we can send it all, set PUSH.
2287644Sroot 	 * (This will keep happy those implementations which only
22910143Ssam 	 * give data to the user when a buffer fills or a PUSH comes in.)
2307644Sroot 	 */
2317644Sroot 	if (len && off+len == so->so_snd.sb_cc)
2327644Sroot 		ti->ti_flags |= TH_PUSH;
2335075Swnj 
2345075Swnj 	/*
2355075Swnj 	 * Put TCP length in extended header, and then
2365075Swnj 	 * checksum extended header and data.
2375075Swnj 	 */
2385441Swnj 	if (len + optlen) {
2395441Swnj 		ti->ti_len = sizeof (struct tcphdr) + optlen + len;
2405441Swnj 		ti->ti_len = htons((u_short)ti->ti_len);
2415441Swnj 	}
2426162Ssam 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + (int)optlen + len);
2435075Swnj 
2445075Swnj 	/*
2457125Swnj 	 * In transmit state, time the transmission and arrange for
2467125Swnj 	 * the retransmit.  In persist state, reset persist time for
2477125Swnj 	 * next persist.
2485088Swnj 	 */
2497125Swnj 	if (tp->t_force == 0) {
2507125Swnj 		/*
2517146Swnj 		 * Advance snd_nxt over sequence space of this segment.
2527125Swnj 		 */
2537125Swnj 		if (flags & (TH_SYN|TH_FIN))
2547125Swnj 			tp->snd_nxt++;
2557125Swnj 		tp->snd_nxt += len;
25615385Ssam 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
2577149Swnj 			tp->snd_max = tp->snd_nxt;
25815385Ssam 			/*
25915385Ssam 			 * Time this transmission if not a retransmission and
26015385Ssam 			 * not currently timing anything.
26115385Ssam 			 */
26215385Ssam 			if (tp->t_rtt == 0) {
26315385Ssam 				tp->t_rtt = 1;
26415385Ssam 				tp->t_rtseq = tp->snd_nxt - len;
26515385Ssam 			}
2667125Swnj 		}
2675088Swnj 
2687125Swnj 		/*
2697125Swnj 		 * Set retransmit timer if not currently set.
2707125Swnj 		 * Initial value for retransmit timer to tcp_beta*tp->t_srtt.
2717125Swnj 		 * Initialize shift counter which is used for exponential
2727125Swnj 		 * backoff of retransmit time.
2737125Swnj 		 */
2747125Swnj 		if (tp->t_timer[TCPT_REXMT] == 0 &&
2757125Swnj 		    tp->snd_nxt != tp->snd_una) {
2767125Swnj 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
2777125Swnj 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
2787125Swnj 			tp->t_rxtshift = 0;
2797125Swnj 		}
2807125Swnj 		tp->t_timer[TCPT_PERSIST] = 0;
2817149Swnj 	} else {
2827149Swnj 		if (SEQ_GT(tp->snd_una+1, tp->snd_max))
2837149Swnj 			tp->snd_max = tp->snd_una+1;
2847146Swnj 	}
2855163Swnj 
2865163Swnj 	/*
2875268Sroot 	 * Trace.
2885268Sroot 	 */
2897146Swnj 	if (so->so_options & SO_DEBUG)
2905268Sroot 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
2915268Sroot 
2925268Sroot 	/*
2935075Swnj 	 * Fill in IP length and desired time to live and
2945075Swnj 	 * send to IP level.
2955075Swnj 	 */
2965441Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
2975075Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
29812418Ssam 	if (so->so_options & SO_DONTROUTE)
29912765Ssam 		error =
30012765Ssam 		   ip_output(m, tp->t_ipopt, (struct route *)0, IP_ROUTETOIF);
30112418Ssam 	else
30212418Ssam 		error = ip_output(m, tp->t_ipopt, &tp->t_inpcb->inp_route, 0);
30312418Ssam 	if (error)
3046505Ssam 		return (error);
3055075Swnj 
3065075Swnj 	/*
3075075Swnj 	 * Data sent (as far as we can tell).
3085075Swnj 	 * If this advertises a larger window than any other segment,
3095245Sroot 	 * then remember the size of the advertised window.
3105088Swnj 	 * Drop send for purpose of ACK requirements.
3115075Swnj 	 */
3125252Sroot 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
3135075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
3145088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
3157125Swnj 	if (sendalot && tp->t_force == 0)
3167125Swnj 		goto again;
3176505Ssam 	return (0);
3184677Swnj }
3197125Swnj 
3207125Swnj tcp_setpersist(tp)
3217125Swnj 	register struct tcpcb *tp;
3227125Swnj {
3237125Swnj 
3247125Swnj 	if (tp->t_timer[TCPT_REXMT])
3257125Swnj 		panic("tcp_output REXMT");
3267125Swnj 	/*
3277125Swnj 	 * Start/restart persistance timer.
3287125Swnj 	 */
3297125Swnj 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
3307125Swnj 	    ((int)(tcp_beta * tp->t_srtt)) << tp->t_rxtshift,
3317125Swnj 	    TCPTV_PERSMIN, TCPTV_MAX);
3327125Swnj 	tp->t_rxtshift++;
3337125Swnj 	if (tp->t_rxtshift >= TCP_MAXRXTSHIFT)
3347125Swnj 		tp->t_rxtshift = 0;
3357125Swnj }
336