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