npf_alg.c revision 1.2.10.1 1 /* $NetBSD: npf_alg.c,v 1.2.10.1 2012/04/17 00:08:38 yamt 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 * XXX: locking
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: npf_alg.c,v 1.2.10.1 2012/04/17 00:08:38 yamt Exp $");
40
41 #include <sys/param.h>
42 #include <sys/kmem.h>
43 #include <sys/pool.h>
44 #include <net/pfil.h>
45
46 #include "npf_impl.h"
47
48 /* NAT ALG structure for registration. */
49 struct npf_alg {
50 LIST_ENTRY(npf_alg) na_entry;
51 npf_alg_t * na_bptr;
52 npf_algfunc_t na_match_func;
53 npf_algfunc_t na_out_func;
54 npf_algfunc_t na_in_func;
55 npf_algfunc_t na_seid_func;
56 };
57
58 static LIST_HEAD(, npf_alg) nat_alg_list __read_mostly;
59
60 void
61 npf_alg_sysinit(void)
62 {
63
64 LIST_INIT(&nat_alg_list);
65 }
66
67 void
68 npf_alg_sysfini(void)
69 {
70
71 KASSERT(LIST_EMPTY(&nat_alg_list));
72 }
73
74 /*
75 * npf_alg_register: register application-level gateway.
76 *
77 * XXX: Protected by module lock, but unify serialisation later.
78 */
79 npf_alg_t *
80 npf_alg_register(npf_algfunc_t match, npf_algfunc_t out, npf_algfunc_t in,
81 npf_algfunc_t seid)
82 {
83 npf_alg_t *alg;
84
85 alg = kmem_zalloc(sizeof(npf_alg_t), KM_SLEEP);
86 alg->na_bptr = alg;
87 alg->na_match_func = match;
88 alg->na_out_func = out;
89 alg->na_in_func = in;
90 alg->na_seid_func = seid;
91 LIST_INSERT_HEAD(&nat_alg_list, alg, na_entry);
92 return alg;
93 }
94
95 /*
96 * npf_alg_unregister: unregister application-level gateway.
97 */
98 int
99 npf_alg_unregister(npf_alg_t *alg)
100 {
101 npf_alg_t *it;
102
103 LIST_FOREACH(it, &nat_alg_list, na_entry) {
104 if (alg == it)
105 break;
106 }
107 if (it != NULL) {
108 LIST_REMOVE(alg, na_entry);
109 }
110 /* TODO: Flush relevant sessions. */
111 kmem_free(alg, sizeof(npf_alg_t));
112 return 0;
113 }
114
115 /*
116 * npf_alg_match: call ALG matching inspectors, determine if any ALG matches.
117 */
118 bool
119 npf_alg_match(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt)
120 {
121 npf_alg_t *alg;
122 npf_algfunc_t func;
123
124 LIST_FOREACH(alg, &nat_alg_list, na_entry) {
125 func = alg->na_match_func;
126 if (func && func(npc, nbuf, nt)) {
127 return true;
128 }
129 }
130 return false;
131 }
132
133 /*
134 * npf_alg_exec: execute ALG hooks for translation.
135 */
136 void
137 npf_alg_exec(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt, const int di)
138 {
139 npf_alg_t *alg;
140
141 LIST_FOREACH(alg, &nat_alg_list, na_entry) {
142 if ((di & PFIL_OUT) != 0 && alg->na_out_func != NULL) {
143 (alg->na_out_func)(npc, nbuf, nt);
144 continue;
145 }
146 if ((di & PFIL_IN) != 0 && alg->na_in_func != NULL) {
147 (alg->na_in_func)(npc, nbuf, nt);
148 continue;
149 }
150 }
151 }
152
153 bool
154 npf_alg_sessionid(npf_cache_t *npc, nbuf_t *nbuf, npf_cache_t *key)
155 {
156 npf_alg_t *alg;
157 npf_algfunc_t func;
158
159 LIST_FOREACH(alg, &nat_alg_list, na_entry) {
160 func = alg->na_seid_func;
161 if (func && func(npc, nbuf, (npf_nat_t *)key)) {
162 return true;
163 }
164 }
165 return false;
166 }
167