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