xref: /freebsd/sys/netinet/tcp_output.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3  *	The Regents of the University of California.  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  *	@(#)tcp_output.c	8.3 (Berkeley) 12/30/93
34  * $Id: tcp_output.c,v 1.7 1995/02/09 23:13:24 wollman 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 #ifdef TCPDEBUG
61 #include <netinet/tcp_debug.h>
62 #endif
63 
64 #ifdef notyet
65 extern struct mbuf *m_copypack();
66 #endif
67 
68 
69 /*
70  * Tcp output routine: figure out what should be sent and send it.
71  */
72 int
73 tcp_output(tp)
74 	register struct tcpcb *tp;
75 {
76 	register struct socket *so = tp->t_inpcb->inp_socket;
77 	register long len, win;
78 	int off, flags, error;
79 	register struct mbuf *m;
80 	register struct tcpiphdr *ti;
81 	u_char opt[TCP_MAXOLEN];
82 	unsigned optlen, hdrlen;
83 	int idle, sendalot;
84 	struct rmxp_tao *taop;
85 	struct rmxp_tao tao_noncached;
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 	flags = tcp_outflags[tp->t_state];
107 	/*
108 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
109 	 * state flags.
110 	 */
111 	if (tp->t_flags & TF_NEEDFIN)
112 		flags |= TH_FIN;
113 	if (tp->t_flags & TF_NEEDSYN)
114 		flags |= TH_SYN;
115 
116 	/*
117 	 * If in persist timeout with window of 0, send 1 byte.
118 	 * Otherwise, if window is small but nonzero
119 	 * and timer expired, we will send what we can
120 	 * and go to transmit state.
121 	 */
122 	if (tp->t_force) {
123 		if (win == 0) {
124 			/*
125 			 * If we still have some data to send, then
126 			 * clear the FIN bit.  Usually this would
127 			 * happen below when it realizes that we
128 			 * aren't sending all the data.  However,
129 			 * if we have exactly 1 byte of unset data,
130 			 * then it won't clear the FIN bit below,
131 			 * and if we are in persist state, we wind
132 			 * up sending the packet without recording
133 			 * that we sent the FIN bit.
134 			 *
135 			 * We can't just blindly clear the FIN bit,
136 			 * because if we don't have any more data
137 			 * to send then the probe will be the FIN
138 			 * itself.
139 			 */
140 			if (off < so->so_snd.sb_cc)
141 				flags &= ~TH_FIN;
142 			win = 1;
143 		} else {
144 			tp->t_timer[TCPT_PERSIST] = 0;
145 			tp->t_rxtshift = 0;
146 		}
147 	}
148 
149 	len = min(so->so_snd.sb_cc, win) - off;
150 
151 	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
152 		taop = &tao_noncached;
153 		bzero(taop, sizeof(*taop));
154 	}
155 
156 	/*
157 	 * Lop off SYN bit if it has already been sent.  However, if this
158 	 * is SYN-SENT state and if segment contains data and if we don't
159 	 * know that foreign host supports TAO, suppress sending segment.
160 	 */
161 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
162 		flags &= ~TH_SYN;
163 		off--, len++;
164 		if (len > 0 && tp->t_state == TCPS_SYN_SENT &&
165 		    taop->tao_ccsent == 0)
166 			return 0;
167 	}
168 
169 	if (len < 0) {
170 		/*
171 		 * If FIN has been sent but not acked,
172 		 * but we haven't been called to retransmit,
173 		 * len will be -1.  Otherwise, window shrank
174 		 * after we sent into it.  If window shrank to 0,
175 		 * cancel pending retransmit and pull snd_nxt
176 		 * back to (closed) window.  We will enter persist
177 		 * state below.  If the window didn't close completely,
178 		 * just wait for an ACK.
179 		 */
180 		len = 0;
181 		if (win == 0) {
182 			tp->t_timer[TCPT_REXMT] = 0;
183 			tp->snd_nxt = tp->snd_una;
184 		}
185 	}
186 	if (len > tp->t_maxseg) {
187 		len = tp->t_maxseg;
188 		sendalot = 1;
189 	}
190 	if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
191 		flags &= ~TH_FIN;
192 
193 	win = sbspace(&so->so_rcv);
194 
195 	/*
196 	 * Sender silly window avoidance.  If connection is idle
197 	 * and can send all data, a maximum segment,
198 	 * at least a maximum default-size segment do it,
199 	 * or are forced, do it; otherwise don't bother.
200 	 * If peer's buffer is tiny, then send
201 	 * when window is at least half open.
202 	 * If retransmitting (possibly after persist timer forced us
203 	 * to send into a small window), then must resend.
204 	 */
205 	if (len) {
206 		if (len == tp->t_maxseg)
207 			goto send;
208 		if ((idle || tp->t_flags & TF_NODELAY) &&
209 		    (tp->t_flags & TF_NOPUSH) == 0 &&
210 		    len + off >= so->so_snd.sb_cc)
211 			goto send;
212 		if (tp->t_force)
213 			goto send;
214 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
215 			goto send;
216 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
217 			goto send;
218 	}
219 
220 	/*
221 	 * Compare available window to amount of window
222 	 * known to peer (as advertised window less
223 	 * next expected input).  If the difference is at least two
224 	 * max size segments, or at least 50% of the maximum possible
225 	 * window, then want to send a window update to peer.
226 	 */
227 	if (win > 0) {
228 		/*
229 		 * "adv" is the amount we can increase the window,
230 		 * taking into account that we are limited by
231 		 * TCP_MAXWIN << tp->rcv_scale.
232 		 */
233 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
234 			(tp->rcv_adv - tp->rcv_nxt);
235 
236 		if (adv >= (long) (2 * tp->t_maxseg))
237 			goto send;
238 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
239 			goto send;
240 	}
241 
242 	/*
243 	 * Send if we owe peer an ACK.
244 	 */
245 	if (tp->t_flags & TF_ACKNOW)
246 		goto send;
247 	if ((flags & TH_RST) ||
248 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
249 		goto send;
250 	if (SEQ_GT(tp->snd_up, tp->snd_una))
251 		goto send;
252 	/*
253 	 * If our state indicates that FIN should be sent
254 	 * and we have not yet done so, or we're retransmitting the FIN,
255 	 * then we need to send.
256 	 */
257 	if (flags & TH_FIN &&
258 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
259 		goto send;
260 
261 	/*
262 	 * TCP window updates are not reliable, rather a polling protocol
263 	 * using ``persist'' packets is used to insure receipt of window
264 	 * updates.  The three ``states'' for the output side are:
265 	 *	idle			not doing retransmits or persists
266 	 *	persisting		to move a small or zero window
267 	 *	(re)transmitting	and thereby not persisting
268 	 *
269 	 * tp->t_timer[TCPT_PERSIST]
270 	 *	is set when we are in persist state.
271 	 * tp->t_force
272 	 *	is set when we are called to send a persist packet.
273 	 * tp->t_timer[TCPT_REXMT]
274 	 *	is set when we are retransmitting
275 	 * The output side is idle when both timers are zero.
276 	 *
277 	 * If send window is too small, there is data to transmit, and no
278 	 * retransmit or persist is pending, then go to persist state.
279 	 * If nothing happens soon, send when timer expires:
280 	 * if window is nonzero, transmit what we can,
281 	 * otherwise force out a byte.
282 	 */
283 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
284 	    tp->t_timer[TCPT_PERSIST] == 0) {
285 		tp->t_rxtshift = 0;
286 		tcp_setpersist(tp);
287 	}
288 
289 	/*
290 	 * No reason to send a segment, just return.
291 	 */
292 	return (0);
293 
294 send:
295 	/*
296 	 * Before ESTABLISHED, force sending of initial options
297 	 * unless TCP set not to do any options.
298 	 * NOTE: we assume that the IP/TCP header plus TCP options
299 	 * always fit in a single mbuf, leaving room for a maximum
300 	 * link header, i.e.
301 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
302 	 */
303 	optlen = 0;
304 	hdrlen = sizeof (struct tcpiphdr);
305 	if (flags & TH_SYN) {
306 		tp->snd_nxt = tp->iss;
307 		if ((tp->t_flags & TF_NOOPT) == 0) {
308 			u_short mss;
309 
310 			opt[0] = TCPOPT_MAXSEG;
311 			opt[1] = TCPOLEN_MAXSEG;
312 			mss = htons((u_short) tcp_mssopt(tp));
313 			bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss));
314 			optlen = TCPOLEN_MAXSEG;
315 
316 			if ((tp->t_flags & TF_REQ_SCALE) &&
317 			    ((flags & TH_ACK) == 0 ||
318 			    (tp->t_flags & TF_RCVD_SCALE))) {
319 				*((u_long *) (opt + optlen)) = htonl(
320 					TCPOPT_NOP << 24 |
321 					TCPOPT_WINDOW << 16 |
322 					TCPOLEN_WINDOW << 8 |
323 					tp->request_r_scale);
324 				optlen += 4;
325 			}
326 		}
327  	}
328 
329  	/*
330 	 * Send a timestamp and echo-reply if this is a SYN and our side
331 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
332 	 * and our peer have sent timestamps in our SYN's.
333  	 */
334  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
335  	    (flags & TH_RST) == 0 &&
336 	    ((flags & TH_ACK) == 0 ||
337 	     (tp->t_flags & TF_RCVD_TSTMP))) {
338 		u_long *lp = (u_long *)(opt + optlen);
339 
340  		/* Form timestamp option as shown in appendix A of RFC 1323. */
341  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
342  		*lp++ = htonl(tcp_now);
343  		*lp   = htonl(tp->ts_recent);
344  		optlen += TCPOLEN_TSTAMP_APPA;
345  	}
346 
347  	/*
348 	 * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
349 	 * options are allowed (!TF_NOOPT) and it's not a RST.
350  	 */
351  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
352  	     (flags & TH_RST) == 0) {
353 		switch (flags & (TH_SYN|TH_ACK)) {
354 		/*
355 		 * This is a normal ACK, send CC if we received CC before
356 		 * from our peer.
357 		 */
358 		case TH_ACK:
359 			if (!(tp->t_flags & TF_RCVD_CC))
360 				break;
361 			/*FALLTHROUGH*/
362 
363 		/*
364 		 * We can only get here in T/TCP's SYN_SENT* state, when
365 		 * we're a sending a non-SYN segment without waiting for
366 		 * the ACK of our SYN.  A check above assures that we only
367 		 * do this if our peer understands T/TCP.
368 		 */
369 		case 0:
370 			opt[optlen++] = TCPOPT_NOP;
371 			opt[optlen++] = TCPOPT_NOP;
372 			opt[optlen++] = TCPOPT_CC;
373 			opt[optlen++] = TCPOLEN_CC;
374 			*(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
375 
376 			optlen += 4;
377 			break;
378 
379 		/*
380 		 * This is our initial SYN, check whether we have to use
381 		 * CC or CC.new.
382 		 */
383 		case TH_SYN:
384 			opt[optlen++] = TCPOPT_NOP;
385 			opt[optlen++] = TCPOPT_NOP;
386 
387 			if (taop->tao_ccsent != 0 &&
388 			    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
389 				opt[optlen++] = TCPOPT_CC;
390 				taop->tao_ccsent = tp->cc_send;
391 			} else {
392 				opt[optlen++] = TCPOPT_CCNEW;
393 				taop->tao_ccsent = 0;
394 			}
395 			opt[optlen++] = TCPOLEN_CC;
396 			*(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
397  			optlen += 4;
398 			break;
399 
400 		/*
401 		 * This is a SYN,ACK; send CC and CC.echo if we received
402 		 * CC from our peer.
403 		 */
404 		case (TH_SYN|TH_ACK):
405 			if (tp->t_flags & TF_RCVD_CC) {
406 				opt[optlen++] = TCPOPT_NOP;
407 				opt[optlen++] = TCPOPT_NOP;
408 				opt[optlen++] = TCPOPT_CC;
409 				opt[optlen++] = TCPOLEN_CC;
410 				*(u_int32_t *)&opt[optlen] =
411 					htonl(tp->cc_send);
412 				optlen += 4;
413 				opt[optlen++] = TCPOPT_NOP;
414 				opt[optlen++] = TCPOPT_NOP;
415 				opt[optlen++] = TCPOPT_CCECHO;
416 				opt[optlen++] = TCPOLEN_CC;
417 				*(u_int32_t *)&opt[optlen] =
418 					htonl(tp->cc_recv);
419 				optlen += 4;
420 			}
421 			break;
422 		}
423  	}
424 
425  	hdrlen += optlen;
426 
427 	/*
428 	 * Adjust data length if insertion of options will
429 	 * bump the packet length beyond the t_maxopd length.
430 	 * Clear the FIN bit because we cut off the tail of
431 	 * the segment.
432 	 */
433 	 if (len + optlen > tp->t_maxopd) {
434 		/*
435 		 * If there is still more to send, don't close the connection.
436 		 */
437 		flags &= ~TH_FIN;
438 
439 		len = tp->t_maxopd - optlen;
440 		sendalot = 1;
441 	}
442 
443 /*#ifdef DIAGNOSTIC*/
444  	if (max_linkhdr + hdrlen > MHLEN)
445 		panic("tcphdr too big");
446 /*#endif*/
447 
448 	/*
449 	 * Grab a header mbuf, attaching a copy of data to
450 	 * be transmitted, and initialize the header from
451 	 * the template for sends on this connection.
452 	 */
453 	if (len) {
454 		if (tp->t_force && len == 1)
455 			tcpstat.tcps_sndprobe++;
456 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
457 			tcpstat.tcps_sndrexmitpack++;
458 			tcpstat.tcps_sndrexmitbyte += len;
459 		} else {
460 			tcpstat.tcps_sndpack++;
461 			tcpstat.tcps_sndbyte += len;
462 		}
463 #ifdef notyet
464 		if ((m = m_copypack(so->so_snd.sb_mb, off,
465 		    (int)len, max_linkhdr + hdrlen)) == 0) {
466 			error = ENOBUFS;
467 			goto out;
468 		}
469 		/*
470 		 * m_copypack left space for our hdr; use it.
471 		 */
472 		m->m_len += hdrlen;
473 		m->m_data -= hdrlen;
474 #else
475 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
476 		if (m == NULL) {
477 			error = ENOBUFS;
478 			goto out;
479 		}
480 		m->m_data += max_linkhdr;
481 		m->m_len = hdrlen;
482 		if (len <= MHLEN - hdrlen - max_linkhdr) {
483 			m_copydata(so->so_snd.sb_mb, off, (int) len,
484 			    mtod(m, caddr_t) + hdrlen);
485 			m->m_len += len;
486 		} else {
487 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
488 			if (m->m_next == 0)
489 				len = 0;
490 		}
491 #endif
492 		/*
493 		 * If we're sending everything we've got, set PUSH.
494 		 * (This will keep happy those implementations which only
495 		 * give data to the user when a buffer fills or
496 		 * a PUSH comes in.)
497 		 */
498 		if (off + len == so->so_snd.sb_cc)
499 			flags |= TH_PUSH;
500 	} else {
501 		if (tp->t_flags & TF_ACKNOW)
502 			tcpstat.tcps_sndacks++;
503 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
504 			tcpstat.tcps_sndctrl++;
505 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
506 			tcpstat.tcps_sndurg++;
507 		else
508 			tcpstat.tcps_sndwinup++;
509 
510 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
511 		if (m == NULL) {
512 			error = ENOBUFS;
513 			goto out;
514 		}
515 		m->m_data += max_linkhdr;
516 		m->m_len = hdrlen;
517 	}
518 	m->m_pkthdr.rcvif = (struct ifnet *)0;
519 	ti = mtod(m, struct tcpiphdr *);
520 	if (tp->t_template == 0)
521 		panic("tcp_output");
522 	bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
523 
524 	/*
525 	 * Fill in fields, remembering maximum advertised
526 	 * window for use in delaying messages about window sizes.
527 	 * If resending a FIN, be sure not to use a new sequence number.
528 	 */
529 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
530 	    tp->snd_nxt == tp->snd_max)
531 		tp->snd_nxt--;
532 	/*
533 	 * If we are doing retransmissions, then snd_nxt will
534 	 * not reflect the first unsent octet.  For ACK only
535 	 * packets, we do not want the sequence number of the
536 	 * retransmitted packet, we want the sequence number
537 	 * of the next unsent octet.  So, if there is no data
538 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
539 	 * when filling in ti_seq.  But if we are in persist
540 	 * state, snd_max might reflect one byte beyond the
541 	 * right edge of the window, so use snd_nxt in that
542 	 * case, since we know we aren't doing a retransmission.
543 	 * (retransmit and persist are mutually exclusive...)
544 	 */
545 	if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
546 		ti->ti_seq = htonl(tp->snd_nxt);
547 	else
548 		ti->ti_seq = htonl(tp->snd_max);
549 	ti->ti_ack = htonl(tp->rcv_nxt);
550 	if (optlen) {
551 		bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
552 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
553 	}
554 	ti->ti_flags = flags;
555 	/*
556 	 * Calculate receive window.  Don't shrink window,
557 	 * but avoid silly window syndrome.
558 	 */
559 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
560 		win = 0;
561 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
562 		win = (long)TCP_MAXWIN << tp->rcv_scale;
563 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
564 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
565 	ti->ti_win = htons((u_short) (win>>tp->rcv_scale));
566 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
567 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
568 		ti->ti_flags |= TH_URG;
569 	} else
570 		/*
571 		 * If no urgent pointer to send, then we pull
572 		 * the urgent pointer to the left edge of the send window
573 		 * so that it doesn't drift into the send window on sequence
574 		 * number wraparound.
575 		 */
576 		tp->snd_up = tp->snd_una;		/* drag it along */
577 
578 	/*
579 	 * Put TCP length in extended header, and then
580 	 * checksum extended header and data.
581 	 */
582 	if (len + optlen)
583 		ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
584 		    optlen + len));
585 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
586 
587 	/*
588 	 * In transmit state, time the transmission and arrange for
589 	 * the retransmit.  In persist state, just set snd_max.
590 	 */
591 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
592 		tcp_seq startseq = tp->snd_nxt;
593 
594 		/*
595 		 * Advance snd_nxt over sequence space of this segment.
596 		 */
597 		if (flags & (TH_SYN|TH_FIN)) {
598 			if (flags & TH_SYN)
599 				tp->snd_nxt++;
600 			if (flags & TH_FIN) {
601 				tp->snd_nxt++;
602 				tp->t_flags |= TF_SENTFIN;
603 			}
604 		}
605 		tp->snd_nxt += len;
606 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
607 			tp->snd_max = tp->snd_nxt;
608 			/*
609 			 * Time this transmission if not a retransmission and
610 			 * not currently timing anything.
611 			 */
612 			if (tp->t_rtt == 0) {
613 				tp->t_rtt = 1;
614 				tp->t_rtseq = startseq;
615 				tcpstat.tcps_segstimed++;
616 			}
617 		}
618 
619 		/*
620 		 * Set retransmit timer if not currently set,
621 		 * and not doing an ack or a keep-alive probe.
622 		 * Initial value for retransmit timer is smoothed
623 		 * round-trip time + 2 * round-trip time variance.
624 		 * Initialize shift counter which is used for backoff
625 		 * of retransmit time.
626 		 */
627 		if (tp->t_timer[TCPT_REXMT] == 0 &&
628 		    tp->snd_nxt != tp->snd_una) {
629 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
630 			if (tp->t_timer[TCPT_PERSIST]) {
631 				tp->t_timer[TCPT_PERSIST] = 0;
632 				tp->t_rxtshift = 0;
633 			}
634 		}
635 	} else
636 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
637 			tp->snd_max = tp->snd_nxt + len;
638 
639 #ifdef TCPDEBUG
640 	/*
641 	 * Trace.
642 	 */
643 	if (so->so_options & SO_DEBUG)
644 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
645 #endif
646 
647 	/*
648 	 * Fill in IP length and desired time to live and
649 	 * send to IP level.  There should be a better way
650 	 * to handle ttl and tos; we could keep them in
651 	 * the template, but need a way to checksum without them.
652 	 */
653 	m->m_pkthdr.len = hdrlen + len;
654 #ifdef TUBA
655 	if (tp->t_tuba_pcb)
656 		error = tuba_output(m, tp);
657 	else
658 #endif
659     {
660 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
661 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
662 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
663 #if BSD >= 43
664 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
665 	    so->so_options & SO_DONTROUTE, 0);
666 #else
667 	error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
668 	    so->so_options & SO_DONTROUTE);
669 #endif
670     }
671 	if (error) {
672 out:
673 		if (error == ENOBUFS) {
674 			tcp_quench(tp->t_inpcb, 0);
675 			return (0);
676 		}
677 		if ((error == EHOSTUNREACH || error == ENETDOWN)
678 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
679 			tp->t_softerror = error;
680 			return (0);
681 		}
682 		return (error);
683 	}
684 	tcpstat.tcps_sndtotal++;
685 
686 	/*
687 	 * Data sent (as far as we can tell).
688 	 * If this advertises a larger window than any other segment,
689 	 * then remember the size of the advertised window.
690 	 * Any pending ACK has now been sent.
691 	 */
692 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
693 		tp->rcv_adv = tp->rcv_nxt + win;
694 	tp->last_ack_sent = tp->rcv_nxt;
695 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
696 	if (sendalot)
697 		goto again;
698 	return (0);
699 }
700 
701 void
702 tcp_setpersist(tp)
703 	register struct tcpcb *tp;
704 {
705 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
706 
707 	if (tp->t_timer[TCPT_REXMT])
708 		panic("tcp_output REXMT");
709 	/*
710 	 * Start/restart persistance timer.
711 	 */
712 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
713 	    t * tcp_backoff[tp->t_rxtshift],
714 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
715 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
716 		tp->t_rxtshift++;
717 }
718