pfctl_parser.h revision 1.1.1.3 1 /* $OpenBSD: pfctl_parser.h,v 1.80 2005/02/07 18:18:14 david 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 #define PF_OPT_OPTIMIZE 0x0800
50 #define PF_OPT_OPTIMIZE_PROFILE 0x1000
51 #define PF_OPT_MERGE 0x2000
52
53 #define PF_TH_ALL 0xFF
54
55 #define PF_NAT_PROXY_PORT_LOW 50001
56 #define PF_NAT_PROXY_PORT_HIGH 65535
57
58 #define FCNT_NAMES { \
59 "searches", \
60 "inserts", \
61 "removals", \
62 NULL \
63 }
64
65 struct pfr_buffer; /* forward definition */
66 struct pf_opt_rule;
67 TAILQ_HEAD(pf_opt_queue, pf_opt_rule);
68
69
70 struct pfctl {
71 int dev;
72 int opts;
73 int loadopt;
74 u_int32_t tticket; /* table ticket */
75 int tdirty; /* kernel dirty */
76 u_int32_t rule_nr;
77 struct pfioc_pooladdr paddr;
78 struct pfioc_altq *paltq;
79 struct pfioc_queue *pqueue;
80 struct pfr_buffer *trans;
81 const char *anchor;
82 const char *ruleset;
83 struct pf_opt_queue opt_queue;
84
85 /* 'set foo' options */
86 u_int32_t timeout[PFTM_MAX];
87 u_int32_t limit[PF_LIMIT_MAX];
88 u_int32_t debug;
89 u_int32_t hostid;
90 char *ifname;
91
92 u_int8_t timeout_set[PFTM_MAX];
93 u_int8_t limit_set[PF_LIMIT_MAX];
94 u_int8_t debug_set;
95 u_int8_t hostid_set;
96 u_int8_t ifname_set;
97 };
98
99 struct node_if {
100 char ifname[IFNAMSIZ];
101 u_int8_t not;
102 u_int8_t dynamic; /* antispoof */
103 u_int ifa_flags;
104 struct node_if *next;
105 struct node_if *tail;
106 };
107
108 struct node_host {
109 struct pf_addr_wrap addr;
110 struct pf_addr bcast;
111 struct pf_addr peer;
112 sa_family_t af;
113 u_int8_t not;
114 u_int32_t ifindex; /* link-local IPv6 addrs */
115 char *ifname;
116 u_int ifa_flags;
117 struct node_host *next;
118 struct node_host *tail;
119 };
120 /* special flags used by ifa_exists */
121 #define PF_IFA_FLAG_GROUP 0x10000
122 #define PF_IFA_FLAG_DYNAMIC 0x20000
123 #define PF_IFA_FLAG_CLONABLE 0x40000
124
125 struct node_os {
126 char *os;
127 pf_osfp_t fingerprint;
128 struct node_os *next;
129 struct node_os *tail;
130 };
131
132 struct node_queue_bw {
133 u_int32_t bw_absolute;
134 u_int16_t bw_percent;
135 };
136
137 struct node_hfsc_sc {
138 struct node_queue_bw m1; /* slope of 1st segment; bps */
139 u_int d; /* x-projection of m1; msec */
140 struct node_queue_bw m2; /* slope of 2nd segment; bps */
141 u_int8_t used;
142 };
143
144 struct node_hfsc_opts {
145 struct node_hfsc_sc realtime;
146 struct node_hfsc_sc linkshare;
147 struct node_hfsc_sc upperlimit;
148 int flags;
149 };
150
151 struct node_queue_opt {
152 int qtype;
153 union {
154 struct cbq_opts cbq_opts;
155 struct priq_opts priq_opts;
156 struct node_hfsc_opts hfsc_opts;
157 } data;
158 };
159
160 SIMPLEQ_HEAD(node_tinithead, node_tinit);
161 struct node_tinit { /* table initializer */
162 SIMPLEQ_ENTRY(node_tinit) entries;
163 struct node_host *host;
164 char *file;
165 };
166
167
168 /* optimizer created tables */
169 struct pf_opt_tbl {
170 char pt_name[PF_TABLE_NAME_SIZE];
171 int pt_rulecount;
172 int pt_generated;
173 struct node_tinithead pt_nodes;
174 struct pfr_buffer *pt_buf;
175 };
176 #define PF_OPT_TABLE_PREFIX "__automatic_"
177
178 /* optimizer pf_rule container */
179 struct pf_opt_rule {
180 struct pf_rule por_rule;
181 struct pf_opt_tbl *por_src_tbl;
182 struct pf_opt_tbl *por_dst_tbl;
183 char por_anchor[MAXPATHLEN];
184 u_int64_t por_profile_count;
185 TAILQ_ENTRY(pf_opt_rule) por_entry;
186 TAILQ_ENTRY(pf_opt_rule) por_skip_entry[PF_SKIP_COUNT];
187 };
188
189
190 int pfctl_rules(int, char *, int, char *, struct pfr_buffer *);
191 int pfctl_optimize_rules(struct pfctl *);
192
193 int pfctl_add_rule(struct pfctl *, struct pf_rule *, const char *);
194 int pfctl_add_altq(struct pfctl *, struct pf_altq *);
195 int pfctl_add_pool(struct pfctl *, struct pf_pool *, sa_family_t);
196 void pfctl_clear_pool(struct pf_pool *);
197
198 int pfctl_set_timeout(struct pfctl *, const char *, int, int);
199 int pfctl_set_optimization(struct pfctl *, const char *);
200 int pfctl_set_limit(struct pfctl *, const char *, unsigned int);
201 int pfctl_set_logif(struct pfctl *, char *);
202 int pfctl_set_hostid(struct pfctl *, u_int32_t);
203 int pfctl_set_debug(struct pfctl *, char *);
204 int pfctl_set_interface_flags(struct pfctl *, char *, int, int);
205
206 int parse_rules(FILE *, struct pfctl *);
207 int parse_flags(char *);
208 int pfctl_load_anchors(int, int, struct pfr_buffer *);
209
210 void print_pool(struct pf_pool *, u_int16_t, u_int16_t, sa_family_t, int);
211 void print_src_node(struct pf_src_node *, int);
212 void print_rule(struct pf_rule *, const char *, int);
213 void print_tabledef(const char *, int, int, struct node_tinithead *);
214 void print_status(struct pf_status *, int);
215
216 int eval_pfaltq(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
217 struct node_queue_opt *);
218 int eval_pfqueue(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
219 struct node_queue_opt *);
220
221 void print_altq(const struct pf_altq *, unsigned, struct node_queue_bw *,
222 struct node_queue_opt *);
223 void print_queue(const struct pf_altq *, unsigned, struct node_queue_bw *,
224 int, struct node_queue_opt *);
225
226 int pfctl_define_table(char *, int, int, const char *, struct pfr_buffer *,
227 u_int32_t);
228
229 void pfctl_clear_fingerprints(int, int);
230 int pfctl_file_fingerprints(int, int, const char *);
231 pf_osfp_t pfctl_get_fingerprint(const char *);
232 int pfctl_load_fingerprints(int, int);
233 char *pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t);
234 void pfctl_show_fingerprints(int);
235
236
237 struct icmptypeent {
238 const char *name;
239 u_int8_t type;
240 };
241
242 struct icmpcodeent {
243 const char *name;
244 u_int8_t type;
245 u_int8_t code;
246 };
247
248 const struct icmptypeent *geticmptypebynumber(u_int8_t, u_int8_t);
249 const struct icmptypeent *geticmptypebyname(char *, u_int8_t);
250 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, u_int8_t);
251 const struct icmpcodeent *geticmpcodebyname(u_long, char *, u_int8_t);
252
253 struct pf_timeout {
254 const char *name;
255 int timeout;
256 };
257
258 #define PFCTL_FLAG_FILTER 0x02
259 #define PFCTL_FLAG_NAT 0x04
260 #define PFCTL_FLAG_OPTION 0x08
261 #define PFCTL_FLAG_ALTQ 0x10
262 #define PFCTL_FLAG_TABLE 0x20
263
264 extern const struct pf_timeout pf_timeouts[];
265
266 void set_ipmask(struct node_host *, u_int8_t);
267 int check_netmask(struct node_host *, sa_family_t);
268 int unmask(struct pf_addr *, sa_family_t);
269 void ifa_load(void);
270 struct node_host *ifa_exists(const char *, int);
271 struct node_host *ifa_lookup(const char *, int);
272 struct node_host *host(const char *);
273
274 int append_addr(struct pfr_buffer *, char *, int);
275 int append_addr_host(struct pfr_buffer *,
276 struct node_host *, int, int);
277
278 #endif /* _PFCTL_PARSER_H_ */
279