npf_rproc.c revision 1.9 1 /* $NetBSD: npf_rproc.c,v 1.9 2013/03/11 01:56:37 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2009-2013 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 extension and rule procedure interface.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD");
38
39 #include <sys/param.h>
40 #include <sys/types.h>
41
42 #include <sys/atomic.h>
43 #include <sys/kmem.h>
44 #include <sys/mutex.h>
45 #include <sys/module.h>
46
47 #include "npf_impl.h"
48
49 #define EXT_NAME_LEN 32
50
51 typedef struct npf_ext {
52 char ext_callname[EXT_NAME_LEN];
53 LIST_ENTRY(npf_ext) ext_entry;
54 const npf_ext_ops_t * ext_ops;
55 unsigned ext_refcnt;
56 } npf_ext_t;
57
58 struct npf_rprocset {
59 LIST_HEAD(, npf_rproc) rps_list;
60 };
61
62 #define RPROC_NAME_LEN 32
63 #define RPROC_EXT_COUNT 16
64
65 struct npf_rproc {
66 /* Flags and reference count. */
67 uint32_t rp_flags;
68 u_int rp_refcnt;
69
70 /* Associated extensions and their metadata . */
71 unsigned rp_ext_count;
72 npf_ext_t * rp_ext[RPROC_EXT_COUNT];
73 void * rp_ext_meta[RPROC_EXT_COUNT];
74
75 /* Name of the procedure and list entry. */
76 char rp_name[RPROC_NAME_LEN];
77 LIST_ENTRY(npf_rproc) rp_entry;
78 };
79
80 static LIST_HEAD(, npf_ext) ext_list __cacheline_aligned;
81 static kmutex_t ext_lock __cacheline_aligned;
82
83 void
84 npf_ext_sysinit(void)
85 {
86 mutex_init(&ext_lock, MUTEX_DEFAULT, IPL_NONE);
87 LIST_INIT(&ext_list);
88 }
89
90 void
91 npf_ext_sysfini(void)
92 {
93 KASSERT(LIST_EMPTY(&ext_list));
94 mutex_destroy(&ext_lock);
95 }
96
97 /*
98 * NPF extension management for the rule procedures.
99 */
100
101 static const char npf_ext_prefix[] = "npf_ext_";
102 #define NPF_EXT_PREFLEN (sizeof(npf_ext_prefix) - 1)
103
104 static npf_ext_t *
105 npf_ext_lookup(const char *name, bool autoload)
106 {
107 npf_ext_t *ext;
108 char modname[RPROC_NAME_LEN + NPF_EXT_PREFLEN];
109 int error;
110
111 KASSERT(mutex_owned(&ext_lock));
112
113 again:
114 LIST_FOREACH(ext, &ext_list, ext_entry)
115 if (strcmp(ext->ext_callname, name) == 0)
116 break;
117
118 if (ext != NULL || !autoload)
119 return ext;
120
121 mutex_exit(&ext_lock);
122 autoload = false;
123 snprintf(modname, sizeof(modname), "%s%s", npf_ext_prefix, name);
124 error = module_autoload(modname, MODULE_CLASS_MISC);
125 mutex_enter(&ext_lock);
126
127 if (error)
128 return NULL;
129 goto again;
130 }
131
132 void *
133 npf_ext_register(const char *name, const npf_ext_ops_t *ops)
134 {
135 npf_ext_t *ext;
136
137 ext = kmem_zalloc(sizeof(npf_ext_t), KM_SLEEP);
138 strlcpy(ext->ext_callname, name, EXT_NAME_LEN);
139 ext->ext_ops = ops;
140
141 mutex_enter(&ext_lock);
142 if (npf_ext_lookup(name, false)) {
143 mutex_exit(&ext_lock);
144 kmem_free(ext, sizeof(npf_ext_t));
145 return NULL;
146 }
147 LIST_INSERT_HEAD(&ext_list, ext, ext_entry);
148 mutex_exit(&ext_lock);
149
150 return (void *)ext;
151 }
152
153 int
154 npf_ext_unregister(void *extid)
155 {
156 npf_ext_t *ext = extid;
157
158 /*
159 * Check if in-use first (re-check with the lock held).
160 */
161 if (ext->ext_refcnt) {
162 return EBUSY;
163 }
164
165 mutex_enter(&ext_lock);
166 if (ext->ext_refcnt) {
167 mutex_exit(&ext_lock);
168 return EBUSY;
169 }
170 KASSERT(npf_ext_lookup(ext->ext_callname, false));
171 LIST_REMOVE(ext, ext_entry);
172 mutex_exit(&ext_lock);
173
174 kmem_free(ext, sizeof(npf_ext_t));
175 return 0;
176 }
177
178 int
179 npf_ext_construct(const char *name, npf_rproc_t *rp, prop_dictionary_t params)
180 {
181 const npf_ext_ops_t *extops;
182 npf_ext_t *ext;
183 unsigned i;
184 int error;
185
186 if (rp->rp_ext_count >= RPROC_EXT_COUNT) {
187 return ENOSPC;
188 }
189
190 mutex_enter(&ext_lock);
191 ext = npf_ext_lookup(name, true);
192 if (ext) {
193 atomic_inc_uint(&ext->ext_refcnt);
194 }
195 mutex_exit(&ext_lock);
196
197 if (!ext) {
198 return ENOENT;
199 }
200
201 extops = ext->ext_ops;
202 KASSERT(extops != NULL);
203
204 error = extops->ctor(rp, params);
205 if (error) {
206 atomic_dec_uint(&ext->ext_refcnt);
207 return error;
208 }
209 i = rp->rp_ext_count++;
210 rp->rp_ext[i] = ext;
211 return 0;
212 }
213
214 /*
215 * Rule procedure management.
216 */
217
218 npf_rprocset_t *
219 npf_rprocset_create(void)
220 {
221 npf_rprocset_t *rpset;
222
223 rpset = kmem_zalloc(sizeof(npf_rprocset_t), KM_SLEEP);
224 LIST_INIT(&rpset->rps_list);
225 return rpset;
226 }
227
228 void
229 npf_rprocset_destroy(npf_rprocset_t *rpset)
230 {
231 npf_rproc_t *rp;
232
233 while ((rp = LIST_FIRST(&rpset->rps_list)) != NULL) {
234 LIST_REMOVE(rp, rp_entry);
235 npf_rproc_release(rp);
236 }
237 kmem_free(rpset, sizeof(npf_rprocset_t));
238 }
239
240 /*
241 * npf_rproc_lookup: find a rule procedure by the name.
242 */
243 npf_rproc_t *
244 npf_rprocset_lookup(npf_rprocset_t *rpset, const char *name)
245 {
246 npf_rproc_t *rp;
247
248 LIST_FOREACH(rp, &rpset->rps_list, rp_entry) {
249 if (strncmp(rp->rp_name, name, RPROC_NAME_LEN) == 0)
250 break;
251 }
252 return rp;
253 }
254
255 /*
256 * npf_rproc_insert: insert a new rule procedure into the set.
257 */
258 void
259 npf_rprocset_insert(npf_rprocset_t *rpset, npf_rproc_t *rp)
260 {
261 LIST_INSERT_HEAD(&rpset->rps_list, rp, rp_entry);
262 }
263
264 /*
265 * npf_rproc_create: construct a new rule procedure, lookup and associate
266 * the extension calls with it.
267 */
268 npf_rproc_t *
269 npf_rproc_create(prop_dictionary_t rpdict)
270 {
271 const char *name;
272 npf_rproc_t *rp;
273
274 if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
275 return NULL;
276 }
277
278 rp = kmem_intr_zalloc(sizeof(npf_rproc_t), KM_SLEEP);
279 rp->rp_refcnt = 1;
280
281 strlcpy(rp->rp_name, name, RPROC_NAME_LEN);
282 prop_dictionary_get_uint32(rpdict, "flags", &rp->rp_flags);
283 return rp;
284 }
285
286 /*
287 * npf_rproc_acquire: acquire the reference on the rule procedure.
288 */
289 void
290 npf_rproc_acquire(npf_rproc_t *rp)
291 {
292 atomic_inc_uint(&rp->rp_refcnt);
293 }
294
295 /*
296 * npf_rproc_release: drop the reference count and destroy the rule
297 * procedure on the last reference.
298 */
299 void
300 npf_rproc_release(npf_rproc_t *rp)
301 {
302
303 KASSERT(rp->rp_refcnt > 0);
304 if (atomic_dec_uint_nv(&rp->rp_refcnt) != 0) {
305 return;
306 }
307 /* XXXintr */
308 for (unsigned i = 0; i < rp->rp_ext_count; i++) {
309 npf_ext_t *ext = rp->rp_ext[i];
310 const npf_ext_ops_t *extops = ext->ext_ops;
311
312 extops->dtor(rp, rp->rp_ext_meta[i]);
313 atomic_dec_uint(&ext->ext_refcnt);
314 }
315 kmem_intr_free(rp, sizeof(npf_rproc_t));
316 }
317
318 void
319 npf_rproc_assign(npf_rproc_t *rp, void *params)
320 {
321 unsigned i = rp->rp_ext_count;
322
323 /* Note: params may be NULL. */
324 KASSERT(i < RPROC_EXT_COUNT);
325 rp->rp_ext_meta[i] = params;
326 }
327
328 /*
329 * npf_rproc_run: run the rule procedure by executing each extension call.
330 *
331 * => Reference on the rule procedure must be held.
332 */
333 void
334 npf_rproc_run(npf_cache_t *npc, nbuf_t *nbuf, npf_rproc_t *rp, int *decision)
335 {
336 const unsigned extcount = rp->rp_ext_count;
337
338 KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
339 KASSERT(rp->rp_refcnt > 0);
340
341 for (unsigned i = 0; i < extcount; i++) {
342 const npf_ext_t *ext = rp->rp_ext[i];
343 const npf_ext_ops_t *extops = ext->ext_ops;
344
345 KASSERT(ext->ext_refcnt > 0);
346 extops->proc(npc, nbuf, rp->rp_ext_meta[i], decision);
347
348 if (nbuf_flag_p(nbuf, NBUF_DATAREF_RESET)) {
349 npf_recache(npc, nbuf);
350 }
351 }
352 }
353