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