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