/*-
* Copyright (c) 2010-2019 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 interface for the Application Level Gateways (ALGs).
*/
/*
* NAT ALG description structure. For more compact use of cache,
* the functions are separated in their own arrays. The number of
* ALGs is expected to be very small.
*/
/* Find a spare slot. */
for (i = 0; i < NPF_MAX_ALGS; i++) {
alg = &aset->alg_list[i];
if (alg->na_name == NULL) {
break;
}
}
if (i == NPF_MAX_ALGS) {
npf_config_exit(npf);
return NULL;
}
/*
* Finally, unregister the ALG. We leave the 'destroy' callback
* as the following will invoke it for the relevant connections.
*/
npf_ruleset_freealg(npf_config_natset(npf), alg);
atomic_store_relaxed(&afuncs->destroy, NULL);
alg->na_name = NULL;
npf_config_exit(npf);
return 0;
}
/*
* npf_alg_match: call the ALG matching inspectors.
*
* The purpose of the "matching" inspector function in the ALG API
* is to determine whether this connection matches the ALG criteria
* i.e. is concerning the ALG. If yes, ALG can associate itself with
* the given NAT state structure and set/save an arbitrary parameter.
* This is done using the using the npf_nat_setalg() function.
*
* => This is called when the packet matches the dynamic NAT policy
* and the NAT state entry is being created for it [NAT-ESTABLISH].
*/
bool
npf_alg_match(npf_cache_t *npc, npf_nat_t *nt, int di)
{
npf_t *npf = npc->npc_ctx;
npf_algset_t *aset = npf->algset;
bool match = false;
unsigned count;
int s;
/*
* npf_alg_exec: execute the ALG translation processors.
*
* The ALG function would perform any additional packet translation
* or manipulation here.
*
* => This is called when the packet is being translated according
* to the dynamic NAT logic [NAT-TRANSLATE].
*/
void
npf_alg_exec(npf_cache_t *npc, npf_nat_t *nt, const npf_flow_t flow)
{
npf_t *npf = npc->npc_ctx;
npf_algset_t *aset = npf->algset;
unsigned count;
int s;
s = npf_config_read_enter(npf);
count = atomic_load_relaxed(&aset->alg_count);
for (unsigned i = 0; i < count; i++) {
const npfa_funcs_t *f = &aset->alg_funcs[i];
bool (*translate_func)(npf_cache_t *, npf_nat_t *, npf_flow_t);
/*
* npf_alg_conn: query ALGs which may perform a custom state lookup.
*
* The purpose of ALG connection inspection function is to provide
* ALGs with a mechanism to override the regular connection state
* lookup, if they need to. For example, some ALGs may want to
* extract and use a different n-tuple to perform a lookup.
*
* => This is called at the beginning of the connection state lookup
* function [CONN-LOOKUP].
*
* => Must use the npf_conn_lookup() function to perform the custom
* connection state lookup and return the result.
*
* => Returning NULL will result in NPF performing a regular state
* lookup for the packet.
*/
npf_conn_t *
npf_alg_conn(npf_cache_t *npc, int di)
{
npf_t *npf = npc->npc_ctx;
npf_algset_t *aset = npf->algset;
npf_conn_t *con = NULL;
unsigned count;
int s;
s = npf_config_read_enter(npf);
count = atomic_load_relaxed(&aset->alg_count);
for (unsigned i = 0; i < count; i++) {
const npfa_funcs_t *f = &aset->alg_funcs[i];
npf_conn_t *(*inspect_func)(npf_cache_t *, int);