117680Spst/*-
217680Spst * SPDX-License-Identifier: BSD-2-Clause
317680Spst *
417680Spst * Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
517680Spst *
617680Spst * Redistribution and use in source and binary forms, with or without
717680Spst * modification, are permitted provided that the following conditions
817680Spst * are met:
917680Spst * 1. Redistributions of source code must retain the above copyright
1017680Spst *    notice, this list of conditions and the following disclaimer.
1117680Spst * 2. Redistributions in binary form must reproduce the above copyright
1217680Spst *    notice, this list of conditions and the following disclaimer in the
1317680Spst *    documentation and/or other materials provided with the distribution.
1417680Spst *
1517680Spst * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1617680Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1717680Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1817680Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1917680Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2017680Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2117680Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2217680Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2317680Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2417680Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2517680Spst * SUCH DAMAGE.
2617680Spst *
2717680Spst */
2817680Spst#ifndef _PF_NV_H_
2917680Spst#define _PF_NV_H_
3017680Spst
3117680Spst#include <sys/nv.h>
3217680Spst#include <sys/sdt.h>
3317680Spst#include <sys/socket.h>
3417680Spst#include <sys/types.h>
3517680Spst
3617680Spst#include <net/if.h>
3717680Spst#include <net/if_var.h>
3817680Spst#include <net/pfvar.h>
3917680Spst
4017680SpstSDT_PROBE_DECLARE(pf, ioctl, function, error);
4117680SpstSDT_PROBE_DECLARE(pf, ioctl, nvchk, error);
4217680Spst
4317680Spst#define	ERROUT_FUNCTION(target, x)					\
4417680Spst	do {								\
4517680Spst		error = (x);						\
4617680Spst		SDT_PROBE3(pf, ioctl, function, error, __func__, error,	\
4717680Spst		    __LINE__);						\
4817680Spst		goto target;						\
4917680Spst	} while (0)
5017680Spst
5117680Spst#define	PFNV_CHK(x)	do {	\
5217680Spst	error = (x);		\
5317680Spst	SDT_PROBE2(pf, ioctl, nvchk, error, error, __LINE__);	\
5417680Spst	if (error != 0)		\
5517680Spst		goto errout;	\
5617680Spst	} while (0)
5717680Spst
5817680Spst#define PF_NV_DEF_UINT(fnname, type, max)				\
5917680Spst	int pf_nv ## fnname ## _opt(const nvlist_t *, const char *,	\
6017680Spst	    type *, type);						\
6117680Spst	int pf_nv ## fnname(const nvlist_t *, const char *, type *);	\
6217680Spst	int pf_nv ## fnname ## _array(const nvlist_t *, const char *,	\
6317680Spst	    type *,size_t, size_t *);					\
6417680Spst	void pf_ ## fnname ## _array_nv(nvlist_t *, const char *,	\
6517680Spst	    const type *, size_t);
6617680Spst
6717680SpstPF_NV_DEF_UINT(uint8, uint8_t, UINT8_MAX);
6817680SpstPF_NV_DEF_UINT(uint16, uint16_t, UINT16_MAX);
6917680SpstPF_NV_DEF_UINT(uint32, uint32_t, UINT32_MAX);
7017680SpstPF_NV_DEF_UINT(uint64, uint64_t, UINT64_MAX);
7117680Spst
7217680Spstint	pf_nvbool(const nvlist_t *, const char *, bool *);
7317680Spstint	pf_nvbinary(const nvlist_t *, const char *, void *, size_t);
7417680Spstint	pf_nvint(const nvlist_t *, const char *, int *);
7517680Spstint	pf_nvstring(const nvlist_t *, const char *, char *, size_t);
7617680Spst
7717680Spst/* Translation functions */
7817680Spst
7917680Spstint		 pf_check_rule_addr(const struct pf_rule_addr *);
8017680Spst
8117680Spstnvlist_t	*pf_krule_to_nvrule(struct pf_krule *);
8217680Spstint		 pf_nvrule_to_krule(const nvlist_t *, struct pf_krule *);
8317680Spstint		 pf_nvstate_kill_to_kstate_kill(const nvlist_t *,
8417680Spst		    struct pf_kstate_kill *);
8517680Spstnvlist_t	*pf_state_to_nvstate(const struct pf_kstate *);
8617680Spst
8717680Spstnvlist_t	*pf_keth_rule_to_nveth_rule(const struct pf_keth_rule *);
8817680Spstint		 pf_nveth_rule_to_keth_rule(const nvlist_t *, struct pf_keth_rule *);
8917680Spst#endif
9017680Spst