]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netpfil/pf/pf_nv.c
Merge commit 'bd136720030ebb0b31e6d5a2236b9d0ddac71b94'
[FreeBSD/FreeBSD.git] / sys / netpfil / pf / pf_nv.c
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 <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/limits.h>
34 #include <sys/systm.h>
35
36 #include <netpfil/pf/pf_nv.h>
37
38 #define PV_NV_IMPL_UINT(fnname, type, max)                                      \
39         int                                                                     \
40         pf_nv ## fnname(const nvlist_t *nvl, const char *name, type *val)       \
41         {                                                                       \
42                 uint64_t raw;                                                   \
43                 if (! nvlist_exists_number(nvl, name))                          \
44                         return (EINVAL);                                        \
45                 raw = nvlist_get_number(nvl, name);                             \
46                 if (raw > max)                                                  \
47                         return (ERANGE);                                        \
48                 *val = (type)raw;                                               \
49                 return (0);                                                     \
50         }                                                                       \
51         int                                                                     \
52         pf_nv ## fnname ## _array(const nvlist_t *nvl, const char *name,        \
53             type *array, size_t maxelems, size_t *nelems)                       \
54         {                                                                       \
55                 const uint64_t *n;                                              \
56                 size_t nitems;                                                  \
57                 bzero(array, sizeof(type) * maxelems);                          \
58                 if (! nvlist_exists_number_array(nvl, name))                    \
59                         return (EINVAL);                                        \
60                 n = nvlist_get_number_array(nvl, name, &nitems);                \
61                 if (nitems != maxelems)                                         \
62                         return (E2BIG);                                         \
63                 if (nelems != NULL)                                             \
64                         *nelems = nitems;                                       \
65                 for (size_t i = 0; i < nitems; i++) {                           \
66                         if (n[i] > max)                                         \
67                                 return (ERANGE);                                \
68                         array[i] = (type)n[i];                                  \
69                 }                                                               \
70                 return (0);                                                     \
71         }                                                                       \
72         void                                                                    \
73         pf_ ## fnname ## _array_nv(nvlist_t *nvl, const char *name,             \
74             const type *numbers, size_t count)                                  \
75         {                                                                       \
76                 uint64_t tmp;                                                   \
77                 for (size_t i = 0; i < count; i++) {                            \
78                         tmp = numbers[i];                                       \
79                         nvlist_append_number_array(nvl, name, tmp);             \
80                 }                                                               \
81         }
82
83 int
84 pf_nvbinary(const nvlist_t *nvl, const char *name, void *data,
85     size_t expected_size)
86 {
87         const uint8_t *nvdata;
88         size_t len;
89
90         bzero(data, expected_size);
91
92         if (! nvlist_exists_binary(nvl, name))
93                 return (EINVAL);
94
95         nvdata = (const uint8_t *)nvlist_get_binary(nvl, name, &len);
96         if (len > expected_size)
97                 return (EINVAL);
98
99         memcpy(data, nvdata, len);
100
101         return (0);
102 }
103
104 PV_NV_IMPL_UINT(uint8, uint8_t, UINT8_MAX)
105 PV_NV_IMPL_UINT(uint16, uint16_t, UINT16_MAX);
106 PV_NV_IMPL_UINT(uint32, uint32_t, UINT32_MAX)
107
108 int
109 pf_nvint(const nvlist_t *nvl, const char *name, int *val)
110 {
111         int64_t raw;
112
113         if (! nvlist_exists_number(nvl, name))
114                 return (EINVAL);
115
116         raw = nvlist_get_number(nvl, name);
117         if (raw > INT_MAX || raw < INT_MIN)
118                 return (ERANGE);
119
120         *val = (int)raw;
121
122         return (0);
123 }
124
125 int
126 pf_nvstring(const nvlist_t *nvl, const char *name, char *str, size_t maxlen)
127 {
128         int ret;
129
130         if (! nvlist_exists_string(nvl, name))
131                 return (EINVAL);
132
133         ret = strlcpy(str, nvlist_get_string(nvl, name), maxlen);
134         if (ret >= maxlen)
135                 return (EINVAL);
136
137         return (0);
138 }