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