xref: /openbsd/sbin/pfctl/pfctl_parser.h (revision 42a2f8b7)
1 /*	$OpenBSD: pfctl_parser.h,v 1.121 2024/11/12 04:14:51 dlg Exp $ */
2 
3 /*
4  * Copyright (c) 2001 Daniel Hartmeier
5  * Copyright (c) 2002 - 2013 Henning Brauer <henning@openbsd.org>
6  * All rights reserved.
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  *
12  *    - Redistributions of source code must retain the above copyright
13  *      notice, this list of conditions and the following disclaimer.
14  *    - Redistributions in binary form must reproduce the above
15  *      copyright notice, this list of conditions and the following
16  *      disclaimer in the documentation and/or other materials provided
17  *      with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef _PFCTL_PARSER_H_
35 #define _PFCTL_PARSER_H_
36 
37 #define PF_OSFP_FILE		"/etc/pf.os"
38 
39 #define PF_OPT_DISABLE		0x00001
40 #define PF_OPT_ENABLE		0x00002
41 #define PF_OPT_VERBOSE		0x00004
42 #define PF_OPT_NOACTION		0x00008
43 #define PF_OPT_QUIET		0x00010
44 #define PF_OPT_CLRRULECTRS	0x00020
45 #define PF_OPT_USEDNS		0x00040
46 #define PF_OPT_VERBOSE2		0x00080
47 #define PF_OPT_DUMMYACTION	0x00100
48 #define PF_OPT_DEBUG		0x00200
49 #define PF_OPT_SHOWALL		0x00400
50 #define PF_OPT_OPTIMIZE		0x00800
51 #define PF_OPT_NODNS		0x01000
52 #define PF_OPT_RECURSE		0x04000
53 #define PF_OPT_PORTNAMES	0x08000
54 #define PF_OPT_IGNFAIL		0x10000
55 #define PF_OPT_CALLSHOW		0x20000
56 
57 #define PF_TH_ALL		0xFF
58 
59 #define PF_NAT_PROXY_PORT_LOW	50001
60 #define PF_NAT_PROXY_PORT_HIGH	65535
61 
62 #define PF_OPTIMIZE_BASIC	0x0001
63 #define PF_OPTIMIZE_PROFILE	0x0002
64 
65 #define FCNT_NAMES { \
66 	"searches", \
67 	"inserts", \
68 	"removals", \
69 	NULL \
70 }
71 
72 struct pfr_buffer;	/* forward definition */
73 
74 
75 struct pfctl {
76 	int dev;
77 	int opts;
78 	int optimize;
79 	int asd;			/* anchor stack depth */
80 	int bn;				/* brace number */
81 	int brace;
82 	int tdirty;			/* kernel dirty */
83 #define PFCTL_ANCHOR_STACK_DEPTH 64
84 	struct pf_anchor *astack[PFCTL_ANCHOR_STACK_DEPTH];
85 	struct pfioc_queue *pqueue;
86 	struct pfr_buffer *trans;
87 	struct pf_anchor *anchor, *alast;
88 	struct pfr_ktablehead pfr_ktlast;
89 	const char *ruleset;
90 
91 	/* 'set foo' options */
92 	u_int32_t	 timeout[PFTM_MAX];
93 	u_int32_t	 limit[PF_LIMIT_MAX];
94 	u_int32_t	 debug;
95 	u_int32_t	 hostid;
96 	u_int32_t	 reassemble;
97 	u_int8_t	 syncookies;
98 	u_int8_t	 syncookieswat[2];	/* lowat, hiwat */
99 	char		*ifname;
100 
101 	u_int8_t	 timeout_set[PFTM_MAX];
102 	u_int8_t	 limit_set[PF_LIMIT_MAX];
103 	u_int8_t	 debug_set;
104 	u_int8_t	 hostid_set;
105 	u_int8_t	 ifname_set;
106 	u_int8_t	 reass_set;
107 	u_int8_t	 syncookies_set;
108 	u_int8_t	 syncookieswat_set;
109 };
110 
111 struct node_if {
112 	char			 ifname[IFNAMSIZ];
113 	u_int8_t		 not;
114 	u_int8_t		 dynamic; /* antispoof */
115 	u_int8_t		 use_rdomain;
116 	u_int			 ifa_flags;
117 	int			 rdomain;
118 	struct node_if		*next;
119 	struct node_if		*tail;
120 };
121 
122 struct node_host {
123 	struct pf_addr_wrap	 addr;
124 	struct pf_addr		 bcast;
125 	struct pf_addr		 peer;
126 	sa_family_t		 af;
127 	u_int8_t		 not;
128 	u_int32_t		 ifindex;	/* link-local IPv6 addrs */
129 	u_int16_t		 weight;	/* load balancing weight */
130 	char			*ifname;
131 	u_int			 ifa_flags;
132 	struct node_host	*next;
133 	struct node_host	*tail;
134 };
135 void	freehostlist(struct node_host *);
136 
137 struct node_os {
138 	char			*os;
139 	pf_osfp_t		 fingerprint;
140 	struct node_os		*next;
141 	struct node_os		*tail;
142 };
143 
144 struct node_queue_bw {
145 	u_int64_t	bw_absolute;
146 	u_int16_t	bw_percent;
147 };
148 
149 struct node_hfsc_sc {
150 	struct node_queue_bw	m1;	/* slope of 1st segment; bps */
151 	u_int			d;	/* x-projection of m1; msec */
152 	struct node_queue_bw	m2;	/* slope of 2nd segment; bps */
153 	u_int8_t		used;
154 };
155 
156 struct node_hfsc_opts {
157 	struct node_hfsc_sc	realtime;
158 	struct node_hfsc_sc	linkshare;
159 	struct node_hfsc_sc	upperlimit;
160 	int			flags;
161 };
162 
163 struct node_queue_opt {
164 	int			 qtype;
165 	union {
166 		struct priq_opts	priq_opts;
167 		struct node_hfsc_opts	hfsc_opts;
168 	}			 data;
169 };
170 
171 SIMPLEQ_HEAD(node_tinithead, node_tinit);
172 struct node_tinit {	/* table initializer */
173 	SIMPLEQ_ENTRY(node_tinit)	 entries;
174 	struct node_host		*host;
175 	char				*file;
176 };
177 
178 
179 /* optimizer created tables */
180 struct pf_opt_tbl {
181 	char			 pt_name[PF_TABLE_NAME_SIZE];
182 	int			 pt_rulecount;
183 	int			 pt_generated;
184 	u_int32_t		 pt_flags;
185 	u_int32_t		 pt_refcnt;
186 	struct node_tinithead	 pt_nodes;
187 	struct pfr_buffer	*pt_buf;
188 };
189 
190 /* optimizer pf_rule container */
191 struct pf_opt_rule {
192 	struct pf_rule		 por_rule;
193 	struct pf_opt_tbl	*por_src_tbl;
194 	struct pf_opt_tbl	*por_dst_tbl;
195 	u_int64_t		 por_profile_count;
196 	TAILQ_ENTRY(pf_opt_rule) por_entry;
197 	TAILQ_ENTRY(pf_opt_rule) por_skip_entry[PF_SKIP_COUNT];
198 };
199 
200 TAILQ_HEAD(pf_opt_queue, pf_opt_rule);
201 
202 extern TAILQ_HEAD(pf_qihead, pfctl_qsitem) qspecs, rootqs;
203 struct pfctl_qsitem {
204 	TAILQ_ENTRY(pfctl_qsitem)	 entries;
205 	struct pf_queuespec		 qs;
206 	struct pf_qihead		 children;
207 	int				 matches;
208 };
209 
210 struct pfctl_watermarks {
211 	u_int32_t	hi;
212 	u_int32_t	lo;
213 };
214 
215 struct pfr_uktable;
216 
217 void		 copy_satopfaddr(struct pf_addr *, struct sockaddr *);
218 
219 int	pfctl_rules(int, char *, int, int, char *, struct pfr_buffer *);
220 int	pfctl_optimize_ruleset(struct pfctl *, struct pf_ruleset *);
221 int     pf_opt_create_table(struct pfctl *, struct pf_opt_tbl *);
222 int     add_opt_table(struct pfctl *, struct pf_opt_tbl **, sa_family_t,
223             struct pf_rule_addr *, char *);
224 
225 void	pfctl_add_rule(struct pfctl *, struct pf_rule *);
226 
227 int	pfctl_set_timeout(struct pfctl *, const char *, int, int);
228 int	pfctl_set_reassembly(struct pfctl *, int, int);
229 int	pfctl_set_syncookies(struct pfctl *, u_int8_t,
230 	    struct pfctl_watermarks *);
231 int	pfctl_set_optimization(struct pfctl *, const char *);
232 int	pfctl_set_limit(struct pfctl *, const char *, unsigned int);
233 int	pfctl_set_logif(struct pfctl *, char *);
234 void	pfctl_set_hostid(struct pfctl *, u_int32_t);
235 int	pfctl_set_debug(struct pfctl *, char *);
236 int	pfctl_set_interface_flags(struct pfctl *, char *, int, int);
237 
238 int	parse_config(char *, struct pfctl *);
239 int	parse_flags(char *);
240 int	pfctl_load_anchors(int, struct pfctl *);
241 
242 int	pfctl_load_queues(struct pfctl *);
243 int	pfctl_add_queue(struct pfctl *, struct pf_queuespec *);
244 struct pfctl_qsitem *	pfctl_find_queue(char *, struct pf_qihead *);
245 
246 void	print_pool(struct pf_pool *, u_int16_t, u_int16_t, sa_family_t, int, int);
247 void	print_src_node(struct pf_src_node *, int);
248 void	print_rule(struct pf_rule *, const char *, int);
249 void	print_tabledef(const char *, int, int, struct node_tinithead *);
250 void	print_status(struct pf_status *, struct pfctl_watermarks *, int);
251 void	print_queuespec(struct pf_queuespec *);
252 
253 int	pfctl_define_table(char *, int, int, const char *, struct pfr_buffer *,
254 	    u_int32_t, struct pfr_uktable *);
255 void	pfctl_expand_label_nr(struct pf_rule *, unsigned int);
256 
257 void		 pfctl_clear_fingerprints(int, int);
258 int		 pfctl_file_fingerprints(int, int, const char *);
259 pf_osfp_t	 pfctl_get_fingerprint(const char *);
260 int		 pfctl_load_fingerprints(int, int);
261 char		*pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t);
262 void		 pfctl_show_fingerprints(int);
263 
264 struct icmptypeent {
265 	const char *name;
266 	u_int8_t type;
267 };
268 
269 struct icmpcodeent {
270 	const char *name;
271 	u_int8_t type;
272 	u_int8_t code;
273 };
274 
275 const struct icmptypeent *geticmptypebynumber(u_int8_t, u_int8_t);
276 const struct icmptypeent *geticmptypebyname(char *, u_int8_t);
277 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, u_int8_t);
278 const struct icmpcodeent *geticmpcodebyname(u_long, char *, u_int8_t);
279 
280 int			  string_to_loglevel(const char *);
281 const char		 *loglevel_to_string(int);
282 
283 struct pf_timeout {
284 	const char	*name;
285 	int		 timeout;
286 };
287 
288 extern const struct pf_timeout pf_timeouts[];
289 
290 void			 set_ipmask(struct node_host *, int);
291 int			 check_netmask(struct node_host *, sa_family_t);
292 int			 unmask(struct pf_addr *);
293 struct node_host	*gen_dynnode(struct node_host *, sa_family_t);
294 void			 ifa_load(void);
295 unsigned int		 ifa_nametoindex(const char *);
296 char			*ifa_indextoname(unsigned int, char *);
297 struct node_host	*ifa_exists(const char *);
298 struct node_host	*ifa_lookup(const char *, int);
299 struct node_host	*host(const char *, int);
300 
301 int			 append_addr(struct pfr_buffer *, char *, int, int);
302 int			 append_addr_host(struct pfr_buffer *,
303 			    struct node_host *, int, int);
304 int			 pfr_ktable_compare(struct pfr_ktable *,
305 			    struct pfr_ktable *);
306 RB_PROTOTYPE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare);
307 
308 #endif /* _PFCTL_PARSER_H_ */
309