xref: /freebsd/sys/netinet/tcp_output.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)tcp_output.c	8.4 (Berkeley) 5/24/95
30  * $FreeBSD$
31  */
32 
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_ipsec.h"
36 #include "opt_mac.h"
37 #include "opt_tcpdebug.h"
38 #include "opt_tcp_sack.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/domain.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mbuf.h>
46 #include <sys/mutex.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 
52 #include <net/route.h>
53 
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/ip_var.h>
59 #include <netinet/ip_options.h>
60 #ifdef INET6
61 #include <netinet6/in6_pcb.h>
62 #include <netinet/ip6.h>
63 #include <netinet6/ip6_var.h>
64 #endif
65 #include <netinet/tcp.h>
66 #define	TCPOUTFLAGS
67 #include <netinet/tcp_fsm.h>
68 #include <netinet/tcp_seq.h>
69 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/tcpip.h>
72 #ifdef TCPDEBUG
73 #include <netinet/tcp_debug.h>
74 #endif
75 
76 #ifdef IPSEC
77 #include <netinet6/ipsec.h>
78 #endif /*IPSEC*/
79 
80 #ifdef FAST_IPSEC
81 #include <netipsec/ipsec.h>
82 #define	IPSEC
83 #endif /*FAST_IPSEC*/
84 
85 #include <machine/in_cksum.h>
86 
87 #include <security/mac/mac_framework.h>
88 
89 #ifdef notyet
90 extern struct mbuf *m_copypack();
91 #endif
92 
93 int path_mtu_discovery = 1;
94 SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW,
95 	&path_mtu_discovery, 1, "Enable Path MTU Discovery");
96 
97 int ss_fltsz = 1;
98 SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW,
99 	&ss_fltsz, 1, "Slow start flight size");
100 
101 int ss_fltsz_local = 4;
102 SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW,
103 	&ss_fltsz_local, 1, "Slow start flight size for local networks");
104 
105 int     tcp_do_newreno = 1;
106 SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno,
107 	0, "Enable NewReno Algorithms");
108 
109 int	tcp_do_tso = 1;
110 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_RW,
111 	&tcp_do_tso, 0, "Enable TCP Segmentation Offload");
112 
113 /*
114  * Tcp output routine: figure out what should be sent and send it.
115  */
116 int
117 tcp_output(struct tcpcb *tp)
118 {
119 	struct socket *so = tp->t_inpcb->inp_socket;
120 	long len, recwin, sendwin;
121 	int off, flags, error;
122 #ifdef TCP_SIGNATURE
123 	int sigoff = 0;
124 #endif
125 	struct mbuf *m;
126 	struct ip *ip = NULL;
127 	struct ipovly *ipov = NULL;
128 	struct tcphdr *th;
129 	u_char opt[TCP_MAXOLEN];
130 	unsigned ipoptlen, optlen, hdrlen;
131 	int idle, sendalot;
132 	int i, sack_rxmit;
133 	int sack_bytes_rxmt;
134 	struct sackhole *p;
135 	int tso = 0;
136 #if 0
137 	int maxburst = TCP_MAXBURST;
138 #endif
139 #ifdef INET6
140 	struct ip6_hdr *ip6 = NULL;
141 	int isipv6;
142 
143 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
144 #endif
145 
146 	INP_LOCK_ASSERT(tp->t_inpcb);
147 
148 	/*
149 	 * Determine length of data that should be transmitted,
150 	 * and flags that will be used.
151 	 * If there is some data or critical controls (SYN, RST)
152 	 * to send, then transmit; otherwise, investigate further.
153 	 */
154 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
155 	if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
156 		/*
157 		 * We have been idle for "a while" and no acks are
158 		 * expected to clock out any data we send --
159 		 * slow start to get ack "clock" running again.
160 		 *
161 		 * Set the slow-start flight size depending on whether
162 		 * this is a local network or not.
163 		 */
164 		int ss = ss_fltsz;
165 #ifdef INET6
166 		if (isipv6) {
167 			if (in6_localaddr(&tp->t_inpcb->in6p_faddr))
168 				ss = ss_fltsz_local;
169 		} else
170 #endif /* INET6 */
171 		if (in_localaddr(tp->t_inpcb->inp_faddr))
172 			ss = ss_fltsz_local;
173 		tp->snd_cwnd = tp->t_maxseg * ss;
174 	}
175 	tp->t_flags &= ~TF_LASTIDLE;
176 	if (idle) {
177 		if (tp->t_flags & TF_MORETOCOME) {
178 			tp->t_flags |= TF_LASTIDLE;
179 			idle = 0;
180 		}
181 	}
182 again:
183 	/*
184 	 * If we've recently taken a timeout, snd_max will be greater than
185 	 * snd_nxt.  There may be SACK information that allows us to avoid
186 	 * resending already delivered data.  Adjust snd_nxt accordingly.
187 	 */
188 	if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max))
189 		tcp_sack_adjust(tp);
190 	sendalot = 0;
191 	off = tp->snd_nxt - tp->snd_una;
192 	sendwin = min(tp->snd_wnd, tp->snd_cwnd);
193 	sendwin = min(sendwin, tp->snd_bwnd);
194 
195 	flags = tcp_outflags[tp->t_state];
196 	/*
197 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
198 	 * to send out new data (when sendalot is 1), bypass this function.
199 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
200 	 * we're replacing a (future) new transmission with a retransmission
201 	 * now, and we previously incremented snd_cwnd in tcp_input().
202 	 */
203 	/*
204 	 * Still in sack recovery , reset rxmit flag to zero.
205 	 */
206 	sack_rxmit = 0;
207 	sack_bytes_rxmt = 0;
208 	len = 0;
209 	p = NULL;
210 	if (tp->sack_enable && IN_FASTRECOVERY(tp) &&
211 	    (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
212 		long cwin;
213 
214 		cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
215 		if (cwin < 0)
216 			cwin = 0;
217 		/* Do not retransmit SACK segments beyond snd_recover */
218 		if (SEQ_GT(p->end, tp->snd_recover)) {
219 			/*
220 			 * (At least) part of sack hole extends beyond
221 			 * snd_recover. Check to see if we can rexmit data
222 			 * for this hole.
223 			 */
224 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
225 				/*
226 				 * Can't rexmit any more data for this hole.
227 				 * That data will be rexmitted in the next
228 				 * sack recovery episode, when snd_recover
229 				 * moves past p->rxmit.
230 				 */
231 				p = NULL;
232 				goto after_sack_rexmit;
233 			} else
234 				/* Can rexmit part of the current hole */
235 				len = ((long)ulmin(cwin,
236 						   tp->snd_recover - p->rxmit));
237 		} else
238 			len = ((long)ulmin(cwin, p->end - p->rxmit));
239 		off = p->rxmit - tp->snd_una;
240 		KASSERT(off >= 0,("%s: sack block to the left of una : %d",
241 		    __func__, off));
242 		if (len > 0) {
243 			sack_rxmit = 1;
244 			sendalot = 1;
245 			tcpstat.tcps_sack_rexmits++;
246 			tcpstat.tcps_sack_rexmit_bytes +=
247 			    min(len, tp->t_maxseg);
248 		}
249 	}
250 after_sack_rexmit:
251 	/*
252 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
253 	 * state flags.
254 	 */
255 	if (tp->t_flags & TF_NEEDFIN)
256 		flags |= TH_FIN;
257 	if (tp->t_flags & TF_NEEDSYN)
258 		flags |= TH_SYN;
259 
260 	SOCKBUF_LOCK(&so->so_snd);
261 	/*
262 	 * If in persist timeout with window of 0, send 1 byte.
263 	 * Otherwise, if window is small but nonzero
264 	 * and timer expired, we will send what we can
265 	 * and go to transmit state.
266 	 */
267 	if (tp->t_flags & TF_FORCEDATA) {
268 		if (sendwin == 0) {
269 			/*
270 			 * If we still have some data to send, then
271 			 * clear the FIN bit.  Usually this would
272 			 * happen below when it realizes that we
273 			 * aren't sending all the data.  However,
274 			 * if we have exactly 1 byte of unsent data,
275 			 * then it won't clear the FIN bit below,
276 			 * and if we are in persist state, we wind
277 			 * up sending the packet without recording
278 			 * that we sent the FIN bit.
279 			 *
280 			 * We can't just blindly clear the FIN bit,
281 			 * because if we don't have any more data
282 			 * to send then the probe will be the FIN
283 			 * itself.
284 			 */
285 			if (off < so->so_snd.sb_cc)
286 				flags &= ~TH_FIN;
287 			sendwin = 1;
288 		} else {
289 			callout_stop(tp->tt_persist);
290 			tp->t_rxtshift = 0;
291 		}
292 	}
293 
294 	/*
295 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
296 	 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
297 	 * a negative length.  This can also occur when TCP opens up
298 	 * its congestion window while receiving additional duplicate
299 	 * acks after fast-retransmit because TCP will reset snd_nxt
300 	 * to snd_max after the fast-retransmit.
301 	 *
302 	 * In the normal retransmit-FIN-only case, however, snd_nxt will
303 	 * be set to snd_una, the offset will be 0, and the length may
304 	 * wind up 0.
305 	 *
306 	 * If sack_rxmit is true we are retransmitting from the scoreboard
307 	 * in which case len is already set.
308 	 */
309 	if (sack_rxmit == 0) {
310 		if (sack_bytes_rxmt == 0)
311 			len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
312 		else {
313 			long cwin;
314 
315                         /*
316 			 * We are inside of a SACK recovery episode and are
317 			 * sending new data, having retransmitted all the
318 			 * data possible in the scoreboard.
319 			 */
320 			len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd)
321 			       - off);
322 			/*
323 			 * Don't remove this (len > 0) check !
324 			 * We explicitly check for len > 0 here (although it
325 			 * isn't really necessary), to work around a gcc
326 			 * optimization issue - to force gcc to compute
327 			 * len above. Without this check, the computation
328 			 * of len is bungled by the optimizer.
329 			 */
330 			if (len > 0) {
331 				cwin = tp->snd_cwnd -
332 					(tp->snd_nxt - tp->sack_newdata) -
333 					sack_bytes_rxmt;
334 				if (cwin < 0)
335 					cwin = 0;
336 				len = lmin(len, cwin);
337 			}
338 		}
339 	}
340 
341 	/*
342 	 * Lop off SYN bit if it has already been sent.  However, if this
343 	 * is SYN-SENT state and if segment contains data and if we don't
344 	 * know that foreign host supports TAO, suppress sending segment.
345 	 */
346 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
347 		if (tp->t_state != TCPS_SYN_RECEIVED)
348 			flags &= ~TH_SYN;
349 		off--, len++;
350 	}
351 
352 	/*
353 	 * Be careful not to send data and/or FIN on SYN segments.
354 	 * This measure is needed to prevent interoperability problems
355 	 * with not fully conformant TCP implementations.
356 	 */
357 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
358 		len = 0;
359 		flags &= ~TH_FIN;
360 	}
361 
362 	if (len < 0) {
363 		/*
364 		 * If FIN has been sent but not acked,
365 		 * but we haven't been called to retransmit,
366 		 * len will be < 0.  Otherwise, window shrank
367 		 * after we sent into it.  If window shrank to 0,
368 		 * cancel pending retransmit, pull snd_nxt back
369 		 * to (closed) window, and set the persist timer
370 		 * if it isn't already going.  If the window didn't
371 		 * close completely, just wait for an ACK.
372 		 */
373 		len = 0;
374 		if (sendwin == 0) {
375 			callout_stop(tp->tt_rexmt);
376 			tp->t_rxtshift = 0;
377 			tp->snd_nxt = tp->snd_una;
378 			if (!callout_active(tp->tt_persist))
379 				tcp_setpersist(tp);
380 		}
381 	}
382 
383 	/*
384 	 * len will be >= 0 after this point.  Truncate to the maximum
385 	 * segment length or enable TCP Segmentation Offloading (if supported
386 	 * by hardware) and ensure that FIN is removed if the length no longer
387 	 * contains the last data byte.
388 	 *
389 	 * TSO may only be used if we are in a pure bulk sending state.  The
390 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and
391 	 * IP options prevent using TSO.  With TSO the TCP header is the same
392 	 * (except for the sequence number) for all generated packets.  This
393 	 * makes it impossible to transmit any options which vary per generated
394 	 * segment or packet.
395 	 *
396 	 * The length of TSO bursts is limited to TCP_MAXWIN.  That limit and
397 	 * removal of FIN (if not already catched here) are handled later after
398 	 * the exact length of the TCP options are known.
399 	 */
400 	if (len > tp->t_maxseg) {
401 		if ((tp->t_flags & TF_TSO) && tcp_do_tso &&
402 		    ((tp->t_flags & TF_SIGNATURE) == 0) &&
403 		    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
404 		    tp->t_inpcb->inp_options == NULL &&
405 		    tp->t_inpcb->in6p_options == NULL &&
406 		    tp->t_inpcb->inp_sp == NULL) {
407 			tso = 1;
408 		} else {
409 			len = tp->t_maxseg;
410 			sendalot = 1;
411 			tso = 0;
412 		}
413 	}
414 	if (sack_rxmit) {
415 		if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
416 			flags &= ~TH_FIN;
417 	} else {
418 		if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
419 			flags &= ~TH_FIN;
420 	}
421 
422 	recwin = sbspace(&so->so_rcv);
423 
424 	/*
425 	 * Sender silly window avoidance.   We transmit under the following
426 	 * conditions when len is non-zero:
427 	 *
428 	 *	- We have a full segment (or more with TSO)
429 	 *	- This is the last buffer in a write()/send() and we are
430 	 *	  either idle or running NODELAY
431 	 *	- we've timed out (e.g. persist timer)
432 	 *	- we have more then 1/2 the maximum send window's worth of
433 	 *	  data (receiver may be limited the window size)
434 	 *	- we need to retransmit
435 	 */
436 	if (len) {
437 		if (len >= tp->t_maxseg)
438 			goto send;
439 		/*
440 		 * NOTE! on localhost connections an 'ack' from the remote
441 		 * end may occur synchronously with the output and cause
442 		 * us to flush a buffer queued with moretocome.  XXX
443 		 *
444 		 * note: the len + off check is almost certainly unnecessary.
445 		 */
446 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
447 		    (idle || (tp->t_flags & TF_NODELAY)) &&
448 		    len + off >= so->so_snd.sb_cc &&
449 		    (tp->t_flags & TF_NOPUSH) == 0) {
450 			goto send;
451 		}
452 		if (tp->t_flags & TF_FORCEDATA)		/* typ. timeout case */
453 			goto send;
454 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
455 			goto send;
456 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))	/* retransmit case */
457 			goto send;
458 		if (sack_rxmit)
459 			goto send;
460 	}
461 
462 	/*
463 	 * Compare available window to amount of window
464 	 * known to peer (as advertised window less
465 	 * next expected input).  If the difference is at least two
466 	 * max size segments, or at least 50% of the maximum possible
467 	 * window, then want to send a window update to peer.
468 	 * Skip this if the connection is in T/TCP half-open state.
469 	 */
470 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) {
471 		/*
472 		 * "adv" is the amount we can increase the window,
473 		 * taking into account that we are limited by
474 		 * TCP_MAXWIN << tp->rcv_scale.
475 		 */
476 		long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
477 			(tp->rcv_adv - tp->rcv_nxt);
478 
479 		if (adv >= (long) (2 * tp->t_maxseg))
480 			goto send;
481 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
482 			goto send;
483 	}
484 
485 	/*
486 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
487 	 * is also a catch-all for the retransmit timer timeout case.
488 	 */
489 	if (tp->t_flags & TF_ACKNOW)
490 		goto send;
491 	if ((flags & TH_RST) ||
492 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
493 		goto send;
494 	if (SEQ_GT(tp->snd_up, tp->snd_una))
495 		goto send;
496 	/*
497 	 * If our state indicates that FIN should be sent
498 	 * and we have not yet done so, then we need to send.
499 	 */
500 	if (flags & TH_FIN &&
501 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
502 		goto send;
503 	/*
504 	 * In SACK, it is possible for tcp_output to fail to send a segment
505 	 * after the retransmission timer has been turned off.  Make sure
506 	 * that the retransmission timer is set.
507 	 */
508 	if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) &&
509 	    !callout_active(tp->tt_rexmt) &&
510 	    !callout_active(tp->tt_persist)) {
511 		callout_reset(tp->tt_rexmt, tp->t_rxtcur,
512 			      tcp_timer_rexmt, tp);
513 		goto just_return;
514 	}
515 	/*
516 	 * TCP window updates are not reliable, rather a polling protocol
517 	 * using ``persist'' packets is used to insure receipt of window
518 	 * updates.  The three ``states'' for the output side are:
519 	 *	idle			not doing retransmits or persists
520 	 *	persisting		to move a small or zero window
521 	 *	(re)transmitting	and thereby not persisting
522 	 *
523 	 * callout_active(tp->tt_persist)
524 	 *	is true when we are in persist state.
525 	 * (tp->t_flags & TF_FORCEDATA)
526 	 *	is set when we are called to send a persist packet.
527 	 * callout_active(tp->tt_rexmt)
528 	 *	is set when we are retransmitting
529 	 * The output side is idle when both timers are zero.
530 	 *
531 	 * If send window is too small, there is data to transmit, and no
532 	 * retransmit or persist is pending, then go to persist state.
533 	 * If nothing happens soon, send when timer expires:
534 	 * if window is nonzero, transmit what we can,
535 	 * otherwise force out a byte.
536 	 */
537 	if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) &&
538 	    !callout_active(tp->tt_persist)) {
539 		tp->t_rxtshift = 0;
540 		tcp_setpersist(tp);
541 	}
542 
543 	/*
544 	 * No reason to send a segment, just return.
545 	 */
546 just_return:
547 	SOCKBUF_UNLOCK(&so->so_snd);
548 	return (0);
549 
550 send:
551 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
552 	/*
553 	 * Before ESTABLISHED, force sending of initial options
554 	 * unless TCP set not to do any options.
555 	 * NOTE: we assume that the IP/TCP header plus TCP options
556 	 * always fit in a single mbuf, leaving room for a maximum
557 	 * link header, i.e.
558 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
559 	 */
560 	optlen = 0;
561 #ifdef INET6
562 	if (isipv6)
563 		hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
564 	else
565 #endif
566 	hdrlen = sizeof (struct tcpiphdr);
567 	if (flags & TH_SYN) {
568 		tp->snd_nxt = tp->iss;
569 		if ((tp->t_flags & TF_NOOPT) == 0) {
570 			u_short mss;
571 
572 			opt[0] = TCPOPT_MAXSEG;
573 			opt[1] = TCPOLEN_MAXSEG;
574 			mss = htons((u_short) tcp_mssopt(&tp->t_inpcb->inp_inc));
575 			(void)memcpy(opt + 2, &mss, sizeof(mss));
576 			optlen = TCPOLEN_MAXSEG;
577 
578 			if ((tp->t_flags & TF_REQ_SCALE) &&
579 			    ((flags & TH_ACK) == 0 ||
580 			    (tp->t_flags & TF_RCVD_SCALE))) {
581 				*((u_int32_t *)(opt + optlen)) = htonl(
582 					TCPOPT_NOP << 24 |
583 					TCPOPT_WINDOW << 16 |
584 					TCPOLEN_WINDOW << 8 |
585 					tp->request_r_scale);
586 				optlen += 4;
587 			}
588 		}
589 	}
590 
591 	/*
592 	 * Send a timestamp and echo-reply if this is a SYN and our side
593 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
594 	 * and our peer have sent timestamps in our SYN's.
595 	 */
596 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
597 	    (flags & TH_RST) == 0 &&
598 	    ((flags & TH_ACK) == 0 ||
599 	     (tp->t_flags & TF_RCVD_TSTMP))) {
600 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
601 
602 		/* Form timestamp option as shown in appendix A of RFC 1323. */
603 		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
604 		*lp++ = htonl(ticks + tp->ts_offset);
605 		*lp   = htonl(tp->ts_recent);
606 		optlen += TCPOLEN_TSTAMP_APPA;
607 	}
608 
609 #ifdef TCP_SIGNATURE
610 #ifdef INET6
611 	if (!isipv6)
612 #endif
613 	if (tp->t_flags & TF_SIGNATURE) {
614 		int i;
615 		u_char *bp;
616 
617 		/* Initialize TCP-MD5 option (RFC2385) */
618 		bp = (u_char *)opt + optlen;
619 		*bp++ = TCPOPT_SIGNATURE;
620 		*bp++ = TCPOLEN_SIGNATURE;
621 		sigoff = optlen + 2;
622 		for (i = 0; i < TCP_SIGLEN; i++)
623 			*bp++ = 0;
624 		optlen += TCPOLEN_SIGNATURE;
625 	}
626 #endif /* TCP_SIGNATURE */
627 
628 	if (tp->sack_enable && ((tp->t_flags & TF_NOOPT) == 0)) {
629 		/*
630 		 * Tack on the SACK permitted option *last*.
631 		 * And do padding of options after tacking this on.
632 		 * This is because of MSS, TS, WinScale and Signatures are
633 		 * all present, we have just 2 bytes left for the SACK
634 		 * permitted option, which is just enough.
635 		 */
636 		/*
637 		 * If this is the first SYN of connection (not a SYN
638 		 * ACK), include SACK permitted option.  If this is a
639 		 * SYN ACK, include SACK permitted option if peer has
640 		 * already done so. This is only for active connect,
641 		 * since the syncache takes care of the passive connect.
642 		 */
643 		if ((flags & TH_SYN) &&
644 		    (!(flags & TH_ACK) || (tp->t_flags & TF_SACK_PERMIT))) {
645 			u_char *bp;
646 			bp = (u_char *)opt + optlen;
647 
648 			*bp++ = TCPOPT_SACK_PERMITTED;
649 			*bp++ = TCPOLEN_SACK_PERMITTED;
650 			optlen += TCPOLEN_SACK_PERMITTED;
651 		}
652 
653 		/*
654 		 * Send SACKs if necessary.  This should be the last
655 		 * option processed.  Only as many SACKs are sent as
656 		 * are permitted by the maximum options size.
657 		 *
658 		 * In general, SACK blocks consume 8*n+2 bytes.
659 		 * So a full size SACK blocks option is 34 bytes
660 		 * (to generate 4 SACK blocks).  At a minimum,
661 		 * we need 10 bytes (to generate 1 SACK block).
662 		 * If TCP Timestamps (12 bytes) and TCP Signatures
663 		 * (18 bytes) are both present, we'll just have
664 		 * 10 bytes for SACK options 40 - (12 + 18).
665 		 */
666 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
667 		    (tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks > 0 &&
668 		    MAX_TCPOPTLEN - optlen - 2 >= TCPOLEN_SACK) {
669 			int nsack, sackoptlen, padlen;
670 			u_char *bp = (u_char *)opt + optlen;
671 			u_int32_t *lp;
672 
673 			nsack = (MAX_TCPOPTLEN - optlen - 2) / TCPOLEN_SACK;
674 			nsack = min(nsack, tp->rcv_numsacks);
675 			sackoptlen = (2 + nsack * TCPOLEN_SACK);
676 
677 			/*
678 			 * First we need to pad options so that the
679 			 * SACK blocks can start at a 4-byte boundary
680 			 * (sack option and length are at a 2 byte offset).
681 			 */
682 			padlen = (MAX_TCPOPTLEN - optlen - sackoptlen) % 4;
683 			optlen += padlen;
684 			while (padlen-- > 0)
685 				*bp++ = TCPOPT_NOP;
686 
687 			tcpstat.tcps_sack_send_blocks++;
688 			*bp++ = TCPOPT_SACK;
689 			*bp++ = sackoptlen;
690 			lp = (u_int32_t *)bp;
691 			for (i = 0; i < nsack; i++) {
692 				struct sackblk sack = tp->sackblks[i];
693 				*lp++ = htonl(sack.start);
694 				*lp++ = htonl(sack.end);
695 			}
696 			optlen += sackoptlen;
697 		}
698 	}
699 
700 	/* Pad TCP options to a 4 byte boundary */
701 	if (optlen < MAX_TCPOPTLEN && (optlen % sizeof(u_int32_t))) {
702 		int pad = sizeof(u_int32_t) - (optlen % sizeof(u_int32_t));
703 		u_char *bp = (u_char *)opt + optlen;
704 
705 		optlen += pad;
706 		while (pad) {
707 			*bp++ = TCPOPT_EOL;
708 			pad--;
709 		}
710 	}
711 
712 	hdrlen += optlen;
713 
714 #ifdef INET6
715 	if (isipv6)
716 		ipoptlen = ip6_optlen(tp->t_inpcb);
717 	else
718 #endif
719 	if (tp->t_inpcb->inp_options)
720 		ipoptlen = tp->t_inpcb->inp_options->m_len -
721 				offsetof(struct ipoption, ipopt_list);
722 	else
723 		ipoptlen = 0;
724 #ifdef IPSEC
725 	ipoptlen += ipsec_hdrsiz_tcp(tp);
726 #endif
727 
728 	/*
729 	 * Adjust data length if insertion of options will
730 	 * bump the packet length beyond the t_maxopd length.
731 	 * Clear the FIN bit because we cut off the tail of
732 	 * the segment.
733 	 *
734 	 * When doing TSO limit a burst to TCP_MAXWIN minus the
735 	 * IP, TCP and Options length to keep ip->ip_len from
736 	 * overflowing.  Prevent the last segment from being
737 	 * fractional thus making them all equal sized and set
738 	 * the flag to continue sending.
739 	 */
740 	if (len + optlen + ipoptlen > tp->t_maxopd) {
741 		flags &= ~TH_FIN;
742 		if (tso) {
743 			if (len > TCP_MAXWIN - hdrlen) {
744 				len = TCP_MAXWIN - hdrlen;
745 				len = len - (len % (tp->t_maxopd - optlen));
746 				sendalot = 1;
747 			} else if (tp->t_flags & TF_NEEDFIN)
748 				sendalot = 1;
749 		} else {
750 			len = tp->t_maxopd - optlen - ipoptlen;
751 			sendalot = 1;
752 		}
753 	}
754 
755 /*#ifdef DIAGNOSTIC*/
756 #ifdef INET6
757 	if (max_linkhdr + hdrlen > MCLBYTES)
758 #else
759 	if (max_linkhdr + hdrlen > MHLEN)
760 #endif
761 		panic("tcphdr too big");
762 /*#endif*/
763 
764 	/*
765 	 * Grab a header mbuf, attaching a copy of data to
766 	 * be transmitted, and initialize the header from
767 	 * the template for sends on this connection.
768 	 */
769 	if (len) {
770 		if ((tp->t_flags & TF_FORCEDATA) && len == 1)
771 			tcpstat.tcps_sndprobe++;
772 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
773 			tcpstat.tcps_sndrexmitpack++;
774 			tcpstat.tcps_sndrexmitbyte += len;
775 		} else {
776 			tcpstat.tcps_sndpack++;
777 			tcpstat.tcps_sndbyte += len;
778 		}
779 #ifdef notyet
780 		if ((m = m_copypack(so->so_snd.sb_mb, off,
781 		    (int)len, max_linkhdr + hdrlen)) == 0) {
782 			SOCKBUF_UNLOCK(&so->so_snd);
783 			error = ENOBUFS;
784 			goto out;
785 		}
786 		/*
787 		 * m_copypack left space for our hdr; use it.
788 		 */
789 		m->m_len += hdrlen;
790 		m->m_data -= hdrlen;
791 #else
792 		MGETHDR(m, M_DONTWAIT, MT_DATA);
793 		if (m == NULL) {
794 			SOCKBUF_UNLOCK(&so->so_snd);
795 			error = ENOBUFS;
796 			goto out;
797 		}
798 #ifdef INET6
799 		if (MHLEN < hdrlen + max_linkhdr) {
800 			MCLGET(m, M_DONTWAIT);
801 			if ((m->m_flags & M_EXT) == 0) {
802 				SOCKBUF_UNLOCK(&so->so_snd);
803 				m_freem(m);
804 				error = ENOBUFS;
805 				goto out;
806 			}
807 		}
808 #endif
809 		m->m_data += max_linkhdr;
810 		m->m_len = hdrlen;
811 		if (len <= MHLEN - hdrlen - max_linkhdr) {
812 			m_copydata(so->so_snd.sb_mb, off, (int) len,
813 			    mtod(m, caddr_t) + hdrlen);
814 			m->m_len += len;
815 		} else {
816 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
817 			if (m->m_next == 0) {
818 				SOCKBUF_UNLOCK(&so->so_snd);
819 				(void) m_free(m);
820 				error = ENOBUFS;
821 				goto out;
822 			}
823 		}
824 #endif
825 		/*
826 		 * If we're sending everything we've got, set PUSH.
827 		 * (This will keep happy those implementations which only
828 		 * give data to the user when a buffer fills or
829 		 * a PUSH comes in.)
830 		 */
831 		if (off + len == so->so_snd.sb_cc)
832 			flags |= TH_PUSH;
833 		SOCKBUF_UNLOCK(&so->so_snd);
834 	} else {
835 		SOCKBUF_UNLOCK(&so->so_snd);
836 		if (tp->t_flags & TF_ACKNOW)
837 			tcpstat.tcps_sndacks++;
838 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
839 			tcpstat.tcps_sndctrl++;
840 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
841 			tcpstat.tcps_sndurg++;
842 		else
843 			tcpstat.tcps_sndwinup++;
844 
845 		MGETHDR(m, M_DONTWAIT, MT_DATA);
846 		if (m == NULL) {
847 			error = ENOBUFS;
848 			goto out;
849 		}
850 #ifdef INET6
851 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
852 		    MHLEN >= hdrlen) {
853 			MH_ALIGN(m, hdrlen);
854 		} else
855 #endif
856 		m->m_data += max_linkhdr;
857 		m->m_len = hdrlen;
858 	}
859 	SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
860 	m->m_pkthdr.rcvif = (struct ifnet *)0;
861 #ifdef MAC
862 	mac_create_mbuf_from_inpcb(tp->t_inpcb, m);
863 #endif
864 #ifdef INET6
865 	if (isipv6) {
866 		ip6 = mtod(m, struct ip6_hdr *);
867 		th = (struct tcphdr *)(ip6 + 1);
868 		tcpip_fillheaders(tp->t_inpcb, ip6, th);
869 	} else
870 #endif /* INET6 */
871 	{
872 		ip = mtod(m, struct ip *);
873 		ipov = (struct ipovly *)ip;
874 		th = (struct tcphdr *)(ip + 1);
875 		tcpip_fillheaders(tp->t_inpcb, ip, th);
876 	}
877 
878 	/*
879 	 * Fill in fields, remembering maximum advertised
880 	 * window for use in delaying messages about window sizes.
881 	 * If resending a FIN, be sure not to use a new sequence number.
882 	 */
883 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
884 	    tp->snd_nxt == tp->snd_max)
885 		tp->snd_nxt--;
886 	/*
887 	 * If we are doing retransmissions, then snd_nxt will
888 	 * not reflect the first unsent octet.  For ACK only
889 	 * packets, we do not want the sequence number of the
890 	 * retransmitted packet, we want the sequence number
891 	 * of the next unsent octet.  So, if there is no data
892 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
893 	 * when filling in ti_seq.  But if we are in persist
894 	 * state, snd_max might reflect one byte beyond the
895 	 * right edge of the window, so use snd_nxt in that
896 	 * case, since we know we aren't doing a retransmission.
897 	 * (retransmit and persist are mutually exclusive...)
898 	 */
899 	if (sack_rxmit == 0) {
900 		if (len || (flags & (TH_SYN|TH_FIN))
901 		    || callout_active(tp->tt_persist))
902 			th->th_seq = htonl(tp->snd_nxt);
903 		else
904 			th->th_seq = htonl(tp->snd_max);
905 	} else {
906 		th->th_seq = htonl(p->rxmit);
907 		p->rxmit += len;
908 		tp->sackhint.sack_bytes_rexmit += len;
909 	}
910 	th->th_ack = htonl(tp->rcv_nxt);
911 	if (optlen) {
912 		bcopy(opt, th + 1, optlen);
913 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
914 	}
915 	th->th_flags = flags;
916 	/*
917 	 * Calculate receive window.  Don't shrink window,
918 	 * but avoid silly window syndrome.
919 	 */
920 	if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
921 	    recwin < (long)tp->t_maxseg)
922 		recwin = 0;
923 	if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
924 		recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
925 	if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
926 		recwin = (long)TCP_MAXWIN << tp->rcv_scale;
927 	th->th_win = htons((u_short) (recwin >> tp->rcv_scale));
928 
929 
930 	/*
931 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised
932 	 * a 0 window.  This may cause the remote transmitter to stall.  This
933 	 * flag tells soreceive() to disable delayed acknowledgements when
934 	 * draining the buffer.  This can occur if the receiver is attempting
935 	 * to read more data then can be buffered prior to transmitting on
936 	 * the connection.
937 	 */
938 	if (recwin == 0)
939 		tp->t_flags |= TF_RXWIN0SENT;
940 	else
941 		tp->t_flags &= ~TF_RXWIN0SENT;
942 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
943 		th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
944 		th->th_flags |= TH_URG;
945 	} else
946 		/*
947 		 * If no urgent pointer to send, then we pull
948 		 * the urgent pointer to the left edge of the send window
949 		 * so that it doesn't drift into the send window on sequence
950 		 * number wraparound.
951 		 */
952 		tp->snd_up = tp->snd_una;		/* drag it along */
953 
954 #ifdef TCP_SIGNATURE
955 #ifdef INET6
956 	if (!isipv6)
957 #endif
958 	if (tp->t_flags & TF_SIGNATURE)
959 		tcp_signature_compute(m, sizeof(struct ip), len, optlen,
960 		    (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
961 #endif
962 
963 	/*
964 	 * Put TCP length in extended header, and then
965 	 * checksum extended header and data.
966 	 */
967 	m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
968 #ifdef INET6
969 	if (isipv6)
970 		/*
971 		 * ip6_plen is not need to be filled now, and will be filled
972 		 * in ip6_output.
973 		 */
974 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
975 				       sizeof(struct tcphdr) + optlen + len);
976 	else
977 #endif /* INET6 */
978 	{
979 		m->m_pkthdr.csum_flags = CSUM_TCP;
980 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
981 		th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
982 		    htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
983 
984 		/* IP version must be set here for ipv4/ipv6 checking later */
985 		KASSERT(ip->ip_v == IPVERSION,
986 		    ("%s: IP version incorrect: %d", __func__, ip->ip_v));
987 	}
988 
989 	/*
990 	 * Enable TSO and specify the size of the segments.
991 	 * The TCP pseudo header checksum is always provided.
992 	 * XXX: Fixme: This is currently not the case for IPv6.
993 	 */
994 	if (tso) {
995 		m->m_pkthdr.csum_flags = CSUM_TSO;
996 		m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen;
997 	}
998 
999 	/*
1000 	 * In transmit state, time the transmission and arrange for
1001 	 * the retransmit.  In persist state, just set snd_max.
1002 	 */
1003 	if ((tp->t_flags & TF_FORCEDATA) == 0 ||
1004 	    !callout_active(tp->tt_persist)) {
1005 		tcp_seq startseq = tp->snd_nxt;
1006 
1007 		/*
1008 		 * Advance snd_nxt over sequence space of this segment.
1009 		 */
1010 		if (flags & (TH_SYN|TH_FIN)) {
1011 			if (flags & TH_SYN)
1012 				tp->snd_nxt++;
1013 			if (flags & TH_FIN) {
1014 				tp->snd_nxt++;
1015 				tp->t_flags |= TF_SENTFIN;
1016 			}
1017 		}
1018 		if (sack_rxmit)
1019 			goto timer;
1020 		tp->snd_nxt += len;
1021 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1022 			tp->snd_max = tp->snd_nxt;
1023 			/*
1024 			 * Time this transmission if not a retransmission and
1025 			 * not currently timing anything.
1026 			 */
1027 			if (tp->t_rtttime == 0) {
1028 				tp->t_rtttime = ticks;
1029 				tp->t_rtseq = startseq;
1030 				tcpstat.tcps_segstimed++;
1031 			}
1032 		}
1033 
1034 		/*
1035 		 * Set retransmit timer if not currently set,
1036 		 * and not doing a pure ack or a keep-alive probe.
1037 		 * Initial value for retransmit timer is smoothed
1038 		 * round-trip time + 2 * round-trip time variance.
1039 		 * Initialize shift counter which is used for backoff
1040 		 * of retransmit time.
1041 		 */
1042 timer:
1043 		if (!callout_active(tp->tt_rexmt) &&
1044 		    ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1045 		     (tp->snd_nxt != tp->snd_una))) {
1046 			if (callout_active(tp->tt_persist)) {
1047 				callout_stop(tp->tt_persist);
1048 				tp->t_rxtshift = 0;
1049 			}
1050 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
1051 				      tcp_timer_rexmt, tp);
1052 		}
1053 	} else {
1054 		/*
1055 		 * Persist case, update snd_max but since we are in
1056 		 * persist mode (no window) we do not update snd_nxt.
1057 		 */
1058 		int xlen = len;
1059 		if (flags & TH_SYN)
1060 			++xlen;
1061 		if (flags & TH_FIN) {
1062 			++xlen;
1063 			tp->t_flags |= TF_SENTFIN;
1064 		}
1065 		if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
1066 			tp->snd_max = tp->snd_nxt + len;
1067 	}
1068 
1069 #ifdef TCPDEBUG
1070 	/*
1071 	 * Trace.
1072 	 */
1073 	if (so->so_options & SO_DEBUG) {
1074 		u_short save = 0;
1075 #ifdef INET6
1076 		if (!isipv6)
1077 #endif
1078 		{
1079 			save = ipov->ih_len;
1080 			ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
1081 		}
1082 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
1083 #ifdef INET6
1084 		if (!isipv6)
1085 #endif
1086 		ipov->ih_len = save;
1087 	}
1088 #endif
1089 
1090 	/*
1091 	 * Fill in IP length and desired time to live and
1092 	 * send to IP level.  There should be a better way
1093 	 * to handle ttl and tos; we could keep them in
1094 	 * the template, but need a way to checksum without them.
1095 	 */
1096 	/*
1097 	 * m->m_pkthdr.len should have been set before cksum calcuration,
1098 	 * because in6_cksum() need it.
1099 	 */
1100 #ifdef INET6
1101 	if (isipv6) {
1102 		/*
1103 		 * we separately set hoplimit for every segment, since the
1104 		 * user might want to change the value via setsockopt.
1105 		 * Also, desired default hop limit might be changed via
1106 		 * Neighbor Discovery.
1107 		 */
1108 		ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1109 
1110 		/* TODO: IPv6 IP6TOS_ECT bit on */
1111 		error = ip6_output(m,
1112 			    tp->t_inpcb->in6p_outputopts, NULL,
1113 			    ((so->so_options & SO_DONTROUTE) ?
1114 			    IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb);
1115 	} else
1116 #endif /* INET6 */
1117     {
1118 	ip->ip_len = m->m_pkthdr.len;
1119 #ifdef INET6
1120 	if (INP_CHECK_SOCKAF(so, AF_INET6))
1121 		ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1122 #endif /* INET6 */
1123 	/*
1124 	 * If we do path MTU discovery, then we set DF on every packet.
1125 	 * This might not be the best thing to do according to RFC3390
1126 	 * Section 2. However the tcp hostcache migitates the problem
1127 	 * so it affects only the first tcp connection with a host.
1128 	 */
1129 	if (path_mtu_discovery)
1130 		ip->ip_off |= IP_DF;
1131 
1132 	error = ip_output(m, tp->t_inpcb->inp_options, NULL,
1133 	    ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0,
1134 	    tp->t_inpcb);
1135     }
1136 	if (error) {
1137 
1138 		/*
1139 		 * We know that the packet was lost, so back out the
1140 		 * sequence number advance, if any.
1141 		 *
1142 		 * If the error is EPERM the packet got blocked by the
1143 		 * local firewall.  Normally we should terminate the
1144 		 * connection but the blocking may have been spurious
1145 		 * due to a firewall reconfiguration cycle.  So we treat
1146 		 * it like a packet loss and let the retransmit timer and
1147 		 * timeouts do their work over time.
1148 		 * XXX: It is a POLA question whether calling tcp_drop right
1149 		 * away would be the really correct behavior instead.
1150 		 */
1151 		if (error != EPERM && ((tp->t_flags & TF_FORCEDATA) == 0 ||
1152 		    !callout_active(tp->tt_persist))) {
1153 			/*
1154 			 * No need to check for TH_FIN here because
1155 			 * the TF_SENTFIN flag handles that case.
1156 			 */
1157 			if ((flags & TH_SYN) == 0) {
1158 				if (sack_rxmit) {
1159 					p->rxmit -= len;
1160 					tp->sackhint.sack_bytes_rexmit -= len;
1161 					KASSERT(tp->sackhint.sack_bytes_rexmit
1162 						>= 0,
1163 						("sackhint bytes rtx >= 0"));
1164 				} else
1165 					tp->snd_nxt -= len;
1166 			}
1167 		}
1168 		if (error == EPERM) {
1169 			tp->t_softerror = error;
1170 			return (error);
1171 		}
1172 
1173 out:
1174 		SOCKBUF_UNLOCK_ASSERT(&so->so_snd);	/* Check gotos. */
1175 		if (error == ENOBUFS) {
1176 	                if (!callout_active(tp->tt_rexmt) &&
1177 			    !callout_active(tp->tt_persist))
1178 	                        callout_reset(tp->tt_rexmt, tp->t_rxtcur,
1179 				    tcp_timer_rexmt, tp);
1180 			tp->snd_cwnd = tp->t_maxseg;
1181 			return (0);
1182 		}
1183 		if (error == EMSGSIZE) {
1184 			/*
1185 			 * For some reason the interface we used initially
1186 			 * to send segments changed to another or lowered
1187 			 * its MTU.
1188 			 *
1189 			 * tcp_mtudisc() will find out the new MTU and as
1190 			 * its last action, initiate retransmission, so it
1191 			 * is important to not do so here.
1192 			 *
1193 			 * If TSO was active we either got an interface
1194 			 * without TSO capabilits or TSO was turned off.
1195 			 * Disable it for this connection as too and
1196 			 * immediatly retry with MSS sized segments generated
1197 			 * by this function.
1198 			 */
1199 			if (tso)
1200 				tp->t_flags &= ~TF_TSO;
1201 			tcp_mtudisc(tp->t_inpcb, 0);
1202 			return 0;
1203 		}
1204 		if ((error == EHOSTUNREACH || error == ENETDOWN)
1205 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
1206 			tp->t_softerror = error;
1207 			return (0);
1208 		}
1209 		return (error);
1210 	}
1211 	tcpstat.tcps_sndtotal++;
1212 
1213 	/*
1214 	 * Data sent (as far as we can tell).
1215 	 * If this advertises a larger window than any other segment,
1216 	 * then remember the size of the advertised window.
1217 	 * Any pending ACK has now been sent.
1218 	 */
1219 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1220 		tp->rcv_adv = tp->rcv_nxt + recwin;
1221 	tp->last_ack_sent = tp->rcv_nxt;
1222 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
1223 	if (callout_active(tp->tt_delack))
1224 		callout_stop(tp->tt_delack);
1225 #if 0
1226 	/*
1227 	 * This completely breaks TCP if newreno is turned on.  What happens
1228 	 * is that if delayed-acks are turned on on the receiver, this code
1229 	 * on the transmitter effectively destroys the TCP window, forcing
1230 	 * it to four packets (1.5Kx4 = 6K window).
1231 	 */
1232 	if (sendalot && (!tcp_do_newreno || --maxburst))
1233 		goto again;
1234 #endif
1235 	if (sendalot)
1236 		goto again;
1237 	return (0);
1238 }
1239 
1240 void
1241 tcp_setpersist(tp)
1242 	register struct tcpcb *tp;
1243 {
1244 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
1245 	int tt;
1246 
1247 	if (callout_active(tp->tt_rexmt))
1248 		panic("tcp_setpersist: retransmit pending");
1249 	/*
1250 	 * Start/restart persistance timer.
1251 	 */
1252 	TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1253 		      TCPTV_PERSMIN, TCPTV_PERSMAX);
1254 	callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp);
1255 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1256 		tp->t_rxtshift++;
1257 }
1258