xref: /freebsd-13.1/sys/netpfil/pf/pf_nv.c (revision 1c8d9e1b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 
34 #include <sys/param.h>
35 #include <sys/errno.h>
36 #include <sys/limits.h>
37 #include <sys/queue.h>
38 #include <sys/systm.h>
39 
40 #include <netpfil/pf/pf_nv.h>
41 
42 #define	PF_NV_IMPL_UINT(fnname, type, max)					\
43 	int									\
44 	pf_nv ## fnname ## _opt(const nvlist_t *nvl, const char *name,		\
45 	    type *val, type dflt)						\
46 	{									\
47 		uint64_t raw;							\
48 		if (! nvlist_exists_number(nvl, name)) {			\
49 			*val = dflt;						\
50 			return (0);						\
51 		}								\
52 		raw = nvlist_get_number(nvl, name);				\
53 		if (raw > max)							\
54 			return (ERANGE);					\
55 		*val = (type)raw;						\
56 		return (0);							\
57 	}									\
58 	int									\
59 	pf_nv ## fnname(const nvlist_t *nvl, const char *name, type *val)	\
60 	{									\
61 		uint64_t raw;							\
62 		if (! nvlist_exists_number(nvl, name))				\
63 			return (EINVAL);					\
64 		raw = nvlist_get_number(nvl, name);				\
65 		if (raw > max)							\
66 			return (ERANGE);					\
67 		*val = (type)raw;						\
68 		return (0);							\
69 	}									\
70 	int									\
71 	pf_nv ## fnname ## _array(const nvlist_t *nvl, const char *name,	\
72 	    type *array, size_t maxelems, size_t *nelems)			\
73 	{									\
74 		const uint64_t *n;						\
75 		size_t nitems;							\
76 		bzero(array, sizeof(type) * maxelems);				\
77 		if (! nvlist_exists_number_array(nvl, name))			\
78 			return (EINVAL);					\
79 		n = nvlist_get_number_array(nvl, name, &nitems);		\
80 		if (nitems != maxelems)						\
81 			return (E2BIG);						\
82 		if (nelems != NULL)						\
83 			*nelems = nitems;					\
84 		for (size_t i = 0; i < nitems; i++) {				\
85 			if (n[i] > max)						\
86 				return (ERANGE);				\
87 			array[i] = (type)n[i];					\
88 		}								\
89 		return (0);							\
90 	}									\
91 	void									\
92 	pf_ ## fnname ## _array_nv(nvlist_t *nvl, const char *name,		\
93 	    const type *numbers, size_t count)					\
94 	{									\
95 		uint64_t tmp;							\
96 		for (size_t i = 0; i < count; i++) {				\
97 			tmp = numbers[i];					\
98 			nvlist_append_number_array(nvl, name, tmp);		\
99 		}								\
100 	}
101 
102 int
pf_nvbool(const nvlist_t * nvl,const char * name,bool * val)103 pf_nvbool(const nvlist_t *nvl, const char *name, bool *val)
104 {
105 	if (! nvlist_exists_bool(nvl, name))
106 		return (EINVAL);
107 
108 	*val = nvlist_get_bool(nvl, name);
109 
110 	return (0);
111 }
112 
113 int
pf_nvbinary(const nvlist_t * nvl,const char * name,void * data,size_t expected_size)114 pf_nvbinary(const nvlist_t *nvl, const char *name, void *data,
115     size_t expected_size)
116 {
117 	const uint8_t *nvdata;
118 	size_t len;
119 
120 	bzero(data, expected_size);
121 
122 	if (! nvlist_exists_binary(nvl, name))
123 		return (EINVAL);
124 
125 	nvdata = (const uint8_t *)nvlist_get_binary(nvl, name, &len);
126 	if (len > expected_size)
127 		return (EINVAL);
128 
129 	memcpy(data, nvdata, len);
130 
131 	return (0);
132 }
133 
134 PF_NV_IMPL_UINT(uint8, uint8_t, UINT8_MAX);
135 PF_NV_IMPL_UINT(uint16, uint16_t, UINT16_MAX);
136 PF_NV_IMPL_UINT(uint32, uint32_t, UINT32_MAX);
137 PF_NV_IMPL_UINT(uint64, uint64_t, UINT64_MAX);
138 
139 int
pf_nvint(const nvlist_t * nvl,const char * name,int * val)140 pf_nvint(const nvlist_t *nvl, const char *name, int *val)
141 {
142 	int64_t raw;
143 
144 	if (! nvlist_exists_number(nvl, name))
145 		return (EINVAL);
146 
147 	raw = nvlist_get_number(nvl, name);
148 	if (raw > INT_MAX || raw < INT_MIN)
149 		return (ERANGE);
150 
151 	*val = (int)raw;
152 
153 	return (0);
154 }
155 
156 int
pf_nvstring(const nvlist_t * nvl,const char * name,char * str,size_t maxlen)157 pf_nvstring(const nvlist_t *nvl, const char *name, char *str, size_t maxlen)
158 {
159 	int ret;
160 
161 	if (! nvlist_exists_string(nvl, name))
162 		return (EINVAL);
163 
164 	ret = strlcpy(str, nvlist_get_string(nvl, name), maxlen);
165 	if (ret >= maxlen)
166 		return (EINVAL);
167 
168 	return (0);
169 }
170 
171 static int
pf_nvaddr_to_addr(const nvlist_t * nvl,struct pf_addr * paddr)172 pf_nvaddr_to_addr(const nvlist_t *nvl, struct pf_addr *paddr)
173 {
174 	return (pf_nvbinary(nvl, "addr", paddr, sizeof(*paddr)));
175 }
176 
177 static nvlist_t *
pf_addr_to_nvaddr(const struct pf_addr * paddr)178 pf_addr_to_nvaddr(const struct pf_addr *paddr)
179 {
180 	nvlist_t *nvl;
181 
182 	nvl = nvlist_create(0);
183 	if (nvl == NULL)
184 		return (NULL);
185 
186 	nvlist_add_binary(nvl, "addr", paddr, sizeof(*paddr));
187 
188 	return (nvl);
189 }
190 
191 static int
pf_nvmape_to_mape(const nvlist_t * nvl,struct pf_mape_portset * mape)192 pf_nvmape_to_mape(const nvlist_t *nvl, struct pf_mape_portset *mape)
193 {
194 	int error = 0;
195 
196 	bzero(mape, sizeof(*mape));
197 	PFNV_CHK(pf_nvuint8(nvl, "offset", &mape->offset));
198 	PFNV_CHK(pf_nvuint8(nvl, "psidlen", &mape->psidlen));
199 	PFNV_CHK(pf_nvuint16(nvl, "psid", &mape->psid));
200 
201 errout:
202 	return (error);
203 }
204 
205 static nvlist_t *
pf_mape_to_nvmape(const struct pf_mape_portset * mape)206 pf_mape_to_nvmape(const struct pf_mape_portset *mape)
207 {
208 	nvlist_t *nvl;
209 
210 	nvl = nvlist_create(0);
211 	if (nvl == NULL)
212 		return (NULL);
213 
214 	nvlist_add_number(nvl, "offset", mape->offset);
215 	nvlist_add_number(nvl, "psidlen", mape->psidlen);
216 	nvlist_add_number(nvl, "psid", mape->psid);
217 
218 	return (nvl);
219 }
220 
221 static int
pf_nvpool_to_pool(const nvlist_t * nvl,struct pf_kpool * kpool)222 pf_nvpool_to_pool(const nvlist_t *nvl, struct pf_kpool *kpool)
223 {
224 	int error = 0;
225 
226 	PFNV_CHK(pf_nvbinary(nvl, "key", &kpool->key, sizeof(kpool->key)));
227 
228 	if (nvlist_exists_nvlist(nvl, "counter")) {
229 		PFNV_CHK(pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "counter"),
230 		    &kpool->counter));
231 	}
232 
233 	PFNV_CHK(pf_nvint(nvl, "tblidx", &kpool->tblidx));
234 	PFNV_CHK(pf_nvuint16_array(nvl, "proxy_port", kpool->proxy_port, 2,
235 	    NULL));
236 	PFNV_CHK(pf_nvuint8(nvl, "opts", &kpool->opts));
237 
238 	if (nvlist_exists_nvlist(nvl, "mape")) {
239 		PFNV_CHK(pf_nvmape_to_mape(nvlist_get_nvlist(nvl, "mape"),
240 		    &kpool->mape));
241 	}
242 
243 errout:
244 	return (error);
245 }
246 
247 static nvlist_t *
pf_pool_to_nvpool(const struct pf_kpool * pool)248 pf_pool_to_nvpool(const struct pf_kpool *pool)
249 {
250 	nvlist_t *nvl;
251 	nvlist_t *tmp;
252 
253 	nvl = nvlist_create(0);
254 	if (nvl == NULL)
255 		return (NULL);
256 
257 	nvlist_add_binary(nvl, "key", &pool->key, sizeof(pool->key));
258 	tmp = pf_addr_to_nvaddr(&pool->counter);
259 	if (tmp == NULL)
260 		goto error;
261 	nvlist_add_nvlist(nvl, "counter", tmp);
262 	nvlist_destroy(tmp);
263 
264 	nvlist_add_number(nvl, "tblidx", pool->tblidx);
265 	pf_uint16_array_nv(nvl, "proxy_port", pool->proxy_port, 2);
266 	nvlist_add_number(nvl, "opts", pool->opts);
267 
268 	tmp = pf_mape_to_nvmape(&pool->mape);
269 	if (tmp == NULL)
270 		goto error;
271 	nvlist_add_nvlist(nvl, "mape", tmp);
272 	nvlist_destroy(tmp);
273 
274 	return (nvl);
275 
276 error:
277 	nvlist_destroy(nvl);
278 	return (NULL);
279 }
280 
281 static int
pf_nvaddr_wrap_to_addr_wrap(const nvlist_t * nvl,struct pf_addr_wrap * addr)282 pf_nvaddr_wrap_to_addr_wrap(const nvlist_t *nvl, struct pf_addr_wrap *addr)
283 {
284 	int error = 0;
285 
286 	bzero(addr, sizeof(*addr));
287 
288 	PFNV_CHK(pf_nvuint8(nvl, "type", &addr->type));
289 	PFNV_CHK(pf_nvuint8(nvl, "iflags", &addr->iflags));
290 	if (addr->type == PF_ADDR_DYNIFTL)
291 		PFNV_CHK(pf_nvstring(nvl, "ifname", addr->v.ifname,
292 		    sizeof(addr->v.ifname)));
293 	if (addr->type == PF_ADDR_TABLE)
294 		PFNV_CHK(pf_nvstring(nvl, "tblname", addr->v.tblname,
295 		    sizeof(addr->v.tblname)));
296 
297 	if (! nvlist_exists_nvlist(nvl, "addr"))
298 		return (EINVAL);
299 	PFNV_CHK(pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "addr"),
300 	    &addr->v.a.addr));
301 
302 	if (! nvlist_exists_nvlist(nvl, "mask"))
303 		return (EINVAL);
304 	PFNV_CHK(pf_nvaddr_to_addr(nvlist_get_nvlist(nvl, "mask"),
305 	    &addr->v.a.mask));
306 
307 	switch (addr->type) {
308 	case PF_ADDR_DYNIFTL:
309 	case PF_ADDR_TABLE:
310 	case PF_ADDR_RANGE:
311 	case PF_ADDR_ADDRMASK:
312 	case PF_ADDR_NOROUTE:
313 	case PF_ADDR_URPFFAILED:
314 		break;
315 	default:
316 		return (EINVAL);
317 	}
318 
319 errout:
320 	return (error);
321 }
322 
323 static nvlist_t *
pf_addr_wrap_to_nvaddr_wrap(const struct pf_addr_wrap * addr)324 pf_addr_wrap_to_nvaddr_wrap(const struct pf_addr_wrap *addr)
325 {
326 	nvlist_t *nvl;
327 	nvlist_t *tmp;
328 	uint64_t num;
329 	struct pfr_ktable *kt;
330 
331 	nvl = nvlist_create(0);
332 	if (nvl == NULL)
333 		return (NULL);
334 
335 	nvlist_add_number(nvl, "type", addr->type);
336 	nvlist_add_number(nvl, "iflags", addr->iflags);
337 	if (addr->type == PF_ADDR_DYNIFTL) {
338 		nvlist_add_string(nvl, "ifname", addr->v.ifname);
339 		num = 0;
340 		if (addr->p.dyn != NULL)
341 			num = addr->p.dyn->pfid_acnt4 +
342 			    addr->p.dyn->pfid_acnt6;
343 		nvlist_add_number(nvl, "dyncnt", num);
344 	}
345 	if (addr->type == PF_ADDR_TABLE) {
346 		nvlist_add_string(nvl, "tblname", addr->v.tblname);
347 		num = -1;
348 		kt = addr->p.tbl;
349 		if ((kt->pfrkt_flags & PFR_TFLAG_ACTIVE) &&
350 		    kt->pfrkt_root != NULL)
351 			kt = kt->pfrkt_root;
352 		if (kt->pfrkt_flags & PFR_TFLAG_ACTIVE)
353 			num = kt->pfrkt_cnt;
354 		nvlist_add_number(nvl, "tblcnt", num);
355 	}
356 
357 	tmp = pf_addr_to_nvaddr(&addr->v.a.addr);
358 	if (tmp == NULL)
359 		goto error;
360 	nvlist_add_nvlist(nvl, "addr", tmp);
361 	nvlist_destroy(tmp);
362 	tmp = pf_addr_to_nvaddr(&addr->v.a.mask);
363 	if (tmp == NULL)
364 		goto error;
365 	nvlist_add_nvlist(nvl, "mask", tmp);
366 	nvlist_destroy(tmp);
367 
368 	return (nvl);
369 
370 error:
371 	nvlist_destroy(nvl);
372 	return (NULL);
373 }
374 
375 static int
pf_validate_op(uint8_t op)376 pf_validate_op(uint8_t op)
377 {
378 	switch (op) {
379 	case PF_OP_NONE:
380 	case PF_OP_IRG:
381 	case PF_OP_EQ:
382 	case PF_OP_NE:
383 	case PF_OP_LT:
384 	case PF_OP_LE:
385 	case PF_OP_GT:
386 	case PF_OP_GE:
387 	case PF_OP_XRG:
388 	case PF_OP_RRG:
389 		break;
390 	default:
391 		return (EINVAL);
392 	}
393 
394 	return (0);
395 }
396 
397 static int
pf_nvrule_addr_to_rule_addr(const nvlist_t * nvl,struct pf_rule_addr * addr)398 pf_nvrule_addr_to_rule_addr(const nvlist_t *nvl, struct pf_rule_addr *addr)
399 {
400 	int error = 0;
401 
402 	if (! nvlist_exists_nvlist(nvl, "addr"))
403 		return (EINVAL);
404 
405 	PFNV_CHK(pf_nvaddr_wrap_to_addr_wrap(nvlist_get_nvlist(nvl, "addr"),
406 	    &addr->addr));
407 	PFNV_CHK(pf_nvuint16_array(nvl, "port", addr->port, 2, NULL));
408 	PFNV_CHK(pf_nvuint8(nvl, "neg", &addr->neg));
409 	PFNV_CHK(pf_nvuint8(nvl, "port_op", &addr->port_op));
410 
411 	PFNV_CHK(pf_validate_op(addr->port_op));
412 
413 errout:
414 	return (error);
415 }
416 
417 static nvlist_t *
pf_rule_addr_to_nvrule_addr(const struct pf_rule_addr * addr)418 pf_rule_addr_to_nvrule_addr(const struct pf_rule_addr *addr)
419 {
420 	nvlist_t *nvl;
421 	nvlist_t *tmp;
422 
423 	nvl = nvlist_create(0);
424 	if (nvl == NULL)
425 		return (NULL);
426 
427 	tmp = pf_addr_wrap_to_nvaddr_wrap(&addr->addr);
428 	if (tmp == NULL)
429 		goto error;
430 	nvlist_add_nvlist(nvl, "addr", tmp);
431 	nvlist_destroy(tmp);
432 	pf_uint16_array_nv(nvl, "port", addr->port, 2);
433 	nvlist_add_number(nvl, "neg", addr->neg);
434 	nvlist_add_number(nvl, "port_op", addr->port_op);
435 
436 	return (nvl);
437 
438 error:
439 	nvlist_destroy(nvl);
440 	return (NULL);
441 }
442 
443 static int
pf_nvrule_uid_to_rule_uid(const nvlist_t * nvl,struct pf_rule_uid * uid)444 pf_nvrule_uid_to_rule_uid(const nvlist_t *nvl, struct pf_rule_uid *uid)
445 {
446 	int error = 0;
447 
448 	bzero(uid, sizeof(*uid));
449 
450 	PFNV_CHK(pf_nvuint32_array(nvl, "uid", uid->uid, 2, NULL));
451 	PFNV_CHK(pf_nvuint8(nvl, "op", &uid->op));
452 
453 	PFNV_CHK(pf_validate_op(uid->op));
454 
455 errout:
456 	return (error);
457 }
458 
459 static nvlist_t *
pf_rule_uid_to_nvrule_uid(const struct pf_rule_uid * uid)460 pf_rule_uid_to_nvrule_uid(const struct pf_rule_uid *uid)
461 {
462 	nvlist_t *nvl;
463 
464 	nvl = nvlist_create(0);
465 	if (nvl == NULL)
466 		return (NULL);
467 
468 	pf_uint32_array_nv(nvl, "uid", uid->uid, 2);
469 	nvlist_add_number(nvl, "op", uid->op);
470 
471 	return (nvl);
472 }
473 
474 static int
pf_nvrule_gid_to_rule_gid(const nvlist_t * nvl,struct pf_rule_gid * gid)475 pf_nvrule_gid_to_rule_gid(const nvlist_t *nvl, struct pf_rule_gid *gid)
476 {
477 	/* Cheat a little. These stucts are the same, other than the name of
478 	 * the first field. */
479 	return (pf_nvrule_uid_to_rule_uid(nvl, (struct pf_rule_uid *)gid));
480 }
481 
482 int
pf_check_rule_addr(const struct pf_rule_addr * addr)483 pf_check_rule_addr(const struct pf_rule_addr *addr)
484 {
485 
486 	switch (addr->addr.type) {
487 	case PF_ADDR_ADDRMASK:
488 	case PF_ADDR_NOROUTE:
489 	case PF_ADDR_DYNIFTL:
490 	case PF_ADDR_TABLE:
491 	case PF_ADDR_URPFFAILED:
492 	case PF_ADDR_RANGE:
493 		break;
494 	default:
495 		return (EINVAL);
496 	}
497 
498 	if (addr->addr.p.dyn != NULL) {
499 		return (EINVAL);
500 	}
501 
502 	return (0);
503 }
504 
505 
506 int
pf_nvrule_to_krule(const nvlist_t * nvl,struct pf_krule * rule)507 pf_nvrule_to_krule(const nvlist_t *nvl, struct pf_krule *rule)
508 {
509 	int error = 0;
510 
511 #define	ERROUT(x)	ERROUT_FUNCTION(errout, x)
512 
513 	PFNV_CHK(pf_nvuint32(nvl, "nr", &rule->nr));
514 
515 	if (! nvlist_exists_nvlist(nvl, "src"))
516 		ERROUT(EINVAL);
517 
518 	error = pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "src"),
519 	    &rule->src);
520 	if (error != 0)
521 		ERROUT(error);
522 
523 	if (! nvlist_exists_nvlist(nvl, "dst"))
524 		ERROUT(EINVAL);
525 
526 	PFNV_CHK(pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "dst"),
527 	    &rule->dst));
528 
529 	if (nvlist_exists_string(nvl, "label")) {
530 		PFNV_CHK(pf_nvstring(nvl, "label", rule->label[0],
531 		    sizeof(rule->label[0])));
532 	} else if (nvlist_exists_string_array(nvl, "labels")) {
533 		const char *const *strs;
534 		size_t items;
535 		int ret;
536 
537 		strs = nvlist_get_string_array(nvl, "labels", &items);
538 		if (items > PF_RULE_MAX_LABEL_COUNT)
539 			ERROUT(E2BIG);
540 
541 		for (size_t i = 0; i < items; i++) {
542 			ret = strlcpy(rule->label[i], strs[i],
543 			    sizeof(rule->label[0]));
544 			if (ret >= sizeof(rule->label[0]))
545 				ERROUT(E2BIG);
546 		}
547 	}
548 
549 	PFNV_CHK(pf_nvuint32_opt(nvl, "ridentifier", &rule->ridentifier, 0));
550 	PFNV_CHK(pf_nvstring(nvl, "ifname", rule->ifname,
551 	    sizeof(rule->ifname)));
552 	PFNV_CHK(pf_nvstring(nvl, "qname", rule->qname, sizeof(rule->qname)));
553 	PFNV_CHK(pf_nvstring(nvl, "pqname", rule->pqname,
554 	    sizeof(rule->pqname)));
555 	PFNV_CHK(pf_nvstring(nvl, "tagname", rule->tagname,
556 	    sizeof(rule->tagname)));
557 	PFNV_CHK(pf_nvstring(nvl, "match_tagname", rule->match_tagname,
558 	    sizeof(rule->match_tagname)));
559 	PFNV_CHK(pf_nvstring(nvl, "overload_tblname", rule->overload_tblname,
560 	    sizeof(rule->overload_tblname)));
561 
562 	if (! nvlist_exists_nvlist(nvl, "rpool"))
563 		ERROUT(EINVAL);
564 	PFNV_CHK(pf_nvpool_to_pool(nvlist_get_nvlist(nvl, "rpool"),
565 	    &rule->rpool));
566 
567 	PFNV_CHK(pf_nvuint32(nvl, "os_fingerprint", &rule->os_fingerprint));
568 
569 	PFNV_CHK(pf_nvint(nvl, "rtableid", &rule->rtableid));
570 	PFNV_CHK(pf_nvuint32_array(nvl, "timeout", rule->timeout, PFTM_MAX, NULL));
571 	PFNV_CHK(pf_nvuint32(nvl, "max_states", &rule->max_states));
572 	PFNV_CHK(pf_nvuint32(nvl, "max_src_nodes", &rule->max_src_nodes));
573 	PFNV_CHK(pf_nvuint32(nvl, "max_src_states", &rule->max_src_states));
574 	PFNV_CHK(pf_nvuint32(nvl, "max_src_conn", &rule->max_src_conn));
575 	PFNV_CHK(pf_nvuint32(nvl, "max_src_conn_rate.limit",
576 	    &rule->max_src_conn_rate.limit));
577 	PFNV_CHK(pf_nvuint32(nvl, "max_src_conn_rate.seconds",
578 	    &rule->max_src_conn_rate.seconds));
579 	PFNV_CHK(pf_nvuint32(nvl, "prob", &rule->prob));
580 	PFNV_CHK(pf_nvuint32(nvl, "cuid", &rule->cuid));
581 	PFNV_CHK(pf_nvuint32(nvl, "cpid", &rule->cpid));
582 
583 	PFNV_CHK(pf_nvuint16(nvl, "return_icmp", &rule->return_icmp));
584 	PFNV_CHK(pf_nvuint16(nvl, "return_icmp6", &rule->return_icmp6));
585 
586 	PFNV_CHK(pf_nvuint16(nvl, "max_mss", &rule->max_mss));
587 	PFNV_CHK(pf_nvuint16(nvl, "scrub_flags", &rule->scrub_flags));
588 
589 	if (! nvlist_exists_nvlist(nvl, "uid"))
590 		ERROUT(EINVAL);
591 	PFNV_CHK(pf_nvrule_uid_to_rule_uid(nvlist_get_nvlist(nvl, "uid"),
592 	    &rule->uid));
593 
594 	if (! nvlist_exists_nvlist(nvl, "gid"))
595 		ERROUT(EINVAL);
596 	PFNV_CHK(pf_nvrule_gid_to_rule_gid(nvlist_get_nvlist(nvl, "gid"),
597 	    &rule->gid));
598 
599 	PFNV_CHK(pf_nvuint32(nvl, "rule_flag", &rule->rule_flag));
600 	PFNV_CHK(pf_nvuint8(nvl, "action", &rule->action));
601 	PFNV_CHK(pf_nvuint8(nvl, "direction", &rule->direction));
602 	PFNV_CHK(pf_nvuint8(nvl, "log", &rule->log));
603 	PFNV_CHK(pf_nvuint8(nvl, "logif", &rule->logif));
604 	PFNV_CHK(pf_nvuint8(nvl, "quick", &rule->quick));
605 	PFNV_CHK(pf_nvuint8(nvl, "ifnot", &rule->ifnot));
606 	PFNV_CHK(pf_nvuint8(nvl, "match_tag_not", &rule->match_tag_not));
607 	PFNV_CHK(pf_nvuint8(nvl, "natpass", &rule->natpass));
608 
609 	PFNV_CHK(pf_nvuint8(nvl, "keep_state", &rule->keep_state));
610 	PFNV_CHK(pf_nvuint8(nvl, "af", &rule->af));
611 	PFNV_CHK(pf_nvuint8(nvl, "proto", &rule->proto));
612 	PFNV_CHK(pf_nvuint8(nvl, "type", &rule->type));
613 	PFNV_CHK(pf_nvuint8(nvl, "code", &rule->code));
614 	PFNV_CHK(pf_nvuint8(nvl, "flags", &rule->flags));
615 	PFNV_CHK(pf_nvuint8(nvl, "flagset", &rule->flagset));
616 	PFNV_CHK(pf_nvuint8(nvl, "min_ttl", &rule->min_ttl));
617 	PFNV_CHK(pf_nvuint8(nvl, "allow_opts", &rule->allow_opts));
618 	PFNV_CHK(pf_nvuint8(nvl, "rt", &rule->rt));
619 	PFNV_CHK(pf_nvuint8(nvl, "return_ttl", &rule->return_ttl));
620 	PFNV_CHK(pf_nvuint8(nvl, "tos", &rule->tos));
621 	PFNV_CHK(pf_nvuint8(nvl, "set_tos", &rule->set_tos));
622 
623 	PFNV_CHK(pf_nvuint8(nvl, "flush", &rule->flush));
624 	PFNV_CHK(pf_nvuint8(nvl, "prio", &rule->prio));
625 
626 	PFNV_CHK(pf_nvuint8_array(nvl, "set_prio", rule->set_prio, 2, NULL));
627 
628 	if (nvlist_exists_nvlist(nvl, "divert")) {
629 		const nvlist_t *nvldivert = nvlist_get_nvlist(nvl, "divert");
630 
631 		if (! nvlist_exists_nvlist(nvldivert, "addr"))
632 			ERROUT(EINVAL);
633 		PFNV_CHK(pf_nvaddr_to_addr(nvlist_get_nvlist(nvldivert, "addr"),
634 		    &rule->divert.addr));
635 		PFNV_CHK(pf_nvuint16(nvldivert, "port", &rule->divert.port));
636 	}
637 
638 	/* Validation */
639 #ifndef INET
640 	if (rule->af == AF_INET)
641 		ERROUT(EAFNOSUPPORT);
642 #endif /* INET */
643 #ifndef INET6
644 	if (rule->af == AF_INET6)
645 		ERROUT(EAFNOSUPPORT);
646 #endif /* INET6 */
647 
648 	PFNV_CHK(pf_check_rule_addr(&rule->src));
649 	PFNV_CHK(pf_check_rule_addr(&rule->dst));
650 
651 	return (0);
652 
653 #undef ERROUT
654 errout:
655 	return (error);
656 }
657 
658 static nvlist_t *
pf_divert_to_nvdivert(const struct pf_krule * rule)659 pf_divert_to_nvdivert(const struct pf_krule *rule)
660 {
661 	nvlist_t *nvl;
662 	nvlist_t *tmp;
663 
664 	nvl = nvlist_create(0);
665 	if (nvl == NULL)
666 		return (NULL);
667 
668 	tmp = pf_addr_to_nvaddr(&rule->divert.addr);
669 	if (tmp == NULL)
670 		goto error;
671 	nvlist_add_nvlist(nvl, "addr", tmp);
672 	nvlist_destroy(tmp);
673 	nvlist_add_number(nvl, "port", rule->divert.port);
674 
675 	return (nvl);
676 
677 error:
678 	nvlist_destroy(nvl);
679 	return (NULL);
680 }
681 
682 nvlist_t *
pf_krule_to_nvrule(struct pf_krule * rule)683 pf_krule_to_nvrule(struct pf_krule *rule)
684 {
685 	nvlist_t *nvl, *tmp;
686 
687 	nvl = nvlist_create(0);
688 	if (nvl == NULL)
689 		return (nvl);
690 
691 	nvlist_add_number(nvl, "nr", rule->nr);
692 	tmp = pf_rule_addr_to_nvrule_addr(&rule->src);
693 	if (tmp == NULL)
694 		goto error;
695 	nvlist_add_nvlist(nvl, "src", tmp);
696 	nvlist_destroy(tmp);
697 	tmp = pf_rule_addr_to_nvrule_addr(&rule->dst);
698 	if (tmp == NULL)
699 		goto error;
700 	nvlist_add_nvlist(nvl, "dst", tmp);
701 	nvlist_destroy(tmp);
702 
703 	for (int i = 0; i < PF_SKIP_COUNT; i++) {
704 		nvlist_append_number_array(nvl, "skip",
705 		    rule->skip[i].ptr ? rule->skip[i].ptr->nr : -1);
706 	}
707 
708 	for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) {
709 		nvlist_append_string_array(nvl, "labels", rule->label[i]);
710 	}
711 	nvlist_add_string(nvl, "label", rule->label[0]);
712 	nvlist_add_number(nvl, "ridentifier", rule->ridentifier);
713 	nvlist_add_string(nvl, "ifname", rule->ifname);
714 	nvlist_add_string(nvl, "qname", rule->qname);
715 	nvlist_add_string(nvl, "pqname", rule->pqname);
716 	nvlist_add_string(nvl, "tagname", rule->tagname);
717 	nvlist_add_string(nvl, "match_tagname", rule->match_tagname);
718 	nvlist_add_string(nvl, "overload_tblname", rule->overload_tblname);
719 
720 	tmp = pf_pool_to_nvpool(&rule->rpool);
721 	if (tmp == NULL)
722 		goto error;
723 	nvlist_add_nvlist(nvl, "rpool", tmp);
724 	nvlist_destroy(tmp);
725 
726 	nvlist_add_number(nvl, "evaluations",
727 	    pf_counter_u64_fetch(&rule->evaluations));
728 	for (int i = 0; i < 2; i++) {
729 		nvlist_append_number_array(nvl, "packets",
730 		    pf_counter_u64_fetch(&rule->packets[i]));
731 		nvlist_append_number_array(nvl, "bytes",
732 		    pf_counter_u64_fetch(&rule->bytes[i]));
733 	}
734 
735 	nvlist_add_number(nvl, "os_fingerprint", rule->os_fingerprint);
736 
737 	nvlist_add_number(nvl, "rtableid", rule->rtableid);
738 	pf_uint32_array_nv(nvl, "timeout", rule->timeout, PFTM_MAX);
739 	nvlist_add_number(nvl, "max_states", rule->max_states);
740 	nvlist_add_number(nvl, "max_src_nodes", rule->max_src_nodes);
741 	nvlist_add_number(nvl, "max_src_states", rule->max_src_states);
742 	nvlist_add_number(nvl, "max_src_conn", rule->max_src_conn);
743 	nvlist_add_number(nvl, "max_src_conn_rate.limit",
744 	    rule->max_src_conn_rate.limit);
745 	nvlist_add_number(nvl, "max_src_conn_rate.seconds",
746 	    rule->max_src_conn_rate.seconds);
747 	nvlist_add_number(nvl, "qid", rule->qid);
748 	nvlist_add_number(nvl, "pqid", rule->pqid);
749 	nvlist_add_number(nvl, "prob", rule->prob);
750 	nvlist_add_number(nvl, "cuid", rule->cuid);
751 	nvlist_add_number(nvl, "cpid", rule->cpid);
752 
753 	nvlist_add_number(nvl, "states_cur",
754 	    counter_u64_fetch(rule->states_cur));
755 	nvlist_add_number(nvl, "states_tot",
756 	    counter_u64_fetch(rule->states_tot));
757 	nvlist_add_number(nvl, "src_nodes",
758 	    counter_u64_fetch(rule->src_nodes));
759 
760 	nvlist_add_number(nvl, "return_icmp", rule->return_icmp);
761 	nvlist_add_number(nvl, "return_icmp6", rule->return_icmp6);
762 
763 	nvlist_add_number(nvl, "max_mss", rule->max_mss);
764 	nvlist_add_number(nvl, "scrub_flags", rule->scrub_flags);
765 
766 	tmp = pf_rule_uid_to_nvrule_uid(&rule->uid);
767 	if (tmp == NULL)
768 		goto error;
769 	nvlist_add_nvlist(nvl, "uid", tmp);
770 	nvlist_destroy(tmp);
771 	tmp = pf_rule_uid_to_nvrule_uid((const struct pf_rule_uid *)&rule->gid);
772 	if (tmp == NULL)
773 		goto error;
774 	nvlist_add_nvlist(nvl, "gid", tmp);
775 	nvlist_destroy(tmp);
776 
777 	nvlist_add_number(nvl, "rule_flag", rule->rule_flag);
778 	nvlist_add_number(nvl, "action", rule->action);
779 	nvlist_add_number(nvl, "direction", rule->direction);
780 	nvlist_add_number(nvl, "log", rule->log);
781 	nvlist_add_number(nvl, "logif", rule->logif);
782 	nvlist_add_number(nvl, "quick", rule->quick);
783 	nvlist_add_number(nvl, "ifnot", rule->ifnot);
784 	nvlist_add_number(nvl, "match_tag_not", rule->match_tag_not);
785 	nvlist_add_number(nvl, "natpass", rule->natpass);
786 
787 	nvlist_add_number(nvl, "keep_state", rule->keep_state);
788 	nvlist_add_number(nvl, "af", rule->af);
789 	nvlist_add_number(nvl, "proto", rule->proto);
790 	nvlist_add_number(nvl, "type", rule->type);
791 	nvlist_add_number(nvl, "code", rule->code);
792 	nvlist_add_number(nvl, "flags", rule->flags);
793 	nvlist_add_number(nvl, "flagset", rule->flagset);
794 	nvlist_add_number(nvl, "min_ttl", rule->min_ttl);
795 	nvlist_add_number(nvl, "allow_opts", rule->allow_opts);
796 	nvlist_add_number(nvl, "rt", rule->rt);
797 	nvlist_add_number(nvl, "return_ttl", rule->return_ttl);
798 	nvlist_add_number(nvl, "tos", rule->tos);
799 	nvlist_add_number(nvl, "set_tos", rule->set_tos);
800 	nvlist_add_number(nvl, "anchor_relative", rule->anchor_relative);
801 	nvlist_add_number(nvl, "anchor_wildcard", rule->anchor_wildcard);
802 
803 	nvlist_add_number(nvl, "flush", rule->flush);
804 	nvlist_add_number(nvl, "prio", rule->prio);
805 
806 	pf_uint8_array_nv(nvl, "set_prio", rule->set_prio, 2);
807 
808 	tmp = pf_divert_to_nvdivert(rule);
809 	if (tmp == NULL)
810 		goto error;
811 	nvlist_add_nvlist(nvl, "divert", tmp);
812 	nvlist_destroy(tmp);
813 
814 	return (nvl);
815 
816 error:
817 	nvlist_destroy(nvl);
818 	return (NULL);
819 }
820 
821 static int
pf_nvstate_cmp_to_state_cmp(const nvlist_t * nvl,struct pf_state_cmp * cmp)822 pf_nvstate_cmp_to_state_cmp(const nvlist_t *nvl, struct pf_state_cmp *cmp)
823 {
824 	int error = 0;
825 
826 	bzero(cmp, sizeof(*cmp));
827 
828 	PFNV_CHK(pf_nvuint64(nvl, "id", &cmp->id));
829 	PFNV_CHK(pf_nvuint32(nvl, "creatorid", &cmp->creatorid));
830 	PFNV_CHK(pf_nvuint8(nvl, "direction", &cmp->direction));
831 
832 errout:
833 	return (error);
834 }
835 
836 int
pf_nvstate_kill_to_kstate_kill(const nvlist_t * nvl,struct pf_kstate_kill * kill)837 pf_nvstate_kill_to_kstate_kill(const nvlist_t *nvl,
838     struct pf_kstate_kill *kill)
839 {
840 	int error = 0;
841 
842 	bzero(kill, sizeof(*kill));
843 
844 	if (! nvlist_exists_nvlist(nvl, "cmp"))
845 		return (EINVAL);
846 
847 	PFNV_CHK(pf_nvstate_cmp_to_state_cmp(nvlist_get_nvlist(nvl, "cmp"),
848 	    &kill->psk_pfcmp));
849 	PFNV_CHK(pf_nvuint8(nvl, "af", &kill->psk_af));
850 	PFNV_CHK(pf_nvint(nvl, "proto", &kill->psk_proto));
851 
852 	if (! nvlist_exists_nvlist(nvl, "src"))
853 		return (EINVAL);
854 	PFNV_CHK(pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "src"),
855 	    &kill->psk_src));
856 	if (! nvlist_exists_nvlist(nvl, "dst"))
857 		return (EINVAL);
858 	PFNV_CHK(pf_nvrule_addr_to_rule_addr(nvlist_get_nvlist(nvl, "dst"),
859 	    &kill->psk_dst));
860 	if (nvlist_exists_nvlist(nvl, "rt_addr")) {
861 		PFNV_CHK(pf_nvrule_addr_to_rule_addr(
862 		    nvlist_get_nvlist(nvl, "rt_addr"), &kill->psk_rt_addr));
863 	}
864 
865 	PFNV_CHK(pf_nvstring(nvl, "ifname", kill->psk_ifname,
866 	    sizeof(kill->psk_ifname)));
867 	PFNV_CHK(pf_nvstring(nvl, "label", kill->psk_label,
868 	    sizeof(kill->psk_label)));
869 	PFNV_CHK(pf_nvbool(nvl, "kill_match", &kill->psk_kill_match));
870 
871 errout:
872 	return (error);
873 }
874 
875 static nvlist_t *
pf_state_key_to_nvstate_key(const struct pf_state_key * key)876 pf_state_key_to_nvstate_key(const struct pf_state_key *key)
877 {
878 	nvlist_t	*nvl, *tmp;
879 
880 	nvl = nvlist_create(0);
881 	if (nvl == NULL)
882 		return (NULL);
883 
884 	for (int i = 0; i < 2; i++) {
885 		tmp = pf_addr_to_nvaddr(&key->addr[i]);
886 		if (tmp == NULL)
887 			goto errout;
888 		nvlist_append_nvlist_array(nvl, "addr", tmp);
889 		nvlist_destroy(tmp);
890 		nvlist_append_number_array(nvl, "port", key->port[i]);
891 	}
892 	nvlist_add_number(nvl, "af", key->af);
893 	nvlist_add_number(nvl, "proto", key->proto);
894 
895 	return (nvl);
896 
897 errout:
898 	nvlist_destroy(nvl);
899 	return (NULL);
900 }
901 
902 static nvlist_t *
pf_state_peer_to_nvstate_peer(const struct pf_state_peer * peer)903 pf_state_peer_to_nvstate_peer(const struct pf_state_peer *peer)
904 {
905 	nvlist_t *nvl;
906 
907 	nvl = nvlist_create(0);
908 	if (nvl == NULL)
909 		return (NULL);
910 
911 	nvlist_add_number(nvl, "seqlo", peer->seqlo);
912 	nvlist_add_number(nvl, "seqhi", peer->seqhi);
913 	nvlist_add_number(nvl, "seqdiff", peer->seqdiff);
914 	nvlist_add_number(nvl, "state", peer->state);
915 	nvlist_add_number(nvl, "wscale", peer->wscale);
916 
917 	return (nvl);
918 }
919 
920 nvlist_t *
pf_state_to_nvstate(const struct pf_kstate * s)921 pf_state_to_nvstate(const struct pf_kstate *s)
922 {
923 	nvlist_t	*nvl, *tmp;
924 	uint32_t	 expire, flags = 0;
925 
926 	nvl = nvlist_create(0);
927 	if (nvl == NULL)
928 		return (NULL);
929 
930 	nvlist_add_number(nvl, "id", s->id);
931 	nvlist_add_string(nvl, "ifname", s->kif->pfik_name);
932 	nvlist_add_string(nvl, "orig_ifname", s->orig_kif->pfik_name);
933 
934 	tmp = pf_state_key_to_nvstate_key(s->key[PF_SK_STACK]);
935 	if (tmp == NULL)
936 		goto errout;
937 	nvlist_add_nvlist(nvl, "stack_key", tmp);
938 	nvlist_destroy(tmp);
939 
940 	tmp = pf_state_key_to_nvstate_key(s->key[PF_SK_WIRE]);
941 	if (tmp == NULL)
942 		goto errout;
943 	nvlist_add_nvlist(nvl, "wire_key", tmp);
944 	nvlist_destroy(tmp);
945 
946 	tmp = pf_state_peer_to_nvstate_peer(&s->src);
947 	if (tmp == NULL)
948 		goto errout;
949 	nvlist_add_nvlist(nvl, "src", tmp);
950 	nvlist_destroy(tmp);
951 
952 	tmp = pf_state_peer_to_nvstate_peer(&s->dst);
953 	if (tmp == NULL)
954 		goto errout;
955 	nvlist_add_nvlist(nvl, "dst", tmp);
956 	nvlist_destroy(tmp);
957 
958 	tmp = pf_addr_to_nvaddr(&s->rt_addr);
959 	if (tmp == NULL)
960 		goto errout;
961 	nvlist_add_nvlist(nvl, "rt_addr", tmp);
962 	nvlist_destroy(tmp);
963 
964 	nvlist_add_number(nvl, "rule", s->rule.ptr ? s->rule.ptr->nr : -1);
965 	nvlist_add_number(nvl, "anchor",
966 	    s->anchor.ptr ? s->anchor.ptr->nr : -1);
967 	nvlist_add_number(nvl, "nat_rule",
968 	    s->nat_rule.ptr ? s->nat_rule.ptr->nr : -1);
969 	nvlist_add_number(nvl, "creation", s->creation);
970 
971 	expire = pf_state_expires(s);
972 	if (expire <= time_uptime)
973 		expire = 0;
974 	else
975 		expire = expire - time_uptime;
976 	nvlist_add_number(nvl, "expire", expire);
977 
978 	for (int i = 0; i < 2; i++) {
979 		nvlist_append_number_array(nvl, "packets",
980 		    s->packets[i]);
981 		nvlist_append_number_array(nvl, "bytes",
982 		    s->bytes[i]);
983 	}
984 
985 	nvlist_add_number(nvl, "creatorid", s->creatorid);
986 	nvlist_add_number(nvl, "direction", s->direction);
987 	nvlist_add_number(nvl, "state_flags", s->state_flags);
988 	if (s->src_node)
989 		flags |= PFSYNC_FLAG_SRCNODE;
990 	if (s->nat_src_node)
991 		flags |= PFSYNC_FLAG_NATSRCNODE;
992 	nvlist_add_number(nvl, "sync_flags", flags);
993 
994 	return (nvl);
995 
996 errout:
997 	nvlist_destroy(nvl);
998 	return (NULL);
999 }
1000