#include <sys/cdefs.h>
#include "opt_inet.h"
#include "opt_inet6.h"
#include "opt_ipsec.h"
#include "opt_ratelimit.h"
#include <sys/param.h>
#include <sys/arb.h>
#include <sys/module.h>
#include <sys/kernel.h>
#ifdef TCP_HHOOK
#include <sys/hhook.h>
#endif
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/qmath.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/tree.h>
#ifdef NETFLIX_STATS
#include <sys/stats.h>
#endif
#include <sys/refcount.h>
#include <sys/queue.h>
#include <sys/smp.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/tim_filter.h>
#include <sys/time.h>
#include <vm/uma.h>
#include <sys/kern_prefetch.h>
#include <net/route.h>
#include <net/vnet.h>
#include <net/ethernet.h>
#include <net/bpf.h>
#define TCPSTATES
#include <netinet/in.h>
#include <netinet/in_kdtrace.h>
#include <netinet/in_pcb.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/ip6.h>
#include <netinet6/in6_pcb.h>
#include <netinet6/ip6_var.h>
#include <netinet/tcp.h>
#include <netinet/tcp_fsm.h>
#include <netinet/tcp_seq.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/tcpip.h>
#include <netinet/tcp_ecn.h>
#include <netinet/tcp_hpts.h>
#include <netinet/tcp_lro.h>
#include <netinet/cc/cc.h>
#include <netinet/tcp_log_buf.h>
#ifdef TCP_OFFLOAD
#include <netinet/tcp_offload.h>
#endif
#ifdef INET6
#include <netinet6/tcp6_var.h>
#endif
#include <netinet/tcp_fastopen.h>
#include <netipsec/ipsec_support.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_private.h>
#if defined(IPSEC) || defined(IPSEC_SUPPORT)
#include <netipsec/ipsec.h>
#include <netipsec/ipsec6.h>
#endif
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <machine/in_cksum.h>
#ifdef MAC
#include <security/mac/mac_framework.h>
#endif
#include "rack_bbr_common.h"
static int
ctf_get_enet_type(struct ifnet *ifp, struct mbuf *m)
{
struct ether_header *eh;
#ifdef INET6
struct ip6_hdr *ip6 = NULL;
#endif
#ifdef INET
struct ip *ip = NULL;
#endif
#if defined(INET) || defined(INET6)
struct tcphdr *th;
int32_t tlen;
uint16_t drop_hdrlen;
#endif
uint16_t etype;
#ifdef INET
uint8_t iptos;
#endif
if (m->m_flags & M_LRO_EHDRSTRP)
return (m->m_pkthdr.lro_etype);
if (bpf_peers_present(ifp->if_bpf))
ETHER_BPF_MTAP(ifp, m);
eh = mtod(m, struct ether_header *);
etype = ntohs(eh->ether_type);
m_adj(m, sizeof(*eh));
switch (etype) {
#ifdef INET6
case ETHERTYPE_IPV6:
{
if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
if (m == NULL) {
KMOD_TCPSTAT_INC(tcps_rcvshort);
return (-1);
}
}
ip6 = (struct ip6_hdr *)(eh + 1);
th = (struct tcphdr *)(ip6 + 1);
drop_hdrlen = sizeof(*ip6);
tlen = ntohs(ip6->ip6_plen);
if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
th->th_sum = m->m_pkthdr.csum_data;
else
th->th_sum = in6_cksum_pseudo(ip6, tlen,
IPPROTO_TCP,
m->m_pkthdr.csum_data);
th->th_sum ^= 0xffff;
} else
th->th_sum = in6_cksum(m, IPPROTO_TCP, drop_hdrlen, tlen);
if (th->th_sum) {
KMOD_TCPSTAT_INC(tcps_rcvbadsum);
m_freem(m);
return (-1);
}
return (etype);
}
#endif
#ifdef INET
case ETHERTYPE_IP:
{
if (m->m_len < sizeof (struct tcpiphdr)) {
m = m_pullup(m, sizeof (struct tcpiphdr));
if (m == NULL) {
KMOD_TCPSTAT_INC(tcps_rcvshort);
return (-1);
}
}
ip = (struct ip *)(eh + 1);
th = (struct tcphdr *)(ip + 1);
drop_hdrlen = sizeof(*ip);
iptos = ip->ip_tos;
tlen = ntohs(ip->ip_len) - sizeof(struct ip);
if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
th->th_sum = m->m_pkthdr.csum_data;
else
th->th_sum = in_pseudo(ip->ip_src.s_addr,
ip->ip_dst.s_addr,
htonl(m->m_pkthdr.csum_data + tlen + IPPROTO_TCP));
th->th_sum ^= 0xffff;
} else {
int len;
struct ipovly *ipov = (struct ipovly *)ip;
len = drop_hdrlen + tlen;
bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
ipov->ih_len = htons(tlen);
th->th_sum = in_cksum(m, len);
ip->ip_len = htons(len);
ip->ip_tos = iptos;
ip->ip_v = IPVERSION;
ip->ip_hl = sizeof(*ip) >> 2;
}
if (th->th_sum) {
KMOD_TCPSTAT_INC(tcps_rcvbadsum);
m_freem(m);
return (-1);
}
break;
}
#endif
};
return (etype);
}
static int
ctf_process_inbound_raw(struct tcpcb *tp, struct mbuf *m, int has_pkt)
{
struct mbuf *m_save;
struct tcphdr *th;
#ifdef INET6
struct ip6_hdr *ip6 = NULL;
#endif
#ifdef INET
struct ip *ip = NULL;
#endif
struct ifnet *ifp;
struct timeval tv;
struct inpcb *inp __diagused;
int32_t retval, nxt_pkt, tlen, off;
int etype = 0;
uint16_t drop_hdrlen;
uint8_t iptos;
inp = tptoinpcb(tp);
INP_WLOCK_ASSERT(inp);
NET_EPOCH_ASSERT();
KASSERT(m != NULL, ("ctf_process_inbound_raw: m == NULL"));
ifp = m_rcvif(m);
KASSERT(ifp != NULL, ("ctf_process_inbound_raw: ifp == NULL"));
CURVNET_SET(ifp->if_vnet);
tcp_get_usecs(&tv);
while (m) {
m_save = m->m_nextpkt;
m->m_nextpkt = NULL;
if ((m->m_flags & M_ACKCMP) == 0) {
etype = ctf_get_enet_type(ifp, m);
if (etype == -1) {
goto skipped_pkt;
}
KASSERT(((etype == ETHERTYPE_IPV6) || (etype == ETHERTYPE_IP)),
("tp:%p m:%p etype:0x%x -- not IP or IPv6", tp, m, etype));
switch (etype) {
#ifdef INET6
case ETHERTYPE_IPV6:
ip6 = mtod(m, struct ip6_hdr *);
th = (struct tcphdr *)(ip6 + 1);
tlen = ntohs(ip6->ip6_plen);
drop_hdrlen = sizeof(*ip6);
iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
break;
#endif
#ifdef INET
case ETHERTYPE_IP:
ip = mtod(m, struct ip *);
th = (struct tcphdr *)(ip + 1);
drop_hdrlen = sizeof(*ip);
iptos = ip->ip_tos;
tlen = ntohs(ip->ip_len) - sizeof(struct ip);
break;
#endif
}
off = th->th_off << 2;
if (off < sizeof (struct tcphdr) || off > tlen) {
printf("off:%d < hdrlen:%zu || > tlen:%u -- dump\n",
off,
sizeof(struct tcphdr),
tlen);
KMOD_TCPSTAT_INC(tcps_rcvbadoff);
m_freem(m);
goto skipped_pkt;
}
tlen -= off;
drop_hdrlen += off;
m->m_pkthdr.lro_nsegs = 1;
} else {
KASSERT((tp->t_flags2 & TF2_MBUF_ACKCMP),
("tp:%p no TF2_MBUF_ACKCMP flags?", tp));
tlen = 0;
drop_hdrlen = 0;
th = NULL;
iptos = 0;
}
tcp_get_usecs(&tv);
if (m_save || has_pkt)
nxt_pkt = 1;
else
nxt_pkt = 0;
if ((m->m_flags & M_ACKCMP) == 0)
KMOD_TCPSTAT_INC(tcps_rcvtotal);
else
KMOD_TCPSTAT_ADD(tcps_rcvtotal, (m->m_len / sizeof(struct tcp_ackent)));
retval = (*tp->t_fb->tfb_do_segment_nounlock)(tp, m, th,
drop_hdrlen, tlen, iptos, nxt_pkt, &tv);
if (retval) {
m = m_save;
while(m) {
m_save = m->m_nextpkt;
m->m_nextpkt = NULL;
m_freem(m);
m = m_save;
}
CURVNET_RESTORE();
INP_UNLOCK_ASSERT(inp);
return (retval);
}
skipped_pkt:
m = m_save;
}
CURVNET_RESTORE();
return (0);
}
int
ctf_do_queued_segments(struct tcpcb *tp, int have_pkt)
{
struct mbuf *m;
if ((m = STAILQ_FIRST(&tp->t_inqueue)) != NULL) {
STAILQ_INIT(&tp->t_inqueue);
if (ctf_process_inbound_raw(tp, m, have_pkt)) {
return(1);
}
}
return (0);
}
uint32_t
ctf_outstanding(struct tcpcb *tp)
{
uint32_t bytes_out;
bytes_out = tp->snd_max - tp->snd_una;
if (tp->t_state < TCPS_ESTABLISHED)
bytes_out++;
if (tp->t_flags & TF_SENTFIN)
bytes_out++;
return (bytes_out);
}
uint32_t
ctf_flight_size(struct tcpcb *tp, uint32_t rc_sacked)
{
if (rc_sacked <= ctf_outstanding(tp))
return(ctf_outstanding(tp) - rc_sacked);
else {
return (0);
}
}
void
ctf_do_dropwithreset(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th,
int32_t tlen)
{
tcp_dropwithreset(m, th, tp, tlen);
if (tp != NULL)
INP_WUNLOCK(tptoinpcb(tp));
}
void
ctf_ack_war_checks(struct tcpcb *tp)
{
sbintime_t now;
if ((V_tcp_ack_war_time_window > 0) && (V_tcp_ack_war_cnt > 0)) {
now = getsbinuptime();
if (tp->t_challenge_ack_end < now) {
tp->t_challenge_ack_cnt = 0;
tp->t_challenge_ack_end = now +
V_tcp_ack_war_time_window * SBT_1MS;
}
if (tp->t_challenge_ack_cnt < V_tcp_ack_war_cnt) {
tp->t_challenge_ack_cnt++;
tp->t_flags |= TF_ACKNOW;
} else
tp->t_flags &= ~TF_ACKNOW;
} else
tp->t_flags |= TF_ACKNOW;
}
int
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)
{
int32_t todrop;
int32_t thflags;
int32_t tlen;
thflags = *thf;
tlen = *tlenp;
todrop = tp->rcv_nxt - th->th_seq;
if (todrop > 0) {
if (thflags & TH_SYN) {
thflags &= ~TH_SYN;
th->th_seq++;
if (th->th_urp > 1)
th->th_urp--;
else
thflags &= ~TH_URG;
todrop--;
}
if (todrop > tlen
|| (todrop == tlen && (thflags & TH_FIN) == 0)) {
thflags &= ~TH_FIN;
ctf_ack_war_checks(tp);
todrop = tlen;
KMOD_TCPSTAT_INC(tcps_rcvduppack);
KMOD_TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
} else {
KMOD_TCPSTAT_INC(tcps_rcvpartduppack);
KMOD_TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
}
if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) {
ctf_ack_war_checks(tp);
if (tp->t_flags & TF_ACKNOW)
tcp_update_sack_list(tp, th->th_seq,
th->th_seq + todrop);
}
*drop_hdrlen += todrop;
th->th_seq += todrop;
tlen -= todrop;
if (th->th_urp > todrop)
th->th_urp -= todrop;
else {
thflags &= ~TH_URG;
th->th_urp = 0;
}
}
todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
if (todrop > 0) {
KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
if (todrop >= tlen) {
KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
ctf_ack_war_checks(tp);
KMOD_TCPSTAT_INC(tcps_rcvwinprobe);
} else {
ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
return (1);
}
} else
KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
m_adj(m, -todrop);
tlen -= todrop;
thflags &= ~(TH_PUSH | TH_FIN);
}
*thf = thflags;
*tlenp = tlen;
return (0);
}
void
ctf_do_dropafterack(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th, int32_t thflags, int32_t tlen, int32_t *ret_val)
{
if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
(SEQ_GT(tp->snd_una, th->th_ack) ||
SEQ_GT(th->th_ack, tp->snd_max))) {
*ret_val = 1;
ctf_do_dropwithreset(m, tp, th, tlen);
return;
} else
*ret_val = 0;
ctf_ack_war_checks(tp);
if (m)
m_freem(m);
}
void
ctf_do_drop(struct mbuf *m, struct tcpcb *tp)
{
if (tp != NULL)
INP_WUNLOCK(tptoinpcb(tp));
if (m)
m_freem(m);
}
int
ctf_process_rst(struct mbuf *m, struct tcphdr *th, struct socket *so,
struct tcpcb *tp)
{
int dropped = 0;
if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
(tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
KASSERT(tp->t_state != TCPS_SYN_SENT,
("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
__func__, th, tp));
if (V_tcp_insecure_rst ||
(tp->last_ack_sent == th->th_seq) ||
(tp->rcv_nxt == th->th_seq)) {
KMOD_TCPSTAT_INC(tcps_drops);
switch (tp->t_state) {
case TCPS_SYN_RECEIVED:
so->so_error = ECONNREFUSED;
goto close;
case TCPS_ESTABLISHED:
case TCPS_FIN_WAIT_1:
case TCPS_FIN_WAIT_2:
case TCPS_CLOSE_WAIT:
case TCPS_CLOSING:
case TCPS_LAST_ACK:
so->so_error = ECONNRESET;
close:
default:
tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_RST);
tp = tcp_close(tp);
}
dropped = 1;
ctf_do_drop(m, tp);
} else {
KMOD_TCPSTAT_INC(tcps_badrst);
tcp_send_challenge_ack(tp, th, m);
}
} else {
m_freem(m);
}
return (dropped);
}
void
ctf_challenge_ack(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, uint8_t iptos, int32_t * ret_val)
{
NET_EPOCH_ASSERT();
KMOD_TCPSTAT_INC(tcps_badsyn);
if (V_tcp_insecure_syn &&
SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
tp = tcp_drop(tp, ECONNRESET);
*ret_val = 1;
ctf_do_drop(m, tp);
} else {
tcp_ecn_input_syn_sent(tp, tcp_get_flags(th), iptos);
tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
tp->snd_nxt, TH_ACK);
tp->last_ack_sent = tp->rcv_nxt;
m = NULL;
*ret_val = 0;
ctf_do_drop(m, NULL);
}
}
int
ctf_ts_check(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
int32_t tlen, int32_t thflags, int32_t * ret_val)
{
if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
tp->ts_recent = 0;
} else {
KMOD_TCPSTAT_INC(tcps_rcvduppack);
KMOD_TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
KMOD_TCPSTAT_INC(tcps_pawsdrop);
*ret_val = 0;
if (tlen) {
ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
} else {
ctf_do_drop(m, NULL);
}
return (1);
}
return (0);
}
int
ctf_ts_check_ac(struct tcpcb *tp, int32_t thflags)
{
if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
tp->ts_recent = 0;
} else {
KMOD_TCPSTAT_INC(tcps_rcvduppack);
KMOD_TCPSTAT_INC(tcps_pawsdrop);
return (1);
}
return (0);
}
void
ctf_calc_rwin(struct socket *so, struct tcpcb *tp)
{
int32_t win;
win = sbspace(&so->so_rcv);
if (win < 0)
win = 0;
tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
}
void
ctf_do_dropwithreset_conn(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th,
int32_t tlen)
{
tcp_dropwithreset(m, th, tp, tlen);
tp = tcp_drop(tp, ETIMEDOUT);
if (tp)
INP_WUNLOCK(tptoinpcb(tp));
}
uint32_t
ctf_fixed_maxseg(struct tcpcb *tp)
{
return (tcp_fixed_maxseg(tp));
}
void
ctf_log_sack_filter(struct tcpcb *tp, int num_sack_blks, struct sackblk *sack_blocks)
{
if (tcp_bblogging_on(tp)) {
union tcp_log_stackspecific log;
struct timeval tv;
memset(&log, 0, sizeof(log));
log.u_bbr.timeStamp = tcp_get_usecs(&tv);
log.u_bbr.flex8 = num_sack_blks;
if (num_sack_blks > 0) {
log.u_bbr.flex1 = sack_blocks[0].start;
log.u_bbr.flex2 = sack_blocks[0].end;
}
if (num_sack_blks > 1) {
log.u_bbr.flex3 = sack_blocks[1].start;
log.u_bbr.flex4 = sack_blocks[1].end;
}
if (num_sack_blks > 2) {
log.u_bbr.flex5 = sack_blocks[2].start;
log.u_bbr.flex6 = sack_blocks[2].end;
}
if (num_sack_blks > 3) {
log.u_bbr.applimited = sack_blocks[3].start;
log.u_bbr.pkts_out = sack_blocks[3].end;
}
TCP_LOG_EVENTP(tp, NULL,
&tptosocket(tp)->so_rcv,
&tptosocket(tp)->so_snd,
TCP_SACK_FILTER_RES, 0,
0, &log, false, &tv);
}
}
uint32_t
ctf_decay_count(uint32_t count, uint32_t decay)
{
uint64_t perc_count, decay_per;
uint32_t decayed_count;
if (decay > 1000) {
return (count);
}
perc_count = count;
decay_per = decay;
perc_count *= decay_per;
perc_count /= 1000;
decayed_count = count - (uint32_t)perc_count;
return(decayed_count);
}
int32_t
ctf_progress_timeout_check(struct tcpcb *tp, bool log)
{
if (tp->t_maxunacktime && tp->t_acktime && TSTMP_GT(ticks, tp->t_acktime)) {
if ((ticks - tp->t_acktime) >= tp->t_maxunacktime) {
if (log)
tcp_log_end_status(tp, TCP_EI_STATUS_PROGRESS);
#ifdef NETFLIX_STATS
KMOD_TCPSTAT_INC(tcps_progdrops);
#endif
return (1);
}
}
return (0);
}