Home | History | Annotate | Line # | Download | only in npf
npf_ctl.c revision 1.34
      1  1.34     rmind /*	$NetBSD: npf_ctl.c,v 1.34 2014/07/19 18:24:16 rmind Exp $	*/
      2   1.1     rmind 
      3   1.1     rmind /*-
      4  1.33     rmind  * Copyright (c) 2009-2014 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.34     rmind __KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.34 2014/07/19 18:24:16 rmind Exp $");
     41   1.1     rmind 
     42   1.1     rmind #include <sys/param.h>
     43   1.1     rmind #include <sys/conf.h>
     44  1.21     rmind #include <sys/kmem.h>
     45  1.21     rmind #include <net/bpf.h>
     46   1.1     rmind 
     47   1.1     rmind #include <prop/proplib.h>
     48   1.1     rmind 
     49   1.1     rmind #include "npf_impl.h"
     50  1.34     rmind #include "npf_conn.h"
     51   1.1     rmind 
     52  1.12     rmind #if defined(DEBUG) || defined(DIAGNOSTIC)
     53  1.12     rmind #define	NPF_ERR_DEBUG(e) \
     54  1.12     rmind 	prop_dictionary_set_cstring_nocopy((e), "source-file", __FILE__); \
     55  1.12     rmind 	prop_dictionary_set_uint32((e), "source-line", __LINE__);
     56  1.12     rmind #else
     57  1.12     rmind #define	NPF_ERR_DEBUG(e)
     58  1.12     rmind #endif
     59  1.12     rmind 
     60   1.1     rmind /*
     61   1.1     rmind  * npfctl_switch: enable or disable packet inspection.
     62   1.1     rmind  */
     63   1.1     rmind int
     64   1.1     rmind npfctl_switch(void *data)
     65   1.1     rmind {
     66   1.1     rmind 	const bool onoff = *(int *)data ? true : false;
     67   1.1     rmind 	int error;
     68   1.1     rmind 
     69   1.1     rmind 	if (onoff) {
     70   1.1     rmind 		/* Enable: add pfil hooks. */
     71  1.31     rmind 		error = npf_pfil_register(false);
     72   1.1     rmind 	} else {
     73   1.1     rmind 		/* Disable: remove pfil hooks. */
     74  1.31     rmind 		npf_pfil_unregister(false);
     75   1.1     rmind 		error = 0;
     76   1.1     rmind 	}
     77   1.1     rmind 	return error;
     78   1.1     rmind }
     79   1.1     rmind 
     80   1.6     rmind static int __noinline
     81  1.33     rmind npf_mk_table_entries(npf_table_t *t, prop_array_t entries)
     82  1.33     rmind {
     83  1.33     rmind 	prop_object_iterator_t eit;
     84  1.33     rmind 	prop_dictionary_t ent;
     85  1.33     rmind 	int error = 0;
     86  1.33     rmind 
     87  1.33     rmind 	/* Fill all the entries. */
     88  1.33     rmind 	eit = prop_array_iterator(entries);
     89  1.33     rmind 	while ((ent = prop_object_iterator_next(eit)) != NULL) {
     90  1.33     rmind 		const npf_addr_t *addr;
     91  1.33     rmind 		npf_netmask_t mask;
     92  1.33     rmind 		int alen;
     93  1.33     rmind 
     94  1.33     rmind 		/* Get address and mask.  Add a table entry. */
     95  1.33     rmind 		prop_object_t obj = prop_dictionary_get(ent, "addr");
     96  1.33     rmind 		addr = (const npf_addr_t *)prop_data_data_nocopy(obj);
     97  1.33     rmind 		prop_dictionary_get_uint8(ent, "mask", &mask);
     98  1.33     rmind 		alen = prop_data_size(obj);
     99  1.33     rmind 
    100  1.33     rmind 		error = npf_table_insert(t, alen, addr, mask);
    101  1.33     rmind 		if (error)
    102  1.33     rmind 			break;
    103  1.33     rmind 	}
    104  1.33     rmind 	prop_object_iterator_release(eit);
    105  1.33     rmind 	return error;
    106  1.33     rmind }
    107  1.33     rmind 
    108  1.33     rmind static int __noinline
    109  1.12     rmind npf_mk_tables(npf_tableset_t *tblset, prop_array_t tables,
    110  1.12     rmind     prop_dictionary_t errdict)
    111   1.1     rmind {
    112   1.1     rmind 	prop_object_iterator_t it;
    113   1.1     rmind 	prop_dictionary_t tbldict;
    114   1.1     rmind 	int error = 0;
    115   1.1     rmind 
    116   1.1     rmind 	/* Tables - array. */
    117  1.12     rmind 	if (prop_object_type(tables) != PROP_TYPE_ARRAY) {
    118  1.12     rmind 		NPF_ERR_DEBUG(errdict);
    119   1.1     rmind 		return EINVAL;
    120  1.12     rmind 	}
    121   1.1     rmind 
    122   1.1     rmind 	it = prop_array_iterator(tables);
    123   1.1     rmind 	while ((tbldict = prop_object_iterator_next(it)) != NULL) {
    124  1.32     rmind 		const char *name;
    125   1.1     rmind 		npf_table_t *t;
    126   1.1     rmind 		u_int tid;
    127   1.1     rmind 		int type;
    128   1.1     rmind 
    129   1.1     rmind 		/* Table - dictionary. */
    130   1.1     rmind 		if (prop_object_type(tbldict) != PROP_TYPE_DICTIONARY) {
    131  1.12     rmind 			NPF_ERR_DEBUG(errdict);
    132   1.1     rmind 			error = EINVAL;
    133   1.1     rmind 			break;
    134   1.1     rmind 		}
    135   1.1     rmind 
    136  1.32     rmind 		/* Table name, ID and type.  Validate them. */
    137  1.32     rmind 		if (!prop_dictionary_get_cstring_nocopy(tbldict, "name", &name)) {
    138  1.32     rmind 			NPF_ERR_DEBUG(errdict);
    139  1.32     rmind 			error = EINVAL;
    140  1.32     rmind 			break;
    141  1.32     rmind 		}
    142   1.6     rmind 		prop_dictionary_get_uint32(tbldict, "id", &tid);
    143   1.6     rmind 		prop_dictionary_get_int32(tbldict, "type", &type);
    144  1.32     rmind 		error = npf_table_check(tblset, name, tid, type);
    145  1.32     rmind 		if (error) {
    146  1.32     rmind 			NPF_ERR_DEBUG(errdict);
    147   1.1     rmind 			break;
    148  1.32     rmind 		}
    149   1.1     rmind 
    150  1.33     rmind 		/* Get the entries or binary data. */
    151  1.33     rmind 		prop_array_t entries = prop_dictionary_get(tbldict, "entries");
    152  1.33     rmind 		if (prop_object_type(entries) != PROP_TYPE_ARRAY) {
    153  1.33     rmind 			NPF_ERR_DEBUG(errdict);
    154  1.33     rmind 			error = EINVAL;
    155  1.33     rmind 			break;
    156  1.33     rmind 		}
    157  1.33     rmind 		prop_object_t obj = prop_dictionary_get(tbldict, "data");
    158  1.33     rmind 		void *blob = prop_data_data(obj);
    159  1.33     rmind 		size_t size = prop_data_size(obj);
    160  1.33     rmind 
    161  1.33     rmind 		if (type == NPF_TABLE_CDB && (blob == NULL || size == 0)) {
    162  1.33     rmind 			NPF_ERR_DEBUG(errdict);
    163  1.33     rmind 			error = EINVAL;
    164  1.33     rmind 			break;
    165  1.33     rmind 		}
    166  1.34     rmind 		if (type == NPF_TABLE_HASH) {
    167  1.34     rmind 			size = 1024; /* XXX */
    168  1.34     rmind 		}
    169  1.33     rmind 
    170   1.1     rmind 		/* Create and insert the table. */
    171  1.33     rmind 		t = npf_table_create(name, tid, type, blob, size);
    172   1.1     rmind 		if (t == NULL) {
    173  1.12     rmind 			NPF_ERR_DEBUG(errdict);
    174   1.1     rmind 			error = ENOMEM;
    175   1.1     rmind 			break;
    176   1.1     rmind 		}
    177   1.1     rmind 		error = npf_tableset_insert(tblset, t);
    178   1.1     rmind 		KASSERT(error == 0);
    179   1.1     rmind 
    180  1.33     rmind 		if ((error = npf_mk_table_entries(t, entries)) != 0) {
    181  1.12     rmind 			NPF_ERR_DEBUG(errdict);
    182   1.1     rmind 			break;
    183   1.1     rmind 		}
    184   1.1     rmind 	}
    185   1.1     rmind 	prop_object_iterator_release(it);
    186   1.1     rmind 	/*
    187   1.4     rmind 	 * Note: in a case of error, caller will free the tableset.
    188   1.1     rmind 	 */
    189   1.1     rmind 	return error;
    190   1.1     rmind }
    191   1.1     rmind 
    192   1.5     rmind static npf_rproc_t *
    193  1.21     rmind npf_mk_singlerproc(prop_dictionary_t rpdict)
    194   1.5     rmind {
    195   1.5     rmind 	prop_object_iterator_t it;
    196  1.21     rmind 	prop_dictionary_t extdict;
    197  1.18     rmind 	prop_array_t extlist;
    198   1.5     rmind 	npf_rproc_t *rp;
    199  1.18     rmind 
    200  1.18     rmind 	extlist = prop_dictionary_get(rpdict, "extcalls");
    201  1.18     rmind 	if (prop_object_type(extlist) != PROP_TYPE_ARRAY) {
    202  1.18     rmind 		return NULL;
    203  1.18     rmind 	}
    204  1.18     rmind 
    205  1.18     rmind 	rp = npf_rproc_create(rpdict);
    206  1.21     rmind 	if (rp == NULL) {
    207  1.18     rmind 		return NULL;
    208  1.18     rmind 	}
    209  1.21     rmind 
    210  1.18     rmind 	it = prop_array_iterator(extlist);
    211  1.18     rmind 	while ((extdict = prop_object_iterator_next(it)) != NULL) {
    212  1.21     rmind 		const char *name;
    213  1.21     rmind 
    214  1.18     rmind 		if (!prop_dictionary_get_cstring_nocopy(extdict,
    215  1.18     rmind 		    "name", &name) || npf_ext_construct(name, rp, extdict)) {
    216  1.18     rmind 			npf_rproc_release(rp);
    217  1.18     rmind 			rp = NULL;
    218  1.18     rmind 			break;
    219  1.18     rmind 		}
    220  1.18     rmind 	}
    221  1.18     rmind 	prop_object_iterator_release(it);
    222  1.21     rmind 	return rp;
    223  1.21     rmind }
    224  1.21     rmind 
    225  1.21     rmind static int __noinline
    226  1.21     rmind npf_mk_rprocs(npf_rprocset_t *rpset, prop_array_t rprocs,
    227  1.21     rmind     prop_dictionary_t errdict)
    228  1.21     rmind {
    229  1.21     rmind 	prop_object_iterator_t it;
    230  1.21     rmind 	prop_dictionary_t rpdict;
    231  1.21     rmind 	int error = 0;
    232  1.21     rmind 
    233  1.21     rmind 	it = prop_array_iterator(rprocs);
    234  1.21     rmind 	while ((rpdict = prop_object_iterator_next(it)) != NULL) {
    235  1.21     rmind 		npf_rproc_t *rp;
    236  1.18     rmind 
    237  1.21     rmind 		if ((rp = npf_mk_singlerproc(rpdict)) == NULL) {
    238  1.21     rmind 			NPF_ERR_DEBUG(errdict);
    239  1.21     rmind 			error = EINVAL;
    240  1.21     rmind 			break;
    241  1.21     rmind 		}
    242  1.21     rmind 		npf_rprocset_insert(rpset, rp);
    243   1.5     rmind 	}
    244  1.21     rmind 	prop_object_iterator_release(it);
    245  1.21     rmind 	return error;
    246   1.5     rmind }
    247   1.5     rmind 
    248  1.24  christos static npf_alg_t *
    249  1.24  christos npf_mk_singlealg(prop_dictionary_t aldict)
    250  1.24  christos {
    251  1.24  christos 	const char *name;
    252  1.24  christos 
    253  1.24  christos 	if (!prop_dictionary_get_cstring_nocopy(aldict, "name", &name))
    254  1.24  christos 		return NULL;
    255  1.24  christos 	return npf_alg_construct(name);
    256  1.24  christos }
    257  1.24  christos 
    258  1.24  christos static int __noinline
    259  1.24  christos npf_mk_algs(prop_array_t alglist, prop_dictionary_t errdict)
    260  1.24  christos {
    261  1.24  christos 	prop_object_iterator_t it;
    262  1.24  christos 	prop_dictionary_t nadict;
    263  1.24  christos 	int error = 0;
    264  1.24  christos 
    265  1.24  christos 	it = prop_array_iterator(alglist);
    266  1.24  christos 	while ((nadict = prop_object_iterator_next(it)) != NULL) {
    267  1.24  christos 		if (npf_mk_singlealg(nadict) == NULL) {
    268  1.24  christos 			NPF_ERR_DEBUG(errdict);
    269  1.24  christos 			error = EINVAL;
    270  1.24  christos 			break;
    271  1.24  christos 		}
    272  1.24  christos 	}
    273  1.24  christos 	prop_object_iterator_release(it);
    274  1.24  christos 	return error;
    275  1.24  christos }
    276  1.24  christos 
    277   1.6     rmind static int __noinline
    278  1.21     rmind npf_mk_code(prop_object_t obj, int type, void **code, size_t *csize,
    279  1.12     rmind     prop_dictionary_t errdict)
    280  1.12     rmind {
    281  1.21     rmind 	const void *cptr;
    282  1.21     rmind 	size_t clen;
    283  1.21     rmind 	void *bc;
    284  1.12     rmind 
    285  1.29     rmind 	if (type != NPF_CODE_BPF) {
    286  1.29     rmind 		return ENOTSUP;
    287  1.29     rmind 	}
    288  1.21     rmind 	cptr = prop_data_data_nocopy(obj);
    289  1.21     rmind 	if (cptr == NULL || (clen = prop_data_size(obj)) == 0) {
    290  1.12     rmind 		NPF_ERR_DEBUG(errdict);
    291  1.21     rmind 		return EINVAL;
    292  1.12     rmind 	}
    293  1.29     rmind 	if (!npf_bpf_validate(cptr, clen)) {
    294  1.29     rmind 		NPF_ERR_DEBUG(errdict);
    295  1.29     rmind 		return EINVAL;
    296  1.12     rmind 	}
    297  1.21     rmind 	bc = kmem_alloc(clen, KM_SLEEP);
    298  1.21     rmind 	memcpy(bc, cptr, clen);
    299  1.21     rmind 
    300  1.21     rmind 	*code = bc;
    301  1.21     rmind 	*csize = clen;
    302  1.12     rmind 	return 0;
    303  1.12     rmind }
    304  1.12     rmind 
    305  1.12     rmind static int __noinline
    306  1.21     rmind npf_mk_singlerule(prop_dictionary_t rldict, npf_rprocset_t *rpset,
    307  1.21     rmind     npf_rule_t **rlret, prop_dictionary_t errdict)
    308   1.1     rmind {
    309  1.21     rmind 	npf_rule_t *rl;
    310  1.21     rmind 	const char *rname;
    311   1.1     rmind 	prop_object_t obj;
    312  1.21     rmind 	int p, error = 0;
    313   1.1     rmind 
    314   1.1     rmind 	/* Rule - dictionary. */
    315  1.12     rmind 	if (prop_object_type(rldict) != PROP_TYPE_DICTIONARY) {
    316  1.12     rmind 		NPF_ERR_DEBUG(errdict);
    317   1.1     rmind 		return EINVAL;
    318  1.12     rmind 	}
    319  1.21     rmind 	if ((rl = npf_rule_alloc(rldict)) == NULL) {
    320  1.21     rmind 		NPF_ERR_DEBUG(errdict);
    321  1.21     rmind 		return EINVAL;
    322  1.21     rmind 	}
    323   1.1     rmind 
    324  1.21     rmind 	/* Assign rule procedure, if any. */
    325  1.21     rmind 	if (prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rname)) {
    326  1.21     rmind 		npf_rproc_t *rp;
    327  1.21     rmind 
    328  1.21     rmind 		if (rpset == NULL) {
    329  1.21     rmind 			error = EINVAL;
    330  1.21     rmind 			goto err;
    331  1.21     rmind 		}
    332  1.21     rmind 		if ((rp = npf_rprocset_lookup(rpset, rname)) == NULL) {
    333  1.18     rmind 			NPF_ERR_DEBUG(errdict);
    334  1.18     rmind 			error = EINVAL;
    335  1.18     rmind 			goto err;
    336  1.18     rmind 		}
    337  1.21     rmind 		npf_rule_setrproc(rl, rp);
    338  1.18     rmind 	}
    339  1.18     rmind 
    340  1.21     rmind 	/* Filter code (binary data). */
    341  1.21     rmind 	if ((obj = prop_dictionary_get(rldict, "code")) != NULL) {
    342  1.21     rmind 		int type;
    343  1.21     rmind 		size_t len;
    344  1.21     rmind 		void *code;
    345  1.21     rmind 
    346  1.21     rmind 		prop_dictionary_get_int32(rldict, "code-type", &type);
    347  1.21     rmind 		error = npf_mk_code(obj, type, &code, &len, errdict);
    348  1.12     rmind 		if (error) {
    349  1.12     rmind 			goto err;
    350   1.4     rmind 		}
    351  1.21     rmind 		npf_rule_setcode(rl, type, code, len);
    352   1.1     rmind 	}
    353   1.1     rmind 
    354  1.21     rmind 	*rlret = rl;
    355   1.6     rmind 	return 0;
    356  1.12     rmind err:
    357  1.21     rmind 	npf_rule_free(rl);
    358  1.12     rmind 	prop_dictionary_get_int32(rldict, "priority", &p); /* XXX */
    359  1.12     rmind 	prop_dictionary_set_int32(errdict, "id", p);
    360  1.12     rmind 	return error;
    361   1.6     rmind }
    362   1.6     rmind 
    363   1.6     rmind static int __noinline
    364  1.21     rmind npf_mk_rules(npf_ruleset_t *rlset, prop_array_t rules, npf_rprocset_t *rpset,
    365  1.12     rmind     prop_dictionary_t errdict)
    366   1.6     rmind {
    367   1.6     rmind 	prop_object_iterator_t it;
    368   1.6     rmind 	prop_dictionary_t rldict;
    369  1.21     rmind 	int error;
    370   1.6     rmind 
    371   1.6     rmind 	if (prop_object_type(rules) != PROP_TYPE_ARRAY) {
    372  1.12     rmind 		NPF_ERR_DEBUG(errdict);
    373   1.6     rmind 		return EINVAL;
    374   1.6     rmind 	}
    375   1.5     rmind 
    376   1.4     rmind 	error = 0;
    377   1.1     rmind 	it = prop_array_iterator(rules);
    378   1.1     rmind 	while ((rldict = prop_object_iterator_next(it)) != NULL) {
    379  1.21     rmind 		npf_rule_t *rl = NULL;
    380   1.1     rmind 
    381   1.1     rmind 		/* Generate a single rule. */
    382  1.21     rmind 		error = npf_mk_singlerule(rldict, rpset, &rl, errdict);
    383   1.6     rmind 		if (error) {
    384   1.1     rmind 			break;
    385   1.6     rmind 		}
    386   1.6     rmind 		npf_ruleset_insert(rlset, rl);
    387   1.1     rmind 	}
    388   1.1     rmind 	prop_object_iterator_release(it);
    389   1.1     rmind 	/*
    390   1.4     rmind 	 * Note: in a case of error, caller will free the ruleset.
    391   1.1     rmind 	 */
    392   1.1     rmind 	return error;
    393   1.1     rmind }
    394   1.1     rmind 
    395   1.6     rmind static int __noinline
    396  1.12     rmind npf_mk_natlist(npf_ruleset_t *nset, prop_array_t natlist,
    397  1.12     rmind     prop_dictionary_t errdict)
    398   1.1     rmind {
    399   1.1     rmind 	prop_object_iterator_t it;
    400   1.1     rmind 	prop_dictionary_t natdict;
    401   1.1     rmind 	int error;
    402   1.1     rmind 
    403   1.1     rmind 	/* NAT policies - array. */
    404  1.12     rmind 	if (prop_object_type(natlist) != PROP_TYPE_ARRAY) {
    405  1.12     rmind 		NPF_ERR_DEBUG(errdict);
    406   1.1     rmind 		return EINVAL;
    407  1.12     rmind 	}
    408   1.1     rmind 
    409   1.4     rmind 	error = 0;
    410   1.1     rmind 	it = prop_array_iterator(natlist);
    411   1.1     rmind 	while ((natdict = prop_object_iterator_next(it)) != NULL) {
    412  1.21     rmind 		npf_rule_t *rl = NULL;
    413   1.1     rmind 		npf_natpolicy_t *np;
    414   1.1     rmind 
    415   1.1     rmind 		/* NAT policy - dictionary. */
    416   1.1     rmind 		if (prop_object_type(natdict) != PROP_TYPE_DICTIONARY) {
    417  1.12     rmind 			NPF_ERR_DEBUG(errdict);
    418   1.1     rmind 			error = EINVAL;
    419   1.1     rmind 			break;
    420   1.1     rmind 		}
    421   1.1     rmind 
    422   1.1     rmind 		/*
    423   1.1     rmind 		 * NAT policies are standard rules, plus additional
    424   1.1     rmind 		 * information for translation.  Make a rule.
    425   1.1     rmind 		 */
    426  1.12     rmind 		error = npf_mk_singlerule(natdict, NULL, &rl, errdict);
    427   1.6     rmind 		if (error) {
    428   1.1     rmind 			break;
    429   1.6     rmind 		}
    430   1.6     rmind 		npf_ruleset_insert(nset, rl);
    431   1.6     rmind 
    432   1.6     rmind 		/* If rule is named, it is a group with NAT policies. */
    433   1.6     rmind 		if (prop_dictionary_get(natdict, "name") &&
    434   1.6     rmind 		    prop_dictionary_get(natdict, "subrules")) {
    435   1.6     rmind 			continue;
    436   1.6     rmind 		}
    437   1.1     rmind 
    438   1.1     rmind 		/* Allocate a new NAT policy and assign to the rule. */
    439   1.5     rmind 		np = npf_nat_newpolicy(natdict, nset);
    440   1.1     rmind 		if (np == NULL) {
    441  1.12     rmind 			NPF_ERR_DEBUG(errdict);
    442   1.1     rmind 			error = ENOMEM;
    443   1.1     rmind 			break;
    444   1.1     rmind 		}
    445   1.1     rmind 		npf_rule_setnat(rl, np);
    446   1.1     rmind 	}
    447   1.1     rmind 	prop_object_iterator_release(it);
    448   1.1     rmind 	/*
    449   1.1     rmind 	 * Note: in a case of error, caller will free entire NAT ruleset
    450   1.1     rmind 	 * with assigned NAT policies.
    451   1.1     rmind 	 */
    452   1.1     rmind 	return error;
    453   1.1     rmind }
    454   1.1     rmind 
    455   1.1     rmind /*
    456   1.1     rmind  * npfctl_reload: store passed data i.e. update settings, create passed
    457   1.1     rmind  * tables, rules and atomically activate all them.
    458   1.1     rmind  */
    459   1.1     rmind int
    460   1.1     rmind npfctl_reload(u_long cmd, void *data)
    461   1.1     rmind {
    462  1.12     rmind 	struct plistref *pref = data;
    463  1.14     rmind 	prop_dictionary_t npf_dict, errdict;
    464  1.24  christos 	prop_array_t alglist, natlist, tables, rprocs, rules;
    465   1.1     rmind 	npf_tableset_t *tblset = NULL;
    466  1.21     rmind 	npf_rprocset_t *rpset = NULL;
    467   1.1     rmind 	npf_ruleset_t *rlset = NULL;
    468   1.1     rmind 	npf_ruleset_t *nset = NULL;
    469  1.20     rmind 	uint32_t ver = 0;
    470  1.21     rmind 	size_t nitems;
    471  1.11     rmind 	bool flush;
    472   1.1     rmind 	int error;
    473   1.1     rmind 
    474   1.1     rmind 	/* Retrieve the dictionary. */
    475  1.15     rmind #ifndef _NPF_TESTING
    476  1.14     rmind 	error = prop_dictionary_copyin_ioctl(pref, cmd, &npf_dict);
    477   1.1     rmind 	if (error)
    478   1.1     rmind 		return error;
    479   1.1     rmind #else
    480  1.15     rmind 	npf_dict = (prop_dictionary_t)pref;
    481   1.1     rmind #endif
    482  1.15     rmind 
    483  1.20     rmind 	/* Dictionary for error reporting and version check. */
    484  1.12     rmind 	errdict = prop_dictionary_create();
    485  1.20     rmind 	prop_dictionary_get_uint32(npf_dict, "version", &ver);
    486  1.20     rmind 	if (ver != NPF_VERSION) {
    487  1.20     rmind 		error = EPROGMISMATCH;
    488  1.20     rmind 		goto fail;
    489  1.20     rmind 	}
    490  1.12     rmind 
    491  1.24  christos 	/* ALGs. */
    492  1.24  christos 	alglist = prop_dictionary_get(npf_dict, "algs");
    493  1.24  christos 	error = npf_mk_algs(alglist, errdict);
    494  1.24  christos 	if (error) {
    495  1.24  christos 		goto fail;
    496  1.24  christos 	}
    497  1.24  christos 
    498   1.1     rmind 	/* NAT policies. */
    499  1.14     rmind 	natlist = prop_dictionary_get(npf_dict, "translation");
    500  1.30     rmind 	if ((nitems = prop_array_count(natlist)) > NPF_MAX_RULES) {
    501  1.30     rmind 		goto fail;
    502  1.30     rmind 	}
    503  1.21     rmind 
    504  1.21     rmind 	nset = npf_ruleset_create(nitems);
    505  1.12     rmind 	error = npf_mk_natlist(nset, natlist, errdict);
    506  1.10     rmind 	if (error) {
    507   1.1     rmind 		goto fail;
    508  1.10     rmind 	}
    509   1.1     rmind 
    510   1.1     rmind 	/* Tables. */
    511  1.14     rmind 	tables = prop_dictionary_get(npf_dict, "tables");
    512  1.32     rmind 	if ((nitems = prop_array_count(tables)) > NPF_MAX_TABLES) {
    513  1.32     rmind 		goto fail;
    514  1.32     rmind 	}
    515  1.32     rmind 	tblset = npf_tableset_create(nitems);
    516  1.12     rmind 	error = npf_mk_tables(tblset, tables, errdict);
    517  1.10     rmind 	if (error) {
    518   1.1     rmind 		goto fail;
    519  1.10     rmind 	}
    520   1.1     rmind 
    521  1.21     rmind 	/* Rule procedures. */
    522  1.32     rmind 	rprocs = prop_dictionary_get(npf_dict, "rprocs");
    523  1.32     rmind 	if ((nitems = prop_array_count(rprocs)) > NPF_MAX_RPROCS) {
    524  1.32     rmind 		goto fail;
    525  1.32     rmind 	}
    526  1.21     rmind 	rpset = npf_rprocset_create();
    527  1.21     rmind 	error = npf_mk_rprocs(rpset, rprocs, errdict);
    528  1.21     rmind 	if (error) {
    529  1.21     rmind 		goto fail;
    530  1.21     rmind 	}
    531  1.21     rmind 
    532  1.21     rmind 	/* Rules. */
    533  1.14     rmind 	rules = prop_dictionary_get(npf_dict, "rules");
    534  1.30     rmind 	if ((nitems = prop_array_count(rules)) > NPF_MAX_RULES) {
    535  1.30     rmind 		goto fail;
    536  1.30     rmind 	}
    537  1.21     rmind 
    538  1.21     rmind 	rlset = npf_ruleset_create(nitems);
    539  1.21     rmind 	error = npf_mk_rules(rlset, rules, rpset, errdict);
    540  1.10     rmind 	if (error) {
    541   1.1     rmind 		goto fail;
    542  1.10     rmind 	}
    543   1.1     rmind 
    544  1.11     rmind 	flush = false;
    545  1.14     rmind 	prop_dictionary_get_bool(npf_dict, "flush", &flush);
    546  1.11     rmind 
    547   1.1     rmind 	/*
    548  1.21     rmind 	 * Finally - perform the reload.
    549   1.1     rmind 	 */
    550  1.21     rmind 	npf_config_reload(npf_dict, rlset, tblset, nset, rpset, flush);
    551   1.1     rmind 
    552  1.34     rmind 	/* Turn on/off connection tracking accordingly. */
    553  1.34     rmind 	npf_conn_tracking(!flush);
    554  1.11     rmind 
    555   1.1     rmind 	/* Done.  Since data is consumed now, we shall not destroy it. */
    556   1.1     rmind 	tblset = NULL;
    557  1.21     rmind 	rpset = NULL;
    558   1.1     rmind 	rlset = NULL;
    559   1.1     rmind 	nset = NULL;
    560   1.1     rmind fail:
    561   1.1     rmind 	/*
    562   1.1     rmind 	 * Note: destroy rulesets first, to drop references to the tableset.
    563   1.1     rmind 	 */
    564  1.21     rmind 	KASSERT(error == 0 || (nset || rpset || rlset || tblset));
    565   1.1     rmind 	if (nset) {
    566   1.1     rmind 		npf_ruleset_destroy(nset);
    567   1.1     rmind 	}
    568   1.1     rmind 	if (rlset) {
    569   1.1     rmind 		npf_ruleset_destroy(rlset);
    570   1.1     rmind 	}
    571  1.21     rmind 	if (rpset) {
    572  1.21     rmind 		npf_rprocset_destroy(rpset);
    573  1.21     rmind 	}
    574   1.1     rmind 	if (tblset) {
    575   1.1     rmind 		npf_tableset_destroy(tblset);
    576   1.1     rmind 	}
    577  1.14     rmind 	if (error) {
    578  1.14     rmind 		prop_object_release(npf_dict);
    579  1.14     rmind 	}
    580  1.12     rmind 
    581  1.12     rmind 	/* Error report. */
    582  1.18     rmind #ifndef _NPF_TESTING
    583  1.12     rmind 	prop_dictionary_set_int32(errdict, "errno", error);
    584  1.12     rmind 	prop_dictionary_copyout_ioctl(pref, cmd, errdict);
    585  1.18     rmind 	prop_object_release(errdict);
    586  1.18     rmind 	error = 0;
    587  1.13     rmind #endif
    588  1.18     rmind 	return error;
    589   1.6     rmind }
    590   1.6     rmind 
    591  1.21     rmind /*
    592  1.21     rmind  * npfctl_rule: add or remove dynamic rules in the specified ruleset.
    593  1.21     rmind  */
    594  1.14     rmind int
    595  1.21     rmind npfctl_rule(u_long cmd, void *data)
    596  1.14     rmind {
    597  1.14     rmind 	struct plistref *pref = data;
    598  1.21     rmind 	prop_dictionary_t npf_rule, retdict = NULL;
    599  1.21     rmind 	npf_ruleset_t *rlset;
    600  1.21     rmind 	npf_rule_t *rl = NULL;
    601  1.21     rmind 	const char *ruleset_name;
    602  1.21     rmind 	uint32_t rcmd = 0;
    603  1.14     rmind 	int error;
    604  1.14     rmind 
    605  1.21     rmind 	error = prop_dictionary_copyin_ioctl(pref, cmd, &npf_rule);
    606  1.21     rmind 	if (error) {
    607  1.21     rmind 		return error;
    608  1.21     rmind 	}
    609  1.21     rmind 	prop_dictionary_get_uint32(npf_rule, "command", &rcmd);
    610  1.21     rmind 	if (!prop_dictionary_get_cstring_nocopy(npf_rule,
    611  1.21     rmind 	    "ruleset-name", &ruleset_name)) {
    612  1.27     rmind 		error = EINVAL;
    613  1.27     rmind 		goto out;
    614  1.21     rmind 	}
    615  1.21     rmind 
    616  1.21     rmind 	if (rcmd == NPF_CMD_RULE_ADD) {
    617  1.27     rmind 		retdict = prop_dictionary_create();
    618  1.27     rmind 		if (npf_mk_singlerule(npf_rule, NULL, &rl, retdict) != 0) {
    619  1.27     rmind 			error = EINVAL;
    620  1.27     rmind 			goto out;
    621  1.21     rmind 		}
    622  1.21     rmind 	}
    623  1.21     rmind 
    624  1.21     rmind 	npf_config_enter();
    625  1.21     rmind 	rlset = npf_config_ruleset();
    626  1.21     rmind 
    627  1.21     rmind 	switch (rcmd) {
    628  1.21     rmind 	case NPF_CMD_RULE_ADD: {
    629  1.21     rmind 		if ((error = npf_ruleset_add(rlset, ruleset_name, rl)) == 0) {
    630  1.21     rmind 			/* Success. */
    631  1.23     rmind 			uint64_t id = npf_rule_getid(rl);
    632  1.23     rmind 			prop_dictionary_set_uint64(retdict, "id", id);
    633  1.21     rmind 			rl = NULL;
    634  1.21     rmind 		}
    635  1.21     rmind 		break;
    636  1.21     rmind 	}
    637  1.21     rmind 	case NPF_CMD_RULE_REMOVE: {
    638  1.23     rmind 		uint64_t id;
    639  1.21     rmind 
    640  1.23     rmind 		if (!prop_dictionary_get_uint64(npf_rule, "id", &id)) {
    641  1.21     rmind 			error = EINVAL;
    642  1.21     rmind 			break;
    643  1.21     rmind 		}
    644  1.23     rmind 		error = npf_ruleset_remove(rlset, ruleset_name, id);
    645  1.21     rmind 		break;
    646  1.21     rmind 	}
    647  1.21     rmind 	case NPF_CMD_RULE_REMKEY: {
    648  1.21     rmind 		prop_object_t obj = prop_dictionary_get(npf_rule, "key");
    649  1.21     rmind 		const void *key = prop_data_data_nocopy(obj);
    650  1.21     rmind 		size_t len = prop_data_size(obj);
    651  1.21     rmind 
    652  1.21     rmind 		if (len == 0 || len > NPF_RULE_MAXKEYLEN) {
    653  1.21     rmind 			error = EINVAL;
    654  1.21     rmind 			break;
    655  1.21     rmind 		}
    656  1.22     rmind 		error = npf_ruleset_remkey(rlset, ruleset_name, key, len);
    657  1.22     rmind 		break;
    658  1.22     rmind 	}
    659  1.22     rmind 	case NPF_CMD_RULE_LIST: {
    660  1.22     rmind 		retdict = npf_ruleset_list(rlset, ruleset_name);
    661  1.22     rmind 		break;
    662  1.22     rmind 	}
    663  1.22     rmind 	case NPF_CMD_RULE_FLUSH: {
    664  1.22     rmind 		error = npf_ruleset_flush(rlset, ruleset_name);
    665  1.21     rmind 		break;
    666  1.21     rmind 	}
    667  1.21     rmind 	default:
    668  1.21     rmind 		error = EINVAL;
    669  1.21     rmind 		break;
    670  1.21     rmind 	}
    671  1.22     rmind 
    672  1.22     rmind 	/* Destroy any removed rules. */
    673  1.22     rmind 	if (!error && rcmd != NPF_CMD_RULE_ADD && rcmd != NPF_CMD_RULE_LIST) {
    674  1.22     rmind 		npf_config_sync();
    675  1.22     rmind 		npf_ruleset_gc(rlset);
    676  1.22     rmind 	}
    677  1.21     rmind 	npf_config_exit();
    678  1.14     rmind 
    679  1.21     rmind 	if (rl) {
    680  1.21     rmind 		npf_rule_free(rl);
    681  1.21     rmind 	}
    682  1.27     rmind out:
    683  1.21     rmind 	if (retdict) {
    684  1.21     rmind 		prop_object_release(npf_rule);
    685  1.21     rmind 		prop_dictionary_copyout_ioctl(pref, cmd, retdict);
    686  1.21     rmind 		prop_object_release(retdict);
    687  1.21     rmind 	}
    688  1.14     rmind 	return error;
    689  1.14     rmind }
    690  1.14     rmind 
    691   1.6     rmind /*
    692  1.21     rmind  * npfctl_getconf: return the config dictionary as it was submitted.
    693  1.21     rmind  * Additionally, indicate whether the ruleset is currently active.
    694   1.6     rmind  */
    695   1.6     rmind int
    696  1.21     rmind npfctl_getconf(u_long cmd, void *data)
    697   1.6     rmind {
    698  1.12     rmind 	struct plistref *pref = data;
    699  1.21     rmind 	prop_dictionary_t npf_dict;
    700   1.6     rmind 	int error;
    701   1.6     rmind 
    702  1.21     rmind 	npf_config_enter();
    703  1.21     rmind 	npf_dict = npf_config_dict();
    704  1.21     rmind 	prop_dictionary_set_bool(npf_dict, "active", npf_pfil_registered_p());
    705  1.21     rmind 	error = prop_dictionary_copyout_ioctl(pref, cmd, npf_dict);
    706  1.21     rmind 	npf_config_exit();
    707  1.12     rmind 
    708   1.1     rmind 	return error;
    709   1.1     rmind }
    710   1.1     rmind 
    711   1.1     rmind /*
    712  1.34     rmind  * npfctl_conn_save: construct a list of connections and export.
    713   1.4     rmind  */
    714   1.4     rmind int
    715  1.34     rmind npfctl_conn_save(u_long cmd, void *data)
    716   1.4     rmind {
    717   1.4     rmind 	struct plistref *pref = data;
    718  1.34     rmind 	prop_array_t conlist, nplist;
    719  1.34     rmind 	prop_dictionary_t dict;
    720   1.4     rmind 	int error;
    721   1.4     rmind 
    722   1.4     rmind 	/* Create a dictionary and two lists. */
    723  1.34     rmind 	dict = prop_dictionary_create();
    724  1.34     rmind 	conlist = prop_array_create();
    725   1.4     rmind 	nplist = prop_array_create();
    726   1.4     rmind 
    727  1.34     rmind 	/* Save the connections. */
    728  1.34     rmind 	error = npf_conn_save(conlist, nplist);
    729   1.4     rmind 	if (error) {
    730   1.4     rmind 		goto fail;
    731   1.4     rmind 	}
    732   1.4     rmind 
    733  1.34     rmind 	/* Set the connection list, NAT policy list and export. */
    734  1.34     rmind 	prop_dictionary_set(dict, "session-list", conlist);
    735  1.34     rmind 	prop_dictionary_set(dict, "nat-policy-list", nplist);
    736  1.34     rmind 	error = prop_dictionary_copyout_ioctl(pref, cmd, dict);
    737   1.4     rmind fail:
    738  1.34     rmind 	prop_object_release(dict);
    739   1.4     rmind 	return error;
    740   1.4     rmind }
    741   1.4     rmind 
    742   1.4     rmind /*
    743  1.34     rmind  * npfctl_conn_load: import a list of connections and load them.
    744   1.4     rmind  */
    745   1.4     rmind int
    746  1.34     rmind npfctl_conn_load(u_long cmd, void *data)
    747   1.4     rmind {
    748   1.4     rmind 	const struct plistref *pref = data;
    749  1.34     rmind 	npf_conndb_t *conndb = NULL;
    750  1.34     rmind 	prop_dictionary_t dict, condict;
    751   1.4     rmind 	prop_object_iterator_t it;
    752  1.34     rmind 	prop_array_t conlist;
    753   1.4     rmind 	int error;
    754   1.4     rmind 
    755  1.34     rmind 	/* Get the dictionary containing connections and NAT policies. */
    756  1.34     rmind 	error = prop_dictionary_copyin_ioctl(pref, cmd, &dict);
    757   1.4     rmind 	if (error)
    758   1.4     rmind 		return error;
    759  1.15     rmind 
    760   1.4     rmind 	/*
    761  1.34     rmind 	 * Note: connection objects contain the references to the NAT
    762  1.34     rmind 	 * policy entries.  Therefore, no need to directly access it.
    763   1.4     rmind 	 */
    764  1.34     rmind 	conlist = prop_dictionary_get(dict, "session-list");
    765  1.34     rmind 	if (prop_object_type(conlist) != PROP_TYPE_ARRAY) {
    766  1.34     rmind 		prop_object_release(conlist);
    767  1.21     rmind 		return EINVAL;
    768   1.4     rmind 	}
    769   1.4     rmind 
    770  1.34     rmind 	/* Create a connection database. */
    771  1.34     rmind 	conndb = npf_conndb_create();
    772   1.4     rmind 
    773   1.4     rmind 	/*
    774  1.34     rmind 	 * Iterate through and construct each connection.  Note: acquire
    775  1.34     rmind 	 * the config lock as we access NAT policies during the restore.
    776   1.4     rmind 	 */
    777   1.4     rmind 	error = 0;
    778  1.34     rmind 	it = prop_array_iterator(conlist);
    779  1.26     rmind 
    780  1.21     rmind 	npf_config_enter();
    781  1.34     rmind 	while ((condict = prop_object_iterator_next(it)) != NULL) {
    782  1.34     rmind 		/* Connection - dictionary. */
    783  1.34     rmind 		if (prop_object_type(condict) != PROP_TYPE_DICTIONARY) {
    784   1.4     rmind 			error = EINVAL;
    785  1.26     rmind 			break;
    786   1.4     rmind 		}
    787  1.34     rmind 		/* Construct and insert real connection structure. */
    788  1.34     rmind 		error = npf_conn_restore(conndb, condict);
    789   1.4     rmind 		if (error) {
    790  1.26     rmind 			break;
    791   1.4     rmind 		}
    792   1.4     rmind 	}
    793  1.21     rmind 	npf_config_exit();
    794  1.26     rmind 
    795  1.21     rmind 	prop_object_iterator_release(it);
    796  1.34     rmind 	prop_object_release(conlist);
    797  1.26     rmind 
    798  1.26     rmind 	if (!error) {
    799  1.26     rmind 		/* Finally, load the new table. */
    800  1.34     rmind 		npf_conn_load(conndb);
    801  1.26     rmind 	} else {
    802  1.34     rmind 		/* Destroy the connection database. */
    803  1.34     rmind 		npf_conndb_destroy(conndb);
    804   1.4     rmind 	}
    805   1.4     rmind 	return error;
    806   1.4     rmind }
    807   1.4     rmind 
    808   1.4     rmind /*
    809   1.2     rmind  * npfctl_table: add, remove or query entries in the specified table.
    810   1.1     rmind  *
    811   1.1     rmind  * For maximum performance, interface is avoiding proplib(3)'s overhead.
    812   1.1     rmind  */
    813   1.1     rmind int
    814   1.1     rmind npfctl_table(void *data)
    815   1.1     rmind {
    816  1.19     rmind 	const npf_ioctl_table_t *nct = data;
    817  1.32     rmind 	char tname[NPF_TABLE_MAXNAMELEN];
    818  1.32     rmind 	npf_tableset_t *ts;
    819  1.32     rmind 	npf_table_t *t;
    820  1.32     rmind 	int s, error;
    821  1.32     rmind 
    822  1.32     rmind 	error = copyinstr(nct->nct_name, tname, sizeof(tname), NULL);
    823  1.32     rmind 	if (error) {
    824  1.32     rmind 		return error;
    825  1.32     rmind 	}
    826   1.1     rmind 
    827  1.32     rmind 	s = npf_config_read_enter(); /* XXX */
    828  1.32     rmind 	ts = npf_config_tableset();
    829  1.32     rmind 	if ((t = npf_tableset_getbyname(ts, tname)) == NULL) {
    830  1.32     rmind 		npf_config_read_exit(s);
    831  1.32     rmind 		return EINVAL;
    832  1.32     rmind 	}
    833  1.21     rmind 
    834  1.21     rmind 	switch (nct->nct_cmd) {
    835  1.21     rmind 	case NPF_CMD_TABLE_LOOKUP:
    836  1.32     rmind 		error = npf_table_lookup(t, nct->nct_data.ent.alen,
    837  1.32     rmind 		    &nct->nct_data.ent.addr);
    838  1.20     rmind 		break;
    839  1.21     rmind 	case NPF_CMD_TABLE_ADD:
    840  1.32     rmind 		error = npf_table_insert(t, nct->nct_data.ent.alen,
    841  1.32     rmind 		    &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
    842   1.1     rmind 		break;
    843  1.21     rmind 	case NPF_CMD_TABLE_REMOVE:
    844  1.32     rmind 		error = npf_table_remove(t, nct->nct_data.ent.alen,
    845  1.32     rmind 		    &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
    846  1.19     rmind 		break;
    847  1.21     rmind 	case NPF_CMD_TABLE_LIST:
    848  1.32     rmind 		error = npf_table_list(t, nct->nct_data.buf.buf,
    849  1.32     rmind 		    nct->nct_data.buf.len);
    850   1.1     rmind 		break;
    851  1.25     rmind 	case NPF_CMD_TABLE_FLUSH:
    852  1.32     rmind 		error = npf_table_flush(t);
    853  1.25     rmind 		break;
    854   1.1     rmind 	default:
    855  1.19     rmind 		error = EINVAL;
    856  1.19     rmind 		break;
    857   1.1     rmind 	}
    858  1.32     rmind 	npf_config_read_exit(s);
    859  1.21     rmind 
    860   1.1     rmind 	return error;
    861   1.1     rmind }
    862