#ifdef _KERNEL
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: npf_state_tcp.c,v 1.21 2020/05/30 14:16:56 rmind Exp $");
#include <sys/param.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif
#include "npf_impl.h"
#define NPF_TCPS_OK 255
#define NPF_TCPS_CLOSED 0
#define NPF_TCPS_SYN_SENT 1
#define NPF_TCPS_SIMSYN_SENT 2
#define NPF_TCPS_SYN_RECEIVED 3
#define NPF_TCPS_ESTABLISHED 4
#define NPF_TCPS_FIN_SENT 5
#define NPF_TCPS_FIN_RECEIVED 6
#define NPF_TCPS_CLOSE_WAIT 7
#define NPF_TCPS_FIN_WAIT 8
#define NPF_TCPS_CLOSING 9
#define NPF_TCPS_LAST_ACK 10
#define NPF_TCPS_TIME_WAIT 11
#define NPF_TCP_NSTATES 12
#define NPF_TCPT_NEW 0
#define NPF_TCPT_ESTABLISHED 1
#define NPF_TCPT_HALFCLOSE 2
#define NPF_TCPT_CLOSE 3
#define NPF_TCPT_TIMEWAIT 4
#define NPF_TCPT_COUNT 5
typedef struct {
int max_ack_win;
int strict_order_rst;
int timeouts[NPF_TCPT_COUNT];
} npf_state_tcp_params_t;
#define SEQ_LT(a,b) ((int)((a)-(b)) < 0)
#define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0)
#define SEQ_GT(a,b) ((int)((a)-(b)) > 0)
#define SEQ_GEQ(a,b) ((int)((a)-(b)) >= 0)
#define TCPFC_INVALID 0
#define TCPFC_SYN 1
#define TCPFC_SYNACK 2
#define TCPFC_ACK 3
#define TCPFC_FIN 4
#define TCPFC_COUNT 5
static inline unsigned
npf_tcpfl2case(const unsigned tcpfl)
{
unsigned i, c;
CTASSERT(TH_FIN == 0x01);
CTASSERT(TH_SYN == 0x02);
CTASSERT(TH_ACK == 0x10);
i = (tcpfl & (TH_SYN | TH_FIN)) | ((tcpfl & TH_ACK) >> 2);
c = (0x2430140 >> (i << 2)) & 7;
KASSERT(c < TCPFC_COUNT);
return c;
}
static const uint8_t npf_tcp_fsm[NPF_TCP_NSTATES][2][TCPFC_COUNT] = {
[NPF_TCPS_CLOSED] = {
[NPF_FLOW_FORW] = {
[TCPFC_SYN] = NPF_TCPS_SYN_SENT,
},
},
[NPF_TCPS_SYN_SENT] = {
[NPF_FLOW_FORW] = {
[TCPFC_SYN] = NPF_TCPS_OK,
},
[NPF_FLOW_BACK] = {
[TCPFC_SYNACK] = NPF_TCPS_SYN_RECEIVED,
[TCPFC_SYN] = NPF_TCPS_SIMSYN_SENT,
},
},
[NPF_TCPS_SIMSYN_SENT] = {
[NPF_FLOW_FORW] = {
[TCPFC_SYN] = NPF_TCPS_OK,
[TCPFC_SYNACK] = NPF_TCPS_SYN_RECEIVED,
},
[NPF_FLOW_BACK] = {
[TCPFC_SYN] = NPF_TCPS_OK,
[TCPFC_SYNACK] = NPF_TCPS_SYN_RECEIVED,
[TCPFC_FIN] = NPF_TCPS_FIN_RECEIVED,
},
},
[NPF_TCPS_SYN_RECEIVED] = {
[NPF_FLOW_FORW] = {
[TCPFC_ACK] = NPF_TCPS_ESTABLISHED,
[TCPFC_FIN] = NPF_TCPS_FIN_SENT,
[TCPFC_SYN] = NPF_TCPS_OK,
},
[NPF_FLOW_BACK] = {
[TCPFC_SYNACK] = NPF_TCPS_OK,
[TCPFC_ACK] = NPF_TCPS_OK,
[TCPFC_FIN] = NPF_TCPS_FIN_RECEIVED,
},
},
[NPF_TCPS_ESTABLISHED] = {
[NPF_FLOW_FORW] = {
[TCPFC_ACK] = NPF_TCPS_OK,
[TCPFC_FIN] = NPF_TCPS_FIN_SENT,
},
[NPF_FLOW_BACK] = {
[TCPFC_ACK] = NPF_TCPS_OK,
[TCPFC_FIN] = NPF_TCPS_FIN_RECEIVED,
},
},
[NPF_TCPS_FIN_SENT] = {
[NPF_FLOW_FORW] = {
[TCPFC_ACK] = NPF_TCPS_OK,
[TCPFC_FIN] = NPF_TCPS_OK,
},
[NPF_FLOW_BACK] = {
[TCPFC_ACK] = NPF_TCPS_FIN_WAIT,
[TCPFC_FIN] = NPF_TCPS_CLOSING,
},
},
[NPF_TCPS_FIN_RECEIVED] = {
[NPF_FLOW_FORW] = {
[TCPFC_ACK] = NPF_TCPS_CLOSE_WAIT,
[TCPFC_FIN] = NPF_TCPS_CLOSING,
},
[NPF_FLOW_BACK] = {
[TCPFC_ACK] = NPF_TCPS_OK,
[TCPFC_FIN] = NPF_TCPS_OK,
},
},
[NPF_TCPS_CLOSE_WAIT] = {
[NPF_FLOW_FORW] = {
[TCPFC_ACK] = NPF_TCPS_OK,
[TCPFC_FIN] = NPF_TCPS_LAST_ACK,
},
[NPF_FLOW_BACK] = {
[TCPFC_ACK] = NPF_TCPS_OK,
[TCPFC_FIN] = NPF_TCPS_LAST_ACK,
},
},
[NPF_TCPS_FIN_WAIT] = {
[NPF_FLOW_FORW] = {
[TCPFC_ACK] = NPF_TCPS_OK,
[TCPFC_FIN] = NPF_TCPS_LAST_ACK,
},
[NPF_FLOW_BACK] = {
[TCPFC_ACK] = NPF_TCPS_OK,
[TCPFC_FIN] = NPF_TCPS_LAST_ACK,
},
},
[NPF_TCPS_CLOSING] = {
[NPF_FLOW_FORW] = {
[TCPFC_ACK] = NPF_TCPS_LAST_ACK,
},
[NPF_FLOW_BACK] = {
[TCPFC_ACK] = NPF_TCPS_LAST_ACK,
},
},
[NPF_TCPS_LAST_ACK] = {
[NPF_FLOW_FORW] = {
[TCPFC_ACK] = NPF_TCPS_TIME_WAIT,
},
[NPF_FLOW_BACK] = {
[TCPFC_ACK] = NPF_TCPS_TIME_WAIT,
},
},
[NPF_TCPS_TIME_WAIT] = {
[NPF_FLOW_FORW] = {
[TCPFC_SYN] = NPF_TCPS_SYN_SENT,
},
},
};
static bool
npf_tcp_inwindow(npf_cache_t *npc, npf_state_t *nst, const npf_flow_t flow)
{
const npf_state_tcp_params_t *params;
const struct tcphdr * const th = npc->npc_l4.tcp;
const int tcpfl = th->th_flags;
npf_tcpstate_t *fstate, *tstate;
int tcpdlen, ackskew;
tcp_seq seq, ack, end;
uint32_t win;
params = npc->npc_ctx->params[NPF_PARAMS_TCP_STATE];
KASSERT(npf_iscached(npc, NPC_TCP));
tcpdlen = npf_tcpsaw(__UNCONST(npc), &seq, &ack, &win);
end = seq + tcpdlen;
if (tcpfl & TH_SYN) {
end++;
}
if (tcpfl & TH_FIN) {
end++;
}
fstate = &nst->nst_tcpst[flow];
tstate = &nst->nst_tcpst[!flow];
win = win ? (win << fstate->nst_wscale) : 1;
if (__predict_false(fstate->nst_maxwin == 0)) {
fstate->nst_end = end;
fstate->nst_maxend = end;
fstate->nst_maxwin = win;
tstate->nst_end = 0;
tstate->nst_maxend = 0;
tstate->nst_maxwin = 1;
fstate->nst_wscale = 0;
(void)npf_fetch_tcpopts(npc, NULL, &fstate->nst_wscale);
tstate->nst_wscale = 0;
return true;
}
if (fstate->nst_end == 0) {
fstate->nst_end = end;
fstate->nst_maxend = end + 1;
fstate->nst_maxwin = win;
fstate->nst_wscale = 0;
if (tcpfl & TH_SYN) {
(void)npf_fetch_tcpopts(npc, NULL, &fstate->nst_wscale);
}
}
if ((tcpfl & TH_ACK) == 0) {
ack = tstate->nst_end;
} else if ((tcpfl & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST) && ack == 0) {
ack = tstate->nst_end;
}
if (__predict_false(tcpfl & TH_RST)) {
if (seq == 0 && nst->nst_state == NPF_TCPS_SYN_SENT) {
end = fstate->nst_end;
seq = end;
}
if (params->strict_order_rst && (fstate->nst_end - seq) > 1) {
return false;
}
}
if (!SEQ_LEQ(end, fstate->nst_maxend)) {
npf_stats_inc(npc->npc_ctx, NPF_STAT_INVALID_STATE_TCP1);
return false;
}
if (!SEQ_GEQ(seq, fstate->nst_end - tstate->nst_maxwin)) {
npf_stats_inc(npc->npc_ctx, NPF_STAT_INVALID_STATE_TCP2);
return false;
}
ackskew = tstate->nst_end - ack;
if (ackskew < -(int)params->max_ack_win ||
ackskew > ((int)params->max_ack_win << fstate->nst_wscale)) {
npf_stats_inc(npc->npc_ctx, NPF_STAT_INVALID_STATE_TCP3);
return false;
}
if (ackskew < 0) {
tstate->nst_end = ack;
}
if (fstate->nst_maxwin < win) {
fstate->nst_maxwin = win;
}
if (SEQ_GT(end, fstate->nst_end)) {
fstate->nst_end = end;
}
if (SEQ_GEQ(ack + win, tstate->nst_maxend)) {
tstate->nst_maxend = ack + win;
}
return true;
}
bool
npf_state_tcp(npf_cache_t *npc, npf_state_t *nst, npf_flow_t flow)
{
const struct tcphdr * const th = npc->npc_l4.tcp;
const unsigned tcpfl = th->th_flags, state = nst->nst_state;
unsigned nstate;
KASSERT(nst->nst_state < NPF_TCP_NSTATES);
if (__predict_true((tcpfl & TH_RST) == 0)) {
const u_int flagcase = npf_tcpfl2case(tcpfl);
nstate = npf_tcp_fsm[state][flow][flagcase];
} else if (state == NPF_TCPS_TIME_WAIT) {
nstate = NPF_TCPS_OK;
} else {
nstate = NPF_TCPS_CLOSED;
}
if (!npf_tcp_inwindow(npc, nst, flow)) {
return false;
}
if (__predict_true(nstate == NPF_TCPS_OK)) {
return true;
}
nst->nst_state = nstate;
return true;
}
int
npf_state_tcp_timeout(npf_t *npf, const npf_state_t *nst)
{
static const uint8_t state_timeout_idx[NPF_TCP_NSTATES] = {
[NPF_TCPS_CLOSED] = NPF_TCPT_CLOSE,
[NPF_TCPS_SYN_SENT] = NPF_TCPT_NEW,
[NPF_TCPS_SIMSYN_SENT] = NPF_TCPT_NEW,
[NPF_TCPS_SYN_RECEIVED] = NPF_TCPT_NEW,
[NPF_TCPS_ESTABLISHED] = NPF_TCPT_ESTABLISHED,
[NPF_TCPS_FIN_SENT] = NPF_TCPT_HALFCLOSE,
[NPF_TCPS_FIN_RECEIVED] = NPF_TCPT_HALFCLOSE,
[NPF_TCPS_CLOSE_WAIT] = NPF_TCPT_HALFCLOSE,
[NPF_TCPS_FIN_WAIT] = NPF_TCPT_HALFCLOSE,
[NPF_TCPS_CLOSING] = NPF_TCPT_CLOSE,
[NPF_TCPS_LAST_ACK] = NPF_TCPT_CLOSE,
[NPF_TCPS_TIME_WAIT] = NPF_TCPT_TIMEWAIT,
};
const npf_state_tcp_params_t *params;
const unsigned state = nst->nst_state;
KASSERT(state < NPF_TCP_NSTATES);
params = npf->params[NPF_PARAMS_TCP_STATE];
return params->timeouts[state_timeout_idx[state]];
}
void
npf_state_tcp_sysinit(npf_t *npf)
{
npf_state_tcp_params_t *params = npf_param_allocgroup(npf,
NPF_PARAMS_TCP_STATE, sizeof(npf_state_tcp_params_t));
npf_param_t param_map[] = {
{
"state.tcp.timeout.new",
¶ms->timeouts[NPF_TCPT_NEW],
.default_val = 30,
.min = 0, .max = INT_MAX
},
{
"state.tcp.timeout.established",
¶ms->timeouts[NPF_TCPT_ESTABLISHED],
.default_val = 60 * 60 * 24,
.min = 0, .max = INT_MAX
},
{
"state.tcp.timeout.half_close",
¶ms->timeouts[NPF_TCPT_HALFCLOSE],
.default_val = 60 * 60 * 6,
.min = 0, .max = INT_MAX
},
{
"state.tcp.timeout.close",
¶ms->timeouts[NPF_TCPT_CLOSE],
.default_val = 10,
.min = 0, .max = INT_MAX
},
{
"state.tcp.timeout.time_wait",
¶ms->timeouts[NPF_TCPT_TIMEWAIT],
.default_val = 60 * 2 * 2,
.min = 0, .max = INT_MAX
},
{
"state.tcp.strict_order_rst",
¶ms->strict_order_rst,
.default_val = 1,
.min = 0, .max = 1
},
{
"state.tcp.max_ack_win",
¶ms->max_ack_win,
.default_val = 66000,
.min = 0, .max = INT_MAX
},
};
npf_param_register(npf, param_map, __arraycount(param_map));
}
void
npf_state_tcp_sysfini(npf_t *npf)
{
const size_t len = sizeof(npf_state_tcp_params_t);
npf_param_freegroup(npf, NPF_PARAMS_TCP_STATE, len);
}