1 /*- 2 * Copyright (c) 2016-2020 Netflix, Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 */ 26 /* 27 * Author: Randall Stewart <rrs@netflix.com> 28 * This work is based on the ACM Queue paper 29 * BBR - Congestion Based Congestion Control 30 * and also numerous discussions with Neal, Yuchung and Van. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "opt_inet.h" 37 #include "opt_inet6.h" 38 #include "opt_ipsec.h" 39 #include "opt_ratelimit.h" 40 #include <sys/param.h> 41 #include <sys/arb.h> 42 #include <sys/module.h> 43 #include <sys/kernel.h> 44 #ifdef TCP_HHOOK 45 #include <sys/hhook.h> 46 #endif 47 #include <sys/malloc.h> 48 #include <sys/mbuf.h> 49 #include <sys/proc.h> 50 #include <sys/qmath.h> 51 #include <sys/socket.h> 52 #include <sys/socketvar.h> 53 #include <sys/sysctl.h> 54 #include <sys/systm.h> 55 #include <sys/tree.h> 56 #ifdef NETFLIX_STATS 57 #include <sys/stats.h> /* Must come after qmath.h and tree.h */ 58 #endif 59 #include <sys/refcount.h> 60 #include <sys/queue.h> 61 #include <sys/smp.h> 62 #include <sys/kthread.h> 63 #include <sys/lock.h> 64 #include <sys/mutex.h> 65 #include <sys/tim_filter.h> 66 #include <sys/time.h> 67 #include <vm/uma.h> 68 #include <sys/kern_prefetch.h> 69 70 #include <net/route.h> 71 #include <net/vnet.h> 72 #include <net/ethernet.h> 73 #include <net/bpf.h> 74 75 #define TCPSTATES /* for logging */ 76 77 #include <netinet/in.h> 78 #include <netinet/in_kdtrace.h> 79 #include <netinet/in_pcb.h> 80 #include <netinet/ip.h> 81 #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 82 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 83 #include <netinet/ip_var.h> 84 #include <netinet/ip6.h> 85 #include <netinet6/in6_pcb.h> 86 #include <netinet6/ip6_var.h> 87 #include <netinet/tcp.h> 88 #include <netinet/tcp_fsm.h> 89 #include <netinet/tcp_seq.h> 90 #include <netinet/tcp_timer.h> 91 #include <netinet/tcp_var.h> 92 #include <netinet/tcpip.h> 93 #include <netinet/tcp_ecn.h> 94 #include <netinet/tcp_hpts.h> 95 #include <netinet/tcp_lro.h> 96 #include <netinet/cc/cc.h> 97 #include <netinet/tcp_log_buf.h> 98 #ifdef TCP_OFFLOAD 99 #include <netinet/tcp_offload.h> 100 #endif 101 #ifdef INET6 102 #include <netinet6/tcp6_var.h> 103 #endif 104 #include <netinet/tcp_fastopen.h> 105 106 #include <netipsec/ipsec_support.h> 107 #include <net/if.h> 108 #include <net/if_var.h> 109 #include <net/if_private.h> 110 111 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 112 #include <netipsec/ipsec.h> 113 #include <netipsec/ipsec6.h> 114 #endif /* IPSEC */ 115 116 #include <netinet/udp.h> 117 #include <netinet/udp_var.h> 118 #include <machine/in_cksum.h> 119 120 #ifdef MAC 121 #include <security/mac/mac_framework.h> 122 #endif 123 #include "rack_bbr_common.h" 124 125 /* 126 * Common TCP Functions - These are shared by borth 127 * rack and BBR. 128 */ 129 static int 130 ctf_get_enet_type(struct ifnet *ifp, struct mbuf *m) 131 { 132 struct ether_header *eh; 133 #ifdef INET6 134 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */ 135 #endif 136 #ifdef INET 137 struct ip *ip = NULL; /* Keep compiler happy. */ 138 #endif 139 #if defined(INET) || defined(INET6) 140 struct tcphdr *th; 141 int32_t tlen; 142 uint16_t drop_hdrlen; 143 #endif 144 uint16_t etype; 145 #ifdef INET 146 uint8_t iptos; 147 #endif 148 149 /* Is it the easy way? */ 150 if (m->m_flags & M_LRO_EHDRSTRP) 151 return (m->m_pkthdr.lro_etype); 152 /* 153 * Ok this is the old style call, the ethernet header is here. 154 * This also means no checksum or BPF were done. This 155 * can happen if the race to setup the inp fails and 156 * LRO sees no INP at packet input, but by the time 157 * we queue the packets an INP gets there. Its rare 158 * but it can occur so we will handle it. Note that 159 * this means duplicated work but with the rarity of it 160 * its not worth worrying about. 161 */ 162 /* Let the BPF see the packet */ 163 if (bpf_peers_present(ifp->if_bpf)) 164 ETHER_BPF_MTAP(ifp, m); 165 /* Now the csum */ 166 eh = mtod(m, struct ether_header *); 167 etype = ntohs(eh->ether_type); 168 m_adj(m, sizeof(*eh)); 169 switch (etype) { 170 #ifdef INET6 171 case ETHERTYPE_IPV6: 172 { 173 if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 174 m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 175 if (m == NULL) { 176 KMOD_TCPSTAT_INC(tcps_rcvshort); 177 return (-1); 178 } 179 } 180 ip6 = (struct ip6_hdr *)(eh + 1); 181 th = (struct tcphdr *)(ip6 + 1); 182 drop_hdrlen = sizeof(*ip6); 183 tlen = ntohs(ip6->ip6_plen); 184 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 185 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 186 th->th_sum = m->m_pkthdr.csum_data; 187 else 188 th->th_sum = in6_cksum_pseudo(ip6, tlen, 189 IPPROTO_TCP, 190 m->m_pkthdr.csum_data); 191 th->th_sum ^= 0xffff; 192 } else 193 th->th_sum = in6_cksum(m, IPPROTO_TCP, drop_hdrlen, tlen); 194 if (th->th_sum) { 195 KMOD_TCPSTAT_INC(tcps_rcvbadsum); 196 m_freem(m); 197 return (-1); 198 } 199 return (etype); 200 } 201 #endif 202 #ifdef INET 203 case ETHERTYPE_IP: 204 { 205 if (m->m_len < sizeof (struct tcpiphdr)) { 206 m = m_pullup(m, sizeof (struct tcpiphdr)); 207 if (m == NULL) { 208 KMOD_TCPSTAT_INC(tcps_rcvshort); 209 return (-1); 210 } 211 } 212 ip = (struct ip *)(eh + 1); 213 th = (struct tcphdr *)(ip + 1); 214 drop_hdrlen = sizeof(*ip); 215 iptos = ip->ip_tos; 216 tlen = ntohs(ip->ip_len) - sizeof(struct ip); 217 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 218 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 219 th->th_sum = m->m_pkthdr.csum_data; 220 else 221 th->th_sum = in_pseudo(ip->ip_src.s_addr, 222 ip->ip_dst.s_addr, 223 htonl(m->m_pkthdr.csum_data + tlen + IPPROTO_TCP)); 224 th->th_sum ^= 0xffff; 225 } else { 226 int len; 227 struct ipovly *ipov = (struct ipovly *)ip; 228 /* 229 * Checksum extended TCP header and data. 230 */ 231 len = drop_hdrlen + tlen; 232 bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 233 ipov->ih_len = htons(tlen); 234 th->th_sum = in_cksum(m, len); 235 /* Reset length for SDT probes. */ 236 ip->ip_len = htons(len); 237 /* Reset TOS bits */ 238 ip->ip_tos = iptos; 239 /* Re-initialization for later version check */ 240 ip->ip_v = IPVERSION; 241 ip->ip_hl = sizeof(*ip) >> 2; 242 } 243 if (th->th_sum) { 244 KMOD_TCPSTAT_INC(tcps_rcvbadsum); 245 m_freem(m); 246 return (-1); 247 } 248 break; 249 } 250 #endif 251 }; 252 return (etype); 253 } 254 255 /* 256 * The function ctf_process_inbound_raw() is used by 257 * transport developers to do the steps needed to 258 * support MBUF Queuing i.e. the flags in 259 * inp->inp_flags2: 260 * 261 * - INP_SUPPORTS_MBUFQ 262 * - INP_MBUF_QUEUE_READY 263 * - INP_DONT_SACK_QUEUE 264 * - INP_MBUF_ACKCMP 265 * 266 * These flags help control how LRO will deliver 267 * packets to the transport. You first set in inp_flags2 268 * the INP_SUPPORTS_MBUFQ to tell the LRO code that you 269 * will gladly take a queue of packets instead of a compressed 270 * single packet. You also set in your t_fb pointer the 271 * tfb_do_queued_segments to point to ctf_process_inbound_raw. 272 * 273 * This then gets you lists of inbound ACK's/Data instead 274 * of a condensed compressed ACK/DATA packet. Why would you 275 * want that? This will get you access to all the arrival 276 * times of at least LRO and possibly at the Hardware (if 277 * the interface card supports that) of the actual ACK/DATA. 278 * In some transport designs this is important since knowing 279 * the actual time we got the packet is useful information. 280 * 281 * A new special type of mbuf may also be supported by the transport 282 * if it has set the INP_MBUF_ACKCMP flag. If its set, LRO will 283 * possibly create a M_ACKCMP type mbuf. This is a mbuf with 284 * an array of "acks". One thing also to note is that when this 285 * occurs a subsequent LRO may find at the back of the untouched 286 * mbuf queue chain a M_ACKCMP and append on to it. This means 287 * that until the transport pulls in the mbuf chain queued 288 * for it more ack's may get on the mbufs that were already 289 * delivered. There currently is a limit of 6 acks condensed 290 * into 1 mbuf which means often when this is occuring, we 291 * don't get that effect but it does happen. 292 * 293 * Now there are some interesting Caveats that the transport 294 * designer needs to take into account when using this feature. 295 * 296 * 1) It is used with HPTS and pacing, when the pacing timer 297 * for output calls it will first call the input. 298 * 2) When you set INP_MBUF_QUEUE_READY this tells LRO 299 * queue normal packets, I am busy pacing out data and 300 * will process the queued packets before my tfb_tcp_output 301 * call from pacing. If a non-normal packet arrives, (e.g. sack) 302 * you will be awoken immediately. 303 * 3) Finally you can add the INP_DONT_SACK_QUEUE to not even 304 * be awoken if a SACK has arrived. You would do this when 305 * you were not only running a pacing for output timer 306 * but a Rack timer as well i.e. you know you are in recovery 307 * and are in the process (via the timers) of dealing with 308 * the loss. 309 * 310 * Now a critical thing you must be aware of here is that the 311 * use of the flags has a far greater scope then just your 312 * typical LRO. Why? Well thats because in the normal compressed 313 * LRO case at the end of a driver interupt all packets are going 314 * to get presented to the transport no matter if there is one 315 * or 100. With the MBUF_QUEUE model, this is not true. You will 316 * only be awoken to process the queue of packets when: 317 * a) The flags discussed above allow it. 318 * <or> 319 * b) You exceed a ack or data limit (by default the 320 * ack limit is infinity (64k acks) and the data 321 * limit is 64k of new TCP data) 322 * <or> 323 * c) The push bit has been set by the peer 324 */ 325 326 static int 327 ctf_process_inbound_raw(struct tcpcb *tp, struct mbuf *m, int has_pkt) 328 { 329 /* 330 * We are passed a raw change of mbuf packets 331 * that arrived in LRO. They are linked via 332 * the m_nextpkt link in the pkt-headers. 333 * 334 * We process each one by: 335 * a) saving off the next 336 * b) stripping off the ether-header 337 * c) formulating the arguments for tfb_do_segment_nounlock() 338 * d) calling each mbuf to tfb_do_segment_nounlock() 339 * after adjusting the time to match the arrival time. 340 * Note that the LRO code assures no IP options are present. 341 * 342 * The symantics for calling tfb_do_segment_nounlock() are the 343 * following: 344 * 1) It returns 0 if all went well and you (the caller) need 345 * to release the lock. 346 * 2) If nxt_pkt is set, then the function will surpress calls 347 * to tcp_output() since you are promising to call again 348 * with another packet. 349 * 3) If it returns 1, then you must free all the packets being 350 * shipped in, the tcb has been destroyed (or about to be destroyed). 351 */ 352 struct mbuf *m_save; 353 struct tcphdr *th; 354 #ifdef INET6 355 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */ 356 #endif 357 #ifdef INET 358 struct ip *ip = NULL; /* Keep compiler happy. */ 359 #endif 360 struct ifnet *ifp; 361 struct timeval tv; 362 struct inpcb *inp __diagused; 363 int32_t retval, nxt_pkt, tlen, off; 364 int etype = 0; 365 uint16_t drop_hdrlen; 366 uint8_t iptos, no_vn=0; 367 368 inp = tptoinpcb(tp); 369 INP_WLOCK_ASSERT(inp); 370 NET_EPOCH_ASSERT(); 371 372 if (m) 373 ifp = m_rcvif(m); 374 else 375 ifp = NULL; 376 if (ifp == NULL) { 377 /* 378 * We probably should not work around 379 * but kassert, since lro alwasy sets rcvif. 380 */ 381 no_vn = 1; 382 goto skip_vnet; 383 } 384 CURVNET_SET(ifp->if_vnet); 385 skip_vnet: 386 tcp_get_usecs(&tv); 387 while (m) { 388 m_save = m->m_nextpkt; 389 m->m_nextpkt = NULL; 390 if ((m->m_flags & M_ACKCMP) == 0) { 391 /* Now lets get the ether header */ 392 etype = ctf_get_enet_type(ifp, m); 393 if (etype == -1) { 394 /* Skip this packet it was freed by checksum */ 395 goto skipped_pkt; 396 } 397 KASSERT(((etype == ETHERTYPE_IPV6) || (etype == ETHERTYPE_IP)), 398 ("tp:%p m:%p etype:0x%x -- not IP or IPv6", tp, m, etype)); 399 /* Trim off the ethernet header */ 400 switch (etype) { 401 #ifdef INET6 402 case ETHERTYPE_IPV6: 403 ip6 = mtod(m, struct ip6_hdr *); 404 th = (struct tcphdr *)(ip6 + 1); 405 tlen = ntohs(ip6->ip6_plen); 406 drop_hdrlen = sizeof(*ip6); 407 iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 408 break; 409 #endif 410 #ifdef INET 411 case ETHERTYPE_IP: 412 ip = mtod(m, struct ip *); 413 th = (struct tcphdr *)(ip + 1); 414 drop_hdrlen = sizeof(*ip); 415 iptos = ip->ip_tos; 416 tlen = ntohs(ip->ip_len) - sizeof(struct ip); 417 break; 418 #endif 419 } /* end switch */ 420 /* 421 * Convert TCP protocol specific fields to host format. 422 */ 423 tcp_fields_to_host(th); 424 off = th->th_off << 2; 425 if (off < sizeof (struct tcphdr) || off > tlen) { 426 printf("off:%d < hdrlen:%zu || > tlen:%u -- dump\n", 427 off, 428 sizeof(struct tcphdr), 429 tlen); 430 KMOD_TCPSTAT_INC(tcps_rcvbadoff); 431 m_freem(m); 432 goto skipped_pkt; 433 } 434 tlen -= off; 435 drop_hdrlen += off; 436 /* 437 * Now lets setup the timeval to be when we should 438 * have been called (if we can). 439 */ 440 m->m_pkthdr.lro_nsegs = 1; 441 /* Now what about next packet? */ 442 } else { 443 /* 444 * This mbuf is an array of acks that have 445 * been compressed. We assert the inp has 446 * the flag set to enable this! 447 */ 448 KASSERT((inp->inp_flags2 & INP_MBUF_ACKCMP), 449 ("tp:%p inp:%p no INP_MBUF_ACKCMP flags?", tp, inp)); 450 tlen = 0; 451 drop_hdrlen = 0; 452 th = NULL; 453 iptos = 0; 454 } 455 tcp_get_usecs(&tv); 456 if (m_save || has_pkt) 457 nxt_pkt = 1; 458 else 459 nxt_pkt = 0; 460 if ((m->m_flags & M_ACKCMP) == 0) 461 KMOD_TCPSTAT_INC(tcps_rcvtotal); 462 else 463 KMOD_TCPSTAT_ADD(tcps_rcvtotal, (m->m_len / sizeof(struct tcp_ackent))); 464 retval = (*tp->t_fb->tfb_do_segment_nounlock)(tp, m, th, 465 drop_hdrlen, tlen, iptos, nxt_pkt, &tv); 466 if (retval) { 467 /* We lost the lock and tcb probably */ 468 m = m_save; 469 while(m) { 470 m_save = m->m_nextpkt; 471 m->m_nextpkt = NULL; 472 m_freem(m); 473 m = m_save; 474 } 475 if (no_vn == 0) { 476 CURVNET_RESTORE(); 477 } 478 INP_UNLOCK_ASSERT(inp); 479 return(retval); 480 } 481 skipped_pkt: 482 m = m_save; 483 } 484 if (no_vn == 0) { 485 CURVNET_RESTORE(); 486 } 487 return(retval); 488 } 489 490 int 491 ctf_do_queued_segments(struct tcpcb *tp, int have_pkt) 492 { 493 struct mbuf *m; 494 495 /* First lets see if we have old packets */ 496 if (tp->t_in_pkt) { 497 m = tp->t_in_pkt; 498 tp->t_in_pkt = NULL; 499 tp->t_tail_pkt = NULL; 500 if (ctf_process_inbound_raw(tp, m, have_pkt)) { 501 /* We lost the tcpcb (maybe a RST came in)? */ 502 return(1); 503 } 504 } 505 return (0); 506 } 507 508 uint32_t 509 ctf_outstanding(struct tcpcb *tp) 510 { 511 uint32_t bytes_out; 512 513 bytes_out = tp->snd_max - tp->snd_una; 514 if (tp->t_state < TCPS_ESTABLISHED) 515 bytes_out++; 516 if (tp->t_flags & TF_SENTFIN) 517 bytes_out++; 518 return (bytes_out); 519 } 520 521 uint32_t 522 ctf_flight_size(struct tcpcb *tp, uint32_t rc_sacked) 523 { 524 if (rc_sacked <= ctf_outstanding(tp)) 525 return(ctf_outstanding(tp) - rc_sacked); 526 else { 527 return (0); 528 } 529 } 530 531 void 532 ctf_do_dropwithreset(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th, 533 int32_t rstreason, int32_t tlen) 534 { 535 if (tp != NULL) { 536 tcp_dropwithreset(m, th, tp, tlen, rstreason); 537 INP_WUNLOCK(tptoinpcb(tp)); 538 } else 539 tcp_dropwithreset(m, th, NULL, tlen, rstreason); 540 } 541 542 void 543 ctf_ack_war_checks(struct tcpcb *tp, uint32_t *ts, uint32_t *cnt) 544 { 545 if ((ts != NULL) && (cnt != NULL) && 546 (tcp_ack_war_time_window > 0) && 547 (tcp_ack_war_cnt > 0)) { 548 /* We are possibly doing ack war prevention */ 549 uint32_t cts; 550 551 /* 552 * We use a msec tick here which gives us 553 * roughly 49 days. We don't need the 554 * precision of a microsecond timestamp which 555 * would only give us hours. 556 */ 557 cts = tcp_ts_getticks(); 558 if (TSTMP_LT((*ts), cts)) { 559 /* Timestamp is in the past */ 560 *cnt = 0; 561 *ts = (cts + tcp_ack_war_time_window); 562 } 563 if (*cnt < tcp_ack_war_cnt) { 564 *cnt = (*cnt + 1); 565 tp->t_flags |= TF_ACKNOW; 566 } else 567 tp->t_flags &= ~TF_ACKNOW; 568 } else 569 tp->t_flags |= TF_ACKNOW; 570 } 571 572 /* 573 * ctf_drop_checks returns 1 for you should not proceed. It places 574 * in ret_val what should be returned 1/0 by the caller. The 1 indicates 575 * that the TCB is unlocked and probably dropped. The 0 indicates the 576 * TCB is still valid and locked. 577 */ 578 int 579 _ctf_drop_checks(struct tcpopt *to, struct mbuf *m, struct tcphdr *th, 580 struct tcpcb *tp, int32_t *tlenp, 581 int32_t *thf, int32_t *drop_hdrlen, int32_t *ret_val, 582 uint32_t *ts, uint32_t *cnt) 583 { 584 int32_t todrop; 585 int32_t thflags; 586 int32_t tlen; 587 588 thflags = *thf; 589 tlen = *tlenp; 590 todrop = tp->rcv_nxt - th->th_seq; 591 if (todrop > 0) { 592 if (thflags & TH_SYN) { 593 thflags &= ~TH_SYN; 594 th->th_seq++; 595 if (th->th_urp > 1) 596 th->th_urp--; 597 else 598 thflags &= ~TH_URG; 599 todrop--; 600 } 601 /* 602 * Following if statement from Stevens, vol. 2, p. 960. 603 */ 604 if (todrop > tlen 605 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 606 /* 607 * Any valid FIN must be to the left of the window. 608 * At this point the FIN must be a duplicate or out 609 * of sequence; drop it. 610 */ 611 thflags &= ~TH_FIN; 612 /* 613 * Send an ACK to resynchronize and drop any data. 614 * But keep on processing for RST or ACK. 615 */ 616 ctf_ack_war_checks(tp, ts, cnt); 617 todrop = tlen; 618 KMOD_TCPSTAT_INC(tcps_rcvduppack); 619 KMOD_TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 620 } else { 621 KMOD_TCPSTAT_INC(tcps_rcvpartduppack); 622 KMOD_TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 623 } 624 /* 625 * DSACK - add SACK block for dropped range 626 */ 627 if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) { 628 /* 629 * ACK now, as the next in-sequence segment 630 * will clear the DSACK block again 631 */ 632 ctf_ack_war_checks(tp, ts, cnt); 633 if (tp->t_flags & TF_ACKNOW) 634 tcp_update_sack_list(tp, th->th_seq, 635 th->th_seq + todrop); 636 } 637 *drop_hdrlen += todrop; /* drop from the top afterwards */ 638 th->th_seq += todrop; 639 tlen -= todrop; 640 if (th->th_urp > todrop) 641 th->th_urp -= todrop; 642 else { 643 thflags &= ~TH_URG; 644 th->th_urp = 0; 645 } 646 } 647 /* 648 * If segment ends after window, drop trailing data (and PUSH and 649 * FIN); if nothing left, just ACK. 650 */ 651 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 652 if (todrop > 0) { 653 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin); 654 if (todrop >= tlen) { 655 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 656 /* 657 * If window is closed can only take segments at 658 * window edge, and have to drop data and PUSH from 659 * incoming segments. Continue processing, but 660 * remember to ack. Otherwise, drop segment and 661 * ack. 662 */ 663 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 664 ctf_ack_war_checks(tp, ts, cnt); 665 KMOD_TCPSTAT_INC(tcps_rcvwinprobe); 666 } else { 667 __ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val, ts, cnt); 668 return (1); 669 } 670 } else 671 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 672 m_adj(m, -todrop); 673 tlen -= todrop; 674 thflags &= ~(TH_PUSH | TH_FIN); 675 } 676 *thf = thflags; 677 *tlenp = tlen; 678 return (0); 679 } 680 681 /* 682 * The value in ret_val informs the caller 683 * if we dropped the tcb (and lock) or not. 684 * 1 = we dropped it, 0 = the TCB is still locked 685 * and valid. 686 */ 687 void 688 __ctf_do_dropafterack(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th, int32_t thflags, int32_t tlen, int32_t *ret_val, uint32_t *ts, uint32_t *cnt) 689 { 690 /* 691 * Generate an ACK dropping incoming segment if it occupies sequence 692 * space, where the ACK reflects our state. 693 * 694 * We can now skip the test for the RST flag since all paths to this 695 * code happen after packets containing RST have been dropped. 696 * 697 * In the SYN-RECEIVED state, don't send an ACK unless the segment 698 * we received passes the SYN-RECEIVED ACK test. If it fails send a 699 * RST. This breaks the loop in the "LAND" DoS attack, and also 700 * prevents an ACK storm between two listening ports that have been 701 * sent forged SYN segments, each with the source address of the 702 * other. 703 */ 704 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 705 (SEQ_GT(tp->snd_una, th->th_ack) || 706 SEQ_GT(th->th_ack, tp->snd_max))) { 707 *ret_val = 1; 708 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 709 return; 710 } else 711 *ret_val = 0; 712 ctf_ack_war_checks(tp, ts, cnt); 713 if (m) 714 m_freem(m); 715 } 716 717 void 718 ctf_do_drop(struct mbuf *m, struct tcpcb *tp) 719 { 720 721 /* 722 * Drop space held by incoming segment and return. 723 */ 724 if (tp != NULL) 725 INP_WUNLOCK(tptoinpcb(tp)); 726 if (m) 727 m_freem(m); 728 } 729 730 int 731 __ctf_process_rst(struct mbuf *m, struct tcphdr *th, struct socket *so, 732 struct tcpcb *tp, uint32_t *ts, uint32_t *cnt) 733 { 734 /* 735 * RFC5961 Section 3.2 736 * 737 * - RST drops connection only if SEG.SEQ == RCV.NXT. - If RST is in 738 * window, we send challenge ACK. 739 * 740 * Note: to take into account delayed ACKs, we should test against 741 * last_ack_sent instead of rcv_nxt. Note 2: we handle special case 742 * of closed window, not covered by the RFC. 743 */ 744 int dropped = 0; 745 746 if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 747 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 748 (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 749 KASSERT(tp->t_state != TCPS_SYN_SENT, 750 ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 751 __func__, th, tp)); 752 753 if (V_tcp_insecure_rst || 754 (tp->last_ack_sent == th->th_seq) || 755 (tp->rcv_nxt == th->th_seq)) { 756 KMOD_TCPSTAT_INC(tcps_drops); 757 /* Drop the connection. */ 758 switch (tp->t_state) { 759 case TCPS_SYN_RECEIVED: 760 so->so_error = ECONNREFUSED; 761 goto close; 762 case TCPS_ESTABLISHED: 763 case TCPS_FIN_WAIT_1: 764 case TCPS_FIN_WAIT_2: 765 case TCPS_CLOSE_WAIT: 766 case TCPS_CLOSING: 767 case TCPS_LAST_ACK: 768 so->so_error = ECONNRESET; 769 close: 770 tcp_state_change(tp, TCPS_CLOSED); 771 /* FALLTHROUGH */ 772 default: 773 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_RST); 774 tp = tcp_close(tp); 775 } 776 dropped = 1; 777 ctf_do_drop(m, tp); 778 } else { 779 int send_challenge; 780 781 KMOD_TCPSTAT_INC(tcps_badrst); 782 if ((ts != NULL) && (cnt != NULL) && 783 (tcp_ack_war_time_window > 0) && 784 (tcp_ack_war_cnt > 0)) { 785 /* We are possibly preventing an ack-rst war prevention */ 786 uint32_t cts; 787 788 /* 789 * We use a msec tick here which gives us 790 * roughly 49 days. We don't need the 791 * precision of a microsecond timestamp which 792 * would only give us hours. 793 */ 794 cts = tcp_ts_getticks(); 795 if (TSTMP_LT((*ts), cts)) { 796 /* Timestamp is in the past */ 797 *cnt = 0; 798 *ts = (cts + tcp_ack_war_time_window); 799 } 800 if (*cnt < tcp_ack_war_cnt) { 801 *cnt = (*cnt + 1); 802 send_challenge = 1; 803 } else 804 send_challenge = 0; 805 } else 806 send_challenge = 1; 807 if (send_challenge) { 808 /* Send challenge ACK. */ 809 tcp_respond(tp, mtod(m, void *), th, m, 810 tp->rcv_nxt, tp->snd_nxt, TH_ACK); 811 tp->last_ack_sent = tp->rcv_nxt; 812 } 813 } 814 } else { 815 m_freem(m); 816 } 817 return (dropped); 818 } 819 820 /* 821 * The value in ret_val informs the caller 822 * if we dropped the tcb (and lock) or not. 823 * 1 = we dropped it, 0 = the TCB is still locked 824 * and valid. 825 */ 826 void 827 ctf_challenge_ack(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, uint8_t iptos, int32_t * ret_val) 828 { 829 830 NET_EPOCH_ASSERT(); 831 832 KMOD_TCPSTAT_INC(tcps_badsyn); 833 if (V_tcp_insecure_syn && 834 SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 835 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 836 tp = tcp_drop(tp, ECONNRESET); 837 *ret_val = 1; 838 ctf_do_drop(m, tp); 839 } else { 840 tcp_ecn_input_syn_sent(tp, tcp_get_flags(th), iptos); 841 /* Send challenge ACK. */ 842 tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 843 tp->snd_nxt, TH_ACK); 844 tp->last_ack_sent = tp->rcv_nxt; 845 m = NULL; 846 *ret_val = 0; 847 ctf_do_drop(m, NULL); 848 } 849 } 850 851 /* 852 * ctf_ts_check returns 1 for you should not proceed, the state 853 * machine should return. It places in ret_val what should 854 * be returned 1/0 by the caller (hpts_do_segment). The 1 indicates 855 * that the TCB is unlocked and probably dropped. The 0 indicates the 856 * TCB is still valid and locked. 857 */ 858 int 859 ctf_ts_check(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 860 int32_t tlen, int32_t thflags, int32_t * ret_val) 861 { 862 863 if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 864 /* 865 * Invalidate ts_recent. If this segment updates ts_recent, 866 * the age will be reset later and ts_recent will get a 867 * valid value. If it does not, setting ts_recent to zero 868 * will at least satisfy the requirement that zero be placed 869 * in the timestamp echo reply when ts_recent isn't valid. 870 * The age isn't reset until we get a valid ts_recent 871 * because we don't want out-of-order segments to be dropped 872 * when ts_recent is old. 873 */ 874 tp->ts_recent = 0; 875 } else { 876 KMOD_TCPSTAT_INC(tcps_rcvduppack); 877 KMOD_TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 878 KMOD_TCPSTAT_INC(tcps_pawsdrop); 879 *ret_val = 0; 880 if (tlen) { 881 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 882 } else { 883 ctf_do_drop(m, NULL); 884 } 885 return (1); 886 } 887 return (0); 888 } 889 890 int 891 ctf_ts_check_ac(struct tcpcb *tp, int32_t thflags) 892 { 893 894 if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 895 /* 896 * Invalidate ts_recent. If this segment updates ts_recent, 897 * the age will be reset later and ts_recent will get a 898 * valid value. If it does not, setting ts_recent to zero 899 * will at least satisfy the requirement that zero be placed 900 * in the timestamp echo reply when ts_recent isn't valid. 901 * The age isn't reset until we get a valid ts_recent 902 * because we don't want out-of-order segments to be dropped 903 * when ts_recent is old. 904 */ 905 tp->ts_recent = 0; 906 } else { 907 KMOD_TCPSTAT_INC(tcps_rcvduppack); 908 KMOD_TCPSTAT_INC(tcps_pawsdrop); 909 return (1); 910 } 911 return (0); 912 } 913 914 915 916 void 917 ctf_calc_rwin(struct socket *so, struct tcpcb *tp) 918 { 919 int32_t win; 920 921 /* 922 * Calculate amount of space in receive window, and then do TCP 923 * input processing. Receive window is amount of space in rcv queue, 924 * but not less than advertised window. 925 */ 926 win = sbspace(&so->so_rcv); 927 if (win < 0) 928 win = 0; 929 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 930 } 931 932 void 933 ctf_do_dropwithreset_conn(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th, 934 int32_t rstreason, int32_t tlen) 935 { 936 937 tcp_dropwithreset(m, th, tp, tlen, rstreason); 938 tp = tcp_drop(tp, ETIMEDOUT); 939 if (tp) 940 INP_WUNLOCK(tptoinpcb(tp)); 941 } 942 943 uint32_t 944 ctf_fixed_maxseg(struct tcpcb *tp) 945 { 946 return (tcp_fixed_maxseg(tp)); 947 } 948 949 void 950 ctf_log_sack_filter(struct tcpcb *tp, int num_sack_blks, struct sackblk *sack_blocks) 951 { 952 if (tcp_bblogging_on(tp)) { 953 union tcp_log_stackspecific log; 954 struct timeval tv; 955 956 memset(&log, 0, sizeof(log)); 957 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 958 log.u_bbr.flex8 = num_sack_blks; 959 if (num_sack_blks > 0) { 960 log.u_bbr.flex1 = sack_blocks[0].start; 961 log.u_bbr.flex2 = sack_blocks[0].end; 962 } 963 if (num_sack_blks > 1) { 964 log.u_bbr.flex3 = sack_blocks[1].start; 965 log.u_bbr.flex4 = sack_blocks[1].end; 966 } 967 if (num_sack_blks > 2) { 968 log.u_bbr.flex5 = sack_blocks[2].start; 969 log.u_bbr.flex6 = sack_blocks[2].end; 970 } 971 if (num_sack_blks > 3) { 972 log.u_bbr.applimited = sack_blocks[3].start; 973 log.u_bbr.pkts_out = sack_blocks[3].end; 974 } 975 TCP_LOG_EVENTP(tp, NULL, 976 &tptosocket(tp)->so_rcv, 977 &tptosocket(tp)->so_snd, 978 TCP_SACK_FILTER_RES, 0, 979 0, &log, false, &tv); 980 } 981 } 982 983 uint32_t 984 ctf_decay_count(uint32_t count, uint32_t decay) 985 { 986 /* 987 * Given a count, decay it by a set percentage. The 988 * percentage is in thousands i.e. 100% = 1000, 989 * 19.3% = 193. 990 */ 991 uint64_t perc_count, decay_per; 992 uint32_t decayed_count; 993 if (decay > 1000) { 994 /* We don't raise it */ 995 return (count); 996 } 997 perc_count = count; 998 decay_per = decay; 999 perc_count *= decay_per; 1000 perc_count /= 1000; 1001 /* 1002 * So now perc_count holds the 1003 * count decay value. 1004 */ 1005 decayed_count = count - (uint32_t)perc_count; 1006 return(decayed_count); 1007 } 1008 1009 int32_t 1010 ctf_progress_timeout_check(struct tcpcb *tp, bool log) 1011 { 1012 if (tp->t_maxunacktime && tp->t_acktime && TSTMP_GT(ticks, tp->t_acktime)) { 1013 if ((ticks - tp->t_acktime) >= tp->t_maxunacktime) { 1014 /* 1015 * There is an assumption that the caller 1016 * will drop the connection so we will 1017 * increment the counters here. 1018 */ 1019 if (log) 1020 tcp_log_end_status(tp, TCP_EI_STATUS_PROGRESS); 1021 #ifdef NETFLIX_STATS 1022 KMOD_TCPSTAT_INC(tcps_progdrops); 1023 #endif 1024 return (1); 1025 } 1026 } 1027 return (0); 1028 } 1029