Home | History | Annotate | Line # | Download | only in npf
npf_alg.c revision 1.5.2.1
      1  1.5.2.1    tls /*	$NetBSD: npf_alg.c,v 1.5.2.1 2013/02/25 00:30:02 tls Exp $	*/
      2      1.1  rmind 
      3      1.1  rmind /*-
      4      1.1  rmind  * Copyright (c) 2010 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.1  rmind  * NPF interface for application level gateways (ALGs).
     34      1.1  rmind  */
     35      1.1  rmind 
     36      1.1  rmind #include <sys/cdefs.h>
     37  1.5.2.1    tls __KERNEL_RCSID(0, "$NetBSD: npf_alg.c,v 1.5.2.1 2013/02/25 00:30:02 tls 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.1  rmind 
     47      1.1  rmind #include "npf_impl.h"
     48      1.1  rmind 
     49      1.1  rmind /* NAT ALG structure for registration. */
     50      1.1  rmind struct npf_alg {
     51  1.5.2.1    tls 	LIST_ENTRY(npf_alg)	na_entry;
     52  1.5.2.1    tls 	npf_alg_t *		na_bptr;
     53  1.5.2.1    tls 	npf_alg_func_t		na_match_func;
     54  1.5.2.1    tls 	npf_alg_func_t		na_tr_func;
     55  1.5.2.1    tls 	npf_alg_sfunc_t		na_se_func;
     56      1.1  rmind };
     57      1.1  rmind 
     58  1.5.2.1    tls static LIST_HEAD(, npf_alg)	nat_alg_list	__cacheline_aligned;
     59  1.5.2.1    tls static kmutex_t			nat_alg_lock	__cacheline_aligned;
     60  1.5.2.1    tls static pserialize_t		nat_alg_psz	__cacheline_aligned;
     61      1.1  rmind 
     62      1.1  rmind void
     63      1.1  rmind npf_alg_sysinit(void)
     64      1.1  rmind {
     65      1.1  rmind 
     66      1.4  rmind 	mutex_init(&nat_alg_lock, MUTEX_DEFAULT, IPL_NONE);
     67      1.4  rmind 	nat_alg_psz = pserialize_create();
     68      1.1  rmind 	LIST_INIT(&nat_alg_list);
     69      1.1  rmind }
     70      1.1  rmind 
     71      1.1  rmind void
     72      1.1  rmind npf_alg_sysfini(void)
     73      1.1  rmind {
     74      1.1  rmind 
     75      1.1  rmind 	KASSERT(LIST_EMPTY(&nat_alg_list));
     76      1.4  rmind 	pserialize_destroy(nat_alg_psz);
     77      1.4  rmind 	mutex_destroy(&nat_alg_lock);
     78      1.1  rmind }
     79      1.1  rmind 
     80      1.1  rmind /*
     81      1.1  rmind  * npf_alg_register: register application-level gateway.
     82      1.1  rmind  */
     83      1.1  rmind npf_alg_t *
     84  1.5.2.1    tls npf_alg_register(npf_alg_func_t mfunc, npf_alg_func_t tfunc,
     85  1.5.2.1    tls     npf_alg_sfunc_t sfunc)
     86      1.1  rmind {
     87      1.1  rmind 	npf_alg_t *alg;
     88      1.1  rmind 
     89      1.3  rmind 	alg = kmem_zalloc(sizeof(npf_alg_t), KM_SLEEP);
     90      1.2  rmind 	alg->na_bptr = alg;
     91  1.5.2.1    tls 	alg->na_match_func = mfunc;
     92  1.5.2.1    tls 	alg->na_tr_func = tfunc;
     93  1.5.2.1    tls 	alg->na_se_func = sfunc;
     94      1.4  rmind 
     95      1.4  rmind 	mutex_enter(&nat_alg_lock);
     96      1.1  rmind 	LIST_INSERT_HEAD(&nat_alg_list, alg, na_entry);
     97      1.4  rmind 	mutex_exit(&nat_alg_lock);
     98      1.4  rmind 
     99      1.1  rmind 	return alg;
    100      1.1  rmind }
    101      1.1  rmind 
    102      1.1  rmind /*
    103      1.1  rmind  * npf_alg_unregister: unregister application-level gateway.
    104      1.1  rmind  */
    105      1.1  rmind int
    106      1.1  rmind npf_alg_unregister(npf_alg_t *alg)
    107      1.1  rmind {
    108      1.4  rmind 	mutex_enter(&nat_alg_lock);
    109      1.4  rmind 	LIST_REMOVE(alg, na_entry);
    110      1.4  rmind 	pserialize_perform(nat_alg_psz);
    111      1.4  rmind 	mutex_exit(&nat_alg_lock);
    112      1.4  rmind 
    113  1.5.2.1    tls 	npf_config_enter();
    114  1.5.2.1    tls 	npf_ruleset_freealg(npf_config_natset(), alg);
    115  1.5.2.1    tls 	npf_config_exit();
    116      1.5  rmind 
    117      1.1  rmind 	kmem_free(alg, sizeof(npf_alg_t));
    118      1.1  rmind 	return 0;
    119      1.1  rmind }
    120      1.1  rmind 
    121      1.2  rmind /*
    122      1.2  rmind  * npf_alg_match: call ALG matching inspectors, determine if any ALG matches.
    123      1.2  rmind  */
    124      1.2  rmind bool
    125  1.5.2.1    tls npf_alg_match(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt, int di)
    126      1.1  rmind {
    127      1.1  rmind 	npf_alg_t *alg;
    128      1.4  rmind 	bool match = false;
    129      1.4  rmind 	int s;
    130      1.1  rmind 
    131      1.4  rmind 	s = pserialize_read_enter();
    132      1.1  rmind 	LIST_FOREACH(alg, &nat_alg_list, na_entry) {
    133  1.5.2.1    tls 		npf_alg_func_t func = alg->na_match_func;
    134      1.4  rmind 
    135  1.5.2.1    tls 		if (func && func(npc, nbuf, nt, di)) {
    136      1.4  rmind 			match = true;
    137      1.4  rmind 			break;
    138      1.1  rmind 		}
    139      1.1  rmind 	}
    140      1.4  rmind 	pserialize_read_exit(s);
    141      1.4  rmind 	return match;
    142      1.1  rmind }
    143      1.1  rmind 
    144      1.1  rmind /*
    145      1.2  rmind  * npf_alg_exec: execute ALG hooks for translation.
    146      1.1  rmind  */
    147      1.1  rmind void
    148  1.5.2.1    tls npf_alg_exec(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt, int di)
    149      1.1  rmind {
    150      1.1  rmind 	npf_alg_t *alg;
    151      1.4  rmind 	int s;
    152      1.1  rmind 
    153      1.4  rmind 	s = pserialize_read_enter();
    154      1.1  rmind 	LIST_FOREACH(alg, &nat_alg_list, na_entry) {
    155  1.5.2.1    tls 		npf_alg_func_t func;
    156  1.5.2.1    tls 
    157  1.5.2.1    tls 		if ((func = alg->na_tr_func) != NULL) {
    158  1.5.2.1    tls 			(func)(npc, nbuf, nt, di);
    159      1.1  rmind 		}
    160      1.1  rmind 	}
    161      1.4  rmind 	pserialize_read_exit(s);
    162      1.1  rmind }
    163      1.1  rmind 
    164  1.5.2.1    tls npf_session_t *
    165  1.5.2.1    tls npf_alg_session(npf_cache_t *npc, nbuf_t *nbuf, int di)
    166      1.1  rmind {
    167  1.5.2.1    tls 	npf_session_t *se = NULL;
    168      1.1  rmind 	npf_alg_t *alg;
    169      1.4  rmind 	int s;
    170      1.1  rmind 
    171      1.4  rmind 	s = pserialize_read_enter();
    172      1.1  rmind 	LIST_FOREACH(alg, &nat_alg_list, na_entry) {
    173  1.5.2.1    tls 		npf_alg_sfunc_t func = alg->na_se_func;
    174      1.4  rmind 
    175  1.5.2.1    tls 		if (func && (se = func(npc, nbuf, di)) != NULL) {
    176      1.4  rmind 			break;
    177      1.1  rmind 		}
    178      1.1  rmind 	}
    179      1.4  rmind 	pserialize_read_exit(s);
    180  1.5.2.1    tls 	return se;
    181      1.1  rmind }
    182