Home | History | Annotate | Line # | Download | only in npf
npf_state.c revision 1.8
      1 /*	$NetBSD: npf_state.c,v 1.8 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 state engine to track sessions.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: npf_state.c,v 1.8 2012/06/22 13:43:17 rmind Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 
     42 #include <sys/mutex.h>
     43 
     44 #include "npf_impl.h"
     45 
     46 /*
     47  * Generic session states and timeout table.
     48  *
     49  * Note: used for connnection-less protocols.
     50  */
     51 
     52 #define	NPF_ANY_SESSION_CLOSED		0
     53 #define	NPF_ANY_SESSION_NEW		1
     54 #define	NPF_ANY_SESSION_ESTABLISHED	2
     55 #define	NPF_ANY_SESSION_NSTATES		3
     56 
     57 static const int npf_generic_fsm[NPF_ANY_SESSION_NSTATES][2] = {
     58 	[NPF_ANY_SESSION_CLOSED] = {
     59 		[NPF_FLOW_FORW]		= NPF_ANY_SESSION_NEW,
     60 	},
     61 	[NPF_ANY_SESSION_NEW] = {
     62 		[NPF_FLOW_FORW]		= NPF_ANY_SESSION_NEW,
     63 		[NPF_FLOW_BACK]		= NPF_ANY_SESSION_ESTABLISHED,
     64 	},
     65 	[NPF_ANY_SESSION_ESTABLISHED] = {
     66 		[NPF_FLOW_FORW]		= NPF_ANY_SESSION_ESTABLISHED,
     67 		[NPF_FLOW_BACK]		= NPF_ANY_SESSION_ESTABLISHED,
     68 	},
     69 };
     70 
     71 static u_int npf_generic_timeout[] __read_mostly = {
     72 	[NPF_ANY_SESSION_CLOSED]	= 0,
     73 	[NPF_ANY_SESSION_NEW]		= 30,
     74 	[NPF_ANY_SESSION_ESTABLISHED]	= 60,
     75 };
     76 
     77 /*
     78  * npf_state_init: initialise the state structure.
     79  *
     80  * Should normally be called on a first packet, which also determines the
     81  * direction in a case of connection-orientated protocol.  Returns true on
     82  * success and false otherwise (e.g. if protocol is not supported).
     83  */
     84 bool
     85 npf_state_init(const npf_cache_t *npc, nbuf_t *nbuf, npf_state_t *nst)
     86 {
     87 	const int proto = npf_cache_ipproto(npc);
     88 	bool ret;
     89 
     90 	KASSERT(npf_iscached(npc, NPC_IP46));
     91 	KASSERT(npf_iscached(npc, NPC_LAYER4));
     92 
     93 	memset(nst, 0, sizeof(npf_state_t));
     94 	mutex_init(&nst->nst_lock, MUTEX_DEFAULT, IPL_SOFTNET);
     95 
     96 	switch (proto) {
     97 	case IPPROTO_TCP:
     98 		/* Pass to TCP state tracking engine. */
     99 		ret = npf_state_tcp(npc, nbuf, nst, NPF_FLOW_FORW);
    100 		break;
    101 	case IPPROTO_UDP:
    102 	case IPPROTO_ICMP:
    103 		/* Generic. */
    104 		nst->nst_state = npf_generic_fsm[nst->nst_state][NPF_FLOW_FORW];
    105 		ret = true;
    106 		break;
    107 	default:
    108 		ret = false;
    109 	}
    110 	return ret;
    111 }
    112 
    113 void
    114 npf_state_destroy(npf_state_t *nst)
    115 {
    116 
    117 	nst->nst_state = 0;
    118 	mutex_destroy(&nst->nst_lock);
    119 }
    120 
    121 /*
    122  * npf_state_inspect: inspect the packet according to the protocol state.
    123  *
    124  * Return true if packet is considered to match the state (e.g. for TCP,
    125  * the packet belongs to the tracked connection) and false otherwise.
    126  */
    127 bool
    128 npf_state_inspect(const npf_cache_t *npc, nbuf_t *nbuf,
    129     npf_state_t *nst, const bool forw)
    130 {
    131 	const int proto = npf_cache_ipproto(npc);
    132 	const int di = forw ? NPF_FLOW_FORW : NPF_FLOW_BACK;
    133 	bool ret;
    134 
    135 	mutex_enter(&nst->nst_lock);
    136 	switch (proto) {
    137 	case IPPROTO_TCP:
    138 		/* Pass to TCP state tracking engine. */
    139 		ret = npf_state_tcp(npc, nbuf, nst, di);
    140 		break;
    141 	case IPPROTO_UDP:
    142 	case IPPROTO_ICMP:
    143 		/* Generic. */
    144 		nst->nst_state = npf_generic_fsm[nst->nst_state][di];
    145 		ret = true;
    146 		break;
    147 	default:
    148 		ret = false;
    149 	}
    150 	NPF_TCP_STATE_SAMPLE(nst, ret);
    151 	mutex_exit(&nst->nst_lock);
    152 
    153 	return ret;
    154 }
    155 
    156 /*
    157  * npf_state_etime: return session expiration time according to the state.
    158  */
    159 int
    160 npf_state_etime(const npf_state_t *nst, const int proto)
    161 {
    162 	const int state = nst->nst_state;
    163 	int timeout = 0;
    164 
    165 	switch (proto) {
    166 	case IPPROTO_TCP:
    167 		/* Pass to TCP state tracking engine. */
    168 		timeout = npf_state_tcp_timeout(nst);
    169 		break;
    170 	case IPPROTO_UDP:
    171 	case IPPROTO_ICMP:
    172 		/* Generic. */
    173 		timeout = npf_generic_timeout[state];
    174 		break;
    175 	default:
    176 		KASSERT(false);
    177 	}
    178 	return timeout;
    179 }
    180 
    181 void
    182 npf_state_dump(npf_state_t *nst)
    183 {
    184 #if defined(DDB) || defined(_NPF_TESTING)
    185 	npf_tcpstate_t *fst = &nst->nst_tcpst[0], *tst = &nst->nst_tcpst[1];
    186 
    187 	printf("\tstate (%p) %d:\n\t\t"
    188 	    "F { end %u maxend %u mwin %u wscale %u }\n\t\t"
    189 	    "T { end %u maxend %u mwin %u wscale %u }\n",
    190 	    nst, nst->nst_state,
    191 	    fst->nst_end, fst->nst_maxend, fst->nst_maxwin, fst->nst_wscale,
    192 	    tst->nst_end, tst->nst_maxend, tst->nst_maxwin, tst->nst_wscale
    193 	);
    194 #endif
    195 }
    196