Home | History | Annotate | Line # | Download | only in npfctl
npf_build.c revision 1.1
      1  1.1  rmind /*	$NetBSD: npf_build.c,v 1.1 2012/01/08 21:34:21 rmind 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.1  rmind __RCSID("$NetBSD: npf_build.c,v 1.1 2012/01/08 21:34:21 rmind 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.1  rmind 	npf_config_destroy(npf_conf);
     79  1.1  rmind 	return error;
     80  1.1  rmind }
     81  1.1  rmind 
     82  1.1  rmind int
     83  1.1  rmind npfctl_config_flush(int fd)
     84  1.1  rmind {
     85  1.1  rmind 	int ret;
     86  1.1  rmind 
     87  1.1  rmind 	/* Pass empty configuration to flush. */
     88  1.1  rmind 	npfctl_config_init(false);
     89  1.1  rmind 	defgroup_set = true;
     90  1.1  rmind 	ret = npfctl_config_send(fd);
     91  1.1  rmind 	if (ret) {
     92  1.1  rmind 		return ret;
     93  1.1  rmind 	}
     94  1.1  rmind 	return npf_sessions_send(fd, NULL);
     95  1.1  rmind }
     96  1.1  rmind 
     97  1.1  rmind bool
     98  1.1  rmind npfctl_table_exists_p(const char *id)
     99  1.1  rmind {
    100  1.1  rmind 	return npf_table_exists_p(npf_conf, atoi(id));
    101  1.1  rmind }
    102  1.1  rmind 
    103  1.1  rmind static in_port_t *
    104  1.1  rmind npfctl_get_singleport(const npfvar_t *vp)
    105  1.1  rmind {
    106  1.1  rmind 	port_range_t *pr;
    107  1.1  rmind 
    108  1.1  rmind 	if (npfvar_get_count(vp) > 1) {
    109  1.1  rmind 		yyerror("multiple ports are not valid");
    110  1.1  rmind 	}
    111  1.1  rmind 	pr = npfvar_get_data(vp, NPFVAR_PORT_RANGE, 0);
    112  1.1  rmind 	if (pr->pr_start != pr->pr_end) {
    113  1.1  rmind 		yyerror("port range is not valid");
    114  1.1  rmind 	}
    115  1.1  rmind 	return &pr->pr_start;
    116  1.1  rmind }
    117  1.1  rmind 
    118  1.1  rmind static fam_addr_mask_t *
    119  1.1  rmind npfctl_get_singlefam(const npfvar_t *vp)
    120  1.1  rmind {
    121  1.1  rmind 	if (npfvar_get_count(vp) > 1) {
    122  1.1  rmind 		yyerror("multiple addresses are not valid");
    123  1.1  rmind 	}
    124  1.1  rmind 	return npfvar_get_data(vp, NPFVAR_FAM, 0);
    125  1.1  rmind }
    126  1.1  rmind 
    127  1.1  rmind static void
    128  1.1  rmind npfctl_build_fam(nc_ctx_t *nc, sa_family_t family,
    129  1.1  rmind     fam_addr_mask_t *fam, int opts)
    130  1.1  rmind {
    131  1.1  rmind 	/*
    132  1.1  rmind 	 * If family is specified, address does not match it and the
    133  1.1  rmind 	 * address is extracted from the interface, then simply ignore.
    134  1.1  rmind 	 * Otherwise, address of invalid family was passed manually.
    135  1.1  rmind 	 */
    136  1.1  rmind 	if (family != AF_UNSPEC && family != fam->fam_family) {
    137  1.1  rmind 		if (!fam->fam_interface) {
    138  1.1  rmind 			yyerror("specified address is not of the required "
    139  1.1  rmind 			    "family %d", family);
    140  1.1  rmind 		}
    141  1.1  rmind 		return;
    142  1.1  rmind 	}
    143  1.1  rmind 
    144  1.1  rmind 	/*
    145  1.1  rmind 	 * Optimise 0.0.0.0/0 case to be NOP.  Otherwise, address with
    146  1.1  rmind 	 * zero mask would never match and therefore is not valid.
    147  1.1  rmind 	 */
    148  1.1  rmind 	if (fam->fam_mask == 0) {
    149  1.1  rmind 		npf_addr_t zero;
    150  1.1  rmind 		memset(&zero, 0, sizeof(npf_addr_t));
    151  1.1  rmind 		if (memcmp(&fam->fam_addr, &zero, sizeof(npf_addr_t))) {
    152  1.1  rmind 			yyerror("filter criterion would never match");
    153  1.1  rmind 		}
    154  1.1  rmind 		return;
    155  1.1  rmind 	}
    156  1.1  rmind 
    157  1.1  rmind 	switch (fam->fam_family) {
    158  1.1  rmind 	case AF_INET:
    159  1.1  rmind 		npfctl_gennc_v4cidr(nc, opts,
    160  1.1  rmind 		    &fam->fam_addr, fam->fam_mask);
    161  1.1  rmind 		break;
    162  1.1  rmind 	case AF_INET6:
    163  1.1  rmind 		npfctl_gennc_v6cidr(nc, opts,
    164  1.1  rmind 		    &fam->fam_addr, fam->fam_mask);
    165  1.1  rmind 		break;
    166  1.1  rmind 	default:
    167  1.1  rmind 		yyerror("family %d is not supported", fam->fam_family);
    168  1.1  rmind 	}
    169  1.1  rmind }
    170  1.1  rmind 
    171  1.1  rmind static void
    172  1.1  rmind npfctl_build_vars(nc_ctx_t *nc, sa_family_t family, npfvar_t *vars, int opts)
    173  1.1  rmind {
    174  1.1  rmind 	const int type = npfvar_get_type(vars);
    175  1.1  rmind 	size_t i;
    176  1.1  rmind 
    177  1.1  rmind 	npfctl_ncgen_group(nc);
    178  1.1  rmind 	for (i = 0; i < npfvar_get_count(vars); i++) {
    179  1.1  rmind 		void *data = npfvar_get_data(vars, type, i);
    180  1.1  rmind 		assert(data != NULL);
    181  1.1  rmind 
    182  1.1  rmind 		switch (type) {
    183  1.1  rmind 		case NPFVAR_FAM: {
    184  1.1  rmind 			fam_addr_mask_t *fam = data;
    185  1.1  rmind 			npfctl_build_fam(nc, family, fam, opts);
    186  1.1  rmind 			break;
    187  1.1  rmind 		}
    188  1.1  rmind 		case NPFVAR_PORT_RANGE: {
    189  1.1  rmind 			port_range_t *pr = data;
    190  1.1  rmind 			if (opts & NC_MATCH_TCP) {
    191  1.1  rmind 				npfctl_gennc_ports(nc, opts & ~NC_MATCH_UDP,
    192  1.1  rmind 				    pr->pr_start, pr->pr_end);
    193  1.1  rmind 			}
    194  1.1  rmind 			if (opts & NC_MATCH_UDP) {
    195  1.1  rmind 				npfctl_gennc_ports(nc, opts & ~NC_MATCH_TCP,
    196  1.1  rmind 				    pr->pr_start, pr->pr_end);
    197  1.1  rmind 			}
    198  1.1  rmind 			break;
    199  1.1  rmind 		}
    200  1.1  rmind 		case NPFVAR_TABLE: {
    201  1.1  rmind 			u_int tid = atoi(data);
    202  1.1  rmind 			npfctl_gennc_tbl(nc, opts, tid);
    203  1.1  rmind 			break;
    204  1.1  rmind 		}
    205  1.1  rmind 		default:
    206  1.1  rmind 			assert(false);
    207  1.1  rmind 		}
    208  1.1  rmind 	}
    209  1.1  rmind 	npfctl_ncgen_endgroup(nc);
    210  1.1  rmind }
    211  1.1  rmind 
    212  1.1  rmind static int
    213  1.1  rmind npfctl_build_proto(nc_ctx_t *nc, const opt_proto_t *op)
    214  1.1  rmind {
    215  1.1  rmind 	const npfvar_t *popts = op->op_opts;
    216  1.1  rmind 	int pflag = 0;
    217  1.1  rmind 
    218  1.1  rmind 	switch (op->op_proto) {
    219  1.1  rmind 	case IPPROTO_TCP:
    220  1.1  rmind 		pflag = NC_MATCH_TCP;
    221  1.1  rmind 		if (!popts) {
    222  1.1  rmind 			break;
    223  1.1  rmind 		}
    224  1.1  rmind 		assert(npfvar_get_count(popts) == 2);
    225  1.1  rmind 
    226  1.1  rmind 		/* Build TCP flags block (optional). */
    227  1.1  rmind 		uint8_t *tf, *tf_mask;
    228  1.1  rmind 
    229  1.1  rmind 		tf = npfvar_get_data(popts, NPFVAR_TCPFLAG, 0);
    230  1.1  rmind 		tf_mask = npfvar_get_data(popts, NPFVAR_TCPFLAG, 1);
    231  1.1  rmind 		npfctl_gennc_tcpfl(nc, *tf, *tf_mask);
    232  1.1  rmind 		break;
    233  1.1  rmind 	case IPPROTO_UDP:
    234  1.1  rmind 		pflag = NC_MATCH_UDP;
    235  1.1  rmind 		break;
    236  1.1  rmind 	case IPPROTO_ICMP:
    237  1.1  rmind 		/*
    238  1.1  rmind 		 * Build ICMP block.
    239  1.1  rmind 		 */
    240  1.1  rmind 		assert(npfvar_get_count(popts) == 2);
    241  1.1  rmind 
    242  1.1  rmind 		int *icmp_type, *icmp_code;
    243  1.1  rmind 		icmp_type = npfvar_get_data(popts, NPFVAR_ICMP, 0);
    244  1.1  rmind 		icmp_code = npfvar_get_data(popts, NPFVAR_ICMP, 1);
    245  1.1  rmind 		npfctl_gennc_icmp(nc, *icmp_type, *icmp_code);
    246  1.1  rmind 		break;
    247  1.1  rmind 	case -1:
    248  1.1  rmind 		pflag = NC_MATCH_TCP | NC_MATCH_UDP;
    249  1.1  rmind 		break;
    250  1.1  rmind 	default:
    251  1.1  rmind 		yyerror("protocol %d is not supported", op->op_proto);
    252  1.1  rmind 	}
    253  1.1  rmind 	return pflag;
    254  1.1  rmind }
    255  1.1  rmind 
    256  1.1  rmind static bool
    257  1.1  rmind npfctl_build_ncode(nl_rule_t *rl, sa_family_t family, const opt_proto_t *op,
    258  1.1  rmind     const filt_opts_t *fopts, bool invert)
    259  1.1  rmind {
    260  1.1  rmind 	nc_ctx_t *nc;
    261  1.1  rmind 	void *code;
    262  1.1  rmind 	size_t len;
    263  1.1  rmind 
    264  1.1  rmind 	if (family == AF_UNSPEC && op->op_proto == -1 &&
    265  1.1  rmind 	    op->op_opts == NULL && !fopts->fo_from && !fopts->fo_to &&
    266  1.1  rmind 	    !fopts->fo_from_port_range && !fopts->fo_to_port_range)
    267  1.1  rmind 		return false;
    268  1.1  rmind 
    269  1.1  rmind 	int srcflag = NC_MATCH_SRC;
    270  1.1  rmind 	int dstflag = NC_MATCH_DST;
    271  1.1  rmind 
    272  1.1  rmind 	if (invert) {
    273  1.1  rmind 		srcflag = NC_MATCH_DST;
    274  1.1  rmind 		dstflag = NC_MATCH_SRC;
    275  1.1  rmind 	}
    276  1.1  rmind 
    277  1.1  rmind 	nc = npfctl_ncgen_create();
    278  1.1  rmind 
    279  1.1  rmind 	/* Build IP address blocks. */
    280  1.1  rmind 	npfctl_build_vars(nc, family, fopts->fo_from, srcflag);
    281  1.1  rmind 	npfctl_build_vars(nc, family, fopts->fo_to, dstflag);
    282  1.1  rmind 
    283  1.1  rmind 	/* Build layer 4 protocol blocks. */
    284  1.1  rmind 	int pflag = npfctl_build_proto(nc, op);
    285  1.1  rmind 
    286  1.1  rmind 	/* Build port-range blocks. */
    287  1.1  rmind 	if (fopts->fo_from_port_range) {
    288  1.1  rmind 		npfctl_build_vars(nc, family, fopts->fo_from_port_range,
    289  1.1  rmind 		    srcflag | pflag);
    290  1.1  rmind 	}
    291  1.1  rmind 	if (fopts->fo_to_port_range) {
    292  1.1  rmind 		npfctl_build_vars(nc, family, fopts->fo_to_port_range,
    293  1.1  rmind 		    dstflag | pflag);
    294  1.1  rmind 	}
    295  1.1  rmind 
    296  1.1  rmind 	/*
    297  1.1  rmind 	 * Complete n-code (destroys the context) and pass to the rule.
    298  1.1  rmind 	 */
    299  1.1  rmind 	code = npfctl_ncgen_complete(nc, &len);
    300  1.1  rmind 	if (npf_debug) {
    301  1.1  rmind 		extern int yylineno;
    302  1.1  rmind 		printf("RULE AT LINE %d\n", yylineno - 1);
    303  1.1  rmind 		npfctl_ncgen_print(code, len);
    304  1.1  rmind 	}
    305  1.1  rmind 	if (npf_rule_setcode(rl, NPF_CODE_NCODE, code, len) == -1) {
    306  1.1  rmind 		errx(EXIT_FAILURE, "npf_rule_setcode failed");
    307  1.1  rmind 	}
    308  1.1  rmind 	free(code);
    309  1.1  rmind 	return true;
    310  1.1  rmind }
    311  1.1  rmind 
    312  1.1  rmind /*
    313  1.1  rmind  * npfctl_build_rproc: create and insert a rule procedure.
    314  1.1  rmind  */
    315  1.1  rmind void
    316  1.1  rmind npfctl_build_rproc(const char *name, npfvar_t *var)
    317  1.1  rmind {
    318  1.1  rmind 	nl_rproc_t *rp;
    319  1.1  rmind 
    320  1.1  rmind 	rp = npf_rproc_create(name);
    321  1.1  rmind 	if (rp == NULL) {
    322  1.1  rmind 		errx(EXIT_FAILURE, "npf_rproc_create failed");
    323  1.1  rmind 	}
    324  1.1  rmind 	npf_rproc_insert(npf_conf, rp);
    325  1.1  rmind }
    326  1.1  rmind 
    327  1.1  rmind /*
    328  1.1  rmind  * npfctl_build_group: create a group, insert into the global ruleset
    329  1.1  rmind  * and update the current group pointer.
    330  1.1  rmind  */
    331  1.1  rmind void
    332  1.1  rmind npfctl_build_group(const char *name, int attr, u_int if_idx)
    333  1.1  rmind {
    334  1.1  rmind 	const int attr_di = (NPF_RULE_IN | NPF_RULE_OUT);
    335  1.1  rmind 	nl_rule_t *rl;
    336  1.1  rmind 
    337  1.1  rmind 	if (attr & NPF_RULE_DEFAULT) {
    338  1.1  rmind 		if (defgroup_set) {
    339  1.1  rmind 			yyerror("multiple default groups are not valid");
    340  1.1  rmind 		}
    341  1.1  rmind 		defgroup_set = true;
    342  1.1  rmind 		attr |= attr_di;
    343  1.1  rmind 
    344  1.1  rmind 	} else if ((attr & attr_di) == 0) {
    345  1.1  rmind 		attr |= attr_di;
    346  1.1  rmind 	}
    347  1.1  rmind 	attr |= (NPF_RULE_PASS | NPF_RULE_FINAL);
    348  1.1  rmind 
    349  1.1  rmind 	rl = npf_rule_create(name, attr, if_idx);
    350  1.1  rmind 	npf_rule_insert(npf_conf, NULL, rl, NPF_PRI_NEXT);
    351  1.1  rmind 	current_group = rl;
    352  1.1  rmind }
    353  1.1  rmind 
    354  1.1  rmind /*
    355  1.1  rmind  * npfctl_build_rule: create a rule, build n-code from filter options,
    356  1.1  rmind  * if any, and insert into the ruleset of current group.
    357  1.1  rmind  */
    358  1.1  rmind void
    359  1.1  rmind npfctl_build_rule(int attr, u_int if_idx, sa_family_t family,
    360  1.1  rmind     const opt_proto_t *op, const filt_opts_t *fopts, const char *rproc)
    361  1.1  rmind {
    362  1.1  rmind 	nl_rule_t *rl;
    363  1.1  rmind 
    364  1.1  rmind 	rl = npf_rule_create(NULL, attr, if_idx);
    365  1.1  rmind 	npfctl_build_ncode(rl, family, op, fopts, false);
    366  1.1  rmind 	if (rproc && npf_rule_setproc(npf_conf, rl, rproc) != 0) {
    367  1.1  rmind 		yyerror("rule procedure '%s' is not defined", rproc);
    368  1.1  rmind 	}
    369  1.1  rmind 	assert(current_group != NULL);
    370  1.1  rmind 	npf_rule_insert(npf_conf, current_group, rl, NPF_PRI_NEXT);
    371  1.1  rmind }
    372  1.1  rmind 
    373  1.1  rmind /*
    374  1.1  rmind  * npfctl_build_nat: create a NAT policy of a specified type with a
    375  1.1  rmind  * given filter options.
    376  1.1  rmind  */
    377  1.1  rmind void
    378  1.1  rmind npfctl_build_nat(int type, u_int if_idx, const filt_opts_t *fopts,
    379  1.1  rmind     npfvar_t *var1, npfvar_t *var2)
    380  1.1  rmind {
    381  1.1  rmind 	opt_proto_t op = { .op_proto = -1, .op_opts = NULL };
    382  1.1  rmind 	nl_nat_t *nat;
    383  1.1  rmind 	fam_addr_mask_t *ai;
    384  1.1  rmind 
    385  1.1  rmind 	assert(type != 0 && if_idx != 0);
    386  1.1  rmind 	assert(fopts != NULL && var1 != NULL);
    387  1.1  rmind 
    388  1.1  rmind 	ai = npfctl_get_singlefam(var1);
    389  1.1  rmind 	assert(ai != NULL);
    390  1.1  rmind 	if (ai->fam_family != AF_INET) {
    391  1.1  rmind 		yyerror("IPv6 NAT is not supported");
    392  1.1  rmind 	}
    393  1.1  rmind 
    394  1.1  rmind 	switch (type) {
    395  1.1  rmind 	case NPFCTL_RDR: {
    396  1.1  rmind 		/*
    397  1.1  rmind 		 * Redirection: an inbound NAT with a specific port.
    398  1.1  rmind 		 */
    399  1.1  rmind 		in_port_t *port = npfctl_get_singleport(var2);
    400  1.1  rmind 		nat = npf_nat_create(NPF_NATIN, NPF_NAT_PORTS,
    401  1.1  rmind 		    if_idx, &ai->fam_addr, ai->fam_family, *port);
    402  1.1  rmind 		break;
    403  1.1  rmind 	}
    404  1.1  rmind 	case NPFCTL_BINAT: {
    405  1.1  rmind 		/*
    406  1.1  rmind 		 * Bi-directional NAT: a combination of inbound NAT and
    407  1.1  rmind 		 * outbound NAT policies.  Note that the translation address
    408  1.1  rmind 		 * is local IP and filter criteria is inverted accordingly.
    409  1.1  rmind 		 */
    410  1.1  rmind 		fam_addr_mask_t *tai = npfctl_get_singlefam(var2);
    411  1.1  rmind 		assert(tai != NULL);
    412  1.1  rmind 		if (ai->fam_family != AF_INET) {
    413  1.1  rmind 			yyerror("IPv6 NAT is not supported");
    414  1.1  rmind 		}
    415  1.1  rmind 		nat = npf_nat_create(NPF_NATIN, 0, if_idx,
    416  1.1  rmind 		    &tai->fam_addr, tai->fam_family, 0);
    417  1.1  rmind 		npfctl_build_ncode(nat, AF_INET, &op, fopts, true);
    418  1.1  rmind 		npf_nat_insert(npf_conf, nat, NPF_PRI_NEXT);
    419  1.1  rmind 		/* FALLTHROUGH */
    420  1.1  rmind 	}
    421  1.1  rmind 	case NPFCTL_NAT: {
    422  1.1  rmind 		/*
    423  1.1  rmind 		 * Traditional NAPT: an outbound NAT policy with port.
    424  1.1  rmind 		 * If this is another hald for bi-directional NAT, then
    425  1.1  rmind 		 * no port translation with mapping.
    426  1.1  rmind 		 */
    427  1.1  rmind 		nat = npf_nat_create(NPF_NATOUT, type == NPFCTL_NAT ?
    428  1.1  rmind 		    (NPF_NAT_PORTS | NPF_NAT_PORTMAP) : 0,
    429  1.1  rmind 		    if_idx, &ai->fam_addr, ai->fam_family, 0);
    430  1.1  rmind 		break;
    431  1.1  rmind 	}
    432  1.1  rmind 	default:
    433  1.1  rmind 		assert(false);
    434  1.1  rmind 	}
    435  1.1  rmind 	npfctl_build_ncode(nat, AF_INET, &op, fopts, false);
    436  1.1  rmind 	npf_nat_insert(npf_conf, nat, NPF_PRI_NEXT);
    437  1.1  rmind }
    438  1.1  rmind 
    439  1.1  rmind /*
    440  1.1  rmind  * npfctl_fill_table: fill NPF table with entries from a specified file.
    441  1.1  rmind  */
    442  1.1  rmind static void
    443  1.1  rmind npfctl_fill_table(nl_table_t *tl, const char *fname)
    444  1.1  rmind {
    445  1.1  rmind 	char *buf = NULL;
    446  1.1  rmind 	int l = 0;
    447  1.1  rmind 	FILE *fp;
    448  1.1  rmind 	size_t n;
    449  1.1  rmind 
    450  1.1  rmind 	fp = fopen(fname, "r");
    451  1.1  rmind 	if (fp == NULL) {
    452  1.1  rmind 		err(EXIT_FAILURE, "open '%s'", fname);
    453  1.1  rmind 	}
    454  1.1  rmind 	while (l++, getline(&buf, &n, fp) != -1) {
    455  1.1  rmind 		fam_addr_mask_t *fam;
    456  1.1  rmind 
    457  1.1  rmind 		if (*buf == '\n' || *buf == '#') {
    458  1.1  rmind 			continue;
    459  1.1  rmind 		}
    460  1.1  rmind 		fam = npfctl_parse_cidr(buf);
    461  1.1  rmind 		if (fam == NULL) {
    462  1.1  rmind 			errx(EXIT_FAILURE, "%s:%d: invalid table entry",
    463  1.1  rmind 			    fname, l);
    464  1.1  rmind 		}
    465  1.1  rmind 
    466  1.1  rmind 		/* Create and add a table entry. */
    467  1.1  rmind 		npf_table_add_entry(tl, &fam->fam_addr, fam->fam_mask);
    468  1.1  rmind 	}
    469  1.1  rmind 	if (buf != NULL) {
    470  1.1  rmind 		free(buf);
    471  1.1  rmind 	}
    472  1.1  rmind }
    473  1.1  rmind 
    474  1.1  rmind /*
    475  1.1  rmind  * npfctl_build_table: create an NPF table, add to the configuration and,
    476  1.1  rmind  * if required, fill with contents from a file.
    477  1.1  rmind  */
    478  1.1  rmind void
    479  1.1  rmind npfctl_build_table(const char *tid, u_int type, const char *fname)
    480  1.1  rmind {
    481  1.1  rmind 	nl_table_t *tl;
    482  1.1  rmind 	u_int id;
    483  1.1  rmind 
    484  1.1  rmind 	id = atoi(tid);
    485  1.1  rmind 	tl = npf_table_create(id, type);
    486  1.1  rmind 	assert(tl != NULL);
    487  1.1  rmind 
    488  1.1  rmind 	if (npf_table_insert(npf_conf, tl)) {
    489  1.1  rmind 		errx(EXIT_FAILURE, "table '%d' is already defined\n", id);
    490  1.1  rmind 	}
    491  1.1  rmind 
    492  1.1  rmind 	if (fname) {
    493  1.1  rmind 		npfctl_fill_table(tl, fname);
    494  1.1  rmind 	}
    495  1.1  rmind }
    496