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