Home | History | Annotate | Line # | Download | only in npf
npf_ruleset.c revision 1.42
      1  1.42  rmind /*	$NetBSD: npf_ruleset.c,v 1.42 2015/03/20 23:36:28 rmind Exp $	*/
      2   1.1  rmind 
      3   1.1  rmind /*-
      4  1.41  rmind  * Copyright (c) 2009-2015 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.42  rmind __KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.42 2015/03/20 23:36:28 rmind 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.20  rmind #include <sys/atomic.h>
     43   1.1  rmind #include <sys/kmem.h>
     44   1.1  rmind #include <sys/queue.h>
     45  1.17  rmind #include <sys/mbuf.h>
     46   1.1  rmind #include <sys/types.h>
     47   1.1  rmind 
     48  1.17  rmind #include <net/bpf.h>
     49  1.20  rmind #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_impl.h"
     54   1.1  rmind 
     55   1.4  rmind struct npf_ruleset {
     56  1.18  rmind 	/*
     57  1.18  rmind 	 * - List of all rules.
     58  1.18  rmind 	 * - Dynamic (i.e. named) rules.
     59  1.18  rmind 	 * - G/C list for convenience.
     60  1.18  rmind 	 */
     61  1.17  rmind 	LIST_HEAD(, npf_rule)	rs_all;
     62  1.17  rmind 	LIST_HEAD(, npf_rule)	rs_dynamic;
     63  1.18  rmind 	LIST_HEAD(, npf_rule)	rs_gc;
     64  1.17  rmind 
     65  1.19  rmind 	/* Unique ID counter. */
     66  1.19  rmind 	uint64_t		rs_idcnt;
     67  1.19  rmind 
     68  1.17  rmind 	/* Number of array slots and active rules. */
     69  1.17  rmind 	u_int			rs_slots;
     70  1.17  rmind 	u_int			rs_nitems;
     71  1.17  rmind 
     72  1.17  rmind 	/* Array of ordered rules. */
     73  1.17  rmind 	npf_rule_t *		rs_rules[];
     74   1.4  rmind };
     75   1.4  rmind 
     76   1.1  rmind struct npf_rule {
     77  1.17  rmind 	/* Attributes, interface and skip slot. */
     78   1.4  rmind 	uint32_t		r_attr;
     79   1.4  rmind 	u_int			r_ifid;
     80  1.17  rmind 	u_int			r_skip_to;
     81  1.17  rmind 
     82  1.17  rmind 	/* Code to process, if any. */
     83  1.17  rmind 	int			r_type;
     84  1.27  rmind 	bpfjit_func_t		r_jcode;
     85  1.17  rmind 	void *			r_code;
     86  1.36  rmind 	u_int			r_clen;
     87  1.17  rmind 
     88  1.17  rmind 	/* NAT policy (optional), rule procedure and subset. */
     89  1.17  rmind 	npf_natpolicy_t *	r_natp;
     90   1.4  rmind 	npf_rproc_t *		r_rproc;
     91  1.17  rmind 
     92  1.42  rmind 	union {
     93  1.42  rmind 		/*
     94  1.42  rmind 		 * Dynamic group: rule subset and a group list entry.
     95  1.42  rmind 		 */
     96  1.42  rmind 		struct {
     97  1.42  rmind 			npf_rule_t *		r_subset;
     98  1.42  rmind 			LIST_ENTRY(npf_rule)	r_dentry;
     99  1.42  rmind 		};
    100  1.17  rmind 
    101  1.42  rmind 		/*
    102  1.42  rmind 		 * Dynamic rule: priority, parent group and next rule.
    103  1.42  rmind 		 */
    104  1.42  rmind 		struct {
    105  1.42  rmind 			int			r_priority;
    106  1.42  rmind 			npf_rule_t *		r_parent;
    107  1.42  rmind 			npf_rule_t *		r_next;
    108  1.42  rmind 		};
    109  1.42  rmind 	};
    110  1.17  rmind 
    111  1.36  rmind 	/* Rule ID, name and the optional key. */
    112  1.19  rmind 	uint64_t		r_id;
    113  1.36  rmind 	char			r_name[NPF_RULE_MAXNAMELEN];
    114  1.36  rmind 	uint8_t			r_key[NPF_RULE_MAXKEYLEN];
    115  1.18  rmind 
    116  1.36  rmind 	/* All-list entry and the auxiliary info. */
    117  1.17  rmind 	LIST_ENTRY(npf_rule)	r_aentry;
    118  1.36  rmind 	prop_data_t		r_info;
    119  1.36  rmind };
    120  1.17  rmind 
    121  1.37  rmind #define	SKIPTO_ADJ_FLAG		(1U << 31)
    122  1.37  rmind #define	SKIPTO_MASK		(SKIPTO_ADJ_FLAG - 1)
    123  1.37  rmind 
    124  1.37  rmind static int	npf_rule_export(const npf_ruleset_t *,
    125  1.37  rmind     const npf_rule_t *, prop_dictionary_t);
    126   1.1  rmind 
    127  1.31  rmind /*
    128  1.31  rmind  * Private attributes - must be in the NPF_RULE_PRIVMASK range.
    129  1.31  rmind  */
    130  1.31  rmind #define	NPF_RULE_KEEPNAT	(0x01000000 & NPF_RULE_PRIVMASK)
    131  1.31  rmind 
    132  1.17  rmind #define	NPF_DYNAMIC_GROUP_P(attr) \
    133  1.17  rmind     (((attr) & NPF_DYNAMIC_GROUP) == NPF_DYNAMIC_GROUP)
    134  1.17  rmind 
    135  1.19  rmind #define	NPF_DYNAMIC_RULE_P(attr) \
    136  1.19  rmind     (((attr) & NPF_DYNAMIC_GROUP) == NPF_RULE_DYNAMIC)
    137  1.19  rmind 
    138   1.1  rmind npf_ruleset_t *
    139  1.17  rmind npf_ruleset_create(size_t slots)
    140   1.1  rmind {
    141  1.17  rmind 	size_t len = offsetof(npf_ruleset_t, rs_rules[slots]);
    142   1.1  rmind 	npf_ruleset_t *rlset;
    143   1.1  rmind 
    144  1.17  rmind 	rlset = kmem_zalloc(len, KM_SLEEP);
    145  1.17  rmind 	LIST_INIT(&rlset->rs_dynamic);
    146  1.17  rmind 	LIST_INIT(&rlset->rs_all);
    147  1.19  rmind 	LIST_INIT(&rlset->rs_gc);
    148  1.19  rmind 	rlset->rs_slots = slots;
    149  1.19  rmind 
    150   1.1  rmind 	return rlset;
    151   1.1  rmind }
    152   1.1  rmind 
    153   1.1  rmind void
    154   1.1  rmind npf_ruleset_destroy(npf_ruleset_t *rlset)
    155   1.1  rmind {
    156  1.17  rmind 	size_t len = offsetof(npf_ruleset_t, rs_rules[rlset->rs_slots]);
    157   1.1  rmind 	npf_rule_t *rl;
    158   1.1  rmind 
    159  1.17  rmind 	while ((rl = LIST_FIRST(&rlset->rs_all)) != NULL) {
    160  1.42  rmind 		if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
    161  1.42  rmind 			/*
    162  1.42  rmind 			 * Note: r_subset may point to the rules which
    163  1.42  rmind 			 * were inherited by a new ruleset.
    164  1.42  rmind 			 */
    165  1.42  rmind 			rl->r_subset = NULL;
    166  1.42  rmind 			LIST_REMOVE(rl, r_dentry);
    167  1.42  rmind 		}
    168  1.42  rmind 		if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
    169  1.42  rmind 			/* Not removing from r_subset, see above. */
    170  1.42  rmind 			KASSERT(rl->r_parent != NULL);
    171  1.42  rmind 		}
    172  1.42  rmind 		LIST_REMOVE(rl, r_aentry);
    173   1.1  rmind 		npf_rule_free(rl);
    174   1.1  rmind 	}
    175  1.17  rmind 	KASSERT(LIST_EMPTY(&rlset->rs_dynamic));
    176  1.18  rmind 	KASSERT(LIST_EMPTY(&rlset->rs_gc));
    177  1.17  rmind 	kmem_free(rlset, len);
    178   1.1  rmind }
    179   1.1  rmind 
    180   1.1  rmind /*
    181   1.1  rmind  * npf_ruleset_insert: insert the rule into the specified ruleset.
    182   1.1  rmind  */
    183   1.1  rmind void
    184   1.1  rmind npf_ruleset_insert(npf_ruleset_t *rlset, npf_rule_t *rl)
    185   1.1  rmind {
    186  1.17  rmind 	u_int n = rlset->rs_nitems;
    187  1.17  rmind 
    188  1.17  rmind 	KASSERT(n < rlset->rs_slots);
    189  1.17  rmind 
    190  1.17  rmind 	LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
    191  1.17  rmind 	if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
    192  1.17  rmind 		LIST_INSERT_HEAD(&rlset->rs_dynamic, rl, r_dentry);
    193  1.24  rmind 	} else {
    194  1.24  rmind 		KASSERTMSG(rl->r_parent == NULL, "cannot be dynamic rule");
    195  1.24  rmind 		rl->r_attr &= ~NPF_RULE_DYNAMIC;
    196  1.17  rmind 	}
    197  1.17  rmind 
    198  1.17  rmind 	rlset->rs_rules[n] = rl;
    199  1.17  rmind 	rlset->rs_nitems++;
    200  1.17  rmind 
    201  1.17  rmind 	if (rl->r_skip_to < ++n) {
    202  1.37  rmind 		rl->r_skip_to = SKIPTO_ADJ_FLAG | n;
    203  1.17  rmind 	}
    204  1.17  rmind }
    205  1.17  rmind 
    206  1.17  rmind static npf_rule_t *
    207  1.17  rmind npf_ruleset_lookup(npf_ruleset_t *rlset, const char *name)
    208  1.17  rmind {
    209  1.17  rmind 	npf_rule_t *rl;
    210  1.17  rmind 
    211  1.17  rmind 	KASSERT(npf_config_locked_p());
    212  1.17  rmind 
    213  1.17  rmind 	LIST_FOREACH(rl, &rlset->rs_dynamic, r_dentry) {
    214  1.17  rmind 		KASSERT(NPF_DYNAMIC_GROUP_P(rl->r_attr));
    215  1.17  rmind 		if (strncmp(rl->r_name, name, NPF_RULE_MAXNAMELEN) == 0)
    216  1.17  rmind 			break;
    217  1.17  rmind 	}
    218  1.17  rmind 	return rl;
    219  1.17  rmind }
    220  1.17  rmind 
    221  1.39  rmind /*
    222  1.39  rmind  * npf_ruleset_add: insert dynamic rule into the (active) ruleset.
    223  1.39  rmind  */
    224  1.17  rmind int
    225  1.17  rmind npf_ruleset_add(npf_ruleset_t *rlset, const char *rname, npf_rule_t *rl)
    226  1.17  rmind {
    227  1.42  rmind 	npf_rule_t *rg, *it, *target;
    228  1.42  rmind 	int priocmd;
    229  1.17  rmind 
    230  1.42  rmind 	if (!NPF_DYNAMIC_RULE_P(rl->r_attr)) {
    231  1.42  rmind 		return EINVAL;
    232  1.42  rmind 	}
    233  1.17  rmind 	rg = npf_ruleset_lookup(rlset, rname);
    234  1.17  rmind 	if (rg == NULL) {
    235  1.19  rmind 		return ESRCH;
    236  1.19  rmind 	}
    237  1.17  rmind 
    238  1.19  rmind 	/* Dynamic rule - assign a unique ID and save the parent. */
    239  1.19  rmind 	rl->r_id = ++rlset->rs_idcnt;
    240  1.17  rmind 	rl->r_parent = rg;
    241  1.17  rmind 
    242  1.17  rmind 	/*
    243  1.17  rmind 	 * Rule priority: (highest) 1, 2 ... n (lowest).
    244  1.17  rmind 	 * Negative priority indicates an operation and is reset to zero.
    245  1.17  rmind 	 */
    246  1.17  rmind 	if ((priocmd = rl->r_priority) < 0) {
    247  1.17  rmind 		rl->r_priority = 0;
    248  1.17  rmind 	}
    249  1.17  rmind 
    250  1.42  rmind 	/*
    251  1.42  rmind 	 * WARNING: once rg->subset or target->r_next of an *active*
    252  1.42  rmind 	 * rule is set, then our rule becomes globally visible and active.
    253  1.42  rmind 	 * Must issue a load fence to ensure rl->r_next visibility first.
    254  1.42  rmind 	 */
    255  1.17  rmind 	switch (priocmd) {
    256  1.17  rmind 	case NPF_PRI_LAST:
    257  1.17  rmind 	default:
    258  1.42  rmind 		target = NULL;
    259  1.42  rmind 		it = rg->r_subset;
    260  1.42  rmind 		while (it && it->r_priority <= rl->r_priority) {
    261  1.42  rmind 			target = it;
    262  1.42  rmind 			it = it->r_next;
    263  1.42  rmind 		}
    264  1.42  rmind 		if (target) {
    265  1.42  rmind 			rl->r_next = target->r_next;
    266  1.42  rmind 			membar_producer();
    267  1.42  rmind 			target->r_next = rl;
    268  1.42  rmind 			break;
    269  1.17  rmind 		}
    270  1.42  rmind 		/* FALLTHROUGH */
    271  1.42  rmind 
    272  1.42  rmind 	case NPF_PRI_FIRST:
    273  1.42  rmind 		rl->r_next = rg->r_subset;
    274  1.42  rmind 		membar_producer();
    275  1.42  rmind 		rg->r_subset = rl;
    276  1.17  rmind 		break;
    277  1.17  rmind 	}
    278  1.17  rmind 
    279  1.17  rmind 	/* Finally, add into the all-list. */
    280  1.17  rmind 	LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
    281  1.17  rmind 	return 0;
    282  1.17  rmind }
    283  1.17  rmind 
    284  1.42  rmind static void
    285  1.42  rmind npf_ruleset_unlink(npf_rule_t *rl, npf_rule_t *prev)
    286  1.42  rmind {
    287  1.42  rmind 	KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    288  1.42  rmind 	if (prev) {
    289  1.42  rmind 		prev->r_next = rl->r_next;
    290  1.42  rmind 	} else {
    291  1.42  rmind 		npf_rule_t *rg = rl->r_parent;
    292  1.42  rmind 		rg->r_subset = rl->r_next;
    293  1.42  rmind 	}
    294  1.42  rmind 	LIST_REMOVE(rl, r_aentry);
    295  1.42  rmind }
    296  1.42  rmind 
    297  1.39  rmind /*
    298  1.39  rmind  * npf_ruleset_remove: remove the dynamic rule given the rule ID.
    299  1.39  rmind  */
    300  1.18  rmind int
    301  1.19  rmind npf_ruleset_remove(npf_ruleset_t *rlset, const char *rname, uint64_t id)
    302  1.17  rmind {
    303  1.42  rmind 	npf_rule_t *rg, *prev = NULL;
    304  1.17  rmind 
    305  1.17  rmind 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    306  1.19  rmind 		return ESRCH;
    307  1.17  rmind 	}
    308  1.42  rmind 	for (npf_rule_t *rl = rg->r_subset; rl; rl = rl->r_next) {
    309  1.24  rmind 		KASSERT(rl->r_parent == rg);
    310  1.42  rmind 		KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    311  1.24  rmind 
    312  1.17  rmind 		/* Compare ID.  On match, remove and return. */
    313  1.19  rmind 		if (rl->r_id == id) {
    314  1.42  rmind 			npf_ruleset_unlink(rl, prev);
    315  1.18  rmind 			LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
    316  1.19  rmind 			return 0;
    317  1.17  rmind 		}
    318  1.42  rmind 		prev = rl;
    319  1.17  rmind 	}
    320  1.19  rmind 	return ENOENT;
    321  1.17  rmind }
    322  1.17  rmind 
    323  1.39  rmind /*
    324  1.39  rmind  * npf_ruleset_remkey: remove the dynamic rule given the rule key.
    325  1.39  rmind  */
    326  1.18  rmind int
    327  1.17  rmind npf_ruleset_remkey(npf_ruleset_t *rlset, const char *rname,
    328  1.17  rmind     const void *key, size_t len)
    329  1.17  rmind {
    330  1.42  rmind 	npf_rule_t *rg, *rlast = NULL, *prev = NULL, *lastprev = NULL;
    331   1.1  rmind 
    332  1.17  rmind 	KASSERT(len && len <= NPF_RULE_MAXKEYLEN);
    333  1.17  rmind 
    334  1.17  rmind 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    335  1.19  rmind 		return ESRCH;
    336  1.17  rmind 	}
    337  1.18  rmind 
    338  1.42  rmind 	/* Compare the key and find the last in the list. */
    339  1.42  rmind 	for (npf_rule_t *rl = rg->r_subset; rl; rl = rl->r_next) {
    340  1.24  rmind 		KASSERT(rl->r_parent == rg);
    341  1.42  rmind 		KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    342  1.17  rmind 		if (memcmp(rl->r_key, key, len) == 0) {
    343  1.42  rmind 			lastprev = prev;
    344  1.42  rmind 			rlast = rl;
    345  1.17  rmind 		}
    346  1.42  rmind 		prev = rl;
    347  1.42  rmind 	}
    348  1.42  rmind 	if (!rlast) {
    349  1.42  rmind 		return ENOENT;
    350   1.1  rmind 	}
    351  1.42  rmind 	npf_ruleset_unlink(rlast, lastprev);
    352  1.42  rmind 	LIST_INSERT_HEAD(&rlset->rs_gc, rlast, r_aentry);
    353  1.42  rmind 	return 0;
    354  1.18  rmind }
    355  1.18  rmind 
    356  1.39  rmind /*
    357  1.39  rmind  * npf_ruleset_list: serialise and return the dynamic rules.
    358  1.39  rmind  */
    359  1.18  rmind prop_dictionary_t
    360  1.18  rmind npf_ruleset_list(npf_ruleset_t *rlset, const char *rname)
    361  1.18  rmind {
    362  1.36  rmind 	prop_dictionary_t rgdict;
    363  1.18  rmind 	prop_array_t rules;
    364  1.42  rmind 	npf_rule_t *rg;
    365  1.18  rmind 
    366  1.36  rmind 	KASSERT(npf_config_locked_p());
    367  1.36  rmind 
    368  1.18  rmind 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    369  1.18  rmind 		return NULL;
    370  1.18  rmind 	}
    371  1.36  rmind 	if ((rgdict = prop_dictionary_create()) == NULL) {
    372  1.18  rmind 		return NULL;
    373  1.18  rmind 	}
    374  1.18  rmind 	if ((rules = prop_array_create()) == NULL) {
    375  1.36  rmind 		prop_object_release(rgdict);
    376  1.18  rmind 		return NULL;
    377  1.18  rmind 	}
    378  1.18  rmind 
    379  1.42  rmind 	for (npf_rule_t *rl = rg->r_subset; rl; rl = rl->r_next) {
    380  1.36  rmind 		prop_dictionary_t rldict;
    381  1.36  rmind 
    382  1.24  rmind 		KASSERT(rl->r_parent == rg);
    383  1.42  rmind 		KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    384  1.36  rmind 
    385  1.42  rmind 		rldict = prop_dictionary_create();
    386  1.37  rmind 		if (npf_rule_export(rlset, rl, rldict)) {
    387  1.18  rmind 			prop_object_release(rldict);
    388  1.19  rmind 			prop_object_release(rules);
    389  1.18  rmind 			return NULL;
    390  1.18  rmind 		}
    391  1.37  rmind 		prop_array_add(rules, rldict);
    392  1.37  rmind 		prop_object_release(rldict);
    393  1.18  rmind 	}
    394  1.19  rmind 
    395  1.36  rmind 	if (!prop_dictionary_set(rgdict, "rules", rules)) {
    396  1.36  rmind 		prop_object_release(rgdict);
    397  1.36  rmind 		rgdict = NULL;
    398  1.18  rmind 	}
    399  1.18  rmind 	prop_object_release(rules);
    400  1.36  rmind 	return rgdict;
    401  1.18  rmind }
    402  1.18  rmind 
    403  1.39  rmind /*
    404  1.39  rmind  * npf_ruleset_flush: flush the dynamic rules in the ruleset by inserting
    405  1.39  rmind  * them into the G/C list.
    406  1.39  rmind  */
    407  1.18  rmind int
    408  1.18  rmind npf_ruleset_flush(npf_ruleset_t *rlset, const char *rname)
    409  1.18  rmind {
    410  1.18  rmind 	npf_rule_t *rg, *rl;
    411  1.18  rmind 
    412  1.18  rmind 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    413  1.19  rmind 		return ESRCH;
    414  1.18  rmind 	}
    415  1.42  rmind 
    416  1.42  rmind 	rl = atomic_swap_ptr(&rg->r_subset, NULL);
    417  1.42  rmind 	membar_producer();
    418  1.42  rmind 
    419  1.42  rmind 	while (rl) {
    420  1.42  rmind 		KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    421  1.24  rmind 		KASSERT(rl->r_parent == rg);
    422  1.42  rmind 
    423  1.42  rmind 		LIST_REMOVE(rl, r_aentry);
    424  1.18  rmind 		LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
    425  1.42  rmind 		rl = rl->r_next;
    426  1.18  rmind 	}
    427  1.18  rmind 	return 0;
    428  1.18  rmind }
    429  1.18  rmind 
    430  1.39  rmind /*
    431  1.39  rmind  * npf_ruleset_gc: destroy the rules in G/C list.
    432  1.39  rmind  */
    433  1.39  rmind void
    434  1.39  rmind npf_ruleset_gc(npf_ruleset_t *rlset)
    435  1.39  rmind {
    436  1.39  rmind 	npf_rule_t *rl;
    437  1.39  rmind 
    438  1.39  rmind 	while ((rl = LIST_FIRST(&rlset->rs_gc)) != NULL) {
    439  1.39  rmind 		LIST_REMOVE(rl, r_aentry);
    440  1.39  rmind 		npf_rule_free(rl);
    441  1.39  rmind 	}
    442  1.39  rmind }
    443  1.39  rmind 
    444  1.39  rmind /*
    445  1.39  rmind  * npf_ruleset_export: serialise and return the static rules.
    446  1.39  rmind  */
    447  1.36  rmind int
    448  1.36  rmind npf_ruleset_export(const npf_ruleset_t *rlset, prop_array_t rules)
    449  1.36  rmind {
    450  1.37  rmind 	const u_int nitems = rlset->rs_nitems;
    451  1.36  rmind 	int error = 0;
    452  1.37  rmind 	u_int n = 0;
    453  1.36  rmind 
    454  1.36  rmind 	KASSERT(npf_config_locked_p());
    455  1.36  rmind 
    456  1.37  rmind 	while (n < nitems) {
    457  1.37  rmind 		const npf_rule_t *rl = rlset->rs_rules[n];
    458  1.36  rmind 		const npf_natpolicy_t *natp = rl->r_natp;
    459  1.36  rmind 		prop_dictionary_t rldict;
    460  1.36  rmind 
    461  1.36  rmind 		rldict = prop_dictionary_create();
    462  1.37  rmind 		if ((error = npf_rule_export(rlset, rl, rldict)) != 0) {
    463  1.36  rmind 			prop_object_release(rldict);
    464  1.36  rmind 			break;
    465  1.36  rmind 		}
    466  1.36  rmind 		if (natp && (error = npf_nat_policyexport(natp, rldict)) != 0) {
    467  1.36  rmind 			prop_object_release(rldict);
    468  1.36  rmind 			break;
    469  1.36  rmind 		}
    470  1.37  rmind 		prop_array_add(rules, rldict);
    471  1.37  rmind 		prop_object_release(rldict);
    472  1.37  rmind 		n++;
    473  1.36  rmind 	}
    474  1.36  rmind 	return error;
    475  1.36  rmind }
    476  1.36  rmind 
    477  1.17  rmind /*
    478  1.31  rmind  * npf_ruleset_reload: prepare the new ruleset by scanning the active
    479  1.39  rmind  * ruleset and: 1) sharing the dynamic rules 2) sharing NAT policies.
    480  1.17  rmind  *
    481  1.31  rmind  * => The active (old) ruleset should be exclusively locked.
    482  1.17  rmind  */
    483  1.17  rmind void
    484  1.40  rmind npf_ruleset_reload(npf_ruleset_t *newset, npf_ruleset_t *oldset, bool load)
    485  1.17  rmind {
    486  1.31  rmind 	npf_rule_t *rg, *rl;
    487  1.35  rmind 	uint64_t nid = 0;
    488  1.17  rmind 
    489  1.17  rmind 	KASSERT(npf_config_locked_p());
    490  1.17  rmind 
    491  1.31  rmind 	/*
    492  1.31  rmind 	 * Scan the dynamic rules and share (migrate) if needed.
    493  1.31  rmind 	 */
    494  1.31  rmind 	LIST_FOREACH(rg, &newset->rs_dynamic, r_dentry) {
    495  1.42  rmind 		npf_rule_t *active_rgroup;
    496  1.18  rmind 
    497  1.31  rmind 		/* Look for a dynamic ruleset group with such name. */
    498  1.42  rmind 		active_rgroup = npf_ruleset_lookup(oldset, rg->r_name);
    499  1.42  rmind 		if (active_rgroup == NULL) {
    500  1.17  rmind 			continue;
    501  1.17  rmind 		}
    502  1.18  rmind 
    503  1.18  rmind 		/*
    504  1.42  rmind 		 * ATOMICITY: Copy the head pointer of the linked-list,
    505  1.42  rmind 		 * but do not remove the rules from the active r_subset.
    506  1.42  rmind 		 * This is necessary because the rules are still active
    507  1.42  rmind 		 * and therefore are accessible for inspection via the
    508  1.42  rmind 		 * old ruleset.
    509  1.18  rmind 		 */
    510  1.42  rmind 		rg->r_subset = active_rgroup->r_subset;
    511  1.42  rmind 
    512  1.42  rmind 		/*
    513  1.42  rmind 		 * We can safely migrate to the new all-rule list and
    514  1.42  rmind 		 * reset the parent rule, though.
    515  1.42  rmind 		 */
    516  1.42  rmind 		for (rl = rg->r_subset; rl; rl = rl->r_next) {
    517  1.42  rmind 			KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    518  1.18  rmind 			LIST_REMOVE(rl, r_aentry);
    519  1.31  rmind 			LIST_INSERT_HEAD(&newset->rs_all, rl, r_aentry);
    520  1.42  rmind 
    521  1.42  rmind 			KASSERT(rl->r_parent == active_rgroup);
    522  1.19  rmind 			rl->r_parent = rg;
    523  1.18  rmind 		}
    524   1.1  rmind 	}
    525  1.19  rmind 
    526  1.31  rmind 	/*
    527  1.40  rmind 	 * If performing the load of connections then NAT policies may
    528  1.40  rmind 	 * already have translated connections associated with them and
    529  1.40  rmind 	 * we should not share or inherit anything.
    530  1.40  rmind 	 */
    531  1.40  rmind 	if (load)
    532  1.40  rmind 		return;
    533  1.40  rmind 
    534  1.40  rmind 	/*
    535  1.31  rmind 	 * Scan all rules in the new ruleset and share NAT policies.
    536  1.35  rmind 	 * Also, assign a unique ID for each policy here.
    537  1.31  rmind 	 */
    538  1.31  rmind 	LIST_FOREACH(rl, &newset->rs_all, r_aentry) {
    539  1.31  rmind 		npf_natpolicy_t *np;
    540  1.31  rmind 		npf_rule_t *actrl;
    541  1.31  rmind 
    542  1.31  rmind 		/* Does the rule have a NAT policy associated? */
    543  1.31  rmind 		if ((np = rl->r_natp) == NULL) {
    544  1.31  rmind 			continue;
    545  1.31  rmind 		}
    546  1.35  rmind 
    547  1.38  rmind 		/*
    548  1.38  rmind 		 * First, try to share the active port map.  If this
    549  1.38  rmind 		 * policy will be unused, npf_nat_freepolicy() will
    550  1.38  rmind 		 * drop the reference.
    551  1.38  rmind 		 */
    552  1.38  rmind 		npf_ruleset_sharepm(oldset, np);
    553  1.38  rmind 
    554  1.31  rmind 		/* Does it match with any policy in the active ruleset? */
    555  1.38  rmind 		LIST_FOREACH(actrl, &oldset->rs_all, r_aentry) {
    556  1.38  rmind 			if (!actrl->r_natp)
    557  1.38  rmind 				continue;
    558  1.38  rmind 			if ((actrl->r_attr & NPF_RULE_KEEPNAT) != 0)
    559  1.38  rmind 				continue;
    560  1.38  rmind 			if (npf_nat_cmppolicy(actrl->r_natp, np))
    561  1.38  rmind 				break;
    562  1.38  rmind 		}
    563  1.38  rmind 		if (!actrl) {
    564  1.38  rmind 			/* No: just set the ID and continue. */
    565  1.35  rmind 			npf_nat_setid(np, ++nid);
    566  1.31  rmind 			continue;
    567  1.31  rmind 		}
    568  1.31  rmind 
    569  1.38  rmind 		/* Yes: inherit the matching NAT policy. */
    570  1.31  rmind 		rl->r_natp = actrl->r_natp;
    571  1.35  rmind 		npf_nat_setid(rl->r_natp, ++nid);
    572  1.31  rmind 
    573  1.31  rmind 		/*
    574  1.31  rmind 		 * Finally, mark the active rule to not destroy its NAT
    575  1.31  rmind 		 * policy later as we inherited it (but the rule must be
    576  1.31  rmind 		 * kept active for now).  Destroy the new/unused policy.
    577  1.31  rmind 		 */
    578  1.31  rmind 		actrl->r_attr |= NPF_RULE_KEEPNAT;
    579  1.31  rmind 		npf_nat_freepolicy(np);
    580  1.31  rmind 	}
    581  1.31  rmind 
    582  1.19  rmind 	/* Inherit the ID counter. */
    583  1.31  rmind 	newset->rs_idcnt = oldset->rs_idcnt;
    584   1.1  rmind }
    585   1.1  rmind 
    586  1.39  rmind /*
    587  1.39  rmind  * npf_ruleset_sharepm: attempt to share the active NAT portmap.
    588  1.39  rmind  */
    589   1.6  rmind npf_rule_t *
    590   1.6  rmind npf_ruleset_sharepm(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
    591   1.6  rmind {
    592   1.6  rmind 	npf_natpolicy_t *np;
    593   1.6  rmind 	npf_rule_t *rl;
    594   1.6  rmind 
    595  1.39  rmind 	/*
    596  1.39  rmind 	 * Scan the NAT policies in the ruleset and match with the
    597  1.39  rmind 	 * given policy based on the translation IP address.  If they
    598  1.39  rmind 	 * match - adjust the given NAT policy to use the active NAT
    599  1.39  rmind 	 * portmap.  In such case the reference on the old portmap is
    600  1.39  rmind 	 * dropped and acquired on the active one.
    601  1.39  rmind 	 */
    602  1.17  rmind 	LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
    603   1.6  rmind 		np = rl->r_natp;
    604   1.6  rmind 		if (np == NULL || np == mnp)
    605   1.6  rmind 			continue;
    606   1.6  rmind 		if (npf_nat_sharepm(np, mnp))
    607   1.6  rmind 			break;
    608   1.6  rmind 	}
    609   1.6  rmind 	return rl;
    610   1.6  rmind }
    611   1.6  rmind 
    612  1.35  rmind npf_natpolicy_t *
    613  1.35  rmind npf_ruleset_findnat(npf_ruleset_t *rlset, uint64_t id)
    614  1.35  rmind {
    615  1.35  rmind 	npf_rule_t *rl;
    616  1.35  rmind 
    617  1.35  rmind 	LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
    618  1.35  rmind 		npf_natpolicy_t *np = rl->r_natp;
    619  1.35  rmind 		if (np && npf_nat_getid(np) == id) {
    620  1.35  rmind 			return np;
    621  1.35  rmind 		}
    622  1.35  rmind 	}
    623  1.35  rmind 	return NULL;
    624  1.35  rmind }
    625  1.35  rmind 
    626   1.1  rmind /*
    627  1.13  rmind  * npf_ruleset_freealg: inspect the ruleset and disassociate specified
    628  1.13  rmind  * ALG from all NAT entries using it.
    629  1.13  rmind  */
    630  1.13  rmind void
    631  1.13  rmind npf_ruleset_freealg(npf_ruleset_t *rlset, npf_alg_t *alg)
    632  1.13  rmind {
    633  1.13  rmind 	npf_rule_t *rl;
    634  1.17  rmind 	npf_natpolicy_t *np;
    635  1.13  rmind 
    636  1.17  rmind 	LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
    637  1.17  rmind 		if ((np = rl->r_natp) != NULL) {
    638  1.13  rmind 			npf_nat_freealg(np, alg);
    639  1.13  rmind 		}
    640  1.13  rmind 	}
    641  1.13  rmind }
    642  1.13  rmind 
    643  1.13  rmind /*
    644  1.25  rmind  * npf_rule_alloc: allocate a rule and initialise it.
    645   1.1  rmind  */
    646   1.4  rmind npf_rule_t *
    647  1.17  rmind npf_rule_alloc(prop_dictionary_t rldict)
    648   1.1  rmind {
    649   1.4  rmind 	npf_rule_t *rl;
    650   1.7  rmind 	const char *rname;
    651  1.36  rmind 	prop_data_t d;
    652   1.1  rmind 
    653   1.4  rmind 	/* Allocate a rule structure. */
    654  1.11  rmind 	rl = kmem_zalloc(sizeof(npf_rule_t), KM_SLEEP);
    655   1.4  rmind 	rl->r_natp = NULL;
    656   1.4  rmind 
    657  1.11  rmind 	/* Name (optional) */
    658   1.7  rmind 	if (prop_dictionary_get_cstring_nocopy(rldict, "name", &rname)) {
    659  1.17  rmind 		strlcpy(rl->r_name, rname, NPF_RULE_MAXNAMELEN);
    660   1.7  rmind 	} else {
    661   1.7  rmind 		rl->r_name[0] = '\0';
    662   1.7  rmind 	}
    663   1.7  rmind 
    664  1.11  rmind 	/* Attributes, priority and interface ID (optional). */
    665  1.36  rmind 	prop_dictionary_get_uint32(rldict, "attr", &rl->r_attr);
    666  1.31  rmind 	rl->r_attr &= ~NPF_RULE_PRIVMASK;
    667  1.26  rmind 
    668  1.42  rmind 	if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
    669  1.42  rmind 		/* Priority of the dynamic rule. */
    670  1.42  rmind 		prop_dictionary_get_int32(rldict, "prio", &rl->r_priority);
    671  1.42  rmind 	} else {
    672  1.42  rmind 		/* The skip-to index.  No need to validate it. */
    673  1.42  rmind 		prop_dictionary_get_uint32(rldict, "skip-to", &rl->r_skip_to);
    674  1.42  rmind 	}
    675  1.42  rmind 
    676  1.42  rmind 	/* Interface name; register and get the npf-if-id. */
    677  1.36  rmind 	if (prop_dictionary_get_cstring_nocopy(rldict, "ifname", &rname)) {
    678  1.26  rmind 		if ((rl->r_ifid = npf_ifmap_register(rname)) == 0) {
    679  1.26  rmind 			kmem_free(rl, sizeof(npf_rule_t));
    680  1.26  rmind 			return NULL;
    681  1.26  rmind 		}
    682  1.26  rmind 	} else {
    683  1.26  rmind 		rl->r_ifid = 0;
    684  1.26  rmind 	}
    685   1.4  rmind 
    686  1.17  rmind 	/* Key (optional). */
    687  1.17  rmind 	prop_object_t obj = prop_dictionary_get(rldict, "key");
    688  1.17  rmind 	const void *key = prop_data_data_nocopy(obj);
    689  1.17  rmind 
    690  1.17  rmind 	if (key) {
    691  1.17  rmind 		size_t len = prop_data_size(obj);
    692  1.17  rmind 		if (len > NPF_RULE_MAXKEYLEN) {
    693  1.17  rmind 			kmem_free(rl, sizeof(npf_rule_t));
    694  1.17  rmind 			return NULL;
    695  1.17  rmind 		}
    696  1.17  rmind 		memcpy(rl->r_key, key, len);
    697   1.4  rmind 	}
    698  1.18  rmind 
    699  1.36  rmind 	if ((d = prop_dictionary_get(rldict, "info")) != NULL) {
    700  1.36  rmind 		rl->r_info = prop_data_copy(d);
    701  1.36  rmind 	}
    702  1.36  rmind 	return rl;
    703  1.36  rmind }
    704  1.36  rmind 
    705  1.36  rmind static int
    706  1.37  rmind npf_rule_export(const npf_ruleset_t *rlset, const npf_rule_t *rl,
    707  1.37  rmind     prop_dictionary_t rldict)
    708  1.36  rmind {
    709  1.37  rmind 	u_int skip_to = 0;
    710  1.36  rmind 	prop_data_t d;
    711  1.36  rmind 
    712  1.36  rmind 	prop_dictionary_set_uint32(rldict, "attr", rl->r_attr);
    713  1.36  rmind 	prop_dictionary_set_int32(rldict, "prio", rl->r_priority);
    714  1.37  rmind 	if ((rl->r_skip_to & SKIPTO_ADJ_FLAG) == 0) {
    715  1.37  rmind 		skip_to = rl->r_skip_to & SKIPTO_MASK;
    716  1.37  rmind 	}
    717  1.37  rmind 	prop_dictionary_set_uint32(rldict, "skip-to", skip_to);
    718  1.36  rmind 	prop_dictionary_set_int32(rldict, "code-type", rl->r_type);
    719  1.36  rmind 	if (rl->r_code) {
    720  1.36  rmind 		d = prop_data_create_data(rl->r_code, rl->r_clen);
    721  1.36  rmind 		prop_dictionary_set_and_rel(rldict, "code", d);
    722  1.36  rmind 	}
    723  1.36  rmind 
    724  1.36  rmind 	if (rl->r_ifid) {
    725  1.36  rmind 		const char *ifname = npf_ifmap_getname(rl->r_ifid);
    726  1.36  rmind 		prop_dictionary_set_cstring(rldict, "ifname", ifname);
    727  1.36  rmind 	}
    728  1.36  rmind 	prop_dictionary_set_uint64(rldict, "id", rl->r_id);
    729  1.36  rmind 
    730  1.36  rmind 	if (rl->r_name[0]) {
    731  1.36  rmind 		prop_dictionary_set_cstring(rldict, "name", rl->r_name);
    732  1.36  rmind 	}
    733  1.19  rmind 	if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
    734  1.36  rmind 		d = prop_data_create_data(rl->r_key, NPF_RULE_MAXKEYLEN);
    735  1.36  rmind 		prop_dictionary_set_and_rel(rldict, "key", d);
    736  1.18  rmind 	}
    737  1.37  rmind 	if (rl->r_info) {
    738  1.37  rmind 		prop_dictionary_set(rldict, "info", rl->r_info);
    739  1.37  rmind 	}
    740  1.36  rmind 	return 0;
    741  1.17  rmind }
    742  1.17  rmind 
    743  1.17  rmind /*
    744  1.17  rmind  * npf_rule_setcode: assign filter code to the rule.
    745  1.17  rmind  *
    746  1.20  rmind  * => The code must be validated by the caller.
    747  1.20  rmind  * => JIT compilation may be performed here.
    748  1.17  rmind  */
    749  1.17  rmind void
    750  1.17  rmind npf_rule_setcode(npf_rule_t *rl, const int type, void *code, size_t size)
    751  1.17  rmind {
    752  1.25  rmind 	KASSERT(type == NPF_CODE_BPF);
    753  1.28  rmind 
    754  1.28  rmind 	rl->r_type = type;
    755  1.36  rmind 	rl->r_code = code;
    756  1.36  rmind 	rl->r_clen = size;
    757  1.36  rmind 	rl->r_jcode = npf_bpf_compile(code, size);
    758  1.17  rmind }
    759  1.17  rmind 
    760  1.17  rmind /*
    761  1.17  rmind  * npf_rule_setrproc: assign a rule procedure and hold a reference on it.
    762  1.17  rmind  */
    763  1.17  rmind void
    764  1.17  rmind npf_rule_setrproc(npf_rule_t *rl, npf_rproc_t *rp)
    765  1.17  rmind {
    766  1.17  rmind 	npf_rproc_acquire(rp);
    767   1.6  rmind 	rl->r_rproc = rp;
    768   1.1  rmind }
    769   1.1  rmind 
    770   1.1  rmind /*
    771   1.1  rmind  * npf_rule_free: free the specified rule.
    772   1.1  rmind  */
    773   1.1  rmind void
    774   1.1  rmind npf_rule_free(npf_rule_t *rl)
    775   1.1  rmind {
    776   1.4  rmind 	npf_natpolicy_t *np = rl->r_natp;
    777   1.4  rmind 	npf_rproc_t *rp = rl->r_rproc;
    778   1.1  rmind 
    779  1.31  rmind 	if (np && (rl->r_attr & NPF_RULE_KEEPNAT) == 0) {
    780   1.4  rmind 		/* Free NAT policy. */
    781   1.4  rmind 		npf_nat_freepolicy(np);
    782   1.4  rmind 	}
    783   1.4  rmind 	if (rp) {
    784   1.6  rmind 		/* Release rule procedure. */
    785   1.4  rmind 		npf_rproc_release(rp);
    786   1.4  rmind 	}
    787  1.17  rmind 	if (rl->r_code) {
    788  1.20  rmind 		/* Free byte-code. */
    789  1.17  rmind 		kmem_free(rl->r_code, rl->r_clen);
    790   1.1  rmind 	}
    791  1.20  rmind 	if (rl->r_jcode) {
    792  1.20  rmind 		/* Free JIT code. */
    793  1.28  rmind 		bpf_jit_freecode(rl->r_jcode);
    794  1.20  rmind 	}
    795  1.36  rmind 	if (rl->r_info) {
    796  1.36  rmind 		prop_object_release(rl->r_info);
    797  1.18  rmind 	}
    798   1.4  rmind 	kmem_free(rl, sizeof(npf_rule_t));
    799   1.1  rmind }
    800   1.1  rmind 
    801   1.1  rmind /*
    802  1.19  rmind  * npf_rule_getid: return the unique ID of a rule.
    803  1.10  rmind  * npf_rule_getrproc: acquire a reference and return rule procedure, if any.
    804   1.1  rmind  * npf_rule_getnat: get NAT policy assigned to the rule.
    805   1.1  rmind  */
    806   1.1  rmind 
    807  1.19  rmind uint64_t
    808  1.19  rmind npf_rule_getid(const npf_rule_t *rl)
    809  1.19  rmind {
    810  1.19  rmind 	KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    811  1.19  rmind 	return rl->r_id;
    812  1.19  rmind }
    813  1.19  rmind 
    814  1.10  rmind npf_rproc_t *
    815  1.30  rmind npf_rule_getrproc(const npf_rule_t *rl)
    816  1.10  rmind {
    817  1.10  rmind 	npf_rproc_t *rp = rl->r_rproc;
    818  1.10  rmind 
    819  1.10  rmind 	if (rp) {
    820  1.10  rmind 		npf_rproc_acquire(rp);
    821  1.10  rmind 	}
    822  1.10  rmind 	return rp;
    823  1.10  rmind }
    824  1.10  rmind 
    825   1.1  rmind npf_natpolicy_t *
    826   1.1  rmind npf_rule_getnat(const npf_rule_t *rl)
    827   1.1  rmind {
    828   1.4  rmind 	return rl->r_natp;
    829   1.1  rmind }
    830   1.1  rmind 
    831   1.4  rmind /*
    832   1.4  rmind  * npf_rule_setnat: assign NAT policy to the rule and insert into the
    833   1.4  rmind  * NAT policy list in the ruleset.
    834   1.4  rmind  */
    835   1.1  rmind void
    836   1.1  rmind npf_rule_setnat(npf_rule_t *rl, npf_natpolicy_t *np)
    837   1.1  rmind {
    838   1.4  rmind 	KASSERT(rl->r_natp == NULL);
    839   1.4  rmind 	rl->r_natp = np;
    840   1.1  rmind }
    841   1.1  rmind 
    842  1.17  rmind /*
    843  1.17  rmind  * npf_rule_inspect: match the interface, direction and run the filter code.
    844  1.29  rmind  * Returns true if rule matches and false otherwise.
    845  1.17  rmind  */
    846  1.17  rmind static inline bool
    847  1.29  rmind npf_rule_inspect(const npf_rule_t *rl, bpf_args_t *bc_args,
    848  1.29  rmind     const int di_mask, const u_int ifid)
    849  1.17  rmind {
    850  1.17  rmind 	/* Match the interface. */
    851  1.29  rmind 	if (rl->r_ifid && rl->r_ifid != ifid) {
    852  1.17  rmind 		return false;
    853  1.17  rmind 	}
    854  1.17  rmind 
    855  1.17  rmind 	/* Match the direction. */
    856  1.17  rmind 	if ((rl->r_attr & NPF_RULE_DIMASK) != NPF_RULE_DIMASK) {
    857  1.17  rmind 		if ((rl->r_attr & di_mask) == 0)
    858  1.17  rmind 			return false;
    859  1.17  rmind 	}
    860  1.17  rmind 
    861  1.24  rmind 	/* Any code? */
    862  1.36  rmind 	if (!rl->r_code) {
    863  1.24  rmind 		KASSERT(rl->r_jcode == NULL);
    864  1.17  rmind 		return true;
    865  1.17  rmind 	}
    866  1.25  rmind 	KASSERT(rl->r_type == NPF_CODE_BPF);
    867  1.29  rmind 	return npf_bpf_filter(bc_args, rl->r_code, rl->r_jcode) != 0;
    868  1.17  rmind }
    869  1.17  rmind 
    870  1.17  rmind /*
    871  1.17  rmind  * npf_rule_reinspect: re-inspect the dynamic rule by iterating its list.
    872  1.17  rmind  * This is only for the dynamic rules.  Subrules cannot have nested rules.
    873  1.17  rmind  */
    874  1.42  rmind static inline npf_rule_t *
    875  1.42  rmind npf_rule_reinspect(const npf_rule_t *rg, bpf_args_t *bc_args,
    876  1.29  rmind     const int di_mask, const u_int ifid)
    877   1.7  rmind {
    878  1.17  rmind 	npf_rule_t *final_rl = NULL, *rl;
    879  1.17  rmind 
    880  1.42  rmind 	KASSERT(NPF_DYNAMIC_GROUP_P(rg->r_attr));
    881   1.7  rmind 
    882  1.42  rmind 	for (rl = rg->r_subset; rl; rl = rl->r_next) {
    883  1.42  rmind 		KASSERT(!final_rl || rl->r_priority >= final_rl->r_priority);
    884  1.29  rmind 		if (!npf_rule_inspect(rl, bc_args, di_mask, ifid)) {
    885   1.7  rmind 			continue;
    886  1.17  rmind 		}
    887  1.17  rmind 		if (rl->r_attr & NPF_RULE_FINAL) {
    888  1.17  rmind 			return rl;
    889  1.17  rmind 		}
    890  1.17  rmind 		final_rl = rl;
    891   1.7  rmind 	}
    892  1.17  rmind 	return final_rl;
    893   1.7  rmind }
    894   1.1  rmind 
    895   1.1  rmind /*
    896   1.7  rmind  * npf_ruleset_inspect: inspect the packet against the given ruleset.
    897   1.1  rmind  *
    898  1.25  rmind  * Loop through the rules in the set and run the byte-code of each rule
    899   1.7  rmind  * against the packet (nbuf chain).  If sub-ruleset is found, inspect it.
    900   1.1  rmind  */
    901   1.1  rmind npf_rule_t *
    902  1.34  rmind npf_ruleset_inspect(npf_cache_t *npc, const npf_ruleset_t *rlset,
    903  1.34  rmind     const int di, const int layer)
    904   1.1  rmind {
    905  1.34  rmind 	nbuf_t *nbuf = npc->npc_nbuf;
    906   1.7  rmind 	const int di_mask = (di & PFIL_IN) ? NPF_RULE_IN : NPF_RULE_OUT;
    907  1.17  rmind 	const u_int nitems = rlset->rs_nitems;
    908  1.29  rmind 	const u_int ifid = nbuf->nb_ifid;
    909  1.17  rmind 	npf_rule_t *final_rl = NULL;
    910  1.29  rmind 	bpf_args_t bc_args;
    911  1.17  rmind 	u_int n = 0;
    912   1.1  rmind 
    913  1.33  rmind 	KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
    914  1.29  rmind 
    915  1.33  rmind 	/*
    916  1.33  rmind 	 * Prepare the external memory store and the arguments for
    917  1.33  rmind 	 * the BPF programs to be executed.
    918  1.33  rmind 	 */
    919  1.33  rmind 	uint32_t bc_words[NPF_BPF_NWORDS];
    920  1.34  rmind 	npf_bpf_prepare(npc, &bc_args, bc_words);
    921  1.17  rmind 
    922  1.17  rmind 	while (n < nitems) {
    923  1.17  rmind 		npf_rule_t *rl = rlset->rs_rules[n];
    924  1.37  rmind 		const u_int skip_to = rl->r_skip_to & SKIPTO_MASK;
    925  1.17  rmind 		const uint32_t attr = rl->r_attr;
    926  1.17  rmind 
    927  1.16  rmind 		KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    928  1.17  rmind 		KASSERT(n < skip_to);
    929   1.1  rmind 
    930  1.17  rmind 		/* Group is a barrier: return a matching if found any. */
    931  1.17  rmind 		if ((attr & NPF_RULE_GROUP) != 0 && final_rl) {
    932  1.17  rmind 			break;
    933  1.17  rmind 		}
    934  1.17  rmind 
    935  1.17  rmind 		/* Main inspection of the rule. */
    936  1.29  rmind 		if (!npf_rule_inspect(rl, &bc_args, di_mask, ifid)) {
    937  1.17  rmind 			n = skip_to;
    938   1.1  rmind 			continue;
    939   1.1  rmind 		}
    940  1.17  rmind 
    941  1.17  rmind 		if (NPF_DYNAMIC_GROUP_P(attr)) {
    942  1.17  rmind 			/*
    943  1.17  rmind 			 * If this is a dynamic rule, re-inspect the subrules.
    944  1.17  rmind 			 * If it has any matching rule, then it is final.
    945  1.17  rmind 			 */
    946  1.29  rmind 			rl = npf_rule_reinspect(rl, &bc_args, di_mask, ifid);
    947  1.17  rmind 			if (rl != NULL) {
    948  1.17  rmind 				final_rl = rl;
    949  1.17  rmind 				break;
    950  1.17  rmind 			}
    951  1.17  rmind 		} else if ((attr & NPF_RULE_GROUP) == 0) {
    952  1.17  rmind 			/*
    953  1.17  rmind 			 * Groups themselves are not matching.
    954  1.17  rmind 			 */
    955  1.17  rmind 			final_rl = rl;
    956   1.1  rmind 		}
    957  1.17  rmind 
    958   1.1  rmind 		/* Set the matching rule and check for "final". */
    959  1.17  rmind 		if (attr & NPF_RULE_FINAL) {
    960   1.2  rmind 			break;
    961   1.1  rmind 		}
    962  1.17  rmind 		n++;
    963   1.2  rmind 	}
    964  1.16  rmind 
    965  1.16  rmind 	KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    966   1.7  rmind 	return final_rl;
    967   1.1  rmind }
    968   1.1  rmind 
    969   1.1  rmind /*
    970  1.17  rmind  * npf_rule_conclude: return decision and the flags for conclusion.
    971   1.1  rmind  *
    972   1.1  rmind  * => Returns ENETUNREACH if "block" and 0 if "pass".
    973   1.1  rmind  */
    974   1.1  rmind int
    975  1.17  rmind npf_rule_conclude(const npf_rule_t *rl, int *retfl)
    976   1.1  rmind {
    977   1.1  rmind 	/* If not passing - drop the packet. */
    978   1.4  rmind 	*retfl = rl->r_attr;
    979  1.17  rmind 	return (rl->r_attr & NPF_RULE_PASS) ? 0 : ENETUNREACH;
    980   1.1  rmind }
    981  1.41  rmind 
    982  1.41  rmind 
    983  1.41  rmind #if defined(DDB) || defined(_NPF_TESTING)
    984  1.41  rmind 
    985  1.41  rmind void
    986  1.41  rmind npf_ruleset_dump(const char *name)
    987  1.41  rmind {
    988  1.41  rmind 	npf_ruleset_t *rlset = npf_config_ruleset();
    989  1.41  rmind 	npf_rule_t *rg, *rl;
    990  1.41  rmind 
    991  1.41  rmind 	LIST_FOREACH(rg, &rlset->rs_dynamic, r_dentry) {
    992  1.41  rmind 		printf("ruleset '%s':\n", rg->r_name);
    993  1.42  rmind 		for (rl = rg->r_subset; rl; rl = rl->r_next) {
    994  1.41  rmind 			printf("\tid %"PRIu64", key: ", rl->r_id);
    995  1.41  rmind 			for (u_int i = 0; i < NPF_RULE_MAXKEYLEN; i++)
    996  1.41  rmind 				printf("%x", rl->r_key[i]);
    997  1.41  rmind 			printf("\n");
    998  1.41  rmind 		}
    999  1.41  rmind 	}
   1000  1.41  rmind }
   1001  1.41  rmind 
   1002  1.41  rmind #endif
   1003