Home | History | Annotate | Line # | Download | only in libnpf
npf.c revision 1.7
      1  1.7     rmind /*	$NetBSD: npf.c,v 1.7 2012/02/05 00:37:13 rmind Exp $	*/
      2  1.1     rmind 
      3  1.1     rmind /*-
      4  1.6     rmind  * Copyright (c) 2010-2012 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.7     rmind __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.7 2012/02/05 00:37:13 rmind 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.1     rmind #include <prop/proplib.h>
     39  1.1     rmind 
     40  1.1     rmind #include <stdlib.h>
     41  1.1     rmind #include <string.h>
     42  1.7     rmind #include <assert.h>
     43  1.1     rmind #include <errno.h>
     44  1.1     rmind #include <err.h>
     45  1.1     rmind 
     46  1.1     rmind #define	_NPF_PRIVATE
     47  1.1     rmind #include "npf.h"
     48  1.1     rmind 
     49  1.1     rmind struct nl_config {
     50  1.1     rmind 	/* Rules, translations, tables, procedures. */
     51  1.1     rmind 	prop_array_t		ncf_rules_list;
     52  1.1     rmind 	prop_array_t		ncf_rproc_list;
     53  1.1     rmind 	prop_array_t		ncf_table_list;
     54  1.1     rmind 	prop_array_t		ncf_nat_list;
     55  1.1     rmind 	/* Priority counters. */
     56  1.1     rmind 	pri_t			ncf_rule_pri;
     57  1.1     rmind 	pri_t			ncf_nat_pri;
     58  1.7     rmind 	/* Error report. */
     59  1.7     rmind 	prop_dictionary_t	ncf_err;
     60  1.4     rmind 	/* Custom file to externalise property-list. */
     61  1.4     rmind 	const char *		ncf_plist;
     62  1.6     rmind 	bool			ncf_flush;
     63  1.1     rmind };
     64  1.1     rmind 
     65  1.1     rmind struct nl_rule {
     66  1.1     rmind 	prop_dictionary_t	nrl_dict;
     67  1.1     rmind };
     68  1.1     rmind 
     69  1.1     rmind struct nl_rproc {
     70  1.1     rmind 	prop_dictionary_t	nrp_dict;
     71  1.1     rmind };
     72  1.1     rmind 
     73  1.1     rmind struct nl_table {
     74  1.1     rmind 	prop_dictionary_t	ntl_dict;
     75  1.1     rmind };
     76  1.1     rmind 
     77  1.1     rmind /*
     78  1.1     rmind  * CONFIGURATION INTERFACE.
     79  1.1     rmind  */
     80  1.1     rmind 
     81  1.1     rmind nl_config_t *
     82  1.1     rmind npf_config_create(void)
     83  1.1     rmind {
     84  1.1     rmind 	nl_config_t *ncf;
     85  1.1     rmind 
     86  1.7     rmind 	ncf = calloc(1, sizeof(*ncf));
     87  1.1     rmind 	if (ncf == NULL) {
     88  1.1     rmind 		return NULL;
     89  1.1     rmind 	}
     90  1.1     rmind 	ncf->ncf_rules_list = prop_array_create();
     91  1.1     rmind 	ncf->ncf_rproc_list = prop_array_create();
     92  1.1     rmind 	ncf->ncf_table_list = prop_array_create();
     93  1.1     rmind 	ncf->ncf_nat_list = prop_array_create();
     94  1.1     rmind 
     95  1.1     rmind 	ncf->ncf_rule_pri = 1;
     96  1.1     rmind 	ncf->ncf_nat_pri = 1;
     97  1.1     rmind 
     98  1.4     rmind 	ncf->ncf_plist = NULL;
     99  1.6     rmind 	ncf->ncf_flush = false;
    100  1.4     rmind 
    101  1.1     rmind 	return ncf;
    102  1.1     rmind }
    103  1.1     rmind 
    104  1.1     rmind int
    105  1.1     rmind npf_config_submit(nl_config_t *ncf, int fd)
    106  1.1     rmind {
    107  1.7     rmind 	const char *plist = ncf->ncf_plist;
    108  1.1     rmind 	prop_dictionary_t npf_dict;
    109  1.1     rmind 	int error = 0;
    110  1.1     rmind 
    111  1.1     rmind 	npf_dict = prop_dictionary_create();
    112  1.1     rmind 	if (npf_dict == NULL) {
    113  1.1     rmind 		return ENOMEM;
    114  1.1     rmind 	}
    115  1.1     rmind 	prop_dictionary_set(npf_dict, "rules", ncf->ncf_rules_list);
    116  1.1     rmind 	prop_dictionary_set(npf_dict, "rprocs", ncf->ncf_rproc_list);
    117  1.1     rmind 	prop_dictionary_set(npf_dict, "tables", ncf->ncf_table_list);
    118  1.1     rmind 	prop_dictionary_set(npf_dict, "translation", ncf->ncf_nat_list);
    119  1.6     rmind 	prop_dictionary_set_bool(npf_dict, "flush", ncf->ncf_flush);
    120  1.1     rmind 
    121  1.4     rmind 	if (plist) {
    122  1.4     rmind 		if (!prop_dictionary_externalize_to_file(npf_dict, plist)) {
    123  1.4     rmind 			error = errno;
    124  1.4     rmind 		}
    125  1.7     rmind 		prop_object_release(npf_dict);
    126  1.7     rmind 		return error;
    127  1.7     rmind 	}
    128  1.7     rmind 
    129  1.7     rmind 	error = prop_dictionary_sendrecv_ioctl(npf_dict, fd,
    130  1.7     rmind 	    IOC_NPF_RELOAD, &ncf->ncf_err);
    131  1.7     rmind 	if (error) {
    132  1.7     rmind 		prop_object_release(npf_dict);
    133  1.7     rmind 		assert(ncf->ncf_err == NULL);
    134  1.7     rmind 		return error;
    135  1.1     rmind 	}
    136  1.7     rmind 
    137  1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err, "errno", &error);
    138  1.1     rmind 	prop_object_release(npf_dict);
    139  1.1     rmind 	return error;
    140  1.1     rmind }
    141  1.1     rmind 
    142  1.6     rmind int
    143  1.6     rmind npf_config_flush(int fd)
    144  1.6     rmind {
    145  1.6     rmind 	nl_config_t *ncf;
    146  1.6     rmind 	int error;
    147  1.6     rmind 
    148  1.6     rmind 	ncf = npf_config_create();
    149  1.6     rmind 	if (ncf == NULL) {
    150  1.6     rmind 		return ENOMEM;
    151  1.6     rmind 	}
    152  1.6     rmind 	ncf->ncf_flush = true;
    153  1.6     rmind 	error = npf_config_submit(ncf, fd);
    154  1.6     rmind 	npf_config_destroy(ncf);
    155  1.6     rmind 	return error;
    156  1.6     rmind }
    157  1.6     rmind 
    158  1.1     rmind void
    159  1.7     rmind _npf_config_error(nl_config_t *ncf, nl_error_t *ne)
    160  1.7     rmind {
    161  1.7     rmind 	memset(ne, 0, sizeof(*ne));
    162  1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err, "id", &ne->ne_id);
    163  1.7     rmind 	prop_dictionary_get_cstring(ncf->ncf_err,
    164  1.7     rmind 	    "source-file", &ne->ne_source_file);
    165  1.7     rmind 	prop_dictionary_get_uint32(ncf->ncf_err,
    166  1.7     rmind 	    "source-line", &ne->ne_source_line);
    167  1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err,
    168  1.7     rmind 	    "ncode-error", &ne->ne_ncode_error);
    169  1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err,
    170  1.7     rmind 	    "ncode-errat", &ne->ne_ncode_errat);
    171  1.7     rmind }
    172  1.7     rmind 
    173  1.7     rmind void
    174  1.1     rmind npf_config_destroy(nl_config_t *ncf)
    175  1.1     rmind {
    176  1.1     rmind 
    177  1.1     rmind 	prop_object_release(ncf->ncf_rules_list);
    178  1.1     rmind 	prop_object_release(ncf->ncf_rproc_list);
    179  1.1     rmind 	prop_object_release(ncf->ncf_table_list);
    180  1.1     rmind 	prop_object_release(ncf->ncf_nat_list);
    181  1.7     rmind 	if (ncf->ncf_err) {
    182  1.7     rmind 		prop_object_release(ncf->ncf_err);
    183  1.7     rmind 	}
    184  1.1     rmind 	free(ncf);
    185  1.1     rmind }
    186  1.1     rmind 
    187  1.4     rmind void
    188  1.4     rmind _npf_config_setsubmit(nl_config_t *ncf, const char *plist_file)
    189  1.4     rmind {
    190  1.4     rmind 
    191  1.4     rmind 	ncf->ncf_plist = plist_file;
    192  1.4     rmind }
    193  1.4     rmind 
    194  1.1     rmind static bool
    195  1.1     rmind _npf_prop_array_lookup(prop_array_t array, const char *key, const char *name)
    196  1.1     rmind {
    197  1.1     rmind 	prop_dictionary_t dict;
    198  1.1     rmind 	prop_object_iterator_t it;
    199  1.1     rmind 
    200  1.1     rmind 	it = prop_array_iterator(array);
    201  1.1     rmind 	while ((dict = prop_object_iterator_next(it)) != NULL) {
    202  1.1     rmind 		const char *lname;
    203  1.1     rmind 		prop_dictionary_get_cstring_nocopy(dict, key, &lname);
    204  1.1     rmind 		if (strcmp(name, lname) == 0)
    205  1.1     rmind 			break;
    206  1.1     rmind 	}
    207  1.1     rmind 	prop_object_iterator_release(it);
    208  1.1     rmind 	return dict ? true : false;
    209  1.1     rmind }
    210  1.1     rmind 
    211  1.1     rmind /*
    212  1.1     rmind  * RULE INTERFACE.
    213  1.1     rmind  */
    214  1.1     rmind 
    215  1.1     rmind nl_rule_t *
    216  1.1     rmind npf_rule_create(const char *name, uint32_t attr, u_int if_idx)
    217  1.1     rmind {
    218  1.1     rmind 	prop_dictionary_t rldict;
    219  1.1     rmind 	nl_rule_t *rl;
    220  1.1     rmind 
    221  1.5  christos 	rl = malloc(sizeof(*rl));
    222  1.1     rmind 	if (rl == NULL) {
    223  1.1     rmind 		return NULL;
    224  1.1     rmind 	}
    225  1.1     rmind 	rldict = prop_dictionary_create();
    226  1.1     rmind 	if (rldict == NULL) {
    227  1.1     rmind 		free(rl);
    228  1.1     rmind 		return NULL;
    229  1.1     rmind 	}
    230  1.1     rmind 	if (name) {
    231  1.1     rmind 		prop_dictionary_set_cstring(rldict, "name", name);
    232  1.1     rmind 	}
    233  1.1     rmind 	prop_dictionary_set_uint32(rldict, "attributes", attr);
    234  1.1     rmind 
    235  1.1     rmind 	if (if_idx) {
    236  1.1     rmind 		prop_dictionary_set_uint32(rldict, "interface", if_idx);
    237  1.1     rmind 	}
    238  1.1     rmind 	rl->nrl_dict = rldict;
    239  1.1     rmind 	return rl;
    240  1.1     rmind }
    241  1.1     rmind 
    242  1.1     rmind int
    243  1.1     rmind npf_rule_setcode(nl_rule_t *rl, int type, const void *code, size_t sz)
    244  1.1     rmind {
    245  1.1     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    246  1.1     rmind 	prop_data_t cdata;
    247  1.1     rmind 
    248  1.1     rmind 	if (type != NPF_CODE_NCODE) {
    249  1.1     rmind 		return ENOTSUP;
    250  1.1     rmind 	}
    251  1.1     rmind 	cdata = prop_data_create_data(code, sz);
    252  1.1     rmind 	if (cdata == NULL) {
    253  1.1     rmind 		return ENOMEM;
    254  1.1     rmind 	}
    255  1.1     rmind 	prop_dictionary_set(rldict, "ncode", cdata);
    256  1.1     rmind 	prop_object_release(cdata);
    257  1.1     rmind 	return 0;
    258  1.1     rmind }
    259  1.1     rmind 
    260  1.1     rmind int
    261  1.1     rmind npf_rule_setproc(nl_config_t *ncf, nl_rule_t *rl, const char *name)
    262  1.1     rmind {
    263  1.1     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    264  1.1     rmind 
    265  1.1     rmind 	if (!npf_rproc_exists_p(ncf, name)) {
    266  1.1     rmind 		return ENOENT;
    267  1.1     rmind 	}
    268  1.1     rmind 	prop_dictionary_set_cstring(rldict, "rproc", name);
    269  1.1     rmind 	return 0;
    270  1.1     rmind }
    271  1.1     rmind 
    272  1.1     rmind bool
    273  1.1     rmind npf_rule_exists_p(nl_config_t *ncf, const char *name)
    274  1.1     rmind {
    275  1.1     rmind 
    276  1.1     rmind 	return _npf_prop_array_lookup(ncf->ncf_rules_list, "name", name);
    277  1.1     rmind }
    278  1.1     rmind 
    279  1.1     rmind int
    280  1.1     rmind npf_rule_insert(nl_config_t *ncf, nl_rule_t *parent, nl_rule_t *rl, pri_t pri)
    281  1.1     rmind {
    282  1.1     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    283  1.1     rmind 	prop_array_t rlset;
    284  1.1     rmind 
    285  1.1     rmind 	if (pri == NPF_PRI_NEXT) {
    286  1.1     rmind 		pri = ncf->ncf_rule_pri++;
    287  1.1     rmind 	} else if (ncf) {
    288  1.1     rmind 		ncf->ncf_rule_pri = pri + 1;
    289  1.1     rmind 	}
    290  1.1     rmind 	prop_dictionary_set_int32(rldict, "priority", pri);
    291  1.1     rmind 
    292  1.1     rmind 	if (parent) {
    293  1.1     rmind 		prop_dictionary_t pdict = parent->nrl_dict;
    294  1.1     rmind 		rlset = prop_dictionary_get(pdict, "subrules");
    295  1.1     rmind 		if (rlset == NULL) {
    296  1.1     rmind 			rlset = prop_array_create();
    297  1.1     rmind 			prop_dictionary_set(pdict, "subrules", rlset);
    298  1.1     rmind 			prop_object_release(rlset);
    299  1.1     rmind 		}
    300  1.1     rmind 	} else {
    301  1.1     rmind 		rlset = ncf->ncf_rules_list;
    302  1.1     rmind 	}
    303  1.1     rmind 	prop_array_add(rlset, rldict);
    304  1.1     rmind 	return 0;
    305  1.1     rmind }
    306  1.1     rmind 
    307  1.1     rmind void
    308  1.1     rmind npf_rule_destroy(nl_rule_t *rl)
    309  1.1     rmind {
    310  1.1     rmind 
    311  1.1     rmind 	prop_object_release(rl->nrl_dict);
    312  1.1     rmind 	free(rl);
    313  1.1     rmind }
    314  1.1     rmind 
    315  1.1     rmind /*
    316  1.1     rmind  * RULE PROCEDURE INTERFACE.
    317  1.1     rmind  */
    318  1.1     rmind 
    319  1.1     rmind nl_rproc_t *
    320  1.1     rmind npf_rproc_create(const char *name)
    321  1.1     rmind {
    322  1.1     rmind 	prop_dictionary_t rpdict;
    323  1.1     rmind 	nl_rproc_t *nrp;
    324  1.1     rmind 
    325  1.1     rmind 	nrp = malloc(sizeof(nl_rproc_t));
    326  1.1     rmind 	if (nrp == NULL) {
    327  1.1     rmind 		return NULL;
    328  1.1     rmind 	}
    329  1.1     rmind 	rpdict = prop_dictionary_create();
    330  1.1     rmind 	if (rpdict == NULL) {
    331  1.1     rmind 		free(nrp);
    332  1.1     rmind 		return NULL;
    333  1.1     rmind 	}
    334  1.1     rmind 	prop_dictionary_set_cstring(rpdict, "name", name);
    335  1.1     rmind 	nrp->nrp_dict = rpdict;
    336  1.1     rmind 	return nrp;
    337  1.1     rmind }
    338  1.1     rmind 
    339  1.1     rmind bool
    340  1.1     rmind npf_rproc_exists_p(nl_config_t *ncf, const char *name)
    341  1.1     rmind {
    342  1.1     rmind 
    343  1.1     rmind 	return _npf_prop_array_lookup(ncf->ncf_rproc_list, "name", name);
    344  1.1     rmind }
    345  1.1     rmind 
    346  1.1     rmind int
    347  1.5  christos _npf_rproc_setnorm(nl_rproc_t *rp, bool rnd, bool no_df, u_int minttl,
    348  1.5  christos     u_int maxmss)
    349  1.1     rmind {
    350  1.1     rmind 	prop_dictionary_t rpdict = rp->nrp_dict;
    351  1.2     rmind 	uint32_t fl;
    352  1.1     rmind 
    353  1.1     rmind 	prop_dictionary_set_bool(rpdict, "randomize-id", rnd);
    354  1.1     rmind 	prop_dictionary_set_bool(rpdict, "no-df", no_df);
    355  1.1     rmind 	prop_dictionary_set_uint32(rpdict, "min-ttl", minttl);
    356  1.1     rmind 	prop_dictionary_set_uint32(rpdict, "max-mss", maxmss);
    357  1.1     rmind 
    358  1.2     rmind 	prop_dictionary_get_uint32(rpdict, "flags", &fl);
    359  1.2     rmind 	prop_dictionary_set_uint32(rpdict, "flags", fl | NPF_RPROC_NORMALIZE);
    360  1.1     rmind 	return 0;
    361  1.1     rmind }
    362  1.1     rmind 
    363  1.1     rmind int
    364  1.1     rmind _npf_rproc_setlog(nl_rproc_t *rp, u_int if_idx)
    365  1.1     rmind {
    366  1.1     rmind 	prop_dictionary_t rpdict = rp->nrp_dict;
    367  1.2     rmind 	uint32_t fl;
    368  1.1     rmind 
    369  1.1     rmind 	prop_dictionary_set_uint32(rpdict, "log-interface", if_idx);
    370  1.1     rmind 
    371  1.2     rmind 	prop_dictionary_get_uint32(rpdict, "flags", &fl);
    372  1.2     rmind 	prop_dictionary_set_uint32(rpdict, "flags", fl | NPF_RPROC_LOG);
    373  1.1     rmind 	return 0;
    374  1.1     rmind }
    375  1.1     rmind 
    376  1.1     rmind int
    377  1.1     rmind npf_rproc_insert(nl_config_t *ncf, nl_rproc_t *rp)
    378  1.1     rmind {
    379  1.1     rmind 	prop_dictionary_t rpdict = rp->nrp_dict;
    380  1.1     rmind 	const char *name;
    381  1.1     rmind 
    382  1.1     rmind 	if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
    383  1.1     rmind 		return EINVAL;
    384  1.1     rmind 	}
    385  1.1     rmind 	if (npf_rproc_exists_p(ncf, name)) {
    386  1.1     rmind 		return EEXIST;
    387  1.1     rmind 	}
    388  1.1     rmind 	prop_array_add(ncf->ncf_rproc_list, rpdict);
    389  1.1     rmind 	return 0;
    390  1.1     rmind }
    391  1.1     rmind 
    392  1.1     rmind /*
    393  1.1     rmind  * TRANSLATION INTERFACE.
    394  1.1     rmind  */
    395  1.1     rmind 
    396  1.1     rmind nl_nat_t *
    397  1.5  christos npf_nat_create(int type, u_int flags, u_int if_idx,
    398  1.1     rmind     npf_addr_t *addr, int af, in_port_t port)
    399  1.1     rmind {
    400  1.1     rmind 	nl_rule_t *rl;
    401  1.1     rmind 	prop_dictionary_t rldict;
    402  1.1     rmind 	prop_data_t addrdat;
    403  1.1     rmind 	uint32_t attr;
    404  1.1     rmind 	size_t sz;
    405  1.1     rmind 
    406  1.1     rmind 	if (af == AF_INET) {
    407  1.1     rmind 		sz = sizeof(struct in_addr);
    408  1.1     rmind 	} else if (af == AF_INET6) {
    409  1.1     rmind 		sz = sizeof(struct in6_addr);
    410  1.1     rmind 	} else {
    411  1.1     rmind 		return NULL;
    412  1.1     rmind 	}
    413  1.1     rmind 
    414  1.1     rmind 	attr = NPF_RULE_PASS | NPF_RULE_FINAL |
    415  1.2     rmind 	    (type == NPF_NATOUT ? NPF_RULE_OUT : NPF_RULE_IN);
    416  1.1     rmind 
    417  1.1     rmind 	/* Create a rule for NAT policy.  Next, will add translation data. */
    418  1.1     rmind 	rl = npf_rule_create(NULL, attr, if_idx);
    419  1.1     rmind 	if (rl == NULL) {
    420  1.1     rmind 		return NULL;
    421  1.1     rmind 	}
    422  1.1     rmind 	rldict = rl->nrl_dict;
    423  1.1     rmind 
    424  1.1     rmind 	/* Translation type and flags. */
    425  1.1     rmind 	prop_dictionary_set_int32(rldict, "type", type);
    426  1.1     rmind 	prop_dictionary_set_uint32(rldict, "flags", flags);
    427  1.1     rmind 
    428  1.1     rmind 	/* Translation IP. */
    429  1.1     rmind 	addrdat = prop_data_create_data(addr, sz);
    430  1.1     rmind 	if (addrdat == NULL) {
    431  1.1     rmind 		npf_rule_destroy(rl);
    432  1.1     rmind 		return NULL;
    433  1.1     rmind 	}
    434  1.1     rmind 	prop_dictionary_set(rldict, "translation-ip", addrdat);
    435  1.1     rmind 	prop_object_release(addrdat);
    436  1.1     rmind 
    437  1.1     rmind 	/* Translation port (for redirect case). */
    438  1.1     rmind 	prop_dictionary_set_uint16(rldict, "translation-port", port);
    439  1.1     rmind 
    440  1.1     rmind 	return (nl_nat_t *)rl;
    441  1.1     rmind }
    442  1.1     rmind 
    443  1.1     rmind int
    444  1.1     rmind npf_nat_insert(nl_config_t *ncf, nl_nat_t *nt, pri_t pri)
    445  1.1     rmind {
    446  1.1     rmind 	prop_dictionary_t rldict = nt->nrl_dict;
    447  1.1     rmind 
    448  1.1     rmind 	if (pri == NPF_PRI_NEXT) {
    449  1.1     rmind 		pri = ncf->ncf_nat_pri++;
    450  1.1     rmind 	} else {
    451  1.1     rmind 		ncf->ncf_nat_pri = pri + 1;
    452  1.1     rmind 	}
    453  1.1     rmind 	prop_dictionary_set_int32(rldict, "priority", pri);
    454  1.1     rmind 	prop_array_add(ncf->ncf_nat_list, rldict);
    455  1.1     rmind 	return 0;
    456  1.1     rmind }
    457  1.1     rmind 
    458  1.1     rmind /*
    459  1.1     rmind  * TABLE INTERFACE.
    460  1.1     rmind  */
    461  1.1     rmind 
    462  1.1     rmind nl_table_t *
    463  1.5  christos npf_table_create(u_int id, int type)
    464  1.1     rmind {
    465  1.1     rmind 	prop_dictionary_t tldict;
    466  1.1     rmind 	prop_array_t tblents;
    467  1.1     rmind 	nl_table_t *tl;
    468  1.1     rmind 
    469  1.5  christos 	tl = malloc(sizeof(*tl));
    470  1.1     rmind 	if (tl == NULL) {
    471  1.1     rmind 		return NULL;
    472  1.1     rmind 	}
    473  1.1     rmind 	tldict = prop_dictionary_create();
    474  1.1     rmind 	if (tldict == NULL) {
    475  1.1     rmind 		free(tl);
    476  1.1     rmind 		return NULL;
    477  1.1     rmind 	}
    478  1.1     rmind 	prop_dictionary_set_uint32(tldict, "id", id);
    479  1.1     rmind 	prop_dictionary_set_int32(tldict, "type", type);
    480  1.1     rmind 
    481  1.1     rmind 	tblents = prop_array_create();
    482  1.1     rmind 	if (tblents == NULL) {
    483  1.1     rmind 		prop_object_release(tldict);
    484  1.1     rmind 		free(tl);
    485  1.1     rmind 		return NULL;
    486  1.1     rmind 	}
    487  1.1     rmind 	prop_dictionary_set(tldict, "entries", tblents);
    488  1.1     rmind 	prop_object_release(tblents);
    489  1.1     rmind 
    490  1.1     rmind 	tl->ntl_dict = tldict;
    491  1.1     rmind 	return tl;
    492  1.1     rmind }
    493  1.1     rmind 
    494  1.1     rmind int
    495  1.3    zoltan npf_table_add_entry(nl_table_t *tl, npf_addr_t *addr, npf_netmask_t mask)
    496  1.1     rmind {
    497  1.1     rmind 	prop_dictionary_t tldict = tl->ntl_dict, entdict;
    498  1.1     rmind 	prop_array_t tblents;
    499  1.3    zoltan 	prop_data_t addrdata;
    500  1.1     rmind 
    501  1.1     rmind 	/* Create the table entry. */
    502  1.1     rmind 	entdict = prop_dictionary_create();
    503  1.1     rmind 	if (entdict) {
    504  1.1     rmind 		return ENOMEM;
    505  1.1     rmind 	}
    506  1.3    zoltan 	addrdata = prop_data_create_data(addr, sizeof(npf_addr_t));
    507  1.3    zoltan 	prop_dictionary_set(entdict, "addr", addrdata);
    508  1.3    zoltan 	prop_dictionary_set_uint8(entdict, "mask", mask);
    509  1.3    zoltan 	prop_object_release(addrdata);
    510  1.1     rmind 
    511  1.1     rmind 	/* Insert the entry. */
    512  1.1     rmind 	tblents = prop_dictionary_get(tldict, "entries");
    513  1.1     rmind 	prop_array_add(tblents, entdict);
    514  1.1     rmind 	prop_object_release(entdict);
    515  1.1     rmind 	return 0;
    516  1.1     rmind }
    517  1.1     rmind 
    518  1.1     rmind bool
    519  1.1     rmind npf_table_exists_p(nl_config_t *ncf, u_int tid)
    520  1.1     rmind {
    521  1.1     rmind 	prop_dictionary_t tldict;
    522  1.1     rmind 	prop_object_iterator_t it;
    523  1.1     rmind 
    524  1.1     rmind 	it = prop_array_iterator(ncf->ncf_table_list);
    525  1.1     rmind 	while ((tldict = prop_object_iterator_next(it)) != NULL) {
    526  1.1     rmind 		u_int i;
    527  1.1     rmind 		if (prop_dictionary_get_uint32(tldict, "id", &i) && tid == i)
    528  1.1     rmind 			break;
    529  1.1     rmind 	}
    530  1.1     rmind 	prop_object_iterator_release(it);
    531  1.1     rmind 	return tldict ? true : false;
    532  1.1     rmind }
    533  1.1     rmind 
    534  1.1     rmind int
    535  1.1     rmind npf_table_insert(nl_config_t *ncf, nl_table_t *tl)
    536  1.1     rmind {
    537  1.1     rmind 	prop_dictionary_t tldict = tl->ntl_dict;
    538  1.1     rmind 	u_int tid;
    539  1.1     rmind 
    540  1.1     rmind 	if (!prop_dictionary_get_uint32(tldict, "id", &tid)) {
    541  1.1     rmind 		return EINVAL;
    542  1.1     rmind 	}
    543  1.1     rmind 	if (npf_table_exists_p(ncf, tid)) {
    544  1.1     rmind 		return EEXIST;
    545  1.1     rmind 	}
    546  1.1     rmind 	prop_array_add(ncf->ncf_table_list, tldict);
    547  1.1     rmind 	return 0;
    548  1.1     rmind }
    549  1.1     rmind 
    550  1.1     rmind void
    551  1.1     rmind npf_table_destroy(nl_table_t *tl)
    552  1.1     rmind {
    553  1.1     rmind 
    554  1.1     rmind 	prop_object_release(tl->ntl_dict);
    555  1.1     rmind 	free(tl);
    556  1.1     rmind }
    557  1.1     rmind 
    558  1.1     rmind /*
    559  1.1     rmind  * MISC.
    560  1.1     rmind  */
    561  1.1     rmind 
    562  1.1     rmind int
    563  1.5  christos npf_update_rule(int fd, const char *rname __unused, nl_rule_t *rl)
    564  1.1     rmind {
    565  1.7     rmind 	prop_dictionary_t rldict = rl->nrl_dict, errdict = NULL;
    566  1.1     rmind 	int error;
    567  1.1     rmind 
    568  1.7     rmind 	error = prop_dictionary_sendrecv_ioctl(rldict, fd,
    569  1.7     rmind 	    IOC_NPF_UPDATE_RULE, &errdict);
    570  1.7     rmind 	if (errdict) {
    571  1.7     rmind 		prop_object_release(errdict);
    572  1.7     rmind 	}
    573  1.1     rmind 	return error;
    574  1.1     rmind }
    575  1.1     rmind 
    576  1.1     rmind int
    577  1.1     rmind npf_sessions_recv(int fd, const char *fpath)
    578  1.1     rmind {
    579  1.1     rmind 	prop_dictionary_t sdict;
    580  1.1     rmind 	int error;
    581  1.1     rmind 
    582  1.1     rmind 	error = prop_dictionary_recv_ioctl(fd, IOC_NPF_SESSIONS_SAVE, &sdict);
    583  1.1     rmind 	if (error) {
    584  1.1     rmind 		return error;
    585  1.1     rmind 	}
    586  1.1     rmind 	if (!prop_dictionary_externalize_to_file(sdict, fpath)) {
    587  1.1     rmind 		error = errno;
    588  1.1     rmind 	}
    589  1.1     rmind 	prop_object_release(sdict);
    590  1.1     rmind 	return error;
    591  1.1     rmind }
    592  1.1     rmind 
    593  1.1     rmind int
    594  1.1     rmind npf_sessions_send(int fd, const char *fpath)
    595  1.1     rmind {
    596  1.1     rmind 	prop_dictionary_t sdict;
    597  1.1     rmind 	int error;
    598  1.1     rmind 
    599  1.1     rmind 	if (fpath) {
    600  1.1     rmind 		sdict = prop_dictionary_internalize_from_file(fpath);
    601  1.1     rmind 		if (sdict == NULL) {
    602  1.1     rmind 			return errno;
    603  1.1     rmind 		}
    604  1.1     rmind 	} else {
    605  1.1     rmind 		/* Empty: will flush the sessions. */
    606  1.1     rmind 		prop_array_t selist = prop_array_create();
    607  1.1     rmind 		sdict = prop_dictionary_create();
    608  1.1     rmind 		prop_dictionary_set(sdict, "session-list", selist);
    609  1.1     rmind 		prop_object_release(selist);
    610  1.1     rmind 	}
    611  1.1     rmind 	error = prop_dictionary_send_ioctl(sdict, fd, IOC_NPF_SESSIONS_LOAD);
    612  1.1     rmind 	prop_object_release(sdict);
    613  1.1     rmind 	return error;
    614  1.1     rmind }
    615