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