npf_conf.c revision 1.5.4.2       1  1.5.4.2  yamt /*	$NetBSD: npf_conf.c,v 1.5.4.2 2014/05/22 11:41:09 yamt Exp $	*/
      2  1.5.4.2  yamt 
      3  1.5.4.2  yamt /*-
      4  1.5.4.2  yamt  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.5.4.2  yamt  * All rights reserved.
      6  1.5.4.2  yamt  *
      7  1.5.4.2  yamt  * This material is based upon work partially supported by The
      8  1.5.4.2  yamt  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9  1.5.4.2  yamt  *
     10  1.5.4.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.5.4.2  yamt  * modification, are permitted provided that the following conditions
     12  1.5.4.2  yamt  * are met:
     13  1.5.4.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.5.4.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.5.4.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.5.4.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.5.4.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.5.4.2  yamt  *
     19  1.5.4.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.5.4.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.5.4.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.5.4.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.5.4.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.5.4.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.5.4.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.5.4.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.5.4.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.5.4.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.5.4.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.5.4.2  yamt  */
     31  1.5.4.2  yamt 
     32  1.5.4.2  yamt /*
     33  1.5.4.2  yamt  * NPF config loading mechanism.
     34  1.5.4.2  yamt  *
     35  1.5.4.2  yamt  * There are few main operations on the config:
     36  1.5.4.2  yamt  * 1) Read access which is primarily from the npf_packet_handler() et al.
     37  1.5.4.2  yamt  * 2) Write access on particular set, mainly rule or table updates.
     38  1.5.4.2  yamt  * 3) Deletion of the config, which is done during the reload operation.
     39  1.5.4.2  yamt  *
     40  1.5.4.2  yamt  * Synchronisation
     41  1.5.4.2  yamt  *
     42  1.5.4.2  yamt  *	For (1) case, passive serialisation is used to allow concurrent
     43  1.5.4.2  yamt  *	access to the configuration set (ruleset, etc).  It guarantees
     44  1.5.4.2  yamt  *	that the config will not be destroyed while accessing it.
     45  1.5.4.2  yamt  *
     46  1.5.4.2  yamt  *	Writers, i.e. cases (2) and (3) use mutual exclusion and when
     47  1.5.4.2  yamt  *	necessary writer-side barrier of the passive serialisation.
     48  1.5.4.2  yamt  */
     49  1.5.4.2  yamt 
     50  1.5.4.2  yamt #include <sys/cdefs.h>
     51  1.5.4.2  yamt __KERNEL_RCSID(0, "$NetBSD: npf_conf.c,v 1.5.4.2 2014/05/22 11:41:09 yamt Exp $");
     52  1.5.4.2  yamt 
     53  1.5.4.2  yamt #include <sys/param.h>
     54  1.5.4.2  yamt #include <sys/types.h>
     55  1.5.4.2  yamt 
     56  1.5.4.2  yamt #include <sys/atomic.h>
     57  1.5.4.2  yamt #include <sys/kmem.h>
     58  1.5.4.2  yamt #include <sys/pserialize.h>
     59  1.5.4.2  yamt #include <sys/mutex.h>
     60  1.5.4.2  yamt 
     61  1.5.4.2  yamt #include "npf_impl.h"
     62  1.5.4.2  yamt 
     63  1.5.4.2  yamt typedef struct {
     64  1.5.4.2  yamt 	npf_ruleset_t *		n_rules;
     65  1.5.4.2  yamt 	npf_tableset_t *	n_tables;
     66  1.5.4.2  yamt 	npf_ruleset_t *		n_nat_rules;
     67  1.5.4.2  yamt 	npf_rprocset_t *	n_rprocs;
     68  1.5.4.2  yamt 	prop_dictionary_t	n_dict;
     69  1.5.4.2  yamt 	bool			n_default_pass;
     70  1.5.4.2  yamt } npf_config_t;
     71  1.5.4.2  yamt 
     72  1.5.4.2  yamt static npf_config_t *		npf_config		__cacheline_aligned;
     73  1.5.4.2  yamt static kmutex_t			npf_config_lock		__cacheline_aligned;
     74  1.5.4.2  yamt static pserialize_t		npf_config_psz		__cacheline_aligned;
     75  1.5.4.2  yamt 
     76  1.5.4.2  yamt void
     77  1.5.4.2  yamt npf_config_init(void)
     78  1.5.4.2  yamt {
     79  1.5.4.2  yamt 	prop_dictionary_t dict;
     80  1.5.4.2  yamt 	npf_ruleset_t *rlset, *nset;
     81  1.5.4.2  yamt 	npf_rprocset_t *rpset;
     82  1.5.4.2  yamt 	npf_tableset_t *tset;
     83  1.5.4.2  yamt 
     84  1.5.4.2  yamt 	mutex_init(&npf_config_lock, MUTEX_DEFAULT, IPL_SOFTNET);
     85  1.5.4.2  yamt 	npf_config_psz = pserialize_create();
     86  1.5.4.2  yamt 
     87  1.5.4.2  yamt 	/* Load the empty configuration. */
     88  1.5.4.2  yamt 	dict = prop_dictionary_create();
     89  1.5.4.2  yamt 	tset = npf_tableset_create(0);
     90  1.5.4.2  yamt 	rpset = npf_rprocset_create();
     91  1.5.4.2  yamt 	rlset = npf_ruleset_create(0);
     92  1.5.4.2  yamt 	nset = npf_ruleset_create(0);
     93  1.5.4.2  yamt 	npf_config_reload(dict, rlset, tset, nset, rpset, true);
     94  1.5.4.2  yamt 	KASSERT(npf_config != NULL);
     95  1.5.4.2  yamt }
     96  1.5.4.2  yamt 
     97  1.5.4.2  yamt static void
     98  1.5.4.2  yamt npf_config_destroy(npf_config_t *nc)
     99  1.5.4.2  yamt {
    100  1.5.4.2  yamt 	prop_object_release(nc->n_dict);
    101  1.5.4.2  yamt 	npf_ruleset_destroy(nc->n_rules);
    102  1.5.4.2  yamt 	npf_ruleset_destroy(nc->n_nat_rules);
    103  1.5.4.2  yamt 	npf_rprocset_destroy(nc->n_rprocs);
    104  1.5.4.2  yamt 	npf_tableset_destroy(nc->n_tables);
    105  1.5.4.2  yamt 	kmem_free(nc, sizeof(npf_config_t));
    106  1.5.4.2  yamt }
    107  1.5.4.2  yamt 
    108  1.5.4.2  yamt void
    109  1.5.4.2  yamt npf_config_fini(void)
    110  1.5.4.2  yamt {
    111  1.5.4.2  yamt 	mutex_enter(&npf_config_lock);
    112  1.5.4.2  yamt 	pserialize_perform(npf_config_psz);
    113  1.5.4.2  yamt 	npf_ifmap_flush();
    114  1.5.4.2  yamt 	mutex_exit(&npf_config_lock);
    115  1.5.4.2  yamt 
    116  1.5.4.2  yamt 	npf_config_destroy(npf_config);
    117  1.5.4.2  yamt 	pserialize_destroy(npf_config_psz);
    118  1.5.4.2  yamt 	mutex_destroy(&npf_config_lock);
    119  1.5.4.2  yamt }
    120  1.5.4.2  yamt 
    121  1.5.4.2  yamt /*
    122  1.5.4.2  yamt  * npf_config_reload: the main routine performing configuration reload.
    123  1.5.4.2  yamt  * Performs the necessary synchronisation and destroys the old config.
    124  1.5.4.2  yamt  */
    125  1.5.4.2  yamt void
    126  1.5.4.2  yamt npf_config_reload(prop_dictionary_t dict, npf_ruleset_t *rset,
    127  1.5.4.2  yamt     npf_tableset_t *tset, npf_ruleset_t *nset, npf_rprocset_t *rpset,
    128  1.5.4.2  yamt     bool flush)
    129  1.5.4.2  yamt {
    130  1.5.4.2  yamt 	npf_config_t *nc, *onc;
    131  1.5.4.2  yamt 
    132  1.5.4.2  yamt 	nc = kmem_zalloc(sizeof(npf_config_t), KM_SLEEP);
    133  1.5.4.2  yamt 	nc->n_rules = rset;
    134  1.5.4.2  yamt 	nc->n_tables = tset;
    135  1.5.4.2  yamt 	nc->n_nat_rules = nset;
    136  1.5.4.2  yamt 	nc->n_rprocs = rpset;
    137  1.5.4.2  yamt 	nc->n_dict = dict;
    138  1.5.4.2  yamt 	nc->n_default_pass = flush;
    139  1.5.4.2  yamt 
    140  1.5.4.2  yamt 	/*
    141  1.5.4.2  yamt 	 * Acquire the lock and perform the first phase:
    142  1.5.4.2  yamt 	 * - Scan and use existing dynamic tables, reload only static.
    143  1.5.4.2  yamt 	 * - Scan and use matching NAT policies to preserve the connections.
    144  1.5.4.2  yamt 	 */
    145  1.5.4.2  yamt 	mutex_enter(&npf_config_lock);
    146  1.5.4.2  yamt 	if ((onc = npf_config) != NULL) {
    147  1.5.4.2  yamt 		npf_ruleset_reload(rset, onc->n_rules);
    148  1.5.4.2  yamt 		npf_tableset_reload(tset, onc->n_tables);
    149  1.5.4.2  yamt 		npf_ruleset_natreload(nset, onc->n_nat_rules);
    150  1.5.4.2  yamt 	}
    151  1.5.4.2  yamt 
    152  1.5.4.2  yamt 	/*
    153  1.5.4.2  yamt 	 * Set the new config and release the lock.
    154  1.5.4.2  yamt 	 */
    155  1.5.4.2  yamt 	membar_sync();
    156  1.5.4.2  yamt 	npf_config = nc;
    157  1.5.4.2  yamt 	if (onc == NULL) {
    158  1.5.4.2  yamt 		/* Initial load, done. */
    159  1.5.4.2  yamt 		npf_ifmap_flush();
    160  1.5.4.2  yamt 		mutex_exit(&npf_config_lock);
    161  1.5.4.2  yamt 		return;
    162  1.5.4.2  yamt 	}
    163  1.5.4.2  yamt 
    164  1.5.4.2  yamt 	/* Synchronise: drain all references. */
    165  1.5.4.2  yamt 	pserialize_perform(npf_config_psz);
    166  1.5.4.2  yamt 	if (flush) {
    167  1.5.4.2  yamt 		npf_ifmap_flush();
    168  1.5.4.2  yamt 	}
    169  1.5.4.2  yamt 
    170  1.5.4.2  yamt 	/* Sync the config proplib data. */
    171  1.5.4.2  yamt 	npf_tableset_syncdict(tset, dict);
    172  1.5.4.2  yamt 	mutex_exit(&npf_config_lock);
    173  1.5.4.2  yamt 
    174  1.5.4.2  yamt 	/* Finally, it is safe to destroy the old config. */
    175  1.5.4.2  yamt 	npf_config_destroy(onc);
    176  1.5.4.2  yamt }
    177  1.5.4.2  yamt 
    178  1.5.4.2  yamt /*
    179  1.5.4.2  yamt  * Writer-side exclusive locking.
    180  1.5.4.2  yamt  */
    181  1.5.4.2  yamt 
    182  1.5.4.2  yamt void
    183  1.5.4.2  yamt npf_config_enter(void)
    184  1.5.4.2  yamt {
    185  1.5.4.2  yamt 	mutex_enter(&npf_config_lock);
    186  1.5.4.2  yamt }
    187  1.5.4.2  yamt 
    188  1.5.4.2  yamt void
    189  1.5.4.2  yamt npf_config_exit(void)
    190  1.5.4.2  yamt {
    191  1.5.4.2  yamt 	mutex_exit(&npf_config_lock);
    192  1.5.4.2  yamt }
    193  1.5.4.2  yamt 
    194  1.5.4.2  yamt bool
    195  1.5.4.2  yamt npf_config_locked_p(void)
    196  1.5.4.2  yamt {
    197  1.5.4.2  yamt 	return mutex_owned(&npf_config_lock);
    198  1.5.4.2  yamt }
    199  1.5.4.2  yamt 
    200  1.5.4.2  yamt void
    201  1.5.4.2  yamt npf_config_sync(void)
    202  1.5.4.2  yamt {
    203  1.5.4.2  yamt 	KASSERT(npf_config_locked_p());
    204  1.5.4.2  yamt 	pserialize_perform(npf_config_psz);
    205  1.5.4.2  yamt }
    206  1.5.4.2  yamt 
    207  1.5.4.2  yamt /*
    208  1.5.4.2  yamt  * Reader-side synchronisation routines.
    209  1.5.4.2  yamt  */
    210  1.5.4.2  yamt 
    211  1.5.4.2  yamt int
    212  1.5.4.2  yamt npf_config_read_enter(void)
    213  1.5.4.2  yamt {
    214  1.5.4.2  yamt 	return pserialize_read_enter();
    215  1.5.4.2  yamt }
    216  1.5.4.2  yamt 
    217  1.5.4.2  yamt void
    218  1.5.4.2  yamt npf_config_read_exit(int s)
    219  1.5.4.2  yamt {
    220  1.5.4.2  yamt 	pserialize_read_exit(s);
    221  1.5.4.2  yamt }
    222  1.5.4.2  yamt 
    223  1.5.4.2  yamt /*
    224  1.5.4.2  yamt  * Accessors.
    225  1.5.4.2  yamt  */
    226  1.5.4.2  yamt 
    227  1.5.4.2  yamt npf_ruleset_t *
    228  1.5.4.2  yamt npf_config_ruleset(void)
    229  1.5.4.2  yamt {
    230  1.5.4.2  yamt 	return npf_config->n_rules;
    231  1.5.4.2  yamt }
    232  1.5.4.2  yamt 
    233  1.5.4.2  yamt npf_ruleset_t *
    234  1.5.4.2  yamt npf_config_natset(void)
    235  1.5.4.2  yamt {
    236  1.5.4.2  yamt 	return npf_config->n_nat_rules;
    237  1.5.4.2  yamt }
    238  1.5.4.2  yamt 
    239  1.5.4.2  yamt npf_tableset_t *
    240  1.5.4.2  yamt npf_config_tableset(void)
    241  1.5.4.2  yamt {
    242  1.5.4.2  yamt 	return npf_config->n_tables;
    243  1.5.4.2  yamt }
    244  1.5.4.2  yamt 
    245  1.5.4.2  yamt prop_dictionary_t
    246  1.5.4.2  yamt npf_config_dict(void)
    247  1.5.4.2  yamt {
    248  1.5.4.2  yamt 	return npf_config->n_dict;
    249  1.5.4.2  yamt }
    250  1.5.4.2  yamt 
    251  1.5.4.2  yamt bool
    252  1.5.4.2  yamt npf_default_pass(void)
    253  1.5.4.2  yamt {
    254  1.5.4.2  yamt 	return npf_config->n_default_pass;
    255  1.5.4.2  yamt }
    256