Home | History | Annotate | Line # | Download | only in npfctl
npf_build.c revision 1.24
      1  1.24     rmind /*	$NetBSD: npf_build.c,v 1.24 2013/05/19 20:45:34 rmind Exp $	*/
      2   1.1     rmind 
      3   1.1     rmind /*-
      4  1.18     rmind  * Copyright (c) 2011-2013 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.24     rmind __RCSID("$NetBSD: npf_build.c,v 1.24 2013/05/19 20:45:34 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.14     rmind #include <errno.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.18     rmind #define	MAX_RULE_NESTING	16
     51  1.18     rmind 
     52   1.1     rmind static nl_config_t *		npf_conf = NULL;
     53   1.1     rmind static bool			npf_debug = false;
     54  1.18     rmind static nl_rule_t *		the_rule = NULL;
     55  1.18     rmind 
     56  1.18     rmind static nl_rule_t *		current_group[MAX_RULE_NESTING];
     57  1.18     rmind static unsigned			rule_nesting_level = 0;
     58  1.18     rmind static nl_rule_t *		defgroup = NULL;
     59   1.1     rmind 
     60   1.1     rmind void
     61   1.1     rmind npfctl_config_init(bool debug)
     62   1.1     rmind {
     63   1.1     rmind 	npf_conf = npf_config_create();
     64   1.1     rmind 	if (npf_conf == NULL) {
     65   1.1     rmind 		errx(EXIT_FAILURE, "npf_config_create failed");
     66   1.1     rmind 	}
     67   1.1     rmind 	npf_debug = debug;
     68  1.18     rmind 	memset(current_group, 0, sizeof(current_group));
     69   1.1     rmind }
     70   1.1     rmind 
     71   1.1     rmind int
     72  1.13     rmind npfctl_config_send(int fd, const char *out)
     73   1.1     rmind {
     74   1.1     rmind 	int error;
     75   1.1     rmind 
     76  1.13     rmind 	if (out) {
     77  1.13     rmind 		_npf_config_setsubmit(npf_conf, out);
     78  1.13     rmind 		printf("\nSaving to %s\n", out);
     79   1.1     rmind 	}
     80  1.18     rmind 	if (!defgroup) {
     81   1.1     rmind 		errx(EXIT_FAILURE, "default group was not defined");
     82   1.1     rmind 	}
     83  1.18     rmind 	npf_rule_insert(npf_conf, NULL, defgroup);
     84   1.1     rmind 	error = npf_config_submit(npf_conf, fd);
     85   1.3     rmind 	if (error) {
     86   1.3     rmind 		nl_error_t ne;
     87   1.3     rmind 		_npf_config_error(npf_conf, &ne);
     88   1.3     rmind 		npfctl_print_error(&ne);
     89   1.3     rmind 	}
     90   1.1     rmind 	npf_config_destroy(npf_conf);
     91   1.1     rmind 	return error;
     92   1.1     rmind }
     93   1.1     rmind 
     94  1.16     rmind nl_config_t *
     95  1.16     rmind npfctl_config_ref(void)
     96  1.16     rmind {
     97  1.16     rmind 	return npf_conf;
     98  1.16     rmind }
     99  1.16     rmind 
    100  1.18     rmind nl_rule_t *
    101  1.18     rmind npfctl_rule_ref(void)
    102  1.18     rmind {
    103  1.18     rmind 	return the_rule;
    104  1.18     rmind }
    105  1.18     rmind 
    106  1.13     rmind unsigned long
    107  1.13     rmind npfctl_debug_addif(const char *ifname)
    108  1.13     rmind {
    109  1.13     rmind 	char tname[] = "npftest";
    110  1.13     rmind 	const size_t tnamelen = sizeof(tname) - 1;
    111  1.13     rmind 
    112  1.13     rmind 	if (!npf_debug || strncmp(ifname, tname, tnamelen) != 0) {
    113  1.13     rmind 		return 0;
    114  1.13     rmind 	}
    115  1.13     rmind 	struct ifaddrs ifa = {
    116  1.13     rmind 		.ifa_name = __UNCONST(ifname),
    117  1.13     rmind 		.ifa_flags = 0
    118  1.13     rmind 	};
    119  1.13     rmind 	unsigned long if_idx = atol(ifname + tnamelen) + 1;
    120  1.13     rmind 	_npf_debug_addif(npf_conf, &ifa, if_idx);
    121  1.13     rmind 	return if_idx;
    122  1.13     rmind }
    123  1.13     rmind 
    124   1.1     rmind bool
    125   1.1     rmind npfctl_table_exists_p(const char *id)
    126   1.1     rmind {
    127   1.1     rmind 	return npf_table_exists_p(npf_conf, atoi(id));
    128   1.1     rmind }
    129   1.1     rmind 
    130   1.7     rmind static in_port_t
    131   1.1     rmind npfctl_get_singleport(const npfvar_t *vp)
    132   1.1     rmind {
    133   1.1     rmind 	port_range_t *pr;
    134   1.7     rmind 	in_port_t *port;
    135   1.1     rmind 
    136   1.1     rmind 	if (npfvar_get_count(vp) > 1) {
    137   1.1     rmind 		yyerror("multiple ports are not valid");
    138   1.1     rmind 	}
    139   1.1     rmind 	pr = npfvar_get_data(vp, NPFVAR_PORT_RANGE, 0);
    140   1.1     rmind 	if (pr->pr_start != pr->pr_end) {
    141   1.1     rmind 		yyerror("port range is not valid");
    142   1.1     rmind 	}
    143   1.7     rmind 	port = &pr->pr_start;
    144   1.7     rmind 	return *port;
    145   1.1     rmind }
    146   1.1     rmind 
    147   1.1     rmind static fam_addr_mask_t *
    148   1.1     rmind npfctl_get_singlefam(const npfvar_t *vp)
    149   1.1     rmind {
    150   1.1     rmind 	if (npfvar_get_count(vp) > 1) {
    151   1.1     rmind 		yyerror("multiple addresses are not valid");
    152   1.1     rmind 	}
    153   1.1     rmind 	return npfvar_get_data(vp, NPFVAR_FAM, 0);
    154   1.1     rmind }
    155   1.1     rmind 
    156  1.10     rmind static bool
    157   1.1     rmind npfctl_build_fam(nc_ctx_t *nc, sa_family_t family,
    158   1.1     rmind     fam_addr_mask_t *fam, int opts)
    159   1.1     rmind {
    160   1.1     rmind 	/*
    161   1.1     rmind 	 * If family is specified, address does not match it and the
    162   1.1     rmind 	 * address is extracted from the interface, then simply ignore.
    163   1.1     rmind 	 * Otherwise, address of invalid family was passed manually.
    164   1.1     rmind 	 */
    165   1.1     rmind 	if (family != AF_UNSPEC && family != fam->fam_family) {
    166  1.15     rmind 		if (!fam->fam_ifindex) {
    167   1.1     rmind 			yyerror("specified address is not of the required "
    168   1.1     rmind 			    "family %d", family);
    169   1.1     rmind 		}
    170  1.10     rmind 		return false;
    171   1.1     rmind 	}
    172   1.1     rmind 
    173   1.1     rmind 	/*
    174   1.1     rmind 	 * Optimise 0.0.0.0/0 case to be NOP.  Otherwise, address with
    175   1.1     rmind 	 * zero mask would never match and therefore is not valid.
    176   1.1     rmind 	 */
    177   1.1     rmind 	if (fam->fam_mask == 0) {
    178   1.1     rmind 		npf_addr_t zero;
    179  1.10     rmind 
    180   1.1     rmind 		memset(&zero, 0, sizeof(npf_addr_t));
    181   1.1     rmind 		if (memcmp(&fam->fam_addr, &zero, sizeof(npf_addr_t))) {
    182   1.1     rmind 			yyerror("filter criterion would never match");
    183   1.1     rmind 		}
    184  1.10     rmind 		return false;
    185   1.1     rmind 	}
    186   1.1     rmind 
    187   1.1     rmind 	switch (fam->fam_family) {
    188   1.1     rmind 	case AF_INET:
    189   1.1     rmind 		npfctl_gennc_v4cidr(nc, opts,
    190   1.1     rmind 		    &fam->fam_addr, fam->fam_mask);
    191   1.1     rmind 		break;
    192   1.1     rmind 	case AF_INET6:
    193   1.1     rmind 		npfctl_gennc_v6cidr(nc, opts,
    194   1.1     rmind 		    &fam->fam_addr, fam->fam_mask);
    195   1.1     rmind 		break;
    196   1.1     rmind 	default:
    197   1.1     rmind 		yyerror("family %d is not supported", fam->fam_family);
    198   1.1     rmind 	}
    199  1.10     rmind 	return true;
    200   1.1     rmind }
    201   1.1     rmind 
    202   1.1     rmind static void
    203   1.1     rmind npfctl_build_vars(nc_ctx_t *nc, sa_family_t family, npfvar_t *vars, int opts)
    204   1.1     rmind {
    205   1.6  christos 	const int type = npfvar_get_type(vars, 0);
    206   1.1     rmind 	size_t i;
    207   1.1     rmind 
    208   1.1     rmind 	npfctl_ncgen_group(nc);
    209   1.1     rmind 	for (i = 0; i < npfvar_get_count(vars); i++) {
    210   1.1     rmind 		void *data = npfvar_get_data(vars, type, i);
    211   1.1     rmind 		assert(data != NULL);
    212   1.1     rmind 
    213   1.1     rmind 		switch (type) {
    214   1.1     rmind 		case NPFVAR_FAM: {
    215   1.1     rmind 			fam_addr_mask_t *fam = data;
    216   1.1     rmind 			npfctl_build_fam(nc, family, fam, opts);
    217   1.1     rmind 			break;
    218   1.1     rmind 		}
    219   1.1     rmind 		case NPFVAR_PORT_RANGE: {
    220   1.1     rmind 			port_range_t *pr = data;
    221   1.1     rmind 			if (opts & NC_MATCH_TCP) {
    222   1.1     rmind 				npfctl_gennc_ports(nc, opts & ~NC_MATCH_UDP,
    223   1.1     rmind 				    pr->pr_start, pr->pr_end);
    224   1.1     rmind 			}
    225   1.1     rmind 			if (opts & NC_MATCH_UDP) {
    226   1.1     rmind 				npfctl_gennc_ports(nc, opts & ~NC_MATCH_TCP,
    227   1.1     rmind 				    pr->pr_start, pr->pr_end);
    228   1.1     rmind 			}
    229   1.1     rmind 			break;
    230   1.1     rmind 		}
    231   1.1     rmind 		case NPFVAR_TABLE: {
    232   1.1     rmind 			u_int tid = atoi(data);
    233   1.1     rmind 			npfctl_gennc_tbl(nc, opts, tid);
    234   1.1     rmind 			break;
    235   1.1     rmind 		}
    236   1.1     rmind 		default:
    237   1.1     rmind 			assert(false);
    238   1.1     rmind 		}
    239   1.1     rmind 	}
    240   1.1     rmind 	npfctl_ncgen_endgroup(nc);
    241   1.1     rmind }
    242   1.1     rmind 
    243   1.1     rmind static int
    244  1.10     rmind npfctl_build_proto(nc_ctx_t *nc, sa_family_t family,
    245  1.20     rmind     const opt_proto_t *op, bool noaddrs, bool noports)
    246   1.1     rmind {
    247   1.1     rmind 	const npfvar_t *popts = op->op_opts;
    248  1.10     rmind 	const int proto = op->op_proto;
    249   1.1     rmind 	int pflag = 0;
    250   1.1     rmind 
    251  1.10     rmind 	switch (proto) {
    252   1.1     rmind 	case IPPROTO_TCP:
    253   1.1     rmind 		pflag = NC_MATCH_TCP;
    254   1.1     rmind 		if (!popts) {
    255   1.1     rmind 			break;
    256   1.1     rmind 		}
    257   1.1     rmind 		assert(npfvar_get_count(popts) == 2);
    258   1.1     rmind 
    259   1.1     rmind 		/* Build TCP flags block (optional). */
    260   1.1     rmind 		uint8_t *tf, *tf_mask;
    261   1.1     rmind 
    262   1.1     rmind 		tf = npfvar_get_data(popts, NPFVAR_TCPFLAG, 0);
    263   1.1     rmind 		tf_mask = npfvar_get_data(popts, NPFVAR_TCPFLAG, 1);
    264   1.1     rmind 		npfctl_gennc_tcpfl(nc, *tf, *tf_mask);
    265  1.20     rmind 		noports = false;
    266   1.1     rmind 		break;
    267   1.1     rmind 	case IPPROTO_UDP:
    268   1.1     rmind 		pflag = NC_MATCH_UDP;
    269   1.1     rmind 		break;
    270   1.1     rmind 	case IPPROTO_ICMP:
    271   1.1     rmind 		/*
    272   1.1     rmind 		 * Build ICMP block.
    273   1.1     rmind 		 */
    274  1.20     rmind 		if (!noports) {
    275  1.10     rmind 			goto invop;
    276  1.10     rmind 		}
    277   1.1     rmind 		assert(npfvar_get_count(popts) == 2);
    278   1.1     rmind 
    279   1.1     rmind 		int *icmp_type, *icmp_code;
    280   1.1     rmind 		icmp_type = npfvar_get_data(popts, NPFVAR_ICMP, 0);
    281   1.1     rmind 		icmp_code = npfvar_get_data(popts, NPFVAR_ICMP, 1);
    282   1.1     rmind 		npfctl_gennc_icmp(nc, *icmp_type, *icmp_code);
    283  1.20     rmind 		noports = false;
    284   1.1     rmind 		break;
    285  1.12       spz 	case IPPROTO_ICMPV6:
    286  1.12       spz 		/*
    287  1.12       spz 		 * Build ICMP block.
    288  1.12       spz 		 */
    289  1.20     rmind 		if (!noports) {
    290  1.12       spz 			goto invop;
    291  1.12       spz 		}
    292  1.12       spz 		assert(npfvar_get_count(popts) == 2);
    293  1.12       spz 
    294  1.12       spz 		int *icmp6_type, *icmp6_code;
    295  1.12       spz 		icmp6_type = npfvar_get_data(popts, NPFVAR_ICMP6, 0);
    296  1.12       spz 		icmp6_code = npfvar_get_data(popts, NPFVAR_ICMP6, 1);
    297  1.12       spz 		npfctl_gennc_icmp6(nc, *icmp6_type, *icmp6_code);
    298  1.20     rmind 		noports = false;
    299  1.12       spz 		break;
    300   1.1     rmind 	case -1:
    301   1.1     rmind 		pflag = NC_MATCH_TCP | NC_MATCH_UDP;
    302  1.20     rmind 		noports = false;
    303   1.1     rmind 		break;
    304   1.1     rmind 	default:
    305  1.10     rmind 		/*
    306  1.20     rmind 		 * No filter options are supported for other protocols,
    307  1.20     rmind 		 * only the IP addresses are allowed.
    308  1.10     rmind 		 */
    309  1.20     rmind 		if (noports) {
    310  1.10     rmind 			break;
    311  1.10     rmind 		}
    312  1.10     rmind invop:
    313  1.10     rmind 		yyerror("invalid filter options for protocol %d", proto);
    314  1.10     rmind 	}
    315  1.10     rmind 
    316  1.10     rmind 	/*
    317  1.10     rmind 	 * Build the protocol block, unless other blocks will implicitly
    318  1.10     rmind 	 * perform the family/protocol checks for us.
    319  1.10     rmind 	 */
    320  1.20     rmind 	if ((family != AF_UNSPEC && noaddrs) || (proto != -1 && noports)) {
    321  1.10     rmind 		uint8_t addrlen;
    322  1.10     rmind 
    323  1.10     rmind 		switch (family) {
    324  1.10     rmind 		case AF_INET:
    325  1.10     rmind 			addrlen = sizeof(struct in_addr);
    326  1.10     rmind 			break;
    327  1.10     rmind 		case AF_INET6:
    328  1.10     rmind 			addrlen = sizeof(struct in6_addr);
    329  1.10     rmind 			break;
    330  1.10     rmind 		default:
    331  1.10     rmind 			addrlen = 0;
    332  1.10     rmind 		}
    333  1.20     rmind 		npfctl_gennc_proto(nc,
    334  1.20     rmind 		    noaddrs ? addrlen : 0,
    335  1.20     rmind 		    noports ? proto : 0xff);
    336   1.1     rmind 	}
    337   1.1     rmind 	return pflag;
    338   1.1     rmind }
    339   1.1     rmind 
    340   1.1     rmind static bool
    341   1.1     rmind npfctl_build_ncode(nl_rule_t *rl, sa_family_t family, const opt_proto_t *op,
    342   1.1     rmind     const filt_opts_t *fopts, bool invert)
    343   1.1     rmind {
    344   1.7     rmind 	const addr_port_t *apfrom = &fopts->fo_from;
    345   1.7     rmind 	const addr_port_t *apto = &fopts->fo_to;
    346  1.10     rmind 	const int proto = op->op_proto;
    347  1.20     rmind 	bool noaddrs, noports;
    348   1.1     rmind 	nc_ctx_t *nc;
    349   1.1     rmind 	void *code;
    350   1.1     rmind 	size_t len;
    351   1.1     rmind 
    352  1.10     rmind 	/*
    353  1.10     rmind 	 * If none specified, no n-code.
    354  1.10     rmind 	 */
    355  1.20     rmind 	noaddrs = !apfrom->ap_netaddr && !apto->ap_netaddr;
    356  1.20     rmind 	noports = !apfrom->ap_portrange && !apto->ap_portrange;
    357  1.20     rmind 	if (family == AF_UNSPEC && proto == -1 && !op->op_opts &&
    358  1.20     rmind 	    noaddrs && noports)
    359   1.1     rmind 		return false;
    360   1.1     rmind 
    361   1.1     rmind 	int srcflag = NC_MATCH_SRC;
    362   1.1     rmind 	int dstflag = NC_MATCH_DST;
    363   1.1     rmind 
    364   1.1     rmind 	if (invert) {
    365   1.1     rmind 		srcflag = NC_MATCH_DST;
    366   1.1     rmind 		dstflag = NC_MATCH_SRC;
    367   1.1     rmind 	}
    368   1.1     rmind 
    369   1.1     rmind 	nc = npfctl_ncgen_create();
    370   1.1     rmind 
    371  1.10     rmind 	/* Build layer 4 protocol blocks. */
    372  1.20     rmind 	int pflag = npfctl_build_proto(nc, family, op, noaddrs, noports);
    373  1.10     rmind 
    374   1.1     rmind 	/* Build IP address blocks. */
    375   1.7     rmind 	npfctl_build_vars(nc, family, apfrom->ap_netaddr, srcflag);
    376   1.7     rmind 	npfctl_build_vars(nc, family, apto->ap_netaddr, dstflag);
    377   1.1     rmind 
    378   1.1     rmind 	/* Build port-range blocks. */
    379  1.10     rmind 	npfctl_build_vars(nc, family, apfrom->ap_portrange, srcflag | pflag);
    380  1.10     rmind 	npfctl_build_vars(nc, family, apto->ap_portrange, dstflag | pflag);
    381   1.1     rmind 
    382   1.1     rmind 	/*
    383   1.1     rmind 	 * Complete n-code (destroys the context) and pass to the rule.
    384   1.1     rmind 	 */
    385   1.1     rmind 	code = npfctl_ncgen_complete(nc, &len);
    386   1.1     rmind 	if (npf_debug) {
    387  1.24     rmind 		extern char *yytext;
    388   1.1     rmind 		extern int yylineno;
    389  1.24     rmind 
    390  1.24     rmind 		printf("RULE AT LINE %d\n", yylineno - (int)(*yytext == '\n'));
    391   1.1     rmind 		npfctl_ncgen_print(code, len);
    392   1.1     rmind 	}
    393  1.10     rmind 	assert(code && len > 0);
    394  1.10     rmind 
    395  1.18     rmind 	if (npf_rule_setcode(rl, NPF_CODE_NC, code, len) == -1) {
    396   1.1     rmind 		errx(EXIT_FAILURE, "npf_rule_setcode failed");
    397   1.1     rmind 	}
    398   1.1     rmind 	free(code);
    399   1.1     rmind 	return true;
    400   1.1     rmind }
    401   1.1     rmind 
    402   1.4     rmind static void
    403   1.4     rmind npfctl_build_rpcall(nl_rproc_t *rp, const char *name, npfvar_t *args)
    404   1.4     rmind {
    405  1.14     rmind 	npf_extmod_t *extmod;
    406  1.14     rmind 	nl_ext_t *extcall;
    407  1.14     rmind 	int error;
    408   1.4     rmind 
    409  1.14     rmind 	extmod = npf_extmod_get(name, &extcall);
    410  1.14     rmind 	if (extmod == NULL) {
    411   1.4     rmind 		yyerror("unknown rule procedure '%s'", name);
    412   1.4     rmind 	}
    413   1.4     rmind 
    414   1.4     rmind 	for (size_t i = 0; i < npfvar_get_count(args); i++) {
    415  1.14     rmind 		const char *param, *value;
    416  1.14     rmind 		proc_param_t *p;
    417   1.4     rmind 
    418  1.14     rmind 		p = npfvar_get_data(args, NPFVAR_PROC_PARAM, i);
    419  1.14     rmind 		param = p->pp_param;
    420  1.14     rmind 		value = p->pp_value;
    421  1.14     rmind 
    422  1.14     rmind 		error = npf_extmod_param(extmod, extcall, param, value);
    423  1.14     rmind 		switch (error) {
    424  1.14     rmind 		case EINVAL:
    425  1.14     rmind 			yyerror("invalid parameter '%s'", param);
    426  1.14     rmind 		default:
    427  1.14     rmind 			break;
    428   1.4     rmind 		}
    429   1.4     rmind 	}
    430  1.14     rmind 	error = npf_rproc_extcall(rp, extcall);
    431  1.14     rmind 	if (error) {
    432  1.14     rmind 		yyerror(error == EEXIST ?
    433  1.14     rmind 		    "duplicate procedure call" : "unexpected error");
    434  1.14     rmind 	}
    435   1.4     rmind }
    436   1.4     rmind 
    437   1.1     rmind /*
    438   1.1     rmind  * npfctl_build_rproc: create and insert a rule procedure.
    439   1.1     rmind  */
    440   1.1     rmind void
    441   1.4     rmind npfctl_build_rproc(const char *name, npfvar_t *procs)
    442   1.1     rmind {
    443   1.1     rmind 	nl_rproc_t *rp;
    444   1.4     rmind 	size_t i;
    445   1.1     rmind 
    446   1.1     rmind 	rp = npf_rproc_create(name);
    447   1.1     rmind 	if (rp == NULL) {
    448  1.23  christos 		errx(EXIT_FAILURE, "%s failed", __func__);
    449   1.1     rmind 	}
    450   1.1     rmind 	npf_rproc_insert(npf_conf, rp);
    451   1.4     rmind 
    452   1.4     rmind 	for (i = 0; i < npfvar_get_count(procs); i++) {
    453  1.14     rmind 		proc_call_t *pc = npfvar_get_data(procs, NPFVAR_PROC, i);
    454  1.14     rmind 		npfctl_build_rpcall(rp, pc->pc_name, pc->pc_opts);
    455   1.4     rmind 	}
    456   1.1     rmind }
    457   1.1     rmind 
    458  1.22     rmind void
    459  1.22     rmind npfctl_build_maprset(const char *name, int attr, u_int if_idx)
    460  1.22     rmind {
    461  1.22     rmind 	const int attr_di = (NPF_RULE_IN | NPF_RULE_OUT);
    462  1.22     rmind 	nl_rule_t *rl;
    463  1.22     rmind 
    464  1.22     rmind 	/* If no direction is not specified, then both. */
    465  1.22     rmind 	if ((attr & attr_di) == 0) {
    466  1.22     rmind 		attr |= attr_di;
    467  1.22     rmind 	}
    468  1.22     rmind 	/* Allow only "in/out" attributes. */
    469  1.22     rmind 	attr = NPF_RULE_GROUP | NPF_RULE_GROUP | (attr & attr_di);
    470  1.22     rmind 	rl = npf_rule_create(name, attr, if_idx);
    471  1.22     rmind 	npf_nat_insert(npf_conf, rl, NPF_PRI_LAST);
    472  1.22     rmind }
    473  1.22     rmind 
    474   1.1     rmind /*
    475  1.18     rmind  * npfctl_build_group: create a group, insert into the global ruleset,
    476  1.18     rmind  * update the current group pointer and increase the nesting level.
    477   1.1     rmind  */
    478   1.1     rmind void
    479  1.18     rmind npfctl_build_group(const char *name, int attr, u_int if_idx, bool def)
    480   1.1     rmind {
    481   1.1     rmind 	const int attr_di = (NPF_RULE_IN | NPF_RULE_OUT);
    482   1.1     rmind 	nl_rule_t *rl;
    483   1.1     rmind 
    484  1.18     rmind 	if (def || (attr & attr_di) == 0) {
    485  1.18     rmind 		attr |= attr_di;
    486  1.18     rmind 	}
    487  1.18     rmind 
    488  1.18     rmind 	rl = npf_rule_create(name, attr | NPF_RULE_GROUP, if_idx);
    489  1.18     rmind 	npf_rule_setprio(rl, NPF_PRI_LAST);
    490  1.18     rmind 	if (def) {
    491  1.18     rmind 		if (defgroup) {
    492   1.1     rmind 			yyerror("multiple default groups are not valid");
    493   1.1     rmind 		}
    494  1.18     rmind 		if (rule_nesting_level) {
    495  1.18     rmind 			yyerror("default group can only be at the top level");
    496  1.18     rmind 		}
    497  1.18     rmind 		defgroup = rl;
    498  1.18     rmind 	} else {
    499  1.18     rmind 		nl_rule_t *cg = current_group[rule_nesting_level];
    500  1.18     rmind 		npf_rule_insert(npf_conf, cg, rl);
    501  1.18     rmind 	}
    502   1.1     rmind 
    503  1.18     rmind 	/* Set the current group and increase the nesting level. */
    504  1.18     rmind 	if (rule_nesting_level >= MAX_RULE_NESTING) {
    505  1.18     rmind 		yyerror("rule nesting limit reached");
    506   1.1     rmind 	}
    507  1.18     rmind 	current_group[++rule_nesting_level] = rl;
    508  1.18     rmind }
    509   1.1     rmind 
    510  1.18     rmind void
    511  1.18     rmind npfctl_build_group_end(void)
    512  1.18     rmind {
    513  1.18     rmind 	assert(rule_nesting_level > 0);
    514  1.18     rmind 	current_group[rule_nesting_level--] = NULL;
    515   1.1     rmind }
    516   1.1     rmind 
    517   1.1     rmind /*
    518   1.1     rmind  * npfctl_build_rule: create a rule, build n-code from filter options,
    519  1.18     rmind  * if any, and insert into the ruleset of current group, or set the rule.
    520   1.1     rmind  */
    521   1.1     rmind void
    522  1.21     rmind npfctl_build_rule(uint32_t attr, u_int if_idx, sa_family_t family,
    523   1.1     rmind     const opt_proto_t *op, const filt_opts_t *fopts, const char *rproc)
    524   1.1     rmind {
    525   1.1     rmind 	nl_rule_t *rl;
    526   1.1     rmind 
    527  1.19     rmind 	attr |= (npf_conf ? 0 : NPF_RULE_DYNAMIC);
    528  1.21     rmind 
    529   1.1     rmind 	rl = npf_rule_create(NULL, attr, if_idx);
    530   1.1     rmind 	npfctl_build_ncode(rl, family, op, fopts, false);
    531  1.18     rmind 	if (rproc) {
    532  1.18     rmind 		npf_rule_setproc(rl, rproc);
    533  1.18     rmind 	}
    534  1.18     rmind 
    535  1.18     rmind 	if (npf_conf) {
    536  1.18     rmind 		nl_rule_t *cg = current_group[rule_nesting_level];
    537  1.18     rmind 
    538  1.18     rmind 		if (rproc && !npf_rproc_exists_p(npf_conf, rproc)) {
    539  1.18     rmind 			yyerror("rule procedure '%s' is not defined", rproc);
    540  1.18     rmind 		}
    541  1.18     rmind 		assert(cg != NULL);
    542  1.18     rmind 		npf_rule_setprio(rl, NPF_PRI_LAST);
    543  1.18     rmind 		npf_rule_insert(npf_conf, cg, rl);
    544  1.18     rmind 	} else {
    545  1.18     rmind 		/* We have parsed a single rule - set it. */
    546  1.18     rmind 		the_rule = rl;
    547   1.1     rmind 	}
    548   1.1     rmind }
    549   1.1     rmind 
    550   1.1     rmind /*
    551  1.14     rmind  * npfctl_build_nat: create a single NAT policy of a specified
    552  1.13     rmind  * type with a given filter options.
    553  1.13     rmind  */
    554  1.13     rmind static void
    555  1.13     rmind npfctl_build_nat(int type, u_int if_idx, sa_family_t family,
    556  1.13     rmind     const addr_port_t *ap, const filt_opts_t *fopts, bool binat)
    557  1.13     rmind {
    558  1.13     rmind 	const opt_proto_t op = { .op_proto = -1, .op_opts = NULL };
    559  1.13     rmind 	fam_addr_mask_t *am;
    560  1.13     rmind 	in_port_t port;
    561  1.13     rmind 	nl_nat_t *nat;
    562  1.13     rmind 
    563  1.13     rmind 	if (!ap->ap_netaddr) {
    564  1.13     rmind 		yyerror("%s network segment is not specified",
    565  1.13     rmind 		    type == NPF_NATIN ? "inbound" : "outbound");
    566  1.13     rmind 	}
    567  1.13     rmind 	am = npfctl_get_singlefam(ap->ap_netaddr);
    568  1.13     rmind 	if (am->fam_family != family) {
    569  1.13     rmind 		yyerror("IPv6 NAT is not supported");
    570  1.13     rmind 	}
    571  1.13     rmind 
    572  1.13     rmind 	switch (type) {
    573  1.13     rmind 	case NPF_NATOUT:
    574  1.13     rmind 		/*
    575  1.13     rmind 		 * Outbound NAT (or source NAT) policy, usually used for the
    576  1.13     rmind 		 * traditional NAPT.  If it is a half for bi-directional NAT,
    577  1.13     rmind 		 * then no port translation with mapping.
    578  1.13     rmind 		 */
    579  1.13     rmind 		nat = npf_nat_create(NPF_NATOUT, !binat ?
    580  1.13     rmind 		    (NPF_NAT_PORTS | NPF_NAT_PORTMAP) : 0,
    581  1.13     rmind 		    if_idx, &am->fam_addr, am->fam_family, 0);
    582  1.13     rmind 		break;
    583  1.13     rmind 	case NPF_NATIN:
    584  1.13     rmind 		/*
    585  1.13     rmind 		 * Inbound NAT (or destination NAT).  Unless bi-NAT, a port
    586  1.13     rmind 		 * must be specified, since it has to be redirection.
    587  1.13     rmind 		 */
    588  1.13     rmind 		port = 0;
    589  1.13     rmind 		if (!binat) {
    590  1.13     rmind 			if (!ap->ap_portrange) {
    591  1.13     rmind 				yyerror("inbound port is not specified");
    592  1.13     rmind 			}
    593  1.13     rmind 			port = npfctl_get_singleport(ap->ap_portrange);
    594  1.13     rmind 		}
    595  1.13     rmind 		nat = npf_nat_create(NPF_NATIN, !binat ? NPF_NAT_PORTS : 0,
    596  1.13     rmind 		    if_idx, &am->fam_addr, am->fam_family, port);
    597  1.13     rmind 		break;
    598  1.13     rmind 	default:
    599  1.13     rmind 		assert(false);
    600  1.13     rmind 	}
    601  1.13     rmind 
    602  1.13     rmind 	npfctl_build_ncode(nat, family, &op, fopts, false);
    603  1.18     rmind 	npf_nat_insert(npf_conf, nat, NPF_PRI_LAST);
    604  1.13     rmind }
    605  1.13     rmind 
    606  1.13     rmind /*
    607  1.14     rmind  * npfctl_build_natseg: validate and create NAT policies.
    608   1.1     rmind  */
    609   1.1     rmind void
    610  1.13     rmind npfctl_build_natseg(int sd, int type, u_int if_idx, const addr_port_t *ap1,
    611   1.7     rmind     const addr_port_t *ap2, const filt_opts_t *fopts)
    612   1.1     rmind {
    613  1.13     rmind 	sa_family_t af = AF_INET;
    614   1.7     rmind 	filt_opts_t imfopts;
    615  1.13     rmind 	bool binat;
    616   1.1     rmind 
    617   1.7     rmind 	if (sd == NPFCTL_NAT_STATIC) {
    618   1.7     rmind 		yyerror("static NAT is not yet supported");
    619   1.7     rmind 	}
    620   1.7     rmind 	assert(sd == NPFCTL_NAT_DYNAMIC);
    621   1.7     rmind 	assert(if_idx != 0);
    622   1.7     rmind 
    623  1.13     rmind 	/*
    624  1.13     rmind 	 * Bi-directional NAT is a combination of inbound NAT and outbound
    625  1.13     rmind 	 * NAT policies.  Note that the translation address is local IP and
    626  1.13     rmind 	 * the filter criteria is inverted accordingly.
    627  1.13     rmind 	 */
    628  1.13     rmind 	binat = (NPF_NATIN | NPF_NATOUT) == type;
    629   1.7     rmind 
    630   1.7     rmind 	/*
    631  1.13     rmind 	 * If the filter criteria is not specified explicitly, apply implicit
    632  1.14     rmind 	 * filtering according to the given network segments.
    633  1.13     rmind 	 *
    634  1.13     rmind 	 * Note: filled below, depending on the type.
    635   1.7     rmind 	 */
    636  1.14     rmind 	if (__predict_true(!fopts)) {
    637   1.7     rmind 		fopts = &imfopts;
    638   1.1     rmind 	}
    639   1.1     rmind 
    640  1.13     rmind 	if (type & NPF_NATIN) {
    641  1.13     rmind 		memset(&imfopts, 0, sizeof(filt_opts_t));
    642  1.13     rmind 		memcpy(&imfopts.fo_to, ap2, sizeof(addr_port_t));
    643  1.13     rmind 		npfctl_build_nat(NPF_NATIN, if_idx, af, ap1, fopts, binat);
    644  1.13     rmind 	}
    645  1.13     rmind 	if (type & NPF_NATOUT) {
    646  1.13     rmind 		memset(&imfopts, 0, sizeof(filt_opts_t));
    647  1.13     rmind 		memcpy(&imfopts.fo_from, ap1, sizeof(addr_port_t));
    648  1.13     rmind 		npfctl_build_nat(NPF_NATOUT, if_idx, af, ap2, fopts, binat);
    649   1.1     rmind 	}
    650   1.1     rmind }
    651   1.1     rmind 
    652   1.1     rmind /*
    653   1.1     rmind  * npfctl_fill_table: fill NPF table with entries from a specified file.
    654   1.1     rmind  */
    655   1.1     rmind static void
    656  1.11     rmind npfctl_fill_table(nl_table_t *tl, u_int type, const char *fname)
    657   1.1     rmind {
    658   1.1     rmind 	char *buf = NULL;
    659   1.1     rmind 	int l = 0;
    660   1.1     rmind 	FILE *fp;
    661   1.1     rmind 	size_t n;
    662   1.1     rmind 
    663   1.1     rmind 	fp = fopen(fname, "r");
    664   1.1     rmind 	if (fp == NULL) {
    665   1.1     rmind 		err(EXIT_FAILURE, "open '%s'", fname);
    666   1.1     rmind 	}
    667   1.1     rmind 	while (l++, getline(&buf, &n, fp) != -1) {
    668  1.11     rmind 		fam_addr_mask_t fam;
    669  1.11     rmind 		int alen;
    670   1.1     rmind 
    671   1.1     rmind 		if (*buf == '\n' || *buf == '#') {
    672   1.1     rmind 			continue;
    673   1.1     rmind 		}
    674  1.11     rmind 
    675  1.11     rmind 		if (!npfctl_parse_cidr(buf, &fam, &alen)) {
    676  1.11     rmind 			errx(EXIT_FAILURE,
    677  1.11     rmind 			    "%s:%d: invalid table entry", fname, l);
    678  1.11     rmind 		}
    679  1.11     rmind 		if (type == NPF_TABLE_HASH && fam.fam_mask != NPF_NO_NETMASK) {
    680  1.11     rmind 			errx(EXIT_FAILURE,
    681  1.11     rmind 			    "%s:%d: mask used with the hash table", fname, l);
    682   1.1     rmind 		}
    683   1.1     rmind 
    684   1.1     rmind 		/* Create and add a table entry. */
    685  1.17     rmind 		npf_table_add_entry(tl, fam.fam_family,
    686  1.17     rmind 		    &fam.fam_addr, fam.fam_mask);
    687   1.1     rmind 	}
    688   1.1     rmind 	if (buf != NULL) {
    689   1.1     rmind 		free(buf);
    690   1.1     rmind 	}
    691   1.1     rmind }
    692   1.1     rmind 
    693   1.1     rmind /*
    694   1.1     rmind  * npfctl_build_table: create an NPF table, add to the configuration and,
    695   1.1     rmind  * if required, fill with contents from a file.
    696   1.1     rmind  */
    697   1.1     rmind void
    698   1.1     rmind npfctl_build_table(const char *tid, u_int type, const char *fname)
    699   1.1     rmind {
    700   1.1     rmind 	nl_table_t *tl;
    701   1.1     rmind 	u_int id;
    702   1.1     rmind 
    703   1.1     rmind 	id = atoi(tid);
    704   1.1     rmind 	tl = npf_table_create(id, type);
    705   1.1     rmind 	assert(tl != NULL);
    706   1.1     rmind 
    707   1.1     rmind 	if (npf_table_insert(npf_conf, tl)) {
    708   1.1     rmind 		errx(EXIT_FAILURE, "table '%d' is already defined\n", id);
    709   1.1     rmind 	}
    710   1.1     rmind 
    711   1.1     rmind 	if (fname) {
    712  1.11     rmind 		npfctl_fill_table(tl, type, fname);
    713   1.1     rmind 	}
    714   1.1     rmind }
    715  1.23  christos 
    716  1.23  christos /*
    717  1.23  christos  * npfctl_build_alg: create an NPF application level gatewayl and add it
    718  1.23  christos  * to the configuration.
    719  1.23  christos  */
    720  1.23  christos void
    721  1.23  christos npfctl_build_alg(const char *al_name)
    722  1.23  christos {
    723  1.23  christos 	if (_npf_alg_load(npf_conf, al_name) != 0) {
    724  1.23  christos 		errx(EXIT_FAILURE, "ALG '%s' already loaded", al_name);
    725  1.23  christos 	}
    726  1.23  christos }
    727