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