npf_build.c revision 1.4.2.4 1 1.4.2.4 riz /* $NetBSD: npf_build.c,v 1.4.2.4 2012/07/16 22:13:28 riz Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2011-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 /*
33 1.1 rmind * npfctl(8) building of the configuration.
34 1.1 rmind */
35 1.1 rmind
36 1.1 rmind #include <sys/cdefs.h>
37 1.4.2.4 riz __RCSID("$NetBSD: npf_build.c,v 1.4.2.4 2012/07/16 22:13:28 riz Exp $");
38 1.1 rmind
39 1.1 rmind #include <sys/types.h>
40 1.1 rmind #include <sys/ioctl.h>
41 1.1 rmind
42 1.1 rmind #include <stdlib.h>
43 1.1 rmind #include <inttypes.h>
44 1.1 rmind #include <string.h>
45 1.1 rmind #include <err.h>
46 1.1 rmind
47 1.1 rmind #include "npfctl.h"
48 1.1 rmind
49 1.1 rmind static nl_config_t * npf_conf = NULL;
50 1.1 rmind static nl_rule_t * current_group = NULL;
51 1.1 rmind static bool npf_debug = false;
52 1.1 rmind static bool defgroup_set = false;
53 1.1 rmind
54 1.1 rmind void
55 1.1 rmind npfctl_config_init(bool debug)
56 1.1 rmind {
57 1.1 rmind
58 1.1 rmind npf_conf = npf_config_create();
59 1.1 rmind if (npf_conf == NULL) {
60 1.1 rmind errx(EXIT_FAILURE, "npf_config_create failed");
61 1.1 rmind }
62 1.1 rmind npf_debug = debug;
63 1.1 rmind }
64 1.1 rmind
65 1.1 rmind int
66 1.1 rmind npfctl_config_send(int fd)
67 1.1 rmind {
68 1.1 rmind int error;
69 1.1 rmind
70 1.1 rmind if (!fd) {
71 1.4.2.3 riz const char *outconf = "/tmp/npf.plist";
72 1.4.2.3 riz _npf_config_setsubmit(npf_conf, outconf);
73 1.4.2.3 riz printf("\nSaving to %s\n", outconf);
74 1.1 rmind }
75 1.1 rmind if (!defgroup_set) {
76 1.1 rmind errx(EXIT_FAILURE, "default group was not defined");
77 1.1 rmind }
78 1.1 rmind error = npf_config_submit(npf_conf, fd);
79 1.3 rmind if (error) {
80 1.3 rmind nl_error_t ne;
81 1.3 rmind _npf_config_error(npf_conf, &ne);
82 1.3 rmind npfctl_print_error(&ne);
83 1.3 rmind }
84 1.1 rmind npf_config_destroy(npf_conf);
85 1.1 rmind return error;
86 1.1 rmind }
87 1.1 rmind
88 1.1 rmind bool
89 1.1 rmind npfctl_table_exists_p(const char *id)
90 1.1 rmind {
91 1.1 rmind return npf_table_exists_p(npf_conf, atoi(id));
92 1.1 rmind }
93 1.1 rmind
94 1.4.2.2 riz static in_port_t
95 1.1 rmind npfctl_get_singleport(const npfvar_t *vp)
96 1.1 rmind {
97 1.1 rmind port_range_t *pr;
98 1.4.2.2 riz in_port_t *port;
99 1.1 rmind
100 1.1 rmind if (npfvar_get_count(vp) > 1) {
101 1.1 rmind yyerror("multiple ports are not valid");
102 1.1 rmind }
103 1.1 rmind pr = npfvar_get_data(vp, NPFVAR_PORT_RANGE, 0);
104 1.1 rmind if (pr->pr_start != pr->pr_end) {
105 1.1 rmind yyerror("port range is not valid");
106 1.1 rmind }
107 1.4.2.2 riz port = &pr->pr_start;
108 1.4.2.2 riz return *port;
109 1.1 rmind }
110 1.1 rmind
111 1.1 rmind static fam_addr_mask_t *
112 1.1 rmind npfctl_get_singlefam(const npfvar_t *vp)
113 1.1 rmind {
114 1.1 rmind if (npfvar_get_count(vp) > 1) {
115 1.1 rmind yyerror("multiple addresses are not valid");
116 1.1 rmind }
117 1.1 rmind return npfvar_get_data(vp, NPFVAR_FAM, 0);
118 1.1 rmind }
119 1.1 rmind
120 1.4.2.3 riz static bool
121 1.1 rmind npfctl_build_fam(nc_ctx_t *nc, sa_family_t family,
122 1.1 rmind fam_addr_mask_t *fam, int opts)
123 1.1 rmind {
124 1.1 rmind /*
125 1.1 rmind * If family is specified, address does not match it and the
126 1.1 rmind * address is extracted from the interface, then simply ignore.
127 1.1 rmind * Otherwise, address of invalid family was passed manually.
128 1.1 rmind */
129 1.1 rmind if (family != AF_UNSPEC && family != fam->fam_family) {
130 1.1 rmind if (!fam->fam_interface) {
131 1.1 rmind yyerror("specified address is not of the required "
132 1.1 rmind "family %d", family);
133 1.1 rmind }
134 1.4.2.3 riz return false;
135 1.1 rmind }
136 1.1 rmind
137 1.1 rmind /*
138 1.1 rmind * Optimise 0.0.0.0/0 case to be NOP. Otherwise, address with
139 1.1 rmind * zero mask would never match and therefore is not valid.
140 1.1 rmind */
141 1.1 rmind if (fam->fam_mask == 0) {
142 1.1 rmind npf_addr_t zero;
143 1.4.2.3 riz
144 1.1 rmind memset(&zero, 0, sizeof(npf_addr_t));
145 1.1 rmind if (memcmp(&fam->fam_addr, &zero, sizeof(npf_addr_t))) {
146 1.1 rmind yyerror("filter criterion would never match");
147 1.1 rmind }
148 1.4.2.3 riz return false;
149 1.1 rmind }
150 1.1 rmind
151 1.1 rmind switch (fam->fam_family) {
152 1.1 rmind case AF_INET:
153 1.1 rmind npfctl_gennc_v4cidr(nc, opts,
154 1.1 rmind &fam->fam_addr, fam->fam_mask);
155 1.1 rmind break;
156 1.1 rmind case AF_INET6:
157 1.1 rmind npfctl_gennc_v6cidr(nc, opts,
158 1.1 rmind &fam->fam_addr, fam->fam_mask);
159 1.1 rmind break;
160 1.1 rmind default:
161 1.1 rmind yyerror("family %d is not supported", fam->fam_family);
162 1.1 rmind }
163 1.4.2.3 riz return true;
164 1.1 rmind }
165 1.1 rmind
166 1.1 rmind static void
167 1.1 rmind npfctl_build_vars(nc_ctx_t *nc, sa_family_t family, npfvar_t *vars, int opts)
168 1.1 rmind {
169 1.4.2.2 riz const int type = npfvar_get_type(vars, 0);
170 1.1 rmind size_t i;
171 1.1 rmind
172 1.1 rmind npfctl_ncgen_group(nc);
173 1.1 rmind for (i = 0; i < npfvar_get_count(vars); i++) {
174 1.1 rmind void *data = npfvar_get_data(vars, type, i);
175 1.1 rmind assert(data != NULL);
176 1.1 rmind
177 1.1 rmind switch (type) {
178 1.1 rmind case NPFVAR_FAM: {
179 1.1 rmind fam_addr_mask_t *fam = data;
180 1.1 rmind npfctl_build_fam(nc, family, fam, opts);
181 1.1 rmind break;
182 1.1 rmind }
183 1.1 rmind case NPFVAR_PORT_RANGE: {
184 1.1 rmind port_range_t *pr = data;
185 1.1 rmind if (opts & NC_MATCH_TCP) {
186 1.1 rmind npfctl_gennc_ports(nc, opts & ~NC_MATCH_UDP,
187 1.1 rmind pr->pr_start, pr->pr_end);
188 1.1 rmind }
189 1.1 rmind if (opts & NC_MATCH_UDP) {
190 1.1 rmind npfctl_gennc_ports(nc, opts & ~NC_MATCH_TCP,
191 1.1 rmind pr->pr_start, pr->pr_end);
192 1.1 rmind }
193 1.1 rmind break;
194 1.1 rmind }
195 1.1 rmind case NPFVAR_TABLE: {
196 1.1 rmind u_int tid = atoi(data);
197 1.1 rmind npfctl_gennc_tbl(nc, opts, tid);
198 1.1 rmind break;
199 1.1 rmind }
200 1.1 rmind default:
201 1.1 rmind assert(false);
202 1.1 rmind }
203 1.1 rmind }
204 1.1 rmind npfctl_ncgen_endgroup(nc);
205 1.1 rmind }
206 1.1 rmind
207 1.1 rmind static int
208 1.4.2.3 riz npfctl_build_proto(nc_ctx_t *nc, sa_family_t family,
209 1.4.2.3 riz const opt_proto_t *op, bool nof, bool nop)
210 1.1 rmind {
211 1.1 rmind const npfvar_t *popts = op->op_opts;
212 1.4.2.3 riz const int proto = op->op_proto;
213 1.1 rmind int pflag = 0;
214 1.1 rmind
215 1.4.2.3 riz switch (proto) {
216 1.1 rmind case IPPROTO_TCP:
217 1.1 rmind pflag = NC_MATCH_TCP;
218 1.1 rmind if (!popts) {
219 1.1 rmind break;
220 1.1 rmind }
221 1.1 rmind assert(npfvar_get_count(popts) == 2);
222 1.1 rmind
223 1.1 rmind /* Build TCP flags block (optional). */
224 1.1 rmind uint8_t *tf, *tf_mask;
225 1.1 rmind
226 1.1 rmind tf = npfvar_get_data(popts, NPFVAR_TCPFLAG, 0);
227 1.1 rmind tf_mask = npfvar_get_data(popts, NPFVAR_TCPFLAG, 1);
228 1.1 rmind npfctl_gennc_tcpfl(nc, *tf, *tf_mask);
229 1.4.2.3 riz nop = false;
230 1.1 rmind break;
231 1.1 rmind case IPPROTO_UDP:
232 1.1 rmind pflag = NC_MATCH_UDP;
233 1.1 rmind break;
234 1.1 rmind case IPPROTO_ICMP:
235 1.1 rmind /*
236 1.1 rmind * Build ICMP block.
237 1.1 rmind */
238 1.4.2.3 riz if (!nop) {
239 1.4.2.3 riz goto invop;
240 1.4.2.3 riz }
241 1.1 rmind assert(npfvar_get_count(popts) == 2);
242 1.1 rmind
243 1.1 rmind int *icmp_type, *icmp_code;
244 1.1 rmind icmp_type = npfvar_get_data(popts, NPFVAR_ICMP, 0);
245 1.1 rmind icmp_code = npfvar_get_data(popts, NPFVAR_ICMP, 1);
246 1.1 rmind npfctl_gennc_icmp(nc, *icmp_type, *icmp_code);
247 1.4.2.3 riz nop = false;
248 1.1 rmind break;
249 1.1 rmind case -1:
250 1.1 rmind pflag = NC_MATCH_TCP | NC_MATCH_UDP;
251 1.4.2.3 riz nop = false;
252 1.1 rmind break;
253 1.1 rmind default:
254 1.4.2.3 riz /*
255 1.4.2.3 riz * No filter options are supported for other protcols.
256 1.4.2.3 riz */
257 1.4.2.3 riz if (nof && nop) {
258 1.4.2.3 riz break;
259 1.4.2.3 riz }
260 1.4.2.3 riz invop:
261 1.4.2.3 riz yyerror("invalid filter options for protocol %d", proto);
262 1.4.2.3 riz }
263 1.4.2.3 riz
264 1.4.2.3 riz /*
265 1.4.2.3 riz * Build the protocol block, unless other blocks will implicitly
266 1.4.2.3 riz * perform the family/protocol checks for us.
267 1.4.2.3 riz */
268 1.4.2.3 riz if ((family != AF_UNSPEC && nof) || (proto != -1 && nop)) {
269 1.4.2.3 riz uint8_t addrlen;
270 1.4.2.3 riz
271 1.4.2.3 riz switch (family) {
272 1.4.2.3 riz case AF_INET:
273 1.4.2.3 riz addrlen = sizeof(struct in_addr);
274 1.4.2.3 riz break;
275 1.4.2.3 riz case AF_INET6:
276 1.4.2.3 riz addrlen = sizeof(struct in6_addr);
277 1.4.2.3 riz break;
278 1.4.2.3 riz default:
279 1.4.2.3 riz addrlen = 0;
280 1.4.2.3 riz }
281 1.4.2.3 riz npfctl_gennc_proto(nc, nof ? addrlen : 0, nop ? proto : 0xff);
282 1.1 rmind }
283 1.1 rmind return pflag;
284 1.1 rmind }
285 1.1 rmind
286 1.1 rmind static bool
287 1.1 rmind npfctl_build_ncode(nl_rule_t *rl, sa_family_t family, const opt_proto_t *op,
288 1.1 rmind const filt_opts_t *fopts, bool invert)
289 1.1 rmind {
290 1.4.2.2 riz const addr_port_t *apfrom = &fopts->fo_from;
291 1.4.2.2 riz const addr_port_t *apto = &fopts->fo_to;
292 1.4.2.3 riz const int proto = op->op_proto;
293 1.4.2.3 riz bool nof, nop;
294 1.1 rmind nc_ctx_t *nc;
295 1.1 rmind void *code;
296 1.1 rmind size_t len;
297 1.1 rmind
298 1.4.2.3 riz /*
299 1.4.2.3 riz * If none specified, no n-code.
300 1.4.2.3 riz */
301 1.4.2.3 riz nof = !apfrom->ap_netaddr && !apto->ap_netaddr;
302 1.4.2.3 riz nop = !apfrom->ap_portrange && !apto->ap_portrange;
303 1.4.2.3 riz if (family == AF_UNSPEC && proto == -1 && !op->op_opts && nof && nop)
304 1.1 rmind return false;
305 1.1 rmind
306 1.1 rmind int srcflag = NC_MATCH_SRC;
307 1.1 rmind int dstflag = NC_MATCH_DST;
308 1.1 rmind
309 1.1 rmind if (invert) {
310 1.1 rmind srcflag = NC_MATCH_DST;
311 1.1 rmind dstflag = NC_MATCH_SRC;
312 1.1 rmind }
313 1.1 rmind
314 1.1 rmind nc = npfctl_ncgen_create();
315 1.1 rmind
316 1.4.2.3 riz /* Build layer 4 protocol blocks. */
317 1.4.2.3 riz int pflag = npfctl_build_proto(nc, family, op, nof, nop);
318 1.4.2.3 riz
319 1.1 rmind /* Build IP address blocks. */
320 1.4.2.2 riz npfctl_build_vars(nc, family, apfrom->ap_netaddr, srcflag);
321 1.4.2.2 riz npfctl_build_vars(nc, family, apto->ap_netaddr, dstflag);
322 1.1 rmind
323 1.1 rmind /* Build port-range blocks. */
324 1.4.2.3 riz npfctl_build_vars(nc, family, apfrom->ap_portrange, srcflag | pflag);
325 1.4.2.3 riz npfctl_build_vars(nc, family, apto->ap_portrange, dstflag | pflag);
326 1.1 rmind
327 1.1 rmind /*
328 1.1 rmind * Complete n-code (destroys the context) and pass to the rule.
329 1.1 rmind */
330 1.1 rmind code = npfctl_ncgen_complete(nc, &len);
331 1.1 rmind if (npf_debug) {
332 1.1 rmind extern int yylineno;
333 1.4.2.2 riz printf("RULE AT LINE %d\n", yylineno);
334 1.1 rmind npfctl_ncgen_print(code, len);
335 1.1 rmind }
336 1.4.2.3 riz assert(code && len > 0);
337 1.4.2.3 riz
338 1.1 rmind if (npf_rule_setcode(rl, NPF_CODE_NCODE, code, len) == -1) {
339 1.1 rmind errx(EXIT_FAILURE, "npf_rule_setcode failed");
340 1.1 rmind }
341 1.1 rmind free(code);
342 1.1 rmind return true;
343 1.1 rmind }
344 1.1 rmind
345 1.4 rmind static void
346 1.4 rmind npfctl_build_rpcall(nl_rproc_t *rp, const char *name, npfvar_t *args)
347 1.4 rmind {
348 1.4 rmind /*
349 1.4 rmind * XXX/TODO: Hardcoded for the first release. However,
350 1.4 rmind * rule procedures will become fully dynamic modules.
351 1.4 rmind */
352 1.4 rmind
353 1.4 rmind bool log = false, norm = false;
354 1.4 rmind bool rnd = false, no_df = false;
355 1.4 rmind int minttl = 0, maxmss = 0;
356 1.4 rmind
357 1.4 rmind if (strcmp(name, "log") == 0) {
358 1.4 rmind log = true;
359 1.4 rmind } else if (strcmp(name, "normalise") == 0) {
360 1.4 rmind norm = true;
361 1.4 rmind } else {
362 1.4 rmind yyerror("unknown rule procedure '%s'", name);
363 1.4 rmind }
364 1.4 rmind
365 1.4 rmind for (size_t i = 0; i < npfvar_get_count(args); i++) {
366 1.4 rmind module_arg_t *arg;
367 1.4 rmind const char *aval;
368 1.4 rmind
369 1.4 rmind arg = npfvar_get_data(args, NPFVAR_MODULE_ARG, i);
370 1.4 rmind aval = arg->ma_name;
371 1.4 rmind
372 1.4 rmind if (log) {
373 1.4 rmind u_int if_idx = npfctl_find_ifindex(aval);
374 1.4 rmind if (!if_idx) {
375 1.4 rmind yyerror("unknown interface '%s'", aval);
376 1.4 rmind }
377 1.4 rmind _npf_rproc_setlog(rp, if_idx);
378 1.4 rmind return;
379 1.4 rmind }
380 1.4 rmind
381 1.4.2.2 riz const int type = npfvar_get_type(arg->ma_opts, 0);
382 1.4 rmind if (type != -1 && type != NPFVAR_NUM) {
383 1.4 rmind yyerror("option '%s' is not numeric", aval);
384 1.4 rmind }
385 1.4 rmind unsigned long *opt;
386 1.4 rmind
387 1.4 rmind if (strcmp(aval, "random-id") == 0) {
388 1.4 rmind rnd = true;
389 1.4 rmind } else if (strcmp(aval, "min-ttl") == 0) {
390 1.4 rmind opt = npfvar_get_data(arg->ma_opts, NPFVAR_NUM, 0);
391 1.4 rmind minttl = *opt;
392 1.4 rmind } else if (strcmp(aval, "max-mss") == 0) {
393 1.4 rmind opt = npfvar_get_data(arg->ma_opts, NPFVAR_NUM, 0);
394 1.4 rmind maxmss = *opt;
395 1.4 rmind } else if (strcmp(aval, "no-df") == 0) {
396 1.4 rmind no_df = true;
397 1.4 rmind } else {
398 1.4 rmind yyerror("unknown argument '%s'", aval);
399 1.4 rmind }
400 1.4 rmind }
401 1.4 rmind assert(norm == true);
402 1.4 rmind _npf_rproc_setnorm(rp, rnd, no_df, minttl, maxmss);
403 1.4 rmind }
404 1.4 rmind
405 1.1 rmind /*
406 1.1 rmind * npfctl_build_rproc: create and insert a rule procedure.
407 1.1 rmind */
408 1.1 rmind void
409 1.4 rmind npfctl_build_rproc(const char *name, npfvar_t *procs)
410 1.1 rmind {
411 1.1 rmind nl_rproc_t *rp;
412 1.4 rmind size_t i;
413 1.1 rmind
414 1.1 rmind rp = npf_rproc_create(name);
415 1.1 rmind if (rp == NULL) {
416 1.1 rmind errx(EXIT_FAILURE, "npf_rproc_create failed");
417 1.1 rmind }
418 1.1 rmind npf_rproc_insert(npf_conf, rp);
419 1.4 rmind
420 1.4 rmind for (i = 0; i < npfvar_get_count(procs); i++) {
421 1.4 rmind proc_op_t *po = npfvar_get_data(procs, NPFVAR_PROC_OP, i);
422 1.4 rmind npfctl_build_rpcall(rp, po->po_name, po->po_opts);
423 1.4 rmind }
424 1.1 rmind }
425 1.1 rmind
426 1.1 rmind /*
427 1.1 rmind * npfctl_build_group: create a group, insert into the global ruleset
428 1.1 rmind * and update the current group pointer.
429 1.1 rmind */
430 1.1 rmind void
431 1.1 rmind npfctl_build_group(const char *name, int attr, u_int if_idx)
432 1.1 rmind {
433 1.1 rmind const int attr_di = (NPF_RULE_IN | NPF_RULE_OUT);
434 1.1 rmind nl_rule_t *rl;
435 1.1 rmind
436 1.1 rmind if (attr & NPF_RULE_DEFAULT) {
437 1.1 rmind if (defgroup_set) {
438 1.1 rmind yyerror("multiple default groups are not valid");
439 1.1 rmind }
440 1.1 rmind defgroup_set = true;
441 1.1 rmind attr |= attr_di;
442 1.1 rmind
443 1.1 rmind } else if ((attr & attr_di) == 0) {
444 1.1 rmind attr |= attr_di;
445 1.1 rmind }
446 1.1 rmind
447 1.4.2.1 riz rl = npf_rule_create(name, attr | NPF_RULE_FINAL, if_idx);
448 1.1 rmind npf_rule_insert(npf_conf, NULL, rl, NPF_PRI_NEXT);
449 1.1 rmind current_group = rl;
450 1.1 rmind }
451 1.1 rmind
452 1.1 rmind /*
453 1.1 rmind * npfctl_build_rule: create a rule, build n-code from filter options,
454 1.1 rmind * if any, and insert into the ruleset of current group.
455 1.1 rmind */
456 1.1 rmind void
457 1.1 rmind npfctl_build_rule(int attr, u_int if_idx, sa_family_t family,
458 1.1 rmind const opt_proto_t *op, const filt_opts_t *fopts, const char *rproc)
459 1.1 rmind {
460 1.1 rmind nl_rule_t *rl;
461 1.1 rmind
462 1.1 rmind rl = npf_rule_create(NULL, attr, if_idx);
463 1.1 rmind npfctl_build_ncode(rl, family, op, fopts, false);
464 1.1 rmind if (rproc && npf_rule_setproc(npf_conf, rl, rproc) != 0) {
465 1.1 rmind yyerror("rule procedure '%s' is not defined", rproc);
466 1.1 rmind }
467 1.1 rmind assert(current_group != NULL);
468 1.1 rmind npf_rule_insert(npf_conf, current_group, rl, NPF_PRI_NEXT);
469 1.1 rmind }
470 1.1 rmind
471 1.1 rmind /*
472 1.1 rmind * npfctl_build_nat: create a NAT policy of a specified type with a
473 1.1 rmind * given filter options.
474 1.1 rmind */
475 1.1 rmind void
476 1.4.2.2 riz npfctl_build_nat(int sd, int type, u_int if_idx, const addr_port_t *ap1,
477 1.4.2.2 riz const addr_port_t *ap2, const filt_opts_t *fopts)
478 1.1 rmind {
479 1.4.2.2 riz const opt_proto_t op = { .op_proto = -1, .op_opts = NULL };
480 1.4.2.3 riz fam_addr_mask_t *am1 = NULL, *am2 = NULL;
481 1.4.2.2 riz filt_opts_t imfopts;
482 1.4.2.2 riz sa_family_t family;
483 1.1 rmind nl_nat_t *nat;
484 1.1 rmind
485 1.4.2.2 riz if (sd == NPFCTL_NAT_STATIC) {
486 1.4.2.2 riz yyerror("static NAT is not yet supported");
487 1.4.2.2 riz }
488 1.4.2.2 riz assert(sd == NPFCTL_NAT_DYNAMIC);
489 1.4.2.2 riz assert(if_idx != 0);
490 1.4.2.2 riz
491 1.4.2.2 riz family = AF_INET;
492 1.1 rmind
493 1.4.2.2 riz if (type & NPF_NATIN) {
494 1.4.2.2 riz if (!ap1->ap_netaddr) {
495 1.4.2.2 riz yyerror("inbound network segment is not specified");
496 1.4.2.2 riz }
497 1.4.2.2 riz am1 = npfctl_get_singlefam(ap1->ap_netaddr);
498 1.4.2.3 riz if (am1->fam_family != family) {
499 1.4.2.2 riz yyerror("IPv6 NAT is not supported");
500 1.4.2.2 riz }
501 1.4.2.2 riz assert(am1 != NULL);
502 1.4.2.3 riz }
503 1.4.2.2 riz
504 1.4.2.2 riz if (type & NPF_NATOUT) {
505 1.4.2.2 riz if (!ap2->ap_netaddr) {
506 1.4.2.2 riz yyerror("outbound network segment is not specified");
507 1.4.2.2 riz }
508 1.4.2.2 riz am2 = npfctl_get_singlefam(ap2->ap_netaddr);
509 1.4.2.2 riz if (am2->fam_family != family) {
510 1.4.2.2 riz yyerror("IPv6 NAT is not supported");
511 1.4.2.2 riz }
512 1.4.2.2 riz assert(am2 != NULL);
513 1.4.2.3 riz }
514 1.4.2.2 riz
515 1.4.2.2 riz /*
516 1.4.2.2 riz * If filter criteria is not specified explicitly, apply implicit
517 1.4.2.2 riz * filtering according to the given network segements.
518 1.4.2.2 riz */
519 1.4.2.2 riz if (!fopts) {
520 1.4.2.2 riz memset(&imfopts, 0, sizeof(filt_opts_t));
521 1.4.2.2 riz if (type & NPF_NATOUT) {
522 1.4.2.2 riz memcpy(&imfopts.fo_from, ap1, sizeof(addr_port_t));
523 1.4.2.2 riz }
524 1.4.2.2 riz if (type & NPF_NATIN) {
525 1.4.2.2 riz memcpy(&imfopts.fo_to, ap2, sizeof(addr_port_t));
526 1.4.2.2 riz }
527 1.4.2.2 riz fopts = &imfopts;
528 1.1 rmind }
529 1.1 rmind
530 1.1 rmind switch (type) {
531 1.4.2.2 riz case NPF_NATIN:
532 1.4.2.2 riz assert(am1 != NULL);
533 1.1 rmind /*
534 1.1 rmind * Redirection: an inbound NAT with a specific port.
535 1.1 rmind */
536 1.4.2.2 riz if (!ap1->ap_portrange) {
537 1.4.2.2 riz yyerror("inbound port is not specified");
538 1.4.2.2 riz }
539 1.4.2.2 riz in_port_t port = npfctl_get_singleport(ap1->ap_portrange);
540 1.1 rmind nat = npf_nat_create(NPF_NATIN, NPF_NAT_PORTS,
541 1.4.2.2 riz if_idx, &am1->fam_addr, am1->fam_family, port);
542 1.1 rmind break;
543 1.4.2.2 riz
544 1.4.2.2 riz case (NPF_NATIN | NPF_NATOUT):
545 1.4.2.2 riz assert(am1 != NULL);
546 1.1 rmind /*
547 1.1 rmind * Bi-directional NAT: a combination of inbound NAT and
548 1.1 rmind * outbound NAT policies. Note that the translation address
549 1.1 rmind * is local IP and filter criteria is inverted accordingly.
550 1.1 rmind */
551 1.1 rmind nat = npf_nat_create(NPF_NATIN, 0, if_idx,
552 1.4.2.2 riz &am1->fam_addr, am1->fam_family, 0);
553 1.4.2.2 riz npfctl_build_ncode(nat, family, &op, fopts, true);
554 1.1 rmind npf_nat_insert(npf_conf, nat, NPF_PRI_NEXT);
555 1.1 rmind /* FALLTHROUGH */
556 1.4.2.2 riz
557 1.4.2.2 riz case NPF_NATOUT:
558 1.4.2.2 riz assert(am2 != NULL);
559 1.1 rmind /*
560 1.1 rmind * Traditional NAPT: an outbound NAT policy with port.
561 1.4.2.2 riz * If this is another half for bi-directional NAT, then
562 1.1 rmind * no port translation with mapping.
563 1.1 rmind */
564 1.4.2.2 riz nat = npf_nat_create(NPF_NATOUT, type == NPF_NATOUT ?
565 1.1 rmind (NPF_NAT_PORTS | NPF_NAT_PORTMAP) : 0,
566 1.4.2.2 riz if_idx, &am2->fam_addr, am2->fam_family, 0);
567 1.1 rmind break;
568 1.4.2.2 riz
569 1.1 rmind default:
570 1.1 rmind assert(false);
571 1.1 rmind }
572 1.4.2.2 riz npfctl_build_ncode(nat, family, &op, fopts, false);
573 1.1 rmind npf_nat_insert(npf_conf, nat, NPF_PRI_NEXT);
574 1.1 rmind }
575 1.1 rmind
576 1.1 rmind /*
577 1.1 rmind * npfctl_fill_table: fill NPF table with entries from a specified file.
578 1.1 rmind */
579 1.1 rmind static void
580 1.4.2.4 riz npfctl_fill_table(nl_table_t *tl, u_int type, const char *fname)
581 1.1 rmind {
582 1.1 rmind char *buf = NULL;
583 1.1 rmind int l = 0;
584 1.1 rmind FILE *fp;
585 1.1 rmind size_t n;
586 1.1 rmind
587 1.1 rmind fp = fopen(fname, "r");
588 1.1 rmind if (fp == NULL) {
589 1.1 rmind err(EXIT_FAILURE, "open '%s'", fname);
590 1.1 rmind }
591 1.1 rmind while (l++, getline(&buf, &n, fp) != -1) {
592 1.4.2.4 riz fam_addr_mask_t fam;
593 1.4.2.4 riz int alen;
594 1.1 rmind
595 1.1 rmind if (*buf == '\n' || *buf == '#') {
596 1.1 rmind continue;
597 1.1 rmind }
598 1.4.2.4 riz
599 1.4.2.4 riz if (!npfctl_parse_cidr(buf, &fam, &alen)) {
600 1.4.2.4 riz errx(EXIT_FAILURE,
601 1.4.2.4 riz "%s:%d: invalid table entry", fname, l);
602 1.4.2.4 riz }
603 1.4.2.4 riz if (type == NPF_TABLE_HASH && fam.fam_mask != NPF_NO_NETMASK) {
604 1.4.2.4 riz errx(EXIT_FAILURE,
605 1.4.2.4 riz "%s:%d: mask used with the hash table", fname, l);
606 1.1 rmind }
607 1.1 rmind
608 1.1 rmind /* Create and add a table entry. */
609 1.4.2.4 riz npf_table_add_entry(tl, alen, &fam.fam_addr, fam.fam_mask);
610 1.1 rmind }
611 1.1 rmind if (buf != NULL) {
612 1.1 rmind free(buf);
613 1.1 rmind }
614 1.1 rmind }
615 1.1 rmind
616 1.1 rmind /*
617 1.1 rmind * npfctl_build_table: create an NPF table, add to the configuration and,
618 1.1 rmind * if required, fill with contents from a file.
619 1.1 rmind */
620 1.1 rmind void
621 1.1 rmind npfctl_build_table(const char *tid, u_int type, const char *fname)
622 1.1 rmind {
623 1.1 rmind nl_table_t *tl;
624 1.1 rmind u_int id;
625 1.1 rmind
626 1.1 rmind id = atoi(tid);
627 1.1 rmind tl = npf_table_create(id, type);
628 1.1 rmind assert(tl != NULL);
629 1.1 rmind
630 1.1 rmind if (npf_table_insert(npf_conf, tl)) {
631 1.1 rmind errx(EXIT_FAILURE, "table '%d' is already defined\n", id);
632 1.1 rmind }
633 1.1 rmind
634 1.1 rmind if (fname) {
635 1.4.2.4 riz npfctl_fill_table(tl, type, fname);
636 1.1 rmind }
637 1.1 rmind }
638