npf_ctl.c revision 1.17.2.5 1 1.17.2.3 tls /* $NetBSD: npf_ctl.c,v 1.17.2.5 2017/12/03 11:39:03 jdolecek Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.17.2.4 tls * Copyright (c) 2009-2014 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 device control.
34 1.1 rmind *
35 1.1 rmind * Implementation of (re)loading, construction of tables and rules.
36 1.1 rmind * NPF proplib(9) dictionary consumer.
37 1.1 rmind */
38 1.1 rmind
39 1.17.2.5 jdolecek #ifdef _KERNEL
40 1.1 rmind #include <sys/cdefs.h>
41 1.17.2.3 tls __KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.17.2.5 2017/12/03 11:39:03 jdolecek Exp $");
42 1.1 rmind
43 1.1 rmind #include <sys/param.h>
44 1.1 rmind #include <sys/conf.h>
45 1.17.2.2 tls #include <sys/kmem.h>
46 1.17.2.2 tls #include <net/bpf.h>
47 1.1 rmind
48 1.1 rmind #include <prop/proplib.h>
49 1.17.2.5 jdolecek #endif
50 1.1 rmind
51 1.1 rmind #include "npf_impl.h"
52 1.17.2.4 tls #include "npf_conn.h"
53 1.1 rmind
54 1.12 rmind #define NPF_ERR_DEBUG(e) \
55 1.12 rmind prop_dictionary_set_cstring_nocopy((e), "source-file", __FILE__); \
56 1.12 rmind prop_dictionary_set_uint32((e), "source-line", __LINE__);
57 1.12 rmind
58 1.17.2.5 jdolecek #ifdef _KERNEL
59 1.1 rmind /*
60 1.1 rmind * npfctl_switch: enable or disable packet inspection.
61 1.1 rmind */
62 1.1 rmind int
63 1.1 rmind npfctl_switch(void *data)
64 1.1 rmind {
65 1.1 rmind const bool onoff = *(int *)data ? true : false;
66 1.1 rmind int error;
67 1.1 rmind
68 1.1 rmind if (onoff) {
69 1.1 rmind /* Enable: add pfil hooks. */
70 1.17.2.4 tls error = npf_pfil_register(false);
71 1.1 rmind } else {
72 1.1 rmind /* Disable: remove pfil hooks. */
73 1.17.2.4 tls npf_pfil_unregister(false);
74 1.1 rmind error = 0;
75 1.1 rmind }
76 1.1 rmind return error;
77 1.1 rmind }
78 1.17.2.5 jdolecek #endif
79 1.1 rmind
80 1.6 rmind static int __noinline
81 1.17.2.4 tls npf_mk_table_entries(npf_table_t *t, prop_array_t entries)
82 1.17.2.4 tls {
83 1.17.2.4 tls prop_object_iterator_t eit;
84 1.17.2.4 tls prop_dictionary_t ent;
85 1.17.2.4 tls int error = 0;
86 1.17.2.4 tls
87 1.17.2.5 jdolecek if (prop_object_type(entries) != PROP_TYPE_ARRAY) {
88 1.17.2.5 jdolecek return EINVAL;
89 1.17.2.5 jdolecek }
90 1.17.2.4 tls eit = prop_array_iterator(entries);
91 1.17.2.4 tls while ((ent = prop_object_iterator_next(eit)) != NULL) {
92 1.17.2.4 tls const npf_addr_t *addr;
93 1.17.2.4 tls npf_netmask_t mask;
94 1.17.2.4 tls int alen;
95 1.17.2.4 tls
96 1.17.2.4 tls /* Get address and mask. Add a table entry. */
97 1.17.2.4 tls prop_object_t obj = prop_dictionary_get(ent, "addr");
98 1.17.2.4 tls addr = (const npf_addr_t *)prop_data_data_nocopy(obj);
99 1.17.2.4 tls prop_dictionary_get_uint8(ent, "mask", &mask);
100 1.17.2.4 tls alen = prop_data_size(obj);
101 1.17.2.4 tls
102 1.17.2.4 tls error = npf_table_insert(t, alen, addr, mask);
103 1.17.2.4 tls if (error)
104 1.17.2.4 tls break;
105 1.17.2.4 tls }
106 1.17.2.4 tls prop_object_iterator_release(eit);
107 1.17.2.4 tls return error;
108 1.17.2.4 tls }
109 1.17.2.4 tls
110 1.17.2.4 tls static int __noinline
111 1.17.2.5 jdolecek npf_mk_tables(npf_t *npf, npf_tableset_t *tblset, prop_array_t tables,
112 1.12 rmind prop_dictionary_t errdict)
113 1.1 rmind {
114 1.1 rmind prop_object_iterator_t it;
115 1.1 rmind prop_dictionary_t tbldict;
116 1.1 rmind int error = 0;
117 1.1 rmind
118 1.1 rmind /* Tables - array. */
119 1.12 rmind if (prop_object_type(tables) != PROP_TYPE_ARRAY) {
120 1.12 rmind NPF_ERR_DEBUG(errdict);
121 1.1 rmind return EINVAL;
122 1.12 rmind }
123 1.1 rmind
124 1.1 rmind it = prop_array_iterator(tables);
125 1.1 rmind while ((tbldict = prop_object_iterator_next(it)) != NULL) {
126 1.17.2.4 tls const char *name;
127 1.1 rmind npf_table_t *t;
128 1.17.2.5 jdolecek uint64_t tid;
129 1.1 rmind int type;
130 1.1 rmind
131 1.1 rmind /* Table - dictionary. */
132 1.1 rmind if (prop_object_type(tbldict) != PROP_TYPE_DICTIONARY) {
133 1.12 rmind NPF_ERR_DEBUG(errdict);
134 1.1 rmind error = EINVAL;
135 1.1 rmind break;
136 1.1 rmind }
137 1.1 rmind
138 1.17.2.4 tls /* Table name, ID and type. Validate them. */
139 1.17.2.4 tls if (!prop_dictionary_get_cstring_nocopy(tbldict, "name", &name)) {
140 1.17.2.4 tls NPF_ERR_DEBUG(errdict);
141 1.17.2.4 tls error = EINVAL;
142 1.17.2.4 tls break;
143 1.17.2.4 tls }
144 1.17.2.5 jdolecek prop_dictionary_get_uint64(tbldict, "id", &tid);
145 1.6 rmind prop_dictionary_get_int32(tbldict, "type", &type);
146 1.17.2.5 jdolecek error = npf_table_check(tblset, name, (u_int)tid, type);
147 1.17.2.4 tls if (error) {
148 1.17.2.4 tls NPF_ERR_DEBUG(errdict);
149 1.17.2.4 tls break;
150 1.17.2.4 tls }
151 1.6 rmind
152 1.17.2.4 tls /* Get the entries or binary data. */
153 1.17.2.5 jdolecek prop_array_t ents = prop_dictionary_get(tbldict, "entries");
154 1.17.2.4 tls prop_object_t obj = prop_dictionary_get(tbldict, "data");
155 1.17.2.4 tls void *blob = prop_data_data(obj);
156 1.17.2.4 tls size_t size = prop_data_size(obj);
157 1.17.2.4 tls
158 1.17.2.4 tls if (type == NPF_TABLE_CDB && (blob == NULL || size == 0)) {
159 1.17.2.4 tls NPF_ERR_DEBUG(errdict);
160 1.17.2.4 tls error = EINVAL;
161 1.1 rmind break;
162 1.17.2.4 tls }
163 1.1 rmind
164 1.1 rmind /* Create and insert the table. */
165 1.17.2.5 jdolecek t = npf_table_create(name, (u_int)tid, type, blob, size);
166 1.1 rmind if (t == NULL) {
167 1.12 rmind NPF_ERR_DEBUG(errdict);
168 1.1 rmind error = ENOMEM;
169 1.1 rmind break;
170 1.1 rmind }
171 1.1 rmind error = npf_tableset_insert(tblset, t);
172 1.1 rmind KASSERT(error == 0);
173 1.1 rmind
174 1.17.2.5 jdolecek if (ents && (error = npf_mk_table_entries(t, ents)) != 0) {
175 1.12 rmind NPF_ERR_DEBUG(errdict);
176 1.1 rmind break;
177 1.1 rmind }
178 1.1 rmind }
179 1.1 rmind prop_object_iterator_release(it);
180 1.1 rmind /*
181 1.4 rmind * Note: in a case of error, caller will free the tableset.
182 1.1 rmind */
183 1.1 rmind return error;
184 1.1 rmind }
185 1.1 rmind
186 1.5 rmind static npf_rproc_t *
187 1.17.2.5 jdolecek npf_mk_singlerproc(npf_t *npf, prop_dictionary_t rpdict)
188 1.5 rmind {
189 1.5 rmind prop_object_iterator_t it;
190 1.17.2.2 tls prop_dictionary_t extdict;
191 1.17.2.1 tls prop_array_t extlist;
192 1.5 rmind npf_rproc_t *rp;
193 1.17.2.1 tls
194 1.17.2.1 tls extlist = prop_dictionary_get(rpdict, "extcalls");
195 1.17.2.1 tls if (prop_object_type(extlist) != PROP_TYPE_ARRAY) {
196 1.17.2.1 tls return NULL;
197 1.17.2.1 tls }
198 1.17.2.1 tls
199 1.17.2.1 tls rp = npf_rproc_create(rpdict);
200 1.17.2.2 tls if (rp == NULL) {
201 1.17.2.1 tls return NULL;
202 1.17.2.1 tls }
203 1.17.2.2 tls
204 1.17.2.1 tls it = prop_array_iterator(extlist);
205 1.17.2.1 tls while ((extdict = prop_object_iterator_next(it)) != NULL) {
206 1.17.2.2 tls const char *name;
207 1.17.2.2 tls
208 1.17.2.5 jdolecek if (!prop_dictionary_get_cstring_nocopy(extdict, "name",
209 1.17.2.5 jdolecek &name) || npf_ext_construct(npf, name, rp, extdict)) {
210 1.17.2.1 tls npf_rproc_release(rp);
211 1.17.2.1 tls rp = NULL;
212 1.17.2.1 tls break;
213 1.17.2.1 tls }
214 1.17.2.1 tls }
215 1.17.2.1 tls prop_object_iterator_release(it);
216 1.5 rmind return rp;
217 1.5 rmind }
218 1.5 rmind
219 1.6 rmind static int __noinline
220 1.17.2.5 jdolecek npf_mk_rprocs(npf_t *npf, npf_rprocset_t *rpset, prop_array_t rprocs,
221 1.12 rmind prop_dictionary_t errdict)
222 1.12 rmind {
223 1.17.2.2 tls prop_object_iterator_t it;
224 1.17.2.2 tls prop_dictionary_t rpdict;
225 1.17.2.2 tls int error = 0;
226 1.12 rmind
227 1.17.2.2 tls it = prop_array_iterator(rprocs);
228 1.17.2.2 tls while ((rpdict = prop_object_iterator_next(it)) != NULL) {
229 1.17.2.2 tls npf_rproc_t *rp;
230 1.17.2.2 tls
231 1.17.2.5 jdolecek if ((rp = npf_mk_singlerproc(npf, rpdict)) == NULL) {
232 1.17.2.2 tls NPF_ERR_DEBUG(errdict);
233 1.17.2.2 tls error = EINVAL;
234 1.17.2.2 tls break;
235 1.17.2.2 tls }
236 1.17.2.2 tls npf_rprocset_insert(rpset, rp);
237 1.12 rmind }
238 1.17.2.2 tls prop_object_iterator_release(it);
239 1.17.2.2 tls return error;
240 1.17.2.2 tls }
241 1.17.2.2 tls
242 1.17.2.3 tls static npf_alg_t *
243 1.17.2.5 jdolecek npf_mk_singlealg(npf_t *npf, prop_dictionary_t aldict)
244 1.17.2.3 tls {
245 1.17.2.3 tls const char *name;
246 1.17.2.3 tls
247 1.17.2.3 tls if (!prop_dictionary_get_cstring_nocopy(aldict, "name", &name))
248 1.17.2.3 tls return NULL;
249 1.17.2.5 jdolecek return npf_alg_construct(npf, name);
250 1.17.2.3 tls }
251 1.17.2.3 tls
252 1.17.2.3 tls static int __noinline
253 1.17.2.5 jdolecek npf_mk_algs(npf_t *npf, prop_array_t alglist, prop_dictionary_t errdict)
254 1.17.2.3 tls {
255 1.17.2.3 tls prop_object_iterator_t it;
256 1.17.2.3 tls prop_dictionary_t nadict;
257 1.17.2.3 tls int error = 0;
258 1.17.2.3 tls
259 1.17.2.3 tls it = prop_array_iterator(alglist);
260 1.17.2.3 tls while ((nadict = prop_object_iterator_next(it)) != NULL) {
261 1.17.2.5 jdolecek if (npf_mk_singlealg(npf, nadict) == NULL) {
262 1.17.2.3 tls NPF_ERR_DEBUG(errdict);
263 1.17.2.3 tls error = EINVAL;
264 1.17.2.3 tls break;
265 1.17.2.3 tls }
266 1.17.2.3 tls }
267 1.17.2.3 tls prop_object_iterator_release(it);
268 1.17.2.3 tls return error;
269 1.17.2.3 tls }
270 1.17.2.3 tls
271 1.17.2.2 tls static int __noinline
272 1.17.2.2 tls npf_mk_code(prop_object_t obj, int type, void **code, size_t *csize,
273 1.17.2.2 tls prop_dictionary_t errdict)
274 1.17.2.2 tls {
275 1.17.2.2 tls const void *cptr;
276 1.17.2.2 tls size_t clen;
277 1.17.2.2 tls void *bc;
278 1.17.2.2 tls
279 1.17.2.4 tls if (type != NPF_CODE_BPF) {
280 1.17.2.4 tls return ENOTSUP;
281 1.17.2.4 tls }
282 1.17.2.2 tls cptr = prop_data_data_nocopy(obj);
283 1.17.2.2 tls if (cptr == NULL || (clen = prop_data_size(obj)) == 0) {
284 1.12 rmind NPF_ERR_DEBUG(errdict);
285 1.12 rmind return EINVAL;
286 1.12 rmind }
287 1.17.2.4 tls if (!npf_bpf_validate(cptr, clen)) {
288 1.17.2.4 tls NPF_ERR_DEBUG(errdict);
289 1.17.2.4 tls return EINVAL;
290 1.17.2.2 tls }
291 1.17.2.2 tls bc = kmem_alloc(clen, KM_SLEEP);
292 1.17.2.2 tls memcpy(bc, cptr, clen);
293 1.17.2.2 tls
294 1.17.2.2 tls *code = bc;
295 1.17.2.2 tls *csize = clen;
296 1.12 rmind return 0;
297 1.12 rmind }
298 1.12 rmind
299 1.12 rmind static int __noinline
300 1.17.2.5 jdolecek npf_mk_singlerule(npf_t *npf, prop_dictionary_t rldict, npf_rprocset_t *rpset,
301 1.17.2.2 tls npf_rule_t **rlret, prop_dictionary_t errdict)
302 1.1 rmind {
303 1.17.2.2 tls npf_rule_t *rl;
304 1.17.2.2 tls const char *rname;
305 1.1 rmind prop_object_t obj;
306 1.17.2.2 tls int p, error = 0;
307 1.1 rmind
308 1.1 rmind /* Rule - dictionary. */
309 1.12 rmind if (prop_object_type(rldict) != PROP_TYPE_DICTIONARY) {
310 1.12 rmind NPF_ERR_DEBUG(errdict);
311 1.1 rmind return EINVAL;
312 1.12 rmind }
313 1.17.2.5 jdolecek if ((rl = npf_rule_alloc(npf, rldict)) == NULL) {
314 1.17.2.2 tls NPF_ERR_DEBUG(errdict);
315 1.17.2.2 tls return EINVAL;
316 1.17.2.2 tls }
317 1.17.2.2 tls
318 1.17.2.2 tls /* Assign rule procedure, if any. */
319 1.17.2.2 tls if (prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rname)) {
320 1.17.2.2 tls npf_rproc_t *rp;
321 1.1 rmind
322 1.17.2.2 tls if (rpset == NULL) {
323 1.17.2.2 tls error = EINVAL;
324 1.17.2.2 tls goto err;
325 1.17.2.2 tls }
326 1.17.2.2 tls if ((rp = npf_rprocset_lookup(rpset, rname)) == NULL) {
327 1.17.2.1 tls NPF_ERR_DEBUG(errdict);
328 1.17.2.1 tls error = EINVAL;
329 1.17.2.1 tls goto err;
330 1.17.2.1 tls }
331 1.17.2.2 tls npf_rule_setrproc(rl, rp);
332 1.17.2.1 tls }
333 1.17.2.1 tls
334 1.17.2.2 tls /* Filter code (binary data). */
335 1.17.2.2 tls if ((obj = prop_dictionary_get(rldict, "code")) != NULL) {
336 1.17.2.2 tls int type;
337 1.17.2.2 tls size_t len;
338 1.17.2.2 tls void *code;
339 1.17.2.2 tls
340 1.17.2.2 tls prop_dictionary_get_int32(rldict, "code-type", &type);
341 1.17.2.2 tls error = npf_mk_code(obj, type, &code, &len, errdict);
342 1.12 rmind if (error) {
343 1.12 rmind goto err;
344 1.4 rmind }
345 1.17.2.2 tls npf_rule_setcode(rl, type, code, len);
346 1.1 rmind }
347 1.1 rmind
348 1.17.2.2 tls *rlret = rl;
349 1.6 rmind return 0;
350 1.12 rmind err:
351 1.17.2.2 tls npf_rule_free(rl);
352 1.17.2.4 tls prop_dictionary_get_int32(rldict, "prio", &p); /* XXX */
353 1.17.2.5 jdolecek prop_dictionary_set_int64(errdict, "id", p);
354 1.12 rmind return error;
355 1.6 rmind }
356 1.6 rmind
357 1.6 rmind static int __noinline
358 1.17.2.5 jdolecek npf_mk_rules(npf_t *npf, npf_ruleset_t *rlset, prop_array_t rules,
359 1.17.2.5 jdolecek npf_rprocset_t *rpset, prop_dictionary_t errdict)
360 1.6 rmind {
361 1.6 rmind prop_object_iterator_t it;
362 1.6 rmind prop_dictionary_t rldict;
363 1.1 rmind int error;
364 1.1 rmind
365 1.17.2.2 tls if (prop_object_type(rules) != PROP_TYPE_ARRAY) {
366 1.12 rmind NPF_ERR_DEBUG(errdict);
367 1.1 rmind return EINVAL;
368 1.12 rmind }
369 1.1 rmind
370 1.4 rmind error = 0;
371 1.1 rmind it = prop_array_iterator(rules);
372 1.1 rmind while ((rldict = prop_object_iterator_next(it)) != NULL) {
373 1.17.2.2 tls npf_rule_t *rl = NULL;
374 1.1 rmind
375 1.1 rmind /* Generate a single rule. */
376 1.17.2.5 jdolecek error = npf_mk_singlerule(npf, rldict, rpset, &rl, errdict);
377 1.6 rmind if (error) {
378 1.1 rmind break;
379 1.6 rmind }
380 1.6 rmind npf_ruleset_insert(rlset, rl);
381 1.1 rmind }
382 1.1 rmind prop_object_iterator_release(it);
383 1.1 rmind /*
384 1.4 rmind * Note: in a case of error, caller will free the ruleset.
385 1.1 rmind */
386 1.1 rmind return error;
387 1.1 rmind }
388 1.1 rmind
389 1.6 rmind static int __noinline
390 1.17.2.5 jdolecek npf_mk_natlist(npf_t *npf, npf_ruleset_t *nset, prop_array_t natlist,
391 1.12 rmind prop_dictionary_t errdict)
392 1.1 rmind {
393 1.1 rmind prop_object_iterator_t it;
394 1.1 rmind prop_dictionary_t natdict;
395 1.1 rmind int error;
396 1.1 rmind
397 1.1 rmind /* NAT policies - array. */
398 1.12 rmind if (prop_object_type(natlist) != PROP_TYPE_ARRAY) {
399 1.12 rmind NPF_ERR_DEBUG(errdict);
400 1.1 rmind return EINVAL;
401 1.12 rmind }
402 1.1 rmind
403 1.4 rmind error = 0;
404 1.1 rmind it = prop_array_iterator(natlist);
405 1.1 rmind while ((natdict = prop_object_iterator_next(it)) != NULL) {
406 1.17.2.2 tls npf_rule_t *rl = NULL;
407 1.1 rmind npf_natpolicy_t *np;
408 1.1 rmind
409 1.1 rmind /* NAT policy - dictionary. */
410 1.1 rmind if (prop_object_type(natdict) != PROP_TYPE_DICTIONARY) {
411 1.12 rmind NPF_ERR_DEBUG(errdict);
412 1.1 rmind error = EINVAL;
413 1.1 rmind break;
414 1.1 rmind }
415 1.1 rmind
416 1.1 rmind /*
417 1.1 rmind * NAT policies are standard rules, plus additional
418 1.1 rmind * information for translation. Make a rule.
419 1.1 rmind */
420 1.17.2.5 jdolecek error = npf_mk_singlerule(npf, natdict, NULL, &rl, errdict);
421 1.6 rmind if (error) {
422 1.1 rmind break;
423 1.6 rmind }
424 1.6 rmind npf_ruleset_insert(nset, rl);
425 1.6 rmind
426 1.6 rmind /* If rule is named, it is a group with NAT policies. */
427 1.6 rmind if (prop_dictionary_get(natdict, "name") &&
428 1.6 rmind prop_dictionary_get(natdict, "subrules")) {
429 1.6 rmind continue;
430 1.6 rmind }
431 1.1 rmind
432 1.1 rmind /* Allocate a new NAT policy and assign to the rule. */
433 1.17.2.5 jdolecek np = npf_nat_newpolicy(npf, natdict, nset);
434 1.1 rmind if (np == NULL) {
435 1.12 rmind NPF_ERR_DEBUG(errdict);
436 1.1 rmind error = ENOMEM;
437 1.1 rmind break;
438 1.1 rmind }
439 1.1 rmind npf_rule_setnat(rl, np);
440 1.1 rmind }
441 1.1 rmind prop_object_iterator_release(it);
442 1.1 rmind /*
443 1.1 rmind * Note: in a case of error, caller will free entire NAT ruleset
444 1.1 rmind * with assigned NAT policies.
445 1.1 rmind */
446 1.1 rmind return error;
447 1.1 rmind }
448 1.1 rmind
449 1.1 rmind /*
450 1.17.2.4 tls * npf_mk_connlist: import a list of connections and load them.
451 1.17.2.4 tls */
452 1.17.2.4 tls static int __noinline
453 1.17.2.5 jdolecek npf_mk_connlist(npf_t *npf, prop_array_t conlist, npf_ruleset_t *natlist,
454 1.17.2.4 tls npf_conndb_t **conndb, prop_dictionary_t errdict)
455 1.17.2.4 tls {
456 1.17.2.4 tls prop_dictionary_t condict;
457 1.17.2.4 tls prop_object_iterator_t it;
458 1.17.2.4 tls npf_conndb_t *cd;
459 1.17.2.5 jdolecek int error = 0;
460 1.17.2.4 tls
461 1.17.2.4 tls /* Connection list - array */
462 1.17.2.4 tls if (prop_object_type(conlist) != PROP_TYPE_ARRAY) {
463 1.17.2.4 tls NPF_ERR_DEBUG(errdict);
464 1.17.2.4 tls return EINVAL;
465 1.17.2.4 tls }
466 1.17.2.4 tls
467 1.17.2.4 tls /* Create a connection database. */
468 1.17.2.4 tls cd = npf_conndb_create();
469 1.17.2.4 tls it = prop_array_iterator(conlist);
470 1.17.2.4 tls while ((condict = prop_object_iterator_next(it)) != NULL) {
471 1.17.2.4 tls /* Connection - dictionary. */
472 1.17.2.4 tls if (prop_object_type(condict) != PROP_TYPE_DICTIONARY) {
473 1.17.2.4 tls NPF_ERR_DEBUG(errdict);
474 1.17.2.4 tls error = EINVAL;
475 1.17.2.4 tls break;
476 1.17.2.4 tls }
477 1.17.2.5 jdolecek /* Construct and insert the connection. */
478 1.17.2.5 jdolecek error = npf_conn_import(npf, cd, condict, natlist);
479 1.17.2.4 tls if (error) {
480 1.17.2.4 tls NPF_ERR_DEBUG(errdict);
481 1.17.2.4 tls break;
482 1.17.2.4 tls }
483 1.17.2.4 tls }
484 1.17.2.4 tls prop_object_iterator_release(it);
485 1.17.2.4 tls if (error) {
486 1.17.2.5 jdolecek npf_conn_gc(npf, cd, true, false);
487 1.17.2.4 tls npf_conndb_destroy(cd);
488 1.17.2.4 tls } else {
489 1.17.2.4 tls *conndb = cd;
490 1.17.2.4 tls }
491 1.17.2.4 tls return error;
492 1.17.2.4 tls }
493 1.17.2.4 tls
494 1.17.2.5 jdolecek #if defined(_NPF_TESTING) || defined(_NPF_STANDALONE)
495 1.17.2.5 jdolecek int npfctl_testing;
496 1.17.2.5 jdolecek #endif
497 1.17.2.5 jdolecek
498 1.17.2.4 tls /*
499 1.17.2.4 tls * npfctl_load: store passed data i.e. update settings, create passed
500 1.1 rmind * tables, rules and atomically activate all them.
501 1.1 rmind */
502 1.1 rmind int
503 1.17.2.5 jdolecek npfctl_load(npf_t *npf, u_long cmd, void *data)
504 1.1 rmind {
505 1.12 rmind struct plistref *pref = data;
506 1.14 rmind prop_dictionary_t npf_dict, errdict;
507 1.17.2.4 tls prop_array_t alglist, natlist, tables, rprocs, rules, conlist;
508 1.1 rmind npf_tableset_t *tblset = NULL;
509 1.17.2.2 tls npf_rprocset_t *rpset = NULL;
510 1.1 rmind npf_ruleset_t *rlset = NULL;
511 1.1 rmind npf_ruleset_t *nset = NULL;
512 1.17.2.4 tls npf_conndb_t *conndb = NULL;
513 1.17.2.2 tls uint32_t ver = 0;
514 1.17.2.2 tls size_t nitems;
515 1.11 rmind bool flush;
516 1.1 rmind int error;
517 1.1 rmind
518 1.1 rmind /* Retrieve the dictionary. */
519 1.17.2.5 jdolecek #if defined(_NPF_TESTING) || defined(_NPF_STANDALONE)
520 1.17.2.5 jdolecek if (npfctl_testing)
521 1.17.2.5 jdolecek npf_dict = (prop_dictionary_t)pref;
522 1.17.2.5 jdolecek else
523 1.1 rmind #endif
524 1.17.2.5 jdolecek {
525 1.17.2.5 jdolecek error = prop_dictionary_copyin_ioctl_size(pref, cmd, &npf_dict,
526 1.17.2.5 jdolecek 4 * 1024 * 1024);
527 1.17.2.5 jdolecek if (error)
528 1.17.2.5 jdolecek return error;
529 1.17.2.5 jdolecek }
530 1.15 rmind
531 1.17.2.2 tls /* Dictionary for error reporting and version check. */
532 1.12 rmind errdict = prop_dictionary_create();
533 1.17.2.2 tls prop_dictionary_get_uint32(npf_dict, "version", &ver);
534 1.17.2.2 tls if (ver != NPF_VERSION) {
535 1.17.2.2 tls error = EPROGMISMATCH;
536 1.17.2.2 tls goto fail;
537 1.17.2.2 tls }
538 1.12 rmind
539 1.17.2.3 tls /* ALGs. */
540 1.17.2.3 tls alglist = prop_dictionary_get(npf_dict, "algs");
541 1.17.2.5 jdolecek error = npf_mk_algs(npf, alglist, errdict);
542 1.17.2.3 tls if (error) {
543 1.17.2.3 tls goto fail;
544 1.17.2.3 tls }
545 1.17.2.3 tls
546 1.1 rmind /* NAT policies. */
547 1.17.2.4 tls natlist = prop_dictionary_get(npf_dict, "nat");
548 1.17.2.4 tls if ((nitems = prop_array_count(natlist)) > NPF_MAX_RULES) {
549 1.17.2.5 jdolecek error = E2BIG;
550 1.17.2.4 tls goto fail;
551 1.17.2.4 tls }
552 1.17.2.2 tls
553 1.17.2.2 tls nset = npf_ruleset_create(nitems);
554 1.17.2.5 jdolecek error = npf_mk_natlist(npf, nset, natlist, errdict);
555 1.10 rmind if (error) {
556 1.1 rmind goto fail;
557 1.10 rmind }
558 1.1 rmind
559 1.1 rmind /* Tables. */
560 1.14 rmind tables = prop_dictionary_get(npf_dict, "tables");
561 1.17.2.4 tls if ((nitems = prop_array_count(tables)) > NPF_MAX_TABLES) {
562 1.17.2.5 jdolecek error = E2BIG;
563 1.17.2.4 tls goto fail;
564 1.17.2.4 tls }
565 1.17.2.4 tls tblset = npf_tableset_create(nitems);
566 1.17.2.5 jdolecek error = npf_mk_tables(npf, tblset, tables, errdict);
567 1.10 rmind if (error) {
568 1.1 rmind goto fail;
569 1.10 rmind }
570 1.1 rmind
571 1.17.2.2 tls /* Rule procedures. */
572 1.14 rmind rprocs = prop_dictionary_get(npf_dict, "rprocs");
573 1.17.2.4 tls if ((nitems = prop_array_count(rprocs)) > NPF_MAX_RPROCS) {
574 1.17.2.5 jdolecek error = E2BIG;
575 1.17.2.4 tls goto fail;
576 1.17.2.4 tls }
577 1.17.2.4 tls rpset = npf_rprocset_create();
578 1.17.2.5 jdolecek error = npf_mk_rprocs(npf, rpset, rprocs, errdict);
579 1.17.2.2 tls if (error) {
580 1.17.2.2 tls goto fail;
581 1.17.2.2 tls }
582 1.17.2.2 tls
583 1.17.2.2 tls /* Rules. */
584 1.14 rmind rules = prop_dictionary_get(npf_dict, "rules");
585 1.17.2.4 tls if ((nitems = prop_array_count(rules)) > NPF_MAX_RULES) {
586 1.17.2.5 jdolecek error = E2BIG;
587 1.17.2.4 tls goto fail;
588 1.17.2.4 tls }
589 1.17.2.2 tls
590 1.17.2.2 tls rlset = npf_ruleset_create(nitems);
591 1.17.2.5 jdolecek error = npf_mk_rules(npf, rlset, rules, rpset, errdict);
592 1.10 rmind if (error) {
593 1.1 rmind goto fail;
594 1.10 rmind }
595 1.1 rmind
596 1.17.2.4 tls /* Connections (if loading any). */
597 1.17.2.4 tls if ((conlist = prop_dictionary_get(npf_dict, "conn-list")) != NULL) {
598 1.17.2.5 jdolecek error = npf_mk_connlist(npf, conlist, nset, &conndb, errdict);
599 1.17.2.4 tls if (error) {
600 1.17.2.4 tls goto fail;
601 1.17.2.4 tls }
602 1.17.2.4 tls }
603 1.17.2.4 tls
604 1.11 rmind flush = false;
605 1.14 rmind prop_dictionary_get_bool(npf_dict, "flush", &flush);
606 1.11 rmind
607 1.1 rmind /*
608 1.17.2.4 tls * Finally - perform the load.
609 1.1 rmind */
610 1.17.2.5 jdolecek npf_config_load(npf, rlset, tblset, nset, rpset, conndb, flush);
611 1.11 rmind
612 1.1 rmind /* Done. Since data is consumed now, we shall not destroy it. */
613 1.1 rmind tblset = NULL;
614 1.17.2.2 tls rpset = NULL;
615 1.1 rmind rlset = NULL;
616 1.1 rmind nset = NULL;
617 1.1 rmind fail:
618 1.1 rmind /*
619 1.1 rmind * Note: destroy rulesets first, to drop references to the tableset.
620 1.1 rmind */
621 1.1 rmind if (nset) {
622 1.1 rmind npf_ruleset_destroy(nset);
623 1.1 rmind }
624 1.1 rmind if (rlset) {
625 1.1 rmind npf_ruleset_destroy(rlset);
626 1.1 rmind }
627 1.17.2.2 tls if (rpset) {
628 1.17.2.2 tls npf_rprocset_destroy(rpset);
629 1.17.2.2 tls }
630 1.1 rmind if (tblset) {
631 1.1 rmind npf_tableset_destroy(tblset);
632 1.1 rmind }
633 1.17.2.5 jdolecek #if defined(_NPF_TESTING) || defined(_NPF_STANDALONE)
634 1.17.2.5 jdolecek /* Free only if allocated by prop_dictionary_copyin_ioctl_size. */
635 1.17.2.5 jdolecek if (!npfctl_testing)
636 1.17.2.5 jdolecek #endif
637 1.17.2.5 jdolecek prop_object_release(npf_dict);
638 1.12 rmind
639 1.17.2.5 jdolecek /*
640 1.17.2.5 jdolecek * - _NPF_STANDALONE doesn't require to set prop.
641 1.17.2.5 jdolecek * - For _NPF_TESTING, if npfctl_testing, setting prop isn't needed,
642 1.17.2.5 jdolecek * otherwise it's needed.
643 1.17.2.5 jdolecek */
644 1.17.2.5 jdolecek #ifndef _NPF_STANDALONE
645 1.17.2.5 jdolecek #ifdef _NPF_TESTING
646 1.17.2.5 jdolecek if (!npfctl_testing) {
647 1.17.2.5 jdolecek #endif
648 1.17.2.5 jdolecek /* Error report. */
649 1.17.2.5 jdolecek prop_dictionary_set_int32(errdict, "errno", error);
650 1.17.2.5 jdolecek prop_dictionary_copyout_ioctl(pref, cmd, errdict);
651 1.17.2.5 jdolecek error = 0;
652 1.17.2.5 jdolecek #ifdef _NPF_TESTING
653 1.17.2.5 jdolecek }
654 1.17.2.1 tls #endif
655 1.17.2.5 jdolecek #endif /* _NPF_STANDALONE */
656 1.17.2.5 jdolecek prop_object_release(errdict);
657 1.17.2.5 jdolecek
658 1.17.2.1 tls return error;
659 1.6 rmind }
660 1.6 rmind
661 1.6 rmind /*
662 1.17.2.4 tls * npfctl_save: export the config dictionary as it was submitted,
663 1.17.2.4 tls * including the current snapshot of the connections. Additionally,
664 1.17.2.4 tls * indicate whether the ruleset is currently active.
665 1.17.2.4 tls */
666 1.17.2.4 tls int
667 1.17.2.5 jdolecek npfctl_save(npf_t *npf, u_long cmd, void *data)
668 1.17.2.4 tls {
669 1.17.2.4 tls struct plistref *pref = data;
670 1.17.2.4 tls prop_array_t rulelist, natlist, tables, rprocs, conlist;
671 1.17.2.4 tls prop_dictionary_t npf_dict = NULL;
672 1.17.2.4 tls int error;
673 1.17.2.4 tls
674 1.17.2.4 tls rulelist = prop_array_create();
675 1.17.2.4 tls natlist = prop_array_create();
676 1.17.2.4 tls tables = prop_array_create();
677 1.17.2.4 tls rprocs = prop_array_create();
678 1.17.2.4 tls conlist = prop_array_create();
679 1.17.2.4 tls
680 1.17.2.4 tls /*
681 1.17.2.4 tls * Serialise the connections and NAT policies.
682 1.17.2.4 tls */
683 1.17.2.5 jdolecek npf_config_enter(npf);
684 1.17.2.5 jdolecek error = npf_conndb_export(npf, conlist);
685 1.17.2.4 tls if (error) {
686 1.17.2.4 tls goto out;
687 1.17.2.4 tls }
688 1.17.2.5 jdolecek error = npf_ruleset_export(npf, npf_config_ruleset(npf), rulelist);
689 1.17.2.4 tls if (error) {
690 1.17.2.4 tls goto out;
691 1.17.2.4 tls }
692 1.17.2.5 jdolecek error = npf_ruleset_export(npf, npf_config_natset(npf), natlist);
693 1.17.2.4 tls if (error) {
694 1.17.2.4 tls goto out;
695 1.17.2.4 tls }
696 1.17.2.5 jdolecek error = npf_tableset_export(npf, npf_config_tableset(npf), tables);
697 1.17.2.4 tls if (error) {
698 1.17.2.4 tls goto out;
699 1.17.2.4 tls }
700 1.17.2.5 jdolecek error = npf_rprocset_export(npf_config_rprocs(npf), rprocs);
701 1.17.2.4 tls if (error) {
702 1.17.2.4 tls goto out;
703 1.17.2.4 tls }
704 1.17.2.5 jdolecek prop_array_t alglist = npf_alg_export(npf);
705 1.17.2.5 jdolecek
706 1.17.2.4 tls npf_dict = prop_dictionary_create();
707 1.17.2.4 tls prop_dictionary_set_uint32(npf_dict, "version", NPF_VERSION);
708 1.17.2.5 jdolecek prop_dictionary_set_and_rel(npf_dict, "algs", alglist);
709 1.17.2.4 tls prop_dictionary_set_and_rel(npf_dict, "rules", rulelist);
710 1.17.2.4 tls prop_dictionary_set_and_rel(npf_dict, "nat", natlist);
711 1.17.2.4 tls prop_dictionary_set_and_rel(npf_dict, "tables", tables);
712 1.17.2.4 tls prop_dictionary_set_and_rel(npf_dict, "rprocs", rprocs);
713 1.17.2.4 tls prop_dictionary_set_and_rel(npf_dict, "conn-list", conlist);
714 1.17.2.5 jdolecek #if !defined(_NPF_STANDALONE)
715 1.17.2.4 tls prop_dictionary_set_bool(npf_dict, "active", npf_pfil_registered_p());
716 1.17.2.4 tls error = prop_dictionary_copyout_ioctl(pref, cmd, npf_dict);
717 1.17.2.5 jdolecek #else
718 1.17.2.5 jdolecek prop_dictionary_set_bool(npf_dict, "active", true);
719 1.17.2.5 jdolecek /* Userspace: just copy the pointer of the dictionary. */
720 1.17.2.5 jdolecek CTASSERT(sizeof(prop_dictionary_t) == sizeof(void *));
721 1.17.2.5 jdolecek memcpy(data, npf_dict, sizeof(void *));
722 1.17.2.5 jdolecek #endif
723 1.17.2.4 tls out:
724 1.17.2.5 jdolecek npf_config_exit(npf);
725 1.17.2.4 tls
726 1.17.2.4 tls if (!npf_dict) {
727 1.17.2.4 tls prop_object_release(rulelist);
728 1.17.2.4 tls prop_object_release(natlist);
729 1.17.2.4 tls prop_object_release(tables);
730 1.17.2.4 tls prop_object_release(rprocs);
731 1.17.2.4 tls prop_object_release(conlist);
732 1.17.2.4 tls } else {
733 1.17.2.5 jdolecek #if !defined(_NPF_STANDALONE)
734 1.17.2.4 tls prop_object_release(npf_dict);
735 1.17.2.5 jdolecek #endif
736 1.17.2.5 jdolecek }
737 1.17.2.5 jdolecek return error;
738 1.17.2.5 jdolecek }
739 1.17.2.5 jdolecek
740 1.17.2.5 jdolecek /*
741 1.17.2.5 jdolecek * npfctl_conn_lookup: lookup a connection in the list of connections
742 1.17.2.5 jdolecek */
743 1.17.2.5 jdolecek int
744 1.17.2.5 jdolecek npfctl_conn_lookup(npf_t *npf, u_long cmd, void *data)
745 1.17.2.5 jdolecek {
746 1.17.2.5 jdolecek struct plistref *pref = data;
747 1.17.2.5 jdolecek prop_dictionary_t conn_data, conn_result;
748 1.17.2.5 jdolecek int error;
749 1.17.2.5 jdolecek
750 1.17.2.5 jdolecek #if !defined(_NPF_STANDALONE)
751 1.17.2.5 jdolecek error = prop_dictionary_copyin_ioctl(pref, cmd, &conn_data);
752 1.17.2.5 jdolecek if (error) {
753 1.17.2.5 jdolecek return error;
754 1.17.2.5 jdolecek }
755 1.17.2.5 jdolecek #else
756 1.17.2.5 jdolecek conn_data = (prop_dictionary_t)pref;
757 1.17.2.5 jdolecek #endif
758 1.17.2.5 jdolecek error = npf_conn_find(npf, conn_data, &conn_result);
759 1.17.2.5 jdolecek if (error) {
760 1.17.2.5 jdolecek goto out;
761 1.17.2.4 tls }
762 1.17.2.5 jdolecek #if !defined(_NPF_STANDALONE)
763 1.17.2.5 jdolecek prop_dictionary_copyout_ioctl(pref, cmd, conn_result);
764 1.17.2.5 jdolecek prop_object_release(conn_result);
765 1.17.2.5 jdolecek #else
766 1.17.2.5 jdolecek CTASSERT(sizeof(prop_dictionary_t) == sizeof(void *));
767 1.17.2.5 jdolecek memcpy(data, conn_result, sizeof(void *));
768 1.17.2.5 jdolecek #endif
769 1.17.2.5 jdolecek out:
770 1.17.2.5 jdolecek prop_object_release(conn_data);
771 1.17.2.4 tls return error;
772 1.17.2.4 tls }
773 1.17.2.4 tls
774 1.17.2.4 tls /*
775 1.17.2.2 tls * npfctl_rule: add or remove dynamic rules in the specified ruleset.
776 1.6 rmind */
777 1.6 rmind int
778 1.17.2.5 jdolecek npfctl_rule(npf_t *npf, u_long cmd, void *data)
779 1.6 rmind {
780 1.12 rmind struct plistref *pref = data;
781 1.17.2.2 tls prop_dictionary_t npf_rule, retdict = NULL;
782 1.6 rmind npf_ruleset_t *rlset;
783 1.17.2.2 tls npf_rule_t *rl = NULL;
784 1.17.2.2 tls const char *ruleset_name;
785 1.17.2.2 tls uint32_t rcmd = 0;
786 1.17.2.5 jdolecek int error = 0;
787 1.6 rmind
788 1.17.2.5 jdolecek #if !defined(_NPF_STANDALONE)
789 1.17.2.2 tls error = prop_dictionary_copyin_ioctl(pref, cmd, &npf_rule);
790 1.6 rmind if (error) {
791 1.6 rmind return error;
792 1.6 rmind }
793 1.17.2.5 jdolecek #else
794 1.17.2.5 jdolecek npf_rule = (prop_dictionary_t)pref;
795 1.17.2.5 jdolecek #endif
796 1.17.2.2 tls prop_dictionary_get_uint32(npf_rule, "command", &rcmd);
797 1.17.2.2 tls if (!prop_dictionary_get_cstring_nocopy(npf_rule,
798 1.17.2.2 tls "ruleset-name", &ruleset_name)) {
799 1.17.2.4 tls error = EINVAL;
800 1.17.2.4 tls goto out;
801 1.17.2.2 tls }
802 1.12 rmind
803 1.17.2.2 tls if (rcmd == NPF_CMD_RULE_ADD) {
804 1.17.2.2 tls retdict = prop_dictionary_create();
805 1.17.2.5 jdolecek if (npf_mk_singlerule(npf, npf_rule, NULL, &rl, retdict) != 0) {
806 1.17.2.4 tls error = EINVAL;
807 1.17.2.4 tls goto out;
808 1.17.2.4 tls }
809 1.17.2.2 tls }
810 1.12 rmind
811 1.17.2.5 jdolecek npf_config_enter(npf);
812 1.17.2.5 jdolecek rlset = npf_config_ruleset(npf);
813 1.17.2.2 tls
814 1.17.2.2 tls switch (rcmd) {
815 1.17.2.2 tls case NPF_CMD_RULE_ADD: {
816 1.17.2.2 tls if ((error = npf_ruleset_add(rlset, ruleset_name, rl)) == 0) {
817 1.17.2.2 tls /* Success. */
818 1.17.2.2 tls uint64_t id = npf_rule_getid(rl);
819 1.17.2.2 tls prop_dictionary_set_uint64(retdict, "id", id);
820 1.17.2.2 tls rl = NULL;
821 1.17.2.2 tls }
822 1.17.2.2 tls break;
823 1.6 rmind }
824 1.17.2.2 tls case NPF_CMD_RULE_REMOVE: {
825 1.17.2.2 tls uint64_t id;
826 1.6 rmind
827 1.17.2.2 tls if (!prop_dictionary_get_uint64(npf_rule, "id", &id)) {
828 1.17.2.2 tls error = EINVAL;
829 1.17.2.2 tls break;
830 1.17.2.2 tls }
831 1.17.2.2 tls error = npf_ruleset_remove(rlset, ruleset_name, id);
832 1.17.2.2 tls break;
833 1.6 rmind }
834 1.17.2.2 tls case NPF_CMD_RULE_REMKEY: {
835 1.17.2.2 tls prop_object_t obj = prop_dictionary_get(npf_rule, "key");
836 1.17.2.2 tls const void *key = prop_data_data_nocopy(obj);
837 1.17.2.2 tls size_t len = prop_data_size(obj);
838 1.17.2.2 tls
839 1.17.2.2 tls if (len == 0 || len > NPF_RULE_MAXKEYLEN) {
840 1.17.2.2 tls error = EINVAL;
841 1.17.2.2 tls break;
842 1.17.2.2 tls }
843 1.17.2.2 tls error = npf_ruleset_remkey(rlset, ruleset_name, key, len);
844 1.17.2.2 tls break;
845 1.17.2.2 tls }
846 1.17.2.2 tls case NPF_CMD_RULE_LIST: {
847 1.17.2.5 jdolecek retdict = npf_ruleset_list(npf, rlset, ruleset_name);
848 1.17.2.5 jdolecek if (!retdict) {
849 1.17.2.5 jdolecek error = ESRCH;
850 1.17.2.5 jdolecek }
851 1.17.2.2 tls break;
852 1.17.2.2 tls }
853 1.17.2.2 tls case NPF_CMD_RULE_FLUSH: {
854 1.17.2.2 tls error = npf_ruleset_flush(rlset, ruleset_name);
855 1.17.2.2 tls break;
856 1.17.2.2 tls }
857 1.17.2.2 tls default:
858 1.17.2.2 tls error = EINVAL;
859 1.17.2.2 tls break;
860 1.17.2.2 tls }
861 1.17.2.2 tls
862 1.17.2.2 tls /* Destroy any removed rules. */
863 1.17.2.2 tls if (!error && rcmd != NPF_CMD_RULE_ADD && rcmd != NPF_CMD_RULE_LIST) {
864 1.17.2.5 jdolecek npf_config_sync(npf);
865 1.17.2.2 tls npf_ruleset_gc(rlset);
866 1.17.2.2 tls }
867 1.17.2.5 jdolecek npf_config_exit(npf);
868 1.17.2.2 tls
869 1.17.2.2 tls if (rl) {
870 1.17.2.5 jdolecek KASSERT(error);
871 1.17.2.2 tls npf_rule_free(rl);
872 1.17.2.2 tls }
873 1.17.2.4 tls out:
874 1.17.2.2 tls if (retdict) {
875 1.17.2.2 tls prop_object_release(npf_rule);
876 1.17.2.5 jdolecek #if !defined(_NPF_STANDALONE)
877 1.17.2.2 tls prop_dictionary_copyout_ioctl(pref, cmd, retdict);
878 1.17.2.2 tls prop_object_release(retdict);
879 1.17.2.5 jdolecek #else
880 1.17.2.5 jdolecek CTASSERT(sizeof(prop_dictionary_t) == sizeof(void *));
881 1.17.2.5 jdolecek memcpy(data, npf_rule, sizeof(void *));
882 1.17.2.5 jdolecek #endif
883 1.17.2.2 tls }
884 1.17.2.2 tls return error;
885 1.17.2.2 tls }
886 1.17.2.2 tls
887 1.17.2.2 tls /*
888 1.2 rmind * npfctl_table: add, remove or query entries in the specified table.
889 1.1 rmind *
890 1.1 rmind * For maximum performance, interface is avoiding proplib(3)'s overhead.
891 1.1 rmind */
892 1.1 rmind int
893 1.17.2.5 jdolecek npfctl_table(npf_t *npf, void *data)
894 1.1 rmind {
895 1.17.2.1 tls const npf_ioctl_table_t *nct = data;
896 1.17.2.4 tls char tname[NPF_TABLE_MAXNAMELEN];
897 1.17.2.4 tls npf_tableset_t *ts;
898 1.17.2.4 tls npf_table_t *t;
899 1.17.2.4 tls int s, error;
900 1.1 rmind
901 1.17.2.4 tls error = copyinstr(nct->nct_name, tname, sizeof(tname), NULL);
902 1.17.2.4 tls if (error) {
903 1.17.2.4 tls return error;
904 1.17.2.4 tls }
905 1.17.2.4 tls
906 1.17.2.4 tls s = npf_config_read_enter(); /* XXX */
907 1.17.2.5 jdolecek ts = npf_config_tableset(npf);
908 1.17.2.4 tls if ((t = npf_tableset_getbyname(ts, tname)) == NULL) {
909 1.17.2.4 tls npf_config_read_exit(s);
910 1.17.2.4 tls return EINVAL;
911 1.17.2.4 tls }
912 1.17.2.2 tls
913 1.17.2.2 tls switch (nct->nct_cmd) {
914 1.17.2.2 tls case NPF_CMD_TABLE_LOOKUP:
915 1.17.2.4 tls error = npf_table_lookup(t, nct->nct_data.ent.alen,
916 1.17.2.4 tls &nct->nct_data.ent.addr);
917 1.17.2.2 tls break;
918 1.17.2.2 tls case NPF_CMD_TABLE_ADD:
919 1.17.2.4 tls error = npf_table_insert(t, nct->nct_data.ent.alen,
920 1.17.2.4 tls &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
921 1.1 rmind break;
922 1.17.2.2 tls case NPF_CMD_TABLE_REMOVE:
923 1.17.2.4 tls error = npf_table_remove(t, nct->nct_data.ent.alen,
924 1.17.2.4 tls &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
925 1.17.2.1 tls break;
926 1.17.2.2 tls case NPF_CMD_TABLE_LIST:
927 1.17.2.4 tls error = npf_table_list(t, nct->nct_data.buf.buf,
928 1.17.2.4 tls nct->nct_data.buf.len);
929 1.1 rmind break;
930 1.17.2.3 tls case NPF_CMD_TABLE_FLUSH:
931 1.17.2.4 tls error = npf_table_flush(t);
932 1.17.2.3 tls break;
933 1.1 rmind default:
934 1.17.2.1 tls error = EINVAL;
935 1.17.2.1 tls break;
936 1.1 rmind }
937 1.17.2.4 tls npf_config_read_exit(s);
938 1.17.2.2 tls
939 1.1 rmind return error;
940 1.1 rmind }
941