npf_state_tcp.c revision 1.13 1 1.13 rmind /* $NetBSD: npf_state_tcp.c,v 1.13 2013/11/04 22:17:21 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.7 rmind * Copyright (c) 2010-2012 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.1 rmind /*
33 1.1 rmind * NPF TCP state engine for connection tracking.
34 1.1 rmind */
35 1.1 rmind
36 1.1 rmind #include <sys/cdefs.h>
37 1.13 rmind __KERNEL_RCSID(0, "$NetBSD: npf_state_tcp.c,v 1.13 2013/11/04 22:17:21 rmind Exp $");
38 1.1 rmind
39 1.1 rmind #include <sys/param.h>
40 1.1 rmind #include <sys/types.h>
41 1.1 rmind
42 1.1 rmind #include <netinet/in.h>
43 1.1 rmind #include <netinet/tcp.h>
44 1.1 rmind #include <netinet/tcp_seq.h>
45 1.1 rmind
46 1.1 rmind #include "npf_impl.h"
47 1.1 rmind
48 1.1 rmind /*
49 1.1 rmind * NPF TCP states. Note: these states are different from the TCP FSM
50 1.4 rmind * states of RFC 793. The packet filter is a man-in-the-middle.
51 1.1 rmind */
52 1.13 rmind #define NPF_TCPS_OK 255
53 1.1 rmind #define NPF_TCPS_CLOSED 0
54 1.1 rmind #define NPF_TCPS_SYN_SENT 1
55 1.1 rmind #define NPF_TCPS_SIMSYN_SENT 2
56 1.1 rmind #define NPF_TCPS_SYN_RECEIVED 3
57 1.1 rmind #define NPF_TCPS_ESTABLISHED 4
58 1.8 rmind #define NPF_TCPS_FIN_SENT 5
59 1.8 rmind #define NPF_TCPS_FIN_RECEIVED 6
60 1.8 rmind #define NPF_TCPS_CLOSE_WAIT 7
61 1.8 rmind #define NPF_TCPS_FIN_WAIT 8
62 1.8 rmind #define NPF_TCPS_CLOSING 9
63 1.8 rmind #define NPF_TCPS_LAST_ACK 10
64 1.8 rmind #define NPF_TCPS_TIME_WAIT 11
65 1.1 rmind
66 1.8 rmind #define NPF_TCP_NSTATES 12
67 1.1 rmind
68 1.1 rmind /*
69 1.1 rmind * TCP connection timeout table (in seconds).
70 1.1 rmind */
71 1.2 rmind static u_int npf_tcp_timeouts[] __read_mostly = {
72 1.1 rmind /* Closed, timeout nearly immediately. */
73 1.1 rmind [NPF_TCPS_CLOSED] = 10,
74 1.1 rmind /* Unsynchronised states. */
75 1.1 rmind [NPF_TCPS_SYN_SENT] = 30,
76 1.1 rmind [NPF_TCPS_SIMSYN_SENT] = 30,
77 1.1 rmind [NPF_TCPS_SYN_RECEIVED] = 60,
78 1.8 rmind /* Established: 24 hours. */
79 1.1 rmind [NPF_TCPS_ESTABLISHED] = 60 * 60 * 24,
80 1.8 rmind /* FIN seen: 4 minutes (2 * MSL). */
81 1.8 rmind [NPF_TCPS_FIN_SENT] = 60 * 2 * 2,
82 1.8 rmind [NPF_TCPS_FIN_RECEIVED] = 60 * 2 * 2,
83 1.8 rmind /* Half-closed cases: 6 hours. */
84 1.8 rmind [NPF_TCPS_CLOSE_WAIT] = 60 * 60 * 6,
85 1.8 rmind [NPF_TCPS_FIN_WAIT] = 60 * 60 * 6,
86 1.8 rmind /* Full close cases: 30 sec and 2 * MSL. */
87 1.1 rmind [NPF_TCPS_CLOSING] = 30,
88 1.1 rmind [NPF_TCPS_LAST_ACK] = 30,
89 1.1 rmind [NPF_TCPS_TIME_WAIT] = 60 * 2 * 2,
90 1.1 rmind };
91 1.1 rmind
92 1.11 rmind static bool npf_strict_order_rst __read_mostly = false;
93 1.11 rmind
94 1.1 rmind #define NPF_TCP_MAXACKWIN 66000
95 1.1 rmind
96 1.2 rmind /*
97 1.2 rmind * List of TCP flag cases and conversion of flags to a case (index).
98 1.2 rmind */
99 1.2 rmind
100 1.2 rmind #define TCPFC_INVALID 0
101 1.2 rmind #define TCPFC_SYN 1
102 1.2 rmind #define TCPFC_SYNACK 2
103 1.2 rmind #define TCPFC_ACK 3
104 1.2 rmind #define TCPFC_FIN 4
105 1.2 rmind #define TCPFC_COUNT 5
106 1.2 rmind
107 1.2 rmind static inline u_int
108 1.13 rmind npf_tcpfl2case(const u_int tcpfl)
109 1.2 rmind {
110 1.2 rmind u_int i, c;
111 1.2 rmind
112 1.3 rmind CTASSERT(TH_FIN == 0x01);
113 1.3 rmind CTASSERT(TH_SYN == 0x02);
114 1.3 rmind CTASSERT(TH_ACK == 0x10);
115 1.3 rmind
116 1.2 rmind /*
117 1.3 rmind * Flags are shifted to use three least significant bits, thus each
118 1.3 rmind * flag combination has a unique number ranging from 0 to 7, e.g.
119 1.3 rmind * TH_SYN | TH_ACK has number 6, since (0x02 | (0x10 >> 2)) == 6.
120 1.3 rmind * However, the requirement is to have number 0 for invalid cases,
121 1.3 rmind * such as TH_SYN | TH_FIN, and to have the same number for TH_FIN
122 1.3 rmind * and TH_FIN|TH_ACK cases. Thus, we generate a mask assigning 3
123 1.3 rmind * bits for each number, which contains the actual case numbers:
124 1.3 rmind *
125 1.3 rmind * TCPFC_SYNACK << (6 << 2) == 0x2000000 (6 - SYN,ACK)
126 1.3 rmind * TCPFC_FIN << (5 << 2) == 0x0400000 (5 - FIN,ACK)
127 1.3 rmind * ...
128 1.3 rmind *
129 1.3 rmind * Hence, OR'ed mask value is 0x2430140.
130 1.2 rmind */
131 1.2 rmind i = (tcpfl & (TH_SYN | TH_FIN)) | ((tcpfl & TH_ACK) >> 2);
132 1.2 rmind c = (0x2430140 >> (i << 2)) & 7;
133 1.2 rmind
134 1.2 rmind KASSERT(c < TCPFC_COUNT);
135 1.2 rmind return c;
136 1.2 rmind }
137 1.1 rmind
138 1.1 rmind /*
139 1.1 rmind * NPF transition table of a tracked TCP connection.
140 1.1 rmind *
141 1.1 rmind * There is a single state, which is changed in the following way:
142 1.1 rmind *
143 1.2 rmind * new_state = npf_tcp_fsm[old_state][direction][npf_tcpfl2case(tcp_flags)];
144 1.1 rmind *
145 1.1 rmind * Note that this state is different from the state in each end (host).
146 1.1 rmind */
147 1.1 rmind
148 1.13 rmind static const uint8_t npf_tcp_fsm[NPF_TCP_NSTATES][2][TCPFC_COUNT] = {
149 1.1 rmind [NPF_TCPS_CLOSED] = {
150 1.1 rmind [NPF_FLOW_FORW] = {
151 1.1 rmind /* Handshake (1): initial SYN. */
152 1.2 rmind [TCPFC_SYN] = NPF_TCPS_SYN_SENT,
153 1.1 rmind },
154 1.1 rmind },
155 1.1 rmind [NPF_TCPS_SYN_SENT] = {
156 1.1 rmind [NPF_FLOW_FORW] = {
157 1.1 rmind /* SYN may be retransmitted. */
158 1.2 rmind [TCPFC_SYN] = NPF_TCPS_OK,
159 1.1 rmind },
160 1.1 rmind [NPF_FLOW_BACK] = {
161 1.1 rmind /* Handshake (2): SYN-ACK is expected. */
162 1.2 rmind [TCPFC_SYNACK] = NPF_TCPS_SYN_RECEIVED,
163 1.1 rmind /* Simultaneous initiation - SYN. */
164 1.2 rmind [TCPFC_SYN] = NPF_TCPS_SIMSYN_SENT,
165 1.1 rmind },
166 1.1 rmind },
167 1.1 rmind [NPF_TCPS_SIMSYN_SENT] = {
168 1.1 rmind [NPF_FLOW_FORW] = {
169 1.1 rmind /* Original SYN re-transmission. */
170 1.2 rmind [TCPFC_SYN] = NPF_TCPS_OK,
171 1.1 rmind /* SYN-ACK response to simultaneous SYN. */
172 1.2 rmind [TCPFC_SYNACK] = NPF_TCPS_SYN_RECEIVED,
173 1.1 rmind },
174 1.1 rmind [NPF_FLOW_BACK] = {
175 1.1 rmind /* Simultaneous SYN re-transmission.*/
176 1.2 rmind [TCPFC_SYN] = NPF_TCPS_OK,
177 1.1 rmind /* SYN-ACK response to original SYN. */
178 1.2 rmind [TCPFC_SYNACK] = NPF_TCPS_SYN_RECEIVED,
179 1.9 rmind /* FIN may occur early. */
180 1.8 rmind [TCPFC_FIN] = NPF_TCPS_FIN_RECEIVED,
181 1.1 rmind },
182 1.1 rmind },
183 1.1 rmind [NPF_TCPS_SYN_RECEIVED] = {
184 1.1 rmind [NPF_FLOW_FORW] = {
185 1.1 rmind /* Handshake (3): ACK is expected. */
186 1.2 rmind [TCPFC_ACK] = NPF_TCPS_ESTABLISHED,
187 1.2 rmind /* FIN may be sent early. */
188 1.8 rmind [TCPFC_FIN] = NPF_TCPS_FIN_SENT,
189 1.1 rmind },
190 1.1 rmind [NPF_FLOW_BACK] = {
191 1.1 rmind /* SYN-ACK may be retransmitted. */
192 1.2 rmind [TCPFC_SYNACK] = NPF_TCPS_OK,
193 1.1 rmind /* XXX: ACK of late SYN in simultaneous case? */
194 1.2 rmind [TCPFC_ACK] = NPF_TCPS_OK,
195 1.9 rmind /* FIN may occur early. */
196 1.8 rmind [TCPFC_FIN] = NPF_TCPS_FIN_RECEIVED,
197 1.1 rmind },
198 1.1 rmind },
199 1.1 rmind [NPF_TCPS_ESTABLISHED] = {
200 1.1 rmind /*
201 1.1 rmind * Regular ACKs (data exchange) or FIN.
202 1.1 rmind * FIN packets may have ACK set.
203 1.1 rmind */
204 1.1 rmind [NPF_FLOW_FORW] = {
205 1.2 rmind [TCPFC_ACK] = NPF_TCPS_OK,
206 1.1 rmind /* FIN by the sender. */
207 1.8 rmind [TCPFC_FIN] = NPF_TCPS_FIN_SENT,
208 1.1 rmind },
209 1.1 rmind [NPF_FLOW_BACK] = {
210 1.2 rmind [TCPFC_ACK] = NPF_TCPS_OK,
211 1.1 rmind /* FIN by the receiver. */
212 1.8 rmind [TCPFC_FIN] = NPF_TCPS_FIN_RECEIVED,
213 1.1 rmind },
214 1.1 rmind },
215 1.8 rmind [NPF_TCPS_FIN_SENT] = {
216 1.8 rmind [NPF_FLOW_FORW] = {
217 1.8 rmind /* FIN may be re-transmitted. Late ACK as well. */
218 1.8 rmind [TCPFC_ACK] = NPF_TCPS_OK,
219 1.8 rmind [TCPFC_FIN] = NPF_TCPS_OK,
220 1.8 rmind },
221 1.8 rmind [NPF_FLOW_BACK] = {
222 1.8 rmind /* If ACK, connection is half-closed now. */
223 1.8 rmind [TCPFC_ACK] = NPF_TCPS_FIN_WAIT,
224 1.8 rmind /* FIN or FIN-ACK race - immediate closing. */
225 1.8 rmind [TCPFC_FIN] = NPF_TCPS_CLOSING,
226 1.8 rmind },
227 1.8 rmind },
228 1.8 rmind [NPF_TCPS_FIN_RECEIVED] = {
229 1.1 rmind /*
230 1.8 rmind * FIN was received. Equivalent scenario to sent FIN.
231 1.1 rmind */
232 1.1 rmind [NPF_FLOW_FORW] = {
233 1.2 rmind [TCPFC_ACK] = NPF_TCPS_CLOSE_WAIT,
234 1.2 rmind [TCPFC_FIN] = NPF_TCPS_CLOSING,
235 1.1 rmind },
236 1.1 rmind [NPF_FLOW_BACK] = {
237 1.8 rmind [TCPFC_ACK] = NPF_TCPS_OK,
238 1.8 rmind [TCPFC_FIN] = NPF_TCPS_OK,
239 1.1 rmind },
240 1.1 rmind },
241 1.1 rmind [NPF_TCPS_CLOSE_WAIT] = {
242 1.1 rmind /* Sender has sent the FIN and closed its end. */
243 1.1 rmind [NPF_FLOW_FORW] = {
244 1.2 rmind [TCPFC_ACK] = NPF_TCPS_OK,
245 1.2 rmind [TCPFC_FIN] = NPF_TCPS_LAST_ACK,
246 1.1 rmind },
247 1.1 rmind [NPF_FLOW_BACK] = {
248 1.2 rmind [TCPFC_ACK] = NPF_TCPS_OK,
249 1.2 rmind [TCPFC_FIN] = NPF_TCPS_LAST_ACK,
250 1.1 rmind },
251 1.1 rmind },
252 1.1 rmind [NPF_TCPS_FIN_WAIT] = {
253 1.1 rmind /* Receiver has closed its end. */
254 1.1 rmind [NPF_FLOW_FORW] = {
255 1.2 rmind [TCPFC_ACK] = NPF_TCPS_OK,
256 1.2 rmind [TCPFC_FIN] = NPF_TCPS_LAST_ACK,
257 1.1 rmind },
258 1.1 rmind [NPF_FLOW_BACK] = {
259 1.2 rmind [TCPFC_ACK] = NPF_TCPS_OK,
260 1.2 rmind [TCPFC_FIN] = NPF_TCPS_LAST_ACK,
261 1.1 rmind },
262 1.1 rmind },
263 1.1 rmind [NPF_TCPS_CLOSING] = {
264 1.1 rmind /* Race of FINs - expecting ACK. */
265 1.1 rmind [NPF_FLOW_FORW] = {
266 1.2 rmind [TCPFC_ACK] = NPF_TCPS_LAST_ACK,
267 1.1 rmind },
268 1.1 rmind [NPF_FLOW_BACK] = {
269 1.2 rmind [TCPFC_ACK] = NPF_TCPS_LAST_ACK,
270 1.1 rmind },
271 1.1 rmind },
272 1.1 rmind [NPF_TCPS_LAST_ACK] = {
273 1.1 rmind /* FINs exchanged - expecting last ACK. */
274 1.1 rmind [NPF_FLOW_FORW] = {
275 1.2 rmind [TCPFC_ACK] = NPF_TCPS_TIME_WAIT,
276 1.1 rmind },
277 1.1 rmind [NPF_FLOW_BACK] = {
278 1.2 rmind [TCPFC_ACK] = NPF_TCPS_TIME_WAIT,
279 1.1 rmind },
280 1.1 rmind },
281 1.1 rmind [NPF_TCPS_TIME_WAIT] = {
282 1.1 rmind /* May re-open the connection as per RFC 1122. */
283 1.1 rmind [NPF_FLOW_FORW] = {
284 1.2 rmind [TCPFC_SYN] = NPF_TCPS_SYN_SENT,
285 1.1 rmind },
286 1.1 rmind },
287 1.1 rmind };
288 1.1 rmind
289 1.1 rmind /*
290 1.1 rmind * npf_tcp_inwindow: determine whether the packet is in the TCP window
291 1.1 rmind * and thus part of the connection we are tracking.
292 1.1 rmind */
293 1.1 rmind static bool
294 1.12 rmind npf_tcp_inwindow(npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst, const int di)
295 1.1 rmind {
296 1.12 rmind const struct tcphdr * const th = npc->npc_l4.tcp;
297 1.1 rmind const int tcpfl = th->th_flags;
298 1.1 rmind npf_tcpstate_t *fstate, *tstate;
299 1.10 rmind int tcpdlen, ackskew;
300 1.1 rmind tcp_seq seq, ack, end;
301 1.1 rmind uint32_t win;
302 1.1 rmind
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.10 rmind (void)npf_fetch_tcpopts(npc, nbuf, 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.10 rmind (void)npf_fetch_tcpopts(npc, nbuf, NULL,
380 1.10 rmind &fstate->nst_wscale);
381 1.1 rmind }
382 1.1 rmind }
383 1.6 rmind
384 1.1 rmind if ((tcpfl & TH_ACK) == 0) {
385 1.1 rmind /* Pretend that an ACK was sent. */
386 1.1 rmind ack = tstate->nst_end;
387 1.1 rmind } else if ((tcpfl & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST) && ack == 0) {
388 1.1 rmind /* Workaround for some TCP stacks. */
389 1.1 rmind ack = tstate->nst_end;
390 1.1 rmind }
391 1.11 rmind
392 1.11 rmind if (__predict_false(tcpfl & TH_RST)) {
393 1.11 rmind /* RST to the initial SYN may have zero SEQ - fix it up. */
394 1.11 rmind if (seq == 0 && nst->nst_state == NPF_TCPS_SYN_SENT) {
395 1.11 rmind end = fstate->nst_end;
396 1.11 rmind seq = end;
397 1.11 rmind }
398 1.11 rmind
399 1.11 rmind /* Strict in-order sequence for RST packets. */
400 1.11 rmind if (npf_strict_order_rst && (fstate->nst_end - seq) > 1) {
401 1.11 rmind return false;
402 1.11 rmind }
403 1.1 rmind }
404 1.11 rmind
405 1.1 rmind /*
406 1.1 rmind * Determine whether the data is within previously noted window,
407 1.1 rmind * that is, upper boundary for valid data (I).
408 1.1 rmind */
409 1.1 rmind if (!SEQ_LEQ(end, fstate->nst_maxend)) {
410 1.1 rmind npf_stats_inc(NPF_STAT_INVALID_STATE_TCP1);
411 1.1 rmind return false;
412 1.1 rmind }
413 1.1 rmind
414 1.1 rmind /* Lower boundary (II), which is no more than one window back. */
415 1.1 rmind if (!SEQ_GEQ(seq, fstate->nst_end - tstate->nst_maxwin)) {
416 1.1 rmind npf_stats_inc(NPF_STAT_INVALID_STATE_TCP2);
417 1.1 rmind return false;
418 1.1 rmind }
419 1.1 rmind
420 1.1 rmind /*
421 1.6 rmind * Boundaries for valid acknowledgments (III, IV) - one predicted
422 1.1 rmind * window up or down, since packets may be fragmented.
423 1.1 rmind */
424 1.1 rmind ackskew = tstate->nst_end - ack;
425 1.1 rmind if (ackskew < -NPF_TCP_MAXACKWIN ||
426 1.1 rmind ackskew > (NPF_TCP_MAXACKWIN << fstate->nst_wscale)) {
427 1.1 rmind npf_stats_inc(NPF_STAT_INVALID_STATE_TCP3);
428 1.1 rmind return false;
429 1.1 rmind }
430 1.1 rmind
431 1.1 rmind /*
432 1.1 rmind * Packet has been passed.
433 1.1 rmind *
434 1.1 rmind * Negative ackskew might be due to fragmented packets. Since the
435 1.1 rmind * total length of the packet is unknown - bump the boundary.
436 1.1 rmind */
437 1.6 rmind
438 1.1 rmind if (ackskew < 0) {
439 1.4 rmind tstate->nst_end = ack;
440 1.1 rmind }
441 1.1 rmind /* Keep track of the maximum window seen. */
442 1.1 rmind if (fstate->nst_maxwin < win) {
443 1.1 rmind fstate->nst_maxwin = win;
444 1.1 rmind }
445 1.1 rmind if (SEQ_GT(end, fstate->nst_end)) {
446 1.1 rmind fstate->nst_end = end;
447 1.1 rmind }
448 1.1 rmind /* Note the window for upper boundary. */
449 1.1 rmind if (SEQ_GEQ(ack + win, tstate->nst_maxend)) {
450 1.1 rmind tstate->nst_maxend = ack + win;
451 1.1 rmind }
452 1.1 rmind return true;
453 1.1 rmind }
454 1.1 rmind
455 1.7 rmind /*
456 1.7 rmind * npf_state_tcp: inspect TCP segment, determine whether it belongs to
457 1.7 rmind * the connection and track its state.
458 1.7 rmind */
459 1.1 rmind bool
460 1.12 rmind npf_state_tcp(npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst, int di)
461 1.1 rmind {
462 1.12 rmind const struct tcphdr * const th = npc->npc_l4.tcp;
463 1.13 rmind const u_int tcpfl = th->th_flags, state = nst->nst_state;
464 1.13 rmind u_int nstate;
465 1.1 rmind
466 1.7 rmind KASSERT(nst->nst_state == 0 || mutex_owned(&nst->nst_lock));
467 1.13 rmind KASSERT(nst->nst_state < NPF_TCP_NSTATES);
468 1.6 rmind
469 1.1 rmind /* Look for a transition to a new state. */
470 1.1 rmind if (__predict_true((tcpfl & TH_RST) == 0)) {
471 1.13 rmind const u_int flagcase = npf_tcpfl2case(tcpfl);
472 1.2 rmind nstate = npf_tcp_fsm[state][di][flagcase];
473 1.1 rmind } else if (state == NPF_TCPS_TIME_WAIT) {
474 1.1 rmind /* Prevent TIME-WAIT assassination (RFC 1337). */
475 1.1 rmind nstate = NPF_TCPS_OK;
476 1.1 rmind } else {
477 1.1 rmind nstate = NPF_TCPS_CLOSED;
478 1.1 rmind }
479 1.5 rmind
480 1.1 rmind /* Determine whether TCP packet really belongs to this connection. */
481 1.1 rmind if (!npf_tcp_inwindow(npc, nbuf, nst, di)) {
482 1.1 rmind return false;
483 1.1 rmind }
484 1.1 rmind if (__predict_true(nstate == NPF_TCPS_OK)) {
485 1.1 rmind return true;
486 1.1 rmind }
487 1.5 rmind
488 1.1 rmind nst->nst_state = nstate;
489 1.1 rmind return true;
490 1.1 rmind }
491 1.1 rmind
492 1.1 rmind int
493 1.1 rmind npf_state_tcp_timeout(const npf_state_t *nst)
494 1.1 rmind {
495 1.1 rmind const u_int state = nst->nst_state;
496 1.1 rmind
497 1.1 rmind KASSERT(state < NPF_TCP_NSTATES);
498 1.1 rmind return npf_tcp_timeouts[state];
499 1.1 rmind }
500