Home | History | Annotate | Line # | Download | only in npf
npf_ruleset.c revision 1.4
      1  1.4  rmind /*	$NetBSD: npf_ruleset.c,v 1.4 2010/12/18 01:07:25 rmind Exp $	*/
      2  1.1  rmind 
      3  1.1  rmind /*-
      4  1.1  rmind  * Copyright (c) 2009-2010 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 #ifdef _KERNEL
     37  1.1  rmind #include <sys/cdefs.h>
     38  1.4  rmind __KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.4 2010/12/18 01:07:25 rmind Exp $");
     39  1.1  rmind 
     40  1.1  rmind #include <sys/param.h>
     41  1.1  rmind #include <sys/kernel.h>
     42  1.1  rmind 
     43  1.1  rmind #include <sys/atomic.h>
     44  1.1  rmind #include <sys/kmem.h>
     45  1.1  rmind #include <sys/pool.h>
     46  1.1  rmind #include <sys/queue.h>
     47  1.1  rmind #include <sys/types.h>
     48  1.1  rmind 
     49  1.3  rmind #include <net/pfil.h>
     50  1.1  rmind #include <net/if.h>
     51  1.3  rmind #endif
     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 /* Ruleset structre (queue and default rule). */
     57  1.4  rmind struct npf_ruleset {
     58  1.4  rmind 	TAILQ_HEAD(, npf_rule)	rs_queue;
     59  1.4  rmind 	npf_rule_t *		rs_default;
     60  1.4  rmind };
     61  1.4  rmind 
     62  1.4  rmind /* Rule hook entry. */
     63  1.1  rmind struct npf_hook {
     64  1.3  rmind 	void			(*hk_fn)(npf_cache_t *, nbuf_t *, void *);
     65  1.3  rmind 	void *			hk_arg;
     66  1.3  rmind 	LIST_ENTRY(npf_hook)	hk_entry;
     67  1.1  rmind };
     68  1.1  rmind 
     69  1.4  rmind /* Rule processing structure. */
     70  1.4  rmind struct npf_rproc {
     71  1.4  rmind 	/* Reference count. */
     72  1.4  rmind 	u_int			rp_refcnt;
     73  1.4  rmind 	/* Normalization options. */
     74  1.4  rmind 	bool			rp_rnd_ipid;
     75  1.4  rmind 	bool			rp_no_df;
     76  1.4  rmind 	u_int			rp_minttl;
     77  1.4  rmind 	u_int			rp_maxmss;
     78  1.4  rmind 	/* Logging interface. */
     79  1.4  rmind 	u_int			rp_log_ifid;
     80  1.1  rmind };
     81  1.1  rmind 
     82  1.1  rmind /* Rule structure. */
     83  1.1  rmind struct npf_rule {
     84  1.4  rmind 	TAILQ_ENTRY(npf_rule)	r_entry;
     85  1.1  rmind 	/* Optional: sub-ruleset, NAT policy. */
     86  1.4  rmind 	npf_ruleset_t		r_subset;
     87  1.4  rmind 	npf_natpolicy_t *	r_natp;
     88  1.1  rmind 	/* Rule priority: (highest) 0, 1, 2 ... n (lowest). */
     89  1.4  rmind 	u_int			r_priority;
     90  1.1  rmind 	/* N-code to process. */
     91  1.4  rmind 	void *			r_ncode;
     92  1.4  rmind 	size_t			r_nc_size;
     93  1.1  rmind 	/* Attributes of this rule. */
     94  1.4  rmind 	uint32_t		r_attr;
     95  1.1  rmind 	/* Interface. */
     96  1.4  rmind 	u_int			r_ifid;
     97  1.1  rmind 	/* Hit counter. */
     98  1.4  rmind 	u_long			r_hitcount;
     99  1.4  rmind 	/* Rule processing data. */
    100  1.4  rmind 	npf_rproc_t *		r_rproc;
    101  1.1  rmind 	/* List of hooks to process on match. */
    102  1.4  rmind 	kmutex_t		r_hooks_lock;
    103  1.4  rmind 	LIST_HEAD(, npf_hook)	r_hooks;
    104  1.1  rmind };
    105  1.1  rmind 
    106  1.1  rmind npf_ruleset_t *
    107  1.1  rmind npf_ruleset_create(void)
    108  1.1  rmind {
    109  1.1  rmind 	npf_ruleset_t *rlset;
    110  1.1  rmind 
    111  1.1  rmind 	rlset = kmem_zalloc(sizeof(npf_ruleset_t), KM_SLEEP);
    112  1.1  rmind 	TAILQ_INIT(&rlset->rs_queue);
    113  1.1  rmind 	return rlset;
    114  1.1  rmind }
    115  1.1  rmind 
    116  1.1  rmind void
    117  1.1  rmind npf_ruleset_destroy(npf_ruleset_t *rlset)
    118  1.1  rmind {
    119  1.1  rmind 	npf_rule_t *rl;
    120  1.1  rmind 
    121  1.1  rmind 	while ((rl = TAILQ_FIRST(&rlset->rs_queue)) != NULL) {
    122  1.1  rmind 		TAILQ_REMOVE(&rlset->rs_queue, rl, r_entry);
    123  1.1  rmind 		npf_rule_free(rl);
    124  1.1  rmind 	}
    125  1.1  rmind 	kmem_free(rlset, sizeof(npf_ruleset_t));
    126  1.1  rmind }
    127  1.1  rmind 
    128  1.1  rmind /*
    129  1.1  rmind  * npf_ruleset_insert: insert the rule into the specified ruleset.
    130  1.1  rmind  *
    131  1.1  rmind  * Note: multiple rules at the same priority are allowed.
    132  1.1  rmind  */
    133  1.1  rmind void
    134  1.1  rmind npf_ruleset_insert(npf_ruleset_t *rlset, npf_rule_t *rl)
    135  1.1  rmind {
    136  1.1  rmind 	npf_rule_t *it;
    137  1.1  rmind 
    138  1.1  rmind 	if (rl->r_attr & NPF_RULE_DEFAULT) {
    139  1.1  rmind 		rlset->rs_default = rl;
    140  1.1  rmind 		return;
    141  1.1  rmind 	}
    142  1.1  rmind 	TAILQ_FOREACH(it, &rlset->rs_queue, r_entry) {
    143  1.1  rmind 		/* Rule priority: (highest) 0, 1, 2, 4 ... n (lowest). */
    144  1.1  rmind 		if (it->r_priority > rl->r_priority)
    145  1.1  rmind 			break;
    146  1.1  rmind 	}
    147  1.1  rmind 	if (it == NULL) {
    148  1.1  rmind 		TAILQ_INSERT_TAIL(&rlset->rs_queue, rl, r_entry);
    149  1.1  rmind 	} else {
    150  1.1  rmind 		TAILQ_INSERT_BEFORE(it, rl, r_entry);
    151  1.1  rmind 	}
    152  1.1  rmind }
    153  1.1  rmind 
    154  1.1  rmind /*
    155  1.4  rmind  * npf_ruleset_matchnat: find a matching NAT policy in the ruleset.
    156  1.1  rmind  */
    157  1.4  rmind npf_rule_t *
    158  1.4  rmind npf_ruleset_matchnat(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
    159  1.1  rmind {
    160  1.4  rmind 	npf_rule_t *rl;
    161  1.1  rmind 
    162  1.4  rmind 	/* Find a matching NAT policy in the old ruleset. */
    163  1.4  rmind 	TAILQ_FOREACH(rl, &rlset->rs_queue, r_entry) {
    164  1.4  rmind 		if (npf_nat_matchpolicy(rl->r_natp, mnp))
    165  1.4  rmind 			break;
    166  1.4  rmind 	}
    167  1.4  rmind 	return rl;
    168  1.1  rmind }
    169  1.1  rmind 
    170  1.1  rmind /*
    171  1.4  rmind  * npf_ruleset_natreload: minimum reload of NAT policies by maching
    172  1.4  rmind  * two (active  and new) NAT rulesets.
    173  1.4  rmind  *
    174  1.4  rmind  * => Active ruleset should be exclusively locked.
    175  1.1  rmind  */
    176  1.4  rmind void
    177  1.4  rmind npf_ruleset_natreload(npf_ruleset_t *nrlset, npf_ruleset_t *arlset)
    178  1.1  rmind {
    179  1.4  rmind 	npf_natpolicy_t *np, *anp;
    180  1.4  rmind 	npf_rule_t *rl, *arl;
    181  1.4  rmind 
    182  1.4  rmind 	KASSERT(npf_core_locked());
    183  1.1  rmind 
    184  1.4  rmind 	/* Scan a new NAT ruleset against NAT policies in old ruleset. */
    185  1.4  rmind 	TAILQ_FOREACH(rl, &nrlset->rs_queue, r_entry) {
    186  1.4  rmind 		np = rl->r_natp;
    187  1.4  rmind 		arl = npf_ruleset_matchnat(arlset, np);
    188  1.4  rmind 		if (arl == NULL) {
    189  1.4  rmind 			continue;
    190  1.4  rmind 		}
    191  1.4  rmind 		/* On match - we exchange NAT policies. */
    192  1.4  rmind 		anp = arl->r_natp;
    193  1.4  rmind 		rl->r_natp = anp;
    194  1.4  rmind 		arl->r_natp = np;
    195  1.1  rmind 	}
    196  1.4  rmind }
    197  1.4  rmind 
    198  1.4  rmind npf_rproc_t *
    199  1.4  rmind npf_rproc_create(prop_dictionary_t rpdict)
    200  1.4  rmind {
    201  1.4  rmind 	npf_rproc_t *rp;
    202  1.4  rmind 	prop_object_t obj;
    203  1.4  rmind 
    204  1.4  rmind 	rp = kmem_alloc(sizeof(npf_rproc_t), KM_SLEEP);
    205  1.4  rmind 	rp->rp_refcnt = 1;
    206  1.4  rmind 
    207  1.4  rmind 	/* Logging interface ID (integer). */
    208  1.4  rmind 	obj = prop_dictionary_get(rpdict, "log-interface");
    209  1.4  rmind 	rp->rp_log_ifid = prop_number_integer_value(obj);
    210  1.4  rmind 
    211  1.4  rmind 	/* Randomize IP ID (bool). */
    212  1.4  rmind 	obj = prop_dictionary_get(rpdict, "randomize-id");
    213  1.4  rmind 	rp->rp_rnd_ipid = prop_bool_true(obj);
    214  1.4  rmind 
    215  1.4  rmind 	/* IP_DF flag cleansing (bool). */
    216  1.4  rmind 	obj = prop_dictionary_get(rpdict, "no-df");
    217  1.4  rmind 	rp->rp_no_df = prop_bool_true(obj);
    218  1.4  rmind 
    219  1.4  rmind 	/* Minimum IP TTL (integer). */
    220  1.4  rmind 	obj = prop_dictionary_get(rpdict, "min-ttl");
    221  1.4  rmind 	rp->rp_minttl = prop_number_integer_value(obj);
    222  1.4  rmind 
    223  1.4  rmind 	/* Maximum TCP MSS (integer). */
    224  1.4  rmind 	obj = prop_dictionary_get(rpdict, "max-mss");
    225  1.4  rmind 	rp->rp_maxmss = prop_number_integer_value(obj);
    226  1.4  rmind 
    227  1.4  rmind 	return rp;
    228  1.4  rmind }
    229  1.4  rmind 
    230  1.4  rmind npf_rproc_t *
    231  1.4  rmind npf_rproc_return(npf_rule_t *rl)
    232  1.4  rmind {
    233  1.4  rmind 	npf_rproc_t *rp = rl->r_rproc;
    234  1.4  rmind 
    235  1.4  rmind 	if (rp) {
    236  1.4  rmind 		atomic_inc_uint(&rp->rp_refcnt);
    237  1.1  rmind 	}
    238  1.4  rmind 	return rp;
    239  1.4  rmind }
    240  1.3  rmind 
    241  1.4  rmind void
    242  1.4  rmind npf_rproc_release(npf_rproc_t *rp)
    243  1.4  rmind {
    244  1.3  rmind 
    245  1.4  rmind 	/* Destroy on last reference. */
    246  1.4  rmind 	if (atomic_dec_uint_nv(&rp->rp_refcnt) != 0) {
    247  1.4  rmind 		return;
    248  1.4  rmind 	}
    249  1.4  rmind 	kmem_free(rp, sizeof(npf_rproc_t));
    250  1.1  rmind }
    251  1.2  rmind 
    252  1.1  rmind void
    253  1.4  rmind npf_rproc_run(npf_cache_t *npc, nbuf_t *nbuf, npf_rproc_t *rp)
    254  1.1  rmind {
    255  1.1  rmind 
    256  1.4  rmind 	KASSERT(rp->rp_refcnt > 0);
    257  1.4  rmind 
    258  1.4  rmind 	/* Normalize the packet, if required. */
    259  1.4  rmind 	(void)npf_normalize(npc, nbuf,
    260  1.4  rmind 	    rp->rp_rnd_ipid, rp->rp_no_df, rp->rp_minttl, rp->rp_maxmss);
    261  1.4  rmind 
    262  1.4  rmind 	/* Log packet, if required. */
    263  1.4  rmind 	if (rp->rp_log_ifid) {
    264  1.4  rmind 		npf_log_packet(npc, nbuf, rp->rp_log_ifid);
    265  1.4  rmind 	}
    266  1.4  rmind 
    267  1.1  rmind }
    268  1.1  rmind 
    269  1.1  rmind /*
    270  1.4  rmind  * npf_rule_alloc: allocate a rule and copy ncode from user-space.
    271  1.4  rmind  *
    272  1.4  rmind  * => N-code should be validated by the caller.
    273  1.1  rmind  */
    274  1.4  rmind npf_rule_t *
    275  1.4  rmind npf_rule_alloc(prop_dictionary_t rldict, void *nc, size_t nc_size)
    276  1.1  rmind {
    277  1.4  rmind 	npf_rule_t *rl;
    278  1.4  rmind 	prop_object_t obj;
    279  1.4  rmind 	int errat;
    280  1.1  rmind 
    281  1.4  rmind 	/* Allocate a rule structure. */
    282  1.4  rmind 	rl = kmem_alloc(sizeof(npf_rule_t), KM_SLEEP);
    283  1.4  rmind 	TAILQ_INIT(&rl->r_subset.rs_queue);
    284  1.4  rmind 	mutex_init(&rl->r_hooks_lock, MUTEX_DEFAULT, IPL_SOFTNET);
    285  1.4  rmind 	LIST_INIT(&rl->r_hooks);
    286  1.4  rmind 	rl->r_hitcount = 0;
    287  1.4  rmind 	rl->r_natp = NULL;
    288  1.4  rmind 
    289  1.4  rmind 	/* N-code. */
    290  1.4  rmind 	KASSERT(nc == NULL || npf_ncode_validate(nc, nc_size, &errat) == 0);
    291  1.4  rmind 	rl->r_ncode = nc;
    292  1.4  rmind 	rl->r_nc_size = nc_size;
    293  1.4  rmind 
    294  1.4  rmind 	/* Attributes (integer). */
    295  1.4  rmind 	obj = prop_dictionary_get(rldict, "attributes");
    296  1.4  rmind 	rl->r_attr = prop_number_integer_value(obj);
    297  1.4  rmind 
    298  1.4  rmind 	/* Priority (integer). */
    299  1.4  rmind 	obj = prop_dictionary_get(rldict, "priority");
    300  1.4  rmind 	rl->r_priority = prop_number_integer_value(obj);
    301  1.4  rmind 
    302  1.4  rmind 	/* Interface ID (integer). */
    303  1.4  rmind 	obj = prop_dictionary_get(rldict, "interface");
    304  1.4  rmind 	rl->r_ifid = prop_number_integer_value(obj);
    305  1.4  rmind 
    306  1.4  rmind 	/* Create rule processing structure, if any. */
    307  1.4  rmind 	if (rl->r_attr & (NPF_RULE_LOG | NPF_RULE_NORMALIZE)) {
    308  1.4  rmind 		rl->r_rproc = npf_rproc_create(rldict);
    309  1.4  rmind 	} else {
    310  1.4  rmind 		rl->r_rproc = NULL;
    311  1.4  rmind 	}
    312  1.4  rmind 	return rl;
    313  1.1  rmind }
    314  1.1  rmind 
    315  1.1  rmind /*
    316  1.1  rmind  * npf_rule_free: free the specified rule.
    317  1.1  rmind  */
    318  1.1  rmind void
    319  1.1  rmind npf_rule_free(npf_rule_t *rl)
    320  1.1  rmind {
    321  1.4  rmind 	npf_natpolicy_t *np = rl->r_natp;
    322  1.4  rmind 	npf_rproc_t *rp = rl->r_rproc;
    323  1.1  rmind 
    324  1.4  rmind 	if (np) {
    325  1.4  rmind 		/* Free NAT policy. */
    326  1.4  rmind 		npf_nat_freepolicy(np);
    327  1.4  rmind 	}
    328  1.4  rmind 	if (rp) {
    329  1.4  rmind 		/* Release/free rule processing structure. */
    330  1.4  rmind 		npf_rproc_release(rp);
    331  1.4  rmind 	}
    332  1.1  rmind 	if (rl->r_ncode) {
    333  1.4  rmind 		/* Free n-code. */
    334  1.1  rmind 		npf_ncode_free(rl->r_ncode, rl->r_nc_size);
    335  1.1  rmind 	}
    336  1.4  rmind 	mutex_destroy(&rl->r_hooks_lock);
    337  1.4  rmind 	kmem_free(rl, sizeof(npf_rule_t));
    338  1.1  rmind }
    339  1.1  rmind 
    340  1.1  rmind /*
    341  1.1  rmind  * npf_rule_subset: return sub-ruleset, if any.
    342  1.1  rmind  * npf_rule_getnat: get NAT policy assigned to the rule.
    343  1.1  rmind  */
    344  1.1  rmind 
    345  1.1  rmind npf_ruleset_t *
    346  1.1  rmind npf_rule_subset(npf_rule_t *rl)
    347  1.1  rmind {
    348  1.1  rmind 	return &rl->r_subset;
    349  1.1  rmind }
    350  1.1  rmind 
    351  1.1  rmind npf_natpolicy_t *
    352  1.1  rmind npf_rule_getnat(const npf_rule_t *rl)
    353  1.1  rmind {
    354  1.4  rmind 	return rl->r_natp;
    355  1.1  rmind }
    356  1.1  rmind 
    357  1.4  rmind /*
    358  1.4  rmind  * npf_rule_setnat: assign NAT policy to the rule and insert into the
    359  1.4  rmind  * NAT policy list in the ruleset.
    360  1.4  rmind  */
    361  1.1  rmind void
    362  1.1  rmind npf_rule_setnat(npf_rule_t *rl, npf_natpolicy_t *np)
    363  1.1  rmind {
    364  1.3  rmind 
    365  1.4  rmind 	KASSERT(rl->r_natp == NULL);
    366  1.4  rmind 	rl->r_natp = np;
    367  1.1  rmind }
    368  1.1  rmind 
    369  1.1  rmind /*
    370  1.1  rmind  * npf_hook_register: register action hook in the rule.
    371  1.1  rmind  */
    372  1.1  rmind npf_hook_t *
    373  1.1  rmind npf_hook_register(npf_rule_t *rl,
    374  1.3  rmind     void (*fn)(npf_cache_t *, nbuf_t *, void *), void *arg)
    375  1.1  rmind {
    376  1.1  rmind 	npf_hook_t *hk;
    377  1.1  rmind 
    378  1.1  rmind 	hk = kmem_alloc(sizeof(npf_hook_t), KM_SLEEP);
    379  1.1  rmind 	if (hk != NULL) {
    380  1.1  rmind 		hk->hk_fn = fn;
    381  1.1  rmind 		hk->hk_arg = arg;
    382  1.4  rmind 		mutex_enter(&rl->r_hooks_lock);
    383  1.1  rmind 		LIST_INSERT_HEAD(&rl->r_hooks, hk, hk_entry);
    384  1.4  rmind 		mutex_exit(&rl->r_hooks_lock);
    385  1.1  rmind 	}
    386  1.1  rmind 	return hk;
    387  1.1  rmind }
    388  1.1  rmind 
    389  1.1  rmind /*
    390  1.1  rmind  * npf_hook_unregister: unregister a specified hook.
    391  1.1  rmind  *
    392  1.1  rmind  * => Hook should have been registered in the rule.
    393  1.1  rmind  */
    394  1.1  rmind void
    395  1.1  rmind npf_hook_unregister(npf_rule_t *rl, npf_hook_t *hk)
    396  1.1  rmind {
    397  1.1  rmind 
    398  1.4  rmind 	mutex_enter(&rl->r_hooks_lock);
    399  1.1  rmind 	LIST_REMOVE(hk, hk_entry);
    400  1.4  rmind 	mutex_exit(&rl->r_hooks_lock);
    401  1.1  rmind 	kmem_free(hk, sizeof(npf_hook_t));
    402  1.1  rmind }
    403  1.1  rmind 
    404  1.1  rmind /*
    405  1.2  rmind  * npf_ruleset_match: inspect the packet against the given ruleset.
    406  1.1  rmind  *
    407  1.2  rmind  * Loop for each rule in the set and run n-code processor of each rule
    408  1.2  rmind  * against the packet (nbuf chain).
    409  1.1  rmind  */
    410  1.1  rmind npf_rule_t *
    411  1.2  rmind npf_ruleset_match(npf_ruleset_t *rlset, npf_cache_t *npc, nbuf_t *nbuf,
    412  1.1  rmind     struct ifnet *ifp, const int di, const int layer)
    413  1.1  rmind {
    414  1.1  rmind 	npf_rule_t *final_rl = NULL, *rl;
    415  1.1  rmind 
    416  1.1  rmind 	KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
    417  1.2  rmind 
    418  1.1  rmind 	TAILQ_FOREACH(rl, &rlset->rs_queue, r_entry) {
    419  1.1  rmind 		KASSERT(!final_rl || rl->r_priority >= final_rl->r_priority);
    420  1.1  rmind 
    421  1.1  rmind 		/* Match the interface. */
    422  1.1  rmind 		if (rl->r_ifid && rl->r_ifid != ifp->if_index) {
    423  1.1  rmind 			continue;
    424  1.1  rmind 		}
    425  1.1  rmind 		/* Match the direction. */
    426  1.1  rmind 		if ((rl->r_attr & NPF_RULE_DIMASK) != NPF_RULE_DIMASK) {
    427  1.1  rmind 			const int di_mask =
    428  1.1  rmind 			    (di & PFIL_IN) ? NPF_RULE_IN : NPF_RULE_OUT;
    429  1.1  rmind 
    430  1.1  rmind 			if ((rl->r_attr & di_mask) == 0)
    431  1.1  rmind 				continue;
    432  1.1  rmind 		}
    433  1.1  rmind 		/* Process the n-code, if any. */
    434  1.1  rmind 		const void *nc = rl->r_ncode;
    435  1.1  rmind 		if (nc && npf_ncode_process(npc, nc, nbuf, layer)) {
    436  1.1  rmind 			continue;
    437  1.1  rmind 		}
    438  1.1  rmind 		/* Set the matching rule and check for "final". */
    439  1.1  rmind 		final_rl = rl;
    440  1.1  rmind 		if (rl->r_attr & NPF_RULE_FINAL) {
    441  1.2  rmind 			break;
    442  1.1  rmind 		}
    443  1.1  rmind 	}
    444  1.1  rmind 	return final_rl;
    445  1.1  rmind }
    446  1.1  rmind 
    447  1.1  rmind /*
    448  1.1  rmind  * npf_ruleset_inspect: inspection of the main ruleset for filtering.
    449  1.2  rmind  * If sub-ruleset is found, inspect it.
    450  1.2  rmind  *
    451  1.2  rmind  * => If found, ruleset is kept read-locked.
    452  1.2  rmind  * => Caller should protect the nbuf chain.
    453  1.1  rmind  */
    454  1.1  rmind npf_rule_t *
    455  1.1  rmind npf_ruleset_inspect(npf_cache_t *npc, nbuf_t *nbuf,
    456  1.1  rmind     struct ifnet *ifp, const int di, const int layer)
    457  1.1  rmind {
    458  1.4  rmind 	npf_ruleset_t *rlset;
    459  1.1  rmind 	npf_rule_t *rl;
    460  1.2  rmind 	bool defed;
    461  1.1  rmind 
    462  1.2  rmind 	defed = false;
    463  1.4  rmind 	npf_core_enter();
    464  1.4  rmind 	rlset = npf_core_ruleset();
    465  1.2  rmind reinspect:
    466  1.2  rmind 	rl = npf_ruleset_match(rlset, npc, nbuf, ifp, di, layer);
    467  1.2  rmind 
    468  1.2  rmind 	/* If no final rule, then - default. */
    469  1.2  rmind 	if (rl == NULL && !defed) {
    470  1.4  rmind 		npf_ruleset_t *mainrlset = npf_core_ruleset();
    471  1.4  rmind 		rl = mainrlset->rs_default;
    472  1.2  rmind 		defed = true;
    473  1.2  rmind 	}
    474  1.2  rmind 	/* Inspect the sub-ruleset, if any. */
    475  1.2  rmind 	if (rl && !TAILQ_EMPTY(&rl->r_subset.rs_queue)) {
    476  1.2  rmind 		rlset = &rl->r_subset;
    477  1.2  rmind 		goto reinspect;
    478  1.2  rmind 	}
    479  1.1  rmind 	if (rl == NULL) {
    480  1.4  rmind 		npf_core_exit();
    481  1.1  rmind 	}
    482  1.1  rmind 	return rl;
    483  1.1  rmind }
    484  1.1  rmind 
    485  1.1  rmind /*
    486  1.1  rmind  * npf_rule_apply: apply the rule i.e. run hooks and return appropriate value.
    487  1.1  rmind  *
    488  1.1  rmind  * => Returns ENETUNREACH if "block" and 0 if "pass".
    489  1.1  rmind  * => Releases the ruleset lock.
    490  1.1  rmind  */
    491  1.1  rmind int
    492  1.4  rmind npf_rule_apply(npf_cache_t *npc, nbuf_t *nbuf, npf_rule_t *rl, int *retfl)
    493  1.1  rmind {
    494  1.1  rmind 	npf_hook_t *hk;
    495  1.4  rmind 	int error;
    496  1.1  rmind 
    497  1.4  rmind 	KASSERT(npf_core_locked());
    498  1.1  rmind 
    499  1.1  rmind 	/* Update the "hit" counter. */
    500  1.1  rmind 	if (rl->r_attr & NPF_RULE_COUNT) {
    501  1.1  rmind 		atomic_inc_ulong(&rl->r_hitcount);
    502  1.1  rmind 	}
    503  1.1  rmind 
    504  1.1  rmind 	/* If not passing - drop the packet. */
    505  1.1  rmind 	if ((rl->r_attr & NPF_RULE_PASS) == 0) {
    506  1.4  rmind 		error = ENETUNREACH;
    507  1.4  rmind 		goto done;
    508  1.1  rmind 	}
    509  1.4  rmind 	error = 0;
    510  1.1  rmind 
    511  1.1  rmind 	/* Passing.  Run the hooks. */
    512  1.1  rmind 	LIST_FOREACH(hk, &rl->r_hooks, hk_entry) {
    513  1.1  rmind 		KASSERT(hk->hk_fn != NULL);
    514  1.3  rmind 		(*hk->hk_fn)(npc, nbuf, hk->hk_arg);
    515  1.3  rmind 	}
    516  1.4  rmind done:
    517  1.4  rmind 	*retfl = rl->r_attr;
    518  1.4  rmind 	npf_core_exit();
    519  1.4  rmind 	return error;
    520  1.1  rmind }
    521  1.1  rmind 
    522  1.1  rmind #if defined(DDB) || defined(_NPF_TESTING)
    523  1.1  rmind 
    524  1.1  rmind void
    525  1.1  rmind npf_rulenc_dump(npf_rule_t *rl)
    526  1.1  rmind {
    527  1.1  rmind 	uint32_t *op = rl->r_ncode;
    528  1.1  rmind 	size_t n = rl->r_nc_size;
    529  1.1  rmind 
    530  1.2  rmind 	while (n) {
    531  1.1  rmind 		printf("\t> |0x%02x|\n", (uint32_t)*op);
    532  1.1  rmind 		op++;
    533  1.1  rmind 		n -= sizeof(*op);
    534  1.2  rmind 	}
    535  1.1  rmind 	printf("-> %s\n", (rl->r_attr & NPF_RULE_PASS) ? "pass" : "block");
    536  1.1  rmind }
    537  1.1  rmind 
    538  1.1  rmind #endif
    539