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