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