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