npf_conf.c revision 1.15 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.15 rmind __KERNEL_RCSID(0, "$NetBSD: npf_conf.c,v 1.15 2019/08/25 13:21:03 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.15 rmind npf_ruleset_destroy(nc->ruleset);
98 1.15 rmind npf_ruleset_destroy(nc->nat_ruleset);
99 1.15 rmind npf_rprocset_destroy(nc->rule_procs);
100 1.15 rmind npf_tableset_destroy(nc->tableset);
101 1.1 rmind kmem_free(nc, sizeof(npf_config_t));
102 1.1 rmind }
103 1.1 rmind
104 1.1 rmind void
105 1.10 christos npf_config_fini(npf_t *npf)
106 1.1 rmind {
107 1.9 rmind npf_conndb_t *cd = npf_conndb_create();
108 1.9 rmind
109 1.7 rmind /* Flush the connections. */
110 1.10 christos mutex_enter(&npf->config_lock);
111 1.10 christos npf_conn_tracking(npf, false);
112 1.15 rmind npf_ebr_full_sync(npf->ebr);
113 1.10 christos npf_conn_load(npf, cd, false);
114 1.10 christos npf_ifmap_flush(npf);
115 1.10 christos mutex_exit(&npf->config_lock);
116 1.10 christos
117 1.10 christos npf_config_destroy(npf->config);
118 1.10 christos mutex_destroy(&npf->config_lock);
119 1.1 rmind }
120 1.1 rmind
121 1.1 rmind /*
122 1.7 rmind * npf_config_load: the main routine performing configuration load.
123 1.1 rmind * Performs the necessary synchronisation and destroys the old config.
124 1.1 rmind */
125 1.1 rmind void
126 1.15 rmind npf_config_load(npf_t *npf, npf_config_t *nc, npf_conndb_t *conns, bool flush)
127 1.1 rmind {
128 1.9 rmind const bool load = conns != NULL;
129 1.15 rmind npf_config_t *onc;
130 1.1 rmind
131 1.15 rmind nc->default_pass = flush;
132 1.1 rmind
133 1.1 rmind /*
134 1.1 rmind * Acquire the lock and perform the first phase:
135 1.1 rmind * - Scan and use existing dynamic tables, reload only static.
136 1.1 rmind * - Scan and use matching NAT policies to preserve the connections.
137 1.1 rmind */
138 1.10 christos mutex_enter(&npf->config_lock);
139 1.10 christos if ((onc = npf->config) != NULL) {
140 1.15 rmind npf_ruleset_reload(npf, nc->ruleset, onc->ruleset, load);
141 1.15 rmind npf_tableset_reload(npf, nc->tableset, onc->tableset);
142 1.15 rmind npf_ruleset_reload(npf, nc->nat_ruleset, onc->nat_ruleset, load);
143 1.1 rmind }
144 1.1 rmind
145 1.1 rmind /*
146 1.1 rmind * Set the new config and release the lock.
147 1.1 rmind */
148 1.1 rmind membar_sync();
149 1.10 christos npf->config = nc;
150 1.1 rmind if (onc == NULL) {
151 1.1 rmind /* Initial load, done. */
152 1.10 christos npf_ifmap_flush(npf);
153 1.10 christos npf_conn_load(npf, conns, !flush);
154 1.10 christos mutex_exit(&npf->config_lock);
155 1.11 rmind goto done;
156 1.1 rmind }
157 1.1 rmind
158 1.7 rmind /*
159 1.7 rmind * If we are going to flush the connections or load the new ones,
160 1.7 rmind * then disable the connection tracking for the grace period.
161 1.7 rmind */
162 1.7 rmind if (flush || conns) {
163 1.10 christos npf_conn_tracking(npf, false);
164 1.7 rmind }
165 1.7 rmind
166 1.1 rmind /* Synchronise: drain all references. */
167 1.15 rmind npf_ebr_full_sync(npf->ebr);
168 1.3 rmind if (flush) {
169 1.14 rmind npf_portmap_flush(npf->portmap);
170 1.10 christos npf_ifmap_flush(npf);
171 1.3 rmind }
172 1.5 rmind
173 1.7 rmind /*
174 1.7 rmind * G/C the existing connections and, if passed, load the new ones.
175 1.7 rmind * If not flushing - enable the connection tracking.
176 1.7 rmind */
177 1.10 christos npf_conn_load(npf, conns, !flush);
178 1.10 christos mutex_exit(&npf->config_lock);
179 1.1 rmind
180 1.1 rmind /* Finally, it is safe to destroy the old config. */
181 1.1 rmind npf_config_destroy(onc);
182 1.11 rmind done:
183 1.11 rmind /* Sync all interface address tables (can be done asynchronously). */
184 1.11 rmind npf_ifaddr_syncall(npf);
185 1.1 rmind }
186 1.1 rmind
187 1.1 rmind /*
188 1.1 rmind * Writer-side exclusive locking.
189 1.1 rmind */
190 1.1 rmind
191 1.15 rmind npf_config_t *
192 1.10 christos npf_config_enter(npf_t *npf)
193 1.1 rmind {
194 1.10 christos mutex_enter(&npf->config_lock);
195 1.15 rmind return npf->config;
196 1.1 rmind }
197 1.1 rmind
198 1.1 rmind void
199 1.10 christos npf_config_exit(npf_t *npf)
200 1.1 rmind {
201 1.10 christos mutex_exit(&npf->config_lock);
202 1.1 rmind }
203 1.1 rmind
204 1.1 rmind bool
205 1.10 christos npf_config_locked_p(npf_t *npf)
206 1.1 rmind {
207 1.10 christos return mutex_owned(&npf->config_lock);
208 1.1 rmind }
209 1.1 rmind
210 1.2 rmind void
211 1.10 christos npf_config_sync(npf_t *npf)
212 1.2 rmind {
213 1.10 christos KASSERT(npf_config_locked_p(npf));
214 1.15 rmind npf_ebr_full_sync(npf->ebr);
215 1.2 rmind }
216 1.2 rmind
217 1.1 rmind /*
218 1.1 rmind * Reader-side synchronisation routines.
219 1.1 rmind */
220 1.1 rmind
221 1.1 rmind int
222 1.15 rmind npf_config_read_enter(npf_t *npf)
223 1.1 rmind {
224 1.15 rmind return npf_ebr_enter(npf->ebr);
225 1.1 rmind }
226 1.1 rmind
227 1.1 rmind void
228 1.15 rmind npf_config_read_exit(npf_t *npf, int s)
229 1.1 rmind {
230 1.15 rmind npf_ebr_exit(npf->ebr, s);
231 1.1 rmind }
232 1.1 rmind
233 1.1 rmind /*
234 1.1 rmind * Accessors.
235 1.1 rmind */
236 1.1 rmind
237 1.1 rmind npf_ruleset_t *
238 1.10 christos npf_config_ruleset(npf_t *npf)
239 1.1 rmind {
240 1.15 rmind KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr));
241 1.15 rmind return npf->config->ruleset;
242 1.1 rmind }
243 1.1 rmind
244 1.1 rmind npf_ruleset_t *
245 1.10 christos npf_config_natset(npf_t *npf)
246 1.1 rmind {
247 1.15 rmind KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr));
248 1.15 rmind return npf->config->nat_ruleset;
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.15 rmind KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr));
255 1.15 rmind return npf->config->tableset;
256 1.1 rmind }
257 1.1 rmind
258 1.1 rmind bool
259 1.10 christos npf_default_pass(npf_t *npf)
260 1.1 rmind {
261 1.15 rmind KASSERT(npf_config_locked_p(npf) || npf_ebr_incrit_p(npf->ebr));
262 1.15 rmind return npf->config->default_pass;
263 1.1 rmind }
264