npf.c revision 1.43.12.1 1 1.1 rmind /*-
2 1.43.12.1 pgoyette * Copyright (c) 2010-2018 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.43.12.1 pgoyette __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.43.12.1 2018/09/30 01:45:33 pgoyette Exp $");
32 1.1 rmind
33 1.1 rmind #include <sys/types.h>
34 1.43.12.1 pgoyette #include <sys/mman.h>
35 1.43.12.1 pgoyette #include <sys/stat.h>
36 1.1 rmind #include <netinet/in_systm.h>
37 1.1 rmind #include <netinet/in.h>
38 1.11 rmind #include <net/if.h>
39 1.1 rmind
40 1.1 rmind #include <stdlib.h>
41 1.1 rmind #include <string.h>
42 1.7 rmind #include <assert.h>
43 1.43.12.1 pgoyette #include <unistd.h>
44 1.1 rmind #include <errno.h>
45 1.1 rmind #include <err.h>
46 1.1 rmind
47 1.43.12.1 pgoyette #include <nv.h>
48 1.43.12.1 pgoyette #include <dnv.h>
49 1.43.12.1 pgoyette
50 1.43.12.1 pgoyette #include <cdbw.h>
51 1.43.12.1 pgoyette
52 1.1 rmind #define _NPF_PRIVATE
53 1.1 rmind #include "npf.h"
54 1.1 rmind
55 1.1 rmind struct nl_rule {
56 1.43.12.1 pgoyette nvlist_t * rule_dict;
57 1.1 rmind };
58 1.1 rmind
59 1.1 rmind struct nl_rproc {
60 1.43.12.1 pgoyette nvlist_t * rproc_dict;
61 1.1 rmind };
62 1.1 rmind
63 1.1 rmind struct nl_table {
64 1.43.12.1 pgoyette nvlist_t * table_dict;
65 1.1 rmind };
66 1.1 rmind
67 1.19 christos struct nl_alg {
68 1.43.12.1 pgoyette nvlist_t * alg_dict;
69 1.19 christos };
70 1.19 christos
71 1.13 rmind struct nl_ext {
72 1.43.12.1 pgoyette nvlist_t * ext_dict;
73 1.13 rmind };
74 1.13 rmind
75 1.20 rmind struct nl_config {
76 1.43.12.1 pgoyette nvlist_t * ncf_dict;
77 1.43.12.1 pgoyette
78 1.43.12.1 pgoyette /* Temporary rule list. */
79 1.43.12.1 pgoyette nvlist_t ** ncf_rule_list;
80 1.43.12.1 pgoyette unsigned ncf_rule_count;
81 1.20 rmind
82 1.20 rmind /* Iterators. */
83 1.43.12.1 pgoyette unsigned ncf_rule_iter;
84 1.43.12.1 pgoyette unsigned ncf_reduce[16];
85 1.43.12.1 pgoyette unsigned ncf_nlevel;
86 1.43.12.1 pgoyette unsigned ncf_counter;
87 1.43.12.1 pgoyette nl_rule_t ncf_cur_rule;
88 1.43.12.1 pgoyette
89 1.43.12.1 pgoyette unsigned ncf_table_iter;
90 1.43.12.1 pgoyette nl_table_t ncf_cur_table;
91 1.20 rmind
92 1.43.12.1 pgoyette unsigned ncf_rproc_iter;
93 1.43.12.1 pgoyette nl_rproc_t ncf_cur_rproc;
94 1.20 rmind };
95 1.20 rmind
96 1.43.12.1 pgoyette /*
97 1.43.12.1 pgoyette * Various helper routines.
98 1.43.12.1 pgoyette */
99 1.16 rmind
100 1.36 christos static bool
101 1.43.12.1 pgoyette _npf_add_addr(nvlist_t *nvl, const char *name, int af, const npf_addr_t *addr)
102 1.36 christos {
103 1.36 christos size_t sz;
104 1.36 christos
105 1.36 christos if (af == AF_INET) {
106 1.36 christos sz = sizeof(struct in_addr);
107 1.36 christos } else if (af == AF_INET6) {
108 1.36 christos sz = sizeof(struct in6_addr);
109 1.36 christos } else {
110 1.36 christos return false;
111 1.36 christos }
112 1.43.12.1 pgoyette nvlist_add_binary(nvl, name, addr, sz);
113 1.43.12.1 pgoyette return nvlist_error(nvl) == 0;
114 1.36 christos }
115 1.36 christos
116 1.41 christos static unsigned
117 1.43.12.1 pgoyette _npf_get_addr(const nvlist_t *nvl, const char *name, npf_addr_t *addr)
118 1.36 christos {
119 1.43.12.1 pgoyette const void *d;
120 1.43.12.1 pgoyette size_t sz = 0;
121 1.36 christos
122 1.43.12.1 pgoyette d = nvlist_get_binary(nvl, name, &sz);
123 1.36 christos switch (sz) {
124 1.36 christos case sizeof(struct in_addr):
125 1.36 christos case sizeof(struct in6_addr):
126 1.36 christos memcpy(addr, d, sz);
127 1.41 christos return (unsigned)sz;
128 1.36 christos }
129 1.43.12.1 pgoyette return 0;
130 1.36 christos }
131 1.40 christos
132 1.43.12.1 pgoyette static bool
133 1.43.12.1 pgoyette _npf_dataset_lookup(const nvlist_t *dict, const char *dataset,
134 1.43.12.1 pgoyette const char *key, const char *name)
135 1.1 rmind {
136 1.43.12.1 pgoyette const nvlist_t * const *items;
137 1.43.12.1 pgoyette size_t nitems;
138 1.1 rmind
139 1.43.12.1 pgoyette if (!nvlist_exists_nvlist_array(dict, dataset)) {
140 1.43.12.1 pgoyette return false;
141 1.1 rmind }
142 1.43.12.1 pgoyette items = nvlist_get_nvlist_array(dict, dataset, &nitems);
143 1.43.12.1 pgoyette for (unsigned i = 0; i < nitems; i++) {
144 1.43.12.1 pgoyette const char *item_name;
145 1.43.12.1 pgoyette
146 1.43.12.1 pgoyette item_name = dnvlist_get_string(items[i], key, NULL);
147 1.43.12.1 pgoyette if (item_name && strcmp(item_name, name) == 0) {
148 1.43.12.1 pgoyette return true;
149 1.43.12.1 pgoyette }
150 1.43.12.1 pgoyette }
151 1.43.12.1 pgoyette return false;
152 1.1 rmind }
153 1.1 rmind
154 1.43.12.1 pgoyette static const nvlist_t *
155 1.43.12.1 pgoyette _npf_dataset_getelement(nvlist_t *dict, const char *dataset, unsigned i)
156 1.1 rmind {
157 1.43.12.1 pgoyette const nvlist_t * const *items;
158 1.43.12.1 pgoyette size_t nitems;
159 1.1 rmind
160 1.43.12.1 pgoyette if (!nvlist_exists_nvlist_array(dict, dataset)) {
161 1.40 christos return NULL;
162 1.1 rmind }
163 1.43.12.1 pgoyette items = nvlist_get_nvlist_array(dict, dataset, &nitems);
164 1.43.12.1 pgoyette if (i < nitems) {
165 1.43.12.1 pgoyette return items[i];
166 1.16 rmind }
167 1.43.12.1 pgoyette return NULL;
168 1.40 christos }
169 1.1 rmind
170 1.43.12.1 pgoyette /*
171 1.43.12.1 pgoyette * _npf_rules_process: transform the ruleset representing nested rules
172 1.43.12.1 pgoyette * with sublists into a single array with skip-to marks.
173 1.43.12.1 pgoyette */
174 1.43.12.1 pgoyette static void
175 1.43.12.1 pgoyette _npf_rules_process(nl_config_t *ncf, nvlist_t *dict, const char *key)
176 1.40 christos {
177 1.43.12.1 pgoyette nvlist_t **items;
178 1.43.12.1 pgoyette size_t nitems;
179 1.40 christos
180 1.43.12.1 pgoyette if (!nvlist_exists_nvlist_array(dict, key)) {
181 1.43.12.1 pgoyette return;
182 1.7 rmind }
183 1.43.12.1 pgoyette items = nvlist_take_nvlist_array(dict, key, &nitems);
184 1.43.12.1 pgoyette for (unsigned i = 0; i < nitems; i++) {
185 1.43.12.1 pgoyette nvlist_t *rule_dict = items[i];
186 1.43.12.1 pgoyette size_t len = (ncf->ncf_rule_count + 1) * sizeof(nvlist_t *);
187 1.43.12.1 pgoyette void *p = realloc(ncf->ncf_rule_list, len);
188 1.43.12.1 pgoyette
189 1.43.12.1 pgoyette /*
190 1.43.12.1 pgoyette * - Add rule to the transformed array.
191 1.43.12.1 pgoyette * - Process subrules recursively.
192 1.43.12.1 pgoyette * - Add the skip-to position.
193 1.43.12.1 pgoyette */
194 1.43.12.1 pgoyette ncf->ncf_rule_list = p;
195 1.43.12.1 pgoyette ncf->ncf_rule_list[ncf->ncf_rule_count] = rule_dict;
196 1.43.12.1 pgoyette ncf->ncf_rule_count++;
197 1.43.12.1 pgoyette
198 1.43.12.1 pgoyette if (nvlist_exists_nvlist_array(rule_dict, "subrules")) {
199 1.43.12.1 pgoyette unsigned idx;
200 1.43.12.1 pgoyette
201 1.43.12.1 pgoyette _npf_rules_process(ncf, rule_dict, "subrules");
202 1.43.12.1 pgoyette idx = ncf->ncf_rule_count; // post-recursion index
203 1.43.12.1 pgoyette nvlist_add_number(rule_dict, "skip-to", idx);
204 1.43.12.1 pgoyette }
205 1.43.12.1 pgoyette assert(nvlist_error(rule_dict) == 0);
206 1.1 rmind }
207 1.43.12.1 pgoyette free(items);
208 1.1 rmind }
209 1.1 rmind
210 1.43.12.1 pgoyette /*
211 1.43.12.1 pgoyette * CONFIGURATION INTERFACE.
212 1.43.12.1 pgoyette */
213 1.43.12.1 pgoyette
214 1.43.12.1 pgoyette nl_config_t *
215 1.43.12.1 pgoyette npf_config_create(void)
216 1.30 rmind {
217 1.30 rmind nl_config_t *ncf;
218 1.30 rmind
219 1.43.12.1 pgoyette ncf = calloc(1, sizeof(nl_config_t));
220 1.43.12.1 pgoyette if (!ncf) {
221 1.30 rmind return NULL;
222 1.30 rmind }
223 1.43.12.1 pgoyette ncf->ncf_dict = nvlist_create(0);
224 1.43.12.1 pgoyette nvlist_add_number(ncf->ncf_dict, "version", NPF_VERSION);
225 1.30 rmind return ncf;
226 1.30 rmind }
227 1.30 rmind
228 1.43.12.1 pgoyette int
229 1.43.12.1 pgoyette npf_config_submit(nl_config_t *ncf, int fd, npf_error_t *errinfo)
230 1.43.12.1 pgoyette {
231 1.43.12.1 pgoyette nvlist_t *errnv = NULL;
232 1.43.12.1 pgoyette int error;
233 1.43.12.1 pgoyette
234 1.43.12.1 pgoyette /* Ensure the config is built. */
235 1.43.12.1 pgoyette (void)npf_config_build(ncf);
236 1.43.12.1 pgoyette
237 1.43.12.1 pgoyette if (nvlist_xfer_ioctl(fd, IOC_NPF_LOAD, ncf->ncf_dict, &errnv) == -1) {
238 1.43.12.1 pgoyette assert(errnv == NULL);
239 1.43.12.1 pgoyette return errno;
240 1.43.12.1 pgoyette }
241 1.43.12.1 pgoyette error = dnvlist_get_number(errnv, "errno", 0);
242 1.43.12.1 pgoyette if (error && errinfo) {
243 1.43.12.1 pgoyette memset(errinfo, 0, sizeof(npf_error_t));
244 1.43.12.1 pgoyette errinfo->id = dnvlist_get_number(errnv, "id", 0);
245 1.43.12.1 pgoyette errinfo->source_file =
246 1.43.12.1 pgoyette dnvlist_take_string(errnv, "source-file", NULL);
247 1.43.12.1 pgoyette errinfo->source_line =
248 1.43.12.1 pgoyette dnvlist_take_number(errnv, "source-line", 0);
249 1.43.12.1 pgoyette }
250 1.43.12.1 pgoyette nvlist_destroy(errnv);
251 1.43.12.1 pgoyette return error;
252 1.43.12.1 pgoyette }
253 1.43.12.1 pgoyette
254 1.8 rmind nl_config_t *
255 1.40 christos npf_config_retrieve(int fd)
256 1.8 rmind {
257 1.8 rmind nl_config_t *ncf;
258 1.8 rmind
259 1.43.12.1 pgoyette ncf = calloc(1, sizeof(nl_config_t));
260 1.43.12.1 pgoyette if (!ncf) {
261 1.8 rmind return NULL;
262 1.8 rmind }
263 1.43.12.1 pgoyette if (nvlist_recv_ioctl(fd, IOC_NPF_SAVE, &ncf->ncf_dict) == -1) {
264 1.43.12.1 pgoyette free(ncf);
265 1.8 rmind return NULL;
266 1.8 rmind }
267 1.8 rmind return ncf;
268 1.8 rmind }
269 1.8 rmind
270 1.40 christos void *
271 1.40 christos npf_config_export(nl_config_t *ncf, size_t *length)
272 1.30 rmind {
273 1.43.12.1 pgoyette /* Ensure the config is built. */
274 1.43.12.1 pgoyette (void)npf_config_build(ncf);
275 1.43.12.1 pgoyette return nvlist_pack(ncf->ncf_dict, length);
276 1.30 rmind }
277 1.30 rmind
278 1.30 rmind nl_config_t *
279 1.43.12.1 pgoyette npf_config_import(const void *blob, size_t len)
280 1.30 rmind {
281 1.30 rmind nl_config_t *ncf;
282 1.30 rmind
283 1.43.12.1 pgoyette ncf = calloc(1, sizeof(nl_config_t));
284 1.43.12.1 pgoyette if (!ncf) {
285 1.30 rmind return NULL;
286 1.30 rmind }
287 1.43.12.1 pgoyette ncf->ncf_dict = nvlist_unpack(blob, len, 0);
288 1.43.12.1 pgoyette if (!ncf->ncf_dict) {
289 1.43.12.1 pgoyette free(ncf);
290 1.30 rmind return NULL;
291 1.30 rmind }
292 1.30 rmind return ncf;
293 1.30 rmind }
294 1.30 rmind
295 1.30 rmind int
296 1.6 rmind npf_config_flush(int fd)
297 1.6 rmind {
298 1.6 rmind nl_config_t *ncf;
299 1.40 christos npf_error_t errinfo;
300 1.6 rmind int error;
301 1.6 rmind
302 1.6 rmind ncf = npf_config_create();
303 1.43.12.1 pgoyette if (!ncf) {
304 1.6 rmind return ENOMEM;
305 1.6 rmind }
306 1.43.12.1 pgoyette nvlist_add_bool(ncf->ncf_dict, "flush", true);
307 1.40 christos error = npf_config_submit(ncf, fd, &errinfo);
308 1.6 rmind npf_config_destroy(ncf);
309 1.6 rmind return error;
310 1.6 rmind }
311 1.6 rmind
312 1.40 christos bool
313 1.40 christos npf_config_active_p(nl_config_t *ncf)
314 1.40 christos {
315 1.43.12.1 pgoyette return dnvlist_get_bool(ncf->ncf_dict, "active", false);
316 1.40 christos }
317 1.40 christos
318 1.40 christos bool
319 1.40 christos npf_config_loaded_p(nl_config_t *ncf)
320 1.40 christos {
321 1.43.12.1 pgoyette return nvlist_exists_nvlist_array(ncf->ncf_dict, "rules");
322 1.40 christos }
323 1.40 christos
324 1.40 christos void *
325 1.40 christos npf_config_build(nl_config_t *ncf)
326 1.7 rmind {
327 1.43.12.1 pgoyette _npf_rules_process(ncf, ncf->ncf_dict, "__rules");
328 1.43.12.1 pgoyette if (ncf->ncf_rule_list) {
329 1.43.12.1 pgoyette /* Set the transformed ruleset. */
330 1.43.12.1 pgoyette nvlist_move_nvlist_array(ncf->ncf_dict, "rules",
331 1.43.12.1 pgoyette ncf->ncf_rule_list, ncf->ncf_rule_count);
332 1.43.12.1 pgoyette
333 1.43.12.1 pgoyette /* Clear the temporary list. */
334 1.43.12.1 pgoyette ncf->ncf_rule_list = NULL;
335 1.43.12.1 pgoyette ncf->ncf_rule_count = 0;
336 1.40 christos }
337 1.43.12.1 pgoyette assert(nvlist_error(ncf->ncf_dict) == 0);
338 1.40 christos return (void *)ncf->ncf_dict;
339 1.7 rmind }
340 1.7 rmind
341 1.7 rmind void
342 1.1 rmind npf_config_destroy(nl_config_t *ncf)
343 1.1 rmind {
344 1.43.12.1 pgoyette nvlist_destroy(ncf->ncf_dict);
345 1.1 rmind free(ncf);
346 1.1 rmind }
347 1.1 rmind
348 1.1 rmind /*
349 1.16 rmind * DYNAMIC RULESET INTERFACE.
350 1.16 rmind */
351 1.16 rmind
352 1.16 rmind int
353 1.18 rmind npf_ruleset_add(int fd, const char *rname, nl_rule_t *rl, uint64_t *id)
354 1.16 rmind {
355 1.43.12.1 pgoyette nvlist_t *rule_dict = rl->rule_dict;
356 1.43.12.1 pgoyette nvlist_t *ret_dict;
357 1.16 rmind
358 1.43.12.1 pgoyette nvlist_add_string(rule_dict, "ruleset-name", rname);
359 1.43.12.1 pgoyette nvlist_add_number(rule_dict, "command", NPF_CMD_RULE_ADD);
360 1.43.12.1 pgoyette if (nvlist_xfer_ioctl(fd, IOC_NPF_RULE, rule_dict, &ret_dict) == -1) {
361 1.43.12.1 pgoyette return errno;
362 1.16 rmind }
363 1.43.12.1 pgoyette *id = nvlist_get_number(ret_dict, "id");
364 1.43.12.1 pgoyette return 0;
365 1.16 rmind }
366 1.16 rmind
367 1.16 rmind int
368 1.18 rmind npf_ruleset_remove(int fd, const char *rname, uint64_t id)
369 1.16 rmind {
370 1.43.12.1 pgoyette nvlist_t *rule_dict = nvlist_create(0);
371 1.16 rmind
372 1.43.12.1 pgoyette nvlist_add_string(rule_dict, "ruleset-name", rname);
373 1.43.12.1 pgoyette nvlist_add_number(rule_dict, "command", NPF_CMD_RULE_REMOVE);
374 1.43.12.1 pgoyette nvlist_add_number(rule_dict, "id", id);
375 1.43.12.1 pgoyette if (nvlist_send_ioctl(fd, IOC_NPF_RULE, rule_dict) == -1) {
376 1.43.12.1 pgoyette return errno;
377 1.16 rmind }
378 1.43.12.1 pgoyette return 0;
379 1.16 rmind }
380 1.16 rmind
381 1.16 rmind int
382 1.16 rmind npf_ruleset_remkey(int fd, const char *rname, const void *key, size_t len)
383 1.16 rmind {
384 1.43.12.1 pgoyette nvlist_t *rule_dict = nvlist_create(0);
385 1.16 rmind
386 1.43.12.1 pgoyette nvlist_add_string(rule_dict, "ruleset-name", rname);
387 1.43.12.1 pgoyette nvlist_add_number(rule_dict, "command", NPF_CMD_RULE_REMKEY);
388 1.43.12.1 pgoyette nvlist_add_binary(rule_dict, "key", key, len);
389 1.43.12.1 pgoyette if (nvlist_send_ioctl(fd, IOC_NPF_RULE, rule_dict) == -1) {
390 1.43.12.1 pgoyette return errno;
391 1.16 rmind }
392 1.43.12.1 pgoyette return 0;
393 1.16 rmind }
394 1.16 rmind
395 1.17 rmind int
396 1.17 rmind npf_ruleset_flush(int fd, const char *rname)
397 1.17 rmind {
398 1.43.12.1 pgoyette nvlist_t *rule_dict = nvlist_create(0);
399 1.16 rmind
400 1.43.12.1 pgoyette nvlist_add_string(rule_dict, "ruleset-name", rname);
401 1.43.12.1 pgoyette nvlist_add_number(rule_dict, "command", NPF_CMD_RULE_FLUSH);
402 1.43.12.1 pgoyette if (nvlist_send_ioctl(fd, IOC_NPF_RULE, rule_dict) == -1) {
403 1.43.12.1 pgoyette return errno;
404 1.16 rmind }
405 1.43.12.1 pgoyette return 0;
406 1.16 rmind }
407 1.16 rmind
408 1.16 rmind /*
409 1.13 rmind * NPF EXTENSION INTERFACE.
410 1.13 rmind */
411 1.13 rmind
412 1.13 rmind nl_ext_t *
413 1.13 rmind npf_ext_construct(const char *name)
414 1.13 rmind {
415 1.13 rmind nl_ext_t *ext;
416 1.13 rmind
417 1.13 rmind ext = malloc(sizeof(*ext));
418 1.43.12.1 pgoyette if (!ext) {
419 1.13 rmind return NULL;
420 1.13 rmind }
421 1.43.12.1 pgoyette ext->ext_dict = nvlist_create(0);
422 1.43.12.1 pgoyette nvlist_add_string(ext->ext_dict, "name", name);
423 1.13 rmind return ext;
424 1.13 rmind }
425 1.13 rmind
426 1.13 rmind void
427 1.13 rmind npf_ext_param_u32(nl_ext_t *ext, const char *key, uint32_t val)
428 1.13 rmind {
429 1.43.12.1 pgoyette nvlist_add_number(ext->ext_dict, key, val);
430 1.13 rmind }
431 1.13 rmind
432 1.13 rmind void
433 1.13 rmind npf_ext_param_bool(nl_ext_t *ext, const char *key, bool val)
434 1.13 rmind {
435 1.43.12.1 pgoyette nvlist_add_bool(ext->ext_dict, key, val);
436 1.13 rmind }
437 1.13 rmind
438 1.29 jakllsch void
439 1.29 jakllsch npf_ext_param_string(nl_ext_t *ext, const char *key, const char *val)
440 1.29 jakllsch {
441 1.43.12.1 pgoyette nvlist_add_string(ext->ext_dict, key, val);
442 1.29 jakllsch }
443 1.29 jakllsch
444 1.13 rmind /*
445 1.1 rmind * RULE INTERFACE.
446 1.1 rmind */
447 1.1 rmind
448 1.1 rmind nl_rule_t *
449 1.22 rmind npf_rule_create(const char *name, uint32_t attr, const char *ifname)
450 1.1 rmind {
451 1.1 rmind nl_rule_t *rl;
452 1.1 rmind
453 1.43.12.1 pgoyette rl = malloc(sizeof(nl_rule_t));
454 1.43.12.1 pgoyette if (!rl) {
455 1.1 rmind return NULL;
456 1.1 rmind }
457 1.43.12.1 pgoyette rl->rule_dict = nvlist_create(0);
458 1.43.12.1 pgoyette nvlist_add_number(rl->rule_dict, "attr", attr);
459 1.1 rmind if (name) {
460 1.43.12.1 pgoyette nvlist_add_string(rl->rule_dict, "name", name);
461 1.1 rmind }
462 1.22 rmind if (ifname) {
463 1.43.12.1 pgoyette nvlist_add_string(rl->rule_dict, "ifname", ifname);
464 1.1 rmind }
465 1.1 rmind return rl;
466 1.1 rmind }
467 1.1 rmind
468 1.1 rmind int
469 1.16 rmind npf_rule_setcode(nl_rule_t *rl, int type, const void *code, size_t len)
470 1.1 rmind {
471 1.43.12.1 pgoyette if (type != NPF_CODE_BPF) {
472 1.1 rmind return ENOTSUP;
473 1.1 rmind }
474 1.43.12.1 pgoyette nvlist_add_number(rl->rule_dict, "code-type", (unsigned)type);
475 1.43.12.1 pgoyette nvlist_add_binary(rl->rule_dict, "code", code, len);
476 1.43.12.1 pgoyette return nvlist_error(rl->rule_dict);
477 1.1 rmind }
478 1.1 rmind
479 1.1 rmind int
480 1.16 rmind npf_rule_setkey(nl_rule_t *rl, const void *key, size_t len)
481 1.1 rmind {
482 1.43.12.1 pgoyette nvlist_add_binary(rl->rule_dict, "key", key, len);
483 1.43.12.1 pgoyette return nvlist_error(rl->rule_dict);
484 1.16 rmind }
485 1.16 rmind
486 1.16 rmind int
487 1.20 rmind npf_rule_setinfo(nl_rule_t *rl, const void *info, size_t len)
488 1.20 rmind {
489 1.43.12.1 pgoyette nvlist_add_binary(rl->rule_dict, "info", info, len);
490 1.43.12.1 pgoyette return nvlist_error(rl->rule_dict);
491 1.20 rmind }
492 1.20 rmind
493 1.20 rmind int
494 1.40 christos npf_rule_setprio(nl_rule_t *rl, int pri)
495 1.16 rmind {
496 1.43.12.1 pgoyette nvlist_add_number(rl->rule_dict, "prio", (uint64_t)pri);
497 1.43.12.1 pgoyette return nvlist_error(rl->rule_dict);
498 1.16 rmind }
499 1.16 rmind
500 1.16 rmind int
501 1.16 rmind npf_rule_setproc(nl_rule_t *rl, const char *name)
502 1.16 rmind {
503 1.43.12.1 pgoyette nvlist_add_string(rl->rule_dict, "rproc", name);
504 1.43.12.1 pgoyette return nvlist_error(rl->rule_dict);
505 1.1 rmind }
506 1.1 rmind
507 1.16 rmind void *
508 1.16 rmind npf_rule_export(nl_rule_t *rl, size_t *length)
509 1.16 rmind {
510 1.43.12.1 pgoyette return nvlist_pack(rl->rule_dict, length);
511 1.16 rmind }
512 1.16 rmind
513 1.1 rmind bool
514 1.1 rmind npf_rule_exists_p(nl_config_t *ncf, const char *name)
515 1.1 rmind {
516 1.43.12.1 pgoyette return _npf_dataset_lookup(ncf->ncf_dict, "rules", "name", name);
517 1.1 rmind }
518 1.1 rmind
519 1.1 rmind int
520 1.16 rmind npf_rule_insert(nl_config_t *ncf, nl_rule_t *parent, nl_rule_t *rl)
521 1.1 rmind {
522 1.43.12.1 pgoyette nvlist_t *rule_dict = rl->rule_dict;
523 1.43.12.1 pgoyette nvlist_t *target;
524 1.43.12.1 pgoyette const char *key;
525 1.1 rmind
526 1.1 rmind if (parent) {
527 1.43.12.1 pgoyette /* Subrule of the parent. */
528 1.43.12.1 pgoyette target = parent->rule_dict;
529 1.43.12.1 pgoyette key = "subrules";
530 1.1 rmind } else {
531 1.43.12.1 pgoyette /* Global ruleset. */
532 1.43.12.1 pgoyette target = ncf->ncf_dict;
533 1.43.12.1 pgoyette key = "__rules";
534 1.1 rmind }
535 1.43.12.1 pgoyette nvlist_append_nvlist_array(target, key, rule_dict);
536 1.43.12.1 pgoyette nvlist_destroy(rule_dict);
537 1.43.12.1 pgoyette free(rl);
538 1.1 rmind return 0;
539 1.1 rmind }
540 1.1 rmind
541 1.20 rmind static nl_rule_t *
542 1.43.12.1 pgoyette _npf_rule_iterate1(nl_config_t *ncf, const char *key, unsigned *level)
543 1.20 rmind {
544 1.43.12.1 pgoyette unsigned i = ncf->ncf_rule_iter++;
545 1.43.12.1 pgoyette const nvlist_t *rule_dict;
546 1.43.12.1 pgoyette uint32_t skipto;
547 1.20 rmind
548 1.43.12.1 pgoyette if (i == 0) {
549 1.20 rmind /* Initialise the iterator. */
550 1.20 rmind ncf->ncf_nlevel = 0;
551 1.20 rmind ncf->ncf_reduce[0] = 0;
552 1.20 rmind ncf->ncf_counter = 0;
553 1.20 rmind }
554 1.20 rmind
555 1.43.12.1 pgoyette rule_dict = _npf_dataset_getelement(ncf->ncf_dict, key, i);
556 1.43.12.1 pgoyette if (!rule_dict) {
557 1.43.12.1 pgoyette /* Reset the iterator. */
558 1.43.12.1 pgoyette ncf->ncf_rule_iter = 0;
559 1.20 rmind return NULL;
560 1.20 rmind }
561 1.43.12.1 pgoyette ncf->ncf_cur_rule.rule_dict = __UNCONST(rule_dict); // XXX
562 1.20 rmind *level = ncf->ncf_nlevel;
563 1.20 rmind
564 1.43.12.1 pgoyette skipto = dnvlist_get_number(rule_dict, "skip-to", 0);
565 1.20 rmind if (skipto) {
566 1.20 rmind ncf->ncf_nlevel++;
567 1.20 rmind ncf->ncf_reduce[ncf->ncf_nlevel] = skipto;
568 1.20 rmind }
569 1.20 rmind if (ncf->ncf_reduce[ncf->ncf_nlevel] == ++ncf->ncf_counter) {
570 1.20 rmind assert(ncf->ncf_nlevel > 0);
571 1.20 rmind ncf->ncf_nlevel--;
572 1.20 rmind }
573 1.20 rmind return &ncf->ncf_cur_rule;
574 1.20 rmind }
575 1.20 rmind
576 1.20 rmind nl_rule_t *
577 1.20 rmind npf_rule_iterate(nl_config_t *ncf, unsigned *level)
578 1.20 rmind {
579 1.43.12.1 pgoyette return _npf_rule_iterate1(ncf, "rules", level);
580 1.20 rmind }
581 1.20 rmind
582 1.20 rmind const char *
583 1.20 rmind npf_rule_getname(nl_rule_t *rl)
584 1.20 rmind {
585 1.43.12.1 pgoyette return dnvlist_get_string(rl->rule_dict, "name", NULL);
586 1.20 rmind }
587 1.20 rmind
588 1.20 rmind uint32_t
589 1.20 rmind npf_rule_getattr(nl_rule_t *rl)
590 1.20 rmind {
591 1.43.12.1 pgoyette return dnvlist_get_number(rl->rule_dict, "attr", 0);
592 1.20 rmind }
593 1.20 rmind
594 1.22 rmind const char *
595 1.20 rmind npf_rule_getinterface(nl_rule_t *rl)
596 1.20 rmind {
597 1.43.12.1 pgoyette return dnvlist_get_string(rl->rule_dict, "ifname", NULL);
598 1.20 rmind }
599 1.20 rmind
600 1.20 rmind const void *
601 1.20 rmind npf_rule_getinfo(nl_rule_t *rl, size_t *len)
602 1.20 rmind {
603 1.43.12.1 pgoyette return dnvlist_get_binary(rl->rule_dict, "info", len, NULL, 0);
604 1.20 rmind }
605 1.20 rmind
606 1.20 rmind const char *
607 1.20 rmind npf_rule_getproc(nl_rule_t *rl)
608 1.20 rmind {
609 1.43.12.1 pgoyette return dnvlist_get_string(rl->rule_dict, "rproc", NULL);
610 1.20 rmind }
611 1.20 rmind
612 1.35 rmind uint64_t
613 1.35 rmind npf_rule_getid(nl_rule_t *rl)
614 1.35 rmind {
615 1.43.12.1 pgoyette return dnvlist_get_number(rl->rule_dict, "id", 0);
616 1.35 rmind }
617 1.35 rmind
618 1.35 rmind const void *
619 1.35 rmind npf_rule_getcode(nl_rule_t *rl, int *type, size_t *len)
620 1.35 rmind {
621 1.43.12.1 pgoyette *type = (int)dnvlist_get_number(rl->rule_dict, "code-type", 0);
622 1.43.12.1 pgoyette return dnvlist_get_binary(rl->rule_dict, "code", len, NULL, 0);
623 1.35 rmind }
624 1.35 rmind
625 1.17 rmind int
626 1.17 rmind _npf_ruleset_list(int fd, const char *rname, nl_config_t *ncf)
627 1.17 rmind {
628 1.43.12.1 pgoyette nvlist_t *req, *ret;
629 1.17 rmind
630 1.43.12.1 pgoyette req = nvlist_create(0);
631 1.43.12.1 pgoyette nvlist_add_string(req, "ruleset-name", rname);
632 1.43.12.1 pgoyette nvlist_add_number(req, "command", NPF_CMD_RULE_LIST);
633 1.43.12.1 pgoyette
634 1.43.12.1 pgoyette if (nvlist_xfer_ioctl(fd, IOC_NPF_RULE, req, &ret) == -1) {
635 1.43.12.1 pgoyette return errno;
636 1.17 rmind }
637 1.43.12.1 pgoyette if (nvlist_exists_nvlist_array(ret, "rules")) {
638 1.43.12.1 pgoyette nvlist_t **rules;
639 1.43.12.1 pgoyette size_t n;
640 1.43.12.1 pgoyette
641 1.43.12.1 pgoyette rules = nvlist_take_nvlist_array(ret, "rules", &n);
642 1.43.12.1 pgoyette nvlist_move_nvlist_array(ncf->ncf_dict, "rules", rules, n);
643 1.17 rmind }
644 1.43.12.1 pgoyette nvlist_destroy(ret);
645 1.43.12.1 pgoyette return 0;
646 1.17 rmind }
647 1.17 rmind
648 1.1 rmind void
649 1.1 rmind npf_rule_destroy(nl_rule_t *rl)
650 1.1 rmind {
651 1.43.12.1 pgoyette nvlist_destroy(rl->rule_dict);
652 1.1 rmind free(rl);
653 1.1 rmind }
654 1.1 rmind
655 1.1 rmind /*
656 1.1 rmind * RULE PROCEDURE INTERFACE.
657 1.1 rmind */
658 1.1 rmind
659 1.1 rmind nl_rproc_t *
660 1.1 rmind npf_rproc_create(const char *name)
661 1.1 rmind {
662 1.43.12.1 pgoyette nl_rproc_t *rp;
663 1.1 rmind
664 1.43.12.1 pgoyette rp = malloc(sizeof(nl_rproc_t));
665 1.43.12.1 pgoyette if (!rp) {
666 1.1 rmind return NULL;
667 1.1 rmind }
668 1.43.12.1 pgoyette rp->rproc_dict = nvlist_create(0);
669 1.43.12.1 pgoyette nvlist_add_string(rp->rproc_dict, "name", name);
670 1.43.12.1 pgoyette return rp;
671 1.1 rmind }
672 1.1 rmind
673 1.1 rmind int
674 1.13 rmind npf_rproc_extcall(nl_rproc_t *rp, nl_ext_t *ext)
675 1.1 rmind {
676 1.43.12.1 pgoyette nvlist_t *rproc_dict = rp->rproc_dict;
677 1.43.12.1 pgoyette const char *name = dnvlist_get_string(ext->ext_dict, "name", NULL);
678 1.1 rmind
679 1.43.12.1 pgoyette if (_npf_dataset_lookup(rproc_dict, "extcalls", "name", name)) {
680 1.13 rmind return EEXIST;
681 1.13 rmind }
682 1.43.12.1 pgoyette nvlist_append_nvlist_array(rproc_dict, "extcalls", ext->ext_dict);
683 1.43.12.1 pgoyette nvlist_destroy(ext->ext_dict);
684 1.43.12.1 pgoyette free(ext);
685 1.1 rmind return 0;
686 1.1 rmind }
687 1.1 rmind
688 1.13 rmind bool
689 1.13 rmind npf_rproc_exists_p(nl_config_t *ncf, const char *name)
690 1.1 rmind {
691 1.43.12.1 pgoyette return _npf_dataset_lookup(ncf->ncf_dict, "rprocs", "name", name);
692 1.1 rmind }
693 1.1 rmind
694 1.1 rmind int
695 1.1 rmind npf_rproc_insert(nl_config_t *ncf, nl_rproc_t *rp)
696 1.1 rmind {
697 1.1 rmind const char *name;
698 1.1 rmind
699 1.43.12.1 pgoyette name = dnvlist_get_string(rp->rproc_dict, "name", NULL);
700 1.43.12.1 pgoyette if (!name) {
701 1.1 rmind return EINVAL;
702 1.1 rmind }
703 1.1 rmind if (npf_rproc_exists_p(ncf, name)) {
704 1.1 rmind return EEXIST;
705 1.1 rmind }
706 1.43.12.1 pgoyette nvlist_append_nvlist_array(ncf->ncf_dict, "rprocs", rp->rproc_dict);
707 1.43.12.1 pgoyette nvlist_destroy(rp->rproc_dict);
708 1.43.12.1 pgoyette free(rp);
709 1.1 rmind return 0;
710 1.1 rmind }
711 1.1 rmind
712 1.20 rmind nl_rproc_t *
713 1.20 rmind npf_rproc_iterate(nl_config_t *ncf)
714 1.20 rmind {
715 1.43.12.1 pgoyette const nvlist_t *rproc_dict;
716 1.43.12.1 pgoyette unsigned i = ncf->ncf_rproc_iter++;
717 1.20 rmind
718 1.43.12.1 pgoyette rproc_dict = _npf_dataset_getelement(ncf->ncf_dict, "rprocs", i);
719 1.43.12.1 pgoyette if (!rproc_dict) {
720 1.43.12.1 pgoyette /* Reset the iterator. */
721 1.43.12.1 pgoyette ncf->ncf_rproc_iter = 0;
722 1.20 rmind return NULL;
723 1.20 rmind }
724 1.43.12.1 pgoyette ncf->ncf_cur_rproc.rproc_dict = __UNCONST(rproc_dict); // XXX
725 1.20 rmind return &ncf->ncf_cur_rproc;
726 1.20 rmind }
727 1.20 rmind
728 1.20 rmind const char *
729 1.20 rmind npf_rproc_getname(nl_rproc_t *rp)
730 1.20 rmind {
731 1.43.12.1 pgoyette return dnvlist_get_string(rp->rproc_dict, "name", NULL);
732 1.20 rmind }
733 1.20 rmind
734 1.1 rmind /*
735 1.32 rmind * NAT INTERFACE.
736 1.1 rmind */
737 1.1 rmind
738 1.1 rmind nl_nat_t *
739 1.43.12.1 pgoyette npf_nat_create(int type, unsigned flags, const char *ifname,
740 1.28 rmind int af, npf_addr_t *addr, npf_netmask_t mask, in_port_t port)
741 1.1 rmind {
742 1.1 rmind nl_rule_t *rl;
743 1.43.12.1 pgoyette nvlist_t *rule_dict;
744 1.1 rmind uint32_t attr;
745 1.1 rmind
746 1.1 rmind attr = NPF_RULE_PASS | NPF_RULE_FINAL |
747 1.2 rmind (type == NPF_NATOUT ? NPF_RULE_OUT : NPF_RULE_IN);
748 1.1 rmind
749 1.32 rmind /* Create a rule for NAT policy. Next, will add NAT data. */
750 1.22 rmind rl = npf_rule_create(NULL, attr, ifname);
751 1.43.12.1 pgoyette if (!rl) {
752 1.1 rmind return NULL;
753 1.1 rmind }
754 1.43.12.1 pgoyette rule_dict = rl->rule_dict;
755 1.1 rmind
756 1.1 rmind /* Translation type and flags. */
757 1.43.12.1 pgoyette nvlist_add_number(rule_dict, "type", type);
758 1.43.12.1 pgoyette nvlist_add_number(rule_dict, "flags", flags);
759 1.1 rmind
760 1.28 rmind /* Translation IP and mask. */
761 1.43.12.1 pgoyette if (!_npf_add_addr(rule_dict, "nat-ip", af, addr)) {
762 1.1 rmind npf_rule_destroy(rl);
763 1.1 rmind return NULL;
764 1.1 rmind }
765 1.43.12.1 pgoyette nvlist_add_number(rule_dict, "nat-mask", (uint32_t)mask);
766 1.1 rmind
767 1.1 rmind /* Translation port (for redirect case). */
768 1.43.12.1 pgoyette nvlist_add_number(rule_dict, "nat-port", port);
769 1.1 rmind
770 1.1 rmind return (nl_nat_t *)rl;
771 1.1 rmind }
772 1.1 rmind
773 1.1 rmind int
774 1.40 christos npf_nat_insert(nl_config_t *ncf, nl_nat_t *nt, int pri __unused)
775 1.1 rmind {
776 1.43.12.1 pgoyette nvlist_add_number(nt->rule_dict, "prio", (uint64_t)NPF_PRI_LAST);
777 1.43.12.1 pgoyette nvlist_append_nvlist_array(ncf->ncf_dict, "nat", nt->rule_dict);
778 1.43.12.1 pgoyette nvlist_destroy(nt->rule_dict);
779 1.43.12.1 pgoyette free(nt);
780 1.1 rmind return 0;
781 1.1 rmind }
782 1.1 rmind
783 1.20 rmind nl_nat_t *
784 1.20 rmind npf_nat_iterate(nl_config_t *ncf)
785 1.20 rmind {
786 1.43.12.1 pgoyette unsigned level;
787 1.43.12.1 pgoyette return _npf_rule_iterate1(ncf, "nat", &level);
788 1.20 rmind }
789 1.20 rmind
790 1.20 rmind int
791 1.43.12.1 pgoyette npf_nat_setalgo(nl_nat_t *nt, unsigned algo)
792 1.28 rmind {
793 1.43.12.1 pgoyette nvlist_add_number(nt->rule_dict, "nat-algo", algo);
794 1.43.12.1 pgoyette return nvlist_error(nt->rule_dict);
795 1.28 rmind }
796 1.28 rmind
797 1.28 rmind int
798 1.28 rmind npf_nat_setnpt66(nl_nat_t *nt, uint16_t adj)
799 1.28 rmind {
800 1.28 rmind int error;
801 1.28 rmind
802 1.28 rmind if ((error = npf_nat_setalgo(nt, NPF_ALGO_NPT66)) != 0) {
803 1.28 rmind return error;
804 1.28 rmind }
805 1.43.12.1 pgoyette nvlist_add_number(nt->rule_dict, "npt66-adj", adj);
806 1.43.12.1 pgoyette return nvlist_error(nt->rule_dict);
807 1.28 rmind }
808 1.28 rmind
809 1.28 rmind int
810 1.20 rmind npf_nat_gettype(nl_nat_t *nt)
811 1.20 rmind {
812 1.43.12.1 pgoyette return dnvlist_get_number(nt->rule_dict, "type", 0);
813 1.20 rmind }
814 1.20 rmind
815 1.43.12.1 pgoyette unsigned
816 1.27 rmind npf_nat_getflags(nl_nat_t *nt)
817 1.27 rmind {
818 1.43.12.1 pgoyette return dnvlist_get_number(nt->rule_dict, "flags", 0);
819 1.27 rmind }
820 1.27 rmind
821 1.20 rmind void
822 1.20 rmind npf_nat_getmap(nl_nat_t *nt, npf_addr_t *addr, size_t *alen, in_port_t *port)
823 1.20 rmind {
824 1.43.12.1 pgoyette const void *data = nvlist_get_binary(nt->rule_dict, "nat-ip", alen);
825 1.43.12.1 pgoyette memcpy(addr, data, *alen);
826 1.43.12.1 pgoyette *port = (uint16_t)dnvlist_get_number(nt->rule_dict, "nat-port", 0);
827 1.20 rmind }
828 1.20 rmind
829 1.1 rmind /*
830 1.1 rmind * TABLE INTERFACE.
831 1.1 rmind */
832 1.1 rmind
833 1.1 rmind nl_table_t *
834 1.43.12.1 pgoyette npf_table_create(const char *name, unsigned id, int type)
835 1.1 rmind {
836 1.1 rmind nl_table_t *tl;
837 1.1 rmind
838 1.5 christos tl = malloc(sizeof(*tl));
839 1.43.12.1 pgoyette if (!tl) {
840 1.1 rmind return NULL;
841 1.1 rmind }
842 1.43.12.1 pgoyette tl->table_dict = nvlist_create(0);
843 1.43.12.1 pgoyette nvlist_add_string(tl->table_dict, "name", name);
844 1.43.12.1 pgoyette nvlist_add_number(tl->table_dict, "id", id);
845 1.43.12.1 pgoyette nvlist_add_number(tl->table_dict, "type", type);
846 1.1 rmind return tl;
847 1.1 rmind }
848 1.1 rmind
849 1.1 rmind int
850 1.15 rmind npf_table_add_entry(nl_table_t *tl, int af, const npf_addr_t *addr,
851 1.15 rmind const npf_netmask_t mask)
852 1.1 rmind {
853 1.43.12.1 pgoyette nvlist_t *entry;
854 1.1 rmind
855 1.1 rmind /* Create the table entry. */
856 1.43.12.1 pgoyette entry = nvlist_create(0);
857 1.43.12.1 pgoyette if (!entry) {
858 1.1 rmind return ENOMEM;
859 1.1 rmind }
860 1.43.12.1 pgoyette if (!_npf_add_addr(entry, "addr", af, addr)) {
861 1.43.12.1 pgoyette nvlist_destroy(entry);
862 1.15 rmind return EINVAL;
863 1.15 rmind }
864 1.43.12.1 pgoyette nvlist_add_number(entry, "mask", mask);
865 1.43.12.1 pgoyette nvlist_append_nvlist_array(tl->table_dict, "entries", entry);
866 1.43.12.1 pgoyette nvlist_destroy(entry);
867 1.1 rmind return 0;
868 1.1 rmind }
869 1.1 rmind
870 1.43.12.1 pgoyette static inline int
871 1.43.12.1 pgoyette _npf_table_build(nl_table_t *tl)
872 1.26 rmind {
873 1.43.12.1 pgoyette struct cdbw *cdbw;
874 1.43.12.1 pgoyette const nvlist_t * const *entries;
875 1.43.12.1 pgoyette int error = 0, fd = -1;
876 1.43.12.1 pgoyette size_t nitems, len;
877 1.43.12.1 pgoyette void *cdb, *buf;
878 1.43.12.1 pgoyette struct stat sb;
879 1.43.12.1 pgoyette char sfn[32];
880 1.26 rmind
881 1.43.12.1 pgoyette if (!nvlist_exists_nvlist_array(tl->table_dict, "entries")) {
882 1.43.12.1 pgoyette return 0;
883 1.26 rmind }
884 1.26 rmind
885 1.43.12.1 pgoyette /*
886 1.43.12.1 pgoyette * Create a constant database and put all the entries.
887 1.43.12.1 pgoyette */
888 1.43.12.1 pgoyette if ((cdbw = cdbw_open()) == NULL) {
889 1.43.12.1 pgoyette return errno;
890 1.43.12.1 pgoyette }
891 1.43.12.1 pgoyette entries = nvlist_get_nvlist_array(tl->table_dict, "entries", &nitems);
892 1.43.12.1 pgoyette for (unsigned i = 0; i < nitems; i++) {
893 1.43.12.1 pgoyette const nvlist_t *entry = entries[i];
894 1.43.12.1 pgoyette const npf_addr_t *addr;
895 1.43.12.1 pgoyette size_t alen;
896 1.43.12.1 pgoyette
897 1.43.12.1 pgoyette addr = dnvlist_get_binary(entry, "addr", &alen, NULL, 0);
898 1.43.12.1 pgoyette if (addr == NULL || alen == 0 || alen > sizeof(npf_addr_t)) {
899 1.43.12.1 pgoyette error = EINVAL;
900 1.43.12.1 pgoyette goto out;
901 1.43.12.1 pgoyette }
902 1.43.12.1 pgoyette if (cdbw_put(cdbw, addr, alen, addr, alen) == -1) {
903 1.43.12.1 pgoyette error = errno;
904 1.43.12.1 pgoyette goto out;
905 1.43.12.1 pgoyette }
906 1.43.12.1 pgoyette }
907 1.1 rmind
908 1.43.12.1 pgoyette /*
909 1.43.12.1 pgoyette * Produce the constant database into a temporary file.
910 1.43.12.1 pgoyette */
911 1.43.12.1 pgoyette strncpy(sfn, "/tmp/npfcdb.XXXXXX", sizeof(sfn));
912 1.43.12.1 pgoyette sfn[sizeof(sfn) - 1] = '\0';
913 1.24 rmind
914 1.43.12.1 pgoyette if ((fd = mkstemp(sfn)) == -1) {
915 1.43.12.1 pgoyette error = errno;
916 1.43.12.1 pgoyette goto out;
917 1.1 rmind }
918 1.43.12.1 pgoyette unlink(sfn);
919 1.43.12.1 pgoyette
920 1.43.12.1 pgoyette if (cdbw_output(cdbw, fd, "npf-table-cdb", NULL) == -1) {
921 1.43.12.1 pgoyette error = errno;
922 1.43.12.1 pgoyette goto out;
923 1.43.12.1 pgoyette }
924 1.43.12.1 pgoyette if (fstat(fd, &sb) == -1) {
925 1.43.12.1 pgoyette error = errno;
926 1.43.12.1 pgoyette goto out;
927 1.43.12.1 pgoyette }
928 1.43.12.1 pgoyette len = sb.st_size;
929 1.43.12.1 pgoyette
930 1.43.12.1 pgoyette /*
931 1.43.12.1 pgoyette * Memory-map the database and copy it into a buffer.
932 1.43.12.1 pgoyette */
933 1.43.12.1 pgoyette buf = malloc(len);
934 1.43.12.1 pgoyette if (!buf) {
935 1.43.12.1 pgoyette error = ENOMEM;
936 1.43.12.1 pgoyette goto out;
937 1.43.12.1 pgoyette }
938 1.43.12.1 pgoyette cdb = mmap(NULL, len, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
939 1.43.12.1 pgoyette if (cdb == MAP_FAILED) {
940 1.43.12.1 pgoyette error = errno;
941 1.43.12.1 pgoyette free(buf);
942 1.43.12.1 pgoyette goto out;
943 1.43.12.1 pgoyette }
944 1.43.12.1 pgoyette munmap(cdb, len);
945 1.43.12.1 pgoyette
946 1.43.12.1 pgoyette /*
947 1.43.12.1 pgoyette * Move the data buffer to the nvlist.
948 1.43.12.1 pgoyette */
949 1.43.12.1 pgoyette nvlist_move_binary(tl->table_dict, "data", buf, len);
950 1.43.12.1 pgoyette error = nvlist_error(tl->table_dict);
951 1.43.12.1 pgoyette out:
952 1.43.12.1 pgoyette if (fd != -1) {
953 1.43.12.1 pgoyette close(fd);
954 1.43.12.1 pgoyette }
955 1.43.12.1 pgoyette cdbw_close(cdbw);
956 1.43.12.1 pgoyette return error;
957 1.1 rmind }
958 1.1 rmind
959 1.1 rmind int
960 1.1 rmind npf_table_insert(nl_config_t *ncf, nl_table_t *tl)
961 1.1 rmind {
962 1.43.12.1 pgoyette const char *name;
963 1.43.12.1 pgoyette int error;
964 1.1 rmind
965 1.43.12.1 pgoyette name = dnvlist_get_string(tl->table_dict, "name", NULL);
966 1.43.12.1 pgoyette if (!name) {
967 1.1 rmind return EINVAL;
968 1.1 rmind }
969 1.43.12.1 pgoyette if (_npf_dataset_lookup(ncf->ncf_dict, "tables", "name", name)) {
970 1.1 rmind return EEXIST;
971 1.1 rmind }
972 1.43.12.1 pgoyette if (dnvlist_get_number(tl->table_dict, "type", 0) == NPF_TABLE_CDB) {
973 1.43.12.1 pgoyette if ((error = _npf_table_build(tl)) != 0) {
974 1.43.12.1 pgoyette return error;
975 1.43.12.1 pgoyette }
976 1.43.12.1 pgoyette }
977 1.43.12.1 pgoyette nvlist_append_nvlist_array(ncf->ncf_dict, "tables", tl->table_dict);
978 1.43.12.1 pgoyette nvlist_destroy(tl->table_dict);
979 1.43.12.1 pgoyette free(tl);
980 1.1 rmind return 0;
981 1.1 rmind }
982 1.1 rmind
983 1.20 rmind nl_table_t *
984 1.20 rmind npf_table_iterate(nl_config_t *ncf)
985 1.20 rmind {
986 1.43.12.1 pgoyette const nvlist_t *table_dict;
987 1.43.12.1 pgoyette unsigned i = ncf->ncf_table_iter++;
988 1.20 rmind
989 1.43.12.1 pgoyette table_dict = _npf_dataset_getelement(ncf->ncf_dict, "tables", i);
990 1.43.12.1 pgoyette if (!table_dict) {
991 1.43.12.1 pgoyette /* Reset the iterator. */
992 1.43.12.1 pgoyette ncf->ncf_table_iter = 0;
993 1.20 rmind return NULL;
994 1.20 rmind }
995 1.43.12.1 pgoyette ncf->ncf_cur_table.table_dict = __UNCONST(table_dict); // XXX
996 1.20 rmind return &ncf->ncf_cur_table;
997 1.20 rmind }
998 1.20 rmind
999 1.20 rmind unsigned
1000 1.20 rmind npf_table_getid(nl_table_t *tl)
1001 1.20 rmind {
1002 1.43.12.1 pgoyette return dnvlist_get_number(tl->table_dict, "id", (unsigned)-1);
1003 1.20 rmind }
1004 1.20 rmind
1005 1.23 rmind const char *
1006 1.23 rmind npf_table_getname(nl_table_t *tl)
1007 1.23 rmind {
1008 1.43.12.1 pgoyette return dnvlist_get_string(tl->table_dict, "name", NULL);
1009 1.23 rmind }
1010 1.23 rmind
1011 1.20 rmind int
1012 1.20 rmind npf_table_gettype(nl_table_t *tl)
1013 1.20 rmind {
1014 1.43.12.1 pgoyette return dnvlist_get_number(tl->table_dict, "type", 0);
1015 1.20 rmind }
1016 1.20 rmind
1017 1.1 rmind void
1018 1.1 rmind npf_table_destroy(nl_table_t *tl)
1019 1.1 rmind {
1020 1.43.12.1 pgoyette nvlist_destroy(tl->table_dict);
1021 1.1 rmind free(tl);
1022 1.1 rmind }
1023 1.1 rmind
1024 1.1 rmind /*
1025 1.19 christos * ALG INTERFACE.
1026 1.19 christos */
1027 1.19 christos
1028 1.19 christos int
1029 1.19 christos _npf_alg_load(nl_config_t *ncf, const char *name)
1030 1.19 christos {
1031 1.43.12.1 pgoyette nvlist_t *alg_dict;
1032 1.19 christos
1033 1.43.12.1 pgoyette if (_npf_dataset_lookup(ncf->ncf_dict, "algs", "name", name)) {
1034 1.19 christos return EEXIST;
1035 1.43.12.1 pgoyette }
1036 1.43.12.1 pgoyette alg_dict = nvlist_create(0);
1037 1.43.12.1 pgoyette nvlist_add_string(alg_dict, "name", name);
1038 1.43.12.1 pgoyette nvlist_append_nvlist_array(ncf->ncf_dict, "algs", alg_dict);
1039 1.43.12.1 pgoyette nvlist_destroy(alg_dict);
1040 1.19 christos return 0;
1041 1.19 christos }
1042 1.19 christos
1043 1.19 christos int
1044 1.19 christos _npf_alg_unload(nl_config_t *ncf, const char *name)
1045 1.19 christos {
1046 1.43.12.1 pgoyette if (!_npf_dataset_lookup(ncf->ncf_dict, "algs", "name", name)) {
1047 1.19 christos return ENOENT;
1048 1.43.12.1 pgoyette }
1049 1.19 christos return ENOTSUP;
1050 1.19 christos }
1051 1.19 christos
1052 1.19 christos /*
1053 1.43.12.1 pgoyette * CONNECTION / NAT ENTRY INTERFACE.
1054 1.1 rmind */
1055 1.1 rmind
1056 1.36 christos int
1057 1.39 christos npf_nat_lookup(int fd, int af, npf_addr_t *addr[2], in_port_t port[2],
1058 1.36 christos int proto, int dir)
1059 1.36 christos {
1060 1.43.12.1 pgoyette nvlist_t *req = NULL, *conn_res;
1061 1.43.12.1 pgoyette const nvlist_t *nat;
1062 1.36 christos int error = EINVAL;
1063 1.36 christos
1064 1.43.12.1 pgoyette /*
1065 1.43.12.1 pgoyette * Setup the connection lookup key.
1066 1.43.12.1 pgoyette */
1067 1.43.12.1 pgoyette conn_res = nvlist_create(0);
1068 1.43.12.1 pgoyette if (!conn_res) {
1069 1.36 christos return ENOMEM;
1070 1.43.12.1 pgoyette }
1071 1.38 christos if (!_npf_add_addr(conn_res, "saddr", af, addr[0]))
1072 1.38 christos goto out;
1073 1.38 christos if (!_npf_add_addr(conn_res, "daddr", af, addr[1]))
1074 1.38 christos goto out;
1075 1.43.12.1 pgoyette nvlist_add_number(conn_res, "sport", port[0]);
1076 1.43.12.1 pgoyette nvlist_add_number(conn_res, "dport", port[1]);
1077 1.43.12.1 pgoyette nvlist_add_number(conn_res, "proto", proto);
1078 1.43.12.1 pgoyette
1079 1.43.12.1 pgoyette /*
1080 1.43.12.1 pgoyette * Setup the request.
1081 1.43.12.1 pgoyette */
1082 1.43.12.1 pgoyette req = nvlist_create(0);
1083 1.43.12.1 pgoyette if (!req) {
1084 1.43.12.1 pgoyette error = ENOMEM;
1085 1.38 christos goto out;
1086 1.43.12.1 pgoyette }
1087 1.43.12.1 pgoyette nvlist_add_number(req, "direction", dir);
1088 1.43.12.1 pgoyette nvlist_move_nvlist(req, "key", conn_res);
1089 1.43 christos conn_res = NULL;
1090 1.36 christos
1091 1.43.12.1 pgoyette /* Lookup: retrieve the connection entry. */
1092 1.43.12.1 pgoyette if (nvlist_xfer_ioctl(fd, IOC_NPF_CONN_LOOKUP, req, &conn_res) == -1) {
1093 1.43.12.1 pgoyette error = errno;
1094 1.36 christos goto out;
1095 1.43.12.1 pgoyette }
1096 1.36 christos
1097 1.43.12.1 pgoyette /*
1098 1.43.12.1 pgoyette * Get the NAT entry and extract the translated pair.
1099 1.43.12.1 pgoyette */
1100 1.43.12.1 pgoyette nat = dnvlist_get_nvlist(conn_res, "nat", NULL);
1101 1.43.12.1 pgoyette if (!nat) {
1102 1.36 christos errno = ENOENT;
1103 1.36 christos goto out;
1104 1.36 christos }
1105 1.36 christos if (!_npf_get_addr(nat, "oaddr", addr[0])) {
1106 1.36 christos error = EINVAL;
1107 1.36 christos goto out;
1108 1.36 christos }
1109 1.43.12.1 pgoyette port[0] = nvlist_get_number(nat, "oport");
1110 1.43.12.1 pgoyette port[1] = nvlist_get_number(nat, "tport");
1111 1.36 christos out:
1112 1.43.12.1 pgoyette if (conn_res) {
1113 1.43.12.1 pgoyette nvlist_destroy(conn_res);
1114 1.43.12.1 pgoyette }
1115 1.43.12.1 pgoyette if (req) {
1116 1.43.12.1 pgoyette nvlist_destroy(req);
1117 1.43.12.1 pgoyette }
1118 1.36 christos return error;
1119 1.36 christos }
1120 1.41 christos
1121 1.43.12.1 pgoyette typedef struct {
1122 1.43.12.1 pgoyette npf_addr_t addr[2];
1123 1.43.12.1 pgoyette in_port_t port[2];
1124 1.43.12.1 pgoyette uint16_t alen;
1125 1.43.12.1 pgoyette uint16_t proto;
1126 1.43.12.1 pgoyette } npf_endpoint_t;
1127 1.41 christos
1128 1.41 christos static bool
1129 1.43.12.1 pgoyette npf_endpoint_load(const nvlist_t *conn, const char *name, npf_endpoint_t *ep)
1130 1.41 christos {
1131 1.43.12.1 pgoyette const nvlist_t *ed = dnvlist_get_nvlist(conn, name, NULL);
1132 1.43.12.1 pgoyette
1133 1.43.12.1 pgoyette if (!ed)
1134 1.41 christos return false;
1135 1.41 christos if (!(ep->alen = _npf_get_addr(ed, "saddr", &ep->addr[0])))
1136 1.41 christos return false;
1137 1.41 christos if (ep->alen != _npf_get_addr(ed, "daddr", &ep->addr[1]))
1138 1.41 christos return false;
1139 1.43.12.1 pgoyette ep->port[0] = nvlist_get_number(ed, "sport");
1140 1.43.12.1 pgoyette ep->port[1] = nvlist_get_number(ed, "dport");
1141 1.43.12.1 pgoyette ep->proto = nvlist_get_number(ed, "proto");
1142 1.41 christos return true;
1143 1.41 christos }
1144 1.41 christos
1145 1.41 christos static void
1146 1.43.12.1 pgoyette npf_conn_handle(const nvlist_t *conn, npf_conn_func_t func, void *arg)
1147 1.41 christos {
1148 1.43.12.1 pgoyette const nvlist_t *nat;
1149 1.43.12.1 pgoyette npf_endpoint_t ep;
1150 1.41 christos uint16_t tport;
1151 1.41 christos const char *ifname;
1152 1.41 christos
1153 1.43.12.1 pgoyette ifname = dnvlist_get_string(conn, "ifname", NULL);
1154 1.43.12.1 pgoyette if (!ifname)
1155 1.41 christos goto err;
1156 1.41 christos
1157 1.43.12.1 pgoyette if ((nat = dnvlist_get_nvlist(conn, "nat", NULL)) != NULL) {
1158 1.43.12.1 pgoyette tport = nvlist_get_number(nat, "tport");
1159 1.41 christos } else {
1160 1.41 christos tport = 0;
1161 1.41 christos }
1162 1.43.12.1 pgoyette if (!npf_endpoint_load(conn, "forw-key", &ep)) {
1163 1.41 christos goto err;
1164 1.43.12.1 pgoyette }
1165 1.41 christos
1166 1.41 christos in_port_t p[] = {
1167 1.41 christos ntohs(ep.port[0]),
1168 1.41 christos ntohs(ep.port[1]),
1169 1.41 christos ntohs(tport)
1170 1.41 christos };
1171 1.43.12.1 pgoyette (*func)((unsigned)ep.alen, ep.addr, p, ifname, arg);
1172 1.41 christos err:
1173 1.41 christos return;
1174 1.41 christos }
1175 1.41 christos
1176 1.41 christos int
1177 1.43.12.1 pgoyette npf_conn_list(int fd, npf_conn_func_t func, void *arg)
1178 1.41 christos {
1179 1.41 christos nl_config_t *ncf;
1180 1.43.12.1 pgoyette const nvlist_t * const *conns;
1181 1.43.12.1 pgoyette size_t nitems;
1182 1.41 christos
1183 1.41 christos ncf = npf_config_retrieve(fd);
1184 1.43.12.1 pgoyette if (!ncf) {
1185 1.41 christos return errno;
1186 1.41 christos }
1187 1.43.12.1 pgoyette if (!nvlist_exists_nvlist_array(ncf->ncf_dict, "conn-list")) {
1188 1.43.12.1 pgoyette return 0;
1189 1.42 rmind }
1190 1.43.12.1 pgoyette conns = nvlist_get_nvlist_array(ncf->ncf_dict, "conn-list", &nitems);
1191 1.43.12.1 pgoyette for (unsigned i = 0; i < nitems; i++) {
1192 1.43.12.1 pgoyette const nvlist_t *conn = conns[i];
1193 1.43.12.1 pgoyette npf_conn_handle(conn, func, arg);
1194 1.41 christos }
1195 1.41 christos return 0;
1196 1.41 christos }
1197 1.43.12.1 pgoyette
1198 1.43.12.1 pgoyette /*
1199 1.43.12.1 pgoyette * MISC.
1200 1.43.12.1 pgoyette */
1201 1.43.12.1 pgoyette
1202 1.43.12.1 pgoyette void
1203 1.43.12.1 pgoyette _npf_debug_addif(nl_config_t *ncf, const char *ifname)
1204 1.43.12.1 pgoyette {
1205 1.43.12.1 pgoyette nvlist_t *debug;
1206 1.43.12.1 pgoyette
1207 1.43.12.1 pgoyette /*
1208 1.43.12.1 pgoyette * Initialise the debug dictionary on the first call.
1209 1.43.12.1 pgoyette */
1210 1.43.12.1 pgoyette debug = dnvlist_take_nvlist(ncf->ncf_dict, "debug", NULL);
1211 1.43.12.1 pgoyette if (debug == NULL) {
1212 1.43.12.1 pgoyette debug = nvlist_create(0);
1213 1.43.12.1 pgoyette }
1214 1.43.12.1 pgoyette if (!_npf_dataset_lookup(debug, "interfaces", "name", ifname)) {
1215 1.43.12.1 pgoyette nvlist_t *ifdict = nvlist_create(0);
1216 1.43.12.1 pgoyette nvlist_add_string(ifdict, "name", ifname);
1217 1.43.12.1 pgoyette nvlist_add_number(ifdict, "index", if_nametoindex(ifname));
1218 1.43.12.1 pgoyette nvlist_append_nvlist_array(debug, "interfaces", ifdict);
1219 1.43.12.1 pgoyette nvlist_destroy(ifdict);
1220 1.43.12.1 pgoyette }
1221 1.43.12.1 pgoyette nvlist_move_nvlist(ncf->ncf_dict, "debug", debug);
1222 1.43.12.1 pgoyette }
1223 1.43.12.1 pgoyette
1224 1.43.12.1 pgoyette void
1225 1.43.12.1 pgoyette _npf_config_dump(nl_config_t *ncf, int fd)
1226 1.43.12.1 pgoyette {
1227 1.43.12.1 pgoyette (void)npf_config_build(ncf);
1228 1.43.12.1 pgoyette nvlist_dump(ncf->ncf_dict, fd);
1229 1.43.12.1 pgoyette }
1230