Home | History | Annotate | Line # | Download | only in npf
npf_alg.c revision 1.15
      1  1.15     rmind /*	$NetBSD: npf_alg.c,v 1.15 2014/08/11 23:48:01 rmind Exp $	*/
      2   1.1     rmind 
      3   1.1     rmind /*-
      4   1.9     rmind  * Copyright (c) 2010-2013 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.9     rmind  * NPF interface for the Application Level Gateways (ALGs).
     34   1.1     rmind  */
     35   1.1     rmind 
     36   1.1     rmind #include <sys/cdefs.h>
     37  1.15     rmind __KERNEL_RCSID(0, "$NetBSD: npf_alg.c,v 1.15 2014/08/11 23:48:01 rmind Exp $");
     38   1.1     rmind 
     39   1.1     rmind #include <sys/param.h>
     40   1.4     rmind #include <sys/types.h>
     41   1.4     rmind 
     42   1.1     rmind #include <sys/kmem.h>
     43   1.4     rmind #include <sys/pserialize.h>
     44   1.4     rmind #include <sys/mutex.h>
     45   1.1     rmind #include <net/pfil.h>
     46   1.8  christos #include <sys/module.h>
     47   1.1     rmind 
     48   1.1     rmind #include "npf_impl.h"
     49   1.1     rmind 
     50   1.9     rmind /*
     51   1.9     rmind  * NAT ALG description structure.  For more compact use of cache,
     52   1.9     rmind  * the functions are separated in their own arrays.  The number of
     53   1.9     rmind  * ALGs is expected to be very small.
     54   1.9     rmind  */
     55   1.9     rmind 
     56   1.1     rmind struct npf_alg {
     57   1.9     rmind 	const char *	na_name;
     58   1.9     rmind 	u_int		na_slot;
     59   1.1     rmind };
     60   1.1     rmind 
     61   1.9     rmind /* List of ALGs and the count. */
     62   1.9     rmind static pserialize_t	alg_psz			__cacheline_aligned;
     63   1.9     rmind static npf_alg_t	alg_list[NPF_MAX_ALGS]	__read_mostly;
     64   1.9     rmind static u_int		alg_count		__read_mostly;
     65   1.9     rmind 
     66  1.11     rmind /* Matching, inspection and translation functions. */
     67  1.11     rmind static npfa_funcs_t	alg_funcs[NPF_MAX_ALGS]	__read_mostly;
     68   1.9     rmind 
     69   1.9     rmind static const char	alg_prefix[] = "npf_alg_";
     70   1.9     rmind #define	NPF_EXT_PREFLEN	(sizeof(alg_prefix) - 1)
     71   1.1     rmind 
     72   1.1     rmind void
     73   1.1     rmind npf_alg_sysinit(void)
     74   1.1     rmind {
     75   1.9     rmind 	alg_psz = pserialize_create();
     76  1.11     rmind 	memset(alg_list, 0, sizeof(alg_list));
     77  1.11     rmind 	memset(alg_funcs, 0, sizeof(alg_funcs));
     78   1.9     rmind 	alg_count = 0;
     79   1.1     rmind }
     80   1.1     rmind 
     81   1.1     rmind void
     82   1.1     rmind npf_alg_sysfini(void)
     83   1.1     rmind {
     84   1.9     rmind 	pserialize_destroy(alg_psz);
     85   1.1     rmind }
     86   1.1     rmind 
     87   1.8  christos static npf_alg_t *
     88   1.9     rmind npf_alg_lookup(const char *name)
     89   1.8  christos {
     90   1.9     rmind 	KASSERT(npf_config_locked_p());
     91   1.8  christos 
     92   1.9     rmind 	for (u_int i = 0; i < alg_count; i++) {
     93   1.9     rmind 		npf_alg_t *alg = &alg_list[i];
     94   1.9     rmind 		const char *aname = alg->na_name;
     95   1.8  christos 
     96   1.9     rmind 		if (aname && strcmp(aname, name) == 0)
     97   1.9     rmind 			return alg;
     98   1.9     rmind 	}
     99   1.9     rmind 	return NULL;
    100   1.8  christos }
    101   1.8  christos 
    102   1.8  christos npf_alg_t *
    103   1.8  christos npf_alg_construct(const char *name)
    104   1.8  christos {
    105   1.8  christos 	npf_alg_t *alg;
    106   1.8  christos 
    107   1.9     rmind 	npf_config_enter();
    108   1.9     rmind 	if ((alg = npf_alg_lookup(name)) == NULL) {
    109   1.9     rmind 		char modname[NPF_EXT_PREFLEN + 64];
    110   1.9     rmind 		snprintf(modname, sizeof(modname), "%s%s", alg_prefix, name);
    111   1.9     rmind 		npf_config_exit();
    112   1.9     rmind 
    113   1.9     rmind 		if (module_autoload(modname, MODULE_CLASS_MISC) != 0) {
    114   1.9     rmind 			return NULL;
    115   1.9     rmind 		}
    116   1.9     rmind 		npf_config_enter();
    117   1.9     rmind 		alg = npf_alg_lookup(name);
    118   1.9     rmind 	}
    119   1.9     rmind 	npf_config_exit();
    120   1.8  christos 	return alg;
    121   1.8  christos }
    122   1.8  christos 
    123   1.1     rmind /*
    124   1.1     rmind  * npf_alg_register: register application-level gateway.
    125   1.1     rmind  */
    126   1.1     rmind npf_alg_t *
    127  1.11     rmind npf_alg_register(const char *name, const npfa_funcs_t *funcs)
    128   1.1     rmind {
    129   1.1     rmind 	npf_alg_t *alg;
    130   1.9     rmind 	u_int i;
    131   1.1     rmind 
    132   1.9     rmind 	npf_config_enter();
    133   1.9     rmind 	if (npf_alg_lookup(name) != NULL) {
    134   1.9     rmind 		npf_config_exit();
    135   1.9     rmind 		return NULL;
    136   1.9     rmind 	}
    137   1.9     rmind 
    138   1.9     rmind 	/* Find a spare slot. */
    139   1.9     rmind 	for (i = 0; i < NPF_MAX_ALGS; i++) {
    140   1.9     rmind 		alg = &alg_list[i];
    141   1.9     rmind 		if (alg->na_name == NULL) {
    142   1.9     rmind 			break;
    143   1.9     rmind 		}
    144   1.9     rmind 	}
    145   1.9     rmind 	if (i == NPF_MAX_ALGS) {
    146   1.9     rmind 		npf_config_exit();
    147   1.8  christos 		return NULL;
    148   1.8  christos 	}
    149   1.4     rmind 
    150   1.9     rmind 	/* Register the ALG. */
    151   1.9     rmind 	alg->na_name = name;
    152   1.9     rmind 	alg->na_slot = i;
    153   1.9     rmind 
    154   1.9     rmind 	/* Assign the functions. */
    155  1.11     rmind 	alg_funcs[i].match = funcs->match;
    156  1.11     rmind 	alg_funcs[i].translate = funcs->translate;
    157  1.11     rmind 	alg_funcs[i].inspect = funcs->inspect;
    158   1.9     rmind 
    159   1.9     rmind 	alg_count = MAX(alg_count, i + 1);
    160   1.9     rmind 	npf_config_exit();
    161  1.11     rmind 
    162   1.1     rmind 	return alg;
    163   1.1     rmind }
    164   1.1     rmind 
    165   1.1     rmind /*
    166   1.1     rmind  * npf_alg_unregister: unregister application-level gateway.
    167   1.1     rmind  */
    168   1.1     rmind int
    169   1.1     rmind npf_alg_unregister(npf_alg_t *alg)
    170   1.1     rmind {
    171   1.9     rmind 	u_int i = alg->na_slot;
    172   1.4     rmind 
    173   1.9     rmind 	/* Deactivate the functions first. */
    174   1.7     rmind 	npf_config_enter();
    175  1.11     rmind 	alg_funcs[i].match = NULL;
    176  1.11     rmind 	alg_funcs[i].translate = NULL;
    177  1.11     rmind 	alg_funcs[i].inspect = NULL;
    178   1.9     rmind 	pserialize_perform(alg_psz);
    179   1.9     rmind 
    180   1.9     rmind 	/* Finally, unregister the ALG. */
    181   1.7     rmind 	npf_ruleset_freealg(npf_config_natset(), alg);
    182   1.9     rmind 	alg->na_name = NULL;
    183   1.7     rmind 	npf_config_exit();
    184   1.5     rmind 
    185   1.1     rmind 	return 0;
    186   1.1     rmind }
    187   1.1     rmind 
    188   1.2     rmind /*
    189   1.2     rmind  * npf_alg_match: call ALG matching inspectors, determine if any ALG matches.
    190   1.2     rmind  */
    191   1.2     rmind bool
    192  1.14     rmind npf_alg_match(npf_cache_t *npc, npf_nat_t *nt, int di)
    193   1.1     rmind {
    194   1.4     rmind 	bool match = false;
    195   1.4     rmind 	int s;
    196   1.1     rmind 
    197   1.4     rmind 	s = pserialize_read_enter();
    198   1.9     rmind 	for (u_int i = 0; i < alg_count; i++) {
    199  1.11     rmind 		const npfa_funcs_t *f = &alg_funcs[i];
    200   1.4     rmind 
    201  1.14     rmind 		if (f->match && f->match(npc, nt, di)) {
    202   1.4     rmind 			match = true;
    203   1.4     rmind 			break;
    204   1.1     rmind 		}
    205   1.1     rmind 	}
    206   1.4     rmind 	pserialize_read_exit(s);
    207   1.4     rmind 	return match;
    208   1.1     rmind }
    209   1.1     rmind 
    210   1.1     rmind /*
    211   1.2     rmind  * npf_alg_exec: execute ALG hooks for translation.
    212   1.1     rmind  */
    213   1.1     rmind void
    214  1.14     rmind npf_alg_exec(npf_cache_t *npc, npf_nat_t *nt, bool forw)
    215   1.1     rmind {
    216   1.4     rmind 	int s;
    217   1.1     rmind 
    218   1.4     rmind 	s = pserialize_read_enter();
    219   1.9     rmind 	for (u_int i = 0; i < alg_count; i++) {
    220  1.11     rmind 		const npfa_funcs_t *f = &alg_funcs[i];
    221   1.6     rmind 
    222  1.11     rmind 		if (f->translate) {
    223  1.14     rmind 			f->translate(npc, nt, forw);
    224   1.1     rmind 		}
    225   1.1     rmind 	}
    226   1.4     rmind 	pserialize_read_exit(s);
    227   1.1     rmind }
    228   1.1     rmind 
    229  1.13     rmind npf_conn_t *
    230  1.14     rmind npf_alg_conn(npf_cache_t *npc, int di)
    231   1.1     rmind {
    232  1.13     rmind 	npf_conn_t *con = NULL;
    233   1.4     rmind 	int s;
    234   1.1     rmind 
    235   1.4     rmind 	s = pserialize_read_enter();
    236   1.9     rmind 	for (u_int i = 0; i < alg_count; i++) {
    237  1.11     rmind 		const npfa_funcs_t *f = &alg_funcs[i];
    238   1.4     rmind 
    239  1.12     rmind 		if (!f->inspect)
    240  1.11     rmind 			continue;
    241  1.14     rmind 		if ((con = f->inspect(npc, di)) != NULL)
    242   1.4     rmind 			break;
    243   1.1     rmind 	}
    244   1.4     rmind 	pserialize_read_exit(s);
    245  1.13     rmind 	return con;
    246   1.1     rmind }
    247  1.15     rmind 
    248  1.15     rmind prop_array_t
    249  1.15     rmind npf_alg_export(void)
    250  1.15     rmind {
    251  1.15     rmind 	prop_array_t alglist = prop_array_create();
    252  1.15     rmind 
    253  1.15     rmind 	KASSERT(npf_config_locked_p());
    254  1.15     rmind 
    255  1.15     rmind 	for (u_int i = 0; i < alg_count; i++) {
    256  1.15     rmind 		const npf_alg_t *alg = &alg_list[i];
    257  1.15     rmind 
    258  1.15     rmind 		if (alg->na_name == NULL) {
    259  1.15     rmind 			continue;
    260  1.15     rmind 		}
    261  1.15     rmind 		prop_dictionary_t algdict = prop_dictionary_create();
    262  1.15     rmind 		prop_dictionary_set_cstring(algdict, "name", alg->na_name);
    263  1.15     rmind 		prop_array_add(alglist, algdict);
    264  1.15     rmind 		prop_object_release(algdict);
    265  1.15     rmind 	}
    266  1.15     rmind 	return alglist;
    267  1.15     rmind }
    268