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