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