]> git.proxmox.com Git - mirror_qemu.git/blame - slirp/tcp_output.c
slirp: Clean up includes
[mirror_qemu.git] / slirp / tcp_output.c
CommitLineData
f0cbd3ec
FB
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
2f5f8996 13 * 3. Neither the name of the University nor the names of its contributors
f0cbd3ec
FB
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)tcp_output.c 8.3 (Berkeley) 12/30/93
30 * tcp_output.c,v 1.3 1994/09/15 10:36:55 davidg Exp
31 */
32
33/*
34 * Changes and additions relating to SLiRP
35 * Copyright (c) 1995 Danny Gasparovski.
5fafdf24
TS
36 *
37 * Please read the file COPYRIGHT for the
f0cbd3ec
FB
38 * terms and conditions of the copyright.
39 */
40
7df7482b 41#include "qemu/osdep.h"
f0cbd3ec
FB
42#include <slirp.h>
43
9634d903 44static const u_char tcp_outflags[TCP_NSTATES] = {
f0cbd3ec 45 TH_RST|TH_ACK, 0, TH_SYN, TH_SYN|TH_ACK,
5fafdf24 46 TH_ACK, TH_ACK, TH_FIN|TH_ACK, TH_FIN|TH_ACK,
f0cbd3ec
FB
47 TH_FIN|TH_ACK, TH_ACK, TH_ACK,
48};
49
50
917cfc1f 51#undef MAX_TCPOPTLEN
f0cbd3ec
FB
52#define MAX_TCPOPTLEN 32 /* max # bytes that go in options */
53
54/*
55 * Tcp output routine: figure out what should be sent and send it.
56 */
57int
511d2b14 58tcp_output(struct tcpcb *tp)
f0cbd3ec
FB
59{
60 register struct socket *so = tp->t_socket;
61 register long len, win;
62 int off, flags, error;
63 register struct mbuf *m;
64 register struct tcpiphdr *ti;
65 u_char opt[MAX_TCPOPTLEN];
66 unsigned optlen, hdrlen;
67 int idle, sendalot;
5fafdf24 68
f0cbd3ec 69 DEBUG_CALL("tcp_output");
ecc804ca 70 DEBUG_ARG("tp = %p", tp);
5fafdf24 71
f0cbd3ec
FB
72 /*
73 * Determine length of data that should be transmitted,
74 * and flags that will be used.
75 * If there is some data or critical controls (SYN, RST)
76 * to send, then transmit; otherwise, investigate further.
77 */
78 idle = (tp->snd_max == tp->snd_una);
79 if (idle && tp->t_idle >= tp->t_rxtcur)
80 /*
81 * We have been idle for "a while" and no acks are
82 * expected to clock out any data we send --
83 * slow start to get ack "clock" running again.
84 */
85 tp->snd_cwnd = tp->t_maxseg;
86again:
87 sendalot = 0;
88 off = tp->snd_nxt - tp->snd_una;
89 win = min(tp->snd_wnd, tp->snd_cwnd);
90
91 flags = tcp_outflags[tp->t_state];
5fafdf24 92
f0cbd3ec 93 DEBUG_MISC((dfd, " --- tcp_output flags = 0x%x\n",flags));
5fafdf24 94
f0cbd3ec
FB
95 /*
96 * If in persist timeout with window of 0, send 1 byte.
97 * Otherwise, if window is small but nonzero
98 * and timer expired, we will send what we can
99 * and go to transmit state.
100 */
101 if (tp->t_force) {
102 if (win == 0) {
103 /*
104 * If we still have some data to send, then
105 * clear the FIN bit. Usually this would
106 * happen below when it realizes that we
107 * aren't sending all the data. However,
108 * if we have exactly 1 byte of unset data,
109 * then it won't clear the FIN bit below,
110 * and if we are in persist state, we wind
111 * up sending the packet without recording
112 * that we sent the FIN bit.
113 *
114 * We can't just blindly clear the FIN bit,
115 * because if we don't have any more data
116 * to send then the probe will be the FIN
117 * itself.
118 */
119 if (off < so->so_snd.sb_cc)
120 flags &= ~TH_FIN;
121 win = 1;
122 } else {
123 tp->t_timer[TCPT_PERSIST] = 0;
124 tp->t_rxtshift = 0;
125 }
126 }
127
128 len = min(so->so_snd.sb_cc, win) - off;
129
130 if (len < 0) {
131 /*
132 * If FIN has been sent but not acked,
133 * but we haven't been called to retransmit,
134 * len will be -1. Otherwise, window shrank
135 * after we sent into it. If window shrank to 0,
136 * cancel pending retransmit and pull snd_nxt
137 * back to (closed) window. We will enter persist
138 * state below. If the window didn't close completely,
139 * just wait for an ACK.
140 */
141 len = 0;
142 if (win == 0) {
143 tp->t_timer[TCPT_REXMT] = 0;
144 tp->snd_nxt = tp->snd_una;
145 }
146 }
5fafdf24 147
f0cbd3ec
FB
148 if (len > tp->t_maxseg) {
149 len = tp->t_maxseg;
150 sendalot = 1;
151 }
152 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
153 flags &= ~TH_FIN;
154
155 win = sbspace(&so->so_rcv);
156
157 /*
158 * Sender silly window avoidance. If connection is idle
159 * and can send all data, a maximum segment,
160 * at least a maximum default-size segment do it,
161 * or are forced, do it; otherwise don't bother.
162 * If peer's buffer is tiny, then send
163 * when window is at least half open.
164 * If retransmitting (possibly after persist timer forced us
165 * to send into a small window), then must resend.
166 */
167 if (len) {
168 if (len == tp->t_maxseg)
169 goto send;
170 if ((1 || idle || tp->t_flags & TF_NODELAY) &&
171 len + off >= so->so_snd.sb_cc)
172 goto send;
173 if (tp->t_force)
174 goto send;
175 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
176 goto send;
177 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
178 goto send;
179 }
180
181 /*
182 * Compare available window to amount of window
183 * known to peer (as advertised window less
184 * next expected input). If the difference is at least two
185 * max size segments, or at least 50% of the maximum possible
186 * window, then want to send a window update to peer.
187 */
188 if (win > 0) {
5fafdf24 189 /*
f0cbd3ec
FB
190 * "adv" is the amount we can increase the window,
191 * taking into account that we are limited by
192 * TCP_MAXWIN << tp->rcv_scale.
193 */
194 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
195 (tp->rcv_adv - tp->rcv_nxt);
196
197 if (adv >= (long) (2 * tp->t_maxseg))
198 goto send;
199 if (2 * adv >= (long) so->so_rcv.sb_datalen)
200 goto send;
201 }
202
203 /*
204 * Send if we owe peer an ACK.
205 */
206 if (tp->t_flags & TF_ACKNOW)
207 goto send;
208 if (flags & (TH_SYN|TH_RST))
209 goto send;
210 if (SEQ_GT(tp->snd_up, tp->snd_una))
211 goto send;
212 /*
213 * If our state indicates that FIN should be sent
214 * and we have not yet done so, or we're retransmitting the FIN,
215 * then we need to send.
216 */
217 if (flags & TH_FIN &&
218 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
219 goto send;
220
221 /*
222 * TCP window updates are not reliable, rather a polling protocol
223 * using ``persist'' packets is used to insure receipt of window
224 * updates. The three ``states'' for the output side are:
225 * idle not doing retransmits or persists
226 * persisting to move a small or zero window
227 * (re)transmitting and thereby not persisting
228 *
229 * tp->t_timer[TCPT_PERSIST]
230 * is set when we are in persist state.
231 * tp->t_force
232 * is set when we are called to send a persist packet.
233 * tp->t_timer[TCPT_REXMT]
234 * is set when we are retransmitting
235 * The output side is idle when both timers are zero.
236 *
237 * If send window is too small, there is data to transmit, and no
238 * retransmit or persist is pending, then go to persist state.
239 * If nothing happens soon, send when timer expires:
240 * if window is nonzero, transmit what we can,
241 * otherwise force out a byte.
242 */
243 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
244 tp->t_timer[TCPT_PERSIST] == 0) {
245 tp->t_rxtshift = 0;
246 tcp_setpersist(tp);
247 }
248
249 /*
250 * No reason to send a segment, just return.
251 */
f0cbd3ec
FB
252 return (0);
253
254send:
255 /*
256 * Before ESTABLISHED, force sending of initial options
257 * unless TCP set not to do any options.
258 * NOTE: we assume that the IP/TCP header plus TCP options
259 * always fit in a single mbuf, leaving room for a maximum
260 * link header, i.e.
261 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
262 */
263 optlen = 0;
264 hdrlen = sizeof (struct tcpiphdr);
265 if (flags & TH_SYN) {
266 tp->snd_nxt = tp->iss;
267 if ((tp->t_flags & TF_NOOPT) == 0) {
b6dce92e 268 uint16_t mss;
f0cbd3ec
FB
269
270 opt[0] = TCPOPT_MAXSEG;
271 opt[1] = 4;
b6dce92e 272 mss = htons((uint16_t) tcp_mss(tp, 0));
f0cbd3ec
FB
273 memcpy((caddr_t)(opt + 2), (caddr_t)&mss, sizeof(mss));
274 optlen = 4;
f0cbd3ec
FB
275 }
276 }
5fafdf24 277
f0cbd3ec 278 hdrlen += optlen;
5fafdf24 279
f0cbd3ec
FB
280 /*
281 * Adjust data length if insertion of options will
282 * bump the packet length beyond the t_maxseg length.
283 */
284 if (len > tp->t_maxseg - optlen) {
285 len = tp->t_maxseg - optlen;
286 sendalot = 1;
287 }
288
289 /*
290 * Grab a header mbuf, attaching a copy of data to
291 * be transmitted, and initialize the header from
292 * the template for sends on this connection.
293 */
294 if (len) {
460fec67 295 m = m_get(so->slirp);
f0cbd3ec 296 if (m == NULL) {
f0cbd3ec
FB
297 error = 1;
298 goto out;
299 }
9634d903 300 m->m_data += IF_MAXLINKHDR;
f0cbd3ec 301 m->m_len = hdrlen;
3b46e624 302
0d62c4cf
JK
303 sbcopy(&so->so_snd, off, (int) len, mtod(m, caddr_t) + hdrlen);
304 m->m_len += len;
f0cbd3ec 305
f0cbd3ec
FB
306 /*
307 * If we're sending everything we've got, set PUSH.
308 * (This will keep happy those implementations which only
309 * give data to the user when a buffer fills or
310 * a PUSH comes in.)
311 */
312 if (off + len == so->so_snd.sb_cc)
313 flags |= TH_PUSH;
314 } else {
460fec67 315 m = m_get(so->slirp);
f0cbd3ec 316 if (m == NULL) {
f0cbd3ec
FB
317 error = 1;
318 goto out;
319 }
9634d903 320 m->m_data += IF_MAXLINKHDR;
f0cbd3ec
FB
321 m->m_len = hdrlen;
322 }
323
324 ti = mtod(m, struct tcpiphdr *);
5fafdf24 325
f0cbd3ec
FB
326 memcpy((caddr_t)ti, &tp->t_template, sizeof (struct tcpiphdr));
327
328 /*
329 * Fill in fields, remembering maximum advertised
330 * window for use in delaying messages about window sizes.
331 * If resending a FIN, be sure not to use a new sequence number.
332 */
5fafdf24 333 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
f0cbd3ec
FB
334 tp->snd_nxt == tp->snd_max)
335 tp->snd_nxt--;
336 /*
337 * If we are doing retransmissions, then snd_nxt will
338 * not reflect the first unsent octet. For ACK only
339 * packets, we do not want the sequence number of the
340 * retransmitted packet, we want the sequence number
341 * of the next unsent octet. So, if there is no data
342 * (and no SYN or FIN), use snd_max instead of snd_nxt
343 * when filling in ti_seq. But if we are in persist
344 * state, snd_max might reflect one byte beyond the
345 * right edge of the window, so use snd_nxt in that
346 * case, since we know we aren't doing a retransmission.
347 * (retransmit and persist are mutually exclusive...)
348 */
349 if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
350 ti->ti_seq = htonl(tp->snd_nxt);
351 else
352 ti->ti_seq = htonl(tp->snd_max);
353 ti->ti_ack = htonl(tp->rcv_nxt);
354 if (optlen) {
355 memcpy((caddr_t)(ti + 1), (caddr_t)opt, optlen);
356 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
357 }
358 ti->ti_flags = flags;
359 /*
360 * Calculate receive window. Don't shrink window,
361 * but avoid silly window syndrome.
362 */
363 if (win < (long)(so->so_rcv.sb_datalen / 4) && win < (long)tp->t_maxseg)
364 win = 0;
365 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
366 win = (long)TCP_MAXWIN << tp->rcv_scale;
367 if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
368 win = (long)(tp->rcv_adv - tp->rcv_nxt);
b6dce92e 369 ti->ti_win = htons((uint16_t) (win>>tp->rcv_scale));
5fafdf24 370
f0cbd3ec 371 if (SEQ_GT(tp->snd_up, tp->snd_una)) {
b6dce92e 372 ti->ti_urp = htons((uint16_t)(tp->snd_up - ntohl(ti->ti_seq)));
f0cbd3ec
FB
373 ti->ti_flags |= TH_URG;
374 } else
375 /*
376 * If no urgent pointer to send, then we pull
377 * the urgent pointer to the left edge of the send window
378 * so that it doesn't drift into the send window on sequence
379 * number wraparound.
380 */
381 tp->snd_up = tp->snd_una; /* drag it along */
382
383 /*
384 * Put TCP length in extended header, and then
385 * checksum extended header and data.
386 */
387 if (len + optlen)
b6dce92e 388 ti->ti_len = htons((uint16_t)(sizeof (struct tcphdr) +
f0cbd3ec
FB
389 optlen + len));
390 ti->ti_sum = cksum(m, (int)(hdrlen + len));
391
392 /*
393 * In transmit state, time the transmission and arrange for
394 * the retransmit. In persist state, just set snd_max.
395 */
396 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
397 tcp_seq startseq = tp->snd_nxt;
398
399 /*
400 * Advance snd_nxt over sequence space of this segment.
401 */
402 if (flags & (TH_SYN|TH_FIN)) {
403 if (flags & TH_SYN)
404 tp->snd_nxt++;
405 if (flags & TH_FIN) {
406 tp->snd_nxt++;
407 tp->t_flags |= TF_SENTFIN;
408 }
409 }
410 tp->snd_nxt += len;
411 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
412 tp->snd_max = tp->snd_nxt;
413 /*
414 * Time this transmission if not a retransmission and
415 * not currently timing anything.
416 */
417 if (tp->t_rtt == 0) {
418 tp->t_rtt = 1;
419 tp->t_rtseq = startseq;
f0cbd3ec
FB
420 }
421 }
422
423 /*
424 * Set retransmit timer if not currently set,
425 * and not doing an ack or a keep-alive probe.
426 * Initial value for retransmit timer is smoothed
427 * round-trip time + 2 * round-trip time variance.
428 * Initialize shift counter which is used for backoff
429 * of retransmit time.
430 */
431 if (tp->t_timer[TCPT_REXMT] == 0 &&
432 tp->snd_nxt != tp->snd_una) {
433 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
434 if (tp->t_timer[TCPT_PERSIST]) {
435 tp->t_timer[TCPT_PERSIST] = 0;
436 tp->t_rxtshift = 0;
437 }
438 }
439 } else
440 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
441 tp->snd_max = tp->snd_nxt + len;
442
443 /*
444 * Fill in IP length and desired time to live and
445 * send to IP level. There should be a better way
446 * to handle ttl and tos; we could keep them in
447 * the template, but need a way to checksum without them.
448 */
449 m->m_len = hdrlen + len; /* XXX Needed? m_len should be correct */
5fafdf24 450
f0cbd3ec 451 {
3b46e624 452
f0cbd3ec
FB
453 ((struct ip *)ti)->ip_len = m->m_len;
454
9634d903 455 ((struct ip *)ti)->ip_ttl = IPDEFTTL;
f0cbd3ec 456 ((struct ip *)ti)->ip_tos = so->so_iptos;
3b46e624 457
f0cbd3ec 458 error = ip_output(so, m);
f0cbd3ec
FB
459 }
460 if (error) {
461out:
f0cbd3ec
FB
462 return (error);
463 }
f0cbd3ec
FB
464
465 /*
466 * Data sent (as far as we can tell).
467 * If this advertises a larger window than any other segment,
468 * then remember the size of the advertised window.
469 * Any pending ACK has now been sent.
470 */
471 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
472 tp->rcv_adv = tp->rcv_nxt + win;
473 tp->last_ack_sent = tp->rcv_nxt;
474 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
475 if (sendalot)
476 goto again;
477
478 return (0);
479}
480
481void
511d2b14 482tcp_setpersist(struct tcpcb *tp)
f0cbd3ec
FB
483{
484 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
485
f0cbd3ec
FB
486 /*
487 * Start/restart persistence timer.
488 */
489 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
490 t * tcp_backoff[tp->t_rxtshift],
491 TCPTV_PERSMIN, TCPTV_PERSMAX);
492 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
493 tp->t_rxtshift++;
494}