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