xref: /netbsd-src/sys/net/npf/npf_conf.c (revision 80d9064ac03cbb6a4174695f0d5b237c8766d3d0)
1 /*	$NetBSD: npf_conf.c,v 1.8 2014/08/11 01:54:12 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.8 2014/08/11 01:54:12 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 	bool			n_default_pass;
70 } npf_config_t;
71 
72 static npf_config_t *		npf_config		__cacheline_aligned;
73 static kmutex_t			npf_config_lock		__cacheline_aligned;
74 static pserialize_t		npf_config_psz		__cacheline_aligned;
75 
76 void
77 npf_config_init(void)
78 {
79 	npf_ruleset_t *rlset, *nset;
80 	npf_rprocset_t *rpset;
81 	npf_tableset_t *tset;
82 
83 	mutex_init(&npf_config_lock, MUTEX_DEFAULT, IPL_SOFTNET);
84 	npf_config_psz = pserialize_create();
85 
86 	/* Load the empty configuration. */
87 	tset = npf_tableset_create(0);
88 	rpset = npf_rprocset_create();
89 	rlset = npf_ruleset_create(0);
90 	nset = npf_ruleset_create(0);
91 	npf_config_load(rlset, tset, nset, rpset, NULL, true);
92 	KASSERT(npf_config != NULL);
93 }
94 
95 static void
96 npf_config_destroy(npf_config_t *nc)
97 {
98 	npf_ruleset_destroy(nc->n_rules);
99 	npf_ruleset_destroy(nc->n_nat_rules);
100 	npf_rprocset_destroy(nc->n_rprocs);
101 	npf_tableset_destroy(nc->n_tables);
102 	kmem_free(nc, sizeof(npf_config_t));
103 }
104 
105 void
106 npf_config_fini(void)
107 {
108 	/* Flush the connections. */
109 	mutex_enter(&npf_config_lock);
110 	npf_conn_tracking(false);
111 	pserialize_perform(npf_config_psz);
112 	npf_conn_load(NULL, false);
113 	npf_ifmap_flush();
114 	mutex_exit(&npf_config_lock);
115 
116 	npf_config_destroy(npf_config);
117 	pserialize_destroy(npf_config_psz);
118 	mutex_destroy(&npf_config_lock);
119 }
120 
121 /*
122  * npf_config_load: the main routine performing configuration load.
123  * Performs the necessary synchronisation and destroys the old config.
124  */
125 void
126 npf_config_load(npf_ruleset_t *rset, npf_tableset_t *tset,
127     npf_ruleset_t *nset, npf_rprocset_t *rpset,
128     npf_conndb_t *conns, bool flush)
129 {
130 	npf_config_t *nc, *onc;
131 
132 	nc = kmem_zalloc(sizeof(npf_config_t), KM_SLEEP);
133 	nc->n_rules = rset;
134 	nc->n_tables = tset;
135 	nc->n_nat_rules = nset;
136 	nc->n_rprocs = rpset;
137 	nc->n_default_pass = flush;
138 
139 	/*
140 	 * Acquire the lock and perform the first phase:
141 	 * - Scan and use existing dynamic tables, reload only static.
142 	 * - Scan and use matching NAT policies to preserve the connections.
143 	 */
144 	mutex_enter(&npf_config_lock);
145 	if ((onc = npf_config) != NULL) {
146 		npf_ruleset_reload(rset, onc->n_rules);
147 		npf_tableset_reload(tset, onc->n_tables);
148 		npf_ruleset_reload(nset, onc->n_nat_rules);
149 	}
150 
151 	/*
152 	 * Set the new config and release the lock.
153 	 */
154 	membar_sync();
155 	npf_config = nc;
156 	if (onc == NULL) {
157 		/* Initial load, done. */
158 		npf_ifmap_flush();
159 		npf_conn_load(conns, !flush);
160 		mutex_exit(&npf_config_lock);
161 		return;
162 	}
163 
164 	/*
165 	 * If we are going to flush the connections or load the new ones,
166 	 * then disable the connection tracking for the grace period.
167 	 */
168 	if (flush || conns) {
169 		npf_conn_tracking(false);
170 	}
171 
172 	/* Synchronise: drain all references. */
173 	pserialize_perform(npf_config_psz);
174 	if (flush) {
175 		npf_ifmap_flush();
176 	}
177 
178 	/*
179 	 * G/C the existing connections and, if passed, load the new ones.
180 	 * If not flushing - enable the connection tracking.
181 	 */
182 	npf_conn_load(conns, !flush);
183 	mutex_exit(&npf_config_lock);
184 
185 	/* Finally, it is safe to destroy the old config. */
186 	npf_config_destroy(onc);
187 }
188 
189 /*
190  * Writer-side exclusive locking.
191  */
192 
193 void
194 npf_config_enter(void)
195 {
196 	mutex_enter(&npf_config_lock);
197 }
198 
199 void
200 npf_config_exit(void)
201 {
202 	mutex_exit(&npf_config_lock);
203 }
204 
205 bool
206 npf_config_locked_p(void)
207 {
208 	return mutex_owned(&npf_config_lock);
209 }
210 
211 void
212 npf_config_sync(void)
213 {
214 	KASSERT(npf_config_locked_p());
215 	pserialize_perform(npf_config_psz);
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(void)
240 {
241 	return npf_config->n_rules;
242 }
243 
244 npf_ruleset_t *
245 npf_config_natset(void)
246 {
247 	return npf_config->n_nat_rules;
248 }
249 
250 npf_tableset_t *
251 npf_config_tableset(void)
252 {
253 	return npf_config->n_tables;
254 }
255 
256 npf_rprocset_t *
257 npf_config_rprocs(void)
258 {
259 	return npf_config->n_rprocs;
260 }
261 
262 bool
263 npf_default_pass(void)
264 {
265 	return npf_config->n_default_pass;
266 }
267