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