tcp_output.c revision 1.10 1 /* $NetBSD: tcp_output.c,v 1.10 1994/06/29 06:38:42 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)tcp_output.c 8.3 (Berkeley) 12/30/93
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/errno.h>
46
47 #include <net/route.h>
48
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/in_pcb.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/tcp.h>
55 #define TCPOUTFLAGS
56 #include <netinet/tcp_fsm.h>
57 #include <netinet/tcp_seq.h>
58 #include <netinet/tcp_timer.h>
59 #include <netinet/tcp_var.h>
60 #include <netinet/tcpip.h>
61 #include <netinet/tcp_debug.h>
62
63 #ifdef notyet
64 extern struct mbuf *m_copypack();
65 #endif
66
67
68 #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */
69
70 /*
71 * Tcp output routine: figure out what should be sent and send it.
72 */
73 int
74 tcp_output(tp)
75 register struct tcpcb *tp;
76 {
77 register struct socket *so = tp->t_inpcb->inp_socket;
78 register long len, win;
79 int off, flags, error;
80 register struct mbuf *m;
81 register struct tcpiphdr *ti;
82 u_char opt[MAX_TCPOPTLEN];
83 unsigned optlen, hdrlen;
84 int idle, sendalot;
85
86 /*
87 * Determine length of data that should be transmitted,
88 * and flags that will be used.
89 * If there is some data or critical controls (SYN, RST)
90 * to send, then transmit; otherwise, investigate further.
91 */
92 idle = (tp->snd_max == tp->snd_una);
93 if (idle && tp->t_idle >= tp->t_rxtcur)
94 /*
95 * We have been idle for "a while" and no acks are
96 * expected to clock out any data we send --
97 * slow start to get ack "clock" running again.
98 */
99 tp->snd_cwnd = tp->t_maxseg;
100 again:
101 sendalot = 0;
102 off = tp->snd_nxt - tp->snd_una;
103 win = min(tp->snd_wnd, tp->snd_cwnd);
104
105 flags = tcp_outflags[tp->t_state];
106 /*
107 * If in persist timeout with window of 0, send 1 byte.
108 * Otherwise, if window is small but nonzero
109 * and timer expired, we will send what we can
110 * and go to transmit state.
111 */
112 if (tp->t_force) {
113 if (win == 0) {
114 /*
115 * If we still have some data to send, then
116 * clear the FIN bit. Usually this would
117 * happen below when it realizes that we
118 * aren't sending all the data. However,
119 * if we have exactly 1 byte of unset data,
120 * then it won't clear the FIN bit below,
121 * and if we are in persist state, we wind
122 * up sending the packet without recording
123 * that we sent the FIN bit.
124 *
125 * We can't just blindly clear the FIN bit,
126 * because if we don't have any more data
127 * to send then the probe will be the FIN
128 * itself.
129 */
130 if (off < so->so_snd.sb_cc)
131 flags &= ~TH_FIN;
132 win = 1;
133 } else {
134 tp->t_timer[TCPT_PERSIST] = 0;
135 tp->t_rxtshift = 0;
136 }
137 }
138
139 len = min(so->so_snd.sb_cc, win) - off;
140
141 if (len < 0) {
142 /*
143 * If FIN has been sent but not acked,
144 * but we haven't been called to retransmit,
145 * len will be -1. Otherwise, window shrank
146 * after we sent into it. If window shrank to 0,
147 * cancel pending retransmit and pull snd_nxt
148 * back to (closed) window. We will enter persist
149 * state below. If the window didn't close completely,
150 * just wait for an ACK.
151 */
152 len = 0;
153 if (win == 0) {
154 tp->t_timer[TCPT_REXMT] = 0;
155 tp->snd_nxt = tp->snd_una;
156 }
157 }
158 if (len > tp->t_maxseg) {
159 len = tp->t_maxseg;
160 sendalot = 1;
161 }
162 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
163 flags &= ~TH_FIN;
164
165 win = sbspace(&so->so_rcv);
166
167 /*
168 * Sender silly window avoidance. If connection is idle
169 * and can send all data, a maximum segment,
170 * at least a maximum default-size segment do it,
171 * or are forced, do it; otherwise don't bother.
172 * If peer's buffer is tiny, then send
173 * when window is at least half open.
174 * If retransmitting (possibly after persist timer forced us
175 * to send into a small window), then must resend.
176 */
177 if (len) {
178 if (len == tp->t_maxseg)
179 goto send;
180 if ((idle || tp->t_flags & TF_NODELAY) &&
181 len + off >= so->so_snd.sb_cc)
182 goto send;
183 if (tp->t_force)
184 goto send;
185 if (len >= tp->max_sndwnd / 2)
186 goto send;
187 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
188 goto send;
189 }
190
191 /*
192 * Compare available window to amount of window
193 * known to peer (as advertised window less
194 * next expected input). If the difference is at least two
195 * max size segments, or at least 50% of the maximum possible
196 * window, then want to send a window update to peer.
197 */
198 if (win > 0) {
199 /*
200 * "adv" is the amount we can increase the window,
201 * taking into account that we are limited by
202 * TCP_MAXWIN << tp->rcv_scale.
203 */
204 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
205 (tp->rcv_adv - tp->rcv_nxt);
206
207 if (adv >= (long) (2 * tp->t_maxseg))
208 goto send;
209 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
210 goto send;
211 }
212
213 /*
214 * Send if we owe peer an ACK.
215 */
216 if (tp->t_flags & TF_ACKNOW)
217 goto send;
218 if (flags & (TH_SYN|TH_RST))
219 goto send;
220 if (SEQ_GT(tp->snd_up, tp->snd_una))
221 goto send;
222 /*
223 * If our state indicates that FIN should be sent
224 * and we have not yet done so, or we're retransmitting the FIN,
225 * then we need to send.
226 */
227 if (flags & TH_FIN &&
228 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
229 goto send;
230
231 /*
232 * TCP window updates are not reliable, rather a polling protocol
233 * using ``persist'' packets is used to insure receipt of window
234 * updates. The three ``states'' for the output side are:
235 * idle not doing retransmits or persists
236 * persisting to move a small or zero window
237 * (re)transmitting and thereby not persisting
238 *
239 * tp->t_timer[TCPT_PERSIST]
240 * is set when we are in persist state.
241 * tp->t_force
242 * is set when we are called to send a persist packet.
243 * tp->t_timer[TCPT_REXMT]
244 * is set when we are retransmitting
245 * The output side is idle when both timers are zero.
246 *
247 * If send window is too small, there is data to transmit, and no
248 * retransmit or persist is pending, then go to persist state.
249 * If nothing happens soon, send when timer expires:
250 * if window is nonzero, transmit what we can,
251 * otherwise force out a byte.
252 */
253 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
254 tp->t_timer[TCPT_PERSIST] == 0) {
255 tp->t_rxtshift = 0;
256 tcp_setpersist(tp);
257 }
258
259 /*
260 * No reason to send a segment, just return.
261 */
262 return (0);
263
264 send:
265 /*
266 * Before ESTABLISHED, force sending of initial options
267 * unless TCP set not to do any options.
268 * NOTE: we assume that the IP/TCP header plus TCP options
269 * always fit in a single mbuf, leaving room for a maximum
270 * link header, i.e.
271 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN
272 */
273 optlen = 0;
274 hdrlen = sizeof (struct tcpiphdr);
275 if (flags & TH_SYN) {
276 tp->snd_nxt = tp->iss;
277 if ((tp->t_flags & TF_NOOPT) == 0) {
278 u_short mss;
279
280 opt[0] = TCPOPT_MAXSEG;
281 opt[1] = 4;
282 mss = htons((u_short) tcp_mss(tp, 0));
283 bcopy((caddr_t)&mss, (caddr_t)(opt + 2), sizeof(mss));
284 optlen = 4;
285
286 if ((tp->t_flags & TF_REQ_SCALE) &&
287 ((flags & TH_ACK) == 0 ||
288 (tp->t_flags & TF_RCVD_SCALE))) {
289 *((u_long *) (opt + optlen)) = htonl(
290 TCPOPT_NOP << 24 |
291 TCPOPT_WINDOW << 16 |
292 TCPOLEN_WINDOW << 8 |
293 tp->request_r_scale);
294 optlen += 4;
295 }
296 }
297 }
298
299 /*
300 * Send a timestamp and echo-reply if this is a SYN and our side
301 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
302 * and our peer have sent timestamps in our SYN's.
303 */
304 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
305 (flags & TH_RST) == 0 &&
306 ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
307 (tp->t_flags & TF_RCVD_TSTMP))) {
308 u_long *lp = (u_long *)(opt + optlen);
309
310 /* Form timestamp option as shown in appendix A of RFC 1323. */
311 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
312 *lp++ = htonl(tcp_now);
313 *lp = htonl(tp->ts_recent);
314 optlen += TCPOLEN_TSTAMP_APPA;
315 }
316
317 hdrlen += optlen;
318
319 /*
320 * Adjust data length if insertion of options will
321 * bump the packet length beyond the t_maxseg length.
322 */
323 if (len > tp->t_maxseg - optlen) {
324 len = tp->t_maxseg - optlen;
325 sendalot = 1;
326 }
327
328
329 #ifdef DIAGNOSTIC
330 if (max_linkhdr + hdrlen > MHLEN)
331 panic("tcphdr too big");
332 #endif
333
334 /*
335 * Grab a header mbuf, attaching a copy of data to
336 * be transmitted, and initialize the header from
337 * the template for sends on this connection.
338 */
339 if (len) {
340 if (tp->t_force && len == 1)
341 tcpstat.tcps_sndprobe++;
342 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
343 tcpstat.tcps_sndrexmitpack++;
344 tcpstat.tcps_sndrexmitbyte += len;
345 } else {
346 tcpstat.tcps_sndpack++;
347 tcpstat.tcps_sndbyte += len;
348 }
349 #ifdef notyet
350 if ((m = m_copypack(so->so_snd.sb_mb, off,
351 (int)len, max_linkhdr + hdrlen)) == 0) {
352 error = ENOBUFS;
353 goto out;
354 }
355 /*
356 * m_copypack left space for our hdr; use it.
357 */
358 m->m_len += hdrlen;
359 m->m_data -= hdrlen;
360 #else
361 MGETHDR(m, M_DONTWAIT, MT_HEADER);
362 if (m == NULL) {
363 error = ENOBUFS;
364 goto out;
365 }
366 m->m_data += max_linkhdr;
367 m->m_len = hdrlen;
368 if (len <= MHLEN - hdrlen - max_linkhdr) {
369 m_copydata(so->so_snd.sb_mb, off, (int) len,
370 mtod(m, caddr_t) + hdrlen);
371 m->m_len += len;
372 } else {
373 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
374 if (m->m_next == 0)
375 len = 0;
376 }
377 #endif
378 /*
379 * If we're sending everything we've got, set PUSH.
380 * (This will keep happy those implementations which only
381 * give data to the user when a buffer fills or
382 * a PUSH comes in.)
383 */
384 if (off + len == so->so_snd.sb_cc)
385 flags |= TH_PUSH;
386 } else {
387 if (tp->t_flags & TF_ACKNOW)
388 tcpstat.tcps_sndacks++;
389 else if (flags & (TH_SYN|TH_FIN|TH_RST))
390 tcpstat.tcps_sndctrl++;
391 else if (SEQ_GT(tp->snd_up, tp->snd_una))
392 tcpstat.tcps_sndurg++;
393 else
394 tcpstat.tcps_sndwinup++;
395
396 MGETHDR(m, M_DONTWAIT, MT_HEADER);
397 if (m == NULL) {
398 error = ENOBUFS;
399 goto out;
400 }
401 m->m_data += max_linkhdr;
402 m->m_len = hdrlen;
403 }
404 m->m_pkthdr.rcvif = (struct ifnet *)0;
405 ti = mtod(m, struct tcpiphdr *);
406 if (tp->t_template == 0)
407 panic("tcp_output");
408 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr));
409
410 /*
411 * Fill in fields, remembering maximum advertised
412 * window for use in delaying messages about window sizes.
413 * If resending a FIN, be sure not to use a new sequence number.
414 */
415 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
416 tp->snd_nxt == tp->snd_max)
417 tp->snd_nxt--;
418 /*
419 * If we are doing retransmissions, then snd_nxt will
420 * not reflect the first unsent octet. For ACK only
421 * packets, we do not want the sequence number of the
422 * retransmitted packet, we want the sequence number
423 * of the next unsent octet. So, if there is no data
424 * (and no SYN or FIN), use snd_max instead of snd_nxt
425 * when filling in ti_seq. But if we are in persist
426 * state, snd_max might reflect one byte beyond the
427 * right edge of the window, so use snd_nxt in that
428 * case, since we know we aren't doing a retransmission.
429 * (retransmit and persist are mutually exclusive...)
430 */
431 if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
432 ti->ti_seq = htonl(tp->snd_nxt);
433 else
434 ti->ti_seq = htonl(tp->snd_max);
435 ti->ti_ack = htonl(tp->rcv_nxt);
436 if (optlen) {
437 bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen);
438 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2;
439 }
440 ti->ti_flags = flags;
441 /*
442 * Calculate receive window. Don't shrink window,
443 * but avoid silly window syndrome.
444 */
445 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
446 win = 0;
447 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
448 win = (long)TCP_MAXWIN << tp->rcv_scale;
449 if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
450 win = (long)(tp->rcv_adv - tp->rcv_nxt);
451 ti->ti_win = htons((u_short) (win>>tp->rcv_scale));
452 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
453 ti->ti_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
454 ti->ti_flags |= TH_URG;
455 } else
456 /*
457 * If no urgent pointer to send, then we pull
458 * the urgent pointer to the left edge of the send window
459 * so that it doesn't drift into the send window on sequence
460 * number wraparound.
461 */
462 tp->snd_up = tp->snd_una; /* drag it along */
463
464 /*
465 * Put TCP length in extended header, and then
466 * checksum extended header and data.
467 */
468 if (len + optlen)
469 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) +
470 optlen + len));
471 ti->ti_sum = in_cksum(m, (int)(hdrlen + len));
472
473 /*
474 * In transmit state, time the transmission and arrange for
475 * the retransmit. In persist state, just set snd_max.
476 */
477 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
478 tcp_seq startseq = tp->snd_nxt;
479
480 /*
481 * Advance snd_nxt over sequence space of this segment.
482 */
483 if (flags & (TH_SYN|TH_FIN)) {
484 if (flags & TH_SYN)
485 tp->snd_nxt++;
486 if (flags & TH_FIN) {
487 tp->snd_nxt++;
488 tp->t_flags |= TF_SENTFIN;
489 }
490 }
491 tp->snd_nxt += len;
492 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
493 tp->snd_max = tp->snd_nxt;
494 /*
495 * Time this transmission if not a retransmission and
496 * not currently timing anything.
497 */
498 if (tp->t_rtt == 0) {
499 tp->t_rtt = 1;
500 tp->t_rtseq = startseq;
501 tcpstat.tcps_segstimed++;
502 }
503 }
504
505 /*
506 * Set retransmit timer if not currently set,
507 * and not doing an ack or a keep-alive probe.
508 * Initial value for retransmit timer is smoothed
509 * round-trip time + 2 * round-trip time variance.
510 * Initialize shift counter which is used for backoff
511 * of retransmit time.
512 */
513 if (tp->t_timer[TCPT_REXMT] == 0 &&
514 tp->snd_nxt != tp->snd_una) {
515 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
516 if (tp->t_timer[TCPT_PERSIST]) {
517 tp->t_timer[TCPT_PERSIST] = 0;
518 tp->t_rxtshift = 0;
519 }
520 }
521 } else
522 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
523 tp->snd_max = tp->snd_nxt + len;
524
525 /*
526 * Trace.
527 */
528 if (so->so_options & SO_DEBUG)
529 tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0);
530
531 /*
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.
536 */
537 m->m_pkthdr.len = hdrlen + len;
538 #ifdef TUBA
539 if (tp->t_tuba_pcb)
540 error = tuba_output(m, tp);
541 else
542 #endif
543 {
544 ((struct ip *)ti)->ip_len = m->m_pkthdr.len;
545 ((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; /* XXX */
546 ((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos; /* XXX */
547 #if BSD >= 43
548 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
549 so->so_options & SO_DONTROUTE, 0);
550 #else
551 error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route,
552 so->so_options & SO_DONTROUTE);
553 #endif
554 }
555 if (error) {
556 out:
557 if (error == ENOBUFS) {
558 tcp_quench(tp->t_inpcb, 0);
559 return (0);
560 }
561 if ((error == EHOSTUNREACH || error == ENETDOWN)
562 && TCPS_HAVERCVDSYN(tp->t_state)) {
563 tp->t_softerror = error;
564 return (0);
565 }
566 return (error);
567 }
568 tcpstat.tcps_sndtotal++;
569
570 /*
571 * Data sent (as far as we can tell).
572 * If this advertises a larger window than any other segment,
573 * then remember the size of the advertised window.
574 * Any pending ACK has now been sent.
575 */
576 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
577 tp->rcv_adv = tp->rcv_nxt + win;
578 tp->last_ack_sent = tp->rcv_nxt;
579 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
580 if (sendalot)
581 goto again;
582 return (0);
583 }
584
585 void
586 tcp_setpersist(tp)
587 register struct tcpcb *tp;
588 {
589 register t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
590
591 if (tp->t_timer[TCPT_REXMT])
592 panic("tcp_output REXMT");
593 /*
594 * Start/restart persistance timer.
595 */
596 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
597 t * tcp_backoff[tp->t_rxtshift],
598 TCPTV_PERSMIN, TCPTV_PERSMAX);
599 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
600 tp->t_rxtshift++;
601 }
602