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