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