xref: /freebsd/sys/netinet/tcp_output.c (revision b287c6c70cd41c6805ade3f16b073028c5ccd836)
1c398230bSWarner Losh /*-
2d7f570e6SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29d7f570e6SGarrett Wollman  *	@(#)tcp_output.c	8.4 (Berkeley) 5/24/95
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
351cfd4b53SBruce M Simpson #include "opt_inet.h"
36fb59c426SYoshinobu Inoue #include "opt_inet6.h"
373a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
380cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
390cc12cc5SJoerg Wunsch 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
42686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h>
4339bc9de5SLawrence Stewart #include <sys/hhook.h>
44fb919e4dSMark Murray #include <sys/kernel.h>
45fb919e4dSMark Murray #include <sys/lock.h>
46fb919e4dSMark Murray #include <sys/mbuf.h>
47fb919e4dSMark Murray #include <sys/mutex.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51fb919e4dSMark Murray #include <sys/sysctl.h>
52df8bae1dSRodney W. Grimes 
534b79449eSBjoern A. Zeeb #include <net/if.h>
54df8bae1dSRodney W. Grimes #include <net/route.h>
55530c0060SRobert Watson #include <net/vnet.h>
56df8bae1dSRodney W. Grimes 
57dbc42409SLawrence Stewart #include <netinet/cc.h>
58df8bae1dSRodney W. Grimes #include <netinet/in.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
60df8bae1dSRodney W. Grimes #include <netinet/ip.h>
61df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
62df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
63ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
64fb59c426SYoshinobu Inoue #ifdef INET6
65686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
66686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
67fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
68fb59c426SYoshinobu Inoue #endif
69df8bae1dSRodney W. Grimes #define	TCPOUTFLAGS
70df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
71df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
72df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
73df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
74df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
75610ee2f9SDavid Greenman #ifdef TCPDEBUG
76df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
77610ee2f9SDavid Greenman #endif
78df8bae1dSRodney W. Grimes 
79b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
80b9234fafSSam Leffler #include <netipsec/ipsec.h>
81b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
82b9234fafSSam Leffler 
83db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
84db4f9cc7SJonathan Lemon 
85aed55708SRobert Watson #include <security/mac/mac_framework.h>
86aed55708SRobert Watson 
87df8bae1dSRodney W. Grimes #ifdef notyet
88df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack();
89df8bae1dSRodney W. Grimes #endif
90df8bae1dSRodney W. Grimes 
9182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, path_mtu_discovery) = 1;
92eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW,
93eddfbb76SRobert Watson 	&VNET_NAME(path_mtu_discovery), 1,
94eddfbb76SRobert Watson 	"Enable Path MTU Discovery");
959a039a5fSDavid Greenman 
9682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ss_fltsz) = 1;
97eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW,
98eddfbb76SRobert Watson 	&VNET_NAME(ss_fltsz), 1,
99eddfbb76SRobert Watson 	"Slow start flight size");
1009b8b58e0SJonathan Lemon 
10182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ss_fltsz_local) = 4;
102eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize,
103eddfbb76SRobert Watson 	CTLFLAG_RW, &VNET_NAME(ss_fltsz_local), 1,
104eddfbb76SRobert Watson 	"Slow start flight size for local networks");
105df8bae1dSRodney W. Grimes 
10682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_tso) = 1;
10782cea7e6SBjoern A. Zeeb #define	V_tcp_do_tso		VNET(tcp_do_tso)
108eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_RW,
109eddfbb76SRobert Watson 	&VNET_NAME(tcp_do_tso), 0,
110eddfbb76SRobert Watson 	"Enable TCP Segmentation Offload");
111b3c0f300SAndre Oppermann 
11282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autosndbuf) = 1;
11382cea7e6SBjoern A. Zeeb #define	V_tcp_do_autosndbuf	VNET(tcp_do_autosndbuf)
114eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_RW,
115eddfbb76SRobert Watson 	&VNET_NAME(tcp_do_autosndbuf), 0,
116eddfbb76SRobert Watson 	"Enable automatic send buffer sizing");
1176741ecf5SAndre Oppermann 
11882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_inc) = 8*1024;
11982cea7e6SBjoern A. Zeeb #define	V_tcp_autosndbuf_inc	VNET(tcp_autosndbuf_inc)
120eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_RW,
121eddfbb76SRobert Watson 	&VNET_NAME(tcp_autosndbuf_inc), 0,
1228b615593SMarko Zec 	"Incrementor step size of automatic send buffer");
1236741ecf5SAndre Oppermann 
12482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_max) = 256*1024;
12582cea7e6SBjoern A. Zeeb #define	V_tcp_autosndbuf_max	VNET(tcp_autosndbuf_max)
126eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_RW,
127eddfbb76SRobert Watson 	&VNET_NAME(tcp_autosndbuf_max), 0,
1288b615593SMarko Zec 	"Max size of automatic send buffer");
1296741ecf5SAndre Oppermann 
13039bc9de5SLawrence Stewart static void inline	hhook_run_tcp_est_out(struct tcpcb *tp,
13139bc9de5SLawrence Stewart 			    struct tcphdr *th, struct tcpopt *to,
13239bc9de5SLawrence Stewart 			    long len, int tso);
133dbc42409SLawrence Stewart static void inline	cc_after_idle(struct tcpcb *tp);
134dbc42409SLawrence Stewart 
135dbc42409SLawrence Stewart /*
13639bc9de5SLawrence Stewart  * Wrapper for the TCP established ouput helper hook.
13739bc9de5SLawrence Stewart  */
13839bc9de5SLawrence Stewart static void inline
13939bc9de5SLawrence Stewart hhook_run_tcp_est_out(struct tcpcb *tp, struct tcphdr *th,
14039bc9de5SLawrence Stewart     struct tcpopt *to, long len, int tso)
14139bc9de5SLawrence Stewart {
14239bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
14339bc9de5SLawrence Stewart 
14439bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_OUT]->hhh_nhooks > 0) {
14539bc9de5SLawrence Stewart 		hhook_data.tp = tp;
14639bc9de5SLawrence Stewart 		hhook_data.th = th;
14739bc9de5SLawrence Stewart 		hhook_data.to = to;
14839bc9de5SLawrence Stewart 		hhook_data.len = len;
14939bc9de5SLawrence Stewart 		hhook_data.tso = tso;
15039bc9de5SLawrence Stewart 
15139bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_OUT], &hhook_data,
15239bc9de5SLawrence Stewart 		    tp->osd);
15339bc9de5SLawrence Stewart 	}
15439bc9de5SLawrence Stewart }
15539bc9de5SLawrence Stewart 
15639bc9de5SLawrence Stewart /*
157dbc42409SLawrence Stewart  * CC wrapper hook functions
158dbc42409SLawrence Stewart  */
159dbc42409SLawrence Stewart static void inline
160dbc42409SLawrence Stewart cc_after_idle(struct tcpcb *tp)
161dbc42409SLawrence Stewart {
162dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
163dbc42409SLawrence Stewart 
164dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->after_idle != NULL)
165dbc42409SLawrence Stewart 		CC_ALGO(tp)->after_idle(tp->ccv);
166dbc42409SLawrence Stewart }
1676741ecf5SAndre Oppermann 
168df8bae1dSRodney W. Grimes /*
169df8bae1dSRodney W. Grimes  * Tcp output routine: figure out what should be sent and send it.
170df8bae1dSRodney W. Grimes  */
171df8bae1dSRodney W. Grimes int
172f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp)
173df8bae1dSRodney W. Grimes {
174f10e85d7SLuigi Rizzo 	struct socket *so = tp->t_inpcb->inp_socket;
175201d185bSAndre Oppermann 	long len, recwin, sendwin;
176*b287c6c7SBjoern A. Zeeb 	int off, flags, error = 0;	/* Keep compiler happy */
177f10e85d7SLuigi Rizzo 	struct mbuf *m;
178fb59c426SYoshinobu Inoue 	struct ip *ip = NULL;
179f10e85d7SLuigi Rizzo 	struct ipovly *ipov = NULL;
180f10e85d7SLuigi Rizzo 	struct tcphdr *th;
181a0292f23SGarrett Wollman 	u_char opt[TCP_MAXOLEN];
182a04884fcSBill Fenner 	unsigned ipoptlen, optlen, hdrlen;
1839ad0173dSBjoern A. Zeeb #ifdef IPSEC
1849ad0173dSBjoern A. Zeeb 	unsigned ipsec_optlen = 0;
1859ad0173dSBjoern A. Zeeb #endif
186df8bae1dSRodney W. Grimes 	int idle, sendalot;
18702a1a643SAndre Oppermann 	int sack_rxmit, sack_bytes_rxmt;
1886d90faf3SPaul Saab 	struct sackhole *p;
189153e5b57SAndre Oppermann 	int tso;
19002a1a643SAndre Oppermann 	struct tcpopt to;
191262c1c1aSMatthew Dillon #if 0
19246f58482SJonathan Lemon 	int maxburst = TCP_MAXBURST;
193262c1c1aSMatthew Dillon #endif
194fb59c426SYoshinobu Inoue #ifdef INET6
195f10e85d7SLuigi Rizzo 	struct ip6_hdr *ip6 = NULL;
196fb59c426SYoshinobu Inoue 	int isipv6;
197fb59c426SYoshinobu Inoue 
198fb59c426SYoshinobu Inoue 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
199fb59c426SYoshinobu Inoue #endif
200df8bae1dSRodney W. Grimes 
2018501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2023d6ade3aSJennifer Yang 
203df8bae1dSRodney W. Grimes 	/*
204df8bae1dSRodney W. Grimes 	 * Determine length of data that should be transmitted,
205df8bae1dSRodney W. Grimes 	 * and flags that will be used.
206df8bae1dSRodney W. Grimes 	 * If there is some data or critical controls (SYN, RST)
207df8bae1dSRodney W. Grimes 	 * to send, then transmit; otherwise, investigate further.
208df8bae1dSRodney W. Grimes 	 */
209c24d5daeSJayanth Vijayaraghavan 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
2102ea8da28SLawrence Stewart 	if (idle && ticks - tp->t_rcvtime >= tp->t_rxtcur)
2112ea8da28SLawrence Stewart 		cc_after_idle(tp);
212c24d5daeSJayanth Vijayaraghavan 	tp->t_flags &= ~TF_LASTIDLE;
213c24d5daeSJayanth Vijayaraghavan 	if (idle) {
214c24d5daeSJayanth Vijayaraghavan 		if (tp->t_flags & TF_MORETOCOME) {
215c24d5daeSJayanth Vijayaraghavan 			tp->t_flags |= TF_LASTIDLE;
216c24d5daeSJayanth Vijayaraghavan 			idle = 0;
217c24d5daeSJayanth Vijayaraghavan 		}
218c24d5daeSJayanth Vijayaraghavan 	}
219df8bae1dSRodney W. Grimes again:
2206d90faf3SPaul Saab 	/*
2216d90faf3SPaul Saab 	 * If we've recently taken a timeout, snd_max will be greater than
2226d90faf3SPaul Saab 	 * snd_nxt.  There may be SACK information that allows us to avoid
2236d90faf3SPaul Saab 	 * resending already delivered data.  Adjust snd_nxt accordingly.
2246d90faf3SPaul Saab 	 */
2253529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
2263529149eSAndre Oppermann 	    SEQ_LT(tp->snd_nxt, tp->snd_max))
2276d90faf3SPaul Saab 		tcp_sack_adjust(tp);
228df8bae1dSRodney W. Grimes 	sendalot = 0;
229153e5b57SAndre Oppermann 	tso = 0;
230df8bae1dSRodney W. Grimes 	off = tp->snd_nxt - tp->snd_una;
231201d185bSAndre Oppermann 	sendwin = min(tp->snd_wnd, tp->snd_cwnd);
232df8bae1dSRodney W. Grimes 
233df8bae1dSRodney W. Grimes 	flags = tcp_outflags[tp->t_state];
234a0292f23SGarrett Wollman 	/*
2356d90faf3SPaul Saab 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
2366d90faf3SPaul Saab 	 * to send out new data (when sendalot is 1), bypass this function.
2376d90faf3SPaul Saab 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
2386d90faf3SPaul Saab 	 * we're replacing a (future) new transmission with a retransmission
2396d90faf3SPaul Saab 	 * now, and we previously incremented snd_cwnd in tcp_input().
2406d90faf3SPaul Saab 	 */
2416d90faf3SPaul Saab 	/*
2426d90faf3SPaul Saab 	 * Still in sack recovery , reset rxmit flag to zero.
2436d90faf3SPaul Saab 	 */
2446d90faf3SPaul Saab 	sack_rxmit = 0;
245a55db2b6SPaul Saab 	sack_bytes_rxmt = 0;
2466d90faf3SPaul Saab 	len = 0;
2476d90faf3SPaul Saab 	p = NULL;
248dbc42409SLawrence Stewart 	if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp->t_flags) &&
249a55db2b6SPaul Saab 	    (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
250a55db2b6SPaul Saab 		long cwin;
251a55db2b6SPaul Saab 
252a55db2b6SPaul Saab 		cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
253a55db2b6SPaul Saab 		if (cwin < 0)
254a55db2b6SPaul Saab 			cwin = 0;
255f787edd8SJayanth Vijayaraghavan 		/* Do not retransmit SACK segments beyond snd_recover */
256f787edd8SJayanth Vijayaraghavan 		if (SEQ_GT(p->end, tp->snd_recover)) {
257f787edd8SJayanth Vijayaraghavan 			/*
258f787edd8SJayanth Vijayaraghavan 			 * (At least) part of sack hole extends beyond
259f787edd8SJayanth Vijayaraghavan 			 * snd_recover. Check to see if we can rexmit data
260f787edd8SJayanth Vijayaraghavan 			 * for this hole.
261f787edd8SJayanth Vijayaraghavan 			 */
262f787edd8SJayanth Vijayaraghavan 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
263f787edd8SJayanth Vijayaraghavan 				/*
264f787edd8SJayanth Vijayaraghavan 				 * Can't rexmit any more data for this hole.
265f787edd8SJayanth Vijayaraghavan 				 * That data will be rexmitted in the next
266f787edd8SJayanth Vijayaraghavan 				 * sack recovery episode, when snd_recover
267f787edd8SJayanth Vijayaraghavan 				 * moves past p->rxmit.
268f787edd8SJayanth Vijayaraghavan 				 */
269f787edd8SJayanth Vijayaraghavan 				p = NULL;
270f787edd8SJayanth Vijayaraghavan 				goto after_sack_rexmit;
271f787edd8SJayanth Vijayaraghavan 			} else
272f787edd8SJayanth Vijayaraghavan 				/* Can rexmit part of the current hole */
273a55db2b6SPaul Saab 				len = ((long)ulmin(cwin,
274f787edd8SJayanth Vijayaraghavan 						   tp->snd_recover - p->rxmit));
275f787edd8SJayanth Vijayaraghavan 		} else
276a55db2b6SPaul Saab 			len = ((long)ulmin(cwin, p->end - p->rxmit));
2776d90faf3SPaul Saab 		off = p->rxmit - tp->snd_una;
278f787edd8SJayanth Vijayaraghavan 		KASSERT(off >= 0,("%s: sack block to the left of una : %d",
279f787edd8SJayanth Vijayaraghavan 		    __func__, off));
2806d90faf3SPaul Saab 		if (len > 0) {
281ab5c14d8SRobert Watson 			sack_rxmit = 1;
282ab5c14d8SRobert Watson 			sendalot = 1;
28378b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rexmits);
28478b50714SRobert Watson 			TCPSTAT_ADD(tcps_sack_rexmit_bytes,
28578b50714SRobert Watson 			    min(len, tp->t_maxseg));
2866d90faf3SPaul Saab 		}
2876d90faf3SPaul Saab 	}
288f787edd8SJayanth Vijayaraghavan after_sack_rexmit:
2896d90faf3SPaul Saab 	/*
290a0292f23SGarrett Wollman 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
291a0292f23SGarrett Wollman 	 * state flags.
292a0292f23SGarrett Wollman 	 */
293a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDFIN)
294a0292f23SGarrett Wollman 		flags |= TH_FIN;
295a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDSYN)
296a0292f23SGarrett Wollman 		flags |= TH_SYN;
297a0292f23SGarrett Wollman 
298cf2942b6SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
299df8bae1dSRodney W. Grimes 	/*
300df8bae1dSRodney W. Grimes 	 * If in persist timeout with window of 0, send 1 byte.
301df8bae1dSRodney W. Grimes 	 * Otherwise, if window is small but nonzero
302df8bae1dSRodney W. Grimes 	 * and timer expired, we will send what we can
303df8bae1dSRodney W. Grimes 	 * and go to transmit state.
304df8bae1dSRodney W. Grimes 	 */
3052cdbfa66SPaul Saab 	if (tp->t_flags & TF_FORCEDATA) {
306201d185bSAndre Oppermann 		if (sendwin == 0) {
307df8bae1dSRodney W. Grimes 			/*
308df8bae1dSRodney W. Grimes 			 * If we still have some data to send, then
309df8bae1dSRodney W. Grimes 			 * clear the FIN bit.  Usually this would
310df8bae1dSRodney W. Grimes 			 * happen below when it realizes that we
311df8bae1dSRodney W. Grimes 			 * aren't sending all the data.  However,
31229089b51SJulian Elischer 			 * if we have exactly 1 byte of unsent data,
313df8bae1dSRodney W. Grimes 			 * then it won't clear the FIN bit below,
314df8bae1dSRodney W. Grimes 			 * and if we are in persist state, we wind
315df8bae1dSRodney W. Grimes 			 * up sending the packet without recording
316df8bae1dSRodney W. Grimes 			 * that we sent the FIN bit.
317df8bae1dSRodney W. Grimes 			 *
318df8bae1dSRodney W. Grimes 			 * We can't just blindly clear the FIN bit,
319df8bae1dSRodney W. Grimes 			 * because if we don't have any more data
320df8bae1dSRodney W. Grimes 			 * to send then the probe will be the FIN
321df8bae1dSRodney W. Grimes 			 * itself.
322df8bae1dSRodney W. Grimes 			 */
323df8bae1dSRodney W. Grimes 			if (off < so->so_snd.sb_cc)
324df8bae1dSRodney W. Grimes 				flags &= ~TH_FIN;
325201d185bSAndre Oppermann 			sendwin = 1;
326df8bae1dSRodney W. Grimes 		} else {
327b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_PERSIST, 0);
328df8bae1dSRodney W. Grimes 			tp->t_rxtshift = 0;
329df8bae1dSRodney W. Grimes 		}
330df8bae1dSRodney W. Grimes 	}
331df8bae1dSRodney W. Grimes 
33228257b5cSMatthew Dillon 	/*
33328257b5cSMatthew Dillon 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
33428257b5cSMatthew Dillon 	 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
335201d185bSAndre Oppermann 	 * a negative length.  This can also occur when TCP opens up
33628257b5cSMatthew Dillon 	 * its congestion window while receiving additional duplicate
33728257b5cSMatthew Dillon 	 * acks after fast-retransmit because TCP will reset snd_nxt
33828257b5cSMatthew Dillon 	 * to snd_max after the fast-retransmit.
33928257b5cSMatthew Dillon 	 *
34028257b5cSMatthew Dillon 	 * In the normal retransmit-FIN-only case, however, snd_nxt will
34128257b5cSMatthew Dillon 	 * be set to snd_una, the offset will be 0, and the length may
34228257b5cSMatthew Dillon 	 * wind up 0.
3436d90faf3SPaul Saab 	 *
3446d90faf3SPaul Saab 	 * If sack_rxmit is true we are retransmitting from the scoreboard
3456d90faf3SPaul Saab 	 * in which case len is already set.
34628257b5cSMatthew Dillon 	 */
347a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
348a55db2b6SPaul Saab 		if (sack_bytes_rxmt == 0)
3496d90faf3SPaul Saab 			len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
350a55db2b6SPaul Saab 		else {
351a55db2b6SPaul Saab 			long cwin;
352a55db2b6SPaul Saab 
353a55db2b6SPaul Saab                         /*
354a55db2b6SPaul Saab 			 * We are inside of a SACK recovery episode and are
355a55db2b6SPaul Saab 			 * sending new data, having retransmitted all the
356a55db2b6SPaul Saab 			 * data possible in the scoreboard.
357a55db2b6SPaul Saab 			 */
3587d5ed1ceSPaul Saab 			len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd)
3597d5ed1ceSPaul Saab 			       - off);
3608d03f2b5SPaul Saab 			/*
3618d03f2b5SPaul Saab 			 * Don't remove this (len > 0) check !
3628d03f2b5SPaul Saab 			 * We explicitly check for len > 0 here (although it
3638d03f2b5SPaul Saab 			 * isn't really necessary), to work around a gcc
3648d03f2b5SPaul Saab 			 * optimization issue - to force gcc to compute
3658d03f2b5SPaul Saab 			 * len above. Without this check, the computation
3668d03f2b5SPaul Saab 			 * of len is bungled by the optimizer.
3678d03f2b5SPaul Saab 			 */
3688d03f2b5SPaul Saab 			if (len > 0) {
3697d5ed1ceSPaul Saab 				cwin = tp->snd_cwnd -
3707d5ed1ceSPaul Saab 					(tp->snd_nxt - tp->sack_newdata) -
371a55db2b6SPaul Saab 					sack_bytes_rxmt;
372a55db2b6SPaul Saab 				if (cwin < 0)
373a55db2b6SPaul Saab 					cwin = 0;
374a55db2b6SPaul Saab 				len = lmin(len, cwin);
375a55db2b6SPaul Saab 			}
376a55db2b6SPaul Saab 		}
3778d03f2b5SPaul Saab 	}
378a0292f23SGarrett Wollman 
379a0292f23SGarrett Wollman 	/*
380a0292f23SGarrett Wollman 	 * Lop off SYN bit if it has already been sent.  However, if this
381a0292f23SGarrett Wollman 	 * is SYN-SENT state and if segment contains data and if we don't
382a0292f23SGarrett Wollman 	 * know that foreign host supports TAO, suppress sending segment.
383a0292f23SGarrett Wollman 	 */
384a0292f23SGarrett Wollman 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
3854b8e98d6SQing Li 		if (tp->t_state != TCPS_SYN_RECEIVED)
386a0292f23SGarrett Wollman 			flags &= ~TH_SYN;
387a0292f23SGarrett Wollman 		off--, len++;
388a0292f23SGarrett Wollman 	}
389a0292f23SGarrett Wollman 
39081165e48SAndras Olah 	/*
391c94c54e4SAndre Oppermann 	 * Be careful not to send data and/or FIN on SYN segments.
39281165e48SAndras Olah 	 * This measure is needed to prevent interoperability problems
39381165e48SAndras Olah 	 * with not fully conformant TCP implementations.
39481165e48SAndras Olah 	 */
395c94c54e4SAndre Oppermann 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
39681165e48SAndras Olah 		len = 0;
39781165e48SAndras Olah 		flags &= ~TH_FIN;
39881165e48SAndras Olah 	}
39981165e48SAndras Olah 
400df8bae1dSRodney W. Grimes 	if (len < 0) {
401df8bae1dSRodney W. Grimes 		/*
402df8bae1dSRodney W. Grimes 		 * If FIN has been sent but not acked,
403df8bae1dSRodney W. Grimes 		 * but we haven't been called to retransmit,
40428257b5cSMatthew Dillon 		 * len will be < 0.  Otherwise, window shrank
405df8bae1dSRodney W. Grimes 		 * after we sent into it.  If window shrank to 0,
4062d8266afSDavid Greenman 		 * cancel pending retransmit, pull snd_nxt back
4072d8266afSDavid Greenman 		 * to (closed) window, and set the persist timer
4082d8266afSDavid Greenman 		 * if it isn't already going.  If the window didn't
4092d8266afSDavid Greenman 		 * close completely, just wait for an ACK.
410df8bae1dSRodney W. Grimes 		 */
411df8bae1dSRodney W. Grimes 		len = 0;
412201d185bSAndre Oppermann 		if (sendwin == 0) {
413b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
4142d8266afSDavid Greenman 			tp->t_rxtshift = 0;
415df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
416b8152ba7SAndre Oppermann 			if (!tcp_timer_active(tp, TT_PERSIST))
4172d8266afSDavid Greenman 				tcp_setpersist(tp);
418df8bae1dSRodney W. Grimes 		}
419df8bae1dSRodney W. Grimes 	}
42028257b5cSMatthew Dillon 
4216741ecf5SAndre Oppermann 	/* len will be >= 0 after this point. */
422c4982faeSBjoern A. Zeeb 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
4236741ecf5SAndre Oppermann 
42428257b5cSMatthew Dillon 	/*
4256741ecf5SAndre Oppermann 	 * Automatic sizing of send socket buffer.  Often the send buffer
4266741ecf5SAndre Oppermann 	 * size is not optimally adjusted to the actual network conditions
4276741ecf5SAndre Oppermann 	 * at hand (delay bandwidth product).  Setting the buffer size too
4286741ecf5SAndre Oppermann 	 * small limits throughput on links with high bandwidth and high
4296741ecf5SAndre Oppermann 	 * delay (eg. trans-continental/oceanic links).  Setting the
4306741ecf5SAndre Oppermann 	 * buffer size too big consumes too much real kernel memory,
4316741ecf5SAndre Oppermann 	 * especially with many connections on busy servers.
4326741ecf5SAndre Oppermann 	 *
4336741ecf5SAndre Oppermann 	 * The criteria to step up the send buffer one notch are:
4346741ecf5SAndre Oppermann 	 *  1. receive window of remote host is larger than send buffer
4356741ecf5SAndre Oppermann 	 *     (with a fudge factor of 5/4th);
4366741ecf5SAndre Oppermann 	 *  2. send buffer is filled to 7/8th with data (so we actually
4376741ecf5SAndre Oppermann 	 *     have data to make use of it);
4386741ecf5SAndre Oppermann 	 *  3. send buffer fill has not hit maximal automatic size;
4396741ecf5SAndre Oppermann 	 *  4. our send window (slow start and cogestion controlled) is
4406741ecf5SAndre Oppermann 	 *     larger than sent but unacknowledged data in send buffer.
4416741ecf5SAndre Oppermann 	 *
4426741ecf5SAndre Oppermann 	 * The remote host receive window scaling factor may limit the
4436741ecf5SAndre Oppermann 	 * growing of the send buffer before it reaches its allowed
4446741ecf5SAndre Oppermann 	 * maximum.
4456741ecf5SAndre Oppermann 	 *
4466741ecf5SAndre Oppermann 	 * It scales directly with slow start or congestion window
4476741ecf5SAndre Oppermann 	 * and does at most one step per received ACK.  This fast
4486741ecf5SAndre Oppermann 	 * scaling has the drawback of growing the send buffer beyond
4496741ecf5SAndre Oppermann 	 * what is strictly necessary to make full use of a given
4506741ecf5SAndre Oppermann 	 * delay*bandwith product.  However testing has shown this not
4516741ecf5SAndre Oppermann 	 * to be much of an problem.  At worst we are trading wasting
4526741ecf5SAndre Oppermann 	 * of available bandwith (the non-use of it) for wasting some
4536741ecf5SAndre Oppermann 	 * socket buffer memory.
4546741ecf5SAndre Oppermann 	 *
4556741ecf5SAndre Oppermann 	 * TODO: Shrink send buffer during idle periods together
4566741ecf5SAndre Oppermann 	 * with congestion window.  Requires another timer.  Has to
4576741ecf5SAndre Oppermann 	 * wait for upcoming tcp timer rewrite.
4586741ecf5SAndre Oppermann 	 */
459603724d3SBjoern A. Zeeb 	if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
4606741ecf5SAndre Oppermann 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
4616741ecf5SAndre Oppermann 		    so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
462603724d3SBjoern A. Zeeb 		    so->so_snd.sb_cc < V_tcp_autosndbuf_max &&
4636741ecf5SAndre Oppermann 		    sendwin >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
4646741ecf5SAndre Oppermann 			if (!sbreserve_locked(&so->so_snd,
465603724d3SBjoern A. Zeeb 			    min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc,
466603724d3SBjoern A. Zeeb 			     V_tcp_autosndbuf_max), so, curthread))
4676741ecf5SAndre Oppermann 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
4686741ecf5SAndre Oppermann 		}
4696741ecf5SAndre Oppermann 	}
4706741ecf5SAndre Oppermann 
4716741ecf5SAndre Oppermann 	/*
472ed420311SAndre Oppermann 	 * Decide if we can use TCP Segmentation Offloading (if supported by
473ed420311SAndre Oppermann 	 * hardware).
474b3c0f300SAndre Oppermann 	 *
475b3c0f300SAndre Oppermann 	 * TSO may only be used if we are in a pure bulk sending state.  The
476b3c0f300SAndre Oppermann 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and
477b3c0f300SAndre Oppermann 	 * IP options prevent using TSO.  With TSO the TCP header is the same
478b3c0f300SAndre Oppermann 	 * (except for the sequence number) for all generated packets.  This
479b3c0f300SAndre Oppermann 	 * makes it impossible to transmit any options which vary per generated
480b3c0f300SAndre Oppermann 	 * segment or packet.
48128257b5cSMatthew Dillon 	 */
4829ad0173dSBjoern A. Zeeb #ifdef IPSEC
4839ad0173dSBjoern A. Zeeb 	/*
4849ad0173dSBjoern A. Zeeb 	 * Pre-calculate here as we save another lookup into the darknesses
4859ad0173dSBjoern A. Zeeb 	 * of IPsec that way and can actually decide if TSO is ok.
4869ad0173dSBjoern A. Zeeb 	 */
4879ad0173dSBjoern A. Zeeb 	ipsec_optlen = ipsec_hdrsiz_tcp(tp);
4889ad0173dSBjoern A. Zeeb #endif
489ed420311SAndre Oppermann 	if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg &&
490b3c0f300SAndre Oppermann 	    ((tp->t_flags & TF_SIGNATURE) == 0) &&
491b3c0f300SAndre Oppermann 	    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
4929ad0173dSBjoern A. Zeeb #ifdef IPSEC
493ed420311SAndre Oppermann 	    ipsec_optlen == 0 &&
4949ad0173dSBjoern A. Zeeb #endif
495ed420311SAndre Oppermann 	    tp->t_inpcb->inp_options == NULL &&
496ed420311SAndre Oppermann 	    tp->t_inpcb->in6p_options == NULL)
497b3c0f300SAndre Oppermann 		tso = 1;
498153e5b57SAndre Oppermann 
4995d3b1b75SJayanth Vijayaraghavan 	if (sack_rxmit) {
5005d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
501df8bae1dSRodney W. Grimes 			flags &= ~TH_FIN;
5025d3b1b75SJayanth Vijayaraghavan 	} else {
5035d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
5045d3b1b75SJayanth Vijayaraghavan 			flags &= ~TH_FIN;
5055d3b1b75SJayanth Vijayaraghavan 	}
506df8bae1dSRodney W. Grimes 
507201d185bSAndre Oppermann 	recwin = sbspace(&so->so_rcv);
508df8bae1dSRodney W. Grimes 
509df8bae1dSRodney W. Grimes 	/*
510262c1c1aSMatthew Dillon 	 * Sender silly window avoidance.   We transmit under the following
511262c1c1aSMatthew Dillon 	 * conditions when len is non-zero:
512262c1c1aSMatthew Dillon 	 *
513b3c0f300SAndre Oppermann 	 *	- We have a full segment (or more with TSO)
514262c1c1aSMatthew Dillon 	 *	- This is the last buffer in a write()/send() and we are
515262c1c1aSMatthew Dillon 	 *	  either idle or running NODELAY
516262c1c1aSMatthew Dillon 	 *	- we've timed out (e.g. persist timer)
517262c1c1aSMatthew Dillon 	 *	- we have more then 1/2 the maximum send window's worth of
518262c1c1aSMatthew Dillon 	 *	  data (receiver may be limited the window size)
519262c1c1aSMatthew Dillon 	 *	- we need to retransmit
520df8bae1dSRodney W. Grimes 	 */
521df8bae1dSRodney W. Grimes 	if (len) {
522b3c0f300SAndre Oppermann 		if (len >= tp->t_maxseg)
523df8bae1dSRodney W. Grimes 			goto send;
524262c1c1aSMatthew Dillon 		/*
525262c1c1aSMatthew Dillon 		 * NOTE! on localhost connections an 'ack' from the remote
526262c1c1aSMatthew Dillon 		 * end may occur synchronously with the output and cause
527262c1c1aSMatthew Dillon 		 * us to flush a buffer queued with moretocome.  XXX
528262c1c1aSMatthew Dillon 		 *
529262c1c1aSMatthew Dillon 		 * note: the len + off check is almost certainly unnecessary.
530262c1c1aSMatthew Dillon 		 */
531262c1c1aSMatthew Dillon 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
532262c1c1aSMatthew Dillon 		    (idle || (tp->t_flags & TF_NODELAY)) &&
533262c1c1aSMatthew Dillon 		    len + off >= so->so_snd.sb_cc &&
534262c1c1aSMatthew Dillon 		    (tp->t_flags & TF_NOPUSH) == 0) {
535df8bae1dSRodney W. Grimes 			goto send;
536262c1c1aSMatthew Dillon 		}
5372cdbfa66SPaul Saab 		if (tp->t_flags & TF_FORCEDATA)		/* typ. timeout case */
538df8bae1dSRodney W. Grimes 			goto send;
539a0292f23SGarrett Wollman 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
540df8bae1dSRodney W. Grimes 			goto send;
541262c1c1aSMatthew Dillon 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))	/* retransmit case */
542df8bae1dSRodney W. Grimes 			goto send;
5436d90faf3SPaul Saab 		if (sack_rxmit)
5446d90faf3SPaul Saab 			goto send;
545df8bae1dSRodney W. Grimes 	}
546df8bae1dSRodney W. Grimes 
547df8bae1dSRodney W. Grimes 	/*
548df8bae1dSRodney W. Grimes 	 * Compare available window to amount of window
549df8bae1dSRodney W. Grimes 	 * known to peer (as advertised window less
550df8bae1dSRodney W. Grimes 	 * next expected input).  If the difference is at least two
551df8bae1dSRodney W. Grimes 	 * max size segments, or at least 50% of the maximum possible
552df8bae1dSRodney W. Grimes 	 * window, then want to send a window update to peer.
5533bfd6421SJonathan Lemon 	 * Skip this if the connection is in T/TCP half-open state.
554b7de7d87SAndre Oppermann 	 * Don't send pure window updates when the peer has closed
555b7de7d87SAndre Oppermann 	 * the connection and won't ever send more data.
556df8bae1dSRodney W. Grimes 	 */
557b7de7d87SAndre Oppermann 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
558b7de7d87SAndre Oppermann 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
559df8bae1dSRodney W. Grimes 		/*
560df8bae1dSRodney W. Grimes 		 * "adv" is the amount we can increase the window,
561df8bae1dSRodney W. Grimes 		 * taking into account that we are limited by
562df8bae1dSRodney W. Grimes 		 * TCP_MAXWIN << tp->rcv_scale.
563df8bae1dSRodney W. Grimes 		 */
564201d185bSAndre Oppermann 		long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
565df8bae1dSRodney W. Grimes 			(tp->rcv_adv - tp->rcv_nxt);
566df8bae1dSRodney W. Grimes 
567da84b2e6SJohn Baldwin 		/*
568da84b2e6SJohn Baldwin 		 * If the new window size ends up being the same as the old
569da84b2e6SJohn Baldwin 		 * size when it is scaled, then don't force a window update.
570da84b2e6SJohn Baldwin 		 */
571da84b2e6SJohn Baldwin 		if ((tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale ==
572da84b2e6SJohn Baldwin 		    (adv + tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale)
573da84b2e6SJohn Baldwin 			goto dontupdate;
574df8bae1dSRodney W. Grimes 		if (adv >= (long) (2 * tp->t_maxseg))
575df8bae1dSRodney W. Grimes 			goto send;
576df8bae1dSRodney W. Grimes 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
577df8bae1dSRodney W. Grimes 			goto send;
578df8bae1dSRodney W. Grimes 	}
579da84b2e6SJohn Baldwin dontupdate:
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes 	/*
58228257b5cSMatthew Dillon 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
58328257b5cSMatthew Dillon 	 * is also a catch-all for the retransmit timer timeout case.
584df8bae1dSRodney W. Grimes 	 */
585df8bae1dSRodney W. Grimes 	if (tp->t_flags & TF_ACKNOW)
586df8bae1dSRodney W. Grimes 		goto send;
587a0292f23SGarrett Wollman 	if ((flags & TH_RST) ||
588a0292f23SGarrett Wollman 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
589df8bae1dSRodney W. Grimes 		goto send;
590df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_una))
591df8bae1dSRodney W. Grimes 		goto send;
592df8bae1dSRodney W. Grimes 	/*
593df8bae1dSRodney W. Grimes 	 * If our state indicates that FIN should be sent
59428257b5cSMatthew Dillon 	 * and we have not yet done so, then we need to send.
595df8bae1dSRodney W. Grimes 	 */
596df8bae1dSRodney W. Grimes 	if (flags & TH_FIN &&
597df8bae1dSRodney W. Grimes 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
598df8bae1dSRodney W. Grimes 		goto send;
5996d90faf3SPaul Saab 	/*
6006d90faf3SPaul Saab 	 * In SACK, it is possible for tcp_output to fail to send a segment
6016d90faf3SPaul Saab 	 * after the retransmission timer has been turned off.  Make sure
6026d90faf3SPaul Saab 	 * that the retransmission timer is set.
6036d90faf3SPaul Saab 	 */
6043529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
6053529149eSAndre Oppermann 	    SEQ_GT(tp->snd_max, tp->snd_una) &&
606b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_REXMT) &&
607b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
608b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
609cf2942b6SRobert Watson 		goto just_return;
6106d90faf3SPaul Saab 	}
611df8bae1dSRodney W. Grimes 	/*
612df8bae1dSRodney W. Grimes 	 * TCP window updates are not reliable, rather a polling protocol
613df8bae1dSRodney W. Grimes 	 * using ``persist'' packets is used to insure receipt of window
614df8bae1dSRodney W. Grimes 	 * updates.  The three ``states'' for the output side are:
615df8bae1dSRodney W. Grimes 	 *	idle			not doing retransmits or persists
616df8bae1dSRodney W. Grimes 	 *	persisting		to move a small or zero window
617df8bae1dSRodney W. Grimes 	 *	(re)transmitting	and thereby not persisting
618df8bae1dSRodney W. Grimes 	 *
619b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_PERSIST)
6209b8b58e0SJonathan Lemon 	 *	is true when we are in persist state.
6212cdbfa66SPaul Saab 	 * (tp->t_flags & TF_FORCEDATA)
622df8bae1dSRodney W. Grimes 	 *	is set when we are called to send a persist packet.
623b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_REXMT)
624df8bae1dSRodney W. Grimes 	 *	is set when we are retransmitting
625df8bae1dSRodney W. Grimes 	 * The output side is idle when both timers are zero.
626df8bae1dSRodney W. Grimes 	 *
627df8bae1dSRodney W. Grimes 	 * If send window is too small, there is data to transmit, and no
628df8bae1dSRodney W. Grimes 	 * retransmit or persist is pending, then go to persist state.
629df8bae1dSRodney W. Grimes 	 * If nothing happens soon, send when timer expires:
630df8bae1dSRodney W. Grimes 	 * if window is nonzero, transmit what we can,
631df8bae1dSRodney W. Grimes 	 * otherwise force out a byte.
632df8bae1dSRodney W. Grimes 	 */
633b8152ba7SAndre Oppermann 	if (so->so_snd.sb_cc && !tcp_timer_active(tp, TT_REXMT) &&
634b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
635df8bae1dSRodney W. Grimes 		tp->t_rxtshift = 0;
636df8bae1dSRodney W. Grimes 		tcp_setpersist(tp);
637df8bae1dSRodney W. Grimes 	}
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes 	/*
640df8bae1dSRodney W. Grimes 	 * No reason to send a segment, just return.
641df8bae1dSRodney W. Grimes 	 */
642cf2942b6SRobert Watson just_return:
643cf2942b6SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
644df8bae1dSRodney W. Grimes 	return (0);
645df8bae1dSRodney W. Grimes 
646df8bae1dSRodney W. Grimes send:
647cf2942b6SRobert Watson 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
648df8bae1dSRodney W. Grimes 	/*
649df8bae1dSRodney W. Grimes 	 * Before ESTABLISHED, force sending of initial options
650df8bae1dSRodney W. Grimes 	 * unless TCP set not to do any options.
651df8bae1dSRodney W. Grimes 	 * NOTE: we assume that the IP/TCP header plus TCP options
652df8bae1dSRodney W. Grimes 	 * always fit in a single mbuf, leaving room for a maximum
653df8bae1dSRodney W. Grimes 	 * link header, i.e.
65433841545SHajimu UMEMOTO 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
655df8bae1dSRodney W. Grimes 	 */
656df8bae1dSRodney W. Grimes 	optlen = 0;
657fb59c426SYoshinobu Inoue #ifdef INET6
658fb59c426SYoshinobu Inoue 	if (isipv6)
659fb59c426SYoshinobu Inoue 		hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
660fb59c426SYoshinobu Inoue 	else
661fb59c426SYoshinobu Inoue #endif
662df8bae1dSRodney W. Grimes 		hdrlen = sizeof (struct tcpiphdr);
66302a1a643SAndre Oppermann 
66402a1a643SAndre Oppermann 	/*
66502a1a643SAndre Oppermann 	 * Compute options for segment.
66602a1a643SAndre Oppermann 	 * We only have to care about SYN and established connection
66702a1a643SAndre Oppermann 	 * segments.  Options for SYN-ACK segments are handled in TCP
66802a1a643SAndre Oppermann 	 * syncache.
66902a1a643SAndre Oppermann 	 */
67002a1a643SAndre Oppermann 	if ((tp->t_flags & TF_NOOPT) == 0) {
67102a1a643SAndre Oppermann 		to.to_flags = 0;
67202a1a643SAndre Oppermann 		/* Maximum segment size. */
673df8bae1dSRodney W. Grimes 		if (flags & TH_SYN) {
674df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->iss;
67502a1a643SAndre Oppermann 			to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc);
67602a1a643SAndre Oppermann 			to.to_flags |= TOF_MSS;
677df8bae1dSRodney W. Grimes 		}
67802a1a643SAndre Oppermann 		/* Window scaling. */
67902a1a643SAndre Oppermann 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
68002a1a643SAndre Oppermann 			to.to_wscale = tp->request_r_scale;
68102a1a643SAndre Oppermann 			to.to_flags |= TOF_SCALE;
682df8bae1dSRodney W. Grimes 		}
68302a1a643SAndre Oppermann 		/* Timestamps. */
68402a1a643SAndre Oppermann 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
68502a1a643SAndre Oppermann 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
68602a1a643SAndre Oppermann 			to.to_tsval = ticks + tp->ts_offset;
68702a1a643SAndre Oppermann 			to.to_tsecr = tp->ts_recent;
68802a1a643SAndre Oppermann 			to.to_flags |= TOF_TS;
6896741ecf5SAndre Oppermann 			/* Set receive buffer autosizing timestamp. */
69002a1a643SAndre Oppermann 			if (tp->rfbuf_ts == 0 &&
69102a1a643SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE))
6926741ecf5SAndre Oppermann 				tp->rfbuf_ts = ticks;
6931cfd4b53SBruce M Simpson 		}
69402a1a643SAndre Oppermann 		/* Selective ACK's. */
6953529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
69602a1a643SAndre Oppermann 			if (flags & TH_SYN)
69702a1a643SAndre Oppermann 				to.to_flags |= TOF_SACKPERM;
69802a1a643SAndre Oppermann 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
69902a1a643SAndre Oppermann 			    (tp->t_flags & TF_SACK_PERMIT) &&
70002a1a643SAndre Oppermann 			    tp->rcv_numsacks > 0) {
70102a1a643SAndre Oppermann 				to.to_flags |= TOF_SACK;
70202a1a643SAndre Oppermann 				to.to_nsacks = tp->rcv_numsacks;
70302a1a643SAndre Oppermann 				to.to_sacks = (u_char *)tp->sackblks;
70402a1a643SAndre Oppermann 			}
70502a1a643SAndre Oppermann 		}
70602a1a643SAndre Oppermann #ifdef TCP_SIGNATURE
70702a1a643SAndre Oppermann 		/* TCP-MD5 (RFC2385). */
70802a1a643SAndre Oppermann 		if (tp->t_flags & TF_SIGNATURE)
70902a1a643SAndre Oppermann 			to.to_flags |= TOF_SIGNATURE;
7101cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
7111cfd4b53SBruce M Simpson 
71202a1a643SAndre Oppermann 		/* Processing the options. */
7134a411b9fSBjoern A. Zeeb 		hdrlen += optlen = tcp_addoptions(&to, opt);
714be3f3b5eSPaul Saab 	}
715be3f3b5eSPaul Saab 
716fb59c426SYoshinobu Inoue #ifdef INET6
717fb59c426SYoshinobu Inoue 	if (isipv6)
718fb59c426SYoshinobu Inoue 		ipoptlen = ip6_optlen(tp->t_inpcb);
719fb59c426SYoshinobu Inoue 	else
720fb59c426SYoshinobu Inoue #endif
721f10e85d7SLuigi Rizzo 	if (tp->t_inpcb->inp_options)
722a04884fcSBill Fenner 		ipoptlen = tp->t_inpcb->inp_options->m_len -
723a04884fcSBill Fenner 				offsetof(struct ipoption, ipopt_list);
724f10e85d7SLuigi Rizzo 	else
725a04884fcSBill Fenner 		ipoptlen = 0;
726b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
7279ad0173dSBjoern A. Zeeb 	ipoptlen += ipsec_optlen;
728fb59c426SYoshinobu Inoue #endif
729a04884fcSBill Fenner 
730df8bae1dSRodney W. Grimes 	/*
731df8bae1dSRodney W. Grimes 	 * Adjust data length if insertion of options will
732a0292f23SGarrett Wollman 	 * bump the packet length beyond the t_maxopd length.
733a0292f23SGarrett Wollman 	 * Clear the FIN bit because we cut off the tail of
734a0292f23SGarrett Wollman 	 * the segment.
735df8bae1dSRodney W. Grimes 	 */
736a04884fcSBill Fenner 	if (len + optlen + ipoptlen > tp->t_maxopd) {
737297a37f3SDavid Greenman 		flags &= ~TH_FIN;
738ed420311SAndre Oppermann 
739b3c0f300SAndre Oppermann 		if (tso) {
740ed420311SAndre Oppermann 			KASSERT(ipoptlen == 0,
741ed420311SAndre Oppermann 			    ("%s: TSO can't do IP options", __func__));
742ed420311SAndre Oppermann 
743ed420311SAndre Oppermann 			/*
744ed420311SAndre Oppermann 			 * Limit a burst to IP_MAXPACKET minus IP,
745ed420311SAndre Oppermann 			 * TCP and options length to keep ip->ip_len
746ed420311SAndre Oppermann 			 * from overflowing.
747ed420311SAndre Oppermann 			 */
748ed420311SAndre Oppermann 			if (len > IP_MAXPACKET - hdrlen) {
749ed420311SAndre Oppermann 				len = IP_MAXPACKET - hdrlen;
750b3c0f300SAndre Oppermann 				sendalot = 1;
751ed420311SAndre Oppermann 			}
752ed420311SAndre Oppermann 
753ed420311SAndre Oppermann 			/*
754ed420311SAndre Oppermann 			 * Prevent the last segment from being
755ed420311SAndre Oppermann 			 * fractional unless the send sockbuf can
756ed420311SAndre Oppermann 			 * be emptied.
757ed420311SAndre Oppermann 			 */
758ed420311SAndre Oppermann 			if (sendalot && off + len < so->so_snd.sb_cc) {
759ed420311SAndre Oppermann 				len -= len % (tp->t_maxopd - optlen);
760b3c0f300SAndre Oppermann 				sendalot = 1;
761ed420311SAndre Oppermann 			}
762ed420311SAndre Oppermann 
763ed420311SAndre Oppermann 			/*
764ed420311SAndre Oppermann 			 * Send the FIN in a separate segment
765ed420311SAndre Oppermann 			 * after the bulk sending is done.
766ed420311SAndre Oppermann 			 * We don't trust the TSO implementations
767ed420311SAndre Oppermann 			 * to clear the FIN flag on all but the
768ed420311SAndre Oppermann 			 * last segment.
769ed420311SAndre Oppermann 			 */
770ed420311SAndre Oppermann 			if (tp->t_flags & TF_NEEDFIN)
771ed420311SAndre Oppermann 				sendalot = 1;
772ed420311SAndre Oppermann 
773b3c0f300SAndre Oppermann 		} else {
774a04884fcSBill Fenner 			len = tp->t_maxopd - optlen - ipoptlen;
775df8bae1dSRodney W. Grimes 			sendalot = 1;
776df8bae1dSRodney W. Grimes 		}
777ed420311SAndre Oppermann 	} else
778ed420311SAndre Oppermann 		tso = 0;
779ed420311SAndre Oppermann 
780ed420311SAndre Oppermann 	KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET,
781ed420311SAndre Oppermann 	    ("%s: len > IP_MAXPACKET", __func__));
782df8bae1dSRodney W. Grimes 
783a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/
784a683a7ddSYoshinobu Inoue #ifdef INET6
785a683a7ddSYoshinobu Inoue 	if (max_linkhdr + hdrlen > MCLBYTES)
786a683a7ddSYoshinobu Inoue #else
787df8bae1dSRodney W. Grimes 	if (max_linkhdr + hdrlen > MHLEN)
788a683a7ddSYoshinobu Inoue #endif
789f10e85d7SLuigi Rizzo 		panic("tcphdr too big");
790a0292f23SGarrett Wollman /*#endif*/
791df8bae1dSRodney W. Grimes 
792df8bae1dSRodney W. Grimes 	/*
793c4982faeSBjoern A. Zeeb 	 * This KASSERT is here to catch edge cases at a well defined place.
794c4982faeSBjoern A. Zeeb 	 * Before, those had triggered (random) panic conditions further down.
795c4982faeSBjoern A. Zeeb 	 */
796c4982faeSBjoern A. Zeeb 	KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__));
797c4982faeSBjoern A. Zeeb 
798c4982faeSBjoern A. Zeeb 	/*
799df8bae1dSRodney W. Grimes 	 * Grab a header mbuf, attaching a copy of data to
800df8bae1dSRodney W. Grimes 	 * be transmitted, and initialize the header from
801df8bae1dSRodney W. Grimes 	 * the template for sends on this connection.
802df8bae1dSRodney W. Grimes 	 */
803df8bae1dSRodney W. Grimes 	if (len) {
8044e023759SAndre Oppermann 		struct mbuf *mb;
8054e023759SAndre Oppermann 		u_int moff;
8064e023759SAndre Oppermann 
8072cdbfa66SPaul Saab 		if ((tp->t_flags & TF_FORCEDATA) && len == 1)
80878b50714SRobert Watson 			TCPSTAT_INC(tcps_sndprobe);
8090ba5d2eeSJohn Baldwin 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
810f5d34df5SGeorge V. Neville-Neil 			tp->t_sndrexmitpack++;
81178b50714SRobert Watson 			TCPSTAT_INC(tcps_sndrexmitpack);
81278b50714SRobert Watson 			TCPSTAT_ADD(tcps_sndrexmitbyte, len);
813df8bae1dSRodney W. Grimes 		} else {
81478b50714SRobert Watson 			TCPSTAT_INC(tcps_sndpack);
81578b50714SRobert Watson 			TCPSTAT_ADD(tcps_sndbyte, len);
816df8bae1dSRodney W. Grimes 		}
817df8bae1dSRodney W. Grimes #ifdef notyet
818df8bae1dSRodney W. Grimes 		if ((m = m_copypack(so->so_snd.sb_mb, off,
819df8bae1dSRodney W. Grimes 		    (int)len, max_linkhdr + hdrlen)) == 0) {
820cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
821df8bae1dSRodney W. Grimes 			error = ENOBUFS;
822df8bae1dSRodney W. Grimes 			goto out;
823df8bae1dSRodney W. Grimes 		}
824df8bae1dSRodney W. Grimes 		/*
825df8bae1dSRodney W. Grimes 		 * m_copypack left space for our hdr; use it.
826df8bae1dSRodney W. Grimes 		 */
827df8bae1dSRodney W. Grimes 		m->m_len += hdrlen;
828df8bae1dSRodney W. Grimes 		m->m_data -= hdrlen;
829df8bae1dSRodney W. Grimes #else
83034333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
831df8bae1dSRodney W. Grimes 		if (m == NULL) {
832cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
833df8bae1dSRodney W. Grimes 			error = ENOBUFS;
834df8bae1dSRodney W. Grimes 			goto out;
835df8bae1dSRodney W. Grimes 		}
836fb59c426SYoshinobu Inoue #ifdef INET6
837a683a7ddSYoshinobu Inoue 		if (MHLEN < hdrlen + max_linkhdr) {
838a163d034SWarner Losh 			MCLGET(m, M_DONTWAIT);
839a683a7ddSYoshinobu Inoue 			if ((m->m_flags & M_EXT) == 0) {
840cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
841a683a7ddSYoshinobu Inoue 				m_freem(m);
842a683a7ddSYoshinobu Inoue 				error = ENOBUFS;
843a683a7ddSYoshinobu Inoue 				goto out;
844a683a7ddSYoshinobu Inoue 			}
845a683a7ddSYoshinobu Inoue 		}
846fb59c426SYoshinobu Inoue #endif
847df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
848df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
8494e023759SAndre Oppermann 
8504e023759SAndre Oppermann 		/*
8514e023759SAndre Oppermann 		 * Start the m_copy functions from the closest mbuf
8524e023759SAndre Oppermann 		 * to the offset in the socket buffer chain.
8534e023759SAndre Oppermann 		 */
8544e023759SAndre Oppermann 		mb = sbsndptr(&so->so_snd, off, len, &moff);
8554e023759SAndre Oppermann 
856df8bae1dSRodney W. Grimes 		if (len <= MHLEN - hdrlen - max_linkhdr) {
8574e023759SAndre Oppermann 			m_copydata(mb, moff, (int)len,
858df8bae1dSRodney W. Grimes 			    mtod(m, caddr_t) + hdrlen);
859df8bae1dSRodney W. Grimes 			m->m_len += len;
860df8bae1dSRodney W. Grimes 		} else {
8614e023759SAndre Oppermann 			m->m_next = m_copy(mb, moff, (int)len);
8624e023759SAndre Oppermann 			if (m->m_next == NULL) {
863cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
864d7f570e6SGarrett Wollman 				(void) m_free(m);
86551823c3aSGarrett Wollman 				error = ENOBUFS;
86651823c3aSGarrett Wollman 				goto out;
86751823c3aSGarrett Wollman 			}
868df8bae1dSRodney W. Grimes 		}
869*b287c6c7SBjoern A. Zeeb #endif /* notyet */
870df8bae1dSRodney W. Grimes 		/*
871df8bae1dSRodney W. Grimes 		 * If we're sending everything we've got, set PUSH.
872df8bae1dSRodney W. Grimes 		 * (This will keep happy those implementations which only
873df8bae1dSRodney W. Grimes 		 * give data to the user when a buffer fills or
874df8bae1dSRodney W. Grimes 		 * a PUSH comes in.)
875df8bae1dSRodney W. Grimes 		 */
876df8bae1dSRodney W. Grimes 		if (off + len == so->so_snd.sb_cc)
877df8bae1dSRodney W. Grimes 			flags |= TH_PUSH;
878cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
879df8bae1dSRodney W. Grimes 	} else {
880cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
881df8bae1dSRodney W. Grimes 		if (tp->t_flags & TF_ACKNOW)
88278b50714SRobert Watson 			TCPSTAT_INC(tcps_sndacks);
883df8bae1dSRodney W. Grimes 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
88478b50714SRobert Watson 			TCPSTAT_INC(tcps_sndctrl);
885df8bae1dSRodney W. Grimes 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
88678b50714SRobert Watson 			TCPSTAT_INC(tcps_sndurg);
887df8bae1dSRodney W. Grimes 		else
88878b50714SRobert Watson 			TCPSTAT_INC(tcps_sndwinup);
889df8bae1dSRodney W. Grimes 
89034333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
891df8bae1dSRodney W. Grimes 		if (m == NULL) {
892df8bae1dSRodney W. Grimes 			error = ENOBUFS;
893df8bae1dSRodney W. Grimes 			goto out;
894df8bae1dSRodney W. Grimes 		}
895fb59c426SYoshinobu Inoue #ifdef INET6
896fb59c426SYoshinobu Inoue 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
897fb59c426SYoshinobu Inoue 		    MHLEN >= hdrlen) {
898fb59c426SYoshinobu Inoue 			MH_ALIGN(m, hdrlen);
899fb59c426SYoshinobu Inoue 		} else
900fb59c426SYoshinobu Inoue #endif
901df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
902df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
903df8bae1dSRodney W. Grimes 	}
904cf2942b6SRobert Watson 	SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
905df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = (struct ifnet *)0;
906c488362eSRobert Watson #ifdef MAC
90730d239bcSRobert Watson 	mac_inpcb_create_mbuf(tp->t_inpcb, m);
908c488362eSRobert Watson #endif
909fb59c426SYoshinobu Inoue #ifdef INET6
910fb59c426SYoshinobu Inoue 	if (isipv6) {
911fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
912fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip6 + 1);
91379909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip6, th);
914fb59c426SYoshinobu Inoue 	} else
915fb59c426SYoshinobu Inoue #endif /* INET6 */
916fb59c426SYoshinobu Inoue 	{
917fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
918fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
919fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip + 1);
92079909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip, th);
921fb59c426SYoshinobu Inoue 	}
922df8bae1dSRodney W. Grimes 
923df8bae1dSRodney W. Grimes 	/*
924df8bae1dSRodney W. Grimes 	 * Fill in fields, remembering maximum advertised
925df8bae1dSRodney W. Grimes 	 * window for use in delaying messages about window sizes.
926df8bae1dSRodney W. Grimes 	 * If resending a FIN, be sure not to use a new sequence number.
927df8bae1dSRodney W. Grimes 	 */
928df8bae1dSRodney W. Grimes 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
929df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max)
930df8bae1dSRodney W. Grimes 		tp->snd_nxt--;
931df8bae1dSRodney W. Grimes 	/*
932f2512ba1SRui Paulo 	 * If we are starting a connection, send ECN setup
933f2512ba1SRui Paulo 	 * SYN packet. If we are on a retransmit, we may
934f2512ba1SRui Paulo 	 * resend those bits a number of times as per
935f2512ba1SRui Paulo 	 * RFC 3168.
936f2512ba1SRui Paulo 	 */
937603724d3SBjoern A. Zeeb 	if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) {
938f2512ba1SRui Paulo 		if (tp->t_rxtshift >= 1) {
939603724d3SBjoern A. Zeeb 			if (tp->t_rxtshift <= V_tcp_ecn_maxretries)
940f2512ba1SRui Paulo 				flags |= TH_ECE|TH_CWR;
941f2512ba1SRui Paulo 		} else
942f2512ba1SRui Paulo 			flags |= TH_ECE|TH_CWR;
943f2512ba1SRui Paulo 	}
944f2512ba1SRui Paulo 
945f2512ba1SRui Paulo 	if (tp->t_state == TCPS_ESTABLISHED &&
946f2512ba1SRui Paulo 	    (tp->t_flags & TF_ECN_PERMIT)) {
947f2512ba1SRui Paulo 		/*
948f2512ba1SRui Paulo 		 * If the peer has ECN, mark data packets with
949f2512ba1SRui Paulo 		 * ECN capable transmission (ECT).
950f2512ba1SRui Paulo 		 * Ignore pure ack packets, retransmissions and window probes.
951f2512ba1SRui Paulo 		 */
952f2512ba1SRui Paulo 		if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
953f2512ba1SRui Paulo 		    !((tp->t_flags & TF_FORCEDATA) && len == 1)) {
954f2512ba1SRui Paulo #ifdef INET6
955f2512ba1SRui Paulo 			if (isipv6)
956f2512ba1SRui Paulo 				ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
957f2512ba1SRui Paulo 			else
958f2512ba1SRui Paulo #endif
959f2512ba1SRui Paulo 				ip->ip_tos |= IPTOS_ECN_ECT0;
96078b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
961f2512ba1SRui Paulo 		}
962f2512ba1SRui Paulo 
963f2512ba1SRui Paulo 		/*
964f2512ba1SRui Paulo 		 * Reply with proper ECN notifications.
965f2512ba1SRui Paulo 		 */
966f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_CWR) {
967f2512ba1SRui Paulo 			flags |= TH_CWR;
968f2512ba1SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_CWR;
969f2512ba1SRui Paulo 		}
970f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_ECE)
971f2512ba1SRui Paulo 			flags |= TH_ECE;
972f2512ba1SRui Paulo 	}
973f2512ba1SRui Paulo 
974f2512ba1SRui Paulo 	/*
975df8bae1dSRodney W. Grimes 	 * If we are doing retransmissions, then snd_nxt will
976df8bae1dSRodney W. Grimes 	 * not reflect the first unsent octet.  For ACK only
977df8bae1dSRodney W. Grimes 	 * packets, we do not want the sequence number of the
978df8bae1dSRodney W. Grimes 	 * retransmitted packet, we want the sequence number
979df8bae1dSRodney W. Grimes 	 * of the next unsent octet.  So, if there is no data
980df8bae1dSRodney W. Grimes 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
981df8bae1dSRodney W. Grimes 	 * when filling in ti_seq.  But if we are in persist
982df8bae1dSRodney W. Grimes 	 * state, snd_max might reflect one byte beyond the
983df8bae1dSRodney W. Grimes 	 * right edge of the window, so use snd_nxt in that
984df8bae1dSRodney W. Grimes 	 * case, since we know we aren't doing a retransmission.
985df8bae1dSRodney W. Grimes 	 * (retransmit and persist are mutually exclusive...)
986df8bae1dSRodney W. Grimes 	 */
987a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
988b8152ba7SAndre Oppermann 		if (len || (flags & (TH_SYN|TH_FIN)) ||
989b8152ba7SAndre Oppermann 		    tcp_timer_active(tp, TT_PERSIST))
990fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_nxt);
991df8bae1dSRodney W. Grimes 		else
992fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_max);
993a55db2b6SPaul Saab 	} else {
9946d90faf3SPaul Saab 		th->th_seq = htonl(p->rxmit);
9956d90faf3SPaul Saab 		p->rxmit += len;
9960077b016SPaul Saab 		tp->sackhint.sack_bytes_rexmit += len;
9976d90faf3SPaul Saab 	}
998fb59c426SYoshinobu Inoue 	th->th_ack = htonl(tp->rcv_nxt);
999df8bae1dSRodney W. Grimes 	if (optlen) {
1000fb59c426SYoshinobu Inoue 		bcopy(opt, th + 1, optlen);
1001fb59c426SYoshinobu Inoue 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
1002df8bae1dSRodney W. Grimes 	}
1003fb59c426SYoshinobu Inoue 	th->th_flags = flags;
1004df8bae1dSRodney W. Grimes 	/*
1005df8bae1dSRodney W. Grimes 	 * Calculate receive window.  Don't shrink window,
1006df8bae1dSRodney W. Grimes 	 * but avoid silly window syndrome.
1007df8bae1dSRodney W. Grimes 	 */
1008201d185bSAndre Oppermann 	if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
1009201d185bSAndre Oppermann 	    recwin < (long)tp->t_maxseg)
1010201d185bSAndre Oppermann 		recwin = 0;
1011201d185bSAndre Oppermann 	if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
1012201d185bSAndre Oppermann 		recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
1013201d185bSAndre Oppermann 	if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
1014201d185bSAndre Oppermann 		recwin = (long)TCP_MAXWIN << tp->rcv_scale;
1015262c1c1aSMatthew Dillon 
1016104ebb2aSAndre Oppermann 	/*
1017104ebb2aSAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
1018104ebb2aSAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK>
1019104ebb2aSAndre Oppermann 	 * case is handled in syncache.
1020104ebb2aSAndre Oppermann 	 */
1021104ebb2aSAndre Oppermann 	if (flags & TH_SYN)
1022104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)
1023104ebb2aSAndre Oppermann 				(min(sbspace(&so->so_rcv), TCP_MAXWIN)));
1024104ebb2aSAndre Oppermann 	else
1025104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
1026262c1c1aSMatthew Dillon 
1027262c1c1aSMatthew Dillon 	/*
1028262c1c1aSMatthew Dillon 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised
1029262c1c1aSMatthew Dillon 	 * a 0 window.  This may cause the remote transmitter to stall.  This
1030262c1c1aSMatthew Dillon 	 * flag tells soreceive() to disable delayed acknowledgements when
1031262c1c1aSMatthew Dillon 	 * draining the buffer.  This can occur if the receiver is attempting
1032b2722702SRui Paulo 	 * to read more data than can be buffered prior to transmitting on
1033262c1c1aSMatthew Dillon 	 * the connection.
1034262c1c1aSMatthew Dillon 	 */
1035f5d34df5SGeorge V. Neville-Neil 	if (th->th_win == 0) {
1036f5d34df5SGeorge V. Neville-Neil 		tp->t_sndzerowin++;
1037262c1c1aSMatthew Dillon 		tp->t_flags |= TF_RXWIN0SENT;
1038f5d34df5SGeorge V. Neville-Neil 	} else
1039262c1c1aSMatthew Dillon 		tp->t_flags &= ~TF_RXWIN0SENT;
1040df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1041fb59c426SYoshinobu Inoue 		th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
1042fb59c426SYoshinobu Inoue 		th->th_flags |= TH_URG;
1043df8bae1dSRodney W. Grimes 	} else
1044df8bae1dSRodney W. Grimes 		/*
1045df8bae1dSRodney W. Grimes 		 * If no urgent pointer to send, then we pull
1046df8bae1dSRodney W. Grimes 		 * the urgent pointer to the left edge of the send window
1047df8bae1dSRodney W. Grimes 		 * so that it doesn't drift into the send window on sequence
1048df8bae1dSRodney W. Grimes 		 * number wraparound.
1049df8bae1dSRodney W. Grimes 		 */
1050df8bae1dSRodney W. Grimes 		tp->snd_up = tp->snd_una;		/* drag it along */
1051df8bae1dSRodney W. Grimes 
10521cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
1053ee763d0dSBjoern A. Zeeb 	if (tp->t_flags & TF_SIGNATURE) {
1054ee763d0dSBjoern A. Zeeb 		int sigoff = to.to_signature - opt;
10553418daf2SBjoern A. Zeeb 		tcp_signature_compute(m, 0, len, optlen,
10561cfd4b53SBruce M Simpson 		    (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
1057ee763d0dSBjoern A. Zeeb 	}
1058265ed012SBruce M Simpson #endif
10591cfd4b53SBruce M Simpson 
1060df8bae1dSRodney W. Grimes 	/*
1061df8bae1dSRodney W. Grimes 	 * Put TCP length in extended header, and then
1062df8bae1dSRodney W. Grimes 	 * checksum extended header and data.
1063df8bae1dSRodney W. Grimes 	 */
1064fb59c426SYoshinobu Inoue 	m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
1065fb59c426SYoshinobu Inoue #ifdef INET6
1066fb59c426SYoshinobu Inoue 	if (isipv6)
1067fb59c426SYoshinobu Inoue 		/*
1068fb59c426SYoshinobu Inoue 		 * ip6_plen is not need to be filled now, and will be filled
1069fb59c426SYoshinobu Inoue 		 * in ip6_output.
1070fb59c426SYoshinobu Inoue 		 */
1071fb59c426SYoshinobu Inoue 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
1072fb59c426SYoshinobu Inoue 				       sizeof(struct tcphdr) + optlen + len);
1073fb59c426SYoshinobu Inoue 	else
1074fb59c426SYoshinobu Inoue #endif /* INET6 */
1075fb59c426SYoshinobu Inoue 	{
1076db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_TCP;
1077db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
107879909384SJonathan Lemon 		th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
107979909384SJonathan Lemon 		    htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
1080fb59c426SYoshinobu Inoue 
1081db4f9cc7SJonathan Lemon 		/* IP version must be set here for ipv4/ipv6 checking later */
1082db4f9cc7SJonathan Lemon 		KASSERT(ip->ip_v == IPVERSION,
10836e551fb6SDavid E. O'Brien 		    ("%s: IP version incorrect: %d", __func__, ip->ip_v));
1084fb59c426SYoshinobu Inoue 	}
1085df8bae1dSRodney W. Grimes 
1086df8bae1dSRodney W. Grimes 	/*
1087b3c0f300SAndre Oppermann 	 * Enable TSO and specify the size of the segments.
1088b3c0f300SAndre Oppermann 	 * The TCP pseudo header checksum is always provided.
1089b3c0f300SAndre Oppermann 	 * XXX: Fixme: This is currently not the case for IPv6.
1090b3c0f300SAndre Oppermann 	 */
1091b3c0f300SAndre Oppermann 	if (tso) {
1092153e5b57SAndre Oppermann 		KASSERT(len > tp->t_maxopd - optlen,
1093153e5b57SAndre Oppermann 		    ("%s: len <= tso_segsz", __func__));
10943579cf4cSKenneth D. Merry 		m->m_pkthdr.csum_flags |= CSUM_TSO;
1095b3c0f300SAndre Oppermann 		m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen;
1096b3c0f300SAndre Oppermann 	}
1097b3c0f300SAndre Oppermann 
1098ed420311SAndre Oppermann 	KASSERT(len + hdrlen + ipoptlen == m_length(m, NULL),
1099ed420311SAndre Oppermann 	    ("%s: mbuf chain shorter than expected", __func__));
1100ed420311SAndre Oppermann 
1101b3c0f300SAndre Oppermann 	/*
1102df8bae1dSRodney W. Grimes 	 * In transmit state, time the transmission and arrange for
1103df8bae1dSRodney W. Grimes 	 * the retransmit.  In persist state, just set snd_max.
1104df8bae1dSRodney W. Grimes 	 */
11052cdbfa66SPaul Saab 	if ((tp->t_flags & TF_FORCEDATA) == 0 ||
1106b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
1107df8bae1dSRodney W. Grimes 		tcp_seq startseq = tp->snd_nxt;
1108df8bae1dSRodney W. Grimes 
1109df8bae1dSRodney W. Grimes 		/*
1110df8bae1dSRodney W. Grimes 		 * Advance snd_nxt over sequence space of this segment.
1111df8bae1dSRodney W. Grimes 		 */
1112df8bae1dSRodney W. Grimes 		if (flags & (TH_SYN|TH_FIN)) {
1113df8bae1dSRodney W. Grimes 			if (flags & TH_SYN)
1114df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
1115df8bae1dSRodney W. Grimes 			if (flags & TH_FIN) {
1116df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
1117df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_SENTFIN;
1118df8bae1dSRodney W. Grimes 			}
1119df8bae1dSRodney W. Grimes 		}
1120a55db2b6SPaul Saab 		if (sack_rxmit)
11216d90faf3SPaul Saab 			goto timer;
1122df8bae1dSRodney W. Grimes 		tp->snd_nxt += len;
1123df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1124df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt;
1125df8bae1dSRodney W. Grimes 			/*
1126df8bae1dSRodney W. Grimes 			 * Time this transmission if not a retransmission and
1127df8bae1dSRodney W. Grimes 			 * not currently timing anything.
1128df8bae1dSRodney W. Grimes 			 */
11299b8b58e0SJonathan Lemon 			if (tp->t_rtttime == 0) {
11309b8b58e0SJonathan Lemon 				tp->t_rtttime = ticks;
1131df8bae1dSRodney W. Grimes 				tp->t_rtseq = startseq;
113278b50714SRobert Watson 				TCPSTAT_INC(tcps_segstimed);
1133df8bae1dSRodney W. Grimes 			}
1134df8bae1dSRodney W. Grimes 		}
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes 		/*
1137df8bae1dSRodney W. Grimes 		 * Set retransmit timer if not currently set,
113828257b5cSMatthew Dillon 		 * and not doing a pure ack or a keep-alive probe.
1139df8bae1dSRodney W. Grimes 		 * Initial value for retransmit timer is smoothed
1140df8bae1dSRodney W. Grimes 		 * round-trip time + 2 * round-trip time variance.
1141df8bae1dSRodney W. Grimes 		 * Initialize shift counter which is used for backoff
1142df8bae1dSRodney W. Grimes 		 * of retransmit time.
1143df8bae1dSRodney W. Grimes 		 */
11446d90faf3SPaul Saab timer:
11454b8e42baSAndre Oppermann 		if (!tcp_timer_active(tp, TT_REXMT) &&
1146a55db2b6SPaul Saab 		    ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1147a55db2b6SPaul Saab 		     (tp->snd_nxt != tp->snd_una))) {
1148b8152ba7SAndre Oppermann 			if (tcp_timer_active(tp, TT_PERSIST)) {
1149b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_PERSIST, 0);
1150df8bae1dSRodney W. Grimes 				tp->t_rxtshift = 0;
1151df8bae1dSRodney W. Grimes 			}
1152b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
1153df8bae1dSRodney W. Grimes 		}
115428257b5cSMatthew Dillon 	} else {
115528257b5cSMatthew Dillon 		/*
115628257b5cSMatthew Dillon 		 * Persist case, update snd_max but since we are in
115728257b5cSMatthew Dillon 		 * persist mode (no window) we do not update snd_nxt.
115828257b5cSMatthew Dillon 		 */
115928257b5cSMatthew Dillon 		int xlen = len;
116028257b5cSMatthew Dillon 		if (flags & TH_SYN)
116128257b5cSMatthew Dillon 			++xlen;
116228257b5cSMatthew Dillon 		if (flags & TH_FIN) {
116328257b5cSMatthew Dillon 			++xlen;
116428257b5cSMatthew Dillon 			tp->t_flags |= TF_SENTFIN;
116528257b5cSMatthew Dillon 		}
1166abac41a6SMatthew Dillon 		if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
1167df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt + len;
116828257b5cSMatthew Dillon 	}
1169df8bae1dSRodney W. Grimes 
117039bc9de5SLawrence Stewart 	/* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */
117139bc9de5SLawrence Stewart 	hhook_run_tcp_est_out(tp, th, &to, len, tso);
117239bc9de5SLawrence Stewart 
1173610ee2f9SDavid Greenman #ifdef TCPDEBUG
1174df8bae1dSRodney W. Grimes 	/*
1175df8bae1dSRodney W. Grimes 	 * Trace.
1176df8bae1dSRodney W. Grimes 	 */
117791f467d5SHartmut Brandt 	if (so->so_options & SO_DEBUG) {
1178f3e0b7efSBruce M Simpson 		u_short save = 0;
11795214cb3fSBruce M Simpson #ifdef INET6
11805214cb3fSBruce M Simpson 		if (!isipv6)
11815214cb3fSBruce M Simpson #endif
11825214cb3fSBruce M Simpson 		{
11835214cb3fSBruce M Simpson 			save = ipov->ih_len;
118491f467d5SHartmut Brandt 			ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
11855214cb3fSBruce M Simpson 		}
1186fb59c426SYoshinobu Inoue 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
11875214cb3fSBruce M Simpson #ifdef INET6
11885214cb3fSBruce M Simpson 		if (!isipv6)
11895214cb3fSBruce M Simpson #endif
119091f467d5SHartmut Brandt 		ipov->ih_len = save;
119191f467d5SHartmut Brandt 	}
1192*b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1193df8bae1dSRodney W. Grimes 
1194df8bae1dSRodney W. Grimes 	/*
1195df8bae1dSRodney W. Grimes 	 * Fill in IP length and desired time to live and
1196df8bae1dSRodney W. Grimes 	 * send to IP level.  There should be a better way
1197df8bae1dSRodney W. Grimes 	 * to handle ttl and tos; we could keep them in
1198df8bae1dSRodney W. Grimes 	 * the template, but need a way to checksum without them.
1199df8bae1dSRodney W. Grimes 	 */
1200fb59c426SYoshinobu Inoue 	/*
1201fb59c426SYoshinobu Inoue 	 * m->m_pkthdr.len should have been set before cksum calcuration,
1202fb59c426SYoshinobu Inoue 	 * because in6_cksum() need it.
1203fb59c426SYoshinobu Inoue 	 */
1204fb59c426SYoshinobu Inoue #ifdef INET6
1205fb59c426SYoshinobu Inoue 	if (isipv6) {
1206fb59c426SYoshinobu Inoue 		/*
1207fb59c426SYoshinobu Inoue 		 * we separately set hoplimit for every segment, since the
1208fb59c426SYoshinobu Inoue 		 * user might want to change the value via setsockopt.
1209fb59c426SYoshinobu Inoue 		 * Also, desired default hop limit might be changed via
1210fb59c426SYoshinobu Inoue 		 * Neighbor Discovery.
1211fb59c426SYoshinobu Inoue 		 */
121297d8d152SAndre Oppermann 		ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1213fb59c426SYoshinobu Inoue 
1214fb59c426SYoshinobu Inoue 		/* TODO: IPv6 IP6TOS_ECT bit on */
1215fb59c426SYoshinobu Inoue 		error = ip6_output(m,
121697d8d152SAndre Oppermann 			    tp->t_inpcb->in6p_outputopts, NULL,
1217b5d47ff5SJohn-Mark Gurney 			    ((so->so_options & SO_DONTROUTE) ?
1218b5d47ff5SJohn-Mark Gurney 			    IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb);
1219*b287c6c7SBjoern A. Zeeb 	}
1220fb59c426SYoshinobu Inoue #endif /* INET6 */
1221*b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1222*b287c6c7SBjoern A. Zeeb 	else
1223*b287c6c7SBjoern A. Zeeb #endif
1224*b287c6c7SBjoern A. Zeeb #ifdef INET
1225df8bae1dSRodney W. Grimes     {
1226fb59c426SYoshinobu Inoue 	ip->ip_len = m->m_pkthdr.len;
1227686cdd19SJun-ichiro itojun Hagino #ifdef INET6
12285cd54324SBjoern A. Zeeb 	if (tp->t_inpcb->inp_vflag & INP_IPV6PROTO)
122997d8d152SAndre Oppermann 		ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1230686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */
1231f138387aSGarrett Wollman 	/*
123297d8d152SAndre Oppermann 	 * If we do path MTU discovery, then we set DF on every packet.
123397d8d152SAndre Oppermann 	 * This might not be the best thing to do according to RFC3390
123497d8d152SAndre Oppermann 	 * Section 2. However the tcp hostcache migitates the problem
123597d8d152SAndre Oppermann 	 * so it affects only the first tcp connection with a host.
1236e4e92660SAndre Oppermann 	 *
1237e4e92660SAndre Oppermann 	 * NB: Don't set DF on small MTU/MSS to have a safe fallback.
1238f138387aSGarrett Wollman 	 */
1239e4e92660SAndre Oppermann 	if (V_path_mtu_discovery && tp->t_maxopd > V_tcp_minmss)
1240fb59c426SYoshinobu Inoue 		ip->ip_off |= IP_DF;
124197d8d152SAndre Oppermann 
124297d8d152SAndre Oppermann 	error = ip_output(m, tp->t_inpcb->inp_options, NULL,
1243b5d47ff5SJohn-Mark Gurney 	    ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0,
1244b5d47ff5SJohn-Mark Gurney 	    tp->t_inpcb);
1245df8bae1dSRodney W. Grimes     }
1246*b287c6c7SBjoern A. Zeeb #endif /* INET */
1247df8bae1dSRodney W. Grimes 	if (error) {
12487734ea06SArchie Cobbs 
12497734ea06SArchie Cobbs 		/*
12507734ea06SArchie Cobbs 		 * We know that the packet was lost, so back out the
12517734ea06SArchie Cobbs 		 * sequence number advance, if any.
12522c30ec0aSAndre Oppermann 		 *
12532c30ec0aSAndre Oppermann 		 * If the error is EPERM the packet got blocked by the
12542c30ec0aSAndre Oppermann 		 * local firewall.  Normally we should terminate the
12552c30ec0aSAndre Oppermann 		 * connection but the blocking may have been spurious
12562c30ec0aSAndre Oppermann 		 * due to a firewall reconfiguration cycle.  So we treat
12572c30ec0aSAndre Oppermann 		 * it like a packet loss and let the retransmit timer and
12582c30ec0aSAndre Oppermann 		 * timeouts do their work over time.
12592c30ec0aSAndre Oppermann 		 * XXX: It is a POLA question whether calling tcp_drop right
12602c30ec0aSAndre Oppermann 		 * away would be the really correct behavior instead.
12617734ea06SArchie Cobbs 		 */
126272757d9aSGleb Smirnoff 		if (((tp->t_flags & TF_FORCEDATA) == 0 ||
1263b8152ba7SAndre Oppermann 		    !tcp_timer_active(tp, TT_PERSIST)) &&
126472757d9aSGleb Smirnoff 		    ((flags & TH_SYN) == 0) &&
126572757d9aSGleb Smirnoff 		    (error != EPERM)) {
12660077b016SPaul Saab 			if (sack_rxmit) {
12675d3b1b75SJayanth Vijayaraghavan 				p->rxmit -= len;
12680077b016SPaul Saab 				tp->sackhint.sack_bytes_rexmit -= len;
126972757d9aSGleb Smirnoff 				KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
12700077b016SPaul Saab 				    ("sackhint bytes rtx >= 0"));
12710077b016SPaul Saab 			} else
12727734ea06SArchie Cobbs 				tp->snd_nxt -= len;
12737734ea06SArchie Cobbs 		}
1274df8bae1dSRodney W. Grimes out:
1275cf2942b6SRobert Watson 		SOCKBUF_UNLOCK_ASSERT(&so->so_snd);	/* Check gotos. */
127672757d9aSGleb Smirnoff 		switch (error) {
127772757d9aSGleb Smirnoff 		case EPERM:
127872757d9aSGleb Smirnoff 			tp->t_softerror = error;
127972757d9aSGleb Smirnoff 			return (error);
128072757d9aSGleb Smirnoff 		case ENOBUFS:
1281b8152ba7SAndre Oppermann 	                if (!tcp_timer_active(tp, TT_REXMT) &&
1282b8152ba7SAndre Oppermann 			    !tcp_timer_active(tp, TT_PERSIST))
1283b8152ba7SAndre Oppermann 	                        tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
12841600372bSAndre Oppermann 			tp->snd_cwnd = tp->t_maxseg;
1285df8bae1dSRodney W. Grimes 			return (0);
128672757d9aSGleb Smirnoff 		case EMSGSIZE:
12873d1f141bSGarrett Wollman 			/*
1288b3c0f300SAndre Oppermann 			 * For some reason the interface we used initially
1289b3c0f300SAndre Oppermann 			 * to send segments changed to another or lowered
1290b3c0f300SAndre Oppermann 			 * its MTU.
1291b3c0f300SAndre Oppermann 			 *
1292b3c0f300SAndre Oppermann 			 * tcp_mtudisc() will find out the new MTU and as
1293b3c0f300SAndre Oppermann 			 * its last action, initiate retransmission, so it
1294b3c0f300SAndre Oppermann 			 * is important to not do so here.
1295b3c0f300SAndre Oppermann 			 *
1296b3c0f300SAndre Oppermann 			 * If TSO was active we either got an interface
1297b3c0f300SAndre Oppermann 			 * without TSO capabilits or TSO was turned off.
1298b3c0f300SAndre Oppermann 			 * Disable it for this connection as too and
1299b3c0f300SAndre Oppermann 			 * immediatly retry with MSS sized segments generated
1300b3c0f300SAndre Oppermann 			 * by this function.
13013d1f141bSGarrett Wollman 			 */
1302b3c0f300SAndre Oppermann 			if (tso)
1303b3c0f300SAndre Oppermann 				tp->t_flags &= ~TF_TSO;
13043d1f141bSGarrett Wollman 			tcp_mtudisc(tp->t_inpcb, 0);
130572757d9aSGleb Smirnoff 			return (0);
13068bec3467SGleb Smirnoff 		case EHOSTDOWN:
130772757d9aSGleb Smirnoff 		case EHOSTUNREACH:
130872757d9aSGleb Smirnoff 		case ENETDOWN:
13098bec3467SGleb Smirnoff 		case ENETUNREACH:
131072757d9aSGleb Smirnoff 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
1311df8bae1dSRodney W. Grimes 				tp->t_softerror = error;
1312df8bae1dSRodney W. Grimes 				return (0);
1313df8bae1dSRodney W. Grimes 			}
131472757d9aSGleb Smirnoff 			/* FALLTHROUGH */
131572757d9aSGleb Smirnoff 		default:
1316df8bae1dSRodney W. Grimes 			return (error);
1317df8bae1dSRodney W. Grimes 		}
131872757d9aSGleb Smirnoff 	}
131978b50714SRobert Watson 	TCPSTAT_INC(tcps_sndtotal);
1320df8bae1dSRodney W. Grimes 
1321df8bae1dSRodney W. Grimes 	/*
1322df8bae1dSRodney W. Grimes 	 * Data sent (as far as we can tell).
1323df8bae1dSRodney W. Grimes 	 * If this advertises a larger window than any other segment,
1324df8bae1dSRodney W. Grimes 	 * then remember the size of the advertised window.
1325df8bae1dSRodney W. Grimes 	 * Any pending ACK has now been sent.
1326df8bae1dSRodney W. Grimes 	 */
1327201d185bSAndre Oppermann 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1328201d185bSAndre Oppermann 		tp->rcv_adv = tp->rcv_nxt + recwin;
1329df8bae1dSRodney W. Grimes 	tp->last_ack_sent = tp->rcv_nxt;
13303bfd6421SJonathan Lemon 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
1331b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_DELACK))
1332b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, 0);
1333d912c694SMatthew Dillon #if 0
1334d912c694SMatthew Dillon 	/*
1335d912c694SMatthew Dillon 	 * This completely breaks TCP if newreno is turned on.  What happens
1336d912c694SMatthew Dillon 	 * is that if delayed-acks are turned on on the receiver, this code
1337d912c694SMatthew Dillon 	 * on the transmitter effectively destroys the TCP window, forcing
1338d912c694SMatthew Dillon 	 * it to four packets (1.5Kx4 = 6K window).
1339d912c694SMatthew Dillon 	 */
1340dbc42409SLawrence Stewart 	if (sendalot && --maxburst)
1341df8bae1dSRodney W. Grimes 		goto again;
1342d912c694SMatthew Dillon #endif
1343d912c694SMatthew Dillon 	if (sendalot)
1344d912c694SMatthew Dillon 		goto again;
1345df8bae1dSRodney W. Grimes 	return (0);
1346df8bae1dSRodney W. Grimes }
1347df8bae1dSRodney W. Grimes 
1348df8bae1dSRodney W. Grimes void
1349ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp)
1350df8bae1dSRodney W. Grimes {
13519b8b58e0SJonathan Lemon 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
13529b8b58e0SJonathan Lemon 	int tt;
1353df8bae1dSRodney W. Grimes 
1354672dc4aeSJohn Baldwin 	tp->t_flags &= ~TF_PREVVALID;
1355b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_REXMT))
13569b8b58e0SJonathan Lemon 		panic("tcp_setpersist: retransmit pending");
1357df8bae1dSRodney W. Grimes 	/*
1358df8bae1dSRodney W. Grimes 	 * Start/restart persistance timer.
1359df8bae1dSRodney W. Grimes 	 */
13609b8b58e0SJonathan Lemon 	TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1361df8bae1dSRodney W. Grimes 		      TCPTV_PERSMIN, TCPTV_PERSMAX);
1362b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_PERSIST, tt);
1363df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1364df8bae1dSRodney W. Grimes 		tp->t_rxtshift++;
1365df8bae1dSRodney W. Grimes }
136602a1a643SAndre Oppermann 
136702a1a643SAndre Oppermann /*
136802a1a643SAndre Oppermann  * Insert TCP options according to the supplied parameters to the place
136902a1a643SAndre Oppermann  * optp in a consistent way.  Can handle unaligned destinations.
137002a1a643SAndre Oppermann  *
137102a1a643SAndre Oppermann  * The order of the option processing is crucial for optimal packing and
137202a1a643SAndre Oppermann  * alignment for the scarce option space.
137302a1a643SAndre Oppermann  *
137402a1a643SAndre Oppermann  * The optimal order for a SYN/SYN-ACK segment is:
137502a1a643SAndre Oppermann  *   MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) +
137602a1a643SAndre Oppermann  *   Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40.
137702a1a643SAndre Oppermann  *
137802a1a643SAndre Oppermann  * The SACK options should be last.  SACK blocks consume 8*n+2 bytes.
137902a1a643SAndre Oppermann  * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks).
138002a1a643SAndre Oppermann  * At minimum we need 10 bytes (to generate 1 SACK block).  If both
138102a1a643SAndre Oppermann  * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present,
138202a1a643SAndre Oppermann  * we only have 10 bytes for SACK options (40 - (12 + 18)).
138302a1a643SAndre Oppermann  */
138402a1a643SAndre Oppermann int
138502a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp)
138602a1a643SAndre Oppermann {
138702a1a643SAndre Oppermann 	u_int mask, optlen = 0;
138802a1a643SAndre Oppermann 
138902a1a643SAndre Oppermann 	for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) {
139002a1a643SAndre Oppermann 		if ((to->to_flags & mask) != mask)
139102a1a643SAndre Oppermann 			continue;
13923a4018c4SAndre Oppermann 		if (optlen == TCP_MAXOLEN)
13933a4018c4SAndre Oppermann 			break;
139402a1a643SAndre Oppermann 		switch (to->to_flags & mask) {
139502a1a643SAndre Oppermann 		case TOF_MSS:
139602a1a643SAndre Oppermann 			while (optlen % 4) {
139702a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
139802a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
139902a1a643SAndre Oppermann 			}
14003a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG)
14013a4018c4SAndre Oppermann 				continue;
140202a1a643SAndre Oppermann 			optlen += TCPOLEN_MAXSEG;
140302a1a643SAndre Oppermann 			*optp++ = TCPOPT_MAXSEG;
140402a1a643SAndre Oppermann 			*optp++ = TCPOLEN_MAXSEG;
140502a1a643SAndre Oppermann 			to->to_mss = htons(to->to_mss);
140602a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss));
140702a1a643SAndre Oppermann 			optp += sizeof(to->to_mss);
140802a1a643SAndre Oppermann 			break;
140902a1a643SAndre Oppermann 		case TOF_SCALE:
141002a1a643SAndre Oppermann 			while (!optlen || optlen % 2 != 1) {
141102a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
141202a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
141302a1a643SAndre Oppermann 			}
14143a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW)
14153a4018c4SAndre Oppermann 				continue;
141602a1a643SAndre Oppermann 			optlen += TCPOLEN_WINDOW;
141702a1a643SAndre Oppermann 			*optp++ = TCPOPT_WINDOW;
141802a1a643SAndre Oppermann 			*optp++ = TCPOLEN_WINDOW;
141902a1a643SAndre Oppermann 			*optp++ = to->to_wscale;
142002a1a643SAndre Oppermann 			break;
142102a1a643SAndre Oppermann 		case TOF_SACKPERM:
142202a1a643SAndre Oppermann 			while (optlen % 2) {
142302a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
142402a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
142502a1a643SAndre Oppermann 			}
14263a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED)
14273a4018c4SAndre Oppermann 				continue;
142802a1a643SAndre Oppermann 			optlen += TCPOLEN_SACK_PERMITTED;
142902a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK_PERMITTED;
143002a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACK_PERMITTED;
143102a1a643SAndre Oppermann 			break;
143202a1a643SAndre Oppermann 		case TOF_TS:
143302a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
143402a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
143502a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
143602a1a643SAndre Oppermann 			}
14373a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP)
14383a4018c4SAndre Oppermann 				continue;
143902a1a643SAndre Oppermann 			optlen += TCPOLEN_TIMESTAMP;
144002a1a643SAndre Oppermann 			*optp++ = TCPOPT_TIMESTAMP;
144102a1a643SAndre Oppermann 			*optp++ = TCPOLEN_TIMESTAMP;
144202a1a643SAndre Oppermann 			to->to_tsval = htonl(to->to_tsval);
144302a1a643SAndre Oppermann 			to->to_tsecr = htonl(to->to_tsecr);
144402a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval));
144502a1a643SAndre Oppermann 			optp += sizeof(to->to_tsval);
144602a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr));
144702a1a643SAndre Oppermann 			optp += sizeof(to->to_tsecr);
144802a1a643SAndre Oppermann 			break;
144902a1a643SAndre Oppermann 		case TOF_SIGNATURE:
145002a1a643SAndre Oppermann 			{
145102a1a643SAndre Oppermann 			int siglen = TCPOLEN_SIGNATURE - 2;
145202a1a643SAndre Oppermann 
145302a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
145402a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
145502a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
145602a1a643SAndre Oppermann 			}
14570d957bbaSAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE)
145802a1a643SAndre Oppermann 				continue;
145902a1a643SAndre Oppermann 			optlen += TCPOLEN_SIGNATURE;
146002a1a643SAndre Oppermann 			*optp++ = TCPOPT_SIGNATURE;
146102a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SIGNATURE;
146202a1a643SAndre Oppermann 			to->to_signature = optp;
146302a1a643SAndre Oppermann 			while (siglen--)
146402a1a643SAndre Oppermann 				 *optp++ = 0;
146502a1a643SAndre Oppermann 			break;
146602a1a643SAndre Oppermann 			}
146702a1a643SAndre Oppermann 		case TOF_SACK:
146802a1a643SAndre Oppermann 			{
146902a1a643SAndre Oppermann 			int sackblks = 0;
147002a1a643SAndre Oppermann 			struct sackblk *sack = (struct sackblk *)to->to_sacks;
147102a1a643SAndre Oppermann 			tcp_seq sack_seq;
147202a1a643SAndre Oppermann 
147302a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
147402a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
147502a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
147602a1a643SAndre Oppermann 			}
14773a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK)
147802a1a643SAndre Oppermann 				continue;
147902a1a643SAndre Oppermann 			optlen += TCPOLEN_SACKHDR;
148002a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK;
148102a1a643SAndre Oppermann 			sackblks = min(to->to_nsacks,
14820d957bbaSAndre Oppermann 					(TCP_MAXOLEN - optlen) / TCPOLEN_SACK);
148302a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK;
148402a1a643SAndre Oppermann 			while (sackblks--) {
148502a1a643SAndre Oppermann 				sack_seq = htonl(sack->start);
148602a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
148702a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
148802a1a643SAndre Oppermann 				sack_seq = htonl(sack->end);
148902a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
149002a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
149102a1a643SAndre Oppermann 				optlen += TCPOLEN_SACK;
149202a1a643SAndre Oppermann 				sack++;
149302a1a643SAndre Oppermann 			}
149478b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_send_blocks);
149502a1a643SAndre Oppermann 			break;
149602a1a643SAndre Oppermann 			}
149702a1a643SAndre Oppermann 		default:
149802a1a643SAndre Oppermann 			panic("%s: unknown TCP option type", __func__);
149902a1a643SAndre Oppermann 			break;
150002a1a643SAndre Oppermann 		}
150102a1a643SAndre Oppermann 	}
150202a1a643SAndre Oppermann 
150302a1a643SAndre Oppermann 	/* Terminate and pad TCP options to a 4 byte boundary. */
150402a1a643SAndre Oppermann 	if (optlen % 4) {
150502a1a643SAndre Oppermann 		optlen += TCPOLEN_EOL;
150602a1a643SAndre Oppermann 		*optp++ = TCPOPT_EOL;
150702a1a643SAndre Oppermann 	}
1508413deb12SBjoern A. Zeeb 	/*
1509413deb12SBjoern A. Zeeb 	 * According to RFC 793 (STD0007):
1510413deb12SBjoern A. Zeeb 	 *   "The content of the header beyond the End-of-Option option
1511413deb12SBjoern A. Zeeb 	 *    must be header padding (i.e., zero)."
1512413deb12SBjoern A. Zeeb 	 *   and later: "The padding is composed of zeros."
1513413deb12SBjoern A. Zeeb 	 */
151402a1a643SAndre Oppermann 	while (optlen % 4) {
1515c343c524SAndre Oppermann 		optlen += TCPOLEN_PAD;
1516c343c524SAndre Oppermann 		*optp++ = TCPOPT_PAD;
151702a1a643SAndre Oppermann 	}
151802a1a643SAndre Oppermann 
15190d957bbaSAndre Oppermann 	KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__));
152002a1a643SAndre Oppermann 	return (optlen);
152102a1a643SAndre Oppermann }
1522