Home | History | Annotate | Line # | Download | only in libnpf
npf.c revision 1.19
      1  1.19  christos /*	$NetBSD: npf.c,v 1.19 2013/03/20 00:29:46 christos Exp $	*/
      2   1.1     rmind 
      3   1.1     rmind /*-
      4  1.16     rmind  * Copyright (c) 2010-2013 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 #include <sys/cdefs.h>
     33  1.19  christos __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.19 2013/03/20 00:29:46 christos Exp $");
     34   1.1     rmind 
     35   1.1     rmind #include <sys/types.h>
     36   1.1     rmind #include <netinet/in_systm.h>
     37   1.1     rmind #include <netinet/in.h>
     38  1.11     rmind #include <net/if.h>
     39   1.1     rmind #include <prop/proplib.h>
     40   1.1     rmind 
     41   1.1     rmind #include <stdlib.h>
     42   1.1     rmind #include <string.h>
     43   1.7     rmind #include <assert.h>
     44   1.1     rmind #include <errno.h>
     45   1.1     rmind #include <err.h>
     46   1.1     rmind 
     47   1.1     rmind #define	_NPF_PRIVATE
     48   1.1     rmind #include "npf.h"
     49   1.1     rmind 
     50   1.1     rmind struct nl_config {
     51   1.1     rmind 	/* Rules, translations, tables, procedures. */
     52   1.8     rmind 	prop_dictionary_t	ncf_dict;
     53  1.19  christos 	prop_array_t		ncf_alg_list;
     54   1.1     rmind 	prop_array_t		ncf_rules_list;
     55   1.1     rmind 	prop_array_t		ncf_rproc_list;
     56   1.1     rmind 	prop_array_t		ncf_table_list;
     57   1.1     rmind 	prop_array_t		ncf_nat_list;
     58  1.11     rmind 	/* Debug information. */
     59  1.11     rmind 	prop_dictionary_t	ncf_debug;
     60   1.7     rmind 	/* Error report. */
     61   1.7     rmind 	prop_dictionary_t	ncf_err;
     62   1.4     rmind 	/* Custom file to externalise property-list. */
     63   1.4     rmind 	const char *		ncf_plist;
     64   1.6     rmind 	bool			ncf_flush;
     65   1.1     rmind };
     66   1.1     rmind 
     67   1.1     rmind struct nl_rule {
     68   1.1     rmind 	prop_dictionary_t	nrl_dict;
     69   1.1     rmind };
     70   1.1     rmind 
     71   1.1     rmind struct nl_rproc {
     72   1.1     rmind 	prop_dictionary_t	nrp_dict;
     73   1.1     rmind };
     74   1.1     rmind 
     75   1.1     rmind struct nl_table {
     76   1.1     rmind 	prop_dictionary_t	ntl_dict;
     77   1.1     rmind };
     78   1.1     rmind 
     79  1.19  christos struct nl_alg {
     80  1.19  christos 	prop_dictionary_t	nal_dict;
     81  1.19  christos };
     82  1.19  christos 
     83  1.13     rmind struct nl_ext {
     84  1.13     rmind 	const char *		nxt_name;
     85  1.13     rmind 	prop_dictionary_t	nxt_dict;
     86  1.13     rmind };
     87  1.13     rmind 
     88  1.16     rmind static prop_array_t	_npf_ruleset_transform(prop_array_t);
     89  1.16     rmind 
     90   1.1     rmind /*
     91   1.1     rmind  * CONFIGURATION INTERFACE.
     92   1.1     rmind  */
     93   1.1     rmind 
     94   1.1     rmind nl_config_t *
     95   1.1     rmind npf_config_create(void)
     96   1.1     rmind {
     97   1.1     rmind 	nl_config_t *ncf;
     98   1.1     rmind 
     99   1.7     rmind 	ncf = calloc(1, sizeof(*ncf));
    100   1.1     rmind 	if (ncf == NULL) {
    101   1.1     rmind 		return NULL;
    102   1.1     rmind 	}
    103  1.19  christos 	ncf->ncf_alg_list = prop_array_create();
    104   1.1     rmind 	ncf->ncf_rules_list = prop_array_create();
    105   1.1     rmind 	ncf->ncf_rproc_list = prop_array_create();
    106   1.1     rmind 	ncf->ncf_table_list = prop_array_create();
    107   1.1     rmind 	ncf->ncf_nat_list = prop_array_create();
    108   1.1     rmind 
    109   1.4     rmind 	ncf->ncf_plist = NULL;
    110   1.6     rmind 	ncf->ncf_flush = false;
    111   1.4     rmind 
    112   1.1     rmind 	return ncf;
    113   1.1     rmind }
    114   1.1     rmind 
    115   1.1     rmind int
    116   1.1     rmind npf_config_submit(nl_config_t *ncf, int fd)
    117   1.1     rmind {
    118   1.7     rmind 	const char *plist = ncf->ncf_plist;
    119   1.1     rmind 	prop_dictionary_t npf_dict;
    120  1.16     rmind 	prop_array_t rlset;
    121   1.1     rmind 	int error = 0;
    122   1.1     rmind 
    123   1.1     rmind 	npf_dict = prop_dictionary_create();
    124   1.1     rmind 	if (npf_dict == NULL) {
    125   1.1     rmind 		return ENOMEM;
    126   1.1     rmind 	}
    127  1.15     rmind 	prop_dictionary_set_uint32(npf_dict, "version", NPF_VERSION);
    128  1.16     rmind 
    129  1.16     rmind 	rlset = _npf_ruleset_transform(ncf->ncf_rules_list);
    130  1.16     rmind 	if (rlset == NULL) {
    131  1.16     rmind 		prop_object_release(npf_dict);
    132  1.16     rmind 		return ENOMEM;
    133  1.16     rmind 	}
    134  1.16     rmind 	prop_dictionary_set(npf_dict, "rules", rlset);
    135  1.16     rmind 	prop_object_release(rlset);
    136  1.16     rmind 
    137  1.19  christos 	prop_dictionary_set(npf_dict, "algs", ncf->ncf_alg_list);
    138   1.1     rmind 	prop_dictionary_set(npf_dict, "rprocs", ncf->ncf_rproc_list);
    139   1.1     rmind 	prop_dictionary_set(npf_dict, "tables", ncf->ncf_table_list);
    140   1.1     rmind 	prop_dictionary_set(npf_dict, "translation", ncf->ncf_nat_list);
    141   1.6     rmind 	prop_dictionary_set_bool(npf_dict, "flush", ncf->ncf_flush);
    142  1.15     rmind 	if (ncf->ncf_debug) {
    143  1.15     rmind 		prop_dictionary_set(npf_dict, "debug", ncf->ncf_debug);
    144  1.15     rmind 	}
    145   1.1     rmind 
    146   1.4     rmind 	if (plist) {
    147   1.4     rmind 		if (!prop_dictionary_externalize_to_file(npf_dict, plist)) {
    148   1.4     rmind 			error = errno;
    149   1.4     rmind 		}
    150   1.7     rmind 		prop_object_release(npf_dict);
    151   1.7     rmind 		return error;
    152   1.7     rmind 	}
    153   1.7     rmind 
    154   1.7     rmind 	error = prop_dictionary_sendrecv_ioctl(npf_dict, fd,
    155   1.7     rmind 	    IOC_NPF_RELOAD, &ncf->ncf_err);
    156   1.7     rmind 	if (error) {
    157   1.7     rmind 		prop_object_release(npf_dict);
    158   1.7     rmind 		assert(ncf->ncf_err == NULL);
    159   1.7     rmind 		return error;
    160   1.1     rmind 	}
    161   1.7     rmind 
    162   1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err, "errno", &error);
    163   1.1     rmind 	prop_object_release(npf_dict);
    164   1.1     rmind 	return error;
    165   1.1     rmind }
    166   1.1     rmind 
    167   1.8     rmind nl_config_t *
    168   1.8     rmind npf_config_retrieve(int fd, bool *active, bool *loaded)
    169   1.8     rmind {
    170   1.8     rmind 	prop_dictionary_t npf_dict;
    171   1.8     rmind 	nl_config_t *ncf;
    172   1.8     rmind 	int error;
    173   1.8     rmind 
    174   1.8     rmind 	error = prop_dictionary_recv_ioctl(fd, IOC_NPF_GETCONF, &npf_dict);
    175   1.8     rmind 	if (error) {
    176   1.8     rmind 		return NULL;
    177   1.8     rmind 	}
    178   1.8     rmind 	ncf = calloc(1, sizeof(*ncf));
    179   1.8     rmind 	if (ncf == NULL) {
    180   1.8     rmind 		prop_object_release(npf_dict);
    181   1.8     rmind 		return NULL;
    182   1.8     rmind 	}
    183   1.8     rmind 	ncf->ncf_dict = npf_dict;
    184  1.19  christos 	ncf->ncf_alg_list = prop_dictionary_get(npf_dict, "algs");
    185   1.8     rmind 	ncf->ncf_rules_list = prop_dictionary_get(npf_dict, "rules");
    186   1.8     rmind 	ncf->ncf_rproc_list = prop_dictionary_get(npf_dict, "rprocs");
    187   1.8     rmind 	ncf->ncf_table_list = prop_dictionary_get(npf_dict, "tables");
    188   1.8     rmind 	ncf->ncf_nat_list = prop_dictionary_get(npf_dict, "translation");
    189   1.8     rmind 
    190   1.8     rmind 	prop_dictionary_get_bool(npf_dict, "active", active);
    191   1.8     rmind 	*loaded = (ncf->ncf_rules_list != NULL);
    192   1.8     rmind 	return ncf;
    193   1.8     rmind }
    194   1.8     rmind 
    195   1.6     rmind int
    196   1.6     rmind npf_config_flush(int fd)
    197   1.6     rmind {
    198   1.6     rmind 	nl_config_t *ncf;
    199   1.6     rmind 	int error;
    200   1.6     rmind 
    201   1.6     rmind 	ncf = npf_config_create();
    202   1.6     rmind 	if (ncf == NULL) {
    203   1.6     rmind 		return ENOMEM;
    204   1.6     rmind 	}
    205   1.6     rmind 	ncf->ncf_flush = true;
    206   1.6     rmind 	error = npf_config_submit(ncf, fd);
    207   1.6     rmind 	npf_config_destroy(ncf);
    208   1.6     rmind 	return error;
    209   1.6     rmind }
    210   1.6     rmind 
    211   1.1     rmind void
    212   1.7     rmind _npf_config_error(nl_config_t *ncf, nl_error_t *ne)
    213   1.7     rmind {
    214   1.7     rmind 	memset(ne, 0, sizeof(*ne));
    215   1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err, "id", &ne->ne_id);
    216   1.7     rmind 	prop_dictionary_get_cstring(ncf->ncf_err,
    217   1.7     rmind 	    "source-file", &ne->ne_source_file);
    218   1.7     rmind 	prop_dictionary_get_uint32(ncf->ncf_err,
    219   1.7     rmind 	    "source-line", &ne->ne_source_line);
    220   1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err,
    221  1.16     rmind 	    "code-error", &ne->ne_ncode_error);
    222   1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err,
    223  1.16     rmind 	    "code-errat", &ne->ne_ncode_errat);
    224   1.7     rmind }
    225   1.7     rmind 
    226   1.7     rmind void
    227   1.1     rmind npf_config_destroy(nl_config_t *ncf)
    228   1.1     rmind {
    229   1.1     rmind 
    230  1.14     rmind 	if (!ncf->ncf_dict) {
    231  1.19  christos 		prop_object_release(ncf->ncf_alg_list);
    232   1.8     rmind 		prop_object_release(ncf->ncf_rules_list);
    233   1.8     rmind 		prop_object_release(ncf->ncf_rproc_list);
    234   1.8     rmind 		prop_object_release(ncf->ncf_table_list);
    235   1.8     rmind 		prop_object_release(ncf->ncf_nat_list);
    236   1.8     rmind 	}
    237   1.7     rmind 	if (ncf->ncf_err) {
    238   1.7     rmind 		prop_object_release(ncf->ncf_err);
    239   1.7     rmind 	}
    240  1.11     rmind 	if (ncf->ncf_debug) {
    241  1.11     rmind 		prop_object_release(ncf->ncf_debug);
    242  1.11     rmind 	}
    243   1.1     rmind 	free(ncf);
    244   1.1     rmind }
    245   1.1     rmind 
    246   1.4     rmind void
    247   1.4     rmind _npf_config_setsubmit(nl_config_t *ncf, const char *plist_file)
    248   1.4     rmind {
    249   1.4     rmind 
    250   1.4     rmind 	ncf->ncf_plist = plist_file;
    251   1.4     rmind }
    252   1.4     rmind 
    253   1.1     rmind static bool
    254   1.1     rmind _npf_prop_array_lookup(prop_array_t array, const char *key, const char *name)
    255   1.1     rmind {
    256   1.1     rmind 	prop_dictionary_t dict;
    257   1.1     rmind 	prop_object_iterator_t it;
    258   1.1     rmind 
    259   1.1     rmind 	it = prop_array_iterator(array);
    260   1.1     rmind 	while ((dict = prop_object_iterator_next(it)) != NULL) {
    261   1.1     rmind 		const char *lname;
    262   1.1     rmind 		prop_dictionary_get_cstring_nocopy(dict, key, &lname);
    263   1.1     rmind 		if (strcmp(name, lname) == 0)
    264   1.1     rmind 			break;
    265   1.1     rmind 	}
    266   1.1     rmind 	prop_object_iterator_release(it);
    267   1.1     rmind 	return dict ? true : false;
    268   1.1     rmind }
    269   1.1     rmind 
    270   1.1     rmind /*
    271  1.16     rmind  * DYNAMIC RULESET INTERFACE.
    272  1.16     rmind  */
    273  1.16     rmind 
    274  1.16     rmind int
    275  1.18     rmind npf_ruleset_add(int fd, const char *rname, nl_rule_t *rl, uint64_t *id)
    276  1.16     rmind {
    277  1.16     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    278  1.16     rmind 	prop_dictionary_t ret;
    279  1.16     rmind 	int error;
    280  1.16     rmind 
    281  1.16     rmind 	prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
    282  1.16     rmind 	prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_ADD);
    283  1.16     rmind 	error = prop_dictionary_sendrecv_ioctl(rldict, fd, IOC_NPF_RULE, &ret);
    284  1.16     rmind 	if (!error) {
    285  1.18     rmind 		prop_dictionary_get_uint64(ret, "id", id);
    286  1.16     rmind 	}
    287  1.16     rmind 	return error;
    288  1.16     rmind }
    289  1.16     rmind 
    290  1.16     rmind int
    291  1.18     rmind npf_ruleset_remove(int fd, const char *rname, uint64_t id)
    292  1.16     rmind {
    293  1.16     rmind 	prop_dictionary_t rldict;
    294  1.16     rmind 
    295  1.16     rmind 	rldict = prop_dictionary_create();
    296  1.16     rmind 	if (rldict == NULL) {
    297  1.16     rmind 		return ENOMEM;
    298  1.16     rmind 	}
    299  1.16     rmind 	prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
    300  1.16     rmind 	prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_REMOVE);
    301  1.18     rmind 	prop_dictionary_set_uint64(rldict, "id", id);
    302  1.16     rmind 	return prop_dictionary_send_ioctl(rldict, fd, IOC_NPF_RULE);
    303  1.16     rmind }
    304  1.16     rmind 
    305  1.16     rmind int
    306  1.16     rmind npf_ruleset_remkey(int fd, const char *rname, const void *key, size_t len)
    307  1.16     rmind {
    308  1.16     rmind 	prop_dictionary_t rldict;
    309  1.16     rmind 	prop_data_t keyobj;
    310  1.16     rmind 
    311  1.16     rmind 	rldict = prop_dictionary_create();
    312  1.16     rmind 	if (rldict == NULL) {
    313  1.16     rmind 		return ENOMEM;
    314  1.16     rmind 	}
    315  1.16     rmind 	prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
    316  1.16     rmind 	prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_REMKEY);
    317  1.16     rmind 
    318  1.16     rmind 	keyobj = prop_data_create_data(key, len);
    319  1.16     rmind 	if (keyobj == NULL) {
    320  1.16     rmind 		prop_object_release(rldict);
    321  1.16     rmind 		return ENOMEM;
    322  1.16     rmind 	}
    323  1.16     rmind 	prop_dictionary_set(rldict, "key", keyobj);
    324  1.16     rmind 	prop_object_release(keyobj);
    325  1.16     rmind 
    326  1.16     rmind 	return prop_dictionary_send_ioctl(rldict, fd, IOC_NPF_RULE);
    327  1.16     rmind }
    328  1.16     rmind 
    329  1.17     rmind int
    330  1.17     rmind npf_ruleset_flush(int fd, const char *rname)
    331  1.17     rmind {
    332  1.17     rmind 	prop_dictionary_t rldict;
    333  1.17     rmind 
    334  1.17     rmind 	rldict = prop_dictionary_create();
    335  1.17     rmind 	if (rldict == NULL) {
    336  1.17     rmind 		return ENOMEM;
    337  1.17     rmind 	}
    338  1.17     rmind 	prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
    339  1.17     rmind 	prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_FLUSH);
    340  1.17     rmind 	return prop_dictionary_send_ioctl(rldict, fd, IOC_NPF_RULE);
    341  1.17     rmind }
    342  1.17     rmind 
    343  1.16     rmind /*
    344  1.16     rmind  * _npf_ruleset_transform: transform the ruleset representing nested
    345  1.16     rmind  * rules with lists into an array.
    346  1.16     rmind  */
    347  1.16     rmind 
    348  1.16     rmind static void
    349  1.16     rmind _npf_ruleset_transform1(prop_array_t rlset, prop_array_t rules)
    350  1.16     rmind {
    351  1.16     rmind 	prop_object_iterator_t it;
    352  1.16     rmind 	prop_dictionary_t rldict;
    353  1.16     rmind 	prop_array_t subrlset;
    354  1.16     rmind 
    355  1.16     rmind 	it = prop_array_iterator(rules);
    356  1.16     rmind 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    357  1.16     rmind 		unsigned idx;
    358  1.16     rmind 
    359  1.16     rmind 		/* Add rules to the array (reference is retained). */
    360  1.16     rmind 		prop_array_add(rlset, rldict);
    361  1.16     rmind 
    362  1.16     rmind 		subrlset = prop_dictionary_get(rldict, "subrules");
    363  1.16     rmind 		if (subrlset) {
    364  1.16     rmind 			/* Process subrules recursively. */
    365  1.16     rmind 			_npf_ruleset_transform1(rlset, subrlset);
    366  1.16     rmind 			/* Add the skip-to position. */
    367  1.16     rmind 			idx = prop_array_count(rlset);
    368  1.16     rmind 			prop_dictionary_set_uint32(rldict, "skip-to", idx);
    369  1.16     rmind 			prop_dictionary_remove(rldict, "subrules");
    370  1.16     rmind 		}
    371  1.16     rmind 	}
    372  1.16     rmind 	prop_object_iterator_release(it);
    373  1.16     rmind }
    374  1.16     rmind 
    375  1.16     rmind static prop_array_t
    376  1.16     rmind _npf_ruleset_transform(prop_array_t rlset)
    377  1.16     rmind {
    378  1.16     rmind 	prop_array_t nrlset;
    379  1.16     rmind 
    380  1.16     rmind 	nrlset = prop_array_create();
    381  1.16     rmind 	_npf_ruleset_transform1(nrlset, rlset);
    382  1.16     rmind 	return nrlset;
    383  1.16     rmind }
    384  1.16     rmind 
    385  1.16     rmind /*
    386  1.13     rmind  * NPF EXTENSION INTERFACE.
    387  1.13     rmind  */
    388  1.13     rmind 
    389  1.13     rmind nl_ext_t *
    390  1.13     rmind npf_ext_construct(const char *name)
    391  1.13     rmind {
    392  1.13     rmind 	nl_ext_t *ext;
    393  1.13     rmind 
    394  1.13     rmind 	ext = malloc(sizeof(*ext));
    395  1.13     rmind 	if (ext == NULL) {
    396  1.13     rmind 		return NULL;
    397  1.13     rmind 	}
    398  1.13     rmind 	ext->nxt_name = strdup(name);
    399  1.13     rmind 	if (ext->nxt_name == NULL) {
    400  1.13     rmind 		free(ext);
    401  1.13     rmind 		return NULL;
    402  1.13     rmind 	}
    403  1.13     rmind 	ext->nxt_dict = prop_dictionary_create();
    404  1.13     rmind 
    405  1.13     rmind 	return ext;
    406  1.13     rmind }
    407  1.13     rmind 
    408  1.13     rmind void
    409  1.13     rmind npf_ext_param_u32(nl_ext_t *ext, const char *key, uint32_t val)
    410  1.13     rmind {
    411  1.13     rmind 	prop_dictionary_t extdict = ext->nxt_dict;
    412  1.13     rmind 	prop_dictionary_set_uint32(extdict, key, val);
    413  1.13     rmind }
    414  1.13     rmind 
    415  1.13     rmind void
    416  1.13     rmind npf_ext_param_bool(nl_ext_t *ext, const char *key, bool val)
    417  1.13     rmind {
    418  1.13     rmind 	prop_dictionary_t extdict = ext->nxt_dict;
    419  1.13     rmind 	prop_dictionary_set_bool(extdict, key, val);
    420  1.13     rmind }
    421  1.13     rmind 
    422  1.13     rmind /*
    423   1.1     rmind  * RULE INTERFACE.
    424   1.1     rmind  */
    425   1.1     rmind 
    426   1.1     rmind nl_rule_t *
    427   1.1     rmind npf_rule_create(const char *name, uint32_t attr, u_int if_idx)
    428   1.1     rmind {
    429   1.1     rmind 	prop_dictionary_t rldict;
    430   1.1     rmind 	nl_rule_t *rl;
    431   1.1     rmind 
    432   1.5  christos 	rl = malloc(sizeof(*rl));
    433   1.1     rmind 	if (rl == NULL) {
    434   1.1     rmind 		return NULL;
    435   1.1     rmind 	}
    436   1.1     rmind 	rldict = prop_dictionary_create();
    437   1.1     rmind 	if (rldict == NULL) {
    438   1.1     rmind 		free(rl);
    439   1.1     rmind 		return NULL;
    440   1.1     rmind 	}
    441   1.1     rmind 	if (name) {
    442   1.1     rmind 		prop_dictionary_set_cstring(rldict, "name", name);
    443   1.1     rmind 	}
    444   1.1     rmind 	prop_dictionary_set_uint32(rldict, "attributes", attr);
    445   1.1     rmind 
    446   1.1     rmind 	if (if_idx) {
    447   1.1     rmind 		prop_dictionary_set_uint32(rldict, "interface", if_idx);
    448   1.1     rmind 	}
    449   1.1     rmind 	rl->nrl_dict = rldict;
    450   1.1     rmind 	return rl;
    451   1.1     rmind }
    452   1.1     rmind 
    453   1.1     rmind int
    454  1.16     rmind npf_rule_setcode(nl_rule_t *rl, int type, const void *code, size_t len)
    455   1.1     rmind {
    456   1.1     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    457   1.1     rmind 	prop_data_t cdata;
    458   1.1     rmind 
    459  1.16     rmind 	switch (type) {
    460  1.16     rmind 	case NPF_CODE_NC:
    461  1.16     rmind 	case NPF_CODE_BPF:
    462  1.16     rmind 		break;
    463  1.16     rmind 	default:
    464   1.1     rmind 		return ENOTSUP;
    465   1.1     rmind 	}
    466  1.16     rmind 	prop_dictionary_set_uint32(rldict, "code-type", type);
    467  1.16     rmind 	if ((cdata = prop_data_create_data(code, len)) == NULL) {
    468   1.1     rmind 		return ENOMEM;
    469   1.1     rmind 	}
    470  1.16     rmind 	prop_dictionary_set(rldict, "code", cdata);
    471   1.1     rmind 	prop_object_release(cdata);
    472   1.1     rmind 	return 0;
    473   1.1     rmind }
    474   1.1     rmind 
    475   1.1     rmind int
    476  1.16     rmind npf_rule_setkey(nl_rule_t *rl, const void *key, size_t len)
    477   1.1     rmind {
    478   1.1     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    479  1.16     rmind 	prop_data_t kdata;
    480   1.1     rmind 
    481  1.16     rmind 	if ((kdata = prop_data_create_data(key, len)) == NULL) {
    482  1.16     rmind 		return ENOMEM;
    483   1.1     rmind 	}
    484  1.16     rmind 	prop_dictionary_set(rldict, "key", kdata);
    485  1.16     rmind 	prop_object_release(kdata);
    486  1.16     rmind 	return 0;
    487  1.16     rmind }
    488  1.16     rmind 
    489  1.16     rmind int
    490  1.16     rmind npf_rule_setprio(nl_rule_t *rl, pri_t pri)
    491  1.16     rmind {
    492  1.16     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    493  1.16     rmind 
    494  1.16     rmind 	prop_dictionary_set_int32(rldict, "priority", pri);
    495  1.16     rmind 	return 0;
    496  1.16     rmind }
    497  1.16     rmind 
    498  1.16     rmind int
    499  1.16     rmind npf_rule_setproc(nl_rule_t *rl, const char *name)
    500  1.16     rmind {
    501  1.16     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    502  1.16     rmind 
    503   1.1     rmind 	prop_dictionary_set_cstring(rldict, "rproc", name);
    504   1.1     rmind 	return 0;
    505   1.1     rmind }
    506   1.1     rmind 
    507  1.16     rmind void *
    508  1.16     rmind npf_rule_export(nl_rule_t *rl, size_t *length)
    509  1.16     rmind {
    510  1.16     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    511  1.16     rmind 	void *xml;
    512  1.16     rmind 
    513  1.16     rmind 	if ((xml = prop_dictionary_externalize(rldict)) == NULL) {
    514  1.16     rmind 		return NULL;
    515  1.16     rmind 	}
    516  1.16     rmind 	*length = strlen(xml);
    517  1.16     rmind 	return xml;
    518  1.16     rmind }
    519  1.16     rmind 
    520   1.1     rmind bool
    521   1.1     rmind npf_rule_exists_p(nl_config_t *ncf, const char *name)
    522   1.1     rmind {
    523   1.1     rmind 	return _npf_prop_array_lookup(ncf->ncf_rules_list, "name", name);
    524   1.1     rmind }
    525   1.1     rmind 
    526   1.1     rmind int
    527  1.16     rmind npf_rule_insert(nl_config_t *ncf, nl_rule_t *parent, nl_rule_t *rl)
    528   1.1     rmind {
    529   1.1     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    530   1.1     rmind 	prop_array_t rlset;
    531   1.1     rmind 
    532   1.1     rmind 	if (parent) {
    533   1.1     rmind 		prop_dictionary_t pdict = parent->nrl_dict;
    534   1.1     rmind 		rlset = prop_dictionary_get(pdict, "subrules");
    535   1.1     rmind 		if (rlset == NULL) {
    536   1.1     rmind 			rlset = prop_array_create();
    537   1.1     rmind 			prop_dictionary_set(pdict, "subrules", rlset);
    538   1.1     rmind 			prop_object_release(rlset);
    539   1.1     rmind 		}
    540   1.1     rmind 	} else {
    541   1.1     rmind 		rlset = ncf->ncf_rules_list;
    542   1.1     rmind 	}
    543   1.1     rmind 	prop_array_add(rlset, rldict);
    544   1.1     rmind 	return 0;
    545   1.1     rmind }
    546   1.1     rmind 
    547   1.8     rmind static int
    548  1.16     rmind _npf_rule_foreach1(prop_array_t rules, nl_rule_callback_t func)
    549   1.8     rmind {
    550   1.8     rmind 	prop_dictionary_t rldict;
    551   1.8     rmind 	prop_object_iterator_t it;
    552  1.16     rmind 	unsigned reduce[16], n;
    553  1.16     rmind 	unsigned nlevel;
    554   1.8     rmind 
    555   1.8     rmind 	if (!rules || prop_object_type(rules) != PROP_TYPE_ARRAY) {
    556   1.8     rmind 		return ENOENT;
    557   1.8     rmind 	}
    558   1.8     rmind 	it = prop_array_iterator(rules);
    559   1.8     rmind 	if (it == NULL) {
    560   1.8     rmind 		return ENOMEM;
    561   1.8     rmind 	}
    562  1.16     rmind 
    563  1.16     rmind 	nlevel = 0;
    564  1.16     rmind 	reduce[nlevel] = 0;
    565  1.16     rmind 	n = 0;
    566  1.16     rmind 
    567   1.8     rmind 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    568  1.16     rmind 		nl_rule_t nrl = { .nrl_dict = rldict };
    569  1.16     rmind 		uint32_t skipto = 0;
    570   1.8     rmind 
    571  1.16     rmind 		prop_dictionary_get_uint32(rldict, "skip-to", &skipto);
    572   1.8     rmind 		(*func)(&nrl, nlevel);
    573  1.16     rmind 		if (skipto) {
    574  1.16     rmind 			nlevel++;
    575  1.16     rmind 			reduce[nlevel] = skipto;
    576  1.16     rmind 		}
    577  1.16     rmind 		if (reduce[nlevel] == ++n) {
    578  1.16     rmind 			assert(nlevel > 0);
    579  1.16     rmind 			nlevel--;
    580  1.14     rmind 		}
    581   1.8     rmind 	}
    582   1.8     rmind 	prop_object_iterator_release(it);
    583   1.8     rmind 	return 0;
    584   1.8     rmind }
    585   1.8     rmind 
    586   1.8     rmind int
    587   1.8     rmind _npf_rule_foreach(nl_config_t *ncf, nl_rule_callback_t func)
    588   1.8     rmind {
    589  1.16     rmind 	return _npf_rule_foreach1(ncf->ncf_rules_list, func);
    590   1.8     rmind }
    591   1.8     rmind 
    592  1.17     rmind int
    593  1.17     rmind _npf_ruleset_list(int fd, const char *rname, nl_config_t *ncf)
    594  1.17     rmind {
    595  1.17     rmind 	prop_dictionary_t rldict, ret;
    596  1.17     rmind 	int error;
    597  1.17     rmind 
    598  1.17     rmind 	rldict = prop_dictionary_create();
    599  1.17     rmind 	if (rldict == NULL) {
    600  1.17     rmind 		return ENOMEM;
    601  1.17     rmind 	}
    602  1.17     rmind 	prop_dictionary_set_cstring(rldict, "ruleset-name", rname);
    603  1.17     rmind 	prop_dictionary_set_uint32(rldict, "command", NPF_CMD_RULE_LIST);
    604  1.17     rmind 	error = prop_dictionary_sendrecv_ioctl(rldict, fd, IOC_NPF_RULE, &ret);
    605  1.17     rmind 	if (!error) {
    606  1.17     rmind 		prop_array_t rules;
    607  1.17     rmind 
    608  1.17     rmind 		rules = prop_dictionary_get(ret, "rules");
    609  1.17     rmind 		if (rules == NULL) {
    610  1.17     rmind 			return EINVAL;
    611  1.17     rmind 		}
    612  1.17     rmind 		prop_object_release(ncf->ncf_rules_list);
    613  1.17     rmind 		ncf->ncf_rules_list = rules;
    614  1.17     rmind 	}
    615  1.17     rmind 	return error;
    616  1.17     rmind }
    617  1.17     rmind 
    618   1.8     rmind pri_t
    619   1.8     rmind _npf_rule_getinfo(nl_rule_t *nrl, const char **rname, uint32_t *attr,
    620   1.8     rmind     u_int *if_idx)
    621   1.8     rmind {
    622   1.8     rmind 	prop_dictionary_t rldict = nrl->nrl_dict;
    623   1.8     rmind 	pri_t prio;
    624   1.8     rmind 
    625   1.8     rmind 	prop_dictionary_get_cstring_nocopy(rldict, "name", rname);
    626   1.8     rmind 	prop_dictionary_get_uint32(rldict, "attributes", attr);
    627   1.8     rmind 	prop_dictionary_get_int32(rldict, "priority", &prio);
    628   1.8     rmind 	prop_dictionary_get_uint32(rldict, "interface", if_idx);
    629   1.8     rmind 	return prio;
    630   1.8     rmind }
    631   1.8     rmind 
    632   1.8     rmind const void *
    633   1.8     rmind _npf_rule_ncode(nl_rule_t *nrl, size_t *size)
    634   1.8     rmind {
    635   1.8     rmind 	prop_dictionary_t rldict = nrl->nrl_dict;
    636  1.16     rmind 	prop_object_t obj = prop_dictionary_get(rldict, "code");
    637   1.8     rmind 	*size = prop_data_size(obj);
    638   1.8     rmind 	return prop_data_data_nocopy(obj);
    639   1.8     rmind }
    640   1.8     rmind 
    641   1.8     rmind const char *
    642   1.8     rmind _npf_rule_rproc(nl_rule_t *nrl)
    643   1.8     rmind {
    644   1.8     rmind 	prop_dictionary_t rldict = nrl->nrl_dict;
    645   1.8     rmind 	const char *rpname = NULL;
    646   1.8     rmind 
    647   1.8     rmind 	prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rpname);
    648   1.8     rmind 	return rpname;
    649   1.8     rmind }
    650   1.8     rmind 
    651   1.1     rmind void
    652   1.1     rmind npf_rule_destroy(nl_rule_t *rl)
    653   1.1     rmind {
    654   1.1     rmind 
    655   1.1     rmind 	prop_object_release(rl->nrl_dict);
    656   1.1     rmind 	free(rl);
    657   1.1     rmind }
    658   1.1     rmind 
    659   1.1     rmind /*
    660   1.1     rmind  * RULE PROCEDURE INTERFACE.
    661   1.1     rmind  */
    662   1.1     rmind 
    663   1.1     rmind nl_rproc_t *
    664   1.1     rmind npf_rproc_create(const char *name)
    665   1.1     rmind {
    666   1.1     rmind 	prop_dictionary_t rpdict;
    667  1.13     rmind 	prop_array_t extcalls;
    668   1.1     rmind 	nl_rproc_t *nrp;
    669   1.1     rmind 
    670   1.1     rmind 	nrp = malloc(sizeof(nl_rproc_t));
    671   1.1     rmind 	if (nrp == NULL) {
    672   1.1     rmind 		return NULL;
    673   1.1     rmind 	}
    674   1.1     rmind 	rpdict = prop_dictionary_create();
    675   1.1     rmind 	if (rpdict == NULL) {
    676   1.1     rmind 		free(nrp);
    677   1.1     rmind 		return NULL;
    678   1.1     rmind 	}
    679   1.1     rmind 	prop_dictionary_set_cstring(rpdict, "name", name);
    680  1.13     rmind 
    681  1.13     rmind 	extcalls = prop_array_create();
    682  1.13     rmind 	if (extcalls == NULL) {
    683  1.13     rmind 		prop_object_release(rpdict);
    684  1.13     rmind 		free(nrp);
    685  1.13     rmind 		return NULL;
    686  1.13     rmind 	}
    687  1.13     rmind 	prop_dictionary_set(rpdict, "extcalls", extcalls);
    688  1.13     rmind 	prop_object_release(extcalls);
    689  1.13     rmind 
    690   1.1     rmind 	nrp->nrp_dict = rpdict;
    691   1.1     rmind 	return nrp;
    692   1.1     rmind }
    693   1.1     rmind 
    694   1.1     rmind int
    695  1.13     rmind npf_rproc_extcall(nl_rproc_t *rp, nl_ext_t *ext)
    696   1.1     rmind {
    697   1.1     rmind 	prop_dictionary_t rpdict = rp->nrp_dict;
    698  1.13     rmind 	prop_dictionary_t extdict = ext->nxt_dict;
    699  1.13     rmind 	prop_array_t extcalls;
    700   1.1     rmind 
    701  1.13     rmind 	extcalls = prop_dictionary_get(rpdict, "extcalls");
    702  1.13     rmind 	if (_npf_prop_array_lookup(extcalls, "name", ext->nxt_name)) {
    703  1.13     rmind 		return EEXIST;
    704  1.13     rmind 	}
    705  1.13     rmind 	prop_dictionary_set_cstring(extdict, "name", ext->nxt_name);
    706  1.13     rmind 	prop_array_add(extcalls, extdict);
    707   1.1     rmind 	return 0;
    708   1.1     rmind }
    709   1.1     rmind 
    710  1.13     rmind bool
    711  1.13     rmind npf_rproc_exists_p(nl_config_t *ncf, const char *name)
    712   1.1     rmind {
    713   1.1     rmind 
    714  1.13     rmind 	return _npf_prop_array_lookup(ncf->ncf_rproc_list, "name", name);
    715   1.1     rmind }
    716   1.1     rmind 
    717   1.1     rmind int
    718   1.1     rmind npf_rproc_insert(nl_config_t *ncf, nl_rproc_t *rp)
    719   1.1     rmind {
    720   1.1     rmind 	prop_dictionary_t rpdict = rp->nrp_dict;
    721   1.1     rmind 	const char *name;
    722   1.1     rmind 
    723   1.1     rmind 	if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
    724   1.1     rmind 		return EINVAL;
    725   1.1     rmind 	}
    726   1.1     rmind 	if (npf_rproc_exists_p(ncf, name)) {
    727   1.1     rmind 		return EEXIST;
    728   1.1     rmind 	}
    729   1.1     rmind 	prop_array_add(ncf->ncf_rproc_list, rpdict);
    730   1.1     rmind 	return 0;
    731   1.1     rmind }
    732   1.1     rmind 
    733   1.1     rmind /*
    734   1.1     rmind  * TRANSLATION INTERFACE.
    735   1.1     rmind  */
    736   1.1     rmind 
    737   1.1     rmind nl_nat_t *
    738   1.5  christos npf_nat_create(int type, u_int flags, u_int if_idx,
    739   1.1     rmind     npf_addr_t *addr, int af, in_port_t port)
    740   1.1     rmind {
    741   1.1     rmind 	nl_rule_t *rl;
    742   1.1     rmind 	prop_dictionary_t rldict;
    743   1.1     rmind 	prop_data_t addrdat;
    744   1.1     rmind 	uint32_t attr;
    745   1.1     rmind 	size_t sz;
    746   1.1     rmind 
    747   1.1     rmind 	if (af == AF_INET) {
    748   1.1     rmind 		sz = sizeof(struct in_addr);
    749   1.1     rmind 	} else if (af == AF_INET6) {
    750   1.1     rmind 		sz = sizeof(struct in6_addr);
    751   1.1     rmind 	} else {
    752   1.1     rmind 		return NULL;
    753   1.1     rmind 	}
    754   1.1     rmind 
    755   1.1     rmind 	attr = NPF_RULE_PASS | NPF_RULE_FINAL |
    756   1.2     rmind 	    (type == NPF_NATOUT ? NPF_RULE_OUT : NPF_RULE_IN);
    757   1.1     rmind 
    758   1.1     rmind 	/* Create a rule for NAT policy.  Next, will add translation data. */
    759   1.1     rmind 	rl = npf_rule_create(NULL, attr, if_idx);
    760   1.1     rmind 	if (rl == NULL) {
    761   1.1     rmind 		return NULL;
    762   1.1     rmind 	}
    763   1.1     rmind 	rldict = rl->nrl_dict;
    764   1.1     rmind 
    765   1.1     rmind 	/* Translation type and flags. */
    766   1.1     rmind 	prop_dictionary_set_int32(rldict, "type", type);
    767   1.1     rmind 	prop_dictionary_set_uint32(rldict, "flags", flags);
    768   1.1     rmind 
    769   1.1     rmind 	/* Translation IP. */
    770   1.1     rmind 	addrdat = prop_data_create_data(addr, sz);
    771   1.1     rmind 	if (addrdat == NULL) {
    772   1.1     rmind 		npf_rule_destroy(rl);
    773   1.1     rmind 		return NULL;
    774   1.1     rmind 	}
    775   1.1     rmind 	prop_dictionary_set(rldict, "translation-ip", addrdat);
    776   1.1     rmind 	prop_object_release(addrdat);
    777   1.1     rmind 
    778   1.1     rmind 	/* Translation port (for redirect case). */
    779   1.1     rmind 	prop_dictionary_set_uint16(rldict, "translation-port", port);
    780   1.1     rmind 
    781   1.1     rmind 	return (nl_nat_t *)rl;
    782   1.1     rmind }
    783   1.1     rmind 
    784   1.1     rmind int
    785  1.19  christos npf_nat_insert(nl_config_t *ncf, nl_nat_t *nt, pri_t pri __unused)
    786   1.1     rmind {
    787   1.1     rmind 	prop_dictionary_t rldict = nt->nrl_dict;
    788   1.1     rmind 
    789  1.16     rmind 	prop_dictionary_set_int32(rldict, "priority", NPF_PRI_LAST);
    790   1.1     rmind 	prop_array_add(ncf->ncf_nat_list, rldict);
    791   1.1     rmind 	return 0;
    792   1.1     rmind }
    793   1.1     rmind 
    794   1.9     rmind int
    795   1.9     rmind _npf_nat_foreach(nl_config_t *ncf, nl_rule_callback_t func)
    796   1.9     rmind {
    797  1.16     rmind 	return _npf_rule_foreach1(ncf->ncf_nat_list, func);
    798   1.9     rmind }
    799   1.9     rmind 
    800   1.9     rmind void
    801   1.9     rmind _npf_nat_getinfo(nl_nat_t *nt, int *type, u_int *flags, npf_addr_t *addr,
    802   1.9     rmind     size_t *alen, in_port_t *port)
    803   1.9     rmind {
    804   1.9     rmind 	prop_dictionary_t rldict = nt->nrl_dict;
    805   1.9     rmind 
    806   1.9     rmind 	prop_dictionary_get_int32(rldict, "type", type);
    807   1.9     rmind 	prop_dictionary_get_uint32(rldict, "flags", flags);
    808   1.9     rmind 
    809   1.9     rmind 	prop_object_t obj = prop_dictionary_get(rldict, "translation-ip");
    810   1.9     rmind 	*alen = prop_data_size(obj);
    811   1.9     rmind 	memcpy(addr, prop_data_data_nocopy(obj), *alen);
    812   1.9     rmind 
    813   1.9     rmind 	prop_dictionary_get_uint16(rldict, "translation-port", port);
    814   1.9     rmind }
    815   1.9     rmind 
    816   1.1     rmind /*
    817   1.1     rmind  * TABLE INTERFACE.
    818   1.1     rmind  */
    819   1.1     rmind 
    820   1.1     rmind nl_table_t *
    821   1.5  christos npf_table_create(u_int id, int type)
    822   1.1     rmind {
    823   1.1     rmind 	prop_dictionary_t tldict;
    824   1.1     rmind 	prop_array_t tblents;
    825   1.1     rmind 	nl_table_t *tl;
    826   1.1     rmind 
    827   1.5  christos 	tl = malloc(sizeof(*tl));
    828   1.1     rmind 	if (tl == NULL) {
    829   1.1     rmind 		return NULL;
    830   1.1     rmind 	}
    831   1.1     rmind 	tldict = prop_dictionary_create();
    832   1.1     rmind 	if (tldict == NULL) {
    833   1.1     rmind 		free(tl);
    834   1.1     rmind 		return NULL;
    835   1.1     rmind 	}
    836   1.1     rmind 	prop_dictionary_set_uint32(tldict, "id", id);
    837   1.1     rmind 	prop_dictionary_set_int32(tldict, "type", type);
    838   1.1     rmind 
    839   1.1     rmind 	tblents = prop_array_create();
    840   1.1     rmind 	if (tblents == NULL) {
    841   1.1     rmind 		prop_object_release(tldict);
    842   1.1     rmind 		free(tl);
    843   1.1     rmind 		return NULL;
    844   1.1     rmind 	}
    845   1.1     rmind 	prop_dictionary_set(tldict, "entries", tblents);
    846   1.1     rmind 	prop_object_release(tblents);
    847   1.1     rmind 
    848   1.1     rmind 	tl->ntl_dict = tldict;
    849   1.1     rmind 	return tl;
    850   1.1     rmind }
    851   1.1     rmind 
    852   1.1     rmind int
    853  1.15     rmind npf_table_add_entry(nl_table_t *tl, int af, const npf_addr_t *addr,
    854  1.15     rmind     const npf_netmask_t mask)
    855   1.1     rmind {
    856   1.1     rmind 	prop_dictionary_t tldict = tl->ntl_dict, entdict;
    857   1.1     rmind 	prop_array_t tblents;
    858   1.3    zoltan 	prop_data_t addrdata;
    859  1.15     rmind 	unsigned alen;
    860   1.1     rmind 
    861   1.1     rmind 	/* Create the table entry. */
    862   1.1     rmind 	entdict = prop_dictionary_create();
    863  1.10     rmind 	if (entdict == NULL) {
    864   1.1     rmind 		return ENOMEM;
    865   1.1     rmind 	}
    866  1.15     rmind 
    867  1.15     rmind 	switch (af) {
    868  1.15     rmind 	case AF_INET:
    869  1.15     rmind 		alen = sizeof(struct in_addr);
    870  1.15     rmind 		break;
    871  1.15     rmind 	case AF_INET6:
    872  1.15     rmind 		alen = sizeof(struct in6_addr);
    873  1.15     rmind 		break;
    874  1.15     rmind 	default:
    875  1.15     rmind 		return EINVAL;
    876  1.15     rmind 	}
    877  1.15     rmind 
    878  1.10     rmind 	addrdata = prop_data_create_data(addr, alen);
    879   1.3    zoltan 	prop_dictionary_set(entdict, "addr", addrdata);
    880   1.3    zoltan 	prop_dictionary_set_uint8(entdict, "mask", mask);
    881   1.3    zoltan 	prop_object_release(addrdata);
    882   1.1     rmind 
    883   1.1     rmind 	tblents = prop_dictionary_get(tldict, "entries");
    884   1.1     rmind 	prop_array_add(tblents, entdict);
    885   1.1     rmind 	prop_object_release(entdict);
    886   1.1     rmind 	return 0;
    887   1.1     rmind }
    888   1.1     rmind 
    889   1.1     rmind bool
    890   1.1     rmind npf_table_exists_p(nl_config_t *ncf, u_int tid)
    891   1.1     rmind {
    892   1.1     rmind 	prop_dictionary_t tldict;
    893   1.1     rmind 	prop_object_iterator_t it;
    894   1.1     rmind 
    895   1.1     rmind 	it = prop_array_iterator(ncf->ncf_table_list);
    896   1.1     rmind 	while ((tldict = prop_object_iterator_next(it)) != NULL) {
    897   1.1     rmind 		u_int i;
    898   1.1     rmind 		if (prop_dictionary_get_uint32(tldict, "id", &i) && tid == i)
    899   1.1     rmind 			break;
    900   1.1     rmind 	}
    901   1.1     rmind 	prop_object_iterator_release(it);
    902   1.1     rmind 	return tldict ? true : false;
    903   1.1     rmind }
    904   1.1     rmind 
    905   1.1     rmind int
    906   1.1     rmind npf_table_insert(nl_config_t *ncf, nl_table_t *tl)
    907   1.1     rmind {
    908   1.1     rmind 	prop_dictionary_t tldict = tl->ntl_dict;
    909   1.1     rmind 	u_int tid;
    910   1.1     rmind 
    911   1.1     rmind 	if (!prop_dictionary_get_uint32(tldict, "id", &tid)) {
    912   1.1     rmind 		return EINVAL;
    913   1.1     rmind 	}
    914   1.1     rmind 	if (npf_table_exists_p(ncf, tid)) {
    915   1.1     rmind 		return EEXIST;
    916   1.1     rmind 	}
    917   1.1     rmind 	prop_array_add(ncf->ncf_table_list, tldict);
    918   1.1     rmind 	return 0;
    919   1.1     rmind }
    920   1.1     rmind 
    921   1.1     rmind void
    922   1.1     rmind npf_table_destroy(nl_table_t *tl)
    923   1.1     rmind {
    924   1.1     rmind 
    925   1.1     rmind 	prop_object_release(tl->ntl_dict);
    926   1.1     rmind 	free(tl);
    927   1.1     rmind }
    928   1.1     rmind 
    929   1.9     rmind void
    930   1.9     rmind _npf_table_foreach(nl_config_t *ncf, nl_table_callback_t func)
    931   1.9     rmind {
    932   1.9     rmind 	prop_dictionary_t tldict;
    933   1.9     rmind 	prop_object_iterator_t it;
    934   1.9     rmind 
    935   1.9     rmind 	it = prop_array_iterator(ncf->ncf_table_list);
    936   1.9     rmind 	while ((tldict = prop_object_iterator_next(it)) != NULL) {
    937   1.9     rmind 		u_int id;
    938   1.9     rmind 		int type;
    939   1.9     rmind 
    940   1.9     rmind 		prop_dictionary_get_uint32(tldict, "id", &id);
    941   1.9     rmind 		prop_dictionary_get_int32(tldict, "type", &type);
    942   1.9     rmind 		(*func)(id, type);
    943   1.9     rmind 	}
    944   1.9     rmind 	prop_object_iterator_release(it);
    945   1.9     rmind }
    946   1.9     rmind 
    947   1.1     rmind /*
    948  1.19  christos  * ALG INTERFACE.
    949  1.19  christos  */
    950  1.19  christos 
    951  1.19  christos int
    952  1.19  christos _npf_alg_load(nl_config_t *ncf, const char *name)
    953  1.19  christos {
    954  1.19  christos 	prop_dictionary_t al_dict;
    955  1.19  christos 
    956  1.19  christos 	if (_npf_prop_array_lookup(ncf->ncf_alg_list, "name", name))
    957  1.19  christos 		return EEXIST;
    958  1.19  christos 
    959  1.19  christos 	al_dict = prop_dictionary_create();
    960  1.19  christos 	prop_dictionary_set_cstring(al_dict, "name", name);
    961  1.19  christos 	prop_array_add(ncf->ncf_alg_list, al_dict);
    962  1.19  christos 	prop_object_release(al_dict);
    963  1.19  christos 	return 0;
    964  1.19  christos }
    965  1.19  christos 
    966  1.19  christos int
    967  1.19  christos _npf_alg_unload(nl_config_t *ncf, const char *name)
    968  1.19  christos {
    969  1.19  christos 
    970  1.19  christos 	if (!_npf_prop_array_lookup(ncf->ncf_alg_list, "name", name))
    971  1.19  christos 		return ENOENT;
    972  1.19  christos 
    973  1.19  christos 	// Not yet: prop_array_add(ncf->ncf_alg_list, al_dict);
    974  1.19  christos 	return ENOTSUP;
    975  1.19  christos }
    976  1.19  christos 
    977  1.19  christos /*
    978   1.1     rmind  * MISC.
    979   1.1     rmind  */
    980   1.1     rmind 
    981   1.1     rmind int
    982   1.1     rmind npf_sessions_recv(int fd, const char *fpath)
    983   1.1     rmind {
    984   1.1     rmind 	prop_dictionary_t sdict;
    985   1.1     rmind 	int error;
    986   1.1     rmind 
    987   1.1     rmind 	error = prop_dictionary_recv_ioctl(fd, IOC_NPF_SESSIONS_SAVE, &sdict);
    988   1.1     rmind 	if (error) {
    989   1.1     rmind 		return error;
    990   1.1     rmind 	}
    991   1.1     rmind 	if (!prop_dictionary_externalize_to_file(sdict, fpath)) {
    992   1.1     rmind 		error = errno;
    993   1.1     rmind 	}
    994   1.1     rmind 	prop_object_release(sdict);
    995   1.1     rmind 	return error;
    996   1.1     rmind }
    997   1.1     rmind 
    998   1.1     rmind int
    999   1.1     rmind npf_sessions_send(int fd, const char *fpath)
   1000   1.1     rmind {
   1001   1.1     rmind 	prop_dictionary_t sdict;
   1002   1.1     rmind 	int error;
   1003   1.1     rmind 
   1004   1.1     rmind 	if (fpath) {
   1005   1.1     rmind 		sdict = prop_dictionary_internalize_from_file(fpath);
   1006   1.1     rmind 		if (sdict == NULL) {
   1007   1.1     rmind 			return errno;
   1008   1.1     rmind 		}
   1009   1.1     rmind 	} else {
   1010   1.1     rmind 		/* Empty: will flush the sessions. */
   1011   1.1     rmind 		prop_array_t selist = prop_array_create();
   1012   1.1     rmind 		sdict = prop_dictionary_create();
   1013   1.1     rmind 		prop_dictionary_set(sdict, "session-list", selist);
   1014   1.1     rmind 		prop_object_release(selist);
   1015   1.1     rmind 	}
   1016   1.1     rmind 	error = prop_dictionary_send_ioctl(sdict, fd, IOC_NPF_SESSIONS_LOAD);
   1017   1.1     rmind 	prop_object_release(sdict);
   1018   1.1     rmind 	return error;
   1019   1.1     rmind }
   1020  1.11     rmind 
   1021  1.11     rmind static prop_dictionary_t
   1022  1.11     rmind _npf_debug_initonce(nl_config_t *ncf)
   1023  1.11     rmind {
   1024  1.11     rmind 	if (!ncf->ncf_debug) {
   1025  1.11     rmind 		prop_array_t iflist = prop_array_create();
   1026  1.11     rmind 		ncf->ncf_debug = prop_dictionary_create();
   1027  1.11     rmind 		prop_dictionary_set(ncf->ncf_debug, "interfaces", iflist);
   1028  1.11     rmind 		prop_object_release(iflist);
   1029  1.11     rmind 	}
   1030  1.11     rmind 	return ncf->ncf_debug;
   1031  1.11     rmind }
   1032  1.11     rmind 
   1033  1.11     rmind void
   1034  1.11     rmind _npf_debug_addif(nl_config_t *ncf, struct ifaddrs *ifa, u_int if_idx)
   1035  1.11     rmind {
   1036  1.11     rmind 	prop_dictionary_t ifdict, dbg = _npf_debug_initonce(ncf);
   1037  1.11     rmind 	prop_array_t iflist = prop_dictionary_get(dbg, "interfaces");
   1038  1.11     rmind 
   1039  1.11     rmind 	if (_npf_prop_array_lookup(iflist, "name", ifa->ifa_name)) {
   1040  1.11     rmind 		return;
   1041  1.11     rmind 	}
   1042  1.11     rmind 
   1043  1.11     rmind 	ifdict = prop_dictionary_create();
   1044  1.11     rmind 	prop_dictionary_set_cstring(ifdict, "name", ifa->ifa_name);
   1045  1.11     rmind 	prop_dictionary_set_uint32(ifdict, "flags", ifa->ifa_flags);
   1046  1.11     rmind 	if (!if_idx) {
   1047  1.11     rmind 		if_idx = if_nametoindex(ifa->ifa_name);
   1048  1.11     rmind 	}
   1049  1.11     rmind 	prop_dictionary_set_uint32(ifdict, "idx", if_idx);
   1050  1.11     rmind 
   1051  1.11     rmind 	const struct sockaddr *sa = ifa->ifa_addr;
   1052  1.11     rmind 	npf_addr_t addr;
   1053  1.11     rmind 	size_t alen = 0;
   1054  1.11     rmind 
   1055  1.11     rmind 	switch (sa ? sa->sa_family : -1) {
   1056  1.11     rmind 	case AF_INET: {
   1057  1.11     rmind 		const struct sockaddr_in *sin = (const void *)sa;
   1058  1.11     rmind 		alen = sizeof(sin->sin_addr);
   1059  1.11     rmind 		memcpy(&addr, &sin->sin_addr, alen);
   1060  1.11     rmind 		break;
   1061  1.11     rmind 	}
   1062  1.11     rmind 	case AF_INET6: {
   1063  1.11     rmind 		const struct sockaddr_in6 *sin6 = (const void *)sa;
   1064  1.11     rmind 		alen = sizeof(sin6->sin6_addr);
   1065  1.11     rmind 		memcpy(&addr, &sin6->sin6_addr, alen);
   1066  1.11     rmind 		break;
   1067  1.11     rmind 	}
   1068  1.11     rmind 	default:
   1069  1.11     rmind 		break;
   1070  1.11     rmind 	}
   1071  1.11     rmind 
   1072  1.11     rmind 	if (alen) {
   1073  1.11     rmind 		prop_data_t addrdata = prop_data_create_data(&addr, alen);
   1074  1.11     rmind 		prop_dictionary_set(ifdict, "addr", addrdata);
   1075  1.11     rmind 		prop_object_release(addrdata);
   1076  1.11     rmind 	}
   1077  1.11     rmind 	prop_array_add(iflist, ifdict);
   1078  1.11     rmind 	prop_object_release(ifdict);
   1079  1.11     rmind }
   1080