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