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