pfctl_parser.h revision 1.3 1 /* $NetBSD: pfctl_parser.h,v 1.3 2004/11/14 11:26:48 yamt Exp $ */
2 /* $OpenBSD: pfctl_parser.h,v 1.77 2004/07/16 23:44:25 frantzen Exp $ */
3
4 /*
5 * Copyright (c) 2001 Daniel Hartmeier
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_OPTIMIZE_PROFILE 0x1000
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
86 struct node_if {
87 char ifname[IFNAMSIZ];
88 u_int8_t not;
89 u_int8_t dynamic; /* antispoof */
90 u_int ifa_flags;
91 struct node_if *next;
92 struct node_if *tail;
93 };
94
95 struct node_host {
96 struct pf_addr_wrap addr;
97 struct pf_addr bcast;
98 struct pf_addr peer;
99 sa_family_t af;
100 u_int8_t not;
101 u_int32_t ifindex; /* link-local IPv6 addrs */
102 char *ifname;
103 u_int ifa_flags;
104 struct node_host *next;
105 struct node_host *tail;
106 };
107 /* special flags used by ifa_exists */
108 #define PF_IFA_FLAG_GROUP 0x10000
109 #define PF_IFA_FLAG_DYNAMIC 0x20000
110 #define PF_IFA_FLAG_CLONABLE 0x40000
111
112 struct node_os {
113 char *os;
114 pf_osfp_t fingerprint;
115 struct node_os *next;
116 struct node_os *tail;
117 };
118
119 struct node_queue_bw {
120 u_int32_t bw_absolute;
121 u_int16_t bw_percent;
122 };
123
124 struct node_hfsc_sc {
125 struct node_queue_bw m1; /* slope of 1st segment; bps */
126 u_int d; /* x-projection of m1; msec */
127 struct node_queue_bw m2; /* slope of 2nd segment; bps */
128 u_int8_t used;
129 };
130
131 struct node_hfsc_opts {
132 struct node_hfsc_sc realtime;
133 struct node_hfsc_sc linkshare;
134 struct node_hfsc_sc upperlimit;
135 int flags;
136 };
137
138 struct node_queue_opt {
139 int qtype;
140 union {
141 struct cbq_opts cbq_opts;
142 struct priq_opts priq_opts;
143 struct node_hfsc_opts hfsc_opts;
144 } data;
145 };
146
147 SIMPLEQ_HEAD(node_tinithead, node_tinit);
148 struct node_tinit { /* table initializer */
149 SIMPLEQ_ENTRY(node_tinit) entries;
150 struct node_host *host;
151 char *file;
152 };
153
154
155 /* optimizer created tables */
156 struct pf_opt_tbl {
157 char pt_name[PF_TABLE_NAME_SIZE];
158 int pt_rulecount;
159 int pt_generated;
160 struct node_tinithead pt_nodes;
161 struct pfr_buffer *pt_buf;
162 };
163 #define PF_OPT_TABLE_PREFIX "__automatic_"
164
165 /* optimizer pf_rule container */
166 struct pf_opt_rule {
167 struct pf_rule por_rule;
168 struct pf_opt_tbl *por_src_tbl;
169 struct pf_opt_tbl *por_dst_tbl;
170 char por_anchor[MAXPATHLEN];
171 u_int64_t por_profile_count;
172 TAILQ_ENTRY(pf_opt_rule) por_entry;
173 TAILQ_ENTRY(pf_opt_rule) por_skip_entry[PF_SKIP_COUNT];
174 };
175
176
177 int pfctl_rules(int, char *, int, char *, struct pfr_buffer *);
178 int pfctl_optimize_rules(struct pfctl *);
179
180 int pfctl_add_rule(struct pfctl *, struct pf_rule *, const char *);
181 int pfctl_add_altq(struct pfctl *, struct pf_altq *);
182 int pfctl_add_pool(struct pfctl *, struct pf_pool *, sa_family_t);
183 void pfctl_clear_pool(struct pf_pool *);
184
185 int pfctl_set_timeout(struct pfctl *, const char *, int, int);
186 int pfctl_set_optimization(struct pfctl *, const char *);
187 int pfctl_set_limit(struct pfctl *, const char *, unsigned int);
188 int pfctl_set_logif(struct pfctl *, char *);
189 int pfctl_set_hostid(struct pfctl *, u_int32_t);
190 int pfctl_set_debug(struct pfctl *, char *);
191
192 int parse_rules(FILE *, struct pfctl *);
193 int parse_flags(char *);
194 int pfctl_load_anchors(int, int, struct pfr_buffer *);
195
196 void print_pool(struct pf_pool *, u_int16_t, u_int16_t, sa_family_t, int);
197 void print_src_node(struct pf_src_node *, int);
198 void print_rule(struct pf_rule *, const char *, int);
199 void print_tabledef(const char *, int, int, struct node_tinithead *);
200 void print_status(struct pf_status *, int);
201
202 int eval_pfaltq(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
203 struct node_queue_opt *);
204 int eval_pfqueue(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
205 struct node_queue_opt *);
206
207 void print_altq(const struct pf_altq *, unsigned, struct node_queue_bw *,
208 struct node_queue_opt *);
209 void print_queue(const struct pf_altq *, unsigned, struct node_queue_bw *,
210 int, struct node_queue_opt *);
211
212 int pfctl_define_table(char *, int, int, const char *, struct pfr_buffer *,
213 u_int32_t);
214
215 void pfctl_clear_fingerprints(int, int);
216 int pfctl_file_fingerprints(int, int, const char *);
217 pf_osfp_t pfctl_get_fingerprint(const char *);
218 int pfctl_load_fingerprints(int, int);
219 char *pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t);
220 void pfctl_show_fingerprints(int);
221
222
223 struct icmptypeent {
224 const char *name;
225 u_int8_t type;
226 };
227
228 struct icmpcodeent {
229 const char *name;
230 u_int8_t type;
231 u_int8_t code;
232 };
233
234 const struct icmptypeent *geticmptypebynumber(u_int8_t, u_int8_t);
235 const struct icmptypeent *geticmptypebyname(char *, u_int8_t);
236 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, u_int8_t);
237 const struct icmpcodeent *geticmpcodebyname(u_long, char *, u_int8_t);
238
239 struct pf_timeout {
240 const char *name;
241 int timeout;
242 };
243
244 #define PFCTL_FLAG_FILTER 0x02
245 #define PFCTL_FLAG_NAT 0x04
246 #define PFCTL_FLAG_OPTION 0x08
247 #define PFCTL_FLAG_ALTQ 0x10
248 #define PFCTL_FLAG_TABLE 0x20
249
250 extern const struct pf_timeout pf_timeouts[];
251
252 void set_ipmask(struct node_host *, u_int8_t);
253 int check_netmask(struct node_host *, sa_family_t);
254 int unmask(struct pf_addr *, sa_family_t);
255 void ifa_load(void);
256 struct node_host *ifa_exists(const char *, int);
257 struct node_host *ifa_lookup(const char *, int);
258 struct node_host *host(const char *);
259
260 int append_addr(struct pfr_buffer *, char *, int);
261 int append_addr_host(struct pfr_buffer *,
262 struct node_host *, int, int);
263
264 #endif /* _PFCTL_PARSER_H_ */
265