110#ifdef TCPDEBUG 111#include <netinet/tcp_debug.h> 112#endif /* TCPDEBUG */ 113#ifdef TCP_OFFLOAD 114#include <netinet/tcp_offload.h> 115#endif 116 117#ifdef IPSEC 118#include <netipsec/ipsec.h> 119#include <netipsec/ipsec6.h> 120#endif /*IPSEC*/ 121 122#include <machine/in_cksum.h> 123 124#include <security/mac/mac_framework.h> 125 126const int tcprexmtthresh; 127 128VNET_DECLARE(int, tcp_autorcvbuf_inc); 129#define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 130VNET_DECLARE(int, tcp_autorcvbuf_max); 131#define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 132VNET_DECLARE(int, tcp_do_rfc3042); 133#define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 134VNET_DECLARE(int, tcp_do_autorcvbuf); 135#define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 136VNET_DECLARE(int, tcp_insecure_rst); 137#define V_tcp_insecure_rst VNET(tcp_insecure_rst) 138VNET_DECLARE(int, tcp_insecure_syn); 139#define V_tcp_insecure_syn VNET(tcp_insecure_syn) 140 141static void tcp_do_segment_fastslow(struct mbuf *, struct tcphdr *, 142 struct socket *, struct tcpcb *, int, int, uint8_t, 143 int); 144 145static void tcp_do_segment_fastack(struct mbuf *, struct tcphdr *, 146 struct socket *, struct tcpcb *, int, int, uint8_t, 147 int); 148 149/* 150 * Indicate whether this ack should be delayed. We can delay the ack if 151 * following conditions are met: 152 * - There is no delayed ack timer in progress. 153 * - Our last ack wasn't a 0-sized window. We never want to delay 154 * the ack that opens up a 0-sized window. 155 * - LRO wasn't used for this segment. We make sure by checking that the 156 * segment size is not larger than the MSS. 157 */ 158#define DELAY_ACK(tp, tlen) \ 159 ((!tcp_timer_active(tp, TT_DELACK) && \ 160 (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 161 (tlen <= tp->t_maxseg) && \ 162 (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 163 164/* 165 * So how is this faster than the normal fast ack? 166 * It basically allows us to also stay in the fastpath 167 * when a window-update ack also arrives. In testing 168 * we saw only 25-30% of connections doing fastpath 169 * due to the fact that along with moving forward 170 * in sequence the window was also updated. 171 */ 172static void 173tcp_do_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 174 struct tcpcb *tp, struct tcpopt *to, int drop_hdrlen, int tlen, 175 int ti_locked, u_long tiwin) 176{ 177 int acked; 178 int winup_only=0; 179#ifdef TCPDEBUG 180 /* 181 * The size of tcp_saveipgen must be the size of the max ip header, 182 * now IPv6. 183 */ 184 u_char tcp_saveipgen[IP6_HDR_LEN]; 185 struct tcphdr tcp_savetcp; 186 short ostate = 0; 187#endif 188 /* 189 * The following if statment will be true if 190 * we are doing the win_up_in_fp <and> 191 * - We have more new data (SEQ_LT(tp->snd_wl1, th->th_seq)) <or> 192 * - No more new data, but we have an ack for new data 193 * (tp->snd_wl1 == th->th_seq && SEQ_LT(tp->snd_wl2, th->th_ack)) 194 * - No more new data, the same ack point but the window grew 195 * (tp->snd_wl1 == th->th_seq && tp->snd_wl2 == th->th_ack && twin > tp->snd_wnd) 196 */ 197 if ((SEQ_LT(tp->snd_wl1, th->th_seq) || 198 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 199 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 200 /* keep track of pure window updates */ 201 if (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) { 202 winup_only = 1; 203 TCPSTAT_INC(tcps_rcvwinupd); 204 } 205 tp->snd_wnd = tiwin; 206 tp->snd_wl1 = th->th_seq; 207 tp->snd_wl2 = th->th_ack; 208 if (tp->snd_wnd > tp->max_sndwnd) 209 tp->max_sndwnd = tp->snd_wnd; 210 } 211 /* 212 * If last ACK falls within this segment's sequence numbers, 213 * record the timestamp. 214 * NOTE that the test is modified according to the latest 215 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 216 */ 217 if ((to->to_flags & TOF_TS) != 0 && 218 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 219 tp->ts_recent_age = tcp_ts_getticks(); 220 tp->ts_recent = to->to_tsval; 221 } 222 /* 223 * This is a pure ack for outstanding data. 224 */ 225 if (ti_locked == TI_RLOCKED) { 226 INP_INFO_RUNLOCK(&V_tcbinfo); 227 } 228 ti_locked = TI_UNLOCKED; 229 230 TCPSTAT_INC(tcps_predack); 231 232 /* 233 * "bad retransmit" recovery. 234 */ 235 if (tp->t_rxtshift == 1 && 236 tp->t_flags & TF_PREVVALID && 237 (int)(ticks - tp->t_badrxtwin) < 0) { 238 cc_cong_signal(tp, th, CC_RTO_ERR); 239 } 240 241 /* 242 * Recalculate the transmit timer / rtt. 243 * 244 * Some boxes send broken timestamp replies 245 * during the SYN+ACK phase, ignore 246 * timestamps of 0 or we could calculate a 247 * huge RTT and blow up the retransmit timer. 248 */ 249 if ((to->to_flags & TOF_TS) != 0 && 250 to->to_tsecr) { 251 u_int t; 252 253 t = tcp_ts_getticks() - to->to_tsecr; 254 if (!tp->t_rttlow || tp->t_rttlow > t) 255 tp->t_rttlow = t; 256 tcp_xmit_timer(tp, 257 TCP_TS_TO_TICKS(t) + 1); 258 } else if (tp->t_rtttime && 259 SEQ_GT(th->th_ack, tp->t_rtseq)) { 260 if (!tp->t_rttlow || 261 tp->t_rttlow > ticks - tp->t_rtttime) 262 tp->t_rttlow = ticks - tp->t_rtttime; 263 tcp_xmit_timer(tp, 264 ticks - tp->t_rtttime); 265 } 266 if (winup_only == 0) { 267 acked = BYTES_THIS_ACK(tp, th); 268 269 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 270 hhook_run_tcp_est_in(tp, th, to); 271 272 TCPSTAT_ADD(tcps_rcvackbyte, acked); 273 sbdrop(&so->so_snd, acked); 274 if (SEQ_GT(tp->snd_una, tp->snd_recover) && 275 SEQ_LEQ(th->th_ack, tp->snd_recover)) 276 tp->snd_recover = th->th_ack - 1; 277 278 /* 279 * Let the congestion control algorithm update 280 * congestion control related information. This 281 * typically means increasing the congestion 282 * window. 283 */ 284 cc_ack_received(tp, th, CC_ACK); 285 286 tp->snd_una = th->th_ack; 287 /* 288 * Pull snd_wl2 up to prevent seq wrap relative 289 * to th_ack. 290 */ 291 tp->snd_wl2 = th->th_ack; 292 tp->t_dupacks = 0; 293 m_freem(m); 294 295 /* 296 * If all outstanding data are acked, stop 297 * retransmit timer, otherwise restart timer 298 * using current (possibly backed-off) value. 299 * If process is waiting for space, 300 * wakeup/selwakeup/signal. If data 301 * are ready to send, let tcp_output 302 * decide between more output or persist. 303 */ 304#ifdef TCPDEBUG 305 if (so->so_options & SO_DEBUG) 306 tcp_trace(TA_INPUT, ostate, tp, 307 (void *)tcp_saveipgen, 308 &tcp_savetcp, 0); 309#endif 310 if (tp->snd_una == tp->snd_max) 311 tcp_timer_activate(tp, TT_REXMT, 0); 312 else if (!tcp_timer_active(tp, TT_PERSIST)) 313 tcp_timer_activate(tp, TT_REXMT, 314 tp->t_rxtcur); 315 } else { 316 /* 317 * Window update only, just free the mbufs and 318 * send out whatever we can. 319 */ 320 m_freem(m); 321 } 322 sowwakeup(so); 323 if (sbavail(&so->so_snd)) 324 (void) tcp_output(tp); 325 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 326 __func__, ti_locked)); 327 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 328 INP_WLOCK_ASSERT(tp->t_inpcb); 329 330 if (tp->t_flags & TF_DELACK) { 331 tp->t_flags &= ~TF_DELACK; 332 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 333 } 334 INP_WUNLOCK(tp->t_inpcb); 335} 336 337/* 338 * Here nothing is really faster, its just that we 339 * have broken out the fast-data path also just like 340 * the fast-ack. 341 */ 342static void 343tcp_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so, 344 struct tcpcb *tp, struct tcpopt *to, int drop_hdrlen, int tlen, 345 int ti_locked, u_long tiwin) 346{ 347 int newsize = 0; /* automatic sockbuf scaling */ 348#ifdef TCPDEBUG 349 /* 350 * The size of tcp_saveipgen must be the size of the max ip header, 351 * now IPv6. 352 */ 353 u_char tcp_saveipgen[IP6_HDR_LEN]; 354 struct tcphdr tcp_savetcp; 355 short ostate = 0; 356#endif 357 /* 358 * If last ACK falls within this segment's sequence numbers, 359 * record the timestamp. 360 * NOTE that the test is modified according to the latest 361 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 362 */ 363 if ((to->to_flags & TOF_TS) != 0 && 364 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 365 tp->ts_recent_age = tcp_ts_getticks(); 366 tp->ts_recent = to->to_tsval; 367 } 368 369 /* 370 * This is a pure, in-sequence data packet with 371 * nothing on the reassembly queue and we have enough 372 * buffer space to take it. 373 */ 374 if (ti_locked == TI_RLOCKED) { 375 INP_INFO_RUNLOCK(&V_tcbinfo); 376 } 377 ti_locked = TI_UNLOCKED; 378 379 /* Clean receiver SACK report if present */ 380 if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 381 tcp_clean_sackreport(tp); 382 TCPSTAT_INC(tcps_preddat); 383 tp->rcv_nxt += tlen; 384 /* 385 * Pull snd_wl1 up to prevent seq wrap relative to 386 * th_seq. 387 */ 388 tp->snd_wl1 = th->th_seq; 389 /* 390 * Pull rcv_up up to prevent seq wrap relative to 391 * rcv_nxt. 392 */ 393 tp->rcv_up = tp->rcv_nxt; 394 TCPSTAT_ADD(tcps_rcvbyte, tlen); 395#ifdef TCPDEBUG 396 if (so->so_options & SO_DEBUG) 397 tcp_trace(TA_INPUT, ostate, tp, 398 (void *)tcp_saveipgen, &tcp_savetcp, 0); 399#endif 400 /* 401 * Automatic sizing of receive socket buffer. Often the send 402 * buffer size is not optimally adjusted to the actual network 403 * conditions at hand (delay bandwidth product). Setting the 404 * buffer size too small limits throughput on links with high 405 * bandwidth and high delay (eg. trans-continental/oceanic links). 406 * 407 * On the receive side the socket buffer memory is only rarely 408 * used to any significant extent. This allows us to be much 409 * more aggressive in scaling the receive socket buffer. For 410 * the case that the buffer space is actually used to a large 411 * extent and we run out of kernel memory we can simply drop 412 * the new segments; TCP on the sender will just retransmit it 413 * later. Setting the buffer size too big may only consume too 414 * much kernel memory if the application doesn't read() from 415 * the socket or packet loss or reordering makes use of the 416 * reassembly queue. 417 * 418 * The criteria to step up the receive buffer one notch are: 419 * 1. Application has not set receive buffer size with 420 * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 421 * 2. the number of bytes received during the time it takes 422 * one timestamp to be reflected back to us (the RTT); 423 * 3. received bytes per RTT is within seven eighth of the 424 * current socket buffer size; 425 * 4. receive buffer size has not hit maximal automatic size; 426 * 427 * This algorithm does one step per RTT at most and only if 428 * we receive a bulk stream w/o packet losses or reorderings. 429 * Shrinking the buffer during idle times is not necessary as 430 * it doesn't consume any memory when idle. 431 * 432 * TODO: Only step up if the application is actually serving 433 * the buffer to better manage the socket buffer resources. 434 */ 435 if (V_tcp_do_autorcvbuf && 436 (to->to_flags & TOF_TS) && 437 to->to_tsecr && 438 (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 439 if (TSTMP_GT(to->to_tsecr, tp->rfbuf_ts) && 440 to->to_tsecr - tp->rfbuf_ts < hz) { 441 if (tp->rfbuf_cnt > 442 (so->so_rcv.sb_hiwat / 8 * 7) && 443 so->so_rcv.sb_hiwat < 444 V_tcp_autorcvbuf_max) { 445 newsize = 446 min(so->so_rcv.sb_hiwat + 447 V_tcp_autorcvbuf_inc, 448 V_tcp_autorcvbuf_max); 449 } 450 /* Start over with next RTT. */ 451 tp->rfbuf_ts = 0; 452 tp->rfbuf_cnt = 0; 453 } else 454 tp->rfbuf_cnt += tlen; /* add up */ 455 } 456 457 /* Add data to socket buffer. */ 458 SOCKBUF_LOCK(&so->so_rcv); 459 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 460 m_freem(m); 461 } else { 462 /* 463 * Set new socket buffer size. 464 * Give up when limit is reached. 465 */ 466 if (newsize) 467 if (!sbreserve_locked(&so->so_rcv, 468 newsize, so, NULL)) 469 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 470 m_adj(m, drop_hdrlen); /* delayed header drop */ 471 sbappendstream_locked(&so->so_rcv, m, 0); 472 } 473 /* NB: sorwakeup_locked() does an implicit unlock. */ 474 sorwakeup_locked(so); 475 if (DELAY_ACK(tp, tlen)) { 476 tp->t_flags |= TF_DELACK; 477 } else { 478 tp->t_flags |= TF_ACKNOW; 479 tcp_output(tp); 480 } 481 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 482 __func__, ti_locked)); 483 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 484 INP_WLOCK_ASSERT(tp->t_inpcb); 485 486 if (tp->t_flags & TF_DELACK) { 487 tp->t_flags &= ~TF_DELACK; 488 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 489 } 490 INP_WUNLOCK(tp->t_inpcb); 491} 492 493/* 494 * The slow-path is the clone of the long long part 495 * of tcp_do_segment past all the fast-path stuff. We 496 * use it here by two different callers, the fast/slow and 497 * the fastack only. 498 */ 499static void 500tcp_do_slowpath(struct mbuf *m, struct tcphdr *th, struct socket *so, 501 struct tcpcb *tp, struct tcpopt *to, int drop_hdrlen, int tlen, 502 int ti_locked, u_long tiwin, int thflags) 503{ 504 int acked, ourfinisacked, needoutput = 0; 505 int rstreason, todrop, win; 506 char *s; 507 struct in_conninfo *inc; 508 struct mbuf *mfree = NULL; 509#ifdef TCPDEBUG 510 /* 511 * The size of tcp_saveipgen must be the size of the max ip header, 512 * now IPv6. 513 */ 514 u_char tcp_saveipgen[IP6_HDR_LEN]; 515 struct tcphdr tcp_savetcp; 516 short ostate = 0; 517#endif 518 /* 519 * Calculate amount of space in receive window, 520 * and then do TCP input processing. 521 * Receive window is amount of space in rcv queue, 522 * but not less than advertised window. 523 */ 524 inc = &tp->t_inpcb->inp_inc; 525 win = sbspace(&so->so_rcv); 526 if (win < 0) 527 win = 0; 528 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 529 530 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 531 tp->rfbuf_ts = 0; 532 tp->rfbuf_cnt = 0; 533 534 switch (tp->t_state) { 535 536 /* 537 * If the state is SYN_RECEIVED: 538 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 539 */ 540 case TCPS_SYN_RECEIVED: 541 if ((thflags & TH_ACK) && 542 (SEQ_LEQ(th->th_ack, tp->snd_una) || 543 SEQ_GT(th->th_ack, tp->snd_max))) { 544 rstreason = BANDLIM_RST_OPENPORT; 545 goto dropwithreset; 546 } 547 break; 548 549 /* 550 * If the state is SYN_SENT: 551 * if seg contains an ACK, but not for our SYN, drop the input. 552 * if seg contains a RST, then drop the connection. 553 * if seg does not contain SYN, then drop it. 554 * Otherwise this is an acceptable SYN segment 555 * initialize tp->rcv_nxt and tp->irs 556 * if seg contains ack then advance tp->snd_una 557 * if seg contains an ECE and ECN support is enabled, the stream 558 * is ECN capable. 559 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 560 * arrange for segment to be acked (eventually) 561 * continue processing rest of data/controls, beginning with URG 562 */ 563 case TCPS_SYN_SENT: 564 if ((thflags & TH_ACK) && 565 (SEQ_LEQ(th->th_ack, tp->iss) || 566 SEQ_GT(th->th_ack, tp->snd_max))) { 567 rstreason = BANDLIM_UNLIMITED; 568 goto dropwithreset; 569 } 570 if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 571 TCP_PROBE5(connect__refused, NULL, tp, 572 mtod(m, const char *), tp, th); 573 tp = tcp_drop(tp, ECONNREFUSED); 574 } 575 if (thflags & TH_RST) 576 goto drop; 577 if (!(thflags & TH_SYN)) 578 goto drop; 579 580 tp->irs = th->th_seq; 581 tcp_rcvseqinit(tp); 582 if (thflags & TH_ACK) { 583 TCPSTAT_INC(tcps_connects); 584 soisconnected(so); 585#ifdef MAC 586 mac_socketpeer_set_from_mbuf(m, so); 587#endif 588 /* Do window scaling on this connection? */ 589 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 590 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 591 tp->rcv_scale = tp->request_r_scale; 592 } 593 tp->rcv_adv += imin(tp->rcv_wnd, 594 TCP_MAXWIN << tp->rcv_scale); 595 tp->snd_una++; /* SYN is acked */ 596 /* 597 * If there's data, delay ACK; if there's also a FIN 598 * ACKNOW will be turned on later. 599 */ 600 if (DELAY_ACK(tp, tlen) && tlen != 0) 601 tcp_timer_activate(tp, TT_DELACK, 602 tcp_delacktime); 603 else 604 tp->t_flags |= TF_ACKNOW; 605 606 if ((thflags & TH_ECE) && V_tcp_do_ecn) { 607 tp->t_flags |= TF_ECN_PERMIT; 608 TCPSTAT_INC(tcps_ecn_shs); 609 } 610 611 /* 612 * Received <SYN,ACK> in SYN_SENT[*] state. 613 * Transitions: 614 * SYN_SENT --> ESTABLISHED 615 * SYN_SENT* --> FIN_WAIT_1 616 */ 617 tp->t_starttime = ticks; 618 if (tp->t_flags & TF_NEEDFIN) { 619 tcp_state_change(tp, TCPS_FIN_WAIT_1); 620 tp->t_flags &= ~TF_NEEDFIN; 621 thflags &= ~TH_SYN; 622 } else { 623 tcp_state_change(tp, TCPS_ESTABLISHED); 624 TCP_PROBE5(connect__established, NULL, tp, 625 mtod(m, const char *), tp, th); 626 cc_conn_init(tp); 627 tcp_timer_activate(tp, TT_KEEP, 628 TP_KEEPIDLE(tp)); 629 } 630 } else { 631 /* 632 * Received initial SYN in SYN-SENT[*] state => 633 * simultaneous open. 634 * If it succeeds, connection is * half-synchronized. 635 * Otherwise, do 3-way handshake: 636 * SYN-SENT -> SYN-RECEIVED 637 * SYN-SENT* -> SYN-RECEIVED* 638 */ 639 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 640 tcp_timer_activate(tp, TT_REXMT, 0); 641 tcp_state_change(tp, TCPS_SYN_RECEIVED); 642 } 643 644 KASSERT(ti_locked == TI_RLOCKED, ("%s: trimthenstep6: " 645 "ti_locked %d", __func__, ti_locked)); 646 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 647 INP_WLOCK_ASSERT(tp->t_inpcb); 648 649 /* 650 * Advance th->th_seq to correspond to first data byte. 651 * If data, trim to stay within window, 652 * dropping FIN if necessary. 653 */ 654 th->th_seq++; 655 if (tlen > tp->rcv_wnd) { 656 todrop = tlen - tp->rcv_wnd; 657 m_adj(m, -todrop); 658 tlen = tp->rcv_wnd; 659 thflags &= ~TH_FIN; 660 TCPSTAT_INC(tcps_rcvpackafterwin); 661 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 662 } 663 tp->snd_wl1 = th->th_seq - 1; 664 tp->rcv_up = th->th_seq; 665 /* 666 * Client side of transaction: already sent SYN and data. 667 * If the remote host used T/TCP to validate the SYN, 668 * our data will be ACK'd; if so, enter normal data segment 669 * processing in the middle of step 5, ack processing. 670 * Otherwise, goto step 6. 671 */ 672 if (thflags & TH_ACK) 673 goto process_ACK; 674 675 goto step6; 676 677 /* 678 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 679 * do normal processing. 680 * 681 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 682 */ 683 case TCPS_LAST_ACK: 684 case TCPS_CLOSING: 685 break; /* continue normal processing */ 686 } 687 688 /* 689 * States other than LISTEN or SYN_SENT. 690 * First check the RST flag and sequence number since reset segments 691 * are exempt from the timestamp and connection count tests. This 692 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 693 * below which allowed reset segments in half the sequence space 694 * to fall though and be processed (which gives forged reset 695 * segments with a random sequence number a 50 percent chance of 696 * killing a connection). 697 * Then check timestamp, if present. 698 * Then check the connection count, if present. 699 * Then check that at least some bytes of segment are within 700 * receive window. If segment begins before rcv_nxt, 701 * drop leading data (and SYN); if nothing left, just ack. 702 */ 703 if (thflags & TH_RST) { 704 /* 705 * RFC5961 Section 3.2 706 * 707 * - RST drops connection only if SEG.SEQ == RCV.NXT. 708 * - If RST is in window, we send challenge ACK. 709 * 710 * Note: to take into account delayed ACKs, we should 711 * test against last_ack_sent instead of rcv_nxt. 712 * Note 2: we handle special case of closed window, not 713 * covered by the RFC. 714 */ 715 if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 716 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 717 (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 718 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 719 KASSERT(ti_locked == TI_RLOCKED, 720 ("%s: TH_RST ti_locked %d, th %p tp %p", 721 __func__, ti_locked, th, tp)); 722 KASSERT(tp->t_state != TCPS_SYN_SENT, 723 ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 724 __func__, th, tp)); 725 726 if (V_tcp_insecure_rst || 727 tp->last_ack_sent == th->th_seq) { 728 TCPSTAT_INC(tcps_drops); 729 /* Drop the connection. */ 730 switch (tp->t_state) { 731 case TCPS_SYN_RECEIVED: 732 so->so_error = ECONNREFUSED; 733 goto close; 734 case TCPS_ESTABLISHED: 735 case TCPS_FIN_WAIT_1: 736 case TCPS_FIN_WAIT_2: 737 case TCPS_CLOSE_WAIT: 738 so->so_error = ECONNRESET; 739 close: 740 tcp_state_change(tp, TCPS_CLOSED); 741 /* FALLTHROUGH */ 742 default: 743 tp = tcp_close(tp); 744 } 745 } else { 746 TCPSTAT_INC(tcps_badrst); 747 /* Send challenge ACK. */ 748 tcp_respond(tp, mtod(m, void *), th, m, 749 tp->rcv_nxt, tp->snd_nxt, TH_ACK); 750 tp->last_ack_sent = tp->rcv_nxt; 751 m = NULL; 752 } 753 } 754 goto drop; 755 } 756 757 /* 758 * RFC5961 Section 4.2 759 * Send challenge ACK for any SYN in synchronized state. 760 */ 761 if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT) { 762 KASSERT(ti_locked == TI_RLOCKED, 763 ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 764 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 765 766 TCPSTAT_INC(tcps_badsyn); 767 if (V_tcp_insecure_syn && 768 SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 769 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 770 tp = tcp_drop(tp, ECONNRESET); 771 rstreason = BANDLIM_UNLIMITED; 772 } else { 773 /* Send challenge ACK. */ 774 tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 775 tp->snd_nxt, TH_ACK); 776 tp->last_ack_sent = tp->rcv_nxt; 777 m = NULL; 778 } 779 goto drop; 780 } 781 782 /* 783 * RFC 1323 PAWS: If we have a timestamp reply on this segment 784 * and it's less than ts_recent, drop it. 785 */ 786 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 787 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 788 789 /* Check to see if ts_recent is over 24 days old. */ 790 if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 791 /* 792 * Invalidate ts_recent. If this segment updates 793 * ts_recent, the age will be reset later and ts_recent 794 * will get a valid value. If it does not, setting 795 * ts_recent to zero will at least satisfy the 796 * requirement that zero be placed in the timestamp 797 * echo reply when ts_recent isn't valid. The 798 * age isn't reset until we get a valid ts_recent 799 * because we don't want out-of-order segments to be 800 * dropped when ts_recent is old. 801 */ 802 tp->ts_recent = 0; 803 } else { 804 TCPSTAT_INC(tcps_rcvduppack); 805 TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 806 TCPSTAT_INC(tcps_pawsdrop); 807 if (tlen) 808 goto dropafterack; 809 goto drop; 810 } 811 } 812 813 /* 814 * In the SYN-RECEIVED state, validate that the packet belongs to 815 * this connection before trimming the data to fit the receive 816 * window. Check the sequence number versus IRS since we know 817 * the sequence numbers haven't wrapped. This is a partial fix 818 * for the "LAND" DoS attack. 819 */ 820 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 821 rstreason = BANDLIM_RST_OPENPORT; 822 goto dropwithreset; 823 } 824 825 todrop = tp->rcv_nxt - th->th_seq; 826 if (todrop > 0) { 827 if (thflags & TH_SYN) { 828 thflags &= ~TH_SYN; 829 th->th_seq++; 830 if (th->th_urp > 1) 831 th->th_urp--; 832 else 833 thflags &= ~TH_URG; 834 todrop--; 835 } 836 /* 837 * Following if statement from Stevens, vol. 2, p. 960. 838 */ 839 if (todrop > tlen 840 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 841 /* 842 * Any valid FIN must be to the left of the window. 843 * At this point the FIN must be a duplicate or out 844 * of sequence; drop it. 845 */ 846 thflags &= ~TH_FIN; 847 848 /* 849 * Send an ACK to resynchronize and drop any data. 850 * But keep on processing for RST or ACK. 851 */ 852 tp->t_flags |= TF_ACKNOW; 853 todrop = tlen; 854 TCPSTAT_INC(tcps_rcvduppack); 855 TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 856 } else { 857 TCPSTAT_INC(tcps_rcvpartduppack); 858 TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 859 } 860 drop_hdrlen += todrop; /* drop from the top afterwards */ 861 th->th_seq += todrop; 862 tlen -= todrop; 863 if (th->th_urp > todrop) 864 th->th_urp -= todrop; 865 else { 866 thflags &= ~TH_URG; 867 th->th_urp = 0; 868 } 869 } 870 871 /* 872 * If new data are received on a connection after the 873 * user processes are gone, then RST the other end. 874 */ 875 if ((so->so_state & SS_NOFDREF) && 876 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 877 KASSERT(ti_locked == TI_RLOCKED, ("%s: SS_NOFDEREF && " 878 "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 879 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 880 881 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 882 log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 883 "after socket was closed, " 884 "sending RST and removing tcpcb\n", 885 s, __func__, tcpstates[tp->t_state], tlen); 886 free(s, M_TCPLOG); 887 } 888 tp = tcp_close(tp); 889 TCPSTAT_INC(tcps_rcvafterclose); 890 rstreason = BANDLIM_UNLIMITED; 891 goto dropwithreset; 892 } 893 894 /* 895 * If segment ends after window, drop trailing data 896 * (and PUSH and FIN); if nothing left, just ACK. 897 */ 898 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 899 if (todrop > 0) { 900 TCPSTAT_INC(tcps_rcvpackafterwin); 901 if (todrop >= tlen) { 902 TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 903 /* 904 * If window is closed can only take segments at 905 * window edge, and have to drop data and PUSH from 906 * incoming segments. Continue processing, but 907 * remember to ack. Otherwise, drop segment 908 * and ack. 909 */ 910 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 911 tp->t_flags |= TF_ACKNOW; 912 TCPSTAT_INC(tcps_rcvwinprobe); 913 } else 914 goto dropafterack; 915 } else 916 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 917 m_adj(m, -todrop); 918 tlen -= todrop; 919 thflags &= ~(TH_PUSH|TH_FIN); 920 } 921 922 /* 923 * If last ACK falls within this segment's sequence numbers, 924 * record its timestamp. 925 * NOTE: 926 * 1) That the test incorporates suggestions from the latest 927 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 928 * 2) That updating only on newer timestamps interferes with 929 * our earlier PAWS tests, so this check should be solely 930 * predicated on the sequence space of this segment. 931 * 3) That we modify the segment boundary check to be 932 * Last.ACK.Sent <= SEG.SEQ + SEG.Len 933 * instead of RFC1323's 934 * Last.ACK.Sent < SEG.SEQ + SEG.Len, 935 * This modified check allows us to overcome RFC1323's 936 * limitations as described in Stevens TCP/IP Illustrated 937 * Vol. 2 p.869. In such cases, we can still calculate the 938 * RTT correctly when RCV.NXT == Last.ACK.Sent. 939 */ 940 if ((to->to_flags & TOF_TS) != 0 && 941 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 942 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 943 ((thflags & (TH_SYN|TH_FIN)) != 0))) { 944 tp->ts_recent_age = tcp_ts_getticks(); 945 tp->ts_recent = to->to_tsval; 946 } 947 948 /* 949 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 950 * flag is on (half-synchronized state), then queue data for 951 * later processing; else drop segment and return. 952 */ 953 if ((thflags & TH_ACK) == 0) { 954 if (tp->t_state == TCPS_SYN_RECEIVED || 955 (tp->t_flags & TF_NEEDSYN)) 956 goto step6; 957 else if (tp->t_flags & TF_ACKNOW) 958 goto dropafterack; 959 else 960 goto drop; 961 } 962 963 /* 964 * Ack processing. 965 */ 966 switch (tp->t_state) { 967 968 /* 969 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 970 * ESTABLISHED state and continue processing. 971 * The ACK was checked above. 972 */ 973 case TCPS_SYN_RECEIVED: 974 975 TCPSTAT_INC(tcps_connects); 976 soisconnected(so); 977 /* Do window scaling? */ 978 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 979 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 980 tp->rcv_scale = tp->request_r_scale; 981 tp->snd_wnd = tiwin; 982 } 983 /* 984 * Make transitions: 985 * SYN-RECEIVED -> ESTABLISHED 986 * SYN-RECEIVED* -> FIN-WAIT-1 987 */ 988 tp->t_starttime = ticks; 989 if (tp->t_flags & TF_NEEDFIN) { 990 tcp_state_change(tp, TCPS_FIN_WAIT_1); 991 tp->t_flags &= ~TF_NEEDFIN; 992 } else { 993 tcp_state_change(tp, TCPS_ESTABLISHED); 994 TCP_PROBE5(accept__established, NULL, tp, 995 mtod(m, const char *), tp, th); 996 cc_conn_init(tp); 997 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 998 } 999 /* 1000 * If segment contains data or ACK, will call tcp_reass() 1001 * later; if not, do so now to pass queued data to user. 1002 */ 1003 if (tlen == 0 && (thflags & TH_FIN) == 0) 1004 (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1005 (struct mbuf *)0); 1006 tp->snd_wl1 = th->th_seq - 1; 1007 /* FALLTHROUGH */ 1008 1009 /* 1010 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1011 * ACKs. If the ack is in the range 1012 * tp->snd_una < th->th_ack <= tp->snd_max 1013 * then advance tp->snd_una to th->th_ack and drop 1014 * data from the retransmission queue. If this ACK reflects 1015 * more up to date window information we update our window information. 1016 */ 1017 case TCPS_ESTABLISHED: 1018 case TCPS_FIN_WAIT_1: 1019 case TCPS_FIN_WAIT_2: 1020 case TCPS_CLOSE_WAIT: 1021 case TCPS_CLOSING: 1022 case TCPS_LAST_ACK: 1023 if (SEQ_GT(th->th_ack, tp->snd_max)) { 1024 TCPSTAT_INC(tcps_rcvacktoomuch); 1025 goto dropafterack; 1026 } 1027 if ((tp->t_flags & TF_SACK_PERMIT) && 1028 ((to->to_flags & TOF_SACK) || 1029 !TAILQ_EMPTY(&tp->snd_holes))) 1030 tcp_sack_doack(tp, to, th->th_ack); 1031 else 1032 /* 1033 * Reset the value so that previous (valid) value 1034 * from the last ack with SACK doesn't get used. 1035 */ 1036 tp->sackhint.sacked_bytes = 0; 1037 1038 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 1039 hhook_run_tcp_est_in(tp, th, to); 1040 1041 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1042 if (tlen == 0 && tiwin == tp->snd_wnd) { 1043 /* 1044 * If this is the first time we've seen a 1045 * FIN from the remote, this is not a 1046 * duplicate and it needs to be processed 1047 * normally. This happens during a 1048 * simultaneous close. 1049 */ 1050 if ((thflags & TH_FIN) && 1051 (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 1052 tp->t_dupacks = 0; 1053 break; 1054 } 1055 TCPSTAT_INC(tcps_rcvdupack); 1056 /* 1057 * If we have outstanding data (other than 1058 * a window probe), this is a completely 1059 * duplicate ack (ie, window info didn't 1060 * change and FIN isn't set), 1061 * the ack is the biggest we've 1062 * seen and we've seen exactly our rexmt 1063 * threshhold of them, assume a packet 1064 * has been dropped and retransmit it. 1065 * Kludge snd_nxt & the congestion 1066 * window so we send only this one 1067 * packet. 1068 * 1069 * We know we're losing at the current 1070 * window size so do congestion avoidance 1071 * (set ssthresh to half the current window 1072 * and pull our congestion window back to 1073 * the new ssthresh). 1074 * 1075 * Dup acks mean that packets have left the 1076 * network (they're now cached at the receiver) 1077 * so bump cwnd by the amount in the receiver 1078 * to keep a constant cwnd packets in the 1079 * network. 1080 * 1081 * When using TCP ECN, notify the peer that 1082 * we reduced the cwnd. 1083 */ 1084 if (!tcp_timer_active(tp, TT_REXMT) || 1085 th->th_ack != tp->snd_una) 1086 tp->t_dupacks = 0; 1087 else if (++tp->t_dupacks > tcprexmtthresh || 1088 IN_FASTRECOVERY(tp->t_flags)) { 1089 cc_ack_received(tp, th, CC_DUPACK); 1090 if ((tp->t_flags & TF_SACK_PERMIT) && 1091 IN_FASTRECOVERY(tp->t_flags)) { 1092 int awnd; 1093 1094 /* 1095 * Compute the amount of data in flight first. 1096 * We can inject new data into the pipe iff 1097 * we have less than 1/2 the original window's 1098 * worth of data in flight. 1099 */ 1100 if (V_tcp_do_rfc6675_pipe) 1101 awnd = tcp_compute_pipe(tp); 1102 else 1103 awnd = (tp->snd_nxt - tp->snd_fack) + 1104 tp->sackhint.sack_bytes_rexmit; 1105 1106 if (awnd < tp->snd_ssthresh) { 1107 tp->snd_cwnd += tp->t_maxseg; 1108 if (tp->snd_cwnd > tp->snd_ssthresh) 1109 tp->snd_cwnd = tp->snd_ssthresh; 1110 } 1111 } else 1112 tp->snd_cwnd += tp->t_maxseg; 1113 (void) tp->t_fb->tfb_tcp_output(tp); 1114 goto drop; 1115 } else if (tp->t_dupacks == tcprexmtthresh) { 1116 tcp_seq onxt = tp->snd_nxt; 1117 1118 /* 1119 * If we're doing sack, check to 1120 * see if we're already in sack 1121 * recovery. If we're not doing sack, 1122 * check to see if we're in newreno 1123 * recovery. 1124 */ 1125 if (tp->t_flags & TF_SACK_PERMIT) { 1126 if (IN_FASTRECOVERY(tp->t_flags)) { 1127 tp->t_dupacks = 0; 1128 break; 1129 } 1130 } else { 1131 if (SEQ_LEQ(th->th_ack, 1132 tp->snd_recover)) { 1133 tp->t_dupacks = 0; 1134 break; 1135 } 1136 } 1137 /* Congestion signal before ack. */ 1138 cc_cong_signal(tp, th, CC_NDUPACK); 1139 cc_ack_received(tp, th, CC_DUPACK); 1140 tcp_timer_activate(tp, TT_REXMT, 0); 1141 tp->t_rtttime = 0; 1142 if (tp->t_flags & TF_SACK_PERMIT) { 1143 TCPSTAT_INC( 1144 tcps_sack_recovery_episode); 1145 tp->sack_newdata = tp->snd_nxt; 1146 tp->snd_cwnd = tp->t_maxseg; 1147 (void) tp->t_fb->tfb_tcp_output(tp); 1148 goto drop; 1149 } 1150 tp->snd_nxt = th->th_ack; 1151 tp->snd_cwnd = tp->t_maxseg; 1152 (void) tp->t_fb->tfb_tcp_output(tp); 1153 KASSERT(tp->snd_limited <= 2, 1154 ("%s: tp->snd_limited too big", 1155 __func__)); 1156 tp->snd_cwnd = tp->snd_ssthresh + 1157 tp->t_maxseg * 1158 (tp->t_dupacks - tp->snd_limited); 1159 if (SEQ_GT(onxt, tp->snd_nxt)) 1160 tp->snd_nxt = onxt; 1161 goto drop; 1162 } else if (V_tcp_do_rfc3042) { 1163 /* 1164 * Process first and second duplicate 1165 * ACKs. Each indicates a segment 1166 * leaving the network, creating room 1167 * for more. Make sure we can send a 1168 * packet on reception of each duplicate 1169 * ACK by increasing snd_cwnd by one 1170 * segment. Restore the original 1171 * snd_cwnd after packet transmission. 1172 */ 1173 cc_ack_received(tp, th, CC_DUPACK); 1174 u_long oldcwnd = tp->snd_cwnd; 1175 tcp_seq oldsndmax = tp->snd_max; 1176 u_int sent; 1177 int avail; 1178 1179 KASSERT(tp->t_dupacks == 1 || 1180 tp->t_dupacks == 2, 1181 ("%s: dupacks not 1 or 2", 1182 __func__)); 1183 if (tp->t_dupacks == 1) 1184 tp->snd_limited = 0; 1185 tp->snd_cwnd = 1186 (tp->snd_nxt - tp->snd_una) + 1187 (tp->t_dupacks - tp->snd_limited) * 1188 tp->t_maxseg; 1189 /* 1190 * Only call tcp_output when there 1191 * is new data available to be sent. 1192 * Otherwise we would send pure ACKs. 1193 */ 1194 SOCKBUF_LOCK(&so->so_snd); 1195 avail = sbavail(&so->so_snd) - 1196 (tp->snd_nxt - tp->snd_una); 1197 SOCKBUF_UNLOCK(&so->so_snd); 1198 if (avail > 0) 1199 (void) tp->t_fb->tfb_tcp_output(tp); 1200 sent = tp->snd_max - oldsndmax; 1201 if (sent > tp->t_maxseg) { 1202 KASSERT((tp->t_dupacks == 2 && 1203 tp->snd_limited == 0) || 1204 (sent == tp->t_maxseg + 1 && 1205 tp->t_flags & TF_SENTFIN), 1206 ("%s: sent too much", 1207 __func__)); 1208 tp->snd_limited = 2; 1209 } else if (sent > 0) 1210 ++tp->snd_limited; 1211 tp->snd_cwnd = oldcwnd; 1212 goto drop; 1213 } 1214 } else 1215 tp->t_dupacks = 0; 1216 break; 1217 } 1218 1219 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 1220 ("%s: th_ack <= snd_una", __func__)); 1221 1222 /* 1223 * If the congestion window was inflated to account 1224 * for the other side's cached packets, retract it. 1225 */ 1226 if (IN_FASTRECOVERY(tp->t_flags)) { 1227 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 1228 if (tp->t_flags & TF_SACK_PERMIT) 1229 tcp_sack_partialack(tp, th); 1230 else 1231 tcp_newreno_partial_ack(tp, th); 1232 } else 1233 cc_post_recovery(tp, th); 1234 } 1235 tp->t_dupacks = 0; 1236 /* 1237 * If we reach this point, ACK is not a duplicate, 1238 * i.e., it ACKs something we sent. 1239 */ 1240 if (tp->t_flags & TF_NEEDSYN) { 1241 /* 1242 * T/TCP: Connection was half-synchronized, and our 1243 * SYN has been ACK'd (so connection is now fully 1244 * synchronized). Go to non-starred state, 1245 * increment snd_una for ACK of SYN, and check if 1246 * we can do window scaling. 1247 */ 1248 tp->t_flags &= ~TF_NEEDSYN; 1249 tp->snd_una++; 1250 /* Do window scaling? */ 1251 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1252 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1253 tp->rcv_scale = tp->request_r_scale; 1254 /* Send window already scaled. */ 1255 } 1256 } 1257 1258process_ACK: 1259 INP_WLOCK_ASSERT(tp->t_inpcb); 1260 1261 acked = BYTES_THIS_ACK(tp, th); 1262 TCPSTAT_INC(tcps_rcvackpack); 1263 TCPSTAT_ADD(tcps_rcvackbyte, acked); 1264 1265 /* 1266 * If we just performed our first retransmit, and the ACK 1267 * arrives within our recovery window, then it was a mistake 1268 * to do the retransmit in the first place. Recover our 1269 * original cwnd and ssthresh, and proceed to transmit where 1270 * we left off. 1271 */ 1272 if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 1273 (int)(ticks - tp->t_badrxtwin) < 0) 1274 cc_cong_signal(tp, th, CC_RTO_ERR); 1275 1276 /* 1277 * If we have a timestamp reply, update smoothed 1278 * round trip time. If no timestamp is present but 1279 * transmit timer is running and timed sequence 1280 * number was acked, update smoothed round trip time. 1281 * Since we now have an rtt measurement, cancel the 1282 * timer backoff (cf., Phil Karn's retransmit alg.). 1283 * Recompute the initial retransmit timer. 1284 * 1285 * Some boxes send broken timestamp replies 1286 * during the SYN+ACK phase, ignore 1287 * timestamps of 0 or we could calculate a 1288 * huge RTT and blow up the retransmit timer. 1289 */ 1290 if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) { 1291 u_int t; 1292 1293 t = tcp_ts_getticks() - to->to_tsecr; 1294 if (!tp->t_rttlow || tp->t_rttlow > t) 1295 tp->t_rttlow = t; 1296 tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 1297 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 1298 if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 1299 tp->t_rttlow = ticks - tp->t_rtttime; 1300 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1301 } 1302 1303 /* 1304 * If all outstanding data is acked, stop retransmit 1305 * timer and remember to restart (more output or persist). 1306 * If there is more data to be acked, restart retransmit 1307 * timer, using current (possibly backed-off) value. 1308 */ 1309 if (th->th_ack == tp->snd_max) { 1310 tcp_timer_activate(tp, TT_REXMT, 0); 1311 needoutput = 1; 1312 } else if (!tcp_timer_active(tp, TT_PERSIST)) 1313 tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 1314 1315 /* 1316 * If no data (only SYN) was ACK'd, 1317 * skip rest of ACK processing. 1318 */ 1319 if (acked == 0) 1320 goto step6; 1321 1322 /* 1323 * Let the congestion control algorithm update congestion 1324 * control related information. This typically means increasing 1325 * the congestion window. 1326 */ 1327 cc_ack_received(tp, th, CC_ACK); 1328 1329 SOCKBUF_LOCK(&so->so_snd); 1330 if (acked > sbavail(&so->so_snd)) { 1331 tp->snd_wnd -= sbavail(&so->so_snd); 1332 mfree = sbcut_locked(&so->so_snd, 1333 (int)sbavail(&so->so_snd)); 1334 ourfinisacked = 1; 1335 } else { 1336 mfree = sbcut_locked(&so->so_snd, acked); 1337 tp->snd_wnd -= acked; 1338 ourfinisacked = 0; 1339 } 1340 /* NB: sowwakeup_locked() does an implicit unlock. */ 1341 sowwakeup_locked(so); 1342 m_freem(mfree); 1343 /* Detect una wraparound. */ 1344 if (!IN_RECOVERY(tp->t_flags) && 1345 SEQ_GT(tp->snd_una, tp->snd_recover) && 1346 SEQ_LEQ(th->th_ack, tp->snd_recover)) 1347 tp->snd_recover = th->th_ack - 1; 1348 /* XXXLAS: Can this be moved up into cc_post_recovery? */ 1349 if (IN_RECOVERY(tp->t_flags) && 1350 SEQ_GEQ(th->th_ack, tp->snd_recover)) { 1351 EXIT_RECOVERY(tp->t_flags); 1352 } 1353 tp->snd_una = th->th_ack; 1354 if (tp->t_flags & TF_SACK_PERMIT) { 1355 if (SEQ_GT(tp->snd_una, tp->snd_recover)) 1356 tp->snd_recover = tp->snd_una; 1357 } 1358 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1359 tp->snd_nxt = tp->snd_una; 1360 1361 switch (tp->t_state) { 1362 1363 /* 1364 * In FIN_WAIT_1 STATE in addition to the processing 1365 * for the ESTABLISHED state if our FIN is now acknowledged 1366 * then enter FIN_WAIT_2. 1367 */ 1368 case TCPS_FIN_WAIT_1: 1369 if (ourfinisacked) { 1370 /* 1371 * If we can't receive any more 1372 * data, then closing user can proceed. 1373 * Starting the timer is contrary to the 1374 * specification, but if we don't get a FIN 1375 * we'll hang forever. 1376 * 1377 * XXXjl: 1378 * we should release the tp also, and use a 1379 * compressed state. 1380 */ 1381 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1382 soisdisconnected(so); 1383 tcp_timer_activate(tp, TT_2MSL, 1384 (tcp_fast_finwait2_recycle ? 1385 tcp_finwait2_timeout : 1386 TP_MAXIDLE(tp))); 1387 } 1388 tcp_state_change(tp, TCPS_FIN_WAIT_2); 1389 } 1390 break; 1391 1392 /* 1393 * In CLOSING STATE in addition to the processing for 1394 * the ESTABLISHED state if the ACK acknowledges our FIN 1395 * then enter the TIME-WAIT state, otherwise ignore 1396 * the segment. 1397 */ 1398 case TCPS_CLOSING: 1399 if (ourfinisacked) { 1400 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1401 tcp_twstart(tp); 1402 INP_INFO_RUNLOCK(&V_tcbinfo); 1403 m_freem(m); 1404 return; 1405 } 1406 break; 1407 1408 /* 1409 * In LAST_ACK, we may still be waiting for data to drain 1410 * and/or to be acked, as well as for the ack of our FIN. 1411 * If our FIN is now acknowledged, delete the TCB, 1412 * enter the closed state and return. 1413 */ 1414 case TCPS_LAST_ACK: 1415 if (ourfinisacked) { 1416 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1417 tp = tcp_close(tp); 1418 goto drop; 1419 } 1420 break; 1421 } 1422 } 1423 1424step6: 1425 INP_WLOCK_ASSERT(tp->t_inpcb); 1426 1427 /* 1428 * Update window information. 1429 * Don't look at window if no ACK: TAC's send garbage on first SYN. 1430 */ 1431 if ((thflags & TH_ACK) && 1432 (SEQ_LT(tp->snd_wl1, th->th_seq) || 1433 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 1434 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 1435 /* keep track of pure window updates */ 1436 if (tlen == 0 && 1437 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 1438 TCPSTAT_INC(tcps_rcvwinupd); 1439 tp->snd_wnd = tiwin; 1440 tp->snd_wl1 = th->th_seq; 1441 tp->snd_wl2 = th->th_ack; 1442 if (tp->snd_wnd > tp->max_sndwnd) 1443 tp->max_sndwnd = tp->snd_wnd; 1444 needoutput = 1; 1445 } 1446 1447 /* 1448 * Process segments with URG. 1449 */ 1450 if ((thflags & TH_URG) && th->th_urp && 1451 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1452 /* 1453 * This is a kludge, but if we receive and accept 1454 * random urgent pointers, we'll crash in 1455 * soreceive. It's hard to imagine someone 1456 * actually wanting to send this much urgent data. 1457 */ 1458 SOCKBUF_LOCK(&so->so_rcv); 1459 if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 1460 th->th_urp = 0; /* XXX */ 1461 thflags &= ~TH_URG; /* XXX */ 1462 SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 1463 goto dodata; /* XXX */ 1464 } 1465 /* 1466 * If this segment advances the known urgent pointer, 1467 * then mark the data stream. This should not happen 1468 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 1469 * a FIN has been received from the remote side. 1470 * In these states we ignore the URG. 1471 * 1472 * According to RFC961 (Assigned Protocols), 1473 * the urgent pointer points to the last octet 1474 * of urgent data. We continue, however, 1475 * to consider it to indicate the first octet 1476 * of data past the urgent section as the original 1477 * spec states (in one of two places). 1478 */ 1479 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 1480 tp->rcv_up = th->th_seq + th->th_urp; 1481 so->so_oobmark = sbavail(&so->so_rcv) + 1482 (tp->rcv_up - tp->rcv_nxt) - 1; 1483 if (so->so_oobmark == 0) 1484 so->so_rcv.sb_state |= SBS_RCVATMARK; 1485 sohasoutofband(so); 1486 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1487 } 1488 SOCKBUF_UNLOCK(&so->so_rcv); 1489 /* 1490 * Remove out of band data so doesn't get presented to user. 1491 * This can happen independent of advancing the URG pointer, 1492 * but if two URG's are pending at once, some out-of-band 1493 * data may creep in... ick. 1494 */ 1495 if (th->th_urp <= (u_long)tlen && 1496 !(so->so_options & SO_OOBINLINE)) { 1497 /* hdr drop is delayed */ 1498 tcp_pulloutofband(so, th, m, drop_hdrlen); 1499 } 1500 } else { 1501 /* 1502 * If no out of band data is expected, 1503 * pull receive urgent pointer along 1504 * with the receive window. 1505 */ 1506 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1507 tp->rcv_up = tp->rcv_nxt; 1508 } 1509dodata: /* XXX */ 1510 INP_WLOCK_ASSERT(tp->t_inpcb); 1511 1512 /* 1513 * Process the segment text, merging it into the TCP sequencing queue, 1514 * and arranging for acknowledgment of receipt if necessary. 1515 * This process logically involves adjusting tp->rcv_wnd as data 1516 * is presented to the user (this happens in tcp_usrreq.c, 1517 * case PRU_RCVD). If a FIN has already been received on this 1518 * connection then we just ignore the text. 1519 */ 1520 if ((tlen || (thflags & TH_FIN)) && 1521 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1522 tcp_seq save_start = th->th_seq; 1523 m_adj(m, drop_hdrlen); /* delayed header drop */ 1524 /* 1525 * Insert segment which includes th into TCP reassembly queue 1526 * with control block tp. Set thflags to whether reassembly now 1527 * includes a segment with FIN. This handles the common case 1528 * inline (segment is the next to be received on an established 1529 * connection, and the queue is empty), avoiding linkage into 1530 * and removal from the queue and repetition of various 1531 * conversions. 1532 * Set DELACK for segments received in order, but ack 1533 * immediately when segments are out of order (so 1534 * fast retransmit can work). 1535 */ 1536 if (th->th_seq == tp->rcv_nxt && 1537 LIST_EMPTY(&tp->t_segq) && 1538 TCPS_HAVEESTABLISHED(tp->t_state)) { 1539 if (DELAY_ACK(tp, tlen)) 1540 tp->t_flags |= TF_DELACK; 1541 else 1542 tp->t_flags |= TF_ACKNOW; 1543 tp->rcv_nxt += tlen; 1544 thflags = th->th_flags & TH_FIN; 1545 TCPSTAT_INC(tcps_rcvpack); 1546 TCPSTAT_ADD(tcps_rcvbyte, tlen); 1547 SOCKBUF_LOCK(&so->so_rcv); 1548 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 1549 m_freem(m); 1550 else 1551 sbappendstream_locked(&so->so_rcv, m, 0); 1552 /* NB: sorwakeup_locked() does an implicit unlock. */ 1553 sorwakeup_locked(so); 1554 } else { 1555 /* 1556 * XXX: Due to the header drop above "th" is 1557 * theoretically invalid by now. Fortunately 1558 * m_adj() doesn't actually frees any mbufs 1559 * when trimming from the head. 1560 */ 1561 thflags = tcp_reass(tp, th, &tlen, m); 1562 tp->t_flags |= TF_ACKNOW; 1563 } 1564 if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 1565 tcp_update_sack_list(tp, save_start, save_start + tlen); 1566#if 0 1567 /* 1568 * Note the amount of data that peer has sent into 1569 * our window, in order to estimate the sender's 1570 * buffer size. 1571 * XXX: Unused. 1572 */ 1573 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 1574 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1575 else 1576 len = so->so_rcv.sb_hiwat; 1577#endif 1578 } else { 1579 m_freem(m); 1580 thflags &= ~TH_FIN; 1581 } 1582 1583 /* 1584 * If FIN is received ACK the FIN and let the user know 1585 * that the connection is closing. 1586 */ 1587 if (thflags & TH_FIN) { 1588 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1589 socantrcvmore(so); 1590 /* 1591 * If connection is half-synchronized 1592 * (ie NEEDSYN flag on) then delay ACK, 1593 * so it may be piggybacked when SYN is sent. 1594 * Otherwise, since we received a FIN then no 1595 * more input can be expected, send ACK now. 1596 */ 1597 if (tp->t_flags & TF_NEEDSYN) 1598 tp->t_flags |= TF_DELACK; 1599 else 1600 tp->t_flags |= TF_ACKNOW; 1601 tp->rcv_nxt++; 1602 } 1603 switch (tp->t_state) { 1604 1605 /* 1606 * In SYN_RECEIVED and ESTABLISHED STATES 1607 * enter the CLOSE_WAIT state. 1608 */ 1609 case TCPS_SYN_RECEIVED: 1610 tp->t_starttime = ticks; 1611 /* FALLTHROUGH */ 1612 case TCPS_ESTABLISHED: 1613 tcp_state_change(tp, TCPS_CLOSE_WAIT); 1614 break; 1615 1616 /* 1617 * If still in FIN_WAIT_1 STATE FIN has not been acked so 1618 * enter the CLOSING state. 1619 */ 1620 case TCPS_FIN_WAIT_1: 1621 tcp_state_change(tp, TCPS_CLOSING); 1622 break; 1623 1624 /* 1625 * In FIN_WAIT_2 state enter the TIME_WAIT state, 1626 * starting the time-wait timer, turning off the other 1627 * standard timers. 1628 */ 1629 case TCPS_FIN_WAIT_2: 1630 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1631 KASSERT(ti_locked == TI_RLOCKED, ("%s: dodata " 1632 "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 1633 ti_locked)); 1634 1635 tcp_twstart(tp); 1636 INP_INFO_RUNLOCK(&V_tcbinfo); 1637 return; 1638 } 1639 } 1640 if (ti_locked == TI_RLOCKED) { 1641 INP_INFO_RUNLOCK(&V_tcbinfo); 1642 } 1643 ti_locked = TI_UNLOCKED; 1644 1645#ifdef TCPDEBUG 1646 if (so->so_options & SO_DEBUG) 1647 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 1648 &tcp_savetcp, 0); 1649#endif 1650 TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 1651 1652 /* 1653 * Return any desired output. 1654 */ 1655 if (needoutput || (tp->t_flags & TF_ACKNOW)) 1656 (void) tp->t_fb->tfb_tcp_output(tp); 1657 1658 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 1659 __func__, ti_locked)); 1660 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1661 INP_WLOCK_ASSERT(tp->t_inpcb); 1662 1663 if (tp->t_flags & TF_DELACK) { 1664 tp->t_flags &= ~TF_DELACK; 1665 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 1666 } 1667 INP_WUNLOCK(tp->t_inpcb); 1668 return; 1669 1670dropafterack: 1671 /* 1672 * Generate an ACK dropping incoming segment if it occupies 1673 * sequence space, where the ACK reflects our state. 1674 * 1675 * We can now skip the test for the RST flag since all 1676 * paths to this code happen after packets containing 1677 * RST have been dropped. 1678 * 1679 * In the SYN-RECEIVED state, don't send an ACK unless the 1680 * segment we received passes the SYN-RECEIVED ACK test. 1681 * If it fails send a RST. This breaks the loop in the 1682 * "LAND" DoS attack, and also prevents an ACK storm 1683 * between two listening ports that have been sent forged 1684 * SYN segments, each with the source address of the other. 1685 */ 1686 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 1687 (SEQ_GT(tp->snd_una, th->th_ack) || 1688 SEQ_GT(th->th_ack, tp->snd_max)) ) { 1689 rstreason = BANDLIM_RST_OPENPORT; 1690 goto dropwithreset; 1691 } 1692#ifdef TCPDEBUG 1693 if (so->so_options & SO_DEBUG) 1694 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 1695 &tcp_savetcp, 0); 1696#endif 1697 TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 1698 if (ti_locked == TI_RLOCKED) { 1699 INP_INFO_RUNLOCK(&V_tcbinfo); 1700 } 1701 ti_locked = TI_UNLOCKED; 1702 1703 tp->t_flags |= TF_ACKNOW; 1704 (void) tp->t_fb->tfb_tcp_output(tp); 1705 INP_WUNLOCK(tp->t_inpcb); 1706 m_freem(m); 1707 return; 1708 1709dropwithreset: 1710 if (ti_locked == TI_RLOCKED) { 1711 INP_INFO_RUNLOCK(&V_tcbinfo); 1712 } 1713 ti_locked = TI_UNLOCKED; 1714 1715 if (tp != NULL) { 1716 tcp_dropwithreset(m, th, tp, tlen, rstreason); 1717 INP_WUNLOCK(tp->t_inpcb); 1718 } else 1719 tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1720 return; 1721 1722drop: 1723 if (ti_locked == TI_RLOCKED) { 1724 INP_INFO_RUNLOCK(&V_tcbinfo); 1725 ti_locked = TI_UNLOCKED; 1726 } 1727#ifdef INVARIANTS 1728 else 1729 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1730#endif 1731 1732 /* 1733 * Drop space held by incoming segment and return. 1734 */ 1735#ifdef TCPDEBUG 1736 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1737 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 1738 &tcp_savetcp, 0); 1739#endif 1740 TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 1741 if (tp != NULL) 1742 INP_WUNLOCK(tp->t_inpcb); 1743 m_freem(m); 1744} 1745 1746 1747/* 1748 * Do fast slow is a combination of the original 1749 * tcp_dosegment and a split fastpath, one function 1750 * for the fast-ack which also includes allowing fastpath 1751 * for window advanced in sequence acks. And also a 1752 * sub-function that handles the insequence data. 1753 */ 1754void 1755tcp_do_segment_fastslow(struct mbuf *m, struct tcphdr *th, struct socket *so, 1756 struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1757 int ti_locked) 1758{ 1759 int thflags; 1760 u_long tiwin; 1761 char *s; 1762 int can_enter; 1763 struct in_conninfo *inc; 1764 struct tcpopt to; 1765 1766 thflags = th->th_flags; 1767 tp->sackhint.last_sack_ack = 0; 1768 inc = &tp->t_inpcb->inp_inc; 1769 /* 1770 * If this is either a state-changing packet or current state isn't 1771 * established, we require a write lock on tcbinfo. Otherwise, we 1772 * allow the tcbinfo to be in either alocked or unlocked, as the 1773 * caller may have unnecessarily acquired a write lock due to a race. 1774 */ 1775 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1776 tp->t_state != TCPS_ESTABLISHED) { 1777 KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for " 1778 "SYN/FIN/RST/!EST", __func__, ti_locked)); 1779 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1780 } else { 1781#ifdef INVARIANTS 1782 if (ti_locked == TI_RLOCKED) { 1783 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1784 } else { 1785 KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1786 "ti_locked: %d", __func__, ti_locked)); 1787 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1788 } 1789#endif 1790 } 1791 INP_WLOCK_ASSERT(tp->t_inpcb); 1792 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1793 __func__)); 1794 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1795 __func__)); 1796 1797 /* 1798 * Segment received on connection. 1799 * Reset idle time and keep-alive timer. 1800 * XXX: This should be done after segment 1801 * validation to ignore broken/spoofed segs. 1802 */ 1803 tp->t_rcvtime = ticks; 1804 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1805 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 1806 1807 /* 1808 * Unscale the window into a 32-bit value. 1809 * For the SYN_SENT state the scale is zero. 1810 */ 1811 tiwin = th->th_win << tp->snd_scale; 1812 1813 /* 1814 * TCP ECN processing. 1815 */ 1816 if (tp->t_flags & TF_ECN_PERMIT) { 1817 if (thflags & TH_CWR) 1818 tp->t_flags &= ~TF_ECN_SND_ECE; 1819 switch (iptos & IPTOS_ECN_MASK) { 1820 case IPTOS_ECN_CE: 1821 tp->t_flags |= TF_ECN_SND_ECE; 1822 TCPSTAT_INC(tcps_ecn_ce); 1823 break; 1824 case IPTOS_ECN_ECT0: 1825 TCPSTAT_INC(tcps_ecn_ect0); 1826 break; 1827 case IPTOS_ECN_ECT1: 1828 TCPSTAT_INC(tcps_ecn_ect1); 1829 break; 1830 } 1831 /* Congestion experienced. */ 1832 if (thflags & TH_ECE) { 1833 cc_cong_signal(tp, th, CC_ECN); 1834 } 1835 } 1836 1837 /* 1838 * Parse options on any incoming segment. 1839 */ 1840 tcp_dooptions(&to, (u_char *)(th + 1), 1841 (th->th_off << 2) - sizeof(struct tcphdr), 1842 (thflags & TH_SYN) ? TO_SYN : 0); 1843 1844 /* 1845 * If echoed timestamp is later than the current time, 1846 * fall back to non RFC1323 RTT calculation. Normalize 1847 * timestamp if syncookies were used when this connection 1848 * was established. 1849 */ 1850 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1851 to.to_tsecr -= tp->ts_offset; 1852 if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1853 to.to_tsecr = 0; 1854 } 1855 /* 1856 * If timestamps were negotiated during SYN/ACK they should 1857 * appear on every segment during this session and vice versa. 1858 */ 1859 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 1860 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1861 log(LOG_DEBUG, "%s; %s: Timestamp missing, " 1862 "no action\n", s, __func__); 1863 free(s, M_TCPLOG); 1864 } 1865 } 1866 if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 1867 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1868 log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 1869 "no action\n", s, __func__); 1870 free(s, M_TCPLOG); 1871 } 1872 } 1873 1874 /* 1875 * Process options only when we get SYN/ACK back. The SYN case 1876 * for incoming connections is handled in tcp_syncache. 1877 * According to RFC1323 the window field in a SYN (i.e., a <SYN> 1878 * or <SYN,ACK>) segment itself is never scaled. 1879 * XXX this is traditional behavior, may need to be cleaned up. 1880 */ 1881 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1882 if ((to.to_flags & TOF_SCALE) && 1883 (tp->t_flags & TF_REQ_SCALE)) { 1884 tp->t_flags |= TF_RCVD_SCALE; 1885 tp->snd_scale = to.to_wscale; 1886 } 1887 /* 1888 * Initial send window. It will be updated with 1889 * the next incoming segment to the scaled value. 1890 */ 1891 tp->snd_wnd = th->th_win; 1892 if (to.to_flags & TOF_TS) { 1893 tp->t_flags |= TF_RCVD_TSTMP; 1894 tp->ts_recent = to.to_tsval; 1895 tp->ts_recent_age = tcp_ts_getticks(); 1896 } 1897 if (to.to_flags & TOF_MSS) 1898 tcp_mss(tp, to.to_mss); 1899 if ((tp->t_flags & TF_SACK_PERMIT) && 1900 (to.to_flags & TOF_SACKPERM) == 0) 1901 tp->t_flags &= ~TF_SACK_PERMIT; 1902 } 1903 can_enter = 0; 1904 if (__predict_true((tlen == 0))) { 1905 /* 1906 * The ack moved forward and we have a window (non-zero) 1907 * <or> 1908 * The ack did not move forward, but the window increased. 1909 */ 1910 if (__predict_true((SEQ_GT(th->th_ack, tp->snd_una) && tiwin) || 1911 ((th->th_ack == tp->snd_una) && tiwin && (tiwin > tp->snd_wnd)))) { 1912 can_enter = 1; 1913 } 1914 } else { 1915 /* 1916 * Data incoming, use the old entry criteria 1917 * for fast-path with data. 1918 */ 1919 if ((tiwin && tiwin == tp->snd_wnd)) { 1920 can_enter = 1; 1921 } 1922 } 1923 /* 1924 * Header prediction: check for the two common cases 1925 * of a uni-directional data xfer. If the packet has 1926 * no control flags, is in-sequence, the window didn't 1927 * change and we're not retransmitting, it's a 1928 * candidate. If the length is zero and the ack moved 1929 * forward, we're the sender side of the xfer. Just 1930 * free the data acked & wake any higher level process 1931 * that was blocked waiting for space. If the length 1932 * is non-zero and the ack didn't move, we're the 1933 * receiver side. If we're getting packets in-order 1934 * (the reassembly queue is empty), add the data to 1935 * the socket buffer and note that we need a delayed ack. 1936 * Make sure that the hidden state-flags are also off. 1937 * Since we check for TCPS_ESTABLISHED first, it can only 1938 * be TH_NEEDSYN. 1939 */ 1940 if (__predict_true(tp->t_state == TCPS_ESTABLISHED && 1941 th->th_seq == tp->rcv_nxt && 1942 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1943 tp->snd_nxt == tp->snd_max && 1944 can_enter && 1945 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1946 LIST_EMPTY(&tp->t_segq) && 1947 ((to.to_flags & TOF_TS) == 0 || 1948 TSTMP_GEQ(to.to_tsval, tp->ts_recent)))) { 1949 if (__predict_true((tlen == 0) && 1950 (SEQ_LEQ(th->th_ack, tp->snd_max) && 1951 !IN_RECOVERY(tp->t_flags) && 1952 (to.to_flags & TOF_SACK) == 0 && 1953 TAILQ_EMPTY(&tp->snd_holes)))) { 1954 /* We are done */ 1955 tcp_do_fastack(m, th, so, tp, &to, drop_hdrlen, tlen, 1956 ti_locked, tiwin); 1957 return; 1958 } else if ((tlen) && 1959 (th->th_ack == tp->snd_una && 1960 tlen <= sbspace(&so->so_rcv))) { 1961 tcp_do_fastnewdata(m, th, so, tp, &to, drop_hdrlen, tlen, 1962 ti_locked, tiwin); 1963 /* We are done */ 1964 return; 1965 } 1966 } 1967 tcp_do_slowpath(m, th, so, tp, &to, drop_hdrlen, tlen, 1968 ti_locked, tiwin, thflags); 1969} 1970 1971 1972/* 1973 * This subfunction is used to try to highly optimize the 1974 * fast path. We again allow window updates that are 1975 * in sequence to remain in the fast-path. We also add 1976 * in the __predict's to attempt to help the compiler. 1977 * Note that if we return a 0, then we can *not* process 1978 * it and the caller should push the packet into the 1979 * slow-path. 1980 */ 1981static int 1982tcp_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 1983 struct tcpcb *tp, struct tcpopt *to, int drop_hdrlen, int tlen, 1984 int ti_locked, u_long tiwin) 1985{ 1986 int acked; 1987 int winup_only=0; 1988#ifdef TCPDEBUG 1989 /* 1990 * The size of tcp_saveipgen must be the size of the max ip header, 1991 * now IPv6. 1992 */ 1993 u_char tcp_saveipgen[IP6_HDR_LEN]; 1994 struct tcphdr tcp_savetcp; 1995 short ostate = 0; 1996#endif 1997 1998 1999 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 2000 /* Old ack, behind (or duplicate to) the last one rcv'd */ 2001 return (0); 2002 } 2003 if (__predict_false(th->th_ack == tp->snd_una) && 2004 __predict_false(tiwin <= tp->snd_wnd)) { 2005 /* duplicate ack <or> a shrinking dup ack with shrinking window */ 2006 return (0); 2007 } 2008 if (__predict_false(tiwin == 0)) { 2009 /* zero window */ 2010 return (0); 2011 } 2012 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) { 2013 /* Above what we have sent? */ 2014 return (0); 2015 } 2016 if (__predict_false(tp->snd_nxt != tp->snd_max)) { 2017 /* We are retransmitting */ 2018 return (0); 2019 } 2020 if (__predict_false(tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))) { 2021 /* We need a SYN or a FIN, unlikely.. */ 2022 return (0); 2023 } 2024 if((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) { 2025 /* Timestamp is behind .. old ack with seq wrap? */ 2026 return (0); 2027 } 2028 if (__predict_false(IN_RECOVERY(tp->t_flags))) { 2029 /* Still recovering */ 2030 return (0); 2031 } 2032 if (__predict_false(to->to_flags & TOF_SACK)) { 2033 /* Sack included in the ack.. */ 2034 return (0); 2035 } 2036 if (!TAILQ_EMPTY(&tp->snd_holes)) { 2037 /* We have sack holes on our scoreboard */ 2038 return (0); 2039 } 2040 /* Ok if we reach here, we can process a fast-ack */ 2041 2042 /* Did the window get updated? */ 2043 if (tiwin != tp->snd_wnd) { 2044 /* keep track of pure window updates */ 2045 if (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) { 2046 winup_only = 1; 2047 TCPSTAT_INC(tcps_rcvwinupd); 2048 } 2049 tp->snd_wnd = tiwin; 2050 tp->snd_wl1 = th->th_seq; 2051 if (tp->snd_wnd > tp->max_sndwnd) 2052 tp->max_sndwnd = tp->snd_wnd; 2053 } 2054 /* 2055 * Pull snd_wl2 up to prevent seq wrap relative 2056 * to th_ack. 2057 */ 2058 tp->snd_wl2 = th->th_ack; 2059 /* 2060 * If last ACK falls within this segment's sequence numbers, 2061 * record the timestamp. 2062 * NOTE that the test is modified according to the latest 2063 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2064 */ 2065 if ((to->to_flags & TOF_TS) != 0 && 2066 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 2067 tp->ts_recent_age = tcp_ts_getticks(); 2068 tp->ts_recent = to->to_tsval; 2069 } 2070 /* 2071 * This is a pure ack for outstanding data. 2072 */ 2073 if (ti_locked == TI_RLOCKED) { 2074 INP_INFO_RUNLOCK(&V_tcbinfo); 2075 } 2076 ti_locked = TI_UNLOCKED; 2077 2078 TCPSTAT_INC(tcps_predack); 2079 2080 /* 2081 * "bad retransmit" recovery. 2082 */ 2083 if (tp->t_rxtshift == 1 && 2084 tp->t_flags & TF_PREVVALID && 2085 (int)(ticks - tp->t_badrxtwin) < 0) { 2086 cc_cong_signal(tp, th, CC_RTO_ERR); 2087 } 2088 2089 /* 2090 * Recalculate the transmit timer / rtt. 2091 * 2092 * Some boxes send broken timestamp replies 2093 * during the SYN+ACK phase, ignore 2094 * timestamps of 0 or we could calculate a 2095 * huge RTT and blow up the retransmit timer. 2096 */ 2097 if ((to->to_flags & TOF_TS) != 0 && 2098 to->to_tsecr) { 2099 u_int t; 2100 2101 t = tcp_ts_getticks() - to->to_tsecr; 2102 if (!tp->t_rttlow || tp->t_rttlow > t) 2103 tp->t_rttlow = t; 2104 tcp_xmit_timer(tp, 2105 TCP_TS_TO_TICKS(t) + 1); 2106 } else if (tp->t_rtttime && 2107 SEQ_GT(th->th_ack, tp->t_rtseq)) { 2108 if (!tp->t_rttlow || 2109 tp->t_rttlow > ticks - tp->t_rtttime) 2110 tp->t_rttlow = ticks - tp->t_rtttime; 2111 tcp_xmit_timer(tp, 2112 ticks - tp->t_rtttime); 2113 } 2114 if (winup_only == 0) { 2115 acked = BYTES_THIS_ACK(tp, th); 2116 2117 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 2118 hhook_run_tcp_est_in(tp, th, to); 2119 2120 TCPSTAT_ADD(tcps_rcvackbyte, acked); 2121 sbdrop(&so->so_snd, acked); 2122 if (SEQ_GT(tp->snd_una, tp->snd_recover) && 2123 SEQ_LEQ(th->th_ack, tp->snd_recover)) 2124 tp->snd_recover = th->th_ack - 1; 2125 2126 /* 2127 * Let the congestion control algorithm update 2128 * congestion control related information. This 2129 * typically means increasing the congestion 2130 * window. 2131 */ 2132 cc_ack_received(tp, th, CC_ACK); 2133 2134 tp->snd_una = th->th_ack; 2135 tp->t_dupacks = 0; 2136 m_freem(m); 2137 2138 /* 2139 * If all outstanding data are acked, stop 2140 * retransmit timer, otherwise restart timer 2141 * using current (possibly backed-off) value. 2142 * If process is waiting for space, 2143 * wakeup/selwakeup/signal. If data 2144 * are ready to send, let tcp_output 2145 * decide between more output or persist. 2146 */ 2147#ifdef TCPDEBUG 2148 if (so->so_options & SO_DEBUG) 2149 tcp_trace(TA_INPUT, ostate, tp, 2150 (void *)tcp_saveipgen, 2151 &tcp_savetcp, 0); 2152#endif 2153 if (tp->snd_una == tp->snd_max) 2154 tcp_timer_activate(tp, TT_REXMT, 0); 2155 else if (!tcp_timer_active(tp, TT_PERSIST)) 2156 tcp_timer_activate(tp, TT_REXMT, 2157 tp->t_rxtcur); 2158 /* Wake up the socket if we have room to write more */ 2159 sowwakeup(so); 2160 } else { 2161 /* 2162 * Window update only, just free the mbufs and 2163 * send out whatever we can. 2164 */ 2165 m_freem(m); 2166 } 2167 if (sbavail(&so->so_snd)) 2168 (void) tcp_output(tp); 2169 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 2170 __func__, ti_locked)); 2171 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2172 INP_WLOCK_ASSERT(tp->t_inpcb); 2173 2174 if (tp->t_flags & TF_DELACK) { 2175 tp->t_flags &= ~TF_DELACK; 2176 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 2177 } 2178 INP_WUNLOCK(tp->t_inpcb); 2179 return (1); 2180} 2181 2182/* 2183 * This tcp-do-segment concentrates on making the fastest 2184 * ack processing path. It does not have a fast-path for 2185 * data (it possibly could which would then eliminate the 2186 * need for fast-slow above). For a content distributor having 2187 * large outgoing elephants and very very little coming in 2188 * having no fastpath for data does not really help (since you 2189 * don't get much data in). The most important thing is 2190 * processing ack's quickly and getting the rest of the data 2191 * output to the peer as quickly as possible. This routine 2192 * seems to be about an overall 3% faster then the old 2193 * tcp_do_segment and keeps us in the fast-path for packets 2194 * much more (by allowing window updates to also stay in the fastpath). 2195 */ 2196void 2197tcp_do_segment_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 2198 struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 2199 int ti_locked) 2200{ 2201 int thflags; 2202 u_long tiwin; 2203 char *s; 2204 struct in_conninfo *inc; 2205 struct tcpopt to; 2206 2207 thflags = th->th_flags; 2208 tp->sackhint.last_sack_ack = 0; 2209 inc = &tp->t_inpcb->inp_inc; 2210 /* 2211 * If this is either a state-changing packet or current state isn't 2212 * established, we require a write lock on tcbinfo. Otherwise, we 2213 * allow the tcbinfo to be in either alocked or unlocked, as the 2214 * caller may have unnecessarily acquired a write lock due to a race. 2215 */ 2216 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 2217 tp->t_state != TCPS_ESTABLISHED) { 2218 KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for " 2219 "SYN/FIN/RST/!EST", __func__, ti_locked)); 2220 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2221 } else { 2222#ifdef INVARIANTS 2223 if (ti_locked == TI_RLOCKED) { 2224 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2225 } else { 2226 KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 2227 "ti_locked: %d", __func__, ti_locked)); 2228 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2229 } 2230#endif 2231 } 2232 INP_WLOCK_ASSERT(tp->t_inpcb); 2233 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 2234 __func__)); 2235 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 2236 __func__)); 2237 2238 /* 2239 * Segment received on connection. 2240 * Reset idle time and keep-alive timer. 2241 * XXX: This should be done after segment 2242 * validation to ignore broken/spoofed segs. 2243 */ 2244 tp->t_rcvtime = ticks; 2245 if (TCPS_HAVEESTABLISHED(tp->t_state)) 2246 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 2247 2248 /* 2249 * Unscale the window into a 32-bit value. 2250 * For the SYN_SENT state the scale is zero. 2251 */ 2252 tiwin = th->th_win << tp->snd_scale; 2253 2254 /* 2255 * TCP ECN processing. 2256 */ 2257 if (tp->t_flags & TF_ECN_PERMIT) { 2258 if (thflags & TH_CWR) 2259 tp->t_flags &= ~TF_ECN_SND_ECE; 2260 switch (iptos & IPTOS_ECN_MASK) { 2261 case IPTOS_ECN_CE: 2262 tp->t_flags |= TF_ECN_SND_ECE; 2263 TCPSTAT_INC(tcps_ecn_ce); 2264 break; 2265 case IPTOS_ECN_ECT0: 2266 TCPSTAT_INC(tcps_ecn_ect0); 2267 break; 2268 case IPTOS_ECN_ECT1: 2269 TCPSTAT_INC(tcps_ecn_ect1); 2270 break; 2271 } 2272 /* Congestion experienced. */ 2273 if (thflags & TH_ECE) { 2274 cc_cong_signal(tp, th, CC_ECN); 2275 } 2276 } 2277 2278 /* 2279 * Parse options on any incoming segment. 2280 */ 2281 tcp_dooptions(&to, (u_char *)(th + 1), 2282 (th->th_off << 2) - sizeof(struct tcphdr), 2283 (thflags & TH_SYN) ? TO_SYN : 0); 2284 2285 /* 2286 * If echoed timestamp is later than the current time, 2287 * fall back to non RFC1323 RTT calculation. Normalize 2288 * timestamp if syncookies were used when this connection 2289 * was established. 2290 */ 2291 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 2292 to.to_tsecr -= tp->ts_offset; 2293 if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 2294 to.to_tsecr = 0; 2295 } 2296 /* 2297 * If timestamps were negotiated during SYN/ACK they should 2298 * appear on every segment during this session and vice versa. 2299 */ 2300 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 2301 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 2302 log(LOG_DEBUG, "%s; %s: Timestamp missing, " 2303 "no action\n", s, __func__); 2304 free(s, M_TCPLOG); 2305 } 2306 } 2307 if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 2308 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 2309 log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 2310 "no action\n", s, __func__); 2311 free(s, M_TCPLOG); 2312 } 2313 } 2314 2315 /* 2316 * Process options only when we get SYN/ACK back. The SYN case 2317 * for incoming connections is handled in tcp_syncache. 2318 * According to RFC1323 the window field in a SYN (i.e., a <SYN> 2319 * or <SYN,ACK>) segment itself is never scaled. 2320 * XXX this is traditional behavior, may need to be cleaned up. 2321 */ 2322 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 2323 if ((to.to_flags & TOF_SCALE) && 2324 (tp->t_flags & TF_REQ_SCALE)) { 2325 tp->t_flags |= TF_RCVD_SCALE; 2326 tp->snd_scale = to.to_wscale; 2327 } 2328 /* 2329 * Initial send window. It will be updated with 2330 * the next incoming segment to the scaled value. 2331 */ 2332 tp->snd_wnd = th->th_win; 2333 if (to.to_flags & TOF_TS) { 2334 tp->t_flags |= TF_RCVD_TSTMP; 2335 tp->ts_recent = to.to_tsval; 2336 tp->ts_recent_age = tcp_ts_getticks(); 2337 } 2338 if (to.to_flags & TOF_MSS) 2339 tcp_mss(tp, to.to_mss); 2340 if ((tp->t_flags & TF_SACK_PERMIT) && 2341 (to.to_flags & TOF_SACKPERM) == 0) 2342 tp->t_flags &= ~TF_SACK_PERMIT; 2343 } 2344 /* 2345 * Header prediction: check for the two common cases 2346 * of a uni-directional data xfer. If the packet has 2347 * no control flags, is in-sequence, the window didn't 2348 * change and we're not retransmitting, it's a 2349 * candidate. If the length is zero and the ack moved 2350 * forward, we're the sender side of the xfer. Just 2351 * free the data acked & wake any higher level process 2352 * that was blocked waiting for space. If the length 2353 * is non-zero and the ack didn't move, we're the 2354 * receiver side. If we're getting packets in-order 2355 * (the reassembly queue is empty), add the data to 2356 * the socket buffer and note that we need a delayed ack. 2357 * Make sure that the hidden state-flags are also off. 2358 * Since we check for TCPS_ESTABLISHED first, it can only 2359 * be TH_NEEDSYN. 2360 */ 2361 if (__predict_true(tp->t_state == TCPS_ESTABLISHED) && 2362 __predict_true(((to.to_flags & TOF_SACK) == 0)) && 2363 __predict_true(tlen == 0) && 2364 __predict_true((thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK) && 2365 __predict_true(LIST_EMPTY(&tp->t_segq)) && 2366 __predict_true(th->th_seq == tp->rcv_nxt)) { 2367 if (tcp_fastack(m, th, so, tp, &to, drop_hdrlen, tlen, 2368 ti_locked, tiwin)) { 2369 return; 2370 } 2371 } 2372 tcp_do_slowpath(m, th, so, tp, &to, drop_hdrlen, tlen, 2373 ti_locked, tiwin, thflags); 2374} 2375 2376struct tcp_function_block __tcp_fastslow = { 2377 "fastslow", 2378 tcp_output, 2379 tcp_do_segment_fastslow, 2380 tcp_default_ctloutput, 2381 NULL, 2382 NULL, 2383 NULL, 2384 NULL, 2385 NULL, 2386 NULL, 2387 NULL, 2388 0, 2389 0 2390 2391}; 2392 2393struct tcp_function_block __tcp_fastack = { 2394 "fastack", 2395 tcp_output, 2396 tcp_do_segment_fastack, 2397 tcp_default_ctloutput, 2398 NULL, 2399 NULL, 2400 NULL, 2401 NULL, 2402 NULL, 2403 NULL, 2404 NULL, 2405 0, 2406 0 2407}; 2408 2409static int 2410tcp_addfastpaths(module_t mod, int type, void *data) 2411{ 2412 int err=0; 2413 2414 switch (type) { 2415 case MOD_LOAD: 2416 err = register_tcp_functions(&__tcp_fastack, M_WAITOK); 2417 if (err) { 2418 printf("Failed to register fastack module -- err:%d\n", err); 2419 return(err); 2420 } 2421 err = register_tcp_functions(&__tcp_fastslow, M_WAITOK); 2422 if (err) { 2423 printf("Failed to register fastslow module -- err:%d\n", err); 2424 deregister_tcp_functions(&__tcp_fastack); 2425 return(err); 2426 } 2427 break; 2428 case MOD_QUIESCE: 2429 if ((__tcp_fastslow.tfb_refcnt) ||( __tcp_fastack.tfb_refcnt)) { 2430 return(EBUSY); 2431 } 2432 break; 2433 case MOD_UNLOAD: 2434 err = deregister_tcp_functions(&__tcp_fastack); 2435 if (err == EBUSY) 2436 break; 2437 err = deregister_tcp_functions(&__tcp_fastslow); 2438 if (err == EBUSY) 2439 break; 2440 err = 0; 2441 break; 2442 default: 2443 return (EOPNOTSUPP); 2444 } 2445 return (err); 2446} 2447 2448static moduledata_t new_tcp_fastpaths = { 2449 .name = "tcp_fastpaths", 2450 .evhand = tcp_addfastpaths, 2451 .priv = 0 2452}; 2453 2454MODULE_VERSION(kern_tcpfastpaths, 1); 2455DECLARE_MODULE(kern_tcpfastpaths, new_tcp_fastpaths, SI_SUB_PSEUDO, SI_ORDER_ANY);
| 111#ifdef TCPDEBUG 112#include <netinet/tcp_debug.h> 113#endif /* TCPDEBUG */ 114#ifdef TCP_OFFLOAD 115#include <netinet/tcp_offload.h> 116#endif 117 118#ifdef IPSEC 119#include <netipsec/ipsec.h> 120#include <netipsec/ipsec6.h> 121#endif /*IPSEC*/ 122 123#include <machine/in_cksum.h> 124 125#include <security/mac/mac_framework.h> 126 127const int tcprexmtthresh; 128 129VNET_DECLARE(int, tcp_autorcvbuf_inc); 130#define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 131VNET_DECLARE(int, tcp_autorcvbuf_max); 132#define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 133VNET_DECLARE(int, tcp_do_rfc3042); 134#define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 135VNET_DECLARE(int, tcp_do_autorcvbuf); 136#define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 137VNET_DECLARE(int, tcp_insecure_rst); 138#define V_tcp_insecure_rst VNET(tcp_insecure_rst) 139VNET_DECLARE(int, tcp_insecure_syn); 140#define V_tcp_insecure_syn VNET(tcp_insecure_syn) 141 142static void tcp_do_segment_fastslow(struct mbuf *, struct tcphdr *, 143 struct socket *, struct tcpcb *, int, int, uint8_t, 144 int); 145 146static void tcp_do_segment_fastack(struct mbuf *, struct tcphdr *, 147 struct socket *, struct tcpcb *, int, int, uint8_t, 148 int); 149 150/* 151 * Indicate whether this ack should be delayed. We can delay the ack if 152 * following conditions are met: 153 * - There is no delayed ack timer in progress. 154 * - Our last ack wasn't a 0-sized window. We never want to delay 155 * the ack that opens up a 0-sized window. 156 * - LRO wasn't used for this segment. We make sure by checking that the 157 * segment size is not larger than the MSS. 158 */ 159#define DELAY_ACK(tp, tlen) \ 160 ((!tcp_timer_active(tp, TT_DELACK) && \ 161 (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 162 (tlen <= tp->t_maxseg) && \ 163 (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 164 165/* 166 * So how is this faster than the normal fast ack? 167 * It basically allows us to also stay in the fastpath 168 * when a window-update ack also arrives. In testing 169 * we saw only 25-30% of connections doing fastpath 170 * due to the fact that along with moving forward 171 * in sequence the window was also updated. 172 */ 173static void 174tcp_do_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 175 struct tcpcb *tp, struct tcpopt *to, int drop_hdrlen, int tlen, 176 int ti_locked, u_long tiwin) 177{ 178 int acked; 179 int winup_only=0; 180#ifdef TCPDEBUG 181 /* 182 * The size of tcp_saveipgen must be the size of the max ip header, 183 * now IPv6. 184 */ 185 u_char tcp_saveipgen[IP6_HDR_LEN]; 186 struct tcphdr tcp_savetcp; 187 short ostate = 0; 188#endif 189 /* 190 * The following if statment will be true if 191 * we are doing the win_up_in_fp <and> 192 * - We have more new data (SEQ_LT(tp->snd_wl1, th->th_seq)) <or> 193 * - No more new data, but we have an ack for new data 194 * (tp->snd_wl1 == th->th_seq && SEQ_LT(tp->snd_wl2, th->th_ack)) 195 * - No more new data, the same ack point but the window grew 196 * (tp->snd_wl1 == th->th_seq && tp->snd_wl2 == th->th_ack && twin > tp->snd_wnd) 197 */ 198 if ((SEQ_LT(tp->snd_wl1, th->th_seq) || 199 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 200 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 201 /* keep track of pure window updates */ 202 if (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) { 203 winup_only = 1; 204 TCPSTAT_INC(tcps_rcvwinupd); 205 } 206 tp->snd_wnd = tiwin; 207 tp->snd_wl1 = th->th_seq; 208 tp->snd_wl2 = th->th_ack; 209 if (tp->snd_wnd > tp->max_sndwnd) 210 tp->max_sndwnd = tp->snd_wnd; 211 } 212 /* 213 * If last ACK falls within this segment's sequence numbers, 214 * record the timestamp. 215 * NOTE that the test is modified according to the latest 216 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 217 */ 218 if ((to->to_flags & TOF_TS) != 0 && 219 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 220 tp->ts_recent_age = tcp_ts_getticks(); 221 tp->ts_recent = to->to_tsval; 222 } 223 /* 224 * This is a pure ack for outstanding data. 225 */ 226 if (ti_locked == TI_RLOCKED) { 227 INP_INFO_RUNLOCK(&V_tcbinfo); 228 } 229 ti_locked = TI_UNLOCKED; 230 231 TCPSTAT_INC(tcps_predack); 232 233 /* 234 * "bad retransmit" recovery. 235 */ 236 if (tp->t_rxtshift == 1 && 237 tp->t_flags & TF_PREVVALID && 238 (int)(ticks - tp->t_badrxtwin) < 0) { 239 cc_cong_signal(tp, th, CC_RTO_ERR); 240 } 241 242 /* 243 * Recalculate the transmit timer / rtt. 244 * 245 * Some boxes send broken timestamp replies 246 * during the SYN+ACK phase, ignore 247 * timestamps of 0 or we could calculate a 248 * huge RTT and blow up the retransmit timer. 249 */ 250 if ((to->to_flags & TOF_TS) != 0 && 251 to->to_tsecr) { 252 u_int t; 253 254 t = tcp_ts_getticks() - to->to_tsecr; 255 if (!tp->t_rttlow || tp->t_rttlow > t) 256 tp->t_rttlow = t; 257 tcp_xmit_timer(tp, 258 TCP_TS_TO_TICKS(t) + 1); 259 } else if (tp->t_rtttime && 260 SEQ_GT(th->th_ack, tp->t_rtseq)) { 261 if (!tp->t_rttlow || 262 tp->t_rttlow > ticks - tp->t_rtttime) 263 tp->t_rttlow = ticks - tp->t_rtttime; 264 tcp_xmit_timer(tp, 265 ticks - tp->t_rtttime); 266 } 267 if (winup_only == 0) { 268 acked = BYTES_THIS_ACK(tp, th); 269 270 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 271 hhook_run_tcp_est_in(tp, th, to); 272 273 TCPSTAT_ADD(tcps_rcvackbyte, acked); 274 sbdrop(&so->so_snd, acked); 275 if (SEQ_GT(tp->snd_una, tp->snd_recover) && 276 SEQ_LEQ(th->th_ack, tp->snd_recover)) 277 tp->snd_recover = th->th_ack - 1; 278 279 /* 280 * Let the congestion control algorithm update 281 * congestion control related information. This 282 * typically means increasing the congestion 283 * window. 284 */ 285 cc_ack_received(tp, th, CC_ACK); 286 287 tp->snd_una = th->th_ack; 288 /* 289 * Pull snd_wl2 up to prevent seq wrap relative 290 * to th_ack. 291 */ 292 tp->snd_wl2 = th->th_ack; 293 tp->t_dupacks = 0; 294 m_freem(m); 295 296 /* 297 * If all outstanding data are acked, stop 298 * retransmit timer, otherwise restart timer 299 * using current (possibly backed-off) value. 300 * If process is waiting for space, 301 * wakeup/selwakeup/signal. If data 302 * are ready to send, let tcp_output 303 * decide between more output or persist. 304 */ 305#ifdef TCPDEBUG 306 if (so->so_options & SO_DEBUG) 307 tcp_trace(TA_INPUT, ostate, tp, 308 (void *)tcp_saveipgen, 309 &tcp_savetcp, 0); 310#endif 311 if (tp->snd_una == tp->snd_max) 312 tcp_timer_activate(tp, TT_REXMT, 0); 313 else if (!tcp_timer_active(tp, TT_PERSIST)) 314 tcp_timer_activate(tp, TT_REXMT, 315 tp->t_rxtcur); 316 } else { 317 /* 318 * Window update only, just free the mbufs and 319 * send out whatever we can. 320 */ 321 m_freem(m); 322 } 323 sowwakeup(so); 324 if (sbavail(&so->so_snd)) 325 (void) tcp_output(tp); 326 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 327 __func__, ti_locked)); 328 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 329 INP_WLOCK_ASSERT(tp->t_inpcb); 330 331 if (tp->t_flags & TF_DELACK) { 332 tp->t_flags &= ~TF_DELACK; 333 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 334 } 335 INP_WUNLOCK(tp->t_inpcb); 336} 337 338/* 339 * Here nothing is really faster, its just that we 340 * have broken out the fast-data path also just like 341 * the fast-ack. 342 */ 343static void 344tcp_do_fastnewdata(struct mbuf *m, struct tcphdr *th, struct socket *so, 345 struct tcpcb *tp, struct tcpopt *to, int drop_hdrlen, int tlen, 346 int ti_locked, u_long tiwin) 347{ 348 int newsize = 0; /* automatic sockbuf scaling */ 349#ifdef TCPDEBUG 350 /* 351 * The size of tcp_saveipgen must be the size of the max ip header, 352 * now IPv6. 353 */ 354 u_char tcp_saveipgen[IP6_HDR_LEN]; 355 struct tcphdr tcp_savetcp; 356 short ostate = 0; 357#endif 358 /* 359 * If last ACK falls within this segment's sequence numbers, 360 * record the timestamp. 361 * NOTE that the test is modified according to the latest 362 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 363 */ 364 if ((to->to_flags & TOF_TS) != 0 && 365 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 366 tp->ts_recent_age = tcp_ts_getticks(); 367 tp->ts_recent = to->to_tsval; 368 } 369 370 /* 371 * This is a pure, in-sequence data packet with 372 * nothing on the reassembly queue and we have enough 373 * buffer space to take it. 374 */ 375 if (ti_locked == TI_RLOCKED) { 376 INP_INFO_RUNLOCK(&V_tcbinfo); 377 } 378 ti_locked = TI_UNLOCKED; 379 380 /* Clean receiver SACK report if present */ 381 if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 382 tcp_clean_sackreport(tp); 383 TCPSTAT_INC(tcps_preddat); 384 tp->rcv_nxt += tlen; 385 /* 386 * Pull snd_wl1 up to prevent seq wrap relative to 387 * th_seq. 388 */ 389 tp->snd_wl1 = th->th_seq; 390 /* 391 * Pull rcv_up up to prevent seq wrap relative to 392 * rcv_nxt. 393 */ 394 tp->rcv_up = tp->rcv_nxt; 395 TCPSTAT_ADD(tcps_rcvbyte, tlen); 396#ifdef TCPDEBUG 397 if (so->so_options & SO_DEBUG) 398 tcp_trace(TA_INPUT, ostate, tp, 399 (void *)tcp_saveipgen, &tcp_savetcp, 0); 400#endif 401 /* 402 * Automatic sizing of receive socket buffer. Often the send 403 * buffer size is not optimally adjusted to the actual network 404 * conditions at hand (delay bandwidth product). Setting the 405 * buffer size too small limits throughput on links with high 406 * bandwidth and high delay (eg. trans-continental/oceanic links). 407 * 408 * On the receive side the socket buffer memory is only rarely 409 * used to any significant extent. This allows us to be much 410 * more aggressive in scaling the receive socket buffer. For 411 * the case that the buffer space is actually used to a large 412 * extent and we run out of kernel memory we can simply drop 413 * the new segments; TCP on the sender will just retransmit it 414 * later. Setting the buffer size too big may only consume too 415 * much kernel memory if the application doesn't read() from 416 * the socket or packet loss or reordering makes use of the 417 * reassembly queue. 418 * 419 * The criteria to step up the receive buffer one notch are: 420 * 1. Application has not set receive buffer size with 421 * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 422 * 2. the number of bytes received during the time it takes 423 * one timestamp to be reflected back to us (the RTT); 424 * 3. received bytes per RTT is within seven eighth of the 425 * current socket buffer size; 426 * 4. receive buffer size has not hit maximal automatic size; 427 * 428 * This algorithm does one step per RTT at most and only if 429 * we receive a bulk stream w/o packet losses or reorderings. 430 * Shrinking the buffer during idle times is not necessary as 431 * it doesn't consume any memory when idle. 432 * 433 * TODO: Only step up if the application is actually serving 434 * the buffer to better manage the socket buffer resources. 435 */ 436 if (V_tcp_do_autorcvbuf && 437 (to->to_flags & TOF_TS) && 438 to->to_tsecr && 439 (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 440 if (TSTMP_GT(to->to_tsecr, tp->rfbuf_ts) && 441 to->to_tsecr - tp->rfbuf_ts < hz) { 442 if (tp->rfbuf_cnt > 443 (so->so_rcv.sb_hiwat / 8 * 7) && 444 so->so_rcv.sb_hiwat < 445 V_tcp_autorcvbuf_max) { 446 newsize = 447 min(so->so_rcv.sb_hiwat + 448 V_tcp_autorcvbuf_inc, 449 V_tcp_autorcvbuf_max); 450 } 451 /* Start over with next RTT. */ 452 tp->rfbuf_ts = 0; 453 tp->rfbuf_cnt = 0; 454 } else 455 tp->rfbuf_cnt += tlen; /* add up */ 456 } 457 458 /* Add data to socket buffer. */ 459 SOCKBUF_LOCK(&so->so_rcv); 460 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 461 m_freem(m); 462 } else { 463 /* 464 * Set new socket buffer size. 465 * Give up when limit is reached. 466 */ 467 if (newsize) 468 if (!sbreserve_locked(&so->so_rcv, 469 newsize, so, NULL)) 470 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 471 m_adj(m, drop_hdrlen); /* delayed header drop */ 472 sbappendstream_locked(&so->so_rcv, m, 0); 473 } 474 /* NB: sorwakeup_locked() does an implicit unlock. */ 475 sorwakeup_locked(so); 476 if (DELAY_ACK(tp, tlen)) { 477 tp->t_flags |= TF_DELACK; 478 } else { 479 tp->t_flags |= TF_ACKNOW; 480 tcp_output(tp); 481 } 482 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 483 __func__, ti_locked)); 484 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 485 INP_WLOCK_ASSERT(tp->t_inpcb); 486 487 if (tp->t_flags & TF_DELACK) { 488 tp->t_flags &= ~TF_DELACK; 489 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 490 } 491 INP_WUNLOCK(tp->t_inpcb); 492} 493 494/* 495 * The slow-path is the clone of the long long part 496 * of tcp_do_segment past all the fast-path stuff. We 497 * use it here by two different callers, the fast/slow and 498 * the fastack only. 499 */ 500static void 501tcp_do_slowpath(struct mbuf *m, struct tcphdr *th, struct socket *so, 502 struct tcpcb *tp, struct tcpopt *to, int drop_hdrlen, int tlen, 503 int ti_locked, u_long tiwin, int thflags) 504{ 505 int acked, ourfinisacked, needoutput = 0; 506 int rstreason, todrop, win; 507 char *s; 508 struct in_conninfo *inc; 509 struct mbuf *mfree = NULL; 510#ifdef TCPDEBUG 511 /* 512 * The size of tcp_saveipgen must be the size of the max ip header, 513 * now IPv6. 514 */ 515 u_char tcp_saveipgen[IP6_HDR_LEN]; 516 struct tcphdr tcp_savetcp; 517 short ostate = 0; 518#endif 519 /* 520 * Calculate amount of space in receive window, 521 * and then do TCP input processing. 522 * Receive window is amount of space in rcv queue, 523 * but not less than advertised window. 524 */ 525 inc = &tp->t_inpcb->inp_inc; 526 win = sbspace(&so->so_rcv); 527 if (win < 0) 528 win = 0; 529 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 530 531 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 532 tp->rfbuf_ts = 0; 533 tp->rfbuf_cnt = 0; 534 535 switch (tp->t_state) { 536 537 /* 538 * If the state is SYN_RECEIVED: 539 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 540 */ 541 case TCPS_SYN_RECEIVED: 542 if ((thflags & TH_ACK) && 543 (SEQ_LEQ(th->th_ack, tp->snd_una) || 544 SEQ_GT(th->th_ack, tp->snd_max))) { 545 rstreason = BANDLIM_RST_OPENPORT; 546 goto dropwithreset; 547 } 548 break; 549 550 /* 551 * If the state is SYN_SENT: 552 * if seg contains an ACK, but not for our SYN, drop the input. 553 * if seg contains a RST, then drop the connection. 554 * if seg does not contain SYN, then drop it. 555 * Otherwise this is an acceptable SYN segment 556 * initialize tp->rcv_nxt and tp->irs 557 * if seg contains ack then advance tp->snd_una 558 * if seg contains an ECE and ECN support is enabled, the stream 559 * is ECN capable. 560 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 561 * arrange for segment to be acked (eventually) 562 * continue processing rest of data/controls, beginning with URG 563 */ 564 case TCPS_SYN_SENT: 565 if ((thflags & TH_ACK) && 566 (SEQ_LEQ(th->th_ack, tp->iss) || 567 SEQ_GT(th->th_ack, tp->snd_max))) { 568 rstreason = BANDLIM_UNLIMITED; 569 goto dropwithreset; 570 } 571 if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 572 TCP_PROBE5(connect__refused, NULL, tp, 573 mtod(m, const char *), tp, th); 574 tp = tcp_drop(tp, ECONNREFUSED); 575 } 576 if (thflags & TH_RST) 577 goto drop; 578 if (!(thflags & TH_SYN)) 579 goto drop; 580 581 tp->irs = th->th_seq; 582 tcp_rcvseqinit(tp); 583 if (thflags & TH_ACK) { 584 TCPSTAT_INC(tcps_connects); 585 soisconnected(so); 586#ifdef MAC 587 mac_socketpeer_set_from_mbuf(m, so); 588#endif 589 /* Do window scaling on this connection? */ 590 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 591 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 592 tp->rcv_scale = tp->request_r_scale; 593 } 594 tp->rcv_adv += imin(tp->rcv_wnd, 595 TCP_MAXWIN << tp->rcv_scale); 596 tp->snd_una++; /* SYN is acked */ 597 /* 598 * If there's data, delay ACK; if there's also a FIN 599 * ACKNOW will be turned on later. 600 */ 601 if (DELAY_ACK(tp, tlen) && tlen != 0) 602 tcp_timer_activate(tp, TT_DELACK, 603 tcp_delacktime); 604 else 605 tp->t_flags |= TF_ACKNOW; 606 607 if ((thflags & TH_ECE) && V_tcp_do_ecn) { 608 tp->t_flags |= TF_ECN_PERMIT; 609 TCPSTAT_INC(tcps_ecn_shs); 610 } 611 612 /* 613 * Received <SYN,ACK> in SYN_SENT[*] state. 614 * Transitions: 615 * SYN_SENT --> ESTABLISHED 616 * SYN_SENT* --> FIN_WAIT_1 617 */ 618 tp->t_starttime = ticks; 619 if (tp->t_flags & TF_NEEDFIN) { 620 tcp_state_change(tp, TCPS_FIN_WAIT_1); 621 tp->t_flags &= ~TF_NEEDFIN; 622 thflags &= ~TH_SYN; 623 } else { 624 tcp_state_change(tp, TCPS_ESTABLISHED); 625 TCP_PROBE5(connect__established, NULL, tp, 626 mtod(m, const char *), tp, th); 627 cc_conn_init(tp); 628 tcp_timer_activate(tp, TT_KEEP, 629 TP_KEEPIDLE(tp)); 630 } 631 } else { 632 /* 633 * Received initial SYN in SYN-SENT[*] state => 634 * simultaneous open. 635 * If it succeeds, connection is * half-synchronized. 636 * Otherwise, do 3-way handshake: 637 * SYN-SENT -> SYN-RECEIVED 638 * SYN-SENT* -> SYN-RECEIVED* 639 */ 640 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 641 tcp_timer_activate(tp, TT_REXMT, 0); 642 tcp_state_change(tp, TCPS_SYN_RECEIVED); 643 } 644 645 KASSERT(ti_locked == TI_RLOCKED, ("%s: trimthenstep6: " 646 "ti_locked %d", __func__, ti_locked)); 647 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 648 INP_WLOCK_ASSERT(tp->t_inpcb); 649 650 /* 651 * Advance th->th_seq to correspond to first data byte. 652 * If data, trim to stay within window, 653 * dropping FIN if necessary. 654 */ 655 th->th_seq++; 656 if (tlen > tp->rcv_wnd) { 657 todrop = tlen - tp->rcv_wnd; 658 m_adj(m, -todrop); 659 tlen = tp->rcv_wnd; 660 thflags &= ~TH_FIN; 661 TCPSTAT_INC(tcps_rcvpackafterwin); 662 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 663 } 664 tp->snd_wl1 = th->th_seq - 1; 665 tp->rcv_up = th->th_seq; 666 /* 667 * Client side of transaction: already sent SYN and data. 668 * If the remote host used T/TCP to validate the SYN, 669 * our data will be ACK'd; if so, enter normal data segment 670 * processing in the middle of step 5, ack processing. 671 * Otherwise, goto step 6. 672 */ 673 if (thflags & TH_ACK) 674 goto process_ACK; 675 676 goto step6; 677 678 /* 679 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 680 * do normal processing. 681 * 682 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 683 */ 684 case TCPS_LAST_ACK: 685 case TCPS_CLOSING: 686 break; /* continue normal processing */ 687 } 688 689 /* 690 * States other than LISTEN or SYN_SENT. 691 * First check the RST flag and sequence number since reset segments 692 * are exempt from the timestamp and connection count tests. This 693 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 694 * below which allowed reset segments in half the sequence space 695 * to fall though and be processed (which gives forged reset 696 * segments with a random sequence number a 50 percent chance of 697 * killing a connection). 698 * Then check timestamp, if present. 699 * Then check the connection count, if present. 700 * Then check that at least some bytes of segment are within 701 * receive window. If segment begins before rcv_nxt, 702 * drop leading data (and SYN); if nothing left, just ack. 703 */ 704 if (thflags & TH_RST) { 705 /* 706 * RFC5961 Section 3.2 707 * 708 * - RST drops connection only if SEG.SEQ == RCV.NXT. 709 * - If RST is in window, we send challenge ACK. 710 * 711 * Note: to take into account delayed ACKs, we should 712 * test against last_ack_sent instead of rcv_nxt. 713 * Note 2: we handle special case of closed window, not 714 * covered by the RFC. 715 */ 716 if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 717 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 718 (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 719 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 720 KASSERT(ti_locked == TI_RLOCKED, 721 ("%s: TH_RST ti_locked %d, th %p tp %p", 722 __func__, ti_locked, th, tp)); 723 KASSERT(tp->t_state != TCPS_SYN_SENT, 724 ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 725 __func__, th, tp)); 726 727 if (V_tcp_insecure_rst || 728 tp->last_ack_sent == th->th_seq) { 729 TCPSTAT_INC(tcps_drops); 730 /* Drop the connection. */ 731 switch (tp->t_state) { 732 case TCPS_SYN_RECEIVED: 733 so->so_error = ECONNREFUSED; 734 goto close; 735 case TCPS_ESTABLISHED: 736 case TCPS_FIN_WAIT_1: 737 case TCPS_FIN_WAIT_2: 738 case TCPS_CLOSE_WAIT: 739 so->so_error = ECONNRESET; 740 close: 741 tcp_state_change(tp, TCPS_CLOSED); 742 /* FALLTHROUGH */ 743 default: 744 tp = tcp_close(tp); 745 } 746 } else { 747 TCPSTAT_INC(tcps_badrst); 748 /* Send challenge ACK. */ 749 tcp_respond(tp, mtod(m, void *), th, m, 750 tp->rcv_nxt, tp->snd_nxt, TH_ACK); 751 tp->last_ack_sent = tp->rcv_nxt; 752 m = NULL; 753 } 754 } 755 goto drop; 756 } 757 758 /* 759 * RFC5961 Section 4.2 760 * Send challenge ACK for any SYN in synchronized state. 761 */ 762 if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT) { 763 KASSERT(ti_locked == TI_RLOCKED, 764 ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 765 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 766 767 TCPSTAT_INC(tcps_badsyn); 768 if (V_tcp_insecure_syn && 769 SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 770 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 771 tp = tcp_drop(tp, ECONNRESET); 772 rstreason = BANDLIM_UNLIMITED; 773 } else { 774 /* Send challenge ACK. */ 775 tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 776 tp->snd_nxt, TH_ACK); 777 tp->last_ack_sent = tp->rcv_nxt; 778 m = NULL; 779 } 780 goto drop; 781 } 782 783 /* 784 * RFC 1323 PAWS: If we have a timestamp reply on this segment 785 * and it's less than ts_recent, drop it. 786 */ 787 if ((to->to_flags & TOF_TS) != 0 && tp->ts_recent && 788 TSTMP_LT(to->to_tsval, tp->ts_recent)) { 789 790 /* Check to see if ts_recent is over 24 days old. */ 791 if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 792 /* 793 * Invalidate ts_recent. If this segment updates 794 * ts_recent, the age will be reset later and ts_recent 795 * will get a valid value. If it does not, setting 796 * ts_recent to zero will at least satisfy the 797 * requirement that zero be placed in the timestamp 798 * echo reply when ts_recent isn't valid. The 799 * age isn't reset until we get a valid ts_recent 800 * because we don't want out-of-order segments to be 801 * dropped when ts_recent is old. 802 */ 803 tp->ts_recent = 0; 804 } else { 805 TCPSTAT_INC(tcps_rcvduppack); 806 TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 807 TCPSTAT_INC(tcps_pawsdrop); 808 if (tlen) 809 goto dropafterack; 810 goto drop; 811 } 812 } 813 814 /* 815 * In the SYN-RECEIVED state, validate that the packet belongs to 816 * this connection before trimming the data to fit the receive 817 * window. Check the sequence number versus IRS since we know 818 * the sequence numbers haven't wrapped. This is a partial fix 819 * for the "LAND" DoS attack. 820 */ 821 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 822 rstreason = BANDLIM_RST_OPENPORT; 823 goto dropwithreset; 824 } 825 826 todrop = tp->rcv_nxt - th->th_seq; 827 if (todrop > 0) { 828 if (thflags & TH_SYN) { 829 thflags &= ~TH_SYN; 830 th->th_seq++; 831 if (th->th_urp > 1) 832 th->th_urp--; 833 else 834 thflags &= ~TH_URG; 835 todrop--; 836 } 837 /* 838 * Following if statement from Stevens, vol. 2, p. 960. 839 */ 840 if (todrop > tlen 841 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 842 /* 843 * Any valid FIN must be to the left of the window. 844 * At this point the FIN must be a duplicate or out 845 * of sequence; drop it. 846 */ 847 thflags &= ~TH_FIN; 848 849 /* 850 * Send an ACK to resynchronize and drop any data. 851 * But keep on processing for RST or ACK. 852 */ 853 tp->t_flags |= TF_ACKNOW; 854 todrop = tlen; 855 TCPSTAT_INC(tcps_rcvduppack); 856 TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 857 } else { 858 TCPSTAT_INC(tcps_rcvpartduppack); 859 TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 860 } 861 drop_hdrlen += todrop; /* drop from the top afterwards */ 862 th->th_seq += todrop; 863 tlen -= todrop; 864 if (th->th_urp > todrop) 865 th->th_urp -= todrop; 866 else { 867 thflags &= ~TH_URG; 868 th->th_urp = 0; 869 } 870 } 871 872 /* 873 * If new data are received on a connection after the 874 * user processes are gone, then RST the other end. 875 */ 876 if ((so->so_state & SS_NOFDREF) && 877 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 878 KASSERT(ti_locked == TI_RLOCKED, ("%s: SS_NOFDEREF && " 879 "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 880 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 881 882 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 883 log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 884 "after socket was closed, " 885 "sending RST and removing tcpcb\n", 886 s, __func__, tcpstates[tp->t_state], tlen); 887 free(s, M_TCPLOG); 888 } 889 tp = tcp_close(tp); 890 TCPSTAT_INC(tcps_rcvafterclose); 891 rstreason = BANDLIM_UNLIMITED; 892 goto dropwithreset; 893 } 894 895 /* 896 * If segment ends after window, drop trailing data 897 * (and PUSH and FIN); if nothing left, just ACK. 898 */ 899 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 900 if (todrop > 0) { 901 TCPSTAT_INC(tcps_rcvpackafterwin); 902 if (todrop >= tlen) { 903 TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 904 /* 905 * If window is closed can only take segments at 906 * window edge, and have to drop data and PUSH from 907 * incoming segments. Continue processing, but 908 * remember to ack. Otherwise, drop segment 909 * and ack. 910 */ 911 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 912 tp->t_flags |= TF_ACKNOW; 913 TCPSTAT_INC(tcps_rcvwinprobe); 914 } else 915 goto dropafterack; 916 } else 917 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 918 m_adj(m, -todrop); 919 tlen -= todrop; 920 thflags &= ~(TH_PUSH|TH_FIN); 921 } 922 923 /* 924 * If last ACK falls within this segment's sequence numbers, 925 * record its timestamp. 926 * NOTE: 927 * 1) That the test incorporates suggestions from the latest 928 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 929 * 2) That updating only on newer timestamps interferes with 930 * our earlier PAWS tests, so this check should be solely 931 * predicated on the sequence space of this segment. 932 * 3) That we modify the segment boundary check to be 933 * Last.ACK.Sent <= SEG.SEQ + SEG.Len 934 * instead of RFC1323's 935 * Last.ACK.Sent < SEG.SEQ + SEG.Len, 936 * This modified check allows us to overcome RFC1323's 937 * limitations as described in Stevens TCP/IP Illustrated 938 * Vol. 2 p.869. In such cases, we can still calculate the 939 * RTT correctly when RCV.NXT == Last.ACK.Sent. 940 */ 941 if ((to->to_flags & TOF_TS) != 0 && 942 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 943 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 944 ((thflags & (TH_SYN|TH_FIN)) != 0))) { 945 tp->ts_recent_age = tcp_ts_getticks(); 946 tp->ts_recent = to->to_tsval; 947 } 948 949 /* 950 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 951 * flag is on (half-synchronized state), then queue data for 952 * later processing; else drop segment and return. 953 */ 954 if ((thflags & TH_ACK) == 0) { 955 if (tp->t_state == TCPS_SYN_RECEIVED || 956 (tp->t_flags & TF_NEEDSYN)) 957 goto step6; 958 else if (tp->t_flags & TF_ACKNOW) 959 goto dropafterack; 960 else 961 goto drop; 962 } 963 964 /* 965 * Ack processing. 966 */ 967 switch (tp->t_state) { 968 969 /* 970 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 971 * ESTABLISHED state and continue processing. 972 * The ACK was checked above. 973 */ 974 case TCPS_SYN_RECEIVED: 975 976 TCPSTAT_INC(tcps_connects); 977 soisconnected(so); 978 /* Do window scaling? */ 979 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 980 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 981 tp->rcv_scale = tp->request_r_scale; 982 tp->snd_wnd = tiwin; 983 } 984 /* 985 * Make transitions: 986 * SYN-RECEIVED -> ESTABLISHED 987 * SYN-RECEIVED* -> FIN-WAIT-1 988 */ 989 tp->t_starttime = ticks; 990 if (tp->t_flags & TF_NEEDFIN) { 991 tcp_state_change(tp, TCPS_FIN_WAIT_1); 992 tp->t_flags &= ~TF_NEEDFIN; 993 } else { 994 tcp_state_change(tp, TCPS_ESTABLISHED); 995 TCP_PROBE5(accept__established, NULL, tp, 996 mtod(m, const char *), tp, th); 997 cc_conn_init(tp); 998 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 999 } 1000 /* 1001 * If segment contains data or ACK, will call tcp_reass() 1002 * later; if not, do so now to pass queued data to user. 1003 */ 1004 if (tlen == 0 && (thflags & TH_FIN) == 0) 1005 (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1006 (struct mbuf *)0); 1007 tp->snd_wl1 = th->th_seq - 1; 1008 /* FALLTHROUGH */ 1009 1010 /* 1011 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1012 * ACKs. If the ack is in the range 1013 * tp->snd_una < th->th_ack <= tp->snd_max 1014 * then advance tp->snd_una to th->th_ack and drop 1015 * data from the retransmission queue. If this ACK reflects 1016 * more up to date window information we update our window information. 1017 */ 1018 case TCPS_ESTABLISHED: 1019 case TCPS_FIN_WAIT_1: 1020 case TCPS_FIN_WAIT_2: 1021 case TCPS_CLOSE_WAIT: 1022 case TCPS_CLOSING: 1023 case TCPS_LAST_ACK: 1024 if (SEQ_GT(th->th_ack, tp->snd_max)) { 1025 TCPSTAT_INC(tcps_rcvacktoomuch); 1026 goto dropafterack; 1027 } 1028 if ((tp->t_flags & TF_SACK_PERMIT) && 1029 ((to->to_flags & TOF_SACK) || 1030 !TAILQ_EMPTY(&tp->snd_holes))) 1031 tcp_sack_doack(tp, to, th->th_ack); 1032 else 1033 /* 1034 * Reset the value so that previous (valid) value 1035 * from the last ack with SACK doesn't get used. 1036 */ 1037 tp->sackhint.sacked_bytes = 0; 1038 1039 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 1040 hhook_run_tcp_est_in(tp, th, to); 1041 1042 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1043 if (tlen == 0 && tiwin == tp->snd_wnd) { 1044 /* 1045 * If this is the first time we've seen a 1046 * FIN from the remote, this is not a 1047 * duplicate and it needs to be processed 1048 * normally. This happens during a 1049 * simultaneous close. 1050 */ 1051 if ((thflags & TH_FIN) && 1052 (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 1053 tp->t_dupacks = 0; 1054 break; 1055 } 1056 TCPSTAT_INC(tcps_rcvdupack); 1057 /* 1058 * If we have outstanding data (other than 1059 * a window probe), this is a completely 1060 * duplicate ack (ie, window info didn't 1061 * change and FIN isn't set), 1062 * the ack is the biggest we've 1063 * seen and we've seen exactly our rexmt 1064 * threshhold of them, assume a packet 1065 * has been dropped and retransmit it. 1066 * Kludge snd_nxt & the congestion 1067 * window so we send only this one 1068 * packet. 1069 * 1070 * We know we're losing at the current 1071 * window size so do congestion avoidance 1072 * (set ssthresh to half the current window 1073 * and pull our congestion window back to 1074 * the new ssthresh). 1075 * 1076 * Dup acks mean that packets have left the 1077 * network (they're now cached at the receiver) 1078 * so bump cwnd by the amount in the receiver 1079 * to keep a constant cwnd packets in the 1080 * network. 1081 * 1082 * When using TCP ECN, notify the peer that 1083 * we reduced the cwnd. 1084 */ 1085 if (!tcp_timer_active(tp, TT_REXMT) || 1086 th->th_ack != tp->snd_una) 1087 tp->t_dupacks = 0; 1088 else if (++tp->t_dupacks > tcprexmtthresh || 1089 IN_FASTRECOVERY(tp->t_flags)) { 1090 cc_ack_received(tp, th, CC_DUPACK); 1091 if ((tp->t_flags & TF_SACK_PERMIT) && 1092 IN_FASTRECOVERY(tp->t_flags)) { 1093 int awnd; 1094 1095 /* 1096 * Compute the amount of data in flight first. 1097 * We can inject new data into the pipe iff 1098 * we have less than 1/2 the original window's 1099 * worth of data in flight. 1100 */ 1101 if (V_tcp_do_rfc6675_pipe) 1102 awnd = tcp_compute_pipe(tp); 1103 else 1104 awnd = (tp->snd_nxt - tp->snd_fack) + 1105 tp->sackhint.sack_bytes_rexmit; 1106 1107 if (awnd < tp->snd_ssthresh) { 1108 tp->snd_cwnd += tp->t_maxseg; 1109 if (tp->snd_cwnd > tp->snd_ssthresh) 1110 tp->snd_cwnd = tp->snd_ssthresh; 1111 } 1112 } else 1113 tp->snd_cwnd += tp->t_maxseg; 1114 (void) tp->t_fb->tfb_tcp_output(tp); 1115 goto drop; 1116 } else if (tp->t_dupacks == tcprexmtthresh) { 1117 tcp_seq onxt = tp->snd_nxt; 1118 1119 /* 1120 * If we're doing sack, check to 1121 * see if we're already in sack 1122 * recovery. If we're not doing sack, 1123 * check to see if we're in newreno 1124 * recovery. 1125 */ 1126 if (tp->t_flags & TF_SACK_PERMIT) { 1127 if (IN_FASTRECOVERY(tp->t_flags)) { 1128 tp->t_dupacks = 0; 1129 break; 1130 } 1131 } else { 1132 if (SEQ_LEQ(th->th_ack, 1133 tp->snd_recover)) { 1134 tp->t_dupacks = 0; 1135 break; 1136 } 1137 } 1138 /* Congestion signal before ack. */ 1139 cc_cong_signal(tp, th, CC_NDUPACK); 1140 cc_ack_received(tp, th, CC_DUPACK); 1141 tcp_timer_activate(tp, TT_REXMT, 0); 1142 tp->t_rtttime = 0; 1143 if (tp->t_flags & TF_SACK_PERMIT) { 1144 TCPSTAT_INC( 1145 tcps_sack_recovery_episode); 1146 tp->sack_newdata = tp->snd_nxt; 1147 tp->snd_cwnd = tp->t_maxseg; 1148 (void) tp->t_fb->tfb_tcp_output(tp); 1149 goto drop; 1150 } 1151 tp->snd_nxt = th->th_ack; 1152 tp->snd_cwnd = tp->t_maxseg; 1153 (void) tp->t_fb->tfb_tcp_output(tp); 1154 KASSERT(tp->snd_limited <= 2, 1155 ("%s: tp->snd_limited too big", 1156 __func__)); 1157 tp->snd_cwnd = tp->snd_ssthresh + 1158 tp->t_maxseg * 1159 (tp->t_dupacks - tp->snd_limited); 1160 if (SEQ_GT(onxt, tp->snd_nxt)) 1161 tp->snd_nxt = onxt; 1162 goto drop; 1163 } else if (V_tcp_do_rfc3042) { 1164 /* 1165 * Process first and second duplicate 1166 * ACKs. Each indicates a segment 1167 * leaving the network, creating room 1168 * for more. Make sure we can send a 1169 * packet on reception of each duplicate 1170 * ACK by increasing snd_cwnd by one 1171 * segment. Restore the original 1172 * snd_cwnd after packet transmission. 1173 */ 1174 cc_ack_received(tp, th, CC_DUPACK); 1175 u_long oldcwnd = tp->snd_cwnd; 1176 tcp_seq oldsndmax = tp->snd_max; 1177 u_int sent; 1178 int avail; 1179 1180 KASSERT(tp->t_dupacks == 1 || 1181 tp->t_dupacks == 2, 1182 ("%s: dupacks not 1 or 2", 1183 __func__)); 1184 if (tp->t_dupacks == 1) 1185 tp->snd_limited = 0; 1186 tp->snd_cwnd = 1187 (tp->snd_nxt - tp->snd_una) + 1188 (tp->t_dupacks - tp->snd_limited) * 1189 tp->t_maxseg; 1190 /* 1191 * Only call tcp_output when there 1192 * is new data available to be sent. 1193 * Otherwise we would send pure ACKs. 1194 */ 1195 SOCKBUF_LOCK(&so->so_snd); 1196 avail = sbavail(&so->so_snd) - 1197 (tp->snd_nxt - tp->snd_una); 1198 SOCKBUF_UNLOCK(&so->so_snd); 1199 if (avail > 0) 1200 (void) tp->t_fb->tfb_tcp_output(tp); 1201 sent = tp->snd_max - oldsndmax; 1202 if (sent > tp->t_maxseg) { 1203 KASSERT((tp->t_dupacks == 2 && 1204 tp->snd_limited == 0) || 1205 (sent == tp->t_maxseg + 1 && 1206 tp->t_flags & TF_SENTFIN), 1207 ("%s: sent too much", 1208 __func__)); 1209 tp->snd_limited = 2; 1210 } else if (sent > 0) 1211 ++tp->snd_limited; 1212 tp->snd_cwnd = oldcwnd; 1213 goto drop; 1214 } 1215 } else 1216 tp->t_dupacks = 0; 1217 break; 1218 } 1219 1220 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 1221 ("%s: th_ack <= snd_una", __func__)); 1222 1223 /* 1224 * If the congestion window was inflated to account 1225 * for the other side's cached packets, retract it. 1226 */ 1227 if (IN_FASTRECOVERY(tp->t_flags)) { 1228 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 1229 if (tp->t_flags & TF_SACK_PERMIT) 1230 tcp_sack_partialack(tp, th); 1231 else 1232 tcp_newreno_partial_ack(tp, th); 1233 } else 1234 cc_post_recovery(tp, th); 1235 } 1236 tp->t_dupacks = 0; 1237 /* 1238 * If we reach this point, ACK is not a duplicate, 1239 * i.e., it ACKs something we sent. 1240 */ 1241 if (tp->t_flags & TF_NEEDSYN) { 1242 /* 1243 * T/TCP: Connection was half-synchronized, and our 1244 * SYN has been ACK'd (so connection is now fully 1245 * synchronized). Go to non-starred state, 1246 * increment snd_una for ACK of SYN, and check if 1247 * we can do window scaling. 1248 */ 1249 tp->t_flags &= ~TF_NEEDSYN; 1250 tp->snd_una++; 1251 /* Do window scaling? */ 1252 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1253 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1254 tp->rcv_scale = tp->request_r_scale; 1255 /* Send window already scaled. */ 1256 } 1257 } 1258 1259process_ACK: 1260 INP_WLOCK_ASSERT(tp->t_inpcb); 1261 1262 acked = BYTES_THIS_ACK(tp, th); 1263 TCPSTAT_INC(tcps_rcvackpack); 1264 TCPSTAT_ADD(tcps_rcvackbyte, acked); 1265 1266 /* 1267 * If we just performed our first retransmit, and the ACK 1268 * arrives within our recovery window, then it was a mistake 1269 * to do the retransmit in the first place. Recover our 1270 * original cwnd and ssthresh, and proceed to transmit where 1271 * we left off. 1272 */ 1273 if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 1274 (int)(ticks - tp->t_badrxtwin) < 0) 1275 cc_cong_signal(tp, th, CC_RTO_ERR); 1276 1277 /* 1278 * If we have a timestamp reply, update smoothed 1279 * round trip time. If no timestamp is present but 1280 * transmit timer is running and timed sequence 1281 * number was acked, update smoothed round trip time. 1282 * Since we now have an rtt measurement, cancel the 1283 * timer backoff (cf., Phil Karn's retransmit alg.). 1284 * Recompute the initial retransmit timer. 1285 * 1286 * Some boxes send broken timestamp replies 1287 * during the SYN+ACK phase, ignore 1288 * timestamps of 0 or we could calculate a 1289 * huge RTT and blow up the retransmit timer. 1290 */ 1291 if ((to->to_flags & TOF_TS) != 0 && to->to_tsecr) { 1292 u_int t; 1293 1294 t = tcp_ts_getticks() - to->to_tsecr; 1295 if (!tp->t_rttlow || tp->t_rttlow > t) 1296 tp->t_rttlow = t; 1297 tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 1298 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 1299 if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 1300 tp->t_rttlow = ticks - tp->t_rtttime; 1301 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1302 } 1303 1304 /* 1305 * If all outstanding data is acked, stop retransmit 1306 * timer and remember to restart (more output or persist). 1307 * If there is more data to be acked, restart retransmit 1308 * timer, using current (possibly backed-off) value. 1309 */ 1310 if (th->th_ack == tp->snd_max) { 1311 tcp_timer_activate(tp, TT_REXMT, 0); 1312 needoutput = 1; 1313 } else if (!tcp_timer_active(tp, TT_PERSIST)) 1314 tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 1315 1316 /* 1317 * If no data (only SYN) was ACK'd, 1318 * skip rest of ACK processing. 1319 */ 1320 if (acked == 0) 1321 goto step6; 1322 1323 /* 1324 * Let the congestion control algorithm update congestion 1325 * control related information. This typically means increasing 1326 * the congestion window. 1327 */ 1328 cc_ack_received(tp, th, CC_ACK); 1329 1330 SOCKBUF_LOCK(&so->so_snd); 1331 if (acked > sbavail(&so->so_snd)) { 1332 tp->snd_wnd -= sbavail(&so->so_snd); 1333 mfree = sbcut_locked(&so->so_snd, 1334 (int)sbavail(&so->so_snd)); 1335 ourfinisacked = 1; 1336 } else { 1337 mfree = sbcut_locked(&so->so_snd, acked); 1338 tp->snd_wnd -= acked; 1339 ourfinisacked = 0; 1340 } 1341 /* NB: sowwakeup_locked() does an implicit unlock. */ 1342 sowwakeup_locked(so); 1343 m_freem(mfree); 1344 /* Detect una wraparound. */ 1345 if (!IN_RECOVERY(tp->t_flags) && 1346 SEQ_GT(tp->snd_una, tp->snd_recover) && 1347 SEQ_LEQ(th->th_ack, tp->snd_recover)) 1348 tp->snd_recover = th->th_ack - 1; 1349 /* XXXLAS: Can this be moved up into cc_post_recovery? */ 1350 if (IN_RECOVERY(tp->t_flags) && 1351 SEQ_GEQ(th->th_ack, tp->snd_recover)) { 1352 EXIT_RECOVERY(tp->t_flags); 1353 } 1354 tp->snd_una = th->th_ack; 1355 if (tp->t_flags & TF_SACK_PERMIT) { 1356 if (SEQ_GT(tp->snd_una, tp->snd_recover)) 1357 tp->snd_recover = tp->snd_una; 1358 } 1359 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1360 tp->snd_nxt = tp->snd_una; 1361 1362 switch (tp->t_state) { 1363 1364 /* 1365 * In FIN_WAIT_1 STATE in addition to the processing 1366 * for the ESTABLISHED state if our FIN is now acknowledged 1367 * then enter FIN_WAIT_2. 1368 */ 1369 case TCPS_FIN_WAIT_1: 1370 if (ourfinisacked) { 1371 /* 1372 * If we can't receive any more 1373 * data, then closing user can proceed. 1374 * Starting the timer is contrary to the 1375 * specification, but if we don't get a FIN 1376 * we'll hang forever. 1377 * 1378 * XXXjl: 1379 * we should release the tp also, and use a 1380 * compressed state. 1381 */ 1382 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1383 soisdisconnected(so); 1384 tcp_timer_activate(tp, TT_2MSL, 1385 (tcp_fast_finwait2_recycle ? 1386 tcp_finwait2_timeout : 1387 TP_MAXIDLE(tp))); 1388 } 1389 tcp_state_change(tp, TCPS_FIN_WAIT_2); 1390 } 1391 break; 1392 1393 /* 1394 * In CLOSING STATE in addition to the processing for 1395 * the ESTABLISHED state if the ACK acknowledges our FIN 1396 * then enter the TIME-WAIT state, otherwise ignore 1397 * the segment. 1398 */ 1399 case TCPS_CLOSING: 1400 if (ourfinisacked) { 1401 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1402 tcp_twstart(tp); 1403 INP_INFO_RUNLOCK(&V_tcbinfo); 1404 m_freem(m); 1405 return; 1406 } 1407 break; 1408 1409 /* 1410 * In LAST_ACK, we may still be waiting for data to drain 1411 * and/or to be acked, as well as for the ack of our FIN. 1412 * If our FIN is now acknowledged, delete the TCB, 1413 * enter the closed state and return. 1414 */ 1415 case TCPS_LAST_ACK: 1416 if (ourfinisacked) { 1417 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1418 tp = tcp_close(tp); 1419 goto drop; 1420 } 1421 break; 1422 } 1423 } 1424 1425step6: 1426 INP_WLOCK_ASSERT(tp->t_inpcb); 1427 1428 /* 1429 * Update window information. 1430 * Don't look at window if no ACK: TAC's send garbage on first SYN. 1431 */ 1432 if ((thflags & TH_ACK) && 1433 (SEQ_LT(tp->snd_wl1, th->th_seq) || 1434 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 1435 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 1436 /* keep track of pure window updates */ 1437 if (tlen == 0 && 1438 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 1439 TCPSTAT_INC(tcps_rcvwinupd); 1440 tp->snd_wnd = tiwin; 1441 tp->snd_wl1 = th->th_seq; 1442 tp->snd_wl2 = th->th_ack; 1443 if (tp->snd_wnd > tp->max_sndwnd) 1444 tp->max_sndwnd = tp->snd_wnd; 1445 needoutput = 1; 1446 } 1447 1448 /* 1449 * Process segments with URG. 1450 */ 1451 if ((thflags & TH_URG) && th->th_urp && 1452 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1453 /* 1454 * This is a kludge, but if we receive and accept 1455 * random urgent pointers, we'll crash in 1456 * soreceive. It's hard to imagine someone 1457 * actually wanting to send this much urgent data. 1458 */ 1459 SOCKBUF_LOCK(&so->so_rcv); 1460 if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 1461 th->th_urp = 0; /* XXX */ 1462 thflags &= ~TH_URG; /* XXX */ 1463 SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 1464 goto dodata; /* XXX */ 1465 } 1466 /* 1467 * If this segment advances the known urgent pointer, 1468 * then mark the data stream. This should not happen 1469 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 1470 * a FIN has been received from the remote side. 1471 * In these states we ignore the URG. 1472 * 1473 * According to RFC961 (Assigned Protocols), 1474 * the urgent pointer points to the last octet 1475 * of urgent data. We continue, however, 1476 * to consider it to indicate the first octet 1477 * of data past the urgent section as the original 1478 * spec states (in one of two places). 1479 */ 1480 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 1481 tp->rcv_up = th->th_seq + th->th_urp; 1482 so->so_oobmark = sbavail(&so->so_rcv) + 1483 (tp->rcv_up - tp->rcv_nxt) - 1; 1484 if (so->so_oobmark == 0) 1485 so->so_rcv.sb_state |= SBS_RCVATMARK; 1486 sohasoutofband(so); 1487 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1488 } 1489 SOCKBUF_UNLOCK(&so->so_rcv); 1490 /* 1491 * Remove out of band data so doesn't get presented to user. 1492 * This can happen independent of advancing the URG pointer, 1493 * but if two URG's are pending at once, some out-of-band 1494 * data may creep in... ick. 1495 */ 1496 if (th->th_urp <= (u_long)tlen && 1497 !(so->so_options & SO_OOBINLINE)) { 1498 /* hdr drop is delayed */ 1499 tcp_pulloutofband(so, th, m, drop_hdrlen); 1500 } 1501 } else { 1502 /* 1503 * If no out of band data is expected, 1504 * pull receive urgent pointer along 1505 * with the receive window. 1506 */ 1507 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1508 tp->rcv_up = tp->rcv_nxt; 1509 } 1510dodata: /* XXX */ 1511 INP_WLOCK_ASSERT(tp->t_inpcb); 1512 1513 /* 1514 * Process the segment text, merging it into the TCP sequencing queue, 1515 * and arranging for acknowledgment of receipt if necessary. 1516 * This process logically involves adjusting tp->rcv_wnd as data 1517 * is presented to the user (this happens in tcp_usrreq.c, 1518 * case PRU_RCVD). If a FIN has already been received on this 1519 * connection then we just ignore the text. 1520 */ 1521 if ((tlen || (thflags & TH_FIN)) && 1522 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1523 tcp_seq save_start = th->th_seq; 1524 m_adj(m, drop_hdrlen); /* delayed header drop */ 1525 /* 1526 * Insert segment which includes th into TCP reassembly queue 1527 * with control block tp. Set thflags to whether reassembly now 1528 * includes a segment with FIN. This handles the common case 1529 * inline (segment is the next to be received on an established 1530 * connection, and the queue is empty), avoiding linkage into 1531 * and removal from the queue and repetition of various 1532 * conversions. 1533 * Set DELACK for segments received in order, but ack 1534 * immediately when segments are out of order (so 1535 * fast retransmit can work). 1536 */ 1537 if (th->th_seq == tp->rcv_nxt && 1538 LIST_EMPTY(&tp->t_segq) && 1539 TCPS_HAVEESTABLISHED(tp->t_state)) { 1540 if (DELAY_ACK(tp, tlen)) 1541 tp->t_flags |= TF_DELACK; 1542 else 1543 tp->t_flags |= TF_ACKNOW; 1544 tp->rcv_nxt += tlen; 1545 thflags = th->th_flags & TH_FIN; 1546 TCPSTAT_INC(tcps_rcvpack); 1547 TCPSTAT_ADD(tcps_rcvbyte, tlen); 1548 SOCKBUF_LOCK(&so->so_rcv); 1549 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 1550 m_freem(m); 1551 else 1552 sbappendstream_locked(&so->so_rcv, m, 0); 1553 /* NB: sorwakeup_locked() does an implicit unlock. */ 1554 sorwakeup_locked(so); 1555 } else { 1556 /* 1557 * XXX: Due to the header drop above "th" is 1558 * theoretically invalid by now. Fortunately 1559 * m_adj() doesn't actually frees any mbufs 1560 * when trimming from the head. 1561 */ 1562 thflags = tcp_reass(tp, th, &tlen, m); 1563 tp->t_flags |= TF_ACKNOW; 1564 } 1565 if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 1566 tcp_update_sack_list(tp, save_start, save_start + tlen); 1567#if 0 1568 /* 1569 * Note the amount of data that peer has sent into 1570 * our window, in order to estimate the sender's 1571 * buffer size. 1572 * XXX: Unused. 1573 */ 1574 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 1575 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1576 else 1577 len = so->so_rcv.sb_hiwat; 1578#endif 1579 } else { 1580 m_freem(m); 1581 thflags &= ~TH_FIN; 1582 } 1583 1584 /* 1585 * If FIN is received ACK the FIN and let the user know 1586 * that the connection is closing. 1587 */ 1588 if (thflags & TH_FIN) { 1589 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1590 socantrcvmore(so); 1591 /* 1592 * If connection is half-synchronized 1593 * (ie NEEDSYN flag on) then delay ACK, 1594 * so it may be piggybacked when SYN is sent. 1595 * Otherwise, since we received a FIN then no 1596 * more input can be expected, send ACK now. 1597 */ 1598 if (tp->t_flags & TF_NEEDSYN) 1599 tp->t_flags |= TF_DELACK; 1600 else 1601 tp->t_flags |= TF_ACKNOW; 1602 tp->rcv_nxt++; 1603 } 1604 switch (tp->t_state) { 1605 1606 /* 1607 * In SYN_RECEIVED and ESTABLISHED STATES 1608 * enter the CLOSE_WAIT state. 1609 */ 1610 case TCPS_SYN_RECEIVED: 1611 tp->t_starttime = ticks; 1612 /* FALLTHROUGH */ 1613 case TCPS_ESTABLISHED: 1614 tcp_state_change(tp, TCPS_CLOSE_WAIT); 1615 break; 1616 1617 /* 1618 * If still in FIN_WAIT_1 STATE FIN has not been acked so 1619 * enter the CLOSING state. 1620 */ 1621 case TCPS_FIN_WAIT_1: 1622 tcp_state_change(tp, TCPS_CLOSING); 1623 break; 1624 1625 /* 1626 * In FIN_WAIT_2 state enter the TIME_WAIT state, 1627 * starting the time-wait timer, turning off the other 1628 * standard timers. 1629 */ 1630 case TCPS_FIN_WAIT_2: 1631 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1632 KASSERT(ti_locked == TI_RLOCKED, ("%s: dodata " 1633 "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 1634 ti_locked)); 1635 1636 tcp_twstart(tp); 1637 INP_INFO_RUNLOCK(&V_tcbinfo); 1638 return; 1639 } 1640 } 1641 if (ti_locked == TI_RLOCKED) { 1642 INP_INFO_RUNLOCK(&V_tcbinfo); 1643 } 1644 ti_locked = TI_UNLOCKED; 1645 1646#ifdef TCPDEBUG 1647 if (so->so_options & SO_DEBUG) 1648 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 1649 &tcp_savetcp, 0); 1650#endif 1651 TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 1652 1653 /* 1654 * Return any desired output. 1655 */ 1656 if (needoutput || (tp->t_flags & TF_ACKNOW)) 1657 (void) tp->t_fb->tfb_tcp_output(tp); 1658 1659 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 1660 __func__, ti_locked)); 1661 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1662 INP_WLOCK_ASSERT(tp->t_inpcb); 1663 1664 if (tp->t_flags & TF_DELACK) { 1665 tp->t_flags &= ~TF_DELACK; 1666 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 1667 } 1668 INP_WUNLOCK(tp->t_inpcb); 1669 return; 1670 1671dropafterack: 1672 /* 1673 * Generate an ACK dropping incoming segment if it occupies 1674 * sequence space, where the ACK reflects our state. 1675 * 1676 * We can now skip the test for the RST flag since all 1677 * paths to this code happen after packets containing 1678 * RST have been dropped. 1679 * 1680 * In the SYN-RECEIVED state, don't send an ACK unless the 1681 * segment we received passes the SYN-RECEIVED ACK test. 1682 * If it fails send a RST. This breaks the loop in the 1683 * "LAND" DoS attack, and also prevents an ACK storm 1684 * between two listening ports that have been sent forged 1685 * SYN segments, each with the source address of the other. 1686 */ 1687 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 1688 (SEQ_GT(tp->snd_una, th->th_ack) || 1689 SEQ_GT(th->th_ack, tp->snd_max)) ) { 1690 rstreason = BANDLIM_RST_OPENPORT; 1691 goto dropwithreset; 1692 } 1693#ifdef TCPDEBUG 1694 if (so->so_options & SO_DEBUG) 1695 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 1696 &tcp_savetcp, 0); 1697#endif 1698 TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 1699 if (ti_locked == TI_RLOCKED) { 1700 INP_INFO_RUNLOCK(&V_tcbinfo); 1701 } 1702 ti_locked = TI_UNLOCKED; 1703 1704 tp->t_flags |= TF_ACKNOW; 1705 (void) tp->t_fb->tfb_tcp_output(tp); 1706 INP_WUNLOCK(tp->t_inpcb); 1707 m_freem(m); 1708 return; 1709 1710dropwithreset: 1711 if (ti_locked == TI_RLOCKED) { 1712 INP_INFO_RUNLOCK(&V_tcbinfo); 1713 } 1714 ti_locked = TI_UNLOCKED; 1715 1716 if (tp != NULL) { 1717 tcp_dropwithreset(m, th, tp, tlen, rstreason); 1718 INP_WUNLOCK(tp->t_inpcb); 1719 } else 1720 tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1721 return; 1722 1723drop: 1724 if (ti_locked == TI_RLOCKED) { 1725 INP_INFO_RUNLOCK(&V_tcbinfo); 1726 ti_locked = TI_UNLOCKED; 1727 } 1728#ifdef INVARIANTS 1729 else 1730 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1731#endif 1732 1733 /* 1734 * Drop space held by incoming segment and return. 1735 */ 1736#ifdef TCPDEBUG 1737 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1738 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 1739 &tcp_savetcp, 0); 1740#endif 1741 TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 1742 if (tp != NULL) 1743 INP_WUNLOCK(tp->t_inpcb); 1744 m_freem(m); 1745} 1746 1747 1748/* 1749 * Do fast slow is a combination of the original 1750 * tcp_dosegment and a split fastpath, one function 1751 * for the fast-ack which also includes allowing fastpath 1752 * for window advanced in sequence acks. And also a 1753 * sub-function that handles the insequence data. 1754 */ 1755void 1756tcp_do_segment_fastslow(struct mbuf *m, struct tcphdr *th, struct socket *so, 1757 struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1758 int ti_locked) 1759{ 1760 int thflags; 1761 u_long tiwin; 1762 char *s; 1763 int can_enter; 1764 struct in_conninfo *inc; 1765 struct tcpopt to; 1766 1767 thflags = th->th_flags; 1768 tp->sackhint.last_sack_ack = 0; 1769 inc = &tp->t_inpcb->inp_inc; 1770 /* 1771 * If this is either a state-changing packet or current state isn't 1772 * established, we require a write lock on tcbinfo. Otherwise, we 1773 * allow the tcbinfo to be in either alocked or unlocked, as the 1774 * caller may have unnecessarily acquired a write lock due to a race. 1775 */ 1776 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1777 tp->t_state != TCPS_ESTABLISHED) { 1778 KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for " 1779 "SYN/FIN/RST/!EST", __func__, ti_locked)); 1780 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1781 } else { 1782#ifdef INVARIANTS 1783 if (ti_locked == TI_RLOCKED) { 1784 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1785 } else { 1786 KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1787 "ti_locked: %d", __func__, ti_locked)); 1788 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1789 } 1790#endif 1791 } 1792 INP_WLOCK_ASSERT(tp->t_inpcb); 1793 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1794 __func__)); 1795 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1796 __func__)); 1797 1798 /* 1799 * Segment received on connection. 1800 * Reset idle time and keep-alive timer. 1801 * XXX: This should be done after segment 1802 * validation to ignore broken/spoofed segs. 1803 */ 1804 tp->t_rcvtime = ticks; 1805 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1806 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 1807 1808 /* 1809 * Unscale the window into a 32-bit value. 1810 * For the SYN_SENT state the scale is zero. 1811 */ 1812 tiwin = th->th_win << tp->snd_scale; 1813 1814 /* 1815 * TCP ECN processing. 1816 */ 1817 if (tp->t_flags & TF_ECN_PERMIT) { 1818 if (thflags & TH_CWR) 1819 tp->t_flags &= ~TF_ECN_SND_ECE; 1820 switch (iptos & IPTOS_ECN_MASK) { 1821 case IPTOS_ECN_CE: 1822 tp->t_flags |= TF_ECN_SND_ECE; 1823 TCPSTAT_INC(tcps_ecn_ce); 1824 break; 1825 case IPTOS_ECN_ECT0: 1826 TCPSTAT_INC(tcps_ecn_ect0); 1827 break; 1828 case IPTOS_ECN_ECT1: 1829 TCPSTAT_INC(tcps_ecn_ect1); 1830 break; 1831 } 1832 /* Congestion experienced. */ 1833 if (thflags & TH_ECE) { 1834 cc_cong_signal(tp, th, CC_ECN); 1835 } 1836 } 1837 1838 /* 1839 * Parse options on any incoming segment. 1840 */ 1841 tcp_dooptions(&to, (u_char *)(th + 1), 1842 (th->th_off << 2) - sizeof(struct tcphdr), 1843 (thflags & TH_SYN) ? TO_SYN : 0); 1844 1845 /* 1846 * If echoed timestamp is later than the current time, 1847 * fall back to non RFC1323 RTT calculation. Normalize 1848 * timestamp if syncookies were used when this connection 1849 * was established. 1850 */ 1851 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1852 to.to_tsecr -= tp->ts_offset; 1853 if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1854 to.to_tsecr = 0; 1855 } 1856 /* 1857 * If timestamps were negotiated during SYN/ACK they should 1858 * appear on every segment during this session and vice versa. 1859 */ 1860 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 1861 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1862 log(LOG_DEBUG, "%s; %s: Timestamp missing, " 1863 "no action\n", s, __func__); 1864 free(s, M_TCPLOG); 1865 } 1866 } 1867 if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 1868 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1869 log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 1870 "no action\n", s, __func__); 1871 free(s, M_TCPLOG); 1872 } 1873 } 1874 1875 /* 1876 * Process options only when we get SYN/ACK back. The SYN case 1877 * for incoming connections is handled in tcp_syncache. 1878 * According to RFC1323 the window field in a SYN (i.e., a <SYN> 1879 * or <SYN,ACK>) segment itself is never scaled. 1880 * XXX this is traditional behavior, may need to be cleaned up. 1881 */ 1882 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1883 if ((to.to_flags & TOF_SCALE) && 1884 (tp->t_flags & TF_REQ_SCALE)) { 1885 tp->t_flags |= TF_RCVD_SCALE; 1886 tp->snd_scale = to.to_wscale; 1887 } 1888 /* 1889 * Initial send window. It will be updated with 1890 * the next incoming segment to the scaled value. 1891 */ 1892 tp->snd_wnd = th->th_win; 1893 if (to.to_flags & TOF_TS) { 1894 tp->t_flags |= TF_RCVD_TSTMP; 1895 tp->ts_recent = to.to_tsval; 1896 tp->ts_recent_age = tcp_ts_getticks(); 1897 } 1898 if (to.to_flags & TOF_MSS) 1899 tcp_mss(tp, to.to_mss); 1900 if ((tp->t_flags & TF_SACK_PERMIT) && 1901 (to.to_flags & TOF_SACKPERM) == 0) 1902 tp->t_flags &= ~TF_SACK_PERMIT; 1903 } 1904 can_enter = 0; 1905 if (__predict_true((tlen == 0))) { 1906 /* 1907 * The ack moved forward and we have a window (non-zero) 1908 * <or> 1909 * The ack did not move forward, but the window increased. 1910 */ 1911 if (__predict_true((SEQ_GT(th->th_ack, tp->snd_una) && tiwin) || 1912 ((th->th_ack == tp->snd_una) && tiwin && (tiwin > tp->snd_wnd)))) { 1913 can_enter = 1; 1914 } 1915 } else { 1916 /* 1917 * Data incoming, use the old entry criteria 1918 * for fast-path with data. 1919 */ 1920 if ((tiwin && tiwin == tp->snd_wnd)) { 1921 can_enter = 1; 1922 } 1923 } 1924 /* 1925 * Header prediction: check for the two common cases 1926 * of a uni-directional data xfer. If the packet has 1927 * no control flags, is in-sequence, the window didn't 1928 * change and we're not retransmitting, it's a 1929 * candidate. If the length is zero and the ack moved 1930 * forward, we're the sender side of the xfer. Just 1931 * free the data acked & wake any higher level process 1932 * that was blocked waiting for space. If the length 1933 * is non-zero and the ack didn't move, we're the 1934 * receiver side. If we're getting packets in-order 1935 * (the reassembly queue is empty), add the data to 1936 * the socket buffer and note that we need a delayed ack. 1937 * Make sure that the hidden state-flags are also off. 1938 * Since we check for TCPS_ESTABLISHED first, it can only 1939 * be TH_NEEDSYN. 1940 */ 1941 if (__predict_true(tp->t_state == TCPS_ESTABLISHED && 1942 th->th_seq == tp->rcv_nxt && 1943 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1944 tp->snd_nxt == tp->snd_max && 1945 can_enter && 1946 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1947 LIST_EMPTY(&tp->t_segq) && 1948 ((to.to_flags & TOF_TS) == 0 || 1949 TSTMP_GEQ(to.to_tsval, tp->ts_recent)))) { 1950 if (__predict_true((tlen == 0) && 1951 (SEQ_LEQ(th->th_ack, tp->snd_max) && 1952 !IN_RECOVERY(tp->t_flags) && 1953 (to.to_flags & TOF_SACK) == 0 && 1954 TAILQ_EMPTY(&tp->snd_holes)))) { 1955 /* We are done */ 1956 tcp_do_fastack(m, th, so, tp, &to, drop_hdrlen, tlen, 1957 ti_locked, tiwin); 1958 return; 1959 } else if ((tlen) && 1960 (th->th_ack == tp->snd_una && 1961 tlen <= sbspace(&so->so_rcv))) { 1962 tcp_do_fastnewdata(m, th, so, tp, &to, drop_hdrlen, tlen, 1963 ti_locked, tiwin); 1964 /* We are done */ 1965 return; 1966 } 1967 } 1968 tcp_do_slowpath(m, th, so, tp, &to, drop_hdrlen, tlen, 1969 ti_locked, tiwin, thflags); 1970} 1971 1972 1973/* 1974 * This subfunction is used to try to highly optimize the 1975 * fast path. We again allow window updates that are 1976 * in sequence to remain in the fast-path. We also add 1977 * in the __predict's to attempt to help the compiler. 1978 * Note that if we return a 0, then we can *not* process 1979 * it and the caller should push the packet into the 1980 * slow-path. 1981 */ 1982static int 1983tcp_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 1984 struct tcpcb *tp, struct tcpopt *to, int drop_hdrlen, int tlen, 1985 int ti_locked, u_long tiwin) 1986{ 1987 int acked; 1988 int winup_only=0; 1989#ifdef TCPDEBUG 1990 /* 1991 * The size of tcp_saveipgen must be the size of the max ip header, 1992 * now IPv6. 1993 */ 1994 u_char tcp_saveipgen[IP6_HDR_LEN]; 1995 struct tcphdr tcp_savetcp; 1996 short ostate = 0; 1997#endif 1998 1999 2000 if (__predict_false(SEQ_LEQ(th->th_ack, tp->snd_una))) { 2001 /* Old ack, behind (or duplicate to) the last one rcv'd */ 2002 return (0); 2003 } 2004 if (__predict_false(th->th_ack == tp->snd_una) && 2005 __predict_false(tiwin <= tp->snd_wnd)) { 2006 /* duplicate ack <or> a shrinking dup ack with shrinking window */ 2007 return (0); 2008 } 2009 if (__predict_false(tiwin == 0)) { 2010 /* zero window */ 2011 return (0); 2012 } 2013 if (__predict_false(SEQ_GT(th->th_ack, tp->snd_max))) { 2014 /* Above what we have sent? */ 2015 return (0); 2016 } 2017 if (__predict_false(tp->snd_nxt != tp->snd_max)) { 2018 /* We are retransmitting */ 2019 return (0); 2020 } 2021 if (__predict_false(tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))) { 2022 /* We need a SYN or a FIN, unlikely.. */ 2023 return (0); 2024 } 2025 if((to->to_flags & TOF_TS) && __predict_false(TSTMP_LT(to->to_tsval, tp->ts_recent))) { 2026 /* Timestamp is behind .. old ack with seq wrap? */ 2027 return (0); 2028 } 2029 if (__predict_false(IN_RECOVERY(tp->t_flags))) { 2030 /* Still recovering */ 2031 return (0); 2032 } 2033 if (__predict_false(to->to_flags & TOF_SACK)) { 2034 /* Sack included in the ack.. */ 2035 return (0); 2036 } 2037 if (!TAILQ_EMPTY(&tp->snd_holes)) { 2038 /* We have sack holes on our scoreboard */ 2039 return (0); 2040 } 2041 /* Ok if we reach here, we can process a fast-ack */ 2042 2043 /* Did the window get updated? */ 2044 if (tiwin != tp->snd_wnd) { 2045 /* keep track of pure window updates */ 2046 if (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) { 2047 winup_only = 1; 2048 TCPSTAT_INC(tcps_rcvwinupd); 2049 } 2050 tp->snd_wnd = tiwin; 2051 tp->snd_wl1 = th->th_seq; 2052 if (tp->snd_wnd > tp->max_sndwnd) 2053 tp->max_sndwnd = tp->snd_wnd; 2054 } 2055 /* 2056 * Pull snd_wl2 up to prevent seq wrap relative 2057 * to th_ack. 2058 */ 2059 tp->snd_wl2 = th->th_ack; 2060 /* 2061 * If last ACK falls within this segment's sequence numbers, 2062 * record the timestamp. 2063 * NOTE that the test is modified according to the latest 2064 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2065 */ 2066 if ((to->to_flags & TOF_TS) != 0 && 2067 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 2068 tp->ts_recent_age = tcp_ts_getticks(); 2069 tp->ts_recent = to->to_tsval; 2070 } 2071 /* 2072 * This is a pure ack for outstanding data. 2073 */ 2074 if (ti_locked == TI_RLOCKED) { 2075 INP_INFO_RUNLOCK(&V_tcbinfo); 2076 } 2077 ti_locked = TI_UNLOCKED; 2078 2079 TCPSTAT_INC(tcps_predack); 2080 2081 /* 2082 * "bad retransmit" recovery. 2083 */ 2084 if (tp->t_rxtshift == 1 && 2085 tp->t_flags & TF_PREVVALID && 2086 (int)(ticks - tp->t_badrxtwin) < 0) { 2087 cc_cong_signal(tp, th, CC_RTO_ERR); 2088 } 2089 2090 /* 2091 * Recalculate the transmit timer / rtt. 2092 * 2093 * Some boxes send broken timestamp replies 2094 * during the SYN+ACK phase, ignore 2095 * timestamps of 0 or we could calculate a 2096 * huge RTT and blow up the retransmit timer. 2097 */ 2098 if ((to->to_flags & TOF_TS) != 0 && 2099 to->to_tsecr) { 2100 u_int t; 2101 2102 t = tcp_ts_getticks() - to->to_tsecr; 2103 if (!tp->t_rttlow || tp->t_rttlow > t) 2104 tp->t_rttlow = t; 2105 tcp_xmit_timer(tp, 2106 TCP_TS_TO_TICKS(t) + 1); 2107 } else if (tp->t_rtttime && 2108 SEQ_GT(th->th_ack, tp->t_rtseq)) { 2109 if (!tp->t_rttlow || 2110 tp->t_rttlow > ticks - tp->t_rtttime) 2111 tp->t_rttlow = ticks - tp->t_rtttime; 2112 tcp_xmit_timer(tp, 2113 ticks - tp->t_rtttime); 2114 } 2115 if (winup_only == 0) { 2116 acked = BYTES_THIS_ACK(tp, th); 2117 2118 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 2119 hhook_run_tcp_est_in(tp, th, to); 2120 2121 TCPSTAT_ADD(tcps_rcvackbyte, acked); 2122 sbdrop(&so->so_snd, acked); 2123 if (SEQ_GT(tp->snd_una, tp->snd_recover) && 2124 SEQ_LEQ(th->th_ack, tp->snd_recover)) 2125 tp->snd_recover = th->th_ack - 1; 2126 2127 /* 2128 * Let the congestion control algorithm update 2129 * congestion control related information. This 2130 * typically means increasing the congestion 2131 * window. 2132 */ 2133 cc_ack_received(tp, th, CC_ACK); 2134 2135 tp->snd_una = th->th_ack; 2136 tp->t_dupacks = 0; 2137 m_freem(m); 2138 2139 /* 2140 * If all outstanding data are acked, stop 2141 * retransmit timer, otherwise restart timer 2142 * using current (possibly backed-off) value. 2143 * If process is waiting for space, 2144 * wakeup/selwakeup/signal. If data 2145 * are ready to send, let tcp_output 2146 * decide between more output or persist. 2147 */ 2148#ifdef TCPDEBUG 2149 if (so->so_options & SO_DEBUG) 2150 tcp_trace(TA_INPUT, ostate, tp, 2151 (void *)tcp_saveipgen, 2152 &tcp_savetcp, 0); 2153#endif 2154 if (tp->snd_una == tp->snd_max) 2155 tcp_timer_activate(tp, TT_REXMT, 0); 2156 else if (!tcp_timer_active(tp, TT_PERSIST)) 2157 tcp_timer_activate(tp, TT_REXMT, 2158 tp->t_rxtcur); 2159 /* Wake up the socket if we have room to write more */ 2160 sowwakeup(so); 2161 } else { 2162 /* 2163 * Window update only, just free the mbufs and 2164 * send out whatever we can. 2165 */ 2166 m_freem(m); 2167 } 2168 if (sbavail(&so->so_snd)) 2169 (void) tcp_output(tp); 2170 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 2171 __func__, ti_locked)); 2172 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2173 INP_WLOCK_ASSERT(tp->t_inpcb); 2174 2175 if (tp->t_flags & TF_DELACK) { 2176 tp->t_flags &= ~TF_DELACK; 2177 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 2178 } 2179 INP_WUNLOCK(tp->t_inpcb); 2180 return (1); 2181} 2182 2183/* 2184 * This tcp-do-segment concentrates on making the fastest 2185 * ack processing path. It does not have a fast-path for 2186 * data (it possibly could which would then eliminate the 2187 * need for fast-slow above). For a content distributor having 2188 * large outgoing elephants and very very little coming in 2189 * having no fastpath for data does not really help (since you 2190 * don't get much data in). The most important thing is 2191 * processing ack's quickly and getting the rest of the data 2192 * output to the peer as quickly as possible. This routine 2193 * seems to be about an overall 3% faster then the old 2194 * tcp_do_segment and keeps us in the fast-path for packets 2195 * much more (by allowing window updates to also stay in the fastpath). 2196 */ 2197void 2198tcp_do_segment_fastack(struct mbuf *m, struct tcphdr *th, struct socket *so, 2199 struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 2200 int ti_locked) 2201{ 2202 int thflags; 2203 u_long tiwin; 2204 char *s; 2205 struct in_conninfo *inc; 2206 struct tcpopt to; 2207 2208 thflags = th->th_flags; 2209 tp->sackhint.last_sack_ack = 0; 2210 inc = &tp->t_inpcb->inp_inc; 2211 /* 2212 * If this is either a state-changing packet or current state isn't 2213 * established, we require a write lock on tcbinfo. Otherwise, we 2214 * allow the tcbinfo to be in either alocked or unlocked, as the 2215 * caller may have unnecessarily acquired a write lock due to a race. 2216 */ 2217 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 2218 tp->t_state != TCPS_ESTABLISHED) { 2219 KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for " 2220 "SYN/FIN/RST/!EST", __func__, ti_locked)); 2221 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2222 } else { 2223#ifdef INVARIANTS 2224 if (ti_locked == TI_RLOCKED) { 2225 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2226 } else { 2227 KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 2228 "ti_locked: %d", __func__, ti_locked)); 2229 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2230 } 2231#endif 2232 } 2233 INP_WLOCK_ASSERT(tp->t_inpcb); 2234 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 2235 __func__)); 2236 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 2237 __func__)); 2238 2239 /* 2240 * Segment received on connection. 2241 * Reset idle time and keep-alive timer. 2242 * XXX: This should be done after segment 2243 * validation to ignore broken/spoofed segs. 2244 */ 2245 tp->t_rcvtime = ticks; 2246 if (TCPS_HAVEESTABLISHED(tp->t_state)) 2247 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 2248 2249 /* 2250 * Unscale the window into a 32-bit value. 2251 * For the SYN_SENT state the scale is zero. 2252 */ 2253 tiwin = th->th_win << tp->snd_scale; 2254 2255 /* 2256 * TCP ECN processing. 2257 */ 2258 if (tp->t_flags & TF_ECN_PERMIT) { 2259 if (thflags & TH_CWR) 2260 tp->t_flags &= ~TF_ECN_SND_ECE; 2261 switch (iptos & IPTOS_ECN_MASK) { 2262 case IPTOS_ECN_CE: 2263 tp->t_flags |= TF_ECN_SND_ECE; 2264 TCPSTAT_INC(tcps_ecn_ce); 2265 break; 2266 case IPTOS_ECN_ECT0: 2267 TCPSTAT_INC(tcps_ecn_ect0); 2268 break; 2269 case IPTOS_ECN_ECT1: 2270 TCPSTAT_INC(tcps_ecn_ect1); 2271 break; 2272 } 2273 /* Congestion experienced. */ 2274 if (thflags & TH_ECE) { 2275 cc_cong_signal(tp, th, CC_ECN); 2276 } 2277 } 2278 2279 /* 2280 * Parse options on any incoming segment. 2281 */ 2282 tcp_dooptions(&to, (u_char *)(th + 1), 2283 (th->th_off << 2) - sizeof(struct tcphdr), 2284 (thflags & TH_SYN) ? TO_SYN : 0); 2285 2286 /* 2287 * If echoed timestamp is later than the current time, 2288 * fall back to non RFC1323 RTT calculation. Normalize 2289 * timestamp if syncookies were used when this connection 2290 * was established. 2291 */ 2292 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 2293 to.to_tsecr -= tp->ts_offset; 2294 if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 2295 to.to_tsecr = 0; 2296 } 2297 /* 2298 * If timestamps were negotiated during SYN/ACK they should 2299 * appear on every segment during this session and vice versa. 2300 */ 2301 if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 2302 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 2303 log(LOG_DEBUG, "%s; %s: Timestamp missing, " 2304 "no action\n", s, __func__); 2305 free(s, M_TCPLOG); 2306 } 2307 } 2308 if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 2309 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 2310 log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 2311 "no action\n", s, __func__); 2312 free(s, M_TCPLOG); 2313 } 2314 } 2315 2316 /* 2317 * Process options only when we get SYN/ACK back. The SYN case 2318 * for incoming connections is handled in tcp_syncache. 2319 * According to RFC1323 the window field in a SYN (i.e., a <SYN> 2320 * or <SYN,ACK>) segment itself is never scaled. 2321 * XXX this is traditional behavior, may need to be cleaned up. 2322 */ 2323 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 2324 if ((to.to_flags & TOF_SCALE) && 2325 (tp->t_flags & TF_REQ_SCALE)) { 2326 tp->t_flags |= TF_RCVD_SCALE; 2327 tp->snd_scale = to.to_wscale; 2328 } 2329 /* 2330 * Initial send window. It will be updated with 2331 * the next incoming segment to the scaled value. 2332 */ 2333 tp->snd_wnd = th->th_win; 2334 if (to.to_flags & TOF_TS) { 2335 tp->t_flags |= TF_RCVD_TSTMP; 2336 tp->ts_recent = to.to_tsval; 2337 tp->ts_recent_age = tcp_ts_getticks(); 2338 } 2339 if (to.to_flags & TOF_MSS) 2340 tcp_mss(tp, to.to_mss); 2341 if ((tp->t_flags & TF_SACK_PERMIT) && 2342 (to.to_flags & TOF_SACKPERM) == 0) 2343 tp->t_flags &= ~TF_SACK_PERMIT; 2344 } 2345 /* 2346 * Header prediction: check for the two common cases 2347 * of a uni-directional data xfer. If the packet has 2348 * no control flags, is in-sequence, the window didn't 2349 * change and we're not retransmitting, it's a 2350 * candidate. If the length is zero and the ack moved 2351 * forward, we're the sender side of the xfer. Just 2352 * free the data acked & wake any higher level process 2353 * that was blocked waiting for space. If the length 2354 * is non-zero and the ack didn't move, we're the 2355 * receiver side. If we're getting packets in-order 2356 * (the reassembly queue is empty), add the data to 2357 * the socket buffer and note that we need a delayed ack. 2358 * Make sure that the hidden state-flags are also off. 2359 * Since we check for TCPS_ESTABLISHED first, it can only 2360 * be TH_NEEDSYN. 2361 */ 2362 if (__predict_true(tp->t_state == TCPS_ESTABLISHED) && 2363 __predict_true(((to.to_flags & TOF_SACK) == 0)) && 2364 __predict_true(tlen == 0) && 2365 __predict_true((thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK) && 2366 __predict_true(LIST_EMPTY(&tp->t_segq)) && 2367 __predict_true(th->th_seq == tp->rcv_nxt)) { 2368 if (tcp_fastack(m, th, so, tp, &to, drop_hdrlen, tlen, 2369 ti_locked, tiwin)) { 2370 return; 2371 } 2372 } 2373 tcp_do_slowpath(m, th, so, tp, &to, drop_hdrlen, tlen, 2374 ti_locked, tiwin, thflags); 2375} 2376 2377struct tcp_function_block __tcp_fastslow = { 2378 "fastslow", 2379 tcp_output, 2380 tcp_do_segment_fastslow, 2381 tcp_default_ctloutput, 2382 NULL, 2383 NULL, 2384 NULL, 2385 NULL, 2386 NULL, 2387 NULL, 2388 NULL, 2389 0, 2390 0 2391 2392}; 2393 2394struct tcp_function_block __tcp_fastack = { 2395 "fastack", 2396 tcp_output, 2397 tcp_do_segment_fastack, 2398 tcp_default_ctloutput, 2399 NULL, 2400 NULL, 2401 NULL, 2402 NULL, 2403 NULL, 2404 NULL, 2405 NULL, 2406 0, 2407 0 2408}; 2409 2410static int 2411tcp_addfastpaths(module_t mod, int type, void *data) 2412{ 2413 int err=0; 2414 2415 switch (type) { 2416 case MOD_LOAD: 2417 err = register_tcp_functions(&__tcp_fastack, M_WAITOK); 2418 if (err) { 2419 printf("Failed to register fastack module -- err:%d\n", err); 2420 return(err); 2421 } 2422 err = register_tcp_functions(&__tcp_fastslow, M_WAITOK); 2423 if (err) { 2424 printf("Failed to register fastslow module -- err:%d\n", err); 2425 deregister_tcp_functions(&__tcp_fastack); 2426 return(err); 2427 } 2428 break; 2429 case MOD_QUIESCE: 2430 if ((__tcp_fastslow.tfb_refcnt) ||( __tcp_fastack.tfb_refcnt)) { 2431 return(EBUSY); 2432 } 2433 break; 2434 case MOD_UNLOAD: 2435 err = deregister_tcp_functions(&__tcp_fastack); 2436 if (err == EBUSY) 2437 break; 2438 err = deregister_tcp_functions(&__tcp_fastslow); 2439 if (err == EBUSY) 2440 break; 2441 err = 0; 2442 break; 2443 default: 2444 return (EOPNOTSUPP); 2445 } 2446 return (err); 2447} 2448 2449static moduledata_t new_tcp_fastpaths = { 2450 .name = "tcp_fastpaths", 2451 .evhand = tcp_addfastpaths, 2452 .priv = 0 2453}; 2454 2455MODULE_VERSION(kern_tcpfastpaths, 1); 2456DECLARE_MODULE(kern_tcpfastpaths, new_tcp_fastpaths, SI_SUB_PSEUDO, SI_ORDER_ANY);
|