Home | History | Annotate | Line # | Download | only in npf
npf_ruleset.c revision 1.14.2.2
      1  1.14.2.2    tls /*	$NetBSD: npf_ruleset.c,v 1.14.2.2 2013/06/23 06:20:25 tls Exp $	*/
      2       1.1  rmind 
      3       1.1  rmind /*-
      4  1.14.2.1    tls  * Copyright (c) 2009-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  * NPF ruleset module.
     34       1.1  rmind  */
     35       1.1  rmind 
     36       1.1  rmind #include <sys/cdefs.h>
     37  1.14.2.2    tls __KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.14.2.2 2013/06/23 06:20:25 tls Exp $");
     38       1.1  rmind 
     39       1.1  rmind #include <sys/param.h>
     40      1.11  rmind #include <sys/types.h>
     41       1.1  rmind 
     42  1.14.2.2    tls #include <sys/atomic.h>
     43       1.1  rmind #include <sys/kmem.h>
     44       1.1  rmind #include <sys/queue.h>
     45  1.14.2.1    tls #include <sys/mbuf.h>
     46       1.1  rmind #include <sys/types.h>
     47       1.1  rmind 
     48  1.14.2.1    tls #include <net/bpf.h>
     49  1.14.2.2    tls #include <net/bpfjit.h>
     50       1.3  rmind #include <net/pfil.h>
     51       1.1  rmind #include <net/if.h>
     52       1.1  rmind 
     53       1.1  rmind #include "npf_ncode.h"
     54       1.1  rmind #include "npf_impl.h"
     55       1.1  rmind 
     56       1.4  rmind struct npf_ruleset {
     57  1.14.2.1    tls 	/*
     58  1.14.2.1    tls 	 * - List of all rules.
     59  1.14.2.1    tls 	 * - Dynamic (i.e. named) rules.
     60  1.14.2.1    tls 	 * - G/C list for convenience.
     61  1.14.2.1    tls 	 */
     62  1.14.2.1    tls 	LIST_HEAD(, npf_rule)	rs_all;
     63  1.14.2.1    tls 	LIST_HEAD(, npf_rule)	rs_dynamic;
     64  1.14.2.1    tls 	LIST_HEAD(, npf_rule)	rs_gc;
     65  1.14.2.1    tls 
     66  1.14.2.1    tls 	/* Unique ID counter. */
     67  1.14.2.1    tls 	uint64_t		rs_idcnt;
     68  1.14.2.1    tls 
     69  1.14.2.1    tls 	/* Number of array slots and active rules. */
     70  1.14.2.1    tls 	u_int			rs_slots;
     71  1.14.2.1    tls 	u_int			rs_nitems;
     72       1.4  rmind 
     73  1.14.2.1    tls 	/* Array of ordered rules. */
     74  1.14.2.1    tls 	npf_rule_t *		rs_rules[];
     75  1.14.2.1    tls };
     76       1.7  rmind 
     77       1.1  rmind struct npf_rule {
     78  1.14.2.1    tls 	/* Attributes, interface and skip slot. */
     79       1.4  rmind 	uint32_t		r_attr;
     80       1.4  rmind 	u_int			r_ifid;
     81  1.14.2.1    tls 	u_int			r_skip_to;
     82  1.14.2.1    tls 
     83  1.14.2.1    tls 	/* Code to process, if any. */
     84  1.14.2.1    tls 	int			r_type;
     85  1.14.2.2    tls 	bpfjit_function_t	r_jcode;
     86  1.14.2.1    tls 	void *			r_code;
     87  1.14.2.1    tls 	size_t			r_clen;
     88  1.14.2.1    tls 
     89  1.14.2.1    tls 	/* NAT policy (optional), rule procedure and subset. */
     90  1.14.2.1    tls 	npf_natpolicy_t *	r_natp;
     91       1.4  rmind 	npf_rproc_t *		r_rproc;
     92  1.14.2.1    tls 
     93  1.14.2.1    tls 	/* Rule priority: (highest) 1, 2 ... n (lowest). */
     94  1.14.2.1    tls 	pri_t			r_priority;
     95  1.14.2.1    tls 
     96  1.14.2.1    tls 	/*
     97  1.14.2.1    tls 	 * Dynamic group: subset queue and a dynamic group list entry.
     98  1.14.2.1    tls 	 * Dynamic rule: entry and the parent rule (the group).
     99  1.14.2.1    tls 	 */
    100  1.14.2.1    tls 	union {
    101  1.14.2.1    tls 		TAILQ_HEAD(npf_ruleq, npf_rule) r_subset;
    102  1.14.2.1    tls 		TAILQ_ENTRY(npf_rule)	r_entry;
    103  1.14.2.1    tls 	} /* C11 */;
    104  1.14.2.1    tls 	union {
    105  1.14.2.1    tls 		LIST_ENTRY(npf_rule)	r_dentry;
    106  1.14.2.1    tls 		npf_rule_t *		r_parent;
    107  1.14.2.1    tls 	} /* C11 */;
    108  1.14.2.1    tls 
    109  1.14.2.1    tls 	/* Rule ID and the original dictionary. */
    110  1.14.2.1    tls 	uint64_t		r_id;
    111  1.14.2.1    tls 	prop_dictionary_t	r_dict;
    112  1.14.2.1    tls 
    113  1.14.2.1    tls 	/* Rule name and all-list entry. */
    114  1.14.2.1    tls 	char			r_name[NPF_RULE_MAXNAMELEN];
    115  1.14.2.1    tls 	LIST_ENTRY(npf_rule)	r_aentry;
    116  1.14.2.1    tls 
    117  1.14.2.1    tls 	/* Key (optional). */
    118  1.14.2.1    tls 	uint8_t			r_key[NPF_RULE_MAXKEYLEN];
    119       1.1  rmind };
    120       1.1  rmind 
    121  1.14.2.1    tls #define	NPF_DYNAMIC_GROUP_P(attr) \
    122  1.14.2.1    tls     (((attr) & NPF_DYNAMIC_GROUP) == NPF_DYNAMIC_GROUP)
    123  1.14.2.1    tls 
    124  1.14.2.1    tls #define	NPF_DYNAMIC_RULE_P(attr) \
    125  1.14.2.1    tls     (((attr) & NPF_DYNAMIC_GROUP) == NPF_RULE_DYNAMIC)
    126  1.14.2.1    tls 
    127       1.1  rmind npf_ruleset_t *
    128  1.14.2.1    tls npf_ruleset_create(size_t slots)
    129       1.1  rmind {
    130  1.14.2.1    tls 	size_t len = offsetof(npf_ruleset_t, rs_rules[slots]);
    131       1.1  rmind 	npf_ruleset_t *rlset;
    132       1.1  rmind 
    133  1.14.2.1    tls 	rlset = kmem_zalloc(len, KM_SLEEP);
    134  1.14.2.1    tls 	LIST_INIT(&rlset->rs_dynamic);
    135  1.14.2.1    tls 	LIST_INIT(&rlset->rs_all);
    136  1.14.2.1    tls 	LIST_INIT(&rlset->rs_gc);
    137  1.14.2.1    tls 	rlset->rs_slots = slots;
    138  1.14.2.1    tls 
    139       1.1  rmind 	return rlset;
    140       1.1  rmind }
    141       1.1  rmind 
    142  1.14.2.1    tls static void
    143  1.14.2.1    tls npf_ruleset_unlink(npf_ruleset_t *rlset, npf_rule_t *rl)
    144  1.14.2.1    tls {
    145  1.14.2.1    tls 	if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
    146  1.14.2.1    tls 		LIST_REMOVE(rl, r_dentry);
    147  1.14.2.1    tls 	}
    148  1.14.2.1    tls 	if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
    149  1.14.2.1    tls 		npf_rule_t *rg = rl->r_parent;
    150  1.14.2.1    tls 		TAILQ_REMOVE(&rg->r_subset, rl, r_entry);
    151  1.14.2.1    tls 	}
    152  1.14.2.1    tls 	LIST_REMOVE(rl, r_aentry);
    153  1.14.2.1    tls }
    154  1.14.2.1    tls 
    155       1.1  rmind void
    156       1.1  rmind npf_ruleset_destroy(npf_ruleset_t *rlset)
    157       1.1  rmind {
    158  1.14.2.1    tls 	size_t len = offsetof(npf_ruleset_t, rs_rules[rlset->rs_slots]);
    159       1.1  rmind 	npf_rule_t *rl;
    160       1.1  rmind 
    161  1.14.2.1    tls 	while ((rl = LIST_FIRST(&rlset->rs_all)) != NULL) {
    162  1.14.2.1    tls 		npf_ruleset_unlink(rlset, rl);
    163       1.1  rmind 		npf_rule_free(rl);
    164       1.1  rmind 	}
    165  1.14.2.1    tls 	KASSERT(LIST_EMPTY(&rlset->rs_dynamic));
    166  1.14.2.1    tls 	KASSERT(LIST_EMPTY(&rlset->rs_gc));
    167  1.14.2.1    tls 	kmem_free(rlset, len);
    168       1.1  rmind }
    169       1.1  rmind 
    170       1.1  rmind /*
    171       1.1  rmind  * npf_ruleset_insert: insert the rule into the specified ruleset.
    172       1.1  rmind  */
    173       1.1  rmind void
    174       1.1  rmind npf_ruleset_insert(npf_ruleset_t *rlset, npf_rule_t *rl)
    175       1.1  rmind {
    176  1.14.2.1    tls 	u_int n = rlset->rs_nitems;
    177  1.14.2.1    tls 
    178  1.14.2.1    tls 	KASSERT(n < rlset->rs_slots);
    179  1.14.2.1    tls 
    180  1.14.2.1    tls 	LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
    181  1.14.2.1    tls 	if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
    182  1.14.2.1    tls 		LIST_INSERT_HEAD(&rlset->rs_dynamic, rl, r_dentry);
    183  1.14.2.1    tls 	}
    184  1.14.2.1    tls 
    185  1.14.2.1    tls 	rlset->rs_rules[n] = rl;
    186  1.14.2.1    tls 	rlset->rs_nitems++;
    187       1.1  rmind 
    188  1.14.2.1    tls 	if (rl->r_skip_to < ++n) {
    189  1.14.2.1    tls 		rl->r_skip_to = n;
    190  1.14.2.1    tls 	}
    191  1.14.2.1    tls }
    192  1.14.2.1    tls 
    193  1.14.2.1    tls static npf_rule_t *
    194  1.14.2.1    tls npf_ruleset_lookup(npf_ruleset_t *rlset, const char *name)
    195  1.14.2.1    tls {
    196  1.14.2.1    tls 	npf_rule_t *rl;
    197  1.14.2.1    tls 
    198  1.14.2.1    tls 	KASSERT(npf_config_locked_p());
    199  1.14.2.1    tls 
    200  1.14.2.1    tls 	LIST_FOREACH(rl, &rlset->rs_dynamic, r_dentry) {
    201  1.14.2.1    tls 		KASSERT(NPF_DYNAMIC_GROUP_P(rl->r_attr));
    202  1.14.2.1    tls 		if (strncmp(rl->r_name, name, NPF_RULE_MAXNAMELEN) == 0)
    203       1.1  rmind 			break;
    204       1.1  rmind 	}
    205  1.14.2.1    tls 	return rl;
    206  1.14.2.1    tls }
    207  1.14.2.1    tls 
    208  1.14.2.1    tls int
    209  1.14.2.1    tls npf_ruleset_add(npf_ruleset_t *rlset, const char *rname, npf_rule_t *rl)
    210  1.14.2.1    tls {
    211  1.14.2.1    tls 	npf_rule_t *rg, *it;
    212  1.14.2.1    tls 	pri_t priocmd;
    213  1.14.2.1    tls 
    214  1.14.2.1    tls 	rg = npf_ruleset_lookup(rlset, rname);
    215  1.14.2.1    tls 	if (rg == NULL) {
    216  1.14.2.1    tls 		return ESRCH;
    217  1.14.2.1    tls 	}
    218  1.14.2.1    tls 	if (!NPF_DYNAMIC_RULE_P(rl->r_attr)) {
    219  1.14.2.1    tls 		return EINVAL;
    220  1.14.2.1    tls 	}
    221  1.14.2.1    tls 
    222  1.14.2.1    tls 	/* Dynamic rule - assign a unique ID and save the parent. */
    223  1.14.2.1    tls 	rl->r_id = ++rlset->rs_idcnt;
    224  1.14.2.1    tls 	rl->r_parent = rg;
    225  1.14.2.1    tls 
    226  1.14.2.1    tls 	/*
    227  1.14.2.1    tls 	 * Rule priority: (highest) 1, 2 ... n (lowest).
    228  1.14.2.1    tls 	 * Negative priority indicates an operation and is reset to zero.
    229  1.14.2.1    tls 	 */
    230  1.14.2.1    tls 	if ((priocmd = rl->r_priority) < 0) {
    231  1.14.2.1    tls 		rl->r_priority = 0;
    232  1.14.2.1    tls 	}
    233  1.14.2.1    tls 
    234  1.14.2.1    tls 	switch (priocmd) {
    235  1.14.2.1    tls 	case NPF_PRI_FIRST:
    236  1.14.2.1    tls 		TAILQ_FOREACH(it, &rg->r_subset, r_entry) {
    237  1.14.2.1    tls 			if (rl->r_priority <= it->r_priority)
    238  1.14.2.1    tls 				break;
    239  1.14.2.1    tls 		}
    240  1.14.2.1    tls 		if (it) {
    241  1.14.2.1    tls 			TAILQ_INSERT_BEFORE(it, rl, r_entry);
    242  1.14.2.1    tls 		} else {
    243  1.14.2.1    tls 			TAILQ_INSERT_HEAD(&rg->r_subset, rl, r_entry);
    244  1.14.2.1    tls 		}
    245  1.14.2.1    tls 		break;
    246  1.14.2.1    tls 	case NPF_PRI_LAST:
    247  1.14.2.1    tls 	default:
    248  1.14.2.1    tls 		TAILQ_FOREACH(it, &rg->r_subset, r_entry) {
    249  1.14.2.1    tls 			if (rl->r_priority < it->r_priority)
    250  1.14.2.1    tls 				break;
    251  1.14.2.1    tls 		}
    252  1.14.2.1    tls 		if (it) {
    253  1.14.2.1    tls 			TAILQ_INSERT_BEFORE(it, rl, r_entry);
    254  1.14.2.1    tls 		} else {
    255  1.14.2.1    tls 			TAILQ_INSERT_TAIL(&rg->r_subset, rl, r_entry);
    256  1.14.2.1    tls 		}
    257  1.14.2.1    tls 		break;
    258  1.14.2.1    tls 	}
    259  1.14.2.1    tls 
    260  1.14.2.1    tls 	/* Finally, add into the all-list. */
    261  1.14.2.1    tls 	LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
    262  1.14.2.1    tls 	return 0;
    263  1.14.2.1    tls }
    264  1.14.2.1    tls 
    265  1.14.2.1    tls int
    266  1.14.2.1    tls npf_ruleset_remove(npf_ruleset_t *rlset, const char *rname, uint64_t id)
    267  1.14.2.1    tls {
    268  1.14.2.1    tls 	npf_rule_t *rg, *rl;
    269  1.14.2.1    tls 
    270  1.14.2.1    tls 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    271  1.14.2.1    tls 		return ESRCH;
    272  1.14.2.1    tls 	}
    273  1.14.2.1    tls 	TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
    274  1.14.2.1    tls 		/* Compare ID.  On match, remove and return. */
    275  1.14.2.1    tls 		if (rl->r_id == id) {
    276  1.14.2.1    tls 			npf_ruleset_unlink(rlset, rl);
    277  1.14.2.1    tls 			LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
    278  1.14.2.1    tls 			return 0;
    279  1.14.2.1    tls 		}
    280  1.14.2.1    tls 	}
    281  1.14.2.1    tls 	return ENOENT;
    282  1.14.2.1    tls }
    283  1.14.2.1    tls 
    284  1.14.2.1    tls int
    285  1.14.2.1    tls npf_ruleset_remkey(npf_ruleset_t *rlset, const char *rname,
    286  1.14.2.1    tls     const void *key, size_t len)
    287  1.14.2.1    tls {
    288  1.14.2.1    tls 	npf_rule_t *rg, *rl;
    289  1.14.2.1    tls 
    290  1.14.2.1    tls 	KASSERT(len && len <= NPF_RULE_MAXKEYLEN);
    291  1.14.2.1    tls 
    292  1.14.2.1    tls 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    293  1.14.2.1    tls 		return ESRCH;
    294  1.14.2.1    tls 	}
    295  1.14.2.1    tls 
    296  1.14.2.1    tls 	/* Find the last in the list. */
    297  1.14.2.1    tls 	TAILQ_FOREACH_REVERSE(rl, &rg->r_subset, npf_ruleq, r_entry) {
    298  1.14.2.1    tls 		/* Compare the key.  On match, remove and return. */
    299  1.14.2.1    tls 		if (memcmp(rl->r_key, key, len) == 0) {
    300  1.14.2.1    tls 			npf_ruleset_unlink(rlset, rl);
    301  1.14.2.1    tls 			LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
    302  1.14.2.1    tls 			return 0;
    303  1.14.2.1    tls 		}
    304  1.14.2.1    tls 	}
    305  1.14.2.1    tls 	return ENOENT;
    306  1.14.2.1    tls }
    307  1.14.2.1    tls 
    308  1.14.2.1    tls prop_dictionary_t
    309  1.14.2.1    tls npf_ruleset_list(npf_ruleset_t *rlset, const char *rname)
    310  1.14.2.1    tls {
    311  1.14.2.1    tls 	prop_dictionary_t rldict;
    312  1.14.2.1    tls 	prop_array_t rules;
    313  1.14.2.1    tls 	npf_rule_t *rg, *rl;
    314  1.14.2.1    tls 
    315  1.14.2.1    tls 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    316  1.14.2.1    tls 		return NULL;
    317  1.14.2.1    tls 	}
    318  1.14.2.1    tls 	if ((rldict = prop_dictionary_create()) == NULL) {
    319  1.14.2.1    tls 		return NULL;
    320  1.14.2.1    tls 	}
    321  1.14.2.1    tls 	if ((rules = prop_array_create()) == NULL) {
    322  1.14.2.1    tls 		prop_object_release(rldict);
    323  1.14.2.1    tls 		return NULL;
    324  1.14.2.1    tls 	}
    325  1.14.2.1    tls 
    326  1.14.2.1    tls 	TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
    327  1.14.2.1    tls 		if (rl->r_dict && !prop_array_add(rules, rl->r_dict)) {
    328  1.14.2.1    tls 			prop_object_release(rldict);
    329  1.14.2.1    tls 			prop_object_release(rules);
    330  1.14.2.1    tls 			return NULL;
    331  1.14.2.1    tls 		}
    332  1.14.2.1    tls 	}
    333  1.14.2.1    tls 
    334  1.14.2.1    tls 	if (!prop_dictionary_set(rldict, "rules", rules)) {
    335  1.14.2.1    tls 		prop_object_release(rldict);
    336  1.14.2.1    tls 		rldict = NULL;
    337  1.14.2.1    tls 	}
    338  1.14.2.1    tls 	prop_object_release(rules);
    339  1.14.2.1    tls 	return rldict;
    340  1.14.2.1    tls }
    341  1.14.2.1    tls 
    342  1.14.2.1    tls int
    343  1.14.2.1    tls npf_ruleset_flush(npf_ruleset_t *rlset, const char *rname)
    344  1.14.2.1    tls {
    345  1.14.2.1    tls 	npf_rule_t *rg, *rl;
    346  1.14.2.1    tls 
    347  1.14.2.1    tls 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    348  1.14.2.1    tls 		return ESRCH;
    349  1.14.2.1    tls 	}
    350  1.14.2.1    tls 	while ((rl = TAILQ_FIRST(&rg->r_subset)) != NULL) {
    351  1.14.2.1    tls 		npf_ruleset_unlink(rlset, rl);
    352  1.14.2.1    tls 		LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
    353  1.14.2.1    tls 	}
    354  1.14.2.1    tls 	return 0;
    355  1.14.2.1    tls }
    356  1.14.2.1    tls 
    357  1.14.2.1    tls void
    358  1.14.2.1    tls npf_ruleset_gc(npf_ruleset_t *rlset)
    359  1.14.2.1    tls {
    360  1.14.2.1    tls 	npf_rule_t *rl;
    361  1.14.2.1    tls 
    362  1.14.2.1    tls 	while ((rl = LIST_FIRST(&rlset->rs_gc)) != NULL) {
    363  1.14.2.1    tls 		LIST_REMOVE(rl, r_aentry);
    364  1.14.2.1    tls 		npf_rule_free(rl);
    365  1.14.2.1    tls 	}
    366  1.14.2.1    tls }
    367  1.14.2.1    tls 
    368  1.14.2.1    tls /*
    369  1.14.2.1    tls  * npf_ruleset_reload: share the dynamic rules.
    370  1.14.2.1    tls  *
    371  1.14.2.1    tls  * => Active ruleset should be exclusively locked.
    372  1.14.2.1    tls  */
    373  1.14.2.1    tls void
    374  1.14.2.1    tls npf_ruleset_reload(npf_ruleset_t *rlset, npf_ruleset_t *arlset)
    375  1.14.2.1    tls {
    376  1.14.2.1    tls 	npf_rule_t *rg;
    377  1.14.2.1    tls 
    378  1.14.2.1    tls 	KASSERT(npf_config_locked_p());
    379  1.14.2.1    tls 
    380  1.14.2.1    tls 	LIST_FOREACH(rg, &rlset->rs_dynamic, r_dentry) {
    381  1.14.2.1    tls 		npf_rule_t *arg, *rl;
    382  1.14.2.1    tls 
    383  1.14.2.1    tls 		if ((arg = npf_ruleset_lookup(arlset, rg->r_name)) == NULL) {
    384  1.14.2.1    tls 			continue;
    385  1.14.2.1    tls 		}
    386  1.14.2.1    tls 
    387  1.14.2.1    tls 		/*
    388  1.14.2.1    tls 		 * Copy the list-head structure and move the rules from the
    389  1.14.2.1    tls 		 * old ruleset to the new by reinserting to a new all-rules
    390  1.14.2.1    tls 		 * list and resetting the parent rule.  Note that the rules
    391  1.14.2.1    tls 		 * are still active and therefore accessible for inspection
    392  1.14.2.1    tls 		 * via the old ruleset.
    393  1.14.2.1    tls 		 */
    394  1.14.2.1    tls 		memcpy(&rg->r_subset, &arg->r_subset, sizeof(rg->r_subset));
    395  1.14.2.1    tls 		TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
    396  1.14.2.1    tls 			LIST_REMOVE(rl, r_aentry);
    397  1.14.2.1    tls 			LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
    398  1.14.2.1    tls 			rl->r_parent = rg;
    399  1.14.2.1    tls 		}
    400       1.1  rmind 	}
    401  1.14.2.1    tls 
    402  1.14.2.1    tls 	/* Inherit the ID counter. */
    403  1.14.2.1    tls 	rlset->rs_idcnt = arlset->rs_idcnt;
    404       1.1  rmind }
    405       1.1  rmind 
    406       1.1  rmind /*
    407       1.4  rmind  * npf_ruleset_matchnat: find a matching NAT policy in the ruleset.
    408       1.1  rmind  */
    409       1.4  rmind npf_rule_t *
    410       1.4  rmind npf_ruleset_matchnat(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
    411       1.1  rmind {
    412       1.4  rmind 	npf_rule_t *rl;
    413       1.1  rmind 
    414       1.4  rmind 	/* Find a matching NAT policy in the old ruleset. */
    415  1.14.2.1    tls 	LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
    416       1.4  rmind 		if (npf_nat_matchpolicy(rl->r_natp, mnp))
    417       1.4  rmind 			break;
    418       1.4  rmind 	}
    419       1.4  rmind 	return rl;
    420       1.1  rmind }
    421       1.1  rmind 
    422       1.6  rmind npf_rule_t *
    423       1.6  rmind npf_ruleset_sharepm(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
    424       1.6  rmind {
    425       1.6  rmind 	npf_natpolicy_t *np;
    426       1.6  rmind 	npf_rule_t *rl;
    427       1.6  rmind 
    428       1.6  rmind 	/* Find a matching NAT policy in the old ruleset. */
    429  1.14.2.1    tls 	LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
    430       1.6  rmind 		/*
    431       1.6  rmind 		 * NAT policy might not yet be set during the creation of
    432       1.6  rmind 		 * the ruleset (in such case, rule is for our policy), or
    433       1.6  rmind 		 * policies might be equal due to rule exchange on reload.
    434       1.6  rmind 		 */
    435       1.6  rmind 		np = rl->r_natp;
    436       1.6  rmind 		if (np == NULL || np == mnp)
    437       1.6  rmind 			continue;
    438       1.6  rmind 		if (npf_nat_sharepm(np, mnp))
    439       1.6  rmind 			break;
    440       1.6  rmind 	}
    441       1.6  rmind 	return rl;
    442       1.6  rmind }
    443       1.6  rmind 
    444       1.1  rmind /*
    445      1.13  rmind  * npf_ruleset_freealg: inspect the ruleset and disassociate specified
    446      1.13  rmind  * ALG from all NAT entries using it.
    447      1.13  rmind  */
    448      1.13  rmind void
    449      1.13  rmind npf_ruleset_freealg(npf_ruleset_t *rlset, npf_alg_t *alg)
    450      1.13  rmind {
    451      1.13  rmind 	npf_rule_t *rl;
    452  1.14.2.1    tls 	npf_natpolicy_t *np;
    453      1.13  rmind 
    454  1.14.2.1    tls 	LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
    455  1.14.2.1    tls 		if ((np = rl->r_natp) != NULL) {
    456      1.13  rmind 			npf_nat_freealg(np, alg);
    457      1.13  rmind 		}
    458      1.13  rmind 	}
    459      1.13  rmind }
    460      1.13  rmind 
    461      1.13  rmind /*
    462       1.4  rmind  * npf_ruleset_natreload: minimum reload of NAT policies by maching
    463       1.6  rmind  * two (active and new) NAT rulesets.
    464       1.4  rmind  *
    465       1.4  rmind  * => Active ruleset should be exclusively locked.
    466       1.1  rmind  */
    467       1.4  rmind void
    468       1.4  rmind npf_ruleset_natreload(npf_ruleset_t *nrlset, npf_ruleset_t *arlset)
    469       1.1  rmind {
    470       1.4  rmind 	npf_natpolicy_t *np, *anp;
    471       1.4  rmind 	npf_rule_t *rl, *arl;
    472       1.4  rmind 
    473       1.4  rmind 	/* Scan a new NAT ruleset against NAT policies in old ruleset. */
    474  1.14.2.1    tls 	LIST_FOREACH(rl, &nrlset->rs_all, r_aentry) {
    475       1.4  rmind 		np = rl->r_natp;
    476       1.4  rmind 		arl = npf_ruleset_matchnat(arlset, np);
    477       1.4  rmind 		if (arl == NULL) {
    478       1.4  rmind 			continue;
    479       1.4  rmind 		}
    480       1.4  rmind 		/* On match - we exchange NAT policies. */
    481       1.4  rmind 		anp = arl->r_natp;
    482       1.4  rmind 		rl->r_natp = anp;
    483       1.4  rmind 		arl->r_natp = np;
    484       1.6  rmind 		/* Update other NAT policies to share portmap. */
    485       1.6  rmind 		(void)npf_ruleset_sharepm(nrlset, anp);
    486       1.1  rmind 	}
    487       1.4  rmind }
    488       1.4  rmind 
    489       1.1  rmind /*
    490       1.6  rmind  * npf_rule_alloc: allocate a rule and copy n-code from user-space.
    491       1.1  rmind  */
    492       1.4  rmind npf_rule_t *
    493  1.14.2.1    tls npf_rule_alloc(prop_dictionary_t rldict)
    494       1.1  rmind {
    495       1.4  rmind 	npf_rule_t *rl;
    496       1.7  rmind 	const char *rname;
    497       1.1  rmind 
    498       1.4  rmind 	/* Allocate a rule structure. */
    499      1.11  rmind 	rl = kmem_zalloc(sizeof(npf_rule_t), KM_SLEEP);
    500  1.14.2.1    tls 	TAILQ_INIT(&rl->r_subset);
    501       1.4  rmind 	rl->r_natp = NULL;
    502       1.4  rmind 
    503      1.11  rmind 	/* Name (optional) */
    504       1.7  rmind 	if (prop_dictionary_get_cstring_nocopy(rldict, "name", &rname)) {
    505  1.14.2.1    tls 		strlcpy(rl->r_name, rname, NPF_RULE_MAXNAMELEN);
    506       1.7  rmind 	} else {
    507       1.7  rmind 		rl->r_name[0] = '\0';
    508       1.7  rmind 	}
    509       1.7  rmind 
    510      1.11  rmind 	/* Attributes, priority and interface ID (optional). */
    511       1.7  rmind 	prop_dictionary_get_uint32(rldict, "attributes", &rl->r_attr);
    512       1.7  rmind 	prop_dictionary_get_int32(rldict, "priority", &rl->r_priority);
    513       1.7  rmind 	prop_dictionary_get_uint32(rldict, "interface", &rl->r_ifid);
    514       1.4  rmind 
    515  1.14.2.1    tls 	/* Get the skip-to index.  No need to validate it. */
    516  1.14.2.1    tls 	prop_dictionary_get_uint32(rldict, "skip-to", &rl->r_skip_to);
    517  1.14.2.1    tls 
    518  1.14.2.1    tls 	/* Key (optional). */
    519  1.14.2.1    tls 	prop_object_t obj = prop_dictionary_get(rldict, "key");
    520  1.14.2.1    tls 	const void *key = prop_data_data_nocopy(obj);
    521  1.14.2.1    tls 
    522  1.14.2.1    tls 	if (key) {
    523  1.14.2.1    tls 		size_t len = prop_data_size(obj);
    524  1.14.2.1    tls 		if (len > NPF_RULE_MAXKEYLEN) {
    525  1.14.2.1    tls 			kmem_free(rl, sizeof(npf_rule_t));
    526  1.14.2.1    tls 			return NULL;
    527  1.14.2.1    tls 		}
    528  1.14.2.1    tls 		memcpy(rl->r_key, key, len);
    529  1.14.2.1    tls 	}
    530  1.14.2.1    tls 
    531  1.14.2.1    tls 	if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
    532  1.14.2.1    tls 		rl->r_dict = prop_dictionary_copy(rldict);
    533       1.4  rmind 	}
    534       1.6  rmind 
    535       1.4  rmind 	return rl;
    536       1.1  rmind }
    537       1.1  rmind 
    538       1.1  rmind /*
    539  1.14.2.1    tls  * npf_rule_setcode: assign filter code to the rule.
    540  1.14.2.1    tls  *
    541  1.14.2.2    tls  * => The code must be validated by the caller.
    542  1.14.2.2    tls  * => JIT compilation may be performed here.
    543  1.14.2.1    tls  */
    544  1.14.2.1    tls void
    545  1.14.2.1    tls npf_rule_setcode(npf_rule_t *rl, const int type, void *code, size_t size)
    546  1.14.2.1    tls {
    547  1.14.2.2    tls 	/* Perform BPF JIT if possible. */
    548  1.14.2.2    tls 	if (type == NPF_CODE_BPF && (membar_consumer(),
    549  1.14.2.2    tls 	    bpfjit_module_ops.bj_generate_code != NULL)) {
    550  1.14.2.2    tls 		KASSERT(rl->r_jcode == NULL);
    551  1.14.2.2    tls 		rl->r_jcode = bpfjit_module_ops.bj_generate_code(code, size);
    552  1.14.2.2    tls 	}
    553  1.14.2.1    tls 	rl->r_type = type;
    554  1.14.2.1    tls 	rl->r_code = code;
    555  1.14.2.1    tls 	rl->r_clen = size;
    556  1.14.2.1    tls }
    557  1.14.2.1    tls 
    558  1.14.2.1    tls /*
    559  1.14.2.1    tls  * npf_rule_setrproc: assign a rule procedure and hold a reference on it.
    560  1.14.2.1    tls  */
    561  1.14.2.1    tls void
    562  1.14.2.1    tls npf_rule_setrproc(npf_rule_t *rl, npf_rproc_t *rp)
    563  1.14.2.1    tls {
    564  1.14.2.1    tls 	npf_rproc_acquire(rp);
    565  1.14.2.1    tls 	rl->r_rproc = rp;
    566  1.14.2.1    tls }
    567  1.14.2.1    tls 
    568  1.14.2.1    tls /*
    569       1.1  rmind  * npf_rule_free: free the specified rule.
    570       1.1  rmind  */
    571       1.1  rmind void
    572       1.1  rmind npf_rule_free(npf_rule_t *rl)
    573       1.1  rmind {
    574       1.4  rmind 	npf_natpolicy_t *np = rl->r_natp;
    575       1.4  rmind 	npf_rproc_t *rp = rl->r_rproc;
    576       1.1  rmind 
    577       1.4  rmind 	if (np) {
    578       1.4  rmind 		/* Free NAT policy. */
    579       1.4  rmind 		npf_nat_freepolicy(np);
    580       1.4  rmind 	}
    581       1.4  rmind 	if (rp) {
    582       1.6  rmind 		/* Release rule procedure. */
    583       1.4  rmind 		npf_rproc_release(rp);
    584       1.4  rmind 	}
    585  1.14.2.1    tls 	if (rl->r_code) {
    586  1.14.2.2    tls 		/* Free byte-code. */
    587  1.14.2.1    tls 		kmem_free(rl->r_code, rl->r_clen);
    588  1.14.2.1    tls 	}
    589  1.14.2.2    tls 	if (rl->r_jcode) {
    590  1.14.2.2    tls 		/* Free JIT code. */
    591  1.14.2.2    tls 		KASSERT(bpfjit_module_ops.bj_free_code != NULL);
    592  1.14.2.2    tls 		bpfjit_module_ops.bj_free_code(rl->r_jcode);
    593  1.14.2.2    tls 	}
    594  1.14.2.1    tls 	if (rl->r_dict) {
    595  1.14.2.1    tls 		/* Destroy the dictionary. */
    596  1.14.2.1    tls 		prop_object_release(rl->r_dict);
    597       1.1  rmind 	}
    598       1.4  rmind 	kmem_free(rl, sizeof(npf_rule_t));
    599       1.1  rmind }
    600       1.1  rmind 
    601       1.1  rmind /*
    602  1.14.2.1    tls  * npf_rule_getid: return the unique ID of a rule.
    603      1.10  rmind  * npf_rule_getrproc: acquire a reference and return rule procedure, if any.
    604       1.1  rmind  * npf_rule_getnat: get NAT policy assigned to the rule.
    605       1.1  rmind  */
    606       1.1  rmind 
    607  1.14.2.1    tls uint64_t
    608  1.14.2.1    tls npf_rule_getid(const npf_rule_t *rl)
    609       1.1  rmind {
    610  1.14.2.1    tls 	KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    611  1.14.2.1    tls 	return rl->r_id;
    612       1.1  rmind }
    613       1.1  rmind 
    614      1.10  rmind npf_rproc_t *
    615      1.10  rmind npf_rule_getrproc(npf_rule_t *rl)
    616      1.10  rmind {
    617      1.10  rmind 	npf_rproc_t *rp = rl->r_rproc;
    618      1.10  rmind 
    619      1.10  rmind 	if (rp) {
    620      1.10  rmind 		npf_rproc_acquire(rp);
    621      1.10  rmind 	}
    622      1.10  rmind 	return rp;
    623      1.10  rmind }
    624      1.10  rmind 
    625       1.1  rmind npf_natpolicy_t *
    626       1.1  rmind npf_rule_getnat(const npf_rule_t *rl)
    627       1.1  rmind {
    628       1.4  rmind 	return rl->r_natp;
    629       1.1  rmind }
    630       1.1  rmind 
    631       1.4  rmind /*
    632       1.4  rmind  * npf_rule_setnat: assign NAT policy to the rule and insert into the
    633       1.4  rmind  * NAT policy list in the ruleset.
    634       1.4  rmind  */
    635       1.1  rmind void
    636       1.1  rmind npf_rule_setnat(npf_rule_t *rl, npf_natpolicy_t *np)
    637       1.1  rmind {
    638       1.3  rmind 
    639       1.4  rmind 	KASSERT(rl->r_natp == NULL);
    640       1.4  rmind 	rl->r_natp = np;
    641       1.1  rmind }
    642       1.1  rmind 
    643  1.14.2.1    tls /*
    644  1.14.2.1    tls  * npf_rule_inspect: match the interface, direction and run the filter code.
    645  1.14.2.1    tls  * Returns true if rule matches, false otherise.
    646  1.14.2.1    tls  */
    647  1.14.2.1    tls static inline bool
    648  1.14.2.1    tls npf_rule_inspect(npf_cache_t *npc, nbuf_t *nbuf, const npf_rule_t *rl,
    649  1.14.2.1    tls     const int di_mask, const int layer)
    650       1.7  rmind {
    651  1.14.2.1    tls 	const ifnet_t *ifp = nbuf->nb_ifp;
    652  1.14.2.1    tls 	const void *code;
    653       1.7  rmind 
    654  1.14.2.1    tls 	/* Match the interface. */
    655  1.14.2.1    tls 	if (rl->r_ifid && rl->r_ifid != ifp->if_index) {
    656  1.14.2.1    tls 		return false;
    657  1.14.2.1    tls 	}
    658  1.14.2.1    tls 
    659  1.14.2.1    tls 	/* Match the direction. */
    660  1.14.2.1    tls 	if ((rl->r_attr & NPF_RULE_DIMASK) != NPF_RULE_DIMASK) {
    661  1.14.2.1    tls 		if ((rl->r_attr & di_mask) == 0)
    662  1.14.2.1    tls 			return false;
    663  1.14.2.1    tls 	}
    664  1.14.2.1    tls 
    665  1.14.2.2    tls 	/* Execute JIT code, if any. */
    666  1.14.2.2    tls 	if (__predict_true(rl->r_jcode)) {
    667  1.14.2.2    tls 		struct mbuf *m = nbuf_head_mbuf(nbuf);
    668  1.14.2.2    tls 		size_t pktlen = m_length(m);
    669  1.14.2.2    tls 
    670  1.14.2.2    tls 		return rl->r_jcode((unsigned char *)m, pktlen, 0) != 0;
    671  1.14.2.2    tls 	}
    672  1.14.2.2    tls 
    673  1.14.2.2    tls 	/* Execute the byte-code, if any. */
    674  1.14.2.1    tls 	if ((code = rl->r_code) == NULL) {
    675  1.14.2.1    tls 		return true;
    676  1.14.2.1    tls 	}
    677  1.14.2.1    tls 
    678  1.14.2.1    tls 	switch (rl->r_type) {
    679  1.14.2.1    tls 	case NPF_CODE_NC:
    680  1.14.2.1    tls 		return npf_ncode_process(npc, code, nbuf, layer) == 0;
    681  1.14.2.1    tls 	case NPF_CODE_BPF: {
    682  1.14.2.1    tls 		struct mbuf *m = nbuf_head_mbuf(nbuf);
    683  1.14.2.1    tls 		size_t pktlen = m_length(m);
    684  1.14.2.1    tls 		return bpf_filter(code, (unsigned char *)m, pktlen, 0) != 0;
    685  1.14.2.1    tls 	}
    686  1.14.2.1    tls 	default:
    687  1.14.2.1    tls 		KASSERT(false);
    688  1.14.2.1    tls 	}
    689  1.14.2.1    tls 	return false;
    690  1.14.2.1    tls }
    691  1.14.2.1    tls 
    692  1.14.2.1    tls /*
    693  1.14.2.1    tls  * npf_rule_reinspect: re-inspect the dynamic rule by iterating its list.
    694  1.14.2.1    tls  * This is only for the dynamic rules.  Subrules cannot have nested rules.
    695  1.14.2.1    tls  */
    696  1.14.2.1    tls static npf_rule_t *
    697  1.14.2.1    tls npf_rule_reinspect(npf_cache_t *npc, nbuf_t *nbuf, const npf_rule_t *drl,
    698  1.14.2.1    tls     const int di_mask, const int layer)
    699  1.14.2.1    tls {
    700  1.14.2.1    tls 	npf_rule_t *final_rl = NULL, *rl;
    701  1.14.2.1    tls 
    702  1.14.2.1    tls 	KASSERT(NPF_DYNAMIC_GROUP_P(drl->r_attr));
    703  1.14.2.1    tls 
    704  1.14.2.1    tls 	TAILQ_FOREACH(rl, &drl->r_subset, r_entry) {
    705  1.14.2.1    tls 		if (!npf_rule_inspect(npc, nbuf, rl, di_mask, layer)) {
    706       1.7  rmind 			continue;
    707  1.14.2.1    tls 		}
    708  1.14.2.1    tls 		if (rl->r_attr & NPF_RULE_FINAL) {
    709  1.14.2.1    tls 			return rl;
    710  1.14.2.1    tls 		}
    711  1.14.2.1    tls 		final_rl = rl;
    712       1.7  rmind 	}
    713  1.14.2.1    tls 	return final_rl;
    714       1.7  rmind }
    715       1.1  rmind 
    716       1.1  rmind /*
    717       1.7  rmind  * npf_ruleset_inspect: inspect the packet against the given ruleset.
    718       1.1  rmind  *
    719       1.7  rmind  * Loop through the rules in the set and run n-code processor of each rule
    720       1.7  rmind  * against the packet (nbuf chain).  If sub-ruleset is found, inspect it.
    721       1.7  rmind  *
    722       1.9  rmind  * => Caller is responsible for nbuf chain protection.
    723       1.1  rmind  */
    724       1.1  rmind npf_rule_t *
    725  1.14.2.1    tls npf_ruleset_inspect(npf_cache_t *npc, nbuf_t *nbuf,
    726  1.14.2.1    tls     const npf_ruleset_t *rlset, const int di, const int layer)
    727       1.1  rmind {
    728       1.7  rmind 	const int di_mask = (di & PFIL_IN) ? NPF_RULE_IN : NPF_RULE_OUT;
    729  1.14.2.1    tls 	const u_int nitems = rlset->rs_nitems;
    730  1.14.2.1    tls 	npf_rule_t *final_rl = NULL;
    731  1.14.2.1    tls 	u_int n = 0;
    732       1.1  rmind 
    733       1.1  rmind 	KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
    734  1.14.2.1    tls 
    735  1.14.2.1    tls 	while (n < nitems) {
    736  1.14.2.1    tls 		npf_rule_t *rl = rlset->rs_rules[n];
    737  1.14.2.1    tls 		const u_int skip_to = rl->r_skip_to;
    738  1.14.2.1    tls 		const uint32_t attr = rl->r_attr;
    739  1.14.2.1    tls 
    740  1.14.2.1    tls 		KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    741       1.1  rmind 		KASSERT(!final_rl || rl->r_priority >= final_rl->r_priority);
    742  1.14.2.1    tls 		KASSERT(n < skip_to);
    743       1.1  rmind 
    744  1.14.2.1    tls 		/* Group is a barrier: return a matching if found any. */
    745  1.14.2.1    tls 		if ((attr & NPF_RULE_GROUP) != 0 && final_rl) {
    746  1.14.2.1    tls 			break;
    747       1.1  rmind 		}
    748  1.14.2.1    tls 
    749  1.14.2.1    tls 		/* Main inspection of the rule. */
    750  1.14.2.1    tls 		if (!npf_rule_inspect(npc, nbuf, rl, di_mask, layer)) {
    751  1.14.2.1    tls 			n = skip_to;
    752       1.1  rmind 			continue;
    753       1.1  rmind 		}
    754  1.14.2.1    tls 
    755  1.14.2.1    tls 		if (NPF_DYNAMIC_GROUP_P(attr)) {
    756  1.14.2.1    tls 			/*
    757  1.14.2.1    tls 			 * If this is a dynamic rule, re-inspect the subrules.
    758  1.14.2.1    tls 			 * If it has any matching rule, then it is final.
    759  1.14.2.1    tls 			 */
    760  1.14.2.1    tls 			rl = npf_rule_reinspect(npc, nbuf, rl, di_mask, layer);
    761  1.14.2.1    tls 			if (rl != NULL) {
    762  1.14.2.1    tls 				final_rl = rl;
    763  1.14.2.1    tls 				break;
    764  1.14.2.1    tls 			}
    765  1.14.2.1    tls 		} else if ((attr & NPF_RULE_GROUP) == 0) {
    766  1.14.2.1    tls 			/*
    767  1.14.2.1    tls 			 * Groups themselves are not matching.
    768  1.14.2.1    tls 			 */
    769  1.14.2.1    tls 			final_rl = rl;
    770  1.14.2.1    tls 		}
    771  1.14.2.1    tls 
    772       1.1  rmind 		/* Set the matching rule and check for "final". */
    773  1.14.2.1    tls 		if (attr & NPF_RULE_FINAL) {
    774       1.2  rmind 			break;
    775       1.1  rmind 		}
    776  1.14.2.1    tls 		n++;
    777       1.1  rmind 	}
    778       1.2  rmind 
    779  1.14.2.1    tls 	KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    780       1.7  rmind 	return final_rl;
    781       1.1  rmind }
    782       1.1  rmind 
    783       1.1  rmind /*
    784  1.14.2.1    tls  * npf_rule_conclude: return decision and the flags for conclusion.
    785       1.1  rmind  *
    786       1.1  rmind  * => Returns ENETUNREACH if "block" and 0 if "pass".
    787       1.1  rmind  */
    788       1.1  rmind int
    789  1.14.2.1    tls npf_rule_conclude(const npf_rule_t *rl, int *retfl)
    790       1.1  rmind {
    791       1.1  rmind 	/* If not passing - drop the packet. */
    792       1.4  rmind 	*retfl = rl->r_attr;
    793  1.14.2.1    tls 	return (rl->r_attr & NPF_RULE_PASS) ? 0 : ENETUNREACH;
    794       1.1  rmind }
    795       1.1  rmind 
    796       1.1  rmind #if defined(DDB) || defined(_NPF_TESTING)
    797       1.1  rmind 
    798       1.1  rmind void
    799      1.12  rmind npf_rulenc_dump(const npf_rule_t *rl)
    800       1.1  rmind {
    801  1.14.2.1    tls 	const uint32_t *op = rl->r_code;
    802  1.14.2.1    tls 	size_t n = rl->r_clen;
    803       1.1  rmind 
    804       1.2  rmind 	while (n) {
    805       1.1  rmind 		printf("\t> |0x%02x|\n", (uint32_t)*op);
    806       1.1  rmind 		op++;
    807       1.1  rmind 		n -= sizeof(*op);
    808       1.2  rmind 	}
    809       1.1  rmind 	printf("-> %s\n", (rl->r_attr & NPF_RULE_PASS) ? "pass" : "block");
    810       1.1  rmind }
    811       1.1  rmind 
    812       1.1  rmind #endif
    813