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