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