npf.c revision 1.49 1 1.1 rmind /*-
2 1.49 rmind * Copyright (c) 2010-2020 The NetBSD Foundation, Inc.
3 1.1 rmind * All rights reserved.
4 1.1 rmind *
5 1.1 rmind * This material is based upon work partially supported by The
6 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
7 1.1 rmind *
8 1.1 rmind * Redistribution and use in source and binary forms, with or without
9 1.1 rmind * modification, are permitted provided that the following conditions
10 1.1 rmind * are met:
11 1.1 rmind * 1. Redistributions of source code must retain the above copyright
12 1.1 rmind * notice, this list of conditions and the following disclaimer.
13 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer in the
15 1.1 rmind * documentation and/or other materials provided with the distribution.
16 1.1 rmind *
17 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
28 1.1 rmind */
29 1.1 rmind
30 1.1 rmind #include <sys/cdefs.h>
31 1.49 rmind __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.49 2020/05/30 14:16:56 rmind Exp $");
32 1.1 rmind
33 1.1 rmind #include <sys/types.h>
34 1.44 rmind #include <sys/mman.h>
35 1.44 rmind #include <sys/stat.h>
36 1.49 rmind #if !defined(_NPF_STANDALONE)
37 1.49 rmind #include <sys/ioctl.h>
38 1.49 rmind #endif
39 1.1 rmind #include <netinet/in_systm.h>
40 1.1 rmind #include <netinet/in.h>
41 1.11 rmind #include <net/if.h>
42 1.1 rmind
43 1.1 rmind #include <stdlib.h>
44 1.1 rmind #include <string.h>
45 1.7 rmind #include <assert.h>
46 1.44 rmind #include <unistd.h>
47 1.1 rmind #include <errno.h>
48 1.1 rmind #include <err.h>
49 1.1 rmind
50 1.44 rmind #include <nv.h>
51 1.44 rmind #include <dnv.h>
52 1.44 rmind
53 1.44 rmind #include <cdbw.h>
54 1.44 rmind
55 1.1 rmind #define _NPF_PRIVATE
56 1.1 rmind #include "npf.h"
57 1.1 rmind
58 1.1 rmind struct nl_rule {
59 1.44 rmind nvlist_t * rule_dict;
60 1.1 rmind };
61 1.1 rmind
62 1.1 rmind struct nl_rproc {
63 1.44 rmind nvlist_t * rproc_dict;
64 1.1 rmind };
65 1.1 rmind
66 1.1 rmind struct nl_table {
67 1.44 rmind nvlist_t * table_dict;
68 1.1 rmind };
69 1.1 rmind
70 1.19 christos struct nl_alg {
71 1.44 rmind nvlist_t * alg_dict;
72 1.19 christos };
73 1.19 christos
74 1.13 rmind struct nl_ext {
75 1.44 rmind nvlist_t * ext_dict;
76 1.13 rmind };
77 1.13 rmind
78 1.20 rmind struct nl_config {
79 1.44 rmind nvlist_t * ncf_dict;
80 1.44 rmind
81 1.44 rmind /* Temporary rule list. */
82 1.44 rmind nvlist_t ** ncf_rule_list;
83 1.44 rmind unsigned ncf_rule_count;
84 1.20 rmind
85 1.20 rmind /* Iterators. */
86 1.44 rmind unsigned ncf_reduce[16];
87 1.44 rmind unsigned ncf_nlevel;
88 1.46 rmind
89 1.44 rmind nl_rule_t ncf_cur_rule;
90 1.44 rmind nl_table_t ncf_cur_table;
91 1.44 rmind nl_rproc_t ncf_cur_rproc;
92 1.20 rmind };
93 1.20 rmind
94 1.44 rmind /*
95 1.44 rmind * Various helper routines.
96 1.44 rmind */
97 1.16 rmind
98 1.36 christos static bool
99 1.44 rmind _npf_add_addr(nvlist_t *nvl, const char *name, int af, const npf_addr_t *addr)
100 1.36 christos {
101 1.36 christos size_t sz;
102 1.36 christos
103 1.36 christos if (af == AF_INET) {
104 1.36 christos sz = sizeof(struct in_addr);
105 1.36 christos } else if (af == AF_INET6) {
106 1.36 christos sz = sizeof(struct in6_addr);
107 1.36 christos } else {
108 1.36 christos return false;
109 1.36 christos }
110 1.44 rmind nvlist_add_binary(nvl, name, addr, sz);
111 1.44 rmind return nvlist_error(nvl) == 0;
112 1.36 christos }
113 1.36 christos
114 1.41 christos static unsigned
115 1.44 rmind _npf_get_addr(const nvlist_t *nvl, const char *name, npf_addr_t *addr)
116 1.36 christos {
117 1.44 rmind const void *d;
118 1.44 rmind size_t sz = 0;
119 1.36 christos
120 1.44 rmind d = nvlist_get_binary(nvl, name, &sz);
121 1.36 christos switch (sz) {
122 1.36 christos case sizeof(struct in_addr):
123 1.36 christos case sizeof(struct in6_addr):
124 1.36 christos memcpy(addr, d, sz);
125 1.41 christos return (unsigned)sz;
126 1.36 christos }
127 1.44 rmind return 0;
128 1.36 christos }
129 1.40 christos
130 1.44 rmind static bool
131 1.44 rmind _npf_dataset_lookup(const nvlist_t *dict, const char *dataset,
132 1.44 rmind const char *key, const char *name)
133 1.1 rmind {
134 1.44 rmind const nvlist_t * const *items;
135 1.44 rmind size_t nitems;
136 1.1 rmind
137 1.44 rmind if (!nvlist_exists_nvlist_array(dict, dataset)) {
138 1.44 rmind return false;
139 1.44 rmind }
140 1.44 rmind items = nvlist_get_nvlist_array(dict, dataset, &nitems);
141 1.44 rmind for (unsigned i = 0; i < nitems; i++) {
142 1.44 rmind const char *item_name;
143 1.44 rmind
144 1.44 rmind item_name = dnvlist_get_string(items[i], key, NULL);
145 1.44 rmind if (item_name && strcmp(item_name, name) == 0) {
146 1.44 rmind return true;
147 1.44 rmind }
148 1.1 rmind }
149 1.44 rmind return false;
150 1.1 rmind }
151 1.1 rmind
152 1.44 rmind static const nvlist_t *
153 1.44 rmind _npf_dataset_getelement(nvlist_t *dict, const char *dataset, unsigned i)
154 1.1 rmind {
155 1.44 rmind const nvlist_t * const *items;
156 1.44 rmind size_t nitems;
157 1.1 rmind
158 1.44 rmind if (!nvlist_exists_nvlist_array(dict, dataset)) {
159 1.40 christos return NULL;
160 1.1 rmind }
161 1.44 rmind items = nvlist_get_nvlist_array(dict, dataset, &nitems);
162 1.44 rmind if (i < nitems) {
163 1.44 rmind return items[i];
164 1.16 rmind }
165 1.44 rmind return NULL;
166 1.40 christos }
167 1.1 rmind
168 1.44 rmind /*
169 1.44 rmind * _npf_rules_process: transform the ruleset representing nested rules
170 1.44 rmind * with sublists into a single array with skip-to marks.
171 1.44 rmind */
172 1.44 rmind static void
173 1.44 rmind _npf_rules_process(nl_config_t *ncf, nvlist_t *dict, const char *key)
174 1.40 christos {
175 1.44 rmind nvlist_t **items;
176 1.44 rmind size_t nitems;
177 1.40 christos
178 1.44 rmind if (!nvlist_exists_nvlist_array(dict, key)) {
179 1.44 rmind return;
180 1.40 christos }
181 1.44 rmind items = nvlist_take_nvlist_array(dict, key, &nitems);
182 1.44 rmind for (unsigned i = 0; i < nitems; i++) {
183 1.44 rmind nvlist_t *rule_dict = items[i];
184 1.44 rmind size_t len = (ncf->ncf_rule_count + 1) * sizeof(nvlist_t *);
185 1.44 rmind void *p = realloc(ncf->ncf_rule_list, len);
186 1.44 rmind
187 1.44 rmind /*
188 1.44 rmind * - Add rule to the transformed array.
189 1.44 rmind * - Process subrules recursively.
190 1.44 rmind * - Add the skip-to position.
191 1.44 rmind */
192 1.44 rmind ncf->ncf_rule_list = p;
193 1.44 rmind ncf->ncf_rule_list[ncf->ncf_rule_count] = rule_dict;
194 1.44 rmind ncf->ncf_rule_count++;
195 1.44 rmind
196 1.44 rmind if (nvlist_exists_nvlist_array(rule_dict, "subrules")) {
197 1.44 rmind unsigned idx;
198 1.44 rmind
199 1.44 rmind _npf_rules_process(ncf, rule_dict, "subrules");
200 1.44 rmind idx = ncf->ncf_rule_count; // post-recursion index
201 1.44 rmind nvlist_add_number(rule_dict, "skip-to", idx);
202 1.44 rmind }
203 1.44 rmind assert(nvlist_error(rule_dict) == 0);
204 1.1 rmind }
205 1.44 rmind free(items);
206 1.1 rmind }
207 1.1 rmind
208 1.44 rmind /*
209 1.47 rmind * _npf_extract_error: check the error number field and extract the
210 1.47 rmind * error details into the npf_error_t structure.
211 1.47 rmind */
212 1.47 rmind static int
213 1.47 rmind _npf_extract_error(nvlist_t *resp, npf_error_t *errinfo)
214 1.47 rmind {
215 1.47 rmind int error;
216 1.47 rmind
217 1.47 rmind error = dnvlist_get_number(resp, "errno", 0);
218 1.47 rmind if (error && errinfo) {
219 1.47 rmind memset(errinfo, 0, sizeof(npf_error_t));
220 1.47 rmind
221 1.47 rmind errinfo->id = dnvlist_get_number(resp, "id", 0);
222 1.47 rmind errinfo->error_msg =
223 1.47 rmind dnvlist_take_string(resp, "error-msg", NULL);
224 1.47 rmind errinfo->source_file =
225 1.47 rmind dnvlist_take_string(resp, "source-file", NULL);
226 1.47 rmind errinfo->source_line =
227 1.47 rmind dnvlist_take_number(resp, "source-line", 0);
228 1.47 rmind }
229 1.47 rmind return error;
230 1.47 rmind }
231 1.47 rmind
232 1.47 rmind /*
233 1.49 rmind * npf_xfer_fd: transfer the given request and receive a response.
234 1.49 rmind *
235 1.49 rmind * => Sets the 'operation' key on the 'req' dictionary.
236 1.49 rmind * => On success: returns 0 and valid nvlist in 'resp'.
237 1.49 rmind * => On failure: returns an error number.
238 1.49 rmind */
239 1.49 rmind static int
240 1.49 rmind _npf_xfer_fd(int fd, unsigned long cmd, nvlist_t *req, nvlist_t **resp)
241 1.49 rmind {
242 1.49 rmind struct stat st;
243 1.49 rmind int kernver;
244 1.49 rmind
245 1.49 rmind /*
246 1.49 rmind * Set the NPF version and operation.
247 1.49 rmind */
248 1.49 rmind if (!nvlist_exists(req, "version")) {
249 1.49 rmind nvlist_add_number(req, "version", NPF_VERSION);
250 1.49 rmind }
251 1.49 rmind nvlist_add_number(req, "operation", cmd);
252 1.49 rmind
253 1.49 rmind /*
254 1.49 rmind * Determine the type of file descriptor:
255 1.49 rmind * - If socket, then perform nvlist_send()/nvlist_recv().
256 1.49 rmind * - If a character device, then use ioctl.
257 1.49 rmind */
258 1.49 rmind if (fstat(fd, &st) == -1) {
259 1.49 rmind goto err;
260 1.49 rmind }
261 1.49 rmind switch (st.st_mode & S_IFMT) {
262 1.49 rmind #if !defined(__NetBSD__)
263 1.49 rmind case S_IFSOCK:
264 1.49 rmind if (nvlist_send(fd, req) == -1) {
265 1.49 rmind goto err;
266 1.49 rmind }
267 1.49 rmind if (resp && (*resp = nvlist_recv(fd, 0)) == NULL) {
268 1.49 rmind goto err;
269 1.49 rmind }
270 1.49 rmind break;
271 1.49 rmind #endif
272 1.49 rmind #if !defined(_NPF_STANDALONE)
273 1.49 rmind case S_IFBLK:
274 1.49 rmind case S_IFCHR:
275 1.49 rmind if (ioctl(fd, IOC_NPF_VERSION, &kernver) == -1) {
276 1.49 rmind goto err;
277 1.49 rmind }
278 1.49 rmind if (kernver != NPF_VERSION) {
279 1.49 rmind errno = EPROGMISMATCH;
280 1.49 rmind goto err;
281 1.49 rmind }
282 1.49 rmind if (nvlist_xfer_ioctl(fd, cmd, req, resp) == -1) {
283 1.49 rmind goto err;
284 1.49 rmind }
285 1.49 rmind break;
286 1.49 rmind #else
287 1.49 rmind (void)kernver;
288 1.49 rmind #endif
289 1.49 rmind default:
290 1.49 rmind errno = ENOTSUP;
291 1.49 rmind goto err;
292 1.49 rmind }
293 1.49 rmind return 0;
294 1.49 rmind err:
295 1.49 rmind return errno ? errno : EIO;
296 1.49 rmind }
297 1.49 rmind
298 1.49 rmind /*
299 1.49 rmind * npf_xfer_fd_errno: same as npf_xfer_fd(), but:
300 1.49 rmind *
301 1.49 rmind * => After successful retrieval of the response, inspects it, extracts
302 1.49 rmind * the 'errno' value (if any) and returns it.
303 1.49 rmind * => Destroys the response.
304 1.49 rmind */
305 1.49 rmind static int
306 1.49 rmind _npf_xfer_fd_errno(int fd, unsigned long cmd, nvlist_t *req)
307 1.49 rmind {
308 1.49 rmind nvlist_t *resp;
309 1.49 rmind int error;
310 1.49 rmind
311 1.49 rmind error = _npf_xfer_fd(fd, cmd, req, &resp);
312 1.49 rmind if (error) {
313 1.49 rmind return error;
314 1.49 rmind }
315 1.49 rmind error = _npf_extract_error(resp, NULL);
316 1.49 rmind nvlist_destroy(resp);
317 1.49 rmind return error;
318 1.49 rmind }
319 1.49 rmind
320 1.49 rmind /*
321 1.44 rmind * CONFIGURATION INTERFACE.
322 1.44 rmind */
323 1.44 rmind
324 1.44 rmind nl_config_t *
325 1.44 rmind npf_config_create(void)
326 1.30 rmind {
327 1.30 rmind nl_config_t *ncf;
328 1.30 rmind
329 1.44 rmind ncf = calloc(1, sizeof(nl_config_t));
330 1.44 rmind if (!ncf) {
331 1.30 rmind return NULL;
332 1.30 rmind }
333 1.44 rmind ncf->ncf_dict = nvlist_create(0);
334 1.44 rmind nvlist_add_number(ncf->ncf_dict, "version", NPF_VERSION);
335 1.30 rmind return ncf;
336 1.30 rmind }
337 1.30 rmind
338 1.44 rmind int
339 1.44 rmind npf_config_submit(nl_config_t *ncf, int fd, npf_error_t *errinfo)
340 1.44 rmind {
341 1.49 rmind nvlist_t *resp = NULL;
342 1.44 rmind int error;
343 1.44 rmind
344 1.44 rmind /* Ensure the config is built. */
345 1.44 rmind (void)npf_config_build(ncf);
346 1.44 rmind
347 1.49 rmind error = _npf_xfer_fd(fd, IOC_NPF_LOAD, ncf->ncf_dict, &resp);
348 1.49 rmind if (error) {
349 1.49 rmind return error;
350 1.44 rmind }
351 1.49 rmind error = _npf_extract_error(resp, errinfo);
352 1.49 rmind nvlist_destroy(resp);
353 1.44 rmind return error;
354 1.44 rmind }
355 1.44 rmind
356 1.8 rmind nl_config_t *
357 1.40 christos npf_config_retrieve(int fd)
358 1.8 rmind {
359 1.8 rmind nl_config_t *ncf;
360 1.49 rmind nvlist_t *req, *resp = NULL;
361 1.49 rmind int error;
362 1.8 rmind
363 1.44 rmind ncf = calloc(1, sizeof(nl_config_t));
364 1.44 rmind if (!ncf) {
365 1.8 rmind return NULL;
366 1.8 rmind }
367 1.49 rmind
368 1.49 rmind req = nvlist_create(0);
369 1.49 rmind error = _npf_xfer_fd(fd, IOC_NPF_SAVE, req, &resp);
370 1.49 rmind nvlist_destroy(req);
371 1.49 rmind
372 1.49 rmind if (error || _npf_extract_error(resp, NULL) != 0) {
373 1.49 rmind nvlist_destroy(resp);
374 1.44 rmind free(ncf);
375 1.8 rmind return NULL;
376 1.8 rmind }
377 1.49 rmind ncf->ncf_dict = resp;
378 1.8 rmind return ncf;
379 1.8 rmind }
380 1.8 rmind
381 1.40 christos void *
382 1.40 christos npf_config_export(nl_config_t *ncf, size_t *length)
383 1.30 rmind {
384 1.44 rmind /* Ensure the config is built. */
385 1.44 rmind (void)npf_config_build(ncf);
386 1.44 rmind return nvlist_pack(ncf->ncf_dict, length);
387 1.30 rmind }
388 1.30 rmind
389 1.30 rmind nl_config_t *
390 1.44 rmind npf_config_import(const void *blob, size_t len)
391 1.30 rmind {
392 1.30 rmind nl_config_t *ncf;
393 1.30 rmind
394 1.44 rmind ncf = calloc(1, sizeof(nl_config_t));
395 1.44 rmind if (!ncf) {
396 1.30 rmind return NULL;
397 1.30 rmind }
398 1.44 rmind ncf->ncf_dict = nvlist_unpack(blob, len, 0);
399 1.44 rmind if (!ncf->ncf_dict) {
400 1.44 rmind free(ncf);
401 1.30 rmind return NULL;
402 1.30 rmind }
403 1.30 rmind return ncf;
404 1.30 rmind }
405 1.30 rmind
406 1.30 rmind int
407 1.6 rmind npf_config_flush(int fd)
408 1.6 rmind {
409 1.6 rmind nl_config_t *ncf;
410 1.40 christos npf_error_t errinfo;
411 1.6 rmind int error;
412 1.6 rmind
413 1.6 rmind ncf = npf_config_create();
414 1.44 rmind if (!ncf) {
415 1.6 rmind return ENOMEM;
416 1.6 rmind }
417 1.44 rmind nvlist_add_bool(ncf->ncf_dict, "flush", true);
418 1.40 christos error = npf_config_submit(ncf, fd, &errinfo);
419 1.6 rmind npf_config_destroy(ncf);
420 1.6 rmind return error;
421 1.6 rmind }
422 1.6 rmind
423 1.40 christos bool
424 1.40 christos npf_config_active_p(nl_config_t *ncf)
425 1.40 christos {
426 1.44 rmind return dnvlist_get_bool(ncf->ncf_dict, "active", false);
427 1.40 christos }
428 1.40 christos
429 1.40 christos bool
430 1.40 christos npf_config_loaded_p(nl_config_t *ncf)
431 1.40 christos {
432 1.44 rmind return nvlist_exists_nvlist_array(ncf->ncf_dict, "rules");
433 1.40 christos }
434 1.40 christos
435 1.49 rmind const void *
436 1.40 christos npf_config_build(nl_config_t *ncf)
437 1.7 rmind {
438 1.44 rmind _npf_rules_process(ncf, ncf->ncf_dict, "__rules");
439 1.44 rmind if (ncf->ncf_rule_list) {
440 1.44 rmind /* Set the transformed ruleset. */
441 1.44 rmind nvlist_move_nvlist_array(ncf->ncf_dict, "rules",
442 1.44 rmind ncf->ncf_rule_list, ncf->ncf_rule_count);
443 1.44 rmind
444 1.44 rmind /* Clear the temporary list. */
445 1.44 rmind ncf->ncf_rule_list = NULL;
446 1.44 rmind ncf->ncf_rule_count = 0;
447 1.40 christos }
448 1.44 rmind assert(nvlist_error(ncf->ncf_dict) == 0);
449 1.40 christos return (void *)ncf->ncf_dict;
450 1.7 rmind }
451 1.7 rmind
452 1.7 rmind void
453 1.1 rmind npf_config_destroy(nl_config_t *ncf)
454 1.1 rmind {
455 1.44 rmind nvlist_destroy(ncf->ncf_dict);
456 1.1 rmind free(ncf);
457 1.1 rmind }
458 1.1 rmind
459 1.1 rmind /*
460 1.46 rmind * PARAMETERS.
461 1.46 rmind */
462 1.46 rmind
463 1.46 rmind int
464 1.46 rmind npf_param_get(nl_config_t *ncf, const char *name, int *valp)
465 1.46 rmind {
466 1.46 rmind const nvlist_t *params;
467 1.46 rmind
468 1.46 rmind params = dnvlist_get_nvlist(ncf->ncf_dict, "params", NULL);
469 1.46 rmind if (params == NULL || !nvlist_exists(params, name)) {
470 1.46 rmind return ENOENT;
471 1.46 rmind }
472 1.46 rmind *valp = (int)dnvlist_get_number(params, name, 0);
473 1.46 rmind return 0;
474 1.46 rmind }
475 1.46 rmind
476 1.46 rmind int
477 1.46 rmind npf_param_set(nl_config_t *ncf, const char *name, int val)
478 1.46 rmind {
479 1.46 rmind nvlist_t *params;
480 1.46 rmind
481 1.46 rmind /* Ensure params dictionary. */
482 1.46 rmind if (nvlist_exists(ncf->ncf_dict, "params")) {
483 1.46 rmind params = nvlist_take_nvlist(ncf->ncf_dict, "params");
484 1.46 rmind } else {
485 1.46 rmind params = nvlist_create(0);
486 1.46 rmind }
487 1.46 rmind
488 1.46 rmind /*
489 1.46 rmind * If the parameter is already set, then free it first.
490 1.46 rmind * Set the parameter. Note: values can be negative.
491 1.46 rmind */
492 1.46 rmind if (nvlist_exists(params, name)) {
493 1.46 rmind nvlist_free_number(params, name);
494 1.46 rmind }
495 1.46 rmind nvlist_add_number(params, name, (uint64_t)val);
496 1.46 rmind nvlist_add_nvlist(ncf->ncf_dict, "params", params);
497 1.46 rmind return 0;
498 1.46 rmind }
499 1.46 rmind
500 1.49 rmind const char *
501 1.49 rmind npf_param_iterate(nl_config_t *ncf, nl_iter_t *iter, int *val, int *defval)
502 1.49 rmind {
503 1.49 rmind void *cookie = (void *)(intptr_t)*iter;
504 1.49 rmind const nvlist_t *params, *dparams;
505 1.49 rmind const char *name;
506 1.49 rmind int type;
507 1.49 rmind
508 1.49 rmind assert(sizeof(nl_iter_t) >= sizeof(void *));
509 1.49 rmind
510 1.49 rmind params = dnvlist_get_nvlist(ncf->ncf_dict, "params", NULL);
511 1.49 rmind if (params == NULL) {
512 1.49 rmind return NULL;
513 1.49 rmind }
514 1.49 rmind skip:
515 1.49 rmind if ((name = nvlist_next(params, &type, &cookie)) == NULL) {
516 1.49 rmind *iter = NPF_ITER_BEGIN;
517 1.49 rmind return NULL;
518 1.49 rmind }
519 1.49 rmind if (type != NV_TYPE_NUMBER) {
520 1.49 rmind goto skip; // should never happen, though
521 1.49 rmind }
522 1.49 rmind if (defval) {
523 1.49 rmind dparams = dnvlist_get_nvlist(ncf->ncf_dict,
524 1.49 rmind "params-defaults", NULL);
525 1.49 rmind if (dparams == NULL) {
526 1.49 rmind errno = EINVAL;
527 1.49 rmind return NULL;
528 1.49 rmind }
529 1.49 rmind *defval = (int)nvlist_get_number(dparams, name);
530 1.49 rmind }
531 1.49 rmind
532 1.49 rmind *val = (int)nvlist_get_number(params, name);
533 1.49 rmind *iter = (intptr_t)cookie;
534 1.49 rmind return name;
535 1.49 rmind }
536 1.49 rmind
537 1.46 rmind /*
538 1.16 rmind * DYNAMIC RULESET INTERFACE.
539 1.16 rmind */
540 1.16 rmind
541 1.48 rmind static inline bool
542 1.48 rmind _npf_nat_ruleset_p(const char *name)
543 1.48 rmind {
544 1.48 rmind return strncmp(name, NPF_RULESET_MAP_PREF,
545 1.48 rmind sizeof(NPF_RULESET_MAP_PREF) - 1) == 0;
546 1.48 rmind }
547 1.48 rmind
548 1.16 rmind int
549 1.18 rmind npf_ruleset_add(int fd, const char *rname, nl_rule_t *rl, uint64_t *id)
550 1.16 rmind {
551 1.48 rmind const bool natset = _npf_nat_ruleset_p(rname);
552 1.49 rmind nvlist_t *rule_nvl = rl->rule_dict, *resp;
553 1.49 rmind int error;
554 1.16 rmind
555 1.49 rmind nvlist_add_number(rule_nvl, "attr",
556 1.49 rmind NPF_RULE_DYNAMIC | nvlist_take_number(rule_nvl, "attr"));
557 1.48 rmind
558 1.49 rmind if (natset && !dnvlist_get_bool(rule_nvl, "nat-rule", false)) {
559 1.48 rmind errno = EINVAL;
560 1.48 rmind return errno;
561 1.48 rmind }
562 1.49 rmind nvlist_add_string(rule_nvl, "ruleset-name", rname);
563 1.49 rmind nvlist_add_bool(rule_nvl, "nat-ruleset", natset);
564 1.49 rmind nvlist_add_number(rule_nvl, "command", NPF_CMD_RULE_ADD);
565 1.48 rmind
566 1.49 rmind error = _npf_xfer_fd(fd, IOC_NPF_RULE, rule_nvl, &resp);
567 1.49 rmind if (error) {
568 1.49 rmind return error;
569 1.16 rmind }
570 1.49 rmind *id = nvlist_get_number(resp, "id");
571 1.49 rmind nvlist_destroy(resp);
572 1.44 rmind return 0;
573 1.16 rmind }
574 1.16 rmind
575 1.16 rmind int
576 1.18 rmind npf_ruleset_remove(int fd, const char *rname, uint64_t id)
577 1.16 rmind {
578 1.48 rmind const bool natset = _npf_nat_ruleset_p(rname);
579 1.49 rmind nvlist_t *rule_nvl = nvlist_create(0);
580 1.49 rmind int error;
581 1.16 rmind
582 1.49 rmind nvlist_add_string(rule_nvl, "ruleset-name", rname);
583 1.49 rmind nvlist_add_bool(rule_nvl, "nat-ruleset", natset);
584 1.49 rmind nvlist_add_number(rule_nvl, "command", NPF_CMD_RULE_REMOVE);
585 1.49 rmind nvlist_add_number(rule_nvl, "id", id);
586 1.48 rmind
587 1.49 rmind error = _npf_xfer_fd_errno(fd, IOC_NPF_RULE, rule_nvl);
588 1.49 rmind nvlist_destroy(rule_nvl);
589 1.49 rmind return error;
590 1.16 rmind }
591 1.16 rmind
592 1.16 rmind int
593 1.16 rmind npf_ruleset_remkey(int fd, const char *rname, const void *key, size_t len)
594 1.16 rmind {
595 1.48 rmind const bool natset = _npf_nat_ruleset_p(rname);
596 1.49 rmind nvlist_t *rule_nvl = nvlist_create(0);
597 1.49 rmind int error;
598 1.16 rmind
599 1.49 rmind nvlist_add_string(rule_nvl, "ruleset-name", rname);
600 1.49 rmind nvlist_add_bool(rule_nvl, "nat-ruleset", natset);
601 1.49 rmind nvlist_add_number(rule_nvl, "command", NPF_CMD_RULE_REMKEY);
602 1.49 rmind nvlist_add_binary(rule_nvl, "key", key, len);
603 1.48 rmind
604 1.49 rmind error = _npf_xfer_fd_errno(fd, IOC_NPF_RULE, rule_nvl);
605 1.49 rmind nvlist_destroy(rule_nvl);
606 1.49 rmind return error;
607 1.16 rmind }
608 1.16 rmind
609 1.17 rmind int
610 1.17 rmind npf_ruleset_flush(int fd, const char *rname)
611 1.17 rmind {
612 1.48 rmind const bool natset = _npf_nat_ruleset_p(rname);
613 1.49 rmind nvlist_t *rule_nvl = nvlist_create(0);
614 1.49 rmind int error;
615 1.17 rmind
616 1.49 rmind nvlist_add_string(rule_nvl, "ruleset-name", rname);
617 1.49 rmind nvlist_add_bool(rule_nvl, "nat-ruleset", natset);
618 1.49 rmind nvlist_add_number(rule_nvl, "command", NPF_CMD_RULE_FLUSH);
619 1.48 rmind
620 1.49 rmind error = _npf_xfer_fd_errno(fd, IOC_NPF_RULE, rule_nvl);
621 1.49 rmind nvlist_destroy(rule_nvl);
622 1.49 rmind return error;
623 1.16 rmind }
624 1.16 rmind
625 1.16 rmind /*
626 1.13 rmind * NPF EXTENSION INTERFACE.
627 1.13 rmind */
628 1.13 rmind
629 1.13 rmind nl_ext_t *
630 1.13 rmind npf_ext_construct(const char *name)
631 1.13 rmind {
632 1.13 rmind nl_ext_t *ext;
633 1.13 rmind
634 1.13 rmind ext = malloc(sizeof(*ext));
635 1.44 rmind if (!ext) {
636 1.13 rmind return NULL;
637 1.13 rmind }
638 1.44 rmind ext->ext_dict = nvlist_create(0);
639 1.44 rmind nvlist_add_string(ext->ext_dict, "name", name);
640 1.13 rmind return ext;
641 1.13 rmind }
642 1.13 rmind
643 1.13 rmind void
644 1.13 rmind npf_ext_param_u32(nl_ext_t *ext, const char *key, uint32_t val)
645 1.13 rmind {
646 1.44 rmind nvlist_add_number(ext->ext_dict, key, val);
647 1.13 rmind }
648 1.13 rmind
649 1.13 rmind void
650 1.13 rmind npf_ext_param_bool(nl_ext_t *ext, const char *key, bool val)
651 1.13 rmind {
652 1.44 rmind nvlist_add_bool(ext->ext_dict, key, val);
653 1.13 rmind }
654 1.13 rmind
655 1.29 jakllsch void
656 1.29 jakllsch npf_ext_param_string(nl_ext_t *ext, const char *key, const char *val)
657 1.29 jakllsch {
658 1.44 rmind nvlist_add_string(ext->ext_dict, key, val);
659 1.29 jakllsch }
660 1.29 jakllsch
661 1.13 rmind /*
662 1.1 rmind * RULE INTERFACE.
663 1.1 rmind */
664 1.1 rmind
665 1.1 rmind nl_rule_t *
666 1.22 rmind npf_rule_create(const char *name, uint32_t attr, const char *ifname)
667 1.1 rmind {
668 1.1 rmind nl_rule_t *rl;
669 1.1 rmind
670 1.44 rmind rl = malloc(sizeof(nl_rule_t));
671 1.44 rmind if (!rl) {
672 1.1 rmind return NULL;
673 1.1 rmind }
674 1.44 rmind rl->rule_dict = nvlist_create(0);
675 1.44 rmind nvlist_add_number(rl->rule_dict, "attr", attr);
676 1.1 rmind if (name) {
677 1.44 rmind nvlist_add_string(rl->rule_dict, "name", name);
678 1.1 rmind }
679 1.22 rmind if (ifname) {
680 1.44 rmind nvlist_add_string(rl->rule_dict, "ifname", ifname);
681 1.1 rmind }
682 1.1 rmind return rl;
683 1.1 rmind }
684 1.1 rmind
685 1.1 rmind int
686 1.16 rmind npf_rule_setcode(nl_rule_t *rl, int type, const void *code, size_t len)
687 1.1 rmind {
688 1.44 rmind if (type != NPF_CODE_BPF) {
689 1.1 rmind return ENOTSUP;
690 1.1 rmind }
691 1.44 rmind nvlist_add_number(rl->rule_dict, "code-type", (unsigned)type);
692 1.44 rmind nvlist_add_binary(rl->rule_dict, "code", code, len);
693 1.44 rmind return nvlist_error(rl->rule_dict);
694 1.1 rmind }
695 1.1 rmind
696 1.1 rmind int
697 1.16 rmind npf_rule_setkey(nl_rule_t *rl, const void *key, size_t len)
698 1.1 rmind {
699 1.44 rmind nvlist_add_binary(rl->rule_dict, "key", key, len);
700 1.44 rmind return nvlist_error(rl->rule_dict);
701 1.16 rmind }
702 1.16 rmind
703 1.16 rmind int
704 1.20 rmind npf_rule_setinfo(nl_rule_t *rl, const void *info, size_t len)
705 1.20 rmind {
706 1.44 rmind nvlist_add_binary(rl->rule_dict, "info", info, len);
707 1.44 rmind return nvlist_error(rl->rule_dict);
708 1.20 rmind }
709 1.20 rmind
710 1.20 rmind int
711 1.40 christos npf_rule_setprio(nl_rule_t *rl, int pri)
712 1.16 rmind {
713 1.44 rmind nvlist_add_number(rl->rule_dict, "prio", (uint64_t)pri);
714 1.44 rmind return nvlist_error(rl->rule_dict);
715 1.16 rmind }
716 1.16 rmind
717 1.16 rmind int
718 1.16 rmind npf_rule_setproc(nl_rule_t *rl, const char *name)
719 1.16 rmind {
720 1.44 rmind nvlist_add_string(rl->rule_dict, "rproc", name);
721 1.44 rmind return nvlist_error(rl->rule_dict);
722 1.1 rmind }
723 1.1 rmind
724 1.16 rmind void *
725 1.16 rmind npf_rule_export(nl_rule_t *rl, size_t *length)
726 1.16 rmind {
727 1.44 rmind return nvlist_pack(rl->rule_dict, length);
728 1.16 rmind }
729 1.16 rmind
730 1.1 rmind bool
731 1.1 rmind npf_rule_exists_p(nl_config_t *ncf, const char *name)
732 1.1 rmind {
733 1.49 rmind const char *key = nvlist_exists_nvlist_array(ncf->ncf_dict,
734 1.49 rmind "rules") ? "rules" : "__rules"; // config may not be built yet
735 1.49 rmind return _npf_dataset_lookup(ncf->ncf_dict, key, "name", name);
736 1.1 rmind }
737 1.1 rmind
738 1.1 rmind int
739 1.16 rmind npf_rule_insert(nl_config_t *ncf, nl_rule_t *parent, nl_rule_t *rl)
740 1.1 rmind {
741 1.44 rmind nvlist_t *rule_dict = rl->rule_dict;
742 1.44 rmind nvlist_t *target;
743 1.44 rmind const char *key;
744 1.1 rmind
745 1.1 rmind if (parent) {
746 1.44 rmind /* Subrule of the parent. */
747 1.44 rmind target = parent->rule_dict;
748 1.44 rmind key = "subrules";
749 1.1 rmind } else {
750 1.44 rmind /* Global ruleset. */
751 1.44 rmind target = ncf->ncf_dict;
752 1.44 rmind key = "__rules";
753 1.1 rmind }
754 1.44 rmind nvlist_append_nvlist_array(target, key, rule_dict);
755 1.44 rmind nvlist_destroy(rule_dict);
756 1.44 rmind free(rl);
757 1.1 rmind return 0;
758 1.1 rmind }
759 1.1 rmind
760 1.20 rmind static nl_rule_t *
761 1.46 rmind _npf_rule_iterate1(nl_config_t *ncf, const char *key,
762 1.46 rmind nl_iter_t *iter, unsigned *level)
763 1.20 rmind {
764 1.46 rmind unsigned i = *iter;
765 1.44 rmind const nvlist_t *rule_dict;
766 1.44 rmind uint32_t skipto;
767 1.20 rmind
768 1.44 rmind if (i == 0) {
769 1.20 rmind /* Initialise the iterator. */
770 1.20 rmind ncf->ncf_nlevel = 0;
771 1.20 rmind ncf->ncf_reduce[0] = 0;
772 1.20 rmind }
773 1.20 rmind
774 1.44 rmind rule_dict = _npf_dataset_getelement(ncf->ncf_dict, key, i);
775 1.44 rmind if (!rule_dict) {
776 1.46 rmind *iter = NPF_ITER_BEGIN;
777 1.20 rmind return NULL;
778 1.20 rmind }
779 1.46 rmind *iter = i + 1; // next
780 1.20 rmind *level = ncf->ncf_nlevel;
781 1.20 rmind
782 1.44 rmind skipto = dnvlist_get_number(rule_dict, "skip-to", 0);
783 1.20 rmind if (skipto) {
784 1.20 rmind ncf->ncf_nlevel++;
785 1.20 rmind ncf->ncf_reduce[ncf->ncf_nlevel] = skipto;
786 1.20 rmind }
787 1.46 rmind if (ncf->ncf_reduce[ncf->ncf_nlevel] == (i + 1)) {
788 1.20 rmind assert(ncf->ncf_nlevel > 0);
789 1.20 rmind ncf->ncf_nlevel--;
790 1.20 rmind }
791 1.46 rmind
792 1.46 rmind ncf->ncf_cur_rule.rule_dict = __UNCONST(rule_dict); // XXX
793 1.20 rmind return &ncf->ncf_cur_rule;
794 1.20 rmind }
795 1.20 rmind
796 1.20 rmind nl_rule_t *
797 1.46 rmind npf_rule_iterate(nl_config_t *ncf, nl_iter_t *iter, unsigned *level)
798 1.20 rmind {
799 1.46 rmind return _npf_rule_iterate1(ncf, "rules", iter, level);
800 1.20 rmind }
801 1.20 rmind
802 1.20 rmind const char *
803 1.20 rmind npf_rule_getname(nl_rule_t *rl)
804 1.20 rmind {
805 1.44 rmind return dnvlist_get_string(rl->rule_dict, "name", NULL);
806 1.20 rmind }
807 1.20 rmind
808 1.20 rmind uint32_t
809 1.20 rmind npf_rule_getattr(nl_rule_t *rl)
810 1.20 rmind {
811 1.44 rmind return dnvlist_get_number(rl->rule_dict, "attr", 0);
812 1.20 rmind }
813 1.20 rmind
814 1.22 rmind const char *
815 1.20 rmind npf_rule_getinterface(nl_rule_t *rl)
816 1.20 rmind {
817 1.44 rmind return dnvlist_get_string(rl->rule_dict, "ifname", NULL);
818 1.20 rmind }
819 1.20 rmind
820 1.20 rmind const void *
821 1.20 rmind npf_rule_getinfo(nl_rule_t *rl, size_t *len)
822 1.20 rmind {
823 1.44 rmind return dnvlist_get_binary(rl->rule_dict, "info", len, NULL, 0);
824 1.20 rmind }
825 1.20 rmind
826 1.20 rmind const char *
827 1.20 rmind npf_rule_getproc(nl_rule_t *rl)
828 1.20 rmind {
829 1.44 rmind return dnvlist_get_string(rl->rule_dict, "rproc", NULL);
830 1.20 rmind }
831 1.20 rmind
832 1.35 rmind uint64_t
833 1.35 rmind npf_rule_getid(nl_rule_t *rl)
834 1.35 rmind {
835 1.44 rmind return dnvlist_get_number(rl->rule_dict, "id", 0);
836 1.35 rmind }
837 1.35 rmind
838 1.35 rmind const void *
839 1.35 rmind npf_rule_getcode(nl_rule_t *rl, int *type, size_t *len)
840 1.35 rmind {
841 1.44 rmind *type = (int)dnvlist_get_number(rl->rule_dict, "code-type", 0);
842 1.44 rmind return dnvlist_get_binary(rl->rule_dict, "code", len, NULL, 0);
843 1.35 rmind }
844 1.35 rmind
845 1.17 rmind int
846 1.17 rmind _npf_ruleset_list(int fd, const char *rname, nl_config_t *ncf)
847 1.17 rmind {
848 1.48 rmind const bool natset = _npf_nat_ruleset_p(rname);
849 1.49 rmind nvlist_t *req, *resp;
850 1.49 rmind int error;
851 1.44 rmind
852 1.44 rmind req = nvlist_create(0);
853 1.44 rmind nvlist_add_string(req, "ruleset-name", rname);
854 1.48 rmind nvlist_add_bool(req, "nat-ruleset", natset);
855 1.44 rmind nvlist_add_number(req, "command", NPF_CMD_RULE_LIST);
856 1.17 rmind
857 1.49 rmind error = _npf_xfer_fd(fd, IOC_NPF_RULE, req, &resp);
858 1.49 rmind nvlist_destroy(req);
859 1.49 rmind if (error) {
860 1.49 rmind return error;
861 1.17 rmind }
862 1.49 rmind
863 1.49 rmind if (nvlist_exists_nvlist_array(resp, "rules")) {
864 1.44 rmind nvlist_t **rules;
865 1.44 rmind size_t n;
866 1.44 rmind
867 1.49 rmind rules = nvlist_take_nvlist_array(resp, "rules", &n);
868 1.44 rmind nvlist_move_nvlist_array(ncf->ncf_dict, "rules", rules, n);
869 1.17 rmind }
870 1.49 rmind nvlist_destroy(resp);
871 1.44 rmind return 0;
872 1.17 rmind }
873 1.17 rmind
874 1.1 rmind void
875 1.1 rmind npf_rule_destroy(nl_rule_t *rl)
876 1.1 rmind {
877 1.44 rmind nvlist_destroy(rl->rule_dict);
878 1.1 rmind free(rl);
879 1.1 rmind }
880 1.1 rmind
881 1.1 rmind /*
882 1.1 rmind * RULE PROCEDURE INTERFACE.
883 1.1 rmind */
884 1.1 rmind
885 1.1 rmind nl_rproc_t *
886 1.1 rmind npf_rproc_create(const char *name)
887 1.1 rmind {
888 1.44 rmind nl_rproc_t *rp;
889 1.1 rmind
890 1.44 rmind rp = malloc(sizeof(nl_rproc_t));
891 1.44 rmind if (!rp) {
892 1.1 rmind return NULL;
893 1.1 rmind }
894 1.44 rmind rp->rproc_dict = nvlist_create(0);
895 1.44 rmind nvlist_add_string(rp->rproc_dict, "name", name);
896 1.44 rmind return rp;
897 1.1 rmind }
898 1.1 rmind
899 1.1 rmind int
900 1.13 rmind npf_rproc_extcall(nl_rproc_t *rp, nl_ext_t *ext)
901 1.1 rmind {
902 1.44 rmind nvlist_t *rproc_dict = rp->rproc_dict;
903 1.44 rmind const char *name = dnvlist_get_string(ext->ext_dict, "name", NULL);
904 1.1 rmind
905 1.44 rmind if (_npf_dataset_lookup(rproc_dict, "extcalls", "name", name)) {
906 1.13 rmind return EEXIST;
907 1.13 rmind }
908 1.44 rmind nvlist_append_nvlist_array(rproc_dict, "extcalls", ext->ext_dict);
909 1.44 rmind nvlist_destroy(ext->ext_dict);
910 1.44 rmind free(ext);
911 1.1 rmind return 0;
912 1.1 rmind }
913 1.1 rmind
914 1.13 rmind bool
915 1.13 rmind npf_rproc_exists_p(nl_config_t *ncf, const char *name)
916 1.1 rmind {
917 1.44 rmind return _npf_dataset_lookup(ncf->ncf_dict, "rprocs", "name", name);
918 1.1 rmind }
919 1.1 rmind
920 1.1 rmind int
921 1.1 rmind npf_rproc_insert(nl_config_t *ncf, nl_rproc_t *rp)
922 1.1 rmind {
923 1.1 rmind const char *name;
924 1.1 rmind
925 1.44 rmind name = dnvlist_get_string(rp->rproc_dict, "name", NULL);
926 1.44 rmind if (!name) {
927 1.1 rmind return EINVAL;
928 1.1 rmind }
929 1.1 rmind if (npf_rproc_exists_p(ncf, name)) {
930 1.1 rmind return EEXIST;
931 1.1 rmind }
932 1.44 rmind nvlist_append_nvlist_array(ncf->ncf_dict, "rprocs", rp->rproc_dict);
933 1.44 rmind nvlist_destroy(rp->rproc_dict);
934 1.44 rmind free(rp);
935 1.1 rmind return 0;
936 1.1 rmind }
937 1.1 rmind
938 1.20 rmind nl_rproc_t *
939 1.46 rmind npf_rproc_iterate(nl_config_t *ncf, nl_iter_t *iter)
940 1.20 rmind {
941 1.44 rmind const nvlist_t *rproc_dict;
942 1.46 rmind unsigned i = *iter;
943 1.20 rmind
944 1.44 rmind rproc_dict = _npf_dataset_getelement(ncf->ncf_dict, "rprocs", i);
945 1.44 rmind if (!rproc_dict) {
946 1.46 rmind *iter = NPF_ITER_BEGIN;
947 1.20 rmind return NULL;
948 1.20 rmind }
949 1.46 rmind *iter = i + 1; // next
950 1.44 rmind ncf->ncf_cur_rproc.rproc_dict = __UNCONST(rproc_dict); // XXX
951 1.20 rmind return &ncf->ncf_cur_rproc;
952 1.20 rmind }
953 1.20 rmind
954 1.20 rmind const char *
955 1.20 rmind npf_rproc_getname(nl_rproc_t *rp)
956 1.20 rmind {
957 1.44 rmind return dnvlist_get_string(rp->rproc_dict, "name", NULL);
958 1.20 rmind }
959 1.20 rmind
960 1.1 rmind /*
961 1.32 rmind * NAT INTERFACE.
962 1.1 rmind */
963 1.1 rmind
964 1.1 rmind nl_nat_t *
965 1.45 rmind npf_nat_create(int type, unsigned flags, const char *ifname)
966 1.1 rmind {
967 1.1 rmind nl_rule_t *rl;
968 1.44 rmind nvlist_t *rule_dict;
969 1.1 rmind uint32_t attr;
970 1.1 rmind
971 1.1 rmind attr = NPF_RULE_PASS | NPF_RULE_FINAL |
972 1.2 rmind (type == NPF_NATOUT ? NPF_RULE_OUT : NPF_RULE_IN);
973 1.1 rmind
974 1.32 rmind /* Create a rule for NAT policy. Next, will add NAT data. */
975 1.22 rmind rl = npf_rule_create(NULL, attr, ifname);
976 1.44 rmind if (!rl) {
977 1.1 rmind return NULL;
978 1.1 rmind }
979 1.44 rmind rule_dict = rl->rule_dict;
980 1.1 rmind
981 1.1 rmind /* Translation type and flags. */
982 1.44 rmind nvlist_add_number(rule_dict, "type", type);
983 1.44 rmind nvlist_add_number(rule_dict, "flags", flags);
984 1.46 rmind nvlist_add_bool(rule_dict, "nat-rule", true);
985 1.1 rmind return (nl_nat_t *)rl;
986 1.1 rmind }
987 1.1 rmind
988 1.1 rmind int
989 1.46 rmind npf_nat_insert(nl_config_t *ncf, nl_nat_t *nt)
990 1.1 rmind {
991 1.44 rmind nvlist_append_nvlist_array(ncf->ncf_dict, "nat", nt->rule_dict);
992 1.44 rmind nvlist_destroy(nt->rule_dict);
993 1.44 rmind free(nt);
994 1.1 rmind return 0;
995 1.1 rmind }
996 1.1 rmind
997 1.20 rmind nl_nat_t *
998 1.46 rmind npf_nat_iterate(nl_config_t *ncf, nl_iter_t *iter)
999 1.20 rmind {
1000 1.44 rmind unsigned level;
1001 1.46 rmind return _npf_rule_iterate1(ncf, "nat", iter, &level);
1002 1.20 rmind }
1003 1.20 rmind
1004 1.20 rmind int
1005 1.45 rmind npf_nat_setaddr(nl_nat_t *nt, int af, npf_addr_t *addr, npf_netmask_t mask)
1006 1.45 rmind {
1007 1.45 rmind /* Translation IP and mask. */
1008 1.46 rmind if (!_npf_add_addr(nt->rule_dict, "nat-addr", af, addr)) {
1009 1.45 rmind return nvlist_error(nt->rule_dict);
1010 1.45 rmind }
1011 1.45 rmind nvlist_add_number(nt->rule_dict, "nat-mask", (uint32_t)mask);
1012 1.45 rmind return nvlist_error(nt->rule_dict);
1013 1.45 rmind }
1014 1.45 rmind
1015 1.45 rmind int
1016 1.45 rmind npf_nat_setport(nl_nat_t *nt, in_port_t port)
1017 1.45 rmind {
1018 1.45 rmind /* Translation port (for redirect case). */
1019 1.45 rmind nvlist_add_number(nt->rule_dict, "nat-port", port);
1020 1.45 rmind return nvlist_error(nt->rule_dict);
1021 1.45 rmind }
1022 1.45 rmind
1023 1.45 rmind int
1024 1.45 rmind npf_nat_settable(nl_nat_t *nt, unsigned tid)
1025 1.45 rmind {
1026 1.46 rmind /*
1027 1.46 rmind * Translation table ID; the address/mask will then serve as a filter.
1028 1.46 rmind */
1029 1.45 rmind nvlist_add_number(nt->rule_dict, "nat-table-id", tid);
1030 1.45 rmind return nvlist_error(nt->rule_dict);
1031 1.45 rmind }
1032 1.45 rmind
1033 1.45 rmind int
1034 1.44 rmind npf_nat_setalgo(nl_nat_t *nt, unsigned algo)
1035 1.28 rmind {
1036 1.44 rmind nvlist_add_number(nt->rule_dict, "nat-algo", algo);
1037 1.44 rmind return nvlist_error(nt->rule_dict);
1038 1.28 rmind }
1039 1.28 rmind
1040 1.28 rmind int
1041 1.28 rmind npf_nat_setnpt66(nl_nat_t *nt, uint16_t adj)
1042 1.28 rmind {
1043 1.28 rmind int error;
1044 1.28 rmind
1045 1.28 rmind if ((error = npf_nat_setalgo(nt, NPF_ALGO_NPT66)) != 0) {
1046 1.28 rmind return error;
1047 1.28 rmind }
1048 1.44 rmind nvlist_add_number(nt->rule_dict, "npt66-adj", adj);
1049 1.44 rmind return nvlist_error(nt->rule_dict);
1050 1.28 rmind }
1051 1.28 rmind
1052 1.28 rmind int
1053 1.20 rmind npf_nat_gettype(nl_nat_t *nt)
1054 1.20 rmind {
1055 1.44 rmind return dnvlist_get_number(nt->rule_dict, "type", 0);
1056 1.20 rmind }
1057 1.20 rmind
1058 1.44 rmind unsigned
1059 1.27 rmind npf_nat_getflags(nl_nat_t *nt)
1060 1.27 rmind {
1061 1.44 rmind return dnvlist_get_number(nt->rule_dict, "flags", 0);
1062 1.27 rmind }
1063 1.27 rmind
1064 1.45 rmind unsigned
1065 1.45 rmind npf_nat_getalgo(nl_nat_t *nt)
1066 1.45 rmind {
1067 1.45 rmind return dnvlist_get_number(nt->rule_dict, "nat-algo", 0);
1068 1.45 rmind }
1069 1.45 rmind
1070 1.45 rmind const npf_addr_t *
1071 1.45 rmind npf_nat_getaddr(nl_nat_t *nt, size_t *alen, npf_netmask_t *mask)
1072 1.45 rmind {
1073 1.45 rmind const void *data;
1074 1.45 rmind
1075 1.46 rmind if (nvlist_exists(nt->rule_dict, "nat-addr")) {
1076 1.46 rmind data = nvlist_get_binary(nt->rule_dict, "nat-addr", alen);
1077 1.45 rmind *mask = nvlist_get_number(nt->rule_dict, "nat-mask");
1078 1.45 rmind } else {
1079 1.45 rmind data = NULL;
1080 1.45 rmind *alen = 0;
1081 1.45 rmind *mask = NPF_NO_NETMASK;
1082 1.45 rmind }
1083 1.45 rmind return data;
1084 1.45 rmind }
1085 1.45 rmind
1086 1.45 rmind in_port_t
1087 1.45 rmind npf_nat_getport(nl_nat_t *nt)
1088 1.45 rmind {
1089 1.45 rmind return (uint16_t)dnvlist_get_number(nt->rule_dict, "nat-port", 0);
1090 1.45 rmind }
1091 1.45 rmind
1092 1.45 rmind unsigned
1093 1.45 rmind npf_nat_gettable(nl_nat_t *nt)
1094 1.20 rmind {
1095 1.45 rmind return dnvlist_get_number(nt->rule_dict, "nat-table-id", 0);
1096 1.20 rmind }
1097 1.20 rmind
1098 1.1 rmind /*
1099 1.1 rmind * TABLE INTERFACE.
1100 1.1 rmind */
1101 1.1 rmind
1102 1.1 rmind nl_table_t *
1103 1.44 rmind npf_table_create(const char *name, unsigned id, int type)
1104 1.1 rmind {
1105 1.1 rmind nl_table_t *tl;
1106 1.1 rmind
1107 1.5 christos tl = malloc(sizeof(*tl));
1108 1.44 rmind if (!tl) {
1109 1.1 rmind return NULL;
1110 1.1 rmind }
1111 1.44 rmind tl->table_dict = nvlist_create(0);
1112 1.44 rmind nvlist_add_string(tl->table_dict, "name", name);
1113 1.44 rmind nvlist_add_number(tl->table_dict, "id", id);
1114 1.44 rmind nvlist_add_number(tl->table_dict, "type", type);
1115 1.1 rmind return tl;
1116 1.1 rmind }
1117 1.1 rmind
1118 1.1 rmind int
1119 1.15 rmind npf_table_add_entry(nl_table_t *tl, int af, const npf_addr_t *addr,
1120 1.15 rmind const npf_netmask_t mask)
1121 1.1 rmind {
1122 1.44 rmind nvlist_t *entry;
1123 1.1 rmind
1124 1.44 rmind entry = nvlist_create(0);
1125 1.44 rmind if (!entry) {
1126 1.1 rmind return ENOMEM;
1127 1.1 rmind }
1128 1.44 rmind if (!_npf_add_addr(entry, "addr", af, addr)) {
1129 1.44 rmind nvlist_destroy(entry);
1130 1.15 rmind return EINVAL;
1131 1.15 rmind }
1132 1.44 rmind nvlist_add_number(entry, "mask", mask);
1133 1.44 rmind nvlist_append_nvlist_array(tl->table_dict, "entries", entry);
1134 1.44 rmind nvlist_destroy(entry);
1135 1.1 rmind return 0;
1136 1.1 rmind }
1137 1.1 rmind
1138 1.44 rmind static inline int
1139 1.47 rmind _npf_table_build_const(nl_table_t *tl)
1140 1.26 rmind {
1141 1.44 rmind struct cdbw *cdbw;
1142 1.44 rmind const nvlist_t * const *entries;
1143 1.44 rmind int error = 0, fd = -1;
1144 1.44 rmind size_t nitems, len;
1145 1.44 rmind void *cdb, *buf;
1146 1.44 rmind struct stat sb;
1147 1.44 rmind char sfn[32];
1148 1.44 rmind
1149 1.47 rmind if (dnvlist_get_number(tl->table_dict, "type", 0) != NPF_TABLE_CONST) {
1150 1.47 rmind return 0;
1151 1.47 rmind }
1152 1.47 rmind
1153 1.44 rmind if (!nvlist_exists_nvlist_array(tl->table_dict, "entries")) {
1154 1.44 rmind return 0;
1155 1.44 rmind }
1156 1.44 rmind
1157 1.44 rmind /*
1158 1.44 rmind * Create a constant database and put all the entries.
1159 1.44 rmind */
1160 1.44 rmind if ((cdbw = cdbw_open()) == NULL) {
1161 1.44 rmind return errno;
1162 1.44 rmind }
1163 1.44 rmind entries = nvlist_get_nvlist_array(tl->table_dict, "entries", &nitems);
1164 1.44 rmind for (unsigned i = 0; i < nitems; i++) {
1165 1.44 rmind const nvlist_t *entry = entries[i];
1166 1.44 rmind const npf_addr_t *addr;
1167 1.44 rmind size_t alen;
1168 1.44 rmind
1169 1.44 rmind addr = dnvlist_get_binary(entry, "addr", &alen, NULL, 0);
1170 1.44 rmind if (addr == NULL || alen == 0 || alen > sizeof(npf_addr_t)) {
1171 1.44 rmind error = EINVAL;
1172 1.44 rmind goto out;
1173 1.44 rmind }
1174 1.44 rmind if (cdbw_put(cdbw, addr, alen, addr, alen) == -1) {
1175 1.44 rmind error = errno;
1176 1.44 rmind goto out;
1177 1.44 rmind }
1178 1.44 rmind }
1179 1.26 rmind
1180 1.44 rmind /*
1181 1.46 rmind * Write the constant database into a temporary file.
1182 1.44 rmind */
1183 1.44 rmind strncpy(sfn, "/tmp/npfcdb.XXXXXX", sizeof(sfn));
1184 1.44 rmind sfn[sizeof(sfn) - 1] = '\0';
1185 1.44 rmind
1186 1.44 rmind if ((fd = mkstemp(sfn)) == -1) {
1187 1.44 rmind error = errno;
1188 1.44 rmind goto out;
1189 1.26 rmind }
1190 1.44 rmind unlink(sfn);
1191 1.26 rmind
1192 1.44 rmind if (cdbw_output(cdbw, fd, "npf-table-cdb", NULL) == -1) {
1193 1.44 rmind error = errno;
1194 1.44 rmind goto out;
1195 1.44 rmind }
1196 1.44 rmind if (fstat(fd, &sb) == -1) {
1197 1.44 rmind error = errno;
1198 1.44 rmind goto out;
1199 1.44 rmind }
1200 1.44 rmind len = sb.st_size;
1201 1.1 rmind
1202 1.44 rmind /*
1203 1.44 rmind * Memory-map the database and copy it into a buffer.
1204 1.44 rmind */
1205 1.44 rmind buf = malloc(len);
1206 1.44 rmind if (!buf) {
1207 1.44 rmind error = ENOMEM;
1208 1.44 rmind goto out;
1209 1.44 rmind }
1210 1.44 rmind cdb = mmap(NULL, len, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
1211 1.44 rmind if (cdb == MAP_FAILED) {
1212 1.44 rmind error = errno;
1213 1.44 rmind free(buf);
1214 1.44 rmind goto out;
1215 1.44 rmind }
1216 1.44 rmind munmap(cdb, len);
1217 1.24 rmind
1218 1.44 rmind /*
1219 1.44 rmind * Move the data buffer to the nvlist.
1220 1.44 rmind */
1221 1.44 rmind nvlist_move_binary(tl->table_dict, "data", buf, len);
1222 1.44 rmind error = nvlist_error(tl->table_dict);
1223 1.44 rmind out:
1224 1.44 rmind if (fd != -1) {
1225 1.44 rmind close(fd);
1226 1.1 rmind }
1227 1.44 rmind cdbw_close(cdbw);
1228 1.44 rmind return error;
1229 1.1 rmind }
1230 1.1 rmind
1231 1.1 rmind int
1232 1.1 rmind npf_table_insert(nl_config_t *ncf, nl_table_t *tl)
1233 1.1 rmind {
1234 1.44 rmind const char *name;
1235 1.44 rmind int error;
1236 1.1 rmind
1237 1.44 rmind name = dnvlist_get_string(tl->table_dict, "name", NULL);
1238 1.44 rmind if (!name) {
1239 1.1 rmind return EINVAL;
1240 1.1 rmind }
1241 1.44 rmind if (_npf_dataset_lookup(ncf->ncf_dict, "tables", "name", name)) {
1242 1.1 rmind return EEXIST;
1243 1.1 rmind }
1244 1.47 rmind if ((error = _npf_table_build_const(tl)) != 0) {
1245 1.47 rmind return error;
1246 1.44 rmind }
1247 1.44 rmind nvlist_append_nvlist_array(ncf->ncf_dict, "tables", tl->table_dict);
1248 1.44 rmind nvlist_destroy(tl->table_dict);
1249 1.44 rmind free(tl);
1250 1.1 rmind return 0;
1251 1.1 rmind }
1252 1.1 rmind
1253 1.47 rmind int
1254 1.47 rmind npf_table_replace(int fd, nl_table_t *tl, npf_error_t *errinfo)
1255 1.47 rmind {
1256 1.49 rmind nvlist_t *resp = NULL;
1257 1.47 rmind int error;
1258 1.47 rmind
1259 1.47 rmind /* Ensure const tables are built. */
1260 1.47 rmind if ((error = _npf_table_build_const(tl)) != 0) {
1261 1.47 rmind return error;
1262 1.47 rmind }
1263 1.49 rmind error = _npf_xfer_fd(fd, IOC_NPF_TABLE_REPLACE, tl->table_dict, &resp);
1264 1.49 rmind if (error) {
1265 1.49 rmind assert(resp == NULL);
1266 1.47 rmind return errno;
1267 1.47 rmind }
1268 1.49 rmind error = _npf_extract_error(resp, errinfo);
1269 1.49 rmind nvlist_destroy(resp);
1270 1.47 rmind return error;
1271 1.47 rmind }
1272 1.47 rmind
1273 1.20 rmind nl_table_t *
1274 1.46 rmind npf_table_iterate(nl_config_t *ncf, nl_iter_t *iter)
1275 1.20 rmind {
1276 1.44 rmind const nvlist_t *table_dict;
1277 1.46 rmind unsigned i = *iter;
1278 1.20 rmind
1279 1.44 rmind table_dict = _npf_dataset_getelement(ncf->ncf_dict, "tables", i);
1280 1.44 rmind if (!table_dict) {
1281 1.46 rmind *iter = NPF_ITER_BEGIN;
1282 1.20 rmind return NULL;
1283 1.20 rmind }
1284 1.46 rmind *iter = i + 1; // next
1285 1.44 rmind ncf->ncf_cur_table.table_dict = __UNCONST(table_dict); // XXX
1286 1.20 rmind return &ncf->ncf_cur_table;
1287 1.20 rmind }
1288 1.20 rmind
1289 1.20 rmind unsigned
1290 1.20 rmind npf_table_getid(nl_table_t *tl)
1291 1.20 rmind {
1292 1.44 rmind return dnvlist_get_number(tl->table_dict, "id", (unsigned)-1);
1293 1.20 rmind }
1294 1.20 rmind
1295 1.23 rmind const char *
1296 1.23 rmind npf_table_getname(nl_table_t *tl)
1297 1.23 rmind {
1298 1.44 rmind return dnvlist_get_string(tl->table_dict, "name", NULL);
1299 1.23 rmind }
1300 1.23 rmind
1301 1.20 rmind int
1302 1.20 rmind npf_table_gettype(nl_table_t *tl)
1303 1.20 rmind {
1304 1.44 rmind return dnvlist_get_number(tl->table_dict, "type", 0);
1305 1.20 rmind }
1306 1.20 rmind
1307 1.1 rmind void
1308 1.1 rmind npf_table_destroy(nl_table_t *tl)
1309 1.1 rmind {
1310 1.44 rmind nvlist_destroy(tl->table_dict);
1311 1.1 rmind free(tl);
1312 1.1 rmind }
1313 1.1 rmind
1314 1.1 rmind /*
1315 1.19 christos * ALG INTERFACE.
1316 1.19 christos */
1317 1.19 christos
1318 1.19 christos int
1319 1.46 rmind npf_alg_load(nl_config_t *ncf, const char *name)
1320 1.19 christos {
1321 1.44 rmind nvlist_t *alg_dict;
1322 1.19 christos
1323 1.44 rmind if (_npf_dataset_lookup(ncf->ncf_dict, "algs", "name", name)) {
1324 1.19 christos return EEXIST;
1325 1.44 rmind }
1326 1.44 rmind alg_dict = nvlist_create(0);
1327 1.44 rmind nvlist_add_string(alg_dict, "name", name);
1328 1.44 rmind nvlist_append_nvlist_array(ncf->ncf_dict, "algs", alg_dict);
1329 1.44 rmind nvlist_destroy(alg_dict);
1330 1.19 christos return 0;
1331 1.19 christos }
1332 1.19 christos
1333 1.19 christos /*
1334 1.44 rmind * CONNECTION / NAT ENTRY INTERFACE.
1335 1.1 rmind */
1336 1.1 rmind
1337 1.49 rmind typedef struct {
1338 1.49 rmind unsigned alen;
1339 1.49 rmind unsigned proto;
1340 1.49 rmind npf_addr_t addr[3];
1341 1.49 rmind in_port_t port[3];
1342 1.49 rmind } npf_connpoint_t;
1343 1.49 rmind
1344 1.49 rmind static int
1345 1.49 rmind _npf_conn_lookup(int fd, const int af, npf_addr_t *addr[2], in_port_t port[2],
1346 1.49 rmind unsigned proto, const char *ifname, unsigned di)
1347 1.36 christos {
1348 1.49 rmind nvlist_t *req = NULL, *resp = NULL, *key_nv;
1349 1.44 rmind const nvlist_t *nat;
1350 1.36 christos int error = EINVAL;
1351 1.36 christos
1352 1.44 rmind /*
1353 1.44 rmind * Setup the connection lookup key.
1354 1.44 rmind */
1355 1.49 rmind if ((key_nv = nvlist_create(0)) == NULL) {
1356 1.36 christos return ENOMEM;
1357 1.44 rmind }
1358 1.49 rmind if (!_npf_add_addr(key_nv, "saddr", af, addr[0])) {
1359 1.49 rmind nvlist_destroy(key_nv);
1360 1.38 christos goto out;
1361 1.49 rmind }
1362 1.49 rmind if (!_npf_add_addr(key_nv, "daddr", af, addr[1])) {
1363 1.49 rmind nvlist_destroy(key_nv);
1364 1.38 christos goto out;
1365 1.49 rmind }
1366 1.49 rmind nvlist_add_number(key_nv, "sport", htons(port[0]));
1367 1.49 rmind nvlist_add_number(key_nv, "dport", htons(port[1]));
1368 1.49 rmind nvlist_add_number(key_nv, "proto", proto);
1369 1.49 rmind if (ifname) {
1370 1.49 rmind nvlist_add_string(key_nv, "ifname", ifname);
1371 1.49 rmind }
1372 1.49 rmind if (di) {
1373 1.49 rmind nvlist_add_number(key_nv, "di", di);
1374 1.49 rmind }
1375 1.44 rmind
1376 1.44 rmind /*
1377 1.44 rmind * Setup the request.
1378 1.44 rmind */
1379 1.49 rmind if ((req = nvlist_create(0)) == NULL) {
1380 1.44 rmind error = ENOMEM;
1381 1.38 christos goto out;
1382 1.44 rmind }
1383 1.49 rmind nvlist_move_nvlist(req, "key", key_nv);
1384 1.36 christos
1385 1.44 rmind /* Lookup: retrieve the connection entry. */
1386 1.49 rmind error = _npf_xfer_fd(fd, IOC_NPF_CONN_LOOKUP, req, &resp);
1387 1.49 rmind if (error) {
1388 1.36 christos goto out;
1389 1.44 rmind }
1390 1.36 christos
1391 1.44 rmind /*
1392 1.44 rmind * Get the NAT entry and extract the translated pair.
1393 1.44 rmind */
1394 1.49 rmind if ((nat = dnvlist_get_nvlist(resp, "nat", NULL)) == NULL) {
1395 1.49 rmind error = ENOENT;
1396 1.36 christos goto out;
1397 1.36 christos }
1398 1.49 rmind if (_npf_get_addr(nat, "oaddr", addr[0]) == 0 ||
1399 1.49 rmind _npf_get_addr(nat, "taddr", addr[1]) == 0) {
1400 1.36 christos error = EINVAL;
1401 1.36 christos goto out;
1402 1.36 christos }
1403 1.49 rmind port[0] = ntohs(nvlist_get_number(nat, "oport"));
1404 1.49 rmind port[1] = ntohs(nvlist_get_number(nat, "tport"));
1405 1.36 christos out:
1406 1.49 rmind if (resp) {
1407 1.49 rmind nvlist_destroy(resp);
1408 1.44 rmind }
1409 1.44 rmind if (req) {
1410 1.44 rmind nvlist_destroy(req);
1411 1.44 rmind }
1412 1.36 christos return error;
1413 1.36 christos }
1414 1.41 christos
1415 1.49 rmind int
1416 1.49 rmind npf_nat_lookup(int fd, int af, npf_addr_t *addr[2], in_port_t port[2],
1417 1.49 rmind int proto, int di __unused)
1418 1.49 rmind {
1419 1.49 rmind int error;
1420 1.49 rmind
1421 1.49 rmind port[0] = ntohs(port[0]); port[1] = ntohs(port[1]);
1422 1.49 rmind error = _npf_conn_lookup(fd, af, addr, port, proto, NULL, 0);
1423 1.49 rmind port[0] = htons(port[0]); port[1] = htons(port[1]);
1424 1.49 rmind return error;
1425 1.49 rmind }
1426 1.41 christos
1427 1.41 christos static bool
1428 1.49 rmind npf_connkey_handle(const nvlist_t *key_nv, npf_connpoint_t *ep)
1429 1.41 christos {
1430 1.49 rmind unsigned alen1, alen2;
1431 1.44 rmind
1432 1.49 rmind alen1 = _npf_get_addr(key_nv, "saddr", &ep->addr[0]);
1433 1.49 rmind alen2 = _npf_get_addr(key_nv, "daddr", &ep->addr[1]);
1434 1.49 rmind if (alen1 == 0 || alen1 != alen2) {
1435 1.41 christos return false;
1436 1.49 rmind }
1437 1.49 rmind ep->alen = alen1;
1438 1.49 rmind ep->port[0] = ntohs(nvlist_get_number(key_nv, "sport"));
1439 1.49 rmind ep->port[1] = ntohs(nvlist_get_number(key_nv, "dport"));
1440 1.49 rmind ep->proto = nvlist_get_number(key_nv, "proto");
1441 1.41 christos return true;
1442 1.41 christos }
1443 1.41 christos
1444 1.41 christos static void
1445 1.44 rmind npf_conn_handle(const nvlist_t *conn, npf_conn_func_t func, void *arg)
1446 1.41 christos {
1447 1.49 rmind const nvlist_t *key_nv, *nat_nv;
1448 1.41 christos const char *ifname;
1449 1.49 rmind npf_connpoint_t ep;
1450 1.49 rmind
1451 1.49 rmind memset(&ep, 0, sizeof(npf_connpoint_t));
1452 1.41 christos
1453 1.44 rmind ifname = dnvlist_get_string(conn, "ifname", NULL);
1454 1.49 rmind key_nv = dnvlist_get_nvlist(conn, "forw-key", NULL);
1455 1.49 rmind if (!npf_connkey_handle(key_nv, &ep)) {
1456 1.41 christos goto err;
1457 1.41 christos }
1458 1.49 rmind if ((nat_nv = dnvlist_get_nvlist(conn, "nat", NULL)) != NULL) {
1459 1.49 rmind if (_npf_get_addr(nat_nv, "taddr", &ep.addr[2]) != ep.alen) {
1460 1.49 rmind goto err;
1461 1.49 rmind }
1462 1.49 rmind ep.port[2] = ntohs(nvlist_get_number(nat_nv, "tport"));
1463 1.44 rmind }
1464 1.49 rmind /*
1465 1.49 rmind * XXX: add 'proto' and 'flow'; perhaps expand and pass the
1466 1.49 rmind * whole to npf_connpoint_t?
1467 1.49 rmind */
1468 1.49 rmind (*func)((unsigned)ep.alen, ep.addr, ep.port, ifname, arg);
1469 1.41 christos err:
1470 1.41 christos return;
1471 1.41 christos }
1472 1.41 christos
1473 1.41 christos int
1474 1.44 rmind npf_conn_list(int fd, npf_conn_func_t func, void *arg)
1475 1.41 christos {
1476 1.41 christos nl_config_t *ncf;
1477 1.44 rmind const nvlist_t * const *conns;
1478 1.44 rmind size_t nitems;
1479 1.41 christos
1480 1.41 christos ncf = npf_config_retrieve(fd);
1481 1.44 rmind if (!ncf) {
1482 1.41 christos return errno;
1483 1.41 christos }
1484 1.44 rmind if (!nvlist_exists_nvlist_array(ncf->ncf_dict, "conn-list")) {
1485 1.44 rmind return 0;
1486 1.44 rmind }
1487 1.44 rmind conns = nvlist_get_nvlist_array(ncf->ncf_dict, "conn-list", &nitems);
1488 1.44 rmind for (unsigned i = 0; i < nitems; i++) {
1489 1.44 rmind const nvlist_t *conn = conns[i];
1490 1.44 rmind npf_conn_handle(conn, func, arg);
1491 1.44 rmind }
1492 1.49 rmind npf_config_destroy(ncf);
1493 1.44 rmind return 0;
1494 1.44 rmind }
1495 1.42 rmind
1496 1.44 rmind /*
1497 1.44 rmind * MISC.
1498 1.44 rmind */
1499 1.44 rmind
1500 1.44 rmind void
1501 1.44 rmind _npf_debug_addif(nl_config_t *ncf, const char *ifname)
1502 1.44 rmind {
1503 1.44 rmind nvlist_t *debug;
1504 1.44 rmind
1505 1.44 rmind /*
1506 1.44 rmind * Initialise the debug dictionary on the first call.
1507 1.44 rmind */
1508 1.44 rmind debug = dnvlist_take_nvlist(ncf->ncf_dict, "debug", NULL);
1509 1.44 rmind if (debug == NULL) {
1510 1.44 rmind debug = nvlist_create(0);
1511 1.44 rmind }
1512 1.44 rmind if (!_npf_dataset_lookup(debug, "interfaces", "name", ifname)) {
1513 1.44 rmind nvlist_t *ifdict = nvlist_create(0);
1514 1.44 rmind nvlist_add_string(ifdict, "name", ifname);
1515 1.44 rmind nvlist_add_number(ifdict, "index", if_nametoindex(ifname));
1516 1.44 rmind nvlist_append_nvlist_array(debug, "interfaces", ifdict);
1517 1.44 rmind nvlist_destroy(ifdict);
1518 1.42 rmind }
1519 1.44 rmind nvlist_move_nvlist(ncf->ncf_dict, "debug", debug);
1520 1.44 rmind }
1521 1.42 rmind
1522 1.44 rmind void
1523 1.44 rmind _npf_config_dump(nl_config_t *ncf, int fd)
1524 1.44 rmind {
1525 1.44 rmind (void)npf_config_build(ncf);
1526 1.44 rmind nvlist_dump(ncf->ncf_dict, fd);
1527 1.41 christos }
1528