npfctl.h revision 1.56 1 1.1 rmind /*-
2 1.55 joe * Copyright (c) 2009-2025 The NetBSD Foundation, Inc.
3 1.1 rmind * All rights reserved.
4 1.1 rmind *
5 1.25 rmind * This material is based upon work partially supported by The
6 1.25 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
7 1.25 rmind *
8 1.1 rmind * Redistribution and use in source and binary forms, with or without
9 1.1 rmind * modification, are permitted provided that the following conditions
10 1.1 rmind * are met:
11 1.1 rmind * 1. Redistributions of source code must retain the above copyright
12 1.1 rmind * notice, this list of conditions and the following disclaimer.
13 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer in the
15 1.1 rmind * documentation and/or other materials provided with the distribution.
16 1.1 rmind *
17 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
28 1.1 rmind */
29 1.1 rmind
30 1.1 rmind #ifndef _NPFCTL_H_
31 1.1 rmind #define _NPFCTL_H_
32 1.1 rmind
33 1.1 rmind #include <stdio.h>
34 1.1 rmind #include <stdbool.h>
35 1.8 rmind #include <inttypes.h>
36 1.17 rmind #include <assert.h>
37 1.22 rmind #include <util.h>
38 1.1 rmind
39 1.30 rmind #define NPF_BPFCOP
40 1.1 rmind #include <net/npf.h>
41 1.6 rmind
42 1.6 rmind #define _NPF_PRIVATE
43 1.6 rmind #include <npf.h>
44 1.1 rmind
45 1.8 rmind #include "npf_var.h"
46 1.1 rmind
47 1.1 rmind #define NPF_DEV_PATH "/dev/npf"
48 1.1 rmind #define NPF_CONF_PATH "/etc/npf.conf"
49 1.38 rmind #define NPF_DB_PATH "/var/db/npf.db"
50 1.1 rmind
51 1.8 rmind typedef struct fam_addr_mask {
52 1.8 rmind sa_family_t fam_family;
53 1.8 rmind npf_addr_t fam_addr;
54 1.8 rmind npf_netmask_t fam_mask;
55 1.23 rmind unsigned long fam_ifindex;
56 1.8 rmind } fam_addr_mask_t;
57 1.8 rmind
58 1.23 rmind typedef struct ifnet_addr {
59 1.34 rmind char * ifna_name;
60 1.23 rmind unsigned long ifna_index;
61 1.23 rmind sa_family_t ifna_family;
62 1.23 rmind npfvar_t * ifna_filter;
63 1.23 rmind npfvar_t * ifna_addrs;
64 1.23 rmind } ifnet_addr_t;
65 1.23 rmind
66 1.8 rmind typedef struct port_range {
67 1.8 rmind in_port_t pr_start;
68 1.8 rmind in_port_t pr_end;
69 1.8 rmind } port_range_t;
70 1.8 rmind
71 1.15 rmind typedef struct addr_port {
72 1.15 rmind npfvar_t * ap_netaddr;
73 1.15 rmind npfvar_t * ap_portrange;
74 1.15 rmind } addr_port_t;
75 1.15 rmind
76 1.56 joe typedef struct l3 {
77 1.15 rmind addr_port_t fo_from;
78 1.15 rmind addr_port_t fo_to;
79 1.56 joe } opt3;
80 1.56 joe
81 1.56 joe typedef struct l2 {
82 1.56 joe npfvar_t * from_mac;
83 1.56 joe npfvar_t * to_mac;
84 1.56 joe uint16_t ether_type;
85 1.56 joe } opt2;
86 1.56 joe
87 1.56 joe typedef struct filt_opts {
88 1.56 joe union {
89 1.56 joe opt3 opt3;
90 1.56 joe opt2 opt2;
91 1.56 joe } filt;
92 1.56 joe struct r_id uid;
93 1.56 joe struct r_id gid;
94 1.56 joe uint32_t layer;
95 1.42 rmind bool fo_finvert;
96 1.42 rmind bool fo_tinvert;
97 1.8 rmind } filt_opts_t;
98 1.8 rmind
99 1.8 rmind typedef struct opt_proto {
100 1.8 rmind int op_proto;
101 1.8 rmind npfvar_t * op_opts;
102 1.8 rmind } opt_proto_t;
103 1.8 rmind
104 1.8 rmind typedef struct rule_group {
105 1.8 rmind const char * rg_name;
106 1.14 rmind uint32_t rg_attr;
107 1.34 rmind const char * rg_ifname;
108 1.25 rmind bool rg_default;
109 1.8 rmind } rule_group_t;
110 1.8 rmind
111 1.20 rmind typedef struct proc_call {
112 1.20 rmind const char * pc_name;
113 1.20 rmind npfvar_t * pc_opts;
114 1.20 rmind } proc_call_t;
115 1.20 rmind
116 1.20 rmind typedef struct proc_param {
117 1.20 rmind const char * pp_param;
118 1.20 rmind const char * pp_value;
119 1.20 rmind } proc_param_t;
120 1.6 rmind
121 1.51 rmind typedef enum {
122 1.51 rmind NPFCTL_PARSE_DEFAULT,
123 1.51 rmind NPFCTL_PARSE_RULE,
124 1.51 rmind NPFCTL_PARSE_MAP
125 1.51 rmind } parse_entry_t;
126 1.25 rmind
127 1.47 rmind #define NPF_IFNET_TABLE_PREF ".ifnet-"
128 1.47 rmind #define NPF_IFNET_TABLE_PREFLEN (sizeof(NPF_IFNET_TABLE_PREF) - 1)
129 1.47 rmind
130 1.9 joerg void yyerror(const char *, ...) __printflike(1, 2) __dead;
131 1.39 christos void npfctl_bpfjit(bool);
132 1.25 rmind void npfctl_parse_file(const char *);
133 1.51 rmind void npfctl_parse_string(const char *, parse_entry_t);
134 1.8 rmind
135 1.53 rmind bool join(char *, size_t, int, char **, const char *);
136 1.53 rmind bool npfctl_addr_iszero(const npf_addr_t *);
137 1.53 rmind
138 1.40 christos void npfctl_print_error(const npf_error_t *);
139 1.41 christos char * npfctl_print_addrmask(int, const char *, const npf_addr_t *,
140 1.41 christos npf_netmask_t);
141 1.34 rmind void npfctl_note_interface(const char *);
142 1.50 rmind nl_table_t * npfctl_table_getbyname(nl_config_t *, const char *);
143 1.35 rmind unsigned npfctl_table_getid(const char *);
144 1.47 rmind const char * npfctl_table_getname(nl_config_t *, unsigned, bool *);
145 1.16 rmind int npfctl_protono(const char *);
146 1.8 rmind in_port_t npfctl_portno(const char *);
147 1.18 spz uint8_t npfctl_icmpcode(int, uint8_t, const char *);
148 1.18 spz uint8_t npfctl_icmptype(int, const char *);
149 1.43 rmind npfvar_t * npfctl_ifnet_table(const char *);
150 1.23 rmind npfvar_t * npfctl_parse_ifnet(const char *, const int);
151 1.8 rmind npfvar_t * npfctl_parse_tcpflag(const char *);
152 1.8 rmind npfvar_t * npfctl_parse_table_id(const char *);
153 1.18 spz npfvar_t * npfctl_parse_icmp(int, int, int);
154 1.8 rmind npfvar_t * npfctl_parse_port_range(in_port_t, in_port_t);
155 1.44 rmind npfvar_t * npfctl_parse_port_range_variable(const char *, npfvar_t *);
156 1.8 rmind npfvar_t * npfctl_parse_fam_addr_mask(const char *, const char *,
157 1.8 rmind unsigned long *);
158 1.54 joe
159 1.54 joe int npfctl_parse_user(const char *, uint32_t *);
160 1.54 joe int npfctl_parse_group(const char *, uint32_t *);
161 1.54 joe void npfctl_init_rid(rid_t *, uint32_t, uint32_t, uint8_t);
162 1.17 rmind bool npfctl_parse_cidr(char *, fam_addr_mask_t *, int *);
163 1.36 rmind uint16_t npfctl_npt66_calcadj(npf_netmask_t, const npf_addr_t *,
164 1.36 rmind const npf_addr_t *);
165 1.56 joe filt_opts_t npfctl_parse_l3filt_opt(npfvar_t *, npfvar_t *, bool,
166 1.56 joe npfvar_t *, npfvar_t *, bool, rid_t, rid_t);
167 1.56 joe filt_opts_t npfctl_parse_l2filt_opt(npfvar_t *, bool, npfvar_t *,
168 1.56 joe bool, uint16_t);
169 1.56 joe npfvar_t * npfctl_parse_mac_addr(const char *);
170 1.56 joe uint16_t npfctl_parse_ether_type(const char *str);
171 1.51 rmind int npfctl_nat_ruleset_p(const char *, bool *);
172 1.8 rmind
173 1.53 rmind void usage(void);
174 1.53 rmind void npfctl_rule(int, int, char **);
175 1.53 rmind void npfctl_table_replace(int, int, char **);
176 1.53 rmind void npfctl_table(int, int, char **);
177 1.53 rmind int npfctl_conn_list(int, int, char **);
178 1.53 rmind
179 1.8 rmind /*
180 1.20 rmind * NPF extension loading.
181 1.20 rmind */
182 1.20 rmind
183 1.20 rmind typedef struct npf_extmod npf_extmod_t;
184 1.20 rmind
185 1.20 rmind npf_extmod_t * npf_extmod_get(const char *, nl_ext_t **);
186 1.20 rmind int npf_extmod_param(npf_extmod_t *, nl_ext_t *,
187 1.20 rmind const char *, const char *);
188 1.20 rmind
189 1.20 rmind /*
190 1.30 rmind * BFF byte-code generation interface.
191 1.30 rmind */
192 1.30 rmind
193 1.30 rmind typedef struct npf_bpf npf_bpf_t;
194 1.30 rmind
195 1.30 rmind #define MATCH_DST 0x01
196 1.30 rmind #define MATCH_SRC 0x02
197 1.42 rmind #define MATCH_INVERT 0x04
198 1.30 rmind
199 1.30 rmind enum {
200 1.31 rmind BM_IPVER, BM_PROTO, BM_SRC_CIDR, BM_SRC_TABLE, BM_DST_CIDR,
201 1.31 rmind BM_DST_TABLE, BM_SRC_PORTS, BM_DST_PORTS, BM_TCPFL, BM_ICMP_TYPE,
202 1.53 rmind BM_ICMP_CODE, BM_SRC_NEG, BM_DST_NEG,
203 1.53 rmind
204 1.53 rmind BM_COUNT // total number of the marks
205 1.30 rmind };
206 1.30 rmind
207 1.56 joe enum { /* book marks for L2 */
208 1.56 joe BM_ETHER_TYPE, BM_SRC_ETHER, BM_DST_ETHER, BM_SRC_ENEG, BM_DST_ENEG,
209 1.56 joe };
210 1.56 joe
211 1.31 rmind npf_bpf_t * npfctl_bpf_create(void);
212 1.30 rmind struct bpf_program *npfctl_bpf_complete(npf_bpf_t *);
213 1.31 rmind const void * npfctl_bpf_bmarks(npf_bpf_t *, size_t *);
214 1.31 rmind void npfctl_bpf_destroy(npf_bpf_t *);
215 1.15 rmind
216 1.53 rmind void npfctl_bpf_group_enter(npf_bpf_t *, bool);
217 1.53 rmind void npfctl_bpf_group_exit(npf_bpf_t *);
218 1.15 rmind
219 1.53 rmind void npfctl_bpf_ipver(npf_bpf_t *, sa_family_t);
220 1.53 rmind void npfctl_bpf_proto(npf_bpf_t *, unsigned);
221 1.31 rmind void npfctl_bpf_cidr(npf_bpf_t *, u_int, sa_family_t,
222 1.31 rmind const npf_addr_t *, const npf_netmask_t);
223 1.31 rmind void npfctl_bpf_ports(npf_bpf_t *, u_int, in_port_t, in_port_t);
224 1.53 rmind void npfctl_bpf_tcpfl(npf_bpf_t *, uint8_t, uint8_t);
225 1.31 rmind void npfctl_bpf_icmp(npf_bpf_t *, int, int);
226 1.31 rmind void npfctl_bpf_table(npf_bpf_t *, u_int, u_int);
227 1.15 rmind
228 1.56 joe void npfctl_bpf_ether(npf_bpf_t *, unsigned, struct ether_addr *);
229 1.56 joe void fetch_ether_type(npf_bpf_t *, uint16_t);
230 1.56 joe
231 1.15 rmind /*
232 1.8 rmind * Configuration building interface.
233 1.8 rmind */
234 1.1 rmind
235 1.15 rmind #define NPFCTL_NAT_DYNAMIC 1
236 1.15 rmind #define NPFCTL_NAT_STATIC 2
237 1.8 rmind
238 1.8 rmind void npfctl_config_init(bool);
239 1.52 rmind void npfctl_config_build(void);
240 1.46 rmind int npfctl_config_send(int);
241 1.24 rmind nl_config_t * npfctl_config_ref(void);
242 1.14 rmind int npfctl_config_show(int);
243 1.40 christos void npfctl_config_save(nl_config_t *, const char *);
244 1.26 rmind int npfctl_ruleset_show(int, const char *);
245 1.26 rmind
246 1.25 rmind nl_rule_t * npfctl_rule_ref(void);
247 1.50 rmind nl_table_t * npfctl_table_ref(void);
248 1.34 rmind bool npfctl_debug_addif(const char *);
249 1.8 rmind
250 1.50 rmind nl_table_t * npfctl_load_table(const char *, int, u_int, const char *, FILE *);
251 1.50 rmind
252 1.29 christos void npfctl_build_alg(const char *);
253 1.8 rmind void npfctl_build_rproc(const char *, npfvar_t *);
254 1.34 rmind void npfctl_build_group(const char *, int, const char *, bool);
255 1.25 rmind void npfctl_build_group_end(void);
256 1.34 rmind void npfctl_build_rule(uint32_t, const char *, sa_family_t,
257 1.53 rmind const npfvar_t *, const filt_opts_t *,
258 1.33 rmind const char *, const char *);
259 1.45 rmind void npfctl_build_natseg(int, int, unsigned, const char *,
260 1.34 rmind const addr_port_t *, const addr_port_t *,
261 1.53 rmind const npfvar_t *, const filt_opts_t *, unsigned);
262 1.34 rmind void npfctl_build_maprset(const char *, int, const char *);
263 1.8 rmind void npfctl_build_table(const char *, u_int, const char *);
264 1.14 rmind
265 1.48 rmind void npfctl_setparam(const char *, int);
266 1.48 rmind
267 1.40 christos /*
268 1.40 christos * For the systems which do not define TH_ECE and TW_CRW.
269 1.40 christos */
270 1.53 rmind #ifndef TH_ECE
271 1.53 rmind #define TH_ECE 0x40
272 1.40 christos #endif
273 1.53 rmind #ifndef TH_CWR
274 1.53 rmind #define TH_CWR 0x80
275 1.40 christos #endif
276 1.40 christos
277 1.1 rmind #endif
278