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