Home | History | Annotate | Line # | Download | only in npf
npf_conf.c revision 1.9.2.1
      1 /*	$NetBSD: npf_conf.c,v 1.9.2.1 2017/01/07 08:56:50 pgoyette 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 #ifdef _KERNEL
     51 #include <sys/cdefs.h>
     52 __KERNEL_RCSID(0, "$NetBSD: npf_conf.c,v 1.9.2.1 2017/01/07 08:56:50 pgoyette Exp $");
     53 
     54 #include <sys/param.h>
     55 #include <sys/types.h>
     56 
     57 #include <sys/atomic.h>
     58 #include <sys/kmem.h>
     59 #include <sys/pserialize.h>
     60 #include <sys/mutex.h>
     61 #endif
     62 
     63 #include "npf_impl.h"
     64 #include "npf_conn.h"
     65 
     66 struct npf_config {
     67 	npf_ruleset_t *		n_rules;
     68 	npf_tableset_t *	n_tables;
     69 	npf_ruleset_t *		n_nat_rules;
     70 	npf_rprocset_t *	n_rprocs;
     71 	bool			n_default_pass;
     72 };
     73 
     74 void
     75 npf_config_init(npf_t *npf)
     76 {
     77 	npf_ruleset_t *rlset, *nset;
     78 	npf_rprocset_t *rpset;
     79 	npf_tableset_t *tset;
     80 
     81 	mutex_init(&npf->config_lock, MUTEX_DEFAULT, IPL_SOFTNET);
     82 
     83 	/* Load the empty configuration. */
     84 	tset = npf_tableset_create(0);
     85 	rpset = npf_rprocset_create();
     86 	rlset = npf_ruleset_create(0);
     87 	nset = npf_ruleset_create(0);
     88 	npf_config_load(npf, rlset, tset, nset, rpset, NULL, true);
     89 	KASSERT(npf->config != NULL);
     90 }
     91 
     92 static void
     93 npf_config_destroy(npf_config_t *nc)
     94 {
     95 	npf_ruleset_destroy(nc->n_rules);
     96 	npf_ruleset_destroy(nc->n_nat_rules);
     97 	npf_rprocset_destroy(nc->n_rprocs);
     98 	npf_tableset_destroy(nc->n_tables);
     99 	kmem_free(nc, sizeof(npf_config_t));
    100 }
    101 
    102 void
    103 npf_config_fini(npf_t *npf)
    104 {
    105 	npf_conndb_t *cd = npf_conndb_create();
    106 
    107 	/* Flush the connections. */
    108 	mutex_enter(&npf->config_lock);
    109 	npf_conn_tracking(npf, false);
    110 	pserialize_perform(npf->qsbr);
    111 	npf_conn_load(npf, cd, false);
    112 	npf_ifmap_flush(npf);
    113 	mutex_exit(&npf->config_lock);
    114 
    115 	npf_config_destroy(npf->config);
    116 	mutex_destroy(&npf->config_lock);
    117 }
    118 
    119 /*
    120  * npf_config_load: the main routine performing configuration load.
    121  * Performs the necessary synchronisation and destroys the old config.
    122  */
    123 void
    124 npf_config_load(npf_t *npf, npf_ruleset_t *rset, npf_tableset_t *tset,
    125     npf_ruleset_t *nset, npf_rprocset_t *rpset,
    126     npf_conndb_t *conns, bool flush)
    127 {
    128 	const bool load = conns != NULL;
    129 	npf_config_t *nc, *onc;
    130 
    131 	nc = kmem_zalloc(sizeof(npf_config_t), KM_SLEEP);
    132 	nc->n_rules = rset;
    133 	nc->n_tables = tset;
    134 	nc->n_nat_rules = nset;
    135 	nc->n_rprocs = rpset;
    136 	nc->n_default_pass = flush;
    137 
    138 	/*
    139 	 * Acquire the lock and perform the first phase:
    140 	 * - Scan and use existing dynamic tables, reload only static.
    141 	 * - Scan and use matching NAT policies to preserve the connections.
    142 	 */
    143 	mutex_enter(&npf->config_lock);
    144 	if ((onc = npf->config) != NULL) {
    145 		npf_ruleset_reload(npf, rset, onc->n_rules, load);
    146 		npf_tableset_reload(npf, tset, onc->n_tables);
    147 		npf_ruleset_reload(npf, nset, onc->n_nat_rules, load);
    148 	}
    149 
    150 	/*
    151 	 * Set the new config and release the lock.
    152 	 */
    153 	membar_sync();
    154 	npf->config = nc;
    155 	if (onc == NULL) {
    156 		/* Initial load, done. */
    157 		npf_ifmap_flush(npf);
    158 		npf_conn_load(npf, conns, !flush);
    159 		mutex_exit(&npf->config_lock);
    160 		goto done;
    161 	}
    162 
    163 	/*
    164 	 * If we are going to flush the connections or load the new ones,
    165 	 * then disable the connection tracking for the grace period.
    166 	 */
    167 	if (flush || conns) {
    168 		npf_conn_tracking(npf, false);
    169 	}
    170 
    171 	/* Synchronise: drain all references. */
    172 	pserialize_perform(npf->qsbr);
    173 	if (flush) {
    174 		npf_ifmap_flush(npf);
    175 	}
    176 
    177 	/*
    178 	 * G/C the existing connections and, if passed, load the new ones.
    179 	 * If not flushing - enable the connection tracking.
    180 	 */
    181 	npf_conn_load(npf, conns, !flush);
    182 	mutex_exit(&npf->config_lock);
    183 
    184 	/* Finally, it is safe to destroy the old config. */
    185 	npf_config_destroy(onc);
    186 done:
    187 	/* Sync all interface address tables (can be done asynchronously). */
    188 	npf_ifaddr_syncall(npf);
    189 }
    190 
    191 /*
    192  * Writer-side exclusive locking.
    193  */
    194 
    195 void
    196 npf_config_enter(npf_t *npf)
    197 {
    198 	mutex_enter(&npf->config_lock);
    199 }
    200 
    201 void
    202 npf_config_exit(npf_t *npf)
    203 {
    204 	mutex_exit(&npf->config_lock);
    205 }
    206 
    207 bool
    208 npf_config_locked_p(npf_t *npf)
    209 {
    210 	return mutex_owned(&npf->config_lock);
    211 }
    212 
    213 void
    214 npf_config_sync(npf_t *npf)
    215 {
    216 	KASSERT(npf_config_locked_p(npf));
    217 	pserialize_perform(npf->qsbr);
    218 }
    219 
    220 /*
    221  * Reader-side synchronisation routines.
    222  */
    223 
    224 int
    225 npf_config_read_enter(void)
    226 {
    227 	return pserialize_read_enter();
    228 }
    229 
    230 void
    231 npf_config_read_exit(int s)
    232 {
    233 	pserialize_read_exit(s);
    234 }
    235 
    236 /*
    237  * Accessors.
    238  */
    239 
    240 npf_ruleset_t *
    241 npf_config_ruleset(npf_t *npf)
    242 {
    243 	return npf->config->n_rules;
    244 }
    245 
    246 npf_ruleset_t *
    247 npf_config_natset(npf_t *npf)
    248 {
    249 	return npf->config->n_nat_rules;
    250 }
    251 
    252 npf_tableset_t *
    253 npf_config_tableset(npf_t *npf)
    254 {
    255 	return npf->config->n_tables;
    256 }
    257 
    258 npf_rprocset_t *
    259 npf_config_rprocs(npf_t *npf)
    260 {
    261 	return npf->config->n_rprocs;
    262 }
    263 
    264 bool
    265 npf_default_pass(npf_t *npf)
    266 {
    267 	return npf->config->n_default_pass;
    268 }
    269