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