xref: /freebsd/sys/netinet/tcp_output.c (revision f2512ba12a4287ab799825c7c4f4f260846cf596)
1c398230bSWarner Losh /*-
2d7f570e6SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29d7f570e6SGarrett Wollman  *	@(#)tcp_output.c	8.4 (Berkeley) 5/24/95
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
351cfd4b53SBruce M Simpson #include "opt_inet.h"
36fb59c426SYoshinobu Inoue #include "opt_inet6.h"
373a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
38c488362eSRobert Watson #include "opt_mac.h"
390cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
400cc12cc5SJoerg Wunsch 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h>
44fb919e4dSMark Murray #include <sys/kernel.h>
45fb919e4dSMark Murray #include <sys/lock.h>
46fb919e4dSMark Murray #include <sys/mbuf.h>
47fb919e4dSMark Murray #include <sys/mutex.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51fb919e4dSMark Murray #include <sys/sysctl.h>
52df8bae1dSRodney W. Grimes 
53df8bae1dSRodney W. Grimes #include <net/route.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <netinet/in.h>
56df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
57df8bae1dSRodney W. Grimes #include <netinet/ip.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
60ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
61fb59c426SYoshinobu Inoue #ifdef INET6
62686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
63686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
64fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
65fb59c426SYoshinobu Inoue #endif
66df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
67df8bae1dSRodney W. Grimes #define	TCPOUTFLAGS
68df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
69df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
70df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
71df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
72df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
73610ee2f9SDavid Greenman #ifdef TCPDEBUG
74df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
75610ee2f9SDavid Greenman #endif
76df8bae1dSRodney W. Grimes 
77b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
78b9234fafSSam Leffler #include <netipsec/ipsec.h>
79b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
80b9234fafSSam Leffler 
81db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
82db4f9cc7SJonathan Lemon 
83aed55708SRobert Watson #include <security/mac/mac_framework.h>
84aed55708SRobert Watson 
85df8bae1dSRodney W. Grimes #ifdef notyet
86df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack();
87df8bae1dSRodney W. Grimes #endif
88df8bae1dSRodney W. Grimes 
89eb5afebaSMike Silbersack int path_mtu_discovery = 1;
909a039a5fSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW,
919a039a5fSDavid Greenman 	&path_mtu_discovery, 1, "Enable Path MTU Discovery");
929a039a5fSDavid Greenman 
939b8b58e0SJonathan Lemon int ss_fltsz = 1;
949b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW,
959b8b58e0SJonathan Lemon 	&ss_fltsz, 1, "Slow start flight size");
969b8b58e0SJonathan Lemon 
973260eb18SMike Silbersack int ss_fltsz_local = 4;
989b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW,
999b8b58e0SJonathan Lemon 	&ss_fltsz_local, 1, "Slow start flight size for local networks");
100df8bae1dSRodney W. Grimes 
1017d200109SJayanth Vijayaraghavan int     tcp_do_newreno = 1;
1028b8ed7a7SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW,
1038b8ed7a7SAndre Oppermann 	&tcp_do_newreno, 0, "Enable NewReno Algorithms");
104314e5a3dSJeffrey Hsu 
105b3c0f300SAndre Oppermann int	tcp_do_tso = 1;
106b3c0f300SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_RW,
107b3c0f300SAndre Oppermann 	&tcp_do_tso, 0, "Enable TCP Segmentation Offload");
108b3c0f300SAndre Oppermann 
1096741ecf5SAndre Oppermann int	tcp_do_autosndbuf = 1;
1106741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_RW,
1116741ecf5SAndre Oppermann 	&tcp_do_autosndbuf, 0, "Enable automatic send buffer sizing");
1126741ecf5SAndre Oppermann 
1136741ecf5SAndre Oppermann int	tcp_autosndbuf_inc = 8*1024;
1146741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_RW,
1156741ecf5SAndre Oppermann 	&tcp_autosndbuf_inc, 0, "Incrementor step size of automatic send buffer");
1166741ecf5SAndre Oppermann 
1176741ecf5SAndre Oppermann int	tcp_autosndbuf_max = 256*1024;
1186741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_RW,
1196741ecf5SAndre Oppermann 	&tcp_autosndbuf_max, 0, "Max size of automatic send buffer");
1206741ecf5SAndre Oppermann 
1216741ecf5SAndre Oppermann 
122df8bae1dSRodney W. Grimes /*
123df8bae1dSRodney W. Grimes  * Tcp output routine: figure out what should be sent and send it.
124df8bae1dSRodney W. Grimes  */
125df8bae1dSRodney W. Grimes int
126f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp)
127df8bae1dSRodney W. Grimes {
128f10e85d7SLuigi Rizzo 	struct socket *so = tp->t_inpcb->inp_socket;
129201d185bSAndre Oppermann 	long len, recwin, sendwin;
130df8bae1dSRodney W. Grimes 	int off, flags, error;
131f10e85d7SLuigi Rizzo 	struct mbuf *m;
132fb59c426SYoshinobu Inoue 	struct ip *ip = NULL;
133f10e85d7SLuigi Rizzo 	struct ipovly *ipov = NULL;
134f10e85d7SLuigi Rizzo 	struct tcphdr *th;
135a0292f23SGarrett Wollman 	u_char opt[TCP_MAXOLEN];
136a04884fcSBill Fenner 	unsigned ipoptlen, optlen, hdrlen;
1379ad0173dSBjoern A. Zeeb #ifdef IPSEC
1389ad0173dSBjoern A. Zeeb 	unsigned ipsec_optlen = 0;
1399ad0173dSBjoern A. Zeeb #endif
140df8bae1dSRodney W. Grimes 	int idle, sendalot;
14102a1a643SAndre Oppermann 	int sack_rxmit, sack_bytes_rxmt;
1426d90faf3SPaul Saab 	struct sackhole *p;
143b3c0f300SAndre Oppermann 	int tso = 0;
14402a1a643SAndre Oppermann 	struct tcpopt to;
145262c1c1aSMatthew Dillon #if 0
14646f58482SJonathan Lemon 	int maxburst = TCP_MAXBURST;
147262c1c1aSMatthew Dillon #endif
148fb59c426SYoshinobu Inoue #ifdef INET6
149f10e85d7SLuigi Rizzo 	struct ip6_hdr *ip6 = NULL;
150fb59c426SYoshinobu Inoue 	int isipv6;
151fb59c426SYoshinobu Inoue 
152fb59c426SYoshinobu Inoue 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
153fb59c426SYoshinobu Inoue #endif
154df8bae1dSRodney W. Grimes 
1558501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1563d6ade3aSJennifer Yang 
157df8bae1dSRodney W. Grimes 	/*
158df8bae1dSRodney W. Grimes 	 * Determine length of data that should be transmitted,
159df8bae1dSRodney W. Grimes 	 * and flags that will be used.
160df8bae1dSRodney W. Grimes 	 * If there is some data or critical controls (SYN, RST)
161df8bae1dSRodney W. Grimes 	 * to send, then transmit; otherwise, investigate further.
162df8bae1dSRodney W. Grimes 	 */
163c24d5daeSJayanth Vijayaraghavan 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
1649b8b58e0SJonathan Lemon 	if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
165df8bae1dSRodney W. Grimes 		/*
166df8bae1dSRodney W. Grimes 		 * We have been idle for "a while" and no acks are
167df8bae1dSRodney W. Grimes 		 * expected to clock out any data we send --
168df8bae1dSRodney W. Grimes 		 * slow start to get ack "clock" running again.
1699b8b58e0SJonathan Lemon 		 *
1709b8b58e0SJonathan Lemon 		 * Set the slow-start flight size depending on whether
1719b8b58e0SJonathan Lemon 		 * this is a local network or not.
172df8bae1dSRodney W. Grimes 		 */
173f10e85d7SLuigi Rizzo 		int ss = ss_fltsz;
174fb59c426SYoshinobu Inoue #ifdef INET6
175f10e85d7SLuigi Rizzo 		if (isipv6) {
176f10e85d7SLuigi Rizzo 			if (in6_localaddr(&tp->t_inpcb->in6p_faddr))
177f10e85d7SLuigi Rizzo 				ss = ss_fltsz_local;
178f10e85d7SLuigi Rizzo 		} else
179f10e85d7SLuigi Rizzo #endif /* INET6 */
180f10e85d7SLuigi Rizzo 		if (in_localaddr(tp->t_inpcb->inp_faddr))
181f10e85d7SLuigi Rizzo 			ss = ss_fltsz_local;
182f10e85d7SLuigi Rizzo 		tp->snd_cwnd = tp->t_maxseg * ss;
1839b8b58e0SJonathan Lemon 	}
184c24d5daeSJayanth Vijayaraghavan 	tp->t_flags &= ~TF_LASTIDLE;
185c24d5daeSJayanth Vijayaraghavan 	if (idle) {
186c24d5daeSJayanth Vijayaraghavan 		if (tp->t_flags & TF_MORETOCOME) {
187c24d5daeSJayanth Vijayaraghavan 			tp->t_flags |= TF_LASTIDLE;
188c24d5daeSJayanth Vijayaraghavan 			idle = 0;
189c24d5daeSJayanth Vijayaraghavan 		}
190c24d5daeSJayanth Vijayaraghavan 	}
191df8bae1dSRodney W. Grimes again:
1926d90faf3SPaul Saab 	/*
1936d90faf3SPaul Saab 	 * If we've recently taken a timeout, snd_max will be greater than
1946d90faf3SPaul Saab 	 * snd_nxt.  There may be SACK information that allows us to avoid
1956d90faf3SPaul Saab 	 * resending already delivered data.  Adjust snd_nxt accordingly.
1966d90faf3SPaul Saab 	 */
1973529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
1983529149eSAndre Oppermann 	    SEQ_LT(tp->snd_nxt, tp->snd_max))
1996d90faf3SPaul Saab 		tcp_sack_adjust(tp);
200df8bae1dSRodney W. Grimes 	sendalot = 0;
201df8bae1dSRodney W. Grimes 	off = tp->snd_nxt - tp->snd_una;
202201d185bSAndre Oppermann 	sendwin = min(tp->snd_wnd, tp->snd_cwnd);
203201d185bSAndre Oppermann 	sendwin = min(sendwin, tp->snd_bwnd);
204df8bae1dSRodney W. Grimes 
205df8bae1dSRodney W. Grimes 	flags = tcp_outflags[tp->t_state];
206a0292f23SGarrett Wollman 	/*
2076d90faf3SPaul Saab 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
2086d90faf3SPaul Saab 	 * to send out new data (when sendalot is 1), bypass this function.
2096d90faf3SPaul Saab 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
2106d90faf3SPaul Saab 	 * we're replacing a (future) new transmission with a retransmission
2116d90faf3SPaul Saab 	 * now, and we previously incremented snd_cwnd in tcp_input().
2126d90faf3SPaul Saab 	 */
2136d90faf3SPaul Saab 	/*
2146d90faf3SPaul Saab 	 * Still in sack recovery , reset rxmit flag to zero.
2156d90faf3SPaul Saab 	 */
2166d90faf3SPaul Saab 	sack_rxmit = 0;
217a55db2b6SPaul Saab 	sack_bytes_rxmt = 0;
2186d90faf3SPaul Saab 	len = 0;
2196d90faf3SPaul Saab 	p = NULL;
2203529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp) &&
221a55db2b6SPaul Saab 	    (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
222a55db2b6SPaul Saab 		long cwin;
223a55db2b6SPaul Saab 
224a55db2b6SPaul Saab 		cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
225a55db2b6SPaul Saab 		if (cwin < 0)
226a55db2b6SPaul Saab 			cwin = 0;
227f787edd8SJayanth Vijayaraghavan 		/* Do not retransmit SACK segments beyond snd_recover */
228f787edd8SJayanth Vijayaraghavan 		if (SEQ_GT(p->end, tp->snd_recover)) {
229f787edd8SJayanth Vijayaraghavan 			/*
230f787edd8SJayanth Vijayaraghavan 			 * (At least) part of sack hole extends beyond
231f787edd8SJayanth Vijayaraghavan 			 * snd_recover. Check to see if we can rexmit data
232f787edd8SJayanth Vijayaraghavan 			 * for this hole.
233f787edd8SJayanth Vijayaraghavan 			 */
234f787edd8SJayanth Vijayaraghavan 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
235f787edd8SJayanth Vijayaraghavan 				/*
236f787edd8SJayanth Vijayaraghavan 				 * Can't rexmit any more data for this hole.
237f787edd8SJayanth Vijayaraghavan 				 * That data will be rexmitted in the next
238f787edd8SJayanth Vijayaraghavan 				 * sack recovery episode, when snd_recover
239f787edd8SJayanth Vijayaraghavan 				 * moves past p->rxmit.
240f787edd8SJayanth Vijayaraghavan 				 */
241f787edd8SJayanth Vijayaraghavan 				p = NULL;
242f787edd8SJayanth Vijayaraghavan 				goto after_sack_rexmit;
243f787edd8SJayanth Vijayaraghavan 			} else
244f787edd8SJayanth Vijayaraghavan 				/* Can rexmit part of the current hole */
245a55db2b6SPaul Saab 				len = ((long)ulmin(cwin,
246f787edd8SJayanth Vijayaraghavan 						   tp->snd_recover - p->rxmit));
247f787edd8SJayanth Vijayaraghavan 		} else
248a55db2b6SPaul Saab 			len = ((long)ulmin(cwin, p->end - p->rxmit));
2496d90faf3SPaul Saab 		off = p->rxmit - tp->snd_una;
250f787edd8SJayanth Vijayaraghavan 		KASSERT(off >= 0,("%s: sack block to the left of una : %d",
251f787edd8SJayanth Vijayaraghavan 		    __func__, off));
2526d90faf3SPaul Saab 		if (len > 0) {
253ab5c14d8SRobert Watson 			sack_rxmit = 1;
254ab5c14d8SRobert Watson 			sendalot = 1;
2556d90faf3SPaul Saab 			tcpstat.tcps_sack_rexmits++;
2566d90faf3SPaul Saab 			tcpstat.tcps_sack_rexmit_bytes +=
2576d90faf3SPaul Saab 			    min(len, tp->t_maxseg);
2586d90faf3SPaul Saab 		}
2596d90faf3SPaul Saab 	}
260f787edd8SJayanth Vijayaraghavan after_sack_rexmit:
2616d90faf3SPaul Saab 	/*
262a0292f23SGarrett Wollman 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
263a0292f23SGarrett Wollman 	 * state flags.
264a0292f23SGarrett Wollman 	 */
265a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDFIN)
266a0292f23SGarrett Wollman 		flags |= TH_FIN;
267a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDSYN)
268a0292f23SGarrett Wollman 		flags |= TH_SYN;
269a0292f23SGarrett Wollman 
270cf2942b6SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
271df8bae1dSRodney W. Grimes 	/*
272df8bae1dSRodney W. Grimes 	 * If in persist timeout with window of 0, send 1 byte.
273df8bae1dSRodney W. Grimes 	 * Otherwise, if window is small but nonzero
274df8bae1dSRodney W. Grimes 	 * and timer expired, we will send what we can
275df8bae1dSRodney W. Grimes 	 * and go to transmit state.
276df8bae1dSRodney W. Grimes 	 */
2772cdbfa66SPaul Saab 	if (tp->t_flags & TF_FORCEDATA) {
278201d185bSAndre Oppermann 		if (sendwin == 0) {
279df8bae1dSRodney W. Grimes 			/*
280df8bae1dSRodney W. Grimes 			 * If we still have some data to send, then
281df8bae1dSRodney W. Grimes 			 * clear the FIN bit.  Usually this would
282df8bae1dSRodney W. Grimes 			 * happen below when it realizes that we
283df8bae1dSRodney W. Grimes 			 * aren't sending all the data.  However,
28429089b51SJulian Elischer 			 * if we have exactly 1 byte of unsent data,
285df8bae1dSRodney W. Grimes 			 * then it won't clear the FIN bit below,
286df8bae1dSRodney W. Grimes 			 * and if we are in persist state, we wind
287df8bae1dSRodney W. Grimes 			 * up sending the packet without recording
288df8bae1dSRodney W. Grimes 			 * that we sent the FIN bit.
289df8bae1dSRodney W. Grimes 			 *
290df8bae1dSRodney W. Grimes 			 * We can't just blindly clear the FIN bit,
291df8bae1dSRodney W. Grimes 			 * because if we don't have any more data
292df8bae1dSRodney W. Grimes 			 * to send then the probe will be the FIN
293df8bae1dSRodney W. Grimes 			 * itself.
294df8bae1dSRodney W. Grimes 			 */
295df8bae1dSRodney W. Grimes 			if (off < so->so_snd.sb_cc)
296df8bae1dSRodney W. Grimes 				flags &= ~TH_FIN;
297201d185bSAndre Oppermann 			sendwin = 1;
298df8bae1dSRodney W. Grimes 		} else {
299b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_PERSIST, 0);
300df8bae1dSRodney W. Grimes 			tp->t_rxtshift = 0;
301df8bae1dSRodney W. Grimes 		}
302df8bae1dSRodney W. Grimes 	}
303df8bae1dSRodney W. Grimes 
30428257b5cSMatthew Dillon 	/*
30528257b5cSMatthew Dillon 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
30628257b5cSMatthew Dillon 	 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
307201d185bSAndre Oppermann 	 * a negative length.  This can also occur when TCP opens up
30828257b5cSMatthew Dillon 	 * its congestion window while receiving additional duplicate
30928257b5cSMatthew Dillon 	 * acks after fast-retransmit because TCP will reset snd_nxt
31028257b5cSMatthew Dillon 	 * to snd_max after the fast-retransmit.
31128257b5cSMatthew Dillon 	 *
31228257b5cSMatthew Dillon 	 * In the normal retransmit-FIN-only case, however, snd_nxt will
31328257b5cSMatthew Dillon 	 * be set to snd_una, the offset will be 0, and the length may
31428257b5cSMatthew Dillon 	 * wind up 0.
3156d90faf3SPaul Saab 	 *
3166d90faf3SPaul Saab 	 * If sack_rxmit is true we are retransmitting from the scoreboard
3176d90faf3SPaul Saab 	 * in which case len is already set.
31828257b5cSMatthew Dillon 	 */
319a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
320a55db2b6SPaul Saab 		if (sack_bytes_rxmt == 0)
3216d90faf3SPaul Saab 			len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
322a55db2b6SPaul Saab 		else {
323a55db2b6SPaul Saab 			long cwin;
324a55db2b6SPaul Saab 
325a55db2b6SPaul Saab                         /*
326a55db2b6SPaul Saab 			 * We are inside of a SACK recovery episode and are
327a55db2b6SPaul Saab 			 * sending new data, having retransmitted all the
328a55db2b6SPaul Saab 			 * data possible in the scoreboard.
329a55db2b6SPaul Saab 			 */
3307d5ed1ceSPaul Saab 			len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd)
3317d5ed1ceSPaul Saab 			       - off);
3328d03f2b5SPaul Saab 			/*
3338d03f2b5SPaul Saab 			 * Don't remove this (len > 0) check !
3348d03f2b5SPaul Saab 			 * We explicitly check for len > 0 here (although it
3358d03f2b5SPaul Saab 			 * isn't really necessary), to work around a gcc
3368d03f2b5SPaul Saab 			 * optimization issue - to force gcc to compute
3378d03f2b5SPaul Saab 			 * len above. Without this check, the computation
3388d03f2b5SPaul Saab 			 * of len is bungled by the optimizer.
3398d03f2b5SPaul Saab 			 */
3408d03f2b5SPaul Saab 			if (len > 0) {
3417d5ed1ceSPaul Saab 				cwin = tp->snd_cwnd -
3427d5ed1ceSPaul Saab 					(tp->snd_nxt - tp->sack_newdata) -
343a55db2b6SPaul Saab 					sack_bytes_rxmt;
344a55db2b6SPaul Saab 				if (cwin < 0)
345a55db2b6SPaul Saab 					cwin = 0;
346a55db2b6SPaul Saab 				len = lmin(len, cwin);
347a55db2b6SPaul Saab 			}
348a55db2b6SPaul Saab 		}
3498d03f2b5SPaul Saab 	}
350a0292f23SGarrett Wollman 
351a0292f23SGarrett Wollman 	/*
352a0292f23SGarrett Wollman 	 * Lop off SYN bit if it has already been sent.  However, if this
353a0292f23SGarrett Wollman 	 * is SYN-SENT state and if segment contains data and if we don't
354a0292f23SGarrett Wollman 	 * know that foreign host supports TAO, suppress sending segment.
355a0292f23SGarrett Wollman 	 */
356a0292f23SGarrett Wollman 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
3574b8e98d6SQing Li 		if (tp->t_state != TCPS_SYN_RECEIVED)
358a0292f23SGarrett Wollman 			flags &= ~TH_SYN;
359a0292f23SGarrett Wollman 		off--, len++;
360a0292f23SGarrett Wollman 	}
361a0292f23SGarrett Wollman 
36281165e48SAndras Olah 	/*
363c94c54e4SAndre Oppermann 	 * Be careful not to send data and/or FIN on SYN segments.
36481165e48SAndras Olah 	 * This measure is needed to prevent interoperability problems
36581165e48SAndras Olah 	 * with not fully conformant TCP implementations.
36681165e48SAndras Olah 	 */
367c94c54e4SAndre Oppermann 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
36881165e48SAndras Olah 		len = 0;
36981165e48SAndras Olah 		flags &= ~TH_FIN;
37081165e48SAndras Olah 	}
37181165e48SAndras Olah 
372df8bae1dSRodney W. Grimes 	if (len < 0) {
373df8bae1dSRodney W. Grimes 		/*
374df8bae1dSRodney W. Grimes 		 * If FIN has been sent but not acked,
375df8bae1dSRodney W. Grimes 		 * but we haven't been called to retransmit,
37628257b5cSMatthew Dillon 		 * len will be < 0.  Otherwise, window shrank
377df8bae1dSRodney W. Grimes 		 * after we sent into it.  If window shrank to 0,
3782d8266afSDavid Greenman 		 * cancel pending retransmit, pull snd_nxt back
3792d8266afSDavid Greenman 		 * to (closed) window, and set the persist timer
3802d8266afSDavid Greenman 		 * if it isn't already going.  If the window didn't
3812d8266afSDavid Greenman 		 * close completely, just wait for an ACK.
382df8bae1dSRodney W. Grimes 		 */
383df8bae1dSRodney W. Grimes 		len = 0;
384201d185bSAndre Oppermann 		if (sendwin == 0) {
385b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
3862d8266afSDavid Greenman 			tp->t_rxtshift = 0;
387df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
388b8152ba7SAndre Oppermann 			if (!tcp_timer_active(tp, TT_PERSIST))
3892d8266afSDavid Greenman 				tcp_setpersist(tp);
390df8bae1dSRodney W. Grimes 		}
391df8bae1dSRodney W. Grimes 	}
39228257b5cSMatthew Dillon 
3936741ecf5SAndre Oppermann 	/* len will be >= 0 after this point. */
3946741ecf5SAndre Oppermann 	KASSERT(len >= 0, ("%s: len < 0", __func__));
3956741ecf5SAndre Oppermann 
39628257b5cSMatthew Dillon 	/*
3976741ecf5SAndre Oppermann 	 * Automatic sizing of send socket buffer.  Often the send buffer
3986741ecf5SAndre Oppermann 	 * size is not optimally adjusted to the actual network conditions
3996741ecf5SAndre Oppermann 	 * at hand (delay bandwidth product).  Setting the buffer size too
4006741ecf5SAndre Oppermann 	 * small limits throughput on links with high bandwidth and high
4016741ecf5SAndre Oppermann 	 * delay (eg. trans-continental/oceanic links).  Setting the
4026741ecf5SAndre Oppermann 	 * buffer size too big consumes too much real kernel memory,
4036741ecf5SAndre Oppermann 	 * especially with many connections on busy servers.
4046741ecf5SAndre Oppermann 	 *
4056741ecf5SAndre Oppermann 	 * The criteria to step up the send buffer one notch are:
4066741ecf5SAndre Oppermann 	 *  1. receive window of remote host is larger than send buffer
4076741ecf5SAndre Oppermann 	 *     (with a fudge factor of 5/4th);
4086741ecf5SAndre Oppermann 	 *  2. send buffer is filled to 7/8th with data (so we actually
4096741ecf5SAndre Oppermann 	 *     have data to make use of it);
4106741ecf5SAndre Oppermann 	 *  3. send buffer fill has not hit maximal automatic size;
4116741ecf5SAndre Oppermann 	 *  4. our send window (slow start and cogestion controlled) is
4126741ecf5SAndre Oppermann 	 *     larger than sent but unacknowledged data in send buffer.
4136741ecf5SAndre Oppermann 	 *
4146741ecf5SAndre Oppermann 	 * The remote host receive window scaling factor may limit the
4156741ecf5SAndre Oppermann 	 * growing of the send buffer before it reaches its allowed
4166741ecf5SAndre Oppermann 	 * maximum.
4176741ecf5SAndre Oppermann 	 *
4186741ecf5SAndre Oppermann 	 * It scales directly with slow start or congestion window
4196741ecf5SAndre Oppermann 	 * and does at most one step per received ACK.  This fast
4206741ecf5SAndre Oppermann 	 * scaling has the drawback of growing the send buffer beyond
4216741ecf5SAndre Oppermann 	 * what is strictly necessary to make full use of a given
4226741ecf5SAndre Oppermann 	 * delay*bandwith product.  However testing has shown this not
4236741ecf5SAndre Oppermann 	 * to be much of an problem.  At worst we are trading wasting
4246741ecf5SAndre Oppermann 	 * of available bandwith (the non-use of it) for wasting some
4256741ecf5SAndre Oppermann 	 * socket buffer memory.
4266741ecf5SAndre Oppermann 	 *
4276741ecf5SAndre Oppermann 	 * TODO: Shrink send buffer during idle periods together
4286741ecf5SAndre Oppermann 	 * with congestion window.  Requires another timer.  Has to
4296741ecf5SAndre Oppermann 	 * wait for upcoming tcp timer rewrite.
4306741ecf5SAndre Oppermann 	 */
4316741ecf5SAndre Oppermann 	if (tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) {
4326741ecf5SAndre Oppermann 		if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat &&
4336741ecf5SAndre Oppermann 		    so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) &&
4346741ecf5SAndre Oppermann 		    so->so_snd.sb_cc < tcp_autosndbuf_max &&
4356741ecf5SAndre Oppermann 		    sendwin >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) {
4366741ecf5SAndre Oppermann 			if (!sbreserve_locked(&so->so_snd,
4376741ecf5SAndre Oppermann 			    min(so->so_snd.sb_hiwat + tcp_autosndbuf_inc,
4386741ecf5SAndre Oppermann 			     tcp_autosndbuf_max), so, curthread))
4396741ecf5SAndre Oppermann 				so->so_snd.sb_flags &= ~SB_AUTOSIZE;
4406741ecf5SAndre Oppermann 		}
4416741ecf5SAndre Oppermann 	}
4426741ecf5SAndre Oppermann 
4436741ecf5SAndre Oppermann 	/*
4446741ecf5SAndre Oppermann 	 * Truncate to the maximum segment length or enable TCP Segmentation
4456741ecf5SAndre Oppermann 	 * Offloading (if supported by hardware) and ensure that FIN is removed
4466741ecf5SAndre Oppermann 	 * if the length no longer contains the last data byte.
447b3c0f300SAndre Oppermann 	 *
448b3c0f300SAndre Oppermann 	 * TSO may only be used if we are in a pure bulk sending state.  The
449b3c0f300SAndre Oppermann 	 * presence of TCP-MD5, SACK retransmits, SACK advertizements and
450b3c0f300SAndre Oppermann 	 * IP options prevent using TSO.  With TSO the TCP header is the same
451b3c0f300SAndre Oppermann 	 * (except for the sequence number) for all generated packets.  This
452b3c0f300SAndre Oppermann 	 * makes it impossible to transmit any options which vary per generated
453b3c0f300SAndre Oppermann 	 * segment or packet.
454b3c0f300SAndre Oppermann 	 *
455b3c0f300SAndre Oppermann 	 * The length of TSO bursts is limited to TCP_MAXWIN.  That limit and
456b3c0f300SAndre Oppermann 	 * removal of FIN (if not already catched here) are handled later after
457b3c0f300SAndre Oppermann 	 * the exact length of the TCP options are known.
45828257b5cSMatthew Dillon 	 */
4599ad0173dSBjoern A. Zeeb #ifdef IPSEC
4609ad0173dSBjoern A. Zeeb 	/*
4619ad0173dSBjoern A. Zeeb 	 * Pre-calculate here as we save another lookup into the darknesses
4629ad0173dSBjoern A. Zeeb 	 * of IPsec that way and can actually decide if TSO is ok.
4639ad0173dSBjoern A. Zeeb 	 */
4649ad0173dSBjoern A. Zeeb 	ipsec_optlen = ipsec_hdrsiz_tcp(tp);
4659ad0173dSBjoern A. Zeeb #endif
466df8bae1dSRodney W. Grimes 	if (len > tp->t_maxseg) {
467b3c0f300SAndre Oppermann 		if ((tp->t_flags & TF_TSO) && tcp_do_tso &&
468b3c0f300SAndre Oppermann 		    ((tp->t_flags & TF_SIGNATURE) == 0) &&
469b3c0f300SAndre Oppermann 		    tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
470b3c0f300SAndre Oppermann 		    tp->t_inpcb->inp_options == NULL &&
4719ad0173dSBjoern A. Zeeb 		    tp->t_inpcb->in6p_options == NULL
4729ad0173dSBjoern A. Zeeb #ifdef IPSEC
4739ad0173dSBjoern A. Zeeb 		    && ipsec_optlen == 0
4749ad0173dSBjoern A. Zeeb #endif
4759ad0173dSBjoern A. Zeeb 		    ) {
476b3c0f300SAndre Oppermann 			tso = 1;
477b3c0f300SAndre Oppermann 		} else {
478df8bae1dSRodney W. Grimes 			len = tp->t_maxseg;
479df8bae1dSRodney W. Grimes 			sendalot = 1;
480b3c0f300SAndre Oppermann 			tso = 0;
481b3c0f300SAndre Oppermann 		}
482df8bae1dSRodney W. Grimes 	}
4835d3b1b75SJayanth Vijayaraghavan 	if (sack_rxmit) {
4845d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
485df8bae1dSRodney W. Grimes 			flags &= ~TH_FIN;
4865d3b1b75SJayanth Vijayaraghavan 	} else {
4875d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
4885d3b1b75SJayanth Vijayaraghavan 			flags &= ~TH_FIN;
4895d3b1b75SJayanth Vijayaraghavan 	}
490df8bae1dSRodney W. Grimes 
491201d185bSAndre Oppermann 	recwin = sbspace(&so->so_rcv);
492df8bae1dSRodney W. Grimes 
493df8bae1dSRodney W. Grimes 	/*
494262c1c1aSMatthew Dillon 	 * Sender silly window avoidance.   We transmit under the following
495262c1c1aSMatthew Dillon 	 * conditions when len is non-zero:
496262c1c1aSMatthew Dillon 	 *
497b3c0f300SAndre Oppermann 	 *	- We have a full segment (or more with TSO)
498262c1c1aSMatthew Dillon 	 *	- This is the last buffer in a write()/send() and we are
499262c1c1aSMatthew Dillon 	 *	  either idle or running NODELAY
500262c1c1aSMatthew Dillon 	 *	- we've timed out (e.g. persist timer)
501262c1c1aSMatthew Dillon 	 *	- we have more then 1/2 the maximum send window's worth of
502262c1c1aSMatthew Dillon 	 *	  data (receiver may be limited the window size)
503262c1c1aSMatthew Dillon 	 *	- we need to retransmit
504df8bae1dSRodney W. Grimes 	 */
505df8bae1dSRodney W. Grimes 	if (len) {
506b3c0f300SAndre Oppermann 		if (len >= tp->t_maxseg)
507df8bae1dSRodney W. Grimes 			goto send;
508262c1c1aSMatthew Dillon 		/*
509262c1c1aSMatthew Dillon 		 * NOTE! on localhost connections an 'ack' from the remote
510262c1c1aSMatthew Dillon 		 * end may occur synchronously with the output and cause
511262c1c1aSMatthew Dillon 		 * us to flush a buffer queued with moretocome.  XXX
512262c1c1aSMatthew Dillon 		 *
513262c1c1aSMatthew Dillon 		 * note: the len + off check is almost certainly unnecessary.
514262c1c1aSMatthew Dillon 		 */
515262c1c1aSMatthew Dillon 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
516262c1c1aSMatthew Dillon 		    (idle || (tp->t_flags & TF_NODELAY)) &&
517262c1c1aSMatthew Dillon 		    len + off >= so->so_snd.sb_cc &&
518262c1c1aSMatthew Dillon 		    (tp->t_flags & TF_NOPUSH) == 0) {
519df8bae1dSRodney W. Grimes 			goto send;
520262c1c1aSMatthew Dillon 		}
5212cdbfa66SPaul Saab 		if (tp->t_flags & TF_FORCEDATA)		/* typ. timeout case */
522df8bae1dSRodney W. Grimes 			goto send;
523a0292f23SGarrett Wollman 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
524df8bae1dSRodney W. Grimes 			goto send;
525262c1c1aSMatthew Dillon 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))	/* retransmit case */
526df8bae1dSRodney W. Grimes 			goto send;
5276d90faf3SPaul Saab 		if (sack_rxmit)
5286d90faf3SPaul Saab 			goto send;
529df8bae1dSRodney W. Grimes 	}
530df8bae1dSRodney W. Grimes 
531df8bae1dSRodney W. Grimes 	/*
532df8bae1dSRodney W. Grimes 	 * Compare available window to amount of window
533df8bae1dSRodney W. Grimes 	 * known to peer (as advertised window less
534df8bae1dSRodney W. Grimes 	 * next expected input).  If the difference is at least two
535df8bae1dSRodney W. Grimes 	 * max size segments, or at least 50% of the maximum possible
536df8bae1dSRodney W. Grimes 	 * window, then want to send a window update to peer.
5373bfd6421SJonathan Lemon 	 * Skip this if the connection is in T/TCP half-open state.
538b7de7d87SAndre Oppermann 	 * Don't send pure window updates when the peer has closed
539b7de7d87SAndre Oppermann 	 * the connection and won't ever send more data.
540df8bae1dSRodney W. Grimes 	 */
541b7de7d87SAndre Oppermann 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) &&
542b7de7d87SAndre Oppermann 	    !TCPS_HAVERCVDFIN(tp->t_state)) {
543df8bae1dSRodney W. Grimes 		/*
544df8bae1dSRodney W. Grimes 		 * "adv" is the amount we can increase the window,
545df8bae1dSRodney W. Grimes 		 * taking into account that we are limited by
546df8bae1dSRodney W. Grimes 		 * TCP_MAXWIN << tp->rcv_scale.
547df8bae1dSRodney W. Grimes 		 */
548201d185bSAndre Oppermann 		long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
549df8bae1dSRodney W. Grimes 			(tp->rcv_adv - tp->rcv_nxt);
550df8bae1dSRodney W. Grimes 
551df8bae1dSRodney W. Grimes 		if (adv >= (long) (2 * tp->t_maxseg))
552df8bae1dSRodney W. Grimes 			goto send;
553df8bae1dSRodney W. Grimes 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
554df8bae1dSRodney W. Grimes 			goto send;
555df8bae1dSRodney W. Grimes 	}
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes 	/*
55828257b5cSMatthew Dillon 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
55928257b5cSMatthew Dillon 	 * is also a catch-all for the retransmit timer timeout case.
560df8bae1dSRodney W. Grimes 	 */
561df8bae1dSRodney W. Grimes 	if (tp->t_flags & TF_ACKNOW)
562df8bae1dSRodney W. Grimes 		goto send;
563a0292f23SGarrett Wollman 	if ((flags & TH_RST) ||
564a0292f23SGarrett Wollman 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
565df8bae1dSRodney W. Grimes 		goto send;
566df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_una))
567df8bae1dSRodney W. Grimes 		goto send;
568df8bae1dSRodney W. Grimes 	/*
569df8bae1dSRodney W. Grimes 	 * If our state indicates that FIN should be sent
57028257b5cSMatthew Dillon 	 * and we have not yet done so, then we need to send.
571df8bae1dSRodney W. Grimes 	 */
572df8bae1dSRodney W. Grimes 	if (flags & TH_FIN &&
573df8bae1dSRodney W. Grimes 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
574df8bae1dSRodney W. Grimes 		goto send;
5756d90faf3SPaul Saab 	/*
5766d90faf3SPaul Saab 	 * In SACK, it is possible for tcp_output to fail to send a segment
5776d90faf3SPaul Saab 	 * after the retransmission timer has been turned off.  Make sure
5786d90faf3SPaul Saab 	 * that the retransmission timer is set.
5796d90faf3SPaul Saab 	 */
5803529149eSAndre Oppermann 	if ((tp->t_flags & TF_SACK_PERMIT) &&
5813529149eSAndre Oppermann 	    SEQ_GT(tp->snd_max, tp->snd_una) &&
582b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_REXMT) &&
583b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
584b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
585cf2942b6SRobert Watson 		goto just_return;
5866d90faf3SPaul Saab 	}
587df8bae1dSRodney W. Grimes 	/*
588df8bae1dSRodney W. Grimes 	 * TCP window updates are not reliable, rather a polling protocol
589df8bae1dSRodney W. Grimes 	 * using ``persist'' packets is used to insure receipt of window
590df8bae1dSRodney W. Grimes 	 * updates.  The three ``states'' for the output side are:
591df8bae1dSRodney W. Grimes 	 *	idle			not doing retransmits or persists
592df8bae1dSRodney W. Grimes 	 *	persisting		to move a small or zero window
593df8bae1dSRodney W. Grimes 	 *	(re)transmitting	and thereby not persisting
594df8bae1dSRodney W. Grimes 	 *
595b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_PERSIST)
5969b8b58e0SJonathan Lemon 	 *	is true when we are in persist state.
5972cdbfa66SPaul Saab 	 * (tp->t_flags & TF_FORCEDATA)
598df8bae1dSRodney W. Grimes 	 *	is set when we are called to send a persist packet.
599b8152ba7SAndre Oppermann 	 * tcp_timer_active(tp, TT_REXMT)
600df8bae1dSRodney W. Grimes 	 *	is set when we are retransmitting
601df8bae1dSRodney W. Grimes 	 * The output side is idle when both timers are zero.
602df8bae1dSRodney W. Grimes 	 *
603df8bae1dSRodney W. Grimes 	 * If send window is too small, there is data to transmit, and no
604df8bae1dSRodney W. Grimes 	 * retransmit or persist is pending, then go to persist state.
605df8bae1dSRodney W. Grimes 	 * If nothing happens soon, send when timer expires:
606df8bae1dSRodney W. Grimes 	 * if window is nonzero, transmit what we can,
607df8bae1dSRodney W. Grimes 	 * otherwise force out a byte.
608df8bae1dSRodney W. Grimes 	 */
609b8152ba7SAndre Oppermann 	if (so->so_snd.sb_cc && !tcp_timer_active(tp, TT_REXMT) &&
610b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
611df8bae1dSRodney W. Grimes 		tp->t_rxtshift = 0;
612df8bae1dSRodney W. Grimes 		tcp_setpersist(tp);
613df8bae1dSRodney W. Grimes 	}
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes 	/*
616df8bae1dSRodney W. Grimes 	 * No reason to send a segment, just return.
617df8bae1dSRodney W. Grimes 	 */
618cf2942b6SRobert Watson just_return:
619cf2942b6SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
620df8bae1dSRodney W. Grimes 	return (0);
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes send:
623cf2942b6SRobert Watson 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
624df8bae1dSRodney W. Grimes 	/*
625df8bae1dSRodney W. Grimes 	 * Before ESTABLISHED, force sending of initial options
626df8bae1dSRodney W. Grimes 	 * unless TCP set not to do any options.
627df8bae1dSRodney W. Grimes 	 * NOTE: we assume that the IP/TCP header plus TCP options
628df8bae1dSRodney W. Grimes 	 * always fit in a single mbuf, leaving room for a maximum
629df8bae1dSRodney W. Grimes 	 * link header, i.e.
63033841545SHajimu UMEMOTO 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
631df8bae1dSRodney W. Grimes 	 */
632df8bae1dSRodney W. Grimes 	optlen = 0;
633fb59c426SYoshinobu Inoue #ifdef INET6
634fb59c426SYoshinobu Inoue 	if (isipv6)
635fb59c426SYoshinobu Inoue 		hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
636fb59c426SYoshinobu Inoue 	else
637fb59c426SYoshinobu Inoue #endif
638df8bae1dSRodney W. Grimes 	hdrlen = sizeof (struct tcpiphdr);
63902a1a643SAndre Oppermann 
64002a1a643SAndre Oppermann 	/*
64102a1a643SAndre Oppermann 	 * Compute options for segment.
64202a1a643SAndre Oppermann 	 * We only have to care about SYN and established connection
64302a1a643SAndre Oppermann 	 * segments.  Options for SYN-ACK segments are handled in TCP
64402a1a643SAndre Oppermann 	 * syncache.
64502a1a643SAndre Oppermann 	 */
64602a1a643SAndre Oppermann 	if ((tp->t_flags & TF_NOOPT) == 0) {
64702a1a643SAndre Oppermann 		to.to_flags = 0;
64802a1a643SAndre Oppermann 		/* Maximum segment size. */
649df8bae1dSRodney W. Grimes 		if (flags & TH_SYN) {
650df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->iss;
65102a1a643SAndre Oppermann 			to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc);
65202a1a643SAndre Oppermann 			to.to_flags |= TOF_MSS;
653df8bae1dSRodney W. Grimes 		}
65402a1a643SAndre Oppermann 		/* Window scaling. */
65502a1a643SAndre Oppermann 		if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) {
65602a1a643SAndre Oppermann 			to.to_wscale = tp->request_r_scale;
65702a1a643SAndre Oppermann 			to.to_flags |= TOF_SCALE;
658df8bae1dSRodney W. Grimes 		}
65902a1a643SAndre Oppermann 		/* Timestamps. */
66002a1a643SAndre Oppermann 		if ((tp->t_flags & TF_RCVD_TSTMP) ||
66102a1a643SAndre Oppermann 		    ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) {
66202a1a643SAndre Oppermann 			to.to_tsval = ticks + tp->ts_offset;
66302a1a643SAndre Oppermann 			to.to_tsecr = tp->ts_recent;
66402a1a643SAndre Oppermann 			to.to_flags |= TOF_TS;
6656741ecf5SAndre Oppermann 			/* Set receive buffer autosizing timestamp. */
66602a1a643SAndre Oppermann 			if (tp->rfbuf_ts == 0 &&
66702a1a643SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE))
6686741ecf5SAndre Oppermann 				tp->rfbuf_ts = ticks;
6691cfd4b53SBruce M Simpson 		}
67002a1a643SAndre Oppermann 		/* Selective ACK's. */
6713529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
67202a1a643SAndre Oppermann 			if (flags & TH_SYN)
67302a1a643SAndre Oppermann 				to.to_flags |= TOF_SACKPERM;
67402a1a643SAndre Oppermann 			else if (TCPS_HAVEESTABLISHED(tp->t_state) &&
67502a1a643SAndre Oppermann 			    (tp->t_flags & TF_SACK_PERMIT) &&
67602a1a643SAndre Oppermann 			    tp->rcv_numsacks > 0) {
67702a1a643SAndre Oppermann 				to.to_flags |= TOF_SACK;
67802a1a643SAndre Oppermann 				to.to_nsacks = tp->rcv_numsacks;
67902a1a643SAndre Oppermann 				to.to_sacks = (u_char *)tp->sackblks;
68002a1a643SAndre Oppermann 			}
68102a1a643SAndre Oppermann 		}
68202a1a643SAndre Oppermann #ifdef TCP_SIGNATURE
68302a1a643SAndre Oppermann 		/* TCP-MD5 (RFC2385). */
68402a1a643SAndre Oppermann #ifdef INET6
68502a1a643SAndre Oppermann 		if (!isipv6 && (tp->t_flags & TF_SIGNATURE))
68602a1a643SAndre Oppermann #else
68702a1a643SAndre Oppermann 		if (tp->t_flags & TF_SIGNATURE)
68802a1a643SAndre Oppermann #endif /* INET6 */
68902a1a643SAndre Oppermann 			to.to_flags |= TOF_SIGNATURE;
6901cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
6911cfd4b53SBruce M Simpson 
69202a1a643SAndre Oppermann 		/* Processing the options. */
6934a411b9fSBjoern A. Zeeb 		hdrlen += optlen = tcp_addoptions(&to, opt);
694be3f3b5eSPaul Saab 	}
695be3f3b5eSPaul Saab 
696fb59c426SYoshinobu Inoue #ifdef INET6
697fb59c426SYoshinobu Inoue 	if (isipv6)
698fb59c426SYoshinobu Inoue 		ipoptlen = ip6_optlen(tp->t_inpcb);
699fb59c426SYoshinobu Inoue 	else
700fb59c426SYoshinobu Inoue #endif
701f10e85d7SLuigi Rizzo 	if (tp->t_inpcb->inp_options)
702a04884fcSBill Fenner 		ipoptlen = tp->t_inpcb->inp_options->m_len -
703a04884fcSBill Fenner 				offsetof(struct ipoption, ipopt_list);
704f10e85d7SLuigi Rizzo 	else
705a04884fcSBill Fenner 		ipoptlen = 0;
706b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
7079ad0173dSBjoern A. Zeeb 	ipoptlen += ipsec_optlen;
708fb59c426SYoshinobu Inoue #endif
709a04884fcSBill Fenner 
710df8bae1dSRodney W. Grimes 	/*
711df8bae1dSRodney W. Grimes 	 * Adjust data length if insertion of options will
712a0292f23SGarrett Wollman 	 * bump the packet length beyond the t_maxopd length.
713a0292f23SGarrett Wollman 	 * Clear the FIN bit because we cut off the tail of
714a0292f23SGarrett Wollman 	 * the segment.
715b3c0f300SAndre Oppermann 	 *
71631ecb34aSAndre Oppermann 	 * When doing TSO limit a burst to TCP_MAXWIN minus the
71731ecb34aSAndre Oppermann 	 * IP, TCP and Options length to keep ip->ip_len from
71831ecb34aSAndre Oppermann 	 * overflowing.  Prevent the last segment from being
71931ecb34aSAndre Oppermann 	 * fractional thus making them all equal sized and set
7206aa5b623SAndre Oppermann 	 * the flag to continue sending.  TSO is disabled when
7216aa5b623SAndre Oppermann 	 * IP options or IPSEC are present.
722df8bae1dSRodney W. Grimes 	 */
723a04884fcSBill Fenner 	if (len + optlen + ipoptlen > tp->t_maxopd) {
724297a37f3SDavid Greenman 		flags &= ~TH_FIN;
725b3c0f300SAndre Oppermann 		if (tso) {
726eec9d82dSAndre Oppermann 			if (len > TCP_MAXWIN - hdrlen - optlen) {
7276aa5b623SAndre Oppermann 				len = TCP_MAXWIN - hdrlen - optlen;
72831ecb34aSAndre Oppermann 				len = len - (len % (tp->t_maxopd - optlen));
729b3c0f300SAndre Oppermann 				sendalot = 1;
730b3c0f300SAndre Oppermann 			} else if (tp->t_flags & TF_NEEDFIN)
731b3c0f300SAndre Oppermann 				sendalot = 1;
732b3c0f300SAndre Oppermann 		} else {
733a04884fcSBill Fenner 			len = tp->t_maxopd - optlen - ipoptlen;
734df8bae1dSRodney W. Grimes 			sendalot = 1;
735df8bae1dSRodney W. Grimes 		}
736b3c0f300SAndre Oppermann 	}
737df8bae1dSRodney W. Grimes 
738a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/
739a683a7ddSYoshinobu Inoue #ifdef INET6
740a683a7ddSYoshinobu Inoue 	if (max_linkhdr + hdrlen > MCLBYTES)
741a683a7ddSYoshinobu Inoue #else
742df8bae1dSRodney W. Grimes 	if (max_linkhdr + hdrlen > MHLEN)
743a683a7ddSYoshinobu Inoue #endif
744f10e85d7SLuigi Rizzo 		panic("tcphdr too big");
745a0292f23SGarrett Wollman /*#endif*/
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes 	/*
748df8bae1dSRodney W. Grimes 	 * Grab a header mbuf, attaching a copy of data to
749df8bae1dSRodney W. Grimes 	 * be transmitted, and initialize the header from
750df8bae1dSRodney W. Grimes 	 * the template for sends on this connection.
751df8bae1dSRodney W. Grimes 	 */
752df8bae1dSRodney W. Grimes 	if (len) {
7534e023759SAndre Oppermann 		struct mbuf *mb;
7544e023759SAndre Oppermann 		u_int moff;
7554e023759SAndre Oppermann 
7562cdbfa66SPaul Saab 		if ((tp->t_flags & TF_FORCEDATA) && len == 1)
757df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndprobe++;
7580ba5d2eeSJohn Baldwin 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) {
759df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitpack++;
760df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitbyte += len;
761df8bae1dSRodney W. Grimes 		} else {
762df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndpack++;
763df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndbyte += len;
764df8bae1dSRodney W. Grimes 		}
765df8bae1dSRodney W. Grimes #ifdef notyet
766df8bae1dSRodney W. Grimes 		if ((m = m_copypack(so->so_snd.sb_mb, off,
767df8bae1dSRodney W. Grimes 		    (int)len, max_linkhdr + hdrlen)) == 0) {
768cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
769df8bae1dSRodney W. Grimes 			error = ENOBUFS;
770df8bae1dSRodney W. Grimes 			goto out;
771df8bae1dSRodney W. Grimes 		}
772df8bae1dSRodney W. Grimes 		/*
773df8bae1dSRodney W. Grimes 		 * m_copypack left space for our hdr; use it.
774df8bae1dSRodney W. Grimes 		 */
775df8bae1dSRodney W. Grimes 		m->m_len += hdrlen;
776df8bae1dSRodney W. Grimes 		m->m_data -= hdrlen;
777df8bae1dSRodney W. Grimes #else
77834333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
779df8bae1dSRodney W. Grimes 		if (m == NULL) {
780cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
781df8bae1dSRodney W. Grimes 			error = ENOBUFS;
782df8bae1dSRodney W. Grimes 			goto out;
783df8bae1dSRodney W. Grimes 		}
784fb59c426SYoshinobu Inoue #ifdef INET6
785a683a7ddSYoshinobu Inoue 		if (MHLEN < hdrlen + max_linkhdr) {
786a163d034SWarner Losh 			MCLGET(m, M_DONTWAIT);
787a683a7ddSYoshinobu Inoue 			if ((m->m_flags & M_EXT) == 0) {
788cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
789a683a7ddSYoshinobu Inoue 				m_freem(m);
790a683a7ddSYoshinobu Inoue 				error = ENOBUFS;
791a683a7ddSYoshinobu Inoue 				goto out;
792a683a7ddSYoshinobu Inoue 			}
793a683a7ddSYoshinobu Inoue 		}
794fb59c426SYoshinobu Inoue #endif
795df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
796df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
7974e023759SAndre Oppermann 
7984e023759SAndre Oppermann 		/*
7994e023759SAndre Oppermann 		 * Start the m_copy functions from the closest mbuf
8004e023759SAndre Oppermann 		 * to the offset in the socket buffer chain.
8014e023759SAndre Oppermann 		 */
8024e023759SAndre Oppermann 		mb = sbsndptr(&so->so_snd, off, len, &moff);
8034e023759SAndre Oppermann 
804df8bae1dSRodney W. Grimes 		if (len <= MHLEN - hdrlen - max_linkhdr) {
8054e023759SAndre Oppermann 			m_copydata(mb, moff, (int)len,
806df8bae1dSRodney W. Grimes 			    mtod(m, caddr_t) + hdrlen);
807df8bae1dSRodney W. Grimes 			m->m_len += len;
808df8bae1dSRodney W. Grimes 		} else {
8094e023759SAndre Oppermann 			m->m_next = m_copy(mb, moff, (int)len);
8104e023759SAndre Oppermann 			if (m->m_next == NULL) {
811cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
812d7f570e6SGarrett Wollman 				(void) m_free(m);
81351823c3aSGarrett Wollman 				error = ENOBUFS;
81451823c3aSGarrett Wollman 				goto out;
81551823c3aSGarrett Wollman 			}
816df8bae1dSRodney W. Grimes 		}
817df8bae1dSRodney W. Grimes #endif
818df8bae1dSRodney W. Grimes 		/*
819df8bae1dSRodney W. Grimes 		 * If we're sending everything we've got, set PUSH.
820df8bae1dSRodney W. Grimes 		 * (This will keep happy those implementations which only
821df8bae1dSRodney W. Grimes 		 * give data to the user when a buffer fills or
822df8bae1dSRodney W. Grimes 		 * a PUSH comes in.)
823df8bae1dSRodney W. Grimes 		 */
824df8bae1dSRodney W. Grimes 		if (off + len == so->so_snd.sb_cc)
825df8bae1dSRodney W. Grimes 			flags |= TH_PUSH;
826cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
827df8bae1dSRodney W. Grimes 	} else {
828cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
829df8bae1dSRodney W. Grimes 		if (tp->t_flags & TF_ACKNOW)
830df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndacks++;
831df8bae1dSRodney W. Grimes 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
832df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndctrl++;
833df8bae1dSRodney W. Grimes 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
834df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndurg++;
835df8bae1dSRodney W. Grimes 		else
836df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndwinup++;
837df8bae1dSRodney W. Grimes 
83834333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
839df8bae1dSRodney W. Grimes 		if (m == NULL) {
840df8bae1dSRodney W. Grimes 			error = ENOBUFS;
841df8bae1dSRodney W. Grimes 			goto out;
842df8bae1dSRodney W. Grimes 		}
843fb59c426SYoshinobu Inoue #ifdef INET6
844fb59c426SYoshinobu Inoue 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
845fb59c426SYoshinobu Inoue 		    MHLEN >= hdrlen) {
846fb59c426SYoshinobu Inoue 			MH_ALIGN(m, hdrlen);
847fb59c426SYoshinobu Inoue 		} else
848fb59c426SYoshinobu Inoue #endif
849df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
850df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
851df8bae1dSRodney W. Grimes 	}
852cf2942b6SRobert Watson 	SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
853df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = (struct ifnet *)0;
854c488362eSRobert Watson #ifdef MAC
85530d239bcSRobert Watson 	mac_inpcb_create_mbuf(tp->t_inpcb, m);
856c488362eSRobert Watson #endif
857fb59c426SYoshinobu Inoue #ifdef INET6
858fb59c426SYoshinobu Inoue 	if (isipv6) {
859fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
860fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip6 + 1);
86179909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip6, th);
862fb59c426SYoshinobu Inoue 	} else
863fb59c426SYoshinobu Inoue #endif /* INET6 */
864fb59c426SYoshinobu Inoue 	{
865fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
866fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
867fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip + 1);
86879909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip, th);
869fb59c426SYoshinobu Inoue 	}
870df8bae1dSRodney W. Grimes 
871df8bae1dSRodney W. Grimes 	/*
872df8bae1dSRodney W. Grimes 	 * Fill in fields, remembering maximum advertised
873df8bae1dSRodney W. Grimes 	 * window for use in delaying messages about window sizes.
874df8bae1dSRodney W. Grimes 	 * If resending a FIN, be sure not to use a new sequence number.
875df8bae1dSRodney W. Grimes 	 */
876df8bae1dSRodney W. Grimes 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
877df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max)
878df8bae1dSRodney W. Grimes 		tp->snd_nxt--;
879df8bae1dSRodney W. Grimes 	/*
880f2512ba1SRui Paulo 	 * If we are starting a connection, send ECN setup
881f2512ba1SRui Paulo 	 * SYN packet. If we are on a retransmit, we may
882f2512ba1SRui Paulo 	 * resend those bits a number of times as per
883f2512ba1SRui Paulo 	 * RFC 3168.
884f2512ba1SRui Paulo 	 */
885f2512ba1SRui Paulo 	if (tp->t_state == TCPS_SYN_SENT && tcp_do_ecn) {
886f2512ba1SRui Paulo 		if (tp->t_rxtshift >= 1) {
887f2512ba1SRui Paulo 			if (tp->t_rxtshift <= tcp_ecn_maxretries)
888f2512ba1SRui Paulo 				flags |= TH_ECE|TH_CWR;
889f2512ba1SRui Paulo 		} else
890f2512ba1SRui Paulo 			flags |= TH_ECE|TH_CWR;
891f2512ba1SRui Paulo 	}
892f2512ba1SRui Paulo 
893f2512ba1SRui Paulo 	if (tp->t_state == TCPS_ESTABLISHED &&
894f2512ba1SRui Paulo 	    (tp->t_flags & TF_ECN_PERMIT)) {
895f2512ba1SRui Paulo 		/*
896f2512ba1SRui Paulo 		 * If the peer has ECN, mark data packets with
897f2512ba1SRui Paulo 		 * ECN capable transmission (ECT).
898f2512ba1SRui Paulo 		 * Ignore pure ack packets, retransmissions and window probes.
899f2512ba1SRui Paulo 		 */
900f2512ba1SRui Paulo 		if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
901f2512ba1SRui Paulo 		    !((tp->t_flags & TF_FORCEDATA) && len == 1)) {
902f2512ba1SRui Paulo #ifdef INET6
903f2512ba1SRui Paulo 			if (isipv6)
904f2512ba1SRui Paulo 				ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
905f2512ba1SRui Paulo 			else
906f2512ba1SRui Paulo #endif
907f2512ba1SRui Paulo 				ip->ip_tos |= IPTOS_ECN_ECT0;
908f2512ba1SRui Paulo 			tcpstat.tcps_ecn_ect0++;
909f2512ba1SRui Paulo 		}
910f2512ba1SRui Paulo 
911f2512ba1SRui Paulo 		/*
912f2512ba1SRui Paulo 		 * Reply with proper ECN notifications.
913f2512ba1SRui Paulo 		 */
914f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_CWR) {
915f2512ba1SRui Paulo 			flags |= TH_CWR;
916f2512ba1SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_CWR;
917f2512ba1SRui Paulo 		}
918f2512ba1SRui Paulo 		if (tp->t_flags & TF_ECN_SND_ECE)
919f2512ba1SRui Paulo 			flags |= TH_ECE;
920f2512ba1SRui Paulo 	}
921f2512ba1SRui Paulo 
922f2512ba1SRui Paulo 	/*
923df8bae1dSRodney W. Grimes 	 * If we are doing retransmissions, then snd_nxt will
924df8bae1dSRodney W. Grimes 	 * not reflect the first unsent octet.  For ACK only
925df8bae1dSRodney W. Grimes 	 * packets, we do not want the sequence number of the
926df8bae1dSRodney W. Grimes 	 * retransmitted packet, we want the sequence number
927df8bae1dSRodney W. Grimes 	 * of the next unsent octet.  So, if there is no data
928df8bae1dSRodney W. Grimes 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
929df8bae1dSRodney W. Grimes 	 * when filling in ti_seq.  But if we are in persist
930df8bae1dSRodney W. Grimes 	 * state, snd_max might reflect one byte beyond the
931df8bae1dSRodney W. Grimes 	 * right edge of the window, so use snd_nxt in that
932df8bae1dSRodney W. Grimes 	 * case, since we know we aren't doing a retransmission.
933df8bae1dSRodney W. Grimes 	 * (retransmit and persist are mutually exclusive...)
934df8bae1dSRodney W. Grimes 	 */
935a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
936b8152ba7SAndre Oppermann 		if (len || (flags & (TH_SYN|TH_FIN)) ||
937b8152ba7SAndre Oppermann 		    tcp_timer_active(tp, TT_PERSIST))
938fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_nxt);
939df8bae1dSRodney W. Grimes 		else
940fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_max);
941a55db2b6SPaul Saab 	} else {
9426d90faf3SPaul Saab 		th->th_seq = htonl(p->rxmit);
9436d90faf3SPaul Saab 		p->rxmit += len;
9440077b016SPaul Saab 		tp->sackhint.sack_bytes_rexmit += len;
9456d90faf3SPaul Saab 	}
946fb59c426SYoshinobu Inoue 	th->th_ack = htonl(tp->rcv_nxt);
947df8bae1dSRodney W. Grimes 	if (optlen) {
948fb59c426SYoshinobu Inoue 		bcopy(opt, th + 1, optlen);
949fb59c426SYoshinobu Inoue 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
950df8bae1dSRodney W. Grimes 	}
951fb59c426SYoshinobu Inoue 	th->th_flags = flags;
952df8bae1dSRodney W. Grimes 	/*
953df8bae1dSRodney W. Grimes 	 * Calculate receive window.  Don't shrink window,
954df8bae1dSRodney W. Grimes 	 * but avoid silly window syndrome.
955df8bae1dSRodney W. Grimes 	 */
956201d185bSAndre Oppermann 	if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
957201d185bSAndre Oppermann 	    recwin < (long)tp->t_maxseg)
958201d185bSAndre Oppermann 		recwin = 0;
959201d185bSAndre Oppermann 	if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
960201d185bSAndre Oppermann 		recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
961201d185bSAndre Oppermann 	if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
962201d185bSAndre Oppermann 		recwin = (long)TCP_MAXWIN << tp->rcv_scale;
963262c1c1aSMatthew Dillon 
964104ebb2aSAndre Oppermann 	/*
965104ebb2aSAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
966104ebb2aSAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.  The <SYN,ACK>
967104ebb2aSAndre Oppermann 	 * case is handled in syncache.
968104ebb2aSAndre Oppermann 	 */
969104ebb2aSAndre Oppermann 	if (flags & TH_SYN)
970104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)
971104ebb2aSAndre Oppermann 				(min(sbspace(&so->so_rcv), TCP_MAXWIN)));
972104ebb2aSAndre Oppermann 	else
973104ebb2aSAndre Oppermann 		th->th_win = htons((u_short)(recwin >> tp->rcv_scale));
974262c1c1aSMatthew Dillon 
975262c1c1aSMatthew Dillon 	/*
976262c1c1aSMatthew Dillon 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised
977262c1c1aSMatthew Dillon 	 * a 0 window.  This may cause the remote transmitter to stall.  This
978262c1c1aSMatthew Dillon 	 * flag tells soreceive() to disable delayed acknowledgements when
979262c1c1aSMatthew Dillon 	 * draining the buffer.  This can occur if the receiver is attempting
980b2722702SRui Paulo 	 * to read more data than can be buffered prior to transmitting on
981262c1c1aSMatthew Dillon 	 * the connection.
982262c1c1aSMatthew Dillon 	 */
983201d185bSAndre Oppermann 	if (recwin == 0)
984262c1c1aSMatthew Dillon 		tp->t_flags |= TF_RXWIN0SENT;
985262c1c1aSMatthew Dillon 	else
986262c1c1aSMatthew Dillon 		tp->t_flags &= ~TF_RXWIN0SENT;
987df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
988fb59c426SYoshinobu Inoue 		th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
989fb59c426SYoshinobu Inoue 		th->th_flags |= TH_URG;
990df8bae1dSRodney W. Grimes 	} else
991df8bae1dSRodney W. Grimes 		/*
992df8bae1dSRodney W. Grimes 		 * If no urgent pointer to send, then we pull
993df8bae1dSRodney W. Grimes 		 * the urgent pointer to the left edge of the send window
994df8bae1dSRodney W. Grimes 		 * so that it doesn't drift into the send window on sequence
995df8bae1dSRodney W. Grimes 		 * number wraparound.
996df8bae1dSRodney W. Grimes 		 */
997df8bae1dSRodney W. Grimes 		tp->snd_up = tp->snd_una;		/* drag it along */
998df8bae1dSRodney W. Grimes 
9991cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
10001cfd4b53SBruce M Simpson #ifdef INET6
10011cfd4b53SBruce M Simpson 	if (!isipv6)
10021cfd4b53SBruce M Simpson #endif
1003ee763d0dSBjoern A. Zeeb 	if (tp->t_flags & TF_SIGNATURE) {
1004ee763d0dSBjoern A. Zeeb 		int sigoff = to.to_signature - opt;
1005265ed012SBruce M Simpson 		tcp_signature_compute(m, sizeof(struct ip), len, optlen,
10061cfd4b53SBruce M Simpson 		    (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
1007ee763d0dSBjoern A. Zeeb 	}
1008265ed012SBruce M Simpson #endif
10091cfd4b53SBruce M Simpson 
1010df8bae1dSRodney W. Grimes 	/*
1011df8bae1dSRodney W. Grimes 	 * Put TCP length in extended header, and then
1012df8bae1dSRodney W. Grimes 	 * checksum extended header and data.
1013df8bae1dSRodney W. Grimes 	 */
1014fb59c426SYoshinobu Inoue 	m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
1015fb59c426SYoshinobu Inoue #ifdef INET6
1016fb59c426SYoshinobu Inoue 	if (isipv6)
1017fb59c426SYoshinobu Inoue 		/*
1018fb59c426SYoshinobu Inoue 		 * ip6_plen is not need to be filled now, and will be filled
1019fb59c426SYoshinobu Inoue 		 * in ip6_output.
1020fb59c426SYoshinobu Inoue 		 */
1021fb59c426SYoshinobu Inoue 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
1022fb59c426SYoshinobu Inoue 				       sizeof(struct tcphdr) + optlen + len);
1023fb59c426SYoshinobu Inoue 	else
1024fb59c426SYoshinobu Inoue #endif /* INET6 */
1025fb59c426SYoshinobu Inoue 	{
1026db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_TCP;
1027db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
102879909384SJonathan Lemon 		th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
102979909384SJonathan Lemon 		    htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
1030fb59c426SYoshinobu Inoue 
1031db4f9cc7SJonathan Lemon 		/* IP version must be set here for ipv4/ipv6 checking later */
1032db4f9cc7SJonathan Lemon 		KASSERT(ip->ip_v == IPVERSION,
10336e551fb6SDavid E. O'Brien 		    ("%s: IP version incorrect: %d", __func__, ip->ip_v));
1034fb59c426SYoshinobu Inoue 	}
1035df8bae1dSRodney W. Grimes 
1036df8bae1dSRodney W. Grimes 	/*
1037b3c0f300SAndre Oppermann 	 * Enable TSO and specify the size of the segments.
1038b3c0f300SAndre Oppermann 	 * The TCP pseudo header checksum is always provided.
1039b3c0f300SAndre Oppermann 	 * XXX: Fixme: This is currently not the case for IPv6.
1040b3c0f300SAndre Oppermann 	 */
1041b3c0f300SAndre Oppermann 	if (tso) {
1042b3c0f300SAndre Oppermann 		m->m_pkthdr.csum_flags = CSUM_TSO;
1043b3c0f300SAndre Oppermann 		m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen;
1044b3c0f300SAndre Oppermann 	}
1045b3c0f300SAndre Oppermann 
1046b3c0f300SAndre Oppermann 	/*
1047df8bae1dSRodney W. Grimes 	 * In transmit state, time the transmission and arrange for
1048df8bae1dSRodney W. Grimes 	 * the retransmit.  In persist state, just set snd_max.
1049df8bae1dSRodney W. Grimes 	 */
10502cdbfa66SPaul Saab 	if ((tp->t_flags & TF_FORCEDATA) == 0 ||
1051b8152ba7SAndre Oppermann 	    !tcp_timer_active(tp, TT_PERSIST)) {
1052df8bae1dSRodney W. Grimes 		tcp_seq startseq = tp->snd_nxt;
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes 		/*
1055df8bae1dSRodney W. Grimes 		 * Advance snd_nxt over sequence space of this segment.
1056df8bae1dSRodney W. Grimes 		 */
1057df8bae1dSRodney W. Grimes 		if (flags & (TH_SYN|TH_FIN)) {
1058df8bae1dSRodney W. Grimes 			if (flags & TH_SYN)
1059df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
1060df8bae1dSRodney W. Grimes 			if (flags & TH_FIN) {
1061df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
1062df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_SENTFIN;
1063df8bae1dSRodney W. Grimes 			}
1064df8bae1dSRodney W. Grimes 		}
1065a55db2b6SPaul Saab 		if (sack_rxmit)
10666d90faf3SPaul Saab 			goto timer;
1067df8bae1dSRodney W. Grimes 		tp->snd_nxt += len;
1068df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1069df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt;
1070df8bae1dSRodney W. Grimes 			/*
1071df8bae1dSRodney W. Grimes 			 * Time this transmission if not a retransmission and
1072df8bae1dSRodney W. Grimes 			 * not currently timing anything.
1073df8bae1dSRodney W. Grimes 			 */
10749b8b58e0SJonathan Lemon 			if (tp->t_rtttime == 0) {
10759b8b58e0SJonathan Lemon 				tp->t_rtttime = ticks;
1076df8bae1dSRodney W. Grimes 				tp->t_rtseq = startseq;
1077df8bae1dSRodney W. Grimes 				tcpstat.tcps_segstimed++;
1078df8bae1dSRodney W. Grimes 			}
1079df8bae1dSRodney W. Grimes 		}
1080df8bae1dSRodney W. Grimes 
1081df8bae1dSRodney W. Grimes 		/*
1082df8bae1dSRodney W. Grimes 		 * Set retransmit timer if not currently set,
108328257b5cSMatthew Dillon 		 * and not doing a pure ack or a keep-alive probe.
1084df8bae1dSRodney W. Grimes 		 * Initial value for retransmit timer is smoothed
1085df8bae1dSRodney W. Grimes 		 * round-trip time + 2 * round-trip time variance.
1086df8bae1dSRodney W. Grimes 		 * Initialize shift counter which is used for backoff
1087df8bae1dSRodney W. Grimes 		 * of retransmit time.
1088df8bae1dSRodney W. Grimes 		 */
10896d90faf3SPaul Saab timer:
10904b8e42baSAndre Oppermann 		if (!tcp_timer_active(tp, TT_REXMT) &&
1091a55db2b6SPaul Saab 		    ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1092a55db2b6SPaul Saab 		     (tp->snd_nxt != tp->snd_una))) {
1093b8152ba7SAndre Oppermann 			if (tcp_timer_active(tp, TT_PERSIST)) {
1094b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_PERSIST, 0);
1095df8bae1dSRodney W. Grimes 				tp->t_rxtshift = 0;
1096df8bae1dSRodney W. Grimes 			}
1097b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
1098df8bae1dSRodney W. Grimes 		}
109928257b5cSMatthew Dillon 	} else {
110028257b5cSMatthew Dillon 		/*
110128257b5cSMatthew Dillon 		 * Persist case, update snd_max but since we are in
110228257b5cSMatthew Dillon 		 * persist mode (no window) we do not update snd_nxt.
110328257b5cSMatthew Dillon 		 */
110428257b5cSMatthew Dillon 		int xlen = len;
110528257b5cSMatthew Dillon 		if (flags & TH_SYN)
110628257b5cSMatthew Dillon 			++xlen;
110728257b5cSMatthew Dillon 		if (flags & TH_FIN) {
110828257b5cSMatthew Dillon 			++xlen;
110928257b5cSMatthew Dillon 			tp->t_flags |= TF_SENTFIN;
111028257b5cSMatthew Dillon 		}
1111abac41a6SMatthew Dillon 		if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
1112df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt + len;
111328257b5cSMatthew Dillon 	}
1114df8bae1dSRodney W. Grimes 
1115610ee2f9SDavid Greenman #ifdef TCPDEBUG
1116df8bae1dSRodney W. Grimes 	/*
1117df8bae1dSRodney W. Grimes 	 * Trace.
1118df8bae1dSRodney W. Grimes 	 */
111991f467d5SHartmut Brandt 	if (so->so_options & SO_DEBUG) {
1120f3e0b7efSBruce M Simpson 		u_short save = 0;
11215214cb3fSBruce M Simpson #ifdef INET6
11225214cb3fSBruce M Simpson 		if (!isipv6)
11235214cb3fSBruce M Simpson #endif
11245214cb3fSBruce M Simpson 		{
11255214cb3fSBruce M Simpson 			save = ipov->ih_len;
112691f467d5SHartmut Brandt 			ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
11275214cb3fSBruce M Simpson 		}
1128fb59c426SYoshinobu Inoue 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
11295214cb3fSBruce M Simpson #ifdef INET6
11305214cb3fSBruce M Simpson 		if (!isipv6)
11315214cb3fSBruce M Simpson #endif
113291f467d5SHartmut Brandt 		ipov->ih_len = save;
113391f467d5SHartmut Brandt 	}
1134610ee2f9SDavid Greenman #endif
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes 	/*
1137df8bae1dSRodney W. Grimes 	 * Fill in IP length and desired time to live and
1138df8bae1dSRodney W. Grimes 	 * send to IP level.  There should be a better way
1139df8bae1dSRodney W. Grimes 	 * to handle ttl and tos; we could keep them in
1140df8bae1dSRodney W. Grimes 	 * the template, but need a way to checksum without them.
1141df8bae1dSRodney W. Grimes 	 */
1142fb59c426SYoshinobu Inoue 	/*
1143fb59c426SYoshinobu Inoue 	 * m->m_pkthdr.len should have been set before cksum calcuration,
1144fb59c426SYoshinobu Inoue 	 * because in6_cksum() need it.
1145fb59c426SYoshinobu Inoue 	 */
1146fb59c426SYoshinobu Inoue #ifdef INET6
1147fb59c426SYoshinobu Inoue 	if (isipv6) {
1148fb59c426SYoshinobu Inoue 		/*
1149fb59c426SYoshinobu Inoue 		 * we separately set hoplimit for every segment, since the
1150fb59c426SYoshinobu Inoue 		 * user might want to change the value via setsockopt.
1151fb59c426SYoshinobu Inoue 		 * Also, desired default hop limit might be changed via
1152fb59c426SYoshinobu Inoue 		 * Neighbor Discovery.
1153fb59c426SYoshinobu Inoue 		 */
115497d8d152SAndre Oppermann 		ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1155fb59c426SYoshinobu Inoue 
1156fb59c426SYoshinobu Inoue 		/* TODO: IPv6 IP6TOS_ECT bit on */
1157fb59c426SYoshinobu Inoue 		error = ip6_output(m,
115897d8d152SAndre Oppermann 			    tp->t_inpcb->in6p_outputopts, NULL,
1159b5d47ff5SJohn-Mark Gurney 			    ((so->so_options & SO_DONTROUTE) ?
1160b5d47ff5SJohn-Mark Gurney 			    IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb);
1161fb59c426SYoshinobu Inoue 	} else
1162fb59c426SYoshinobu Inoue #endif /* INET6 */
1163df8bae1dSRodney W. Grimes     {
1164fb59c426SYoshinobu Inoue 	ip->ip_len = m->m_pkthdr.len;
1165686cdd19SJun-ichiro itojun Hagino #ifdef INET6
1166686cdd19SJun-ichiro itojun Hagino 	if (INP_CHECK_SOCKAF(so, AF_INET6))
116797d8d152SAndre Oppermann 		ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1168686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */
1169f138387aSGarrett Wollman 	/*
117097d8d152SAndre Oppermann 	 * If we do path MTU discovery, then we set DF on every packet.
117197d8d152SAndre Oppermann 	 * This might not be the best thing to do according to RFC3390
117297d8d152SAndre Oppermann 	 * Section 2. However the tcp hostcache migitates the problem
117397d8d152SAndre Oppermann 	 * so it affects only the first tcp connection with a host.
1174f138387aSGarrett Wollman 	 */
117597d8d152SAndre Oppermann 	if (path_mtu_discovery)
1176fb59c426SYoshinobu Inoue 		ip->ip_off |= IP_DF;
117797d8d152SAndre Oppermann 
117897d8d152SAndre Oppermann 	error = ip_output(m, tp->t_inpcb->inp_options, NULL,
1179b5d47ff5SJohn-Mark Gurney 	    ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0,
1180b5d47ff5SJohn-Mark Gurney 	    tp->t_inpcb);
1181df8bae1dSRodney W. Grimes     }
1182df8bae1dSRodney W. Grimes 	if (error) {
11837734ea06SArchie Cobbs 
11847734ea06SArchie Cobbs 		/*
11857734ea06SArchie Cobbs 		 * We know that the packet was lost, so back out the
11867734ea06SArchie Cobbs 		 * sequence number advance, if any.
11872c30ec0aSAndre Oppermann 		 *
11882c30ec0aSAndre Oppermann 		 * If the error is EPERM the packet got blocked by the
11892c30ec0aSAndre Oppermann 		 * local firewall.  Normally we should terminate the
11902c30ec0aSAndre Oppermann 		 * connection but the blocking may have been spurious
11912c30ec0aSAndre Oppermann 		 * due to a firewall reconfiguration cycle.  So we treat
11922c30ec0aSAndre Oppermann 		 * it like a packet loss and let the retransmit timer and
11932c30ec0aSAndre Oppermann 		 * timeouts do their work over time.
11942c30ec0aSAndre Oppermann 		 * XXX: It is a POLA question whether calling tcp_drop right
11952c30ec0aSAndre Oppermann 		 * away would be the really correct behavior instead.
11967734ea06SArchie Cobbs 		 */
119772757d9aSGleb Smirnoff 		if (((tp->t_flags & TF_FORCEDATA) == 0 ||
1198b8152ba7SAndre Oppermann 		    !tcp_timer_active(tp, TT_PERSIST)) &&
119972757d9aSGleb Smirnoff 		    ((flags & TH_SYN) == 0) &&
120072757d9aSGleb Smirnoff 		    (error != EPERM)) {
12010077b016SPaul Saab 			if (sack_rxmit) {
12025d3b1b75SJayanth Vijayaraghavan 				p->rxmit -= len;
12030077b016SPaul Saab 				tp->sackhint.sack_bytes_rexmit -= len;
120472757d9aSGleb Smirnoff 				KASSERT(tp->sackhint.sack_bytes_rexmit >= 0,
12050077b016SPaul Saab 				    ("sackhint bytes rtx >= 0"));
12060077b016SPaul Saab 			} else
12077734ea06SArchie Cobbs 				tp->snd_nxt -= len;
12087734ea06SArchie Cobbs 		}
1209df8bae1dSRodney W. Grimes out:
1210cf2942b6SRobert Watson 		SOCKBUF_UNLOCK_ASSERT(&so->so_snd);	/* Check gotos. */
121172757d9aSGleb Smirnoff 		switch (error) {
121272757d9aSGleb Smirnoff 		case EPERM:
121372757d9aSGleb Smirnoff 			tp->t_softerror = error;
121472757d9aSGleb Smirnoff 			return (error);
121572757d9aSGleb Smirnoff 		case ENOBUFS:
1216b8152ba7SAndre Oppermann 	                if (!tcp_timer_active(tp, TT_REXMT) &&
1217b8152ba7SAndre Oppermann 			    !tcp_timer_active(tp, TT_PERSIST))
1218b8152ba7SAndre Oppermann 	                        tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
12191600372bSAndre Oppermann 			tp->snd_cwnd = tp->t_maxseg;
1220df8bae1dSRodney W. Grimes 			return (0);
122172757d9aSGleb Smirnoff 		case EMSGSIZE:
12223d1f141bSGarrett Wollman 			/*
1223b3c0f300SAndre Oppermann 			 * For some reason the interface we used initially
1224b3c0f300SAndre Oppermann 			 * to send segments changed to another or lowered
1225b3c0f300SAndre Oppermann 			 * its MTU.
1226b3c0f300SAndre Oppermann 			 *
1227b3c0f300SAndre Oppermann 			 * tcp_mtudisc() will find out the new MTU and as
1228b3c0f300SAndre Oppermann 			 * its last action, initiate retransmission, so it
1229b3c0f300SAndre Oppermann 			 * is important to not do so here.
1230b3c0f300SAndre Oppermann 			 *
1231b3c0f300SAndre Oppermann 			 * If TSO was active we either got an interface
1232b3c0f300SAndre Oppermann 			 * without TSO capabilits or TSO was turned off.
1233b3c0f300SAndre Oppermann 			 * Disable it for this connection as too and
1234b3c0f300SAndre Oppermann 			 * immediatly retry with MSS sized segments generated
1235b3c0f300SAndre Oppermann 			 * by this function.
12363d1f141bSGarrett Wollman 			 */
1237b3c0f300SAndre Oppermann 			if (tso)
1238b3c0f300SAndre Oppermann 				tp->t_flags &= ~TF_TSO;
12393d1f141bSGarrett Wollman 			tcp_mtudisc(tp->t_inpcb, 0);
124072757d9aSGleb Smirnoff 			return (0);
12418bec3467SGleb Smirnoff 		case EHOSTDOWN:
124272757d9aSGleb Smirnoff 		case EHOSTUNREACH:
124372757d9aSGleb Smirnoff 		case ENETDOWN:
12448bec3467SGleb Smirnoff 		case ENETUNREACH:
124572757d9aSGleb Smirnoff 			if (TCPS_HAVERCVDSYN(tp->t_state)) {
1246df8bae1dSRodney W. Grimes 				tp->t_softerror = error;
1247df8bae1dSRodney W. Grimes 				return (0);
1248df8bae1dSRodney W. Grimes 			}
124972757d9aSGleb Smirnoff 			/* FALLTHROUGH */
125072757d9aSGleb Smirnoff 		default:
1251df8bae1dSRodney W. Grimes 			return (error);
1252df8bae1dSRodney W. Grimes 		}
125372757d9aSGleb Smirnoff 	}
1254df8bae1dSRodney W. Grimes 	tcpstat.tcps_sndtotal++;
1255df8bae1dSRodney W. Grimes 
1256df8bae1dSRodney W. Grimes 	/*
1257df8bae1dSRodney W. Grimes 	 * Data sent (as far as we can tell).
1258df8bae1dSRodney W. Grimes 	 * If this advertises a larger window than any other segment,
1259df8bae1dSRodney W. Grimes 	 * then remember the size of the advertised window.
1260df8bae1dSRodney W. Grimes 	 * Any pending ACK has now been sent.
1261df8bae1dSRodney W. Grimes 	 */
1262201d185bSAndre Oppermann 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1263201d185bSAndre Oppermann 		tp->rcv_adv = tp->rcv_nxt + recwin;
1264df8bae1dSRodney W. Grimes 	tp->last_ack_sent = tp->rcv_nxt;
12653bfd6421SJonathan Lemon 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
1266b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_DELACK))
1267b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, 0);
1268d912c694SMatthew Dillon #if 0
1269d912c694SMatthew Dillon 	/*
1270d912c694SMatthew Dillon 	 * This completely breaks TCP if newreno is turned on.  What happens
1271d912c694SMatthew Dillon 	 * is that if delayed-acks are turned on on the receiver, this code
1272d912c694SMatthew Dillon 	 * on the transmitter effectively destroys the TCP window, forcing
1273d912c694SMatthew Dillon 	 * it to four packets (1.5Kx4 = 6K window).
1274d912c694SMatthew Dillon 	 */
127546f58482SJonathan Lemon 	if (sendalot && (!tcp_do_newreno || --maxburst))
1276df8bae1dSRodney W. Grimes 		goto again;
1277d912c694SMatthew Dillon #endif
1278d912c694SMatthew Dillon 	if (sendalot)
1279d912c694SMatthew Dillon 		goto again;
1280df8bae1dSRodney W. Grimes 	return (0);
1281df8bae1dSRodney W. Grimes }
1282df8bae1dSRodney W. Grimes 
1283df8bae1dSRodney W. Grimes void
1284ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp)
1285df8bae1dSRodney W. Grimes {
12869b8b58e0SJonathan Lemon 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
12879b8b58e0SJonathan Lemon 	int tt;
1288df8bae1dSRodney W. Grimes 
1289b8152ba7SAndre Oppermann 	if (tcp_timer_active(tp, TT_REXMT))
12909b8b58e0SJonathan Lemon 		panic("tcp_setpersist: retransmit pending");
1291df8bae1dSRodney W. Grimes 	/*
1292df8bae1dSRodney W. Grimes 	 * Start/restart persistance timer.
1293df8bae1dSRodney W. Grimes 	 */
12949b8b58e0SJonathan Lemon 	TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1295df8bae1dSRodney W. Grimes 		      TCPTV_PERSMIN, TCPTV_PERSMAX);
1296b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_PERSIST, tt);
1297df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1298df8bae1dSRodney W. Grimes 		tp->t_rxtshift++;
1299df8bae1dSRodney W. Grimes }
130002a1a643SAndre Oppermann 
130102a1a643SAndre Oppermann /*
130202a1a643SAndre Oppermann  * Insert TCP options according to the supplied parameters to the place
130302a1a643SAndre Oppermann  * optp in a consistent way.  Can handle unaligned destinations.
130402a1a643SAndre Oppermann  *
130502a1a643SAndre Oppermann  * The order of the option processing is crucial for optimal packing and
130602a1a643SAndre Oppermann  * alignment for the scarce option space.
130702a1a643SAndre Oppermann  *
130802a1a643SAndre Oppermann  * The optimal order for a SYN/SYN-ACK segment is:
130902a1a643SAndre Oppermann  *   MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) +
131002a1a643SAndre Oppermann  *   Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40.
131102a1a643SAndre Oppermann  *
131202a1a643SAndre Oppermann  * The SACK options should be last.  SACK blocks consume 8*n+2 bytes.
131302a1a643SAndre Oppermann  * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks).
131402a1a643SAndre Oppermann  * At minimum we need 10 bytes (to generate 1 SACK block).  If both
131502a1a643SAndre Oppermann  * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present,
131602a1a643SAndre Oppermann  * we only have 10 bytes for SACK options (40 - (12 + 18)).
131702a1a643SAndre Oppermann  */
131802a1a643SAndre Oppermann int
131902a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp)
132002a1a643SAndre Oppermann {
132102a1a643SAndre Oppermann 	u_int mask, optlen = 0;
132202a1a643SAndre Oppermann 
132302a1a643SAndre Oppermann 	for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) {
132402a1a643SAndre Oppermann 		if ((to->to_flags & mask) != mask)
132502a1a643SAndre Oppermann 			continue;
13263a4018c4SAndre Oppermann 		if (optlen == TCP_MAXOLEN)
13273a4018c4SAndre Oppermann 			break;
132802a1a643SAndre Oppermann 		switch (to->to_flags & mask) {
132902a1a643SAndre Oppermann 		case TOF_MSS:
133002a1a643SAndre Oppermann 			while (optlen % 4) {
133102a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
133202a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
133302a1a643SAndre Oppermann 			}
13343a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG)
13353a4018c4SAndre Oppermann 				continue;
133602a1a643SAndre Oppermann 			optlen += TCPOLEN_MAXSEG;
133702a1a643SAndre Oppermann 			*optp++ = TCPOPT_MAXSEG;
133802a1a643SAndre Oppermann 			*optp++ = TCPOLEN_MAXSEG;
133902a1a643SAndre Oppermann 			to->to_mss = htons(to->to_mss);
134002a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss));
134102a1a643SAndre Oppermann 			optp += sizeof(to->to_mss);
134202a1a643SAndre Oppermann 			break;
134302a1a643SAndre Oppermann 		case TOF_SCALE:
134402a1a643SAndre Oppermann 			while (!optlen || optlen % 2 != 1) {
134502a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
134602a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
134702a1a643SAndre Oppermann 			}
13483a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW)
13493a4018c4SAndre Oppermann 				continue;
135002a1a643SAndre Oppermann 			optlen += TCPOLEN_WINDOW;
135102a1a643SAndre Oppermann 			*optp++ = TCPOPT_WINDOW;
135202a1a643SAndre Oppermann 			*optp++ = TCPOLEN_WINDOW;
135302a1a643SAndre Oppermann 			*optp++ = to->to_wscale;
135402a1a643SAndre Oppermann 			break;
135502a1a643SAndre Oppermann 		case TOF_SACKPERM:
135602a1a643SAndre Oppermann 			while (optlen % 2) {
135702a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
135802a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
135902a1a643SAndre Oppermann 			}
13603a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED)
13613a4018c4SAndre Oppermann 				continue;
136202a1a643SAndre Oppermann 			optlen += TCPOLEN_SACK_PERMITTED;
136302a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK_PERMITTED;
136402a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACK_PERMITTED;
136502a1a643SAndre Oppermann 			break;
136602a1a643SAndre Oppermann 		case TOF_TS:
136702a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
136802a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
136902a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
137002a1a643SAndre Oppermann 			}
13713a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP)
13723a4018c4SAndre Oppermann 				continue;
137302a1a643SAndre Oppermann 			optlen += TCPOLEN_TIMESTAMP;
137402a1a643SAndre Oppermann 			*optp++ = TCPOPT_TIMESTAMP;
137502a1a643SAndre Oppermann 			*optp++ = TCPOLEN_TIMESTAMP;
137602a1a643SAndre Oppermann 			to->to_tsval = htonl(to->to_tsval);
137702a1a643SAndre Oppermann 			to->to_tsecr = htonl(to->to_tsecr);
137802a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval));
137902a1a643SAndre Oppermann 			optp += sizeof(to->to_tsval);
138002a1a643SAndre Oppermann 			bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr));
138102a1a643SAndre Oppermann 			optp += sizeof(to->to_tsecr);
138202a1a643SAndre Oppermann 			break;
138302a1a643SAndre Oppermann 		case TOF_SIGNATURE:
138402a1a643SAndre Oppermann 			{
138502a1a643SAndre Oppermann 			int siglen = TCPOLEN_SIGNATURE - 2;
138602a1a643SAndre Oppermann 
138702a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
138802a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
138902a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
139002a1a643SAndre Oppermann 			}
13910d957bbaSAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE)
139202a1a643SAndre Oppermann 				continue;
139302a1a643SAndre Oppermann 			optlen += TCPOLEN_SIGNATURE;
139402a1a643SAndre Oppermann 			*optp++ = TCPOPT_SIGNATURE;
139502a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SIGNATURE;
139602a1a643SAndre Oppermann 			to->to_signature = optp;
139702a1a643SAndre Oppermann 			while (siglen--)
139802a1a643SAndre Oppermann 				 *optp++ = 0;
139902a1a643SAndre Oppermann 			break;
140002a1a643SAndre Oppermann 			}
140102a1a643SAndre Oppermann 		case TOF_SACK:
140202a1a643SAndre Oppermann 			{
140302a1a643SAndre Oppermann 			int sackblks = 0;
140402a1a643SAndre Oppermann 			struct sackblk *sack = (struct sackblk *)to->to_sacks;
140502a1a643SAndre Oppermann 			tcp_seq sack_seq;
140602a1a643SAndre Oppermann 
140702a1a643SAndre Oppermann 			while (!optlen || optlen % 4 != 2) {
140802a1a643SAndre Oppermann 				optlen += TCPOLEN_NOP;
140902a1a643SAndre Oppermann 				*optp++ = TCPOPT_NOP;
141002a1a643SAndre Oppermann 			}
14113a4018c4SAndre Oppermann 			if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK)
141202a1a643SAndre Oppermann 				continue;
141302a1a643SAndre Oppermann 			optlen += TCPOLEN_SACKHDR;
141402a1a643SAndre Oppermann 			*optp++ = TCPOPT_SACK;
141502a1a643SAndre Oppermann 			sackblks = min(to->to_nsacks,
14160d957bbaSAndre Oppermann 					(TCP_MAXOLEN - optlen) / TCPOLEN_SACK);
141702a1a643SAndre Oppermann 			*optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK;
141802a1a643SAndre Oppermann 			while (sackblks--) {
141902a1a643SAndre Oppermann 				sack_seq = htonl(sack->start);
142002a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
142102a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
142202a1a643SAndre Oppermann 				sack_seq = htonl(sack->end);
142302a1a643SAndre Oppermann 				bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq));
142402a1a643SAndre Oppermann 				optp += sizeof(sack_seq);
142502a1a643SAndre Oppermann 				optlen += TCPOLEN_SACK;
142602a1a643SAndre Oppermann 				sack++;
142702a1a643SAndre Oppermann 			}
142802a1a643SAndre Oppermann 			tcpstat.tcps_sack_send_blocks++;
142902a1a643SAndre Oppermann 			break;
143002a1a643SAndre Oppermann 			}
143102a1a643SAndre Oppermann 		default:
143202a1a643SAndre Oppermann 			panic("%s: unknown TCP option type", __func__);
143302a1a643SAndre Oppermann 			break;
143402a1a643SAndre Oppermann 		}
143502a1a643SAndre Oppermann 	}
143602a1a643SAndre Oppermann 
143702a1a643SAndre Oppermann 	/* Terminate and pad TCP options to a 4 byte boundary. */
143802a1a643SAndre Oppermann 	if (optlen % 4) {
143902a1a643SAndre Oppermann 		optlen += TCPOLEN_EOL;
144002a1a643SAndre Oppermann 		*optp++ = TCPOPT_EOL;
144102a1a643SAndre Oppermann 	}
1442413deb12SBjoern A. Zeeb 	/*
1443413deb12SBjoern A. Zeeb 	 * According to RFC 793 (STD0007):
1444413deb12SBjoern A. Zeeb 	 *   "The content of the header beyond the End-of-Option option
1445413deb12SBjoern A. Zeeb 	 *    must be header padding (i.e., zero)."
1446413deb12SBjoern A. Zeeb 	 *   and later: "The padding is composed of zeros."
1447413deb12SBjoern A. Zeeb 	 */
144802a1a643SAndre Oppermann 	while (optlen % 4) {
1449c343c524SAndre Oppermann 		optlen += TCPOLEN_PAD;
1450c343c524SAndre Oppermann 		*optp++ = TCPOPT_PAD;
145102a1a643SAndre Oppermann 	}
145202a1a643SAndre Oppermann 
14530d957bbaSAndre Oppermann 	KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__));
145402a1a643SAndre Oppermann 	return (optlen);
145502a1a643SAndre Oppermann }
1456