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