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