xref: /freebsd/sys/netinet/tcp_output.c (revision 78b5071407e7f59724b2c3d3a4bd9f5cca1f0794)
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 
544b79449eSBjoern A. Zeeb #include <net/if.h>
55df8bae1dSRodney W. Grimes #include <net/route.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <netinet/in.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
61df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
62ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
63fb59c426SYoshinobu Inoue #ifdef INET6
64686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
65686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
66fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
67fb59c426SYoshinobu Inoue #endif
68df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
69df8bae1dSRodney W. Grimes #define	TCPOUTFLAGS
70df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
71df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
72df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
73df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
74df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
75610ee2f9SDavid Greenman #ifdef TCPDEBUG
76df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
77610ee2f9SDavid Greenman #endif
784b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
79df8bae1dSRodney W. Grimes 
80b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
81b9234fafSSam Leffler #include <netipsec/ipsec.h>
82b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
83b9234fafSSam Leffler 
84db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
85db4f9cc7SJonathan Lemon 
86aed55708SRobert Watson #include <security/mac/mac_framework.h>
87aed55708SRobert Watson 
88df8bae1dSRodney W. Grimes #ifdef notyet
89df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack();
90df8bae1dSRodney W. Grimes #endif
91df8bae1dSRodney W. Grimes 
9244e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
9344e33a07SMarko Zec int path_mtu_discovery;
9444e33a07SMarko Zec int ss_fltsz;
9544e33a07SMarko Zec int ss_fltsz_local;
9644e33a07SMarko Zec int tcp_do_newreno;
9744e33a07SMarko Zec int tcp_do_tso;
9844e33a07SMarko Zec int tcp_do_autosndbuf;
9944e33a07SMarko Zec int tcp_autosndbuf_inc;
10044e33a07SMarko Zec int tcp_autosndbuf_max;
10144e33a07SMarko Zec #endif
10244e33a07SMarko Zec 
1038b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, path_mtu_discovery,
1048b615593SMarko Zec 	CTLFLAG_RW, path_mtu_discovery, 1, "Enable Path MTU Discovery");
1059a039a5fSDavid Greenman 
1068b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO,
1078b615593SMarko Zec 	slowstart_flightsize, CTLFLAG_RW,
1088b615593SMarko Zec 	ss_fltsz, 1, "Slow start flight size");
1099b8b58e0SJonathan Lemon 
1108b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO,
1118b615593SMarko Zec 	local_slowstart_flightsize, CTLFLAG_RW,
1128b615593SMarko Zec 	ss_fltsz_local, 1, "Slow start flight size for local networks");
113df8bae1dSRodney W. Grimes 
1148b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW,
1158b615593SMarko Zec 	tcp_do_newreno, 0, "Enable NewReno Algorithms");
116314e5a3dSJeffrey Hsu 
1178b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, tso, CTLFLAG_RW,
1188b615593SMarko Zec 	tcp_do_tso, 0, "Enable TCP Segmentation Offload");
119b3c0f300SAndre Oppermann 
1208b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, sendbuf_auto,
1218b615593SMarko Zec 	CTLFLAG_RW,
1228b615593SMarko Zec 	tcp_do_autosndbuf, 0, "Enable automatic send buffer sizing");
1236741ecf5SAndre Oppermann 
1248b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, sendbuf_inc,
1258b615593SMarko Zec 	CTLFLAG_RW, tcp_autosndbuf_inc, 0,
1268b615593SMarko Zec 	"Incrementor step size of automatic send buffer");
1276741ecf5SAndre Oppermann 
1288b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, sendbuf_max,
1298b615593SMarko Zec 	CTLFLAG_RW, tcp_autosndbuf_max, 0,
1308b615593SMarko Zec 	"Max size of automatic send buffer");
1316741ecf5SAndre Oppermann 
1326741ecf5SAndre Oppermann 
133df8bae1dSRodney W. Grimes /*
134df8bae1dSRodney W. Grimes  * Tcp output routine: figure out what should be sent and send it.
135df8bae1dSRodney W. Grimes  */
136df8bae1dSRodney W. Grimes int
137f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp)
138df8bae1dSRodney W. Grimes {
1398b615593SMarko Zec 	INIT_VNET_INET(tp->t_inpcb->inp_vnet);
140f10e85d7SLuigi Rizzo 	struct socket *so = tp->t_inpcb->inp_socket;
141201d185bSAndre Oppermann 	long len, recwin, sendwin;
142df8bae1dSRodney W. Grimes 	int off, flags, error;
143f10e85d7SLuigi Rizzo 	struct mbuf *m;
144fb59c426SYoshinobu Inoue 	struct ip *ip = NULL;
145f10e85d7SLuigi Rizzo 	struct ipovly *ipov = NULL;
146f10e85d7SLuigi Rizzo 	struct tcphdr *th;
147a0292f23SGarrett Wollman 	u_char opt[TCP_MAXOLEN];
148a04884fcSBill Fenner 	unsigned ipoptlen, optlen, hdrlen;
1499ad0173dSBjoern A. Zeeb #ifdef IPSEC
1509ad0173dSBjoern A. Zeeb 	unsigned ipsec_optlen = 0;
1519ad0173dSBjoern A. Zeeb #endif
152df8bae1dSRodney W. Grimes 	int idle, sendalot;
15302a1a643SAndre Oppermann 	int sack_rxmit, sack_bytes_rxmt;
1546d90faf3SPaul Saab 	struct sackhole *p;
155b3c0f300SAndre Oppermann 	int tso = 0;
15602a1a643SAndre Oppermann 	struct tcpopt to;
157262c1c1aSMatthew Dillon #if 0
15846f58482SJonathan Lemon 	int maxburst = TCP_MAXBURST;
159262c1c1aSMatthew Dillon #endif
160fb59c426SYoshinobu Inoue #ifdef INET6
161f10e85d7SLuigi Rizzo 	struct ip6_hdr *ip6 = NULL;
162fb59c426SYoshinobu Inoue 	int isipv6;
163fb59c426SYoshinobu Inoue 
164fb59c426SYoshinobu Inoue 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
165fb59c426SYoshinobu Inoue #endif
166df8bae1dSRodney W. Grimes 
1678501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1683d6ade3aSJennifer Yang 
169df8bae1dSRodney W. Grimes 	/*
170df8bae1dSRodney W. Grimes 	 * Determine length of data that should be transmitted,
171df8bae1dSRodney W. Grimes 	 * and flags that will be used.
172df8bae1dSRodney W. Grimes 	 * If there is some data or critical controls (SYN, RST)
173df8bae1dSRodney W. Grimes 	 * to send, then transmit; otherwise, investigate further.
174df8bae1dSRodney W. Grimes 	 */
175c24d5daeSJayanth Vijayaraghavan 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
1769b8b58e0SJonathan Lemon 	if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
177df8bae1dSRodney W. Grimes 		/*
178df8bae1dSRodney W. Grimes 		 * We have been idle for "a while" and no acks are
179df8bae1dSRodney W. Grimes 		 * expected to clock out any data we send --
180df8bae1dSRodney W. Grimes 		 * slow start to get ack "clock" running again.
1819b8b58e0SJonathan Lemon 		 *
1829b8b58e0SJonathan Lemon 		 * Set the slow-start flight size depending on whether
1839b8b58e0SJonathan Lemon 		 * this is a local network or not.
184df8bae1dSRodney W. Grimes 		 */
185603724d3SBjoern A. Zeeb 		int ss = V_ss_fltsz;
186fb59c426SYoshinobu Inoue #ifdef INET6
187f10e85d7SLuigi Rizzo 		if (isipv6) {
188f10e85d7SLuigi Rizzo 			if (in6_localaddr(&tp->t_inpcb->in6p_faddr))
189603724d3SBjoern A. Zeeb 				ss = V_ss_fltsz_local;
190f10e85d7SLuigi Rizzo 		} else
191f10e85d7SLuigi Rizzo #endif /* INET6 */
192f10e85d7SLuigi Rizzo 		if (in_localaddr(tp->t_inpcb->inp_faddr))
193603724d3SBjoern A. Zeeb 			ss = V_ss_fltsz_local;
194f10e85d7SLuigi Rizzo 		tp->snd_cwnd = tp->t_maxseg * ss;
1959b8b58e0SJonathan Lemon 	}
196c24d5daeSJayanth Vijayaraghavan 	tp->t_flags &= ~TF_LASTIDLE;
197c24d5daeSJayanth Vijayaraghavan 	if (idle) {
198c24d5daeSJayanth Vijayaraghavan 		if (tp->t_flags & TF_MORETOCOME) {
199c24d5daeSJayanth Vijayaraghavan 			tp->t_flags |= TF_LASTIDLE;
200c24d5daeSJayanth Vijayaraghavan 			idle = 0;
201c24d5daeSJayanth Vijayaraghavan 		}
202c24d5daeSJayanth Vijayaraghavan 	}
203df8bae1dSRodney W. Grimes again:
2046d90faf3SPaul Saab 	/*
2056d90faf3SPaul Saab 	 * If we've recently taken a timeout, snd_max will be greater than
2066d90faf3SPaul Saab 	 * snd_nxt.  There may be SACK information that allows us to avoid
2076d90faf3SPaul Saab 	 * resending already delivered data.  Adjust snd_nxt accordingly.
2086d90faf3SPaul Saab 	 */
2093529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
2103529149eSAndre Oppermann 	    SEQ_LT(tp->snd_nxt, tp->snd_max))
2116d90faf3SPaul Saab 		tcp_sack_adjust(tp);
212df8bae1dSRodney W. Grimes 	sendalot = 0;
213df8bae1dSRodney W. Grimes 	off = tp->snd_nxt - tp->snd_una;
214201d185bSAndre Oppermann 	sendwin = min(tp->snd_wnd, tp->snd_cwnd);
215201d185bSAndre Oppermann 	sendwin = min(sendwin, tp->snd_bwnd);
216df8bae1dSRodney W. Grimes 
217df8bae1dSRodney W. Grimes 	flags = tcp_outflags[tp->t_state];
218a0292f23SGarrett Wollman 	/*
2196d90faf3SPaul Saab 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
2206d90faf3SPaul Saab 	 * to send out new data (when sendalot is 1), bypass this function.
2216d90faf3SPaul Saab 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
2226d90faf3SPaul Saab 	 * we're replacing a (future) new transmission with a retransmission
2236d90faf3SPaul Saab 	 * now, and we previously incremented snd_cwnd in tcp_input().
2246d90faf3SPaul Saab 	 */
2256d90faf3SPaul Saab 	/*
2266d90faf3SPaul Saab 	 * Still in sack recovery , reset rxmit flag to zero.
2276d90faf3SPaul Saab 	 */
2286d90faf3SPaul Saab 	sack_rxmit = 0;
229a55db2b6SPaul Saab 	sack_bytes_rxmt = 0;
2306d90faf3SPaul Saab 	len = 0;
2316d90faf3SPaul Saab 	p = NULL;
2323529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp) &&
233a55db2b6SPaul Saab 	    (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
234a55db2b6SPaul Saab 		long cwin;
235a55db2b6SPaul Saab 
236a55db2b6SPaul Saab 		cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
237a55db2b6SPaul Saab 		if (cwin < 0)
238a55db2b6SPaul Saab 			cwin = 0;
239f787edd8SJayanth Vijayaraghavan 		/* Do not retransmit SACK segments beyond snd_recover */
240f787edd8SJayanth Vijayaraghavan 		if (SEQ_GT(p->end, tp->snd_recover)) {
241f787edd8SJayanth Vijayaraghavan 			/*
242f787edd8SJayanth Vijayaraghavan 			 * (At least) part of sack hole extends beyond
243f787edd8SJayanth Vijayaraghavan 			 * snd_recover. Check to see if we can rexmit data
244f787edd8SJayanth Vijayaraghavan 			 * for this hole.
245f787edd8SJayanth Vijayaraghavan 			 */
246f787edd8SJayanth Vijayaraghavan 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
247f787edd8SJayanth Vijayaraghavan 				/*
248f787edd8SJayanth Vijayaraghavan 				 * Can't rexmit any more data for this hole.
249f787edd8SJayanth Vijayaraghavan 				 * That data will be rexmitted in the next
250f787edd8SJayanth Vijayaraghavan 				 * sack recovery episode, when snd_recover
251f787edd8SJayanth Vijayaraghavan 				 * moves past p->rxmit.
252f787edd8SJayanth Vijayaraghavan 				 */
253f787edd8SJayanth Vijayaraghavan 				p = NULL;
254f787edd8SJayanth Vijayaraghavan 				goto after_sack_rexmit;
255f787edd8SJayanth Vijayaraghavan 			} else
256f787edd8SJayanth Vijayaraghavan 				/* Can rexmit part of the current hole */
257a55db2b6SPaul Saab 				len = ((long)ulmin(cwin,
258f787edd8SJayanth Vijayaraghavan 						   tp->snd_recover - p->rxmit));
259f787edd8SJayanth Vijayaraghavan 		} else
260a55db2b6SPaul Saab 			len = ((long)ulmin(cwin, p->end - p->rxmit));
2616d90faf3SPaul Saab 		off = p->rxmit - tp->snd_una;
262f787edd8SJayanth Vijayaraghavan 		KASSERT(off >= 0,("%s: sack block to the left of una : %d",
263f787edd8SJayanth Vijayaraghavan 		    __func__, off));
2646d90faf3SPaul Saab 		if (len > 0) {
265ab5c14d8SRobert Watson 			sack_rxmit = 1;
266ab5c14d8SRobert Watson 			sendalot = 1;
26778b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rexmits);
26878b50714SRobert Watson 			TCPSTAT_ADD(tcps_sack_rexmit_bytes,
26978b50714SRobert Watson 			    min(len, tp->t_maxseg));
2706d90faf3SPaul Saab 		}
2716d90faf3SPaul Saab 	}
272f787edd8SJayanth Vijayaraghavan after_sack_rexmit:
2736d90faf3SPaul Saab 	/*
274a0292f23SGarrett Wollman 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
275a0292f23SGarrett Wollman 	 * state flags.
276a0292f23SGarrett Wollman 	 */
277a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDFIN)
278a0292f23SGarrett Wollman 		flags |= TH_FIN;
279a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDSYN)
280a0292f23SGarrett Wollman 		flags |= TH_SYN;
281a0292f23SGarrett Wollman 
282cf2942b6SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
283df8bae1dSRodney W. Grimes 	/*
284df8bae1dSRodney W. Grimes 	 * If in persist timeout with window of 0, send 1 byte.
285df8bae1dSRodney W. Grimes 	 * Otherwise, if window is small but nonzero
286df8bae1dSRodney W. Grimes 	 * and timer expired, we will send what we can
287df8bae1dSRodney W. Grimes 	 * and go to transmit state.
288df8bae1dSRodney W. Grimes 	 */
2892cdbfa66SPaul Saab 	if (tp->t_flags & TF_FORCEDATA) {
290201d185bSAndre Oppermann 		if (sendwin == 0) {
291df8bae1dSRodney W. Grimes 			/*
292df8bae1dSRodney W. Grimes 			 * If we still have some data to send, then
293df8bae1dSRodney W. Grimes 			 * clear the FIN bit.  Usually this would
294df8bae1dSRodney W. Grimes 			 * happen below when it realizes that we
295df8bae1dSRodney W. Grimes 			 * aren't sending all the data.  However,
29629089b51SJulian Elischer 			 * if we have exactly 1 byte of unsent data,
297df8bae1dSRodney W. Grimes 			 * then it won't clear the FIN bit below,
298df8bae1dSRodney W. Grimes 			 * and if we are in persist state, we wind
299df8bae1dSRodney W. Grimes 			 * up sending the packet without recording
300df8bae1dSRodney W. Grimes 			 * that we sent the FIN bit.
301df8bae1dSRodney W. Grimes 			 *
302df8bae1dSRodney W. Grimes 			 * We can't just blindly clear the FIN bit,
303df8bae1dSRodney W. Grimes 			 * because if we don't have any more data
304df8bae1dSRodney W. Grimes 			 * to send then the probe will be the FIN
305df8bae1dSRodney W. Grimes 			 * itself.
306df8bae1dSRodney W. Grimes 			 */
307df8bae1dSRodney W. Grimes 			if (off < so->so_snd.sb_cc)
308df8bae1dSRodney W. Grimes 				flags &= ~TH_FIN;
309201d185bSAndre Oppermann 			sendwin = 1;
310df8bae1dSRodney W. Grimes 		} else {
311b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_PERSIST, 0);
312df8bae1dSRodney W. Grimes 			tp->t_rxtshift = 0;
313df8bae1dSRodney W. Grimes 		}
314df8bae1dSRodney W. Grimes 	}
315df8bae1dSRodney W. Grimes 
31628257b5cSMatthew Dillon 	/*
31728257b5cSMatthew Dillon 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
31828257b5cSMatthew Dillon 	 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
319201d185bSAndre Oppermann 	 * a negative length.  This can also occur when TCP opens up
32028257b5cSMatthew Dillon 	 * its congestion window while receiving additional duplicate
32128257b5cSMatthew Dillon 	 * acks after fast-retransmit because TCP will reset snd_nxt
32228257b5cSMatthew Dillon 	 * to snd_max after the fast-retransmit.
32328257b5cSMatthew Dillon 	 *
32428257b5cSMatthew Dillon 	 * In the normal retransmit-FIN-only case, however, snd_nxt will
32528257b5cSMatthew Dillon 	 * be set to snd_una, the offset will be 0, and the length may
32628257b5cSMatthew Dillon 	 * wind up 0.
3276d90faf3SPaul Saab 	 *
3286d90faf3SPaul Saab 	 * If sack_rxmit is true we are retransmitting from the scoreboard
3296d90faf3SPaul Saab 	 * in which case len is already set.
33028257b5cSMatthew Dillon 	 */
331a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
332a55db2b6SPaul Saab 		if (sack_bytes_rxmt == 0)
3336d90faf3SPaul Saab 			len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
334a55db2b6SPaul Saab 		else {
335a55db2b6SPaul Saab 			long cwin;
336a55db2b6SPaul Saab 
337a55db2b6SPaul Saab                         /*
338a55db2b6SPaul Saab 			 * We are inside of a SACK recovery episode and are
339a55db2b6SPaul Saab 			 * sending new data, having retransmitted all the
340a55db2b6SPaul Saab 			 * data possible in the scoreboard.
341a55db2b6SPaul Saab 			 */
3427d5ed1ceSPaul Saab 			len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd)
3437d5ed1ceSPaul Saab 			       - off);
3448d03f2b5SPaul Saab 			/*
3458d03f2b5SPaul Saab 			 * Don't remove this (len > 0) check !
3468d03f2b5SPaul Saab 			 * We explicitly check for len > 0 here (although it
3478d03f2b5SPaul Saab 			 * isn't really necessary), to work around a gcc
3488d03f2b5SPaul Saab 			 * optimization issue - to force gcc to compute
3498d03f2b5SPaul Saab 			 * len above. Without this check, the computation
3508d03f2b5SPaul Saab 			 * of len is bungled by the optimizer.
3518d03f2b5SPaul Saab 			 */
3528d03f2b5SPaul Saab 			if (len > 0) {
3537d5ed1ceSPaul Saab 				cwin = tp->snd_cwnd -
3547d5ed1ceSPaul Saab 					(tp->snd_nxt - tp->sack_newdata) -
355a55db2b6SPaul Saab 					sack_bytes_rxmt;
356a55db2b6SPaul Saab 				if (cwin < 0)
357a55db2b6SPaul Saab 					cwin = 0;
358a55db2b6SPaul Saab 				len = lmin(len, cwin);
359a55db2b6SPaul Saab 			}
360a55db2b6SPaul Saab 		}
3618d03f2b5SPaul Saab 	}
362a0292f23SGarrett Wollman 
363a0292f23SGarrett Wollman 	/*
364a0292f23SGarrett Wollman 	 * Lop off SYN bit if it has already been sent.  However, if this
365a0292f23SGarrett Wollman 	 * is SYN-SENT state and if segment contains data and if we don't
366a0292f23SGarrett Wollman 	 * know that foreign host supports TAO, suppress sending segment.
367a0292f23SGarrett Wollman 	 */
368a0292f23SGarrett Wollman 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
3694b8e98d6SQing Li 		if (tp->t_state != TCPS_SYN_RECEIVED)
370a0292f23SGarrett Wollman 			flags &= ~TH_SYN;
371a0292f23SGarrett Wollman 		off--, len++;
372a0292f23SGarrett Wollman 	}
373a0292f23SGarrett Wollman 
37481165e48SAndras Olah 	/*
375c94c54e4SAndre Oppermann 	 * Be careful not to send data and/or FIN on SYN segments.
37681165e48SAndras Olah 	 * This measure is needed to prevent interoperability problems
37781165e48SAndras Olah 	 * with not fully conformant TCP implementations.
37881165e48SAndras Olah 	 */
379c94c54e4SAndre Oppermann 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
38081165e48SAndras Olah 		len = 0;
38181165e48SAndras Olah 		flags &= ~TH_FIN;
38281165e48SAndras Olah 	}
38381165e48SAndras Olah 
384df8bae1dSRodney W. Grimes 	if (len < 0) {
385df8bae1dSRodney W. Grimes 		/*
386df8bae1dSRodney W. Grimes 		 * If FIN has been sent but not acked,
387df8bae1dSRodney W. Grimes 		 * but we haven't been called to retransmit,
38828257b5cSMatthew Dillon 		 * len will be < 0.  Otherwise, window shrank
389df8bae1dSRodney W. Grimes 		 * after we sent into it.  If window shrank to 0,
3902d8266afSDavid Greenman 		 * cancel pending retransmit, pull snd_nxt back
3912d8266afSDavid Greenman 		 * to (closed) window, and set the persist timer
3922d8266afSDavid Greenman 		 * if it isn't already going.  If the window didn't
3932d8266afSDavid Greenman 		 * close completely, just wait for an ACK.
394df8bae1dSRodney W. Grimes 		 */
395df8bae1dSRodney W. Grimes 		len = 0;
396201d185bSAndre Oppermann 		if (sendwin == 0) {
397b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
3982d8266afSDavid Greenman 			tp->t_rxtshift = 0;
399df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
400b8152ba7SAndre Oppermann 			if (!tcp_timer_active(tp, TT_PERSIST))
4012d8266afSDavid Greenman 				tcp_setpersist(tp);
402df8bae1dSRodney W. Grimes 		}
403df8bae1dSRodney W. Grimes 	}
40428257b5cSMatthew Dillon 
4056741ecf5SAndre Oppermann 	/* len will be >= 0 after this point. */
406c4982faeSBjoern A. Zeeb 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
4076741ecf5SAndre Oppermann 
40828257b5cSMatthew Dillon 	/*
4096741ecf5SAndre Oppermann 	 * Automatic sizing of send socket buffer.  Often the send buffer
4106741ecf5SAndre Oppermann 	 * size is not optimally adjusted to the actual network conditions
4116741ecf5SAndre Oppermann 	 * at hand (delay bandwidth product).  Setting the buffer size too
4126741ecf5SAndre Oppermann 	 * small limits throughput on links with high bandwidth and high
4136741ecf5SAndre Oppermann 	 * delay (eg. trans-continental/oceanic links).  Setting the
4146741ecf5SAndre Oppermann 	 * buffer size too big consumes too much real kernel memory,
4156741ecf5SAndre Oppermann 	 * especially with many connections on busy servers.
4166741ecf5SAndre Oppermann 	 *
4176741ecf5SAndre Oppermann 	 * The criteria to step up the send buffer one notch are:
4186741ecf5SAndre Oppermann 	 *  1. receive window of remote host is larger than send buffer
4196741ecf5SAndre Oppermann 	 *     (with a fudge factor of 5/4th);
4206741ecf5SAndre Oppermann 	 *  2. send buffer is filled to 7/8th with data (so we actually
4216741ecf5SAndre Oppermann 	 *     have data to make use of it);
4226741ecf5SAndre Oppermann 	 *  3. send buffer fill has not hit maximal automatic size;
4236741ecf5SAndre Oppermann 	 *  4. our send window (slow start and cogestion controlled) is
4246741ecf5SAndre Oppermann 	 *     larger than sent but unacknowledged data in send buffer.
4256741ecf5SAndre Oppermann 	 *
4266741ecf5SAndre Oppermann 	 * The remote host receive window scaling factor may limit the
4276741ecf5SAndre Oppermann 	 * growing of the send buffer before it reaches its allowed
4286741ecf5SAndre Oppermann 	 * maximum.
4296741ecf5SAndre Oppermann 	 *
4306741ecf5SAndre Oppermann 	 * It scales directly with slow start or congestion window
4316741ecf5SAndre Oppermann 	 * and does at most one step per received ACK.  This fast
4326741ecf5SAndre Oppermann 	 * scaling has the drawback of growing the send buffer beyond
4336741ecf5SAndre Oppermann 	 * what is strictly necessary to make full use of a given
4346741ecf5SAndre Oppermann 	 * delay*bandwith product.  However testing has shown this not
4356741ecf5SAndre Oppermann 	 * to be much of an problem.  At worst we are trading wasting
4366741ecf5SAndre Oppermann 	 * of available bandwith (the non-use of it) for wasting some
4376741ecf5SAndre Oppermann 	 * socket buffer memory.
4386741ecf5SAndre Oppermann 	 *
4396741ecf5SAndre Oppermann 	 * TODO: Shrink send buffer during idle periods together
4406741ecf5SAndre Oppermann 	 * with congestion window.  Requires another timer.  Has to
4416741ecf5SAndre Oppermann 	 * wait for upcoming tcp timer rewrite.
4426741ecf5SAndre Oppermann 	 */
443603724d3SBjoern A. Zeeb 	if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
4446741ecf5SAndre Oppermann 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
4456741ecf5SAndre Oppermann 		    so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
446603724d3SBjoern A. Zeeb 		    so->so_snd.sb_cc < V_tcp_autosndbuf_max &&
4476741ecf5SAndre Oppermann 		    sendwin >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
4486741ecf5SAndre Oppermann 			if (!sbreserve_locked(&so->so_snd,
449603724d3SBjoern A. Zeeb 			    min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc,
450603724d3SBjoern A. Zeeb 			     V_tcp_autosndbuf_max), so, curthread))
4516741ecf5SAndre Oppermann 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
4526741ecf5SAndre Oppermann 		}
4536741ecf5SAndre Oppermann 	}
4546741ecf5SAndre Oppermann 
4556741ecf5SAndre Oppermann 	/*
4566741ecf5SAndre Oppermann 	 * Truncate to the maximum segment length or enable TCP Segmentation
4576741ecf5SAndre Oppermann 	 * Offloading (if supported by hardware) and ensure that FIN is removed
4586741ecf5SAndre Oppermann 	 * if the length no longer contains the last data byte.
459b3c0f300SAndre Oppermann 	 *
460b3c0f300SAndre Oppermann 	 * TSO may only be used if we are in a pure bulk sending state.  The
461b3c0f300SAndre Oppermann 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and
462b3c0f300SAndre Oppermann 	 * IP options prevent using TSO.  With TSO the TCP header is the same
463b3c0f300SAndre Oppermann 	 * (except for the sequence number) for all generated packets.  This
464b3c0f300SAndre Oppermann 	 * makes it impossible to transmit any options which vary per generated
465b3c0f300SAndre Oppermann 	 * segment or packet.
466b3c0f300SAndre Oppermann 	 *
467b3c0f300SAndre Oppermann 	 * The length of TSO bursts is limited to TCP_MAXWIN.  That limit and
468b3c0f300SAndre Oppermann 	 * removal of FIN (if not already catched here) are handled later after
469b3c0f300SAndre Oppermann 	 * the exact length of the TCP options are known.
47028257b5cSMatthew Dillon 	 */
4719ad0173dSBjoern A. Zeeb #ifdef IPSEC
4729ad0173dSBjoern A. Zeeb 	/*
4739ad0173dSBjoern A. Zeeb 	 * Pre-calculate here as we save another lookup into the darknesses
4749ad0173dSBjoern A. Zeeb 	 * of IPsec that way and can actually decide if TSO is ok.
4759ad0173dSBjoern A. Zeeb 	 */
4769ad0173dSBjoern A. Zeeb 	ipsec_optlen = ipsec_hdrsiz_tcp(tp);
4779ad0173dSBjoern A. Zeeb #endif
478df8bae1dSRodney W. Grimes 	if (len > tp->t_maxseg) {
479603724d3SBjoern A. Zeeb 		if ((tp->t_flags & TF_TSO) && V_tcp_do_tso &&
480b3c0f300SAndre Oppermann 		    ((tp->t_flags & TF_SIGNATURE) == 0) &&
481b3c0f300SAndre Oppermann 		    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
482b3c0f300SAndre Oppermann 		    tp->t_inpcb->inp_options == NULL &&
4839ad0173dSBjoern A. Zeeb 		    tp->t_inpcb->in6p_options == NULL
4849ad0173dSBjoern A. Zeeb #ifdef IPSEC
4859ad0173dSBjoern A. Zeeb 		    && ipsec_optlen == 0
4869ad0173dSBjoern A. Zeeb #endif
4879ad0173dSBjoern A. Zeeb 		    ) {
488b3c0f300SAndre Oppermann 			tso = 1;
489b3c0f300SAndre Oppermann 		} else {
490df8bae1dSRodney W. Grimes 			len = tp->t_maxseg;
491df8bae1dSRodney W. Grimes 			sendalot = 1;
492b3c0f300SAndre Oppermann 			tso = 0;
493b3c0f300SAndre Oppermann 		}
494df8bae1dSRodney W. Grimes 	}
4955d3b1b75SJayanth Vijayaraghavan 	if (sack_rxmit) {
4965d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
497df8bae1dSRodney W. Grimes 			flags &= ~TH_FIN;
4985d3b1b75SJayanth Vijayaraghavan 	} else {
4995d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
5005d3b1b75SJayanth Vijayaraghavan 			flags &= ~TH_FIN;
5015d3b1b75SJayanth Vijayaraghavan 	}
502df8bae1dSRodney W. Grimes 
503201d185bSAndre Oppermann 	recwin = sbspace(&so->so_rcv);
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes 	/*
506262c1c1aSMatthew Dillon 	 * Sender silly window avoidance.   We transmit under the following
507262c1c1aSMatthew Dillon 	 * conditions when len is non-zero:
508262c1c1aSMatthew Dillon 	 *
509b3c0f300SAndre Oppermann 	 *	- We have a full segment (or more with TSO)
510262c1c1aSMatthew Dillon 	 *	- This is the last buffer in a write()/send() and we are
511262c1c1aSMatthew Dillon 	 *	  either idle or running NODELAY
512262c1c1aSMatthew Dillon 	 *	- we've timed out (e.g. persist timer)
513262c1c1aSMatthew Dillon 	 *	- we have more then 1/2 the maximum send window's worth of
514262c1c1aSMatthew Dillon 	 *	  data (receiver may be limited the window size)
515262c1c1aSMatthew Dillon 	 *	- we need to retransmit
516df8bae1dSRodney W. Grimes 	 */
517df8bae1dSRodney W. Grimes 	if (len) {
518b3c0f300SAndre Oppermann 		if (len >= tp->t_maxseg)
519df8bae1dSRodney W. Grimes 			goto send;
520262c1c1aSMatthew Dillon 		/*
521262c1c1aSMatthew Dillon 		 * NOTE! on localhost connections an 'ack' from the remote
522262c1c1aSMatthew Dillon 		 * end may occur synchronously with the output and cause
523262c1c1aSMatthew Dillon 		 * us to flush a buffer queued with moretocome.  XXX
524262c1c1aSMatthew Dillon 		 *
525262c1c1aSMatthew Dillon 		 * note: the len + off check is almost certainly unnecessary.
526262c1c1aSMatthew Dillon 		 */
527262c1c1aSMatthew Dillon 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
528262c1c1aSMatthew Dillon 		    (idle || (tp->t_flags & TF_NODELAY)) &&
529262c1c1aSMatthew Dillon 		    len + off >= so->so_snd.sb_cc &&
530262c1c1aSMatthew Dillon 		    (tp->t_flags & TF_NOPUSH) == 0) {
531df8bae1dSRodney W. Grimes 			goto send;
532262c1c1aSMatthew Dillon 		}
5332cdbfa66SPaul Saab 		if (tp->t_flags & TF_FORCEDATA)		/* typ. timeout case */
534df8bae1dSRodney W. Grimes 			goto send;
535a0292f23SGarrett Wollman 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
536df8bae1dSRodney W. Grimes 			goto send;
537262c1c1aSMatthew Dillon 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))	/* retransmit case */
538df8bae1dSRodney W. Grimes 			goto send;
5396d90faf3SPaul Saab 		if (sack_rxmit)
5406d90faf3SPaul Saab 			goto send;
541df8bae1dSRodney W. Grimes 	}
542df8bae1dSRodney W. Grimes 
543df8bae1dSRodney W. Grimes 	/*
544df8bae1dSRodney W. Grimes 	 * Compare available window to amount of window
545df8bae1dSRodney W. Grimes 	 * known to peer (as advertised window less
546df8bae1dSRodney W. Grimes 	 * next expected input).  If the difference is at least two
547df8bae1dSRodney W. Grimes 	 * max size segments, or at least 50% of the maximum possible
548df8bae1dSRodney W. Grimes 	 * window, then want to send a window update to peer.
5493bfd6421SJonathan Lemon 	 * Skip this if the connection is in T/TCP half-open state.
550b7de7d87SAndre Oppermann 	 * Don't send pure window updates when the peer has closed
551b7de7d87SAndre Oppermann 	 * the connection and won't ever send more data.
552df8bae1dSRodney W. Grimes 	 */
553b7de7d87SAndre Oppermann 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
554b7de7d87SAndre Oppermann 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
555df8bae1dSRodney W. Grimes 		/*
556df8bae1dSRodney W. Grimes 		 * "adv" is the amount we can increase the window,
557df8bae1dSRodney W. Grimes 		 * taking into account that we are limited by
558df8bae1dSRodney W. Grimes 		 * TCP_MAXWIN << tp->rcv_scale.
559df8bae1dSRodney W. Grimes 		 */
560201d185bSAndre Oppermann 		long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
561df8bae1dSRodney W. Grimes 			(tp->rcv_adv - tp->rcv_nxt);
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes 		if (adv >= (long) (2 * tp->t_maxseg))
564df8bae1dSRodney W. Grimes 			goto send;
565df8bae1dSRodney W. Grimes 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
566df8bae1dSRodney W. Grimes 			goto send;
567df8bae1dSRodney W. Grimes 	}
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes 	/*
57028257b5cSMatthew Dillon 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
57128257b5cSMatthew Dillon 	 * is also a catch-all for the retransmit timer timeout case.
572df8bae1dSRodney W. Grimes 	 */
573df8bae1dSRodney W. Grimes 	if (tp->t_flags & TF_ACKNOW)
574df8bae1dSRodney W. Grimes 		goto send;
575a0292f23SGarrett Wollman 	if ((flags & TH_RST) ||
576a0292f23SGarrett Wollman 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
577df8bae1dSRodney W. Grimes 		goto send;
578df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_una))
579df8bae1dSRodney W. Grimes 		goto send;
580df8bae1dSRodney W. Grimes 	/*
581df8bae1dSRodney W. Grimes 	 * If our state indicates that FIN should be sent
58228257b5cSMatthew Dillon 	 * and we have not yet done so, then we need to send.
583df8bae1dSRodney W. Grimes 	 */
584df8bae1dSRodney W. Grimes 	if (flags & TH_FIN &&
585df8bae1dSRodney W. Grimes 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
586df8bae1dSRodney W. Grimes 		goto send;
5876d90faf3SPaul Saab 	/*
5886d90faf3SPaul Saab 	 * In SACK, it is possible for tcp_output to fail to send a segment
5896d90faf3SPaul Saab 	 * after the retransmission timer has been turned off.  Make sure
5906d90faf3SPaul Saab 	 * that the retransmission timer is set.
5916d90faf3SPaul Saab 	 */
5923529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
5933529149eSAndre Oppermann 	    SEQ_GT(tp->snd_max, tp->snd_una) &&
594b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_REXMT) &&
595b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
596b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
597cf2942b6SRobert Watson 		goto just_return;
5986d90faf3SPaul Saab 	}
599df8bae1dSRodney W. Grimes 	/*
600df8bae1dSRodney W. Grimes 	 * TCP window updates are not reliable, rather a polling protocol
601df8bae1dSRodney W. Grimes 	 * using ``persist'' packets is used to insure receipt of window
602df8bae1dSRodney W. Grimes 	 * updates.  The three ``states'' for the output side are:
603df8bae1dSRodney W. Grimes 	 *	idle			not doing retransmits or persists
604df8bae1dSRodney W. Grimes 	 *	persisting		to move a small or zero window
605df8bae1dSRodney W. Grimes 	 *	(re)transmitting	and thereby not persisting
606df8bae1dSRodney W. Grimes 	 *
607b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_PERSIST)
6089b8b58e0SJonathan Lemon 	 *	is true when we are in persist state.
6092cdbfa66SPaul Saab 	 * (tp->t_flags & TF_FORCEDATA)
610df8bae1dSRodney W. Grimes 	 *	is set when we are called to send a persist packet.
611b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_REXMT)
612df8bae1dSRodney W. Grimes 	 *	is set when we are retransmitting
613df8bae1dSRodney W. Grimes 	 * The output side is idle when both timers are zero.
614df8bae1dSRodney W. Grimes 	 *
615df8bae1dSRodney W. Grimes 	 * If send window is too small, there is data to transmit, and no
616df8bae1dSRodney W. Grimes 	 * retransmit or persist is pending, then go to persist state.
617df8bae1dSRodney W. Grimes 	 * If nothing happens soon, send when timer expires:
618df8bae1dSRodney W. Grimes 	 * if window is nonzero, transmit what we can,
619df8bae1dSRodney W. Grimes 	 * otherwise force out a byte.
620df8bae1dSRodney W. Grimes 	 */
621b8152ba7SAndre Oppermann 	if (so->so_snd.sb_cc && !tcp_timer_active(tp, TT_REXMT) &&
622b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
623df8bae1dSRodney W. Grimes 		tp->t_rxtshift = 0;
624df8bae1dSRodney W. Grimes 		tcp_setpersist(tp);
625df8bae1dSRodney W. Grimes 	}
626df8bae1dSRodney W. Grimes 
627df8bae1dSRodney W. Grimes 	/*
628df8bae1dSRodney W. Grimes 	 * No reason to send a segment, just return.
629df8bae1dSRodney W. Grimes 	 */
630cf2942b6SRobert Watson just_return:
631cf2942b6SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
632df8bae1dSRodney W. Grimes 	return (0);
633df8bae1dSRodney W. Grimes 
634df8bae1dSRodney W. Grimes send:
635cf2942b6SRobert Watson 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
636df8bae1dSRodney W. Grimes 	/*
637df8bae1dSRodney W. Grimes 	 * Before ESTABLISHED, force sending of initial options
638df8bae1dSRodney W. Grimes 	 * unless TCP set not to do any options.
639df8bae1dSRodney W. Grimes 	 * NOTE: we assume that the IP/TCP header plus TCP options
640df8bae1dSRodney W. Grimes 	 * always fit in a single mbuf, leaving room for a maximum
641df8bae1dSRodney W. Grimes 	 * link header, i.e.
64233841545SHajimu UMEMOTO 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
643df8bae1dSRodney W. Grimes 	 */
644df8bae1dSRodney W. Grimes 	optlen = 0;
645fb59c426SYoshinobu Inoue #ifdef INET6
646fb59c426SYoshinobu Inoue 	if (isipv6)
647fb59c426SYoshinobu Inoue 		hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
648fb59c426SYoshinobu Inoue 	else
649fb59c426SYoshinobu Inoue #endif
650df8bae1dSRodney W. Grimes 	hdrlen = sizeof (struct tcpiphdr);
65102a1a643SAndre Oppermann 
65202a1a643SAndre Oppermann 	/*
65302a1a643SAndre Oppermann 	 * Compute options for segment.
65402a1a643SAndre Oppermann 	 * We only have to care about SYN and established connection
65502a1a643SAndre Oppermann 	 * segments.  Options for SYN-ACK segments are handled in TCP
65602a1a643SAndre Oppermann 	 * syncache.
65702a1a643SAndre Oppermann 	 */
65802a1a643SAndre Oppermann 	if ((tp->t_flags & TF_NOOPT) == 0) {
65902a1a643SAndre Oppermann 		to.to_flags = 0;
66002a1a643SAndre Oppermann 		/* Maximum segment size. */
661df8bae1dSRodney W. Grimes 		if (flags & TH_SYN) {
662df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->iss;
66302a1a643SAndre Oppermann 			to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc);
66402a1a643SAndre Oppermann 			to.to_flags |= TOF_MSS;
665df8bae1dSRodney W. Grimes 		}
66602a1a643SAndre Oppermann 		/* Window scaling. */
66702a1a643SAndre Oppermann 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
66802a1a643SAndre Oppermann 			to.to_wscale = tp->request_r_scale;
66902a1a643SAndre Oppermann 			to.to_flags |= TOF_SCALE;
670df8bae1dSRodney W. Grimes 		}
67102a1a643SAndre Oppermann 		/* Timestamps. */
67202a1a643SAndre Oppermann 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
67302a1a643SAndre Oppermann 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
67402a1a643SAndre Oppermann 			to.to_tsval = ticks + tp->ts_offset;
67502a1a643SAndre Oppermann 			to.to_tsecr = tp->ts_recent;
67602a1a643SAndre Oppermann 			to.to_flags |= TOF_TS;
6776741ecf5SAndre Oppermann 			/* Set receive buffer autosizing timestamp. */
67802a1a643SAndre Oppermann 			if (tp->rfbuf_ts == 0 &&
67902a1a643SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE))
6806741ecf5SAndre Oppermann 				tp->rfbuf_ts = ticks;
6811cfd4b53SBruce M Simpson 		}
68202a1a643SAndre Oppermann 		/* Selective ACK's. */
6833529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
68402a1a643SAndre Oppermann 			if (flags & TH_SYN)
68502a1a643SAndre Oppermann 				to.to_flags |= TOF_SACKPERM;
68602a1a643SAndre Oppermann 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
68702a1a643SAndre Oppermann 			    (tp->t_flags & TF_SACK_PERMIT) &&
68802a1a643SAndre Oppermann 			    tp->rcv_numsacks > 0) {
68902a1a643SAndre Oppermann 				to.to_flags |= TOF_SACK;
69002a1a643SAndre Oppermann 				to.to_nsacks = tp->rcv_numsacks;
69102a1a643SAndre Oppermann 				to.to_sacks = (u_char *)tp->sackblks;
69202a1a643SAndre Oppermann 			}
69302a1a643SAndre Oppermann 		}
69402a1a643SAndre Oppermann #ifdef TCP_SIGNATURE
69502a1a643SAndre Oppermann 		/* TCP-MD5 (RFC2385). */
69602a1a643SAndre Oppermann 		if (tp->t_flags & TF_SIGNATURE)
69702a1a643SAndre Oppermann 			to.to_flags |= TOF_SIGNATURE;
6981cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
6991cfd4b53SBruce M Simpson 
70002a1a643SAndre Oppermann 		/* Processing the options. */
7014a411b9fSBjoern A. Zeeb 		hdrlen += optlen = tcp_addoptions(&to, opt);
702be3f3b5eSPaul Saab 	}
703be3f3b5eSPaul Saab 
704fb59c426SYoshinobu Inoue #ifdef INET6
705fb59c426SYoshinobu Inoue 	if (isipv6)
706fb59c426SYoshinobu Inoue 		ipoptlen = ip6_optlen(tp->t_inpcb);
707fb59c426SYoshinobu Inoue 	else
708fb59c426SYoshinobu Inoue #endif
709f10e85d7SLuigi Rizzo 	if (tp->t_inpcb->inp_options)
710a04884fcSBill Fenner 		ipoptlen = tp->t_inpcb->inp_options->m_len -
711a04884fcSBill Fenner 				offsetof(struct ipoption, ipopt_list);
712f10e85d7SLuigi Rizzo 	else
713a04884fcSBill Fenner 		ipoptlen = 0;
714b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
7159ad0173dSBjoern A. Zeeb 	ipoptlen += ipsec_optlen;
716fb59c426SYoshinobu Inoue #endif
717a04884fcSBill Fenner 
718df8bae1dSRodney W. Grimes 	/*
719df8bae1dSRodney W. Grimes 	 * Adjust data length if insertion of options will
720a0292f23SGarrett Wollman 	 * bump the packet length beyond the t_maxopd length.
721a0292f23SGarrett Wollman 	 * Clear the FIN bit because we cut off the tail of
722a0292f23SGarrett Wollman 	 * the segment.
723b3c0f300SAndre Oppermann 	 *
72431ecb34aSAndre Oppermann 	 * When doing TSO limit a burst to TCP_MAXWIN minus the
72531ecb34aSAndre Oppermann 	 * IP, TCP and Options length to keep ip->ip_len from
72631ecb34aSAndre Oppermann 	 * overflowing.  Prevent the last segment from being
72731ecb34aSAndre Oppermann 	 * fractional thus making them all equal sized and set
7286aa5b623SAndre Oppermann 	 * the flag to continue sending.  TSO is disabled when
7296aa5b623SAndre Oppermann 	 * IP options or IPSEC are present.
730df8bae1dSRodney W. Grimes 	 */
731a04884fcSBill Fenner 	if (len + optlen + ipoptlen > tp->t_maxopd) {
732297a37f3SDavid Greenman 		flags &= ~TH_FIN;
733b3c0f300SAndre Oppermann 		if (tso) {
734eec9d82dSAndre Oppermann 			if (len > TCP_MAXWIN - hdrlen - optlen) {
7356aa5b623SAndre Oppermann 				len = TCP_MAXWIN - hdrlen - optlen;
73631ecb34aSAndre Oppermann 				len = len - (len % (tp->t_maxopd - optlen));
737b3c0f300SAndre Oppermann 				sendalot = 1;
738b3c0f300SAndre Oppermann 			} else if (tp->t_flags & TF_NEEDFIN)
739b3c0f300SAndre Oppermann 				sendalot = 1;
740b3c0f300SAndre Oppermann 		} else {
741a04884fcSBill Fenner 			len = tp->t_maxopd - optlen - ipoptlen;
742df8bae1dSRodney W. Grimes 			sendalot = 1;
743df8bae1dSRodney W. Grimes 		}
744b3c0f300SAndre Oppermann 	}
745df8bae1dSRodney W. Grimes 
746a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/
747a683a7ddSYoshinobu Inoue #ifdef INET6
748a683a7ddSYoshinobu Inoue 	if (max_linkhdr + hdrlen > MCLBYTES)
749a683a7ddSYoshinobu Inoue #else
750df8bae1dSRodney W. Grimes 	if (max_linkhdr + hdrlen > MHLEN)
751a683a7ddSYoshinobu Inoue #endif
752f10e85d7SLuigi Rizzo 		panic("tcphdr too big");
753a0292f23SGarrett Wollman /*#endif*/
754df8bae1dSRodney W. Grimes 
755df8bae1dSRodney W. Grimes 	/*
756c4982faeSBjoern A. Zeeb 	 * This KASSERT is here to catch edge cases at a well defined place.
757c4982faeSBjoern A. Zeeb 	 * Before, those had triggered (random) panic conditions further down.
758c4982faeSBjoern A. Zeeb 	 */
759c4982faeSBjoern A. Zeeb 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
760c4982faeSBjoern A. Zeeb 
761c4982faeSBjoern A. Zeeb 	/*
762df8bae1dSRodney W. Grimes 	 * Grab a header mbuf, attaching a copy of data to
763df8bae1dSRodney W. Grimes 	 * be transmitted, and initialize the header from
764df8bae1dSRodney W. Grimes 	 * the template for sends on this connection.
765df8bae1dSRodney W. Grimes 	 */
766df8bae1dSRodney W. Grimes 	if (len) {
7674e023759SAndre Oppermann 		struct mbuf *mb;
7684e023759SAndre Oppermann 		u_int moff;
7694e023759SAndre Oppermann 
7702cdbfa66SPaul Saab 		if ((tp->t_flags & TF_FORCEDATA) && len == 1)
77178b50714SRobert Watson 			TCPSTAT_INC(tcps_sndprobe);
7720ba5d2eeSJohn Baldwin 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
77378b50714SRobert Watson 			TCPSTAT_INC(tcps_sndrexmitpack);
77478b50714SRobert Watson 			TCPSTAT_ADD(tcps_sndrexmitbyte, len);
775df8bae1dSRodney W. Grimes 		} else {
77678b50714SRobert Watson 			TCPSTAT_INC(tcps_sndpack);
77778b50714SRobert Watson 			TCPSTAT_ADD(tcps_sndbyte, len);
778df8bae1dSRodney W. Grimes 		}
779df8bae1dSRodney W. Grimes #ifdef notyet
780df8bae1dSRodney W. Grimes 		if ((m = m_copypack(so->so_snd.sb_mb, off,
781df8bae1dSRodney W. Grimes 		    (int)len, max_linkhdr + hdrlen)) == 0) {
782cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
783df8bae1dSRodney W. Grimes 			error = ENOBUFS;
784df8bae1dSRodney W. Grimes 			goto out;
785df8bae1dSRodney W. Grimes 		}
786df8bae1dSRodney W. Grimes 		/*
787df8bae1dSRodney W. Grimes 		 * m_copypack left space for our hdr; use it.
788df8bae1dSRodney W. Grimes 		 */
789df8bae1dSRodney W. Grimes 		m->m_len += hdrlen;
790df8bae1dSRodney W. Grimes 		m->m_data -= hdrlen;
791df8bae1dSRodney W. Grimes #else
79234333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
793df8bae1dSRodney W. Grimes 		if (m == NULL) {
794cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
795df8bae1dSRodney W. Grimes 			error = ENOBUFS;
796df8bae1dSRodney W. Grimes 			goto out;
797df8bae1dSRodney W. Grimes 		}
798fb59c426SYoshinobu Inoue #ifdef INET6
799a683a7ddSYoshinobu Inoue 		if (MHLEN < hdrlen + max_linkhdr) {
800a163d034SWarner Losh 			MCLGET(m, M_DONTWAIT);
801a683a7ddSYoshinobu Inoue 			if ((m->m_flags & M_EXT) == 0) {
802cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
803a683a7ddSYoshinobu Inoue 				m_freem(m);
804a683a7ddSYoshinobu Inoue 				error = ENOBUFS;
805a683a7ddSYoshinobu Inoue 				goto out;
806a683a7ddSYoshinobu Inoue 			}
807a683a7ddSYoshinobu Inoue 		}
808fb59c426SYoshinobu Inoue #endif
809df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
810df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
8114e023759SAndre Oppermann 
8124e023759SAndre Oppermann 		/*
8134e023759SAndre Oppermann 		 * Start the m_copy functions from the closest mbuf
8144e023759SAndre Oppermann 		 * to the offset in the socket buffer chain.
8154e023759SAndre Oppermann 		 */
8164e023759SAndre Oppermann 		mb = sbsndptr(&so->so_snd, off, len, &moff);
8174e023759SAndre Oppermann 
818df8bae1dSRodney W. Grimes 		if (len <= MHLEN - hdrlen - max_linkhdr) {
8194e023759SAndre Oppermann 			m_copydata(mb, moff, (int)len,
820df8bae1dSRodney W. Grimes 			    mtod(m, caddr_t) + hdrlen);
821df8bae1dSRodney W. Grimes 			m->m_len += len;
822df8bae1dSRodney W. Grimes 		} else {
8234e023759SAndre Oppermann 			m->m_next = m_copy(mb, moff, (int)len);
8244e023759SAndre Oppermann 			if (m->m_next == NULL) {
825cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
826d7f570e6SGarrett Wollman 				(void) m_free(m);
82751823c3aSGarrett Wollman 				error = ENOBUFS;
82851823c3aSGarrett Wollman 				goto out;
82951823c3aSGarrett Wollman 			}
830df8bae1dSRodney W. Grimes 		}
831df8bae1dSRodney W. Grimes #endif
832df8bae1dSRodney W. Grimes 		/*
833df8bae1dSRodney W. Grimes 		 * If we're sending everything we've got, set PUSH.
834df8bae1dSRodney W. Grimes 		 * (This will keep happy those implementations which only
835df8bae1dSRodney W. Grimes 		 * give data to the user when a buffer fills or
836df8bae1dSRodney W. Grimes 		 * a PUSH comes in.)
837df8bae1dSRodney W. Grimes 		 */
838df8bae1dSRodney W. Grimes 		if (off + len == so->so_snd.sb_cc)
839df8bae1dSRodney W. Grimes 			flags |= TH_PUSH;
840cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
841df8bae1dSRodney W. Grimes 	} else {
842cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
843df8bae1dSRodney W. Grimes 		if (tp->t_flags & TF_ACKNOW)
84478b50714SRobert Watson 			TCPSTAT_INC(tcps_sndacks);
845df8bae1dSRodney W. Grimes 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
84678b50714SRobert Watson 			TCPSTAT_INC(tcps_sndctrl);
847df8bae1dSRodney W. Grimes 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
84878b50714SRobert Watson 			TCPSTAT_INC(tcps_sndurg);
849df8bae1dSRodney W. Grimes 		else
85078b50714SRobert Watson 			TCPSTAT_INC(tcps_sndwinup);
851df8bae1dSRodney W. Grimes 
85234333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
853df8bae1dSRodney W. Grimes 		if (m == NULL) {
854df8bae1dSRodney W. Grimes 			error = ENOBUFS;
855df8bae1dSRodney W. Grimes 			goto out;
856df8bae1dSRodney W. Grimes 		}
857fb59c426SYoshinobu Inoue #ifdef INET6
858fb59c426SYoshinobu Inoue 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
859fb59c426SYoshinobu Inoue 		    MHLEN >= hdrlen) {
860fb59c426SYoshinobu Inoue 			MH_ALIGN(m, hdrlen);
861fb59c426SYoshinobu Inoue 		} else
862fb59c426SYoshinobu Inoue #endif
863df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
864df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
865df8bae1dSRodney W. Grimes 	}
866cf2942b6SRobert Watson 	SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
867df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = (struct ifnet *)0;
868c488362eSRobert Watson #ifdef MAC
86930d239bcSRobert Watson 	mac_inpcb_create_mbuf(tp->t_inpcb, m);
870c488362eSRobert Watson #endif
871fb59c426SYoshinobu Inoue #ifdef INET6
872fb59c426SYoshinobu Inoue 	if (isipv6) {
873fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
874fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip6 + 1);
87579909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip6, th);
876fb59c426SYoshinobu Inoue 	} else
877fb59c426SYoshinobu Inoue #endif /* INET6 */
878fb59c426SYoshinobu Inoue 	{
879fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
880fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
881fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip + 1);
88279909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip, th);
883fb59c426SYoshinobu Inoue 	}
884df8bae1dSRodney W. Grimes 
885df8bae1dSRodney W. Grimes 	/*
886df8bae1dSRodney W. Grimes 	 * Fill in fields, remembering maximum advertised
887df8bae1dSRodney W. Grimes 	 * window for use in delaying messages about window sizes.
888df8bae1dSRodney W. Grimes 	 * If resending a FIN, be sure not to use a new sequence number.
889df8bae1dSRodney W. Grimes 	 */
890df8bae1dSRodney W. Grimes 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
891df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max)
892df8bae1dSRodney W. Grimes 		tp->snd_nxt--;
893df8bae1dSRodney W. Grimes 	/*
894f2512ba1SRui Paulo 	 * If we are starting a connection, send ECN setup
895f2512ba1SRui Paulo 	 * SYN packet. If we are on a retransmit, we may
896f2512ba1SRui Paulo 	 * resend those bits a number of times as per
897f2512ba1SRui Paulo 	 * RFC 3168.
898f2512ba1SRui Paulo 	 */
899603724d3SBjoern A. Zeeb 	if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
900f2512ba1SRui Paulo 		if (tp->t_rxtshift >= 1) {
901603724d3SBjoern A. Zeeb 			if (tp->t_rxtshift <= V_tcp_ecn_maxretries)
902f2512ba1SRui Paulo 				flags |= TH_ECE|TH_CWR;
903f2512ba1SRui Paulo 		} else
904f2512ba1SRui Paulo 			flags |= TH_ECE|TH_CWR;
905f2512ba1SRui Paulo 	}
906f2512ba1SRui Paulo 
907f2512ba1SRui Paulo 	if (tp->t_state == TCPS_ESTABLISHED &&
908f2512ba1SRui Paulo 	    (tp->t_flags & TF_ECN_PERMIT)) {
909f2512ba1SRui Paulo 		/*
910f2512ba1SRui Paulo 		 * If the peer has ECN, mark data packets with
911f2512ba1SRui Paulo 		 * ECN capable transmission (ECT).
912f2512ba1SRui Paulo 		 * Ignore pure ack packets, retransmissions and window probes.
913f2512ba1SRui Paulo 		 */
914f2512ba1SRui Paulo 		if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
915f2512ba1SRui Paulo 		    !((tp->t_flags & TF_FORCEDATA) && len == 1)) {
916f2512ba1SRui Paulo #ifdef INET6
917f2512ba1SRui Paulo 			if (isipv6)
918f2512ba1SRui Paulo 				ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
919f2512ba1SRui Paulo 			else
920f2512ba1SRui Paulo #endif
921f2512ba1SRui Paulo 				ip->ip_tos |= IPTOS_ECN_ECT0;
92278b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
923f2512ba1SRui Paulo 		}
924f2512ba1SRui Paulo 
925f2512ba1SRui Paulo 		/*
926f2512ba1SRui Paulo 		 * Reply with proper ECN notifications.
927f2512ba1SRui Paulo 		 */
928f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_CWR) {
929f2512ba1SRui Paulo 			flags |= TH_CWR;
930f2512ba1SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_CWR;
931f2512ba1SRui Paulo 		}
932f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_ECE)
933f2512ba1SRui Paulo 			flags |= TH_ECE;
934f2512ba1SRui Paulo 	}
935f2512ba1SRui Paulo 
936f2512ba1SRui Paulo 	/*
937df8bae1dSRodney W. Grimes 	 * If we are doing retransmissions, then snd_nxt will
938df8bae1dSRodney W. Grimes 	 * not reflect the first unsent octet.  For ACK only
939df8bae1dSRodney W. Grimes 	 * packets, we do not want the sequence number of the
940df8bae1dSRodney W. Grimes 	 * retransmitted packet, we want the sequence number
941df8bae1dSRodney W. Grimes 	 * of the next unsent octet.  So, if there is no data
942df8bae1dSRodney W. Grimes 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
943df8bae1dSRodney W. Grimes 	 * when filling in ti_seq.  But if we are in persist
944df8bae1dSRodney W. Grimes 	 * state, snd_max might reflect one byte beyond the
945df8bae1dSRodney W. Grimes 	 * right edge of the window, so use snd_nxt in that
946df8bae1dSRodney W. Grimes 	 * case, since we know we aren't doing a retransmission.
947df8bae1dSRodney W. Grimes 	 * (retransmit and persist are mutually exclusive...)
948df8bae1dSRodney W. Grimes 	 */
949a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
950b8152ba7SAndre Oppermann 		if (len || (flags & (TH_SYN|TH_FIN)) ||
951b8152ba7SAndre Oppermann 		    tcp_timer_active(tp, TT_PERSIST))
952fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_nxt);
953df8bae1dSRodney W. Grimes 		else
954fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_max);
955a55db2b6SPaul Saab 	} else {
9566d90faf3SPaul Saab 		th->th_seq = htonl(p->rxmit);
9576d90faf3SPaul Saab 		p->rxmit += len;
9580077b016SPaul Saab 		tp->sackhint.sack_bytes_rexmit += len;
9596d90faf3SPaul Saab 	}
960fb59c426SYoshinobu Inoue 	th->th_ack = htonl(tp->rcv_nxt);
961df8bae1dSRodney W. Grimes 	if (optlen) {
962fb59c426SYoshinobu Inoue 		bcopy(opt, th + 1, optlen);
963fb59c426SYoshinobu Inoue 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
964df8bae1dSRodney W. Grimes 	}
965fb59c426SYoshinobu Inoue 	th->th_flags = flags;
966df8bae1dSRodney W. Grimes 	/*
967df8bae1dSRodney W. Grimes 	 * Calculate receive window.  Don't shrink window,
968df8bae1dSRodney W. Grimes 	 * but avoid silly window syndrome.
969df8bae1dSRodney W. Grimes 	 */
970201d185bSAndre Oppermann 	if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
971201d185bSAndre Oppermann 	    recwin < (long)tp->t_maxseg)
972201d185bSAndre Oppermann 		recwin = 0;
973201d185bSAndre Oppermann 	if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
974201d185bSAndre Oppermann 		recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
975201d185bSAndre Oppermann 	if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
976201d185bSAndre Oppermann 		recwin = (long)TCP_MAXWIN << tp->rcv_scale;
977262c1c1aSMatthew Dillon 
978104ebb2aSAndre Oppermann 	/*
979104ebb2aSAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
980104ebb2aSAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK>
981104ebb2aSAndre Oppermann 	 * case is handled in syncache.
982104ebb2aSAndre Oppermann 	 */
983104ebb2aSAndre Oppermann 	if (flags & TH_SYN)
984104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)
985104ebb2aSAndre Oppermann 				(min(sbspace(&so->so_rcv), TCP_MAXWIN)));
986104ebb2aSAndre Oppermann 	else
987104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
988262c1c1aSMatthew Dillon 
989262c1c1aSMatthew Dillon 	/*
990262c1c1aSMatthew Dillon 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised
991262c1c1aSMatthew Dillon 	 * a 0 window.  This may cause the remote transmitter to stall.  This
992262c1c1aSMatthew Dillon 	 * flag tells soreceive() to disable delayed acknowledgements when
993262c1c1aSMatthew Dillon 	 * draining the buffer.  This can occur if the receiver is attempting
994b2722702SRui Paulo 	 * to read more data than can be buffered prior to transmitting on
995262c1c1aSMatthew Dillon 	 * the connection.
996262c1c1aSMatthew Dillon 	 */
997201d185bSAndre Oppermann 	if (recwin == 0)
998262c1c1aSMatthew Dillon 		tp->t_flags |= TF_RXWIN0SENT;
999262c1c1aSMatthew Dillon 	else
1000262c1c1aSMatthew Dillon 		tp->t_flags &= ~TF_RXWIN0SENT;
1001df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1002fb59c426SYoshinobu Inoue 		th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
1003fb59c426SYoshinobu Inoue 		th->th_flags |= TH_URG;
1004df8bae1dSRodney W. Grimes 	} else
1005df8bae1dSRodney W. Grimes 		/*
1006df8bae1dSRodney W. Grimes 		 * If no urgent pointer to send, then we pull
1007df8bae1dSRodney W. Grimes 		 * the urgent pointer to the left edge of the send window
1008df8bae1dSRodney W. Grimes 		 * so that it doesn't drift into the send window on sequence
1009df8bae1dSRodney W. Grimes 		 * number wraparound.
1010df8bae1dSRodney W. Grimes 		 */
1011df8bae1dSRodney W. Grimes 		tp->snd_up = tp->snd_una;		/* drag it along */
1012df8bae1dSRodney W. Grimes 
10131cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
1014ee763d0dSBjoern A. Zeeb 	if (tp->t_flags & TF_SIGNATURE) {
1015ee763d0dSBjoern A. Zeeb 		int sigoff = to.to_signature - opt;
10163418daf2SBjoern A. Zeeb 		tcp_signature_compute(m, 0, len, optlen,
10171cfd4b53SBruce M Simpson 		    (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
1018ee763d0dSBjoern A. Zeeb 	}
1019265ed012SBruce M Simpson #endif
10201cfd4b53SBruce M Simpson 
1021df8bae1dSRodney W. Grimes 	/*
1022df8bae1dSRodney W. Grimes 	 * Put TCP length in extended header, and then
1023df8bae1dSRodney W. Grimes 	 * checksum extended header and data.
1024df8bae1dSRodney W. Grimes 	 */
1025fb59c426SYoshinobu Inoue 	m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
1026fb59c426SYoshinobu Inoue #ifdef INET6
1027fb59c426SYoshinobu Inoue 	if (isipv6)
1028fb59c426SYoshinobu Inoue 		/*
1029fb59c426SYoshinobu Inoue 		 * ip6_plen is not need to be filled now, and will be filled
1030fb59c426SYoshinobu Inoue 		 * in ip6_output.
1031fb59c426SYoshinobu Inoue 		 */
1032fb59c426SYoshinobu Inoue 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
1033fb59c426SYoshinobu Inoue 				       sizeof(struct tcphdr) + optlen + len);
1034fb59c426SYoshinobu Inoue 	else
1035fb59c426SYoshinobu Inoue #endif /* INET6 */
1036fb59c426SYoshinobu Inoue 	{
1037db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_TCP;
1038db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
103979909384SJonathan Lemon 		th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
104079909384SJonathan Lemon 		    htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
1041fb59c426SYoshinobu Inoue 
1042db4f9cc7SJonathan Lemon 		/* IP version must be set here for ipv4/ipv6 checking later */
1043db4f9cc7SJonathan Lemon 		KASSERT(ip->ip_v == IPVERSION,
10446e551fb6SDavid E. O'Brien 		    ("%s: IP version incorrect: %d", __func__, ip->ip_v));
1045fb59c426SYoshinobu Inoue 	}
1046df8bae1dSRodney W. Grimes 
1047df8bae1dSRodney W. Grimes 	/*
1048b3c0f300SAndre Oppermann 	 * Enable TSO and specify the size of the segments.
1049b3c0f300SAndre Oppermann 	 * The TCP pseudo header checksum is always provided.
1050b3c0f300SAndre Oppermann 	 * XXX: Fixme: This is currently not the case for IPv6.
1051b3c0f300SAndre Oppermann 	 */
1052b3c0f300SAndre Oppermann 	if (tso) {
1053b3c0f300SAndre Oppermann 		m->m_pkthdr.csum_flags = CSUM_TSO;
1054b3c0f300SAndre Oppermann 		m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen;
1055b3c0f300SAndre Oppermann 	}
1056b3c0f300SAndre Oppermann 
1057b3c0f300SAndre Oppermann 	/*
1058df8bae1dSRodney W. Grimes 	 * In transmit state, time the transmission and arrange for
1059df8bae1dSRodney W. Grimes 	 * the retransmit.  In persist state, just set snd_max.
1060df8bae1dSRodney W. Grimes 	 */
10612cdbfa66SPaul Saab 	if ((tp->t_flags & TF_FORCEDATA) == 0 ||
1062b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
1063df8bae1dSRodney W. Grimes 		tcp_seq startseq = tp->snd_nxt;
1064df8bae1dSRodney W. Grimes 
1065df8bae1dSRodney W. Grimes 		/*
1066df8bae1dSRodney W. Grimes 		 * Advance snd_nxt over sequence space of this segment.
1067df8bae1dSRodney W. Grimes 		 */
1068df8bae1dSRodney W. Grimes 		if (flags & (TH_SYN|TH_FIN)) {
1069df8bae1dSRodney W. Grimes 			if (flags & TH_SYN)
1070df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
1071df8bae1dSRodney W. Grimes 			if (flags & TH_FIN) {
1072df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
1073df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_SENTFIN;
1074df8bae1dSRodney W. Grimes 			}
1075df8bae1dSRodney W. Grimes 		}
1076a55db2b6SPaul Saab 		if (sack_rxmit)
10776d90faf3SPaul Saab 			goto timer;
1078df8bae1dSRodney W. Grimes 		tp->snd_nxt += len;
1079df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1080df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt;
1081df8bae1dSRodney W. Grimes 			/*
1082df8bae1dSRodney W. Grimes 			 * Time this transmission if not a retransmission and
1083df8bae1dSRodney W. Grimes 			 * not currently timing anything.
1084df8bae1dSRodney W. Grimes 			 */
10859b8b58e0SJonathan Lemon 			if (tp->t_rtttime == 0) {
10869b8b58e0SJonathan Lemon 				tp->t_rtttime = ticks;
1087df8bae1dSRodney W. Grimes 				tp->t_rtseq = startseq;
108878b50714SRobert Watson 				TCPSTAT_INC(tcps_segstimed);
1089df8bae1dSRodney W. Grimes 			}
1090df8bae1dSRodney W. Grimes 		}
1091df8bae1dSRodney W. Grimes 
1092df8bae1dSRodney W. Grimes 		/*
1093df8bae1dSRodney W. Grimes 		 * Set retransmit timer if not currently set,
109428257b5cSMatthew Dillon 		 * and not doing a pure ack or a keep-alive probe.
1095df8bae1dSRodney W. Grimes 		 * Initial value for retransmit timer is smoothed
1096df8bae1dSRodney W. Grimes 		 * round-trip time + 2 * round-trip time variance.
1097df8bae1dSRodney W. Grimes 		 * Initialize shift counter which is used for backoff
1098df8bae1dSRodney W. Grimes 		 * of retransmit time.
1099df8bae1dSRodney W. Grimes 		 */
11006d90faf3SPaul Saab timer:
11014b8e42baSAndre Oppermann 		if (!tcp_timer_active(tp, TT_REXMT) &&
1102a55db2b6SPaul Saab 		    ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1103a55db2b6SPaul Saab 		     (tp->snd_nxt != tp->snd_una))) {
1104b8152ba7SAndre Oppermann 			if (tcp_timer_active(tp, TT_PERSIST)) {
1105b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_PERSIST, 0);
1106df8bae1dSRodney W. Grimes 				tp->t_rxtshift = 0;
1107df8bae1dSRodney W. Grimes 			}
1108b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
1109df8bae1dSRodney W. Grimes 		}
111028257b5cSMatthew Dillon 	} else {
111128257b5cSMatthew Dillon 		/*
111228257b5cSMatthew Dillon 		 * Persist case, update snd_max but since we are in
111328257b5cSMatthew Dillon 		 * persist mode (no window) we do not update snd_nxt.
111428257b5cSMatthew Dillon 		 */
111528257b5cSMatthew Dillon 		int xlen = len;
111628257b5cSMatthew Dillon 		if (flags & TH_SYN)
111728257b5cSMatthew Dillon 			++xlen;
111828257b5cSMatthew Dillon 		if (flags & TH_FIN) {
111928257b5cSMatthew Dillon 			++xlen;
112028257b5cSMatthew Dillon 			tp->t_flags |= TF_SENTFIN;
112128257b5cSMatthew Dillon 		}
1122abac41a6SMatthew Dillon 		if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
1123df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt + len;
112428257b5cSMatthew Dillon 	}
1125df8bae1dSRodney W. Grimes 
1126610ee2f9SDavid Greenman #ifdef TCPDEBUG
1127df8bae1dSRodney W. Grimes 	/*
1128df8bae1dSRodney W. Grimes 	 * Trace.
1129df8bae1dSRodney W. Grimes 	 */
113091f467d5SHartmut Brandt 	if (so->so_options & SO_DEBUG) {
1131f3e0b7efSBruce M Simpson 		u_short save = 0;
11325214cb3fSBruce M Simpson #ifdef INET6
11335214cb3fSBruce M Simpson 		if (!isipv6)
11345214cb3fSBruce M Simpson #endif
11355214cb3fSBruce M Simpson 		{
11365214cb3fSBruce M Simpson 			save = ipov->ih_len;
113791f467d5SHartmut Brandt 			ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
11385214cb3fSBruce M Simpson 		}
1139fb59c426SYoshinobu Inoue 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
11405214cb3fSBruce M Simpson #ifdef INET6
11415214cb3fSBruce M Simpson 		if (!isipv6)
11425214cb3fSBruce M Simpson #endif
114391f467d5SHartmut Brandt 		ipov->ih_len = save;
114491f467d5SHartmut Brandt 	}
1145610ee2f9SDavid Greenman #endif
1146df8bae1dSRodney W. Grimes 
1147df8bae1dSRodney W. Grimes 	/*
1148df8bae1dSRodney W. Grimes 	 * Fill in IP length and desired time to live and
1149df8bae1dSRodney W. Grimes 	 * send to IP level.  There should be a better way
1150df8bae1dSRodney W. Grimes 	 * to handle ttl and tos; we could keep them in
1151df8bae1dSRodney W. Grimes 	 * the template, but need a way to checksum without them.
1152df8bae1dSRodney W. Grimes 	 */
1153fb59c426SYoshinobu Inoue 	/*
1154fb59c426SYoshinobu Inoue 	 * m->m_pkthdr.len should have been set before cksum calcuration,
1155fb59c426SYoshinobu Inoue 	 * because in6_cksum() need it.
1156fb59c426SYoshinobu Inoue 	 */
1157fb59c426SYoshinobu Inoue #ifdef INET6
1158fb59c426SYoshinobu Inoue 	if (isipv6) {
1159fb59c426SYoshinobu Inoue 		/*
1160fb59c426SYoshinobu Inoue 		 * we separately set hoplimit for every segment, since the
1161fb59c426SYoshinobu Inoue 		 * user might want to change the value via setsockopt.
1162fb59c426SYoshinobu Inoue 		 * Also, desired default hop limit might be changed via
1163fb59c426SYoshinobu Inoue 		 * Neighbor Discovery.
1164fb59c426SYoshinobu Inoue 		 */
116597d8d152SAndre Oppermann 		ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1166fb59c426SYoshinobu Inoue 
1167fb59c426SYoshinobu Inoue 		/* TODO: IPv6 IP6TOS_ECT bit on */
1168fb59c426SYoshinobu Inoue 		error = ip6_output(m,
116997d8d152SAndre Oppermann 			    tp->t_inpcb->in6p_outputopts, NULL,
1170b5d47ff5SJohn-Mark Gurney 			    ((so->so_options & SO_DONTROUTE) ?
1171b5d47ff5SJohn-Mark Gurney 			    IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb);
1172fb59c426SYoshinobu Inoue 	} else
1173fb59c426SYoshinobu Inoue #endif /* INET6 */
1174df8bae1dSRodney W. Grimes     {
1175fb59c426SYoshinobu Inoue 	ip->ip_len = m->m_pkthdr.len;
1176686cdd19SJun-ichiro itojun Hagino #ifdef INET6
11775cd54324SBjoern A. Zeeb 	if (tp->t_inpcb->inp_vflag & INP_IPV6PROTO)
117897d8d152SAndre Oppermann 		ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1179686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */
1180f138387aSGarrett Wollman 	/*
118197d8d152SAndre Oppermann 	 * If we do path MTU discovery, then we set DF on every packet.
118297d8d152SAndre Oppermann 	 * This might not be the best thing to do according to RFC3390
118397d8d152SAndre Oppermann 	 * Section 2. However the tcp hostcache migitates the problem
118497d8d152SAndre Oppermann 	 * so it affects only the first tcp connection with a host.
1185f138387aSGarrett Wollman 	 */
1186603724d3SBjoern A. Zeeb 	if (V_path_mtu_discovery)
1187fb59c426SYoshinobu Inoue 		ip->ip_off |= IP_DF;
118897d8d152SAndre Oppermann 
118997d8d152SAndre Oppermann 	error = ip_output(m, tp->t_inpcb->inp_options, NULL,
1190b5d47ff5SJohn-Mark Gurney 	    ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0,
1191b5d47ff5SJohn-Mark Gurney 	    tp->t_inpcb);
1192df8bae1dSRodney W. Grimes     }
1193df8bae1dSRodney W. Grimes 	if (error) {
11947734ea06SArchie Cobbs 
11957734ea06SArchie Cobbs 		/*
11967734ea06SArchie Cobbs 		 * We know that the packet was lost, so back out the
11977734ea06SArchie Cobbs 		 * sequence number advance, if any.
11982c30ec0aSAndre Oppermann 		 *
11992c30ec0aSAndre Oppermann 		 * If the error is EPERM the packet got blocked by the
12002c30ec0aSAndre Oppermann 		 * local firewall.  Normally we should terminate the
12012c30ec0aSAndre Oppermann 		 * connection but the blocking may have been spurious
12022c30ec0aSAndre Oppermann 		 * due to a firewall reconfiguration cycle.  So we treat
12032c30ec0aSAndre Oppermann 		 * it like a packet loss and let the retransmit timer and
12042c30ec0aSAndre Oppermann 		 * timeouts do their work over time.
12052c30ec0aSAndre Oppermann 		 * XXX: It is a POLA question whether calling tcp_drop right
12062c30ec0aSAndre Oppermann 		 * away would be the really correct behavior instead.
12077734ea06SArchie Cobbs 		 */
120872757d9aSGleb Smirnoff 		if (((tp->t_flags & TF_FORCEDATA) == 0 ||
1209b8152ba7SAndre Oppermann 		    !tcp_timer_active(tp, TT_PERSIST)) &&
121072757d9aSGleb Smirnoff 		    ((flags & TH_SYN) == 0) &&
121172757d9aSGleb Smirnoff 		    (error != EPERM)) {
12120077b016SPaul Saab 			if (sack_rxmit) {
12135d3b1b75SJayanth Vijayaraghavan 				p->rxmit -= len;
12140077b016SPaul Saab 				tp->sackhint.sack_bytes_rexmit -= len;
121572757d9aSGleb Smirnoff 				KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
12160077b016SPaul Saab 				    ("sackhint bytes rtx >= 0"));
12170077b016SPaul Saab 			} else
12187734ea06SArchie Cobbs 				tp->snd_nxt -= len;
12197734ea06SArchie Cobbs 		}
1220df8bae1dSRodney W. Grimes out:
1221cf2942b6SRobert Watson 		SOCKBUF_UNLOCK_ASSERT(&so->so_snd);	/* Check gotos. */
122272757d9aSGleb Smirnoff 		switch (error) {
122372757d9aSGleb Smirnoff 		case EPERM:
122472757d9aSGleb Smirnoff 			tp->t_softerror = error;
122572757d9aSGleb Smirnoff 			return (error);
122672757d9aSGleb Smirnoff 		case ENOBUFS:
1227b8152ba7SAndre Oppermann 	                if (!tcp_timer_active(tp, TT_REXMT) &&
1228b8152ba7SAndre Oppermann 			    !tcp_timer_active(tp, TT_PERSIST))
1229b8152ba7SAndre Oppermann 	                        tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
12301600372bSAndre Oppermann 			tp->snd_cwnd = tp->t_maxseg;
1231df8bae1dSRodney W. Grimes 			return (0);
123272757d9aSGleb Smirnoff 		case EMSGSIZE:
12333d1f141bSGarrett Wollman 			/*
1234b3c0f300SAndre Oppermann 			 * For some reason the interface we used initially
1235b3c0f300SAndre Oppermann 			 * to send segments changed to another or lowered
1236b3c0f300SAndre Oppermann 			 * its MTU.
1237b3c0f300SAndre Oppermann 			 *
1238b3c0f300SAndre Oppermann 			 * tcp_mtudisc() will find out the new MTU and as
1239b3c0f300SAndre Oppermann 			 * its last action, initiate retransmission, so it
1240b3c0f300SAndre Oppermann 			 * is important to not do so here.
1241b3c0f300SAndre Oppermann 			 *
1242b3c0f300SAndre Oppermann 			 * If TSO was active we either got an interface
1243b3c0f300SAndre Oppermann 			 * without TSO capabilits or TSO was turned off.
1244b3c0f300SAndre Oppermann 			 * Disable it for this connection as too and
1245b3c0f300SAndre Oppermann 			 * immediatly retry with MSS sized segments generated
1246b3c0f300SAndre Oppermann 			 * by this function.
12473d1f141bSGarrett Wollman 			 */
1248b3c0f300SAndre Oppermann 			if (tso)
1249b3c0f300SAndre Oppermann 				tp->t_flags &= ~TF_TSO;
12503d1f141bSGarrett Wollman 			tcp_mtudisc(tp->t_inpcb, 0);
125172757d9aSGleb Smirnoff 			return (0);
12528bec3467SGleb Smirnoff 		case EHOSTDOWN:
125372757d9aSGleb Smirnoff 		case EHOSTUNREACH:
125472757d9aSGleb Smirnoff 		case ENETDOWN:
12558bec3467SGleb Smirnoff 		case ENETUNREACH:
125672757d9aSGleb Smirnoff 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
1257df8bae1dSRodney W. Grimes 				tp->t_softerror = error;
1258df8bae1dSRodney W. Grimes 				return (0);
1259df8bae1dSRodney W. Grimes 			}
126072757d9aSGleb Smirnoff 			/* FALLTHROUGH */
126172757d9aSGleb Smirnoff 		default:
1262df8bae1dSRodney W. Grimes 			return (error);
1263df8bae1dSRodney W. Grimes 		}
126472757d9aSGleb Smirnoff 	}
126578b50714SRobert Watson 	TCPSTAT_INC(tcps_sndtotal);
1266df8bae1dSRodney W. Grimes 
1267df8bae1dSRodney W. Grimes 	/*
1268df8bae1dSRodney W. Grimes 	 * Data sent (as far as we can tell).
1269df8bae1dSRodney W. Grimes 	 * If this advertises a larger window than any other segment,
1270df8bae1dSRodney W. Grimes 	 * then remember the size of the advertised window.
1271df8bae1dSRodney W. Grimes 	 * Any pending ACK has now been sent.
1272df8bae1dSRodney W. Grimes 	 */
1273201d185bSAndre Oppermann 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1274201d185bSAndre Oppermann 		tp->rcv_adv = tp->rcv_nxt + recwin;
1275df8bae1dSRodney W. Grimes 	tp->last_ack_sent = tp->rcv_nxt;
12763bfd6421SJonathan Lemon 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
1277b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_DELACK))
1278b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, 0);
1279d912c694SMatthew Dillon #if 0
1280d912c694SMatthew Dillon 	/*
1281d912c694SMatthew Dillon 	 * This completely breaks TCP if newreno is turned on.  What happens
1282d912c694SMatthew Dillon 	 * is that if delayed-acks are turned on on the receiver, this code
1283d912c694SMatthew Dillon 	 * on the transmitter effectively destroys the TCP window, forcing
1284d912c694SMatthew Dillon 	 * it to four packets (1.5Kx4 = 6K window).
1285d912c694SMatthew Dillon 	 */
1286603724d3SBjoern A. Zeeb 	if (sendalot && (!V_tcp_do_newreno || --maxburst))
1287df8bae1dSRodney W. Grimes 		goto again;
1288d912c694SMatthew Dillon #endif
1289d912c694SMatthew Dillon 	if (sendalot)
1290d912c694SMatthew Dillon 		goto again;
1291df8bae1dSRodney W. Grimes 	return (0);
1292df8bae1dSRodney W. Grimes }
1293df8bae1dSRodney W. Grimes 
1294df8bae1dSRodney W. Grimes void
1295ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp)
1296df8bae1dSRodney W. Grimes {
12979b8b58e0SJonathan Lemon 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
12989b8b58e0SJonathan Lemon 	int tt;
1299df8bae1dSRodney W. Grimes 
1300b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_REXMT))
13019b8b58e0SJonathan Lemon 		panic("tcp_setpersist: retransmit pending");
1302df8bae1dSRodney W. Grimes 	/*
1303df8bae1dSRodney W. Grimes 	 * Start/restart persistance timer.
1304df8bae1dSRodney W. Grimes 	 */
13059b8b58e0SJonathan Lemon 	TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1306df8bae1dSRodney W. Grimes 		      TCPTV_PERSMIN, TCPTV_PERSMAX);
1307b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_PERSIST, tt);
1308df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1309df8bae1dSRodney W. Grimes 		tp->t_rxtshift++;
1310df8bae1dSRodney W. Grimes }
131102a1a643SAndre Oppermann 
131202a1a643SAndre Oppermann /*
131302a1a643SAndre Oppermann  * Insert TCP options according to the supplied parameters to the place
131402a1a643SAndre Oppermann  * optp in a consistent way.  Can handle unaligned destinations.
131502a1a643SAndre Oppermann  *
131602a1a643SAndre Oppermann  * The order of the option processing is crucial for optimal packing and
131702a1a643SAndre Oppermann  * alignment for the scarce option space.
131802a1a643SAndre Oppermann  *
131902a1a643SAndre Oppermann  * The optimal order for a SYN/SYN-ACK segment is:
132002a1a643SAndre Oppermann  *   MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) +
132102a1a643SAndre Oppermann  *   Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40.
132202a1a643SAndre Oppermann  *
132302a1a643SAndre Oppermann  * The SACK options should be last.  SACK blocks consume 8*n+2 bytes.
132402a1a643SAndre Oppermann  * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks).
132502a1a643SAndre Oppermann  * At minimum we need 10 bytes (to generate 1 SACK block).  If both
132602a1a643SAndre Oppermann  * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present,
132702a1a643SAndre Oppermann  * we only have 10 bytes for SACK options (40 - (12 + 18)).
132802a1a643SAndre Oppermann  */
132902a1a643SAndre Oppermann int
133002a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp)
133102a1a643SAndre Oppermann {
13328b615593SMarko Zec 	INIT_VNET_INET(curvnet);
133302a1a643SAndre Oppermann 	u_int mask, optlen = 0;
133402a1a643SAndre Oppermann 
133502a1a643SAndre Oppermann 	for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) {
133602a1a643SAndre Oppermann 		if ((to->to_flags & mask) != mask)
133702a1a643SAndre Oppermann 			continue;
13383a4018c4SAndre Oppermann 		if (optlen == TCP_MAXOLEN)
13393a4018c4SAndre Oppermann 			break;
134002a1a643SAndre Oppermann 		switch (to->to_flags & mask) {
134102a1a643SAndre Oppermann 		case TOF_MSS:
134202a1a643SAndre Oppermann 			while (optlen % 4) {
134302a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
134402a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
134502a1a643SAndre Oppermann 			}
13463a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG)
13473a4018c4SAndre Oppermann 				continue;
134802a1a643SAndre Oppermann 			optlen += TCPOLEN_MAXSEG;
134902a1a643SAndre Oppermann 			*optp++ = TCPOPT_MAXSEG;
135002a1a643SAndre Oppermann 			*optp++ = TCPOLEN_MAXSEG;
135102a1a643SAndre Oppermann 			to->to_mss = htons(to->to_mss);
135202a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss));
135302a1a643SAndre Oppermann 			optp += sizeof(to->to_mss);
135402a1a643SAndre Oppermann 			break;
135502a1a643SAndre Oppermann 		case TOF_SCALE:
135602a1a643SAndre Oppermann 			while (!optlen || optlen % 2 != 1) {
135702a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
135802a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
135902a1a643SAndre Oppermann 			}
13603a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW)
13613a4018c4SAndre Oppermann 				continue;
136202a1a643SAndre Oppermann 			optlen += TCPOLEN_WINDOW;
136302a1a643SAndre Oppermann 			*optp++ = TCPOPT_WINDOW;
136402a1a643SAndre Oppermann 			*optp++ = TCPOLEN_WINDOW;
136502a1a643SAndre Oppermann 			*optp++ = to->to_wscale;
136602a1a643SAndre Oppermann 			break;
136702a1a643SAndre Oppermann 		case TOF_SACKPERM:
136802a1a643SAndre Oppermann 			while (optlen % 2) {
136902a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
137002a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
137102a1a643SAndre Oppermann 			}
13723a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED)
13733a4018c4SAndre Oppermann 				continue;
137402a1a643SAndre Oppermann 			optlen += TCPOLEN_SACK_PERMITTED;
137502a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK_PERMITTED;
137602a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACK_PERMITTED;
137702a1a643SAndre Oppermann 			break;
137802a1a643SAndre Oppermann 		case TOF_TS:
137902a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
138002a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
138102a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
138202a1a643SAndre Oppermann 			}
13833a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP)
13843a4018c4SAndre Oppermann 				continue;
138502a1a643SAndre Oppermann 			optlen += TCPOLEN_TIMESTAMP;
138602a1a643SAndre Oppermann 			*optp++ = TCPOPT_TIMESTAMP;
138702a1a643SAndre Oppermann 			*optp++ = TCPOLEN_TIMESTAMP;
138802a1a643SAndre Oppermann 			to->to_tsval = htonl(to->to_tsval);
138902a1a643SAndre Oppermann 			to->to_tsecr = htonl(to->to_tsecr);
139002a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval));
139102a1a643SAndre Oppermann 			optp += sizeof(to->to_tsval);
139202a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr));
139302a1a643SAndre Oppermann 			optp += sizeof(to->to_tsecr);
139402a1a643SAndre Oppermann 			break;
139502a1a643SAndre Oppermann 		case TOF_SIGNATURE:
139602a1a643SAndre Oppermann 			{
139702a1a643SAndre Oppermann 			int siglen = TCPOLEN_SIGNATURE - 2;
139802a1a643SAndre Oppermann 
139902a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
140002a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
140102a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
140202a1a643SAndre Oppermann 			}
14030d957bbaSAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE)
140402a1a643SAndre Oppermann 				continue;
140502a1a643SAndre Oppermann 			optlen += TCPOLEN_SIGNATURE;
140602a1a643SAndre Oppermann 			*optp++ = TCPOPT_SIGNATURE;
140702a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SIGNATURE;
140802a1a643SAndre Oppermann 			to->to_signature = optp;
140902a1a643SAndre Oppermann 			while (siglen--)
141002a1a643SAndre Oppermann 				 *optp++ = 0;
141102a1a643SAndre Oppermann 			break;
141202a1a643SAndre Oppermann 			}
141302a1a643SAndre Oppermann 		case TOF_SACK:
141402a1a643SAndre Oppermann 			{
141502a1a643SAndre Oppermann 			int sackblks = 0;
141602a1a643SAndre Oppermann 			struct sackblk *sack = (struct sackblk *)to->to_sacks;
141702a1a643SAndre Oppermann 			tcp_seq sack_seq;
141802a1a643SAndre Oppermann 
141902a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
142002a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
142102a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
142202a1a643SAndre Oppermann 			}
14233a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK)
142402a1a643SAndre Oppermann 				continue;
142502a1a643SAndre Oppermann 			optlen += TCPOLEN_SACKHDR;
142602a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK;
142702a1a643SAndre Oppermann 			sackblks = min(to->to_nsacks,
14280d957bbaSAndre Oppermann 					(TCP_MAXOLEN - optlen) / TCPOLEN_SACK);
142902a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK;
143002a1a643SAndre Oppermann 			while (sackblks--) {
143102a1a643SAndre Oppermann 				sack_seq = htonl(sack->start);
143202a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
143302a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
143402a1a643SAndre Oppermann 				sack_seq = htonl(sack->end);
143502a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
143602a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
143702a1a643SAndre Oppermann 				optlen += TCPOLEN_SACK;
143802a1a643SAndre Oppermann 				sack++;
143902a1a643SAndre Oppermann 			}
144078b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_send_blocks);
144102a1a643SAndre Oppermann 			break;
144202a1a643SAndre Oppermann 			}
144302a1a643SAndre Oppermann 		default:
144402a1a643SAndre Oppermann 			panic("%s: unknown TCP option type", __func__);
144502a1a643SAndre Oppermann 			break;
144602a1a643SAndre Oppermann 		}
144702a1a643SAndre Oppermann 	}
144802a1a643SAndre Oppermann 
144902a1a643SAndre Oppermann 	/* Terminate and pad TCP options to a 4 byte boundary. */
145002a1a643SAndre Oppermann 	if (optlen % 4) {
145102a1a643SAndre Oppermann 		optlen += TCPOLEN_EOL;
145202a1a643SAndre Oppermann 		*optp++ = TCPOPT_EOL;
145302a1a643SAndre Oppermann 	}
1454413deb12SBjoern A. Zeeb 	/*
1455413deb12SBjoern A. Zeeb 	 * According to RFC 793 (STD0007):
1456413deb12SBjoern A. Zeeb 	 *   "The content of the header beyond the End-of-Option option
1457413deb12SBjoern A. Zeeb 	 *    must be header padding (i.e., zero)."
1458413deb12SBjoern A. Zeeb 	 *   and later: "The padding is composed of zeros."
1459413deb12SBjoern A. Zeeb 	 */
146002a1a643SAndre Oppermann 	while (optlen % 4) {
1461c343c524SAndre Oppermann 		optlen += TCPOLEN_PAD;
1462c343c524SAndre Oppermann 		*optp++ = TCPOPT_PAD;
146302a1a643SAndre Oppermann 	}
146402a1a643SAndre Oppermann 
14650d957bbaSAndre Oppermann 	KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__));
146602a1a643SAndre Oppermann 	return (optlen);
146702a1a643SAndre Oppermann }
1468