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