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