xref: /openbsd/sbin/pfctl/pfctl_parser.h (revision 264ca280)
1 /*	$OpenBSD: pfctl_parser.h,v 1.105 2016/07/18 19:11:18 henning 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_RECURSE		0x4000
52 #define PF_OPT_PORTNAMES	0x8000
53 
54 #define PF_TH_ALL		0xFF
55 
56 #define PF_NAT_PROXY_PORT_LOW	50001
57 #define PF_NAT_PROXY_PORT_HIGH	65535
58 
59 #define PF_OPTIMIZE_BASIC	0x0001
60 #define PF_OPTIMIZE_PROFILE	0x0002
61 
62 #define FCNT_NAMES { \
63 	"searches", \
64 	"inserts", \
65 	"removals", \
66 	NULL \
67 }
68 
69 struct pfr_buffer;	/* forward definition */
70 
71 
72 struct pfctl {
73 	int dev;
74 	int opts;
75 	int optimize;
76 	int asd;			/* anchor stack depth */
77 	int bn;				/* brace number */
78 	int brace;
79 	int tdirty;			/* kernel dirty */
80 #define PFCTL_ANCHOR_STACK_DEPTH 64
81 	struct pf_anchor *astack[PFCTL_ANCHOR_STACK_DEPTH];
82 	struct pfioc_queue *pqueue;
83 	struct pfr_buffer *trans;
84 	struct pf_anchor *anchor, *alast;
85 	const char *ruleset;
86 
87 	/* 'set foo' options */
88 	u_int32_t	 timeout[PFTM_MAX];
89 	u_int32_t	 limit[PF_LIMIT_MAX];
90 	u_int32_t	 debug;
91 	u_int32_t	 hostid;
92 	u_int32_t	 reassemble;
93 	char		*ifname;
94 
95 	u_int8_t	 timeout_set[PFTM_MAX];
96 	u_int8_t	 limit_set[PF_LIMIT_MAX];
97 	u_int8_t	 debug_set;
98 	u_int8_t	 hostid_set;
99 	u_int8_t	 ifname_set;
100 	u_int8_t	 reass_set;
101 };
102 
103 struct node_if {
104 	char			 ifname[IFNAMSIZ];
105 	u_int8_t		 not;
106 	u_int8_t		 dynamic; /* antispoof */
107 	u_int8_t		 use_rdomain;
108 	u_int			 ifa_flags;
109 	int			 rdomain;
110 	struct node_if		*next;
111 	struct node_if		*tail;
112 };
113 
114 struct node_host {
115 	struct pf_addr_wrap	 addr;
116 	struct pf_addr		 bcast;
117 	struct pf_addr		 peer;
118 	sa_family_t		 af;
119 	u_int8_t		 not;
120 	u_int32_t		 ifindex;	/* link-local IPv6 addrs */
121 	u_int16_t		 weight;	/* load balancing weight */
122 	char			*ifname;
123 	u_int			 ifa_flags;
124 	struct node_host	*next;
125 	struct node_host	*tail;
126 };
127 void	freehostlist(struct node_host *);
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_queue_opt {
156 	int			 qtype;
157 	union {
158 		struct priq_opts	priq_opts;
159 		struct node_hfsc_opts	hfsc_opts;
160 	}			 data;
161 };
162 
163 SIMPLEQ_HEAD(node_tinithead, node_tinit);
164 struct node_tinit {	/* table initializer */
165 	SIMPLEQ_ENTRY(node_tinit)	 entries;
166 	struct node_host		*host;
167 	char				*file;
168 };
169 
170 
171 /* optimizer created tables */
172 struct pf_opt_tbl {
173 	char			 pt_name[PF_TABLE_NAME_SIZE];
174 	int			 pt_rulecount;
175 	int			 pt_generated;
176 	u_int32_t		 pt_flags;
177 	struct node_tinithead	 pt_nodes;
178 	struct pfr_buffer	*pt_buf;
179 };
180 #define PF_OPT_TABLE_PREFIX	"__automatic_"
181 
182 /* optimizer pf_rule container */
183 struct pf_opt_rule {
184 	struct pf_rule		 por_rule;
185 	struct pf_opt_tbl	*por_src_tbl;
186 	struct pf_opt_tbl	*por_dst_tbl;
187 	u_int64_t		 por_profile_count;
188 	TAILQ_ENTRY(pf_opt_rule) por_entry;
189 	TAILQ_ENTRY(pf_opt_rule) por_skip_entry[PF_SKIP_COUNT];
190 };
191 
192 TAILQ_HEAD(pf_opt_queue, pf_opt_rule);
193 
194 extern TAILQ_HEAD(pf_qihead, pfctl_qsitem) qspecs, rootqs;
195 struct pfctl_qsitem {
196 	TAILQ_ENTRY(pfctl_qsitem)	 entries;
197 	struct pf_queuespec		 qs;
198 	struct pf_qihead		 children;
199 	int				 matches;
200 };
201 
202 
203 int	pfctl_rules(int, char *, int, int, char *, struct pfr_buffer *);
204 int	pfctl_optimize_ruleset(struct pfctl *, struct pf_ruleset *);
205 int     pf_opt_create_table(struct pfctl *, struct pf_opt_tbl *);
206 int     add_opt_table(struct pfctl *, struct pf_opt_tbl **, sa_family_t,
207             struct pf_rule_addr *, char *);
208 
209 int	pfctl_add_rule(struct pfctl *, struct pf_rule *, const char *);
210 int	pfctl_add_pool(struct pfctl *, struct pf_pool *, sa_family_t, int);
211 void	pfctl_move_pool(struct pf_pool *, struct pf_pool *);
212 void	pfctl_clear_pool(struct pf_pool *);
213 
214 int	pfctl_set_timeout(struct pfctl *, const char *, int, int);
215 int	pfctl_set_reassembly(struct pfctl *, int, int);
216 int	pfctl_set_optimization(struct pfctl *, const char *);
217 int	pfctl_set_limit(struct pfctl *, const char *, unsigned int);
218 int	pfctl_set_logif(struct pfctl *, char *);
219 void	pfctl_set_hostid(struct pfctl *, u_int32_t);
220 int	pfctl_set_debug(struct pfctl *, char *);
221 int	pfctl_set_interface_flags(struct pfctl *, char *, int, int);
222 
223 int	parse_config(char *, struct pfctl *);
224 int	parse_flags(char *);
225 int	pfctl_load_anchors(int, struct pfctl *, struct pfr_buffer *);
226 
227 int	pfctl_load_queues(struct pfctl *);
228 int	pfctl_add_queue(struct pfctl *, struct pf_queuespec *);
229 struct pfctl_qsitem *	pfctl_find_queue(char *, struct pf_qihead *);
230 
231 void	print_pool(struct pf_pool *, u_int16_t, u_int16_t, sa_family_t, int, int);
232 void	print_src_node(struct pf_src_node *, int);
233 void	print_rule(struct pf_rule *, const char *, int);
234 void	print_tabledef(const char *, int, int, struct node_tinithead *);
235 void	print_status(struct pf_status *, int);
236 void	print_queuespec(struct pf_queuespec *);
237 
238 int	pfctl_define_table(char *, int, int, const char *, struct pfr_buffer *,
239 	    u_int32_t);
240 
241 void		 pfctl_clear_fingerprints(int, int);
242 int		 pfctl_file_fingerprints(int, int, const char *);
243 pf_osfp_t	 pfctl_get_fingerprint(const char *);
244 int		 pfctl_load_fingerprints(int, int);
245 char		*pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t);
246 void		 pfctl_show_fingerprints(int);
247 
248 struct icmptypeent {
249 	const char *name;
250 	u_int8_t type;
251 };
252 
253 struct icmpcodeent {
254 	const char *name;
255 	u_int8_t type;
256 	u_int8_t code;
257 };
258 
259 const struct icmptypeent *geticmptypebynumber(u_int8_t, u_int8_t);
260 const struct icmptypeent *geticmptypebyname(char *, u_int8_t);
261 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, u_int8_t);
262 const struct icmpcodeent *geticmpcodebyname(u_long, char *, u_int8_t);
263 
264 int			  string_to_loglevel(const char *);
265 const char		 *loglevel_to_string(int);
266 
267 struct pf_timeout {
268 	const char	*name;
269 	int		 timeout;
270 };
271 
272 extern const struct pf_timeout pf_timeouts[];
273 
274 void			 set_ipmask(struct node_host *, u_int8_t);
275 int			 check_netmask(struct node_host *, sa_family_t);
276 int			 unmask(struct pf_addr *, sa_family_t);
277 struct node_host	*gen_dynnode(struct node_host *, sa_family_t);
278 void			 ifa_load(void);
279 unsigned int		 ifa_nametoindex(const char *);
280 char			*ifa_indextoname(unsigned int, char *);
281 struct node_host	*ifa_exists(const char *);
282 struct node_host	*ifa_lookup(const char *, int);
283 struct node_host	*host(const char *);
284 
285 int			 append_addr(struct pfr_buffer *, char *, int);
286 int			 append_addr_host(struct pfr_buffer *,
287 			    struct node_host *, int, int);
288 
289 #endif /* _PFCTL_PARSER_H_ */
290