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