xref: /freebsd/sys/netinet/tcp_output.c (revision 5d3b1b755608211cd404e22b435c0a7498db7f4f)
1df8bae1dSRodney W. Grimes /*
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
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
331cfd4b53SBruce M Simpson #include "opt_inet.h"
34fb59c426SYoshinobu Inoue #include "opt_inet6.h"
353a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
36c488362eSRobert Watson #include "opt_mac.h"
370cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
386d90faf3SPaul Saab #include "opt_tcp_sack.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>
43fb919e4dSMark Murray #include <sys/kernel.h>
44fb919e4dSMark Murray #include <sys/lock.h>
45c488362eSRobert Watson #include <sys/mac.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>
60fb59c426SYoshinobu Inoue #ifdef INET6
61686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
62686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
63fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
64fb59c426SYoshinobu Inoue #endif
65df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
66df8bae1dSRodney W. Grimes #define	TCPOUTFLAGS
67df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
68df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
69df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
70df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
71df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
72610ee2f9SDavid Greenman #ifdef TCPDEBUG
73df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
74610ee2f9SDavid Greenman #endif
75df8bae1dSRodney W. Grimes 
763a2a9f79SYoshinobu Inoue #ifdef IPSEC
773a2a9f79SYoshinobu Inoue #include <netinet6/ipsec.h>
783a2a9f79SYoshinobu Inoue #endif /*IPSEC*/
793a2a9f79SYoshinobu Inoue 
80b9234fafSSam Leffler #ifdef FAST_IPSEC
81b9234fafSSam Leffler #include <netipsec/ipsec.h>
82b9234fafSSam Leffler #define	IPSEC
83b9234fafSSam Leffler #endif /*FAST_IPSEC*/
84b9234fafSSam Leffler 
85db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
86db4f9cc7SJonathan Lemon 
87df8bae1dSRodney W. Grimes #ifdef notyet
88df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack();
89df8bae1dSRodney W. Grimes #endif
90df8bae1dSRodney W. Grimes 
91eb5afebaSMike Silbersack int path_mtu_discovery = 1;
929a039a5fSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW,
939a039a5fSDavid Greenman 	&path_mtu_discovery, 1, "Enable Path MTU Discovery");
949a039a5fSDavid Greenman 
959b8b58e0SJonathan Lemon int ss_fltsz = 1;
969b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW,
979b8b58e0SJonathan Lemon 	&ss_fltsz, 1, "Slow start flight size");
989b8b58e0SJonathan Lemon 
993260eb18SMike Silbersack int ss_fltsz_local = 4;
1009b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW,
1019b8b58e0SJonathan Lemon 	&ss_fltsz_local, 1, "Slow start flight size for local networks");
102df8bae1dSRodney W. Grimes 
1037d200109SJayanth Vijayaraghavan int     tcp_do_newreno = 1;
10446f58482SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno,
10546f58482SJonathan Lemon         0, "Enable NewReno Algorithms");
106314e5a3dSJeffrey Hsu 
107df8bae1dSRodney W. Grimes /*
108df8bae1dSRodney W. Grimes  * Tcp output routine: figure out what should be sent and send it.
109df8bae1dSRodney W. Grimes  */
110df8bae1dSRodney W. Grimes int
111f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp)
112df8bae1dSRodney W. Grimes {
113f10e85d7SLuigi Rizzo 	struct socket *so = tp->t_inpcb->inp_socket;
114201d185bSAndre Oppermann 	long len, recwin, sendwin;
115df8bae1dSRodney W. Grimes 	int off, flags, error;
11645d370eeSBruce M Simpson #ifdef TCP_SIGNATURE
1171cfd4b53SBruce M Simpson 	int sigoff = 0;
118265ed012SBruce M Simpson #endif
119f10e85d7SLuigi Rizzo 	struct mbuf *m;
120fb59c426SYoshinobu Inoue 	struct ip *ip = NULL;
121f10e85d7SLuigi Rizzo 	struct ipovly *ipov = NULL;
122f10e85d7SLuigi Rizzo 	struct tcphdr *th;
123a0292f23SGarrett Wollman 	u_char opt[TCP_MAXOLEN];
124a04884fcSBill Fenner 	unsigned ipoptlen, optlen, hdrlen;
125df8bae1dSRodney W. Grimes 	int idle, sendalot;
1266d90faf3SPaul Saab 	int i, sack_rxmit;
1276d90faf3SPaul Saab 	struct sackhole *p;
128262c1c1aSMatthew Dillon #if 0
12946f58482SJonathan Lemon 	int maxburst = TCP_MAXBURST;
130262c1c1aSMatthew Dillon #endif
13197d8d152SAndre Oppermann 	struct rmxp_tao tao;
132fb59c426SYoshinobu Inoue #ifdef INET6
133f10e85d7SLuigi Rizzo 	struct ip6_hdr *ip6 = NULL;
134fb59c426SYoshinobu Inoue 	int isipv6;
135fb59c426SYoshinobu Inoue 
13697d8d152SAndre Oppermann 	bzero(&tao, sizeof(tao));
137fb59c426SYoshinobu Inoue 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
138fb59c426SYoshinobu Inoue #endif
139df8bae1dSRodney W. Grimes 
140fa286d7dSSam Leffler 	INP_LOCK_ASSERT(tp->t_inpcb);
1413d6ade3aSJennifer Yang 
142df8bae1dSRodney W. Grimes 	/*
143df8bae1dSRodney W. Grimes 	 * Determine length of data that should be transmitted,
144df8bae1dSRodney W. Grimes 	 * and flags that will be used.
145df8bae1dSRodney W. Grimes 	 * If there is some data or critical controls (SYN, RST)
146df8bae1dSRodney W. Grimes 	 * to send, then transmit; otherwise, investigate further.
147df8bae1dSRodney W. Grimes 	 */
148c24d5daeSJayanth Vijayaraghavan 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
1499b8b58e0SJonathan Lemon 	if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
150df8bae1dSRodney W. Grimes 		/*
151df8bae1dSRodney W. Grimes 		 * We have been idle for "a while" and no acks are
152df8bae1dSRodney W. Grimes 		 * expected to clock out any data we send --
153df8bae1dSRodney W. Grimes 		 * slow start to get ack "clock" running again.
1549b8b58e0SJonathan Lemon 		 *
1559b8b58e0SJonathan Lemon 		 * Set the slow-start flight size depending on whether
1569b8b58e0SJonathan Lemon 		 * this is a local network or not.
157df8bae1dSRodney W. Grimes 		 */
158f10e85d7SLuigi Rizzo 		int ss = ss_fltsz;
159fb59c426SYoshinobu Inoue #ifdef INET6
160f10e85d7SLuigi Rizzo 		if (isipv6) {
161f10e85d7SLuigi Rizzo 			if (in6_localaddr(&tp->t_inpcb->in6p_faddr))
162f10e85d7SLuigi Rizzo 				ss = ss_fltsz_local;
163f10e85d7SLuigi Rizzo 		} else
164f10e85d7SLuigi Rizzo #endif /* INET6 */
165f10e85d7SLuigi Rizzo 		if (in_localaddr(tp->t_inpcb->inp_faddr))
166f10e85d7SLuigi Rizzo 			ss = ss_fltsz_local;
167f10e85d7SLuigi Rizzo 		tp->snd_cwnd = tp->t_maxseg * ss;
1689b8b58e0SJonathan Lemon 	}
169c24d5daeSJayanth Vijayaraghavan 	tp->t_flags &= ~TF_LASTIDLE;
170c24d5daeSJayanth Vijayaraghavan 	if (idle) {
171c24d5daeSJayanth Vijayaraghavan 		if (tp->t_flags & TF_MORETOCOME) {
172c24d5daeSJayanth Vijayaraghavan 			tp->t_flags |= TF_LASTIDLE;
173c24d5daeSJayanth Vijayaraghavan 			idle = 0;
174c24d5daeSJayanth Vijayaraghavan 		}
175c24d5daeSJayanth Vijayaraghavan 	}
176df8bae1dSRodney W. Grimes again:
1776d90faf3SPaul Saab 	/*
1786d90faf3SPaul Saab 	 * If we've recently taken a timeout, snd_max will be greater than
1796d90faf3SPaul Saab 	 * snd_nxt.  There may be SACK information that allows us to avoid
1806d90faf3SPaul Saab 	 * resending already delivered data.  Adjust snd_nxt accordingly.
1816d90faf3SPaul Saab 	 */
1826d90faf3SPaul Saab 	if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max))
1836d90faf3SPaul Saab 		tcp_sack_adjust(tp);
184df8bae1dSRodney W. Grimes 	sendalot = 0;
185df8bae1dSRodney W. Grimes 	off = tp->snd_nxt - tp->snd_una;
186201d185bSAndre Oppermann 	sendwin = min(tp->snd_wnd, tp->snd_cwnd);
187201d185bSAndre Oppermann 	sendwin = min(sendwin, tp->snd_bwnd);
188df8bae1dSRodney W. Grimes 
189df8bae1dSRodney W. Grimes 	flags = tcp_outflags[tp->t_state];
190a0292f23SGarrett Wollman 	/*
1916d90faf3SPaul Saab 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
1926d90faf3SPaul Saab 	 * to send out new data (when sendalot is 1), bypass this function.
1936d90faf3SPaul Saab 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
1946d90faf3SPaul Saab 	 * we're replacing a (future) new transmission with a retransmission
1956d90faf3SPaul Saab 	 * now, and we previously incremented snd_cwnd in tcp_input().
1966d90faf3SPaul Saab 	 */
1976d90faf3SPaul Saab 	/*
1986d90faf3SPaul Saab 	 * Still in sack recovery , reset rxmit flag to zero.
1996d90faf3SPaul Saab 	 */
2006d90faf3SPaul Saab 	sack_rxmit = 0;
2016d90faf3SPaul Saab 	len = 0;
2026d90faf3SPaul Saab 	p = NULL;
20304f0d9a0SJayanth Vijayaraghavan 	if (tp->sack_enable && IN_FASTRECOVERY(tp) &&
2046d90faf3SPaul Saab 	    (p = tcp_sack_output(tp))) {
205f787edd8SJayanth Vijayaraghavan 		KASSERT(tp->snd_cwnd >= 0,
206f787edd8SJayanth Vijayaraghavan 			("%s: CWIN is negative : %ld", __func__, tp->snd_cwnd));
207f787edd8SJayanth Vijayaraghavan 		/* Do not retransmit SACK segments beyond snd_recover */
208f787edd8SJayanth Vijayaraghavan 		if (SEQ_GT(p->end, tp->snd_recover)) {
209f787edd8SJayanth Vijayaraghavan 			/*
210f787edd8SJayanth Vijayaraghavan 			 * (At least) part of sack hole extends beyond
211f787edd8SJayanth Vijayaraghavan 			 * snd_recover. Check to see if we can rexmit data
212f787edd8SJayanth Vijayaraghavan 			 * for this hole.
213f787edd8SJayanth Vijayaraghavan 			 */
214f787edd8SJayanth Vijayaraghavan 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
215f787edd8SJayanth Vijayaraghavan 				/*
216f787edd8SJayanth Vijayaraghavan 				 * Can't rexmit any more data for this hole.
217f787edd8SJayanth Vijayaraghavan 				 * That data will be rexmitted in the next
218f787edd8SJayanth Vijayaraghavan 				 * sack recovery episode, when snd_recover
219f787edd8SJayanth Vijayaraghavan 				 * moves past p->rxmit.
220f787edd8SJayanth Vijayaraghavan 				 */
221f787edd8SJayanth Vijayaraghavan 				p = NULL;
222f787edd8SJayanth Vijayaraghavan 				goto after_sack_rexmit;
223f787edd8SJayanth Vijayaraghavan 			} else
224f787edd8SJayanth Vijayaraghavan 				/* Can rexmit part of the current hole */
225f787edd8SJayanth Vijayaraghavan 				len = ((long)ulmin(tp->snd_cwnd,
226f787edd8SJayanth Vijayaraghavan 					  tp->snd_recover - p->rxmit));
227f787edd8SJayanth Vijayaraghavan 		} else
228f787edd8SJayanth Vijayaraghavan 			len = ((long)ulmin(tp->snd_cwnd, p->end - p->rxmit));
2296d90faf3SPaul Saab 		sack_rxmit = 1;
2306d90faf3SPaul Saab 		sendalot = 1;
2316d90faf3SPaul Saab 		off = p->rxmit - tp->snd_una;
232f787edd8SJayanth Vijayaraghavan 		KASSERT(off >= 0,("%s: sack block to the left of una : %d",
233f787edd8SJayanth Vijayaraghavan 		    __func__, off));
2346d90faf3SPaul Saab 		if (len > 0) {
2356d90faf3SPaul Saab 			tcpstat.tcps_sack_rexmits++;
2366d90faf3SPaul Saab 			tcpstat.tcps_sack_rexmit_bytes +=
2376d90faf3SPaul Saab 			    min(len, tp->t_maxseg);
2386d90faf3SPaul Saab 		}
2396d90faf3SPaul Saab 	}
240f787edd8SJayanth Vijayaraghavan after_sack_rexmit:
2416d90faf3SPaul Saab 	/*
242a0292f23SGarrett Wollman 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
243a0292f23SGarrett Wollman 	 * state flags.
244a0292f23SGarrett Wollman 	 */
245a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDFIN)
246a0292f23SGarrett Wollman 		flags |= TH_FIN;
247a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDSYN)
248a0292f23SGarrett Wollman 		flags |= TH_SYN;
249a0292f23SGarrett Wollman 
250df8bae1dSRodney W. Grimes 	/*
251df8bae1dSRodney W. Grimes 	 * If in persist timeout with window of 0, send 1 byte.
252df8bae1dSRodney W. Grimes 	 * Otherwise, if window is small but nonzero
253df8bae1dSRodney W. Grimes 	 * and timer expired, we will send what we can
254df8bae1dSRodney W. Grimes 	 * and go to transmit state.
255df8bae1dSRodney W. Grimes 	 */
256df8bae1dSRodney W. Grimes 	if (tp->t_force) {
257201d185bSAndre Oppermann 		if (sendwin == 0) {
258df8bae1dSRodney W. Grimes 			/*
259df8bae1dSRodney W. Grimes 			 * If we still have some data to send, then
260df8bae1dSRodney W. Grimes 			 * clear the FIN bit.  Usually this would
261df8bae1dSRodney W. Grimes 			 * happen below when it realizes that we
262df8bae1dSRodney W. Grimes 			 * aren't sending all the data.  However,
26329089b51SJulian Elischer 			 * if we have exactly 1 byte of unsent data,
264df8bae1dSRodney W. Grimes 			 * then it won't clear the FIN bit below,
265df8bae1dSRodney W. Grimes 			 * and if we are in persist state, we wind
266df8bae1dSRodney W. Grimes 			 * up sending the packet without recording
267df8bae1dSRodney W. Grimes 			 * that we sent the FIN bit.
268df8bae1dSRodney W. Grimes 			 *
269df8bae1dSRodney W. Grimes 			 * We can't just blindly clear the FIN bit,
270df8bae1dSRodney W. Grimes 			 * because if we don't have any more data
271df8bae1dSRodney W. Grimes 			 * to send then the probe will be the FIN
272df8bae1dSRodney W. Grimes 			 * itself.
273df8bae1dSRodney W. Grimes 			 */
274df8bae1dSRodney W. Grimes 			if (off < so->so_snd.sb_cc)
275df8bae1dSRodney W. Grimes 				flags &= ~TH_FIN;
276201d185bSAndre Oppermann 			sendwin = 1;
277df8bae1dSRodney W. Grimes 		} else {
2789b8b58e0SJonathan Lemon 			callout_stop(tp->tt_persist);
279df8bae1dSRodney W. Grimes 			tp->t_rxtshift = 0;
280df8bae1dSRodney W. Grimes 		}
281df8bae1dSRodney W. Grimes 	}
282df8bae1dSRodney W. Grimes 
28328257b5cSMatthew Dillon 	/*
28428257b5cSMatthew Dillon 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
28528257b5cSMatthew Dillon 	 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
286201d185bSAndre Oppermann 	 * a negative length.  This can also occur when TCP opens up
28728257b5cSMatthew Dillon 	 * its congestion window while receiving additional duplicate
28828257b5cSMatthew Dillon 	 * acks after fast-retransmit because TCP will reset snd_nxt
28928257b5cSMatthew Dillon 	 * to snd_max after the fast-retransmit.
29028257b5cSMatthew Dillon 	 *
29128257b5cSMatthew Dillon 	 * In the normal retransmit-FIN-only case, however, snd_nxt will
29228257b5cSMatthew Dillon 	 * be set to snd_una, the offset will be 0, and the length may
29328257b5cSMatthew Dillon 	 * wind up 0.
2946d90faf3SPaul Saab 	 *
2956d90faf3SPaul Saab 	 * If sack_rxmit is true we are retransmitting from the scoreboard
2966d90faf3SPaul Saab 	 * in which case len is already set.
29728257b5cSMatthew Dillon 	 */
2986d90faf3SPaul Saab 	if (!sack_rxmit)
2996d90faf3SPaul Saab 		len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
300a0292f23SGarrett Wollman 
301a0292f23SGarrett Wollman 	/*
302a0292f23SGarrett Wollman 	 * Lop off SYN bit if it has already been sent.  However, if this
303a0292f23SGarrett Wollman 	 * is SYN-SENT state and if segment contains data and if we don't
304a0292f23SGarrett Wollman 	 * know that foreign host supports TAO, suppress sending segment.
305a0292f23SGarrett Wollman 	 */
306a0292f23SGarrett Wollman 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
307a0292f23SGarrett Wollman 		flags &= ~TH_SYN;
308a0292f23SGarrett Wollman 		off--, len++;
30997d8d152SAndre Oppermann 		if (tcp_do_rfc1644)
31097d8d152SAndre Oppermann 			tcp_hc_gettao(&tp->t_inpcb->inp_inc, &tao);
311a0292f23SGarrett Wollman 		if (len > 0 && tp->t_state == TCPS_SYN_SENT &&
31297d8d152SAndre Oppermann 		     tao.tao_ccsent == 0)
313a0292f23SGarrett Wollman 			return 0;
314a0292f23SGarrett Wollman 	}
315a0292f23SGarrett Wollman 
31681165e48SAndras Olah 	/*
31781165e48SAndras Olah 	 * Be careful not to send data and/or FIN on SYN segments
31881165e48SAndras Olah 	 * in cases when no CC option will be sent.
31981165e48SAndras Olah 	 * This measure is needed to prevent interoperability problems
32081165e48SAndras Olah 	 * with not fully conformant TCP implementations.
32181165e48SAndras Olah 	 */
32281165e48SAndras Olah 	if ((flags & TH_SYN) &&
32381165e48SAndras Olah 	    ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) ||
32481165e48SAndras Olah 	     ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) {
32581165e48SAndras Olah 		len = 0;
32681165e48SAndras Olah 		flags &= ~TH_FIN;
32781165e48SAndras Olah 	}
32881165e48SAndras Olah 
329df8bae1dSRodney W. Grimes 	if (len < 0) {
330df8bae1dSRodney W. Grimes 		/*
331df8bae1dSRodney W. Grimes 		 * If FIN has been sent but not acked,
332df8bae1dSRodney W. Grimes 		 * but we haven't been called to retransmit,
33328257b5cSMatthew Dillon 		 * len will be < 0.  Otherwise, window shrank
334df8bae1dSRodney W. Grimes 		 * after we sent into it.  If window shrank to 0,
3352d8266afSDavid Greenman 		 * cancel pending retransmit, pull snd_nxt back
3362d8266afSDavid Greenman 		 * to (closed) window, and set the persist timer
3372d8266afSDavid Greenman 		 * if it isn't already going.  If the window didn't
3382d8266afSDavid Greenman 		 * close completely, just wait for an ACK.
339df8bae1dSRodney W. Grimes 		 */
340df8bae1dSRodney W. Grimes 		len = 0;
341201d185bSAndre Oppermann 		if (sendwin == 0) {
3429b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
3432d8266afSDavid Greenman 			tp->t_rxtshift = 0;
344df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
3459b8b58e0SJonathan Lemon 			if (!callout_active(tp->tt_persist))
3462d8266afSDavid Greenman 				tcp_setpersist(tp);
347df8bae1dSRodney W. Grimes 		}
348df8bae1dSRodney W. Grimes 	}
34928257b5cSMatthew Dillon 
35028257b5cSMatthew Dillon 	/*
35128257b5cSMatthew Dillon 	 * len will be >= 0 after this point.  Truncate to the maximum
35228257b5cSMatthew Dillon 	 * segment length and ensure that FIN is removed if the length
35328257b5cSMatthew Dillon 	 * no longer contains the last data byte.
35428257b5cSMatthew Dillon 	 */
355df8bae1dSRodney W. Grimes 	if (len > tp->t_maxseg) {
356df8bae1dSRodney W. Grimes 		len = tp->t_maxseg;
357df8bae1dSRodney W. Grimes 		sendalot = 1;
358df8bae1dSRodney W. Grimes 	}
3595d3b1b75SJayanth Vijayaraghavan 	if (sack_rxmit) {
3605d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
361df8bae1dSRodney W. Grimes 			flags &= ~TH_FIN;
3625d3b1b75SJayanth Vijayaraghavan 	} else {
3635d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
3645d3b1b75SJayanth Vijayaraghavan 			flags &= ~TH_FIN;
3655d3b1b75SJayanth Vijayaraghavan 	}
366df8bae1dSRodney W. Grimes 
367201d185bSAndre Oppermann 	recwin = sbspace(&so->so_rcv);
368df8bae1dSRodney W. Grimes 
369df8bae1dSRodney W. Grimes 	/*
370262c1c1aSMatthew Dillon 	 * Sender silly window avoidance.   We transmit under the following
371262c1c1aSMatthew Dillon 	 * conditions when len is non-zero:
372262c1c1aSMatthew Dillon 	 *
373262c1c1aSMatthew Dillon 	 *	- We have a full segment
374262c1c1aSMatthew Dillon 	 *	- This is the last buffer in a write()/send() and we are
375262c1c1aSMatthew Dillon 	 *	  either idle or running NODELAY
376262c1c1aSMatthew Dillon 	 *	- we've timed out (e.g. persist timer)
377262c1c1aSMatthew Dillon 	 *	- we have more then 1/2 the maximum send window's worth of
378262c1c1aSMatthew Dillon 	 *	  data (receiver may be limited the window size)
379262c1c1aSMatthew Dillon 	 *	- we need to retransmit
380df8bae1dSRodney W. Grimes 	 */
381df8bae1dSRodney W. Grimes 	if (len) {
382df8bae1dSRodney W. Grimes 		if (len == tp->t_maxseg)
383df8bae1dSRodney W. Grimes 			goto send;
384262c1c1aSMatthew Dillon 		/*
385262c1c1aSMatthew Dillon 		 * NOTE! on localhost connections an 'ack' from the remote
386262c1c1aSMatthew Dillon 		 * end may occur synchronously with the output and cause
387262c1c1aSMatthew Dillon 		 * us to flush a buffer queued with moretocome.  XXX
388262c1c1aSMatthew Dillon 		 *
389262c1c1aSMatthew Dillon 		 * note: the len + off check is almost certainly unnecessary.
390262c1c1aSMatthew Dillon 		 */
391262c1c1aSMatthew Dillon 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
392262c1c1aSMatthew Dillon 		    (idle || (tp->t_flags & TF_NODELAY)) &&
393262c1c1aSMatthew Dillon 		    len + off >= so->so_snd.sb_cc &&
394262c1c1aSMatthew Dillon 		    (tp->t_flags & TF_NOPUSH) == 0) {
395df8bae1dSRodney W. Grimes 			goto send;
396262c1c1aSMatthew Dillon 		}
397262c1c1aSMatthew Dillon 		if (tp->t_force)			/* typ. timeout case */
398df8bae1dSRodney W. Grimes 			goto send;
399a0292f23SGarrett Wollman 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
400df8bae1dSRodney W. Grimes 			goto send;
401262c1c1aSMatthew Dillon 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))	/* retransmit case */
402df8bae1dSRodney W. Grimes 			goto send;
4036d90faf3SPaul Saab 		if (sack_rxmit)
4046d90faf3SPaul Saab 			goto send;
405df8bae1dSRodney W. Grimes 	}
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes 	/*
408df8bae1dSRodney W. Grimes 	 * Compare available window to amount of window
409df8bae1dSRodney W. Grimes 	 * known to peer (as advertised window less
410df8bae1dSRodney W. Grimes 	 * next expected input).  If the difference is at least two
411df8bae1dSRodney W. Grimes 	 * max size segments, or at least 50% of the maximum possible
412df8bae1dSRodney W. Grimes 	 * window, then want to send a window update to peer.
4133bfd6421SJonathan Lemon 	 * Skip this if the connection is in T/TCP half-open state.
414df8bae1dSRodney W. Grimes 	 */
415201d185bSAndre Oppermann 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) {
416df8bae1dSRodney W. Grimes 		/*
417df8bae1dSRodney W. Grimes 		 * "adv" is the amount we can increase the window,
418df8bae1dSRodney W. Grimes 		 * taking into account that we are limited by
419df8bae1dSRodney W. Grimes 		 * TCP_MAXWIN << tp->rcv_scale.
420df8bae1dSRodney W. Grimes 		 */
421201d185bSAndre Oppermann 		long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
422df8bae1dSRodney W. Grimes 			(tp->rcv_adv - tp->rcv_nxt);
423df8bae1dSRodney W. Grimes 
424df8bae1dSRodney W. Grimes 		if (adv >= (long) (2 * tp->t_maxseg))
425df8bae1dSRodney W. Grimes 			goto send;
426df8bae1dSRodney W. Grimes 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
427df8bae1dSRodney W. Grimes 			goto send;
428df8bae1dSRodney W. Grimes 	}
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes 	/*
43128257b5cSMatthew Dillon 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
43228257b5cSMatthew Dillon 	 * is also a catch-all for the retransmit timer timeout case.
433df8bae1dSRodney W. Grimes 	 */
434df8bae1dSRodney W. Grimes 	if (tp->t_flags & TF_ACKNOW)
435df8bae1dSRodney W. Grimes 		goto send;
436a0292f23SGarrett Wollman 	if ((flags & TH_RST) ||
437a0292f23SGarrett Wollman 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
438df8bae1dSRodney W. Grimes 		goto send;
439df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_una))
440df8bae1dSRodney W. Grimes 		goto send;
441df8bae1dSRodney W. Grimes 	/*
442df8bae1dSRodney W. Grimes 	 * If our state indicates that FIN should be sent
44328257b5cSMatthew Dillon 	 * and we have not yet done so, then we need to send.
444df8bae1dSRodney W. Grimes 	 */
445df8bae1dSRodney W. Grimes 	if (flags & TH_FIN &&
446df8bae1dSRodney W. Grimes 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
447df8bae1dSRodney W. Grimes 		goto send;
4486d90faf3SPaul Saab 	/*
4496d90faf3SPaul Saab 	 * In SACK, it is possible for tcp_output to fail to send a segment
4506d90faf3SPaul Saab 	 * after the retransmission timer has been turned off.  Make sure
4516d90faf3SPaul Saab 	 * that the retransmission timer is set.
4526d90faf3SPaul Saab 	 */
4536d90faf3SPaul Saab 	if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) &&
4546d90faf3SPaul Saab 	    !callout_active(tp->tt_rexmt) &&
4556d90faf3SPaul Saab 	    !callout_active(tp->tt_persist)) {
4566d90faf3SPaul Saab 		callout_reset(tp->tt_rexmt, tp->t_rxtcur,
4576d90faf3SPaul Saab 			      tcp_timer_rexmt, tp);
4586d90faf3SPaul Saab                 return (0);
4596d90faf3SPaul Saab         }
460df8bae1dSRodney W. Grimes 	/*
461df8bae1dSRodney W. Grimes 	 * TCP window updates are not reliable, rather a polling protocol
462df8bae1dSRodney W. Grimes 	 * using ``persist'' packets is used to insure receipt of window
463df8bae1dSRodney W. Grimes 	 * updates.  The three ``states'' for the output side are:
464df8bae1dSRodney W. Grimes 	 *	idle			not doing retransmits or persists
465df8bae1dSRodney W. Grimes 	 *	persisting		to move a small or zero window
466df8bae1dSRodney W. Grimes 	 *	(re)transmitting	and thereby not persisting
467df8bae1dSRodney W. Grimes 	 *
4689b8b58e0SJonathan Lemon 	 * callout_active(tp->tt_persist)
4699b8b58e0SJonathan Lemon 	 *	is true when we are in persist state.
470df8bae1dSRodney W. Grimes 	 * tp->t_force
471df8bae1dSRodney W. Grimes 	 *	is set when we are called to send a persist packet.
4729b8b58e0SJonathan Lemon 	 * callout_active(tp->tt_rexmt)
473df8bae1dSRodney W. Grimes 	 *	is set when we are retransmitting
474df8bae1dSRodney W. Grimes 	 * The output side is idle when both timers are zero.
475df8bae1dSRodney W. Grimes 	 *
476df8bae1dSRodney W. Grimes 	 * If send window is too small, there is data to transmit, and no
477df8bae1dSRodney W. Grimes 	 * retransmit or persist is pending, then go to persist state.
478df8bae1dSRodney W. Grimes 	 * If nothing happens soon, send when timer expires:
479df8bae1dSRodney W. Grimes 	 * if window is nonzero, transmit what we can,
480df8bae1dSRodney W. Grimes 	 * otherwise force out a byte.
481df8bae1dSRodney W. Grimes 	 */
4829b8b58e0SJonathan Lemon 	if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) &&
4839b8b58e0SJonathan Lemon 	    !callout_active(tp->tt_persist)) {
484df8bae1dSRodney W. Grimes 		tp->t_rxtshift = 0;
485df8bae1dSRodney W. Grimes 		tcp_setpersist(tp);
486df8bae1dSRodney W. Grimes 	}
487df8bae1dSRodney W. Grimes 
488df8bae1dSRodney W. Grimes 	/*
489df8bae1dSRodney W. Grimes 	 * No reason to send a segment, just return.
490df8bae1dSRodney W. Grimes 	 */
491df8bae1dSRodney W. Grimes 	return (0);
492df8bae1dSRodney W. Grimes 
493df8bae1dSRodney W. Grimes send:
494df8bae1dSRodney W. Grimes 	/*
495df8bae1dSRodney W. Grimes 	 * Before ESTABLISHED, force sending of initial options
496df8bae1dSRodney W. Grimes 	 * unless TCP set not to do any options.
497df8bae1dSRodney W. Grimes 	 * NOTE: we assume that the IP/TCP header plus TCP options
498df8bae1dSRodney W. Grimes 	 * always fit in a single mbuf, leaving room for a maximum
499df8bae1dSRodney W. Grimes 	 * link header, i.e.
50033841545SHajimu UMEMOTO 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
501df8bae1dSRodney W. Grimes 	 */
502df8bae1dSRodney W. Grimes 	optlen = 0;
503fb59c426SYoshinobu Inoue #ifdef INET6
504fb59c426SYoshinobu Inoue 	if (isipv6)
505fb59c426SYoshinobu Inoue 		hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
506fb59c426SYoshinobu Inoue 	else
507fb59c426SYoshinobu Inoue #endif
508df8bae1dSRodney W. Grimes 	hdrlen = sizeof (struct tcpiphdr);
509df8bae1dSRodney W. Grimes 	if (flags & TH_SYN) {
510df8bae1dSRodney W. Grimes 		tp->snd_nxt = tp->iss;
511df8bae1dSRodney W. Grimes 		if ((tp->t_flags & TF_NOOPT) == 0) {
512df8bae1dSRodney W. Grimes 			u_short mss;
513df8bae1dSRodney W. Grimes 
514df8bae1dSRodney W. Grimes 			opt[0] = TCPOPT_MAXSEG;
515a0292f23SGarrett Wollman 			opt[1] = TCPOLEN_MAXSEG;
51697d8d152SAndre Oppermann 			mss = htons((u_short) tcp_mssopt(&tp->t_inpcb->inp_inc));
51794a5d9b6SDavid Greenman 			(void)memcpy(opt + 2, &mss, sizeof(mss));
518a0292f23SGarrett Wollman 			optlen = TCPOLEN_MAXSEG;
519df8bae1dSRodney W. Grimes 
5206d90faf3SPaul Saab                         /*
5216d90faf3SPaul Saab                          * If this is the first SYN of connection (not a SYN
5226d90faf3SPaul Saab                          * ACK), include SACK_PERMIT_HDR option.  If this is a
5236d90faf3SPaul Saab                          * SYN ACK, include SACK_PERMIT_HDR option if peer has
5246d90faf3SPaul Saab                          * already done so. This is only for active connect,
5256d90faf3SPaul Saab 			 * since the syncache takes care of the passive connect.
5266d90faf3SPaul Saab                          */
5276d90faf3SPaul Saab                         if (tp->sack_enable && ((flags & TH_ACK) == 0 ||
5286d90faf3SPaul Saab 			    (tp->t_flags & TF_SACK_PERMIT))) {
5296d90faf3SPaul Saab                                 *((u_int32_t *) (opt + optlen)) =
5306d90faf3SPaul Saab 					htonl(TCPOPT_SACK_PERMIT_HDR);
5316d90faf3SPaul Saab                                 optlen += 4;
5326d90faf3SPaul Saab                         }
533df8bae1dSRodney W. Grimes 			if ((tp->t_flags & TF_REQ_SCALE) &&
534df8bae1dSRodney W. Grimes 			    ((flags & TH_ACK) == 0 ||
535df8bae1dSRodney W. Grimes 			    (tp->t_flags & TF_RCVD_SCALE))) {
5369105bb46SBruce Evans 				*((u_int32_t *)(opt + optlen)) = htonl(
537df8bae1dSRodney W. Grimes 					TCPOPT_NOP << 24 |
538df8bae1dSRodney W. Grimes 					TCPOPT_WINDOW << 16 |
539df8bae1dSRodney W. Grimes 					TCPOLEN_WINDOW << 8 |
540df8bae1dSRodney W. Grimes 					tp->request_r_scale);
541df8bae1dSRodney W. Grimes 				optlen += 4;
542df8bae1dSRodney W. Grimes 			}
543df8bae1dSRodney W. Grimes 		}
544df8bae1dSRodney W. Grimes  	}
545df8bae1dSRodney W. Grimes 
546df8bae1dSRodney W. Grimes  	/*
547df8bae1dSRodney W. Grimes 	 * Send a timestamp and echo-reply if this is a SYN and our side
548df8bae1dSRodney W. Grimes 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
549df8bae1dSRodney W. Grimes 	 * and our peer have sent timestamps in our SYN's.
550df8bae1dSRodney W. Grimes  	 */
551df8bae1dSRodney W. Grimes  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
552df8bae1dSRodney W. Grimes  	    (flags & TH_RST) == 0 &&
553a0292f23SGarrett Wollman 	    ((flags & TH_ACK) == 0 ||
554df8bae1dSRodney W. Grimes 	     (tp->t_flags & TF_RCVD_TSTMP))) {
5559105bb46SBruce Evans 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes  		/* Form timestamp option as shown in appendix A of RFC 1323. */
558df8bae1dSRodney W. Grimes  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
5599b8b58e0SJonathan Lemon  		*lp++ = htonl(ticks);
560df8bae1dSRodney W. Grimes  		*lp   = htonl(tp->ts_recent);
561df8bae1dSRodney W. Grimes  		optlen += TCPOLEN_TSTAMP_APPA;
562df8bae1dSRodney W. Grimes  	}
563df8bae1dSRodney W. Grimes 
564a0292f23SGarrett Wollman 	/*
5656d90faf3SPaul Saab 	 * Send SACKs if necessary.  This should be the last option processed.
5666d90faf3SPaul Saab 	 * Only as many SACKs are sent as are permitted by the maximum options
5676d90faf3SPaul Saab 	 * size.  No more than three SACKs are sent.
5686d90faf3SPaul Saab 	 */
5696d90faf3SPaul Saab 	if (tp->sack_enable && tp->t_state == TCPS_ESTABLISHED &&
5706d90faf3SPaul Saab 	    (tp->t_flags & (TF_SACK_PERMIT|TF_NOOPT)) == TF_SACK_PERMIT &&
5716d90faf3SPaul Saab 	    tp->rcv_numsacks) {
5726d90faf3SPaul Saab 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
5736d90faf3SPaul Saab 		u_int32_t *olp = lp++;
5746d90faf3SPaul Saab 		int count = 0;  /* actual number of SACKs inserted */
5756d90faf3SPaul Saab 		int maxsack = (MAX_TCPOPTLEN - (optlen + 4))/TCPOLEN_SACK;
5766d90faf3SPaul Saab 
5776d90faf3SPaul Saab 		tcpstat.tcps_sack_send_blocks++;
5786d90faf3SPaul Saab 		maxsack = min(maxsack, TCP_MAX_SACK);
5796d90faf3SPaul Saab 		for (i = 0; (i < tp->rcv_numsacks && count < maxsack); i++) {
5806d90faf3SPaul Saab 			struct sackblk sack = tp->sackblks[i];
5816d90faf3SPaul Saab 			if (sack.start == 0 && sack.end == 0)
5826d90faf3SPaul Saab 				continue;
5836d90faf3SPaul Saab 			*lp++ = htonl(sack.start);
5846d90faf3SPaul Saab 			*lp++ = htonl(sack.end);
5856d90faf3SPaul Saab 			count++;
5866d90faf3SPaul Saab 		}
5876d90faf3SPaul Saab 		*olp = htonl(TCPOPT_SACK_HDR|(TCPOLEN_SACK*count+2));
5886d90faf3SPaul Saab 		optlen += TCPOLEN_SACK*count + 4; /* including leading NOPs */
5896d90faf3SPaul Saab 	}
5906d90faf3SPaul Saab  	/*
591a0292f23SGarrett Wollman 	 * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
592a0292f23SGarrett Wollman 	 * options are allowed (!TF_NOOPT) and it's not a RST.
593a0292f23SGarrett Wollman  	 */
594a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
595a0292f23SGarrett Wollman  	     (flags & TH_RST) == 0) {
596a0292f23SGarrett Wollman 		switch (flags & (TH_SYN|TH_ACK)) {
597a0292f23SGarrett Wollman 		/*
598a0292f23SGarrett Wollman 		 * This is a normal ACK, send CC if we received CC before
599a0292f23SGarrett Wollman 		 * from our peer.
600a0292f23SGarrett Wollman 		 */
601a0292f23SGarrett Wollman 		case TH_ACK:
602a0292f23SGarrett Wollman 			if (!(tp->t_flags & TF_RCVD_CC))
603a0292f23SGarrett Wollman 				break;
604a0292f23SGarrett Wollman 			/*FALLTHROUGH*/
605a0292f23SGarrett Wollman 
606a0292f23SGarrett Wollman 		/*
607a0292f23SGarrett Wollman 		 * We can only get here in T/TCP's SYN_SENT* state, when
608a0292f23SGarrett Wollman 		 * we're a sending a non-SYN segment without waiting for
609a0292f23SGarrett Wollman 		 * the ACK of our SYN.  A check above assures that we only
610a0292f23SGarrett Wollman 		 * do this if our peer understands T/TCP.
611a0292f23SGarrett Wollman 		 */
612a0292f23SGarrett Wollman 		case 0:
613a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
614a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
615a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_CC;
616a0292f23SGarrett Wollman 			opt[optlen++] = TCPOLEN_CC;
617a0292f23SGarrett Wollman 			*(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
618a0292f23SGarrett Wollman 
619a0292f23SGarrett Wollman 			optlen += 4;
620a0292f23SGarrett Wollman 			break;
621a0292f23SGarrett Wollman 
622a0292f23SGarrett Wollman 		/*
623a0292f23SGarrett Wollman 		 * This is our initial SYN, check whether we have to use
624a0292f23SGarrett Wollman 		 * CC or CC.new.
625a0292f23SGarrett Wollman 		 */
626a0292f23SGarrett Wollman 		case TH_SYN:
627a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
628a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
629a45d2726SAndras Olah 			opt[optlen++] = tp->t_flags & TF_SENDCCNEW ?
630a45d2726SAndras Olah 						TCPOPT_CCNEW : TCPOPT_CC;
631a0292f23SGarrett Wollman 			opt[optlen++] = TCPOLEN_CC;
632a0292f23SGarrett Wollman 			*(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
633a0292f23SGarrett Wollman  			optlen += 4;
634a0292f23SGarrett Wollman 			break;
635a0292f23SGarrett Wollman 
636a0292f23SGarrett Wollman 		/*
637a0292f23SGarrett Wollman 		 * This is a SYN,ACK; send CC and CC.echo if we received
638a0292f23SGarrett Wollman 		 * CC from our peer.
639a0292f23SGarrett Wollman 		 */
640a0292f23SGarrett Wollman 		case (TH_SYN|TH_ACK):
641a0292f23SGarrett Wollman 			if (tp->t_flags & TF_RCVD_CC) {
642a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
643a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
644a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_CC;
645a0292f23SGarrett Wollman 				opt[optlen++] = TCPOLEN_CC;
646a0292f23SGarrett Wollman 				*(u_int32_t *)&opt[optlen] =
647a0292f23SGarrett Wollman 					htonl(tp->cc_send);
648a0292f23SGarrett Wollman 				optlen += 4;
649a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
650a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
651a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_CCECHO;
652a0292f23SGarrett Wollman 				opt[optlen++] = TCPOLEN_CC;
653a0292f23SGarrett Wollman 				*(u_int32_t *)&opt[optlen] =
654a0292f23SGarrett Wollman 					htonl(tp->cc_recv);
655a0292f23SGarrett Wollman 				optlen += 4;
656a0292f23SGarrett Wollman 			}
657a0292f23SGarrett Wollman 			break;
658a0292f23SGarrett Wollman 		}
659a0292f23SGarrett Wollman  	}
660a0292f23SGarrett Wollman 
6611cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
6621cfd4b53SBruce M Simpson #ifdef INET6
6631cfd4b53SBruce M Simpson 	if (!isipv6)
6641cfd4b53SBruce M Simpson #endif
6651cfd4b53SBruce M Simpson 	if (tp->t_flags & TF_SIGNATURE) {
6661cfd4b53SBruce M Simpson 		int i;
6671cfd4b53SBruce M Simpson 		u_char *bp;
668bca0e5bfSBruce M Simpson 
669bca0e5bfSBruce M Simpson 		/* Initialize TCP-MD5 option (RFC2385) */
6701cfd4b53SBruce M Simpson 		bp = (u_char *)opt + optlen;
6711cfd4b53SBruce M Simpson 		*bp++ = TCPOPT_SIGNATURE;
6721cfd4b53SBruce M Simpson 		*bp++ = TCPOLEN_SIGNATURE;
6731cfd4b53SBruce M Simpson 		sigoff = optlen + 2;
6741cfd4b53SBruce M Simpson 		for (i = 0; i < TCP_SIGLEN; i++)
6751cfd4b53SBruce M Simpson 			*bp++ = 0;
6761cfd4b53SBruce M Simpson 		optlen += TCPOLEN_SIGNATURE;
677bca0e5bfSBruce M Simpson 
678bca0e5bfSBruce M Simpson 		/* Terminate options list and maintain 32-bit alignment. */
6791cfd4b53SBruce M Simpson 		*bp++ = TCPOPT_NOP;
6801cfd4b53SBruce M Simpson 		*bp++ = TCPOPT_EOL;
6811cfd4b53SBruce M Simpson 		optlen += 2;
6821cfd4b53SBruce M Simpson 	}
6831cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
6841cfd4b53SBruce M Simpson 
685df8bae1dSRodney W. Grimes  	hdrlen += optlen;
686df8bae1dSRodney W. Grimes 
687fb59c426SYoshinobu Inoue #ifdef INET6
688fb59c426SYoshinobu Inoue 	if (isipv6)
689fb59c426SYoshinobu Inoue 		ipoptlen = ip6_optlen(tp->t_inpcb);
690fb59c426SYoshinobu Inoue 	else
691fb59c426SYoshinobu Inoue #endif
692f10e85d7SLuigi Rizzo 	if (tp->t_inpcb->inp_options)
693a04884fcSBill Fenner 		ipoptlen = tp->t_inpcb->inp_options->m_len -
694a04884fcSBill Fenner 				offsetof(struct ipoption, ipopt_list);
695f10e85d7SLuigi Rizzo 	else
696a04884fcSBill Fenner 		ipoptlen = 0;
697fb59c426SYoshinobu Inoue #ifdef IPSEC
698fb59c426SYoshinobu Inoue 	ipoptlen += ipsec_hdrsiz_tcp(tp);
699fb59c426SYoshinobu Inoue #endif
700a04884fcSBill Fenner 
701df8bae1dSRodney W. Grimes 	/*
702df8bae1dSRodney W. Grimes 	 * Adjust data length if insertion of options will
703a0292f23SGarrett Wollman 	 * bump the packet length beyond the t_maxopd length.
704a0292f23SGarrett Wollman 	 * Clear the FIN bit because we cut off the tail of
705a0292f23SGarrett Wollman 	 * the segment.
706df8bae1dSRodney W. Grimes 	 */
707a04884fcSBill Fenner 	if (len + optlen + ipoptlen > tp->t_maxopd) {
708297a37f3SDavid Greenman 		/*
709297a37f3SDavid Greenman 		 * If there is still more to send, don't close the connection.
710297a37f3SDavid Greenman 		 */
711297a37f3SDavid Greenman 		flags &= ~TH_FIN;
712a04884fcSBill Fenner 		len = tp->t_maxopd - optlen - ipoptlen;
713df8bae1dSRodney W. Grimes 		sendalot = 1;
714df8bae1dSRodney W. Grimes 	}
715df8bae1dSRodney W. Grimes 
716a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/
717a683a7ddSYoshinobu Inoue #ifdef INET6
718a683a7ddSYoshinobu Inoue  	if (max_linkhdr + hdrlen > MCLBYTES)
719a683a7ddSYoshinobu Inoue #else
720df8bae1dSRodney W. Grimes  	if (max_linkhdr + hdrlen > MHLEN)
721a683a7ddSYoshinobu Inoue #endif
722f10e85d7SLuigi Rizzo 		panic("tcphdr too big");
723a0292f23SGarrett Wollman /*#endif*/
724df8bae1dSRodney W. Grimes 
725df8bae1dSRodney W. Grimes 	/*
726df8bae1dSRodney W. Grimes 	 * Grab a header mbuf, attaching a copy of data to
727df8bae1dSRodney W. Grimes 	 * be transmitted, and initialize the header from
728df8bae1dSRodney W. Grimes 	 * the template for sends on this connection.
729df8bae1dSRodney W. Grimes 	 */
730df8bae1dSRodney W. Grimes 	if (len) {
731df8bae1dSRodney W. Grimes 		if (tp->t_force && len == 1)
732df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndprobe++;
733df8bae1dSRodney W. Grimes 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
734df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitpack++;
735df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitbyte += len;
736df8bae1dSRodney W. Grimes 		} else {
737df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndpack++;
738df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndbyte += len;
739df8bae1dSRodney W. Grimes 		}
740df8bae1dSRodney W. Grimes #ifdef notyet
741df8bae1dSRodney W. Grimes 		if ((m = m_copypack(so->so_snd.sb_mb, off,
742df8bae1dSRodney W. Grimes 		    (int)len, max_linkhdr + hdrlen)) == 0) {
743df8bae1dSRodney W. Grimes 			error = ENOBUFS;
744df8bae1dSRodney W. Grimes 			goto out;
745df8bae1dSRodney W. Grimes 		}
746df8bae1dSRodney W. Grimes 		/*
747df8bae1dSRodney W. Grimes 		 * m_copypack left space for our hdr; use it.
748df8bae1dSRodney W. Grimes 		 */
749df8bae1dSRodney W. Grimes 		m->m_len += hdrlen;
750df8bae1dSRodney W. Grimes 		m->m_data -= hdrlen;
751df8bae1dSRodney W. Grimes #else
752a163d034SWarner Losh 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
753df8bae1dSRodney W. Grimes 		if (m == NULL) {
754df8bae1dSRodney W. Grimes 			error = ENOBUFS;
755df8bae1dSRodney W. Grimes 			goto out;
756df8bae1dSRodney W. Grimes 		}
757fb59c426SYoshinobu Inoue #ifdef INET6
758a683a7ddSYoshinobu Inoue 		if (MHLEN < hdrlen + max_linkhdr) {
759a163d034SWarner Losh 			MCLGET(m, M_DONTWAIT);
760a683a7ddSYoshinobu Inoue 			if ((m->m_flags & M_EXT) == 0) {
761a683a7ddSYoshinobu Inoue 				m_freem(m);
762a683a7ddSYoshinobu Inoue 				error = ENOBUFS;
763a683a7ddSYoshinobu Inoue 				goto out;
764a683a7ddSYoshinobu Inoue 			}
765a683a7ddSYoshinobu Inoue 		}
766fb59c426SYoshinobu Inoue #endif
767df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
768df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
769df8bae1dSRodney W. Grimes 		if (len <= MHLEN - hdrlen - max_linkhdr) {
770df8bae1dSRodney W. Grimes 			m_copydata(so->so_snd.sb_mb, off, (int) len,
771df8bae1dSRodney W. Grimes 			    mtod(m, caddr_t) + hdrlen);
772df8bae1dSRodney W. Grimes 			m->m_len += len;
773df8bae1dSRodney W. Grimes 		} else {
774df8bae1dSRodney W. Grimes 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
77551823c3aSGarrett Wollman 			if (m->m_next == 0) {
776d7f570e6SGarrett Wollman 				(void) m_free(m);
77751823c3aSGarrett Wollman 				error = ENOBUFS;
77851823c3aSGarrett Wollman 				goto out;
77951823c3aSGarrett Wollman 			}
780df8bae1dSRodney W. Grimes 		}
781df8bae1dSRodney W. Grimes #endif
782df8bae1dSRodney W. Grimes 		/*
783df8bae1dSRodney W. Grimes 		 * If we're sending everything we've got, set PUSH.
784df8bae1dSRodney W. Grimes 		 * (This will keep happy those implementations which only
785df8bae1dSRodney W. Grimes 		 * give data to the user when a buffer fills or
786df8bae1dSRodney W. Grimes 		 * a PUSH comes in.)
787df8bae1dSRodney W. Grimes 		 */
788df8bae1dSRodney W. Grimes 		if (off + len == so->so_snd.sb_cc)
789df8bae1dSRodney W. Grimes 			flags |= TH_PUSH;
790df8bae1dSRodney W. Grimes 	} else {
791df8bae1dSRodney W. Grimes 		if (tp->t_flags & TF_ACKNOW)
792df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndacks++;
793df8bae1dSRodney W. Grimes 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
794df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndctrl++;
795df8bae1dSRodney W. Grimes 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
796df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndurg++;
797df8bae1dSRodney W. Grimes 		else
798df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndwinup++;
799df8bae1dSRodney W. Grimes 
800a163d034SWarner Losh 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
801df8bae1dSRodney W. Grimes 		if (m == NULL) {
802df8bae1dSRodney W. Grimes 			error = ENOBUFS;
803df8bae1dSRodney W. Grimes 			goto out;
804df8bae1dSRodney W. Grimes 		}
805fb59c426SYoshinobu Inoue #ifdef INET6
806fb59c426SYoshinobu Inoue 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
807fb59c426SYoshinobu Inoue 		    MHLEN >= hdrlen) {
808fb59c426SYoshinobu Inoue 			MH_ALIGN(m, hdrlen);
809fb59c426SYoshinobu Inoue 		} else
810fb59c426SYoshinobu Inoue #endif
811df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
812df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
813df8bae1dSRodney W. Grimes 	}
814df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = (struct ifnet *)0;
815c488362eSRobert Watson #ifdef MAC
816c18b97c6SRobert Watson 	mac_create_mbuf_from_inpcb(tp->t_inpcb, m);
817c488362eSRobert Watson #endif
818fb59c426SYoshinobu Inoue #ifdef INET6
819fb59c426SYoshinobu Inoue 	if (isipv6) {
820fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
821fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip6 + 1);
82279909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip6, th);
823fb59c426SYoshinobu Inoue 	} else
824fb59c426SYoshinobu Inoue #endif /* INET6 */
825fb59c426SYoshinobu Inoue       {
826fb59c426SYoshinobu Inoue 	ip = mtod(m, struct ip *);
827fb59c426SYoshinobu Inoue 	ipov = (struct ipovly *)ip;
828fb59c426SYoshinobu Inoue 	th = (struct tcphdr *)(ip + 1);
82979909384SJonathan Lemon 	tcpip_fillheaders(tp->t_inpcb, ip, th);
830fb59c426SYoshinobu Inoue       }
831df8bae1dSRodney W. Grimes 
832df8bae1dSRodney W. Grimes 	/*
833df8bae1dSRodney W. Grimes 	 * Fill in fields, remembering maximum advertised
834df8bae1dSRodney W. Grimes 	 * window for use in delaying messages about window sizes.
835df8bae1dSRodney W. Grimes 	 * If resending a FIN, be sure not to use a new sequence number.
836df8bae1dSRodney W. Grimes 	 */
837df8bae1dSRodney W. Grimes 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
838df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max)
839df8bae1dSRodney W. Grimes 		tp->snd_nxt--;
840df8bae1dSRodney W. Grimes 	/*
841df8bae1dSRodney W. Grimes 	 * If we are doing retransmissions, then snd_nxt will
842df8bae1dSRodney W. Grimes 	 * not reflect the first unsent octet.  For ACK only
843df8bae1dSRodney W. Grimes 	 * packets, we do not want the sequence number of the
844df8bae1dSRodney W. Grimes 	 * retransmitted packet, we want the sequence number
845df8bae1dSRodney W. Grimes 	 * of the next unsent octet.  So, if there is no data
846df8bae1dSRodney W. Grimes 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
847df8bae1dSRodney W. Grimes 	 * when filling in ti_seq.  But if we are in persist
848df8bae1dSRodney W. Grimes 	 * state, snd_max might reflect one byte beyond the
849df8bae1dSRodney W. Grimes 	 * right edge of the window, so use snd_nxt in that
850df8bae1dSRodney W. Grimes 	 * case, since we know we aren't doing a retransmission.
851df8bae1dSRodney W. Grimes 	 * (retransmit and persist are mutually exclusive...)
852df8bae1dSRodney W. Grimes 	 */
8539b8b58e0SJonathan Lemon 	if (len || (flags & (TH_SYN|TH_FIN))
8549b8b58e0SJonathan Lemon 	    || callout_active(tp->tt_persist))
855fb59c426SYoshinobu Inoue 		th->th_seq = htonl(tp->snd_nxt);
856df8bae1dSRodney W. Grimes 	else
857fb59c426SYoshinobu Inoue 		th->th_seq = htonl(tp->snd_max);
8586d90faf3SPaul Saab 	if (sack_rxmit) {
8596d90faf3SPaul Saab 		th->th_seq = htonl(p->rxmit);
8606d90faf3SPaul Saab 		p->rxmit += len;
8616d90faf3SPaul Saab 	}
862fb59c426SYoshinobu Inoue 	th->th_ack = htonl(tp->rcv_nxt);
863df8bae1dSRodney W. Grimes 	if (optlen) {
864fb59c426SYoshinobu Inoue 		bcopy(opt, th + 1, optlen);
865fb59c426SYoshinobu Inoue 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
866df8bae1dSRodney W. Grimes 	}
867fb59c426SYoshinobu Inoue 	th->th_flags = flags;
868df8bae1dSRodney W. Grimes 	/*
869df8bae1dSRodney W. Grimes 	 * Calculate receive window.  Don't shrink window,
870df8bae1dSRodney W. Grimes 	 * but avoid silly window syndrome.
871df8bae1dSRodney W. Grimes 	 */
872201d185bSAndre Oppermann 	if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
873201d185bSAndre Oppermann 	    recwin < (long)tp->t_maxseg)
874201d185bSAndre Oppermann 		recwin = 0;
875201d185bSAndre Oppermann 	if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
876201d185bSAndre Oppermann 		recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
877201d185bSAndre Oppermann 	if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
878201d185bSAndre Oppermann 		recwin = (long)TCP_MAXWIN << tp->rcv_scale;
879201d185bSAndre Oppermann 	th->th_win = htons((u_short) (recwin >> tp->rcv_scale));
880262c1c1aSMatthew Dillon 
881262c1c1aSMatthew Dillon 
882262c1c1aSMatthew Dillon 	/*
883262c1c1aSMatthew Dillon 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised
884262c1c1aSMatthew Dillon 	 * a 0 window.  This may cause the remote transmitter to stall.  This
885262c1c1aSMatthew Dillon 	 * flag tells soreceive() to disable delayed acknowledgements when
886262c1c1aSMatthew Dillon 	 * draining the buffer.  This can occur if the receiver is attempting
887262c1c1aSMatthew Dillon 	 * to read more data then can be buffered prior to transmitting on
888262c1c1aSMatthew Dillon 	 * the connection.
889262c1c1aSMatthew Dillon 	 */
890201d185bSAndre Oppermann 	if (recwin == 0)
891262c1c1aSMatthew Dillon 		tp->t_flags |= TF_RXWIN0SENT;
892262c1c1aSMatthew Dillon 	else
893262c1c1aSMatthew Dillon 		tp->t_flags &= ~TF_RXWIN0SENT;
894df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
895fb59c426SYoshinobu Inoue 		th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
896fb59c426SYoshinobu Inoue 		th->th_flags |= TH_URG;
897df8bae1dSRodney W. Grimes 	} else
898df8bae1dSRodney W. Grimes 		/*
899df8bae1dSRodney W. Grimes 		 * If no urgent pointer to send, then we pull
900df8bae1dSRodney W. Grimes 		 * the urgent pointer to the left edge of the send window
901df8bae1dSRodney W. Grimes 		 * so that it doesn't drift into the send window on sequence
902df8bae1dSRodney W. Grimes 		 * number wraparound.
903df8bae1dSRodney W. Grimes 		 */
904df8bae1dSRodney W. Grimes 		tp->snd_up = tp->snd_una;		/* drag it along */
905df8bae1dSRodney W. Grimes 
9061cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
9071cfd4b53SBruce M Simpson #ifdef INET6
9081cfd4b53SBruce M Simpson 	if (!isipv6)
9091cfd4b53SBruce M Simpson #endif
9101cfd4b53SBruce M Simpson 	if (tp->t_flags & TF_SIGNATURE)
911265ed012SBruce M Simpson 		tcp_signature_compute(m, sizeof(struct ip), len, optlen,
9121cfd4b53SBruce M Simpson 		    (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
913265ed012SBruce M Simpson #endif
9141cfd4b53SBruce M Simpson 
915df8bae1dSRodney W. Grimes 	/*
916df8bae1dSRodney W. Grimes 	 * Put TCP length in extended header, and then
917df8bae1dSRodney W. Grimes 	 * checksum extended header and data.
918df8bae1dSRodney W. Grimes 	 */
919fb59c426SYoshinobu Inoue 	m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
920fb59c426SYoshinobu Inoue #ifdef INET6
921fb59c426SYoshinobu Inoue 	if (isipv6)
922fb59c426SYoshinobu Inoue 		/*
923fb59c426SYoshinobu Inoue 		 * ip6_plen is not need to be filled now, and will be filled
924fb59c426SYoshinobu Inoue 		 * in ip6_output.
925fb59c426SYoshinobu Inoue 		 */
926fb59c426SYoshinobu Inoue 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
927fb59c426SYoshinobu Inoue 				       sizeof(struct tcphdr) + optlen + len);
928fb59c426SYoshinobu Inoue 	else
929fb59c426SYoshinobu Inoue #endif /* INET6 */
930fb59c426SYoshinobu Inoue       {
931db4f9cc7SJonathan Lemon 	m->m_pkthdr.csum_flags = CSUM_TCP;
932db4f9cc7SJonathan Lemon 	m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
93379909384SJonathan Lemon 	th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
93479909384SJonathan Lemon 	    htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
935fb59c426SYoshinobu Inoue 
936db4f9cc7SJonathan Lemon 	/* IP version must be set here for ipv4/ipv6 checking later */
937db4f9cc7SJonathan Lemon 	KASSERT(ip->ip_v == IPVERSION,
9386e551fb6SDavid E. O'Brien 	    ("%s: IP version incorrect: %d", __func__, ip->ip_v));
939fb59c426SYoshinobu Inoue       }
940df8bae1dSRodney W. Grimes 
941df8bae1dSRodney W. Grimes 	/*
942df8bae1dSRodney W. Grimes 	 * In transmit state, time the transmission and arrange for
943df8bae1dSRodney W. Grimes 	 * the retransmit.  In persist state, just set snd_max.
944df8bae1dSRodney W. Grimes 	 */
9459b8b58e0SJonathan Lemon 	if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
946df8bae1dSRodney W. Grimes 		tcp_seq startseq = tp->snd_nxt;
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes 		/*
949df8bae1dSRodney W. Grimes 		 * Advance snd_nxt over sequence space of this segment.
950df8bae1dSRodney W. Grimes 		 */
951df8bae1dSRodney W. Grimes 		if (flags & (TH_SYN|TH_FIN)) {
952df8bae1dSRodney W. Grimes 			if (flags & TH_SYN)
953df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
954df8bae1dSRodney W. Grimes 			if (flags & TH_FIN) {
955df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
956df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_SENTFIN;
957df8bae1dSRodney W. Grimes 			}
958df8bae1dSRodney W. Grimes 		}
959e9f2f80eSJayanth Vijayaraghavan 		if (tp->sack_enable && sack_rxmit)
9606d90faf3SPaul Saab 			goto timer;
961df8bae1dSRodney W. Grimes 		tp->snd_nxt += len;
962df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
963df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt;
964df8bae1dSRodney W. Grimes 			/*
965df8bae1dSRodney W. Grimes 			 * Time this transmission if not a retransmission and
966df8bae1dSRodney W. Grimes 			 * not currently timing anything.
967df8bae1dSRodney W. Grimes 			 */
9689b8b58e0SJonathan Lemon 			if (tp->t_rtttime == 0) {
9699b8b58e0SJonathan Lemon 				tp->t_rtttime = ticks;
970df8bae1dSRodney W. Grimes 				tp->t_rtseq = startseq;
971df8bae1dSRodney W. Grimes 				tcpstat.tcps_segstimed++;
972df8bae1dSRodney W. Grimes 			}
973df8bae1dSRodney W. Grimes 		}
974df8bae1dSRodney W. Grimes 
975df8bae1dSRodney W. Grimes 		/*
976df8bae1dSRodney W. Grimes 		 * Set retransmit timer if not currently set,
97728257b5cSMatthew Dillon 		 * and not doing a pure ack or a keep-alive probe.
978df8bae1dSRodney W. Grimes 		 * Initial value for retransmit timer is smoothed
979df8bae1dSRodney W. Grimes 		 * round-trip time + 2 * round-trip time variance.
980df8bae1dSRodney W. Grimes 		 * Initialize shift counter which is used for backoff
981df8bae1dSRodney W. Grimes 		 * of retransmit time.
982df8bae1dSRodney W. Grimes 		 */
9836d90faf3SPaul Saab timer:
9846d90faf3SPaul Saab 		if (tp->sack_enable && sack_rxmit &&
9856d90faf3SPaul Saab 		    !callout_active(tp->tt_rexmt) &&
9866d90faf3SPaul Saab 		    tp->snd_nxt != tp->snd_max) {
9876d90faf3SPaul Saab 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
9886d90faf3SPaul Saab 				      tcp_timer_rexmt, tp);
9896d90faf3SPaul Saab 			if (callout_active(tp->tt_persist)) {
9906d90faf3SPaul Saab 				callout_stop(tp->tt_persist);
9916d90faf3SPaul Saab 				tp->t_rxtshift = 0;
9926d90faf3SPaul Saab 			}
9936d90faf3SPaul Saab 		}
9949b8b58e0SJonathan Lemon 		if (!callout_active(tp->tt_rexmt) &&
995df8bae1dSRodney W. Grimes 		    tp->snd_nxt != tp->snd_una) {
9969b8b58e0SJonathan Lemon 			if (callout_active(tp->tt_persist)) {
9979b8b58e0SJonathan Lemon 				callout_stop(tp->tt_persist);
998df8bae1dSRodney W. Grimes 				tp->t_rxtshift = 0;
999df8bae1dSRodney W. Grimes 			}
100046f58482SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
100146f58482SJonathan Lemon 				      tcp_timer_rexmt, tp);
1002df8bae1dSRodney W. Grimes 		}
100328257b5cSMatthew Dillon 	} else {
100428257b5cSMatthew Dillon 		/*
100528257b5cSMatthew Dillon 		 * Persist case, update snd_max but since we are in
100628257b5cSMatthew Dillon 		 * persist mode (no window) we do not update snd_nxt.
100728257b5cSMatthew Dillon 		 */
100828257b5cSMatthew Dillon 		int xlen = len;
100928257b5cSMatthew Dillon 		if (flags & TH_SYN)
101028257b5cSMatthew Dillon 			++xlen;
101128257b5cSMatthew Dillon 		if (flags & TH_FIN) {
101228257b5cSMatthew Dillon 			++xlen;
101328257b5cSMatthew Dillon 			tp->t_flags |= TF_SENTFIN;
101428257b5cSMatthew Dillon 		}
1015abac41a6SMatthew Dillon 		if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
1016df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt + len;
101728257b5cSMatthew Dillon 	}
1018df8bae1dSRodney W. Grimes 
1019610ee2f9SDavid Greenman #ifdef TCPDEBUG
1020df8bae1dSRodney W. Grimes 	/*
1021df8bae1dSRodney W. Grimes 	 * Trace.
1022df8bae1dSRodney W. Grimes 	 */
102391f467d5SHartmut Brandt 	if (so->so_options & SO_DEBUG) {
1024f3e0b7efSBruce M Simpson 		u_short save = 0;
10255214cb3fSBruce M Simpson #ifdef INET6
10265214cb3fSBruce M Simpson 		if (!isipv6)
10275214cb3fSBruce M Simpson #endif
10285214cb3fSBruce M Simpson 		{
10295214cb3fSBruce M Simpson 			save = ipov->ih_len;
103091f467d5SHartmut Brandt 			ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
10315214cb3fSBruce M Simpson 		}
1032fb59c426SYoshinobu Inoue 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
10335214cb3fSBruce M Simpson #ifdef INET6
10345214cb3fSBruce M Simpson 		if (!isipv6)
10355214cb3fSBruce M Simpson #endif
103691f467d5SHartmut Brandt 		ipov->ih_len = save;
103791f467d5SHartmut Brandt 	}
1038610ee2f9SDavid Greenman #endif
1039df8bae1dSRodney W. Grimes 
1040df8bae1dSRodney W. Grimes 	/*
1041df8bae1dSRodney W. Grimes 	 * Fill in IP length and desired time to live and
1042df8bae1dSRodney W. Grimes 	 * send to IP level.  There should be a better way
1043df8bae1dSRodney W. Grimes 	 * to handle ttl and tos; we could keep them in
1044df8bae1dSRodney W. Grimes 	 * the template, but need a way to checksum without them.
1045df8bae1dSRodney W. Grimes 	 */
1046fb59c426SYoshinobu Inoue 	/*
1047fb59c426SYoshinobu Inoue 	 * m->m_pkthdr.len should have been set before cksum calcuration,
1048fb59c426SYoshinobu Inoue 	 * because in6_cksum() need it.
1049fb59c426SYoshinobu Inoue 	 */
1050fb59c426SYoshinobu Inoue #ifdef INET6
1051fb59c426SYoshinobu Inoue 	if (isipv6) {
1052fb59c426SYoshinobu Inoue 		/*
1053fb59c426SYoshinobu Inoue 		 * we separately set hoplimit for every segment, since the
1054fb59c426SYoshinobu Inoue 		 * user might want to change the value via setsockopt.
1055fb59c426SYoshinobu Inoue 		 * Also, desired default hop limit might be changed via
1056fb59c426SYoshinobu Inoue 		 * Neighbor Discovery.
1057fb59c426SYoshinobu Inoue 		 */
105897d8d152SAndre Oppermann 		ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1059fb59c426SYoshinobu Inoue 
1060fb59c426SYoshinobu Inoue 		/* TODO: IPv6 IP6TOS_ECT bit on */
1061fb59c426SYoshinobu Inoue 		error = ip6_output(m,
106297d8d152SAndre Oppermann 			    tp->t_inpcb->in6p_outputopts, NULL,
10635d846453SSam Leffler 			    (so->so_options & SO_DONTROUTE), NULL, NULL,
10645d846453SSam Leffler 			    tp->t_inpcb);
1065fb59c426SYoshinobu Inoue 	} else
1066fb59c426SYoshinobu Inoue #endif /* INET6 */
1067df8bae1dSRodney W. Grimes     {
1068fb59c426SYoshinobu Inoue 	ip->ip_len = m->m_pkthdr.len;
1069686cdd19SJun-ichiro itojun Hagino #ifdef INET6
1070686cdd19SJun-ichiro itojun Hagino  	if (INP_CHECK_SOCKAF(so, AF_INET6))
107197d8d152SAndre Oppermann  		ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1072686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */
1073f138387aSGarrett Wollman 	/*
107497d8d152SAndre Oppermann 	 * If we do path MTU discovery, then we set DF on every packet.
107597d8d152SAndre Oppermann 	 * This might not be the best thing to do according to RFC3390
107697d8d152SAndre Oppermann 	 * Section 2. However the tcp hostcache migitates the problem
107797d8d152SAndre Oppermann 	 * so it affects only the first tcp connection with a host.
1078f138387aSGarrett Wollman 	 */
107997d8d152SAndre Oppermann 	if (path_mtu_discovery)
1080fb59c426SYoshinobu Inoue 		ip->ip_off |= IP_DF;
108197d8d152SAndre Oppermann 
108297d8d152SAndre Oppermann 	error = ip_output(m, tp->t_inpcb->inp_options, NULL,
10835d846453SSam Leffler 	    (so->so_options & SO_DONTROUTE), 0, tp->t_inpcb);
1084df8bae1dSRodney W. Grimes     }
1085df8bae1dSRodney W. Grimes 	if (error) {
10867734ea06SArchie Cobbs 
10877734ea06SArchie Cobbs 		/*
10887734ea06SArchie Cobbs 		 * We know that the packet was lost, so back out the
10897734ea06SArchie Cobbs 		 * sequence number advance, if any.
10907734ea06SArchie Cobbs 		 */
10917734ea06SArchie Cobbs 		if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
10927734ea06SArchie Cobbs 			/*
10937734ea06SArchie Cobbs 			 * No need to check for TH_FIN here because
10947734ea06SArchie Cobbs 			 * the TF_SENTFIN flag handles that case.
10957734ea06SArchie Cobbs 			 */
10965d3b1b75SJayanth Vijayaraghavan 			if ((flags & TH_SYN) == 0) {
10975d3b1b75SJayanth Vijayaraghavan 				if (sack_rxmit)
10985d3b1b75SJayanth Vijayaraghavan 					p->rxmit -= len;
10995d3b1b75SJayanth Vijayaraghavan 				else
11007734ea06SArchie Cobbs 					tp->snd_nxt -= len;
11017734ea06SArchie Cobbs 			}
11025d3b1b75SJayanth Vijayaraghavan 		}
11037734ea06SArchie Cobbs 
1104df8bae1dSRodney W. Grimes out:
1105df8bae1dSRodney W. Grimes 		if (error == ENOBUFS) {
110659f577adSJonathan Lemon 	                if (!callout_active(tp->tt_rexmt) &&
110759f577adSJonathan Lemon                             !callout_active(tp->tt_persist))
110859f577adSJonathan Lemon 	                        callout_reset(tp->tt_rexmt, tp->t_rxtcur,
110959f577adSJonathan Lemon                                       tcp_timer_rexmt, tp);
1110df8bae1dSRodney W. Grimes 			tcp_quench(tp->t_inpcb, 0);
1111df8bae1dSRodney W. Grimes 			return (0);
1112df8bae1dSRodney W. Grimes 		}
11133d1f141bSGarrett Wollman 		if (error == EMSGSIZE) {
11143d1f141bSGarrett Wollman 			/*
11153d1f141bSGarrett Wollman 			 * ip_output() will have already fixed the route
11163d1f141bSGarrett Wollman 			 * for us.  tcp_mtudisc() will, as its last action,
11173d1f141bSGarrett Wollman 			 * initiate retransmission, so it is important to
11183d1f141bSGarrett Wollman 			 * not do so here.
11193d1f141bSGarrett Wollman 			 */
11203d1f141bSGarrett Wollman 			tcp_mtudisc(tp->t_inpcb, 0);
11213d1f141bSGarrett Wollman 			return 0;
11223d1f141bSGarrett Wollman 		}
1123df8bae1dSRodney W. Grimes 		if ((error == EHOSTUNREACH || error == ENETDOWN)
1124df8bae1dSRodney W. Grimes 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
1125df8bae1dSRodney W. Grimes 			tp->t_softerror = error;
1126df8bae1dSRodney W. Grimes 			return (0);
1127df8bae1dSRodney W. Grimes 		}
1128df8bae1dSRodney W. Grimes 		return (error);
1129df8bae1dSRodney W. Grimes 	}
1130df8bae1dSRodney W. Grimes 	tcpstat.tcps_sndtotal++;
1131df8bae1dSRodney W. Grimes 
1132df8bae1dSRodney W. Grimes 	/*
1133df8bae1dSRodney W. Grimes 	 * Data sent (as far as we can tell).
1134df8bae1dSRodney W. Grimes 	 * If this advertises a larger window than any other segment,
1135df8bae1dSRodney W. Grimes 	 * then remember the size of the advertised window.
1136df8bae1dSRodney W. Grimes 	 * Any pending ACK has now been sent.
1137df8bae1dSRodney W. Grimes 	 */
1138201d185bSAndre Oppermann 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1139201d185bSAndre Oppermann 		tp->rcv_adv = tp->rcv_nxt + recwin;
1140df8bae1dSRodney W. Grimes 	tp->last_ack_sent = tp->rcv_nxt;
11413bfd6421SJonathan Lemon 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
11423bfd6421SJonathan Lemon 	if (callout_active(tp->tt_delack))
11439b8b58e0SJonathan Lemon 		callout_stop(tp->tt_delack);
1144d912c694SMatthew Dillon #if 0
1145d912c694SMatthew Dillon 	/*
1146d912c694SMatthew Dillon 	 * This completely breaks TCP if newreno is turned on.  What happens
1147d912c694SMatthew Dillon 	 * is that if delayed-acks are turned on on the receiver, this code
1148d912c694SMatthew Dillon 	 * on the transmitter effectively destroys the TCP window, forcing
1149d912c694SMatthew Dillon 	 * it to four packets (1.5Kx4 = 6K window).
1150d912c694SMatthew Dillon 	 */
115146f58482SJonathan Lemon 	if (sendalot && (!tcp_do_newreno || --maxburst))
1152df8bae1dSRodney W. Grimes 		goto again;
1153d912c694SMatthew Dillon #endif
1154d912c694SMatthew Dillon 	if (sendalot)
1155d912c694SMatthew Dillon 		goto again;
1156df8bae1dSRodney W. Grimes 	return (0);
1157df8bae1dSRodney W. Grimes }
1158df8bae1dSRodney W. Grimes 
1159df8bae1dSRodney W. Grimes void
1160df8bae1dSRodney W. Grimes tcp_setpersist(tp)
1161df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1162df8bae1dSRodney W. Grimes {
11639b8b58e0SJonathan Lemon 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
11649b8b58e0SJonathan Lemon 	int tt;
1165df8bae1dSRodney W. Grimes 
11669b8b58e0SJonathan Lemon 	if (callout_active(tp->tt_rexmt))
11679b8b58e0SJonathan Lemon 		panic("tcp_setpersist: retransmit pending");
1168df8bae1dSRodney W. Grimes 	/*
1169df8bae1dSRodney W. Grimes 	 * Start/restart persistance timer.
1170df8bae1dSRodney W. Grimes 	 */
11719b8b58e0SJonathan Lemon 	TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1172df8bae1dSRodney W. Grimes 		      TCPTV_PERSMIN, TCPTV_PERSMAX);
11739b8b58e0SJonathan Lemon 	callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp);
1174df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1175df8bae1dSRodney W. Grimes 		tp->t_rxtshift++;
1176df8bae1dSRodney W. Grimes }
1177