xref: /freebsd/sys/netinet/tcp_output.c (revision 3df96ee68e232ef06ed7ea4b0f97a439b67ed45d)
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>
43bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
4439bc9de5SLawrence Stewart #include <sys/hhook.h>
45bd79708dSJonathan T. Looney #endif
46fb919e4dSMark Murray #include <sys/kernel.h>
47fb919e4dSMark Murray #include <sys/lock.h>
48fb919e4dSMark Murray #include <sys/mbuf.h>
49fb919e4dSMark Murray #include <sys/mutex.h>
50df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5157f60867SMark Johnston #include <sys/sdt.h>
52df8bae1dSRodney W. Grimes #include <sys/socket.h>
53df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
54fb919e4dSMark Murray #include <sys/sysctl.h>
55df8bae1dSRodney W. Grimes 
564b79449eSBjoern A. Zeeb #include <net/if.h>
57df8bae1dSRodney W. Grimes #include <net/route.h>
58530c0060SRobert Watson #include <net/vnet.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <netinet/in.h>
6157f60867SMark Johnston #include <netinet/in_kdtrace.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
63df8bae1dSRodney W. Grimes #include <netinet/ip.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
65df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
66ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
67fb59c426SYoshinobu Inoue #ifdef INET6
68686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
69686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
70fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
71fb59c426SYoshinobu Inoue #endif
72281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
73281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h>
74281a0fd4SPatrick Kelsey #endif
752de3e790SGleb Smirnoff #include <netinet/tcp.h>
76df8bae1dSRodney W. Grimes #define	TCPOUTFLAGS
77df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
78df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
79df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
80df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
81df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
824644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
8386a996e6SHiren Panchasara #ifdef TCPPCAP
8486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
8586a996e6SHiren Panchasara #endif
86610ee2f9SDavid Greenman #ifdef TCPDEBUG
87df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
88610ee2f9SDavid Greenman #endif
8909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
9009fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
9109fe6320SNavdeep Parhar #endif
92df8bae1dSRodney W. Grimes 
93b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
94b9234fafSSam Leffler #include <netipsec/ipsec.h>
95b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
96b9234fafSSam Leffler 
97db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
98db4f9cc7SJonathan Lemon 
99aed55708SRobert Watson #include <security/mac/mac_framework.h>
100aed55708SRobert Watson 
10182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, path_mtu_discovery) = 1;
1026df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_VNET | CTLFLAG_RW,
103eddfbb76SRobert Watson 	&VNET_NAME(path_mtu_discovery), 1,
104eddfbb76SRobert Watson 	"Enable Path MTU Discovery");
1059a039a5fSDavid Greenman 
10682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_tso) = 1;
10782cea7e6SBjoern A. Zeeb #define	V_tcp_do_tso		VNET(tcp_do_tso)
1086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_VNET | CTLFLAG_RW,
109eddfbb76SRobert Watson 	&VNET_NAME(tcp_do_tso), 0,
110eddfbb76SRobert Watson 	"Enable TCP Segmentation Offload");
111b3c0f300SAndre Oppermann 
112873789cbSAndre Oppermann VNET_DEFINE(int, tcp_sendspace) = 1024*32;
113873789cbSAndre Oppermann #define	V_tcp_sendspace	VNET(tcp_sendspace)
1146df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_VNET | CTLFLAG_RW,
115873789cbSAndre Oppermann 	&VNET_NAME(tcp_sendspace), 0, "Initial send socket buffer size");
116873789cbSAndre Oppermann 
11782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autosndbuf) = 1;
11882cea7e6SBjoern A. Zeeb #define	V_tcp_do_autosndbuf	VNET(tcp_do_autosndbuf)
1196df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
120eddfbb76SRobert Watson 	&VNET_NAME(tcp_do_autosndbuf), 0,
121eddfbb76SRobert Watson 	"Enable automatic send buffer sizing");
1226741ecf5SAndre Oppermann 
12382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_inc) = 8*1024;
12482cea7e6SBjoern A. Zeeb #define	V_tcp_autosndbuf_inc	VNET(tcp_autosndbuf_inc)
1256df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_VNET | CTLFLAG_RW,
126eddfbb76SRobert Watson 	&VNET_NAME(tcp_autosndbuf_inc), 0,
1278b615593SMarko Zec 	"Incrementor step size of automatic send buffer");
1286741ecf5SAndre Oppermann 
129b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_max) = 2*1024*1024;
13082cea7e6SBjoern A. Zeeb #define	V_tcp_autosndbuf_max	VNET(tcp_autosndbuf_max)
1316df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
132eddfbb76SRobert Watson 	&VNET_NAME(tcp_autosndbuf_max), 0,
1338b615593SMarko Zec 	"Max size of automatic send buffer");
1346741ecf5SAndre Oppermann 
135425b7639SSepherosa Ziehau /*
136425b7639SSepherosa Ziehau  * Make sure that either retransmit or persist timer is set for SYN, FIN and
137425b7639SSepherosa Ziehau  * non-ACK.
138425b7639SSepherosa Ziehau  */
139425b7639SSepherosa Ziehau #define TCP_XMIT_TIMER_ASSERT(tp, len, th_flags)			\
140425b7639SSepherosa Ziehau 	KASSERT(((len) == 0 && ((th_flags) & (TH_SYN | TH_FIN)) == 0) ||\
141425b7639SSepherosa Ziehau 	    tcp_timer_active((tp), TT_REXMT) ||				\
142425b7639SSepherosa Ziehau 	    tcp_timer_active((tp), TT_PERSIST),				\
143425b7639SSepherosa Ziehau 	    ("neither rexmt nor persist timer is set"))
144425b7639SSepherosa Ziehau 
145bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
14639bc9de5SLawrence Stewart static void inline	hhook_run_tcp_est_out(struct tcpcb *tp,
14739bc9de5SLawrence Stewart 			    struct tcphdr *th, struct tcpopt *to,
1483ac12506SJonathan T. Looney 			    uint32_t len, int tso);
149bd79708dSJonathan T. Looney #endif
150dbc42409SLawrence Stewart static void inline	cc_after_idle(struct tcpcb *tp);
151dbc42409SLawrence Stewart 
152bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
153dbc42409SLawrence Stewart /*
1540f5e7edcSKevin Lo  * Wrapper for the TCP established output helper hook.
15539bc9de5SLawrence Stewart  */
15639bc9de5SLawrence Stewart static void inline
15739bc9de5SLawrence Stewart hhook_run_tcp_est_out(struct tcpcb *tp, struct tcphdr *th,
1583ac12506SJonathan T. Looney     struct tcpopt *to, uint32_t len, int tso)
15939bc9de5SLawrence Stewart {
16039bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
16139bc9de5SLawrence Stewart 
16239bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_OUT]->hhh_nhooks > 0) {
16339bc9de5SLawrence Stewart 		hhook_data.tp = tp;
16439bc9de5SLawrence Stewart 		hhook_data.th = th;
16539bc9de5SLawrence Stewart 		hhook_data.to = to;
16639bc9de5SLawrence Stewart 		hhook_data.len = len;
16739bc9de5SLawrence Stewart 		hhook_data.tso = tso;
16839bc9de5SLawrence Stewart 
16939bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_OUT], &hhook_data,
17039bc9de5SLawrence Stewart 		    tp->osd);
17139bc9de5SLawrence Stewart 	}
17239bc9de5SLawrence Stewart }
173bd79708dSJonathan T. Looney #endif
17439bc9de5SLawrence Stewart 
17539bc9de5SLawrence Stewart /*
176dbc42409SLawrence Stewart  * CC wrapper hook functions
177dbc42409SLawrence Stewart  */
178dbc42409SLawrence Stewart static void inline
179dbc42409SLawrence Stewart cc_after_idle(struct tcpcb *tp)
180dbc42409SLawrence Stewart {
181dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
182dbc42409SLawrence Stewart 
183dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->after_idle != NULL)
184dbc42409SLawrence Stewart 		CC_ALGO(tp)->after_idle(tp->ccv);
185dbc42409SLawrence Stewart }
1866741ecf5SAndre Oppermann 
187df8bae1dSRodney W. Grimes /*
188df8bae1dSRodney W. Grimes  * Tcp output routine: figure out what should be sent and send it.
189df8bae1dSRodney W. Grimes  */
190df8bae1dSRodney W. Grimes int
191f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp)
192df8bae1dSRodney W. Grimes {
193f10e85d7SLuigi Rizzo 	struct socket *so = tp->t_inpcb->inp_socket;
1943ac12506SJonathan T. Looney 	int32_t len;
1953ac12506SJonathan T. Looney 	uint32_t recwin, sendwin;
196b287c6c7SBjoern A. Zeeb 	int off, flags, error = 0;	/* Keep compiler happy */
197f10e85d7SLuigi Rizzo 	struct mbuf *m;
198fb59c426SYoshinobu Inoue 	struct ip *ip = NULL;
199f10e85d7SLuigi Rizzo 	struct ipovly *ipov = NULL;
200f10e85d7SLuigi Rizzo 	struct tcphdr *th;
201a0292f23SGarrett Wollman 	u_char opt[TCP_MAXOLEN];
202a04884fcSBill Fenner 	unsigned ipoptlen, optlen, hdrlen;
2039ad0173dSBjoern A. Zeeb #ifdef IPSEC
2049ad0173dSBjoern A. Zeeb 	unsigned ipsec_optlen = 0;
2059ad0173dSBjoern A. Zeeb #endif
206df8bae1dSRodney W. Grimes 	int idle, sendalot;
20702a1a643SAndre Oppermann 	int sack_rxmit, sack_bytes_rxmt;
2086d90faf3SPaul Saab 	struct sackhole *p;
209df0633a1SGleb Smirnoff 	int tso, mtu;
21002a1a643SAndre Oppermann 	struct tcpopt to;
211262c1c1aSMatthew Dillon #if 0
21246f58482SJonathan Lemon 	int maxburst = TCP_MAXBURST;
213262c1c1aSMatthew Dillon #endif
214fb59c426SYoshinobu Inoue #ifdef INET6
215f10e85d7SLuigi Rizzo 	struct ip6_hdr *ip6 = NULL;
216fb59c426SYoshinobu Inoue 	int isipv6;
217fb59c426SYoshinobu Inoue 
218fb59c426SYoshinobu Inoue 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
219fb59c426SYoshinobu Inoue #endif
220df8bae1dSRodney W. Grimes 
2218501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2223d6ade3aSJennifer Yang 
22309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
22409fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
22509fe6320SNavdeep Parhar 		return (tcp_offload_output(tp));
22609fe6320SNavdeep Parhar #endif
22709fe6320SNavdeep Parhar 
228281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
229281a0fd4SPatrick Kelsey 	/*
230281a0fd4SPatrick Kelsey 	 * For TFO connections in SYN_RECEIVED, only allow the initial
231281a0fd4SPatrick Kelsey 	 * SYN|ACK and those sent by the retransmit timer.
232281a0fd4SPatrick Kelsey 	 */
23368bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
234281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED) &&
235a4641f4eSPedro F. Giffuni 	    SEQ_GT(tp->snd_max, tp->snd_una) &&    /* initial SYN|ACK sent */
236281a0fd4SPatrick Kelsey 	    (tp->snd_nxt != tp->snd_una))          /* not a retransmit */
237281a0fd4SPatrick Kelsey 		return (0);
238281a0fd4SPatrick Kelsey #endif
239df8bae1dSRodney W. Grimes 	/*
240df8bae1dSRodney W. Grimes 	 * Determine length of data that should be transmitted,
241df8bae1dSRodney W. Grimes 	 * and flags that will be used.
242df8bae1dSRodney W. Grimes 	 * If there is some data or critical controls (SYN, RST)
243df8bae1dSRodney W. Grimes 	 * to send, then transmit; otherwise, investigate further.
244df8bae1dSRodney W. Grimes 	 */
245c24d5daeSJayanth Vijayaraghavan 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
2462ea8da28SLawrence Stewart 	if (idle && ticks - tp->t_rcvtime >= tp->t_rxtcur)
2472ea8da28SLawrence Stewart 		cc_after_idle(tp);
248c24d5daeSJayanth Vijayaraghavan 	tp->t_flags &= ~TF_LASTIDLE;
249c24d5daeSJayanth Vijayaraghavan 	if (idle) {
250c24d5daeSJayanth Vijayaraghavan 		if (tp->t_flags & TF_MORETOCOME) {
251c24d5daeSJayanth Vijayaraghavan 			tp->t_flags |= TF_LASTIDLE;
252c24d5daeSJayanth Vijayaraghavan 			idle = 0;
253c24d5daeSJayanth Vijayaraghavan 		}
254c24d5daeSJayanth Vijayaraghavan 	}
255df8bae1dSRodney W. Grimes again:
2566d90faf3SPaul Saab 	/*
2576d90faf3SPaul Saab 	 * If we've recently taken a timeout, snd_max will be greater than
2586d90faf3SPaul Saab 	 * snd_nxt.  There may be SACK information that allows us to avoid
2596d90faf3SPaul Saab 	 * resending already delivered data.  Adjust snd_nxt accordingly.
2606d90faf3SPaul Saab 	 */
2613529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
2623529149eSAndre Oppermann 	    SEQ_LT(tp->snd_nxt, tp->snd_max))
2636d90faf3SPaul Saab 		tcp_sack_adjust(tp);
264df8bae1dSRodney W. Grimes 	sendalot = 0;
265153e5b57SAndre Oppermann 	tso = 0;
266df0633a1SGleb Smirnoff 	mtu = 0;
267df8bae1dSRodney W. Grimes 	off = tp->snd_nxt - tp->snd_una;
268201d185bSAndre Oppermann 	sendwin = min(tp->snd_wnd, tp->snd_cwnd);
269df8bae1dSRodney W. Grimes 
270df8bae1dSRodney W. Grimes 	flags = tcp_outflags[tp->t_state];
271a0292f23SGarrett Wollman 	/*
2726d90faf3SPaul Saab 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
2736d90faf3SPaul Saab 	 * to send out new data (when sendalot is 1), bypass this function.
2746d90faf3SPaul Saab 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
2756d90faf3SPaul Saab 	 * we're replacing a (future) new transmission with a retransmission
2766d90faf3SPaul Saab 	 * now, and we previously incremented snd_cwnd in tcp_input().
2776d90faf3SPaul Saab 	 */
2786d90faf3SPaul Saab 	/*
2796d90faf3SPaul Saab 	 * Still in sack recovery , reset rxmit flag to zero.
2806d90faf3SPaul Saab 	 */
2816d90faf3SPaul Saab 	sack_rxmit = 0;
282a55db2b6SPaul Saab 	sack_bytes_rxmt = 0;
2836d90faf3SPaul Saab 	len = 0;
2846d90faf3SPaul Saab 	p = NULL;
285dbc42409SLawrence Stewart 	if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp->t_flags) &&
286a55db2b6SPaul Saab 	    (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
2873ac12506SJonathan T. Looney 		uint32_t cwin;
288a55db2b6SPaul Saab 
2893ac12506SJonathan T. Looney 		cwin =
2903ac12506SJonathan T. Looney 		    imax(min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt, 0);
291f787edd8SJayanth Vijayaraghavan 		/* Do not retransmit SACK segments beyond snd_recover */
292f787edd8SJayanth Vijayaraghavan 		if (SEQ_GT(p->end, tp->snd_recover)) {
293f787edd8SJayanth Vijayaraghavan 			/*
294f787edd8SJayanth Vijayaraghavan 			 * (At least) part of sack hole extends beyond
295f787edd8SJayanth Vijayaraghavan 			 * snd_recover. Check to see if we can rexmit data
296f787edd8SJayanth Vijayaraghavan 			 * for this hole.
297f787edd8SJayanth Vijayaraghavan 			 */
298f787edd8SJayanth Vijayaraghavan 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
299f787edd8SJayanth Vijayaraghavan 				/*
300f787edd8SJayanth Vijayaraghavan 				 * Can't rexmit any more data for this hole.
301f787edd8SJayanth Vijayaraghavan 				 * That data will be rexmitted in the next
302f787edd8SJayanth Vijayaraghavan 				 * sack recovery episode, when snd_recover
303f787edd8SJayanth Vijayaraghavan 				 * moves past p->rxmit.
304f787edd8SJayanth Vijayaraghavan 				 */
305f787edd8SJayanth Vijayaraghavan 				p = NULL;
306f787edd8SJayanth Vijayaraghavan 				goto after_sack_rexmit;
307f787edd8SJayanth Vijayaraghavan 			} else
308f787edd8SJayanth Vijayaraghavan 				/* Can rexmit part of the current hole */
3093ac12506SJonathan T. Looney 				len = ((int32_t)ulmin(cwin,
310f787edd8SJayanth Vijayaraghavan 						   tp->snd_recover - p->rxmit));
311f787edd8SJayanth Vijayaraghavan 		} else
3123ac12506SJonathan T. Looney 			len = ((int32_t)ulmin(cwin, p->end - p->rxmit));
3136d90faf3SPaul Saab 		off = p->rxmit - tp->snd_una;
314f787edd8SJayanth Vijayaraghavan 		KASSERT(off >= 0,("%s: sack block to the left of una : %d",
315f787edd8SJayanth Vijayaraghavan 		    __func__, off));
3166d90faf3SPaul Saab 		if (len > 0) {
317ab5c14d8SRobert Watson 			sack_rxmit = 1;
318ab5c14d8SRobert Watson 			sendalot = 1;
31978b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rexmits);
32078b50714SRobert Watson 			TCPSTAT_ADD(tcps_sack_rexmit_bytes,
32178b50714SRobert Watson 			    min(len, tp->t_maxseg));
3226d90faf3SPaul Saab 		}
3236d90faf3SPaul Saab 	}
324f787edd8SJayanth Vijayaraghavan after_sack_rexmit:
3256d90faf3SPaul Saab 	/*
326a0292f23SGarrett Wollman 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
327a0292f23SGarrett Wollman 	 * state flags.
328a0292f23SGarrett Wollman 	 */
329a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDFIN)
330a0292f23SGarrett Wollman 		flags |= TH_FIN;
331a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDSYN)
332a0292f23SGarrett Wollman 		flags |= TH_SYN;
333a0292f23SGarrett Wollman 
334cf2942b6SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
335df8bae1dSRodney W. Grimes 	/*
336df8bae1dSRodney W. Grimes 	 * If in persist timeout with window of 0, send 1 byte.
337df8bae1dSRodney W. Grimes 	 * Otherwise, if window is small but nonzero
338df8bae1dSRodney W. Grimes 	 * and timer expired, we will send what we can
339df8bae1dSRodney W. Grimes 	 * and go to transmit state.
340df8bae1dSRodney W. Grimes 	 */
3412cdbfa66SPaul Saab 	if (tp->t_flags & TF_FORCEDATA) {
342201d185bSAndre Oppermann 		if (sendwin == 0) {
343df8bae1dSRodney W. Grimes 			/*
344df8bae1dSRodney W. Grimes 			 * If we still have some data to send, then
345df8bae1dSRodney W. Grimes 			 * clear the FIN bit.  Usually this would
346df8bae1dSRodney W. Grimes 			 * happen below when it realizes that we
347df8bae1dSRodney W. Grimes 			 * aren't sending all the data.  However,
34829089b51SJulian Elischer 			 * if we have exactly 1 byte of unsent data,
349df8bae1dSRodney W. Grimes 			 * then it won't clear the FIN bit below,
350df8bae1dSRodney W. Grimes 			 * and if we are in persist state, we wind
351df8bae1dSRodney W. Grimes 			 * up sending the packet without recording
352df8bae1dSRodney W. Grimes 			 * that we sent the FIN bit.
353df8bae1dSRodney W. Grimes 			 *
354df8bae1dSRodney W. Grimes 			 * We can't just blindly clear the FIN bit,
355df8bae1dSRodney W. Grimes 			 * because if we don't have any more data
356df8bae1dSRodney W. Grimes 			 * to send then the probe will be the FIN
357df8bae1dSRodney W. Grimes 			 * itself.
358df8bae1dSRodney W. Grimes 			 */
359cfa6009eSGleb Smirnoff 			if (off < sbused(&so->so_snd))
360df8bae1dSRodney W. Grimes 				flags &= ~TH_FIN;
361201d185bSAndre Oppermann 			sendwin = 1;
362df8bae1dSRodney W. Grimes 		} else {
363b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_PERSIST, 0);
364df8bae1dSRodney W. Grimes 			tp->t_rxtshift = 0;
365df8bae1dSRodney W. Grimes 		}
366df8bae1dSRodney W. Grimes 	}
367df8bae1dSRodney W. Grimes 
36828257b5cSMatthew Dillon 	/*
36928257b5cSMatthew Dillon 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
37028257b5cSMatthew Dillon 	 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
371201d185bSAndre Oppermann 	 * a negative length.  This can also occur when TCP opens up
37228257b5cSMatthew Dillon 	 * its congestion window while receiving additional duplicate
37328257b5cSMatthew Dillon 	 * acks after fast-retransmit because TCP will reset snd_nxt
37428257b5cSMatthew Dillon 	 * to snd_max after the fast-retransmit.
37528257b5cSMatthew Dillon 	 *
37628257b5cSMatthew Dillon 	 * In the normal retransmit-FIN-only case, however, snd_nxt will
37728257b5cSMatthew Dillon 	 * be set to snd_una, the offset will be 0, and the length may
37828257b5cSMatthew Dillon 	 * wind up 0.
3796d90faf3SPaul Saab 	 *
3806d90faf3SPaul Saab 	 * If sack_rxmit is true we are retransmitting from the scoreboard
3816d90faf3SPaul Saab 	 * in which case len is already set.
38228257b5cSMatthew Dillon 	 */
383a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
384a55db2b6SPaul Saab 		if (sack_bytes_rxmt == 0)
3853ac12506SJonathan T. Looney 			len = ((int32_t)ulmin(sbavail(&so->so_snd), sendwin) -
386cfa6009eSGleb Smirnoff 			    off);
387a55db2b6SPaul Saab 		else {
3883ac12506SJonathan T. Looney 			int32_t cwin;
389a55db2b6SPaul Saab 
390a55db2b6SPaul Saab                         /*
391a55db2b6SPaul Saab 			 * We are inside of a SACK recovery episode and are
392a55db2b6SPaul Saab 			 * sending new data, having retransmitted all the
393a55db2b6SPaul Saab 			 * data possible in the scoreboard.
394a55db2b6SPaul Saab 			 */
3953ac12506SJonathan T. Looney 			len = ((int32_t)min(sbavail(&so->so_snd), tp->snd_wnd) -
396cfa6009eSGleb Smirnoff 			    off);
3978d03f2b5SPaul Saab 			/*
3988d03f2b5SPaul Saab 			 * Don't remove this (len > 0) check !
3998d03f2b5SPaul Saab 			 * We explicitly check for len > 0 here (although it
4008d03f2b5SPaul Saab 			 * isn't really necessary), to work around a gcc
4018d03f2b5SPaul Saab 			 * optimization issue - to force gcc to compute
4028d03f2b5SPaul Saab 			 * len above. Without this check, the computation
4038d03f2b5SPaul Saab 			 * of len is bungled by the optimizer.
4048d03f2b5SPaul Saab 			 */
4058d03f2b5SPaul Saab 			if (len > 0) {
4067d5ed1ceSPaul Saab 				cwin = tp->snd_cwnd -
4077d5ed1ceSPaul Saab 					(tp->snd_nxt - tp->sack_newdata) -
408a55db2b6SPaul Saab 					sack_bytes_rxmt;
409a55db2b6SPaul Saab 				if (cwin < 0)
410a55db2b6SPaul Saab 					cwin = 0;
4113ac12506SJonathan T. Looney 				len = imin(len, cwin);
412a55db2b6SPaul Saab 			}
413a55db2b6SPaul Saab 		}
4148d03f2b5SPaul Saab 	}
415a0292f23SGarrett Wollman 
416a0292f23SGarrett Wollman 	/*
417a0292f23SGarrett Wollman 	 * Lop off SYN bit if it has already been sent.  However, if this
418a0292f23SGarrett Wollman 	 * is SYN-SENT state and if segment contains data and if we don't
419a0292f23SGarrett Wollman 	 * know that foreign host supports TAO, suppress sending segment.
420a0292f23SGarrett Wollman 	 */
421a0292f23SGarrett Wollman 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
4224b8e98d6SQing Li 		if (tp->t_state != TCPS_SYN_RECEIVED)
423a0292f23SGarrett Wollman 			flags &= ~TH_SYN;
424281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
425281a0fd4SPatrick Kelsey 		/*
426281a0fd4SPatrick Kelsey 		 * When sending additional segments following a TFO SYN|ACK,
427281a0fd4SPatrick Kelsey 		 * do not include the SYN bit.
428281a0fd4SPatrick Kelsey 		 */
42968bd7ed1SJonathan T. Looney 		if (IS_FASTOPEN(tp->t_flags) &&
430281a0fd4SPatrick Kelsey 		    (tp->t_state == TCPS_SYN_RECEIVED))
431281a0fd4SPatrick Kelsey 			flags &= ~TH_SYN;
432281a0fd4SPatrick Kelsey #endif
433a0292f23SGarrett Wollman 		off--, len++;
434a0292f23SGarrett Wollman 	}
435a0292f23SGarrett Wollman 
43681165e48SAndras Olah 	/*
437c94c54e4SAndre Oppermann 	 * Be careful not to send data and/or FIN on SYN segments.
43881165e48SAndras Olah 	 * This measure is needed to prevent interoperability problems
43981165e48SAndras Olah 	 * with not fully conformant TCP implementations.
44081165e48SAndras Olah 	 */
441c94c54e4SAndre Oppermann 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
44281165e48SAndras Olah 		len = 0;
44381165e48SAndras Olah 		flags &= ~TH_FIN;
44481165e48SAndras Olah 	}
44581165e48SAndras Olah 
446281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
447281a0fd4SPatrick Kelsey 	/*
448281a0fd4SPatrick Kelsey 	 * When retransmitting SYN|ACK on a passively-created TFO socket,
449281a0fd4SPatrick Kelsey 	 * don't include data, as the presence of data may have caused the
450281a0fd4SPatrick Kelsey 	 * original SYN|ACK to have been dropped by a middlebox.
451281a0fd4SPatrick Kelsey 	 */
45268bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
453281a0fd4SPatrick Kelsey 	    (((tp->t_state == TCPS_SYN_RECEIVED) && (tp->t_rxtshift > 0)) ||
454281a0fd4SPatrick Kelsey 	     (flags & TH_RST)))
455281a0fd4SPatrick Kelsey 		len = 0;
456281a0fd4SPatrick Kelsey #endif
45747a8e865SXin LI 	if (len <= 0) {
458df8bae1dSRodney W. Grimes 		/*
459df8bae1dSRodney W. Grimes 		 * If FIN has been sent but not acked,
460df8bae1dSRodney W. Grimes 		 * but we haven't been called to retransmit,
46128257b5cSMatthew Dillon 		 * len will be < 0.  Otherwise, window shrank
462df8bae1dSRodney W. Grimes 		 * after we sent into it.  If window shrank to 0,
4632d8266afSDavid Greenman 		 * cancel pending retransmit, pull snd_nxt back
4642d8266afSDavid Greenman 		 * to (closed) window, and set the persist timer
4652d8266afSDavid Greenman 		 * if it isn't already going.  If the window didn't
4662d8266afSDavid Greenman 		 * close completely, just wait for an ACK.
46747a8e865SXin LI 		 *
46847a8e865SXin LI 		 * We also do a general check here to ensure that
46947a8e865SXin LI 		 * we will set the persist timer when we have data
47047a8e865SXin LI 		 * to send, but a 0-byte window. This makes sure
47147a8e865SXin LI 		 * the persist timer is set even if the packet
47247a8e865SXin LI 		 * hits one of the "goto send" lines below.
473df8bae1dSRodney W. Grimes 		 */
474df8bae1dSRodney W. Grimes 		len = 0;
47547a8e865SXin LI 		if ((sendwin == 0) && (TCPS_HAVEESTABLISHED(tp->t_state)) &&
47647a8e865SXin LI 			(off < (int) sbavail(&so->so_snd))) {
477b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
4782d8266afSDavid Greenman 			tp->t_rxtshift = 0;
479df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
480b8152ba7SAndre Oppermann 			if (!tcp_timer_active(tp, TT_PERSIST))
4812d8266afSDavid Greenman 				tcp_setpersist(tp);
482df8bae1dSRodney W. Grimes 		}
483df8bae1dSRodney W. Grimes 	}
48428257b5cSMatthew Dillon 
4856741ecf5SAndre Oppermann 	/* len will be >= 0 after this point. */
486c4982faeSBjoern A. Zeeb 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
4876741ecf5SAndre Oppermann 
48828257b5cSMatthew Dillon 	/*
4896741ecf5SAndre Oppermann 	 * Automatic sizing of send socket buffer.  Often the send buffer
4906741ecf5SAndre Oppermann 	 * size is not optimally adjusted to the actual network conditions
4916741ecf5SAndre Oppermann 	 * at hand (delay bandwidth product).  Setting the buffer size too
4926741ecf5SAndre Oppermann 	 * small limits throughput on links with high bandwidth and high
4936741ecf5SAndre Oppermann 	 * delay (eg. trans-continental/oceanic links).  Setting the
4946741ecf5SAndre Oppermann 	 * buffer size too big consumes too much real kernel memory,
4956741ecf5SAndre Oppermann 	 * especially with many connections on busy servers.
4966741ecf5SAndre Oppermann 	 *
4976741ecf5SAndre Oppermann 	 * The criteria to step up the send buffer one notch are:
4986741ecf5SAndre Oppermann 	 *  1. receive window of remote host is larger than send buffer
4996741ecf5SAndre Oppermann 	 *     (with a fudge factor of 5/4th);
5006741ecf5SAndre Oppermann 	 *  2. send buffer is filled to 7/8th with data (so we actually
5016741ecf5SAndre Oppermann 	 *     have data to make use of it);
5026741ecf5SAndre Oppermann 	 *  3. send buffer fill has not hit maximal automatic size;
5036741ecf5SAndre Oppermann 	 *  4. our send window (slow start and cogestion controlled) is
5046741ecf5SAndre Oppermann 	 *     larger than sent but unacknowledged data in send buffer.
5056741ecf5SAndre Oppermann 	 *
5066741ecf5SAndre Oppermann 	 * The remote host receive window scaling factor may limit the
5076741ecf5SAndre Oppermann 	 * growing of the send buffer before it reaches its allowed
5086741ecf5SAndre Oppermann 	 * maximum.
5096741ecf5SAndre Oppermann 	 *
5106741ecf5SAndre Oppermann 	 * It scales directly with slow start or congestion window
5116741ecf5SAndre Oppermann 	 * and does at most one step per received ACK.  This fast
5126741ecf5SAndre Oppermann 	 * scaling has the drawback of growing the send buffer beyond
5136741ecf5SAndre Oppermann 	 * what is strictly necessary to make full use of a given
514a4641f4eSPedro F. Giffuni 	 * delay*bandwidth product.  However testing has shown this not
5156741ecf5SAndre Oppermann 	 * to be much of an problem.  At worst we are trading wasting
516a4641f4eSPedro F. Giffuni 	 * of available bandwidth (the non-use of it) for wasting some
5176741ecf5SAndre Oppermann 	 * socket buffer memory.
5186741ecf5SAndre Oppermann 	 *
5196741ecf5SAndre Oppermann 	 * TODO: Shrink send buffer during idle periods together
5206741ecf5SAndre Oppermann 	 * with congestion window.  Requires another timer.  Has to
5216741ecf5SAndre Oppermann 	 * wait for upcoming tcp timer rewrite.
522cfa6009eSGleb Smirnoff 	 *
523cfa6009eSGleb Smirnoff 	 * XXXGL: should there be used sbused() or sbavail()?
5246741ecf5SAndre Oppermann 	 */
525603724d3SBjoern A. Zeeb 	if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
5266741ecf5SAndre Oppermann 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
527cfa6009eSGleb Smirnoff 		    sbused(&so->so_snd) >= (so->so_snd.sb_hiwat / 8 * 7) &&
528cfa6009eSGleb Smirnoff 		    sbused(&so->so_snd) < V_tcp_autosndbuf_max &&
529cfa6009eSGleb Smirnoff 		    sendwin >= (sbused(&so->so_snd) -
530cfa6009eSGleb Smirnoff 		    (tp->snd_nxt - tp->snd_una))) {
5316741ecf5SAndre Oppermann 			if (!sbreserve_locked(&so->so_snd,
532603724d3SBjoern A. Zeeb 			    min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc,
533603724d3SBjoern A. Zeeb 			     V_tcp_autosndbuf_max), so, curthread))
5346741ecf5SAndre Oppermann 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
5356741ecf5SAndre Oppermann 		}
5366741ecf5SAndre Oppermann 	}
5376741ecf5SAndre Oppermann 
5386741ecf5SAndre Oppermann 	/*
539ed420311SAndre Oppermann 	 * Decide if we can use TCP Segmentation Offloading (if supported by
540ed420311SAndre Oppermann 	 * hardware).
541b3c0f300SAndre Oppermann 	 *
542b3c0f300SAndre Oppermann 	 * TSO may only be used if we are in a pure bulk sending state.  The
543b3c0f300SAndre Oppermann 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and
544b3c0f300SAndre Oppermann 	 * IP options prevent using TSO.  With TSO the TCP header is the same
545b3c0f300SAndre Oppermann 	 * (except for the sequence number) for all generated packets.  This
546b3c0f300SAndre Oppermann 	 * makes it impossible to transmit any options which vary per generated
547b3c0f300SAndre Oppermann 	 * segment or packet.
548b6ff6724SHiren Panchasara 	 *
549b6ff6724SHiren Panchasara 	 * IPv4 handling has a clear separation of ip options and ip header
550b6ff6724SHiren Panchasara 	 * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does
551b6ff6724SHiren Panchasara 	 * the right thing below to provide length of just ip options and thus
552b6ff6724SHiren Panchasara 	 * checking for ipoptlen is enough to decide if ip options are present.
55328257b5cSMatthew Dillon 	 */
5549ad0173dSBjoern A. Zeeb #ifdef IPSEC
5559ad0173dSBjoern A. Zeeb 	/*
5569ad0173dSBjoern A. Zeeb 	 * Pre-calculate here as we save another lookup into the darknesses
5579ad0173dSBjoern A. Zeeb 	 * of IPsec that way and can actually decide if TSO is ok.
5589ad0173dSBjoern A. Zeeb 	 */
5599ad0173dSBjoern A. Zeeb 	ipsec_optlen = ipsec_hdrsiz_tcp(tp);
5609ad0173dSBjoern A. Zeeb #endif
561b6ff6724SHiren Panchasara 
562b6ff6724SHiren Panchasara #ifdef INET6
563b6ff6724SHiren Panchasara 	if (isipv6)
564b6ff6724SHiren Panchasara 		ipoptlen = ip6_optlen(tp->t_inpcb);
565b6ff6724SHiren Panchasara 	else
566b6ff6724SHiren Panchasara #endif
567b6ff6724SHiren Panchasara 	if (tp->t_inpcb->inp_options)
568b6ff6724SHiren Panchasara 		ipoptlen = tp->t_inpcb->inp_options->m_len -
569b6ff6724SHiren Panchasara 				offsetof(struct ipoption, ipopt_list);
570b6ff6724SHiren Panchasara 	else
571b6ff6724SHiren Panchasara 		ipoptlen = 0;
572b6ff6724SHiren Panchasara #ifdef IPSEC
573b6ff6724SHiren Panchasara 	ipoptlen += ipsec_optlen;
574b6ff6724SHiren Panchasara #endif
575b6ff6724SHiren Panchasara 
576ed420311SAndre Oppermann 	if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg &&
577b3c0f300SAndre Oppermann 	    ((tp->t_flags & TF_SIGNATURE) == 0) &&
578b3c0f300SAndre Oppermann 	    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
579b6ff6724SHiren Panchasara 	    ipoptlen == 0)
580b3c0f300SAndre Oppermann 		tso = 1;
581153e5b57SAndre Oppermann 
5825d3b1b75SJayanth Vijayaraghavan 	if (sack_rxmit) {
583cfa6009eSGleb Smirnoff 		if (SEQ_LT(p->rxmit + len, tp->snd_una + sbused(&so->so_snd)))
584df8bae1dSRodney W. Grimes 			flags &= ~TH_FIN;
5855d3b1b75SJayanth Vijayaraghavan 	} else {
586cfa6009eSGleb Smirnoff 		if (SEQ_LT(tp->snd_nxt + len, tp->snd_una +
587cfa6009eSGleb Smirnoff 		    sbused(&so->so_snd)))
5885d3b1b75SJayanth Vijayaraghavan 			flags &= ~TH_FIN;
5895d3b1b75SJayanth Vijayaraghavan 	}
590df8bae1dSRodney W. Grimes 
5913ac12506SJonathan T. Looney 	recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
5923ac12506SJonathan T. Looney 	    (long)TCP_MAXWIN << tp->rcv_scale);
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes 	/*
595262c1c1aSMatthew Dillon 	 * Sender silly window avoidance.   We transmit under the following
596262c1c1aSMatthew Dillon 	 * conditions when len is non-zero:
597262c1c1aSMatthew Dillon 	 *
598b3c0f300SAndre Oppermann 	 *	- We have a full segment (or more with TSO)
599262c1c1aSMatthew Dillon 	 *	- This is the last buffer in a write()/send() and we are
600262c1c1aSMatthew Dillon 	 *	  either idle or running NODELAY
601262c1c1aSMatthew Dillon 	 *	- we've timed out (e.g. persist timer)
602262c1c1aSMatthew Dillon 	 *	- we have more then 1/2 the maximum send window's worth of
603262c1c1aSMatthew Dillon 	 *	  data (receiver may be limited the window size)
604262c1c1aSMatthew Dillon 	 *	- we need to retransmit
605df8bae1dSRodney W. Grimes 	 */
606df8bae1dSRodney W. Grimes 	if (len) {
607b3c0f300SAndre Oppermann 		if (len >= tp->t_maxseg)
608df8bae1dSRodney W. Grimes 			goto send;
609262c1c1aSMatthew Dillon 		/*
610262c1c1aSMatthew Dillon 		 * NOTE! on localhost connections an 'ack' from the remote
611262c1c1aSMatthew Dillon 		 * end may occur synchronously with the output and cause
612262c1c1aSMatthew Dillon 		 * us to flush a buffer queued with moretocome.  XXX
613262c1c1aSMatthew Dillon 		 *
614262c1c1aSMatthew Dillon 		 * note: the len + off check is almost certainly unnecessary.
615262c1c1aSMatthew Dillon 		 */
616262c1c1aSMatthew Dillon 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
617262c1c1aSMatthew Dillon 		    (idle || (tp->t_flags & TF_NODELAY)) &&
6183ac12506SJonathan T. Looney 		    (uint32_t)len + (uint32_t)off >= sbavail(&so->so_snd) &&
619262c1c1aSMatthew Dillon 		    (tp->t_flags & TF_NOPUSH) == 0) {
620df8bae1dSRodney W. Grimes 			goto send;
621262c1c1aSMatthew Dillon 		}
6222cdbfa66SPaul Saab 		if (tp->t_flags & TF_FORCEDATA)		/* typ. timeout case */
623df8bae1dSRodney W. Grimes 			goto send;
624a0292f23SGarrett Wollman 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
625df8bae1dSRodney W. Grimes 			goto send;
626262c1c1aSMatthew Dillon 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))	/* retransmit case */
627df8bae1dSRodney W. Grimes 			goto send;
6286d90faf3SPaul Saab 		if (sack_rxmit)
6296d90faf3SPaul Saab 			goto send;
630df8bae1dSRodney W. Grimes 	}
631df8bae1dSRodney W. Grimes 
632df8bae1dSRodney W. Grimes 	/*
633f62563d3SAndre Oppermann 	 * Sending of standalone window updates.
634f62563d3SAndre Oppermann 	 *
63578f59b4bSAndre Oppermann 	 * Window updates are important when we close our window due to a
63678f59b4bSAndre Oppermann 	 * full socket buffer and are opening it again after the application
637f62563d3SAndre Oppermann 	 * reads data from it.  Once the window has opened again and the
638f62563d3SAndre Oppermann 	 * remote end starts to send again the ACK clock takes over and
639f62563d3SAndre Oppermann 	 * provides the most current window information.
640f62563d3SAndre Oppermann 	 *
64178f59b4bSAndre Oppermann 	 * We must avoid the silly window syndrome whereas every read
642f62563d3SAndre Oppermann 	 * from the receive buffer, no matter how small, causes a window
643f62563d3SAndre Oppermann 	 * update to be sent.  We also should avoid sending a flurry of
644f62563d3SAndre Oppermann 	 * window updates when the socket buffer had queued a lot of data
645f62563d3SAndre Oppermann 	 * and the application is doing small reads.
646f62563d3SAndre Oppermann 	 *
647f62563d3SAndre Oppermann 	 * Prevent a flurry of pointless window updates by only sending
648f62563d3SAndre Oppermann 	 * an update when we can increase the advertized window by more
649f62563d3SAndre Oppermann 	 * than 1/4th of the socket buffer capacity.  When the buffer is
650f62563d3SAndre Oppermann 	 * getting full or is very small be more aggressive and send an
651f62563d3SAndre Oppermann 	 * update whenever we can increase by two mss sized segments.
652f62563d3SAndre Oppermann 	 * In all other situations the ACK's to new incoming data will
653f62563d3SAndre Oppermann 	 * carry further window increases.
6544249614cSAndre Oppermann 	 *
6554249614cSAndre Oppermann 	 * Don't send an independent window update if a delayed
6564249614cSAndre Oppermann 	 * ACK is pending (it will get piggy-backed on it) or the
6574249614cSAndre Oppermann 	 * remote side already has done a half-close and won't send
658f62563d3SAndre Oppermann 	 * more data.  Skip this if the connection is in T/TCP
659f62563d3SAndre Oppermann 	 * half-open state.
660df8bae1dSRodney W. Grimes 	 */
661b7de7d87SAndre Oppermann 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
6624249614cSAndre Oppermann 	    !(tp->t_flags & TF_DELACK) &&
663b7de7d87SAndre Oppermann 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
664df8bae1dSRodney W. Grimes 		/*
665f62563d3SAndre Oppermann 		 * "adv" is the amount we could increase the window,
666df8bae1dSRodney W. Grimes 		 * taking into account that we are limited by
667df8bae1dSRodney W. Grimes 		 * TCP_MAXWIN << tp->rcv_scale.
668df8bae1dSRodney W. Grimes 		 */
6693ac12506SJonathan T. Looney 		int32_t adv;
670f701e30dSJohn Baldwin 		int oldwin;
671f701e30dSJohn Baldwin 
6723ac12506SJonathan T. Looney 		adv = recwin;
673f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) {
674f701e30dSJohn Baldwin 			oldwin = (tp->rcv_adv - tp->rcv_nxt);
675f701e30dSJohn Baldwin 			adv -= oldwin;
676f701e30dSJohn Baldwin 		} else
677f701e30dSJohn Baldwin 			oldwin = 0;
678df8bae1dSRodney W. Grimes 
679da84b2e6SJohn Baldwin 		/*
6800dda76b8SJonathan T. Looney 		 * If the new window size ends up being the same as or less
6810dda76b8SJonathan T. Looney 		 * than the old size when it is scaled, then don't force
6820dda76b8SJonathan T. Looney 		 * a window update.
683da84b2e6SJohn Baldwin 		 */
6840dda76b8SJonathan T. Looney 		if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale)
685da84b2e6SJohn Baldwin 			goto dontupdate;
686f62563d3SAndre Oppermann 
6873ac12506SJonathan T. Looney 		if (adv >= (int32_t)(2 * tp->t_maxseg) &&
6883ac12506SJonathan T. Looney 		    (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) ||
6893ac12506SJonathan T. Looney 		     recwin <= (so->so_rcv.sb_hiwat / 8) ||
690f62563d3SAndre Oppermann 		     so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg))
691df8bae1dSRodney W. Grimes 			goto send;
692df8bae1dSRodney W. Grimes 	}
693da84b2e6SJohn Baldwin dontupdate:
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes 	/*
69628257b5cSMatthew Dillon 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
69728257b5cSMatthew Dillon 	 * is also a catch-all for the retransmit timer timeout case.
698df8bae1dSRodney W. Grimes 	 */
699df8bae1dSRodney W. Grimes 	if (tp->t_flags & TF_ACKNOW)
700df8bae1dSRodney W. Grimes 		goto send;
701a0292f23SGarrett Wollman 	if ((flags & TH_RST) ||
702a0292f23SGarrett Wollman 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
703df8bae1dSRodney W. Grimes 		goto send;
704df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_una))
705df8bae1dSRodney W. Grimes 		goto send;
706df8bae1dSRodney W. Grimes 	/*
707df8bae1dSRodney W. Grimes 	 * If our state indicates that FIN should be sent
70828257b5cSMatthew Dillon 	 * and we have not yet done so, then we need to send.
709df8bae1dSRodney W. Grimes 	 */
710df8bae1dSRodney W. Grimes 	if (flags & TH_FIN &&
711df8bae1dSRodney W. Grimes 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
712df8bae1dSRodney W. Grimes 		goto send;
7136d90faf3SPaul Saab 	/*
7146d90faf3SPaul Saab 	 * In SACK, it is possible for tcp_output to fail to send a segment
7156d90faf3SPaul Saab 	 * after the retransmission timer has been turned off.  Make sure
7166d90faf3SPaul Saab 	 * that the retransmission timer is set.
7176d90faf3SPaul Saab 	 */
7183529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
7193529149eSAndre Oppermann 	    SEQ_GT(tp->snd_max, tp->snd_una) &&
720b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_REXMT) &&
721b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
722b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
723cf2942b6SRobert Watson 		goto just_return;
7246d90faf3SPaul Saab 	}
725df8bae1dSRodney W. Grimes 	/*
726df8bae1dSRodney W. Grimes 	 * TCP window updates are not reliable, rather a polling protocol
727df8bae1dSRodney W. Grimes 	 * using ``persist'' packets is used to insure receipt of window
728df8bae1dSRodney W. Grimes 	 * updates.  The three ``states'' for the output side are:
729df8bae1dSRodney W. Grimes 	 *	idle			not doing retransmits or persists
730df8bae1dSRodney W. Grimes 	 *	persisting		to move a small or zero window
731df8bae1dSRodney W. Grimes 	 *	(re)transmitting	and thereby not persisting
732df8bae1dSRodney W. Grimes 	 *
733b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_PERSIST)
7349b8b58e0SJonathan Lemon 	 *	is true when we are in persist state.
7352cdbfa66SPaul Saab 	 * (tp->t_flags & TF_FORCEDATA)
736df8bae1dSRodney W. Grimes 	 *	is set when we are called to send a persist packet.
737b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_REXMT)
738df8bae1dSRodney W. Grimes 	 *	is set when we are retransmitting
739df8bae1dSRodney W. Grimes 	 * The output side is idle when both timers are zero.
740df8bae1dSRodney W. Grimes 	 *
741df8bae1dSRodney W. Grimes 	 * If send window is too small, there is data to transmit, and no
742df8bae1dSRodney W. Grimes 	 * retransmit or persist is pending, then go to persist state.
743df8bae1dSRodney W. Grimes 	 * If nothing happens soon, send when timer expires:
744df8bae1dSRodney W. Grimes 	 * if window is nonzero, transmit what we can,
745df8bae1dSRodney W. Grimes 	 * otherwise force out a byte.
746df8bae1dSRodney W. Grimes 	 */
747cfa6009eSGleb Smirnoff 	if (sbavail(&so->so_snd) && !tcp_timer_active(tp, TT_REXMT) &&
748b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
749df8bae1dSRodney W. Grimes 		tp->t_rxtshift = 0;
750df8bae1dSRodney W. Grimes 		tcp_setpersist(tp);
751df8bae1dSRodney W. Grimes 	}
752df8bae1dSRodney W. Grimes 
753df8bae1dSRodney W. Grimes 	/*
754df8bae1dSRodney W. Grimes 	 * No reason to send a segment, just return.
755df8bae1dSRodney W. Grimes 	 */
756cf2942b6SRobert Watson just_return:
757cf2942b6SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
758df8bae1dSRodney W. Grimes 	return (0);
759df8bae1dSRodney W. Grimes 
760df8bae1dSRodney W. Grimes send:
761cf2942b6SRobert Watson 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
762f6f6703fSSean Bruno 	if (len > 0) {
763f6f6703fSSean Bruno 		if (len >= tp->t_maxseg)
764f6f6703fSSean Bruno 			tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT;
765f6f6703fSSean Bruno 		else
766f6f6703fSSean Bruno 			tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT;
767f6f6703fSSean Bruno 	}
768df8bae1dSRodney W. Grimes 	/*
769df8bae1dSRodney W. Grimes 	 * Before ESTABLISHED, force sending of initial options
770df8bae1dSRodney W. Grimes 	 * unless TCP set not to do any options.
771df8bae1dSRodney W. Grimes 	 * NOTE: we assume that the IP/TCP header plus TCP options
772df8bae1dSRodney W. Grimes 	 * always fit in a single mbuf, leaving room for a maximum
773df8bae1dSRodney W. Grimes 	 * link header, i.e.
77433841545SHajimu UMEMOTO 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
775df8bae1dSRodney W. Grimes 	 */
776df8bae1dSRodney W. Grimes 	optlen = 0;
777fb59c426SYoshinobu Inoue #ifdef INET6
778fb59c426SYoshinobu Inoue 	if (isipv6)
779fb59c426SYoshinobu Inoue 		hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
780fb59c426SYoshinobu Inoue 	else
781fb59c426SYoshinobu Inoue #endif
782df8bae1dSRodney W. Grimes 		hdrlen = sizeof (struct tcpiphdr);
78302a1a643SAndre Oppermann 
78402a1a643SAndre Oppermann 	/*
78502a1a643SAndre Oppermann 	 * Compute options for segment.
78602a1a643SAndre Oppermann 	 * We only have to care about SYN and established connection
78702a1a643SAndre Oppermann 	 * segments.  Options for SYN-ACK segments are handled in TCP
78802a1a643SAndre Oppermann 	 * syncache.
78902a1a643SAndre Oppermann 	 */
79002a1a643SAndre Oppermann 	to.to_flags = 0;
791f73d9fd2SGleb Smirnoff 	if ((tp->t_flags & TF_NOOPT) == 0) {
79202a1a643SAndre Oppermann 		/* Maximum segment size. */
793df8bae1dSRodney W. Grimes 		if (flags & TH_SYN) {
794df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->iss;
79502a1a643SAndre Oppermann 			to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc);
79602a1a643SAndre Oppermann 			to.to_flags |= TOF_MSS;
797281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
798281a0fd4SPatrick Kelsey 			/*
799281a0fd4SPatrick Kelsey 			 * Only include the TFO option on the first
800281a0fd4SPatrick Kelsey 			 * transmission of the SYN|ACK on a
801281a0fd4SPatrick Kelsey 			 * passively-created TFO socket, as the presence of
802281a0fd4SPatrick Kelsey 			 * the TFO option may have caused the original
803281a0fd4SPatrick Kelsey 			 * SYN|ACK to have been dropped by a middlebox.
804281a0fd4SPatrick Kelsey 			 */
80568bd7ed1SJonathan T. Looney 			if (IS_FASTOPEN(tp->t_flags) &&
806281a0fd4SPatrick Kelsey 			    (tp->t_state == TCPS_SYN_RECEIVED) &&
807281a0fd4SPatrick Kelsey 			    (tp->t_rxtshift == 0)) {
808281a0fd4SPatrick Kelsey 				to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
809281a0fd4SPatrick Kelsey 				to.to_tfo_cookie = (u_char *)&tp->t_tfo_cookie;
810281a0fd4SPatrick Kelsey 				to.to_flags |= TOF_FASTOPEN;
811281a0fd4SPatrick Kelsey 			}
812281a0fd4SPatrick Kelsey #endif
813df8bae1dSRodney W. Grimes 		}
81402a1a643SAndre Oppermann 		/* Window scaling. */
81502a1a643SAndre Oppermann 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
81602a1a643SAndre Oppermann 			to.to_wscale = tp->request_r_scale;
81702a1a643SAndre Oppermann 			to.to_flags |= TOF_SCALE;
818df8bae1dSRodney W. Grimes 		}
81902a1a643SAndre Oppermann 		/* Timestamps. */
82002a1a643SAndre Oppermann 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
82102a1a643SAndre Oppermann 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
822d8951c8aSBjoern A. Zeeb 			to.to_tsval = tcp_ts_getticks() + tp->ts_offset;
82302a1a643SAndre Oppermann 			to.to_tsecr = tp->ts_recent;
82402a1a643SAndre Oppermann 			to.to_flags |= TOF_TS;
8256741ecf5SAndre Oppermann 			/* Set receive buffer autosizing timestamp. */
82602a1a643SAndre Oppermann 			if (tp->rfbuf_ts == 0 &&
82702a1a643SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE))
828d8951c8aSBjoern A. Zeeb 				tp->rfbuf_ts = tcp_ts_getticks();
8291cfd4b53SBruce M Simpson 		}
83002a1a643SAndre Oppermann 		/* Selective ACK's. */
8313529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
83202a1a643SAndre Oppermann 			if (flags & TH_SYN)
83302a1a643SAndre Oppermann 				to.to_flags |= TOF_SACKPERM;
83402a1a643SAndre Oppermann 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
83502a1a643SAndre Oppermann 			    (tp->t_flags & TF_SACK_PERMIT) &&
83602a1a643SAndre Oppermann 			    tp->rcv_numsacks > 0) {
83702a1a643SAndre Oppermann 				to.to_flags |= TOF_SACK;
83802a1a643SAndre Oppermann 				to.to_nsacks = tp->rcv_numsacks;
83902a1a643SAndre Oppermann 				to.to_sacks = (u_char *)tp->sackblks;
84002a1a643SAndre Oppermann 			}
84102a1a643SAndre Oppermann 		}
84202a1a643SAndre Oppermann #ifdef TCP_SIGNATURE
84302a1a643SAndre Oppermann 		/* TCP-MD5 (RFC2385). */
84402a1a643SAndre Oppermann 		if (tp->t_flags & TF_SIGNATURE)
84502a1a643SAndre Oppermann 			to.to_flags |= TOF_SIGNATURE;
8461cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
8471cfd4b53SBruce M Simpson 
84802a1a643SAndre Oppermann 		/* Processing the options. */
8494a411b9fSBjoern A. Zeeb 		hdrlen += optlen = tcp_addoptions(&to, opt);
850be3f3b5eSPaul Saab 	}
851be3f3b5eSPaul Saab 
852df8bae1dSRodney W. Grimes 	/*
853df8bae1dSRodney W. Grimes 	 * Adjust data length if insertion of options will
8540c39d38dSGleb Smirnoff 	 * bump the packet length beyond the t_maxseg length.
855a0292f23SGarrett Wollman 	 * Clear the FIN bit because we cut off the tail of
856a0292f23SGarrett Wollman 	 * the segment.
857df8bae1dSRodney W. Grimes 	 */
8580c39d38dSGleb Smirnoff 	if (len + optlen + ipoptlen > tp->t_maxseg) {
859297a37f3SDavid Greenman 		flags &= ~TH_FIN;
860ed420311SAndre Oppermann 
861b3c0f300SAndre Oppermann 		if (tso) {
8629fd573c3SHans Petter Selasky 			u_int if_hw_tsomax;
8639fd573c3SHans Petter Selasky 			u_int if_hw_tsomaxsegcount;
8649fd573c3SHans Petter Selasky 			u_int if_hw_tsomaxsegsize;
8659fd573c3SHans Petter Selasky 			struct mbuf *mb;
8669fd573c3SHans Petter Selasky 			u_int moff;
8679fd573c3SHans Petter Selasky 			int max_len;
8689fd573c3SHans Petter Selasky 
8699fd573c3SHans Petter Selasky 			/* extract TSO information */
8709fd573c3SHans Petter Selasky 			if_hw_tsomax = tp->t_tsomax;
8719fd573c3SHans Petter Selasky 			if_hw_tsomaxsegcount = tp->t_tsomaxsegcount;
8729fd573c3SHans Petter Selasky 			if_hw_tsomaxsegsize = tp->t_tsomaxsegsize;
8739fd573c3SHans Petter Selasky 
8749fd573c3SHans Petter Selasky 			/*
8759fd573c3SHans Petter Selasky 			 * Limit a TSO burst to prevent it from
8769fd573c3SHans Petter Selasky 			 * overflowing or exceeding the maximum length
8779fd573c3SHans Petter Selasky 			 * allowed by the network interface:
8789fd573c3SHans Petter Selasky 			 */
879ed420311SAndre Oppermann 			KASSERT(ipoptlen == 0,
880ed420311SAndre Oppermann 			    ("%s: TSO can't do IP options", __func__));
881ed420311SAndre Oppermann 
882ed420311SAndre Oppermann 			/*
8839fd573c3SHans Petter Selasky 			 * Check if we should limit by maximum payload
8849fd573c3SHans Petter Selasky 			 * length:
885ed420311SAndre Oppermann 			 */
8869fd573c3SHans Petter Selasky 			if (if_hw_tsomax != 0) {
8879fd573c3SHans Petter Selasky 				/* compute maximum TSO length */
888d76d4012SHans Petter Selasky 				max_len = (if_hw_tsomax - hdrlen -
889d76d4012SHans Petter Selasky 				    max_linkhdr);
8909fd573c3SHans Petter Selasky 				if (max_len <= 0) {
8919fd573c3SHans Petter Selasky 					len = 0;
8923c7c188cSHans Petter Selasky 				} else if (len > max_len) {
893b3c0f300SAndre Oppermann 					sendalot = 1;
8943c7c188cSHans Petter Selasky 					len = max_len;
8959fd573c3SHans Petter Selasky 				}
8969fd573c3SHans Petter Selasky 			}
8979fd573c3SHans Petter Selasky 
8989fd573c3SHans Petter Selasky 			/*
8999fd573c3SHans Petter Selasky 			 * Check if we should limit by maximum segment
9009fd573c3SHans Petter Selasky 			 * size and count:
9019fd573c3SHans Petter Selasky 			 */
902b228e6bfSHans Petter Selasky 			if (if_hw_tsomaxsegcount != 0 &&
903b228e6bfSHans Petter Selasky 			    if_hw_tsomaxsegsize != 0) {
904d76d4012SHans Petter Selasky 				/*
905d76d4012SHans Petter Selasky 				 * Subtract one segment for the LINK
906d76d4012SHans Petter Selasky 				 * and TCP/IP headers mbuf that will
907d76d4012SHans Petter Selasky 				 * be prepended to this mbuf chain
908d76d4012SHans Petter Selasky 				 * after the code in this section
909d76d4012SHans Petter Selasky 				 * limits the number of mbufs in the
910d76d4012SHans Petter Selasky 				 * chain to if_hw_tsomaxsegcount.
911d76d4012SHans Petter Selasky 				 */
912d76d4012SHans Petter Selasky 				if_hw_tsomaxsegcount -= 1;
9139fd573c3SHans Petter Selasky 				max_len = 0;
9149fd573c3SHans Petter Selasky 				mb = sbsndmbuf(&so->so_snd, off, &moff);
9159fd573c3SHans Petter Selasky 
9163c7c188cSHans Petter Selasky 				while (mb != NULL && max_len < len) {
917b228e6bfSHans Petter Selasky 					u_int mlen;
918b228e6bfSHans Petter Selasky 					u_int frags;
9199fd573c3SHans Petter Selasky 
9209fd573c3SHans Petter Selasky 					/*
9219fd573c3SHans Petter Selasky 					 * Get length of mbuf fragment
922b228e6bfSHans Petter Selasky 					 * and how many hardware frags,
923b228e6bfSHans Petter Selasky 					 * rounded up, it would use:
9249fd573c3SHans Petter Selasky 					 */
925b228e6bfSHans Petter Selasky 					mlen = (mb->m_len - moff);
926b228e6bfSHans Petter Selasky 					frags = howmany(mlen,
927b228e6bfSHans Petter Selasky 					    if_hw_tsomaxsegsize);
9289fd573c3SHans Petter Selasky 
9299fd573c3SHans Petter Selasky 					/* Handle special case: Zero Length Mbuf */
930b228e6bfSHans Petter Selasky 					if (frags == 0)
931b228e6bfSHans Petter Selasky 						frags = 1;
9329fd573c3SHans Petter Selasky 
9339fd573c3SHans Petter Selasky 					/*
9349fd573c3SHans Petter Selasky 					 * Check if the fragment limit
935b228e6bfSHans Petter Selasky 					 * will be reached or exceeded:
9369fd573c3SHans Petter Selasky 					 */
937b228e6bfSHans Petter Selasky 					if (frags >= if_hw_tsomaxsegcount) {
938b228e6bfSHans Petter Selasky 						max_len += min(mlen,
9399fd573c3SHans Petter Selasky 						    if_hw_tsomaxsegcount *
9409fd573c3SHans Petter Selasky 						    if_hw_tsomaxsegsize);
9419fd573c3SHans Petter Selasky 						break;
9429fd573c3SHans Petter Selasky 					}
943b228e6bfSHans Petter Selasky 					max_len += mlen;
944b228e6bfSHans Petter Selasky 					if_hw_tsomaxsegcount -= frags;
9459fd573c3SHans Petter Selasky 					moff = 0;
9469fd573c3SHans Petter Selasky 					mb = mb->m_next;
9479fd573c3SHans Petter Selasky 				}
9489fd573c3SHans Petter Selasky 				if (max_len <= 0) {
9499fd573c3SHans Petter Selasky 					len = 0;
9503c7c188cSHans Petter Selasky 				} else if (len > max_len) {
9519fd573c3SHans Petter Selasky 					sendalot = 1;
9523c7c188cSHans Petter Selasky 					len = max_len;
9539fd573c3SHans Petter Selasky 				}
954ed420311SAndre Oppermann 			}
955ed420311SAndre Oppermann 
956ed420311SAndre Oppermann 			/*
957ed420311SAndre Oppermann 			 * Prevent the last segment from being
9589fd573c3SHans Petter Selasky 			 * fractional unless the send sockbuf can be
9599fd573c3SHans Petter Selasky 			 * emptied:
960ed420311SAndre Oppermann 			 */
9610c39d38dSGleb Smirnoff 			max_len = (tp->t_maxseg - optlen);
9623ac12506SJonathan T. Looney 			if (((uint32_t)off + (uint32_t)len) <
9633ac12506SJonathan T. Looney 			    sbavail(&so->so_snd)) {
9643c7c188cSHans Petter Selasky 				moff = len % max_len;
9659fd573c3SHans Petter Selasky 				if (moff != 0) {
9669fd573c3SHans Petter Selasky 					len -= moff;
967b3c0f300SAndre Oppermann 					sendalot = 1;
968ed420311SAndre Oppermann 				}
9699fd573c3SHans Petter Selasky 			}
9709fd573c3SHans Petter Selasky 
9719fd573c3SHans Petter Selasky 			/*
9729fd573c3SHans Petter Selasky 			 * In case there are too many small fragments
9739fd573c3SHans Petter Selasky 			 * don't use TSO:
9749fd573c3SHans Petter Selasky 			 */
9753c7c188cSHans Petter Selasky 			if (len <= max_len) {
9763c7c188cSHans Petter Selasky 				len = max_len;
9779fd573c3SHans Petter Selasky 				sendalot = 1;
9789fd573c3SHans Petter Selasky 				tso = 0;
9799fd573c3SHans Petter Selasky 			}
980ed420311SAndre Oppermann 
981ed420311SAndre Oppermann 			/*
982ed420311SAndre Oppermann 			 * Send the FIN in a separate segment
983ed420311SAndre Oppermann 			 * after the bulk sending is done.
984ed420311SAndre Oppermann 			 * We don't trust the TSO implementations
985ed420311SAndre Oppermann 			 * to clear the FIN flag on all but the
986ed420311SAndre Oppermann 			 * last segment.
987ed420311SAndre Oppermann 			 */
988ed420311SAndre Oppermann 			if (tp->t_flags & TF_NEEDFIN)
989ed420311SAndre Oppermann 				sendalot = 1;
990ed420311SAndre Oppermann 
991b3c0f300SAndre Oppermann 		} else {
9920c39d38dSGleb Smirnoff 			len = tp->t_maxseg - optlen - ipoptlen;
993df8bae1dSRodney W. Grimes 			sendalot = 1;
994df8bae1dSRodney W. Grimes 		}
995ed420311SAndre Oppermann 	} else
996ed420311SAndre Oppermann 		tso = 0;
997ed420311SAndre Oppermann 
998ed420311SAndre Oppermann 	KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
999ed420311SAndre Oppermann 	    ("%s: len > IP_MAXPACKET", __func__));
1000df8bae1dSRodney W. Grimes 
1001a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/
1002a683a7ddSYoshinobu Inoue #ifdef INET6
1003a683a7ddSYoshinobu Inoue 	if (max_linkhdr + hdrlen > MCLBYTES)
1004a683a7ddSYoshinobu Inoue #else
1005df8bae1dSRodney W. Grimes 	if (max_linkhdr + hdrlen > MHLEN)
1006a683a7ddSYoshinobu Inoue #endif
1007f10e85d7SLuigi Rizzo 		panic("tcphdr too big");
1008a0292f23SGarrett Wollman /*#endif*/
1009df8bae1dSRodney W. Grimes 
1010df8bae1dSRodney W. Grimes 	/*
1011c4982faeSBjoern A. Zeeb 	 * This KASSERT is here to catch edge cases at a well defined place.
1012c4982faeSBjoern A. Zeeb 	 * Before, those had triggered (random) panic conditions further down.
1013c4982faeSBjoern A. Zeeb 	 */
1014c4982faeSBjoern A. Zeeb 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
1015c4982faeSBjoern A. Zeeb 
1016c4982faeSBjoern A. Zeeb 	/*
1017df8bae1dSRodney W. Grimes 	 * Grab a header mbuf, attaching a copy of data to
1018df8bae1dSRodney W. Grimes 	 * be transmitted, and initialize the header from
1019df8bae1dSRodney W. Grimes 	 * the template for sends on this connection.
1020df8bae1dSRodney W. Grimes 	 */
1021df8bae1dSRodney W. Grimes 	if (len) {
10224e023759SAndre Oppermann 		struct mbuf *mb;
10234e023759SAndre Oppermann 		u_int moff;
10244e023759SAndre Oppermann 
10252cdbfa66SPaul Saab 		if ((tp->t_flags & TF_FORCEDATA) && len == 1)
102678b50714SRobert Watson 			TCPSTAT_INC(tcps_sndprobe);
10270ba5d2eeSJohn Baldwin 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
1028f5d34df5SGeorge V. Neville-Neil 			tp->t_sndrexmitpack++;
102978b50714SRobert Watson 			TCPSTAT_INC(tcps_sndrexmitpack);
103078b50714SRobert Watson 			TCPSTAT_ADD(tcps_sndrexmitbyte, len);
1031df8bae1dSRodney W. Grimes 		} else {
103278b50714SRobert Watson 			TCPSTAT_INC(tcps_sndpack);
103378b50714SRobert Watson 			TCPSTAT_ADD(tcps_sndbyte, len);
1034df8bae1dSRodney W. Grimes 		}
103539f6074eSGleb Smirnoff #ifdef INET6
103639f6074eSGleb Smirnoff 		if (MHLEN < hdrlen + max_linkhdr)
103739f6074eSGleb Smirnoff 			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
103839f6074eSGleb Smirnoff 		else
103939f6074eSGleb Smirnoff #endif
104039f6074eSGleb Smirnoff 			m = m_gethdr(M_NOWAIT, MT_DATA);
104139f6074eSGleb Smirnoff 
1042df8bae1dSRodney W. Grimes 		if (m == NULL) {
1043cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
1044df8bae1dSRodney W. Grimes 			error = ENOBUFS;
10450e2bc05cSGleb Smirnoff 			sack_rxmit = 0;
1046df8bae1dSRodney W. Grimes 			goto out;
1047df8bae1dSRodney W. Grimes 		}
104839f6074eSGleb Smirnoff 
1049df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
1050df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
10514e023759SAndre Oppermann 
10524e023759SAndre Oppermann 		/*
10534e023759SAndre Oppermann 		 * Start the m_copy functions from the closest mbuf
10544e023759SAndre Oppermann 		 * to the offset in the socket buffer chain.
10554e023759SAndre Oppermann 		 */
10564e023759SAndre Oppermann 		mb = sbsndptr(&so->so_snd, off, len, &moff);
10574e023759SAndre Oppermann 
1058df8bae1dSRodney W. Grimes 		if (len <= MHLEN - hdrlen - max_linkhdr) {
10593ac12506SJonathan T. Looney 			m_copydata(mb, moff, len,
1060df8bae1dSRodney W. Grimes 			    mtod(m, caddr_t) + hdrlen);
1061df8bae1dSRodney W. Grimes 			m->m_len += len;
1062df8bae1dSRodney W. Grimes 		} else {
1063c3bef61eSKevin Lo 			m->m_next = m_copym(mb, moff, len, M_NOWAIT);
10644e023759SAndre Oppermann 			if (m->m_next == NULL) {
1065cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
1066d7f570e6SGarrett Wollman 				(void) m_free(m);
106751823c3aSGarrett Wollman 				error = ENOBUFS;
10680e2bc05cSGleb Smirnoff 				sack_rxmit = 0;
106951823c3aSGarrett Wollman 				goto out;
107051823c3aSGarrett Wollman 			}
1071df8bae1dSRodney W. Grimes 		}
1072472ea5beSColin Percival 
1073df8bae1dSRodney W. Grimes 		/*
1074df8bae1dSRodney W. Grimes 		 * If we're sending everything we've got, set PUSH.
1075df8bae1dSRodney W. Grimes 		 * (This will keep happy those implementations which only
1076df8bae1dSRodney W. Grimes 		 * give data to the user when a buffer fills or
1077df8bae1dSRodney W. Grimes 		 * a PUSH comes in.)
1078df8bae1dSRodney W. Grimes 		 */
10793ac12506SJonathan T. Looney 		if (((uint32_t)off + (uint32_t)len == sbused(&so->so_snd)) &&
10803ac12506SJonathan T. Looney 		    !(flags & TH_SYN))
1081df8bae1dSRodney W. Grimes 			flags |= TH_PUSH;
1082cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1083df8bae1dSRodney W. Grimes 	} else {
1084cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1085df8bae1dSRodney W. Grimes 		if (tp->t_flags & TF_ACKNOW)
108678b50714SRobert Watson 			TCPSTAT_INC(tcps_sndacks);
1087df8bae1dSRodney W. Grimes 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
108878b50714SRobert Watson 			TCPSTAT_INC(tcps_sndctrl);
1089df8bae1dSRodney W. Grimes 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
109078b50714SRobert Watson 			TCPSTAT_INC(tcps_sndurg);
1091df8bae1dSRodney W. Grimes 		else
109278b50714SRobert Watson 			TCPSTAT_INC(tcps_sndwinup);
1093df8bae1dSRodney W. Grimes 
1094aa8bd99dSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
1095df8bae1dSRodney W. Grimes 		if (m == NULL) {
1096df8bae1dSRodney W. Grimes 			error = ENOBUFS;
10970e2bc05cSGleb Smirnoff 			sack_rxmit = 0;
1098df8bae1dSRodney W. Grimes 			goto out;
1099df8bae1dSRodney W. Grimes 		}
1100fb59c426SYoshinobu Inoue #ifdef INET6
1101fb59c426SYoshinobu Inoue 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
1102fb59c426SYoshinobu Inoue 		    MHLEN >= hdrlen) {
1103ed6a66caSRobert Watson 			M_ALIGN(m, hdrlen);
1104fb59c426SYoshinobu Inoue 		} else
1105fb59c426SYoshinobu Inoue #endif
1106df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
1107df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
1108df8bae1dSRodney W. Grimes 	}
1109cf2942b6SRobert Watson 	SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
1110df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = (struct ifnet *)0;
1111c488362eSRobert Watson #ifdef MAC
111230d239bcSRobert Watson 	mac_inpcb_create_mbuf(tp->t_inpcb, m);
1113c488362eSRobert Watson #endif
1114fb59c426SYoshinobu Inoue #ifdef INET6
1115fb59c426SYoshinobu Inoue 	if (isipv6) {
1116fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
1117fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip6 + 1);
111879909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip6, th);
1119fb59c426SYoshinobu Inoue 	} else
1120fb59c426SYoshinobu Inoue #endif /* INET6 */
1121fb59c426SYoshinobu Inoue 	{
1122fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
1123fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
1124fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip + 1);
112579909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip, th);
1126fb59c426SYoshinobu Inoue 	}
1127df8bae1dSRodney W. Grimes 
1128df8bae1dSRodney W. Grimes 	/*
1129df8bae1dSRodney W. Grimes 	 * Fill in fields, remembering maximum advertised
1130df8bae1dSRodney W. Grimes 	 * window for use in delaying messages about window sizes.
1131df8bae1dSRodney W. Grimes 	 * If resending a FIN, be sure not to use a new sequence number.
1132df8bae1dSRodney W. Grimes 	 */
1133df8bae1dSRodney W. Grimes 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
1134df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max)
1135df8bae1dSRodney W. Grimes 		tp->snd_nxt--;
1136df8bae1dSRodney W. Grimes 	/*
1137f2512ba1SRui Paulo 	 * If we are starting a connection, send ECN setup
1138f2512ba1SRui Paulo 	 * SYN packet. If we are on a retransmit, we may
1139f2512ba1SRui Paulo 	 * resend those bits a number of times as per
1140f2512ba1SRui Paulo 	 * RFC 3168.
1141f2512ba1SRui Paulo 	 */
1142883054b4SDon Lewis 	if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn == 1) {
1143f2512ba1SRui Paulo 		if (tp->t_rxtshift >= 1) {
1144603724d3SBjoern A. Zeeb 			if (tp->t_rxtshift <= V_tcp_ecn_maxretries)
1145f2512ba1SRui Paulo 				flags |= TH_ECE|TH_CWR;
1146f2512ba1SRui Paulo 		} else
1147f2512ba1SRui Paulo 			flags |= TH_ECE|TH_CWR;
1148f2512ba1SRui Paulo 	}
1149f2512ba1SRui Paulo 
1150f2512ba1SRui Paulo 	if (tp->t_state == TCPS_ESTABLISHED &&
1151f2512ba1SRui Paulo 	    (tp->t_flags & TF_ECN_PERMIT)) {
1152f2512ba1SRui Paulo 		/*
1153f2512ba1SRui Paulo 		 * If the peer has ECN, mark data packets with
1154f2512ba1SRui Paulo 		 * ECN capable transmission (ECT).
1155f2512ba1SRui Paulo 		 * Ignore pure ack packets, retransmissions and window probes.
1156f2512ba1SRui Paulo 		 */
1157f2512ba1SRui Paulo 		if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
1158f2512ba1SRui Paulo 		    !((tp->t_flags & TF_FORCEDATA) && len == 1)) {
1159f2512ba1SRui Paulo #ifdef INET6
1160f2512ba1SRui Paulo 			if (isipv6)
1161f2512ba1SRui Paulo 				ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
1162f2512ba1SRui Paulo 			else
1163f2512ba1SRui Paulo #endif
1164f2512ba1SRui Paulo 				ip->ip_tos |= IPTOS_ECN_ECT0;
116578b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1166f2512ba1SRui Paulo 		}
1167f2512ba1SRui Paulo 
1168f2512ba1SRui Paulo 		/*
1169f2512ba1SRui Paulo 		 * Reply with proper ECN notifications.
1170f2512ba1SRui Paulo 		 */
1171f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_CWR) {
1172f2512ba1SRui Paulo 			flags |= TH_CWR;
1173f2512ba1SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_CWR;
1174f2512ba1SRui Paulo 		}
1175f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_ECE)
1176f2512ba1SRui Paulo 			flags |= TH_ECE;
1177f2512ba1SRui Paulo 	}
1178f2512ba1SRui Paulo 
1179f2512ba1SRui Paulo 	/*
1180df8bae1dSRodney W. Grimes 	 * If we are doing retransmissions, then snd_nxt will
1181df8bae1dSRodney W. Grimes 	 * not reflect the first unsent octet.  For ACK only
1182df8bae1dSRodney W. Grimes 	 * packets, we do not want the sequence number of the
1183df8bae1dSRodney W. Grimes 	 * retransmitted packet, we want the sequence number
1184df8bae1dSRodney W. Grimes 	 * of the next unsent octet.  So, if there is no data
1185df8bae1dSRodney W. Grimes 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
1186df8bae1dSRodney W. Grimes 	 * when filling in ti_seq.  But if we are in persist
1187df8bae1dSRodney W. Grimes 	 * state, snd_max might reflect one byte beyond the
1188df8bae1dSRodney W. Grimes 	 * right edge of the window, so use snd_nxt in that
1189df8bae1dSRodney W. Grimes 	 * case, since we know we aren't doing a retransmission.
1190df8bae1dSRodney W. Grimes 	 * (retransmit and persist are mutually exclusive...)
1191df8bae1dSRodney W. Grimes 	 */
1192a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
1193b8152ba7SAndre Oppermann 		if (len || (flags & (TH_SYN|TH_FIN)) ||
1194b8152ba7SAndre Oppermann 		    tcp_timer_active(tp, TT_PERSIST))
1195fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_nxt);
1196df8bae1dSRodney W. Grimes 		else
1197fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_max);
1198a55db2b6SPaul Saab 	} else {
11996d90faf3SPaul Saab 		th->th_seq = htonl(p->rxmit);
12006d90faf3SPaul Saab 		p->rxmit += len;
12010077b016SPaul Saab 		tp->sackhint.sack_bytes_rexmit += len;
12026d90faf3SPaul Saab 	}
1203fb59c426SYoshinobu Inoue 	th->th_ack = htonl(tp->rcv_nxt);
1204df8bae1dSRodney W. Grimes 	if (optlen) {
1205fb59c426SYoshinobu Inoue 		bcopy(opt, th + 1, optlen);
1206fb59c426SYoshinobu Inoue 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
1207df8bae1dSRodney W. Grimes 	}
1208fb59c426SYoshinobu Inoue 	th->th_flags = flags;
1209df8bae1dSRodney W. Grimes 	/*
1210df8bae1dSRodney W. Grimes 	 * Calculate receive window.  Don't shrink window,
1211df8bae1dSRodney W. Grimes 	 * but avoid silly window syndrome.
1212df8bae1dSRodney W. Grimes 	 */
12133ac12506SJonathan T. Looney 	if (recwin < (so->so_rcv.sb_hiwat / 4) &&
12143ac12506SJonathan T. Looney 	    recwin < tp->t_maxseg)
1215201d185bSAndre Oppermann 		recwin = 0;
1216f701e30dSJohn Baldwin 	if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
12173ac12506SJonathan T. Looney 	    recwin < (tp->rcv_adv - tp->rcv_nxt))
12183ac12506SJonathan T. Looney 		recwin = (tp->rcv_adv - tp->rcv_nxt);
1219262c1c1aSMatthew Dillon 
1220104ebb2aSAndre Oppermann 	/*
1221104ebb2aSAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
1222104ebb2aSAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK>
1223104ebb2aSAndre Oppermann 	 * case is handled in syncache.
1224104ebb2aSAndre Oppermann 	 */
1225104ebb2aSAndre Oppermann 	if (flags & TH_SYN)
1226104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)
1227104ebb2aSAndre Oppermann 				(min(sbspace(&so->so_rcv), TCP_MAXWIN)));
1228104ebb2aSAndre Oppermann 	else
1229104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
1230262c1c1aSMatthew Dillon 
1231262c1c1aSMatthew Dillon 	/*
1232262c1c1aSMatthew Dillon 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised
1233262c1c1aSMatthew Dillon 	 * a 0 window.  This may cause the remote transmitter to stall.  This
1234262c1c1aSMatthew Dillon 	 * flag tells soreceive() to disable delayed acknowledgements when
1235262c1c1aSMatthew Dillon 	 * draining the buffer.  This can occur if the receiver is attempting
1236b2722702SRui Paulo 	 * to read more data than can be buffered prior to transmitting on
1237262c1c1aSMatthew Dillon 	 * the connection.
1238262c1c1aSMatthew Dillon 	 */
1239f5d34df5SGeorge V. Neville-Neil 	if (th->th_win == 0) {
1240f5d34df5SGeorge V. Neville-Neil 		tp->t_sndzerowin++;
1241262c1c1aSMatthew Dillon 		tp->t_flags |= TF_RXWIN0SENT;
1242f5d34df5SGeorge V. Neville-Neil 	} else
1243262c1c1aSMatthew Dillon 		tp->t_flags &= ~TF_RXWIN0SENT;
1244df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1245fb59c426SYoshinobu Inoue 		th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
1246fb59c426SYoshinobu Inoue 		th->th_flags |= TH_URG;
1247df8bae1dSRodney W. Grimes 	} else
1248df8bae1dSRodney W. Grimes 		/*
1249df8bae1dSRodney W. Grimes 		 * If no urgent pointer to send, then we pull
1250df8bae1dSRodney W. Grimes 		 * the urgent pointer to the left edge of the send window
1251df8bae1dSRodney W. Grimes 		 * so that it doesn't drift into the send window on sequence
1252df8bae1dSRodney W. Grimes 		 * number wraparound.
1253df8bae1dSRodney W. Grimes 		 */
1254df8bae1dSRodney W. Grimes 		tp->snd_up = tp->snd_una;		/* drag it along */
1255df8bae1dSRodney W. Grimes 
12561cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
1257f73d9fd2SGleb Smirnoff 	if (to.to_flags & TOF_SIGNATURE) {
1258ee763d0dSBjoern A. Zeeb 		int sigoff = to.to_signature - opt;
12593418daf2SBjoern A. Zeeb 		tcp_signature_compute(m, 0, len, optlen,
12601cfd4b53SBruce M Simpson 		    (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
1261ee763d0dSBjoern A. Zeeb 	}
1262265ed012SBruce M Simpson #endif
12631cfd4b53SBruce M Simpson 
1264df8bae1dSRodney W. Grimes 	/*
1265df8bae1dSRodney W. Grimes 	 * Put TCP length in extended header, and then
1266df8bae1dSRodney W. Grimes 	 * checksum extended header and data.
1267df8bae1dSRodney W. Grimes 	 */
1268fb59c426SYoshinobu Inoue 	m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
126945747ba5SBjoern A. Zeeb 	m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1270fb59c426SYoshinobu Inoue #ifdef INET6
127145747ba5SBjoern A. Zeeb 	if (isipv6) {
1272fb59c426SYoshinobu Inoue 		/*
1273*3df96ee6SCy Schubert 		 * There is no need to fill in ip6_plen right now.
1274*3df96ee6SCy Schubert 		 * It will be filled later by ip6_output.
1275fb59c426SYoshinobu Inoue 		 */
1276356ab07eSBjoern A. Zeeb 		m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
127745747ba5SBjoern A. Zeeb 		th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) +
127845747ba5SBjoern A. Zeeb 		    optlen + len, IPPROTO_TCP, 0);
127945747ba5SBjoern A. Zeeb 	}
128045747ba5SBjoern A. Zeeb #endif
128145747ba5SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1282fb59c426SYoshinobu Inoue 	else
128345747ba5SBjoern A. Zeeb #endif
128445747ba5SBjoern A. Zeeb #ifdef INET
1285fb59c426SYoshinobu Inoue 	{
1286356ab07eSBjoern A. Zeeb 		m->m_pkthdr.csum_flags = CSUM_TCP;
128779909384SJonathan Lemon 		th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
128879909384SJonathan Lemon 		    htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
1289fb59c426SYoshinobu Inoue 
1290db4f9cc7SJonathan Lemon 		/* IP version must be set here for ipv4/ipv6 checking later */
1291db4f9cc7SJonathan Lemon 		KASSERT(ip->ip_v == IPVERSION,
12926e551fb6SDavid E. O'Brien 		    ("%s: IP version incorrect: %d", __func__, ip->ip_v));
1293fb59c426SYoshinobu Inoue 	}
129445747ba5SBjoern A. Zeeb #endif
1295df8bae1dSRodney W. Grimes 
1296df8bae1dSRodney W. Grimes 	/*
1297b3c0f300SAndre Oppermann 	 * Enable TSO and specify the size of the segments.
1298b3c0f300SAndre Oppermann 	 * The TCP pseudo header checksum is always provided.
1299b3c0f300SAndre Oppermann 	 */
1300b3c0f300SAndre Oppermann 	if (tso) {
13010c39d38dSGleb Smirnoff 		KASSERT(len > tp->t_maxseg - optlen,
1302153e5b57SAndre Oppermann 		    ("%s: len <= tso_segsz", __func__));
13033579cf4cSKenneth D. Merry 		m->m_pkthdr.csum_flags |= CSUM_TSO;
13040c39d38dSGleb Smirnoff 		m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen;
1305b3c0f300SAndre Oppermann 	}
1306b3c0f300SAndre Oppermann 
130775497cc5SBjoern A. Zeeb #ifdef IPSEC
130875497cc5SBjoern A. Zeeb 	KASSERT(len + hdrlen + ipoptlen - ipsec_optlen == m_length(m, NULL),
13093ac12506SJonathan T. Looney 	    ("%s: mbuf chain shorter than expected: %d + %u + %u - %u != %u",
131075497cc5SBjoern A. Zeeb 	    __func__, len, hdrlen, ipoptlen, ipsec_optlen, m_length(m, NULL)));
131175497cc5SBjoern A. Zeeb #else
1312ed420311SAndre Oppermann 	KASSERT(len + hdrlen + ipoptlen == m_length(m, NULL),
13133ac12506SJonathan T. Looney 	    ("%s: mbuf chain shorter than expected: %d + %u + %u != %u",
131475497cc5SBjoern A. Zeeb 	    __func__, len, hdrlen, ipoptlen, m_length(m, NULL)));
131575497cc5SBjoern A. Zeeb #endif
1316ed420311SAndre Oppermann 
1317bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
131839bc9de5SLawrence Stewart 	/* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
131939bc9de5SLawrence Stewart 	hhook_run_tcp_est_out(tp, th, &to, len, tso);
1320bd79708dSJonathan T. Looney #endif
132139bc9de5SLawrence Stewart 
1322610ee2f9SDavid Greenman #ifdef TCPDEBUG
1323df8bae1dSRodney W. Grimes 	/*
1324df8bae1dSRodney W. Grimes 	 * Trace.
1325df8bae1dSRodney W. Grimes 	 */
132691f467d5SHartmut Brandt 	if (so->so_options & SO_DEBUG) {
1327f3e0b7efSBruce M Simpson 		u_short save = 0;
13285214cb3fSBruce M Simpson #ifdef INET6
13295214cb3fSBruce M Simpson 		if (!isipv6)
13305214cb3fSBruce M Simpson #endif
13315214cb3fSBruce M Simpson 		{
13325214cb3fSBruce M Simpson 			save = ipov->ih_len;
133391f467d5SHartmut Brandt 			ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
13345214cb3fSBruce M Simpson 		}
1335fb59c426SYoshinobu Inoue 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
13365214cb3fSBruce M Simpson #ifdef INET6
13375214cb3fSBruce M Simpson 		if (!isipv6)
13385214cb3fSBruce M Simpson #endif
133991f467d5SHartmut Brandt 		ipov->ih_len = save;
134091f467d5SHartmut Brandt 	}
1341b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
13422b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__output, tp, th, m);
1343df8bae1dSRodney W. Grimes 
1344df8bae1dSRodney W. Grimes 	/*
1345df8bae1dSRodney W. Grimes 	 * Fill in IP length and desired time to live and
1346df8bae1dSRodney W. Grimes 	 * send to IP level.  There should be a better way
1347df8bae1dSRodney W. Grimes 	 * to handle ttl and tos; we could keep them in
1348df8bae1dSRodney W. Grimes 	 * the template, but need a way to checksum without them.
1349df8bae1dSRodney W. Grimes 	 */
1350fb59c426SYoshinobu Inoue 	/*
135143630e62SHiren Panchasara 	 * m->m_pkthdr.len should have been set before checksum calculation,
1352fb59c426SYoshinobu Inoue 	 * because in6_cksum() need it.
1353fb59c426SYoshinobu Inoue 	 */
1354fb59c426SYoshinobu Inoue #ifdef INET6
1355fb59c426SYoshinobu Inoue 	if (isipv6) {
1356df0633a1SGleb Smirnoff 		struct route_in6 ro;
1357df0633a1SGleb Smirnoff 
1358df0633a1SGleb Smirnoff 		bzero(&ro, sizeof(ro));
1359fb59c426SYoshinobu Inoue 		/*
1360fb59c426SYoshinobu Inoue 		 * we separately set hoplimit for every segment, since the
1361fb59c426SYoshinobu Inoue 		 * user might want to change the value via setsockopt.
1362fb59c426SYoshinobu Inoue 		 * Also, desired default hop limit might be changed via
1363fb59c426SYoshinobu Inoue 		 * Neighbor Discovery.
1364fb59c426SYoshinobu Inoue 		 */
136597d8d152SAndre Oppermann 		ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1366fb59c426SYoshinobu Inoue 
136757f60867SMark Johnston 		/*
136857f60867SMark Johnston 		 * Set the packet size here for the benefit of DTrace probes.
136957f60867SMark Johnston 		 * ip6_output() will set it properly; it's supposed to include
137057f60867SMark Johnston 		 * the option header lengths as well.
137157f60867SMark Johnston 		 */
137257f60867SMark Johnston 		ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
137357f60867SMark Johnston 
13740c39d38dSGleb Smirnoff 		if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss)
13750f3e3bc5SSean Bruno 			tp->t_flags2 |= TF2_PLPMTU_PMTUD;
13760f3e3bc5SSean Bruno 		else
13770f3e3bc5SSean Bruno 			tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
13780f3e3bc5SSean Bruno 
137957f60867SMark Johnston 		if (tp->t_state == TCPS_SYN_SENT)
1380d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th);
138157f60867SMark Johnston 
138257f60867SMark Johnston 		TCP_PROBE5(send, NULL, tp, ip6, tp, th);
138357f60867SMark Johnston 
138486a996e6SHiren Panchasara #ifdef TCPPCAP
138586a996e6SHiren Panchasara 		/* Save packet, if requested. */
138686a996e6SHiren Panchasara 		tcp_pcap_add(th, m, &(tp->t_outpkts));
138786a996e6SHiren Panchasara #endif
138886a996e6SHiren Panchasara 
1389fb59c426SYoshinobu Inoue 		/* TODO: IPv6 IP6TOS_ECT bit on */
1390df0633a1SGleb Smirnoff 		error = ip6_output(m, tp->t_inpcb->in6p_outputopts, &ro,
1391df0633a1SGleb Smirnoff 		    ((so->so_options & SO_DONTROUTE) ?  IP_ROUTETOIF : 0),
1392df0633a1SGleb Smirnoff 		    NULL, NULL, tp->t_inpcb);
1393df0633a1SGleb Smirnoff 
1394df0633a1SGleb Smirnoff 		if (error == EMSGSIZE && ro.ro_rt != NULL)
1395e3a7aa6fSGleb Smirnoff 			mtu = ro.ro_rt->rt_mtu;
1396df0633a1SGleb Smirnoff 		RO_RTFREE(&ro);
1397b287c6c7SBjoern A. Zeeb 	}
1398fb59c426SYoshinobu Inoue #endif /* INET6 */
1399b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1400b287c6c7SBjoern A. Zeeb 	else
1401b287c6c7SBjoern A. Zeeb #endif
1402b287c6c7SBjoern A. Zeeb #ifdef INET
1403df8bae1dSRodney W. Grimes     {
14048f134647SGleb Smirnoff 	ip->ip_len = htons(m->m_pkthdr.len);
1405686cdd19SJun-ichiro itojun Hagino #ifdef INET6
14065cd54324SBjoern A. Zeeb 	if (tp->t_inpcb->inp_vflag & INP_IPV6PROTO)
140797d8d152SAndre Oppermann 		ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1408686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */
1409f138387aSGarrett Wollman 	/*
141097d8d152SAndre Oppermann 	 * If we do path MTU discovery, then we set DF on every packet.
141197d8d152SAndre Oppermann 	 * This might not be the best thing to do according to RFC3390
141297d8d152SAndre Oppermann 	 * Section 2. However the tcp hostcache migitates the problem
141397d8d152SAndre Oppermann 	 * so it affects only the first tcp connection with a host.
1414e4e92660SAndre Oppermann 	 *
1415e4e92660SAndre Oppermann 	 * NB: Don't set DF on small MTU/MSS to have a safe fallback.
1416f138387aSGarrett Wollman 	 */
14170c39d38dSGleb Smirnoff 	if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) {
14188f134647SGleb Smirnoff 		ip->ip_off |= htons(IP_DF);
1419f6f6703fSSean Bruno 		tp->t_flags2 |= TF2_PLPMTU_PMTUD;
1420f6f6703fSSean Bruno 	} else {
1421f6f6703fSSean Bruno 		tp->t_flags2 &= ~TF2_PLPMTU_PMTUD;
1422f6f6703fSSean Bruno 	}
142397d8d152SAndre Oppermann 
142457f60867SMark Johnston 	if (tp->t_state == TCPS_SYN_SENT)
1425d9fae5abSAndriy Gapon 		TCP_PROBE5(connect__request, NULL, tp, ip, tp, th);
142657f60867SMark Johnston 
142757f60867SMark Johnston 	TCP_PROBE5(send, NULL, tp, ip, tp, th);
142857f60867SMark Johnston 
142986a996e6SHiren Panchasara #ifdef TCPPCAP
143086a996e6SHiren Panchasara 	/* Save packet, if requested. */
143186a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_outpkts));
143286a996e6SHiren Panchasara #endif
143386a996e6SHiren Panchasara 
143484cc0778SGeorge V. Neville-Neil 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
1435b5d47ff5SJohn-Mark Gurney 	    ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0,
1436b5d47ff5SJohn-Mark Gurney 	    tp->t_inpcb);
1437df0633a1SGleb Smirnoff 
143884cc0778SGeorge V. Neville-Neil 	if (error == EMSGSIZE && tp->t_inpcb->inp_route.ro_rt != NULL)
143984cc0778SGeorge V. Neville-Neil 		mtu = tp->t_inpcb->inp_route.ro_rt->rt_mtu;
1440df8bae1dSRodney W. Grimes     }
1441b287c6c7SBjoern A. Zeeb #endif /* INET */
14420e2bc05cSGleb Smirnoff 
14430e2bc05cSGleb Smirnoff out:
14440e2bc05cSGleb Smirnoff 	/*
14450e2bc05cSGleb Smirnoff 	 * In transmit state, time the transmission and arrange for
14460e2bc05cSGleb Smirnoff 	 * the retransmit.  In persist state, just set snd_max.
14470e2bc05cSGleb Smirnoff 	 */
14480e2bc05cSGleb Smirnoff 	if ((tp->t_flags & TF_FORCEDATA) == 0 ||
14490e2bc05cSGleb Smirnoff 	    !tcp_timer_active(tp, TT_PERSIST)) {
14500e2bc05cSGleb Smirnoff 		tcp_seq startseq = tp->snd_nxt;
14510e2bc05cSGleb Smirnoff 
14520e2bc05cSGleb Smirnoff 		/*
14530e2bc05cSGleb Smirnoff 		 * Advance snd_nxt over sequence space of this segment.
14540e2bc05cSGleb Smirnoff 		 */
14550e2bc05cSGleb Smirnoff 		if (flags & (TH_SYN|TH_FIN)) {
14560e2bc05cSGleb Smirnoff 			if (flags & TH_SYN)
14570e2bc05cSGleb Smirnoff 				tp->snd_nxt++;
14580e2bc05cSGleb Smirnoff 			if (flags & TH_FIN) {
14590e2bc05cSGleb Smirnoff 				tp->snd_nxt++;
14600e2bc05cSGleb Smirnoff 				tp->t_flags |= TF_SENTFIN;
14610e2bc05cSGleb Smirnoff 			}
14620e2bc05cSGleb Smirnoff 		}
14630e2bc05cSGleb Smirnoff 		if (sack_rxmit)
14640e2bc05cSGleb Smirnoff 			goto timer;
14650e2bc05cSGleb Smirnoff 		tp->snd_nxt += len;
14660e2bc05cSGleb Smirnoff 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
14670e2bc05cSGleb Smirnoff 			tp->snd_max = tp->snd_nxt;
14680e2bc05cSGleb Smirnoff 			/*
14690e2bc05cSGleb Smirnoff 			 * Time this transmission if not a retransmission and
14700e2bc05cSGleb Smirnoff 			 * not currently timing anything.
14710e2bc05cSGleb Smirnoff 			 */
14720e2bc05cSGleb Smirnoff 			if (tp->t_rtttime == 0) {
14730e2bc05cSGleb Smirnoff 				tp->t_rtttime = ticks;
14740e2bc05cSGleb Smirnoff 				tp->t_rtseq = startseq;
14750e2bc05cSGleb Smirnoff 				TCPSTAT_INC(tcps_segstimed);
14760e2bc05cSGleb Smirnoff 			}
14770e2bc05cSGleb Smirnoff 		}
14780e2bc05cSGleb Smirnoff 
14790e2bc05cSGleb Smirnoff 		/*
14800e2bc05cSGleb Smirnoff 		 * Set retransmit timer if not currently set,
14810e2bc05cSGleb Smirnoff 		 * and not doing a pure ack or a keep-alive probe.
14820e2bc05cSGleb Smirnoff 		 * Initial value for retransmit timer is smoothed
14830e2bc05cSGleb Smirnoff 		 * round-trip time + 2 * round-trip time variance.
14840e2bc05cSGleb Smirnoff 		 * Initialize shift counter which is used for backoff
14850e2bc05cSGleb Smirnoff 		 * of retransmit time.
14860e2bc05cSGleb Smirnoff 		 */
14870e2bc05cSGleb Smirnoff timer:
14880e2bc05cSGleb Smirnoff 		if (!tcp_timer_active(tp, TT_REXMT) &&
14890e2bc05cSGleb Smirnoff 		    ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
14900e2bc05cSGleb Smirnoff 		     (tp->snd_nxt != tp->snd_una))) {
14910e2bc05cSGleb Smirnoff 			if (tcp_timer_active(tp, TT_PERSIST)) {
14920e2bc05cSGleb Smirnoff 				tcp_timer_activate(tp, TT_PERSIST, 0);
14930e2bc05cSGleb Smirnoff 				tp->t_rxtshift = 0;
14940e2bc05cSGleb Smirnoff 			}
14950e2bc05cSGleb Smirnoff 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
1496f8568079SHiren Panchasara 		} else if (len == 0 && sbavail(&so->so_snd) &&
1497f8568079SHiren Panchasara 		    !tcp_timer_active(tp, TT_REXMT) &&
1498f8568079SHiren Panchasara 		    !tcp_timer_active(tp, TT_PERSIST)) {
1499f8568079SHiren Panchasara 			/*
1500f8568079SHiren Panchasara 			 * Avoid a situation where we do not set persist timer
1501f8568079SHiren Panchasara 			 * after a zero window condition. For example:
1502f8568079SHiren Panchasara 			 * 1) A -> B: packet with enough data to fill the window
1503f8568079SHiren Panchasara 			 * 2) B -> A: ACK for #1 + new data (0 window
1504f8568079SHiren Panchasara 			 *    advertisement)
1505f8568079SHiren Panchasara 			 * 3) A -> B: ACK for #2, 0 len packet
1506f8568079SHiren Panchasara 			 *
1507f8568079SHiren Panchasara 			 * In this case, A will not activate the persist timer,
1508f8568079SHiren Panchasara 			 * because it chose to send a packet. Unless tcp_output
1509f8568079SHiren Panchasara 			 * is called for some other reason (delayed ack timer,
1510f8568079SHiren Panchasara 			 * another input packet from B, socket syscall), A will
1511f8568079SHiren Panchasara 			 * not send zero window probes.
1512f8568079SHiren Panchasara 			 *
1513f8568079SHiren Panchasara 			 * So, if you send a 0-length packet, but there is data
1514f8568079SHiren Panchasara 			 * in the socket buffer, and neither the rexmt or
1515f8568079SHiren Panchasara 			 * persist timer is already set, then activate the
1516f8568079SHiren Panchasara 			 * persist timer.
1517f8568079SHiren Panchasara 			 */
1518f8568079SHiren Panchasara 			tp->t_rxtshift = 0;
1519f8568079SHiren Panchasara 			tcp_setpersist(tp);
15200e2bc05cSGleb Smirnoff 		}
15210e2bc05cSGleb Smirnoff 	} else {
15220e2bc05cSGleb Smirnoff 		/*
15230e2bc05cSGleb Smirnoff 		 * Persist case, update snd_max but since we are in
15240e2bc05cSGleb Smirnoff 		 * persist mode (no window) we do not update snd_nxt.
15250e2bc05cSGleb Smirnoff 		 */
15260e2bc05cSGleb Smirnoff 		int xlen = len;
15270e2bc05cSGleb Smirnoff 		if (flags & TH_SYN)
15280e2bc05cSGleb Smirnoff 			++xlen;
15290e2bc05cSGleb Smirnoff 		if (flags & TH_FIN) {
15300e2bc05cSGleb Smirnoff 			++xlen;
15310e2bc05cSGleb Smirnoff 			tp->t_flags |= TF_SENTFIN;
15320e2bc05cSGleb Smirnoff 		}
15330e2bc05cSGleb Smirnoff 		if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
153415c82571SJonathan T. Looney 			tp->snd_max = tp->snd_nxt + xlen;
15350e2bc05cSGleb Smirnoff 	}
15360e2bc05cSGleb Smirnoff 
1537df8bae1dSRodney W. Grimes 	if (error) {
15387734ea06SArchie Cobbs 
15397734ea06SArchie Cobbs 		/*
15407734ea06SArchie Cobbs 		 * We know that the packet was lost, so back out the
15417734ea06SArchie Cobbs 		 * sequence number advance, if any.
15422c30ec0aSAndre Oppermann 		 *
15432c30ec0aSAndre Oppermann 		 * If the error is EPERM the packet got blocked by the
15442c30ec0aSAndre Oppermann 		 * local firewall.  Normally we should terminate the
15452c30ec0aSAndre Oppermann 		 * connection but the blocking may have been spurious
15462c30ec0aSAndre Oppermann 		 * due to a firewall reconfiguration cycle.  So we treat
15472c30ec0aSAndre Oppermann 		 * it like a packet loss and let the retransmit timer and
15482c30ec0aSAndre Oppermann 		 * timeouts do their work over time.
15492c30ec0aSAndre Oppermann 		 * XXX: It is a POLA question whether calling tcp_drop right
15502c30ec0aSAndre Oppermann 		 * away would be the really correct behavior instead.
15517734ea06SArchie Cobbs 		 */
155272757d9aSGleb Smirnoff 		if (((tp->t_flags & TF_FORCEDATA) == 0 ||
1553b8152ba7SAndre Oppermann 		    !tcp_timer_active(tp, TT_PERSIST)) &&
155472757d9aSGleb Smirnoff 		    ((flags & TH_SYN) == 0) &&
155572757d9aSGleb Smirnoff 		    (error != EPERM)) {
15560077b016SPaul Saab 			if (sack_rxmit) {
15575d3b1b75SJayanth Vijayaraghavan 				p->rxmit -= len;
15580077b016SPaul Saab 				tp->sackhint.sack_bytes_rexmit -= len;
155972757d9aSGleb Smirnoff 				KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
15600077b016SPaul Saab 				    ("sackhint bytes rtx >= 0"));
15610077b016SPaul Saab 			} else
15627734ea06SArchie Cobbs 				tp->snd_nxt -= len;
15637734ea06SArchie Cobbs 		}
1564cf2942b6SRobert Watson 		SOCKBUF_UNLOCK_ASSERT(&so->so_snd);	/* Check gotos. */
156572757d9aSGleb Smirnoff 		switch (error) {
156672757d9aSGleb Smirnoff 		case EPERM:
156772757d9aSGleb Smirnoff 			tp->t_softerror = error;
156872757d9aSGleb Smirnoff 			return (error);
156972757d9aSGleb Smirnoff 		case ENOBUFS:
1570425b7639SSepherosa Ziehau 			TCP_XMIT_TIMER_ASSERT(tp, len, flags);
15711600372bSAndre Oppermann 			tp->snd_cwnd = tp->t_maxseg;
1572df8bae1dSRodney W. Grimes 			return (0);
157372757d9aSGleb Smirnoff 		case EMSGSIZE:
15743d1f141bSGarrett Wollman 			/*
1575b3c0f300SAndre Oppermann 			 * For some reason the interface we used initially
1576b3c0f300SAndre Oppermann 			 * to send segments changed to another or lowered
1577b3c0f300SAndre Oppermann 			 * its MTU.
1578b3c0f300SAndre Oppermann 			 * If TSO was active we either got an interface
1579b3c0f300SAndre Oppermann 			 * without TSO capabilits or TSO was turned off.
1580df0633a1SGleb Smirnoff 			 * If we obtained mtu from ip_output() then update
1581df0633a1SGleb Smirnoff 			 * it and try again.
15823d1f141bSGarrett Wollman 			 */
1583b3c0f300SAndre Oppermann 			if (tso)
1584b3c0f300SAndre Oppermann 				tp->t_flags &= ~TF_TSO;
1585df0633a1SGleb Smirnoff 			if (mtu != 0) {
1586df0633a1SGleb Smirnoff 				tcp_mss_update(tp, -1, mtu, NULL, NULL);
1587df0633a1SGleb Smirnoff 				goto again;
1588df0633a1SGleb Smirnoff 			}
1589df0633a1SGleb Smirnoff 			return (error);
15908bec3467SGleb Smirnoff 		case EHOSTDOWN:
159172757d9aSGleb Smirnoff 		case EHOSTUNREACH:
159272757d9aSGleb Smirnoff 		case ENETDOWN:
15938bec3467SGleb Smirnoff 		case ENETUNREACH:
159472757d9aSGleb Smirnoff 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
1595df8bae1dSRodney W. Grimes 				tp->t_softerror = error;
1596df8bae1dSRodney W. Grimes 				return (0);
1597df8bae1dSRodney W. Grimes 			}
159872757d9aSGleb Smirnoff 			/* FALLTHROUGH */
159972757d9aSGleb Smirnoff 		default:
1600df8bae1dSRodney W. Grimes 			return (error);
1601df8bae1dSRodney W. Grimes 		}
160272757d9aSGleb Smirnoff 	}
160378b50714SRobert Watson 	TCPSTAT_INC(tcps_sndtotal);
1604df8bae1dSRodney W. Grimes 
1605df8bae1dSRodney W. Grimes 	/*
1606df8bae1dSRodney W. Grimes 	 * Data sent (as far as we can tell).
1607df8bae1dSRodney W. Grimes 	 * If this advertises a larger window than any other segment,
1608df8bae1dSRodney W. Grimes 	 * then remember the size of the advertised window.
1609df8bae1dSRodney W. Grimes 	 * Any pending ACK has now been sent.
1610df8bae1dSRodney W. Grimes 	 */
16113ac12506SJonathan T. Looney 	if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1612201d185bSAndre Oppermann 		tp->rcv_adv = tp->rcv_nxt + recwin;
1613df8bae1dSRodney W. Grimes 	tp->last_ack_sent = tp->rcv_nxt;
16143bfd6421SJonathan Lemon 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
1615b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_DELACK))
1616b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, 0);
1617d912c694SMatthew Dillon #if 0
1618d912c694SMatthew Dillon 	/*
1619d912c694SMatthew Dillon 	 * This completely breaks TCP if newreno is turned on.  What happens
1620d912c694SMatthew Dillon 	 * is that if delayed-acks are turned on on the receiver, this code
1621d912c694SMatthew Dillon 	 * on the transmitter effectively destroys the TCP window, forcing
1622d912c694SMatthew Dillon 	 * it to four packets (1.5Kx4 = 6K window).
1623d912c694SMatthew Dillon 	 */
1624dbc42409SLawrence Stewart 	if (sendalot && --maxburst)
1625df8bae1dSRodney W. Grimes 		goto again;
1626d912c694SMatthew Dillon #endif
1627d912c694SMatthew Dillon 	if (sendalot)
1628d912c694SMatthew Dillon 		goto again;
1629df8bae1dSRodney W. Grimes 	return (0);
1630df8bae1dSRodney W. Grimes }
1631df8bae1dSRodney W. Grimes 
1632df8bae1dSRodney W. Grimes void
1633ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp)
1634df8bae1dSRodney W. Grimes {
16359b8b58e0SJonathan Lemon 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
16369b8b58e0SJonathan Lemon 	int tt;
1637df8bae1dSRodney W. Grimes 
1638672dc4aeSJohn Baldwin 	tp->t_flags &= ~TF_PREVVALID;
1639b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_REXMT))
16409b8b58e0SJonathan Lemon 		panic("tcp_setpersist: retransmit pending");
1641df8bae1dSRodney W. Grimes 	/*
1642a4641f4eSPedro F. Giffuni 	 * Start/restart persistence timer.
1643df8bae1dSRodney W. Grimes 	 */
16449b8b58e0SJonathan Lemon 	TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
16450645c604SHiren Panchasara 		      tcp_persmin, tcp_persmax);
1646b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_PERSIST, tt);
1647df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1648df8bae1dSRodney W. Grimes 		tp->t_rxtshift++;
1649df8bae1dSRodney W. Grimes }
165002a1a643SAndre Oppermann 
165102a1a643SAndre Oppermann /*
165202a1a643SAndre Oppermann  * Insert TCP options according to the supplied parameters to the place
165302a1a643SAndre Oppermann  * optp in a consistent way.  Can handle unaligned destinations.
165402a1a643SAndre Oppermann  *
165502a1a643SAndre Oppermann  * The order of the option processing is crucial for optimal packing and
165602a1a643SAndre Oppermann  * alignment for the scarce option space.
165702a1a643SAndre Oppermann  *
165802a1a643SAndre Oppermann  * The optimal order for a SYN/SYN-ACK segment is:
165902a1a643SAndre Oppermann  *   MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) +
166002a1a643SAndre Oppermann  *   Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40.
166102a1a643SAndre Oppermann  *
166202a1a643SAndre Oppermann  * The SACK options should be last.  SACK blocks consume 8*n+2 bytes.
166302a1a643SAndre Oppermann  * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks).
166402a1a643SAndre Oppermann  * At minimum we need 10 bytes (to generate 1 SACK block).  If both
166502a1a643SAndre Oppermann  * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present,
166602a1a643SAndre Oppermann  * we only have 10 bytes for SACK options (40 - (12 + 18)).
166702a1a643SAndre Oppermann  */
166802a1a643SAndre Oppermann int
166902a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp)
167002a1a643SAndre Oppermann {
16715d20f974SJonathan T. Looney 	u_int32_t mask, optlen = 0;
167202a1a643SAndre Oppermann 
167302a1a643SAndre Oppermann 	for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) {
167402a1a643SAndre Oppermann 		if ((to->to_flags & mask) != mask)
167502a1a643SAndre Oppermann 			continue;
16763a4018c4SAndre Oppermann 		if (optlen == TCP_MAXOLEN)
16773a4018c4SAndre Oppermann 			break;
167802a1a643SAndre Oppermann 		switch (to->to_flags & mask) {
167902a1a643SAndre Oppermann 		case TOF_MSS:
168002a1a643SAndre Oppermann 			while (optlen % 4) {
168102a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
168202a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
168302a1a643SAndre Oppermann 			}
16843a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG)
16853a4018c4SAndre Oppermann 				continue;
168602a1a643SAndre Oppermann 			optlen += TCPOLEN_MAXSEG;
168702a1a643SAndre Oppermann 			*optp++ = TCPOPT_MAXSEG;
168802a1a643SAndre Oppermann 			*optp++ = TCPOLEN_MAXSEG;
168902a1a643SAndre Oppermann 			to->to_mss = htons(to->to_mss);
169002a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss));
169102a1a643SAndre Oppermann 			optp += sizeof(to->to_mss);
169202a1a643SAndre Oppermann 			break;
169302a1a643SAndre Oppermann 		case TOF_SCALE:
169402a1a643SAndre Oppermann 			while (!optlen || optlen % 2 != 1) {
169502a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
169602a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
169702a1a643SAndre Oppermann 			}
16983a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW)
16993a4018c4SAndre Oppermann 				continue;
170002a1a643SAndre Oppermann 			optlen += TCPOLEN_WINDOW;
170102a1a643SAndre Oppermann 			*optp++ = TCPOPT_WINDOW;
170202a1a643SAndre Oppermann 			*optp++ = TCPOLEN_WINDOW;
170302a1a643SAndre Oppermann 			*optp++ = to->to_wscale;
170402a1a643SAndre Oppermann 			break;
170502a1a643SAndre Oppermann 		case TOF_SACKPERM:
170602a1a643SAndre Oppermann 			while (optlen % 2) {
170702a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
170802a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
170902a1a643SAndre Oppermann 			}
17103a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED)
17113a4018c4SAndre Oppermann 				continue;
171202a1a643SAndre Oppermann 			optlen += TCPOLEN_SACK_PERMITTED;
171302a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK_PERMITTED;
171402a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACK_PERMITTED;
171502a1a643SAndre Oppermann 			break;
171602a1a643SAndre Oppermann 		case TOF_TS:
171702a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
171802a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
171902a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
172002a1a643SAndre Oppermann 			}
17213a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP)
17223a4018c4SAndre Oppermann 				continue;
172302a1a643SAndre Oppermann 			optlen += TCPOLEN_TIMESTAMP;
172402a1a643SAndre Oppermann 			*optp++ = TCPOPT_TIMESTAMP;
172502a1a643SAndre Oppermann 			*optp++ = TCPOLEN_TIMESTAMP;
172602a1a643SAndre Oppermann 			to->to_tsval = htonl(to->to_tsval);
172702a1a643SAndre Oppermann 			to->to_tsecr = htonl(to->to_tsecr);
172802a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval));
172902a1a643SAndre Oppermann 			optp += sizeof(to->to_tsval);
173002a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr));
173102a1a643SAndre Oppermann 			optp += sizeof(to->to_tsecr);
173202a1a643SAndre Oppermann 			break;
1733f73d9fd2SGleb Smirnoff #ifdef TCP_SIGNATURE
173402a1a643SAndre Oppermann 		case TOF_SIGNATURE:
173502a1a643SAndre Oppermann 			{
173602a1a643SAndre Oppermann 			int siglen = TCPOLEN_SIGNATURE - 2;
173702a1a643SAndre Oppermann 
173802a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
173902a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
174002a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
174102a1a643SAndre Oppermann 			}
17420d957bbaSAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE)
174302a1a643SAndre Oppermann 				continue;
174402a1a643SAndre Oppermann 			optlen += TCPOLEN_SIGNATURE;
174502a1a643SAndre Oppermann 			*optp++ = TCPOPT_SIGNATURE;
174602a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SIGNATURE;
174702a1a643SAndre Oppermann 			to->to_signature = optp;
174802a1a643SAndre Oppermann 			while (siglen--)
174902a1a643SAndre Oppermann 				 *optp++ = 0;
175002a1a643SAndre Oppermann 			break;
175102a1a643SAndre Oppermann 			}
1752f73d9fd2SGleb Smirnoff #endif
175302a1a643SAndre Oppermann 		case TOF_SACK:
175402a1a643SAndre Oppermann 			{
175502a1a643SAndre Oppermann 			int sackblks = 0;
175602a1a643SAndre Oppermann 			struct sackblk *sack = (struct sackblk *)to->to_sacks;
175702a1a643SAndre Oppermann 			tcp_seq sack_seq;
175802a1a643SAndre Oppermann 
175902a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
176002a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
176102a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
176202a1a643SAndre Oppermann 			}
17633a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK)
176402a1a643SAndre Oppermann 				continue;
176502a1a643SAndre Oppermann 			optlen += TCPOLEN_SACKHDR;
176602a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK;
176702a1a643SAndre Oppermann 			sackblks = min(to->to_nsacks,
17680d957bbaSAndre Oppermann 					(TCP_MAXOLEN - optlen) / TCPOLEN_SACK);
176902a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK;
177002a1a643SAndre Oppermann 			while (sackblks--) {
177102a1a643SAndre Oppermann 				sack_seq = htonl(sack->start);
177202a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
177302a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
177402a1a643SAndre Oppermann 				sack_seq = htonl(sack->end);
177502a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
177602a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
177702a1a643SAndre Oppermann 				optlen += TCPOLEN_SACK;
177802a1a643SAndre Oppermann 				sack++;
177902a1a643SAndre Oppermann 			}
178078b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_send_blocks);
178102a1a643SAndre Oppermann 			break;
178202a1a643SAndre Oppermann 			}
1783281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1784281a0fd4SPatrick Kelsey 		case TOF_FASTOPEN:
1785281a0fd4SPatrick Kelsey 			{
1786281a0fd4SPatrick Kelsey 			int total_len;
1787281a0fd4SPatrick Kelsey 
1788281a0fd4SPatrick Kelsey 			/* XXX is there any point to aligning this option? */
1789281a0fd4SPatrick Kelsey 			total_len = TCPOLEN_FAST_OPEN_EMPTY + to->to_tfo_len;
1790281a0fd4SPatrick Kelsey 			if (TCP_MAXOLEN - optlen < total_len)
1791281a0fd4SPatrick Kelsey 				continue;
1792281a0fd4SPatrick Kelsey 			*optp++ = TCPOPT_FAST_OPEN;
1793281a0fd4SPatrick Kelsey 			*optp++ = total_len;
1794281a0fd4SPatrick Kelsey 			if (to->to_tfo_len > 0) {
1795281a0fd4SPatrick Kelsey 				bcopy(to->to_tfo_cookie, optp, to->to_tfo_len);
1796281a0fd4SPatrick Kelsey 				optp += to->to_tfo_len;
1797281a0fd4SPatrick Kelsey 			}
1798281a0fd4SPatrick Kelsey 			optlen += total_len;
1799281a0fd4SPatrick Kelsey 			break;
1800281a0fd4SPatrick Kelsey 			}
1801281a0fd4SPatrick Kelsey #endif
180202a1a643SAndre Oppermann 		default:
180302a1a643SAndre Oppermann 			panic("%s: unknown TCP option type", __func__);
180402a1a643SAndre Oppermann 			break;
180502a1a643SAndre Oppermann 		}
180602a1a643SAndre Oppermann 	}
180702a1a643SAndre Oppermann 
180802a1a643SAndre Oppermann 	/* Terminate and pad TCP options to a 4 byte boundary. */
180902a1a643SAndre Oppermann 	if (optlen % 4) {
181002a1a643SAndre Oppermann 		optlen += TCPOLEN_EOL;
181102a1a643SAndre Oppermann 		*optp++ = TCPOPT_EOL;
181202a1a643SAndre Oppermann 	}
1813413deb12SBjoern A. Zeeb 	/*
1814413deb12SBjoern A. Zeeb 	 * According to RFC 793 (STD0007):
1815413deb12SBjoern A. Zeeb 	 *   "The content of the header beyond the End-of-Option option
1816413deb12SBjoern A. Zeeb 	 *    must be header padding (i.e., zero)."
1817413deb12SBjoern A. Zeeb 	 *   and later: "The padding is composed of zeros."
1818413deb12SBjoern A. Zeeb 	 */
181902a1a643SAndre Oppermann 	while (optlen % 4) {
1820c343c524SAndre Oppermann 		optlen += TCPOLEN_PAD;
1821c343c524SAndre Oppermann 		*optp++ = TCPOPT_PAD;
182202a1a643SAndre Oppermann 	}
182302a1a643SAndre Oppermann 
18240d957bbaSAndre Oppermann 	KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__));
182502a1a643SAndre Oppermann 	return (optlen);
182602a1a643SAndre Oppermann }
1827