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