xref: /freebsd/sys/netinet/tcp_output.c (revision 3d1f141b2394649c285cb064a4f8a858f407b78d)
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33d7f570e6SGarrett Wollman  *	@(#)tcp_output.c	8.4 (Berkeley) 5/24/95
343d1f141bSGarrett Wollman  *	$Id: tcp_output.c,v 1.14 1995/09/22 20:05:58 wollman Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39df8bae1dSRodney W. Grimes #include <sys/malloc.h>
40df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
41df8bae1dSRodney W. Grimes #include <sys/protosw.h>
42df8bae1dSRodney W. Grimes #include <sys/socket.h>
43df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
44df8bae1dSRodney W. Grimes #include <sys/errno.h>
4515bd2b43SDavid Greenman #include <sys/queue.h>
46df8bae1dSRodney W. Grimes 
47df8bae1dSRodney W. Grimes #include <net/route.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <netinet/in.h>
50df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
51df8bae1dSRodney W. Grimes #include <netinet/ip.h>
52df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
53df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
54df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
55df8bae1dSRodney W. Grimes #define	TCPOUTFLAGS
56df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
57df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
58df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
59df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
60df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
61610ee2f9SDavid Greenman #ifdef TCPDEBUG
62df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
63610ee2f9SDavid Greenman #endif
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #ifdef notyet
66df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack();
67df8bae1dSRodney W. Grimes #endif
68df8bae1dSRodney W. Grimes 
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  * Tcp output routine: figure out what should be sent and send it.
72df8bae1dSRodney W. Grimes  */
73df8bae1dSRodney W. Grimes int
74df8bae1dSRodney W. Grimes tcp_output(tp)
75df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
76df8bae1dSRodney W. Grimes {
77df8bae1dSRodney W. Grimes 	register struct socket *so = tp->t_inpcb->inp_socket;
78df8bae1dSRodney W. Grimes 	register long len, win;
79df8bae1dSRodney W. Grimes 	int off, flags, error;
80df8bae1dSRodney W. Grimes 	register struct mbuf *m;
81df8bae1dSRodney W. Grimes 	register struct tcpiphdr *ti;
82a0292f23SGarrett Wollman 	u_char opt[TCP_MAXOLEN];
83df8bae1dSRodney W. Grimes 	unsigned optlen, hdrlen;
84df8bae1dSRodney W. Grimes 	int idle, sendalot;
85a0292f23SGarrett Wollman 	struct rmxp_tao *taop;
86a0292f23SGarrett Wollman 	struct rmxp_tao tao_noncached;
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes 	/*
89df8bae1dSRodney W. Grimes 	 * Determine length of data that should be transmitted,
90df8bae1dSRodney W. Grimes 	 * and flags that will be used.
91df8bae1dSRodney W. Grimes 	 * If there is some data or critical controls (SYN, RST)
92df8bae1dSRodney W. Grimes 	 * to send, then transmit; otherwise, investigate further.
93df8bae1dSRodney W. Grimes 	 */
94df8bae1dSRodney W. Grimes 	idle = (tp->snd_max == tp->snd_una);
95df8bae1dSRodney W. Grimes 	if (idle && tp->t_idle >= tp->t_rxtcur)
96df8bae1dSRodney W. Grimes 		/*
97df8bae1dSRodney W. Grimes 		 * We have been idle for "a while" and no acks are
98df8bae1dSRodney W. Grimes 		 * expected to clock out any data we send --
99df8bae1dSRodney W. Grimes 		 * slow start to get ack "clock" running again.
100df8bae1dSRodney W. Grimes 		 */
101df8bae1dSRodney W. Grimes 		tp->snd_cwnd = tp->t_maxseg;
102df8bae1dSRodney W. Grimes again:
103df8bae1dSRodney W. Grimes 	sendalot = 0;
104df8bae1dSRodney W. Grimes 	off = tp->snd_nxt - tp->snd_una;
105df8bae1dSRodney W. Grimes 	win = min(tp->snd_wnd, tp->snd_cwnd);
106df8bae1dSRodney W. Grimes 
107df8bae1dSRodney W. Grimes 	flags = tcp_outflags[tp->t_state];
108a0292f23SGarrett Wollman 	/*
109a0292f23SGarrett Wollman 	 * Get standard flags, and add SYN or FIN if requested by 'hidden'
110a0292f23SGarrett Wollman 	 * state flags.
111a0292f23SGarrett Wollman 	 */
112a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDFIN)
113a0292f23SGarrett Wollman 		flags |= TH_FIN;
114a0292f23SGarrett Wollman 	if (tp->t_flags & TF_NEEDSYN)
115a0292f23SGarrett Wollman 		flags |= TH_SYN;
116a0292f23SGarrett Wollman 
117df8bae1dSRodney W. Grimes 	/*
118df8bae1dSRodney W. Grimes 	 * If in persist timeout with window of 0, send 1 byte.
119df8bae1dSRodney W. Grimes 	 * Otherwise, if window is small but nonzero
120df8bae1dSRodney W. Grimes 	 * and timer expired, we will send what we can
121df8bae1dSRodney W. Grimes 	 * and go to transmit state.
122df8bae1dSRodney W. Grimes 	 */
123df8bae1dSRodney W. Grimes 	if (tp->t_force) {
124df8bae1dSRodney W. Grimes 		if (win == 0) {
125df8bae1dSRodney W. Grimes 			/*
126df8bae1dSRodney W. Grimes 			 * If we still have some data to send, then
127df8bae1dSRodney W. Grimes 			 * clear the FIN bit.  Usually this would
128df8bae1dSRodney W. Grimes 			 * happen below when it realizes that we
129df8bae1dSRodney W. Grimes 			 * aren't sending all the data.  However,
130df8bae1dSRodney W. Grimes 			 * if we have exactly 1 byte of unset data,
131df8bae1dSRodney W. Grimes 			 * then it won't clear the FIN bit below,
132df8bae1dSRodney W. Grimes 			 * and if we are in persist state, we wind
133df8bae1dSRodney W. Grimes 			 * up sending the packet without recording
134df8bae1dSRodney W. Grimes 			 * that we sent the FIN bit.
135df8bae1dSRodney W. Grimes 			 *
136df8bae1dSRodney W. Grimes 			 * We can't just blindly clear the FIN bit,
137df8bae1dSRodney W. Grimes 			 * because if we don't have any more data
138df8bae1dSRodney W. Grimes 			 * to send then the probe will be the FIN
139df8bae1dSRodney W. Grimes 			 * itself.
140df8bae1dSRodney W. Grimes 			 */
141df8bae1dSRodney W. Grimes 			if (off < so->so_snd.sb_cc)
142df8bae1dSRodney W. Grimes 				flags &= ~TH_FIN;
143df8bae1dSRodney W. Grimes 			win = 1;
144df8bae1dSRodney W. Grimes 		} else {
145df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_PERSIST] = 0;
146df8bae1dSRodney W. Grimes 			tp->t_rxtshift = 0;
147df8bae1dSRodney W. Grimes 		}
148df8bae1dSRodney W. Grimes 	}
149df8bae1dSRodney W. Grimes 
150df8bae1dSRodney W. Grimes 	len = min(so->so_snd.sb_cc, win) - off;
151df8bae1dSRodney W. Grimes 
152a0292f23SGarrett Wollman 	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
153a0292f23SGarrett Wollman 		taop = &tao_noncached;
154a0292f23SGarrett Wollman 		bzero(taop, sizeof(*taop));
155a0292f23SGarrett Wollman 	}
156a0292f23SGarrett Wollman 
157a0292f23SGarrett Wollman 	/*
158a0292f23SGarrett Wollman 	 * Lop off SYN bit if it has already been sent.  However, if this
159a0292f23SGarrett Wollman 	 * is SYN-SENT state and if segment contains data and if we don't
160a0292f23SGarrett Wollman 	 * know that foreign host supports TAO, suppress sending segment.
161a0292f23SGarrett Wollman 	 */
162a0292f23SGarrett Wollman 	if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
163a0292f23SGarrett Wollman 		flags &= ~TH_SYN;
164a0292f23SGarrett Wollman 		off--, len++;
165a0292f23SGarrett Wollman 		if (len > 0 && tp->t_state == TCPS_SYN_SENT &&
166a0292f23SGarrett Wollman 		    taop->tao_ccsent == 0)
167a0292f23SGarrett Wollman 			return 0;
168a0292f23SGarrett Wollman 	}
169a0292f23SGarrett Wollman 
170df8bae1dSRodney W. Grimes 	if (len < 0) {
171df8bae1dSRodney W. Grimes 		/*
172df8bae1dSRodney W. Grimes 		 * If FIN has been sent but not acked,
173df8bae1dSRodney W. Grimes 		 * but we haven't been called to retransmit,
174df8bae1dSRodney W. Grimes 		 * len will be -1.  Otherwise, window shrank
175df8bae1dSRodney W. Grimes 		 * after we sent into it.  If window shrank to 0,
176df8bae1dSRodney W. Grimes 		 * cancel pending retransmit and pull snd_nxt
177df8bae1dSRodney W. Grimes 		 * back to (closed) window.  We will enter persist
178df8bae1dSRodney W. Grimes 		 * state below.  If the window didn't close completely,
179df8bae1dSRodney W. Grimes 		 * just wait for an ACK.
180df8bae1dSRodney W. Grimes 		 */
181df8bae1dSRodney W. Grimes 		len = 0;
182df8bae1dSRodney W. Grimes 		if (win == 0) {
183df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_REXMT] = 0;
184df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
185df8bae1dSRodney W. Grimes 		}
186df8bae1dSRodney W. Grimes 	}
187df8bae1dSRodney W. Grimes 	if (len > tp->t_maxseg) {
188df8bae1dSRodney W. Grimes 		len = tp->t_maxseg;
189df8bae1dSRodney W. Grimes 		sendalot = 1;
190df8bae1dSRodney W. Grimes 	}
191df8bae1dSRodney W. Grimes 	if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
192df8bae1dSRodney W. Grimes 		flags &= ~TH_FIN;
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
195df8bae1dSRodney W. Grimes 
196df8bae1dSRodney W. Grimes 	/*
197df8bae1dSRodney W. Grimes 	 * Sender silly window avoidance.  If connection is idle
198df8bae1dSRodney W. Grimes 	 * and can send all data, a maximum segment,
199df8bae1dSRodney W. Grimes 	 * at least a maximum default-size segment do it,
200df8bae1dSRodney W. Grimes 	 * or are forced, do it; otherwise don't bother.
201df8bae1dSRodney W. Grimes 	 * If peer's buffer is tiny, then send
202df8bae1dSRodney W. Grimes 	 * when window is at least half open.
203df8bae1dSRodney W. Grimes 	 * If retransmitting (possibly after persist timer forced us
204df8bae1dSRodney W. Grimes 	 * to send into a small window), then must resend.
205df8bae1dSRodney W. Grimes 	 */
206df8bae1dSRodney W. Grimes 	if (len) {
207df8bae1dSRodney W. Grimes 		if (len == tp->t_maxseg)
208df8bae1dSRodney W. Grimes 			goto send;
209df8bae1dSRodney W. Grimes 		if ((idle || tp->t_flags & TF_NODELAY) &&
210a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NOPUSH) == 0 &&
211df8bae1dSRodney W. Grimes 		    len + off >= so->so_snd.sb_cc)
212df8bae1dSRodney W. Grimes 			goto send;
213df8bae1dSRodney W. Grimes 		if (tp->t_force)
214df8bae1dSRodney W. Grimes 			goto send;
215a0292f23SGarrett Wollman 		if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
216df8bae1dSRodney W. Grimes 			goto send;
217df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_max))
218df8bae1dSRodney W. Grimes 			goto send;
219df8bae1dSRodney W. Grimes 	}
220df8bae1dSRodney W. Grimes 
221df8bae1dSRodney W. Grimes 	/*
222df8bae1dSRodney W. Grimes 	 * Compare available window to amount of window
223df8bae1dSRodney W. Grimes 	 * known to peer (as advertised window less
224df8bae1dSRodney W. Grimes 	 * next expected input).  If the difference is at least two
225df8bae1dSRodney W. Grimes 	 * max size segments, or at least 50% of the maximum possible
226df8bae1dSRodney W. Grimes 	 * window, then want to send a window update to peer.
227df8bae1dSRodney W. Grimes 	 */
228df8bae1dSRodney W. Grimes 	if (win > 0) {
229df8bae1dSRodney W. Grimes 		/*
230df8bae1dSRodney W. Grimes 		 * "adv" is the amount we can increase the window,
231df8bae1dSRodney W. Grimes 		 * taking into account that we are limited by
232df8bae1dSRodney W. Grimes 		 * TCP_MAXWIN << tp->rcv_scale.
233df8bae1dSRodney W. Grimes 		 */
234df8bae1dSRodney W. Grimes 		long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
235df8bae1dSRodney W. Grimes 			(tp->rcv_adv - tp->rcv_nxt);
236df8bae1dSRodney W. Grimes 
237df8bae1dSRodney W. Grimes 		if (adv >= (long) (2 * tp->t_maxseg))
238df8bae1dSRodney W. Grimes 			goto send;
239df8bae1dSRodney W. Grimes 		if (2 * adv >= (long) so->so_rcv.sb_hiwat)
240df8bae1dSRodney W. Grimes 			goto send;
241df8bae1dSRodney W. Grimes 	}
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes 	/*
244df8bae1dSRodney W. Grimes 	 * Send if we owe peer an ACK.
245df8bae1dSRodney W. Grimes 	 */
246df8bae1dSRodney W. Grimes 	if (tp->t_flags & TF_ACKNOW)
247df8bae1dSRodney W. Grimes 		goto send;
248a0292f23SGarrett Wollman 	if ((flags & TH_RST) ||
249a0292f23SGarrett Wollman 	    ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
250df8bae1dSRodney W. Grimes 		goto send;
251df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_una))
252df8bae1dSRodney W. Grimes 		goto send;
253df8bae1dSRodney W. Grimes 	/*
254df8bae1dSRodney W. Grimes 	 * If our state indicates that FIN should be sent
255df8bae1dSRodney W. Grimes 	 * and we have not yet done so, or we're retransmitting the FIN,
256df8bae1dSRodney W. Grimes 	 * then we need to send.
257df8bae1dSRodney W. Grimes 	 */
258df8bae1dSRodney W. Grimes 	if (flags & TH_FIN &&
259df8bae1dSRodney W. Grimes 	    ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
260df8bae1dSRodney W. Grimes 		goto send;
261df8bae1dSRodney W. Grimes 
262df8bae1dSRodney W. Grimes 	/*
263df8bae1dSRodney W. Grimes 	 * TCP window updates are not reliable, rather a polling protocol
264df8bae1dSRodney W. Grimes 	 * using ``persist'' packets is used to insure receipt of window
265df8bae1dSRodney W. Grimes 	 * updates.  The three ``states'' for the output side are:
266df8bae1dSRodney W. Grimes 	 *	idle			not doing retransmits or persists
267df8bae1dSRodney W. Grimes 	 *	persisting		to move a small or zero window
268df8bae1dSRodney W. Grimes 	 *	(re)transmitting	and thereby not persisting
269df8bae1dSRodney W. Grimes 	 *
270df8bae1dSRodney W. Grimes 	 * tp->t_timer[TCPT_PERSIST]
271df8bae1dSRodney W. Grimes 	 *	is set when we are in persist state.
272df8bae1dSRodney W. Grimes 	 * tp->t_force
273df8bae1dSRodney W. Grimes 	 *	is set when we are called to send a persist packet.
274df8bae1dSRodney W. Grimes 	 * tp->t_timer[TCPT_REXMT]
275df8bae1dSRodney W. Grimes 	 *	is set when we are retransmitting
276df8bae1dSRodney W. Grimes 	 * The output side is idle when both timers are zero.
277df8bae1dSRodney W. Grimes 	 *
278df8bae1dSRodney W. Grimes 	 * If send window is too small, there is data to transmit, and no
279df8bae1dSRodney W. Grimes 	 * retransmit or persist is pending, then go to persist state.
280df8bae1dSRodney W. Grimes 	 * If nothing happens soon, send when timer expires:
281df8bae1dSRodney W. Grimes 	 * if window is nonzero, transmit what we can,
282df8bae1dSRodney W. Grimes 	 * otherwise force out a byte.
283df8bae1dSRodney W. Grimes 	 */
284df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
285df8bae1dSRodney W. Grimes 	    tp->t_timer[TCPT_PERSIST] == 0) {
286df8bae1dSRodney W. Grimes 		tp->t_rxtshift = 0;
287df8bae1dSRodney W. Grimes 		tcp_setpersist(tp);
288df8bae1dSRodney W. Grimes 	}
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes 	/*
291df8bae1dSRodney W. Grimes 	 * No reason to send a segment, just return.
292df8bae1dSRodney W. Grimes 	 */
293df8bae1dSRodney W. Grimes 	return (0);
294df8bae1dSRodney W. Grimes 
295df8bae1dSRodney W. Grimes send:
296df8bae1dSRodney W. Grimes 	/*
297df8bae1dSRodney W. Grimes 	 * Before ESTABLISHED, force sending of initial options
298df8bae1dSRodney W. Grimes 	 * unless TCP set not to do any options.
299df8bae1dSRodney W. Grimes 	 * NOTE: we assume that the IP/TCP header plus TCP options
300df8bae1dSRodney W. Grimes 	 * always fit in a single mbuf, leaving room for a maximum
301df8bae1dSRodney W. Grimes 	 * link header, i.e.
302df8bae1dSRodney W. Grimes 	 *	max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
303df8bae1dSRodney W. Grimes 	 */
304df8bae1dSRodney W. Grimes 	optlen = 0;
305df8bae1dSRodney W. Grimes 	hdrlen = sizeof (struct tcpiphdr);
306df8bae1dSRodney W. Grimes 	if (flags & TH_SYN) {
307df8bae1dSRodney W. Grimes 		tp->snd_nxt = tp->iss;
308df8bae1dSRodney W. Grimes 		if ((tp->t_flags & TF_NOOPT) == 0) {
309df8bae1dSRodney W. Grimes 			u_short mss;
310df8bae1dSRodney W. Grimes 
311df8bae1dSRodney W. Grimes 			opt[0] = TCPOPT_MAXSEG;
312a0292f23SGarrett Wollman 			opt[1] = TCPOLEN_MAXSEG;
313a0292f23SGarrett Wollman 			mss = htons((u_short) tcp_mssopt(tp));
31494a5d9b6SDavid Greenman 			(void)memcpy(opt + 2, &mss, sizeof(mss));
315a0292f23SGarrett Wollman 			optlen = TCPOLEN_MAXSEG;
316df8bae1dSRodney W. Grimes 
317df8bae1dSRodney W. Grimes 			if ((tp->t_flags & TF_REQ_SCALE) &&
318df8bae1dSRodney W. Grimes 			    ((flags & TH_ACK) == 0 ||
319df8bae1dSRodney W. Grimes 			    (tp->t_flags & TF_RCVD_SCALE))) {
320df8bae1dSRodney W. Grimes 				*((u_long *) (opt + optlen)) = htonl(
321df8bae1dSRodney W. Grimes 					TCPOPT_NOP << 24 |
322df8bae1dSRodney W. Grimes 					TCPOPT_WINDOW << 16 |
323df8bae1dSRodney W. Grimes 					TCPOLEN_WINDOW << 8 |
324df8bae1dSRodney W. Grimes 					tp->request_r_scale);
325df8bae1dSRodney W. Grimes 				optlen += 4;
326df8bae1dSRodney W. Grimes 			}
327df8bae1dSRodney W. Grimes 		}
328df8bae1dSRodney W. Grimes  	}
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes  	/*
331df8bae1dSRodney W. Grimes 	 * Send a timestamp and echo-reply if this is a SYN and our side
332df8bae1dSRodney W. Grimes 	 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
333df8bae1dSRodney W. Grimes 	 * and our peer have sent timestamps in our SYN's.
334df8bae1dSRodney W. Grimes  	 */
335df8bae1dSRodney W. Grimes  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
336df8bae1dSRodney W. Grimes  	    (flags & TH_RST) == 0 &&
337a0292f23SGarrett Wollman 	    ((flags & TH_ACK) == 0 ||
338df8bae1dSRodney W. Grimes 	     (tp->t_flags & TF_RCVD_TSTMP))) {
339df8bae1dSRodney W. Grimes 		u_long *lp = (u_long *)(opt + optlen);
340df8bae1dSRodney W. Grimes 
341df8bae1dSRodney W. Grimes  		/* Form timestamp option as shown in appendix A of RFC 1323. */
342df8bae1dSRodney W. Grimes  		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
343df8bae1dSRodney W. Grimes  		*lp++ = htonl(tcp_now);
344df8bae1dSRodney W. Grimes  		*lp   = htonl(tp->ts_recent);
345df8bae1dSRodney W. Grimes  		optlen += TCPOLEN_TSTAMP_APPA;
346df8bae1dSRodney W. Grimes  	}
347df8bae1dSRodney W. Grimes 
348a0292f23SGarrett Wollman  	/*
349a0292f23SGarrett Wollman 	 * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
350a0292f23SGarrett Wollman 	 * options are allowed (!TF_NOOPT) and it's not a RST.
351a0292f23SGarrett Wollman  	 */
352a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
353a0292f23SGarrett Wollman  	     (flags & TH_RST) == 0) {
354a0292f23SGarrett Wollman 		switch (flags & (TH_SYN|TH_ACK)) {
355a0292f23SGarrett Wollman 		/*
356a0292f23SGarrett Wollman 		 * This is a normal ACK, send CC if we received CC before
357a0292f23SGarrett Wollman 		 * from our peer.
358a0292f23SGarrett Wollman 		 */
359a0292f23SGarrett Wollman 		case TH_ACK:
360a0292f23SGarrett Wollman 			if (!(tp->t_flags & TF_RCVD_CC))
361a0292f23SGarrett Wollman 				break;
362a0292f23SGarrett Wollman 			/*FALLTHROUGH*/
363a0292f23SGarrett Wollman 
364a0292f23SGarrett Wollman 		/*
365a0292f23SGarrett Wollman 		 * We can only get here in T/TCP's SYN_SENT* state, when
366a0292f23SGarrett Wollman 		 * we're a sending a non-SYN segment without waiting for
367a0292f23SGarrett Wollman 		 * the ACK of our SYN.  A check above assures that we only
368a0292f23SGarrett Wollman 		 * do this if our peer understands T/TCP.
369a0292f23SGarrett Wollman 		 */
370a0292f23SGarrett Wollman 		case 0:
371a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
372a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
373a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_CC;
374a0292f23SGarrett Wollman 			opt[optlen++] = TCPOLEN_CC;
375a0292f23SGarrett Wollman 			*(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
376a0292f23SGarrett Wollman 
377a0292f23SGarrett Wollman 			optlen += 4;
378a0292f23SGarrett Wollman 			break;
379a0292f23SGarrett Wollman 
380a0292f23SGarrett Wollman 		/*
381a0292f23SGarrett Wollman 		 * This is our initial SYN, check whether we have to use
382a0292f23SGarrett Wollman 		 * CC or CC.new.
383a0292f23SGarrett Wollman 		 */
384a0292f23SGarrett Wollman 		case TH_SYN:
385a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
386a0292f23SGarrett Wollman 			opt[optlen++] = TCPOPT_NOP;
387a0292f23SGarrett Wollman 
388a0292f23SGarrett Wollman 			if (taop->tao_ccsent != 0 &&
389a0292f23SGarrett Wollman 			    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
390a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_CC;
391a0292f23SGarrett Wollman 				taop->tao_ccsent = tp->cc_send;
392a0292f23SGarrett Wollman 			} else {
393a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_CCNEW;
394a0292f23SGarrett Wollman 				taop->tao_ccsent = 0;
395a0292f23SGarrett Wollman 			}
396a0292f23SGarrett Wollman 			opt[optlen++] = TCPOLEN_CC;
397a0292f23SGarrett Wollman 			*(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
398a0292f23SGarrett Wollman  			optlen += 4;
399a0292f23SGarrett Wollman 			break;
400a0292f23SGarrett Wollman 
401a0292f23SGarrett Wollman 		/*
402a0292f23SGarrett Wollman 		 * This is a SYN,ACK; send CC and CC.echo if we received
403a0292f23SGarrett Wollman 		 * CC from our peer.
404a0292f23SGarrett Wollman 		 */
405a0292f23SGarrett Wollman 		case (TH_SYN|TH_ACK):
406a0292f23SGarrett Wollman 			if (tp->t_flags & TF_RCVD_CC) {
407a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
408a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
409a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_CC;
410a0292f23SGarrett Wollman 				opt[optlen++] = TCPOLEN_CC;
411a0292f23SGarrett Wollman 				*(u_int32_t *)&opt[optlen] =
412a0292f23SGarrett Wollman 					htonl(tp->cc_send);
413a0292f23SGarrett Wollman 				optlen += 4;
414a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
415a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_NOP;
416a0292f23SGarrett Wollman 				opt[optlen++] = TCPOPT_CCECHO;
417a0292f23SGarrett Wollman 				opt[optlen++] = TCPOLEN_CC;
418a0292f23SGarrett Wollman 				*(u_int32_t *)&opt[optlen] =
419a0292f23SGarrett Wollman 					htonl(tp->cc_recv);
420a0292f23SGarrett Wollman 				optlen += 4;
421a0292f23SGarrett Wollman 			}
422a0292f23SGarrett Wollman 			break;
423a0292f23SGarrett Wollman 		}
424a0292f23SGarrett Wollman  	}
425a0292f23SGarrett Wollman 
426df8bae1dSRodney W. Grimes  	hdrlen += optlen;
427df8bae1dSRodney W. Grimes 
428df8bae1dSRodney W. Grimes 	/*
429df8bae1dSRodney W. Grimes 	 * Adjust data length if insertion of options will
430a0292f23SGarrett Wollman 	 * bump the packet length beyond the t_maxopd length.
431a0292f23SGarrett Wollman 	 * Clear the FIN bit because we cut off the tail of
432a0292f23SGarrett Wollman 	 * the segment.
433df8bae1dSRodney W. Grimes 	 */
434a0292f23SGarrett Wollman 	 if (len + optlen > tp->t_maxopd) {
435297a37f3SDavid Greenman 		/*
436297a37f3SDavid Greenman 		 * If there is still more to send, don't close the connection.
437297a37f3SDavid Greenman 		 */
438297a37f3SDavid Greenman 		flags &= ~TH_FIN;
439a0292f23SGarrett Wollman 		len = tp->t_maxopd - optlen;
440df8bae1dSRodney W. Grimes 		sendalot = 1;
441df8bae1dSRodney W. Grimes 	}
442df8bae1dSRodney W. Grimes 
443a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/
444df8bae1dSRodney W. Grimes  	if (max_linkhdr + hdrlen > MHLEN)
445df8bae1dSRodney W. Grimes 		panic("tcphdr too big");
446a0292f23SGarrett Wollman /*#endif*/
447df8bae1dSRodney W. Grimes 
448df8bae1dSRodney W. Grimes 	/*
449df8bae1dSRodney W. Grimes 	 * Grab a header mbuf, attaching a copy of data to
450df8bae1dSRodney W. Grimes 	 * be transmitted, and initialize the header from
451df8bae1dSRodney W. Grimes 	 * the template for sends on this connection.
452df8bae1dSRodney W. Grimes 	 */
453df8bae1dSRodney W. Grimes 	if (len) {
454df8bae1dSRodney W. Grimes 		if (tp->t_force && len == 1)
455df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndprobe++;
456df8bae1dSRodney W. Grimes 		else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
457df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitpack++;
458df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndrexmitbyte += len;
459df8bae1dSRodney W. Grimes 		} else {
460df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndpack++;
461df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndbyte += len;
462df8bae1dSRodney W. Grimes 		}
463df8bae1dSRodney W. Grimes #ifdef notyet
464df8bae1dSRodney W. Grimes 		if ((m = m_copypack(so->so_snd.sb_mb, off,
465df8bae1dSRodney W. Grimes 		    (int)len, max_linkhdr + hdrlen)) == 0) {
466df8bae1dSRodney W. Grimes 			error = ENOBUFS;
467df8bae1dSRodney W. Grimes 			goto out;
468df8bae1dSRodney W. Grimes 		}
469df8bae1dSRodney W. Grimes 		/*
470df8bae1dSRodney W. Grimes 		 * m_copypack left space for our hdr; use it.
471df8bae1dSRodney W. Grimes 		 */
472df8bae1dSRodney W. Grimes 		m->m_len += hdrlen;
473df8bae1dSRodney W. Grimes 		m->m_data -= hdrlen;
474df8bae1dSRodney W. Grimes #else
475df8bae1dSRodney W. Grimes 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
476df8bae1dSRodney W. Grimes 		if (m == NULL) {
477df8bae1dSRodney W. Grimes 			error = ENOBUFS;
478df8bae1dSRodney W. Grimes 			goto out;
479df8bae1dSRodney W. Grimes 		}
480df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
481df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
482df8bae1dSRodney W. Grimes 		if (len <= MHLEN - hdrlen - max_linkhdr) {
483df8bae1dSRodney W. Grimes 			m_copydata(so->so_snd.sb_mb, off, (int) len,
484df8bae1dSRodney W. Grimes 			    mtod(m, caddr_t) + hdrlen);
485df8bae1dSRodney W. Grimes 			m->m_len += len;
486df8bae1dSRodney W. Grimes 		} else {
487df8bae1dSRodney W. Grimes 			m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
48851823c3aSGarrett Wollman 			if (m->m_next == 0) {
489d7f570e6SGarrett Wollman 				(void) m_free(m);
49051823c3aSGarrett Wollman 				error = ENOBUFS;
49151823c3aSGarrett Wollman 				goto out;
49251823c3aSGarrett Wollman 			}
493df8bae1dSRodney W. Grimes 		}
494df8bae1dSRodney W. Grimes #endif
495df8bae1dSRodney W. Grimes 		/*
496df8bae1dSRodney W. Grimes 		 * If we're sending everything we've got, set PUSH.
497df8bae1dSRodney W. Grimes 		 * (This will keep happy those implementations which only
498df8bae1dSRodney W. Grimes 		 * give data to the user when a buffer fills or
499df8bae1dSRodney W. Grimes 		 * a PUSH comes in.)
500df8bae1dSRodney W. Grimes 		 */
501df8bae1dSRodney W. Grimes 		if (off + len == so->so_snd.sb_cc)
502df8bae1dSRodney W. Grimes 			flags |= TH_PUSH;
503df8bae1dSRodney W. Grimes 	} else {
504df8bae1dSRodney W. Grimes 		if (tp->t_flags & TF_ACKNOW)
505df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndacks++;
506df8bae1dSRodney W. Grimes 		else if (flags & (TH_SYN|TH_FIN|TH_RST))
507df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndctrl++;
508df8bae1dSRodney W. Grimes 		else if (SEQ_GT(tp->snd_up, tp->snd_una))
509df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndurg++;
510df8bae1dSRodney W. Grimes 		else
511df8bae1dSRodney W. Grimes 			tcpstat.tcps_sndwinup++;
512df8bae1dSRodney W. Grimes 
513df8bae1dSRodney W. Grimes 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
514df8bae1dSRodney W. Grimes 		if (m == NULL) {
515df8bae1dSRodney W. Grimes 			error = ENOBUFS;
516df8bae1dSRodney W. Grimes 			goto out;
517df8bae1dSRodney W. Grimes 		}
518df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
519df8bae1dSRodney W. Grimes 		m->m_len = hdrlen;
520df8bae1dSRodney W. Grimes 	}
521df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = (struct ifnet *)0;
522df8bae1dSRodney W. Grimes 	ti = mtod(m, struct tcpiphdr *);
523df8bae1dSRodney W. Grimes 	if (tp->t_template == 0)
524df8bae1dSRodney W. Grimes 		panic("tcp_output");
52594a5d9b6SDavid Greenman 	(void)memcpy(ti, tp->t_template, sizeof (struct tcpiphdr));
526df8bae1dSRodney W. Grimes 
527df8bae1dSRodney W. Grimes 	/*
528df8bae1dSRodney W. Grimes 	 * Fill in fields, remembering maximum advertised
529df8bae1dSRodney W. Grimes 	 * window for use in delaying messages about window sizes.
530df8bae1dSRodney W. Grimes 	 * If resending a FIN, be sure not to use a new sequence number.
531df8bae1dSRodney W. Grimes 	 */
532df8bae1dSRodney W. Grimes 	if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
533df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max)
534df8bae1dSRodney W. Grimes 		tp->snd_nxt--;
535df8bae1dSRodney W. Grimes 	/*
536df8bae1dSRodney W. Grimes 	 * If we are doing retransmissions, then snd_nxt will
537df8bae1dSRodney W. Grimes 	 * not reflect the first unsent octet.  For ACK only
538df8bae1dSRodney W. Grimes 	 * packets, we do not want the sequence number of the
539df8bae1dSRodney W. Grimes 	 * retransmitted packet, we want the sequence number
540df8bae1dSRodney W. Grimes 	 * of the next unsent octet.  So, if there is no data
541df8bae1dSRodney W. Grimes 	 * (and no SYN or FIN), use snd_max instead of snd_nxt
542df8bae1dSRodney W. Grimes 	 * when filling in ti_seq.  But if we are in persist
543df8bae1dSRodney W. Grimes 	 * state, snd_max might reflect one byte beyond the
544df8bae1dSRodney W. Grimes 	 * right edge of the window, so use snd_nxt in that
545df8bae1dSRodney W. Grimes 	 * case, since we know we aren't doing a retransmission.
546df8bae1dSRodney W. Grimes 	 * (retransmit and persist are mutually exclusive...)
547df8bae1dSRodney W. Grimes 	 */
548df8bae1dSRodney W. Grimes 	if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
549df8bae1dSRodney W. Grimes 		ti->ti_seq = htonl(tp->snd_nxt);
550df8bae1dSRodney W. Grimes 	else
551df8bae1dSRodney W. Grimes 		ti->ti_seq = htonl(tp->snd_max);
552df8bae1dSRodney W. Grimes 	ti->ti_ack = htonl(tp->rcv_nxt);
553df8bae1dSRodney W. Grimes 	if (optlen) {
55494a5d9b6SDavid Greenman 		(void)memcpy(ti + 1, opt, optlen);
555df8bae1dSRodney W. Grimes 		ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
556df8bae1dSRodney W. Grimes 	}
557df8bae1dSRodney W. Grimes 	ti->ti_flags = flags;
558df8bae1dSRodney W. Grimes 	/*
559df8bae1dSRodney W. Grimes 	 * Calculate receive window.  Don't shrink window,
560df8bae1dSRodney W. Grimes 	 * but avoid silly window syndrome.
561df8bae1dSRodney W. Grimes 	 */
562df8bae1dSRodney W. Grimes 	if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
563df8bae1dSRodney W. Grimes 		win = 0;
564df8bae1dSRodney W. Grimes 	if (win > (long)TCP_MAXWIN << tp->rcv_scale)
565df8bae1dSRodney W. Grimes 		win = (long)TCP_MAXWIN << tp->rcv_scale;
566df8bae1dSRodney W. Grimes 	if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
567df8bae1dSRodney W. Grimes 		win = (long)(tp->rcv_adv - tp->rcv_nxt);
568df8bae1dSRodney W. Grimes 	ti->ti_win = htons((u_short) (win>>tp->rcv_scale));
569df8bae1dSRodney W. Grimes 	if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
570df8bae1dSRodney W. Grimes 		ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
571df8bae1dSRodney W. Grimes 		ti->ti_flags |= TH_URG;
572df8bae1dSRodney W. Grimes 	} else
573df8bae1dSRodney W. Grimes 		/*
574df8bae1dSRodney W. Grimes 		 * If no urgent pointer to send, then we pull
575df8bae1dSRodney W. Grimes 		 * the urgent pointer to the left edge of the send window
576df8bae1dSRodney W. Grimes 		 * so that it doesn't drift into the send window on sequence
577df8bae1dSRodney W. Grimes 		 * number wraparound.
578df8bae1dSRodney W. Grimes 		 */
579df8bae1dSRodney W. Grimes 		tp->snd_up = tp->snd_una;		/* drag it along */
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes 	/*
582df8bae1dSRodney W. Grimes 	 * Put TCP length in extended header, and then
583df8bae1dSRodney W. Grimes 	 * checksum extended header and data.
584df8bae1dSRodney W. Grimes 	 */
585df8bae1dSRodney W. Grimes 	if (len + optlen)
586df8bae1dSRodney W. Grimes 		ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
587df8bae1dSRodney W. Grimes 		    optlen + len));
588df8bae1dSRodney W. Grimes 	ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
589df8bae1dSRodney W. Grimes 
590df8bae1dSRodney W. Grimes 	/*
591df8bae1dSRodney W. Grimes 	 * In transmit state, time the transmission and arrange for
592df8bae1dSRodney W. Grimes 	 * the retransmit.  In persist state, just set snd_max.
593df8bae1dSRodney W. Grimes 	 */
594df8bae1dSRodney W. Grimes 	if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
595df8bae1dSRodney W. Grimes 		tcp_seq startseq = tp->snd_nxt;
596df8bae1dSRodney W. Grimes 
597df8bae1dSRodney W. Grimes 		/*
598df8bae1dSRodney W. Grimes 		 * Advance snd_nxt over sequence space of this segment.
599df8bae1dSRodney W. Grimes 		 */
600df8bae1dSRodney W. Grimes 		if (flags & (TH_SYN|TH_FIN)) {
601df8bae1dSRodney W. Grimes 			if (flags & TH_SYN)
602df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
603df8bae1dSRodney W. Grimes 			if (flags & TH_FIN) {
604df8bae1dSRodney W. Grimes 				tp->snd_nxt++;
605df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_SENTFIN;
606df8bae1dSRodney W. Grimes 			}
607df8bae1dSRodney W. Grimes 		}
608df8bae1dSRodney W. Grimes 		tp->snd_nxt += len;
609df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
610df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt;
611df8bae1dSRodney W. Grimes 			/*
612df8bae1dSRodney W. Grimes 			 * Time this transmission if not a retransmission and
613df8bae1dSRodney W. Grimes 			 * not currently timing anything.
614df8bae1dSRodney W. Grimes 			 */
615df8bae1dSRodney W. Grimes 			if (tp->t_rtt == 0) {
616df8bae1dSRodney W. Grimes 				tp->t_rtt = 1;
617df8bae1dSRodney W. Grimes 				tp->t_rtseq = startseq;
618df8bae1dSRodney W. Grimes 				tcpstat.tcps_segstimed++;
619df8bae1dSRodney W. Grimes 			}
620df8bae1dSRodney W. Grimes 		}
621df8bae1dSRodney W. Grimes 
622df8bae1dSRodney W. Grimes 		/*
623df8bae1dSRodney W. Grimes 		 * Set retransmit timer if not currently set,
624df8bae1dSRodney W. Grimes 		 * and not doing an ack or a keep-alive probe.
625df8bae1dSRodney W. Grimes 		 * Initial value for retransmit timer is smoothed
626df8bae1dSRodney W. Grimes 		 * round-trip time + 2 * round-trip time variance.
627df8bae1dSRodney W. Grimes 		 * Initialize shift counter which is used for backoff
628df8bae1dSRodney W. Grimes 		 * of retransmit time.
629df8bae1dSRodney W. Grimes 		 */
630df8bae1dSRodney W. Grimes 		if (tp->t_timer[TCPT_REXMT] == 0 &&
631df8bae1dSRodney W. Grimes 		    tp->snd_nxt != tp->snd_una) {
632df8bae1dSRodney W. Grimes 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
633df8bae1dSRodney W. Grimes 			if (tp->t_timer[TCPT_PERSIST]) {
634df8bae1dSRodney W. Grimes 				tp->t_timer[TCPT_PERSIST] = 0;
635df8bae1dSRodney W. Grimes 				tp->t_rxtshift = 0;
636df8bae1dSRodney W. Grimes 			}
637df8bae1dSRodney W. Grimes 		}
638df8bae1dSRodney W. Grimes 	} else
639df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
640df8bae1dSRodney W. Grimes 			tp->snd_max = tp->snd_nxt + len;
641df8bae1dSRodney W. Grimes 
642610ee2f9SDavid Greenman #ifdef TCPDEBUG
643df8bae1dSRodney W. Grimes 	/*
644df8bae1dSRodney W. Grimes 	 * Trace.
645df8bae1dSRodney W. Grimes 	 */
646df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG)
647df8bae1dSRodney W. Grimes 		tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
648610ee2f9SDavid Greenman #endif
649df8bae1dSRodney W. Grimes 
650df8bae1dSRodney W. Grimes 	/*
651df8bae1dSRodney W. Grimes 	 * Fill in IP length and desired time to live and
652df8bae1dSRodney W. Grimes 	 * send to IP level.  There should be a better way
653df8bae1dSRodney W. Grimes 	 * to handle ttl and tos; we could keep them in
654df8bae1dSRodney W. Grimes 	 * the template, but need a way to checksum without them.
655df8bae1dSRodney W. Grimes 	 */
656df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = hdrlen + len;
657df8bae1dSRodney W. Grimes #ifdef TUBA
658df8bae1dSRodney W. Grimes 	if (tp->t_tuba_pcb)
659df8bae1dSRodney W. Grimes 		error = tuba_output(m, tp);
660df8bae1dSRodney W. Grimes 	else
661df8bae1dSRodney W. Grimes #endif
662df8bae1dSRodney W. Grimes     {
663f138387aSGarrett Wollman #ifdef MTUDISC
664f138387aSGarrett Wollman 	struct rtentry *rt;
665f138387aSGarrett Wollman #endif
666df8bae1dSRodney W. Grimes 	((struct ip *)ti)->ip_len = m->m_pkthdr.len;
667df8bae1dSRodney W. Grimes 	((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;	/* XXX */
668df8bae1dSRodney W. Grimes 	((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos;	/* XXX */
669f138387aSGarrett Wollman #ifdef MTUDISC
670f138387aSGarrett Wollman 	/*
671f138387aSGarrett Wollman 	 * See if we should do MTU discovery.  We do it only if the following
672f138387aSGarrett Wollman 	 * are true:
673f138387aSGarrett Wollman 	 *	1) we have a valid route to the destination
674f138387aSGarrett Wollman 	 *	2) the MTU is not locked (if it is, then discovery has been
675f138387aSGarrett Wollman 	 *	   disabled)
676f138387aSGarrett Wollman 	 */
677f138387aSGarrett Wollman 	if ((rt = tp->t_inpcb->inp_route.ro_rt)
678f138387aSGarrett Wollman 	    && rt->rt_flags & RTF_UP
679f138387aSGarrett Wollman 	    && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
680f138387aSGarrett Wollman 		((struct ip *)ti)->ip_off |= IP_DF;
681f138387aSGarrett Wollman 	}
682f138387aSGarrett Wollman #endif /* MTUDISC */
683df8bae1dSRodney W. Grimes 	error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
684df8bae1dSRodney W. Grimes 	    so->so_options & SO_DONTROUTE, 0);
685df8bae1dSRodney W. Grimes     }
686df8bae1dSRodney W. Grimes 	if (error) {
687df8bae1dSRodney W. Grimes out:
688df8bae1dSRodney W. Grimes 		if (error == ENOBUFS) {
689df8bae1dSRodney W. Grimes 			tcp_quench(tp->t_inpcb, 0);
690df8bae1dSRodney W. Grimes 			return (0);
691df8bae1dSRodney W. Grimes 		}
6923d1f141bSGarrett Wollman #ifdef MTUDISC
6933d1f141bSGarrett Wollman 		if (error == EMSGSIZE) {
6943d1f141bSGarrett Wollman 			/*
6953d1f141bSGarrett Wollman 			 * ip_output() will have already fixed the route
6963d1f141bSGarrett Wollman 			 * for us.  tcp_mtudisc() will, as its last action,
6973d1f141bSGarrett Wollman 			 * initiate retransmission, so it is important to
6983d1f141bSGarrett Wollman 			 * not do so here.
6993d1f141bSGarrett Wollman 			 */
7003d1f141bSGarrett Wollman 			tcp_mtudisc(tp->t_inpcb, 0);
7013d1f141bSGarrett Wollman 			return 0;
7023d1f141bSGarrett Wollman 		}
7033d1f141bSGarrett Wollman #endif /* MTUDISC */.
704df8bae1dSRodney W. Grimes 		if ((error == EHOSTUNREACH || error == ENETDOWN)
705df8bae1dSRodney W. Grimes 		    && TCPS_HAVERCVDSYN(tp->t_state)) {
706df8bae1dSRodney W. Grimes 			tp->t_softerror = error;
707df8bae1dSRodney W. Grimes 			return (0);
708df8bae1dSRodney W. Grimes 		}
709df8bae1dSRodney W. Grimes 		return (error);
710df8bae1dSRodney W. Grimes 	}
711df8bae1dSRodney W. Grimes 	tcpstat.tcps_sndtotal++;
712df8bae1dSRodney W. Grimes 
713df8bae1dSRodney W. Grimes 	/*
714df8bae1dSRodney W. Grimes 	 * Data sent (as far as we can tell).
715df8bae1dSRodney W. Grimes 	 * If this advertises a larger window than any other segment,
716df8bae1dSRodney W. Grimes 	 * then remember the size of the advertised window.
717df8bae1dSRodney W. Grimes 	 * Any pending ACK has now been sent.
718df8bae1dSRodney W. Grimes 	 */
719df8bae1dSRodney W. Grimes 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
720df8bae1dSRodney W. Grimes 		tp->rcv_adv = tp->rcv_nxt + win;
721df8bae1dSRodney W. Grimes 	tp->last_ack_sent = tp->rcv_nxt;
722df8bae1dSRodney W. Grimes 	tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
723df8bae1dSRodney W. Grimes 	if (sendalot)
724df8bae1dSRodney W. Grimes 		goto again;
725df8bae1dSRodney W. Grimes 	return (0);
726df8bae1dSRodney W. Grimes }
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes void
729df8bae1dSRodney W. Grimes tcp_setpersist(tp)
730df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
731df8bae1dSRodney W. Grimes {
732df8bae1dSRodney W. Grimes 	register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
733df8bae1dSRodney W. Grimes 
734df8bae1dSRodney W. Grimes 	if (tp->t_timer[TCPT_REXMT])
735df8bae1dSRodney W. Grimes 		panic("tcp_output REXMT");
736df8bae1dSRodney W. Grimes 	/*
737df8bae1dSRodney W. Grimes 	 * Start/restart persistance timer.
738df8bae1dSRodney W. Grimes 	 */
739df8bae1dSRodney W. Grimes 	TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
740df8bae1dSRodney W. Grimes 	    t * tcp_backoff[tp->t_rxtshift],
741df8bae1dSRodney W. Grimes 	    TCPTV_PERSMIN, TCPTV_PERSMAX);
742df8bae1dSRodney W. Grimes 	if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
743df8bae1dSRodney W. Grimes 		tp->t_rxtshift++;
744df8bae1dSRodney W. Grimes }
745