Make the LINT kernel compile with -W -Wreturn-type -Wcomment -Werror, and
[unix-history] / sys / netinet / tcp_output.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
3 * 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.
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.
20 *
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
31 * SUCH DAMAGE.
32 *
38e82238 33 * from: @(#)tcp_output.c 7.22 (Berkeley) 8/31/90
4c45483e 34 * $Id: tcp_output.c,v 1.2 1993/10/16 18:26:29 rgrimes Exp $
15637ed4
RG
35 */
36
37#include "param.h"
38#include "systm.h"
39#include "malloc.h"
40#include "mbuf.h"
41#include "protosw.h"
42#include "socket.h"
43#include "socketvar.h"
44#include "errno.h"
45
46#include "../net/route.h"
47
48#include "in.h"
49#include "in_systm.h"
50#include "ip.h"
51#include "in_pcb.h"
52#include "ip_var.h"
53#include "tcp.h"
54#define TCPOUTFLAGS
55#include "tcp_fsm.h"
56#include "tcp_seq.h"
57#include "tcp_timer.h"
58#include "tcp_var.h"
59#include "tcpip.h"
60#include "tcp_debug.h"
61
15637ed4
RG
62/*
63 * Initial options.
64 */
65u_char tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
66
67/*
68 * Tcp output routine: figure out what should be sent and send it.
69 */
4c45483e 70int
15637ed4
RG
71tcp_output(tp)
72 register struct tcpcb *tp;
73{
74 register struct socket *so = tp->t_inpcb->inp_socket;
75 register long len, win;
76 int off, flags, error;
77 register struct mbuf *m;
78 register struct tcpiphdr *ti;
4c45483e 79 u_char *opt = 0;
15637ed4
RG
80 unsigned optlen, hdrlen;
81 int idle, sendalot;
82
83 /*
84 * Determine length of data that should be transmitted,
85 * and flags that will be used.
86 * If there is some data or critical controls (SYN, RST)
87 * to send, then transmit; otherwise, investigate further.
88 */
89 idle = (tp->snd_max == tp->snd_una);
90 if (idle && tp->t_idle >= tp->t_rxtcur)
91 /*
92 * We have been idle for "a while" and no acks are
93 * expected to clock out any data we send --
94 * slow start to get ack "clock" running again.
95 */
96 tp->snd_cwnd = tp->t_maxseg;
97again:
98 sendalot = 0;
99 off = tp->snd_nxt - tp->snd_una;
100 win = min(tp->snd_wnd, tp->snd_cwnd);
101
102 /*
103 * If in persist timeout with window of 0, send 1 byte.
104 * Otherwise, if window is small but nonzero
105 * and timer expired, we will send what we can
106 * and go to transmit state.
107 */
108 if (tp->t_force) {
109 if (win == 0)
110 win = 1;
111 else {
112 tp->t_timer[TCPT_PERSIST] = 0;
113 tp->t_rxtshift = 0;
114 }
115 }
116
117 flags = tcp_outflags[tp->t_state];
118 len = min(so->so_snd.sb_cc, win) - off;
119
120 if (len < 0) {
121 /*
122 * If FIN has been sent but not acked,
123 * but we haven't been called to retransmit,
124 * len will be -1. Otherwise, window shrank
125 * after we sent into it. If window shrank to 0,
126 * cancel pending retransmit and pull snd_nxt
127 * back to (closed) window. We will enter persist
128 * state below. If the window didn't close completely,
129 * just wait for an ACK.
130 */
131 len = 0;
132 if (win == 0) {
133 tp->t_timer[TCPT_REXMT] = 0;
134 tp->snd_nxt = tp->snd_una;
135 }
136 }
137 if (len > tp->t_maxseg) {
138 len = tp->t_maxseg;
139 sendalot = 1;
140 }
141 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
142 flags &= ~TH_FIN;
143
144 win = sbspace(&so->so_rcv);
145
146 /*
147 * Sender silly window avoidance. If connection is idle
148 * and can send all data, a maximum segment,
149 * at least a maximum default-size segment do it,
150 * or are forced, do it; otherwise don't bother.
151 * If peer's buffer is tiny, then send
152 * when window is at least half open.
153 * If retransmitting (possibly after persist timer forced us
154 * to send into a small window), then must resend.
155 */
156 if (len) {
157 if (len == tp->t_maxseg)
158 goto send;
159 if ((idle || tp->t_flags & TF_NODELAY) &&
160 len + off >= so->so_snd.sb_cc)
161 goto send;
162 if (tp->t_force)
163 goto send;
164 if (len >= tp->max_sndwnd / 2)
165 goto send;
166 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
167 goto send;
168 }
169
170 /*
171 * Compare available window to amount of window
172 * known to peer (as advertised window less
173 * next expected input). If the difference is at least two
174 * max size segments, or at least 50% of the maximum possible
175 * window, then want to send a window update to peer.
176 */
177 if (win > 0) {
178 long adv = win - (tp->rcv_adv - tp->rcv_nxt);
179
180 if (adv >= (long) (2 * tp->t_maxseg))
181 goto send;
182 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
183 goto send;
184 }
185
186 /*
187 * Send if we owe peer an ACK.
188 */
189 if (tp->t_flags & TF_ACKNOW)
190 goto send;
191 if (flags & (TH_SYN|TH_RST))
192 goto send;
193 if (SEQ_GT(tp->snd_up, tp->snd_una))
194 goto send;
195 /*
196 * If our state indicates that FIN should be sent
197 * and we have not yet done so, or we're retransmitting the FIN,
198 * then we need to send.
199 */
200 if (flags & TH_FIN &&
201 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
202 goto send;
203
204 /*
205 * TCP window updates are not reliable, rather a polling protocol
206 * using ``persist'' packets is used to insure receipt of window
207 * updates. The three ``states'' for the output side are:
208 * idle not doing retransmits or persists
209 * persisting to move a small or zero window
210 * (re)transmitting and thereby not persisting
211 *
212 * tp->t_timer[TCPT_PERSIST]
213 * is set when we are in persist state.
214 * tp->t_force
215 * is set when we are called to send a persist packet.
216 * tp->t_timer[TCPT_REXMT]
217 * is set when we are retransmitting
218 * The output side is idle when both timers are zero.
219 *
220 * If send window is too small, there is data to transmit, and no
221 * retransmit or persist is pending, then go to persist state.
222 * If nothing happens soon, send when timer expires:
223 * if window is nonzero, transmit what we can,
224 * otherwise force out a byte.
225 */
226 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
227 tp->t_timer[TCPT_PERSIST] == 0) {
228 tp->t_rxtshift = 0;
229 tcp_setpersist(tp);
230 }
231
232 /*
233 * No reason to send a segment, just return.
234 */
235 return (0);
236
237send:
238 /*
239 * Before ESTABLISHED, force sending of initial options
240 * unless TCP set not to do any options.
241 * NOTE: we assume that the IP/TCP header plus TCP options
242 * always fit in a single mbuf, leaving room for a maximum
243 * link header, i.e.
244 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
245 */
246 optlen = 0;
247 hdrlen = sizeof (struct tcpiphdr);
248 if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) {
249 opt = tcp_initopt;
250 optlen = sizeof (tcp_initopt);
251 hdrlen += sizeof (tcp_initopt);
252 *(u_short *)(opt + 2) = htons((u_short) tcp_mss(tp, 0));
253#ifdef DIAGNOSTIC
254 if (max_linkhdr + hdrlen > MHLEN)
255 panic("tcphdr too big");
256#endif
257 }
258
259 /*
260 * Grab a header mbuf, attaching a copy of data to
261 * be transmitted, and initialize the header from
262 * the template for sends on this connection.
263 */
264 if (len) {
265 if (tp->t_force && len == 1)
266 tcpstat.tcps_sndprobe++;
267 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
268 tcpstat.tcps_sndrexmitpack++;
269 tcpstat.tcps_sndrexmitbyte += len;
270 } else {
271 tcpstat.tcps_sndpack++;
272 tcpstat.tcps_sndbyte += len;
273 }
274#ifdef notyet
275 if ((m = m_copypack(so->so_snd.sb_mb, off,
276 (int)len, max_linkhdr + hdrlen)) == 0) {
277 error = ENOBUFS;
278 goto out;
279 }
280 /*
281 * m_copypack left space for our hdr; use it.
282 */
283 m->m_len += hdrlen;
284 m->m_data -= hdrlen;
285#else
286 MGETHDR(m, M_DONTWAIT, MT_HEADER);
287 if (m == NULL) {
288 error = ENOBUFS;
289 goto out;
290 }
291 m->m_data += max_linkhdr;
292 m->m_len = hdrlen;
293 if (len <= MHLEN - hdrlen - max_linkhdr) {
294 m_copydata(so->so_snd.sb_mb, off, (int) len,
295 mtod(m, caddr_t) + hdrlen);
296 m->m_len += len;
297 } else {
298 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
299 if (m->m_next == 0)
300 len = 0;
301 }
302#endif
303 /*
304 * If we're sending everything we've got, set PUSH.
305 * (This will keep happy those implementations which only
306 * give data to the user when a buffer fills or
307 * a PUSH comes in.)
308 */
309 if (off + len == so->so_snd.sb_cc)
310 flags |= TH_PUSH;
311 } else {
312 if (tp->t_flags & TF_ACKNOW)
313 tcpstat.tcps_sndacks++;
314 else if (flags & (TH_SYN|TH_FIN|TH_RST))
315 tcpstat.tcps_sndctrl++;
316 else if (SEQ_GT(tp->snd_up, tp->snd_una))
317 tcpstat.tcps_sndurg++;
318 else
319 tcpstat.tcps_sndwinup++;
320
321 MGETHDR(m, M_DONTWAIT, MT_HEADER);
322 if (m == NULL) {
323 error = ENOBUFS;
324 goto out;
325 }
326 m->m_data += max_linkhdr;
327 m->m_len = hdrlen;
328 }
329 m->m_pkthdr.rcvif = (struct ifnet *)0;
330 ti = mtod(m, struct tcpiphdr *);
331 if (tp->t_template == 0)
332 panic("tcp_output");
333 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
334
335 /*
336 * Fill in fields, remembering maximum advertised
337 * window for use in delaying messages about window sizes.
338 * If resending a FIN, be sure not to use a new sequence number.
339 */
340 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
341 tp->snd_nxt == tp->snd_max)
342 tp->snd_nxt--;
343 ti->ti_seq = htonl(tp->snd_nxt);
344 ti->ti_ack = htonl(tp->rcv_nxt);
345 if (optlen) {
346 bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
347 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
348 }
349 ti->ti_flags = flags;
350 /*
351 * Calculate receive window. Don't shrink window,
352 * but avoid silly window syndrome.
353 */
354 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
355 win = 0;
356 if (win > TCP_MAXWIN)
357 win = TCP_MAXWIN;
358 if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
359 win = (long)(tp->rcv_adv - tp->rcv_nxt);
360 ti->ti_win = htons((u_short)win);
361 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
362 ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
363 ti->ti_flags |= TH_URG;
364 } else
365 /*
366 * If no urgent pointer to send, then we pull
367 * the urgent pointer to the left edge of the send window
368 * so that it doesn't drift into the send window on sequence
369 * number wraparound.
370 */
371 tp->snd_up = tp->snd_una; /* drag it along */
372
373 /*
374 * Put TCP length in extended header, and then
375 * checksum extended header and data.
376 */
377 if (len + optlen)
378 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
379 optlen + len));
380 ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
381
382 /*
383 * In transmit state, time the transmission and arrange for
384 * the retransmit. In persist state, just set snd_max.
385 */
386 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
387 tcp_seq startseq = tp->snd_nxt;
388
389 /*
390 * Advance snd_nxt over sequence space of this segment.
391 */
392 if (flags & (TH_SYN|TH_FIN)) {
393 if (flags & TH_SYN)
394 tp->snd_nxt++;
395 if (flags & TH_FIN) {
396 tp->snd_nxt++;
397 tp->t_flags |= TF_SENTFIN;
398 }
399 }
400 tp->snd_nxt += len;
401 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
402 tp->snd_max = tp->snd_nxt;
403 /*
404 * Time this transmission if not a retransmission and
405 * not currently timing anything.
406 */
407 if (tp->t_rtt == 0) {
408 tp->t_rtt = 1;
409 tp->t_rtseq = startseq;
410 tcpstat.tcps_segstimed++;
411 }
412 }
413
414 /*
415 * Set retransmit timer if not currently set,
416 * and not doing an ack or a keep-alive probe.
417 * Initial value for retransmit timer is smoothed
418 * round-trip time + 2 * round-trip time variance.
419 * Initialize shift counter which is used for backoff
420 * of retransmit time.
421 */
422 if (tp->t_timer[TCPT_REXMT] == 0 &&
423 tp->snd_nxt != tp->snd_una) {
424 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
425 if (tp->t_timer[TCPT_PERSIST]) {
426 tp->t_timer[TCPT_PERSIST] = 0;
427 tp->t_rxtshift = 0;
428 }
429 }
430 } else
431 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
432 tp->snd_max = tp->snd_nxt + len;
433
434 /*
435 * Trace.
436 */
437 if (so->so_options & SO_DEBUG)
438 tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
439
440 /*
441 * Fill in IP length and desired time to live and
442 * send to IP level. There should be a better way
443 * to handle ttl and tos; we could keep them in
444 * the template, but need a way to checksum without them.
445 */
446 m->m_pkthdr.len = hdrlen + len;
447 ((struct ip *)ti)->ip_len = m->m_pkthdr.len;
448 ((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; /* XXX */
449 ((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos; /* XXX */
450#if BSD >= 43
451 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
452 so->so_options & SO_DONTROUTE);
453#else
454 error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
455 so->so_options & SO_DONTROUTE);
456#endif
457 if (error) {
458out:
459 if (error == ENOBUFS) {
4c45483e 460 tcp_quench(tp->t_inpcb, 0);
15637ed4
RG
461 return (0);
462 }
463 if ((error == EHOSTUNREACH || error == ENETDOWN)
464 && TCPS_HAVERCVDSYN(tp->t_state)) {
465 tp->t_softerror = error;
466 return (0);
467 }
468 return (error);
469 }
470 tcpstat.tcps_sndtotal++;
471
472 /*
473 * Data sent (as far as we can tell).
474 * If this advertises a larger window than any other segment,
475 * then remember the size of the advertised window.
476 * Any pending ACK has now been sent.
477 */
478 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
479 tp->rcv_adv = tp->rcv_nxt + win;
480 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
481 if (sendalot)
482 goto again;
483 return (0);
484}
485
4c45483e 486void
15637ed4
RG
487tcp_setpersist(tp)
488 register struct tcpcb *tp;
489{
490 register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
491
492 if (tp->t_timer[TCPT_REXMT])
493 panic("tcp_output REXMT");
494 /*
495 * Start/restart persistance timer.
496 */
497 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
498 t * tcp_backoff[tp->t_rxtshift],
499 TCPTV_PERSMIN, TCPTV_PERSMAX);
500 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
501 tp->t_rxtshift++;
502}