Home | History | Annotate | Line # | Download | only in netinet
tcp_output.c revision 1.6
      1 /*
      2  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: @(#)tcp_output.c	7.22 (Berkeley) 8/31/90
     34  *	$Id: tcp_output.c,v 1.6 1994/01/08 23:07:20 mycroft Exp $
     35  */
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/malloc.h>
     40 #include <sys/mbuf.h>
     41 #include <sys/protosw.h>
     42 #include <sys/socket.h>
     43 #include <sys/socketvar.h>
     44 #include <sys/errno.h>
     45 
     46 #include <net/route.h>
     47 
     48 #include <netinet/in.h>
     49 #include <netinet/in_systm.h>
     50 #include <netinet/ip.h>
     51 #include <netinet/in_pcb.h>
     52 #include <netinet/ip_var.h>
     53 #include <netinet/tcp.h>
     54 #define	TCPOUTFLAGS
     55 #include <netinet/tcp_fsm.h>
     56 #include <netinet/tcp_seq.h>
     57 #include <netinet/tcp_timer.h>
     58 #include <netinet/tcp_var.h>
     59 #include <netinet/tcpip.h>
     60 #include <netinet/tcp_debug.h>
     61 
     62 #ifdef notyet
     63 extern struct mbuf *m_copypack();
     64 #endif
     65 
     66 /*
     67  * Initial options.
     68  */
     69 u_char	tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
     70 
     71 /*
     72  * Tcp output routine: figure out what should be sent and send it.
     73  */
     74 int
     75 tcp_output(tp)
     76 	register struct tcpcb *tp;
     77 {
     78 	register struct socket *so = tp->t_inpcb->inp_socket;
     79 	register long len, win;
     80 	int off, flags, error;
     81 	register struct mbuf *m;
     82 	register struct tcpiphdr *ti;
     83 	u_char *opt;
     84 	unsigned optlen, hdrlen;
     85 	int idle, sendalot;
     86 
     87 	/*
     88 	 * Determine length of data that should be transmitted,
     89 	 * and flags that will be used.
     90 	 * If there is some data or critical controls (SYN, RST)
     91 	 * to send, then transmit; otherwise, investigate further.
     92 	 */
     93 	idle = (tp->snd_max == tp->snd_una);
     94 	if (idle && tp->t_idle >= tp->t_rxtcur)
     95 		/*
     96 		 * We have been idle for "a while" and no acks are
     97 		 * expected to clock out any data we send --
     98 		 * slow start to get ack "clock" running again.
     99 		 */
    100 		tp->snd_cwnd = tp->t_maxseg;
    101 again:
    102 	sendalot = 0;
    103 	off = tp->snd_nxt - tp->snd_una;
    104 	win = min(tp->snd_wnd, tp->snd_cwnd);
    105 
    106 	/*
    107 	 * If in persist timeout with window of 0, send 1 byte.
    108 	 * Otherwise, if window is small but nonzero
    109 	 * and timer expired, we will send what we can
    110 	 * and go to transmit state.
    111 	 */
    112 	if (tp->t_force) {
    113 		if (win == 0)
    114 			win = 1;
    115 		else {
    116 			tp->t_timer[TCPT_PERSIST] = 0;
    117 			tp->t_rxtshift = 0;
    118 		}
    119 	}
    120 
    121 	flags = tcp_outflags[tp->t_state];
    122 	len = min(so->so_snd.sb_cc, win) - off;
    123 
    124 	if (len < 0) {
    125 		/*
    126 		 * If FIN has been sent but not acked,
    127 		 * but we haven't been called to retransmit,
    128 		 * len will be -1.  Otherwise, window shrank
    129 		 * after we sent into it.  If window shrank to 0,
    130 		 * cancel pending retransmit and pull snd_nxt
    131 		 * back to (closed) window.  We will enter persist
    132 		 * state below.  If the window didn't close completely,
    133 		 * just wait for an ACK.
    134 		 */
    135 		len = 0;
    136 		if (win == 0) {
    137 			tp->t_timer[TCPT_REXMT] = 0;
    138 			tp->snd_nxt = tp->snd_una;
    139 		}
    140 	}
    141 	if (len > tp->t_maxseg) {
    142 		len = tp->t_maxseg;
    143 		sendalot = 1;
    144 	}
    145 	if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
    146 		flags &= ~TH_FIN;
    147 
    148 	win = sbspace(&so->so_rcv);
    149 
    150 	/*
    151 	 * Sender silly window avoidance.  If connection is idle
    152 	 * and can send all data, a maximum segment,
    153 	 * at least a maximum default-size segment do it,
    154 	 * or are forced, do it; otherwise don't bother.
    155 	 * If peer's buffer is tiny, then send
    156 	 * when window is at least half open.
    157 	 * If retransmitting (possibly after persist timer forced us
    158 	 * to send into a small window), then must resend.
    159 	 */
    160 	if (len) {
    161 		if (len == tp->t_maxseg)
    162 			goto send;
    163 		if ((idle || tp->t_flags & TF_NODELAY) &&
    164 		    len + off >= so->so_snd.sb_cc)
    165 			goto send;
    166 		if (tp->t_force)
    167 			goto send;
    168 		if (len >= tp->max_sndwnd / 2)
    169 			goto send;
    170 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
    171 			goto send;
    172 	}
    173 
    174 	/*
    175 	 * Compare available window to amount of window
    176 	 * known to peer (as advertised window less
    177 	 * next expected input).  If the difference is at least two
    178 	 * max size segments, or at least 50% of the maximum possible
    179 	 * window, then want to send a window update to peer.
    180 	 */
    181 	if (win > 0) {
    182 		long adv = win - (tp->rcv_adv - tp->rcv_nxt);
    183 
    184 		if (adv >= (long) (2 * tp->t_maxseg))
    185 			goto send;
    186 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
    187 			goto send;
    188 	}
    189 
    190 	/*
    191 	 * Send if we owe peer an ACK.
    192 	 */
    193 	if (tp->t_flags & TF_ACKNOW)
    194 		goto send;
    195 	if (flags & (TH_SYN|TH_RST))
    196 		goto send;
    197 	if (SEQ_GT(tp->snd_up, tp->snd_una))
    198 		goto send;
    199 	/*
    200 	 * If our state indicates that FIN should be sent
    201 	 * and we have not yet done so, or we're retransmitting the FIN,
    202 	 * then we need to send.
    203 	 */
    204 	if (flags & TH_FIN &&
    205 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
    206 		goto send;
    207 
    208 	/*
    209 	 * TCP window updates are not reliable, rather a polling protocol
    210 	 * using ``persist'' packets is used to insure receipt of window
    211 	 * updates.  The three ``states'' for the output side are:
    212 	 *	idle			not doing retransmits or persists
    213 	 *	persisting		to move a small or zero window
    214 	 *	(re)transmitting	and thereby not persisting
    215 	 *
    216 	 * tp->t_timer[TCPT_PERSIST]
    217 	 *	is set when we are in persist state.
    218 	 * tp->t_force
    219 	 *	is set when we are called to send a persist packet.
    220 	 * tp->t_timer[TCPT_REXMT]
    221 	 *	is set when we are retransmitting
    222 	 * The output side is idle when both timers are zero.
    223 	 *
    224 	 * If send window is too small, there is data to transmit, and no
    225 	 * retransmit or persist is pending, then go to persist state.
    226 	 * If nothing happens soon, send when timer expires:
    227 	 * if window is nonzero, transmit what we can,
    228 	 * otherwise force out a byte.
    229 	 */
    230 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
    231 	    tp->t_timer[TCPT_PERSIST] == 0) {
    232 		tp->t_rxtshift = 0;
    233 		tcp_setpersist(tp);
    234 	}
    235 
    236 	/*
    237 	 * No reason to send a segment, just return.
    238 	 */
    239 	return (0);
    240 
    241 send:
    242 	/*
    243 	 * Before ESTABLISHED, force sending of initial options
    244 	 * unless TCP set not to do any options.
    245 	 * NOTE: we assume that the IP/TCP header plus TCP options
    246 	 * always fit in a single mbuf, leaving room for a maximum
    247 	 * link header, i.e.
    248 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
    249 	 */
    250 	optlen = 0;
    251 	hdrlen = sizeof (struct tcpiphdr);
    252 	if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) {
    253 		opt = tcp_initopt;
    254 		optlen = sizeof (tcp_initopt);
    255 		hdrlen += sizeof (tcp_initopt);
    256 		*(u_short *)(opt + 2) = htons((u_short) tcp_mss(tp, 0));
    257 #ifdef DIAGNOSTIC
    258 	 	if (max_linkhdr + hdrlen > MHLEN)
    259 			panic("tcphdr too big");
    260 #endif
    261 	}
    262 
    263 	/*
    264 	 * Grab a header mbuf, attaching a copy of data to
    265 	 * be transmitted, and initialize the header from
    266 	 * the template for sends on this connection.
    267 	 */
    268 	if (len) {
    269 		if (tp->t_force && len == 1)
    270 			tcpstat.tcps_sndprobe++;
    271 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
    272 			tcpstat.tcps_sndrexmitpack++;
    273 			tcpstat.tcps_sndrexmitbyte += len;
    274 		} else {
    275 			tcpstat.tcps_sndpack++;
    276 			tcpstat.tcps_sndbyte += len;
    277 		}
    278 #ifdef notyet
    279 		if ((m = m_copypack(so->so_snd.sb_mb, off,
    280 		    (int)len, max_linkhdr + hdrlen)) == 0) {
    281 			error = ENOBUFS;
    282 			goto out;
    283 		}
    284 		/*
    285 		 * m_copypack left space for our hdr; use it.
    286 		 */
    287 		m->m_len += hdrlen;
    288 		m->m_data -= hdrlen;
    289 #else
    290 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    291 		if (m == NULL) {
    292 			error = ENOBUFS;
    293 			goto out;
    294 		}
    295 		m->m_data += max_linkhdr;
    296 		m->m_len = hdrlen;
    297 		if (len <= MHLEN - hdrlen - max_linkhdr) {
    298 			m_copydata(so->so_snd.sb_mb, off, (int) len,
    299 			    mtod(m, caddr_t) + hdrlen);
    300 			m->m_len += len;
    301 		} else {
    302 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
    303 			if (m->m_next == 0)
    304 				len = 0;
    305 		}
    306 #endif
    307 		/*
    308 		 * If we're sending everything we've got, set PUSH.
    309 		 * (This will keep happy those implementations which only
    310 		 * give data to the user when a buffer fills or
    311 		 * a PUSH comes in.)
    312 		 */
    313 		if (off + len == so->so_snd.sb_cc)
    314 			flags |= TH_PUSH;
    315 	} else {
    316 		if (tp->t_flags & TF_ACKNOW)
    317 			tcpstat.tcps_sndacks++;
    318 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
    319 			tcpstat.tcps_sndctrl++;
    320 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
    321 			tcpstat.tcps_sndurg++;
    322 		else
    323 			tcpstat.tcps_sndwinup++;
    324 
    325 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    326 		if (m == NULL) {
    327 			error = ENOBUFS;
    328 			goto out;
    329 		}
    330 		m->m_data += max_linkhdr;
    331 		m->m_len = hdrlen;
    332 	}
    333 	m->m_pkthdr.rcvif = (struct ifnet *)0;
    334 	ti = mtod(m, struct tcpiphdr *);
    335 	if (tp->t_template == 0)
    336 		panic("tcp_output");
    337 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
    338 
    339 	/*
    340 	 * Fill in fields, remembering maximum advertised
    341 	 * window for use in delaying messages about window sizes.
    342 	 * If resending a FIN, be sure not to use a new sequence number.
    343 	 */
    344 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
    345 	    tp->snd_nxt == tp->snd_max)
    346 		tp->snd_nxt--;
    347 	ti->ti_seq = htonl(tp->snd_nxt);
    348 	ti->ti_ack = htonl(tp->rcv_nxt);
    349 	if (optlen) {
    350 		bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
    351 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
    352 	}
    353 	ti->ti_flags = flags;
    354 	/*
    355 	 * Calculate receive window.  Don't shrink window,
    356 	 * but avoid silly window syndrome.
    357 	 */
    358 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
    359 		win = 0;
    360 	if (win > TCP_MAXWIN)
    361 		win = TCP_MAXWIN;
    362 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
    363 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
    364 	ti->ti_win = htons((u_short)win);
    365 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
    366 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
    367 		ti->ti_flags |= TH_URG;
    368 	} else
    369 		/*
    370 		 * If no urgent pointer to send, then we pull
    371 		 * the urgent pointer to the left edge of the send window
    372 		 * so that it doesn't drift into the send window on sequence
    373 		 * number wraparound.
    374 		 */
    375 		tp->snd_up = tp->snd_una;		/* drag it along */
    376 
    377 	/*
    378 	 * Put TCP length in extended header, and then
    379 	 * checksum extended header and data.
    380 	 */
    381 	if (len + optlen)
    382 		ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
    383 		    optlen + len));
    384 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
    385 
    386 	/*
    387 	 * In transmit state, time the transmission and arrange for
    388 	 * the retransmit.  In persist state, just set snd_max.
    389 	 */
    390 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
    391 		tcp_seq startseq = tp->snd_nxt;
    392 
    393 		/*
    394 		 * Advance snd_nxt over sequence space of this segment.
    395 		 */
    396 		if (flags & (TH_SYN|TH_FIN)) {
    397 			if (flags & TH_SYN)
    398 				tp->snd_nxt++;
    399 			if (flags & TH_FIN) {
    400 				tp->snd_nxt++;
    401 				tp->t_flags |= TF_SENTFIN;
    402 			}
    403 		}
    404 		tp->snd_nxt += len;
    405 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
    406 			tp->snd_max = tp->snd_nxt;
    407 			/*
    408 			 * Time this transmission if not a retransmission and
    409 			 * not currently timing anything.
    410 			 */
    411 			if (tp->t_rtt == 0) {
    412 				tp->t_rtt = 1;
    413 				tp->t_rtseq = startseq;
    414 				tcpstat.tcps_segstimed++;
    415 			}
    416 		}
    417 
    418 		/*
    419 		 * Set retransmit timer if not currently set,
    420 		 * and not doing an ack or a keep-alive probe.
    421 		 * Initial value for retransmit timer is smoothed
    422 		 * round-trip time + 2 * round-trip time variance.
    423 		 * Initialize shift counter which is used for backoff
    424 		 * of retransmit time.
    425 		 */
    426 		if (tp->t_timer[TCPT_REXMT] == 0 &&
    427 		    tp->snd_nxt != tp->snd_una) {
    428 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
    429 			if (tp->t_timer[TCPT_PERSIST]) {
    430 				tp->t_timer[TCPT_PERSIST] = 0;
    431 				tp->t_rxtshift = 0;
    432 			}
    433 		}
    434 	} else
    435 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
    436 			tp->snd_max = tp->snd_nxt + len;
    437 
    438 	/*
    439 	 * Trace.
    440 	 */
    441 	if (so->so_options & SO_DEBUG)
    442 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
    443 
    444 	/*
    445 	 * Fill in IP length and desired time to live and
    446 	 * send to IP level.  There should be a better way
    447 	 * to handle ttl and tos; we could keep them in
    448 	 * the template, but need a way to checksum without them.
    449 	 */
    450 	m->m_pkthdr.len = hdrlen + len;
    451 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
    452 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
    453 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
    454 #if BSD >= 43
    455 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
    456 	    so->so_options & SO_DONTROUTE);
    457 #else
    458 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
    459 	    so->so_options & SO_DONTROUTE);
    460 #endif
    461 	if (error) {
    462 out:
    463 		if (error == ENOBUFS) {
    464 			tcp_quench(tp->t_inpcb, 0);
    465 			return (0);
    466 		}
    467 		if ((error == EHOSTUNREACH || error == ENETDOWN)
    468 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
    469 			tp->t_softerror = error;
    470 			return (0);
    471 		}
    472 		return (error);
    473 	}
    474 	tcpstat.tcps_sndtotal++;
    475 
    476 	/*
    477 	 * Data sent (as far as we can tell).
    478 	 * If this advertises a larger window than any other segment,
    479 	 * then remember the size of the advertised window.
    480 	 * Any pending ACK has now been sent.
    481 	 */
    482 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
    483 		tp->rcv_adv = tp->rcv_nxt + win;
    484 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
    485 	if (sendalot)
    486 		goto again;
    487 	return (0);
    488 }
    489 
    490 void
    491 tcp_setpersist(tp)
    492 	register struct tcpcb *tp;
    493 {
    494 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
    495 
    496 	if (tp->t_timer[TCPT_REXMT])
    497 		panic("tcp_output REXMT");
    498 	/*
    499 	 * Start/restart persistance timer.
    500 	 */
    501 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
    502 	    t * tcp_backoff[tp->t_rxtshift],
    503 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
    504 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
    505 		tp->t_rxtshift++;
    506 }
    507