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