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