npf_state_tcp.c revision 1.1 1 1.1 rmind /* $NetBSD: npf_state_tcp.c,v 1.1 2011/11/29 20:05:30 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2010-2011 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.1 rmind __KERNEL_RCSID(0, "$NetBSD: npf_state_tcp.c,v 1.1 2011/11/29 20:05:30 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 #ifndef _KERNEL
43 1.1 rmind #include <stdio.h>
44 1.1 rmind #include <stdbool.h>
45 1.1 rmind #include <inttypes.h>
46 1.1 rmind #endif
47 1.1 rmind #include <netinet/in.h>
48 1.1 rmind #include <netinet/tcp.h>
49 1.1 rmind #include <netinet/tcp_seq.h>
50 1.1 rmind
51 1.1 rmind #include "npf_impl.h"
52 1.1 rmind
53 1.1 rmind #if defined(_NPF_TESTING)
54 1.1 rmind void npf_state_sample(npf_state_t *);
55 1.1 rmind #define NPF_TCP_STATE_SAMPLE(nst) npf_state_sample(nst)
56 1.1 rmind #else
57 1.1 rmind #define NPF_TCP_STATE_SAMPLE(nst)
58 1.1 rmind #endif
59 1.1 rmind
60 1.1 rmind /*
61 1.1 rmind * NPF TCP states. Note: these states are different from the TCP FSM
62 1.1 rmind * states of RFC 793. Mind that packet filter is a man-in-the-middle.
63 1.1 rmind */
64 1.1 rmind #define NPF_TCPS_OK (-1)
65 1.1 rmind #define NPF_TCPS_CLOSED 0
66 1.1 rmind #define NPF_TCPS_SYN_SENT 1
67 1.1 rmind #define NPF_TCPS_SIMSYN_SENT 2
68 1.1 rmind #define NPF_TCPS_SYN_RECEIVED 3
69 1.1 rmind #define NPF_TCPS_ESTABLISHED 4
70 1.1 rmind #define NPF_TCPS_FIN_SEEN 5
71 1.1 rmind #define NPF_TCPS_CLOSE_WAIT 6
72 1.1 rmind #define NPF_TCPS_FIN_WAIT 7
73 1.1 rmind #define NPF_TCPS_CLOSING 8
74 1.1 rmind #define NPF_TCPS_LAST_ACK 9
75 1.1 rmind #define NPF_TCPS_TIME_WAIT 10
76 1.1 rmind
77 1.1 rmind #define NPF_TCP_NSTATES 11
78 1.1 rmind
79 1.1 rmind /*
80 1.1 rmind * TCP connection timeout table (in seconds).
81 1.1 rmind */
82 1.1 rmind static const u_int npf_tcp_timeouts[] __read_mostly = {
83 1.1 rmind /* Closed, timeout nearly immediately. */
84 1.1 rmind [NPF_TCPS_CLOSED] = 10,
85 1.1 rmind /* Unsynchronised states. */
86 1.1 rmind [NPF_TCPS_SYN_SENT] = 30,
87 1.1 rmind [NPF_TCPS_SIMSYN_SENT] = 30,
88 1.1 rmind [NPF_TCPS_SYN_RECEIVED] = 60,
89 1.1 rmind /* Established, timeout: 24 hours. */
90 1.1 rmind [NPF_TCPS_ESTABLISHED] = 60 * 60 * 24,
91 1.1 rmind /* Closure cases, timeout: 4 minutes (2 * MSL). */
92 1.1 rmind [NPF_TCPS_FIN_SEEN] = 60 * 2 * 2,
93 1.1 rmind [NPF_TCPS_CLOSE_WAIT] = 60 * 2 * 2,
94 1.1 rmind [NPF_TCPS_FIN_WAIT] = 60 * 2 * 2,
95 1.1 rmind [NPF_TCPS_CLOSING] = 30,
96 1.1 rmind [NPF_TCPS_LAST_ACK] = 30,
97 1.1 rmind [NPF_TCPS_TIME_WAIT] = 60 * 2 * 2,
98 1.1 rmind };
99 1.1 rmind
100 1.1 rmind #define NPF_TCP_MAXACKWIN 66000
101 1.1 rmind
102 1.1 rmind #define TH_STATE_MASK (TH_SYN | TH_ACK | TH_FIN)
103 1.1 rmind #define TH_SYNACK (TH_SYN | TH_ACK)
104 1.1 rmind #define TH_FINACK (TH_FIN | TH_ACK)
105 1.1 rmind
106 1.1 rmind /*
107 1.1 rmind * NPF transition table of a tracked TCP connection.
108 1.1 rmind *
109 1.1 rmind * There is a single state, which is changed in the following way:
110 1.1 rmind *
111 1.1 rmind * new_state = npf_tcp_fsm[old_state][direction][tcp_flags & TH_STATE_MASK];
112 1.1 rmind *
113 1.1 rmind * Note that this state is different from the state in each end (host).
114 1.1 rmind */
115 1.1 rmind
116 1.1 rmind static const int npf_tcp_fsm[NPF_TCP_NSTATES][2][TH_STATE_MASK + 1]
117 1.1 rmind __read_mostly = {
118 1.1 rmind [NPF_TCPS_CLOSED] = {
119 1.1 rmind [NPF_FLOW_FORW] = {
120 1.1 rmind /* Handshake (1): initial SYN. */
121 1.1 rmind [TH_SYN] = NPF_TCPS_SYN_SENT,
122 1.1 rmind },
123 1.1 rmind },
124 1.1 rmind [NPF_TCPS_SYN_SENT] = {
125 1.1 rmind [NPF_FLOW_FORW] = {
126 1.1 rmind /* SYN may be retransmitted. */
127 1.1 rmind [TH_SYN] = NPF_TCPS_OK,
128 1.1 rmind },
129 1.1 rmind [NPF_FLOW_BACK] = {
130 1.1 rmind /* Handshake (2): SYN-ACK is expected. */
131 1.1 rmind [TH_SYNACK] = NPF_TCPS_SYN_RECEIVED,
132 1.1 rmind /* Simultaneous initiation - SYN. */
133 1.1 rmind [TH_SYN] = NPF_TCPS_SIMSYN_SENT,
134 1.1 rmind },
135 1.1 rmind },
136 1.1 rmind [NPF_TCPS_SIMSYN_SENT] = {
137 1.1 rmind [NPF_FLOW_FORW] = {
138 1.1 rmind /* Original SYN re-transmission. */
139 1.1 rmind [TH_SYN] = NPF_TCPS_OK,
140 1.1 rmind /* SYN-ACK response to simultaneous SYN. */
141 1.1 rmind [TH_SYNACK] = NPF_TCPS_SYN_RECEIVED,
142 1.1 rmind },
143 1.1 rmind [NPF_FLOW_BACK] = {
144 1.1 rmind /* Simultaneous SYN re-transmission.*/
145 1.1 rmind [TH_SYN] = NPF_TCPS_OK,
146 1.1 rmind /* SYN-ACK response to original SYN. */
147 1.1 rmind [TH_SYNACK] = NPF_TCPS_SYN_RECEIVED,
148 1.1 rmind /* FIN may be sent at this point. */
149 1.1 rmind [TH_FIN] = NPF_TCPS_FIN_SEEN,
150 1.1 rmind [TH_FINACK] = NPF_TCPS_FIN_SEEN,
151 1.1 rmind },
152 1.1 rmind },
153 1.1 rmind [NPF_TCPS_SYN_RECEIVED] = {
154 1.1 rmind [NPF_FLOW_FORW] = {
155 1.1 rmind /* Handshake (3): ACK is expected. */
156 1.1 rmind [TH_ACK] = NPF_TCPS_ESTABLISHED,
157 1.1 rmind [TH_FIN] = NPF_TCPS_CLOSING,
158 1.1 rmind [TH_FINACK] = NPF_TCPS_CLOSING,
159 1.1 rmind },
160 1.1 rmind [NPF_FLOW_BACK] = {
161 1.1 rmind /* SYN-ACK may be retransmitted. */
162 1.1 rmind [TH_SYNACK] = NPF_TCPS_OK,
163 1.1 rmind /* XXX: ACK of late SYN in simultaneous case? */
164 1.1 rmind [TH_ACK] = NPF_TCPS_OK,
165 1.1 rmind /* XXX: Can this happen?
166 1.1 rmind [TH_FIN] = NPF_TCPS_CLOSING, */
167 1.1 rmind },
168 1.1 rmind },
169 1.1 rmind [NPF_TCPS_ESTABLISHED] = {
170 1.1 rmind /*
171 1.1 rmind * Regular ACKs (data exchange) or FIN.
172 1.1 rmind * FIN packets may have ACK set.
173 1.1 rmind */
174 1.1 rmind [NPF_FLOW_FORW] = {
175 1.1 rmind [TH_ACK] = NPF_TCPS_OK,
176 1.1 rmind /* FIN by the sender. */
177 1.1 rmind [TH_FIN] = NPF_TCPS_FIN_SEEN,
178 1.1 rmind [TH_FINACK] = NPF_TCPS_FIN_SEEN,
179 1.1 rmind },
180 1.1 rmind [NPF_FLOW_BACK] = {
181 1.1 rmind [TH_ACK] = NPF_TCPS_OK,
182 1.1 rmind /* FIN by the receiver. */
183 1.1 rmind [TH_FIN] = NPF_TCPS_FIN_SEEN,
184 1.1 rmind [TH_FINACK] = NPF_TCPS_FIN_SEEN,
185 1.1 rmind },
186 1.1 rmind },
187 1.1 rmind [NPF_TCPS_FIN_SEEN] = {
188 1.1 rmind /*
189 1.1 rmind * FIN was seen. If ACK only, connection is half-closed now,
190 1.1 rmind * need to determine which end is closed (sender or receiver).
191 1.1 rmind * However, both FIN and FIN-ACK may race here - in which
192 1.1 rmind * case we are closing immediately.
193 1.1 rmind */
194 1.1 rmind [NPF_FLOW_FORW] = {
195 1.1 rmind [TH_ACK] = NPF_TCPS_CLOSE_WAIT,
196 1.1 rmind [TH_FIN] = NPF_TCPS_CLOSING,
197 1.1 rmind [TH_FINACK] = NPF_TCPS_CLOSING,
198 1.1 rmind },
199 1.1 rmind [NPF_FLOW_BACK] = {
200 1.1 rmind [TH_ACK] = NPF_TCPS_FIN_WAIT,
201 1.1 rmind [TH_FIN] = NPF_TCPS_CLOSING,
202 1.1 rmind [TH_FINACK] = NPF_TCPS_CLOSING,
203 1.1 rmind },
204 1.1 rmind },
205 1.1 rmind [NPF_TCPS_CLOSE_WAIT] = {
206 1.1 rmind /* Sender has sent the FIN and closed its end. */
207 1.1 rmind [NPF_FLOW_FORW] = {
208 1.1 rmind [TH_ACK] = NPF_TCPS_OK,
209 1.1 rmind [TH_FIN] = NPF_TCPS_LAST_ACK,
210 1.1 rmind [TH_FINACK] = NPF_TCPS_LAST_ACK,
211 1.1 rmind },
212 1.1 rmind [NPF_FLOW_BACK] = {
213 1.1 rmind [TH_ACK] = NPF_TCPS_OK,
214 1.1 rmind [TH_FIN] = NPF_TCPS_LAST_ACK,
215 1.1 rmind [TH_FINACK] = NPF_TCPS_LAST_ACK,
216 1.1 rmind },
217 1.1 rmind },
218 1.1 rmind [NPF_TCPS_FIN_WAIT] = {
219 1.1 rmind /* Receiver has closed its end. */
220 1.1 rmind [NPF_FLOW_FORW] = {
221 1.1 rmind [TH_ACK] = NPF_TCPS_OK,
222 1.1 rmind [TH_FIN] = NPF_TCPS_LAST_ACK,
223 1.1 rmind [TH_FINACK] = NPF_TCPS_LAST_ACK,
224 1.1 rmind },
225 1.1 rmind [NPF_FLOW_BACK] = {
226 1.1 rmind [TH_ACK] = NPF_TCPS_OK,
227 1.1 rmind [TH_FIN] = NPF_TCPS_LAST_ACK,
228 1.1 rmind [TH_FINACK] = NPF_TCPS_LAST_ACK,
229 1.1 rmind },
230 1.1 rmind },
231 1.1 rmind [NPF_TCPS_CLOSING] = {
232 1.1 rmind /* Race of FINs - expecting ACK. */
233 1.1 rmind [NPF_FLOW_FORW] = {
234 1.1 rmind [TH_ACK] = NPF_TCPS_LAST_ACK,
235 1.1 rmind },
236 1.1 rmind [NPF_FLOW_BACK] = {
237 1.1 rmind [TH_ACK] = NPF_TCPS_LAST_ACK,
238 1.1 rmind },
239 1.1 rmind },
240 1.1 rmind [NPF_TCPS_LAST_ACK] = {
241 1.1 rmind /* FINs exchanged - expecting last ACK. */
242 1.1 rmind [NPF_FLOW_FORW] = {
243 1.1 rmind [TH_ACK] = NPF_TCPS_TIME_WAIT,
244 1.1 rmind },
245 1.1 rmind [NPF_FLOW_BACK] = {
246 1.1 rmind [TH_ACK] = NPF_TCPS_TIME_WAIT,
247 1.1 rmind },
248 1.1 rmind },
249 1.1 rmind [NPF_TCPS_TIME_WAIT] = {
250 1.1 rmind /* May re-open the connection as per RFC 1122. */
251 1.1 rmind [NPF_FLOW_FORW] = {
252 1.1 rmind [TH_SYN] = NPF_TCPS_SYN_SENT,
253 1.1 rmind },
254 1.1 rmind },
255 1.1 rmind };
256 1.1 rmind
257 1.1 rmind /*
258 1.1 rmind * npf_tcp_inwindow: determine whether the packet is in the TCP window
259 1.1 rmind * and thus part of the connection we are tracking.
260 1.1 rmind */
261 1.1 rmind static bool
262 1.1 rmind npf_tcp_inwindow(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst,
263 1.1 rmind const int di)
264 1.1 rmind {
265 1.1 rmind const struct tcphdr * const th = &npc->npc_l4.tcp;
266 1.1 rmind const int tcpfl = th->th_flags;
267 1.1 rmind npf_tcpstate_t *fstate, *tstate;
268 1.1 rmind int tcpdlen, wscale, ackskew;
269 1.1 rmind tcp_seq seq, ack, end;
270 1.1 rmind uint32_t win;
271 1.1 rmind
272 1.1 rmind KASSERT(npf_iscached(npc, NPC_TCP));
273 1.1 rmind KASSERT(di == NPF_FLOW_FORW || di == NPF_FLOW_BACK);
274 1.1 rmind
275 1.1 rmind /*
276 1.1 rmind * Perform SEQ/ACK numbers check against boundaries. Reference:
277 1.1 rmind *
278 1.1 rmind * Rooij G., "Real stateful TCP packet filtering in IP Filter",
279 1.1 rmind * 10th USENIX Security Symposium invited talk, Aug. 2001.
280 1.1 rmind *
281 1.1 rmind * There four boundaries are defined as following:
282 1.1 rmind * I) SEQ + LEN <= MAX { SND.ACK + MAX(SND.WIN, 1) }
283 1.1 rmind * II) SEQ >= MAX { SND.SEQ + SND.LEN }
284 1.1 rmind * III) ACK <= MAX { RCV.SEQ + RCV.LEN }
285 1.1 rmind * IV) ACK >= MAX { RCV.SEQ + RCV.LEN } - MAXACKWIN
286 1.1 rmind *
287 1.1 rmind * Let these members of npf_tcpstate_t be the maximum seen values of:
288 1.1 rmind * nst_end - SEQ + LEN
289 1.1 rmind * nst_maxend - ACK + MAX(WIN, 1)
290 1.1 rmind * nst_maxwin - MAX(WIN, 1)
291 1.1 rmind */
292 1.1 rmind
293 1.1 rmind tcpdlen = npf_tcpsaw(__UNCONST(npc), &seq, &ack, &win);
294 1.1 rmind end = seq + tcpdlen;
295 1.1 rmind if (tcpfl & TH_SYN) {
296 1.1 rmind end++;
297 1.1 rmind }
298 1.1 rmind if (tcpfl & TH_FIN) {
299 1.1 rmind end++;
300 1.1 rmind }
301 1.1 rmind
302 1.1 rmind fstate = &nst->nst_tcpst[di];
303 1.1 rmind tstate = &nst->nst_tcpst[!di];
304 1.1 rmind win = win ? (win << fstate->nst_wscale) : 1;
305 1.1 rmind
306 1.1 rmind /*
307 1.1 rmind * Initialise if the first packet.
308 1.1 rmind * Note: only case when nst_maxwin is zero.
309 1.1 rmind */
310 1.1 rmind if (__predict_false(fstate->nst_maxwin == 0)) {
311 1.1 rmind /*
312 1.1 rmind * Should be first SYN or re-transmission of SYN. State of
313 1.1 rmind * other side will get set with a SYN-ACK reply (see below).
314 1.1 rmind */
315 1.1 rmind fstate->nst_end = end;
316 1.1 rmind fstate->nst_maxend = end;
317 1.1 rmind fstate->nst_maxwin = win;
318 1.1 rmind tstate->nst_end = 0;
319 1.1 rmind tstate->nst_maxend = 0;
320 1.1 rmind tstate->nst_maxwin = 1;
321 1.1 rmind
322 1.1 rmind /*
323 1.1 rmind * Handle TCP Window Scaling (RFC 1323). Both sides may
324 1.1 rmind * send this option in their SYN packets.
325 1.1 rmind */
326 1.1 rmind if (npf_fetch_tcpopts(npc, nbuf, NULL, &wscale)) {
327 1.1 rmind fstate->nst_wscale = wscale;
328 1.1 rmind } else {
329 1.1 rmind fstate->nst_wscale = 0;
330 1.1 rmind }
331 1.1 rmind tstate->nst_wscale = 0;
332 1.1 rmind
333 1.1 rmind /* Done. */
334 1.1 rmind return true;
335 1.1 rmind }
336 1.1 rmind if (fstate->nst_end == 0) {
337 1.1 rmind /*
338 1.1 rmind * Should be a SYN-ACK reply to SYN. If SYN is not set,
339 1.1 rmind * then we are in the middle of connection and lost tracking.
340 1.1 rmind */
341 1.1 rmind fstate->nst_end = end;
342 1.1 rmind fstate->nst_maxend = end + 1;
343 1.1 rmind fstate->nst_maxwin = win;
344 1.1 rmind
345 1.1 rmind /* Handle TCP Window Scaling (must be ignored if no SYN). */
346 1.1 rmind if (tcpfl & TH_SYN) {
347 1.1 rmind fstate->nst_wscale =
348 1.1 rmind npf_fetch_tcpopts(npc, nbuf, NULL, &wscale) ?
349 1.1 rmind wscale : 0;
350 1.1 rmind }
351 1.1 rmind }
352 1.1 rmind if ((tcpfl & TH_ACK) == 0) {
353 1.1 rmind /* Pretend that an ACK was sent. */
354 1.1 rmind ack = tstate->nst_end;
355 1.1 rmind } else if ((tcpfl & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST) && ack == 0) {
356 1.1 rmind /* Workaround for some TCP stacks. */
357 1.1 rmind ack = tstate->nst_end;
358 1.1 rmind }
359 1.1 rmind if (seq == end) {
360 1.1 rmind /* If packet contains no data - assume it is valid. */
361 1.1 rmind end = fstate->nst_end;
362 1.1 rmind seq = end;
363 1.1 rmind }
364 1.1 rmind
365 1.1 rmind NPF_TCP_STATE_SAMPLE(nst);
366 1.1 rmind #if 0
367 1.1 rmind /* Strict in-order sequence for RST packets. */
368 1.1 rmind if (((tcpfl & TH_RST) != 0) && (fstate->nst_end - seq) > 1) {
369 1.1 rmind return false;
370 1.1 rmind }
371 1.1 rmind #endif
372 1.1 rmind /*
373 1.1 rmind * Determine whether the data is within previously noted window,
374 1.1 rmind * that is, upper boundary for valid data (I).
375 1.1 rmind */
376 1.1 rmind if (!SEQ_LEQ(end, fstate->nst_maxend)) {
377 1.1 rmind npf_stats_inc(NPF_STAT_INVALID_STATE_TCP1);
378 1.1 rmind return false;
379 1.1 rmind }
380 1.1 rmind
381 1.1 rmind /* Lower boundary (II), which is no more than one window back. */
382 1.1 rmind if (!SEQ_GEQ(seq, fstate->nst_end - tstate->nst_maxwin)) {
383 1.1 rmind npf_stats_inc(NPF_STAT_INVALID_STATE_TCP2);
384 1.1 rmind return false;
385 1.1 rmind }
386 1.1 rmind
387 1.1 rmind /*
388 1.1 rmind * Boundaries for valid acknowledgments (III, IV) - on predicted
389 1.1 rmind * window up or down, since packets may be fragmented.
390 1.1 rmind */
391 1.1 rmind ackskew = tstate->nst_end - ack;
392 1.1 rmind if (ackskew < -NPF_TCP_MAXACKWIN ||
393 1.1 rmind ackskew > (NPF_TCP_MAXACKWIN << fstate->nst_wscale)) {
394 1.1 rmind npf_stats_inc(NPF_STAT_INVALID_STATE_TCP3);
395 1.1 rmind return false;
396 1.1 rmind }
397 1.1 rmind
398 1.1 rmind /*
399 1.1 rmind * Packet has been passed.
400 1.1 rmind *
401 1.1 rmind * Negative ackskew might be due to fragmented packets. Since the
402 1.1 rmind * total length of the packet is unknown - bump the boundary.
403 1.1 rmind */
404 1.1 rmind if (ackskew < 0) {
405 1.1 rmind tstate->nst_end = end;
406 1.1 rmind }
407 1.1 rmind /* Keep track of the maximum window seen. */
408 1.1 rmind if (fstate->nst_maxwin < win) {
409 1.1 rmind fstate->nst_maxwin = win;
410 1.1 rmind }
411 1.1 rmind if (SEQ_GT(end, fstate->nst_end)) {
412 1.1 rmind fstate->nst_end = end;
413 1.1 rmind }
414 1.1 rmind /* Note the window for upper boundary. */
415 1.1 rmind if (SEQ_GEQ(ack + win, tstate->nst_maxend)) {
416 1.1 rmind tstate->nst_maxend = ack + win;
417 1.1 rmind }
418 1.1 rmind return true;
419 1.1 rmind }
420 1.1 rmind
421 1.1 rmind bool
422 1.1 rmind npf_state_tcp(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst, int di)
423 1.1 rmind {
424 1.1 rmind const struct tcphdr * const th = &npc->npc_l4.tcp;
425 1.1 rmind const int tcpfl = th->th_flags, state = nst->nst_state;
426 1.1 rmind int nstate;
427 1.1 rmind
428 1.1 rmind /* Look for a transition to a new state. */
429 1.1 rmind if (__predict_true((tcpfl & TH_RST) == 0)) {
430 1.1 rmind nstate = npf_tcp_fsm[state][di][tcpfl & TH_STATE_MASK];
431 1.1 rmind } else if (state == NPF_TCPS_TIME_WAIT) {
432 1.1 rmind /* Prevent TIME-WAIT assassination (RFC 1337). */
433 1.1 rmind nstate = NPF_TCPS_OK;
434 1.1 rmind } else {
435 1.1 rmind nstate = NPF_TCPS_CLOSED;
436 1.1 rmind }
437 1.1 rmind /* Determine whether TCP packet really belongs to this connection. */
438 1.1 rmind if (!npf_tcp_inwindow(npc, nbuf, nst, di)) {
439 1.1 rmind return false;
440 1.1 rmind }
441 1.1 rmind if (__predict_true(nstate == NPF_TCPS_OK)) {
442 1.1 rmind return true;
443 1.1 rmind }
444 1.1 rmind nst->nst_state = nstate;
445 1.1 rmind return true;
446 1.1 rmind }
447 1.1 rmind
448 1.1 rmind int
449 1.1 rmind npf_state_tcp_timeout(const npf_state_t *nst)
450 1.1 rmind {
451 1.1 rmind const u_int state = nst->nst_state;
452 1.1 rmind
453 1.1 rmind KASSERT(state < NPF_TCP_NSTATES);
454 1.1 rmind return npf_tcp_timeouts[state];
455 1.1 rmind }
456