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 * $FreeBSD$ 35 */ 36 37 #include "opt_inet6.h" 38 #include "opt_ipsec.h" 39 #include "opt_tcpdebug.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/domain.h> 44 #include <sys/kernel.h> 45 #include <sys/lock.h> 46 #include <sys/mbuf.h> 47 #include <sys/mutex.h> 48 #include <sys/protosw.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/sysctl.h> 52 53 #include <net/route.h> 54 55 #include <netinet/in.h> 56 #include <netinet/in_systm.h> 57 #include <netinet/ip.h> 58 #include <netinet/in_pcb.h> 59 #include <netinet/ip_var.h> 60 #ifdef INET6 61 #include <netinet6/in6_pcb.h> 62 #include <netinet/ip6.h> 63 #include <netinet6/ip6_var.h> 64 #endif 65 #include <netinet/tcp.h> 66 #define TCPOUTFLAGS 67 #include <netinet/tcp_fsm.h> 68 #include <netinet/tcp_seq.h> 69 #include <netinet/tcp_timer.h> 70 #include <netinet/tcp_var.h> 71 #include <netinet/tcpip.h> 72 #ifdef TCPDEBUG 73 #include <netinet/tcp_debug.h> 74 #endif 75 76 #ifdef IPSEC 77 #include <netinet6/ipsec.h> 78 #endif /*IPSEC*/ 79 80 #include <machine/in_cksum.h> 81 82 #ifdef notyet 83 extern struct mbuf *m_copypack(); 84 #endif 85 86 static int path_mtu_discovery = 1; 87 SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW, 88 &path_mtu_discovery, 1, "Enable Path MTU Discovery"); 89 90 int ss_fltsz = 1; 91 SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW, 92 &ss_fltsz, 1, "Slow start flight size"); 93 94 int ss_fltsz_local = TCP_MAXWIN; /* something large */ 95 SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW, 96 &ss_fltsz_local, 1, "Slow start flight size for local networks"); 97 98 int tcp_do_newreno = 1; 99 SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno, 100 0, "Enable NewReno Algorithms"); 101 /* 102 * Tcp output routine: figure out what should be sent and send it. 103 */ 104 int 105 tcp_output(tp) 106 register struct tcpcb *tp; 107 { 108 register struct socket *so = tp->t_inpcb->inp_socket; 109 register long len, win; 110 int off, flags, error; 111 register struct mbuf *m; 112 struct ip *ip = NULL; 113 register struct ipovly *ipov = NULL; 114 #ifdef INET6 115 struct ip6_hdr *ip6 = NULL; 116 #endif /* INET6 */ 117 register struct tcphdr *th; 118 u_char opt[TCP_MAXOLEN]; 119 unsigned ipoptlen, optlen, hdrlen; 120 int idle, sendalot; 121 int maxburst = TCP_MAXBURST; 122 struct rmxp_tao *taop; 123 struct rmxp_tao tao_noncached; 124 #ifdef INET6 125 int isipv6; 126 #endif 127 128 #ifdef INET6 129 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 130 #endif 131 132 /* 133 * Determine length of data that should be transmitted, 134 * and flags that will be used. 135 * If there is some data or critical controls (SYN, RST) 136 * to send, then transmit; otherwise, investigate further. 137 */ 138 idle = (tp->snd_max == tp->snd_una); 139 if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) { 140 /* 141 * We have been idle for "a while" and no acks are 142 * expected to clock out any data we send -- 143 * slow start to get ack "clock" running again. 144 * 145 * Set the slow-start flight size depending on whether 146 * this is a local network or not. 147 */ 148 if ( 149 #ifdef INET6 150 (isipv6 && in6_localaddr(&tp->t_inpcb->in6p_faddr)) || 151 (!isipv6 && 152 #endif 153 in_localaddr(tp->t_inpcb->inp_faddr) 154 #ifdef INET6 155 ) 156 #endif 157 ) 158 tp->snd_cwnd = tp->t_maxseg * ss_fltsz_local; 159 else 160 tp->snd_cwnd = tp->t_maxseg * ss_fltsz; 161 } 162 again: 163 sendalot = 0; 164 off = tp->snd_nxt - tp->snd_una; 165 win = min(tp->snd_wnd, tp->snd_cwnd); 166 167 flags = tcp_outflags[tp->t_state]; 168 /* 169 * Get standard flags, and add SYN or FIN if requested by 'hidden' 170 * state flags. 171 */ 172 if (tp->t_flags & TF_NEEDFIN) 173 flags |= TH_FIN; 174 if (tp->t_flags & TF_NEEDSYN) 175 flags |= TH_SYN; 176 177 /* 178 * If in persist timeout with window of 0, send 1 byte. 179 * Otherwise, if window is small but nonzero 180 * and timer expired, we will send what we can 181 * and go to transmit state. 182 */ 183 if (tp->t_force) { 184 if (win == 0) { 185 /* 186 * If we still have some data to send, then 187 * clear the FIN bit. Usually this would 188 * happen below when it realizes that we 189 * aren't sending all the data. However, 190 * if we have exactly 1 byte of unsent data, 191 * then it won't clear the FIN bit below, 192 * and if we are in persist state, we wind 193 * up sending the packet without recording 194 * that we sent the FIN bit. 195 * 196 * We can't just blindly clear the FIN bit, 197 * because if we don't have any more data 198 * to send then the probe will be the FIN 199 * itself. 200 */ 201 if (off < so->so_snd.sb_cc) 202 flags &= ~TH_FIN; 203 win = 1; 204 } else { 205 callout_stop(tp->tt_persist); 206 tp->t_rxtshift = 0; 207 } 208 } 209 210 len = (long)ulmin(so->so_snd.sb_cc, win) - off; 211 212 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) { 213 taop = &tao_noncached; 214 bzero(taop, sizeof(*taop)); 215 } 216 217 /* 218 * Lop off SYN bit if it has already been sent. However, if this 219 * is SYN-SENT state and if segment contains data and if we don't 220 * know that foreign host supports TAO, suppress sending segment. 221 */ 222 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 223 flags &= ~TH_SYN; 224 off--, len++; 225 if (len > 0 && tp->t_state == TCPS_SYN_SENT && 226 taop->tao_ccsent == 0) 227 return 0; 228 } 229 230 /* 231 * Be careful not to send data and/or FIN on SYN segments 232 * in cases when no CC option will be sent. 233 * This measure is needed to prevent interoperability problems 234 * with not fully conformant TCP implementations. 235 */ 236 if ((flags & TH_SYN) && 237 ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) || 238 ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) { 239 len = 0; 240 flags &= ~TH_FIN; 241 } 242 243 if (len < 0) { 244 /* 245 * If FIN has been sent but not acked, 246 * but we haven't been called to retransmit, 247 * len will be -1. Otherwise, window shrank 248 * after we sent into it. If window shrank to 0, 249 * cancel pending retransmit, pull snd_nxt back 250 * to (closed) window, and set the persist timer 251 * if it isn't already going. If the window didn't 252 * close completely, just wait for an ACK. 253 */ 254 len = 0; 255 if (win == 0) { 256 callout_stop(tp->tt_rexmt); 257 tp->t_rxtshift = 0; 258 tp->snd_nxt = tp->snd_una; 259 if (!callout_active(tp->tt_persist)) 260 tcp_setpersist(tp); 261 } 262 } 263 if (len > tp->t_maxseg) { 264 len = tp->t_maxseg; 265 sendalot = 1; 266 } 267 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 268 flags &= ~TH_FIN; 269 270 win = sbspace(&so->so_rcv); 271 272 /* 273 * Sender silly window avoidance. If connection is idle 274 * and can send all data, a maximum segment, 275 * at least a maximum default-size segment do it, 276 * or are forced, do it; otherwise don't bother. 277 * If peer's buffer is tiny, then send 278 * when window is at least half open. 279 * If retransmitting (possibly after persist timer forced us 280 * to send into a small window), then must resend. 281 */ 282 if (len) { 283 if (len == tp->t_maxseg) 284 goto send; 285 if (!(tp->t_flags & TF_MORETOCOME) && 286 (idle || tp->t_flags & TF_NODELAY) && 287 (tp->t_flags & TF_NOPUSH) == 0 && 288 len + off >= so->so_snd.sb_cc) 289 goto send; 290 if (tp->t_force) 291 goto send; 292 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 293 goto send; 294 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 295 goto send; 296 } 297 298 /* 299 * Compare available window to amount of window 300 * known to peer (as advertised window less 301 * next expected input). If the difference is at least two 302 * max size segments, or at least 50% of the maximum possible 303 * window, then want to send a window update to peer. 304 */ 305 if (win > 0) { 306 /* 307 * "adv" is the amount we can increase the window, 308 * taking into account that we are limited by 309 * TCP_MAXWIN << tp->rcv_scale. 310 */ 311 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) - 312 (tp->rcv_adv - tp->rcv_nxt); 313 314 if (adv >= (long) (2 * tp->t_maxseg)) 315 goto send; 316 if (2 * adv >= (long) so->so_rcv.sb_hiwat) 317 goto send; 318 } 319 320 /* 321 * Send if we owe peer an ACK. 322 */ 323 if (tp->t_flags & TF_ACKNOW) 324 goto send; 325 if ((flags & TH_RST) || 326 ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 327 goto send; 328 if (SEQ_GT(tp->snd_up, tp->snd_una)) 329 goto send; 330 /* 331 * If our state indicates that FIN should be sent 332 * and we have not yet done so, or we're retransmitting the FIN, 333 * then we need to send. 334 */ 335 if (flags & TH_FIN && 336 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 337 goto send; 338 339 /* 340 * TCP window updates are not reliable, rather a polling protocol 341 * using ``persist'' packets is used to insure receipt of window 342 * updates. The three ``states'' for the output side are: 343 * idle not doing retransmits or persists 344 * persisting to move a small or zero window 345 * (re)transmitting and thereby not persisting 346 * 347 * callout_active(tp->tt_persist) 348 * is true when we are in persist state. 349 * tp->t_force 350 * is set when we are called to send a persist packet. 351 * callout_active(tp->tt_rexmt) 352 * is set when we are retransmitting 353 * The output side is idle when both timers are zero. 354 * 355 * If send window is too small, there is data to transmit, and no 356 * retransmit or persist is pending, then go to persist state. 357 * If nothing happens soon, send when timer expires: 358 * if window is nonzero, transmit what we can, 359 * otherwise force out a byte. 360 */ 361 if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) && 362 !callout_active(tp->tt_persist)) { 363 tp->t_rxtshift = 0; 364 tcp_setpersist(tp); 365 } 366 367 /* 368 * No reason to send a segment, just return. 369 */ 370 return (0); 371 372 send: 373 /* 374 * Before ESTABLISHED, force sending of initial options 375 * unless TCP set not to do any options. 376 * NOTE: we assume that the IP/TCP header plus TCP options 377 * always fit in a single mbuf, leaving room for a maximum 378 * link header, i.e. 379 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 380 */ 381 optlen = 0; 382 #ifdef INET6 383 if (isipv6) 384 hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 385 else 386 #endif 387 hdrlen = sizeof (struct tcpiphdr); 388 if (flags & TH_SYN) { 389 tp->snd_nxt = tp->iss; 390 if ((tp->t_flags & TF_NOOPT) == 0) { 391 u_short mss; 392 393 opt[0] = TCPOPT_MAXSEG; 394 opt[1] = TCPOLEN_MAXSEG; 395 mss = htons((u_short) tcp_mssopt(tp)); 396 (void)memcpy(opt + 2, &mss, sizeof(mss)); 397 optlen = TCPOLEN_MAXSEG; 398 399 if ((tp->t_flags & TF_REQ_SCALE) && 400 ((flags & TH_ACK) == 0 || 401 (tp->t_flags & TF_RCVD_SCALE))) { 402 *((u_int32_t *)(opt + optlen)) = htonl( 403 TCPOPT_NOP << 24 | 404 TCPOPT_WINDOW << 16 | 405 TCPOLEN_WINDOW << 8 | 406 tp->request_r_scale); 407 optlen += 4; 408 } 409 } 410 } 411 412 /* 413 * Send a timestamp and echo-reply if this is a SYN and our side 414 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 415 * and our peer have sent timestamps in our SYN's. 416 */ 417 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 418 (flags & TH_RST) == 0 && 419 ((flags & TH_ACK) == 0 || 420 (tp->t_flags & TF_RCVD_TSTMP))) { 421 u_int32_t *lp = (u_int32_t *)(opt + optlen); 422 423 /* Form timestamp option as shown in appendix A of RFC 1323. */ 424 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 425 *lp++ = htonl(ticks); 426 *lp = htonl(tp->ts_recent); 427 optlen += TCPOLEN_TSTAMP_APPA; 428 } 429 430 /* 431 * Send `CC-family' options if our side wants to use them (TF_REQ_CC), 432 * options are allowed (!TF_NOOPT) and it's not a RST. 433 */ 434 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 435 (flags & TH_RST) == 0) { 436 switch (flags & (TH_SYN|TH_ACK)) { 437 /* 438 * This is a normal ACK, send CC if we received CC before 439 * from our peer. 440 */ 441 case TH_ACK: 442 if (!(tp->t_flags & TF_RCVD_CC)) 443 break; 444 /*FALLTHROUGH*/ 445 446 /* 447 * We can only get here in T/TCP's SYN_SENT* state, when 448 * we're a sending a non-SYN segment without waiting for 449 * the ACK of our SYN. A check above assures that we only 450 * do this if our peer understands T/TCP. 451 */ 452 case 0: 453 opt[optlen++] = TCPOPT_NOP; 454 opt[optlen++] = TCPOPT_NOP; 455 opt[optlen++] = TCPOPT_CC; 456 opt[optlen++] = TCPOLEN_CC; 457 *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); 458 459 optlen += 4; 460 break; 461 462 /* 463 * This is our initial SYN, check whether we have to use 464 * CC or CC.new. 465 */ 466 case TH_SYN: 467 opt[optlen++] = TCPOPT_NOP; 468 opt[optlen++] = TCPOPT_NOP; 469 opt[optlen++] = tp->t_flags & TF_SENDCCNEW ? 470 TCPOPT_CCNEW : TCPOPT_CC; 471 opt[optlen++] = TCPOLEN_CC; 472 *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); 473 optlen += 4; 474 break; 475 476 /* 477 * This is a SYN,ACK; send CC and CC.echo if we received 478 * CC from our peer. 479 */ 480 case (TH_SYN|TH_ACK): 481 if (tp->t_flags & TF_RCVD_CC) { 482 opt[optlen++] = TCPOPT_NOP; 483 opt[optlen++] = TCPOPT_NOP; 484 opt[optlen++] = TCPOPT_CC; 485 opt[optlen++] = TCPOLEN_CC; 486 *(u_int32_t *)&opt[optlen] = 487 htonl(tp->cc_send); 488 optlen += 4; 489 opt[optlen++] = TCPOPT_NOP; 490 opt[optlen++] = TCPOPT_NOP; 491 opt[optlen++] = TCPOPT_CCECHO; 492 opt[optlen++] = TCPOLEN_CC; 493 *(u_int32_t *)&opt[optlen] = 494 htonl(tp->cc_recv); 495 optlen += 4; 496 } 497 break; 498 } 499 } 500 501 hdrlen += optlen; 502 503 #ifdef INET6 504 if (isipv6) 505 ipoptlen = ip6_optlen(tp->t_inpcb); 506 else 507 #endif 508 { 509 if (tp->t_inpcb->inp_options) { 510 ipoptlen = tp->t_inpcb->inp_options->m_len - 511 offsetof(struct ipoption, ipopt_list); 512 } else { 513 ipoptlen = 0; 514 } 515 } 516 #ifdef IPSEC 517 ipoptlen += ipsec_hdrsiz_tcp(tp); 518 #endif 519 520 /* 521 * Adjust data length if insertion of options will 522 * bump the packet length beyond the t_maxopd length. 523 * Clear the FIN bit because we cut off the tail of 524 * the segment. 525 */ 526 if (len + optlen + ipoptlen > tp->t_maxopd) { 527 /* 528 * If there is still more to send, don't close the connection. 529 */ 530 flags &= ~TH_FIN; 531 len = tp->t_maxopd - optlen - ipoptlen; 532 sendalot = 1; 533 } 534 535 /*#ifdef DIAGNOSTIC*/ 536 #ifdef INET6 537 if (max_linkhdr + hdrlen > MCLBYTES) 538 panic("tcphdr too big"); 539 #else 540 if (max_linkhdr + hdrlen > MHLEN) 541 panic("tcphdr too big"); 542 #endif 543 /*#endif*/ 544 545 /* 546 * Grab a header mbuf, attaching a copy of data to 547 * be transmitted, and initialize the header from 548 * the template for sends on this connection. 549 */ 550 if (len) { 551 if (tp->t_force && len == 1) 552 tcpstat.tcps_sndprobe++; 553 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 554 tcpstat.tcps_sndrexmitpack++; 555 tcpstat.tcps_sndrexmitbyte += len; 556 } else { 557 tcpstat.tcps_sndpack++; 558 tcpstat.tcps_sndbyte += len; 559 } 560 #ifdef notyet 561 if ((m = m_copypack(so->so_snd.sb_mb, off, 562 (int)len, max_linkhdr + hdrlen)) == 0) { 563 error = ENOBUFS; 564 goto out; 565 } 566 /* 567 * m_copypack left space for our hdr; use it. 568 */ 569 m->m_len += hdrlen; 570 m->m_data -= hdrlen; 571 #else 572 MGETHDR(m, M_DONTWAIT, MT_HEADER); 573 if (m == NULL) { 574 error = ENOBUFS; 575 goto out; 576 } 577 #ifdef INET6 578 if (MHLEN < hdrlen + max_linkhdr) { 579 MCLGET(m, M_DONTWAIT); 580 if ((m->m_flags & M_EXT) == 0) { 581 m_freem(m); 582 error = ENOBUFS; 583 goto out; 584 } 585 } 586 #endif 587 m->m_data += max_linkhdr; 588 m->m_len = hdrlen; 589 if (len <= MHLEN - hdrlen - max_linkhdr) { 590 m_copydata(so->so_snd.sb_mb, off, (int) len, 591 mtod(m, caddr_t) + hdrlen); 592 m->m_len += len; 593 } else { 594 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 595 if (m->m_next == 0) { 596 (void) m_free(m); 597 error = ENOBUFS; 598 goto out; 599 } 600 } 601 #endif 602 /* 603 * If we're sending everything we've got, set PUSH. 604 * (This will keep happy those implementations which only 605 * give data to the user when a buffer fills or 606 * a PUSH comes in.) 607 */ 608 if (off + len == so->so_snd.sb_cc) 609 flags |= TH_PUSH; 610 } else { 611 if (tp->t_flags & TF_ACKNOW) 612 tcpstat.tcps_sndacks++; 613 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 614 tcpstat.tcps_sndctrl++; 615 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 616 tcpstat.tcps_sndurg++; 617 else 618 tcpstat.tcps_sndwinup++; 619 620 MGETHDR(m, M_DONTWAIT, MT_HEADER); 621 if (m == NULL) { 622 error = ENOBUFS; 623 goto out; 624 } 625 #ifdef INET6 626 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 627 MHLEN >= hdrlen) { 628 MH_ALIGN(m, hdrlen); 629 } else 630 #endif 631 m->m_data += max_linkhdr; 632 m->m_len = hdrlen; 633 } 634 m->m_pkthdr.rcvif = (struct ifnet *)0; 635 #ifdef INET6 636 if (isipv6) { 637 ip6 = mtod(m, struct ip6_hdr *); 638 th = (struct tcphdr *)(ip6 + 1); 639 tcp_fillheaders(tp, ip6, th); 640 } else 641 #endif /* INET6 */ 642 { 643 ip = mtod(m, struct ip *); 644 ipov = (struct ipovly *)ip; 645 th = (struct tcphdr *)(ip + 1); 646 /* this picks up the pseudo header (w/o the length) */ 647 tcp_fillheaders(tp, ip, th); 648 } 649 650 /* 651 * Fill in fields, remembering maximum advertised 652 * window for use in delaying messages about window sizes. 653 * If resending a FIN, be sure not to use a new sequence number. 654 */ 655 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 656 tp->snd_nxt == tp->snd_max) 657 tp->snd_nxt--; 658 /* 659 * If we are doing retransmissions, then snd_nxt will 660 * not reflect the first unsent octet. For ACK only 661 * packets, we do not want the sequence number of the 662 * retransmitted packet, we want the sequence number 663 * of the next unsent octet. So, if there is no data 664 * (and no SYN or FIN), use snd_max instead of snd_nxt 665 * when filling in ti_seq. But if we are in persist 666 * state, snd_max might reflect one byte beyond the 667 * right edge of the window, so use snd_nxt in that 668 * case, since we know we aren't doing a retransmission. 669 * (retransmit and persist are mutually exclusive...) 670 */ 671 if (len || (flags & (TH_SYN|TH_FIN)) 672 || callout_active(tp->tt_persist)) 673 th->th_seq = htonl(tp->snd_nxt); 674 else 675 th->th_seq = htonl(tp->snd_max); 676 th->th_ack = htonl(tp->rcv_nxt); 677 if (optlen) { 678 bcopy(opt, th + 1, optlen); 679 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 680 } 681 th->th_flags = flags; 682 /* 683 * Calculate receive window. Don't shrink window, 684 * but avoid silly window syndrome. 685 */ 686 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) 687 win = 0; 688 if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) 689 win = (long)(tp->rcv_adv - tp->rcv_nxt); 690 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 691 win = (long)TCP_MAXWIN << tp->rcv_scale; 692 th->th_win = htons((u_short) (win>>tp->rcv_scale)); 693 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 694 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 695 th->th_flags |= TH_URG; 696 } else 697 /* 698 * If no urgent pointer to send, then we pull 699 * the urgent pointer to the left edge of the send window 700 * so that it doesn't drift into the send window on sequence 701 * number wraparound. 702 */ 703 tp->snd_up = tp->snd_una; /* drag it along */ 704 705 /* 706 * Put TCP length in extended header, and then 707 * checksum extended header and data. 708 */ 709 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 710 #ifdef INET6 711 if (isipv6) 712 /* 713 * ip6_plen is not need to be filled now, and will be filled 714 * in ip6_output. 715 */ 716 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 717 sizeof(struct tcphdr) + optlen + len); 718 else 719 #endif /* INET6 */ 720 { 721 m->m_pkthdr.csum_flags = CSUM_TCP; 722 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 723 if (len + optlen) 724 th->th_sum = in_addword(th->th_sum, 725 htons((u_short)(optlen + len))); 726 727 /* IP version must be set here for ipv4/ipv6 checking later */ 728 KASSERT(ip->ip_v == IPVERSION, 729 ("%s: IP version incorrect: %d", __FUNCTION__, ip->ip_v)); 730 } 731 732 /* 733 * In transmit state, time the transmission and arrange for 734 * the retransmit. In persist state, just set snd_max. 735 */ 736 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) { 737 tcp_seq startseq = tp->snd_nxt; 738 739 /* 740 * Advance snd_nxt over sequence space of this segment. 741 */ 742 if (flags & (TH_SYN|TH_FIN)) { 743 if (flags & TH_SYN) 744 tp->snd_nxt++; 745 if (flags & TH_FIN) { 746 tp->snd_nxt++; 747 tp->t_flags |= TF_SENTFIN; 748 } 749 } 750 tp->snd_nxt += len; 751 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 752 tp->snd_max = tp->snd_nxt; 753 /* 754 * Time this transmission if not a retransmission and 755 * not currently timing anything. 756 */ 757 if (tp->t_rtttime == 0) { 758 tp->t_rtttime = ticks; 759 tp->t_rtseq = startseq; 760 tcpstat.tcps_segstimed++; 761 } 762 } 763 764 /* 765 * Set retransmit timer if not currently set, 766 * and not doing an ack or a keep-alive probe. 767 * Initial value for retransmit timer is smoothed 768 * round-trip time + 2 * round-trip time variance. 769 * Initialize shift counter which is used for backoff 770 * of retransmit time. 771 */ 772 if (!callout_active(tp->tt_rexmt) && 773 tp->snd_nxt != tp->snd_una) { 774 if (callout_active(tp->tt_persist)) { 775 callout_stop(tp->tt_persist); 776 tp->t_rxtshift = 0; 777 } 778 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 779 tcp_timer_rexmt, tp); 780 } 781 } else 782 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 783 tp->snd_max = tp->snd_nxt + len; 784 785 #ifdef TCPDEBUG 786 /* 787 * Trace. 788 */ 789 if (so->so_options & SO_DEBUG) 790 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 791 #endif 792 793 /* 794 * Fill in IP length and desired time to live and 795 * send to IP level. There should be a better way 796 * to handle ttl and tos; we could keep them in 797 * the template, but need a way to checksum without them. 798 */ 799 /* 800 * m->m_pkthdr.len should have been set before cksum calcuration, 801 * because in6_cksum() need it. 802 */ 803 #ifdef INET6 804 if (isipv6) { 805 /* 806 * we separately set hoplimit for every segment, since the 807 * user might want to change the value via setsockopt. 808 * Also, desired default hop limit might be changed via 809 * Neighbor Discovery. 810 */ 811 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, 812 tp->t_inpcb->in6p_route.ro_rt ? 813 tp->t_inpcb->in6p_route.ro_rt->rt_ifp 814 : NULL); 815 816 /* TODO: IPv6 IP6TOS_ECT bit on */ 817 #ifdef IPSEC 818 if (ipsec_setsocket(m, so) != 0) { 819 m_freem(m); 820 error = ENOBUFS; 821 goto out; 822 } 823 #endif /*IPSEC*/ 824 error = ip6_output(m, 825 tp->t_inpcb->in6p_outputopts, 826 &tp->t_inpcb->in6p_route, 827 (so->so_options & SO_DONTROUTE), NULL, NULL); 828 } else 829 #endif /* INET6 */ 830 { 831 struct rtentry *rt; 832 ip->ip_len = m->m_pkthdr.len; 833 #ifdef INET6 834 if (INP_CHECK_SOCKAF(so, AF_INET6)) 835 ip->ip_ttl = in6_selecthlim(tp->t_inpcb, 836 tp->t_inpcb->in6p_route.ro_rt ? 837 tp->t_inpcb->in6p_route.ro_rt->rt_ifp 838 : NULL); 839 else 840 #endif /* INET6 */ 841 ip->ip_ttl = tp->t_inpcb->inp_ip_ttl; /* XXX */ 842 ip->ip_tos = tp->t_inpcb->inp_ip_tos; /* XXX */ 843 /* 844 * See if we should do MTU discovery. We do it only if the following 845 * are true: 846 * 1) we have a valid route to the destination 847 * 2) the MTU is not locked (if it is, then discovery has been 848 * disabled) 849 */ 850 if (path_mtu_discovery 851 && (rt = tp->t_inpcb->inp_route.ro_rt) 852 && rt->rt_flags & RTF_UP 853 && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { 854 ip->ip_off |= IP_DF; 855 } 856 #ifdef IPSEC 857 ipsec_setsocket(m, so); 858 #endif /*IPSEC*/ 859 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 860 (so->so_options & SO_DONTROUTE), 0); 861 } 862 if (error) { 863 864 /* 865 * We know that the packet was lost, so back out the 866 * sequence number advance, if any. 867 */ 868 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) { 869 /* 870 * No need to check for TH_FIN here because 871 * the TF_SENTFIN flag handles that case. 872 */ 873 if ((flags & TH_SYN) == 0) 874 tp->snd_nxt -= len; 875 } 876 877 out: 878 if (error == ENOBUFS) { 879 if (!callout_active(tp->tt_rexmt) && 880 !callout_active(tp->tt_persist)) 881 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 882 tcp_timer_rexmt, tp); 883 tcp_quench(tp->t_inpcb, 0); 884 return (0); 885 } 886 if (error == EMSGSIZE) { 887 /* 888 * ip_output() will have already fixed the route 889 * for us. tcp_mtudisc() will, as its last action, 890 * initiate retransmission, so it is important to 891 * not do so here. 892 */ 893 tcp_mtudisc(tp->t_inpcb, 0); 894 return 0; 895 } 896 if ((error == EHOSTUNREACH || error == ENETDOWN) 897 && TCPS_HAVERCVDSYN(tp->t_state)) { 898 tp->t_softerror = error; 899 return (0); 900 } 901 return (error); 902 } 903 tcpstat.tcps_sndtotal++; 904 905 /* 906 * Data sent (as far as we can tell). 907 * If this advertises a larger window than any other segment, 908 * then remember the size of the advertised window. 909 * Any pending ACK has now been sent. 910 */ 911 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 912 tp->rcv_adv = tp->rcv_nxt + win; 913 tp->last_ack_sent = tp->rcv_nxt; 914 tp->t_flags &= ~TF_ACKNOW; 915 if (tcp_delack_enabled) 916 callout_stop(tp->tt_delack); 917 if (sendalot && (!tcp_do_newreno || --maxburst)) 918 goto again; 919 return (0); 920 } 921 922 void 923 tcp_setpersist(tp) 924 register struct tcpcb *tp; 925 { 926 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 927 int tt; 928 929 if (callout_active(tp->tt_rexmt)) 930 panic("tcp_setpersist: retransmit pending"); 931 /* 932 * Start/restart persistance timer. 933 */ 934 TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 935 TCPTV_PERSMIN, TCPTV_PERSMAX); 936 callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp); 937 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 938 tp->t_rxtshift++; 939 } 940