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