Home | History | Annotate | Line # | Download | only in npf
npf_ruleset.c revision 1.52.6.1
      1       1.1     rmind /*-
      2      1.51     rmind  * Copyright (c) 2020 Mindaugas Rasiukevicius <rmind at noxt eu>
      3  1.52.6.1  perseant  * Copyright (c) 2009-2025 The NetBSD Foundation, Inc.
      4       1.1     rmind  * All rights reserved.
      5       1.1     rmind  *
      6       1.1     rmind  * This material is based upon work partially supported by The
      7       1.1     rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      8       1.1     rmind  *
      9       1.1     rmind  * Redistribution and use in source and binary forms, with or without
     10       1.1     rmind  * modification, are permitted provided that the following conditions
     11       1.1     rmind  * are met:
     12       1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     13       1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     14       1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     16       1.1     rmind  *    documentation and/or other materials provided with the distribution.
     17       1.1     rmind  *
     18       1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19       1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20       1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21       1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22       1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23       1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24       1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25       1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26       1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27       1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28       1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     29       1.1     rmind  */
     30       1.1     rmind 
     31       1.1     rmind /*
     32       1.1     rmind  * NPF ruleset module.
     33       1.1     rmind  */
     34       1.1     rmind 
     35      1.43  christos #ifdef _KERNEL
     36       1.1     rmind #include <sys/cdefs.h>
     37  1.52.6.1  perseant __KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.52.6.1 2025/08/02 05:57:48 perseant 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.52.6.1  perseant #include <sys/kauth.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.43  christos #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.51     rmind 	unsigned		rs_slots;
     72      1.51     rmind 	unsigned		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.51     rmind 	unsigned		r_ifid;
     82      1.51     rmind 	unsigned		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.51     rmind 	unsigned		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.47     rmind 	nvlist_t *		r_info;
    121      1.47     rmind 	size_t			r_info_len;
    122  1.52.6.1  perseant 
    123  1.52.6.1  perseant 	rid_t uid;
    124  1.52.6.1  perseant 	rid_t gid;
    125      1.36     rmind };
    126      1.17     rmind 
    127      1.37     rmind #define	SKIPTO_ADJ_FLAG		(1U << 31)
    128      1.37     rmind #define	SKIPTO_MASK		(SKIPTO_ADJ_FLAG - 1)
    129      1.37     rmind 
    130      1.47     rmind static nvlist_t *	npf_rule_export(npf_t *, const npf_rule_t *);
    131       1.1     rmind 
    132      1.31     rmind /*
    133      1.31     rmind  * Private attributes - must be in the NPF_RULE_PRIVMASK range.
    134      1.31     rmind  */
    135      1.31     rmind #define	NPF_RULE_KEEPNAT	(0x01000000 & NPF_RULE_PRIVMASK)
    136      1.31     rmind 
    137      1.17     rmind #define	NPF_DYNAMIC_GROUP_P(attr) \
    138      1.17     rmind     (((attr) & NPF_DYNAMIC_GROUP) == NPF_DYNAMIC_GROUP)
    139      1.17     rmind 
    140      1.19     rmind #define	NPF_DYNAMIC_RULE_P(attr) \
    141      1.19     rmind     (((attr) & NPF_DYNAMIC_GROUP) == NPF_RULE_DYNAMIC)
    142      1.19     rmind 
    143       1.1     rmind npf_ruleset_t *
    144      1.17     rmind npf_ruleset_create(size_t slots)
    145       1.1     rmind {
    146      1.17     rmind 	size_t len = offsetof(npf_ruleset_t, rs_rules[slots]);
    147       1.1     rmind 	npf_ruleset_t *rlset;
    148       1.1     rmind 
    149      1.17     rmind 	rlset = kmem_zalloc(len, KM_SLEEP);
    150      1.17     rmind 	LIST_INIT(&rlset->rs_dynamic);
    151      1.17     rmind 	LIST_INIT(&rlset->rs_all);
    152      1.19     rmind 	LIST_INIT(&rlset->rs_gc);
    153      1.19     rmind 	rlset->rs_slots = slots;
    154      1.19     rmind 
    155       1.1     rmind 	return rlset;
    156       1.1     rmind }
    157       1.1     rmind 
    158       1.1     rmind void
    159       1.1     rmind npf_ruleset_destroy(npf_ruleset_t *rlset)
    160       1.1     rmind {
    161      1.17     rmind 	size_t len = offsetof(npf_ruleset_t, rs_rules[rlset->rs_slots]);
    162       1.1     rmind 	npf_rule_t *rl;
    163       1.1     rmind 
    164      1.17     rmind 	while ((rl = LIST_FIRST(&rlset->rs_all)) != NULL) {
    165      1.42     rmind 		if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
    166      1.42     rmind 			/*
    167      1.42     rmind 			 * Note: r_subset may point to the rules which
    168      1.42     rmind 			 * were inherited by a new ruleset.
    169      1.42     rmind 			 */
    170      1.42     rmind 			rl->r_subset = NULL;
    171      1.42     rmind 			LIST_REMOVE(rl, r_dentry);
    172      1.42     rmind 		}
    173      1.42     rmind 		if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
    174      1.42     rmind 			/* Not removing from r_subset, see above. */
    175      1.42     rmind 			KASSERT(rl->r_parent != NULL);
    176      1.42     rmind 		}
    177      1.42     rmind 		LIST_REMOVE(rl, r_aentry);
    178       1.1     rmind 		npf_rule_free(rl);
    179       1.1     rmind 	}
    180      1.17     rmind 	KASSERT(LIST_EMPTY(&rlset->rs_dynamic));
    181      1.43  christos 
    182      1.43  christos 	npf_ruleset_gc(rlset);
    183      1.18     rmind 	KASSERT(LIST_EMPTY(&rlset->rs_gc));
    184      1.17     rmind 	kmem_free(rlset, len);
    185       1.1     rmind }
    186       1.1     rmind 
    187       1.1     rmind /*
    188       1.1     rmind  * npf_ruleset_insert: insert the rule into the specified ruleset.
    189       1.1     rmind  */
    190       1.1     rmind void
    191       1.1     rmind npf_ruleset_insert(npf_ruleset_t *rlset, npf_rule_t *rl)
    192       1.1     rmind {
    193      1.51     rmind 	unsigned n = rlset->rs_nitems;
    194      1.17     rmind 
    195      1.17     rmind 	KASSERT(n < rlset->rs_slots);
    196      1.17     rmind 
    197      1.17     rmind 	LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
    198      1.17     rmind 	if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
    199      1.17     rmind 		LIST_INSERT_HEAD(&rlset->rs_dynamic, rl, r_dentry);
    200      1.24     rmind 	} else {
    201      1.24     rmind 		KASSERTMSG(rl->r_parent == NULL, "cannot be dynamic rule");
    202      1.24     rmind 		rl->r_attr &= ~NPF_RULE_DYNAMIC;
    203      1.17     rmind 	}
    204      1.17     rmind 
    205      1.17     rmind 	rlset->rs_rules[n] = rl;
    206      1.17     rmind 	rlset->rs_nitems++;
    207      1.45  christos 	rl->r_id = ++rlset->rs_idcnt;
    208      1.17     rmind 
    209      1.17     rmind 	if (rl->r_skip_to < ++n) {
    210      1.37     rmind 		rl->r_skip_to = SKIPTO_ADJ_FLAG | n;
    211      1.17     rmind 	}
    212      1.17     rmind }
    213      1.17     rmind 
    214      1.46     rmind npf_rule_t *
    215      1.17     rmind npf_ruleset_lookup(npf_ruleset_t *rlset, const char *name)
    216      1.17     rmind {
    217      1.17     rmind 	npf_rule_t *rl;
    218      1.17     rmind 
    219      1.17     rmind 	LIST_FOREACH(rl, &rlset->rs_dynamic, r_dentry) {
    220      1.17     rmind 		KASSERT(NPF_DYNAMIC_GROUP_P(rl->r_attr));
    221      1.17     rmind 		if (strncmp(rl->r_name, name, NPF_RULE_MAXNAMELEN) == 0)
    222      1.17     rmind 			break;
    223      1.17     rmind 	}
    224      1.17     rmind 	return rl;
    225      1.17     rmind }
    226      1.17     rmind 
    227      1.39     rmind /*
    228      1.39     rmind  * npf_ruleset_add: insert dynamic rule into the (active) ruleset.
    229      1.39     rmind  */
    230      1.17     rmind int
    231      1.17     rmind npf_ruleset_add(npf_ruleset_t *rlset, const char *rname, npf_rule_t *rl)
    232      1.17     rmind {
    233      1.42     rmind 	npf_rule_t *rg, *it, *target;
    234      1.42     rmind 	int priocmd;
    235      1.17     rmind 
    236      1.42     rmind 	if (!NPF_DYNAMIC_RULE_P(rl->r_attr)) {
    237      1.42     rmind 		return EINVAL;
    238      1.42     rmind 	}
    239      1.17     rmind 	rg = npf_ruleset_lookup(rlset, rname);
    240      1.17     rmind 	if (rg == NULL) {
    241      1.19     rmind 		return ESRCH;
    242      1.19     rmind 	}
    243      1.17     rmind 
    244      1.19     rmind 	/* Dynamic rule - assign a unique ID and save the parent. */
    245      1.19     rmind 	rl->r_id = ++rlset->rs_idcnt;
    246      1.17     rmind 	rl->r_parent = rg;
    247      1.17     rmind 
    248      1.17     rmind 	/*
    249      1.17     rmind 	 * Rule priority: (highest) 1, 2 ... n (lowest).
    250      1.17     rmind 	 * Negative priority indicates an operation and is reset to zero.
    251      1.17     rmind 	 */
    252      1.17     rmind 	if ((priocmd = rl->r_priority) < 0) {
    253      1.17     rmind 		rl->r_priority = 0;
    254      1.17     rmind 	}
    255      1.17     rmind 
    256      1.42     rmind 	/*
    257      1.42     rmind 	 * WARNING: once rg->subset or target->r_next of an *active*
    258      1.42     rmind 	 * rule is set, then our rule becomes globally visible and active.
    259      1.42     rmind 	 * Must issue a load fence to ensure rl->r_next visibility first.
    260      1.42     rmind 	 */
    261      1.17     rmind 	switch (priocmd) {
    262      1.17     rmind 	case NPF_PRI_LAST:
    263      1.17     rmind 	default:
    264      1.42     rmind 		target = NULL;
    265      1.42     rmind 		it = rg->r_subset;
    266      1.42     rmind 		while (it && it->r_priority <= rl->r_priority) {
    267      1.42     rmind 			target = it;
    268      1.42     rmind 			it = it->r_next;
    269      1.42     rmind 		}
    270      1.42     rmind 		if (target) {
    271      1.51     rmind 			atomic_store_relaxed(&rl->r_next, target->r_next);
    272      1.42     rmind 			membar_producer();
    273      1.51     rmind 			atomic_store_relaxed(&target->r_next, rl);
    274      1.42     rmind 			break;
    275      1.17     rmind 		}
    276      1.42     rmind 		/* FALLTHROUGH */
    277      1.42     rmind 
    278      1.42     rmind 	case NPF_PRI_FIRST:
    279      1.51     rmind 		atomic_store_relaxed(&rl->r_next, rg->r_subset);
    280      1.42     rmind 		membar_producer();
    281      1.51     rmind 		atomic_store_relaxed(&rg->r_subset, rl);
    282      1.17     rmind 		break;
    283      1.17     rmind 	}
    284      1.17     rmind 
    285      1.17     rmind 	/* Finally, add into the all-list. */
    286      1.17     rmind 	LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
    287      1.17     rmind 	return 0;
    288      1.17     rmind }
    289      1.17     rmind 
    290      1.42     rmind static void
    291      1.42     rmind npf_ruleset_unlink(npf_rule_t *rl, npf_rule_t *prev)
    292      1.42     rmind {
    293      1.42     rmind 	KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    294      1.42     rmind 	if (prev) {
    295      1.42     rmind 		prev->r_next = rl->r_next;
    296      1.42     rmind 	} else {
    297      1.42     rmind 		npf_rule_t *rg = rl->r_parent;
    298      1.42     rmind 		rg->r_subset = rl->r_next;
    299      1.42     rmind 	}
    300      1.42     rmind 	LIST_REMOVE(rl, r_aentry);
    301      1.42     rmind }
    302      1.42     rmind 
    303      1.39     rmind /*
    304      1.39     rmind  * npf_ruleset_remove: remove the dynamic rule given the rule ID.
    305      1.39     rmind  */
    306      1.18     rmind int
    307      1.19     rmind npf_ruleset_remove(npf_ruleset_t *rlset, const char *rname, uint64_t id)
    308      1.17     rmind {
    309      1.42     rmind 	npf_rule_t *rg, *prev = NULL;
    310      1.17     rmind 
    311      1.17     rmind 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    312      1.19     rmind 		return ESRCH;
    313      1.17     rmind 	}
    314      1.42     rmind 	for (npf_rule_t *rl = rg->r_subset; rl; rl = rl->r_next) {
    315      1.24     rmind 		KASSERT(rl->r_parent == rg);
    316      1.42     rmind 		KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    317      1.24     rmind 
    318      1.17     rmind 		/* Compare ID.  On match, remove and return. */
    319      1.19     rmind 		if (rl->r_id == id) {
    320      1.42     rmind 			npf_ruleset_unlink(rl, prev);
    321      1.18     rmind 			LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
    322      1.19     rmind 			return 0;
    323      1.17     rmind 		}
    324      1.42     rmind 		prev = rl;
    325      1.17     rmind 	}
    326      1.19     rmind 	return ENOENT;
    327      1.17     rmind }
    328      1.17     rmind 
    329      1.39     rmind /*
    330      1.39     rmind  * npf_ruleset_remkey: remove the dynamic rule given the rule key.
    331      1.39     rmind  */
    332      1.18     rmind int
    333      1.17     rmind npf_ruleset_remkey(npf_ruleset_t *rlset, const char *rname,
    334      1.17     rmind     const void *key, size_t len)
    335      1.17     rmind {
    336      1.42     rmind 	npf_rule_t *rg, *rlast = NULL, *prev = NULL, *lastprev = NULL;
    337       1.1     rmind 
    338      1.17     rmind 	KASSERT(len && len <= NPF_RULE_MAXKEYLEN);
    339      1.17     rmind 
    340      1.17     rmind 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    341      1.19     rmind 		return ESRCH;
    342      1.17     rmind 	}
    343      1.18     rmind 
    344      1.42     rmind 	/* Compare the key and find the last in the list. */
    345      1.42     rmind 	for (npf_rule_t *rl = rg->r_subset; rl; rl = rl->r_next) {
    346      1.24     rmind 		KASSERT(rl->r_parent == rg);
    347      1.42     rmind 		KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    348      1.17     rmind 		if (memcmp(rl->r_key, key, len) == 0) {
    349      1.42     rmind 			lastprev = prev;
    350      1.42     rmind 			rlast = rl;
    351      1.17     rmind 		}
    352      1.42     rmind 		prev = rl;
    353      1.42     rmind 	}
    354      1.42     rmind 	if (!rlast) {
    355      1.42     rmind 		return ENOENT;
    356       1.1     rmind 	}
    357      1.42     rmind 	npf_ruleset_unlink(rlast, lastprev);
    358      1.42     rmind 	LIST_INSERT_HEAD(&rlset->rs_gc, rlast, r_aentry);
    359      1.42     rmind 	return 0;
    360      1.18     rmind }
    361      1.18     rmind 
    362      1.39     rmind /*
    363      1.39     rmind  * npf_ruleset_list: serialise and return the dynamic rules.
    364      1.39     rmind  */
    365      1.51     rmind int
    366      1.51     rmind npf_ruleset_list(npf_t *npf, npf_ruleset_t *rlset, const char *rname,
    367      1.51     rmind     nvlist_t *rlset_nvl)
    368      1.18     rmind {
    369      1.51     rmind 	const npf_rule_t *rg;
    370      1.18     rmind 
    371      1.43  christos 	KASSERT(npf_config_locked_p(npf));
    372      1.36     rmind 
    373      1.18     rmind 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    374      1.51     rmind 		return ESRCH;
    375      1.18     rmind 	}
    376      1.51     rmind 	for (const npf_rule_t *rl = rg->r_subset; rl; rl = rl->r_next) {
    377      1.47     rmind 		nvlist_t *rule;
    378      1.36     rmind 
    379      1.24     rmind 		KASSERT(rl->r_parent == rg);
    380      1.42     rmind 		KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    381      1.36     rmind 
    382      1.51     rmind 		if ((rule = npf_rule_export(npf, rl)) == NULL) {
    383      1.51     rmind 			return ENOMEM;
    384      1.18     rmind 		}
    385      1.51     rmind 		nvlist_append_nvlist_array(rlset_nvl, "rules", rule);
    386      1.47     rmind 		nvlist_destroy(rule);
    387      1.18     rmind 	}
    388      1.51     rmind 	return 0;
    389      1.18     rmind }
    390      1.18     rmind 
    391      1.39     rmind /*
    392      1.39     rmind  * npf_ruleset_flush: flush the dynamic rules in the ruleset by inserting
    393      1.39     rmind  * them into the G/C list.
    394      1.39     rmind  */
    395      1.18     rmind int
    396      1.18     rmind npf_ruleset_flush(npf_ruleset_t *rlset, const char *rname)
    397      1.18     rmind {
    398      1.18     rmind 	npf_rule_t *rg, *rl;
    399      1.18     rmind 
    400      1.18     rmind 	if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
    401      1.19     rmind 		return ESRCH;
    402      1.18     rmind 	}
    403      1.42     rmind 
    404      1.42     rmind 	rl = atomic_swap_ptr(&rg->r_subset, NULL);
    405      1.42     rmind 	membar_producer();
    406      1.42     rmind 
    407      1.42     rmind 	while (rl) {
    408      1.42     rmind 		KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    409      1.24     rmind 		KASSERT(rl->r_parent == rg);
    410      1.42     rmind 
    411      1.42     rmind 		LIST_REMOVE(rl, r_aentry);
    412      1.18     rmind 		LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
    413      1.42     rmind 		rl = rl->r_next;
    414      1.18     rmind 	}
    415      1.45  christos 	rlset->rs_idcnt = 0;
    416      1.18     rmind 	return 0;
    417      1.18     rmind }
    418      1.18     rmind 
    419      1.39     rmind /*
    420      1.39     rmind  * npf_ruleset_gc: destroy the rules in G/C list.
    421      1.39     rmind  */
    422      1.39     rmind void
    423      1.39     rmind npf_ruleset_gc(npf_ruleset_t *rlset)
    424      1.39     rmind {
    425      1.39     rmind 	npf_rule_t *rl;
    426      1.39     rmind 
    427      1.39     rmind 	while ((rl = LIST_FIRST(&rlset->rs_gc)) != NULL) {
    428      1.39     rmind 		LIST_REMOVE(rl, r_aentry);
    429      1.39     rmind 		npf_rule_free(rl);
    430      1.39     rmind 	}
    431      1.39     rmind }
    432      1.39     rmind 
    433      1.39     rmind /*
    434      1.39     rmind  * npf_ruleset_export: serialise and return the static rules.
    435      1.39     rmind  */
    436      1.36     rmind int
    437      1.47     rmind npf_ruleset_export(npf_t *npf, const npf_ruleset_t *rlset,
    438      1.51     rmind     const char *key, nvlist_t *npf_nv)
    439      1.36     rmind {
    440      1.47     rmind 	const unsigned nitems = rlset->rs_nitems;
    441      1.47     rmind 	unsigned n = 0;
    442      1.36     rmind 	int error = 0;
    443      1.36     rmind 
    444      1.43  christos 	KASSERT(npf_config_locked_p(npf));
    445      1.36     rmind 
    446      1.37     rmind 	while (n < nitems) {
    447      1.37     rmind 		const npf_rule_t *rl = rlset->rs_rules[n];
    448      1.36     rmind 		const npf_natpolicy_t *natp = rl->r_natp;
    449      1.47     rmind 		nvlist_t *rule;
    450      1.36     rmind 
    451      1.47     rmind 		rule = npf_rule_export(npf, rl);
    452      1.47     rmind 		if (!rule) {
    453      1.47     rmind 			error = ENOMEM;
    454      1.36     rmind 			break;
    455      1.36     rmind 		}
    456      1.51     rmind 		if (natp && (error = npf_natpolicy_export(natp, rule)) != 0) {
    457      1.47     rmind 			nvlist_destroy(rule);
    458      1.36     rmind 			break;
    459      1.36     rmind 		}
    460      1.51     rmind 		nvlist_append_nvlist_array(npf_nv, key, rule);
    461      1.47     rmind 		nvlist_destroy(rule);
    462      1.37     rmind 		n++;
    463      1.36     rmind 	}
    464      1.36     rmind 	return error;
    465      1.36     rmind }
    466      1.36     rmind 
    467      1.17     rmind /*
    468      1.31     rmind  * npf_ruleset_reload: prepare the new ruleset by scanning the active
    469      1.39     rmind  * ruleset and: 1) sharing the dynamic rules 2) sharing NAT policies.
    470      1.17     rmind  *
    471      1.31     rmind  * => The active (old) ruleset should be exclusively locked.
    472      1.17     rmind  */
    473      1.17     rmind void
    474      1.43  christos npf_ruleset_reload(npf_t *npf, npf_ruleset_t *newset,
    475      1.43  christos     npf_ruleset_t *oldset, bool load)
    476      1.17     rmind {
    477      1.31     rmind 	npf_rule_t *rg, *rl;
    478      1.35     rmind 	uint64_t nid = 0;
    479      1.17     rmind 
    480      1.43  christos 	KASSERT(npf_config_locked_p(npf));
    481      1.17     rmind 
    482      1.31     rmind 	/*
    483      1.31     rmind 	 * Scan the dynamic rules and share (migrate) if needed.
    484      1.31     rmind 	 */
    485      1.31     rmind 	LIST_FOREACH(rg, &newset->rs_dynamic, r_dentry) {
    486      1.42     rmind 		npf_rule_t *active_rgroup;
    487      1.18     rmind 
    488      1.31     rmind 		/* Look for a dynamic ruleset group with such name. */
    489      1.42     rmind 		active_rgroup = npf_ruleset_lookup(oldset, rg->r_name);
    490      1.42     rmind 		if (active_rgroup == NULL) {
    491      1.17     rmind 			continue;
    492      1.17     rmind 		}
    493      1.18     rmind 
    494      1.18     rmind 		/*
    495      1.42     rmind 		 * ATOMICITY: Copy the head pointer of the linked-list,
    496      1.42     rmind 		 * but do not remove the rules from the active r_subset.
    497      1.42     rmind 		 * This is necessary because the rules are still active
    498      1.42     rmind 		 * and therefore are accessible for inspection via the
    499      1.42     rmind 		 * old ruleset.
    500      1.18     rmind 		 */
    501      1.42     rmind 		rg->r_subset = active_rgroup->r_subset;
    502      1.42     rmind 
    503      1.42     rmind 		/*
    504      1.42     rmind 		 * We can safely migrate to the new all-rule list and
    505      1.42     rmind 		 * reset the parent rule, though.
    506      1.42     rmind 		 */
    507      1.42     rmind 		for (rl = rg->r_subset; rl; rl = rl->r_next) {
    508      1.42     rmind 			KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    509      1.18     rmind 			LIST_REMOVE(rl, r_aentry);
    510      1.31     rmind 			LIST_INSERT_HEAD(&newset->rs_all, rl, r_aentry);
    511      1.42     rmind 
    512      1.42     rmind 			KASSERT(rl->r_parent == active_rgroup);
    513      1.19     rmind 			rl->r_parent = rg;
    514      1.18     rmind 		}
    515       1.1     rmind 	}
    516      1.19     rmind 
    517      1.31     rmind 	/*
    518      1.51     rmind 	 * If performing the load of connections then NAT policies might
    519      1.40     rmind 	 * already have translated connections associated with them and
    520      1.40     rmind 	 * we should not share or inherit anything.
    521      1.40     rmind 	 */
    522      1.40     rmind 	if (load)
    523      1.40     rmind 		return;
    524      1.40     rmind 
    525      1.40     rmind 	/*
    526      1.48     rmind 	 * Scan all rules in the new ruleset and inherit the active NAT
    527      1.48     rmind 	 * policies if they are the same.  Also, assign a unique ID for
    528      1.48     rmind 	 * each policy here.
    529      1.31     rmind 	 */
    530      1.31     rmind 	LIST_FOREACH(rl, &newset->rs_all, r_aentry) {
    531      1.31     rmind 		npf_natpolicy_t *np;
    532      1.31     rmind 		npf_rule_t *actrl;
    533      1.31     rmind 
    534      1.31     rmind 		/* Does the rule have a NAT policy associated? */
    535      1.31     rmind 		if ((np = rl->r_natp) == NULL) {
    536      1.31     rmind 			continue;
    537      1.31     rmind 		}
    538      1.35     rmind 
    539      1.31     rmind 		/* Does it match with any policy in the active ruleset? */
    540      1.38     rmind 		LIST_FOREACH(actrl, &oldset->rs_all, r_aentry) {
    541      1.38     rmind 			if (!actrl->r_natp)
    542      1.38     rmind 				continue;
    543      1.38     rmind 			if ((actrl->r_attr & NPF_RULE_KEEPNAT) != 0)
    544      1.38     rmind 				continue;
    545      1.51     rmind 			if (npf_natpolicy_cmp(actrl->r_natp, np))
    546      1.38     rmind 				break;
    547      1.38     rmind 		}
    548      1.38     rmind 		if (!actrl) {
    549      1.38     rmind 			/* No: just set the ID and continue. */
    550      1.35     rmind 			npf_nat_setid(np, ++nid);
    551      1.31     rmind 			continue;
    552      1.31     rmind 		}
    553      1.31     rmind 
    554      1.38     rmind 		/* Yes: inherit the matching NAT policy. */
    555      1.31     rmind 		rl->r_natp = actrl->r_natp;
    556      1.35     rmind 		npf_nat_setid(rl->r_natp, ++nid);
    557      1.31     rmind 
    558      1.31     rmind 		/*
    559      1.31     rmind 		 * Finally, mark the active rule to not destroy its NAT
    560      1.31     rmind 		 * policy later as we inherited it (but the rule must be
    561      1.31     rmind 		 * kept active for now).  Destroy the new/unused policy.
    562      1.31     rmind 		 */
    563      1.31     rmind 		actrl->r_attr |= NPF_RULE_KEEPNAT;
    564      1.51     rmind 		npf_natpolicy_destroy(np);
    565      1.31     rmind 	}
    566      1.31     rmind 
    567      1.19     rmind 	/* Inherit the ID counter. */
    568      1.31     rmind 	newset->rs_idcnt = oldset->rs_idcnt;
    569       1.1     rmind }
    570       1.1     rmind 
    571      1.39     rmind /*
    572      1.48     rmind  * npf_ruleset_findnat: find a NAT policy in the ruleset by a given ID.
    573      1.39     rmind  */
    574      1.35     rmind npf_natpolicy_t *
    575      1.35     rmind npf_ruleset_findnat(npf_ruleset_t *rlset, uint64_t id)
    576      1.35     rmind {
    577      1.35     rmind 	npf_rule_t *rl;
    578      1.35     rmind 
    579      1.35     rmind 	LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
    580      1.35     rmind 		npf_natpolicy_t *np = rl->r_natp;
    581      1.35     rmind 		if (np && npf_nat_getid(np) == id) {
    582      1.35     rmind 			return np;
    583      1.35     rmind 		}
    584      1.35     rmind 	}
    585      1.35     rmind 	return NULL;
    586      1.35     rmind }
    587      1.35     rmind 
    588       1.1     rmind /*
    589      1.13     rmind  * npf_ruleset_freealg: inspect the ruleset and disassociate specified
    590      1.13     rmind  * ALG from all NAT entries using it.
    591      1.13     rmind  */
    592      1.13     rmind void
    593      1.13     rmind npf_ruleset_freealg(npf_ruleset_t *rlset, npf_alg_t *alg)
    594      1.13     rmind {
    595      1.13     rmind 	npf_rule_t *rl;
    596      1.17     rmind 	npf_natpolicy_t *np;
    597      1.13     rmind 
    598      1.17     rmind 	LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
    599      1.17     rmind 		if ((np = rl->r_natp) != NULL) {
    600      1.13     rmind 			npf_nat_freealg(np, alg);
    601      1.13     rmind 		}
    602      1.13     rmind 	}
    603      1.13     rmind }
    604      1.13     rmind 
    605      1.13     rmind /*
    606      1.25     rmind  * npf_rule_alloc: allocate a rule and initialise it.
    607       1.1     rmind  */
    608       1.4     rmind npf_rule_t *
    609      1.47     rmind npf_rule_alloc(npf_t *npf, const nvlist_t *rule)
    610       1.1     rmind {
    611       1.4     rmind 	npf_rule_t *rl;
    612       1.7     rmind 	const char *rname;
    613      1.47     rmind 	const void *key, *info;
    614      1.47     rmind 	size_t len;
    615       1.1     rmind 
    616      1.47     rmind 	/* Allocate a rule structure and keep the information. */
    617      1.11     rmind 	rl = kmem_zalloc(sizeof(npf_rule_t), KM_SLEEP);
    618      1.47     rmind 	info = dnvlist_get_binary(rule, "info", &rl->r_info_len, NULL, 0);
    619      1.47     rmind 	if (info) {
    620      1.47     rmind 		rl->r_info = kmem_alloc(rl->r_info_len, KM_SLEEP);
    621      1.47     rmind 		memcpy(rl->r_info, info, rl->r_info_len);
    622      1.47     rmind 	}
    623       1.4     rmind 	rl->r_natp = NULL;
    624       1.4     rmind 
    625      1.11     rmind 	/* Name (optional) */
    626      1.47     rmind 	if ((rname = dnvlist_get_string(rule, "name", NULL)) != NULL) {
    627      1.17     rmind 		strlcpy(rl->r_name, rname, NPF_RULE_MAXNAMELEN);
    628       1.7     rmind 	} else {
    629       1.7     rmind 		rl->r_name[0] = '\0';
    630       1.7     rmind 	}
    631       1.7     rmind 
    632      1.11     rmind 	/* Attributes, priority and interface ID (optional). */
    633      1.47     rmind 	rl->r_attr = dnvlist_get_number(rule, "attr", 0);
    634      1.31     rmind 	rl->r_attr &= ~NPF_RULE_PRIVMASK;
    635      1.26     rmind 
    636      1.42     rmind 	if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
    637      1.42     rmind 		/* Priority of the dynamic rule. */
    638      1.48     rmind 		rl->r_priority = (int)dnvlist_get_number(rule, "prio", 0);
    639      1.42     rmind 	} else {
    640      1.42     rmind 		/* The skip-to index.  No need to validate it. */
    641      1.47     rmind 		rl->r_skip_to = dnvlist_get_number(rule, "skip-to", 0);
    642      1.42     rmind 	}
    643      1.42     rmind 
    644      1.42     rmind 	/* Interface name; register and get the npf-if-id. */
    645      1.47     rmind 	if ((rname = dnvlist_get_string(rule, "ifname", NULL)) != NULL) {
    646      1.43  christos 		if ((rl->r_ifid = npf_ifmap_register(npf, rname)) == 0) {
    647      1.26     rmind 			kmem_free(rl, sizeof(npf_rule_t));
    648      1.26     rmind 			return NULL;
    649      1.26     rmind 		}
    650      1.26     rmind 	} else {
    651      1.26     rmind 		rl->r_ifid = 0;
    652      1.26     rmind 	}
    653       1.4     rmind 
    654      1.17     rmind 	/* Key (optional). */
    655      1.47     rmind 	if ((key = dnvlist_get_binary(rule, "key", &len, NULL, 0)) != NULL) {
    656      1.17     rmind 		if (len > NPF_RULE_MAXKEYLEN) {
    657      1.17     rmind 			kmem_free(rl, sizeof(npf_rule_t));
    658      1.17     rmind 			return NULL;
    659      1.17     rmind 		}
    660      1.17     rmind 		memcpy(rl->r_key, key, len);
    661       1.4     rmind 	}
    662  1.52.6.1  perseant 
    663  1.52.6.1  perseant 	/* no gid/uid set yet */
    664  1.52.6.1  perseant 	rl->gid.op = rl->uid.op = NPF_OP_NONE;
    665      1.36     rmind 	return rl;
    666      1.36     rmind }
    667      1.36     rmind 
    668  1.52.6.1  perseant static void
    669  1.52.6.1  perseant npf_rid_export(nvlist_t *rl, struct r_id rid, const char *name)
    670  1.52.6.1  perseant {
    671  1.52.6.1  perseant 	uint64_t uid_element[3] = { rid.id[0], rid.id[1], rid.op };
    672  1.52.6.1  perseant 	nvlist_add_number_array(rl, name, uid_element, 3);
    673  1.52.6.1  perseant }
    674  1.52.6.1  perseant 
    675      1.47     rmind static nvlist_t *
    676      1.47     rmind npf_rule_export(npf_t *npf, const npf_rule_t *rl)
    677      1.36     rmind {
    678      1.47     rmind 	nvlist_t *rule = nvlist_create(0);
    679      1.47     rmind 	unsigned skip_to = 0;
    680      1.47     rmind 	npf_rproc_t *rp;
    681      1.36     rmind 
    682      1.47     rmind 	nvlist_add_number(rule, "attr", rl->r_attr);
    683      1.47     rmind 	nvlist_add_number(rule, "prio", rl->r_priority);
    684      1.37     rmind 	if ((rl->r_skip_to & SKIPTO_ADJ_FLAG) == 0) {
    685      1.37     rmind 		skip_to = rl->r_skip_to & SKIPTO_MASK;
    686      1.37     rmind 	}
    687      1.47     rmind 	nvlist_add_number(rule, "skip-to", skip_to);
    688      1.47     rmind 	nvlist_add_number(rule, "code-type", rl->r_type);
    689      1.36     rmind 	if (rl->r_code) {
    690      1.47     rmind 		nvlist_add_binary(rule, "code", rl->r_code, rl->r_clen);
    691      1.36     rmind 	}
    692      1.36     rmind 	if (rl->r_ifid) {
    693      1.49     rmind 		char ifname[IFNAMSIZ];
    694      1.49     rmind 		npf_ifmap_copyname(npf, rl->r_ifid, ifname, sizeof(ifname));
    695      1.47     rmind 		nvlist_add_string(rule, "ifname", ifname);
    696      1.36     rmind 	}
    697      1.47     rmind 	nvlist_add_number(rule, "id", rl->r_id);
    698      1.36     rmind 
    699      1.36     rmind 	if (rl->r_name[0]) {
    700      1.47     rmind 		nvlist_add_string(rule, "name", rl->r_name);
    701      1.36     rmind 	}
    702      1.19     rmind 	if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
    703      1.47     rmind 		nvlist_add_binary(rule, "key", rl->r_key, NPF_RULE_MAXKEYLEN);
    704      1.18     rmind 	}
    705      1.37     rmind 	if (rl->r_info) {
    706      1.47     rmind 		nvlist_add_binary(rule, "info", rl->r_info, rl->r_info_len);
    707      1.37     rmind 	}
    708  1.52.6.1  perseant 	if (rl->uid.op != NPF_OP_NONE) {
    709  1.52.6.1  perseant 		npf_rid_export(rule, rl->uid, "r_user");
    710  1.52.6.1  perseant 	}
    711  1.52.6.1  perseant 	if (rl->gid.op != NPF_OP_NONE) {
    712  1.52.6.1  perseant 		npf_rid_export(rule, rl->gid, "r_group");
    713  1.52.6.1  perseant 	}
    714      1.47     rmind 	if ((rp = npf_rule_getrproc(rl)) != NULL) {
    715      1.47     rmind 		const char *rname = npf_rproc_getname(rp);
    716      1.47     rmind 		nvlist_add_string(rule, "rproc", rname);
    717      1.44  christos 		npf_rproc_release(rp);
    718      1.44  christos 	}
    719      1.47     rmind 	return rule;
    720      1.17     rmind }
    721      1.17     rmind 
    722      1.17     rmind /*
    723      1.17     rmind  * npf_rule_setcode: assign filter code to the rule.
    724      1.17     rmind  *
    725      1.20     rmind  * => The code must be validated by the caller.
    726      1.20     rmind  * => JIT compilation may be performed here.
    727      1.17     rmind  */
    728      1.17     rmind void
    729      1.17     rmind npf_rule_setcode(npf_rule_t *rl, const int type, void *code, size_t size)
    730      1.17     rmind {
    731      1.25     rmind 	KASSERT(type == NPF_CODE_BPF);
    732      1.28     rmind 
    733      1.28     rmind 	rl->r_type = type;
    734      1.36     rmind 	rl->r_code = code;
    735      1.36     rmind 	rl->r_clen = size;
    736      1.36     rmind 	rl->r_jcode = npf_bpf_compile(code, size);
    737      1.17     rmind }
    738      1.17     rmind 
    739  1.52.6.1  perseant void
    740  1.52.6.1  perseant npf_rule_setrid(const nvlist_t *req, npf_rule_t *rl, const char *name)
    741  1.52.6.1  perseant {
    742  1.52.6.1  perseant 	size_t nitems;
    743  1.52.6.1  perseant 	rid_t id;
    744  1.52.6.1  perseant 	const uint64_t *rid = nvlist_get_number_array(req, name, &nitems);
    745  1.52.6.1  perseant 	KASSERT(nitems == 3);
    746  1.52.6.1  perseant 
    747  1.52.6.1  perseant 	id.id[0] = (uint32_t)rid[0];
    748  1.52.6.1  perseant 	id.id[1] = (uint32_t)rid[1];
    749  1.52.6.1  perseant 	id.op = (uint8_t)rid[2];
    750  1.52.6.1  perseant 
    751  1.52.6.1  perseant 	if (!strcmp(name, "r_user"))
    752  1.52.6.1  perseant 		rl->uid = id;
    753  1.52.6.1  perseant 	else if (!strcmp(name, "r_group"))
    754  1.52.6.1  perseant 		rl->gid = id;
    755  1.52.6.1  perseant }
    756  1.52.6.1  perseant 
    757      1.17     rmind /*
    758      1.17     rmind  * npf_rule_setrproc: assign a rule procedure and hold a reference on it.
    759      1.17     rmind  */
    760      1.17     rmind void
    761      1.17     rmind npf_rule_setrproc(npf_rule_t *rl, npf_rproc_t *rp)
    762      1.17     rmind {
    763      1.17     rmind 	npf_rproc_acquire(rp);
    764       1.6     rmind 	rl->r_rproc = rp;
    765       1.1     rmind }
    766       1.1     rmind 
    767       1.1     rmind /*
    768       1.1     rmind  * npf_rule_free: free the specified rule.
    769       1.1     rmind  */
    770       1.1     rmind void
    771       1.1     rmind npf_rule_free(npf_rule_t *rl)
    772       1.1     rmind {
    773       1.4     rmind 	npf_natpolicy_t *np = rl->r_natp;
    774       1.4     rmind 	npf_rproc_t *rp = rl->r_rproc;
    775       1.1     rmind 
    776      1.31     rmind 	if (np && (rl->r_attr & NPF_RULE_KEEPNAT) == 0) {
    777      1.51     rmind 		/* Destroy the NAT policy. */
    778      1.51     rmind 		npf_natpolicy_destroy(np);
    779       1.4     rmind 	}
    780       1.4     rmind 	if (rp) {
    781       1.6     rmind 		/* Release rule procedure. */
    782       1.4     rmind 		npf_rproc_release(rp);
    783       1.4     rmind 	}
    784      1.17     rmind 	if (rl->r_code) {
    785      1.20     rmind 		/* Free byte-code. */
    786      1.17     rmind 		kmem_free(rl->r_code, rl->r_clen);
    787       1.1     rmind 	}
    788      1.20     rmind 	if (rl->r_jcode) {
    789      1.20     rmind 		/* Free JIT code. */
    790      1.28     rmind 		bpf_jit_freecode(rl->r_jcode);
    791      1.20     rmind 	}
    792      1.36     rmind 	if (rl->r_info) {
    793      1.47     rmind 		kmem_free(rl->r_info, rl->r_info_len);
    794      1.18     rmind 	}
    795       1.4     rmind 	kmem_free(rl, sizeof(npf_rule_t));
    796       1.1     rmind }
    797       1.1     rmind 
    798       1.1     rmind /*
    799      1.19     rmind  * npf_rule_getid: return the unique ID of a rule.
    800      1.10     rmind  * npf_rule_getrproc: acquire a reference and return rule procedure, if any.
    801       1.1     rmind  * npf_rule_getnat: get NAT policy assigned to the rule.
    802       1.1     rmind  */
    803       1.1     rmind 
    804      1.19     rmind uint64_t
    805      1.19     rmind npf_rule_getid(const npf_rule_t *rl)
    806      1.19     rmind {
    807      1.19     rmind 	KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
    808      1.19     rmind 	return rl->r_id;
    809      1.19     rmind }
    810      1.19     rmind 
    811      1.10     rmind npf_rproc_t *
    812      1.30     rmind npf_rule_getrproc(const npf_rule_t *rl)
    813      1.10     rmind {
    814      1.10     rmind 	npf_rproc_t *rp = rl->r_rproc;
    815      1.10     rmind 
    816      1.10     rmind 	if (rp) {
    817      1.10     rmind 		npf_rproc_acquire(rp);
    818      1.10     rmind 	}
    819      1.10     rmind 	return rp;
    820      1.10     rmind }
    821      1.10     rmind 
    822       1.1     rmind npf_natpolicy_t *
    823       1.1     rmind npf_rule_getnat(const npf_rule_t *rl)
    824       1.1     rmind {
    825       1.4     rmind 	return rl->r_natp;
    826       1.1     rmind }
    827       1.1     rmind 
    828       1.4     rmind /*
    829       1.4     rmind  * npf_rule_setnat: assign NAT policy to the rule and insert into the
    830       1.4     rmind  * NAT policy list in the ruleset.
    831       1.4     rmind  */
    832       1.1     rmind void
    833       1.1     rmind npf_rule_setnat(npf_rule_t *rl, npf_natpolicy_t *np)
    834       1.1     rmind {
    835       1.4     rmind 	KASSERT(rl->r_natp == NULL);
    836       1.4     rmind 	rl->r_natp = np;
    837       1.1     rmind }
    838       1.1     rmind 
    839      1.17     rmind /*
    840      1.17     rmind  * npf_rule_inspect: match the interface, direction and run the filter code.
    841      1.29     rmind  * Returns true if rule matches and false otherwise.
    842      1.17     rmind  */
    843      1.17     rmind static inline bool
    844      1.29     rmind npf_rule_inspect(const npf_rule_t *rl, bpf_args_t *bc_args,
    845      1.51     rmind     const int di_mask, const unsigned ifid)
    846      1.17     rmind {
    847      1.17     rmind 	/* Match the interface. */
    848      1.29     rmind 	if (rl->r_ifid && rl->r_ifid != ifid) {
    849      1.17     rmind 		return false;
    850      1.17     rmind 	}
    851      1.17     rmind 
    852      1.17     rmind 	/* Match the direction. */
    853      1.17     rmind 	if ((rl->r_attr & NPF_RULE_DIMASK) != NPF_RULE_DIMASK) {
    854      1.17     rmind 		if ((rl->r_attr & di_mask) == 0)
    855      1.17     rmind 			return false;
    856      1.17     rmind 	}
    857      1.17     rmind 
    858      1.24     rmind 	/* Any code? */
    859      1.36     rmind 	if (!rl->r_code) {
    860      1.24     rmind 		KASSERT(rl->r_jcode == NULL);
    861      1.17     rmind 		return true;
    862      1.17     rmind 	}
    863      1.25     rmind 	KASSERT(rl->r_type == NPF_CODE_BPF);
    864      1.29     rmind 	return npf_bpf_filter(bc_args, rl->r_code, rl->r_jcode) != 0;
    865      1.17     rmind }
    866      1.17     rmind 
    867      1.17     rmind /*
    868      1.17     rmind  * npf_rule_reinspect: re-inspect the dynamic rule by iterating its list.
    869      1.17     rmind  * This is only for the dynamic rules.  Subrules cannot have nested rules.
    870      1.17     rmind  */
    871      1.42     rmind static inline npf_rule_t *
    872      1.42     rmind npf_rule_reinspect(const npf_rule_t *rg, bpf_args_t *bc_args,
    873      1.51     rmind     const int di_mask, const unsigned ifid)
    874       1.7     rmind {
    875      1.17     rmind 	npf_rule_t *final_rl = NULL, *rl;
    876      1.17     rmind 
    877      1.42     rmind 	KASSERT(NPF_DYNAMIC_GROUP_P(rg->r_attr));
    878       1.7     rmind 
    879      1.51     rmind 	rl = atomic_load_relaxed(&rg->r_subset);
    880      1.51     rmind 	for (; rl; rl = atomic_load_relaxed(&rl->r_next)) {
    881      1.42     rmind 		KASSERT(!final_rl || rl->r_priority >= final_rl->r_priority);
    882      1.29     rmind 		if (!npf_rule_inspect(rl, bc_args, di_mask, ifid)) {
    883       1.7     rmind 			continue;
    884      1.17     rmind 		}
    885      1.17     rmind 		if (rl->r_attr & NPF_RULE_FINAL) {
    886      1.17     rmind 			return rl;
    887      1.17     rmind 		}
    888      1.17     rmind 		final_rl = rl;
    889       1.7     rmind 	}
    890      1.17     rmind 	return final_rl;
    891       1.7     rmind }
    892       1.1     rmind 
    893       1.1     rmind /*
    894       1.7     rmind  * npf_ruleset_inspect: inspect the packet against the given ruleset.
    895       1.1     rmind  *
    896      1.25     rmind  * Loop through the rules in the set and run the byte-code of each rule
    897       1.7     rmind  * against the packet (nbuf chain).  If sub-ruleset is found, inspect it.
    898       1.1     rmind  */
    899       1.1     rmind npf_rule_t *
    900      1.34     rmind npf_ruleset_inspect(npf_cache_t *npc, const npf_ruleset_t *rlset,
    901      1.34     rmind     const int di, const int layer)
    902       1.1     rmind {
    903      1.34     rmind 	nbuf_t *nbuf = npc->npc_nbuf;
    904       1.7     rmind 	const int di_mask = (di & PFIL_IN) ? NPF_RULE_IN : NPF_RULE_OUT;
    905      1.51     rmind 	const unsigned nitems = rlset->rs_nitems;
    906      1.51     rmind 	const unsigned ifid = nbuf->nb_ifid;
    907      1.17     rmind 	npf_rule_t *final_rl = NULL;
    908      1.29     rmind 	bpf_args_t bc_args;
    909      1.51     rmind 	unsigned n = 0;
    910       1.1     rmind 
    911      1.33     rmind 	KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
    912      1.29     rmind 
    913      1.33     rmind 	/*
    914      1.33     rmind 	 * Prepare the external memory store and the arguments for
    915      1.43  christos 	 * the BPF programs to be executed.  Reset mbuf before taking
    916      1.43  christos 	 * any pointers for the BPF.
    917      1.33     rmind 	 */
    918      1.33     rmind 	uint32_t bc_words[NPF_BPF_NWORDS];
    919      1.43  christos 
    920      1.43  christos 	nbuf_reset(nbuf);
    921      1.34     rmind 	npf_bpf_prepare(npc, &bc_args, bc_words);
    922      1.17     rmind 
    923      1.17     rmind 	while (n < nitems) {
    924      1.17     rmind 		npf_rule_t *rl = rlset->rs_rules[n];
    925      1.51     rmind 		const unsigned skip_to = rl->r_skip_to & SKIPTO_MASK;
    926      1.17     rmind 		const uint32_t attr = rl->r_attr;
    927      1.17     rmind 
    928  1.52.6.1  perseant 		if ((attr & layer) == 0) {
    929  1.52.6.1  perseant 			n = skip_to;
    930  1.52.6.1  perseant 			continue;
    931  1.52.6.1  perseant 		}
    932  1.52.6.1  perseant 
    933      1.16     rmind 		KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    934      1.17     rmind 		KASSERT(n < skip_to);
    935       1.1     rmind 
    936      1.17     rmind 		/* Group is a barrier: return a matching if found any. */
    937      1.52    kardel 		if ((attr & NPF_DYNAMIC_GROUP) == NPF_RULE_GROUP && final_rl) {
    938      1.17     rmind 			break;
    939      1.17     rmind 		}
    940      1.17     rmind 
    941      1.17     rmind 		/* Main inspection of the rule. */
    942      1.29     rmind 		if (!npf_rule_inspect(rl, &bc_args, di_mask, ifid)) {
    943      1.17     rmind 			n = skip_to;
    944       1.1     rmind 			continue;
    945       1.1     rmind 		}
    946      1.17     rmind 
    947      1.17     rmind 		if (NPF_DYNAMIC_GROUP_P(attr)) {
    948      1.17     rmind 			/*
    949      1.17     rmind 			 * If this is a dynamic rule, re-inspect the subrules.
    950      1.17     rmind 			 * If it has any matching rule, then it is final.
    951      1.17     rmind 			 */
    952      1.29     rmind 			rl = npf_rule_reinspect(rl, &bc_args, di_mask, ifid);
    953      1.17     rmind 			if (rl != NULL) {
    954      1.17     rmind 				final_rl = rl;
    955      1.17     rmind 				break;
    956      1.17     rmind 			}
    957      1.17     rmind 		} else if ((attr & NPF_RULE_GROUP) == 0) {
    958      1.17     rmind 			/*
    959      1.17     rmind 			 * Groups themselves are not matching.
    960      1.17     rmind 			 */
    961      1.17     rmind 			final_rl = rl;
    962       1.1     rmind 		}
    963      1.17     rmind 
    964       1.1     rmind 		/* Set the matching rule and check for "final". */
    965      1.17     rmind 		if (attr & NPF_RULE_FINAL) {
    966       1.2     rmind 			break;
    967       1.1     rmind 		}
    968      1.17     rmind 		n++;
    969       1.2     rmind 	}
    970      1.16     rmind 
    971      1.16     rmind 	KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    972       1.7     rmind 	return final_rl;
    973       1.1     rmind }
    974       1.1     rmind 
    975       1.1     rmind /*
    976  1.52.6.1  perseant  * just exchange the flag attributes for pass/block for the diff protocols.
    977  1.52.6.1  perseant  * for passing, we set the STATEFULNESS for TCP connection establishment
    978  1.52.6.1  perseant  * if ret == 0, it is for a pass to be changed to block
    979  1.52.6.1  perseant  * non-zero ret indicates a block to pass
    980  1.52.6.1  perseant  * when we change to block, we assume the default RST rerturn for TCP
    981  1.52.6.1  perseant  * when we change to pass, we ensure no bit field for RST for tcp and ICMP for udp
    982  1.52.6.1  perseant  * finally change the ret condition too
    983  1.52.6.1  perseant  */
    984  1.52.6.1  perseant int
    985  1.52.6.1  perseant npf_rule_reverse(npf_cache_t *npc, npf_match_info_t *mi, int ret)
    986  1.52.6.1  perseant {
    987  1.52.6.1  perseant 	KASSERT(npf_iscached(npc, NPC_LAYER4));
    988  1.52.6.1  perseant 	switch(npc->npc_proto) {
    989  1.52.6.1  perseant 		case IPPROTO_TCP:
    990  1.52.6.1  perseant 			if (ret == 0) /* switch pass to block */ {
    991  1.52.6.1  perseant 				mi->mi_retfl &= !(NPF_RULE_PASS | NPF_RULE_STATEFUL |
    992  1.52.6.1  perseant 					NPF_RULE_GSTATEFUL);
    993  1.52.6.1  perseant 				mi->mi_retfl |= NPF_RULE_RETRST;
    994  1.52.6.1  perseant 			}
    995  1.52.6.1  perseant 			else /* block to pass */ {
    996  1.52.6.1  perseant 				mi->mi_retfl &= !(NPF_RULE_RETRST);
    997  1.52.6.1  perseant 				mi->mi_retfl |= (NPF_RULE_PASS | NPF_RULE_STATEFUL |
    998  1.52.6.1  perseant 					NPF_RULE_GSTATEFUL);
    999  1.52.6.1  perseant 			}
   1000  1.52.6.1  perseant 			break;
   1001  1.52.6.1  perseant 		case IPPROTO_UDP:
   1002  1.52.6.1  perseant 			if (ret == 0) /* pass to block */ {
   1003  1.52.6.1  perseant 				mi->mi_retfl &= !(NPF_RULE_PASS);
   1004  1.52.6.1  perseant 				mi->mi_retfl |= NPF_RULE_RETICMP;
   1005  1.52.6.1  perseant 			}
   1006  1.52.6.1  perseant 			else /* block to pass */ {
   1007  1.52.6.1  perseant 				mi->mi_retfl &= !(NPF_RULE_RETICMP);
   1008  1.52.6.1  perseant 				mi->mi_retfl |= NPF_RULE_PASS;
   1009  1.52.6.1  perseant 			}
   1010  1.52.6.1  perseant 			break;
   1011  1.52.6.1  perseant 	}
   1012  1.52.6.1  perseant 
   1013  1.52.6.1  perseant 	return (ret == 0) ? ENETUNREACH : 0;
   1014  1.52.6.1  perseant }
   1015  1.52.6.1  perseant 
   1016  1.52.6.1  perseant /* only perform uid/gid checks when set */
   1017  1.52.6.1  perseant int
   1018  1.52.6.1  perseant npf_rule_match_rid(npf_rule_t *rl, npf_cache_t *npc, int dir)
   1019  1.52.6.1  perseant {
   1020  1.52.6.1  perseant 	uint32_t sock_gid, sock_uid;
   1021  1.52.6.1  perseant 	bool uid_matched = false, gid_matched = false;
   1022  1.52.6.1  perseant 
   1023  1.52.6.1  perseant 	if (rl->gid.op == NPF_OP_NONE && rl->uid.op == NPF_OP_NONE)
   1024  1.52.6.1  perseant 		return -1; /* quickly return if packet has nothing to do with rids */
   1025  1.52.6.1  perseant 
   1026  1.52.6.1  perseant 	KASSERT(npf_iscached(npc, NPC_IP46));
   1027  1.52.6.1  perseant 	KASSERT(npf_iscached(npc, NPC_LAYER4));
   1028  1.52.6.1  perseant 
   1029  1.52.6.1  perseant 	if (rl->gid.op != NPF_OP_NONE) {
   1030  1.52.6.1  perseant 		if (npf_socket_lookup_rid(npc, kauth_cred_getegid, &sock_gid, dir) == -1)
   1031  1.52.6.1  perseant 			return ENOTCONN;
   1032  1.52.6.1  perseant 
   1033  1.52.6.1  perseant 		gid_matched |= npf_match_rid(&rl->gid, sock_gid);
   1034  1.52.6.1  perseant 	}
   1035  1.52.6.1  perseant 	if (rl->uid.op != NPF_OP_NONE) {
   1036  1.52.6.1  perseant 		if (npf_socket_lookup_rid(npc, kauth_cred_geteuid, &sock_uid, dir) == -1)
   1037  1.52.6.1  perseant 			return ENOTCONN;
   1038  1.52.6.1  perseant 
   1039  1.52.6.1  perseant 		uid_matched |= npf_match_rid(&rl->uid, sock_uid);
   1040  1.52.6.1  perseant 	}
   1041  1.52.6.1  perseant 
   1042  1.52.6.1  perseant 	/* if both uid and gid are set on rule, both must be matching to agree */
   1043  1.52.6.1  perseant 	if (rl->gid.op && rl->uid.op)
   1044  1.52.6.1  perseant 		return gid_matched && uid_matched;
   1045  1.52.6.1  perseant 	else
   1046  1.52.6.1  perseant 		return gid_matched || uid_matched;
   1047  1.52.6.1  perseant }
   1048  1.52.6.1  perseant 
   1049  1.52.6.1  perseant /*
   1050      1.17     rmind  * npf_rule_conclude: return decision and the flags for conclusion.
   1051       1.1     rmind  *
   1052       1.1     rmind  * => Returns ENETUNREACH if "block" and 0 if "pass".
   1053       1.1     rmind  */
   1054       1.1     rmind int
   1055      1.45  christos npf_rule_conclude(const npf_rule_t *rl, npf_match_info_t *mi)
   1056       1.1     rmind {
   1057       1.1     rmind 	/* If not passing - drop the packet. */
   1058      1.45  christos 	mi->mi_retfl = rl->r_attr;
   1059      1.45  christos 	mi->mi_rid = rl->r_id;
   1060      1.17     rmind 	return (rl->r_attr & NPF_RULE_PASS) ? 0 : ENETUNREACH;
   1061       1.1     rmind }
   1062      1.41     rmind 
   1063      1.41     rmind 
   1064      1.41     rmind #if defined(DDB) || defined(_NPF_TESTING)
   1065      1.41     rmind 
   1066      1.41     rmind void
   1067      1.43  christos npf_ruleset_dump(npf_t *npf, const char *name)
   1068      1.41     rmind {
   1069      1.43  christos 	npf_ruleset_t *rlset = npf_config_ruleset(npf);
   1070      1.41     rmind 	npf_rule_t *rg, *rl;
   1071      1.41     rmind 
   1072      1.41     rmind 	LIST_FOREACH(rg, &rlset->rs_dynamic, r_dentry) {
   1073      1.41     rmind 		printf("ruleset '%s':\n", rg->r_name);
   1074      1.42     rmind 		for (rl = rg->r_subset; rl; rl = rl->r_next) {
   1075      1.41     rmind 			printf("\tid %"PRIu64", key: ", rl->r_id);
   1076      1.51     rmind 			for (unsigned i = 0; i < NPF_RULE_MAXKEYLEN; i++)
   1077      1.41     rmind 				printf("%x", rl->r_key[i]);
   1078      1.41     rmind 			printf("\n");
   1079      1.41     rmind 		}
   1080      1.41     rmind 	}
   1081      1.41     rmind }
   1082      1.41     rmind 
   1083      1.41     rmind #endif
   1084