]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netpfil/pf/pf_nv.c
pf: Introduce nvlist variant of DIOCADDRULE
[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         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         fnname ## _array(const nvlist_t *nvl, const char *name, type *array,    \
53             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 int
73 pf_nvbinary(const nvlist_t *nvl, const char *name, void *data,
74     size_t expected_size)
75 {
76         const uint8_t *nvdata;
77         size_t len;
78
79         bzero(data, expected_size);
80
81         if (! nvlist_exists_binary(nvl, name))
82                 return (EINVAL);
83
84         nvdata = (const uint8_t *)nvlist_get_binary(nvl, name, &len);
85         if (len > expected_size)
86                 return (EINVAL);
87
88         memcpy(data, nvdata, len);
89
90         return (0);
91 }
92
93 PV_NV_IMPL_UINT(pf_nvuint8, uint8_t, UINT8_MAX)
94 PV_NV_IMPL_UINT(pf_nvuint16, uint16_t, UINT16_MAX);
95 PV_NV_IMPL_UINT(pf_nvuint32, uint32_t, UINT32_MAX)
96
97 int
98 pf_nvint(const nvlist_t *nvl, const char *name, int *val)
99 {
100         int64_t raw;
101
102         if (! nvlist_exists_number(nvl, name))
103                 return (EINVAL);
104
105         raw = nvlist_get_number(nvl, name);
106         if (raw > INT_MAX || raw < INT_MIN)
107                 return (ERANGE);
108
109         *val = (int)raw;
110
111         return (0);
112 }
113
114 int
115 pf_nvstring(const nvlist_t *nvl, const char *name, char *str, size_t maxlen)
116 {
117         int ret;
118
119         if (! nvlist_exists_string(nvl, name))
120                 return (EINVAL);
121
122         ret = strlcpy(str, nvlist_get_string(nvl, name), maxlen);
123         if (ret >= maxlen)
124                 return (EINVAL);
125
126         return (0);
127 }