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) id_entry; 81 RB_ENTRY(pfctl_statelim) nm_entry; 82 }; 83 84 RB_HEAD(pfctl_statelim_ids, pfctl_statelim); 85 RB_HEAD(pfctl_statelim_nms, pfctl_statelim); 86 87 struct pfctl_sourcelim { 88 struct pfctl_source_lim ioc; 89 RB_ENTRY(pfctl_sourcelim) id_entry; 90 RB_ENTRY(pfctl_sourcelim) nm_entry; 91 }; 92 93 RB_HEAD(pfctl_sourcelim_ids, pfctl_sourcelim); 94 RB_HEAD(pfctl_sourcelim_nms, pfctl_sourcelim); 95 96 struct pfctl { 97 int dev; 98 struct pfctl_handle *h; 99 int opts; 100 int optimize; 101 int loadopt; 102 int asd; /* anchor stack depth */ 103 int bn; /* brace number */ 104 int tdirty; /* kernel dirty */ 105 #define PFCTL_ANCHOR_STACK_DEPTH 64 106 struct pfctl_anchor *astack[PFCTL_ANCHOR_STACK_DEPTH]; 107 struct pfioc_pooladdr paddr; 108 struct pfioc_altq *paltq; 109 struct pfioc_queue *pqueue; 110 struct pfr_buffer *trans; 111 struct pfctl_anchor *anchor, *alast; 112 struct pfr_ktablehead pfr_ktlast; 113 int eth_nr; 114 struct pfctl_eth_anchor *eanchor, *ealast; 115 struct pfctl_eth_anchor *eastack[PFCTL_ANCHOR_STACK_DEPTH]; 116 u_int32_t eth_ticket; 117 const char *ruleset; 118 119 struct pfctl_statelim_ids statelim_ids; 120 struct pfctl_statelim_nms statelim_nms; 121 struct pfctl_sourcelim_ids sourcelim_ids; 122 struct pfctl_sourcelim_nms sourcelim_nms; 123 124 /* 'set foo' options */ 125 u_int32_t timeout[PFTM_MAX]; 126 u_int32_t limit[PF_LIMIT_MAX]; 127 u_int32_t debug; 128 u_int32_t hostid; 129 u_int32_t reassemble; 130 char *ifname; 131 bool keep_counters; 132 u_int8_t syncookies; 133 u_int8_t syncookieswat[2]; /* lowat, highwat, in % */ 134 u_int8_t syncookieswat_set; 135 136 u_int8_t timeout_set[PFTM_MAX]; 137 u_int8_t limit_set[PF_LIMIT_MAX]; 138 u_int8_t debug_set; 139 u_int8_t hostid_set; 140 u_int8_t ifname_set; 141 u_int8_t reass_set; 142 }; 143 144 struct node_if { 145 char ifname[IFNAMSIZ]; 146 u_int8_t not; 147 u_int8_t dynamic; /* antispoof */ 148 u_int ifa_flags; 149 struct node_if *next; 150 struct node_if *tail; 151 }; 152 153 struct node_host { 154 struct pf_addr_wrap addr; 155 struct pf_addr bcast; 156 struct pf_addr peer; 157 sa_family_t af; 158 u_int8_t not; 159 u_int32_t ifindex; /* link-local IPv6 addrs */ 160 char *ifname; 161 u_int ifa_flags; 162 struct node_host *next; 163 struct node_host *tail; 164 }; 165 166 void freehostlist(struct node_host *); 167 168 struct node_mac { 169 u_int8_t mac[ETHER_ADDR_LEN]; 170 u_int8_t mask[ETHER_ADDR_LEN]; 171 bool neg; 172 bool isset; 173 struct node_mac *next; 174 struct node_mac *tail; 175 }; 176 177 struct node_os { 178 char *os; 179 pf_osfp_t fingerprint; 180 struct node_os *next; 181 struct node_os *tail; 182 }; 183 184 struct node_queue_bw { 185 u_int64_t bw_absolute; 186 u_int16_t bw_percent; 187 }; 188 189 struct node_hfsc_sc { 190 struct node_queue_bw m1; /* slope of 1st segment; bps */ 191 u_int d; /* x-projection of m1; msec */ 192 struct node_queue_bw m2; /* slope of 2nd segment; bps */ 193 u_int8_t used; 194 }; 195 196 struct node_hfsc_opts { 197 struct node_hfsc_sc realtime; 198 struct node_hfsc_sc linkshare; 199 struct node_hfsc_sc upperlimit; 200 int flags; 201 }; 202 203 struct node_fairq_sc { 204 struct node_queue_bw m1; /* slope of 1st segment; bps */ 205 u_int d; /* x-projection of m1; msec */ 206 struct node_queue_bw m2; /* slope of 2nd segment; bps */ 207 u_int8_t used; 208 }; 209 210 struct node_fairq_opts { 211 struct node_fairq_sc linkshare; 212 struct node_queue_bw hogs_bw; 213 u_int nbuckets; 214 int flags; 215 }; 216 217 struct node_queue_opt { 218 int qtype; 219 union { 220 struct cbq_opts cbq_opts; 221 struct codel_opts codel_opts; 222 struct priq_opts priq_opts; 223 struct node_hfsc_opts hfsc_opts; 224 struct node_fairq_opts fairq_opts; 225 } data; 226 }; 227 228 #define QPRI_BITSET_SIZE 256 229 __BITSET_DEFINE(qpri_bitset, QPRI_BITSET_SIZE); 230 LIST_HEAD(gen_sc, segment); 231 232 struct pfctl_altq { 233 struct pf_altq pa; 234 struct { 235 STAILQ_ENTRY(pfctl_altq) link; 236 u_int64_t bwsum; 237 struct qpri_bitset qpris; 238 int children; 239 int root_classes; 240 int default_classes; 241 struct gen_sc lssc; 242 struct gen_sc rtsc; 243 } meta; 244 }; 245 246 struct pfctl_watermarks { 247 uint32_t hi; 248 uint32_t lo; 249 }; 250 251 #ifdef __FreeBSD__ 252 /* 253 * XXX 254 * Absolutely this is not correct location to define this. 255 * Should we use an another sperate header file? 256 */ 257 #define SIMPLEQ_HEAD STAILQ_HEAD 258 #define SIMPLEQ_HEAD_INITIALIZER STAILQ_HEAD_INITIALIZER 259 #define SIMPLEQ_ENTRY STAILQ_ENTRY 260 #define SIMPLEQ_FIRST STAILQ_FIRST 261 #define SIMPLEQ_END(head) NULL 262 #define SIMPLEQ_EMPTY STAILQ_EMPTY 263 #define SIMPLEQ_NEXT STAILQ_NEXT 264 /*#define SIMPLEQ_FOREACH STAILQ_FOREACH*/ 265 #define SIMPLEQ_FOREACH(var, head, field) \ 266 for((var) = SIMPLEQ_FIRST(head); \ 267 (var) != SIMPLEQ_END(head); \ 268 (var) = SIMPLEQ_NEXT(var, field)) 269 #define SIMPLEQ_INIT STAILQ_INIT 270 #define SIMPLEQ_INSERT_HEAD STAILQ_INSERT_HEAD 271 #define SIMPLEQ_INSERT_TAIL STAILQ_INSERT_TAIL 272 #define SIMPLEQ_INSERT_AFTER STAILQ_INSERT_AFTER 273 #define SIMPLEQ_REMOVE_HEAD STAILQ_REMOVE_HEAD 274 #endif 275 SIMPLEQ_HEAD(node_tinithead, node_tinit); 276 struct node_tinit { /* table initializer */ 277 SIMPLEQ_ENTRY(node_tinit) entries; 278 struct node_host *host; 279 char *file; 280 }; 281 282 283 /* optimizer created tables */ 284 struct pf_opt_tbl { 285 char pt_name[PF_TABLE_NAME_SIZE]; 286 int pt_rulecount; 287 int pt_generated; 288 uint32_t pt_refcnt; 289 struct node_tinithead pt_nodes; 290 struct pfr_buffer *pt_buf; 291 }; 292 293 /* optimizer pf_rule container */ 294 struct pf_opt_rule { 295 struct pfctl_rule por_rule; 296 struct pf_opt_tbl *por_src_tbl; 297 struct pf_opt_tbl *por_dst_tbl; 298 u_int64_t por_profile_count; 299 TAILQ_ENTRY(pf_opt_rule) por_entry; 300 TAILQ_ENTRY(pf_opt_rule) por_skip_entry[PF_SKIP_COUNT]; 301 }; 302 303 TAILQ_HEAD(pf_opt_queue, pf_opt_rule); 304 305 struct pfr_uktable; 306 307 void copy_satopfaddr(struct pf_addr *, struct sockaddr *); 308 309 int pfctl_rules(int, char *, int, int, char *, struct pfr_buffer *); 310 int pfctl_optimize_ruleset(struct pfctl *, struct pfctl_ruleset *); 311 312 void pfctl_init_rule(struct pfctl_rule *r); 313 void pfctl_append_rule(struct pfctl *, struct pfctl_rule *); 314 int pfctl_append_eth_rule(struct pfctl *, struct pfctl_eth_rule *, 315 const char *); 316 int pfctl_add_altq(struct pfctl *, struct pf_altq *); 317 int pfctl_add_pool(struct pfctl *, struct pfctl_pool *, int); 318 void pfctl_move_pool(struct pfctl_pool *, struct pfctl_pool *); 319 void pfctl_clear_pool(struct pfctl_pool *); 320 321 int pfctl_add_statelim(struct pfctl *, struct pfctl_statelim *); 322 struct pfctl_statelim * 323 pfctl_get_statelim_id(struct pfctl *, uint32_t); 324 struct pfctl_statelim * 325 pfctl_get_statelim_nm(struct pfctl *, const char *); 326 int pfctl_add_sourcelim(struct pfctl *, struct pfctl_sourcelim *); 327 struct pfctl_sourcelim * 328 pfctl_get_sourcelim_id(struct pfctl *, uint32_t); 329 struct pfctl_sourcelim * 330 pfctl_get_sourcelim_nm(struct pfctl *, const char *); 331 332 int pfctl_apply_timeout(struct pfctl *, const char *, int, int); 333 int pfctl_set_reassembly(struct pfctl *, int, int); 334 int pfctl_set_optimization(struct pfctl *, const char *); 335 int pfctl_apply_limit(struct pfctl *, const char *, unsigned int); 336 int pfctl_set_logif(struct pfctl *, char *); 337 void pfctl_set_hostid(struct pfctl *, u_int32_t); 338 int pfctl_do_set_debug(struct pfctl *, char *); 339 int pfctl_set_interface_flags(struct pfctl *, char *, int, int); 340 int pfctl_cfg_syncookies(struct pfctl *, uint8_t, struct pfctl_watermarks *); 341 342 int parse_config(char *, struct pfctl *); 343 int parse_flags(char *); 344 int pfctl_load_anchors(int, struct pfctl *); 345 346 void print_pool(struct pfctl_pool *, u_int16_t, u_int16_t, int); 347 void print_src_node(struct pfctl_src_node *, int); 348 void print_statelim(const struct pfctl_state_lim *); 349 void print_sourcelim(const struct pfctl_source_lim *); 350 void print_eth_rule(struct pfctl_eth_rule *, const char *, int); 351 void print_rule(struct pfctl_rule *, const char *, int, int); 352 void print_tabledef(const char *, int, int, struct node_tinithead *); 353 void print_status(struct pfctl_status *, struct pfctl_syncookies *, int); 354 void print_running(struct pfctl_status *); 355 356 int eval_pfaltq(struct pfctl *, struct pf_altq *, struct node_queue_bw *, 357 struct node_queue_opt *); 358 int eval_pfqueue(struct pfctl *, struct pf_altq *, struct node_queue_bw *, 359 struct node_queue_opt *); 360 361 void print_altq(const struct pf_altq *, unsigned, struct node_queue_bw *, 362 struct node_queue_opt *); 363 void print_queue(const struct pf_altq *, unsigned, struct node_queue_bw *, 364 int, struct node_queue_opt *); 365 366 int pfctl_define_table(char *, int, int, const char *, struct pfr_buffer *, 367 u_int32_t, struct pfr_uktable *); 368 369 void pfctl_clear_fingerprints(int, int); 370 int pfctl_file_fingerprints(int, int, const char *); 371 pf_osfp_t pfctl_get_fingerprint(const char *); 372 int pfctl_load_fingerprints(int, int); 373 char *pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t); 374 void pfctl_show_fingerprints(int); 375 376 377 struct icmptypeent { 378 const char *name; 379 u_int8_t type; 380 }; 381 382 struct icmpcodeent { 383 const char *name; 384 u_int8_t type; 385 u_int8_t code; 386 }; 387 388 const struct icmptypeent *geticmptypebynumber(u_int8_t, sa_family_t); 389 const struct icmptypeent *geticmptypebyname(char *, sa_family_t); 390 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, sa_family_t); 391 const struct icmpcodeent *geticmpcodebyname(u_long, char *, sa_family_t); 392 393 struct pf_timeout { 394 const char *name; 395 int timeout; 396 }; 397 398 #define PFCTL_FLAG_FILTER 0x02 399 #define PFCTL_FLAG_NAT 0x04 400 #define PFCTL_FLAG_OPTION 0x08 401 #define PFCTL_FLAG_ALTQ 0x10 402 #define PFCTL_FLAG_TABLE 0x20 403 #define PFCTL_FLAG_ETH 0x40 404 405 extern const struct pf_timeout pf_timeouts[]; 406 407 void set_ipmask(struct node_host *, int); 408 int check_netmask(struct node_host *, sa_family_t); 409 int unmask(struct pf_addr *); 410 struct node_host *gen_dynnode(struct node_host *, sa_family_t); 411 void ifa_load(void); 412 unsigned int ifa_nametoindex(const char *); 413 char *ifa_indextoname(unsigned int, char *); 414 int get_query_socket(void); 415 struct node_host *ifa_exists(char *); 416 struct node_host *ifa_grouplookup(char *ifa_name, int flags); 417 struct node_host *ifa_lookup(char *, int); 418 struct node_host *host(const char *, int); 419 420 int append_addr(struct pfr_buffer *, char *, int, int); 421 int append_addr_host(struct pfr_buffer *, 422 struct node_host *, int, int); 423 int pfr_ktable_compare(struct pfr_ktable *, 424 struct pfr_ktable *); 425 RB_PROTOTYPE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare); 426 427 #endif /* _PFCTL_PARSER_H_ */ 428