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