Tue Jun 7 16:27:24 2022 UTC ()
Add method to initialize errinfo so that npfctl does not print random strings.


(christos)
diff -r1.49 -r1.50 src/lib/libnpf/npf.c

cvs diff -r1.49 -r1.50 src/lib/libnpf/npf.c (expand / switch to unified diff)

--- src/lib/libnpf/npf.c 2020/05/30 14:16:56 1.49
+++ src/lib/libnpf/npf.c 2022/06/07 16:27:24 1.50
@@ -18,27 +18,27 @@ @@ -18,27 +18,27 @@
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE. 27 * POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29 29
30#include <sys/cdefs.h> 30#include <sys/cdefs.h>
31__KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.49 2020/05/30 14:16:56 rmind Exp $"); 31__KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.50 2022/06/07 16:27:24 christos Exp $");
32 32
33#include <sys/types.h> 33#include <sys/types.h>
34#include <sys/mman.h> 34#include <sys/mman.h>
35#include <sys/stat.h> 35#include <sys/stat.h>
36#if !defined(_NPF_STANDALONE) 36#if !defined(_NPF_STANDALONE)
37#include <sys/ioctl.h> 37#include <sys/ioctl.h>
38#endif 38#endif
39#include <netinet/in_systm.h> 39#include <netinet/in_systm.h>
40#include <netinet/in.h> 40#include <netinet/in.h>
41#include <net/if.h> 41#include <net/if.h>
42 42
43#include <stdlib.h> 43#include <stdlib.h>
44#include <string.h> 44#include <string.h>
@@ -196,26 +196,40 @@ _npf_rules_process(nl_config_t *ncf, nvl @@ -196,26 +196,40 @@ _npf_rules_process(nl_config_t *ncf, nvl
196 if (nvlist_exists_nvlist_array(rule_dict, "subrules")) { 196 if (nvlist_exists_nvlist_array(rule_dict, "subrules")) {
197 unsigned idx; 197 unsigned idx;
198 198
199 _npf_rules_process(ncf, rule_dict, "subrules"); 199 _npf_rules_process(ncf, rule_dict, "subrules");
200 idx = ncf->ncf_rule_count; // post-recursion index 200 idx = ncf->ncf_rule_count; // post-recursion index
201 nvlist_add_number(rule_dict, "skip-to", idx); 201 nvlist_add_number(rule_dict, "skip-to", idx);
202 } 202 }
203 assert(nvlist_error(rule_dict) == 0); 203 assert(nvlist_error(rule_dict) == 0);
204 } 204 }
205 free(items); 205 free(items);
206} 206}
207 207
208/* 208/*
 209 * _npf_init_error: initialize the error structure with the message
 210 * from the current error number
 211 */
 212static int
 213_npf_init_error(int error, npf_error_t *errinfo)
 214{
 215 if (error && errinfo) {
 216 memset(errinfo, 0, sizeof(*errinfo));
 217 errinfo->error_msg = strerror(error);
 218 }
 219 return error;
 220}
 221
 222/*
209 * _npf_extract_error: check the error number field and extract the 223 * _npf_extract_error: check the error number field and extract the
210 * error details into the npf_error_t structure. 224 * error details into the npf_error_t structure.
211 */ 225 */
212static int 226static int
213_npf_extract_error(nvlist_t *resp, npf_error_t *errinfo) 227_npf_extract_error(nvlist_t *resp, npf_error_t *errinfo)
214{ 228{
215 int error; 229 int error;
216 230
217 error = dnvlist_get_number(resp, "errno", 0); 231 error = dnvlist_get_number(resp, "errno", 0);
218 if (error && errinfo) { 232 if (error && errinfo) {
219 memset(errinfo, 0, sizeof(npf_error_t)); 233 memset(errinfo, 0, sizeof(npf_error_t));
220 234
221 errinfo->id = dnvlist_get_number(resp, "id", 0); 235 errinfo->id = dnvlist_get_number(resp, "id", 0);
@@ -336,27 +350,27 @@ npf_config_create(void) @@ -336,27 +350,27 @@ npf_config_create(void)
336} 350}
337 351
338int 352int
339npf_config_submit(nl_config_t *ncf, int fd, npf_error_t *errinfo) 353npf_config_submit(nl_config_t *ncf, int fd, npf_error_t *errinfo)
340{ 354{
341 nvlist_t *resp = NULL; 355 nvlist_t *resp = NULL;
342 int error; 356 int error;
343 357
344 /* Ensure the config is built. */ 358 /* Ensure the config is built. */
345 (void)npf_config_build(ncf); 359 (void)npf_config_build(ncf);
346 360
347 error = _npf_xfer_fd(fd, IOC_NPF_LOAD, ncf->ncf_dict, &resp); 361 error = _npf_xfer_fd(fd, IOC_NPF_LOAD, ncf->ncf_dict, &resp);
348 if (error) { 362 if (error) {
349 return error; 363 return _npf_init_error(errno, errinfo);
350 } 364 }
351 error = _npf_extract_error(resp, errinfo); 365 error = _npf_extract_error(resp, errinfo);
352 nvlist_destroy(resp); 366 nvlist_destroy(resp);
353 return error; 367 return error;
354} 368}
355 369
356nl_config_t * 370nl_config_t *
357npf_config_retrieve(int fd) 371npf_config_retrieve(int fd)
358{ 372{
359 nl_config_t *ncf; 373 nl_config_t *ncf;
360 nvlist_t *req, *resp = NULL; 374 nvlist_t *req, *resp = NULL;
361 int error; 375 int error;
362 376
@@ -1248,32 +1262,32 @@ npf_table_insert(nl_config_t *ncf, nl_ta @@ -1248,32 +1262,32 @@ npf_table_insert(nl_config_t *ncf, nl_ta
1248 nvlist_destroy(tl->table_dict); 1262 nvlist_destroy(tl->table_dict);
1249 free(tl); 1263 free(tl);
1250 return 0; 1264 return 0;
1251} 1265}
1252 1266
1253int 1267int
1254npf_table_replace(int fd, nl_table_t *tl, npf_error_t *errinfo) 1268npf_table_replace(int fd, nl_table_t *tl, npf_error_t *errinfo)
1255{ 1269{
1256 nvlist_t *resp = NULL; 1270 nvlist_t *resp = NULL;
1257 int error; 1271 int error;
1258 1272
1259 /* Ensure const tables are built. */ 1273 /* Ensure const tables are built. */
1260 if ((error = _npf_table_build_const(tl)) != 0) { 1274 if ((error = _npf_table_build_const(tl)) != 0) {
1261 return error; 1275 return _npf_init_error(errno, errinfo);
1262 } 1276 }
1263 error = _npf_xfer_fd(fd, IOC_NPF_TABLE_REPLACE, tl->table_dict, &resp); 1277 error = _npf_xfer_fd(fd, IOC_NPF_TABLE_REPLACE, tl->table_dict, &resp);
1264 if (error) { 1278 if (error) {
1265 assert(resp == NULL); 1279 assert(resp == NULL);
1266 return errno; 1280 return _npf_init_error(errno, errinfo);
1267 } 1281 }
1268 error = _npf_extract_error(resp, errinfo); 1282 error = _npf_extract_error(resp, errinfo);
1269 nvlist_destroy(resp); 1283 nvlist_destroy(resp);
1270 return error; 1284 return error;
1271} 1285}
1272 1286
1273nl_table_t * 1287nl_table_t *
1274npf_table_iterate(nl_config_t *ncf, nl_iter_t *iter) 1288npf_table_iterate(nl_config_t *ncf, nl_iter_t *iter)
1275{ 1289{
1276 const nvlist_t *table_dict; 1290 const nvlist_t *table_dict;
1277 unsigned i = *iter; 1291 unsigned i = *iter;
1278 1292
1279 table_dict = _npf_dataset_getelement(ncf->ncf_dict, "tables", i); 1293 table_dict = _npf_dataset_getelement(ncf->ncf_dict, "tables", i);