xref: /freebsd/sys/netinet/tcp_output.c (revision 0ea3482342b4d7d6e71f3007ce4dafe445c639fd)
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.15 1995/10/16 18:21:12 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 			opt[optlen++] = tp->t_flags & TF_SENDCCNEW ?
388 						TCPOPT_CCNEW : TCPOPT_CC;
389 			opt[optlen++] = TCPOLEN_CC;
390 			*(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
391  			optlen += 4;
392 			break;
393 
394 		/*
395 		 * This is a SYN,ACK; send CC and CC.echo if we received
396 		 * CC from our peer.
397 		 */
398 		case (TH_SYN|TH_ACK):
399 			if (tp->t_flags & TF_RCVD_CC) {
400 				opt[optlen++] = TCPOPT_NOP;
401 				opt[optlen++] = TCPOPT_NOP;
402 				opt[optlen++] = TCPOPT_CC;
403 				opt[optlen++] = TCPOLEN_CC;
404 				*(u_int32_t *)&opt[optlen] =
405 					htonl(tp->cc_send);
406 				optlen += 4;
407 				opt[optlen++] = TCPOPT_NOP;
408 				opt[optlen++] = TCPOPT_NOP;
409 				opt[optlen++] = TCPOPT_CCECHO;
410 				opt[optlen++] = TCPOLEN_CC;
411 				*(u_int32_t *)&opt[optlen] =
412 					htonl(tp->cc_recv);
413 				optlen += 4;
414 			}
415 			break;
416 		}
417  	}
418 
419  	hdrlen += optlen;
420 
421 	/*
422 	 * Adjust data length if insertion of options will
423 	 * bump the packet length beyond the t_maxopd length.
424 	 * Clear the FIN bit because we cut off the tail of
425 	 * the segment.
426 	 */
427 	 if (len + optlen > tp->t_maxopd) {
428 		/*
429 		 * If there is still more to send, don't close the connection.
430 		 */
431 		flags &= ~TH_FIN;
432 		len = tp->t_maxopd - optlen;
433 		sendalot = 1;
434 	}
435 
436 /*#ifdef DIAGNOSTIC*/
437  	if (max_linkhdr + hdrlen > MHLEN)
438 		panic("tcphdr too big");
439 /*#endif*/
440 
441 	/*
442 	 * Grab a header mbuf, attaching a copy of data to
443 	 * be transmitted, and initialize the header from
444 	 * the template for sends on this connection.
445 	 */
446 	if (len) {
447 		if (tp->t_force && len == 1)
448 			tcpstat.tcps_sndprobe++;
449 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
450 			tcpstat.tcps_sndrexmitpack++;
451 			tcpstat.tcps_sndrexmitbyte += len;
452 		} else {
453 			tcpstat.tcps_sndpack++;
454 			tcpstat.tcps_sndbyte += len;
455 		}
456 #ifdef notyet
457 		if ((m = m_copypack(so->so_snd.sb_mb, off,
458 		    (int)len, max_linkhdr + hdrlen)) == 0) {
459 			error = ENOBUFS;
460 			goto out;
461 		}
462 		/*
463 		 * m_copypack left space for our hdr; use it.
464 		 */
465 		m->m_len += hdrlen;
466 		m->m_data -= hdrlen;
467 #else
468 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
469 		if (m == NULL) {
470 			error = ENOBUFS;
471 			goto out;
472 		}
473 		m->m_data += max_linkhdr;
474 		m->m_len = hdrlen;
475 		if (len <= MHLEN - hdrlen - max_linkhdr) {
476 			m_copydata(so->so_snd.sb_mb, off, (int) len,
477 			    mtod(m, caddr_t) + hdrlen);
478 			m->m_len += len;
479 		} else {
480 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
481 			if (m->m_next == 0) {
482 				(void) m_free(m);
483 				error = ENOBUFS;
484 				goto out;
485 			}
486 		}
487 #endif
488 		/*
489 		 * If we're sending everything we've got, set PUSH.
490 		 * (This will keep happy those implementations which only
491 		 * give data to the user when a buffer fills or
492 		 * a PUSH comes in.)
493 		 */
494 		if (off + len == so->so_snd.sb_cc)
495 			flags |= TH_PUSH;
496 	} else {
497 		if (tp->t_flags & TF_ACKNOW)
498 			tcpstat.tcps_sndacks++;
499 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
500 			tcpstat.tcps_sndctrl++;
501 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
502 			tcpstat.tcps_sndurg++;
503 		else
504 			tcpstat.tcps_sndwinup++;
505 
506 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
507 		if (m == NULL) {
508 			error = ENOBUFS;
509 			goto out;
510 		}
511 		m->m_data += max_linkhdr;
512 		m->m_len = hdrlen;
513 	}
514 	m->m_pkthdr.rcvif = (struct ifnet *)0;
515 	ti = mtod(m, struct tcpiphdr *);
516 	if (tp->t_template == 0)
517 		panic("tcp_output");
518 	(void)memcpy(ti, tp->t_template, sizeof (struct tcpiphdr));
519 
520 	/*
521 	 * Fill in fields, remembering maximum advertised
522 	 * window for use in delaying messages about window sizes.
523 	 * If resending a FIN, be sure not to use a new sequence number.
524 	 */
525 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
526 	    tp->snd_nxt == tp->snd_max)
527 		tp->snd_nxt--;
528 	/*
529 	 * If we are doing retransmissions, then snd_nxt will
530 	 * not reflect the first unsent octet.  For ACK only
531 	 * packets, we do not want the sequence number of the
532 	 * retransmitted packet, we want the sequence number
533 	 * of the next unsent octet.  So, if there is no data
534 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
535 	 * when filling in ti_seq.  But if we are in persist
536 	 * state, snd_max might reflect one byte beyond the
537 	 * right edge of the window, so use snd_nxt in that
538 	 * case, since we know we aren't doing a retransmission.
539 	 * (retransmit and persist are mutually exclusive...)
540 	 */
541 	if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
542 		ti->ti_seq = htonl(tp->snd_nxt);
543 	else
544 		ti->ti_seq = htonl(tp->snd_max);
545 	ti->ti_ack = htonl(tp->rcv_nxt);
546 	if (optlen) {
547 		(void)memcpy(ti + 1, opt, optlen);
548 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
549 	}
550 	ti->ti_flags = flags;
551 	/*
552 	 * Calculate receive window.  Don't shrink window,
553 	 * but avoid silly window syndrome.
554 	 */
555 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
556 		win = 0;
557 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
558 		win = (long)TCP_MAXWIN << tp->rcv_scale;
559 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
560 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
561 	ti->ti_win = htons((u_short) (win>>tp->rcv_scale));
562 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
563 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
564 		ti->ti_flags |= TH_URG;
565 	} else
566 		/*
567 		 * If no urgent pointer to send, then we pull
568 		 * the urgent pointer to the left edge of the send window
569 		 * so that it doesn't drift into the send window on sequence
570 		 * number wraparound.
571 		 */
572 		tp->snd_up = tp->snd_una;		/* drag it along */
573 
574 	/*
575 	 * Put TCP length in extended header, and then
576 	 * checksum extended header and data.
577 	 */
578 	if (len + optlen)
579 		ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
580 		    optlen + len));
581 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
582 
583 	/*
584 	 * In transmit state, time the transmission and arrange for
585 	 * the retransmit.  In persist state, just set snd_max.
586 	 */
587 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
588 		tcp_seq startseq = tp->snd_nxt;
589 
590 		/*
591 		 * Advance snd_nxt over sequence space of this segment.
592 		 */
593 		if (flags & (TH_SYN|TH_FIN)) {
594 			if (flags & TH_SYN)
595 				tp->snd_nxt++;
596 			if (flags & TH_FIN) {
597 				tp->snd_nxt++;
598 				tp->t_flags |= TF_SENTFIN;
599 			}
600 		}
601 		tp->snd_nxt += len;
602 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
603 			tp->snd_max = tp->snd_nxt;
604 			/*
605 			 * Time this transmission if not a retransmission and
606 			 * not currently timing anything.
607 			 */
608 			if (tp->t_rtt == 0) {
609 				tp->t_rtt = 1;
610 				tp->t_rtseq = startseq;
611 				tcpstat.tcps_segstimed++;
612 			}
613 		}
614 
615 		/*
616 		 * Set retransmit timer if not currently set,
617 		 * and not doing an ack or a keep-alive probe.
618 		 * Initial value for retransmit timer is smoothed
619 		 * round-trip time + 2 * round-trip time variance.
620 		 * Initialize shift counter which is used for backoff
621 		 * of retransmit time.
622 		 */
623 		if (tp->t_timer[TCPT_REXMT] == 0 &&
624 		    tp->snd_nxt != tp->snd_una) {
625 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
626 			if (tp->t_timer[TCPT_PERSIST]) {
627 				tp->t_timer[TCPT_PERSIST] = 0;
628 				tp->t_rxtshift = 0;
629 			}
630 		}
631 	} else
632 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
633 			tp->snd_max = tp->snd_nxt + len;
634 
635 #ifdef TCPDEBUG
636 	/*
637 	 * Trace.
638 	 */
639 	if (so->so_options & SO_DEBUG)
640 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
641 #endif
642 
643 	/*
644 	 * Fill in IP length and desired time to live and
645 	 * send to IP level.  There should be a better way
646 	 * to handle ttl and tos; we could keep them in
647 	 * the template, but need a way to checksum without them.
648 	 */
649 	m->m_pkthdr.len = hdrlen + len;
650 #ifdef TUBA
651 	if (tp->t_tuba_pcb)
652 		error = tuba_output(m, tp);
653 	else
654 #endif
655     {
656 #ifdef MTUDISC
657 	struct rtentry *rt;
658 #endif
659 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
660 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
661 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
662 #ifdef MTUDISC
663 	/*
664 	 * See if we should do MTU discovery.  We do it only if the following
665 	 * are true:
666 	 *	1) we have a valid route to the destination
667 	 *	2) the MTU is not locked (if it is, then discovery has been
668 	 *	   disabled)
669 	 */
670 	if ((rt = tp->t_inpcb->inp_route.ro_rt)
671 	    && rt->rt_flags & RTF_UP
672 	    && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
673 		((struct ip *)ti)->ip_off |= IP_DF;
674 	}
675 #endif /* MTUDISC */
676 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
677 	    so->so_options & SO_DONTROUTE, 0);
678     }
679 	if (error) {
680 out:
681 		if (error == ENOBUFS) {
682 			tcp_quench(tp->t_inpcb, 0);
683 			return (0);
684 		}
685 #ifdef MTUDISC
686 		if (error == EMSGSIZE) {
687 			/*
688 			 * ip_output() will have already fixed the route
689 			 * for us.  tcp_mtudisc() will, as its last action,
690 			 * initiate retransmission, so it is important to
691 			 * not do so here.
692 			 */
693 			tcp_mtudisc(tp->t_inpcb, 0);
694 			return 0;
695 		}
696 #endif /* MTUDISC */.
697 		if ((error == EHOSTUNREACH || error == ENETDOWN)
698 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
699 			tp->t_softerror = error;
700 			return (0);
701 		}
702 		return (error);
703 	}
704 	tcpstat.tcps_sndtotal++;
705 
706 	/*
707 	 * Data sent (as far as we can tell).
708 	 * If this advertises a larger window than any other segment,
709 	 * then remember the size of the advertised window.
710 	 * Any pending ACK has now been sent.
711 	 */
712 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
713 		tp->rcv_adv = tp->rcv_nxt + win;
714 	tp->last_ack_sent = tp->rcv_nxt;
715 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
716 	if (sendalot)
717 		goto again;
718 	return (0);
719 }
720 
721 void
722 tcp_setpersist(tp)
723 	register struct tcpcb *tp;
724 {
725 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
726 
727 	if (tp->t_timer[TCPT_REXMT])
728 		panic("tcp_output REXMT");
729 	/*
730 	 * Start/restart persistance timer.
731 	 */
732 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
733 	    t * tcp_backoff[tp->t_rxtshift],
734 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
735 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
736 		tp->t_rxtshift++;
737 }
738