Home | History | Annotate | Line # | Download | only in npf
npf_ctl.c revision 1.4
      1  1.4  rmind /*	$NetBSD: npf_ctl.c,v 1.4 2010/12/18 01:07:25 rmind Exp $	*/
      2  1.1  rmind 
      3  1.1  rmind /*-
      4  1.1  rmind  * Copyright (c) 2009-2010 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.4  rmind __KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.4 2010/12/18 01:07:25 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.1  rmind static int
     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 	prop_object_t obj;
     77  1.1  rmind 	int error = 0;
     78  1.1  rmind 
     79  1.1  rmind 	/* Tables - array. */
     80  1.1  rmind 	if (prop_object_type(tables) != PROP_TYPE_ARRAY)
     81  1.1  rmind 		return EINVAL;
     82  1.1  rmind 
     83  1.1  rmind 	it = prop_array_iterator(tables);
     84  1.1  rmind 	while ((tbldict = prop_object_iterator_next(it)) != NULL) {
     85  1.1  rmind 		prop_dictionary_t ent;
     86  1.1  rmind 		prop_object_iterator_t eit;
     87  1.1  rmind 		prop_array_t entries;
     88  1.1  rmind 		npf_table_t *t;
     89  1.1  rmind 		u_int tid;
     90  1.1  rmind 		int type;
     91  1.1  rmind 
     92  1.1  rmind 		/* Table - dictionary. */
     93  1.1  rmind 		if (prop_object_type(tbldict) != PROP_TYPE_DICTIONARY) {
     94  1.1  rmind 			error = EINVAL;
     95  1.1  rmind 			break;
     96  1.1  rmind 		}
     97  1.1  rmind 
     98  1.1  rmind 		/* Table ID and type. */
     99  1.1  rmind 		obj = prop_dictionary_get(tbldict, "id");
    100  1.1  rmind 		tid = (u_int)prop_number_integer_value(obj);
    101  1.1  rmind 		obj = prop_dictionary_get(tbldict, "type");
    102  1.1  rmind 		type = (int)prop_number_integer_value(obj);
    103  1.1  rmind 		/* Validate them. */
    104  1.1  rmind 		error = npf_table_check(tblset, tid, type);
    105  1.1  rmind 		if (error)
    106  1.1  rmind 			break;
    107  1.1  rmind 
    108  1.1  rmind 		/* Create and insert the table. */
    109  1.1  rmind 		t = npf_table_create(tid, type, 1024);	/* XXX */
    110  1.1  rmind 		if (t == NULL) {
    111  1.1  rmind 			error = ENOMEM;
    112  1.1  rmind 			break;
    113  1.1  rmind 		}
    114  1.1  rmind 		error = npf_tableset_insert(tblset, t);
    115  1.1  rmind 		KASSERT(error == 0);
    116  1.1  rmind 
    117  1.1  rmind 		/* Entries. */
    118  1.1  rmind 		entries = prop_dictionary_get(tbldict, "entries");
    119  1.1  rmind 		if (prop_object_type(entries) != PROP_TYPE_ARRAY) {
    120  1.1  rmind 			error = EINVAL;
    121  1.1  rmind 			break;
    122  1.1  rmind 		}
    123  1.1  rmind 		eit = prop_array_iterator(entries);
    124  1.1  rmind 		while ((ent = prop_object_iterator_next(eit)) != NULL) {
    125  1.1  rmind 			in_addr_t addr, mask;	/* XXX: IPv6 */
    126  1.1  rmind 
    127  1.1  rmind 			/* Address. */
    128  1.1  rmind 			obj = prop_dictionary_get(ent, "addr");
    129  1.1  rmind 			addr = (in_addr_t)prop_number_integer_value(obj);
    130  1.1  rmind 			/* Mask. */
    131  1.1  rmind 			obj = prop_dictionary_get(ent, "mask");
    132  1.1  rmind 			mask = (in_addr_t)prop_number_integer_value(obj);
    133  1.1  rmind 			/* Add a table entry. */
    134  1.1  rmind 			error = npf_table_add_v4cidr(tblset, tid, addr, mask);
    135  1.1  rmind 			if (error)
    136  1.1  rmind 				break;
    137  1.1  rmind 		}
    138  1.1  rmind 		prop_object_iterator_release(eit);
    139  1.1  rmind 		if (error)
    140  1.1  rmind 			break;
    141  1.1  rmind 	}
    142  1.1  rmind 	prop_object_iterator_release(it);
    143  1.1  rmind 	/*
    144  1.4  rmind 	 * Note: in a case of error, caller will free the tableset.
    145  1.1  rmind 	 */
    146  1.1  rmind 	return error;
    147  1.1  rmind }
    148  1.1  rmind 
    149  1.1  rmind static int
    150  1.1  rmind npf_mk_singlerule(prop_dictionary_t rldict,
    151  1.1  rmind     npf_ruleset_t *rlset, npf_rule_t **parent)
    152  1.1  rmind {
    153  1.1  rmind 	npf_rule_t *rl;
    154  1.1  rmind 	prop_object_t obj;
    155  1.1  rmind 	size_t nc_size;
    156  1.1  rmind 	void *nc;
    157  1.1  rmind 
    158  1.1  rmind 	/* Rule - dictionary. */
    159  1.1  rmind 	if (prop_object_type(rldict) != PROP_TYPE_DICTIONARY)
    160  1.1  rmind 		return EINVAL;
    161  1.1  rmind 
    162  1.1  rmind 	/* N-code (binary data). */
    163  1.1  rmind 	obj = prop_dictionary_get(rldict, "ncode");
    164  1.1  rmind 	if (obj) {
    165  1.1  rmind 		const void *ncptr;
    166  1.4  rmind 		int npf_err, errat;
    167  1.1  rmind 
    168  1.4  rmind 		/*
    169  1.4  rmind 		 * Allocate, copy and validate n-code. XXX: Inefficient.
    170  1.4  rmind 		 */
    171  1.4  rmind 		ncptr = prop_data_data_nocopy(obj);
    172  1.1  rmind 		nc_size = prop_data_size(obj);
    173  1.1  rmind 		if (ncptr == NULL || nc_size > NPF_NCODE_LIMIT) {
    174  1.1  rmind 			return EINVAL;
    175  1.1  rmind 		}
    176  1.4  rmind 		nc = npf_ncode_alloc(nc_size);
    177  1.1  rmind 		if (nc == NULL) {
    178  1.1  rmind 			return EINVAL;
    179  1.1  rmind 		}
    180  1.4  rmind 		memcpy(nc, ncptr, nc_size);
    181  1.4  rmind 		npf_err = npf_ncode_validate(nc, nc_size, &errat);
    182  1.4  rmind 		if (npf_err) {
    183  1.4  rmind 			npf_ncode_free(nc, nc_size);
    184  1.4  rmind 			/* TODO: return error details via proplib */
    185  1.4  rmind 			return EINVAL;
    186  1.4  rmind 		}
    187  1.1  rmind 	} else {
    188  1.1  rmind 		/* No n-code. */
    189  1.1  rmind 		nc = NULL;
    190  1.1  rmind 		nc_size = 0;
    191  1.1  rmind 	}
    192  1.1  rmind 
    193  1.1  rmind 	/* Allocate and setup NPF rule. */
    194  1.4  rmind 	rl = npf_rule_alloc(rldict, nc, nc_size);
    195  1.1  rmind 	if (rl == NULL) {
    196  1.1  rmind 		if (nc) {
    197  1.1  rmind 			npf_ncode_free(nc, nc_size);	/* XXX */
    198  1.1  rmind 		}
    199  1.1  rmind 		return ENOMEM;
    200  1.1  rmind 	}
    201  1.1  rmind 	npf_ruleset_insert(rlset, rl);
    202  1.1  rmind 	if (parent) {
    203  1.1  rmind 		*parent = rl;
    204  1.1  rmind 	}
    205  1.1  rmind 	return 0;
    206  1.1  rmind }
    207  1.1  rmind 
    208  1.1  rmind static int
    209  1.1  rmind npf_mk_rules(npf_ruleset_t *rlset, prop_array_t rules)
    210  1.1  rmind {
    211  1.1  rmind 	prop_object_iterator_t it;
    212  1.1  rmind 	prop_dictionary_t rldict;
    213  1.1  rmind 	int error;
    214  1.1  rmind 
    215  1.1  rmind 	/* Ruleset - array. */
    216  1.1  rmind 	if (prop_object_type(rules) != PROP_TYPE_ARRAY)
    217  1.1  rmind 		return EINVAL;
    218  1.1  rmind 
    219  1.4  rmind 	error = 0;
    220  1.1  rmind 	it = prop_array_iterator(rules);
    221  1.1  rmind 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    222  1.1  rmind 		prop_object_iterator_t sit;
    223  1.1  rmind 		prop_array_t subrules;
    224  1.1  rmind 		prop_dictionary_t srldict;
    225  1.1  rmind 		npf_rule_t *myrl;
    226  1.1  rmind 
    227  1.1  rmind 		/* Generate a single rule. */
    228  1.1  rmind 		error = npf_mk_singlerule(rldict, rlset, &myrl);
    229  1.1  rmind 		if (error)
    230  1.1  rmind 			break;
    231  1.1  rmind 
    232  1.1  rmind 		/* Check for subrules. */
    233  1.1  rmind 		subrules = prop_dictionary_get(rldict, "subrules");
    234  1.1  rmind 		if (subrules == NULL) {
    235  1.1  rmind 			/* No subrules, next.. */
    236  1.1  rmind 			continue;
    237  1.1  rmind 		}
    238  1.1  rmind 		/* Generate subrules, if any. */
    239  1.1  rmind 		if (prop_object_type(subrules) != PROP_TYPE_ARRAY) {
    240  1.1  rmind 			error = EINVAL;
    241  1.1  rmind 			break;
    242  1.1  rmind 		}
    243  1.1  rmind 		sit = prop_array_iterator(subrules);
    244  1.1  rmind 		while ((srldict = prop_object_iterator_next(sit)) != NULL) {
    245  1.1  rmind 			/* For subrule, pass ruleset pointer of parent. */
    246  1.1  rmind 			error = npf_mk_singlerule(srldict,
    247  1.1  rmind 			    npf_rule_subset(myrl), NULL);
    248  1.1  rmind 			if (error)
    249  1.1  rmind 				break;
    250  1.1  rmind 		}
    251  1.1  rmind 		prop_object_iterator_release(sit);
    252  1.1  rmind 		if (error)
    253  1.1  rmind 			break;
    254  1.1  rmind 	}
    255  1.1  rmind 	prop_object_iterator_release(it);
    256  1.1  rmind 	/*
    257  1.4  rmind 	 * Note: in a case of error, caller will free the ruleset.
    258  1.1  rmind 	 */
    259  1.1  rmind 	return error;
    260  1.1  rmind }
    261  1.1  rmind 
    262  1.1  rmind static int
    263  1.1  rmind npf_mk_natlist(npf_ruleset_t *nset, prop_array_t natlist)
    264  1.1  rmind {
    265  1.1  rmind 	prop_object_iterator_t it;
    266  1.1  rmind 	prop_dictionary_t natdict;
    267  1.1  rmind 	int error;
    268  1.1  rmind 
    269  1.1  rmind 	/* NAT policies - array. */
    270  1.1  rmind 	if (prop_object_type(natlist) != PROP_TYPE_ARRAY)
    271  1.1  rmind 		return EINVAL;
    272  1.1  rmind 
    273  1.4  rmind 	error = 0;
    274  1.1  rmind 	it = prop_array_iterator(natlist);
    275  1.1  rmind 	while ((natdict = prop_object_iterator_next(it)) != NULL) {
    276  1.1  rmind 		npf_natpolicy_t *np;
    277  1.1  rmind 		npf_rule_t *rl;
    278  1.1  rmind 
    279  1.1  rmind 		/* NAT policy - dictionary. */
    280  1.1  rmind 		if (prop_object_type(natdict) != PROP_TYPE_DICTIONARY) {
    281  1.1  rmind 			error = EINVAL;
    282  1.1  rmind 			break;
    283  1.1  rmind 		}
    284  1.1  rmind 
    285  1.1  rmind 		/*
    286  1.1  rmind 		 * NAT policies are standard rules, plus additional
    287  1.1  rmind 		 * information for translation.  Make a rule.
    288  1.1  rmind 		 */
    289  1.1  rmind 		error = npf_mk_singlerule(natdict, nset, &rl);
    290  1.1  rmind 		if (error)
    291  1.1  rmind 			break;
    292  1.1  rmind 
    293  1.1  rmind 		/* Allocate a new NAT policy and assign to the rule. */
    294  1.4  rmind 		np = npf_nat_newpolicy(natdict);
    295  1.1  rmind 		if (np == NULL) {
    296  1.4  rmind 			npf_rule_free(rl);
    297  1.1  rmind 			error = ENOMEM;
    298  1.1  rmind 			break;
    299  1.1  rmind 		}
    300  1.1  rmind 		npf_rule_setnat(rl, np);
    301  1.1  rmind 	}
    302  1.1  rmind 	prop_object_iterator_release(it);
    303  1.1  rmind 	/*
    304  1.1  rmind 	 * Note: in a case of error, caller will free entire NAT ruleset
    305  1.1  rmind 	 * with assigned NAT policies.
    306  1.1  rmind 	 */
    307  1.1  rmind 	return error;
    308  1.1  rmind }
    309  1.1  rmind 
    310  1.1  rmind /*
    311  1.1  rmind  * npfctl_reload: store passed data i.e. update settings, create passed
    312  1.1  rmind  * tables, rules and atomically activate all them.
    313  1.1  rmind  */
    314  1.1  rmind int
    315  1.1  rmind npfctl_reload(u_long cmd, void *data)
    316  1.1  rmind {
    317  1.1  rmind 	const struct plistref *pref = data;
    318  1.1  rmind 	npf_tableset_t *tblset = NULL;
    319  1.1  rmind 	npf_ruleset_t *rlset = NULL;
    320  1.1  rmind 	npf_ruleset_t *nset = NULL;
    321  1.1  rmind 	prop_dictionary_t dict;
    322  1.1  rmind 	prop_array_t natlist, tables, rules;
    323  1.1  rmind 	int error;
    324  1.1  rmind 
    325  1.1  rmind 	/* Retrieve the dictionary. */
    326  1.1  rmind #ifdef _KERNEL
    327  1.1  rmind 	error = prop_dictionary_copyin_ioctl(pref, cmd, &dict);
    328  1.1  rmind 	if (error)
    329  1.1  rmind 		return error;
    330  1.1  rmind #else
    331  1.1  rmind 	dict = prop_dictionary_internalize_from_file(data);
    332  1.1  rmind 	if (dict == NULL)
    333  1.1  rmind 		return EINVAL;
    334  1.1  rmind #endif
    335  1.1  rmind 	/* NAT policies. */
    336  1.1  rmind 	nset = npf_ruleset_create();
    337  1.2  rmind 	natlist = prop_dictionary_get(dict, "translation");
    338  1.1  rmind 	error = npf_mk_natlist(nset, natlist);
    339  1.1  rmind 	if (error)
    340  1.1  rmind 		goto fail;
    341  1.1  rmind 
    342  1.1  rmind 	/* Tables. */
    343  1.1  rmind 	tblset = npf_tableset_create();
    344  1.1  rmind 	tables = prop_dictionary_get(dict, "tables");
    345  1.1  rmind 	error = npf_mk_tables(tblset, tables);
    346  1.1  rmind 	if (error)
    347  1.1  rmind 		goto fail;
    348  1.1  rmind 
    349  1.1  rmind 	/* Rules. */
    350  1.1  rmind 	rlset = npf_ruleset_create();
    351  1.1  rmind 	rules = prop_dictionary_get(dict, "rules");
    352  1.1  rmind 	error = npf_mk_rules(rlset, rules);
    353  1.1  rmind 	if (error)
    354  1.1  rmind 		goto fail;
    355  1.1  rmind 
    356  1.1  rmind 	/*
    357  1.4  rmind 	 * Finally - reload ruleset, tableset and NAT policies.
    358  1.1  rmind 	 * Operation will be performed as a single transaction.
    359  1.1  rmind 	 */
    360  1.4  rmind 	npf_reload(rlset, tblset, nset);
    361  1.1  rmind 
    362  1.1  rmind 	/* Done.  Since data is consumed now, we shall not destroy it. */
    363  1.1  rmind 	tblset = NULL;
    364  1.1  rmind 	rlset = NULL;
    365  1.1  rmind 	nset = NULL;
    366  1.1  rmind fail:
    367  1.1  rmind 	prop_object_release(dict);
    368  1.1  rmind 	/*
    369  1.1  rmind 	 * Note: destroy rulesets first, to drop references to the tableset.
    370  1.1  rmind 	 */
    371  1.1  rmind 	KASSERT(error == 0 || (nset || rlset || tblset));
    372  1.1  rmind 	if (nset) {
    373  1.1  rmind 		npf_ruleset_destroy(nset);
    374  1.1  rmind 	}
    375  1.1  rmind 	if (rlset) {
    376  1.1  rmind 		npf_ruleset_destroy(rlset);
    377  1.1  rmind 	}
    378  1.1  rmind 	if (tblset) {
    379  1.1  rmind 		npf_tableset_destroy(tblset);
    380  1.1  rmind 	}
    381  1.1  rmind 	return error;
    382  1.1  rmind }
    383  1.1  rmind 
    384  1.1  rmind /*
    385  1.4  rmind  * npfctl_sessions_save: construct a list of sessions and export for saving.
    386  1.4  rmind  */
    387  1.4  rmind int
    388  1.4  rmind npfctl_sessions_save(u_long cmd, void *data)
    389  1.4  rmind {
    390  1.4  rmind 	struct plistref *pref = data;
    391  1.4  rmind 	prop_dictionary_t sesdict;
    392  1.4  rmind 	prop_array_t selist, nplist;
    393  1.4  rmind 	int error;
    394  1.4  rmind 
    395  1.4  rmind 	/* Create a dictionary and two lists. */
    396  1.4  rmind 	sesdict = prop_dictionary_create();
    397  1.4  rmind 	selist = prop_array_create();
    398  1.4  rmind 	nplist = prop_array_create();
    399  1.4  rmind 
    400  1.4  rmind 	/* Save the sessions. */
    401  1.4  rmind 	error = npf_session_save(selist, nplist);
    402  1.4  rmind 	if (error) {
    403  1.4  rmind 		goto fail;
    404  1.4  rmind 	}
    405  1.4  rmind 
    406  1.4  rmind 	/* Set the session list, NAT policy list and export the dictionary. */
    407  1.4  rmind 	prop_dictionary_set(sesdict, "session-list", selist);
    408  1.4  rmind 	prop_dictionary_set(sesdict, "nat-policy-list", nplist);
    409  1.4  rmind #ifdef _KERNEL
    410  1.4  rmind 	error = prop_dictionary_copyout_ioctl(pref, cmd, sesdict);
    411  1.4  rmind #else
    412  1.4  rmind 	error = prop_dictionary_externalize_to_file(sesdict, data) ? 0 : errno;
    413  1.4  rmind #endif
    414  1.4  rmind fail:
    415  1.4  rmind 	prop_object_release(sesdict);
    416  1.4  rmind 	return error;
    417  1.4  rmind }
    418  1.4  rmind 
    419  1.4  rmind /*
    420  1.4  rmind  * npfctl_sessions_load: import a list of sessions, reconstruct them and load.
    421  1.4  rmind  */
    422  1.4  rmind int
    423  1.4  rmind npfctl_sessions_load(u_long cmd, void *data)
    424  1.4  rmind {
    425  1.4  rmind 	const struct plistref *pref = data;
    426  1.4  rmind 	npf_sehash_t *sehasht = NULL;
    427  1.4  rmind 	prop_dictionary_t sesdict, sedict;
    428  1.4  rmind 	prop_object_iterator_t it;
    429  1.4  rmind 	prop_array_t selist;
    430  1.4  rmind 	int error;
    431  1.4  rmind 
    432  1.4  rmind 	/* Retrieve the dictionary containing session and NAT policy lists. */
    433  1.4  rmind #ifdef _KERNEL
    434  1.4  rmind 	error = prop_dictionary_copyin_ioctl(pref, cmd, &sesdict);
    435  1.4  rmind 	if (error)
    436  1.4  rmind 		return error;
    437  1.4  rmind #else
    438  1.4  rmind 	sesdict = prop_dictionary_internalize_from_file(data);
    439  1.4  rmind 	if (sesdict == NULL)
    440  1.4  rmind 		return EINVAL;
    441  1.4  rmind #endif
    442  1.4  rmind 	/*
    443  1.4  rmind 	 * Note: session objects contain the references to the NAT policy
    444  1.4  rmind 	 * entries.  Therefore, no need to directly access it.
    445  1.4  rmind 	 */
    446  1.4  rmind 	selist = prop_dictionary_get(sesdict, "session-list");
    447  1.4  rmind 	if (prop_object_type(selist) != PROP_TYPE_ARRAY) {
    448  1.4  rmind 		error = EINVAL;
    449  1.4  rmind 		goto fail;
    450  1.4  rmind 	}
    451  1.4  rmind 
    452  1.4  rmind 	/* Create a session hash table. */
    453  1.4  rmind 	sehasht = sess_htable_create();
    454  1.4  rmind 	if (sehasht == NULL) {
    455  1.4  rmind 		error = ENOMEM;
    456  1.4  rmind 		goto fail;
    457  1.4  rmind 	}
    458  1.4  rmind 
    459  1.4  rmind 	/*
    460  1.4  rmind 	 * Iterate through and construct each session.
    461  1.4  rmind 	 */
    462  1.4  rmind 	error = 0;
    463  1.4  rmind 	it = prop_array_iterator(selist);
    464  1.4  rmind 	npf_core_enter();
    465  1.4  rmind 	while ((sedict = prop_object_iterator_next(it)) != NULL) {
    466  1.4  rmind 		/* Session - dictionary. */
    467  1.4  rmind 		if (prop_object_type(sedict) != PROP_TYPE_DICTIONARY) {
    468  1.4  rmind 			error = EINVAL;
    469  1.4  rmind 			goto fail;
    470  1.4  rmind 		}
    471  1.4  rmind 		/* Construct and insert real session structure. */
    472  1.4  rmind 		error = npf_session_restore(sehasht, sedict);
    473  1.4  rmind 		if (error) {
    474  1.4  rmind 			goto fail;
    475  1.4  rmind 		}
    476  1.4  rmind 	}
    477  1.4  rmind 	npf_core_exit();
    478  1.4  rmind 	sess_htable_reload(sehasht);
    479  1.4  rmind fail:
    480  1.4  rmind 	prop_object_release(selist);
    481  1.4  rmind 	if (error && sehasht) {
    482  1.4  rmind 		/* Destroy session table. */
    483  1.4  rmind 		sess_htable_destroy(sehasht);
    484  1.4  rmind 	}
    485  1.4  rmind 	return error;
    486  1.4  rmind }
    487  1.4  rmind 
    488  1.4  rmind /*
    489  1.2  rmind  * npfctl_table: add, remove or query entries in the specified table.
    490  1.1  rmind  *
    491  1.1  rmind  * For maximum performance, interface is avoiding proplib(3)'s overhead.
    492  1.1  rmind  */
    493  1.1  rmind int
    494  1.1  rmind npfctl_table(void *data)
    495  1.1  rmind {
    496  1.1  rmind 	npf_ioctl_table_t *nct = data;
    497  1.1  rmind 	int error;
    498  1.1  rmind 
    499  1.1  rmind 	switch (nct->nct_action) {
    500  1.1  rmind 	case NPF_IOCTL_TBLENT_ADD:
    501  1.1  rmind 		error = npf_table_add_v4cidr(NULL, nct->nct_tid,
    502  1.1  rmind 		    nct->nct_addr, nct->nct_mask);
    503  1.1  rmind 		break;
    504  1.1  rmind 	case NPF_IOCTL_TBLENT_REM:
    505  1.1  rmind 		error = npf_table_rem_v4cidr(NULL, nct->nct_tid,
    506  1.1  rmind 		    nct->nct_addr, nct->nct_mask);
    507  1.1  rmind 		break;
    508  1.1  rmind 	default:
    509  1.1  rmind 		/* XXX */
    510  1.1  rmind 		error = npf_table_match_v4addr(nct->nct_tid, nct->nct_addr);
    511  1.1  rmind 		if (error) {
    512  1.1  rmind 			error = EINVAL;
    513  1.1  rmind 		}
    514  1.1  rmind 	}
    515  1.1  rmind 	return error;
    516  1.1  rmind }
    517