Home | History | Annotate | Line # | Download | only in npf
npf_ctl.c revision 1.19
      1  1.19   rmind /*	$NetBSD: npf_ctl.c,v 1.19 2012/10/29 02:27:12 rmind Exp $	*/
      2   1.1   rmind 
      3   1.1   rmind /*-
      4  1.13   rmind  * Copyright (c) 2009-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 /*
     33   1.1   rmind  * NPF device control.
     34   1.1   rmind  *
     35   1.1   rmind  * Implementation of (re)loading, construction of tables and rules.
     36   1.1   rmind  * NPF proplib(9) dictionary consumer.
     37   1.1   rmind  */
     38   1.1   rmind 
     39   1.1   rmind #include <sys/cdefs.h>
     40  1.19   rmind __KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.19 2012/10/29 02:27:12 rmind Exp $");
     41   1.1   rmind 
     42   1.1   rmind #include <sys/param.h>
     43   1.1   rmind #include <sys/conf.h>
     44   1.1   rmind 
     45   1.1   rmind #include <prop/proplib.h>
     46   1.1   rmind 
     47   1.1   rmind #include "npf_ncode.h"
     48   1.1   rmind #include "npf_impl.h"
     49   1.1   rmind 
     50  1.12   rmind #if defined(DEBUG) || defined(DIAGNOSTIC)
     51  1.12   rmind #define	NPF_ERR_DEBUG(e) \
     52  1.12   rmind 	prop_dictionary_set_cstring_nocopy((e), "source-file", __FILE__); \
     53  1.12   rmind 	prop_dictionary_set_uint32((e), "source-line", __LINE__);
     54  1.12   rmind #else
     55  1.12   rmind #define	NPF_ERR_DEBUG(e)
     56  1.12   rmind #endif
     57  1.12   rmind 
     58   1.1   rmind /*
     59   1.1   rmind  * npfctl_switch: enable or disable packet inspection.
     60   1.1   rmind  */
     61   1.1   rmind int
     62   1.1   rmind npfctl_switch(void *data)
     63   1.1   rmind {
     64   1.1   rmind 	const bool onoff = *(int *)data ? true : false;
     65   1.1   rmind 	int error;
     66   1.1   rmind 
     67   1.1   rmind 	if (onoff) {
     68   1.1   rmind 		/* Enable: add pfil hooks. */
     69  1.14   rmind 		error = npf_pfil_register();
     70   1.1   rmind 	} else {
     71   1.1   rmind 		/* Disable: remove pfil hooks. */
     72  1.14   rmind 		npf_pfil_unregister();
     73   1.1   rmind 		error = 0;
     74   1.1   rmind 	}
     75   1.1   rmind 	return error;
     76   1.1   rmind }
     77   1.1   rmind 
     78   1.6   rmind static int __noinline
     79  1.12   rmind npf_mk_tables(npf_tableset_t *tblset, prop_array_t tables,
     80  1.12   rmind     prop_dictionary_t errdict)
     81   1.1   rmind {
     82   1.1   rmind 	prop_object_iterator_t it;
     83   1.1   rmind 	prop_dictionary_t tbldict;
     84   1.1   rmind 	int error = 0;
     85   1.1   rmind 
     86   1.1   rmind 	/* Tables - array. */
     87  1.12   rmind 	if (prop_object_type(tables) != PROP_TYPE_ARRAY) {
     88  1.12   rmind 		NPF_ERR_DEBUG(errdict);
     89   1.1   rmind 		return EINVAL;
     90  1.12   rmind 	}
     91   1.1   rmind 
     92   1.1   rmind 	it = prop_array_iterator(tables);
     93   1.1   rmind 	while ((tbldict = prop_object_iterator_next(it)) != NULL) {
     94   1.1   rmind 		prop_dictionary_t ent;
     95   1.1   rmind 		prop_object_iterator_t eit;
     96   1.1   rmind 		prop_array_t entries;
     97   1.1   rmind 		npf_table_t *t;
     98   1.1   rmind 		u_int tid;
     99   1.1   rmind 		int type;
    100   1.1   rmind 
    101   1.1   rmind 		/* Table - dictionary. */
    102   1.1   rmind 		if (prop_object_type(tbldict) != PROP_TYPE_DICTIONARY) {
    103  1.12   rmind 			NPF_ERR_DEBUG(errdict);
    104   1.1   rmind 			error = EINVAL;
    105   1.1   rmind 			break;
    106   1.1   rmind 		}
    107   1.1   rmind 
    108   1.1   rmind 		/* Table ID and type. */
    109   1.6   rmind 		prop_dictionary_get_uint32(tbldict, "id", &tid);
    110   1.6   rmind 		prop_dictionary_get_int32(tbldict, "type", &type);
    111   1.6   rmind 
    112  1.12   rmind 		/* Validate them, check for duplicate IDs. */
    113   1.1   rmind 		error = npf_table_check(tblset, tid, type);
    114   1.1   rmind 		if (error)
    115   1.1   rmind 			break;
    116   1.1   rmind 
    117   1.1   rmind 		/* Create and insert the table. */
    118   1.1   rmind 		t = npf_table_create(tid, type, 1024);	/* XXX */
    119   1.1   rmind 		if (t == NULL) {
    120  1.12   rmind 			NPF_ERR_DEBUG(errdict);
    121   1.1   rmind 			error = ENOMEM;
    122   1.1   rmind 			break;
    123   1.1   rmind 		}
    124   1.1   rmind 		error = npf_tableset_insert(tblset, t);
    125   1.1   rmind 		KASSERT(error == 0);
    126   1.1   rmind 
    127   1.1   rmind 		/* Entries. */
    128   1.1   rmind 		entries = prop_dictionary_get(tbldict, "entries");
    129   1.1   rmind 		if (prop_object_type(entries) != PROP_TYPE_ARRAY) {
    130  1.12   rmind 			NPF_ERR_DEBUG(errdict);
    131   1.1   rmind 			error = EINVAL;
    132   1.1   rmind 			break;
    133   1.1   rmind 		}
    134   1.1   rmind 		eit = prop_array_iterator(entries);
    135   1.1   rmind 		while ((ent = prop_object_iterator_next(eit)) != NULL) {
    136   1.7  zoltan 			const npf_addr_t *addr;
    137   1.9   rmind 			npf_netmask_t mask;
    138  1.16   rmind 			int alen;
    139   1.1   rmind 
    140   1.6   rmind 			/* Get address and mask.  Add a table entry. */
    141  1.16   rmind 			prop_object_t obj = prop_dictionary_get(ent, "addr");
    142  1.16   rmind 			addr = (const npf_addr_t *)prop_data_data_nocopy(obj);
    143   1.7  zoltan 			prop_dictionary_get_uint8(ent, "mask", &mask);
    144  1.16   rmind 			alen = prop_data_size(obj);
    145  1.16   rmind 
    146  1.16   rmind 			error = npf_table_insert(tblset, tid, alen, addr, mask);
    147   1.1   rmind 			if (error)
    148   1.1   rmind 				break;
    149   1.1   rmind 		}
    150   1.1   rmind 		prop_object_iterator_release(eit);
    151   1.1   rmind 		if (error)
    152   1.1   rmind 			break;
    153   1.1   rmind 	}
    154   1.1   rmind 	prop_object_iterator_release(it);
    155   1.1   rmind 	/*
    156   1.4   rmind 	 * Note: in a case of error, caller will free the tableset.
    157   1.1   rmind 	 */
    158   1.1   rmind 	return error;
    159   1.1   rmind }
    160   1.1   rmind 
    161   1.5   rmind static npf_rproc_t *
    162   1.6   rmind npf_mk_rproc(prop_array_t rprocs, const char *rpname)
    163   1.5   rmind {
    164   1.5   rmind 	prop_object_iterator_t it;
    165  1.18   rmind 	prop_dictionary_t rpdict, extdict;
    166  1.18   rmind 	prop_array_t extlist;
    167   1.5   rmind 	npf_rproc_t *rp;
    168  1.18   rmind 	const char *name;
    169  1.17   rmind 	uint64_t rpval;
    170   1.5   rmind 
    171   1.5   rmind 	it = prop_array_iterator(rprocs);
    172   1.5   rmind 	while ((rpdict = prop_object_iterator_next(it)) != NULL) {
    173  1.18   rmind 		prop_dictionary_get_cstring_nocopy(rpdict, "name", &name);
    174  1.18   rmind 		KASSERT(name != NULL);
    175  1.18   rmind 		if (strcmp(rpname, name) == 0)
    176   1.5   rmind 			break;
    177   1.5   rmind 	}
    178   1.6   rmind 	prop_object_iterator_release(it);
    179  1.18   rmind 	if (!rpdict) {
    180   1.5   rmind 		return NULL;
    181   1.5   rmind 	}
    182   1.5   rmind 	CTASSERT(sizeof(uintptr_t) <= sizeof(uint64_t));
    183  1.18   rmind 	if (prop_dictionary_get_uint64(rpdict, "rproc-ptr", &rpval)) {
    184  1.18   rmind 		return (npf_rproc_t *)(uintptr_t)rpval;
    185  1.18   rmind 	}
    186  1.18   rmind 
    187  1.18   rmind 	extlist = prop_dictionary_get(rpdict, "extcalls");
    188  1.18   rmind 	if (prop_object_type(extlist) != PROP_TYPE_ARRAY) {
    189  1.18   rmind 		return NULL;
    190  1.18   rmind 	}
    191  1.18   rmind 
    192  1.18   rmind 	rp = npf_rproc_create(rpdict);
    193  1.18   rmind 	if (!rp) {
    194  1.18   rmind 		return NULL;
    195  1.18   rmind 	}
    196  1.18   rmind 	it = prop_array_iterator(extlist);
    197  1.18   rmind 	while ((extdict = prop_object_iterator_next(it)) != NULL) {
    198  1.18   rmind 		if (!prop_dictionary_get_cstring_nocopy(extdict,
    199  1.18   rmind 		    "name", &name) || npf_ext_construct(name, rp, extdict)) {
    200  1.18   rmind 			npf_rproc_release(rp);
    201  1.18   rmind 			rp = NULL;
    202  1.18   rmind 			break;
    203  1.18   rmind 		}
    204  1.18   rmind 	}
    205  1.18   rmind 	prop_object_iterator_release(it);
    206  1.18   rmind 
    207  1.18   rmind 	if (rp) {
    208  1.17   rmind 		rpval = (uint64_t)(uintptr_t)rp;
    209  1.17   rmind 		prop_dictionary_set_uint64(rpdict, "rproc-ptr", rpval);
    210   1.5   rmind 	}
    211   1.5   rmind 	return rp;
    212   1.5   rmind }
    213   1.5   rmind 
    214   1.6   rmind static int __noinline
    215  1.12   rmind npf_mk_ncode(prop_object_t obj, void **code, size_t *csize,
    216  1.12   rmind     prop_dictionary_t errdict)
    217  1.12   rmind {
    218  1.12   rmind 	const void *ncptr;
    219  1.12   rmind 	int nc_err, errat;
    220  1.12   rmind 	size_t nc_size;
    221  1.12   rmind 	void *nc;
    222  1.12   rmind 
    223  1.12   rmind 	/*
    224  1.12   rmind 	 * Allocate, copy and validate n-code. XXX: Inefficient.
    225  1.12   rmind 	 */
    226  1.12   rmind 	ncptr = prop_data_data_nocopy(obj);
    227  1.12   rmind 	nc_size = prop_data_size(obj);
    228  1.12   rmind 	if (ncptr == NULL || nc_size > NPF_NCODE_LIMIT) {
    229  1.12   rmind 		NPF_ERR_DEBUG(errdict);
    230  1.12   rmind 		return ERANGE;
    231  1.12   rmind 	}
    232  1.12   rmind 	nc = npf_ncode_alloc(nc_size);
    233  1.12   rmind 	if (nc == NULL) {
    234  1.12   rmind 		NPF_ERR_DEBUG(errdict);
    235  1.12   rmind 		return ENOMEM;
    236  1.12   rmind 	}
    237  1.12   rmind 	memcpy(nc, ncptr, nc_size);
    238  1.12   rmind 	nc_err = npf_ncode_validate(nc, nc_size, &errat);
    239  1.12   rmind 	if (nc_err) {
    240  1.12   rmind 		npf_ncode_free(nc, nc_size);
    241  1.12   rmind 		prop_dictionary_set_int32(errdict, "ncode-error", nc_err);
    242  1.12   rmind 		prop_dictionary_set_int32(errdict, "ncode-errat", errat);
    243  1.12   rmind 		return EINVAL;
    244  1.12   rmind 	}
    245  1.12   rmind 	*code = nc;
    246  1.12   rmind 	*csize = nc_size;
    247  1.12   rmind 	return 0;
    248  1.12   rmind }
    249  1.12   rmind 
    250  1.12   rmind static int __noinline
    251  1.12   rmind npf_mk_singlerule(prop_dictionary_t rldict, prop_array_t rps, npf_rule_t **rl,
    252  1.12   rmind     prop_dictionary_t errdict)
    253   1.1   rmind {
    254   1.6   rmind 	const char *rnm;
    255   1.5   rmind 	npf_rproc_t *rp;
    256   1.1   rmind 	prop_object_t obj;
    257   1.1   rmind 	size_t nc_size;
    258   1.1   rmind 	void *nc;
    259  1.12   rmind 	int p, error;
    260   1.1   rmind 
    261   1.1   rmind 	/* Rule - dictionary. */
    262  1.12   rmind 	if (prop_object_type(rldict) != PROP_TYPE_DICTIONARY) {
    263  1.12   rmind 		NPF_ERR_DEBUG(errdict);
    264   1.1   rmind 		return EINVAL;
    265  1.12   rmind 	}
    266   1.1   rmind 
    267  1.18   rmind 	/* Make the rule procedure, if any. */
    268  1.18   rmind 	if (rps && prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rnm)) {
    269  1.18   rmind 		rp = npf_mk_rproc(rps, rnm);
    270  1.18   rmind 		if (rp == NULL) {
    271  1.18   rmind 			NPF_ERR_DEBUG(errdict);
    272  1.18   rmind 			error = EINVAL;
    273  1.18   rmind 			goto err;
    274  1.18   rmind 		}
    275  1.18   rmind 	} else {
    276  1.18   rmind 		rp = NULL;
    277  1.18   rmind 	}
    278  1.18   rmind 
    279  1.12   rmind 	error = 0;
    280   1.1   rmind 	obj = prop_dictionary_get(rldict, "ncode");
    281   1.1   rmind 	if (obj) {
    282  1.12   rmind 		/* N-code (binary data). */
    283  1.12   rmind 		error = npf_mk_ncode(obj, &nc, &nc_size, errdict);
    284  1.12   rmind 		if (error) {
    285  1.12   rmind 			goto err;
    286   1.4   rmind 		}
    287   1.1   rmind 	} else {
    288   1.1   rmind 		/* No n-code. */
    289   1.1   rmind 		nc = NULL;
    290   1.1   rmind 		nc_size = 0;
    291   1.1   rmind 	}
    292   1.1   rmind 
    293   1.6   rmind 	/* Finally, allocate and return the rule. */
    294   1.6   rmind 	*rl = npf_rule_alloc(rldict, rp, nc, nc_size);
    295  1.12   rmind 	KASSERT(*rl != NULL);
    296   1.6   rmind 	return 0;
    297  1.12   rmind err:
    298  1.18   rmind 	if (rp) {
    299  1.18   rmind 		npf_rproc_release(rp);
    300  1.18   rmind 	}
    301  1.12   rmind 	prop_dictionary_get_int32(rldict, "priority", &p); /* XXX */
    302  1.12   rmind 	prop_dictionary_set_int32(errdict, "id", p);
    303  1.12   rmind 	return error;
    304   1.6   rmind }
    305   1.6   rmind 
    306   1.6   rmind static int __noinline
    307  1.12   rmind npf_mk_subrules(npf_ruleset_t *rlset, prop_array_t rules, prop_array_t rprocs,
    308  1.12   rmind     prop_dictionary_t errdict)
    309   1.6   rmind {
    310   1.6   rmind 	prop_object_iterator_t it;
    311   1.6   rmind 	prop_dictionary_t rldict;
    312   1.6   rmind 	int error = 0;
    313   1.6   rmind 
    314   1.6   rmind 	if (prop_object_type(rules) != PROP_TYPE_ARRAY) {
    315  1.12   rmind 		NPF_ERR_DEBUG(errdict);
    316   1.6   rmind 		return EINVAL;
    317   1.6   rmind 	}
    318   1.6   rmind 	it = prop_array_iterator(rules);
    319   1.6   rmind 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    320   1.6   rmind 		npf_rule_t *rl;
    321  1.12   rmind 		error = npf_mk_singlerule(rldict, rprocs, &rl, errdict);
    322   1.6   rmind 		if (error) {
    323   1.6   rmind 			break;
    324   1.6   rmind 		}
    325   1.6   rmind 		npf_ruleset_insert(rlset, rl);
    326   1.1   rmind 	}
    327   1.6   rmind 	prop_object_iterator_release(it);
    328   1.6   rmind 	return error;
    329   1.1   rmind }
    330   1.1   rmind 
    331   1.6   rmind static int __noinline
    332  1.12   rmind npf_mk_rules(npf_ruleset_t *rlset, prop_array_t rules, prop_array_t rprocs,
    333  1.12   rmind     prop_dictionary_t errdict)
    334   1.1   rmind {
    335   1.1   rmind 	prop_object_iterator_t it;
    336   1.5   rmind 	prop_dictionary_t rldict, rpdict;
    337   1.1   rmind 	int error;
    338   1.1   rmind 
    339   1.5   rmind 	/* Rule procedures and the ruleset - arrays. */
    340   1.5   rmind 	if (prop_object_type(rprocs) != PROP_TYPE_ARRAY ||
    341  1.12   rmind 	    prop_object_type(rules) != PROP_TYPE_ARRAY) {
    342  1.12   rmind 		NPF_ERR_DEBUG(errdict);
    343   1.1   rmind 		return EINVAL;
    344  1.12   rmind 	}
    345   1.1   rmind 
    346   1.5   rmind 	it = prop_array_iterator(rprocs);
    347   1.5   rmind 	while ((rpdict = prop_object_iterator_next(it)) != NULL) {
    348   1.6   rmind 		if (prop_dictionary_get(rpdict, "rproc-ptr")) {
    349   1.6   rmind 			prop_object_iterator_release(it);
    350  1.12   rmind 			NPF_ERR_DEBUG(errdict);
    351   1.5   rmind 			return EINVAL;
    352   1.6   rmind 		}
    353   1.5   rmind 	}
    354   1.5   rmind 	prop_object_iterator_release(it);
    355   1.5   rmind 
    356   1.4   rmind 	error = 0;
    357   1.1   rmind 	it = prop_array_iterator(rules);
    358   1.1   rmind 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    359   1.1   rmind 		prop_array_t subrules;
    360   1.6   rmind 		npf_ruleset_t *rlsetsub;
    361   1.6   rmind 		npf_rule_t *rl;
    362   1.1   rmind 
    363   1.1   rmind 		/* Generate a single rule. */
    364  1.12   rmind 		error = npf_mk_singlerule(rldict, rprocs, &rl, errdict);
    365   1.6   rmind 		if (error) {
    366   1.1   rmind 			break;
    367   1.6   rmind 		}
    368   1.6   rmind 		npf_ruleset_insert(rlset, rl);
    369   1.1   rmind 
    370   1.6   rmind 		/* Check for sub-rules and generate, if any. */
    371   1.1   rmind 		subrules = prop_dictionary_get(rldict, "subrules");
    372   1.1   rmind 		if (subrules == NULL) {
    373   1.1   rmind 			/* No subrules, next.. */
    374   1.1   rmind 			continue;
    375   1.1   rmind 		}
    376   1.6   rmind 		rlsetsub = npf_rule_subset(rl);
    377  1.12   rmind 		error = npf_mk_subrules(rlsetsub, subrules, rprocs, errdict);
    378   1.1   rmind 		if (error)
    379   1.1   rmind 			break;
    380   1.1   rmind 	}
    381   1.1   rmind 	prop_object_iterator_release(it);
    382   1.1   rmind 	/*
    383   1.4   rmind 	 * Note: in a case of error, caller will free the ruleset.
    384   1.1   rmind 	 */
    385   1.1   rmind 	return error;
    386   1.1   rmind }
    387   1.1   rmind 
    388   1.6   rmind static int __noinline
    389  1.12   rmind npf_mk_natlist(npf_ruleset_t *nset, prop_array_t natlist,
    390  1.12   rmind     prop_dictionary_t errdict)
    391   1.1   rmind {
    392   1.1   rmind 	prop_object_iterator_t it;
    393   1.1   rmind 	prop_dictionary_t natdict;
    394   1.1   rmind 	int error;
    395   1.1   rmind 
    396   1.1   rmind 	/* NAT policies - array. */
    397  1.12   rmind 	if (prop_object_type(natlist) != PROP_TYPE_ARRAY) {
    398  1.12   rmind 		NPF_ERR_DEBUG(errdict);
    399   1.1   rmind 		return EINVAL;
    400  1.12   rmind 	}
    401   1.1   rmind 
    402   1.4   rmind 	error = 0;
    403   1.1   rmind 	it = prop_array_iterator(natlist);
    404   1.1   rmind 	while ((natdict = prop_object_iterator_next(it)) != NULL) {
    405   1.1   rmind 		npf_natpolicy_t *np;
    406   1.1   rmind 		npf_rule_t *rl;
    407   1.1   rmind 
    408   1.1   rmind 		/* NAT policy - dictionary. */
    409   1.1   rmind 		if (prop_object_type(natdict) != PROP_TYPE_DICTIONARY) {
    410  1.12   rmind 			NPF_ERR_DEBUG(errdict);
    411   1.1   rmind 			error = EINVAL;
    412   1.1   rmind 			break;
    413   1.1   rmind 		}
    414   1.1   rmind 
    415   1.1   rmind 		/*
    416   1.1   rmind 		 * NAT policies are standard rules, plus additional
    417   1.1   rmind 		 * information for translation.  Make a rule.
    418   1.1   rmind 		 */
    419  1.12   rmind 		error = npf_mk_singlerule(natdict, NULL, &rl, errdict);
    420   1.6   rmind 		if (error) {
    421   1.1   rmind 			break;
    422   1.6   rmind 		}
    423   1.6   rmind 		npf_ruleset_insert(nset, rl);
    424   1.6   rmind 
    425   1.6   rmind 		/* If rule is named, it is a group with NAT policies. */
    426   1.6   rmind 		if (prop_dictionary_get(natdict, "name") &&
    427   1.6   rmind 		    prop_dictionary_get(natdict, "subrules")) {
    428   1.6   rmind 			continue;
    429   1.6   rmind 		}
    430   1.1   rmind 
    431   1.1   rmind 		/* Allocate a new NAT policy and assign to the rule. */
    432   1.5   rmind 		np = npf_nat_newpolicy(natdict, nset);
    433   1.1   rmind 		if (np == NULL) {
    434  1.12   rmind 			NPF_ERR_DEBUG(errdict);
    435   1.1   rmind 			error = ENOMEM;
    436   1.1   rmind 			break;
    437   1.1   rmind 		}
    438   1.1   rmind 		npf_rule_setnat(rl, np);
    439   1.1   rmind 	}
    440   1.1   rmind 	prop_object_iterator_release(it);
    441   1.1   rmind 	/*
    442   1.1   rmind 	 * Note: in a case of error, caller will free entire NAT ruleset
    443   1.1   rmind 	 * with assigned NAT policies.
    444   1.1   rmind 	 */
    445   1.1   rmind 	return error;
    446   1.1   rmind }
    447   1.1   rmind 
    448   1.1   rmind /*
    449   1.1   rmind  * npfctl_reload: store passed data i.e. update settings, create passed
    450   1.1   rmind  * tables, rules and atomically activate all them.
    451   1.1   rmind  */
    452   1.1   rmind int
    453   1.1   rmind npfctl_reload(u_long cmd, void *data)
    454   1.1   rmind {
    455  1.12   rmind 	struct plistref *pref = data;
    456  1.14   rmind 	prop_dictionary_t npf_dict, errdict;
    457   1.5   rmind 	prop_array_t natlist, tables, rprocs, rules;
    458   1.1   rmind 	npf_tableset_t *tblset = NULL;
    459   1.1   rmind 	npf_ruleset_t *rlset = NULL;
    460   1.1   rmind 	npf_ruleset_t *nset = NULL;
    461  1.11   rmind 	bool flush;
    462   1.1   rmind 	int error;
    463   1.1   rmind 
    464   1.1   rmind 	/* Retrieve the dictionary. */
    465  1.15   rmind #ifndef _NPF_TESTING
    466  1.14   rmind 	error = prop_dictionary_copyin_ioctl(pref, cmd, &npf_dict);
    467   1.1   rmind 	if (error)
    468   1.1   rmind 		return error;
    469   1.1   rmind #else
    470  1.15   rmind 	npf_dict = (prop_dictionary_t)pref;
    471   1.1   rmind #endif
    472  1.15   rmind 
    473  1.12   rmind 	/* Dictionary for error reporting. */
    474  1.12   rmind 	errdict = prop_dictionary_create();
    475  1.12   rmind 
    476   1.1   rmind 	/* NAT policies. */
    477   1.1   rmind 	nset = npf_ruleset_create();
    478  1.14   rmind 	natlist = prop_dictionary_get(npf_dict, "translation");
    479  1.12   rmind 	error = npf_mk_natlist(nset, natlist, errdict);
    480  1.10   rmind 	if (error) {
    481   1.1   rmind 		goto fail;
    482  1.10   rmind 	}
    483   1.1   rmind 
    484   1.1   rmind 	/* Tables. */
    485   1.1   rmind 	tblset = npf_tableset_create();
    486  1.14   rmind 	tables = prop_dictionary_get(npf_dict, "tables");
    487  1.12   rmind 	error = npf_mk_tables(tblset, tables, errdict);
    488  1.10   rmind 	if (error) {
    489   1.1   rmind 		goto fail;
    490  1.10   rmind 	}
    491   1.1   rmind 
    492   1.5   rmind 	/* Rules and rule procedures. */
    493   1.1   rmind 	rlset = npf_ruleset_create();
    494  1.14   rmind 	rprocs = prop_dictionary_get(npf_dict, "rprocs");
    495  1.14   rmind 	rules = prop_dictionary_get(npf_dict, "rules");
    496  1.12   rmind 	error = npf_mk_rules(rlset, rules, rprocs, errdict);
    497  1.10   rmind 	if (error) {
    498   1.1   rmind 		goto fail;
    499  1.10   rmind 	}
    500   1.1   rmind 
    501  1.11   rmind 	flush = false;
    502  1.14   rmind 	prop_dictionary_get_bool(npf_dict, "flush", &flush);
    503  1.11   rmind 
    504   1.1   rmind 	/*
    505   1.4   rmind 	 * Finally - reload ruleset, tableset and NAT policies.
    506   1.1   rmind 	 * Operation will be performed as a single transaction.
    507   1.1   rmind 	 */
    508  1.14   rmind 	npf_reload(npf_dict, rlset, tblset, nset, flush);
    509   1.1   rmind 
    510  1.11   rmind 	/* Turn on/off session tracking accordingly. */
    511  1.11   rmind 	npf_session_tracking(!flush);
    512  1.11   rmind 
    513   1.1   rmind 	/* Done.  Since data is consumed now, we shall not destroy it. */
    514   1.1   rmind 	tblset = NULL;
    515   1.1   rmind 	rlset = NULL;
    516   1.1   rmind 	nset = NULL;
    517   1.1   rmind fail:
    518   1.1   rmind 	/*
    519   1.1   rmind 	 * Note: destroy rulesets first, to drop references to the tableset.
    520   1.1   rmind 	 */
    521   1.1   rmind 	KASSERT(error == 0 || (nset || rlset || tblset));
    522   1.1   rmind 	if (nset) {
    523   1.1   rmind 		npf_ruleset_destroy(nset);
    524   1.1   rmind 	}
    525   1.1   rmind 	if (rlset) {
    526   1.1   rmind 		npf_ruleset_destroy(rlset);
    527   1.1   rmind 	}
    528   1.1   rmind 	if (tblset) {
    529   1.1   rmind 		npf_tableset_destroy(tblset);
    530   1.1   rmind 	}
    531  1.14   rmind 	if (error) {
    532  1.14   rmind 		prop_object_release(npf_dict);
    533  1.14   rmind 	}
    534  1.12   rmind 
    535  1.12   rmind 	/* Error report. */
    536  1.18   rmind #ifndef _NPF_TESTING
    537  1.12   rmind 	prop_dictionary_set_int32(errdict, "errno", error);
    538  1.12   rmind 	prop_dictionary_copyout_ioctl(pref, cmd, errdict);
    539  1.18   rmind 	prop_object_release(errdict);
    540  1.18   rmind 	error = 0;
    541  1.13   rmind #endif
    542  1.18   rmind 	return error;
    543   1.6   rmind }
    544   1.6   rmind 
    545  1.14   rmind int
    546  1.14   rmind npfctl_getconf(u_long cmd, void *data)
    547  1.14   rmind {
    548  1.14   rmind 	struct plistref *pref = data;
    549  1.14   rmind 	prop_dictionary_t npf_dict;
    550  1.14   rmind 	int error;
    551  1.14   rmind 
    552  1.14   rmind 	npf_core_enter();
    553  1.14   rmind 	npf_dict = npf_core_dict();
    554  1.14   rmind 	prop_dictionary_set_bool(npf_dict, "active", npf_pfil_registered_p());
    555  1.14   rmind 	error = prop_dictionary_copyout_ioctl(pref, cmd, npf_dict);
    556  1.14   rmind 	npf_core_exit();
    557  1.14   rmind 
    558  1.14   rmind 	return error;
    559  1.14   rmind }
    560  1.14   rmind 
    561   1.6   rmind /*
    562   1.6   rmind  * npfctl_update_rule: reload a specific rule identified by the name.
    563   1.6   rmind  */
    564   1.6   rmind int
    565   1.6   rmind npfctl_update_rule(u_long cmd, void *data)
    566   1.6   rmind {
    567  1.12   rmind 	struct plistref *pref = data;
    568  1.12   rmind 	prop_dictionary_t dict, errdict;
    569   1.6   rmind 	prop_array_t subrules;
    570   1.6   rmind 	prop_object_t obj;
    571   1.6   rmind 	npf_ruleset_t *rlset;
    572   1.6   rmind 	const char *name;
    573   1.6   rmind 	int error;
    574   1.6   rmind 
    575   1.6   rmind 	/* Retrieve and construct the rule. */
    576   1.6   rmind 	error = prop_dictionary_copyin_ioctl(pref, cmd, &dict);
    577   1.6   rmind 	if (error) {
    578   1.6   rmind 		return error;
    579   1.6   rmind 	}
    580  1.12   rmind 
    581  1.12   rmind 	/* Dictionary for error reporting. */
    582  1.12   rmind 	errdict = prop_dictionary_create();
    583  1.12   rmind 
    584   1.6   rmind 	/* Create the ruleset and construct sub-rules. */
    585   1.6   rmind 	rlset = npf_ruleset_create();
    586   1.6   rmind 	subrules = prop_dictionary_get(dict, "subrules");
    587  1.12   rmind 	error = npf_mk_subrules(rlset, subrules, NULL, errdict);
    588   1.6   rmind 	if (error) {
    589   1.6   rmind 		goto out;
    590   1.6   rmind 	}
    591   1.6   rmind 
    592   1.6   rmind 	/* Lookup the rule by name, and replace its subset (sub-rules). */
    593   1.6   rmind 	obj = prop_dictionary_get(dict, "name");
    594   1.6   rmind 	name = prop_string_cstring_nocopy(obj);
    595   1.6   rmind 	if (npf_ruleset_replace(name, rlset) == NULL) {
    596   1.6   rmind 		/* Not found. */
    597   1.6   rmind 		error = ENOENT;
    598   1.6   rmind out:		/* Error path. */
    599   1.6   rmind 		npf_ruleset_destroy(rlset);
    600   1.6   rmind 	}
    601   1.6   rmind 	prop_object_release(dict);
    602  1.12   rmind 
    603  1.12   rmind 	/* Error report. */
    604  1.12   rmind 	prop_dictionary_set_int32(errdict, "errno", error);
    605  1.12   rmind 	prop_dictionary_copyout_ioctl(pref, cmd, errdict);
    606  1.12   rmind 	prop_object_release(errdict);
    607   1.1   rmind 	return error;
    608   1.1   rmind }
    609   1.1   rmind 
    610   1.1   rmind /*
    611   1.4   rmind  * npfctl_sessions_save: construct a list of sessions and export for saving.
    612   1.4   rmind  */
    613   1.4   rmind int
    614   1.4   rmind npfctl_sessions_save(u_long cmd, void *data)
    615   1.4   rmind {
    616   1.4   rmind 	struct plistref *pref = data;
    617   1.4   rmind 	prop_dictionary_t sesdict;
    618   1.4   rmind 	prop_array_t selist, nplist;
    619   1.4   rmind 	int error;
    620   1.4   rmind 
    621   1.4   rmind 	/* Create a dictionary and two lists. */
    622   1.4   rmind 	sesdict = prop_dictionary_create();
    623   1.4   rmind 	selist = prop_array_create();
    624   1.4   rmind 	nplist = prop_array_create();
    625   1.4   rmind 
    626   1.4   rmind 	/* Save the sessions. */
    627   1.4   rmind 	error = npf_session_save(selist, nplist);
    628   1.4   rmind 	if (error) {
    629   1.4   rmind 		goto fail;
    630   1.4   rmind 	}
    631   1.4   rmind 
    632   1.4   rmind 	/* Set the session list, NAT policy list and export the dictionary. */
    633   1.4   rmind 	prop_dictionary_set(sesdict, "session-list", selist);
    634   1.4   rmind 	prop_dictionary_set(sesdict, "nat-policy-list", nplist);
    635   1.4   rmind 	error = prop_dictionary_copyout_ioctl(pref, cmd, sesdict);
    636   1.4   rmind fail:
    637   1.4   rmind 	prop_object_release(sesdict);
    638   1.4   rmind 	return error;
    639   1.4   rmind }
    640   1.4   rmind 
    641   1.4   rmind /*
    642   1.4   rmind  * npfctl_sessions_load: import a list of sessions, reconstruct them and load.
    643   1.4   rmind  */
    644   1.4   rmind int
    645   1.4   rmind npfctl_sessions_load(u_long cmd, void *data)
    646   1.4   rmind {
    647   1.4   rmind 	const struct plistref *pref = data;
    648   1.4   rmind 	npf_sehash_t *sehasht = NULL;
    649   1.4   rmind 	prop_dictionary_t sesdict, sedict;
    650   1.4   rmind 	prop_object_iterator_t it;
    651   1.4   rmind 	prop_array_t selist;
    652   1.4   rmind 	int error;
    653   1.4   rmind 
    654   1.4   rmind 	/* Retrieve the dictionary containing session and NAT policy lists. */
    655   1.4   rmind 	error = prop_dictionary_copyin_ioctl(pref, cmd, &sesdict);
    656   1.4   rmind 	if (error)
    657   1.4   rmind 		return error;
    658  1.15   rmind 
    659   1.4   rmind 	/*
    660   1.4   rmind 	 * Note: session objects contain the references to the NAT policy
    661   1.4   rmind 	 * entries.  Therefore, no need to directly access it.
    662   1.4   rmind 	 */
    663   1.4   rmind 	selist = prop_dictionary_get(sesdict, "session-list");
    664   1.4   rmind 	if (prop_object_type(selist) != PROP_TYPE_ARRAY) {
    665   1.4   rmind 		error = EINVAL;
    666   1.4   rmind 		goto fail;
    667   1.4   rmind 	}
    668   1.4   rmind 
    669   1.4   rmind 	/* Create a session hash table. */
    670   1.4   rmind 	sehasht = sess_htable_create();
    671   1.4   rmind 	if (sehasht == NULL) {
    672   1.4   rmind 		error = ENOMEM;
    673   1.4   rmind 		goto fail;
    674   1.4   rmind 	}
    675   1.4   rmind 
    676   1.4   rmind 	/*
    677   1.4   rmind 	 * Iterate through and construct each session.
    678   1.4   rmind 	 */
    679   1.4   rmind 	error = 0;
    680   1.4   rmind 	it = prop_array_iterator(selist);
    681   1.4   rmind 	npf_core_enter();
    682   1.4   rmind 	while ((sedict = prop_object_iterator_next(it)) != NULL) {
    683   1.4   rmind 		/* Session - dictionary. */
    684   1.4   rmind 		if (prop_object_type(sedict) != PROP_TYPE_DICTIONARY) {
    685   1.4   rmind 			error = EINVAL;
    686   1.4   rmind 			goto fail;
    687   1.4   rmind 		}
    688   1.4   rmind 		/* Construct and insert real session structure. */
    689   1.4   rmind 		error = npf_session_restore(sehasht, sedict);
    690   1.4   rmind 		if (error) {
    691   1.4   rmind 			goto fail;
    692   1.4   rmind 		}
    693   1.4   rmind 	}
    694   1.4   rmind 	npf_core_exit();
    695   1.4   rmind 	sess_htable_reload(sehasht);
    696   1.4   rmind fail:
    697   1.4   rmind 	prop_object_release(selist);
    698   1.4   rmind 	if (error && sehasht) {
    699   1.4   rmind 		/* Destroy session table. */
    700   1.4   rmind 		sess_htable_destroy(sehasht);
    701   1.4   rmind 	}
    702   1.4   rmind 	return error;
    703   1.4   rmind }
    704   1.4   rmind 
    705   1.4   rmind /*
    706   1.2   rmind  * npfctl_table: add, remove or query entries in the specified table.
    707   1.1   rmind  *
    708   1.1   rmind  * For maximum performance, interface is avoiding proplib(3)'s overhead.
    709   1.1   rmind  */
    710   1.1   rmind int
    711   1.1   rmind npfctl_table(void *data)
    712   1.1   rmind {
    713  1.19   rmind 	const npf_ioctl_table_t *nct = data;
    714  1.10   rmind 	npf_tableset_t *tblset;
    715   1.1   rmind 	int error;
    716   1.1   rmind 
    717   1.6   rmind 	npf_core_enter(); /* XXXSMP */
    718  1.10   rmind 	tblset = npf_core_tableset();
    719   1.1   rmind 	switch (nct->nct_action) {
    720  1.19   rmind 	case NPF_IOCTL_TBLENT_LOOKUP:
    721  1.19   rmind 		error = npf_table_lookup(tblset, nct->nct_tid,
    722  1.19   rmind 		    nct->nct_data.ent.alen, &nct->nct_data.ent.addr);
    723   1.1   rmind 	case NPF_IOCTL_TBLENT_ADD:
    724  1.16   rmind 		error = npf_table_insert(tblset, nct->nct_tid,
    725  1.19   rmind 		    nct->nct_data.ent.alen, &nct->nct_data.ent.addr,
    726  1.19   rmind 		    nct->nct_data.ent.mask);
    727   1.1   rmind 		break;
    728   1.1   rmind 	case NPF_IOCTL_TBLENT_REM:
    729  1.16   rmind 		error = npf_table_remove(tblset, nct->nct_tid,
    730  1.19   rmind 		    nct->nct_data.ent.alen, &nct->nct_data.ent.addr,
    731  1.19   rmind 		    nct->nct_data.ent.mask);
    732  1.19   rmind 		break;
    733  1.19   rmind 	case NPF_IOCTL_TBLENT_LIST:
    734  1.19   rmind 		error = npf_table_list(tblset, nct->nct_tid,
    735  1.19   rmind 		    nct->nct_data.buf.buf, nct->nct_data.buf.len);
    736   1.1   rmind 		break;
    737   1.1   rmind 	default:
    738  1.19   rmind 		error = EINVAL;
    739  1.19   rmind 		break;
    740   1.1   rmind 	}
    741   1.6   rmind 	npf_core_exit(); /* XXXSMP */
    742   1.1   rmind 	return error;
    743   1.1   rmind }
    744