npf_state.c revision 1.1 1 1.1 rmind /* $NetBSD: npf_state.c,v 1.1 2010/11/11 06:30:39 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.1 rmind __KERNEL_RCSID(0, "$NetBSD: npf_state.c,v 1.1 2010/11/11 06:30:39 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.1 rmind
47 1.1 rmind #include "npf_impl.h"
48 1.1 rmind
49 1.1 rmind #define MAXACKWINDOW 66000
50 1.1 rmind
51 1.1 rmind /* Session expiration table. XXX revisit later */
52 1.1 rmind static const u_int expire_table[ ] = {
53 1.1 rmind [IPPROTO_TCP] = 86400, /* 24 hours */
54 1.1 rmind [IPPROTO_UDP] = 120, /* 2 min */
55 1.1 rmind [IPPROTO_ICMP] = 30 /* 1 min */
56 1.1 rmind };
57 1.1 rmind
58 1.1 rmind static bool
59 1.1 rmind npf_tcp_inwindow(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst,
60 1.1 rmind const bool forw)
61 1.1 rmind {
62 1.1 rmind const struct tcphdr *th = &npc->npc_l4.tcp;
63 1.1 rmind const int tcpfl = th->th_flags;
64 1.1 rmind npf_tcpstate_t *fstate, *tstate;
65 1.1 rmind int tcpdlen, wscale, ackskew;
66 1.1 rmind tcp_seq seq, ack, end;
67 1.1 rmind uint32_t win;
68 1.1 rmind
69 1.1 rmind KASSERT(npf_iscached(npc, NPC_TCP));
70 1.1 rmind tcpdlen = npf_tcpsaw(__UNCONST(npc), &seq, &ack, &win);
71 1.1 rmind end = seq + tcpdlen;
72 1.1 rmind if (tcpfl & TH_SYN) {
73 1.1 rmind end++;
74 1.1 rmind }
75 1.1 rmind if (tcpfl & TH_FIN) {
76 1.1 rmind end++;
77 1.1 rmind }
78 1.1 rmind
79 1.1 rmind /*
80 1.1 rmind * Perform SEQ/ACK numbers check against boundaries. Reference:
81 1.1 rmind *
82 1.1 rmind * Rooij G., "Real stateful TCP packet filtering in IP Filter",
83 1.1 rmind * 10th USENIX Security Symposium invited talk, Aug. 2001.
84 1.1 rmind */
85 1.1 rmind
86 1.1 rmind fstate = &nst->nst_tcpst[forw ? 0 : 1];
87 1.1 rmind tstate = &nst->nst_tcpst[forw ? 1 : 0];
88 1.1 rmind win = win ? (win << fstate->nst_wscale) : 1;
89 1.1 rmind
90 1.1 rmind if (tcpfl == TH_SYN) {
91 1.1 rmind /*
92 1.1 rmind * First SYN or re-transmission of SYN. Initialize all
93 1.1 rmind * values. State of other side will get set with a SYN-ACK
94 1.1 rmind * reply (see below).
95 1.1 rmind */
96 1.1 rmind fstate->nst_seqend = end;
97 1.1 rmind fstate->nst_ackend = end;
98 1.1 rmind fstate->nst_maxwin = win;
99 1.1 rmind tstate->nst_ackend = 0;
100 1.1 rmind tstate->nst_ackend = 0;
101 1.1 rmind tstate->nst_maxwin = 0;
102 1.1 rmind /*
103 1.1 rmind * Handle TCP Window Scaling (RFC 1323). Both sides may
104 1.1 rmind * send this option in their SYN packets.
105 1.1 rmind */
106 1.1 rmind if (npf_fetch_tcpopts(npc, nbuf, NULL, &wscale)) {
107 1.1 rmind fstate->nst_wscale = wscale;
108 1.1 rmind } else {
109 1.1 rmind fstate->nst_wscale = 0;
110 1.1 rmind }
111 1.1 rmind tstate->nst_wscale = 0;
112 1.1 rmind /* Done. */
113 1.1 rmind return true;
114 1.1 rmind }
115 1.1 rmind if (fstate->nst_seqend == 0) {
116 1.1 rmind /*
117 1.1 rmind * Should be a SYN-ACK reply to SYN. If SYN is not set,
118 1.1 rmind * then we are in the middle connection and lost tracking.
119 1.1 rmind */
120 1.1 rmind fstate->nst_seqend = end;
121 1.1 rmind fstate->nst_ackend = end + 1;
122 1.1 rmind fstate->nst_maxwin = 1;
123 1.1 rmind
124 1.1 rmind /* Handle TCP Window Scaling (must be ignored if no SYN). */
125 1.1 rmind if (tcpfl & TH_SYN) {
126 1.1 rmind fstate->nst_wscale =
127 1.1 rmind npf_fetch_tcpopts(npc, nbuf, NULL, &wscale) ?
128 1.1 rmind wscale : 0;
129 1.1 rmind }
130 1.1 rmind }
131 1.1 rmind if ((tcpfl & TH_ACK) == 0) {
132 1.1 rmind /* Pretend that an ACK was sent. */
133 1.1 rmind ack = tstate->nst_seqend;
134 1.1 rmind } else if ((tcpfl & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST) && ack == 0) {
135 1.1 rmind /* Workaround for some TCP stacks. */
136 1.1 rmind ack = tstate->nst_seqend;
137 1.1 rmind }
138 1.1 rmind if (seq == end) {
139 1.1 rmind /* If packet contains no data - assume it is valid. */
140 1.1 rmind end = fstate->nst_seqend;
141 1.1 rmind seq = end;
142 1.1 rmind }
143 1.1 rmind
144 1.1 rmind /*
145 1.1 rmind * Determine whether the data is within previously noted window,
146 1.1 rmind * that is, upper boundary for valid data (I).
147 1.1 rmind */
148 1.1 rmind if (!SEQ_GEQ(fstate->nst_ackend, end)) {
149 1.1 rmind return false;
150 1.1 rmind }
151 1.1 rmind /* Lower boundary (II), which is no more than one window back. */
152 1.1 rmind if (!SEQ_GEQ(seq, fstate->nst_seqend - tstate->nst_maxwin)) {
153 1.1 rmind return false;
154 1.1 rmind }
155 1.1 rmind /*
156 1.1 rmind * Boundaries for valid acknowledgments (III, IV) - on predicted
157 1.1 rmind * window up or down, since packets may be fragmented.
158 1.1 rmind */
159 1.1 rmind ackskew = tstate->nst_seqend - ack;
160 1.1 rmind if (ackskew < -MAXACKWINDOW || ackskew > MAXACKWINDOW) {
161 1.1 rmind return false;
162 1.1 rmind }
163 1.1 rmind
164 1.1 rmind /*
165 1.1 rmind * Negative ackskew might be due to fragmented packets. Since the
166 1.1 rmind * total length of the packet is unknown - bump the boundary.
167 1.1 rmind */
168 1.1 rmind if (ackskew < 0) {
169 1.1 rmind tstate->nst_seqend = end;
170 1.1 rmind }
171 1.1 rmind /* Keep track of the maximum window seen. */
172 1.1 rmind if (fstate->nst_maxwin < win) {
173 1.1 rmind fstate->nst_maxwin = win;
174 1.1 rmind }
175 1.1 rmind if (SEQ_GT(end, fstate->nst_seqend)) {
176 1.1 rmind fstate->nst_seqend = end;
177 1.1 rmind }
178 1.1 rmind /* Note the window for upper boundary. */
179 1.1 rmind if (SEQ_GEQ(ack + win, tstate->nst_ackend)) {
180 1.1 rmind tstate->nst_ackend = ack + win;
181 1.1 rmind }
182 1.1 rmind return true;
183 1.1 rmind }
184 1.1 rmind
185 1.1 rmind static inline bool
186 1.1 rmind npf_state_tcp(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst,
187 1.1 rmind const bool forw)
188 1.1 rmind {
189 1.1 rmind const struct tcphdr *th = &npc->npc_l4.tcp;
190 1.1 rmind const int tcpfl = th->th_flags;
191 1.1 rmind
192 1.1 rmind /*
193 1.1 rmind * Handle 3-way handshake (SYN -> SYN,ACK -> ACK).
194 1.1 rmind */
195 1.1 rmind switch (nst->nst_state) {
196 1.1 rmind case ST_ESTABLISHED:
197 1.1 rmind /* Common case - connection established. */
198 1.1 rmind if (tcpfl & TH_ACK) {
199 1.1 rmind /*
200 1.1 rmind * Data transmission.
201 1.1 rmind */
202 1.1 rmind } else if (tcpfl & TH_FIN) {
203 1.1 rmind /* XXX TODO */
204 1.1 rmind }
205 1.1 rmind break;
206 1.1 rmind case ST_OPENING:
207 1.1 rmind /* SYN has been sent, expecting SYN-ACK. */
208 1.1 rmind if (tcpfl == (TH_SYN | TH_ACK) && !forw) {
209 1.1 rmind /* Received backwards SYN-ACK. */
210 1.1 rmind nst->nst_state = ST_ACKNOWLEDGE;
211 1.1 rmind } else if (tcpfl == TH_SYN && forw) {
212 1.1 rmind /* Re-transmission of SYN. */
213 1.1 rmind } else {
214 1.1 rmind return false;
215 1.1 rmind }
216 1.1 rmind break;
217 1.1 rmind case ST_ACKNOWLEDGE:
218 1.1 rmind /* SYN-ACK was seen, expecting ACK. */
219 1.1 rmind if (tcpfl == TH_ACK && forw) {
220 1.1 rmind nst->nst_state = ST_ESTABLISHED;
221 1.1 rmind } else {
222 1.1 rmind return false;
223 1.1 rmind }
224 1.1 rmind break;
225 1.1 rmind case ST_CLOSING:
226 1.1 rmind /* XXX TODO */
227 1.1 rmind break;
228 1.1 rmind default:
229 1.1 rmind npf_state_dump(nst);
230 1.1 rmind KASSERT(false);
231 1.1 rmind }
232 1.1 rmind return npf_tcp_inwindow(npc, nbuf, nst, forw);
233 1.1 rmind }
234 1.1 rmind
235 1.1 rmind bool
236 1.1 rmind npf_state_init(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst)
237 1.1 rmind {
238 1.1 rmind const int proto = npf_cache_ipproto(npc);
239 1.1 rmind
240 1.1 rmind KASSERT(npf_iscached(npc, NPC_IP46 | NPC_LAYER4));
241 1.1 rmind if (proto == IPPROTO_TCP) {
242 1.1 rmind const struct tcphdr *th = &npc->npc_l4.tcp;
243 1.1 rmind /* TCP case: must be SYN. */
244 1.1 rmind KASSERT(npf_iscached(npc, NPC_TCP));
245 1.1 rmind if (th->th_flags != TH_SYN) {
246 1.1 rmind return false;
247 1.1 rmind }
248 1.1 rmind /* Initial values for TCP window and sequence tracking. */
249 1.1 rmind if (!npf_tcp_inwindow(npc, nbuf, nst, true)) {
250 1.1 rmind return false;
251 1.1 rmind }
252 1.1 rmind }
253 1.1 rmind mutex_init(&nst->nst_lock, MUTEX_DEFAULT, IPL_SOFTNET);
254 1.1 rmind nst->nst_state = ST_OPENING;
255 1.1 rmind return true;
256 1.1 rmind }
257 1.1 rmind
258 1.1 rmind void
259 1.1 rmind npf_state_destroy(npf_state_t *nst)
260 1.1 rmind {
261 1.1 rmind
262 1.1 rmind KASSERT(nst->nst_state != 0);
263 1.1 rmind mutex_destroy(&nst->nst_lock);
264 1.1 rmind }
265 1.1 rmind
266 1.1 rmind bool
267 1.1 rmind npf_state_inspect(const npf_cache_t *npc, nbuf_t *nbuf,
268 1.1 rmind npf_state_t *nst, const bool forw)
269 1.1 rmind {
270 1.1 rmind const int proto = npf_cache_ipproto(npc);
271 1.1 rmind bool ret;
272 1.1 rmind
273 1.1 rmind mutex_enter(&nst->nst_lock);
274 1.1 rmind switch (proto) {
275 1.1 rmind case IPPROTO_TCP:
276 1.1 rmind /* Handle TCP. */
277 1.1 rmind ret = npf_state_tcp(npc, nbuf, nst, forw);
278 1.1 rmind break;
279 1.1 rmind default:
280 1.1 rmind /* Handle UDP or ICMP response for opening session. */
281 1.1 rmind if (nst->nst_state == ST_OPENING && !forw) {
282 1.1 rmind nst->nst_state = ST_ESTABLISHED;
283 1.1 rmind }
284 1.1 rmind ret = true;
285 1.1 rmind }
286 1.1 rmind mutex_exit(&nst->nst_lock);
287 1.1 rmind return ret;
288 1.1 rmind }
289 1.1 rmind
290 1.1 rmind int
291 1.1 rmind npf_state_etime(const npf_state_t *nst, const int proto)
292 1.1 rmind {
293 1.1 rmind
294 1.1 rmind if (nst->nst_state == ST_ESTABLISHED) {
295 1.1 rmind return expire_table[proto];
296 1.1 rmind }
297 1.1 rmind return 10; /* XXX TODO */
298 1.1 rmind }
299 1.1 rmind
300 1.1 rmind #if defined(DDB) || defined(_NPF_TESTING)
301 1.1 rmind
302 1.1 rmind void
303 1.1 rmind npf_state_dump(npf_state_t *nst)
304 1.1 rmind {
305 1.1 rmind npf_tcpstate_t *fst = &nst->nst_tcpst[0], *tst = &nst->nst_tcpst[1];
306 1.1 rmind
307 1.1 rmind printf("\tstate (%p) %d:\n\t\t"
308 1.1 rmind "F { seqend %u ackend %u mwin %u wscale %u }\n\t\t"
309 1.1 rmind "T { seqend %u, ackend %u mwin %u wscale %u }\n",
310 1.1 rmind nst, nst->nst_state,
311 1.1 rmind fst->nst_seqend, fst->nst_ackend, fst->nst_maxwin, fst->nst_wscale,
312 1.1 rmind tst->nst_seqend, tst->nst_ackend, tst->nst_maxwin, tst->nst_wscale
313 1.1 rmind );
314 1.1 rmind }
315 1.1 rmind
316 1.1 rmind #endif
317