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 * $FreeBSD$ 34 */ 35 36 #ifndef _PFCTL_PARSER_H_ 37 #define _PFCTL_PARSER_H_ 38 39 #include <libpfctl.h> 40 41 #define PF_OSFP_FILE "/etc/pf.os" 42 43 #define PF_OPT_DISABLE 0x0001 44 #define PF_OPT_ENABLE 0x0002 45 #define PF_OPT_VERBOSE 0x0004 46 #define PF_OPT_NOACTION 0x0008 47 #define PF_OPT_QUIET 0x0010 48 #define PF_OPT_CLRRULECTRS 0x0020 49 #define PF_OPT_USEDNS 0x0040 50 #define PF_OPT_VERBOSE2 0x0080 51 #define PF_OPT_DUMMYACTION 0x0100 52 #define PF_OPT_DEBUG 0x0200 53 #define PF_OPT_SHOWALL 0x0400 54 #define PF_OPT_OPTIMIZE 0x0800 55 #define PF_OPT_NUMERIC 0x1000 56 #define PF_OPT_MERGE 0x2000 57 #define PF_OPT_RECURSE 0x4000 58 #define PF_OPT_KILLMATCH 0x8000 59 60 #define PF_TH_ALL 0xFF 61 62 #define PF_NAT_PROXY_PORT_LOW 50001 63 #define PF_NAT_PROXY_PORT_HIGH 65535 64 65 #define PF_OPTIMIZE_BASIC 0x0001 66 #define PF_OPTIMIZE_PROFILE 0x0002 67 68 #define FCNT_NAMES { \ 69 "searches", \ 70 "inserts", \ 71 "removals", \ 72 NULL \ 73 } 74 75 struct pfr_buffer; /* forward definition */ 76 77 78 struct pfctl { 79 int dev; 80 int opts; 81 int optimize; 82 int loadopt; 83 int asd; /* anchor stack depth */ 84 int bn; /* brace number */ 85 int tdirty; /* kernel dirty */ 86 #define PFCTL_ANCHOR_STACK_DEPTH 64 87 struct pfctl_anchor *astack[PFCTL_ANCHOR_STACK_DEPTH]; 88 struct pfioc_pooladdr paddr; 89 struct pfioc_altq *paltq; 90 struct pfioc_queue *pqueue; 91 struct pfr_buffer *trans; 92 struct pfctl_anchor *anchor, *alast; 93 int eth_nr; 94 struct pfctl_eth_anchor *eanchor, *ealast; 95 struct pfctl_eth_anchor *eastack[PFCTL_ANCHOR_STACK_DEPTH]; 96 u_int32_t eth_ticket; 97 const char *ruleset; 98 99 /* 'set foo' options */ 100 u_int32_t timeout[PFTM_MAX]; 101 u_int32_t limit[PF_LIMIT_MAX]; 102 u_int32_t debug; 103 u_int32_t hostid; 104 char *ifname; 105 bool keep_counters; 106 u_int8_t syncookies; 107 u_int8_t syncookieswat[2]; /* lowat, highwat, in % */ 108 u_int8_t syncookieswat_set; 109 110 u_int8_t timeout_set[PFTM_MAX]; 111 u_int8_t limit_set[PF_LIMIT_MAX]; 112 u_int8_t debug_set; 113 u_int8_t hostid_set; 114 u_int8_t ifname_set; 115 }; 116 117 struct node_if { 118 char ifname[IFNAMSIZ]; 119 u_int8_t not; 120 u_int8_t dynamic; /* antispoof */ 121 u_int ifa_flags; 122 struct node_if *next; 123 struct node_if *tail; 124 }; 125 126 struct node_host { 127 struct pf_addr_wrap addr; 128 struct pf_addr bcast; 129 struct pf_addr peer; 130 sa_family_t af; 131 u_int8_t not; 132 u_int32_t ifindex; /* link-local IPv6 addrs */ 133 char *ifname; 134 u_int ifa_flags; 135 struct node_host *next; 136 struct node_host *tail; 137 }; 138 139 struct node_mac { 140 u_int8_t mac[ETHER_ADDR_LEN]; 141 bool neg; 142 struct node_mac *next; 143 struct node_mac *tail; 144 }; 145 146 struct node_os { 147 char *os; 148 pf_osfp_t fingerprint; 149 struct node_os *next; 150 struct node_os *tail; 151 }; 152 153 struct node_queue_bw { 154 u_int64_t bw_absolute; 155 u_int16_t bw_percent; 156 }; 157 158 struct node_hfsc_sc { 159 struct node_queue_bw m1; /* slope of 1st segment; bps */ 160 u_int d; /* x-projection of m1; msec */ 161 struct node_queue_bw m2; /* slope of 2nd segment; bps */ 162 u_int8_t used; 163 }; 164 165 struct node_hfsc_opts { 166 struct node_hfsc_sc realtime; 167 struct node_hfsc_sc linkshare; 168 struct node_hfsc_sc upperlimit; 169 int flags; 170 }; 171 172 struct node_fairq_sc { 173 struct node_queue_bw m1; /* slope of 1st segment; bps */ 174 u_int d; /* x-projection of m1; msec */ 175 struct node_queue_bw m2; /* slope of 2nd segment; bps */ 176 u_int8_t used; 177 }; 178 179 struct node_fairq_opts { 180 struct node_fairq_sc linkshare; 181 struct node_queue_bw hogs_bw; 182 u_int nbuckets; 183 int flags; 184 }; 185 186 struct node_queue_opt { 187 int qtype; 188 union { 189 struct cbq_opts cbq_opts; 190 struct codel_opts codel_opts; 191 struct priq_opts priq_opts; 192 struct node_hfsc_opts hfsc_opts; 193 struct node_fairq_opts fairq_opts; 194 } data; 195 }; 196 197 #define QPRI_BITSET_SIZE 256 198 __BITSET_DEFINE(qpri_bitset, QPRI_BITSET_SIZE); 199 LIST_HEAD(gen_sc, segment); 200 201 struct pfctl_altq { 202 struct pf_altq pa; 203 struct { 204 STAILQ_ENTRY(pfctl_altq) link; 205 u_int64_t bwsum; 206 struct qpri_bitset qpris; 207 int children; 208 int root_classes; 209 int default_classes; 210 struct gen_sc lssc; 211 struct gen_sc rtsc; 212 } meta; 213 }; 214 215 struct pfctl_watermarks { 216 uint32_t hi; 217 uint32_t lo; 218 }; 219 220 #ifdef __FreeBSD__ 221 /* 222 * XXX 223 * Absolutely this is not correct location to define this. 224 * Should we use an another sperate header file? 225 */ 226 #define SIMPLEQ_HEAD STAILQ_HEAD 227 #define SIMPLEQ_HEAD_INITIALIZER STAILQ_HEAD_INITIALIZER 228 #define SIMPLEQ_ENTRY STAILQ_ENTRY 229 #define SIMPLEQ_FIRST STAILQ_FIRST 230 #define SIMPLEQ_END(head) NULL 231 #define SIMPLEQ_EMPTY STAILQ_EMPTY 232 #define SIMPLEQ_NEXT STAILQ_NEXT 233 /*#define SIMPLEQ_FOREACH STAILQ_FOREACH*/ 234 #define SIMPLEQ_FOREACH(var, head, field) \ 235 for((var) = SIMPLEQ_FIRST(head); \ 236 (var) != SIMPLEQ_END(head); \ 237 (var) = SIMPLEQ_NEXT(var, field)) 238 #define SIMPLEQ_INIT STAILQ_INIT 239 #define SIMPLEQ_INSERT_HEAD STAILQ_INSERT_HEAD 240 #define SIMPLEQ_INSERT_TAIL STAILQ_INSERT_TAIL 241 #define SIMPLEQ_INSERT_AFTER STAILQ_INSERT_AFTER 242 #define SIMPLEQ_REMOVE_HEAD STAILQ_REMOVE_HEAD 243 #endif 244 SIMPLEQ_HEAD(node_tinithead, node_tinit); 245 struct node_tinit { /* table initializer */ 246 SIMPLEQ_ENTRY(node_tinit) entries; 247 struct node_host *host; 248 char *file; 249 }; 250 251 252 /* optimizer created tables */ 253 struct pf_opt_tbl { 254 char pt_name[PF_TABLE_NAME_SIZE]; 255 int pt_rulecount; 256 int pt_generated; 257 struct node_tinithead pt_nodes; 258 struct pfr_buffer *pt_buf; 259 }; 260 #define PF_OPT_TABLE_PREFIX "__automatic_" 261 262 /* optimizer pf_rule container */ 263 struct pf_opt_rule { 264 struct pfctl_rule por_rule; 265 struct pf_opt_tbl *por_src_tbl; 266 struct pf_opt_tbl *por_dst_tbl; 267 u_int64_t por_profile_count; 268 TAILQ_ENTRY(pf_opt_rule) por_entry; 269 TAILQ_ENTRY(pf_opt_rule) por_skip_entry[PF_SKIP_COUNT]; 270 }; 271 272 TAILQ_HEAD(pf_opt_queue, pf_opt_rule); 273 274 int pfctl_rules(int, char *, int, int, char *, struct pfr_buffer *); 275 int pfctl_optimize_ruleset(struct pfctl *, struct pfctl_ruleset *); 276 277 int pfctl_append_rule(struct pfctl *, struct pfctl_rule *, const char *); 278 int pfctl_append_eth_rule(struct pfctl *, struct pfctl_eth_rule *, 279 const char *); 280 int pfctl_add_altq(struct pfctl *, struct pf_altq *); 281 int pfctl_add_pool(struct pfctl *, struct pfctl_pool *, sa_family_t); 282 void pfctl_move_pool(struct pfctl_pool *, struct pfctl_pool *); 283 void pfctl_clear_pool(struct pfctl_pool *); 284 285 int pfctl_set_timeout(struct pfctl *, const char *, int, int); 286 int pfctl_set_optimization(struct pfctl *, const char *); 287 int pfctl_set_limit(struct pfctl *, const char *, unsigned int); 288 int pfctl_set_logif(struct pfctl *, char *); 289 int pfctl_set_hostid(struct pfctl *, u_int32_t); 290 int pfctl_set_debug(struct pfctl *, char *); 291 int pfctl_set_interface_flags(struct pfctl *, char *, int, int); 292 int pfctl_cfg_syncookies(struct pfctl *, uint8_t, struct pfctl_watermarks *); 293 294 int parse_config(char *, struct pfctl *); 295 int parse_flags(char *); 296 int pfctl_load_anchors(int, struct pfctl *, struct pfr_buffer *); 297 298 void print_pool(struct pfctl_pool *, u_int16_t, u_int16_t, sa_family_t, int); 299 void print_src_node(struct pf_src_node *, int); 300 void print_eth_rule(struct pfctl_eth_rule *, const char *, int); 301 void print_rule(struct pfctl_rule *, const char *, int, int); 302 void print_tabledef(const char *, int, int, struct node_tinithead *); 303 void print_status(struct pfctl_status *, struct pfctl_syncookies *, int); 304 void print_running(struct pfctl_status *); 305 306 int eval_pfaltq(struct pfctl *, struct pf_altq *, struct node_queue_bw *, 307 struct node_queue_opt *); 308 int eval_pfqueue(struct pfctl *, struct pf_altq *, struct node_queue_bw *, 309 struct node_queue_opt *); 310 311 void print_altq(const struct pf_altq *, unsigned, struct node_queue_bw *, 312 struct node_queue_opt *); 313 void print_queue(const struct pf_altq *, unsigned, struct node_queue_bw *, 314 int, struct node_queue_opt *); 315 316 int pfctl_define_table(char *, int, int, const char *, struct pfr_buffer *, 317 u_int32_t); 318 319 void pfctl_clear_fingerprints(int, int); 320 int pfctl_file_fingerprints(int, int, const char *); 321 pf_osfp_t pfctl_get_fingerprint(const char *); 322 int pfctl_load_fingerprints(int, int); 323 char *pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t); 324 void pfctl_show_fingerprints(int); 325 326 327 struct icmptypeent { 328 const char *name; 329 u_int8_t type; 330 }; 331 332 struct icmpcodeent { 333 const char *name; 334 u_int8_t type; 335 u_int8_t code; 336 }; 337 338 const struct icmptypeent *geticmptypebynumber(u_int8_t, u_int8_t); 339 const struct icmptypeent *geticmptypebyname(char *, u_int8_t); 340 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, u_int8_t); 341 const struct icmpcodeent *geticmpcodebyname(u_long, char *, u_int8_t); 342 343 struct pf_timeout { 344 const char *name; 345 int timeout; 346 }; 347 348 #define PFCTL_FLAG_FILTER 0x02 349 #define PFCTL_FLAG_NAT 0x04 350 #define PFCTL_FLAG_OPTION 0x08 351 #define PFCTL_FLAG_ALTQ 0x10 352 #define PFCTL_FLAG_TABLE 0x20 353 #define PFCTL_FLAG_ETH 0x40 354 355 extern const struct pf_timeout pf_timeouts[]; 356 357 void set_ipmask(struct node_host *, u_int8_t); 358 int check_netmask(struct node_host *, sa_family_t); 359 int unmask(struct pf_addr *, sa_family_t); 360 void ifa_load(void); 361 int get_query_socket(void); 362 struct node_host *ifa_exists(char *); 363 struct node_host *ifa_grouplookup(char *ifa_name, int flags); 364 struct node_host *ifa_lookup(char *, int); 365 struct node_host *host(const char *); 366 367 int append_addr(struct pfr_buffer *, char *, int); 368 int append_addr_host(struct pfr_buffer *, 369 struct node_host *, int, int); 370 371 #endif /* _PFCTL_PARSER_H_ */ 372