Home | History | Annotate | Line # | Download | only in npf
npf_alg.c revision 1.1
      1 /*	$NetBSD: npf_alg.c,v 1.1 2010/08/22 18:56:22 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This material is based upon work partially supported by The
      8  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * NPF interface for application level gateways (ALGs).
     34  */
     35 
     36 #ifdef _KERNEL
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: npf_alg.c,v 1.1 2010/08/22 18:56:22 rmind Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/kernel.h>
     42 #endif
     43 
     44 #include <sys/kmem.h>
     45 #include <sys/pool.h>
     46 #include <net/pfil.h>
     47 
     48 #include "npf_impl.h"
     49 
     50 /* NAT ALG structure for registration. */
     51 struct npf_alg {
     52 	LIST_ENTRY(npf_alg)		na_entry;
     53 	void *				na_ptr;
     54 	npf_algfunc_t			na_match_func;
     55 	npf_algfunc_t			na_out_func;
     56 	npf_algfunc_t			na_in_func;
     57 	npf_algfunc_t			na_seid_func;
     58 };
     59 
     60 static LIST_HEAD(, npf_alg)		nat_alg_list;
     61 
     62 void
     63 npf_alg_sysinit(void)
     64 {
     65 
     66 	LIST_INIT(&nat_alg_list);
     67 }
     68 
     69 void
     70 npf_alg_sysfini(void)
     71 {
     72 
     73 	KASSERT(LIST_EMPTY(&nat_alg_list));
     74 }
     75 
     76 /*
     77  * npf_alg_register: register application-level gateway.
     78  *
     79  * XXX: Protected by module lock, but unify serialisation later.
     80  */
     81 npf_alg_t *
     82 npf_alg_register(npf_algfunc_t match, npf_algfunc_t out, npf_algfunc_t in,
     83     npf_algfunc_t seid)
     84 {
     85 	npf_alg_t *alg;
     86 
     87 	alg = kmem_alloc(sizeof(npf_alg_t), KM_SLEEP);
     88 	alg->na_ptr = alg;
     89 	alg->na_match_func = match;
     90 	alg->na_out_func = out;
     91 	alg->na_in_func = in;
     92 	alg->na_seid_func = seid;
     93 	LIST_INSERT_HEAD(&nat_alg_list, alg, na_entry);
     94 	return alg;
     95 }
     96 
     97 /*
     98  * npf_alg_unregister: unregister application-level gateway.
     99  */
    100 int
    101 npf_alg_unregister(npf_alg_t *alg)
    102 {
    103 	npf_alg_t *it;
    104 
    105 	LIST_FOREACH(it, &nat_alg_list, na_entry) {
    106 		if (alg == it)
    107 			break;
    108 	}
    109 	if (it != NULL) {
    110 		LIST_REMOVE(alg, na_entry);
    111 	}
    112 	/* TODO: Flush relevant sessions. */
    113 	kmem_free(alg, sizeof(npf_alg_t));
    114 	return 0;
    115 }
    116 
    117 void
    118 npf_alg_match(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt)
    119 {
    120 	npf_alg_t *alg;
    121 	npf_algfunc_t func;
    122 
    123 	LIST_FOREACH(alg, &nat_alg_list, na_entry) {
    124 		func = alg->na_match_func;
    125 		if (__predict_true(func != NULL)) {
    126 			func(npc, nbuf, nt);
    127 			return;
    128 		}
    129 	}
    130 }
    131 
    132 /*
    133  * npf_alg_exec: execute in/out inspection hooks of each ALG.
    134  */
    135 void
    136 npf_alg_exec(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt, const int di)
    137 {
    138 	npf_alg_t *alg;
    139 
    140 	LIST_FOREACH(alg, &nat_alg_list, na_entry) {
    141 		if ((di & PFIL_OUT) != 0 && alg->na_out_func != NULL) {
    142 			(alg->na_out_func)(npc, nbuf, nt);
    143 			continue;
    144 		}
    145 		if ((di & PFIL_IN) != 0 && alg->na_in_func != NULL) {
    146 			(alg->na_in_func)(npc, nbuf, nt);
    147 			continue;
    148 		}
    149 	}
    150 }
    151 
    152 bool
    153 npf_alg_sessionid(npf_cache_t *npc, nbuf_t *nbuf, npf_cache_t *key)
    154 {
    155 	npf_alg_t *alg;
    156 	npf_algfunc_t func;
    157 
    158 	LIST_FOREACH(alg, &nat_alg_list, na_entry) {
    159 		func = alg->na_seid_func;
    160 		if (__predict_true(func == NULL)) {
    161 			continue;
    162 		}
    163 		if (func(npc, nbuf, key)) {
    164 			return true;
    165 		}
    166 	}
    167 	return false;
    168 }
    169