Home | History | Annotate | Line # | Download | only in libnpf
      1 /*-
      2  * Copyright (c) 2011-2025 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This material is based upon work partially supported by The
      6  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     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
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     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
     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
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #ifndef _NPF_LIB_H_
     31 #define _NPF_LIB_H_
     32 
     33 #include <sys/types.h>
     34 #include <net/npf.h>
     35 
     36 __BEGIN_DECLS
     37 
     38 struct nl_config;
     39 struct nl_rule;
     40 struct nl_rproc;
     41 struct nl_table;
     42 struct nl_ext;
     43 
     44 typedef struct nl_config	nl_config_t;
     45 typedef struct nl_rule		nl_rule_t;
     46 typedef struct nl_rproc		nl_rproc_t;
     47 typedef struct nl_table		nl_table_t;
     48 typedef struct nl_rule		nl_nat_t;
     49 typedef struct nl_ext		nl_ext_t;
     50 
     51 /*
     52  * Iterator.
     53  */
     54 #define	NPF_ITER_BEGIN		0
     55 
     56 typedef signed long		nl_iter_t;
     57 
     58 /*
     59  * Ruleset prefix(es).
     60  */
     61 
     62 #define	NPF_RULESET_MAP_PREF	"map:"
     63 
     64 /*
     65  * Extensions API types.
     66  */
     67 typedef int (*npfext_initfunc_t)(void);
     68 typedef nl_ext_t *(*npfext_consfunc_t)(const char *);
     69 typedef int (*npfext_paramfunc_t)(nl_ext_t *, const char *, const char *);
     70 
     71 typedef int (*npf_conn_func_t)(unsigned, const npf_addr_t *,
     72     const in_port_t *, const char *, void *);
     73 
     74 /*
     75  * API functions.
     76  */
     77 
     78 nl_config_t *	npf_config_create(void);
     79 void		npf_config_destroy(nl_config_t *);
     80 int		npf_config_submit(nl_config_t *, int, npf_error_t *);
     81 nl_config_t *	npf_config_retrieve(int);
     82 int		npf_config_flush(int);
     83 nl_config_t *	npf_config_import(const void *, size_t);
     84 void *		npf_config_export(nl_config_t *, size_t *);
     85 bool		npf_config_active_p(nl_config_t *);
     86 bool		npf_config_loaded_p(nl_config_t *);
     87 const void *	npf_config_build(nl_config_t *);
     88 
     89 int		npf_alg_load(nl_config_t *, const char *);
     90 
     91 int		npf_param_get(nl_config_t *, const char *, int *);
     92 int		npf_param_set(nl_config_t *, const char *, int);
     93 const char *	npf_param_iterate(nl_config_t *, nl_iter_t *, int *, int *);
     94 
     95 int		npf_ruleset_add(int, const char *, nl_rule_t *, uint64_t *);
     96 int		npf_ruleset_remove(int, const char *, uint64_t);
     97 int		npf_ruleset_remkey(int, const char *, const void *, size_t);
     98 int		npf_ruleset_flush(int, const char *);
     99 
    100 nl_ext_t *	npf_ext_construct(const char *);
    101 void		npf_ext_param_u32(nl_ext_t *, const char *, uint32_t);
    102 void		npf_ext_param_bool(nl_ext_t *, const char *, bool);
    103 void		npf_ext_param_string(nl_ext_t *, const char *, const char *);
    104 
    105 nl_rule_t *	npf_rule_create(const char *, uint32_t, const char *);
    106 int		npf_rule_setcode(nl_rule_t *, int, const void *, size_t);
    107 int		npf_rule_setprio(nl_rule_t *, int);
    108 int		npf_rule_setproc(nl_rule_t *, const char *);
    109 int		npf_rule_setrid(nl_rule_t *, struct r_id, const char *);
    110 int		npf_rule_setkey(nl_rule_t *, const void *, size_t);
    111 int		npf_rule_setinfo(nl_rule_t *, const void *, size_t);
    112 const char *	npf_rule_getname(nl_rule_t *);
    113 uint32_t	npf_rule_getattr(nl_rule_t *);
    114 const char *	npf_rule_getinterface(nl_rule_t *);
    115 const void *	npf_rule_getinfo(nl_rule_t *, size_t *);
    116 const char *	npf_rule_getproc(nl_rule_t *);
    117 int		npf_rule_getrid(struct r_id *, nl_rule_t *, const char *);
    118 uint64_t	npf_rule_getid(nl_rule_t *);
    119 const void *	npf_rule_getcode(nl_rule_t *, int *, size_t *);
    120 bool		npf_rule_exists_p(nl_config_t *, const char *);
    121 int		npf_rule_insert(nl_config_t *, nl_rule_t *, nl_rule_t *);
    122 void *		npf_rule_export(nl_rule_t *, size_t *);
    123 void		npf_rule_destroy(nl_rule_t *);
    124 
    125 nl_rproc_t *	npf_rproc_create(const char *);
    126 int		npf_rproc_extcall(nl_rproc_t *, nl_ext_t *);
    127 bool		npf_rproc_exists_p(nl_config_t *, const char *);
    128 int		npf_rproc_insert(nl_config_t *, nl_rproc_t *);
    129 const char *	npf_rproc_getname(nl_rproc_t *);
    130 
    131 nl_nat_t *	npf_nat_create(int, unsigned, const char *);
    132 int		npf_nat_setaddr(nl_nat_t *, int, npf_addr_t *, npf_netmask_t);
    133 int		npf_nat_setport(nl_nat_t *, in_port_t);
    134 int		npf_nat_settable(nl_nat_t *, unsigned);
    135 int		npf_nat_settablefilter(nl_nat_t *, int, npf_addr_t *, npf_netmask_t);
    136 int		npf_nat_setalgo(nl_nat_t *, unsigned);
    137 int		npf_nat_setnpt66(nl_nat_t *, uint16_t);
    138 int		npf_nat_gettype(nl_nat_t *);
    139 unsigned	npf_nat_getflags(nl_nat_t *);
    140 const npf_addr_t *npf_nat_getaddr(nl_nat_t *, size_t *, npf_netmask_t *);
    141 in_port_t	npf_nat_getport(nl_nat_t *);
    142 unsigned	npf_nat_gettable(nl_nat_t *);
    143 unsigned	npf_nat_getalgo(nl_nat_t *);
    144 int		npf_nat_insert(nl_config_t *, nl_nat_t *);
    145 int		npf_nat_lookup(int, int, npf_addr_t *[2], in_port_t [2], int, int);
    146 
    147 int		npf_conn_list(int, npf_conn_func_t, void *);
    148 
    149 nl_table_t *	npf_table_create(const char *, unsigned, int);
    150 const char *	npf_table_getname(nl_table_t *);
    151 unsigned	npf_table_getid(nl_table_t *);
    152 int		npf_table_gettype(nl_table_t *);
    153 int		npf_table_add_entry(nl_table_t *, int,
    154 		    const npf_addr_t *, const npf_netmask_t);
    155 int		npf_table_insert(nl_config_t *, nl_table_t *);
    156 void		npf_table_destroy(nl_table_t *);
    157 
    158 int		npf_table_replace(int, nl_table_t *, npf_error_t *);
    159 
    160 #ifdef _NPF_PRIVATE
    161 
    162 #include <ifaddrs.h>
    163 
    164 nl_rule_t *	npf_rule_iterate(nl_config_t *, nl_iter_t *, unsigned *);
    165 nl_nat_t *	npf_nat_iterate(nl_config_t *, nl_iter_t *);
    166 nl_rproc_t *	npf_rproc_iterate(nl_config_t *, nl_iter_t *);
    167 nl_table_t *	npf_table_iterate(nl_config_t *, nl_iter_t *);
    168 
    169 int		_npf_ruleset_list(int, const char *, nl_config_t *);
    170 void		_npf_debug_addif(nl_config_t *, const char *);
    171 void		_npf_config_dump(nl_config_t *, int);
    172 
    173 #endif
    174 
    175 __END_DECLS
    176 
    177 #endif	/* _NPF_LIB_H_ */
    178