Home | History | Annotate | Line # | Download | only in libnpf
npf.c revision 1.14
      1  1.14     rmind /*	$NetBSD: npf.c,v 1.14 2012/10/28 16:27:20 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.14     rmind __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.14 2012/10/28 16:27:20 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.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.1     rmind 	prop_array_t		ncf_rules_list;
     54   1.1     rmind 	prop_array_t		ncf_rproc_list;
     55   1.1     rmind 	prop_array_t		ncf_table_list;
     56   1.1     rmind 	prop_array_t		ncf_nat_list;
     57   1.1     rmind 	/* Priority counters. */
     58   1.1     rmind 	pri_t			ncf_rule_pri;
     59   1.1     rmind 	pri_t			ncf_nat_pri;
     60  1.11     rmind 	/* Debug information. */
     61  1.11     rmind 	prop_dictionary_t	ncf_debug;
     62   1.7     rmind 	/* Error report. */
     63   1.7     rmind 	prop_dictionary_t	ncf_err;
     64   1.4     rmind 	/* Custom file to externalise property-list. */
     65   1.4     rmind 	const char *		ncf_plist;
     66   1.6     rmind 	bool			ncf_flush;
     67   1.1     rmind };
     68   1.1     rmind 
     69   1.1     rmind struct nl_rule {
     70   1.1     rmind 	prop_dictionary_t	nrl_dict;
     71   1.1     rmind };
     72   1.1     rmind 
     73   1.1     rmind struct nl_rproc {
     74   1.1     rmind 	prop_dictionary_t	nrp_dict;
     75   1.1     rmind };
     76   1.1     rmind 
     77   1.1     rmind struct nl_table {
     78   1.1     rmind 	prop_dictionary_t	ntl_dict;
     79   1.1     rmind };
     80   1.1     rmind 
     81  1.13     rmind struct nl_ext {
     82  1.13     rmind 	const char *		nxt_name;
     83  1.13     rmind 	prop_dictionary_t	nxt_dict;
     84  1.13     rmind };
     85  1.13     rmind 
     86   1.1     rmind /*
     87   1.1     rmind  * CONFIGURATION INTERFACE.
     88   1.1     rmind  */
     89   1.1     rmind 
     90   1.1     rmind nl_config_t *
     91   1.1     rmind npf_config_create(void)
     92   1.1     rmind {
     93   1.1     rmind 	nl_config_t *ncf;
     94   1.1     rmind 
     95   1.7     rmind 	ncf = calloc(1, sizeof(*ncf));
     96   1.1     rmind 	if (ncf == NULL) {
     97   1.1     rmind 		return NULL;
     98   1.1     rmind 	}
     99   1.1     rmind 	ncf->ncf_rules_list = prop_array_create();
    100   1.1     rmind 	ncf->ncf_rproc_list = prop_array_create();
    101   1.1     rmind 	ncf->ncf_table_list = prop_array_create();
    102   1.1     rmind 	ncf->ncf_nat_list = prop_array_create();
    103   1.1     rmind 
    104   1.1     rmind 	ncf->ncf_rule_pri = 1;
    105   1.1     rmind 	ncf->ncf_nat_pri = 1;
    106   1.1     rmind 
    107   1.4     rmind 	ncf->ncf_plist = NULL;
    108   1.6     rmind 	ncf->ncf_flush = false;
    109   1.4     rmind 
    110   1.1     rmind 	return ncf;
    111   1.1     rmind }
    112   1.1     rmind 
    113   1.1     rmind int
    114   1.1     rmind npf_config_submit(nl_config_t *ncf, int fd)
    115   1.1     rmind {
    116   1.7     rmind 	const char *plist = ncf->ncf_plist;
    117   1.1     rmind 	prop_dictionary_t npf_dict;
    118   1.1     rmind 	int error = 0;
    119   1.1     rmind 
    120   1.1     rmind 	npf_dict = prop_dictionary_create();
    121   1.1     rmind 	if (npf_dict == NULL) {
    122   1.1     rmind 		return ENOMEM;
    123   1.1     rmind 	}
    124  1.11     rmind 	if (ncf->ncf_debug) {
    125  1.11     rmind 		prop_dictionary_set(npf_dict, "debug", ncf->ncf_debug);
    126  1.11     rmind 	}
    127   1.1     rmind 	prop_dictionary_set(npf_dict, "rules", ncf->ncf_rules_list);
    128   1.1     rmind 	prop_dictionary_set(npf_dict, "rprocs", ncf->ncf_rproc_list);
    129   1.1     rmind 	prop_dictionary_set(npf_dict, "tables", ncf->ncf_table_list);
    130   1.1     rmind 	prop_dictionary_set(npf_dict, "translation", ncf->ncf_nat_list);
    131   1.6     rmind 	prop_dictionary_set_bool(npf_dict, "flush", ncf->ncf_flush);
    132   1.1     rmind 
    133   1.4     rmind 	if (plist) {
    134   1.4     rmind 		if (!prop_dictionary_externalize_to_file(npf_dict, plist)) {
    135   1.4     rmind 			error = errno;
    136   1.4     rmind 		}
    137   1.7     rmind 		prop_object_release(npf_dict);
    138   1.7     rmind 		return error;
    139   1.7     rmind 	}
    140   1.7     rmind 
    141   1.7     rmind 	error = prop_dictionary_sendrecv_ioctl(npf_dict, fd,
    142   1.7     rmind 	    IOC_NPF_RELOAD, &ncf->ncf_err);
    143   1.7     rmind 	if (error) {
    144   1.7     rmind 		prop_object_release(npf_dict);
    145   1.7     rmind 		assert(ncf->ncf_err == NULL);
    146   1.7     rmind 		return error;
    147   1.1     rmind 	}
    148   1.7     rmind 
    149   1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err, "errno", &error);
    150   1.1     rmind 	prop_object_release(npf_dict);
    151   1.1     rmind 	return error;
    152   1.1     rmind }
    153   1.1     rmind 
    154   1.8     rmind nl_config_t *
    155   1.8     rmind npf_config_retrieve(int fd, bool *active, bool *loaded)
    156   1.8     rmind {
    157   1.8     rmind 	prop_dictionary_t npf_dict;
    158   1.8     rmind 	nl_config_t *ncf;
    159   1.8     rmind 	int error;
    160   1.8     rmind 
    161   1.8     rmind 	error = prop_dictionary_recv_ioctl(fd, IOC_NPF_GETCONF, &npf_dict);
    162   1.8     rmind 	if (error) {
    163   1.8     rmind 		return NULL;
    164   1.8     rmind 	}
    165   1.8     rmind 	ncf = calloc(1, sizeof(*ncf));
    166   1.8     rmind 	if (ncf == NULL) {
    167   1.8     rmind 		prop_object_release(npf_dict);
    168   1.8     rmind 		return NULL;
    169   1.8     rmind 	}
    170   1.8     rmind 	ncf->ncf_dict = npf_dict;
    171   1.8     rmind 	ncf->ncf_rules_list = prop_dictionary_get(npf_dict, "rules");
    172   1.8     rmind 	ncf->ncf_rproc_list = prop_dictionary_get(npf_dict, "rprocs");
    173   1.8     rmind 	ncf->ncf_table_list = prop_dictionary_get(npf_dict, "tables");
    174   1.8     rmind 	ncf->ncf_nat_list = prop_dictionary_get(npf_dict, "translation");
    175   1.8     rmind 
    176   1.8     rmind 	prop_dictionary_get_bool(npf_dict, "active", active);
    177   1.8     rmind 	*loaded = (ncf->ncf_rules_list != NULL);
    178   1.8     rmind 	return ncf;
    179   1.8     rmind }
    180   1.8     rmind 
    181   1.6     rmind int
    182   1.6     rmind npf_config_flush(int fd)
    183   1.6     rmind {
    184   1.6     rmind 	nl_config_t *ncf;
    185   1.6     rmind 	int error;
    186   1.6     rmind 
    187   1.6     rmind 	ncf = npf_config_create();
    188   1.6     rmind 	if (ncf == NULL) {
    189   1.6     rmind 		return ENOMEM;
    190   1.6     rmind 	}
    191   1.6     rmind 	ncf->ncf_flush = true;
    192   1.6     rmind 	error = npf_config_submit(ncf, fd);
    193   1.6     rmind 	npf_config_destroy(ncf);
    194   1.6     rmind 	return error;
    195   1.6     rmind }
    196   1.6     rmind 
    197   1.1     rmind void
    198   1.7     rmind _npf_config_error(nl_config_t *ncf, nl_error_t *ne)
    199   1.7     rmind {
    200   1.7     rmind 	memset(ne, 0, sizeof(*ne));
    201   1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err, "id", &ne->ne_id);
    202   1.7     rmind 	prop_dictionary_get_cstring(ncf->ncf_err,
    203   1.7     rmind 	    "source-file", &ne->ne_source_file);
    204   1.7     rmind 	prop_dictionary_get_uint32(ncf->ncf_err,
    205   1.7     rmind 	    "source-line", &ne->ne_source_line);
    206   1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err,
    207   1.7     rmind 	    "ncode-error", &ne->ne_ncode_error);
    208   1.7     rmind 	prop_dictionary_get_int32(ncf->ncf_err,
    209   1.7     rmind 	    "ncode-errat", &ne->ne_ncode_errat);
    210   1.7     rmind }
    211   1.7     rmind 
    212   1.7     rmind void
    213   1.1     rmind npf_config_destroy(nl_config_t *ncf)
    214   1.1     rmind {
    215   1.1     rmind 
    216  1.14     rmind 	if (!ncf->ncf_dict) {
    217   1.8     rmind 		prop_object_release(ncf->ncf_rules_list);
    218   1.8     rmind 		prop_object_release(ncf->ncf_rproc_list);
    219   1.8     rmind 		prop_object_release(ncf->ncf_table_list);
    220   1.8     rmind 		prop_object_release(ncf->ncf_nat_list);
    221   1.8     rmind 	}
    222   1.7     rmind 	if (ncf->ncf_err) {
    223   1.7     rmind 		prop_object_release(ncf->ncf_err);
    224   1.7     rmind 	}
    225  1.11     rmind 	if (ncf->ncf_debug) {
    226  1.11     rmind 		prop_object_release(ncf->ncf_debug);
    227  1.11     rmind 	}
    228   1.1     rmind 	free(ncf);
    229   1.1     rmind }
    230   1.1     rmind 
    231   1.4     rmind void
    232   1.4     rmind _npf_config_setsubmit(nl_config_t *ncf, const char *plist_file)
    233   1.4     rmind {
    234   1.4     rmind 
    235   1.4     rmind 	ncf->ncf_plist = plist_file;
    236   1.4     rmind }
    237   1.4     rmind 
    238   1.1     rmind static bool
    239   1.1     rmind _npf_prop_array_lookup(prop_array_t array, const char *key, const char *name)
    240   1.1     rmind {
    241   1.1     rmind 	prop_dictionary_t dict;
    242   1.1     rmind 	prop_object_iterator_t it;
    243   1.1     rmind 
    244   1.1     rmind 	it = prop_array_iterator(array);
    245   1.1     rmind 	while ((dict = prop_object_iterator_next(it)) != NULL) {
    246   1.1     rmind 		const char *lname;
    247   1.1     rmind 		prop_dictionary_get_cstring_nocopy(dict, key, &lname);
    248   1.1     rmind 		if (strcmp(name, lname) == 0)
    249   1.1     rmind 			break;
    250   1.1     rmind 	}
    251   1.1     rmind 	prop_object_iterator_release(it);
    252   1.1     rmind 	return dict ? true : false;
    253   1.1     rmind }
    254   1.1     rmind 
    255   1.1     rmind /*
    256  1.13     rmind  * NPF EXTENSION INTERFACE.
    257  1.13     rmind  */
    258  1.13     rmind 
    259  1.13     rmind nl_ext_t *
    260  1.13     rmind npf_ext_construct(const char *name)
    261  1.13     rmind {
    262  1.13     rmind 	nl_ext_t *ext;
    263  1.13     rmind 
    264  1.13     rmind 	ext = malloc(sizeof(*ext));
    265  1.13     rmind 	if (ext == NULL) {
    266  1.13     rmind 		return NULL;
    267  1.13     rmind 	}
    268  1.13     rmind 	ext->nxt_name = strdup(name);
    269  1.13     rmind 	if (ext->nxt_name == NULL) {
    270  1.13     rmind 		free(ext);
    271  1.13     rmind 		return NULL;
    272  1.13     rmind 	}
    273  1.13     rmind 	ext->nxt_dict = prop_dictionary_create();
    274  1.13     rmind 
    275  1.13     rmind 	return ext;
    276  1.13     rmind }
    277  1.13     rmind 
    278  1.13     rmind void
    279  1.13     rmind npf_ext_param_u32(nl_ext_t *ext, const char *key, uint32_t val)
    280  1.13     rmind {
    281  1.13     rmind 	prop_dictionary_t extdict = ext->nxt_dict;
    282  1.13     rmind 	prop_dictionary_set_uint32(extdict, key, val);
    283  1.13     rmind }
    284  1.13     rmind 
    285  1.13     rmind void
    286  1.13     rmind npf_ext_param_bool(nl_ext_t *ext, const char *key, bool val)
    287  1.13     rmind {
    288  1.13     rmind 	prop_dictionary_t extdict = ext->nxt_dict;
    289  1.13     rmind 	prop_dictionary_set_bool(extdict, key, val);
    290  1.13     rmind }
    291  1.13     rmind 
    292  1.13     rmind /*
    293   1.1     rmind  * RULE INTERFACE.
    294   1.1     rmind  */
    295   1.1     rmind 
    296   1.1     rmind nl_rule_t *
    297   1.1     rmind npf_rule_create(const char *name, uint32_t attr, u_int if_idx)
    298   1.1     rmind {
    299   1.1     rmind 	prop_dictionary_t rldict;
    300   1.1     rmind 	nl_rule_t *rl;
    301   1.1     rmind 
    302   1.5  christos 	rl = malloc(sizeof(*rl));
    303   1.1     rmind 	if (rl == NULL) {
    304   1.1     rmind 		return NULL;
    305   1.1     rmind 	}
    306   1.1     rmind 	rldict = prop_dictionary_create();
    307   1.1     rmind 	if (rldict == NULL) {
    308   1.1     rmind 		free(rl);
    309   1.1     rmind 		return NULL;
    310   1.1     rmind 	}
    311   1.1     rmind 	if (name) {
    312   1.1     rmind 		prop_dictionary_set_cstring(rldict, "name", name);
    313   1.1     rmind 	}
    314   1.1     rmind 	prop_dictionary_set_uint32(rldict, "attributes", attr);
    315   1.1     rmind 
    316   1.1     rmind 	if (if_idx) {
    317   1.1     rmind 		prop_dictionary_set_uint32(rldict, "interface", if_idx);
    318   1.1     rmind 	}
    319   1.1     rmind 	rl->nrl_dict = rldict;
    320   1.1     rmind 	return rl;
    321   1.1     rmind }
    322   1.1     rmind 
    323   1.1     rmind int
    324   1.1     rmind npf_rule_setcode(nl_rule_t *rl, int type, const void *code, size_t sz)
    325   1.1     rmind {
    326   1.1     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    327   1.1     rmind 	prop_data_t cdata;
    328   1.1     rmind 
    329   1.1     rmind 	if (type != NPF_CODE_NCODE) {
    330   1.1     rmind 		return ENOTSUP;
    331   1.1     rmind 	}
    332   1.1     rmind 	cdata = prop_data_create_data(code, sz);
    333   1.1     rmind 	if (cdata == NULL) {
    334   1.1     rmind 		return ENOMEM;
    335   1.1     rmind 	}
    336   1.1     rmind 	prop_dictionary_set(rldict, "ncode", cdata);
    337   1.1     rmind 	prop_object_release(cdata);
    338   1.1     rmind 	return 0;
    339   1.1     rmind }
    340   1.1     rmind 
    341   1.1     rmind int
    342   1.1     rmind npf_rule_setproc(nl_config_t *ncf, nl_rule_t *rl, const char *name)
    343   1.1     rmind {
    344   1.1     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    345   1.1     rmind 
    346   1.1     rmind 	if (!npf_rproc_exists_p(ncf, name)) {
    347   1.1     rmind 		return ENOENT;
    348   1.1     rmind 	}
    349   1.1     rmind 	prop_dictionary_set_cstring(rldict, "rproc", name);
    350   1.1     rmind 	return 0;
    351   1.1     rmind }
    352   1.1     rmind 
    353   1.1     rmind bool
    354   1.1     rmind npf_rule_exists_p(nl_config_t *ncf, const char *name)
    355   1.1     rmind {
    356   1.1     rmind 
    357   1.1     rmind 	return _npf_prop_array_lookup(ncf->ncf_rules_list, "name", name);
    358   1.1     rmind }
    359   1.1     rmind 
    360   1.1     rmind int
    361   1.1     rmind npf_rule_insert(nl_config_t *ncf, nl_rule_t *parent, nl_rule_t *rl, pri_t pri)
    362   1.1     rmind {
    363   1.1     rmind 	prop_dictionary_t rldict = rl->nrl_dict;
    364   1.1     rmind 	prop_array_t rlset;
    365   1.1     rmind 
    366   1.1     rmind 	if (pri == NPF_PRI_NEXT) {
    367   1.1     rmind 		pri = ncf->ncf_rule_pri++;
    368   1.1     rmind 	} else if (ncf) {
    369   1.1     rmind 		ncf->ncf_rule_pri = pri + 1;
    370   1.1     rmind 	}
    371   1.1     rmind 	prop_dictionary_set_int32(rldict, "priority", pri);
    372   1.1     rmind 
    373   1.1     rmind 	if (parent) {
    374   1.1     rmind 		prop_dictionary_t pdict = parent->nrl_dict;
    375   1.1     rmind 		rlset = prop_dictionary_get(pdict, "subrules");
    376   1.1     rmind 		if (rlset == NULL) {
    377   1.1     rmind 			rlset = prop_array_create();
    378   1.1     rmind 			prop_dictionary_set(pdict, "subrules", rlset);
    379   1.1     rmind 			prop_object_release(rlset);
    380   1.1     rmind 		}
    381   1.1     rmind 	} else {
    382   1.1     rmind 		rlset = ncf->ncf_rules_list;
    383   1.1     rmind 	}
    384   1.1     rmind 	prop_array_add(rlset, rldict);
    385   1.1     rmind 	return 0;
    386   1.1     rmind }
    387   1.1     rmind 
    388   1.8     rmind static int
    389   1.8     rmind _npf_rule_foreach1(prop_array_t rules, unsigned nlevel, nl_rule_callback_t func)
    390   1.8     rmind {
    391   1.8     rmind 	prop_dictionary_t rldict;
    392   1.8     rmind 	prop_object_iterator_t it;
    393   1.8     rmind 
    394   1.8     rmind 	if (!rules || prop_object_type(rules) != PROP_TYPE_ARRAY) {
    395   1.8     rmind 		return ENOENT;
    396   1.8     rmind 	}
    397   1.8     rmind 	it = prop_array_iterator(rules);
    398   1.8     rmind 	if (it == NULL) {
    399   1.8     rmind 		return ENOMEM;
    400   1.8     rmind 	}
    401   1.8     rmind 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    402   1.8     rmind 		prop_array_t subrules;
    403   1.8     rmind 		nl_rule_t nrl;
    404   1.8     rmind 
    405   1.8     rmind 		nrl.nrl_dict = rldict;
    406   1.8     rmind 		(*func)(&nrl, nlevel);
    407   1.8     rmind 
    408   1.8     rmind 		subrules = prop_dictionary_get(rldict, "subrules");
    409  1.14     rmind 		if (!subrules) {
    410  1.14     rmind 			continue;
    411  1.14     rmind 		}
    412   1.8     rmind 		(void)_npf_rule_foreach1(subrules, nlevel + 1, func);
    413  1.13     rmind 		prop_object_release(subrules);
    414   1.8     rmind 	}
    415   1.8     rmind 	prop_object_iterator_release(it);
    416   1.8     rmind 	return 0;
    417   1.8     rmind }
    418   1.8     rmind 
    419   1.8     rmind int
    420   1.8     rmind _npf_rule_foreach(nl_config_t *ncf, nl_rule_callback_t func)
    421   1.8     rmind {
    422   1.8     rmind 
    423   1.8     rmind 	return _npf_rule_foreach1(ncf->ncf_rules_list, 0, func);
    424   1.8     rmind }
    425   1.8     rmind 
    426   1.8     rmind pri_t
    427   1.8     rmind _npf_rule_getinfo(nl_rule_t *nrl, const char **rname, uint32_t *attr,
    428   1.8     rmind     u_int *if_idx)
    429   1.8     rmind {
    430   1.8     rmind 	prop_dictionary_t rldict = nrl->nrl_dict;
    431   1.8     rmind 	pri_t prio;
    432   1.8     rmind 
    433   1.8     rmind 	prop_dictionary_get_cstring_nocopy(rldict, "name", rname);
    434   1.8     rmind 	prop_dictionary_get_uint32(rldict, "attributes", attr);
    435   1.8     rmind 	prop_dictionary_get_int32(rldict, "priority", &prio);
    436   1.8     rmind 	prop_dictionary_get_uint32(rldict, "interface", if_idx);
    437   1.8     rmind 	return prio;
    438   1.8     rmind }
    439   1.8     rmind 
    440   1.8     rmind const void *
    441   1.8     rmind _npf_rule_ncode(nl_rule_t *nrl, size_t *size)
    442   1.8     rmind {
    443   1.8     rmind 	prop_dictionary_t rldict = nrl->nrl_dict;
    444   1.8     rmind 	prop_object_t obj = prop_dictionary_get(rldict, "ncode");
    445   1.8     rmind 	*size = prop_data_size(obj);
    446   1.8     rmind 	return prop_data_data_nocopy(obj);
    447   1.8     rmind }
    448   1.8     rmind 
    449   1.8     rmind const char *
    450   1.8     rmind _npf_rule_rproc(nl_rule_t *nrl)
    451   1.8     rmind {
    452   1.8     rmind 	prop_dictionary_t rldict = nrl->nrl_dict;
    453   1.8     rmind 	const char *rpname = NULL;
    454   1.8     rmind 
    455   1.8     rmind 	prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rpname);
    456   1.8     rmind 	return rpname;
    457   1.8     rmind }
    458   1.8     rmind 
    459   1.1     rmind void
    460   1.1     rmind npf_rule_destroy(nl_rule_t *rl)
    461   1.1     rmind {
    462   1.1     rmind 
    463   1.1     rmind 	prop_object_release(rl->nrl_dict);
    464   1.1     rmind 	free(rl);
    465   1.1     rmind }
    466   1.1     rmind 
    467   1.1     rmind /*
    468   1.1     rmind  * RULE PROCEDURE INTERFACE.
    469   1.1     rmind  */
    470   1.1     rmind 
    471   1.1     rmind nl_rproc_t *
    472   1.1     rmind npf_rproc_create(const char *name)
    473   1.1     rmind {
    474   1.1     rmind 	prop_dictionary_t rpdict;
    475  1.13     rmind 	prop_array_t extcalls;
    476   1.1     rmind 	nl_rproc_t *nrp;
    477   1.1     rmind 
    478   1.1     rmind 	nrp = malloc(sizeof(nl_rproc_t));
    479   1.1     rmind 	if (nrp == NULL) {
    480   1.1     rmind 		return NULL;
    481   1.1     rmind 	}
    482   1.1     rmind 	rpdict = prop_dictionary_create();
    483   1.1     rmind 	if (rpdict == NULL) {
    484   1.1     rmind 		free(nrp);
    485   1.1     rmind 		return NULL;
    486   1.1     rmind 	}
    487   1.1     rmind 	prop_dictionary_set_cstring(rpdict, "name", name);
    488  1.13     rmind 
    489  1.13     rmind 	extcalls = prop_array_create();
    490  1.13     rmind 	if (extcalls == NULL) {
    491  1.13     rmind 		prop_object_release(rpdict);
    492  1.13     rmind 		free(nrp);
    493  1.13     rmind 		return NULL;
    494  1.13     rmind 	}
    495  1.13     rmind 	prop_dictionary_set(rpdict, "extcalls", extcalls);
    496  1.13     rmind 	prop_object_release(extcalls);
    497  1.13     rmind 
    498   1.1     rmind 	nrp->nrp_dict = rpdict;
    499   1.1     rmind 	return nrp;
    500   1.1     rmind }
    501   1.1     rmind 
    502   1.1     rmind int
    503  1.13     rmind npf_rproc_extcall(nl_rproc_t *rp, nl_ext_t *ext)
    504   1.1     rmind {
    505   1.1     rmind 	prop_dictionary_t rpdict = rp->nrp_dict;
    506  1.13     rmind 	prop_dictionary_t extdict = ext->nxt_dict;
    507  1.13     rmind 	prop_array_t extcalls;
    508   1.1     rmind 
    509  1.13     rmind 	extcalls = prop_dictionary_get(rpdict, "extcalls");
    510  1.13     rmind 	if (_npf_prop_array_lookup(extcalls, "name", ext->nxt_name)) {
    511  1.13     rmind 		return EEXIST;
    512  1.13     rmind 	}
    513  1.13     rmind 	prop_dictionary_set_cstring(extdict, "name", ext->nxt_name);
    514  1.13     rmind 	prop_array_add(extcalls, extdict);
    515   1.1     rmind 	return 0;
    516   1.1     rmind }
    517   1.1     rmind 
    518  1.13     rmind bool
    519  1.13     rmind npf_rproc_exists_p(nl_config_t *ncf, const char *name)
    520   1.1     rmind {
    521   1.1     rmind 
    522  1.13     rmind 	return _npf_prop_array_lookup(ncf->ncf_rproc_list, "name", name);
    523   1.1     rmind }
    524   1.1     rmind 
    525   1.1     rmind int
    526   1.1     rmind npf_rproc_insert(nl_config_t *ncf, nl_rproc_t *rp)
    527   1.1     rmind {
    528   1.1     rmind 	prop_dictionary_t rpdict = rp->nrp_dict;
    529   1.1     rmind 	const char *name;
    530   1.1     rmind 
    531   1.1     rmind 	if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
    532   1.1     rmind 		return EINVAL;
    533   1.1     rmind 	}
    534   1.1     rmind 	if (npf_rproc_exists_p(ncf, name)) {
    535   1.1     rmind 		return EEXIST;
    536   1.1     rmind 	}
    537   1.1     rmind 	prop_array_add(ncf->ncf_rproc_list, rpdict);
    538   1.1     rmind 	return 0;
    539   1.1     rmind }
    540   1.1     rmind 
    541   1.1     rmind /*
    542   1.1     rmind  * TRANSLATION INTERFACE.
    543   1.1     rmind  */
    544   1.1     rmind 
    545   1.1     rmind nl_nat_t *
    546   1.5  christos npf_nat_create(int type, u_int flags, u_int if_idx,
    547   1.1     rmind     npf_addr_t *addr, int af, in_port_t port)
    548   1.1     rmind {
    549   1.1     rmind 	nl_rule_t *rl;
    550   1.1     rmind 	prop_dictionary_t rldict;
    551   1.1     rmind 	prop_data_t addrdat;
    552   1.1     rmind 	uint32_t attr;
    553   1.1     rmind 	size_t sz;
    554   1.1     rmind 
    555   1.1     rmind 	if (af == AF_INET) {
    556   1.1     rmind 		sz = sizeof(struct in_addr);
    557   1.1     rmind 	} else if (af == AF_INET6) {
    558   1.1     rmind 		sz = sizeof(struct in6_addr);
    559   1.1     rmind 	} else {
    560   1.1     rmind 		return NULL;
    561   1.1     rmind 	}
    562   1.1     rmind 
    563   1.1     rmind 	attr = NPF_RULE_PASS | NPF_RULE_FINAL |
    564   1.2     rmind 	    (type == NPF_NATOUT ? NPF_RULE_OUT : NPF_RULE_IN);
    565   1.1     rmind 
    566   1.1     rmind 	/* Create a rule for NAT policy.  Next, will add translation data. */
    567   1.1     rmind 	rl = npf_rule_create(NULL, attr, if_idx);
    568   1.1     rmind 	if (rl == NULL) {
    569   1.1     rmind 		return NULL;
    570   1.1     rmind 	}
    571   1.1     rmind 	rldict = rl->nrl_dict;
    572   1.1     rmind 
    573   1.1     rmind 	/* Translation type and flags. */
    574   1.1     rmind 	prop_dictionary_set_int32(rldict, "type", type);
    575   1.1     rmind 	prop_dictionary_set_uint32(rldict, "flags", flags);
    576   1.1     rmind 
    577   1.1     rmind 	/* Translation IP. */
    578   1.1     rmind 	addrdat = prop_data_create_data(addr, sz);
    579   1.1     rmind 	if (addrdat == NULL) {
    580   1.1     rmind 		npf_rule_destroy(rl);
    581   1.1     rmind 		return NULL;
    582   1.1     rmind 	}
    583   1.1     rmind 	prop_dictionary_set(rldict, "translation-ip", addrdat);
    584   1.1     rmind 	prop_object_release(addrdat);
    585   1.1     rmind 
    586   1.1     rmind 	/* Translation port (for redirect case). */
    587   1.1     rmind 	prop_dictionary_set_uint16(rldict, "translation-port", port);
    588   1.1     rmind 
    589   1.1     rmind 	return (nl_nat_t *)rl;
    590   1.1     rmind }
    591   1.1     rmind 
    592   1.1     rmind int
    593   1.1     rmind npf_nat_insert(nl_config_t *ncf, nl_nat_t *nt, pri_t pri)
    594   1.1     rmind {
    595   1.1     rmind 	prop_dictionary_t rldict = nt->nrl_dict;
    596   1.1     rmind 
    597   1.1     rmind 	if (pri == NPF_PRI_NEXT) {
    598   1.1     rmind 		pri = ncf->ncf_nat_pri++;
    599   1.1     rmind 	} else {
    600   1.1     rmind 		ncf->ncf_nat_pri = pri + 1;
    601   1.1     rmind 	}
    602   1.1     rmind 	prop_dictionary_set_int32(rldict, "priority", pri);
    603   1.1     rmind 	prop_array_add(ncf->ncf_nat_list, rldict);
    604   1.1     rmind 	return 0;
    605   1.1     rmind }
    606   1.1     rmind 
    607   1.9     rmind int
    608   1.9     rmind _npf_nat_foreach(nl_config_t *ncf, nl_rule_callback_t func)
    609   1.9     rmind {
    610   1.9     rmind 
    611   1.9     rmind 	return _npf_rule_foreach1(ncf->ncf_nat_list, 0, func);
    612   1.9     rmind }
    613   1.9     rmind 
    614   1.9     rmind void
    615   1.9     rmind _npf_nat_getinfo(nl_nat_t *nt, int *type, u_int *flags, npf_addr_t *addr,
    616   1.9     rmind     size_t *alen, in_port_t *port)
    617   1.9     rmind {
    618   1.9     rmind 	prop_dictionary_t rldict = nt->nrl_dict;
    619   1.9     rmind 
    620   1.9     rmind 	prop_dictionary_get_int32(rldict, "type", type);
    621   1.9     rmind 	prop_dictionary_get_uint32(rldict, "flags", flags);
    622   1.9     rmind 
    623   1.9     rmind 	prop_object_t obj = prop_dictionary_get(rldict, "translation-ip");
    624   1.9     rmind 	*alen = prop_data_size(obj);
    625   1.9     rmind 	memcpy(addr, prop_data_data_nocopy(obj), *alen);
    626   1.9     rmind 
    627   1.9     rmind 	prop_dictionary_get_uint16(rldict, "translation-port", port);
    628   1.9     rmind }
    629   1.9     rmind 
    630   1.1     rmind /*
    631   1.1     rmind  * TABLE INTERFACE.
    632   1.1     rmind  */
    633   1.1     rmind 
    634   1.1     rmind nl_table_t *
    635   1.5  christos npf_table_create(u_int id, int type)
    636   1.1     rmind {
    637   1.1     rmind 	prop_dictionary_t tldict;
    638   1.1     rmind 	prop_array_t tblents;
    639   1.1     rmind 	nl_table_t *tl;
    640   1.1     rmind 
    641   1.5  christos 	tl = malloc(sizeof(*tl));
    642   1.1     rmind 	if (tl == NULL) {
    643   1.1     rmind 		return NULL;
    644   1.1     rmind 	}
    645   1.1     rmind 	tldict = prop_dictionary_create();
    646   1.1     rmind 	if (tldict == NULL) {
    647   1.1     rmind 		free(tl);
    648   1.1     rmind 		return NULL;
    649   1.1     rmind 	}
    650   1.1     rmind 	prop_dictionary_set_uint32(tldict, "id", id);
    651   1.1     rmind 	prop_dictionary_set_int32(tldict, "type", type);
    652   1.1     rmind 
    653   1.1     rmind 	tblents = prop_array_create();
    654   1.1     rmind 	if (tblents == NULL) {
    655   1.1     rmind 		prop_object_release(tldict);
    656   1.1     rmind 		free(tl);
    657   1.1     rmind 		return NULL;
    658   1.1     rmind 	}
    659   1.1     rmind 	prop_dictionary_set(tldict, "entries", tblents);
    660   1.1     rmind 	prop_object_release(tblents);
    661   1.1     rmind 
    662   1.1     rmind 	tl->ntl_dict = tldict;
    663   1.1     rmind 	return tl;
    664   1.1     rmind }
    665   1.1     rmind 
    666   1.1     rmind int
    667  1.10     rmind npf_table_add_entry(nl_table_t *tl, const int alen,
    668  1.10     rmind     const npf_addr_t *addr, const npf_netmask_t mask)
    669   1.1     rmind {
    670   1.1     rmind 	prop_dictionary_t tldict = tl->ntl_dict, entdict;
    671   1.1     rmind 	prop_array_t tblents;
    672   1.3    zoltan 	prop_data_t addrdata;
    673   1.1     rmind 
    674   1.1     rmind 	/* Create the table entry. */
    675   1.1     rmind 	entdict = prop_dictionary_create();
    676  1.10     rmind 	if (entdict == NULL) {
    677   1.1     rmind 		return ENOMEM;
    678   1.1     rmind 	}
    679  1.10     rmind 	addrdata = prop_data_create_data(addr, alen);
    680   1.3    zoltan 	prop_dictionary_set(entdict, "addr", addrdata);
    681   1.3    zoltan 	prop_dictionary_set_uint8(entdict, "mask", mask);
    682   1.3    zoltan 	prop_object_release(addrdata);
    683   1.1     rmind 
    684   1.1     rmind 	/* Insert the entry. */
    685   1.1     rmind 	tblents = prop_dictionary_get(tldict, "entries");
    686   1.1     rmind 	prop_array_add(tblents, entdict);
    687   1.1     rmind 	prop_object_release(entdict);
    688   1.1     rmind 	return 0;
    689   1.1     rmind }
    690   1.1     rmind 
    691   1.1     rmind bool
    692   1.1     rmind npf_table_exists_p(nl_config_t *ncf, u_int tid)
    693   1.1     rmind {
    694   1.1     rmind 	prop_dictionary_t tldict;
    695   1.1     rmind 	prop_object_iterator_t it;
    696   1.1     rmind 
    697   1.1     rmind 	it = prop_array_iterator(ncf->ncf_table_list);
    698   1.1     rmind 	while ((tldict = prop_object_iterator_next(it)) != NULL) {
    699   1.1     rmind 		u_int i;
    700   1.1     rmind 		if (prop_dictionary_get_uint32(tldict, "id", &i) && tid == i)
    701   1.1     rmind 			break;
    702   1.1     rmind 	}
    703   1.1     rmind 	prop_object_iterator_release(it);
    704   1.1     rmind 	return tldict ? true : false;
    705   1.1     rmind }
    706   1.1     rmind 
    707   1.1     rmind int
    708   1.1     rmind npf_table_insert(nl_config_t *ncf, nl_table_t *tl)
    709   1.1     rmind {
    710   1.1     rmind 	prop_dictionary_t tldict = tl->ntl_dict;
    711   1.1     rmind 	u_int tid;
    712   1.1     rmind 
    713   1.1     rmind 	if (!prop_dictionary_get_uint32(tldict, "id", &tid)) {
    714   1.1     rmind 		return EINVAL;
    715   1.1     rmind 	}
    716   1.1     rmind 	if (npf_table_exists_p(ncf, tid)) {
    717   1.1     rmind 		return EEXIST;
    718   1.1     rmind 	}
    719   1.1     rmind 	prop_array_add(ncf->ncf_table_list, tldict);
    720   1.1     rmind 	return 0;
    721   1.1     rmind }
    722   1.1     rmind 
    723   1.1     rmind void
    724   1.1     rmind npf_table_destroy(nl_table_t *tl)
    725   1.1     rmind {
    726   1.1     rmind 
    727   1.1     rmind 	prop_object_release(tl->ntl_dict);
    728   1.1     rmind 	free(tl);
    729   1.1     rmind }
    730   1.1     rmind 
    731   1.9     rmind void
    732   1.9     rmind _npf_table_foreach(nl_config_t *ncf, nl_table_callback_t func)
    733   1.9     rmind {
    734   1.9     rmind 	prop_dictionary_t tldict;
    735   1.9     rmind 	prop_object_iterator_t it;
    736   1.9     rmind 
    737   1.9     rmind 	it = prop_array_iterator(ncf->ncf_table_list);
    738   1.9     rmind 	while ((tldict = prop_object_iterator_next(it)) != NULL) {
    739   1.9     rmind 		u_int id;
    740   1.9     rmind 		int type;
    741   1.9     rmind 
    742   1.9     rmind 		prop_dictionary_get_uint32(tldict, "id", &id);
    743   1.9     rmind 		prop_dictionary_get_int32(tldict, "type", &type);
    744   1.9     rmind 		(*func)(id, type);
    745   1.9     rmind 	}
    746   1.9     rmind 	prop_object_iterator_release(it);
    747   1.9     rmind }
    748   1.9     rmind 
    749   1.1     rmind /*
    750   1.1     rmind  * MISC.
    751   1.1     rmind  */
    752   1.1     rmind 
    753   1.1     rmind int
    754   1.5  christos npf_update_rule(int fd, const char *rname __unused, nl_rule_t *rl)
    755   1.1     rmind {
    756   1.7     rmind 	prop_dictionary_t rldict = rl->nrl_dict, errdict = NULL;
    757   1.1     rmind 	int error;
    758   1.1     rmind 
    759   1.7     rmind 	error = prop_dictionary_sendrecv_ioctl(rldict, fd,
    760   1.7     rmind 	    IOC_NPF_UPDATE_RULE, &errdict);
    761   1.7     rmind 	if (errdict) {
    762   1.7     rmind 		prop_object_release(errdict);
    763   1.7     rmind 	}
    764   1.1     rmind 	return error;
    765   1.1     rmind }
    766   1.1     rmind 
    767   1.1     rmind int
    768   1.1     rmind npf_sessions_recv(int fd, const char *fpath)
    769   1.1     rmind {
    770   1.1     rmind 	prop_dictionary_t sdict;
    771   1.1     rmind 	int error;
    772   1.1     rmind 
    773   1.1     rmind 	error = prop_dictionary_recv_ioctl(fd, IOC_NPF_SESSIONS_SAVE, &sdict);
    774   1.1     rmind 	if (error) {
    775   1.1     rmind 		return error;
    776   1.1     rmind 	}
    777   1.1     rmind 	if (!prop_dictionary_externalize_to_file(sdict, fpath)) {
    778   1.1     rmind 		error = errno;
    779   1.1     rmind 	}
    780   1.1     rmind 	prop_object_release(sdict);
    781   1.1     rmind 	return error;
    782   1.1     rmind }
    783   1.1     rmind 
    784   1.1     rmind int
    785   1.1     rmind npf_sessions_send(int fd, const char *fpath)
    786   1.1     rmind {
    787   1.1     rmind 	prop_dictionary_t sdict;
    788   1.1     rmind 	int error;
    789   1.1     rmind 
    790   1.1     rmind 	if (fpath) {
    791   1.1     rmind 		sdict = prop_dictionary_internalize_from_file(fpath);
    792   1.1     rmind 		if (sdict == NULL) {
    793   1.1     rmind 			return errno;
    794   1.1     rmind 		}
    795   1.1     rmind 	} else {
    796   1.1     rmind 		/* Empty: will flush the sessions. */
    797   1.1     rmind 		prop_array_t selist = prop_array_create();
    798   1.1     rmind 		sdict = prop_dictionary_create();
    799   1.1     rmind 		prop_dictionary_set(sdict, "session-list", selist);
    800   1.1     rmind 		prop_object_release(selist);
    801   1.1     rmind 	}
    802   1.1     rmind 	error = prop_dictionary_send_ioctl(sdict, fd, IOC_NPF_SESSIONS_LOAD);
    803   1.1     rmind 	prop_object_release(sdict);
    804   1.1     rmind 	return error;
    805   1.1     rmind }
    806  1.11     rmind 
    807  1.11     rmind static prop_dictionary_t
    808  1.11     rmind _npf_debug_initonce(nl_config_t *ncf)
    809  1.11     rmind {
    810  1.11     rmind 	if (!ncf->ncf_debug) {
    811  1.11     rmind 		prop_array_t iflist = prop_array_create();
    812  1.11     rmind 		ncf->ncf_debug = prop_dictionary_create();
    813  1.11     rmind 		prop_dictionary_set(ncf->ncf_debug, "interfaces", iflist);
    814  1.11     rmind 		prop_object_release(iflist);
    815  1.11     rmind 	}
    816  1.11     rmind 	return ncf->ncf_debug;
    817  1.11     rmind }
    818  1.11     rmind 
    819  1.11     rmind void
    820  1.11     rmind _npf_debug_addif(nl_config_t *ncf, struct ifaddrs *ifa, u_int if_idx)
    821  1.11     rmind {
    822  1.11     rmind 	prop_dictionary_t ifdict, dbg = _npf_debug_initonce(ncf);
    823  1.11     rmind 	prop_array_t iflist = prop_dictionary_get(dbg, "interfaces");
    824  1.11     rmind 
    825  1.11     rmind 	if (_npf_prop_array_lookup(iflist, "name", ifa->ifa_name)) {
    826  1.11     rmind 		return;
    827  1.11     rmind 	}
    828  1.11     rmind 
    829  1.11     rmind 	ifdict = prop_dictionary_create();
    830  1.11     rmind 	prop_dictionary_set_cstring(ifdict, "name", ifa->ifa_name);
    831  1.11     rmind 	prop_dictionary_set_uint32(ifdict, "flags", ifa->ifa_flags);
    832  1.11     rmind 	if (!if_idx) {
    833  1.11     rmind 		if_idx = if_nametoindex(ifa->ifa_name);
    834  1.11     rmind 	}
    835  1.11     rmind 	prop_dictionary_set_uint32(ifdict, "idx", if_idx);
    836  1.11     rmind 
    837  1.11     rmind 	const struct sockaddr *sa = ifa->ifa_addr;
    838  1.11     rmind 	npf_addr_t addr;
    839  1.11     rmind 	size_t alen = 0;
    840  1.11     rmind 
    841  1.11     rmind 	switch (sa ? sa->sa_family : -1) {
    842  1.11     rmind 	case AF_INET: {
    843  1.11     rmind 		const struct sockaddr_in *sin = (const void *)sa;
    844  1.11     rmind 		alen = sizeof(sin->sin_addr);
    845  1.11     rmind 		memcpy(&addr, &sin->sin_addr, alen);
    846  1.11     rmind 		break;
    847  1.11     rmind 	}
    848  1.11     rmind 	case AF_INET6: {
    849  1.11     rmind 		const struct sockaddr_in6 *sin6 = (const void *)sa;
    850  1.11     rmind 		alen = sizeof(sin6->sin6_addr);
    851  1.11     rmind 		memcpy(&addr, &sin6->sin6_addr, alen);
    852  1.11     rmind 		break;
    853  1.11     rmind 	}
    854  1.11     rmind 	default:
    855  1.11     rmind 		break;
    856  1.11     rmind 	}
    857  1.11     rmind 
    858  1.11     rmind 	if (alen) {
    859  1.11     rmind 		prop_data_t addrdata = prop_data_create_data(&addr, alen);
    860  1.11     rmind 		prop_dictionary_set(ifdict, "addr", addrdata);
    861  1.11     rmind 		prop_object_release(addrdata);
    862  1.11     rmind 	}
    863  1.11     rmind 	prop_array_add(iflist, ifdict);
    864  1.11     rmind 	prop_object_release(ifdict);
    865  1.11     rmind }
    866