npf.c revision 1.7.2.5.4.1 1 1.7.2.5.4.1 riz /* $NetBSD: npf.c,v 1.7.2.5.4.1 2012/11/19 18:15:24 riz Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.6 rmind * Copyright (c) 2010-2012 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.7.2.5.4.1 riz __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.7.2.5.4.1 2012/11/19 18:15:24 riz 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.7.2.4 riz #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_config {
51 1.1 rmind /* Rules, translations, tables, procedures. */
52 1.7.2.1 riz prop_dictionary_t ncf_dict;
53 1.1 rmind prop_array_t ncf_rules_list;
54 1.1 rmind prop_array_t ncf_rproc_list;
55 1.1 rmind prop_array_t ncf_table_list;
56 1.1 rmind prop_array_t ncf_nat_list;
57 1.1 rmind /* Priority counters. */
58 1.1 rmind pri_t ncf_rule_pri;
59 1.1 rmind pri_t ncf_nat_pri;
60 1.7.2.4 riz /* Debug information. */
61 1.7.2.4 riz prop_dictionary_t ncf_debug;
62 1.7 rmind /* Error report. */
63 1.7 rmind prop_dictionary_t ncf_err;
64 1.4 rmind /* Custom file to externalise property-list. */
65 1.4 rmind const char * ncf_plist;
66 1.6 rmind bool ncf_flush;
67 1.1 rmind };
68 1.1 rmind
69 1.1 rmind struct nl_rule {
70 1.1 rmind prop_dictionary_t nrl_dict;
71 1.1 rmind };
72 1.1 rmind
73 1.1 rmind struct nl_rproc {
74 1.1 rmind prop_dictionary_t nrp_dict;
75 1.1 rmind };
76 1.1 rmind
77 1.1 rmind struct nl_table {
78 1.1 rmind prop_dictionary_t ntl_dict;
79 1.1 rmind };
80 1.1 rmind
81 1.1 rmind /*
82 1.1 rmind * CONFIGURATION INTERFACE.
83 1.1 rmind */
84 1.1 rmind
85 1.1 rmind nl_config_t *
86 1.1 rmind npf_config_create(void)
87 1.1 rmind {
88 1.1 rmind nl_config_t *ncf;
89 1.1 rmind
90 1.7 rmind ncf = calloc(1, sizeof(*ncf));
91 1.1 rmind if (ncf == NULL) {
92 1.1 rmind return NULL;
93 1.1 rmind }
94 1.1 rmind ncf->ncf_rules_list = prop_array_create();
95 1.1 rmind ncf->ncf_rproc_list = prop_array_create();
96 1.1 rmind ncf->ncf_table_list = prop_array_create();
97 1.1 rmind ncf->ncf_nat_list = prop_array_create();
98 1.1 rmind
99 1.1 rmind ncf->ncf_rule_pri = 1;
100 1.1 rmind ncf->ncf_nat_pri = 1;
101 1.1 rmind
102 1.4 rmind ncf->ncf_plist = NULL;
103 1.6 rmind ncf->ncf_flush = false;
104 1.4 rmind
105 1.1 rmind return ncf;
106 1.1 rmind }
107 1.1 rmind
108 1.1 rmind int
109 1.1 rmind npf_config_submit(nl_config_t *ncf, int fd)
110 1.1 rmind {
111 1.7 rmind const char *plist = ncf->ncf_plist;
112 1.1 rmind prop_dictionary_t npf_dict;
113 1.1 rmind int error = 0;
114 1.1 rmind
115 1.1 rmind npf_dict = prop_dictionary_create();
116 1.1 rmind if (npf_dict == NULL) {
117 1.1 rmind return ENOMEM;
118 1.1 rmind }
119 1.7.2.4 riz if (ncf->ncf_debug) {
120 1.7.2.4 riz prop_dictionary_set(npf_dict, "debug", ncf->ncf_debug);
121 1.7.2.4 riz }
122 1.1 rmind prop_dictionary_set(npf_dict, "rules", ncf->ncf_rules_list);
123 1.1 rmind prop_dictionary_set(npf_dict, "rprocs", ncf->ncf_rproc_list);
124 1.1 rmind prop_dictionary_set(npf_dict, "tables", ncf->ncf_table_list);
125 1.1 rmind prop_dictionary_set(npf_dict, "translation", ncf->ncf_nat_list);
126 1.6 rmind prop_dictionary_set_bool(npf_dict, "flush", ncf->ncf_flush);
127 1.1 rmind
128 1.4 rmind if (plist) {
129 1.4 rmind if (!prop_dictionary_externalize_to_file(npf_dict, plist)) {
130 1.4 rmind error = errno;
131 1.4 rmind }
132 1.7 rmind prop_object_release(npf_dict);
133 1.7 rmind return error;
134 1.7 rmind }
135 1.7 rmind
136 1.7 rmind error = prop_dictionary_sendrecv_ioctl(npf_dict, fd,
137 1.7 rmind IOC_NPF_RELOAD, &ncf->ncf_err);
138 1.7 rmind if (error) {
139 1.7 rmind prop_object_release(npf_dict);
140 1.7 rmind assert(ncf->ncf_err == NULL);
141 1.7 rmind return error;
142 1.1 rmind }
143 1.7 rmind
144 1.7 rmind prop_dictionary_get_int32(ncf->ncf_err, "errno", &error);
145 1.1 rmind prop_object_release(npf_dict);
146 1.1 rmind return error;
147 1.1 rmind }
148 1.1 rmind
149 1.7.2.1 riz nl_config_t *
150 1.7.2.1 riz npf_config_retrieve(int fd, bool *active, bool *loaded)
151 1.7.2.1 riz {
152 1.7.2.1 riz prop_dictionary_t npf_dict;
153 1.7.2.1 riz nl_config_t *ncf;
154 1.7.2.1 riz int error;
155 1.7.2.1 riz
156 1.7.2.1 riz error = prop_dictionary_recv_ioctl(fd, IOC_NPF_GETCONF, &npf_dict);
157 1.7.2.1 riz if (error) {
158 1.7.2.1 riz return NULL;
159 1.7.2.1 riz }
160 1.7.2.1 riz ncf = calloc(1, sizeof(*ncf));
161 1.7.2.1 riz if (ncf == NULL) {
162 1.7.2.1 riz prop_object_release(npf_dict);
163 1.7.2.1 riz return NULL;
164 1.7.2.1 riz }
165 1.7.2.1 riz ncf->ncf_dict = npf_dict;
166 1.7.2.1 riz ncf->ncf_rules_list = prop_dictionary_get(npf_dict, "rules");
167 1.7.2.1 riz ncf->ncf_rproc_list = prop_dictionary_get(npf_dict, "rprocs");
168 1.7.2.1 riz ncf->ncf_table_list = prop_dictionary_get(npf_dict, "tables");
169 1.7.2.1 riz ncf->ncf_nat_list = prop_dictionary_get(npf_dict, "translation");
170 1.7.2.1 riz
171 1.7.2.1 riz prop_dictionary_get_bool(npf_dict, "active", active);
172 1.7.2.1 riz *loaded = (ncf->ncf_rules_list != NULL);
173 1.7.2.1 riz return ncf;
174 1.7.2.1 riz }
175 1.7.2.1 riz
176 1.6 rmind int
177 1.6 rmind npf_config_flush(int fd)
178 1.6 rmind {
179 1.6 rmind nl_config_t *ncf;
180 1.6 rmind int error;
181 1.6 rmind
182 1.6 rmind ncf = npf_config_create();
183 1.6 rmind if (ncf == NULL) {
184 1.6 rmind return ENOMEM;
185 1.6 rmind }
186 1.6 rmind ncf->ncf_flush = true;
187 1.6 rmind error = npf_config_submit(ncf, fd);
188 1.6 rmind npf_config_destroy(ncf);
189 1.6 rmind return error;
190 1.6 rmind }
191 1.6 rmind
192 1.1 rmind void
193 1.7 rmind _npf_config_error(nl_config_t *ncf, nl_error_t *ne)
194 1.7 rmind {
195 1.7 rmind memset(ne, 0, sizeof(*ne));
196 1.7 rmind prop_dictionary_get_int32(ncf->ncf_err, "id", &ne->ne_id);
197 1.7 rmind prop_dictionary_get_cstring(ncf->ncf_err,
198 1.7 rmind "source-file", &ne->ne_source_file);
199 1.7 rmind prop_dictionary_get_uint32(ncf->ncf_err,
200 1.7 rmind "source-line", &ne->ne_source_line);
201 1.7 rmind prop_dictionary_get_int32(ncf->ncf_err,
202 1.7 rmind "ncode-error", &ne->ne_ncode_error);
203 1.7 rmind prop_dictionary_get_int32(ncf->ncf_err,
204 1.7 rmind "ncode-errat", &ne->ne_ncode_errat);
205 1.7 rmind }
206 1.7 rmind
207 1.7 rmind void
208 1.1 rmind npf_config_destroy(nl_config_t *ncf)
209 1.1 rmind {
210 1.1 rmind
211 1.7.2.5.4.1 riz if (!ncf->ncf_dict) {
212 1.7.2.1 riz prop_object_release(ncf->ncf_rules_list);
213 1.7.2.1 riz prop_object_release(ncf->ncf_rproc_list);
214 1.7.2.1 riz prop_object_release(ncf->ncf_table_list);
215 1.7.2.1 riz prop_object_release(ncf->ncf_nat_list);
216 1.7.2.1 riz }
217 1.7 rmind if (ncf->ncf_err) {
218 1.7 rmind prop_object_release(ncf->ncf_err);
219 1.7 rmind }
220 1.7.2.4 riz if (ncf->ncf_debug) {
221 1.7.2.4 riz prop_object_release(ncf->ncf_debug);
222 1.7.2.4 riz }
223 1.1 rmind free(ncf);
224 1.1 rmind }
225 1.1 rmind
226 1.4 rmind void
227 1.4 rmind _npf_config_setsubmit(nl_config_t *ncf, const char *plist_file)
228 1.4 rmind {
229 1.4 rmind
230 1.4 rmind ncf->ncf_plist = plist_file;
231 1.4 rmind }
232 1.4 rmind
233 1.1 rmind static bool
234 1.1 rmind _npf_prop_array_lookup(prop_array_t array, const char *key, const char *name)
235 1.1 rmind {
236 1.1 rmind prop_dictionary_t dict;
237 1.1 rmind prop_object_iterator_t it;
238 1.1 rmind
239 1.1 rmind it = prop_array_iterator(array);
240 1.1 rmind while ((dict = prop_object_iterator_next(it)) != NULL) {
241 1.1 rmind const char *lname;
242 1.1 rmind prop_dictionary_get_cstring_nocopy(dict, key, &lname);
243 1.1 rmind if (strcmp(name, lname) == 0)
244 1.1 rmind break;
245 1.1 rmind }
246 1.1 rmind prop_object_iterator_release(it);
247 1.1 rmind return dict ? true : false;
248 1.1 rmind }
249 1.1 rmind
250 1.1 rmind /*
251 1.1 rmind * RULE INTERFACE.
252 1.1 rmind */
253 1.1 rmind
254 1.1 rmind nl_rule_t *
255 1.1 rmind npf_rule_create(const char *name, uint32_t attr, u_int if_idx)
256 1.1 rmind {
257 1.1 rmind prop_dictionary_t rldict;
258 1.1 rmind nl_rule_t *rl;
259 1.1 rmind
260 1.5 christos rl = malloc(sizeof(*rl));
261 1.1 rmind if (rl == NULL) {
262 1.1 rmind return NULL;
263 1.1 rmind }
264 1.1 rmind rldict = prop_dictionary_create();
265 1.1 rmind if (rldict == NULL) {
266 1.1 rmind free(rl);
267 1.1 rmind return NULL;
268 1.1 rmind }
269 1.1 rmind if (name) {
270 1.1 rmind prop_dictionary_set_cstring(rldict, "name", name);
271 1.1 rmind }
272 1.1 rmind prop_dictionary_set_uint32(rldict, "attributes", attr);
273 1.1 rmind
274 1.1 rmind if (if_idx) {
275 1.1 rmind prop_dictionary_set_uint32(rldict, "interface", if_idx);
276 1.1 rmind }
277 1.1 rmind rl->nrl_dict = rldict;
278 1.1 rmind return rl;
279 1.1 rmind }
280 1.1 rmind
281 1.1 rmind int
282 1.1 rmind npf_rule_setcode(nl_rule_t *rl, int type, const void *code, size_t sz)
283 1.1 rmind {
284 1.1 rmind prop_dictionary_t rldict = rl->nrl_dict;
285 1.1 rmind prop_data_t cdata;
286 1.1 rmind
287 1.1 rmind if (type != NPF_CODE_NCODE) {
288 1.1 rmind return ENOTSUP;
289 1.1 rmind }
290 1.1 rmind cdata = prop_data_create_data(code, sz);
291 1.1 rmind if (cdata == NULL) {
292 1.1 rmind return ENOMEM;
293 1.1 rmind }
294 1.1 rmind prop_dictionary_set(rldict, "ncode", cdata);
295 1.1 rmind prop_object_release(cdata);
296 1.1 rmind return 0;
297 1.1 rmind }
298 1.1 rmind
299 1.1 rmind int
300 1.1 rmind npf_rule_setproc(nl_config_t *ncf, nl_rule_t *rl, const char *name)
301 1.1 rmind {
302 1.1 rmind prop_dictionary_t rldict = rl->nrl_dict;
303 1.1 rmind
304 1.1 rmind if (!npf_rproc_exists_p(ncf, name)) {
305 1.1 rmind return ENOENT;
306 1.1 rmind }
307 1.1 rmind prop_dictionary_set_cstring(rldict, "rproc", name);
308 1.1 rmind return 0;
309 1.1 rmind }
310 1.1 rmind
311 1.1 rmind bool
312 1.1 rmind npf_rule_exists_p(nl_config_t *ncf, const char *name)
313 1.1 rmind {
314 1.1 rmind
315 1.1 rmind return _npf_prop_array_lookup(ncf->ncf_rules_list, "name", name);
316 1.1 rmind }
317 1.1 rmind
318 1.1 rmind int
319 1.1 rmind npf_rule_insert(nl_config_t *ncf, nl_rule_t *parent, nl_rule_t *rl, pri_t pri)
320 1.1 rmind {
321 1.1 rmind prop_dictionary_t rldict = rl->nrl_dict;
322 1.1 rmind prop_array_t rlset;
323 1.1 rmind
324 1.1 rmind if (pri == NPF_PRI_NEXT) {
325 1.1 rmind pri = ncf->ncf_rule_pri++;
326 1.1 rmind } else if (ncf) {
327 1.1 rmind ncf->ncf_rule_pri = pri + 1;
328 1.1 rmind }
329 1.1 rmind prop_dictionary_set_int32(rldict, "priority", pri);
330 1.1 rmind
331 1.1 rmind if (parent) {
332 1.1 rmind prop_dictionary_t pdict = parent->nrl_dict;
333 1.1 rmind rlset = prop_dictionary_get(pdict, "subrules");
334 1.1 rmind if (rlset == NULL) {
335 1.1 rmind rlset = prop_array_create();
336 1.1 rmind prop_dictionary_set(pdict, "subrules", rlset);
337 1.1 rmind prop_object_release(rlset);
338 1.1 rmind }
339 1.1 rmind } else {
340 1.1 rmind rlset = ncf->ncf_rules_list;
341 1.1 rmind }
342 1.1 rmind prop_array_add(rlset, rldict);
343 1.1 rmind return 0;
344 1.1 rmind }
345 1.1 rmind
346 1.7.2.1 riz static int
347 1.7.2.1 riz _npf_rule_foreach1(prop_array_t rules, unsigned nlevel, nl_rule_callback_t func)
348 1.7.2.1 riz {
349 1.7.2.1 riz prop_dictionary_t rldict;
350 1.7.2.1 riz prop_object_iterator_t it;
351 1.7.2.1 riz
352 1.7.2.1 riz if (!rules || prop_object_type(rules) != PROP_TYPE_ARRAY) {
353 1.7.2.1 riz return ENOENT;
354 1.7.2.1 riz }
355 1.7.2.1 riz it = prop_array_iterator(rules);
356 1.7.2.1 riz if (it == NULL) {
357 1.7.2.1 riz return ENOMEM;
358 1.7.2.1 riz }
359 1.7.2.1 riz while ((rldict = prop_object_iterator_next(it)) != NULL) {
360 1.7.2.1 riz prop_array_t subrules;
361 1.7.2.1 riz nl_rule_t nrl;
362 1.7.2.1 riz
363 1.7.2.1 riz nrl.nrl_dict = rldict;
364 1.7.2.1 riz (*func)(&nrl, nlevel);
365 1.7.2.1 riz
366 1.7.2.1 riz subrules = prop_dictionary_get(rldict, "subrules");
367 1.7.2.5.4.1 riz if (!subrules) {
368 1.7.2.5.4.1 riz continue;
369 1.7.2.5.4.1 riz }
370 1.7.2.1 riz (void)_npf_rule_foreach1(subrules, nlevel + 1, func);
371 1.7.2.1 riz }
372 1.7.2.1 riz prop_object_iterator_release(it);
373 1.7.2.1 riz return 0;
374 1.7.2.1 riz }
375 1.7.2.1 riz
376 1.7.2.1 riz int
377 1.7.2.1 riz _npf_rule_foreach(nl_config_t *ncf, nl_rule_callback_t func)
378 1.7.2.1 riz {
379 1.7.2.1 riz
380 1.7.2.1 riz return _npf_rule_foreach1(ncf->ncf_rules_list, 0, func);
381 1.7.2.1 riz }
382 1.7.2.1 riz
383 1.7.2.1 riz pri_t
384 1.7.2.1 riz _npf_rule_getinfo(nl_rule_t *nrl, const char **rname, uint32_t *attr,
385 1.7.2.1 riz u_int *if_idx)
386 1.7.2.1 riz {
387 1.7.2.1 riz prop_dictionary_t rldict = nrl->nrl_dict;
388 1.7.2.1 riz pri_t prio;
389 1.7.2.1 riz
390 1.7.2.1 riz prop_dictionary_get_cstring_nocopy(rldict, "name", rname);
391 1.7.2.1 riz prop_dictionary_get_uint32(rldict, "attributes", attr);
392 1.7.2.1 riz prop_dictionary_get_int32(rldict, "priority", &prio);
393 1.7.2.1 riz prop_dictionary_get_uint32(rldict, "interface", if_idx);
394 1.7.2.1 riz return prio;
395 1.7.2.1 riz }
396 1.7.2.1 riz
397 1.7.2.1 riz const void *
398 1.7.2.1 riz _npf_rule_ncode(nl_rule_t *nrl, size_t *size)
399 1.7.2.1 riz {
400 1.7.2.1 riz prop_dictionary_t rldict = nrl->nrl_dict;
401 1.7.2.1 riz prop_object_t obj = prop_dictionary_get(rldict, "ncode");
402 1.7.2.1 riz *size = prop_data_size(obj);
403 1.7.2.1 riz return prop_data_data_nocopy(obj);
404 1.7.2.1 riz }
405 1.7.2.1 riz
406 1.7.2.1 riz const char *
407 1.7.2.1 riz _npf_rule_rproc(nl_rule_t *nrl)
408 1.7.2.1 riz {
409 1.7.2.1 riz prop_dictionary_t rldict = nrl->nrl_dict;
410 1.7.2.1 riz const char *rpname = NULL;
411 1.7.2.1 riz
412 1.7.2.1 riz prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rpname);
413 1.7.2.1 riz return rpname;
414 1.7.2.1 riz }
415 1.7.2.1 riz
416 1.1 rmind void
417 1.1 rmind npf_rule_destroy(nl_rule_t *rl)
418 1.1 rmind {
419 1.1 rmind
420 1.1 rmind prop_object_release(rl->nrl_dict);
421 1.1 rmind free(rl);
422 1.1 rmind }
423 1.1 rmind
424 1.1 rmind /*
425 1.1 rmind * RULE PROCEDURE INTERFACE.
426 1.1 rmind */
427 1.1 rmind
428 1.1 rmind nl_rproc_t *
429 1.1 rmind npf_rproc_create(const char *name)
430 1.1 rmind {
431 1.1 rmind prop_dictionary_t rpdict;
432 1.1 rmind nl_rproc_t *nrp;
433 1.1 rmind
434 1.1 rmind nrp = malloc(sizeof(nl_rproc_t));
435 1.1 rmind if (nrp == NULL) {
436 1.1 rmind return NULL;
437 1.1 rmind }
438 1.1 rmind rpdict = prop_dictionary_create();
439 1.1 rmind if (rpdict == NULL) {
440 1.1 rmind free(nrp);
441 1.1 rmind return NULL;
442 1.1 rmind }
443 1.1 rmind prop_dictionary_set_cstring(rpdict, "name", name);
444 1.1 rmind nrp->nrp_dict = rpdict;
445 1.1 rmind return nrp;
446 1.1 rmind }
447 1.1 rmind
448 1.1 rmind bool
449 1.1 rmind npf_rproc_exists_p(nl_config_t *ncf, const char *name)
450 1.1 rmind {
451 1.1 rmind
452 1.1 rmind return _npf_prop_array_lookup(ncf->ncf_rproc_list, "name", name);
453 1.1 rmind }
454 1.1 rmind
455 1.1 rmind int
456 1.5 christos _npf_rproc_setnorm(nl_rproc_t *rp, bool rnd, bool no_df, u_int minttl,
457 1.5 christos u_int maxmss)
458 1.1 rmind {
459 1.1 rmind prop_dictionary_t rpdict = rp->nrp_dict;
460 1.7.2.5 riz uint32_t fl = 0;
461 1.1 rmind
462 1.1 rmind prop_dictionary_set_bool(rpdict, "randomize-id", rnd);
463 1.1 rmind prop_dictionary_set_bool(rpdict, "no-df", no_df);
464 1.1 rmind prop_dictionary_set_uint32(rpdict, "min-ttl", minttl);
465 1.1 rmind prop_dictionary_set_uint32(rpdict, "max-mss", maxmss);
466 1.1 rmind
467 1.2 rmind prop_dictionary_get_uint32(rpdict, "flags", &fl);
468 1.2 rmind prop_dictionary_set_uint32(rpdict, "flags", fl | NPF_RPROC_NORMALIZE);
469 1.1 rmind return 0;
470 1.1 rmind }
471 1.1 rmind
472 1.1 rmind int
473 1.1 rmind _npf_rproc_setlog(nl_rproc_t *rp, u_int if_idx)
474 1.1 rmind {
475 1.1 rmind prop_dictionary_t rpdict = rp->nrp_dict;
476 1.7.2.5 riz uint32_t fl = 0;
477 1.1 rmind
478 1.1 rmind prop_dictionary_set_uint32(rpdict, "log-interface", if_idx);
479 1.1 rmind
480 1.2 rmind prop_dictionary_get_uint32(rpdict, "flags", &fl);
481 1.2 rmind prop_dictionary_set_uint32(rpdict, "flags", fl | NPF_RPROC_LOG);
482 1.1 rmind return 0;
483 1.1 rmind }
484 1.1 rmind
485 1.1 rmind int
486 1.1 rmind npf_rproc_insert(nl_config_t *ncf, nl_rproc_t *rp)
487 1.1 rmind {
488 1.1 rmind prop_dictionary_t rpdict = rp->nrp_dict;
489 1.1 rmind const char *name;
490 1.1 rmind
491 1.1 rmind if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
492 1.1 rmind return EINVAL;
493 1.1 rmind }
494 1.1 rmind if (npf_rproc_exists_p(ncf, name)) {
495 1.1 rmind return EEXIST;
496 1.1 rmind }
497 1.1 rmind prop_array_add(ncf->ncf_rproc_list, rpdict);
498 1.1 rmind return 0;
499 1.1 rmind }
500 1.1 rmind
501 1.1 rmind /*
502 1.1 rmind * TRANSLATION INTERFACE.
503 1.1 rmind */
504 1.1 rmind
505 1.1 rmind nl_nat_t *
506 1.5 christos npf_nat_create(int type, u_int flags, u_int if_idx,
507 1.1 rmind npf_addr_t *addr, int af, in_port_t port)
508 1.1 rmind {
509 1.1 rmind nl_rule_t *rl;
510 1.1 rmind prop_dictionary_t rldict;
511 1.1 rmind prop_data_t addrdat;
512 1.1 rmind uint32_t attr;
513 1.1 rmind size_t sz;
514 1.1 rmind
515 1.1 rmind if (af == AF_INET) {
516 1.1 rmind sz = sizeof(struct in_addr);
517 1.1 rmind } else if (af == AF_INET6) {
518 1.1 rmind sz = sizeof(struct in6_addr);
519 1.1 rmind } else {
520 1.1 rmind return NULL;
521 1.1 rmind }
522 1.1 rmind
523 1.1 rmind attr = NPF_RULE_PASS | NPF_RULE_FINAL |
524 1.2 rmind (type == NPF_NATOUT ? NPF_RULE_OUT : NPF_RULE_IN);
525 1.1 rmind
526 1.1 rmind /* Create a rule for NAT policy. Next, will add translation data. */
527 1.1 rmind rl = npf_rule_create(NULL, attr, if_idx);
528 1.1 rmind if (rl == NULL) {
529 1.1 rmind return NULL;
530 1.1 rmind }
531 1.1 rmind rldict = rl->nrl_dict;
532 1.1 rmind
533 1.1 rmind /* Translation type and flags. */
534 1.1 rmind prop_dictionary_set_int32(rldict, "type", type);
535 1.1 rmind prop_dictionary_set_uint32(rldict, "flags", flags);
536 1.1 rmind
537 1.1 rmind /* Translation IP. */
538 1.1 rmind addrdat = prop_data_create_data(addr, sz);
539 1.1 rmind if (addrdat == NULL) {
540 1.1 rmind npf_rule_destroy(rl);
541 1.1 rmind return NULL;
542 1.1 rmind }
543 1.1 rmind prop_dictionary_set(rldict, "translation-ip", addrdat);
544 1.1 rmind prop_object_release(addrdat);
545 1.1 rmind
546 1.1 rmind /* Translation port (for redirect case). */
547 1.1 rmind prop_dictionary_set_uint16(rldict, "translation-port", port);
548 1.1 rmind
549 1.1 rmind return (nl_nat_t *)rl;
550 1.1 rmind }
551 1.1 rmind
552 1.1 rmind int
553 1.1 rmind npf_nat_insert(nl_config_t *ncf, nl_nat_t *nt, pri_t pri)
554 1.1 rmind {
555 1.1 rmind prop_dictionary_t rldict = nt->nrl_dict;
556 1.1 rmind
557 1.1 rmind if (pri == NPF_PRI_NEXT) {
558 1.1 rmind pri = ncf->ncf_nat_pri++;
559 1.1 rmind } else {
560 1.1 rmind ncf->ncf_nat_pri = pri + 1;
561 1.1 rmind }
562 1.1 rmind prop_dictionary_set_int32(rldict, "priority", pri);
563 1.1 rmind prop_array_add(ncf->ncf_nat_list, rldict);
564 1.1 rmind return 0;
565 1.1 rmind }
566 1.1 rmind
567 1.7.2.2 riz int
568 1.7.2.2 riz _npf_nat_foreach(nl_config_t *ncf, nl_rule_callback_t func)
569 1.7.2.2 riz {
570 1.7.2.2 riz
571 1.7.2.2 riz return _npf_rule_foreach1(ncf->ncf_nat_list, 0, func);
572 1.7.2.2 riz }
573 1.7.2.2 riz
574 1.7.2.2 riz void
575 1.7.2.2 riz _npf_nat_getinfo(nl_nat_t *nt, int *type, u_int *flags, npf_addr_t *addr,
576 1.7.2.2 riz size_t *alen, in_port_t *port)
577 1.7.2.2 riz {
578 1.7.2.2 riz prop_dictionary_t rldict = nt->nrl_dict;
579 1.7.2.2 riz
580 1.7.2.2 riz prop_dictionary_get_int32(rldict, "type", type);
581 1.7.2.2 riz prop_dictionary_get_uint32(rldict, "flags", flags);
582 1.7.2.2 riz
583 1.7.2.2 riz prop_object_t obj = prop_dictionary_get(rldict, "translation-ip");
584 1.7.2.2 riz *alen = prop_data_size(obj);
585 1.7.2.2 riz memcpy(addr, prop_data_data_nocopy(obj), *alen);
586 1.7.2.2 riz
587 1.7.2.2 riz prop_dictionary_get_uint16(rldict, "translation-port", port);
588 1.7.2.2 riz }
589 1.7.2.2 riz
590 1.1 rmind /*
591 1.1 rmind * TABLE INTERFACE.
592 1.1 rmind */
593 1.1 rmind
594 1.1 rmind nl_table_t *
595 1.5 christos npf_table_create(u_int id, int type)
596 1.1 rmind {
597 1.1 rmind prop_dictionary_t tldict;
598 1.1 rmind prop_array_t tblents;
599 1.1 rmind nl_table_t *tl;
600 1.1 rmind
601 1.5 christos tl = malloc(sizeof(*tl));
602 1.1 rmind if (tl == NULL) {
603 1.1 rmind return NULL;
604 1.1 rmind }
605 1.1 rmind tldict = prop_dictionary_create();
606 1.1 rmind if (tldict == NULL) {
607 1.1 rmind free(tl);
608 1.1 rmind return NULL;
609 1.1 rmind }
610 1.1 rmind prop_dictionary_set_uint32(tldict, "id", id);
611 1.1 rmind prop_dictionary_set_int32(tldict, "type", type);
612 1.1 rmind
613 1.1 rmind tblents = prop_array_create();
614 1.1 rmind if (tblents == NULL) {
615 1.1 rmind prop_object_release(tldict);
616 1.1 rmind free(tl);
617 1.1 rmind return NULL;
618 1.1 rmind }
619 1.1 rmind prop_dictionary_set(tldict, "entries", tblents);
620 1.1 rmind prop_object_release(tblents);
621 1.1 rmind
622 1.1 rmind tl->ntl_dict = tldict;
623 1.1 rmind return tl;
624 1.1 rmind }
625 1.1 rmind
626 1.1 rmind int
627 1.7.2.3 riz npf_table_add_entry(nl_table_t *tl, const int alen,
628 1.7.2.3 riz const npf_addr_t *addr, const npf_netmask_t mask)
629 1.1 rmind {
630 1.1 rmind prop_dictionary_t tldict = tl->ntl_dict, entdict;
631 1.1 rmind prop_array_t tblents;
632 1.3 zoltan prop_data_t addrdata;
633 1.1 rmind
634 1.1 rmind /* Create the table entry. */
635 1.1 rmind entdict = prop_dictionary_create();
636 1.7.2.3 riz if (entdict == NULL) {
637 1.1 rmind return ENOMEM;
638 1.1 rmind }
639 1.7.2.3 riz addrdata = prop_data_create_data(addr, alen);
640 1.3 zoltan prop_dictionary_set(entdict, "addr", addrdata);
641 1.3 zoltan prop_dictionary_set_uint8(entdict, "mask", mask);
642 1.3 zoltan prop_object_release(addrdata);
643 1.1 rmind
644 1.1 rmind /* Insert the entry. */
645 1.1 rmind tblents = prop_dictionary_get(tldict, "entries");
646 1.1 rmind prop_array_add(tblents, entdict);
647 1.1 rmind prop_object_release(entdict);
648 1.1 rmind return 0;
649 1.1 rmind }
650 1.1 rmind
651 1.1 rmind bool
652 1.1 rmind npf_table_exists_p(nl_config_t *ncf, u_int tid)
653 1.1 rmind {
654 1.1 rmind prop_dictionary_t tldict;
655 1.1 rmind prop_object_iterator_t it;
656 1.1 rmind
657 1.1 rmind it = prop_array_iterator(ncf->ncf_table_list);
658 1.1 rmind while ((tldict = prop_object_iterator_next(it)) != NULL) {
659 1.1 rmind u_int i;
660 1.1 rmind if (prop_dictionary_get_uint32(tldict, "id", &i) && tid == i)
661 1.1 rmind break;
662 1.1 rmind }
663 1.1 rmind prop_object_iterator_release(it);
664 1.1 rmind return tldict ? true : false;
665 1.1 rmind }
666 1.1 rmind
667 1.1 rmind int
668 1.1 rmind npf_table_insert(nl_config_t *ncf, nl_table_t *tl)
669 1.1 rmind {
670 1.1 rmind prop_dictionary_t tldict = tl->ntl_dict;
671 1.1 rmind u_int tid;
672 1.1 rmind
673 1.1 rmind if (!prop_dictionary_get_uint32(tldict, "id", &tid)) {
674 1.1 rmind return EINVAL;
675 1.1 rmind }
676 1.1 rmind if (npf_table_exists_p(ncf, tid)) {
677 1.1 rmind return EEXIST;
678 1.1 rmind }
679 1.1 rmind prop_array_add(ncf->ncf_table_list, tldict);
680 1.1 rmind return 0;
681 1.1 rmind }
682 1.1 rmind
683 1.1 rmind void
684 1.1 rmind npf_table_destroy(nl_table_t *tl)
685 1.1 rmind {
686 1.1 rmind
687 1.1 rmind prop_object_release(tl->ntl_dict);
688 1.1 rmind free(tl);
689 1.1 rmind }
690 1.1 rmind
691 1.7.2.2 riz void
692 1.7.2.2 riz _npf_table_foreach(nl_config_t *ncf, nl_table_callback_t func)
693 1.7.2.2 riz {
694 1.7.2.2 riz prop_dictionary_t tldict;
695 1.7.2.2 riz prop_object_iterator_t it;
696 1.7.2.2 riz
697 1.7.2.2 riz it = prop_array_iterator(ncf->ncf_table_list);
698 1.7.2.2 riz while ((tldict = prop_object_iterator_next(it)) != NULL) {
699 1.7.2.2 riz u_int id;
700 1.7.2.2 riz int type;
701 1.7.2.2 riz
702 1.7.2.2 riz prop_dictionary_get_uint32(tldict, "id", &id);
703 1.7.2.2 riz prop_dictionary_get_int32(tldict, "type", &type);
704 1.7.2.2 riz (*func)(id, type);
705 1.7.2.2 riz }
706 1.7.2.2 riz prop_object_iterator_release(it);
707 1.7.2.2 riz }
708 1.7.2.2 riz
709 1.1 rmind /*
710 1.1 rmind * MISC.
711 1.1 rmind */
712 1.1 rmind
713 1.1 rmind int
714 1.5 christos npf_update_rule(int fd, const char *rname __unused, nl_rule_t *rl)
715 1.1 rmind {
716 1.7 rmind prop_dictionary_t rldict = rl->nrl_dict, errdict = NULL;
717 1.1 rmind int error;
718 1.1 rmind
719 1.7 rmind error = prop_dictionary_sendrecv_ioctl(rldict, fd,
720 1.7 rmind IOC_NPF_UPDATE_RULE, &errdict);
721 1.7 rmind if (errdict) {
722 1.7 rmind prop_object_release(errdict);
723 1.7 rmind }
724 1.1 rmind return error;
725 1.1 rmind }
726 1.1 rmind
727 1.1 rmind int
728 1.1 rmind npf_sessions_recv(int fd, const char *fpath)
729 1.1 rmind {
730 1.1 rmind prop_dictionary_t sdict;
731 1.1 rmind int error;
732 1.1 rmind
733 1.1 rmind error = prop_dictionary_recv_ioctl(fd, IOC_NPF_SESSIONS_SAVE, &sdict);
734 1.1 rmind if (error) {
735 1.1 rmind return error;
736 1.1 rmind }
737 1.1 rmind if (!prop_dictionary_externalize_to_file(sdict, fpath)) {
738 1.1 rmind error = errno;
739 1.1 rmind }
740 1.1 rmind prop_object_release(sdict);
741 1.1 rmind return error;
742 1.1 rmind }
743 1.1 rmind
744 1.1 rmind int
745 1.1 rmind npf_sessions_send(int fd, const char *fpath)
746 1.1 rmind {
747 1.1 rmind prop_dictionary_t sdict;
748 1.1 rmind int error;
749 1.1 rmind
750 1.1 rmind if (fpath) {
751 1.1 rmind sdict = prop_dictionary_internalize_from_file(fpath);
752 1.1 rmind if (sdict == NULL) {
753 1.1 rmind return errno;
754 1.1 rmind }
755 1.1 rmind } else {
756 1.1 rmind /* Empty: will flush the sessions. */
757 1.1 rmind prop_array_t selist = prop_array_create();
758 1.1 rmind sdict = prop_dictionary_create();
759 1.1 rmind prop_dictionary_set(sdict, "session-list", selist);
760 1.1 rmind prop_object_release(selist);
761 1.1 rmind }
762 1.1 rmind error = prop_dictionary_send_ioctl(sdict, fd, IOC_NPF_SESSIONS_LOAD);
763 1.1 rmind prop_object_release(sdict);
764 1.1 rmind return error;
765 1.1 rmind }
766 1.7.2.4 riz
767 1.7.2.4 riz static prop_dictionary_t
768 1.7.2.4 riz _npf_debug_initonce(nl_config_t *ncf)
769 1.7.2.4 riz {
770 1.7.2.4 riz if (!ncf->ncf_debug) {
771 1.7.2.4 riz prop_array_t iflist = prop_array_create();
772 1.7.2.4 riz ncf->ncf_debug = prop_dictionary_create();
773 1.7.2.4 riz prop_dictionary_set(ncf->ncf_debug, "interfaces", iflist);
774 1.7.2.4 riz prop_object_release(iflist);
775 1.7.2.4 riz }
776 1.7.2.4 riz return ncf->ncf_debug;
777 1.7.2.4 riz }
778 1.7.2.4 riz
779 1.7.2.4 riz void
780 1.7.2.4 riz _npf_debug_addif(nl_config_t *ncf, struct ifaddrs *ifa, u_int if_idx)
781 1.7.2.4 riz {
782 1.7.2.4 riz prop_dictionary_t ifdict, dbg = _npf_debug_initonce(ncf);
783 1.7.2.4 riz prop_array_t iflist = prop_dictionary_get(dbg, "interfaces");
784 1.7.2.4 riz
785 1.7.2.4 riz if (_npf_prop_array_lookup(iflist, "name", ifa->ifa_name)) {
786 1.7.2.4 riz return;
787 1.7.2.4 riz }
788 1.7.2.4 riz
789 1.7.2.4 riz ifdict = prop_dictionary_create();
790 1.7.2.4 riz prop_dictionary_set_cstring(ifdict, "name", ifa->ifa_name);
791 1.7.2.4 riz prop_dictionary_set_uint32(ifdict, "flags", ifa->ifa_flags);
792 1.7.2.4 riz if (!if_idx) {
793 1.7.2.4 riz if_idx = if_nametoindex(ifa->ifa_name);
794 1.7.2.4 riz }
795 1.7.2.4 riz prop_dictionary_set_uint32(ifdict, "idx", if_idx);
796 1.7.2.4 riz
797 1.7.2.4 riz const struct sockaddr *sa = ifa->ifa_addr;
798 1.7.2.4 riz npf_addr_t addr;
799 1.7.2.4 riz size_t alen = 0;
800 1.7.2.4 riz
801 1.7.2.4 riz switch (sa ? sa->sa_family : -1) {
802 1.7.2.4 riz case AF_INET: {
803 1.7.2.4 riz const struct sockaddr_in *sin = (const void *)sa;
804 1.7.2.4 riz alen = sizeof(sin->sin_addr);
805 1.7.2.4 riz memcpy(&addr, &sin->sin_addr, alen);
806 1.7.2.4 riz break;
807 1.7.2.4 riz }
808 1.7.2.4 riz case AF_INET6: {
809 1.7.2.4 riz const struct sockaddr_in6 *sin6 = (const void *)sa;
810 1.7.2.4 riz alen = sizeof(sin6->sin6_addr);
811 1.7.2.4 riz memcpy(&addr, &sin6->sin6_addr, alen);
812 1.7.2.4 riz break;
813 1.7.2.4 riz }
814 1.7.2.4 riz default:
815 1.7.2.4 riz break;
816 1.7.2.4 riz }
817 1.7.2.4 riz
818 1.7.2.4 riz if (alen) {
819 1.7.2.4 riz prop_data_t addrdata = prop_data_create_data(&addr, alen);
820 1.7.2.4 riz prop_dictionary_set(ifdict, "addr", addrdata);
821 1.7.2.4 riz prop_object_release(addrdata);
822 1.7.2.4 riz }
823 1.7.2.4 riz prop_array_add(iflist, ifdict);
824 1.7.2.4 riz prop_object_release(ifdict);
825 1.7.2.4 riz }
826