npf_conf.c revision 1.17 1 /*-
2 * Copyright (c) 2013-2020 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 configuration loading mechanism.
32 *
33 * The main operations on the configuration are the following:
34 * 1) Read access, primarily from the npf_packet_handler() function.
35 * 2) Write access on a particular set, mainly rule or table updates.
36 * 3) Deletion of the configuration after the reload operation.
37 *
38 * Synchronization
39 *
40 * For the (1) case, EBR is used to allow concurrent access to
41 * the configuration set (ruleset, etc). It guarantees that the
42 * configuration will not be destroyed while accessing it.
43 *
44 * For the cases (2) and (3), mutual exclusion (npf_t::config_lock)
45 * is used with, when necessary, the writer-side barrier of EBR.
46 */
47
48 #ifdef _KERNEL
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: npf_conf.c,v 1.17 2020/05/30 14:16:56 rmind 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/mutex.h>
58 #endif
59
60 #include "npf_impl.h"
61 #include "npf_conn.h"
62
63 void
64 npf_config_init(npf_t *npf)
65 {
66 npf_config_t *nc;
67
68 mutex_init(&npf->config_lock, MUTEX_DEFAULT, IPL_SOFTNET);
69 nc = npf_config_create();
70
71 /*
72 * Load an empty configuration.
73 */
74 nc->ruleset = npf_ruleset_create(0);
75 nc->nat_ruleset = npf_ruleset_create(0);
76 nc->rule_procs = npf_rprocset_create();
77 nc->tableset = npf_tableset_create(0);
78 nc->default_pass = true;
79
80 npf_config_load(npf, nc, NULL, true);
81 KASSERT(npf->config != NULL);
82 }
83
84 npf_config_t *
85 npf_config_create(void)
86 {
87 return kmem_zalloc(sizeof(npf_config_t), KM_SLEEP);
88 }
89
90 void
91 npf_config_destroy(npf_config_t *nc)
92 {
93 /*
94 * Note: the rulesets must be destroyed first, in order to drop
95 * any references to the tableset.
96 */
97 if (nc->ruleset) {
98 npf_ruleset_destroy(nc->ruleset);
99 }
100 if (nc->nat_ruleset) {
101 npf_ruleset_destroy(nc->nat_ruleset);
102 }
103 if (nc->rule_procs) {
104 npf_rprocset_destroy(nc->rule_procs);
105 }
106 if (nc->tableset) {
107 npf_tableset_destroy(nc->tableset);
108 }
109 kmem_free(nc, sizeof(npf_config_t));
110 }
111
112 void
113 npf_config_fini(npf_t *npf)
114 {
115 npf_conndb_t *cd = npf_conndb_create();
116
117 /* Flush the connections. */
118 mutex_enter(&npf->config_lock);
119 npf_conn_tracking(npf, false);
120 npf_ebr_full_sync(npf->ebr);
121 npf_conn_load(npf, cd, false);
122 npf_ifmap_flush(npf);
123 mutex_exit(&npf->config_lock);
124
125 npf_config_destroy(npf->config);
126 mutex_destroy(&npf->config_lock);
127 }
128
129 /*
130 * npf_config_load: the main routine performing configuration load.
131 * Performs the necessary synchronization and destroys the old config.
132 */
133 void
134 npf_config_load(npf_t *npf, npf_config_t *nc, npf_conndb_t *conns, bool flush)
135 {
136 const bool load = conns != NULL;
137 npf_config_t *onc;
138
139 nc->default_pass = flush;
140
141 /*
142 * Acquire the lock and perform the first phase:
143 * - Scan and use existing dynamic tables, reload only static.
144 * - Scan and use matching NAT policies to preserve the connections.
145 */
146 mutex_enter(&npf->config_lock);
147 if ((onc = atomic_load_relaxed(&npf->config)) != NULL) {
148 npf_ruleset_reload(npf, nc->ruleset, onc->ruleset, load);
149 npf_tableset_reload(npf, nc->tableset, onc->tableset);
150 npf_ruleset_reload(npf, nc->nat_ruleset, onc->nat_ruleset, load);
151 }
152
153 /*
154 * Set the new config and release the lock.
155 */
156 membar_sync();
157 atomic_store_relaxed(&npf->config, nc);
158 if (onc == NULL) {
159 /* Initial load, done. */
160 npf_ifmap_flush(npf);
161 npf_conn_load(npf, conns, !flush);
162 mutex_exit(&npf->config_lock);
163 goto done;
164 }
165
166 /*
167 * If we are going to flush the connections or load the new ones,
168 * then disable the connection tracking for the grace period.
169 */
170 if (flush || conns) {
171 npf_conn_tracking(npf, false);
172 }
173
174 /* Synchronise: drain all references. */
175 npf_ebr_full_sync(npf->ebr);
176 if (flush) {
177 npf_portmap_flush(npf->portmap);
178 npf_ifmap_flush(npf);
179 }
180
181 /*
182 * G/C the existing connections and, if passed, load the new ones.
183 * If not flushing - enable the connection tracking.
184 */
185 npf_conn_load(npf, conns, !flush);
186 mutex_exit(&npf->config_lock);
187
188 /* Finally, it is safe to destroy the old config. */
189 npf_config_destroy(onc);
190 done:
191 /* Sync all interface address tables (can be done asynchronously). */
192 npf_ifaddr_syncall(npf);
193 }
194
195 /*
196 * Writer-side exclusive locking.
197 */
198
199 npf_config_t *
200 npf_config_enter(npf_t *npf)
201 {
202 mutex_enter(&npf->config_lock);
203 return npf->config;
204 }
205
206 void
207 npf_config_exit(npf_t *npf)
208 {
209 mutex_exit(&npf->config_lock);
210 }
211
212 bool
213 npf_config_locked_p(npf_t *npf)
214 {
215 return mutex_owned(&npf->config_lock);
216 }
217
218 void
219 npf_config_sync(npf_t *npf)
220 {
221 KASSERT(npf_config_locked_p(npf));
222 npf_ebr_full_sync(npf->ebr);
223 }
224
225 /*
226 * Reader-side synchronization routines.
227 */
228
229 int
230 npf_config_read_enter(npf_t *npf)
231 {
232 /* Note: issues an acquire fence. */
233 return npf_ebr_enter(npf->ebr);
234 }
235
236 void
237 npf_config_read_exit(npf_t *npf, int s)
238 {
239 /* Note: issues a release fence. */
240 npf_ebr_exit(npf->ebr, s);
241 }
242
243 /*
244 * Accessors.
245 */
246
247 npf_ruleset_t *
248 npf_config_ruleset(npf_t *npf)
249 {
250 npf_config_t *config = atomic_load_relaxed(&npf->config);
251 KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr));
252 return config->ruleset;
253 }
254
255 npf_ruleset_t *
256 npf_config_natset(npf_t *npf)
257 {
258 npf_config_t *config = atomic_load_relaxed(&npf->config);
259 KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr));
260 return config->nat_ruleset;
261 }
262
263 npf_tableset_t *
264 npf_config_tableset(npf_t *npf)
265 {
266 npf_config_t *config = atomic_load_relaxed(&npf->config);
267 KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr));
268 return config->tableset;
269 }
270
271 bool
272 npf_default_pass(npf_t *npf)
273 {
274 npf_config_t *config = atomic_load_relaxed(&npf->config);
275 KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr));
276 return config->default_pass;
277 }
278