2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
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
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
34 * Changes and additions relating to SLiRP
35 * Copyright (c) 1995 Danny Gasparovski.
37 * Please read the file COPYRIGHT for the
38 * terms and conditions of the copyright.
43 static const uint8_t tcp_outflags
[TCP_NSTATES
] = {
44 TH_RST
|TH_ACK
, 0, TH_SYN
, TH_SYN
|TH_ACK
,
45 TH_ACK
, TH_ACK
, TH_FIN
|TH_ACK
, TH_FIN
|TH_ACK
,
46 TH_FIN
|TH_ACK
, TH_ACK
, TH_ACK
,
51 #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */
54 * Tcp output routine: figure out what should be sent and send it.
57 tcp_output(struct tcpcb
*tp
)
59 register struct socket
*so
= tp
->t_socket
;
60 register long len
, win
;
61 int off
, flags
, error
;
62 register struct mbuf
*m
;
63 register struct tcpiphdr
*ti
, tcpiph_save
;
66 uint8_t opt
[MAX_TCPOPTLEN
];
67 unsigned optlen
, hdrlen
;
70 DEBUG_CALL("tcp_output");
71 DEBUG_ARG("tp = %p", tp
);
74 * Determine length of data that should be transmitted,
75 * and flags that will be used.
76 * If there is some data or critical controls (SYN, RST)
77 * to send, then transmit; otherwise, investigate further.
79 idle
= (tp
->snd_max
== tp
->snd_una
);
80 if (idle
&& tp
->t_idle
>= tp
->t_rxtcur
)
82 * We have been idle for "a while" and no acks are
83 * expected to clock out any data we send --
84 * slow start to get ack "clock" running again.
86 tp
->snd_cwnd
= tp
->t_maxseg
;
89 off
= tp
->snd_nxt
- tp
->snd_una
;
90 win
= MIN(tp
->snd_wnd
, tp
->snd_cwnd
);
92 flags
= tcp_outflags
[tp
->t_state
];
94 DEBUG_MISC(" --- tcp_output flags = 0x%x", flags
);
97 * If in persist timeout with window of 0, send 1 byte.
98 * Otherwise, if window is small but nonzero
99 * and timer expired, we will send what we can
100 * and go to transmit state.
105 * If we still have some data to send, then
106 * clear the FIN bit. Usually this would
107 * happen below when it realizes that we
108 * aren't sending all the data. However,
109 * if we have exactly 1 byte of unset data,
110 * then it won't clear the FIN bit below,
111 * and if we are in persist state, we wind
112 * up sending the packet without recording
113 * that we sent the FIN bit.
115 * We can't just blindly clear the FIN bit,
116 * because if we don't have any more data
117 * to send then the probe will be the FIN
120 if (off
< so
->so_snd
.sb_cc
)
124 tp
->t_timer
[TCPT_PERSIST
] = 0;
129 len
= MIN(so
->so_snd
.sb_cc
, win
) - off
;
133 * If FIN has been sent but not acked,
134 * but we haven't been called to retransmit,
135 * len will be -1. Otherwise, window shrank
136 * after we sent into it. If window shrank to 0,
137 * cancel pending retransmit and pull snd_nxt
138 * back to (closed) window. We will enter persist
139 * state below. If the window didn't close completely,
140 * just wait for an ACK.
144 tp
->t_timer
[TCPT_REXMT
] = 0;
145 tp
->snd_nxt
= tp
->snd_una
;
149 if (len
> tp
->t_maxseg
) {
153 if (SEQ_LT(tp
->snd_nxt
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
))
156 win
= sbspace(&so
->so_rcv
);
159 * Sender silly window avoidance. If connection is idle
160 * and can send all data, a maximum segment,
161 * at least a maximum default-size segment do it,
162 * or are forced, do it; otherwise don't bother.
163 * If peer's buffer is tiny, then send
164 * when window is at least half open.
165 * If retransmitting (possibly after persist timer forced us
166 * to send into a small window), then must resend.
169 if (len
== tp
->t_maxseg
)
171 if ((1 || idle
|| tp
->t_flags
& TF_NODELAY
) &&
172 len
+ off
>= so
->so_snd
.sb_cc
)
176 if (len
>= tp
->max_sndwnd
/ 2 && tp
->max_sndwnd
> 0)
178 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
))
183 * Compare available window to amount of window
184 * known to peer (as advertised window less
185 * next expected input). If the difference is at least two
186 * max size segments, or at least 50% of the maximum possible
187 * window, then want to send a window update to peer.
191 * "adv" is the amount we can increase the window,
192 * taking into account that we are limited by
193 * TCP_MAXWIN << tp->rcv_scale.
195 long adv
= MIN(win
, (long)TCP_MAXWIN
<< tp
->rcv_scale
) -
196 (tp
->rcv_adv
- tp
->rcv_nxt
);
198 if (adv
>= (long) (2 * tp
->t_maxseg
))
200 if (2 * adv
>= (long) so
->so_rcv
.sb_datalen
)
205 * Send if we owe peer an ACK.
207 if (tp
->t_flags
& TF_ACKNOW
)
209 if (flags
& (TH_SYN
|TH_RST
))
211 if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
214 * If our state indicates that FIN should be sent
215 * and we have not yet done so, or we're retransmitting the FIN,
216 * then we need to send.
218 if (flags
& TH_FIN
&&
219 ((tp
->t_flags
& TF_SENTFIN
) == 0 || tp
->snd_nxt
== tp
->snd_una
))
223 * TCP window updates are not reliable, rather a polling protocol
224 * using ``persist'' packets is used to insure receipt of window
225 * updates. The three ``states'' for the output side are:
226 * idle not doing retransmits or persists
227 * persisting to move a small or zero window
228 * (re)transmitting and thereby not persisting
230 * tp->t_timer[TCPT_PERSIST]
231 * is set when we are in persist state.
233 * is set when we are called to send a persist packet.
234 * tp->t_timer[TCPT_REXMT]
235 * is set when we are retransmitting
236 * The output side is idle when both timers are zero.
238 * If send window is too small, there is data to transmit, and no
239 * retransmit or persist is pending, then go to persist state.
240 * If nothing happens soon, send when timer expires:
241 * if window is nonzero, transmit what we can,
242 * otherwise force out a byte.
244 if (so
->so_snd
.sb_cc
&& tp
->t_timer
[TCPT_REXMT
] == 0 &&
245 tp
->t_timer
[TCPT_PERSIST
] == 0) {
251 * No reason to send a segment, just return.
257 * Before ESTABLISHED, force sending of initial options
258 * unless TCP set not to do any options.
259 * NOTE: we assume that the IP/TCP header plus TCP options
260 * always fit in a single mbuf, leaving room for a maximum
262 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
265 hdrlen
= sizeof (struct tcpiphdr
);
266 if (flags
& TH_SYN
) {
267 tp
->snd_nxt
= tp
->iss
;
268 if ((tp
->t_flags
& TF_NOOPT
) == 0) {
271 opt
[0] = TCPOPT_MAXSEG
;
273 mss
= htons((uint16_t) tcp_mss(tp
, 0));
274 memcpy((char *)(opt
+ 2), (char *)&mss
, sizeof(mss
));
282 * Adjust data length if insertion of options will
283 * bump the packet length beyond the t_maxseg length.
285 if (len
> tp
->t_maxseg
- optlen
) {
286 len
= tp
->t_maxseg
- optlen
;
291 * Grab a header mbuf, attaching a copy of data to
292 * be transmitted, and initialize the header from
293 * the template for sends on this connection.
296 m
= m_get(so
->slirp
);
301 m
->m_data
+= IF_MAXLINKHDR
;
304 sbcopy(&so
->so_snd
, off
, (int) len
, mtod(m
, char *) + hdrlen
);
308 * If we're sending everything we've got, set PUSH.
309 * (This will keep happy those implementations which only
310 * give data to the user when a buffer fills or
313 if (off
+ len
== so
->so_snd
.sb_cc
)
316 m
= m_get(so
->slirp
);
321 m
->m_data
+= IF_MAXLINKHDR
;
325 ti
= mtod(m
, struct tcpiphdr
*);
327 memcpy((char *)ti
, &tp
->t_template
, sizeof (struct tcpiphdr
));
330 * Fill in fields, remembering maximum advertised
331 * window for use in delaying messages about window sizes.
332 * If resending a FIN, be sure not to use a new sequence number.
334 if (flags
& TH_FIN
&& tp
->t_flags
& TF_SENTFIN
&&
335 tp
->snd_nxt
== tp
->snd_max
)
338 * If we are doing retransmissions, then snd_nxt will
339 * not reflect the first unsent octet. For ACK only
340 * packets, we do not want the sequence number of the
341 * retransmitted packet, we want the sequence number
342 * of the next unsent octet. So, if there is no data
343 * (and no SYN or FIN), use snd_max instead of snd_nxt
344 * when filling in ti_seq. But if we are in persist
345 * state, snd_max might reflect one byte beyond the
346 * right edge of the window, so use snd_nxt in that
347 * case, since we know we aren't doing a retransmission.
348 * (retransmit and persist are mutually exclusive...)
350 if (len
|| (flags
& (TH_SYN
|TH_FIN
)) || tp
->t_timer
[TCPT_PERSIST
])
351 ti
->ti_seq
= htonl(tp
->snd_nxt
);
353 ti
->ti_seq
= htonl(tp
->snd_max
);
354 ti
->ti_ack
= htonl(tp
->rcv_nxt
);
356 memcpy((char *)(ti
+ 1), (char *)opt
, optlen
);
357 ti
->ti_off
= (sizeof (struct tcphdr
) + optlen
) >> 2;
359 ti
->ti_flags
= flags
;
361 * Calculate receive window. Don't shrink window,
362 * but avoid silly window syndrome.
364 if (win
< (long)(so
->so_rcv
.sb_datalen
/ 4) && win
< (long)tp
->t_maxseg
)
366 if (win
> (long)TCP_MAXWIN
<< tp
->rcv_scale
)
367 win
= (long)TCP_MAXWIN
<< tp
->rcv_scale
;
368 if (win
< (long)(tp
->rcv_adv
- tp
->rcv_nxt
))
369 win
= (long)(tp
->rcv_adv
- tp
->rcv_nxt
);
370 ti
->ti_win
= htons((uint16_t) (win
>>tp
->rcv_scale
));
372 if (SEQ_GT(tp
->snd_up
, tp
->snd_una
)) {
373 ti
->ti_urp
= htons((uint16_t)(tp
->snd_up
- ntohl(ti
->ti_seq
)));
374 ti
->ti_flags
|= TH_URG
;
377 * If no urgent pointer to send, then we pull
378 * the urgent pointer to the left edge of the send window
379 * so that it doesn't drift into the send window on sequence
382 tp
->snd_up
= tp
->snd_una
; /* drag it along */
385 * Put TCP length in extended header, and then
386 * checksum extended header and data.
389 ti
->ti_len
= htons((uint16_t)(sizeof (struct tcphdr
) +
391 ti
->ti_sum
= cksum(m
, (int)(hdrlen
+ len
));
394 * In transmit state, time the transmission and arrange for
395 * the retransmit. In persist state, just set snd_max.
397 if (tp
->t_force
== 0 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
398 tcp_seq startseq
= tp
->snd_nxt
;
401 * Advance snd_nxt over sequence space of this segment.
403 if (flags
& (TH_SYN
|TH_FIN
)) {
406 if (flags
& TH_FIN
) {
408 tp
->t_flags
|= TF_SENTFIN
;
412 if (SEQ_GT(tp
->snd_nxt
, tp
->snd_max
)) {
413 tp
->snd_max
= tp
->snd_nxt
;
415 * Time this transmission if not a retransmission and
416 * not currently timing anything.
418 if (tp
->t_rtt
== 0) {
420 tp
->t_rtseq
= startseq
;
425 * Set retransmit timer if not currently set,
426 * and not doing an ack or a keep-alive probe.
427 * Initial value for retransmit timer is smoothed
428 * round-trip time + 2 * round-trip time variance.
429 * Initialize shift counter which is used for backoff
430 * of retransmit time.
432 if (tp
->t_timer
[TCPT_REXMT
] == 0 &&
433 tp
->snd_nxt
!= tp
->snd_una
) {
434 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
435 if (tp
->t_timer
[TCPT_PERSIST
]) {
436 tp
->t_timer
[TCPT_PERSIST
] = 0;
441 if (SEQ_GT(tp
->snd_nxt
+ len
, tp
->snd_max
))
442 tp
->snd_max
= tp
->snd_nxt
+ len
;
445 * Fill in IP length and desired time to live and
446 * send to IP level. There should be a better way
447 * to handle ttl and tos; we could keep them in
448 * the template, but need a way to checksum without them.
450 m
->m_len
= hdrlen
+ len
; /* XXX Needed? m_len should be correct */
451 tcpiph_save
= *mtod(m
, struct tcpiphdr
*);
453 switch (so
->so_ffamily
) {
455 m
->m_data
+= sizeof(struct tcpiphdr
) - sizeof(struct tcphdr
)
457 m
->m_len
-= sizeof(struct tcpiphdr
) - sizeof(struct tcphdr
)
459 ip
= mtod(m
, struct ip
*);
461 ip
->ip_len
= m
->m_len
;
462 ip
->ip_dst
= tcpiph_save
.ti_dst
;
463 ip
->ip_src
= tcpiph_save
.ti_src
;
464 ip
->ip_p
= tcpiph_save
.ti_pr
;
466 ip
->ip_ttl
= IPDEFTTL
;
467 ip
->ip_tos
= so
->so_iptos
;
468 error
= ip_output(so
, m
);
472 m
->m_data
+= sizeof(struct tcpiphdr
) - sizeof(struct tcphdr
)
473 - sizeof(struct ip6
);
474 m
->m_len
-= sizeof(struct tcpiphdr
) - sizeof(struct tcphdr
)
475 - sizeof(struct ip6
);
476 ip6
= mtod(m
, struct ip6
*);
478 ip6
->ip_pl
= tcpiph_save
.ti_len
;
479 ip6
->ip_dst
= tcpiph_save
.ti_dst6
;
480 ip6
->ip_src
= tcpiph_save
.ti_src6
;
481 ip6
->ip_nh
= tcpiph_save
.ti_nh6
;
483 error
= ip6_output(so
, m
, 0);
487 g_assert_not_reached();
496 * Data sent (as far as we can tell).
497 * If this advertises a larger window than any other segment,
498 * then remember the size of the advertised window.
499 * Any pending ACK has now been sent.
501 if (win
> 0 && SEQ_GT(tp
->rcv_nxt
+win
, tp
->rcv_adv
))
502 tp
->rcv_adv
= tp
->rcv_nxt
+ win
;
503 tp
->last_ack_sent
= tp
->rcv_nxt
;
504 tp
->t_flags
&= ~(TF_ACKNOW
|TF_DELACK
);
512 tcp_setpersist(struct tcpcb
*tp
)
514 int t
= ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1;
517 * Start/restart persistence timer.
519 TCPT_RANGESET(tp
->t_timer
[TCPT_PERSIST
],
520 t
* tcp_backoff
[tp
->t_rxtshift
],
521 TCPTV_PERSMIN
, TCPTV_PERSMAX
);
522 if (tp
->t_rxtshift
< TCP_MAXRXTSHIFT
)