xref: /csrg-svn/sys/netinet/tcp_output.c (revision 5088)
1*5088Swnj /*	tcp_output.c	4.19	81/11/26	*/
24677Swnj 
34677Swnj #include "../h/param.h"
44677Swnj #include "../h/systm.h"
54677Swnj #include "../h/mbuf.h"
64677Swnj #include "../h/socket.h"
74804Swnj #include "../h/socketvar.h"
8*5088Swnj #include "../net/in.h"
9*5088Swnj #include "../net/in_pcb.h"
10*5088Swnj #include "../net/in_systm.h"
114804Swnj #include "../net/ip.h"
124900Swnj #include "../net/ip_var.h"
134804Swnj #include "../net/tcp.h"
14*5088Swnj #define	TCPOUTFLAGS
15*5088Swnj #include "../net/tcp_fsm.h"
16*5088Swnj #include "../net/tcp_seq.h"
17*5088Swnj #include "../net/tcp_timer.h"
184804Swnj #include "../net/tcp_var.h"
19*5088Swnj #include "../net/tcpip.h"
204804Swnj #include "/usr/include/errno.h"
214677Swnj 
224678Swnj /*
235075Swnj  * Tcp output routine: figure out what should be sent
245075Swnj  * and, if nothing, send a null segment anyways if force is nonzero
255075Swnj  * (e.g. to be sure to send an ACK).
265075Swnj  *
275075Swnj  * This routine can be called only after SYNs have been exchanged.
284678Swnj  */
295075Swnj tcp_output(tp)
305075Swnj 	register struct tcpcb *tp;
314678Swnj {
325075Swnj 	register struct socket *so = tp->t_inpcb->inp_socket;
335075Swnj 	register int len;
345075Swnj 	struct mbuf *m0;
355075Swnj 	int off, flags;
365075Swnj 	register struct mbuf *m;
375075Swnj 	register struct tcpiphdr *ti;
385075Swnj 	int win;
394678Swnj 
405075Swnj COUNT(TCP_OUTPUT);
414678Swnj 
425075Swnj 	/*
43*5088Swnj 	 * Determine length of data that can be transmitted,
44*5088Swnj 	 * and flags that will be used.
45*5088Swnj 	 * If there is some data or critical controls (SYN, RST)
46*5088Swnj 	 * to send, then transmit; otherwise, investigate further.
475075Swnj 	 */
485075Swnj 	off = tp->snd_nxt - tp->snd_una;
495075Swnj 	len = MIN(so->so_snd.sb_cc, tp->snd_wnd) - off;
50*5088Swnj 	if (len > tp->t_maxseg)
51*5088Swnj 		len = tp->t_maxseg;
52*5088Swnj 	flags = tcp_outflags[tp->t_state];
53*5088Swnj 	if (len || (flags & (TH_SYN|TH_RST)))
545075Swnj 		goto send;
554678Swnj 
565075Swnj 	/*
57*5088Swnj 	 * See if we owe peer an ACK or have a unacked FIN to send.
585075Swnj 	 */
59*5088Swnj 	if (tp->t_flags & TF_ACKNOW)
605075Swnj 		goto send;
61*5088Swnj 	if ((so->so_state & SS_CANTSENDMORE) &&
62*5088Swnj 	    TCPS_OURFINNOTACKED(tp->t_state))
635075Swnj 		goto send;
644678Swnj 
655075Swnj 	/*
665075Swnj 	 * Calculate available window in i, and also amount
675075Swnj 	 * of window known to peer (as advertised window less
685075Swnj 	 * next expected input.)  If this is 35% or more of the
695075Swnj 	 * maximum possible window, then want to send a segment to peer.
705075Swnj 	 */
71*5088Swnj 	win = sbspace(&so->so_rcv);
72*5088Swnj 	if (win > 0 &&
73*5088Swnj 	    ((100*(win-(tp->rcv_adv-tp->rcv_nxt))/so->so_rcv.sb_hiwat) >= 35))
745075Swnj 		goto send;
754678Swnj 
765075Swnj 	/*
775075Swnj 	 * No reason to send a segment, just return.
785075Swnj 	 */
795075Swnj 	return;
804678Swnj 
815075Swnj send:
825075Swnj 	/*
835075Swnj 	 * Grab a header mbuf, attaching a copy of data to
845075Swnj 	 * be transmitted, and initialize the header from
855075Swnj 	 * the template for sends on this connection.
865075Swnj 	 */
874677Swnj 	MGET(m, 0);
884677Swnj 	if (m == 0)
894677Swnj 		return (0);
904885Swnj 	m->m_off = MMAXOFF - sizeof(struct tcpiphdr);
914885Swnj 	m->m_len = sizeof (struct tcpiphdr);
925075Swnj 	if (len) {
935075Swnj 		m->m_next = m_copy(so->so_snd.sb_mb, off, len);
945075Swnj 		if (m->m_next == 0)
955075Swnj 			len = 0;
965075Swnj 	}
975075Swnj 	ti = mtod(m, struct tcpiphdr *);
985075Swnj 	if (tp->t_template == 0)
995075Swnj 		panic("tcp_output");
1005075Swnj 	bcopy((caddr_t)tp->t_template, ti, sizeof (struct tcpiphdr));
1015075Swnj 
1025075Swnj 	/*
1035075Swnj 	 * Fill in fields, remembering maximum advertised
1045075Swnj 	 * window for use in delaying messages about window sizes.
1055075Swnj 	 */
1065075Swnj 	ti->ti_seq = htonl(tp->snd_nxt);
107*5088Swnj 	ti->ti_ack = htonl(tp->rcv_nxt);
108*5088Swnj 	if (tp->t_tcpopt) {
109*5088Swnj 		m->m_next = m_get(0);
110*5088Swnj 		if (m->m_next == 0) {
111*5088Swnj 			(void) m_free(m);
112*5088Swnj 			return (0);
113*5088Swnj 		}
114*5088Swnj 		m->m_next->m_next = m0;
115*5088Swnj 		m->m_off = MMINOFF;
116*5088Swnj 		m->m_len = tp->t_tcpopt->m_len;
117*5088Swnj 		bcopy(mtod(tp->t_tcpopt, caddr_t), mtod(m, caddr_t),
118*5088Swnj 		    tp->t_tcpopt->m_len);
119*5088Swnj 		ti->ti_off = (sizeof (struct tcphdr)+tp->t_tcpopt->m_len) >> 2;
120*5088Swnj 	}
121*5088Swnj 	ti->ti_flags = flags;
1225075Swnj 	win = sbspace(&so->so_rcv);
1235075Swnj 	if (win > 0)
1245075Swnj 		ti->ti_win = htons(win);
125*5088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
126*5088Swnj 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
1275075Swnj 		ti->ti_flags |= TH_URG;
1285075Swnj 	} else
1295075Swnj 		/*
1305075Swnj 		 * If no urgent pointer to send, then we pull
1315075Swnj 		 * the urgent pointer to the left edge of the send window
1325075Swnj 		 * so that it doesn't drift into the send window on sequence
1335075Swnj 		 * number wraparound.
1345075Swnj 		 */
135*5088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
136*5088Swnj 	/* PUSH */
1375075Swnj 
1385075Swnj 	/*
1395075Swnj 	 * Put TCP length in extended header, and then
1405075Swnj 	 * checksum extended header and data.
1415075Swnj 	 */
1425075Swnj 	if (len)
1435075Swnj 		ti->ti_len = htons((u_short)(len + sizeof (struct tcphdr)));
144*5088Swnj 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + len);
1455075Swnj 
1465075Swnj 	/*
147*5088Swnj 	 * Advance snd_nxt over sequence space of this segment
148*5088Swnj 	 */
149*5088Swnj 	if (flags & (TH_SYN|TH_FIN))
150*5088Swnj 		len++;
151*5088Swnj 	tp->snd_nxt += len;
152*5088Swnj 
153*5088Swnj 	/*
154*5088Swnj 	 * Arrange for retransmit and time this transmit if
155*5088Swnj 	 * not already a retransmit and sending either data,
156*5088Swnj 	 * SYN or FIN.
157*5088Swnj 	 */
158*5088Swnj 	if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
159*5088Swnj 		tp->rxt_seq = tp->snd_nxt - len;
160*5088Swnj 		tp->rxt_time = 0;
161*5088Swnj 		tp->rxt_cnt = 0;
162*5088Swnj 		tp->t_timer[TCPT_REXMT] = 0;		/* XXX */
163*5088Swnj 	}
164*5088Swnj 
165*5088Swnj 	/*
1665075Swnj 	 * Fill in IP length and desired time to live and
1675075Swnj 	 * send to IP level.
1685075Swnj 	 */
1695075Swnj 	((struct ip *)ti)->ip_len = len + sizeof (struct tcpiphdr);
1705075Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
171*5088Swnj 	if (ip_output(m, tp->t_ipopt) == 0)
172*5088Swnj 		return (0);
1735075Swnj 
1745075Swnj 	/*
1755075Swnj 	 * Data sent (as far as we can tell).
1765075Swnj 	 * If this advertises a larger window than any other segment,
1775075Swnj 	 * then record its sequence to be used in suppressing messages.
178*5088Swnj 	 * Drop send for purpose of ACK requirements.
1795075Swnj 	 */
1805075Swnj 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1815075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
182*5088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
183*5088Swnj 	tp->snd_max = tp->snd_nxt;
184*5088Swnj 	return (1);
1854677Swnj }
186