npf.c revision 1.2.2.2 1 1.2.2.2 bouyer /* $NetBSD: npf.c,v 1.2.2.2 2011/02/08 19:01:36 bouyer Exp $ */
2 1.2.2.2 bouyer
3 1.2.2.2 bouyer /*-
4 1.2.2.2 bouyer * Copyright (c) 2010-2011 The NetBSD Foundation, Inc.
5 1.2.2.2 bouyer * All rights reserved.
6 1.2.2.2 bouyer *
7 1.2.2.2 bouyer * This material is based upon work partially supported by The
8 1.2.2.2 bouyer * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.2.2.2 bouyer *
10 1.2.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.2.2.2 bouyer * are met:
13 1.2.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.2.2.2 bouyer *
19 1.2.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
30 1.2.2.2 bouyer */
31 1.2.2.2 bouyer
32 1.2.2.2 bouyer #include <sys/cdefs.h>
33 1.2.2.2 bouyer __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.2.2.2 2011/02/08 19:01:36 bouyer Exp $");
34 1.2.2.2 bouyer
35 1.2.2.2 bouyer #include <sys/types.h>
36 1.2.2.2 bouyer #include <netinet/in_systm.h>
37 1.2.2.2 bouyer #include <netinet/in.h>
38 1.2.2.2 bouyer #include <prop/proplib.h>
39 1.2.2.2 bouyer
40 1.2.2.2 bouyer #include <stdlib.h>
41 1.2.2.2 bouyer #include <string.h>
42 1.2.2.2 bouyer #include <errno.h>
43 1.2.2.2 bouyer #include <err.h>
44 1.2.2.2 bouyer
45 1.2.2.2 bouyer #define _NPF_PRIVATE
46 1.2.2.2 bouyer #include "npf.h"
47 1.2.2.2 bouyer
48 1.2.2.2 bouyer struct nl_config {
49 1.2.2.2 bouyer /* Rules, translations, tables, procedures. */
50 1.2.2.2 bouyer prop_array_t ncf_rules_list;
51 1.2.2.2 bouyer prop_array_t ncf_rproc_list;
52 1.2.2.2 bouyer prop_array_t ncf_table_list;
53 1.2.2.2 bouyer prop_array_t ncf_nat_list;
54 1.2.2.2 bouyer /* Priority counters. */
55 1.2.2.2 bouyer pri_t ncf_rule_pri;
56 1.2.2.2 bouyer pri_t ncf_nat_pri;
57 1.2.2.2 bouyer };
58 1.2.2.2 bouyer
59 1.2.2.2 bouyer struct nl_rule {
60 1.2.2.2 bouyer prop_dictionary_t nrl_dict;
61 1.2.2.2 bouyer };
62 1.2.2.2 bouyer
63 1.2.2.2 bouyer struct nl_rproc {
64 1.2.2.2 bouyer prop_dictionary_t nrp_dict;
65 1.2.2.2 bouyer };
66 1.2.2.2 bouyer
67 1.2.2.2 bouyer struct nl_table {
68 1.2.2.2 bouyer prop_dictionary_t ntl_dict;
69 1.2.2.2 bouyer };
70 1.2.2.2 bouyer
71 1.2.2.2 bouyer /*
72 1.2.2.2 bouyer * CONFIGURATION INTERFACE.
73 1.2.2.2 bouyer */
74 1.2.2.2 bouyer
75 1.2.2.2 bouyer nl_config_t *
76 1.2.2.2 bouyer npf_config_create(void)
77 1.2.2.2 bouyer {
78 1.2.2.2 bouyer nl_config_t *ncf;
79 1.2.2.2 bouyer
80 1.2.2.2 bouyer ncf = malloc(sizeof(nl_config_t));
81 1.2.2.2 bouyer if (ncf == NULL) {
82 1.2.2.2 bouyer return NULL;
83 1.2.2.2 bouyer }
84 1.2.2.2 bouyer ncf->ncf_rules_list = prop_array_create();
85 1.2.2.2 bouyer ncf->ncf_rproc_list = prop_array_create();
86 1.2.2.2 bouyer ncf->ncf_table_list = prop_array_create();
87 1.2.2.2 bouyer ncf->ncf_nat_list = prop_array_create();
88 1.2.2.2 bouyer
89 1.2.2.2 bouyer ncf->ncf_rule_pri = 1;
90 1.2.2.2 bouyer ncf->ncf_nat_pri = 1;
91 1.2.2.2 bouyer
92 1.2.2.2 bouyer return ncf;
93 1.2.2.2 bouyer }
94 1.2.2.2 bouyer
95 1.2.2.2 bouyer int
96 1.2.2.2 bouyer npf_config_submit(nl_config_t *ncf, int fd)
97 1.2.2.2 bouyer {
98 1.2.2.2 bouyer prop_dictionary_t npf_dict;
99 1.2.2.2 bouyer int error = 0;
100 1.2.2.2 bouyer
101 1.2.2.2 bouyer npf_dict = prop_dictionary_create();
102 1.2.2.2 bouyer if (npf_dict == NULL) {
103 1.2.2.2 bouyer return ENOMEM;
104 1.2.2.2 bouyer }
105 1.2.2.2 bouyer prop_dictionary_set(npf_dict, "rules", ncf->ncf_rules_list);
106 1.2.2.2 bouyer prop_dictionary_set(npf_dict, "rprocs", ncf->ncf_rproc_list);
107 1.2.2.2 bouyer prop_dictionary_set(npf_dict, "tables", ncf->ncf_table_list);
108 1.2.2.2 bouyer prop_dictionary_set(npf_dict, "translation", ncf->ncf_nat_list);
109 1.2.2.2 bouyer
110 1.2.2.2 bouyer #ifndef _NPF_TESTING
111 1.2.2.2 bouyer error = prop_dictionary_send_ioctl(npf_dict, fd, IOC_NPF_RELOAD);
112 1.2.2.2 bouyer #else
113 1.2.2.2 bouyer if (!prop_dictionary_externalize_to_file(npf_dict, "./npf.plist")) {
114 1.2.2.2 bouyer error = errno;
115 1.2.2.2 bouyer }
116 1.2.2.2 bouyer #endif
117 1.2.2.2 bouyer prop_object_release(npf_dict);
118 1.2.2.2 bouyer return error;
119 1.2.2.2 bouyer }
120 1.2.2.2 bouyer
121 1.2.2.2 bouyer void
122 1.2.2.2 bouyer npf_config_destroy(nl_config_t *ncf)
123 1.2.2.2 bouyer {
124 1.2.2.2 bouyer
125 1.2.2.2 bouyer prop_object_release(ncf->ncf_rules_list);
126 1.2.2.2 bouyer prop_object_release(ncf->ncf_rproc_list);
127 1.2.2.2 bouyer prop_object_release(ncf->ncf_table_list);
128 1.2.2.2 bouyer prop_object_release(ncf->ncf_nat_list);
129 1.2.2.2 bouyer free(ncf);
130 1.2.2.2 bouyer }
131 1.2.2.2 bouyer
132 1.2.2.2 bouyer static bool
133 1.2.2.2 bouyer _npf_prop_array_lookup(prop_array_t array, const char *key, const char *name)
134 1.2.2.2 bouyer {
135 1.2.2.2 bouyer prop_dictionary_t dict;
136 1.2.2.2 bouyer prop_object_iterator_t it;
137 1.2.2.2 bouyer
138 1.2.2.2 bouyer it = prop_array_iterator(array);
139 1.2.2.2 bouyer while ((dict = prop_object_iterator_next(it)) != NULL) {
140 1.2.2.2 bouyer const char *lname;
141 1.2.2.2 bouyer prop_dictionary_get_cstring_nocopy(dict, key, &lname);
142 1.2.2.2 bouyer if (strcmp(name, lname) == 0)
143 1.2.2.2 bouyer break;
144 1.2.2.2 bouyer }
145 1.2.2.2 bouyer prop_object_iterator_release(it);
146 1.2.2.2 bouyer return dict ? true : false;
147 1.2.2.2 bouyer }
148 1.2.2.2 bouyer
149 1.2.2.2 bouyer /*
150 1.2.2.2 bouyer * RULE INTERFACE.
151 1.2.2.2 bouyer */
152 1.2.2.2 bouyer
153 1.2.2.2 bouyer nl_rule_t *
154 1.2.2.2 bouyer npf_rule_create(const char *name, uint32_t attr, u_int if_idx)
155 1.2.2.2 bouyer {
156 1.2.2.2 bouyer prop_dictionary_t rldict;
157 1.2.2.2 bouyer nl_rule_t *rl;
158 1.2.2.2 bouyer
159 1.2.2.2 bouyer rl = malloc(sizeof(nl_rule_t));
160 1.2.2.2 bouyer if (rl == NULL) {
161 1.2.2.2 bouyer return NULL;
162 1.2.2.2 bouyer }
163 1.2.2.2 bouyer rldict = prop_dictionary_create();
164 1.2.2.2 bouyer if (rldict == NULL) {
165 1.2.2.2 bouyer free(rl);
166 1.2.2.2 bouyer return NULL;
167 1.2.2.2 bouyer }
168 1.2.2.2 bouyer if (name) {
169 1.2.2.2 bouyer prop_dictionary_set_cstring(rldict, "name", name);
170 1.2.2.2 bouyer }
171 1.2.2.2 bouyer prop_dictionary_set_uint32(rldict, "attributes", attr);
172 1.2.2.2 bouyer
173 1.2.2.2 bouyer if (if_idx) {
174 1.2.2.2 bouyer prop_dictionary_set_uint32(rldict, "interface", if_idx);
175 1.2.2.2 bouyer }
176 1.2.2.2 bouyer rl->nrl_dict = rldict;
177 1.2.2.2 bouyer return rl;
178 1.2.2.2 bouyer }
179 1.2.2.2 bouyer
180 1.2.2.2 bouyer int
181 1.2.2.2 bouyer npf_rule_setcode(nl_rule_t *rl, int type, const void *code, size_t sz)
182 1.2.2.2 bouyer {
183 1.2.2.2 bouyer prop_dictionary_t rldict = rl->nrl_dict;
184 1.2.2.2 bouyer prop_data_t cdata;
185 1.2.2.2 bouyer
186 1.2.2.2 bouyer if (type != NPF_CODE_NCODE) {
187 1.2.2.2 bouyer return ENOTSUP;
188 1.2.2.2 bouyer }
189 1.2.2.2 bouyer cdata = prop_data_create_data(code, sz);
190 1.2.2.2 bouyer if (cdata == NULL) {
191 1.2.2.2 bouyer return ENOMEM;
192 1.2.2.2 bouyer }
193 1.2.2.2 bouyer prop_dictionary_set(rldict, "ncode", cdata);
194 1.2.2.2 bouyer prop_object_release(cdata);
195 1.2.2.2 bouyer return 0;
196 1.2.2.2 bouyer }
197 1.2.2.2 bouyer
198 1.2.2.2 bouyer int
199 1.2.2.2 bouyer npf_rule_setproc(nl_config_t *ncf, nl_rule_t *rl, const char *name)
200 1.2.2.2 bouyer {
201 1.2.2.2 bouyer prop_dictionary_t rldict = rl->nrl_dict;
202 1.2.2.2 bouyer
203 1.2.2.2 bouyer if (!npf_rproc_exists_p(ncf, name)) {
204 1.2.2.2 bouyer return ENOENT;
205 1.2.2.2 bouyer }
206 1.2.2.2 bouyer prop_dictionary_set_cstring(rldict, "rproc", name);
207 1.2.2.2 bouyer return 0;
208 1.2.2.2 bouyer }
209 1.2.2.2 bouyer
210 1.2.2.2 bouyer bool
211 1.2.2.2 bouyer npf_rule_exists_p(nl_config_t *ncf, const char *name)
212 1.2.2.2 bouyer {
213 1.2.2.2 bouyer
214 1.2.2.2 bouyer return _npf_prop_array_lookup(ncf->ncf_rules_list, "name", name);
215 1.2.2.2 bouyer }
216 1.2.2.2 bouyer
217 1.2.2.2 bouyer int
218 1.2.2.2 bouyer npf_rule_insert(nl_config_t *ncf, nl_rule_t *parent, nl_rule_t *rl, pri_t pri)
219 1.2.2.2 bouyer {
220 1.2.2.2 bouyer prop_dictionary_t rldict = rl->nrl_dict;
221 1.2.2.2 bouyer prop_array_t rlset;
222 1.2.2.2 bouyer
223 1.2.2.2 bouyer if (pri == NPF_PRI_NEXT) {
224 1.2.2.2 bouyer pri = ncf->ncf_rule_pri++;
225 1.2.2.2 bouyer } else if (ncf) {
226 1.2.2.2 bouyer ncf->ncf_rule_pri = pri + 1;
227 1.2.2.2 bouyer }
228 1.2.2.2 bouyer prop_dictionary_set_int32(rldict, "priority", pri);
229 1.2.2.2 bouyer
230 1.2.2.2 bouyer if (parent) {
231 1.2.2.2 bouyer prop_dictionary_t pdict = parent->nrl_dict;
232 1.2.2.2 bouyer rlset = prop_dictionary_get(pdict, "subrules");
233 1.2.2.2 bouyer if (rlset == NULL) {
234 1.2.2.2 bouyer rlset = prop_array_create();
235 1.2.2.2 bouyer prop_dictionary_set(pdict, "subrules", rlset);
236 1.2.2.2 bouyer prop_object_release(rlset);
237 1.2.2.2 bouyer }
238 1.2.2.2 bouyer } else {
239 1.2.2.2 bouyer rlset = ncf->ncf_rules_list;
240 1.2.2.2 bouyer }
241 1.2.2.2 bouyer prop_array_add(rlset, rldict);
242 1.2.2.2 bouyer return 0;
243 1.2.2.2 bouyer }
244 1.2.2.2 bouyer
245 1.2.2.2 bouyer void
246 1.2.2.2 bouyer npf_rule_destroy(nl_rule_t *rl)
247 1.2.2.2 bouyer {
248 1.2.2.2 bouyer
249 1.2.2.2 bouyer prop_object_release(rl->nrl_dict);
250 1.2.2.2 bouyer free(rl);
251 1.2.2.2 bouyer }
252 1.2.2.2 bouyer
253 1.2.2.2 bouyer /*
254 1.2.2.2 bouyer * RULE PROCEDURE INTERFACE.
255 1.2.2.2 bouyer */
256 1.2.2.2 bouyer
257 1.2.2.2 bouyer nl_rproc_t *
258 1.2.2.2 bouyer npf_rproc_create(const char *name)
259 1.2.2.2 bouyer {
260 1.2.2.2 bouyer prop_dictionary_t rpdict;
261 1.2.2.2 bouyer nl_rproc_t *nrp;
262 1.2.2.2 bouyer
263 1.2.2.2 bouyer nrp = malloc(sizeof(nl_rproc_t));
264 1.2.2.2 bouyer if (nrp == NULL) {
265 1.2.2.2 bouyer return NULL;
266 1.2.2.2 bouyer }
267 1.2.2.2 bouyer rpdict = prop_dictionary_create();
268 1.2.2.2 bouyer if (rpdict == NULL) {
269 1.2.2.2 bouyer free(nrp);
270 1.2.2.2 bouyer return NULL;
271 1.2.2.2 bouyer }
272 1.2.2.2 bouyer prop_dictionary_set_cstring(rpdict, "name", name);
273 1.2.2.2 bouyer nrp->nrp_dict = rpdict;
274 1.2.2.2 bouyer return nrp;
275 1.2.2.2 bouyer }
276 1.2.2.2 bouyer
277 1.2.2.2 bouyer bool
278 1.2.2.2 bouyer npf_rproc_exists_p(nl_config_t *ncf, const char *name)
279 1.2.2.2 bouyer {
280 1.2.2.2 bouyer
281 1.2.2.2 bouyer return _npf_prop_array_lookup(ncf->ncf_rproc_list, "name", name);
282 1.2.2.2 bouyer }
283 1.2.2.2 bouyer
284 1.2.2.2 bouyer int
285 1.2.2.2 bouyer _npf_rproc_setnorm(nl_rproc_t *rp, bool rnd, bool no_df, int minttl, int maxmss)
286 1.2.2.2 bouyer {
287 1.2.2.2 bouyer prop_dictionary_t rpdict = rp->nrp_dict;
288 1.2.2.2 bouyer uint32_t fl;
289 1.2.2.2 bouyer
290 1.2.2.2 bouyer prop_dictionary_set_bool(rpdict, "randomize-id", rnd);
291 1.2.2.2 bouyer prop_dictionary_set_bool(rpdict, "no-df", no_df);
292 1.2.2.2 bouyer prop_dictionary_set_uint32(rpdict, "min-ttl", minttl);
293 1.2.2.2 bouyer prop_dictionary_set_uint32(rpdict, "max-mss", maxmss);
294 1.2.2.2 bouyer
295 1.2.2.2 bouyer prop_dictionary_get_uint32(rpdict, "flags", &fl);
296 1.2.2.2 bouyer prop_dictionary_set_uint32(rpdict, "flags", fl | NPF_RPROC_NORMALIZE);
297 1.2.2.2 bouyer return 0;
298 1.2.2.2 bouyer }
299 1.2.2.2 bouyer
300 1.2.2.2 bouyer int
301 1.2.2.2 bouyer _npf_rproc_setlog(nl_rproc_t *rp, u_int if_idx)
302 1.2.2.2 bouyer {
303 1.2.2.2 bouyer prop_dictionary_t rpdict = rp->nrp_dict;
304 1.2.2.2 bouyer uint32_t fl;
305 1.2.2.2 bouyer
306 1.2.2.2 bouyer prop_dictionary_set_uint32(rpdict, "log-interface", if_idx);
307 1.2.2.2 bouyer
308 1.2.2.2 bouyer prop_dictionary_get_uint32(rpdict, "flags", &fl);
309 1.2.2.2 bouyer prop_dictionary_set_uint32(rpdict, "flags", fl | NPF_RPROC_LOG);
310 1.2.2.2 bouyer return 0;
311 1.2.2.2 bouyer }
312 1.2.2.2 bouyer
313 1.2.2.2 bouyer int
314 1.2.2.2 bouyer npf_rproc_insert(nl_config_t *ncf, nl_rproc_t *rp)
315 1.2.2.2 bouyer {
316 1.2.2.2 bouyer prop_dictionary_t rpdict = rp->nrp_dict;
317 1.2.2.2 bouyer const char *name;
318 1.2.2.2 bouyer
319 1.2.2.2 bouyer if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
320 1.2.2.2 bouyer return EINVAL;
321 1.2.2.2 bouyer }
322 1.2.2.2 bouyer if (npf_rproc_exists_p(ncf, name)) {
323 1.2.2.2 bouyer return EEXIST;
324 1.2.2.2 bouyer }
325 1.2.2.2 bouyer prop_array_add(ncf->ncf_rproc_list, rpdict);
326 1.2.2.2 bouyer return 0;
327 1.2.2.2 bouyer }
328 1.2.2.2 bouyer
329 1.2.2.2 bouyer /*
330 1.2.2.2 bouyer * TRANSLATION INTERFACE.
331 1.2.2.2 bouyer */
332 1.2.2.2 bouyer
333 1.2.2.2 bouyer nl_nat_t *
334 1.2.2.2 bouyer npf_nat_create(int type, int flags, u_int if_idx,
335 1.2.2.2 bouyer npf_addr_t *addr, int af, in_port_t port)
336 1.2.2.2 bouyer {
337 1.2.2.2 bouyer nl_rule_t *rl;
338 1.2.2.2 bouyer prop_dictionary_t rldict;
339 1.2.2.2 bouyer prop_data_t addrdat;
340 1.2.2.2 bouyer uint32_t attr;
341 1.2.2.2 bouyer size_t sz;
342 1.2.2.2 bouyer
343 1.2.2.2 bouyer if (af == AF_INET) {
344 1.2.2.2 bouyer sz = sizeof(struct in_addr);
345 1.2.2.2 bouyer } else if (af == AF_INET6) {
346 1.2.2.2 bouyer sz = sizeof(struct in6_addr);
347 1.2.2.2 bouyer } else {
348 1.2.2.2 bouyer return NULL;
349 1.2.2.2 bouyer }
350 1.2.2.2 bouyer
351 1.2.2.2 bouyer attr = NPF_RULE_PASS | NPF_RULE_FINAL |
352 1.2.2.2 bouyer (type == NPF_NATOUT ? NPF_RULE_OUT : NPF_RULE_IN);
353 1.2.2.2 bouyer
354 1.2.2.2 bouyer /* Create a rule for NAT policy. Next, will add translation data. */
355 1.2.2.2 bouyer rl = npf_rule_create(NULL, attr, if_idx);
356 1.2.2.2 bouyer if (rl == NULL) {
357 1.2.2.2 bouyer return NULL;
358 1.2.2.2 bouyer }
359 1.2.2.2 bouyer rldict = rl->nrl_dict;
360 1.2.2.2 bouyer
361 1.2.2.2 bouyer /* Translation type and flags. */
362 1.2.2.2 bouyer prop_dictionary_set_int32(rldict, "type", type);
363 1.2.2.2 bouyer prop_dictionary_set_uint32(rldict, "flags", flags);
364 1.2.2.2 bouyer
365 1.2.2.2 bouyer /* Translation IP. */
366 1.2.2.2 bouyer addrdat = prop_data_create_data(addr, sz);
367 1.2.2.2 bouyer if (addrdat == NULL) {
368 1.2.2.2 bouyer npf_rule_destroy(rl);
369 1.2.2.2 bouyer return NULL;
370 1.2.2.2 bouyer }
371 1.2.2.2 bouyer prop_dictionary_set(rldict, "translation-ip", addrdat);
372 1.2.2.2 bouyer prop_object_release(addrdat);
373 1.2.2.2 bouyer
374 1.2.2.2 bouyer /* Translation port (for redirect case). */
375 1.2.2.2 bouyer prop_dictionary_set_uint16(rldict, "translation-port", port);
376 1.2.2.2 bouyer
377 1.2.2.2 bouyer return (nl_nat_t *)rl;
378 1.2.2.2 bouyer }
379 1.2.2.2 bouyer
380 1.2.2.2 bouyer int
381 1.2.2.2 bouyer npf_nat_insert(nl_config_t *ncf, nl_nat_t *nt, pri_t pri)
382 1.2.2.2 bouyer {
383 1.2.2.2 bouyer prop_dictionary_t rldict = nt->nrl_dict;
384 1.2.2.2 bouyer
385 1.2.2.2 bouyer if (pri == NPF_PRI_NEXT) {
386 1.2.2.2 bouyer pri = ncf->ncf_nat_pri++;
387 1.2.2.2 bouyer } else {
388 1.2.2.2 bouyer ncf->ncf_nat_pri = pri + 1;
389 1.2.2.2 bouyer }
390 1.2.2.2 bouyer prop_dictionary_set_int32(rldict, "priority", pri);
391 1.2.2.2 bouyer prop_array_add(ncf->ncf_nat_list, rldict);
392 1.2.2.2 bouyer return 0;
393 1.2.2.2 bouyer }
394 1.2.2.2 bouyer
395 1.2.2.2 bouyer /*
396 1.2.2.2 bouyer * TABLE INTERFACE.
397 1.2.2.2 bouyer */
398 1.2.2.2 bouyer
399 1.2.2.2 bouyer nl_table_t *
400 1.2.2.2 bouyer npf_table_create(int id, int type)
401 1.2.2.2 bouyer {
402 1.2.2.2 bouyer prop_dictionary_t tldict;
403 1.2.2.2 bouyer prop_array_t tblents;
404 1.2.2.2 bouyer nl_table_t *tl;
405 1.2.2.2 bouyer
406 1.2.2.2 bouyer tl = malloc(sizeof(nl_table_t));
407 1.2.2.2 bouyer if (tl == NULL) {
408 1.2.2.2 bouyer return NULL;
409 1.2.2.2 bouyer }
410 1.2.2.2 bouyer tldict = prop_dictionary_create();
411 1.2.2.2 bouyer if (tldict == NULL) {
412 1.2.2.2 bouyer free(tl);
413 1.2.2.2 bouyer return NULL;
414 1.2.2.2 bouyer }
415 1.2.2.2 bouyer prop_dictionary_set_uint32(tldict, "id", id);
416 1.2.2.2 bouyer prop_dictionary_set_int32(tldict, "type", type);
417 1.2.2.2 bouyer
418 1.2.2.2 bouyer tblents = prop_array_create();
419 1.2.2.2 bouyer if (tblents == NULL) {
420 1.2.2.2 bouyer prop_object_release(tldict);
421 1.2.2.2 bouyer free(tl);
422 1.2.2.2 bouyer return NULL;
423 1.2.2.2 bouyer }
424 1.2.2.2 bouyer prop_dictionary_set(tldict, "entries", tblents);
425 1.2.2.2 bouyer prop_object_release(tblents);
426 1.2.2.2 bouyer
427 1.2.2.2 bouyer tl->ntl_dict = tldict;
428 1.2.2.2 bouyer return tl;
429 1.2.2.2 bouyer }
430 1.2.2.2 bouyer
431 1.2.2.2 bouyer int
432 1.2.2.2 bouyer npf_table_add_entry(nl_table_t *tl, in_addr_t addr, in_addr_t mask)
433 1.2.2.2 bouyer {
434 1.2.2.2 bouyer prop_dictionary_t tldict = tl->ntl_dict, entdict;
435 1.2.2.2 bouyer prop_array_t tblents;
436 1.2.2.2 bouyer
437 1.2.2.2 bouyer /* Create the table entry. */
438 1.2.2.2 bouyer entdict = prop_dictionary_create();
439 1.2.2.2 bouyer if (entdict) {
440 1.2.2.2 bouyer return ENOMEM;
441 1.2.2.2 bouyer }
442 1.2.2.2 bouyer prop_dictionary_set_uint32(entdict, "addr", addr);
443 1.2.2.2 bouyer prop_dictionary_set_uint32(entdict, "mask", mask);
444 1.2.2.2 bouyer
445 1.2.2.2 bouyer /* Insert the entry. */
446 1.2.2.2 bouyer tblents = prop_dictionary_get(tldict, "entries");
447 1.2.2.2 bouyer prop_array_add(tblents, entdict);
448 1.2.2.2 bouyer prop_object_release(entdict);
449 1.2.2.2 bouyer return 0;
450 1.2.2.2 bouyer }
451 1.2.2.2 bouyer
452 1.2.2.2 bouyer bool
453 1.2.2.2 bouyer npf_table_exists_p(nl_config_t *ncf, u_int tid)
454 1.2.2.2 bouyer {
455 1.2.2.2 bouyer prop_dictionary_t tldict;
456 1.2.2.2 bouyer prop_object_iterator_t it;
457 1.2.2.2 bouyer
458 1.2.2.2 bouyer it = prop_array_iterator(ncf->ncf_table_list);
459 1.2.2.2 bouyer while ((tldict = prop_object_iterator_next(it)) != NULL) {
460 1.2.2.2 bouyer u_int i;
461 1.2.2.2 bouyer if (prop_dictionary_get_uint32(tldict, "id", &i) && tid == i)
462 1.2.2.2 bouyer break;
463 1.2.2.2 bouyer }
464 1.2.2.2 bouyer prop_object_iterator_release(it);
465 1.2.2.2 bouyer return tldict ? true : false;
466 1.2.2.2 bouyer }
467 1.2.2.2 bouyer
468 1.2.2.2 bouyer int
469 1.2.2.2 bouyer npf_table_insert(nl_config_t *ncf, nl_table_t *tl)
470 1.2.2.2 bouyer {
471 1.2.2.2 bouyer prop_dictionary_t tldict = tl->ntl_dict;
472 1.2.2.2 bouyer u_int tid;
473 1.2.2.2 bouyer
474 1.2.2.2 bouyer if (!prop_dictionary_get_uint32(tldict, "id", &tid)) {
475 1.2.2.2 bouyer return EINVAL;
476 1.2.2.2 bouyer }
477 1.2.2.2 bouyer if (npf_table_exists_p(ncf, tid)) {
478 1.2.2.2 bouyer return EEXIST;
479 1.2.2.2 bouyer }
480 1.2.2.2 bouyer prop_array_add(ncf->ncf_table_list, tldict);
481 1.2.2.2 bouyer return 0;
482 1.2.2.2 bouyer }
483 1.2.2.2 bouyer
484 1.2.2.2 bouyer void
485 1.2.2.2 bouyer npf_table_destroy(nl_table_t *tl)
486 1.2.2.2 bouyer {
487 1.2.2.2 bouyer
488 1.2.2.2 bouyer prop_object_release(tl->ntl_dict);
489 1.2.2.2 bouyer free(tl);
490 1.2.2.2 bouyer }
491 1.2.2.2 bouyer
492 1.2.2.2 bouyer /*
493 1.2.2.2 bouyer * MISC.
494 1.2.2.2 bouyer */
495 1.2.2.2 bouyer
496 1.2.2.2 bouyer int
497 1.2.2.2 bouyer npf_update_rule(int fd, char *rname, nl_rule_t *rl)
498 1.2.2.2 bouyer {
499 1.2.2.2 bouyer prop_dictionary_t rldict = rl->nrl_dict;
500 1.2.2.2 bouyer int error;
501 1.2.2.2 bouyer
502 1.2.2.2 bouyer error = prop_dictionary_send_ioctl(rldict, fd, IOC_NPF_UPDATE_RULE);
503 1.2.2.2 bouyer return error;
504 1.2.2.2 bouyer }
505 1.2.2.2 bouyer
506 1.2.2.2 bouyer int
507 1.2.2.2 bouyer npf_sessions_recv(int fd, const char *fpath)
508 1.2.2.2 bouyer {
509 1.2.2.2 bouyer prop_dictionary_t sdict;
510 1.2.2.2 bouyer int error;
511 1.2.2.2 bouyer
512 1.2.2.2 bouyer error = prop_dictionary_recv_ioctl(fd, IOC_NPF_SESSIONS_SAVE, &sdict);
513 1.2.2.2 bouyer if (error) {
514 1.2.2.2 bouyer return error;
515 1.2.2.2 bouyer }
516 1.2.2.2 bouyer if (!prop_dictionary_externalize_to_file(sdict, fpath)) {
517 1.2.2.2 bouyer error = errno;
518 1.2.2.2 bouyer }
519 1.2.2.2 bouyer prop_object_release(sdict);
520 1.2.2.2 bouyer return error;
521 1.2.2.2 bouyer }
522 1.2.2.2 bouyer
523 1.2.2.2 bouyer int
524 1.2.2.2 bouyer npf_sessions_send(int fd, const char *fpath)
525 1.2.2.2 bouyer {
526 1.2.2.2 bouyer prop_dictionary_t sdict;
527 1.2.2.2 bouyer int error;
528 1.2.2.2 bouyer
529 1.2.2.2 bouyer if (fpath) {
530 1.2.2.2 bouyer sdict = prop_dictionary_internalize_from_file(fpath);
531 1.2.2.2 bouyer if (sdict == NULL) {
532 1.2.2.2 bouyer return errno;
533 1.2.2.2 bouyer }
534 1.2.2.2 bouyer } else {
535 1.2.2.2 bouyer /* Empty: will flush the sessions. */
536 1.2.2.2 bouyer prop_array_t selist = prop_array_create();
537 1.2.2.2 bouyer sdict = prop_dictionary_create();
538 1.2.2.2 bouyer prop_dictionary_set(sdict, "session-list", selist);
539 1.2.2.2 bouyer prop_object_release(selist);
540 1.2.2.2 bouyer }
541 1.2.2.2 bouyer error = prop_dictionary_send_ioctl(sdict, fd, IOC_NPF_SESSIONS_LOAD);
542 1.2.2.2 bouyer prop_object_release(sdict);
543 1.2.2.2 bouyer return error;
544 1.2.2.2 bouyer }
545