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