13b0b41e6SRandall Stewart /*- 2963fb2adSRandall Stewart * Copyright (c) 2016-2020 Netflix, Inc. 33b0b41e6SRandall Stewart * 43b0b41e6SRandall Stewart * Redistribution and use in source and binary forms, with or without 53b0b41e6SRandall Stewart * modification, are permitted provided that the following conditions 63b0b41e6SRandall Stewart * are met: 73b0b41e6SRandall Stewart * 1. Redistributions of source code must retain the above copyright 83b0b41e6SRandall Stewart * notice, this list of conditions and the following disclaimer. 93b0b41e6SRandall Stewart * 2. Redistributions in binary form must reproduce the above copyright 103b0b41e6SRandall Stewart * notice, this list of conditions and the following disclaimer in the 113b0b41e6SRandall Stewart * documentation and/or other materials provided with the distribution. 123b0b41e6SRandall Stewart * 133b0b41e6SRandall Stewart * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 143b0b41e6SRandall Stewart * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 153b0b41e6SRandall Stewart * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 163b0b41e6SRandall Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 173b0b41e6SRandall Stewart * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 183b0b41e6SRandall Stewart * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 193b0b41e6SRandall Stewart * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 203b0b41e6SRandall Stewart * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 213b0b41e6SRandall Stewart * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 223b0b41e6SRandall Stewart * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 233b0b41e6SRandall Stewart * SUCH DAMAGE. 243b0b41e6SRandall Stewart * 253b0b41e6SRandall Stewart */ 263b0b41e6SRandall Stewart /* 273b0b41e6SRandall Stewart * Author: Randall Stewart <rrs@netflix.com> 283b0b41e6SRandall Stewart * This work is based on the ACM Queue paper 293b0b41e6SRandall Stewart * BBR - Congestion Based Congestion Control 303b0b41e6SRandall Stewart * and also numerous discussions with Neal, Yuchung and Van. 313b0b41e6SRandall Stewart */ 323b0b41e6SRandall Stewart 333b0b41e6SRandall Stewart #include <sys/cdefs.h> 343b0b41e6SRandall Stewart __FBSDID("$FreeBSD$"); 353b0b41e6SRandall Stewart 363b0b41e6SRandall Stewart #include "opt_inet.h" 373b0b41e6SRandall Stewart #include "opt_inet6.h" 383b0b41e6SRandall Stewart #include "opt_ipsec.h" 393b0b41e6SRandall Stewart #include "opt_ratelimit.h" 4035c7bb34SRandall Stewart #include "opt_kern_tls.h" 413b0b41e6SRandall Stewart #include <sys/param.h> 421cf55767SRandall Stewart #include <sys/arb.h> 433b0b41e6SRandall Stewart #include <sys/module.h> 443b0b41e6SRandall Stewart #include <sys/kernel.h> 453b0b41e6SRandall Stewart #ifdef TCP_HHOOK 463b0b41e6SRandall Stewart #include <sys/hhook.h> 473b0b41e6SRandall Stewart #endif 483b0b41e6SRandall Stewart #include <sys/malloc.h> 493b0b41e6SRandall Stewart #include <sys/mbuf.h> 503b0b41e6SRandall Stewart #include <sys/proc.h> 5135c7bb34SRandall Stewart #include <sys/qmath.h> 523b0b41e6SRandall Stewart #include <sys/socket.h> 533b0b41e6SRandall Stewart #include <sys/socketvar.h> 543b0b41e6SRandall Stewart #ifdef KERN_TLS 5535c7bb34SRandall Stewart #include <sys/ktls.h> 563b0b41e6SRandall Stewart #endif 573b0b41e6SRandall Stewart #include <sys/sysctl.h> 583b0b41e6SRandall Stewart #include <sys/systm.h> 593b0b41e6SRandall Stewart #include <sys/tree.h> 6035c7bb34SRandall Stewart #ifdef NETFLIX_STATS 6135c7bb34SRandall Stewart #include <sys/stats.h> /* Must come after qmath.h and tree.h */ 6235c7bb34SRandall Stewart #endif 633b0b41e6SRandall Stewart #include <sys/refcount.h> 643b0b41e6SRandall Stewart #include <sys/queue.h> 653b0b41e6SRandall Stewart #include <sys/smp.h> 663b0b41e6SRandall Stewart #include <sys/kthread.h> 673b0b41e6SRandall Stewart #include <sys/lock.h> 683b0b41e6SRandall Stewart #include <sys/mutex.h> 6935c7bb34SRandall Stewart #include <sys/tim_filter.h> 703b0b41e6SRandall Stewart #include <sys/time.h> 713b0b41e6SRandall Stewart #include <vm/uma.h> 723b0b41e6SRandall Stewart #include <sys/kern_prefetch.h> 733b0b41e6SRandall Stewart 743b0b41e6SRandall Stewart #include <net/route.h> 753b0b41e6SRandall Stewart #include <net/vnet.h> 763b0b41e6SRandall Stewart #include <net/ethernet.h> 773b0b41e6SRandall Stewart #include <net/bpf.h> 783b0b41e6SRandall Stewart 793b0b41e6SRandall Stewart #define TCPSTATES /* for logging */ 803b0b41e6SRandall Stewart 813b0b41e6SRandall Stewart #include <netinet/in.h> 823b0b41e6SRandall Stewart #include <netinet/in_kdtrace.h> 833b0b41e6SRandall Stewart #include <netinet/in_pcb.h> 843b0b41e6SRandall Stewart #include <netinet/ip.h> 853b0b41e6SRandall Stewart #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 863b0b41e6SRandall Stewart #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 873b0b41e6SRandall Stewart #include <netinet/ip_var.h> 883b0b41e6SRandall Stewart #include <netinet/ip6.h> 893b0b41e6SRandall Stewart #include <netinet6/in6_pcb.h> 903b0b41e6SRandall Stewart #include <netinet6/ip6_var.h> 913b0b41e6SRandall Stewart #include <netinet/tcp.h> 923b0b41e6SRandall Stewart #include <netinet/tcp_fsm.h> 933b0b41e6SRandall Stewart #include <netinet/tcp_seq.h> 943b0b41e6SRandall Stewart #include <netinet/tcp_timer.h> 953b0b41e6SRandall Stewart #include <netinet/tcp_var.h> 963b0b41e6SRandall Stewart #include <netinet/tcpip.h> 9783c1ec92SRichard Scheffenegger #include <netinet/tcp_ecn.h> 983b0b41e6SRandall Stewart #include <netinet/tcp_hpts.h> 995d8fd932SRandall Stewart #include <netinet/tcp_lro.h> 1003b0b41e6SRandall Stewart #include <netinet/cc/cc.h> 1013b0b41e6SRandall Stewart #include <netinet/tcp_log_buf.h> 1023b0b41e6SRandall Stewart #ifdef TCP_OFFLOAD 1033b0b41e6SRandall Stewart #include <netinet/tcp_offload.h> 1043b0b41e6SRandall Stewart #endif 1053b0b41e6SRandall Stewart #ifdef INET6 1063b0b41e6SRandall Stewart #include <netinet6/tcp6_var.h> 1073b0b41e6SRandall Stewart #endif 1083b0b41e6SRandall Stewart #include <netinet/tcp_fastopen.h> 1093b0b41e6SRandall Stewart 1103b0b41e6SRandall Stewart #include <netipsec/ipsec_support.h> 1113b0b41e6SRandall Stewart #include <net/if.h> 1123b0b41e6SRandall Stewart #include <net/if_var.h> 1133d0d5b21SJustin Hibbits #include <net/if_private.h> 1143b0b41e6SRandall Stewart 1153b0b41e6SRandall Stewart #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1163b0b41e6SRandall Stewart #include <netipsec/ipsec.h> 1173b0b41e6SRandall Stewart #include <netipsec/ipsec6.h> 1183b0b41e6SRandall Stewart #endif /* IPSEC */ 1193b0b41e6SRandall Stewart 1203b0b41e6SRandall Stewart #include <netinet/udp.h> 1213b0b41e6SRandall Stewart #include <netinet/udp_var.h> 1223b0b41e6SRandall Stewart #include <machine/in_cksum.h> 1233b0b41e6SRandall Stewart 1243b0b41e6SRandall Stewart #ifdef MAC 1253b0b41e6SRandall Stewart #include <security/mac/mac_framework.h> 1263b0b41e6SRandall Stewart #endif 1273b0b41e6SRandall Stewart #include "rack_bbr_common.h" 1283b0b41e6SRandall Stewart 1293b0b41e6SRandall Stewart /* 1303b0b41e6SRandall Stewart * Common TCP Functions - These are shared by borth 1313b0b41e6SRandall Stewart * rack and BBR. 1323b0b41e6SRandall Stewart */ 1333b0b41e6SRandall Stewart #ifdef KERN_TLS 1343b0b41e6SRandall Stewart uint32_t 1353b0b41e6SRandall Stewart ctf_get_opt_tls_size(struct socket *so, uint32_t rwnd) 1363b0b41e6SRandall Stewart { 13735c7bb34SRandall Stewart struct ktls_session *tls; 1383b0b41e6SRandall Stewart uint32_t len; 1393b0b41e6SRandall Stewart 1403b0b41e6SRandall Stewart again: 1413b0b41e6SRandall Stewart tls = so->so_snd.sb_tls_info; 14235c7bb34SRandall Stewart len = tls->params.max_frame_len; /* max tls payload */ 14335c7bb34SRandall Stewart len += tls->params.tls_hlen; /* tls header len */ 14435c7bb34SRandall Stewart len += tls->params.tls_tlen; /* tls trailer len */ 1453b0b41e6SRandall Stewart if ((len * 4) > rwnd) { 1463b0b41e6SRandall Stewart /* 1473b0b41e6SRandall Stewart * Stroke this will suck counter and what 1483b0b41e6SRandall Stewart * else should we do Drew? From the 1493b0b41e6SRandall Stewart * TCP perspective I am not sure 1503b0b41e6SRandall Stewart * what should be done... 1513b0b41e6SRandall Stewart */ 15235c7bb34SRandall Stewart if (tls->params.max_frame_len > 4096) { 15335c7bb34SRandall Stewart tls->params.max_frame_len -= 4096; 15435c7bb34SRandall Stewart if (tls->params.max_frame_len < 4096) 15535c7bb34SRandall Stewart tls->params.max_frame_len = 4096; 1563b0b41e6SRandall Stewart goto again; 1573b0b41e6SRandall Stewart } 1583b0b41e6SRandall Stewart } 1593b0b41e6SRandall Stewart return (len); 1603b0b41e6SRandall Stewart } 1613b0b41e6SRandall Stewart #endif 1623b0b41e6SRandall Stewart 1635d8fd932SRandall Stewart static int 1645d8fd932SRandall Stewart ctf_get_enet_type(struct ifnet *ifp, struct mbuf *m) 1655d8fd932SRandall Stewart { 1665d8fd932SRandall Stewart struct ether_header *eh; 1675d8fd932SRandall Stewart #ifdef INET6 1685d8fd932SRandall Stewart struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */ 1695d8fd932SRandall Stewart #endif 1705d8fd932SRandall Stewart #ifdef INET 1715d8fd932SRandall Stewart struct ip *ip = NULL; /* Keep compiler happy. */ 1725d8fd932SRandall Stewart #endif 173b1e806c0SAndrew Gallatin #if defined(INET) || defined(INET6) 174b1e806c0SAndrew Gallatin struct tcphdr *th; 1755d8fd932SRandall Stewart int32_t tlen; 1765d8fd932SRandall Stewart uint16_t drop_hdrlen; 177b1e806c0SAndrew Gallatin #endif 1785d8fd932SRandall Stewart uint16_t etype; 179b1e806c0SAndrew Gallatin #ifdef INET 1805d8fd932SRandall Stewart uint8_t iptos; 181b1e806c0SAndrew Gallatin #endif 1825d8fd932SRandall Stewart 1835d8fd932SRandall Stewart /* Is it the easy way? */ 1845d8fd932SRandall Stewart if (m->m_flags & M_LRO_EHDRSTRP) 1855d8fd932SRandall Stewart return (m->m_pkthdr.lro_etype); 1865d8fd932SRandall Stewart /* 1875d8fd932SRandall Stewart * Ok this is the old style call, the ethernet header is here. 1885d8fd932SRandall Stewart * This also means no checksum or BPF were done. This 1895d8fd932SRandall Stewart * can happen if the race to setup the inp fails and 1905d8fd932SRandall Stewart * LRO sees no INP at packet input, but by the time 1915d8fd932SRandall Stewart * we queue the packets an INP gets there. Its rare 1925d8fd932SRandall Stewart * but it can occur so we will handle it. Note that 1935d8fd932SRandall Stewart * this means duplicated work but with the rarity of it 1945d8fd932SRandall Stewart * its not worth worrying about. 1955d8fd932SRandall Stewart */ 1965d8fd932SRandall Stewart /* Let the BPF see the packet */ 1975d8fd932SRandall Stewart if (bpf_peers_present(ifp->if_bpf)) 1985d8fd932SRandall Stewart ETHER_BPF_MTAP(ifp, m); 1995d8fd932SRandall Stewart /* Now the csum */ 2005d8fd932SRandall Stewart eh = mtod(m, struct ether_header *); 2015d8fd932SRandall Stewart etype = ntohs(eh->ether_type); 2025d8fd932SRandall Stewart m_adj(m, sizeof(*eh)); 2035d8fd932SRandall Stewart switch (etype) { 2045d8fd932SRandall Stewart #ifdef INET6 2055d8fd932SRandall Stewart case ETHERTYPE_IPV6: 2065d8fd932SRandall Stewart { 2075d8fd932SRandall Stewart if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 2085d8fd932SRandall Stewart m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 2095d8fd932SRandall Stewart if (m == NULL) { 2105d8fd932SRandall Stewart KMOD_TCPSTAT_INC(tcps_rcvshort); 2115d8fd932SRandall Stewart return (-1); 2125d8fd932SRandall Stewart } 2135d8fd932SRandall Stewart } 2145d8fd932SRandall Stewart ip6 = (struct ip6_hdr *)(eh + 1); 2155d8fd932SRandall Stewart th = (struct tcphdr *)(ip6 + 1); 2165d8fd932SRandall Stewart drop_hdrlen = sizeof(*ip6); 2175d8fd932SRandall Stewart tlen = ntohs(ip6->ip6_plen); 2185d8fd932SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 2195d8fd932SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 2205d8fd932SRandall Stewart th->th_sum = m->m_pkthdr.csum_data; 2215d8fd932SRandall Stewart else 2225d8fd932SRandall Stewart th->th_sum = in6_cksum_pseudo(ip6, tlen, 2235d8fd932SRandall Stewart IPPROTO_TCP, 2245d8fd932SRandall Stewart m->m_pkthdr.csum_data); 2255d8fd932SRandall Stewart th->th_sum ^= 0xffff; 2265d8fd932SRandall Stewart } else 2275d8fd932SRandall Stewart th->th_sum = in6_cksum(m, IPPROTO_TCP, drop_hdrlen, tlen); 2285d8fd932SRandall Stewart if (th->th_sum) { 2295d8fd932SRandall Stewart KMOD_TCPSTAT_INC(tcps_rcvbadsum); 2305d8fd932SRandall Stewart m_freem(m); 2315d8fd932SRandall Stewart return (-1); 2325d8fd932SRandall Stewart } 2335d8fd932SRandall Stewart return (etype); 2345d8fd932SRandall Stewart } 2355d8fd932SRandall Stewart #endif 2365d8fd932SRandall Stewart #ifdef INET 2375d8fd932SRandall Stewart case ETHERTYPE_IP: 2385d8fd932SRandall Stewart { 2395d8fd932SRandall Stewart if (m->m_len < sizeof (struct tcpiphdr)) { 2405d8fd932SRandall Stewart m = m_pullup(m, sizeof (struct tcpiphdr)); 2415d8fd932SRandall Stewart if (m == NULL) { 2425d8fd932SRandall Stewart KMOD_TCPSTAT_INC(tcps_rcvshort); 2435d8fd932SRandall Stewart return (-1); 2445d8fd932SRandall Stewart } 2455d8fd932SRandall Stewart } 2465d8fd932SRandall Stewart ip = (struct ip *)(eh + 1); 2475d8fd932SRandall Stewart th = (struct tcphdr *)(ip + 1); 2485d8fd932SRandall Stewart drop_hdrlen = sizeof(*ip); 2495d8fd932SRandall Stewart iptos = ip->ip_tos; 2505d8fd932SRandall Stewart tlen = ntohs(ip->ip_len) - sizeof(struct ip); 2515d8fd932SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 2525d8fd932SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 2535d8fd932SRandall Stewart th->th_sum = m->m_pkthdr.csum_data; 2545d8fd932SRandall Stewart else 2555d8fd932SRandall Stewart th->th_sum = in_pseudo(ip->ip_src.s_addr, 2565d8fd932SRandall Stewart ip->ip_dst.s_addr, 2575d8fd932SRandall Stewart htonl(m->m_pkthdr.csum_data + tlen + IPPROTO_TCP)); 2585d8fd932SRandall Stewart th->th_sum ^= 0xffff; 2595d8fd932SRandall Stewart } else { 2605d8fd932SRandall Stewart int len; 2615d8fd932SRandall Stewart struct ipovly *ipov = (struct ipovly *)ip; 2625d8fd932SRandall Stewart /* 2635d8fd932SRandall Stewart * Checksum extended TCP header and data. 2645d8fd932SRandall Stewart */ 2655d8fd932SRandall Stewart len = drop_hdrlen + tlen; 2665d8fd932SRandall Stewart bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 2675d8fd932SRandall Stewart ipov->ih_len = htons(tlen); 2685d8fd932SRandall Stewart th->th_sum = in_cksum(m, len); 2695d8fd932SRandall Stewart /* Reset length for SDT probes. */ 2705d8fd932SRandall Stewart ip->ip_len = htons(len); 2715d8fd932SRandall Stewart /* Reset TOS bits */ 2725d8fd932SRandall Stewart ip->ip_tos = iptos; 2735d8fd932SRandall Stewart /* Re-initialization for later version check */ 2745d8fd932SRandall Stewart ip->ip_v = IPVERSION; 2755d8fd932SRandall Stewart ip->ip_hl = sizeof(*ip) >> 2; 2765d8fd932SRandall Stewart } 2775d8fd932SRandall Stewart if (th->th_sum) { 2785d8fd932SRandall Stewart KMOD_TCPSTAT_INC(tcps_rcvbadsum); 2795d8fd932SRandall Stewart m_freem(m); 2805d8fd932SRandall Stewart return (-1); 2815d8fd932SRandall Stewart } 2825d8fd932SRandall Stewart break; 2835d8fd932SRandall Stewart } 2845d8fd932SRandall Stewart #endif 2855d8fd932SRandall Stewart }; 2865d8fd932SRandall Stewart return (etype); 2875d8fd932SRandall Stewart } 2885d8fd932SRandall Stewart 289e57b2d0eSRandall Stewart /* 290e57b2d0eSRandall Stewart * The function ctf_process_inbound_raw() is used by 291e57b2d0eSRandall Stewart * transport developers to do the steps needed to 292e57b2d0eSRandall Stewart * support MBUF Queuing i.e. the flags in 293e57b2d0eSRandall Stewart * inp->inp_flags2: 294e57b2d0eSRandall Stewart * 295e57b2d0eSRandall Stewart * - INP_SUPPORTS_MBUFQ 296e57b2d0eSRandall Stewart * - INP_MBUF_QUEUE_READY 297e57b2d0eSRandall Stewart * - INP_DONT_SACK_QUEUE 2985d8fd932SRandall Stewart * - INP_MBUF_ACKCMP 299e57b2d0eSRandall Stewart * 300e57b2d0eSRandall Stewart * These flags help control how LRO will deliver 301e57b2d0eSRandall Stewart * packets to the transport. You first set in inp_flags2 302e57b2d0eSRandall Stewart * the INP_SUPPORTS_MBUFQ to tell the LRO code that you 303e57b2d0eSRandall Stewart * will gladly take a queue of packets instead of a compressed 304e57b2d0eSRandall Stewart * single packet. You also set in your t_fb pointer the 305e57b2d0eSRandall Stewart * tfb_do_queued_segments to point to ctf_process_inbound_raw. 306e57b2d0eSRandall Stewart * 307e57b2d0eSRandall Stewart * This then gets you lists of inbound ACK's/Data instead 308e57b2d0eSRandall Stewart * of a condensed compressed ACK/DATA packet. Why would you 309e57b2d0eSRandall Stewart * want that? This will get you access to all the arrival 310e57b2d0eSRandall Stewart * times of at least LRO and possibly at the Hardware (if 311e57b2d0eSRandall Stewart * the interface card supports that) of the actual ACK/DATA. 312e57b2d0eSRandall Stewart * In some transport designs this is important since knowing 313e57b2d0eSRandall Stewart * the actual time we got the packet is useful information. 314e57b2d0eSRandall Stewart * 3155d8fd932SRandall Stewart * A new special type of mbuf may also be supported by the transport 3165d8fd932SRandall Stewart * if it has set the INP_MBUF_ACKCMP flag. If its set, LRO will 3175d8fd932SRandall Stewart * possibly create a M_ACKCMP type mbuf. This is a mbuf with 3185d8fd932SRandall Stewart * an array of "acks". One thing also to note is that when this 3195d8fd932SRandall Stewart * occurs a subsequent LRO may find at the back of the untouched 3205d8fd932SRandall Stewart * mbuf queue chain a M_ACKCMP and append on to it. This means 3215d8fd932SRandall Stewart * that until the transport pulls in the mbuf chain queued 3225d8fd932SRandall Stewart * for it more ack's may get on the mbufs that were already 3235d8fd932SRandall Stewart * delivered. There currently is a limit of 6 acks condensed 3245d8fd932SRandall Stewart * into 1 mbuf which means often when this is occuring, we 3255d8fd932SRandall Stewart * don't get that effect but it does happen. 3265d8fd932SRandall Stewart * 327e57b2d0eSRandall Stewart * Now there are some interesting Caveats that the transport 328e57b2d0eSRandall Stewart * designer needs to take into account when using this feature. 329e57b2d0eSRandall Stewart * 330e57b2d0eSRandall Stewart * 1) It is used with HPTS and pacing, when the pacing timer 331e57b2d0eSRandall Stewart * for output calls it will first call the input. 332e57b2d0eSRandall Stewart * 2) When you set INP_MBUF_QUEUE_READY this tells LRO 333e57b2d0eSRandall Stewart * queue normal packets, I am busy pacing out data and 334e57b2d0eSRandall Stewart * will process the queued packets before my tfb_tcp_output 335e57b2d0eSRandall Stewart * call from pacing. If a non-normal packet arrives, (e.g. sack) 336e57b2d0eSRandall Stewart * you will be awoken immediately. 337e57b2d0eSRandall Stewart * 3) Finally you can add the INP_DONT_SACK_QUEUE to not even 338e57b2d0eSRandall Stewart * be awoken if a SACK has arrived. You would do this when 339e57b2d0eSRandall Stewart * you were not only running a pacing for output timer 340e57b2d0eSRandall Stewart * but a Rack timer as well i.e. you know you are in recovery 341e57b2d0eSRandall Stewart * and are in the process (via the timers) of dealing with 342e57b2d0eSRandall Stewart * the loss. 343e57b2d0eSRandall Stewart * 344e57b2d0eSRandall Stewart * Now a critical thing you must be aware of here is that the 345e57b2d0eSRandall Stewart * use of the flags has a far greater scope then just your 346e57b2d0eSRandall Stewart * typical LRO. Why? Well thats because in the normal compressed 347e57b2d0eSRandall Stewart * LRO case at the end of a driver interupt all packets are going 348e57b2d0eSRandall Stewart * to get presented to the transport no matter if there is one 349e57b2d0eSRandall Stewart * or 100. With the MBUF_QUEUE model, this is not true. You will 350e57b2d0eSRandall Stewart * only be awoken to process the queue of packets when: 351e57b2d0eSRandall Stewart * a) The flags discussed above allow it. 352e57b2d0eSRandall Stewart * <or> 353e57b2d0eSRandall Stewart * b) You exceed a ack or data limit (by default the 354e57b2d0eSRandall Stewart * ack limit is infinity (64k acks) and the data 355e57b2d0eSRandall Stewart * limit is 64k of new TCP data) 356e57b2d0eSRandall Stewart * <or> 357e57b2d0eSRandall Stewart * c) The push bit has been set by the peer 358e57b2d0eSRandall Stewart */ 359e57b2d0eSRandall Stewart 3603b0b41e6SRandall Stewart int 3613b0b41e6SRandall Stewart ctf_process_inbound_raw(struct tcpcb *tp, struct socket *so, struct mbuf *m, int has_pkt) 3623b0b41e6SRandall Stewart { 3633b0b41e6SRandall Stewart /* 3643b0b41e6SRandall Stewart * We are passed a raw change of mbuf packets 3653b0b41e6SRandall Stewart * that arrived in LRO. They are linked via 3663b0b41e6SRandall Stewart * the m_nextpkt link in the pkt-headers. 3673b0b41e6SRandall Stewart * 3683b0b41e6SRandall Stewart * We process each one by: 3693b0b41e6SRandall Stewart * a) saving off the next 3703b0b41e6SRandall Stewart * b) stripping off the ether-header 3713b0b41e6SRandall Stewart * c) formulating the arguments for 3723b0b41e6SRandall Stewart * the tfb_tcp_hpts_do_segment 3733b0b41e6SRandall Stewart * d) calling each mbuf to tfb_tcp_hpts_do_segment 3743b0b41e6SRandall Stewart * after adjusting the time to match the arrival time. 3753b0b41e6SRandall Stewart * Note that the LRO code assures no IP options are present. 3763b0b41e6SRandall Stewart * 3773b0b41e6SRandall Stewart * The symantics for calling tfb_tcp_hpts_do_segment are the 3783b0b41e6SRandall Stewart * following: 3793b0b41e6SRandall Stewart * 1) It returns 0 if all went well and you (the caller) need 3803b0b41e6SRandall Stewart * to release the lock. 3813b0b41e6SRandall Stewart * 2) If nxt_pkt is set, then the function will surpress calls 38240fa3e40SGleb Smirnoff * to tcp_output() since you are promising to call again 3833b0b41e6SRandall Stewart * with another packet. 3843b0b41e6SRandall Stewart * 3) If it returns 1, then you must free all the packets being 3853b0b41e6SRandall Stewart * shipped in, the tcb has been destroyed (or about to be destroyed). 3863b0b41e6SRandall Stewart */ 3873b0b41e6SRandall Stewart struct mbuf *m_save; 3883b0b41e6SRandall Stewart struct tcphdr *th; 3893b0b41e6SRandall Stewart #ifdef INET6 3903b0b41e6SRandall Stewart struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */ 3913b0b41e6SRandall Stewart #endif 3923b0b41e6SRandall Stewart #ifdef INET 3933b0b41e6SRandall Stewart struct ip *ip = NULL; /* Keep compiler happy. */ 3943b0b41e6SRandall Stewart #endif 3953b0b41e6SRandall Stewart struct ifnet *ifp; 3963b0b41e6SRandall Stewart struct timeval tv; 3970fd5c299SMateusz Guzik struct inpcb *inp __diagused; 3983b0b41e6SRandall Stewart int32_t retval, nxt_pkt, tlen, off; 3995d8fd932SRandall Stewart int etype = 0; 4003b0b41e6SRandall Stewart uint16_t drop_hdrlen; 4015d8fd932SRandall Stewart uint8_t iptos, no_vn=0; 4023b0b41e6SRandall Stewart 4039eb0e832SGleb Smirnoff inp = tptoinpcb(tp); 4049eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(inp); 405d40c0d47SGleb Smirnoff NET_EPOCH_ASSERT(); 4069eb0e832SGleb Smirnoff 4075d8fd932SRandall Stewart if (m) 4085d8fd932SRandall Stewart ifp = m_rcvif(m); 4093b0b41e6SRandall Stewart else 4103b0b41e6SRandall Stewart ifp = NULL; 4115d8fd932SRandall Stewart if (ifp == NULL) { 4123b0b41e6SRandall Stewart /* 4133b0b41e6SRandall Stewart * We probably should not work around 4143b0b41e6SRandall Stewart * but kassert, since lro alwasy sets rcvif. 4153b0b41e6SRandall Stewart */ 4163b0b41e6SRandall Stewart no_vn = 1; 4173b0b41e6SRandall Stewart goto skip_vnet; 4183b0b41e6SRandall Stewart } 4193b0b41e6SRandall Stewart CURVNET_SET(ifp->if_vnet); 4203b0b41e6SRandall Stewart skip_vnet: 4215d8fd932SRandall Stewart tcp_get_usecs(&tv); 4223b0b41e6SRandall Stewart while (m) { 4233b0b41e6SRandall Stewart m_save = m->m_nextpkt; 4243b0b41e6SRandall Stewart m->m_nextpkt = NULL; 4255d8fd932SRandall Stewart if ((m->m_flags & M_ACKCMP) == 0) { 4263b0b41e6SRandall Stewart /* Now lets get the ether header */ 4275d8fd932SRandall Stewart etype = ctf_get_enet_type(ifp, m); 4285d8fd932SRandall Stewart if (etype == -1) { 4295d8fd932SRandall Stewart /* Skip this packet it was freed by checksum */ 4305d8fd932SRandall Stewart goto skipped_pkt; 4315d8fd932SRandall Stewart } 4325d8fd932SRandall Stewart KASSERT(((etype == ETHERTYPE_IPV6) || (etype == ETHERTYPE_IP)), 4335d8fd932SRandall Stewart ("tp:%p m:%p etype:0x%x -- not IP or IPv6", tp, m, etype)); 4343b0b41e6SRandall Stewart /* Trim off the ethernet header */ 4353b0b41e6SRandall Stewart switch (etype) { 4363b0b41e6SRandall Stewart #ifdef INET6 4373b0b41e6SRandall Stewart case ETHERTYPE_IPV6: 4385d8fd932SRandall Stewart ip6 = mtod(m, struct ip6_hdr *); 4393b0b41e6SRandall Stewart th = (struct tcphdr *)(ip6 + 1); 4403b0b41e6SRandall Stewart tlen = ntohs(ip6->ip6_plen); 4413b0b41e6SRandall Stewart drop_hdrlen = sizeof(*ip6); 4425d8fd932SRandall Stewart iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 4433b0b41e6SRandall Stewart break; 4443b0b41e6SRandall Stewart #endif 4453b0b41e6SRandall Stewart #ifdef INET 4463b0b41e6SRandall Stewart case ETHERTYPE_IP: 4475d8fd932SRandall Stewart ip = mtod(m, struct ip *); 4483b0b41e6SRandall Stewart th = (struct tcphdr *)(ip + 1); 4493b0b41e6SRandall Stewart drop_hdrlen = sizeof(*ip); 4503b0b41e6SRandall Stewart iptos = ip->ip_tos; 4513b0b41e6SRandall Stewart tlen = ntohs(ip->ip_len) - sizeof(struct ip); 4523b0b41e6SRandall Stewart break; 4533b0b41e6SRandall Stewart #endif 4545d8fd932SRandall Stewart } /* end switch */ 4553b0b41e6SRandall Stewart /* 4563b0b41e6SRandall Stewart * Convert TCP protocol specific fields to host format. 4573b0b41e6SRandall Stewart */ 4583b0b41e6SRandall Stewart tcp_fields_to_host(th); 4593b0b41e6SRandall Stewart off = th->th_off << 2; 4603b0b41e6SRandall Stewart if (off < sizeof (struct tcphdr) || off > tlen) { 4615d8fd932SRandall Stewart printf("off:%d < hdrlen:%zu || > tlen:%u -- dump\n", 4625d8fd932SRandall Stewart off, 4635d8fd932SRandall Stewart sizeof(struct tcphdr), 4645d8fd932SRandall Stewart tlen); 4657ca6e296SMichael Tuexen KMOD_TCPSTAT_INC(tcps_rcvbadoff); 4663b0b41e6SRandall Stewart m_freem(m); 4673b0b41e6SRandall Stewart goto skipped_pkt; 4683b0b41e6SRandall Stewart } 4693b0b41e6SRandall Stewart tlen -= off; 4703b0b41e6SRandall Stewart drop_hdrlen += off; 4713b0b41e6SRandall Stewart /* 4723b0b41e6SRandall Stewart * Now lets setup the timeval to be when we should 4733b0b41e6SRandall Stewart * have been called (if we can). 4743b0b41e6SRandall Stewart */ 4753b0b41e6SRandall Stewart m->m_pkthdr.lro_nsegs = 1; 4763b0b41e6SRandall Stewart /* Now what about next packet? */ 4775d8fd932SRandall Stewart } else { 4785d8fd932SRandall Stewart /* 4795d8fd932SRandall Stewart * This mbuf is an array of acks that have 4805d8fd932SRandall Stewart * been compressed. We assert the inp has 4815d8fd932SRandall Stewart * the flag set to enable this! 4825d8fd932SRandall Stewart */ 4839eb0e832SGleb Smirnoff KASSERT((inp->inp_flags2 & INP_MBUF_ACKCMP), 4849eb0e832SGleb Smirnoff ("tp:%p inp:%p no INP_MBUF_ACKCMP flags?", tp, inp)); 4855d8fd932SRandall Stewart tlen = 0; 4865d8fd932SRandall Stewart drop_hdrlen = 0; 4875d8fd932SRandall Stewart th = NULL; 4885d8fd932SRandall Stewart iptos = 0; 4895d8fd932SRandall Stewart } 4905d8fd932SRandall Stewart tcp_get_usecs(&tv); 4913b0b41e6SRandall Stewart if (m_save || has_pkt) 4923b0b41e6SRandall Stewart nxt_pkt = 1; 4933b0b41e6SRandall Stewart else 4943b0b41e6SRandall Stewart nxt_pkt = 0; 4955d8fd932SRandall Stewart if ((m->m_flags & M_ACKCMP) == 0) 496963fb2adSRandall Stewart KMOD_TCPSTAT_INC(tcps_rcvtotal); 4975d8fd932SRandall Stewart else 4985d8fd932SRandall Stewart KMOD_TCPSTAT_ADD(tcps_rcvtotal, (m->m_len / sizeof(struct tcp_ackent))); 4993b0b41e6SRandall Stewart retval = (*tp->t_fb->tfb_do_segment_nounlock)(m, th, so, tp, drop_hdrlen, tlen, 5003b0b41e6SRandall Stewart iptos, nxt_pkt, &tv); 5013b0b41e6SRandall Stewart if (retval) { 5023b0b41e6SRandall Stewart /* We lost the lock and tcb probably */ 5033b0b41e6SRandall Stewart m = m_save; 5043b0b41e6SRandall Stewart while(m) { 5053b0b41e6SRandall Stewart m_save = m->m_nextpkt; 5063b0b41e6SRandall Stewart m->m_nextpkt = NULL; 5073b0b41e6SRandall Stewart m_freem(m); 5083b0b41e6SRandall Stewart m = m_save; 5093b0b41e6SRandall Stewart } 510a730d823SMichael Tuexen if (no_vn == 0) { 5113b0b41e6SRandall Stewart CURVNET_RESTORE(); 512a730d823SMichael Tuexen } 5135d8fd932SRandall Stewart INP_UNLOCK_ASSERT(inp); 5143b0b41e6SRandall Stewart return(retval); 5153b0b41e6SRandall Stewart } 5163b0b41e6SRandall Stewart skipped_pkt: 5173b0b41e6SRandall Stewart m = m_save; 5183b0b41e6SRandall Stewart } 519a730d823SMichael Tuexen if (no_vn == 0) { 5203b0b41e6SRandall Stewart CURVNET_RESTORE(); 521a730d823SMichael Tuexen } 5223b0b41e6SRandall Stewart return(retval); 5233b0b41e6SRandall Stewart } 5243b0b41e6SRandall Stewart 5253b0b41e6SRandall Stewart int 5263b0b41e6SRandall Stewart ctf_do_queued_segments(struct socket *so, struct tcpcb *tp, int have_pkt) 5273b0b41e6SRandall Stewart { 5283b0b41e6SRandall Stewart struct mbuf *m; 5293b0b41e6SRandall Stewart 5303b0b41e6SRandall Stewart /* First lets see if we have old packets */ 5313b0b41e6SRandall Stewart if (tp->t_in_pkt) { 5323b0b41e6SRandall Stewart m = tp->t_in_pkt; 5333b0b41e6SRandall Stewart tp->t_in_pkt = NULL; 5343b0b41e6SRandall Stewart tp->t_tail_pkt = NULL; 5353b0b41e6SRandall Stewart if (ctf_process_inbound_raw(tp, so, m, have_pkt)) { 5363b0b41e6SRandall Stewart /* We lost the tcpcb (maybe a RST came in)? */ 5373b0b41e6SRandall Stewart return(1); 5383b0b41e6SRandall Stewart } 5393b0b41e6SRandall Stewart } 5403b0b41e6SRandall Stewart return (0); 5413b0b41e6SRandall Stewart } 5423b0b41e6SRandall Stewart 5433b0b41e6SRandall Stewart uint32_t 5443b0b41e6SRandall Stewart ctf_outstanding(struct tcpcb *tp) 5453b0b41e6SRandall Stewart { 546777b88d6SRandall Stewart uint32_t bytes_out; 547777b88d6SRandall Stewart 548777b88d6SRandall Stewart bytes_out = tp->snd_max - tp->snd_una; 549777b88d6SRandall Stewart if (tp->t_state < TCPS_ESTABLISHED) 550777b88d6SRandall Stewart bytes_out++; 551777b88d6SRandall Stewart if (tp->t_flags & TF_SENTFIN) 552777b88d6SRandall Stewart bytes_out++; 553777b88d6SRandall Stewart return (bytes_out); 5543b0b41e6SRandall Stewart } 5553b0b41e6SRandall Stewart 5563b0b41e6SRandall Stewart uint32_t 5573b0b41e6SRandall Stewart ctf_flight_size(struct tcpcb *tp, uint32_t rc_sacked) 5583b0b41e6SRandall Stewart { 5593b0b41e6SRandall Stewart if (rc_sacked <= ctf_outstanding(tp)) 5603b0b41e6SRandall Stewart return(ctf_outstanding(tp) - rc_sacked); 5613b0b41e6SRandall Stewart else { 5623b0b41e6SRandall Stewart return (0); 5633b0b41e6SRandall Stewart } 5643b0b41e6SRandall Stewart } 5653b0b41e6SRandall Stewart 5663b0b41e6SRandall Stewart void 5673b0b41e6SRandall Stewart ctf_do_dropwithreset(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th, 5683b0b41e6SRandall Stewart int32_t rstreason, int32_t tlen) 5693b0b41e6SRandall Stewart { 5703b0b41e6SRandall Stewart if (tp != NULL) { 5713b0b41e6SRandall Stewart tcp_dropwithreset(m, th, tp, tlen, rstreason); 5729eb0e832SGleb Smirnoff INP_WUNLOCK(tptoinpcb(tp)); 5733b0b41e6SRandall Stewart } else 5743b0b41e6SRandall Stewart tcp_dropwithreset(m, th, NULL, tlen, rstreason); 5753b0b41e6SRandall Stewart } 5763b0b41e6SRandall Stewart 5775d8fd932SRandall Stewart void 5785d8fd932SRandall Stewart ctf_ack_war_checks(struct tcpcb *tp, uint32_t *ts, uint32_t *cnt) 5795d8fd932SRandall Stewart { 5805d8fd932SRandall Stewart if ((ts != NULL) && (cnt != NULL) && 5815d8fd932SRandall Stewart (tcp_ack_war_time_window > 0) && 5825d8fd932SRandall Stewart (tcp_ack_war_cnt > 0)) { 5835d8fd932SRandall Stewart /* We are possibly doing ack war prevention */ 5845d8fd932SRandall Stewart uint32_t cts; 5855d8fd932SRandall Stewart 5865d8fd932SRandall Stewart /* 5875d8fd932SRandall Stewart * We use a msec tick here which gives us 5885d8fd932SRandall Stewart * roughly 49 days. We don't need the 5895d8fd932SRandall Stewart * precision of a microsecond timestamp which 5905d8fd932SRandall Stewart * would only give us hours. 5915d8fd932SRandall Stewart */ 5925d8fd932SRandall Stewart cts = tcp_ts_getticks(); 5935d8fd932SRandall Stewart if (TSTMP_LT((*ts), cts)) { 5945d8fd932SRandall Stewart /* Timestamp is in the past */ 5955d8fd932SRandall Stewart *cnt = 0; 5965d8fd932SRandall Stewart *ts = (cts + tcp_ack_war_time_window); 5975d8fd932SRandall Stewart } 5985d8fd932SRandall Stewart if (*cnt < tcp_ack_war_cnt) { 5995d8fd932SRandall Stewart *cnt = (*cnt + 1); 6005d8fd932SRandall Stewart tp->t_flags |= TF_ACKNOW; 6015d8fd932SRandall Stewart } else 6025d8fd932SRandall Stewart tp->t_flags &= ~TF_ACKNOW; 6035d8fd932SRandall Stewart } else 6045d8fd932SRandall Stewart tp->t_flags |= TF_ACKNOW; 6055d8fd932SRandall Stewart } 6065d8fd932SRandall Stewart 6073b0b41e6SRandall Stewart /* 6083b0b41e6SRandall Stewart * ctf_drop_checks returns 1 for you should not proceed. It places 6093b0b41e6SRandall Stewart * in ret_val what should be returned 1/0 by the caller. The 1 indicates 6103b0b41e6SRandall Stewart * that the TCB is unlocked and probably dropped. The 0 indicates the 6113b0b41e6SRandall Stewart * TCB is still valid and locked. 6123b0b41e6SRandall Stewart */ 6133b0b41e6SRandall Stewart int 6145d8fd932SRandall Stewart _ctf_drop_checks(struct tcpopt *to, struct mbuf *m, struct tcphdr *th, 6155d8fd932SRandall Stewart struct tcpcb *tp, int32_t *tlenp, 6165d8fd932SRandall Stewart int32_t *thf, int32_t *drop_hdrlen, int32_t *ret_val, 6175d8fd932SRandall Stewart uint32_t *ts, uint32_t *cnt) 6183b0b41e6SRandall Stewart { 6193b0b41e6SRandall Stewart int32_t todrop; 6203b0b41e6SRandall Stewart int32_t thflags; 6213b0b41e6SRandall Stewart int32_t tlen; 6223b0b41e6SRandall Stewart 6233b0b41e6SRandall Stewart thflags = *thf; 6243b0b41e6SRandall Stewart tlen = *tlenp; 6253b0b41e6SRandall Stewart todrop = tp->rcv_nxt - th->th_seq; 6263b0b41e6SRandall Stewart if (todrop > 0) { 6273b0b41e6SRandall Stewart if (thflags & TH_SYN) { 6283b0b41e6SRandall Stewart thflags &= ~TH_SYN; 6293b0b41e6SRandall Stewart th->th_seq++; 6303b0b41e6SRandall Stewart if (th->th_urp > 1) 6313b0b41e6SRandall Stewart th->th_urp--; 6323b0b41e6SRandall Stewart else 6333b0b41e6SRandall Stewart thflags &= ~TH_URG; 6343b0b41e6SRandall Stewart todrop--; 6353b0b41e6SRandall Stewart } 6363b0b41e6SRandall Stewart /* 6373b0b41e6SRandall Stewart * Following if statement from Stevens, vol. 2, p. 960. 6383b0b41e6SRandall Stewart */ 6393b0b41e6SRandall Stewart if (todrop > tlen 6403b0b41e6SRandall Stewart || (todrop == tlen && (thflags & TH_FIN) == 0)) { 6413b0b41e6SRandall Stewart /* 6423b0b41e6SRandall Stewart * Any valid FIN must be to the left of the window. 6433b0b41e6SRandall Stewart * At this point the FIN must be a duplicate or out 6443b0b41e6SRandall Stewart * of sequence; drop it. 6453b0b41e6SRandall Stewart */ 6463b0b41e6SRandall Stewart thflags &= ~TH_FIN; 6473b0b41e6SRandall Stewart /* 6483b0b41e6SRandall Stewart * Send an ACK to resynchronize and drop any data. 6493b0b41e6SRandall Stewart * But keep on processing for RST or ACK. 6503b0b41e6SRandall Stewart */ 6515d8fd932SRandall Stewart ctf_ack_war_checks(tp, ts, cnt); 6523b0b41e6SRandall Stewart todrop = tlen; 6537ca6e296SMichael Tuexen KMOD_TCPSTAT_INC(tcps_rcvduppack); 6547ca6e296SMichael Tuexen KMOD_TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 6553b0b41e6SRandall Stewart } else { 6567ca6e296SMichael Tuexen KMOD_TCPSTAT_INC(tcps_rcvpartduppack); 6577ca6e296SMichael Tuexen KMOD_TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 6583b0b41e6SRandall Stewart } 6593b0b41e6SRandall Stewart /* 6603b0b41e6SRandall Stewart * DSACK - add SACK block for dropped range 6613b0b41e6SRandall Stewart */ 6628f63a52bSMichael Tuexen if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) { 6633b0b41e6SRandall Stewart /* 6643b0b41e6SRandall Stewart * ACK now, as the next in-sequence segment 6653b0b41e6SRandall Stewart * will clear the DSACK block again 6663b0b41e6SRandall Stewart */ 6675d8fd932SRandall Stewart ctf_ack_war_checks(tp, ts, cnt); 6685d8fd932SRandall Stewart if (tp->t_flags & TF_ACKNOW) 6695d8fd932SRandall Stewart tcp_update_sack_list(tp, th->th_seq, 6705d8fd932SRandall Stewart th->th_seq + todrop); 6713b0b41e6SRandall Stewart } 6723b0b41e6SRandall Stewart *drop_hdrlen += todrop; /* drop from the top afterwards */ 6733b0b41e6SRandall Stewart th->th_seq += todrop; 6743b0b41e6SRandall Stewart tlen -= todrop; 6753b0b41e6SRandall Stewart if (th->th_urp > todrop) 6763b0b41e6SRandall Stewart th->th_urp -= todrop; 6773b0b41e6SRandall Stewart else { 6783b0b41e6SRandall Stewart thflags &= ~TH_URG; 6793b0b41e6SRandall Stewart th->th_urp = 0; 6803b0b41e6SRandall Stewart } 6813b0b41e6SRandall Stewart } 6823b0b41e6SRandall Stewart /* 6833b0b41e6SRandall Stewart * If segment ends after window, drop trailing data (and PUSH and 6843b0b41e6SRandall Stewart * FIN); if nothing left, just ACK. 6853b0b41e6SRandall Stewart */ 6863b0b41e6SRandall Stewart todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 6873b0b41e6SRandall Stewart if (todrop > 0) { 6887ca6e296SMichael Tuexen KMOD_TCPSTAT_INC(tcps_rcvpackafterwin); 6893b0b41e6SRandall Stewart if (todrop >= tlen) { 6907ca6e296SMichael Tuexen KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 6913b0b41e6SRandall Stewart /* 6923b0b41e6SRandall Stewart * If window is closed can only take segments at 6933b0b41e6SRandall Stewart * window edge, and have to drop data and PUSH from 6943b0b41e6SRandall Stewart * incoming segments. Continue processing, but 6953b0b41e6SRandall Stewart * remember to ack. Otherwise, drop segment and 6963b0b41e6SRandall Stewart * ack. 6973b0b41e6SRandall Stewart */ 6983b0b41e6SRandall Stewart if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 6995d8fd932SRandall Stewart ctf_ack_war_checks(tp, ts, cnt); 7007ca6e296SMichael Tuexen KMOD_TCPSTAT_INC(tcps_rcvwinprobe); 7013b0b41e6SRandall Stewart } else { 7025d8fd932SRandall Stewart __ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val, ts, cnt); 7033b0b41e6SRandall Stewart return (1); 7043b0b41e6SRandall Stewart } 7053b0b41e6SRandall Stewart } else 7067ca6e296SMichael Tuexen KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 7073b0b41e6SRandall Stewart m_adj(m, -todrop); 7083b0b41e6SRandall Stewart tlen -= todrop; 7093b0b41e6SRandall Stewart thflags &= ~(TH_PUSH | TH_FIN); 7103b0b41e6SRandall Stewart } 7113b0b41e6SRandall Stewart *thf = thflags; 7123b0b41e6SRandall Stewart *tlenp = tlen; 7133b0b41e6SRandall Stewart return (0); 7143b0b41e6SRandall Stewart } 7153b0b41e6SRandall Stewart 7163b0b41e6SRandall Stewart /* 7173b0b41e6SRandall Stewart * The value in ret_val informs the caller 7183b0b41e6SRandall Stewart * if we dropped the tcb (and lock) or not. 7193b0b41e6SRandall Stewart * 1 = we dropped it, 0 = the TCB is still locked 7203b0b41e6SRandall Stewart * and valid. 7213b0b41e6SRandall Stewart */ 7223b0b41e6SRandall Stewart void 7235d8fd932SRandall Stewart __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) 7243b0b41e6SRandall Stewart { 7253b0b41e6SRandall Stewart /* 7263b0b41e6SRandall Stewart * Generate an ACK dropping incoming segment if it occupies sequence 7273b0b41e6SRandall Stewart * space, where the ACK reflects our state. 7283b0b41e6SRandall Stewart * 7293b0b41e6SRandall Stewart * We can now skip the test for the RST flag since all paths to this 7303b0b41e6SRandall Stewart * code happen after packets containing RST have been dropped. 7313b0b41e6SRandall Stewart * 7323b0b41e6SRandall Stewart * In the SYN-RECEIVED state, don't send an ACK unless the segment 7333b0b41e6SRandall Stewart * we received passes the SYN-RECEIVED ACK test. If it fails send a 7343b0b41e6SRandall Stewart * RST. This breaks the loop in the "LAND" DoS attack, and also 7353b0b41e6SRandall Stewart * prevents an ACK storm between two listening ports that have been 7363b0b41e6SRandall Stewart * sent forged SYN segments, each with the source address of the 7373b0b41e6SRandall Stewart * other. 7383b0b41e6SRandall Stewart */ 7393b0b41e6SRandall Stewart if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 7403b0b41e6SRandall Stewart (SEQ_GT(tp->snd_una, th->th_ack) || 7413b0b41e6SRandall Stewart SEQ_GT(th->th_ack, tp->snd_max))) { 7423b0b41e6SRandall Stewart *ret_val = 1; 7433b0b41e6SRandall Stewart ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen); 7443b0b41e6SRandall Stewart return; 7453b0b41e6SRandall Stewart } else 7463b0b41e6SRandall Stewart *ret_val = 0; 7475d8fd932SRandall Stewart ctf_ack_war_checks(tp, ts, cnt); 7483b0b41e6SRandall Stewart if (m) 7493b0b41e6SRandall Stewart m_freem(m); 7503b0b41e6SRandall Stewart } 7513b0b41e6SRandall Stewart 7523b0b41e6SRandall Stewart void 7533b0b41e6SRandall Stewart ctf_do_drop(struct mbuf *m, struct tcpcb *tp) 7543b0b41e6SRandall Stewart { 7553b0b41e6SRandall Stewart 7563b0b41e6SRandall Stewart /* 7573b0b41e6SRandall Stewart * Drop space held by incoming segment and return. 7583b0b41e6SRandall Stewart */ 7593b0b41e6SRandall Stewart if (tp != NULL) 7609eb0e832SGleb Smirnoff INP_WUNLOCK(tptoinpcb(tp)); 7613b0b41e6SRandall Stewart if (m) 7623b0b41e6SRandall Stewart m_freem(m); 7633b0b41e6SRandall Stewart } 7643b0b41e6SRandall Stewart 7653b0b41e6SRandall Stewart int 76697e28f0fSRandall Stewart __ctf_process_rst(struct mbuf *m, struct tcphdr *th, struct socket *so, 76797e28f0fSRandall Stewart struct tcpcb *tp, uint32_t *ts, uint32_t *cnt) 7683b0b41e6SRandall Stewart { 7693b0b41e6SRandall Stewart /* 7703b0b41e6SRandall Stewart * RFC5961 Section 3.2 7713b0b41e6SRandall Stewart * 7723b0b41e6SRandall Stewart * - RST drops connection only if SEG.SEQ == RCV.NXT. - If RST is in 7733b0b41e6SRandall Stewart * window, we send challenge ACK. 7743b0b41e6SRandall Stewart * 7753b0b41e6SRandall Stewart * Note: to take into account delayed ACKs, we should test against 7763b0b41e6SRandall Stewart * last_ack_sent instead of rcv_nxt. Note 2: we handle special case 7773b0b41e6SRandall Stewart * of closed window, not covered by the RFC. 7783b0b41e6SRandall Stewart */ 7793b0b41e6SRandall Stewart int dropped = 0; 7803b0b41e6SRandall Stewart 7815d8fd932SRandall Stewart if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 7823b0b41e6SRandall Stewart SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 7833b0b41e6SRandall Stewart (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 7843b0b41e6SRandall Stewart KASSERT(tp->t_state != TCPS_SYN_SENT, 7853b0b41e6SRandall Stewart ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 7863b0b41e6SRandall Stewart __func__, th, tp)); 7873b0b41e6SRandall Stewart 7883b0b41e6SRandall Stewart if (V_tcp_insecure_rst || 7893b0b41e6SRandall Stewart (tp->last_ack_sent == th->th_seq) || 7905d8fd932SRandall Stewart (tp->rcv_nxt == th->th_seq)) { 7917ca6e296SMichael Tuexen KMOD_TCPSTAT_INC(tcps_drops); 7923b0b41e6SRandall Stewart /* Drop the connection. */ 7933b0b41e6SRandall Stewart switch (tp->t_state) { 7943b0b41e6SRandall Stewart case TCPS_SYN_RECEIVED: 7953b0b41e6SRandall Stewart so->so_error = ECONNREFUSED; 7963b0b41e6SRandall Stewart goto close; 7973b0b41e6SRandall Stewart case TCPS_ESTABLISHED: 7983b0b41e6SRandall Stewart case TCPS_FIN_WAIT_1: 7993b0b41e6SRandall Stewart case TCPS_FIN_WAIT_2: 8003b0b41e6SRandall Stewart case TCPS_CLOSE_WAIT: 8013b0b41e6SRandall Stewart case TCPS_CLOSING: 8023b0b41e6SRandall Stewart case TCPS_LAST_ACK: 8033b0b41e6SRandall Stewart so->so_error = ECONNRESET; 8043b0b41e6SRandall Stewart close: 8053b0b41e6SRandall Stewart tcp_state_change(tp, TCPS_CLOSED); 8063b0b41e6SRandall Stewart /* FALLTHROUGH */ 8073b0b41e6SRandall Stewart default: 808963fb2adSRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_RST); 8093b0b41e6SRandall Stewart tp = tcp_close(tp); 8103b0b41e6SRandall Stewart } 8113b0b41e6SRandall Stewart dropped = 1; 8123b0b41e6SRandall Stewart ctf_do_drop(m, tp); 8133b0b41e6SRandall Stewart } else { 81497e28f0fSRandall Stewart int send_challenge; 81597e28f0fSRandall Stewart 8167ca6e296SMichael Tuexen KMOD_TCPSTAT_INC(tcps_badrst); 81797e28f0fSRandall Stewart if ((ts != NULL) && (cnt != NULL) && 81897e28f0fSRandall Stewart (tcp_ack_war_time_window > 0) && 81997e28f0fSRandall Stewart (tcp_ack_war_cnt > 0)) { 82097e28f0fSRandall Stewart /* We are possibly preventing an ack-rst war prevention */ 82197e28f0fSRandall Stewart uint32_t cts; 82297e28f0fSRandall Stewart 82397e28f0fSRandall Stewart /* 82497e28f0fSRandall Stewart * We use a msec tick here which gives us 82597e28f0fSRandall Stewart * roughly 49 days. We don't need the 82697e28f0fSRandall Stewart * precision of a microsecond timestamp which 82797e28f0fSRandall Stewart * would only give us hours. 82897e28f0fSRandall Stewart */ 82997e28f0fSRandall Stewart cts = tcp_ts_getticks(); 83097e28f0fSRandall Stewart if (TSTMP_LT((*ts), cts)) { 83197e28f0fSRandall Stewart /* Timestamp is in the past */ 83297e28f0fSRandall Stewart *cnt = 0; 83397e28f0fSRandall Stewart *ts = (cts + tcp_ack_war_time_window); 83497e28f0fSRandall Stewart } 83597e28f0fSRandall Stewart if (*cnt < tcp_ack_war_cnt) { 83697e28f0fSRandall Stewart *cnt = (*cnt + 1); 83797e28f0fSRandall Stewart send_challenge = 1; 83897e28f0fSRandall Stewart } else 83997e28f0fSRandall Stewart send_challenge = 0; 84097e28f0fSRandall Stewart } else 84197e28f0fSRandall Stewart send_challenge = 1; 84297e28f0fSRandall Stewart if (send_challenge) { 8433b0b41e6SRandall Stewart /* Send challenge ACK. */ 8443b0b41e6SRandall Stewart tcp_respond(tp, mtod(m, void *), th, m, 8453b0b41e6SRandall Stewart tp->rcv_nxt, tp->snd_nxt, TH_ACK); 8463b0b41e6SRandall Stewart tp->last_ack_sent = tp->rcv_nxt; 8473b0b41e6SRandall Stewart } 84897e28f0fSRandall Stewart } 8493b0b41e6SRandall Stewart } else { 8503b0b41e6SRandall Stewart m_freem(m); 8513b0b41e6SRandall Stewart } 8523b0b41e6SRandall Stewart return (dropped); 8533b0b41e6SRandall Stewart } 8543b0b41e6SRandall Stewart 8553b0b41e6SRandall Stewart /* 8563b0b41e6SRandall Stewart * The value in ret_val informs the caller 8573b0b41e6SRandall Stewart * if we dropped the tcb (and lock) or not. 8583b0b41e6SRandall Stewart * 1 = we dropped it, 0 = the TCB is still locked 8593b0b41e6SRandall Stewart * and valid. 8603b0b41e6SRandall Stewart */ 8613b0b41e6SRandall Stewart void 86283c1ec92SRichard Scheffenegger ctf_challenge_ack(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, uint8_t iptos, int32_t * ret_val) 8633b0b41e6SRandall Stewart { 864d40c0d47SGleb Smirnoff 865d40c0d47SGleb Smirnoff NET_EPOCH_ASSERT(); 8663b0b41e6SRandall Stewart 8677ca6e296SMichael Tuexen KMOD_TCPSTAT_INC(tcps_badsyn); 8683b0b41e6SRandall Stewart if (V_tcp_insecure_syn && 8693b0b41e6SRandall Stewart SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 8703b0b41e6SRandall Stewart SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 8713b0b41e6SRandall Stewart tp = tcp_drop(tp, ECONNRESET); 8723b0b41e6SRandall Stewart *ret_val = 1; 8733b0b41e6SRandall Stewart ctf_do_drop(m, tp); 8743b0b41e6SRandall Stewart } else { 87583c1ec92SRichard Scheffenegger tcp_ecn_input_syn_sent(tp, tcp_get_flags(th), iptos); 8763b0b41e6SRandall Stewart /* Send challenge ACK. */ 8773b0b41e6SRandall Stewart tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 8783b0b41e6SRandall Stewart tp->snd_nxt, TH_ACK); 8793b0b41e6SRandall Stewart tp->last_ack_sent = tp->rcv_nxt; 8803b0b41e6SRandall Stewart m = NULL; 8813b0b41e6SRandall Stewart *ret_val = 0; 8823b0b41e6SRandall Stewart ctf_do_drop(m, NULL); 8833b0b41e6SRandall Stewart } 8843b0b41e6SRandall Stewart } 8853b0b41e6SRandall Stewart 8863b0b41e6SRandall Stewart /* 8875d8fd932SRandall Stewart * ctf_ts_check returns 1 for you should not proceed, the state 8883b0b41e6SRandall Stewart * machine should return. It places in ret_val what should 8893b0b41e6SRandall Stewart * be returned 1/0 by the caller (hpts_do_segment). The 1 indicates 8903b0b41e6SRandall Stewart * that the TCB is unlocked and probably dropped. The 0 indicates the 8913b0b41e6SRandall Stewart * TCB is still valid and locked. 8923b0b41e6SRandall Stewart */ 8933b0b41e6SRandall Stewart int 8943b0b41e6SRandall Stewart ctf_ts_check(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 8953b0b41e6SRandall Stewart int32_t tlen, int32_t thflags, int32_t * ret_val) 8963b0b41e6SRandall Stewart { 8973b0b41e6SRandall Stewart 8983b0b41e6SRandall Stewart if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 8993b0b41e6SRandall Stewart /* 9003b0b41e6SRandall Stewart * Invalidate ts_recent. If this segment updates ts_recent, 9013b0b41e6SRandall Stewart * the age will be reset later and ts_recent will get a 9023b0b41e6SRandall Stewart * valid value. If it does not, setting ts_recent to zero 9033b0b41e6SRandall Stewart * will at least satisfy the requirement that zero be placed 9043b0b41e6SRandall Stewart * in the timestamp echo reply when ts_recent isn't valid. 9053b0b41e6SRandall Stewart * The age isn't reset until we get a valid ts_recent 9063b0b41e6SRandall Stewart * because we don't want out-of-order segments to be dropped 9073b0b41e6SRandall Stewart * when ts_recent is old. 9083b0b41e6SRandall Stewart */ 9093b0b41e6SRandall Stewart tp->ts_recent = 0; 9103b0b41e6SRandall Stewart } else { 9117ca6e296SMichael Tuexen KMOD_TCPSTAT_INC(tcps_rcvduppack); 9127ca6e296SMichael Tuexen KMOD_TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 9137ca6e296SMichael Tuexen KMOD_TCPSTAT_INC(tcps_pawsdrop); 9143b0b41e6SRandall Stewart *ret_val = 0; 9153b0b41e6SRandall Stewart if (tlen) { 9163b0b41e6SRandall Stewart ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val); 9173b0b41e6SRandall Stewart } else { 9183b0b41e6SRandall Stewart ctf_do_drop(m, NULL); 9193b0b41e6SRandall Stewart } 9203b0b41e6SRandall Stewart return (1); 9213b0b41e6SRandall Stewart } 9223b0b41e6SRandall Stewart return (0); 9233b0b41e6SRandall Stewart } 9243b0b41e6SRandall Stewart 9255d8fd932SRandall Stewart int 9265d8fd932SRandall Stewart ctf_ts_check_ac(struct tcpcb *tp, int32_t thflags) 9275d8fd932SRandall Stewart { 9285d8fd932SRandall Stewart 9295d8fd932SRandall Stewart if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 9305d8fd932SRandall Stewart /* 9315d8fd932SRandall Stewart * Invalidate ts_recent. If this segment updates ts_recent, 9325d8fd932SRandall Stewart * the age will be reset later and ts_recent will get a 9335d8fd932SRandall Stewart * valid value. If it does not, setting ts_recent to zero 9345d8fd932SRandall Stewart * will at least satisfy the requirement that zero be placed 9355d8fd932SRandall Stewart * in the timestamp echo reply when ts_recent isn't valid. 9365d8fd932SRandall Stewart * The age isn't reset until we get a valid ts_recent 9375d8fd932SRandall Stewart * because we don't want out-of-order segments to be dropped 9385d8fd932SRandall Stewart * when ts_recent is old. 9395d8fd932SRandall Stewart */ 9405d8fd932SRandall Stewart tp->ts_recent = 0; 9415d8fd932SRandall Stewart } else { 9425d8fd932SRandall Stewart KMOD_TCPSTAT_INC(tcps_rcvduppack); 9435d8fd932SRandall Stewart KMOD_TCPSTAT_INC(tcps_pawsdrop); 9445d8fd932SRandall Stewart return (1); 9455d8fd932SRandall Stewart } 9465d8fd932SRandall Stewart return (0); 9475d8fd932SRandall Stewart } 9485d8fd932SRandall Stewart 9495d8fd932SRandall Stewart 9505d8fd932SRandall Stewart 9513b0b41e6SRandall Stewart void 9523b0b41e6SRandall Stewart ctf_calc_rwin(struct socket *so, struct tcpcb *tp) 9533b0b41e6SRandall Stewart { 9543b0b41e6SRandall Stewart int32_t win; 9553b0b41e6SRandall Stewart 9563b0b41e6SRandall Stewart /* 9573b0b41e6SRandall Stewart * Calculate amount of space in receive window, and then do TCP 9583b0b41e6SRandall Stewart * input processing. Receive window is amount of space in rcv queue, 9593b0b41e6SRandall Stewart * but not less than advertised window. 9603b0b41e6SRandall Stewart */ 9613b0b41e6SRandall Stewart win = sbspace(&so->so_rcv); 9623b0b41e6SRandall Stewart if (win < 0) 9633b0b41e6SRandall Stewart win = 0; 9643b0b41e6SRandall Stewart tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 9653b0b41e6SRandall Stewart } 9663b0b41e6SRandall Stewart 9673b0b41e6SRandall Stewart void 9683b0b41e6SRandall Stewart ctf_do_dropwithreset_conn(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th, 9693b0b41e6SRandall Stewart int32_t rstreason, int32_t tlen) 9703b0b41e6SRandall Stewart { 9713b0b41e6SRandall Stewart 9723b0b41e6SRandall Stewart tcp_dropwithreset(m, th, tp, tlen, rstreason); 97317ac6b1cSGleb Smirnoff tp = tcp_drop(tp, ETIMEDOUT); 97417ac6b1cSGleb Smirnoff if (tp) 9759eb0e832SGleb Smirnoff INP_WUNLOCK(tptoinpcb(tp)); 9763b0b41e6SRandall Stewart } 9773b0b41e6SRandall Stewart 9783b0b41e6SRandall Stewart uint32_t 9793b0b41e6SRandall Stewart ctf_fixed_maxseg(struct tcpcb *tp) 9803b0b41e6SRandall Stewart { 9815d8fd932SRandall Stewart return (tcp_fixed_maxseg(tp)); 9823b0b41e6SRandall Stewart } 9833b0b41e6SRandall Stewart 9843b0b41e6SRandall Stewart void 9853b0b41e6SRandall Stewart ctf_log_sack_filter(struct tcpcb *tp, int num_sack_blks, struct sackblk *sack_blocks) 9863b0b41e6SRandall Stewart { 987*69c7c811SRandall Stewart if (tcp_bblogging_on(tp)) { 9883b0b41e6SRandall Stewart union tcp_log_stackspecific log; 9893b0b41e6SRandall Stewart struct timeval tv; 9903b0b41e6SRandall Stewart 9913b0b41e6SRandall Stewart memset(&log, 0, sizeof(log)); 9923b0b41e6SRandall Stewart log.u_bbr.timeStamp = tcp_get_usecs(&tv); 9933b0b41e6SRandall Stewart log.u_bbr.flex8 = num_sack_blks; 9943b0b41e6SRandall Stewart if (num_sack_blks > 0) { 9953b0b41e6SRandall Stewart log.u_bbr.flex1 = sack_blocks[0].start; 9963b0b41e6SRandall Stewart log.u_bbr.flex2 = sack_blocks[0].end; 9973b0b41e6SRandall Stewart } 9983b0b41e6SRandall Stewart if (num_sack_blks > 1) { 9993b0b41e6SRandall Stewart log.u_bbr.flex3 = sack_blocks[1].start; 10003b0b41e6SRandall Stewart log.u_bbr.flex4 = sack_blocks[1].end; 10013b0b41e6SRandall Stewart } 10023b0b41e6SRandall Stewart if (num_sack_blks > 2) { 10033b0b41e6SRandall Stewart log.u_bbr.flex5 = sack_blocks[2].start; 10043b0b41e6SRandall Stewart log.u_bbr.flex6 = sack_blocks[2].end; 10053b0b41e6SRandall Stewart } 10063b0b41e6SRandall Stewart if (num_sack_blks > 3) { 10073b0b41e6SRandall Stewart log.u_bbr.applimited = sack_blocks[3].start; 10083b0b41e6SRandall Stewart log.u_bbr.pkts_out = sack_blocks[3].end; 10093b0b41e6SRandall Stewart } 10103b0b41e6SRandall Stewart TCP_LOG_EVENTP(tp, NULL, 10119eb0e832SGleb Smirnoff &tptosocket(tp)->so_rcv, 10129eb0e832SGleb Smirnoff &tptosocket(tp)->so_snd, 10133b0b41e6SRandall Stewart TCP_SACK_FILTER_RES, 0, 10143b0b41e6SRandall Stewart 0, &log, false, &tv); 10153b0b41e6SRandall Stewart } 10163b0b41e6SRandall Stewart } 10173b0b41e6SRandall Stewart 10183b0b41e6SRandall Stewart uint32_t 10193b0b41e6SRandall Stewart ctf_decay_count(uint32_t count, uint32_t decay) 10203b0b41e6SRandall Stewart { 10213b0b41e6SRandall Stewart /* 10223b0b41e6SRandall Stewart * Given a count, decay it by a set percentage. The 10233b0b41e6SRandall Stewart * percentage is in thousands i.e. 100% = 1000, 10243b0b41e6SRandall Stewart * 19.3% = 193. 10253b0b41e6SRandall Stewart */ 10263b0b41e6SRandall Stewart uint64_t perc_count, decay_per; 10273b0b41e6SRandall Stewart uint32_t decayed_count; 10283b0b41e6SRandall Stewart if (decay > 1000) { 10293b0b41e6SRandall Stewart /* We don't raise it */ 10303b0b41e6SRandall Stewart return (count); 10313b0b41e6SRandall Stewart } 10323b0b41e6SRandall Stewart perc_count = count; 10333b0b41e6SRandall Stewart decay_per = decay; 10343b0b41e6SRandall Stewart perc_count *= decay_per; 10353b0b41e6SRandall Stewart perc_count /= 1000; 10363b0b41e6SRandall Stewart /* 10373b0b41e6SRandall Stewart * So now perc_count holds the 10383b0b41e6SRandall Stewart * count decay value. 10393b0b41e6SRandall Stewart */ 10403b0b41e6SRandall Stewart decayed_count = count - (uint32_t)perc_count; 10413b0b41e6SRandall Stewart return(decayed_count); 10423b0b41e6SRandall Stewart } 1043963fb2adSRandall Stewart 1044963fb2adSRandall Stewart int32_t 1045963fb2adSRandall Stewart ctf_progress_timeout_check(struct tcpcb *tp, bool log) 1046963fb2adSRandall Stewart { 1047963fb2adSRandall Stewart if (tp->t_maxunacktime && tp->t_acktime && TSTMP_GT(ticks, tp->t_acktime)) { 1048963fb2adSRandall Stewart if ((ticks - tp->t_acktime) >= tp->t_maxunacktime) { 1049963fb2adSRandall Stewart /* 1050963fb2adSRandall Stewart * There is an assumption that the caller 1051963fb2adSRandall Stewart * will drop the connection so we will 1052963fb2adSRandall Stewart * increment the counters here. 1053963fb2adSRandall Stewart */ 1054963fb2adSRandall Stewart if (log) 1055963fb2adSRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_PROGRESS); 1056963fb2adSRandall Stewart #ifdef NETFLIX_STATS 1057963fb2adSRandall Stewart KMOD_TCPSTAT_INC(tcps_progdrops); 1058963fb2adSRandall Stewart #endif 1059963fb2adSRandall Stewart return (1); 1060963fb2adSRandall Stewart } 1061963fb2adSRandall Stewart } 1062963fb2adSRandall Stewart return (0); 1063963fb2adSRandall Stewart } 1064