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