xref: /freebsd/sys/netinet/tcp_stacks/rack_bbr_common.c (revision 1cf55767b800492f15d2d49884f78f05506049c0)
13b0b41e6SRandall Stewart /*-
2*1cf55767SRandall Stewart  * Copyright (c) 2016-9
33b0b41e6SRandall Stewart  *	Netflix Inc.
43b0b41e6SRandall Stewart  *      All rights reserved.
53b0b41e6SRandall Stewart  *
63b0b41e6SRandall Stewart  * Redistribution and use in source and binary forms, with or without
73b0b41e6SRandall Stewart  * modification, are permitted provided that the following conditions
83b0b41e6SRandall Stewart  * are met:
93b0b41e6SRandall Stewart  * 1. Redistributions of source code must retain the above copyright
103b0b41e6SRandall Stewart  *    notice, this list of conditions and the following disclaimer.
113b0b41e6SRandall Stewart  * 2. Redistributions in binary form must reproduce the above copyright
123b0b41e6SRandall Stewart  *    notice, this list of conditions and the following disclaimer in the
133b0b41e6SRandall Stewart  *    documentation and/or other materials provided with the distribution.
143b0b41e6SRandall Stewart  *
153b0b41e6SRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
163b0b41e6SRandall Stewart  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173b0b41e6SRandall Stewart  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183b0b41e6SRandall Stewart  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
193b0b41e6SRandall Stewart  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
203b0b41e6SRandall Stewart  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213b0b41e6SRandall Stewart  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223b0b41e6SRandall Stewart  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233b0b41e6SRandall Stewart  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
243b0b41e6SRandall Stewart  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
253b0b41e6SRandall Stewart  * SUCH DAMAGE.
263b0b41e6SRandall Stewart  *
273b0b41e6SRandall Stewart  */
283b0b41e6SRandall Stewart /*
293b0b41e6SRandall Stewart  * Author: Randall Stewart <rrs@netflix.com>
303b0b41e6SRandall Stewart  * This work is based on the ACM Queue paper
313b0b41e6SRandall Stewart  * BBR - Congestion Based Congestion Control
323b0b41e6SRandall Stewart  * and also numerous discussions with Neal, Yuchung and Van.
333b0b41e6SRandall Stewart  */
343b0b41e6SRandall Stewart 
353b0b41e6SRandall Stewart #include <sys/cdefs.h>
363b0b41e6SRandall Stewart __FBSDID("$FreeBSD$");
373b0b41e6SRandall Stewart 
383b0b41e6SRandall Stewart #include "opt_inet.h"
393b0b41e6SRandall Stewart #include "opt_inet6.h"
403b0b41e6SRandall Stewart #include "opt_ipsec.h"
413b0b41e6SRandall Stewart #include "opt_tcpdebug.h"
423b0b41e6SRandall Stewart #include "opt_ratelimit.h"
4335c7bb34SRandall Stewart #include "opt_kern_tls.h"
443b0b41e6SRandall Stewart #include <sys/param.h>
45*1cf55767SRandall Stewart #include <sys/arb.h>
463b0b41e6SRandall Stewart #include <sys/module.h>
473b0b41e6SRandall Stewart #include <sys/kernel.h>
483b0b41e6SRandall Stewart #ifdef TCP_HHOOK
493b0b41e6SRandall Stewart #include <sys/hhook.h>
503b0b41e6SRandall Stewart #endif
513b0b41e6SRandall Stewart #include <sys/malloc.h>
523b0b41e6SRandall Stewart #include <sys/mbuf.h>
533b0b41e6SRandall Stewart #include <sys/proc.h>
5435c7bb34SRandall Stewart #include <sys/qmath.h>
553b0b41e6SRandall Stewart #include <sys/socket.h>
563b0b41e6SRandall Stewart #include <sys/socketvar.h>
573b0b41e6SRandall Stewart #ifdef KERN_TLS
5835c7bb34SRandall Stewart #include <sys/ktls.h>
593b0b41e6SRandall Stewart #endif
603b0b41e6SRandall Stewart #include <sys/sysctl.h>
613b0b41e6SRandall Stewart #include <sys/systm.h>
623b0b41e6SRandall Stewart #include <sys/tree.h>
6335c7bb34SRandall Stewart #ifdef NETFLIX_STATS
6435c7bb34SRandall Stewart #include <sys/stats.h> /* Must come after qmath.h and tree.h */
6535c7bb34SRandall Stewart #endif
663b0b41e6SRandall Stewart #include <sys/refcount.h>
673b0b41e6SRandall Stewart #include <sys/queue.h>
683b0b41e6SRandall Stewart #include <sys/smp.h>
693b0b41e6SRandall Stewart #include <sys/kthread.h>
703b0b41e6SRandall Stewart #include <sys/lock.h>
713b0b41e6SRandall Stewart #include <sys/mutex.h>
7235c7bb34SRandall Stewart #include <sys/tim_filter.h>
733b0b41e6SRandall Stewart #include <sys/time.h>
743b0b41e6SRandall Stewart #include <vm/uma.h>
753b0b41e6SRandall Stewart #include <sys/kern_prefetch.h>
763b0b41e6SRandall Stewart 
773b0b41e6SRandall Stewart #include <net/route.h>
783b0b41e6SRandall Stewart #include <net/vnet.h>
793b0b41e6SRandall Stewart #include <net/ethernet.h>
803b0b41e6SRandall Stewart #include <net/bpf.h>
813b0b41e6SRandall Stewart 
823b0b41e6SRandall Stewart #define TCPSTATES		/* for logging */
833b0b41e6SRandall Stewart 
843b0b41e6SRandall Stewart #include <netinet/in.h>
853b0b41e6SRandall Stewart #include <netinet/in_kdtrace.h>
863b0b41e6SRandall Stewart #include <netinet/in_pcb.h>
873b0b41e6SRandall Stewart #include <netinet/ip.h>
883b0b41e6SRandall Stewart #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
893b0b41e6SRandall Stewart #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
903b0b41e6SRandall Stewart #include <netinet/ip_var.h>
913b0b41e6SRandall Stewart #include <netinet/ip6.h>
923b0b41e6SRandall Stewart #include <netinet6/in6_pcb.h>
933b0b41e6SRandall Stewart #include <netinet6/ip6_var.h>
9435c7bb34SRandall Stewart #define	TCPOUTFLAGS
953b0b41e6SRandall Stewart #include <netinet/tcp.h>
963b0b41e6SRandall Stewart #include <netinet/tcp_fsm.h>
973b0b41e6SRandall Stewart #include <netinet/tcp_seq.h>
983b0b41e6SRandall Stewart #include <netinet/tcp_timer.h>
993b0b41e6SRandall Stewart #include <netinet/tcp_var.h>
1003b0b41e6SRandall Stewart #include <netinet/tcpip.h>
1013b0b41e6SRandall Stewart #include <netinet/tcp_hpts.h>
1023b0b41e6SRandall Stewart #include <netinet/cc/cc.h>
1033b0b41e6SRandall Stewart #include <netinet/tcp_log_buf.h>
1043b0b41e6SRandall Stewart #ifdef TCPDEBUG
1053b0b41e6SRandall Stewart #include <netinet/tcp_debug.h>
1063b0b41e6SRandall Stewart #endif				/* TCPDEBUG */
1073b0b41e6SRandall Stewart #ifdef TCP_OFFLOAD
1083b0b41e6SRandall Stewart #include <netinet/tcp_offload.h>
1093b0b41e6SRandall Stewart #endif
1103b0b41e6SRandall Stewart #ifdef INET6
1113b0b41e6SRandall Stewart #include <netinet6/tcp6_var.h>
1123b0b41e6SRandall Stewart #endif
1133b0b41e6SRandall Stewart #include <netinet/tcp_fastopen.h>
1143b0b41e6SRandall Stewart 
1153b0b41e6SRandall Stewart #include <netipsec/ipsec_support.h>
1163b0b41e6SRandall Stewart #include <net/if.h>
1173b0b41e6SRandall Stewart #include <net/if_var.h>
1183b0b41e6SRandall Stewart 
1193b0b41e6SRandall Stewart #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1203b0b41e6SRandall Stewart #include <netipsec/ipsec.h>
1213b0b41e6SRandall Stewart #include <netipsec/ipsec6.h>
1223b0b41e6SRandall Stewart #endif				/* IPSEC */
1233b0b41e6SRandall Stewart 
1243b0b41e6SRandall Stewart #include <netinet/udp.h>
1253b0b41e6SRandall Stewart #include <netinet/udp_var.h>
1263b0b41e6SRandall Stewart #include <machine/in_cksum.h>
1273b0b41e6SRandall Stewart 
1283b0b41e6SRandall Stewart #ifdef MAC
1293b0b41e6SRandall Stewart #include <security/mac/mac_framework.h>
1303b0b41e6SRandall Stewart #endif
1313b0b41e6SRandall Stewart #include "rack_bbr_common.h"
1323b0b41e6SRandall Stewart 
1333b0b41e6SRandall Stewart /*
1343b0b41e6SRandall Stewart  * Common TCP Functions - These are shared by borth
1353b0b41e6SRandall Stewart  * rack and BBR.
1363b0b41e6SRandall Stewart  */
1373b0b41e6SRandall Stewart #ifdef KERN_TLS
1383b0b41e6SRandall Stewart uint32_t
1393b0b41e6SRandall Stewart ctf_get_opt_tls_size(struct socket *so, uint32_t rwnd)
1403b0b41e6SRandall Stewart {
14135c7bb34SRandall Stewart 	struct ktls_session *tls;
1423b0b41e6SRandall Stewart 	uint32_t len;
1433b0b41e6SRandall Stewart 
1443b0b41e6SRandall Stewart again:
1453b0b41e6SRandall Stewart 	tls = so->so_snd.sb_tls_info;
14635c7bb34SRandall Stewart 	len = tls->params.max_frame_len;         /* max tls payload */
14735c7bb34SRandall Stewart 	len += tls->params.tls_hlen;      /* tls header len  */
14835c7bb34SRandall Stewart 	len += tls->params.tls_tlen;      /* tls trailer len */
1493b0b41e6SRandall Stewart 	if ((len * 4) > rwnd) {
1503b0b41e6SRandall Stewart 		/*
1513b0b41e6SRandall Stewart 		 * Stroke this will suck counter and what
1523b0b41e6SRandall Stewart 		 * else should we do Drew? From the
1533b0b41e6SRandall Stewart 		 * TCP perspective I am not sure
1543b0b41e6SRandall Stewart 		 * what should be done...
1553b0b41e6SRandall Stewart 		 */
15635c7bb34SRandall Stewart 		if (tls->params.max_frame_len > 4096) {
15735c7bb34SRandall Stewart 			tls->params.max_frame_len -= 4096;
15835c7bb34SRandall Stewart 			if (tls->params.max_frame_len < 4096)
15935c7bb34SRandall Stewart 				tls->params.max_frame_len = 4096;
1603b0b41e6SRandall Stewart 			goto again;
1613b0b41e6SRandall Stewart 		}
1623b0b41e6SRandall Stewart 	}
1633b0b41e6SRandall Stewart 	return (len);
1643b0b41e6SRandall Stewart }
1653b0b41e6SRandall Stewart #endif
1663b0b41e6SRandall Stewart 
167e57b2d0eSRandall Stewart 
168e57b2d0eSRandall Stewart /*
169e57b2d0eSRandall Stewart  * The function ctf_process_inbound_raw() is used by
170e57b2d0eSRandall Stewart  * transport developers to do the steps needed to
171e57b2d0eSRandall Stewart  * support MBUF Queuing i.e. the flags in
172e57b2d0eSRandall Stewart  * inp->inp_flags2:
173e57b2d0eSRandall Stewart  *
174e57b2d0eSRandall Stewart  * - INP_SUPPORTS_MBUFQ
175e57b2d0eSRandall Stewart  * - INP_MBUF_QUEUE_READY
176e57b2d0eSRandall Stewart  * - INP_DONT_SACK_QUEUE
177e57b2d0eSRandall Stewart  *
178e57b2d0eSRandall Stewart  * These flags help control how LRO will deliver
179e57b2d0eSRandall Stewart  * packets to the transport. You first set in inp_flags2
180e57b2d0eSRandall Stewart  * the INP_SUPPORTS_MBUFQ to tell the LRO code that you
181e57b2d0eSRandall Stewart  * will gladly take a queue of packets instead of a compressed
182e57b2d0eSRandall Stewart  * single packet. You also set in your t_fb pointer the
183e57b2d0eSRandall Stewart  * tfb_do_queued_segments to point to ctf_process_inbound_raw.
184e57b2d0eSRandall Stewart  *
185e57b2d0eSRandall Stewart  * This then gets you lists of inbound ACK's/Data instead
186e57b2d0eSRandall Stewart  * of a condensed compressed ACK/DATA packet. Why would you
187e57b2d0eSRandall Stewart  * want that? This will get you access to all the arrival
188e57b2d0eSRandall Stewart  * times of at least LRO and possibly at the Hardware (if
189e57b2d0eSRandall Stewart  * the interface card supports that) of the actual ACK/DATA.
190e57b2d0eSRandall Stewart  * In some transport designs this is important since knowing
191e57b2d0eSRandall Stewart  * the actual time we got the packet is useful information.
192e57b2d0eSRandall Stewart  *
193e57b2d0eSRandall Stewart  * Now there are some interesting Caveats that the transport
194e57b2d0eSRandall Stewart  * designer needs to take into account when using this feature.
195e57b2d0eSRandall Stewart  *
196e57b2d0eSRandall Stewart  * 1) It is used with HPTS and pacing, when the pacing timer
197e57b2d0eSRandall Stewart  *    for output calls it will first call the input.
198e57b2d0eSRandall Stewart  * 2) When you set INP_MBUF_QUEUE_READY this tells LRO
199e57b2d0eSRandall Stewart  *    queue normal packets, I am busy pacing out data and
200e57b2d0eSRandall Stewart  *    will process the queued packets before my tfb_tcp_output
201e57b2d0eSRandall Stewart  *    call from pacing. If a non-normal packet arrives, (e.g. sack)
202e57b2d0eSRandall Stewart  *    you will be awoken immediately.
203e57b2d0eSRandall Stewart  * 3) Finally you can add the INP_DONT_SACK_QUEUE to not even
204e57b2d0eSRandall Stewart  *    be awoken if a SACK has arrived. You would do this when
205e57b2d0eSRandall Stewart  *    you were not only running a pacing for output timer
206e57b2d0eSRandall Stewart  *    but a Rack timer as well i.e. you know you are in recovery
207e57b2d0eSRandall Stewart  *    and are in the process (via the timers) of dealing with
208e57b2d0eSRandall Stewart  *    the loss.
209e57b2d0eSRandall Stewart  *
210e57b2d0eSRandall Stewart  * Now a critical thing you must be aware of here is that the
211e57b2d0eSRandall Stewart  * use of the flags has a far greater scope then just your
212e57b2d0eSRandall Stewart  * typical LRO. Why? Well thats because in the normal compressed
213e57b2d0eSRandall Stewart  * LRO case at the end of a driver interupt all packets are going
214e57b2d0eSRandall Stewart  * to get presented to the transport no matter if there is one
215e57b2d0eSRandall Stewart  * or 100. With the MBUF_QUEUE model, this is not true. You will
216e57b2d0eSRandall Stewart  * only be awoken to process the queue of packets when:
217e57b2d0eSRandall Stewart  *     a) The flags discussed above allow it.
218e57b2d0eSRandall Stewart  *          <or>
219e57b2d0eSRandall Stewart  *     b) You exceed a ack or data limit (by default the
220e57b2d0eSRandall Stewart  *        ack limit is infinity (64k acks) and the data
221e57b2d0eSRandall Stewart  *        limit is 64k of new TCP data)
222e57b2d0eSRandall Stewart  *         <or>
223e57b2d0eSRandall Stewart  *     c) The push bit has been set by the peer
224e57b2d0eSRandall Stewart  */
225e57b2d0eSRandall Stewart 
2263b0b41e6SRandall Stewart int
2273b0b41e6SRandall Stewart ctf_process_inbound_raw(struct tcpcb *tp, struct socket *so, struct mbuf *m, int has_pkt)
2283b0b41e6SRandall Stewart {
2293b0b41e6SRandall Stewart 	/*
2303b0b41e6SRandall Stewart 	 * We are passed a raw change of mbuf packets
2313b0b41e6SRandall Stewart 	 * that arrived in LRO. They are linked via
2323b0b41e6SRandall Stewart 	 * the m_nextpkt link in the pkt-headers.
2333b0b41e6SRandall Stewart 	 *
2343b0b41e6SRandall Stewart 	 * We process each one by:
2353b0b41e6SRandall Stewart 	 * a) saving off the next
2363b0b41e6SRandall Stewart 	 * b) stripping off the ether-header
2373b0b41e6SRandall Stewart 	 * c) formulating the arguments for
2383b0b41e6SRandall Stewart 	 *    the tfb_tcp_hpts_do_segment
2393b0b41e6SRandall Stewart 	 * d) calling each mbuf to tfb_tcp_hpts_do_segment
2403b0b41e6SRandall Stewart 	 *    after adjusting the time to match the arrival time.
2413b0b41e6SRandall Stewart 	 * Note that the LRO code assures no IP options are present.
2423b0b41e6SRandall Stewart 	 *
2433b0b41e6SRandall Stewart 	 * The symantics for calling tfb_tcp_hpts_do_segment are the
2443b0b41e6SRandall Stewart 	 * following:
2453b0b41e6SRandall Stewart 	 * 1) It returns 0 if all went well and you (the caller) need
2463b0b41e6SRandall Stewart 	 *    to release the lock.
2473b0b41e6SRandall Stewart 	 * 2) If nxt_pkt is set, then the function will surpress calls
2483b0b41e6SRandall Stewart 	 *    to tfb_tcp_output() since you are promising to call again
2493b0b41e6SRandall Stewart 	 *    with another packet.
2503b0b41e6SRandall Stewart 	 * 3) If it returns 1, then you must free all the packets being
2513b0b41e6SRandall Stewart 	 *    shipped in, the tcb has been destroyed (or about to be destroyed).
2523b0b41e6SRandall Stewart 	 */
2533b0b41e6SRandall Stewart 	struct mbuf *m_save;
2543b0b41e6SRandall Stewart 	struct ether_header *eh;
2553b0b41e6SRandall Stewart 	struct tcphdr *th;
2563b0b41e6SRandall Stewart #ifdef INET6
2573b0b41e6SRandall Stewart 	struct ip6_hdr *ip6 = NULL;	/* Keep compiler happy. */
2583b0b41e6SRandall Stewart #endif
2593b0b41e6SRandall Stewart #ifdef INET
2603b0b41e6SRandall Stewart 	struct ip *ip = NULL;		/* Keep compiler happy. */
2613b0b41e6SRandall Stewart #endif
2623b0b41e6SRandall Stewart 	struct ifnet *ifp;
2633b0b41e6SRandall Stewart 	struct timeval tv;
2643b0b41e6SRandall Stewart 	int32_t retval, nxt_pkt, tlen, off;
2653b0b41e6SRandall Stewart 	uint16_t etype;
2663b0b41e6SRandall Stewart 	uint16_t drop_hdrlen;
2673b0b41e6SRandall Stewart 	uint8_t iptos, no_vn=0, bpf_req=0;
2683b0b41e6SRandall Stewart 
269d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
270d40c0d47SGleb Smirnoff 
2713b0b41e6SRandall Stewart 	if (m && m->m_pkthdr.rcvif)
2723b0b41e6SRandall Stewart 		ifp = m->m_pkthdr.rcvif;
2733b0b41e6SRandall Stewart 	else
2743b0b41e6SRandall Stewart 		ifp = NULL;
2753b0b41e6SRandall Stewart 	if (ifp) {
2763b0b41e6SRandall Stewart 		bpf_req = bpf_peers_present(ifp->if_bpf);
2773b0b41e6SRandall Stewart 	} else  {
2783b0b41e6SRandall Stewart 		/*
2793b0b41e6SRandall Stewart 		 * We probably should not work around
2803b0b41e6SRandall Stewart 		 * but kassert, since lro alwasy sets rcvif.
2813b0b41e6SRandall Stewart 		 */
2823b0b41e6SRandall Stewart 		no_vn = 1;
2833b0b41e6SRandall Stewart 		goto skip_vnet;
2843b0b41e6SRandall Stewart 	}
2853b0b41e6SRandall Stewart 	CURVNET_SET(ifp->if_vnet);
2863b0b41e6SRandall Stewart skip_vnet:
2873b0b41e6SRandall Stewart 	while (m) {
2883b0b41e6SRandall Stewart 		m_save = m->m_nextpkt;
2893b0b41e6SRandall Stewart 		m->m_nextpkt = NULL;
2903b0b41e6SRandall Stewart 		/* Now lets get the ether header */
2913b0b41e6SRandall Stewart 		eh = mtod(m, struct ether_header *);
2923b0b41e6SRandall Stewart 		etype = ntohs(eh->ether_type);
2933b0b41e6SRandall Stewart 		/* Let the BPF see the packet */
2943b0b41e6SRandall Stewart 		if (bpf_req && ifp)
2953b0b41e6SRandall Stewart 			ETHER_BPF_MTAP(ifp, m);
2963b0b41e6SRandall Stewart 		m_adj(m,  sizeof(*eh));
2973b0b41e6SRandall Stewart 		/* Trim off the ethernet header */
2983b0b41e6SRandall Stewart 		switch (etype) {
2993b0b41e6SRandall Stewart #ifdef INET6
3003b0b41e6SRandall Stewart 		case ETHERTYPE_IPV6:
3013b0b41e6SRandall Stewart 		{
3023b0b41e6SRandall Stewart 			if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
3033b0b41e6SRandall Stewart 				m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
3043b0b41e6SRandall Stewart 				if (m == NULL) {
3053b0b41e6SRandall Stewart 					TCPSTAT_INC(tcps_rcvshort);
3063b0b41e6SRandall Stewart 					m_freem(m);
3073b0b41e6SRandall Stewart 					goto skipped_pkt;
3083b0b41e6SRandall Stewart 				}
3093b0b41e6SRandall Stewart 			}
3103b0b41e6SRandall Stewart 			ip6 = (struct ip6_hdr *)(eh + 1);
3113b0b41e6SRandall Stewart 			th = (struct tcphdr *)(ip6 + 1);
3123b0b41e6SRandall Stewart 			tlen = ntohs(ip6->ip6_plen);
3133b0b41e6SRandall Stewart 			drop_hdrlen = sizeof(*ip6);
3143b0b41e6SRandall Stewart 			if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
3153b0b41e6SRandall Stewart 				if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
3163b0b41e6SRandall Stewart 					th->th_sum = m->m_pkthdr.csum_data;
3173b0b41e6SRandall Stewart 				else
3183b0b41e6SRandall Stewart 					th->th_sum = in6_cksum_pseudo(ip6, tlen,
3193b0b41e6SRandall Stewart 								      IPPROTO_TCP, m->m_pkthdr.csum_data);
3203b0b41e6SRandall Stewart 				th->th_sum ^= 0xffff;
3213b0b41e6SRandall Stewart 			} else
3223b0b41e6SRandall Stewart 				th->th_sum = in6_cksum(m, IPPROTO_TCP, drop_hdrlen, tlen);
3233b0b41e6SRandall Stewart 			if (th->th_sum) {
3243b0b41e6SRandall Stewart 				TCPSTAT_INC(tcps_rcvbadsum);
3253b0b41e6SRandall Stewart 				m_freem(m);
3263b0b41e6SRandall Stewart 				goto skipped_pkt;
3273b0b41e6SRandall Stewart 			}
3283b0b41e6SRandall Stewart 			/*
3293b0b41e6SRandall Stewart 			 * Be proactive about unspecified IPv6 address in source.
3303b0b41e6SRandall Stewart 			 * As we use all-zero to indicate unbounded/unconnected pcb,
3313b0b41e6SRandall Stewart 			 * unspecified IPv6 address can be used to confuse us.
3323b0b41e6SRandall Stewart 			 *
3333b0b41e6SRandall Stewart 			 * Note that packets with unspecified IPv6 destination is
3343b0b41e6SRandall Stewart 			 * already dropped in ip6_input.
3353b0b41e6SRandall Stewart 			 */
3363b0b41e6SRandall Stewart 			if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
3373b0b41e6SRandall Stewart 				/* XXX stat */
3383b0b41e6SRandall Stewart 				m_freem(m);
3393b0b41e6SRandall Stewart 				goto skipped_pkt;
3403b0b41e6SRandall Stewart 			}
3413b0b41e6SRandall Stewart 			iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
3423b0b41e6SRandall Stewart 			break;
3433b0b41e6SRandall Stewart 		}
3443b0b41e6SRandall Stewart #endif
3453b0b41e6SRandall Stewart #ifdef INET
3463b0b41e6SRandall Stewart 		case ETHERTYPE_IP:
3473b0b41e6SRandall Stewart 		{
3483b0b41e6SRandall Stewart 			if (m->m_len < sizeof (struct tcpiphdr)) {
3493b0b41e6SRandall Stewart 				if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
3503b0b41e6SRandall Stewart 				    == NULL) {
3513b0b41e6SRandall Stewart 					TCPSTAT_INC(tcps_rcvshort);
3523b0b41e6SRandall Stewart 					m_freem(m);
3533b0b41e6SRandall Stewart 					goto skipped_pkt;
3543b0b41e6SRandall Stewart 				}
3553b0b41e6SRandall Stewart 			}
3563b0b41e6SRandall Stewart 			ip = (struct ip *)(eh + 1);
3573b0b41e6SRandall Stewart 			th = (struct tcphdr *)(ip + 1);
3583b0b41e6SRandall Stewart 			drop_hdrlen = sizeof(*ip);
3593b0b41e6SRandall Stewart 			iptos = ip->ip_tos;
3603b0b41e6SRandall Stewart 			tlen = ntohs(ip->ip_len) - sizeof(struct ip);
3613b0b41e6SRandall Stewart 			if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
3623b0b41e6SRandall Stewart 				if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
3633b0b41e6SRandall Stewart 					th->th_sum = m->m_pkthdr.csum_data;
3643b0b41e6SRandall Stewart 				else
3653b0b41e6SRandall Stewart 					th->th_sum = in_pseudo(ip->ip_src.s_addr,
3663b0b41e6SRandall Stewart 							       ip->ip_dst.s_addr,
3673b0b41e6SRandall Stewart 							       htonl(m->m_pkthdr.csum_data + tlen +
3683b0b41e6SRandall Stewart 								     IPPROTO_TCP));
3693b0b41e6SRandall Stewart 				th->th_sum ^= 0xffff;
3703b0b41e6SRandall Stewart 			} else {
3713b0b41e6SRandall Stewart 				int len;
3723b0b41e6SRandall Stewart 				struct ipovly *ipov = (struct ipovly *)ip;
3733b0b41e6SRandall Stewart 				/*
3743b0b41e6SRandall Stewart 				 * Checksum extended TCP header and data.
3753b0b41e6SRandall Stewart 				 */
3763b0b41e6SRandall Stewart 				len = drop_hdrlen + tlen;
3773b0b41e6SRandall Stewart 				bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
3783b0b41e6SRandall Stewart 				ipov->ih_len = htons(tlen);
3793b0b41e6SRandall Stewart 				th->th_sum = in_cksum(m, len);
3803b0b41e6SRandall Stewart 				/* Reset length for SDT probes. */
3813b0b41e6SRandall Stewart 				ip->ip_len = htons(len);
3823b0b41e6SRandall Stewart 				/* Reset TOS bits */
3833b0b41e6SRandall Stewart 				ip->ip_tos = iptos;
3843b0b41e6SRandall Stewart 				/* Re-initialization for later version check */
3853b0b41e6SRandall Stewart 				ip->ip_v = IPVERSION;
3863b0b41e6SRandall Stewart 				ip->ip_hl = sizeof(*ip) >> 2;
3873b0b41e6SRandall Stewart 			}
3883b0b41e6SRandall Stewart 			if (th->th_sum) {
3893b0b41e6SRandall Stewart 				TCPSTAT_INC(tcps_rcvbadsum);
3903b0b41e6SRandall Stewart 				m_freem(m);
3913b0b41e6SRandall Stewart 				goto skipped_pkt;
3923b0b41e6SRandall Stewart 			}
3933b0b41e6SRandall Stewart 			break;
3943b0b41e6SRandall Stewart 		}
3953b0b41e6SRandall Stewart #endif
3963b0b41e6SRandall Stewart 		}
3973b0b41e6SRandall Stewart 		/*
3983b0b41e6SRandall Stewart 		 * Convert TCP protocol specific fields to host format.
3993b0b41e6SRandall Stewart 		 */
4003b0b41e6SRandall Stewart 		tcp_fields_to_host(th);
4013b0b41e6SRandall Stewart 
4023b0b41e6SRandall Stewart 		off = th->th_off << 2;
4033b0b41e6SRandall Stewart 		if (off < sizeof (struct tcphdr) || off > tlen) {
4043b0b41e6SRandall Stewart 			TCPSTAT_INC(tcps_rcvbadoff);
4053b0b41e6SRandall Stewart 				m_freem(m);
4063b0b41e6SRandall Stewart 				goto skipped_pkt;
4073b0b41e6SRandall Stewart 		}
4083b0b41e6SRandall Stewart 		tlen -= off;
4093b0b41e6SRandall Stewart 		drop_hdrlen += off;
4103b0b41e6SRandall Stewart 		/*
4113b0b41e6SRandall Stewart 		 * Now lets setup the timeval to be when we should
4123b0b41e6SRandall Stewart 		 * have been called (if we can).
4133b0b41e6SRandall Stewart 		 */
4143b0b41e6SRandall Stewart 		m->m_pkthdr.lro_nsegs = 1;
41535c7bb34SRandall Stewart 		if (m->m_flags & M_TSTMP_LRO) {
41635c7bb34SRandall Stewart 			tv.tv_sec = m->m_pkthdr.rcv_tstmp /1000000000;
41735c7bb34SRandall Stewart 			tv.tv_usec = (m->m_pkthdr.rcv_tstmp % 1000000000)/1000;
41835c7bb34SRandall Stewart 		} else {
41935c7bb34SRandall Stewart 			/* Should not be should we kassert instead? */
4203b0b41e6SRandall Stewart 			tcp_get_usecs(&tv);
42135c7bb34SRandall Stewart 		}
4223b0b41e6SRandall Stewart 		/* Now what about next packet? */
4233b0b41e6SRandall Stewart 		if (m_save || has_pkt)
4243b0b41e6SRandall Stewart 			nxt_pkt = 1;
4253b0b41e6SRandall Stewart 		else
4263b0b41e6SRandall Stewart 			nxt_pkt = 0;
4273b0b41e6SRandall Stewart 		retval = (*tp->t_fb->tfb_do_segment_nounlock)(m, th, so, tp, drop_hdrlen, tlen,
4283b0b41e6SRandall Stewart 							      iptos, nxt_pkt, &tv);
4293b0b41e6SRandall Stewart 		if (retval) {
4303b0b41e6SRandall Stewart 			/* We lost the lock and tcb probably */
4313b0b41e6SRandall Stewart 			m = m_save;
4323b0b41e6SRandall Stewart 			while(m) {
4333b0b41e6SRandall Stewart 				m_save = m->m_nextpkt;
4343b0b41e6SRandall Stewart 				m->m_nextpkt = NULL;
4353b0b41e6SRandall Stewart 				m_freem(m);
4363b0b41e6SRandall Stewart 				m = m_save;
4373b0b41e6SRandall Stewart 			}
4383b0b41e6SRandall Stewart 			if (no_vn == 0)
4393b0b41e6SRandall Stewart 				CURVNET_RESTORE();
4403b0b41e6SRandall Stewart 			return(retval);
4413b0b41e6SRandall Stewart 		}
4423b0b41e6SRandall Stewart skipped_pkt:
4433b0b41e6SRandall Stewart 		m = m_save;
4443b0b41e6SRandall Stewart 	}
4453b0b41e6SRandall Stewart 	if (no_vn == 0)
4463b0b41e6SRandall Stewart 		CURVNET_RESTORE();
4473b0b41e6SRandall Stewart 	return(retval);
4483b0b41e6SRandall Stewart }
4493b0b41e6SRandall Stewart 
4503b0b41e6SRandall Stewart int
4513b0b41e6SRandall Stewart ctf_do_queued_segments(struct socket *so, struct tcpcb *tp, int have_pkt)
4523b0b41e6SRandall Stewart {
4533b0b41e6SRandall Stewart 	struct mbuf *m;
4543b0b41e6SRandall Stewart 
4553b0b41e6SRandall Stewart 	/* First lets see if we have old packets */
4563b0b41e6SRandall Stewart 	if (tp->t_in_pkt) {
4573b0b41e6SRandall Stewart 		m = tp->t_in_pkt;
4583b0b41e6SRandall Stewart 		tp->t_in_pkt = NULL;
4593b0b41e6SRandall Stewart 		tp->t_tail_pkt = NULL;
4603b0b41e6SRandall Stewart 		if (ctf_process_inbound_raw(tp, so, m, have_pkt)) {
4613b0b41e6SRandall Stewart 			/* We lost the tcpcb (maybe a RST came in)? */
4623b0b41e6SRandall Stewart 			return(1);
4633b0b41e6SRandall Stewart 		}
4643b0b41e6SRandall Stewart 	}
4653b0b41e6SRandall Stewart 	return (0);
4663b0b41e6SRandall Stewart }
4673b0b41e6SRandall Stewart 
4683b0b41e6SRandall Stewart uint32_t
4693b0b41e6SRandall Stewart ctf_outstanding(struct tcpcb *tp)
4703b0b41e6SRandall Stewart {
4713b0b41e6SRandall Stewart 	return(tp->snd_max - tp->snd_una);
4723b0b41e6SRandall Stewart }
4733b0b41e6SRandall Stewart 
4743b0b41e6SRandall Stewart uint32_t
4753b0b41e6SRandall Stewart ctf_flight_size(struct tcpcb *tp, uint32_t rc_sacked)
4763b0b41e6SRandall Stewart {
4773b0b41e6SRandall Stewart 	if (rc_sacked <= ctf_outstanding(tp))
4783b0b41e6SRandall Stewart 		return(ctf_outstanding(tp) - rc_sacked);
4793b0b41e6SRandall Stewart 	else {
4803b0b41e6SRandall Stewart 		/* TSNH */
4813b0b41e6SRandall Stewart #ifdef INVARIANTS
4823b0b41e6SRandall Stewart 		panic("tp:%p rc_sacked:%d > out:%d",
4833b0b41e6SRandall Stewart 		      tp, rc_sacked, ctf_outstanding(tp));
4843b0b41e6SRandall Stewart #endif
4853b0b41e6SRandall Stewart 		return (0);
4863b0b41e6SRandall Stewart 	}
4873b0b41e6SRandall Stewart }
4883b0b41e6SRandall Stewart 
4893b0b41e6SRandall Stewart void
4903b0b41e6SRandall Stewart ctf_do_dropwithreset(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th,
4913b0b41e6SRandall Stewart     int32_t rstreason, int32_t tlen)
4923b0b41e6SRandall Stewart {
4933b0b41e6SRandall Stewart 	if (tp != NULL) {
4943b0b41e6SRandall Stewart 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
4953b0b41e6SRandall Stewart 		INP_WUNLOCK(tp->t_inpcb);
4963b0b41e6SRandall Stewart 	} else
4973b0b41e6SRandall Stewart 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
4983b0b41e6SRandall Stewart }
4993b0b41e6SRandall Stewart 
5003b0b41e6SRandall Stewart /*
5013b0b41e6SRandall Stewart  * ctf_drop_checks returns 1 for you should not proceed. It places
5023b0b41e6SRandall Stewart  * in ret_val what should be returned 1/0 by the caller. The 1 indicates
5033b0b41e6SRandall Stewart  * that the TCB is unlocked and probably dropped. The 0 indicates the
5043b0b41e6SRandall Stewart  * TCB is still valid and locked.
5053b0b41e6SRandall Stewart  */
5063b0b41e6SRandall Stewart int
5073b0b41e6SRandall Stewart ctf_drop_checks(struct tcpopt *to, struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, int32_t * tlenp,  int32_t * thf, int32_t * drop_hdrlen, int32_t * ret_val)
5083b0b41e6SRandall Stewart {
5093b0b41e6SRandall Stewart 	int32_t todrop;
5103b0b41e6SRandall Stewart 	int32_t thflags;
5113b0b41e6SRandall Stewart 	int32_t tlen;
5123b0b41e6SRandall Stewart 
5133b0b41e6SRandall Stewart 	thflags = *thf;
5143b0b41e6SRandall Stewart 	tlen = *tlenp;
5153b0b41e6SRandall Stewart 	todrop = tp->rcv_nxt - th->th_seq;
5163b0b41e6SRandall Stewart 	if (todrop > 0) {
5173b0b41e6SRandall Stewart 		if (thflags & TH_SYN) {
5183b0b41e6SRandall Stewart 			thflags &= ~TH_SYN;
5193b0b41e6SRandall Stewart 			th->th_seq++;
5203b0b41e6SRandall Stewart 			if (th->th_urp > 1)
5213b0b41e6SRandall Stewart 				th->th_urp--;
5223b0b41e6SRandall Stewart 			else
5233b0b41e6SRandall Stewart 				thflags &= ~TH_URG;
5243b0b41e6SRandall Stewart 			todrop--;
5253b0b41e6SRandall Stewart 		}
5263b0b41e6SRandall Stewart 		/*
5273b0b41e6SRandall Stewart 		 * Following if statement from Stevens, vol. 2, p. 960.
5283b0b41e6SRandall Stewart 		 */
5293b0b41e6SRandall Stewart 		if (todrop > tlen
5303b0b41e6SRandall Stewart 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
5313b0b41e6SRandall Stewart 			/*
5323b0b41e6SRandall Stewart 			 * Any valid FIN must be to the left of the window.
5333b0b41e6SRandall Stewart 			 * At this point the FIN must be a duplicate or out
5343b0b41e6SRandall Stewart 			 * of sequence; drop it.
5353b0b41e6SRandall Stewart 			 */
5363b0b41e6SRandall Stewart 			thflags &= ~TH_FIN;
5373b0b41e6SRandall Stewart 			/*
5383b0b41e6SRandall Stewart 			 * Send an ACK to resynchronize and drop any data.
5393b0b41e6SRandall Stewart 			 * But keep on processing for RST or ACK.
5403b0b41e6SRandall Stewart 			 */
5413b0b41e6SRandall Stewart 			tp->t_flags |= TF_ACKNOW;
5423b0b41e6SRandall Stewart 			todrop = tlen;
5433b0b41e6SRandall Stewart 			TCPSTAT_INC(tcps_rcvduppack);
5443b0b41e6SRandall Stewart 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
5453b0b41e6SRandall Stewart 		} else {
5463b0b41e6SRandall Stewart 			TCPSTAT_INC(tcps_rcvpartduppack);
5473b0b41e6SRandall Stewart 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
5483b0b41e6SRandall Stewart 		}
5493b0b41e6SRandall Stewart 		/*
5503b0b41e6SRandall Stewart 		 * DSACK - add SACK block for dropped range
5513b0b41e6SRandall Stewart 		 */
5523b0b41e6SRandall Stewart 		if (tp->t_flags & TF_SACK_PERMIT) {
553fe5dee73SMichael Tuexen 			tcp_update_sack_list(tp, th->th_seq,
554fe5dee73SMichael Tuexen 			    th->th_seq + todrop);
5553b0b41e6SRandall Stewart 			/*
5563b0b41e6SRandall Stewart 			 * ACK now, as the next in-sequence segment
5573b0b41e6SRandall Stewart 			 * will clear the DSACK block again
5583b0b41e6SRandall Stewart 			 */
5593b0b41e6SRandall Stewart 			tp->t_flags |= TF_ACKNOW;
5603b0b41e6SRandall Stewart 		}
5613b0b41e6SRandall Stewart 		*drop_hdrlen += todrop;	/* drop from the top afterwards */
5623b0b41e6SRandall Stewart 		th->th_seq += todrop;
5633b0b41e6SRandall Stewart 		tlen -= todrop;
5643b0b41e6SRandall Stewart 		if (th->th_urp > todrop)
5653b0b41e6SRandall Stewart 			th->th_urp -= todrop;
5663b0b41e6SRandall Stewart 		else {
5673b0b41e6SRandall Stewart 			thflags &= ~TH_URG;
5683b0b41e6SRandall Stewart 			th->th_urp = 0;
5693b0b41e6SRandall Stewart 		}
5703b0b41e6SRandall Stewart 	}
5713b0b41e6SRandall Stewart 	/*
5723b0b41e6SRandall Stewart 	 * If segment ends after window, drop trailing data (and PUSH and
5733b0b41e6SRandall Stewart 	 * FIN); if nothing left, just ACK.
5743b0b41e6SRandall Stewart 	 */
5753b0b41e6SRandall Stewart 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
5763b0b41e6SRandall Stewart 	if (todrop > 0) {
5773b0b41e6SRandall Stewart 		TCPSTAT_INC(tcps_rcvpackafterwin);
5783b0b41e6SRandall Stewart 		if (todrop >= tlen) {
5793b0b41e6SRandall Stewart 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
5803b0b41e6SRandall Stewart 			/*
5813b0b41e6SRandall Stewart 			 * If window is closed can only take segments at
5823b0b41e6SRandall Stewart 			 * window edge, and have to drop data and PUSH from
5833b0b41e6SRandall Stewart 			 * incoming segments.  Continue processing, but
5843b0b41e6SRandall Stewart 			 * remember to ack.  Otherwise, drop segment and
5853b0b41e6SRandall Stewart 			 * ack.
5863b0b41e6SRandall Stewart 			 */
5873b0b41e6SRandall Stewart 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
5883b0b41e6SRandall Stewart 				tp->t_flags |= TF_ACKNOW;
5893b0b41e6SRandall Stewart 				TCPSTAT_INC(tcps_rcvwinprobe);
5903b0b41e6SRandall Stewart 			} else {
5913b0b41e6SRandall Stewart 				ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
5923b0b41e6SRandall Stewart 				return (1);
5933b0b41e6SRandall Stewart 			}
5943b0b41e6SRandall Stewart 		} else
5953b0b41e6SRandall Stewart 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
5963b0b41e6SRandall Stewart 		m_adj(m, -todrop);
5973b0b41e6SRandall Stewart 		tlen -= todrop;
5983b0b41e6SRandall Stewart 		thflags &= ~(TH_PUSH | TH_FIN);
5993b0b41e6SRandall Stewart 	}
6003b0b41e6SRandall Stewart 	*thf = thflags;
6013b0b41e6SRandall Stewart 	*tlenp = tlen;
6023b0b41e6SRandall Stewart 	return (0);
6033b0b41e6SRandall Stewart }
6043b0b41e6SRandall Stewart 
6053b0b41e6SRandall Stewart /*
6063b0b41e6SRandall Stewart  * The value in ret_val informs the caller
6073b0b41e6SRandall Stewart  * if we dropped the tcb (and lock) or not.
6083b0b41e6SRandall Stewart  * 1 = we dropped it, 0 = the TCB is still locked
6093b0b41e6SRandall Stewart  * and valid.
6103b0b41e6SRandall Stewart  */
6113b0b41e6SRandall Stewart void
6123b0b41e6SRandall Stewart ctf_do_dropafterack(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th, int32_t thflags, int32_t tlen, int32_t * ret_val)
6133b0b41e6SRandall Stewart {
6143b0b41e6SRandall Stewart 	/*
6153b0b41e6SRandall Stewart 	 * Generate an ACK dropping incoming segment if it occupies sequence
6163b0b41e6SRandall Stewart 	 * space, where the ACK reflects our state.
6173b0b41e6SRandall Stewart 	 *
6183b0b41e6SRandall Stewart 	 * We can now skip the test for the RST flag since all paths to this
6193b0b41e6SRandall Stewart 	 * code happen after packets containing RST have been dropped.
6203b0b41e6SRandall Stewart 	 *
6213b0b41e6SRandall Stewart 	 * In the SYN-RECEIVED state, don't send an ACK unless the segment
6223b0b41e6SRandall Stewart 	 * we received passes the SYN-RECEIVED ACK test. If it fails send a
6233b0b41e6SRandall Stewart 	 * RST.  This breaks the loop in the "LAND" DoS attack, and also
6243b0b41e6SRandall Stewart 	 * prevents an ACK storm between two listening ports that have been
6253b0b41e6SRandall Stewart 	 * sent forged SYN segments, each with the source address of the
6263b0b41e6SRandall Stewart 	 * other.
6273b0b41e6SRandall Stewart 	 */
6283b0b41e6SRandall Stewart 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
6293b0b41e6SRandall Stewart 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
6303b0b41e6SRandall Stewart 	    SEQ_GT(th->th_ack, tp->snd_max))) {
6313b0b41e6SRandall Stewart 		*ret_val = 1;
6323b0b41e6SRandall Stewart 		ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
6333b0b41e6SRandall Stewart 		return;
6343b0b41e6SRandall Stewart 	} else
6353b0b41e6SRandall Stewart 		*ret_val = 0;
6363b0b41e6SRandall Stewart 	tp->t_flags |= TF_ACKNOW;
6373b0b41e6SRandall Stewart 	if (m)
6383b0b41e6SRandall Stewart 		m_freem(m);
6393b0b41e6SRandall Stewart }
6403b0b41e6SRandall Stewart 
6413b0b41e6SRandall Stewart void
6423b0b41e6SRandall Stewart ctf_do_drop(struct mbuf *m, struct tcpcb *tp)
6433b0b41e6SRandall Stewart {
6443b0b41e6SRandall Stewart 
6453b0b41e6SRandall Stewart 	/*
6463b0b41e6SRandall Stewart 	 * Drop space held by incoming segment and return.
6473b0b41e6SRandall Stewart 	 */
6483b0b41e6SRandall Stewart 	if (tp != NULL)
6493b0b41e6SRandall Stewart 		INP_WUNLOCK(tp->t_inpcb);
6503b0b41e6SRandall Stewart 	if (m)
6513b0b41e6SRandall Stewart 		m_freem(m);
6523b0b41e6SRandall Stewart }
6533b0b41e6SRandall Stewart 
6543b0b41e6SRandall Stewart int
6553b0b41e6SRandall Stewart ctf_process_rst(struct mbuf *m, struct tcphdr *th, struct socket *so, struct tcpcb *tp)
6563b0b41e6SRandall Stewart {
6573b0b41e6SRandall Stewart 	/*
6583b0b41e6SRandall Stewart 	 * RFC5961 Section 3.2
6593b0b41e6SRandall Stewart 	 *
6603b0b41e6SRandall Stewart 	 * - RST drops connection only if SEG.SEQ == RCV.NXT. - If RST is in
6613b0b41e6SRandall Stewart 	 * window, we send challenge ACK.
6623b0b41e6SRandall Stewart 	 *
6633b0b41e6SRandall Stewart 	 * Note: to take into account delayed ACKs, we should test against
6643b0b41e6SRandall Stewart 	 * last_ack_sent instead of rcv_nxt. Note 2: we handle special case
6653b0b41e6SRandall Stewart 	 * of closed window, not covered by the RFC.
6663b0b41e6SRandall Stewart 	 */
6673b0b41e6SRandall Stewart 	int dropped = 0;
6683b0b41e6SRandall Stewart 
6693b0b41e6SRandall Stewart 	if ((SEQ_GEQ(th->th_seq, (tp->last_ack_sent - 1)) &&
6703b0b41e6SRandall Stewart 	    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
6713b0b41e6SRandall Stewart 	    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
6723b0b41e6SRandall Stewart 
6733b0b41e6SRandall Stewart 		KASSERT(tp->t_state != TCPS_SYN_SENT,
6743b0b41e6SRandall Stewart 		    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
6753b0b41e6SRandall Stewart 		    __func__, th, tp));
6763b0b41e6SRandall Stewart 
6773b0b41e6SRandall Stewart 		if (V_tcp_insecure_rst ||
6783b0b41e6SRandall Stewart 		    (tp->last_ack_sent == th->th_seq) ||
6793b0b41e6SRandall Stewart 		    (tp->rcv_nxt == th->th_seq) ||
6803b0b41e6SRandall Stewart 		    ((tp->last_ack_sent - 1) == th->th_seq)) {
6813b0b41e6SRandall Stewart 			TCPSTAT_INC(tcps_drops);
6823b0b41e6SRandall Stewart 			/* Drop the connection. */
6833b0b41e6SRandall Stewart 			switch (tp->t_state) {
6843b0b41e6SRandall Stewart 			case TCPS_SYN_RECEIVED:
6853b0b41e6SRandall Stewart 				so->so_error = ECONNREFUSED;
6863b0b41e6SRandall Stewart 				goto close;
6873b0b41e6SRandall Stewart 			case TCPS_ESTABLISHED:
6883b0b41e6SRandall Stewart 			case TCPS_FIN_WAIT_1:
6893b0b41e6SRandall Stewart 			case TCPS_FIN_WAIT_2:
6903b0b41e6SRandall Stewart 			case TCPS_CLOSE_WAIT:
6913b0b41e6SRandall Stewart 			case TCPS_CLOSING:
6923b0b41e6SRandall Stewart 			case TCPS_LAST_ACK:
6933b0b41e6SRandall Stewart 				so->so_error = ECONNRESET;
6943b0b41e6SRandall Stewart 		close:
6953b0b41e6SRandall Stewart 				tcp_state_change(tp, TCPS_CLOSED);
6963b0b41e6SRandall Stewart 				/* FALLTHROUGH */
6973b0b41e6SRandall Stewart 			default:
6983b0b41e6SRandall Stewart 				tp = tcp_close(tp);
6993b0b41e6SRandall Stewart 			}
7003b0b41e6SRandall Stewart 			dropped = 1;
7013b0b41e6SRandall Stewart 			ctf_do_drop(m, tp);
7023b0b41e6SRandall Stewart 		} else {
7033b0b41e6SRandall Stewart 			TCPSTAT_INC(tcps_badrst);
7043b0b41e6SRandall Stewart 			/* Send challenge ACK. */
7053b0b41e6SRandall Stewart 			tcp_respond(tp, mtod(m, void *), th, m,
7063b0b41e6SRandall Stewart 			    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
7073b0b41e6SRandall Stewart 			tp->last_ack_sent = tp->rcv_nxt;
7083b0b41e6SRandall Stewart 		}
7093b0b41e6SRandall Stewart 	} else {
7103b0b41e6SRandall Stewart 		m_freem(m);
7113b0b41e6SRandall Stewart 	}
7123b0b41e6SRandall Stewart 	return (dropped);
7133b0b41e6SRandall Stewart }
7143b0b41e6SRandall Stewart 
7153b0b41e6SRandall Stewart /*
7163b0b41e6SRandall Stewart  * The value in ret_val informs the caller
7173b0b41e6SRandall Stewart  * if we dropped the tcb (and lock) or not.
7183b0b41e6SRandall Stewart  * 1 = we dropped it, 0 = the TCB is still locked
7193b0b41e6SRandall Stewart  * and valid.
7203b0b41e6SRandall Stewart  */
7213b0b41e6SRandall Stewart void
7223b0b41e6SRandall Stewart ctf_challenge_ack(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, int32_t * ret_val)
7233b0b41e6SRandall Stewart {
724d40c0d47SGleb Smirnoff 
725d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
7263b0b41e6SRandall Stewart 
7273b0b41e6SRandall Stewart 	TCPSTAT_INC(tcps_badsyn);
7283b0b41e6SRandall Stewart 	if (V_tcp_insecure_syn &&
7293b0b41e6SRandall Stewart 	    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
7303b0b41e6SRandall Stewart 	    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
7313b0b41e6SRandall Stewart 		tp = tcp_drop(tp, ECONNRESET);
7323b0b41e6SRandall Stewart 		*ret_val = 1;
7333b0b41e6SRandall Stewart 		ctf_do_drop(m, tp);
7343b0b41e6SRandall Stewart 	} else {
7353b0b41e6SRandall Stewart 		/* Send challenge ACK. */
7363b0b41e6SRandall Stewart 		tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
7373b0b41e6SRandall Stewart 		    tp->snd_nxt, TH_ACK);
7383b0b41e6SRandall Stewart 		tp->last_ack_sent = tp->rcv_nxt;
7393b0b41e6SRandall Stewart 		m = NULL;
7403b0b41e6SRandall Stewart 		*ret_val = 0;
7413b0b41e6SRandall Stewart 		ctf_do_drop(m, NULL);
7423b0b41e6SRandall Stewart 	}
7433b0b41e6SRandall Stewart }
7443b0b41e6SRandall Stewart 
7453b0b41e6SRandall Stewart /*
7463b0b41e6SRandall Stewart  * bbr_ts_check returns 1 for you should not proceed, the state
7473b0b41e6SRandall Stewart  * machine should return. It places in ret_val what should
7483b0b41e6SRandall Stewart  * be returned 1/0 by the caller (hpts_do_segment). The 1 indicates
7493b0b41e6SRandall Stewart  * that the TCB is unlocked and probably dropped. The 0 indicates the
7503b0b41e6SRandall Stewart  * TCB is still valid and locked.
7513b0b41e6SRandall Stewart  */
7523b0b41e6SRandall Stewart int
7533b0b41e6SRandall Stewart ctf_ts_check(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
7543b0b41e6SRandall Stewart     int32_t tlen, int32_t thflags, int32_t * ret_val)
7553b0b41e6SRandall Stewart {
7563b0b41e6SRandall Stewart 
7573b0b41e6SRandall Stewart 	if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
7583b0b41e6SRandall Stewart 		/*
7593b0b41e6SRandall Stewart 		 * Invalidate ts_recent.  If this segment updates ts_recent,
7603b0b41e6SRandall Stewart 		 * the age will be reset later and ts_recent will get a
7613b0b41e6SRandall Stewart 		 * valid value.  If it does not, setting ts_recent to zero
7623b0b41e6SRandall Stewart 		 * will at least satisfy the requirement that zero be placed
7633b0b41e6SRandall Stewart 		 * in the timestamp echo reply when ts_recent isn't valid.
7643b0b41e6SRandall Stewart 		 * The age isn't reset until we get a valid ts_recent
7653b0b41e6SRandall Stewart 		 * because we don't want out-of-order segments to be dropped
7663b0b41e6SRandall Stewart 		 * when ts_recent is old.
7673b0b41e6SRandall Stewart 		 */
7683b0b41e6SRandall Stewart 		tp->ts_recent = 0;
7693b0b41e6SRandall Stewart 	} else {
7703b0b41e6SRandall Stewart 		TCPSTAT_INC(tcps_rcvduppack);
7713b0b41e6SRandall Stewart 		TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
7723b0b41e6SRandall Stewart 		TCPSTAT_INC(tcps_pawsdrop);
7733b0b41e6SRandall Stewart 		*ret_val = 0;
7743b0b41e6SRandall Stewart 		if (tlen) {
7753b0b41e6SRandall Stewart 			ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
7763b0b41e6SRandall Stewart 		} else {
7773b0b41e6SRandall Stewart 			ctf_do_drop(m, NULL);
7783b0b41e6SRandall Stewart 		}
7793b0b41e6SRandall Stewart 		return (1);
7803b0b41e6SRandall Stewart 	}
7813b0b41e6SRandall Stewart 	return (0);
7823b0b41e6SRandall Stewart }
7833b0b41e6SRandall Stewart 
7843b0b41e6SRandall Stewart void
7853b0b41e6SRandall Stewart ctf_calc_rwin(struct socket *so, struct tcpcb *tp)
7863b0b41e6SRandall Stewart {
7873b0b41e6SRandall Stewart 	int32_t win;
7883b0b41e6SRandall Stewart 
7893b0b41e6SRandall Stewart 	/*
7903b0b41e6SRandall Stewart 	 * Calculate amount of space in receive window, and then do TCP
7913b0b41e6SRandall Stewart 	 * input processing. Receive window is amount of space in rcv queue,
7923b0b41e6SRandall Stewart 	 * but not less than advertised window.
7933b0b41e6SRandall Stewart 	 */
7943b0b41e6SRandall Stewart 	win = sbspace(&so->so_rcv);
7953b0b41e6SRandall Stewart 	if (win < 0)
7963b0b41e6SRandall Stewart 		win = 0;
7973b0b41e6SRandall Stewart 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
7983b0b41e6SRandall Stewart }
7993b0b41e6SRandall Stewart 
8003b0b41e6SRandall Stewart void
8013b0b41e6SRandall Stewart ctf_do_dropwithreset_conn(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th,
8023b0b41e6SRandall Stewart     int32_t rstreason, int32_t tlen)
8033b0b41e6SRandall Stewart {
8043b0b41e6SRandall Stewart 
8053b0b41e6SRandall Stewart 	if (tp->t_inpcb) {
8063b0b41e6SRandall Stewart 		tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
8073b0b41e6SRandall Stewart 	}
8083b0b41e6SRandall Stewart 	tcp_dropwithreset(m, th, tp, tlen, rstreason);
8093b0b41e6SRandall Stewart 	INP_WUNLOCK(tp->t_inpcb);
8103b0b41e6SRandall Stewart }
8113b0b41e6SRandall Stewart 
8123b0b41e6SRandall Stewart uint32_t
8133b0b41e6SRandall Stewart ctf_fixed_maxseg(struct tcpcb *tp)
8143b0b41e6SRandall Stewart {
8153b0b41e6SRandall Stewart 	int optlen;
8163b0b41e6SRandall Stewart 
8173b0b41e6SRandall Stewart 	if (tp->t_flags & TF_NOOPT)
8183b0b41e6SRandall Stewart 		return (tp->t_maxseg);
8193b0b41e6SRandall Stewart 
8203b0b41e6SRandall Stewart 	/*
8213b0b41e6SRandall Stewart 	 * Here we have a simplified code from tcp_addoptions(),
8223b0b41e6SRandall Stewart 	 * without a proper loop, and having most of paddings hardcoded.
8233b0b41e6SRandall Stewart 	 * We only consider fixed options that we would send every
8243b0b41e6SRandall Stewart 	 * time I.e. SACK is not considered.
8253b0b41e6SRandall Stewart 	 *
8263b0b41e6SRandall Stewart 	 */
8273b0b41e6SRandall Stewart #define	PAD(len)	((((len) / 4) + !!((len) % 4)) * 4)
8283b0b41e6SRandall Stewart 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
8293b0b41e6SRandall Stewart 		if (tp->t_flags & TF_RCVD_TSTMP)
8303b0b41e6SRandall Stewart 			optlen = TCPOLEN_TSTAMP_APPA;
8313b0b41e6SRandall Stewart 		else
8323b0b41e6SRandall Stewart 			optlen = 0;
8333b0b41e6SRandall Stewart #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
8343b0b41e6SRandall Stewart 		if (tp->t_flags & TF_SIGNATURE)
8353b0b41e6SRandall Stewart 			optlen += PAD(TCPOLEN_SIGNATURE);
8363b0b41e6SRandall Stewart #endif
8373b0b41e6SRandall Stewart 	} else {
8383b0b41e6SRandall Stewart 		if (tp->t_flags & TF_REQ_TSTMP)
8393b0b41e6SRandall Stewart 			optlen = TCPOLEN_TSTAMP_APPA;
8403b0b41e6SRandall Stewart 		else
8413b0b41e6SRandall Stewart 			optlen = PAD(TCPOLEN_MAXSEG);
8423b0b41e6SRandall Stewart 		if (tp->t_flags & TF_REQ_SCALE)
8433b0b41e6SRandall Stewart 			optlen += PAD(TCPOLEN_WINDOW);
8443b0b41e6SRandall Stewart #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
8453b0b41e6SRandall Stewart 		if (tp->t_flags & TF_SIGNATURE)
8463b0b41e6SRandall Stewart 			optlen += PAD(TCPOLEN_SIGNATURE);
8473b0b41e6SRandall Stewart #endif
8483b0b41e6SRandall Stewart 		if (tp->t_flags & TF_SACK_PERMIT)
8493b0b41e6SRandall Stewart 			optlen += PAD(TCPOLEN_SACK_PERMITTED);
8503b0b41e6SRandall Stewart 	}
8513b0b41e6SRandall Stewart #undef PAD
8523b0b41e6SRandall Stewart 	optlen = min(optlen, TCP_MAXOLEN);
8533b0b41e6SRandall Stewart 	return (tp->t_maxseg - optlen);
8543b0b41e6SRandall Stewart }
8553b0b41e6SRandall Stewart 
8563b0b41e6SRandall Stewart void
8573b0b41e6SRandall Stewart ctf_log_sack_filter(struct tcpcb *tp, int num_sack_blks, struct sackblk *sack_blocks)
8583b0b41e6SRandall Stewart {
8593b0b41e6SRandall Stewart 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
8603b0b41e6SRandall Stewart 		union tcp_log_stackspecific log;
8613b0b41e6SRandall Stewart 		struct timeval tv;
8623b0b41e6SRandall Stewart 
8633b0b41e6SRandall Stewart 		memset(&log, 0, sizeof(log));
8643b0b41e6SRandall Stewart 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
8653b0b41e6SRandall Stewart 		log.u_bbr.flex8 = num_sack_blks;
8663b0b41e6SRandall Stewart 		if (num_sack_blks > 0) {
8673b0b41e6SRandall Stewart 			log.u_bbr.flex1 = sack_blocks[0].start;
8683b0b41e6SRandall Stewart 			log.u_bbr.flex2 = sack_blocks[0].end;
8693b0b41e6SRandall Stewart 		}
8703b0b41e6SRandall Stewart 		if (num_sack_blks > 1) {
8713b0b41e6SRandall Stewart 			log.u_bbr.flex3 = sack_blocks[1].start;
8723b0b41e6SRandall Stewart 			log.u_bbr.flex4 = sack_blocks[1].end;
8733b0b41e6SRandall Stewart 		}
8743b0b41e6SRandall Stewart 		if (num_sack_blks > 2) {
8753b0b41e6SRandall Stewart 			log.u_bbr.flex5 = sack_blocks[2].start;
8763b0b41e6SRandall Stewart 			log.u_bbr.flex6 = sack_blocks[2].end;
8773b0b41e6SRandall Stewart 		}
8783b0b41e6SRandall Stewart 		if (num_sack_blks > 3) {
8793b0b41e6SRandall Stewart 			log.u_bbr.applimited = sack_blocks[3].start;
8803b0b41e6SRandall Stewart 			log.u_bbr.pkts_out = sack_blocks[3].end;
8813b0b41e6SRandall Stewart 		}
8823b0b41e6SRandall Stewart 		TCP_LOG_EVENTP(tp, NULL,
8833b0b41e6SRandall Stewart 		    &tp->t_inpcb->inp_socket->so_rcv,
8843b0b41e6SRandall Stewart 		    &tp->t_inpcb->inp_socket->so_snd,
8853b0b41e6SRandall Stewart 		    TCP_SACK_FILTER_RES, 0,
8863b0b41e6SRandall Stewart 		    0, &log, false, &tv);
8873b0b41e6SRandall Stewart 	}
8883b0b41e6SRandall Stewart }
8893b0b41e6SRandall Stewart 
8903b0b41e6SRandall Stewart uint32_t
8913b0b41e6SRandall Stewart ctf_decay_count(uint32_t count, uint32_t decay)
8923b0b41e6SRandall Stewart {
8933b0b41e6SRandall Stewart 	/*
8943b0b41e6SRandall Stewart 	 * Given a count, decay it by a set percentage. The
8953b0b41e6SRandall Stewart 	 * percentage is in thousands i.e. 100% = 1000,
8963b0b41e6SRandall Stewart 	 * 19.3% = 193.
8973b0b41e6SRandall Stewart 	 */
8983b0b41e6SRandall Stewart 	uint64_t perc_count, decay_per;
8993b0b41e6SRandall Stewart 	uint32_t decayed_count;
9003b0b41e6SRandall Stewart 	if (decay > 1000) {
9013b0b41e6SRandall Stewart 		/* We don't raise it */
9023b0b41e6SRandall Stewart 		return (count);
9033b0b41e6SRandall Stewart 	}
9043b0b41e6SRandall Stewart 	perc_count = count;
9053b0b41e6SRandall Stewart 	decay_per = decay;
9063b0b41e6SRandall Stewart 	perc_count *= decay_per;
9073b0b41e6SRandall Stewart 	perc_count /= 1000;
9083b0b41e6SRandall Stewart 	/*
9093b0b41e6SRandall Stewart 	 * So now perc_count holds the
9103b0b41e6SRandall Stewart 	 * count decay value.
9113b0b41e6SRandall Stewart 	 */
9123b0b41e6SRandall Stewart 	decayed_count = count - (uint32_t)perc_count;
9133b0b41e6SRandall Stewart 	return(decayed_count);
9143b0b41e6SRandall Stewart }
915