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