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