2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/limits.h>
34 #include <sys/systm.h>
36 #include <netpfil/pf/pf_nv.h>
38 #define PF_NV_IMPL_UINT(fnname, type, max) \
40 pf_nv ## fnname(const nvlist_t *nvl, const char *name, type *val) \
43 if (! nvlist_exists_number(nvl, name)) \
45 raw = nvlist_get_number(nvl, name); \
52 pf_nv ## fnname ## _array(const nvlist_t *nvl, const char *name, \
53 type *array, size_t maxelems, size_t *nelems) \
57 bzero(array, sizeof(type) * maxelems); \
58 if (! nvlist_exists_number_array(nvl, name)) \
60 n = nvlist_get_number_array(nvl, name, &nitems); \
61 if (nitems != maxelems) \
65 for (size_t i = 0; i < nitems; i++) { \
68 array[i] = (type)n[i]; \
73 pf_ ## fnname ## _array_nv(nvlist_t *nvl, const char *name, \
74 const type *numbers, size_t count) \
77 for (size_t i = 0; i < count; i++) { \
79 nvlist_append_number_array(nvl, name, tmp); \
84 pf_nvbinary(const nvlist_t *nvl, const char *name, void *data,
87 const uint8_t *nvdata;
90 bzero(data, expected_size);
92 if (! nvlist_exists_binary(nvl, name))
95 nvdata = (const uint8_t *)nvlist_get_binary(nvl, name, &len);
96 if (len > expected_size)
99 memcpy(data, nvdata, len);
104 PF_NV_IMPL_UINT(uint8, uint8_t, UINT8_MAX);
105 PF_NV_IMPL_UINT(uint16, uint16_t, UINT16_MAX);
106 PF_NV_IMPL_UINT(uint32, uint32_t, UINT32_MAX);
109 pf_nvint(const nvlist_t *nvl, const char *name, int *val)
113 if (! nvlist_exists_number(nvl, name))
116 raw = nvlist_get_number(nvl, name);
117 if (raw > INT_MAX || raw < INT_MIN)
126 pf_nvstring(const nvlist_t *nvl, const char *name, char *str, size_t maxlen)
130 if (! nvlist_exists_string(nvl, name))
133 ret = strlcpy(str, nvlist_get_string(nvl, name), maxlen);