Home | History | Annotate | Line # | Download | only in npfctl
npf_build.c revision 1.50.2.1
      1       1.1     rmind /*-
      2      1.49     rmind  * Copyright (c) 2011-2019 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.50.2.1    martin __RCSID("$NetBSD: npf_build.c,v 1.50.2.1 2019/08/11 10:10:23 martin 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.18     rmind 
     60      1.46     rmind static bool			defgroup = false;
     61      1.18     rmind static nl_rule_t *		current_group[MAX_RULE_NESTING];
     62      1.18     rmind static unsigned			rule_nesting_level = 0;
     63      1.43     rmind static unsigned			npfctl_tid_counter = 0;
     64       1.1     rmind 
     65      1.27     rmind static void			npfctl_dump_bpf(struct bpf_program *);
     66      1.27     rmind 
     67       1.1     rmind void
     68       1.1     rmind npfctl_config_init(bool debug)
     69       1.1     rmind {
     70       1.1     rmind 	npf_conf = npf_config_create();
     71       1.1     rmind 	if (npf_conf == NULL) {
     72       1.1     rmind 		errx(EXIT_FAILURE, "npf_config_create failed");
     73       1.1     rmind 	}
     74       1.1     rmind 	npf_debug = debug;
     75      1.18     rmind 	memset(current_group, 0, sizeof(current_group));
     76       1.1     rmind }
     77       1.1     rmind 
     78       1.1     rmind int
     79      1.46     rmind npfctl_config_send(int fd)
     80       1.1     rmind {
     81      1.41  christos 	npf_error_t errinfo;
     82      1.41  christos 	int error = 0;
     83       1.1     rmind 
     84      1.18     rmind 	if (!defgroup) {
     85       1.1     rmind 		errx(EXIT_FAILURE, "default group was not defined");
     86       1.1     rmind 	}
     87      1.46     rmind 	error = npf_config_submit(npf_conf, fd, &errinfo);
     88      1.39     rmind 	if (error == EEXIST) { /* XXX */
     89      1.39     rmind 		errx(EXIT_FAILURE, "(re)load failed: "
     90      1.39     rmind 		    "some table has a duplicate entry?");
     91      1.39     rmind 	}
     92       1.3     rmind 	if (error) {
     93      1.41  christos 		npfctl_print_error(&errinfo);
     94       1.3     rmind 	}
     95      1.41  christos 	npf_config_destroy(npf_conf);
     96      1.41  christos 	return error;
     97      1.41  christos }
     98      1.41  christos 
     99      1.41  christos void
    100      1.41  christos npfctl_config_save(nl_config_t *ncf, const char *outfile)
    101      1.41  christos {
    102      1.41  christos 	void *blob;
    103      1.41  christos 	size_t len;
    104      1.41  christos 	int fd;
    105      1.41  christos 
    106      1.41  christos 	blob = npf_config_export(ncf, &len);
    107      1.41  christos 	if (!blob)
    108      1.41  christos 		err(EXIT_FAILURE, "npf_config_export");
    109      1.41  christos 	if ((fd = open(outfile, O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1)
    110      1.41  christos 		err(EXIT_FAILURE, "could not open %s", outfile);
    111      1.41  christos 	if (write(fd, blob, len) != (ssize_t)len) {
    112      1.41  christos 		err(EXIT_FAILURE, "write to %s failed", outfile);
    113      1.25     rmind 	}
    114      1.41  christos 	free(blob);
    115      1.41  christos 	close(fd);
    116       1.1     rmind }
    117       1.1     rmind 
    118      1.46     rmind void
    119      1.46     rmind npfctl_config_debug(const char *outfile)
    120      1.46     rmind {
    121      1.46     rmind 	printf("\nConfiguration:\n\n");
    122      1.46     rmind 	_npf_config_dump(npf_conf, STDOUT_FILENO);
    123      1.46     rmind 
    124      1.46     rmind 	printf("\nSaving binary to %s\n", outfile);
    125      1.46     rmind 	npfctl_config_save(npf_conf, outfile);
    126      1.46     rmind 	npf_config_destroy(npf_conf);
    127      1.46     rmind }
    128      1.46     rmind 
    129      1.16     rmind nl_config_t *
    130      1.16     rmind npfctl_config_ref(void)
    131      1.16     rmind {
    132      1.16     rmind 	return npf_conf;
    133      1.16     rmind }
    134      1.16     rmind 
    135      1.18     rmind nl_rule_t *
    136      1.18     rmind npfctl_rule_ref(void)
    137      1.18     rmind {
    138      1.18     rmind 	return the_rule;
    139      1.18     rmind }
    140      1.18     rmind 
    141      1.28     rmind bool
    142      1.13     rmind npfctl_debug_addif(const char *ifname)
    143      1.13     rmind {
    144      1.28     rmind 	const char tname[] = "npftest";
    145      1.13     rmind 	const size_t tnamelen = sizeof(tname) - 1;
    146      1.13     rmind 
    147      1.28     rmind 	if (npf_debug) {
    148      1.28     rmind 		_npf_debug_addif(npf_conf, ifname);
    149      1.28     rmind 		return strncmp(ifname, tname, tnamelen) == 0;
    150      1.13     rmind 	}
    151      1.28     rmind 	return 0;
    152      1.13     rmind }
    153      1.13     rmind 
    154      1.32     rmind unsigned
    155      1.32     rmind npfctl_table_getid(const char *name)
    156       1.1     rmind {
    157      1.32     rmind 	unsigned tid = (unsigned)-1;
    158      1.49     rmind 	nl_iter_t i = NPF_ITER_BEGIN;
    159      1.32     rmind 	nl_table_t *tl;
    160      1.32     rmind 
    161      1.32     rmind 	/* XXX dynamic ruleset */
    162      1.32     rmind 	if (!npf_conf) {
    163      1.32     rmind 		return (unsigned)-1;
    164      1.32     rmind 	}
    165      1.49     rmind 	while ((tl = npf_table_iterate(npf_conf, &i)) != NULL) {
    166      1.32     rmind 		const char *tname = npf_table_getname(tl);
    167      1.32     rmind 		if (strcmp(tname, name) == 0) {
    168      1.32     rmind 			tid = npf_table_getid(tl);
    169      1.49     rmind 			break;
    170      1.32     rmind 		}
    171      1.32     rmind 	}
    172      1.32     rmind 	return tid;
    173       1.1     rmind }
    174       1.1     rmind 
    175      1.47     rmind const char *
    176      1.47     rmind npfctl_table_getname(nl_config_t *ncf, unsigned tid, bool *ifaddr)
    177      1.47     rmind {
    178      1.47     rmind 	const char *name = NULL;
    179      1.49     rmind 	nl_iter_t i = NPF_ITER_BEGIN;
    180      1.47     rmind 	nl_table_t *tl;
    181      1.47     rmind 
    182      1.49     rmind 	while ((tl = npf_table_iterate(ncf, &i)) != NULL) {
    183      1.47     rmind 		if (npf_table_getid(tl) == tid) {
    184      1.47     rmind 			name = npf_table_getname(tl);
    185      1.49     rmind 			break;
    186      1.47     rmind 		}
    187      1.47     rmind 	}
    188      1.47     rmind 	if (!name) {
    189      1.47     rmind 		return NULL;
    190      1.47     rmind 	}
    191      1.47     rmind 	if (!strncmp(name, NPF_IFNET_TABLE_PREF, NPF_IFNET_TABLE_PREFLEN)) {
    192      1.47     rmind 		name += NPF_IFNET_TABLE_PREFLEN;
    193      1.47     rmind 		*ifaddr = true;
    194      1.47     rmind 	} else {
    195      1.47     rmind 		*ifaddr = false;
    196      1.47     rmind 	}
    197      1.47     rmind 	return name;
    198      1.47     rmind }
    199      1.47     rmind 
    200       1.7     rmind static in_port_t
    201       1.1     rmind npfctl_get_singleport(const npfvar_t *vp)
    202       1.1     rmind {
    203       1.1     rmind 	port_range_t *pr;
    204       1.7     rmind 	in_port_t *port;
    205       1.1     rmind 
    206       1.1     rmind 	if (npfvar_get_count(vp) > 1) {
    207       1.1     rmind 		yyerror("multiple ports are not valid");
    208       1.1     rmind 	}
    209       1.1     rmind 	pr = npfvar_get_data(vp, NPFVAR_PORT_RANGE, 0);
    210       1.1     rmind 	if (pr->pr_start != pr->pr_end) {
    211       1.1     rmind 		yyerror("port range is not valid");
    212       1.1     rmind 	}
    213       1.7     rmind 	port = &pr->pr_start;
    214       1.7     rmind 	return *port;
    215       1.1     rmind }
    216       1.1     rmind 
    217       1.1     rmind static fam_addr_mask_t *
    218       1.1     rmind npfctl_get_singlefam(const npfvar_t *vp)
    219       1.1     rmind {
    220      1.47     rmind 	fam_addr_mask_t *am;
    221      1.47     rmind 
    222      1.47     rmind 	if (npfvar_get_type(vp, 0) != NPFVAR_FAM) {
    223      1.47     rmind 		yyerror("map segment must be an address or network");
    224      1.47     rmind 	}
    225      1.47     rmind 	if (npfvar_get_count(vp) > 1) {
    226      1.47     rmind 		yyerror("map segment cannot have multiple static addresses");
    227      1.47     rmind 	}
    228      1.47     rmind 	am = npfvar_get_data(vp, NPFVAR_FAM, 0);
    229      1.47     rmind 	if (am == NULL) {
    230      1.47     rmind 		yyerror("invalid map segment");
    231      1.47     rmind 	}
    232      1.47     rmind 	return am;
    233      1.47     rmind }
    234      1.47     rmind 
    235      1.47     rmind static unsigned
    236      1.47     rmind npfctl_get_singletable(const npfvar_t *vp)
    237      1.47     rmind {
    238      1.47     rmind 	unsigned *tid;
    239      1.47     rmind 
    240       1.1     rmind 	if (npfvar_get_count(vp) > 1) {
    241      1.47     rmind 		yyerror("multiple tables are not valid");
    242       1.1     rmind 	}
    243      1.47     rmind 	tid = npfvar_get_data(vp, NPFVAR_TABLE, 0);
    244      1.47     rmind 	assert(tid != NULL);
    245      1.47     rmind 	return *tid;
    246       1.1     rmind }
    247       1.1     rmind 
    248      1.10     rmind static bool
    249      1.25     rmind npfctl_build_fam(npf_bpf_t *ctx, sa_family_t family,
    250       1.1     rmind     fam_addr_mask_t *fam, int opts)
    251       1.1     rmind {
    252       1.1     rmind 	/*
    253       1.1     rmind 	 * If family is specified, address does not match it and the
    254       1.1     rmind 	 * address is extracted from the interface, then simply ignore.
    255       1.1     rmind 	 * Otherwise, address of invalid family was passed manually.
    256       1.1     rmind 	 */
    257       1.1     rmind 	if (family != AF_UNSPEC && family != fam->fam_family) {
    258      1.15     rmind 		if (!fam->fam_ifindex) {
    259       1.1     rmind 			yyerror("specified address is not of the required "
    260       1.1     rmind 			    "family %d", family);
    261       1.1     rmind 		}
    262      1.10     rmind 		return false;
    263       1.1     rmind 	}
    264      1.30     rmind 
    265      1.25     rmind 	family = fam->fam_family;
    266      1.30     rmind 	if (family != AF_INET && family != AF_INET6) {
    267      1.30     rmind 		yyerror("family %d is not supported", family);
    268      1.30     rmind 	}
    269       1.1     rmind 
    270       1.1     rmind 	/*
    271       1.1     rmind 	 * Optimise 0.0.0.0/0 case to be NOP.  Otherwise, address with
    272       1.1     rmind 	 * zero mask would never match and therefore is not valid.
    273       1.1     rmind 	 */
    274       1.1     rmind 	if (fam->fam_mask == 0) {
    275      1.30     rmind 		static const npf_addr_t zero; /* must be static */
    276      1.10     rmind 
    277       1.1     rmind 		if (memcmp(&fam->fam_addr, &zero, sizeof(npf_addr_t))) {
    278       1.1     rmind 			yyerror("filter criterion would never match");
    279       1.1     rmind 		}
    280      1.10     rmind 		return false;
    281       1.1     rmind 	}
    282       1.1     rmind 
    283      1.25     rmind 	npfctl_bpf_cidr(ctx, opts, family, &fam->fam_addr, fam->fam_mask);
    284      1.10     rmind 	return true;
    285       1.1     rmind }
    286       1.1     rmind 
    287       1.1     rmind static void
    288      1.25     rmind npfctl_build_vars(npf_bpf_t *ctx, sa_family_t family, npfvar_t *vars, int opts)
    289       1.1     rmind {
    290       1.6  christos 	const int type = npfvar_get_type(vars, 0);
    291       1.1     rmind 	size_t i;
    292       1.1     rmind 
    293  1.50.2.1    martin 	npfctl_bpf_group_enter(ctx);
    294       1.1     rmind 	for (i = 0; i < npfvar_get_count(vars); i++) {
    295       1.1     rmind 		void *data = npfvar_get_data(vars, type, i);
    296       1.1     rmind 		assert(data != NULL);
    297       1.1     rmind 
    298       1.1     rmind 		switch (type) {
    299       1.1     rmind 		case NPFVAR_FAM: {
    300       1.1     rmind 			fam_addr_mask_t *fam = data;
    301      1.25     rmind 			npfctl_build_fam(ctx, family, fam, opts);
    302       1.1     rmind 			break;
    303       1.1     rmind 		}
    304       1.1     rmind 		case NPFVAR_PORT_RANGE: {
    305       1.1     rmind 			port_range_t *pr = data;
    306      1.25     rmind 			npfctl_bpf_ports(ctx, opts, pr->pr_start, pr->pr_end);
    307       1.1     rmind 			break;
    308       1.1     rmind 		}
    309       1.1     rmind 		case NPFVAR_TABLE: {
    310      1.32     rmind 			u_int tid;
    311      1.32     rmind 			memcpy(&tid, data, sizeof(u_int));
    312      1.25     rmind 			npfctl_bpf_table(ctx, opts, tid);
    313       1.1     rmind 			break;
    314       1.1     rmind 		}
    315       1.1     rmind 		default:
    316       1.1     rmind 			assert(false);
    317       1.1     rmind 		}
    318       1.1     rmind 	}
    319  1.50.2.1    martin 	npfctl_bpf_group_exit(ctx, (opts & MATCH_INVERT) != 0);
    320       1.1     rmind }
    321       1.1     rmind 
    322      1.25     rmind static void
    323      1.25     rmind npfctl_build_proto(npf_bpf_t *ctx, sa_family_t family, const opt_proto_t *op)
    324       1.1     rmind {
    325       1.1     rmind 	const npfvar_t *popts = op->op_opts;
    326      1.10     rmind 	const int proto = op->op_proto;
    327      1.25     rmind 
    328      1.25     rmind 	/* IP version and/or L4 protocol matching. */
    329      1.25     rmind 	if (family != AF_UNSPEC || proto != -1) {
    330      1.25     rmind 		npfctl_bpf_proto(ctx, family, proto);
    331      1.25     rmind 	}
    332       1.1     rmind 
    333      1.10     rmind 	switch (proto) {
    334       1.1     rmind 	case IPPROTO_TCP:
    335      1.25     rmind 		/* Build TCP flags matching (optional). */
    336      1.25     rmind 		if (popts) {
    337      1.25     rmind 			uint8_t *tf, *tf_mask;
    338      1.25     rmind 
    339      1.25     rmind 			assert(npfvar_get_count(popts) == 2);
    340      1.25     rmind 			tf = npfvar_get_data(popts, NPFVAR_TCPFLAG, 0);
    341      1.25     rmind 			tf_mask = npfvar_get_data(popts, NPFVAR_TCPFLAG, 1);
    342      1.37     rmind 			npfctl_bpf_tcpfl(ctx, *tf, *tf_mask, false);
    343       1.1     rmind 		}
    344       1.1     rmind 		break;
    345       1.1     rmind 	case IPPROTO_ICMP:
    346      1.12       spz 	case IPPROTO_ICMPV6:
    347      1.25     rmind 		/* Build ICMP/ICMPv6 type and/or code matching. */
    348      1.25     rmind 		if (popts) {
    349      1.25     rmind 			int *icmp_type, *icmp_code;
    350      1.25     rmind 
    351      1.25     rmind 			assert(npfvar_get_count(popts) == 2);
    352      1.25     rmind 			icmp_type = npfvar_get_data(popts, NPFVAR_ICMP, 0);
    353      1.25     rmind 			icmp_code = npfvar_get_data(popts, NPFVAR_ICMP, 1);
    354      1.25     rmind 			npfctl_bpf_icmp(ctx, *icmp_type, *icmp_code);
    355      1.12       spz 		}
    356      1.12       spz 		break;
    357      1.25     rmind 	default:
    358      1.25     rmind 		/* No options for other protocols. */
    359       1.1     rmind 		break;
    360      1.10     rmind 	}
    361       1.1     rmind }
    362       1.1     rmind 
    363       1.1     rmind static bool
    364      1.25     rmind npfctl_build_code(nl_rule_t *rl, sa_family_t family, const opt_proto_t *op,
    365      1.27     rmind     const filt_opts_t *fopts)
    366       1.1     rmind {
    367      1.48       tih 	bool noproto, noaddrs, noports, nostate, need_tcpudp = false;
    368       1.7     rmind 	const addr_port_t *apfrom = &fopts->fo_from;
    369       1.7     rmind 	const addr_port_t *apto = &fopts->fo_to;
    370      1.10     rmind 	const int proto = op->op_proto;
    371      1.25     rmind 	npf_bpf_t *bc;
    372      1.42     rmind 	unsigned opts;
    373       1.1     rmind 	size_t len;
    374       1.1     rmind 
    375      1.25     rmind 	/* If none specified, then no byte-code. */
    376      1.25     rmind 	noproto = family == AF_UNSPEC && proto == -1 && !op->op_opts;
    377      1.20     rmind 	noaddrs = !apfrom->ap_netaddr && !apto->ap_netaddr;
    378      1.20     rmind 	noports = !apfrom->ap_portrange && !apto->ap_portrange;
    379      1.48       tih 	nostate = !(npf_rule_getattr(rl) & NPF_RULE_STATEFUL);
    380      1.48       tih 	if (noproto && noaddrs && noports && nostate) {
    381       1.1     rmind 		return false;
    382      1.25     rmind 	}
    383       1.1     rmind 
    384      1.25     rmind 	/*
    385      1.25     rmind 	 * Sanity check: ports can only be used with TCP or UDP protocol.
    386      1.25     rmind 	 * No filter options are supported for other protocols, only the
    387      1.25     rmind 	 * IP addresses are allowed.
    388      1.25     rmind 	 */
    389      1.25     rmind 	if (!noports) {
    390      1.25     rmind 		switch (proto) {
    391      1.25     rmind 		case IPPROTO_TCP:
    392      1.25     rmind 		case IPPROTO_UDP:
    393      1.38     rmind 			break;
    394      1.25     rmind 		case -1:
    395      1.38     rmind 			need_tcpudp = true;
    396      1.25     rmind 			break;
    397      1.25     rmind 		default:
    398      1.25     rmind 			yyerror("invalid filter options for protocol %d", proto);
    399      1.25     rmind 		}
    400      1.25     rmind 	}
    401       1.1     rmind 
    402      1.25     rmind 	bc = npfctl_bpf_create();
    403       1.1     rmind 
    404      1.10     rmind 	/* Build layer 4 protocol blocks. */
    405      1.25     rmind 	npfctl_build_proto(bc, family, op);
    406      1.10     rmind 
    407      1.37     rmind 	/*
    408      1.37     rmind 	 * If this is a stateful rule and TCP flags are not specified,
    409      1.37     rmind 	 * then add "flags S/SAFR" filter for TCP protocol case.
    410      1.37     rmind 	 */
    411      1.37     rmind 	if ((npf_rule_getattr(rl) & NPF_RULE_STATEFUL) != 0 &&
    412      1.37     rmind 	    (proto == -1 || (proto == IPPROTO_TCP && !op->op_opts))) {
    413      1.37     rmind 		npfctl_bpf_tcpfl(bc, TH_SYN,
    414      1.37     rmind 		    TH_SYN | TH_ACK | TH_FIN | TH_RST, proto == -1);
    415      1.37     rmind 	}
    416      1.37     rmind 
    417       1.1     rmind 	/* Build IP address blocks. */
    418      1.42     rmind 	opts = MATCH_SRC | (fopts->fo_finvert ? MATCH_INVERT : 0);
    419      1.42     rmind 	npfctl_build_vars(bc, family, apfrom->ap_netaddr, opts);
    420      1.42     rmind 	opts = MATCH_DST | (fopts->fo_tinvert ? MATCH_INVERT : 0);
    421      1.42     rmind 	npfctl_build_vars(bc, family, apto->ap_netaddr, opts);
    422       1.1     rmind 
    423       1.1     rmind 	/* Build port-range blocks. */
    424      1.38     rmind 	if (need_tcpudp) {
    425      1.38     rmind 		/* TCP/UDP check for the ports. */
    426  1.50.2.1    martin 		npfctl_bpf_group_enter(bc);
    427      1.38     rmind 		npfctl_bpf_proto(bc, AF_UNSPEC, IPPROTO_TCP);
    428      1.38     rmind 		npfctl_bpf_proto(bc, AF_UNSPEC, IPPROTO_UDP);
    429  1.50.2.1    martin 		npfctl_bpf_group_exit(bc, false);
    430      1.38     rmind 	}
    431      1.27     rmind 	npfctl_build_vars(bc, family, apfrom->ap_portrange, MATCH_SRC);
    432      1.27     rmind 	npfctl_build_vars(bc, family, apto->ap_portrange, MATCH_DST);
    433      1.25     rmind 
    434      1.25     rmind 	/* Set the byte-code marks, if any. */
    435      1.25     rmind 	const void *bmarks = npfctl_bpf_bmarks(bc, &len);
    436      1.25     rmind 	if (npf_rule_setinfo(rl, bmarks, len) == -1) {
    437      1.25     rmind 		errx(EXIT_FAILURE, "npf_rule_setinfo failed");
    438      1.25     rmind 	}
    439       1.1     rmind 
    440      1.25     rmind 	/* Complete BPF byte-code and pass to the rule. */
    441      1.25     rmind 	struct bpf_program *bf = npfctl_bpf_complete(bc);
    442      1.40     rmind 	if (bf == NULL) {
    443      1.40     rmind 		npfctl_bpf_destroy(bc);
    444      1.40     rmind 		return true;
    445      1.40     rmind 	}
    446      1.25     rmind 	len = bf->bf_len * sizeof(struct bpf_insn);
    447      1.10     rmind 
    448      1.49     rmind 	if (npf_rule_setcode(rl, NPF_CODE_BPF, bf->bf_insns, len) != 0) {
    449       1.1     rmind 		errx(EXIT_FAILURE, "npf_rule_setcode failed");
    450       1.1     rmind 	}
    451      1.27     rmind 	npfctl_dump_bpf(bf);
    452      1.25     rmind 	npfctl_bpf_destroy(bc);
    453      1.25     rmind 
    454       1.1     rmind 	return true;
    455       1.1     rmind }
    456       1.1     rmind 
    457       1.4     rmind static void
    458      1.27     rmind npfctl_build_pcap(nl_rule_t *rl, const char *filter)
    459      1.27     rmind {
    460      1.27     rmind 	const size_t maxsnaplen = 64 * 1024;
    461      1.27     rmind 	struct bpf_program bf;
    462      1.27     rmind 	size_t len;
    463      1.27     rmind 
    464      1.27     rmind 	if (pcap_compile_nopcap(maxsnaplen, DLT_RAW, &bf,
    465      1.27     rmind 	    filter, 1, PCAP_NETMASK_UNKNOWN) == -1) {
    466      1.27     rmind 		yyerror("invalid pcap-filter(7) syntax");
    467      1.27     rmind 	}
    468      1.27     rmind 	len = bf.bf_len * sizeof(struct bpf_insn);
    469      1.27     rmind 
    470      1.49     rmind 	if (npf_rule_setcode(rl, NPF_CODE_BPF, bf.bf_insns, len) != 0) {
    471      1.27     rmind 		errx(EXIT_FAILURE, "npf_rule_setcode failed");
    472      1.27     rmind 	}
    473      1.27     rmind 	npfctl_dump_bpf(&bf);
    474      1.27     rmind 	pcap_freecode(&bf);
    475      1.27     rmind }
    476      1.27     rmind 
    477      1.27     rmind static void
    478       1.4     rmind npfctl_build_rpcall(nl_rproc_t *rp, const char *name, npfvar_t *args)
    479       1.4     rmind {
    480      1.14     rmind 	npf_extmod_t *extmod;
    481      1.14     rmind 	nl_ext_t *extcall;
    482      1.14     rmind 	int error;
    483       1.4     rmind 
    484      1.14     rmind 	extmod = npf_extmod_get(name, &extcall);
    485      1.14     rmind 	if (extmod == NULL) {
    486       1.4     rmind 		yyerror("unknown rule procedure '%s'", name);
    487       1.4     rmind 	}
    488       1.4     rmind 
    489       1.4     rmind 	for (size_t i = 0; i < npfvar_get_count(args); i++) {
    490      1.14     rmind 		const char *param, *value;
    491      1.14     rmind 		proc_param_t *p;
    492       1.4     rmind 
    493      1.14     rmind 		p = npfvar_get_data(args, NPFVAR_PROC_PARAM, i);
    494      1.14     rmind 		param = p->pp_param;
    495      1.14     rmind 		value = p->pp_value;
    496      1.14     rmind 
    497      1.14     rmind 		error = npf_extmod_param(extmod, extcall, param, value);
    498      1.14     rmind 		switch (error) {
    499      1.14     rmind 		case EINVAL:
    500      1.14     rmind 			yyerror("invalid parameter '%s'", param);
    501      1.14     rmind 		default:
    502      1.14     rmind 			break;
    503       1.4     rmind 		}
    504       1.4     rmind 	}
    505      1.14     rmind 	error = npf_rproc_extcall(rp, extcall);
    506      1.14     rmind 	if (error) {
    507      1.14     rmind 		yyerror(error == EEXIST ?
    508      1.14     rmind 		    "duplicate procedure call" : "unexpected error");
    509      1.14     rmind 	}
    510       1.4     rmind }
    511       1.4     rmind 
    512       1.1     rmind /*
    513       1.1     rmind  * npfctl_build_rproc: create and insert a rule procedure.
    514       1.1     rmind  */
    515       1.1     rmind void
    516       1.4     rmind npfctl_build_rproc(const char *name, npfvar_t *procs)
    517       1.1     rmind {
    518       1.1     rmind 	nl_rproc_t *rp;
    519       1.4     rmind 	size_t i;
    520       1.1     rmind 
    521       1.1     rmind 	rp = npf_rproc_create(name);
    522       1.1     rmind 	if (rp == NULL) {
    523      1.23  christos 		errx(EXIT_FAILURE, "%s failed", __func__);
    524       1.1     rmind 	}
    525       1.4     rmind 
    526       1.4     rmind 	for (i = 0; i < npfvar_get_count(procs); i++) {
    527      1.14     rmind 		proc_call_t *pc = npfvar_get_data(procs, NPFVAR_PROC, i);
    528      1.14     rmind 		npfctl_build_rpcall(rp, pc->pc_name, pc->pc_opts);
    529       1.4     rmind 	}
    530      1.46     rmind 	npf_rproc_insert(npf_conf, rp);
    531       1.1     rmind }
    532       1.1     rmind 
    533      1.22     rmind void
    534      1.28     rmind npfctl_build_maprset(const char *name, int attr, const char *ifname)
    535      1.22     rmind {
    536      1.22     rmind 	const int attr_di = (NPF_RULE_IN | NPF_RULE_OUT);
    537      1.22     rmind 	nl_rule_t *rl;
    538      1.22     rmind 
    539      1.22     rmind 	/* If no direction is not specified, then both. */
    540      1.22     rmind 	if ((attr & attr_di) == 0) {
    541      1.22     rmind 		attr |= attr_di;
    542      1.22     rmind 	}
    543      1.22     rmind 	/* Allow only "in/out" attributes. */
    544      1.45     rmind 	attr = NPF_RULE_GROUP | NPF_RULE_DYNAMIC | (attr & attr_di);
    545      1.28     rmind 	rl = npf_rule_create(name, attr, ifname);
    546      1.49     rmind 	npf_rule_setprio(rl, NPF_PRI_LAST);
    547      1.49     rmind 	npf_nat_insert(npf_conf, rl);
    548      1.22     rmind }
    549      1.22     rmind 
    550       1.1     rmind /*
    551      1.46     rmind  * npfctl_build_group: create a group, update the current group pointer
    552      1.46     rmind  * and increase the nesting level.
    553       1.1     rmind  */
    554       1.1     rmind void
    555      1.28     rmind npfctl_build_group(const char *name, int attr, const char *ifname, bool def)
    556       1.1     rmind {
    557       1.1     rmind 	const int attr_di = (NPF_RULE_IN | NPF_RULE_OUT);
    558       1.1     rmind 	nl_rule_t *rl;
    559       1.1     rmind 
    560      1.18     rmind 	if (def || (attr & attr_di) == 0) {
    561      1.18     rmind 		attr |= attr_di;
    562      1.18     rmind 	}
    563      1.18     rmind 
    564      1.28     rmind 	rl = npf_rule_create(name, attr | NPF_RULE_GROUP, ifname);
    565      1.18     rmind 	npf_rule_setprio(rl, NPF_PRI_LAST);
    566      1.18     rmind 	if (def) {
    567      1.18     rmind 		if (defgroup) {
    568       1.1     rmind 			yyerror("multiple default groups are not valid");
    569       1.1     rmind 		}
    570      1.18     rmind 		if (rule_nesting_level) {
    571      1.18     rmind 			yyerror("default group can only be at the top level");
    572      1.18     rmind 		}
    573      1.46     rmind 		defgroup = true;
    574      1.18     rmind 	}
    575       1.1     rmind 
    576      1.18     rmind 	/* Set the current group and increase the nesting level. */
    577      1.18     rmind 	if (rule_nesting_level >= MAX_RULE_NESTING) {
    578      1.18     rmind 		yyerror("rule nesting limit reached");
    579       1.1     rmind 	}
    580      1.18     rmind 	current_group[++rule_nesting_level] = rl;
    581      1.18     rmind }
    582       1.1     rmind 
    583      1.18     rmind void
    584      1.18     rmind npfctl_build_group_end(void)
    585      1.18     rmind {
    586      1.46     rmind 	nl_rule_t *parent, *group;
    587      1.46     rmind 
    588      1.18     rmind 	assert(rule_nesting_level > 0);
    589      1.46     rmind 	parent = current_group[rule_nesting_level - 1];
    590      1.46     rmind 	group = current_group[rule_nesting_level];
    591      1.18     rmind 	current_group[rule_nesting_level--] = NULL;
    592      1.46     rmind 
    593      1.46     rmind 	/* Note: if the parent is NULL, then it is a global rule. */
    594      1.46     rmind 	npf_rule_insert(npf_conf, parent, group);
    595       1.1     rmind }
    596       1.1     rmind 
    597       1.1     rmind /*
    598      1.26     rmind  * npfctl_build_rule: create a rule, build byte-code from filter options,
    599      1.18     rmind  * if any, and insert into the ruleset of current group, or set the rule.
    600       1.1     rmind  */
    601       1.1     rmind void
    602      1.28     rmind npfctl_build_rule(uint32_t attr, const char *ifname, sa_family_t family,
    603      1.27     rmind     const opt_proto_t *op, const filt_opts_t *fopts,
    604      1.27     rmind     const char *pcap_filter, const char *rproc)
    605       1.1     rmind {
    606       1.1     rmind 	nl_rule_t *rl;
    607       1.1     rmind 
    608      1.19     rmind 	attr |= (npf_conf ? 0 : NPF_RULE_DYNAMIC);
    609      1.21     rmind 
    610      1.28     rmind 	rl = npf_rule_create(NULL, attr, ifname);
    611      1.27     rmind 	if (pcap_filter) {
    612      1.27     rmind 		npfctl_build_pcap(rl, pcap_filter);
    613      1.27     rmind 	} else {
    614      1.27     rmind 		npfctl_build_code(rl, family, op, fopts);
    615      1.27     rmind 	}
    616      1.27     rmind 
    617      1.18     rmind 	if (rproc) {
    618      1.18     rmind 		npf_rule_setproc(rl, rproc);
    619      1.18     rmind 	}
    620      1.18     rmind 
    621      1.18     rmind 	if (npf_conf) {
    622      1.18     rmind 		nl_rule_t *cg = current_group[rule_nesting_level];
    623      1.18     rmind 
    624      1.18     rmind 		if (rproc && !npf_rproc_exists_p(npf_conf, rproc)) {
    625      1.18     rmind 			yyerror("rule procedure '%s' is not defined", rproc);
    626      1.18     rmind 		}
    627      1.18     rmind 		assert(cg != NULL);
    628      1.18     rmind 		npf_rule_setprio(rl, NPF_PRI_LAST);
    629      1.18     rmind 		npf_rule_insert(npf_conf, cg, rl);
    630      1.18     rmind 	} else {
    631      1.18     rmind 		/* We have parsed a single rule - set it. */
    632      1.18     rmind 		the_rule = rl;
    633       1.1     rmind 	}
    634       1.1     rmind }
    635       1.1     rmind 
    636       1.1     rmind /*
    637      1.14     rmind  * npfctl_build_nat: create a single NAT policy of a specified
    638      1.13     rmind  * type with a given filter options.
    639      1.13     rmind  */
    640      1.36     rmind static nl_nat_t *
    641      1.36     rmind npfctl_build_nat(int type, const char *ifname, const addr_port_t *ap,
    642      1.49     rmind     const opt_proto_t *op, const filt_opts_t *fopts, unsigned flags)
    643      1.13     rmind {
    644      1.44     rmind 	const opt_proto_t def_op = { .op_proto = -1, .op_opts = NULL };
    645      1.47     rmind 	fam_addr_mask_t *am;
    646      1.47     rmind 	sa_family_t family;
    647      1.13     rmind 	in_port_t port;
    648      1.13     rmind 	nl_nat_t *nat;
    649      1.47     rmind 	unsigned tid;
    650      1.13     rmind 
    651      1.35     rmind 	if (ap->ap_portrange) {
    652      1.45     rmind 		/*
    653      1.45     rmind 		 * The port forwarding case.  In such case, there has to
    654      1.45     rmind 		 * be a single port used for translation; we keep the port
    655      1.45     rmind 		 * translation on, but disable the port map.
    656      1.45     rmind 		 */
    657      1.35     rmind 		port = npfctl_get_singleport(ap->ap_portrange);
    658      1.47     rmind 		flags = (flags & ~NPF_NAT_PORTMAP) | NPF_NAT_PORTS;
    659      1.35     rmind 	} else {
    660      1.13     rmind 		port = 0;
    661      1.13     rmind 	}
    662      1.44     rmind 	if (!op) {
    663      1.44     rmind 		op = &def_op;
    664      1.44     rmind 	}
    665      1.13     rmind 
    666      1.47     rmind 	nat = npf_nat_create(type, flags, ifname);
    667      1.47     rmind 
    668      1.47     rmind 	switch (npfvar_get_type(ap->ap_netaddr, 0)) {
    669      1.47     rmind 	case NPFVAR_FAM:
    670      1.47     rmind 		/* Translation address. */
    671      1.47     rmind 		am = npfctl_get_singlefam(ap->ap_netaddr);
    672      1.47     rmind 		family = am->fam_family;
    673      1.47     rmind 		npf_nat_setaddr(nat, family, &am->fam_addr, am->fam_mask);
    674      1.47     rmind 		break;
    675      1.47     rmind 	case NPFVAR_TABLE:
    676      1.47     rmind 		/* Translation table. */
    677      1.47     rmind 		family = AF_UNSPEC;
    678      1.47     rmind 		tid = npfctl_get_singletable(ap->ap_netaddr);
    679      1.47     rmind 		npf_nat_settable(nat, tid);
    680      1.47     rmind 		break;
    681      1.47     rmind 	default:
    682      1.47     rmind 		yyerror("map must have a valid translation address");
    683      1.47     rmind 		abort();
    684      1.47     rmind 	}
    685      1.47     rmind 	npf_nat_setport(nat, port);
    686      1.47     rmind 	npfctl_build_code(nat, family, op, fopts);
    687      1.36     rmind 	return nat;
    688      1.13     rmind }
    689      1.13     rmind 
    690      1.49     rmind static void
    691      1.49     rmind npfctl_dnat_check(const addr_port_t *ap, const unsigned algo)
    692      1.49     rmind {
    693      1.49     rmind 	int type = npfvar_get_type(ap->ap_netaddr, 0);
    694      1.49     rmind 	fam_addr_mask_t *am;
    695      1.49     rmind 
    696      1.49     rmind 	switch (algo) {
    697      1.49     rmind 	case NPF_ALGO_NETMAP:
    698      1.49     rmind 		if (type == NPFVAR_FAM) {
    699      1.49     rmind 			break;
    700      1.49     rmind 		}
    701      1.49     rmind 		yyerror("translation address using NETMAP must be "
    702      1.49     rmind 		    "a network and not a dynamic pool");
    703      1.49     rmind 		break;
    704      1.49     rmind 	case NPF_ALGO_IPHASH:
    705      1.49     rmind 	case NPF_ALGO_RR:
    706      1.49     rmind 	case NPF_ALGO_NONE:
    707      1.49     rmind 		if (type != NPFVAR_FAM) {
    708      1.49     rmind 			break;
    709      1.49     rmind 		}
    710      1.49     rmind 		am = npfctl_get_singlefam(ap->ap_netaddr);
    711      1.49     rmind 		if (am->fam_mask == NPF_NO_NETMASK) {
    712      1.49     rmind 			break;
    713      1.49     rmind 		}
    714      1.49     rmind 		yyerror("translation address, given the specified algorithm, "
    715      1.49     rmind 		    "must be a pool or a single address");
    716      1.49     rmind 		break;
    717      1.49     rmind 	default:
    718      1.49     rmind 		yyerror("invalid algorithm specified for dynamic NAT");
    719      1.49     rmind 	}
    720      1.49     rmind }
    721      1.49     rmind 
    722      1.13     rmind /*
    723      1.14     rmind  * npfctl_build_natseg: validate and create NAT policies.
    724       1.1     rmind  */
    725       1.1     rmind void
    726      1.45     rmind npfctl_build_natseg(int sd, int type, unsigned mflags, const char *ifname,
    727      1.44     rmind     const addr_port_t *ap1, const addr_port_t *ap2, const opt_proto_t *op,
    728      1.47     rmind     const filt_opts_t *fopts, unsigned algo)
    729       1.1     rmind {
    730      1.36     rmind 	fam_addr_mask_t *am1 = NULL, *am2 = NULL;
    731      1.36     rmind 	nl_nat_t *nt1 = NULL, *nt2 = NULL;
    732       1.7     rmind 	filt_opts_t imfopts;
    733      1.36     rmind 	uint16_t adj = 0;
    734      1.47     rmind 	unsigned flags;
    735      1.13     rmind 	bool binat;
    736       1.1     rmind 
    737      1.28     rmind 	assert(ifname != NULL);
    738       1.7     rmind 
    739      1.13     rmind 	/*
    740      1.47     rmind 	 * Validate that mapping has the translation address(es) set.
    741      1.47     rmind 	 */
    742      1.47     rmind 	if ((type & NPF_NATIN) != 0 && ap1->ap_netaddr == NULL) {
    743      1.47     rmind 		yyerror("inbound network segment is not specified");
    744      1.47     rmind 	}
    745      1.47     rmind 	if ((type & NPF_NATOUT) != 0 && ap2->ap_netaddr == NULL) {
    746      1.47     rmind 		yyerror("outbound network segment is not specified");
    747      1.47     rmind 	}
    748      1.47     rmind 
    749      1.47     rmind 	/*
    750      1.13     rmind 	 * Bi-directional NAT is a combination of inbound NAT and outbound
    751      1.35     rmind 	 * NAT policies with the translation segments inverted respectively.
    752      1.13     rmind 	 */
    753      1.13     rmind 	binat = (NPF_NATIN | NPF_NATOUT) == type;
    754       1.7     rmind 
    755      1.35     rmind 	switch (sd) {
    756      1.35     rmind 	case NPFCTL_NAT_DYNAMIC:
    757      1.35     rmind 		/*
    758      1.47     rmind 		 * Dynamic NAT: stateful translation -- traditional NAPT
    759      1.47     rmind 		 * is expected.  Unless it is bi-directional NAT, perform
    760      1.47     rmind 		 * the port mapping.
    761      1.35     rmind 		 */
    762      1.35     rmind 		flags = !binat ? (NPF_NAT_PORTS | NPF_NAT_PORTMAP) : 0;
    763      1.49     rmind 		if (type & NPF_NATIN) {
    764      1.49     rmind 			npfctl_dnat_check(ap1, algo);
    765      1.49     rmind 		}
    766      1.49     rmind 		if (type & NPF_NATOUT) {
    767      1.49     rmind 			npfctl_dnat_check(ap2, algo);
    768      1.47     rmind 		}
    769      1.35     rmind 		break;
    770      1.35     rmind 	case NPFCTL_NAT_STATIC:
    771      1.47     rmind 		/*
    772      1.47     rmind 		 * Static NAT: stateless translation.
    773      1.47     rmind 		 */
    774      1.35     rmind 		flags = NPF_NAT_STATIC;
    775      1.47     rmind 
    776      1.47     rmind 		/* Note: translation address/network cannot be a table. */
    777      1.47     rmind 		am1 = npfctl_get_singlefam(ap1->ap_netaddr);
    778      1.47     rmind 		am2 = npfctl_get_singlefam(ap2->ap_netaddr);
    779      1.47     rmind 
    780      1.47     rmind 		/* Validate the algorithm. */
    781      1.47     rmind 		switch (algo) {
    782      1.47     rmind 		case NPF_ALGO_NPT66:
    783      1.47     rmind 			if (am1->fam_mask != am2->fam_mask) {
    784      1.47     rmind 				yyerror("asymmetric NPTv6 is not supported");
    785      1.47     rmind 			}
    786      1.47     rmind 			adj = npfctl_npt66_calcadj(am1->fam_mask,
    787      1.47     rmind 			    &am1->fam_addr, &am2->fam_addr);
    788      1.47     rmind 			break;
    789      1.47     rmind 		case NPF_ALGO_NETMAP:
    790      1.47     rmind 			if (am1->fam_mask != am2->fam_mask) {
    791      1.47     rmind 				yyerror("net-to-net mapping using the "
    792      1.47     rmind 				    "NETMAP algorithm must be 1:1");
    793      1.47     rmind 			}
    794      1.47     rmind 			break;
    795      1.47     rmind 		case NPF_ALGO_NONE:
    796      1.47     rmind 			if (am1->fam_mask != NPF_NO_NETMASK ||
    797      1.47     rmind 			    am2->fam_mask != NPF_NO_NETMASK) {
    798      1.47     rmind 				yyerror("static net-to-net translation "
    799      1.47     rmind 				    "must have an algorithm specified");
    800      1.47     rmind 			}
    801      1.47     rmind 			break;
    802      1.47     rmind 		default:
    803      1.47     rmind 			yyerror("invalid algorithm specified for static NAT");
    804      1.47     rmind 		}
    805      1.35     rmind 		break;
    806      1.35     rmind 	default:
    807      1.35     rmind 		abort();
    808      1.35     rmind 	}
    809      1.35     rmind 
    810       1.7     rmind 	/*
    811      1.45     rmind 	 * Apply the flag modifications.
    812      1.45     rmind 	 */
    813      1.45     rmind 	if (mflags & NPF_NAT_PORTS) {
    814      1.45     rmind 		flags &= ~(NPF_NAT_PORTS | NPF_NAT_PORTMAP);
    815      1.45     rmind 	}
    816      1.45     rmind 
    817      1.45     rmind 	/*
    818      1.13     rmind 	 * If the filter criteria is not specified explicitly, apply implicit
    819      1.14     rmind 	 * filtering according to the given network segments.
    820      1.13     rmind 	 *
    821      1.13     rmind 	 * Note: filled below, depending on the type.
    822       1.7     rmind 	 */
    823      1.14     rmind 	if (__predict_true(!fopts)) {
    824       1.7     rmind 		fopts = &imfopts;
    825       1.1     rmind 	}
    826       1.1     rmind 
    827      1.13     rmind 	if (type & NPF_NATIN) {
    828      1.13     rmind 		memset(&imfopts, 0, sizeof(filt_opts_t));
    829      1.13     rmind 		memcpy(&imfopts.fo_to, ap2, sizeof(addr_port_t));
    830      1.44     rmind 		nt1 = npfctl_build_nat(NPF_NATIN, ifname, ap1, op, fopts, flags);
    831      1.13     rmind 	}
    832      1.13     rmind 	if (type & NPF_NATOUT) {
    833      1.13     rmind 		memset(&imfopts, 0, sizeof(filt_opts_t));
    834      1.13     rmind 		memcpy(&imfopts.fo_from, ap1, sizeof(addr_port_t));
    835      1.44     rmind 		nt2 = npfctl_build_nat(NPF_NATOUT, ifname, ap2, op, fopts, flags);
    836      1.36     rmind 	}
    837      1.36     rmind 
    838      1.49     rmind 	switch (algo) {
    839      1.49     rmind 	case NPF_ALGO_NONE:
    840      1.49     rmind 		break;
    841      1.49     rmind 	case NPF_ALGO_NPT66:
    842      1.47     rmind 		/*
    843      1.47     rmind 		 * NPTv6 is a special case using special adjustment value.
    844      1.47     rmind 		 * It is always bidirectional NAT.
    845      1.47     rmind 		 */
    846      1.47     rmind 		assert(nt1 && nt2);
    847      1.36     rmind 		npf_nat_setnpt66(nt1, ~adj);
    848      1.36     rmind 		npf_nat_setnpt66(nt2, adj);
    849      1.49     rmind 		break;
    850      1.49     rmind 	default:
    851      1.47     rmind 		/*
    852      1.47     rmind 		 * Set the algorithm.
    853      1.47     rmind 		 */
    854      1.47     rmind 		if (nt1) {
    855      1.47     rmind 			npf_nat_setalgo(nt1, algo);
    856      1.47     rmind 		}
    857      1.47     rmind 		if (nt2) {
    858      1.47     rmind 			npf_nat_setalgo(nt2, algo);
    859      1.47     rmind 		}
    860       1.1     rmind 	}
    861      1.46     rmind 
    862      1.46     rmind 	if (nt1) {
    863      1.49     rmind 		npf_rule_setprio(nt1, NPF_PRI_LAST);
    864      1.49     rmind 		npf_nat_insert(npf_conf, nt1);
    865      1.46     rmind 	}
    866      1.46     rmind 	if (nt2) {
    867      1.49     rmind 		npf_rule_setprio(nt2, NPF_PRI_LAST);
    868      1.49     rmind 		npf_nat_insert(npf_conf, nt2);
    869      1.46     rmind 	}
    870       1.1     rmind }
    871       1.1     rmind 
    872       1.1     rmind /*
    873       1.1     rmind  * npfctl_fill_table: fill NPF table with entries from a specified file.
    874       1.1     rmind  */
    875       1.1     rmind static void
    876      1.11     rmind npfctl_fill_table(nl_table_t *tl, u_int type, const char *fname)
    877       1.1     rmind {
    878       1.1     rmind 	char *buf = NULL;
    879       1.1     rmind 	int l = 0;
    880       1.1     rmind 	FILE *fp;
    881       1.1     rmind 	size_t n;
    882       1.1     rmind 
    883       1.1     rmind 	fp = fopen(fname, "r");
    884       1.1     rmind 	if (fp == NULL) {
    885       1.1     rmind 		err(EXIT_FAILURE, "open '%s'", fname);
    886       1.1     rmind 	}
    887       1.1     rmind 	while (l++, getline(&buf, &n, fp) != -1) {
    888      1.11     rmind 		fam_addr_mask_t fam;
    889      1.11     rmind 		int alen;
    890       1.1     rmind 
    891       1.1     rmind 		if (*buf == '\n' || *buf == '#') {
    892       1.1     rmind 			continue;
    893       1.1     rmind 		}
    894      1.11     rmind 
    895      1.11     rmind 		if (!npfctl_parse_cidr(buf, &fam, &alen)) {
    896      1.11     rmind 			errx(EXIT_FAILURE,
    897      1.11     rmind 			    "%s:%d: invalid table entry", fname, l);
    898      1.11     rmind 		}
    899      1.47     rmind 		if (type != NPF_TABLE_LPM && fam.fam_mask != NPF_NO_NETMASK) {
    900      1.33     rmind 			errx(EXIT_FAILURE, "%s:%d: mask used with the "
    901      1.47     rmind 			    "table type other than \"lpm\"", fname, l);
    902       1.1     rmind 		}
    903       1.1     rmind 
    904      1.46     rmind 		npf_table_add_entry(tl, fam.fam_family,
    905      1.46     rmind 		    &fam.fam_addr, fam.fam_mask);
    906      1.33     rmind 	}
    907      1.46     rmind 	free(buf);
    908       1.1     rmind }
    909       1.1     rmind 
    910       1.1     rmind /*
    911       1.1     rmind  * npfctl_build_table: create an NPF table, add to the configuration and,
    912       1.1     rmind  * if required, fill with contents from a file.
    913       1.1     rmind  */
    914       1.1     rmind void
    915      1.29     rmind npfctl_build_table(const char *tname, u_int type, const char *fname)
    916       1.1     rmind {
    917       1.1     rmind 	nl_table_t *tl;
    918       1.1     rmind 
    919      1.43     rmind 	tl = npf_table_create(tname, npfctl_tid_counter++, type);
    920       1.1     rmind 	assert(tl != NULL);
    921       1.1     rmind 
    922       1.1     rmind 	if (fname) {
    923      1.11     rmind 		npfctl_fill_table(tl, type, fname);
    924      1.47     rmind 	} else if (type == NPF_TABLE_CONST) {
    925      1.47     rmind 		yyerror("table type 'const' must be loaded from a file");
    926       1.1     rmind 	}
    927      1.46     rmind 
    928      1.46     rmind 	if (npf_table_insert(npf_conf, tl)) {
    929      1.46     rmind 		yyerror("table '%s' is already defined", tname);
    930      1.46     rmind 	}
    931       1.1     rmind }
    932      1.23  christos 
    933      1.47     rmind /*
    934      1.47     rmind  * npfctl_ifnet_table: get a variable with ifaddr-table; auto-create
    935      1.47     rmind  * the table on first reference.
    936      1.47     rmind  */
    937      1.43     rmind npfvar_t *
    938      1.43     rmind npfctl_ifnet_table(const char *ifname)
    939      1.43     rmind {
    940      1.43     rmind 	char tname[NPF_TABLE_MAXNAMELEN];
    941      1.43     rmind 	nl_table_t *tl;
    942      1.43     rmind 	u_int tid;
    943      1.43     rmind 
    944      1.47     rmind 	snprintf(tname, sizeof(tname), NPF_IFNET_TABLE_PREF "%s", ifname);
    945      1.43     rmind 
    946      1.43     rmind 	tid = npfctl_table_getid(tname);
    947      1.43     rmind 	if (tid == (unsigned)-1) {
    948      1.43     rmind 		tid = npfctl_tid_counter++;
    949      1.47     rmind 		tl = npf_table_create(tname, tid, NPF_TABLE_IFADDR);
    950      1.43     rmind 		(void)npf_table_insert(npf_conf, tl);
    951      1.43     rmind 	}
    952      1.43     rmind 	return npfvar_create_element(NPFVAR_TABLE, &tid, sizeof(u_int));
    953      1.43     rmind }
    954      1.43     rmind 
    955      1.23  christos /*
    956      1.25     rmind  * npfctl_build_alg: create an NPF application level gateway and add it
    957      1.23  christos  * to the configuration.
    958      1.23  christos  */
    959      1.23  christos void
    960      1.23  christos npfctl_build_alg(const char *al_name)
    961      1.23  christos {
    962      1.49     rmind 	if (npf_alg_load(npf_conf, al_name) != 0) {
    963      1.49     rmind 		yyerror("ALG '%s' is already loaded", al_name);
    964      1.49     rmind 	}
    965      1.49     rmind }
    966      1.49     rmind 
    967      1.49     rmind void
    968      1.49     rmind npfctl_setparam(const char *name, int val)
    969      1.49     rmind {
    970      1.49     rmind 	if (strcmp(name, "bpf.jit") == 0) {
    971      1.49     rmind 		npfctl_bpfjit(val != 0);
    972      1.50     rmind 		return;
    973      1.49     rmind 	}
    974      1.49     rmind 	if (npf_param_set(npf_conf, name, val) != 0) {
    975      1.49     rmind 		yyerror("invalid parameter `%s` or its value", name);
    976      1.23  christos 	}
    977      1.23  christos }
    978      1.27     rmind 
    979      1.27     rmind static void
    980      1.27     rmind npfctl_dump_bpf(struct bpf_program *bf)
    981      1.27     rmind {
    982      1.27     rmind 	if (npf_debug) {
    983      1.27     rmind 		extern char *yytext;
    984      1.27     rmind 		extern int yylineno;
    985      1.27     rmind 
    986      1.27     rmind 		int rule_line = yylineno - (int)(*yytext == '\n');
    987      1.27     rmind 		printf("\nRULE AT LINE %d\n", rule_line);
    988      1.27     rmind 		bpf_dump(bf, 0);
    989      1.27     rmind 	}
    990      1.27     rmind }
    991