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