1 /* $NetBSD: tcp_output.c,v 1.34 1998/04/13 21:18:19 kml Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation 9 * Facility, NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 42 * The Regents of the University of California. All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95 73 */ 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/malloc.h> 78 #include <sys/mbuf.h> 79 #include <sys/protosw.h> 80 #include <sys/socket.h> 81 #include <sys/socketvar.h> 82 #include <sys/errno.h> 83 84 #include <net/if.h> 85 #include <net/route.h> 86 87 #include <netinet/in.h> 88 #include <netinet/in_systm.h> 89 #include <netinet/ip.h> 90 #include <netinet/in_pcb.h> 91 #include <netinet/ip_var.h> 92 #include <netinet/tcp.h> 93 #define TCPOUTFLAGS 94 #include <netinet/tcp_fsm.h> 95 #include <netinet/tcp_seq.h> 96 #include <netinet/tcp_timer.h> 97 #include <netinet/tcp_var.h> 98 #include <netinet/tcpip.h> 99 #include <netinet/tcp_debug.h> 100 101 #ifdef TUBA 102 #include <netiso/iso.h> 103 #include <netiso/tuba_table.h> 104 #endif 105 106 #ifdef notyet 107 extern struct mbuf *m_copypack(); 108 #endif 109 110 #define MAX_TCPOPTLEN 32 /* max # bytes that go in options */ 111 112 /* 113 * Knob to enable Congestion Window Monitoring. 114 */ 115 #ifdef TCP_CWM 116 int tcp_cwm = 1; 117 #else 118 int tcp_cwm = 0; 119 #endif 120 121 static __inline void tcp_segsize __P((struct tcpcb *, int *, int *)); 122 static __inline void 123 tcp_segsize(tp, txsegsizep, rxsegsizep) 124 struct tcpcb *tp; 125 int *txsegsizep, *rxsegsizep; 126 { 127 struct inpcb *inp = tp->t_inpcb; 128 struct rtentry *rt; 129 struct ifnet *ifp; 130 int size; 131 132 if ((rt = in_pcbrtentry(inp)) == NULL) { 133 size = tcp_mssdflt; 134 goto out; 135 } 136 137 ifp = rt->rt_ifp; 138 139 if (rt->rt_rmx.rmx_mtu != 0) 140 size = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 141 else if (ip_mtudisc || in_localaddr(inp->inp_faddr) || 142 ifp->if_flags & IFF_LOOPBACK) 143 size = ifp->if_mtu - sizeof(struct tcpiphdr); 144 else 145 size = tcp_mssdflt; 146 size -= (tcp_optlen(tp) + ip_optlen(tp->t_inpcb)); 147 148 out: 149 *txsegsizep = min(tp->t_peermss, size); 150 *rxsegsizep = min(tp->t_ourmss, size); 151 152 if (*txsegsizep != tp->t_segsz) { 153 /* 154 * XXX: should we check to make sure these are all 155 * at least txsegsize in length, now? 156 */ 157 tp->snd_cwnd = (tp->snd_cwnd / tp->t_segsz) * *txsegsizep; 158 tp->snd_ssthresh = (tp->snd_ssthresh / tp->t_segsz) * 159 *txsegsizep; 160 tp->t_segsz = *txsegsizep; 161 } 162 } 163 164 /* 165 * Tcp output routine: figure out what should be sent and send it. 166 */ 167 int 168 tcp_output(tp) 169 register struct tcpcb *tp; 170 { 171 register struct socket *so = tp->t_inpcb->inp_socket; 172 register long len, win; 173 int off, flags, error; 174 register struct mbuf *m; 175 register struct tcpiphdr *ti; 176 u_char opt[MAX_TCPOPTLEN]; 177 unsigned optlen, hdrlen; 178 int idle, sendalot, txsegsize, rxsegsize; 179 180 tcp_segsize(tp, &txsegsize, &rxsegsize); 181 182 idle = (tp->snd_max == tp->snd_una); 183 184 if (tcp_cwm) { 185 /* 186 * Hughes/Touch/Heidemann Congestion Window Monitoring. 187 * Count the number of packets currently pending 188 * acknowledgement, and limit our congestion window 189 * to the initial congestion window plus that count. 190 * This prevents bursting once all pending packets have 191 * been acknowledged (i.e. transmission is idle). 192 */ 193 tp->snd_cwnd = min(tp->snd_cwnd, 194 TCP_INITIAL_WINDOW(tcp_init_win, txsegsize) + 195 (tp->snd_nxt - tp->snd_una)); 196 } else { 197 if (idle && tp->t_idle >= tp->t_rxtcur) { 198 /* 199 * We have been idle for "a while" and no acks are 200 * expected to clock out any data we send -- 201 * slow start to get ack "clock" running again. 202 */ 203 tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win, 204 txsegsize); 205 } 206 } 207 208 again: 209 /* 210 * Determine length of data that should be transmitted, and 211 * flags that should be used. If there is some data or critical 212 * controls (SYN, RST) to send, then transmit; otherwise, 213 * investigate further. 214 */ 215 sendalot = 0; 216 off = tp->snd_nxt - tp->snd_una; 217 win = min(tp->snd_wnd, tp->snd_cwnd); 218 219 flags = tcp_outflags[tp->t_state]; 220 /* 221 * If in persist timeout with window of 0, send 1 byte. 222 * Otherwise, if window is small but nonzero 223 * and timer expired, we will send what we can 224 * and go to transmit state. 225 */ 226 if (tp->t_force) { 227 if (win == 0) { 228 /* 229 * If we still have some data to send, then 230 * clear the FIN bit. Usually this would 231 * happen below when it realizes that we 232 * aren't sending all the data. However, 233 * if we have exactly 1 byte of unset data, 234 * then it won't clear the FIN bit below, 235 * and if we are in persist state, we wind 236 * up sending the packet without recording 237 * that we sent the FIN bit. 238 * 239 * We can't just blindly clear the FIN bit, 240 * because if we don't have any more data 241 * to send then the probe will be the FIN 242 * itself. 243 */ 244 if (off < so->so_snd.sb_cc) 245 flags &= ~TH_FIN; 246 win = 1; 247 } else { 248 tp->t_timer[TCPT_PERSIST] = 0; 249 tp->t_rxtshift = 0; 250 } 251 } 252 253 if (win < so->so_snd.sb_cc) { 254 len = win - off; 255 flags &= ~TH_FIN; 256 } else 257 len = so->so_snd.sb_cc - off; 258 259 if (len < 0) { 260 /* 261 * If FIN has been sent but not acked, 262 * but we haven't been called to retransmit, 263 * len will be -1. Otherwise, window shrank 264 * after we sent into it. If window shrank to 0, 265 * cancel pending retransmit, pull snd_nxt back 266 * to (closed) window, and set the persist timer 267 * if it isn't already going. If the window didn't 268 * close completely, just wait for an ACK. 269 */ 270 len = 0; 271 if (win == 0) { 272 tp->t_timer[TCPT_REXMT] = 0; 273 tp->t_rxtshift = 0; 274 tp->snd_nxt = tp->snd_una; 275 if (tp->t_timer[TCPT_PERSIST] == 0) 276 tcp_setpersist(tp); 277 } 278 } 279 if (len > txsegsize) { 280 len = txsegsize; 281 flags &= ~TH_FIN; 282 sendalot = 1; 283 } 284 285 win = sbspace(&so->so_rcv); 286 287 /* 288 * Sender silly window avoidance. If connection is idle 289 * and can send all data, a maximum segment, 290 * at least a maximum default-size segment do it, 291 * or are forced, do it; otherwise don't bother. 292 * If peer's buffer is tiny, then send 293 * when window is at least half open. 294 * If retransmitting (possibly after persist timer forced us 295 * to send into a small window), then must resend. 296 */ 297 if (len) { 298 if (len == txsegsize) 299 goto send; 300 if ((idle || tp->t_flags & TF_NODELAY) && 301 len + off >= so->so_snd.sb_cc) 302 goto send; 303 if (tp->t_force) 304 goto send; 305 if (len >= tp->max_sndwnd / 2) 306 goto send; 307 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 308 goto send; 309 } 310 311 /* 312 * Compare available window to amount of window known to peer 313 * (as advertised window less next expected input). If the 314 * difference is at least twice the size of the largest segment 315 * we expect to receive (i.e. two segments) or at least 50% of 316 * the maximum possible window, then want to send a window update 317 * to peer. 318 */ 319 if (win > 0) { 320 /* 321 * "adv" is the amount we can increase the window, 322 * taking into account that we are limited by 323 * TCP_MAXWIN << tp->rcv_scale. 324 */ 325 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) - 326 (tp->rcv_adv - tp->rcv_nxt); 327 328 if (adv >= (long) (2 * rxsegsize)) 329 goto send; 330 if (2 * adv >= (long) so->so_rcv.sb_hiwat) 331 goto send; 332 } 333 334 /* 335 * Send if we owe peer an ACK. 336 */ 337 if (tp->t_flags & TF_ACKNOW) 338 goto send; 339 if (flags & (TH_SYN|TH_RST)) 340 goto send; 341 if (SEQ_GT(tp->snd_up, tp->snd_una)) 342 goto send; 343 /* 344 * If our state indicates that FIN should be sent 345 * and we have not yet done so, or we're retransmitting the FIN, 346 * then we need to send. 347 */ 348 if (flags & TH_FIN && 349 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 350 goto send; 351 352 /* 353 * TCP window updates are not reliable, rather a polling protocol 354 * using ``persist'' packets is used to insure receipt of window 355 * updates. The three ``states'' for the output side are: 356 * idle not doing retransmits or persists 357 * persisting to move a small or zero window 358 * (re)transmitting and thereby not persisting 359 * 360 * tp->t_timer[TCPT_PERSIST] 361 * is set when we are in persist state. 362 * tp->t_force 363 * is set when we are called to send a persist packet. 364 * tp->t_timer[TCPT_REXMT] 365 * is set when we are retransmitting 366 * The output side is idle when both timers are zero. 367 * 368 * If send window is too small, there is data to transmit, and no 369 * retransmit or persist is pending, then go to persist state. 370 * If nothing happens soon, send when timer expires: 371 * if window is nonzero, transmit what we can, 372 * otherwise force out a byte. 373 */ 374 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && 375 tp->t_timer[TCPT_PERSIST] == 0) { 376 tp->t_rxtshift = 0; 377 tcp_setpersist(tp); 378 } 379 380 /* 381 * No reason to send a segment, just return. 382 */ 383 return (0); 384 385 send: 386 /* 387 * Before ESTABLISHED, force sending of initial options 388 * unless TCP set not to do any options. 389 * NOTE: we assume that the IP/TCP header plus TCP options 390 * always fit in a single mbuf, leaving room for a maximum 391 * link header, i.e. 392 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MHLEN 393 */ 394 optlen = 0; 395 hdrlen = sizeof (struct tcpiphdr); 396 if (flags & TH_SYN) { 397 struct rtentry *rt = in_pcbrtentry(tp->t_inpcb); 398 399 tp->snd_nxt = tp->iss; 400 tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ? 401 rt->rt_ifp : NULL); 402 if ((tp->t_flags & TF_NOOPT) == 0) { 403 opt[0] = TCPOPT_MAXSEG; 404 opt[1] = 4; 405 opt[2] = (tp->t_ourmss >> 8) & 0xff; 406 opt[3] = tp->t_ourmss & 0xff; 407 optlen = 4; 408 409 if ((tp->t_flags & TF_REQ_SCALE) && 410 ((flags & TH_ACK) == 0 || 411 (tp->t_flags & TF_RCVD_SCALE))) { 412 *((u_int32_t *) (opt + optlen)) = htonl( 413 TCPOPT_NOP << 24 | 414 TCPOPT_WINDOW << 16 | 415 TCPOLEN_WINDOW << 8 | 416 tp->request_r_scale); 417 optlen += 4; 418 } 419 } 420 } 421 422 /* 423 * Send a timestamp and echo-reply if this is a SYN and our side 424 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 425 * and our peer have sent timestamps in our SYN's. 426 */ 427 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 428 (flags & TH_RST) == 0 && 429 ((flags & (TH_SYN|TH_ACK)) == TH_SYN || 430 (tp->t_flags & TF_RCVD_TSTMP))) { 431 u_int32_t *lp = (u_int32_t *)(opt + optlen); 432 433 /* Form timestamp option as shown in appendix A of RFC 1323. */ 434 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 435 *lp++ = htonl(tcp_now); 436 *lp = htonl(tp->ts_recent); 437 optlen += TCPOLEN_TSTAMP_APPA; 438 } 439 440 hdrlen += optlen; 441 442 #ifdef DIAGNOSTIC 443 if (len > txsegsize) 444 panic("tcp data to be sent is larger than segment"); 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 (void) m_freem(m); 491 error = ENOBUFS; 492 goto out; 493 } 494 } 495 #endif 496 /* 497 * If we're sending everything we've got, set PUSH. 498 * (This will keep happy those implementations which only 499 * give data to the user when a buffer fills or 500 * a PUSH comes in.) 501 */ 502 if (off + len == so->so_snd.sb_cc) 503 flags |= TH_PUSH; 504 } else { 505 if (tp->t_flags & TF_ACKNOW) 506 tcpstat.tcps_sndacks++; 507 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 508 tcpstat.tcps_sndctrl++; 509 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 510 tcpstat.tcps_sndurg++; 511 else 512 tcpstat.tcps_sndwinup++; 513 514 MGETHDR(m, M_DONTWAIT, MT_HEADER); 515 if (m == NULL) { 516 error = ENOBUFS; 517 goto out; 518 } 519 m->m_data += max_linkhdr; 520 m->m_len = hdrlen; 521 } 522 m->m_pkthdr.rcvif = (struct ifnet *)0; 523 ti = mtod(m, struct tcpiphdr *); 524 if (tp->t_template == 0) 525 panic("tcp_output"); 526 bcopy((caddr_t)tp->t_template, (caddr_t)ti, sizeof (struct tcpiphdr)); 527 528 /* 529 * Fill in fields, remembering maximum advertised 530 * window for use in delaying messages about window sizes. 531 * If resending a FIN, be sure not to use a new sequence number. 532 */ 533 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 534 tp->snd_nxt == tp->snd_max) 535 tp->snd_nxt--; 536 /* 537 * If we are doing retransmissions, then snd_nxt will 538 * not reflect the first unsent octet. For ACK only 539 * packets, we do not want the sequence number of the 540 * retransmitted packet, we want the sequence number 541 * of the next unsent octet. So, if there is no data 542 * (and no SYN or FIN), use snd_max instead of snd_nxt 543 * when filling in ti_seq. But if we are in persist 544 * state, snd_max might reflect one byte beyond the 545 * right edge of the window, so use snd_nxt in that 546 * case, since we know we aren't doing a retransmission. 547 * (retransmit and persist are mutually exclusive...) 548 */ 549 if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST]) 550 ti->ti_seq = htonl(tp->snd_nxt); 551 else 552 ti->ti_seq = htonl(tp->snd_max); 553 ti->ti_ack = htonl(tp->rcv_nxt); 554 if (optlen) { 555 bcopy((caddr_t)opt, (caddr_t)(ti + 1), optlen); 556 ti->ti_off = (sizeof (struct tcphdr) + optlen) >> 2; 557 } 558 ti->ti_flags = flags; 559 /* 560 * Calculate receive window. Don't shrink window, 561 * but avoid silly window syndrome. 562 */ 563 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize) 564 win = 0; 565 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 566 win = (long)TCP_MAXWIN << tp->rcv_scale; 567 if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) 568 win = (long)(tp->rcv_adv - tp->rcv_nxt); 569 ti->ti_win = htons((u_int16_t) (win>>tp->rcv_scale)); 570 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 571 u_int32_t urp = tp->snd_up - tp->snd_nxt; 572 if (urp > IP_MAXPACKET) 573 urp = IP_MAXPACKET; 574 ti->ti_urp = htons((u_int16_t)urp); 575 ti->ti_flags |= TH_URG; 576 } else 577 /* 578 * If no urgent pointer to send, then we pull 579 * the urgent pointer to the left edge of the send window 580 * so that it doesn't drift into the send window on sequence 581 * number wraparound. 582 */ 583 tp->snd_up = tp->snd_una; /* drag it along */ 584 585 /* 586 * Put TCP length in extended header, and then 587 * checksum extended header and data. 588 */ 589 if (len + optlen) 590 ti->ti_len = htons((u_int16_t)(sizeof (struct tcphdr) + 591 optlen + len)); 592 ti->ti_sum = in_cksum(m, (int)(hdrlen + len)); 593 594 /* 595 * In transmit state, time the transmission and arrange for 596 * the retransmit. In persist state, just set snd_max. 597 */ 598 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { 599 tcp_seq startseq = tp->snd_nxt; 600 601 /* 602 * Advance snd_nxt over sequence space of this segment. 603 */ 604 if (flags & (TH_SYN|TH_FIN)) { 605 if (flags & TH_SYN) 606 tp->snd_nxt++; 607 if (flags & TH_FIN) { 608 tp->snd_nxt++; 609 tp->t_flags |= TF_SENTFIN; 610 } 611 } 612 tp->snd_nxt += len; 613 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 614 tp->snd_max = tp->snd_nxt; 615 /* 616 * Time this transmission if not a retransmission and 617 * not currently timing anything. 618 */ 619 if (tp->t_rtt == 0) { 620 tp->t_rtt = 1; 621 tp->t_rtseq = startseq; 622 tcpstat.tcps_segstimed++; 623 } 624 } 625 626 /* 627 * Set retransmit timer if not currently set, 628 * and not doing an ack or a keep-alive probe. 629 * Initial value for retransmit timer is smoothed 630 * round-trip time + 2 * round-trip time variance. 631 * Initialize shift counter which is used for backoff 632 * of retransmit time. 633 */ 634 if (tp->t_timer[TCPT_REXMT] == 0 && 635 tp->snd_nxt != tp->snd_una) { 636 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 637 if (tp->t_timer[TCPT_PERSIST]) { 638 tp->t_timer[TCPT_PERSIST] = 0; 639 tp->t_rxtshift = 0; 640 } 641 } 642 } else 643 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 644 tp->snd_max = tp->snd_nxt + len; 645 646 /* 647 * Trace. 648 */ 649 if (so->so_options & SO_DEBUG) 650 tcp_trace(TA_OUTPUT, tp->t_state, tp, ti, 0); 651 652 /* 653 * Fill in IP length and desired time to live and 654 * send to IP level. There should be a better way 655 * to handle ttl and tos; we could keep them in 656 * the template, but need a way to checksum without them. 657 */ 658 m->m_pkthdr.len = hdrlen + len; 659 #ifdef TUBA 660 if (tp->t_tuba_pcb) 661 error = tuba_output(m, tp); 662 else 663 #endif 664 { 665 struct rtentry *rt; 666 667 ((struct ip *)ti)->ip_len = m->m_pkthdr.len; 668 ((struct ip *)ti)->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; /* XXX */ 669 ((struct ip *)ti)->ip_tos = tp->t_inpcb->inp_ip.ip_tos; /* XXX */ 670 671 if (ip_mtudisc && (rt = in_pcbrtentry(tp->t_inpcb)) != 0 && 672 (rt->rt_rmx.rmx_locks & RTV_MTU) == 0) 673 ((struct ip *)ti)->ip_off |= IP_DF; 674 675 #if BSD >= 43 676 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 677 so->so_options & SO_DONTROUTE, 0); 678 #else 679 error = ip_output(m, (struct mbuf *)0, &tp->t_inpcb->inp_route, 680 so->so_options & SO_DONTROUTE); 681 #endif 682 } 683 if (error) { 684 out: 685 if (error == ENOBUFS) { 686 tcp_quench(tp->t_inpcb, 0); 687 return (0); 688 } 689 if ((error == EHOSTUNREACH || error == ENETDOWN) 690 && TCPS_HAVERCVDSYN(tp->t_state)) { 691 tp->t_softerror = error; 692 return (0); 693 } 694 return (error); 695 } 696 tcpstat.tcps_sndtotal++; 697 if (tp->t_flags & TF_DELACK) 698 tcpstat.tcps_delack++; 699 700 /* 701 * Data sent (as far as we can tell). 702 * If this advertises a larger window than any other segment, 703 * then remember the size of the advertised window. 704 * Any pending ACK has now been sent. 705 */ 706 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 707 tp->rcv_adv = tp->rcv_nxt + win; 708 tp->last_ack_sent = tp->rcv_nxt; 709 tp->t_flags &= ~TF_ACKNOW; 710 TCP_CLEAR_DELACK(tp); 711 if (sendalot) 712 goto again; 713 return (0); 714 } 715 716 void 717 tcp_setpersist(tp) 718 register struct tcpcb *tp; 719 { 720 register int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2); 721 722 if (tp->t_timer[TCPT_REXMT]) 723 panic("tcp_output REXMT"); 724 /* 725 * Start/restart persistance timer. 726 */ 727 if (t < tp->t_rttmin) 728 t = tp->t_rttmin; 729 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], 730 t * tcp_backoff[tp->t_rxtshift], 731 TCPTV_PERSMIN, TCPTV_PERSMAX); 732 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 733 tp->t_rxtshift++; 734 } 735