npf.c revision 1.36 1 1.36 christos /* $NetBSD: npf.c,v 1.36 2016/12/10 05:37:55 christos Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.35 rmind * Copyright (c) 2010-2015 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.1 rmind #include <sys/cdefs.h>
33 1.36 christos __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.36 2016/12/10 05:37:55 christos Exp $");
34 1.1 rmind
35 1.1 rmind #include <sys/types.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 #include <prop/proplib.h>
40 1.1 rmind
41 1.1 rmind #include <stdlib.h>
42 1.1 rmind #include <string.h>
43 1.7 rmind #include <assert.h>
44 1.1 rmind #include <errno.h>
45 1.1 rmind #include <err.h>
46 1.1 rmind
47 1.1 rmind #define _NPF_PRIVATE
48 1.1 rmind #include "npf.h"
49 1.1 rmind
50 1.1 rmind struct nl_rule {
51 1.1 rmind prop_dictionary_t nrl_dict;
52 1.1 rmind };
53 1.1 rmind
54 1.1 rmind struct nl_rproc {
55 1.1 rmind prop_dictionary_t nrp_dict;
56 1.1 rmind };
57 1.1 rmind
58 1.1 rmind struct nl_table {
59 1.1 rmind prop_dictionary_t ntl_dict;
60 1.1 rmind };
61 1.1 rmind
62 1.19 christos struct nl_alg {
63 1.19 christos prop_dictionary_t nal_dict;
64 1.19 christos };
65 1.19 christos
66 1.13 rmind struct nl_ext {
67 1.13 rmind const char * nxt_name;
68 1.13 rmind prop_dictionary_t nxt_dict;
69 1.13 rmind };
70 1.13 rmind
71 1.20 rmind struct nl_config {
72 1.34 rmind /* Rules, translations, procedures, tables, connections. */
73 1.20 rmind prop_dictionary_t ncf_dict;
74 1.20 rmind prop_array_t ncf_alg_list;
75 1.20 rmind prop_array_t ncf_rules_list;
76 1.20 rmind prop_array_t ncf_rproc_list;
77 1.20 rmind prop_array_t ncf_table_list;
78 1.20 rmind prop_array_t ncf_nat_list;
79 1.34 rmind prop_array_t ncf_conn_list;
80 1.20 rmind
81 1.20 rmind /* Iterators. */
82 1.20 rmind prop_object_iterator_t ncf_rule_iter;
83 1.20 rmind unsigned ncf_reduce[16];
84 1.20 rmind unsigned ncf_nlevel;
85 1.20 rmind unsigned ncf_counter;
86 1.20 rmind nl_rule_t ncf_cur_rule;
87 1.20 rmind
88 1.20 rmind prop_object_iterator_t ncf_table_iter;
89 1.20 rmind nl_table_t ncf_cur_table;
90 1.20 rmind
91 1.20 rmind prop_object_iterator_t ncf_rproc_iter;
92 1.20 rmind nl_rproc_t ncf_cur_rproc;
93 1.20 rmind
94 1.20 rmind /* Error report and debug information. */
95 1.20 rmind prop_dictionary_t ncf_err;
96 1.20 rmind prop_dictionary_t ncf_debug;
97 1.20 rmind
98 1.20 rmind /* Custom file to externalise property-list. */
99 1.20 rmind const char * ncf_plist;
100 1.20 rmind bool ncf_flush;
101 1.20 rmind };
102 1.20 rmind
103 1.16 rmind static prop_array_t _npf_ruleset_transform(prop_array_t);
104 1.16 rmind
105 1.36 christos static bool
106 1.36 christos _npf_add_addr(prop_dictionary_t dict, const char *name, int af,
107 1.36 christos const npf_addr_t *addr)
108 1.36 christos {
109 1.36 christos size_t sz;
110 1.36 christos
111 1.36 christos if (af == AF_INET) {
112 1.36 christos sz = sizeof(struct in_addr);
113 1.36 christos } else if (af == AF_INET6) {
114 1.36 christos sz = sizeof(struct in6_addr);
115 1.36 christos } else {
116 1.36 christos return false;
117 1.36 christos }
118 1.36 christos prop_data_t addrdat = prop_data_create_data(addr, sz);
119 1.36 christos if (addrdat == NULL) {
120 1.36 christos return false;
121 1.36 christos }
122 1.36 christos prop_dictionary_set(dict, name, addrdat);
123 1.36 christos prop_object_release(addrdat);
124 1.36 christos return true;
125 1.36 christos }
126 1.36 christos
127 1.36 christos static bool
128 1.36 christos _npf_get_addr(prop_dictionary_t dict, const char *name, npf_addr_t *addr)
129 1.36 christos {
130 1.36 christos prop_object_t obj = prop_dictionary_get(dict, name);
131 1.36 christos const void *d = prop_data_data_nocopy(obj);
132 1.36 christos
133 1.36 christos if (d == NULL)
134 1.36 christos return false;
135 1.36 christos
136 1.36 christos size_t sz = prop_data_size(obj);
137 1.36 christos switch (sz) {
138 1.36 christos case sizeof(struct in_addr):
139 1.36 christos case sizeof(struct in6_addr):
140 1.36 christos memcpy(addr, d, sz);
141 1.36 christos return true;
142 1.36 christos default:
143 1.36 christos return false;
144 1.36 christos }
145 1.36 christos }
146 1.36 christos
147 1.1 rmind /*
148 1.1 rmind * CONFIGURATION INTERFACE.
149 1.1 rmind */
150 1.1 rmind
151 1.1 rmind nl_config_t *
152 1.1 rmind npf_config_create(void)
153 1.1 rmind {
154 1.1 rmind nl_config_t *ncf;
155 1.1 rmind
156 1.7 rmind ncf = calloc(1, sizeof(*ncf));
157 1.1 rmind if (ncf == NULL) {
158 1.1 rmind return NULL;
159 1.1 rmind }
160 1.19 christos ncf->ncf_alg_list = prop_array_create();
161 1.1 rmind ncf->ncf_rules_list = prop_array_create();
162 1.1 rmind ncf->ncf_rproc_list = prop_array_create();
163 1.1 rmind ncf->ncf_table_list = prop_array_create();
164 1.1 rmind ncf->ncf_nat_list = prop_array_create();
165 1.1 rmind
166 1.4 rmind ncf->ncf_plist = NULL;
167 1.6 rmind ncf->ncf_flush = false;
168 1.4 rmind
169 1.1 rmind return ncf;
170 1.1 rmind }
171 1.1 rmind
172 1.1 rmind int
173 1.1 rmind npf_config_submit(nl_config_t *ncf, int fd)
174 1.1 rmind {
175 1.7 rmind const char *plist = ncf->ncf_plist;
176 1.1 rmind prop_dictionary_t npf_dict;
177 1.16 rmind prop_array_t rlset;
178 1.1 rmind int error = 0;
179 1.1 rmind
180 1.1 rmind npf_dict = prop_dictionary_create();
181 1.1 rmind if (npf_dict == NULL) {
182 1.1 rmind return ENOMEM;
183 1.1 rmind }
184 1.15 rmind prop_dictionary_set_uint32(npf_dict, "version", NPF_VERSION);
185 1.16 rmind
186 1.16 rmind rlset = _npf_ruleset_transform(ncf->ncf_rules_list);
187 1.16 rmind if (rlset == NULL) {
188 1.16 rmind prop_object_release(npf_dict);
189 1.16 rmind return ENOMEM;
190 1.16 rmind }
191 1.20 rmind prop_object_release(ncf->ncf_rules_list);
192 1.20 rmind ncf->ncf_rules_list = rlset;
193 1.16 rmind
194 1.20 rmind prop_dictionary_set(npf_dict, "rules", ncf->ncf_rules_list);
195 1.19 christos prop_dictionary_set(npf_dict, "algs", ncf->ncf_alg_list);
196 1.1 rmind prop_dictionary_set(npf_dict, "rprocs", ncf->ncf_rproc_list);
197 1.1 rmind prop_dictionary_set(npf_dict, "tables", ncf->ncf_table_list);
198 1.32 rmind prop_dictionary_set(npf_dict, "nat", ncf->ncf_nat_list);
199 1.34 rmind if (ncf->ncf_conn_list) {
200 1.34 rmind prop_dictionary_set(npf_dict, "conn-list",
201 1.34 rmind ncf->ncf_conn_list);
202 1.34 rmind }
203 1.6 rmind prop_dictionary_set_bool(npf_dict, "flush", ncf->ncf_flush);
204 1.15 rmind if (ncf->ncf_debug) {
205 1.15 rmind prop_dictionary_set(npf_dict, "debug", ncf->ncf_debug);
206 1.15 rmind }
207 1.1 rmind
208 1.4 rmind if (plist) {
209 1.4 rmind if (!prop_dictionary_externalize_to_file(npf_dict, plist)) {
210 1.4 rmind error = errno;
211 1.4 rmind }
212 1.7 rmind prop_object_release(npf_dict);
213 1.7 rmind return error;
214 1.7 rmind }
215 1.20 rmind if (fd) {
216 1.20 rmind error = prop_dictionary_sendrecv_ioctl(npf_dict, fd,
217 1.30 rmind IOC_NPF_LOAD, &ncf->ncf_err);
218 1.20 rmind if (error) {
219 1.20 rmind prop_object_release(npf_dict);
220 1.20 rmind assert(ncf->ncf_err == NULL);
221 1.20 rmind return error;
222 1.20 rmind }
223 1.20 rmind prop_dictionary_get_int32(ncf->ncf_err, "errno", &error);
224 1.1 rmind }
225 1.1 rmind prop_object_release(npf_dict);
226 1.1 rmind return error;
227 1.1 rmind }
228 1.1 rmind
229 1.30 rmind static nl_config_t *
230 1.30 rmind _npf_config_consdict(prop_dictionary_t npf_dict)
231 1.30 rmind {
232 1.30 rmind nl_config_t *ncf;
233 1.30 rmind
234 1.30 rmind ncf = calloc(1, sizeof(*ncf));
235 1.30 rmind if (ncf == NULL) {
236 1.30 rmind return NULL;
237 1.30 rmind }
238 1.30 rmind ncf->ncf_dict = npf_dict;
239 1.30 rmind ncf->ncf_alg_list = prop_dictionary_get(npf_dict, "algs");
240 1.30 rmind ncf->ncf_rules_list = prop_dictionary_get(npf_dict, "rules");
241 1.30 rmind ncf->ncf_rproc_list = prop_dictionary_get(npf_dict, "rprocs");
242 1.30 rmind ncf->ncf_table_list = prop_dictionary_get(npf_dict, "tables");
243 1.32 rmind ncf->ncf_nat_list = prop_dictionary_get(npf_dict, "nat");
244 1.34 rmind ncf->ncf_conn_list = prop_dictionary_get(npf_dict, "conn-list");
245 1.30 rmind return ncf;
246 1.30 rmind }
247 1.30 rmind
248 1.8 rmind nl_config_t *
249 1.8 rmind npf_config_retrieve(int fd, bool *active, bool *loaded)
250 1.8 rmind {
251 1.8 rmind prop_dictionary_t npf_dict;
252 1.8 rmind nl_config_t *ncf;
253 1.8 rmind int error;
254 1.8 rmind
255 1.30 rmind error = prop_dictionary_recv_ioctl(fd, IOC_NPF_SAVE, &npf_dict);
256 1.8 rmind if (error) {
257 1.8 rmind return NULL;
258 1.8 rmind }
259 1.30 rmind ncf = _npf_config_consdict(npf_dict);
260 1.8 rmind if (ncf == NULL) {
261 1.8 rmind prop_object_release(npf_dict);
262 1.8 rmind return NULL;
263 1.8 rmind }
264 1.8 rmind prop_dictionary_get_bool(npf_dict, "active", active);
265 1.8 rmind *loaded = (ncf->ncf_rules_list != NULL);
266 1.8 rmind return ncf;
267 1.8 rmind }
268 1.8 rmind
269 1.6 rmind int
270 1.30 rmind npf_config_export(const nl_config_t *ncf, const char *path)
271 1.30 rmind {
272 1.30 rmind prop_dictionary_t npf_dict = ncf->ncf_dict;
273 1.30 rmind int error = 0;
274 1.30 rmind
275 1.30 rmind if (!prop_dictionary_externalize_to_file(npf_dict, path)) {
276 1.30 rmind error = errno;
277 1.30 rmind }
278 1.31 htodd return error;
279 1.30 rmind }
280 1.30 rmind
281 1.30 rmind nl_config_t *
282 1.30 rmind npf_config_import(const char *path)
283 1.30 rmind {
284 1.30 rmind prop_dictionary_t npf_dict;
285 1.30 rmind nl_config_t *ncf;
286 1.30 rmind
287 1.30 rmind npf_dict = prop_dictionary_internalize_from_file(path);
288 1.33 rmind if (!npf_dict) {
289 1.30 rmind return NULL;
290 1.30 rmind }
291 1.30 rmind ncf = _npf_config_consdict(npf_dict);
292 1.33 rmind if (!ncf) {
293 1.30 rmind prop_object_release(npf_dict);
294 1.30 rmind return NULL;
295 1.30 rmind }
296 1.30 rmind return ncf;
297 1.30 rmind }
298 1.30 rmind
299 1.30 rmind int
300 1.6 rmind npf_config_flush(int fd)
301 1.6 rmind {
302 1.6 rmind nl_config_t *ncf;
303 1.6 rmind int error;
304 1.6 rmind
305 1.6 rmind ncf = npf_config_create();
306 1.6 rmind if (ncf == NULL) {
307 1.6 rmind return ENOMEM;
308 1.6 rmind }
309 1.6 rmind ncf->ncf_flush = true;
310 1.6 rmind error = npf_config_submit(ncf, fd);
311 1.6 rmind npf_config_destroy(ncf);
312 1.6 rmind return error;
313 1.6 rmind }
314 1.6 rmind
315 1.1 rmind void
316 1.7 rmind _npf_config_error(nl_config_t *ncf, nl_error_t *ne)
317 1.7 rmind {
318 1.7 rmind memset(ne, 0, sizeof(*ne));
319 1.7 rmind prop_dictionary_get_int32(ncf->ncf_err, "id", &ne->ne_id);
320 1.7 rmind prop_dictionary_get_cstring(ncf->ncf_err,
321 1.7 rmind "source-file", &ne->ne_source_file);
322 1.7 rmind prop_dictionary_get_uint32(ncf->ncf_err,
323 1.7 rmind "source-line", &ne->ne_source_line);
324 1.7 rmind prop_dictionary_get_int32(ncf->ncf_err,
325 1.16 rmind "code-error", &ne->ne_ncode_error);
326 1.7 rmind prop_dictionary_get_int32(ncf->ncf_err,
327 1.16 rmind "code-errat", &ne->ne_ncode_errat);
328 1.7 rmind }
329 1.7 rmind
330 1.7 rmind void
331 1.1 rmind npf_config_destroy(nl_config_t *ncf)
332 1.1 rmind {
333 1.14 rmind if (!ncf->ncf_dict) {
334 1.19 christos prop_object_release(ncf->ncf_alg_list);
335 1.8 rmind prop_object_release(ncf->ncf_rules_list);
336 1.8 rmind prop_object_release(ncf->ncf_rproc_list);
337 1.8 rmind prop_object_release(ncf->ncf_table_list);
338 1.8 rmind prop_object_release(ncf->ncf_nat_list);
339 1.8 rmind }
340 1.7 rmind if (ncf->ncf_err) {
341 1.7 rmind prop_object_release(ncf->ncf_err);
342 1.7 rmind }
343 1.11 rmind if (ncf->ncf_debug) {
344 1.11 rmind prop_object_release(ncf->ncf_debug);
345 1.11 rmind }
346 1.1 rmind free(ncf);
347 1.1 rmind }
348 1.1 rmind
349 1.4 rmind void
350 1.4 rmind _npf_config_setsubmit(nl_config_t *ncf, const char *plist_file)
351 1.4 rmind {
352 1.4 rmind ncf->ncf_plist = plist_file;
353 1.4 rmind }
354 1.4 rmind
355 1.1 rmind static bool
356 1.1 rmind _npf_prop_array_lookup(prop_array_t array, const char *key, const char *name)
357 1.1 rmind {
358 1.1 rmind prop_dictionary_t dict;
359 1.1 rmind prop_object_iterator_t it;
360 1.1 rmind
361 1.1 rmind it = prop_array_iterator(array);
362 1.1 rmind while ((dict = prop_object_iterator_next(it)) != NULL) {
363 1.1 rmind const char *lname;
364 1.1 rmind prop_dictionary_get_cstring_nocopy(dict, key, &lname);
365 1.1 rmind if (strcmp(name, lname) == 0)
366 1.1 rmind break;
367 1.1 rmind }
368 1.1 rmind prop_object_iterator_release(it);
369 1.1 rmind return dict ? true : false;
370 1.1 rmind }
371 1.1 rmind
372 1.1 rmind /*
373 1.16 rmind * DYNAMIC RULESET INTERFACE.
374 1.16 rmind */
375 1.16 rmind
376 1.16 rmind int
377 1.18 rmind npf_ruleset_add(int fd, const char *rname, nl_rule_t *rl, uint64_t *id)
378 1.16 rmind {
379 1.16 rmind prop_dictionary_t rldict = rl->nrl_dict;
380 1.16 rmind prop_dictionary_t ret;
381 1.16 rmind int error;
382 1.16 rmind
383 1.16 rmind prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
384 1.16 rmind prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_ADD);
385 1.16 rmind error = prop_dictionary_sendrecv_ioctl(rldict, fd, IOC_NPF_RULE, &ret);
386 1.16 rmind if (!error) {
387 1.18 rmind prop_dictionary_get_uint64(ret, "id", id);
388 1.16 rmind }
389 1.16 rmind return error;
390 1.16 rmind }
391 1.16 rmind
392 1.16 rmind int
393 1.18 rmind npf_ruleset_remove(int fd, const char *rname, uint64_t id)
394 1.16 rmind {
395 1.16 rmind prop_dictionary_t rldict;
396 1.16 rmind
397 1.16 rmind rldict = prop_dictionary_create();
398 1.16 rmind if (rldict == NULL) {
399 1.16 rmind return ENOMEM;
400 1.16 rmind }
401 1.16 rmind prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
402 1.16 rmind prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_REMOVE);
403 1.18 rmind prop_dictionary_set_uint64(rldict, "id", id);
404 1.16 rmind return prop_dictionary_send_ioctl(rldict, fd, IOC_NPF_RULE);
405 1.16 rmind }
406 1.16 rmind
407 1.16 rmind int
408 1.16 rmind npf_ruleset_remkey(int fd, const char *rname, const void *key, size_t len)
409 1.16 rmind {
410 1.16 rmind prop_dictionary_t rldict;
411 1.16 rmind prop_data_t keyobj;
412 1.16 rmind
413 1.16 rmind rldict = prop_dictionary_create();
414 1.16 rmind if (rldict == NULL) {
415 1.16 rmind return ENOMEM;
416 1.16 rmind }
417 1.16 rmind prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
418 1.16 rmind prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_REMKEY);
419 1.16 rmind
420 1.16 rmind keyobj = prop_data_create_data(key, len);
421 1.16 rmind if (keyobj == NULL) {
422 1.16 rmind prop_object_release(rldict);
423 1.16 rmind return ENOMEM;
424 1.16 rmind }
425 1.16 rmind prop_dictionary_set(rldict, "key", keyobj);
426 1.16 rmind prop_object_release(keyobj);
427 1.16 rmind
428 1.16 rmind return prop_dictionary_send_ioctl(rldict, fd, IOC_NPF_RULE);
429 1.16 rmind }
430 1.16 rmind
431 1.17 rmind int
432 1.17 rmind npf_ruleset_flush(int fd, const char *rname)
433 1.17 rmind {
434 1.17 rmind prop_dictionary_t rldict;
435 1.17 rmind
436 1.17 rmind rldict = prop_dictionary_create();
437 1.17 rmind if (rldict == NULL) {
438 1.17 rmind return ENOMEM;
439 1.17 rmind }
440 1.17 rmind prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
441 1.17 rmind prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_FLUSH);
442 1.17 rmind return prop_dictionary_send_ioctl(rldict, fd, IOC_NPF_RULE);
443 1.17 rmind }
444 1.17 rmind
445 1.16 rmind /*
446 1.16 rmind * _npf_ruleset_transform: transform the ruleset representing nested
447 1.16 rmind * rules with lists into an array.
448 1.16 rmind */
449 1.16 rmind
450 1.16 rmind static void
451 1.16 rmind _npf_ruleset_transform1(prop_array_t rlset, prop_array_t rules)
452 1.16 rmind {
453 1.16 rmind prop_object_iterator_t it;
454 1.16 rmind prop_dictionary_t rldict;
455 1.16 rmind prop_array_t subrlset;
456 1.16 rmind
457 1.16 rmind it = prop_array_iterator(rules);
458 1.16 rmind while ((rldict = prop_object_iterator_next(it)) != NULL) {
459 1.16 rmind unsigned idx;
460 1.16 rmind
461 1.16 rmind /* Add rules to the array (reference is retained). */
462 1.16 rmind prop_array_add(rlset, rldict);
463 1.16 rmind
464 1.16 rmind subrlset = prop_dictionary_get(rldict, "subrules");
465 1.16 rmind if (subrlset) {
466 1.16 rmind /* Process subrules recursively. */
467 1.16 rmind _npf_ruleset_transform1(rlset, subrlset);
468 1.16 rmind /* Add the skip-to position. */
469 1.16 rmind idx = prop_array_count(rlset);
470 1.16 rmind prop_dictionary_set_uint32(rldict, "skip-to", idx);
471 1.16 rmind prop_dictionary_remove(rldict, "subrules");
472 1.16 rmind }
473 1.16 rmind }
474 1.16 rmind prop_object_iterator_release(it);
475 1.16 rmind }
476 1.16 rmind
477 1.16 rmind static prop_array_t
478 1.16 rmind _npf_ruleset_transform(prop_array_t rlset)
479 1.16 rmind {
480 1.16 rmind prop_array_t nrlset;
481 1.16 rmind
482 1.16 rmind nrlset = prop_array_create();
483 1.16 rmind _npf_ruleset_transform1(nrlset, rlset);
484 1.16 rmind return nrlset;
485 1.16 rmind }
486 1.16 rmind
487 1.16 rmind /*
488 1.13 rmind * NPF EXTENSION INTERFACE.
489 1.13 rmind */
490 1.13 rmind
491 1.13 rmind nl_ext_t *
492 1.13 rmind npf_ext_construct(const char *name)
493 1.13 rmind {
494 1.13 rmind nl_ext_t *ext;
495 1.13 rmind
496 1.13 rmind ext = malloc(sizeof(*ext));
497 1.13 rmind if (ext == NULL) {
498 1.13 rmind return NULL;
499 1.13 rmind }
500 1.13 rmind ext->nxt_name = strdup(name);
501 1.13 rmind if (ext->nxt_name == NULL) {
502 1.13 rmind free(ext);
503 1.13 rmind return NULL;
504 1.13 rmind }
505 1.13 rmind ext->nxt_dict = prop_dictionary_create();
506 1.13 rmind
507 1.13 rmind return ext;
508 1.13 rmind }
509 1.13 rmind
510 1.13 rmind void
511 1.13 rmind npf_ext_param_u32(nl_ext_t *ext, const char *key, uint32_t val)
512 1.13 rmind {
513 1.13 rmind prop_dictionary_t extdict = ext->nxt_dict;
514 1.13 rmind prop_dictionary_set_uint32(extdict, key, val);
515 1.13 rmind }
516 1.13 rmind
517 1.13 rmind void
518 1.13 rmind npf_ext_param_bool(nl_ext_t *ext, const char *key, bool val)
519 1.13 rmind {
520 1.13 rmind prop_dictionary_t extdict = ext->nxt_dict;
521 1.13 rmind prop_dictionary_set_bool(extdict, key, val);
522 1.13 rmind }
523 1.13 rmind
524 1.29 jakllsch void
525 1.29 jakllsch npf_ext_param_string(nl_ext_t *ext, const char *key, const char *val)
526 1.29 jakllsch {
527 1.29 jakllsch prop_dictionary_t extdict = ext->nxt_dict;
528 1.29 jakllsch prop_dictionary_set_cstring(extdict, key, val);
529 1.29 jakllsch }
530 1.29 jakllsch
531 1.13 rmind /*
532 1.1 rmind * RULE INTERFACE.
533 1.1 rmind */
534 1.1 rmind
535 1.1 rmind nl_rule_t *
536 1.22 rmind npf_rule_create(const char *name, uint32_t attr, const char *ifname)
537 1.1 rmind {
538 1.1 rmind prop_dictionary_t rldict;
539 1.1 rmind nl_rule_t *rl;
540 1.1 rmind
541 1.5 christos rl = malloc(sizeof(*rl));
542 1.1 rmind if (rl == NULL) {
543 1.1 rmind return NULL;
544 1.1 rmind }
545 1.1 rmind rldict = prop_dictionary_create();
546 1.1 rmind if (rldict == NULL) {
547 1.1 rmind free(rl);
548 1.1 rmind return NULL;
549 1.1 rmind }
550 1.1 rmind if (name) {
551 1.1 rmind prop_dictionary_set_cstring(rldict, "name", name);
552 1.1 rmind }
553 1.32 rmind prop_dictionary_set_uint32(rldict, "attr", attr);
554 1.1 rmind
555 1.22 rmind if (ifname) {
556 1.32 rmind prop_dictionary_set_cstring(rldict, "ifname", ifname);
557 1.1 rmind }
558 1.1 rmind rl->nrl_dict = rldict;
559 1.1 rmind return rl;
560 1.1 rmind }
561 1.1 rmind
562 1.1 rmind int
563 1.16 rmind npf_rule_setcode(nl_rule_t *rl, int type, const void *code, size_t len)
564 1.1 rmind {
565 1.1 rmind prop_dictionary_t rldict = rl->nrl_dict;
566 1.1 rmind prop_data_t cdata;
567 1.1 rmind
568 1.16 rmind switch (type) {
569 1.16 rmind case NPF_CODE_NC:
570 1.16 rmind case NPF_CODE_BPF:
571 1.16 rmind break;
572 1.16 rmind default:
573 1.1 rmind return ENOTSUP;
574 1.1 rmind }
575 1.36 christos prop_dictionary_set_uint32(rldict, "code-type", (uint32_t)type);
576 1.16 rmind if ((cdata = prop_data_create_data(code, len)) == NULL) {
577 1.1 rmind return ENOMEM;
578 1.1 rmind }
579 1.16 rmind prop_dictionary_set(rldict, "code", cdata);
580 1.1 rmind prop_object_release(cdata);
581 1.1 rmind return 0;
582 1.1 rmind }
583 1.1 rmind
584 1.1 rmind int
585 1.16 rmind npf_rule_setkey(nl_rule_t *rl, const void *key, size_t len)
586 1.1 rmind {
587 1.1 rmind prop_dictionary_t rldict = rl->nrl_dict;
588 1.16 rmind prop_data_t kdata;
589 1.1 rmind
590 1.16 rmind if ((kdata = prop_data_create_data(key, len)) == NULL) {
591 1.16 rmind return ENOMEM;
592 1.1 rmind }
593 1.16 rmind prop_dictionary_set(rldict, "key", kdata);
594 1.16 rmind prop_object_release(kdata);
595 1.16 rmind return 0;
596 1.16 rmind }
597 1.16 rmind
598 1.16 rmind int
599 1.20 rmind npf_rule_setinfo(nl_rule_t *rl, const void *info, size_t len)
600 1.20 rmind {
601 1.20 rmind prop_dictionary_t rldict = rl->nrl_dict;
602 1.20 rmind prop_data_t idata;
603 1.20 rmind
604 1.20 rmind if ((idata = prop_data_create_data(info, len)) == NULL) {
605 1.20 rmind return ENOMEM;
606 1.20 rmind }
607 1.20 rmind prop_dictionary_set(rldict, "info", idata);
608 1.20 rmind prop_object_release(idata);
609 1.20 rmind return 0;
610 1.20 rmind }
611 1.20 rmind
612 1.20 rmind int
613 1.16 rmind npf_rule_setprio(nl_rule_t *rl, pri_t pri)
614 1.16 rmind {
615 1.16 rmind prop_dictionary_t rldict = rl->nrl_dict;
616 1.16 rmind
617 1.32 rmind prop_dictionary_set_int32(rldict, "prio", pri);
618 1.16 rmind return 0;
619 1.16 rmind }
620 1.16 rmind
621 1.16 rmind int
622 1.16 rmind npf_rule_setproc(nl_rule_t *rl, const char *name)
623 1.16 rmind {
624 1.16 rmind prop_dictionary_t rldict = rl->nrl_dict;
625 1.16 rmind
626 1.1 rmind prop_dictionary_set_cstring(rldict, "rproc", name);
627 1.1 rmind return 0;
628 1.1 rmind }
629 1.1 rmind
630 1.16 rmind void *
631 1.16 rmind npf_rule_export(nl_rule_t *rl, size_t *length)
632 1.16 rmind {
633 1.16 rmind prop_dictionary_t rldict = rl->nrl_dict;
634 1.16 rmind void *xml;
635 1.16 rmind
636 1.16 rmind if ((xml = prop_dictionary_externalize(rldict)) == NULL) {
637 1.16 rmind return NULL;
638 1.16 rmind }
639 1.16 rmind *length = strlen(xml);
640 1.16 rmind return xml;
641 1.16 rmind }
642 1.16 rmind
643 1.1 rmind bool
644 1.1 rmind npf_rule_exists_p(nl_config_t *ncf, const char *name)
645 1.1 rmind {
646 1.1 rmind return _npf_prop_array_lookup(ncf->ncf_rules_list, "name", name);
647 1.1 rmind }
648 1.1 rmind
649 1.1 rmind int
650 1.16 rmind npf_rule_insert(nl_config_t *ncf, nl_rule_t *parent, nl_rule_t *rl)
651 1.1 rmind {
652 1.1 rmind prop_dictionary_t rldict = rl->nrl_dict;
653 1.1 rmind prop_array_t rlset;
654 1.1 rmind
655 1.1 rmind if (parent) {
656 1.1 rmind prop_dictionary_t pdict = parent->nrl_dict;
657 1.1 rmind rlset = prop_dictionary_get(pdict, "subrules");
658 1.1 rmind if (rlset == NULL) {
659 1.1 rmind rlset = prop_array_create();
660 1.1 rmind prop_dictionary_set(pdict, "subrules", rlset);
661 1.1 rmind prop_object_release(rlset);
662 1.1 rmind }
663 1.1 rmind } else {
664 1.1 rmind rlset = ncf->ncf_rules_list;
665 1.1 rmind }
666 1.1 rmind prop_array_add(rlset, rldict);
667 1.1 rmind return 0;
668 1.1 rmind }
669 1.1 rmind
670 1.20 rmind static nl_rule_t *
671 1.20 rmind _npf_rule_iterate1(nl_config_t *ncf, prop_array_t rlist, unsigned *level)
672 1.20 rmind {
673 1.20 rmind prop_dictionary_t rldict;
674 1.20 rmind uint32_t skipto = 0;
675 1.20 rmind
676 1.20 rmind if (!ncf->ncf_rule_iter) {
677 1.20 rmind /* Initialise the iterator. */
678 1.20 rmind ncf->ncf_rule_iter = prop_array_iterator(rlist);
679 1.20 rmind ncf->ncf_nlevel = 0;
680 1.20 rmind ncf->ncf_reduce[0] = 0;
681 1.20 rmind ncf->ncf_counter = 0;
682 1.20 rmind }
683 1.20 rmind
684 1.20 rmind rldict = prop_object_iterator_next(ncf->ncf_rule_iter);
685 1.20 rmind if ((ncf->ncf_cur_rule.nrl_dict = rldict) == NULL) {
686 1.20 rmind prop_object_iterator_release(ncf->ncf_rule_iter);
687 1.20 rmind ncf->ncf_rule_iter = NULL;
688 1.20 rmind return NULL;
689 1.20 rmind }
690 1.20 rmind *level = ncf->ncf_nlevel;
691 1.20 rmind
692 1.20 rmind prop_dictionary_get_uint32(rldict, "skip-to", &skipto);
693 1.20 rmind if (skipto) {
694 1.20 rmind ncf->ncf_nlevel++;
695 1.20 rmind ncf->ncf_reduce[ncf->ncf_nlevel] = skipto;
696 1.20 rmind }
697 1.20 rmind if (ncf->ncf_reduce[ncf->ncf_nlevel] == ++ncf->ncf_counter) {
698 1.20 rmind assert(ncf->ncf_nlevel > 0);
699 1.20 rmind ncf->ncf_nlevel--;
700 1.20 rmind }
701 1.20 rmind return &ncf->ncf_cur_rule;
702 1.20 rmind }
703 1.20 rmind
704 1.20 rmind nl_rule_t *
705 1.20 rmind npf_rule_iterate(nl_config_t *ncf, unsigned *level)
706 1.20 rmind {
707 1.20 rmind return _npf_rule_iterate1(ncf, ncf->ncf_rules_list, level);
708 1.20 rmind }
709 1.20 rmind
710 1.20 rmind const char *
711 1.20 rmind npf_rule_getname(nl_rule_t *rl)
712 1.20 rmind {
713 1.20 rmind prop_dictionary_t rldict = rl->nrl_dict;
714 1.20 rmind const char *rname = NULL;
715 1.20 rmind
716 1.20 rmind prop_dictionary_get_cstring_nocopy(rldict, "name", &rname);
717 1.20 rmind return rname;
718 1.20 rmind }
719 1.20 rmind
720 1.20 rmind uint32_t
721 1.20 rmind npf_rule_getattr(nl_rule_t *rl)
722 1.20 rmind {
723 1.20 rmind prop_dictionary_t rldict = rl->nrl_dict;
724 1.20 rmind uint32_t attr = 0;
725 1.20 rmind
726 1.32 rmind prop_dictionary_get_uint32(rldict, "attr", &attr);
727 1.20 rmind return attr;
728 1.20 rmind }
729 1.20 rmind
730 1.22 rmind const char *
731 1.20 rmind npf_rule_getinterface(nl_rule_t *rl)
732 1.20 rmind {
733 1.20 rmind prop_dictionary_t rldict = rl->nrl_dict;
734 1.22 rmind const char *ifname = NULL;
735 1.20 rmind
736 1.32 rmind prop_dictionary_get_cstring_nocopy(rldict, "ifname", &ifname);
737 1.22 rmind return ifname;
738 1.20 rmind }
739 1.20 rmind
740 1.20 rmind const void *
741 1.20 rmind npf_rule_getinfo(nl_rule_t *rl, size_t *len)
742 1.20 rmind {
743 1.20 rmind prop_dictionary_t rldict = rl->nrl_dict;
744 1.20 rmind prop_object_t obj = prop_dictionary_get(rldict, "info");
745 1.20 rmind
746 1.20 rmind *len = prop_data_size(obj);
747 1.20 rmind return prop_data_data_nocopy(obj);
748 1.20 rmind }
749 1.20 rmind
750 1.20 rmind const char *
751 1.20 rmind npf_rule_getproc(nl_rule_t *rl)
752 1.20 rmind {
753 1.20 rmind prop_dictionary_t rldict = rl->nrl_dict;
754 1.20 rmind const char *rpname = NULL;
755 1.20 rmind
756 1.20 rmind prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rpname);
757 1.20 rmind return rpname;
758 1.20 rmind }
759 1.20 rmind
760 1.35 rmind uint64_t
761 1.35 rmind npf_rule_getid(nl_rule_t *rl)
762 1.35 rmind {
763 1.35 rmind prop_dictionary_t rldict = rl->nrl_dict;
764 1.35 rmind uint64_t id = 0;
765 1.35 rmind
766 1.35 rmind (void)prop_dictionary_get_uint64(rldict, "id", &id);
767 1.35 rmind return id;
768 1.35 rmind }
769 1.35 rmind
770 1.35 rmind const void *
771 1.35 rmind npf_rule_getcode(nl_rule_t *rl, int *type, size_t *len)
772 1.35 rmind {
773 1.35 rmind prop_dictionary_t rldict = rl->nrl_dict;
774 1.35 rmind prop_object_t obj = prop_dictionary_get(rldict, "code");
775 1.35 rmind
776 1.35 rmind prop_dictionary_get_uint32(rldict, "code-type", (uint32_t *)type);
777 1.35 rmind *len = prop_data_size(obj);
778 1.35 rmind return prop_data_data_nocopy(obj);
779 1.35 rmind }
780 1.35 rmind
781 1.17 rmind int
782 1.17 rmind _npf_ruleset_list(int fd, const char *rname, nl_config_t *ncf)
783 1.17 rmind {
784 1.17 rmind prop_dictionary_t rldict, ret;
785 1.17 rmind int error;
786 1.17 rmind
787 1.17 rmind rldict = prop_dictionary_create();
788 1.17 rmind if (rldict == NULL) {
789 1.17 rmind return ENOMEM;
790 1.17 rmind }
791 1.17 rmind prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
792 1.17 rmind prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_LIST);
793 1.17 rmind error = prop_dictionary_sendrecv_ioctl(rldict, fd, IOC_NPF_RULE, &ret);
794 1.17 rmind if (!error) {
795 1.17 rmind prop_array_t rules;
796 1.17 rmind
797 1.17 rmind rules = prop_dictionary_get(ret, "rules");
798 1.17 rmind if (rules == NULL) {
799 1.17 rmind return EINVAL;
800 1.17 rmind }
801 1.17 rmind prop_object_release(ncf->ncf_rules_list);
802 1.17 rmind ncf->ncf_rules_list = rules;
803 1.17 rmind }
804 1.17 rmind return error;
805 1.17 rmind }
806 1.17 rmind
807 1.1 rmind void
808 1.1 rmind npf_rule_destroy(nl_rule_t *rl)
809 1.1 rmind {
810 1.1 rmind
811 1.1 rmind prop_object_release(rl->nrl_dict);
812 1.1 rmind free(rl);
813 1.1 rmind }
814 1.1 rmind
815 1.1 rmind /*
816 1.1 rmind * RULE PROCEDURE INTERFACE.
817 1.1 rmind */
818 1.1 rmind
819 1.1 rmind nl_rproc_t *
820 1.1 rmind npf_rproc_create(const char *name)
821 1.1 rmind {
822 1.1 rmind prop_dictionary_t rpdict;
823 1.13 rmind prop_array_t extcalls;
824 1.1 rmind nl_rproc_t *nrp;
825 1.1 rmind
826 1.1 rmind nrp = malloc(sizeof(nl_rproc_t));
827 1.1 rmind if (nrp == NULL) {
828 1.1 rmind return NULL;
829 1.1 rmind }
830 1.1 rmind rpdict = prop_dictionary_create();
831 1.1 rmind if (rpdict == NULL) {
832 1.1 rmind free(nrp);
833 1.1 rmind return NULL;
834 1.1 rmind }
835 1.1 rmind prop_dictionary_set_cstring(rpdict, "name", name);
836 1.13 rmind
837 1.13 rmind extcalls = prop_array_create();
838 1.13 rmind if (extcalls == NULL) {
839 1.13 rmind prop_object_release(rpdict);
840 1.13 rmind free(nrp);
841 1.13 rmind return NULL;
842 1.13 rmind }
843 1.13 rmind prop_dictionary_set(rpdict, "extcalls", extcalls);
844 1.13 rmind prop_object_release(extcalls);
845 1.13 rmind
846 1.1 rmind nrp->nrp_dict = rpdict;
847 1.1 rmind return nrp;
848 1.1 rmind }
849 1.1 rmind
850 1.1 rmind int
851 1.13 rmind npf_rproc_extcall(nl_rproc_t *rp, nl_ext_t *ext)
852 1.1 rmind {
853 1.1 rmind prop_dictionary_t rpdict = rp->nrp_dict;
854 1.13 rmind prop_dictionary_t extdict = ext->nxt_dict;
855 1.13 rmind prop_array_t extcalls;
856 1.1 rmind
857 1.13 rmind extcalls = prop_dictionary_get(rpdict, "extcalls");
858 1.13 rmind if (_npf_prop_array_lookup(extcalls, "name", ext->nxt_name)) {
859 1.13 rmind return EEXIST;
860 1.13 rmind }
861 1.13 rmind prop_dictionary_set_cstring(extdict, "name", ext->nxt_name);
862 1.13 rmind prop_array_add(extcalls, extdict);
863 1.1 rmind return 0;
864 1.1 rmind }
865 1.1 rmind
866 1.13 rmind bool
867 1.13 rmind npf_rproc_exists_p(nl_config_t *ncf, const char *name)
868 1.1 rmind {
869 1.13 rmind return _npf_prop_array_lookup(ncf->ncf_rproc_list, "name", name);
870 1.1 rmind }
871 1.1 rmind
872 1.1 rmind int
873 1.1 rmind npf_rproc_insert(nl_config_t *ncf, nl_rproc_t *rp)
874 1.1 rmind {
875 1.1 rmind prop_dictionary_t rpdict = rp->nrp_dict;
876 1.1 rmind const char *name;
877 1.1 rmind
878 1.1 rmind if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
879 1.1 rmind return EINVAL;
880 1.1 rmind }
881 1.1 rmind if (npf_rproc_exists_p(ncf, name)) {
882 1.1 rmind return EEXIST;
883 1.1 rmind }
884 1.1 rmind prop_array_add(ncf->ncf_rproc_list, rpdict);
885 1.1 rmind return 0;
886 1.1 rmind }
887 1.1 rmind
888 1.20 rmind nl_rproc_t *
889 1.20 rmind npf_rproc_iterate(nl_config_t *ncf)
890 1.20 rmind {
891 1.20 rmind prop_dictionary_t rpdict;
892 1.20 rmind
893 1.20 rmind if (!ncf->ncf_rproc_iter) {
894 1.20 rmind /* Initialise the iterator. */
895 1.20 rmind ncf->ncf_rproc_iter = prop_array_iterator(ncf->ncf_rproc_list);
896 1.20 rmind }
897 1.20 rmind rpdict = prop_object_iterator_next(ncf->ncf_rproc_iter);
898 1.20 rmind if ((ncf->ncf_cur_rproc.nrp_dict = rpdict) == NULL) {
899 1.20 rmind prop_object_iterator_release(ncf->ncf_rproc_iter);
900 1.20 rmind ncf->ncf_rproc_iter = NULL;
901 1.20 rmind return NULL;
902 1.20 rmind }
903 1.20 rmind return &ncf->ncf_cur_rproc;
904 1.20 rmind }
905 1.20 rmind
906 1.20 rmind const char *
907 1.20 rmind npf_rproc_getname(nl_rproc_t *rp)
908 1.20 rmind {
909 1.20 rmind prop_dictionary_t rpdict = rp->nrp_dict;
910 1.20 rmind const char *rpname = NULL;
911 1.20 rmind
912 1.20 rmind prop_dictionary_get_cstring_nocopy(rpdict, "name", &rpname);
913 1.20 rmind return rpname;
914 1.20 rmind }
915 1.20 rmind
916 1.1 rmind /*
917 1.32 rmind * NAT INTERFACE.
918 1.1 rmind */
919 1.1 rmind
920 1.1 rmind nl_nat_t *
921 1.22 rmind npf_nat_create(int type, u_int flags, const char *ifname,
922 1.28 rmind int af, npf_addr_t *addr, npf_netmask_t mask, in_port_t port)
923 1.1 rmind {
924 1.1 rmind nl_rule_t *rl;
925 1.1 rmind prop_dictionary_t rldict;
926 1.1 rmind uint32_t attr;
927 1.1 rmind
928 1.1 rmind attr = NPF_RULE_PASS | NPF_RULE_FINAL |
929 1.2 rmind (type == NPF_NATOUT ? NPF_RULE_OUT : NPF_RULE_IN);
930 1.1 rmind
931 1.32 rmind /* Create a rule for NAT policy. Next, will add NAT data. */
932 1.22 rmind rl = npf_rule_create(NULL, attr, ifname);
933 1.1 rmind if (rl == NULL) {
934 1.1 rmind return NULL;
935 1.1 rmind }
936 1.1 rmind rldict = rl->nrl_dict;
937 1.1 rmind
938 1.1 rmind /* Translation type and flags. */
939 1.1 rmind prop_dictionary_set_int32(rldict, "type", type);
940 1.1 rmind prop_dictionary_set_uint32(rldict, "flags", flags);
941 1.1 rmind
942 1.28 rmind /* Translation IP and mask. */
943 1.36 christos if (!_npf_add_addr(rldict, "nat-ip", af, addr)) {
944 1.1 rmind npf_rule_destroy(rl);
945 1.1 rmind return NULL;
946 1.1 rmind }
947 1.36 christos prop_dictionary_set_uint32(rldict, "nat-mask", (uint32_t)mask);
948 1.1 rmind
949 1.1 rmind /* Translation port (for redirect case). */
950 1.32 rmind prop_dictionary_set_uint16(rldict, "nat-port", port);
951 1.1 rmind
952 1.1 rmind return (nl_nat_t *)rl;
953 1.1 rmind }
954 1.1 rmind
955 1.1 rmind int
956 1.19 christos npf_nat_insert(nl_config_t *ncf, nl_nat_t *nt, pri_t pri __unused)
957 1.1 rmind {
958 1.1 rmind prop_dictionary_t rldict = nt->nrl_dict;
959 1.1 rmind
960 1.32 rmind prop_dictionary_set_int32(rldict, "prio", NPF_PRI_LAST);
961 1.1 rmind prop_array_add(ncf->ncf_nat_list, rldict);
962 1.1 rmind return 0;
963 1.1 rmind }
964 1.1 rmind
965 1.20 rmind nl_nat_t *
966 1.20 rmind npf_nat_iterate(nl_config_t *ncf)
967 1.20 rmind {
968 1.20 rmind u_int level;
969 1.20 rmind return _npf_rule_iterate1(ncf, ncf->ncf_nat_list, &level);
970 1.20 rmind }
971 1.20 rmind
972 1.20 rmind int
973 1.28 rmind npf_nat_setalgo(nl_nat_t *nt, u_int algo)
974 1.28 rmind {
975 1.28 rmind prop_dictionary_t rldict = nt->nrl_dict;
976 1.32 rmind prop_dictionary_set_uint32(rldict, "nat-algo", algo);
977 1.28 rmind return 0;
978 1.28 rmind }
979 1.28 rmind
980 1.28 rmind int
981 1.28 rmind npf_nat_setnpt66(nl_nat_t *nt, uint16_t adj)
982 1.28 rmind {
983 1.28 rmind prop_dictionary_t rldict = nt->nrl_dict;
984 1.28 rmind int error;
985 1.28 rmind
986 1.28 rmind if ((error = npf_nat_setalgo(nt, NPF_ALGO_NPT66)) != 0) {
987 1.28 rmind return error;
988 1.28 rmind }
989 1.32 rmind prop_dictionary_set_uint16(rldict, "npt66-adj", adj);
990 1.28 rmind return 0;
991 1.28 rmind }
992 1.28 rmind
993 1.28 rmind int
994 1.20 rmind npf_nat_gettype(nl_nat_t *nt)
995 1.20 rmind {
996 1.20 rmind prop_dictionary_t rldict = nt->nrl_dict;
997 1.20 rmind int type = 0;
998 1.20 rmind
999 1.20 rmind prop_dictionary_get_int32(rldict, "type", &type);
1000 1.20 rmind return type;
1001 1.20 rmind }
1002 1.20 rmind
1003 1.27 rmind u_int
1004 1.27 rmind npf_nat_getflags(nl_nat_t *nt)
1005 1.27 rmind {
1006 1.27 rmind prop_dictionary_t rldict = nt->nrl_dict;
1007 1.27 rmind unsigned flags = 0;
1008 1.27 rmind
1009 1.27 rmind prop_dictionary_get_uint32(rldict, "flags", &flags);
1010 1.27 rmind return flags;
1011 1.27 rmind }
1012 1.27 rmind
1013 1.20 rmind void
1014 1.20 rmind npf_nat_getmap(nl_nat_t *nt, npf_addr_t *addr, size_t *alen, in_port_t *port)
1015 1.20 rmind {
1016 1.20 rmind prop_dictionary_t rldict = nt->nrl_dict;
1017 1.32 rmind prop_object_t obj = prop_dictionary_get(rldict, "nat-ip");
1018 1.20 rmind
1019 1.20 rmind *alen = prop_data_size(obj);
1020 1.20 rmind memcpy(addr, prop_data_data_nocopy(obj), *alen);
1021 1.20 rmind
1022 1.20 rmind *port = 0;
1023 1.32 rmind prop_dictionary_get_uint16(rldict, "nat-port", port);
1024 1.20 rmind }
1025 1.20 rmind
1026 1.1 rmind /*
1027 1.1 rmind * TABLE INTERFACE.
1028 1.1 rmind */
1029 1.1 rmind
1030 1.1 rmind nl_table_t *
1031 1.23 rmind npf_table_create(const char *name, u_int id, int type)
1032 1.1 rmind {
1033 1.1 rmind prop_dictionary_t tldict;
1034 1.1 rmind prop_array_t tblents;
1035 1.1 rmind nl_table_t *tl;
1036 1.1 rmind
1037 1.5 christos tl = malloc(sizeof(*tl));
1038 1.1 rmind if (tl == NULL) {
1039 1.1 rmind return NULL;
1040 1.1 rmind }
1041 1.1 rmind tldict = prop_dictionary_create();
1042 1.1 rmind if (tldict == NULL) {
1043 1.1 rmind free(tl);
1044 1.1 rmind return NULL;
1045 1.1 rmind }
1046 1.23 rmind prop_dictionary_set_cstring(tldict, "name", name);
1047 1.1 rmind prop_dictionary_set_uint32(tldict, "id", id);
1048 1.1 rmind prop_dictionary_set_int32(tldict, "type", type);
1049 1.1 rmind
1050 1.1 rmind tblents = prop_array_create();
1051 1.1 rmind if (tblents == NULL) {
1052 1.1 rmind prop_object_release(tldict);
1053 1.1 rmind free(tl);
1054 1.1 rmind return NULL;
1055 1.1 rmind }
1056 1.1 rmind prop_dictionary_set(tldict, "entries", tblents);
1057 1.1 rmind prop_object_release(tblents);
1058 1.1 rmind
1059 1.1 rmind tl->ntl_dict = tldict;
1060 1.1 rmind return tl;
1061 1.1 rmind }
1062 1.1 rmind
1063 1.1 rmind int
1064 1.15 rmind npf_table_add_entry(nl_table_t *tl, int af, const npf_addr_t *addr,
1065 1.15 rmind const npf_netmask_t mask)
1066 1.1 rmind {
1067 1.1 rmind prop_dictionary_t tldict = tl->ntl_dict, entdict;
1068 1.1 rmind prop_array_t tblents;
1069 1.1 rmind
1070 1.1 rmind /* Create the table entry. */
1071 1.1 rmind entdict = prop_dictionary_create();
1072 1.10 rmind if (entdict == NULL) {
1073 1.1 rmind return ENOMEM;
1074 1.1 rmind }
1075 1.15 rmind
1076 1.36 christos if (!_npf_add_addr(entdict, "addr", af, addr)) {
1077 1.15 rmind return EINVAL;
1078 1.15 rmind }
1079 1.3 zoltan prop_dictionary_set_uint8(entdict, "mask", mask);
1080 1.1 rmind
1081 1.1 rmind tblents = prop_dictionary_get(tldict, "entries");
1082 1.1 rmind prop_array_add(tblents, entdict);
1083 1.1 rmind prop_object_release(entdict);
1084 1.1 rmind return 0;
1085 1.1 rmind }
1086 1.1 rmind
1087 1.26 rmind int
1088 1.26 rmind npf_table_setdata(nl_table_t *tl, const void *blob, size_t len)
1089 1.26 rmind {
1090 1.26 rmind prop_dictionary_t tldict = tl->ntl_dict;
1091 1.26 rmind prop_data_t bobj;
1092 1.26 rmind
1093 1.26 rmind if ((bobj = prop_data_create_data(blob, len)) == NULL) {
1094 1.26 rmind return ENOMEM;
1095 1.26 rmind }
1096 1.26 rmind prop_dictionary_set(tldict, "data", bobj);
1097 1.26 rmind prop_object_release(bobj);
1098 1.26 rmind return 0;
1099 1.26 rmind }
1100 1.26 rmind
1101 1.25 rmind static bool
1102 1.25 rmind _npf_table_exists_p(nl_config_t *ncf, const char *name)
1103 1.1 rmind {
1104 1.1 rmind prop_dictionary_t tldict;
1105 1.1 rmind prop_object_iterator_t it;
1106 1.1 rmind
1107 1.1 rmind it = prop_array_iterator(ncf->ncf_table_list);
1108 1.1 rmind while ((tldict = prop_object_iterator_next(it)) != NULL) {
1109 1.24 rmind const char *tname = NULL;
1110 1.24 rmind
1111 1.24 rmind if (prop_dictionary_get_cstring_nocopy(tldict, "name", &tname)
1112 1.24 rmind && strcmp(tname, name) == 0)
1113 1.1 rmind break;
1114 1.1 rmind }
1115 1.1 rmind prop_object_iterator_release(it);
1116 1.1 rmind return tldict ? true : false;
1117 1.1 rmind }
1118 1.1 rmind
1119 1.1 rmind int
1120 1.1 rmind npf_table_insert(nl_config_t *ncf, nl_table_t *tl)
1121 1.1 rmind {
1122 1.1 rmind prop_dictionary_t tldict = tl->ntl_dict;
1123 1.24 rmind const char *name = NULL;
1124 1.1 rmind
1125 1.24 rmind if (!prop_dictionary_get_cstring_nocopy(tldict, "name", &name)) {
1126 1.1 rmind return EINVAL;
1127 1.1 rmind }
1128 1.25 rmind if (_npf_table_exists_p(ncf, name)) {
1129 1.1 rmind return EEXIST;
1130 1.1 rmind }
1131 1.1 rmind prop_array_add(ncf->ncf_table_list, tldict);
1132 1.1 rmind return 0;
1133 1.1 rmind }
1134 1.1 rmind
1135 1.20 rmind nl_table_t *
1136 1.20 rmind npf_table_iterate(nl_config_t *ncf)
1137 1.20 rmind {
1138 1.20 rmind prop_dictionary_t tldict;
1139 1.20 rmind
1140 1.20 rmind if (!ncf->ncf_table_iter) {
1141 1.20 rmind /* Initialise the iterator. */
1142 1.20 rmind ncf->ncf_table_iter = prop_array_iterator(ncf->ncf_table_list);
1143 1.20 rmind }
1144 1.20 rmind tldict = prop_object_iterator_next(ncf->ncf_table_iter);
1145 1.20 rmind if ((ncf->ncf_cur_table.ntl_dict = tldict) == NULL) {
1146 1.20 rmind prop_object_iterator_release(ncf->ncf_table_iter);
1147 1.20 rmind ncf->ncf_table_iter = NULL;
1148 1.20 rmind return NULL;
1149 1.20 rmind }
1150 1.20 rmind return &ncf->ncf_cur_table;
1151 1.20 rmind }
1152 1.20 rmind
1153 1.20 rmind unsigned
1154 1.20 rmind npf_table_getid(nl_table_t *tl)
1155 1.20 rmind {
1156 1.20 rmind prop_dictionary_t tldict = tl->ntl_dict;
1157 1.23 rmind unsigned id = (unsigned)-1;
1158 1.20 rmind
1159 1.20 rmind prop_dictionary_get_uint32(tldict, "id", &id);
1160 1.20 rmind return id;
1161 1.20 rmind }
1162 1.20 rmind
1163 1.23 rmind const char *
1164 1.23 rmind npf_table_getname(nl_table_t *tl)
1165 1.23 rmind {
1166 1.23 rmind prop_dictionary_t tldict = tl->ntl_dict;
1167 1.23 rmind const char *tname = NULL;
1168 1.23 rmind
1169 1.23 rmind prop_dictionary_get_cstring_nocopy(tldict, "name", &tname);
1170 1.23 rmind return tname;
1171 1.23 rmind }
1172 1.23 rmind
1173 1.20 rmind int
1174 1.20 rmind npf_table_gettype(nl_table_t *tl)
1175 1.20 rmind {
1176 1.20 rmind prop_dictionary_t tldict = tl->ntl_dict;
1177 1.20 rmind int type = 0;
1178 1.20 rmind
1179 1.20 rmind prop_dictionary_get_int32(tldict, "type", &type);
1180 1.20 rmind return type;
1181 1.20 rmind }
1182 1.20 rmind
1183 1.1 rmind void
1184 1.1 rmind npf_table_destroy(nl_table_t *tl)
1185 1.1 rmind {
1186 1.1 rmind prop_object_release(tl->ntl_dict);
1187 1.1 rmind free(tl);
1188 1.1 rmind }
1189 1.1 rmind
1190 1.1 rmind /*
1191 1.19 christos * ALG INTERFACE.
1192 1.19 christos */
1193 1.19 christos
1194 1.19 christos int
1195 1.19 christos _npf_alg_load(nl_config_t *ncf, const char *name)
1196 1.19 christos {
1197 1.19 christos prop_dictionary_t al_dict;
1198 1.19 christos
1199 1.19 christos if (_npf_prop_array_lookup(ncf->ncf_alg_list, "name", name))
1200 1.19 christos return EEXIST;
1201 1.19 christos
1202 1.19 christos al_dict = prop_dictionary_create();
1203 1.19 christos prop_dictionary_set_cstring(al_dict, "name", name);
1204 1.19 christos prop_array_add(ncf->ncf_alg_list, al_dict);
1205 1.19 christos prop_object_release(al_dict);
1206 1.19 christos return 0;
1207 1.19 christos }
1208 1.19 christos
1209 1.19 christos int
1210 1.19 christos _npf_alg_unload(nl_config_t *ncf, const char *name)
1211 1.19 christos {
1212 1.19 christos if (!_npf_prop_array_lookup(ncf->ncf_alg_list, "name", name))
1213 1.19 christos return ENOENT;
1214 1.19 christos
1215 1.19 christos // Not yet: prop_array_add(ncf->ncf_alg_list, al_dict);
1216 1.19 christos return ENOTSUP;
1217 1.19 christos }
1218 1.19 christos
1219 1.19 christos /*
1220 1.1 rmind * MISC.
1221 1.1 rmind */
1222 1.1 rmind
1223 1.11 rmind static prop_dictionary_t
1224 1.11 rmind _npf_debug_initonce(nl_config_t *ncf)
1225 1.11 rmind {
1226 1.11 rmind if (!ncf->ncf_debug) {
1227 1.11 rmind prop_array_t iflist = prop_array_create();
1228 1.11 rmind ncf->ncf_debug = prop_dictionary_create();
1229 1.11 rmind prop_dictionary_set(ncf->ncf_debug, "interfaces", iflist);
1230 1.11 rmind prop_object_release(iflist);
1231 1.11 rmind }
1232 1.11 rmind return ncf->ncf_debug;
1233 1.11 rmind }
1234 1.11 rmind
1235 1.11 rmind void
1236 1.22 rmind _npf_debug_addif(nl_config_t *ncf, const char *ifname)
1237 1.11 rmind {
1238 1.11 rmind prop_dictionary_t ifdict, dbg = _npf_debug_initonce(ncf);
1239 1.11 rmind prop_array_t iflist = prop_dictionary_get(dbg, "interfaces");
1240 1.22 rmind u_int if_idx = if_nametoindex(ifname);
1241 1.11 rmind
1242 1.22 rmind if (_npf_prop_array_lookup(iflist, "name", ifname)) {
1243 1.11 rmind return;
1244 1.11 rmind }
1245 1.11 rmind ifdict = prop_dictionary_create();
1246 1.22 rmind prop_dictionary_set_cstring(ifdict, "name", ifname);
1247 1.22 rmind prop_dictionary_set_uint32(ifdict, "index", if_idx);
1248 1.11 rmind prop_array_add(iflist, ifdict);
1249 1.11 rmind prop_object_release(ifdict);
1250 1.11 rmind }
1251 1.36 christos
1252 1.36 christos
1253 1.36 christos int
1254 1.36 christos npf_nat_lookup(int fd, int af, npf_addr_t **addr, in_port_t *port,
1255 1.36 christos int proto, int dir)
1256 1.36 christos {
1257 1.36 christos prop_dictionary_t conn_dict, conn_res;
1258 1.36 christos int error = EINVAL;
1259 1.36 christos
1260 1.36 christos conn_dict = prop_dictionary_create();
1261 1.36 christos if (conn_dict == NULL) {
1262 1.36 christos return ENOMEM;
1263 1.36 christos }
1264 1.36 christos if (!_npf_add_addr(conn_dict, "saddr", af, addr[0]))
1265 1.36 christos goto out;
1266 1.36 christos
1267 1.36 christos if (!_npf_add_addr(conn_dict, "daddr", af, addr[1]))
1268 1.36 christos goto out;
1269 1.36 christos
1270 1.36 christos prop_dictionary_set_uint16(conn_dict, "sport", port[0]);
1271 1.36 christos prop_dictionary_set_uint16(conn_dict, "dport", port[1]);
1272 1.36 christos prop_dictionary_set_uint16(conn_dict, "proto", proto);
1273 1.36 christos prop_dictionary_set_uint16(conn_dict, "direction", dir);
1274 1.36 christos
1275 1.36 christos prop_dictionary_externalize_to_file(conn_dict, "/tmp/in");
1276 1.36 christos error = prop_dictionary_sendrecv_ioctl(conn_dict, fd,
1277 1.36 christos IOC_NPF_CONN_LOOKUP, &conn_res);
1278 1.36 christos if (error != 0)
1279 1.36 christos goto out;
1280 1.36 christos
1281 1.36 christos prop_dictionary_externalize_to_file(conn_res, "/tmp/out");
1282 1.36 christos prop_dictionary_t nat = prop_dictionary_get(conn_res, "nat");
1283 1.36 christos if (nat == NULL) {
1284 1.36 christos errno = ENOENT;
1285 1.36 christos goto out;
1286 1.36 christos }
1287 1.36 christos if (!_npf_get_addr(nat, "oaddr", addr[0])) {
1288 1.36 christos prop_object_release(nat);
1289 1.36 christos error = EINVAL;
1290 1.36 christos goto out;
1291 1.36 christos }
1292 1.36 christos prop_dictionary_get_uint16(nat, "oport", &port[0]);
1293 1.36 christos prop_dictionary_get_uint16(nat, "tport", &port[1]);
1294 1.36 christos ntohs(port[1]));
1295 1.36 christos prop_object_release(conn_res);
1296 1.36 christos out:
1297 1.36 christos prop_object_release(conn_dict);
1298 1.36 christos return error;
1299 1.36 christos }
1300