Home | History | Annotate | Line # | Download | only in npfctl
npf_build.c revision 1.41
      1  1.41  christos /*	$NetBSD: npf_build.c,v 1.41 2016/12/26 23:05:05 christos Exp $	*/
      2   1.1     rmind 
      3   1.1     rmind /*-
      4  1.33     rmind  * Copyright (c) 2011-2014 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.41  christos __RCSID("$NetBSD: npf_build.c,v 1.41 2016/12/26 23:05:05 christos Exp $");
     38   1.1     rmind 
     39   1.1     rmind #include <sys/types.h>
     40  1.33     rmind #include <sys/mman.h>
     41  1.33     rmind #include <sys/stat.h>
     42  1.41  christos #define	__FAVOR_BSD
     43  1.37     rmind #include <netinet/tcp.h>
     44   1.1     rmind 
     45   1.1     rmind #include <stdlib.h>
     46   1.1     rmind #include <inttypes.h>
     47   1.1     rmind #include <string.h>
     48  1.29     rmind #include <ctype.h>
     49  1.33     rmind #include <unistd.h>
     50  1.41  christos #include <fcntl.h>
     51  1.14     rmind #include <errno.h>
     52   1.1     rmind #include <err.h>
     53   1.1     rmind 
     54  1.25     rmind #include <pcap/pcap.h>
     55  1.33     rmind #include <cdbw.h>
     56  1.25     rmind 
     57   1.1     rmind #include "npfctl.h"
     58   1.1     rmind 
     59  1.18     rmind #define	MAX_RULE_NESTING	16
     60  1.18     rmind 
     61   1.1     rmind static nl_config_t *		npf_conf = NULL;
     62   1.1     rmind static bool			npf_debug = false;
     63  1.18     rmind static nl_rule_t *		the_rule = NULL;
     64  1.18     rmind 
     65  1.18     rmind static nl_rule_t *		current_group[MAX_RULE_NESTING];
     66  1.18     rmind static unsigned			rule_nesting_level = 0;
     67  1.18     rmind static nl_rule_t *		defgroup = NULL;
     68   1.1     rmind 
     69  1.27     rmind static void			npfctl_dump_bpf(struct bpf_program *);
     70  1.27     rmind 
     71   1.1     rmind void
     72   1.1     rmind npfctl_config_init(bool debug)
     73   1.1     rmind {
     74   1.1     rmind 	npf_conf = npf_config_create();
     75   1.1     rmind 	if (npf_conf == NULL) {
     76   1.1     rmind 		errx(EXIT_FAILURE, "npf_config_create failed");
     77   1.1     rmind 	}
     78   1.1     rmind 	npf_debug = debug;
     79  1.18     rmind 	memset(current_group, 0, sizeof(current_group));
     80   1.1     rmind }
     81   1.1     rmind 
     82   1.1     rmind int
     83  1.13     rmind npfctl_config_send(int fd, const char *out)
     84   1.1     rmind {
     85  1.41  christos 	npf_error_t errinfo;
     86  1.41  christos 	int error = 0;
     87   1.1     rmind 
     88  1.18     rmind 	if (!defgroup) {
     89   1.1     rmind 		errx(EXIT_FAILURE, "default group was not defined");
     90   1.1     rmind 	}
     91  1.18     rmind 	npf_rule_insert(npf_conf, NULL, defgroup);
     92  1.41  christos 	if (out) {
     93  1.41  christos 		printf("\nSaving to %s\n", out);
     94  1.41  christos 		npfctl_config_save(npf_conf, out);
     95  1.41  christos 	} else {
     96  1.41  christos 		error = npf_config_submit(npf_conf, fd, &errinfo);
     97  1.41  christos 	}
     98  1.39     rmind 	if (error == EEXIST) { /* XXX */
     99  1.39     rmind 		errx(EXIT_FAILURE, "(re)load failed: "
    100  1.39     rmind 		    "some table has a duplicate entry?");
    101  1.39     rmind 	}
    102   1.3     rmind 	if (error) {
    103  1.41  christos 		npfctl_print_error(&errinfo);
    104   1.3     rmind 	}
    105  1.41  christos 	npf_config_destroy(npf_conf);
    106  1.41  christos 	return error;
    107  1.41  christos }
    108  1.41  christos 
    109  1.41  christos void
    110  1.41  christos npfctl_config_save(nl_config_t *ncf, const char *outfile)
    111  1.41  christos {
    112  1.41  christos 	void *blob;
    113  1.41  christos 	size_t len;
    114  1.41  christos 	int fd;
    115  1.41  christos 
    116  1.41  christos 	blob = npf_config_export(ncf, &len);
    117  1.41  christos 	if (!blob)
    118  1.41  christos 		err(EXIT_FAILURE, "npf_config_export");
    119  1.41  christos 	if ((fd = open(outfile, O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1)
    120  1.41  christos 		err(EXIT_FAILURE, "could not open %s", outfile);
    121  1.41  christos 	if (write(fd, blob, len) != (ssize_t)len) {
    122  1.41  christos 		err(EXIT_FAILURE, "write to %s failed", outfile);
    123  1.25     rmind 	}
    124  1.41  christos 	free(blob);
    125  1.41  christos 	close(fd);
    126   1.1     rmind }
    127   1.1     rmind 
    128  1.16     rmind nl_config_t *
    129  1.16     rmind npfctl_config_ref(void)
    130  1.16     rmind {
    131  1.16     rmind 	return npf_conf;
    132  1.16     rmind }
    133  1.16     rmind 
    134  1.18     rmind nl_rule_t *
    135  1.18     rmind npfctl_rule_ref(void)
    136  1.18     rmind {
    137  1.18     rmind 	return the_rule;
    138  1.18     rmind }
    139  1.18     rmind 
    140  1.28     rmind bool
    141  1.13     rmind npfctl_debug_addif(const char *ifname)
    142  1.13     rmind {
    143  1.28     rmind 	const char tname[] = "npftest";
    144  1.13     rmind 	const size_t tnamelen = sizeof(tname) - 1;
    145  1.13     rmind 
    146  1.28     rmind 	if (npf_debug) {
    147  1.28     rmind 		_npf_debug_addif(npf_conf, ifname);
    148  1.28     rmind 		return strncmp(ifname, tname, tnamelen) == 0;
    149  1.13     rmind 	}
    150  1.28     rmind 	return 0;
    151  1.13     rmind }
    152  1.13     rmind 
    153  1.32     rmind unsigned
    154  1.32     rmind npfctl_table_getid(const char *name)
    155   1.1     rmind {
    156  1.32     rmind 	unsigned tid = (unsigned)-1;
    157  1.32     rmind 	nl_table_t *tl;
    158  1.32     rmind 
    159  1.32     rmind 	/* XXX dynamic ruleset */
    160  1.32     rmind 	if (!npf_conf) {
    161  1.32     rmind 		return (unsigned)-1;
    162  1.32     rmind 	}
    163  1.32     rmind 
    164  1.32     rmind 	/* XXX: Iterating all as we need to rewind for the next call. */
    165  1.32     rmind 	while ((tl = npf_table_iterate(npf_conf)) != NULL) {
    166  1.32     rmind 		const char *tname = npf_table_getname(tl);
    167  1.32     rmind 		if (strcmp(tname, name) == 0) {
    168  1.32     rmind 			tid = npf_table_getid(tl);
    169  1.32     rmind 		}
    170  1.32     rmind 	}
    171  1.32     rmind 	return tid;
    172   1.1     rmind }
    173   1.1     rmind 
    174   1.7     rmind static in_port_t
    175   1.1     rmind npfctl_get_singleport(const npfvar_t *vp)
    176   1.1     rmind {
    177   1.1     rmind 	port_range_t *pr;
    178   1.7     rmind 	in_port_t *port;
    179   1.1     rmind 
    180   1.1     rmind 	if (npfvar_get_count(vp) > 1) {
    181   1.1     rmind 		yyerror("multiple ports are not valid");
    182   1.1     rmind 	}
    183   1.1     rmind 	pr = npfvar_get_data(vp, NPFVAR_PORT_RANGE, 0);
    184   1.1     rmind 	if (pr->pr_start != pr->pr_end) {
    185   1.1     rmind 		yyerror("port range is not valid");
    186   1.1     rmind 	}
    187   1.7     rmind 	port = &pr->pr_start;
    188   1.7     rmind 	return *port;
    189   1.1     rmind }
    190   1.1     rmind 
    191   1.1     rmind static fam_addr_mask_t *
    192   1.1     rmind npfctl_get_singlefam(const npfvar_t *vp)
    193   1.1     rmind {
    194   1.1     rmind 	if (npfvar_get_count(vp) > 1) {
    195   1.1     rmind 		yyerror("multiple addresses are not valid");
    196   1.1     rmind 	}
    197   1.1     rmind 	return npfvar_get_data(vp, NPFVAR_FAM, 0);
    198   1.1     rmind }
    199   1.1     rmind 
    200  1.10     rmind static bool
    201  1.25     rmind npfctl_build_fam(npf_bpf_t *ctx, sa_family_t family,
    202   1.1     rmind     fam_addr_mask_t *fam, int opts)
    203   1.1     rmind {
    204   1.1     rmind 	/*
    205   1.1     rmind 	 * If family is specified, address does not match it and the
    206   1.1     rmind 	 * address is extracted from the interface, then simply ignore.
    207   1.1     rmind 	 * Otherwise, address of invalid family was passed manually.
    208   1.1     rmind 	 */
    209   1.1     rmind 	if (family != AF_UNSPEC && family != fam->fam_family) {
    210  1.15     rmind 		if (!fam->fam_ifindex) {
    211   1.1     rmind 			yyerror("specified address is not of the required "
    212   1.1     rmind 			    "family %d", family);
    213   1.1     rmind 		}
    214  1.10     rmind 		return false;
    215   1.1     rmind 	}
    216  1.30     rmind 
    217  1.25     rmind 	family = fam->fam_family;
    218  1.30     rmind 	if (family != AF_INET && family != AF_INET6) {
    219  1.30     rmind 		yyerror("family %d is not supported", family);
    220  1.30     rmind 	}
    221   1.1     rmind 
    222   1.1     rmind 	/*
    223   1.1     rmind 	 * Optimise 0.0.0.0/0 case to be NOP.  Otherwise, address with
    224   1.1     rmind 	 * zero mask would never match and therefore is not valid.
    225   1.1     rmind 	 */
    226   1.1     rmind 	if (fam->fam_mask == 0) {
    227  1.30     rmind 		static const npf_addr_t zero; /* must be static */
    228  1.10     rmind 
    229   1.1     rmind 		if (memcmp(&fam->fam_addr, &zero, sizeof(npf_addr_t))) {
    230   1.1     rmind 			yyerror("filter criterion would never match");
    231   1.1     rmind 		}
    232  1.10     rmind 		return false;
    233   1.1     rmind 	}
    234   1.1     rmind 
    235  1.25     rmind 	npfctl_bpf_cidr(ctx, opts, family, &fam->fam_addr, fam->fam_mask);
    236  1.10     rmind 	return true;
    237   1.1     rmind }
    238   1.1     rmind 
    239   1.1     rmind static void
    240  1.25     rmind npfctl_build_vars(npf_bpf_t *ctx, sa_family_t family, npfvar_t *vars, int opts)
    241   1.1     rmind {
    242   1.6  christos 	const int type = npfvar_get_type(vars, 0);
    243   1.1     rmind 	size_t i;
    244   1.1     rmind 
    245  1.25     rmind 	npfctl_bpf_group(ctx);
    246   1.1     rmind 	for (i = 0; i < npfvar_get_count(vars); i++) {
    247   1.1     rmind 		void *data = npfvar_get_data(vars, type, i);
    248   1.1     rmind 		assert(data != NULL);
    249   1.1     rmind 
    250   1.1     rmind 		switch (type) {
    251   1.1     rmind 		case NPFVAR_FAM: {
    252   1.1     rmind 			fam_addr_mask_t *fam = data;
    253  1.25     rmind 			npfctl_build_fam(ctx, family, fam, opts);
    254   1.1     rmind 			break;
    255   1.1     rmind 		}
    256   1.1     rmind 		case NPFVAR_PORT_RANGE: {
    257   1.1     rmind 			port_range_t *pr = data;
    258  1.25     rmind 			npfctl_bpf_ports(ctx, opts, pr->pr_start, pr->pr_end);
    259   1.1     rmind 			break;
    260   1.1     rmind 		}
    261   1.1     rmind 		case NPFVAR_TABLE: {
    262  1.32     rmind 			u_int tid;
    263  1.32     rmind 			memcpy(&tid, data, sizeof(u_int));
    264  1.25     rmind 			npfctl_bpf_table(ctx, opts, tid);
    265   1.1     rmind 			break;
    266   1.1     rmind 		}
    267   1.1     rmind 		default:
    268   1.1     rmind 			assert(false);
    269   1.1     rmind 		}
    270   1.1     rmind 	}
    271  1.25     rmind 	npfctl_bpf_endgroup(ctx);
    272   1.1     rmind }
    273   1.1     rmind 
    274  1.25     rmind static void
    275  1.25     rmind npfctl_build_proto(npf_bpf_t *ctx, sa_family_t family, const opt_proto_t *op)
    276   1.1     rmind {
    277   1.1     rmind 	const npfvar_t *popts = op->op_opts;
    278  1.10     rmind 	const int proto = op->op_proto;
    279  1.25     rmind 
    280  1.25     rmind 	/* IP version and/or L4 protocol matching. */
    281  1.25     rmind 	if (family != AF_UNSPEC || proto != -1) {
    282  1.25     rmind 		npfctl_bpf_proto(ctx, family, proto);
    283  1.25     rmind 	}
    284   1.1     rmind 
    285  1.10     rmind 	switch (proto) {
    286   1.1     rmind 	case IPPROTO_TCP:
    287  1.25     rmind 		/* Build TCP flags matching (optional). */
    288  1.25     rmind 		if (popts) {
    289  1.25     rmind 			uint8_t *tf, *tf_mask;
    290  1.25     rmind 
    291  1.25     rmind 			assert(npfvar_get_count(popts) == 2);
    292  1.25     rmind 			tf = npfvar_get_data(popts, NPFVAR_TCPFLAG, 0);
    293  1.25     rmind 			tf_mask = npfvar_get_data(popts, NPFVAR_TCPFLAG, 1);
    294  1.37     rmind 			npfctl_bpf_tcpfl(ctx, *tf, *tf_mask, false);
    295   1.1     rmind 		}
    296   1.1     rmind 		break;
    297   1.1     rmind 	case IPPROTO_ICMP:
    298  1.12       spz 	case IPPROTO_ICMPV6:
    299  1.25     rmind 		/* Build ICMP/ICMPv6 type and/or code matching. */
    300  1.25     rmind 		if (popts) {
    301  1.25     rmind 			int *icmp_type, *icmp_code;
    302  1.25     rmind 
    303  1.25     rmind 			assert(npfvar_get_count(popts) == 2);
    304  1.25     rmind 			icmp_type = npfvar_get_data(popts, NPFVAR_ICMP, 0);
    305  1.25     rmind 			icmp_code = npfvar_get_data(popts, NPFVAR_ICMP, 1);
    306  1.25     rmind 			npfctl_bpf_icmp(ctx, *icmp_type, *icmp_code);
    307  1.12       spz 		}
    308  1.12       spz 		break;
    309  1.25     rmind 	default:
    310  1.25     rmind 		/* No options for other protocols. */
    311   1.1     rmind 		break;
    312  1.10     rmind 	}
    313   1.1     rmind }
    314   1.1     rmind 
    315   1.1     rmind static bool
    316  1.25     rmind npfctl_build_code(nl_rule_t *rl, sa_family_t family, const opt_proto_t *op,
    317  1.27     rmind     const filt_opts_t *fopts)
    318   1.1     rmind {
    319  1.38     rmind 	bool noproto, noaddrs, noports, need_tcpudp = false;
    320   1.7     rmind 	const addr_port_t *apfrom = &fopts->fo_from;
    321   1.7     rmind 	const addr_port_t *apto = &fopts->fo_to;
    322  1.10     rmind 	const int proto = op->op_proto;
    323  1.25     rmind 	npf_bpf_t *bc;
    324   1.1     rmind 	size_t len;
    325   1.1     rmind 
    326  1.25     rmind 	/* If none specified, then no byte-code. */
    327  1.25     rmind 	noproto = family == AF_UNSPEC && proto == -1 && !op->op_opts;
    328  1.20     rmind 	noaddrs = !apfrom->ap_netaddr && !apto->ap_netaddr;
    329  1.20     rmind 	noports = !apfrom->ap_portrange && !apto->ap_portrange;
    330  1.25     rmind 	if (noproto && noaddrs && noports) {
    331   1.1     rmind 		return false;
    332  1.25     rmind 	}
    333   1.1     rmind 
    334  1.25     rmind 	/*
    335  1.25     rmind 	 * Sanity check: ports can only be used with TCP or UDP protocol.
    336  1.25     rmind 	 * No filter options are supported for other protocols, only the
    337  1.25     rmind 	 * IP addresses are allowed.
    338  1.25     rmind 	 */
    339  1.25     rmind 	if (!noports) {
    340  1.25     rmind 		switch (proto) {
    341  1.25     rmind 		case IPPROTO_TCP:
    342  1.25     rmind 		case IPPROTO_UDP:
    343  1.38     rmind 			break;
    344  1.25     rmind 		case -1:
    345  1.38     rmind 			need_tcpudp = true;
    346  1.25     rmind 			break;
    347  1.25     rmind 		default:
    348  1.25     rmind 			yyerror("invalid filter options for protocol %d", proto);
    349  1.25     rmind 		}
    350  1.25     rmind 	}
    351   1.1     rmind 
    352  1.25     rmind 	bc = npfctl_bpf_create();
    353   1.1     rmind 
    354  1.10     rmind 	/* Build layer 4 protocol blocks. */
    355  1.25     rmind 	npfctl_build_proto(bc, family, op);
    356  1.10     rmind 
    357  1.37     rmind 	/*
    358  1.37     rmind 	 * If this is a stateful rule and TCP flags are not specified,
    359  1.37     rmind 	 * then add "flags S/SAFR" filter for TCP protocol case.
    360  1.37     rmind 	 */
    361  1.37     rmind 	if ((npf_rule_getattr(rl) & NPF_RULE_STATEFUL) != 0 &&
    362  1.37     rmind 	    (proto == -1 || (proto == IPPROTO_TCP && !op->op_opts))) {
    363  1.37     rmind 		npfctl_bpf_tcpfl(bc, TH_SYN,
    364  1.37     rmind 		    TH_SYN | TH_ACK | TH_FIN | TH_RST, proto == -1);
    365  1.37     rmind 	}
    366  1.37     rmind 
    367   1.1     rmind 	/* Build IP address blocks. */
    368  1.27     rmind 	npfctl_build_vars(bc, family, apfrom->ap_netaddr, MATCH_SRC);
    369  1.27     rmind 	npfctl_build_vars(bc, family, apto->ap_netaddr, MATCH_DST);
    370   1.1     rmind 
    371   1.1     rmind 	/* Build port-range blocks. */
    372  1.38     rmind 	if (need_tcpudp) {
    373  1.38     rmind 		/* TCP/UDP check for the ports. */
    374  1.38     rmind 		npfctl_bpf_group(bc);
    375  1.38     rmind 		npfctl_bpf_proto(bc, AF_UNSPEC, IPPROTO_TCP);
    376  1.38     rmind 		npfctl_bpf_proto(bc, AF_UNSPEC, IPPROTO_UDP);
    377  1.38     rmind 		npfctl_bpf_endgroup(bc);
    378  1.38     rmind 	}
    379  1.27     rmind 	npfctl_build_vars(bc, family, apfrom->ap_portrange, MATCH_SRC);
    380  1.27     rmind 	npfctl_build_vars(bc, family, apto->ap_portrange, MATCH_DST);
    381  1.25     rmind 
    382  1.25     rmind 	/* Set the byte-code marks, if any. */
    383  1.25     rmind 	const void *bmarks = npfctl_bpf_bmarks(bc, &len);
    384  1.25     rmind 	if (npf_rule_setinfo(rl, bmarks, len) == -1) {
    385  1.25     rmind 		errx(EXIT_FAILURE, "npf_rule_setinfo failed");
    386  1.25     rmind 	}
    387   1.1     rmind 
    388  1.25     rmind 	/* Complete BPF byte-code and pass to the rule. */
    389  1.25     rmind 	struct bpf_program *bf = npfctl_bpf_complete(bc);
    390  1.40     rmind 	if (bf == NULL) {
    391  1.40     rmind 		npfctl_bpf_destroy(bc);
    392  1.40     rmind 		return true;
    393  1.40     rmind 	}
    394  1.25     rmind 	len = bf->bf_len * sizeof(struct bpf_insn);
    395  1.10     rmind 
    396  1.25     rmind 	if (npf_rule_setcode(rl, NPF_CODE_BPF, bf->bf_insns, len) == -1) {
    397   1.1     rmind 		errx(EXIT_FAILURE, "npf_rule_setcode failed");
    398   1.1     rmind 	}
    399  1.27     rmind 	npfctl_dump_bpf(bf);
    400  1.25     rmind 	npfctl_bpf_destroy(bc);
    401  1.25     rmind 
    402   1.1     rmind 	return true;
    403   1.1     rmind }
    404   1.1     rmind 
    405   1.4     rmind static void
    406  1.27     rmind npfctl_build_pcap(nl_rule_t *rl, const char *filter)
    407  1.27     rmind {
    408  1.27     rmind 	const size_t maxsnaplen = 64 * 1024;
    409  1.27     rmind 	struct bpf_program bf;
    410  1.27     rmind 	size_t len;
    411  1.27     rmind 
    412  1.27     rmind 	if (pcap_compile_nopcap(maxsnaplen, DLT_RAW, &bf,
    413  1.27     rmind 	    filter, 1, PCAP_NETMASK_UNKNOWN) == -1) {
    414  1.27     rmind 		yyerror("invalid pcap-filter(7) syntax");
    415  1.27     rmind 	}
    416  1.27     rmind 	len = bf.bf_len * sizeof(struct bpf_insn);
    417  1.27     rmind 
    418  1.27     rmind 	if (npf_rule_setcode(rl, NPF_CODE_BPF, bf.bf_insns, len) == -1) {
    419  1.27     rmind 		errx(EXIT_FAILURE, "npf_rule_setcode failed");
    420  1.27     rmind 	}
    421  1.27     rmind 	npfctl_dump_bpf(&bf);
    422  1.27     rmind 	pcap_freecode(&bf);
    423  1.27     rmind }
    424  1.27     rmind 
    425  1.27     rmind static void
    426   1.4     rmind npfctl_build_rpcall(nl_rproc_t *rp, const char *name, npfvar_t *args)
    427   1.4     rmind {
    428  1.14     rmind 	npf_extmod_t *extmod;
    429  1.14     rmind 	nl_ext_t *extcall;
    430  1.14     rmind 	int error;
    431   1.4     rmind 
    432  1.14     rmind 	extmod = npf_extmod_get(name, &extcall);
    433  1.14     rmind 	if (extmod == NULL) {
    434   1.4     rmind 		yyerror("unknown rule procedure '%s'", name);
    435   1.4     rmind 	}
    436   1.4     rmind 
    437   1.4     rmind 	for (size_t i = 0; i < npfvar_get_count(args); i++) {
    438  1.14     rmind 		const char *param, *value;
    439  1.14     rmind 		proc_param_t *p;
    440   1.4     rmind 
    441  1.14     rmind 		p = npfvar_get_data(args, NPFVAR_PROC_PARAM, i);
    442  1.14     rmind 		param = p->pp_param;
    443  1.14     rmind 		value = p->pp_value;
    444  1.14     rmind 
    445  1.14     rmind 		error = npf_extmod_param(extmod, extcall, param, value);
    446  1.14     rmind 		switch (error) {
    447  1.14     rmind 		case EINVAL:
    448  1.14     rmind 			yyerror("invalid parameter '%s'", param);
    449  1.14     rmind 		default:
    450  1.14     rmind 			break;
    451   1.4     rmind 		}
    452   1.4     rmind 	}
    453  1.14     rmind 	error = npf_rproc_extcall(rp, extcall);
    454  1.14     rmind 	if (error) {
    455  1.14     rmind 		yyerror(error == EEXIST ?
    456  1.14     rmind 		    "duplicate procedure call" : "unexpected error");
    457  1.14     rmind 	}
    458   1.4     rmind }
    459   1.4     rmind 
    460   1.1     rmind /*
    461   1.1     rmind  * npfctl_build_rproc: create and insert a rule procedure.
    462   1.1     rmind  */
    463   1.1     rmind void
    464   1.4     rmind npfctl_build_rproc(const char *name, npfvar_t *procs)
    465   1.1     rmind {
    466   1.1     rmind 	nl_rproc_t *rp;
    467   1.4     rmind 	size_t i;
    468   1.1     rmind 
    469   1.1     rmind 	rp = npf_rproc_create(name);
    470   1.1     rmind 	if (rp == NULL) {
    471  1.23  christos 		errx(EXIT_FAILURE, "%s failed", __func__);
    472   1.1     rmind 	}
    473   1.1     rmind 	npf_rproc_insert(npf_conf, rp);
    474   1.4     rmind 
    475   1.4     rmind 	for (i = 0; i < npfvar_get_count(procs); i++) {
    476  1.14     rmind 		proc_call_t *pc = npfvar_get_data(procs, NPFVAR_PROC, i);
    477  1.14     rmind 		npfctl_build_rpcall(rp, pc->pc_name, pc->pc_opts);
    478   1.4     rmind 	}
    479   1.1     rmind }
    480   1.1     rmind 
    481  1.22     rmind void
    482  1.28     rmind npfctl_build_maprset(const char *name, int attr, const char *ifname)
    483  1.22     rmind {
    484  1.22     rmind 	const int attr_di = (NPF_RULE_IN | NPF_RULE_OUT);
    485  1.22     rmind 	nl_rule_t *rl;
    486  1.22     rmind 
    487  1.22     rmind 	/* If no direction is not specified, then both. */
    488  1.22     rmind 	if ((attr & attr_di) == 0) {
    489  1.22     rmind 		attr |= attr_di;
    490  1.22     rmind 	}
    491  1.22     rmind 	/* Allow only "in/out" attributes. */
    492  1.22     rmind 	attr = NPF_RULE_GROUP | NPF_RULE_GROUP | (attr & attr_di);
    493  1.28     rmind 	rl = npf_rule_create(name, attr, ifname);
    494  1.22     rmind 	npf_nat_insert(npf_conf, rl, NPF_PRI_LAST);
    495  1.22     rmind }
    496  1.22     rmind 
    497   1.1     rmind /*
    498  1.18     rmind  * npfctl_build_group: create a group, insert into the global ruleset,
    499  1.18     rmind  * update the current group pointer and increase the nesting level.
    500   1.1     rmind  */
    501   1.1     rmind void
    502  1.28     rmind npfctl_build_group(const char *name, int attr, const char *ifname, bool def)
    503   1.1     rmind {
    504   1.1     rmind 	const int attr_di = (NPF_RULE_IN | NPF_RULE_OUT);
    505   1.1     rmind 	nl_rule_t *rl;
    506   1.1     rmind 
    507  1.18     rmind 	if (def || (attr & attr_di) == 0) {
    508  1.18     rmind 		attr |= attr_di;
    509  1.18     rmind 	}
    510  1.18     rmind 
    511  1.28     rmind 	rl = npf_rule_create(name, attr | NPF_RULE_GROUP, ifname);
    512  1.18     rmind 	npf_rule_setprio(rl, NPF_PRI_LAST);
    513  1.18     rmind 	if (def) {
    514  1.18     rmind 		if (defgroup) {
    515   1.1     rmind 			yyerror("multiple default groups are not valid");
    516   1.1     rmind 		}
    517  1.18     rmind 		if (rule_nesting_level) {
    518  1.18     rmind 			yyerror("default group can only be at the top level");
    519  1.18     rmind 		}
    520  1.18     rmind 		defgroup = rl;
    521  1.18     rmind 	} else {
    522  1.18     rmind 		nl_rule_t *cg = current_group[rule_nesting_level];
    523  1.18     rmind 		npf_rule_insert(npf_conf, cg, rl);
    524  1.18     rmind 	}
    525   1.1     rmind 
    526  1.18     rmind 	/* Set the current group and increase the nesting level. */
    527  1.18     rmind 	if (rule_nesting_level >= MAX_RULE_NESTING) {
    528  1.18     rmind 		yyerror("rule nesting limit reached");
    529   1.1     rmind 	}
    530  1.18     rmind 	current_group[++rule_nesting_level] = rl;
    531  1.18     rmind }
    532   1.1     rmind 
    533  1.18     rmind void
    534  1.18     rmind npfctl_build_group_end(void)
    535  1.18     rmind {
    536  1.18     rmind 	assert(rule_nesting_level > 0);
    537  1.18     rmind 	current_group[rule_nesting_level--] = NULL;
    538   1.1     rmind }
    539   1.1     rmind 
    540   1.1     rmind /*
    541  1.26     rmind  * npfctl_build_rule: create a rule, build byte-code from filter options,
    542  1.18     rmind  * if any, and insert into the ruleset of current group, or set the rule.
    543   1.1     rmind  */
    544   1.1     rmind void
    545  1.28     rmind npfctl_build_rule(uint32_t attr, const char *ifname, sa_family_t family,
    546  1.27     rmind     const opt_proto_t *op, const filt_opts_t *fopts,
    547  1.27     rmind     const char *pcap_filter, const char *rproc)
    548   1.1     rmind {
    549   1.1     rmind 	nl_rule_t *rl;
    550   1.1     rmind 
    551  1.19     rmind 	attr |= (npf_conf ? 0 : NPF_RULE_DYNAMIC);
    552  1.21     rmind 
    553  1.28     rmind 	rl = npf_rule_create(NULL, attr, ifname);
    554  1.27     rmind 	if (pcap_filter) {
    555  1.27     rmind 		npfctl_build_pcap(rl, pcap_filter);
    556  1.27     rmind 	} else {
    557  1.27     rmind 		npfctl_build_code(rl, family, op, fopts);
    558  1.27     rmind 	}
    559  1.27     rmind 
    560  1.18     rmind 	if (rproc) {
    561  1.18     rmind 		npf_rule_setproc(rl, rproc);
    562  1.18     rmind 	}
    563  1.18     rmind 
    564  1.18     rmind 	if (npf_conf) {
    565  1.18     rmind 		nl_rule_t *cg = current_group[rule_nesting_level];
    566  1.18     rmind 
    567  1.18     rmind 		if (rproc && !npf_rproc_exists_p(npf_conf, rproc)) {
    568  1.18     rmind 			yyerror("rule procedure '%s' is not defined", rproc);
    569  1.18     rmind 		}
    570  1.18     rmind 		assert(cg != NULL);
    571  1.18     rmind 		npf_rule_setprio(rl, NPF_PRI_LAST);
    572  1.18     rmind 		npf_rule_insert(npf_conf, cg, rl);
    573  1.18     rmind 	} else {
    574  1.18     rmind 		/* We have parsed a single rule - set it. */
    575  1.18     rmind 		the_rule = rl;
    576   1.1     rmind 	}
    577   1.1     rmind }
    578   1.1     rmind 
    579   1.1     rmind /*
    580  1.14     rmind  * npfctl_build_nat: create a single NAT policy of a specified
    581  1.13     rmind  * type with a given filter options.
    582  1.13     rmind  */
    583  1.36     rmind static nl_nat_t *
    584  1.36     rmind npfctl_build_nat(int type, const char *ifname, const addr_port_t *ap,
    585  1.36     rmind     const filt_opts_t *fopts, u_int flags)
    586  1.13     rmind {
    587  1.13     rmind 	const opt_proto_t op = { .op_proto = -1, .op_opts = NULL };
    588  1.36     rmind 	fam_addr_mask_t *am = npfctl_get_singlefam(ap->ap_netaddr);
    589  1.13     rmind 	in_port_t port;
    590  1.13     rmind 	nl_nat_t *nat;
    591  1.13     rmind 
    592  1.35     rmind 	if (ap->ap_portrange) {
    593  1.35     rmind 		port = npfctl_get_singleport(ap->ap_portrange);
    594  1.35     rmind 		flags &= ~NPF_NAT_PORTMAP;
    595  1.35     rmind 		flags |= NPF_NAT_PORTS;
    596  1.35     rmind 	} else {
    597  1.13     rmind 		port = 0;
    598  1.13     rmind 	}
    599  1.13     rmind 
    600  1.36     rmind 	nat = npf_nat_create(type, flags, ifname, am->fam_family,
    601  1.36     rmind 	    &am->fam_addr, am->fam_mask, port);
    602  1.36     rmind 	npfctl_build_code(nat, am->fam_family, &op, fopts);
    603  1.18     rmind 	npf_nat_insert(npf_conf, nat, NPF_PRI_LAST);
    604  1.36     rmind 	return nat;
    605  1.13     rmind }
    606  1.13     rmind 
    607  1.13     rmind /*
    608  1.14     rmind  * npfctl_build_natseg: validate and create NAT policies.
    609   1.1     rmind  */
    610   1.1     rmind void
    611  1.28     rmind npfctl_build_natseg(int sd, int type, const char *ifname,
    612  1.28     rmind     const addr_port_t *ap1, const addr_port_t *ap2,
    613  1.36     rmind     const filt_opts_t *fopts, u_int algo)
    614   1.1     rmind {
    615  1.36     rmind 	fam_addr_mask_t *am1 = NULL, *am2 = NULL;
    616  1.36     rmind 	nl_nat_t *nt1 = NULL, *nt2 = NULL;
    617   1.7     rmind 	filt_opts_t imfopts;
    618  1.36     rmind 	uint16_t adj = 0;
    619  1.35     rmind 	u_int flags;
    620  1.13     rmind 	bool binat;
    621   1.1     rmind 
    622  1.28     rmind 	assert(ifname != NULL);
    623   1.7     rmind 
    624  1.13     rmind 	/*
    625  1.13     rmind 	 * Bi-directional NAT is a combination of inbound NAT and outbound
    626  1.35     rmind 	 * NAT policies with the translation segments inverted respectively.
    627  1.13     rmind 	 */
    628  1.13     rmind 	binat = (NPF_NATIN | NPF_NATOUT) == type;
    629   1.7     rmind 
    630  1.35     rmind 	switch (sd) {
    631  1.35     rmind 	case NPFCTL_NAT_DYNAMIC:
    632  1.35     rmind 		/*
    633  1.35     rmind 		 * Dynamic NAT: traditional NAPT is expected.  Unless it
    634  1.35     rmind 		 * is bi-directional NAT, perform port mapping.
    635  1.35     rmind 		 */
    636  1.35     rmind 		flags = !binat ? (NPF_NAT_PORTS | NPF_NAT_PORTMAP) : 0;
    637  1.35     rmind 		break;
    638  1.35     rmind 	case NPFCTL_NAT_STATIC:
    639  1.35     rmind 		/* Static NAT: mechanic translation. */
    640  1.35     rmind 		flags = NPF_NAT_STATIC;
    641  1.35     rmind 		break;
    642  1.35     rmind 	default:
    643  1.35     rmind 		abort();
    644  1.35     rmind 	}
    645  1.35     rmind 
    646   1.7     rmind 	/*
    647  1.36     rmind 	 * Validate the mappings and their configuration.
    648  1.36     rmind 	 */
    649  1.36     rmind 
    650  1.36     rmind 	if ((type & NPF_NATIN) != 0) {
    651  1.36     rmind 		if (!ap1->ap_netaddr)
    652  1.36     rmind 			yyerror("inbound network segment is not specified");
    653  1.36     rmind 		am1 = npfctl_get_singlefam(ap1->ap_netaddr);
    654  1.36     rmind 	}
    655  1.36     rmind 	if ((type & NPF_NATOUT) != 0) {
    656  1.36     rmind 		if (!ap2->ap_netaddr)
    657  1.36     rmind 			yyerror("outbound network segment is not specified");
    658  1.36     rmind 		am2 = npfctl_get_singlefam(ap2->ap_netaddr);
    659  1.36     rmind 	}
    660  1.36     rmind 
    661  1.36     rmind 	switch (algo) {
    662  1.36     rmind 	case NPF_ALGO_NPT66:
    663  1.36     rmind 		if (am1 == NULL || am2 == NULL)
    664  1.36     rmind 			yyerror("1:1 mapping of two segments must be "
    665  1.36     rmind 			    "used for NPTv6");
    666  1.36     rmind 		if (am1->fam_mask != am2->fam_mask)
    667  1.36     rmind 			yyerror("asymmetric translation is not supported");
    668  1.36     rmind 		adj = npfctl_npt66_calcadj(am1->fam_mask,
    669  1.36     rmind 		    &am1->fam_addr, &am2->fam_addr);
    670  1.36     rmind 		break;
    671  1.36     rmind 	default:
    672  1.36     rmind 		if ((am1 && am1->fam_mask != NPF_NO_NETMASK) ||
    673  1.36     rmind 		    (am2 && am2->fam_mask != NPF_NO_NETMASK))
    674  1.36     rmind 			yyerror("net-to-net translation is not supported");
    675  1.36     rmind 		break;
    676  1.36     rmind 	}
    677  1.36     rmind 
    678  1.36     rmind 	/*
    679  1.13     rmind 	 * If the filter criteria is not specified explicitly, apply implicit
    680  1.14     rmind 	 * filtering according to the given network segments.
    681  1.13     rmind 	 *
    682  1.13     rmind 	 * Note: filled below, depending on the type.
    683   1.7     rmind 	 */
    684  1.14     rmind 	if (__predict_true(!fopts)) {
    685   1.7     rmind 		fopts = &imfopts;
    686   1.1     rmind 	}
    687   1.1     rmind 
    688  1.13     rmind 	if (type & NPF_NATIN) {
    689  1.13     rmind 		memset(&imfopts, 0, sizeof(filt_opts_t));
    690  1.13     rmind 		memcpy(&imfopts.fo_to, ap2, sizeof(addr_port_t));
    691  1.36     rmind 		nt1 = npfctl_build_nat(NPF_NATIN, ifname, ap1, fopts, flags);
    692  1.13     rmind 	}
    693  1.13     rmind 	if (type & NPF_NATOUT) {
    694  1.13     rmind 		memset(&imfopts, 0, sizeof(filt_opts_t));
    695  1.13     rmind 		memcpy(&imfopts.fo_from, ap1, sizeof(addr_port_t));
    696  1.36     rmind 		nt2 = npfctl_build_nat(NPF_NATOUT, ifname, ap2, fopts, flags);
    697  1.36     rmind 	}
    698  1.36     rmind 
    699  1.36     rmind 	if (algo == NPF_ALGO_NPT66) {
    700  1.36     rmind 		npf_nat_setnpt66(nt1, ~adj);
    701  1.36     rmind 		npf_nat_setnpt66(nt2, adj);
    702   1.1     rmind 	}
    703   1.1     rmind }
    704   1.1     rmind 
    705   1.1     rmind /*
    706   1.1     rmind  * npfctl_fill_table: fill NPF table with entries from a specified file.
    707   1.1     rmind  */
    708   1.1     rmind static void
    709  1.11     rmind npfctl_fill_table(nl_table_t *tl, u_int type, const char *fname)
    710   1.1     rmind {
    711  1.34  christos 	struct cdbw *cdbw = NULL;	/* XXX: gcc */
    712   1.1     rmind 	char *buf = NULL;
    713   1.1     rmind 	int l = 0;
    714   1.1     rmind 	FILE *fp;
    715   1.1     rmind 	size_t n;
    716   1.1     rmind 
    717  1.33     rmind 	if (type == NPF_TABLE_CDB && (cdbw = cdbw_open()) == NULL) {
    718  1.33     rmind 		err(EXIT_FAILURE, "cdbw_open");
    719  1.33     rmind 	}
    720   1.1     rmind 	fp = fopen(fname, "r");
    721   1.1     rmind 	if (fp == NULL) {
    722   1.1     rmind 		err(EXIT_FAILURE, "open '%s'", fname);
    723   1.1     rmind 	}
    724   1.1     rmind 	while (l++, getline(&buf, &n, fp) != -1) {
    725  1.11     rmind 		fam_addr_mask_t fam;
    726  1.11     rmind 		int alen;
    727   1.1     rmind 
    728   1.1     rmind 		if (*buf == '\n' || *buf == '#') {
    729   1.1     rmind 			continue;
    730   1.1     rmind 		}
    731  1.11     rmind 
    732  1.11     rmind 		if (!npfctl_parse_cidr(buf, &fam, &alen)) {
    733  1.11     rmind 			errx(EXIT_FAILURE,
    734  1.11     rmind 			    "%s:%d: invalid table entry", fname, l);
    735  1.11     rmind 		}
    736  1.33     rmind 		if (type != NPF_TABLE_TREE && fam.fam_mask != NPF_NO_NETMASK) {
    737  1.33     rmind 			errx(EXIT_FAILURE, "%s:%d: mask used with the "
    738  1.33     rmind 			    "non-tree table", fname, l);
    739   1.1     rmind 		}
    740   1.1     rmind 
    741  1.33     rmind 		/*
    742  1.33     rmind 		 * Create and add a table entry.
    743  1.33     rmind 		 */
    744  1.33     rmind 		if (type == NPF_TABLE_CDB) {
    745  1.33     rmind 			const npf_addr_t *addr = &fam.fam_addr;
    746  1.33     rmind 			if (cdbw_put(cdbw, addr, alen, addr, alen) == -1) {
    747  1.33     rmind 				err(EXIT_FAILURE, "cdbw_put");
    748  1.33     rmind 			}
    749  1.33     rmind 		} else {
    750  1.33     rmind 			npf_table_add_entry(tl, fam.fam_family,
    751  1.33     rmind 			    &fam.fam_addr, fam.fam_mask);
    752  1.33     rmind 		}
    753   1.1     rmind 	}
    754   1.1     rmind 	if (buf != NULL) {
    755   1.1     rmind 		free(buf);
    756   1.1     rmind 	}
    757  1.33     rmind 
    758  1.33     rmind 	if (type == NPF_TABLE_CDB) {
    759  1.33     rmind 		struct stat sb;
    760  1.33     rmind 		char sfn[32];
    761  1.33     rmind 		void *cdb;
    762  1.33     rmind 		int fd;
    763  1.33     rmind 
    764  1.41  christos 		strncpy(sfn, "/tmp/npfcdb.XXXXXX", sizeof(sfn));
    765  1.41  christos 		sfn[sizeof(sfn) - 1] = '\0';
    766  1.41  christos 
    767  1.33     rmind 		if ((fd = mkstemp(sfn)) == -1) {
    768  1.33     rmind 			err(EXIT_FAILURE, "mkstemp");
    769  1.33     rmind 		}
    770  1.33     rmind 		unlink(sfn);
    771  1.33     rmind 
    772  1.33     rmind 		if (cdbw_output(cdbw, fd, "npf-table-cdb", NULL) == -1) {
    773  1.33     rmind 			err(EXIT_FAILURE, "cdbw_output");
    774  1.33     rmind 		}
    775  1.33     rmind 		cdbw_close(cdbw);
    776  1.33     rmind 
    777  1.33     rmind 		if (fstat(fd, &sb) == -1) {
    778  1.33     rmind 			err(EXIT_FAILURE, "fstat");
    779  1.33     rmind 		}
    780  1.33     rmind 		if ((cdb = mmap(NULL, sb.st_size, PROT_READ,
    781  1.33     rmind 		    MAP_FILE | MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
    782  1.33     rmind 			err(EXIT_FAILURE, "mmap");
    783  1.33     rmind 		}
    784  1.33     rmind 		npf_table_setdata(tl, cdb, sb.st_size);
    785  1.33     rmind 
    786  1.33     rmind 		close(fd);
    787  1.33     rmind 	}
    788   1.1     rmind }
    789   1.1     rmind 
    790   1.1     rmind /*
    791   1.1     rmind  * npfctl_build_table: create an NPF table, add to the configuration and,
    792   1.1     rmind  * if required, fill with contents from a file.
    793   1.1     rmind  */
    794   1.1     rmind void
    795  1.29     rmind npfctl_build_table(const char *tname, u_int type, const char *fname)
    796   1.1     rmind {
    797  1.29     rmind 	static unsigned tid = 0;
    798   1.1     rmind 	nl_table_t *tl;
    799   1.1     rmind 
    800  1.29     rmind 	tl = npf_table_create(tname, tid++, type);
    801   1.1     rmind 	assert(tl != NULL);
    802   1.1     rmind 
    803   1.1     rmind 	if (npf_table_insert(npf_conf, tl)) {
    804  1.29     rmind 		yyerror("table '%s' is already defined", tname);
    805   1.1     rmind 	}
    806   1.1     rmind 
    807   1.1     rmind 	if (fname) {
    808  1.11     rmind 		npfctl_fill_table(tl, type, fname);
    809  1.33     rmind 	} else if (type == NPF_TABLE_CDB) {
    810  1.33     rmind 		errx(EXIT_FAILURE, "tables of cdb type must be static");
    811   1.1     rmind 	}
    812   1.1     rmind }
    813  1.23  christos 
    814  1.23  christos /*
    815  1.25     rmind  * npfctl_build_alg: create an NPF application level gateway and add it
    816  1.23  christos  * to the configuration.
    817  1.23  christos  */
    818  1.23  christos void
    819  1.23  christos npfctl_build_alg(const char *al_name)
    820  1.23  christos {
    821  1.23  christos 	if (_npf_alg_load(npf_conf, al_name) != 0) {
    822  1.23  christos 		errx(EXIT_FAILURE, "ALG '%s' already loaded", al_name);
    823  1.23  christos 	}
    824  1.23  christos }
    825  1.27     rmind 
    826  1.27     rmind static void
    827  1.27     rmind npfctl_dump_bpf(struct bpf_program *bf)
    828  1.27     rmind {
    829  1.27     rmind 	if (npf_debug) {
    830  1.27     rmind 		extern char *yytext;
    831  1.27     rmind 		extern int yylineno;
    832  1.27     rmind 
    833  1.27     rmind 		int rule_line = yylineno - (int)(*yytext == '\n');
    834  1.27     rmind 		printf("\nRULE AT LINE %d\n", rule_line);
    835  1.27     rmind 		bpf_dump(bf, 0);
    836  1.27     rmind 	}
    837  1.27     rmind }
    838