]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/pf/pfctl/pfctl_parser.h
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / pf / pfctl / pfctl_parser.h
1 /*      $OpenBSD: pfctl_parser.h,v 1.86 2006/10/31 23:46:25 mcbride 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  * $FreeBSD$
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_MERGE            0x2000
52 #define PF_OPT_RECURSE          0x4000
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 loadopt;
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_pooladdr paddr;
84         struct pfioc_altq *paltq;
85         struct pfioc_queue *pqueue;
86         struct pfr_buffer *trans;
87         struct pf_anchor *anchor, *alast;
88         const char *ruleset;
89
90         /* 'set foo' options */
91         u_int32_t        timeout[PFTM_MAX];
92         u_int32_t        limit[PF_LIMIT_MAX];
93         u_int32_t        debug;
94         u_int32_t        hostid;
95         char            *ifname;
96
97         u_int8_t         timeout_set[PFTM_MAX];
98         u_int8_t         limit_set[PF_LIMIT_MAX];
99         u_int8_t         debug_set;
100         u_int8_t         hostid_set;
101         u_int8_t         ifname_set;
102 };
103
104 struct node_if {
105         char                     ifname[IFNAMSIZ];
106         u_int8_t                 not;
107         u_int8_t                 dynamic; /* antispoof */
108         u_int                    ifa_flags;
109         struct node_if          *next;
110         struct node_if          *tail;
111 };
112
113 struct node_host {
114         struct pf_addr_wrap      addr;
115         struct pf_addr           bcast;
116         struct pf_addr           peer;
117         sa_family_t              af;
118         u_int8_t                 not;
119         u_int32_t                ifindex;       /* link-local IPv6 addrs */
120         char                    *ifname;
121         u_int                    ifa_flags;
122         struct node_host        *next;
123         struct node_host        *tail;
124 };
125
126 struct node_os {
127         char                    *os;
128         pf_osfp_t                fingerprint;
129         struct node_os          *next;
130         struct node_os          *tail;
131 };
132
133 struct node_queue_bw {
134         u_int32_t       bw_absolute;
135         u_int16_t       bw_percent;
136 };
137
138 struct node_hfsc_sc {
139         struct node_queue_bw    m1;     /* slope of 1st segment; bps */
140         u_int                   d;      /* x-projection of m1; msec */
141         struct node_queue_bw    m2;     /* slope of 2nd segment; bps */
142         u_int8_t                used;
143 };
144
145 struct node_hfsc_opts {
146         struct node_hfsc_sc     realtime;
147         struct node_hfsc_sc     linkshare;
148         struct node_hfsc_sc     upperlimit;
149         int                     flags;
150 };
151
152 struct node_queue_opt {
153         int                      qtype;
154         union {
155                 struct cbq_opts         cbq_opts;
156                 struct priq_opts        priq_opts;
157                 struct node_hfsc_opts   hfsc_opts;
158         }                        data;
159 };
160
161 #ifdef __FreeBSD__
162 /*
163  * XXX
164  * Absolutely this is not correct location to define this.
165  * Should we use an another sperate header file?
166  */
167 #define SIMPLEQ_HEAD                    STAILQ_HEAD
168 #define SIMPLEQ_HEAD_INITIALIZER        STAILQ_HEAD_INITIALIZER
169 #define SIMPLEQ_ENTRY                   STAILQ_ENTRY
170 #define SIMPLEQ_FIRST                   STAILQ_FIRST
171 #define SIMPLEQ_END(head)               NULL
172 #define SIMPLEQ_EMPTY                   STAILQ_EMPTY
173 #define SIMPLEQ_NEXT                    STAILQ_NEXT
174 /*#define SIMPLEQ_FOREACH               STAILQ_FOREACH*/
175 #define SIMPLEQ_FOREACH(var, head, field)       \
176     for((var) = SIMPLEQ_FIRST(head);            \
177         (var) != SIMPLEQ_END(head);             \
178         (var) = SIMPLEQ_NEXT(var, field))
179 #define SIMPLEQ_INIT                    STAILQ_INIT
180 #define SIMPLEQ_INSERT_HEAD             STAILQ_INSERT_HEAD
181 #define SIMPLEQ_INSERT_TAIL             STAILQ_INSERT_TAIL
182 #define SIMPLEQ_INSERT_AFTER            STAILQ_INSERT_AFTER
183 #define SIMPLEQ_REMOVE_HEAD             STAILQ_REMOVE_HEAD
184 #endif
185 SIMPLEQ_HEAD(node_tinithead, node_tinit);
186 struct node_tinit {     /* table initializer */
187         SIMPLEQ_ENTRY(node_tinit)        entries;
188         struct node_host                *host;
189         char                            *file;
190 };
191
192
193 /* optimizer created tables */
194 struct pf_opt_tbl {
195         char                     pt_name[PF_TABLE_NAME_SIZE];
196         int                      pt_rulecount;
197         int                      pt_generated;
198         struct node_tinithead    pt_nodes;
199         struct pfr_buffer       *pt_buf;
200 };
201 #define PF_OPT_TABLE_PREFIX     "__automatic_"
202
203 /* optimizer pf_rule container */
204 struct pf_opt_rule {
205         struct pf_rule           por_rule;
206         struct pf_opt_tbl       *por_src_tbl;
207         struct pf_opt_tbl       *por_dst_tbl;
208         u_int64_t                por_profile_count;
209         TAILQ_ENTRY(pf_opt_rule) por_entry;
210         TAILQ_ENTRY(pf_opt_rule) por_skip_entry[PF_SKIP_COUNT];
211 };
212
213 TAILQ_HEAD(pf_opt_queue, pf_opt_rule);
214
215 int     pfctl_rules(int, char *, FILE *, int, int, char *, struct pfr_buffer *);
216 int     pfctl_optimize_ruleset(struct pfctl *, struct pf_ruleset *);
217
218 int     pfctl_add_rule(struct pfctl *, struct pf_rule *, const char *);
219 int     pfctl_add_altq(struct pfctl *, struct pf_altq *);
220 int     pfctl_add_pool(struct pfctl *, struct pf_pool *, sa_family_t);
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_optimization(struct pfctl *, const char *);
226 int     pfctl_set_limit(struct pfctl *, const char *, unsigned int);
227 int     pfctl_set_logif(struct pfctl *, char *);
228 int     pfctl_set_hostid(struct pfctl *, u_int32_t);
229 int     pfctl_set_debug(struct pfctl *, char *);
230 int     pfctl_set_interface_flags(struct pfctl *, char *, int, int);
231
232 int     parse_rules(FILE *, struct pfctl *);
233 int     parse_flags(char *);
234 int     pfctl_load_anchors(int, struct pfctl *, struct pfr_buffer *);
235
236 void    print_pool(struct pf_pool *, u_int16_t, u_int16_t, sa_family_t, int);
237 void    print_src_node(struct pf_src_node *, int);
238 void    print_rule(struct pf_rule *, const char *, int);
239 void    print_tabledef(const char *, int, int, struct node_tinithead *);
240 void    print_status(struct pf_status *, int);
241
242 int     eval_pfaltq(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
243             struct node_queue_opt *);
244 int     eval_pfqueue(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
245             struct node_queue_opt *);
246
247 void     print_altq(const struct pf_altq *, unsigned, struct node_queue_bw *,
248             struct node_queue_opt *);
249 void     print_queue(const struct pf_altq *, unsigned, struct node_queue_bw *,
250             int, struct node_queue_opt *);
251
252 int     pfctl_define_table(char *, int, int, const char *, struct pfr_buffer *,
253             u_int32_t);
254
255 void             pfctl_clear_fingerprints(int, int);
256 int              pfctl_file_fingerprints(int, int, const char *);
257 pf_osfp_t        pfctl_get_fingerprint(const char *);
258 int              pfctl_load_fingerprints(int, int);
259 char            *pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t);
260 void             pfctl_show_fingerprints(int);
261
262
263 struct icmptypeent {
264         const char *name;
265         u_int8_t type;
266 };
267
268 struct icmpcodeent {
269         const char *name;
270         u_int8_t type;
271         u_int8_t code;
272 };
273
274 const struct icmptypeent *geticmptypebynumber(u_int8_t, u_int8_t);
275 const struct icmptypeent *geticmptypebyname(char *, u_int8_t);
276 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, u_int8_t);
277 const struct icmpcodeent *geticmpcodebyname(u_long, char *, u_int8_t);
278
279 struct pf_timeout {
280         const char      *name;
281         int              timeout;
282 };
283
284 #define PFCTL_FLAG_FILTER       0x02
285 #define PFCTL_FLAG_NAT          0x04
286 #define PFCTL_FLAG_OPTION       0x08
287 #define PFCTL_FLAG_ALTQ         0x10
288 #define PFCTL_FLAG_TABLE        0x20
289
290 extern const struct pf_timeout pf_timeouts[];
291
292 void                     set_ipmask(struct node_host *, u_int8_t);
293 int                      check_netmask(struct node_host *, sa_family_t);
294 int                      unmask(struct pf_addr *, sa_family_t);
295 void                     ifa_load(void);
296 struct node_host        *ifa_exists(const char *);
297 struct node_host        *ifa_lookup(const char *, int);
298 struct node_host        *host(const char *);
299
300 int                      append_addr(struct pfr_buffer *, char *, int);
301 int                      append_addr_host(struct pfr_buffer *,
302                             struct node_host *, int, int);
303
304 #endif /* _PFCTL_PARSER_H_ */