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