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