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