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