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