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