Home | History | Annotate | Line # | Download | only in npfctl
npf_build.c revision 1.6
      1  1.6  christos /*	$NetBSD: npf_build.c,v 1.6 2012/02/26 21:50:05 christos Exp $	*/
      2  1.1     rmind 
      3  1.1     rmind /*-
      4  1.1     rmind  * Copyright (c) 2011-2012 The NetBSD Foundation, Inc.
      5  1.1     rmind  * All rights reserved.
      6  1.1     rmind  *
      7  1.1     rmind  * This material is based upon work partially supported by The
      8  1.1     rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9  1.1     rmind  *
     10  1.1     rmind  * Redistribution and use in source and binary forms, with or without
     11  1.1     rmind  * modification, are permitted provided that the following conditions
     12  1.1     rmind  * are met:
     13  1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     14  1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     15  1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     rmind  *    documentation and/or other materials provided with the distribution.
     18  1.1     rmind  *
     19  1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1     rmind  */
     31  1.1     rmind 
     32  1.1     rmind /*
     33  1.1     rmind  * npfctl(8) building of the configuration.
     34  1.1     rmind  */
     35  1.1     rmind 
     36  1.1     rmind #include <sys/cdefs.h>
     37  1.6  christos __RCSID("$NetBSD: npf_build.c,v 1.6 2012/02/26 21:50:05 christos Exp $");
     38  1.1     rmind 
     39  1.1     rmind #include <sys/types.h>
     40  1.1     rmind #include <sys/ioctl.h>
     41  1.1     rmind 
     42  1.1     rmind #include <stdlib.h>
     43  1.1     rmind #include <inttypes.h>
     44  1.1     rmind #include <string.h>
     45  1.1     rmind #include <assert.h>
     46  1.1     rmind #include <err.h>
     47  1.1     rmind 
     48  1.1     rmind #include "npfctl.h"
     49  1.1     rmind 
     50  1.1     rmind static nl_config_t *		npf_conf = NULL;
     51  1.1     rmind static nl_rule_t *		current_group = NULL;
     52  1.1     rmind static bool			npf_debug = false;
     53  1.1     rmind static bool			defgroup_set = false;
     54  1.1     rmind 
     55  1.1     rmind void
     56  1.1     rmind npfctl_config_init(bool debug)
     57  1.1     rmind {
     58  1.1     rmind 
     59  1.1     rmind 	npf_conf = npf_config_create();
     60  1.1     rmind 	if (npf_conf == NULL) {
     61  1.1     rmind 		errx(EXIT_FAILURE, "npf_config_create failed");
     62  1.1     rmind 	}
     63  1.1     rmind 	npf_debug = debug;
     64  1.1     rmind }
     65  1.1     rmind 
     66  1.1     rmind int
     67  1.1     rmind npfctl_config_send(int fd)
     68  1.1     rmind {
     69  1.1     rmind 	int error;
     70  1.1     rmind 
     71  1.1     rmind 	if (!fd) {
     72  1.1     rmind 		_npf_config_setsubmit(npf_conf, "./npf.plist");
     73  1.1     rmind 	}
     74  1.1     rmind 	if (!defgroup_set) {
     75  1.1     rmind 		errx(EXIT_FAILURE, "default group was not defined");
     76  1.1     rmind 	}
     77  1.1     rmind 	error = npf_config_submit(npf_conf, fd);
     78  1.3     rmind 	if (error) {
     79  1.3     rmind 		nl_error_t ne;
     80  1.3     rmind 		_npf_config_error(npf_conf, &ne);
     81  1.3     rmind 		npfctl_print_error(&ne);
     82  1.3     rmind 	}
     83  1.1     rmind 	npf_config_destroy(npf_conf);
     84  1.1     rmind 	return error;
     85  1.1     rmind }
     86  1.1     rmind 
     87  1.1     rmind bool
     88  1.1     rmind npfctl_table_exists_p(const char *id)
     89  1.1     rmind {
     90  1.1     rmind 	return npf_table_exists_p(npf_conf, atoi(id));
     91  1.1     rmind }
     92  1.1     rmind 
     93  1.1     rmind static in_port_t *
     94  1.1     rmind npfctl_get_singleport(const npfvar_t *vp)
     95  1.1     rmind {
     96  1.1     rmind 	port_range_t *pr;
     97  1.1     rmind 
     98  1.1     rmind 	if (npfvar_get_count(vp) > 1) {
     99  1.1     rmind 		yyerror("multiple ports are not valid");
    100  1.1     rmind 	}
    101  1.1     rmind 	pr = npfvar_get_data(vp, NPFVAR_PORT_RANGE, 0);
    102  1.1     rmind 	if (pr->pr_start != pr->pr_end) {
    103  1.1     rmind 		yyerror("port range is not valid");
    104  1.1     rmind 	}
    105  1.1     rmind 	return &pr->pr_start;
    106  1.1     rmind }
    107  1.1     rmind 
    108  1.1     rmind static fam_addr_mask_t *
    109  1.1     rmind npfctl_get_singlefam(const npfvar_t *vp)
    110  1.1     rmind {
    111  1.1     rmind 	if (npfvar_get_count(vp) > 1) {
    112  1.1     rmind 		yyerror("multiple addresses are not valid");
    113  1.1     rmind 	}
    114  1.1     rmind 	return npfvar_get_data(vp, NPFVAR_FAM, 0);
    115  1.1     rmind }
    116  1.1     rmind 
    117  1.1     rmind static void
    118  1.1     rmind npfctl_build_fam(nc_ctx_t *nc, sa_family_t family,
    119  1.1     rmind     fam_addr_mask_t *fam, int opts)
    120  1.1     rmind {
    121  1.1     rmind 	/*
    122  1.1     rmind 	 * If family is specified, address does not match it and the
    123  1.1     rmind 	 * address is extracted from the interface, then simply ignore.
    124  1.1     rmind 	 * Otherwise, address of invalid family was passed manually.
    125  1.1     rmind 	 */
    126  1.1     rmind 	if (family != AF_UNSPEC && family != fam->fam_family) {
    127  1.1     rmind 		if (!fam->fam_interface) {
    128  1.1     rmind 			yyerror("specified address is not of the required "
    129  1.1     rmind 			    "family %d", family);
    130  1.1     rmind 		}
    131  1.1     rmind 		return;
    132  1.1     rmind 	}
    133  1.1     rmind 
    134  1.1     rmind 	/*
    135  1.1     rmind 	 * Optimise 0.0.0.0/0 case to be NOP.  Otherwise, address with
    136  1.1     rmind 	 * zero mask would never match and therefore is not valid.
    137  1.1     rmind 	 */
    138  1.1     rmind 	if (fam->fam_mask == 0) {
    139  1.1     rmind 		npf_addr_t zero;
    140  1.1     rmind 		memset(&zero, 0, sizeof(npf_addr_t));
    141  1.1     rmind 		if (memcmp(&fam->fam_addr, &zero, sizeof(npf_addr_t))) {
    142  1.1     rmind 			yyerror("filter criterion would never match");
    143  1.1     rmind 		}
    144  1.1     rmind 		return;
    145  1.1     rmind 	}
    146  1.1     rmind 
    147  1.1     rmind 	switch (fam->fam_family) {
    148  1.1     rmind 	case AF_INET:
    149  1.1     rmind 		npfctl_gennc_v4cidr(nc, opts,
    150  1.1     rmind 		    &fam->fam_addr, fam->fam_mask);
    151  1.1     rmind 		break;
    152  1.1     rmind 	case AF_INET6:
    153  1.1     rmind 		npfctl_gennc_v6cidr(nc, opts,
    154  1.1     rmind 		    &fam->fam_addr, fam->fam_mask);
    155  1.1     rmind 		break;
    156  1.1     rmind 	default:
    157  1.1     rmind 		yyerror("family %d is not supported", fam->fam_family);
    158  1.1     rmind 	}
    159  1.1     rmind }
    160  1.1     rmind 
    161  1.1     rmind static void
    162  1.1     rmind npfctl_build_vars(nc_ctx_t *nc, sa_family_t family, npfvar_t *vars, int opts)
    163  1.1     rmind {
    164  1.6  christos 	const int type = npfvar_get_type(vars, 0);
    165  1.1     rmind 	size_t i;
    166  1.1     rmind 
    167  1.1     rmind 	npfctl_ncgen_group(nc);
    168  1.1     rmind 	for (i = 0; i < npfvar_get_count(vars); i++) {
    169  1.1     rmind 		void *data = npfvar_get_data(vars, type, i);
    170  1.1     rmind 		assert(data != NULL);
    171  1.1     rmind 
    172  1.1     rmind 		switch (type) {
    173  1.1     rmind 		case NPFVAR_FAM: {
    174  1.1     rmind 			fam_addr_mask_t *fam = data;
    175  1.1     rmind 			npfctl_build_fam(nc, family, fam, opts);
    176  1.1     rmind 			break;
    177  1.1     rmind 		}
    178  1.1     rmind 		case NPFVAR_PORT_RANGE: {
    179  1.1     rmind 			port_range_t *pr = data;
    180  1.1     rmind 			if (opts & NC_MATCH_TCP) {
    181  1.1     rmind 				npfctl_gennc_ports(nc, opts & ~NC_MATCH_UDP,
    182  1.1     rmind 				    pr->pr_start, pr->pr_end);
    183  1.1     rmind 			}
    184  1.1     rmind 			if (opts & NC_MATCH_UDP) {
    185  1.1     rmind 				npfctl_gennc_ports(nc, opts & ~NC_MATCH_TCP,
    186  1.1     rmind 				    pr->pr_start, pr->pr_end);
    187  1.1     rmind 			}
    188  1.1     rmind 			break;
    189  1.1     rmind 		}
    190  1.1     rmind 		case NPFVAR_TABLE: {
    191  1.1     rmind 			u_int tid = atoi(data);
    192  1.1     rmind 			npfctl_gennc_tbl(nc, opts, tid);
    193  1.1     rmind 			break;
    194  1.1     rmind 		}
    195  1.1     rmind 		default:
    196  1.1     rmind 			assert(false);
    197  1.1     rmind 		}
    198  1.1     rmind 	}
    199  1.1     rmind 	npfctl_ncgen_endgroup(nc);
    200  1.1     rmind }
    201  1.1     rmind 
    202  1.1     rmind static int
    203  1.1     rmind npfctl_build_proto(nc_ctx_t *nc, const opt_proto_t *op)
    204  1.1     rmind {
    205  1.1     rmind 	const npfvar_t *popts = op->op_opts;
    206  1.1     rmind 	int pflag = 0;
    207  1.1     rmind 
    208  1.1     rmind 	switch (op->op_proto) {
    209  1.1     rmind 	case IPPROTO_TCP:
    210  1.1     rmind 		pflag = NC_MATCH_TCP;
    211  1.1     rmind 		if (!popts) {
    212  1.1     rmind 			break;
    213  1.1     rmind 		}
    214  1.1     rmind 		assert(npfvar_get_count(popts) == 2);
    215  1.1     rmind 
    216  1.1     rmind 		/* Build TCP flags block (optional). */
    217  1.1     rmind 		uint8_t *tf, *tf_mask;
    218  1.1     rmind 
    219  1.1     rmind 		tf = npfvar_get_data(popts, NPFVAR_TCPFLAG, 0);
    220  1.1     rmind 		tf_mask = npfvar_get_data(popts, NPFVAR_TCPFLAG, 1);
    221  1.1     rmind 		npfctl_gennc_tcpfl(nc, *tf, *tf_mask);
    222  1.1     rmind 		break;
    223  1.1     rmind 	case IPPROTO_UDP:
    224  1.1     rmind 		pflag = NC_MATCH_UDP;
    225  1.1     rmind 		break;
    226  1.1     rmind 	case IPPROTO_ICMP:
    227  1.1     rmind 		/*
    228  1.1     rmind 		 * Build ICMP block.
    229  1.1     rmind 		 */
    230  1.1     rmind 		assert(npfvar_get_count(popts) == 2);
    231  1.1     rmind 
    232  1.1     rmind 		int *icmp_type, *icmp_code;
    233  1.1     rmind 		icmp_type = npfvar_get_data(popts, NPFVAR_ICMP, 0);
    234  1.1     rmind 		icmp_code = npfvar_get_data(popts, NPFVAR_ICMP, 1);
    235  1.1     rmind 		npfctl_gennc_icmp(nc, *icmp_type, *icmp_code);
    236  1.1     rmind 		break;
    237  1.1     rmind 	case -1:
    238  1.1     rmind 		pflag = NC_MATCH_TCP | NC_MATCH_UDP;
    239  1.1     rmind 		break;
    240  1.1     rmind 	default:
    241  1.1     rmind 		yyerror("protocol %d is not supported", op->op_proto);
    242  1.1     rmind 	}
    243  1.1     rmind 	return pflag;
    244  1.1     rmind }
    245  1.1     rmind 
    246  1.1     rmind static bool
    247  1.1     rmind npfctl_build_ncode(nl_rule_t *rl, sa_family_t family, const opt_proto_t *op,
    248  1.1     rmind     const filt_opts_t *fopts, bool invert)
    249  1.1     rmind {
    250  1.1     rmind 	nc_ctx_t *nc;
    251  1.1     rmind 	void *code;
    252  1.1     rmind 	size_t len;
    253  1.1     rmind 
    254  1.1     rmind 	if (family == AF_UNSPEC && op->op_proto == -1 &&
    255  1.1     rmind 	    op->op_opts == NULL && !fopts->fo_from && !fopts->fo_to &&
    256  1.1     rmind 	    !fopts->fo_from_port_range && !fopts->fo_to_port_range)
    257  1.1     rmind 		return false;
    258  1.1     rmind 
    259  1.1     rmind 	int srcflag = NC_MATCH_SRC;
    260  1.1     rmind 	int dstflag = NC_MATCH_DST;
    261  1.1     rmind 
    262  1.1     rmind 	if (invert) {
    263  1.1     rmind 		srcflag = NC_MATCH_DST;
    264  1.1     rmind 		dstflag = NC_MATCH_SRC;
    265  1.1     rmind 	}
    266  1.1     rmind 
    267  1.1     rmind 	nc = npfctl_ncgen_create();
    268  1.1     rmind 
    269  1.1     rmind 	/* Build IP address blocks. */
    270  1.1     rmind 	npfctl_build_vars(nc, family, fopts->fo_from, srcflag);
    271  1.1     rmind 	npfctl_build_vars(nc, family, fopts->fo_to, dstflag);
    272  1.1     rmind 
    273  1.1     rmind 	/* Build layer 4 protocol blocks. */
    274  1.1     rmind 	int pflag = npfctl_build_proto(nc, op);
    275  1.1     rmind 
    276  1.1     rmind 	/* Build port-range blocks. */
    277  1.1     rmind 	if (fopts->fo_from_port_range) {
    278  1.1     rmind 		npfctl_build_vars(nc, family, fopts->fo_from_port_range,
    279  1.1     rmind 		    srcflag | pflag);
    280  1.1     rmind 	}
    281  1.1     rmind 	if (fopts->fo_to_port_range) {
    282  1.1     rmind 		npfctl_build_vars(nc, family, fopts->fo_to_port_range,
    283  1.1     rmind 		    dstflag | pflag);
    284  1.1     rmind 	}
    285  1.1     rmind 
    286  1.1     rmind 	/*
    287  1.1     rmind 	 * Complete n-code (destroys the context) and pass to the rule.
    288  1.1     rmind 	 */
    289  1.1     rmind 	code = npfctl_ncgen_complete(nc, &len);
    290  1.1     rmind 	if (npf_debug) {
    291  1.1     rmind 		extern int yylineno;
    292  1.1     rmind 		printf("RULE AT LINE %d\n", yylineno - 1);
    293  1.1     rmind 		npfctl_ncgen_print(code, len);
    294  1.1     rmind 	}
    295  1.1     rmind 	if (npf_rule_setcode(rl, NPF_CODE_NCODE, code, len) == -1) {
    296  1.1     rmind 		errx(EXIT_FAILURE, "npf_rule_setcode failed");
    297  1.1     rmind 	}
    298  1.1     rmind 	free(code);
    299  1.1     rmind 	return true;
    300  1.1     rmind }
    301  1.1     rmind 
    302  1.4     rmind static void
    303  1.4     rmind npfctl_build_rpcall(nl_rproc_t *rp, const char *name, npfvar_t *args)
    304  1.4     rmind {
    305  1.4     rmind 	/*
    306  1.4     rmind 	 * XXX/TODO: Hardcoded for the first release.  However,
    307  1.4     rmind 	 * rule procedures will become fully dynamic modules.
    308  1.4     rmind 	 */
    309  1.4     rmind 
    310  1.4     rmind 	bool log = false, norm = false;
    311  1.4     rmind 	bool rnd = false, no_df = false;
    312  1.4     rmind 	int minttl = 0, maxmss = 0;
    313  1.4     rmind 
    314  1.4     rmind 	if (strcmp(name, "log") == 0) {
    315  1.4     rmind 		log = true;
    316  1.4     rmind 	} else if (strcmp(name, "normalise") == 0) {
    317  1.4     rmind 		norm = true;
    318  1.4     rmind 	} else {
    319  1.4     rmind 		yyerror("unknown rule procedure '%s'", name);
    320  1.4     rmind 	}
    321  1.4     rmind 
    322  1.4     rmind 	for (size_t i = 0; i < npfvar_get_count(args); i++) {
    323  1.4     rmind 		module_arg_t *arg;
    324  1.4     rmind 		const char *aval;
    325  1.4     rmind 
    326  1.4     rmind 		arg = npfvar_get_data(args, NPFVAR_MODULE_ARG, i);
    327  1.4     rmind 		aval = arg->ma_name;
    328  1.4     rmind 
    329  1.4     rmind 		if (log) {
    330  1.4     rmind 			u_int if_idx = npfctl_find_ifindex(aval);
    331  1.4     rmind 			if (!if_idx) {
    332  1.4     rmind 				yyerror("unknown interface '%s'", aval);
    333  1.4     rmind 			}
    334  1.4     rmind 			_npf_rproc_setlog(rp, if_idx);
    335  1.4     rmind 			return;
    336  1.4     rmind 		}
    337  1.4     rmind 
    338  1.6  christos 		const int type = npfvar_get_type(arg->ma_opts, 0);
    339  1.4     rmind 		if (type != -1 && type != NPFVAR_NUM) {
    340  1.4     rmind 			yyerror("option '%s' is not numeric", aval);
    341  1.4     rmind 		}
    342  1.4     rmind 		unsigned long *opt;
    343  1.4     rmind 
    344  1.4     rmind 		if (strcmp(aval, "random-id") == 0) {
    345  1.4     rmind 			rnd = true;
    346  1.4     rmind 		} else if (strcmp(aval, "min-ttl") == 0) {
    347  1.4     rmind 			opt = npfvar_get_data(arg->ma_opts, NPFVAR_NUM, 0);
    348  1.4     rmind 			minttl = *opt;
    349  1.4     rmind 		} else if (strcmp(aval, "max-mss") == 0) {
    350  1.4     rmind 			opt = npfvar_get_data(arg->ma_opts, NPFVAR_NUM, 0);
    351  1.4     rmind 			maxmss = *opt;
    352  1.4     rmind 		} else if (strcmp(aval, "no-df") == 0) {
    353  1.4     rmind 			no_df = true;
    354  1.4     rmind 		} else {
    355  1.4     rmind 			yyerror("unknown argument '%s'", aval);
    356  1.4     rmind 		}
    357  1.4     rmind 	}
    358  1.4     rmind 	assert(norm == true);
    359  1.4     rmind 	_npf_rproc_setnorm(rp, rnd, no_df, minttl, maxmss);
    360  1.4     rmind }
    361  1.4     rmind 
    362  1.1     rmind /*
    363  1.1     rmind  * npfctl_build_rproc: create and insert a rule procedure.
    364  1.1     rmind  */
    365  1.1     rmind void
    366  1.4     rmind npfctl_build_rproc(const char *name, npfvar_t *procs)
    367  1.1     rmind {
    368  1.1     rmind 	nl_rproc_t *rp;
    369  1.4     rmind 	size_t i;
    370  1.1     rmind 
    371  1.1     rmind 	rp = npf_rproc_create(name);
    372  1.1     rmind 	if (rp == NULL) {
    373  1.1     rmind 		errx(EXIT_FAILURE, "npf_rproc_create failed");
    374  1.1     rmind 	}
    375  1.1     rmind 	npf_rproc_insert(npf_conf, rp);
    376  1.4     rmind 
    377  1.4     rmind 	for (i = 0; i < npfvar_get_count(procs); i++) {
    378  1.4     rmind 		proc_op_t *po = npfvar_get_data(procs, NPFVAR_PROC_OP, i);
    379  1.4     rmind 		npfctl_build_rpcall(rp, po->po_name, po->po_opts);
    380  1.4     rmind 	}
    381  1.1     rmind }
    382  1.1     rmind 
    383  1.1     rmind /*
    384  1.1     rmind  * npfctl_build_group: create a group, insert into the global ruleset
    385  1.1     rmind  * and update the current group pointer.
    386  1.1     rmind  */
    387  1.1     rmind void
    388  1.1     rmind npfctl_build_group(const char *name, int attr, u_int if_idx)
    389  1.1     rmind {
    390  1.1     rmind 	const int attr_di = (NPF_RULE_IN | NPF_RULE_OUT);
    391  1.1     rmind 	nl_rule_t *rl;
    392  1.1     rmind 
    393  1.1     rmind 	if (attr & NPF_RULE_DEFAULT) {
    394  1.1     rmind 		if (defgroup_set) {
    395  1.1     rmind 			yyerror("multiple default groups are not valid");
    396  1.1     rmind 		}
    397  1.1     rmind 		defgroup_set = true;
    398  1.1     rmind 		attr |= attr_di;
    399  1.1     rmind 
    400  1.1     rmind 	} else if ((attr & attr_di) == 0) {
    401  1.1     rmind 		attr |= attr_di;
    402  1.1     rmind 	}
    403  1.1     rmind 
    404  1.5     rmind 	rl = npf_rule_create(name, attr | NPF_RULE_FINAL, if_idx);
    405  1.1     rmind 	npf_rule_insert(npf_conf, NULL, rl, NPF_PRI_NEXT);
    406  1.1     rmind 	current_group = rl;
    407  1.1     rmind }
    408  1.1     rmind 
    409  1.1     rmind /*
    410  1.1     rmind  * npfctl_build_rule: create a rule, build n-code from filter options,
    411  1.1     rmind  * if any, and insert into the ruleset of current group.
    412  1.1     rmind  */
    413  1.1     rmind void
    414  1.1     rmind npfctl_build_rule(int attr, u_int if_idx, sa_family_t family,
    415  1.1     rmind     const opt_proto_t *op, const filt_opts_t *fopts, const char *rproc)
    416  1.1     rmind {
    417  1.1     rmind 	nl_rule_t *rl;
    418  1.1     rmind 
    419  1.1     rmind 	rl = npf_rule_create(NULL, attr, if_idx);
    420  1.1     rmind 	npfctl_build_ncode(rl, family, op, fopts, false);
    421  1.1     rmind 	if (rproc && npf_rule_setproc(npf_conf, rl, rproc) != 0) {
    422  1.1     rmind 		yyerror("rule procedure '%s' is not defined", rproc);
    423  1.1     rmind 	}
    424  1.1     rmind 	assert(current_group != NULL);
    425  1.1     rmind 	npf_rule_insert(npf_conf, current_group, rl, NPF_PRI_NEXT);
    426  1.1     rmind }
    427  1.1     rmind 
    428  1.1     rmind /*
    429  1.1     rmind  * npfctl_build_nat: create a NAT policy of a specified type with a
    430  1.1     rmind  * given filter options.
    431  1.1     rmind  */
    432  1.1     rmind void
    433  1.1     rmind npfctl_build_nat(int type, u_int if_idx, const filt_opts_t *fopts,
    434  1.1     rmind     npfvar_t *var1, npfvar_t *var2)
    435  1.1     rmind {
    436  1.1     rmind 	opt_proto_t op = { .op_proto = -1, .op_opts = NULL };
    437  1.1     rmind 	nl_nat_t *nat;
    438  1.1     rmind 	fam_addr_mask_t *ai;
    439  1.1     rmind 
    440  1.1     rmind 	assert(type != 0 && if_idx != 0);
    441  1.1     rmind 	assert(fopts != NULL && var1 != NULL);
    442  1.1     rmind 
    443  1.1     rmind 	ai = npfctl_get_singlefam(var1);
    444  1.1     rmind 	assert(ai != NULL);
    445  1.1     rmind 	if (ai->fam_family != AF_INET) {
    446  1.1     rmind 		yyerror("IPv6 NAT is not supported");
    447  1.1     rmind 	}
    448  1.1     rmind 
    449  1.1     rmind 	switch (type) {
    450  1.1     rmind 	case NPFCTL_RDR: {
    451  1.1     rmind 		/*
    452  1.1     rmind 		 * Redirection: an inbound NAT with a specific port.
    453  1.1     rmind 		 */
    454  1.1     rmind 		in_port_t *port = npfctl_get_singleport(var2);
    455  1.1     rmind 		nat = npf_nat_create(NPF_NATIN, NPF_NAT_PORTS,
    456  1.1     rmind 		    if_idx, &ai->fam_addr, ai->fam_family, *port);
    457  1.1     rmind 		break;
    458  1.1     rmind 	}
    459  1.1     rmind 	case NPFCTL_BINAT: {
    460  1.1     rmind 		/*
    461  1.1     rmind 		 * Bi-directional NAT: a combination of inbound NAT and
    462  1.1     rmind 		 * outbound NAT policies.  Note that the translation address
    463  1.1     rmind 		 * is local IP and filter criteria is inverted accordingly.
    464  1.1     rmind 		 */
    465  1.1     rmind 		fam_addr_mask_t *tai = npfctl_get_singlefam(var2);
    466  1.1     rmind 		assert(tai != NULL);
    467  1.1     rmind 		if (ai->fam_family != AF_INET) {
    468  1.1     rmind 			yyerror("IPv6 NAT is not supported");
    469  1.1     rmind 		}
    470  1.1     rmind 		nat = npf_nat_create(NPF_NATIN, 0, if_idx,
    471  1.1     rmind 		    &tai->fam_addr, tai->fam_family, 0);
    472  1.1     rmind 		npfctl_build_ncode(nat, AF_INET, &op, fopts, true);
    473  1.1     rmind 		npf_nat_insert(npf_conf, nat, NPF_PRI_NEXT);
    474  1.1     rmind 		/* FALLTHROUGH */
    475  1.1     rmind 	}
    476  1.1     rmind 	case NPFCTL_NAT: {
    477  1.1     rmind 		/*
    478  1.1     rmind 		 * Traditional NAPT: an outbound NAT policy with port.
    479  1.1     rmind 		 * If this is another hald for bi-directional NAT, then
    480  1.1     rmind 		 * no port translation with mapping.
    481  1.1     rmind 		 */
    482  1.1     rmind 		nat = npf_nat_create(NPF_NATOUT, type == NPFCTL_NAT ?
    483  1.1     rmind 		    (NPF_NAT_PORTS | NPF_NAT_PORTMAP) : 0,
    484  1.1     rmind 		    if_idx, &ai->fam_addr, ai->fam_family, 0);
    485  1.1     rmind 		break;
    486  1.1     rmind 	}
    487  1.1     rmind 	default:
    488  1.1     rmind 		assert(false);
    489  1.1     rmind 	}
    490  1.1     rmind 	npfctl_build_ncode(nat, AF_INET, &op, fopts, false);
    491  1.1     rmind 	npf_nat_insert(npf_conf, nat, NPF_PRI_NEXT);
    492  1.1     rmind }
    493  1.1     rmind 
    494  1.1     rmind /*
    495  1.1     rmind  * npfctl_fill_table: fill NPF table with entries from a specified file.
    496  1.1     rmind  */
    497  1.1     rmind static void
    498  1.1     rmind npfctl_fill_table(nl_table_t *tl, const char *fname)
    499  1.1     rmind {
    500  1.1     rmind 	char *buf = NULL;
    501  1.1     rmind 	int l = 0;
    502  1.1     rmind 	FILE *fp;
    503  1.1     rmind 	size_t n;
    504  1.1     rmind 
    505  1.1     rmind 	fp = fopen(fname, "r");
    506  1.1     rmind 	if (fp == NULL) {
    507  1.1     rmind 		err(EXIT_FAILURE, "open '%s'", fname);
    508  1.1     rmind 	}
    509  1.1     rmind 	while (l++, getline(&buf, &n, fp) != -1) {
    510  1.1     rmind 		fam_addr_mask_t *fam;
    511  1.1     rmind 
    512  1.1     rmind 		if (*buf == '\n' || *buf == '#') {
    513  1.1     rmind 			continue;
    514  1.1     rmind 		}
    515  1.1     rmind 		fam = npfctl_parse_cidr(buf);
    516  1.1     rmind 		if (fam == NULL) {
    517  1.1     rmind 			errx(EXIT_FAILURE, "%s:%d: invalid table entry",
    518  1.1     rmind 			    fname, l);
    519  1.1     rmind 		}
    520  1.1     rmind 
    521  1.1     rmind 		/* Create and add a table entry. */
    522  1.1     rmind 		npf_table_add_entry(tl, &fam->fam_addr, fam->fam_mask);
    523  1.1     rmind 	}
    524  1.1     rmind 	if (buf != NULL) {
    525  1.1     rmind 		free(buf);
    526  1.1     rmind 	}
    527  1.1     rmind }
    528  1.1     rmind 
    529  1.1     rmind /*
    530  1.1     rmind  * npfctl_build_table: create an NPF table, add to the configuration and,
    531  1.1     rmind  * if required, fill with contents from a file.
    532  1.1     rmind  */
    533  1.1     rmind void
    534  1.1     rmind npfctl_build_table(const char *tid, u_int type, const char *fname)
    535  1.1     rmind {
    536  1.1     rmind 	nl_table_t *tl;
    537  1.1     rmind 	u_int id;
    538  1.1     rmind 
    539  1.1     rmind 	id = atoi(tid);
    540  1.1     rmind 	tl = npf_table_create(id, type);
    541  1.1     rmind 	assert(tl != NULL);
    542  1.1     rmind 
    543  1.1     rmind 	if (npf_table_insert(npf_conf, tl)) {
    544  1.1     rmind 		errx(EXIT_FAILURE, "table '%d' is already defined\n", id);
    545  1.1     rmind 	}
    546  1.1     rmind 
    547  1.1     rmind 	if (fname) {
    548  1.1     rmind 		npfctl_fill_table(tl, fname);
    549  1.1     rmind 	}
    550  1.1     rmind }
    551