Home | History | Annotate | Line # | Download | only in npf
npf_state_tcp.c revision 1.20
      1 /*-
      2  * Copyright (c) 2010-2012 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This material is based upon work partially supported by The
      6  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * NPF TCP state engine for connection tracking.
     32  */
     33 
     34 #ifdef _KERNEL
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: npf_state_tcp.c,v 1.20 2019/07/23 00:52:01 rmind Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/types.h>
     40 
     41 #include <netinet/in.h>
     42 #include <netinet/tcp.h>
     43 #endif
     44 
     45 #include "npf_impl.h"
     46 
     47 /*
     48  * NPF TCP states.  Note: these states are different from the TCP FSM
     49  * states of RFC 793.  The packet filter is a man-in-the-middle.
     50  */
     51 #define	NPF_TCPS_OK		255
     52 #define	NPF_TCPS_CLOSED		0
     53 #define	NPF_TCPS_SYN_SENT	1
     54 #define	NPF_TCPS_SIMSYN_SENT	2
     55 #define	NPF_TCPS_SYN_RECEIVED	3
     56 #define	NPF_TCPS_ESTABLISHED	4
     57 #define	NPF_TCPS_FIN_SENT	5
     58 #define	NPF_TCPS_FIN_RECEIVED	6
     59 #define	NPF_TCPS_CLOSE_WAIT	7
     60 #define	NPF_TCPS_FIN_WAIT	8
     61 #define	NPF_TCPS_CLOSING	9
     62 #define	NPF_TCPS_LAST_ACK	10
     63 #define	NPF_TCPS_TIME_WAIT	11
     64 
     65 #define	NPF_TCP_NSTATES		12
     66 
     67 /* Timeouts */
     68 #define	NPF_TCPT_NEW		0
     69 #define	NPF_TCPT_ESTABLISHED	1
     70 #define	NPF_TCPT_HALFCLOSE	2
     71 #define	NPF_TCPT_CLOSE		3
     72 #define	NPF_TCPT_TIMEWAIT	4
     73 #define	NPF_TCPT_COUNT		5
     74 
     75 /*
     76  * Parameters.
     77  */
     78 typedef struct {
     79 	int		max_ack_win;
     80 	int		strict_order_rst;
     81 	int		timeouts[NPF_TCPT_COUNT];
     82 } npf_state_tcp_params_t;
     83 
     84 /*
     85  * Helpers.
     86  */
     87 #define	SEQ_LT(a,b)		((int)((a)-(b)) < 0)
     88 #define	SEQ_LEQ(a,b)		((int)((a)-(b)) <= 0)
     89 #define	SEQ_GT(a,b)		((int)((a)-(b)) > 0)
     90 #define	SEQ_GEQ(a,b)		((int)((a)-(b)) >= 0)
     91 
     92 /*
     93  * List of TCP flag cases and conversion of flags to a case (index).
     94  */
     95 
     96 #define	TCPFC_INVALID		0
     97 #define	TCPFC_SYN		1
     98 #define	TCPFC_SYNACK		2
     99 #define	TCPFC_ACK		3
    100 #define	TCPFC_FIN		4
    101 #define	TCPFC_COUNT		5
    102 
    103 static inline unsigned
    104 npf_tcpfl2case(const unsigned tcpfl)
    105 {
    106 	unsigned i, c;
    107 
    108 	CTASSERT(TH_FIN == 0x01);
    109 	CTASSERT(TH_SYN == 0x02);
    110 	CTASSERT(TH_ACK == 0x10);
    111 
    112 	/*
    113 	 * Flags are shifted to use three least significant bits, thus each
    114 	 * flag combination has a unique number ranging from 0 to 7, e.g.
    115 	 * TH_SYN | TH_ACK has number 6, since (0x02 | (0x10 >> 2)) == 6.
    116 	 * However, the requirement is to have number 0 for invalid cases,
    117 	 * such as TH_SYN | TH_FIN, and to have the same number for TH_FIN
    118 	 * and TH_FIN|TH_ACK cases.  Thus, we generate a mask assigning 3
    119 	 * bits for each number, which contains the actual case numbers:
    120 	 *
    121 	 * TCPFC_SYNACK	<< (6 << 2) == 0x2000000 (6 - SYN,ACK)
    122 	 * TCPFC_FIN	<< (5 << 2) == 0x0400000 (5 - FIN,ACK)
    123 	 * ...
    124 	 *
    125 	 * Hence, OR'ed mask value is 0x2430140.
    126 	 */
    127 	i = (tcpfl & (TH_SYN | TH_FIN)) | ((tcpfl & TH_ACK) >> 2);
    128 	c = (0x2430140 >> (i << 2)) & 7;
    129 
    130 	KASSERT(c < TCPFC_COUNT);
    131 	return c;
    132 }
    133 
    134 /*
    135  * NPF transition table of a tracked TCP connection.
    136  *
    137  * There is a single state, which is changed in the following way:
    138  *
    139  * new_state = npf_tcp_fsm[old_state][direction][npf_tcpfl2case(tcp_flags)];
    140  *
    141  * Note that this state is different from the state in each end (host).
    142  */
    143 
    144 static const uint8_t npf_tcp_fsm[NPF_TCP_NSTATES][2][TCPFC_COUNT] = {
    145 	[NPF_TCPS_CLOSED] = {
    146 		[NPF_FLOW_FORW] = {
    147 			/* Handshake (1): initial SYN. */
    148 			[TCPFC_SYN]	= NPF_TCPS_SYN_SENT,
    149 		},
    150 	},
    151 	[NPF_TCPS_SYN_SENT] = {
    152 		[NPF_FLOW_FORW] = {
    153 			/* SYN may be retransmitted. */
    154 			[TCPFC_SYN]	= NPF_TCPS_OK,
    155 		},
    156 		[NPF_FLOW_BACK] = {
    157 			/* Handshake (2): SYN-ACK is expected. */
    158 			[TCPFC_SYNACK]	= NPF_TCPS_SYN_RECEIVED,
    159 			/* Simultaneous initiation - SYN. */
    160 			[TCPFC_SYN]	= NPF_TCPS_SIMSYN_SENT,
    161 		},
    162 	},
    163 	[NPF_TCPS_SIMSYN_SENT] = {
    164 		[NPF_FLOW_FORW] = {
    165 			/* Original SYN re-transmission. */
    166 			[TCPFC_SYN]	= NPF_TCPS_OK,
    167 			/* SYN-ACK response to simultaneous SYN. */
    168 			[TCPFC_SYNACK]	= NPF_TCPS_SYN_RECEIVED,
    169 		},
    170 		[NPF_FLOW_BACK] = {
    171 			/* Simultaneous SYN re-transmission.*/
    172 			[TCPFC_SYN]	= NPF_TCPS_OK,
    173 			/* SYN-ACK response to original SYN. */
    174 			[TCPFC_SYNACK]	= NPF_TCPS_SYN_RECEIVED,
    175 			/* FIN may occur early. */
    176 			[TCPFC_FIN]	= NPF_TCPS_FIN_RECEIVED,
    177 		},
    178 	},
    179 	[NPF_TCPS_SYN_RECEIVED] = {
    180 		[NPF_FLOW_FORW] = {
    181 			/* Handshake (3): ACK is expected. */
    182 			[TCPFC_ACK]	= NPF_TCPS_ESTABLISHED,
    183 			/* FIN may be sent early. */
    184 			[TCPFC_FIN]	= NPF_TCPS_FIN_SENT,
    185 			/* Late SYN re-transmission. */
    186 			[TCPFC_SYN]	= NPF_TCPS_OK,
    187 		},
    188 		[NPF_FLOW_BACK] = {
    189 			/* SYN-ACK may be retransmitted. */
    190 			[TCPFC_SYNACK]	= NPF_TCPS_OK,
    191 			/* XXX: ACK of late SYN in simultaneous case? */
    192 			[TCPFC_ACK]	= NPF_TCPS_OK,
    193 			/* FIN may occur early. */
    194 			[TCPFC_FIN]	= NPF_TCPS_FIN_RECEIVED,
    195 		},
    196 	},
    197 	[NPF_TCPS_ESTABLISHED] = {
    198 		/*
    199 		 * Regular ACKs (data exchange) or FIN.
    200 		 * FIN packets may have ACK set.
    201 		 */
    202 		[NPF_FLOW_FORW] = {
    203 			[TCPFC_ACK]	= NPF_TCPS_OK,
    204 			/* FIN by the sender. */
    205 			[TCPFC_FIN]	= NPF_TCPS_FIN_SENT,
    206 		},
    207 		[NPF_FLOW_BACK] = {
    208 			[TCPFC_ACK]	= NPF_TCPS_OK,
    209 			/* FIN by the receiver. */
    210 			[TCPFC_FIN]	= NPF_TCPS_FIN_RECEIVED,
    211 		},
    212 	},
    213 	[NPF_TCPS_FIN_SENT] = {
    214 		[NPF_FLOW_FORW] = {
    215 			/* FIN may be re-transmitted.  Late ACK as well. */
    216 			[TCPFC_ACK]	= NPF_TCPS_OK,
    217 			[TCPFC_FIN]	= NPF_TCPS_OK,
    218 		},
    219 		[NPF_FLOW_BACK] = {
    220 			/* If ACK, connection is half-closed now. */
    221 			[TCPFC_ACK]	= NPF_TCPS_FIN_WAIT,
    222 			/* FIN or FIN-ACK race - immediate closing. */
    223 			[TCPFC_FIN]	= NPF_TCPS_CLOSING,
    224 		},
    225 	},
    226 	[NPF_TCPS_FIN_RECEIVED] = {
    227 		/*
    228 		 * FIN was received.  Equivalent scenario to sent FIN.
    229 		 */
    230 		[NPF_FLOW_FORW] = {
    231 			[TCPFC_ACK]	= NPF_TCPS_CLOSE_WAIT,
    232 			[TCPFC_FIN]	= NPF_TCPS_CLOSING,
    233 		},
    234 		[NPF_FLOW_BACK] = {
    235 			[TCPFC_ACK]	= NPF_TCPS_OK,
    236 			[TCPFC_FIN]	= NPF_TCPS_OK,
    237 		},
    238 	},
    239 	[NPF_TCPS_CLOSE_WAIT] = {
    240 		/* Sender has sent the FIN and closed its end. */
    241 		[NPF_FLOW_FORW] = {
    242 			[TCPFC_ACK]	= NPF_TCPS_OK,
    243 			[TCPFC_FIN]	= NPF_TCPS_LAST_ACK,
    244 		},
    245 		[NPF_FLOW_BACK] = {
    246 			[TCPFC_ACK]	= NPF_TCPS_OK,
    247 			[TCPFC_FIN]	= NPF_TCPS_LAST_ACK,
    248 		},
    249 	},
    250 	[NPF_TCPS_FIN_WAIT] = {
    251 		/* Receiver has closed its end. */
    252 		[NPF_FLOW_FORW] = {
    253 			[TCPFC_ACK]	= NPF_TCPS_OK,
    254 			[TCPFC_FIN]	= NPF_TCPS_LAST_ACK,
    255 		},
    256 		[NPF_FLOW_BACK] = {
    257 			[TCPFC_ACK]	= NPF_TCPS_OK,
    258 			[TCPFC_FIN]	= NPF_TCPS_LAST_ACK,
    259 		},
    260 	},
    261 	[NPF_TCPS_CLOSING] = {
    262 		/* Race of FINs - expecting ACK. */
    263 		[NPF_FLOW_FORW] = {
    264 			[TCPFC_ACK]	= NPF_TCPS_LAST_ACK,
    265 		},
    266 		[NPF_FLOW_BACK] = {
    267 			[TCPFC_ACK]	= NPF_TCPS_LAST_ACK,
    268 		},
    269 	},
    270 	[NPF_TCPS_LAST_ACK] = {
    271 		/* FINs exchanged - expecting last ACK. */
    272 		[NPF_FLOW_FORW] = {
    273 			[TCPFC_ACK]	= NPF_TCPS_TIME_WAIT,
    274 		},
    275 		[NPF_FLOW_BACK] = {
    276 			[TCPFC_ACK]	= NPF_TCPS_TIME_WAIT,
    277 		},
    278 	},
    279 	[NPF_TCPS_TIME_WAIT] = {
    280 		/* May re-open the connection as per RFC 1122. */
    281 		[NPF_FLOW_FORW] = {
    282 			[TCPFC_SYN]	= NPF_TCPS_SYN_SENT,
    283 		},
    284 	},
    285 };
    286 
    287 /*
    288  * npf_tcp_inwindow: determine whether the packet is in the TCP window
    289  * and thus part of the connection we are tracking.
    290  */
    291 static bool
    292 npf_tcp_inwindow(npf_cache_t *npc, npf_state_t *nst, const int di)
    293 {
    294 	const npf_state_tcp_params_t *params;
    295 	const struct tcphdr * const th = npc->npc_l4.tcp;
    296 	const int tcpfl = th->th_flags;
    297 	npf_tcpstate_t *fstate, *tstate;
    298 	int tcpdlen, ackskew;
    299 	tcp_seq seq, ack, end;
    300 	uint32_t win;
    301 
    302 	params = npc->npc_ctx->params[NPF_PARAMS_TCP_STATE];
    303 	KASSERT(npf_iscached(npc, NPC_TCP));
    304 	KASSERT(di == NPF_FLOW_FORW || di == NPF_FLOW_BACK);
    305 
    306 	/*
    307 	 * Perform SEQ/ACK numbers check against boundaries.  Reference:
    308 	 *
    309 	 *	Rooij G., "Real stateful TCP packet filtering in IP Filter",
    310 	 *	10th USENIX Security Symposium invited talk, Aug. 2001.
    311 	 *
    312 	 * There are four boundaries defined as following:
    313 	 *	I)   SEQ + LEN	<= MAX { SND.ACK + MAX(SND.WIN, 1) }
    314 	 *	II)  SEQ	>= MAX { SND.SEQ + SND.LEN - MAX(RCV.WIN, 1) }
    315 	 *	III) ACK	<= MAX { RCV.SEQ + RCV.LEN }
    316 	 *	IV)  ACK	>= MAX { RCV.SEQ + RCV.LEN } - MAXACKWIN
    317 	 *
    318 	 * Let these members of npf_tcpstate_t be the maximum seen values of:
    319 	 *	nst_end		- SEQ + LEN
    320 	 *	nst_maxend	- ACK + MAX(WIN, 1)
    321 	 *	nst_maxwin	- MAX(WIN, 1)
    322 	 */
    323 
    324 	tcpdlen = npf_tcpsaw(__UNCONST(npc), &seq, &ack, &win);
    325 	end = seq + tcpdlen;
    326 	if (tcpfl & TH_SYN) {
    327 		end++;
    328 	}
    329 	if (tcpfl & TH_FIN) {
    330 		end++;
    331 	}
    332 
    333 	fstate = &nst->nst_tcpst[di];
    334 	tstate = &nst->nst_tcpst[!di];
    335 	win = win ? (win << fstate->nst_wscale) : 1;
    336 
    337 	/*
    338 	 * Initialise if the first packet.
    339 	 * Note: only case when nst_maxwin is zero.
    340 	 */
    341 	if (__predict_false(fstate->nst_maxwin == 0)) {
    342 		/*
    343 		 * Normally, it should be the first SYN or a re-transmission
    344 		 * of SYN.  The state of the other side will get set with a
    345 		 * SYN-ACK reply (see below).
    346 		 */
    347 		fstate->nst_end = end;
    348 		fstate->nst_maxend = end;
    349 		fstate->nst_maxwin = win;
    350 		tstate->nst_end = 0;
    351 		tstate->nst_maxend = 0;
    352 		tstate->nst_maxwin = 1;
    353 
    354 		/*
    355 		 * Handle TCP Window Scaling (RFC 1323).  Both sides may
    356 		 * send this option in their SYN packets.
    357 		 */
    358 		fstate->nst_wscale = 0;
    359 		(void)npf_fetch_tcpopts(npc, NULL, &fstate->nst_wscale);
    360 
    361 		tstate->nst_wscale = 0;
    362 
    363 		/* Done. */
    364 		return true;
    365 	}
    366 
    367 	if (fstate->nst_end == 0) {
    368 		/*
    369 		 * Should be a SYN-ACK reply to SYN.  If SYN is not set,
    370 		 * then we are in the middle of connection and lost tracking.
    371 		 */
    372 		fstate->nst_end = end;
    373 		fstate->nst_maxend = end + 1;
    374 		fstate->nst_maxwin = win;
    375 		fstate->nst_wscale = 0;
    376 
    377 		/* Handle TCP Window Scaling (must be ignored if no SYN). */
    378 		if (tcpfl & TH_SYN) {
    379 			(void)npf_fetch_tcpopts(npc, NULL, &fstate->nst_wscale);
    380 		}
    381 	}
    382 
    383 	if ((tcpfl & TH_ACK) == 0) {
    384 		/* Pretend that an ACK was sent. */
    385 		ack = tstate->nst_end;
    386 	} else if ((tcpfl & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST) && ack == 0) {
    387 		/* Workaround for some TCP stacks. */
    388 		ack = tstate->nst_end;
    389 	}
    390 
    391 	if (__predict_false(tcpfl & TH_RST)) {
    392 		/* RST to the initial SYN may have zero SEQ - fix it up. */
    393 		if (seq == 0 && nst->nst_state == NPF_TCPS_SYN_SENT) {
    394 			end = fstate->nst_end;
    395 			seq = end;
    396 		}
    397 
    398 		/* Strict in-order sequence for RST packets (RFC 5961). */
    399 		if (params->strict_order_rst && (fstate->nst_end - seq) > 1) {
    400 			return false;
    401 		}
    402 	}
    403 
    404 	/*
    405 	 * Determine whether the data is within previously noted window,
    406 	 * that is, upper boundary for valid data (I).
    407 	 */
    408 	if (!SEQ_LEQ(end, fstate->nst_maxend)) {
    409 		npf_stats_inc(npc->npc_ctx, NPF_STAT_INVALID_STATE_TCP1);
    410 		return false;
    411 	}
    412 
    413 	/* Lower boundary (II), which is no more than one window back. */
    414 	if (!SEQ_GEQ(seq, fstate->nst_end - tstate->nst_maxwin)) {
    415 		npf_stats_inc(npc->npc_ctx, NPF_STAT_INVALID_STATE_TCP2);
    416 		return false;
    417 	}
    418 
    419 	/*
    420 	 * Boundaries for valid acknowledgments (III, IV) - one predicted
    421 	 * window up or down, since packets may be fragmented.
    422 	 */
    423 	ackskew = tstate->nst_end - ack;
    424 	if (ackskew < -(int)params->max_ack_win ||
    425 	    ackskew > ((int)params->max_ack_win << fstate->nst_wscale)) {
    426 		npf_stats_inc(npc->npc_ctx, NPF_STAT_INVALID_STATE_TCP3);
    427 		return false;
    428 	}
    429 
    430 	/*
    431 	 * Packet has been passed.
    432 	 *
    433 	 * Negative ackskew might be due to fragmented packets.  Since the
    434 	 * total length of the packet is unknown - bump the boundary.
    435 	 */
    436 
    437 	if (ackskew < 0) {
    438 		tstate->nst_end = ack;
    439 	}
    440 	/* Keep track of the maximum window seen. */
    441 	if (fstate->nst_maxwin < win) {
    442 		fstate->nst_maxwin = win;
    443 	}
    444 	if (SEQ_GT(end, fstate->nst_end)) {
    445 		fstate->nst_end = end;
    446 	}
    447 	/* Note the window for upper boundary. */
    448 	if (SEQ_GEQ(ack + win, tstate->nst_maxend)) {
    449 		tstate->nst_maxend = ack + win;
    450 	}
    451 	return true;
    452 }
    453 
    454 /*
    455  * npf_state_tcp: inspect TCP segment, determine whether it belongs to
    456  * the connection and track its state.
    457  */
    458 bool
    459 npf_state_tcp(npf_cache_t *npc, npf_state_t *nst, int di)
    460 {
    461 	const struct tcphdr * const th = npc->npc_l4.tcp;
    462 	const unsigned tcpfl = th->th_flags, state = nst->nst_state;
    463 	unsigned nstate;
    464 
    465 	KASSERT(nst->nst_state < NPF_TCP_NSTATES);
    466 
    467 	/* Look for a transition to a new state. */
    468 	if (__predict_true((tcpfl & TH_RST) == 0)) {
    469 		const u_int flagcase = npf_tcpfl2case(tcpfl);
    470 		nstate = npf_tcp_fsm[state][di][flagcase];
    471 	} else if (state == NPF_TCPS_TIME_WAIT) {
    472 		/* Prevent TIME-WAIT assassination (RFC 1337). */
    473 		nstate = NPF_TCPS_OK;
    474 	} else {
    475 		nstate = NPF_TCPS_CLOSED;
    476 	}
    477 
    478 	/* Determine whether TCP packet really belongs to this connection. */
    479 	if (!npf_tcp_inwindow(npc, nst, di)) {
    480 		return false;
    481 	}
    482 	if (__predict_true(nstate == NPF_TCPS_OK)) {
    483 		return true;
    484 	}
    485 
    486 	nst->nst_state = nstate;
    487 	return true;
    488 }
    489 
    490 int
    491 npf_state_tcp_timeout(npf_t *npf, const npf_state_t *nst)
    492 {
    493 	static const uint8_t state_timeout_idx[NPF_TCP_NSTATES] = {
    494 		[NPF_TCPS_CLOSED]	= NPF_TCPT_CLOSE,
    495 		/* Unsynchronised states. */
    496 		[NPF_TCPS_SYN_SENT]	= NPF_TCPT_NEW,
    497 		[NPF_TCPS_SIMSYN_SENT]	= NPF_TCPT_NEW,
    498 		[NPF_TCPS_SYN_RECEIVED]	= NPF_TCPT_NEW,
    499 		/* Established (synchronised state). */
    500 		[NPF_TCPS_ESTABLISHED]	= NPF_TCPT_ESTABLISHED,
    501 		/* Half-closed cases. */
    502 		[NPF_TCPS_FIN_SENT]	= NPF_TCPT_HALFCLOSE,
    503 		[NPF_TCPS_FIN_RECEIVED]	= NPF_TCPT_HALFCLOSE,
    504 		[NPF_TCPS_CLOSE_WAIT]	= NPF_TCPT_HALFCLOSE,
    505 		[NPF_TCPS_FIN_WAIT]	= NPF_TCPT_HALFCLOSE,
    506 		/* Full close cases. */
    507 		[NPF_TCPS_CLOSING]	= NPF_TCPT_CLOSE,
    508 		[NPF_TCPS_LAST_ACK]	= NPF_TCPT_CLOSE,
    509 		[NPF_TCPS_TIME_WAIT]	= NPF_TCPT_TIMEWAIT,
    510 	};
    511 	const npf_state_tcp_params_t *params;
    512 	const unsigned state = nst->nst_state;
    513 
    514 	KASSERT(state < NPF_TCP_NSTATES);
    515 	params = npf->params[NPF_PARAMS_TCP_STATE];
    516 	return params->timeouts[state_timeout_idx[state]];
    517 }
    518 
    519 void
    520 npf_state_tcp_sysinit(npf_t *npf)
    521 {
    522 	npf_state_tcp_params_t *params = npf_param_allocgroup(npf,
    523 	    NPF_PARAMS_TCP_STATE, sizeof(npf_state_tcp_params_t));
    524 	npf_param_t param_map[] = {
    525 		/*
    526 		 * TCP connection timeout table (in seconds).
    527 		 */
    528 
    529 		/* Unsynchronised states. */
    530 		{
    531 			"state.tcp.timeout.new",
    532 			&params->timeouts[NPF_TCPT_NEW],
    533 			.default_val = 30,
    534 			.min = 0, .max = INT_MAX
    535 		},
    536 		/* Established. */
    537 		{
    538 			"state.tcp.timeout.established",
    539 			&params->timeouts[NPF_TCPT_ESTABLISHED],
    540 			.default_val = 60 * 60 * 24,
    541 			.min = 0, .max = INT_MAX
    542 		},
    543 		/* Half-closed cases. */
    544 		{
    545 			"state.tcp.timeout.half_close",
    546 			&params->timeouts[NPF_TCPT_HALFCLOSE],
    547 			.default_val = 60 * 60 * 6,
    548 			.min = 0, .max = INT_MAX
    549 		},
    550 		/* Full close cases. */
    551 		{
    552 			"state.tcp.timeout.close",
    553 			&params->timeouts[NPF_TCPT_CLOSE],
    554 			.default_val = 10,
    555 			.min = 0, .max = INT_MAX
    556 		},
    557 		/* TCP time-wait (2 * MSL). */
    558 		{
    559 			"state.tcp.timeout.time_wait",
    560 			&params->timeouts[NPF_TCPT_TIMEWAIT],
    561 			.default_val = 60 * 2 * 2,
    562 			.min = 0, .max = INT_MAX
    563 		},
    564 
    565 		/*
    566 		 * Enforce strict order RST.
    567 		 */
    568 		{
    569 			"state.tcp.strict_order_rst",
    570 			&params->strict_order_rst,
    571 			.default_val = 1, // true
    572 			.min = 0, .max = 1
    573 		},
    574 
    575 		/*
    576 		 * TCP state tracking: maximum allowed ACK window.
    577 		 */
    578 		{
    579 			"state.tcp.max_ack_win",
    580 			&params->max_ack_win,
    581 			.default_val = 66000,
    582 			.min = 0, .max = INT_MAX
    583 		},
    584 	};
    585 	npf_param_register(npf, param_map, __arraycount(param_map));
    586 }
    587 
    588 void
    589 npf_state_tcp_sysfini(npf_t *npf)
    590 {
    591 	const size_t len = sizeof(npf_state_tcp_params_t);
    592 	npf_param_freegroup(npf, NPF_PARAMS_TCP_STATE, len);
    593 }
    594