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