npf_ctl.c revision 1.58 1 1.1 rmind /*-
2 1.54 rmind * Copyright (c) 2009-2019 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 device control.
32 1.1 rmind *
33 1.1 rmind * Implementation of (re)loading, construction of tables and rules.
34 1.51 rmind * NPF nvlist(3) consumer.
35 1.1 rmind */
36 1.1 rmind
37 1.45 christos #ifdef _KERNEL
38 1.1 rmind #include <sys/cdefs.h>
39 1.58 rmind __KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.58 2019/08/25 17:38:25 rmind Exp $");
40 1.1 rmind
41 1.1 rmind #include <sys/param.h>
42 1.1 rmind #include <sys/conf.h>
43 1.21 rmind #include <sys/kmem.h>
44 1.21 rmind #include <net/bpf.h>
45 1.45 christos #endif
46 1.1 rmind
47 1.1 rmind #include "npf_impl.h"
48 1.34 rmind #include "npf_conn.h"
49 1.1 rmind
50 1.51 rmind #define NPF_IOCTL_DATA_LIMIT (4 * 1024 * 1024)
51 1.51 rmind
52 1.12 rmind #define NPF_ERR_DEBUG(e) \
53 1.51 rmind nvlist_add_string((e), "source-file", __FILE__); \
54 1.51 rmind nvlist_add_number((e), "source-line", __LINE__);
55 1.12 rmind
56 1.51 rmind static int
57 1.52 christos npf_nvlist_copyin(npf_t *npf, void *data, nvlist_t **nvl)
58 1.51 rmind {
59 1.51 rmind int error = 0;
60 1.51 rmind
61 1.53 rmind if (npf->mbufops == NULL) {
62 1.53 rmind error = nvlist_copyin(data, nvl, NPF_IOCTL_DATA_LIMIT);
63 1.53 rmind } else {
64 1.52 christos *nvl = (nvlist_t *)data;
65 1.53 rmind }
66 1.51 rmind return error;
67 1.51 rmind }
68 1.51 rmind
69 1.51 rmind static int
70 1.52 christos npf_nvlist_copyout(npf_t *npf, void *data, nvlist_t *nvl)
71 1.51 rmind {
72 1.51 rmind int error = 0;
73 1.51 rmind
74 1.53 rmind if (npf->mbufops == NULL) {
75 1.52 christos error = nvlist_copyout(data, nvl);
76 1.53 rmind }
77 1.51 rmind nvlist_destroy(nvl);
78 1.51 rmind return error;
79 1.51 rmind }
80 1.51 rmind
81 1.6 rmind static int __noinline
82 1.54 rmind npf_mk_params(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict, bool set)
83 1.54 rmind {
84 1.54 rmind const nvlist_t *params;
85 1.54 rmind int type, error, val;
86 1.54 rmind const char *name;
87 1.54 rmind void *cookie;
88 1.54 rmind
89 1.54 rmind params = dnvlist_get_nvlist(npf_dict, "params", NULL);
90 1.54 rmind if (params == NULL) {
91 1.54 rmind return 0;
92 1.54 rmind }
93 1.54 rmind cookie = NULL;
94 1.54 rmind while ((name = nvlist_next(params, &type, &cookie)) != NULL) {
95 1.54 rmind if (type != NV_TYPE_NUMBER) {
96 1.54 rmind NPF_ERR_DEBUG(errdict);
97 1.54 rmind return EINVAL;
98 1.54 rmind }
99 1.54 rmind val = (int)nvlist_get_number(params, name);
100 1.54 rmind if (set) {
101 1.54 rmind /* Actually set the parameter. */
102 1.55 rmind error = npfk_param_set(npf, name, val);
103 1.54 rmind KASSERT(error == 0);
104 1.54 rmind continue;
105 1.54 rmind }
106 1.54 rmind
107 1.54 rmind /* Validate the parameter and its value. */
108 1.54 rmind error = npf_param_check(npf, name, val);
109 1.54 rmind if (__predict_true(error == 0)) {
110 1.54 rmind continue;
111 1.54 rmind }
112 1.54 rmind if (error == ENOENT) {
113 1.54 rmind nvlist_add_stringf(errdict, "error-msg",
114 1.54 rmind "invalid parameter `%s`", name);
115 1.54 rmind }
116 1.54 rmind if (error == EINVAL) {
117 1.54 rmind nvlist_add_stringf(errdict, "error-msg",
118 1.54 rmind "invalid parameter `%s` value %d", name, val);
119 1.54 rmind }
120 1.54 rmind return error;
121 1.54 rmind }
122 1.54 rmind return 0;
123 1.54 rmind }
124 1.54 rmind
125 1.54 rmind static int __noinline
126 1.51 rmind npf_mk_table_entries(npf_table_t *t, const nvlist_t *table, nvlist_t *errdict)
127 1.33 rmind {
128 1.51 rmind const nvlist_t * const *entries;
129 1.51 rmind size_t nitems;
130 1.33 rmind int error = 0;
131 1.33 rmind
132 1.51 rmind if (!nvlist_exists_nvlist_array(table, "entries")) {
133 1.51 rmind return 0;
134 1.39 rmind }
135 1.51 rmind entries = nvlist_get_nvlist_array(table, "entries", &nitems);
136 1.51 rmind for (unsigned i = 0; i < nitems; i++) {
137 1.51 rmind const nvlist_t *entry = entries[i];
138 1.33 rmind const npf_addr_t *addr;
139 1.33 rmind npf_netmask_t mask;
140 1.51 rmind size_t alen;
141 1.33 rmind
142 1.33 rmind /* Get address and mask. Add a table entry. */
143 1.51 rmind addr = dnvlist_get_binary(entry, "addr", &alen, NULL, 0);
144 1.51 rmind mask = dnvlist_get_number(entry, "mask", NPF_NO_NETMASK);
145 1.51 rmind if (addr == NULL || alen == 0) {
146 1.51 rmind NPF_ERR_DEBUG(errdict);
147 1.51 rmind error = EINVAL;
148 1.51 rmind break;
149 1.51 rmind }
150 1.33 rmind error = npf_table_insert(t, alen, addr, mask);
151 1.51 rmind if (error) {
152 1.51 rmind NPF_ERR_DEBUG(errdict);
153 1.33 rmind break;
154 1.51 rmind }
155 1.33 rmind }
156 1.33 rmind return error;
157 1.33 rmind }
158 1.33 rmind
159 1.56 rmind /*
160 1.56 rmind * npf_mk_table: create a table from provided nvlist.
161 1.56 rmind */
162 1.56 rmind static int __noinline
163 1.56 rmind npf_mk_table(npf_t *npf, const nvlist_t *tbl_dict, nvlist_t *errdict,
164 1.56 rmind npf_tableset_t *tblset, npf_table_t **tblp, bool replacing)
165 1.56 rmind {
166 1.56 rmind npf_table_t *t;
167 1.56 rmind const char *name;
168 1.56 rmind const void *blob;
169 1.56 rmind uint64_t tid;
170 1.56 rmind size_t size;
171 1.56 rmind int type;
172 1.56 rmind int error = 0;
173 1.56 rmind
174 1.56 rmind KASSERT(tblp != NULL);
175 1.56 rmind
176 1.56 rmind /* Table name, ID and type. Validate them. */
177 1.56 rmind name = dnvlist_get_string(tbl_dict, "name", NULL);
178 1.56 rmind if (!name) {
179 1.56 rmind NPF_ERR_DEBUG(errdict);
180 1.56 rmind error = EINVAL;
181 1.56 rmind goto out;
182 1.56 rmind }
183 1.56 rmind tid = dnvlist_get_number(tbl_dict, "id", UINT64_MAX);
184 1.56 rmind type = dnvlist_get_number(tbl_dict, "type", UINT64_MAX);
185 1.56 rmind error = npf_table_check(tblset, name, tid, type, replacing);
186 1.56 rmind if (error) {
187 1.56 rmind NPF_ERR_DEBUG(errdict);
188 1.56 rmind goto out;
189 1.56 rmind }
190 1.56 rmind
191 1.56 rmind /* Get the entries or binary data. */
192 1.56 rmind blob = dnvlist_get_binary(tbl_dict, "data", &size, NULL, 0);
193 1.56 rmind if (type == NPF_TABLE_CONST && (blob == NULL || size == 0)) {
194 1.56 rmind NPF_ERR_DEBUG(errdict);
195 1.56 rmind error = EINVAL;
196 1.56 rmind goto out;
197 1.56 rmind }
198 1.56 rmind
199 1.56 rmind t = npf_table_create(name, (u_int)tid, type, blob, size);
200 1.56 rmind if (t == NULL) {
201 1.56 rmind NPF_ERR_DEBUG(errdict);
202 1.56 rmind error = ENOMEM;
203 1.56 rmind goto out;
204 1.56 rmind }
205 1.56 rmind
206 1.56 rmind if ((error = npf_mk_table_entries(t, tbl_dict, errdict)) != 0) {
207 1.56 rmind npf_table_destroy(t);
208 1.56 rmind goto out;
209 1.56 rmind }
210 1.56 rmind
211 1.56 rmind *tblp = t;
212 1.56 rmind out:
213 1.56 rmind return error;
214 1.56 rmind }
215 1.56 rmind
216 1.33 rmind static int __noinline
217 1.51 rmind npf_mk_tables(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
218 1.57 rmind npf_config_t *nc)
219 1.1 rmind {
220 1.51 rmind const nvlist_t * const *tables;
221 1.51 rmind npf_tableset_t *tblset;
222 1.51 rmind size_t nitems;
223 1.1 rmind int error = 0;
224 1.1 rmind
225 1.51 rmind if (nvlist_exists_nvlist_array(npf_dict, "tables")) {
226 1.51 rmind tables = nvlist_get_nvlist_array(npf_dict, "tables", &nitems);
227 1.51 rmind if (nitems > NPF_MAX_TABLES) {
228 1.51 rmind NPF_ERR_DEBUG(errdict);
229 1.51 rmind return E2BIG;
230 1.51 rmind }
231 1.51 rmind } else {
232 1.51 rmind tables = NULL;
233 1.51 rmind nitems = 0;
234 1.12 rmind }
235 1.51 rmind tblset = npf_tableset_create(nitems);
236 1.51 rmind for (unsigned i = 0; i < nitems; i++) {
237 1.51 rmind const nvlist_t *table = tables[i];
238 1.1 rmind npf_table_t *t;
239 1.1 rmind
240 1.56 rmind error = npf_mk_table(npf, table, errdict, tblset, &t, 0);
241 1.32 rmind if (error) {
242 1.33 rmind break;
243 1.33 rmind }
244 1.33 rmind
245 1.1 rmind error = npf_tableset_insert(tblset, t);
246 1.1 rmind KASSERT(error == 0);
247 1.1 rmind }
248 1.57 rmind nc->tableset = tblset;
249 1.1 rmind return error;
250 1.1 rmind }
251 1.1 rmind
252 1.5 rmind static npf_rproc_t *
253 1.51 rmind npf_mk_singlerproc(npf_t *npf, const nvlist_t *rproc, nvlist_t *errdict)
254 1.5 rmind {
255 1.51 rmind const nvlist_t * const *extcalls;
256 1.51 rmind size_t nitems;
257 1.5 rmind npf_rproc_t *rp;
258 1.18 rmind
259 1.51 rmind if (!nvlist_exists_nvlist_array(rproc, "extcalls")) {
260 1.51 rmind NPF_ERR_DEBUG(errdict);
261 1.18 rmind return NULL;
262 1.18 rmind }
263 1.51 rmind rp = npf_rproc_create(rproc);
264 1.21 rmind if (rp == NULL) {
265 1.51 rmind NPF_ERR_DEBUG(errdict);
266 1.18 rmind return NULL;
267 1.18 rmind }
268 1.51 rmind extcalls = nvlist_get_nvlist_array(rproc, "extcalls", &nitems);
269 1.51 rmind for (unsigned i = 0; i < nitems; i++) {
270 1.51 rmind const nvlist_t *extcall = extcalls[i];
271 1.21 rmind const char *name;
272 1.21 rmind
273 1.51 rmind name = dnvlist_get_string(extcall, "name", NULL);
274 1.51 rmind if (!name || npf_ext_construct(npf, name, rp, extcall)) {
275 1.51 rmind NPF_ERR_DEBUG(errdict);
276 1.18 rmind npf_rproc_release(rp);
277 1.18 rmind rp = NULL;
278 1.18 rmind break;
279 1.18 rmind }
280 1.18 rmind }
281 1.21 rmind return rp;
282 1.21 rmind }
283 1.21 rmind
284 1.21 rmind static int __noinline
285 1.51 rmind npf_mk_rprocs(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
286 1.57 rmind npf_config_t *nc)
287 1.21 rmind {
288 1.51 rmind const nvlist_t * const *rprocs;
289 1.51 rmind npf_rprocset_t *rpset;
290 1.51 rmind size_t nitems;
291 1.21 rmind int error = 0;
292 1.21 rmind
293 1.51 rmind if (nvlist_exists_nvlist_array(npf_dict, "rprocs")) {
294 1.51 rmind rprocs = nvlist_get_nvlist_array(npf_dict, "rprocs", &nitems);
295 1.51 rmind if (nitems > NPF_MAX_RPROCS) {
296 1.51 rmind NPF_ERR_DEBUG(errdict);
297 1.51 rmind return E2BIG;
298 1.51 rmind }
299 1.51 rmind } else {
300 1.51 rmind rprocs = NULL;
301 1.51 rmind nitems = 0;
302 1.51 rmind }
303 1.51 rmind rpset = npf_rprocset_create();
304 1.51 rmind for (unsigned i = 0; i < nitems; i++) {
305 1.51 rmind const nvlist_t *rproc = rprocs[i];
306 1.21 rmind npf_rproc_t *rp;
307 1.18 rmind
308 1.51 rmind if ((rp = npf_mk_singlerproc(npf, rproc, errdict)) == NULL) {
309 1.21 rmind error = EINVAL;
310 1.21 rmind break;
311 1.21 rmind }
312 1.21 rmind npf_rprocset_insert(rpset, rp);
313 1.5 rmind }
314 1.57 rmind nc->rule_procs = rpset;
315 1.21 rmind return error;
316 1.5 rmind }
317 1.5 rmind
318 1.51 rmind static int __noinline
319 1.51 rmind npf_mk_algs(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict)
320 1.24 christos {
321 1.51 rmind const nvlist_t * const *algs;
322 1.51 rmind size_t nitems;
323 1.24 christos
324 1.51 rmind if (nvlist_exists_nvlist_array(npf_dict, "algs")) {
325 1.51 rmind algs = nvlist_get_nvlist_array(npf_dict, "algs", &nitems);
326 1.51 rmind } else {
327 1.51 rmind algs = NULL;
328 1.51 rmind nitems = 0;
329 1.51 rmind }
330 1.51 rmind for (unsigned i = 0; i < nitems; i++) {
331 1.51 rmind const nvlist_t *alg = algs[i];
332 1.51 rmind const char *name;
333 1.24 christos
334 1.51 rmind name = dnvlist_get_string(alg, "name", NULL);
335 1.51 rmind if (!name) {
336 1.51 rmind NPF_ERR_DEBUG(errdict);
337 1.51 rmind return EINVAL;
338 1.51 rmind }
339 1.51 rmind if (!npf_alg_construct(npf, name)) {
340 1.24 christos NPF_ERR_DEBUG(errdict);
341 1.51 rmind return EINVAL;
342 1.24 christos }
343 1.24 christos }
344 1.12 rmind return 0;
345 1.12 rmind }
346 1.12 rmind
347 1.12 rmind static int __noinline
348 1.51 rmind npf_mk_singlerule(npf_t *npf, const nvlist_t *rule, npf_rprocset_t *rpset,
349 1.51 rmind npf_rule_t **rlret, nvlist_t *errdict)
350 1.1 rmind {
351 1.21 rmind npf_rule_t *rl;
352 1.21 rmind const char *rname;
353 1.51 rmind const void *code;
354 1.51 rmind size_t clen;
355 1.51 rmind int error = 0;
356 1.1 rmind
357 1.51 rmind if ((rl = npf_rule_alloc(npf, rule)) == NULL) {
358 1.21 rmind NPF_ERR_DEBUG(errdict);
359 1.21 rmind return EINVAL;
360 1.21 rmind }
361 1.1 rmind
362 1.51 rmind /* Assign the rule procedure, if any. */
363 1.51 rmind if ((rname = dnvlist_get_string(rule, "rproc", NULL)) != NULL) {
364 1.21 rmind npf_rproc_t *rp;
365 1.21 rmind
366 1.21 rmind if (rpset == NULL) {
367 1.51 rmind NPF_ERR_DEBUG(errdict);
368 1.21 rmind error = EINVAL;
369 1.21 rmind goto err;
370 1.21 rmind }
371 1.21 rmind if ((rp = npf_rprocset_lookup(rpset, rname)) == NULL) {
372 1.18 rmind NPF_ERR_DEBUG(errdict);
373 1.18 rmind error = EINVAL;
374 1.18 rmind goto err;
375 1.18 rmind }
376 1.21 rmind npf_rule_setrproc(rl, rp);
377 1.18 rmind }
378 1.18 rmind
379 1.51 rmind /* Filter byte-code (binary data). */
380 1.51 rmind code = dnvlist_get_binary(rule, "code", &clen, NULL, 0);
381 1.51 rmind if (code) {
382 1.51 rmind void *bc;
383 1.21 rmind int type;
384 1.21 rmind
385 1.51 rmind type = dnvlist_get_number(rule, "code-type", UINT64_MAX);
386 1.51 rmind if (type != NPF_CODE_BPF) {
387 1.51 rmind NPF_ERR_DEBUG(errdict);
388 1.51 rmind error = ENOTSUP;
389 1.51 rmind goto err;
390 1.51 rmind }
391 1.51 rmind if (clen == 0) {
392 1.51 rmind NPF_ERR_DEBUG(errdict);
393 1.51 rmind error = EINVAL;
394 1.51 rmind goto err;
395 1.51 rmind }
396 1.51 rmind if (!npf_bpf_validate(code, clen)) {
397 1.51 rmind NPF_ERR_DEBUG(errdict);
398 1.51 rmind error = EINVAL;
399 1.12 rmind goto err;
400 1.4 rmind }
401 1.51 rmind bc = kmem_alloc(clen, KM_SLEEP);
402 1.51 rmind memcpy(bc, code, clen); // XXX: use nvlist_take
403 1.51 rmind npf_rule_setcode(rl, type, bc, clen);
404 1.1 rmind }
405 1.1 rmind
406 1.21 rmind *rlret = rl;
407 1.6 rmind return 0;
408 1.12 rmind err:
409 1.51 rmind nvlist_add_number(errdict, "id", dnvlist_get_number(rule, "prio", 0));
410 1.21 rmind npf_rule_free(rl);
411 1.12 rmind return error;
412 1.6 rmind }
413 1.6 rmind
414 1.6 rmind static int __noinline
415 1.51 rmind npf_mk_rules(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
416 1.57 rmind npf_config_t *nc)
417 1.6 rmind {
418 1.51 rmind const nvlist_t * const *rules;
419 1.51 rmind npf_ruleset_t *rlset;
420 1.51 rmind size_t nitems;
421 1.51 rmind int error = 0;
422 1.6 rmind
423 1.51 rmind if (nvlist_exists_nvlist_array(npf_dict, "rules")) {
424 1.51 rmind rules = nvlist_get_nvlist_array(npf_dict, "rules", &nitems);
425 1.51 rmind if (nitems > NPF_MAX_RULES) {
426 1.51 rmind NPF_ERR_DEBUG(errdict);
427 1.51 rmind return E2BIG;
428 1.51 rmind }
429 1.51 rmind } else {
430 1.51 rmind rules = NULL;
431 1.51 rmind nitems = 0;
432 1.6 rmind }
433 1.51 rmind rlset = npf_ruleset_create(nitems);
434 1.51 rmind for (unsigned i = 0; i < nitems; i++) {
435 1.51 rmind const nvlist_t *rule = rules[i];
436 1.21 rmind npf_rule_t *rl = NULL;
437 1.50 rmind const char *name;
438 1.1 rmind
439 1.57 rmind error = npf_mk_singlerule(npf, rule, nc->rule_procs, &rl,
440 1.57 rmind errdict);
441 1.6 rmind if (error) {
442 1.1 rmind break;
443 1.6 rmind }
444 1.51 rmind name = dnvlist_get_string(rule, "name", NULL);
445 1.51 rmind if (name && npf_ruleset_lookup(rlset, name)) {
446 1.50 rmind NPF_ERR_DEBUG(errdict);
447 1.50 rmind npf_rule_free(rl);
448 1.51 rmind error = EEXIST;
449 1.51 rmind break;
450 1.50 rmind }
451 1.6 rmind npf_ruleset_insert(rlset, rl);
452 1.1 rmind }
453 1.57 rmind nc->ruleset = rlset;
454 1.1 rmind return error;
455 1.1 rmind }
456 1.1 rmind
457 1.6 rmind static int __noinline
458 1.54 rmind npf_mk_singlenat(npf_t *npf, const nvlist_t *nat, npf_ruleset_t *ntset,
459 1.54 rmind npf_tableset_t *tblset, nvlist_t *errdict, npf_rule_t **rlp)
460 1.54 rmind {
461 1.54 rmind npf_rule_t *rl = NULL;
462 1.54 rmind npf_natpolicy_t *np;
463 1.54 rmind int error;
464 1.54 rmind
465 1.54 rmind /*
466 1.54 rmind * NAT rules are standard rules, plus the translation policy.
467 1.54 rmind * We first construct the rule structure.
468 1.54 rmind */
469 1.54 rmind error = npf_mk_singlerule(npf, nat, NULL, &rl, errdict);
470 1.54 rmind if (error) {
471 1.54 rmind return error;
472 1.54 rmind }
473 1.54 rmind KASSERT(rl != NULL);
474 1.54 rmind *rlp = rl;
475 1.54 rmind
476 1.54 rmind /* If rule is named, it is a group with NAT policies. */
477 1.54 rmind if (dnvlist_get_string(nat, "name", NULL)) {
478 1.54 rmind return 0;
479 1.54 rmind }
480 1.54 rmind
481 1.54 rmind /* Check the table ID. */
482 1.54 rmind if (nvlist_exists_number(nat, "nat-table-id")) {
483 1.54 rmind unsigned tid = nvlist_get_number(nat, "nat-table-id");
484 1.54 rmind
485 1.54 rmind if (!npf_tableset_getbyid(tblset, tid)) {
486 1.54 rmind NPF_ERR_DEBUG(errdict);
487 1.54 rmind error = EINVAL;
488 1.54 rmind goto out;
489 1.54 rmind }
490 1.54 rmind }
491 1.54 rmind
492 1.54 rmind /* Allocate a new NAT policy and assign it to the rule. */
493 1.54 rmind np = npf_nat_newpolicy(npf, nat, ntset);
494 1.54 rmind if (np == NULL) {
495 1.54 rmind NPF_ERR_DEBUG(errdict);
496 1.54 rmind error = ENOMEM;
497 1.54 rmind goto out;
498 1.54 rmind }
499 1.54 rmind npf_rule_setnat(rl, np);
500 1.54 rmind out:
501 1.54 rmind if (error) {
502 1.54 rmind npf_rule_free(rl);
503 1.54 rmind }
504 1.54 rmind return error;
505 1.54 rmind }
506 1.54 rmind
507 1.54 rmind static int __noinline
508 1.57 rmind npf_mk_natlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
509 1.57 rmind npf_config_t *nc)
510 1.1 rmind {
511 1.51 rmind const nvlist_t * const *nat_rules;
512 1.51 rmind npf_ruleset_t *ntset;
513 1.51 rmind size_t nitems;
514 1.51 rmind int error = 0;
515 1.1 rmind
516 1.51 rmind /*
517 1.51 rmind * NAT policies must be an array, but enforce a limit.
518 1.51 rmind */
519 1.51 rmind if (nvlist_exists_nvlist_array(npf_dict, "nat")) {
520 1.51 rmind nat_rules = nvlist_get_nvlist_array(npf_dict, "nat", &nitems);
521 1.51 rmind if (nitems > NPF_MAX_RULES) {
522 1.51 rmind NPF_ERR_DEBUG(errdict);
523 1.51 rmind return E2BIG;
524 1.51 rmind }
525 1.51 rmind } else {
526 1.51 rmind nat_rules = NULL;
527 1.51 rmind nitems = 0;
528 1.12 rmind }
529 1.51 rmind ntset = npf_ruleset_create(nitems);
530 1.51 rmind for (unsigned i = 0; i < nitems; i++) {
531 1.51 rmind const nvlist_t *nat = nat_rules[i];
532 1.21 rmind npf_rule_t *rl = NULL;
533 1.1 rmind
534 1.57 rmind error = npf_mk_singlenat(npf, nat, ntset, nc->tableset,
535 1.57 rmind errdict, &rl);
536 1.6 rmind if (error) {
537 1.1 rmind break;
538 1.6 rmind }
539 1.51 rmind npf_ruleset_insert(ntset, rl);
540 1.1 rmind }
541 1.57 rmind nc->nat_ruleset = ntset;
542 1.1 rmind return error;
543 1.1 rmind }
544 1.1 rmind
545 1.1 rmind /*
546 1.35 rmind * npf_mk_connlist: import a list of connections and load them.
547 1.35 rmind */
548 1.35 rmind static int __noinline
549 1.51 rmind npf_mk_connlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict,
550 1.57 rmind npf_config_t *nc, npf_conndb_t **conndb)
551 1.35 rmind {
552 1.51 rmind const nvlist_t * const *conns;
553 1.35 rmind npf_conndb_t *cd;
554 1.51 rmind size_t nitems;
555 1.40 rmind int error = 0;
556 1.35 rmind
557 1.51 rmind if (!nvlist_exists_nvlist_array(npf_dict, "conn-list")) {
558 1.51 rmind *conndb = NULL;
559 1.51 rmind return 0;
560 1.35 rmind }
561 1.51 rmind cd = npf_conndb_create();
562 1.51 rmind conns = nvlist_get_nvlist_array(npf_dict, "conn-list", &nitems);
563 1.51 rmind for (unsigned i = 0; i < nitems; i++) {
564 1.51 rmind const nvlist_t *conn = conns[i];
565 1.35 rmind
566 1.40 rmind /* Construct and insert the connection. */
567 1.57 rmind error = npf_conn_import(npf, cd, conn, nc->nat_ruleset);
568 1.35 rmind if (error) {
569 1.35 rmind NPF_ERR_DEBUG(errdict);
570 1.35 rmind break;
571 1.35 rmind }
572 1.35 rmind }
573 1.35 rmind if (error) {
574 1.53 rmind npf_conndb_gc(npf, cd, true, false);
575 1.35 rmind npf_conndb_destroy(cd);
576 1.35 rmind } else {
577 1.35 rmind *conndb = cd;
578 1.35 rmind }
579 1.35 rmind return error;
580 1.35 rmind }
581 1.35 rmind
582 1.35 rmind /*
583 1.51 rmind * npfctl_load_nvlist: store passed data i.e. the update settings, create
584 1.51 rmind * the passed tables, rules, etc and atomically activate all them.
585 1.1 rmind */
586 1.51 rmind static int
587 1.51 rmind npfctl_load_nvlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict)
588 1.1 rmind {
589 1.57 rmind npf_config_t *nc;
590 1.35 rmind npf_conndb_t *conndb = NULL;
591 1.51 rmind uint64_t ver;
592 1.11 rmind bool flush;
593 1.1 rmind int error;
594 1.1 rmind
595 1.57 rmind nc = npf_config_create();
596 1.51 rmind ver = dnvlist_get_number(npf_dict, "version", UINT64_MAX);
597 1.20 rmind if (ver != NPF_VERSION) {
598 1.20 rmind error = EPROGMISMATCH;
599 1.20 rmind goto fail;
600 1.20 rmind }
601 1.54 rmind error = npf_mk_params(npf, npf_dict, errdict, false /* validate */);
602 1.54 rmind if (error) {
603 1.54 rmind goto fail;
604 1.54 rmind }
605 1.51 rmind error = npf_mk_algs(npf, npf_dict, errdict);
606 1.24 christos if (error) {
607 1.24 christos goto fail;
608 1.24 christos }
609 1.57 rmind error = npf_mk_tables(npf, npf_dict, errdict, nc);
610 1.51 rmind if (error) {
611 1.30 rmind goto fail;
612 1.30 rmind }
613 1.57 rmind error = npf_mk_rprocs(npf, npf_dict, errdict, nc);
614 1.10 rmind if (error) {
615 1.1 rmind goto fail;
616 1.10 rmind }
617 1.57 rmind error = npf_mk_natlist(npf, npf_dict, errdict, nc);
618 1.10 rmind if (error) {
619 1.1 rmind goto fail;
620 1.10 rmind }
621 1.57 rmind error = npf_mk_rules(npf, npf_dict, errdict, nc);
622 1.21 rmind if (error) {
623 1.21 rmind goto fail;
624 1.21 rmind }
625 1.57 rmind error = npf_mk_connlist(npf, npf_dict, errdict, nc, &conndb);
626 1.10 rmind if (error) {
627 1.1 rmind goto fail;
628 1.10 rmind }
629 1.1 rmind
630 1.57 rmind flush = dnvlist_get_bool(npf_dict, "flush", false);
631 1.57 rmind nc->default_pass = flush;
632 1.57 rmind
633 1.1 rmind /*
634 1.35 rmind * Finally - perform the load.
635 1.1 rmind */
636 1.57 rmind npf_config_load(npf, nc, conndb, flush);
637 1.54 rmind npf_mk_params(npf, npf_dict, errdict, true /* set the params */);
638 1.11 rmind
639 1.1 rmind /* Done. Since data is consumed now, we shall not destroy it. */
640 1.57 rmind nc = NULL;
641 1.1 rmind fail:
642 1.57 rmind if (nc) {
643 1.57 rmind npf_config_destroy(nc);
644 1.1 rmind }
645 1.51 rmind nvlist_destroy(npf_dict);
646 1.51 rmind return error;
647 1.51 rmind }
648 1.51 rmind
649 1.51 rmind int
650 1.51 rmind npfctl_load(npf_t *npf, u_long cmd, void *data)
651 1.51 rmind {
652 1.51 rmind nvlist_t *request, *response;
653 1.51 rmind int error;
654 1.12 rmind
655 1.49 ozaki /*
656 1.51 rmind * Retrieve the configuration and check the version.
657 1.51 rmind * Construct a response with error reporting.
658 1.49 ozaki */
659 1.52 christos error = npf_nvlist_copyin(npf, data, &request);
660 1.51 rmind if (error) {
661 1.51 rmind return error;
662 1.49 ozaki }
663 1.51 rmind response = nvlist_create(0);
664 1.51 rmind error = npfctl_load_nvlist(npf, request, response);
665 1.51 rmind nvlist_add_number(response, "errno", error);
666 1.52 christos return npf_nvlist_copyout(npf, data, response);
667 1.6 rmind }
668 1.6 rmind
669 1.21 rmind /*
670 1.35 rmind * npfctl_save: export the config dictionary as it was submitted,
671 1.35 rmind * including the current snapshot of the connections. Additionally,
672 1.35 rmind * indicate whether the ruleset is currently active.
673 1.35 rmind */
674 1.35 rmind int
675 1.45 christos npfctl_save(npf_t *npf, u_long cmd, void *data)
676 1.35 rmind {
677 1.57 rmind npf_config_t *nc;
678 1.51 rmind nvlist_t *npf_dict;
679 1.35 rmind int error;
680 1.35 rmind
681 1.51 rmind npf_dict = nvlist_create(0);
682 1.51 rmind nvlist_add_number(npf_dict, "version", NPF_VERSION);
683 1.35 rmind
684 1.37 rmind /*
685 1.51 rmind * Serialise the whole NPF config, including connections.
686 1.37 rmind */
687 1.57 rmind nc = npf_config_enter(npf);
688 1.51 rmind error = npf_conndb_export(npf, npf_dict);
689 1.37 rmind if (error) {
690 1.37 rmind goto out;
691 1.37 rmind }
692 1.57 rmind error = npf_ruleset_export(npf, nc->ruleset, "rules", npf_dict);
693 1.38 rmind if (error) {
694 1.38 rmind goto out;
695 1.38 rmind }
696 1.57 rmind error = npf_ruleset_export(npf, nc->nat_ruleset, "nat", npf_dict);
697 1.35 rmind if (error) {
698 1.35 rmind goto out;
699 1.35 rmind }
700 1.57 rmind error = npf_tableset_export(npf, nc->tableset, npf_dict);
701 1.38 rmind if (error) {
702 1.38 rmind goto out;
703 1.38 rmind }
704 1.57 rmind error = npf_rprocset_export(nc->rule_procs, npf_dict);
705 1.38 rmind if (error) {
706 1.38 rmind goto out;
707 1.38 rmind }
708 1.51 rmind error = npf_alg_export(npf, npf_dict);
709 1.51 rmind if (error) {
710 1.51 rmind goto out;
711 1.51 rmind }
712 1.58 rmind nvlist_add_bool(npf_dict, "active", npf_active_p());
713 1.52 christos error = npf_nvlist_copyout(npf, data, npf_dict);
714 1.51 rmind npf_dict = NULL;
715 1.35 rmind out:
716 1.45 christos npf_config_exit(npf);
717 1.51 rmind if (npf_dict) {
718 1.51 rmind nvlist_destroy(npf_dict);
719 1.37 rmind }
720 1.35 rmind return error;
721 1.35 rmind }
722 1.35 rmind
723 1.35 rmind /*
724 1.56 rmind * npfctl_table_replace_nvlist: atomically replace a table's contents
725 1.56 rmind * with the passed table data.
726 1.56 rmind */
727 1.56 rmind static int __noinline
728 1.56 rmind npfctl_table_replace_nvlist(npf_t *npf, nvlist_t *npf_dict, nvlist_t *errdict)
729 1.56 rmind {
730 1.56 rmind npf_table_t *tbl, *gc_tbl = NULL;
731 1.57 rmind npf_config_t *nc;
732 1.56 rmind int error = 0;
733 1.56 rmind
734 1.57 rmind nc = npf_config_enter(npf);
735 1.57 rmind error = npf_mk_table(npf, npf_dict, errdict, nc->tableset, &tbl, true);
736 1.56 rmind if (error) {
737 1.56 rmind goto err;
738 1.56 rmind }
739 1.57 rmind gc_tbl = npf_tableset_swap(nc->tableset, tbl);
740 1.56 rmind if (gc_tbl == NULL) {
741 1.56 rmind error = EINVAL;
742 1.56 rmind gc_tbl = tbl;
743 1.56 rmind goto err;
744 1.56 rmind }
745 1.56 rmind npf_config_sync(npf);
746 1.56 rmind err:
747 1.56 rmind npf_config_exit(npf);
748 1.56 rmind if (gc_tbl) {
749 1.56 rmind npf_table_destroy(gc_tbl);
750 1.56 rmind }
751 1.56 rmind return error;
752 1.56 rmind }
753 1.56 rmind
754 1.56 rmind int
755 1.56 rmind npfctl_table_replace(npf_t *npf, u_long cmd, void *data)
756 1.56 rmind {
757 1.56 rmind nvlist_t *request, *response;
758 1.56 rmind int error;
759 1.56 rmind
760 1.56 rmind /*
761 1.56 rmind * Retrieve the configuration and check the version.
762 1.56 rmind * Construct a response with error reporting.
763 1.56 rmind */
764 1.56 rmind error = npf_nvlist_copyin(npf, data, &request);
765 1.56 rmind if (error) {
766 1.56 rmind return error;
767 1.56 rmind }
768 1.56 rmind response = nvlist_create(0);
769 1.56 rmind error = npfctl_table_replace_nvlist(npf, request, response);
770 1.56 rmind nvlist_add_number(response, "errno", error);
771 1.56 rmind error = npf_nvlist_copyout(npf, data, response);
772 1.56 rmind nvlist_destroy(request);
773 1.56 rmind return error;
774 1.56 rmind }
775 1.56 rmind
776 1.56 rmind /*
777 1.44 christos * npfctl_conn_lookup: lookup a connection in the list of connections
778 1.44 christos */
779 1.44 christos int
780 1.45 christos npfctl_conn_lookup(npf_t *npf, u_long cmd, void *data)
781 1.44 christos {
782 1.51 rmind nvlist_t *conn_data, *conn_result;
783 1.44 christos int error;
784 1.44 christos
785 1.52 christos error = npf_nvlist_copyin(npf, data, &conn_data);
786 1.44 christos if (error) {
787 1.44 christos return error;
788 1.44 christos }
789 1.45 christos error = npf_conn_find(npf, conn_data, &conn_result);
790 1.44 christos if (error) {
791 1.44 christos goto out;
792 1.44 christos }
793 1.52 christos error = npf_nvlist_copyout(npf, data, conn_result);
794 1.44 christos out:
795 1.51 rmind nvlist_destroy(conn_data);
796 1.44 christos return error;
797 1.44 christos }
798 1.44 christos
799 1.44 christos /*
800 1.21 rmind * npfctl_rule: add or remove dynamic rules in the specified ruleset.
801 1.21 rmind */
802 1.14 rmind int
803 1.45 christos npfctl_rule(npf_t *npf, u_long cmd, void *data)
804 1.14 rmind {
805 1.51 rmind nvlist_t *npf_rule, *retdict = NULL;
806 1.21 rmind npf_ruleset_t *rlset;
807 1.21 rmind npf_rule_t *rl = NULL;
808 1.21 rmind const char *ruleset_name;
809 1.57 rmind npf_config_t *nc;
810 1.51 rmind uint32_t rcmd;
811 1.45 christos int error = 0;
812 1.54 rmind bool natset;
813 1.14 rmind
814 1.52 christos error = npf_nvlist_copyin(npf, data, &npf_rule);
815 1.21 rmind if (error) {
816 1.21 rmind return error;
817 1.21 rmind }
818 1.51 rmind rcmd = dnvlist_get_number(npf_rule, "command", 0);
819 1.54 rmind natset = dnvlist_get_bool(npf_rule, "nat-rule", false);
820 1.51 rmind ruleset_name = dnvlist_get_string(npf_rule, "ruleset-name", NULL);
821 1.51 rmind if (!ruleset_name) {
822 1.27 rmind error = EINVAL;
823 1.27 rmind goto out;
824 1.21 rmind }
825 1.21 rmind
826 1.57 rmind nc = npf_config_enter(npf);
827 1.57 rmind rlset = natset ? nc->nat_ruleset : nc->ruleset;
828 1.54 rmind switch (rcmd) {
829 1.54 rmind case NPF_CMD_RULE_ADD: {
830 1.51 rmind retdict = nvlist_create(0);
831 1.54 rmind if (natset) {
832 1.54 rmind /*
833 1.54 rmind * Translation rule.
834 1.54 rmind */
835 1.54 rmind error = npf_mk_singlenat(npf, npf_rule, rlset,
836 1.57 rmind nc->tableset, retdict, &rl);
837 1.54 rmind } else {
838 1.54 rmind /*
839 1.54 rmind * Standard rule.
840 1.54 rmind */
841 1.54 rmind error = npf_mk_singlerule(npf, npf_rule, NULL,
842 1.54 rmind &rl, retdict);
843 1.54 rmind }
844 1.51 rmind if (error) {
845 1.54 rmind npf_config_exit(npf);
846 1.27 rmind goto out;
847 1.21 rmind }
848 1.21 rmind if ((error = npf_ruleset_add(rlset, ruleset_name, rl)) == 0) {
849 1.21 rmind /* Success. */
850 1.23 rmind uint64_t id = npf_rule_getid(rl);
851 1.51 rmind nvlist_add_number(retdict, "id", id);
852 1.21 rmind rl = NULL;
853 1.21 rmind }
854 1.21 rmind break;
855 1.21 rmind }
856 1.21 rmind case NPF_CMD_RULE_REMOVE: {
857 1.51 rmind uint64_t id = dnvlist_get_number(npf_rule, "id", UINT64_MAX);
858 1.23 rmind error = npf_ruleset_remove(rlset, ruleset_name, id);
859 1.21 rmind break;
860 1.21 rmind }
861 1.21 rmind case NPF_CMD_RULE_REMKEY: {
862 1.51 rmind const void *key;
863 1.51 rmind size_t len;
864 1.21 rmind
865 1.51 rmind key = dnvlist_get_binary(npf_rule, "key", &len, NULL, 0);
866 1.21 rmind if (len == 0 || len > NPF_RULE_MAXKEYLEN) {
867 1.21 rmind error = EINVAL;
868 1.21 rmind break;
869 1.21 rmind }
870 1.22 rmind error = npf_ruleset_remkey(rlset, ruleset_name, key, len);
871 1.22 rmind break;
872 1.22 rmind }
873 1.22 rmind case NPF_CMD_RULE_LIST: {
874 1.45 christos retdict = npf_ruleset_list(npf, rlset, ruleset_name);
875 1.41 rmind if (!retdict) {
876 1.41 rmind error = ESRCH;
877 1.41 rmind }
878 1.22 rmind break;
879 1.22 rmind }
880 1.22 rmind case NPF_CMD_RULE_FLUSH: {
881 1.22 rmind error = npf_ruleset_flush(rlset, ruleset_name);
882 1.21 rmind break;
883 1.21 rmind }
884 1.21 rmind default:
885 1.21 rmind error = EINVAL;
886 1.21 rmind break;
887 1.21 rmind }
888 1.22 rmind
889 1.22 rmind /* Destroy any removed rules. */
890 1.22 rmind if (!error && rcmd != NPF_CMD_RULE_ADD && rcmd != NPF_CMD_RULE_LIST) {
891 1.45 christos npf_config_sync(npf);
892 1.22 rmind npf_ruleset_gc(rlset);
893 1.22 rmind }
894 1.45 christos npf_config_exit(npf);
895 1.14 rmind
896 1.21 rmind if (rl) {
897 1.41 rmind KASSERT(error);
898 1.21 rmind npf_rule_free(rl);
899 1.21 rmind }
900 1.27 rmind out:
901 1.52 christos if (retdict && npf_nvlist_copyout(npf, data, retdict) != 0) {
902 1.51 rmind error = EFAULT; // copyout failure
903 1.21 rmind }
904 1.51 rmind nvlist_destroy(npf_rule);
905 1.14 rmind return error;
906 1.14 rmind }
907 1.14 rmind
908 1.6 rmind /*
909 1.2 rmind * npfctl_table: add, remove or query entries in the specified table.
910 1.1 rmind *
911 1.51 rmind * For maximum performance, the interface is using plain structures.
912 1.1 rmind */
913 1.1 rmind int
914 1.45 christos npfctl_table(npf_t *npf, void *data)
915 1.1 rmind {
916 1.19 rmind const npf_ioctl_table_t *nct = data;
917 1.32 rmind char tname[NPF_TABLE_MAXNAMELEN];
918 1.57 rmind npf_config_t *nc;
919 1.32 rmind npf_table_t *t;
920 1.53 rmind int error;
921 1.32 rmind
922 1.32 rmind error = copyinstr(nct->nct_name, tname, sizeof(tname), NULL);
923 1.32 rmind if (error) {
924 1.32 rmind return error;
925 1.32 rmind }
926 1.1 rmind
927 1.57 rmind nc = npf_config_enter(npf);
928 1.57 rmind if ((t = npf_tableset_getbyname(nc->tableset, tname)) == NULL) {
929 1.53 rmind npf_config_exit(npf);
930 1.32 rmind return EINVAL;
931 1.32 rmind }
932 1.21 rmind
933 1.21 rmind switch (nct->nct_cmd) {
934 1.21 rmind case NPF_CMD_TABLE_LOOKUP:
935 1.32 rmind error = npf_table_lookup(t, nct->nct_data.ent.alen,
936 1.32 rmind &nct->nct_data.ent.addr);
937 1.20 rmind break;
938 1.21 rmind case NPF_CMD_TABLE_ADD:
939 1.32 rmind error = npf_table_insert(t, nct->nct_data.ent.alen,
940 1.32 rmind &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
941 1.1 rmind break;
942 1.21 rmind case NPF_CMD_TABLE_REMOVE:
943 1.32 rmind error = npf_table_remove(t, nct->nct_data.ent.alen,
944 1.32 rmind &nct->nct_data.ent.addr, nct->nct_data.ent.mask);
945 1.19 rmind break;
946 1.21 rmind case NPF_CMD_TABLE_LIST:
947 1.32 rmind error = npf_table_list(t, nct->nct_data.buf.buf,
948 1.32 rmind nct->nct_data.buf.len);
949 1.1 rmind break;
950 1.25 rmind case NPF_CMD_TABLE_FLUSH:
951 1.32 rmind error = npf_table_flush(t);
952 1.25 rmind break;
953 1.1 rmind default:
954 1.19 rmind error = EINVAL;
955 1.19 rmind break;
956 1.1 rmind }
957 1.53 rmind npf_table_gc(npf, t);
958 1.53 rmind npf_config_exit(npf);
959 1.21 rmind
960 1.1 rmind return error;
961 1.1 rmind }
962