Home | History | Annotate | Line # | Download | only in npf
npf_alg.c revision 1.4
      1  1.4  rmind /*	$NetBSD: npf_alg.c,v 1.4 2012/06/22 13:43:17 rmind 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.4  rmind __KERNEL_RCSID(0, "$NetBSD: npf_alg.c,v 1.4 2012/06/22 13:43:17 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.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.1  rmind 	LIST_ENTRY(npf_alg)		na_entry;
     52  1.2  rmind 	npf_alg_t *			na_bptr;
     53  1.1  rmind 	npf_algfunc_t			na_match_func;
     54  1.1  rmind 	npf_algfunc_t			na_out_func;
     55  1.1  rmind 	npf_algfunc_t			na_in_func;
     56  1.1  rmind 	npf_algfunc_t			na_seid_func;
     57  1.1  rmind };
     58  1.1  rmind 
     59  1.4  rmind static LIST_HEAD(, npf_alg)		nat_alg_list	__cacheline_aligned;
     60  1.4  rmind static kmutex_t				nat_alg_lock	__cacheline_aligned;
     61  1.4  rmind static pserialize_t			nat_alg_psz	__cacheline_aligned;
     62  1.1  rmind 
     63  1.1  rmind void
     64  1.1  rmind npf_alg_sysinit(void)
     65  1.1  rmind {
     66  1.1  rmind 
     67  1.4  rmind 	mutex_init(&nat_alg_lock, MUTEX_DEFAULT, IPL_NONE);
     68  1.4  rmind 	nat_alg_psz = pserialize_create();
     69  1.1  rmind 	LIST_INIT(&nat_alg_list);
     70  1.1  rmind }
     71  1.1  rmind 
     72  1.1  rmind void
     73  1.1  rmind npf_alg_sysfini(void)
     74  1.1  rmind {
     75  1.1  rmind 
     76  1.1  rmind 	KASSERT(LIST_EMPTY(&nat_alg_list));
     77  1.4  rmind 	pserialize_destroy(nat_alg_psz);
     78  1.4  rmind 	mutex_destroy(&nat_alg_lock);
     79  1.1  rmind }
     80  1.1  rmind 
     81  1.1  rmind /*
     82  1.1  rmind  * npf_alg_register: register application-level gateway.
     83  1.1  rmind  *
     84  1.1  rmind  * XXX: Protected by module lock, but unify serialisation later.
     85  1.1  rmind  */
     86  1.1  rmind npf_alg_t *
     87  1.1  rmind npf_alg_register(npf_algfunc_t match, npf_algfunc_t out, npf_algfunc_t in,
     88  1.1  rmind     npf_algfunc_t seid)
     89  1.1  rmind {
     90  1.1  rmind 	npf_alg_t *alg;
     91  1.1  rmind 
     92  1.3  rmind 	alg = kmem_zalloc(sizeof(npf_alg_t), KM_SLEEP);
     93  1.2  rmind 	alg->na_bptr = alg;
     94  1.1  rmind 	alg->na_match_func = match;
     95  1.1  rmind 	alg->na_out_func = out;
     96  1.1  rmind 	alg->na_in_func = in;
     97  1.1  rmind 	alg->na_seid_func = seid;
     98  1.4  rmind 
     99  1.4  rmind 	mutex_enter(&nat_alg_lock);
    100  1.1  rmind 	LIST_INSERT_HEAD(&nat_alg_list, alg, na_entry);
    101  1.4  rmind 	mutex_exit(&nat_alg_lock);
    102  1.4  rmind 
    103  1.1  rmind 	return alg;
    104  1.1  rmind }
    105  1.1  rmind 
    106  1.1  rmind /*
    107  1.1  rmind  * npf_alg_unregister: unregister application-level gateway.
    108  1.1  rmind  */
    109  1.1  rmind int
    110  1.1  rmind npf_alg_unregister(npf_alg_t *alg)
    111  1.1  rmind {
    112  1.1  rmind 
    113  1.4  rmind 	mutex_enter(&nat_alg_lock);
    114  1.4  rmind 	LIST_REMOVE(alg, na_entry);
    115  1.4  rmind 	pserialize_perform(nat_alg_psz);
    116  1.4  rmind 	mutex_exit(&nat_alg_lock);
    117  1.4  rmind 
    118  1.4  rmind 	npf_nat_freealg(alg);
    119  1.1  rmind 	kmem_free(alg, sizeof(npf_alg_t));
    120  1.4  rmind 
    121  1.1  rmind 	return 0;
    122  1.1  rmind }
    123  1.1  rmind 
    124  1.2  rmind /*
    125  1.2  rmind  * npf_alg_match: call ALG matching inspectors, determine if any ALG matches.
    126  1.2  rmind  */
    127  1.2  rmind bool
    128  1.1  rmind npf_alg_match(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt)
    129  1.1  rmind {
    130  1.1  rmind 	npf_alg_t *alg;
    131  1.4  rmind 	bool match = false;
    132  1.4  rmind 	int s;
    133  1.1  rmind 
    134  1.4  rmind 	s = pserialize_read_enter();
    135  1.1  rmind 	LIST_FOREACH(alg, &nat_alg_list, na_entry) {
    136  1.4  rmind 		npf_algfunc_t func = alg->na_match_func;
    137  1.4  rmind 
    138  1.2  rmind 		if (func && func(npc, nbuf, nt)) {
    139  1.4  rmind 			match = true;
    140  1.4  rmind 			break;
    141  1.1  rmind 		}
    142  1.1  rmind 	}
    143  1.4  rmind 	pserialize_read_exit(s);
    144  1.4  rmind 	return match;
    145  1.1  rmind }
    146  1.1  rmind 
    147  1.1  rmind /*
    148  1.2  rmind  * npf_alg_exec: execute ALG hooks for translation.
    149  1.1  rmind  */
    150  1.1  rmind void
    151  1.1  rmind npf_alg_exec(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt, const int di)
    152  1.1  rmind {
    153  1.1  rmind 	npf_alg_t *alg;
    154  1.4  rmind 	int s;
    155  1.1  rmind 
    156  1.4  rmind 	s = pserialize_read_enter();
    157  1.1  rmind 	LIST_FOREACH(alg, &nat_alg_list, na_entry) {
    158  1.1  rmind 		if ((di & PFIL_OUT) != 0 && alg->na_out_func != NULL) {
    159  1.1  rmind 			(alg->na_out_func)(npc, nbuf, nt);
    160  1.1  rmind 			continue;
    161  1.1  rmind 		}
    162  1.1  rmind 		if ((di & PFIL_IN) != 0 && alg->na_in_func != NULL) {
    163  1.1  rmind 			(alg->na_in_func)(npc, nbuf, nt);
    164  1.1  rmind 			continue;
    165  1.1  rmind 		}
    166  1.1  rmind 	}
    167  1.4  rmind 	pserialize_read_exit(s);
    168  1.1  rmind }
    169  1.1  rmind 
    170  1.1  rmind bool
    171  1.1  rmind npf_alg_sessionid(npf_cache_t *npc, nbuf_t *nbuf, npf_cache_t *key)
    172  1.1  rmind {
    173  1.1  rmind 	npf_alg_t *alg;
    174  1.4  rmind 	bool nkey = false;
    175  1.4  rmind 	int s;
    176  1.1  rmind 
    177  1.4  rmind 	s = pserialize_read_enter();
    178  1.1  rmind 	LIST_FOREACH(alg, &nat_alg_list, na_entry) {
    179  1.4  rmind 		npf_algfunc_t func = alg->na_seid_func;
    180  1.4  rmind 
    181  1.2  rmind 		if (func && func(npc, nbuf, (npf_nat_t *)key)) {
    182  1.4  rmind 			nkey = true;
    183  1.4  rmind 			break;
    184  1.1  rmind 		}
    185  1.1  rmind 	}
    186  1.4  rmind 	pserialize_read_exit(s);
    187  1.4  rmind 	return nkey;
    188  1.1  rmind }
    189