npf_rproc.c revision 1.13 1 /* $NetBSD: npf_rproc.c,v 1.13 2016/12/10 19:02:18 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 int
265 npf_rprocset_export(const npf_rprocset_t *rpset, prop_array_t rprocs)
266 {
267 prop_dictionary_t rpdict;
268 const npf_rproc_t *rp;
269
270 LIST_FOREACH(rp, &rpset->rps_list, rp_entry) {
271 rpdict = prop_dictionary_create();
272 prop_array_t extcalls = prop_array_create();
273 prop_dictionary_set_and_rel(rpdict, "extcalls", extcalls);
274 prop_dictionary_set_cstring(rpdict, "name", rp->rp_name);
275 prop_dictionary_set_uint32(rpdict, "flags", rp->rp_flags);
276 prop_array_add(rprocs, rpdict);
277 prop_object_release(rpdict);
278 }
279 return 0;
280 }
281
282 /*
283 * npf_rproc_create: construct a new rule procedure, lookup and associate
284 * the extension calls with it.
285 */
286 npf_rproc_t *
287 npf_rproc_create(prop_dictionary_t rpdict)
288 {
289 const char *name;
290 npf_rproc_t *rp;
291
292 if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
293 return NULL;
294 }
295
296 rp = kmem_intr_zalloc(sizeof(npf_rproc_t), KM_SLEEP);
297 rp->rp_refcnt = 1;
298
299 strlcpy(rp->rp_name, name, RPROC_NAME_LEN);
300 prop_dictionary_get_uint32(rpdict, "flags", &rp->rp_flags);
301 return rp;
302 }
303
304 /*
305 * npf_rproc_acquire: acquire the reference on the rule procedure.
306 */
307 void
308 npf_rproc_acquire(npf_rproc_t *rp)
309 {
310 atomic_inc_uint(&rp->rp_refcnt);
311 }
312
313 /*
314 * npf_rproc_release: drop the reference count and destroy the rule
315 * procedure on the last reference.
316 */
317 void
318 npf_rproc_release(npf_rproc_t *rp)
319 {
320
321 KASSERT(rp->rp_refcnt > 0);
322 if (atomic_dec_uint_nv(&rp->rp_refcnt) != 0) {
323 return;
324 }
325 /* XXXintr */
326 for (unsigned i = 0; i < rp->rp_ext_count; i++) {
327 npf_ext_t *ext = rp->rp_ext[i];
328 const npf_ext_ops_t *extops = ext->ext_ops;
329
330 extops->dtor(rp, rp->rp_ext_meta[i]);
331 atomic_dec_uint(&ext->ext_refcnt);
332 }
333 kmem_intr_free(rp, sizeof(npf_rproc_t));
334 }
335
336 void
337 npf_rproc_assign(npf_rproc_t *rp, void *params)
338 {
339 unsigned i = rp->rp_ext_count;
340
341 /* Note: params may be NULL. */
342 KASSERT(i < RPROC_EXT_COUNT);
343 rp->rp_ext_meta[i] = params;
344 }
345
346 /*
347 * npf_rproc_run: run the rule procedure by executing each extension call.
348 *
349 * => Reference on the rule procedure must be held.
350 */
351 bool
352 npf_rproc_run(npf_cache_t *npc, npf_rproc_t *rp, int *decision)
353 {
354 const unsigned extcount = rp->rp_ext_count;
355
356 KASSERT(!nbuf_flag_p(npc->npc_nbuf, NBUF_DATAREF_RESET));
357 KASSERT(rp->rp_refcnt > 0);
358
359 for (unsigned i = 0; i < extcount; i++) {
360 const npf_ext_t *ext = rp->rp_ext[i];
361 const npf_ext_ops_t *extops = ext->ext_ops;
362
363 KASSERT(ext->ext_refcnt > 0);
364 if (!extops->proc(npc, rp->rp_ext_meta[i], decision)) {
365 return false;
366 }
367
368 if (nbuf_flag_p(npc->npc_nbuf, NBUF_DATAREF_RESET)) {
369 npf_recache(npc);
370 }
371 }
372
373 return true;
374 }
375