/*-
* Copyright (c) 2010-2012 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This material is based upon work partially supported by The
* NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* npf_state_init: initialise the state structure.
*
* Should normally be called on a first packet, which also determines the
* direction in a case of connection-orientated protocol. Returns true on
* success and false otherwise (e.g. if protocol is not supported).
*/
bool
npf_state_init(npf_cache_t *npc, npf_state_t *nst)
{
const int proto = npc->npc_proto;
bool ret;
switch (proto) {
case IPPROTO_TCP:
/* Pass to TCP state tracking engine. */
ret = npf_state_tcp(npc, nst, NPF_FLOW_FORW);
break;
case IPPROTO_UDP:
case IPPROTO_ICMP:
case IPPROTO_GRE:
/* Generic. */
nst->nst_state = npf_generic_fsm[nst->nst_state][NPF_FLOW_FORW];
ret = true;
break;
default:
ret = false;
}
NPF_STATE_SAMPLE(nst, ret);
return ret;
}
/*
* npf_state_inspect: inspect the packet according to the protocol state.
*
* Return true if packet is considered to match the state (e.g. for TCP,
* the packet belongs to the tracked connection) and false otherwise.
*/
bool
npf_state_inspect(npf_cache_t *npc, npf_state_t *nst, const npf_flow_t flow)
{
const int proto = npc->npc_proto;
bool ret;
switch (proto) {
case IPPROTO_TCP:
/* Pass to TCP state tracking engine. */
ret = npf_state_tcp(npc, nst, flow);
break;
case IPPROTO_UDP:
case IPPROTO_ICMP:
case IPPROTO_GRE:
/* Generic. */
nst->nst_state = npf_generic_fsm[nst->nst_state][flow];
ret = true;
break;
default:
ret = false;
}
NPF_STATE_SAMPLE(nst, ret);
return ret;
}
/*
* npf_state_etime: return the expiration time depending on the state.
*/
int
npf_state_etime(npf_t *npf, const npf_state_t *nst, const int proto)
{
const npf_state_params_t *params;
const unsigned state = nst->nst_state;
int timeout = 0;
switch (proto) {
case IPPROTO_TCP:
/* Pass to TCP state tracking engine. */
timeout = npf_state_tcp_timeout(npf, nst);
break;
case IPPROTO_UDP:
case IPPROTO_ICMP:
/* Generic. */
params = npf->params[NPF_PARAMS_GENERIC_STATE];
timeout = params->timeouts[state];
break;
case IPPROTO_GRE:
params = npf->params[NPF_PARAMS_GENERIC_STATE];
timeout = params->gre_timeout;
break;
default:
KASSERT(false);
}
return timeout;
}