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