npf_conf.c revision 1.11 1 1.11 rmind /* $NetBSD: npf_conf.c,v 1.11 2017/01/03 00:58:05 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.1 rmind /*
33 1.1 rmind * NPF config loading mechanism.
34 1.1 rmind *
35 1.1 rmind * There are few main operations on the config:
36 1.1 rmind * 1) Read access which is primarily from the npf_packet_handler() et al.
37 1.1 rmind * 2) Write access on particular set, mainly rule or table updates.
38 1.1 rmind * 3) Deletion of the config, which is done during the reload operation.
39 1.1 rmind *
40 1.1 rmind * Synchronisation
41 1.1 rmind *
42 1.1 rmind * For (1) case, passive serialisation is used to allow concurrent
43 1.1 rmind * access to the configuration set (ruleset, etc). It guarantees
44 1.1 rmind * that the config will not be destroyed while accessing it.
45 1.1 rmind *
46 1.1 rmind * Writers, i.e. cases (2) and (3) use mutual exclusion and when
47 1.1 rmind * necessary writer-side barrier of the passive serialisation.
48 1.1 rmind */
49 1.1 rmind
50 1.10 christos #ifdef _KERNEL
51 1.1 rmind #include <sys/cdefs.h>
52 1.11 rmind __KERNEL_RCSID(0, "$NetBSD: npf_conf.c,v 1.11 2017/01/03 00:58:05 rmind Exp $");
53 1.1 rmind
54 1.1 rmind #include <sys/param.h>
55 1.1 rmind #include <sys/types.h>
56 1.1 rmind
57 1.1 rmind #include <sys/atomic.h>
58 1.1 rmind #include <sys/kmem.h>
59 1.1 rmind #include <sys/pserialize.h>
60 1.1 rmind #include <sys/mutex.h>
61 1.10 christos #endif
62 1.1 rmind
63 1.1 rmind #include "npf_impl.h"
64 1.7 rmind #include "npf_conn.h"
65 1.1 rmind
66 1.10 christos struct npf_config {
67 1.1 rmind npf_ruleset_t * n_rules;
68 1.1 rmind npf_tableset_t * n_tables;
69 1.1 rmind npf_ruleset_t * n_nat_rules;
70 1.1 rmind npf_rprocset_t * n_rprocs;
71 1.1 rmind bool n_default_pass;
72 1.10 christos };
73 1.1 rmind
74 1.1 rmind void
75 1.10 christos npf_config_init(npf_t *npf)
76 1.1 rmind {
77 1.1 rmind npf_ruleset_t *rlset, *nset;
78 1.1 rmind npf_rprocset_t *rpset;
79 1.1 rmind npf_tableset_t *tset;
80 1.1 rmind
81 1.10 christos mutex_init(&npf->config_lock, MUTEX_DEFAULT, IPL_SOFTNET);
82 1.1 rmind
83 1.1 rmind /* Load the empty configuration. */
84 1.4 rmind tset = npf_tableset_create(0);
85 1.1 rmind rpset = npf_rprocset_create();
86 1.1 rmind rlset = npf_ruleset_create(0);
87 1.1 rmind nset = npf_ruleset_create(0);
88 1.10 christos npf_config_load(npf, rlset, tset, nset, rpset, NULL, true);
89 1.10 christos KASSERT(npf->config != NULL);
90 1.1 rmind }
91 1.1 rmind
92 1.1 rmind static void
93 1.1 rmind npf_config_destroy(npf_config_t *nc)
94 1.1 rmind {
95 1.1 rmind npf_ruleset_destroy(nc->n_rules);
96 1.1 rmind npf_ruleset_destroy(nc->n_nat_rules);
97 1.1 rmind npf_rprocset_destroy(nc->n_rprocs);
98 1.1 rmind npf_tableset_destroy(nc->n_tables);
99 1.1 rmind kmem_free(nc, sizeof(npf_config_t));
100 1.1 rmind }
101 1.1 rmind
102 1.1 rmind void
103 1.10 christos npf_config_fini(npf_t *npf)
104 1.1 rmind {
105 1.9 rmind npf_conndb_t *cd = npf_conndb_create();
106 1.9 rmind
107 1.7 rmind /* Flush the connections. */
108 1.10 christos mutex_enter(&npf->config_lock);
109 1.10 christos npf_conn_tracking(npf, false);
110 1.10 christos pserialize_perform(npf->qsbr);
111 1.10 christos npf_conn_load(npf, cd, false);
112 1.10 christos npf_ifmap_flush(npf);
113 1.10 christos mutex_exit(&npf->config_lock);
114 1.10 christos
115 1.10 christos npf_config_destroy(npf->config);
116 1.10 christos mutex_destroy(&npf->config_lock);
117 1.1 rmind }
118 1.1 rmind
119 1.1 rmind /*
120 1.7 rmind * npf_config_load: the main routine performing configuration load.
121 1.1 rmind * Performs the necessary synchronisation and destroys the old config.
122 1.1 rmind */
123 1.1 rmind void
124 1.10 christos npf_config_load(npf_t *npf, npf_ruleset_t *rset, npf_tableset_t *tset,
125 1.8 rmind npf_ruleset_t *nset, npf_rprocset_t *rpset,
126 1.7 rmind npf_conndb_t *conns, bool flush)
127 1.1 rmind {
128 1.9 rmind const bool load = conns != NULL;
129 1.1 rmind npf_config_t *nc, *onc;
130 1.1 rmind
131 1.1 rmind nc = kmem_zalloc(sizeof(npf_config_t), KM_SLEEP);
132 1.1 rmind nc->n_rules = rset;
133 1.1 rmind nc->n_tables = tset;
134 1.1 rmind nc->n_nat_rules = nset;
135 1.1 rmind nc->n_rprocs = rpset;
136 1.1 rmind nc->n_default_pass = flush;
137 1.1 rmind
138 1.1 rmind /*
139 1.1 rmind * Acquire the lock and perform the first phase:
140 1.1 rmind * - Scan and use existing dynamic tables, reload only static.
141 1.1 rmind * - Scan and use matching NAT policies to preserve the connections.
142 1.1 rmind */
143 1.10 christos mutex_enter(&npf->config_lock);
144 1.10 christos if ((onc = npf->config) != NULL) {
145 1.10 christos npf_ruleset_reload(npf, rset, onc->n_rules, load);
146 1.10 christos npf_tableset_reload(npf, tset, onc->n_tables);
147 1.10 christos npf_ruleset_reload(npf, nset, onc->n_nat_rules, load);
148 1.1 rmind }
149 1.1 rmind
150 1.1 rmind /*
151 1.1 rmind * Set the new config and release the lock.
152 1.1 rmind */
153 1.1 rmind membar_sync();
154 1.10 christos npf->config = nc;
155 1.1 rmind if (onc == NULL) {
156 1.1 rmind /* Initial load, done. */
157 1.10 christos npf_ifmap_flush(npf);
158 1.10 christos npf_conn_load(npf, conns, !flush);
159 1.10 christos mutex_exit(&npf->config_lock);
160 1.11 rmind goto done;
161 1.1 rmind }
162 1.1 rmind
163 1.7 rmind /*
164 1.7 rmind * If we are going to flush the connections or load the new ones,
165 1.7 rmind * then disable the connection tracking for the grace period.
166 1.7 rmind */
167 1.7 rmind if (flush || conns) {
168 1.10 christos npf_conn_tracking(npf, false);
169 1.7 rmind }
170 1.7 rmind
171 1.1 rmind /* Synchronise: drain all references. */
172 1.10 christos pserialize_perform(npf->qsbr);
173 1.3 rmind if (flush) {
174 1.10 christos npf_ifmap_flush(npf);
175 1.3 rmind }
176 1.5 rmind
177 1.7 rmind /*
178 1.7 rmind * G/C the existing connections and, if passed, load the new ones.
179 1.7 rmind * If not flushing - enable the connection tracking.
180 1.7 rmind */
181 1.10 christos npf_conn_load(npf, conns, !flush);
182 1.10 christos mutex_exit(&npf->config_lock);
183 1.1 rmind
184 1.1 rmind /* Finally, it is safe to destroy the old config. */
185 1.1 rmind npf_config_destroy(onc);
186 1.11 rmind done:
187 1.11 rmind /* Sync all interface address tables (can be done asynchronously). */
188 1.11 rmind npf_ifaddr_syncall(npf);
189 1.1 rmind }
190 1.1 rmind
191 1.1 rmind /*
192 1.1 rmind * Writer-side exclusive locking.
193 1.1 rmind */
194 1.1 rmind
195 1.1 rmind void
196 1.10 christos npf_config_enter(npf_t *npf)
197 1.1 rmind {
198 1.10 christos mutex_enter(&npf->config_lock);
199 1.1 rmind }
200 1.1 rmind
201 1.1 rmind void
202 1.10 christos npf_config_exit(npf_t *npf)
203 1.1 rmind {
204 1.10 christos mutex_exit(&npf->config_lock);
205 1.1 rmind }
206 1.1 rmind
207 1.1 rmind bool
208 1.10 christos npf_config_locked_p(npf_t *npf)
209 1.1 rmind {
210 1.10 christos return mutex_owned(&npf->config_lock);
211 1.1 rmind }
212 1.1 rmind
213 1.2 rmind void
214 1.10 christos npf_config_sync(npf_t *npf)
215 1.2 rmind {
216 1.10 christos KASSERT(npf_config_locked_p(npf));
217 1.10 christos pserialize_perform(npf->qsbr);
218 1.2 rmind }
219 1.2 rmind
220 1.1 rmind /*
221 1.1 rmind * Reader-side synchronisation routines.
222 1.1 rmind */
223 1.1 rmind
224 1.1 rmind int
225 1.1 rmind npf_config_read_enter(void)
226 1.1 rmind {
227 1.1 rmind return pserialize_read_enter();
228 1.1 rmind }
229 1.1 rmind
230 1.1 rmind void
231 1.1 rmind npf_config_read_exit(int s)
232 1.1 rmind {
233 1.1 rmind pserialize_read_exit(s);
234 1.1 rmind }
235 1.1 rmind
236 1.1 rmind /*
237 1.1 rmind * Accessors.
238 1.1 rmind */
239 1.1 rmind
240 1.1 rmind npf_ruleset_t *
241 1.10 christos npf_config_ruleset(npf_t *npf)
242 1.1 rmind {
243 1.10 christos return npf->config->n_rules;
244 1.1 rmind }
245 1.1 rmind
246 1.1 rmind npf_ruleset_t *
247 1.10 christos npf_config_natset(npf_t *npf)
248 1.1 rmind {
249 1.10 christos return npf->config->n_nat_rules;
250 1.1 rmind }
251 1.1 rmind
252 1.1 rmind npf_tableset_t *
253 1.10 christos npf_config_tableset(npf_t *npf)
254 1.1 rmind {
255 1.10 christos return npf->config->n_tables;
256 1.1 rmind }
257 1.1 rmind
258 1.8 rmind npf_rprocset_t *
259 1.10 christos npf_config_rprocs(npf_t *npf)
260 1.1 rmind {
261 1.10 christos return npf->config->n_rprocs;
262 1.1 rmind }
263 1.1 rmind
264 1.1 rmind bool
265 1.10 christos npf_default_pass(npf_t *npf)
266 1.1 rmind {
267 1.10 christos return npf->config->n_default_pass;
268 1.1 rmind }
269