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