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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95 30 * $FreeBSD$ 31 */ 32 33 #include "opt_inet.h" 34 #include "opt_inet6.h" 35 #include "opt_ipsec.h" 36 #include "opt_mac.h" 37 #include "opt_tcpdebug.h" 38 #include "opt_tcp_sack.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/domain.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/mac.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 #ifdef FAST_IPSEC 81 #include <netipsec/ipsec.h> 82 #define IPSEC 83 #endif /*FAST_IPSEC*/ 84 85 #include <machine/in_cksum.h> 86 87 #ifdef notyet 88 extern struct mbuf *m_copypack(); 89 #endif 90 91 int path_mtu_discovery = 1; 92 SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW, 93 &path_mtu_discovery, 1, "Enable Path MTU Discovery"); 94 95 int ss_fltsz = 1; 96 SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW, 97 &ss_fltsz, 1, "Slow start flight size"); 98 99 int ss_fltsz_local = 4; 100 SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW, 101 &ss_fltsz_local, 1, "Slow start flight size for local networks"); 102 103 int tcp_do_newreno = 1; 104 SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno, 105 0, "Enable NewReno Algorithms"); 106 107 /* 108 * Tcp output routine: figure out what should be sent and send it. 109 */ 110 int 111 tcp_output(struct tcpcb *tp) 112 { 113 struct socket *so = tp->t_inpcb->inp_socket; 114 long len, recwin, sendwin; 115 int off, flags, error; 116 #ifdef TCP_SIGNATURE 117 int sigoff = 0; 118 #endif 119 struct mbuf *m; 120 struct ip *ip = NULL; 121 struct ipovly *ipov = NULL; 122 struct tcphdr *th; 123 u_char opt[TCP_MAXOLEN]; 124 unsigned ipoptlen, optlen, hdrlen; 125 int idle, sendalot; 126 int i, sack_rxmit; 127 int sack_bytes_rxmt; 128 struct sackhole *p; 129 #if 0 130 int maxburst = TCP_MAXBURST; 131 #endif 132 #ifdef INET6 133 struct ip6_hdr *ip6 = NULL; 134 int isipv6; 135 136 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 137 #endif 138 139 INP_LOCK_ASSERT(tp->t_inpcb); 140 141 /* 142 * Determine length of data that should be transmitted, 143 * and flags that will be used. 144 * If there is some data or critical controls (SYN, RST) 145 * to send, then transmit; otherwise, investigate further. 146 */ 147 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 148 if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) { 149 /* 150 * We have been idle for "a while" and no acks are 151 * expected to clock out any data we send -- 152 * slow start to get ack "clock" running again. 153 * 154 * Set the slow-start flight size depending on whether 155 * this is a local network or not. 156 */ 157 int ss = ss_fltsz; 158 #ifdef INET6 159 if (isipv6) { 160 if (in6_localaddr(&tp->t_inpcb->in6p_faddr)) 161 ss = ss_fltsz_local; 162 } else 163 #endif /* INET6 */ 164 if (in_localaddr(tp->t_inpcb->inp_faddr)) 165 ss = ss_fltsz_local; 166 tp->snd_cwnd = tp->t_maxseg * ss; 167 } 168 tp->t_flags &= ~TF_LASTIDLE; 169 if (idle) { 170 if (tp->t_flags & TF_MORETOCOME) { 171 tp->t_flags |= TF_LASTIDLE; 172 idle = 0; 173 } 174 } 175 again: 176 /* 177 * If we've recently taken a timeout, snd_max will be greater than 178 * snd_nxt. There may be SACK information that allows us to avoid 179 * resending already delivered data. Adjust snd_nxt accordingly. 180 */ 181 if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max)) 182 tcp_sack_adjust(tp); 183 sendalot = 0; 184 off = tp->snd_nxt - tp->snd_una; 185 sendwin = min(tp->snd_wnd, tp->snd_cwnd); 186 sendwin = min(sendwin, tp->snd_bwnd); 187 188 flags = tcp_outflags[tp->t_state]; 189 /* 190 * Send any SACK-generated retransmissions. If we're explicitly trying 191 * to send out new data (when sendalot is 1), bypass this function. 192 * If we retransmit in fast recovery mode, decrement snd_cwnd, since 193 * we're replacing a (future) new transmission with a retransmission 194 * now, and we previously incremented snd_cwnd in tcp_input(). 195 */ 196 /* 197 * Still in sack recovery , reset rxmit flag to zero. 198 */ 199 sack_rxmit = 0; 200 sack_bytes_rxmt = 0; 201 len = 0; 202 p = NULL; 203 if (tp->sack_enable && IN_FASTRECOVERY(tp) && 204 (p = tcp_sack_output(tp, &sack_bytes_rxmt))) { 205 long cwin; 206 207 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt; 208 if (cwin < 0) 209 cwin = 0; 210 /* Do not retransmit SACK segments beyond snd_recover */ 211 if (SEQ_GT(p->end, tp->snd_recover)) { 212 /* 213 * (At least) part of sack hole extends beyond 214 * snd_recover. Check to see if we can rexmit data 215 * for this hole. 216 */ 217 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 218 /* 219 * Can't rexmit any more data for this hole. 220 * That data will be rexmitted in the next 221 * sack recovery episode, when snd_recover 222 * moves past p->rxmit. 223 */ 224 p = NULL; 225 goto after_sack_rexmit; 226 } else 227 /* Can rexmit part of the current hole */ 228 len = ((long)ulmin(cwin, 229 tp->snd_recover - p->rxmit)); 230 } else 231 len = ((long)ulmin(cwin, p->end - p->rxmit)); 232 off = p->rxmit - tp->snd_una; 233 KASSERT(off >= 0,("%s: sack block to the left of una : %d", 234 __func__, off)); 235 if (len > 0) { 236 sack_rxmit = 1; 237 sendalot = 1; 238 tcpstat.tcps_sack_rexmits++; 239 tcpstat.tcps_sack_rexmit_bytes += 240 min(len, tp->t_maxseg); 241 } 242 } 243 after_sack_rexmit: 244 /* 245 * Get standard flags, and add SYN or FIN if requested by 'hidden' 246 * state flags. 247 */ 248 if (tp->t_flags & TF_NEEDFIN) 249 flags |= TH_FIN; 250 if (tp->t_flags & TF_NEEDSYN) 251 flags |= TH_SYN; 252 253 SOCKBUF_LOCK(&so->so_snd); 254 /* 255 * If in persist timeout with window of 0, send 1 byte. 256 * Otherwise, if window is small but nonzero 257 * and timer expired, we will send what we can 258 * and go to transmit state. 259 */ 260 if (tp->t_force) { 261 if (sendwin == 0) { 262 /* 263 * If we still have some data to send, then 264 * clear the FIN bit. Usually this would 265 * happen below when it realizes that we 266 * aren't sending all the data. However, 267 * if we have exactly 1 byte of unsent data, 268 * then it won't clear the FIN bit below, 269 * and if we are in persist state, we wind 270 * up sending the packet without recording 271 * that we sent the FIN bit. 272 * 273 * We can't just blindly clear the FIN bit, 274 * because if we don't have any more data 275 * to send then the probe will be the FIN 276 * itself. 277 */ 278 if (off < so->so_snd.sb_cc) 279 flags &= ~TH_FIN; 280 sendwin = 1; 281 } else { 282 callout_stop(tp->tt_persist); 283 tp->t_rxtshift = 0; 284 } 285 } 286 287 /* 288 * If snd_nxt == snd_max and we have transmitted a FIN, the 289 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in 290 * a negative length. This can also occur when TCP opens up 291 * its congestion window while receiving additional duplicate 292 * acks after fast-retransmit because TCP will reset snd_nxt 293 * to snd_max after the fast-retransmit. 294 * 295 * In the normal retransmit-FIN-only case, however, snd_nxt will 296 * be set to snd_una, the offset will be 0, and the length may 297 * wind up 0. 298 * 299 * If sack_rxmit is true we are retransmitting from the scoreboard 300 * in which case len is already set. 301 */ 302 if (sack_rxmit == 0) { 303 if (sack_bytes_rxmt == 0) 304 len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off); 305 else { 306 long cwin; 307 308 /* 309 * We are inside of a SACK recovery episode and are 310 * sending new data, having retransmitted all the 311 * data possible in the scoreboard. 312 */ 313 len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd) 314 - off); 315 /* 316 * Don't remove this (len > 0) check ! 317 * We explicitly check for len > 0 here (although it 318 * isn't really necessary), to work around a gcc 319 * optimization issue - to force gcc to compute 320 * len above. Without this check, the computation 321 * of len is bungled by the optimizer. 322 */ 323 if (len > 0) { 324 cwin = tp->snd_cwnd - 325 (tp->snd_nxt - tp->sack_newdata) - 326 sack_bytes_rxmt; 327 if (cwin < 0) 328 cwin = 0; 329 len = lmin(len, cwin); 330 } 331 } 332 } 333 334 /* 335 * Lop off SYN bit if it has already been sent. However, if this 336 * is SYN-SENT state and if segment contains data and if we don't 337 * know that foreign host supports TAO, suppress sending segment. 338 */ 339 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 340 flags &= ~TH_SYN; 341 off--, len++; 342 } 343 344 /* 345 * Be careful not to send data and/or FIN on SYN segments. 346 * This measure is needed to prevent interoperability problems 347 * with not fully conformant TCP implementations. 348 */ 349 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 350 len = 0; 351 flags &= ~TH_FIN; 352 } 353 354 if (len < 0) { 355 /* 356 * If FIN has been sent but not acked, 357 * but we haven't been called to retransmit, 358 * len will be < 0. Otherwise, window shrank 359 * after we sent into it. If window shrank to 0, 360 * cancel pending retransmit, pull snd_nxt back 361 * to (closed) window, and set the persist timer 362 * if it isn't already going. If the window didn't 363 * close completely, just wait for an ACK. 364 */ 365 len = 0; 366 if (sendwin == 0) { 367 callout_stop(tp->tt_rexmt); 368 tp->t_rxtshift = 0; 369 tp->snd_nxt = tp->snd_una; 370 if (!callout_active(tp->tt_persist)) 371 tcp_setpersist(tp); 372 } 373 } 374 375 /* 376 * len will be >= 0 after this point. Truncate to the maximum 377 * segment length and ensure that FIN is removed if the length 378 * no longer contains the last data byte. 379 */ 380 if (len > tp->t_maxseg) { 381 len = tp->t_maxseg; 382 sendalot = 1; 383 } 384 if (sack_rxmit) { 385 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc)) 386 flags &= ~TH_FIN; 387 } else { 388 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 389 flags &= ~TH_FIN; 390 } 391 392 recwin = sbspace(&so->so_rcv); 393 394 /* 395 * Sender silly window avoidance. We transmit under the following 396 * conditions when len is non-zero: 397 * 398 * - We have a full segment 399 * - This is the last buffer in a write()/send() and we are 400 * either idle or running NODELAY 401 * - we've timed out (e.g. persist timer) 402 * - we have more then 1/2 the maximum send window's worth of 403 * data (receiver may be limited the window size) 404 * - we need to retransmit 405 */ 406 if (len) { 407 if (len == tp->t_maxseg) 408 goto send; 409 /* 410 * NOTE! on localhost connections an 'ack' from the remote 411 * end may occur synchronously with the output and cause 412 * us to flush a buffer queued with moretocome. XXX 413 * 414 * note: the len + off check is almost certainly unnecessary. 415 */ 416 if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 417 (idle || (tp->t_flags & TF_NODELAY)) && 418 len + off >= so->so_snd.sb_cc && 419 (tp->t_flags & TF_NOPUSH) == 0) { 420 goto send; 421 } 422 if (tp->t_force) /* typ. timeout case */ 423 goto send; 424 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 425 goto send; 426 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 427 goto send; 428 if (sack_rxmit) 429 goto send; 430 } 431 432 /* 433 * Compare available window to amount of window 434 * known to peer (as advertised window less 435 * next expected input). If the difference is at least two 436 * max size segments, or at least 50% of the maximum possible 437 * window, then want to send a window update to peer. 438 * Skip this if the connection is in T/TCP half-open state. 439 */ 440 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) { 441 /* 442 * "adv" is the amount we can increase the window, 443 * taking into account that we are limited by 444 * TCP_MAXWIN << tp->rcv_scale. 445 */ 446 long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) - 447 (tp->rcv_adv - tp->rcv_nxt); 448 449 if (adv >= (long) (2 * tp->t_maxseg)) 450 goto send; 451 if (2 * adv >= (long) so->so_rcv.sb_hiwat) 452 goto send; 453 } 454 455 /* 456 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 457 * is also a catch-all for the retransmit timer timeout case. 458 */ 459 if (tp->t_flags & TF_ACKNOW) 460 goto send; 461 if ((flags & TH_RST) || 462 ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 463 goto send; 464 if (SEQ_GT(tp->snd_up, tp->snd_una)) 465 goto send; 466 /* 467 * If our state indicates that FIN should be sent 468 * and we have not yet done so, then we need to send. 469 */ 470 if (flags & TH_FIN && 471 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 472 goto send; 473 /* 474 * In SACK, it is possible for tcp_output to fail to send a segment 475 * after the retransmission timer has been turned off. Make sure 476 * that the retransmission timer is set. 477 */ 478 if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) && 479 !callout_active(tp->tt_rexmt) && 480 !callout_active(tp->tt_persist)) { 481 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 482 tcp_timer_rexmt, tp); 483 goto just_return; 484 } 485 /* 486 * TCP window updates are not reliable, rather a polling protocol 487 * using ``persist'' packets is used to insure receipt of window 488 * updates. The three ``states'' for the output side are: 489 * idle not doing retransmits or persists 490 * persisting to move a small or zero window 491 * (re)transmitting and thereby not persisting 492 * 493 * callout_active(tp->tt_persist) 494 * is true when we are in persist state. 495 * tp->t_force 496 * is set when we are called to send a persist packet. 497 * callout_active(tp->tt_rexmt) 498 * is set when we are retransmitting 499 * The output side is idle when both timers are zero. 500 * 501 * If send window is too small, there is data to transmit, and no 502 * retransmit or persist is pending, then go to persist state. 503 * If nothing happens soon, send when timer expires: 504 * if window is nonzero, transmit what we can, 505 * otherwise force out a byte. 506 */ 507 if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) && 508 !callout_active(tp->tt_persist)) { 509 tp->t_rxtshift = 0; 510 tcp_setpersist(tp); 511 } 512 513 /* 514 * No reason to send a segment, just return. 515 */ 516 just_return: 517 SOCKBUF_UNLOCK(&so->so_snd); 518 return (0); 519 520 send: 521 SOCKBUF_LOCK_ASSERT(&so->so_snd); 522 /* 523 * Before ESTABLISHED, force sending of initial options 524 * unless TCP set not to do any options. 525 * NOTE: we assume that the IP/TCP header plus TCP options 526 * always fit in a single mbuf, leaving room for a maximum 527 * link header, i.e. 528 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 529 */ 530 optlen = 0; 531 #ifdef INET6 532 if (isipv6) 533 hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 534 else 535 #endif 536 hdrlen = sizeof (struct tcpiphdr); 537 if (flags & TH_SYN) { 538 tp->snd_nxt = tp->iss; 539 if ((tp->t_flags & TF_NOOPT) == 0) { 540 u_short mss; 541 542 opt[0] = TCPOPT_MAXSEG; 543 opt[1] = TCPOLEN_MAXSEG; 544 mss = htons((u_short) tcp_mssopt(&tp->t_inpcb->inp_inc)); 545 (void)memcpy(opt + 2, &mss, sizeof(mss)); 546 optlen = TCPOLEN_MAXSEG; 547 548 if ((tp->t_flags & TF_REQ_SCALE) && 549 ((flags & TH_ACK) == 0 || 550 (tp->t_flags & TF_RCVD_SCALE))) { 551 *((u_int32_t *)(opt + optlen)) = htonl( 552 TCPOPT_NOP << 24 | 553 TCPOPT_WINDOW << 16 | 554 TCPOLEN_WINDOW << 8 | 555 tp->request_r_scale); 556 optlen += 4; 557 } 558 } 559 } 560 561 /* 562 * Send a timestamp and echo-reply if this is a SYN and our side 563 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 564 * and our peer have sent timestamps in our SYN's. 565 */ 566 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 567 (flags & TH_RST) == 0 && 568 ((flags & TH_ACK) == 0 || 569 (tp->t_flags & TF_RCVD_TSTMP))) { 570 u_int32_t *lp = (u_int32_t *)(opt + optlen); 571 572 /* Form timestamp option as shown in appendix A of RFC 1323. */ 573 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 574 *lp++ = htonl(ticks); 575 *lp = htonl(tp->ts_recent); 576 optlen += TCPOLEN_TSTAMP_APPA; 577 } 578 579 #ifdef TCP_SIGNATURE 580 #ifdef INET6 581 if (!isipv6) 582 #endif 583 if (tp->t_flags & TF_SIGNATURE) { 584 int i; 585 u_char *bp; 586 587 /* Initialize TCP-MD5 option (RFC2385) */ 588 bp = (u_char *)opt + optlen; 589 *bp++ = TCPOPT_SIGNATURE; 590 *bp++ = TCPOLEN_SIGNATURE; 591 sigoff = optlen + 2; 592 for (i = 0; i < TCP_SIGLEN; i++) 593 *bp++ = 0; 594 optlen += TCPOLEN_SIGNATURE; 595 } 596 #endif /* TCP_SIGNATURE */ 597 598 if (tp->sack_enable && ((tp->t_flags & TF_NOOPT) == 0)) { 599 /* 600 * Tack on the SACK permitted option *last*. 601 * And do padding of options after tacking this on. 602 * This is because of MSS, TS, WinScale and Signatures are 603 * all present, we have just 2 bytes left for the SACK 604 * permitted option, which is just enough. 605 */ 606 /* 607 * If this is the first SYN of connection (not a SYN 608 * ACK), include SACK permitted option. If this is a 609 * SYN ACK, include SACK permitted option if peer has 610 * already done so. This is only for active connect, 611 * since the syncache takes care of the passive connect. 612 */ 613 if ((flags & TH_SYN) && 614 (!(flags & TH_ACK) || (tp->t_flags & TF_SACK_PERMIT))) { 615 u_char *bp; 616 bp = (u_char *)opt + optlen; 617 618 *bp++ = TCPOPT_SACK_PERMITTED; 619 *bp++ = TCPOLEN_SACK_PERMITTED; 620 optlen += TCPOLEN_SACK_PERMITTED; 621 } 622 623 /* 624 * Send SACKs if necessary. This should be the last 625 * option processed. Only as many SACKs are sent as 626 * are permitted by the maximum options size. 627 * 628 * In general, SACK blocks consume 8*n+2 bytes. 629 * So a full size SACK blocks option is 34 bytes 630 * (to generate 4 SACK blocks). At a minimum, 631 * we need 10 bytes (to generate 1 SACK block). 632 * If TCP Timestamps (12 bytes) and TCP Signatures 633 * (18 bytes) are both present, we'll just have 634 * 10 bytes for SACK options 40 - (12 + 18). 635 */ 636 if (TCPS_HAVEESTABLISHED(tp->t_state) && 637 (tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks > 0 && 638 MAX_TCPOPTLEN - optlen - 2 >= TCPOLEN_SACK) { 639 int nsack, sackoptlen, padlen; 640 u_char *bp = (u_char *)opt + optlen; 641 u_int32_t *lp; 642 643 nsack = (MAX_TCPOPTLEN - optlen - 2) / TCPOLEN_SACK; 644 nsack = min(nsack, tp->rcv_numsacks); 645 sackoptlen = (2 + nsack * TCPOLEN_SACK); 646 647 /* 648 * First we need to pad options so that the 649 * SACK blocks can start at a 4-byte boundary 650 * (sack option and length are at a 2 byte offset). 651 */ 652 padlen = (MAX_TCPOPTLEN - optlen - sackoptlen) % 4; 653 optlen += padlen; 654 while (padlen-- > 0) 655 *bp++ = TCPOPT_NOP; 656 657 tcpstat.tcps_sack_send_blocks++; 658 *bp++ = TCPOPT_SACK; 659 *bp++ = sackoptlen; 660 lp = (u_int32_t *)bp; 661 for (i = 0; i < nsack; i++) { 662 struct sackblk sack = tp->sackblks[i]; 663 *lp++ = htonl(sack.start); 664 *lp++ = htonl(sack.end); 665 } 666 optlen += sackoptlen; 667 } 668 } 669 670 /* Pad TCP options to a 4 byte boundary */ 671 if (optlen < MAX_TCPOPTLEN && (optlen % sizeof(u_int32_t))) { 672 int pad = sizeof(u_int32_t) - (optlen % sizeof(u_int32_t)); 673 u_char *bp = (u_char *)opt + optlen; 674 675 optlen += pad; 676 while (pad) { 677 *bp++ = TCPOPT_EOL; 678 pad--; 679 } 680 } 681 682 hdrlen += optlen; 683 684 #ifdef INET6 685 if (isipv6) 686 ipoptlen = ip6_optlen(tp->t_inpcb); 687 else 688 #endif 689 if (tp->t_inpcb->inp_options) 690 ipoptlen = tp->t_inpcb->inp_options->m_len - 691 offsetof(struct ipoption, ipopt_list); 692 else 693 ipoptlen = 0; 694 #ifdef IPSEC 695 ipoptlen += ipsec_hdrsiz_tcp(tp); 696 #endif 697 698 /* 699 * Adjust data length if insertion of options will 700 * bump the packet length beyond the t_maxopd length. 701 * Clear the FIN bit because we cut off the tail of 702 * the segment. 703 */ 704 if (len + optlen + ipoptlen > tp->t_maxopd) { 705 /* 706 * If there is still more to send, don't close the connection. 707 */ 708 flags &= ~TH_FIN; 709 len = tp->t_maxopd - optlen - ipoptlen; 710 sendalot = 1; 711 } 712 713 /*#ifdef DIAGNOSTIC*/ 714 #ifdef INET6 715 if (max_linkhdr + hdrlen > MCLBYTES) 716 #else 717 if (max_linkhdr + hdrlen > MHLEN) 718 #endif 719 panic("tcphdr too big"); 720 /*#endif*/ 721 722 /* 723 * Grab a header mbuf, attaching a copy of data to 724 * be transmitted, and initialize the header from 725 * the template for sends on this connection. 726 */ 727 if (len) { 728 if (tp->t_force && len == 1) 729 tcpstat.tcps_sndprobe++; 730 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 731 tcpstat.tcps_sndrexmitpack++; 732 tcpstat.tcps_sndrexmitbyte += len; 733 } else { 734 tcpstat.tcps_sndpack++; 735 tcpstat.tcps_sndbyte += len; 736 } 737 #ifdef notyet 738 if ((m = m_copypack(so->so_snd.sb_mb, off, 739 (int)len, max_linkhdr + hdrlen)) == 0) { 740 SOCKBUF_UNLOCK(&so->so_snd); 741 error = ENOBUFS; 742 goto out; 743 } 744 /* 745 * m_copypack left space for our hdr; use it. 746 */ 747 m->m_len += hdrlen; 748 m->m_data -= hdrlen; 749 #else 750 MGETHDR(m, M_DONTWAIT, MT_HEADER); 751 if (m == NULL) { 752 SOCKBUF_UNLOCK(&so->so_snd); 753 error = ENOBUFS; 754 goto out; 755 } 756 #ifdef INET6 757 if (MHLEN < hdrlen + max_linkhdr) { 758 MCLGET(m, M_DONTWAIT); 759 if ((m->m_flags & M_EXT) == 0) { 760 SOCKBUF_UNLOCK(&so->so_snd); 761 m_freem(m); 762 error = ENOBUFS; 763 goto out; 764 } 765 } 766 #endif 767 m->m_data += max_linkhdr; 768 m->m_len = hdrlen; 769 if (len <= MHLEN - hdrlen - max_linkhdr) { 770 m_copydata(so->so_snd.sb_mb, off, (int) len, 771 mtod(m, caddr_t) + hdrlen); 772 m->m_len += len; 773 } else { 774 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 775 if (m->m_next == 0) { 776 SOCKBUF_UNLOCK(&so->so_snd); 777 (void) m_free(m); 778 error = ENOBUFS; 779 goto out; 780 } 781 } 782 #endif 783 /* 784 * If we're sending everything we've got, set PUSH. 785 * (This will keep happy those implementations which only 786 * give data to the user when a buffer fills or 787 * a PUSH comes in.) 788 */ 789 if (off + len == so->so_snd.sb_cc) 790 flags |= TH_PUSH; 791 SOCKBUF_UNLOCK(&so->so_snd); 792 } else { 793 SOCKBUF_UNLOCK(&so->so_snd); 794 if (tp->t_flags & TF_ACKNOW) 795 tcpstat.tcps_sndacks++; 796 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 797 tcpstat.tcps_sndctrl++; 798 else if (SEQ_GT(tp->snd_up, tp->snd_una)) 799 tcpstat.tcps_sndurg++; 800 else 801 tcpstat.tcps_sndwinup++; 802 803 MGETHDR(m, M_DONTWAIT, MT_HEADER); 804 if (m == NULL) { 805 error = ENOBUFS; 806 goto out; 807 } 808 #ifdef INET6 809 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 810 MHLEN >= hdrlen) { 811 MH_ALIGN(m, hdrlen); 812 } else 813 #endif 814 m->m_data += max_linkhdr; 815 m->m_len = hdrlen; 816 } 817 SOCKBUF_UNLOCK_ASSERT(&so->so_snd); 818 m->m_pkthdr.rcvif = (struct ifnet *)0; 819 #ifdef MAC 820 mac_create_mbuf_from_inpcb(tp->t_inpcb, m); 821 #endif 822 #ifdef INET6 823 if (isipv6) { 824 ip6 = mtod(m, struct ip6_hdr *); 825 th = (struct tcphdr *)(ip6 + 1); 826 tcpip_fillheaders(tp->t_inpcb, ip6, th); 827 } else 828 #endif /* INET6 */ 829 { 830 ip = mtod(m, struct ip *); 831 ipov = (struct ipovly *)ip; 832 th = (struct tcphdr *)(ip + 1); 833 tcpip_fillheaders(tp->t_inpcb, ip, th); 834 } 835 836 /* 837 * Fill in fields, remembering maximum advertised 838 * window for use in delaying messages about window sizes. 839 * If resending a FIN, be sure not to use a new sequence number. 840 */ 841 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 842 tp->snd_nxt == tp->snd_max) 843 tp->snd_nxt--; 844 /* 845 * If we are doing retransmissions, then snd_nxt will 846 * not reflect the first unsent octet. For ACK only 847 * packets, we do not want the sequence number of the 848 * retransmitted packet, we want the sequence number 849 * of the next unsent octet. So, if there is no data 850 * (and no SYN or FIN), use snd_max instead of snd_nxt 851 * when filling in ti_seq. But if we are in persist 852 * state, snd_max might reflect one byte beyond the 853 * right edge of the window, so use snd_nxt in that 854 * case, since we know we aren't doing a retransmission. 855 * (retransmit and persist are mutually exclusive...) 856 */ 857 if (sack_rxmit == 0) { 858 if (len || (flags & (TH_SYN|TH_FIN)) 859 || callout_active(tp->tt_persist)) 860 th->th_seq = htonl(tp->snd_nxt); 861 else 862 th->th_seq = htonl(tp->snd_max); 863 } else { 864 th->th_seq = htonl(p->rxmit); 865 p->rxmit += len; 866 } 867 th->th_ack = htonl(tp->rcv_nxt); 868 if (optlen) { 869 bcopy(opt, th + 1, optlen); 870 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 871 } 872 th->th_flags = flags; 873 /* 874 * Calculate receive window. Don't shrink window, 875 * but avoid silly window syndrome. 876 */ 877 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) && 878 recwin < (long)tp->t_maxseg) 879 recwin = 0; 880 if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt)) 881 recwin = (long)(tp->rcv_adv - tp->rcv_nxt); 882 if (recwin > (long)TCP_MAXWIN << tp->rcv_scale) 883 recwin = (long)TCP_MAXWIN << tp->rcv_scale; 884 th->th_win = htons((u_short) (recwin >> tp->rcv_scale)); 885 886 887 /* 888 * Adjust the RXWIN0SENT flag - indicate that we have advertised 889 * a 0 window. This may cause the remote transmitter to stall. This 890 * flag tells soreceive() to disable delayed acknowledgements when 891 * draining the buffer. This can occur if the receiver is attempting 892 * to read more data then can be buffered prior to transmitting on 893 * the connection. 894 */ 895 if (recwin == 0) 896 tp->t_flags |= TF_RXWIN0SENT; 897 else 898 tp->t_flags &= ~TF_RXWIN0SENT; 899 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 900 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 901 th->th_flags |= TH_URG; 902 } else 903 /* 904 * If no urgent pointer to send, then we pull 905 * the urgent pointer to the left edge of the send window 906 * so that it doesn't drift into the send window on sequence 907 * number wraparound. 908 */ 909 tp->snd_up = tp->snd_una; /* drag it along */ 910 911 #ifdef TCP_SIGNATURE 912 #ifdef INET6 913 if (!isipv6) 914 #endif 915 if (tp->t_flags & TF_SIGNATURE) 916 tcp_signature_compute(m, sizeof(struct ip), len, optlen, 917 (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND); 918 #endif 919 920 /* 921 * Put TCP length in extended header, and then 922 * checksum extended header and data. 923 */ 924 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 925 #ifdef INET6 926 if (isipv6) 927 /* 928 * ip6_plen is not need to be filled now, and will be filled 929 * in ip6_output. 930 */ 931 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 932 sizeof(struct tcphdr) + optlen + len); 933 else 934 #endif /* INET6 */ 935 { 936 m->m_pkthdr.csum_flags = CSUM_TCP; 937 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 938 th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 939 htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen)); 940 941 /* IP version must be set here for ipv4/ipv6 checking later */ 942 KASSERT(ip->ip_v == IPVERSION, 943 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 944 } 945 946 /* 947 * In transmit state, time the transmission and arrange for 948 * the retransmit. In persist state, just set snd_max. 949 */ 950 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) { 951 tcp_seq startseq = tp->snd_nxt; 952 953 /* 954 * Advance snd_nxt over sequence space of this segment. 955 */ 956 if (flags & (TH_SYN|TH_FIN)) { 957 if (flags & TH_SYN) 958 tp->snd_nxt++; 959 if (flags & TH_FIN) { 960 tp->snd_nxt++; 961 tp->t_flags |= TF_SENTFIN; 962 } 963 } 964 if (sack_rxmit) 965 goto timer; 966 tp->snd_nxt += len; 967 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 968 tp->snd_max = tp->snd_nxt; 969 /* 970 * Time this transmission if not a retransmission and 971 * not currently timing anything. 972 */ 973 if (tp->t_rtttime == 0) { 974 tp->t_rtttime = ticks; 975 tp->t_rtseq = startseq; 976 tcpstat.tcps_segstimed++; 977 } 978 } 979 980 /* 981 * Set retransmit timer if not currently set, 982 * and not doing a pure ack or a keep-alive probe. 983 * Initial value for retransmit timer is smoothed 984 * round-trip time + 2 * round-trip time variance. 985 * Initialize shift counter which is used for backoff 986 * of retransmit time. 987 */ 988 timer: 989 if (!callout_active(tp->tt_rexmt) && 990 ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 991 (tp->snd_nxt != tp->snd_una))) { 992 if (callout_active(tp->tt_persist)) { 993 callout_stop(tp->tt_persist); 994 tp->t_rxtshift = 0; 995 } 996 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 997 tcp_timer_rexmt, tp); 998 } 999 } else { 1000 /* 1001 * Persist case, update snd_max but since we are in 1002 * persist mode (no window) we do not update snd_nxt. 1003 */ 1004 int xlen = len; 1005 if (flags & TH_SYN) 1006 ++xlen; 1007 if (flags & TH_FIN) { 1008 ++xlen; 1009 tp->t_flags |= TF_SENTFIN; 1010 } 1011 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) 1012 tp->snd_max = tp->snd_nxt + len; 1013 } 1014 1015 #ifdef TCPDEBUG 1016 /* 1017 * Trace. 1018 */ 1019 if (so->so_options & SO_DEBUG) { 1020 u_short save = 0; 1021 #ifdef INET6 1022 if (!isipv6) 1023 #endif 1024 { 1025 save = ipov->ih_len; 1026 ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */); 1027 } 1028 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 1029 #ifdef INET6 1030 if (!isipv6) 1031 #endif 1032 ipov->ih_len = save; 1033 } 1034 #endif 1035 1036 /* 1037 * Fill in IP length and desired time to live and 1038 * send to IP level. There should be a better way 1039 * to handle ttl and tos; we could keep them in 1040 * the template, but need a way to checksum without them. 1041 */ 1042 /* 1043 * m->m_pkthdr.len should have been set before cksum calcuration, 1044 * because in6_cksum() need it. 1045 */ 1046 #ifdef INET6 1047 if (isipv6) { 1048 /* 1049 * we separately set hoplimit for every segment, since the 1050 * user might want to change the value via setsockopt. 1051 * Also, desired default hop limit might be changed via 1052 * Neighbor Discovery. 1053 */ 1054 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); 1055 1056 /* TODO: IPv6 IP6TOS_ECT bit on */ 1057 error = ip6_output(m, 1058 tp->t_inpcb->in6p_outputopts, NULL, 1059 ((so->so_options & SO_DONTROUTE) ? 1060 IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb); 1061 } else 1062 #endif /* INET6 */ 1063 { 1064 ip->ip_len = m->m_pkthdr.len; 1065 #ifdef INET6 1066 if (INP_CHECK_SOCKAF(so, AF_INET6)) 1067 ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL); 1068 #endif /* INET6 */ 1069 /* 1070 * If we do path MTU discovery, then we set DF on every packet. 1071 * This might not be the best thing to do according to RFC3390 1072 * Section 2. However the tcp hostcache migitates the problem 1073 * so it affects only the first tcp connection with a host. 1074 */ 1075 if (path_mtu_discovery) 1076 ip->ip_off |= IP_DF; 1077 1078 error = ip_output(m, tp->t_inpcb->inp_options, NULL, 1079 ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, 1080 tp->t_inpcb); 1081 } 1082 if (error) { 1083 1084 /* 1085 * We know that the packet was lost, so back out the 1086 * sequence number advance, if any. 1087 */ 1088 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) { 1089 /* 1090 * No need to check for TH_FIN here because 1091 * the TF_SENTFIN flag handles that case. 1092 */ 1093 if ((flags & TH_SYN) == 0) { 1094 if (sack_rxmit) 1095 p->rxmit -= len; 1096 else 1097 tp->snd_nxt -= len; 1098 } 1099 } 1100 1101 out: 1102 SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */ 1103 if (error == ENOBUFS) { 1104 if (!callout_active(tp->tt_rexmt) && 1105 !callout_active(tp->tt_persist)) 1106 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 1107 tcp_timer_rexmt, tp); 1108 tp->snd_cwnd = tp->t_maxseg; 1109 return (0); 1110 } 1111 if (error == EMSGSIZE) { 1112 /* 1113 * ip_output() will have already fixed the route 1114 * for us. tcp_mtudisc() will, as its last action, 1115 * initiate retransmission, so it is important to 1116 * not do so here. 1117 */ 1118 tcp_mtudisc(tp->t_inpcb, 0); 1119 return 0; 1120 } 1121 if ((error == EHOSTUNREACH || error == ENETDOWN) 1122 && TCPS_HAVERCVDSYN(tp->t_state)) { 1123 tp->t_softerror = error; 1124 return (0); 1125 } 1126 return (error); 1127 } 1128 tcpstat.tcps_sndtotal++; 1129 1130 /* 1131 * Data sent (as far as we can tell). 1132 * If this advertises a larger window than any other segment, 1133 * then remember the size of the advertised window. 1134 * Any pending ACK has now been sent. 1135 */ 1136 if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 1137 tp->rcv_adv = tp->rcv_nxt + recwin; 1138 tp->last_ack_sent = tp->rcv_nxt; 1139 tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 1140 if (callout_active(tp->tt_delack)) 1141 callout_stop(tp->tt_delack); 1142 #if 0 1143 /* 1144 * This completely breaks TCP if newreno is turned on. What happens 1145 * is that if delayed-acks are turned on on the receiver, this code 1146 * on the transmitter effectively destroys the TCP window, forcing 1147 * it to four packets (1.5Kx4 = 6K window). 1148 */ 1149 if (sendalot && (!tcp_do_newreno || --maxburst)) 1150 goto again; 1151 #endif 1152 if (sendalot) 1153 goto again; 1154 return (0); 1155 } 1156 1157 void 1158 tcp_setpersist(tp) 1159 register struct tcpcb *tp; 1160 { 1161 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 1162 int tt; 1163 1164 if (callout_active(tp->tt_rexmt)) 1165 panic("tcp_setpersist: retransmit pending"); 1166 /* 1167 * Start/restart persistance timer. 1168 */ 1169 TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 1170 TCPTV_PERSMIN, TCPTV_PERSMAX); 1171 callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp); 1172 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1173 tp->t_rxtshift++; 1174 } 1175