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