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 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 = 4; 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(struct tcpcb *tp) 106 { 107 struct socket *so = tp->t_inpcb->inp_socket; 108 long len, win; 109 int off, flags, error; 110 struct mbuf *m; 111 struct ip *ip = NULL; 112 struct ipovly *ipov = NULL; 113 struct tcphdr *th; 114 u_char opt[TCP_MAXOLEN]; 115 unsigned ipoptlen, optlen, hdrlen; 116 int idle, sendalot; 117 #if 0 118 int maxburst = TCP_MAXBURST; 119 #endif 120 struct rmxp_tao *taop; 121 struct rmxp_tao tao_noncached; 122 #ifdef INET6 123 struct ip6_hdr *ip6 = NULL; 124 int isipv6; 125 126 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 127 #endif 128 129 /* 130 * Determine length of data that should be transmitted, 131 * and flags that will be used. 132 * If there is some data or critical controls (SYN, RST) 133 * to send, then transmit; otherwise, investigate further. 134 */ 135 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 136 if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) { 137 /* 138 * We have been idle for "a while" and no acks are 139 * expected to clock out any data we send -- 140 * slow start to get ack "clock" running again. 141 * 142 * Set the slow-start flight size depending on whether 143 * this is a local network or not. 144 */ 145 int ss = ss_fltsz; 146 #ifdef INET6 147 if (isipv6) { 148 if (in6_localaddr(&tp->t_inpcb->in6p_faddr)) 149 ss = ss_fltsz_local; 150 } else 151 #endif /* INET6 */ 152 if (in_localaddr(tp->t_inpcb->inp_faddr)) 153 ss = ss_fltsz_local; 154 tp->snd_cwnd = tp->t_maxseg * ss; 155 } 156 tp->t_flags &= ~TF_LASTIDLE; 157 if (idle) { 158 if (tp->t_flags & TF_MORETOCOME) { 159 tp->t_flags |= TF_LASTIDLE; 160 idle = 0; 161 } 162 } 163 again: 164 sendalot = 0; 165 off = tp->snd_nxt - tp->snd_una; 166 win = min(tp->snd_wnd, tp->snd_cwnd); 167 168 flags = tcp_outflags[tp->t_state]; 169 /* 170 * Get standard flags, and add SYN or FIN if requested by 'hidden' 171 * state flags. 172 */ 173 if (tp->t_flags & TF_NEEDFIN) 174 flags |= TH_FIN; 175 if (tp->t_flags & TF_NEEDSYN) 176 flags |= TH_SYN; 177 178 /* 179 * If in persist timeout with window of 0, send 1 byte. 180 * Otherwise, if window is small but nonzero 181 * and timer expired, we will send what we can 182 * and go to transmit state. 183 */ 184 if (tp->t_force) { 185 if (win == 0) { 186 /* 187 * If we still have some data to send, then 188 * clear the FIN bit. Usually this would 189 * happen below when it realizes that we 190 * aren't sending all the data. However, 191 * if we have exactly 1 byte of unsent data, 192 * then it won't clear the FIN bit below, 193 * and if we are in persist state, we wind 194 * up sending the packet without recording 195 * that we sent the FIN bit. 196 * 197 * We can't just blindly clear the FIN bit, 198 * because if we don't have any more data 199 * to send then the probe will be the FIN 200 * itself. 201 */ 202 if (off < so->so_snd.sb_cc) 203 flags &= ~TH_FIN; 204 win = 1; 205 } else { 206 callout_stop(tp->tt_persist); 207 tp->t_rxtshift = 0; 208 } 209 } 210 211 len = (long)ulmin(so->so_snd.sb_cc, win) - off; 212 213 if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 214 taop = &tao_noncached; 215 bzero(taop, sizeof(*taop)); 216 } 217 218 /* 219 * Lop off SYN bit if it has already been sent. However, if this 220 * is SYN-SENT state and if segment contains data and if we don't 221 * know that foreign host supports TAO, suppress sending segment. 222 */ 223 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 224 flags &= ~TH_SYN; 225 off--, len++; 226 if (len > 0 && tp->t_state == TCPS_SYN_SENT && 227 taop->tao_ccsent == 0) 228 return 0; 229 } 230 231 /* 232 * Be careful not to send data and/or FIN on SYN segments 233 * in cases when no CC option will be sent. 234 * This measure is needed to prevent interoperability problems 235 * with not fully conformant TCP implementations. 236 */ 237 if ((flags & TH_SYN) && 238 ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) || 239 ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) { 240 len = 0; 241 flags &= ~TH_FIN; 242 } 243 244 if (len < 0) { 245 /* 246 * If FIN has been sent but not acked, 247 * but we haven't been called to retransmit, 248 * len will be -1. Otherwise, window shrank 249 * after we sent into it. If window shrank to 0, 250 * cancel pending retransmit, pull snd_nxt back 251 * to (closed) window, and set the persist timer 252 * if it isn't already going. If the window didn't 253 * close completely, just wait for an ACK. 254 */ 255 len = 0; 256 if (win == 0) { 257 callout_stop(tp->tt_rexmt); 258 tp->t_rxtshift = 0; 259 tp->snd_nxt = tp->snd_una; 260 if (!callout_active(tp->tt_persist)) 261 tcp_setpersist(tp); 262 } 263 } 264 if (len > tp->t_maxseg) { 265 len = tp->t_maxseg; 266 sendalot = 1; 267 } 268 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 269 flags &= ~TH_FIN; 270 271 win = sbspace(&so->so_rcv); 272 273 /* 274 * Sender silly window avoidance. We transmit under the following 275 * conditions when len is non-zero: 276 * 277 * - We have a full segment 278 * - This is the last buffer in a write()/send() and we are 279 * either idle or running NODELAY 280 * - we've timed out (e.g. persist timer) 281 * - we have more then 1/2 the maximum send window's worth of 282 * data (receiver may be limited the window size) 283 * - we need to retransmit 284 */ 285 if (len) { 286 if (len == tp->t_maxseg) 287 goto send; 288 /* 289 * NOTE! on localhost connections an 'ack' from the remote 290 * end may occur synchronously with the output and cause 291 * us to flush a buffer queued with moretocome. XXX 292 * 293 * note: the len + off check is almost certainly unnecessary. 294 */ 295 if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 296 (idle || (tp->t_flags & TF_NODELAY)) && 297 len + off >= so->so_snd.sb_cc && 298 (tp->t_flags & TF_NOPUSH) == 0) { 299 goto send; 300 } 301 if (tp->t_force) /* typ. timeout case */ 302 goto send; 303 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 304 goto send; 305 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 306 goto send; 307 } 308 309 /* 310 * Compare available window to amount of window 311 * known to peer (as advertised window less 312 * next expected input). If the difference is at least two 313 * max size segments, or at least 50% of the maximum possible 314 * window, then want to send a window update to peer. 315 */ 316 if (win > 0) { 317 /* 318 * "adv" is the amount we can increase the window, 319 * taking into account that we are limited by 320 * TCP_MAXWIN << tp->rcv_scale. 321 */ 322 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) - 323 (tp->rcv_adv - tp->rcv_nxt); 324 325 if (adv >= (long) (2 * tp->t_maxseg)) 326 goto send; 327 if (2 * adv >= (long) so->so_rcv.sb_hiwat) 328 goto send; 329 } 330 331 /* 332 * Send if we owe peer an ACK. 333 */ 334 if (tp->t_flags & TF_ACKNOW) 335 goto send; 336 if ((flags & TH_RST) || 337 ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 338 goto send; 339 if (SEQ_GT(tp->snd_up, tp->snd_una)) 340 goto send; 341 /* 342 * If our state indicates that FIN should be sent 343 * and we have not yet done so, or we're retransmitting the FIN, 344 * then we need to send. 345 */ 346 if (flags & TH_FIN && 347 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 348 goto send; 349 350 /* 351 * TCP window updates are not reliable, rather a polling protocol 352 * using ``persist'' packets is used to insure receipt of window 353 * updates. The three ``states'' for the output side are: 354 * idle not doing retransmits or persists 355 * persisting to move a small or zero window 356 * (re)transmitting and thereby not persisting 357 * 358 * callout_active(tp->tt_persist) 359 * is true when we are in persist state. 360 * tp->t_force 361 * is set when we are called to send a persist packet. 362 * callout_active(tp->tt_rexmt) 363 * is set when we are retransmitting 364 * The output side is idle when both timers are zero. 365 * 366 * If send window is too small, there is data to transmit, and no 367 * retransmit or persist is pending, then go to persist state. 368 * If nothing happens soon, send when timer expires: 369 * if window is nonzero, transmit what we can, 370 * otherwise force out a byte. 371 */ 372 if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) && 373 !callout_active(tp->tt_persist)) { 374 tp->t_rxtshift = 0; 375 tcp_setpersist(tp); 376 } 377 378 /* 379 * No reason to send a segment, just return. 380 */ 381 return (0); 382 383 send: 384 /* 385 * Before ESTABLISHED, force sending of initial options 386 * unless TCP set not to do any options. 387 * NOTE: we assume that the IP/TCP header plus TCP options 388 * always fit in a single mbuf, leaving room for a maximum 389 * link header, i.e. 390 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 391 */ 392 optlen = 0; 393 #ifdef INET6 394 if (isipv6) 395 hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 396 else 397 #endif 398 hdrlen = sizeof (struct tcpiphdr); 399 if (flags & TH_SYN) { 400 tp->snd_nxt = tp->iss; 401 if ((tp->t_flags & TF_NOOPT) == 0) { 402 u_short mss; 403 404 opt[0] = TCPOPT_MAXSEG; 405 opt[1] = TCPOLEN_MAXSEG; 406 mss = htons((u_short) tcp_mssopt(tp)); 407 (void)memcpy(opt + 2, &mss, sizeof(mss)); 408 optlen = TCPOLEN_MAXSEG; 409 410 if ((tp->t_flags & TF_REQ_SCALE) && 411 ((flags & TH_ACK) == 0 || 412 (tp->t_flags & TF_RCVD_SCALE))) { 413 *((u_int32_t *)(opt + optlen)) = htonl( 414 TCPOPT_NOP << 24 | 415 TCPOPT_WINDOW << 16 | 416 TCPOLEN_WINDOW << 8 | 417 tp->request_r_scale); 418 optlen += 4; 419 } 420 } 421 } 422 423 /* 424 * Send a timestamp and echo-reply if this is a SYN and our side 425 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 426 * and our peer have sent timestamps in our SYN's. 427 */ 428 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 429 (flags & TH_RST) == 0 && 430 ((flags & TH_ACK) == 0 || 431 (tp->t_flags & TF_RCVD_TSTMP))) { 432 u_int32_t *lp = (u_int32_t *)(opt + optlen); 433 434 /* Form timestamp option as shown in appendix A of RFC 1323. */ 435 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 436 *lp++ = htonl(ticks); 437 *lp = htonl(tp->ts_recent); 438 optlen += TCPOLEN_TSTAMP_APPA; 439 } 440 441 /* 442 * Send `CC-family' options if our side wants to use them (TF_REQ_CC), 443 * options are allowed (!TF_NOOPT) and it's not a RST. 444 */ 445 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 446 (flags & TH_RST) == 0) { 447 switch (flags & (TH_SYN|TH_ACK)) { 448 /* 449 * This is a normal ACK, send CC if we received CC before 450 * from our peer. 451 */ 452 case TH_ACK: 453 if (!(tp->t_flags & TF_RCVD_CC)) 454 break; 455 /*FALLTHROUGH*/ 456 457 /* 458 * We can only get here in T/TCP's SYN_SENT* state, when 459 * we're a sending a non-SYN segment without waiting for 460 * the ACK of our SYN. A check above assures that we only 461 * do this if our peer understands T/TCP. 462 */ 463 case 0: 464 opt[optlen++] = TCPOPT_NOP; 465 opt[optlen++] = TCPOPT_NOP; 466 opt[optlen++] = TCPOPT_CC; 467 opt[optlen++] = TCPOLEN_CC; 468 *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); 469 470 optlen += 4; 471 break; 472 473 /* 474 * This is our initial SYN, check whether we have to use 475 * CC or CC.new. 476 */ 477 case TH_SYN: 478 opt[optlen++] = TCPOPT_NOP; 479 opt[optlen++] = TCPOPT_NOP; 480 opt[optlen++] = tp->t_flags & TF_SENDCCNEW ? 481 TCPOPT_CCNEW : TCPOPT_CC; 482 opt[optlen++] = TCPOLEN_CC; 483 *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); 484 optlen += 4; 485 break; 486 487 /* 488 * This is a SYN,ACK; send CC and CC.echo if we received 489 * CC from our peer. 490 */ 491 case (TH_SYN|TH_ACK): 492 if (tp->t_flags & TF_RCVD_CC) { 493 opt[optlen++] = TCPOPT_NOP; 494 opt[optlen++] = TCPOPT_NOP; 495 opt[optlen++] = TCPOPT_CC; 496 opt[optlen++] = TCPOLEN_CC; 497 *(u_int32_t *)&opt[optlen] = 498 htonl(tp->cc_send); 499 optlen += 4; 500 opt[optlen++] = TCPOPT_NOP; 501 opt[optlen++] = TCPOPT_NOP; 502 opt[optlen++] = TCPOPT_CCECHO; 503 opt[optlen++] = TCPOLEN_CC; 504 *(u_int32_t *)&opt[optlen] = 505 htonl(tp->cc_recv); 506 optlen += 4; 507 } 508 break; 509 } 510 } 511 512 hdrlen += optlen; 513 514 #ifdef INET6 515 if (isipv6) 516 ipoptlen = ip6_optlen(tp->t_inpcb); 517 else 518 #endif 519 if (tp->t_inpcb->inp_options) 520 ipoptlen = tp->t_inpcb->inp_options->m_len - 521 offsetof(struct ipoption, ipopt_list); 522 else 523 ipoptlen = 0; 524 #ifdef IPSEC 525 ipoptlen += ipsec_hdrsiz_tcp(tp); 526 #endif 527 528 /* 529 * Adjust data length if insertion of options will 530 * bump the packet length beyond the t_maxopd length. 531 * Clear the FIN bit because we cut off the tail of 532 * the segment. 533 */ 534 if (len + optlen + ipoptlen > tp->t_maxopd) { 535 /* 536 * If there is still more to send, don't close the connection. 537 */ 538 flags &= ~TH_FIN; 539 len = tp->t_maxopd - optlen - ipoptlen; 540 sendalot = 1; 541 } 542 543 /*#ifdef DIAGNOSTIC*/ 544 #ifdef INET6 545 if (max_linkhdr + hdrlen > MCLBYTES) 546 #else 547 if (max_linkhdr + hdrlen > MHLEN) 548 #endif 549 panic("tcphdr too big"); 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 701 702 /* 703 * Adjust the RXWIN0SENT flag - indicate that we have advertised 704 * a 0 window. This may cause the remote transmitter to stall. This 705 * flag tells soreceive() to disable delayed acknowledgements when 706 * draining the buffer. This can occur if the receiver is attempting 707 * to read more data then can be buffered prior to transmitting on 708 * the connection. 709 */ 710 if (win == 0) 711 tp->t_flags |= TF_RXWIN0SENT; 712 else 713 tp->t_flags &= ~TF_RXWIN0SENT; 714 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 715 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 716 th->th_flags |= TH_URG; 717 } else 718 /* 719 * If no urgent pointer to send, then we pull 720 * the urgent pointer to the left edge of the send window 721 * so that it doesn't drift into the send window on sequence 722 * number wraparound. 723 */ 724 tp->snd_up = tp->snd_una; /* drag it along */ 725 726 /* 727 * Put TCP length in extended header, and then 728 * checksum extended header and data. 729 */ 730 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 731 #ifdef INET6 732 if (isipv6) 733 /* 734 * ip6_plen is not need to be filled now, and will be filled 735 * in ip6_output. 736 */ 737 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 738 sizeof(struct tcphdr) + optlen + len); 739 else 740 #endif /* INET6 */ 741 { 742 m->m_pkthdr.csum_flags = CSUM_TCP; 743 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 744 if (len + optlen) 745 th->th_sum = in_addword(th->th_sum, 746 htons((u_short)(optlen + len))); 747 748 /* IP version must be set here for ipv4/ipv6 checking later */ 749 KASSERT(ip->ip_v == IPVERSION, 750 ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 751 } 752 753 /* 754 * In transmit state, time the transmission and arrange for 755 * the retransmit. In persist state, just set snd_max. 756 */ 757 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) { 758 tcp_seq startseq = tp->snd_nxt; 759 760 /* 761 * Advance snd_nxt over sequence space of this segment. 762 */ 763 if (flags & (TH_SYN|TH_FIN)) { 764 if (flags & TH_SYN) 765 tp->snd_nxt++; 766 if (flags & TH_FIN) { 767 tp->snd_nxt++; 768 tp->t_flags |= TF_SENTFIN; 769 } 770 } 771 tp->snd_nxt += len; 772 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 773 tp->snd_max = tp->snd_nxt; 774 /* 775 * Time this transmission if not a retransmission and 776 * not currently timing anything. 777 */ 778 if (tp->t_rtttime == 0) { 779 tp->t_rtttime = ticks; 780 tp->t_rtseq = startseq; 781 tcpstat.tcps_segstimed++; 782 } 783 } 784 785 /* 786 * Set retransmit timer if not currently set, 787 * and not doing an ack or a keep-alive probe. 788 * Initial value for retransmit timer is smoothed 789 * round-trip time + 2 * round-trip time variance. 790 * Initialize shift counter which is used for backoff 791 * of retransmit time. 792 */ 793 if (!callout_active(tp->tt_rexmt) && 794 tp->snd_nxt != tp->snd_una) { 795 if (callout_active(tp->tt_persist)) { 796 callout_stop(tp->tt_persist); 797 tp->t_rxtshift = 0; 798 } 799 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 800 tcp_timer_rexmt, tp); 801 } 802 } else 803 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) 804 tp->snd_max = tp->snd_nxt + len; 805 806 #ifdef TCPDEBUG 807 /* 808 * Trace. 809 */ 810 if (so->so_options & SO_DEBUG) 811 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 812 #endif 813 814 /* 815 * Fill in IP length and desired time to live and 816 * send to IP level. There should be a better way 817 * to handle ttl and tos; we could keep them in 818 * the template, but need a way to checksum without them. 819 */ 820 /* 821 * m->m_pkthdr.len should have been set before cksum calcuration, 822 * because in6_cksum() need it. 823 */ 824 #ifdef INET6 825 if (isipv6) { 826 /* 827 * we separately set hoplimit for every segment, since the 828 * user might want to change the value via setsockopt. 829 * Also, desired default hop limit might be changed via 830 * Neighbor Discovery. 831 */ 832 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, 833 tp->t_inpcb->in6p_route.ro_rt ? 834 tp->t_inpcb->in6p_route.ro_rt->rt_ifp 835 : NULL); 836 837 /* TODO: IPv6 IP6TOS_ECT bit on */ 838 #ifdef IPSEC 839 if (ipsec_setsocket(m, so) != 0) { 840 m_freem(m); 841 error = ENOBUFS; 842 goto out; 843 } 844 #endif /*IPSEC*/ 845 error = ip6_output(m, 846 tp->t_inpcb->in6p_outputopts, 847 &tp->t_inpcb->in6p_route, 848 (so->so_options & SO_DONTROUTE), NULL, NULL); 849 } else 850 #endif /* INET6 */ 851 { 852 struct rtentry *rt; 853 ip->ip_len = m->m_pkthdr.len; 854 #ifdef INET6 855 if (INP_CHECK_SOCKAF(so, AF_INET6)) 856 ip->ip_ttl = in6_selecthlim(tp->t_inpcb, 857 tp->t_inpcb->in6p_route.ro_rt ? 858 tp->t_inpcb->in6p_route.ro_rt->rt_ifp 859 : NULL); 860 else 861 #endif /* INET6 */ 862 ip->ip_ttl = tp->t_inpcb->inp_ip_ttl; /* XXX */ 863 ip->ip_tos = tp->t_inpcb->inp_ip_tos; /* XXX */ 864 /* 865 * See if we should do MTU discovery. We do it only if the following 866 * are true: 867 * 1) we have a valid route to the destination 868 * 2) the MTU is not locked (if it is, then discovery has been 869 * disabled) 870 */ 871 if (path_mtu_discovery 872 && (rt = tp->t_inpcb->inp_route.ro_rt) 873 && rt->rt_flags & RTF_UP 874 && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { 875 ip->ip_off |= IP_DF; 876 } 877 #ifdef IPSEC 878 ipsec_setsocket(m, so); 879 #endif /*IPSEC*/ 880 error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 881 (so->so_options & SO_DONTROUTE), 0); 882 } 883 if (error) { 884 885 /* 886 * We know that the packet was lost, so back out the 887 * sequence number advance, if any. 888 */ 889 if (tp->t_force == 0 || !callout_active(tp->tt_persist)) { 890 /* 891 * No need to check for TH_FIN here because 892 * the TF_SENTFIN flag handles that case. 893 */ 894 if ((flags & TH_SYN) == 0) 895 tp->snd_nxt -= len; 896 } 897 898 out: 899 if (error == ENOBUFS) { 900 if (!callout_active(tp->tt_rexmt) && 901 !callout_active(tp->tt_persist)) 902 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 903 tcp_timer_rexmt, tp); 904 tcp_quench(tp->t_inpcb, 0); 905 return (0); 906 } 907 if (error == EMSGSIZE) { 908 /* 909 * ip_output() will have already fixed the route 910 * for us. tcp_mtudisc() will, as its last action, 911 * initiate retransmission, so it is important to 912 * not do so here. 913 */ 914 tcp_mtudisc(tp->t_inpcb, 0); 915 return 0; 916 } 917 if ((error == EHOSTUNREACH || error == ENETDOWN) 918 && TCPS_HAVERCVDSYN(tp->t_state)) { 919 tp->t_softerror = error; 920 return (0); 921 } 922 return (error); 923 } 924 tcpstat.tcps_sndtotal++; 925 926 /* 927 * Data sent (as far as we can tell). 928 * If this advertises a larger window than any other segment, 929 * then remember the size of the advertised window. 930 * Any pending ACK has now been sent. 931 */ 932 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) 933 tp->rcv_adv = tp->rcv_nxt + win; 934 tp->last_ack_sent = tp->rcv_nxt; 935 tp->t_flags &= ~TF_ACKNOW; 936 if (tcp_delack_enabled) 937 callout_stop(tp->tt_delack); 938 #if 0 939 /* 940 * This completely breaks TCP if newreno is turned on. What happens 941 * is that if delayed-acks are turned on on the receiver, this code 942 * on the transmitter effectively destroys the TCP window, forcing 943 * it to four packets (1.5Kx4 = 6K window). 944 */ 945 if (sendalot && (!tcp_do_newreno || --maxburst)) 946 goto again; 947 #endif 948 if (sendalot) 949 goto again; 950 return (0); 951 } 952 953 void 954 tcp_setpersist(tp) 955 register struct tcpcb *tp; 956 { 957 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 958 int tt; 959 960 if (callout_active(tp->tt_rexmt)) 961 panic("tcp_setpersist: retransmit pending"); 962 /* 963 * Start/restart persistance timer. 964 */ 965 TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 966 TCPTV_PERSMIN, TCPTV_PERSMAX); 967 callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp); 968 if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 969 tp->t_rxtshift++; 970 } 971