npf_build.c revision 1.56 1 1.1 rmind /*-
2 1.55 rmind * Copyright (c) 2011-2020 The NetBSD Foundation, Inc.
3 1.1 rmind * All rights reserved.
4 1.1 rmind *
5 1.1 rmind * This material is based upon work partially supported by The
6 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
7 1.1 rmind *
8 1.1 rmind * Redistribution and use in source and binary forms, with or without
9 1.1 rmind * modification, are permitted provided that the following conditions
10 1.1 rmind * are met:
11 1.1 rmind * 1. Redistributions of source code must retain the above copyright
12 1.1 rmind * notice, this list of conditions and the following disclaimer.
13 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer in the
15 1.1 rmind * documentation and/or other materials provided with the distribution.
16 1.1 rmind *
17 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
28 1.1 rmind */
29 1.1 rmind
30 1.1 rmind /*
31 1.1 rmind * npfctl(8) building of the configuration.
32 1.1 rmind */
33 1.1 rmind
34 1.1 rmind #include <sys/cdefs.h>
35 1.56 tnn __RCSID("$NetBSD: npf_build.c,v 1.56 2023/08/18 14:26:50 tnn Exp $");
36 1.1 rmind
37 1.1 rmind #include <sys/types.h>
38 1.41 christos #define __FAVOR_BSD
39 1.37 rmind #include <netinet/tcp.h>
40 1.1 rmind
41 1.1 rmind #include <stdlib.h>
42 1.1 rmind #include <inttypes.h>
43 1.1 rmind #include <string.h>
44 1.29 rmind #include <ctype.h>
45 1.33 rmind #include <unistd.h>
46 1.41 christos #include <fcntl.h>
47 1.14 rmind #include <errno.h>
48 1.1 rmind #include <err.h>
49 1.1 rmind
50 1.25 rmind #include <pcap/pcap.h>
51 1.25 rmind
52 1.1 rmind #include "npfctl.h"
53 1.1 rmind
54 1.18 rmind #define MAX_RULE_NESTING 16
55 1.18 rmind
56 1.1 rmind static nl_config_t * npf_conf = NULL;
57 1.1 rmind static bool npf_debug = false;
58 1.18 rmind static nl_rule_t * the_rule = NULL;
59 1.54 rmind static bool npf_conf_built = false;
60 1.18 rmind
61 1.54 rmind static nl_rule_t * defgroup = NULL;
62 1.18 rmind static nl_rule_t * current_group[MAX_RULE_NESTING];
63 1.18 rmind static unsigned rule_nesting_level = 0;
64 1.43 rmind static unsigned npfctl_tid_counter = 0;
65 1.1 rmind
66 1.27 rmind static void npfctl_dump_bpf(struct bpf_program *);
67 1.27 rmind
68 1.1 rmind void
69 1.1 rmind npfctl_config_init(bool debug)
70 1.1 rmind {
71 1.1 rmind npf_conf = npf_config_create();
72 1.1 rmind if (npf_conf == NULL) {
73 1.55 rmind errx(EXIT_FAILURE, "npf_config_create() failed");
74 1.1 rmind }
75 1.54 rmind memset(current_group, 0, sizeof(current_group));
76 1.1 rmind npf_debug = debug;
77 1.54 rmind npf_conf_built = false;
78 1.54 rmind }
79 1.54 rmind
80 1.55 rmind nl_config_t *
81 1.55 rmind npfctl_config_ref(void)
82 1.55 rmind {
83 1.55 rmind return npf_conf;
84 1.55 rmind }
85 1.55 rmind
86 1.55 rmind nl_rule_t *
87 1.55 rmind npfctl_rule_ref(void)
88 1.55 rmind {
89 1.55 rmind return the_rule;
90 1.55 rmind }
91 1.55 rmind
92 1.54 rmind void
93 1.54 rmind npfctl_config_build(void)
94 1.54 rmind {
95 1.54 rmind /* Run-once. */
96 1.54 rmind if (npf_conf_built) {
97 1.54 rmind return;
98 1.54 rmind }
99 1.54 rmind
100 1.54 rmind /*
101 1.54 rmind * The default group is mandatory. Note: npfctl_build_group_end()
102 1.54 rmind * skipped the default rule, since it must be the last one.
103 1.54 rmind */
104 1.54 rmind if (!defgroup) {
105 1.54 rmind errx(EXIT_FAILURE, "default group was not defined");
106 1.54 rmind }
107 1.54 rmind assert(rule_nesting_level == 0);
108 1.54 rmind npf_rule_insert(npf_conf, NULL, defgroup);
109 1.54 rmind
110 1.54 rmind npf_config_build(npf_conf);
111 1.54 rmind npf_conf_built = true;
112 1.1 rmind }
113 1.1 rmind
114 1.1 rmind int
115 1.46 rmind npfctl_config_send(int fd)
116 1.1 rmind {
117 1.41 christos npf_error_t errinfo;
118 1.41 christos int error = 0;
119 1.1 rmind
120 1.54 rmind npfctl_config_build();
121 1.46 rmind error = npf_config_submit(npf_conf, fd, &errinfo);
122 1.3 rmind if (error) {
123 1.41 christos npfctl_print_error(&errinfo);
124 1.3 rmind }
125 1.41 christos npf_config_destroy(npf_conf);
126 1.41 christos return error;
127 1.41 christos }
128 1.41 christos
129 1.41 christos void
130 1.41 christos npfctl_config_save(nl_config_t *ncf, const char *outfile)
131 1.41 christos {
132 1.41 christos void *blob;
133 1.41 christos size_t len;
134 1.41 christos int fd;
135 1.41 christos
136 1.41 christos blob = npf_config_export(ncf, &len);
137 1.55 rmind if (!blob) {
138 1.41 christos err(EXIT_FAILURE, "npf_config_export");
139 1.55 rmind }
140 1.55 rmind if ((fd = open(outfile, O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) {
141 1.41 christos err(EXIT_FAILURE, "could not open %s", outfile);
142 1.55 rmind }
143 1.41 christos if (write(fd, blob, len) != (ssize_t)len) {
144 1.41 christos err(EXIT_FAILURE, "write to %s failed", outfile);
145 1.25 rmind }
146 1.41 christos free(blob);
147 1.41 christos close(fd);
148 1.1 rmind }
149 1.1 rmind
150 1.28 rmind bool
151 1.13 rmind npfctl_debug_addif(const char *ifname)
152 1.13 rmind {
153 1.28 rmind const char tname[] = "npftest";
154 1.13 rmind const size_t tnamelen = sizeof(tname) - 1;
155 1.13 rmind
156 1.28 rmind if (npf_debug) {
157 1.28 rmind _npf_debug_addif(npf_conf, ifname);
158 1.28 rmind return strncmp(ifname, tname, tnamelen) == 0;
159 1.13 rmind }
160 1.28 rmind return 0;
161 1.13 rmind }
162 1.13 rmind
163 1.52 rmind nl_table_t *
164 1.52 rmind npfctl_table_getbyname(nl_config_t *ncf, const char *name)
165 1.1 rmind {
166 1.49 rmind nl_iter_t i = NPF_ITER_BEGIN;
167 1.32 rmind nl_table_t *tl;
168 1.32 rmind
169 1.32 rmind /* XXX dynamic ruleset */
170 1.52 rmind if (!ncf) {
171 1.52 rmind return NULL;
172 1.32 rmind }
173 1.52 rmind while ((tl = npf_table_iterate(ncf, &i)) != NULL) {
174 1.32 rmind const char *tname = npf_table_getname(tl);
175 1.32 rmind if (strcmp(tname, name) == 0) {
176 1.49 rmind break;
177 1.32 rmind }
178 1.32 rmind }
179 1.52 rmind return tl;
180 1.52 rmind }
181 1.52 rmind
182 1.52 rmind unsigned
183 1.52 rmind npfctl_table_getid(const char *name)
184 1.52 rmind {
185 1.52 rmind nl_table_t *tl;
186 1.52 rmind
187 1.52 rmind tl = npfctl_table_getbyname(npf_conf, name);
188 1.52 rmind return tl ? npf_table_getid(tl) : (unsigned)-1;
189 1.1 rmind }
190 1.1 rmind
191 1.47 rmind const char *
192 1.47 rmind npfctl_table_getname(nl_config_t *ncf, unsigned tid, bool *ifaddr)
193 1.47 rmind {
194 1.47 rmind const char *name = NULL;
195 1.49 rmind nl_iter_t i = NPF_ITER_BEGIN;
196 1.47 rmind nl_table_t *tl;
197 1.47 rmind
198 1.49 rmind while ((tl = npf_table_iterate(ncf, &i)) != NULL) {
199 1.47 rmind if (npf_table_getid(tl) == tid) {
200 1.47 rmind name = npf_table_getname(tl);
201 1.49 rmind break;
202 1.47 rmind }
203 1.47 rmind }
204 1.47 rmind if (!name) {
205 1.47 rmind return NULL;
206 1.47 rmind }
207 1.47 rmind if (!strncmp(name, NPF_IFNET_TABLE_PREF, NPF_IFNET_TABLE_PREFLEN)) {
208 1.47 rmind name += NPF_IFNET_TABLE_PREFLEN;
209 1.47 rmind *ifaddr = true;
210 1.47 rmind } else {
211 1.47 rmind *ifaddr = false;
212 1.47 rmind }
213 1.47 rmind return name;
214 1.47 rmind }
215 1.47 rmind
216 1.7 rmind static in_port_t
217 1.1 rmind npfctl_get_singleport(const npfvar_t *vp)
218 1.1 rmind {
219 1.1 rmind port_range_t *pr;
220 1.7 rmind in_port_t *port;
221 1.1 rmind
222 1.1 rmind if (npfvar_get_count(vp) > 1) {
223 1.1 rmind yyerror("multiple ports are not valid");
224 1.1 rmind }
225 1.1 rmind pr = npfvar_get_data(vp, NPFVAR_PORT_RANGE, 0);
226 1.1 rmind if (pr->pr_start != pr->pr_end) {
227 1.1 rmind yyerror("port range is not valid");
228 1.1 rmind }
229 1.7 rmind port = &pr->pr_start;
230 1.7 rmind return *port;
231 1.1 rmind }
232 1.1 rmind
233 1.1 rmind static fam_addr_mask_t *
234 1.1 rmind npfctl_get_singlefam(const npfvar_t *vp)
235 1.1 rmind {
236 1.47 rmind fam_addr_mask_t *am;
237 1.47 rmind
238 1.47 rmind if (npfvar_get_type(vp, 0) != NPFVAR_FAM) {
239 1.47 rmind yyerror("map segment must be an address or network");
240 1.47 rmind }
241 1.47 rmind if (npfvar_get_count(vp) > 1) {
242 1.47 rmind yyerror("map segment cannot have multiple static addresses");
243 1.47 rmind }
244 1.47 rmind am = npfvar_get_data(vp, NPFVAR_FAM, 0);
245 1.47 rmind if (am == NULL) {
246 1.47 rmind yyerror("invalid map segment");
247 1.47 rmind }
248 1.47 rmind return am;
249 1.47 rmind }
250 1.47 rmind
251 1.47 rmind static unsigned
252 1.47 rmind npfctl_get_singletable(const npfvar_t *vp)
253 1.47 rmind {
254 1.47 rmind unsigned *tid;
255 1.47 rmind
256 1.1 rmind if (npfvar_get_count(vp) > 1) {
257 1.55 rmind yyerror("invalid use of multiple tables");
258 1.1 rmind }
259 1.47 rmind tid = npfvar_get_data(vp, NPFVAR_TABLE, 0);
260 1.47 rmind assert(tid != NULL);
261 1.47 rmind return *tid;
262 1.1 rmind }
263 1.1 rmind
264 1.10 rmind static bool
265 1.25 rmind npfctl_build_fam(npf_bpf_t *ctx, sa_family_t family,
266 1.55 rmind fam_addr_mask_t *fam, unsigned opts)
267 1.1 rmind {
268 1.1 rmind /*
269 1.1 rmind * If family is specified, address does not match it and the
270 1.1 rmind * address is extracted from the interface, then simply ignore.
271 1.1 rmind * Otherwise, address of invalid family was passed manually.
272 1.1 rmind */
273 1.1 rmind if (family != AF_UNSPEC && family != fam->fam_family) {
274 1.15 rmind if (!fam->fam_ifindex) {
275 1.1 rmind yyerror("specified address is not of the required "
276 1.1 rmind "family %d", family);
277 1.1 rmind }
278 1.10 rmind return false;
279 1.1 rmind }
280 1.30 rmind
281 1.25 rmind family = fam->fam_family;
282 1.30 rmind if (family != AF_INET && family != AF_INET6) {
283 1.30 rmind yyerror("family %d is not supported", family);
284 1.30 rmind }
285 1.1 rmind
286 1.1 rmind /*
287 1.1 rmind * Optimise 0.0.0.0/0 case to be NOP. Otherwise, address with
288 1.1 rmind * zero mask would never match and therefore is not valid.
289 1.1 rmind */
290 1.1 rmind if (fam->fam_mask == 0) {
291 1.55 rmind if (!npfctl_addr_iszero(&fam->fam_addr)) {
292 1.1 rmind yyerror("filter criterion would never match");
293 1.1 rmind }
294 1.10 rmind return false;
295 1.1 rmind }
296 1.1 rmind
297 1.25 rmind npfctl_bpf_cidr(ctx, opts, family, &fam->fam_addr, fam->fam_mask);
298 1.10 rmind return true;
299 1.1 rmind }
300 1.1 rmind
301 1.1 rmind static void
302 1.25 rmind npfctl_build_vars(npf_bpf_t *ctx, sa_family_t family, npfvar_t *vars, int opts)
303 1.1 rmind {
304 1.55 rmind npfctl_bpf_group_enter(ctx, (opts & MATCH_INVERT) != 0);
305 1.55 rmind for (unsigned i = 0; i < npfvar_get_count(vars); i++) {
306 1.55 rmind const unsigned type = npfvar_get_type(vars, i);
307 1.55 rmind void *data = npfvar_get_data(vars, type, i);
308 1.1 rmind
309 1.1 rmind assert(data != NULL);
310 1.1 rmind
311 1.1 rmind switch (type) {
312 1.1 rmind case NPFVAR_FAM: {
313 1.1 rmind fam_addr_mask_t *fam = data;
314 1.25 rmind npfctl_build_fam(ctx, family, fam, opts);
315 1.1 rmind break;
316 1.1 rmind }
317 1.1 rmind case NPFVAR_PORT_RANGE: {
318 1.1 rmind port_range_t *pr = data;
319 1.25 rmind npfctl_bpf_ports(ctx, opts, pr->pr_start, pr->pr_end);
320 1.1 rmind break;
321 1.1 rmind }
322 1.1 rmind case NPFVAR_TABLE: {
323 1.55 rmind unsigned tid;
324 1.55 rmind memcpy(&tid, data, sizeof(unsigned));
325 1.25 rmind npfctl_bpf_table(ctx, opts, tid);
326 1.1 rmind break;
327 1.1 rmind }
328 1.1 rmind default:
329 1.55 rmind yyerror("unexpected %s", npfvar_type(type));
330 1.1 rmind }
331 1.1 rmind }
332 1.55 rmind npfctl_bpf_group_exit(ctx);
333 1.1 rmind }
334 1.1 rmind
335 1.25 rmind static void
336 1.55 rmind npfctl_build_proto_block(npf_bpf_t *ctx, const opt_proto_t *op, bool multiple)
337 1.1 rmind {
338 1.55 rmind const unsigned proto = op->op_proto;
339 1.55 rmind npfvar_t *popts = op->op_opts;
340 1.25 rmind
341 1.55 rmind if (multiple && popts) {
342 1.55 rmind yyerror("multiple protocol options with protocol filters "
343 1.55 rmind "are not yet supported");
344 1.25 rmind }
345 1.1 rmind
346 1.55 rmind /* Build the protocol filter. */
347 1.55 rmind npfctl_bpf_proto(ctx, proto);
348 1.55 rmind
349 1.10 rmind switch (proto) {
350 1.1 rmind case IPPROTO_TCP:
351 1.25 rmind /* Build TCP flags matching (optional). */
352 1.25 rmind if (popts) {
353 1.25 rmind uint8_t *tf, *tf_mask;
354 1.25 rmind
355 1.25 rmind assert(npfvar_get_count(popts) == 2);
356 1.25 rmind tf = npfvar_get_data(popts, NPFVAR_TCPFLAG, 0);
357 1.25 rmind tf_mask = npfvar_get_data(popts, NPFVAR_TCPFLAG, 1);
358 1.55 rmind npfctl_bpf_tcpfl(ctx, *tf, *tf_mask);
359 1.1 rmind }
360 1.1 rmind break;
361 1.1 rmind case IPPROTO_ICMP:
362 1.12 spz case IPPROTO_ICMPV6:
363 1.25 rmind /* Build ICMP/ICMPv6 type and/or code matching. */
364 1.25 rmind if (popts) {
365 1.25 rmind int *icmp_type, *icmp_code;
366 1.25 rmind
367 1.25 rmind assert(npfvar_get_count(popts) == 2);
368 1.25 rmind icmp_type = npfvar_get_data(popts, NPFVAR_ICMP, 0);
369 1.25 rmind icmp_code = npfvar_get_data(popts, NPFVAR_ICMP, 1);
370 1.25 rmind npfctl_bpf_icmp(ctx, *icmp_type, *icmp_code);
371 1.12 spz }
372 1.12 spz break;
373 1.25 rmind default:
374 1.25 rmind /* No options for other protocols. */
375 1.1 rmind break;
376 1.10 rmind }
377 1.1 rmind }
378 1.1 rmind
379 1.55 rmind static void
380 1.55 rmind npfctl_build_proto(npf_bpf_t *ctx, const npfvar_t *vars)
381 1.55 rmind {
382 1.55 rmind const unsigned count = npfvar_get_count(vars);
383 1.55 rmind
384 1.55 rmind /*
385 1.55 rmind * XXX: For now, just do not support multiple protocol
386 1.55 rmind * blocks with options; this is because npfctl_bpf_tcpfl()
387 1.55 rmind * and npfctl_bpf_icmp() will not work correctly in a group.
388 1.55 rmind */
389 1.55 rmind if (count == 1) {
390 1.55 rmind const opt_proto_t *op = npfvar_get_data(vars, NPFVAR_PROTO, 0);
391 1.55 rmind npfctl_build_proto_block(ctx, op, false);
392 1.55 rmind return;
393 1.55 rmind }
394 1.55 rmind
395 1.55 rmind npfctl_bpf_group_enter(ctx, false);
396 1.55 rmind for (unsigned i = 0; i < count; i++) {
397 1.55 rmind const opt_proto_t *op = npfvar_get_data(vars, NPFVAR_PROTO, i);
398 1.55 rmind npfctl_build_proto_block(ctx, op, true);
399 1.55 rmind }
400 1.55 rmind npfctl_bpf_group_exit(ctx);
401 1.55 rmind }
402 1.55 rmind
403 1.55 rmind static bool
404 1.55 rmind npfctl_check_proto(const npfvar_t *vars, bool *non_tcpudp, bool *tcp_with_nofl)
405 1.55 rmind {
406 1.55 rmind unsigned count;
407 1.55 rmind
408 1.55 rmind *non_tcpudp = false;
409 1.55 rmind *tcp_with_nofl = false;
410 1.55 rmind
411 1.55 rmind if (vars == NULL) {
412 1.55 rmind return false;
413 1.55 rmind }
414 1.55 rmind
415 1.55 rmind count = npfvar_get_count(vars);
416 1.55 rmind for (unsigned i = 0; i < count; i++) {
417 1.55 rmind const opt_proto_t *op = npfvar_get_data(vars, NPFVAR_PROTO, i);
418 1.55 rmind
419 1.55 rmind switch (op->op_proto) {
420 1.55 rmind case IPPROTO_TCP:
421 1.55 rmind *tcp_with_nofl = op->op_opts == NULL;
422 1.55 rmind break;
423 1.55 rmind case IPPROTO_UDP:
424 1.55 rmind case -1:
425 1.55 rmind break;
426 1.55 rmind default:
427 1.55 rmind *non_tcpudp = true;
428 1.55 rmind break;
429 1.55 rmind }
430 1.55 rmind }
431 1.55 rmind return count != 0;
432 1.55 rmind }
433 1.55 rmind
434 1.1 rmind static bool
435 1.55 rmind npfctl_build_code(nl_rule_t *rl, sa_family_t family, const npfvar_t *popts,
436 1.27 rmind const filt_opts_t *fopts)
437 1.1 rmind {
438 1.7 rmind const addr_port_t *apfrom = &fopts->fo_from;
439 1.7 rmind const addr_port_t *apto = &fopts->fo_to;
440 1.55 rmind bool any_proto, any_addrs, any_ports, stateful;
441 1.55 rmind bool any_l4proto, non_tcpudp, tcp_with_nofl;
442 1.25 rmind npf_bpf_t *bc;
443 1.42 rmind unsigned opts;
444 1.1 rmind size_t len;
445 1.1 rmind
446 1.55 rmind /*
447 1.55 rmind * Gather some information about the protocol options, if any.
448 1.55 rmind * Check the filter criteria in general -- if none specified,
449 1.55 rmind * then no byte-code.
450 1.55 rmind */
451 1.55 rmind any_l4proto = npfctl_check_proto(popts, &non_tcpudp, &tcp_with_nofl);
452 1.55 rmind any_proto = (family != AF_UNSPEC) || any_l4proto;
453 1.55 rmind any_addrs = apfrom->ap_netaddr || apto->ap_netaddr;
454 1.55 rmind any_ports = apfrom->ap_portrange || apto->ap_portrange;
455 1.55 rmind stateful = (npf_rule_getattr(rl) & NPF_RULE_STATEFUL) != 0;
456 1.55 rmind if (!any_proto && !any_addrs && !any_ports && !stateful) {
457 1.1 rmind return false;
458 1.25 rmind }
459 1.1 rmind
460 1.25 rmind /*
461 1.25 rmind * Sanity check: ports can only be used with TCP or UDP protocol.
462 1.25 rmind */
463 1.55 rmind if (any_ports && non_tcpudp) {
464 1.55 rmind yyerror("invalid filter options for given the protocol(s)");
465 1.25 rmind }
466 1.1 rmind
467 1.25 rmind bc = npfctl_bpf_create();
468 1.1 rmind
469 1.55 rmind /* Build layer 3 and 4 protocol blocks. */
470 1.55 rmind if (family != AF_UNSPEC) {
471 1.55 rmind npfctl_bpf_ipver(bc, family);
472 1.55 rmind }
473 1.55 rmind if (any_l4proto) {
474 1.55 rmind npfctl_build_proto(bc, popts);
475 1.55 rmind }
476 1.10 rmind
477 1.37 rmind /*
478 1.37 rmind * If this is a stateful rule and TCP flags are not specified,
479 1.37 rmind * then add "flags S/SAFR" filter for TCP protocol case.
480 1.37 rmind */
481 1.55 rmind if (stateful && (!any_l4proto || tcp_with_nofl)) {
482 1.55 rmind npfctl_bpf_tcpfl(bc, TH_SYN, TH_SYN | TH_ACK | TH_FIN | TH_RST);
483 1.37 rmind }
484 1.37 rmind
485 1.1 rmind /* Build IP address blocks. */
486 1.42 rmind opts = MATCH_SRC | (fopts->fo_finvert ? MATCH_INVERT : 0);
487 1.42 rmind npfctl_build_vars(bc, family, apfrom->ap_netaddr, opts);
488 1.42 rmind opts = MATCH_DST | (fopts->fo_tinvert ? MATCH_INVERT : 0);
489 1.42 rmind npfctl_build_vars(bc, family, apto->ap_netaddr, opts);
490 1.1 rmind
491 1.55 rmind /*
492 1.55 rmind * Build the port-range blocks. If no protocol is specified,
493 1.55 rmind * then we implicitly filter for the TCP / UDP protocols.
494 1.55 rmind */
495 1.55 rmind if (any_ports && !any_l4proto) {
496 1.55 rmind npfctl_bpf_group_enter(bc, false);
497 1.55 rmind npfctl_bpf_proto(bc, IPPROTO_TCP);
498 1.55 rmind npfctl_bpf_proto(bc, IPPROTO_UDP);
499 1.55 rmind npfctl_bpf_group_exit(bc);
500 1.38 rmind }
501 1.27 rmind npfctl_build_vars(bc, family, apfrom->ap_portrange, MATCH_SRC);
502 1.27 rmind npfctl_build_vars(bc, family, apto->ap_portrange, MATCH_DST);
503 1.25 rmind
504 1.25 rmind /* Set the byte-code marks, if any. */
505 1.25 rmind const void *bmarks = npfctl_bpf_bmarks(bc, &len);
506 1.55 rmind if (bmarks && npf_rule_setinfo(rl, bmarks, len) != 0) {
507 1.55 rmind errx(EXIT_FAILURE, "npf_rule_setinfo");
508 1.25 rmind }
509 1.1 rmind
510 1.25 rmind /* Complete BPF byte-code and pass to the rule. */
511 1.25 rmind struct bpf_program *bf = npfctl_bpf_complete(bc);
512 1.40 rmind if (bf == NULL) {
513 1.40 rmind npfctl_bpf_destroy(bc);
514 1.40 rmind return true;
515 1.40 rmind }
516 1.25 rmind len = bf->bf_len * sizeof(struct bpf_insn);
517 1.10 rmind
518 1.49 rmind if (npf_rule_setcode(rl, NPF_CODE_BPF, bf->bf_insns, len) != 0) {
519 1.55 rmind errx(EXIT_FAILURE, "npf_rule_setcode");
520 1.1 rmind }
521 1.27 rmind npfctl_dump_bpf(bf);
522 1.25 rmind npfctl_bpf_destroy(bc);
523 1.25 rmind
524 1.1 rmind return true;
525 1.1 rmind }
526 1.1 rmind
527 1.4 rmind static void
528 1.27 rmind npfctl_build_pcap(nl_rule_t *rl, const char *filter)
529 1.27 rmind {
530 1.27 rmind const size_t maxsnaplen = 64 * 1024;
531 1.27 rmind struct bpf_program bf;
532 1.27 rmind size_t len;
533 1.56 tnn pcap_t *pd;
534 1.27 rmind
535 1.56 tnn pd = pcap_open_dead(DLT_RAW, maxsnaplen);
536 1.56 tnn if (pd == NULL) {
537 1.56 tnn err(EXIT_FAILURE, "pcap_open_dead");
538 1.56 tnn }
539 1.56 tnn
540 1.56 tnn if (pcap_compile(pd, &bf,
541 1.27 rmind filter, 1, PCAP_NETMASK_UNKNOWN) == -1) {
542 1.27 rmind yyerror("invalid pcap-filter(7) syntax");
543 1.27 rmind }
544 1.27 rmind len = bf.bf_len * sizeof(struct bpf_insn);
545 1.27 rmind
546 1.49 rmind if (npf_rule_setcode(rl, NPF_CODE_BPF, bf.bf_insns, len) != 0) {
547 1.27 rmind errx(EXIT_FAILURE, "npf_rule_setcode failed");
548 1.27 rmind }
549 1.27 rmind npfctl_dump_bpf(&bf);
550 1.27 rmind pcap_freecode(&bf);
551 1.56 tnn pcap_close(pd);
552 1.27 rmind }
553 1.27 rmind
554 1.27 rmind static void
555 1.4 rmind npfctl_build_rpcall(nl_rproc_t *rp, const char *name, npfvar_t *args)
556 1.4 rmind {
557 1.14 rmind npf_extmod_t *extmod;
558 1.14 rmind nl_ext_t *extcall;
559 1.14 rmind int error;
560 1.4 rmind
561 1.14 rmind extmod = npf_extmod_get(name, &extcall);
562 1.14 rmind if (extmod == NULL) {
563 1.4 rmind yyerror("unknown rule procedure '%s'", name);
564 1.4 rmind }
565 1.4 rmind
566 1.4 rmind for (size_t i = 0; i < npfvar_get_count(args); i++) {
567 1.14 rmind const char *param, *value;
568 1.14 rmind proc_param_t *p;
569 1.4 rmind
570 1.14 rmind p = npfvar_get_data(args, NPFVAR_PROC_PARAM, i);
571 1.14 rmind param = p->pp_param;
572 1.14 rmind value = p->pp_value;
573 1.14 rmind
574 1.14 rmind error = npf_extmod_param(extmod, extcall, param, value);
575 1.14 rmind switch (error) {
576 1.14 rmind case EINVAL:
577 1.14 rmind yyerror("invalid parameter '%s'", param);
578 1.14 rmind default:
579 1.14 rmind break;
580 1.4 rmind }
581 1.4 rmind }
582 1.14 rmind error = npf_rproc_extcall(rp, extcall);
583 1.14 rmind if (error) {
584 1.14 rmind yyerror(error == EEXIST ?
585 1.14 rmind "duplicate procedure call" : "unexpected error");
586 1.14 rmind }
587 1.4 rmind }
588 1.4 rmind
589 1.1 rmind /*
590 1.1 rmind * npfctl_build_rproc: create and insert a rule procedure.
591 1.1 rmind */
592 1.1 rmind void
593 1.4 rmind npfctl_build_rproc(const char *name, npfvar_t *procs)
594 1.1 rmind {
595 1.1 rmind nl_rproc_t *rp;
596 1.4 rmind size_t i;
597 1.1 rmind
598 1.1 rmind rp = npf_rproc_create(name);
599 1.1 rmind if (rp == NULL) {
600 1.23 christos errx(EXIT_FAILURE, "%s failed", __func__);
601 1.1 rmind }
602 1.4 rmind
603 1.4 rmind for (i = 0; i < npfvar_get_count(procs); i++) {
604 1.14 rmind proc_call_t *pc = npfvar_get_data(procs, NPFVAR_PROC, i);
605 1.14 rmind npfctl_build_rpcall(rp, pc->pc_name, pc->pc_opts);
606 1.4 rmind }
607 1.46 rmind npf_rproc_insert(npf_conf, rp);
608 1.1 rmind }
609 1.1 rmind
610 1.53 rmind /*
611 1.53 rmind * npfctl_build_maprset: create and insert a NAT ruleset.
612 1.53 rmind */
613 1.22 rmind void
614 1.28 rmind npfctl_build_maprset(const char *name, int attr, const char *ifname)
615 1.22 rmind {
616 1.22 rmind const int attr_di = (NPF_RULE_IN | NPF_RULE_OUT);
617 1.22 rmind nl_rule_t *rl;
618 1.53 rmind bool natset;
619 1.53 rmind int err;
620 1.53 rmind
621 1.53 rmind /* Validate the prefix. */
622 1.53 rmind err = npfctl_nat_ruleset_p(name, &natset);
623 1.53 rmind if (!natset) {
624 1.53 rmind yyerror("NAT ruleset names must be prefixed with `"
625 1.53 rmind NPF_RULESET_MAP_PREF "`");
626 1.53 rmind }
627 1.53 rmind if (err) {
628 1.53 rmind yyerror("NAT ruleset is missing a name (only prefix found)");
629 1.53 rmind }
630 1.22 rmind
631 1.22 rmind /* If no direction is not specified, then both. */
632 1.22 rmind if ((attr & attr_di) == 0) {
633 1.22 rmind attr |= attr_di;
634 1.22 rmind }
635 1.53 rmind
636 1.22 rmind /* Allow only "in/out" attributes. */
637 1.45 rmind attr = NPF_RULE_GROUP | NPF_RULE_DYNAMIC | (attr & attr_di);
638 1.28 rmind rl = npf_rule_create(name, attr, ifname);
639 1.49 rmind npf_rule_setprio(rl, NPF_PRI_LAST);
640 1.49 rmind npf_nat_insert(npf_conf, rl);
641 1.22 rmind }
642 1.22 rmind
643 1.1 rmind /*
644 1.46 rmind * npfctl_build_group: create a group, update the current group pointer
645 1.46 rmind * and increase the nesting level.
646 1.1 rmind */
647 1.1 rmind void
648 1.28 rmind npfctl_build_group(const char *name, int attr, const char *ifname, bool def)
649 1.1 rmind {
650 1.1 rmind const int attr_di = (NPF_RULE_IN | NPF_RULE_OUT);
651 1.1 rmind nl_rule_t *rl;
652 1.1 rmind
653 1.18 rmind if (def || (attr & attr_di) == 0) {
654 1.18 rmind attr |= attr_di;
655 1.18 rmind }
656 1.18 rmind
657 1.28 rmind rl = npf_rule_create(name, attr | NPF_RULE_GROUP, ifname);
658 1.18 rmind npf_rule_setprio(rl, NPF_PRI_LAST);
659 1.18 rmind if (def) {
660 1.18 rmind if (defgroup) {
661 1.1 rmind yyerror("multiple default groups are not valid");
662 1.1 rmind }
663 1.18 rmind if (rule_nesting_level) {
664 1.18 rmind yyerror("default group can only be at the top level");
665 1.18 rmind }
666 1.54 rmind defgroup = rl;
667 1.18 rmind }
668 1.1 rmind
669 1.18 rmind /* Set the current group and increase the nesting level. */
670 1.18 rmind if (rule_nesting_level >= MAX_RULE_NESTING) {
671 1.18 rmind yyerror("rule nesting limit reached");
672 1.1 rmind }
673 1.18 rmind current_group[++rule_nesting_level] = rl;
674 1.18 rmind }
675 1.1 rmind
676 1.18 rmind void
677 1.18 rmind npfctl_build_group_end(void)
678 1.18 rmind {
679 1.46 rmind nl_rule_t *parent, *group;
680 1.46 rmind
681 1.18 rmind assert(rule_nesting_level > 0);
682 1.46 rmind parent = current_group[rule_nesting_level - 1];
683 1.46 rmind group = current_group[rule_nesting_level];
684 1.18 rmind current_group[rule_nesting_level--] = NULL;
685 1.46 rmind
686 1.54 rmind /*
687 1.54 rmind * Note:
688 1.54 rmind * - If the parent is NULL, then it is a global rule.
689 1.54 rmind * - The default rule must be the last, so it is inserted later.
690 1.54 rmind */
691 1.54 rmind if (group == defgroup) {
692 1.54 rmind assert(parent == NULL);
693 1.54 rmind return;
694 1.54 rmind }
695 1.46 rmind npf_rule_insert(npf_conf, parent, group);
696 1.1 rmind }
697 1.1 rmind
698 1.1 rmind /*
699 1.26 rmind * npfctl_build_rule: create a rule, build byte-code from filter options,
700 1.18 rmind * if any, and insert into the ruleset of current group, or set the rule.
701 1.1 rmind */
702 1.1 rmind void
703 1.28 rmind npfctl_build_rule(uint32_t attr, const char *ifname, sa_family_t family,
704 1.55 rmind const npfvar_t *popts, const filt_opts_t *fopts,
705 1.27 rmind const char *pcap_filter, const char *rproc)
706 1.1 rmind {
707 1.1 rmind nl_rule_t *rl;
708 1.1 rmind
709 1.19 rmind attr |= (npf_conf ? 0 : NPF_RULE_DYNAMIC);
710 1.21 rmind
711 1.28 rmind rl = npf_rule_create(NULL, attr, ifname);
712 1.27 rmind if (pcap_filter) {
713 1.27 rmind npfctl_build_pcap(rl, pcap_filter);
714 1.27 rmind } else {
715 1.55 rmind npfctl_build_code(rl, family, popts, fopts);
716 1.27 rmind }
717 1.27 rmind
718 1.18 rmind if (rproc) {
719 1.18 rmind npf_rule_setproc(rl, rproc);
720 1.18 rmind }
721 1.18 rmind
722 1.18 rmind if (npf_conf) {
723 1.18 rmind nl_rule_t *cg = current_group[rule_nesting_level];
724 1.18 rmind
725 1.18 rmind if (rproc && !npf_rproc_exists_p(npf_conf, rproc)) {
726 1.18 rmind yyerror("rule procedure '%s' is not defined", rproc);
727 1.18 rmind }
728 1.18 rmind assert(cg != NULL);
729 1.18 rmind npf_rule_setprio(rl, NPF_PRI_LAST);
730 1.18 rmind npf_rule_insert(npf_conf, cg, rl);
731 1.18 rmind } else {
732 1.18 rmind /* We have parsed a single rule - set it. */
733 1.18 rmind the_rule = rl;
734 1.1 rmind }
735 1.1 rmind }
736 1.1 rmind
737 1.1 rmind /*
738 1.14 rmind * npfctl_build_nat: create a single NAT policy of a specified
739 1.13 rmind * type with a given filter options.
740 1.13 rmind */
741 1.36 rmind static nl_nat_t *
742 1.36 rmind npfctl_build_nat(int type, const char *ifname, const addr_port_t *ap,
743 1.55 rmind const npfvar_t *popts, const filt_opts_t *fopts, unsigned flags)
744 1.13 rmind {
745 1.47 rmind fam_addr_mask_t *am;
746 1.47 rmind sa_family_t family;
747 1.13 rmind in_port_t port;
748 1.13 rmind nl_nat_t *nat;
749 1.47 rmind unsigned tid;
750 1.13 rmind
751 1.35 rmind if (ap->ap_portrange) {
752 1.45 rmind /*
753 1.45 rmind * The port forwarding case. In such case, there has to
754 1.45 rmind * be a single port used for translation; we keep the port
755 1.45 rmind * translation on, but disable the port map.
756 1.45 rmind */
757 1.35 rmind port = npfctl_get_singleport(ap->ap_portrange);
758 1.47 rmind flags = (flags & ~NPF_NAT_PORTMAP) | NPF_NAT_PORTS;
759 1.35 rmind } else {
760 1.13 rmind port = 0;
761 1.13 rmind }
762 1.13 rmind
763 1.47 rmind nat = npf_nat_create(type, flags, ifname);
764 1.47 rmind
765 1.47 rmind switch (npfvar_get_type(ap->ap_netaddr, 0)) {
766 1.47 rmind case NPFVAR_FAM:
767 1.47 rmind /* Translation address. */
768 1.47 rmind am = npfctl_get_singlefam(ap->ap_netaddr);
769 1.47 rmind family = am->fam_family;
770 1.47 rmind npf_nat_setaddr(nat, family, &am->fam_addr, am->fam_mask);
771 1.47 rmind break;
772 1.47 rmind case NPFVAR_TABLE:
773 1.47 rmind /* Translation table. */
774 1.47 rmind family = AF_UNSPEC;
775 1.47 rmind tid = npfctl_get_singletable(ap->ap_netaddr);
776 1.47 rmind npf_nat_settable(nat, tid);
777 1.47 rmind break;
778 1.47 rmind default:
779 1.47 rmind yyerror("map must have a valid translation address");
780 1.47 rmind abort();
781 1.47 rmind }
782 1.47 rmind npf_nat_setport(nat, port);
783 1.55 rmind npfctl_build_code(nat, family, popts, fopts);
784 1.36 rmind return nat;
785 1.13 rmind }
786 1.13 rmind
787 1.49 rmind static void
788 1.49 rmind npfctl_dnat_check(const addr_port_t *ap, const unsigned algo)
789 1.49 rmind {
790 1.55 rmind const unsigned type = npfvar_get_type(ap->ap_netaddr, 0);
791 1.49 rmind fam_addr_mask_t *am;
792 1.49 rmind
793 1.49 rmind switch (algo) {
794 1.49 rmind case NPF_ALGO_NETMAP:
795 1.49 rmind if (type == NPFVAR_FAM) {
796 1.49 rmind break;
797 1.49 rmind }
798 1.49 rmind yyerror("translation address using NETMAP must be "
799 1.49 rmind "a network and not a dynamic pool");
800 1.49 rmind break;
801 1.49 rmind case NPF_ALGO_IPHASH:
802 1.49 rmind case NPF_ALGO_RR:
803 1.49 rmind case NPF_ALGO_NONE:
804 1.49 rmind if (type != NPFVAR_FAM) {
805 1.49 rmind break;
806 1.49 rmind }
807 1.49 rmind am = npfctl_get_singlefam(ap->ap_netaddr);
808 1.49 rmind if (am->fam_mask == NPF_NO_NETMASK) {
809 1.49 rmind break;
810 1.49 rmind }
811 1.49 rmind yyerror("translation address, given the specified algorithm, "
812 1.49 rmind "must be a pool or a single address");
813 1.49 rmind break;
814 1.49 rmind default:
815 1.49 rmind yyerror("invalid algorithm specified for dynamic NAT");
816 1.49 rmind }
817 1.49 rmind }
818 1.49 rmind
819 1.13 rmind /*
820 1.14 rmind * npfctl_build_natseg: validate and create NAT policies.
821 1.1 rmind */
822 1.1 rmind void
823 1.45 rmind npfctl_build_natseg(int sd, int type, unsigned mflags, const char *ifname,
824 1.55 rmind const addr_port_t *ap1, const addr_port_t *ap2, const npfvar_t *popts,
825 1.47 rmind const filt_opts_t *fopts, unsigned algo)
826 1.1 rmind {
827 1.36 rmind fam_addr_mask_t *am1 = NULL, *am2 = NULL;
828 1.36 rmind nl_nat_t *nt1 = NULL, *nt2 = NULL;
829 1.7 rmind filt_opts_t imfopts;
830 1.36 rmind uint16_t adj = 0;
831 1.47 rmind unsigned flags;
832 1.13 rmind bool binat;
833 1.1 rmind
834 1.28 rmind assert(ifname != NULL);
835 1.7 rmind
836 1.13 rmind /*
837 1.47 rmind * Validate that mapping has the translation address(es) set.
838 1.47 rmind */
839 1.47 rmind if ((type & NPF_NATIN) != 0 && ap1->ap_netaddr == NULL) {
840 1.47 rmind yyerror("inbound network segment is not specified");
841 1.47 rmind }
842 1.47 rmind if ((type & NPF_NATOUT) != 0 && ap2->ap_netaddr == NULL) {
843 1.47 rmind yyerror("outbound network segment is not specified");
844 1.47 rmind }
845 1.47 rmind
846 1.47 rmind /*
847 1.13 rmind * Bi-directional NAT is a combination of inbound NAT and outbound
848 1.35 rmind * NAT policies with the translation segments inverted respectively.
849 1.13 rmind */
850 1.13 rmind binat = (NPF_NATIN | NPF_NATOUT) == type;
851 1.7 rmind
852 1.35 rmind switch (sd) {
853 1.35 rmind case NPFCTL_NAT_DYNAMIC:
854 1.35 rmind /*
855 1.47 rmind * Dynamic NAT: stateful translation -- traditional NAPT
856 1.47 rmind * is expected. Unless it is bi-directional NAT, perform
857 1.47 rmind * the port mapping.
858 1.35 rmind */
859 1.35 rmind flags = !binat ? (NPF_NAT_PORTS | NPF_NAT_PORTMAP) : 0;
860 1.49 rmind if (type & NPF_NATIN) {
861 1.49 rmind npfctl_dnat_check(ap1, algo);
862 1.49 rmind }
863 1.49 rmind if (type & NPF_NATOUT) {
864 1.49 rmind npfctl_dnat_check(ap2, algo);
865 1.47 rmind }
866 1.35 rmind break;
867 1.35 rmind case NPFCTL_NAT_STATIC:
868 1.47 rmind /*
869 1.47 rmind * Static NAT: stateless translation.
870 1.47 rmind */
871 1.35 rmind flags = NPF_NAT_STATIC;
872 1.47 rmind
873 1.47 rmind /* Note: translation address/network cannot be a table. */
874 1.55 rmind if (type & NPF_NATIN) {
875 1.55 rmind am1 = npfctl_get_singlefam(ap1->ap_netaddr);
876 1.55 rmind }
877 1.55 rmind if (type & NPF_NATOUT) {
878 1.55 rmind am2 = npfctl_get_singlefam(ap2->ap_netaddr);
879 1.55 rmind }
880 1.47 rmind
881 1.47 rmind /* Validate the algorithm. */
882 1.47 rmind switch (algo) {
883 1.47 rmind case NPF_ALGO_NPT66:
884 1.55 rmind if (!binat || am1->fam_mask != am2->fam_mask) {
885 1.47 rmind yyerror("asymmetric NPTv6 is not supported");
886 1.47 rmind }
887 1.47 rmind adj = npfctl_npt66_calcadj(am1->fam_mask,
888 1.47 rmind &am1->fam_addr, &am2->fam_addr);
889 1.47 rmind break;
890 1.47 rmind case NPF_ALGO_NETMAP:
891 1.55 rmind if (binat && am1->fam_mask != am2->fam_mask) {
892 1.47 rmind yyerror("net-to-net mapping using the "
893 1.47 rmind "NETMAP algorithm must be 1:1");
894 1.47 rmind }
895 1.47 rmind break;
896 1.47 rmind case NPF_ALGO_NONE:
897 1.55 rmind if ((am1 && am1->fam_mask != NPF_NO_NETMASK) ||
898 1.55 rmind (am2 && am2->fam_mask != NPF_NO_NETMASK)) {
899 1.47 rmind yyerror("static net-to-net translation "
900 1.47 rmind "must have an algorithm specified");
901 1.47 rmind }
902 1.47 rmind break;
903 1.47 rmind default:
904 1.47 rmind yyerror("invalid algorithm specified for static NAT");
905 1.47 rmind }
906 1.35 rmind break;
907 1.35 rmind default:
908 1.35 rmind abort();
909 1.35 rmind }
910 1.35 rmind
911 1.7 rmind /*
912 1.45 rmind * Apply the flag modifications.
913 1.45 rmind */
914 1.45 rmind if (mflags & NPF_NAT_PORTS) {
915 1.45 rmind flags &= ~(NPF_NAT_PORTS | NPF_NAT_PORTMAP);
916 1.45 rmind }
917 1.45 rmind
918 1.45 rmind /*
919 1.13 rmind * If the filter criteria is not specified explicitly, apply implicit
920 1.14 rmind * filtering according to the given network segments.
921 1.13 rmind *
922 1.13 rmind * Note: filled below, depending on the type.
923 1.7 rmind */
924 1.14 rmind if (__predict_true(!fopts)) {
925 1.7 rmind fopts = &imfopts;
926 1.1 rmind }
927 1.1 rmind
928 1.13 rmind if (type & NPF_NATIN) {
929 1.13 rmind memset(&imfopts, 0, sizeof(filt_opts_t));
930 1.13 rmind memcpy(&imfopts.fo_to, ap2, sizeof(addr_port_t));
931 1.55 rmind nt1 = npfctl_build_nat(NPF_NATIN, ifname,
932 1.55 rmind ap1, popts, fopts, flags);
933 1.13 rmind }
934 1.13 rmind if (type & NPF_NATOUT) {
935 1.13 rmind memset(&imfopts, 0, sizeof(filt_opts_t));
936 1.13 rmind memcpy(&imfopts.fo_from, ap1, sizeof(addr_port_t));
937 1.55 rmind nt2 = npfctl_build_nat(NPF_NATOUT, ifname,
938 1.55 rmind ap2, popts, fopts, flags);
939 1.36 rmind }
940 1.36 rmind
941 1.49 rmind switch (algo) {
942 1.49 rmind case NPF_ALGO_NONE:
943 1.49 rmind break;
944 1.49 rmind case NPF_ALGO_NPT66:
945 1.47 rmind /*
946 1.47 rmind * NPTv6 is a special case using special adjustment value.
947 1.47 rmind * It is always bidirectional NAT.
948 1.47 rmind */
949 1.47 rmind assert(nt1 && nt2);
950 1.36 rmind npf_nat_setnpt66(nt1, ~adj);
951 1.36 rmind npf_nat_setnpt66(nt2, adj);
952 1.49 rmind break;
953 1.49 rmind default:
954 1.47 rmind /*
955 1.47 rmind * Set the algorithm.
956 1.47 rmind */
957 1.47 rmind if (nt1) {
958 1.47 rmind npf_nat_setalgo(nt1, algo);
959 1.47 rmind }
960 1.47 rmind if (nt2) {
961 1.47 rmind npf_nat_setalgo(nt2, algo);
962 1.47 rmind }
963 1.1 rmind }
964 1.46 rmind
965 1.53 rmind if (npf_conf) {
966 1.53 rmind if (nt1) {
967 1.53 rmind npf_rule_setprio(nt1, NPF_PRI_LAST);
968 1.53 rmind npf_nat_insert(npf_conf, nt1);
969 1.53 rmind }
970 1.53 rmind if (nt2) {
971 1.53 rmind npf_rule_setprio(nt2, NPF_PRI_LAST);
972 1.53 rmind npf_nat_insert(npf_conf, nt2);
973 1.53 rmind }
974 1.53 rmind } else {
975 1.53 rmind // XXX/TODO: need to refactor a bit to enable this..
976 1.53 rmind if (nt1 && nt2) {
977 1.53 rmind errx(EXIT_FAILURE, "bidirectional NAT is currently "
978 1.53 rmind "not yet supported in the dynamic rules");
979 1.53 rmind }
980 1.53 rmind the_rule = nt1 ? nt1 : nt2;
981 1.46 rmind }
982 1.1 rmind }
983 1.1 rmind
984 1.1 rmind /*
985 1.1 rmind * npfctl_fill_table: fill NPF table with entries from a specified file.
986 1.1 rmind */
987 1.1 rmind static void
988 1.55 rmind npfctl_fill_table(nl_table_t *tl, unsigned type, const char *fname, FILE *fp)
989 1.1 rmind {
990 1.1 rmind char *buf = NULL;
991 1.1 rmind int l = 0;
992 1.1 rmind size_t n;
993 1.1 rmind
994 1.52 rmind if (fp == NULL && (fp = fopen(fname, "r")) == NULL) {
995 1.1 rmind err(EXIT_FAILURE, "open '%s'", fname);
996 1.1 rmind }
997 1.1 rmind while (l++, getline(&buf, &n, fp) != -1) {
998 1.11 rmind fam_addr_mask_t fam;
999 1.11 rmind int alen;
1000 1.1 rmind
1001 1.1 rmind if (*buf == '\n' || *buf == '#') {
1002 1.1 rmind continue;
1003 1.1 rmind }
1004 1.11 rmind
1005 1.11 rmind if (!npfctl_parse_cidr(buf, &fam, &alen)) {
1006 1.11 rmind errx(EXIT_FAILURE,
1007 1.11 rmind "%s:%d: invalid table entry", fname, l);
1008 1.11 rmind }
1009 1.47 rmind if (type != NPF_TABLE_LPM && fam.fam_mask != NPF_NO_NETMASK) {
1010 1.33 rmind errx(EXIT_FAILURE, "%s:%d: mask used with the "
1011 1.47 rmind "table type other than \"lpm\"", fname, l);
1012 1.1 rmind }
1013 1.1 rmind
1014 1.46 rmind npf_table_add_entry(tl, fam.fam_family,
1015 1.46 rmind &fam.fam_addr, fam.fam_mask);
1016 1.33 rmind }
1017 1.46 rmind free(buf);
1018 1.1 rmind }
1019 1.1 rmind
1020 1.1 rmind /*
1021 1.52 rmind * npfctl_load_table: create an NPF table and fill with contents from a file.
1022 1.52 rmind */
1023 1.52 rmind nl_table_t *
1024 1.55 rmind npfctl_load_table(const char *tname, int tid, unsigned type,
1025 1.52 rmind const char *fname, FILE *fp)
1026 1.52 rmind {
1027 1.52 rmind nl_table_t *tl;
1028 1.52 rmind
1029 1.52 rmind tl = npf_table_create(tname, tid, type);
1030 1.52 rmind if (tl && fname) {
1031 1.52 rmind npfctl_fill_table(tl, type, fname, fp);
1032 1.52 rmind }
1033 1.52 rmind
1034 1.52 rmind return tl;
1035 1.52 rmind }
1036 1.52 rmind
1037 1.52 rmind /*
1038 1.1 rmind * npfctl_build_table: create an NPF table, add to the configuration and,
1039 1.1 rmind * if required, fill with contents from a file.
1040 1.1 rmind */
1041 1.1 rmind void
1042 1.55 rmind npfctl_build_table(const char *tname, unsigned type, const char *fname)
1043 1.1 rmind {
1044 1.1 rmind nl_table_t *tl;
1045 1.1 rmind
1046 1.52 rmind if (type == NPF_TABLE_CONST && !fname) {
1047 1.47 rmind yyerror("table type 'const' must be loaded from a file");
1048 1.1 rmind }
1049 1.46 rmind
1050 1.52 rmind tl = npfctl_load_table(tname, npfctl_tid_counter++, type, fname, NULL);
1051 1.52 rmind assert(tl != NULL);
1052 1.52 rmind
1053 1.46 rmind if (npf_table_insert(npf_conf, tl)) {
1054 1.46 rmind yyerror("table '%s' is already defined", tname);
1055 1.46 rmind }
1056 1.1 rmind }
1057 1.23 christos
1058 1.47 rmind /*
1059 1.47 rmind * npfctl_ifnet_table: get a variable with ifaddr-table; auto-create
1060 1.47 rmind * the table on first reference.
1061 1.47 rmind */
1062 1.43 rmind npfvar_t *
1063 1.43 rmind npfctl_ifnet_table(const char *ifname)
1064 1.43 rmind {
1065 1.43 rmind char tname[NPF_TABLE_MAXNAMELEN];
1066 1.43 rmind nl_table_t *tl;
1067 1.53 rmind unsigned tid;
1068 1.43 rmind
1069 1.47 rmind snprintf(tname, sizeof(tname), NPF_IFNET_TABLE_PREF "%s", ifname);
1070 1.53 rmind if (!npf_conf) {
1071 1.53 rmind errx(EXIT_FAILURE, "expression `ifaddrs(%s)` is currently "
1072 1.53 rmind "not yet supported in dynamic rules", ifname);
1073 1.53 rmind }
1074 1.43 rmind
1075 1.43 rmind tid = npfctl_table_getid(tname);
1076 1.43 rmind if (tid == (unsigned)-1) {
1077 1.43 rmind tid = npfctl_tid_counter++;
1078 1.47 rmind tl = npf_table_create(tname, tid, NPF_TABLE_IFADDR);
1079 1.43 rmind (void)npf_table_insert(npf_conf, tl);
1080 1.43 rmind }
1081 1.53 rmind return npfvar_create_element(NPFVAR_TABLE, &tid, sizeof(unsigned));
1082 1.43 rmind }
1083 1.43 rmind
1084 1.23 christos /*
1085 1.25 rmind * npfctl_build_alg: create an NPF application level gateway and add it
1086 1.23 christos * to the configuration.
1087 1.23 christos */
1088 1.23 christos void
1089 1.23 christos npfctl_build_alg(const char *al_name)
1090 1.23 christos {
1091 1.49 rmind if (npf_alg_load(npf_conf, al_name) != 0) {
1092 1.49 rmind yyerror("ALG '%s' is already loaded", al_name);
1093 1.49 rmind }
1094 1.49 rmind }
1095 1.49 rmind
1096 1.49 rmind void
1097 1.49 rmind npfctl_setparam(const char *name, int val)
1098 1.49 rmind {
1099 1.49 rmind if (strcmp(name, "bpf.jit") == 0) {
1100 1.49 rmind npfctl_bpfjit(val != 0);
1101 1.50 rmind return;
1102 1.49 rmind }
1103 1.49 rmind if (npf_param_set(npf_conf, name, val) != 0) {
1104 1.49 rmind yyerror("invalid parameter `%s` or its value", name);
1105 1.23 christos }
1106 1.23 christos }
1107 1.27 rmind
1108 1.27 rmind static void
1109 1.27 rmind npfctl_dump_bpf(struct bpf_program *bf)
1110 1.27 rmind {
1111 1.27 rmind if (npf_debug) {
1112 1.27 rmind extern char *yytext;
1113 1.27 rmind extern int yylineno;
1114 1.27 rmind
1115 1.27 rmind int rule_line = yylineno - (int)(*yytext == '\n');
1116 1.27 rmind printf("\nRULE AT LINE %d\n", rule_line);
1117 1.27 rmind bpf_dump(bf, 0);
1118 1.27 rmind }
1119 1.27 rmind }
1120