npf_state.c revision 1.3 1 1.3 rmind /* $NetBSD: npf_state.c,v 1.3 2011/01/18 20:33:46 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2010 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 state engine to track connections.
34 1.1 rmind */
35 1.1 rmind
36 1.1 rmind #include <sys/cdefs.h>
37 1.3 rmind __KERNEL_RCSID(0, "$NetBSD: npf_state.c,v 1.3 2011/01/18 20:33:46 rmind Exp $");
38 1.1 rmind
39 1.1 rmind #include <sys/param.h>
40 1.1 rmind #include <sys/systm.h>
41 1.1 rmind
42 1.1 rmind #include <sys/mutex.h>
43 1.1 rmind #include <netinet/in.h>
44 1.1 rmind #include <netinet/tcp.h>
45 1.1 rmind #include <netinet/tcp_seq.h>
46 1.3 rmind #include <netinet/tcp_fsm.h>
47 1.1 rmind
48 1.1 rmind #include "npf_impl.h"
49 1.1 rmind
50 1.3 rmind /* TCP session expiration table. */
51 1.3 rmind static const u_int tcp_expire_table[ ] __read_mostly = {
52 1.3 rmind /* Initial synchronisation. Timeout: 30 sec and 1 minute. */
53 1.3 rmind [TCPS_SYN_SENT] = 30,
54 1.3 rmind [TCPS_SYN_RECEIVED] = 60,
55 1.3 rmind /* Established (synchronised). Timeout: 24 hours. */
56 1.3 rmind [TCPS_ESTABLISHED] = 60 * 60 * 24,
57 1.3 rmind [TCPS_FIN_WAIT_1] = 60 * 60 * 24,
58 1.3 rmind [TCPS_FIN_WAIT_2] = 60 * 60 * 24,
59 1.3 rmind /* UNUSED [TCPS_CLOSE_WAIT] = 60 * 60 * 24, */
60 1.3 rmind /* Closure. Timeout: 4 minutes (2 * MSL). */
61 1.3 rmind [TCPS_CLOSING] = 60 * 4,
62 1.3 rmind [TCPS_LAST_ACK] = 60 * 4,
63 1.3 rmind [TCPS_TIME_WAIT] = 60 * 4,
64 1.3 rmind /* Fully closed. Timeout immediately. */
65 1.3 rmind [TCPS_CLOSED] = 0
66 1.3 rmind };
67 1.1 rmind
68 1.3 rmind /* Session expiration table. */
69 1.3 rmind static const u_int expire_table[ ] __read_mostly = {
70 1.3 rmind [IPPROTO_UDP] = 60, /* 1 min */
71 1.3 rmind [IPPROTO_ICMP] = 30 /* 30 sec */
72 1.1 rmind };
73 1.1 rmind
74 1.3 rmind #define MAXACKWINDOW 66000
75 1.3 rmind
76 1.1 rmind static bool
77 1.1 rmind npf_tcp_inwindow(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst,
78 1.1 rmind const bool forw)
79 1.1 rmind {
80 1.3 rmind const struct tcphdr * const th = &npc->npc_l4.tcp;
81 1.1 rmind const int tcpfl = th->th_flags;
82 1.1 rmind npf_tcpstate_t *fstate, *tstate;
83 1.1 rmind int tcpdlen, wscale, ackskew;
84 1.1 rmind tcp_seq seq, ack, end;
85 1.1 rmind uint32_t win;
86 1.1 rmind
87 1.1 rmind KASSERT(npf_iscached(npc, NPC_TCP));
88 1.1 rmind tcpdlen = npf_tcpsaw(__UNCONST(npc), &seq, &ack, &win);
89 1.1 rmind end = seq + tcpdlen;
90 1.1 rmind if (tcpfl & TH_SYN) {
91 1.1 rmind end++;
92 1.1 rmind }
93 1.1 rmind if (tcpfl & TH_FIN) {
94 1.1 rmind end++;
95 1.1 rmind }
96 1.1 rmind
97 1.1 rmind /*
98 1.1 rmind * Perform SEQ/ACK numbers check against boundaries. Reference:
99 1.1 rmind *
100 1.1 rmind * Rooij G., "Real stateful TCP packet filtering in IP Filter",
101 1.1 rmind * 10th USENIX Security Symposium invited talk, Aug. 2001.
102 1.1 rmind */
103 1.1 rmind
104 1.1 rmind fstate = &nst->nst_tcpst[forw ? 0 : 1];
105 1.1 rmind tstate = &nst->nst_tcpst[forw ? 1 : 0];
106 1.1 rmind win = win ? (win << fstate->nst_wscale) : 1;
107 1.1 rmind
108 1.1 rmind if (tcpfl == TH_SYN) {
109 1.1 rmind /*
110 1.1 rmind * First SYN or re-transmission of SYN. Initialize all
111 1.1 rmind * values. State of other side will get set with a SYN-ACK
112 1.1 rmind * reply (see below).
113 1.1 rmind */
114 1.1 rmind fstate->nst_seqend = end;
115 1.1 rmind fstate->nst_ackend = end;
116 1.1 rmind fstate->nst_maxwin = win;
117 1.1 rmind tstate->nst_ackend = 0;
118 1.1 rmind tstate->nst_ackend = 0;
119 1.1 rmind tstate->nst_maxwin = 0;
120 1.1 rmind /*
121 1.1 rmind * Handle TCP Window Scaling (RFC 1323). Both sides may
122 1.1 rmind * send this option in their SYN packets.
123 1.1 rmind */
124 1.1 rmind if (npf_fetch_tcpopts(npc, nbuf, NULL, &wscale)) {
125 1.1 rmind fstate->nst_wscale = wscale;
126 1.1 rmind } else {
127 1.1 rmind fstate->nst_wscale = 0;
128 1.1 rmind }
129 1.1 rmind tstate->nst_wscale = 0;
130 1.1 rmind /* Done. */
131 1.1 rmind return true;
132 1.1 rmind }
133 1.1 rmind if (fstate->nst_seqend == 0) {
134 1.1 rmind /*
135 1.1 rmind * Should be a SYN-ACK reply to SYN. If SYN is not set,
136 1.1 rmind * then we are in the middle connection and lost tracking.
137 1.1 rmind */
138 1.1 rmind fstate->nst_seqend = end;
139 1.1 rmind fstate->nst_ackend = end + 1;
140 1.1 rmind fstate->nst_maxwin = 1;
141 1.1 rmind
142 1.1 rmind /* Handle TCP Window Scaling (must be ignored if no SYN). */
143 1.1 rmind if (tcpfl & TH_SYN) {
144 1.1 rmind fstate->nst_wscale =
145 1.1 rmind npf_fetch_tcpopts(npc, nbuf, NULL, &wscale) ?
146 1.1 rmind wscale : 0;
147 1.1 rmind }
148 1.1 rmind }
149 1.1 rmind if ((tcpfl & TH_ACK) == 0) {
150 1.1 rmind /* Pretend that an ACK was sent. */
151 1.1 rmind ack = tstate->nst_seqend;
152 1.1 rmind } else if ((tcpfl & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST) && ack == 0) {
153 1.1 rmind /* Workaround for some TCP stacks. */
154 1.1 rmind ack = tstate->nst_seqend;
155 1.1 rmind }
156 1.1 rmind if (seq == end) {
157 1.1 rmind /* If packet contains no data - assume it is valid. */
158 1.1 rmind end = fstate->nst_seqend;
159 1.1 rmind seq = end;
160 1.1 rmind }
161 1.1 rmind
162 1.1 rmind /*
163 1.1 rmind * Determine whether the data is within previously noted window,
164 1.1 rmind * that is, upper boundary for valid data (I).
165 1.1 rmind */
166 1.1 rmind if (!SEQ_GEQ(fstate->nst_ackend, end)) {
167 1.2 rmind npf_stats_inc(NPF_STAT_INVALID_STATE_TCP1);
168 1.1 rmind return false;
169 1.1 rmind }
170 1.1 rmind /* Lower boundary (II), which is no more than one window back. */
171 1.1 rmind if (!SEQ_GEQ(seq, fstate->nst_seqend - tstate->nst_maxwin)) {
172 1.2 rmind npf_stats_inc(NPF_STAT_INVALID_STATE_TCP2);
173 1.1 rmind return false;
174 1.1 rmind }
175 1.1 rmind /*
176 1.1 rmind * Boundaries for valid acknowledgments (III, IV) - on predicted
177 1.1 rmind * window up or down, since packets may be fragmented.
178 1.1 rmind */
179 1.1 rmind ackskew = tstate->nst_seqend - ack;
180 1.1 rmind if (ackskew < -MAXACKWINDOW || ackskew > MAXACKWINDOW) {
181 1.2 rmind npf_stats_inc(NPF_STAT_INVALID_STATE_TCP3);
182 1.1 rmind return false;
183 1.1 rmind }
184 1.1 rmind
185 1.1 rmind /*
186 1.2 rmind * Packet is passed now.
187 1.2 rmind *
188 1.1 rmind * Negative ackskew might be due to fragmented packets. Since the
189 1.1 rmind * total length of the packet is unknown - bump the boundary.
190 1.1 rmind */
191 1.1 rmind if (ackskew < 0) {
192 1.1 rmind tstate->nst_seqend = end;
193 1.1 rmind }
194 1.1 rmind /* Keep track of the maximum window seen. */
195 1.1 rmind if (fstate->nst_maxwin < win) {
196 1.1 rmind fstate->nst_maxwin = win;
197 1.1 rmind }
198 1.1 rmind if (SEQ_GT(end, fstate->nst_seqend)) {
199 1.1 rmind fstate->nst_seqend = end;
200 1.1 rmind }
201 1.1 rmind /* Note the window for upper boundary. */
202 1.1 rmind if (SEQ_GEQ(ack + win, tstate->nst_ackend)) {
203 1.1 rmind tstate->nst_ackend = ack + win;
204 1.1 rmind }
205 1.1 rmind return true;
206 1.1 rmind }
207 1.1 rmind
208 1.1 rmind static inline bool
209 1.1 rmind npf_state_tcp(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst,
210 1.1 rmind const bool forw)
211 1.1 rmind {
212 1.3 rmind const struct tcphdr * const th = &npc->npc_l4.tcp;
213 1.3 rmind const int tcpfl = th->th_flags, state = nst->nst_state;
214 1.3 rmind #if 0
215 1.3 rmind /* Determine whether TCP packet really belongs to this connection. */
216 1.3 rmind if (!npf_tcp_inwindow(npc, nbuf, nst, forw)) {
217 1.3 rmind return false;
218 1.3 rmind }
219 1.3 rmind #endif
220 1.1 rmind /*
221 1.3 rmind * Handle 3-way handshake (SYN -> SYN,ACK -> ACK), connection
222 1.3 rmind * reset (RST), half-open connections, connection closure, etc.
223 1.1 rmind */
224 1.3 rmind if (__predict_false(tcpfl & TH_RST)) {
225 1.3 rmind nst->nst_state = TCPS_CLOSED;
226 1.3 rmind return true;
227 1.3 rmind }
228 1.3 rmind switch (state) {
229 1.3 rmind case TCPS_ESTABLISHED:
230 1.3 rmind case TCPS_FIN_WAIT_2:
231 1.3 rmind /* Common case - connection is established. */
232 1.3 rmind if ((tcpfl & (TH_SYN | TH_ACK | TH_FIN)) == TH_ACK) {
233 1.3 rmind return true;
234 1.1 rmind }
235 1.3 rmind /* Otherwise, can only be a FIN. */
236 1.3 rmind if ((tcpfl & TH_FIN) == 0) {
237 1.3 rmind break;
238 1.3 rmind }
239 1.3 rmind /* XXX see below TCPS_CLOSE_WAIT */
240 1.3 rmind if (state != TCPS_FIN_WAIT_2) {
241 1.3 rmind /* First FIN: closure of one end. */
242 1.3 rmind nst->nst_state = TCPS_FIN_WAIT_1;
243 1.3 rmind } else {
244 1.3 rmind /* Second FIN: connection closure, wait for ACK. */
245 1.3 rmind nst->nst_state = TCPS_LAST_ACK;
246 1.3 rmind }
247 1.3 rmind return true;
248 1.3 rmind case TCPS_SYN_SENT:
249 1.3 rmind /* After SYN expecting SYN-ACK. */
250 1.1 rmind if (tcpfl == (TH_SYN | TH_ACK) && !forw) {
251 1.1 rmind /* Received backwards SYN-ACK. */
252 1.3 rmind nst->nst_state = TCPS_SYN_RECEIVED;
253 1.3 rmind return true;
254 1.3 rmind }
255 1.3 rmind if (tcpfl == TH_SYN && forw) {
256 1.1 rmind /* Re-transmission of SYN. */
257 1.3 rmind return true;
258 1.1 rmind }
259 1.1 rmind break;
260 1.3 rmind case TCPS_SYN_RECEIVED:
261 1.1 rmind /* SYN-ACK was seen, expecting ACK. */
262 1.3 rmind if ((tcpfl & (TH_SYN | TH_ACK | TH_FIN)) == TH_ACK) {
263 1.3 rmind /* ACK - establish connection. */
264 1.3 rmind nst->nst_state = TCPS_ESTABLISHED;
265 1.3 rmind return true;
266 1.3 rmind }
267 1.3 rmind if (tcpfl == (TH_SYN | TH_ACK)) {
268 1.3 rmind /* Re-transmission of SYN-ACK. */
269 1.3 rmind return true;
270 1.3 rmind }
271 1.3 rmind break;
272 1.3 rmind case TCPS_CLOSE_WAIT:
273 1.3 rmind /* UNUSED */
274 1.3 rmind case TCPS_FIN_WAIT_1:
275 1.3 rmind /*
276 1.3 rmind * XXX: FIN re-transmission is not handled, use TCPS_CLOSE_WAIT.
277 1.3 rmind */
278 1.3 rmind /*
279 1.3 rmind * First FIN was seen, expecting ACK. However, we may receive
280 1.3 rmind * a simultaneous FIN or exchange of FINs with FIN-ACK.
281 1.3 rmind */
282 1.3 rmind if ((tcpfl & (TH_ACK | TH_FIN)) == (TH_ACK | TH_FIN)) {
283 1.3 rmind /* Exchange of FINs with ACK. Wait for last ACK. */
284 1.3 rmind nst->nst_state = TCPS_LAST_ACK;
285 1.3 rmind return true;
286 1.3 rmind } else if (tcpfl & TH_ACK) {
287 1.3 rmind /* ACK of first FIN. */
288 1.3 rmind nst->nst_state = TCPS_FIN_WAIT_2;
289 1.3 rmind return true;
290 1.3 rmind } else if (tcpfl & TH_FIN) {
291 1.3 rmind /* Simultaneous FIN. Need to wait for ACKs. */
292 1.3 rmind nst->nst_state = TCPS_CLOSING;
293 1.3 rmind return true;
294 1.3 rmind }
295 1.3 rmind break;
296 1.3 rmind case TCPS_CLOSING:
297 1.3 rmind case TCPS_LAST_ACK:
298 1.3 rmind case TCPS_TIME_WAIT:
299 1.3 rmind /* Expecting only ACK. */
300 1.3 rmind if ((tcpfl & (TH_SYN | TH_ACK | TH_FIN)) != TH_ACK) {
301 1.1 rmind return false;
302 1.1 rmind }
303 1.3 rmind switch (state) {
304 1.3 rmind case TCPS_CLOSING:
305 1.3 rmind /* One ACK noted, wait for last one. */
306 1.3 rmind nst->nst_state = TCPS_LAST_ACK;
307 1.3 rmind break;
308 1.3 rmind case TCPS_LAST_ACK:
309 1.3 rmind /* Last ACK received, quiet wait now. */
310 1.3 rmind nst->nst_state = TCPS_TIME_WAIT;
311 1.3 rmind break;
312 1.3 rmind }
313 1.3 rmind return true;
314 1.3 rmind case TCPS_CLOSED:
315 1.3 rmind /* XXX: Drop or pass? */
316 1.1 rmind break;
317 1.1 rmind default:
318 1.1 rmind npf_state_dump(nst);
319 1.1 rmind KASSERT(false);
320 1.1 rmind }
321 1.3 rmind return false;
322 1.1 rmind }
323 1.1 rmind
324 1.1 rmind bool
325 1.1 rmind npf_state_init(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst)
326 1.1 rmind {
327 1.1 rmind const int proto = npf_cache_ipproto(npc);
328 1.1 rmind
329 1.1 rmind KASSERT(npf_iscached(npc, NPC_IP46 | NPC_LAYER4));
330 1.2 rmind
331 1.2 rmind mutex_init(&nst->nst_lock, MUTEX_DEFAULT, IPL_SOFTNET);
332 1.2 rmind
333 1.1 rmind if (proto == IPPROTO_TCP) {
334 1.1 rmind const struct tcphdr *th = &npc->npc_l4.tcp;
335 1.3 rmind
336 1.1 rmind /* TCP case: must be SYN. */
337 1.1 rmind KASSERT(npf_iscached(npc, NPC_TCP));
338 1.1 rmind if (th->th_flags != TH_SYN) {
339 1.2 rmind npf_stats_inc(NPF_STAT_INVALID_STATE);
340 1.1 rmind return false;
341 1.1 rmind }
342 1.1 rmind /* Initial values for TCP window and sequence tracking. */
343 1.1 rmind if (!npf_tcp_inwindow(npc, nbuf, nst, true)) {
344 1.2 rmind npf_stats_inc(NPF_STAT_INVALID_STATE);
345 1.1 rmind return false;
346 1.1 rmind }
347 1.1 rmind }
348 1.3 rmind
349 1.3 rmind /*
350 1.3 rmind * Initial state: SYN sent, waiting for response from the other side.
351 1.3 rmind * Note: for UDP or ICMP, reuse SYN-sent flag to note response.
352 1.3 rmind */
353 1.3 rmind nst->nst_state = TCPS_SYN_SENT;
354 1.1 rmind return true;
355 1.1 rmind }
356 1.1 rmind
357 1.1 rmind void
358 1.1 rmind npf_state_destroy(npf_state_t *nst)
359 1.1 rmind {
360 1.1 rmind
361 1.1 rmind mutex_destroy(&nst->nst_lock);
362 1.1 rmind }
363 1.1 rmind
364 1.1 rmind bool
365 1.1 rmind npf_state_inspect(const npf_cache_t *npc, nbuf_t *nbuf,
366 1.1 rmind npf_state_t *nst, const bool forw)
367 1.1 rmind {
368 1.1 rmind const int proto = npf_cache_ipproto(npc);
369 1.1 rmind bool ret;
370 1.1 rmind
371 1.1 rmind mutex_enter(&nst->nst_lock);
372 1.1 rmind switch (proto) {
373 1.1 rmind case IPPROTO_TCP:
374 1.1 rmind /* Handle TCP. */
375 1.1 rmind ret = npf_state_tcp(npc, nbuf, nst, forw);
376 1.1 rmind break;
377 1.1 rmind default:
378 1.3 rmind /*
379 1.3 rmind * Handle UDP or ICMP response for opening session.
380 1.3 rmind */
381 1.3 rmind if (nst->nst_state == TCPS_SYN_SENT && !forw) {
382 1.3 rmind nst->nst_state= TCPS_ESTABLISHED;
383 1.1 rmind }
384 1.1 rmind ret = true;
385 1.1 rmind }
386 1.1 rmind mutex_exit(&nst->nst_lock);
387 1.2 rmind if (__predict_false(!ret)) {
388 1.2 rmind npf_stats_inc(NPF_STAT_INVALID_STATE);
389 1.2 rmind }
390 1.1 rmind return ret;
391 1.1 rmind }
392 1.1 rmind
393 1.3 rmind /*
394 1.3 rmind * npf_state_etime: return session expiration time according to the state.
395 1.3 rmind */
396 1.1 rmind int
397 1.1 rmind npf_state_etime(const npf_state_t *nst, const int proto)
398 1.1 rmind {
399 1.3 rmind const int state = nst->nst_state;
400 1.1 rmind
401 1.3 rmind if (__predict_true(proto == IPPROTO_TCP)) {
402 1.3 rmind return tcp_expire_table[state];
403 1.1 rmind }
404 1.3 rmind return expire_table[proto];
405 1.1 rmind }
406 1.1 rmind
407 1.1 rmind #if defined(DDB) || defined(_NPF_TESTING)
408 1.1 rmind
409 1.1 rmind void
410 1.1 rmind npf_state_dump(npf_state_t *nst)
411 1.1 rmind {
412 1.1 rmind npf_tcpstate_t *fst = &nst->nst_tcpst[0], *tst = &nst->nst_tcpst[1];
413 1.1 rmind
414 1.1 rmind printf("\tstate (%p) %d:\n\t\t"
415 1.1 rmind "F { seqend %u ackend %u mwin %u wscale %u }\n\t\t"
416 1.3 rmind "T { seqend %u ackend %u mwin %u wscale %u }\n",
417 1.1 rmind nst, nst->nst_state,
418 1.1 rmind fst->nst_seqend, fst->nst_ackend, fst->nst_maxwin, fst->nst_wscale,
419 1.1 rmind tst->nst_seqend, tst->nst_ackend, tst->nst_maxwin, tst->nst_wscale
420 1.1 rmind );
421 1.1 rmind }
422 1.1 rmind
423 1.1 rmind #endif
424