xref: /openbsd-src/sys/netinet/tcp_output.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: tcp_output.c,v 1.96 2011/05/13 14:31:17 oga Exp $	*/
2 /*	$NetBSD: tcp_output.c,v 1.16 1997/06/03 16:17:09 kml Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1988, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
33  *
34  * NRL grants permission for redistribution and use in source and binary
35  * forms, with or without modification, of the software and documentation
36  * created at NRL provided that the following conditions are met:
37  *
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgements:
45  * 	This product includes software developed by the University of
46  * 	California, Berkeley and its contributors.
47  * 	This product includes software developed at the Information
48  * 	Technology Division, US Naval Research Laboratory.
49  * 4. Neither the name of the NRL nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
54  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
56  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
57  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  *
65  * The views and conclusions contained in the software and documentation
66  * are those of the authors and should not be interpreted as representing
67  * official policies, either expressed or implied, of the US Naval
68  * Research Laboratory (NRL).
69  */
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/mbuf.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/kernel.h>
78 
79 #include <net/route.h>
80 #include <net/if.h>
81 
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/in_pcb.h>
86 #include <netinet/ip_var.h>
87 #include <netinet/tcp.h>
88 #define	TCPOUTFLAGS
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcpip.h>
94 #include <netinet/tcp_debug.h>
95 
96 #ifdef INET6
97 #include <netinet6/tcpipv6.h>
98 #include <netinet6/in6_var.h>
99 #endif /* INET6 */
100 
101 #ifdef notyet
102 extern struct mbuf *m_copypack();
103 #endif
104 
105 #ifdef TCP_SACK
106 extern int tcprexmtthresh;
107 #endif
108 
109 #ifdef TCP_SACK
110 #ifdef TCP_SACK_DEBUG
111 void tcp_print_holes(struct tcpcb *tp);
112 
113 void
114 tcp_print_holes(struct tcpcb *tp)
115 {
116 	struct sackhole *p = tp->snd_holes;
117 	if (p == 0)
118 		return;
119 	printf("Hole report: start--end dups rxmit\n");
120 	while (p) {
121 		printf("%x--%x d %d r %x\n", p->start, p->end, p->dups,
122 		    p->rxmit);
123 		p = p->next;
124 	}
125 	printf("\n");
126 }
127 #endif /* TCP_SACK_DEBUG */
128 
129 /*
130  * Returns pointer to a sackhole if there are any pending retransmissions;
131  * NULL otherwise.
132  */
133 struct sackhole *
134 tcp_sack_output(struct tcpcb *tp)
135 {
136 	struct sackhole *p;
137 
138 	if (!tp->sack_enable)
139 		return (NULL);
140 	p = tp->snd_holes;
141 	while (p) {
142 #ifndef TCP_FACK
143 		if (p->dups >= tcprexmtthresh && SEQ_LT(p->rxmit, p->end)) {
144 #else
145 		/* In FACK, if p->dups is less than tcprexmtthresh, but
146 		 * snd_fack advances more than tcprextmtthresh * tp->t_maxseg,
147 		 * tcp_input() will try fast retransmit. This forces output.
148 		 */
149 		if ((p->dups >= tcprexmtthresh ||
150 		     tp->t_dupacks == tcprexmtthresh) &&
151 		    SEQ_LT(p->rxmit, p->end)) {
152 #endif /* TCP_FACK */
153 			if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
154 				p = p->next;
155 				continue;
156 			}
157 #ifdef TCP_SACK_DEBUG
158 			if (p)
159 				tcp_print_holes(tp);
160 #endif
161 			return (p);
162 		}
163         	p = p->next;
164 	}
165 	return (NULL);
166 }
167 
168 /*
169  * After a timeout, the SACK list may be rebuilt.  This SACK information
170  * should be used to avoid retransmitting SACKed data.  This function
171  * traverses the SACK list to see if snd_nxt should be moved forward.
172  */
173 
174 void
175 tcp_sack_adjust(struct tcpcb *tp)
176 {
177 	struct sackhole *cur = tp->snd_holes;
178 	if (cur == NULL)
179 		return; /* No holes */
180 	if (SEQ_GEQ(tp->snd_nxt, tp->rcv_lastsack))
181 		return; /* We're already beyond any SACKed blocks */
182 	/*
183 	 * Two cases for which we want to advance snd_nxt:
184 	 * i) snd_nxt lies between end of one hole and beginning of another
185 	 * ii) snd_nxt lies between end of last hole and rcv_lastsack
186 	 */
187 	while (cur->next) {
188 		if (SEQ_LT(tp->snd_nxt, cur->end))
189 			return;
190 		if (SEQ_GEQ(tp->snd_nxt, cur->next->start))
191 			cur = cur->next;
192 		else {
193 			tp->snd_nxt = cur->next->start;
194 			return;
195 		}
196 	}
197 	if (SEQ_LT(tp->snd_nxt, cur->end))
198 		return;
199 	tp->snd_nxt = tp->rcv_lastsack;
200 	return;
201 }
202 #endif /* TCP_SACK */
203 
204 /*
205  * Tcp output routine: figure out what should be sent and send it.
206  */
207 int
208 tcp_output(struct tcpcb *tp)
209 {
210 	struct socket *so = tp->t_inpcb->inp_socket;
211 	long len, win, txmaxseg;
212 	int off, flags, error;
213 	struct mbuf *m;
214 	struct tcphdr *th;
215 	u_int32_t optbuf[howmany(MAX_TCPOPTLEN, sizeof(u_int32_t))];
216 	u_char *opt = (u_char *)optbuf;
217 	unsigned int optlen, hdrlen, packetlen;
218 	int idle, sendalot = 0;
219 #ifdef TCP_SACK
220 	int i, sack_rxmit = 0;
221 	struct sackhole *p;
222 	int maxburst = TCP_MAXBURST;
223 #endif
224 #ifdef TCP_SIGNATURE
225 	unsigned int sigoff;
226 #endif /* TCP_SIGNATURE */
227 #ifdef TCP_ECN
228 	int needect;
229 #endif
230 
231 	if (tp->t_flags & TF_BLOCKOUTPUT) {
232 		tp->t_flags |= TF_NEEDOUTPUT;
233 		return (0);
234 	} else
235 		tp->t_flags &= ~TF_NEEDOUTPUT;
236 
237 #if defined(TCP_SACK) && defined(TCP_SIGNATURE) && defined(DIAGNOSTIC)
238 	if (tp->sack_enable && (tp->t_flags & TF_SIGNATURE))
239 		return (EINVAL);
240 #endif /* defined(TCP_SACK) && defined(TCP_SIGNATURE) && defined(DIAGNOSTIC) */
241 
242 	/*
243 	 * Determine length of data that should be transmitted,
244 	 * and flags that will be used.
245 	 * If there is some data or critical controls (SYN, RST)
246 	 * to send, then transmit; otherwise, investigate further.
247 	 */
248 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
249 	if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur)
250 		/*
251 		 * We have been idle for "a while" and no acks are
252 		 * expected to clock out any data we send --
253 		 * slow start to get ack "clock" running again.
254 		 */
255 		tp->snd_cwnd = 2 * tp->t_maxseg;
256 
257 	/* remember 'idle' for next invocation of tcp_output */
258 	if (idle && soissending(so)) {
259 		tp->t_flags |= TF_LASTIDLE;
260 		idle = 0;
261 	} else
262 		tp->t_flags &= ~TF_LASTIDLE;
263 
264 again:
265 #ifdef TCP_SACK
266 	/*
267 	 * If we've recently taken a timeout, snd_max will be greater than
268 	 * snd_nxt.  There may be SACK information that allows us to avoid
269 	 * resending already delivered data.  Adjust snd_nxt accordingly.
270 	 */
271 	if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max))
272 		tcp_sack_adjust(tp);
273 #endif
274 	off = tp->snd_nxt - tp->snd_una;
275 #if defined(TCP_SACK) && defined(TCP_FACK)
276 	/* Normally, sendable data is limited by off < tp->snd_cwnd.
277 	 * But in FACK, sendable data is limited by snd_awnd < snd_cwnd,
278 	 * regardless of offset.
279 	 */
280 	if (tp->sack_enable && (tp->t_dupacks > tcprexmtthresh))
281 		win = tp->snd_wnd;
282 	else
283 #endif
284 	win = ulmin(tp->snd_wnd, tp->snd_cwnd);
285 
286 	flags = tcp_outflags[tp->t_state];
287 
288 #ifdef TCP_SACK
289 	/*
290 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
291 	 * to send out new data (when sendalot is 1), bypass this function.
292 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
293 	 * we're replacing a (future) new transmission with a retransmission
294 	 * now, and we previously incremented snd_cwnd in tcp_input().
295 	 */
296 	if (tp->sack_enable && !sendalot) {
297 		if (tp->t_dupacks >= tcprexmtthresh &&
298 		    (p = tcp_sack_output(tp))) {
299 			off = p->rxmit - tp->snd_una;
300 			sack_rxmit = 1;
301 			/* Coalesce holes into a single retransmission */
302 			len = min(tp->t_maxseg, p->end - p->rxmit);
303 #ifndef TCP_FACK
304 			/* in FACK, hold snd_cwnd constant during recovery */
305 			if (SEQ_LT(tp->snd_una, tp->snd_last))
306 				tp->snd_cwnd -= tp->t_maxseg;
307 #endif
308     		}
309 	}
310 #endif /* TCP_SACK */
311 
312 	sendalot = 0;
313 	/*
314 	 * If in persist timeout with window of 0, send 1 byte.
315 	 * Otherwise, if window is small but nonzero
316 	 * and timer expired, we will send what we can
317 	 * and go to transmit state.
318 	 */
319 	if (tp->t_force) {
320 		if (win == 0) {
321 			/*
322 			 * If we still have some data to send, then
323 			 * clear the FIN bit.  Usually this would
324 			 * happen below when it realizes that we
325 			 * aren't sending all the data.  However,
326 			 * if we have exactly 1 byte of unset data,
327 			 * then it won't clear the FIN bit below,
328 			 * and if we are in persist state, we wind
329 			 * up sending the packet without recording
330 			 * that we sent the FIN bit.
331 			 *
332 			 * We can't just blindly clear the FIN bit,
333 			 * because if we don't have any more data
334 			 * to send then the probe will be the FIN
335 			 * itself.
336 			 */
337 			if (off < so->so_snd.sb_cc)
338 				flags &= ~TH_FIN;
339 			win = 1;
340 		} else {
341 			TCP_TIMER_DISARM(tp, TCPT_PERSIST);
342 			tp->t_rxtshift = 0;
343 		}
344 	}
345 
346 #ifdef TCP_SACK
347 	if (!sack_rxmit) {
348 #endif
349 	len = ulmin(so->so_snd.sb_cc, win) - off;
350 
351 #if defined(TCP_SACK) && defined(TCP_FACK)
352 	/*
353 	 * If we're in fast recovery (SEQ_GT(tp->snd_last, tp->snd_una)), and
354 	 * amount of outstanding data (snd_awnd) is >= snd_cwnd, then
355 	 * do not send data (like zero window conditions)
356 	 */
357 	if (tp->sack_enable && len && SEQ_GT(tp->snd_last, tp->snd_una) &&
358 	    (tp->snd_awnd >= tp->snd_cwnd))
359 		len = 0;
360 #endif /* TCP_FACK */
361 #ifdef TCP_SACK
362 	}
363 #endif
364 
365 	if (len < 0) {
366 		/*
367 		 * If FIN has been sent but not acked,
368 		 * but we haven't been called to retransmit,
369 		 * len will be -1.  Otherwise, window shrank
370 		 * after we sent into it.  If window shrank to 0,
371 		 * cancel pending retransmit, pull snd_nxt back
372 		 * to (closed) window, and set the persist timer
373 		 * if it isn't already going.  If the window didn't
374 		 * close completely, just wait for an ACK.
375 		 */
376 		len = 0;
377 		if (win == 0) {
378 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
379 			tp->t_rxtshift = 0;
380 			tp->snd_nxt = tp->snd_una;
381 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
382 				tcp_setpersist(tp);
383 		}
384 	}
385 
386         /*
387          * Never send more than half a buffer full.  This insures that we can
388          * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and
389          * therefore acks will never be delayed unless we run out of data to
390          * transmit.
391          */
392 	txmaxseg = ulmin(so->so_snd.sb_hiwat / 2, tp->t_maxseg);
393 
394 	if (len > txmaxseg) {
395 		len = txmaxseg;
396 		sendalot = 1;
397 	}
398 	if (off + len < so->so_snd.sb_cc)
399 		flags &= ~TH_FIN;
400 
401 	win = sbspace(&so->so_rcv);
402 
403 	/*
404 	 * Sender silly window avoidance.  If connection is idle
405 	 * and can send all data, a maximum segment,
406 	 * at least a maximum default-size segment do it,
407 	 * or are forced, do it; otherwise don't bother.
408 	 * If peer's buffer is tiny, then send
409 	 * when window is at least half open.
410 	 * If retransmitting (possibly after persist timer forced us
411 	 * to send into a small window), then must resend.
412 	 */
413 	if (len) {
414 		if (len == txmaxseg)
415 			goto send;
416 		if ((idle || tp->t_flags & TF_NODELAY) &&
417 		    len + off >= so->so_snd.sb_cc && !soissending(so))
418 			goto send;
419 		if (tp->t_force)
420 			goto send;
421 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
422 			goto send;
423 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
424 			goto send;
425 #ifdef TCP_SACK
426 		if (sack_rxmit)
427 			goto send;
428 #endif
429 	}
430 
431 	/*
432 	 * Compare available window to amount of window
433 	 * known to peer (as advertised window less
434 	 * next expected input).  If the difference is at least two
435 	 * max size segments, or at least 50% of the maximum possible
436 	 * window, then want to send a window update to peer.
437 	 */
438 	if (win > 0) {
439 		/*
440 		 * "adv" is the amount we can increase the window,
441 		 * taking into account that we are limited by
442 		 * TCP_MAXWIN << tp->rcv_scale.
443 		 */
444 		long adv = lmin(win, (long)TCP_MAXWIN << tp->rcv_scale) -
445 			(tp->rcv_adv - tp->rcv_nxt);
446 
447 		if (adv >= (long) (2 * tp->t_maxseg))
448 			goto send;
449 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
450 			goto send;
451 	}
452 
453 	/*
454 	 * Send if we owe peer an ACK.
455 	 */
456 	if (tp->t_flags & TF_ACKNOW)
457 		goto send;
458 	if (flags & (TH_SYN|TH_RST))
459 		goto send;
460 	if (SEQ_GT(tp->snd_up, tp->snd_una))
461 		goto send;
462 	/*
463 	 * If our state indicates that FIN should be sent
464 	 * and we have not yet done so, or we're retransmitting the FIN,
465 	 * then we need to send.
466 	 */
467 	if (flags & TH_FIN &&
468 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
469 		goto send;
470 #ifdef TCP_SACK
471 	/*
472 	 * In SACK, it is possible for tcp_output to fail to send a segment
473 	 * after the retransmission timer has been turned off.  Make sure
474 	 * that the retransmission timer is set.
475 	 */
476 	if (SEQ_GT(tp->snd_max, tp->snd_una) &&
477 	    TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
478 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
479 		TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
480 		return (0);
481 	}
482 #endif /* TCP_SACK */
483 
484 	/*
485 	 * TCP window updates are not reliable, rather a polling protocol
486 	 * using ``persist'' packets is used to insure receipt of window
487 	 * updates.  The three ``states'' for the output side are:
488 	 *	idle			not doing retransmits or persists
489 	 *	persisting		to move a small or zero window
490 	 *	(re)transmitting	and thereby not persisting
491 	 *
492 	 * tp->t_timer[TCPT_PERSIST]
493 	 *	is set when we are in persist state.
494 	 * tp->t_force
495 	 *	is set when we are called to send a persist packet.
496 	 * tp->t_timer[TCPT_REXMT]
497 	 *	is set when we are retransmitting
498 	 * The output side is idle when both timers are zero.
499 	 *
500 	 * If send window is too small, there is data to transmit, and no
501 	 * retransmit or persist is pending, then go to persist state.
502 	 * If nothing happens soon, send when timer expires:
503 	 * if window is nonzero, transmit what we can,
504 	 * otherwise force out a byte.
505 	 */
506 	if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
507 	    TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
508 		tp->t_rxtshift = 0;
509 		tcp_setpersist(tp);
510 	}
511 
512 	/*
513 	 * No reason to send a segment, just return.
514 	 */
515 	return (0);
516 
517 send:
518 	/*
519 	 * Before ESTABLISHED, force sending of initial options
520 	 * unless TCP set not to do any options.
521 	 * NOTE: we assume that the IP/TCP header plus TCP options
522 	 * always fit in a single mbuf, leaving room for a maximum
523 	 * link header, i.e.
524 	 *	max_linkhdr + sizeof(network header) + sizeof(struct tcphdr +
525 	 * 		optlen <= MHLEN
526 	 */
527 	optlen = 0;
528 
529 	switch (tp->pf) {
530 	case 0:	/*default to PF_INET*/
531 #ifdef INET
532 	case PF_INET:
533 		hdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
534 		break;
535 #endif /* INET */
536 #ifdef INET6
537 	case PF_INET6:
538 		hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
539 		break;
540 #endif /* INET6 */
541 	default:
542 		return (EPFNOSUPPORT);
543 	}
544 
545 	if (flags & TH_SYN) {
546 		tp->snd_nxt = tp->iss;
547 		if ((tp->t_flags & TF_NOOPT) == 0) {
548 			u_int16_t mss;
549 
550 			opt[0] = TCPOPT_MAXSEG;
551 			opt[1] = 4;
552 			mss = htons((u_int16_t) tcp_mss(tp, 0));
553 			bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss));
554 			optlen = 4;
555 
556 			if (flags & TH_ACK)
557 				tcp_mss_update(tp);
558 #ifdef TCP_SACK
559 			/*
560 			 * If this is the first SYN of connection (not a SYN
561 			 * ACK), include SACK_PERMIT_HDR option.  If this is a
562 			 * SYN ACK, include SACK_PERMIT_HDR option if peer has
563 			 * already done so.
564 			 */
565 			if (tp->sack_enable && ((flags & TH_ACK) == 0 ||
566 			    (tp->t_flags & TF_SACK_PERMIT))) {
567 				*((u_int32_t *) (opt + optlen)) =
568 				    htonl(TCPOPT_SACK_PERMIT_HDR);
569 				optlen += 4;
570 			}
571 #endif
572 
573 			if ((tp->t_flags & TF_REQ_SCALE) &&
574 			    ((flags & TH_ACK) == 0 ||
575 			    (tp->t_flags & TF_RCVD_SCALE))) {
576 				*((u_int32_t *) (opt + optlen)) = htonl(
577 					TCPOPT_NOP << 24 |
578 					TCPOPT_WINDOW << 16 |
579 					TCPOLEN_WINDOW << 8 |
580 					tp->request_r_scale);
581 				optlen += 4;
582 			}
583 		}
584 	}
585 
586 	/*
587 	 * Send a timestamp and echo-reply if this is a SYN and our side
588 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
589 	 * and our peer have sent timestamps in our SYN's.
590 	 */
591 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
592 	     (flags & TH_RST) == 0 &&
593 	    ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
594 	     (tp->t_flags & TF_RCVD_TSTMP))) {
595 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
596 
597 		/* Form timestamp option as shown in appendix A of RFC 1323. */
598 		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
599 		*lp++ = htonl(tcp_now + tp->ts_modulate);
600 		*lp   = htonl(tp->ts_recent);
601 		optlen += TCPOLEN_TSTAMP_APPA;
602 
603 		/* Set receive buffer autosizing timestamp. */
604 		if (tp->rfbuf_ts == 0)
605 			tp->rfbuf_ts = tcp_now;
606 
607 	}
608 
609 #ifdef TCP_SIGNATURE
610 	if (tp->t_flags & TF_SIGNATURE) {
611 		u_int8_t *bp = (u_int8_t *)(opt + optlen);
612 
613 		/* Send signature option */
614 		*(bp++) = TCPOPT_SIGNATURE;
615 		*(bp++) = TCPOLEN_SIGNATURE;
616 		sigoff = optlen + 2;
617 
618 		{
619 			unsigned int i;
620 
621 			for (i = 0; i < 16; i++)
622 				*(bp++) = 0;
623 		}
624 
625 
626 		/* Pad options list to the next 32 bit boundary and
627 		 * terminate it.
628 		 */
629 		*bp++ = TCPOPT_NOP;
630 		*bp++ = TCPOPT_NOP;
631 
632 		optlen += TCPOLEN_SIGLEN;
633 	}
634 #endif /* TCP_SIGNATURE */
635 
636 #ifdef TCP_SACK
637 	/*
638 	 * Send SACKs if necessary.  This should be the last option processed.
639 	 * Only as many SACKs are sent as are permitted by the maximum options
640 	 * size.  No more than three SACKs are sent.
641 	 */
642 	if (tp->sack_enable && tp->t_state == TCPS_ESTABLISHED &&
643 	    (tp->t_flags & (TF_SACK_PERMIT|TF_NOOPT)) == TF_SACK_PERMIT &&
644 	    tp->rcv_numsacks) {
645 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
646 		u_int32_t *olp = lp++;
647 		int count = 0;  /* actual number of SACKs inserted */
648 		int maxsack = (MAX_TCPOPTLEN - (optlen + 4))/TCPOLEN_SACK;
649 
650 		tcpstat.tcps_sack_snd_opts++;
651 		maxsack = min(maxsack, TCP_MAX_SACK);
652 		for (i = 0; (i < tp->rcv_numsacks && count < maxsack); i++) {
653 			struct sackblk sack = tp->sackblks[i];
654 			if (sack.start == 0 && sack.end == 0)
655 				continue;
656 			*lp++ = htonl(sack.start);
657 			*lp++ = htonl(sack.end);
658 			count++;
659 		}
660 		*olp = htonl(TCPOPT_SACK_HDR|(TCPOLEN_SACK*count+2));
661 		optlen += TCPOLEN_SACK*count + 4; /* including leading NOPs */
662 	}
663 #endif /* TCP_SACK */
664 
665 #ifdef DIAGNOSTIC
666 	if (optlen > MAX_TCPOPTLEN)
667 		panic("tcp_output: options too long");
668 #endif /* DIAGNOSTIC */
669 
670 	hdrlen += optlen;
671 
672 	/*
673 	 * Adjust data length if insertion of options will
674 	 * bump the packet length beyond the t_maxopd length.
675 	 */
676 	if (len > tp->t_maxopd - optlen) {
677 		len = tp->t_maxopd - optlen;
678 		sendalot = 1;
679 		flags &= ~TH_FIN;
680 	 }
681 
682 #ifdef DIAGNOSTIC
683 	if (max_linkhdr + hdrlen > MCLBYTES)
684 		panic("tcphdr too big");
685 #endif
686 
687 	/*
688 	 * Grab a header mbuf, attaching a copy of data to
689 	 * be transmitted, and initialize the header from
690 	 * the template for sends on this connection.
691 	 */
692 	if (len) {
693 		if (tp->t_force && len == 1)
694 			tcpstat.tcps_sndprobe++;
695 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
696 			tcpstat.tcps_sndrexmitpack++;
697 			tcpstat.tcps_sndrexmitbyte += len;
698 		} else {
699 			tcpstat.tcps_sndpack++;
700 			tcpstat.tcps_sndbyte += len;
701 		}
702 #ifdef notyet
703 		if ((m = m_copypack(so->so_snd.sb_mb, off,
704 		    (int)len, max_linkhdr + hdrlen)) == 0) {
705 			error = ENOBUFS;
706 			goto out;
707 		}
708 		/*
709 		 * m_copypack left space for our hdr; use it.
710 		 */
711 		m->m_len += hdrlen;
712 		m->m_data -= hdrlen;
713 #else
714 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
715 		if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
716 			MCLGET(m, M_DONTWAIT);
717 			if ((m->m_flags & M_EXT) == 0) {
718 				m_freem(m);
719 				m = NULL;
720 			}
721 		}
722 		if (m == NULL) {
723 			error = ENOBUFS;
724 			goto out;
725 		}
726 		m->m_data += max_linkhdr;
727 		m->m_len = hdrlen;
728 		if (len <= M_TRAILINGSPACE(m)) {
729 			m_copydata(so->so_snd.sb_mb, off, (int) len,
730 			    mtod(m, caddr_t) + hdrlen);
731 			m->m_len += len;
732 		} else {
733 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
734 			if (m->m_next == 0) {
735 				(void) m_free(m);
736 				error = ENOBUFS;
737 				goto out;
738 			}
739 		}
740 #endif
741 		/*
742 		 * If we're sending everything we've got, set PUSH.
743 		 * (This will keep happy those implementations which only
744 		 * give data to the user when a buffer fills or
745 		 * a PUSH comes in.)
746 		 */
747 		if (off + len == so->so_snd.sb_cc && !soissending(so))
748 			flags |= TH_PUSH;
749 	} else {
750 		if (tp->t_flags & TF_ACKNOW)
751 			tcpstat.tcps_sndacks++;
752 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
753 			tcpstat.tcps_sndctrl++;
754 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
755 			tcpstat.tcps_sndurg++;
756 		else
757 			tcpstat.tcps_sndwinup++;
758 
759 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
760 		if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
761 			MCLGET(m, M_DONTWAIT);
762 			if ((m->m_flags & M_EXT) == 0) {
763 				m_freem(m);
764 				m = NULL;
765 			}
766 		}
767 		if (m == NULL) {
768 			error = ENOBUFS;
769 			goto out;
770 		}
771 		m->m_data += max_linkhdr;
772 		m->m_len = hdrlen;
773 	}
774 	m->m_pkthdr.rcvif = (struct ifnet *)0;
775 	m->m_pkthdr.len = hdrlen + len;
776 
777 	if (!tp->t_template)
778 		panic("tcp_output");
779 #ifdef DIAGNOSTIC
780 	if (tp->t_template->m_len != hdrlen - optlen)
781 		panic("tcp_output: template len != hdrlen - optlen");
782 #endif /* DIAGNOSTIC */
783 	bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t),
784 		tp->t_template->m_len);
785 	th = (struct tcphdr *)(mtod(m, caddr_t) + tp->t_template->m_len -
786 		sizeof(struct tcphdr));
787 
788 	/*
789 	 * Fill in fields, remembering maximum advertised
790 	 * window for use in delaying messages about window sizes.
791 	 * If resending a FIN, be sure not to use a new sequence number.
792 	 */
793 	if ((flags & TH_FIN) && (tp->t_flags & TF_SENTFIN) &&
794 	    (tp->snd_nxt == tp->snd_max))
795 		tp->snd_nxt--;
796 	/*
797 	 * If we are doing retransmissions, then snd_nxt will
798 	 * not reflect the first unsent octet.  For ACK only
799 	 * packets, we do not want the sequence number of the
800 	 * retransmitted packet, we want the sequence number
801 	 * of the next unsent octet.  So, if there is no data
802 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
803 	 * when filling in ti_seq.  But if we are in persist
804 	 * state, snd_max might reflect one byte beyond the
805 	 * right edge of the window, so use snd_nxt in that
806 	 * case, since we know we aren't doing a retransmission.
807 	 * (retransmit and persist are mutually exclusive...)
808 	 */
809 	if (len || (flags & (TH_SYN|TH_FIN)) || TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
810 		th->th_seq = htonl(tp->snd_nxt);
811 	else
812 		th->th_seq = htonl(tp->snd_max);
813 
814 #ifdef TCP_SACK
815 	if (sack_rxmit) {
816 		/*
817 		 * If sendalot was turned on (due to option stuffing), turn it
818 		 * off. Properly set th_seq field.  Advance the ret'x pointer
819 		 * by len.
820 		 */
821 		if (sendalot)
822 			sendalot = 0;
823 		th->th_seq = htonl(p->rxmit);
824 		p->rxmit += len;
825 #if defined(TCP_SACK) && defined(TCP_FACK)
826 		tp->retran_data += len;
827 #endif /* TCP_FACK */
828 		tcpstat.tcps_sack_rexmits++;
829 		tcpstat.tcps_sack_rexmit_bytes += len;
830 	}
831 #endif /* TCP_SACK */
832 
833 	th->th_ack = htonl(tp->rcv_nxt);
834 	if (optlen) {
835 		bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen);
836 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
837 	}
838 #ifdef TCP_ECN
839 	if (tcp_do_ecn) {
840 		/*
841 		 * if we have received congestion experienced segs,
842 		 * set ECE bit.
843 		 */
844 		if (tp->t_flags & TF_RCVD_CE) {
845 			flags |= TH_ECE;
846 			tcpstat.tcps_ecn_sndece++;
847 		}
848 		if (!(tp->t_flags & TF_DISABLE_ECN)) {
849 			/*
850 			 * if this is a SYN seg, set ECE and CWR.
851 			 * set only ECE for SYN-ACK if peer supports ECN.
852 			 */
853 			if ((flags & (TH_SYN|TH_ACK)) == TH_SYN)
854 				flags |= (TH_ECE|TH_CWR);
855 			else if ((tp->t_flags & TF_ECN_PERMIT) &&
856 				 (flags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK))
857 				flags |= TH_ECE;
858 		}
859 		/*
860 		 * if we have reduced the congestion window, notify
861 		 * the peer by setting CWR bit.
862 		 */
863 		if ((tp->t_flags & TF_ECN_PERMIT) &&
864 		    (tp->t_flags & TF_SEND_CWR)) {
865 			flags |= TH_CWR;
866 			tp->t_flags &= ~TF_SEND_CWR;
867 			tcpstat.tcps_ecn_sndcwr++;
868 		}
869 	}
870 #endif
871 	th->th_flags = flags;
872 
873 	/*
874 	 * Calculate receive window.  Don't shrink window,
875 	 * but avoid silly window syndrome.
876 	 */
877 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
878 		win = 0;
879 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
880 		win = (long)TCP_MAXWIN << tp->rcv_scale;
881 	if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt))
882 		win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt);
883 	if (flags & TH_RST)
884 		win = 0;
885 	th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
886 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
887 		u_int32_t urp = tp->snd_up - tp->snd_nxt;
888 		if (urp > IP_MAXPACKET)
889 			urp = IP_MAXPACKET;
890 		th->th_urp = htons((u_int16_t)urp);
891 		th->th_flags |= TH_URG;
892 	} else
893 		/*
894 		 * If no urgent pointer to send, then we pull
895 		 * the urgent pointer to the left edge of the send window
896 		 * so that it doesn't drift into the send window on sequence
897 		 * number wraparound.
898 		 */
899 		tp->snd_up = tp->snd_una;		/* drag it along */
900 
901 #ifdef TCP_SIGNATURE
902 	if (tp->t_flags & TF_SIGNATURE) {
903 		int iphlen;
904 		union sockaddr_union src, dst;
905 		struct tdb *tdb;
906 
907 		bzero(&src, sizeof(union sockaddr_union));
908 		bzero(&dst, sizeof(union sockaddr_union));
909 
910 		switch (tp->pf) {
911 		case 0:	/*default to PF_INET*/
912 #ifdef INET
913 		case AF_INET:
914 			iphlen = sizeof(struct ip);
915 			src.sa.sa_len = sizeof(struct sockaddr_in);
916 			src.sa.sa_family = AF_INET;
917 			src.sin.sin_addr = mtod(m, struct ip *)->ip_src;
918 			dst.sa.sa_len = sizeof(struct sockaddr_in);
919 			dst.sa.sa_family = AF_INET;
920 			dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst;
921 			break;
922 #endif /* INET */
923 #ifdef INET6
924 		case AF_INET6:
925 			iphlen = sizeof(struct ip6_hdr);
926 			src.sa.sa_len = sizeof(struct sockaddr_in6);
927 			src.sa.sa_family = AF_INET6;
928 			src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src;
929 			dst.sa.sa_len = sizeof(struct sockaddr_in6);
930 			dst.sa.sa_family = AF_INET6;
931 			dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst;
932 			break;
933 #endif /* INET6 */
934 		}
935 
936 		/* XXX gettdbbysrcdst() should really be called at spltdb(). */
937 		/* XXX this is splsoftnet(), currently they are the same. */
938 		tdb = gettdbbysrcdst(rtable_l2(tp->t_inpcb->inp_rtableid),
939 		    0, &src, &dst, IPPROTO_TCP);
940 		if (tdb == NULL)
941 			return (EPERM);
942 
943 		if (tcp_signature(tdb, tp->pf, m, th, iphlen, 0,
944 		    mtod(m, caddr_t) + hdrlen - optlen + sigoff) < 0)
945 			return (EINVAL);
946 	}
947 #endif /* TCP_SIGNATURE */
948 
949 	/*
950 	 * Put TCP length in extended header, and then
951 	 * checksum extended header and data.
952 	 */
953 	switch (tp->pf) {
954 	case 0:	/*default to PF_INET*/
955 #ifdef INET
956 	case AF_INET:
957 		/* Defer checksumming until later (ip_output() or hardware) */
958 		m->m_pkthdr.csum_flags |= M_TCP_CSUM_OUT;
959 		if (len + optlen)
960 			th->th_sum = in_cksum_addword(th->th_sum,
961 			    htons((u_int16_t)(len + optlen)));
962 		break;
963 #endif /* INET */
964 #ifdef INET6
965 	case AF_INET6:
966 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
967 			hdrlen - sizeof(struct ip6_hdr) + len);
968 		break;
969 #endif /* INET6 */
970 	}
971 
972 	/*
973 	 * In transmit state, time the transmission and arrange for
974 	 * the retransmit.  In persist state, just set snd_max.
975 	 */
976 	if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
977 		tcp_seq startseq = tp->snd_nxt;
978 
979 		/*
980 		 * Advance snd_nxt over sequence space of this segment.
981 		 */
982 		if (flags & (TH_SYN|TH_FIN)) {
983 			if (flags & TH_SYN)
984 				tp->snd_nxt++;
985 			if (flags & TH_FIN) {
986 				tp->snd_nxt++;
987 				tp->t_flags |= TF_SENTFIN;
988 			}
989 		}
990 #ifdef TCP_SACK
991 		if (tp->sack_enable) {
992 			if (sack_rxmit && (p->rxmit != tp->snd_nxt)) {
993 				goto timer;
994 			}
995 		}
996 #endif
997 		tp->snd_nxt += len;
998 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
999 			tp->snd_max = tp->snd_nxt;
1000 			/*
1001 			 * Time this transmission if not a retransmission and
1002 			 * not currently timing anything.
1003 			 */
1004 			if (tp->t_rtttime == 0) {
1005 				tp->t_rtttime = tcp_now;
1006 				tp->t_rtseq = startseq;
1007 				tcpstat.tcps_segstimed++;
1008 			}
1009 		}
1010 
1011 		/*
1012 		 * Set retransmit timer if not currently set,
1013 		 * and not doing an ack or a keep-alive probe.
1014 		 * Initial value for retransmit timer is smoothed
1015 		 * round-trip time + 2 * round-trip time variance.
1016 		 * Initialize shift counter which is used for backoff
1017 		 * of retransmit time.
1018 		 */
1019 #ifdef TCP_SACK
1020  timer:
1021 		if (tp->sack_enable && sack_rxmit &&
1022 		    TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
1023 		    tp->snd_nxt != tp->snd_max) {
1024 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1025 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
1026 				TCP_TIMER_DISARM(tp, TCPT_PERSIST);
1027 				tp->t_rxtshift = 0;
1028 			}
1029 		}
1030 #endif
1031 
1032 		if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
1033 		    tp->snd_nxt != tp->snd_una) {
1034 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1035 			if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
1036 				TCP_TIMER_DISARM(tp, TCPT_PERSIST);
1037 				tp->t_rxtshift = 0;
1038 			}
1039 		}
1040 	} else
1041 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
1042 			tp->snd_max = tp->snd_nxt + len;
1043 
1044 	tcp_update_sndspace(tp);
1045 
1046 	/*
1047 	 * Trace.
1048 	 */
1049 	if (so->so_options & SO_DEBUG)
1050 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, caddr_t), 0,
1051 			len);
1052 
1053 	/*
1054 	 * Fill in IP length and desired time to live and
1055 	 * send to IP level.  There should be a better way
1056 	 * to handle ttl and tos; we could keep them in
1057 	 * the template, but need a way to checksum without them.
1058 	 */
1059 
1060 #ifdef TCP_ECN
1061 	/*
1062 	 * if peer is ECN capable, set the ECT bit in the IP header.
1063 	 * but don't set ECT for a pure ack, a retransmit or a window probe.
1064 	 */
1065 	needect = 0;
1066 	if (tcp_do_ecn && (tp->t_flags & TF_ECN_PERMIT)) {
1067 		if (len == 0 || SEQ_LT(tp->snd_nxt, tp->snd_max) ||
1068 		    (tp->t_force && len == 1)) {
1069 			/* don't set ECT */
1070 		} else {
1071 			needect = 1;
1072 			tcpstat.tcps_ecn_sndect++;
1073 		}
1074 	}
1075 #endif
1076 
1077 	/* force routing domain */
1078 	m->m_pkthdr.rdomain = tp->t_inpcb->inp_rtableid;
1079 
1080 	switch (tp->pf) {
1081 	case 0:	/*default to PF_INET*/
1082 #ifdef INET
1083 	case AF_INET:
1084 		{
1085 			struct ip *ip;
1086 
1087 			ip = mtod(m, struct ip *);
1088 			ip->ip_len = htons(m->m_pkthdr.len);
1089 			packetlen = m->m_pkthdr.len;
1090 			ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
1091 			ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
1092 #ifdef TCP_ECN
1093 			if (needect)
1094 				ip->ip_tos |= IPTOS_ECN_ECT0;
1095 #endif
1096 		}
1097 		error = ip_output(m, tp->t_inpcb->inp_options,
1098 			&tp->t_inpcb->inp_route,
1099 			(ip_mtudisc ? IP_MTUDISC : 0) |
1100 				  (so->so_options & SO_DONTROUTE),
1101 			(void *)NULL, tp->t_inpcb);
1102 		break;
1103 #endif /* INET */
1104 #ifdef INET6
1105 	case AF_INET6:
1106 		{
1107 			struct ip6_hdr *ip6;
1108 
1109 			ip6 = mtod(m, struct ip6_hdr *);
1110 			ip6->ip6_plen = m->m_pkthdr.len -
1111 				sizeof(struct ip6_hdr);
1112 			packetlen = m->m_pkthdr.len;
1113 			ip6->ip6_nxt = IPPROTO_TCP;
1114 			ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1115 #ifdef TCP_ECN
1116 			if (needect)
1117 				ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
1118 #endif
1119 		}
1120 		error = ip6_output(m, tp->t_inpcb->inp_outputopts6,
1121 			  &tp->t_inpcb->inp_route6,
1122 			  (so->so_options & SO_DONTROUTE), NULL, NULL,
1123 			  tp->t_inpcb);
1124 		break;
1125 #endif /* INET6 */
1126 	}
1127 
1128 #if defined(TCP_SACK) && defined(TCP_FACK)
1129 	/* Update snd_awnd to reflect the new data that was sent.  */
1130 	tp->snd_awnd = tcp_seq_subtract(tp->snd_max, tp->snd_fack) +
1131 		tp->retran_data;
1132 #endif /* defined(TCP_SACK) && defined(TCP_FACK) */
1133 
1134 	if (error) {
1135 out:
1136 		if (error == ENOBUFS) {
1137 			/*
1138 			 * If the interface queue is full, or IP cannot
1139 			 * get an mbuf, trigger TCP slow start.
1140 			 */
1141 			tp->snd_cwnd = tp->t_maxseg;
1142 			return (0);
1143 		}
1144 		if (error == EMSGSIZE) {
1145 			/*
1146 			 * ip_output() will have already fixed the route
1147 			 * for us.  tcp_mtudisc() will, as its last action,
1148 			 * initiate retransmission, so it is important to
1149 			 * not do so here.
1150 			 */
1151 			tcp_mtudisc(tp->t_inpcb, -1);
1152 			return (0);
1153 		}
1154 		if (error == EACCES)	/* translate pf(4) error for userland */
1155 			error = EHOSTUNREACH;
1156 		if ((error == EHOSTUNREACH || error == ENETDOWN) &&
1157 		    TCPS_HAVERCVDSYN(tp->t_state)) {
1158 			tp->t_softerror = error;
1159 			return (0);
1160 		}
1161 
1162 		/* Restart the delayed ACK timer, if necessary. */
1163 		if (tp->t_flags & TF_DELACK)
1164 			TCP_RESTART_DELACK(tp);
1165 
1166 		return (error);
1167 	}
1168 
1169 	if (packetlen > tp->t_pmtud_mtu_sent)
1170 		tp->t_pmtud_mtu_sent = packetlen;
1171 
1172 	tcpstat.tcps_sndtotal++;
1173 	if (tp->t_flags & TF_DELACK)
1174 		tcpstat.tcps_delack++;
1175 
1176 	/*
1177 	 * Data sent (as far as we can tell).
1178 	 * If this advertises a larger window than any other segment,
1179 	 * then remember the size of the advertised window.
1180 	 * Any pending ACK has now been sent.
1181 	 */
1182 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1183 		tp->rcv_adv = tp->rcv_nxt + win;
1184 	tp->last_ack_sent = tp->rcv_nxt;
1185 	tp->t_flags &= ~TF_ACKNOW;
1186 	TCP_CLEAR_DELACK(tp);
1187 #if defined(TCP_SACK)
1188 	if (sendalot && --maxburst)
1189 #else
1190 	if (sendalot)
1191 #endif
1192 		goto again;
1193 	return (0);
1194 }
1195 
1196 void
1197 tcp_setpersist(struct tcpcb *tp)
1198 {
1199 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + TCP_RTT_BASE_SHIFT);
1200 	int nticks;
1201 
1202 	if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
1203 		panic("tcp_output REXMT");
1204 	/*
1205 	 * Start/restart persistence timer.
1206 	 */
1207 	if (t < tp->t_rttmin)
1208 		t = tp->t_rttmin;
1209 	TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
1210 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
1211 	TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
1212 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1213 		tp->t_rxtshift++;
1214 }
1215