xref: /freebsd/sys/netinet/tcp_output.c (revision 8bec3467b1a0cc3ac7a48152e39755f30feecfb0)
1c398230bSWarner Losh /*-
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  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29d7f570e6SGarrett Wollman  *	@(#)tcp_output.c	8.4 (Berkeley) 5/24/95
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
331cfd4b53SBruce M Simpson #include "opt_inet.h"
34fb59c426SYoshinobu Inoue #include "opt_inet6.h"
353a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
36c488362eSRobert Watson #include "opt_mac.h"
370cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
386d90faf3SPaul Saab #include "opt_tcp_sack.h"
390cc12cc5SJoerg Wunsch 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
42686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h>
43fb919e4dSMark Murray #include <sys/kernel.h>
44fb919e4dSMark Murray #include <sys/lock.h>
45fb919e4dSMark Murray #include <sys/mbuf.h>
46fb919e4dSMark Murray #include <sys/mutex.h>
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48df8bae1dSRodney W. Grimes #include <sys/socket.h>
49df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
50fb919e4dSMark Murray #include <sys/sysctl.h>
51df8bae1dSRodney W. Grimes 
52df8bae1dSRodney W. Grimes #include <net/route.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <netinet/in.h>
55df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
56df8bae1dSRodney W. Grimes #include <netinet/ip.h>
57df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
58df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
59ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
60fb59c426SYoshinobu Inoue #ifdef INET6
61686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
62686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
63fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
64fb59c426SYoshinobu Inoue #endif
65df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
66df8bae1dSRodney W. Grimes #define	TCPOUTFLAGS
67df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
68df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
69df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
70df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
71df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
72610ee2f9SDavid Greenman #ifdef TCPDEBUG
73df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
74610ee2f9SDavid Greenman #endif
75df8bae1dSRodney W. Grimes 
763a2a9f79SYoshinobu Inoue #ifdef IPSEC
773a2a9f79SYoshinobu Inoue #include <netinet6/ipsec.h>
783a2a9f79SYoshinobu Inoue #endif /*IPSEC*/
793a2a9f79SYoshinobu Inoue 
80b9234fafSSam Leffler #ifdef FAST_IPSEC
81b9234fafSSam Leffler #include <netipsec/ipsec.h>
82b9234fafSSam Leffler #define	IPSEC
83b9234fafSSam Leffler #endif /*FAST_IPSEC*/
84b9234fafSSam Leffler 
85db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
86db4f9cc7SJonathan Lemon 
87aed55708SRobert Watson #include <security/mac/mac_framework.h>
88aed55708SRobert Watson 
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 
109b3c0f300SAndre Oppermann int	tcp_do_tso = 1;
110b3c0f300SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_RW,
111b3c0f300SAndre Oppermann 	&tcp_do_tso, 0, "Enable TCP Segmentation Offload");
112b3c0f300SAndre Oppermann 
1136741ecf5SAndre Oppermann int	tcp_do_autosndbuf = 1;
1146741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_RW,
1156741ecf5SAndre Oppermann 	&tcp_do_autosndbuf, 0, "Enable automatic send buffer sizing");
1166741ecf5SAndre Oppermann 
1176741ecf5SAndre Oppermann int	tcp_autosndbuf_inc = 8*1024;
1186741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_RW,
1196741ecf5SAndre Oppermann 	&tcp_autosndbuf_inc, 0, "Incrementor step size of automatic send buffer");
1206741ecf5SAndre Oppermann 
1216741ecf5SAndre Oppermann int	tcp_autosndbuf_max = 256*1024;
1226741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_RW,
1236741ecf5SAndre Oppermann 	&tcp_autosndbuf_max, 0, "Max size of automatic send buffer");
1246741ecf5SAndre Oppermann 
1256741ecf5SAndre Oppermann 
126df8bae1dSRodney W. Grimes /*
127df8bae1dSRodney W. Grimes  * Tcp output routine: figure out what should be sent and send it.
128df8bae1dSRodney W. Grimes  */
129df8bae1dSRodney W. Grimes int
130f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp)
131df8bae1dSRodney W. Grimes {
132f10e85d7SLuigi Rizzo 	struct socket *so = tp->t_inpcb->inp_socket;
133201d185bSAndre Oppermann 	long len, recwin, sendwin;
134df8bae1dSRodney W. Grimes 	int off, flags, error;
13545d370eeSBruce M Simpson #ifdef TCP_SIGNATURE
1361cfd4b53SBruce M Simpson 	int sigoff = 0;
137265ed012SBruce M Simpson #endif
138f10e85d7SLuigi Rizzo 	struct mbuf *m;
139fb59c426SYoshinobu Inoue 	struct ip *ip = NULL;
140f10e85d7SLuigi Rizzo 	struct ipovly *ipov = NULL;
141f10e85d7SLuigi Rizzo 	struct tcphdr *th;
142a0292f23SGarrett Wollman 	u_char opt[TCP_MAXOLEN];
143a04884fcSBill Fenner 	unsigned ipoptlen, optlen, hdrlen;
144df8bae1dSRodney W. Grimes 	int idle, sendalot;
1456d90faf3SPaul Saab 	int i, sack_rxmit;
146a55db2b6SPaul Saab 	int sack_bytes_rxmt;
1476d90faf3SPaul Saab 	struct sackhole *p;
148b3c0f300SAndre Oppermann 	int tso = 0;
149262c1c1aSMatthew Dillon #if 0
15046f58482SJonathan Lemon 	int maxburst = TCP_MAXBURST;
151262c1c1aSMatthew Dillon #endif
152fb59c426SYoshinobu Inoue #ifdef INET6
153f10e85d7SLuigi Rizzo 	struct ip6_hdr *ip6 = NULL;
154fb59c426SYoshinobu Inoue 	int isipv6;
155fb59c426SYoshinobu Inoue 
156fb59c426SYoshinobu Inoue 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
157fb59c426SYoshinobu Inoue #endif
158df8bae1dSRodney W. Grimes 
159fa286d7dSSam Leffler 	INP_LOCK_ASSERT(tp->t_inpcb);
1603d6ade3aSJennifer Yang 
161df8bae1dSRodney W. Grimes 	/*
162df8bae1dSRodney W. Grimes 	 * Determine length of data that should be transmitted,
163df8bae1dSRodney W. Grimes 	 * and flags that will be used.
164df8bae1dSRodney W. Grimes 	 * If there is some data or critical controls (SYN, RST)
165df8bae1dSRodney W. Grimes 	 * to send, then transmit; otherwise, investigate further.
166df8bae1dSRodney W. Grimes 	 */
167c24d5daeSJayanth Vijayaraghavan 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
1689b8b58e0SJonathan Lemon 	if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
169df8bae1dSRodney W. Grimes 		/*
170df8bae1dSRodney W. Grimes 		 * We have been idle for "a while" and no acks are
171df8bae1dSRodney W. Grimes 		 * expected to clock out any data we send --
172df8bae1dSRodney W. Grimes 		 * slow start to get ack "clock" running again.
1739b8b58e0SJonathan Lemon 		 *
1749b8b58e0SJonathan Lemon 		 * Set the slow-start flight size depending on whether
1759b8b58e0SJonathan Lemon 		 * this is a local network or not.
176df8bae1dSRodney W. Grimes 		 */
177f10e85d7SLuigi Rizzo 		int ss = ss_fltsz;
178fb59c426SYoshinobu Inoue #ifdef INET6
179f10e85d7SLuigi Rizzo 		if (isipv6) {
180f10e85d7SLuigi Rizzo 			if (in6_localaddr(&tp->t_inpcb->in6p_faddr))
181f10e85d7SLuigi Rizzo 				ss = ss_fltsz_local;
182f10e85d7SLuigi Rizzo 		} else
183f10e85d7SLuigi Rizzo #endif /* INET6 */
184f10e85d7SLuigi Rizzo 		if (in_localaddr(tp->t_inpcb->inp_faddr))
185f10e85d7SLuigi Rizzo 			ss = ss_fltsz_local;
186f10e85d7SLuigi Rizzo 		tp->snd_cwnd = tp->t_maxseg * ss;
1879b8b58e0SJonathan Lemon 	}
188c24d5daeSJayanth Vijayaraghavan 	tp->t_flags &= ~TF_LASTIDLE;
189c24d5daeSJayanth Vijayaraghavan 	if (idle) {
190c24d5daeSJayanth Vijayaraghavan 		if (tp->t_flags & TF_MORETOCOME) {
191c24d5daeSJayanth Vijayaraghavan 			tp->t_flags |= TF_LASTIDLE;
192c24d5daeSJayanth Vijayaraghavan 			idle = 0;
193c24d5daeSJayanth Vijayaraghavan 		}
194c24d5daeSJayanth Vijayaraghavan 	}
195df8bae1dSRodney W. Grimes again:
1966d90faf3SPaul Saab 	/*
1976d90faf3SPaul Saab 	 * If we've recently taken a timeout, snd_max will be greater than
1986d90faf3SPaul Saab 	 * snd_nxt.  There may be SACK information that allows us to avoid
1996d90faf3SPaul Saab 	 * resending already delivered data.  Adjust snd_nxt accordingly.
2006d90faf3SPaul Saab 	 */
2016d90faf3SPaul Saab 	if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max))
2026d90faf3SPaul Saab 		tcp_sack_adjust(tp);
203df8bae1dSRodney W. Grimes 	sendalot = 0;
204df8bae1dSRodney W. Grimes 	off = tp->snd_nxt - tp->snd_una;
205201d185bSAndre Oppermann 	sendwin = min(tp->snd_wnd, tp->snd_cwnd);
206201d185bSAndre Oppermann 	sendwin = min(sendwin, tp->snd_bwnd);
207df8bae1dSRodney W. Grimes 
208df8bae1dSRodney W. Grimes 	flags = tcp_outflags[tp->t_state];
209a0292f23SGarrett Wollman 	/*
2106d90faf3SPaul Saab 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
2116d90faf3SPaul Saab 	 * to send out new data (when sendalot is 1), bypass this function.
2126d90faf3SPaul Saab 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
2136d90faf3SPaul Saab 	 * we're replacing a (future) new transmission with a retransmission
2146d90faf3SPaul Saab 	 * now, and we previously incremented snd_cwnd in tcp_input().
2156d90faf3SPaul Saab 	 */
2166d90faf3SPaul Saab 	/*
2176d90faf3SPaul Saab 	 * Still in sack recovery , reset rxmit flag to zero.
2186d90faf3SPaul Saab 	 */
2196d90faf3SPaul Saab 	sack_rxmit = 0;
220a55db2b6SPaul Saab 	sack_bytes_rxmt = 0;
2216d90faf3SPaul Saab 	len = 0;
2226d90faf3SPaul Saab 	p = NULL;
22304f0d9a0SJayanth Vijayaraghavan 	if (tp->sack_enable && IN_FASTRECOVERY(tp) &&
224a55db2b6SPaul Saab 	    (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
225a55db2b6SPaul Saab 		long cwin;
226a55db2b6SPaul Saab 
227a55db2b6SPaul Saab 		cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
228a55db2b6SPaul Saab 		if (cwin < 0)
229a55db2b6SPaul Saab 			cwin = 0;
230f787edd8SJayanth Vijayaraghavan 		/* Do not retransmit SACK segments beyond snd_recover */
231f787edd8SJayanth Vijayaraghavan 		if (SEQ_GT(p->end, tp->snd_recover)) {
232f787edd8SJayanth Vijayaraghavan 			/*
233f787edd8SJayanth Vijayaraghavan 			 * (At least) part of sack hole extends beyond
234f787edd8SJayanth Vijayaraghavan 			 * snd_recover. Check to see if we can rexmit data
235f787edd8SJayanth Vijayaraghavan 			 * for this hole.
236f787edd8SJayanth Vijayaraghavan 			 */
237f787edd8SJayanth Vijayaraghavan 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
238f787edd8SJayanth Vijayaraghavan 				/*
239f787edd8SJayanth Vijayaraghavan 				 * Can't rexmit any more data for this hole.
240f787edd8SJayanth Vijayaraghavan 				 * That data will be rexmitted in the next
241f787edd8SJayanth Vijayaraghavan 				 * sack recovery episode, when snd_recover
242f787edd8SJayanth Vijayaraghavan 				 * moves past p->rxmit.
243f787edd8SJayanth Vijayaraghavan 				 */
244f787edd8SJayanth Vijayaraghavan 				p = NULL;
245f787edd8SJayanth Vijayaraghavan 				goto after_sack_rexmit;
246f787edd8SJayanth Vijayaraghavan 			} else
247f787edd8SJayanth Vijayaraghavan 				/* Can rexmit part of the current hole */
248a55db2b6SPaul Saab 				len = ((long)ulmin(cwin,
249f787edd8SJayanth Vijayaraghavan 						   tp->snd_recover - p->rxmit));
250f787edd8SJayanth Vijayaraghavan 		} else
251a55db2b6SPaul Saab 			len = ((long)ulmin(cwin, p->end - p->rxmit));
2526d90faf3SPaul Saab 		off = p->rxmit - tp->snd_una;
253f787edd8SJayanth Vijayaraghavan 		KASSERT(off >= 0,("%s: sack block to the left of una : %d",
254f787edd8SJayanth Vijayaraghavan 		    __func__, off));
2556d90faf3SPaul Saab 		if (len > 0) {
256ab5c14d8SRobert Watson 			sack_rxmit = 1;
257ab5c14d8SRobert Watson 			sendalot = 1;
2586d90faf3SPaul Saab 			tcpstat.tcps_sack_rexmits++;
2596d90faf3SPaul Saab 			tcpstat.tcps_sack_rexmit_bytes +=
2606d90faf3SPaul Saab 			    min(len, tp->t_maxseg);
2616d90faf3SPaul Saab 		}
2626d90faf3SPaul Saab 	}
263f787edd8SJayanth Vijayaraghavan after_sack_rexmit:
2646d90faf3SPaul Saab 	/*
265a0292f23SGarrett Wollman 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
266a0292f23SGarrett Wollman 	 * state flags.
267a0292f23SGarrett Wollman 	 */
268a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDFIN)
269a0292f23SGarrett Wollman 		flags |= TH_FIN;
270a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDSYN)
271a0292f23SGarrett Wollman 		flags |= TH_SYN;
272a0292f23SGarrett Wollman 
273cf2942b6SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
274df8bae1dSRodney W. Grimes 	/*
275df8bae1dSRodney W. Grimes 	 * If in persist timeout with window of 0, send 1 byte.
276df8bae1dSRodney W. Grimes 	 * Otherwise, if window is small but nonzero
277df8bae1dSRodney W. Grimes 	 * and timer expired, we will send what we can
278df8bae1dSRodney W. Grimes 	 * and go to transmit state.
279df8bae1dSRodney W. Grimes 	 */
2802cdbfa66SPaul Saab 	if (tp->t_flags & TF_FORCEDATA) {
281201d185bSAndre Oppermann 		if (sendwin == 0) {
282df8bae1dSRodney W. Grimes 			/*
283df8bae1dSRodney W. Grimes 			 * If we still have some data to send, then
284df8bae1dSRodney W. Grimes 			 * clear the FIN bit.  Usually this would
285df8bae1dSRodney W. Grimes 			 * happen below when it realizes that we
286df8bae1dSRodney W. Grimes 			 * aren't sending all the data.  However,
28729089b51SJulian Elischer 			 * if we have exactly 1 byte of unsent data,
288df8bae1dSRodney W. Grimes 			 * then it won't clear the FIN bit below,
289df8bae1dSRodney W. Grimes 			 * and if we are in persist state, we wind
290df8bae1dSRodney W. Grimes 			 * up sending the packet without recording
291df8bae1dSRodney W. Grimes 			 * that we sent the FIN bit.
292df8bae1dSRodney W. Grimes 			 *
293df8bae1dSRodney W. Grimes 			 * We can't just blindly clear the FIN bit,
294df8bae1dSRodney W. Grimes 			 * because if we don't have any more data
295df8bae1dSRodney W. Grimes 			 * to send then the probe will be the FIN
296df8bae1dSRodney W. Grimes 			 * itself.
297df8bae1dSRodney W. Grimes 			 */
298df8bae1dSRodney W. Grimes 			if (off < so->so_snd.sb_cc)
299df8bae1dSRodney W. Grimes 				flags &= ~TH_FIN;
300201d185bSAndre Oppermann 			sendwin = 1;
301df8bae1dSRodney W. Grimes 		} else {
3029b8b58e0SJonathan Lemon 			callout_stop(tp->tt_persist);
303df8bae1dSRodney W. Grimes 			tp->t_rxtshift = 0;
304df8bae1dSRodney W. Grimes 		}
305df8bae1dSRodney W. Grimes 	}
306df8bae1dSRodney W. Grimes 
30728257b5cSMatthew Dillon 	/*
30828257b5cSMatthew Dillon 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
30928257b5cSMatthew Dillon 	 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
310201d185bSAndre Oppermann 	 * a negative length.  This can also occur when TCP opens up
31128257b5cSMatthew Dillon 	 * its congestion window while receiving additional duplicate
31228257b5cSMatthew Dillon 	 * acks after fast-retransmit because TCP will reset snd_nxt
31328257b5cSMatthew Dillon 	 * to snd_max after the fast-retransmit.
31428257b5cSMatthew Dillon 	 *
31528257b5cSMatthew Dillon 	 * In the normal retransmit-FIN-only case, however, snd_nxt will
31628257b5cSMatthew Dillon 	 * be set to snd_una, the offset will be 0, and the length may
31728257b5cSMatthew Dillon 	 * wind up 0.
3186d90faf3SPaul Saab 	 *
3196d90faf3SPaul Saab 	 * If sack_rxmit is true we are retransmitting from the scoreboard
3206d90faf3SPaul Saab 	 * in which case len is already set.
32128257b5cSMatthew Dillon 	 */
322a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
323a55db2b6SPaul Saab 		if (sack_bytes_rxmt == 0)
3246d90faf3SPaul Saab 			len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
325a55db2b6SPaul Saab 		else {
326a55db2b6SPaul Saab 			long cwin;
327a55db2b6SPaul Saab 
328a55db2b6SPaul Saab                         /*
329a55db2b6SPaul Saab 			 * We are inside of a SACK recovery episode and are
330a55db2b6SPaul Saab 			 * sending new data, having retransmitted all the
331a55db2b6SPaul Saab 			 * data possible in the scoreboard.
332a55db2b6SPaul Saab 			 */
3337d5ed1ceSPaul Saab 			len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd)
3347d5ed1ceSPaul Saab 			       - off);
3358d03f2b5SPaul Saab 			/*
3368d03f2b5SPaul Saab 			 * Don't remove this (len > 0) check !
3378d03f2b5SPaul Saab 			 * We explicitly check for len > 0 here (although it
3388d03f2b5SPaul Saab 			 * isn't really necessary), to work around a gcc
3398d03f2b5SPaul Saab 			 * optimization issue - to force gcc to compute
3408d03f2b5SPaul Saab 			 * len above. Without this check, the computation
3418d03f2b5SPaul Saab 			 * of len is bungled by the optimizer.
3428d03f2b5SPaul Saab 			 */
3438d03f2b5SPaul Saab 			if (len > 0) {
3447d5ed1ceSPaul Saab 				cwin = tp->snd_cwnd -
3457d5ed1ceSPaul Saab 					(tp->snd_nxt - tp->sack_newdata) -
346a55db2b6SPaul Saab 					sack_bytes_rxmt;
347a55db2b6SPaul Saab 				if (cwin < 0)
348a55db2b6SPaul Saab 					cwin = 0;
349a55db2b6SPaul Saab 				len = lmin(len, cwin);
350a55db2b6SPaul Saab 			}
351a55db2b6SPaul Saab 		}
3528d03f2b5SPaul Saab 	}
353a0292f23SGarrett Wollman 
354a0292f23SGarrett Wollman 	/*
355a0292f23SGarrett Wollman 	 * Lop off SYN bit if it has already been sent.  However, if this
356a0292f23SGarrett Wollman 	 * is SYN-SENT state and if segment contains data and if we don't
357a0292f23SGarrett Wollman 	 * know that foreign host supports TAO, suppress sending segment.
358a0292f23SGarrett Wollman 	 */
359a0292f23SGarrett Wollman 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
3604b8e98d6SQing Li 		if (tp->t_state != TCPS_SYN_RECEIVED)
361a0292f23SGarrett Wollman 			flags &= ~TH_SYN;
362a0292f23SGarrett Wollman 		off--, len++;
363a0292f23SGarrett Wollman 	}
364a0292f23SGarrett Wollman 
36581165e48SAndras Olah 	/*
366c94c54e4SAndre Oppermann 	 * Be careful not to send data and/or FIN on SYN segments.
36781165e48SAndras Olah 	 * This measure is needed to prevent interoperability problems
36881165e48SAndras Olah 	 * with not fully conformant TCP implementations.
36981165e48SAndras Olah 	 */
370c94c54e4SAndre Oppermann 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
37181165e48SAndras Olah 		len = 0;
37281165e48SAndras Olah 		flags &= ~TH_FIN;
37381165e48SAndras Olah 	}
37481165e48SAndras Olah 
375df8bae1dSRodney W. Grimes 	if (len < 0) {
376df8bae1dSRodney W. Grimes 		/*
377df8bae1dSRodney W. Grimes 		 * If FIN has been sent but not acked,
378df8bae1dSRodney W. Grimes 		 * but we haven't been called to retransmit,
37928257b5cSMatthew Dillon 		 * len will be < 0.  Otherwise, window shrank
380df8bae1dSRodney W. Grimes 		 * after we sent into it.  If window shrank to 0,
3812d8266afSDavid Greenman 		 * cancel pending retransmit, pull snd_nxt back
3822d8266afSDavid Greenman 		 * to (closed) window, and set the persist timer
3832d8266afSDavid Greenman 		 * if it isn't already going.  If the window didn't
3842d8266afSDavid Greenman 		 * close completely, just wait for an ACK.
385df8bae1dSRodney W. Grimes 		 */
386df8bae1dSRodney W. Grimes 		len = 0;
387201d185bSAndre Oppermann 		if (sendwin == 0) {
3889b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
3892d8266afSDavid Greenman 			tp->t_rxtshift = 0;
390df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
3919b8b58e0SJonathan Lemon 			if (!callout_active(tp->tt_persist))
3922d8266afSDavid Greenman 				tcp_setpersist(tp);
393df8bae1dSRodney W. Grimes 		}
394df8bae1dSRodney W. Grimes 	}
39528257b5cSMatthew Dillon 
3966741ecf5SAndre Oppermann 	/* len will be >= 0 after this point. */
3976741ecf5SAndre Oppermann 	KASSERT(len >= 0, ("%s: len < 0", __func__));
3986741ecf5SAndre Oppermann 
39928257b5cSMatthew Dillon 	/*
4006741ecf5SAndre Oppermann 	 * Automatic sizing of send socket buffer.  Often the send buffer
4016741ecf5SAndre Oppermann 	 * size is not optimally adjusted to the actual network conditions
4026741ecf5SAndre Oppermann 	 * at hand (delay bandwidth product).  Setting the buffer size too
4036741ecf5SAndre Oppermann 	 * small limits throughput on links with high bandwidth and high
4046741ecf5SAndre Oppermann 	 * delay (eg. trans-continental/oceanic links).  Setting the
4056741ecf5SAndre Oppermann 	 * buffer size too big consumes too much real kernel memory,
4066741ecf5SAndre Oppermann 	 * especially with many connections on busy servers.
4076741ecf5SAndre Oppermann 	 *
4086741ecf5SAndre Oppermann 	 * The criteria to step up the send buffer one notch are:
4096741ecf5SAndre Oppermann 	 *  1. receive window of remote host is larger than send buffer
4106741ecf5SAndre Oppermann 	 *     (with a fudge factor of 5/4th);
4116741ecf5SAndre Oppermann 	 *  2. send buffer is filled to 7/8th with data (so we actually
4126741ecf5SAndre Oppermann 	 *     have data to make use of it);
4136741ecf5SAndre Oppermann 	 *  3. send buffer fill has not hit maximal automatic size;
4146741ecf5SAndre Oppermann 	 *  4. our send window (slow start and cogestion controlled) is
4156741ecf5SAndre Oppermann 	 *     larger than sent but unacknowledged data in send buffer.
4166741ecf5SAndre Oppermann 	 *
4176741ecf5SAndre Oppermann 	 * The remote host receive window scaling factor may limit the
4186741ecf5SAndre Oppermann 	 * growing of the send buffer before it reaches its allowed
4196741ecf5SAndre Oppermann 	 * maximum.
4206741ecf5SAndre Oppermann 	 *
4216741ecf5SAndre Oppermann 	 * It scales directly with slow start or congestion window
4226741ecf5SAndre Oppermann 	 * and does at most one step per received ACK.  This fast
4236741ecf5SAndre Oppermann 	 * scaling has the drawback of growing the send buffer beyond
4246741ecf5SAndre Oppermann 	 * what is strictly necessary to make full use of a given
4256741ecf5SAndre Oppermann 	 * delay*bandwith product.  However testing has shown this not
4266741ecf5SAndre Oppermann 	 * to be much of an problem.  At worst we are trading wasting
4276741ecf5SAndre Oppermann 	 * of available bandwith (the non-use of it) for wasting some
4286741ecf5SAndre Oppermann 	 * socket buffer memory.
4296741ecf5SAndre Oppermann 	 *
4306741ecf5SAndre Oppermann 	 * TODO: Shrink send buffer during idle periods together
4316741ecf5SAndre Oppermann 	 * with congestion window.  Requires another timer.  Has to
4326741ecf5SAndre Oppermann 	 * wait for upcoming tcp timer rewrite.
4336741ecf5SAndre Oppermann 	 */
4346741ecf5SAndre Oppermann 	if (tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
4356741ecf5SAndre Oppermann 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
4366741ecf5SAndre Oppermann 		    so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
4376741ecf5SAndre Oppermann 		    so->so_snd.sb_cc < tcp_autosndbuf_max &&
4386741ecf5SAndre Oppermann 		    sendwin >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
4396741ecf5SAndre Oppermann 			if (!sbreserve_locked(&so->so_snd,
4406741ecf5SAndre Oppermann 			    min(so->so_snd.sb_hiwat + tcp_autosndbuf_inc,
4416741ecf5SAndre Oppermann 			     tcp_autosndbuf_max), so, curthread))
4426741ecf5SAndre Oppermann 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
4436741ecf5SAndre Oppermann 		}
4446741ecf5SAndre Oppermann 	}
4456741ecf5SAndre Oppermann 
4466741ecf5SAndre Oppermann 	/*
4476741ecf5SAndre Oppermann 	 * Truncate to the maximum segment length or enable TCP Segmentation
4486741ecf5SAndre Oppermann 	 * Offloading (if supported by hardware) and ensure that FIN is removed
4496741ecf5SAndre Oppermann 	 * if the length no longer contains the last data byte.
450b3c0f300SAndre Oppermann 	 *
451b3c0f300SAndre Oppermann 	 * TSO may only be used if we are in a pure bulk sending state.  The
452b3c0f300SAndre Oppermann 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and
453b3c0f300SAndre Oppermann 	 * IP options prevent using TSO.  With TSO the TCP header is the same
454b3c0f300SAndre Oppermann 	 * (except for the sequence number) for all generated packets.  This
455b3c0f300SAndre Oppermann 	 * makes it impossible to transmit any options which vary per generated
456b3c0f300SAndre Oppermann 	 * segment or packet.
457b3c0f300SAndre Oppermann 	 *
458b3c0f300SAndre Oppermann 	 * The length of TSO bursts is limited to TCP_MAXWIN.  That limit and
459b3c0f300SAndre Oppermann 	 * removal of FIN (if not already catched here) are handled later after
460b3c0f300SAndre Oppermann 	 * the exact length of the TCP options are known.
46128257b5cSMatthew Dillon 	 */
462df8bae1dSRodney W. Grimes 	if (len > tp->t_maxseg) {
463b3c0f300SAndre Oppermann 		if ((tp->t_flags & TF_TSO) && tcp_do_tso &&
464b3c0f300SAndre Oppermann 		    ((tp->t_flags & TF_SIGNATURE) == 0) &&
465b3c0f300SAndre Oppermann 		    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
466b3c0f300SAndre Oppermann 		    tp->t_inpcb->inp_options == NULL &&
467b3c0f300SAndre Oppermann 		    tp->t_inpcb->in6p_options == NULL &&
468b3c0f300SAndre Oppermann 		    tp->t_inpcb->inp_sp == NULL) {
469b3c0f300SAndre Oppermann 			tso = 1;
470b3c0f300SAndre Oppermann 		} else {
471df8bae1dSRodney W. Grimes 			len = tp->t_maxseg;
472df8bae1dSRodney W. Grimes 			sendalot = 1;
473b3c0f300SAndre Oppermann 			tso = 0;
474b3c0f300SAndre Oppermann 		}
475df8bae1dSRodney W. Grimes 	}
4765d3b1b75SJayanth Vijayaraghavan 	if (sack_rxmit) {
4775d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
478df8bae1dSRodney W. Grimes 			flags &= ~TH_FIN;
4795d3b1b75SJayanth Vijayaraghavan 	} else {
4805d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
4815d3b1b75SJayanth Vijayaraghavan 			flags &= ~TH_FIN;
4825d3b1b75SJayanth Vijayaraghavan 	}
483df8bae1dSRodney W. Grimes 
484201d185bSAndre Oppermann 	recwin = sbspace(&so->so_rcv);
485df8bae1dSRodney W. Grimes 
486df8bae1dSRodney W. Grimes 	/*
487262c1c1aSMatthew Dillon 	 * Sender silly window avoidance.   We transmit under the following
488262c1c1aSMatthew Dillon 	 * conditions when len is non-zero:
489262c1c1aSMatthew Dillon 	 *
490b3c0f300SAndre Oppermann 	 *	- We have a full segment (or more with TSO)
491262c1c1aSMatthew Dillon 	 *	- This is the last buffer in a write()/send() and we are
492262c1c1aSMatthew Dillon 	 *	  either idle or running NODELAY
493262c1c1aSMatthew Dillon 	 *	- we've timed out (e.g. persist timer)
494262c1c1aSMatthew Dillon 	 *	- we have more then 1/2 the maximum send window's worth of
495262c1c1aSMatthew Dillon 	 *	  data (receiver may be limited the window size)
496262c1c1aSMatthew Dillon 	 *	- we need to retransmit
497df8bae1dSRodney W. Grimes 	 */
498df8bae1dSRodney W. Grimes 	if (len) {
499b3c0f300SAndre Oppermann 		if (len >= tp->t_maxseg)
500df8bae1dSRodney W. Grimes 			goto send;
501262c1c1aSMatthew Dillon 		/*
502262c1c1aSMatthew Dillon 		 * NOTE! on localhost connections an 'ack' from the remote
503262c1c1aSMatthew Dillon 		 * end may occur synchronously with the output and cause
504262c1c1aSMatthew Dillon 		 * us to flush a buffer queued with moretocome.  XXX
505262c1c1aSMatthew Dillon 		 *
506262c1c1aSMatthew Dillon 		 * note: the len + off check is almost certainly unnecessary.
507262c1c1aSMatthew Dillon 		 */
508262c1c1aSMatthew Dillon 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
509262c1c1aSMatthew Dillon 		    (idle || (tp->t_flags & TF_NODELAY)) &&
510262c1c1aSMatthew Dillon 		    len + off >= so->so_snd.sb_cc &&
511262c1c1aSMatthew Dillon 		    (tp->t_flags & TF_NOPUSH) == 0) {
512df8bae1dSRodney W. Grimes 			goto send;
513262c1c1aSMatthew Dillon 		}
5142cdbfa66SPaul Saab 		if (tp->t_flags & TF_FORCEDATA)		/* typ. timeout case */
515df8bae1dSRodney W. Grimes 			goto send;
516a0292f23SGarrett Wollman 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
517df8bae1dSRodney W. Grimes 			goto send;
518262c1c1aSMatthew Dillon 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))	/* retransmit case */
519df8bae1dSRodney W. Grimes 			goto send;
5206d90faf3SPaul Saab 		if (sack_rxmit)
5216d90faf3SPaul Saab 			goto send;
522df8bae1dSRodney W. Grimes 	}
523df8bae1dSRodney W. Grimes 
524df8bae1dSRodney W. Grimes 	/*
525df8bae1dSRodney W. Grimes 	 * Compare available window to amount of window
526df8bae1dSRodney W. Grimes 	 * known to peer (as advertised window less
527df8bae1dSRodney W. Grimes 	 * next expected input).  If the difference is at least two
528df8bae1dSRodney W. Grimes 	 * max size segments, or at least 50% of the maximum possible
529df8bae1dSRodney W. Grimes 	 * window, then want to send a window update to peer.
5303bfd6421SJonathan Lemon 	 * Skip this if the connection is in T/TCP half-open state.
531df8bae1dSRodney W. Grimes 	 */
532201d185bSAndre Oppermann 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) {
533df8bae1dSRodney W. Grimes 		/*
534df8bae1dSRodney W. Grimes 		 * "adv" is the amount we can increase the window,
535df8bae1dSRodney W. Grimes 		 * taking into account that we are limited by
536df8bae1dSRodney W. Grimes 		 * TCP_MAXWIN << tp->rcv_scale.
537df8bae1dSRodney W. Grimes 		 */
538201d185bSAndre Oppermann 		long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
539df8bae1dSRodney W. Grimes 			(tp->rcv_adv - tp->rcv_nxt);
540df8bae1dSRodney W. Grimes 
541df8bae1dSRodney W. Grimes 		if (adv >= (long) (2 * tp->t_maxseg))
542df8bae1dSRodney W. Grimes 			goto send;
543df8bae1dSRodney W. Grimes 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
544df8bae1dSRodney W. Grimes 			goto send;
545df8bae1dSRodney W. Grimes 	}
546df8bae1dSRodney W. Grimes 
547df8bae1dSRodney W. Grimes 	/*
54828257b5cSMatthew Dillon 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
54928257b5cSMatthew Dillon 	 * is also a catch-all for the retransmit timer timeout case.
550df8bae1dSRodney W. Grimes 	 */
551df8bae1dSRodney W. Grimes 	if (tp->t_flags & TF_ACKNOW)
552df8bae1dSRodney W. Grimes 		goto send;
553a0292f23SGarrett Wollman 	if ((flags & TH_RST) ||
554a0292f23SGarrett Wollman 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
555df8bae1dSRodney W. Grimes 		goto send;
556df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_una))
557df8bae1dSRodney W. Grimes 		goto send;
558df8bae1dSRodney W. Grimes 	/*
559df8bae1dSRodney W. Grimes 	 * If our state indicates that FIN should be sent
56028257b5cSMatthew Dillon 	 * and we have not yet done so, then we need to send.
561df8bae1dSRodney W. Grimes 	 */
562df8bae1dSRodney W. Grimes 	if (flags & TH_FIN &&
563df8bae1dSRodney W. Grimes 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
564df8bae1dSRodney W. Grimes 		goto send;
5656d90faf3SPaul Saab 	/*
5666d90faf3SPaul Saab 	 * In SACK, it is possible for tcp_output to fail to send a segment
5676d90faf3SPaul Saab 	 * after the retransmission timer has been turned off.  Make sure
5686d90faf3SPaul Saab 	 * that the retransmission timer is set.
5696d90faf3SPaul Saab 	 */
5706d90faf3SPaul Saab 	if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) &&
5716d90faf3SPaul Saab 	    !callout_active(tp->tt_rexmt) &&
5726d90faf3SPaul Saab 	    !callout_active(tp->tt_persist)) {
5736d90faf3SPaul Saab 		callout_reset(tp->tt_rexmt, tp->t_rxtcur,
5746d90faf3SPaul Saab 			      tcp_timer_rexmt, tp);
575cf2942b6SRobert Watson 		goto just_return;
5766d90faf3SPaul Saab 	}
577df8bae1dSRodney W. Grimes 	/*
578df8bae1dSRodney W. Grimes 	 * TCP window updates are not reliable, rather a polling protocol
579df8bae1dSRodney W. Grimes 	 * using ``persist'' packets is used to insure receipt of window
580df8bae1dSRodney W. Grimes 	 * updates.  The three ``states'' for the output side are:
581df8bae1dSRodney W. Grimes 	 *	idle			not doing retransmits or persists
582df8bae1dSRodney W. Grimes 	 *	persisting		to move a small or zero window
583df8bae1dSRodney W. Grimes 	 *	(re)transmitting	and thereby not persisting
584df8bae1dSRodney W. Grimes 	 *
5859b8b58e0SJonathan Lemon 	 * callout_active(tp->tt_persist)
5869b8b58e0SJonathan Lemon 	 *	is true when we are in persist state.
5872cdbfa66SPaul Saab 	 * (tp->t_flags & TF_FORCEDATA)
588df8bae1dSRodney W. Grimes 	 *	is set when we are called to send a persist packet.
5899b8b58e0SJonathan Lemon 	 * callout_active(tp->tt_rexmt)
590df8bae1dSRodney W. Grimes 	 *	is set when we are retransmitting
591df8bae1dSRodney W. Grimes 	 * The output side is idle when both timers are zero.
592df8bae1dSRodney W. Grimes 	 *
593df8bae1dSRodney W. Grimes 	 * If send window is too small, there is data to transmit, and no
594df8bae1dSRodney W. Grimes 	 * retransmit or persist is pending, then go to persist state.
595df8bae1dSRodney W. Grimes 	 * If nothing happens soon, send when timer expires:
596df8bae1dSRodney W. Grimes 	 * if window is nonzero, transmit what we can,
597df8bae1dSRodney W. Grimes 	 * otherwise force out a byte.
598df8bae1dSRodney W. Grimes 	 */
5999b8b58e0SJonathan Lemon 	if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) &&
6009b8b58e0SJonathan Lemon 	    !callout_active(tp->tt_persist)) {
601df8bae1dSRodney W. Grimes 		tp->t_rxtshift = 0;
602df8bae1dSRodney W. Grimes 		tcp_setpersist(tp);
603df8bae1dSRodney W. Grimes 	}
604df8bae1dSRodney W. Grimes 
605df8bae1dSRodney W. Grimes 	/*
606df8bae1dSRodney W. Grimes 	 * No reason to send a segment, just return.
607df8bae1dSRodney W. Grimes 	 */
608cf2942b6SRobert Watson just_return:
609cf2942b6SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
610df8bae1dSRodney W. Grimes 	return (0);
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes send:
613cf2942b6SRobert Watson 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
614df8bae1dSRodney W. Grimes 	/*
615df8bae1dSRodney W. Grimes 	 * Before ESTABLISHED, force sending of initial options
616df8bae1dSRodney W. Grimes 	 * unless TCP set not to do any options.
617df8bae1dSRodney W. Grimes 	 * NOTE: we assume that the IP/TCP header plus TCP options
618df8bae1dSRodney W. Grimes 	 * always fit in a single mbuf, leaving room for a maximum
619df8bae1dSRodney W. Grimes 	 * link header, i.e.
62033841545SHajimu UMEMOTO 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
621df8bae1dSRodney W. Grimes 	 */
622df8bae1dSRodney W. Grimes 	optlen = 0;
623fb59c426SYoshinobu Inoue #ifdef INET6
624fb59c426SYoshinobu Inoue 	if (isipv6)
625fb59c426SYoshinobu Inoue 		hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
626fb59c426SYoshinobu Inoue 	else
627fb59c426SYoshinobu Inoue #endif
628df8bae1dSRodney W. Grimes 	hdrlen = sizeof (struct tcpiphdr);
629df8bae1dSRodney W. Grimes 	if (flags & TH_SYN) {
630df8bae1dSRodney W. Grimes 		tp->snd_nxt = tp->iss;
631df8bae1dSRodney W. Grimes 		if ((tp->t_flags & TF_NOOPT) == 0) {
632df8bae1dSRodney W. Grimes 			u_short mss;
633df8bae1dSRodney W. Grimes 
634df8bae1dSRodney W. Grimes 			opt[0] = TCPOPT_MAXSEG;
635a0292f23SGarrett Wollman 			opt[1] = TCPOLEN_MAXSEG;
63697d8d152SAndre Oppermann 			mss = htons((u_short) tcp_mssopt(&tp->t_inpcb->inp_inc));
63794a5d9b6SDavid Greenman 			(void)memcpy(opt + 2, &mss, sizeof(mss));
638a0292f23SGarrett Wollman 			optlen = TCPOLEN_MAXSEG;
639df8bae1dSRodney W. Grimes 
640df8bae1dSRodney W. Grimes 			if ((tp->t_flags & TF_REQ_SCALE) &&
641df8bae1dSRodney W. Grimes 			    ((flags & TH_ACK) == 0 ||
642df8bae1dSRodney W. Grimes 			    (tp->t_flags & TF_RCVD_SCALE))) {
6439105bb46SBruce Evans 				*((u_int32_t *)(opt + optlen)) = htonl(
644df8bae1dSRodney W. Grimes 					TCPOPT_NOP << 24 |
645df8bae1dSRodney W. Grimes 					TCPOPT_WINDOW << 16 |
646df8bae1dSRodney W. Grimes 					TCPOLEN_WINDOW << 8 |
647df8bae1dSRodney W. Grimes 					tp->request_r_scale);
648df8bae1dSRodney W. Grimes 				optlen += 4;
649df8bae1dSRodney W. Grimes 			}
650df8bae1dSRodney W. Grimes 		}
651df8bae1dSRodney W. Grimes 	}
652df8bae1dSRodney W. Grimes 
653df8bae1dSRodney W. Grimes 	/*
654df8bae1dSRodney W. Grimes 	 * Send a timestamp and echo-reply if this is a SYN and our side
655df8bae1dSRodney W. Grimes 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
656df8bae1dSRodney W. Grimes 	 * and our peer have sent timestamps in our SYN's.
657df8bae1dSRodney W. Grimes 	 */
658df8bae1dSRodney W. Grimes 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
659df8bae1dSRodney W. Grimes 	    (flags & TH_RST) == 0 &&
660a0292f23SGarrett Wollman 	    ((flags & TH_ACK) == 0 ||
661df8bae1dSRodney W. Grimes 	     (tp->t_flags & TF_RCVD_TSTMP))) {
6629105bb46SBruce Evans 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
663df8bae1dSRodney W. Grimes 
664df8bae1dSRodney W. Grimes 		/* Form timestamp option as shown in appendix A of RFC 1323. */
665df8bae1dSRodney W. Grimes 		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
666bf6d304aSAndre Oppermann 		*lp++ = htonl(ticks + tp->ts_offset);
667df8bae1dSRodney W. Grimes 		*lp   = htonl(tp->ts_recent);
668df8bae1dSRodney W. Grimes 		optlen += TCPOLEN_TSTAMP_APPA;
669df8bae1dSRodney W. Grimes 	}
670df8bae1dSRodney W. Grimes 
6716741ecf5SAndre Oppermann 	/* Set receive buffer autosizing timestamp. */
6726741ecf5SAndre Oppermann 	if (tp->rfbuf_ts == 0 && (so->so_rcv.sb_flags & SB_AUTOSIZE))
6736741ecf5SAndre Oppermann 		tp->rfbuf_ts = ticks;
6746741ecf5SAndre Oppermann 
6751cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
6761cfd4b53SBruce M Simpson #ifdef INET6
6771cfd4b53SBruce M Simpson 	if (!isipv6)
6781cfd4b53SBruce M Simpson #endif
6791cfd4b53SBruce M Simpson 	if (tp->t_flags & TF_SIGNATURE) {
6801cfd4b53SBruce M Simpson 		int i;
6811cfd4b53SBruce M Simpson 		u_char *bp;
682bca0e5bfSBruce M Simpson 
683bca0e5bfSBruce M Simpson 		/* Initialize TCP-MD5 option (RFC2385) */
6841cfd4b53SBruce M Simpson 		bp = (u_char *)opt + optlen;
6851cfd4b53SBruce M Simpson 		*bp++ = TCPOPT_SIGNATURE;
6861cfd4b53SBruce M Simpson 		*bp++ = TCPOLEN_SIGNATURE;
6871cfd4b53SBruce M Simpson 		sigoff = optlen + 2;
6881cfd4b53SBruce M Simpson 		for (i = 0; i < TCP_SIGLEN; i++)
6891cfd4b53SBruce M Simpson 			*bp++ = 0;
6901cfd4b53SBruce M Simpson 		optlen += TCPOLEN_SIGNATURE;
6911cfd4b53SBruce M Simpson 	}
6921cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
6931cfd4b53SBruce M Simpson 
694be3f3b5eSPaul Saab 	if (tp->sack_enable && ((tp->t_flags & TF_NOOPT) == 0)) {
695be3f3b5eSPaul Saab 		/*
696be3f3b5eSPaul Saab 		 * Tack on the SACK permitted option *last*.
697be3f3b5eSPaul Saab 		 * And do padding of options after tacking this on.
698be3f3b5eSPaul Saab 		 * This is because of MSS, TS, WinScale and Signatures are
699be3f3b5eSPaul Saab 		 * all present, we have just 2 bytes left for the SACK
700be3f3b5eSPaul Saab 		 * permitted option, which is just enough.
701be3f3b5eSPaul Saab 		 */
702be3f3b5eSPaul Saab 		/*
703be3f3b5eSPaul Saab 		 * If this is the first SYN of connection (not a SYN
704be3f3b5eSPaul Saab 		 * ACK), include SACK permitted option.  If this is a
705be3f3b5eSPaul Saab 		 * SYN ACK, include SACK permitted option if peer has
706be3f3b5eSPaul Saab 		 * already done so. This is only for active connect,
707be3f3b5eSPaul Saab 		 * since the syncache takes care of the passive connect.
708be3f3b5eSPaul Saab 		 */
709be3f3b5eSPaul Saab 		if ((flags & TH_SYN) &&
710be3f3b5eSPaul Saab 		    (!(flags & TH_ACK) || (tp->t_flags & TF_SACK_PERMIT))) {
711be3f3b5eSPaul Saab 			u_char *bp;
712be3f3b5eSPaul Saab 			bp = (u_char *)opt + optlen;
713be3f3b5eSPaul Saab 
714be3f3b5eSPaul Saab 			*bp++ = TCPOPT_SACK_PERMITTED;
715be3f3b5eSPaul Saab 			*bp++ = TCPOLEN_SACK_PERMITTED;
716be3f3b5eSPaul Saab 			optlen += TCPOLEN_SACK_PERMITTED;
717be3f3b5eSPaul Saab 		}
718be3f3b5eSPaul Saab 
719be3f3b5eSPaul Saab 		/*
720be3f3b5eSPaul Saab 		 * Send SACKs if necessary.  This should be the last
721be3f3b5eSPaul Saab 		 * option processed.  Only as many SACKs are sent as
722be3f3b5eSPaul Saab 		 * are permitted by the maximum options size.
723be3f3b5eSPaul Saab 		 *
724be3f3b5eSPaul Saab 		 * In general, SACK blocks consume 8*n+2 bytes.
725be3f3b5eSPaul Saab 		 * So a full size SACK blocks option is 34 bytes
726be3f3b5eSPaul Saab 		 * (to generate 4 SACK blocks).  At a minimum,
727be3f3b5eSPaul Saab 		 * we need 10 bytes (to generate 1 SACK block).
728be3f3b5eSPaul Saab 		 * If TCP Timestamps (12 bytes) and TCP Signatures
729be3f3b5eSPaul Saab 		 * (18 bytes) are both present, we'll just have
730be3f3b5eSPaul Saab 		 * 10 bytes for SACK options 40 - (12 + 18).
731be3f3b5eSPaul Saab 		 */
732be3f3b5eSPaul Saab 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
733be3f3b5eSPaul Saab 		    (tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks > 0 &&
734be3f3b5eSPaul Saab 		    MAX_TCPOPTLEN - optlen - 2 >= TCPOLEN_SACK) {
735be3f3b5eSPaul Saab 			int nsack, sackoptlen, padlen;
736be3f3b5eSPaul Saab 			u_char *bp = (u_char *)opt + optlen;
737be3f3b5eSPaul Saab 			u_int32_t *lp;
738be3f3b5eSPaul Saab 
739be3f3b5eSPaul Saab 			nsack = (MAX_TCPOPTLEN - optlen - 2) / TCPOLEN_SACK;
740be3f3b5eSPaul Saab 			nsack = min(nsack, tp->rcv_numsacks);
741be3f3b5eSPaul Saab 			sackoptlen = (2 + nsack * TCPOLEN_SACK);
742be3f3b5eSPaul Saab 
743be3f3b5eSPaul Saab 			/*
744be3f3b5eSPaul Saab 			 * First we need to pad options so that the
745be3f3b5eSPaul Saab 			 * SACK blocks can start at a 4-byte boundary
746be3f3b5eSPaul Saab 			 * (sack option and length are at a 2 byte offset).
747be3f3b5eSPaul Saab 			 */
748be3f3b5eSPaul Saab 			padlen = (MAX_TCPOPTLEN - optlen - sackoptlen) % 4;
749be3f3b5eSPaul Saab 			optlen += padlen;
750be3f3b5eSPaul Saab 			while (padlen-- > 0)
751be3f3b5eSPaul Saab 				*bp++ = TCPOPT_NOP;
752be3f3b5eSPaul Saab 
753be3f3b5eSPaul Saab 			tcpstat.tcps_sack_send_blocks++;
754be3f3b5eSPaul Saab 			*bp++ = TCPOPT_SACK;
755be3f3b5eSPaul Saab 			*bp++ = sackoptlen;
756be3f3b5eSPaul Saab 			lp = (u_int32_t *)bp;
757be3f3b5eSPaul Saab 			for (i = 0; i < nsack; i++) {
758be3f3b5eSPaul Saab 				struct sackblk sack = tp->sackblks[i];
759be3f3b5eSPaul Saab 				*lp++ = htonl(sack.start);
760be3f3b5eSPaul Saab 				*lp++ = htonl(sack.end);
761be3f3b5eSPaul Saab 			}
762be3f3b5eSPaul Saab 			optlen += sackoptlen;
763be3f3b5eSPaul Saab 		}
764be3f3b5eSPaul Saab 	}
765be3f3b5eSPaul Saab 
766be3f3b5eSPaul Saab 	/* Pad TCP options to a 4 byte boundary */
767be3f3b5eSPaul Saab 	if (optlen < MAX_TCPOPTLEN && (optlen % sizeof(u_int32_t))) {
768be3f3b5eSPaul Saab 		int pad = sizeof(u_int32_t) - (optlen % sizeof(u_int32_t));
769be3f3b5eSPaul Saab 		u_char *bp = (u_char *)opt + optlen;
770be3f3b5eSPaul Saab 
771be3f3b5eSPaul Saab 		optlen += pad;
772be3f3b5eSPaul Saab 		while (pad) {
773be3f3b5eSPaul Saab 			*bp++ = TCPOPT_EOL;
774be3f3b5eSPaul Saab 			pad--;
775be3f3b5eSPaul Saab 		}
776be3f3b5eSPaul Saab 	}
777be3f3b5eSPaul Saab 
778df8bae1dSRodney W. Grimes 	hdrlen += optlen;
779df8bae1dSRodney W. Grimes 
780fb59c426SYoshinobu Inoue #ifdef INET6
781fb59c426SYoshinobu Inoue 	if (isipv6)
782fb59c426SYoshinobu Inoue 		ipoptlen = ip6_optlen(tp->t_inpcb);
783fb59c426SYoshinobu Inoue 	else
784fb59c426SYoshinobu Inoue #endif
785f10e85d7SLuigi Rizzo 	if (tp->t_inpcb->inp_options)
786a04884fcSBill Fenner 		ipoptlen = tp->t_inpcb->inp_options->m_len -
787a04884fcSBill Fenner 				offsetof(struct ipoption, ipopt_list);
788f10e85d7SLuigi Rizzo 	else
789a04884fcSBill Fenner 		ipoptlen = 0;
790fb59c426SYoshinobu Inoue #ifdef IPSEC
791fb59c426SYoshinobu Inoue 	ipoptlen += ipsec_hdrsiz_tcp(tp);
792fb59c426SYoshinobu Inoue #endif
793a04884fcSBill Fenner 
794df8bae1dSRodney W. Grimes 	/*
795df8bae1dSRodney W. Grimes 	 * Adjust data length if insertion of options will
796a0292f23SGarrett Wollman 	 * bump the packet length beyond the t_maxopd length.
797a0292f23SGarrett Wollman 	 * Clear the FIN bit because we cut off the tail of
798a0292f23SGarrett Wollman 	 * the segment.
799b3c0f300SAndre Oppermann 	 *
80031ecb34aSAndre Oppermann 	 * When doing TSO limit a burst to TCP_MAXWIN minus the
80131ecb34aSAndre Oppermann 	 * IP, TCP and Options length to keep ip->ip_len from
80231ecb34aSAndre Oppermann 	 * overflowing.  Prevent the last segment from being
80331ecb34aSAndre Oppermann 	 * fractional thus making them all equal sized and set
80431ecb34aSAndre Oppermann 	 * the flag to continue sending.
805df8bae1dSRodney W. Grimes 	 */
806a04884fcSBill Fenner 	if (len + optlen + ipoptlen > tp->t_maxopd) {
807297a37f3SDavid Greenman 		flags &= ~TH_FIN;
808b3c0f300SAndre Oppermann 		if (tso) {
8096a2257d9SAndre Oppermann 			if (len > TCP_MAXWIN - hdrlen) {
81031ecb34aSAndre Oppermann 				len = TCP_MAXWIN - hdrlen;
81131ecb34aSAndre Oppermann 				len = len - (len % (tp->t_maxopd - optlen));
812b3c0f300SAndre Oppermann 				sendalot = 1;
813b3c0f300SAndre Oppermann 			} else if (tp->t_flags & TF_NEEDFIN)
814b3c0f300SAndre Oppermann 				sendalot = 1;
815b3c0f300SAndre Oppermann 		} else {
816a04884fcSBill Fenner 			len = tp->t_maxopd - optlen - ipoptlen;
817df8bae1dSRodney W. Grimes 			sendalot = 1;
818df8bae1dSRodney W. Grimes 		}
819b3c0f300SAndre Oppermann 	}
820df8bae1dSRodney W. Grimes 
821a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/
822a683a7ddSYoshinobu Inoue #ifdef INET6
823a683a7ddSYoshinobu Inoue 	if (max_linkhdr + hdrlen > MCLBYTES)
824a683a7ddSYoshinobu Inoue #else
825df8bae1dSRodney W. Grimes 	if (max_linkhdr + hdrlen > MHLEN)
826a683a7ddSYoshinobu Inoue #endif
827f10e85d7SLuigi Rizzo 		panic("tcphdr too big");
828a0292f23SGarrett Wollman /*#endif*/
829df8bae1dSRodney W. Grimes 
830df8bae1dSRodney W. Grimes 	/*
831df8bae1dSRodney W. Grimes 	 * Grab a header mbuf, attaching a copy of data to
832df8bae1dSRodney W. Grimes 	 * be transmitted, and initialize the header from
833df8bae1dSRodney W. Grimes 	 * the template for sends on this connection.
834df8bae1dSRodney W. Grimes 	 */
835df8bae1dSRodney W. Grimes 	if (len) {
8362cdbfa66SPaul Saab 		if ((tp->t_flags & TF_FORCEDATA) && len == 1)
837df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndprobe++;
838df8bae1dSRodney W. Grimes 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
839df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitpack++;
840df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitbyte += len;
841df8bae1dSRodney W. Grimes 		} else {
842df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndpack++;
843df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndbyte += len;
844df8bae1dSRodney W. Grimes 		}
845df8bae1dSRodney W. Grimes #ifdef notyet
846df8bae1dSRodney W. Grimes 		if ((m = m_copypack(so->so_snd.sb_mb, off,
847df8bae1dSRodney W. Grimes 		    (int)len, max_linkhdr + hdrlen)) == 0) {
848cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
849df8bae1dSRodney W. Grimes 			error = ENOBUFS;
850df8bae1dSRodney W. Grimes 			goto out;
851df8bae1dSRodney W. Grimes 		}
852df8bae1dSRodney W. Grimes 		/*
853df8bae1dSRodney W. Grimes 		 * m_copypack left space for our hdr; use it.
854df8bae1dSRodney W. Grimes 		 */
855df8bae1dSRodney W. Grimes 		m->m_len += hdrlen;
856df8bae1dSRodney W. Grimes 		m->m_data -= hdrlen;
857df8bae1dSRodney W. Grimes #else
85834333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
859df8bae1dSRodney W. Grimes 		if (m == NULL) {
860cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
861df8bae1dSRodney W. Grimes 			error = ENOBUFS;
862df8bae1dSRodney W. Grimes 			goto out;
863df8bae1dSRodney W. Grimes 		}
864fb59c426SYoshinobu Inoue #ifdef INET6
865a683a7ddSYoshinobu Inoue 		if (MHLEN < hdrlen + max_linkhdr) {
866a163d034SWarner Losh 			MCLGET(m, M_DONTWAIT);
867a683a7ddSYoshinobu Inoue 			if ((m->m_flags & M_EXT) == 0) {
868cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
869a683a7ddSYoshinobu Inoue 				m_freem(m);
870a683a7ddSYoshinobu Inoue 				error = ENOBUFS;
871a683a7ddSYoshinobu Inoue 				goto out;
872a683a7ddSYoshinobu Inoue 			}
873a683a7ddSYoshinobu Inoue 		}
874fb59c426SYoshinobu Inoue #endif
875df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
876df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
877df8bae1dSRodney W. Grimes 		if (len <= MHLEN - hdrlen - max_linkhdr) {
878df8bae1dSRodney W. Grimes 			m_copydata(so->so_snd.sb_mb, off, (int) len,
879df8bae1dSRodney W. Grimes 			    mtod(m, caddr_t) + hdrlen);
880df8bae1dSRodney W. Grimes 			m->m_len += len;
881df8bae1dSRodney W. Grimes 		} else {
882df8bae1dSRodney W. Grimes 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
88351823c3aSGarrett Wollman 			if (m->m_next == 0) {
884cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
885d7f570e6SGarrett Wollman 				(void) m_free(m);
88651823c3aSGarrett Wollman 				error = ENOBUFS;
88751823c3aSGarrett Wollman 				goto out;
88851823c3aSGarrett Wollman 			}
889df8bae1dSRodney W. Grimes 		}
890df8bae1dSRodney W. Grimes #endif
891df8bae1dSRodney W. Grimes 		/*
892df8bae1dSRodney W. Grimes 		 * If we're sending everything we've got, set PUSH.
893df8bae1dSRodney W. Grimes 		 * (This will keep happy those implementations which only
894df8bae1dSRodney W. Grimes 		 * give data to the user when a buffer fills or
895df8bae1dSRodney W. Grimes 		 * a PUSH comes in.)
896df8bae1dSRodney W. Grimes 		 */
897df8bae1dSRodney W. Grimes 		if (off + len == so->so_snd.sb_cc)
898df8bae1dSRodney W. Grimes 			flags |= TH_PUSH;
899cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
900df8bae1dSRodney W. Grimes 	} else {
901cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
902df8bae1dSRodney W. Grimes 		if (tp->t_flags & TF_ACKNOW)
903df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndacks++;
904df8bae1dSRodney W. Grimes 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
905df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndctrl++;
906df8bae1dSRodney W. Grimes 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
907df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndurg++;
908df8bae1dSRodney W. Grimes 		else
909df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndwinup++;
910df8bae1dSRodney W. Grimes 
91134333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
912df8bae1dSRodney W. Grimes 		if (m == NULL) {
913df8bae1dSRodney W. Grimes 			error = ENOBUFS;
914df8bae1dSRodney W. Grimes 			goto out;
915df8bae1dSRodney W. Grimes 		}
916fb59c426SYoshinobu Inoue #ifdef INET6
917fb59c426SYoshinobu Inoue 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
918fb59c426SYoshinobu Inoue 		    MHLEN >= hdrlen) {
919fb59c426SYoshinobu Inoue 			MH_ALIGN(m, hdrlen);
920fb59c426SYoshinobu Inoue 		} else
921fb59c426SYoshinobu Inoue #endif
922df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
923df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
924df8bae1dSRodney W. Grimes 	}
925cf2942b6SRobert Watson 	SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
926df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = (struct ifnet *)0;
927c488362eSRobert Watson #ifdef MAC
928c18b97c6SRobert Watson 	mac_create_mbuf_from_inpcb(tp->t_inpcb, m);
929c488362eSRobert Watson #endif
930fb59c426SYoshinobu Inoue #ifdef INET6
931fb59c426SYoshinobu Inoue 	if (isipv6) {
932fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
933fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip6 + 1);
93479909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip6, th);
935fb59c426SYoshinobu Inoue 	} else
936fb59c426SYoshinobu Inoue #endif /* INET6 */
937fb59c426SYoshinobu Inoue 	{
938fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
939fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
940fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip + 1);
94179909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip, th);
942fb59c426SYoshinobu Inoue 	}
943df8bae1dSRodney W. Grimes 
944df8bae1dSRodney W. Grimes 	/*
945df8bae1dSRodney W. Grimes 	 * Fill in fields, remembering maximum advertised
946df8bae1dSRodney W. Grimes 	 * window for use in delaying messages about window sizes.
947df8bae1dSRodney W. Grimes 	 * If resending a FIN, be sure not to use a new sequence number.
948df8bae1dSRodney W. Grimes 	 */
949df8bae1dSRodney W. Grimes 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
950df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max)
951df8bae1dSRodney W. Grimes 		tp->snd_nxt--;
952df8bae1dSRodney W. Grimes 	/*
953df8bae1dSRodney W. Grimes 	 * If we are doing retransmissions, then snd_nxt will
954df8bae1dSRodney W. Grimes 	 * not reflect the first unsent octet.  For ACK only
955df8bae1dSRodney W. Grimes 	 * packets, we do not want the sequence number of the
956df8bae1dSRodney W. Grimes 	 * retransmitted packet, we want the sequence number
957df8bae1dSRodney W. Grimes 	 * of the next unsent octet.  So, if there is no data
958df8bae1dSRodney W. Grimes 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
959df8bae1dSRodney W. Grimes 	 * when filling in ti_seq.  But if we are in persist
960df8bae1dSRodney W. Grimes 	 * state, snd_max might reflect one byte beyond the
961df8bae1dSRodney W. Grimes 	 * right edge of the window, so use snd_nxt in that
962df8bae1dSRodney W. Grimes 	 * case, since we know we aren't doing a retransmission.
963df8bae1dSRodney W. Grimes 	 * (retransmit and persist are mutually exclusive...)
964df8bae1dSRodney W. Grimes 	 */
965a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
9669b8b58e0SJonathan Lemon 		if (len || (flags & (TH_SYN|TH_FIN))
9679b8b58e0SJonathan Lemon 		    || callout_active(tp->tt_persist))
968fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_nxt);
969df8bae1dSRodney W. Grimes 		else
970fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_max);
971a55db2b6SPaul Saab 	} else {
9726d90faf3SPaul Saab 		th->th_seq = htonl(p->rxmit);
9736d90faf3SPaul Saab 		p->rxmit += len;
9740077b016SPaul Saab 		tp->sackhint.sack_bytes_rexmit += len;
9756d90faf3SPaul Saab 	}
976fb59c426SYoshinobu Inoue 	th->th_ack = htonl(tp->rcv_nxt);
977df8bae1dSRodney W. Grimes 	if (optlen) {
978fb59c426SYoshinobu Inoue 		bcopy(opt, th + 1, optlen);
979fb59c426SYoshinobu Inoue 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
980df8bae1dSRodney W. Grimes 	}
981fb59c426SYoshinobu Inoue 	th->th_flags = flags;
982df8bae1dSRodney W. Grimes 	/*
983df8bae1dSRodney W. Grimes 	 * Calculate receive window.  Don't shrink window,
984df8bae1dSRodney W. Grimes 	 * but avoid silly window syndrome.
985df8bae1dSRodney W. Grimes 	 */
986201d185bSAndre Oppermann 	if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
987201d185bSAndre Oppermann 	    recwin < (long)tp->t_maxseg)
988201d185bSAndre Oppermann 		recwin = 0;
989201d185bSAndre Oppermann 	if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
990201d185bSAndre Oppermann 		recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
991201d185bSAndre Oppermann 	if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
992201d185bSAndre Oppermann 		recwin = (long)TCP_MAXWIN << tp->rcv_scale;
993201d185bSAndre Oppermann 	th->th_win = htons((u_short) (recwin >> tp->rcv_scale));
994262c1c1aSMatthew Dillon 
995262c1c1aSMatthew Dillon 
996262c1c1aSMatthew Dillon 	/*
997262c1c1aSMatthew Dillon 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised
998262c1c1aSMatthew Dillon 	 * a 0 window.  This may cause the remote transmitter to stall.  This
999262c1c1aSMatthew Dillon 	 * flag tells soreceive() to disable delayed acknowledgements when
1000262c1c1aSMatthew Dillon 	 * draining the buffer.  This can occur if the receiver is attempting
1001262c1c1aSMatthew Dillon 	 * to read more data then can be buffered prior to transmitting on
1002262c1c1aSMatthew Dillon 	 * the connection.
1003262c1c1aSMatthew Dillon 	 */
1004201d185bSAndre Oppermann 	if (recwin == 0)
1005262c1c1aSMatthew Dillon 		tp->t_flags |= TF_RXWIN0SENT;
1006262c1c1aSMatthew Dillon 	else
1007262c1c1aSMatthew Dillon 		tp->t_flags &= ~TF_RXWIN0SENT;
1008df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1009fb59c426SYoshinobu Inoue 		th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
1010fb59c426SYoshinobu Inoue 		th->th_flags |= TH_URG;
1011df8bae1dSRodney W. Grimes 	} else
1012df8bae1dSRodney W. Grimes 		/*
1013df8bae1dSRodney W. Grimes 		 * If no urgent pointer to send, then we pull
1014df8bae1dSRodney W. Grimes 		 * the urgent pointer to the left edge of the send window
1015df8bae1dSRodney W. Grimes 		 * so that it doesn't drift into the send window on sequence
1016df8bae1dSRodney W. Grimes 		 * number wraparound.
1017df8bae1dSRodney W. Grimes 		 */
1018df8bae1dSRodney W. Grimes 		tp->snd_up = tp->snd_una;		/* drag it along */
1019df8bae1dSRodney W. Grimes 
10201cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
10211cfd4b53SBruce M Simpson #ifdef INET6
10221cfd4b53SBruce M Simpson 	if (!isipv6)
10231cfd4b53SBruce M Simpson #endif
10241cfd4b53SBruce M Simpson 	if (tp->t_flags & TF_SIGNATURE)
1025265ed012SBruce M Simpson 		tcp_signature_compute(m, sizeof(struct ip), len, optlen,
10261cfd4b53SBruce M Simpson 		    (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
1027265ed012SBruce M Simpson #endif
10281cfd4b53SBruce M Simpson 
1029df8bae1dSRodney W. Grimes 	/*
1030df8bae1dSRodney W. Grimes 	 * Put TCP length in extended header, and then
1031df8bae1dSRodney W. Grimes 	 * checksum extended header and data.
1032df8bae1dSRodney W. Grimes 	 */
1033fb59c426SYoshinobu Inoue 	m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
1034fb59c426SYoshinobu Inoue #ifdef INET6
1035fb59c426SYoshinobu Inoue 	if (isipv6)
1036fb59c426SYoshinobu Inoue 		/*
1037fb59c426SYoshinobu Inoue 		 * ip6_plen is not need to be filled now, and will be filled
1038fb59c426SYoshinobu Inoue 		 * in ip6_output.
1039fb59c426SYoshinobu Inoue 		 */
1040fb59c426SYoshinobu Inoue 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
1041fb59c426SYoshinobu Inoue 				       sizeof(struct tcphdr) + optlen + len);
1042fb59c426SYoshinobu Inoue 	else
1043fb59c426SYoshinobu Inoue #endif /* INET6 */
1044fb59c426SYoshinobu Inoue 	{
1045db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_TCP;
1046db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
104779909384SJonathan Lemon 		th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
104879909384SJonathan Lemon 		    htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
1049fb59c426SYoshinobu Inoue 
1050db4f9cc7SJonathan Lemon 		/* IP version must be set here for ipv4/ipv6 checking later */
1051db4f9cc7SJonathan Lemon 		KASSERT(ip->ip_v == IPVERSION,
10526e551fb6SDavid E. O'Brien 		    ("%s: IP version incorrect: %d", __func__, ip->ip_v));
1053fb59c426SYoshinobu Inoue 	}
1054df8bae1dSRodney W. Grimes 
1055df8bae1dSRodney W. Grimes 	/*
1056b3c0f300SAndre Oppermann 	 * Enable TSO and specify the size of the segments.
1057b3c0f300SAndre Oppermann 	 * The TCP pseudo header checksum is always provided.
1058b3c0f300SAndre Oppermann 	 * XXX: Fixme: This is currently not the case for IPv6.
1059b3c0f300SAndre Oppermann 	 */
1060b3c0f300SAndre Oppermann 	if (tso) {
1061b3c0f300SAndre Oppermann 		m->m_pkthdr.csum_flags = CSUM_TSO;
1062b3c0f300SAndre Oppermann 		m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen;
1063b3c0f300SAndre Oppermann 	}
1064b3c0f300SAndre Oppermann 
1065b3c0f300SAndre Oppermann 	/*
1066df8bae1dSRodney W. Grimes 	 * In transmit state, time the transmission and arrange for
1067df8bae1dSRodney W. Grimes 	 * the retransmit.  In persist state, just set snd_max.
1068df8bae1dSRodney W. Grimes 	 */
10692cdbfa66SPaul Saab 	if ((tp->t_flags & TF_FORCEDATA) == 0 ||
10702cdbfa66SPaul Saab 	    !callout_active(tp->tt_persist)) {
1071df8bae1dSRodney W. Grimes 		tcp_seq startseq = tp->snd_nxt;
1072df8bae1dSRodney W. Grimes 
1073df8bae1dSRodney W. Grimes 		/*
1074df8bae1dSRodney W. Grimes 		 * Advance snd_nxt over sequence space of this segment.
1075df8bae1dSRodney W. Grimes 		 */
1076df8bae1dSRodney W. Grimes 		if (flags & (TH_SYN|TH_FIN)) {
1077df8bae1dSRodney W. Grimes 			if (flags & TH_SYN)
1078df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
1079df8bae1dSRodney W. Grimes 			if (flags & TH_FIN) {
1080df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
1081df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_SENTFIN;
1082df8bae1dSRodney W. Grimes 			}
1083df8bae1dSRodney W. Grimes 		}
1084a55db2b6SPaul Saab 		if (sack_rxmit)
10856d90faf3SPaul Saab 			goto timer;
1086df8bae1dSRodney W. Grimes 		tp->snd_nxt += len;
1087df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1088df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt;
1089df8bae1dSRodney W. Grimes 			/*
1090df8bae1dSRodney W. Grimes 			 * Time this transmission if not a retransmission and
1091df8bae1dSRodney W. Grimes 			 * not currently timing anything.
1092df8bae1dSRodney W. Grimes 			 */
10939b8b58e0SJonathan Lemon 			if (tp->t_rtttime == 0) {
10949b8b58e0SJonathan Lemon 				tp->t_rtttime = ticks;
1095df8bae1dSRodney W. Grimes 				tp->t_rtseq = startseq;
1096df8bae1dSRodney W. Grimes 				tcpstat.tcps_segstimed++;
1097df8bae1dSRodney W. Grimes 			}
1098df8bae1dSRodney W. Grimes 		}
1099df8bae1dSRodney W. Grimes 
1100df8bae1dSRodney W. Grimes 		/*
1101df8bae1dSRodney W. Grimes 		 * Set retransmit timer if not currently set,
110228257b5cSMatthew Dillon 		 * and not doing a pure ack or a keep-alive probe.
1103df8bae1dSRodney W. Grimes 		 * Initial value for retransmit timer is smoothed
1104df8bae1dSRodney W. Grimes 		 * round-trip time + 2 * round-trip time variance.
1105df8bae1dSRodney W. Grimes 		 * Initialize shift counter which is used for backoff
1106df8bae1dSRodney W. Grimes 		 * of retransmit time.
1107df8bae1dSRodney W. Grimes 		 */
11086d90faf3SPaul Saab timer:
11099b8b58e0SJonathan Lemon 		if (!callout_active(tp->tt_rexmt) &&
1110a55db2b6SPaul Saab 		    ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1111a55db2b6SPaul Saab 		     (tp->snd_nxt != tp->snd_una))) {
11129b8b58e0SJonathan Lemon 			if (callout_active(tp->tt_persist)) {
11139b8b58e0SJonathan Lemon 				callout_stop(tp->tt_persist);
1114df8bae1dSRodney W. Grimes 				tp->t_rxtshift = 0;
1115df8bae1dSRodney W. Grimes 			}
111646f58482SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
111746f58482SJonathan Lemon 				      tcp_timer_rexmt, tp);
1118df8bae1dSRodney W. Grimes 		}
111928257b5cSMatthew Dillon 	} else {
112028257b5cSMatthew Dillon 		/*
112128257b5cSMatthew Dillon 		 * Persist case, update snd_max but since we are in
112228257b5cSMatthew Dillon 		 * persist mode (no window) we do not update snd_nxt.
112328257b5cSMatthew Dillon 		 */
112428257b5cSMatthew Dillon 		int xlen = len;
112528257b5cSMatthew Dillon 		if (flags & TH_SYN)
112628257b5cSMatthew Dillon 			++xlen;
112728257b5cSMatthew Dillon 		if (flags & TH_FIN) {
112828257b5cSMatthew Dillon 			++xlen;
112928257b5cSMatthew Dillon 			tp->t_flags |= TF_SENTFIN;
113028257b5cSMatthew Dillon 		}
1131abac41a6SMatthew Dillon 		if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
1132df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt + len;
113328257b5cSMatthew Dillon 	}
1134df8bae1dSRodney W. Grimes 
1135610ee2f9SDavid Greenman #ifdef TCPDEBUG
1136df8bae1dSRodney W. Grimes 	/*
1137df8bae1dSRodney W. Grimes 	 * Trace.
1138df8bae1dSRodney W. Grimes 	 */
113991f467d5SHartmut Brandt 	if (so->so_options & SO_DEBUG) {
1140f3e0b7efSBruce M Simpson 		u_short save = 0;
11415214cb3fSBruce M Simpson #ifdef INET6
11425214cb3fSBruce M Simpson 		if (!isipv6)
11435214cb3fSBruce M Simpson #endif
11445214cb3fSBruce M Simpson 		{
11455214cb3fSBruce M Simpson 			save = ipov->ih_len;
114691f467d5SHartmut Brandt 			ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
11475214cb3fSBruce M Simpson 		}
1148fb59c426SYoshinobu Inoue 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
11495214cb3fSBruce M Simpson #ifdef INET6
11505214cb3fSBruce M Simpson 		if (!isipv6)
11515214cb3fSBruce M Simpson #endif
115291f467d5SHartmut Brandt 		ipov->ih_len = save;
115391f467d5SHartmut Brandt 	}
1154610ee2f9SDavid Greenman #endif
1155df8bae1dSRodney W. Grimes 
1156df8bae1dSRodney W. Grimes 	/*
1157df8bae1dSRodney W. Grimes 	 * Fill in IP length and desired time to live and
1158df8bae1dSRodney W. Grimes 	 * send to IP level.  There should be a better way
1159df8bae1dSRodney W. Grimes 	 * to handle ttl and tos; we could keep them in
1160df8bae1dSRodney W. Grimes 	 * the template, but need a way to checksum without them.
1161df8bae1dSRodney W. Grimes 	 */
1162fb59c426SYoshinobu Inoue 	/*
1163fb59c426SYoshinobu Inoue 	 * m->m_pkthdr.len should have been set before cksum calcuration,
1164fb59c426SYoshinobu Inoue 	 * because in6_cksum() need it.
1165fb59c426SYoshinobu Inoue 	 */
1166fb59c426SYoshinobu Inoue #ifdef INET6
1167fb59c426SYoshinobu Inoue 	if (isipv6) {
1168fb59c426SYoshinobu Inoue 		/*
1169fb59c426SYoshinobu Inoue 		 * we separately set hoplimit for every segment, since the
1170fb59c426SYoshinobu Inoue 		 * user might want to change the value via setsockopt.
1171fb59c426SYoshinobu Inoue 		 * Also, desired default hop limit might be changed via
1172fb59c426SYoshinobu Inoue 		 * Neighbor Discovery.
1173fb59c426SYoshinobu Inoue 		 */
117497d8d152SAndre Oppermann 		ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1175fb59c426SYoshinobu Inoue 
1176fb59c426SYoshinobu Inoue 		/* TODO: IPv6 IP6TOS_ECT bit on */
1177fb59c426SYoshinobu Inoue 		error = ip6_output(m,
117897d8d152SAndre Oppermann 			    tp->t_inpcb->in6p_outputopts, NULL,
1179b5d47ff5SJohn-Mark Gurney 			    ((so->so_options & SO_DONTROUTE) ?
1180b5d47ff5SJohn-Mark Gurney 			    IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb);
1181fb59c426SYoshinobu Inoue 	} else
1182fb59c426SYoshinobu Inoue #endif /* INET6 */
1183df8bae1dSRodney W. Grimes     {
1184fb59c426SYoshinobu Inoue 	ip->ip_len = m->m_pkthdr.len;
1185686cdd19SJun-ichiro itojun Hagino #ifdef INET6
1186686cdd19SJun-ichiro itojun Hagino 	if (INP_CHECK_SOCKAF(so, AF_INET6))
118797d8d152SAndre Oppermann 		ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1188686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */
1189f138387aSGarrett Wollman 	/*
119097d8d152SAndre Oppermann 	 * If we do path MTU discovery, then we set DF on every packet.
119197d8d152SAndre Oppermann 	 * This might not be the best thing to do according to RFC3390
119297d8d152SAndre Oppermann 	 * Section 2. However the tcp hostcache migitates the problem
119397d8d152SAndre Oppermann 	 * so it affects only the first tcp connection with a host.
1194f138387aSGarrett Wollman 	 */
119597d8d152SAndre Oppermann 	if (path_mtu_discovery)
1196fb59c426SYoshinobu Inoue 		ip->ip_off |= IP_DF;
119797d8d152SAndre Oppermann 
119897d8d152SAndre Oppermann 	error = ip_output(m, tp->t_inpcb->inp_options, NULL,
1199b5d47ff5SJohn-Mark Gurney 	    ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0,
1200b5d47ff5SJohn-Mark Gurney 	    tp->t_inpcb);
1201df8bae1dSRodney W. Grimes     }
1202df8bae1dSRodney W. Grimes 	if (error) {
12037734ea06SArchie Cobbs 
12047734ea06SArchie Cobbs 		/*
12057734ea06SArchie Cobbs 		 * We know that the packet was lost, so back out the
12067734ea06SArchie Cobbs 		 * sequence number advance, if any.
12072c30ec0aSAndre Oppermann 		 *
12082c30ec0aSAndre Oppermann 		 * If the error is EPERM the packet got blocked by the
12092c30ec0aSAndre Oppermann 		 * local firewall.  Normally we should terminate the
12102c30ec0aSAndre Oppermann 		 * connection but the blocking may have been spurious
12112c30ec0aSAndre Oppermann 		 * due to a firewall reconfiguration cycle.  So we treat
12122c30ec0aSAndre Oppermann 		 * it like a packet loss and let the retransmit timer and
12132c30ec0aSAndre Oppermann 		 * timeouts do their work over time.
12142c30ec0aSAndre Oppermann 		 * XXX: It is a POLA question whether calling tcp_drop right
12152c30ec0aSAndre Oppermann 		 * away would be the really correct behavior instead.
12167734ea06SArchie Cobbs 		 */
121772757d9aSGleb Smirnoff 		if (((tp->t_flags & TF_FORCEDATA) == 0 ||
121872757d9aSGleb Smirnoff 		    !callout_active(tp->tt_persist)) &&
121972757d9aSGleb Smirnoff 		    ((flags & TH_SYN) == 0) &&
122072757d9aSGleb Smirnoff 		    (error != EPERM)) {
12210077b016SPaul Saab 			if (sack_rxmit) {
12225d3b1b75SJayanth Vijayaraghavan 				p->rxmit -= len;
12230077b016SPaul Saab 				tp->sackhint.sack_bytes_rexmit -= len;
122472757d9aSGleb Smirnoff 				KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
12250077b016SPaul Saab 				    ("sackhint bytes rtx >= 0"));
12260077b016SPaul Saab 			} else
12277734ea06SArchie Cobbs 				tp->snd_nxt -= len;
12287734ea06SArchie Cobbs 		}
1229df8bae1dSRodney W. Grimes out:
1230cf2942b6SRobert Watson 		SOCKBUF_UNLOCK_ASSERT(&so->so_snd);	/* Check gotos. */
123172757d9aSGleb Smirnoff 		switch (error) {
123272757d9aSGleb Smirnoff 		case EPERM:
123372757d9aSGleb Smirnoff 			tp->t_softerror = error;
123472757d9aSGleb Smirnoff 			return (error);
123572757d9aSGleb Smirnoff 		case ENOBUFS:
123659f577adSJonathan Lemon 	                if (!callout_active(tp->tt_rexmt) &&
123759f577adSJonathan Lemon 			    !callout_active(tp->tt_persist))
123859f577adSJonathan Lemon 	                        callout_reset(tp->tt_rexmt, tp->t_rxtcur,
123959f577adSJonathan Lemon 				    tcp_timer_rexmt, tp);
12401600372bSAndre Oppermann 			tp->snd_cwnd = tp->t_maxseg;
1241df8bae1dSRodney W. Grimes 			return (0);
124272757d9aSGleb Smirnoff 		case EMSGSIZE:
12433d1f141bSGarrett Wollman 			/*
1244b3c0f300SAndre Oppermann 			 * For some reason the interface we used initially
1245b3c0f300SAndre Oppermann 			 * to send segments changed to another or lowered
1246b3c0f300SAndre Oppermann 			 * its MTU.
1247b3c0f300SAndre Oppermann 			 *
1248b3c0f300SAndre Oppermann 			 * tcp_mtudisc() will find out the new MTU and as
1249b3c0f300SAndre Oppermann 			 * its last action, initiate retransmission, so it
1250b3c0f300SAndre Oppermann 			 * is important to not do so here.
1251b3c0f300SAndre Oppermann 			 *
1252b3c0f300SAndre Oppermann 			 * If TSO was active we either got an interface
1253b3c0f300SAndre Oppermann 			 * without TSO capabilits or TSO was turned off.
1254b3c0f300SAndre Oppermann 			 * Disable it for this connection as too and
1255b3c0f300SAndre Oppermann 			 * immediatly retry with MSS sized segments generated
1256b3c0f300SAndre Oppermann 			 * by this function.
12573d1f141bSGarrett Wollman 			 */
1258b3c0f300SAndre Oppermann 			if (tso)
1259b3c0f300SAndre Oppermann 				tp->t_flags &= ~TF_TSO;
12603d1f141bSGarrett Wollman 			tcp_mtudisc(tp->t_inpcb, 0);
126172757d9aSGleb Smirnoff 			return (0);
12628bec3467SGleb Smirnoff 		case EHOSTDOWN:
126372757d9aSGleb Smirnoff 		case EHOSTUNREACH:
126472757d9aSGleb Smirnoff 		case ENETDOWN:
12658bec3467SGleb Smirnoff 		case ENETUNREACH:
126672757d9aSGleb Smirnoff 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
1267df8bae1dSRodney W. Grimes 				tp->t_softerror = error;
1268df8bae1dSRodney W. Grimes 				return (0);
1269df8bae1dSRodney W. Grimes 			}
127072757d9aSGleb Smirnoff 			/* FALLTHROUGH */
127172757d9aSGleb Smirnoff 		default:
1272df8bae1dSRodney W. Grimes 			return (error);
1273df8bae1dSRodney W. Grimes 		}
127472757d9aSGleb Smirnoff 	}
1275df8bae1dSRodney W. Grimes 	tcpstat.tcps_sndtotal++;
1276df8bae1dSRodney W. Grimes 
1277df8bae1dSRodney W. Grimes 	/*
1278df8bae1dSRodney W. Grimes 	 * Data sent (as far as we can tell).
1279df8bae1dSRodney W. Grimes 	 * If this advertises a larger window than any other segment,
1280df8bae1dSRodney W. Grimes 	 * then remember the size of the advertised window.
1281df8bae1dSRodney W. Grimes 	 * Any pending ACK has now been sent.
1282df8bae1dSRodney W. Grimes 	 */
1283201d185bSAndre Oppermann 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1284201d185bSAndre Oppermann 		tp->rcv_adv = tp->rcv_nxt + recwin;
1285df8bae1dSRodney W. Grimes 	tp->last_ack_sent = tp->rcv_nxt;
12863bfd6421SJonathan Lemon 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
12873bfd6421SJonathan Lemon 	if (callout_active(tp->tt_delack))
12889b8b58e0SJonathan Lemon 		callout_stop(tp->tt_delack);
1289d912c694SMatthew Dillon #if 0
1290d912c694SMatthew Dillon 	/*
1291d912c694SMatthew Dillon 	 * This completely breaks TCP if newreno is turned on.  What happens
1292d912c694SMatthew Dillon 	 * is that if delayed-acks are turned on on the receiver, this code
1293d912c694SMatthew Dillon 	 * on the transmitter effectively destroys the TCP window, forcing
1294d912c694SMatthew Dillon 	 * it to four packets (1.5Kx4 = 6K window).
1295d912c694SMatthew Dillon 	 */
129646f58482SJonathan Lemon 	if (sendalot && (!tcp_do_newreno || --maxburst))
1297df8bae1dSRodney W. Grimes 		goto again;
1298d912c694SMatthew Dillon #endif
1299d912c694SMatthew Dillon 	if (sendalot)
1300d912c694SMatthew Dillon 		goto again;
1301df8bae1dSRodney W. Grimes 	return (0);
1302df8bae1dSRodney W. Grimes }
1303df8bae1dSRodney W. Grimes 
1304df8bae1dSRodney W. Grimes void
1305df8bae1dSRodney W. Grimes tcp_setpersist(tp)
1306df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1307df8bae1dSRodney W. Grimes {
13089b8b58e0SJonathan Lemon 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
13099b8b58e0SJonathan Lemon 	int tt;
1310df8bae1dSRodney W. Grimes 
13119b8b58e0SJonathan Lemon 	if (callout_active(tp->tt_rexmt))
13129b8b58e0SJonathan Lemon 		panic("tcp_setpersist: retransmit pending");
1313df8bae1dSRodney W. Grimes 	/*
1314df8bae1dSRodney W. Grimes 	 * Start/restart persistance timer.
1315df8bae1dSRodney W. Grimes 	 */
13169b8b58e0SJonathan Lemon 	TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1317df8bae1dSRodney W. Grimes 		      TCPTV_PERSMIN, TCPTV_PERSMAX);
13189b8b58e0SJonathan Lemon 	callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp);
1319df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1320df8bae1dSRodney W. Grimes 		tp->t_rxtshift++;
1321df8bae1dSRodney W. Grimes }
1322