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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)tcp_output.c 8.3 (Berkeley) 12/30/93
34 * tcp_output.c,v 1.3 1994/09/15 10:36:55 davidg Exp
38 * Changes and additions relating to SLiRP
39 * Copyright (c) 1995 Danny Gasparovski.
41 * Please read the file COPYRIGHT for the
42 * terms and conditions of the copyright.
48 #define max(x,y) ((x) > (y) ? (x) : (y))
49 #define min(x,y) ((x) < (y) ? (x) : (y))
52 * Since this is only used in "stats socket", we give meaning
53 * names instead of the REAL names
56 /* "CLOSED", "LISTEN", "SYN_SENT", "SYN_RCVD", */
57 "REDIRECT", "LISTEN", "SYN_SENT", "SYN_RCVD",
58 "ESTABLISHED", "CLOSE_WAIT", "FIN_WAIT_1", "CLOSING",
59 "LAST_ACK", "FIN_WAIT_2", "TIME_WAIT",
62 u_char tcp_outflags
[TCP_NSTATES
] = {
63 TH_RST
|TH_ACK
, 0, TH_SYN
, TH_SYN
|TH_ACK
,
64 TH_ACK
, TH_ACK
, TH_FIN
|TH_ACK
, TH_FIN
|TH_ACK
,
65 TH_FIN
|TH_ACK
, TH_ACK
, TH_ACK
,
69 #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */
72 * Tcp output routine: figure out what should be sent and send it.
76 register struct tcpcb
*tp
;
78 register struct socket
*so
= tp
->t_socket
;
79 register long len
, win
;
80 int off
, flags
, error
;
81 register struct mbuf
*m
;
82 register struct tcpiphdr
*ti
;
83 u_char opt
[MAX_TCPOPTLEN
];
84 unsigned optlen
, hdrlen
;
87 DEBUG_CALL("tcp_output");
88 DEBUG_ARG("tp = %lx", (long )tp
);
91 * Determine length of data that should be transmitted,
92 * and flags that will be used.
93 * If there is some data or critical controls (SYN, RST)
94 * to send, then transmit; otherwise, investigate further.
96 idle
= (tp
->snd_max
== tp
->snd_una
);
97 if (idle
&& tp
->t_idle
>= tp
->t_rxtcur
)
99 * We have been idle for "a while" and no acks are
100 * expected to clock out any data we send --
101 * slow start to get ack "clock" running again.
103 tp
->snd_cwnd
= tp
->t_maxseg
;
106 off
= tp
->snd_nxt
- tp
->snd_una
;
107 win
= min(tp
->snd_wnd
, tp
->snd_cwnd
);
109 flags
= tcp_outflags
[tp
->t_state
];
111 DEBUG_MISC((dfd
, " --- tcp_output flags = 0x%x\n",flags
));
114 * If in persist timeout with window of 0, send 1 byte.
115 * Otherwise, if window is small but nonzero
116 * and timer expired, we will send what we can
117 * and go to transmit state.
122 * If we still have some data to send, then
123 * clear the FIN bit. Usually this would
124 * happen below when it realizes that we
125 * aren't sending all the data. However,
126 * if we have exactly 1 byte of unset data,
127 * then it won't clear the FIN bit below,
128 * and if we are in persist state, we wind
129 * up sending the packet without recording
130 * that we sent the FIN bit.
132 * We can't just blindly clear the FIN bit,
133 * because if we don't have any more data
134 * to send then the probe will be the FIN
137 if (off
< so
->so_snd
.sb_cc
)
141 tp
->t_timer
[TCPT_PERSIST
] = 0;
146 len
= min(so
->so_snd
.sb_cc
, win
) - off
;
150 * If FIN has been sent but not acked,
151 * but we haven't been called to retransmit,
152 * len will be -1. Otherwise, window shrank
153 * after we sent into it. If window shrank to 0,
154 * cancel pending retransmit and pull snd_nxt
155 * back to (closed) window. We will enter persist
156 * state below. If the window didn't close completely,
157 * just wait for an ACK.
161 tp
->t_timer
[TCPT_REXMT
] = 0;
162 tp
->snd_nxt
= tp
->snd_una
;
166 if (len
> tp
->t_maxseg
) {
170 if (SEQ_LT(tp
->snd_nxt
+ len
, tp
->snd_una
+ so
->so_snd
.sb_cc
))
173 win
= sbspace(&so
->so_rcv
);
176 * Sender silly window avoidance. If connection is idle
177 * and can send all data, a maximum segment,
178 * at least a maximum default-size segment do it,
179 * or are forced, do it; otherwise don't bother.
180 * If peer's buffer is tiny, then send
181 * when window is at least half open.
182 * If retransmitting (possibly after persist timer forced us
183 * to send into a small window), then must resend.
186 if (len
== tp
->t_maxseg
)
188 if ((1 || idle
|| tp
->t_flags
& TF_NODELAY
) &&
189 len
+ off
>= so
->so_snd
.sb_cc
)
193 if (len
>= tp
->max_sndwnd
/ 2 && tp
->max_sndwnd
> 0)
195 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
))
200 * Compare available window to amount of window
201 * known to peer (as advertised window less
202 * next expected input). If the difference is at least two
203 * max size segments, or at least 50% of the maximum possible
204 * window, then want to send a window update to peer.
208 * "adv" is the amount we can increase the window,
209 * taking into account that we are limited by
210 * TCP_MAXWIN << tp->rcv_scale.
212 long adv
= min(win
, (long)TCP_MAXWIN
<< tp
->rcv_scale
) -
213 (tp
->rcv_adv
- tp
->rcv_nxt
);
215 if (adv
>= (long) (2 * tp
->t_maxseg
))
217 if (2 * adv
>= (long) so
->so_rcv
.sb_datalen
)
222 * Send if we owe peer an ACK.
224 if (tp
->t_flags
& TF_ACKNOW
)
226 if (flags
& (TH_SYN
|TH_RST
))
228 if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
231 * If our state indicates that FIN should be sent
232 * and we have not yet done so, or we're retransmitting the FIN,
233 * then we need to send.
235 if (flags
& TH_FIN
&&
236 ((tp
->t_flags
& TF_SENTFIN
) == 0 || tp
->snd_nxt
== tp
->snd_una
))
240 * TCP window updates are not reliable, rather a polling protocol
241 * using ``persist'' packets is used to insure receipt of window
242 * updates. The three ``states'' for the output side are:
243 * idle not doing retransmits or persists
244 * persisting to move a small or zero window
245 * (re)transmitting and thereby not persisting
247 * tp->t_timer[TCPT_PERSIST]
248 * is set when we are in persist state.
250 * is set when we are called to send a persist packet.
251 * tp->t_timer[TCPT_REXMT]
252 * is set when we are retransmitting
253 * The output side is idle when both timers are zero.
255 * If send window is too small, there is data to transmit, and no
256 * retransmit or persist is pending, then go to persist state.
257 * If nothing happens soon, send when timer expires:
258 * if window is nonzero, transmit what we can,
259 * otherwise force out a byte.
261 if (so
->so_snd
.sb_cc
&& tp
->t_timer
[TCPT_REXMT
] == 0 &&
262 tp
->t_timer
[TCPT_PERSIST
] == 0) {
268 * No reason to send a segment, just return.
270 tcpstat
.tcps_didnuttin
++;
276 * Before ESTABLISHED, force sending of initial options
277 * unless TCP set not to do any options.
278 * NOTE: we assume that the IP/TCP header plus TCP options
279 * always fit in a single mbuf, leaving room for a maximum
281 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
284 hdrlen
= sizeof (struct tcpiphdr
);
285 if (flags
& TH_SYN
) {
286 tp
->snd_nxt
= tp
->iss
;
287 if ((tp
->t_flags
& TF_NOOPT
) == 0) {
290 opt
[0] = TCPOPT_MAXSEG
;
292 mss
= htons((u_int16_t
) tcp_mss(tp
, 0));
293 memcpy((caddr_t
)(opt
+ 2), (caddr_t
)&mss
, sizeof(mss
));
296 /* if ((tp->t_flags & TF_REQ_SCALE) &&
297 * ((flags & TH_ACK) == 0 ||
298 * (tp->t_flags & TF_RCVD_SCALE))) {
299 * *((u_int32_t *) (opt + optlen)) = htonl(
301 * TCPOPT_WINDOW << 16 |
302 * TCPOLEN_WINDOW << 8 |
303 * tp->request_r_scale);
311 * Send a timestamp and echo-reply if this is a SYN and our side
312 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
313 * and our peer have sent timestamps in our SYN's.
315 /* if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
316 * (flags & TH_RST) == 0 &&
317 * ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
318 * (tp->t_flags & TF_RCVD_TSTMP))) {
319 * u_int32_t *lp = (u_int32_t *)(opt + optlen);
321 * / * Form timestamp option as shown in appendix A of RFC 1323. * /
322 * *lp++ = htonl(TCPOPT_TSTAMP_HDR);
323 * *lp++ = htonl(tcp_now);
324 * *lp = htonl(tp->ts_recent);
325 * optlen += TCPOLEN_TSTAMP_APPA;
331 * Adjust data length if insertion of options will
332 * bump the packet length beyond the t_maxseg length.
334 if (len
> tp
->t_maxseg
- optlen
) {
335 len
= tp
->t_maxseg
- optlen
;
340 * Grab a header mbuf, attaching a copy of data to
341 * be transmitted, and initialize the header from
342 * the template for sends on this connection.
345 if (tp
->t_force
&& len
== 1)
346 tcpstat
.tcps_sndprobe
++;
347 else if (SEQ_LT(tp
->snd_nxt
, tp
->snd_max
)) {
348 tcpstat
.tcps_sndrexmitpack
++;
349 tcpstat
.tcps_sndrexmitbyte
+= len
;
351 tcpstat
.tcps_sndpack
++;
352 tcpstat
.tcps_sndbyte
+= len
;
357 /* error = ENOBUFS; */
361 m
->m_data
+= if_maxlinkhdr
;
365 * This will always succeed, since we make sure our mbufs
366 * are big enough to hold one MSS packet + header + ... etc.
368 /* if (len <= MHLEN - hdrlen - max_linkhdr) { */
370 sbcopy(&so
->so_snd
, off
, (int) len
, mtod(m
, caddr_t
) + hdrlen
);
374 * m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
375 * if (m->m_next == 0)
380 * If we're sending everything we've got, set PUSH.
381 * (This will keep happy those implementations which only
382 * give data to the user when a buffer fills or
385 if (off
+ len
== so
->so_snd
.sb_cc
)
388 if (tp
->t_flags
& TF_ACKNOW
)
389 tcpstat
.tcps_sndacks
++;
390 else if (flags
& (TH_SYN
|TH_FIN
|TH_RST
))
391 tcpstat
.tcps_sndctrl
++;
392 else if (SEQ_GT(tp
->snd_up
, tp
->snd_una
))
393 tcpstat
.tcps_sndurg
++;
395 tcpstat
.tcps_sndwinup
++;
399 /* error = ENOBUFS; */
403 m
->m_data
+= if_maxlinkhdr
;
407 ti
= mtod(m
, struct tcpiphdr
*);
409 memcpy((caddr_t
)ti
, &tp
->t_template
, sizeof (struct tcpiphdr
));
412 * Fill in fields, remembering maximum advertised
413 * window for use in delaying messages about window sizes.
414 * If resending a FIN, be sure not to use a new sequence number.
416 if (flags
& TH_FIN
&& tp
->t_flags
& TF_SENTFIN
&&
417 tp
->snd_nxt
== tp
->snd_max
)
420 * If we are doing retransmissions, then snd_nxt will
421 * not reflect the first unsent octet. For ACK only
422 * packets, we do not want the sequence number of the
423 * retransmitted packet, we want the sequence number
424 * of the next unsent octet. So, if there is no data
425 * (and no SYN or FIN), use snd_max instead of snd_nxt
426 * when filling in ti_seq. But if we are in persist
427 * state, snd_max might reflect one byte beyond the
428 * right edge of the window, so use snd_nxt in that
429 * case, since we know we aren't doing a retransmission.
430 * (retransmit and persist are mutually exclusive...)
432 if (len
|| (flags
& (TH_SYN
|TH_FIN
)) || tp
->t_timer
[TCPT_PERSIST
])
433 ti
->ti_seq
= htonl(tp
->snd_nxt
);
435 ti
->ti_seq
= htonl(tp
->snd_max
);
436 ti
->ti_ack
= htonl(tp
->rcv_nxt
);
438 memcpy((caddr_t
)(ti
+ 1), (caddr_t
)opt
, optlen
);
439 ti
->ti_off
= (sizeof (struct tcphdr
) + optlen
) >> 2;
441 ti
->ti_flags
= flags
;
443 * Calculate receive window. Don't shrink window,
444 * but avoid silly window syndrome.
446 if (win
< (long)(so
->so_rcv
.sb_datalen
/ 4) && win
< (long)tp
->t_maxseg
)
448 if (win
> (long)TCP_MAXWIN
<< tp
->rcv_scale
)
449 win
= (long)TCP_MAXWIN
<< tp
->rcv_scale
;
450 if (win
< (long)(tp
->rcv_adv
- tp
->rcv_nxt
))
451 win
= (long)(tp
->rcv_adv
- tp
->rcv_nxt
);
452 ti
->ti_win
= htons((u_int16_t
) (win
>>tp
->rcv_scale
));
454 if (SEQ_GT(tp
->snd_up
, tp
->snd_una
)) {
455 ti
->ti_urp
= htons((u_int16_t
)(tp
->snd_up
- ntohl(ti
->ti_seq
)));
457 if (SEQ_GT(tp
->snd_up
, tp
->snd_nxt
)) {
458 ti
->ti_urp
= htons((u_int16_t
)(tp
->snd_up
- tp
->snd_nxt
));
460 ti
->ti_flags
|= TH_URG
;
463 * If no urgent pointer to send, then we pull
464 * the urgent pointer to the left edge of the send window
465 * so that it doesn't drift into the send window on sequence
468 tp
->snd_up
= tp
->snd_una
; /* drag it along */
471 * Put TCP length in extended header, and then
472 * checksum extended header and data.
475 ti
->ti_len
= htons((u_int16_t
)(sizeof (struct tcphdr
) +
477 ti
->ti_sum
= cksum(m
, (int)(hdrlen
+ len
));
480 * In transmit state, time the transmission and arrange for
481 * the retransmit. In persist state, just set snd_max.
483 if (tp
->t_force
== 0 || tp
->t_timer
[TCPT_PERSIST
] == 0) {
484 tcp_seq startseq
= tp
->snd_nxt
;
487 * Advance snd_nxt over sequence space of this segment.
489 if (flags
& (TH_SYN
|TH_FIN
)) {
492 if (flags
& TH_FIN
) {
494 tp
->t_flags
|= TF_SENTFIN
;
498 if (SEQ_GT(tp
->snd_nxt
, tp
->snd_max
)) {
499 tp
->snd_max
= tp
->snd_nxt
;
501 * Time this transmission if not a retransmission and
502 * not currently timing anything.
504 if (tp
->t_rtt
== 0) {
506 tp
->t_rtseq
= startseq
;
507 tcpstat
.tcps_segstimed
++;
512 * Set retransmit timer if not currently set,
513 * and not doing an ack or a keep-alive probe.
514 * Initial value for retransmit timer is smoothed
515 * round-trip time + 2 * round-trip time variance.
516 * Initialize shift counter which is used for backoff
517 * of retransmit time.
519 if (tp
->t_timer
[TCPT_REXMT
] == 0 &&
520 tp
->snd_nxt
!= tp
->snd_una
) {
521 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
522 if (tp
->t_timer
[TCPT_PERSIST
]) {
523 tp
->t_timer
[TCPT_PERSIST
] = 0;
528 if (SEQ_GT(tp
->snd_nxt
+ len
, tp
->snd_max
))
529 tp
->snd_max
= tp
->snd_nxt
+ len
;
532 * Fill in IP length and desired time to live and
533 * send to IP level. There should be a better way
534 * to handle ttl and tos; we could keep them in
535 * the template, but need a way to checksum without them.
537 m
->m_len
= hdrlen
+ len
; /* XXX Needed? m_len should be correct */
541 ((struct ip
*)ti
)->ip_len
= m
->m_len
;
543 ((struct ip
*)ti
)->ip_ttl
= ip_defttl
;
544 ((struct ip
*)ti
)->ip_tos
= so
->so_iptos
;
547 /* Don't do IP options... */
548 /* error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
549 * so->so_options & SO_DONTROUTE, 0);
551 error
= ip_output(so
, m
);
554 * error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
555 * so->so_options & SO_DONTROUTE);
561 /* if (error == ENOBUFS) {
562 * tcp_quench(tp->t_inpcb, 0);
566 /* if ((error == EHOSTUNREACH || error == ENETDOWN)
567 * && TCPS_HAVERCVDSYN(tp->t_state)) {
568 * tp->t_softerror = error;
574 tcpstat
.tcps_sndtotal
++;
577 * Data sent (as far as we can tell).
578 * If this advertises a larger window than any other segment,
579 * then remember the size of the advertised window.
580 * Any pending ACK has now been sent.
582 if (win
> 0 && SEQ_GT(tp
->rcv_nxt
+win
, tp
->rcv_adv
))
583 tp
->rcv_adv
= tp
->rcv_nxt
+ win
;
584 tp
->last_ack_sent
= tp
->rcv_nxt
;
585 tp
->t_flags
&= ~(TF_ACKNOW
|TF_DELACK
);
594 register struct tcpcb
*tp
;
596 int t
= ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1;
598 /* if (tp->t_timer[TCPT_REXMT])
599 * panic("tcp_output REXMT");
602 * Start/restart persistence timer.
604 TCPT_RANGESET(tp
->t_timer
[TCPT_PERSIST
],
605 t
* tcp_backoff
[tp
->t_rxtshift
],
606 TCPTV_PERSMIN
, TCPTV_PERSMAX
);
607 if (tp
->t_rxtshift
< TCP_MAXRXTSHIFT
)