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