xref: /freebsd/sys/netinet/tcp_output.c (revision c94c54e4df9ae401d299ca3b08f00acade2fccc8)
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;
127a55db2b6SPaul Saab 	int sack_bytes_rxmt;
1286d90faf3SPaul Saab 	struct sackhole *p;
129262c1c1aSMatthew Dillon #if 0
13046f58482SJonathan Lemon 	int maxburst = TCP_MAXBURST;
131262c1c1aSMatthew Dillon #endif
132fb59c426SYoshinobu Inoue #ifdef INET6
133f10e85d7SLuigi Rizzo 	struct ip6_hdr *ip6 = NULL;
134fb59c426SYoshinobu Inoue 	int isipv6;
135fb59c426SYoshinobu Inoue 
136fb59c426SYoshinobu Inoue 	isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
137fb59c426SYoshinobu Inoue #endif
138df8bae1dSRodney W. Grimes 
139fa286d7dSSam Leffler 	INP_LOCK_ASSERT(tp->t_inpcb);
1403d6ade3aSJennifer Yang 
141df8bae1dSRodney W. Grimes 	/*
142df8bae1dSRodney W. Grimes 	 * Determine length of data that should be transmitted,
143df8bae1dSRodney W. Grimes 	 * and flags that will be used.
144df8bae1dSRodney W. Grimes 	 * If there is some data or critical controls (SYN, RST)
145df8bae1dSRodney W. Grimes 	 * to send, then transmit; otherwise, investigate further.
146df8bae1dSRodney W. Grimes 	 */
147c24d5daeSJayanth Vijayaraghavan 	idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
1489b8b58e0SJonathan Lemon 	if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
149df8bae1dSRodney W. Grimes 		/*
150df8bae1dSRodney W. Grimes 		 * We have been idle for "a while" and no acks are
151df8bae1dSRodney W. Grimes 		 * expected to clock out any data we send --
152df8bae1dSRodney W. Grimes 		 * slow start to get ack "clock" running again.
1539b8b58e0SJonathan Lemon 		 *
1549b8b58e0SJonathan Lemon 		 * Set the slow-start flight size depending on whether
1559b8b58e0SJonathan Lemon 		 * this is a local network or not.
156df8bae1dSRodney W. Grimes 		 */
157f10e85d7SLuigi Rizzo 		int ss = ss_fltsz;
158fb59c426SYoshinobu Inoue #ifdef INET6
159f10e85d7SLuigi Rizzo 		if (isipv6) {
160f10e85d7SLuigi Rizzo 			if (in6_localaddr(&tp->t_inpcb->in6p_faddr))
161f10e85d7SLuigi Rizzo 				ss = ss_fltsz_local;
162f10e85d7SLuigi Rizzo 		} else
163f10e85d7SLuigi Rizzo #endif /* INET6 */
164f10e85d7SLuigi Rizzo 		if (in_localaddr(tp->t_inpcb->inp_faddr))
165f10e85d7SLuigi Rizzo 			ss = ss_fltsz_local;
166f10e85d7SLuigi Rizzo 		tp->snd_cwnd = tp->t_maxseg * ss;
1679b8b58e0SJonathan Lemon 	}
168c24d5daeSJayanth Vijayaraghavan 	tp->t_flags &= ~TF_LASTIDLE;
169c24d5daeSJayanth Vijayaraghavan 	if (idle) {
170c24d5daeSJayanth Vijayaraghavan 		if (tp->t_flags & TF_MORETOCOME) {
171c24d5daeSJayanth Vijayaraghavan 			tp->t_flags |= TF_LASTIDLE;
172c24d5daeSJayanth Vijayaraghavan 			idle = 0;
173c24d5daeSJayanth Vijayaraghavan 		}
174c24d5daeSJayanth Vijayaraghavan 	}
175df8bae1dSRodney W. Grimes again:
1766d90faf3SPaul Saab 	/*
1776d90faf3SPaul Saab 	 * If we've recently taken a timeout, snd_max will be greater than
1786d90faf3SPaul Saab 	 * snd_nxt.  There may be SACK information that allows us to avoid
1796d90faf3SPaul Saab 	 * resending already delivered data.  Adjust snd_nxt accordingly.
1806d90faf3SPaul Saab 	 */
1816d90faf3SPaul Saab 	if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max))
1826d90faf3SPaul Saab 		tcp_sack_adjust(tp);
183df8bae1dSRodney W. Grimes 	sendalot = 0;
184df8bae1dSRodney W. Grimes 	off = tp->snd_nxt - tp->snd_una;
185201d185bSAndre Oppermann 	sendwin = min(tp->snd_wnd, tp->snd_cwnd);
186201d185bSAndre Oppermann 	sendwin = min(sendwin, tp->snd_bwnd);
187df8bae1dSRodney W. Grimes 
188df8bae1dSRodney W. Grimes 	flags = tcp_outflags[tp->t_state];
189a0292f23SGarrett Wollman 	/*
1906d90faf3SPaul Saab 	 * Send any SACK-generated retransmissions.  If we're explicitly trying
1916d90faf3SPaul Saab 	 * to send out new data (when sendalot is 1), bypass this function.
1926d90faf3SPaul Saab 	 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
1936d90faf3SPaul Saab 	 * we're replacing a (future) new transmission with a retransmission
1946d90faf3SPaul Saab 	 * now, and we previously incremented snd_cwnd in tcp_input().
1956d90faf3SPaul Saab 	 */
1966d90faf3SPaul Saab 	/*
1976d90faf3SPaul Saab 	 * Still in sack recovery , reset rxmit flag to zero.
1986d90faf3SPaul Saab 	 */
1996d90faf3SPaul Saab 	sack_rxmit = 0;
200a55db2b6SPaul Saab 	sack_bytes_rxmt = 0;
2016d90faf3SPaul Saab 	len = 0;
2026d90faf3SPaul Saab 	p = NULL;
20304f0d9a0SJayanth Vijayaraghavan 	if (tp->sack_enable && IN_FASTRECOVERY(tp) &&
204a55db2b6SPaul Saab 	    (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
205a55db2b6SPaul Saab 		long cwin;
206a55db2b6SPaul Saab 
207a55db2b6SPaul Saab 		cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
208a55db2b6SPaul Saab 		if (cwin < 0)
209a55db2b6SPaul Saab 			cwin = 0;
210f787edd8SJayanth Vijayaraghavan 		/* Do not retransmit SACK segments beyond snd_recover */
211f787edd8SJayanth Vijayaraghavan 		if (SEQ_GT(p->end, tp->snd_recover)) {
212f787edd8SJayanth Vijayaraghavan 			/*
213f787edd8SJayanth Vijayaraghavan 			 * (At least) part of sack hole extends beyond
214f787edd8SJayanth Vijayaraghavan 			 * snd_recover. Check to see if we can rexmit data
215f787edd8SJayanth Vijayaraghavan 			 * for this hole.
216f787edd8SJayanth Vijayaraghavan 			 */
217f787edd8SJayanth Vijayaraghavan 			if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
218f787edd8SJayanth Vijayaraghavan 				/*
219f787edd8SJayanth Vijayaraghavan 				 * Can't rexmit any more data for this hole.
220f787edd8SJayanth Vijayaraghavan 				 * That data will be rexmitted in the next
221f787edd8SJayanth Vijayaraghavan 				 * sack recovery episode, when snd_recover
222f787edd8SJayanth Vijayaraghavan 				 * moves past p->rxmit.
223f787edd8SJayanth Vijayaraghavan 				 */
224f787edd8SJayanth Vijayaraghavan 				p = NULL;
225f787edd8SJayanth Vijayaraghavan 				goto after_sack_rexmit;
226f787edd8SJayanth Vijayaraghavan 			} else
227f787edd8SJayanth Vijayaraghavan 				/* Can rexmit part of the current hole */
228a55db2b6SPaul Saab 				len = ((long)ulmin(cwin,
229f787edd8SJayanth Vijayaraghavan 						   tp->snd_recover - p->rxmit));
230f787edd8SJayanth Vijayaraghavan 		} else
231a55db2b6SPaul Saab 			len = ((long)ulmin(cwin, p->end - p->rxmit));
2326d90faf3SPaul Saab 		off = p->rxmit - tp->snd_una;
233f787edd8SJayanth Vijayaraghavan 		KASSERT(off >= 0,("%s: sack block to the left of una : %d",
234f787edd8SJayanth Vijayaraghavan 		    __func__, off));
2356d90faf3SPaul Saab 		if (len > 0) {
236ab5c14d8SRobert Watson 			sack_rxmit = 1;
237ab5c14d8SRobert Watson 			sendalot = 1;
2386d90faf3SPaul Saab 			tcpstat.tcps_sack_rexmits++;
2396d90faf3SPaul Saab 			tcpstat.tcps_sack_rexmit_bytes +=
2406d90faf3SPaul Saab 			    min(len, tp->t_maxseg);
2416d90faf3SPaul Saab 		}
2426d90faf3SPaul Saab 	}
243f787edd8SJayanth Vijayaraghavan after_sack_rexmit:
2446d90faf3SPaul Saab 	/*
245a0292f23SGarrett Wollman 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
246a0292f23SGarrett Wollman 	 * state flags.
247a0292f23SGarrett Wollman 	 */
248a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDFIN)
249a0292f23SGarrett Wollman 		flags |= TH_FIN;
250a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDSYN)
251a0292f23SGarrett Wollman 		flags |= TH_SYN;
252a0292f23SGarrett Wollman 
253cf2942b6SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
254df8bae1dSRodney W. Grimes 	/*
255df8bae1dSRodney W. Grimes 	 * If in persist timeout with window of 0, send 1 byte.
256df8bae1dSRodney W. Grimes 	 * Otherwise, if window is small but nonzero
257df8bae1dSRodney W. Grimes 	 * and timer expired, we will send what we can
258df8bae1dSRodney W. Grimes 	 * and go to transmit state.
259df8bae1dSRodney W. Grimes 	 */
260df8bae1dSRodney W. Grimes 	if (tp->t_force) {
261201d185bSAndre Oppermann 		if (sendwin == 0) {
262df8bae1dSRodney W. Grimes 			/*
263df8bae1dSRodney W. Grimes 			 * If we still have some data to send, then
264df8bae1dSRodney W. Grimes 			 * clear the FIN bit.  Usually this would
265df8bae1dSRodney W. Grimes 			 * happen below when it realizes that we
266df8bae1dSRodney W. Grimes 			 * aren't sending all the data.  However,
26729089b51SJulian Elischer 			 * if we have exactly 1 byte of unsent data,
268df8bae1dSRodney W. Grimes 			 * then it won't clear the FIN bit below,
269df8bae1dSRodney W. Grimes 			 * and if we are in persist state, we wind
270df8bae1dSRodney W. Grimes 			 * up sending the packet without recording
271df8bae1dSRodney W. Grimes 			 * that we sent the FIN bit.
272df8bae1dSRodney W. Grimes 			 *
273df8bae1dSRodney W. Grimes 			 * We can't just blindly clear the FIN bit,
274df8bae1dSRodney W. Grimes 			 * because if we don't have any more data
275df8bae1dSRodney W. Grimes 			 * to send then the probe will be the FIN
276df8bae1dSRodney W. Grimes 			 * itself.
277df8bae1dSRodney W. Grimes 			 */
278df8bae1dSRodney W. Grimes 			if (off < so->so_snd.sb_cc)
279df8bae1dSRodney W. Grimes 				flags &= ~TH_FIN;
280201d185bSAndre Oppermann 			sendwin = 1;
281df8bae1dSRodney W. Grimes 		} else {
2829b8b58e0SJonathan Lemon 			callout_stop(tp->tt_persist);
283df8bae1dSRodney W. Grimes 			tp->t_rxtshift = 0;
284df8bae1dSRodney W. Grimes 		}
285df8bae1dSRodney W. Grimes 	}
286df8bae1dSRodney W. Grimes 
28728257b5cSMatthew Dillon 	/*
28828257b5cSMatthew Dillon 	 * If snd_nxt == snd_max and we have transmitted a FIN, the
28928257b5cSMatthew Dillon 	 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
290201d185bSAndre Oppermann 	 * a negative length.  This can also occur when TCP opens up
29128257b5cSMatthew Dillon 	 * its congestion window while receiving additional duplicate
29228257b5cSMatthew Dillon 	 * acks after fast-retransmit because TCP will reset snd_nxt
29328257b5cSMatthew Dillon 	 * to snd_max after the fast-retransmit.
29428257b5cSMatthew Dillon 	 *
29528257b5cSMatthew Dillon 	 * In the normal retransmit-FIN-only case, however, snd_nxt will
29628257b5cSMatthew Dillon 	 * be set to snd_una, the offset will be 0, and the length may
29728257b5cSMatthew Dillon 	 * wind up 0.
2986d90faf3SPaul Saab 	 *
2996d90faf3SPaul Saab 	 * If sack_rxmit is true we are retransmitting from the scoreboard
3006d90faf3SPaul Saab 	 * in which case len is already set.
30128257b5cSMatthew Dillon 	 */
302a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
303a55db2b6SPaul Saab 		if (sack_bytes_rxmt == 0)
3046d90faf3SPaul Saab 			len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
305a55db2b6SPaul Saab 		else {
306a55db2b6SPaul Saab 			long cwin;
307a55db2b6SPaul Saab 
308a55db2b6SPaul Saab                         /*
309a55db2b6SPaul Saab 			 * We are inside of a SACK recovery episode and are
310a55db2b6SPaul Saab 			 * sending new data, having retransmitted all the
311a55db2b6SPaul Saab 			 * data possible in the scoreboard.
312a55db2b6SPaul Saab 			 */
313a55db2b6SPaul Saab 			len = so->so_snd.sb_cc - off;
314a55db2b6SPaul Saab 			cwin = sendwin - (tp->snd_nxt - tp->sack_newdata) -
315a55db2b6SPaul Saab 				sack_bytes_rxmt;
316a55db2b6SPaul Saab 			if (cwin < 0)
317a55db2b6SPaul Saab 				cwin = 0;
318a55db2b6SPaul Saab 			len = lmin(len, cwin);
319a55db2b6SPaul Saab 		}
320a55db2b6SPaul Saab 	}
321a0292f23SGarrett Wollman 
322a0292f23SGarrett Wollman 	/*
323a0292f23SGarrett Wollman 	 * Lop off SYN bit if it has already been sent.  However, if this
324a0292f23SGarrett Wollman 	 * is SYN-SENT state and if segment contains data and if we don't
325a0292f23SGarrett Wollman 	 * know that foreign host supports TAO, suppress sending segment.
326a0292f23SGarrett Wollman 	 */
327a0292f23SGarrett Wollman 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
328a0292f23SGarrett Wollman 		flags &= ~TH_SYN;
329a0292f23SGarrett Wollman 		off--, len++;
330a0292f23SGarrett Wollman 	}
331a0292f23SGarrett Wollman 
33281165e48SAndras Olah 	/*
333c94c54e4SAndre Oppermann 	 * Be careful not to send data and/or FIN on SYN segments.
33481165e48SAndras Olah 	 * This measure is needed to prevent interoperability problems
33581165e48SAndras Olah 	 * with not fully conformant TCP implementations.
33681165e48SAndras Olah 	 */
337c94c54e4SAndre Oppermann 	if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
33881165e48SAndras Olah 		len = 0;
33981165e48SAndras Olah 		flags &= ~TH_FIN;
34081165e48SAndras Olah 	}
34181165e48SAndras Olah 
342df8bae1dSRodney W. Grimes 	if (len < 0) {
343df8bae1dSRodney W. Grimes 		/*
344df8bae1dSRodney W. Grimes 		 * If FIN has been sent but not acked,
345df8bae1dSRodney W. Grimes 		 * but we haven't been called to retransmit,
34628257b5cSMatthew Dillon 		 * len will be < 0.  Otherwise, window shrank
347df8bae1dSRodney W. Grimes 		 * after we sent into it.  If window shrank to 0,
3482d8266afSDavid Greenman 		 * cancel pending retransmit, pull snd_nxt back
3492d8266afSDavid Greenman 		 * to (closed) window, and set the persist timer
3502d8266afSDavid Greenman 		 * if it isn't already going.  If the window didn't
3512d8266afSDavid Greenman 		 * close completely, just wait for an ACK.
352df8bae1dSRodney W. Grimes 		 */
353df8bae1dSRodney W. Grimes 		len = 0;
354201d185bSAndre Oppermann 		if (sendwin == 0) {
3559b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
3562d8266afSDavid Greenman 			tp->t_rxtshift = 0;
357df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
3589b8b58e0SJonathan Lemon 			if (!callout_active(tp->tt_persist))
3592d8266afSDavid Greenman 				tcp_setpersist(tp);
360df8bae1dSRodney W. Grimes 		}
361df8bae1dSRodney W. Grimes 	}
36228257b5cSMatthew Dillon 
36328257b5cSMatthew Dillon 	/*
36428257b5cSMatthew Dillon 	 * len will be >= 0 after this point.  Truncate to the maximum
36528257b5cSMatthew Dillon 	 * segment length and ensure that FIN is removed if the length
36628257b5cSMatthew Dillon 	 * no longer contains the last data byte.
36728257b5cSMatthew Dillon 	 */
368df8bae1dSRodney W. Grimes 	if (len > tp->t_maxseg) {
369df8bae1dSRodney W. Grimes 		len = tp->t_maxseg;
370df8bae1dSRodney W. Grimes 		sendalot = 1;
371df8bae1dSRodney W. Grimes 	}
3725d3b1b75SJayanth Vijayaraghavan 	if (sack_rxmit) {
3735d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
374df8bae1dSRodney W. Grimes 			flags &= ~TH_FIN;
3755d3b1b75SJayanth Vijayaraghavan 	} else {
3765d3b1b75SJayanth Vijayaraghavan 		if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
3775d3b1b75SJayanth Vijayaraghavan 			flags &= ~TH_FIN;
3785d3b1b75SJayanth Vijayaraghavan 	}
379df8bae1dSRodney W. Grimes 
380201d185bSAndre Oppermann 	recwin = sbspace(&so->so_rcv);
381df8bae1dSRodney W. Grimes 
382df8bae1dSRodney W. Grimes 	/*
383262c1c1aSMatthew Dillon 	 * Sender silly window avoidance.   We transmit under the following
384262c1c1aSMatthew Dillon 	 * conditions when len is non-zero:
385262c1c1aSMatthew Dillon 	 *
386262c1c1aSMatthew Dillon 	 *	- We have a full segment
387262c1c1aSMatthew Dillon 	 *	- This is the last buffer in a write()/send() and we are
388262c1c1aSMatthew Dillon 	 *	  either idle or running NODELAY
389262c1c1aSMatthew Dillon 	 *	- we've timed out (e.g. persist timer)
390262c1c1aSMatthew Dillon 	 *	- we have more then 1/2 the maximum send window's worth of
391262c1c1aSMatthew Dillon 	 *	  data (receiver may be limited the window size)
392262c1c1aSMatthew Dillon 	 *	- we need to retransmit
393df8bae1dSRodney W. Grimes 	 */
394df8bae1dSRodney W. Grimes 	if (len) {
395df8bae1dSRodney W. Grimes 		if (len == tp->t_maxseg)
396df8bae1dSRodney W. Grimes 			goto send;
397262c1c1aSMatthew Dillon 		/*
398262c1c1aSMatthew Dillon 		 * NOTE! on localhost connections an 'ack' from the remote
399262c1c1aSMatthew Dillon 		 * end may occur synchronously with the output and cause
400262c1c1aSMatthew Dillon 		 * us to flush a buffer queued with moretocome.  XXX
401262c1c1aSMatthew Dillon 		 *
402262c1c1aSMatthew Dillon 		 * note: the len + off check is almost certainly unnecessary.
403262c1c1aSMatthew Dillon 		 */
404262c1c1aSMatthew Dillon 		if (!(tp->t_flags & TF_MORETOCOME) &&	/* normal case */
405262c1c1aSMatthew Dillon 		    (idle || (tp->t_flags & TF_NODELAY)) &&
406262c1c1aSMatthew Dillon 		    len + off >= so->so_snd.sb_cc &&
407262c1c1aSMatthew Dillon 		    (tp->t_flags & TF_NOPUSH) == 0) {
408df8bae1dSRodney W. Grimes 			goto send;
409262c1c1aSMatthew Dillon 		}
410262c1c1aSMatthew Dillon 		if (tp->t_force)			/* typ. timeout case */
411df8bae1dSRodney W. Grimes 			goto send;
412a0292f23SGarrett Wollman 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
413df8bae1dSRodney W. Grimes 			goto send;
414262c1c1aSMatthew Dillon 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))	/* retransmit case */
415df8bae1dSRodney W. Grimes 			goto send;
4166d90faf3SPaul Saab 		if (sack_rxmit)
4176d90faf3SPaul Saab 			goto send;
418df8bae1dSRodney W. Grimes 	}
419df8bae1dSRodney W. Grimes 
420df8bae1dSRodney W. Grimes 	/*
421df8bae1dSRodney W. Grimes 	 * Compare available window to amount of window
422df8bae1dSRodney W. Grimes 	 * known to peer (as advertised window less
423df8bae1dSRodney W. Grimes 	 * next expected input).  If the difference is at least two
424df8bae1dSRodney W. Grimes 	 * max size segments, or at least 50% of the maximum possible
425df8bae1dSRodney W. Grimes 	 * window, then want to send a window update to peer.
4263bfd6421SJonathan Lemon 	 * Skip this if the connection is in T/TCP half-open state.
427df8bae1dSRodney W. Grimes 	 */
428201d185bSAndre Oppermann 	if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) {
429df8bae1dSRodney W. Grimes 		/*
430df8bae1dSRodney W. Grimes 		 * "adv" is the amount we can increase the window,
431df8bae1dSRodney W. Grimes 		 * taking into account that we are limited by
432df8bae1dSRodney W. Grimes 		 * TCP_MAXWIN << tp->rcv_scale.
433df8bae1dSRodney W. Grimes 		 */
434201d185bSAndre Oppermann 		long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
435df8bae1dSRodney W. Grimes 			(tp->rcv_adv - tp->rcv_nxt);
436df8bae1dSRodney W. Grimes 
437df8bae1dSRodney W. Grimes 		if (adv >= (long) (2 * tp->t_maxseg))
438df8bae1dSRodney W. Grimes 			goto send;
439df8bae1dSRodney W. Grimes 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
440df8bae1dSRodney W. Grimes 			goto send;
441df8bae1dSRodney W. Grimes 	}
442df8bae1dSRodney W. Grimes 
443df8bae1dSRodney W. Grimes 	/*
44428257b5cSMatthew Dillon 	 * Send if we owe the peer an ACK, RST, SYN, or urgent data.  ACKNOW
44528257b5cSMatthew Dillon 	 * is also a catch-all for the retransmit timer timeout case.
446df8bae1dSRodney W. Grimes 	 */
447df8bae1dSRodney W. Grimes 	if (tp->t_flags & TF_ACKNOW)
448df8bae1dSRodney W. Grimes 		goto send;
449a0292f23SGarrett Wollman 	if ((flags & TH_RST) ||
450a0292f23SGarrett Wollman 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
451df8bae1dSRodney W. Grimes 		goto send;
452df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_una))
453df8bae1dSRodney W. Grimes 		goto send;
454df8bae1dSRodney W. Grimes 	/*
455df8bae1dSRodney W. Grimes 	 * If our state indicates that FIN should be sent
45628257b5cSMatthew Dillon 	 * and we have not yet done so, then we need to send.
457df8bae1dSRodney W. Grimes 	 */
458df8bae1dSRodney W. Grimes 	if (flags & TH_FIN &&
459df8bae1dSRodney W. Grimes 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
460df8bae1dSRodney W. Grimes 		goto send;
4616d90faf3SPaul Saab 	/*
4626d90faf3SPaul Saab 	 * In SACK, it is possible for tcp_output to fail to send a segment
4636d90faf3SPaul Saab 	 * after the retransmission timer has been turned off.  Make sure
4646d90faf3SPaul Saab 	 * that the retransmission timer is set.
4656d90faf3SPaul Saab 	 */
4666d90faf3SPaul Saab 	if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) &&
4676d90faf3SPaul Saab 	    !callout_active(tp->tt_rexmt) &&
4686d90faf3SPaul Saab 	    !callout_active(tp->tt_persist)) {
4696d90faf3SPaul Saab 		callout_reset(tp->tt_rexmt, tp->t_rxtcur,
4706d90faf3SPaul Saab 			      tcp_timer_rexmt, tp);
471cf2942b6SRobert Watson 		goto just_return;
4726d90faf3SPaul Saab 	}
473df8bae1dSRodney W. Grimes 	/*
474df8bae1dSRodney W. Grimes 	 * TCP window updates are not reliable, rather a polling protocol
475df8bae1dSRodney W. Grimes 	 * using ``persist'' packets is used to insure receipt of window
476df8bae1dSRodney W. Grimes 	 * updates.  The three ``states'' for the output side are:
477df8bae1dSRodney W. Grimes 	 *	idle			not doing retransmits or persists
478df8bae1dSRodney W. Grimes 	 *	persisting		to move a small or zero window
479df8bae1dSRodney W. Grimes 	 *	(re)transmitting	and thereby not persisting
480df8bae1dSRodney W. Grimes 	 *
4819b8b58e0SJonathan Lemon 	 * callout_active(tp->tt_persist)
4829b8b58e0SJonathan Lemon 	 *	is true when we are in persist state.
483df8bae1dSRodney W. Grimes 	 * tp->t_force
484df8bae1dSRodney W. Grimes 	 *	is set when we are called to send a persist packet.
4859b8b58e0SJonathan Lemon 	 * callout_active(tp->tt_rexmt)
486df8bae1dSRodney W. Grimes 	 *	is set when we are retransmitting
487df8bae1dSRodney W. Grimes 	 * The output side is idle when both timers are zero.
488df8bae1dSRodney W. Grimes 	 *
489df8bae1dSRodney W. Grimes 	 * If send window is too small, there is data to transmit, and no
490df8bae1dSRodney W. Grimes 	 * retransmit or persist is pending, then go to persist state.
491df8bae1dSRodney W. Grimes 	 * If nothing happens soon, send when timer expires:
492df8bae1dSRodney W. Grimes 	 * if window is nonzero, transmit what we can,
493df8bae1dSRodney W. Grimes 	 * otherwise force out a byte.
494df8bae1dSRodney W. Grimes 	 */
4959b8b58e0SJonathan Lemon 	if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) &&
4969b8b58e0SJonathan Lemon 	    !callout_active(tp->tt_persist)) {
497df8bae1dSRodney W. Grimes 		tp->t_rxtshift = 0;
498df8bae1dSRodney W. Grimes 		tcp_setpersist(tp);
499df8bae1dSRodney W. Grimes 	}
500df8bae1dSRodney W. Grimes 
501df8bae1dSRodney W. Grimes 	/*
502df8bae1dSRodney W. Grimes 	 * No reason to send a segment, just return.
503df8bae1dSRodney W. Grimes 	 */
504cf2942b6SRobert Watson just_return:
505cf2942b6SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
506df8bae1dSRodney W. Grimes 	return (0);
507df8bae1dSRodney W. Grimes 
508df8bae1dSRodney W. Grimes send:
509cf2942b6SRobert Watson 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
510df8bae1dSRodney W. Grimes 	/*
511df8bae1dSRodney W. Grimes 	 * Before ESTABLISHED, force sending of initial options
512df8bae1dSRodney W. Grimes 	 * unless TCP set not to do any options.
513df8bae1dSRodney W. Grimes 	 * NOTE: we assume that the IP/TCP header plus TCP options
514df8bae1dSRodney W. Grimes 	 * always fit in a single mbuf, leaving room for a maximum
515df8bae1dSRodney W. Grimes 	 * link header, i.e.
51633841545SHajimu UMEMOTO 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
517df8bae1dSRodney W. Grimes 	 */
518df8bae1dSRodney W. Grimes 	optlen = 0;
519fb59c426SYoshinobu Inoue #ifdef INET6
520fb59c426SYoshinobu Inoue 	if (isipv6)
521fb59c426SYoshinobu Inoue 		hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
522fb59c426SYoshinobu Inoue 	else
523fb59c426SYoshinobu Inoue #endif
524df8bae1dSRodney W. Grimes 	hdrlen = sizeof (struct tcpiphdr);
525df8bae1dSRodney W. Grimes 	if (flags & TH_SYN) {
526df8bae1dSRodney W. Grimes 		tp->snd_nxt = tp->iss;
527df8bae1dSRodney W. Grimes 		if ((tp->t_flags & TF_NOOPT) == 0) {
528df8bae1dSRodney W. Grimes 			u_short mss;
529df8bae1dSRodney W. Grimes 
530df8bae1dSRodney W. Grimes 			opt[0] = TCPOPT_MAXSEG;
531a0292f23SGarrett Wollman 			opt[1] = TCPOLEN_MAXSEG;
53297d8d152SAndre Oppermann 			mss = htons((u_short) tcp_mssopt(&tp->t_inpcb->inp_inc));
53394a5d9b6SDavid Greenman 			(void)memcpy(opt + 2, &mss, sizeof(mss));
534a0292f23SGarrett Wollman 			optlen = TCPOLEN_MAXSEG;
535df8bae1dSRodney W. Grimes 
5366d90faf3SPaul Saab 			/*
5376d90faf3SPaul Saab 			 * If this is the first SYN of connection (not a SYN
5386d90faf3SPaul Saab 			 * ACK), include SACK_PERMIT_HDR option.  If this is a
5396d90faf3SPaul Saab 			 * SYN ACK, include SACK_PERMIT_HDR option if peer has
5406d90faf3SPaul Saab 			 * already done so. This is only for active connect,
5416d90faf3SPaul Saab 			 * since the syncache takes care of the passive connect.
5426d90faf3SPaul Saab 			 */
5436d90faf3SPaul Saab 			if (tp->sack_enable && ((flags & TH_ACK) == 0 ||
5446d90faf3SPaul Saab 			    (tp->t_flags & TF_SACK_PERMIT))) {
5456d90faf3SPaul Saab 				*((u_int32_t *) (opt + optlen)) =
5466d90faf3SPaul Saab 					htonl(TCPOPT_SACK_PERMIT_HDR);
5476d90faf3SPaul Saab 				optlen += 4;
5486d90faf3SPaul Saab 			}
549df8bae1dSRodney W. Grimes 			if ((tp->t_flags & TF_REQ_SCALE) &&
550df8bae1dSRodney W. Grimes 			    ((flags & TH_ACK) == 0 ||
551df8bae1dSRodney W. Grimes 			    (tp->t_flags & TF_RCVD_SCALE))) {
5529105bb46SBruce Evans 				*((u_int32_t *)(opt + optlen)) = htonl(
553df8bae1dSRodney W. Grimes 					TCPOPT_NOP << 24 |
554df8bae1dSRodney W. Grimes 					TCPOPT_WINDOW << 16 |
555df8bae1dSRodney W. Grimes 					TCPOLEN_WINDOW << 8 |
556df8bae1dSRodney W. Grimes 					tp->request_r_scale);
557df8bae1dSRodney W. Grimes 				optlen += 4;
558df8bae1dSRodney W. Grimes 			}
559df8bae1dSRodney W. Grimes 		}
560df8bae1dSRodney W. Grimes 	}
561df8bae1dSRodney W. Grimes 
562df8bae1dSRodney W. Grimes 	/*
563df8bae1dSRodney W. Grimes 	 * Send a timestamp and echo-reply if this is a SYN and our side
564df8bae1dSRodney W. Grimes 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
565df8bae1dSRodney W. Grimes 	 * and our peer have sent timestamps in our SYN's.
566df8bae1dSRodney W. Grimes 	 */
567df8bae1dSRodney W. Grimes 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
568df8bae1dSRodney W. Grimes 	    (flags & TH_RST) == 0 &&
569a0292f23SGarrett Wollman 	    ((flags & TH_ACK) == 0 ||
570df8bae1dSRodney W. Grimes 	     (tp->t_flags & TF_RCVD_TSTMP))) {
5719105bb46SBruce Evans 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
572df8bae1dSRodney W. Grimes 
573df8bae1dSRodney W. Grimes 		/* Form timestamp option as shown in appendix A of RFC 1323. */
574df8bae1dSRodney W. Grimes 		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
5759b8b58e0SJonathan Lemon 		*lp++ = htonl(ticks);
576df8bae1dSRodney W. Grimes 		*lp   = htonl(tp->ts_recent);
577df8bae1dSRodney W. Grimes 		optlen += TCPOLEN_TSTAMP_APPA;
578df8bae1dSRodney W. Grimes 	}
579df8bae1dSRodney W. Grimes 
580a0292f23SGarrett Wollman 	/*
5816d90faf3SPaul Saab 	 * Send SACKs if necessary.  This should be the last option processed.
5826d90faf3SPaul Saab 	 * Only as many SACKs are sent as are permitted by the maximum options
5836d90faf3SPaul Saab 	 * size.  No more than three SACKs are sent.
5846d90faf3SPaul Saab 	 */
5856d90faf3SPaul Saab 	if (tp->sack_enable && tp->t_state == TCPS_ESTABLISHED &&
5866d90faf3SPaul Saab 	    (tp->t_flags & (TF_SACK_PERMIT|TF_NOOPT)) == TF_SACK_PERMIT &&
5876d90faf3SPaul Saab 	    tp->rcv_numsacks) {
5886d90faf3SPaul Saab 		u_int32_t *lp = (u_int32_t *)(opt + optlen);
5896d90faf3SPaul Saab 		u_int32_t *olp = lp++;
5906d90faf3SPaul Saab 		int count = 0;  /* actual number of SACKs inserted */
5916d90faf3SPaul Saab 		int maxsack = (MAX_TCPOPTLEN - (optlen + 4))/TCPOLEN_SACK;
5926d90faf3SPaul Saab 
5936d90faf3SPaul Saab 		tcpstat.tcps_sack_send_blocks++;
5946d90faf3SPaul Saab 		maxsack = min(maxsack, TCP_MAX_SACK);
5956d90faf3SPaul Saab 		for (i = 0; (i < tp->rcv_numsacks && count < maxsack); i++) {
5966d90faf3SPaul Saab 			struct sackblk sack = tp->sackblks[i];
5976d90faf3SPaul Saab 			if (sack.start == 0 && sack.end == 0)
5986d90faf3SPaul Saab 				continue;
5996d90faf3SPaul Saab 			*lp++ = htonl(sack.start);
6006d90faf3SPaul Saab 			*lp++ = htonl(sack.end);
6016d90faf3SPaul Saab 			count++;
6026d90faf3SPaul Saab 		}
6036d90faf3SPaul Saab 		*olp = htonl(TCPOPT_SACK_HDR|(TCPOLEN_SACK*count+2));
6046d90faf3SPaul Saab 		optlen += TCPOLEN_SACK*count + 4; /* including leading NOPs */
6056d90faf3SPaul Saab 	}
606a0292f23SGarrett Wollman 
6071cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
6081cfd4b53SBruce M Simpson #ifdef INET6
6091cfd4b53SBruce M Simpson 	if (!isipv6)
6101cfd4b53SBruce M Simpson #endif
6111cfd4b53SBruce M Simpson 	if (tp->t_flags & TF_SIGNATURE) {
6121cfd4b53SBruce M Simpson 		int i;
6131cfd4b53SBruce M Simpson 		u_char *bp;
614bca0e5bfSBruce M Simpson 
615bca0e5bfSBruce M Simpson 		/* Initialize TCP-MD5 option (RFC2385) */
6161cfd4b53SBruce M Simpson 		bp = (u_char *)opt + optlen;
6171cfd4b53SBruce M Simpson 		*bp++ = TCPOPT_SIGNATURE;
6181cfd4b53SBruce M Simpson 		*bp++ = TCPOLEN_SIGNATURE;
6191cfd4b53SBruce M Simpson 		sigoff = optlen + 2;
6201cfd4b53SBruce M Simpson 		for (i = 0; i < TCP_SIGLEN; i++)
6211cfd4b53SBruce M Simpson 			*bp++ = 0;
6221cfd4b53SBruce M Simpson 		optlen += TCPOLEN_SIGNATURE;
623bca0e5bfSBruce M Simpson 
624bca0e5bfSBruce M Simpson 		/* Terminate options list and maintain 32-bit alignment. */
6251cfd4b53SBruce M Simpson 		*bp++ = TCPOPT_NOP;
6261cfd4b53SBruce M Simpson 		*bp++ = TCPOPT_EOL;
6271cfd4b53SBruce M Simpson 		optlen += 2;
6281cfd4b53SBruce M Simpson 	}
6291cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
6301cfd4b53SBruce M Simpson 
631df8bae1dSRodney W. Grimes 	hdrlen += optlen;
632df8bae1dSRodney W. Grimes 
633fb59c426SYoshinobu Inoue #ifdef INET6
634fb59c426SYoshinobu Inoue 	if (isipv6)
635fb59c426SYoshinobu Inoue 		ipoptlen = ip6_optlen(tp->t_inpcb);
636fb59c426SYoshinobu Inoue 	else
637fb59c426SYoshinobu Inoue #endif
638f10e85d7SLuigi Rizzo 	if (tp->t_inpcb->inp_options)
639a04884fcSBill Fenner 		ipoptlen = tp->t_inpcb->inp_options->m_len -
640a04884fcSBill Fenner 				offsetof(struct ipoption, ipopt_list);
641f10e85d7SLuigi Rizzo 	else
642a04884fcSBill Fenner 		ipoptlen = 0;
643fb59c426SYoshinobu Inoue #ifdef IPSEC
644fb59c426SYoshinobu Inoue 	ipoptlen += ipsec_hdrsiz_tcp(tp);
645fb59c426SYoshinobu Inoue #endif
646a04884fcSBill Fenner 
647df8bae1dSRodney W. Grimes 	/*
648df8bae1dSRodney W. Grimes 	 * Adjust data length if insertion of options will
649a0292f23SGarrett Wollman 	 * bump the packet length beyond the t_maxopd length.
650a0292f23SGarrett Wollman 	 * Clear the FIN bit because we cut off the tail of
651a0292f23SGarrett Wollman 	 * the segment.
652df8bae1dSRodney W. Grimes 	 */
653a04884fcSBill Fenner 	if (len + optlen + ipoptlen > tp->t_maxopd) {
654297a37f3SDavid Greenman 		/*
655297a37f3SDavid Greenman 		 * If there is still more to send, don't close the connection.
656297a37f3SDavid Greenman 		 */
657297a37f3SDavid Greenman 		flags &= ~TH_FIN;
658a04884fcSBill Fenner 		len = tp->t_maxopd - optlen - ipoptlen;
659df8bae1dSRodney W. Grimes 		sendalot = 1;
660df8bae1dSRodney W. Grimes 	}
661df8bae1dSRodney W. Grimes 
662a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/
663a683a7ddSYoshinobu Inoue #ifdef INET6
664a683a7ddSYoshinobu Inoue 	if (max_linkhdr + hdrlen > MCLBYTES)
665a683a7ddSYoshinobu Inoue #else
666df8bae1dSRodney W. Grimes 	if (max_linkhdr + hdrlen > MHLEN)
667a683a7ddSYoshinobu Inoue #endif
668f10e85d7SLuigi Rizzo 		panic("tcphdr too big");
669a0292f23SGarrett Wollman /*#endif*/
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 	/*
672df8bae1dSRodney W. Grimes 	 * Grab a header mbuf, attaching a copy of data to
673df8bae1dSRodney W. Grimes 	 * be transmitted, and initialize the header from
674df8bae1dSRodney W. Grimes 	 * the template for sends on this connection.
675df8bae1dSRodney W. Grimes 	 */
676df8bae1dSRodney W. Grimes 	if (len) {
677df8bae1dSRodney W. Grimes 		if (tp->t_force && len == 1)
678df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndprobe++;
679df8bae1dSRodney W. Grimes 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
680df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitpack++;
681df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitbyte += len;
682df8bae1dSRodney W. Grimes 		} else {
683df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndpack++;
684df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndbyte += len;
685df8bae1dSRodney W. Grimes 		}
686df8bae1dSRodney W. Grimes #ifdef notyet
687df8bae1dSRodney W. Grimes 		if ((m = m_copypack(so->so_snd.sb_mb, off,
688df8bae1dSRodney W. Grimes 		    (int)len, max_linkhdr + hdrlen)) == 0) {
689cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
690df8bae1dSRodney W. Grimes 			error = ENOBUFS;
691df8bae1dSRodney W. Grimes 			goto out;
692df8bae1dSRodney W. Grimes 		}
693df8bae1dSRodney W. Grimes 		/*
694df8bae1dSRodney W. Grimes 		 * m_copypack left space for our hdr; use it.
695df8bae1dSRodney W. Grimes 		 */
696df8bae1dSRodney W. Grimes 		m->m_len += hdrlen;
697df8bae1dSRodney W. Grimes 		m->m_data -= hdrlen;
698df8bae1dSRodney W. Grimes #else
699a163d034SWarner Losh 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
700df8bae1dSRodney W. Grimes 		if (m == NULL) {
701cf2942b6SRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
702df8bae1dSRodney W. Grimes 			error = ENOBUFS;
703df8bae1dSRodney W. Grimes 			goto out;
704df8bae1dSRodney W. Grimes 		}
705fb59c426SYoshinobu Inoue #ifdef INET6
706a683a7ddSYoshinobu Inoue 		if (MHLEN < hdrlen + max_linkhdr) {
707a163d034SWarner Losh 			MCLGET(m, M_DONTWAIT);
708a683a7ddSYoshinobu Inoue 			if ((m->m_flags & M_EXT) == 0) {
709cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
710a683a7ddSYoshinobu Inoue 				m_freem(m);
711a683a7ddSYoshinobu Inoue 				error = ENOBUFS;
712a683a7ddSYoshinobu Inoue 				goto out;
713a683a7ddSYoshinobu Inoue 			}
714a683a7ddSYoshinobu Inoue 		}
715fb59c426SYoshinobu Inoue #endif
716df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
717df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
718df8bae1dSRodney W. Grimes 		if (len <= MHLEN - hdrlen - max_linkhdr) {
719df8bae1dSRodney W. Grimes 			m_copydata(so->so_snd.sb_mb, off, (int) len,
720df8bae1dSRodney W. Grimes 			    mtod(m, caddr_t) + hdrlen);
721df8bae1dSRodney W. Grimes 			m->m_len += len;
722df8bae1dSRodney W. Grimes 		} else {
723df8bae1dSRodney W. Grimes 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
72451823c3aSGarrett Wollman 			if (m->m_next == 0) {
725cf2942b6SRobert Watson 				SOCKBUF_UNLOCK(&so->so_snd);
726d7f570e6SGarrett Wollman 				(void) m_free(m);
72751823c3aSGarrett Wollman 				error = ENOBUFS;
72851823c3aSGarrett Wollman 				goto out;
72951823c3aSGarrett Wollman 			}
730df8bae1dSRodney W. Grimes 		}
731df8bae1dSRodney W. Grimes #endif
732df8bae1dSRodney W. Grimes 		/*
733df8bae1dSRodney W. Grimes 		 * If we're sending everything we've got, set PUSH.
734df8bae1dSRodney W. Grimes 		 * (This will keep happy those implementations which only
735df8bae1dSRodney W. Grimes 		 * give data to the user when a buffer fills or
736df8bae1dSRodney W. Grimes 		 * a PUSH comes in.)
737df8bae1dSRodney W. Grimes 		 */
738df8bae1dSRodney W. Grimes 		if (off + len == so->so_snd.sb_cc)
739df8bae1dSRodney W. Grimes 			flags |= TH_PUSH;
740cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
741df8bae1dSRodney W. Grimes 	} else {
742cf2942b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
743df8bae1dSRodney W. Grimes 		if (tp->t_flags & TF_ACKNOW)
744df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndacks++;
745df8bae1dSRodney W. Grimes 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
746df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndctrl++;
747df8bae1dSRodney W. Grimes 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
748df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndurg++;
749df8bae1dSRodney W. Grimes 		else
750df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndwinup++;
751df8bae1dSRodney W. Grimes 
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
758fb59c426SYoshinobu Inoue 		if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
759fb59c426SYoshinobu Inoue 		    MHLEN >= hdrlen) {
760fb59c426SYoshinobu Inoue 			MH_ALIGN(m, hdrlen);
761fb59c426SYoshinobu Inoue 		} else
762fb59c426SYoshinobu Inoue #endif
763df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
764df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
765df8bae1dSRodney W. Grimes 	}
766cf2942b6SRobert Watson 	SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
767df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = (struct ifnet *)0;
768c488362eSRobert Watson #ifdef MAC
769c18b97c6SRobert Watson 	mac_create_mbuf_from_inpcb(tp->t_inpcb, m);
770c488362eSRobert Watson #endif
771fb59c426SYoshinobu Inoue #ifdef INET6
772fb59c426SYoshinobu Inoue 	if (isipv6) {
773fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
774fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip6 + 1);
77579909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip6, th);
776fb59c426SYoshinobu Inoue 	} else
777fb59c426SYoshinobu Inoue #endif /* INET6 */
778fb59c426SYoshinobu Inoue 	{
779fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
780fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
781fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)(ip + 1);
78279909384SJonathan Lemon 		tcpip_fillheaders(tp->t_inpcb, ip, th);
783fb59c426SYoshinobu Inoue 	}
784df8bae1dSRodney W. Grimes 
785df8bae1dSRodney W. Grimes 	/*
786df8bae1dSRodney W. Grimes 	 * Fill in fields, remembering maximum advertised
787df8bae1dSRodney W. Grimes 	 * window for use in delaying messages about window sizes.
788df8bae1dSRodney W. Grimes 	 * If resending a FIN, be sure not to use a new sequence number.
789df8bae1dSRodney W. Grimes 	 */
790df8bae1dSRodney W. Grimes 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
791df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max)
792df8bae1dSRodney W. Grimes 		tp->snd_nxt--;
793df8bae1dSRodney W. Grimes 	/*
794df8bae1dSRodney W. Grimes 	 * If we are doing retransmissions, then snd_nxt will
795df8bae1dSRodney W. Grimes 	 * not reflect the first unsent octet.  For ACK only
796df8bae1dSRodney W. Grimes 	 * packets, we do not want the sequence number of the
797df8bae1dSRodney W. Grimes 	 * retransmitted packet, we want the sequence number
798df8bae1dSRodney W. Grimes 	 * of the next unsent octet.  So, if there is no data
799df8bae1dSRodney W. Grimes 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
800df8bae1dSRodney W. Grimes 	 * when filling in ti_seq.  But if we are in persist
801df8bae1dSRodney W. Grimes 	 * state, snd_max might reflect one byte beyond the
802df8bae1dSRodney W. Grimes 	 * right edge of the window, so use snd_nxt in that
803df8bae1dSRodney W. Grimes 	 * case, since we know we aren't doing a retransmission.
804df8bae1dSRodney W. Grimes 	 * (retransmit and persist are mutually exclusive...)
805df8bae1dSRodney W. Grimes 	 */
806a55db2b6SPaul Saab 	if (sack_rxmit == 0) {
8079b8b58e0SJonathan Lemon 		if (len || (flags & (TH_SYN|TH_FIN))
8089b8b58e0SJonathan Lemon 		    || callout_active(tp->tt_persist))
809fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_nxt);
810df8bae1dSRodney W. Grimes 		else
811fb59c426SYoshinobu Inoue 			th->th_seq = htonl(tp->snd_max);
812a55db2b6SPaul Saab 	} else {
8136d90faf3SPaul Saab 		th->th_seq = htonl(p->rxmit);
8146d90faf3SPaul Saab 		p->rxmit += len;
8156d90faf3SPaul Saab 	}
816fb59c426SYoshinobu Inoue 	th->th_ack = htonl(tp->rcv_nxt);
817df8bae1dSRodney W. Grimes 	if (optlen) {
818fb59c426SYoshinobu Inoue 		bcopy(opt, th + 1, optlen);
819fb59c426SYoshinobu Inoue 		th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
820df8bae1dSRodney W. Grimes 	}
821fb59c426SYoshinobu Inoue 	th->th_flags = flags;
822df8bae1dSRodney W. Grimes 	/*
823df8bae1dSRodney W. Grimes 	 * Calculate receive window.  Don't shrink window,
824df8bae1dSRodney W. Grimes 	 * but avoid silly window syndrome.
825df8bae1dSRodney W. Grimes 	 */
826201d185bSAndre Oppermann 	if (recwin < (long)(so->so_rcv.sb_hiwat / 4) &&
827201d185bSAndre Oppermann 	    recwin < (long)tp->t_maxseg)
828201d185bSAndre Oppermann 		recwin = 0;
829201d185bSAndre Oppermann 	if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
830201d185bSAndre Oppermann 		recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
831201d185bSAndre Oppermann 	if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
832201d185bSAndre Oppermann 		recwin = (long)TCP_MAXWIN << tp->rcv_scale;
833201d185bSAndre Oppermann 	th->th_win = htons((u_short) (recwin >> tp->rcv_scale));
834262c1c1aSMatthew Dillon 
835262c1c1aSMatthew Dillon 
836262c1c1aSMatthew Dillon 	/*
837262c1c1aSMatthew Dillon 	 * Adjust the RXWIN0SENT flag - indicate that we have advertised
838262c1c1aSMatthew Dillon 	 * a 0 window.  This may cause the remote transmitter to stall.  This
839262c1c1aSMatthew Dillon 	 * flag tells soreceive() to disable delayed acknowledgements when
840262c1c1aSMatthew Dillon 	 * draining the buffer.  This can occur if the receiver is attempting
841262c1c1aSMatthew Dillon 	 * to read more data then can be buffered prior to transmitting on
842262c1c1aSMatthew Dillon 	 * the connection.
843262c1c1aSMatthew Dillon 	 */
844201d185bSAndre Oppermann 	if (recwin == 0)
845262c1c1aSMatthew Dillon 		tp->t_flags |= TF_RXWIN0SENT;
846262c1c1aSMatthew Dillon 	else
847262c1c1aSMatthew Dillon 		tp->t_flags &= ~TF_RXWIN0SENT;
848df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
849fb59c426SYoshinobu Inoue 		th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
850fb59c426SYoshinobu Inoue 		th->th_flags |= TH_URG;
851df8bae1dSRodney W. Grimes 	} else
852df8bae1dSRodney W. Grimes 		/*
853df8bae1dSRodney W. Grimes 		 * If no urgent pointer to send, then we pull
854df8bae1dSRodney W. Grimes 		 * the urgent pointer to the left edge of the send window
855df8bae1dSRodney W. Grimes 		 * so that it doesn't drift into the send window on sequence
856df8bae1dSRodney W. Grimes 		 * number wraparound.
857df8bae1dSRodney W. Grimes 		 */
858df8bae1dSRodney W. Grimes 		tp->snd_up = tp->snd_una;		/* drag it along */
859df8bae1dSRodney W. Grimes 
8601cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
8611cfd4b53SBruce M Simpson #ifdef INET6
8621cfd4b53SBruce M Simpson 	if (!isipv6)
8631cfd4b53SBruce M Simpson #endif
8641cfd4b53SBruce M Simpson 	if (tp->t_flags & TF_SIGNATURE)
865265ed012SBruce M Simpson 		tcp_signature_compute(m, sizeof(struct ip), len, optlen,
8661cfd4b53SBruce M Simpson 		    (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND);
867265ed012SBruce M Simpson #endif
8681cfd4b53SBruce M Simpson 
869df8bae1dSRodney W. Grimes 	/*
870df8bae1dSRodney W. Grimes 	 * Put TCP length in extended header, and then
871df8bae1dSRodney W. Grimes 	 * checksum extended header and data.
872df8bae1dSRodney W. Grimes 	 */
873fb59c426SYoshinobu Inoue 	m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
874fb59c426SYoshinobu Inoue #ifdef INET6
875fb59c426SYoshinobu Inoue 	if (isipv6)
876fb59c426SYoshinobu Inoue 		/*
877fb59c426SYoshinobu Inoue 		 * ip6_plen is not need to be filled now, and will be filled
878fb59c426SYoshinobu Inoue 		 * in ip6_output.
879fb59c426SYoshinobu Inoue 		 */
880fb59c426SYoshinobu Inoue 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
881fb59c426SYoshinobu Inoue 				       sizeof(struct tcphdr) + optlen + len);
882fb59c426SYoshinobu Inoue 	else
883fb59c426SYoshinobu Inoue #endif /* INET6 */
884fb59c426SYoshinobu Inoue 	{
885db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_TCP;
886db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
88779909384SJonathan Lemon 		th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
88879909384SJonathan Lemon 		    htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen));
889fb59c426SYoshinobu Inoue 
890db4f9cc7SJonathan Lemon 		/* IP version must be set here for ipv4/ipv6 checking later */
891db4f9cc7SJonathan Lemon 		KASSERT(ip->ip_v == IPVERSION,
8926e551fb6SDavid E. O'Brien 		    ("%s: IP version incorrect: %d", __func__, ip->ip_v));
893fb59c426SYoshinobu Inoue 	}
894df8bae1dSRodney W. Grimes 
895df8bae1dSRodney W. Grimes 	/*
896df8bae1dSRodney W. Grimes 	 * In transmit state, time the transmission and arrange for
897df8bae1dSRodney W. Grimes 	 * the retransmit.  In persist state, just set snd_max.
898df8bae1dSRodney W. Grimes 	 */
8999b8b58e0SJonathan Lemon 	if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
900df8bae1dSRodney W. Grimes 		tcp_seq startseq = tp->snd_nxt;
901df8bae1dSRodney W. Grimes 
902df8bae1dSRodney W. Grimes 		/*
903df8bae1dSRodney W. Grimes 		 * Advance snd_nxt over sequence space of this segment.
904df8bae1dSRodney W. Grimes 		 */
905df8bae1dSRodney W. Grimes 		if (flags & (TH_SYN|TH_FIN)) {
906df8bae1dSRodney W. Grimes 			if (flags & TH_SYN)
907df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
908df8bae1dSRodney W. Grimes 			if (flags & TH_FIN) {
909df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
910df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_SENTFIN;
911df8bae1dSRodney W. Grimes 			}
912df8bae1dSRodney W. Grimes 		}
913a55db2b6SPaul Saab 		if (sack_rxmit)
9146d90faf3SPaul Saab 			goto timer;
915df8bae1dSRodney W. Grimes 		tp->snd_nxt += len;
916df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
917df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt;
918df8bae1dSRodney W. Grimes 			/*
919df8bae1dSRodney W. Grimes 			 * Time this transmission if not a retransmission and
920df8bae1dSRodney W. Grimes 			 * not currently timing anything.
921df8bae1dSRodney W. Grimes 			 */
9229b8b58e0SJonathan Lemon 			if (tp->t_rtttime == 0) {
9239b8b58e0SJonathan Lemon 				tp->t_rtttime = ticks;
924df8bae1dSRodney W. Grimes 				tp->t_rtseq = startseq;
925df8bae1dSRodney W. Grimes 				tcpstat.tcps_segstimed++;
926df8bae1dSRodney W. Grimes 			}
927df8bae1dSRodney W. Grimes 		}
928df8bae1dSRodney W. Grimes 
929df8bae1dSRodney W. Grimes 		/*
930df8bae1dSRodney W. Grimes 		 * Set retransmit timer if not currently set,
93128257b5cSMatthew Dillon 		 * and not doing a pure ack or a keep-alive probe.
932df8bae1dSRodney W. Grimes 		 * Initial value for retransmit timer is smoothed
933df8bae1dSRodney W. Grimes 		 * round-trip time + 2 * round-trip time variance.
934df8bae1dSRodney W. Grimes 		 * Initialize shift counter which is used for backoff
935df8bae1dSRodney W. Grimes 		 * of retransmit time.
936df8bae1dSRodney W. Grimes 		 */
9376d90faf3SPaul Saab timer:
9389b8b58e0SJonathan Lemon 		if (!callout_active(tp->tt_rexmt) &&
939a55db2b6SPaul Saab 		    ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
940a55db2b6SPaul Saab 		     (tp->snd_nxt != tp->snd_una))) {
9419b8b58e0SJonathan Lemon 			if (callout_active(tp->tt_persist)) {
9429b8b58e0SJonathan Lemon 				callout_stop(tp->tt_persist);
943df8bae1dSRodney W. Grimes 				tp->t_rxtshift = 0;
944df8bae1dSRodney W. Grimes 			}
94546f58482SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
94646f58482SJonathan Lemon 				      tcp_timer_rexmt, tp);
947df8bae1dSRodney W. Grimes 		}
94828257b5cSMatthew Dillon 	} else {
94928257b5cSMatthew Dillon 		/*
95028257b5cSMatthew Dillon 		 * Persist case, update snd_max but since we are in
95128257b5cSMatthew Dillon 		 * persist mode (no window) we do not update snd_nxt.
95228257b5cSMatthew Dillon 		 */
95328257b5cSMatthew Dillon 		int xlen = len;
95428257b5cSMatthew Dillon 		if (flags & TH_SYN)
95528257b5cSMatthew Dillon 			++xlen;
95628257b5cSMatthew Dillon 		if (flags & TH_FIN) {
95728257b5cSMatthew Dillon 			++xlen;
95828257b5cSMatthew Dillon 			tp->t_flags |= TF_SENTFIN;
95928257b5cSMatthew Dillon 		}
960abac41a6SMatthew Dillon 		if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
961df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt + len;
96228257b5cSMatthew Dillon 	}
963df8bae1dSRodney W. Grimes 
964610ee2f9SDavid Greenman #ifdef TCPDEBUG
965df8bae1dSRodney W. Grimes 	/*
966df8bae1dSRodney W. Grimes 	 * Trace.
967df8bae1dSRodney W. Grimes 	 */
96891f467d5SHartmut Brandt 	if (so->so_options & SO_DEBUG) {
969f3e0b7efSBruce M Simpson 		u_short save = 0;
9705214cb3fSBruce M Simpson #ifdef INET6
9715214cb3fSBruce M Simpson 		if (!isipv6)
9725214cb3fSBruce M Simpson #endif
9735214cb3fSBruce M Simpson 		{
9745214cb3fSBruce M Simpson 			save = ipov->ih_len;
97591f467d5SHartmut Brandt 			ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */);
9765214cb3fSBruce M Simpson 		}
977fb59c426SYoshinobu Inoue 		tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
9785214cb3fSBruce M Simpson #ifdef INET6
9795214cb3fSBruce M Simpson 		if (!isipv6)
9805214cb3fSBruce M Simpson #endif
98191f467d5SHartmut Brandt 		ipov->ih_len = save;
98291f467d5SHartmut Brandt 	}
983610ee2f9SDavid Greenman #endif
984df8bae1dSRodney W. Grimes 
985df8bae1dSRodney W. Grimes 	/*
986df8bae1dSRodney W. Grimes 	 * Fill in IP length and desired time to live and
987df8bae1dSRodney W. Grimes 	 * send to IP level.  There should be a better way
988df8bae1dSRodney W. Grimes 	 * to handle ttl and tos; we could keep them in
989df8bae1dSRodney W. Grimes 	 * the template, but need a way to checksum without them.
990df8bae1dSRodney W. Grimes 	 */
991fb59c426SYoshinobu Inoue 	/*
992fb59c426SYoshinobu Inoue 	 * m->m_pkthdr.len should have been set before cksum calcuration,
993fb59c426SYoshinobu Inoue 	 * because in6_cksum() need it.
994fb59c426SYoshinobu Inoue 	 */
995fb59c426SYoshinobu Inoue #ifdef INET6
996fb59c426SYoshinobu Inoue 	if (isipv6) {
997fb59c426SYoshinobu Inoue 		/*
998fb59c426SYoshinobu Inoue 		 * we separately set hoplimit for every segment, since the
999fb59c426SYoshinobu Inoue 		 * user might want to change the value via setsockopt.
1000fb59c426SYoshinobu Inoue 		 * Also, desired default hop limit might be changed via
1001fb59c426SYoshinobu Inoue 		 * Neighbor Discovery.
1002fb59c426SYoshinobu Inoue 		 */
100397d8d152SAndre Oppermann 		ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL);
1004fb59c426SYoshinobu Inoue 
1005fb59c426SYoshinobu Inoue 		/* TODO: IPv6 IP6TOS_ECT bit on */
1006fb59c426SYoshinobu Inoue 		error = ip6_output(m,
100797d8d152SAndre Oppermann 			    tp->t_inpcb->in6p_outputopts, NULL,
1008b5d47ff5SJohn-Mark Gurney 			    ((so->so_options & SO_DONTROUTE) ?
1009b5d47ff5SJohn-Mark Gurney 			    IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb);
1010fb59c426SYoshinobu Inoue 	} else
1011fb59c426SYoshinobu Inoue #endif /* INET6 */
1012df8bae1dSRodney W. Grimes     {
1013fb59c426SYoshinobu Inoue 	ip->ip_len = m->m_pkthdr.len;
1014686cdd19SJun-ichiro itojun Hagino #ifdef INET6
1015686cdd19SJun-ichiro itojun Hagino 	if (INP_CHECK_SOCKAF(so, AF_INET6))
101697d8d152SAndre Oppermann 		ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL);
1017686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */
1018f138387aSGarrett Wollman 	/*
101997d8d152SAndre Oppermann 	 * If we do path MTU discovery, then we set DF on every packet.
102097d8d152SAndre Oppermann 	 * This might not be the best thing to do according to RFC3390
102197d8d152SAndre Oppermann 	 * Section 2. However the tcp hostcache migitates the problem
102297d8d152SAndre Oppermann 	 * so it affects only the first tcp connection with a host.
1023f138387aSGarrett Wollman 	 */
102497d8d152SAndre Oppermann 	if (path_mtu_discovery)
1025fb59c426SYoshinobu Inoue 		ip->ip_off |= IP_DF;
102697d8d152SAndre Oppermann 
102797d8d152SAndre Oppermann 	error = ip_output(m, tp->t_inpcb->inp_options, NULL,
1028b5d47ff5SJohn-Mark Gurney 	    ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0,
1029b5d47ff5SJohn-Mark Gurney 	    tp->t_inpcb);
1030df8bae1dSRodney W. Grimes     }
1031df8bae1dSRodney W. Grimes 	if (error) {
10327734ea06SArchie Cobbs 
10337734ea06SArchie Cobbs 		/*
10347734ea06SArchie Cobbs 		 * We know that the packet was lost, so back out the
10357734ea06SArchie Cobbs 		 * sequence number advance, if any.
10367734ea06SArchie Cobbs 		 */
10377734ea06SArchie Cobbs 		if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
10387734ea06SArchie Cobbs 			/*
10397734ea06SArchie Cobbs 			 * No need to check for TH_FIN here because
10407734ea06SArchie Cobbs 			 * the TF_SENTFIN flag handles that case.
10417734ea06SArchie Cobbs 			 */
10425d3b1b75SJayanth Vijayaraghavan 			if ((flags & TH_SYN) == 0) {
10435d3b1b75SJayanth Vijayaraghavan 				if (sack_rxmit)
10445d3b1b75SJayanth Vijayaraghavan 					p->rxmit -= len;
10455d3b1b75SJayanth Vijayaraghavan 				else
10467734ea06SArchie Cobbs 					tp->snd_nxt -= len;
10477734ea06SArchie Cobbs 			}
10485d3b1b75SJayanth Vijayaraghavan 		}
10497734ea06SArchie Cobbs 
1050df8bae1dSRodney W. Grimes out:
1051cf2942b6SRobert Watson 		SOCKBUF_UNLOCK_ASSERT(&so->so_snd);	/* Check gotos. */
1052df8bae1dSRodney W. Grimes 		if (error == ENOBUFS) {
105359f577adSJonathan Lemon 	                if (!callout_active(tp->tt_rexmt) &&
105459f577adSJonathan Lemon 			    !callout_active(tp->tt_persist))
105559f577adSJonathan Lemon 	                        callout_reset(tp->tt_rexmt, tp->t_rxtcur,
105659f577adSJonathan Lemon 				    tcp_timer_rexmt, tp);
1057df8bae1dSRodney W. Grimes 			tcp_quench(tp->t_inpcb, 0);
1058df8bae1dSRodney W. Grimes 			return (0);
1059df8bae1dSRodney W. Grimes 		}
10603d1f141bSGarrett Wollman 		if (error == EMSGSIZE) {
10613d1f141bSGarrett Wollman 			/*
10623d1f141bSGarrett Wollman 			 * ip_output() will have already fixed the route
10633d1f141bSGarrett Wollman 			 * for us.  tcp_mtudisc() will, as its last action,
10643d1f141bSGarrett Wollman 			 * initiate retransmission, so it is important to
10653d1f141bSGarrett Wollman 			 * not do so here.
10663d1f141bSGarrett Wollman 			 */
10673d1f141bSGarrett Wollman 			tcp_mtudisc(tp->t_inpcb, 0);
10683d1f141bSGarrett Wollman 			return 0;
10693d1f141bSGarrett Wollman 		}
1070df8bae1dSRodney W. Grimes 		if ((error == EHOSTUNREACH || error == ENETDOWN)
1071df8bae1dSRodney W. Grimes 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
1072df8bae1dSRodney W. Grimes 			tp->t_softerror = error;
1073df8bae1dSRodney W. Grimes 			return (0);
1074df8bae1dSRodney W. Grimes 		}
1075df8bae1dSRodney W. Grimes 		return (error);
1076df8bae1dSRodney W. Grimes 	}
1077df8bae1dSRodney W. Grimes 	tcpstat.tcps_sndtotal++;
1078df8bae1dSRodney W. Grimes 
1079df8bae1dSRodney W. Grimes 	/*
1080df8bae1dSRodney W. Grimes 	 * Data sent (as far as we can tell).
1081df8bae1dSRodney W. Grimes 	 * If this advertises a larger window than any other segment,
1082df8bae1dSRodney W. Grimes 	 * then remember the size of the advertised window.
1083df8bae1dSRodney W. Grimes 	 * Any pending ACK has now been sent.
1084df8bae1dSRodney W. Grimes 	 */
1085201d185bSAndre Oppermann 	if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv))
1086201d185bSAndre Oppermann 		tp->rcv_adv = tp->rcv_nxt + recwin;
1087df8bae1dSRodney W. Grimes 	tp->last_ack_sent = tp->rcv_nxt;
10883bfd6421SJonathan Lemon 	tp->t_flags &= ~(TF_ACKNOW | TF_DELACK);
10893bfd6421SJonathan Lemon 	if (callout_active(tp->tt_delack))
10909b8b58e0SJonathan Lemon 		callout_stop(tp->tt_delack);
1091d912c694SMatthew Dillon #if 0
1092d912c694SMatthew Dillon 	/*
1093d912c694SMatthew Dillon 	 * This completely breaks TCP if newreno is turned on.  What happens
1094d912c694SMatthew Dillon 	 * is that if delayed-acks are turned on on the receiver, this code
1095d912c694SMatthew Dillon 	 * on the transmitter effectively destroys the TCP window, forcing
1096d912c694SMatthew Dillon 	 * it to four packets (1.5Kx4 = 6K window).
1097d912c694SMatthew Dillon 	 */
109846f58482SJonathan Lemon 	if (sendalot && (!tcp_do_newreno || --maxburst))
1099df8bae1dSRodney W. Grimes 		goto again;
1100d912c694SMatthew Dillon #endif
1101d912c694SMatthew Dillon 	if (sendalot)
1102d912c694SMatthew Dillon 		goto again;
1103df8bae1dSRodney W. Grimes 	return (0);
1104df8bae1dSRodney W. Grimes }
1105df8bae1dSRodney W. Grimes 
1106df8bae1dSRodney W. Grimes void
1107df8bae1dSRodney W. Grimes tcp_setpersist(tp)
1108df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1109df8bae1dSRodney W. Grimes {
11109b8b58e0SJonathan Lemon 	int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
11119b8b58e0SJonathan Lemon 	int tt;
1112df8bae1dSRodney W. Grimes 
11139b8b58e0SJonathan Lemon 	if (callout_active(tp->tt_rexmt))
11149b8b58e0SJonathan Lemon 		panic("tcp_setpersist: retransmit pending");
1115df8bae1dSRodney W. Grimes 	/*
1116df8bae1dSRodney W. Grimes 	 * Start/restart persistance timer.
1117df8bae1dSRodney W. Grimes 	 */
11189b8b58e0SJonathan Lemon 	TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
1119df8bae1dSRodney W. Grimes 		      TCPTV_PERSMIN, TCPTV_PERSMAX);
11209b8b58e0SJonathan Lemon 	callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp);
1121df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1122df8bae1dSRodney W. Grimes 		tp->t_rxtshift++;
1123df8bae1dSRodney W. Grimes }
1124