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