These are the current versions (actually already 2 years old).
[unix-history] / usr / src / sys / netinet / tcp_output.c
CommitLineData
8ae0e4b4 1/*
0880b18e 2 * Copyright (c) 1982, 1986 Regents of the University of California.
2b6b6284 3 * All rights reserved.
8ae0e4b4 4 *
2b6b6284 5 * Redistribution and use in source and binary forms are permitted
616d42db
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2b6b6284 16 *
616d42db 17 * @(#)tcp_output.c 7.17 (Berkeley) %G%
8ae0e4b4 18 */
76ee76df 19
20666ad3
JB
20#include "param.h"
21#include "systm.h"
22#include "mbuf.h"
23#include "protosw.h"
24#include "socket.h"
25#include "socketvar.h"
26#include "errno.h"
f4d55810 27
c124e997 28#include "../net/route.h"
f4d55810 29
20666ad3
JB
30#include "in.h"
31#include "in_pcb.h"
32#include "in_systm.h"
33#include "ip.h"
34#include "ip_var.h"
35#include "tcp.h"
0974b45c 36#define TCPOUTFLAGS
20666ad3
JB
37#include "tcp_fsm.h"
38#include "tcp_seq.h"
39#include "tcp_timer.h"
40#include "tcp_var.h"
41#include "tcpip.h"
42#include "tcp_debug.h"
76ee76df 43
8b5a83bb 44/*
77a4e3ca 45 * Initial options.
8b5a83bb 46 */
8b5a83bb 47u_char tcp_initopt[4] = { TCPOPT_MAXSEG, 4, 0x0, 0x0, };
8b5a83bb 48
ea727f86 49/*
4aed14e3 50 * Tcp output routine: figure out what should be sent and send it.
ea727f86 51 */
a6503abf 52tcp_output(tp)
53a5409e 53 register struct tcpcb *tp;
ea727f86 54{
53a5409e 55 register struct socket *so = tp->t_inpcb->inp_socket;
8129ec6e 56 register long len, win;
a6503abf 57 struct mbuf *m0;
acad00cc 58 int off, flags, error;
a6503abf
BJ
59 register struct mbuf *m;
60 register struct tcpiphdr *ti;
8b5a83bb
BJ
61 u_char *opt;
62 unsigned optlen = 0;
acad00cc 63 int idle, sendalot;
76ee76df 64
a6503abf 65 /*
8ae6c089 66 * Determine length of data that should be transmitted,
0974b45c
BJ
67 * and flags that will be used.
68 * If there is some data or critical controls (SYN, RST)
69 * to send, then transmit; otherwise, investigate further.
a6503abf 70 */
acad00cc 71 idle = (tp->snd_max == tp->snd_una);
2266a466
BJ
72again:
73 sendalot = 0;
a6503abf 74 off = tp->snd_nxt - tp->snd_una;
eaf69575 75 win = MIN(tp->snd_wnd, tp->snd_cwnd);
e4af65f3 76
eaf69575
MK
77 /*
78 * If in persist timeout with window of 0, send 1 byte.
acad00cc
MK
79 * Otherwise, if window is small but nonzero
80 * and timer expired, we will send what we can
81 * and go to transmit state.
eaf69575
MK
82 */
83 if (tp->t_force) {
e4af65f3 84 if (win == 0)
eaf69575
MK
85 win = 1;
86 else {
87 tp->t_timer[TCPT_PERSIST] = 0;
88 tp->t_rxtshift = 0;
89 }
90 }
acad00cc 91
8278ae69 92 len = MIN(so->so_snd.sb_cc, win) - off;
0974b45c 93 flags = tcp_outflags[tp->t_state];
e4af65f3
MK
94
95 if (len < 0) {
96 /*
dd0ed0b1
MK
97 * If FIN has been sent but not acked,
98 * but we haven't been called to retransmit,
449e69b4
MK
99 * len will be -1. Otherwise, window shrank
100 * after we sent into it. If window shrank to 0,
101 * cancel pending retransmit and pull snd_nxt
102 * back to (closed) window. We will enter persist
103 * state below. If the window didn't close completely,
dd0ed0b1 104 * just wait for an ACK.
e4af65f3 105 */
449e69b4
MK
106 len = 0;
107 if (win == 0) {
faa26a98
MK
108 tp->t_timer[TCPT_REXMT] = 0;
109 tp->snd_nxt = tp->snd_una;
449e69b4 110 }
faa26a98
MK
111 }
112 if (len > tp->t_maxseg) {
113 len = tp->t_maxseg;
2e5a76f2 114 sendalot = 1;
e4af65f3 115 }
baf677ce
MK
116 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
117 flags &= ~TH_FIN;
e4af65f3 118 win = sbspace(&so->so_rcv);
acad00cc 119
faa26a98
MK
120
121 /*
122 * If our state indicates that FIN should be sent
123 * and we have not yet done so, or we're retransmitting the FIN,
124 * then we need to send.
125 */
126 if (flags & TH_FIN &&
127 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
128 goto send;
acad00cc
MK
129 /*
130 * Send if we owe peer an ACK.
131 */
e4af65f3
MK
132 if (tp->t_flags & TF_ACKNOW)
133 goto send;
faa26a98 134 if (flags & (TH_SYN|TH_RST))
acad00cc 135 goto send;
8ae6c089 136 if (SEQ_GT(tp->snd_up, tp->snd_una))
a6503abf
BJ
137 goto send;
138
8ae6c089 139 /*
7d304adf
MK
140 * Sender silly window avoidance. If connection is idle
141 * and can send all data, a maximum segment,
142 * at least a maximum default-size segment do it,
8ae6c089 143 * or are forced, do it; otherwise don't bother.
18a438b6
MK
144 * If peer's buffer is tiny, then send
145 * when window is at least half open.
eaf69575
MK
146 * If retransmitting (possibly after persist timer forced us
147 * to send into a small window), then must resend.
8ae6c089
BJ
148 */
149 if (len) {
3d92549d 150 if (len == tp->t_maxseg)
8ae6c089 151 goto send;
acad00cc
MK
152 if ((idle || tp->t_flags & TF_NODELAY) &&
153 len + off >= so->so_snd.sb_cc)
8ae6c089
BJ
154 goto send;
155 if (tp->t_force)
156 goto send;
18a438b6
MK
157 if (len >= tp->max_sndwnd / 2)
158 goto send;
eaf69575
MK
159 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
160 goto send;
e4af65f3 161 }
76ee76df 162
a6503abf 163 /*
acad00cc
MK
164 * Compare available window to amount of window
165 * known to peer (as advertised window less
449e69b4
MK
166 * next expected input). If the difference is at least two
167 * max size segments or at least 35% of the maximum possible
168 * window, then want to send a window update to peer.
a6503abf 169 */
a6bbda13
MK
170 if (win > 0) {
171 int adv = win - (tp->rcv_adv - tp->rcv_nxt);
172
449e69b4 173 if (so->so_rcv.sb_cc == 0 && adv >= 2 * tp->t_maxseg)
a6bbda13 174 goto send;
449e69b4 175 if (100 * adv / so->so_rcv.sb_hiwat >= 35)
a6bbda13
MK
176 goto send;
177 }
a6503abf 178
2266a466
BJ
179 /*
180 * TCP window updates are not reliable, rather a polling protocol
181 * using ``persist'' packets is used to insure receipt of window
182 * updates. The three ``states'' for the output side are:
183 * idle not doing retransmits or persists
acad00cc 184 * persisting to move a small or zero window
2266a466
BJ
185 * (re)transmitting and thereby not persisting
186 *
187 * tp->t_timer[TCPT_PERSIST]
188 * is set when we are in persist state.
189 * tp->t_force
190 * is set when we are called to send a persist packet.
191 * tp->t_timer[TCPT_REXMT]
192 * is set when we are retransmitting
193 * The output side is idle when both timers are zero.
194 *
eaf69575
MK
195 * If send window is too small, there is data to transmit, and no
196 * retransmit or persist is pending, then go to persist state.
197 * If nothing happens soon, send when timer expires:
198 * if window is nonzero, transmit what we can,
199 * otherwise force out a byte.
2266a466 200 */
eaf69575
MK
201 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
202 tp->t_timer[TCPT_PERSIST] == 0) {
2266a466
BJ
203 tp->t_rxtshift = 0;
204 tcp_setpersist(tp);
205 }
206
a6503abf
BJ
207 /*
208 * No reason to send a segment, just return.
209 */
f1b2fa5b 210 return (0);
a6503abf
BJ
211
212send:
213 /*
214 * Grab a header mbuf, attaching a copy of data to
215 * be transmitted, and initialize the header from
216 * the template for sends on this connection.
217 */
60d68e9e
SL
218 MGET(m, M_DONTWAIT, MT_HEADER);
219 if (m == NULL)
8a2f82db 220 return (ENOBUFS);
8129ec6e
MK
221#define MAXLINKHDR 32 /* belongs elsewhere */
222#define DATASPACE (MMAXOFF - (MMINOFF + MAXLINKHDR + sizeof (struct tcpiphdr)))
223 m->m_off = MMINOFF + MAXLINKHDR;
53a5409e 224 m->m_len = sizeof (struct tcpiphdr);
8129ec6e 225 ti = mtod(m, struct tcpiphdr *);
a6503abf 226 if (len) {
3b52afc5
MK
227 if (tp->t_force && len == 1)
228 tcpstat.tcps_sndprobe++;
229 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
230 tcpstat.tcps_sndrexmitpack++;
231 tcpstat.tcps_sndrexmitbyte += len;
232 } else {
233 tcpstat.tcps_sndpack++;
234 tcpstat.tcps_sndbyte += len;
235 }
8129ec6e 236 if (len <= DATASPACE) {
9340d736 237 m_copydata(so->so_snd.sb_mb, off, (int) len,
8129ec6e
MK
238 mtod(m, caddr_t) + sizeof(struct tcpiphdr));
239 m->m_len += len;
240 } else {
9340d736 241 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
8129ec6e
MK
242 if (m->m_next == 0)
243 len = 0;
244 }
3b52afc5
MK
245 } else if (tp->t_flags & TF_ACKNOW)
246 tcpstat.tcps_sndacks++;
247 else if (flags & (TH_SYN|TH_FIN|TH_RST))
248 tcpstat.tcps_sndctrl++;
249 else if (SEQ_GT(tp->snd_up, tp->snd_una))
250 tcpstat.tcps_sndurg++;
251 else
252 tcpstat.tcps_sndwinup++;
253
a6503abf
BJ
254 if (tp->t_template == 0)
255 panic("tcp_output");
f1b2fa5b 256 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
a6503abf
BJ
257
258 /*
259 * Fill in fields, remembering maximum advertised
260 * window for use in delaying messages about window sizes.
faa26a98 261 * If resending a FIN, be sure not to use a new sequence number.
a6503abf 262 */
3d92549d
MK
263 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
264 tp->snd_nxt == tp->snd_max)
faa26a98 265 tp->snd_nxt--;
acad00cc
MK
266 ti->ti_seq = htonl(tp->snd_nxt);
267 ti->ti_ack = htonl(tp->rcv_nxt);
8b5a83bb
BJ
268 /*
269 * Before ESTABLISHED, force sending of initial options
270 * unless TCP set to not do any options.
271 */
acad00cc 272 opt = NULL;
1ccb6fcd 273 if (flags & TH_SYN && (tp->t_flags & TF_NOOPT) == 0) {
8011f5df 274 u_short mss;
99578149 275
99578149 276 mss = MIN(so->so_rcv.sb_hiwat / 2, tcp_mss(tp));
acad00cc
MK
277 if (mss > IP_MSS - sizeof(struct tcpiphdr)) {
278 opt = tcp_initopt;
279 optlen = sizeof (tcp_initopt);
280 *(u_short *)(opt + 2) = htons(mss);
281 }
8b5a83bb 282 }
77a4e3ca 283 if (opt) {
f1b2fa5b 284 m0 = m->m_next;
cce93e4b 285 m->m_next = m_get(M_DONTWAIT, MT_DATA);
0974b45c
BJ
286 if (m->m_next == 0) {
287 (void) m_free(m);
8b5a83bb 288 m_freem(m0);
8a2f82db 289 return (ENOBUFS);
0974b45c
BJ
290 }
291 m->m_next->m_next = m0;
8b5a83bb 292 m0 = m->m_next;
8b5a83bb 293 m0->m_len = optlen;
668cc26d 294 bcopy((caddr_t)opt, mtod(m0, caddr_t), optlen);
8b5a83bb 295 opt = (u_char *)(mtod(m0, caddr_t) + optlen);
8b5a83bb
BJ
296 while (m0->m_len & 0x3) {
297 *opt++ = TCPOPT_EOL;
298 m0->m_len++;
299 }
300 optlen = m0->m_len;
301 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
0974b45c
BJ
302 }
303 ti->ti_flags = flags;
acad00cc
MK
304 /*
305 * Calculate receive window. Don't shrink window,
306 * but avoid silly window syndrome.
307 */
6def2330 308 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
acad00cc 309 win = 0;
8129ec6e
MK
310 if (win > IP_MAXPACKET)
311 win = IP_MAXPACKET;
6def2330
MK
312 if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
313 win = (long)(tp->rcv_adv - tp->rcv_nxt);
dd861483
MK
314 if (win > IP_MAXPACKET)
315 win = IP_MAXPACKET;
acad00cc 316 ti->ti_win = htons((u_short)win);
0974b45c 317 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
8011f5df 318 ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
a6503abf
BJ
319 ti->ti_flags |= TH_URG;
320 } else
321 /*
322 * If no urgent pointer to send, then we pull
323 * the urgent pointer to the left edge of the send window
324 * so that it doesn't drift into the send window on sequence
325 * number wraparound.
326 */
0974b45c 327 tp->snd_up = tp->snd_una; /* drag it along */
02c1608b
BJ
328 /*
329 * If anything to send and we can send it all, set PUSH.
330 * (This will keep happy those implementations which only
5cdc4d65 331 * give data to the user when a buffer fills or a PUSH comes in.)
02c1608b 332 */
02c1608b
BJ
333 if (len && off+len == so->so_snd.sb_cc)
334 ti->ti_flags |= TH_PUSH;
a6503abf
BJ
335
336 /*
337 * Put TCP length in extended header, and then
338 * checksum extended header and data.
339 */
acad00cc
MK
340 if (len + optlen)
341 ti->ti_len = htons((u_short)(sizeof(struct tcphdr) +
342 optlen + len));
9340d736
MK
343 ti->ti_sum = in_cksum(m,
344 (int)(sizeof (struct tcpiphdr) + (int)optlen + len));
0974b45c
BJ
345
346 /*
2266a466 347 * In transmit state, time the transmission and arrange for
eaf69575 348 * the retransmit. In persist state, just set snd_max.
0974b45c 349 */
eaf69575 350 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
eb5508b2
MK
351 tcp_seq startseq = tp->snd_nxt;
352
2266a466 353 /*
8931cb5b 354 * Advance snd_nxt over sequence space of this segment.
2266a466 355 */
faa26a98
MK
356 if (flags & TH_SYN)
357 tp->snd_nxt++;
358 if (flags & TH_FIN) {
2266a466 359 tp->snd_nxt++;
faa26a98
MK
360 tp->t_flags |= TF_SENTFIN;
361 }
2266a466 362 tp->snd_nxt += len;
eb5508b2 363 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
e45e6858 364 tp->snd_max = tp->snd_nxt;
eb5508b2
MK
365 /*
366 * Time this transmission if not a retransmission and
367 * not currently timing anything.
368 */
369 if (tp->t_rtt == 0) {
370 tp->t_rtt = 1;
371 tp->t_rtseq = startseq;
372 tcpstat.tcps_segstimed++;
373 }
374 }
405c9168 375
2266a466 376 /*
eaf69575 377 * Set retransmit timer if not currently set,
6be9a225 378 * and not doing an ack or a keep-alive probe.
7cc62c26
MK
379 * Initial value for retransmit timer is smoothed
380 * round-trip time + 2 * round-trip time variance.
e4af65f3
MK
381 * Initialize shift counter which is used for backoff
382 * of retransmit time.
2266a466
BJ
383 */
384 if (tp->t_timer[TCPT_REXMT] == 0 &&
385 tp->snd_nxt != tp->snd_una) {
a6bbda13
MK
386 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
387 if (tp->t_timer[TCPT_PERSIST]) {
388 tp->t_timer[TCPT_PERSIST] = 0;
389 tp->t_rxtshift = 0;
390 }
2266a466 391 }
6be9a225 392 } else
acad00cc
MK
393 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
394 tp->snd_max = tp->snd_nxt + len;
a6503abf 395
f1dd32da
BJ
396 /*
397 * Trace.
398 */
8931cb5b 399 if (so->so_options & SO_DEBUG)
f1dd32da
BJ
400 tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
401
a6503abf
BJ
402 /*
403 * Fill in IP length and desired time to live and
404 * send to IP level.
405 */
8b5a83bb 406 ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + optlen + len;
a6503abf 407 ((struct ip *)ti)->ip_ttl = TCP_TTL;
9d866d2f 408#if BSD>=43
d55475b1
MK
409 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
410 so->so_options & SO_DONTROUTE);
9d866d2f
MK
411#else
412 error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
413 so->so_options & SO_DONTROUTE);
414#endif
83591b9d
MK
415 if (error) {
416 if (error == ENOBUFS) {
417 tcp_quench(tp->t_inpcb);
418 return (0);
419 }
8a2f82db 420 return (error);
83591b9d 421 }
3b52afc5 422 tcpstat.tcps_sndtotal++;
a6503abf
BJ
423
424 /*
425 * Data sent (as far as we can tell).
426 * If this advertises a larger window than any other segment,
4aed14e3 427 * then remember the size of the advertised window.
acad00cc 428 * Any pending ACK has now been sent.
a6503abf 429 */
be43ac7f 430 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
a6503abf 431 tp->rcv_adv = tp->rcv_nxt + win;
0974b45c 432 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
acad00cc 433 if (sendalot)
2266a466 434 goto again;
8a2f82db 435 return (0);
76ee76df 436}
2266a466
BJ
437
438tcp_setpersist(tp)
439 register struct tcpcb *tp;
440{
7cc62c26 441 register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
2266a466
BJ
442
443 if (tp->t_timer[TCPT_REXMT])
444 panic("tcp_output REXMT");
445 /*
446 * Start/restart persistance timer.
447 */
448 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
7cc62c26 449 t * tcp_backoff[tp->t_rxtshift],
3d92549d
MK
450 TCPTV_PERSMIN, TCPTV_PERSMAX);
451 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
452 tp->t_rxtshift++;
2266a466 453}