]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netpfil/pf/pf_nv.c
tcsh: update to version 6.22.04.
[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 PF_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 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);
107 PF_NV_IMPL_UINT(uint64, uint64_t, UINT64_MAX);
108
109 int
110 pf_nvint(const nvlist_t *nvl, const char *name, int *val)
111 {
112         int64_t raw;
113
114         if (! nvlist_exists_number(nvl, name))
115                 return (EINVAL);
116
117         raw = nvlist_get_number(nvl, name);
118         if (raw > INT_MAX || raw < INT_MIN)
119                 return (ERANGE);
120
121         *val = (int)raw;
122
123         return (0);
124 }
125
126 int
127 pf_nvstring(const nvlist_t *nvl, const char *name, char *str, size_t maxlen)
128 {
129         int ret;
130
131         if (! nvlist_exists_string(nvl, name))
132                 return (EINVAL);
133
134         ret = strlcpy(str, nvlist_get_string(nvl, name), maxlen);
135         if (ret >= maxlen)
136                 return (EINVAL);
137
138         return (0);
139 }