xref: /freebsd/sbin/pfctl/pfctl_parser.h (revision 9dfc5e03da50d12f02c2b481139acf9f089d504f)
1 /*	$OpenBSD: pfctl_parser.h,v 1.86 2006/10/31 23:46:25 mcbride Exp $ */
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2001 Daniel Hartmeier
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  *    - Redistributions of source code must retain the above copyright
14  *      notice, this list of conditions and the following disclaimer.
15  *    - Redistributions in binary form must reproduce the above
16  *      copyright notice, this list of conditions and the following
17  *      disclaimer in the documentation and/or other materials provided
18  *      with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _PFCTL_PARSER_H_
35 #define _PFCTL_PARSER_H_
36 
37 #include <libpfctl.h>
38 
39 #include <pfctl.h>
40 
41 #define PF_OSFP_FILE		"/etc/pf.os"
42 
43 #define PF_OPT_DISABLE		0x00001
44 #define PF_OPT_ENABLE		0x00002
45 #define PF_OPT_VERBOSE		0x00004
46 #define PF_OPT_NOACTION		0x00008
47 #define PF_OPT_QUIET		0x00010
48 #define PF_OPT_CLRRULECTRS	0x00020
49 #define PF_OPT_USEDNS		0x00040
50 #define PF_OPT_VERBOSE2		0x00080
51 #define PF_OPT_DUMMYACTION	0x00100
52 #define PF_OPT_DEBUG		0x00200
53 #define PF_OPT_SHOWALL		0x00400
54 #define PF_OPT_OPTIMIZE		0x00800
55 #define PF_OPT_NUMERIC		0x01000
56 #define PF_OPT_MERGE		0x02000
57 #define PF_OPT_RECURSE		0x04000
58 #define PF_OPT_KILLMATCH	0x08000
59 #define PF_OPT_NODNS		0x10000
60 #define PF_OPT_IGNFAIL		0x20000
61 #define PF_OPT_CALLSHOW		0x40000
62 
63 #define PF_NAT_PROXY_PORT_LOW	50001
64 #define PF_NAT_PROXY_PORT_HIGH	65535
65 
66 #define PF_OPTIMIZE_BASIC	0x0001
67 #define PF_OPTIMIZE_PROFILE	0x0002
68 
69 #define FCNT_NAMES { \
70 	"searches", \
71 	"inserts", \
72 	"removals", \
73 	NULL \
74 }
75 
76 struct pfr_buffer;	/* forward definition */
77 
78 
79 struct pfctl {
80 	int dev;
81 	struct pfctl_handle *h;
82 	int opts;
83 	int optimize;
84 	int loadopt;
85 	int asd;			/* anchor stack depth */
86 	int bn;				/* brace number */
87 	int tdirty;			/* kernel dirty */
88 #define PFCTL_ANCHOR_STACK_DEPTH 64
89 	struct pfctl_anchor *astack[PFCTL_ANCHOR_STACK_DEPTH];
90 	struct pfioc_pooladdr paddr;
91 	struct pfioc_altq *paltq;
92 	struct pfioc_queue *pqueue;
93 	struct pfr_buffer *trans;
94 	struct pfctl_anchor *anchor, *alast;
95 	struct pfr_ktablehead pfr_ktlast;
96 	int eth_nr;
97 	struct pfctl_eth_anchor *eanchor, *ealast;
98 	struct pfctl_eth_anchor *eastack[PFCTL_ANCHOR_STACK_DEPTH];
99 	u_int32_t eth_ticket;
100 	const char *ruleset;
101 
102 	/* 'set foo' options */
103 	u_int32_t	 timeout[PFTM_MAX];
104 	u_int32_t	 limit[PF_LIMIT_MAX];
105 	u_int32_t	 debug;
106 	u_int32_t	 hostid;
107 	u_int32_t	 reassemble;
108 	char		*ifname;
109 	bool		 keep_counters;
110 	u_int8_t	 syncookies;
111 	u_int8_t	 syncookieswat[2];	/* lowat, highwat, in % */
112 	u_int8_t	 syncookieswat_set;
113 
114 	u_int8_t	 timeout_set[PFTM_MAX];
115 	u_int8_t	 limit_set[PF_LIMIT_MAX];
116 	u_int8_t	 debug_set;
117 	u_int8_t	 hostid_set;
118 	u_int8_t	 ifname_set;
119 	u_int8_t	 reass_set;
120 };
121 
122 struct node_if {
123 	char			 ifname[IFNAMSIZ];
124 	u_int8_t		 not;
125 	u_int8_t		 dynamic; /* antispoof */
126 	u_int			 ifa_flags;
127 	struct node_if		*next;
128 	struct node_if		*tail;
129 };
130 
131 struct node_host {
132 	struct pf_addr_wrap	 addr;
133 	struct pf_addr		 bcast;
134 	struct pf_addr		 peer;
135 	sa_family_t		 af;
136 	u_int8_t		 not;
137 	u_int32_t		 ifindex;	/* link-local IPv6 addrs */
138 	char			*ifname;
139 	u_int			 ifa_flags;
140 	struct node_host	*next;
141 	struct node_host	*tail;
142 };
143 
144 void	freehostlist(struct node_host *);
145 
146 struct node_mac {
147 	u_int8_t	 mac[ETHER_ADDR_LEN];
148 	u_int8_t	 mask[ETHER_ADDR_LEN];
149 	bool		 neg;
150 	bool		 isset;
151 	struct node_mac	*next;
152 	struct node_mac	*tail;
153 };
154 
155 struct node_os {
156 	char			*os;
157 	pf_osfp_t		 fingerprint;
158 	struct node_os		*next;
159 	struct node_os		*tail;
160 };
161 
162 struct node_queue_bw {
163 	u_int64_t	bw_absolute;
164 	u_int16_t	bw_percent;
165 };
166 
167 struct node_hfsc_sc {
168 	struct node_queue_bw	m1;	/* slope of 1st segment; bps */
169 	u_int			d;	/* x-projection of m1; msec */
170 	struct node_queue_bw	m2;	/* slope of 2nd segment; bps */
171 	u_int8_t		used;
172 };
173 
174 struct node_hfsc_opts {
175 	struct node_hfsc_sc	realtime;
176 	struct node_hfsc_sc	linkshare;
177 	struct node_hfsc_sc	upperlimit;
178 	int			flags;
179 };
180 
181 struct node_fairq_sc {
182 	struct node_queue_bw	m1;	/* slope of 1st segment; bps */
183 	u_int			d;	/* x-projection of m1; msec */
184 	struct node_queue_bw	m2;	/* slope of 2nd segment; bps */
185 	u_int8_t		used;
186 };
187 
188 struct node_fairq_opts {
189 	struct node_fairq_sc	linkshare;
190 	struct node_queue_bw	hogs_bw;
191 	u_int			nbuckets;
192 	int			flags;
193 };
194 
195 struct node_queue_opt {
196 	int			 qtype;
197 	union {
198 		struct cbq_opts		cbq_opts;
199 		struct codel_opts	codel_opts;
200 		struct priq_opts	priq_opts;
201 		struct node_hfsc_opts	hfsc_opts;
202 		struct node_fairq_opts	fairq_opts;
203 	}			 data;
204 };
205 
206 #define QPRI_BITSET_SIZE	256
207 __BITSET_DEFINE(qpri_bitset, QPRI_BITSET_SIZE);
208 LIST_HEAD(gen_sc, segment);
209 
210 struct pfctl_altq {
211 	struct pf_altq	pa;
212 	struct {
213 		STAILQ_ENTRY(pfctl_altq)	link;
214 		u_int64_t			bwsum;
215 		struct qpri_bitset		qpris;
216 		int				children;
217 		int				root_classes;
218 		int				default_classes;
219 		struct gen_sc			lssc;
220 		struct gen_sc			rtsc;
221 	} meta;
222 };
223 
224 struct pfctl_watermarks {
225 	uint32_t	hi;
226 	uint32_t	lo;
227 };
228 
229 #ifdef __FreeBSD__
230 /*
231  * XXX
232  * Absolutely this is not correct location to define this.
233  * Should we use an another sperate header file?
234  */
235 #define	SIMPLEQ_HEAD			STAILQ_HEAD
236 #define	SIMPLEQ_HEAD_INITIALIZER	STAILQ_HEAD_INITIALIZER
237 #define	SIMPLEQ_ENTRY			STAILQ_ENTRY
238 #define	SIMPLEQ_FIRST			STAILQ_FIRST
239 #define	SIMPLEQ_END(head)		NULL
240 #define	SIMPLEQ_EMPTY			STAILQ_EMPTY
241 #define	SIMPLEQ_NEXT			STAILQ_NEXT
242 /*#define	SIMPLEQ_FOREACH			STAILQ_FOREACH*/
243 #define	SIMPLEQ_FOREACH(var, head, field)		\
244     for((var) = SIMPLEQ_FIRST(head);			\
245 	(var) != SIMPLEQ_END(head);			\
246 	(var) = SIMPLEQ_NEXT(var, field))
247 #define	SIMPLEQ_INIT			STAILQ_INIT
248 #define	SIMPLEQ_INSERT_HEAD		STAILQ_INSERT_HEAD
249 #define	SIMPLEQ_INSERT_TAIL		STAILQ_INSERT_TAIL
250 #define	SIMPLEQ_INSERT_AFTER		STAILQ_INSERT_AFTER
251 #define	SIMPLEQ_REMOVE_HEAD		STAILQ_REMOVE_HEAD
252 #endif
253 SIMPLEQ_HEAD(node_tinithead, node_tinit);
254 struct node_tinit {	/* table initializer */
255 	SIMPLEQ_ENTRY(node_tinit)	 entries;
256 	struct node_host		*host;
257 	char				*file;
258 };
259 
260 
261 /* optimizer created tables */
262 struct pf_opt_tbl {
263 	char			 pt_name[PF_TABLE_NAME_SIZE];
264 	int			 pt_rulecount;
265 	int			 pt_generated;
266 	uint32_t		 pt_refcnt;
267 	struct node_tinithead	 pt_nodes;
268 	struct pfr_buffer	*pt_buf;
269 };
270 
271 /* optimizer pf_rule container */
272 struct pf_opt_rule {
273 	struct pfctl_rule	 por_rule;
274 	struct pf_opt_tbl	*por_src_tbl;
275 	struct pf_opt_tbl	*por_dst_tbl;
276 	u_int64_t		 por_profile_count;
277 	TAILQ_ENTRY(pf_opt_rule) por_entry;
278 	TAILQ_ENTRY(pf_opt_rule) por_skip_entry[PF_SKIP_COUNT];
279 };
280 
281 TAILQ_HEAD(pf_opt_queue, pf_opt_rule);
282 
283 struct pfr_uktable;
284 
285 void	copy_satopfaddr(struct pf_addr *, struct sockaddr *);
286 
287 int	pfctl_rules(int, char *, int, int, char *, struct pfr_buffer *);
288 int	pfctl_optimize_ruleset(struct pfctl *, struct pfctl_ruleset *);
289 
290 void	pfctl_init_rule(struct pfctl_rule *r);
291 int	pfctl_append_rule(struct pfctl *, struct pfctl_rule *, const char *);
292 int	pfctl_append_eth_rule(struct pfctl *, struct pfctl_eth_rule *,
293 	    const char *);
294 int	pfctl_add_altq(struct pfctl *, struct pf_altq *);
295 int	pfctl_add_pool(struct pfctl *, struct pfctl_pool *, int);
296 void	pfctl_move_pool(struct pfctl_pool *, struct pfctl_pool *);
297 void	pfctl_clear_pool(struct pfctl_pool *);
298 
299 int	pfctl_apply_timeout(struct pfctl *, const char *, int, int);
300 int	pfctl_set_reassembly(struct pfctl *, int, int);
301 int	pfctl_set_optimization(struct pfctl *, const char *);
302 int	pfctl_apply_limit(struct pfctl *, const char *, unsigned int);
303 int	pfctl_set_logif(struct pfctl *, char *);
304 void	pfctl_set_hostid(struct pfctl *, u_int32_t);
305 int	pfctl_do_set_debug(struct pfctl *, char *);
306 int	pfctl_set_interface_flags(struct pfctl *, char *, int, int);
307 int	pfctl_cfg_syncookies(struct pfctl *, uint8_t, struct pfctl_watermarks *);
308 
309 int	parse_config(char *, struct pfctl *);
310 int	parse_flags(char *);
311 int	pfctl_load_anchors(int, struct pfctl *);
312 
313 void	print_pool(struct pfctl_pool *, u_int16_t, u_int16_t, int);
314 void	print_src_node(struct pfctl_src_node *, int);
315 void	print_eth_rule(struct pfctl_eth_rule *, const char *, int);
316 void	print_rule(struct pfctl_rule *, const char *, int, int);
317 void	print_tabledef(const char *, int, int, struct node_tinithead *);
318 void	print_status(struct pfctl_status *, struct pfctl_syncookies *, int);
319 void	print_running(struct pfctl_status *);
320 
321 int	eval_pfaltq(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
322 	    struct node_queue_opt *);
323 int	eval_pfqueue(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
324 	    struct node_queue_opt *);
325 
326 void	 print_altq(const struct pf_altq *, unsigned, struct node_queue_bw *,
327 	    struct node_queue_opt *);
328 void	 print_queue(const struct pf_altq *, unsigned, struct node_queue_bw *,
329 	    int, struct node_queue_opt *);
330 
331 int	pfctl_define_table(char *, int, int, const char *, struct pfr_buffer *,
332 	    u_int32_t, struct pfr_uktable *);
333 
334 void		 pfctl_clear_fingerprints(int, int);
335 int		 pfctl_file_fingerprints(int, int, const char *);
336 pf_osfp_t	 pfctl_get_fingerprint(const char *);
337 int		 pfctl_load_fingerprints(int, int);
338 char		*pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t);
339 void		 pfctl_show_fingerprints(int);
340 
341 
342 struct icmptypeent {
343 	const char *name;
344 	u_int8_t type;
345 };
346 
347 struct icmpcodeent {
348 	const char *name;
349 	u_int8_t type;
350 	u_int8_t code;
351 };
352 
353 const struct icmptypeent *geticmptypebynumber(u_int8_t, sa_family_t);
354 const struct icmptypeent *geticmptypebyname(char *, sa_family_t);
355 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, sa_family_t);
356 const struct icmpcodeent *geticmpcodebyname(u_long, char *, sa_family_t);
357 
358 struct pf_timeout {
359 	const char	*name;
360 	int		 timeout;
361 };
362 
363 #define PFCTL_FLAG_FILTER	0x02
364 #define PFCTL_FLAG_NAT		0x04
365 #define PFCTL_FLAG_OPTION	0x08
366 #define PFCTL_FLAG_ALTQ		0x10
367 #define PFCTL_FLAG_TABLE	0x20
368 #define PFCTL_FLAG_ETH		0x40
369 
370 extern const struct pf_timeout pf_timeouts[];
371 
372 void			 set_ipmask(struct node_host *, int);
373 int			 check_netmask(struct node_host *, sa_family_t);
374 int			 unmask(struct pf_addr *);
375 struct node_host	*gen_dynnode(struct node_host *, sa_family_t);
376 void			 ifa_load(void);
377 unsigned int		 ifa_nametoindex(const char *);
378 char			*ifa_indextoname(unsigned int, char *);
379 int			 get_query_socket(void);
380 struct node_host	*ifa_exists(char *);
381 struct node_host	*ifa_grouplookup(char *ifa_name, int flags);
382 struct node_host	*ifa_lookup(char *, int);
383 struct node_host	*host(const char *, int);
384 
385 int			 append_addr(struct pfr_buffer *, char *, int, int);
386 int			 append_addr_host(struct pfr_buffer *,
387 			    struct node_host *, int, int);
388 int			 pfr_ktable_compare(struct pfr_ktable *,
389 			    struct pfr_ktable *);
390 RB_PROTOTYPE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare);
391 
392 #endif /* _PFCTL_PARSER_H_ */
393