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