Home | History | Annotate | Line # | Download | only in npf
npf_state.c revision 1.2.2.1
      1  1.2.2.1  jruoho /*	$NetBSD: npf_state.c,v 1.2.2.1 2011/06/06 09:09:53 jruoho 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.2.2.1  jruoho __KERNEL_RCSID(0, "$NetBSD: npf_state.c,v 1.2.2.1 2011/06/06 09:09:53 jruoho 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.2.2.1  jruoho #include <netinet/tcp_fsm.h>
     47      1.1   rmind 
     48      1.1   rmind #include "npf_impl.h"
     49      1.1   rmind 
     50  1.2.2.1  jruoho /* TCP session expiration table. */
     51  1.2.2.1  jruoho static const u_int tcp_expire_table[ ] __read_mostly = {
     52  1.2.2.1  jruoho 	/* Initial synchronisation.  Timeout: 30 sec and 1 minute. */
     53  1.2.2.1  jruoho 	[TCPS_SYN_SENT]		= 30,
     54  1.2.2.1  jruoho 	[TCPS_SYN_RECEIVED]	= 60,
     55  1.2.2.1  jruoho 	/* Established (synchronised).  Timeout: 24 hours. */
     56  1.2.2.1  jruoho 	[TCPS_ESTABLISHED]	= 60 * 60 * 24,
     57  1.2.2.1  jruoho 	[TCPS_FIN_WAIT_1]	= 60 * 60 * 24,
     58  1.2.2.1  jruoho 	[TCPS_FIN_WAIT_2]	= 60 * 60 * 24,
     59  1.2.2.1  jruoho 	/* UNUSED [TCPS_CLOSE_WAIT]	= 60 * 60 * 24, */
     60  1.2.2.1  jruoho 	/* Closure.  Timeout: 4 minutes (2 * MSL). */
     61  1.2.2.1  jruoho 	[TCPS_CLOSING]		= 60 * 4,
     62  1.2.2.1  jruoho 	[TCPS_LAST_ACK]		= 60 * 4,
     63  1.2.2.1  jruoho 	[TCPS_TIME_WAIT]	= 60 * 4,
     64  1.2.2.1  jruoho 	/* Fully closed.  Timeout immediately. */
     65  1.2.2.1  jruoho 	[TCPS_CLOSED]		= 0
     66  1.2.2.1  jruoho };
     67      1.1   rmind 
     68  1.2.2.1  jruoho /* Session expiration table. */
     69  1.2.2.1  jruoho static const u_int expire_table[ ] __read_mostly = {
     70  1.2.2.1  jruoho 	[IPPROTO_UDP]		= 60,		/* 1 min */
     71  1.2.2.1  jruoho 	[IPPROTO_ICMP]		= 30		/* 30 sec */
     72      1.1   rmind };
     73      1.1   rmind 
     74  1.2.2.1  jruoho #define	MAXACKWINDOW		66000
     75  1.2.2.1  jruoho 
     76      1.1   rmind static bool
     77      1.1   rmind npf_tcp_inwindow(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst,
     78      1.1   rmind     const bool forw)
     79      1.1   rmind {
     80  1.2.2.1  jruoho 	const struct tcphdr * const th = &npc->npc_l4.tcp;
     81      1.1   rmind 	const int tcpfl = th->th_flags;
     82      1.1   rmind 	npf_tcpstate_t *fstate, *tstate;
     83      1.1   rmind 	int tcpdlen, wscale, ackskew;
     84      1.1   rmind 	tcp_seq seq, ack, end;
     85      1.1   rmind 	uint32_t win;
     86      1.1   rmind 
     87      1.1   rmind 	KASSERT(npf_iscached(npc, NPC_TCP));
     88      1.1   rmind 	tcpdlen = npf_tcpsaw(__UNCONST(npc), &seq, &ack, &win);
     89      1.1   rmind 	end = seq + tcpdlen;
     90      1.1   rmind 	if (tcpfl & TH_SYN) {
     91      1.1   rmind 		end++;
     92      1.1   rmind 	}
     93      1.1   rmind 	if (tcpfl & TH_FIN) {
     94      1.1   rmind 		end++;
     95      1.1   rmind 	}
     96      1.1   rmind 
     97      1.1   rmind 	/*
     98      1.1   rmind 	 * Perform SEQ/ACK numbers check against boundaries.  Reference:
     99      1.1   rmind 	 *
    100      1.1   rmind 	 *	Rooij G., "Real stateful TCP packet filtering in IP Filter",
    101      1.1   rmind 	 *	10th USENIX Security Symposium invited talk, Aug. 2001.
    102      1.1   rmind 	 */
    103      1.1   rmind 
    104      1.1   rmind 	fstate = &nst->nst_tcpst[forw ? 0 : 1];
    105      1.1   rmind 	tstate = &nst->nst_tcpst[forw ? 1 : 0];
    106      1.1   rmind 	win = win ? (win << fstate->nst_wscale) : 1;
    107      1.1   rmind 
    108      1.1   rmind 	if (tcpfl == TH_SYN) {
    109      1.1   rmind 		/*
    110      1.1   rmind 		 * First SYN or re-transmission of SYN.  Initialize all
    111      1.1   rmind 		 * values.  State of other side will get set with a SYN-ACK
    112      1.1   rmind 		 * reply (see below).
    113      1.1   rmind 		 */
    114      1.1   rmind 		fstate->nst_seqend = end;
    115      1.1   rmind 		fstate->nst_ackend = end;
    116      1.1   rmind 		fstate->nst_maxwin = win;
    117      1.1   rmind 		tstate->nst_ackend = 0;
    118      1.1   rmind 		tstate->nst_ackend = 0;
    119      1.1   rmind 		tstate->nst_maxwin = 0;
    120      1.1   rmind 		/*
    121      1.1   rmind 		 * Handle TCP Window Scaling (RFC 1323).  Both sides may
    122      1.1   rmind 		 * send this option in their SYN packets.
    123      1.1   rmind 		 */
    124      1.1   rmind 		if (npf_fetch_tcpopts(npc, nbuf, NULL, &wscale)) {
    125      1.1   rmind 			fstate->nst_wscale = wscale;
    126      1.1   rmind 		} else {
    127      1.1   rmind 			fstate->nst_wscale = 0;
    128      1.1   rmind 		}
    129      1.1   rmind 		tstate->nst_wscale = 0;
    130      1.1   rmind 		/* Done. */
    131      1.1   rmind 		return true;
    132      1.1   rmind 	}
    133      1.1   rmind 	if (fstate->nst_seqend == 0) {
    134      1.1   rmind 		/*
    135      1.1   rmind 		 * Should be a SYN-ACK reply to SYN.  If SYN is not set,
    136      1.1   rmind 		 * then we are in the middle connection and lost tracking.
    137      1.1   rmind 		 */
    138      1.1   rmind 		fstate->nst_seqend = end;
    139      1.1   rmind 		fstate->nst_ackend = end + 1;
    140      1.1   rmind 		fstate->nst_maxwin = 1;
    141      1.1   rmind 
    142      1.1   rmind 		/* Handle TCP Window Scaling (must be ignored if no SYN). */
    143      1.1   rmind 		if (tcpfl & TH_SYN) {
    144      1.1   rmind 			fstate->nst_wscale =
    145      1.1   rmind 			    npf_fetch_tcpopts(npc, nbuf, NULL, &wscale) ?
    146      1.1   rmind 			    wscale : 0;
    147      1.1   rmind 		}
    148      1.1   rmind 	}
    149      1.1   rmind 	if ((tcpfl & TH_ACK) == 0) {
    150      1.1   rmind 		/* Pretend that an ACK was sent. */
    151      1.1   rmind 		ack = tstate->nst_seqend;
    152      1.1   rmind 	} else if ((tcpfl & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST) && ack == 0) {
    153      1.1   rmind 		/* Workaround for some TCP stacks. */
    154      1.1   rmind 		ack = tstate->nst_seqend;
    155      1.1   rmind 	}
    156      1.1   rmind 	if (seq == end) {
    157      1.1   rmind 		/* If packet contains no data - assume it is valid. */
    158      1.1   rmind 		end = fstate->nst_seqend;
    159      1.1   rmind 		seq = end;
    160      1.1   rmind 	}
    161      1.1   rmind 
    162      1.1   rmind 	/*
    163      1.1   rmind 	 * Determine whether the data is within previously noted window,
    164      1.1   rmind 	 * that is, upper boundary for valid data (I).
    165      1.1   rmind 	 */
    166      1.1   rmind 	if (!SEQ_GEQ(fstate->nst_ackend, end)) {
    167      1.2   rmind 		npf_stats_inc(NPF_STAT_INVALID_STATE_TCP1);
    168      1.1   rmind 		return false;
    169      1.1   rmind 	}
    170      1.1   rmind 	/* Lower boundary (II), which is no more than one window back. */
    171      1.1   rmind 	if (!SEQ_GEQ(seq, fstate->nst_seqend - tstate->nst_maxwin)) {
    172      1.2   rmind 		npf_stats_inc(NPF_STAT_INVALID_STATE_TCP2);
    173      1.1   rmind 		return false;
    174      1.1   rmind 	}
    175      1.1   rmind 	/*
    176      1.1   rmind 	 * Boundaries for valid acknowledgments (III, IV) - on predicted
    177      1.1   rmind 	 * window up or down, since packets may be fragmented.
    178      1.1   rmind 	 */
    179      1.1   rmind 	ackskew = tstate->nst_seqend - ack;
    180      1.1   rmind 	if (ackskew < -MAXACKWINDOW || ackskew > MAXACKWINDOW) {
    181      1.2   rmind 		npf_stats_inc(NPF_STAT_INVALID_STATE_TCP3);
    182      1.1   rmind 		return false;
    183      1.1   rmind 	}
    184      1.1   rmind 
    185      1.1   rmind 	/*
    186      1.2   rmind 	 * Packet is passed now.
    187      1.2   rmind 	 *
    188      1.1   rmind 	 * Negative ackskew might be due to fragmented packets.  Since the
    189      1.1   rmind 	 * total length of the packet is unknown - bump the boundary.
    190      1.1   rmind 	 */
    191      1.1   rmind 	if (ackskew < 0) {
    192      1.1   rmind 		tstate->nst_seqend = end;
    193      1.1   rmind 	}
    194      1.1   rmind 	/* Keep track of the maximum window seen. */
    195      1.1   rmind 	if (fstate->nst_maxwin < win) {
    196      1.1   rmind 		fstate->nst_maxwin = win;
    197      1.1   rmind 	}
    198      1.1   rmind 	if (SEQ_GT(end, fstate->nst_seqend)) {
    199      1.1   rmind 		fstate->nst_seqend = end;
    200      1.1   rmind 	}
    201      1.1   rmind 	/* Note the window for upper boundary. */
    202      1.1   rmind 	if (SEQ_GEQ(ack + win, tstate->nst_ackend)) {
    203      1.1   rmind 		tstate->nst_ackend = ack + win;
    204      1.1   rmind 	}
    205      1.1   rmind 	return true;
    206      1.1   rmind }
    207      1.1   rmind 
    208      1.1   rmind static inline bool
    209      1.1   rmind npf_state_tcp(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst,
    210      1.1   rmind     const bool forw)
    211      1.1   rmind {
    212  1.2.2.1  jruoho 	const struct tcphdr * const th = &npc->npc_l4.tcp;
    213  1.2.2.1  jruoho 	const int tcpfl = th->th_flags, state = nst->nst_state;
    214  1.2.2.1  jruoho #if 0
    215  1.2.2.1  jruoho 	/* Determine whether TCP packet really belongs to this connection. */
    216  1.2.2.1  jruoho 	if (!npf_tcp_inwindow(npc, nbuf, nst, forw)) {
    217  1.2.2.1  jruoho 		return false;
    218  1.2.2.1  jruoho 	}
    219  1.2.2.1  jruoho #endif
    220      1.1   rmind 	/*
    221  1.2.2.1  jruoho 	 * Handle 3-way handshake (SYN -> SYN,ACK -> ACK), connection
    222  1.2.2.1  jruoho 	 * reset (RST), half-open connections, connection closure, etc.
    223      1.1   rmind 	 */
    224  1.2.2.1  jruoho 	if (__predict_false(tcpfl & TH_RST)) {
    225  1.2.2.1  jruoho 		nst->nst_state = TCPS_CLOSED;
    226  1.2.2.1  jruoho 		return true;
    227  1.2.2.1  jruoho 	}
    228  1.2.2.1  jruoho 	switch (state) {
    229  1.2.2.1  jruoho 	case TCPS_ESTABLISHED:
    230  1.2.2.1  jruoho 	case TCPS_FIN_WAIT_2:
    231  1.2.2.1  jruoho 		/* Common case - connection is established. */
    232  1.2.2.1  jruoho 		if ((tcpfl & (TH_SYN | TH_ACK | TH_FIN)) == TH_ACK) {
    233  1.2.2.1  jruoho 			return true;
    234      1.1   rmind 		}
    235  1.2.2.1  jruoho 		/* Otherwise, can only be a FIN. */
    236  1.2.2.1  jruoho 		if ((tcpfl & TH_FIN) == 0) {
    237  1.2.2.1  jruoho 			break;
    238  1.2.2.1  jruoho 		}
    239  1.2.2.1  jruoho 		/* XXX see below TCPS_CLOSE_WAIT */
    240  1.2.2.1  jruoho 		if (state != TCPS_FIN_WAIT_2) {
    241  1.2.2.1  jruoho 			/* First FIN: closure of one end. */
    242  1.2.2.1  jruoho 			nst->nst_state = TCPS_FIN_WAIT_1;
    243  1.2.2.1  jruoho 		} else {
    244  1.2.2.1  jruoho 			/* Second FIN: connection closure, wait for ACK. */
    245  1.2.2.1  jruoho 			nst->nst_state = TCPS_LAST_ACK;
    246  1.2.2.1  jruoho 		}
    247  1.2.2.1  jruoho 		return true;
    248  1.2.2.1  jruoho 	case TCPS_SYN_SENT:
    249  1.2.2.1  jruoho 		/* After SYN expecting SYN-ACK. */
    250      1.1   rmind 		if (tcpfl == (TH_SYN | TH_ACK) && !forw) {
    251      1.1   rmind 			/* Received backwards SYN-ACK. */
    252  1.2.2.1  jruoho 			nst->nst_state = TCPS_SYN_RECEIVED;
    253  1.2.2.1  jruoho 			return true;
    254  1.2.2.1  jruoho 		}
    255  1.2.2.1  jruoho 		if (tcpfl == TH_SYN && forw) {
    256      1.1   rmind 			/* Re-transmission of SYN. */
    257  1.2.2.1  jruoho 			return true;
    258      1.1   rmind 		}
    259      1.1   rmind 		break;
    260  1.2.2.1  jruoho 	case TCPS_SYN_RECEIVED:
    261      1.1   rmind 		/* SYN-ACK was seen, expecting ACK. */
    262  1.2.2.1  jruoho 		if ((tcpfl & (TH_SYN | TH_ACK | TH_FIN)) == TH_ACK) {
    263  1.2.2.1  jruoho 			/* ACK - establish connection. */
    264  1.2.2.1  jruoho 			nst->nst_state = TCPS_ESTABLISHED;
    265  1.2.2.1  jruoho 			return true;
    266  1.2.2.1  jruoho 		}
    267  1.2.2.1  jruoho 		if (tcpfl == (TH_SYN | TH_ACK)) {
    268  1.2.2.1  jruoho 			/* Re-transmission of SYN-ACK. */
    269  1.2.2.1  jruoho 			return true;
    270      1.1   rmind 		}
    271      1.1   rmind 		break;
    272  1.2.2.1  jruoho 	case TCPS_CLOSE_WAIT:
    273  1.2.2.1  jruoho 		/* UNUSED */
    274  1.2.2.1  jruoho 	case TCPS_FIN_WAIT_1:
    275  1.2.2.1  jruoho 		/*
    276  1.2.2.1  jruoho 		 * XXX: FIN re-transmission is not handled, use TCPS_CLOSE_WAIT.
    277  1.2.2.1  jruoho 		 */
    278  1.2.2.1  jruoho 		/*
    279  1.2.2.1  jruoho 		 * First FIN was seen, expecting ACK.  However, we may receive
    280  1.2.2.1  jruoho 		 * a simultaneous FIN or exchange of FINs with FIN-ACK.
    281  1.2.2.1  jruoho 		 */
    282  1.2.2.1  jruoho 		if ((tcpfl & (TH_ACK | TH_FIN)) == (TH_ACK | TH_FIN)) {
    283  1.2.2.1  jruoho 			/* Exchange of FINs with ACK.  Wait for last ACK. */
    284  1.2.2.1  jruoho 			nst->nst_state = TCPS_LAST_ACK;
    285  1.2.2.1  jruoho 			return true;
    286  1.2.2.1  jruoho 		} else if (tcpfl & TH_ACK) {
    287  1.2.2.1  jruoho 			/* ACK of first FIN. */
    288  1.2.2.1  jruoho 			nst->nst_state = TCPS_FIN_WAIT_2;
    289  1.2.2.1  jruoho 			return true;
    290  1.2.2.1  jruoho 		} else if (tcpfl & TH_FIN) {
    291  1.2.2.1  jruoho 			/* Simultaneous FIN.  Need to wait for ACKs. */
    292  1.2.2.1  jruoho 			nst->nst_state = TCPS_CLOSING;
    293  1.2.2.1  jruoho 			return true;
    294  1.2.2.1  jruoho 		}
    295  1.2.2.1  jruoho 		break;
    296  1.2.2.1  jruoho 	case TCPS_CLOSING:
    297  1.2.2.1  jruoho 	case TCPS_LAST_ACK:
    298  1.2.2.1  jruoho 	case TCPS_TIME_WAIT:
    299  1.2.2.1  jruoho 		/* Expecting only ACK. */
    300  1.2.2.1  jruoho 		if ((tcpfl & (TH_SYN | TH_ACK | TH_FIN)) != TH_ACK) {
    301  1.2.2.1  jruoho 			return false;
    302  1.2.2.1  jruoho 		}
    303  1.2.2.1  jruoho 		switch (state) {
    304  1.2.2.1  jruoho 		case TCPS_CLOSING:
    305  1.2.2.1  jruoho 			/* One ACK noted, wait for last one. */
    306  1.2.2.1  jruoho 			nst->nst_state = TCPS_LAST_ACK;
    307  1.2.2.1  jruoho 			break;
    308  1.2.2.1  jruoho 		case TCPS_LAST_ACK:
    309  1.2.2.1  jruoho 			/* Last ACK received, quiet wait now. */
    310  1.2.2.1  jruoho 			nst->nst_state = TCPS_TIME_WAIT;
    311  1.2.2.1  jruoho 			break;
    312  1.2.2.1  jruoho 		}
    313  1.2.2.1  jruoho 		return true;
    314  1.2.2.1  jruoho 	case TCPS_CLOSED:
    315  1.2.2.1  jruoho 		/* XXX: Drop or pass? */
    316      1.1   rmind 		break;
    317      1.1   rmind 	default:
    318      1.1   rmind 		npf_state_dump(nst);
    319      1.1   rmind 		KASSERT(false);
    320      1.1   rmind 	}
    321  1.2.2.1  jruoho 	return false;
    322      1.1   rmind }
    323      1.1   rmind 
    324      1.1   rmind bool
    325      1.1   rmind npf_state_init(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst)
    326      1.1   rmind {
    327      1.1   rmind 	const int proto = npf_cache_ipproto(npc);
    328      1.1   rmind 
    329      1.1   rmind 	KASSERT(npf_iscached(npc, NPC_IP46 | NPC_LAYER4));
    330      1.2   rmind 
    331      1.2   rmind 	mutex_init(&nst->nst_lock, MUTEX_DEFAULT, IPL_SOFTNET);
    332      1.2   rmind 
    333      1.1   rmind 	if (proto == IPPROTO_TCP) {
    334      1.1   rmind 		const struct tcphdr *th = &npc->npc_l4.tcp;
    335  1.2.2.1  jruoho 
    336      1.1   rmind 		/* TCP case: must be SYN. */
    337      1.1   rmind 		KASSERT(npf_iscached(npc, NPC_TCP));
    338      1.1   rmind 		if (th->th_flags != TH_SYN) {
    339      1.2   rmind 			npf_stats_inc(NPF_STAT_INVALID_STATE);
    340      1.1   rmind 			return false;
    341      1.1   rmind 		}
    342      1.1   rmind 		/* Initial values for TCP window and sequence tracking. */
    343      1.1   rmind 		if (!npf_tcp_inwindow(npc, nbuf, nst, true)) {
    344      1.2   rmind 			npf_stats_inc(NPF_STAT_INVALID_STATE);
    345      1.1   rmind 			return false;
    346      1.1   rmind 		}
    347      1.1   rmind 	}
    348  1.2.2.1  jruoho 
    349  1.2.2.1  jruoho 	/*
    350  1.2.2.1  jruoho 	 * Initial state: SYN sent, waiting for response from the other side.
    351  1.2.2.1  jruoho 	 * Note: for UDP or ICMP, reuse SYN-sent flag to note response.
    352  1.2.2.1  jruoho 	 */
    353  1.2.2.1  jruoho 	nst->nst_state = TCPS_SYN_SENT;
    354      1.1   rmind 	return true;
    355      1.1   rmind }
    356      1.1   rmind 
    357      1.1   rmind void
    358      1.1   rmind npf_state_destroy(npf_state_t *nst)
    359      1.1   rmind {
    360      1.1   rmind 
    361      1.1   rmind 	mutex_destroy(&nst->nst_lock);
    362      1.1   rmind }
    363      1.1   rmind 
    364      1.1   rmind bool
    365      1.1   rmind npf_state_inspect(const npf_cache_t *npc, nbuf_t *nbuf,
    366      1.1   rmind     npf_state_t *nst, const bool forw)
    367      1.1   rmind {
    368      1.1   rmind 	const int proto = npf_cache_ipproto(npc);
    369      1.1   rmind 	bool ret;
    370      1.1   rmind 
    371      1.1   rmind 	mutex_enter(&nst->nst_lock);
    372      1.1   rmind 	switch (proto) {
    373      1.1   rmind 	case IPPROTO_TCP:
    374      1.1   rmind 		/* Handle TCP. */
    375      1.1   rmind 		ret = npf_state_tcp(npc, nbuf, nst, forw);
    376      1.1   rmind 		break;
    377      1.1   rmind 	default:
    378  1.2.2.1  jruoho 		/*
    379  1.2.2.1  jruoho 		 * Handle UDP or ICMP response for opening session.
    380  1.2.2.1  jruoho 		 */
    381  1.2.2.1  jruoho 		if (nst->nst_state == TCPS_SYN_SENT && !forw) {
    382  1.2.2.1  jruoho 			nst->nst_state= TCPS_ESTABLISHED;
    383      1.1   rmind 		}
    384      1.1   rmind 		ret = true;
    385      1.1   rmind 	}
    386      1.1   rmind 	mutex_exit(&nst->nst_lock);
    387      1.2   rmind 	if (__predict_false(!ret)) {
    388      1.2   rmind 		npf_stats_inc(NPF_STAT_INVALID_STATE);
    389      1.2   rmind 	}
    390      1.1   rmind 	return ret;
    391      1.1   rmind }
    392      1.1   rmind 
    393  1.2.2.1  jruoho /*
    394  1.2.2.1  jruoho  * npf_state_etime: return session expiration time according to the state.
    395  1.2.2.1  jruoho  */
    396      1.1   rmind int
    397      1.1   rmind npf_state_etime(const npf_state_t *nst, const int proto)
    398      1.1   rmind {
    399  1.2.2.1  jruoho 	const int state = nst->nst_state;
    400      1.1   rmind 
    401  1.2.2.1  jruoho 	if (__predict_true(proto == IPPROTO_TCP)) {
    402  1.2.2.1  jruoho 		return tcp_expire_table[state];
    403      1.1   rmind 	}
    404  1.2.2.1  jruoho 	return expire_table[proto];
    405      1.1   rmind }
    406      1.1   rmind 
    407      1.1   rmind void
    408      1.1   rmind npf_state_dump(npf_state_t *nst)
    409      1.1   rmind {
    410  1.2.2.1  jruoho #if defined(DDB) || defined(_NPF_TESTING)
    411      1.1   rmind 	npf_tcpstate_t *fst = &nst->nst_tcpst[0], *tst = &nst->nst_tcpst[1];
    412      1.1   rmind 
    413      1.1   rmind 	printf("\tstate (%p) %d:\n\t\t"
    414      1.1   rmind 	    "F { seqend %u ackend %u mwin %u wscale %u }\n\t\t"
    415  1.2.2.1  jruoho 	    "T { seqend %u ackend %u mwin %u wscale %u }\n",
    416      1.1   rmind 	    nst, nst->nst_state,
    417      1.1   rmind 	    fst->nst_seqend, fst->nst_ackend, fst->nst_maxwin, fst->nst_wscale,
    418      1.1   rmind 	    tst->nst_seqend, tst->nst_ackend, tst->nst_maxwin, tst->nst_wscale
    419      1.1   rmind 	);
    420      1.1   rmind #endif
    421  1.2.2.1  jruoho }
    422