tcp_output.c revision 1.1.1.3 1 1.1 cgd /*
2 1.1.1.3 thorpej * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 1.1.1.2 thorpej * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd *
33 1.1.1.3 thorpej * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
34 1.1 cgd */
35 1.1 cgd
36 1.1.1.2 thorpej #include <sys/param.h>
37 1.1.1.2 thorpej #include <sys/systm.h>
38 1.1.1.2 thorpej #include <sys/malloc.h>
39 1.1.1.2 thorpej #include <sys/mbuf.h>
40 1.1.1.2 thorpej #include <sys/protosw.h>
41 1.1.1.2 thorpej #include <sys/socket.h>
42 1.1.1.2 thorpej #include <sys/socketvar.h>
43 1.1.1.2 thorpej #include <sys/errno.h>
44 1.1.1.2 thorpej
45 1.1.1.2 thorpej #include <net/route.h>
46 1.1.1.2 thorpej
47 1.1.1.2 thorpej #include <netinet/in.h>
48 1.1.1.2 thorpej #include <netinet/in_systm.h>
49 1.1.1.2 thorpej #include <netinet/ip.h>
50 1.1.1.2 thorpej #include <netinet/in_pcb.h>
51 1.1.1.2 thorpej #include <netinet/ip_var.h>
52 1.1.1.2 thorpej #include <netinet/tcp.h>
53 1.1 cgd #define TCPOUTFLAGS
54 1.1.1.2 thorpej #include <netinet/tcp_fsm.h>
55 1.1.1.2 thorpej #include <netinet/tcp_seq.h>
56 1.1.1.2 thorpej #include <netinet/tcp_timer.h>
57 1.1.1.2 thorpej #include <netinet/tcp_var.h>
58 1.1.1.2 thorpej #include <netinet/tcpip.h>
59 1.1.1.2 thorpej #include <netinet/tcp_debug.h>
60 1.1 cgd
61 1.1 cgd #ifdef notyet
62 1.1 cgd extern struct mbuf *m_copypack();
63 1.1 cgd #endif
64 1.1 cgd
65 1.1.1.2 thorpej
66 1.1.1.2 thorpej #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */
67 1.1 cgd
68 1.1 cgd /*
69 1.1 cgd * Tcp output routine: figure out what should be sent and send it.
70 1.1 cgd */
71 1.1.1.2 thorpej int
72 1.1 cgd tcp_output(tp)
73 1.1 cgd register struct tcpcb *tp;
74 1.1 cgd {
75 1.1 cgd register struct socket *so = tp->t_inpcb->inp_socket;
76 1.1 cgd register long len, win;
77 1.1 cgd int off, flags, error;
78 1.1 cgd register struct mbuf *m;
79 1.1 cgd register struct tcpiphdr *ti;
80 1.1.1.2 thorpej u_char opt[MAX_TCPOPTLEN];
81 1.1 cgd unsigned optlen, hdrlen;
82 1.1 cgd int idle, sendalot;
83 1.1 cgd
84 1.1 cgd /*
85 1.1 cgd * Determine length of data that should be transmitted,
86 1.1 cgd * and flags that will be used.
87 1.1 cgd * If there is some data or critical controls (SYN, RST)
88 1.1 cgd * to send, then transmit; otherwise, investigate further.
89 1.1 cgd */
90 1.1 cgd idle = (tp->snd_max == tp->snd_una);
91 1.1 cgd if (idle && tp->t_idle >= tp->t_rxtcur)
92 1.1 cgd /*
93 1.1 cgd * We have been idle for "a while" and no acks are
94 1.1 cgd * expected to clock out any data we send --
95 1.1 cgd * slow start to get ack "clock" running again.
96 1.1 cgd */
97 1.1 cgd tp->snd_cwnd = tp->t_maxseg;
98 1.1 cgd again:
99 1.1 cgd sendalot = 0;
100 1.1 cgd off = tp->snd_nxt - tp->snd_una;
101 1.1 cgd win = min(tp->snd_wnd, tp->snd_cwnd);
102 1.1 cgd
103 1.1.1.2 thorpej flags = tcp_outflags[tp->t_state];
104 1.1 cgd /*
105 1.1 cgd * If in persist timeout with window of 0, send 1 byte.
106 1.1 cgd * Otherwise, if window is small but nonzero
107 1.1 cgd * and timer expired, we will send what we can
108 1.1 cgd * and go to transmit state.
109 1.1 cgd */
110 1.1 cgd if (tp->t_force) {
111 1.1.1.2 thorpej if (win == 0) {
112 1.1.1.2 thorpej /*
113 1.1.1.2 thorpej * If we still have some data to send, then
114 1.1.1.2 thorpej * clear the FIN bit. Usually this would
115 1.1.1.2 thorpej * happen below when it realizes that we
116 1.1.1.2 thorpej * aren't sending all the data. However,
117 1.1.1.2 thorpej * if we have exactly 1 byte of unset data,
118 1.1.1.2 thorpej * then it won't clear the FIN bit below,
119 1.1.1.2 thorpej * and if we are in persist state, we wind
120 1.1.1.2 thorpej * up sending the packet without recording
121 1.1.1.2 thorpej * that we sent the FIN bit.
122 1.1.1.2 thorpej *
123 1.1.1.2 thorpej * We can't just blindly clear the FIN bit,
124 1.1.1.2 thorpej * because if we don't have any more data
125 1.1.1.2 thorpej * to send then the probe will be the FIN
126 1.1.1.2 thorpej * itself.
127 1.1.1.2 thorpej */
128 1.1.1.2 thorpej if (off < so->so_snd.sb_cc)
129 1.1.1.2 thorpej flags &= ~TH_FIN;
130 1.1 cgd win = 1;
131 1.1.1.2 thorpej } else {
132 1.1 cgd tp->t_timer[TCPT_PERSIST] = 0;
133 1.1 cgd tp->t_rxtshift = 0;
134 1.1 cgd }
135 1.1 cgd }
136 1.1 cgd
137 1.1 cgd len = min(so->so_snd.sb_cc, win) - off;
138 1.1 cgd
139 1.1 cgd if (len < 0) {
140 1.1 cgd /*
141 1.1 cgd * If FIN has been sent but not acked,
142 1.1 cgd * but we haven't been called to retransmit,
143 1.1 cgd * len will be -1. Otherwise, window shrank
144 1.1 cgd * after we sent into it. If window shrank to 0,
145 1.1 cgd * cancel pending retransmit and pull snd_nxt
146 1.1 cgd * back to (closed) window. We will enter persist
147 1.1 cgd * state below. If the window didn't close completely,
148 1.1 cgd * just wait for an ACK.
149 1.1 cgd */
150 1.1 cgd len = 0;
151 1.1 cgd if (win == 0) {
152 1.1 cgd tp->t_timer[TCPT_REXMT] = 0;
153 1.1 cgd tp->snd_nxt = tp->snd_una;
154 1.1 cgd }
155 1.1 cgd }
156 1.1 cgd if (len > tp->t_maxseg) {
157 1.1 cgd len = tp->t_maxseg;
158 1.1 cgd sendalot = 1;
159 1.1 cgd }
160 1.1 cgd if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
161 1.1 cgd flags &= ~TH_FIN;
162 1.1 cgd
163 1.1 cgd win = sbspace(&so->so_rcv);
164 1.1 cgd
165 1.1 cgd /*
166 1.1 cgd * Sender silly window avoidance. If connection is idle
167 1.1 cgd * and can send all data, a maximum segment,
168 1.1 cgd * at least a maximum default-size segment do it,
169 1.1 cgd * or are forced, do it; otherwise don't bother.
170 1.1 cgd * If peer's buffer is tiny, then send
171 1.1 cgd * when window is at least half open.
172 1.1 cgd * If retransmitting (possibly after persist timer forced us
173 1.1 cgd * to send into a small window), then must resend.
174 1.1 cgd */
175 1.1 cgd if (len) {
176 1.1 cgd if (len == tp->t_maxseg)
177 1.1 cgd goto send;
178 1.1 cgd if ((idle || tp->t_flags & TF_NODELAY) &&
179 1.1 cgd len + off >= so->so_snd.sb_cc)
180 1.1 cgd goto send;
181 1.1 cgd if (tp->t_force)
182 1.1 cgd goto send;
183 1.1 cgd if (len >= tp->max_sndwnd / 2)
184 1.1 cgd goto send;
185 1.1 cgd if (SEQ_LT(tp->snd_nxt, tp->snd_max))
186 1.1 cgd goto send;
187 1.1 cgd }
188 1.1 cgd
189 1.1 cgd /*
190 1.1 cgd * Compare available window to amount of window
191 1.1 cgd * known to peer (as advertised window less
192 1.1 cgd * next expected input). If the difference is at least two
193 1.1 cgd * max size segments, or at least 50% of the maximum possible
194 1.1 cgd * window, then want to send a window update to peer.
195 1.1 cgd */
196 1.1 cgd if (win > 0) {
197 1.1.1.2 thorpej /*
198 1.1.1.2 thorpej * "adv" is the amount we can increase the window,
199 1.1.1.2 thorpej * taking into account that we are limited by
200 1.1.1.2 thorpej * TCP_MAXWIN << tp->rcv_scale.
201 1.1.1.2 thorpej */
202 1.1.1.2 thorpej long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
203 1.1.1.2 thorpej (tp->rcv_adv - tp->rcv_nxt);
204 1.1 cgd
205 1.1 cgd if (adv >= (long) (2 * tp->t_maxseg))
206 1.1 cgd goto send;
207 1.1 cgd if (2 * adv >= (long) so->so_rcv.sb_hiwat)
208 1.1 cgd goto send;
209 1.1 cgd }
210 1.1 cgd
211 1.1 cgd /*
212 1.1 cgd * Send if we owe peer an ACK.
213 1.1 cgd */
214 1.1 cgd if (tp->t_flags & TF_ACKNOW)
215 1.1 cgd goto send;
216 1.1 cgd if (flags & (TH_SYN|TH_RST))
217 1.1 cgd goto send;
218 1.1 cgd if (SEQ_GT(tp->snd_up, tp->snd_una))
219 1.1 cgd goto send;
220 1.1 cgd /*
221 1.1 cgd * If our state indicates that FIN should be sent
222 1.1 cgd * and we have not yet done so, or we're retransmitting the FIN,
223 1.1 cgd * then we need to send.
224 1.1 cgd */
225 1.1 cgd if (flags & TH_FIN &&
226 1.1 cgd ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
227 1.1 cgd goto send;
228 1.1 cgd
229 1.1 cgd /*
230 1.1 cgd * TCP window updates are not reliable, rather a polling protocol
231 1.1 cgd * using ``persist'' packets is used to insure receipt of window
232 1.1 cgd * updates. The three ``states'' for the output side are:
233 1.1 cgd * idle not doing retransmits or persists
234 1.1 cgd * persisting to move a small or zero window
235 1.1 cgd * (re)transmitting and thereby not persisting
236 1.1 cgd *
237 1.1 cgd * tp->t_timer[TCPT_PERSIST]
238 1.1 cgd * is set when we are in persist state.
239 1.1 cgd * tp->t_force
240 1.1 cgd * is set when we are called to send a persist packet.
241 1.1 cgd * tp->t_timer[TCPT_REXMT]
242 1.1 cgd * is set when we are retransmitting
243 1.1 cgd * The output side is idle when both timers are zero.
244 1.1 cgd *
245 1.1 cgd * If send window is too small, there is data to transmit, and no
246 1.1 cgd * retransmit or persist is pending, then go to persist state.
247 1.1 cgd * If nothing happens soon, send when timer expires:
248 1.1 cgd * if window is nonzero, transmit what we can,
249 1.1 cgd * otherwise force out a byte.
250 1.1 cgd */
251 1.1 cgd if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
252 1.1 cgd tp->t_timer[TCPT_PERSIST] == 0) {
253 1.1 cgd tp->t_rxtshift = 0;
254 1.1 cgd tcp_setpersist(tp);
255 1.1 cgd }
256 1.1 cgd
257 1.1 cgd /*
258 1.1 cgd * No reason to send a segment, just return.
259 1.1 cgd */
260 1.1 cgd return (0);
261 1.1 cgd
262 1.1 cgd send:
263 1.1 cgd /*
264 1.1 cgd * Before ESTABLISHED, force sending of initial options
265 1.1 cgd * unless TCP set not to do any options.
266 1.1 cgd * NOTE: we assume that the IP/TCP header plus TCP options
267 1.1 cgd * always fit in a single mbuf, leaving room for a maximum
268 1.1 cgd * link header, i.e.
269 1.1 cgd * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
270 1.1 cgd */
271 1.1 cgd optlen = 0;
272 1.1 cgd hdrlen = sizeof (struct tcpiphdr);
273 1.1.1.2 thorpej if (flags & TH_SYN) {
274 1.1.1.2 thorpej tp->snd_nxt = tp->iss;
275 1.1.1.2 thorpej if ((tp->t_flags & TF_NOOPT) == 0) {
276 1.1.1.2 thorpej u_short mss;
277 1.1.1.2 thorpej
278 1.1.1.2 thorpej opt[0] = TCPOPT_MAXSEG;
279 1.1.1.2 thorpej opt[1] = 4;
280 1.1.1.2 thorpej mss = htons((u_short) tcp_mss(tp, 0));
281 1.1.1.2 thorpej bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss));
282 1.1.1.2 thorpej optlen = 4;
283 1.1.1.2 thorpej
284 1.1.1.2 thorpej if ((tp->t_flags & TF_REQ_SCALE) &&
285 1.1.1.2 thorpej ((flags & TH_ACK) == 0 ||
286 1.1.1.2 thorpej (tp->t_flags & TF_RCVD_SCALE))) {
287 1.1.1.2 thorpej *((u_long *) (opt + optlen)) = htonl(
288 1.1.1.2 thorpej TCPOPT_NOP << 24 |
289 1.1.1.2 thorpej TCPOPT_WINDOW << 16 |
290 1.1.1.2 thorpej TCPOLEN_WINDOW << 8 |
291 1.1.1.2 thorpej tp->request_r_scale);
292 1.1.1.2 thorpej optlen += 4;
293 1.1.1.2 thorpej }
294 1.1.1.2 thorpej }
295 1.1.1.2 thorpej }
296 1.1.1.2 thorpej
297 1.1.1.2 thorpej /*
298 1.1.1.2 thorpej * Send a timestamp and echo-reply if this is a SYN and our side
299 1.1.1.2 thorpej * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
300 1.1.1.2 thorpej * and our peer have sent timestamps in our SYN's.
301 1.1.1.2 thorpej */
302 1.1.1.2 thorpej if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
303 1.1.1.2 thorpej (flags & TH_RST) == 0 &&
304 1.1.1.2 thorpej ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
305 1.1.1.2 thorpej (tp->t_flags & TF_RCVD_TSTMP))) {
306 1.1.1.2 thorpej u_long *lp = (u_long *)(opt + optlen);
307 1.1.1.2 thorpej
308 1.1.1.2 thorpej /* Form timestamp option as shown in appendix A of RFC 1323. */
309 1.1.1.2 thorpej *lp++ = htonl(TCPOPT_TSTAMP_HDR);
310 1.1.1.2 thorpej *lp++ = htonl(tcp_now);
311 1.1.1.2 thorpej *lp = htonl(tp->ts_recent);
312 1.1.1.2 thorpej optlen += TCPOLEN_TSTAMP_APPA;
313 1.1.1.2 thorpej }
314 1.1.1.2 thorpej
315 1.1.1.2 thorpej hdrlen += optlen;
316 1.1.1.2 thorpej
317 1.1.1.2 thorpej /*
318 1.1.1.2 thorpej * Adjust data length if insertion of options will
319 1.1.1.2 thorpej * bump the packet length beyond the t_maxseg length.
320 1.1.1.2 thorpej */
321 1.1.1.3 thorpej if (len > tp->t_maxseg - optlen) {
322 1.1.1.2 thorpej len = tp->t_maxseg - optlen;
323 1.1.1.2 thorpej sendalot = 1;
324 1.1.1.3 thorpej flags &= ~TH_FIN;
325 1.1.1.2 thorpej }
326 1.1.1.2 thorpej
327 1.1.1.2 thorpej
328 1.1 cgd #ifdef DIAGNOSTIC
329 1.1.1.2 thorpej if (max_linkhdr + hdrlen > MHLEN)
330 1.1.1.2 thorpej panic("tcphdr too big");
331 1.1 cgd #endif
332 1.1 cgd
333 1.1 cgd /*
334 1.1 cgd * Grab a header mbuf, attaching a copy of data to
335 1.1 cgd * be transmitted, and initialize the header from
336 1.1 cgd * the template for sends on this connection.
337 1.1 cgd */
338 1.1 cgd if (len) {
339 1.1 cgd if (tp->t_force && len == 1)
340 1.1 cgd tcpstat.tcps_sndprobe++;
341 1.1 cgd else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
342 1.1 cgd tcpstat.tcps_sndrexmitpack++;
343 1.1 cgd tcpstat.tcps_sndrexmitbyte += len;
344 1.1 cgd } else {
345 1.1 cgd tcpstat.tcps_sndpack++;
346 1.1 cgd tcpstat.tcps_sndbyte += len;
347 1.1 cgd }
348 1.1 cgd #ifdef notyet
349 1.1 cgd if ((m = m_copypack(so->so_snd.sb_mb, off,
350 1.1 cgd (int)len, max_linkhdr + hdrlen)) == 0) {
351 1.1 cgd error = ENOBUFS;
352 1.1 cgd goto out;
353 1.1 cgd }
354 1.1 cgd /*
355 1.1 cgd * m_copypack left space for our hdr; use it.
356 1.1 cgd */
357 1.1 cgd m->m_len += hdrlen;
358 1.1 cgd m->m_data -= hdrlen;
359 1.1 cgd #else
360 1.1 cgd MGETHDR(m, M_DONTWAIT, MT_HEADER);
361 1.1 cgd if (m == NULL) {
362 1.1 cgd error = ENOBUFS;
363 1.1 cgd goto out;
364 1.1 cgd }
365 1.1 cgd m->m_data += max_linkhdr;
366 1.1 cgd m->m_len = hdrlen;
367 1.1 cgd if (len <= MHLEN - hdrlen - max_linkhdr) {
368 1.1 cgd m_copydata(so->so_snd.sb_mb, off, (int) len,
369 1.1 cgd mtod(m, caddr_t) + hdrlen);
370 1.1 cgd m->m_len += len;
371 1.1 cgd } else {
372 1.1 cgd m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
373 1.1.1.3 thorpej if (m->m_next == 0) {
374 1.1.1.3 thorpej (void) m_free(m);
375 1.1.1.3 thorpej error = ENOBUFS;
376 1.1.1.3 thorpej goto out;
377 1.1.1.3 thorpej }
378 1.1 cgd }
379 1.1 cgd #endif
380 1.1 cgd /*
381 1.1 cgd * If we're sending everything we've got, set PUSH.
382 1.1 cgd * (This will keep happy those implementations which only
383 1.1 cgd * give data to the user when a buffer fills or
384 1.1 cgd * a PUSH comes in.)
385 1.1 cgd */
386 1.1 cgd if (off + len == so->so_snd.sb_cc)
387 1.1 cgd flags |= TH_PUSH;
388 1.1 cgd } else {
389 1.1 cgd if (tp->t_flags & TF_ACKNOW)
390 1.1 cgd tcpstat.tcps_sndacks++;
391 1.1 cgd else if (flags & (TH_SYN|TH_FIN|TH_RST))
392 1.1 cgd tcpstat.tcps_sndctrl++;
393 1.1 cgd else if (SEQ_GT(tp->snd_up, tp->snd_una))
394 1.1 cgd tcpstat.tcps_sndurg++;
395 1.1 cgd else
396 1.1 cgd tcpstat.tcps_sndwinup++;
397 1.1 cgd
398 1.1 cgd MGETHDR(m, M_DONTWAIT, MT_HEADER);
399 1.1 cgd if (m == NULL) {
400 1.1 cgd error = ENOBUFS;
401 1.1 cgd goto out;
402 1.1 cgd }
403 1.1 cgd m->m_data += max_linkhdr;
404 1.1 cgd m->m_len = hdrlen;
405 1.1 cgd }
406 1.1 cgd m->m_pkthdr.rcvif = (struct ifnet *)0;
407 1.1 cgd ti = mtod(m, struct tcpiphdr *);
408 1.1 cgd if (tp->t_template == 0)
409 1.1 cgd panic("tcp_output");
410 1.1 cgd bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
411 1.1 cgd
412 1.1 cgd /*
413 1.1 cgd * Fill in fields, remembering maximum advertised
414 1.1 cgd * window for use in delaying messages about window sizes.
415 1.1 cgd * If resending a FIN, be sure not to use a new sequence number.
416 1.1 cgd */
417 1.1 cgd if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
418 1.1 cgd tp->snd_nxt == tp->snd_max)
419 1.1 cgd tp->snd_nxt--;
420 1.1.1.2 thorpej /*
421 1.1.1.2 thorpej * If we are doing retransmissions, then snd_nxt will
422 1.1.1.2 thorpej * not reflect the first unsent octet. For ACK only
423 1.1.1.2 thorpej * packets, we do not want the sequence number of the
424 1.1.1.2 thorpej * retransmitted packet, we want the sequence number
425 1.1.1.2 thorpej * of the next unsent octet. So, if there is no data
426 1.1.1.2 thorpej * (and no SYN or FIN), use snd_max instead of snd_nxt
427 1.1.1.2 thorpej * when filling in ti_seq. But if we are in persist
428 1.1.1.2 thorpej * state, snd_max might reflect one byte beyond the
429 1.1.1.2 thorpej * right edge of the window, so use snd_nxt in that
430 1.1.1.2 thorpej * case, since we know we aren't doing a retransmission.
431 1.1.1.2 thorpej * (retransmit and persist are mutually exclusive...)
432 1.1.1.2 thorpej */
433 1.1.1.2 thorpej if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
434 1.1.1.2 thorpej ti->ti_seq = htonl(tp->snd_nxt);
435 1.1.1.2 thorpej else
436 1.1.1.2 thorpej ti->ti_seq = htonl(tp->snd_max);
437 1.1 cgd ti->ti_ack = htonl(tp->rcv_nxt);
438 1.1 cgd if (optlen) {
439 1.1 cgd bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
440 1.1 cgd ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
441 1.1 cgd }
442 1.1 cgd ti->ti_flags = flags;
443 1.1 cgd /*
444 1.1 cgd * Calculate receive window. Don't shrink window,
445 1.1 cgd * but avoid silly window syndrome.
446 1.1 cgd */
447 1.1 cgd if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
448 1.1 cgd win = 0;
449 1.1.1.2 thorpej if (win > (long)TCP_MAXWIN << tp->rcv_scale)
450 1.1.1.2 thorpej win = (long)TCP_MAXWIN << tp->rcv_scale;
451 1.1 cgd if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
452 1.1 cgd win = (long)(tp->rcv_adv - tp->rcv_nxt);
453 1.1.1.2 thorpej ti->ti_win = htons((u_short) (win>>tp->rcv_scale));
454 1.1 cgd if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
455 1.1 cgd ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
456 1.1 cgd ti->ti_flags |= TH_URG;
457 1.1 cgd } else
458 1.1 cgd /*
459 1.1 cgd * If no urgent pointer to send, then we pull
460 1.1 cgd * the urgent pointer to the left edge of the send window
461 1.1 cgd * so that it doesn't drift into the send window on sequence
462 1.1 cgd * number wraparound.
463 1.1 cgd */
464 1.1 cgd tp->snd_up = tp->snd_una; /* drag it along */
465 1.1 cgd
466 1.1 cgd /*
467 1.1 cgd * Put TCP length in extended header, and then
468 1.1 cgd * checksum extended header and data.
469 1.1 cgd */
470 1.1 cgd if (len + optlen)
471 1.1 cgd ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
472 1.1 cgd optlen + len));
473 1.1 cgd ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
474 1.1 cgd
475 1.1 cgd /*
476 1.1 cgd * In transmit state, time the transmission and arrange for
477 1.1 cgd * the retransmit. In persist state, just set snd_max.
478 1.1 cgd */
479 1.1 cgd if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
480 1.1 cgd tcp_seq startseq = tp->snd_nxt;
481 1.1 cgd
482 1.1 cgd /*
483 1.1 cgd * Advance snd_nxt over sequence space of this segment.
484 1.1 cgd */
485 1.1 cgd if (flags & (TH_SYN|TH_FIN)) {
486 1.1 cgd if (flags & TH_SYN)
487 1.1 cgd tp->snd_nxt++;
488 1.1 cgd if (flags & TH_FIN) {
489 1.1 cgd tp->snd_nxt++;
490 1.1 cgd tp->t_flags |= TF_SENTFIN;
491 1.1 cgd }
492 1.1 cgd }
493 1.1 cgd tp->snd_nxt += len;
494 1.1 cgd if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
495 1.1 cgd tp->snd_max = tp->snd_nxt;
496 1.1 cgd /*
497 1.1 cgd * Time this transmission if not a retransmission and
498 1.1 cgd * not currently timing anything.
499 1.1 cgd */
500 1.1 cgd if (tp->t_rtt == 0) {
501 1.1 cgd tp->t_rtt = 1;
502 1.1 cgd tp->t_rtseq = startseq;
503 1.1 cgd tcpstat.tcps_segstimed++;
504 1.1 cgd }
505 1.1 cgd }
506 1.1 cgd
507 1.1 cgd /*
508 1.1 cgd * Set retransmit timer if not currently set,
509 1.1 cgd * and not doing an ack or a keep-alive probe.
510 1.1 cgd * Initial value for retransmit timer is smoothed
511 1.1 cgd * round-trip time + 2 * round-trip time variance.
512 1.1 cgd * Initialize shift counter which is used for backoff
513 1.1 cgd * of retransmit time.
514 1.1 cgd */
515 1.1 cgd if (tp->t_timer[TCPT_REXMT] == 0 &&
516 1.1 cgd tp->snd_nxt != tp->snd_una) {
517 1.1 cgd tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
518 1.1 cgd if (tp->t_timer[TCPT_PERSIST]) {
519 1.1 cgd tp->t_timer[TCPT_PERSIST] = 0;
520 1.1 cgd tp->t_rxtshift = 0;
521 1.1 cgd }
522 1.1 cgd }
523 1.1 cgd } else
524 1.1 cgd if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
525 1.1 cgd tp->snd_max = tp->snd_nxt + len;
526 1.1 cgd
527 1.1 cgd /*
528 1.1 cgd * Trace.
529 1.1 cgd */
530 1.1 cgd if (so->so_options & SO_DEBUG)
531 1.1 cgd tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
532 1.1 cgd
533 1.1 cgd /*
534 1.1 cgd * Fill in IP length and desired time to live and
535 1.1 cgd * send to IP level. There should be a better way
536 1.1 cgd * to handle ttl and tos; we could keep them in
537 1.1 cgd * the template, but need a way to checksum without them.
538 1.1 cgd */
539 1.1 cgd m->m_pkthdr.len = hdrlen + len;
540 1.1.1.2 thorpej #ifdef TUBA
541 1.1.1.2 thorpej if (tp->t_tuba_pcb)
542 1.1.1.2 thorpej error = tuba_output(m, tp);
543 1.1.1.2 thorpej else
544 1.1.1.2 thorpej #endif
545 1.1.1.2 thorpej {
546 1.1 cgd ((struct ip *)ti)->ip_len = m->m_pkthdr.len;
547 1.1 cgd ((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; /* XXX */
548 1.1 cgd ((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos; /* XXX */
549 1.1 cgd #if BSD >= 43
550 1.1 cgd error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
551 1.1.1.2 thorpej so->so_options & SO_DONTROUTE, 0);
552 1.1 cgd #else
553 1.1 cgd error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
554 1.1 cgd so->so_options & SO_DONTROUTE);
555 1.1 cgd #endif
556 1.1.1.2 thorpej }
557 1.1 cgd if (error) {
558 1.1 cgd out:
559 1.1 cgd if (error == ENOBUFS) {
560 1.1.1.2 thorpej tcp_quench(tp->t_inpcb, 0);
561 1.1 cgd return (0);
562 1.1 cgd }
563 1.1 cgd if ((error == EHOSTUNREACH || error == ENETDOWN)
564 1.1 cgd && TCPS_HAVERCVDSYN(tp->t_state)) {
565 1.1 cgd tp->t_softerror = error;
566 1.1 cgd return (0);
567 1.1 cgd }
568 1.1 cgd return (error);
569 1.1 cgd }
570 1.1 cgd tcpstat.tcps_sndtotal++;
571 1.1 cgd
572 1.1 cgd /*
573 1.1 cgd * Data sent (as far as we can tell).
574 1.1 cgd * If this advertises a larger window than any other segment,
575 1.1 cgd * then remember the size of the advertised window.
576 1.1 cgd * Any pending ACK has now been sent.
577 1.1 cgd */
578 1.1 cgd if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
579 1.1 cgd tp->rcv_adv = tp->rcv_nxt + win;
580 1.1.1.2 thorpej tp->last_ack_sent = tp->rcv_nxt;
581 1.1 cgd tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
582 1.1 cgd if (sendalot)
583 1.1 cgd goto again;
584 1.1 cgd return (0);
585 1.1 cgd }
586 1.1 cgd
587 1.1.1.2 thorpej void
588 1.1 cgd tcp_setpersist(tp)
589 1.1 cgd register struct tcpcb *tp;
590 1.1 cgd {
591 1.1 cgd register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
592 1.1 cgd
593 1.1 cgd if (tp->t_timer[TCPT_REXMT])
594 1.1 cgd panic("tcp_output REXMT");
595 1.1 cgd /*
596 1.1 cgd * Start/restart persistance timer.
597 1.1 cgd */
598 1.1 cgd TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
599 1.1 cgd t * tcp_backoff[tp->t_rxtshift],
600 1.1 cgd TCPTV_PERSMIN, TCPTV_PERSMAX);
601 1.1 cgd if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
602 1.1 cgd tp->t_rxtshift++;
603 1.1 cgd }
604