xref: /freebsd/sys/netinet/tcp_output.c (revision 0645c6049ddf6277262a789c5ac232c951b8f853)
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"
380cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
390cc12cc5SJoerg Wunsch 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
42686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h>
4339bc9de5SLawrence Stewart #include <sys/hhook.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>
4957f60867SMark Johnston #include <sys/sdt.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
52fb919e4dSMark Murray #include <sys/sysctl.h>
53df8bae1dSRodney W. Grimes 
544b79449eSBjoern A. Zeeb #include <net/if.h>
55df8bae1dSRodney W. Grimes #include <net/route.h>
56530c0060SRobert Watson #include <net/vnet.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <netinet/in.h>
5957f60867SMark Johnston #include <netinet/in_kdtrace.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
61df8bae1dSRodney W. Grimes #include <netinet/ip.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
63df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
64ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
65fb59c426SYoshinobu Inoue #ifdef INET6
66686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
67686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
68fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
69fb59c426SYoshinobu Inoue #endif
70281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
71281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h>
72281a0fd4SPatrick Kelsey #endif
732de3e790SGleb Smirnoff #include <netinet/tcp.h>
74df8bae1dSRodney W. Grimes #define	TCPOUTFLAGS
75df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
76df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
77df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
78df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
79df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
802de3e790SGleb Smirnoff #include <netinet/tcp_cc.h>
8186a996e6SHiren Panchasara #ifdef TCPPCAP
8286a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
8386a996e6SHiren Panchasara #endif
84610ee2f9SDavid Greenman #ifdef TCPDEBUG
85df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
86610ee2f9SDavid Greenman #endif
8709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
8809fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
8909fe6320SNavdeep Parhar #endif
90df8bae1dSRodney W. Grimes 
91b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
92b9234fafSSam Leffler #include <netipsec/ipsec.h>
93b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
94b9234fafSSam Leffler 
95db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
96db4f9cc7SJonathan Lemon 
97aed55708SRobert Watson #include <security/mac/mac_framework.h>
98aed55708SRobert Watson 
9982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, path_mtu_discovery) = 1;
1006df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_VNET | CTLFLAG_RW,
101eddfbb76SRobert Watson 	&VNET_NAME(path_mtu_discovery), 1,
102eddfbb76SRobert Watson 	"Enable Path MTU Discovery");
1039a039a5fSDavid Greenman 
10482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_tso) = 1;
10582cea7e6SBjoern A. Zeeb #define	V_tcp_do_tso		VNET(tcp_do_tso)
1066df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_VNET | CTLFLAG_RW,
107eddfbb76SRobert Watson 	&VNET_NAME(tcp_do_tso), 0,
108eddfbb76SRobert Watson 	"Enable TCP Segmentation Offload");
109b3c0f300SAndre Oppermann 
110873789cbSAndre Oppermann VNET_DEFINE(int, tcp_sendspace) = 1024*32;
111873789cbSAndre Oppermann #define	V_tcp_sendspace	VNET(tcp_sendspace)
1126df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_VNET | CTLFLAG_RW,
113873789cbSAndre Oppermann 	&VNET_NAME(tcp_sendspace), 0, "Initial send socket buffer size");
114873789cbSAndre Oppermann 
11582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autosndbuf) = 1;
11682cea7e6SBjoern A. Zeeb #define	V_tcp_do_autosndbuf	VNET(tcp_do_autosndbuf)
1176df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
118eddfbb76SRobert Watson 	&VNET_NAME(tcp_do_autosndbuf), 0,
119eddfbb76SRobert Watson 	"Enable automatic send buffer sizing");
1206741ecf5SAndre Oppermann 
12182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_inc) = 8*1024;
12282cea7e6SBjoern A. Zeeb #define	V_tcp_autosndbuf_inc	VNET(tcp_autosndbuf_inc)
1236df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_VNET | CTLFLAG_RW,
124eddfbb76SRobert Watson 	&VNET_NAME(tcp_autosndbuf_inc), 0,
1258b615593SMarko Zec 	"Incrementor step size of automatic send buffer");
1266741ecf5SAndre Oppermann 
127b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_max) = 2*1024*1024;
12882cea7e6SBjoern A. Zeeb #define	V_tcp_autosndbuf_max	VNET(tcp_autosndbuf_max)
1296df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
130eddfbb76SRobert Watson 	&VNET_NAME(tcp_autosndbuf_max), 0,
1318b615593SMarko Zec 	"Max size of automatic send buffer");
1326741ecf5SAndre Oppermann 
13339bc9de5SLawrence Stewart static void inline	hhook_run_tcp_est_out(struct tcpcb *tp,
13439bc9de5SLawrence Stewart 			    struct tcphdr *th, struct tcpopt *to,
13539bc9de5SLawrence Stewart 			    long len, int tso);
136dbc42409SLawrence Stewart static void inline	cc_after_idle(struct tcpcb *tp);
137dbc42409SLawrence Stewart 
138dbc42409SLawrence Stewart /*
1390f5e7edcSKevin Lo  * Wrapper for the TCP established output helper hook.
14039bc9de5SLawrence Stewart  */
14139bc9de5SLawrence Stewart static void inline
14239bc9de5SLawrence Stewart hhook_run_tcp_est_out(struct tcpcb *tp, struct tcphdr *th,
14339bc9de5SLawrence Stewart     struct tcpopt *to, long len, int tso)
14439bc9de5SLawrence Stewart {
14539bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
14639bc9de5SLawrence Stewart 
14739bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_OUT]->hhh_nhooks > 0) {
14839bc9de5SLawrence Stewart 		hhook_data.tp = tp;
14939bc9de5SLawrence Stewart 		hhook_data.th = th;
15039bc9de5SLawrence Stewart 		hhook_data.to = to;
15139bc9de5SLawrence Stewart 		hhook_data.len = len;
15239bc9de5SLawrence Stewart 		hhook_data.tso = tso;
15339bc9de5SLawrence Stewart 
15439bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_OUT], &hhook_data,
15539bc9de5SLawrence Stewart 		    tp->osd);
15639bc9de5SLawrence Stewart 	}
15739bc9de5SLawrence Stewart }
15839bc9de5SLawrence Stewart 
15939bc9de5SLawrence Stewart /*
160dbc42409SLawrence Stewart  * CC wrapper hook functions
161dbc42409SLawrence Stewart  */
162dbc42409SLawrence Stewart static void inline
163dbc42409SLawrence Stewart cc_after_idle(struct tcpcb *tp)
164dbc42409SLawrence Stewart {
165dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
166dbc42409SLawrence Stewart 
167dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->after_idle != NULL)
168dbc42409SLawrence Stewart 		CC_ALGO(tp)->after_idle(tp->ccv);
169dbc42409SLawrence Stewart }
1706741ecf5SAndre Oppermann 
171df8bae1dSRodney W. Grimes /*
172df8bae1dSRodney W. Grimes  * Tcp output routine: figure out what should be sent and send it.
173df8bae1dSRodney W. Grimes  */
174df8bae1dSRodney W. Grimes int
175f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp)
176df8bae1dSRodney W. Grimes {
177f10e85d7SLuigi Rizzo 	struct socket *so = tp->t_inpcb->inp_socket;
178201d185bSAndre Oppermann 	long len, recwin, sendwin;
179b287c6c7SBjoern A. Zeeb 	int off, flags, error = 0;	/* Keep compiler happy */
180f10e85d7SLuigi Rizzo 	struct mbuf *m;
181fb59c426SYoshinobu Inoue 	struct ip *ip = NULL;
182f10e85d7SLuigi Rizzo 	struct ipovly *ipov = NULL;
183f10e85d7SLuigi Rizzo 	struct tcphdr *th;
184a0292f23SGarrett Wollman 	u_char opt[TCP_MAXOLEN];
185a04884fcSBill Fenner 	unsigned ipoptlen, optlen, hdrlen;
1869ad0173dSBjoern A. Zeeb #ifdef IPSEC
1879ad0173dSBjoern A. Zeeb 	unsigned ipsec_optlen = 0;
1889ad0173dSBjoern A. Zeeb #endif
189df8bae1dSRodney W. Grimes 	int idle, sendalot;
19002a1a643SAndre Oppermann 	int sack_rxmit, sack_bytes_rxmt;
1916d90faf3SPaul Saab 	struct sackhole *p;
192df0633a1SGleb Smirnoff 	int tso, mtu;
19302a1a643SAndre Oppermann 	struct tcpopt to;
194262c1c1aSMatthew Dillon #if 0
19546f58482SJonathan Lemon 	int maxburst = TCP_MAXBURST;
196262c1c1aSMatthew Dillon #endif
197fb59c426SYoshinobu Inoue #ifdef INET6
198f10e85d7SLuigi Rizzo 	struct ip6_hdr *ip6 = NULL;
199fb59c426SYoshinobu Inoue 	int isipv6;
200fb59c426SYoshinobu Inoue 
201fb59c426SYoshinobu Inoue 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
202fb59c426SYoshinobu Inoue #endif
203df8bae1dSRodney W. Grimes 
2048501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2053d6ade3aSJennifer Yang 
20609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
20709fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
20809fe6320SNavdeep Parhar 		return (tcp_offload_output(tp));
20909fe6320SNavdeep Parhar #endif
21009fe6320SNavdeep Parhar 
211281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
212281a0fd4SPatrick Kelsey 	/*
213281a0fd4SPatrick Kelsey 	 * For TFO connections in SYN_RECEIVED, only allow the initial
214281a0fd4SPatrick Kelsey 	 * SYN|ACK and those sent by the retransmit timer.
215281a0fd4SPatrick Kelsey 	 */
216281a0fd4SPatrick Kelsey 	if ((tp->t_flags & TF_FASTOPEN) &&
217281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED) &&
218281a0fd4SPatrick Kelsey 	    SEQ_GT(tp->snd_max, tp->snd_una) &&    /* inital SYN|ACK sent */
219281a0fd4SPatrick Kelsey 	    (tp->snd_nxt != tp->snd_una))          /* not a retransmit */
220281a0fd4SPatrick Kelsey 		return (0);
221281a0fd4SPatrick Kelsey #endif
222df8bae1dSRodney W. Grimes 	/*
223df8bae1dSRodney W. Grimes 	 * Determine length of data that should be transmitted,
224df8bae1dSRodney W. Grimes 	 * and flags that will be used.
225df8bae1dSRodney W. Grimes 	 * If there is some data or critical controls (SYN, RST)
226df8bae1dSRodney W. Grimes 	 * to send, then transmit; otherwise, investigate further.
227df8bae1dSRodney W. Grimes 	 */
228c24d5daeSJayanth Vijayaraghavan 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
2292ea8da28SLawrence Stewart 	if (idle && ticks - tp->t_rcvtime >= tp->t_rxtcur)
2302ea8da28SLawrence Stewart 		cc_after_idle(tp);
231c24d5daeSJayanth Vijayaraghavan 	tp->t_flags &= ~TF_LASTIDLE;
232c24d5daeSJayanth Vijayaraghavan 	if (idle) {
233c24d5daeSJayanth Vijayaraghavan 		if (tp->t_flags & TF_MORETOCOME) {
234c24d5daeSJayanth Vijayaraghavan 			tp->t_flags |= TF_LASTIDLE;
235c24d5daeSJayanth Vijayaraghavan 			idle = 0;
236c24d5daeSJayanth Vijayaraghavan 		}
237c24d5daeSJayanth Vijayaraghavan 	}
238df8bae1dSRodney W. Grimes again:
2396d90faf3SPaul Saab 	/*
2406d90faf3SPaul Saab 	 * If we've recently taken a timeout, snd_max will be greater than
2416d90faf3SPaul Saab 	 * snd_nxt.  There may be SACK information that allows us to avoid
2426d90faf3SPaul Saab 	 * resending already delivered data.  Adjust snd_nxt accordingly.
2436d90faf3SPaul Saab 	 */
2443529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
2453529149eSAndre Oppermann 	    SEQ_LT(tp->snd_nxt, tp->snd_max))
2466d90faf3SPaul Saab 		tcp_sack_adjust(tp);
247df8bae1dSRodney W. Grimes 	sendalot = 0;
248153e5b57SAndre Oppermann 	tso = 0;
249df0633a1SGleb Smirnoff 	mtu = 0;
250df8bae1dSRodney W. Grimes 	off = tp->snd_nxt - tp->snd_una;
251201d185bSAndre Oppermann 	sendwin = min(tp->snd_wnd, tp->snd_cwnd);
252df8bae1dSRodney W. Grimes 
253df8bae1dSRodney W. Grimes 	flags = tcp_outflags[tp->t_state];
254a0292f23SGarrett Wollman 	/*
2556d90faf3SPaul Saab 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
2566d90faf3SPaul Saab 	 * to send out new data (when sendalot is 1), bypass this function.
2576d90faf3SPaul Saab 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
2586d90faf3SPaul Saab 	 * we're replacing a (future) new transmission with a retransmission
2596d90faf3SPaul Saab 	 * now, and we previously incremented snd_cwnd in tcp_input().
2606d90faf3SPaul Saab 	 */
2616d90faf3SPaul Saab 	/*
2626d90faf3SPaul Saab 	 * Still in sack recovery , reset rxmit flag to zero.
2636d90faf3SPaul Saab 	 */
2646d90faf3SPaul Saab 	sack_rxmit = 0;
265a55db2b6SPaul Saab 	sack_bytes_rxmt = 0;
2666d90faf3SPaul Saab 	len = 0;
2676d90faf3SPaul Saab 	p = NULL;
268dbc42409SLawrence Stewart 	if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp->t_flags) &&
269a55db2b6SPaul Saab 	    (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
270a55db2b6SPaul Saab 		long cwin;
271a55db2b6SPaul Saab 
272a55db2b6SPaul Saab 		cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
273a55db2b6SPaul Saab 		if (cwin < 0)
274a55db2b6SPaul Saab 			cwin = 0;
275f787edd8SJayanth Vijayaraghavan 		/* Do not retransmit SACK segments beyond snd_recover */
276f787edd8SJayanth Vijayaraghavan 		if (SEQ_GT(p->end, tp->snd_recover)) {
277f787edd8SJayanth Vijayaraghavan 			/*
278f787edd8SJayanth Vijayaraghavan 			 * (At least) part of sack hole extends beyond
279f787edd8SJayanth Vijayaraghavan 			 * snd_recover. Check to see if we can rexmit data
280f787edd8SJayanth Vijayaraghavan 			 * for this hole.
281f787edd8SJayanth Vijayaraghavan 			 */
282f787edd8SJayanth Vijayaraghavan 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
283f787edd8SJayanth Vijayaraghavan 				/*
284f787edd8SJayanth Vijayaraghavan 				 * Can't rexmit any more data for this hole.
285f787edd8SJayanth Vijayaraghavan 				 * That data will be rexmitted in the next
286f787edd8SJayanth Vijayaraghavan 				 * sack recovery episode, when snd_recover
287f787edd8SJayanth Vijayaraghavan 				 * moves past p->rxmit.
288f787edd8SJayanth Vijayaraghavan 				 */
289f787edd8SJayanth Vijayaraghavan 				p = NULL;
290f787edd8SJayanth Vijayaraghavan 				goto after_sack_rexmit;
291f787edd8SJayanth Vijayaraghavan 			} else
292f787edd8SJayanth Vijayaraghavan 				/* Can rexmit part of the current hole */
293a55db2b6SPaul Saab 				len = ((long)ulmin(cwin,
294f787edd8SJayanth Vijayaraghavan 						   tp->snd_recover - p->rxmit));
295f787edd8SJayanth Vijayaraghavan 		} else
296a55db2b6SPaul Saab 			len = ((long)ulmin(cwin, p->end - p->rxmit));
2976d90faf3SPaul Saab 		off = p->rxmit - tp->snd_una;
298f787edd8SJayanth Vijayaraghavan 		KASSERT(off >= 0,("%s: sack block to the left of una : %d",
299f787edd8SJayanth Vijayaraghavan 		    __func__, off));
3006d90faf3SPaul Saab 		if (len > 0) {
301ab5c14d8SRobert Watson 			sack_rxmit = 1;
302ab5c14d8SRobert Watson 			sendalot = 1;
30378b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rexmits);
30478b50714SRobert Watson 			TCPSTAT_ADD(tcps_sack_rexmit_bytes,
30578b50714SRobert Watson 			    min(len, tp->t_maxseg));
3066d90faf3SPaul Saab 		}
3076d90faf3SPaul Saab 	}
308f787edd8SJayanth Vijayaraghavan after_sack_rexmit:
3096d90faf3SPaul Saab 	/*
310a0292f23SGarrett Wollman 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
311a0292f23SGarrett Wollman 	 * state flags.
312a0292f23SGarrett Wollman 	 */
313a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDFIN)
314a0292f23SGarrett Wollman 		flags |= TH_FIN;
315a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDSYN)
316a0292f23SGarrett Wollman 		flags |= TH_SYN;
317a0292f23SGarrett Wollman 
318cf2942b6SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
319df8bae1dSRodney W. Grimes 	/*
320df8bae1dSRodney W. Grimes 	 * If in persist timeout with window of 0, send 1 byte.
321df8bae1dSRodney W. Grimes 	 * Otherwise, if window is small but nonzero
322df8bae1dSRodney W. Grimes 	 * and timer expired, we will send what we can
323df8bae1dSRodney W. Grimes 	 * and go to transmit state.
324df8bae1dSRodney W. Grimes 	 */
3252cdbfa66SPaul Saab 	if (tp->t_flags & TF_FORCEDATA) {
326201d185bSAndre Oppermann 		if (sendwin == 0) {
327df8bae1dSRodney W. Grimes 			/*
328df8bae1dSRodney W. Grimes 			 * If we still have some data to send, then
329df8bae1dSRodney W. Grimes 			 * clear the FIN bit.  Usually this would
330df8bae1dSRodney W. Grimes 			 * happen below when it realizes that we
331df8bae1dSRodney W. Grimes 			 * aren't sending all the data.  However,
33229089b51SJulian Elischer 			 * if we have exactly 1 byte of unsent data,
333df8bae1dSRodney W. Grimes 			 * then it won't clear the FIN bit below,
334df8bae1dSRodney W. Grimes 			 * and if we are in persist state, we wind
335df8bae1dSRodney W. Grimes 			 * up sending the packet without recording
336df8bae1dSRodney W. Grimes 			 * that we sent the FIN bit.
337df8bae1dSRodney W. Grimes 			 *
338df8bae1dSRodney W. Grimes 			 * We can't just blindly clear the FIN bit,
339df8bae1dSRodney W. Grimes 			 * because if we don't have any more data
340df8bae1dSRodney W. Grimes 			 * to send then the probe will be the FIN
341df8bae1dSRodney W. Grimes 			 * itself.
342df8bae1dSRodney W. Grimes 			 */
343cfa6009eSGleb Smirnoff 			if (off < sbused(&so->so_snd))
344df8bae1dSRodney W. Grimes 				flags &= ~TH_FIN;
345201d185bSAndre Oppermann 			sendwin = 1;
346df8bae1dSRodney W. Grimes 		} else {
347b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_PERSIST, 0);
348df8bae1dSRodney W. Grimes 			tp->t_rxtshift = 0;
349df8bae1dSRodney W. Grimes 		}
350df8bae1dSRodney W. Grimes 	}
351df8bae1dSRodney W. Grimes 
35228257b5cSMatthew Dillon 	/*
35328257b5cSMatthew Dillon 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
35428257b5cSMatthew Dillon 	 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
355201d185bSAndre Oppermann 	 * a negative length.  This can also occur when TCP opens up
35628257b5cSMatthew Dillon 	 * its congestion window while receiving additional duplicate
35728257b5cSMatthew Dillon 	 * acks after fast-retransmit because TCP will reset snd_nxt
35828257b5cSMatthew Dillon 	 * to snd_max after the fast-retransmit.
35928257b5cSMatthew Dillon 	 *
36028257b5cSMatthew Dillon 	 * In the normal retransmit-FIN-only case, however, snd_nxt will
36128257b5cSMatthew Dillon 	 * be set to snd_una, the offset will be 0, and the length may
36228257b5cSMatthew Dillon 	 * wind up 0.
3636d90faf3SPaul Saab 	 *
3646d90faf3SPaul Saab 	 * If sack_rxmit is true we are retransmitting from the scoreboard
3656d90faf3SPaul Saab 	 * in which case len is already set.
36628257b5cSMatthew Dillon 	 */
367a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
368a55db2b6SPaul Saab 		if (sack_bytes_rxmt == 0)
369cfa6009eSGleb Smirnoff 			len = ((long)ulmin(sbavail(&so->so_snd), sendwin) -
370cfa6009eSGleb Smirnoff 			    off);
371a55db2b6SPaul Saab 		else {
372a55db2b6SPaul Saab 			long cwin;
373a55db2b6SPaul Saab 
374a55db2b6SPaul Saab                         /*
375a55db2b6SPaul Saab 			 * We are inside of a SACK recovery episode and are
376a55db2b6SPaul Saab 			 * sending new data, having retransmitted all the
377a55db2b6SPaul Saab 			 * data possible in the scoreboard.
378a55db2b6SPaul Saab 			 */
379cfa6009eSGleb Smirnoff 			len = ((long)ulmin(sbavail(&so->so_snd), tp->snd_wnd) -
380cfa6009eSGleb Smirnoff 			    off);
3818d03f2b5SPaul Saab 			/*
3828d03f2b5SPaul Saab 			 * Don't remove this (len > 0) check !
3838d03f2b5SPaul Saab 			 * We explicitly check for len > 0 here (although it
3848d03f2b5SPaul Saab 			 * isn't really necessary), to work around a gcc
3858d03f2b5SPaul Saab 			 * optimization issue - to force gcc to compute
3868d03f2b5SPaul Saab 			 * len above. Without this check, the computation
3878d03f2b5SPaul Saab 			 * of len is bungled by the optimizer.
3888d03f2b5SPaul Saab 			 */
3898d03f2b5SPaul Saab 			if (len > 0) {
3907d5ed1ceSPaul Saab 				cwin = tp->snd_cwnd -
3917d5ed1ceSPaul Saab 					(tp->snd_nxt - tp->sack_newdata) -
392a55db2b6SPaul Saab 					sack_bytes_rxmt;
393a55db2b6SPaul Saab 				if (cwin < 0)
394a55db2b6SPaul Saab 					cwin = 0;
395a55db2b6SPaul Saab 				len = lmin(len, cwin);
396a55db2b6SPaul Saab 			}
397a55db2b6SPaul Saab 		}
3988d03f2b5SPaul Saab 	}
399a0292f23SGarrett Wollman 
400a0292f23SGarrett Wollman 	/*
401a0292f23SGarrett Wollman 	 * Lop off SYN bit if it has already been sent.  However, if this
402a0292f23SGarrett Wollman 	 * is SYN-SENT state and if segment contains data and if we don't
403a0292f23SGarrett Wollman 	 * know that foreign host supports TAO, suppress sending segment.
404a0292f23SGarrett Wollman 	 */
405a0292f23SGarrett Wollman 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
4064b8e98d6SQing Li 		if (tp->t_state != TCPS_SYN_RECEIVED)
407a0292f23SGarrett Wollman 			flags &= ~TH_SYN;
408281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
409281a0fd4SPatrick Kelsey 		/*
410281a0fd4SPatrick Kelsey 		 * When sending additional segments following a TFO SYN|ACK,
411281a0fd4SPatrick Kelsey 		 * do not include the SYN bit.
412281a0fd4SPatrick Kelsey 		 */
413281a0fd4SPatrick Kelsey 		if ((tp->t_flags & TF_FASTOPEN) &&
414281a0fd4SPatrick Kelsey 		    (tp->t_state == TCPS_SYN_RECEIVED))
415281a0fd4SPatrick Kelsey 			flags &= ~TH_SYN;
416281a0fd4SPatrick Kelsey #endif
417a0292f23SGarrett Wollman 		off--, len++;
418a0292f23SGarrett Wollman 	}
419a0292f23SGarrett Wollman 
42081165e48SAndras Olah 	/*
421c94c54e4SAndre Oppermann 	 * Be careful not to send data and/or FIN on SYN segments.
42281165e48SAndras Olah 	 * This measure is needed to prevent interoperability problems
42381165e48SAndras Olah 	 * with not fully conformant TCP implementations.
42481165e48SAndras Olah 	 */
425c94c54e4SAndre Oppermann 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
42681165e48SAndras Olah 		len = 0;
42781165e48SAndras Olah 		flags &= ~TH_FIN;
42881165e48SAndras Olah 	}
42981165e48SAndras Olah 
430281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
431281a0fd4SPatrick Kelsey 	/*
432281a0fd4SPatrick Kelsey 	 * When retransmitting SYN|ACK on a passively-created TFO socket,
433281a0fd4SPatrick Kelsey 	 * don't include data, as the presence of data may have caused the
434281a0fd4SPatrick Kelsey 	 * original SYN|ACK to have been dropped by a middlebox.
435281a0fd4SPatrick Kelsey 	 */
436281a0fd4SPatrick Kelsey 	if ((tp->t_flags & TF_FASTOPEN) &&
437281a0fd4SPatrick Kelsey 	    (((tp->t_state == TCPS_SYN_RECEIVED) && (tp->t_rxtshift > 0)) ||
438281a0fd4SPatrick Kelsey 	     (flags & TH_RST)))
439281a0fd4SPatrick Kelsey 		len = 0;
440281a0fd4SPatrick Kelsey #endif
44147a8e865SXin LI 	if (len <= 0) {
442df8bae1dSRodney W. Grimes 		/*
443df8bae1dSRodney W. Grimes 		 * If FIN has been sent but not acked,
444df8bae1dSRodney W. Grimes 		 * but we haven't been called to retransmit,
44528257b5cSMatthew Dillon 		 * len will be < 0.  Otherwise, window shrank
446df8bae1dSRodney W. Grimes 		 * after we sent into it.  If window shrank to 0,
4472d8266afSDavid Greenman 		 * cancel pending retransmit, pull snd_nxt back
4482d8266afSDavid Greenman 		 * to (closed) window, and set the persist timer
4492d8266afSDavid Greenman 		 * if it isn't already going.  If the window didn't
4502d8266afSDavid Greenman 		 * close completely, just wait for an ACK.
45147a8e865SXin LI 		 *
45247a8e865SXin LI 		 * We also do a general check here to ensure that
45347a8e865SXin LI 		 * we will set the persist timer when we have data
45447a8e865SXin LI 		 * to send, but a 0-byte window. This makes sure
45547a8e865SXin LI 		 * the persist timer is set even if the packet
45647a8e865SXin LI 		 * hits one of the "goto send" lines below.
457df8bae1dSRodney W. Grimes 		 */
458df8bae1dSRodney W. Grimes 		len = 0;
45947a8e865SXin LI 		if ((sendwin == 0) && (TCPS_HAVEESTABLISHED(tp->t_state)) &&
46047a8e865SXin LI 			(off < (int) sbavail(&so->so_snd))) {
461b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
4622d8266afSDavid Greenman 			tp->t_rxtshift = 0;
463df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
464b8152ba7SAndre Oppermann 			if (!tcp_timer_active(tp, TT_PERSIST))
4652d8266afSDavid Greenman 				tcp_setpersist(tp);
466df8bae1dSRodney W. Grimes 		}
467df8bae1dSRodney W. Grimes 	}
46828257b5cSMatthew Dillon 
4696741ecf5SAndre Oppermann 	/* len will be >= 0 after this point. */
470c4982faeSBjoern A. Zeeb 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
4716741ecf5SAndre Oppermann 
47228257b5cSMatthew Dillon 	/*
4736741ecf5SAndre Oppermann 	 * Automatic sizing of send socket buffer.  Often the send buffer
4746741ecf5SAndre Oppermann 	 * size is not optimally adjusted to the actual network conditions
4756741ecf5SAndre Oppermann 	 * at hand (delay bandwidth product).  Setting the buffer size too
4766741ecf5SAndre Oppermann 	 * small limits throughput on links with high bandwidth and high
4776741ecf5SAndre Oppermann 	 * delay (eg. trans-continental/oceanic links).  Setting the
4786741ecf5SAndre Oppermann 	 * buffer size too big consumes too much real kernel memory,
4796741ecf5SAndre Oppermann 	 * especially with many connections on busy servers.
4806741ecf5SAndre Oppermann 	 *
4816741ecf5SAndre Oppermann 	 * The criteria to step up the send buffer one notch are:
4826741ecf5SAndre Oppermann 	 *  1. receive window of remote host is larger than send buffer
4836741ecf5SAndre Oppermann 	 *     (with a fudge factor of 5/4th);
4846741ecf5SAndre Oppermann 	 *  2. send buffer is filled to 7/8th with data (so we actually
4856741ecf5SAndre Oppermann 	 *     have data to make use of it);
4866741ecf5SAndre Oppermann 	 *  3. send buffer fill has not hit maximal automatic size;
4876741ecf5SAndre Oppermann 	 *  4. our send window (slow start and cogestion controlled) is
4886741ecf5SAndre Oppermann 	 *     larger than sent but unacknowledged data in send buffer.
4896741ecf5SAndre Oppermann 	 *
4906741ecf5SAndre Oppermann 	 * The remote host receive window scaling factor may limit the
4916741ecf5SAndre Oppermann 	 * growing of the send buffer before it reaches its allowed
4926741ecf5SAndre Oppermann 	 * maximum.
4936741ecf5SAndre Oppermann 	 *
4946741ecf5SAndre Oppermann 	 * It scales directly with slow start or congestion window
4956741ecf5SAndre Oppermann 	 * and does at most one step per received ACK.  This fast
4966741ecf5SAndre Oppermann 	 * scaling has the drawback of growing the send buffer beyond
4976741ecf5SAndre Oppermann 	 * what is strictly necessary to make full use of a given
4986741ecf5SAndre Oppermann 	 * delay*bandwith product.  However testing has shown this not
4996741ecf5SAndre Oppermann 	 * to be much of an problem.  At worst we are trading wasting
5006741ecf5SAndre Oppermann 	 * of available bandwith (the non-use of it) for wasting some
5016741ecf5SAndre Oppermann 	 * socket buffer memory.
5026741ecf5SAndre Oppermann 	 *
5036741ecf5SAndre Oppermann 	 * TODO: Shrink send buffer during idle periods together
5046741ecf5SAndre Oppermann 	 * with congestion window.  Requires another timer.  Has to
5056741ecf5SAndre Oppermann 	 * wait for upcoming tcp timer rewrite.
506cfa6009eSGleb Smirnoff 	 *
507cfa6009eSGleb Smirnoff 	 * XXXGL: should there be used sbused() or sbavail()?
5086741ecf5SAndre Oppermann 	 */
509603724d3SBjoern A. Zeeb 	if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
5106741ecf5SAndre Oppermann 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
511cfa6009eSGleb Smirnoff 		    sbused(&so->so_snd) >= (so->so_snd.sb_hiwat / 8 * 7) &&
512cfa6009eSGleb Smirnoff 		    sbused(&so->so_snd) < V_tcp_autosndbuf_max &&
513cfa6009eSGleb Smirnoff 		    sendwin >= (sbused(&so->so_snd) -
514cfa6009eSGleb Smirnoff 		    (tp->snd_nxt - tp->snd_una))) {
5156741ecf5SAndre Oppermann 			if (!sbreserve_locked(&so->so_snd,
516603724d3SBjoern A. Zeeb 			    min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc,
517603724d3SBjoern A. Zeeb 			     V_tcp_autosndbuf_max), so, curthread))
5186741ecf5SAndre Oppermann 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
5196741ecf5SAndre Oppermann 		}
5206741ecf5SAndre Oppermann 	}
5216741ecf5SAndre Oppermann 
5226741ecf5SAndre Oppermann 	/*
523ed420311SAndre Oppermann 	 * Decide if we can use TCP Segmentation Offloading (if supported by
524ed420311SAndre Oppermann 	 * hardware).
525b3c0f300SAndre Oppermann 	 *
526b3c0f300SAndre Oppermann 	 * TSO may only be used if we are in a pure bulk sending state.  The
527b3c0f300SAndre Oppermann 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and
528b3c0f300SAndre Oppermann 	 * IP options prevent using TSO.  With TSO the TCP header is the same
529b3c0f300SAndre Oppermann 	 * (except for the sequence number) for all generated packets.  This
530b3c0f300SAndre Oppermann 	 * makes it impossible to transmit any options which vary per generated
531b3c0f300SAndre Oppermann 	 * segment or packet.
53228257b5cSMatthew Dillon 	 */
5339ad0173dSBjoern A. Zeeb #ifdef IPSEC
5349ad0173dSBjoern A. Zeeb 	/*
5359ad0173dSBjoern A. Zeeb 	 * Pre-calculate here as we save another lookup into the darknesses
5369ad0173dSBjoern A. Zeeb 	 * of IPsec that way and can actually decide if TSO is ok.
5379ad0173dSBjoern A. Zeeb 	 */
5389ad0173dSBjoern A. Zeeb 	ipsec_optlen = ipsec_hdrsiz_tcp(tp);
5399ad0173dSBjoern A. Zeeb #endif
540ed420311SAndre Oppermann 	if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg &&
541b3c0f300SAndre Oppermann 	    ((tp->t_flags & TF_SIGNATURE) == 0) &&
542b3c0f300SAndre Oppermann 	    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
5439ad0173dSBjoern A. Zeeb #ifdef IPSEC
544ed420311SAndre Oppermann 	    ipsec_optlen == 0 &&
5459ad0173dSBjoern A. Zeeb #endif
546ed420311SAndre Oppermann 	    tp->t_inpcb->inp_options == NULL &&
547ed420311SAndre Oppermann 	    tp->t_inpcb->in6p_options == NULL)
548b3c0f300SAndre Oppermann 		tso = 1;
549153e5b57SAndre Oppermann 
5505d3b1b75SJayanth Vijayaraghavan 	if (sack_rxmit) {
551cfa6009eSGleb Smirnoff 		if (SEQ_LT(p->rxmit + len, tp->snd_una + sbused(&so->so_snd)))
552df8bae1dSRodney W. Grimes 			flags &= ~TH_FIN;
5535d3b1b75SJayanth Vijayaraghavan 	} else {
554cfa6009eSGleb Smirnoff 		if (SEQ_LT(tp->snd_nxt + len, tp->snd_una +
555cfa6009eSGleb Smirnoff 		    sbused(&so->so_snd)))
5565d3b1b75SJayanth Vijayaraghavan 			flags &= ~TH_FIN;
5575d3b1b75SJayanth Vijayaraghavan 	}
558df8bae1dSRodney W. Grimes 
559201d185bSAndre Oppermann 	recwin = sbspace(&so->so_rcv);
560df8bae1dSRodney W. Grimes 
561df8bae1dSRodney W. Grimes 	/*
562262c1c1aSMatthew Dillon 	 * Sender silly window avoidance.   We transmit under the following
563262c1c1aSMatthew Dillon 	 * conditions when len is non-zero:
564262c1c1aSMatthew Dillon 	 *
565b3c0f300SAndre Oppermann 	 *	- We have a full segment (or more with TSO)
566262c1c1aSMatthew Dillon 	 *	- This is the last buffer in a write()/send() and we are
567262c1c1aSMatthew Dillon 	 *	  either idle or running NODELAY
568262c1c1aSMatthew Dillon 	 *	- we've timed out (e.g. persist timer)
569262c1c1aSMatthew Dillon 	 *	- we have more then 1/2 the maximum send window's worth of
570262c1c1aSMatthew Dillon 	 *	  data (receiver may be limited the window size)
571262c1c1aSMatthew Dillon 	 *	- we need to retransmit
572df8bae1dSRodney W. Grimes 	 */
573df8bae1dSRodney W. Grimes 	if (len) {
574b3c0f300SAndre Oppermann 		if (len >= tp->t_maxseg)
575df8bae1dSRodney W. Grimes 			goto send;
576262c1c1aSMatthew Dillon 		/*
577262c1c1aSMatthew Dillon 		 * NOTE! on localhost connections an 'ack' from the remote
578262c1c1aSMatthew Dillon 		 * end may occur synchronously with the output and cause
579262c1c1aSMatthew Dillon 		 * us to flush a buffer queued with moretocome.  XXX
580262c1c1aSMatthew Dillon 		 *
581262c1c1aSMatthew Dillon 		 * note: the len + off check is almost certainly unnecessary.
582262c1c1aSMatthew Dillon 		 */
583262c1c1aSMatthew Dillon 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
584262c1c1aSMatthew Dillon 		    (idle || (tp->t_flags & TF_NODELAY)) &&
585cfa6009eSGleb Smirnoff 		    len + off >= sbavail(&so->so_snd) &&
586262c1c1aSMatthew Dillon 		    (tp->t_flags & TF_NOPUSH) == 0) {
587df8bae1dSRodney W. Grimes 			goto send;
588262c1c1aSMatthew Dillon 		}
5892cdbfa66SPaul Saab 		if (tp->t_flags & TF_FORCEDATA)		/* typ. timeout case */
590df8bae1dSRodney W. Grimes 			goto send;
591a0292f23SGarrett Wollman 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
592df8bae1dSRodney W. Grimes 			goto send;
593262c1c1aSMatthew Dillon 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))	/* retransmit case */
594df8bae1dSRodney W. Grimes 			goto send;
5956d90faf3SPaul Saab 		if (sack_rxmit)
5966d90faf3SPaul Saab 			goto send;
597df8bae1dSRodney W. Grimes 	}
598df8bae1dSRodney W. Grimes 
599df8bae1dSRodney W. Grimes 	/*
600f62563d3SAndre Oppermann 	 * Sending of standalone window updates.
601f62563d3SAndre Oppermann 	 *
60278f59b4bSAndre Oppermann 	 * Window updates are important when we close our window due to a
60378f59b4bSAndre Oppermann 	 * full socket buffer and are opening it again after the application
604f62563d3SAndre Oppermann 	 * reads data from it.  Once the window has opened again and the
605f62563d3SAndre Oppermann 	 * remote end starts to send again the ACK clock takes over and
606f62563d3SAndre Oppermann 	 * provides the most current window information.
607f62563d3SAndre Oppermann 	 *
60878f59b4bSAndre Oppermann 	 * We must avoid the silly window syndrome whereas every read
609f62563d3SAndre Oppermann 	 * from the receive buffer, no matter how small, causes a window
610f62563d3SAndre Oppermann 	 * update to be sent.  We also should avoid sending a flurry of
611f62563d3SAndre Oppermann 	 * window updates when the socket buffer had queued a lot of data
612f62563d3SAndre Oppermann 	 * and the application is doing small reads.
613f62563d3SAndre Oppermann 	 *
614f62563d3SAndre Oppermann 	 * Prevent a flurry of pointless window updates by only sending
615f62563d3SAndre Oppermann 	 * an update when we can increase the advertized window by more
616f62563d3SAndre Oppermann 	 * than 1/4th of the socket buffer capacity.  When the buffer is
617f62563d3SAndre Oppermann 	 * getting full or is very small be more aggressive and send an
618f62563d3SAndre Oppermann 	 * update whenever we can increase by two mss sized segments.
619f62563d3SAndre Oppermann 	 * In all other situations the ACK's to new incoming data will
620f62563d3SAndre Oppermann 	 * carry further window increases.
6214249614cSAndre Oppermann 	 *
6224249614cSAndre Oppermann 	 * Don't send an independent window update if a delayed
6234249614cSAndre Oppermann 	 * ACK is pending (it will get piggy-backed on it) or the
6244249614cSAndre Oppermann 	 * remote side already has done a half-close and won't send
625f62563d3SAndre Oppermann 	 * more data.  Skip this if the connection is in T/TCP
626f62563d3SAndre Oppermann 	 * half-open state.
627df8bae1dSRodney W. Grimes 	 */
628b7de7d87SAndre Oppermann 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
6294249614cSAndre Oppermann 	    !(tp->t_flags & TF_DELACK) &&
630b7de7d87SAndre Oppermann 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
631df8bae1dSRodney W. Grimes 		/*
632f62563d3SAndre Oppermann 		 * "adv" is the amount we could increase the window,
633df8bae1dSRodney W. Grimes 		 * taking into account that we are limited by
634df8bae1dSRodney W. Grimes 		 * TCP_MAXWIN << tp->rcv_scale.
635df8bae1dSRodney W. Grimes 		 */
636f701e30dSJohn Baldwin 		long adv;
637f701e30dSJohn Baldwin 		int oldwin;
638f701e30dSJohn Baldwin 
639f701e30dSJohn Baldwin 		adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale);
640f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
641f701e30dSJohn Baldwin 			oldwin = (tp->rcv_adv - tp->rcv_nxt);
642f701e30dSJohn Baldwin 			adv -= oldwin;
643f701e30dSJohn Baldwin 		} else
644f701e30dSJohn Baldwin 			oldwin = 0;
645df8bae1dSRodney W. Grimes 
646da84b2e6SJohn Baldwin 		/*
647da84b2e6SJohn Baldwin 		 * If the new window size ends up being the same as the old
648da84b2e6SJohn Baldwin 		 * size when it is scaled, then don't force a window update.
649da84b2e6SJohn Baldwin 		 */
650f701e30dSJohn Baldwin 		if (oldwin >> tp->rcv_scale == (adv + oldwin) >> tp->rcv_scale)
651da84b2e6SJohn Baldwin 			goto dontupdate;
652f62563d3SAndre Oppermann 
653f62563d3SAndre Oppermann 		if (adv >= (long)(2 * tp->t_maxseg) &&
654f62563d3SAndre Oppermann 		    (adv >= (long)(so->so_rcv.sb_hiwat / 4) ||
655f62563d3SAndre Oppermann 		     recwin <= (long)(so->so_rcv.sb_hiwat / 8) ||
656f62563d3SAndre Oppermann 		     so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg))
657df8bae1dSRodney W. Grimes 			goto send;
658df8bae1dSRodney W. Grimes 	}
659da84b2e6SJohn Baldwin dontupdate:
660df8bae1dSRodney W. Grimes 
661df8bae1dSRodney W. Grimes 	/*
66228257b5cSMatthew Dillon 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
66328257b5cSMatthew Dillon 	 * is also a catch-all for the retransmit timer timeout case.
664df8bae1dSRodney W. Grimes 	 */
665df8bae1dSRodney W. Grimes 	if (tp->t_flags & TF_ACKNOW)
666df8bae1dSRodney W. Grimes 		goto send;
667a0292f23SGarrett Wollman 	if ((flags & TH_RST) ||
668a0292f23SGarrett Wollman 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
669df8bae1dSRodney W. Grimes 		goto send;
670df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_una))
671df8bae1dSRodney W. Grimes 		goto send;
672df8bae1dSRodney W. Grimes 	/*
673df8bae1dSRodney W. Grimes 	 * If our state indicates that FIN should be sent
67428257b5cSMatthew Dillon 	 * and we have not yet done so, then we need to send.
675df8bae1dSRodney W. Grimes 	 */
676df8bae1dSRodney W. Grimes 	if (flags & TH_FIN &&
677df8bae1dSRodney W. Grimes 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
678df8bae1dSRodney W. Grimes 		goto send;
6796d90faf3SPaul Saab 	/*
6806d90faf3SPaul Saab 	 * In SACK, it is possible for tcp_output to fail to send a segment
6816d90faf3SPaul Saab 	 * after the retransmission timer has been turned off.  Make sure
6826d90faf3SPaul Saab 	 * that the retransmission timer is set.
6836d90faf3SPaul Saab 	 */
6843529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
6853529149eSAndre Oppermann 	    SEQ_GT(tp->snd_max, tp->snd_una) &&
686b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_REXMT) &&
687b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
688b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
689cf2942b6SRobert Watson 		goto just_return;
6906d90faf3SPaul Saab 	}
691df8bae1dSRodney W. Grimes 	/*
692df8bae1dSRodney W. Grimes 	 * TCP window updates are not reliable, rather a polling protocol
693df8bae1dSRodney W. Grimes 	 * using ``persist'' packets is used to insure receipt of window
694df8bae1dSRodney W. Grimes 	 * updates.  The three ``states'' for the output side are:
695df8bae1dSRodney W. Grimes 	 *	idle			not doing retransmits or persists
696df8bae1dSRodney W. Grimes 	 *	persisting		to move a small or zero window
697df8bae1dSRodney W. Grimes 	 *	(re)transmitting	and thereby not persisting
698df8bae1dSRodney W. Grimes 	 *
699b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_PERSIST)
7009b8b58e0SJonathan Lemon 	 *	is true when we are in persist state.
7012cdbfa66SPaul Saab 	 * (tp->t_flags & TF_FORCEDATA)
702df8bae1dSRodney W. Grimes 	 *	is set when we are called to send a persist packet.
703b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_REXMT)
704df8bae1dSRodney W. Grimes 	 *	is set when we are retransmitting
705df8bae1dSRodney W. Grimes 	 * The output side is idle when both timers are zero.
706df8bae1dSRodney W. Grimes 	 *
707df8bae1dSRodney W. Grimes 	 * If send window is too small, there is data to transmit, and no
708df8bae1dSRodney W. Grimes 	 * retransmit or persist is pending, then go to persist state.
709df8bae1dSRodney W. Grimes 	 * If nothing happens soon, send when timer expires:
710df8bae1dSRodney W. Grimes 	 * if window is nonzero, transmit what we can,
711df8bae1dSRodney W. Grimes 	 * otherwise force out a byte.
712df8bae1dSRodney W. Grimes 	 */
713cfa6009eSGleb Smirnoff 	if (sbavail(&so->so_snd) && !tcp_timer_active(tp, TT_REXMT) &&
714b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
715df8bae1dSRodney W. Grimes 		tp->t_rxtshift = 0;
716df8bae1dSRodney W. Grimes 		tcp_setpersist(tp);
717df8bae1dSRodney W. Grimes 	}
718df8bae1dSRodney W. Grimes 
719df8bae1dSRodney W. Grimes 	/*
720df8bae1dSRodney W. Grimes 	 * No reason to send a segment, just return.
721df8bae1dSRodney W. Grimes 	 */
722cf2942b6SRobert Watson just_return:
723cf2942b6SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
724df8bae1dSRodney W. Grimes 	return (0);
725df8bae1dSRodney W. Grimes 
726df8bae1dSRodney W. Grimes send:
727cf2942b6SRobert Watson 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
728f6f6703fSSean Bruno 	if (len > 0) {
729f6f6703fSSean Bruno 		if (len >= tp->t_maxseg)
730f6f6703fSSean Bruno 			tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
731f6f6703fSSean Bruno 		else
732f6f6703fSSean Bruno 			tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
733f6f6703fSSean Bruno 	}
734df8bae1dSRodney W. Grimes 	/*
735df8bae1dSRodney W. Grimes 	 * Before ESTABLISHED, force sending of initial options
736df8bae1dSRodney W. Grimes 	 * unless TCP set not to do any options.
737df8bae1dSRodney W. Grimes 	 * NOTE: we assume that the IP/TCP header plus TCP options
738df8bae1dSRodney W. Grimes 	 * always fit in a single mbuf, leaving room for a maximum
739df8bae1dSRodney W. Grimes 	 * link header, i.e.
74033841545SHajimu UMEMOTO 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
741df8bae1dSRodney W. Grimes 	 */
742df8bae1dSRodney W. Grimes 	optlen = 0;
743fb59c426SYoshinobu Inoue #ifdef INET6
744fb59c426SYoshinobu Inoue 	if (isipv6)
745fb59c426SYoshinobu Inoue 		hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
746fb59c426SYoshinobu Inoue 	else
747fb59c426SYoshinobu Inoue #endif
748df8bae1dSRodney W. Grimes 		hdrlen = sizeof (struct tcpiphdr);
74902a1a643SAndre Oppermann 
75002a1a643SAndre Oppermann 	/*
75102a1a643SAndre Oppermann 	 * Compute options for segment.
75202a1a643SAndre Oppermann 	 * We only have to care about SYN and established connection
75302a1a643SAndre Oppermann 	 * segments.  Options for SYN-ACK segments are handled in TCP
75402a1a643SAndre Oppermann 	 * syncache.
75502a1a643SAndre Oppermann 	 */
75602a1a643SAndre Oppermann 	to.to_flags = 0;
757f73d9fd2SGleb Smirnoff 	if ((tp->t_flags & TF_NOOPT) == 0) {
75802a1a643SAndre Oppermann 		/* Maximum segment size. */
759df8bae1dSRodney W. Grimes 		if (flags & TH_SYN) {
760df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->iss;
76102a1a643SAndre Oppermann 			to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc);
76202a1a643SAndre Oppermann 			to.to_flags |= TOF_MSS;
763281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
764281a0fd4SPatrick Kelsey 			/*
765281a0fd4SPatrick Kelsey 			 * Only include the TFO option on the first
766281a0fd4SPatrick Kelsey 			 * transmission of the SYN|ACK on a
767281a0fd4SPatrick Kelsey 			 * passively-created TFO socket, as the presence of
768281a0fd4SPatrick Kelsey 			 * the TFO option may have caused the original
769281a0fd4SPatrick Kelsey 			 * SYN|ACK to have been dropped by a middlebox.
770281a0fd4SPatrick Kelsey 			 */
771281a0fd4SPatrick Kelsey 			if ((tp->t_flags & TF_FASTOPEN) &&
772281a0fd4SPatrick Kelsey 			    (tp->t_state == TCPS_SYN_RECEIVED) &&
773281a0fd4SPatrick Kelsey 			    (tp->t_rxtshift == 0)) {
774281a0fd4SPatrick Kelsey 				to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
775281a0fd4SPatrick Kelsey 				to.to_tfo_cookie = (u_char *)&tp->t_tfo_cookie;
776281a0fd4SPatrick Kelsey 				to.to_flags |= TOF_FASTOPEN;
777281a0fd4SPatrick Kelsey 			}
778281a0fd4SPatrick Kelsey #endif
779df8bae1dSRodney W. Grimes 		}
78002a1a643SAndre Oppermann 		/* Window scaling. */
78102a1a643SAndre Oppermann 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
78202a1a643SAndre Oppermann 			to.to_wscale = tp->request_r_scale;
78302a1a643SAndre Oppermann 			to.to_flags |= TOF_SCALE;
784df8bae1dSRodney W. Grimes 		}
78502a1a643SAndre Oppermann 		/* Timestamps. */
78602a1a643SAndre Oppermann 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
78702a1a643SAndre Oppermann 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
788d8951c8aSBjoern A. Zeeb 			to.to_tsval = tcp_ts_getticks() + tp->ts_offset;
78902a1a643SAndre Oppermann 			to.to_tsecr = tp->ts_recent;
79002a1a643SAndre Oppermann 			to.to_flags |= TOF_TS;
7916741ecf5SAndre Oppermann 			/* Set receive buffer autosizing timestamp. */
79202a1a643SAndre Oppermann 			if (tp->rfbuf_ts == 0 &&
79302a1a643SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE))
794d8951c8aSBjoern A. Zeeb 				tp->rfbuf_ts = tcp_ts_getticks();
7951cfd4b53SBruce M Simpson 		}
79602a1a643SAndre Oppermann 		/* Selective ACK's. */
7973529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
79802a1a643SAndre Oppermann 			if (flags & TH_SYN)
79902a1a643SAndre Oppermann 				to.to_flags |= TOF_SACKPERM;
80002a1a643SAndre Oppermann 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
80102a1a643SAndre Oppermann 			    (tp->t_flags & TF_SACK_PERMIT) &&
80202a1a643SAndre Oppermann 			    tp->rcv_numsacks > 0) {
80302a1a643SAndre Oppermann 				to.to_flags |= TOF_SACK;
80402a1a643SAndre Oppermann 				to.to_nsacks = tp->rcv_numsacks;
80502a1a643SAndre Oppermann 				to.to_sacks = (u_char *)tp->sackblks;
80602a1a643SAndre Oppermann 			}
80702a1a643SAndre Oppermann 		}
80802a1a643SAndre Oppermann #ifdef TCP_SIGNATURE
80902a1a643SAndre Oppermann 		/* TCP-MD5 (RFC2385). */
81002a1a643SAndre Oppermann 		if (tp->t_flags & TF_SIGNATURE)
81102a1a643SAndre Oppermann 			to.to_flags |= TOF_SIGNATURE;
8121cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
8131cfd4b53SBruce M Simpson 
81402a1a643SAndre Oppermann 		/* Processing the options. */
8154a411b9fSBjoern A. Zeeb 		hdrlen += optlen = tcp_addoptions(&to, opt);
816be3f3b5eSPaul Saab 	}
817be3f3b5eSPaul Saab 
818fb59c426SYoshinobu Inoue #ifdef INET6
819fb59c426SYoshinobu Inoue 	if (isipv6)
820fb59c426SYoshinobu Inoue 		ipoptlen = ip6_optlen(tp->t_inpcb);
821fb59c426SYoshinobu Inoue 	else
822fb59c426SYoshinobu Inoue #endif
823f10e85d7SLuigi Rizzo 	if (tp->t_inpcb->inp_options)
824a04884fcSBill Fenner 		ipoptlen = tp->t_inpcb->inp_options->m_len -
825a04884fcSBill Fenner 				offsetof(struct ipoption, ipopt_list);
826f10e85d7SLuigi Rizzo 	else
827a04884fcSBill Fenner 		ipoptlen = 0;
828b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
8299ad0173dSBjoern A. Zeeb 	ipoptlen += ipsec_optlen;
830fb59c426SYoshinobu Inoue #endif
831a04884fcSBill Fenner 
832df8bae1dSRodney W. Grimes 	/*
833df8bae1dSRodney W. Grimes 	 * Adjust data length if insertion of options will
8340c39d38dSGleb Smirnoff 	 * bump the packet length beyond the t_maxseg length.
835a0292f23SGarrett Wollman 	 * Clear the FIN bit because we cut off the tail of
836a0292f23SGarrett Wollman 	 * the segment.
837df8bae1dSRodney W. Grimes 	 */
8380c39d38dSGleb Smirnoff 	if (len + optlen + ipoptlen > tp->t_maxseg) {
839297a37f3SDavid Greenman 		flags &= ~TH_FIN;
840ed420311SAndre Oppermann 
841b3c0f300SAndre Oppermann 		if (tso) {
8429fd573c3SHans Petter Selasky 			u_int if_hw_tsomax;
8439fd573c3SHans Petter Selasky 			u_int if_hw_tsomaxsegcount;
8449fd573c3SHans Petter Selasky 			u_int if_hw_tsomaxsegsize;
8459fd573c3SHans Petter Selasky 			struct mbuf *mb;
8469fd573c3SHans Petter Selasky 			u_int moff;
8479fd573c3SHans Petter Selasky 			int max_len;
8489fd573c3SHans Petter Selasky 
8499fd573c3SHans Petter Selasky 			/* extract TSO information */
8509fd573c3SHans Petter Selasky 			if_hw_tsomax = tp->t_tsomax;
8519fd573c3SHans Petter Selasky 			if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
8529fd573c3SHans Petter Selasky 			if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
8539fd573c3SHans Petter Selasky 
8549fd573c3SHans Petter Selasky 			/*
8559fd573c3SHans Petter Selasky 			 * Limit a TSO burst to prevent it from
8569fd573c3SHans Petter Selasky 			 * overflowing or exceeding the maximum length
8579fd573c3SHans Petter Selasky 			 * allowed by the network interface:
8589fd573c3SHans Petter Selasky 			 */
859ed420311SAndre Oppermann 			KASSERT(ipoptlen == 0,
860ed420311SAndre Oppermann 			    ("%s: TSO can't do IP options", __func__));
861ed420311SAndre Oppermann 
862ed420311SAndre Oppermann 			/*
8639fd573c3SHans Petter Selasky 			 * Check if we should limit by maximum payload
8649fd573c3SHans Petter Selasky 			 * length:
865ed420311SAndre Oppermann 			 */
8669fd573c3SHans Petter Selasky 			if (if_hw_tsomax != 0) {
8679fd573c3SHans Petter Selasky 				/* compute maximum TSO length */
868d76d4012SHans Petter Selasky 				max_len = (if_hw_tsomax - hdrlen -
869d76d4012SHans Petter Selasky 				    max_linkhdr);
8709fd573c3SHans Petter Selasky 				if (max_len <= 0) {
8719fd573c3SHans Petter Selasky 					len = 0;
8723c7c188cSHans Petter Selasky 				} else if (len > max_len) {
873b3c0f300SAndre Oppermann 					sendalot = 1;
8743c7c188cSHans Petter Selasky 					len = max_len;
8759fd573c3SHans Petter Selasky 				}
8769fd573c3SHans Petter Selasky 			}
8779fd573c3SHans Petter Selasky 
8789fd573c3SHans Petter Selasky 			/*
8799fd573c3SHans Petter Selasky 			 * Check if we should limit by maximum segment
8809fd573c3SHans Petter Selasky 			 * size and count:
8819fd573c3SHans Petter Selasky 			 */
882b228e6bfSHans Petter Selasky 			if (if_hw_tsomaxsegcount != 0 &&
883b228e6bfSHans Petter Selasky 			    if_hw_tsomaxsegsize != 0) {
884d76d4012SHans Petter Selasky 				/*
885d76d4012SHans Petter Selasky 				 * Subtract one segment for the LINK
886d76d4012SHans Petter Selasky 				 * and TCP/IP headers mbuf that will
887d76d4012SHans Petter Selasky 				 * be prepended to this mbuf chain
888d76d4012SHans Petter Selasky 				 * after the code in this section
889d76d4012SHans Petter Selasky 				 * limits the number of mbufs in the
890d76d4012SHans Petter Selasky 				 * chain to if_hw_tsomaxsegcount.
891d76d4012SHans Petter Selasky 				 */
892d76d4012SHans Petter Selasky 				if_hw_tsomaxsegcount -= 1;
8939fd573c3SHans Petter Selasky 				max_len = 0;
8949fd573c3SHans Petter Selasky 				mb = sbsndmbuf(&so->so_snd, off, &moff);
8959fd573c3SHans Petter Selasky 
8963c7c188cSHans Petter Selasky 				while (mb != NULL && max_len < len) {
897b228e6bfSHans Petter Selasky 					u_int mlen;
898b228e6bfSHans Petter Selasky 					u_int frags;
8999fd573c3SHans Petter Selasky 
9009fd573c3SHans Petter Selasky 					/*
9019fd573c3SHans Petter Selasky 					 * Get length of mbuf fragment
902b228e6bfSHans Petter Selasky 					 * and how many hardware frags,
903b228e6bfSHans Petter Selasky 					 * rounded up, it would use:
9049fd573c3SHans Petter Selasky 					 */
905b228e6bfSHans Petter Selasky 					mlen = (mb->m_len - moff);
906b228e6bfSHans Petter Selasky 					frags = howmany(mlen,
907b228e6bfSHans Petter Selasky 					    if_hw_tsomaxsegsize);
9089fd573c3SHans Petter Selasky 
9099fd573c3SHans Petter Selasky 					/* Handle special case: Zero Length Mbuf */
910b228e6bfSHans Petter Selasky 					if (frags == 0)
911b228e6bfSHans Petter Selasky 						frags = 1;
9129fd573c3SHans Petter Selasky 
9139fd573c3SHans Petter Selasky 					/*
9149fd573c3SHans Petter Selasky 					 * Check if the fragment limit
915b228e6bfSHans Petter Selasky 					 * will be reached or exceeded:
9169fd573c3SHans Petter Selasky 					 */
917b228e6bfSHans Petter Selasky 					if (frags >= if_hw_tsomaxsegcount) {
918b228e6bfSHans Petter Selasky 						max_len += min(mlen,
9199fd573c3SHans Petter Selasky 						    if_hw_tsomaxsegcount *
9209fd573c3SHans Petter Selasky 						    if_hw_tsomaxsegsize);
9219fd573c3SHans Petter Selasky 						break;
9229fd573c3SHans Petter Selasky 					}
923b228e6bfSHans Petter Selasky 					max_len += mlen;
924b228e6bfSHans Petter Selasky 					if_hw_tsomaxsegcount -= frags;
9259fd573c3SHans Petter Selasky 					moff = 0;
9269fd573c3SHans Petter Selasky 					mb = mb->m_next;
9279fd573c3SHans Petter Selasky 				}
9289fd573c3SHans Petter Selasky 				if (max_len <= 0) {
9299fd573c3SHans Petter Selasky 					len = 0;
9303c7c188cSHans Petter Selasky 				} else if (len > max_len) {
9319fd573c3SHans Petter Selasky 					sendalot = 1;
9323c7c188cSHans Petter Selasky 					len = max_len;
9339fd573c3SHans Petter Selasky 				}
934ed420311SAndre Oppermann 			}
935ed420311SAndre Oppermann 
936ed420311SAndre Oppermann 			/*
937ed420311SAndre Oppermann 			 * Prevent the last segment from being
9389fd573c3SHans Petter Selasky 			 * fractional unless the send sockbuf can be
9399fd573c3SHans Petter Selasky 			 * emptied:
940ed420311SAndre Oppermann 			 */
9410c39d38dSGleb Smirnoff 			max_len = (tp->t_maxseg - optlen);
942cfa6009eSGleb Smirnoff 			if ((off + len) < sbavail(&so->so_snd)) {
9433c7c188cSHans Petter Selasky 				moff = len % max_len;
9449fd573c3SHans Petter Selasky 				if (moff != 0) {
9459fd573c3SHans Petter Selasky 					len -= moff;
946b3c0f300SAndre Oppermann 					sendalot = 1;
947ed420311SAndre Oppermann 				}
9489fd573c3SHans Petter Selasky 			}
9499fd573c3SHans Petter Selasky 
9509fd573c3SHans Petter Selasky 			/*
9519fd573c3SHans Petter Selasky 			 * In case there are too many small fragments
9529fd573c3SHans Petter Selasky 			 * don't use TSO:
9539fd573c3SHans Petter Selasky 			 */
9543c7c188cSHans Petter Selasky 			if (len <= max_len) {
9553c7c188cSHans Petter Selasky 				len = max_len;
9569fd573c3SHans Petter Selasky 				sendalot = 1;
9579fd573c3SHans Petter Selasky 				tso = 0;
9589fd573c3SHans Petter Selasky 			}
959ed420311SAndre Oppermann 
960ed420311SAndre Oppermann 			/*
961ed420311SAndre Oppermann 			 * Send the FIN in a separate segment
962ed420311SAndre Oppermann 			 * after the bulk sending is done.
963ed420311SAndre Oppermann 			 * We don't trust the TSO implementations
964ed420311SAndre Oppermann 			 * to clear the FIN flag on all but the
965ed420311SAndre Oppermann 			 * last segment.
966ed420311SAndre Oppermann 			 */
967ed420311SAndre Oppermann 			if (tp->t_flags & TF_NEEDFIN)
968ed420311SAndre Oppermann 				sendalot = 1;
969ed420311SAndre Oppermann 
970b3c0f300SAndre Oppermann 		} else {
9710c39d38dSGleb Smirnoff 			len = tp->t_maxseg - optlen - ipoptlen;
972df8bae1dSRodney W. Grimes 			sendalot = 1;
973df8bae1dSRodney W. Grimes 		}
974ed420311SAndre Oppermann 	} else
975ed420311SAndre Oppermann 		tso = 0;
976ed420311SAndre Oppermann 
977ed420311SAndre Oppermann 	KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
978ed420311SAndre Oppermann 	    ("%s: len > IP_MAXPACKET", __func__));
979df8bae1dSRodney W. Grimes 
980a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/
981a683a7ddSYoshinobu Inoue #ifdef INET6
982a683a7ddSYoshinobu Inoue 	if (max_linkhdr + hdrlen > MCLBYTES)
983a683a7ddSYoshinobu Inoue #else
984df8bae1dSRodney W. Grimes 	if (max_linkhdr + hdrlen > MHLEN)
985a683a7ddSYoshinobu Inoue #endif
986f10e85d7SLuigi Rizzo 		panic("tcphdr too big");
987a0292f23SGarrett Wollman /*#endif*/
988df8bae1dSRodney W. Grimes 
989df8bae1dSRodney W. Grimes 	/*
990c4982faeSBjoern A. Zeeb 	 * This KASSERT is here to catch edge cases at a well defined place.
991c4982faeSBjoern A. Zeeb 	 * Before, those had triggered (random) panic conditions further down.
992c4982faeSBjoern A. Zeeb 	 */
993c4982faeSBjoern A. Zeeb 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
994c4982faeSBjoern A. Zeeb 
995c4982faeSBjoern A. Zeeb 	/*
996df8bae1dSRodney W. Grimes 	 * Grab a header mbuf, attaching a copy of data to
997df8bae1dSRodney W. Grimes 	 * be transmitted, and initialize the header from
998df8bae1dSRodney W. Grimes 	 * the template for sends on this connection.
999df8bae1dSRodney W. Grimes 	 */
1000df8bae1dSRodney W. Grimes 	if (len) {
10014e023759SAndre Oppermann 		struct mbuf *mb;
10024e023759SAndre Oppermann 		u_int moff;
10034e023759SAndre Oppermann 
10042cdbfa66SPaul Saab 		if ((tp->t_flags & TF_FORCEDATA) && len == 1)
100578b50714SRobert Watson 			TCPSTAT_INC(tcps_sndprobe);
10060ba5d2eeSJohn Baldwin 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
1007f5d34df5SGeorge V. Neville-Neil 			tp->t_sndrexmitpack++;
100878b50714SRobert Watson 			TCPSTAT_INC(tcps_sndrexmitpack);
100978b50714SRobert Watson 			TCPSTAT_ADD(tcps_sndrexmitbyte, len);
1010df8bae1dSRodney W. Grimes 		} else {
101178b50714SRobert Watson 			TCPSTAT_INC(tcps_sndpack);
101278b50714SRobert Watson 			TCPSTAT_ADD(tcps_sndbyte, len);
1013df8bae1dSRodney W. Grimes 		}
101439f6074eSGleb Smirnoff #ifdef INET6
101539f6074eSGleb Smirnoff 		if (MHLEN < hdrlen + max_linkhdr)
101639f6074eSGleb Smirnoff 			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
101739f6074eSGleb Smirnoff 		else
101839f6074eSGleb Smirnoff #endif
101939f6074eSGleb Smirnoff 			m = m_gethdr(M_NOWAIT, MT_DATA);
102039f6074eSGleb Smirnoff 
1021df8bae1dSRodney W. Grimes 		if (m == NULL) {
1022cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
1023df8bae1dSRodney W. Grimes 			error = ENOBUFS;
10240e2bc05cSGleb Smirnoff 			sack_rxmit = 0;
1025df8bae1dSRodney W. Grimes 			goto out;
1026df8bae1dSRodney W. Grimes 		}
102739f6074eSGleb Smirnoff 
1028df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
1029df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
10304e023759SAndre Oppermann 
10314e023759SAndre Oppermann 		/*
10324e023759SAndre Oppermann 		 * Start the m_copy functions from the closest mbuf
10334e023759SAndre Oppermann 		 * to the offset in the socket buffer chain.
10344e023759SAndre Oppermann 		 */
10354e023759SAndre Oppermann 		mb = sbsndptr(&so->so_snd, off, len, &moff);
10364e023759SAndre Oppermann 
1037df8bae1dSRodney W. Grimes 		if (len <= MHLEN - hdrlen - max_linkhdr) {
10384e023759SAndre Oppermann 			m_copydata(mb, moff, (int)len,
1039df8bae1dSRodney W. Grimes 			    mtod(m, caddr_t) + hdrlen);
1040df8bae1dSRodney W. Grimes 			m->m_len += len;
1041df8bae1dSRodney W. Grimes 		} else {
10424e023759SAndre Oppermann 			m->m_next = m_copy(mb, moff, (int)len);
10434e023759SAndre Oppermann 			if (m->m_next == NULL) {
1044cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
1045d7f570e6SGarrett Wollman 				(void) m_free(m);
104651823c3aSGarrett Wollman 				error = ENOBUFS;
10470e2bc05cSGleb Smirnoff 				sack_rxmit = 0;
104851823c3aSGarrett Wollman 				goto out;
104951823c3aSGarrett Wollman 			}
1050df8bae1dSRodney W. Grimes 		}
1051472ea5beSColin Percival 
1052df8bae1dSRodney W. Grimes 		/*
1053df8bae1dSRodney W. Grimes 		 * If we're sending everything we've got, set PUSH.
1054df8bae1dSRodney W. Grimes 		 * (This will keep happy those implementations which only
1055df8bae1dSRodney W. Grimes 		 * give data to the user when a buffer fills or
1056df8bae1dSRodney W. Grimes 		 * a PUSH comes in.)
1057df8bae1dSRodney W. Grimes 		 */
1058281a0fd4SPatrick Kelsey 		if ((off + len == sbused(&so->so_snd)) && !(flags & TH_SYN))
1059df8bae1dSRodney W. Grimes 			flags |= TH_PUSH;
1060cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1061df8bae1dSRodney W. Grimes 	} else {
1062cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1063df8bae1dSRodney W. Grimes 		if (tp->t_flags & TF_ACKNOW)
106478b50714SRobert Watson 			TCPSTAT_INC(tcps_sndacks);
1065df8bae1dSRodney W. Grimes 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
106678b50714SRobert Watson 			TCPSTAT_INC(tcps_sndctrl);
1067df8bae1dSRodney W. Grimes 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
106878b50714SRobert Watson 			TCPSTAT_INC(tcps_sndurg);
1069df8bae1dSRodney W. Grimes 		else
107078b50714SRobert Watson 			TCPSTAT_INC(tcps_sndwinup);
1071df8bae1dSRodney W. Grimes 
1072aa8bd99dSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
1073df8bae1dSRodney W. Grimes 		if (m == NULL) {
1074df8bae1dSRodney W. Grimes 			error = ENOBUFS;
10750e2bc05cSGleb Smirnoff 			sack_rxmit = 0;
1076df8bae1dSRodney W. Grimes 			goto out;
1077df8bae1dSRodney W. Grimes 		}
1078fb59c426SYoshinobu Inoue #ifdef INET6
1079fb59c426SYoshinobu Inoue 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
1080fb59c426SYoshinobu Inoue 		    MHLEN >= hdrlen) {
1081ed6a66caSRobert Watson 			M_ALIGN(m, hdrlen);
1082fb59c426SYoshinobu Inoue 		} else
1083fb59c426SYoshinobu Inoue #endif
1084df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
1085df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
1086df8bae1dSRodney W. Grimes 	}
1087cf2942b6SRobert Watson 	SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
1088df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = (struct ifnet *)0;
1089c488362eSRobert Watson #ifdef MAC
109030d239bcSRobert Watson 	mac_inpcb_create_mbuf(tp->t_inpcb, m);
1091c488362eSRobert Watson #endif
1092fb59c426SYoshinobu Inoue #ifdef INET6
1093fb59c426SYoshinobu Inoue 	if (isipv6) {
1094fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
1095fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip6 + 1);
109679909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip6, th);
1097fb59c426SYoshinobu Inoue 	} else
1098fb59c426SYoshinobu Inoue #endif /* INET6 */
1099fb59c426SYoshinobu Inoue 	{
1100fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
1101fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
1102fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip + 1);
110379909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip, th);
1104fb59c426SYoshinobu Inoue 	}
1105df8bae1dSRodney W. Grimes 
1106df8bae1dSRodney W. Grimes 	/*
1107df8bae1dSRodney W. Grimes 	 * Fill in fields, remembering maximum advertised
1108df8bae1dSRodney W. Grimes 	 * window for use in delaying messages about window sizes.
1109df8bae1dSRodney W. Grimes 	 * If resending a FIN, be sure not to use a new sequence number.
1110df8bae1dSRodney W. Grimes 	 */
1111df8bae1dSRodney W. Grimes 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
1112df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max)
1113df8bae1dSRodney W. Grimes 		tp->snd_nxt--;
1114df8bae1dSRodney W. Grimes 	/*
1115f2512ba1SRui Paulo 	 * If we are starting a connection, send ECN setup
1116f2512ba1SRui Paulo 	 * SYN packet. If we are on a retransmit, we may
1117f2512ba1SRui Paulo 	 * resend those bits a number of times as per
1118f2512ba1SRui Paulo 	 * RFC 3168.
1119f2512ba1SRui Paulo 	 */
1120603724d3SBjoern A. Zeeb 	if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
1121f2512ba1SRui Paulo 		if (tp->t_rxtshift >= 1) {
1122603724d3SBjoern A. Zeeb 			if (tp->t_rxtshift <= V_tcp_ecn_maxretries)
1123f2512ba1SRui Paulo 				flags |= TH_ECE|TH_CWR;
1124f2512ba1SRui Paulo 		} else
1125f2512ba1SRui Paulo 			flags |= TH_ECE|TH_CWR;
1126f2512ba1SRui Paulo 	}
1127f2512ba1SRui Paulo 
1128f2512ba1SRui Paulo 	if (tp->t_state == TCPS_ESTABLISHED &&
1129f2512ba1SRui Paulo 	    (tp->t_flags & TF_ECN_PERMIT)) {
1130f2512ba1SRui Paulo 		/*
1131f2512ba1SRui Paulo 		 * If the peer has ECN, mark data packets with
1132f2512ba1SRui Paulo 		 * ECN capable transmission (ECT).
1133f2512ba1SRui Paulo 		 * Ignore pure ack packets, retransmissions and window probes.
1134f2512ba1SRui Paulo 		 */
1135f2512ba1SRui Paulo 		if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
1136f2512ba1SRui Paulo 		    !((tp->t_flags & TF_FORCEDATA) && len == 1)) {
1137f2512ba1SRui Paulo #ifdef INET6
1138f2512ba1SRui Paulo 			if (isipv6)
1139f2512ba1SRui Paulo 				ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
1140f2512ba1SRui Paulo 			else
1141f2512ba1SRui Paulo #endif
1142f2512ba1SRui Paulo 				ip->ip_tos |= IPTOS_ECN_ECT0;
114378b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1144f2512ba1SRui Paulo 		}
1145f2512ba1SRui Paulo 
1146f2512ba1SRui Paulo 		/*
1147f2512ba1SRui Paulo 		 * Reply with proper ECN notifications.
1148f2512ba1SRui Paulo 		 */
1149f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_CWR) {
1150f2512ba1SRui Paulo 			flags |= TH_CWR;
1151f2512ba1SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_CWR;
1152f2512ba1SRui Paulo 		}
1153f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_ECE)
1154f2512ba1SRui Paulo 			flags |= TH_ECE;
1155f2512ba1SRui Paulo 	}
1156f2512ba1SRui Paulo 
1157f2512ba1SRui Paulo 	/*
1158df8bae1dSRodney W. Grimes 	 * If we are doing retransmissions, then snd_nxt will
1159df8bae1dSRodney W. Grimes 	 * not reflect the first unsent octet.  For ACK only
1160df8bae1dSRodney W. Grimes 	 * packets, we do not want the sequence number of the
1161df8bae1dSRodney W. Grimes 	 * retransmitted packet, we want the sequence number
1162df8bae1dSRodney W. Grimes 	 * of the next unsent octet.  So, if there is no data
1163df8bae1dSRodney W. Grimes 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
1164df8bae1dSRodney W. Grimes 	 * when filling in ti_seq.  But if we are in persist
1165df8bae1dSRodney W. Grimes 	 * state, snd_max might reflect one byte beyond the
1166df8bae1dSRodney W. Grimes 	 * right edge of the window, so use snd_nxt in that
1167df8bae1dSRodney W. Grimes 	 * case, since we know we aren't doing a retransmission.
1168df8bae1dSRodney W. Grimes 	 * (retransmit and persist are mutually exclusive...)
1169df8bae1dSRodney W. Grimes 	 */
1170a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
1171b8152ba7SAndre Oppermann 		if (len || (flags & (TH_SYN|TH_FIN)) ||
1172b8152ba7SAndre Oppermann 		    tcp_timer_active(tp, TT_PERSIST))
1173fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_nxt);
1174df8bae1dSRodney W. Grimes 		else
1175fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_max);
1176a55db2b6SPaul Saab 	} else {
11776d90faf3SPaul Saab 		th->th_seq = htonl(p->rxmit);
11786d90faf3SPaul Saab 		p->rxmit += len;
11790077b016SPaul Saab 		tp->sackhint.sack_bytes_rexmit += len;
11806d90faf3SPaul Saab 	}
1181fb59c426SYoshinobu Inoue 	th->th_ack = htonl(tp->rcv_nxt);
1182df8bae1dSRodney W. Grimes 	if (optlen) {
1183fb59c426SYoshinobu Inoue 		bcopy(opt, th + 1, optlen);
1184fb59c426SYoshinobu Inoue 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
1185df8bae1dSRodney W. Grimes 	}
1186fb59c426SYoshinobu Inoue 	th->th_flags = flags;
1187df8bae1dSRodney W. Grimes 	/*
1188df8bae1dSRodney W. Grimes 	 * Calculate receive window.  Don't shrink window,
1189df8bae1dSRodney W. Grimes 	 * but avoid silly window syndrome.
1190df8bae1dSRodney W. Grimes 	 */
1191201d185bSAndre Oppermann 	if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
1192201d185bSAndre Oppermann 	    recwin < (long)tp->t_maxseg)
1193201d185bSAndre Oppermann 		recwin = 0;
1194f701e30dSJohn Baldwin 	if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
1195f701e30dSJohn Baldwin 	    recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
1196201d185bSAndre Oppermann 		recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
1197201d185bSAndre Oppermann 	if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
1198201d185bSAndre Oppermann 		recwin = (long)TCP_MAXWIN << tp->rcv_scale;
1199262c1c1aSMatthew Dillon 
1200104ebb2aSAndre Oppermann 	/*
1201104ebb2aSAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
1202104ebb2aSAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK>
1203104ebb2aSAndre Oppermann 	 * case is handled in syncache.
1204104ebb2aSAndre Oppermann 	 */
1205104ebb2aSAndre Oppermann 	if (flags & TH_SYN)
1206104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)
1207104ebb2aSAndre Oppermann 				(min(sbspace(&so->so_rcv), TCP_MAXWIN)));
1208104ebb2aSAndre Oppermann 	else
1209104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
1210262c1c1aSMatthew Dillon 
1211262c1c1aSMatthew Dillon 	/*
1212262c1c1aSMatthew Dillon 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised
1213262c1c1aSMatthew Dillon 	 * a 0 window.  This may cause the remote transmitter to stall.  This
1214262c1c1aSMatthew Dillon 	 * flag tells soreceive() to disable delayed acknowledgements when
1215262c1c1aSMatthew Dillon 	 * draining the buffer.  This can occur if the receiver is attempting
1216b2722702SRui Paulo 	 * to read more data than can be buffered prior to transmitting on
1217262c1c1aSMatthew Dillon 	 * the connection.
1218262c1c1aSMatthew Dillon 	 */
1219f5d34df5SGeorge V. Neville-Neil 	if (th->th_win == 0) {
1220f5d34df5SGeorge V. Neville-Neil 		tp->t_sndzerowin++;
1221262c1c1aSMatthew Dillon 		tp->t_flags |= TF_RXWIN0SENT;
1222f5d34df5SGeorge V. Neville-Neil 	} else
1223262c1c1aSMatthew Dillon 		tp->t_flags &= ~TF_RXWIN0SENT;
1224df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1225fb59c426SYoshinobu Inoue 		th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
1226fb59c426SYoshinobu Inoue 		th->th_flags |= TH_URG;
1227df8bae1dSRodney W. Grimes 	} else
1228df8bae1dSRodney W. Grimes 		/*
1229df8bae1dSRodney W. Grimes 		 * If no urgent pointer to send, then we pull
1230df8bae1dSRodney W. Grimes 		 * the urgent pointer to the left edge of the send window
1231df8bae1dSRodney W. Grimes 		 * so that it doesn't drift into the send window on sequence
1232df8bae1dSRodney W. Grimes 		 * number wraparound.
1233df8bae1dSRodney W. Grimes 		 */
1234df8bae1dSRodney W. Grimes 		tp->snd_up = tp->snd_una;		/* drag it along */
1235df8bae1dSRodney W. Grimes 
12361cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
1237f73d9fd2SGleb Smirnoff 	if (to.to_flags & TOF_SIGNATURE) {
1238ee763d0dSBjoern A. Zeeb 		int sigoff = to.to_signature - opt;
12393418daf2SBjoern A. Zeeb 		tcp_signature_compute(m, 0, len, optlen,
12401cfd4b53SBruce M Simpson 		    (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
1241ee763d0dSBjoern A. Zeeb 	}
1242265ed012SBruce M Simpson #endif
12431cfd4b53SBruce M Simpson 
1244df8bae1dSRodney W. Grimes 	/*
1245df8bae1dSRodney W. Grimes 	 * Put TCP length in extended header, and then
1246df8bae1dSRodney W. Grimes 	 * checksum extended header and data.
1247df8bae1dSRodney W. Grimes 	 */
1248fb59c426SYoshinobu Inoue 	m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
124945747ba5SBjoern A. Zeeb 	m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1250fb59c426SYoshinobu Inoue #ifdef INET6
125145747ba5SBjoern A. Zeeb 	if (isipv6) {
1252fb59c426SYoshinobu Inoue 		/*
1253fb59c426SYoshinobu Inoue 		 * ip6_plen is not need to be filled now, and will be filled
1254fb59c426SYoshinobu Inoue 		 * in ip6_output.
1255fb59c426SYoshinobu Inoue 		 */
1256356ab07eSBjoern A. Zeeb 		m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
125745747ba5SBjoern A. Zeeb 		th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) +
125845747ba5SBjoern A. Zeeb 		    optlen + len, IPPROTO_TCP, 0);
125945747ba5SBjoern A. Zeeb 	}
126045747ba5SBjoern A. Zeeb #endif
126145747ba5SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1262fb59c426SYoshinobu Inoue 	else
126345747ba5SBjoern A. Zeeb #endif
126445747ba5SBjoern A. Zeeb #ifdef INET
1265fb59c426SYoshinobu Inoue 	{
1266356ab07eSBjoern A. Zeeb 		m->m_pkthdr.csum_flags = CSUM_TCP;
126779909384SJonathan Lemon 		th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
126879909384SJonathan Lemon 		    htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
1269fb59c426SYoshinobu Inoue 
1270db4f9cc7SJonathan Lemon 		/* IP version must be set here for ipv4/ipv6 checking later */
1271db4f9cc7SJonathan Lemon 		KASSERT(ip->ip_v == IPVERSION,
12726e551fb6SDavid E. O'Brien 		    ("%s: IP version incorrect: %d", __func__, ip->ip_v));
1273fb59c426SYoshinobu Inoue 	}
127445747ba5SBjoern A. Zeeb #endif
1275df8bae1dSRodney W. Grimes 
1276df8bae1dSRodney W. Grimes 	/*
1277b3c0f300SAndre Oppermann 	 * Enable TSO and specify the size of the segments.
1278b3c0f300SAndre Oppermann 	 * The TCP pseudo header checksum is always provided.
1279b3c0f300SAndre Oppermann 	 */
1280b3c0f300SAndre Oppermann 	if (tso) {
12810c39d38dSGleb Smirnoff 		KASSERT(len > tp->t_maxseg - optlen,
1282153e5b57SAndre Oppermann 		    ("%s: len <= tso_segsz", __func__));
12833579cf4cSKenneth D. Merry 		m->m_pkthdr.csum_flags |= CSUM_TSO;
12840c39d38dSGleb Smirnoff 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
1285b3c0f300SAndre Oppermann 	}
1286b3c0f300SAndre Oppermann 
128775497cc5SBjoern A. Zeeb #ifdef IPSEC
128875497cc5SBjoern A. Zeeb 	KASSERT(len + hdrlen + ipoptlen - ipsec_optlen == m_length(m, NULL),
128975497cc5SBjoern A. Zeeb 	    ("%s: mbuf chain shorter than expected: %ld + %u + %u - %u != %u",
129075497cc5SBjoern A. Zeeb 	    __func__, len, hdrlen, ipoptlen, ipsec_optlen, m_length(m, NULL)));
129175497cc5SBjoern A. Zeeb #else
1292ed420311SAndre Oppermann 	KASSERT(len + hdrlen + ipoptlen == m_length(m, NULL),
129375497cc5SBjoern A. Zeeb 	    ("%s: mbuf chain shorter than expected: %ld + %u + %u != %u",
129475497cc5SBjoern A. Zeeb 	    __func__, len, hdrlen, ipoptlen, m_length(m, NULL)));
129575497cc5SBjoern A. Zeeb #endif
1296ed420311SAndre Oppermann 
129739bc9de5SLawrence Stewart 	/* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
129839bc9de5SLawrence Stewart 	hhook_run_tcp_est_out(tp, th, &to, len, tso);
129939bc9de5SLawrence Stewart 
1300610ee2f9SDavid Greenman #ifdef TCPDEBUG
1301df8bae1dSRodney W. Grimes 	/*
1302df8bae1dSRodney W. Grimes 	 * Trace.
1303df8bae1dSRodney W. Grimes 	 */
130491f467d5SHartmut Brandt 	if (so->so_options & SO_DEBUG) {
1305f3e0b7efSBruce M Simpson 		u_short save = 0;
13065214cb3fSBruce M Simpson #ifdef INET6
13075214cb3fSBruce M Simpson 		if (!isipv6)
13085214cb3fSBruce M Simpson #endif
13095214cb3fSBruce M Simpson 		{
13105214cb3fSBruce M Simpson 			save = ipov->ih_len;
131191f467d5SHartmut Brandt 			ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
13125214cb3fSBruce M Simpson 		}
1313fb59c426SYoshinobu Inoue 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
13145214cb3fSBruce M Simpson #ifdef INET6
13155214cb3fSBruce M Simpson 		if (!isipv6)
13165214cb3fSBruce M Simpson #endif
131791f467d5SHartmut Brandt 		ipov->ih_len = save;
131891f467d5SHartmut Brandt 	}
1319b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
13205d06879aSGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, mtod(m, const char *));
1321df8bae1dSRodney W. Grimes 
1322df8bae1dSRodney W. Grimes 	/*
1323df8bae1dSRodney W. Grimes 	 * Fill in IP length and desired time to live and
1324df8bae1dSRodney W. Grimes 	 * send to IP level.  There should be a better way
1325df8bae1dSRodney W. Grimes 	 * to handle ttl and tos; we could keep them in
1326df8bae1dSRodney W. Grimes 	 * the template, but need a way to checksum without them.
1327df8bae1dSRodney W. Grimes 	 */
1328fb59c426SYoshinobu Inoue 	/*
132943630e62SHiren Panchasara 	 * m->m_pkthdr.len should have been set before checksum calculation,
1330fb59c426SYoshinobu Inoue 	 * because in6_cksum() need it.
1331fb59c426SYoshinobu Inoue 	 */
1332fb59c426SYoshinobu Inoue #ifdef INET6
1333fb59c426SYoshinobu Inoue 	if (isipv6) {
1334df0633a1SGleb Smirnoff 		struct route_in6 ro;
1335df0633a1SGleb Smirnoff 
1336df0633a1SGleb Smirnoff 		bzero(&ro, sizeof(ro));
1337fb59c426SYoshinobu Inoue 		/*
1338fb59c426SYoshinobu Inoue 		 * we separately set hoplimit for every segment, since the
1339fb59c426SYoshinobu Inoue 		 * user might want to change the value via setsockopt.
1340fb59c426SYoshinobu Inoue 		 * Also, desired default hop limit might be changed via
1341fb59c426SYoshinobu Inoue 		 * Neighbor Discovery.
1342fb59c426SYoshinobu Inoue 		 */
134397d8d152SAndre Oppermann 		ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1344fb59c426SYoshinobu Inoue 
134557f60867SMark Johnston 		/*
134657f60867SMark Johnston 		 * Set the packet size here for the benefit of DTrace probes.
134757f60867SMark Johnston 		 * ip6_output() will set it properly; it's supposed to include
134857f60867SMark Johnston 		 * the option header lengths as well.
134957f60867SMark Johnston 		 */
135057f60867SMark Johnston 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
135157f60867SMark Johnston 
13520c39d38dSGleb Smirnoff 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
13530f3e3bc5SSean Bruno 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13540f3e3bc5SSean Bruno 		else
13550f3e3bc5SSean Bruno 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13560f3e3bc5SSean Bruno 
135757f60867SMark Johnston 		if (tp->t_state == TCPS_SYN_SENT)
1358d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
135957f60867SMark Johnston 
136057f60867SMark Johnston 		TCP_PROBE5(send, NULL, tp, ip6, tp, th);
136157f60867SMark Johnston 
136286a996e6SHiren Panchasara #ifdef TCPPCAP
136386a996e6SHiren Panchasara 		/* Save packet, if requested. */
136486a996e6SHiren Panchasara 		tcp_pcap_add(th, m, &(tp->t_outpkts));
136586a996e6SHiren Panchasara #endif
136686a996e6SHiren Panchasara 
1367fb59c426SYoshinobu Inoue 		/* TODO: IPv6 IP6TOS_ECT bit on */
1368df0633a1SGleb Smirnoff 		error = ip6_output(m, tp->t_inpcb->in6p_outputopts, &ro,
1369df0633a1SGleb Smirnoff 		    ((so->so_options & SO_DONTROUTE) ?  IP_ROUTETOIF : 0),
1370df0633a1SGleb Smirnoff 		    NULL, NULL, tp->t_inpcb);
1371df0633a1SGleb Smirnoff 
1372df0633a1SGleb Smirnoff 		if (error == EMSGSIZE && ro.ro_rt != NULL)
1373e3a7aa6fSGleb Smirnoff 			mtu = ro.ro_rt->rt_mtu;
1374df0633a1SGleb Smirnoff 		RO_RTFREE(&ro);
1375b287c6c7SBjoern A. Zeeb 	}
1376fb59c426SYoshinobu Inoue #endif /* INET6 */
1377b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1378b287c6c7SBjoern A. Zeeb 	else
1379b287c6c7SBjoern A. Zeeb #endif
1380b287c6c7SBjoern A. Zeeb #ifdef INET
1381df8bae1dSRodney W. Grimes     {
1382df0633a1SGleb Smirnoff 	struct route ro;
1383df0633a1SGleb Smirnoff 
1384df0633a1SGleb Smirnoff 	bzero(&ro, sizeof(ro));
13858f134647SGleb Smirnoff 	ip->ip_len = htons(m->m_pkthdr.len);
1386686cdd19SJun-ichiro itojun Hagino #ifdef INET6
13875cd54324SBjoern A. Zeeb 	if (tp->t_inpcb->inp_vflag & INP_IPV6PROTO)
138897d8d152SAndre Oppermann 		ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1389686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */
1390f138387aSGarrett Wollman 	/*
139197d8d152SAndre Oppermann 	 * If we do path MTU discovery, then we set DF on every packet.
139297d8d152SAndre Oppermann 	 * This might not be the best thing to do according to RFC3390
139397d8d152SAndre Oppermann 	 * Section 2. However the tcp hostcache migitates the problem
139497d8d152SAndre Oppermann 	 * so it affects only the first tcp connection with a host.
1395e4e92660SAndre Oppermann 	 *
1396e4e92660SAndre Oppermann 	 * NB: Don't set DF on small MTU/MSS to have a safe fallback.
1397f138387aSGarrett Wollman 	 */
13980c39d38dSGleb Smirnoff 	if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
13998f134647SGleb Smirnoff 		ip->ip_off |= htons(IP_DF);
1400f6f6703fSSean Bruno 		tp->t_flags2 |= TF2_PLPMTU_PMTUD;
1401f6f6703fSSean Bruno 	} else {
1402f6f6703fSSean Bruno 		tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
1403f6f6703fSSean Bruno 	}
140497d8d152SAndre Oppermann 
140557f60867SMark Johnston 	if (tp->t_state == TCPS_SYN_SENT)
1406d9fae5abSAndriy Gapon 		TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
140757f60867SMark Johnston 
140857f60867SMark Johnston 	TCP_PROBE5(send, NULL, tp, ip, tp, th);
140957f60867SMark Johnston 
141086a996e6SHiren Panchasara #ifdef TCPPCAP
141186a996e6SHiren Panchasara 	/* Save packet, if requested. */
141286a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_outpkts));
141386a996e6SHiren Panchasara #endif
141486a996e6SHiren Panchasara 
1415df0633a1SGleb Smirnoff 	error = ip_output(m, tp->t_inpcb->inp_options, &ro,
1416b5d47ff5SJohn-Mark Gurney 	    ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0,
1417b5d47ff5SJohn-Mark Gurney 	    tp->t_inpcb);
1418df0633a1SGleb Smirnoff 
1419df0633a1SGleb Smirnoff 	if (error == EMSGSIZE && ro.ro_rt != NULL)
1420e3a7aa6fSGleb Smirnoff 		mtu = ro.ro_rt->rt_mtu;
1421df0633a1SGleb Smirnoff 	RO_RTFREE(&ro);
1422df8bae1dSRodney W. Grimes     }
1423b287c6c7SBjoern A. Zeeb #endif /* INET */
14240e2bc05cSGleb Smirnoff 
14250e2bc05cSGleb Smirnoff out:
14260e2bc05cSGleb Smirnoff 	/*
14270e2bc05cSGleb Smirnoff 	 * In transmit state, time the transmission and arrange for
14280e2bc05cSGleb Smirnoff 	 * the retransmit.  In persist state, just set snd_max.
14290e2bc05cSGleb Smirnoff 	 */
14300e2bc05cSGleb Smirnoff 	if ((tp->t_flags & TF_FORCEDATA) == 0 ||
14310e2bc05cSGleb Smirnoff 	    !tcp_timer_active(tp, TT_PERSIST)) {
14320e2bc05cSGleb Smirnoff 		tcp_seq startseq = tp->snd_nxt;
14330e2bc05cSGleb Smirnoff 
14340e2bc05cSGleb Smirnoff 		/*
14350e2bc05cSGleb Smirnoff 		 * Advance snd_nxt over sequence space of this segment.
14360e2bc05cSGleb Smirnoff 		 */
14370e2bc05cSGleb Smirnoff 		if (flags & (TH_SYN|TH_FIN)) {
14380e2bc05cSGleb Smirnoff 			if (flags & TH_SYN)
14390e2bc05cSGleb Smirnoff 				tp->snd_nxt++;
14400e2bc05cSGleb Smirnoff 			if (flags & TH_FIN) {
14410e2bc05cSGleb Smirnoff 				tp->snd_nxt++;
14420e2bc05cSGleb Smirnoff 				tp->t_flags |= TF_SENTFIN;
14430e2bc05cSGleb Smirnoff 			}
14440e2bc05cSGleb Smirnoff 		}
14450e2bc05cSGleb Smirnoff 		if (sack_rxmit)
14460e2bc05cSGleb Smirnoff 			goto timer;
14470e2bc05cSGleb Smirnoff 		tp->snd_nxt += len;
14480e2bc05cSGleb Smirnoff 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
14490e2bc05cSGleb Smirnoff 			tp->snd_max = tp->snd_nxt;
14500e2bc05cSGleb Smirnoff 			/*
14510e2bc05cSGleb Smirnoff 			 * Time this transmission if not a retransmission and
14520e2bc05cSGleb Smirnoff 			 * not currently timing anything.
14530e2bc05cSGleb Smirnoff 			 */
14540e2bc05cSGleb Smirnoff 			if (tp->t_rtttime == 0) {
14550e2bc05cSGleb Smirnoff 				tp->t_rtttime = ticks;
14560e2bc05cSGleb Smirnoff 				tp->t_rtseq = startseq;
14570e2bc05cSGleb Smirnoff 				TCPSTAT_INC(tcps_segstimed);
14580e2bc05cSGleb Smirnoff 			}
14590e2bc05cSGleb Smirnoff 		}
14600e2bc05cSGleb Smirnoff 
14610e2bc05cSGleb Smirnoff 		/*
14620e2bc05cSGleb Smirnoff 		 * Set retransmit timer if not currently set,
14630e2bc05cSGleb Smirnoff 		 * and not doing a pure ack or a keep-alive probe.
14640e2bc05cSGleb Smirnoff 		 * Initial value for retransmit timer is smoothed
14650e2bc05cSGleb Smirnoff 		 * round-trip time + 2 * round-trip time variance.
14660e2bc05cSGleb Smirnoff 		 * Initialize shift counter which is used for backoff
14670e2bc05cSGleb Smirnoff 		 * of retransmit time.
14680e2bc05cSGleb Smirnoff 		 */
14690e2bc05cSGleb Smirnoff timer:
14700e2bc05cSGleb Smirnoff 		if (!tcp_timer_active(tp, TT_REXMT) &&
14710e2bc05cSGleb Smirnoff 		    ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
14720e2bc05cSGleb Smirnoff 		     (tp->snd_nxt != tp->snd_una))) {
14730e2bc05cSGleb Smirnoff 			if (tcp_timer_active(tp, TT_PERSIST)) {
14740e2bc05cSGleb Smirnoff 				tcp_timer_activate(tp, TT_PERSIST, 0);
14750e2bc05cSGleb Smirnoff 				tp->t_rxtshift = 0;
14760e2bc05cSGleb Smirnoff 			}
14770e2bc05cSGleb Smirnoff 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
1478f8568079SHiren Panchasara 		} else if (len == 0 && sbavail(&so->so_snd) &&
1479f8568079SHiren Panchasara 		    !tcp_timer_active(tp, TT_REXMT) &&
1480f8568079SHiren Panchasara 		    !tcp_timer_active(tp, TT_PERSIST)) {
1481f8568079SHiren Panchasara 			/*
1482f8568079SHiren Panchasara 			 * Avoid a situation where we do not set persist timer
1483f8568079SHiren Panchasara 			 * after a zero window condition. For example:
1484f8568079SHiren Panchasara 			 * 1) A -> B: packet with enough data to fill the window
1485f8568079SHiren Panchasara 			 * 2) B -> A: ACK for #1 + new data (0 window
1486f8568079SHiren Panchasara 			 *    advertisement)
1487f8568079SHiren Panchasara 			 * 3) A -> B: ACK for #2, 0 len packet
1488f8568079SHiren Panchasara 			 *
1489f8568079SHiren Panchasara 			 * In this case, A will not activate the persist timer,
1490f8568079SHiren Panchasara 			 * because it chose to send a packet. Unless tcp_output
1491f8568079SHiren Panchasara 			 * is called for some other reason (delayed ack timer,
1492f8568079SHiren Panchasara 			 * another input packet from B, socket syscall), A will
1493f8568079SHiren Panchasara 			 * not send zero window probes.
1494f8568079SHiren Panchasara 			 *
1495f8568079SHiren Panchasara 			 * So, if you send a 0-length packet, but there is data
1496f8568079SHiren Panchasara 			 * in the socket buffer, and neither the rexmt or
1497f8568079SHiren Panchasara 			 * persist timer is already set, then activate the
1498f8568079SHiren Panchasara 			 * persist timer.
1499f8568079SHiren Panchasara 			 */
1500f8568079SHiren Panchasara 			tp->t_rxtshift = 0;
1501f8568079SHiren Panchasara 			tcp_setpersist(tp);
15020e2bc05cSGleb Smirnoff 		}
15030e2bc05cSGleb Smirnoff 	} else {
15040e2bc05cSGleb Smirnoff 		/*
15050e2bc05cSGleb Smirnoff 		 * Persist case, update snd_max but since we are in
15060e2bc05cSGleb Smirnoff 		 * persist mode (no window) we do not update snd_nxt.
15070e2bc05cSGleb Smirnoff 		 */
15080e2bc05cSGleb Smirnoff 		int xlen = len;
15090e2bc05cSGleb Smirnoff 		if (flags & TH_SYN)
15100e2bc05cSGleb Smirnoff 			++xlen;
15110e2bc05cSGleb Smirnoff 		if (flags & TH_FIN) {
15120e2bc05cSGleb Smirnoff 			++xlen;
15130e2bc05cSGleb Smirnoff 			tp->t_flags |= TF_SENTFIN;
15140e2bc05cSGleb Smirnoff 		}
15150e2bc05cSGleb Smirnoff 		if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
15160e2bc05cSGleb Smirnoff 			tp->snd_max = tp->snd_nxt + len;
15170e2bc05cSGleb Smirnoff 	}
15180e2bc05cSGleb Smirnoff 
1519df8bae1dSRodney W. Grimes 	if (error) {
15207734ea06SArchie Cobbs 
15217734ea06SArchie Cobbs 		/*
15227734ea06SArchie Cobbs 		 * We know that the packet was lost, so back out the
15237734ea06SArchie Cobbs 		 * sequence number advance, if any.
15242c30ec0aSAndre Oppermann 		 *
15252c30ec0aSAndre Oppermann 		 * If the error is EPERM the packet got blocked by the
15262c30ec0aSAndre Oppermann 		 * local firewall.  Normally we should terminate the
15272c30ec0aSAndre Oppermann 		 * connection but the blocking may have been spurious
15282c30ec0aSAndre Oppermann 		 * due to a firewall reconfiguration cycle.  So we treat
15292c30ec0aSAndre Oppermann 		 * it like a packet loss and let the retransmit timer and
15302c30ec0aSAndre Oppermann 		 * timeouts do their work over time.
15312c30ec0aSAndre Oppermann 		 * XXX: It is a POLA question whether calling tcp_drop right
15322c30ec0aSAndre Oppermann 		 * away would be the really correct behavior instead.
15337734ea06SArchie Cobbs 		 */
153472757d9aSGleb Smirnoff 		if (((tp->t_flags & TF_FORCEDATA) == 0 ||
1535b8152ba7SAndre Oppermann 		    !tcp_timer_active(tp, TT_PERSIST)) &&
153672757d9aSGleb Smirnoff 		    ((flags & TH_SYN) == 0) &&
153772757d9aSGleb Smirnoff 		    (error != EPERM)) {
15380077b016SPaul Saab 			if (sack_rxmit) {
15395d3b1b75SJayanth Vijayaraghavan 				p->rxmit -= len;
15400077b016SPaul Saab 				tp->sackhint.sack_bytes_rexmit -= len;
154172757d9aSGleb Smirnoff 				KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
15420077b016SPaul Saab 				    ("sackhint bytes rtx >= 0"));
15430077b016SPaul Saab 			} else
15447734ea06SArchie Cobbs 				tp->snd_nxt -= len;
15457734ea06SArchie Cobbs 		}
1546cf2942b6SRobert Watson 		SOCKBUF_UNLOCK_ASSERT(&so->so_snd);	/* Check gotos. */
154772757d9aSGleb Smirnoff 		switch (error) {
154872757d9aSGleb Smirnoff 		case EPERM:
154972757d9aSGleb Smirnoff 			tp->t_softerror = error;
155072757d9aSGleb Smirnoff 			return (error);
155172757d9aSGleb Smirnoff 		case ENOBUFS:
1552b8152ba7SAndre Oppermann 	                if (!tcp_timer_active(tp, TT_REXMT) &&
1553b8152ba7SAndre Oppermann 			    !tcp_timer_active(tp, TT_PERSIST))
1554b8152ba7SAndre Oppermann 	                        tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
15551600372bSAndre Oppermann 			tp->snd_cwnd = tp->t_maxseg;
1556df8bae1dSRodney W. Grimes 			return (0);
155772757d9aSGleb Smirnoff 		case EMSGSIZE:
15583d1f141bSGarrett Wollman 			/*
1559b3c0f300SAndre Oppermann 			 * For some reason the interface we used initially
1560b3c0f300SAndre Oppermann 			 * to send segments changed to another or lowered
1561b3c0f300SAndre Oppermann 			 * its MTU.
1562b3c0f300SAndre Oppermann 			 * If TSO was active we either got an interface
1563b3c0f300SAndre Oppermann 			 * without TSO capabilits or TSO was turned off.
1564df0633a1SGleb Smirnoff 			 * If we obtained mtu from ip_output() then update
1565df0633a1SGleb Smirnoff 			 * it and try again.
15663d1f141bSGarrett Wollman 			 */
1567b3c0f300SAndre Oppermann 			if (tso)
1568b3c0f300SAndre Oppermann 				tp->t_flags &= ~TF_TSO;
1569df0633a1SGleb Smirnoff 			if (mtu != 0) {
1570df0633a1SGleb Smirnoff 				tcp_mss_update(tp, -1, mtu, NULL, NULL);
1571df0633a1SGleb Smirnoff 				goto again;
1572df0633a1SGleb Smirnoff 			}
1573df0633a1SGleb Smirnoff 			return (error);
15748bec3467SGleb Smirnoff 		case EHOSTDOWN:
157572757d9aSGleb Smirnoff 		case EHOSTUNREACH:
157672757d9aSGleb Smirnoff 		case ENETDOWN:
15778bec3467SGleb Smirnoff 		case ENETUNREACH:
157872757d9aSGleb Smirnoff 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
1579df8bae1dSRodney W. Grimes 				tp->t_softerror = error;
1580df8bae1dSRodney W. Grimes 				return (0);
1581df8bae1dSRodney W. Grimes 			}
158272757d9aSGleb Smirnoff 			/* FALLTHROUGH */
158372757d9aSGleb Smirnoff 		default:
1584df8bae1dSRodney W. Grimes 			return (error);
1585df8bae1dSRodney W. Grimes 		}
158672757d9aSGleb Smirnoff 	}
158778b50714SRobert Watson 	TCPSTAT_INC(tcps_sndtotal);
1588df8bae1dSRodney W. Grimes 
1589df8bae1dSRodney W. Grimes 	/*
1590df8bae1dSRodney W. Grimes 	 * Data sent (as far as we can tell).
1591df8bae1dSRodney W. Grimes 	 * If this advertises a larger window than any other segment,
1592df8bae1dSRodney W. Grimes 	 * then remember the size of the advertised window.
1593df8bae1dSRodney W. Grimes 	 * Any pending ACK has now been sent.
1594df8bae1dSRodney W. Grimes 	 */
15956b7c15e5SJohn Baldwin 	if (recwin >= 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1596201d185bSAndre Oppermann 		tp->rcv_adv = tp->rcv_nxt + recwin;
1597df8bae1dSRodney W. Grimes 	tp->last_ack_sent = tp->rcv_nxt;
15983bfd6421SJonathan Lemon 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
1599b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_DELACK))
1600b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, 0);
1601d912c694SMatthew Dillon #if 0
1602d912c694SMatthew Dillon 	/*
1603d912c694SMatthew Dillon 	 * This completely breaks TCP if newreno is turned on.  What happens
1604d912c694SMatthew Dillon 	 * is that if delayed-acks are turned on on the receiver, this code
1605d912c694SMatthew Dillon 	 * on the transmitter effectively destroys the TCP window, forcing
1606d912c694SMatthew Dillon 	 * it to four packets (1.5Kx4 = 6K window).
1607d912c694SMatthew Dillon 	 */
1608dbc42409SLawrence Stewart 	if (sendalot && --maxburst)
1609df8bae1dSRodney W. Grimes 		goto again;
1610d912c694SMatthew Dillon #endif
1611d912c694SMatthew Dillon 	if (sendalot)
1612d912c694SMatthew Dillon 		goto again;
1613df8bae1dSRodney W. Grimes 	return (0);
1614df8bae1dSRodney W. Grimes }
1615df8bae1dSRodney W. Grimes 
1616df8bae1dSRodney W. Grimes void
1617ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp)
1618df8bae1dSRodney W. Grimes {
16199b8b58e0SJonathan Lemon 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
16209b8b58e0SJonathan Lemon 	int tt;
1621df8bae1dSRodney W. Grimes 
1622672dc4aeSJohn Baldwin 	tp->t_flags &= ~TF_PREVVALID;
1623b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_REXMT))
16249b8b58e0SJonathan Lemon 		panic("tcp_setpersist: retransmit pending");
1625df8bae1dSRodney W. Grimes 	/*
1626df8bae1dSRodney W. Grimes 	 * Start/restart persistance timer.
1627df8bae1dSRodney W. Grimes 	 */
16289b8b58e0SJonathan Lemon 	TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1629*0645c604SHiren Panchasara 		      tcp_persmin, tcp_persmax);
1630b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_PERSIST, tt);
1631df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1632df8bae1dSRodney W. Grimes 		tp->t_rxtshift++;
1633df8bae1dSRodney W. Grimes }
163402a1a643SAndre Oppermann 
163502a1a643SAndre Oppermann /*
163602a1a643SAndre Oppermann  * Insert TCP options according to the supplied parameters to the place
163702a1a643SAndre Oppermann  * optp in a consistent way.  Can handle unaligned destinations.
163802a1a643SAndre Oppermann  *
163902a1a643SAndre Oppermann  * The order of the option processing is crucial for optimal packing and
164002a1a643SAndre Oppermann  * alignment for the scarce option space.
164102a1a643SAndre Oppermann  *
164202a1a643SAndre Oppermann  * The optimal order for a SYN/SYN-ACK segment is:
164302a1a643SAndre Oppermann  *   MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) +
164402a1a643SAndre Oppermann  *   Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40.
164502a1a643SAndre Oppermann  *
164602a1a643SAndre Oppermann  * The SACK options should be last.  SACK blocks consume 8*n+2 bytes.
164702a1a643SAndre Oppermann  * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks).
164802a1a643SAndre Oppermann  * At minimum we need 10 bytes (to generate 1 SACK block).  If both
164902a1a643SAndre Oppermann  * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present,
165002a1a643SAndre Oppermann  * we only have 10 bytes for SACK options (40 - (12 + 18)).
165102a1a643SAndre Oppermann  */
165202a1a643SAndre Oppermann int
165302a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp)
165402a1a643SAndre Oppermann {
165502a1a643SAndre Oppermann 	u_int mask, optlen = 0;
165602a1a643SAndre Oppermann 
165702a1a643SAndre Oppermann 	for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) {
165802a1a643SAndre Oppermann 		if ((to->to_flags & mask) != mask)
165902a1a643SAndre Oppermann 			continue;
16603a4018c4SAndre Oppermann 		if (optlen == TCP_MAXOLEN)
16613a4018c4SAndre Oppermann 			break;
166202a1a643SAndre Oppermann 		switch (to->to_flags & mask) {
166302a1a643SAndre Oppermann 		case TOF_MSS:
166402a1a643SAndre Oppermann 			while (optlen % 4) {
166502a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
166602a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
166702a1a643SAndre Oppermann 			}
16683a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG)
16693a4018c4SAndre Oppermann 				continue;
167002a1a643SAndre Oppermann 			optlen += TCPOLEN_MAXSEG;
167102a1a643SAndre Oppermann 			*optp++ = TCPOPT_MAXSEG;
167202a1a643SAndre Oppermann 			*optp++ = TCPOLEN_MAXSEG;
167302a1a643SAndre Oppermann 			to->to_mss = htons(to->to_mss);
167402a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss));
167502a1a643SAndre Oppermann 			optp += sizeof(to->to_mss);
167602a1a643SAndre Oppermann 			break;
167702a1a643SAndre Oppermann 		case TOF_SCALE:
167802a1a643SAndre Oppermann 			while (!optlen || optlen % 2 != 1) {
167902a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
168002a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
168102a1a643SAndre Oppermann 			}
16823a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW)
16833a4018c4SAndre Oppermann 				continue;
168402a1a643SAndre Oppermann 			optlen += TCPOLEN_WINDOW;
168502a1a643SAndre Oppermann 			*optp++ = TCPOPT_WINDOW;
168602a1a643SAndre Oppermann 			*optp++ = TCPOLEN_WINDOW;
168702a1a643SAndre Oppermann 			*optp++ = to->to_wscale;
168802a1a643SAndre Oppermann 			break;
168902a1a643SAndre Oppermann 		case TOF_SACKPERM:
169002a1a643SAndre Oppermann 			while (optlen % 2) {
169102a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
169202a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
169302a1a643SAndre Oppermann 			}
16943a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED)
16953a4018c4SAndre Oppermann 				continue;
169602a1a643SAndre Oppermann 			optlen += TCPOLEN_SACK_PERMITTED;
169702a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK_PERMITTED;
169802a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACK_PERMITTED;
169902a1a643SAndre Oppermann 			break;
170002a1a643SAndre Oppermann 		case TOF_TS:
170102a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
170202a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
170302a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
170402a1a643SAndre Oppermann 			}
17053a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP)
17063a4018c4SAndre Oppermann 				continue;
170702a1a643SAndre Oppermann 			optlen += TCPOLEN_TIMESTAMP;
170802a1a643SAndre Oppermann 			*optp++ = TCPOPT_TIMESTAMP;
170902a1a643SAndre Oppermann 			*optp++ = TCPOLEN_TIMESTAMP;
171002a1a643SAndre Oppermann 			to->to_tsval = htonl(to->to_tsval);
171102a1a643SAndre Oppermann 			to->to_tsecr = htonl(to->to_tsecr);
171202a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval));
171302a1a643SAndre Oppermann 			optp += sizeof(to->to_tsval);
171402a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr));
171502a1a643SAndre Oppermann 			optp += sizeof(to->to_tsecr);
171602a1a643SAndre Oppermann 			break;
1717f73d9fd2SGleb Smirnoff #ifdef TCP_SIGNATURE
171802a1a643SAndre Oppermann 		case TOF_SIGNATURE:
171902a1a643SAndre Oppermann 			{
172002a1a643SAndre Oppermann 			int siglen = TCPOLEN_SIGNATURE - 2;
172102a1a643SAndre Oppermann 
172202a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
172302a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
172402a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
172502a1a643SAndre Oppermann 			}
17260d957bbaSAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE)
172702a1a643SAndre Oppermann 				continue;
172802a1a643SAndre Oppermann 			optlen += TCPOLEN_SIGNATURE;
172902a1a643SAndre Oppermann 			*optp++ = TCPOPT_SIGNATURE;
173002a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SIGNATURE;
173102a1a643SAndre Oppermann 			to->to_signature = optp;
173202a1a643SAndre Oppermann 			while (siglen--)
173302a1a643SAndre Oppermann 				 *optp++ = 0;
173402a1a643SAndre Oppermann 			break;
173502a1a643SAndre Oppermann 			}
1736f73d9fd2SGleb Smirnoff #endif
173702a1a643SAndre Oppermann 		case TOF_SACK:
173802a1a643SAndre Oppermann 			{
173902a1a643SAndre Oppermann 			int sackblks = 0;
174002a1a643SAndre Oppermann 			struct sackblk *sack = (struct sackblk *)to->to_sacks;
174102a1a643SAndre Oppermann 			tcp_seq sack_seq;
174202a1a643SAndre Oppermann 
174302a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
174402a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
174502a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
174602a1a643SAndre Oppermann 			}
17473a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK)
174802a1a643SAndre Oppermann 				continue;
174902a1a643SAndre Oppermann 			optlen += TCPOLEN_SACKHDR;
175002a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK;
175102a1a643SAndre Oppermann 			sackblks = min(to->to_nsacks,
17520d957bbaSAndre Oppermann 					(TCP_MAXOLEN - optlen) / TCPOLEN_SACK);
175302a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK;
175402a1a643SAndre Oppermann 			while (sackblks--) {
175502a1a643SAndre Oppermann 				sack_seq = htonl(sack->start);
175602a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
175702a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
175802a1a643SAndre Oppermann 				sack_seq = htonl(sack->end);
175902a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
176002a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
176102a1a643SAndre Oppermann 				optlen += TCPOLEN_SACK;
176202a1a643SAndre Oppermann 				sack++;
176302a1a643SAndre Oppermann 			}
176478b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_send_blocks);
176502a1a643SAndre Oppermann 			break;
176602a1a643SAndre Oppermann 			}
1767281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1768281a0fd4SPatrick Kelsey 		case TOF_FASTOPEN:
1769281a0fd4SPatrick Kelsey 			{
1770281a0fd4SPatrick Kelsey 			int total_len;
1771281a0fd4SPatrick Kelsey 
1772281a0fd4SPatrick Kelsey 			/* XXX is there any point to aligning this option? */
1773281a0fd4SPatrick Kelsey 			total_len = TCPOLEN_FAST_OPEN_EMPTY + to->to_tfo_len;
1774281a0fd4SPatrick Kelsey 			if (TCP_MAXOLEN - optlen < total_len)
1775281a0fd4SPatrick Kelsey 				continue;
1776281a0fd4SPatrick Kelsey 			*optp++ = TCPOPT_FAST_OPEN;
1777281a0fd4SPatrick Kelsey 			*optp++ = total_len;
1778281a0fd4SPatrick Kelsey 			if (to->to_tfo_len > 0) {
1779281a0fd4SPatrick Kelsey 				bcopy(to->to_tfo_cookie, optp, to->to_tfo_len);
1780281a0fd4SPatrick Kelsey 				optp += to->to_tfo_len;
1781281a0fd4SPatrick Kelsey 			}
1782281a0fd4SPatrick Kelsey 			optlen += total_len;
1783281a0fd4SPatrick Kelsey 			break;
1784281a0fd4SPatrick Kelsey 			}
1785281a0fd4SPatrick Kelsey #endif
178602a1a643SAndre Oppermann 		default:
178702a1a643SAndre Oppermann 			panic("%s: unknown TCP option type", __func__);
178802a1a643SAndre Oppermann 			break;
178902a1a643SAndre Oppermann 		}
179002a1a643SAndre Oppermann 	}
179102a1a643SAndre Oppermann 
179202a1a643SAndre Oppermann 	/* Terminate and pad TCP options to a 4 byte boundary. */
179302a1a643SAndre Oppermann 	if (optlen % 4) {
179402a1a643SAndre Oppermann 		optlen += TCPOLEN_EOL;
179502a1a643SAndre Oppermann 		*optp++ = TCPOPT_EOL;
179602a1a643SAndre Oppermann 	}
1797413deb12SBjoern A. Zeeb 	/*
1798413deb12SBjoern A. Zeeb 	 * According to RFC 793 (STD0007):
1799413deb12SBjoern A. Zeeb 	 *   "The content of the header beyond the End-of-Option option
1800413deb12SBjoern A. Zeeb 	 *    must be header padding (i.e., zero)."
1801413deb12SBjoern A. Zeeb 	 *   and later: "The padding is composed of zeros."
1802413deb12SBjoern A. Zeeb 	 */
180302a1a643SAndre Oppermann 	while (optlen % 4) {
1804c343c524SAndre Oppermann 		optlen += TCPOLEN_PAD;
1805c343c524SAndre Oppermann 		*optp++ = TCPOPT_PAD;
180602a1a643SAndre Oppermann 	}
180702a1a643SAndre Oppermann 
18080d957bbaSAndre Oppermann 	KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__));
180902a1a643SAndre Oppermann 	return (optlen);
181002a1a643SAndre Oppermann }
1811