Home | History | Annotate | Line # | Download | only in npf
npf_ctl.c revision 1.12
      1 /*	$NetBSD: npf_ctl.c,v 1.12 2012/02/05 00:37:13 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009-2011 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 device control.
     34  *
     35  * Implementation of (re)loading, construction of tables and rules.
     36  * NPF proplib(9) dictionary consumer.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.12 2012/02/05 00:37:13 rmind Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/conf.h>
     44 #include <sys/kernel.h>
     45 
     46 #include <prop/proplib.h>
     47 
     48 #include "npf_ncode.h"
     49 #include "npf_impl.h"
     50 
     51 #if defined(DEBUG) || defined(DIAGNOSTIC)
     52 #define	NPF_ERR_DEBUG(e) \
     53 	prop_dictionary_set_cstring_nocopy((e), "source-file", __FILE__); \
     54 	prop_dictionary_set_uint32((e), "source-line", __LINE__);
     55 #else
     56 #define	NPF_ERR_DEBUG(e)
     57 #endif
     58 
     59 /*
     60  * npfctl_switch: enable or disable packet inspection.
     61  */
     62 int
     63 npfctl_switch(void *data)
     64 {
     65 	const bool onoff = *(int *)data ? true : false;
     66 	int error;
     67 
     68 	if (onoff) {
     69 		/* Enable: add pfil hooks. */
     70 		error = npf_register_pfil();
     71 	} else {
     72 		/* Disable: remove pfil hooks. */
     73 		npf_unregister_pfil();
     74 		error = 0;
     75 	}
     76 	return error;
     77 }
     78 
     79 static int __noinline
     80 npf_mk_tables(npf_tableset_t *tblset, prop_array_t tables,
     81     prop_dictionary_t errdict)
     82 {
     83 	prop_object_iterator_t it;
     84 	prop_dictionary_t tbldict;
     85 	int error = 0;
     86 
     87 	/* Tables - array. */
     88 	if (prop_object_type(tables) != PROP_TYPE_ARRAY) {
     89 		NPF_ERR_DEBUG(errdict);
     90 		return EINVAL;
     91 	}
     92 
     93 	it = prop_array_iterator(tables);
     94 	while ((tbldict = prop_object_iterator_next(it)) != NULL) {
     95 		prop_dictionary_t ent;
     96 		prop_object_iterator_t eit;
     97 		prop_array_t entries;
     98 		npf_table_t *t;
     99 		u_int tid;
    100 		int type;
    101 
    102 		/* Table - dictionary. */
    103 		if (prop_object_type(tbldict) != PROP_TYPE_DICTIONARY) {
    104 			NPF_ERR_DEBUG(errdict);
    105 			error = EINVAL;
    106 			break;
    107 		}
    108 
    109 		/* Table ID and type. */
    110 		prop_dictionary_get_uint32(tbldict, "id", &tid);
    111 		prop_dictionary_get_int32(tbldict, "type", &type);
    112 
    113 		/* Validate them, check for duplicate IDs. */
    114 		error = npf_table_check(tblset, tid, type);
    115 		if (error)
    116 			break;
    117 
    118 		/* Create and insert the table. */
    119 		t = npf_table_create(tid, type, 1024);	/* XXX */
    120 		if (t == NULL) {
    121 			NPF_ERR_DEBUG(errdict);
    122 			error = ENOMEM;
    123 			break;
    124 		}
    125 		error = npf_tableset_insert(tblset, t);
    126 		KASSERT(error == 0);
    127 
    128 		/* Entries. */
    129 		entries = prop_dictionary_get(tbldict, "entries");
    130 		if (prop_object_type(entries) != PROP_TYPE_ARRAY) {
    131 			NPF_ERR_DEBUG(errdict);
    132 			error = EINVAL;
    133 			break;
    134 		}
    135 		eit = prop_array_iterator(entries);
    136 		while ((ent = prop_object_iterator_next(eit)) != NULL) {
    137 			const npf_addr_t *addr;
    138 			npf_netmask_t mask;
    139 
    140 			/* Get address and mask.  Add a table entry. */
    141 			addr = (const npf_addr_t *)prop_data_data_nocopy(
    142 			    prop_dictionary_get(ent, "addr"));
    143 			prop_dictionary_get_uint8(ent, "mask", &mask);
    144 			error = npf_table_add_cidr(tblset, tid, addr, mask);
    145 			if (error)
    146 				break;
    147 		}
    148 		prop_object_iterator_release(eit);
    149 		if (error)
    150 			break;
    151 	}
    152 	prop_object_iterator_release(it);
    153 	/*
    154 	 * Note: in a case of error, caller will free the tableset.
    155 	 */
    156 	return error;
    157 }
    158 
    159 static npf_rproc_t *
    160 npf_mk_rproc(prop_array_t rprocs, const char *rpname)
    161 {
    162 	prop_object_iterator_t it;
    163 	prop_dictionary_t rpdict;
    164 	npf_rproc_t *rp;
    165 
    166 	it = prop_array_iterator(rprocs);
    167 	while ((rpdict = prop_object_iterator_next(it)) != NULL) {
    168 		const char *iname;
    169 		prop_dictionary_get_cstring_nocopy(rpdict, "name", &iname);
    170 		KASSERT(iname != NULL);
    171 		if (strcmp(rpname, iname) == 0)
    172 			break;
    173 	}
    174 	prop_object_iterator_release(it);
    175 	if (rpdict == NULL) {
    176 		return NULL;
    177 	}
    178 	CTASSERT(sizeof(uintptr_t) <= sizeof(uint64_t));
    179 	if (!prop_dictionary_get_uint64(rpdict, "rproc-ptr", (uint64_t *)&rp)) {
    180 		rp = npf_rproc_create(rpdict);
    181 		prop_dictionary_set_uint64(rpdict, "rproc-ptr",
    182 		    (uint64_t)(uintptr_t)rp);
    183 	}
    184 	return rp;
    185 }
    186 
    187 static int __noinline
    188 npf_mk_ncode(prop_object_t obj, void **code, size_t *csize,
    189     prop_dictionary_t errdict)
    190 {
    191 	const void *ncptr;
    192 	int nc_err, errat;
    193 	size_t nc_size;
    194 	void *nc;
    195 
    196 	/*
    197 	 * Allocate, copy and validate n-code. XXX: Inefficient.
    198 	 */
    199 	ncptr = prop_data_data_nocopy(obj);
    200 	nc_size = prop_data_size(obj);
    201 	if (ncptr == NULL || nc_size > NPF_NCODE_LIMIT) {
    202 		NPF_ERR_DEBUG(errdict);
    203 		return ERANGE;
    204 	}
    205 	nc = npf_ncode_alloc(nc_size);
    206 	if (nc == NULL) {
    207 		NPF_ERR_DEBUG(errdict);
    208 		return ENOMEM;
    209 	}
    210 	memcpy(nc, ncptr, nc_size);
    211 	nc_err = npf_ncode_validate(nc, nc_size, &errat);
    212 	if (nc_err) {
    213 		npf_ncode_free(nc, nc_size);
    214 		prop_dictionary_set_int32(errdict, "ncode-error", nc_err);
    215 		prop_dictionary_set_int32(errdict, "ncode-errat", errat);
    216 		return EINVAL;
    217 	}
    218 	*code = nc;
    219 	*csize = nc_size;
    220 	return 0;
    221 }
    222 
    223 static int __noinline
    224 npf_mk_singlerule(prop_dictionary_t rldict, prop_array_t rps, npf_rule_t **rl,
    225     prop_dictionary_t errdict)
    226 {
    227 	const char *rnm;
    228 	npf_rproc_t *rp;
    229 	prop_object_t obj;
    230 	size_t nc_size;
    231 	void *nc;
    232 	int p, error;
    233 
    234 	/* Rule - dictionary. */
    235 	if (prop_object_type(rldict) != PROP_TYPE_DICTIONARY) {
    236 		NPF_ERR_DEBUG(errdict);
    237 		return EINVAL;
    238 	}
    239 
    240 	error = 0;
    241 	obj = prop_dictionary_get(rldict, "ncode");
    242 	if (obj) {
    243 		/* N-code (binary data). */
    244 		error = npf_mk_ncode(obj, &nc, &nc_size, errdict);
    245 		if (error) {
    246 			goto err;
    247 		}
    248 	} else {
    249 		/* No n-code. */
    250 		nc = NULL;
    251 		nc_size = 0;
    252 	}
    253 
    254 	/* Check for rule procedure. */
    255 	if (rps && prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rnm)) {
    256 		rp = npf_mk_rproc(rps, rnm);
    257 		if (rp == NULL) {
    258 			if (nc) {
    259 				npf_ncode_free(nc, nc_size);	/* XXX */
    260 			}
    261 			NPF_ERR_DEBUG(errdict);
    262 			error = EINVAL;
    263 			goto err;
    264 		}
    265 	} else {
    266 		rp = NULL;
    267 	}
    268 
    269 	/* Finally, allocate and return the rule. */
    270 	*rl = npf_rule_alloc(rldict, rp, nc, nc_size);
    271 	KASSERT(*rl != NULL);
    272 	return 0;
    273 err:
    274 	prop_dictionary_get_int32(rldict, "priority", &p); /* XXX */
    275 	prop_dictionary_set_int32(errdict, "id", p);
    276 	return error;
    277 }
    278 
    279 static int __noinline
    280 npf_mk_subrules(npf_ruleset_t *rlset, prop_array_t rules, prop_array_t rprocs,
    281     prop_dictionary_t errdict)
    282 {
    283 	prop_object_iterator_t it;
    284 	prop_dictionary_t rldict;
    285 	int error = 0;
    286 
    287 	if (prop_object_type(rules) != PROP_TYPE_ARRAY) {
    288 		NPF_ERR_DEBUG(errdict);
    289 		return EINVAL;
    290 	}
    291 	it = prop_array_iterator(rules);
    292 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    293 		npf_rule_t *rl;
    294 		error = npf_mk_singlerule(rldict, rprocs, &rl, errdict);
    295 		if (error) {
    296 			break;
    297 		}
    298 		npf_ruleset_insert(rlset, rl);
    299 	}
    300 	prop_object_iterator_release(it);
    301 	return error;
    302 }
    303 
    304 static int __noinline
    305 npf_mk_rules(npf_ruleset_t *rlset, prop_array_t rules, prop_array_t rprocs,
    306     prop_dictionary_t errdict)
    307 {
    308 	prop_object_iterator_t it;
    309 	prop_dictionary_t rldict, rpdict;
    310 	int error;
    311 
    312 	/* Rule procedures and the ruleset - arrays. */
    313 	if (prop_object_type(rprocs) != PROP_TYPE_ARRAY ||
    314 	    prop_object_type(rules) != PROP_TYPE_ARRAY) {
    315 		NPF_ERR_DEBUG(errdict);
    316 		return EINVAL;
    317 	}
    318 
    319 	it = prop_array_iterator(rprocs);
    320 	while ((rpdict = prop_object_iterator_next(it)) != NULL) {
    321 		if (prop_dictionary_get(rpdict, "rproc-ptr")) {
    322 			prop_object_iterator_release(it);
    323 			NPF_ERR_DEBUG(errdict);
    324 			return EINVAL;
    325 		}
    326 	}
    327 	prop_object_iterator_release(it);
    328 
    329 	error = 0;
    330 	it = prop_array_iterator(rules);
    331 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    332 		prop_array_t subrules;
    333 		npf_ruleset_t *rlsetsub;
    334 		npf_rule_t *rl;
    335 
    336 		/* Generate a single rule. */
    337 		error = npf_mk_singlerule(rldict, rprocs, &rl, errdict);
    338 		if (error) {
    339 			break;
    340 		}
    341 		npf_ruleset_insert(rlset, rl);
    342 
    343 		/* Check for sub-rules and generate, if any. */
    344 		subrules = prop_dictionary_get(rldict, "subrules");
    345 		if (subrules == NULL) {
    346 			/* No subrules, next.. */
    347 			continue;
    348 		}
    349 		rlsetsub = npf_rule_subset(rl);
    350 		error = npf_mk_subrules(rlsetsub, subrules, rprocs, errdict);
    351 		if (error)
    352 			break;
    353 	}
    354 	prop_object_iterator_release(it);
    355 	/*
    356 	 * Note: in a case of error, caller will free the ruleset.
    357 	 */
    358 	return error;
    359 }
    360 
    361 static int __noinline
    362 npf_mk_natlist(npf_ruleset_t *nset, prop_array_t natlist,
    363     prop_dictionary_t errdict)
    364 {
    365 	prop_object_iterator_t it;
    366 	prop_dictionary_t natdict;
    367 	int error;
    368 
    369 	/* NAT policies - array. */
    370 	if (prop_object_type(natlist) != PROP_TYPE_ARRAY) {
    371 		NPF_ERR_DEBUG(errdict);
    372 		return EINVAL;
    373 	}
    374 
    375 	error = 0;
    376 	it = prop_array_iterator(natlist);
    377 	while ((natdict = prop_object_iterator_next(it)) != NULL) {
    378 		npf_natpolicy_t *np;
    379 		npf_rule_t *rl;
    380 
    381 		/* NAT policy - dictionary. */
    382 		if (prop_object_type(natdict) != PROP_TYPE_DICTIONARY) {
    383 			NPF_ERR_DEBUG(errdict);
    384 			error = EINVAL;
    385 			break;
    386 		}
    387 
    388 		/*
    389 		 * NAT policies are standard rules, plus additional
    390 		 * information for translation.  Make a rule.
    391 		 */
    392 		error = npf_mk_singlerule(natdict, NULL, &rl, errdict);
    393 		if (error) {
    394 			break;
    395 		}
    396 		npf_ruleset_insert(nset, rl);
    397 
    398 		/* If rule is named, it is a group with NAT policies. */
    399 		if (prop_dictionary_get(natdict, "name") &&
    400 		    prop_dictionary_get(natdict, "subrules")) {
    401 			continue;
    402 		}
    403 
    404 		/* Allocate a new NAT policy and assign to the rule. */
    405 		np = npf_nat_newpolicy(natdict, nset);
    406 		if (np == NULL) {
    407 			NPF_ERR_DEBUG(errdict);
    408 			error = ENOMEM;
    409 			break;
    410 		}
    411 		npf_rule_setnat(rl, np);
    412 	}
    413 	prop_object_iterator_release(it);
    414 	/*
    415 	 * Note: in a case of error, caller will free entire NAT ruleset
    416 	 * with assigned NAT policies.
    417 	 */
    418 	return error;
    419 }
    420 
    421 /*
    422  * npfctl_reload: store passed data i.e. update settings, create passed
    423  * tables, rules and atomically activate all them.
    424  */
    425 int
    426 npfctl_reload(u_long cmd, void *data)
    427 {
    428 	struct plistref *pref = data;
    429 	prop_dictionary_t dict, errdict;
    430 	prop_array_t natlist, tables, rprocs, rules;
    431 	npf_tableset_t *tblset = NULL;
    432 	npf_ruleset_t *rlset = NULL;
    433 	npf_ruleset_t *nset = NULL;
    434 	bool flush;
    435 	int error;
    436 
    437 	/* Retrieve the dictionary. */
    438 #ifdef _KERNEL
    439 	error = prop_dictionary_copyin_ioctl(pref, cmd, &dict);
    440 	if (error)
    441 		return error;
    442 #else
    443 	dict = prop_dictionary_internalize_from_file(data);
    444 	if (dict == NULL)
    445 		return EINVAL;
    446 #endif
    447 	/* Dictionary for error reporting. */
    448 	errdict = prop_dictionary_create();
    449 
    450 	/* NAT policies. */
    451 	nset = npf_ruleset_create();
    452 	natlist = prop_dictionary_get(dict, "translation");
    453 	error = npf_mk_natlist(nset, natlist, errdict);
    454 	if (error) {
    455 		goto fail;
    456 	}
    457 
    458 	/* Tables. */
    459 	tblset = npf_tableset_create();
    460 	tables = prop_dictionary_get(dict, "tables");
    461 	error = npf_mk_tables(tblset, tables, errdict);
    462 	if (error) {
    463 		goto fail;
    464 	}
    465 
    466 	/* Rules and rule procedures. */
    467 	rlset = npf_ruleset_create();
    468 	rprocs = prop_dictionary_get(dict, "rprocs");
    469 	rules = prop_dictionary_get(dict, "rules");
    470 	error = npf_mk_rules(rlset, rules, rprocs, errdict);
    471 	if (error) {
    472 		goto fail;
    473 	}
    474 
    475 	flush = false;
    476 	prop_dictionary_get_bool(dict, "flush", &flush);
    477 
    478 	/*
    479 	 * Finally - reload ruleset, tableset and NAT policies.
    480 	 * Operation will be performed as a single transaction.
    481 	 */
    482 	npf_reload(rlset, tblset, nset);
    483 
    484 	/* Turn on/off session tracking accordingly. */
    485 	npf_session_tracking(!flush);
    486 
    487 	/* Done.  Since data is consumed now, we shall not destroy it. */
    488 	tblset = NULL;
    489 	rlset = NULL;
    490 	nset = NULL;
    491 fail:
    492 	/*
    493 	 * Note: destroy rulesets first, to drop references to the tableset.
    494 	 */
    495 	KASSERT(error == 0 || (nset || rlset || tblset));
    496 	if (nset) {
    497 		npf_ruleset_destroy(nset);
    498 	}
    499 	if (rlset) {
    500 		npf_ruleset_destroy(rlset);
    501 	}
    502 	if (tblset) {
    503 		npf_tableset_destroy(tblset);
    504 	}
    505 	prop_object_release(dict);
    506 
    507 	/* Error report. */
    508 	prop_dictionary_set_int32(errdict, "errno", error);
    509 	prop_dictionary_copyout_ioctl(pref, cmd, errdict);
    510 	prop_object_release(errdict);
    511 	return 0;
    512 }
    513 
    514 /*
    515  * npfctl_update_rule: reload a specific rule identified by the name.
    516  */
    517 int
    518 npfctl_update_rule(u_long cmd, void *data)
    519 {
    520 	struct plistref *pref = data;
    521 	prop_dictionary_t dict, errdict;
    522 	prop_array_t subrules;
    523 	prop_object_t obj;
    524 	npf_ruleset_t *rlset;
    525 	const char *name;
    526 	int error;
    527 
    528 #ifdef _KERNEL
    529 	/* Retrieve and construct the rule. */
    530 	error = prop_dictionary_copyin_ioctl(pref, cmd, &dict);
    531 	if (error) {
    532 		return error;
    533 	}
    534 #else
    535 	dict = prop_dictionary_internalize_from_file(data);
    536 	if (dict == NULL)
    537 		return EINVAL;
    538 #endif
    539 
    540 	/* Dictionary for error reporting. */
    541 	errdict = prop_dictionary_create();
    542 
    543 	/* Create the ruleset and construct sub-rules. */
    544 	rlset = npf_ruleset_create();
    545 	subrules = prop_dictionary_get(dict, "subrules");
    546 	error = npf_mk_subrules(rlset, subrules, NULL, errdict);
    547 	if (error) {
    548 		goto out;
    549 	}
    550 
    551 	/* Lookup the rule by name, and replace its subset (sub-rules). */
    552 	obj = prop_dictionary_get(dict, "name");
    553 	name = prop_string_cstring_nocopy(obj);
    554 	if (npf_ruleset_replace(name, rlset) == NULL) {
    555 		/* Not found. */
    556 		error = ENOENT;
    557 out:		/* Error path. */
    558 		npf_ruleset_destroy(rlset);
    559 	}
    560 	prop_object_release(dict);
    561 
    562 	/* Error report. */
    563 	prop_dictionary_set_int32(errdict, "errno", error);
    564 	prop_dictionary_copyout_ioctl(pref, cmd, errdict);
    565 	prop_object_release(errdict);
    566 	return error;
    567 }
    568 
    569 /*
    570  * npfctl_sessions_save: construct a list of sessions and export for saving.
    571  */
    572 int
    573 npfctl_sessions_save(u_long cmd, void *data)
    574 {
    575 	struct plistref *pref = data;
    576 	prop_dictionary_t sesdict;
    577 	prop_array_t selist, nplist;
    578 	int error;
    579 
    580 	/* Create a dictionary and two lists. */
    581 	sesdict = prop_dictionary_create();
    582 	selist = prop_array_create();
    583 	nplist = prop_array_create();
    584 
    585 	/* Save the sessions. */
    586 	error = npf_session_save(selist, nplist);
    587 	if (error) {
    588 		goto fail;
    589 	}
    590 
    591 	/* Set the session list, NAT policy list and export the dictionary. */
    592 	prop_dictionary_set(sesdict, "session-list", selist);
    593 	prop_dictionary_set(sesdict, "nat-policy-list", nplist);
    594 #ifdef _KERNEL
    595 	error = prop_dictionary_copyout_ioctl(pref, cmd, sesdict);
    596 #else
    597 	error = prop_dictionary_externalize_to_file(sesdict, data) ? 0 : errno;
    598 #endif
    599 fail:
    600 	prop_object_release(sesdict);
    601 	return error;
    602 }
    603 
    604 /*
    605  * npfctl_sessions_load: import a list of sessions, reconstruct them and load.
    606  */
    607 int
    608 npfctl_sessions_load(u_long cmd, void *data)
    609 {
    610 	const struct plistref *pref = data;
    611 	npf_sehash_t *sehasht = NULL;
    612 	prop_dictionary_t sesdict, sedict;
    613 	prop_object_iterator_t it;
    614 	prop_array_t selist;
    615 	int error;
    616 
    617 	/* Retrieve the dictionary containing session and NAT policy lists. */
    618 #ifdef _KERNEL
    619 	error = prop_dictionary_copyin_ioctl(pref, cmd, &sesdict);
    620 	if (error)
    621 		return error;
    622 #else
    623 	sesdict = prop_dictionary_internalize_from_file(data);
    624 	if (sesdict == NULL)
    625 		return EINVAL;
    626 #endif
    627 	/*
    628 	 * Note: session objects contain the references to the NAT policy
    629 	 * entries.  Therefore, no need to directly access it.
    630 	 */
    631 	selist = prop_dictionary_get(sesdict, "session-list");
    632 	if (prop_object_type(selist) != PROP_TYPE_ARRAY) {
    633 		error = EINVAL;
    634 		goto fail;
    635 	}
    636 
    637 	/* Create a session hash table. */
    638 	sehasht = sess_htable_create();
    639 	if (sehasht == NULL) {
    640 		error = ENOMEM;
    641 		goto fail;
    642 	}
    643 
    644 	/*
    645 	 * Iterate through and construct each session.
    646 	 */
    647 	error = 0;
    648 	it = prop_array_iterator(selist);
    649 	npf_core_enter();
    650 	while ((sedict = prop_object_iterator_next(it)) != NULL) {
    651 		/* Session - dictionary. */
    652 		if (prop_object_type(sedict) != PROP_TYPE_DICTIONARY) {
    653 			error = EINVAL;
    654 			goto fail;
    655 		}
    656 		/* Construct and insert real session structure. */
    657 		error = npf_session_restore(sehasht, sedict);
    658 		if (error) {
    659 			goto fail;
    660 		}
    661 	}
    662 	npf_core_exit();
    663 	sess_htable_reload(sehasht);
    664 fail:
    665 	prop_object_release(selist);
    666 	if (error && sehasht) {
    667 		/* Destroy session table. */
    668 		sess_htable_destroy(sehasht);
    669 	}
    670 	return error;
    671 }
    672 
    673 /*
    674  * npfctl_table: add, remove or query entries in the specified table.
    675  *
    676  * For maximum performance, interface is avoiding proplib(3)'s overhead.
    677  */
    678 int
    679 npfctl_table(void *data)
    680 {
    681 	npf_ioctl_table_t *nct = data;
    682 	npf_tableset_t *tblset;
    683 	int error;
    684 
    685 	npf_core_enter(); /* XXXSMP */
    686 	tblset = npf_core_tableset();
    687 	switch (nct->nct_action) {
    688 	case NPF_IOCTL_TBLENT_ADD:
    689 		error = npf_table_add_cidr(tblset, nct->nct_tid,
    690 		    &nct->nct_addr, nct->nct_mask);
    691 		break;
    692 	case NPF_IOCTL_TBLENT_REM:
    693 		error = npf_table_rem_cidr(tblset, nct->nct_tid,
    694 		    &nct->nct_addr, nct->nct_mask);
    695 		break;
    696 	default:
    697 		error = npf_table_match_addr(tblset, nct->nct_tid,
    698 		    &nct->nct_addr);
    699 	}
    700 	npf_core_exit(); /* XXXSMP */
    701 	return error;
    702 }
    703