xref: /freebsd/sys/netinet/tcp_output.c (revision 3bfd6421c2ae8aca85759b11c187f5b8a35089d9)
1df8bae1dSRodney W. Grimes /*
2d7f570e6SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33d7f570e6SGarrett Wollman  *	@(#)tcp_output.c	8.4 (Berkeley) 5/24/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37fb59c426SYoshinobu Inoue #include "opt_inet6.h"
383a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
39c488362eSRobert Watson #include "opt_mac.h"
400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
410cc12cc5SJoerg Wunsch 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h>
45fb919e4dSMark Murray #include <sys/kernel.h>
46fb919e4dSMark Murray #include <sys/lock.h>
47c488362eSRobert Watson #include <sys/mac.h>
48fb919e4dSMark Murray #include <sys/mbuf.h>
49fb919e4dSMark Murray #include <sys/mutex.h>
50df8bae1dSRodney W. Grimes #include <sys/protosw.h>
51df8bae1dSRodney W. Grimes #include <sys/socket.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
53fb919e4dSMark Murray #include <sys/sysctl.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <net/route.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <netinet/in.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
61df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
62fb59c426SYoshinobu Inoue #ifdef INET6
63686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
64686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
65fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
66fb59c426SYoshinobu Inoue #endif
67df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
68df8bae1dSRodney W. Grimes #define	TCPOUTFLAGS
69df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
70df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
71df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
72df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
73df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
74610ee2f9SDavid Greenman #ifdef TCPDEBUG
75df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
76610ee2f9SDavid Greenman #endif
77df8bae1dSRodney W. Grimes 
783a2a9f79SYoshinobu Inoue #ifdef IPSEC
793a2a9f79SYoshinobu Inoue #include <netinet6/ipsec.h>
803a2a9f79SYoshinobu Inoue #endif /*IPSEC*/
813a2a9f79SYoshinobu Inoue 
82b9234fafSSam Leffler #ifdef FAST_IPSEC
83b9234fafSSam Leffler #include <netipsec/ipsec.h>
84b9234fafSSam Leffler #define	IPSEC
85b9234fafSSam Leffler #endif /*FAST_IPSEC*/
86b9234fafSSam Leffler 
87db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
88db4f9cc7SJonathan Lemon 
89df8bae1dSRodney W. Grimes #ifdef notyet
90df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack();
91df8bae1dSRodney W. Grimes #endif
92df8bae1dSRodney W. Grimes 
93eb5afebaSMike Silbersack int path_mtu_discovery = 1;
949a039a5fSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW,
959a039a5fSDavid Greenman 	&path_mtu_discovery, 1, "Enable Path MTU Discovery");
969a039a5fSDavid Greenman 
979b8b58e0SJonathan Lemon int ss_fltsz = 1;
989b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW,
999b8b58e0SJonathan Lemon 	&ss_fltsz, 1, "Slow start flight size");
1009b8b58e0SJonathan Lemon 
1013260eb18SMike Silbersack int ss_fltsz_local = 4;
1029b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW,
1039b8b58e0SJonathan Lemon 	&ss_fltsz_local, 1, "Slow start flight size for local networks");
104df8bae1dSRodney W. Grimes 
1057d200109SJayanth Vijayaraghavan int     tcp_do_newreno = 1;
10646f58482SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno,
10746f58482SJonathan Lemon         0, "Enable NewReno Algorithms");
108314e5a3dSJeffrey Hsu 
109df8bae1dSRodney W. Grimes /*
110df8bae1dSRodney W. Grimes  * Tcp output routine: figure out what should be sent and send it.
111df8bae1dSRodney W. Grimes  */
112df8bae1dSRodney W. Grimes int
113f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp)
114df8bae1dSRodney W. Grimes {
115f10e85d7SLuigi Rizzo 	struct socket *so = tp->t_inpcb->inp_socket;
116f10e85d7SLuigi Rizzo 	long len, win;
117df8bae1dSRodney W. Grimes 	int off, flags, error;
118f10e85d7SLuigi Rizzo 	struct mbuf *m;
119fb59c426SYoshinobu Inoue 	struct ip *ip = NULL;
120f10e85d7SLuigi Rizzo 	struct ipovly *ipov = NULL;
121f10e85d7SLuigi Rizzo 	struct tcphdr *th;
122a0292f23SGarrett Wollman 	u_char opt[TCP_MAXOLEN];
123a04884fcSBill Fenner 	unsigned ipoptlen, optlen, hdrlen;
124df8bae1dSRodney W. Grimes 	int idle, sendalot;
125262c1c1aSMatthew Dillon #if 0
12646f58482SJonathan Lemon 	int maxburst = TCP_MAXBURST;
127262c1c1aSMatthew Dillon #endif
128a0292f23SGarrett Wollman 	struct rmxp_tao *taop;
129fb59c426SYoshinobu Inoue #ifdef INET6
130f10e85d7SLuigi Rizzo 	struct ip6_hdr *ip6 = NULL;
131fb59c426SYoshinobu Inoue 	int isipv6;
132fb59c426SYoshinobu Inoue 
133fb59c426SYoshinobu Inoue 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
134fb59c426SYoshinobu Inoue #endif
135df8bae1dSRodney W. Grimes 
1364a03a8a8SJennifer Yang #ifndef INET6
1373d6ade3aSJennifer Yang 	mtx_assert(&tp->t_inpcb->inp_mtx, MA_OWNED);
1384a03a8a8SJennifer Yang #endif
1393d6ade3aSJennifer Yang 
140df8bae1dSRodney W. Grimes 	/*
141df8bae1dSRodney W. Grimes 	 * Determine length of data that should be transmitted,
142df8bae1dSRodney W. Grimes 	 * and flags that will be used.
143df8bae1dSRodney W. Grimes 	 * If there is some data or critical controls (SYN, RST)
144df8bae1dSRodney W. Grimes 	 * to send, then transmit; otherwise, investigate further.
145df8bae1dSRodney W. Grimes 	 */
146c24d5daeSJayanth Vijayaraghavan 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
1479b8b58e0SJonathan Lemon 	if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
148df8bae1dSRodney W. Grimes 		/*
149df8bae1dSRodney W. Grimes 		 * We have been idle for "a while" and no acks are
150df8bae1dSRodney W. Grimes 		 * expected to clock out any data we send --
151df8bae1dSRodney W. Grimes 		 * slow start to get ack "clock" running again.
1529b8b58e0SJonathan Lemon 		 *
1539b8b58e0SJonathan Lemon 		 * Set the slow-start flight size depending on whether
1549b8b58e0SJonathan Lemon 		 * this is a local network or not.
155df8bae1dSRodney W. Grimes 		 */
156f10e85d7SLuigi Rizzo 		int ss = ss_fltsz;
157fb59c426SYoshinobu Inoue #ifdef INET6
158f10e85d7SLuigi Rizzo 		if (isipv6) {
159f10e85d7SLuigi Rizzo 			if (in6_localaddr(&tp->t_inpcb->in6p_faddr))
160f10e85d7SLuigi Rizzo 				ss = ss_fltsz_local;
161f10e85d7SLuigi Rizzo 		} else
162f10e85d7SLuigi Rizzo #endif /* INET6 */
163f10e85d7SLuigi Rizzo 		if (in_localaddr(tp->t_inpcb->inp_faddr))
164f10e85d7SLuigi Rizzo 			ss = ss_fltsz_local;
165f10e85d7SLuigi Rizzo 		tp->snd_cwnd = tp->t_maxseg * ss;
1669b8b58e0SJonathan Lemon 	}
167c24d5daeSJayanth Vijayaraghavan 	tp->t_flags &= ~TF_LASTIDLE;
168c24d5daeSJayanth Vijayaraghavan 	if (idle) {
169c24d5daeSJayanth Vijayaraghavan 		if (tp->t_flags & TF_MORETOCOME) {
170c24d5daeSJayanth Vijayaraghavan 			tp->t_flags |= TF_LASTIDLE;
171c24d5daeSJayanth Vijayaraghavan 			idle = 0;
172c24d5daeSJayanth Vijayaraghavan 		}
173c24d5daeSJayanth Vijayaraghavan 	}
174df8bae1dSRodney W. Grimes again:
175df8bae1dSRodney W. Grimes 	sendalot = 0;
176df8bae1dSRodney W. Grimes 	off = tp->snd_nxt - tp->snd_una;
177df8bae1dSRodney W. Grimes 	win = min(tp->snd_wnd, tp->snd_cwnd);
1781fcc99b5SMatthew Dillon 	win = min(win, tp->snd_bwnd);
179df8bae1dSRodney W. Grimes 
180df8bae1dSRodney W. Grimes 	flags = tcp_outflags[tp->t_state];
181a0292f23SGarrett Wollman 	/*
182a0292f23SGarrett Wollman 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
183a0292f23SGarrett Wollman 	 * state flags.
184a0292f23SGarrett Wollman 	 */
185a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDFIN)
186a0292f23SGarrett Wollman 		flags |= TH_FIN;
187a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDSYN)
188a0292f23SGarrett Wollman 		flags |= TH_SYN;
189a0292f23SGarrett Wollman 
190df8bae1dSRodney W. Grimes 	/*
191df8bae1dSRodney W. Grimes 	 * If in persist timeout with window of 0, send 1 byte.
192df8bae1dSRodney W. Grimes 	 * Otherwise, if window is small but nonzero
193df8bae1dSRodney W. Grimes 	 * and timer expired, we will send what we can
194df8bae1dSRodney W. Grimes 	 * and go to transmit state.
195df8bae1dSRodney W. Grimes 	 */
196df8bae1dSRodney W. Grimes 	if (tp->t_force) {
197df8bae1dSRodney W. Grimes 		if (win == 0) {
198df8bae1dSRodney W. Grimes 			/*
199df8bae1dSRodney W. Grimes 			 * If we still have some data to send, then
200df8bae1dSRodney W. Grimes 			 * clear the FIN bit.  Usually this would
201df8bae1dSRodney W. Grimes 			 * happen below when it realizes that we
202df8bae1dSRodney W. Grimes 			 * aren't sending all the data.  However,
20329089b51SJulian Elischer 			 * if we have exactly 1 byte of unsent data,
204df8bae1dSRodney W. Grimes 			 * then it won't clear the FIN bit below,
205df8bae1dSRodney W. Grimes 			 * and if we are in persist state, we wind
206df8bae1dSRodney W. Grimes 			 * up sending the packet without recording
207df8bae1dSRodney W. Grimes 			 * that we sent the FIN bit.
208df8bae1dSRodney W. Grimes 			 *
209df8bae1dSRodney W. Grimes 			 * We can't just blindly clear the FIN bit,
210df8bae1dSRodney W. Grimes 			 * because if we don't have any more data
211df8bae1dSRodney W. Grimes 			 * to send then the probe will be the FIN
212df8bae1dSRodney W. Grimes 			 * itself.
213df8bae1dSRodney W. Grimes 			 */
214df8bae1dSRodney W. Grimes 			if (off < so->so_snd.sb_cc)
215df8bae1dSRodney W. Grimes 				flags &= ~TH_FIN;
216df8bae1dSRodney W. Grimes 			win = 1;
217df8bae1dSRodney W. Grimes 		} else {
2189b8b58e0SJonathan Lemon 			callout_stop(tp->tt_persist);
219df8bae1dSRodney W. Grimes 			tp->t_rxtshift = 0;
220df8bae1dSRodney W. Grimes 		}
221df8bae1dSRodney W. Grimes 	}
222df8bae1dSRodney W. Grimes 
22328257b5cSMatthew Dillon 	/*
22428257b5cSMatthew Dillon 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
22528257b5cSMatthew Dillon 	 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
22628257b5cSMatthew Dillon 	 * a negative length.  This can also occur when tcp opens up
22728257b5cSMatthew Dillon 	 * its congestion window while receiving additional duplicate
22828257b5cSMatthew Dillon 	 * acks after fast-retransmit because TCP will reset snd_nxt
22928257b5cSMatthew Dillon 	 * to snd_max after the fast-retransmit.
23028257b5cSMatthew Dillon 	 *
23128257b5cSMatthew Dillon 	 * In the normal retransmit-FIN-only case, however, snd_nxt will
23228257b5cSMatthew Dillon 	 * be set to snd_una, the offset will be 0, and the length may
23328257b5cSMatthew Dillon 	 * wind up 0.
23428257b5cSMatthew Dillon 	 */
2359105bb46SBruce Evans 	len = (long)ulmin(so->so_snd.sb_cc, win) - off;
236df8bae1dSRodney W. Grimes 
237314e5a3dSJeffrey Hsu 	taop = tcp_gettaocache(&tp->t_inpcb->inp_inc);
238a0292f23SGarrett Wollman 
239a0292f23SGarrett Wollman 	/*
240a0292f23SGarrett Wollman 	 * Lop off SYN bit if it has already been sent.  However, if this
241a0292f23SGarrett Wollman 	 * is SYN-SENT state and if segment contains data and if we don't
242a0292f23SGarrett Wollman 	 * know that foreign host supports TAO, suppress sending segment.
243a0292f23SGarrett Wollman 	 */
244a0292f23SGarrett Wollman 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
245a0292f23SGarrett Wollman 		flags &= ~TH_SYN;
246a0292f23SGarrett Wollman 		off--, len++;
247a0292f23SGarrett Wollman 		if (len > 0 && tp->t_state == TCPS_SYN_SENT &&
248314e5a3dSJeffrey Hsu 		    (taop == NULL || taop->tao_ccsent == 0))
249a0292f23SGarrett Wollman 			return 0;
250a0292f23SGarrett Wollman 	}
251a0292f23SGarrett Wollman 
25281165e48SAndras Olah 	/*
25381165e48SAndras Olah 	 * Be careful not to send data and/or FIN on SYN segments
25481165e48SAndras Olah 	 * in cases when no CC option will be sent.
25581165e48SAndras Olah 	 * This measure is needed to prevent interoperability problems
25681165e48SAndras Olah 	 * with not fully conformant TCP implementations.
25781165e48SAndras Olah 	 */
25881165e48SAndras Olah 	if ((flags & TH_SYN) &&
25981165e48SAndras Olah 	    ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) ||
26081165e48SAndras Olah 	     ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) {
26181165e48SAndras Olah 		len = 0;
26281165e48SAndras Olah 		flags &= ~TH_FIN;
26381165e48SAndras Olah 	}
26481165e48SAndras Olah 
265df8bae1dSRodney W. Grimes 	if (len < 0) {
266df8bae1dSRodney W. Grimes 		/*
267df8bae1dSRodney W. Grimes 		 * If FIN has been sent but not acked,
268df8bae1dSRodney W. Grimes 		 * but we haven't been called to retransmit,
26928257b5cSMatthew Dillon 		 * len will be < 0.  Otherwise, window shrank
270df8bae1dSRodney W. Grimes 		 * after we sent into it.  If window shrank to 0,
2712d8266afSDavid Greenman 		 * cancel pending retransmit, pull snd_nxt back
2722d8266afSDavid Greenman 		 * to (closed) window, and set the persist timer
2732d8266afSDavid Greenman 		 * if it isn't already going.  If the window didn't
2742d8266afSDavid Greenman 		 * close completely, just wait for an ACK.
275df8bae1dSRodney W. Grimes 		 */
276df8bae1dSRodney W. Grimes 		len = 0;
277df8bae1dSRodney W. Grimes 		if (win == 0) {
2789b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
2792d8266afSDavid Greenman 			tp->t_rxtshift = 0;
280df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2819b8b58e0SJonathan Lemon 			if (!callout_active(tp->tt_persist))
2822d8266afSDavid Greenman 				tcp_setpersist(tp);
283df8bae1dSRodney W. Grimes 		}
284df8bae1dSRodney W. Grimes 	}
28528257b5cSMatthew Dillon 
28628257b5cSMatthew Dillon 	/*
28728257b5cSMatthew Dillon 	 * len will be >= 0 after this point.  Truncate to the maximum
28828257b5cSMatthew Dillon 	 * segment length and ensure that FIN is removed if the length
28928257b5cSMatthew Dillon 	 * no longer contains the last data byte.
29028257b5cSMatthew Dillon 	 */
291df8bae1dSRodney W. Grimes 	if (len > tp->t_maxseg) {
292df8bae1dSRodney W. Grimes 		len = tp->t_maxseg;
293df8bae1dSRodney W. Grimes 		sendalot = 1;
294df8bae1dSRodney W. Grimes 	}
295df8bae1dSRodney W. Grimes 	if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
296df8bae1dSRodney W. Grimes 		flags &= ~TH_FIN;
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes 	/*
301262c1c1aSMatthew Dillon 	 * Sender silly window avoidance.   We transmit under the following
302262c1c1aSMatthew Dillon 	 * conditions when len is non-zero:
303262c1c1aSMatthew Dillon 	 *
304262c1c1aSMatthew Dillon 	 *	- We have a full segment
305262c1c1aSMatthew Dillon 	 *	- This is the last buffer in a write()/send() and we are
306262c1c1aSMatthew Dillon 	 *	  either idle or running NODELAY
307262c1c1aSMatthew Dillon 	 *	- we've timed out (e.g. persist timer)
308262c1c1aSMatthew Dillon 	 *	- we have more then 1/2 the maximum send window's worth of
309262c1c1aSMatthew Dillon 	 *	  data (receiver may be limited the window size)
310262c1c1aSMatthew Dillon 	 *	- we need to retransmit
311df8bae1dSRodney W. Grimes 	 */
312df8bae1dSRodney W. Grimes 	if (len) {
313df8bae1dSRodney W. Grimes 		if (len == tp->t_maxseg)
314df8bae1dSRodney W. Grimes 			goto send;
315262c1c1aSMatthew Dillon 		/*
316262c1c1aSMatthew Dillon 		 * NOTE! on localhost connections an 'ack' from the remote
317262c1c1aSMatthew Dillon 		 * end may occur synchronously with the output and cause
318262c1c1aSMatthew Dillon 		 * us to flush a buffer queued with moretocome.  XXX
319262c1c1aSMatthew Dillon 		 *
320262c1c1aSMatthew Dillon 		 * note: the len + off check is almost certainly unnecessary.
321262c1c1aSMatthew Dillon 		 */
322262c1c1aSMatthew Dillon 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
323262c1c1aSMatthew Dillon 		    (idle || (tp->t_flags & TF_NODELAY)) &&
324262c1c1aSMatthew Dillon 		    len + off >= so->so_snd.sb_cc &&
325262c1c1aSMatthew Dillon 		    (tp->t_flags & TF_NOPUSH) == 0) {
326df8bae1dSRodney W. Grimes 			goto send;
327262c1c1aSMatthew Dillon 		}
328262c1c1aSMatthew Dillon 		if (tp->t_force)			/* typ. timeout case */
329df8bae1dSRodney W. Grimes 			goto send;
330a0292f23SGarrett Wollman 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
331df8bae1dSRodney W. Grimes 			goto send;
332262c1c1aSMatthew Dillon 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))	/* retransmit case */
333df8bae1dSRodney W. Grimes 			goto send;
334df8bae1dSRodney W. Grimes 	}
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	/*
337df8bae1dSRodney W. Grimes 	 * Compare available window to amount of window
338df8bae1dSRodney W. Grimes 	 * known to peer (as advertised window less
339df8bae1dSRodney W. Grimes 	 * next expected input).  If the difference is at least two
340df8bae1dSRodney W. Grimes 	 * max size segments, or at least 50% of the maximum possible
341df8bae1dSRodney W. Grimes 	 * window, then want to send a window update to peer.
3423bfd6421SJonathan Lemon 	 * Skip this if the connection is in T/TCP half-open state.
343df8bae1dSRodney W. Grimes 	 */
3443bfd6421SJonathan Lemon 	if (win > 0 && !(tp->t_flags & TF_NEEDSYN)) {
345df8bae1dSRodney W. Grimes 		/*
346df8bae1dSRodney W. Grimes 		 * "adv" is the amount we can increase the window,
347df8bae1dSRodney W. Grimes 		 * taking into account that we are limited by
348df8bae1dSRodney W. Grimes 		 * TCP_MAXWIN << tp->rcv_scale.
349df8bae1dSRodney W. Grimes 		 */
350df8bae1dSRodney W. Grimes 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
351df8bae1dSRodney W. Grimes 			(tp->rcv_adv - tp->rcv_nxt);
352df8bae1dSRodney W. Grimes 
353df8bae1dSRodney W. Grimes 		if (adv >= (long) (2 * tp->t_maxseg))
354df8bae1dSRodney W. Grimes 			goto send;
355df8bae1dSRodney W. Grimes 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
356df8bae1dSRodney W. Grimes 			goto send;
357df8bae1dSRodney W. Grimes 	}
358df8bae1dSRodney W. Grimes 
359df8bae1dSRodney W. Grimes 	/*
36028257b5cSMatthew Dillon 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
36128257b5cSMatthew Dillon 	 * is also a catch-all for the retransmit timer timeout case.
362df8bae1dSRodney W. Grimes 	 */
363df8bae1dSRodney W. Grimes 	if (tp->t_flags & TF_ACKNOW)
364df8bae1dSRodney W. Grimes 		goto send;
365a0292f23SGarrett Wollman 	if ((flags & TH_RST) ||
366a0292f23SGarrett Wollman 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
367df8bae1dSRodney W. Grimes 		goto send;
368df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_una))
369df8bae1dSRodney W. Grimes 		goto send;
370df8bae1dSRodney W. Grimes 	/*
371df8bae1dSRodney W. Grimes 	 * If our state indicates that FIN should be sent
37228257b5cSMatthew Dillon 	 * and we have not yet done so, then we need to send.
373df8bae1dSRodney W. Grimes 	 */
374df8bae1dSRodney W. Grimes 	if (flags & TH_FIN &&
375df8bae1dSRodney W. Grimes 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
376df8bae1dSRodney W. Grimes 		goto send;
377df8bae1dSRodney W. Grimes 
378df8bae1dSRodney W. Grimes 	/*
379df8bae1dSRodney W. Grimes 	 * TCP window updates are not reliable, rather a polling protocol
380df8bae1dSRodney W. Grimes 	 * using ``persist'' packets is used to insure receipt of window
381df8bae1dSRodney W. Grimes 	 * updates.  The three ``states'' for the output side are:
382df8bae1dSRodney W. Grimes 	 *	idle			not doing retransmits or persists
383df8bae1dSRodney W. Grimes 	 *	persisting		to move a small or zero window
384df8bae1dSRodney W. Grimes 	 *	(re)transmitting	and thereby not persisting
385df8bae1dSRodney W. Grimes 	 *
3869b8b58e0SJonathan Lemon 	 * callout_active(tp->tt_persist)
3879b8b58e0SJonathan Lemon 	 *	is true when we are in persist state.
388df8bae1dSRodney W. Grimes 	 * tp->t_force
389df8bae1dSRodney W. Grimes 	 *	is set when we are called to send a persist packet.
3909b8b58e0SJonathan Lemon 	 * callout_active(tp->tt_rexmt)
391df8bae1dSRodney W. Grimes 	 *	is set when we are retransmitting
392df8bae1dSRodney W. Grimes 	 * The output side is idle when both timers are zero.
393df8bae1dSRodney W. Grimes 	 *
394df8bae1dSRodney W. Grimes 	 * If send window is too small, there is data to transmit, and no
395df8bae1dSRodney W. Grimes 	 * retransmit or persist is pending, then go to persist state.
396df8bae1dSRodney W. Grimes 	 * If nothing happens soon, send when timer expires:
397df8bae1dSRodney W. Grimes 	 * if window is nonzero, transmit what we can,
398df8bae1dSRodney W. Grimes 	 * otherwise force out a byte.
399df8bae1dSRodney W. Grimes 	 */
4009b8b58e0SJonathan Lemon 	if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) &&
4019b8b58e0SJonathan Lemon 	    !callout_active(tp->tt_persist)) {
402df8bae1dSRodney W. Grimes 		tp->t_rxtshift = 0;
403df8bae1dSRodney W. Grimes 		tcp_setpersist(tp);
404df8bae1dSRodney W. Grimes 	}
405df8bae1dSRodney W. Grimes 
406df8bae1dSRodney W. Grimes 	/*
407df8bae1dSRodney W. Grimes 	 * No reason to send a segment, just return.
408df8bae1dSRodney W. Grimes 	 */
409df8bae1dSRodney W. Grimes 	return (0);
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes send:
412df8bae1dSRodney W. Grimes 	/*
413df8bae1dSRodney W. Grimes 	 * Before ESTABLISHED, force sending of initial options
414df8bae1dSRodney W. Grimes 	 * unless TCP set not to do any options.
415df8bae1dSRodney W. Grimes 	 * NOTE: we assume that the IP/TCP header plus TCP options
416df8bae1dSRodney W. Grimes 	 * always fit in a single mbuf, leaving room for a maximum
417df8bae1dSRodney W. Grimes 	 * link header, i.e.
41833841545SHajimu UMEMOTO 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
419df8bae1dSRodney W. Grimes 	 */
420df8bae1dSRodney W. Grimes 	optlen = 0;
421fb59c426SYoshinobu Inoue #ifdef INET6
422fb59c426SYoshinobu Inoue 	if (isipv6)
423fb59c426SYoshinobu Inoue 		hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
424fb59c426SYoshinobu Inoue 	else
425fb59c426SYoshinobu Inoue #endif
426df8bae1dSRodney W. Grimes 	hdrlen = sizeof (struct tcpiphdr);
427df8bae1dSRodney W. Grimes 	if (flags & TH_SYN) {
428df8bae1dSRodney W. Grimes 		tp->snd_nxt = tp->iss;
429df8bae1dSRodney W. Grimes 		if ((tp->t_flags & TF_NOOPT) == 0) {
430df8bae1dSRodney W. Grimes 			u_short mss;
431df8bae1dSRodney W. Grimes 
432df8bae1dSRodney W. Grimes 			opt[0] = TCPOPT_MAXSEG;
433a0292f23SGarrett Wollman 			opt[1] = TCPOLEN_MAXSEG;
434a0292f23SGarrett Wollman 			mss = htons((u_short) tcp_mssopt(tp));
43594a5d9b6SDavid Greenman 			(void)memcpy(opt + 2, &mss, sizeof(mss));
436a0292f23SGarrett Wollman 			optlen = TCPOLEN_MAXSEG;
437df8bae1dSRodney W. Grimes 
438df8bae1dSRodney W. Grimes 			if ((tp->t_flags & TF_REQ_SCALE) &&
439df8bae1dSRodney W. Grimes 			    ((flags & TH_ACK) == 0 ||
440df8bae1dSRodney W. Grimes 			    (tp->t_flags & TF_RCVD_SCALE))) {
4419105bb46SBruce Evans 				*((u_int32_t *)(opt + optlen)) = htonl(
442df8bae1dSRodney W. Grimes 					TCPOPT_NOP << 24 |
443df8bae1dSRodney W. Grimes 					TCPOPT_WINDOW << 16 |
444df8bae1dSRodney W. Grimes 					TCPOLEN_WINDOW << 8 |
445df8bae1dSRodney W. Grimes 					tp->request_r_scale);
446df8bae1dSRodney W. Grimes 				optlen += 4;
447df8bae1dSRodney W. Grimes 			}
448df8bae1dSRodney W. Grimes 		}
449df8bae1dSRodney W. Grimes  	}
450df8bae1dSRodney W. Grimes 
451df8bae1dSRodney W. Grimes  	/*
452df8bae1dSRodney W. Grimes 	 * Send a timestamp and echo-reply if this is a SYN and our side
453df8bae1dSRodney W. Grimes 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
454df8bae1dSRodney W. Grimes 	 * and our peer have sent timestamps in our SYN's.
455df8bae1dSRodney W. Grimes  	 */
456df8bae1dSRodney W. Grimes  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
457df8bae1dSRodney W. Grimes  	    (flags & TH_RST) == 0 &&
458a0292f23SGarrett Wollman 	    ((flags & TH_ACK) == 0 ||
459df8bae1dSRodney W. Grimes 	     (tp->t_flags & TF_RCVD_TSTMP))) {
4609105bb46SBruce Evans 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
461df8bae1dSRodney W. Grimes 
462df8bae1dSRodney W. Grimes  		/* Form timestamp option as shown in appendix A of RFC 1323. */
463df8bae1dSRodney W. Grimes  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
4649b8b58e0SJonathan Lemon  		*lp++ = htonl(ticks);
465df8bae1dSRodney W. Grimes  		*lp   = htonl(tp->ts_recent);
466df8bae1dSRodney W. Grimes  		optlen += TCPOLEN_TSTAMP_APPA;
467df8bae1dSRodney W. Grimes  	}
468df8bae1dSRodney W. Grimes 
469a0292f23SGarrett Wollman  	/*
470a0292f23SGarrett Wollman 	 * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
471a0292f23SGarrett Wollman 	 * options are allowed (!TF_NOOPT) and it's not a RST.
472a0292f23SGarrett Wollman  	 */
473a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
474a0292f23SGarrett Wollman  	     (flags & TH_RST) == 0) {
475a0292f23SGarrett Wollman 		switch (flags & (TH_SYN|TH_ACK)) {
476a0292f23SGarrett Wollman 		/*
477a0292f23SGarrett Wollman 		 * This is a normal ACK, send CC if we received CC before
478a0292f23SGarrett Wollman 		 * from our peer.
479a0292f23SGarrett Wollman 		 */
480a0292f23SGarrett Wollman 		case TH_ACK:
481a0292f23SGarrett Wollman 			if (!(tp->t_flags & TF_RCVD_CC))
482a0292f23SGarrett Wollman 				break;
483a0292f23SGarrett Wollman 			/*FALLTHROUGH*/
484a0292f23SGarrett Wollman 
485a0292f23SGarrett Wollman 		/*
486a0292f23SGarrett Wollman 		 * We can only get here in T/TCP's SYN_SENT* state, when
487a0292f23SGarrett Wollman 		 * we're a sending a non-SYN segment without waiting for
488a0292f23SGarrett Wollman 		 * the ACK of our SYN.  A check above assures that we only
489a0292f23SGarrett Wollman 		 * do this if our peer understands T/TCP.
490a0292f23SGarrett Wollman 		 */
491a0292f23SGarrett Wollman 		case 0:
492a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
493a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
494a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_CC;
495a0292f23SGarrett Wollman 			opt[optlen++] = TCPOLEN_CC;
496a0292f23SGarrett Wollman 			*(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
497a0292f23SGarrett Wollman 
498a0292f23SGarrett Wollman 			optlen += 4;
499a0292f23SGarrett Wollman 			break;
500a0292f23SGarrett Wollman 
501a0292f23SGarrett Wollman 		/*
502a0292f23SGarrett Wollman 		 * This is our initial SYN, check whether we have to use
503a0292f23SGarrett Wollman 		 * CC or CC.new.
504a0292f23SGarrett Wollman 		 */
505a0292f23SGarrett Wollman 		case TH_SYN:
506a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
507a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
508a45d2726SAndras Olah 			opt[optlen++] = tp->t_flags & TF_SENDCCNEW ?
509a45d2726SAndras Olah 						TCPOPT_CCNEW : TCPOPT_CC;
510a0292f23SGarrett Wollman 			opt[optlen++] = TCPOLEN_CC;
511a0292f23SGarrett Wollman 			*(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
512a0292f23SGarrett Wollman  			optlen += 4;
513a0292f23SGarrett Wollman 			break;
514a0292f23SGarrett Wollman 
515a0292f23SGarrett Wollman 		/*
516a0292f23SGarrett Wollman 		 * This is a SYN,ACK; send CC and CC.echo if we received
517a0292f23SGarrett Wollman 		 * CC from our peer.
518a0292f23SGarrett Wollman 		 */
519a0292f23SGarrett Wollman 		case (TH_SYN|TH_ACK):
520a0292f23SGarrett Wollman 			if (tp->t_flags & TF_RCVD_CC) {
521a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
522a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
523a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_CC;
524a0292f23SGarrett Wollman 				opt[optlen++] = TCPOLEN_CC;
525a0292f23SGarrett Wollman 				*(u_int32_t *)&opt[optlen] =
526a0292f23SGarrett Wollman 					htonl(tp->cc_send);
527a0292f23SGarrett Wollman 				optlen += 4;
528a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
529a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
530a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_CCECHO;
531a0292f23SGarrett Wollman 				opt[optlen++] = TCPOLEN_CC;
532a0292f23SGarrett Wollman 				*(u_int32_t *)&opt[optlen] =
533a0292f23SGarrett Wollman 					htonl(tp->cc_recv);
534a0292f23SGarrett Wollman 				optlen += 4;
535a0292f23SGarrett Wollman 			}
536a0292f23SGarrett Wollman 			break;
537a0292f23SGarrett Wollman 		}
538a0292f23SGarrett Wollman  	}
539a0292f23SGarrett Wollman 
540df8bae1dSRodney W. Grimes  	hdrlen += optlen;
541df8bae1dSRodney W. Grimes 
542fb59c426SYoshinobu Inoue #ifdef INET6
543fb59c426SYoshinobu Inoue 	if (isipv6)
544fb59c426SYoshinobu Inoue 		ipoptlen = ip6_optlen(tp->t_inpcb);
545fb59c426SYoshinobu Inoue 	else
546fb59c426SYoshinobu Inoue #endif
547f10e85d7SLuigi Rizzo 	if (tp->t_inpcb->inp_options)
548a04884fcSBill Fenner 		ipoptlen = tp->t_inpcb->inp_options->m_len -
549a04884fcSBill Fenner 				offsetof(struct ipoption, ipopt_list);
550f10e85d7SLuigi Rizzo 	else
551a04884fcSBill Fenner 		ipoptlen = 0;
552fb59c426SYoshinobu Inoue #ifdef IPSEC
553fb59c426SYoshinobu Inoue 	ipoptlen += ipsec_hdrsiz_tcp(tp);
554fb59c426SYoshinobu Inoue #endif
555a04884fcSBill Fenner 
556df8bae1dSRodney W. Grimes 	/*
557df8bae1dSRodney W. Grimes 	 * Adjust data length if insertion of options will
558a0292f23SGarrett Wollman 	 * bump the packet length beyond the t_maxopd length.
559a0292f23SGarrett Wollman 	 * Clear the FIN bit because we cut off the tail of
560a0292f23SGarrett Wollman 	 * the segment.
561df8bae1dSRodney W. Grimes 	 */
562a04884fcSBill Fenner 	if (len + optlen + ipoptlen > tp->t_maxopd) {
563297a37f3SDavid Greenman 		/*
564297a37f3SDavid Greenman 		 * If there is still more to send, don't close the connection.
565297a37f3SDavid Greenman 		 */
566297a37f3SDavid Greenman 		flags &= ~TH_FIN;
567a04884fcSBill Fenner 		len = tp->t_maxopd - optlen - ipoptlen;
568df8bae1dSRodney W. Grimes 		sendalot = 1;
569df8bae1dSRodney W. Grimes 	}
570df8bae1dSRodney W. Grimes 
571a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/
572a683a7ddSYoshinobu Inoue #ifdef INET6
573a683a7ddSYoshinobu Inoue  	if (max_linkhdr + hdrlen > MCLBYTES)
574a683a7ddSYoshinobu Inoue #else
575df8bae1dSRodney W. Grimes  	if (max_linkhdr + hdrlen > MHLEN)
576a683a7ddSYoshinobu Inoue #endif
577f10e85d7SLuigi Rizzo 		panic("tcphdr too big");
578a0292f23SGarrett Wollman /*#endif*/
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes 	/*
581df8bae1dSRodney W. Grimes 	 * Grab a header mbuf, attaching a copy of data to
582df8bae1dSRodney W. Grimes 	 * be transmitted, and initialize the header from
583df8bae1dSRodney W. Grimes 	 * the template for sends on this connection.
584df8bae1dSRodney W. Grimes 	 */
585df8bae1dSRodney W. Grimes 	if (len) {
586df8bae1dSRodney W. Grimes 		if (tp->t_force && len == 1)
587df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndprobe++;
588df8bae1dSRodney W. Grimes 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
589df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitpack++;
590df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitbyte += len;
591df8bae1dSRodney W. Grimes 		} else {
592df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndpack++;
593df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndbyte += len;
594df8bae1dSRodney W. Grimes 		}
595df8bae1dSRodney W. Grimes #ifdef notyet
596df8bae1dSRodney W. Grimes 		if ((m = m_copypack(so->so_snd.sb_mb, off,
597df8bae1dSRodney W. Grimes 		    (int)len, max_linkhdr + hdrlen)) == 0) {
598df8bae1dSRodney W. Grimes 			error = ENOBUFS;
599df8bae1dSRodney W. Grimes 			goto out;
600df8bae1dSRodney W. Grimes 		}
601df8bae1dSRodney W. Grimes 		/*
602df8bae1dSRodney W. Grimes 		 * m_copypack left space for our hdr; use it.
603df8bae1dSRodney W. Grimes 		 */
604df8bae1dSRodney W. Grimes 		m->m_len += hdrlen;
605df8bae1dSRodney W. Grimes 		m->m_data -= hdrlen;
606df8bae1dSRodney W. Grimes #else
607a163d034SWarner Losh 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
608df8bae1dSRodney W. Grimes 		if (m == NULL) {
609df8bae1dSRodney W. Grimes 			error = ENOBUFS;
610df8bae1dSRodney W. Grimes 			goto out;
611df8bae1dSRodney W. Grimes 		}
612fb59c426SYoshinobu Inoue #ifdef INET6
613a683a7ddSYoshinobu Inoue 		if (MHLEN < hdrlen + max_linkhdr) {
614a163d034SWarner Losh 			MCLGET(m, M_DONTWAIT);
615a683a7ddSYoshinobu Inoue 			if ((m->m_flags & M_EXT) == 0) {
616a683a7ddSYoshinobu Inoue 				m_freem(m);
617a683a7ddSYoshinobu Inoue 				error = ENOBUFS;
618a683a7ddSYoshinobu Inoue 				goto out;
619a683a7ddSYoshinobu Inoue 			}
620a683a7ddSYoshinobu Inoue 		}
621fb59c426SYoshinobu Inoue #endif
622df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
623df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
624df8bae1dSRodney W. Grimes 		if (len <= MHLEN - hdrlen - max_linkhdr) {
625df8bae1dSRodney W. Grimes 			m_copydata(so->so_snd.sb_mb, off, (int) len,
626df8bae1dSRodney W. Grimes 			    mtod(m, caddr_t) + hdrlen);
627df8bae1dSRodney W. Grimes 			m->m_len += len;
628df8bae1dSRodney W. Grimes 		} else {
629df8bae1dSRodney W. Grimes 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
63051823c3aSGarrett Wollman 			if (m->m_next == 0) {
631d7f570e6SGarrett Wollman 				(void) m_free(m);
63251823c3aSGarrett Wollman 				error = ENOBUFS;
63351823c3aSGarrett Wollman 				goto out;
63451823c3aSGarrett Wollman 			}
635df8bae1dSRodney W. Grimes 		}
636df8bae1dSRodney W. Grimes #endif
637df8bae1dSRodney W. Grimes 		/*
638df8bae1dSRodney W. Grimes 		 * If we're sending everything we've got, set PUSH.
639df8bae1dSRodney W. Grimes 		 * (This will keep happy those implementations which only
640df8bae1dSRodney W. Grimes 		 * give data to the user when a buffer fills or
641df8bae1dSRodney W. Grimes 		 * a PUSH comes in.)
642df8bae1dSRodney W. Grimes 		 */
643df8bae1dSRodney W. Grimes 		if (off + len == so->so_snd.sb_cc)
644df8bae1dSRodney W. Grimes 			flags |= TH_PUSH;
645df8bae1dSRodney W. Grimes 	} else {
646df8bae1dSRodney W. Grimes 		if (tp->t_flags & TF_ACKNOW)
647df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndacks++;
648df8bae1dSRodney W. Grimes 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
649df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndctrl++;
650df8bae1dSRodney W. Grimes 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
651df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndurg++;
652df8bae1dSRodney W. Grimes 		else
653df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndwinup++;
654df8bae1dSRodney W. Grimes 
655a163d034SWarner Losh 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
656df8bae1dSRodney W. Grimes 		if (m == NULL) {
657df8bae1dSRodney W. Grimes 			error = ENOBUFS;
658df8bae1dSRodney W. Grimes 			goto out;
659df8bae1dSRodney W. Grimes 		}
660fb59c426SYoshinobu Inoue #ifdef INET6
661fb59c426SYoshinobu Inoue 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
662fb59c426SYoshinobu Inoue 		    MHLEN >= hdrlen) {
663fb59c426SYoshinobu Inoue 			MH_ALIGN(m, hdrlen);
664fb59c426SYoshinobu Inoue 		} else
665fb59c426SYoshinobu Inoue #endif
666df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
667df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
668df8bae1dSRodney W. Grimes 	}
669df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = (struct ifnet *)0;
670c488362eSRobert Watson #ifdef MAC
671c488362eSRobert Watson 	mac_create_mbuf_from_socket(so, m);
672c488362eSRobert Watson #endif
673fb59c426SYoshinobu Inoue #ifdef INET6
674fb59c426SYoshinobu Inoue 	if (isipv6) {
675fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
676fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip6 + 1);
67708517d53SMike Silbersack 		tcp_fillheaders(tp, ip6, th);
678fb59c426SYoshinobu Inoue 	} else
679fb59c426SYoshinobu Inoue #endif /* INET6 */
680fb59c426SYoshinobu Inoue       {
681fb59c426SYoshinobu Inoue 	ip = mtod(m, struct ip *);
682fb59c426SYoshinobu Inoue 	ipov = (struct ipovly *)ip;
683fb59c426SYoshinobu Inoue 	th = (struct tcphdr *)(ip + 1);
684db4f9cc7SJonathan Lemon 	/* this picks up the pseudo header (w/o the length) */
68508517d53SMike Silbersack 	tcp_fillheaders(tp, ip, th);
686fb59c426SYoshinobu Inoue       }
687df8bae1dSRodney W. Grimes 
688df8bae1dSRodney W. Grimes 	/*
689df8bae1dSRodney W. Grimes 	 * Fill in fields, remembering maximum advertised
690df8bae1dSRodney W. Grimes 	 * window for use in delaying messages about window sizes.
691df8bae1dSRodney W. Grimes 	 * If resending a FIN, be sure not to use a new sequence number.
692df8bae1dSRodney W. Grimes 	 */
693df8bae1dSRodney W. Grimes 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
694df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max)
695df8bae1dSRodney W. Grimes 		tp->snd_nxt--;
696df8bae1dSRodney W. Grimes 	/*
697df8bae1dSRodney W. Grimes 	 * If we are doing retransmissions, then snd_nxt will
698df8bae1dSRodney W. Grimes 	 * not reflect the first unsent octet.  For ACK only
699df8bae1dSRodney W. Grimes 	 * packets, we do not want the sequence number of the
700df8bae1dSRodney W. Grimes 	 * retransmitted packet, we want the sequence number
701df8bae1dSRodney W. Grimes 	 * of the next unsent octet.  So, if there is no data
702df8bae1dSRodney W. Grimes 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
703df8bae1dSRodney W. Grimes 	 * when filling in ti_seq.  But if we are in persist
704df8bae1dSRodney W. Grimes 	 * state, snd_max might reflect one byte beyond the
705df8bae1dSRodney W. Grimes 	 * right edge of the window, so use snd_nxt in that
706df8bae1dSRodney W. Grimes 	 * case, since we know we aren't doing a retransmission.
707df8bae1dSRodney W. Grimes 	 * (retransmit and persist are mutually exclusive...)
708df8bae1dSRodney W. Grimes 	 */
7099b8b58e0SJonathan Lemon 	if (len || (flags & (TH_SYN|TH_FIN))
7109b8b58e0SJonathan Lemon 	    || callout_active(tp->tt_persist))
711fb59c426SYoshinobu Inoue 		th->th_seq = htonl(tp->snd_nxt);
712df8bae1dSRodney W. Grimes 	else
713fb59c426SYoshinobu Inoue 		th->th_seq = htonl(tp->snd_max);
714fb59c426SYoshinobu Inoue 	th->th_ack = htonl(tp->rcv_nxt);
715df8bae1dSRodney W. Grimes 	if (optlen) {
716fb59c426SYoshinobu Inoue 		bcopy(opt, th + 1, optlen);
717fb59c426SYoshinobu Inoue 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
718df8bae1dSRodney W. Grimes 	}
719fb59c426SYoshinobu Inoue 	th->th_flags = flags;
720df8bae1dSRodney W. Grimes 	/*
721df8bae1dSRodney W. Grimes 	 * Calculate receive window.  Don't shrink window,
722df8bae1dSRodney W. Grimes 	 * but avoid silly window syndrome.
723df8bae1dSRodney W. Grimes 	 */
724df8bae1dSRodney W. Grimes 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
725df8bae1dSRodney W. Grimes 		win = 0;
726df8bae1dSRodney W. Grimes 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
727df8bae1dSRodney W. Grimes 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
728610a2e9cSBill Fenner 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
729610a2e9cSBill Fenner 		win = (long)TCP_MAXWIN << tp->rcv_scale;
730fb59c426SYoshinobu Inoue 	th->th_win = htons((u_short) (win>>tp->rcv_scale));
731262c1c1aSMatthew Dillon 
732262c1c1aSMatthew Dillon 
733262c1c1aSMatthew Dillon 	/*
734262c1c1aSMatthew Dillon 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised
735262c1c1aSMatthew Dillon 	 * a 0 window.  This may cause the remote transmitter to stall.  This
736262c1c1aSMatthew Dillon 	 * flag tells soreceive() to disable delayed acknowledgements when
737262c1c1aSMatthew Dillon 	 * draining the buffer.  This can occur if the receiver is attempting
738262c1c1aSMatthew Dillon 	 * to read more data then can be buffered prior to transmitting on
739262c1c1aSMatthew Dillon 	 * the connection.
740262c1c1aSMatthew Dillon 	 */
741262c1c1aSMatthew Dillon 	if (win == 0)
742262c1c1aSMatthew Dillon 		tp->t_flags |= TF_RXWIN0SENT;
743262c1c1aSMatthew Dillon 	else
744262c1c1aSMatthew Dillon 		tp->t_flags &= ~TF_RXWIN0SENT;
745df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
746fb59c426SYoshinobu Inoue 		th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
747fb59c426SYoshinobu Inoue 		th->th_flags |= TH_URG;
748df8bae1dSRodney W. Grimes 	} else
749df8bae1dSRodney W. Grimes 		/*
750df8bae1dSRodney W. Grimes 		 * If no urgent pointer to send, then we pull
751df8bae1dSRodney W. Grimes 		 * the urgent pointer to the left edge of the send window
752df8bae1dSRodney W. Grimes 		 * so that it doesn't drift into the send window on sequence
753df8bae1dSRodney W. Grimes 		 * number wraparound.
754df8bae1dSRodney W. Grimes 		 */
755df8bae1dSRodney W. Grimes 		tp->snd_up = tp->snd_una;		/* drag it along */
756df8bae1dSRodney W. Grimes 
757df8bae1dSRodney W. Grimes 	/*
758df8bae1dSRodney W. Grimes 	 * Put TCP length in extended header, and then
759df8bae1dSRodney W. Grimes 	 * checksum extended header and data.
760df8bae1dSRodney W. Grimes 	 */
761fb59c426SYoshinobu Inoue 	m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
762fb59c426SYoshinobu Inoue #ifdef INET6
763fb59c426SYoshinobu Inoue 	if (isipv6)
764fb59c426SYoshinobu Inoue 		/*
765fb59c426SYoshinobu Inoue 		 * ip6_plen is not need to be filled now, and will be filled
766fb59c426SYoshinobu Inoue 		 * in ip6_output.
767fb59c426SYoshinobu Inoue 		 */
768fb59c426SYoshinobu Inoue 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
769fb59c426SYoshinobu Inoue 				       sizeof(struct tcphdr) + optlen + len);
770fb59c426SYoshinobu Inoue 	else
771fb59c426SYoshinobu Inoue #endif /* INET6 */
772fb59c426SYoshinobu Inoue       {
773db4f9cc7SJonathan Lemon 	m->m_pkthdr.csum_flags = CSUM_TCP;
774db4f9cc7SJonathan Lemon 	m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
775df8bae1dSRodney W. Grimes 	if (len + optlen)
776db4f9cc7SJonathan Lemon 		th->th_sum = in_addword(th->th_sum,
777db4f9cc7SJonathan Lemon 		    htons((u_short)(optlen + len)));
778fb59c426SYoshinobu Inoue 
779db4f9cc7SJonathan Lemon 	/* IP version must be set here for ipv4/ipv6 checking later */
780db4f9cc7SJonathan Lemon 	KASSERT(ip->ip_v == IPVERSION,
7816e551fb6SDavid E. O'Brien 	    ("%s: IP version incorrect: %d", __func__, ip->ip_v));
782fb59c426SYoshinobu Inoue       }
783df8bae1dSRodney W. Grimes 
784df8bae1dSRodney W. Grimes 	/*
785df8bae1dSRodney W. Grimes 	 * In transmit state, time the transmission and arrange for
786df8bae1dSRodney W. Grimes 	 * the retransmit.  In persist state, just set snd_max.
787df8bae1dSRodney W. Grimes 	 */
7889b8b58e0SJonathan Lemon 	if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
789df8bae1dSRodney W. Grimes 		tcp_seq startseq = tp->snd_nxt;
790df8bae1dSRodney W. Grimes 
791df8bae1dSRodney W. Grimes 		/*
792df8bae1dSRodney W. Grimes 		 * Advance snd_nxt over sequence space of this segment.
793df8bae1dSRodney W. Grimes 		 */
794df8bae1dSRodney W. Grimes 		if (flags & (TH_SYN|TH_FIN)) {
795df8bae1dSRodney W. Grimes 			if (flags & TH_SYN)
796df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
797df8bae1dSRodney W. Grimes 			if (flags & TH_FIN) {
798df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
799df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_SENTFIN;
800df8bae1dSRodney W. Grimes 			}
801df8bae1dSRodney W. Grimes 		}
802df8bae1dSRodney W. Grimes 		tp->snd_nxt += len;
803df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
804df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt;
805df8bae1dSRodney W. Grimes 			/*
806df8bae1dSRodney W. Grimes 			 * Time this transmission if not a retransmission and
807df8bae1dSRodney W. Grimes 			 * not currently timing anything.
808df8bae1dSRodney W. Grimes 			 */
8099b8b58e0SJonathan Lemon 			if (tp->t_rtttime == 0) {
8109b8b58e0SJonathan Lemon 				tp->t_rtttime = ticks;
811df8bae1dSRodney W. Grimes 				tp->t_rtseq = startseq;
812df8bae1dSRodney W. Grimes 				tcpstat.tcps_segstimed++;
813df8bae1dSRodney W. Grimes 			}
814df8bae1dSRodney W. Grimes 		}
815df8bae1dSRodney W. Grimes 
816df8bae1dSRodney W. Grimes 		/*
817df8bae1dSRodney W. Grimes 		 * Set retransmit timer if not currently set,
81828257b5cSMatthew Dillon 		 * and not doing a pure ack or a keep-alive probe.
819df8bae1dSRodney W. Grimes 		 * Initial value for retransmit timer is smoothed
820df8bae1dSRodney W. Grimes 		 * round-trip time + 2 * round-trip time variance.
821df8bae1dSRodney W. Grimes 		 * Initialize shift counter which is used for backoff
822df8bae1dSRodney W. Grimes 		 * of retransmit time.
823df8bae1dSRodney W. Grimes 		 */
8249b8b58e0SJonathan Lemon 		if (!callout_active(tp->tt_rexmt) &&
825df8bae1dSRodney W. Grimes 		    tp->snd_nxt != tp->snd_una) {
8269b8b58e0SJonathan Lemon 			if (callout_active(tp->tt_persist)) {
8279b8b58e0SJonathan Lemon 				callout_stop(tp->tt_persist);
828df8bae1dSRodney W. Grimes 				tp->t_rxtshift = 0;
829df8bae1dSRodney W. Grimes 			}
83046f58482SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
83146f58482SJonathan Lemon 				      tcp_timer_rexmt, tp);
832df8bae1dSRodney W. Grimes 		}
83328257b5cSMatthew Dillon 	} else {
83428257b5cSMatthew Dillon 		/*
83528257b5cSMatthew Dillon 		 * Persist case, update snd_max but since we are in
83628257b5cSMatthew Dillon 		 * persist mode (no window) we do not update snd_nxt.
83728257b5cSMatthew Dillon 		 */
83828257b5cSMatthew Dillon 		int xlen = len;
83928257b5cSMatthew Dillon 		if (flags & TH_SYN)
84028257b5cSMatthew Dillon 			++xlen;
84128257b5cSMatthew Dillon 		if (flags & TH_FIN) {
84228257b5cSMatthew Dillon 			++xlen;
84328257b5cSMatthew Dillon 			tp->t_flags |= TF_SENTFIN;
84428257b5cSMatthew Dillon 		}
845abac41a6SMatthew Dillon 		if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
846df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt + len;
84728257b5cSMatthew Dillon 	}
848df8bae1dSRodney W. Grimes 
849610ee2f9SDavid Greenman #ifdef TCPDEBUG
850df8bae1dSRodney W. Grimes 	/*
851df8bae1dSRodney W. Grimes 	 * Trace.
852df8bae1dSRodney W. Grimes 	 */
8534cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
854fb59c426SYoshinobu Inoue 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
855610ee2f9SDavid Greenman #endif
856df8bae1dSRodney W. Grimes 
857df8bae1dSRodney W. Grimes 	/*
858df8bae1dSRodney W. Grimes 	 * Fill in IP length and desired time to live and
859df8bae1dSRodney W. Grimes 	 * send to IP level.  There should be a better way
860df8bae1dSRodney W. Grimes 	 * to handle ttl and tos; we could keep them in
861df8bae1dSRodney W. Grimes 	 * the template, but need a way to checksum without them.
862df8bae1dSRodney W. Grimes 	 */
863fb59c426SYoshinobu Inoue 	/*
864fb59c426SYoshinobu Inoue 	 * m->m_pkthdr.len should have been set before cksum calcuration,
865fb59c426SYoshinobu Inoue 	 * because in6_cksum() need it.
866fb59c426SYoshinobu Inoue 	 */
867fb59c426SYoshinobu Inoue #ifdef INET6
868fb59c426SYoshinobu Inoue 	if (isipv6) {
869fb59c426SYoshinobu Inoue 		/*
870fb59c426SYoshinobu Inoue 		 * we separately set hoplimit for every segment, since the
871fb59c426SYoshinobu Inoue 		 * user might want to change the value via setsockopt.
872fb59c426SYoshinobu Inoue 		 * Also, desired default hop limit might be changed via
873fb59c426SYoshinobu Inoue 		 * Neighbor Discovery.
874fb59c426SYoshinobu Inoue 		 */
875fb59c426SYoshinobu Inoue 		ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb,
876fb59c426SYoshinobu Inoue 					       tp->t_inpcb->in6p_route.ro_rt ?
877fb59c426SYoshinobu Inoue 					       tp->t_inpcb->in6p_route.ro_rt->rt_ifp
878fb59c426SYoshinobu Inoue 					       : NULL);
879fb59c426SYoshinobu Inoue 
880fb59c426SYoshinobu Inoue 		/* TODO: IPv6 IP6TOS_ECT bit on */
881fb59c426SYoshinobu Inoue 		error = ip6_output(m,
882fb59c426SYoshinobu Inoue 			    tp->t_inpcb->in6p_outputopts,
883fb59c426SYoshinobu Inoue 			    &tp->t_inpcb->in6p_route,
8845d846453SSam Leffler 			    (so->so_options & SO_DONTROUTE), NULL, NULL,
8855d846453SSam Leffler 			    tp->t_inpcb);
886fb59c426SYoshinobu Inoue 	} else
887fb59c426SYoshinobu Inoue #endif /* INET6 */
888df8bae1dSRodney W. Grimes     {
889f138387aSGarrett Wollman 	struct rtentry *rt;
890fb59c426SYoshinobu Inoue 	ip->ip_len = m->m_pkthdr.len;
891686cdd19SJun-ichiro itojun Hagino #ifdef INET6
892686cdd19SJun-ichiro itojun Hagino  	if (INP_CHECK_SOCKAF(so, AF_INET6))
893686cdd19SJun-ichiro itojun Hagino  		ip->ip_ttl = in6_selecthlim(tp->t_inpcb,
894686cdd19SJun-ichiro itojun Hagino  					    tp->t_inpcb->in6p_route.ro_rt ?
895686cdd19SJun-ichiro itojun Hagino  					    tp->t_inpcb->in6p_route.ro_rt->rt_ifp
896686cdd19SJun-ichiro itojun Hagino  					    : NULL);
897686cdd19SJun-ichiro itojun Hagino  	else
898686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */
899fb59c426SYoshinobu Inoue 	ip->ip_ttl = tp->t_inpcb->inp_ip_ttl;	/* XXX */
900fb59c426SYoshinobu Inoue 	ip->ip_tos = tp->t_inpcb->inp_ip_tos;	/* XXX */
901f138387aSGarrett Wollman 	/*
902f138387aSGarrett Wollman 	 * See if we should do MTU discovery.  We do it only if the following
903f138387aSGarrett Wollman 	 * are true:
904f138387aSGarrett Wollman 	 *	1) we have a valid route to the destination
905f138387aSGarrett Wollman 	 *	2) the MTU is not locked (if it is, then discovery has been
906f138387aSGarrett Wollman 	 *	   disabled)
907f138387aSGarrett Wollman 	 */
9089a039a5fSDavid Greenman 	if (path_mtu_discovery
9099a039a5fSDavid Greenman 	    && (rt = tp->t_inpcb->inp_route.ro_rt)
910f138387aSGarrett Wollman 	    && rt->rt_flags & RTF_UP
911f138387aSGarrett Wollman 	    && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
912fb59c426SYoshinobu Inoue 		ip->ip_off |= IP_DF;
913f138387aSGarrett Wollman 	}
914df8bae1dSRodney W. Grimes 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
9155d846453SSam Leffler 	    (so->so_options & SO_DONTROUTE), 0, tp->t_inpcb);
916df8bae1dSRodney W. Grimes     }
917df8bae1dSRodney W. Grimes 	if (error) {
9187734ea06SArchie Cobbs 
9197734ea06SArchie Cobbs 		/*
9207734ea06SArchie Cobbs 		 * We know that the packet was lost, so back out the
9217734ea06SArchie Cobbs 		 * sequence number advance, if any.
9227734ea06SArchie Cobbs 		 */
9237734ea06SArchie Cobbs 		if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
9247734ea06SArchie Cobbs 			/*
9257734ea06SArchie Cobbs 			 * No need to check for TH_FIN here because
9267734ea06SArchie Cobbs 			 * the TF_SENTFIN flag handles that case.
9277734ea06SArchie Cobbs 			 */
9286612c70eSArchie Cobbs 			if ((flags & TH_SYN) == 0)
9297734ea06SArchie Cobbs 				tp->snd_nxt -= len;
9307734ea06SArchie Cobbs 		}
9317734ea06SArchie Cobbs 
932df8bae1dSRodney W. Grimes out:
933df8bae1dSRodney W. Grimes 		if (error == ENOBUFS) {
93459f577adSJonathan Lemon 	                if (!callout_active(tp->tt_rexmt) &&
93559f577adSJonathan Lemon                             !callout_active(tp->tt_persist))
93659f577adSJonathan Lemon 	                        callout_reset(tp->tt_rexmt, tp->t_rxtcur,
93759f577adSJonathan Lemon                                       tcp_timer_rexmt, tp);
938df8bae1dSRodney W. Grimes 			tcp_quench(tp->t_inpcb, 0);
939df8bae1dSRodney W. Grimes 			return (0);
940df8bae1dSRodney W. Grimes 		}
9413d1f141bSGarrett Wollman 		if (error == EMSGSIZE) {
9423d1f141bSGarrett Wollman 			/*
9433d1f141bSGarrett Wollman 			 * ip_output() will have already fixed the route
9443d1f141bSGarrett Wollman 			 * for us.  tcp_mtudisc() will, as its last action,
9453d1f141bSGarrett Wollman 			 * initiate retransmission, so it is important to
9463d1f141bSGarrett Wollman 			 * not do so here.
9473d1f141bSGarrett Wollman 			 */
9483d1f141bSGarrett Wollman 			tcp_mtudisc(tp->t_inpcb, 0);
9493d1f141bSGarrett Wollman 			return 0;
9503d1f141bSGarrett Wollman 		}
951df8bae1dSRodney W. Grimes 		if ((error == EHOSTUNREACH || error == ENETDOWN)
952df8bae1dSRodney W. Grimes 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
953df8bae1dSRodney W. Grimes 			tp->t_softerror = error;
954df8bae1dSRodney W. Grimes 			return (0);
955df8bae1dSRodney W. Grimes 		}
956df8bae1dSRodney W. Grimes 		return (error);
957df8bae1dSRodney W. Grimes 	}
958df8bae1dSRodney W. Grimes 	tcpstat.tcps_sndtotal++;
959df8bae1dSRodney W. Grimes 
960df8bae1dSRodney W. Grimes 	/*
961df8bae1dSRodney W. Grimes 	 * Data sent (as far as we can tell).
962df8bae1dSRodney W. Grimes 	 * If this advertises a larger window than any other segment,
963df8bae1dSRodney W. Grimes 	 * then remember the size of the advertised window.
964df8bae1dSRodney W. Grimes 	 * Any pending ACK has now been sent.
965df8bae1dSRodney W. Grimes 	 */
966df8bae1dSRodney W. Grimes 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
967df8bae1dSRodney W. Grimes 		tp->rcv_adv = tp->rcv_nxt + win;
968df8bae1dSRodney W. Grimes 	tp->last_ack_sent = tp->rcv_nxt;
9693bfd6421SJonathan Lemon 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
9703bfd6421SJonathan Lemon 	if (callout_active(tp->tt_delack))
9719b8b58e0SJonathan Lemon 		callout_stop(tp->tt_delack);
972d912c694SMatthew Dillon #if 0
973d912c694SMatthew Dillon 	/*
974d912c694SMatthew Dillon 	 * This completely breaks TCP if newreno is turned on.  What happens
975d912c694SMatthew Dillon 	 * is that if delayed-acks are turned on on the receiver, this code
976d912c694SMatthew Dillon 	 * on the transmitter effectively destroys the TCP window, forcing
977d912c694SMatthew Dillon 	 * it to four packets (1.5Kx4 = 6K window).
978d912c694SMatthew Dillon 	 */
97946f58482SJonathan Lemon 	if (sendalot && (!tcp_do_newreno || --maxburst))
980df8bae1dSRodney W. Grimes 		goto again;
981d912c694SMatthew Dillon #endif
982d912c694SMatthew Dillon 	if (sendalot)
983d912c694SMatthew Dillon 		goto again;
984df8bae1dSRodney W. Grimes 	return (0);
985df8bae1dSRodney W. Grimes }
986df8bae1dSRodney W. Grimes 
987df8bae1dSRodney W. Grimes void
988df8bae1dSRodney W. Grimes tcp_setpersist(tp)
989df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
990df8bae1dSRodney W. Grimes {
9919b8b58e0SJonathan Lemon 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
9929b8b58e0SJonathan Lemon 	int tt;
993df8bae1dSRodney W. Grimes 
9949b8b58e0SJonathan Lemon 	if (callout_active(tp->tt_rexmt))
9959b8b58e0SJonathan Lemon 		panic("tcp_setpersist: retransmit pending");
996df8bae1dSRodney W. Grimes 	/*
997df8bae1dSRodney W. Grimes 	 * Start/restart persistance timer.
998df8bae1dSRodney W. Grimes 	 */
9999b8b58e0SJonathan Lemon 	TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1000df8bae1dSRodney W. Grimes 		      TCPTV_PERSMIN, TCPTV_PERSMAX);
10019b8b58e0SJonathan Lemon 	callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp);
1002df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1003df8bae1dSRodney W. Grimes 		tp->t_rxtshift++;
1004df8bae1dSRodney W. Grimes }
1005