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