1 /* $OpenBSD: pfctl_parser.h,v 1.86 2006/10/31 23:46:25 mcbride Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2001 Daniel Hartmeier 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * - Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * - Redistributions in binary form must reproduce the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer in the documentation and/or other materials provided 18 * with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #ifndef _PFCTL_PARSER_H_ 35 #define _PFCTL_PARSER_H_ 36 37 #include <libpfctl.h> 38 39 #include <pfctl.h> 40 41 #define PF_OSFP_FILE "/etc/pf.os" 42 43 #define PF_OPT_DISABLE 0x00001 44 #define PF_OPT_ENABLE 0x00002 45 #define PF_OPT_VERBOSE 0x00004 46 #define PF_OPT_NOACTION 0x00008 47 #define PF_OPT_QUIET 0x00010 48 #define PF_OPT_CLRRULECTRS 0x00020 49 #define PF_OPT_USEDNS 0x00040 50 #define PF_OPT_VERBOSE2 0x00080 51 #define PF_OPT_DUMMYACTION 0x00100 52 #define PF_OPT_DEBUG 0x00200 53 #define PF_OPT_SHOWALL 0x00400 54 #define PF_OPT_OPTIMIZE 0x00800 55 #define PF_OPT_NUMERIC 0x01000 56 #define PF_OPT_MERGE 0x02000 57 #define PF_OPT_RECURSE 0x04000 58 #define PF_OPT_KILLMATCH 0x08000 59 #define PF_OPT_NODNS 0x10000 60 #define PF_OPT_IGNFAIL 0x20000 61 #define PF_OPT_CALLSHOW 0x40000 62 63 #define PF_NAT_PROXY_PORT_LOW 50001 64 #define PF_NAT_PROXY_PORT_HIGH 65535 65 66 #define PF_OPTIMIZE_BASIC 0x0001 67 #define PF_OPTIMIZE_PROFILE 0x0002 68 69 #define FCNT_NAMES { \ 70 "searches", \ 71 "inserts", \ 72 "removals", \ 73 NULL \ 74 } 75 76 struct pfr_buffer; /* forward definition */ 77 78 struct pfctl_statelim { 79 struct pfctl_state_lim ioc; 80 RB_ENTRY(pfctl_statelim) entry; 81 }; 82 83 RB_HEAD(pfctl_statelim_ids, pfctl_statelim); 84 RB_HEAD(pfctl_statelim_nms, pfctl_statelim); 85 86 struct pfctl_sourcelim { 87 struct pfctl_source_lim ioc; 88 RB_ENTRY(pfctl_sourcelim) entry; 89 }; 90 91 RB_HEAD(pfctl_sourcelim_ids, pfctl_sourcelim); 92 RB_HEAD(pfctl_sourcelim_nms, pfctl_sourcelim); 93 94 struct pfctl { 95 int dev; 96 struct pfctl_handle *h; 97 int opts; 98 int optimize; 99 int loadopt; 100 int asd; /* anchor stack depth */ 101 int bn; /* brace number */ 102 int tdirty; /* kernel dirty */ 103 #define PFCTL_ANCHOR_STACK_DEPTH 64 104 struct pfctl_anchor *astack[PFCTL_ANCHOR_STACK_DEPTH]; 105 struct pfioc_pooladdr paddr; 106 struct pfioc_altq *paltq; 107 struct pfioc_queue *pqueue; 108 struct pfr_buffer *trans; 109 struct pfctl_anchor *anchor, *alast; 110 struct pfr_ktablehead pfr_ktlast; 111 int eth_nr; 112 struct pfctl_eth_anchor *eanchor, *ealast; 113 struct pfctl_eth_anchor *eastack[PFCTL_ANCHOR_STACK_DEPTH]; 114 u_int32_t eth_ticket; 115 const char *ruleset; 116 117 struct pfctl_statelim_ids statelim_ids; 118 struct pfctl_statelim_nms statelim_nms; 119 struct pfctl_sourcelim_ids sourcelim_ids; 120 struct pfctl_sourcelim_nms sourcelim_nms; 121 122 /* 'set foo' options */ 123 u_int32_t timeout[PFTM_MAX]; 124 u_int32_t limit[PF_LIMIT_MAX]; 125 u_int32_t debug; 126 u_int32_t hostid; 127 u_int32_t reassemble; 128 char *ifname; 129 bool keep_counters; 130 u_int8_t syncookies; 131 u_int8_t syncookieswat[2]; /* lowat, highwat, in % */ 132 u_int8_t syncookieswat_set; 133 134 u_int8_t timeout_set[PFTM_MAX]; 135 u_int8_t limit_set[PF_LIMIT_MAX]; 136 u_int8_t debug_set; 137 u_int8_t hostid_set; 138 u_int8_t ifname_set; 139 u_int8_t reass_set; 140 }; 141 142 struct node_if { 143 char ifname[IFNAMSIZ]; 144 u_int8_t not; 145 u_int8_t dynamic; /* antispoof */ 146 u_int ifa_flags; 147 struct node_if *next; 148 struct node_if *tail; 149 }; 150 151 struct node_host { 152 struct pf_addr_wrap addr; 153 struct pf_addr bcast; 154 struct pf_addr peer; 155 sa_family_t af; 156 u_int8_t not; 157 u_int32_t ifindex; /* link-local IPv6 addrs */ 158 char *ifname; 159 u_int ifa_flags; 160 struct node_host *next; 161 struct node_host *tail; 162 }; 163 164 void freehostlist(struct node_host *); 165 166 struct node_mac { 167 u_int8_t mac[ETHER_ADDR_LEN]; 168 u_int8_t mask[ETHER_ADDR_LEN]; 169 bool neg; 170 bool isset; 171 struct node_mac *next; 172 struct node_mac *tail; 173 }; 174 175 struct node_os { 176 char *os; 177 pf_osfp_t fingerprint; 178 struct node_os *next; 179 struct node_os *tail; 180 }; 181 182 struct node_queue_bw { 183 u_int64_t bw_absolute; 184 u_int16_t bw_percent; 185 }; 186 187 struct node_hfsc_sc { 188 struct node_queue_bw m1; /* slope of 1st segment; bps */ 189 u_int d; /* x-projection of m1; msec */ 190 struct node_queue_bw m2; /* slope of 2nd segment; bps */ 191 u_int8_t used; 192 }; 193 194 struct node_hfsc_opts { 195 struct node_hfsc_sc realtime; 196 struct node_hfsc_sc linkshare; 197 struct node_hfsc_sc upperlimit; 198 int flags; 199 }; 200 201 struct node_fairq_sc { 202 struct node_queue_bw m1; /* slope of 1st segment; bps */ 203 u_int d; /* x-projection of m1; msec */ 204 struct node_queue_bw m2; /* slope of 2nd segment; bps */ 205 u_int8_t used; 206 }; 207 208 struct node_fairq_opts { 209 struct node_fairq_sc linkshare; 210 struct node_queue_bw hogs_bw; 211 u_int nbuckets; 212 int flags; 213 }; 214 215 struct node_queue_opt { 216 int qtype; 217 union { 218 struct cbq_opts cbq_opts; 219 struct codel_opts codel_opts; 220 struct priq_opts priq_opts; 221 struct node_hfsc_opts hfsc_opts; 222 struct node_fairq_opts fairq_opts; 223 } data; 224 }; 225 226 #define QPRI_BITSET_SIZE 256 227 __BITSET_DEFINE(qpri_bitset, QPRI_BITSET_SIZE); 228 LIST_HEAD(gen_sc, segment); 229 230 struct pfctl_altq { 231 struct pf_altq pa; 232 struct { 233 STAILQ_ENTRY(pfctl_altq) link; 234 u_int64_t bwsum; 235 struct qpri_bitset qpris; 236 int children; 237 int root_classes; 238 int default_classes; 239 struct gen_sc lssc; 240 struct gen_sc rtsc; 241 } meta; 242 }; 243 244 struct pfctl_watermarks { 245 uint32_t hi; 246 uint32_t lo; 247 }; 248 249 #ifdef __FreeBSD__ 250 /* 251 * XXX 252 * Absolutely this is not correct location to define this. 253 * Should we use an another sperate header file? 254 */ 255 #define SIMPLEQ_HEAD STAILQ_HEAD 256 #define SIMPLEQ_HEAD_INITIALIZER STAILQ_HEAD_INITIALIZER 257 #define SIMPLEQ_ENTRY STAILQ_ENTRY 258 #define SIMPLEQ_FIRST STAILQ_FIRST 259 #define SIMPLEQ_END(head) NULL 260 #define SIMPLEQ_EMPTY STAILQ_EMPTY 261 #define SIMPLEQ_NEXT STAILQ_NEXT 262 /*#define SIMPLEQ_FOREACH STAILQ_FOREACH*/ 263 #define SIMPLEQ_FOREACH(var, head, field) \ 264 for((var) = SIMPLEQ_FIRST(head); \ 265 (var) != SIMPLEQ_END(head); \ 266 (var) = SIMPLEQ_NEXT(var, field)) 267 #define SIMPLEQ_INIT STAILQ_INIT 268 #define SIMPLEQ_INSERT_HEAD STAILQ_INSERT_HEAD 269 #define SIMPLEQ_INSERT_TAIL STAILQ_INSERT_TAIL 270 #define SIMPLEQ_INSERT_AFTER STAILQ_INSERT_AFTER 271 #define SIMPLEQ_REMOVE_HEAD STAILQ_REMOVE_HEAD 272 #endif 273 SIMPLEQ_HEAD(node_tinithead, node_tinit); 274 struct node_tinit { /* table initializer */ 275 SIMPLEQ_ENTRY(node_tinit) entries; 276 struct node_host *host; 277 char *file; 278 }; 279 280 281 /* optimizer created tables */ 282 struct pf_opt_tbl { 283 char pt_name[PF_TABLE_NAME_SIZE]; 284 int pt_rulecount; 285 int pt_generated; 286 uint32_t pt_refcnt; 287 struct node_tinithead pt_nodes; 288 struct pfr_buffer *pt_buf; 289 }; 290 291 /* optimizer pf_rule container */ 292 struct pf_opt_rule { 293 struct pfctl_rule por_rule; 294 struct pf_opt_tbl *por_src_tbl; 295 struct pf_opt_tbl *por_dst_tbl; 296 u_int64_t por_profile_count; 297 TAILQ_ENTRY(pf_opt_rule) por_entry; 298 TAILQ_ENTRY(pf_opt_rule) por_skip_entry[PF_SKIP_COUNT]; 299 }; 300 301 TAILQ_HEAD(pf_opt_queue, pf_opt_rule); 302 303 struct pfr_uktable; 304 305 void copy_satopfaddr(struct pf_addr *, struct sockaddr *); 306 307 int pfctl_rules(int, char *, int, int, char *, struct pfr_buffer *); 308 int pfctl_optimize_ruleset(struct pfctl *, struct pfctl_ruleset *); 309 310 void pfctl_init_rule(struct pfctl_rule *r); 311 void pfctl_append_rule(struct pfctl *, struct pfctl_rule *); 312 int pfctl_append_eth_rule(struct pfctl *, struct pfctl_eth_rule *, 313 const char *); 314 int pfctl_add_altq(struct pfctl *, struct pf_altq *); 315 int pfctl_add_pool(struct pfctl *, struct pfctl_pool *, int); 316 void pfctl_move_pool(struct pfctl_pool *, struct pfctl_pool *); 317 void pfctl_clear_pool(struct pfctl_pool *); 318 319 int pfctl_add_statelim(struct pfctl *, struct pfctl_statelim *); 320 struct pfctl_statelim * 321 pfctl_get_statelim_id(struct pfctl *, uint32_t); 322 struct pfctl_statelim * 323 pfctl_get_statelim_nm(struct pfctl *, const char *); 324 int pfctl_add_sourcelim(struct pfctl *, struct pfctl_sourcelim *); 325 struct pfctl_sourcelim * 326 pfctl_get_sourcelim_id(struct pfctl *, uint32_t); 327 struct pfctl_sourcelim * 328 pfctl_get_sourcelim_nm(struct pfctl *, const char *); 329 330 int pfctl_apply_timeout(struct pfctl *, const char *, int, int); 331 int pfctl_set_reassembly(struct pfctl *, int, int); 332 int pfctl_set_optimization(struct pfctl *, const char *); 333 int pfctl_apply_limit(struct pfctl *, const char *, unsigned int); 334 int pfctl_set_logif(struct pfctl *, char *); 335 void pfctl_set_hostid(struct pfctl *, u_int32_t); 336 int pfctl_do_set_debug(struct pfctl *, char *); 337 int pfctl_set_interface_flags(struct pfctl *, char *, int, int); 338 int pfctl_cfg_syncookies(struct pfctl *, uint8_t, struct pfctl_watermarks *); 339 340 int parse_config(char *, struct pfctl *); 341 int parse_flags(char *); 342 int pfctl_load_anchors(int, struct pfctl *); 343 344 void print_pool(struct pfctl_pool *, u_int16_t, u_int16_t, int); 345 void print_src_node(struct pfctl_src_node *, int); 346 void print_statelim(const struct pfctl_state_lim *); 347 void print_sourcelim(const struct pfctl_source_lim *); 348 void print_eth_rule(struct pfctl_eth_rule *, const char *, int); 349 void print_rule(struct pfctl_rule *, const char *, int, int); 350 void print_tabledef(const char *, int, int, struct node_tinithead *); 351 void print_status(struct pfctl_status *, struct pfctl_syncookies *, int); 352 void print_running(struct pfctl_status *); 353 354 int eval_pfaltq(struct pfctl *, struct pf_altq *, struct node_queue_bw *, 355 struct node_queue_opt *); 356 int eval_pfqueue(struct pfctl *, struct pf_altq *, struct node_queue_bw *, 357 struct node_queue_opt *); 358 359 void print_altq(const struct pf_altq *, unsigned, struct node_queue_bw *, 360 struct node_queue_opt *); 361 void print_queue(const struct pf_altq *, unsigned, struct node_queue_bw *, 362 int, struct node_queue_opt *); 363 364 int pfctl_define_table(char *, int, int, const char *, struct pfr_buffer *, 365 u_int32_t, struct pfr_uktable *); 366 367 void pfctl_clear_fingerprints(int, int); 368 int pfctl_file_fingerprints(int, int, const char *); 369 pf_osfp_t pfctl_get_fingerprint(const char *); 370 int pfctl_load_fingerprints(int, int); 371 char *pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t); 372 void pfctl_show_fingerprints(int); 373 374 375 struct icmptypeent { 376 const char *name; 377 u_int8_t type; 378 }; 379 380 struct icmpcodeent { 381 const char *name; 382 u_int8_t type; 383 u_int8_t code; 384 }; 385 386 const struct icmptypeent *geticmptypebynumber(u_int8_t, sa_family_t); 387 const struct icmptypeent *geticmptypebyname(char *, sa_family_t); 388 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, sa_family_t); 389 const struct icmpcodeent *geticmpcodebyname(u_long, char *, sa_family_t); 390 391 struct pf_timeout { 392 const char *name; 393 int timeout; 394 }; 395 396 #define PFCTL_FLAG_FILTER 0x02 397 #define PFCTL_FLAG_NAT 0x04 398 #define PFCTL_FLAG_OPTION 0x08 399 #define PFCTL_FLAG_ALTQ 0x10 400 #define PFCTL_FLAG_TABLE 0x20 401 #define PFCTL_FLAG_ETH 0x40 402 403 extern const struct pf_timeout pf_timeouts[]; 404 405 void set_ipmask(struct node_host *, int); 406 int check_netmask(struct node_host *, sa_family_t); 407 int unmask(struct pf_addr *); 408 struct node_host *gen_dynnode(struct node_host *, sa_family_t); 409 void ifa_load(void); 410 unsigned int ifa_nametoindex(const char *); 411 char *ifa_indextoname(unsigned int, char *); 412 int get_query_socket(void); 413 struct node_host *ifa_exists(char *); 414 struct node_host *ifa_grouplookup(char *ifa_name, int flags); 415 struct node_host *ifa_lookup(char *, int); 416 struct node_host *host(const char *, int); 417 418 int append_addr(struct pfr_buffer *, char *, int, int); 419 int append_addr_host(struct pfr_buffer *, 420 struct node_host *, int, int); 421 int pfr_ktable_compare(struct pfr_ktable *, 422 struct pfr_ktable *); 423 RB_PROTOTYPE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare); 424 425 #endif /* _PFCTL_PARSER_H_ */ 426