xref: /csrg-svn/sys/netinet/tcp_output.c (revision 5110)
1*5110Swnj /*	tcp_output.c	4.20	81/11/29	*/
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"
85088Swnj #include "../net/in.h"
95088Swnj #include "../net/in_pcb.h"
105088Swnj #include "../net/in_systm.h"
114804Swnj #include "../net/ip.h"
124900Swnj #include "../net/ip_var.h"
134804Swnj #include "../net/tcp.h"
145088Swnj #define	TCPOUTFLAGS
155088Swnj #include "../net/tcp_fsm.h"
165088Swnj #include "../net/tcp_seq.h"
175088Swnj #include "../net/tcp_timer.h"
184804Swnj #include "../net/tcp_var.h"
195088Swnj #include "../net/tcpip.h"
20*5110Swnj #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 	/*
435088Swnj 	 * Determine length of data that can be transmitted,
445088Swnj 	 * and flags that will be used.
455088Swnj 	 * If there is some data or critical controls (SYN, RST)
465088Swnj 	 * 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;
505088Swnj 	if (len > tp->t_maxseg)
515088Swnj 		len = tp->t_maxseg;
525088Swnj 	flags = tcp_outflags[tp->t_state];
535088Swnj 	if (len || (flags & (TH_SYN|TH_RST)))
545075Swnj 		goto send;
554678Swnj 
565075Swnj 	/*
575088Swnj 	 * See if we owe peer an ACK or have a unacked FIN to send.
585075Swnj 	 */
595088Swnj 	if (tp->t_flags & TF_ACKNOW)
605075Swnj 		goto send;
615088Swnj 	if ((so->so_state & SS_CANTSENDMORE) &&
625088Swnj 	    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 	 */
715088Swnj 	win = sbspace(&so->so_rcv);
725088Swnj 	if (win > 0 &&
735088Swnj 	    ((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 	 */
79*5110Swnj 	return (0);
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");
100*5110Swnj 	bcopy((caddr_t)tp->t_template, (caddr_t)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);
1075088Swnj 	ti->ti_ack = htonl(tp->rcv_nxt);
1085088Swnj 	if (tp->t_tcpopt) {
109*5110Swnj 		m0 = m->m_next;
1105088Swnj 		m->m_next = m_get(0);
1115088Swnj 		if (m->m_next == 0) {
1125088Swnj 			(void) m_free(m);
113*5110Swnj 			m_freem(m);
1145088Swnj 			return (0);
1155088Swnj 		}
1165088Swnj 		m->m_next->m_next = m0;
1175088Swnj 		m->m_off = MMINOFF;
1185088Swnj 		m->m_len = tp->t_tcpopt->m_len;
1195088Swnj 		bcopy(mtod(tp->t_tcpopt, caddr_t), mtod(m, caddr_t),
120*5110Swnj 		    (unsigned)tp->t_tcpopt->m_len);
1215088Swnj 		ti->ti_off = (sizeof (struct tcphdr)+tp->t_tcpopt->m_len) >> 2;
1225088Swnj 	}
1235088Swnj 	ti->ti_flags = flags;
1245075Swnj 	win = sbspace(&so->so_rcv);
1255075Swnj 	if (win > 0)
126*5110Swnj 		ti->ti_win = htons((u_short)win);
1275088Swnj 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1285088Swnj 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
1295075Swnj 		ti->ti_flags |= TH_URG;
1305075Swnj 	} else
1315075Swnj 		/*
1325075Swnj 		 * If no urgent pointer to send, then we pull
1335075Swnj 		 * the urgent pointer to the left edge of the send window
1345075Swnj 		 * so that it doesn't drift into the send window on sequence
1355075Swnj 		 * number wraparound.
1365075Swnj 		 */
1375088Swnj 		tp->snd_up = tp->snd_una;		/* drag it along */
1385088Swnj 	/* PUSH */
1395075Swnj 
1405075Swnj 	/*
1415075Swnj 	 * Put TCP length in extended header, and then
1425075Swnj 	 * checksum extended header and data.
1435075Swnj 	 */
1445075Swnj 	if (len)
1455075Swnj 		ti->ti_len = htons((u_short)(len + sizeof (struct tcphdr)));
1465088Swnj 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + len);
1475075Swnj 
1485075Swnj 	/*
1495088Swnj 	 * Advance snd_nxt over sequence space of this segment
1505088Swnj 	 */
1515088Swnj 	if (flags & (TH_SYN|TH_FIN))
1525088Swnj 		len++;
1535088Swnj 	tp->snd_nxt += len;
1545088Swnj 
1555088Swnj 	/*
1565088Swnj 	 * Arrange for retransmit and time this transmit if
1575088Swnj 	 * not already a retransmit and sending either data,
1585088Swnj 	 * SYN or FIN.
1595088Swnj 	 */
1605088Swnj 	if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1615088Swnj 		tp->rxt_seq = tp->snd_nxt - len;
1625088Swnj 		tp->rxt_time = 0;
1635088Swnj 		tp->rxt_cnt = 0;
1645088Swnj 		tp->t_timer[TCPT_REXMT] = 0;		/* XXX */
1655088Swnj 	}
1665088Swnj 
1675088Swnj 	/*
1685075Swnj 	 * Fill in IP length and desired time to live and
1695075Swnj 	 * send to IP level.
1705075Swnj 	 */
1715075Swnj 	((struct ip *)ti)->ip_len = len + sizeof (struct tcpiphdr);
1725075Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
1735088Swnj 	if (ip_output(m, tp->t_ipopt) == 0)
1745088Swnj 		return (0);
1755075Swnj 
1765075Swnj 	/*
1775075Swnj 	 * Data sent (as far as we can tell).
1785075Swnj 	 * If this advertises a larger window than any other segment,
1795075Swnj 	 * then record its sequence to be used in suppressing messages.
1805088Swnj 	 * Drop send for purpose of ACK requirements.
1815075Swnj 	 */
1825075Swnj 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1835075Swnj 		tp->rcv_adv = tp->rcv_nxt + win;
1845088Swnj 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
1855088Swnj 	tp->snd_max = tp->snd_nxt;
1865088Swnj 	return (1);
1874677Swnj }
188