xref: /freebsd/sys/netinet/tcp_output.c (revision 8b615593fc0d78ef8366c1328f5966256b82a9c0)
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
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
351cfd4b53SBruce M Simpson #include "opt_inet.h"
36fb59c426SYoshinobu Inoue #include "opt_inet6.h"
373a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
38c488362eSRobert Watson #include "opt_mac.h"
390cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
400cc12cc5SJoerg Wunsch 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h>
44fb919e4dSMark Murray #include <sys/kernel.h>
45fb919e4dSMark Murray #include <sys/lock.h>
46fb919e4dSMark Murray #include <sys/mbuf.h>
47fb919e4dSMark Murray #include <sys/mutex.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51fb919e4dSMark Murray #include <sys/sysctl.h>
52603724d3SBjoern A. Zeeb #include <sys/vimage.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <net/route.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes #include <netinet/in.h>
57df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
58df8bae1dSRodney W. Grimes #include <netinet/ip.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
60df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
61ef39adf0SAndre Oppermann #include <netinet/ip_options.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 
78b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
79b9234fafSSam Leffler #include <netipsec/ipsec.h>
80b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
81b9234fafSSam Leffler 
82db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
83db4f9cc7SJonathan Lemon 
84aed55708SRobert Watson #include <security/mac/mac_framework.h>
85aed55708SRobert Watson 
86df8bae1dSRodney W. Grimes #ifdef notyet
87df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack();
88df8bae1dSRodney W. Grimes #endif
89df8bae1dSRodney W. Grimes 
90eb5afebaSMike Silbersack int path_mtu_discovery = 1;
918b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, path_mtu_discovery,
928b615593SMarko Zec 	CTLFLAG_RW, path_mtu_discovery, 1, "Enable Path MTU Discovery");
939a039a5fSDavid Greenman 
949b8b58e0SJonathan Lemon int ss_fltsz = 1;
958b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO,
968b615593SMarko Zec 	slowstart_flightsize, CTLFLAG_RW,
978b615593SMarko Zec 	ss_fltsz, 1, "Slow start flight size");
989b8b58e0SJonathan Lemon 
993260eb18SMike Silbersack int ss_fltsz_local = 4;
1008b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO,
1018b615593SMarko Zec 	local_slowstart_flightsize, CTLFLAG_RW,
1028b615593SMarko Zec 	ss_fltsz_local, 1, "Slow start flight size for local networks");
103df8bae1dSRodney W. Grimes 
1047d200109SJayanth Vijayaraghavan int     tcp_do_newreno = 1;
1058b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW,
1068b615593SMarko Zec 	tcp_do_newreno, 0, "Enable NewReno Algorithms");
107314e5a3dSJeffrey Hsu 
108b3c0f300SAndre Oppermann int	tcp_do_tso = 1;
1098b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, tso, CTLFLAG_RW,
1108b615593SMarko Zec 	tcp_do_tso, 0, "Enable TCP Segmentation Offload");
111b3c0f300SAndre Oppermann 
1126741ecf5SAndre Oppermann int	tcp_do_autosndbuf = 1;
1138b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, sendbuf_auto,
1148b615593SMarko Zec 	CTLFLAG_RW,
1158b615593SMarko Zec 	tcp_do_autosndbuf, 0, "Enable automatic send buffer sizing");
1166741ecf5SAndre Oppermann 
1176741ecf5SAndre Oppermann int	tcp_autosndbuf_inc = 8*1024;
1188b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, sendbuf_inc,
1198b615593SMarko Zec 	CTLFLAG_RW, tcp_autosndbuf_inc, 0,
1208b615593SMarko Zec 	"Incrementor step size of automatic send buffer");
1216741ecf5SAndre Oppermann 
1226741ecf5SAndre Oppermann int	tcp_autosndbuf_max = 256*1024;
1238b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, sendbuf_max,
1248b615593SMarko Zec 	CTLFLAG_RW, tcp_autosndbuf_max, 0,
1258b615593SMarko Zec 	"Max size of automatic send buffer");
1266741ecf5SAndre Oppermann 
1276741ecf5SAndre Oppermann 
128df8bae1dSRodney W. Grimes /*
129df8bae1dSRodney W. Grimes  * Tcp output routine: figure out what should be sent and send it.
130df8bae1dSRodney W. Grimes  */
131df8bae1dSRodney W. Grimes int
132f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp)
133df8bae1dSRodney W. Grimes {
1348b615593SMarko Zec 	INIT_VNET_INET(tp->t_inpcb->inp_vnet);
135f10e85d7SLuigi Rizzo 	struct socket *so = tp->t_inpcb->inp_socket;
136201d185bSAndre Oppermann 	long len, recwin, sendwin;
137df8bae1dSRodney W. Grimes 	int off, flags, error;
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;
1449ad0173dSBjoern A. Zeeb #ifdef IPSEC
1459ad0173dSBjoern A. Zeeb 	unsigned ipsec_optlen = 0;
1469ad0173dSBjoern A. Zeeb #endif
147df8bae1dSRodney W. Grimes 	int idle, sendalot;
14802a1a643SAndre Oppermann 	int sack_rxmit, sack_bytes_rxmt;
1496d90faf3SPaul Saab 	struct sackhole *p;
150b3c0f300SAndre Oppermann 	int tso = 0;
15102a1a643SAndre Oppermann 	struct tcpopt to;
152262c1c1aSMatthew Dillon #if 0
15346f58482SJonathan Lemon 	int maxburst = TCP_MAXBURST;
154262c1c1aSMatthew Dillon #endif
155fb59c426SYoshinobu Inoue #ifdef INET6
156f10e85d7SLuigi Rizzo 	struct ip6_hdr *ip6 = NULL;
157fb59c426SYoshinobu Inoue 	int isipv6;
158fb59c426SYoshinobu Inoue 
159fb59c426SYoshinobu Inoue 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
160fb59c426SYoshinobu Inoue #endif
161df8bae1dSRodney W. Grimes 
1628501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1633d6ade3aSJennifer Yang 
164df8bae1dSRodney W. Grimes 	/*
165df8bae1dSRodney W. Grimes 	 * Determine length of data that should be transmitted,
166df8bae1dSRodney W. Grimes 	 * and flags that will be used.
167df8bae1dSRodney W. Grimes 	 * If there is some data or critical controls (SYN, RST)
168df8bae1dSRodney W. Grimes 	 * to send, then transmit; otherwise, investigate further.
169df8bae1dSRodney W. Grimes 	 */
170c24d5daeSJayanth Vijayaraghavan 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
1719b8b58e0SJonathan Lemon 	if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
172df8bae1dSRodney W. Grimes 		/*
173df8bae1dSRodney W. Grimes 		 * We have been idle for "a while" and no acks are
174df8bae1dSRodney W. Grimes 		 * expected to clock out any data we send --
175df8bae1dSRodney W. Grimes 		 * slow start to get ack "clock" running again.
1769b8b58e0SJonathan Lemon 		 *
1779b8b58e0SJonathan Lemon 		 * Set the slow-start flight size depending on whether
1789b8b58e0SJonathan Lemon 		 * this is a local network or not.
179df8bae1dSRodney W. Grimes 		 */
180603724d3SBjoern A. Zeeb 		int ss = V_ss_fltsz;
181fb59c426SYoshinobu Inoue #ifdef INET6
182f10e85d7SLuigi Rizzo 		if (isipv6) {
183f10e85d7SLuigi Rizzo 			if (in6_localaddr(&tp->t_inpcb->in6p_faddr))
184603724d3SBjoern A. Zeeb 				ss = V_ss_fltsz_local;
185f10e85d7SLuigi Rizzo 		} else
186f10e85d7SLuigi Rizzo #endif /* INET6 */
187f10e85d7SLuigi Rizzo 		if (in_localaddr(tp->t_inpcb->inp_faddr))
188603724d3SBjoern A. Zeeb 			ss = V_ss_fltsz_local;
189f10e85d7SLuigi Rizzo 		tp->snd_cwnd = tp->t_maxseg * ss;
1909b8b58e0SJonathan Lemon 	}
191c24d5daeSJayanth Vijayaraghavan 	tp->t_flags &= ~TF_LASTIDLE;
192c24d5daeSJayanth Vijayaraghavan 	if (idle) {
193c24d5daeSJayanth Vijayaraghavan 		if (tp->t_flags & TF_MORETOCOME) {
194c24d5daeSJayanth Vijayaraghavan 			tp->t_flags |= TF_LASTIDLE;
195c24d5daeSJayanth Vijayaraghavan 			idle = 0;
196c24d5daeSJayanth Vijayaraghavan 		}
197c24d5daeSJayanth Vijayaraghavan 	}
198df8bae1dSRodney W. Grimes again:
1996d90faf3SPaul Saab 	/*
2006d90faf3SPaul Saab 	 * If we've recently taken a timeout, snd_max will be greater than
2016d90faf3SPaul Saab 	 * snd_nxt.  There may be SACK information that allows us to avoid
2026d90faf3SPaul Saab 	 * resending already delivered data.  Adjust snd_nxt accordingly.
2036d90faf3SPaul Saab 	 */
2043529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
2053529149eSAndre Oppermann 	    SEQ_LT(tp->snd_nxt, tp->snd_max))
2066d90faf3SPaul Saab 		tcp_sack_adjust(tp);
207df8bae1dSRodney W. Grimes 	sendalot = 0;
208df8bae1dSRodney W. Grimes 	off = tp->snd_nxt - tp->snd_una;
209201d185bSAndre Oppermann 	sendwin = min(tp->snd_wnd, tp->snd_cwnd);
210201d185bSAndre Oppermann 	sendwin = min(sendwin, tp->snd_bwnd);
211df8bae1dSRodney W. Grimes 
212df8bae1dSRodney W. Grimes 	flags = tcp_outflags[tp->t_state];
213a0292f23SGarrett Wollman 	/*
2146d90faf3SPaul Saab 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
2156d90faf3SPaul Saab 	 * to send out new data (when sendalot is 1), bypass this function.
2166d90faf3SPaul Saab 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
2176d90faf3SPaul Saab 	 * we're replacing a (future) new transmission with a retransmission
2186d90faf3SPaul Saab 	 * now, and we previously incremented snd_cwnd in tcp_input().
2196d90faf3SPaul Saab 	 */
2206d90faf3SPaul Saab 	/*
2216d90faf3SPaul Saab 	 * Still in sack recovery , reset rxmit flag to zero.
2226d90faf3SPaul Saab 	 */
2236d90faf3SPaul Saab 	sack_rxmit = 0;
224a55db2b6SPaul Saab 	sack_bytes_rxmt = 0;
2256d90faf3SPaul Saab 	len = 0;
2266d90faf3SPaul Saab 	p = NULL;
2273529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp) &&
228a55db2b6SPaul Saab 	    (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
229a55db2b6SPaul Saab 		long cwin;
230a55db2b6SPaul Saab 
231a55db2b6SPaul Saab 		cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
232a55db2b6SPaul Saab 		if (cwin < 0)
233a55db2b6SPaul Saab 			cwin = 0;
234f787edd8SJayanth Vijayaraghavan 		/* Do not retransmit SACK segments beyond snd_recover */
235f787edd8SJayanth Vijayaraghavan 		if (SEQ_GT(p->end, tp->snd_recover)) {
236f787edd8SJayanth Vijayaraghavan 			/*
237f787edd8SJayanth Vijayaraghavan 			 * (At least) part of sack hole extends beyond
238f787edd8SJayanth Vijayaraghavan 			 * snd_recover. Check to see if we can rexmit data
239f787edd8SJayanth Vijayaraghavan 			 * for this hole.
240f787edd8SJayanth Vijayaraghavan 			 */
241f787edd8SJayanth Vijayaraghavan 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
242f787edd8SJayanth Vijayaraghavan 				/*
243f787edd8SJayanth Vijayaraghavan 				 * Can't rexmit any more data for this hole.
244f787edd8SJayanth Vijayaraghavan 				 * That data will be rexmitted in the next
245f787edd8SJayanth Vijayaraghavan 				 * sack recovery episode, when snd_recover
246f787edd8SJayanth Vijayaraghavan 				 * moves past p->rxmit.
247f787edd8SJayanth Vijayaraghavan 				 */
248f787edd8SJayanth Vijayaraghavan 				p = NULL;
249f787edd8SJayanth Vijayaraghavan 				goto after_sack_rexmit;
250f787edd8SJayanth Vijayaraghavan 			} else
251f787edd8SJayanth Vijayaraghavan 				/* Can rexmit part of the current hole */
252a55db2b6SPaul Saab 				len = ((long)ulmin(cwin,
253f787edd8SJayanth Vijayaraghavan 						   tp->snd_recover - p->rxmit));
254f787edd8SJayanth Vijayaraghavan 		} else
255a55db2b6SPaul Saab 			len = ((long)ulmin(cwin, p->end - p->rxmit));
2566d90faf3SPaul Saab 		off = p->rxmit - tp->snd_una;
257f787edd8SJayanth Vijayaraghavan 		KASSERT(off >= 0,("%s: sack block to the left of una : %d",
258f787edd8SJayanth Vijayaraghavan 		    __func__, off));
2596d90faf3SPaul Saab 		if (len > 0) {
260ab5c14d8SRobert Watson 			sack_rxmit = 1;
261ab5c14d8SRobert Watson 			sendalot = 1;
262603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sack_rexmits++;
263603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sack_rexmit_bytes +=
2646d90faf3SPaul Saab 			    min(len, tp->t_maxseg);
2656d90faf3SPaul Saab 		}
2666d90faf3SPaul Saab 	}
267f787edd8SJayanth Vijayaraghavan after_sack_rexmit:
2686d90faf3SPaul Saab 	/*
269a0292f23SGarrett Wollman 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
270a0292f23SGarrett Wollman 	 * state flags.
271a0292f23SGarrett Wollman 	 */
272a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDFIN)
273a0292f23SGarrett Wollman 		flags |= TH_FIN;
274a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDSYN)
275a0292f23SGarrett Wollman 		flags |= TH_SYN;
276a0292f23SGarrett Wollman 
277cf2942b6SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
278df8bae1dSRodney W. Grimes 	/*
279df8bae1dSRodney W. Grimes 	 * If in persist timeout with window of 0, send 1 byte.
280df8bae1dSRodney W. Grimes 	 * Otherwise, if window is small but nonzero
281df8bae1dSRodney W. Grimes 	 * and timer expired, we will send what we can
282df8bae1dSRodney W. Grimes 	 * and go to transmit state.
283df8bae1dSRodney W. Grimes 	 */
2842cdbfa66SPaul Saab 	if (tp->t_flags & TF_FORCEDATA) {
285201d185bSAndre Oppermann 		if (sendwin == 0) {
286df8bae1dSRodney W. Grimes 			/*
287df8bae1dSRodney W. Grimes 			 * If we still have some data to send, then
288df8bae1dSRodney W. Grimes 			 * clear the FIN bit.  Usually this would
289df8bae1dSRodney W. Grimes 			 * happen below when it realizes that we
290df8bae1dSRodney W. Grimes 			 * aren't sending all the data.  However,
29129089b51SJulian Elischer 			 * if we have exactly 1 byte of unsent data,
292df8bae1dSRodney W. Grimes 			 * then it won't clear the FIN bit below,
293df8bae1dSRodney W. Grimes 			 * and if we are in persist state, we wind
294df8bae1dSRodney W. Grimes 			 * up sending the packet without recording
295df8bae1dSRodney W. Grimes 			 * that we sent the FIN bit.
296df8bae1dSRodney W. Grimes 			 *
297df8bae1dSRodney W. Grimes 			 * We can't just blindly clear the FIN bit,
298df8bae1dSRodney W. Grimes 			 * because if we don't have any more data
299df8bae1dSRodney W. Grimes 			 * to send then the probe will be the FIN
300df8bae1dSRodney W. Grimes 			 * itself.
301df8bae1dSRodney W. Grimes 			 */
302df8bae1dSRodney W. Grimes 			if (off < so->so_snd.sb_cc)
303df8bae1dSRodney W. Grimes 				flags &= ~TH_FIN;
304201d185bSAndre Oppermann 			sendwin = 1;
305df8bae1dSRodney W. Grimes 		} else {
306b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_PERSIST, 0);
307df8bae1dSRodney W. Grimes 			tp->t_rxtshift = 0;
308df8bae1dSRodney W. Grimes 		}
309df8bae1dSRodney W. Grimes 	}
310df8bae1dSRodney W. Grimes 
31128257b5cSMatthew Dillon 	/*
31228257b5cSMatthew Dillon 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
31328257b5cSMatthew Dillon 	 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
314201d185bSAndre Oppermann 	 * a negative length.  This can also occur when TCP opens up
31528257b5cSMatthew Dillon 	 * its congestion window while receiving additional duplicate
31628257b5cSMatthew Dillon 	 * acks after fast-retransmit because TCP will reset snd_nxt
31728257b5cSMatthew Dillon 	 * to snd_max after the fast-retransmit.
31828257b5cSMatthew Dillon 	 *
31928257b5cSMatthew Dillon 	 * In the normal retransmit-FIN-only case, however, snd_nxt will
32028257b5cSMatthew Dillon 	 * be set to snd_una, the offset will be 0, and the length may
32128257b5cSMatthew Dillon 	 * wind up 0.
3226d90faf3SPaul Saab 	 *
3236d90faf3SPaul Saab 	 * If sack_rxmit is true we are retransmitting from the scoreboard
3246d90faf3SPaul Saab 	 * in which case len is already set.
32528257b5cSMatthew Dillon 	 */
326a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
327a55db2b6SPaul Saab 		if (sack_bytes_rxmt == 0)
3286d90faf3SPaul Saab 			len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
329a55db2b6SPaul Saab 		else {
330a55db2b6SPaul Saab 			long cwin;
331a55db2b6SPaul Saab 
332a55db2b6SPaul Saab                         /*
333a55db2b6SPaul Saab 			 * We are inside of a SACK recovery episode and are
334a55db2b6SPaul Saab 			 * sending new data, having retransmitted all the
335a55db2b6SPaul Saab 			 * data possible in the scoreboard.
336a55db2b6SPaul Saab 			 */
3377d5ed1ceSPaul Saab 			len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd)
3387d5ed1ceSPaul Saab 			       - off);
3398d03f2b5SPaul Saab 			/*
3408d03f2b5SPaul Saab 			 * Don't remove this (len > 0) check !
3418d03f2b5SPaul Saab 			 * We explicitly check for len > 0 here (although it
3428d03f2b5SPaul Saab 			 * isn't really necessary), to work around a gcc
3438d03f2b5SPaul Saab 			 * optimization issue - to force gcc to compute
3448d03f2b5SPaul Saab 			 * len above. Without this check, the computation
3458d03f2b5SPaul Saab 			 * of len is bungled by the optimizer.
3468d03f2b5SPaul Saab 			 */
3478d03f2b5SPaul Saab 			if (len > 0) {
3487d5ed1ceSPaul Saab 				cwin = tp->snd_cwnd -
3497d5ed1ceSPaul Saab 					(tp->snd_nxt - tp->sack_newdata) -
350a55db2b6SPaul Saab 					sack_bytes_rxmt;
351a55db2b6SPaul Saab 				if (cwin < 0)
352a55db2b6SPaul Saab 					cwin = 0;
353a55db2b6SPaul Saab 				len = lmin(len, cwin);
354a55db2b6SPaul Saab 			}
355a55db2b6SPaul Saab 		}
3568d03f2b5SPaul Saab 	}
357a0292f23SGarrett Wollman 
358a0292f23SGarrett Wollman 	/*
359a0292f23SGarrett Wollman 	 * Lop off SYN bit if it has already been sent.  However, if this
360a0292f23SGarrett Wollman 	 * is SYN-SENT state and if segment contains data and if we don't
361a0292f23SGarrett Wollman 	 * know that foreign host supports TAO, suppress sending segment.
362a0292f23SGarrett Wollman 	 */
363a0292f23SGarrett Wollman 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
3644b8e98d6SQing Li 		if (tp->t_state != TCPS_SYN_RECEIVED)
365a0292f23SGarrett Wollman 			flags &= ~TH_SYN;
366a0292f23SGarrett Wollman 		off--, len++;
367a0292f23SGarrett Wollman 	}
368a0292f23SGarrett Wollman 
36981165e48SAndras Olah 	/*
370c94c54e4SAndre Oppermann 	 * Be careful not to send data and/or FIN on SYN segments.
37181165e48SAndras Olah 	 * This measure is needed to prevent interoperability problems
37281165e48SAndras Olah 	 * with not fully conformant TCP implementations.
37381165e48SAndras Olah 	 */
374c94c54e4SAndre Oppermann 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
37581165e48SAndras Olah 		len = 0;
37681165e48SAndras Olah 		flags &= ~TH_FIN;
37781165e48SAndras Olah 	}
37881165e48SAndras Olah 
379df8bae1dSRodney W. Grimes 	if (len < 0) {
380df8bae1dSRodney W. Grimes 		/*
381df8bae1dSRodney W. Grimes 		 * If FIN has been sent but not acked,
382df8bae1dSRodney W. Grimes 		 * but we haven't been called to retransmit,
38328257b5cSMatthew Dillon 		 * len will be < 0.  Otherwise, window shrank
384df8bae1dSRodney W. Grimes 		 * after we sent into it.  If window shrank to 0,
3852d8266afSDavid Greenman 		 * cancel pending retransmit, pull snd_nxt back
3862d8266afSDavid Greenman 		 * to (closed) window, and set the persist timer
3872d8266afSDavid Greenman 		 * if it isn't already going.  If the window didn't
3882d8266afSDavid Greenman 		 * close completely, just wait for an ACK.
389df8bae1dSRodney W. Grimes 		 */
390df8bae1dSRodney W. Grimes 		len = 0;
391201d185bSAndre Oppermann 		if (sendwin == 0) {
392b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
3932d8266afSDavid Greenman 			tp->t_rxtshift = 0;
394df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
395b8152ba7SAndre Oppermann 			if (!tcp_timer_active(tp, TT_PERSIST))
3962d8266afSDavid Greenman 				tcp_setpersist(tp);
397df8bae1dSRodney W. Grimes 		}
398df8bae1dSRodney W. Grimes 	}
39928257b5cSMatthew Dillon 
4006741ecf5SAndre Oppermann 	/* len will be >= 0 after this point. */
401c4982faeSBjoern A. Zeeb 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
4026741ecf5SAndre Oppermann 
40328257b5cSMatthew Dillon 	/*
4046741ecf5SAndre Oppermann 	 * Automatic sizing of send socket buffer.  Often the send buffer
4056741ecf5SAndre Oppermann 	 * size is not optimally adjusted to the actual network conditions
4066741ecf5SAndre Oppermann 	 * at hand (delay bandwidth product).  Setting the buffer size too
4076741ecf5SAndre Oppermann 	 * small limits throughput on links with high bandwidth and high
4086741ecf5SAndre Oppermann 	 * delay (eg. trans-continental/oceanic links).  Setting the
4096741ecf5SAndre Oppermann 	 * buffer size too big consumes too much real kernel memory,
4106741ecf5SAndre Oppermann 	 * especially with many connections on busy servers.
4116741ecf5SAndre Oppermann 	 *
4126741ecf5SAndre Oppermann 	 * The criteria to step up the send buffer one notch are:
4136741ecf5SAndre Oppermann 	 *  1. receive window of remote host is larger than send buffer
4146741ecf5SAndre Oppermann 	 *     (with a fudge factor of 5/4th);
4156741ecf5SAndre Oppermann 	 *  2. send buffer is filled to 7/8th with data (so we actually
4166741ecf5SAndre Oppermann 	 *     have data to make use of it);
4176741ecf5SAndre Oppermann 	 *  3. send buffer fill has not hit maximal automatic size;
4186741ecf5SAndre Oppermann 	 *  4. our send window (slow start and cogestion controlled) is
4196741ecf5SAndre Oppermann 	 *     larger than sent but unacknowledged data in send buffer.
4206741ecf5SAndre Oppermann 	 *
4216741ecf5SAndre Oppermann 	 * The remote host receive window scaling factor may limit the
4226741ecf5SAndre Oppermann 	 * growing of the send buffer before it reaches its allowed
4236741ecf5SAndre Oppermann 	 * maximum.
4246741ecf5SAndre Oppermann 	 *
4256741ecf5SAndre Oppermann 	 * It scales directly with slow start or congestion window
4266741ecf5SAndre Oppermann 	 * and does at most one step per received ACK.  This fast
4276741ecf5SAndre Oppermann 	 * scaling has the drawback of growing the send buffer beyond
4286741ecf5SAndre Oppermann 	 * what is strictly necessary to make full use of a given
4296741ecf5SAndre Oppermann 	 * delay*bandwith product.  However testing has shown this not
4306741ecf5SAndre Oppermann 	 * to be much of an problem.  At worst we are trading wasting
4316741ecf5SAndre Oppermann 	 * of available bandwith (the non-use of it) for wasting some
4326741ecf5SAndre Oppermann 	 * socket buffer memory.
4336741ecf5SAndre Oppermann 	 *
4346741ecf5SAndre Oppermann 	 * TODO: Shrink send buffer during idle periods together
4356741ecf5SAndre Oppermann 	 * with congestion window.  Requires another timer.  Has to
4366741ecf5SAndre Oppermann 	 * wait for upcoming tcp timer rewrite.
4376741ecf5SAndre Oppermann 	 */
438603724d3SBjoern A. Zeeb 	if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
4396741ecf5SAndre Oppermann 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
4406741ecf5SAndre Oppermann 		    so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
441603724d3SBjoern A. Zeeb 		    so->so_snd.sb_cc < V_tcp_autosndbuf_max &&
4426741ecf5SAndre Oppermann 		    sendwin >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
4436741ecf5SAndre Oppermann 			if (!sbreserve_locked(&so->so_snd,
444603724d3SBjoern A. Zeeb 			    min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc,
445603724d3SBjoern A. Zeeb 			     V_tcp_autosndbuf_max), so, curthread))
4466741ecf5SAndre Oppermann 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
4476741ecf5SAndre Oppermann 		}
4486741ecf5SAndre Oppermann 	}
4496741ecf5SAndre Oppermann 
4506741ecf5SAndre Oppermann 	/*
4516741ecf5SAndre Oppermann 	 * Truncate to the maximum segment length or enable TCP Segmentation
4526741ecf5SAndre Oppermann 	 * Offloading (if supported by hardware) and ensure that FIN is removed
4536741ecf5SAndre Oppermann 	 * if the length no longer contains the last data byte.
454b3c0f300SAndre Oppermann 	 *
455b3c0f300SAndre Oppermann 	 * TSO may only be used if we are in a pure bulk sending state.  The
456b3c0f300SAndre Oppermann 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and
457b3c0f300SAndre Oppermann 	 * IP options prevent using TSO.  With TSO the TCP header is the same
458b3c0f300SAndre Oppermann 	 * (except for the sequence number) for all generated packets.  This
459b3c0f300SAndre Oppermann 	 * makes it impossible to transmit any options which vary per generated
460b3c0f300SAndre Oppermann 	 * segment or packet.
461b3c0f300SAndre Oppermann 	 *
462b3c0f300SAndre Oppermann 	 * The length of TSO bursts is limited to TCP_MAXWIN.  That limit and
463b3c0f300SAndre Oppermann 	 * removal of FIN (if not already catched here) are handled later after
464b3c0f300SAndre Oppermann 	 * the exact length of the TCP options are known.
46528257b5cSMatthew Dillon 	 */
4669ad0173dSBjoern A. Zeeb #ifdef IPSEC
4679ad0173dSBjoern A. Zeeb 	/*
4689ad0173dSBjoern A. Zeeb 	 * Pre-calculate here as we save another lookup into the darknesses
4699ad0173dSBjoern A. Zeeb 	 * of IPsec that way and can actually decide if TSO is ok.
4709ad0173dSBjoern A. Zeeb 	 */
4719ad0173dSBjoern A. Zeeb 	ipsec_optlen = ipsec_hdrsiz_tcp(tp);
4729ad0173dSBjoern A. Zeeb #endif
473df8bae1dSRodney W. Grimes 	if (len > tp->t_maxseg) {
474603724d3SBjoern A. Zeeb 		if ((tp->t_flags & TF_TSO) && V_tcp_do_tso &&
475b3c0f300SAndre Oppermann 		    ((tp->t_flags & TF_SIGNATURE) == 0) &&
476b3c0f300SAndre Oppermann 		    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
477b3c0f300SAndre Oppermann 		    tp->t_inpcb->inp_options == NULL &&
4789ad0173dSBjoern A. Zeeb 		    tp->t_inpcb->in6p_options == NULL
4799ad0173dSBjoern A. Zeeb #ifdef IPSEC
4809ad0173dSBjoern A. Zeeb 		    && ipsec_optlen == 0
4819ad0173dSBjoern A. Zeeb #endif
4829ad0173dSBjoern A. Zeeb 		    ) {
483b3c0f300SAndre Oppermann 			tso = 1;
484b3c0f300SAndre Oppermann 		} else {
485df8bae1dSRodney W. Grimes 			len = tp->t_maxseg;
486df8bae1dSRodney W. Grimes 			sendalot = 1;
487b3c0f300SAndre Oppermann 			tso = 0;
488b3c0f300SAndre Oppermann 		}
489df8bae1dSRodney W. Grimes 	}
4905d3b1b75SJayanth Vijayaraghavan 	if (sack_rxmit) {
4915d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
492df8bae1dSRodney W. Grimes 			flags &= ~TH_FIN;
4935d3b1b75SJayanth Vijayaraghavan 	} else {
4945d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
4955d3b1b75SJayanth Vijayaraghavan 			flags &= ~TH_FIN;
4965d3b1b75SJayanth Vijayaraghavan 	}
497df8bae1dSRodney W. Grimes 
498201d185bSAndre Oppermann 	recwin = sbspace(&so->so_rcv);
499df8bae1dSRodney W. Grimes 
500df8bae1dSRodney W. Grimes 	/*
501262c1c1aSMatthew Dillon 	 * Sender silly window avoidance.   We transmit under the following
502262c1c1aSMatthew Dillon 	 * conditions when len is non-zero:
503262c1c1aSMatthew Dillon 	 *
504b3c0f300SAndre Oppermann 	 *	- We have a full segment (or more with TSO)
505262c1c1aSMatthew Dillon 	 *	- This is the last buffer in a write()/send() and we are
506262c1c1aSMatthew Dillon 	 *	  either idle or running NODELAY
507262c1c1aSMatthew Dillon 	 *	- we've timed out (e.g. persist timer)
508262c1c1aSMatthew Dillon 	 *	- we have more then 1/2 the maximum send window's worth of
509262c1c1aSMatthew Dillon 	 *	  data (receiver may be limited the window size)
510262c1c1aSMatthew Dillon 	 *	- we need to retransmit
511df8bae1dSRodney W. Grimes 	 */
512df8bae1dSRodney W. Grimes 	if (len) {
513b3c0f300SAndre Oppermann 		if (len >= tp->t_maxseg)
514df8bae1dSRodney W. Grimes 			goto send;
515262c1c1aSMatthew Dillon 		/*
516262c1c1aSMatthew Dillon 		 * NOTE! on localhost connections an 'ack' from the remote
517262c1c1aSMatthew Dillon 		 * end may occur synchronously with the output and cause
518262c1c1aSMatthew Dillon 		 * us to flush a buffer queued with moretocome.  XXX
519262c1c1aSMatthew Dillon 		 *
520262c1c1aSMatthew Dillon 		 * note: the len + off check is almost certainly unnecessary.
521262c1c1aSMatthew Dillon 		 */
522262c1c1aSMatthew Dillon 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
523262c1c1aSMatthew Dillon 		    (idle || (tp->t_flags & TF_NODELAY)) &&
524262c1c1aSMatthew Dillon 		    len + off >= so->so_snd.sb_cc &&
525262c1c1aSMatthew Dillon 		    (tp->t_flags & TF_NOPUSH) == 0) {
526df8bae1dSRodney W. Grimes 			goto send;
527262c1c1aSMatthew Dillon 		}
5282cdbfa66SPaul Saab 		if (tp->t_flags & TF_FORCEDATA)		/* typ. timeout case */
529df8bae1dSRodney W. Grimes 			goto send;
530a0292f23SGarrett Wollman 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
531df8bae1dSRodney W. Grimes 			goto send;
532262c1c1aSMatthew Dillon 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))	/* retransmit case */
533df8bae1dSRodney W. Grimes 			goto send;
5346d90faf3SPaul Saab 		if (sack_rxmit)
5356d90faf3SPaul Saab 			goto send;
536df8bae1dSRodney W. Grimes 	}
537df8bae1dSRodney W. Grimes 
538df8bae1dSRodney W. Grimes 	/*
539df8bae1dSRodney W. Grimes 	 * Compare available window to amount of window
540df8bae1dSRodney W. Grimes 	 * known to peer (as advertised window less
541df8bae1dSRodney W. Grimes 	 * next expected input).  If the difference is at least two
542df8bae1dSRodney W. Grimes 	 * max size segments, or at least 50% of the maximum possible
543df8bae1dSRodney W. Grimes 	 * window, then want to send a window update to peer.
5443bfd6421SJonathan Lemon 	 * Skip this if the connection is in T/TCP half-open state.
545b7de7d87SAndre Oppermann 	 * Don't send pure window updates when the peer has closed
546b7de7d87SAndre Oppermann 	 * the connection and won't ever send more data.
547df8bae1dSRodney W. Grimes 	 */
548b7de7d87SAndre Oppermann 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
549b7de7d87SAndre Oppermann 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
550df8bae1dSRodney W. Grimes 		/*
551df8bae1dSRodney W. Grimes 		 * "adv" is the amount we can increase the window,
552df8bae1dSRodney W. Grimes 		 * taking into account that we are limited by
553df8bae1dSRodney W. Grimes 		 * TCP_MAXWIN << tp->rcv_scale.
554df8bae1dSRodney W. Grimes 		 */
555201d185bSAndre Oppermann 		long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
556df8bae1dSRodney W. Grimes 			(tp->rcv_adv - tp->rcv_nxt);
557df8bae1dSRodney W. Grimes 
558df8bae1dSRodney W. Grimes 		if (adv >= (long) (2 * tp->t_maxseg))
559df8bae1dSRodney W. Grimes 			goto send;
560df8bae1dSRodney W. Grimes 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
561df8bae1dSRodney W. Grimes 			goto send;
562df8bae1dSRodney W. Grimes 	}
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes 	/*
56528257b5cSMatthew Dillon 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
56628257b5cSMatthew Dillon 	 * is also a catch-all for the retransmit timer timeout case.
567df8bae1dSRodney W. Grimes 	 */
568df8bae1dSRodney W. Grimes 	if (tp->t_flags & TF_ACKNOW)
569df8bae1dSRodney W. Grimes 		goto send;
570a0292f23SGarrett Wollman 	if ((flags & TH_RST) ||
571a0292f23SGarrett Wollman 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
572df8bae1dSRodney W. Grimes 		goto send;
573df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_una))
574df8bae1dSRodney W. Grimes 		goto send;
575df8bae1dSRodney W. Grimes 	/*
576df8bae1dSRodney W. Grimes 	 * If our state indicates that FIN should be sent
57728257b5cSMatthew Dillon 	 * and we have not yet done so, then we need to send.
578df8bae1dSRodney W. Grimes 	 */
579df8bae1dSRodney W. Grimes 	if (flags & TH_FIN &&
580df8bae1dSRodney W. Grimes 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
581df8bae1dSRodney W. Grimes 		goto send;
5826d90faf3SPaul Saab 	/*
5836d90faf3SPaul Saab 	 * In SACK, it is possible for tcp_output to fail to send a segment
5846d90faf3SPaul Saab 	 * after the retransmission timer has been turned off.  Make sure
5856d90faf3SPaul Saab 	 * that the retransmission timer is set.
5866d90faf3SPaul Saab 	 */
5873529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
5883529149eSAndre Oppermann 	    SEQ_GT(tp->snd_max, tp->snd_una) &&
589b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_REXMT) &&
590b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
591b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
592cf2942b6SRobert Watson 		goto just_return;
5936d90faf3SPaul Saab 	}
594df8bae1dSRodney W. Grimes 	/*
595df8bae1dSRodney W. Grimes 	 * TCP window updates are not reliable, rather a polling protocol
596df8bae1dSRodney W. Grimes 	 * using ``persist'' packets is used to insure receipt of window
597df8bae1dSRodney W. Grimes 	 * updates.  The three ``states'' for the output side are:
598df8bae1dSRodney W. Grimes 	 *	idle			not doing retransmits or persists
599df8bae1dSRodney W. Grimes 	 *	persisting		to move a small or zero window
600df8bae1dSRodney W. Grimes 	 *	(re)transmitting	and thereby not persisting
601df8bae1dSRodney W. Grimes 	 *
602b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_PERSIST)
6039b8b58e0SJonathan Lemon 	 *	is true when we are in persist state.
6042cdbfa66SPaul Saab 	 * (tp->t_flags & TF_FORCEDATA)
605df8bae1dSRodney W. Grimes 	 *	is set when we are called to send a persist packet.
606b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_REXMT)
607df8bae1dSRodney W. Grimes 	 *	is set when we are retransmitting
608df8bae1dSRodney W. Grimes 	 * The output side is idle when both timers are zero.
609df8bae1dSRodney W. Grimes 	 *
610df8bae1dSRodney W. Grimes 	 * If send window is too small, there is data to transmit, and no
611df8bae1dSRodney W. Grimes 	 * retransmit or persist is pending, then go to persist state.
612df8bae1dSRodney W. Grimes 	 * If nothing happens soon, send when timer expires:
613df8bae1dSRodney W. Grimes 	 * if window is nonzero, transmit what we can,
614df8bae1dSRodney W. Grimes 	 * otherwise force out a byte.
615df8bae1dSRodney W. Grimes 	 */
616b8152ba7SAndre Oppermann 	if (so->so_snd.sb_cc && !tcp_timer_active(tp, TT_REXMT) &&
617b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
618df8bae1dSRodney W. Grimes 		tp->t_rxtshift = 0;
619df8bae1dSRodney W. Grimes 		tcp_setpersist(tp);
620df8bae1dSRodney W. Grimes 	}
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes 	/*
623df8bae1dSRodney W. Grimes 	 * No reason to send a segment, just return.
624df8bae1dSRodney W. Grimes 	 */
625cf2942b6SRobert Watson just_return:
626cf2942b6SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
627df8bae1dSRodney W. Grimes 	return (0);
628df8bae1dSRodney W. Grimes 
629df8bae1dSRodney W. Grimes send:
630cf2942b6SRobert Watson 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
631df8bae1dSRodney W. Grimes 	/*
632df8bae1dSRodney W. Grimes 	 * Before ESTABLISHED, force sending of initial options
633df8bae1dSRodney W. Grimes 	 * unless TCP set not to do any options.
634df8bae1dSRodney W. Grimes 	 * NOTE: we assume that the IP/TCP header plus TCP options
635df8bae1dSRodney W. Grimes 	 * always fit in a single mbuf, leaving room for a maximum
636df8bae1dSRodney W. Grimes 	 * link header, i.e.
63733841545SHajimu UMEMOTO 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
638df8bae1dSRodney W. Grimes 	 */
639df8bae1dSRodney W. Grimes 	optlen = 0;
640fb59c426SYoshinobu Inoue #ifdef INET6
641fb59c426SYoshinobu Inoue 	if (isipv6)
642fb59c426SYoshinobu Inoue 		hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
643fb59c426SYoshinobu Inoue 	else
644fb59c426SYoshinobu Inoue #endif
645df8bae1dSRodney W. Grimes 	hdrlen = sizeof (struct tcpiphdr);
64602a1a643SAndre Oppermann 
64702a1a643SAndre Oppermann 	/*
64802a1a643SAndre Oppermann 	 * Compute options for segment.
64902a1a643SAndre Oppermann 	 * We only have to care about SYN and established connection
65002a1a643SAndre Oppermann 	 * segments.  Options for SYN-ACK segments are handled in TCP
65102a1a643SAndre Oppermann 	 * syncache.
65202a1a643SAndre Oppermann 	 */
65302a1a643SAndre Oppermann 	if ((tp->t_flags & TF_NOOPT) == 0) {
65402a1a643SAndre Oppermann 		to.to_flags = 0;
65502a1a643SAndre Oppermann 		/* Maximum segment size. */
656df8bae1dSRodney W. Grimes 		if (flags & TH_SYN) {
657df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->iss;
65802a1a643SAndre Oppermann 			to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc);
65902a1a643SAndre Oppermann 			to.to_flags |= TOF_MSS;
660df8bae1dSRodney W. Grimes 		}
66102a1a643SAndre Oppermann 		/* Window scaling. */
66202a1a643SAndre Oppermann 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
66302a1a643SAndre Oppermann 			to.to_wscale = tp->request_r_scale;
66402a1a643SAndre Oppermann 			to.to_flags |= TOF_SCALE;
665df8bae1dSRodney W. Grimes 		}
66602a1a643SAndre Oppermann 		/* Timestamps. */
66702a1a643SAndre Oppermann 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
66802a1a643SAndre Oppermann 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
66902a1a643SAndre Oppermann 			to.to_tsval = ticks + tp->ts_offset;
67002a1a643SAndre Oppermann 			to.to_tsecr = tp->ts_recent;
67102a1a643SAndre Oppermann 			to.to_flags |= TOF_TS;
6726741ecf5SAndre Oppermann 			/* Set receive buffer autosizing timestamp. */
67302a1a643SAndre Oppermann 			if (tp->rfbuf_ts == 0 &&
67402a1a643SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE))
6756741ecf5SAndre Oppermann 				tp->rfbuf_ts = ticks;
6761cfd4b53SBruce M Simpson 		}
67702a1a643SAndre Oppermann 		/* Selective ACK's. */
6783529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
67902a1a643SAndre Oppermann 			if (flags & TH_SYN)
68002a1a643SAndre Oppermann 				to.to_flags |= TOF_SACKPERM;
68102a1a643SAndre Oppermann 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
68202a1a643SAndre Oppermann 			    (tp->t_flags & TF_SACK_PERMIT) &&
68302a1a643SAndre Oppermann 			    tp->rcv_numsacks > 0) {
68402a1a643SAndre Oppermann 				to.to_flags |= TOF_SACK;
68502a1a643SAndre Oppermann 				to.to_nsacks = tp->rcv_numsacks;
68602a1a643SAndre Oppermann 				to.to_sacks = (u_char *)tp->sackblks;
68702a1a643SAndre Oppermann 			}
68802a1a643SAndre Oppermann 		}
68902a1a643SAndre Oppermann #ifdef TCP_SIGNATURE
69002a1a643SAndre Oppermann 		/* TCP-MD5 (RFC2385). */
69102a1a643SAndre Oppermann 		if (tp->t_flags & TF_SIGNATURE)
69202a1a643SAndre Oppermann 			to.to_flags |= TOF_SIGNATURE;
6931cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
6941cfd4b53SBruce M Simpson 
69502a1a643SAndre Oppermann 		/* Processing the options. */
6964a411b9fSBjoern A. Zeeb 		hdrlen += optlen = tcp_addoptions(&to, opt);
697be3f3b5eSPaul Saab 	}
698be3f3b5eSPaul Saab 
699fb59c426SYoshinobu Inoue #ifdef INET6
700fb59c426SYoshinobu Inoue 	if (isipv6)
701fb59c426SYoshinobu Inoue 		ipoptlen = ip6_optlen(tp->t_inpcb);
702fb59c426SYoshinobu Inoue 	else
703fb59c426SYoshinobu Inoue #endif
704f10e85d7SLuigi Rizzo 	if (tp->t_inpcb->inp_options)
705a04884fcSBill Fenner 		ipoptlen = tp->t_inpcb->inp_options->m_len -
706a04884fcSBill Fenner 				offsetof(struct ipoption, ipopt_list);
707f10e85d7SLuigi Rizzo 	else
708a04884fcSBill Fenner 		ipoptlen = 0;
709b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
7109ad0173dSBjoern A. Zeeb 	ipoptlen += ipsec_optlen;
711fb59c426SYoshinobu Inoue #endif
712a04884fcSBill Fenner 
713df8bae1dSRodney W. Grimes 	/*
714df8bae1dSRodney W. Grimes 	 * Adjust data length if insertion of options will
715a0292f23SGarrett Wollman 	 * bump the packet length beyond the t_maxopd length.
716a0292f23SGarrett Wollman 	 * Clear the FIN bit because we cut off the tail of
717a0292f23SGarrett Wollman 	 * the segment.
718b3c0f300SAndre Oppermann 	 *
71931ecb34aSAndre Oppermann 	 * When doing TSO limit a burst to TCP_MAXWIN minus the
72031ecb34aSAndre Oppermann 	 * IP, TCP and Options length to keep ip->ip_len from
72131ecb34aSAndre Oppermann 	 * overflowing.  Prevent the last segment from being
72231ecb34aSAndre Oppermann 	 * fractional thus making them all equal sized and set
7236aa5b623SAndre Oppermann 	 * the flag to continue sending.  TSO is disabled when
7246aa5b623SAndre Oppermann 	 * IP options or IPSEC are present.
725df8bae1dSRodney W. Grimes 	 */
726a04884fcSBill Fenner 	if (len + optlen + ipoptlen > tp->t_maxopd) {
727297a37f3SDavid Greenman 		flags &= ~TH_FIN;
728b3c0f300SAndre Oppermann 		if (tso) {
729eec9d82dSAndre Oppermann 			if (len > TCP_MAXWIN - hdrlen - optlen) {
7306aa5b623SAndre Oppermann 				len = TCP_MAXWIN - hdrlen - optlen;
73131ecb34aSAndre Oppermann 				len = len - (len % (tp->t_maxopd - optlen));
732b3c0f300SAndre Oppermann 				sendalot = 1;
733b3c0f300SAndre Oppermann 			} else if (tp->t_flags & TF_NEEDFIN)
734b3c0f300SAndre Oppermann 				sendalot = 1;
735b3c0f300SAndre Oppermann 		} else {
736a04884fcSBill Fenner 			len = tp->t_maxopd - optlen - ipoptlen;
737df8bae1dSRodney W. Grimes 			sendalot = 1;
738df8bae1dSRodney W. Grimes 		}
739b3c0f300SAndre Oppermann 	}
740df8bae1dSRodney W. Grimes 
741a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/
742a683a7ddSYoshinobu Inoue #ifdef INET6
743a683a7ddSYoshinobu Inoue 	if (max_linkhdr + hdrlen > MCLBYTES)
744a683a7ddSYoshinobu Inoue #else
745df8bae1dSRodney W. Grimes 	if (max_linkhdr + hdrlen > MHLEN)
746a683a7ddSYoshinobu Inoue #endif
747f10e85d7SLuigi Rizzo 		panic("tcphdr too big");
748a0292f23SGarrett Wollman /*#endif*/
749df8bae1dSRodney W. Grimes 
750df8bae1dSRodney W. Grimes 	/*
751c4982faeSBjoern A. Zeeb 	 * This KASSERT is here to catch edge cases at a well defined place.
752c4982faeSBjoern A. Zeeb 	 * Before, those had triggered (random) panic conditions further down.
753c4982faeSBjoern A. Zeeb 	 */
754c4982faeSBjoern A. Zeeb 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
755c4982faeSBjoern A. Zeeb 
756c4982faeSBjoern A. Zeeb 	/*
757df8bae1dSRodney W. Grimes 	 * Grab a header mbuf, attaching a copy of data to
758df8bae1dSRodney W. Grimes 	 * be transmitted, and initialize the header from
759df8bae1dSRodney W. Grimes 	 * the template for sends on this connection.
760df8bae1dSRodney W. Grimes 	 */
761df8bae1dSRodney W. Grimes 	if (len) {
7624e023759SAndre Oppermann 		struct mbuf *mb;
7634e023759SAndre Oppermann 		u_int moff;
7644e023759SAndre Oppermann 
7652cdbfa66SPaul Saab 		if ((tp->t_flags & TF_FORCEDATA) && len == 1)
766603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sndprobe++;
7670ba5d2eeSJohn Baldwin 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
768603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sndrexmitpack++;
769603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sndrexmitbyte += len;
770df8bae1dSRodney W. Grimes 		} else {
771603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sndpack++;
772603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sndbyte += len;
773df8bae1dSRodney W. Grimes 		}
774df8bae1dSRodney W. Grimes #ifdef notyet
775df8bae1dSRodney W. Grimes 		if ((m = m_copypack(so->so_snd.sb_mb, off,
776df8bae1dSRodney W. Grimes 		    (int)len, max_linkhdr + hdrlen)) == 0) {
777cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
778df8bae1dSRodney W. Grimes 			error = ENOBUFS;
779df8bae1dSRodney W. Grimes 			goto out;
780df8bae1dSRodney W. Grimes 		}
781df8bae1dSRodney W. Grimes 		/*
782df8bae1dSRodney W. Grimes 		 * m_copypack left space for our hdr; use it.
783df8bae1dSRodney W. Grimes 		 */
784df8bae1dSRodney W. Grimes 		m->m_len += hdrlen;
785df8bae1dSRodney W. Grimes 		m->m_data -= hdrlen;
786df8bae1dSRodney W. Grimes #else
78734333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
788df8bae1dSRodney W. Grimes 		if (m == NULL) {
789cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
790df8bae1dSRodney W. Grimes 			error = ENOBUFS;
791df8bae1dSRodney W. Grimes 			goto out;
792df8bae1dSRodney W. Grimes 		}
793fb59c426SYoshinobu Inoue #ifdef INET6
794a683a7ddSYoshinobu Inoue 		if (MHLEN < hdrlen + max_linkhdr) {
795a163d034SWarner Losh 			MCLGET(m, M_DONTWAIT);
796a683a7ddSYoshinobu Inoue 			if ((m->m_flags & M_EXT) == 0) {
797cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
798a683a7ddSYoshinobu Inoue 				m_freem(m);
799a683a7ddSYoshinobu Inoue 				error = ENOBUFS;
800a683a7ddSYoshinobu Inoue 				goto out;
801a683a7ddSYoshinobu Inoue 			}
802a683a7ddSYoshinobu Inoue 		}
803fb59c426SYoshinobu Inoue #endif
804df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
805df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
8064e023759SAndre Oppermann 
8074e023759SAndre Oppermann 		/*
8084e023759SAndre Oppermann 		 * Start the m_copy functions from the closest mbuf
8094e023759SAndre Oppermann 		 * to the offset in the socket buffer chain.
8104e023759SAndre Oppermann 		 */
8114e023759SAndre Oppermann 		mb = sbsndptr(&so->so_snd, off, len, &moff);
8124e023759SAndre Oppermann 
813df8bae1dSRodney W. Grimes 		if (len <= MHLEN - hdrlen - max_linkhdr) {
8144e023759SAndre Oppermann 			m_copydata(mb, moff, (int)len,
815df8bae1dSRodney W. Grimes 			    mtod(m, caddr_t) + hdrlen);
816df8bae1dSRodney W. Grimes 			m->m_len += len;
817df8bae1dSRodney W. Grimes 		} else {
8184e023759SAndre Oppermann 			m->m_next = m_copy(mb, moff, (int)len);
8194e023759SAndre Oppermann 			if (m->m_next == NULL) {
820cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
821d7f570e6SGarrett Wollman 				(void) m_free(m);
82251823c3aSGarrett Wollman 				error = ENOBUFS;
82351823c3aSGarrett Wollman 				goto out;
82451823c3aSGarrett Wollman 			}
825df8bae1dSRodney W. Grimes 		}
826df8bae1dSRodney W. Grimes #endif
827df8bae1dSRodney W. Grimes 		/*
828df8bae1dSRodney W. Grimes 		 * If we're sending everything we've got, set PUSH.
829df8bae1dSRodney W. Grimes 		 * (This will keep happy those implementations which only
830df8bae1dSRodney W. Grimes 		 * give data to the user when a buffer fills or
831df8bae1dSRodney W. Grimes 		 * a PUSH comes in.)
832df8bae1dSRodney W. Grimes 		 */
833df8bae1dSRodney W. Grimes 		if (off + len == so->so_snd.sb_cc)
834df8bae1dSRodney W. Grimes 			flags |= TH_PUSH;
835cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
836df8bae1dSRodney W. Grimes 	} else {
837cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
838df8bae1dSRodney W. Grimes 		if (tp->t_flags & TF_ACKNOW)
839603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sndacks++;
840df8bae1dSRodney W. Grimes 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
841603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sndctrl++;
842df8bae1dSRodney W. Grimes 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
843603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sndurg++;
844df8bae1dSRodney W. Grimes 		else
845603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sndwinup++;
846df8bae1dSRodney W. Grimes 
84734333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
848df8bae1dSRodney W. Grimes 		if (m == NULL) {
849df8bae1dSRodney W. Grimes 			error = ENOBUFS;
850df8bae1dSRodney W. Grimes 			goto out;
851df8bae1dSRodney W. Grimes 		}
852fb59c426SYoshinobu Inoue #ifdef INET6
853fb59c426SYoshinobu Inoue 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
854fb59c426SYoshinobu Inoue 		    MHLEN >= hdrlen) {
855fb59c426SYoshinobu Inoue 			MH_ALIGN(m, hdrlen);
856fb59c426SYoshinobu Inoue 		} else
857fb59c426SYoshinobu Inoue #endif
858df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
859df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
860df8bae1dSRodney W. Grimes 	}
861cf2942b6SRobert Watson 	SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
862df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = (struct ifnet *)0;
863c488362eSRobert Watson #ifdef MAC
86430d239bcSRobert Watson 	mac_inpcb_create_mbuf(tp->t_inpcb, m);
865c488362eSRobert Watson #endif
866fb59c426SYoshinobu Inoue #ifdef INET6
867fb59c426SYoshinobu Inoue 	if (isipv6) {
868fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
869fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip6 + 1);
87079909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip6, th);
871fb59c426SYoshinobu Inoue 	} else
872fb59c426SYoshinobu Inoue #endif /* INET6 */
873fb59c426SYoshinobu Inoue 	{
874fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
875fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
876fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip + 1);
87779909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip, th);
878fb59c426SYoshinobu Inoue 	}
879df8bae1dSRodney W. Grimes 
880df8bae1dSRodney W. Grimes 	/*
881df8bae1dSRodney W. Grimes 	 * Fill in fields, remembering maximum advertised
882df8bae1dSRodney W. Grimes 	 * window for use in delaying messages about window sizes.
883df8bae1dSRodney W. Grimes 	 * If resending a FIN, be sure not to use a new sequence number.
884df8bae1dSRodney W. Grimes 	 */
885df8bae1dSRodney W. Grimes 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
886df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max)
887df8bae1dSRodney W. Grimes 		tp->snd_nxt--;
888df8bae1dSRodney W. Grimes 	/*
889f2512ba1SRui Paulo 	 * If we are starting a connection, send ECN setup
890f2512ba1SRui Paulo 	 * SYN packet. If we are on a retransmit, we may
891f2512ba1SRui Paulo 	 * resend those bits a number of times as per
892f2512ba1SRui Paulo 	 * RFC 3168.
893f2512ba1SRui Paulo 	 */
894603724d3SBjoern A. Zeeb 	if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
895f2512ba1SRui Paulo 		if (tp->t_rxtshift >= 1) {
896603724d3SBjoern A. Zeeb 			if (tp->t_rxtshift <= V_tcp_ecn_maxretries)
897f2512ba1SRui Paulo 				flags |= TH_ECE|TH_CWR;
898f2512ba1SRui Paulo 		} else
899f2512ba1SRui Paulo 			flags |= TH_ECE|TH_CWR;
900f2512ba1SRui Paulo 	}
901f2512ba1SRui Paulo 
902f2512ba1SRui Paulo 	if (tp->t_state == TCPS_ESTABLISHED &&
903f2512ba1SRui Paulo 	    (tp->t_flags & TF_ECN_PERMIT)) {
904f2512ba1SRui Paulo 		/*
905f2512ba1SRui Paulo 		 * If the peer has ECN, mark data packets with
906f2512ba1SRui Paulo 		 * ECN capable transmission (ECT).
907f2512ba1SRui Paulo 		 * Ignore pure ack packets, retransmissions and window probes.
908f2512ba1SRui Paulo 		 */
909f2512ba1SRui Paulo 		if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
910f2512ba1SRui Paulo 		    !((tp->t_flags & TF_FORCEDATA) && len == 1)) {
911f2512ba1SRui Paulo #ifdef INET6
912f2512ba1SRui Paulo 			if (isipv6)
913f2512ba1SRui Paulo 				ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
914f2512ba1SRui Paulo 			else
915f2512ba1SRui Paulo #endif
916f2512ba1SRui Paulo 				ip->ip_tos |= IPTOS_ECN_ECT0;
917603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_ecn_ect0++;
918f2512ba1SRui Paulo 		}
919f2512ba1SRui Paulo 
920f2512ba1SRui Paulo 		/*
921f2512ba1SRui Paulo 		 * Reply with proper ECN notifications.
922f2512ba1SRui Paulo 		 */
923f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_CWR) {
924f2512ba1SRui Paulo 			flags |= TH_CWR;
925f2512ba1SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_CWR;
926f2512ba1SRui Paulo 		}
927f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_ECE)
928f2512ba1SRui Paulo 			flags |= TH_ECE;
929f2512ba1SRui Paulo 	}
930f2512ba1SRui Paulo 
931f2512ba1SRui Paulo 	/*
932df8bae1dSRodney W. Grimes 	 * If we are doing retransmissions, then snd_nxt will
933df8bae1dSRodney W. Grimes 	 * not reflect the first unsent octet.  For ACK only
934df8bae1dSRodney W. Grimes 	 * packets, we do not want the sequence number of the
935df8bae1dSRodney W. Grimes 	 * retransmitted packet, we want the sequence number
936df8bae1dSRodney W. Grimes 	 * of the next unsent octet.  So, if there is no data
937df8bae1dSRodney W. Grimes 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
938df8bae1dSRodney W. Grimes 	 * when filling in ti_seq.  But if we are in persist
939df8bae1dSRodney W. Grimes 	 * state, snd_max might reflect one byte beyond the
940df8bae1dSRodney W. Grimes 	 * right edge of the window, so use snd_nxt in that
941df8bae1dSRodney W. Grimes 	 * case, since we know we aren't doing a retransmission.
942df8bae1dSRodney W. Grimes 	 * (retransmit and persist are mutually exclusive...)
943df8bae1dSRodney W. Grimes 	 */
944a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
945b8152ba7SAndre Oppermann 		if (len || (flags & (TH_SYN|TH_FIN)) ||
946b8152ba7SAndre Oppermann 		    tcp_timer_active(tp, TT_PERSIST))
947fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_nxt);
948df8bae1dSRodney W. Grimes 		else
949fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_max);
950a55db2b6SPaul Saab 	} else {
9516d90faf3SPaul Saab 		th->th_seq = htonl(p->rxmit);
9526d90faf3SPaul Saab 		p->rxmit += len;
9530077b016SPaul Saab 		tp->sackhint.sack_bytes_rexmit += len;
9546d90faf3SPaul Saab 	}
955fb59c426SYoshinobu Inoue 	th->th_ack = htonl(tp->rcv_nxt);
956df8bae1dSRodney W. Grimes 	if (optlen) {
957fb59c426SYoshinobu Inoue 		bcopy(opt, th + 1, optlen);
958fb59c426SYoshinobu Inoue 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
959df8bae1dSRodney W. Grimes 	}
960fb59c426SYoshinobu Inoue 	th->th_flags = flags;
961df8bae1dSRodney W. Grimes 	/*
962df8bae1dSRodney W. Grimes 	 * Calculate receive window.  Don't shrink window,
963df8bae1dSRodney W. Grimes 	 * but avoid silly window syndrome.
964df8bae1dSRodney W. Grimes 	 */
965201d185bSAndre Oppermann 	if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
966201d185bSAndre Oppermann 	    recwin < (long)tp->t_maxseg)
967201d185bSAndre Oppermann 		recwin = 0;
968201d185bSAndre Oppermann 	if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
969201d185bSAndre Oppermann 		recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
970201d185bSAndre Oppermann 	if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
971201d185bSAndre Oppermann 		recwin = (long)TCP_MAXWIN << tp->rcv_scale;
972262c1c1aSMatthew Dillon 
973104ebb2aSAndre Oppermann 	/*
974104ebb2aSAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
975104ebb2aSAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK>
976104ebb2aSAndre Oppermann 	 * case is handled in syncache.
977104ebb2aSAndre Oppermann 	 */
978104ebb2aSAndre Oppermann 	if (flags & TH_SYN)
979104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)
980104ebb2aSAndre Oppermann 				(min(sbspace(&so->so_rcv), TCP_MAXWIN)));
981104ebb2aSAndre Oppermann 	else
982104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
983262c1c1aSMatthew Dillon 
984262c1c1aSMatthew Dillon 	/*
985262c1c1aSMatthew Dillon 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised
986262c1c1aSMatthew Dillon 	 * a 0 window.  This may cause the remote transmitter to stall.  This
987262c1c1aSMatthew Dillon 	 * flag tells soreceive() to disable delayed acknowledgements when
988262c1c1aSMatthew Dillon 	 * draining the buffer.  This can occur if the receiver is attempting
989b2722702SRui Paulo 	 * to read more data than can be buffered prior to transmitting on
990262c1c1aSMatthew Dillon 	 * the connection.
991262c1c1aSMatthew Dillon 	 */
992201d185bSAndre Oppermann 	if (recwin == 0)
993262c1c1aSMatthew Dillon 		tp->t_flags |= TF_RXWIN0SENT;
994262c1c1aSMatthew Dillon 	else
995262c1c1aSMatthew Dillon 		tp->t_flags &= ~TF_RXWIN0SENT;
996df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
997fb59c426SYoshinobu Inoue 		th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
998fb59c426SYoshinobu Inoue 		th->th_flags |= TH_URG;
999df8bae1dSRodney W. Grimes 	} else
1000df8bae1dSRodney W. Grimes 		/*
1001df8bae1dSRodney W. Grimes 		 * If no urgent pointer to send, then we pull
1002df8bae1dSRodney W. Grimes 		 * the urgent pointer to the left edge of the send window
1003df8bae1dSRodney W. Grimes 		 * so that it doesn't drift into the send window on sequence
1004df8bae1dSRodney W. Grimes 		 * number wraparound.
1005df8bae1dSRodney W. Grimes 		 */
1006df8bae1dSRodney W. Grimes 		tp->snd_up = tp->snd_una;		/* drag it along */
1007df8bae1dSRodney W. Grimes 
10081cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
1009ee763d0dSBjoern A. Zeeb 	if (tp->t_flags & TF_SIGNATURE) {
1010ee763d0dSBjoern A. Zeeb 		int sigoff = to.to_signature - opt;
10113418daf2SBjoern A. Zeeb 		tcp_signature_compute(m, 0, len, optlen,
10121cfd4b53SBruce M Simpson 		    (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
1013ee763d0dSBjoern A. Zeeb 	}
1014265ed012SBruce M Simpson #endif
10151cfd4b53SBruce M Simpson 
1016df8bae1dSRodney W. Grimes 	/*
1017df8bae1dSRodney W. Grimes 	 * Put TCP length in extended header, and then
1018df8bae1dSRodney W. Grimes 	 * checksum extended header and data.
1019df8bae1dSRodney W. Grimes 	 */
1020fb59c426SYoshinobu Inoue 	m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
1021fb59c426SYoshinobu Inoue #ifdef INET6
1022fb59c426SYoshinobu Inoue 	if (isipv6)
1023fb59c426SYoshinobu Inoue 		/*
1024fb59c426SYoshinobu Inoue 		 * ip6_plen is not need to be filled now, and will be filled
1025fb59c426SYoshinobu Inoue 		 * in ip6_output.
1026fb59c426SYoshinobu Inoue 		 */
1027fb59c426SYoshinobu Inoue 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
1028fb59c426SYoshinobu Inoue 				       sizeof(struct tcphdr) + optlen + len);
1029fb59c426SYoshinobu Inoue 	else
1030fb59c426SYoshinobu Inoue #endif /* INET6 */
1031fb59c426SYoshinobu Inoue 	{
1032db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_TCP;
1033db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
103479909384SJonathan Lemon 		th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
103579909384SJonathan Lemon 		    htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
1036fb59c426SYoshinobu Inoue 
1037db4f9cc7SJonathan Lemon 		/* IP version must be set here for ipv4/ipv6 checking later */
1038db4f9cc7SJonathan Lemon 		KASSERT(ip->ip_v == IPVERSION,
10396e551fb6SDavid E. O'Brien 		    ("%s: IP version incorrect: %d", __func__, ip->ip_v));
1040fb59c426SYoshinobu Inoue 	}
1041df8bae1dSRodney W. Grimes 
1042df8bae1dSRodney W. Grimes 	/*
1043b3c0f300SAndre Oppermann 	 * Enable TSO and specify the size of the segments.
1044b3c0f300SAndre Oppermann 	 * The TCP pseudo header checksum is always provided.
1045b3c0f300SAndre Oppermann 	 * XXX: Fixme: This is currently not the case for IPv6.
1046b3c0f300SAndre Oppermann 	 */
1047b3c0f300SAndre Oppermann 	if (tso) {
1048b3c0f300SAndre Oppermann 		m->m_pkthdr.csum_flags = CSUM_TSO;
1049b3c0f300SAndre Oppermann 		m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen;
1050b3c0f300SAndre Oppermann 	}
1051b3c0f300SAndre Oppermann 
1052b3c0f300SAndre Oppermann 	/*
1053df8bae1dSRodney W. Grimes 	 * In transmit state, time the transmission and arrange for
1054df8bae1dSRodney W. Grimes 	 * the retransmit.  In persist state, just set snd_max.
1055df8bae1dSRodney W. Grimes 	 */
10562cdbfa66SPaul Saab 	if ((tp->t_flags & TF_FORCEDATA) == 0 ||
1057b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
1058df8bae1dSRodney W. Grimes 		tcp_seq startseq = tp->snd_nxt;
1059df8bae1dSRodney W. Grimes 
1060df8bae1dSRodney W. Grimes 		/*
1061df8bae1dSRodney W. Grimes 		 * Advance snd_nxt over sequence space of this segment.
1062df8bae1dSRodney W. Grimes 		 */
1063df8bae1dSRodney W. Grimes 		if (flags & (TH_SYN|TH_FIN)) {
1064df8bae1dSRodney W. Grimes 			if (flags & TH_SYN)
1065df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
1066df8bae1dSRodney W. Grimes 			if (flags & TH_FIN) {
1067df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
1068df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_SENTFIN;
1069df8bae1dSRodney W. Grimes 			}
1070df8bae1dSRodney W. Grimes 		}
1071a55db2b6SPaul Saab 		if (sack_rxmit)
10726d90faf3SPaul Saab 			goto timer;
1073df8bae1dSRodney W. Grimes 		tp->snd_nxt += len;
1074df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1075df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt;
1076df8bae1dSRodney W. Grimes 			/*
1077df8bae1dSRodney W. Grimes 			 * Time this transmission if not a retransmission and
1078df8bae1dSRodney W. Grimes 			 * not currently timing anything.
1079df8bae1dSRodney W. Grimes 			 */
10809b8b58e0SJonathan Lemon 			if (tp->t_rtttime == 0) {
10819b8b58e0SJonathan Lemon 				tp->t_rtttime = ticks;
1082df8bae1dSRodney W. Grimes 				tp->t_rtseq = startseq;
1083603724d3SBjoern A. Zeeb 				V_tcpstat.tcps_segstimed++;
1084df8bae1dSRodney W. Grimes 			}
1085df8bae1dSRodney W. Grimes 		}
1086df8bae1dSRodney W. Grimes 
1087df8bae1dSRodney W. Grimes 		/*
1088df8bae1dSRodney W. Grimes 		 * Set retransmit timer if not currently set,
108928257b5cSMatthew Dillon 		 * and not doing a pure ack or a keep-alive probe.
1090df8bae1dSRodney W. Grimes 		 * Initial value for retransmit timer is smoothed
1091df8bae1dSRodney W. Grimes 		 * round-trip time + 2 * round-trip time variance.
1092df8bae1dSRodney W. Grimes 		 * Initialize shift counter which is used for backoff
1093df8bae1dSRodney W. Grimes 		 * of retransmit time.
1094df8bae1dSRodney W. Grimes 		 */
10956d90faf3SPaul Saab timer:
10964b8e42baSAndre Oppermann 		if (!tcp_timer_active(tp, TT_REXMT) &&
1097a55db2b6SPaul Saab 		    ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1098a55db2b6SPaul Saab 		     (tp->snd_nxt != tp->snd_una))) {
1099b8152ba7SAndre Oppermann 			if (tcp_timer_active(tp, TT_PERSIST)) {
1100b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_PERSIST, 0);
1101df8bae1dSRodney W. Grimes 				tp->t_rxtshift = 0;
1102df8bae1dSRodney W. Grimes 			}
1103b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
1104df8bae1dSRodney W. Grimes 		}
110528257b5cSMatthew Dillon 	} else {
110628257b5cSMatthew Dillon 		/*
110728257b5cSMatthew Dillon 		 * Persist case, update snd_max but since we are in
110828257b5cSMatthew Dillon 		 * persist mode (no window) we do not update snd_nxt.
110928257b5cSMatthew Dillon 		 */
111028257b5cSMatthew Dillon 		int xlen = len;
111128257b5cSMatthew Dillon 		if (flags & TH_SYN)
111228257b5cSMatthew Dillon 			++xlen;
111328257b5cSMatthew Dillon 		if (flags & TH_FIN) {
111428257b5cSMatthew Dillon 			++xlen;
111528257b5cSMatthew Dillon 			tp->t_flags |= TF_SENTFIN;
111628257b5cSMatthew Dillon 		}
1117abac41a6SMatthew Dillon 		if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
1118df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt + len;
111928257b5cSMatthew Dillon 	}
1120df8bae1dSRodney W. Grimes 
1121610ee2f9SDavid Greenman #ifdef TCPDEBUG
1122df8bae1dSRodney W. Grimes 	/*
1123df8bae1dSRodney W. Grimes 	 * Trace.
1124df8bae1dSRodney W. Grimes 	 */
112591f467d5SHartmut Brandt 	if (so->so_options & SO_DEBUG) {
1126f3e0b7efSBruce M Simpson 		u_short save = 0;
11275214cb3fSBruce M Simpson #ifdef INET6
11285214cb3fSBruce M Simpson 		if (!isipv6)
11295214cb3fSBruce M Simpson #endif
11305214cb3fSBruce M Simpson 		{
11315214cb3fSBruce M Simpson 			save = ipov->ih_len;
113291f467d5SHartmut Brandt 			ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
11335214cb3fSBruce M Simpson 		}
1134fb59c426SYoshinobu Inoue 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
11355214cb3fSBruce M Simpson #ifdef INET6
11365214cb3fSBruce M Simpson 		if (!isipv6)
11375214cb3fSBruce M Simpson #endif
113891f467d5SHartmut Brandt 		ipov->ih_len = save;
113991f467d5SHartmut Brandt 	}
1140610ee2f9SDavid Greenman #endif
1141df8bae1dSRodney W. Grimes 
1142df8bae1dSRodney W. Grimes 	/*
1143df8bae1dSRodney W. Grimes 	 * Fill in IP length and desired time to live and
1144df8bae1dSRodney W. Grimes 	 * send to IP level.  There should be a better way
1145df8bae1dSRodney W. Grimes 	 * to handle ttl and tos; we could keep them in
1146df8bae1dSRodney W. Grimes 	 * the template, but need a way to checksum without them.
1147df8bae1dSRodney W. Grimes 	 */
1148fb59c426SYoshinobu Inoue 	/*
1149fb59c426SYoshinobu Inoue 	 * m->m_pkthdr.len should have been set before cksum calcuration,
1150fb59c426SYoshinobu Inoue 	 * because in6_cksum() need it.
1151fb59c426SYoshinobu Inoue 	 */
1152fb59c426SYoshinobu Inoue #ifdef INET6
1153fb59c426SYoshinobu Inoue 	if (isipv6) {
1154fb59c426SYoshinobu Inoue 		/*
1155fb59c426SYoshinobu Inoue 		 * we separately set hoplimit for every segment, since the
1156fb59c426SYoshinobu Inoue 		 * user might want to change the value via setsockopt.
1157fb59c426SYoshinobu Inoue 		 * Also, desired default hop limit might be changed via
1158fb59c426SYoshinobu Inoue 		 * Neighbor Discovery.
1159fb59c426SYoshinobu Inoue 		 */
116097d8d152SAndre Oppermann 		ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1161fb59c426SYoshinobu Inoue 
1162fb59c426SYoshinobu Inoue 		/* TODO: IPv6 IP6TOS_ECT bit on */
1163fb59c426SYoshinobu Inoue 		error = ip6_output(m,
116497d8d152SAndre Oppermann 			    tp->t_inpcb->in6p_outputopts, NULL,
1165b5d47ff5SJohn-Mark Gurney 			    ((so->so_options & SO_DONTROUTE) ?
1166b5d47ff5SJohn-Mark Gurney 			    IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb);
1167fb59c426SYoshinobu Inoue 	} else
1168fb59c426SYoshinobu Inoue #endif /* INET6 */
1169df8bae1dSRodney W. Grimes     {
1170fb59c426SYoshinobu Inoue 	ip->ip_len = m->m_pkthdr.len;
1171686cdd19SJun-ichiro itojun Hagino #ifdef INET6
1172686cdd19SJun-ichiro itojun Hagino 	if (INP_CHECK_SOCKAF(so, AF_INET6))
117397d8d152SAndre Oppermann 		ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1174686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */
1175f138387aSGarrett Wollman 	/*
117697d8d152SAndre Oppermann 	 * If we do path MTU discovery, then we set DF on every packet.
117797d8d152SAndre Oppermann 	 * This might not be the best thing to do according to RFC3390
117897d8d152SAndre Oppermann 	 * Section 2. However the tcp hostcache migitates the problem
117997d8d152SAndre Oppermann 	 * so it affects only the first tcp connection with a host.
1180f138387aSGarrett Wollman 	 */
1181603724d3SBjoern A. Zeeb 	if (V_path_mtu_discovery)
1182fb59c426SYoshinobu Inoue 		ip->ip_off |= IP_DF;
118397d8d152SAndre Oppermann 
118497d8d152SAndre Oppermann 	error = ip_output(m, tp->t_inpcb->inp_options, NULL,
1185b5d47ff5SJohn-Mark Gurney 	    ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0,
1186b5d47ff5SJohn-Mark Gurney 	    tp->t_inpcb);
1187df8bae1dSRodney W. Grimes     }
1188df8bae1dSRodney W. Grimes 	if (error) {
11897734ea06SArchie Cobbs 
11907734ea06SArchie Cobbs 		/*
11917734ea06SArchie Cobbs 		 * We know that the packet was lost, so back out the
11927734ea06SArchie Cobbs 		 * sequence number advance, if any.
11932c30ec0aSAndre Oppermann 		 *
11942c30ec0aSAndre Oppermann 		 * If the error is EPERM the packet got blocked by the
11952c30ec0aSAndre Oppermann 		 * local firewall.  Normally we should terminate the
11962c30ec0aSAndre Oppermann 		 * connection but the blocking may have been spurious
11972c30ec0aSAndre Oppermann 		 * due to a firewall reconfiguration cycle.  So we treat
11982c30ec0aSAndre Oppermann 		 * it like a packet loss and let the retransmit timer and
11992c30ec0aSAndre Oppermann 		 * timeouts do their work over time.
12002c30ec0aSAndre Oppermann 		 * XXX: It is a POLA question whether calling tcp_drop right
12012c30ec0aSAndre Oppermann 		 * away would be the really correct behavior instead.
12027734ea06SArchie Cobbs 		 */
120372757d9aSGleb Smirnoff 		if (((tp->t_flags & TF_FORCEDATA) == 0 ||
1204b8152ba7SAndre Oppermann 		    !tcp_timer_active(tp, TT_PERSIST)) &&
120572757d9aSGleb Smirnoff 		    ((flags & TH_SYN) == 0) &&
120672757d9aSGleb Smirnoff 		    (error != EPERM)) {
12070077b016SPaul Saab 			if (sack_rxmit) {
12085d3b1b75SJayanth Vijayaraghavan 				p->rxmit -= len;
12090077b016SPaul Saab 				tp->sackhint.sack_bytes_rexmit -= len;
121072757d9aSGleb Smirnoff 				KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
12110077b016SPaul Saab 				    ("sackhint bytes rtx >= 0"));
12120077b016SPaul Saab 			} else
12137734ea06SArchie Cobbs 				tp->snd_nxt -= len;
12147734ea06SArchie Cobbs 		}
1215df8bae1dSRodney W. Grimes out:
1216cf2942b6SRobert Watson 		SOCKBUF_UNLOCK_ASSERT(&so->so_snd);	/* Check gotos. */
121772757d9aSGleb Smirnoff 		switch (error) {
121872757d9aSGleb Smirnoff 		case EPERM:
121972757d9aSGleb Smirnoff 			tp->t_softerror = error;
122072757d9aSGleb Smirnoff 			return (error);
122172757d9aSGleb Smirnoff 		case ENOBUFS:
1222b8152ba7SAndre Oppermann 	                if (!tcp_timer_active(tp, TT_REXMT) &&
1223b8152ba7SAndre Oppermann 			    !tcp_timer_active(tp, TT_PERSIST))
1224b8152ba7SAndre Oppermann 	                        tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
12251600372bSAndre Oppermann 			tp->snd_cwnd = tp->t_maxseg;
1226df8bae1dSRodney W. Grimes 			return (0);
122772757d9aSGleb Smirnoff 		case EMSGSIZE:
12283d1f141bSGarrett Wollman 			/*
1229b3c0f300SAndre Oppermann 			 * For some reason the interface we used initially
1230b3c0f300SAndre Oppermann 			 * to send segments changed to another or lowered
1231b3c0f300SAndre Oppermann 			 * its MTU.
1232b3c0f300SAndre Oppermann 			 *
1233b3c0f300SAndre Oppermann 			 * tcp_mtudisc() will find out the new MTU and as
1234b3c0f300SAndre Oppermann 			 * its last action, initiate retransmission, so it
1235b3c0f300SAndre Oppermann 			 * is important to not do so here.
1236b3c0f300SAndre Oppermann 			 *
1237b3c0f300SAndre Oppermann 			 * If TSO was active we either got an interface
1238b3c0f300SAndre Oppermann 			 * without TSO capabilits or TSO was turned off.
1239b3c0f300SAndre Oppermann 			 * Disable it for this connection as too and
1240b3c0f300SAndre Oppermann 			 * immediatly retry with MSS sized segments generated
1241b3c0f300SAndre Oppermann 			 * by this function.
12423d1f141bSGarrett Wollman 			 */
1243b3c0f300SAndre Oppermann 			if (tso)
1244b3c0f300SAndre Oppermann 				tp->t_flags &= ~TF_TSO;
12453d1f141bSGarrett Wollman 			tcp_mtudisc(tp->t_inpcb, 0);
124672757d9aSGleb Smirnoff 			return (0);
12478bec3467SGleb Smirnoff 		case EHOSTDOWN:
124872757d9aSGleb Smirnoff 		case EHOSTUNREACH:
124972757d9aSGleb Smirnoff 		case ENETDOWN:
12508bec3467SGleb Smirnoff 		case ENETUNREACH:
125172757d9aSGleb Smirnoff 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
1252df8bae1dSRodney W. Grimes 				tp->t_softerror = error;
1253df8bae1dSRodney W. Grimes 				return (0);
1254df8bae1dSRodney W. Grimes 			}
125572757d9aSGleb Smirnoff 			/* FALLTHROUGH */
125672757d9aSGleb Smirnoff 		default:
1257df8bae1dSRodney W. Grimes 			return (error);
1258df8bae1dSRodney W. Grimes 		}
125972757d9aSGleb Smirnoff 	}
1260603724d3SBjoern A. Zeeb 	V_tcpstat.tcps_sndtotal++;
1261df8bae1dSRodney W. Grimes 
1262df8bae1dSRodney W. Grimes 	/*
1263df8bae1dSRodney W. Grimes 	 * Data sent (as far as we can tell).
1264df8bae1dSRodney W. Grimes 	 * If this advertises a larger window than any other segment,
1265df8bae1dSRodney W. Grimes 	 * then remember the size of the advertised window.
1266df8bae1dSRodney W. Grimes 	 * Any pending ACK has now been sent.
1267df8bae1dSRodney W. Grimes 	 */
1268201d185bSAndre Oppermann 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1269201d185bSAndre Oppermann 		tp->rcv_adv = tp->rcv_nxt + recwin;
1270df8bae1dSRodney W. Grimes 	tp->last_ack_sent = tp->rcv_nxt;
12713bfd6421SJonathan Lemon 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
1272b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_DELACK))
1273b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, 0);
1274d912c694SMatthew Dillon #if 0
1275d912c694SMatthew Dillon 	/*
1276d912c694SMatthew Dillon 	 * This completely breaks TCP if newreno is turned on.  What happens
1277d912c694SMatthew Dillon 	 * is that if delayed-acks are turned on on the receiver, this code
1278d912c694SMatthew Dillon 	 * on the transmitter effectively destroys the TCP window, forcing
1279d912c694SMatthew Dillon 	 * it to four packets (1.5Kx4 = 6K window).
1280d912c694SMatthew Dillon 	 */
1281603724d3SBjoern A. Zeeb 	if (sendalot && (!V_tcp_do_newreno || --maxburst))
1282df8bae1dSRodney W. Grimes 		goto again;
1283d912c694SMatthew Dillon #endif
1284d912c694SMatthew Dillon 	if (sendalot)
1285d912c694SMatthew Dillon 		goto again;
1286df8bae1dSRodney W. Grimes 	return (0);
1287df8bae1dSRodney W. Grimes }
1288df8bae1dSRodney W. Grimes 
1289df8bae1dSRodney W. Grimes void
1290ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp)
1291df8bae1dSRodney W. Grimes {
12929b8b58e0SJonathan Lemon 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
12939b8b58e0SJonathan Lemon 	int tt;
1294df8bae1dSRodney W. Grimes 
1295b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_REXMT))
12969b8b58e0SJonathan Lemon 		panic("tcp_setpersist: retransmit pending");
1297df8bae1dSRodney W. Grimes 	/*
1298df8bae1dSRodney W. Grimes 	 * Start/restart persistance timer.
1299df8bae1dSRodney W. Grimes 	 */
13009b8b58e0SJonathan Lemon 	TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1301df8bae1dSRodney W. Grimes 		      TCPTV_PERSMIN, TCPTV_PERSMAX);
1302b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_PERSIST, tt);
1303df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1304df8bae1dSRodney W. Grimes 		tp->t_rxtshift++;
1305df8bae1dSRodney W. Grimes }
130602a1a643SAndre Oppermann 
130702a1a643SAndre Oppermann /*
130802a1a643SAndre Oppermann  * Insert TCP options according to the supplied parameters to the place
130902a1a643SAndre Oppermann  * optp in a consistent way.  Can handle unaligned destinations.
131002a1a643SAndre Oppermann  *
131102a1a643SAndre Oppermann  * The order of the option processing is crucial for optimal packing and
131202a1a643SAndre Oppermann  * alignment for the scarce option space.
131302a1a643SAndre Oppermann  *
131402a1a643SAndre Oppermann  * The optimal order for a SYN/SYN-ACK segment is:
131502a1a643SAndre Oppermann  *   MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) +
131602a1a643SAndre Oppermann  *   Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40.
131702a1a643SAndre Oppermann  *
131802a1a643SAndre Oppermann  * The SACK options should be last.  SACK blocks consume 8*n+2 bytes.
131902a1a643SAndre Oppermann  * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks).
132002a1a643SAndre Oppermann  * At minimum we need 10 bytes (to generate 1 SACK block).  If both
132102a1a643SAndre Oppermann  * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present,
132202a1a643SAndre Oppermann  * we only have 10 bytes for SACK options (40 - (12 + 18)).
132302a1a643SAndre Oppermann  */
132402a1a643SAndre Oppermann int
132502a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp)
132602a1a643SAndre Oppermann {
13278b615593SMarko Zec 	INIT_VNET_INET(curvnet);
132802a1a643SAndre Oppermann 	u_int mask, optlen = 0;
132902a1a643SAndre Oppermann 
133002a1a643SAndre Oppermann 	for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) {
133102a1a643SAndre Oppermann 		if ((to->to_flags & mask) != mask)
133202a1a643SAndre Oppermann 			continue;
13333a4018c4SAndre Oppermann 		if (optlen == TCP_MAXOLEN)
13343a4018c4SAndre Oppermann 			break;
133502a1a643SAndre Oppermann 		switch (to->to_flags & mask) {
133602a1a643SAndre Oppermann 		case TOF_MSS:
133702a1a643SAndre Oppermann 			while (optlen % 4) {
133802a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
133902a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
134002a1a643SAndre Oppermann 			}
13413a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG)
13423a4018c4SAndre Oppermann 				continue;
134302a1a643SAndre Oppermann 			optlen += TCPOLEN_MAXSEG;
134402a1a643SAndre Oppermann 			*optp++ = TCPOPT_MAXSEG;
134502a1a643SAndre Oppermann 			*optp++ = TCPOLEN_MAXSEG;
134602a1a643SAndre Oppermann 			to->to_mss = htons(to->to_mss);
134702a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss));
134802a1a643SAndre Oppermann 			optp += sizeof(to->to_mss);
134902a1a643SAndre Oppermann 			break;
135002a1a643SAndre Oppermann 		case TOF_SCALE:
135102a1a643SAndre Oppermann 			while (!optlen || optlen % 2 != 1) {
135202a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
135302a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
135402a1a643SAndre Oppermann 			}
13553a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW)
13563a4018c4SAndre Oppermann 				continue;
135702a1a643SAndre Oppermann 			optlen += TCPOLEN_WINDOW;
135802a1a643SAndre Oppermann 			*optp++ = TCPOPT_WINDOW;
135902a1a643SAndre Oppermann 			*optp++ = TCPOLEN_WINDOW;
136002a1a643SAndre Oppermann 			*optp++ = to->to_wscale;
136102a1a643SAndre Oppermann 			break;
136202a1a643SAndre Oppermann 		case TOF_SACKPERM:
136302a1a643SAndre Oppermann 			while (optlen % 2) {
136402a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
136502a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
136602a1a643SAndre Oppermann 			}
13673a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED)
13683a4018c4SAndre Oppermann 				continue;
136902a1a643SAndre Oppermann 			optlen += TCPOLEN_SACK_PERMITTED;
137002a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK_PERMITTED;
137102a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACK_PERMITTED;
137202a1a643SAndre Oppermann 			break;
137302a1a643SAndre Oppermann 		case TOF_TS:
137402a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
137502a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
137602a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
137702a1a643SAndre Oppermann 			}
13783a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP)
13793a4018c4SAndre Oppermann 				continue;
138002a1a643SAndre Oppermann 			optlen += TCPOLEN_TIMESTAMP;
138102a1a643SAndre Oppermann 			*optp++ = TCPOPT_TIMESTAMP;
138202a1a643SAndre Oppermann 			*optp++ = TCPOLEN_TIMESTAMP;
138302a1a643SAndre Oppermann 			to->to_tsval = htonl(to->to_tsval);
138402a1a643SAndre Oppermann 			to->to_tsecr = htonl(to->to_tsecr);
138502a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval));
138602a1a643SAndre Oppermann 			optp += sizeof(to->to_tsval);
138702a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr));
138802a1a643SAndre Oppermann 			optp += sizeof(to->to_tsecr);
138902a1a643SAndre Oppermann 			break;
139002a1a643SAndre Oppermann 		case TOF_SIGNATURE:
139102a1a643SAndre Oppermann 			{
139202a1a643SAndre Oppermann 			int siglen = TCPOLEN_SIGNATURE - 2;
139302a1a643SAndre Oppermann 
139402a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
139502a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
139602a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
139702a1a643SAndre Oppermann 			}
13980d957bbaSAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE)
139902a1a643SAndre Oppermann 				continue;
140002a1a643SAndre Oppermann 			optlen += TCPOLEN_SIGNATURE;
140102a1a643SAndre Oppermann 			*optp++ = TCPOPT_SIGNATURE;
140202a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SIGNATURE;
140302a1a643SAndre Oppermann 			to->to_signature = optp;
140402a1a643SAndre Oppermann 			while (siglen--)
140502a1a643SAndre Oppermann 				 *optp++ = 0;
140602a1a643SAndre Oppermann 			break;
140702a1a643SAndre Oppermann 			}
140802a1a643SAndre Oppermann 		case TOF_SACK:
140902a1a643SAndre Oppermann 			{
141002a1a643SAndre Oppermann 			int sackblks = 0;
141102a1a643SAndre Oppermann 			struct sackblk *sack = (struct sackblk *)to->to_sacks;
141202a1a643SAndre Oppermann 			tcp_seq sack_seq;
141302a1a643SAndre Oppermann 
141402a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
141502a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
141602a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
141702a1a643SAndre Oppermann 			}
14183a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK)
141902a1a643SAndre Oppermann 				continue;
142002a1a643SAndre Oppermann 			optlen += TCPOLEN_SACKHDR;
142102a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK;
142202a1a643SAndre Oppermann 			sackblks = min(to->to_nsacks,
14230d957bbaSAndre Oppermann 					(TCP_MAXOLEN - optlen) / TCPOLEN_SACK);
142402a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK;
142502a1a643SAndre Oppermann 			while (sackblks--) {
142602a1a643SAndre Oppermann 				sack_seq = htonl(sack->start);
142702a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
142802a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
142902a1a643SAndre Oppermann 				sack_seq = htonl(sack->end);
143002a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
143102a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
143202a1a643SAndre Oppermann 				optlen += TCPOLEN_SACK;
143302a1a643SAndre Oppermann 				sack++;
143402a1a643SAndre Oppermann 			}
1435603724d3SBjoern A. Zeeb 			V_tcpstat.tcps_sack_send_blocks++;
143602a1a643SAndre Oppermann 			break;
143702a1a643SAndre Oppermann 			}
143802a1a643SAndre Oppermann 		default:
143902a1a643SAndre Oppermann 			panic("%s: unknown TCP option type", __func__);
144002a1a643SAndre Oppermann 			break;
144102a1a643SAndre Oppermann 		}
144202a1a643SAndre Oppermann 	}
144302a1a643SAndre Oppermann 
144402a1a643SAndre Oppermann 	/* Terminate and pad TCP options to a 4 byte boundary. */
144502a1a643SAndre Oppermann 	if (optlen % 4) {
144602a1a643SAndre Oppermann 		optlen += TCPOLEN_EOL;
144702a1a643SAndre Oppermann 		*optp++ = TCPOPT_EOL;
144802a1a643SAndre Oppermann 	}
1449413deb12SBjoern A. Zeeb 	/*
1450413deb12SBjoern A. Zeeb 	 * According to RFC 793 (STD0007):
1451413deb12SBjoern A. Zeeb 	 *   "The content of the header beyond the End-of-Option option
1452413deb12SBjoern A. Zeeb 	 *    must be header padding (i.e., zero)."
1453413deb12SBjoern A. Zeeb 	 *   and later: "The padding is composed of zeros."
1454413deb12SBjoern A. Zeeb 	 */
145502a1a643SAndre Oppermann 	while (optlen % 4) {
1456c343c524SAndre Oppermann 		optlen += TCPOLEN_PAD;
1457c343c524SAndre Oppermann 		*optp++ = TCPOPT_PAD;
145802a1a643SAndre Oppermann 	}
145902a1a643SAndre Oppermann 
14600d957bbaSAndre Oppermann 	KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__));
146102a1a643SAndre Oppermann 	return (optlen);
146202a1a643SAndre Oppermann }
1463