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