xref: /openbsd-src/sbin/pfctl/pfctl_parser.h (revision 5e3c7963eb248119b7dfd4b0defad58a7d9cd306)
1 /*	$OpenBSD: pfctl_parser.h,v 1.112 2018/09/06 15:07:34 kn Exp $ */
2 
3 /*
4  * Copyright (c) 2001 Daniel Hartmeier
5  * Copyright (c) 2002 - 2013 Henning Brauer <henning@openbsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *    - Redistributions of source code must retain the above copyright
13  *      notice, this list of conditions and the following disclaimer.
14  *    - Redistributions in binary form must reproduce the above
15  *      copyright notice, this list of conditions and the following
16  *      disclaimer in the documentation and/or other materials provided
17  *      with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef _PFCTL_PARSER_H_
35 #define _PFCTL_PARSER_H_
36 
37 #define PF_OSFP_FILE		"/etc/pf.os"
38 
39 #define PF_OPT_DISABLE		0x0001
40 #define PF_OPT_ENABLE		0x0002
41 #define PF_OPT_VERBOSE		0x0004
42 #define PF_OPT_NOACTION		0x0008
43 #define PF_OPT_QUIET		0x0010
44 #define PF_OPT_CLRRULECTRS	0x0020
45 #define PF_OPT_USEDNS		0x0040
46 #define PF_OPT_VERBOSE2		0x0080
47 #define PF_OPT_DUMMYACTION	0x0100
48 #define PF_OPT_DEBUG		0x0200
49 #define PF_OPT_SHOWALL		0x0400
50 #define PF_OPT_OPTIMIZE		0x0800
51 #define PF_OPT_NODNS		0x1000
52 #define PF_OPT_RECURSE		0x4000
53 #define PF_OPT_PORTNAMES	0x8000
54 
55 #define PF_TH_ALL		0xFF
56 
57 #define PF_NAT_PROXY_PORT_LOW	50001
58 #define PF_NAT_PROXY_PORT_HIGH	65535
59 
60 #define PF_OPTIMIZE_BASIC	0x0001
61 #define PF_OPTIMIZE_PROFILE	0x0002
62 
63 #define FCNT_NAMES { \
64 	"searches", \
65 	"inserts", \
66 	"removals", \
67 	NULL \
68 }
69 
70 struct pfr_buffer;	/* forward definition */
71 
72 
73 struct pfctl {
74 	int dev;
75 	int opts;
76 	int optimize;
77 	int asd;			/* anchor stack depth */
78 	int bn;				/* brace number */
79 	int brace;
80 	int tdirty;			/* kernel dirty */
81 #define PFCTL_ANCHOR_STACK_DEPTH 64
82 	struct pf_anchor *astack[PFCTL_ANCHOR_STACK_DEPTH];
83 	struct pfioc_queue *pqueue;
84 	struct pfr_buffer *trans;
85 	struct pf_anchor *anchor, *alast;
86 	const char *ruleset;
87 
88 	/* 'set foo' options */
89 	u_int32_t	 timeout[PFTM_MAX];
90 	u_int32_t	 limit[PF_LIMIT_MAX];
91 	u_int32_t	 debug;
92 	u_int32_t	 hostid;
93 	u_int32_t	 reassemble;
94 	u_int8_t	 syncookies;
95 	u_int8_t	 syncookieswat[2];	/* lowat, hiwat */
96 	char		*ifname;
97 
98 	u_int8_t	 timeout_set[PFTM_MAX];
99 	u_int8_t	 limit_set[PF_LIMIT_MAX];
100 	u_int8_t	 debug_set;
101 	u_int8_t	 hostid_set;
102 	u_int8_t	 ifname_set;
103 	u_int8_t	 reass_set;
104 	u_int8_t	 syncookies_set;
105 	u_int8_t	 syncookieswat_set;
106 };
107 
108 struct node_if {
109 	char			 ifname[IFNAMSIZ];
110 	u_int8_t		 not;
111 	u_int8_t		 dynamic; /* antispoof */
112 	u_int8_t		 use_rdomain;
113 	u_int			 ifa_flags;
114 	int			 rdomain;
115 	struct node_if		*next;
116 	struct node_if		*tail;
117 };
118 
119 struct node_host {
120 	struct pf_addr_wrap	 addr;
121 	struct pf_addr		 bcast;
122 	struct pf_addr		 peer;
123 	sa_family_t		 af;
124 	u_int8_t		 not;
125 	u_int32_t		 ifindex;	/* link-local IPv6 addrs */
126 	u_int16_t		 weight;	/* load balancing weight */
127 	char			*ifname;
128 	u_int			 ifa_flags;
129 	struct node_host	*next;
130 	struct node_host	*tail;
131 };
132 void	freehostlist(struct node_host *);
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_int32_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_queue_opt {
161 	int			 qtype;
162 	union {
163 		struct priq_opts	priq_opts;
164 		struct node_hfsc_opts	hfsc_opts;
165 	}			 data;
166 };
167 
168 SIMPLEQ_HEAD(node_tinithead, node_tinit);
169 struct node_tinit {	/* table initializer */
170 	SIMPLEQ_ENTRY(node_tinit)	 entries;
171 	struct node_host		*host;
172 	char				*file;
173 };
174 
175 
176 /* optimizer created tables */
177 struct pf_opt_tbl {
178 	char			 pt_name[PF_TABLE_NAME_SIZE];
179 	int			 pt_rulecount;
180 	int			 pt_generated;
181 	u_int32_t		 pt_flags;
182 	u_int32_t		 pt_refcnt;
183 	struct node_tinithead	 pt_nodes;
184 	struct pfr_buffer	*pt_buf;
185 };
186 #define PF_OPT_TABLE_PREFIX	"__automatic_"
187 
188 /* optimizer pf_rule container */
189 struct pf_opt_rule {
190 	struct pf_rule		 por_rule;
191 	struct pf_opt_tbl	*por_src_tbl;
192 	struct pf_opt_tbl	*por_dst_tbl;
193 	u_int64_t		 por_profile_count;
194 	TAILQ_ENTRY(pf_opt_rule) por_entry;
195 	TAILQ_ENTRY(pf_opt_rule) por_skip_entry[PF_SKIP_COUNT];
196 };
197 
198 TAILQ_HEAD(pf_opt_queue, pf_opt_rule);
199 
200 extern TAILQ_HEAD(pf_qihead, pfctl_qsitem) qspecs, rootqs;
201 struct pfctl_qsitem {
202 	TAILQ_ENTRY(pfctl_qsitem)	 entries;
203 	struct pf_queuespec		 qs;
204 	struct pf_qihead		 children;
205 	int				 matches;
206 };
207 
208 struct pfctl_watermarks {
209 	u_int32_t	hi;
210 	u_int32_t	lo;
211 };
212 
213 int	pfctl_rules(int, char *, int, int, char *, struct pfr_buffer *);
214 int	pfctl_optimize_ruleset(struct pfctl *, struct pf_ruleset *);
215 int     pf_opt_create_table(struct pfctl *, struct pf_opt_tbl *);
216 int     add_opt_table(struct pfctl *, struct pf_opt_tbl **, sa_family_t,
217             struct pf_rule_addr *, char *);
218 
219 int	pfctl_add_rule(struct pfctl *, struct pf_rule *, const char *);
220 int	pfctl_add_pool(struct pfctl *, struct pf_pool *, sa_family_t, int);
221 void	pfctl_move_pool(struct pf_pool *, struct pf_pool *);
222 void	pfctl_clear_pool(struct pf_pool *);
223 
224 int	pfctl_set_timeout(struct pfctl *, const char *, int, int);
225 int	pfctl_set_reassembly(struct pfctl *, int, int);
226 int	pfctl_set_syncookies(struct pfctl *, u_int8_t,
227 	    struct pfctl_watermarks *);
228 int	pfctl_set_optimization(struct pfctl *, const char *);
229 int	pfctl_set_limit(struct pfctl *, const char *, unsigned int);
230 int	pfctl_set_logif(struct pfctl *, char *);
231 void	pfctl_set_hostid(struct pfctl *, u_int32_t);
232 int	pfctl_set_debug(struct pfctl *, char *);
233 int	pfctl_set_interface_flags(struct pfctl *, char *, int, int);
234 
235 int	parse_config(char *, struct pfctl *);
236 int	parse_flags(char *);
237 int	pfctl_load_anchors(int, struct pfctl *, struct pfr_buffer *);
238 
239 int	pfctl_load_queues(struct pfctl *);
240 int	pfctl_add_queue(struct pfctl *, struct pf_queuespec *);
241 struct pfctl_qsitem *	pfctl_find_queue(char *, struct pf_qihead *);
242 
243 void	print_pool(struct pf_pool *, u_int16_t, u_int16_t, sa_family_t, int, int);
244 void	print_src_node(struct pf_src_node *, int);
245 void	print_rule(struct pf_rule *, const char *, int);
246 void	print_tabledef(const char *, int, int, struct node_tinithead *);
247 void	print_status(struct pf_status *, struct pfctl_watermarks *, int);
248 void	print_queuespec(struct pf_queuespec *);
249 
250 int	pfctl_define_table(char *, int, int, const char *, struct pfr_buffer *,
251 	    u_int32_t);
252 
253 void		 pfctl_clear_fingerprints(int, int);
254 int		 pfctl_file_fingerprints(int, int, const char *);
255 pf_osfp_t	 pfctl_get_fingerprint(const char *);
256 int		 pfctl_load_fingerprints(int, int);
257 char		*pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t);
258 void		 pfctl_show_fingerprints(int);
259 
260 struct icmptypeent {
261 	const char *name;
262 	u_int8_t type;
263 };
264 
265 struct icmpcodeent {
266 	const char *name;
267 	u_int8_t type;
268 	u_int8_t code;
269 };
270 
271 const struct icmptypeent *geticmptypebynumber(u_int8_t, u_int8_t);
272 const struct icmptypeent *geticmptypebyname(char *, u_int8_t);
273 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, u_int8_t);
274 const struct icmpcodeent *geticmpcodebyname(u_long, char *, u_int8_t);
275 
276 int			  string_to_loglevel(const char *);
277 const char		 *loglevel_to_string(int);
278 
279 struct pf_timeout {
280 	const char	*name;
281 	int		 timeout;
282 };
283 
284 extern const struct pf_timeout pf_timeouts[];
285 
286 void			 set_ipmask(struct node_host *, int);
287 int			 check_netmask(struct node_host *, sa_family_t);
288 int			 unmask(struct pf_addr *);
289 struct node_host	*gen_dynnode(struct node_host *, sa_family_t);
290 void			 ifa_load(void);
291 unsigned int		 ifa_nametoindex(const char *);
292 char			*ifa_indextoname(unsigned int, char *);
293 struct node_host	*ifa_exists(const char *);
294 struct node_host	*ifa_lookup(const char *, int);
295 struct node_host	*host(const char *, int);
296 
297 int			 append_addr(struct pfr_buffer *, char *, int, int);
298 int			 append_addr_host(struct pfr_buffer *,
299 			    struct node_host *, int, int);
300 
301 #endif /* _PFCTL_PARSER_H_ */
302