Home | History | Annotate | Line # | Download | only in npf
npf_conf.c revision 1.5.2.1
      1  1.5.2.1    tls /*	$NetBSD: npf_conf.c,v 1.5.2.1 2014/08/10 06:56:16 tls Exp $	*/
      2      1.1  rmind 
      3      1.1  rmind /*-
      4      1.1  rmind  * Copyright (c) 2013 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 config loading mechanism.
     34      1.1  rmind  *
     35      1.1  rmind  * There are few main operations on the config:
     36      1.1  rmind  * 1) Read access which is primarily from the npf_packet_handler() et al.
     37      1.1  rmind  * 2) Write access on particular set, mainly rule or table updates.
     38      1.1  rmind  * 3) Deletion of the config, which is done during the reload operation.
     39      1.1  rmind  *
     40      1.1  rmind  * Synchronisation
     41      1.1  rmind  *
     42      1.1  rmind  *	For (1) case, passive serialisation is used to allow concurrent
     43      1.1  rmind  *	access to the configuration set (ruleset, etc).  It guarantees
     44      1.1  rmind  *	that the config will not be destroyed while accessing it.
     45      1.1  rmind  *
     46      1.1  rmind  *	Writers, i.e. cases (2) and (3) use mutual exclusion and when
     47      1.1  rmind  *	necessary writer-side barrier of the passive serialisation.
     48      1.1  rmind  */
     49      1.1  rmind 
     50      1.1  rmind #include <sys/cdefs.h>
     51  1.5.2.1    tls __KERNEL_RCSID(0, "$NetBSD: npf_conf.c,v 1.5.2.1 2014/08/10 06:56:16 tls Exp $");
     52      1.1  rmind 
     53      1.1  rmind #include <sys/param.h>
     54      1.1  rmind #include <sys/types.h>
     55      1.1  rmind 
     56      1.1  rmind #include <sys/atomic.h>
     57      1.1  rmind #include <sys/kmem.h>
     58      1.1  rmind #include <sys/pserialize.h>
     59      1.1  rmind #include <sys/mutex.h>
     60      1.1  rmind 
     61      1.1  rmind #include "npf_impl.h"
     62  1.5.2.1    tls #include "npf_conn.h"
     63      1.1  rmind 
     64      1.1  rmind typedef struct {
     65      1.1  rmind 	npf_ruleset_t *		n_rules;
     66      1.1  rmind 	npf_tableset_t *	n_tables;
     67      1.1  rmind 	npf_ruleset_t *		n_nat_rules;
     68      1.1  rmind 	npf_rprocset_t *	n_rprocs;
     69      1.1  rmind 	prop_dictionary_t	n_dict;
     70      1.1  rmind 	bool			n_default_pass;
     71      1.1  rmind } npf_config_t;
     72      1.1  rmind 
     73      1.1  rmind static npf_config_t *		npf_config		__cacheline_aligned;
     74      1.1  rmind static kmutex_t			npf_config_lock		__cacheline_aligned;
     75      1.1  rmind static pserialize_t		npf_config_psz		__cacheline_aligned;
     76      1.1  rmind 
     77      1.1  rmind void
     78      1.1  rmind npf_config_init(void)
     79      1.1  rmind {
     80      1.1  rmind 	prop_dictionary_t dict;
     81      1.1  rmind 	npf_ruleset_t *rlset, *nset;
     82      1.1  rmind 	npf_rprocset_t *rpset;
     83      1.1  rmind 	npf_tableset_t *tset;
     84      1.1  rmind 
     85      1.1  rmind 	mutex_init(&npf_config_lock, MUTEX_DEFAULT, IPL_SOFTNET);
     86      1.1  rmind 	npf_config_psz = pserialize_create();
     87      1.1  rmind 
     88      1.1  rmind 	/* Load the empty configuration. */
     89      1.1  rmind 	dict = prop_dictionary_create();
     90      1.4  rmind 	tset = npf_tableset_create(0);
     91      1.1  rmind 	rpset = npf_rprocset_create();
     92      1.1  rmind 	rlset = npf_ruleset_create(0);
     93      1.1  rmind 	nset = npf_ruleset_create(0);
     94  1.5.2.1    tls 	npf_config_load(dict, rlset, tset, nset, rpset, NULL, true);
     95      1.1  rmind 	KASSERT(npf_config != NULL);
     96      1.1  rmind }
     97      1.1  rmind 
     98      1.1  rmind static void
     99      1.1  rmind npf_config_destroy(npf_config_t *nc)
    100      1.1  rmind {
    101      1.1  rmind 	prop_object_release(nc->n_dict);
    102      1.1  rmind 	npf_ruleset_destroy(nc->n_rules);
    103      1.1  rmind 	npf_ruleset_destroy(nc->n_nat_rules);
    104      1.1  rmind 	npf_rprocset_destroy(nc->n_rprocs);
    105      1.1  rmind 	npf_tableset_destroy(nc->n_tables);
    106      1.1  rmind 	kmem_free(nc, sizeof(npf_config_t));
    107      1.1  rmind }
    108      1.1  rmind 
    109      1.1  rmind void
    110      1.1  rmind npf_config_fini(void)
    111      1.1  rmind {
    112  1.5.2.1    tls 	/* Flush the connections. */
    113      1.3  rmind 	mutex_enter(&npf_config_lock);
    114  1.5.2.1    tls 	npf_conn_tracking(false);
    115      1.3  rmind 	pserialize_perform(npf_config_psz);
    116  1.5.2.1    tls 	npf_conn_load(NULL, false);
    117      1.3  rmind 	npf_ifmap_flush();
    118      1.3  rmind 	mutex_exit(&npf_config_lock);
    119      1.3  rmind 
    120      1.1  rmind 	npf_config_destroy(npf_config);
    121      1.1  rmind 	pserialize_destroy(npf_config_psz);
    122      1.1  rmind 	mutex_destroy(&npf_config_lock);
    123      1.1  rmind }
    124      1.1  rmind 
    125      1.1  rmind /*
    126  1.5.2.1    tls  * npf_config_load: the main routine performing configuration load.
    127      1.1  rmind  * Performs the necessary synchronisation and destroys the old config.
    128      1.1  rmind  */
    129      1.1  rmind void
    130  1.5.2.1    tls npf_config_load(prop_dictionary_t dict, npf_ruleset_t *rset,
    131      1.1  rmind     npf_tableset_t *tset, npf_ruleset_t *nset, npf_rprocset_t *rpset,
    132  1.5.2.1    tls     npf_conndb_t *conns, bool flush)
    133      1.1  rmind {
    134      1.1  rmind 	npf_config_t *nc, *onc;
    135      1.1  rmind 
    136      1.1  rmind 	nc = kmem_zalloc(sizeof(npf_config_t), KM_SLEEP);
    137      1.1  rmind 	nc->n_rules = rset;
    138      1.1  rmind 	nc->n_tables = tset;
    139      1.1  rmind 	nc->n_nat_rules = nset;
    140      1.1  rmind 	nc->n_rprocs = rpset;
    141      1.1  rmind 	nc->n_dict = dict;
    142      1.1  rmind 	nc->n_default_pass = flush;
    143      1.1  rmind 
    144      1.1  rmind 	/*
    145      1.1  rmind 	 * Acquire the lock and perform the first phase:
    146      1.1  rmind 	 * - Scan and use existing dynamic tables, reload only static.
    147      1.1  rmind 	 * - Scan and use matching NAT policies to preserve the connections.
    148      1.1  rmind 	 */
    149      1.1  rmind 	mutex_enter(&npf_config_lock);
    150      1.1  rmind 	if ((onc = npf_config) != NULL) {
    151      1.1  rmind 		npf_ruleset_reload(rset, onc->n_rules);
    152      1.1  rmind 		npf_tableset_reload(tset, onc->n_tables);
    153  1.5.2.1    tls 		npf_ruleset_reload(nset, onc->n_nat_rules);
    154      1.1  rmind 	}
    155      1.1  rmind 
    156      1.1  rmind 	/*
    157      1.1  rmind 	 * Set the new config and release the lock.
    158      1.1  rmind 	 */
    159      1.1  rmind 	membar_sync();
    160      1.1  rmind 	npf_config = nc;
    161      1.1  rmind 	if (onc == NULL) {
    162      1.1  rmind 		/* Initial load, done. */
    163      1.3  rmind 		npf_ifmap_flush();
    164  1.5.2.1    tls 		npf_conn_load(conns, !flush);
    165      1.1  rmind 		mutex_exit(&npf_config_lock);
    166      1.1  rmind 		return;
    167      1.1  rmind 	}
    168      1.1  rmind 
    169  1.5.2.1    tls 	/*
    170  1.5.2.1    tls 	 * If we are going to flush the connections or load the new ones,
    171  1.5.2.1    tls 	 * then disable the connection tracking for the grace period.
    172  1.5.2.1    tls 	 */
    173  1.5.2.1    tls 	if (flush || conns) {
    174  1.5.2.1    tls 		npf_conn_tracking(false);
    175  1.5.2.1    tls 	}
    176  1.5.2.1    tls 
    177      1.1  rmind 	/* Synchronise: drain all references. */
    178      1.1  rmind 	pserialize_perform(npf_config_psz);
    179      1.3  rmind 	if (flush) {
    180      1.3  rmind 		npf_ifmap_flush();
    181      1.3  rmind 	}
    182      1.5  rmind 
    183  1.5.2.1    tls 	/*
    184  1.5.2.1    tls 	 * G/C the existing connections and, if passed, load the new ones.
    185  1.5.2.1    tls 	 * If not flushing - enable the connection tracking.
    186  1.5.2.1    tls 	 */
    187  1.5.2.1    tls 	npf_conn_load(conns, !flush);
    188  1.5.2.1    tls 
    189      1.5  rmind 	/* Sync the config proplib data. */
    190      1.5  rmind 	npf_tableset_syncdict(tset, dict);
    191      1.1  rmind 	mutex_exit(&npf_config_lock);
    192      1.1  rmind 
    193      1.1  rmind 	/* Finally, it is safe to destroy the old config. */
    194      1.1  rmind 	npf_config_destroy(onc);
    195      1.1  rmind }
    196      1.1  rmind 
    197      1.1  rmind /*
    198      1.1  rmind  * Writer-side exclusive locking.
    199      1.1  rmind  */
    200      1.1  rmind 
    201      1.1  rmind void
    202      1.1  rmind npf_config_enter(void)
    203      1.1  rmind {
    204      1.1  rmind 	mutex_enter(&npf_config_lock);
    205      1.1  rmind }
    206      1.1  rmind 
    207      1.1  rmind void
    208      1.1  rmind npf_config_exit(void)
    209      1.1  rmind {
    210      1.1  rmind 	mutex_exit(&npf_config_lock);
    211      1.1  rmind }
    212      1.1  rmind 
    213      1.1  rmind bool
    214      1.1  rmind npf_config_locked_p(void)
    215      1.1  rmind {
    216      1.1  rmind 	return mutex_owned(&npf_config_lock);
    217      1.1  rmind }
    218      1.1  rmind 
    219      1.2  rmind void
    220      1.2  rmind npf_config_sync(void)
    221      1.2  rmind {
    222      1.2  rmind 	KASSERT(npf_config_locked_p());
    223      1.2  rmind 	pserialize_perform(npf_config_psz);
    224      1.2  rmind }
    225      1.2  rmind 
    226      1.1  rmind /*
    227      1.1  rmind  * Reader-side synchronisation routines.
    228      1.1  rmind  */
    229      1.1  rmind 
    230      1.1  rmind int
    231      1.1  rmind npf_config_read_enter(void)
    232      1.1  rmind {
    233      1.1  rmind 	return pserialize_read_enter();
    234      1.1  rmind }
    235      1.1  rmind 
    236      1.1  rmind void
    237      1.1  rmind npf_config_read_exit(int s)
    238      1.1  rmind {
    239      1.1  rmind 	pserialize_read_exit(s);
    240      1.1  rmind }
    241      1.1  rmind 
    242      1.1  rmind /*
    243      1.1  rmind  * Accessors.
    244      1.1  rmind  */
    245      1.1  rmind 
    246      1.1  rmind npf_ruleset_t *
    247      1.1  rmind npf_config_ruleset(void)
    248      1.1  rmind {
    249      1.1  rmind 	return npf_config->n_rules;
    250      1.1  rmind }
    251      1.1  rmind 
    252      1.1  rmind npf_ruleset_t *
    253      1.1  rmind npf_config_natset(void)
    254      1.1  rmind {
    255      1.1  rmind 	return npf_config->n_nat_rules;
    256      1.1  rmind }
    257      1.1  rmind 
    258      1.1  rmind npf_tableset_t *
    259      1.1  rmind npf_config_tableset(void)
    260      1.1  rmind {
    261      1.1  rmind 	return npf_config->n_tables;
    262      1.1  rmind }
    263      1.1  rmind 
    264      1.1  rmind prop_dictionary_t
    265      1.1  rmind npf_config_dict(void)
    266      1.1  rmind {
    267      1.1  rmind 	return npf_config->n_dict;
    268      1.1  rmind }
    269      1.1  rmind 
    270      1.1  rmind bool
    271      1.1  rmind npf_default_pass(void)
    272      1.1  rmind {
    273      1.1  rmind 	return npf_config->n_default_pass;
    274      1.1  rmind }
    275