1 /*	$OpenBSD: pfctl_parser.h,v 1.75 2004/04/14 11:16:43 cedric Exp $ */
2 
3 /*
4  * Copyright (c) 2001 Daniel Hartmeier
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *    - Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *    - Redistributions in binary form must reproduce the above
14  *      copyright notice, this list of conditions and the following
15  *      disclaimer in the documentation and/or other materials provided
16  *      with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #ifndef _PFCTL_PARSER_H_
34 #define _PFCTL_PARSER_H_
35 
36 #define PF_OSFP_FILE		"/etc/pf.os"
37 
38 #define PF_OPT_DISABLE		0x0001
39 #define PF_OPT_ENABLE		0x0002
40 #define PF_OPT_VERBOSE		0x0004
41 #define PF_OPT_NOACTION		0x0008
42 #define PF_OPT_QUIET		0x0010
43 #define PF_OPT_CLRRULECTRS	0x0020
44 #define PF_OPT_USEDNS		0x0040
45 #define PF_OPT_VERBOSE2		0x0080
46 #define PF_OPT_DUMMYACTION	0x0100
47 #define PF_OPT_DEBUG		0x0200
48 #define PF_OPT_SHOWALL		0x0400
49 
50 #define PF_TH_ALL		0xFF
51 
52 #define PF_NAT_PROXY_PORT_LOW	50001
53 #define PF_NAT_PROXY_PORT_HIGH	65535
54 
55 #define FCNT_NAMES { \
56 	"searches", \
57 	"inserts", \
58 	"removals", \
59 	NULL \
60 }
61 
62 struct pfctl {
63 	int dev;
64 	int opts;
65 	int loadopt;
66 	u_int32_t tticket;		/* table ticket */
67 	int tdirty;			/* kernel dirty */
68 	u_int32_t rule_nr;
69 	struct pfioc_pooladdr paddr;
70 	struct pfioc_altq *paltq;
71 	struct pfioc_queue *pqueue;
72 	struct pfr_buffer *trans;
73 	const char *anchor;
74 	const char *ruleset;
75 };
76 
77 struct node_if {
78 	char			 ifname[IFNAMSIZ];
79 	u_int8_t		 not;
80 	u_int8_t		 dynamic; /* antispoof */
81 	u_int			 ifa_flags;
82 	struct node_if		*next;
83 	struct node_if		*tail;
84 };
85 
86 struct node_host {
87 	struct pf_addr_wrap	 addr;
88 	struct pf_addr		 bcast;
89 	struct pf_addr		 peer;
90 	sa_family_t		 af;
91 	u_int8_t		 not;
92 	u_int32_t		 ifindex;	/* link-local IPv6 addrs */
93 	char			*ifname;
94 	u_int			 ifa_flags;
95 	struct node_host	*next;
96 	struct node_host	*tail;
97 };
98 /* special flags used by ifa_exists */
99 #define PF_IFA_FLAG_GROUP	0x10000
100 #define PF_IFA_FLAG_DYNAMIC	0x20000
101 #define PF_IFA_FLAG_CLONABLE	0x40000
102 
103 struct node_os {
104 	char			*os;
105 	pf_osfp_t		 fingerprint;
106 	struct node_os		*next;
107 	struct node_os		*tail;
108 };
109 
110 struct node_queue_bw {
111 	u_int32_t	bw_absolute;
112 	u_int16_t	bw_percent;
113 };
114 
115 struct node_hfsc_sc {
116 	struct node_queue_bw	m1;	/* slope of 1st segment; bps */
117 	u_int			d;	/* x-projection of m1; msec */
118 	struct node_queue_bw	m2;	/* slope of 2nd segment; bps */
119 	u_int8_t		used;
120 };
121 
122 struct node_hfsc_opts {
123 	struct node_hfsc_sc	realtime;
124 	struct node_hfsc_sc	linkshare;
125 	struct node_hfsc_sc	upperlimit;
126 	int			flags;
127 };
128 
129 struct node_queue_opt {
130 	int			 qtype;
131 	union {
132 		struct cbq_opts		cbq_opts;
133 		struct priq_opts	priq_opts;
134 		struct node_hfsc_opts	hfsc_opts;
135 	}			 data;
136 };
137 
138 SIMPLEQ_HEAD(node_tinithead, node_tinit);
139 struct node_tinit {	/* table initializer */
140 	SIMPLEQ_ENTRY(node_tinit)	 entries;
141 	struct node_host		*host;
142 	char				*file;
143 };
144 
145 struct pfr_buffer;	/* forward definition */
146 
147 int	pfctl_rules(int, char *, int, char *, char *, struct pfr_buffer *);
148 
149 int	pfctl_add_rule(struct pfctl *, struct pf_rule *);
150 int	pfctl_add_altq(struct pfctl *, struct pf_altq *);
151 int	pfctl_add_pool(struct pfctl *, struct pf_pool *, sa_family_t);
152 void	pfctl_clear_pool(struct pf_pool *);
153 
154 int	pfctl_set_timeout(struct pfctl *, const char *, int, int);
155 int	pfctl_set_optimization(struct pfctl *, const char *);
156 int	pfctl_set_limit(struct pfctl *, const char *, unsigned int);
157 int	pfctl_set_logif(struct pfctl *, char *);
158 int	pfctl_set_hostid(struct pfctl *, u_int32_t);
159 int	pfctl_set_debug(struct pfctl *, char *);
160 
161 int	parse_rules(FILE *, struct pfctl *);
162 int	parse_flags(char *);
163 int	pfctl_load_anchors(int, int, struct pfr_buffer *);
164 
165 void	print_pool(struct pf_pool *, u_int16_t, u_int16_t, sa_family_t, int);
166 void	print_src_node(struct pf_src_node *, int);
167 void	print_rule(struct pf_rule *, int);
168 void	print_tabledef(const char *, int, int, struct node_tinithead *);
169 void	print_status(struct pf_status *, int);
170 
171 int	eval_pfaltq(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
172 	    struct node_queue_opt *);
173 int	eval_pfqueue(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
174 	    struct node_queue_opt *);
175 
176 void	 print_altq(const struct pf_altq *, unsigned, struct node_queue_bw *,
177 	    struct node_queue_opt *);
178 void	 print_queue(const struct pf_altq *, unsigned, struct node_queue_bw *,
179 	    int, struct node_queue_opt *);
180 
181 int	pfctl_define_table(char *, int, int, const char *, const char *,
182 	    struct pfr_buffer *, u_int32_t);
183 
184 void		 pfctl_clear_fingerprints(int, int);
185 int		 pfctl_file_fingerprints(int, int, const char *);
186 pf_osfp_t	 pfctl_get_fingerprint(const char *);
187 int		 pfctl_load_fingerprints(int, int);
188 char		*pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t);
189 void		 pfctl_show_fingerprints(int);
190 
191 
192 struct icmptypeent {
193 	const char *name;
194 	u_int8_t type;
195 };
196 
197 struct icmpcodeent {
198 	const char *name;
199 	u_int8_t type;
200 	u_int8_t code;
201 };
202 
203 const struct icmptypeent *geticmptypebynumber(u_int8_t, u_int8_t);
204 const struct icmptypeent *geticmptypebyname(char *, u_int8_t);
205 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, u_int8_t);
206 const struct icmpcodeent *geticmpcodebyname(u_long, char *, u_int8_t);
207 
208 struct pf_timeout {
209 	const char	*name;
210 	int		 timeout;
211 };
212 
213 #define PFCTL_FLAG_FILTER	0x02
214 #define PFCTL_FLAG_NAT		0x04
215 #define PFCTL_FLAG_OPTION	0x08
216 #define PFCTL_FLAG_ALTQ		0x10
217 #define PFCTL_FLAG_TABLE	0x20
218 
219 extern const struct pf_timeout pf_timeouts[];
220 
221 void			 set_ipmask(struct node_host *, u_int8_t);
222 int			 check_netmask(struct node_host *, sa_family_t);
223 void			 ifa_load(void);
224 struct node_host	*ifa_exists(const char *, int);
225 struct node_host	*ifa_lookup(const char *, int);
226 struct node_host	*host(const char *);
227 
228 int			 append_addr(struct pfr_buffer *, char *, int);
229 int			 append_addr_host(struct pfr_buffer *,
230 			    struct node_host *, int, int);
231 
232 #endif /* _PFCTL_PARSER_H_ */
233