1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 * $OpenBSD: pfvar.h,v 1.282 2009/01/29 15:12:28 pyr Exp $ 32 * $FreeBSD$ 33 */ 34 35 #ifndef _NET_PFVAR_H_ 36 #define _NET_PFVAR_H_ 37 38 #include <sys/param.h> 39 #include <sys/queue.h> 40 #include <sys/counter.h> 41 #include <sys/cpuset.h> 42 #include <sys/malloc.h> 43 #include <sys/refcount.h> 44 #include <sys/lock.h> 45 #include <sys/rmlock.h> 46 #include <sys/tree.h> 47 #include <vm/uma.h> 48 49 #include <net/radix.h> 50 #include <netinet/in.h> 51 52 #include <netpfil/pf/pf.h> 53 #include <netpfil/pf/pf_altq.h> 54 #include <netpfil/pf/pf_mtag.h> 55 56 struct pf_addr { 57 union { 58 struct in_addr v4; 59 struct in6_addr v6; 60 u_int8_t addr8[16]; 61 u_int16_t addr16[8]; 62 u_int32_t addr32[4]; 63 } pfa; /* 128-bit address */ 64 #define v4 pfa.v4 65 #define v6 pfa.v6 66 #define addr8 pfa.addr8 67 #define addr16 pfa.addr16 68 #define addr32 pfa.addr32 69 }; 70 71 #define PFI_AFLAG_NETWORK 0x01 72 #define PFI_AFLAG_BROADCAST 0x02 73 #define PFI_AFLAG_PEER 0x04 74 #define PFI_AFLAG_MODEMASK 0x07 75 #define PFI_AFLAG_NOALIAS 0x08 76 77 struct pf_addr_wrap { 78 union { 79 struct { 80 struct pf_addr addr; 81 struct pf_addr mask; 82 } a; 83 char ifname[IFNAMSIZ]; 84 char tblname[PF_TABLE_NAME_SIZE]; 85 } v; 86 union { 87 struct pfi_dynaddr *dyn; 88 struct pfr_ktable *tbl; 89 int dyncnt; 90 int tblcnt; 91 } p; 92 u_int8_t type; /* PF_ADDR_* */ 93 u_int8_t iflags; /* PFI_AFLAG_* */ 94 }; 95 96 #ifdef _KERNEL 97 98 struct pfi_dynaddr { 99 TAILQ_ENTRY(pfi_dynaddr) entry; 100 struct pf_addr pfid_addr4; 101 struct pf_addr pfid_mask4; 102 struct pf_addr pfid_addr6; 103 struct pf_addr pfid_mask6; 104 struct pfr_ktable *pfid_kt; 105 struct pfi_kif *pfid_kif; 106 int pfid_net; /* mask or 128 */ 107 int pfid_acnt4; /* address count IPv4 */ 108 int pfid_acnt6; /* address count IPv6 */ 109 sa_family_t pfid_af; /* rule af */ 110 u_int8_t pfid_iflags; /* PFI_AFLAG_* */ 111 }; 112 113 /* 114 * Address manipulation macros 115 */ 116 #define HTONL(x) (x) = htonl((__uint32_t)(x)) 117 #define HTONS(x) (x) = htons((__uint16_t)(x)) 118 #define NTOHL(x) (x) = ntohl((__uint32_t)(x)) 119 #define NTOHS(x) (x) = ntohs((__uint16_t)(x)) 120 121 #define PF_NAME "pf" 122 123 #define PF_HASHROW_ASSERT(h) mtx_assert(&(h)->lock, MA_OWNED) 124 #define PF_HASHROW_LOCK(h) mtx_lock(&(h)->lock) 125 #define PF_HASHROW_UNLOCK(h) mtx_unlock(&(h)->lock) 126 127 #define PF_STATE_LOCK(s) \ 128 do { \ 129 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(s)]; \ 130 PF_HASHROW_LOCK(_ih); \ 131 } while (0) 132 133 #define PF_STATE_UNLOCK(s) \ 134 do { \ 135 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH((s))]; \ 136 PF_HASHROW_UNLOCK(_ih); \ 137 } while (0) 138 139 #ifdef INVARIANTS 140 #define PF_STATE_LOCK_ASSERT(s) \ 141 do { \ 142 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(s)]; \ 143 PF_HASHROW_ASSERT(_ih); \ 144 } while (0) 145 #else /* !INVARIANTS */ 146 #define PF_STATE_LOCK_ASSERT(s) do {} while (0) 147 #endif /* INVARIANTS */ 148 149 extern struct mtx pf_unlnkdrules_mtx; 150 #define PF_UNLNKDRULES_LOCK() mtx_lock(&pf_unlnkdrules_mtx) 151 #define PF_UNLNKDRULES_UNLOCK() mtx_unlock(&pf_unlnkdrules_mtx) 152 153 extern struct rmlock pf_rules_lock; 154 #define PF_RULES_RLOCK_TRACKER struct rm_priotracker _pf_rules_tracker 155 #define PF_RULES_RLOCK() rm_rlock(&pf_rules_lock, &_pf_rules_tracker) 156 #define PF_RULES_RUNLOCK() rm_runlock(&pf_rules_lock, &_pf_rules_tracker) 157 #define PF_RULES_WLOCK() rm_wlock(&pf_rules_lock) 158 #define PF_RULES_WUNLOCK() rm_wunlock(&pf_rules_lock) 159 #define PF_RULES_ASSERT() rm_assert(&pf_rules_lock, RA_LOCKED) 160 #define PF_RULES_RASSERT() rm_assert(&pf_rules_lock, RA_RLOCKED) 161 #define PF_RULES_WASSERT() rm_assert(&pf_rules_lock, RA_WLOCKED) 162 163 extern struct sx pf_end_lock; 164 165 #define PF_MODVER 1 166 #define PFLOG_MODVER 1 167 #define PFSYNC_MODVER 1 168 169 #define PFLOG_MINVER 1 170 #define PFLOG_PREFVER PFLOG_MODVER 171 #define PFLOG_MAXVER 1 172 #define PFSYNC_MINVER 1 173 #define PFSYNC_PREFVER PFSYNC_MODVER 174 #define PFSYNC_MAXVER 1 175 176 #ifdef INET 177 #ifndef INET6 178 #define PF_INET_ONLY 179 #endif /* ! INET6 */ 180 #endif /* INET */ 181 182 #ifdef INET6 183 #ifndef INET 184 #define PF_INET6_ONLY 185 #endif /* ! INET */ 186 #endif /* INET6 */ 187 188 #ifdef INET 189 #ifdef INET6 190 #define PF_INET_INET6 191 #endif /* INET6 */ 192 #endif /* INET */ 193 194 #else 195 196 #define PF_INET_INET6 197 198 #endif /* _KERNEL */ 199 200 /* Both IPv4 and IPv6 */ 201 #ifdef PF_INET_INET6 202 203 #define PF_AEQ(a, b, c) \ 204 ((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \ 205 (c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \ 206 (a)->addr32[2] == (b)->addr32[2] && \ 207 (a)->addr32[1] == (b)->addr32[1] && \ 208 (a)->addr32[0] == (b)->addr32[0])) \ 209 210 #define PF_ANEQ(a, b, c) \ 211 ((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \ 212 (c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \ 213 (a)->addr32[1] != (b)->addr32[1] || \ 214 (a)->addr32[2] != (b)->addr32[2] || \ 215 (a)->addr32[3] != (b)->addr32[3]))) \ 216 217 #define PF_AZERO(a, c) \ 218 ((c == AF_INET && !(a)->addr32[0]) || \ 219 (c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \ 220 !(a)->addr32[2] && !(a)->addr32[3] )) \ 221 222 #define PF_MATCHA(n, a, m, b, f) \ 223 pf_match_addr(n, a, m, b, f) 224 225 #define PF_ACPY(a, b, f) \ 226 pf_addrcpy(a, b, f) 227 228 #define PF_AINC(a, f) \ 229 pf_addr_inc(a, f) 230 231 #define PF_POOLMASK(a, b, c, d, f) \ 232 pf_poolmask(a, b, c, d, f) 233 234 #else 235 236 /* Just IPv6 */ 237 238 #ifdef PF_INET6_ONLY 239 240 #define PF_AEQ(a, b, c) \ 241 ((a)->addr32[3] == (b)->addr32[3] && \ 242 (a)->addr32[2] == (b)->addr32[2] && \ 243 (a)->addr32[1] == (b)->addr32[1] && \ 244 (a)->addr32[0] == (b)->addr32[0]) \ 245 246 #define PF_ANEQ(a, b, c) \ 247 ((a)->addr32[3] != (b)->addr32[3] || \ 248 (a)->addr32[2] != (b)->addr32[2] || \ 249 (a)->addr32[1] != (b)->addr32[1] || \ 250 (a)->addr32[0] != (b)->addr32[0]) \ 251 252 #define PF_AZERO(a, c) \ 253 (!(a)->addr32[0] && \ 254 !(a)->addr32[1] && \ 255 !(a)->addr32[2] && \ 256 !(a)->addr32[3] ) \ 257 258 #define PF_MATCHA(n, a, m, b, f) \ 259 pf_match_addr(n, a, m, b, f) 260 261 #define PF_ACPY(a, b, f) \ 262 pf_addrcpy(a, b, f) 263 264 #define PF_AINC(a, f) \ 265 pf_addr_inc(a, f) 266 267 #define PF_POOLMASK(a, b, c, d, f) \ 268 pf_poolmask(a, b, c, d, f) 269 270 #else 271 272 /* Just IPv4 */ 273 #ifdef PF_INET_ONLY 274 275 #define PF_AEQ(a, b, c) \ 276 ((a)->addr32[0] == (b)->addr32[0]) 277 278 #define PF_ANEQ(a, b, c) \ 279 ((a)->addr32[0] != (b)->addr32[0]) 280 281 #define PF_AZERO(a, c) \ 282 (!(a)->addr32[0]) 283 284 #define PF_MATCHA(n, a, m, b, f) \ 285 pf_match_addr(n, a, m, b, f) 286 287 #define PF_ACPY(a, b, f) \ 288 (a)->v4.s_addr = (b)->v4.s_addr 289 290 #define PF_AINC(a, f) \ 291 do { \ 292 (a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \ 293 } while (0) 294 295 #define PF_POOLMASK(a, b, c, d, f) \ 296 do { \ 297 (a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \ 298 (((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \ 299 } while (0) 300 301 #endif /* PF_INET_ONLY */ 302 #endif /* PF_INET6_ONLY */ 303 #endif /* PF_INET_INET6 */ 304 305 /* 306 * XXX callers not FIB-aware in our version of pf yet. 307 * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio. 308 */ 309 #define PF_MISMATCHAW(aw, x, af, neg, ifp, rtid) \ 310 ( \ 311 (((aw)->type == PF_ADDR_NOROUTE && \ 312 pf_routable((x), (af), NULL, (rtid))) || \ 313 (((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL && \ 314 pf_routable((x), (af), (ifp), (rtid))) || \ 315 ((aw)->type == PF_ADDR_TABLE && \ 316 !pfr_match_addr((aw)->p.tbl, (x), (af))) || \ 317 ((aw)->type == PF_ADDR_DYNIFTL && \ 318 !pfi_match_addr((aw)->p.dyn, (x), (af))) || \ 319 ((aw)->type == PF_ADDR_RANGE && \ 320 !pf_match_addr_range(&(aw)->v.a.addr, \ 321 &(aw)->v.a.mask, (x), (af))) || \ 322 ((aw)->type == PF_ADDR_ADDRMASK && \ 323 !PF_AZERO(&(aw)->v.a.mask, (af)) && \ 324 !PF_MATCHA(0, &(aw)->v.a.addr, \ 325 &(aw)->v.a.mask, (x), (af))))) != \ 326 (neg) \ 327 ) 328 329 330 struct pf_rule_uid { 331 uid_t uid[2]; 332 u_int8_t op; 333 }; 334 335 struct pf_rule_gid { 336 uid_t gid[2]; 337 u_int8_t op; 338 }; 339 340 struct pf_rule_addr { 341 struct pf_addr_wrap addr; 342 u_int16_t port[2]; 343 u_int8_t neg; 344 u_int8_t port_op; 345 }; 346 347 struct pf_pooladdr { 348 struct pf_addr_wrap addr; 349 TAILQ_ENTRY(pf_pooladdr) entries; 350 char ifname[IFNAMSIZ]; 351 struct pfi_kif *kif; 352 }; 353 354 TAILQ_HEAD(pf_palist, pf_pooladdr); 355 356 struct pf_poolhashkey { 357 union { 358 u_int8_t key8[16]; 359 u_int16_t key16[8]; 360 u_int32_t key32[4]; 361 } pfk; /* 128-bit hash key */ 362 #define key8 pfk.key8 363 #define key16 pfk.key16 364 #define key32 pfk.key32 365 }; 366 367 struct pf_pool { 368 struct pf_palist list; 369 struct pf_pooladdr *cur; 370 struct pf_poolhashkey key; 371 struct pf_addr counter; 372 int tblidx; 373 u_int16_t proxy_port[2]; 374 u_int8_t opts; 375 }; 376 377 378 /* A packed Operating System description for fingerprinting */ 379 typedef u_int32_t pf_osfp_t; 380 #define PF_OSFP_ANY ((pf_osfp_t)0) 381 #define PF_OSFP_UNKNOWN ((pf_osfp_t)-1) 382 #define PF_OSFP_NOMATCH ((pf_osfp_t)-2) 383 384 struct pf_osfp_entry { 385 SLIST_ENTRY(pf_osfp_entry) fp_entry; 386 pf_osfp_t fp_os; 387 int fp_enflags; 388 #define PF_OSFP_EXPANDED 0x001 /* expanded entry */ 389 #define PF_OSFP_GENERIC 0x002 /* generic signature */ 390 #define PF_OSFP_NODETAIL 0x004 /* no p0f details */ 391 #define PF_OSFP_LEN 32 392 char fp_class_nm[PF_OSFP_LEN]; 393 char fp_version_nm[PF_OSFP_LEN]; 394 char fp_subtype_nm[PF_OSFP_LEN]; 395 }; 396 #define PF_OSFP_ENTRY_EQ(a, b) \ 397 ((a)->fp_os == (b)->fp_os && \ 398 memcmp((a)->fp_class_nm, (b)->fp_class_nm, PF_OSFP_LEN) == 0 && \ 399 memcmp((a)->fp_version_nm, (b)->fp_version_nm, PF_OSFP_LEN) == 0 && \ 400 memcmp((a)->fp_subtype_nm, (b)->fp_subtype_nm, PF_OSFP_LEN) == 0) 401 402 /* handle pf_osfp_t packing */ 403 #define _FP_RESERVED_BIT 1 /* For the special negative #defines */ 404 #define _FP_UNUSED_BITS 1 405 #define _FP_CLASS_BITS 10 /* OS Class (Windows, Linux) */ 406 #define _FP_VERSION_BITS 10 /* OS version (95, 98, NT, 2.4.54, 3.2) */ 407 #define _FP_SUBTYPE_BITS 10 /* patch level (NT SP4, SP3, ECN patch) */ 408 #define PF_OSFP_UNPACK(osfp, class, version, subtype) do { \ 409 (class) = ((osfp) >> (_FP_VERSION_BITS+_FP_SUBTYPE_BITS)) & \ 410 ((1 << _FP_CLASS_BITS) - 1); \ 411 (version) = ((osfp) >> _FP_SUBTYPE_BITS) & \ 412 ((1 << _FP_VERSION_BITS) - 1);\ 413 (subtype) = (osfp) & ((1 << _FP_SUBTYPE_BITS) - 1); \ 414 } while(0) 415 #define PF_OSFP_PACK(osfp, class, version, subtype) do { \ 416 (osfp) = ((class) & ((1 << _FP_CLASS_BITS) - 1)) << (_FP_VERSION_BITS \ 417 + _FP_SUBTYPE_BITS); \ 418 (osfp) |= ((version) & ((1 << _FP_VERSION_BITS) - 1)) << \ 419 _FP_SUBTYPE_BITS; \ 420 (osfp) |= (subtype) & ((1 << _FP_SUBTYPE_BITS) - 1); \ 421 } while(0) 422 423 /* the fingerprint of an OSes TCP SYN packet */ 424 typedef u_int64_t pf_tcpopts_t; 425 struct pf_os_fingerprint { 426 SLIST_HEAD(pf_osfp_enlist, pf_osfp_entry) fp_oses; /* list of matches */ 427 pf_tcpopts_t fp_tcpopts; /* packed TCP options */ 428 u_int16_t fp_wsize; /* TCP window size */ 429 u_int16_t fp_psize; /* ip->ip_len */ 430 u_int16_t fp_mss; /* TCP MSS */ 431 u_int16_t fp_flags; 432 #define PF_OSFP_WSIZE_MOD 0x0001 /* Window modulus */ 433 #define PF_OSFP_WSIZE_DC 0x0002 /* Window don't care */ 434 #define PF_OSFP_WSIZE_MSS 0x0004 /* Window multiple of MSS */ 435 #define PF_OSFP_WSIZE_MTU 0x0008 /* Window multiple of MTU */ 436 #define PF_OSFP_PSIZE_MOD 0x0010 /* packet size modulus */ 437 #define PF_OSFP_PSIZE_DC 0x0020 /* packet size don't care */ 438 #define PF_OSFP_WSCALE 0x0040 /* TCP window scaling */ 439 #define PF_OSFP_WSCALE_MOD 0x0080 /* TCP window scale modulus */ 440 #define PF_OSFP_WSCALE_DC 0x0100 /* TCP window scale dont-care */ 441 #define PF_OSFP_MSS 0x0200 /* TCP MSS */ 442 #define PF_OSFP_MSS_MOD 0x0400 /* TCP MSS modulus */ 443 #define PF_OSFP_MSS_DC 0x0800 /* TCP MSS dont-care */ 444 #define PF_OSFP_DF 0x1000 /* IPv4 don't fragment bit */ 445 #define PF_OSFP_TS0 0x2000 /* Zero timestamp */ 446 #define PF_OSFP_INET6 0x4000 /* IPv6 */ 447 u_int8_t fp_optcnt; /* TCP option count */ 448 u_int8_t fp_wscale; /* TCP window scaling */ 449 u_int8_t fp_ttl; /* IPv4 TTL */ 450 #define PF_OSFP_MAXTTL_OFFSET 40 451 /* TCP options packing */ 452 #define PF_OSFP_TCPOPT_NOP 0x0 /* TCP NOP option */ 453 #define PF_OSFP_TCPOPT_WSCALE 0x1 /* TCP window scaling option */ 454 #define PF_OSFP_TCPOPT_MSS 0x2 /* TCP max segment size opt */ 455 #define PF_OSFP_TCPOPT_SACK 0x3 /* TCP SACK OK option */ 456 #define PF_OSFP_TCPOPT_TS 0x4 /* TCP timestamp option */ 457 #define PF_OSFP_TCPOPT_BITS 3 /* bits used by each option */ 458 #define PF_OSFP_MAX_OPTS \ 459 (sizeof(((struct pf_os_fingerprint *)0)->fp_tcpopts) * 8) \ 460 / PF_OSFP_TCPOPT_BITS 461 462 SLIST_ENTRY(pf_os_fingerprint) fp_next; 463 }; 464 465 struct pf_osfp_ioctl { 466 struct pf_osfp_entry fp_os; 467 pf_tcpopts_t fp_tcpopts; /* packed TCP options */ 468 u_int16_t fp_wsize; /* TCP window size */ 469 u_int16_t fp_psize; /* ip->ip_len */ 470 u_int16_t fp_mss; /* TCP MSS */ 471 u_int16_t fp_flags; 472 u_int8_t fp_optcnt; /* TCP option count */ 473 u_int8_t fp_wscale; /* TCP window scaling */ 474 u_int8_t fp_ttl; /* IPv4 TTL */ 475 476 int fp_getnum; /* DIOCOSFPGET number */ 477 }; 478 479 480 union pf_rule_ptr { 481 struct pf_rule *ptr; 482 u_int32_t nr; 483 }; 484 485 #define PF_ANCHOR_NAME_SIZE 64 486 487 struct pf_rule { 488 struct pf_rule_addr src; 489 struct pf_rule_addr dst; 490 #define PF_SKIP_IFP 0 491 #define PF_SKIP_DIR 1 492 #define PF_SKIP_AF 2 493 #define PF_SKIP_PROTO 3 494 #define PF_SKIP_SRC_ADDR 4 495 #define PF_SKIP_SRC_PORT 5 496 #define PF_SKIP_DST_ADDR 6 497 #define PF_SKIP_DST_PORT 7 498 #define PF_SKIP_COUNT 8 499 union pf_rule_ptr skip[PF_SKIP_COUNT]; 500 #define PF_RULE_LABEL_SIZE 64 501 char label[PF_RULE_LABEL_SIZE]; 502 char ifname[IFNAMSIZ]; 503 char qname[PF_QNAME_SIZE]; 504 char pqname[PF_QNAME_SIZE]; 505 #define PF_TAG_NAME_SIZE 64 506 char tagname[PF_TAG_NAME_SIZE]; 507 char match_tagname[PF_TAG_NAME_SIZE]; 508 509 char overload_tblname[PF_TABLE_NAME_SIZE]; 510 511 TAILQ_ENTRY(pf_rule) entries; 512 struct pf_pool rpool; 513 514 u_int64_t evaluations; 515 u_int64_t packets[2]; 516 u_int64_t bytes[2]; 517 518 struct pfi_kif *kif; 519 struct pf_anchor *anchor; 520 struct pfr_ktable *overload_tbl; 521 522 pf_osfp_t os_fingerprint; 523 524 int rtableid; 525 u_int32_t timeout[PFTM_MAX]; 526 u_int32_t max_states; 527 u_int32_t max_src_nodes; 528 u_int32_t max_src_states; 529 u_int32_t max_src_conn; 530 struct { 531 u_int32_t limit; 532 u_int32_t seconds; 533 } max_src_conn_rate; 534 u_int32_t qid; 535 u_int32_t pqid; 536 u_int32_t rt_listid; 537 u_int32_t nr; 538 u_int32_t prob; 539 uid_t cuid; 540 pid_t cpid; 541 542 counter_u64_t states_cur; 543 counter_u64_t states_tot; 544 counter_u64_t src_nodes; 545 546 u_int16_t return_icmp; 547 u_int16_t return_icmp6; 548 u_int16_t max_mss; 549 u_int16_t tag; 550 u_int16_t match_tag; 551 u_int16_t scrub_flags; 552 553 struct pf_rule_uid uid; 554 struct pf_rule_gid gid; 555 556 u_int32_t rule_flag; 557 u_int8_t action; 558 u_int8_t direction; 559 u_int8_t log; 560 u_int8_t logif; 561 u_int8_t quick; 562 u_int8_t ifnot; 563 u_int8_t match_tag_not; 564 u_int8_t natpass; 565 566 #define PF_STATE_NORMAL 0x1 567 #define PF_STATE_MODULATE 0x2 568 #define PF_STATE_SYNPROXY 0x3 569 u_int8_t keep_state; 570 sa_family_t af; 571 u_int8_t proto; 572 u_int8_t type; 573 u_int8_t code; 574 u_int8_t flags; 575 u_int8_t flagset; 576 u_int8_t min_ttl; 577 u_int8_t allow_opts; 578 u_int8_t rt; 579 u_int8_t return_ttl; 580 u_int8_t tos; 581 u_int8_t set_tos; 582 u_int8_t anchor_relative; 583 u_int8_t anchor_wildcard; 584 585 #define PF_FLUSH 0x01 586 #define PF_FLUSH_GLOBAL 0x02 587 u_int8_t flush; 588 #define PF_PRIO_ZERO 0xff /* match "prio 0" packets */ 589 #define PF_PRIO_MAX 7 590 u_int8_t prio; 591 u_int8_t set_prio[2]; 592 593 struct { 594 struct pf_addr addr; 595 u_int16_t port; 596 } divert; 597 598 uint64_t u_states_cur; 599 uint64_t u_states_tot; 600 uint64_t u_src_nodes; 601 }; 602 603 /* rule flags */ 604 #define PFRULE_DROP 0x0000 605 #define PFRULE_RETURNRST 0x0001 606 #define PFRULE_FRAGMENT 0x0002 607 #define PFRULE_RETURNICMP 0x0004 608 #define PFRULE_RETURN 0x0008 609 #define PFRULE_NOSYNC 0x0010 610 #define PFRULE_SRCTRACK 0x0020 /* track source states */ 611 #define PFRULE_RULESRCTRACK 0x0040 /* per rule */ 612 #define PFRULE_REFS 0x0080 /* rule has references */ 613 614 /* scrub flags */ 615 #define PFRULE_NODF 0x0100 616 #define PFRULE_RANDOMID 0x0800 617 #define PFRULE_REASSEMBLE_TCP 0x1000 618 #define PFRULE_SET_TOS 0x2000 619 620 /* rule flags again */ 621 #define PFRULE_IFBOUND 0x00010000 /* if-bound */ 622 #define PFRULE_STATESLOPPY 0x00020000 /* sloppy state tracking */ 623 624 #define PFSTATE_HIWAT 100000 /* default state table size */ 625 #define PFSTATE_ADAPT_START 60000 /* default adaptive timeout start */ 626 #define PFSTATE_ADAPT_END 120000 /* default adaptive timeout end */ 627 628 629 struct pf_threshold { 630 u_int32_t limit; 631 #define PF_THRESHOLD_MULT 1000 632 #define PF_THRESHOLD_MAX 0xffffffff / PF_THRESHOLD_MULT 633 u_int32_t seconds; 634 u_int32_t count; 635 u_int32_t last; 636 }; 637 638 struct pf_src_node { 639 LIST_ENTRY(pf_src_node) entry; 640 struct pf_addr addr; 641 struct pf_addr raddr; 642 union pf_rule_ptr rule; 643 struct pfi_kif *kif; 644 u_int64_t bytes[2]; 645 u_int64_t packets[2]; 646 u_int32_t states; 647 u_int32_t conn; 648 struct pf_threshold conn_rate; 649 u_int32_t creation; 650 u_int32_t expire; 651 sa_family_t af; 652 u_int8_t ruletype; 653 }; 654 655 #define PFSNODE_HIWAT 10000 /* default source node table size */ 656 657 struct pf_state_scrub { 658 struct timeval pfss_last; /* time received last packet */ 659 u_int32_t pfss_tsecr; /* last echoed timestamp */ 660 u_int32_t pfss_tsval; /* largest timestamp */ 661 u_int32_t pfss_tsval0; /* original timestamp */ 662 u_int16_t pfss_flags; 663 #define PFSS_TIMESTAMP 0x0001 /* modulate timestamp */ 664 #define PFSS_PAWS 0x0010 /* stricter PAWS checks */ 665 #define PFSS_PAWS_IDLED 0x0020 /* was idle too long. no PAWS */ 666 #define PFSS_DATA_TS 0x0040 /* timestamp on data packets */ 667 #define PFSS_DATA_NOTS 0x0080 /* no timestamp on data packets */ 668 u_int8_t pfss_ttl; /* stashed TTL */ 669 u_int8_t pad; 670 u_int32_t pfss_ts_mod; /* timestamp modulation */ 671 }; 672 673 struct pf_state_host { 674 struct pf_addr addr; 675 u_int16_t port; 676 u_int16_t pad; 677 }; 678 679 struct pf_state_peer { 680 struct pf_state_scrub *scrub; /* state is scrubbed */ 681 u_int32_t seqlo; /* Max sequence number sent */ 682 u_int32_t seqhi; /* Max the other end ACKd + win */ 683 u_int32_t seqdiff; /* Sequence number modulator */ 684 u_int16_t max_win; /* largest window (pre scaling) */ 685 u_int16_t mss; /* Maximum segment size option */ 686 u_int8_t state; /* active state level */ 687 u_int8_t wscale; /* window scaling factor */ 688 u_int8_t tcp_est; /* Did we reach TCPS_ESTABLISHED */ 689 u_int8_t pad[1]; 690 }; 691 692 /* Keep synced with struct pf_state_key. */ 693 struct pf_state_key_cmp { 694 struct pf_addr addr[2]; 695 u_int16_t port[2]; 696 sa_family_t af; 697 u_int8_t proto; 698 u_int8_t pad[2]; 699 }; 700 701 struct pf_state_key { 702 struct pf_addr addr[2]; 703 u_int16_t port[2]; 704 sa_family_t af; 705 u_int8_t proto; 706 u_int8_t pad[2]; 707 708 LIST_ENTRY(pf_state_key) entry; 709 TAILQ_HEAD(, pf_state) states[2]; 710 }; 711 712 /* Keep synced with struct pf_state. */ 713 struct pf_state_cmp { 714 u_int64_t id; 715 u_int32_t creatorid; 716 u_int8_t direction; 717 u_int8_t pad[3]; 718 }; 719 720 struct pf_state { 721 u_int64_t id; 722 u_int32_t creatorid; 723 u_int8_t direction; 724 u_int8_t pad[3]; 725 726 u_int refs; 727 TAILQ_ENTRY(pf_state) sync_list; 728 TAILQ_ENTRY(pf_state) key_list[2]; 729 LIST_ENTRY(pf_state) entry; 730 struct pf_state_peer src; 731 struct pf_state_peer dst; 732 union pf_rule_ptr rule; 733 union pf_rule_ptr anchor; 734 union pf_rule_ptr nat_rule; 735 struct pf_addr rt_addr; 736 struct pf_state_key *key[2]; /* addresses stack and wire */ 737 struct pfi_kif *kif; 738 struct pfi_kif *rt_kif; 739 struct pf_src_node *src_node; 740 struct pf_src_node *nat_src_node; 741 u_int64_t packets[2]; 742 u_int64_t bytes[2]; 743 u_int32_t creation; 744 u_int32_t expire; 745 u_int32_t pfsync_time; 746 u_int16_t tag; 747 u_int8_t log; 748 u_int8_t state_flags; 749 #define PFSTATE_ALLOWOPTS 0x01 750 #define PFSTATE_SLOPPY 0x02 751 /* was PFSTATE_PFLOW 0x04 */ 752 #define PFSTATE_NOSYNC 0x08 753 #define PFSTATE_ACK 0x10 754 #define PFSTATE_SETPRIO 0x0200 755 #define PFSTATE_SETMASK (PFSTATE_SETPRIO) 756 u_int8_t timeout; 757 u_int8_t sync_state; /* PFSYNC_S_x */ 758 759 /* XXX */ 760 u_int8_t sync_updates; 761 u_int8_t _tail[3]; 762 }; 763 764 /* 765 * Unified state structures for pulling states out of the kernel 766 * used by pfsync(4) and the pf(4) ioctl. 767 */ 768 struct pfsync_state_scrub { 769 u_int16_t pfss_flags; 770 u_int8_t pfss_ttl; /* stashed TTL */ 771 #define PFSYNC_SCRUB_FLAG_VALID 0x01 772 u_int8_t scrub_flag; 773 u_int32_t pfss_ts_mod; /* timestamp modulation */ 774 } __packed; 775 776 struct pfsync_state_peer { 777 struct pfsync_state_scrub scrub; /* state is scrubbed */ 778 u_int32_t seqlo; /* Max sequence number sent */ 779 u_int32_t seqhi; /* Max the other end ACKd + win */ 780 u_int32_t seqdiff; /* Sequence number modulator */ 781 u_int16_t max_win; /* largest window (pre scaling) */ 782 u_int16_t mss; /* Maximum segment size option */ 783 u_int8_t state; /* active state level */ 784 u_int8_t wscale; /* window scaling factor */ 785 u_int8_t pad[6]; 786 } __packed; 787 788 struct pfsync_state_key { 789 struct pf_addr addr[2]; 790 u_int16_t port[2]; 791 }; 792 793 struct pfsync_state { 794 u_int64_t id; 795 char ifname[IFNAMSIZ]; 796 struct pfsync_state_key key[2]; 797 struct pfsync_state_peer src; 798 struct pfsync_state_peer dst; 799 struct pf_addr rt_addr; 800 u_int32_t rule; 801 u_int32_t anchor; 802 u_int32_t nat_rule; 803 u_int32_t creation; 804 u_int32_t expire; 805 u_int32_t packets[2][2]; 806 u_int32_t bytes[2][2]; 807 u_int32_t creatorid; 808 sa_family_t af; 809 u_int8_t proto; 810 u_int8_t direction; 811 u_int8_t __spare[2]; 812 u_int8_t log; 813 u_int8_t state_flags; 814 u_int8_t timeout; 815 u_int8_t sync_flags; 816 u_int8_t updates; 817 } __packed; 818 819 #ifdef _KERNEL 820 /* pfsync */ 821 typedef int pfsync_state_import_t(struct pfsync_state *, u_int8_t); 822 typedef void pfsync_insert_state_t(struct pf_state *); 823 typedef void pfsync_update_state_t(struct pf_state *); 824 typedef void pfsync_delete_state_t(struct pf_state *); 825 typedef void pfsync_clear_states_t(u_int32_t, const char *); 826 typedef int pfsync_defer_t(struct pf_state *, struct mbuf *); 827 828 extern pfsync_state_import_t *pfsync_state_import_ptr; 829 extern pfsync_insert_state_t *pfsync_insert_state_ptr; 830 extern pfsync_update_state_t *pfsync_update_state_ptr; 831 extern pfsync_delete_state_t *pfsync_delete_state_ptr; 832 extern pfsync_clear_states_t *pfsync_clear_states_ptr; 833 extern pfsync_defer_t *pfsync_defer_ptr; 834 835 void pfsync_state_export(struct pfsync_state *, 836 struct pf_state *); 837 838 /* pflog */ 839 struct pf_ruleset; 840 struct pf_pdesc; 841 typedef int pflog_packet_t(struct pfi_kif *, struct mbuf *, sa_family_t, 842 u_int8_t, u_int8_t, struct pf_rule *, struct pf_rule *, 843 struct pf_ruleset *, struct pf_pdesc *, int); 844 extern pflog_packet_t *pflog_packet_ptr; 845 846 #endif /* _KERNEL */ 847 848 #define PFSYNC_FLAG_SRCNODE 0x04 849 #define PFSYNC_FLAG_NATSRCNODE 0x08 850 851 /* for copies to/from network byte order */ 852 /* ioctl interface also uses network byte order */ 853 #define pf_state_peer_hton(s,d) do { \ 854 (d)->seqlo = htonl((s)->seqlo); \ 855 (d)->seqhi = htonl((s)->seqhi); \ 856 (d)->seqdiff = htonl((s)->seqdiff); \ 857 (d)->max_win = htons((s)->max_win); \ 858 (d)->mss = htons((s)->mss); \ 859 (d)->state = (s)->state; \ 860 (d)->wscale = (s)->wscale; \ 861 if ((s)->scrub) { \ 862 (d)->scrub.pfss_flags = \ 863 htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP); \ 864 (d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl; \ 865 (d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\ 866 (d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID; \ 867 } \ 868 } while (0) 869 870 #define pf_state_peer_ntoh(s,d) do { \ 871 (d)->seqlo = ntohl((s)->seqlo); \ 872 (d)->seqhi = ntohl((s)->seqhi); \ 873 (d)->seqdiff = ntohl((s)->seqdiff); \ 874 (d)->max_win = ntohs((s)->max_win); \ 875 (d)->mss = ntohs((s)->mss); \ 876 (d)->state = (s)->state; \ 877 (d)->wscale = (s)->wscale; \ 878 if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && \ 879 (d)->scrub != NULL) { \ 880 (d)->scrub->pfss_flags = \ 881 ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP; \ 882 (d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl; \ 883 (d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\ 884 } \ 885 } while (0) 886 887 #define pf_state_counter_hton(s,d) do { \ 888 d[0] = htonl((s>>32)&0xffffffff); \ 889 d[1] = htonl(s&0xffffffff); \ 890 } while (0) 891 892 #define pf_state_counter_from_pfsync(s) \ 893 (((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1])) 894 895 #define pf_state_counter_ntoh(s,d) do { \ 896 d = ntohl(s[0]); \ 897 d = d<<32; \ 898 d += ntohl(s[1]); \ 899 } while (0) 900 901 TAILQ_HEAD(pf_rulequeue, pf_rule); 902 903 struct pf_anchor; 904 905 struct pf_ruleset { 906 struct { 907 struct pf_rulequeue queues[2]; 908 struct { 909 struct pf_rulequeue *ptr; 910 struct pf_rule **ptr_array; 911 u_int32_t rcount; 912 u_int32_t ticket; 913 int open; 914 } active, inactive; 915 } rules[PF_RULESET_MAX]; 916 struct pf_anchor *anchor; 917 u_int32_t tticket; 918 int tables; 919 int topen; 920 }; 921 922 RB_HEAD(pf_anchor_global, pf_anchor); 923 RB_HEAD(pf_anchor_node, pf_anchor); 924 struct pf_anchor { 925 RB_ENTRY(pf_anchor) entry_global; 926 RB_ENTRY(pf_anchor) entry_node; 927 struct pf_anchor *parent; 928 struct pf_anchor_node children; 929 char name[PF_ANCHOR_NAME_SIZE]; 930 char path[MAXPATHLEN]; 931 struct pf_ruleset ruleset; 932 int refcnt; /* anchor rules */ 933 int match; /* XXX: used for pfctl black magic */ 934 }; 935 RB_PROTOTYPE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare); 936 RB_PROTOTYPE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare); 937 938 #define PF_RESERVED_ANCHOR "_pf" 939 940 #define PFR_TFLAG_PERSIST 0x00000001 941 #define PFR_TFLAG_CONST 0x00000002 942 #define PFR_TFLAG_ACTIVE 0x00000004 943 #define PFR_TFLAG_INACTIVE 0x00000008 944 #define PFR_TFLAG_REFERENCED 0x00000010 945 #define PFR_TFLAG_REFDANCHOR 0x00000020 946 #define PFR_TFLAG_COUNTERS 0x00000040 947 /* Adjust masks below when adding flags. */ 948 #define PFR_TFLAG_USRMASK (PFR_TFLAG_PERSIST | \ 949 PFR_TFLAG_CONST | \ 950 PFR_TFLAG_COUNTERS) 951 #define PFR_TFLAG_SETMASK (PFR_TFLAG_ACTIVE | \ 952 PFR_TFLAG_INACTIVE | \ 953 PFR_TFLAG_REFERENCED | \ 954 PFR_TFLAG_REFDANCHOR) 955 #define PFR_TFLAG_ALLMASK (PFR_TFLAG_PERSIST | \ 956 PFR_TFLAG_CONST | \ 957 PFR_TFLAG_ACTIVE | \ 958 PFR_TFLAG_INACTIVE | \ 959 PFR_TFLAG_REFERENCED | \ 960 PFR_TFLAG_REFDANCHOR | \ 961 PFR_TFLAG_COUNTERS) 962 963 struct pf_anchor_stackframe; 964 965 struct pfr_table { 966 char pfrt_anchor[MAXPATHLEN]; 967 char pfrt_name[PF_TABLE_NAME_SIZE]; 968 u_int32_t pfrt_flags; 969 u_int8_t pfrt_fback; 970 }; 971 972 enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED, 973 PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE, 974 PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX }; 975 976 struct pfr_addr { 977 union { 978 struct in_addr _pfra_ip4addr; 979 struct in6_addr _pfra_ip6addr; 980 } pfra_u; 981 u_int8_t pfra_af; 982 u_int8_t pfra_net; 983 u_int8_t pfra_not; 984 u_int8_t pfra_fback; 985 }; 986 #define pfra_ip4addr pfra_u._pfra_ip4addr 987 #define pfra_ip6addr pfra_u._pfra_ip6addr 988 989 enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX }; 990 enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX }; 991 #define PFR_OP_XPASS PFR_OP_ADDR_MAX 992 993 struct pfr_astats { 994 struct pfr_addr pfras_a; 995 u_int64_t pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 996 u_int64_t pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 997 long pfras_tzero; 998 }; 999 1000 enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX }; 1001 1002 struct pfr_tstats { 1003 struct pfr_table pfrts_t; 1004 u_int64_t pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1005 u_int64_t pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1006 u_int64_t pfrts_match; 1007 u_int64_t pfrts_nomatch; 1008 long pfrts_tzero; 1009 int pfrts_cnt; 1010 int pfrts_refcnt[PFR_REFCNT_MAX]; 1011 }; 1012 #define pfrts_name pfrts_t.pfrt_name 1013 #define pfrts_flags pfrts_t.pfrt_flags 1014 1015 #ifndef _SOCKADDR_UNION_DEFINED 1016 #define _SOCKADDR_UNION_DEFINED 1017 union sockaddr_union { 1018 struct sockaddr sa; 1019 struct sockaddr_in sin; 1020 struct sockaddr_in6 sin6; 1021 }; 1022 #endif /* _SOCKADDR_UNION_DEFINED */ 1023 1024 struct pfr_kcounters { 1025 u_int64_t pfrkc_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 1026 u_int64_t pfrkc_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 1027 }; 1028 1029 SLIST_HEAD(pfr_kentryworkq, pfr_kentry); 1030 struct pfr_kentry { 1031 struct radix_node pfrke_node[2]; 1032 union sockaddr_union pfrke_sa; 1033 SLIST_ENTRY(pfr_kentry) pfrke_workq; 1034 struct pfr_kcounters *pfrke_counters; 1035 long pfrke_tzero; 1036 u_int8_t pfrke_af; 1037 u_int8_t pfrke_net; 1038 u_int8_t pfrke_not; 1039 u_int8_t pfrke_mark; 1040 }; 1041 1042 SLIST_HEAD(pfr_ktableworkq, pfr_ktable); 1043 RB_HEAD(pfr_ktablehead, pfr_ktable); 1044 struct pfr_ktable { 1045 struct pfr_tstats pfrkt_ts; 1046 RB_ENTRY(pfr_ktable) pfrkt_tree; 1047 SLIST_ENTRY(pfr_ktable) pfrkt_workq; 1048 struct radix_node_head *pfrkt_ip4; 1049 struct radix_node_head *pfrkt_ip6; 1050 struct pfr_ktable *pfrkt_shadow; 1051 struct pfr_ktable *pfrkt_root; 1052 struct pf_ruleset *pfrkt_rs; 1053 long pfrkt_larg; 1054 int pfrkt_nflags; 1055 }; 1056 #define pfrkt_t pfrkt_ts.pfrts_t 1057 #define pfrkt_name pfrkt_t.pfrt_name 1058 #define pfrkt_anchor pfrkt_t.pfrt_anchor 1059 #define pfrkt_ruleset pfrkt_t.pfrt_ruleset 1060 #define pfrkt_flags pfrkt_t.pfrt_flags 1061 #define pfrkt_cnt pfrkt_ts.pfrts_cnt 1062 #define pfrkt_refcnt pfrkt_ts.pfrts_refcnt 1063 #define pfrkt_packets pfrkt_ts.pfrts_packets 1064 #define pfrkt_bytes pfrkt_ts.pfrts_bytes 1065 #define pfrkt_match pfrkt_ts.pfrts_match 1066 #define pfrkt_nomatch pfrkt_ts.pfrts_nomatch 1067 #define pfrkt_tzero pfrkt_ts.pfrts_tzero 1068 1069 /* keep synced with pfi_kif, used in RB_FIND */ 1070 struct pfi_kif_cmp { 1071 char pfik_name[IFNAMSIZ]; 1072 }; 1073 1074 struct pfi_kif { 1075 char pfik_name[IFNAMSIZ]; 1076 union { 1077 RB_ENTRY(pfi_kif) _pfik_tree; 1078 LIST_ENTRY(pfi_kif) _pfik_list; 1079 } _pfik_glue; 1080 #define pfik_tree _pfik_glue._pfik_tree 1081 #define pfik_list _pfik_glue._pfik_list 1082 u_int64_t pfik_packets[2][2][2]; 1083 u_int64_t pfik_bytes[2][2][2]; 1084 u_int32_t pfik_tzero; 1085 u_int pfik_flags; 1086 struct ifnet *pfik_ifp; 1087 struct ifg_group *pfik_group; 1088 u_int pfik_rulerefs; 1089 TAILQ_HEAD(, pfi_dynaddr) pfik_dynaddrs; 1090 }; 1091 1092 #define PFI_IFLAG_REFS 0x0001 /* has state references */ 1093 #define PFI_IFLAG_SKIP 0x0100 /* skip filtering on interface */ 1094 1095 struct pf_pdesc { 1096 struct { 1097 int done; 1098 uid_t uid; 1099 gid_t gid; 1100 } lookup; 1101 u_int64_t tot_len; /* Make Mickey money */ 1102 union { 1103 struct tcphdr *tcp; 1104 struct udphdr *udp; 1105 struct icmp *icmp; 1106 #ifdef INET6 1107 struct icmp6_hdr *icmp6; 1108 #endif /* INET6 */ 1109 void *any; 1110 } hdr; 1111 1112 struct pf_rule *nat_rule; /* nat/rdr rule applied to packet */ 1113 struct pf_addr *src; /* src address */ 1114 struct pf_addr *dst; /* dst address */ 1115 u_int16_t *sport; 1116 u_int16_t *dport; 1117 struct pf_mtag *pf_mtag; 1118 1119 u_int32_t p_len; /* total length of payload */ 1120 1121 u_int16_t *ip_sum; 1122 u_int16_t *proto_sum; 1123 u_int16_t flags; /* Let SCRUB trigger behavior in 1124 * state code. Easier than tags */ 1125 #define PFDESC_TCP_NORM 0x0001 /* TCP shall be statefully scrubbed */ 1126 #define PFDESC_IP_REAS 0x0002 /* IP frags would've been reassembled */ 1127 sa_family_t af; 1128 u_int8_t proto; 1129 u_int8_t tos; 1130 u_int8_t dir; /* direction */ 1131 u_int8_t sidx; /* key index for source */ 1132 u_int8_t didx; /* key index for destination */ 1133 }; 1134 1135 /* flags for RDR options */ 1136 #define PF_DPORT_RANGE 0x01 /* Dest port uses range */ 1137 #define PF_RPORT_RANGE 0x02 /* RDR'ed port uses range */ 1138 1139 /* UDP state enumeration */ 1140 #define PFUDPS_NO_TRAFFIC 0 1141 #define PFUDPS_SINGLE 1 1142 #define PFUDPS_MULTIPLE 2 1143 1144 #define PFUDPS_NSTATES 3 /* number of state levels */ 1145 1146 #define PFUDPS_NAMES { \ 1147 "NO_TRAFFIC", \ 1148 "SINGLE", \ 1149 "MULTIPLE", \ 1150 NULL \ 1151 } 1152 1153 /* Other protocol state enumeration */ 1154 #define PFOTHERS_NO_TRAFFIC 0 1155 #define PFOTHERS_SINGLE 1 1156 #define PFOTHERS_MULTIPLE 2 1157 1158 #define PFOTHERS_NSTATES 3 /* number of state levels */ 1159 1160 #define PFOTHERS_NAMES { \ 1161 "NO_TRAFFIC", \ 1162 "SINGLE", \ 1163 "MULTIPLE", \ 1164 NULL \ 1165 } 1166 1167 #define ACTION_SET(a, x) \ 1168 do { \ 1169 if ((a) != NULL) \ 1170 *(a) = (x); \ 1171 } while (0) 1172 1173 #define REASON_SET(a, x) \ 1174 do { \ 1175 if ((a) != NULL) \ 1176 *(a) = (x); \ 1177 if (x < PFRES_MAX) \ 1178 counter_u64_add(V_pf_status.counters[x], 1); \ 1179 } while (0) 1180 1181 struct pf_kstatus { 1182 counter_u64_t counters[PFRES_MAX]; /* reason for passing/dropping */ 1183 counter_u64_t lcounters[LCNT_MAX]; /* limit counters */ 1184 counter_u64_t fcounters[FCNT_MAX]; /* state operation counters */ 1185 counter_u64_t scounters[SCNT_MAX]; /* src_node operation counters */ 1186 uint32_t states; 1187 uint32_t src_nodes; 1188 uint32_t running; 1189 uint32_t since; 1190 uint32_t debug; 1191 uint32_t hostid; 1192 char ifname[IFNAMSIZ]; 1193 uint8_t pf_chksum[PF_MD5_DIGEST_LENGTH]; 1194 }; 1195 1196 struct pf_divert { 1197 union { 1198 struct in_addr ipv4; 1199 struct in6_addr ipv6; 1200 } addr; 1201 u_int16_t port; 1202 }; 1203 1204 #define PFFRAG_FRENT_HIWAT 5000 /* Number of fragment entries */ 1205 #define PFR_KENTRY_HIWAT 200000 /* Number of table entries */ 1206 1207 /* 1208 * ioctl parameter structures 1209 */ 1210 1211 struct pfioc_pooladdr { 1212 u_int32_t action; 1213 u_int32_t ticket; 1214 u_int32_t nr; 1215 u_int32_t r_num; 1216 u_int8_t r_action; 1217 u_int8_t r_last; 1218 u_int8_t af; 1219 char anchor[MAXPATHLEN]; 1220 struct pf_pooladdr addr; 1221 }; 1222 1223 struct pfioc_rule { 1224 u_int32_t action; 1225 u_int32_t ticket; 1226 u_int32_t pool_ticket; 1227 u_int32_t nr; 1228 char anchor[MAXPATHLEN]; 1229 char anchor_call[MAXPATHLEN]; 1230 struct pf_rule rule; 1231 }; 1232 1233 struct pfioc_natlook { 1234 struct pf_addr saddr; 1235 struct pf_addr daddr; 1236 struct pf_addr rsaddr; 1237 struct pf_addr rdaddr; 1238 u_int16_t sport; 1239 u_int16_t dport; 1240 u_int16_t rsport; 1241 u_int16_t rdport; 1242 sa_family_t af; 1243 u_int8_t proto; 1244 u_int8_t direction; 1245 }; 1246 1247 struct pfioc_state { 1248 struct pfsync_state state; 1249 }; 1250 1251 struct pfioc_src_node_kill { 1252 sa_family_t psnk_af; 1253 struct pf_rule_addr psnk_src; 1254 struct pf_rule_addr psnk_dst; 1255 u_int psnk_killed; 1256 }; 1257 1258 struct pfioc_state_kill { 1259 struct pf_state_cmp psk_pfcmp; 1260 sa_family_t psk_af; 1261 int psk_proto; 1262 struct pf_rule_addr psk_src; 1263 struct pf_rule_addr psk_dst; 1264 char psk_ifname[IFNAMSIZ]; 1265 char psk_label[PF_RULE_LABEL_SIZE]; 1266 u_int psk_killed; 1267 }; 1268 1269 struct pfioc_states { 1270 int ps_len; 1271 union { 1272 caddr_t psu_buf; 1273 struct pfsync_state *psu_states; 1274 } ps_u; 1275 #define ps_buf ps_u.psu_buf 1276 #define ps_states ps_u.psu_states 1277 }; 1278 1279 struct pfioc_src_nodes { 1280 int psn_len; 1281 union { 1282 caddr_t psu_buf; 1283 struct pf_src_node *psu_src_nodes; 1284 } psn_u; 1285 #define psn_buf psn_u.psu_buf 1286 #define psn_src_nodes psn_u.psu_src_nodes 1287 }; 1288 1289 struct pfioc_if { 1290 char ifname[IFNAMSIZ]; 1291 }; 1292 1293 struct pfioc_tm { 1294 int timeout; 1295 int seconds; 1296 }; 1297 1298 struct pfioc_limit { 1299 int index; 1300 unsigned limit; 1301 }; 1302 1303 struct pfioc_altq_v0 { 1304 u_int32_t action; 1305 u_int32_t ticket; 1306 u_int32_t nr; 1307 struct pf_altq_v0 altq; 1308 }; 1309 1310 struct pfioc_altq_v1 { 1311 u_int32_t action; 1312 u_int32_t ticket; 1313 u_int32_t nr; 1314 /* 1315 * Placed here so code that only uses the above parameters can be 1316 * written entirely in terms of the v0 or v1 type. 1317 */ 1318 u_int32_t version; 1319 struct pf_altq_v1 altq; 1320 }; 1321 1322 /* 1323 * Latest version of struct pfioc_altq_vX. This must move in lock-step with 1324 * the latest version of struct pf_altq_vX as it has that struct as a 1325 * member. 1326 */ 1327 #define PFIOC_ALTQ_VERSION PF_ALTQ_VERSION 1328 1329 struct pfioc_qstats_v0 { 1330 u_int32_t ticket; 1331 u_int32_t nr; 1332 void *buf; 1333 int nbytes; 1334 u_int8_t scheduler; 1335 }; 1336 1337 struct pfioc_qstats_v1 { 1338 u_int32_t ticket; 1339 u_int32_t nr; 1340 void *buf; 1341 int nbytes; 1342 u_int8_t scheduler; 1343 /* 1344 * Placed here so code that only uses the above parameters can be 1345 * written entirely in terms of the v0 or v1 type. 1346 */ 1347 u_int32_t version; /* Requested version of stats struct */ 1348 }; 1349 1350 /* Latest version of struct pfioc_qstats_vX */ 1351 #define PFIOC_QSTATS_VERSION 1 1352 1353 struct pfioc_ruleset { 1354 u_int32_t nr; 1355 char path[MAXPATHLEN]; 1356 char name[PF_ANCHOR_NAME_SIZE]; 1357 }; 1358 1359 #define PF_RULESET_ALTQ (PF_RULESET_MAX) 1360 #define PF_RULESET_TABLE (PF_RULESET_MAX+1) 1361 struct pfioc_trans { 1362 int size; /* number of elements */ 1363 int esize; /* size of each element in bytes */ 1364 struct pfioc_trans_e { 1365 int rs_num; 1366 char anchor[MAXPATHLEN]; 1367 u_int32_t ticket; 1368 } *array; 1369 }; 1370 1371 #define PFR_FLAG_ATOMIC 0x00000001 /* unused */ 1372 #define PFR_FLAG_DUMMY 0x00000002 1373 #define PFR_FLAG_FEEDBACK 0x00000004 1374 #define PFR_FLAG_CLSTATS 0x00000008 1375 #define PFR_FLAG_ADDRSTOO 0x00000010 1376 #define PFR_FLAG_REPLACE 0x00000020 1377 #define PFR_FLAG_ALLRSETS 0x00000040 1378 #define PFR_FLAG_ALLMASK 0x0000007F 1379 #ifdef _KERNEL 1380 #define PFR_FLAG_USERIOCTL 0x10000000 1381 #endif 1382 1383 struct pfioc_table { 1384 struct pfr_table pfrio_table; 1385 void *pfrio_buffer; 1386 int pfrio_esize; 1387 int pfrio_size; 1388 int pfrio_size2; 1389 int pfrio_nadd; 1390 int pfrio_ndel; 1391 int pfrio_nchange; 1392 int pfrio_flags; 1393 u_int32_t pfrio_ticket; 1394 }; 1395 #define pfrio_exists pfrio_nadd 1396 #define pfrio_nzero pfrio_nadd 1397 #define pfrio_nmatch pfrio_nadd 1398 #define pfrio_naddr pfrio_size2 1399 #define pfrio_setflag pfrio_size2 1400 #define pfrio_clrflag pfrio_nadd 1401 1402 struct pfioc_iface { 1403 char pfiio_name[IFNAMSIZ]; 1404 void *pfiio_buffer; 1405 int pfiio_esize; 1406 int pfiio_size; 1407 int pfiio_nzero; 1408 int pfiio_flags; 1409 }; 1410 1411 1412 /* 1413 * ioctl operations 1414 */ 1415 1416 #define DIOCSTART _IO ('D', 1) 1417 #define DIOCSTOP _IO ('D', 2) 1418 #define DIOCADDRULE _IOWR('D', 4, struct pfioc_rule) 1419 #define DIOCGETRULES _IOWR('D', 6, struct pfioc_rule) 1420 #define DIOCGETRULE _IOWR('D', 7, struct pfioc_rule) 1421 /* XXX cut 8 - 17 */ 1422 #define DIOCCLRSTATES _IOWR('D', 18, struct pfioc_state_kill) 1423 #define DIOCGETSTATE _IOWR('D', 19, struct pfioc_state) 1424 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if) 1425 #define DIOCGETSTATUS _IOWR('D', 21, struct pf_status) 1426 #define DIOCCLRSTATUS _IO ('D', 22) 1427 #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook) 1428 #define DIOCSETDEBUG _IOWR('D', 24, u_int32_t) 1429 #define DIOCGETSTATES _IOWR('D', 25, struct pfioc_states) 1430 #define DIOCCHANGERULE _IOWR('D', 26, struct pfioc_rule) 1431 /* XXX cut 26 - 28 */ 1432 #define DIOCSETTIMEOUT _IOWR('D', 29, struct pfioc_tm) 1433 #define DIOCGETTIMEOUT _IOWR('D', 30, struct pfioc_tm) 1434 #define DIOCADDSTATE _IOWR('D', 37, struct pfioc_state) 1435 #define DIOCCLRRULECTRS _IO ('D', 38) 1436 #define DIOCGETLIMIT _IOWR('D', 39, struct pfioc_limit) 1437 #define DIOCSETLIMIT _IOWR('D', 40, struct pfioc_limit) 1438 #define DIOCKILLSTATES _IOWR('D', 41, struct pfioc_state_kill) 1439 #define DIOCSTARTALTQ _IO ('D', 42) 1440 #define DIOCSTOPALTQ _IO ('D', 43) 1441 #define DIOCADDALTQV0 _IOWR('D', 45, struct pfioc_altq_v0) 1442 #define DIOCADDALTQV1 _IOWR('D', 45, struct pfioc_altq_v1) 1443 #define DIOCGETALTQSV0 _IOWR('D', 47, struct pfioc_altq_v0) 1444 #define DIOCGETALTQSV1 _IOWR('D', 47, struct pfioc_altq_v1) 1445 #define DIOCGETALTQV0 _IOWR('D', 48, struct pfioc_altq_v0) 1446 #define DIOCGETALTQV1 _IOWR('D', 48, struct pfioc_altq_v1) 1447 #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0) 1448 #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1) 1449 #define DIOCGETQSTATSV0 _IOWR('D', 50, struct pfioc_qstats_v0) 1450 #define DIOCGETQSTATSV1 _IOWR('D', 50, struct pfioc_qstats_v1) 1451 #define DIOCBEGINADDRS _IOWR('D', 51, struct pfioc_pooladdr) 1452 #define DIOCADDADDR _IOWR('D', 52, struct pfioc_pooladdr) 1453 #define DIOCGETADDRS _IOWR('D', 53, struct pfioc_pooladdr) 1454 #define DIOCGETADDR _IOWR('D', 54, struct pfioc_pooladdr) 1455 #define DIOCCHANGEADDR _IOWR('D', 55, struct pfioc_pooladdr) 1456 /* XXX cut 55 - 57 */ 1457 #define DIOCGETRULESETS _IOWR('D', 58, struct pfioc_ruleset) 1458 #define DIOCGETRULESET _IOWR('D', 59, struct pfioc_ruleset) 1459 #define DIOCRCLRTABLES _IOWR('D', 60, struct pfioc_table) 1460 #define DIOCRADDTABLES _IOWR('D', 61, struct pfioc_table) 1461 #define DIOCRDELTABLES _IOWR('D', 62, struct pfioc_table) 1462 #define DIOCRGETTABLES _IOWR('D', 63, struct pfioc_table) 1463 #define DIOCRGETTSTATS _IOWR('D', 64, struct pfioc_table) 1464 #define DIOCRCLRTSTATS _IOWR('D', 65, struct pfioc_table) 1465 #define DIOCRCLRADDRS _IOWR('D', 66, struct pfioc_table) 1466 #define DIOCRADDADDRS _IOWR('D', 67, struct pfioc_table) 1467 #define DIOCRDELADDRS _IOWR('D', 68, struct pfioc_table) 1468 #define DIOCRSETADDRS _IOWR('D', 69, struct pfioc_table) 1469 #define DIOCRGETADDRS _IOWR('D', 70, struct pfioc_table) 1470 #define DIOCRGETASTATS _IOWR('D', 71, struct pfioc_table) 1471 #define DIOCRCLRASTATS _IOWR('D', 72, struct pfioc_table) 1472 #define DIOCRTSTADDRS _IOWR('D', 73, struct pfioc_table) 1473 #define DIOCRSETTFLAGS _IOWR('D', 74, struct pfioc_table) 1474 #define DIOCRINADEFINE _IOWR('D', 77, struct pfioc_table) 1475 #define DIOCOSFPFLUSH _IO('D', 78) 1476 #define DIOCOSFPADD _IOWR('D', 79, struct pf_osfp_ioctl) 1477 #define DIOCOSFPGET _IOWR('D', 80, struct pf_osfp_ioctl) 1478 #define DIOCXBEGIN _IOWR('D', 81, struct pfioc_trans) 1479 #define DIOCXCOMMIT _IOWR('D', 82, struct pfioc_trans) 1480 #define DIOCXROLLBACK _IOWR('D', 83, struct pfioc_trans) 1481 #define DIOCGETSRCNODES _IOWR('D', 84, struct pfioc_src_nodes) 1482 #define DIOCCLRSRCNODES _IO('D', 85) 1483 #define DIOCSETHOSTID _IOWR('D', 86, u_int32_t) 1484 #define DIOCIGETIFACES _IOWR('D', 87, struct pfioc_iface) 1485 #define DIOCSETIFFLAG _IOWR('D', 89, struct pfioc_iface) 1486 #define DIOCCLRIFFLAG _IOWR('D', 90, struct pfioc_iface) 1487 #define DIOCKILLSRCNODES _IOWR('D', 91, struct pfioc_src_node_kill) 1488 struct pf_ifspeed_v0 { 1489 char ifname[IFNAMSIZ]; 1490 u_int32_t baudrate; 1491 }; 1492 1493 struct pf_ifspeed_v1 { 1494 char ifname[IFNAMSIZ]; 1495 u_int32_t baudrate32; 1496 /* layout identical to struct pf_ifspeed_v0 up to this point */ 1497 u_int64_t baudrate; 1498 }; 1499 1500 /* Latest version of struct pf_ifspeed_vX */ 1501 #define PF_IFSPEED_VERSION 1 1502 1503 #define DIOCGIFSPEEDV0 _IOWR('D', 92, struct pf_ifspeed_v0) 1504 #define DIOCGIFSPEEDV1 _IOWR('D', 92, struct pf_ifspeed_v1) 1505 1506 /* 1507 * Compatibility and convenience macros 1508 */ 1509 #ifndef _KERNEL 1510 #ifdef PFIOC_USE_LATEST 1511 /* 1512 * Maintaining in-tree consumers of the ioctl interface is easier when that 1513 * code can be written in terms old names that refer to the latest interface 1514 * version as that reduces the required changes in the consumers to those 1515 * that are functionally necessary to accommodate a new interface version. 1516 */ 1517 #define pfioc_altq __CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION) 1518 #define pfioc_qstats __CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION) 1519 #define pf_ifspeed __CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION) 1520 1521 #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION) 1522 #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION) 1523 #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION) 1524 #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION) 1525 #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION) 1526 #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION) 1527 #else 1528 /* 1529 * When building out-of-tree code that is written for the old interface, 1530 * such as may exist in ports for example, resolve the old struct tags and 1531 * ioctl command names to the v0 versions. 1532 */ 1533 #define pfioc_altq __CONCAT(pfioc_altq_v, 0) 1534 #define pfioc_qstats __CONCAT(pfioc_qstats_v, 0) 1535 #define pf_ifspeed __CONCAT(pf_ifspeed_v, 0) 1536 1537 #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, 0) 1538 #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, 0) 1539 #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, 0) 1540 #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, 0) 1541 #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, 0) 1542 #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, 0) 1543 #endif /* PFIOC_USE_LATEST */ 1544 #endif /* _KERNEL */ 1545 1546 #ifdef _KERNEL 1547 LIST_HEAD(pf_src_node_list, pf_src_node); 1548 struct pf_srchash { 1549 struct pf_src_node_list nodes; 1550 struct mtx lock; 1551 }; 1552 1553 struct pf_keyhash { 1554 LIST_HEAD(, pf_state_key) keys; 1555 struct mtx lock; 1556 }; 1557 1558 struct pf_idhash { 1559 LIST_HEAD(, pf_state) states; 1560 struct mtx lock; 1561 }; 1562 1563 extern u_long pf_hashmask; 1564 extern u_long pf_srchashmask; 1565 #define PF_HASHSIZ (131072) 1566 #define PF_SRCHASHSIZ (PF_HASHSIZ/4) 1567 VNET_DECLARE(struct pf_keyhash *, pf_keyhash); 1568 VNET_DECLARE(struct pf_idhash *, pf_idhash); 1569 #define V_pf_keyhash VNET(pf_keyhash) 1570 #define V_pf_idhash VNET(pf_idhash) 1571 VNET_DECLARE(struct pf_srchash *, pf_srchash); 1572 #define V_pf_srchash VNET(pf_srchash) 1573 1574 #define PF_IDHASH(s) (be64toh((s)->id) % (pf_hashmask + 1)) 1575 1576 VNET_DECLARE(void *, pf_swi_cookie); 1577 #define V_pf_swi_cookie VNET(pf_swi_cookie) 1578 1579 VNET_DECLARE(uint64_t, pf_stateid[MAXCPU]); 1580 #define V_pf_stateid VNET(pf_stateid) 1581 1582 TAILQ_HEAD(pf_altqqueue, pf_altq); 1583 VNET_DECLARE(struct pf_altqqueue, pf_altqs[2]); 1584 #define V_pf_altqs VNET(pf_altqs) 1585 VNET_DECLARE(struct pf_palist, pf_pabuf); 1586 #define V_pf_pabuf VNET(pf_pabuf) 1587 1588 VNET_DECLARE(u_int32_t, ticket_altqs_active); 1589 #define V_ticket_altqs_active VNET(ticket_altqs_active) 1590 VNET_DECLARE(u_int32_t, ticket_altqs_inactive); 1591 #define V_ticket_altqs_inactive VNET(ticket_altqs_inactive) 1592 VNET_DECLARE(int, altqs_inactive_open); 1593 #define V_altqs_inactive_open VNET(altqs_inactive_open) 1594 VNET_DECLARE(u_int32_t, ticket_pabuf); 1595 #define V_ticket_pabuf VNET(ticket_pabuf) 1596 VNET_DECLARE(struct pf_altqqueue *, pf_altqs_active); 1597 #define V_pf_altqs_active VNET(pf_altqs_active) 1598 VNET_DECLARE(struct pf_altqqueue *, pf_altqs_inactive); 1599 #define V_pf_altqs_inactive VNET(pf_altqs_inactive) 1600 1601 VNET_DECLARE(struct pf_rulequeue, pf_unlinked_rules); 1602 #define V_pf_unlinked_rules VNET(pf_unlinked_rules) 1603 1604 void pf_initialize(void); 1605 void pf_mtag_initialize(void); 1606 void pf_mtag_cleanup(void); 1607 void pf_cleanup(void); 1608 1609 struct pf_mtag *pf_get_mtag(struct mbuf *); 1610 1611 extern void pf_calc_skip_steps(struct pf_rulequeue *); 1612 #ifdef ALTQ 1613 extern void pf_altq_ifnet_event(struct ifnet *, int); 1614 #endif 1615 VNET_DECLARE(uma_zone_t, pf_state_z); 1616 #define V_pf_state_z VNET(pf_state_z) 1617 VNET_DECLARE(uma_zone_t, pf_state_key_z); 1618 #define V_pf_state_key_z VNET(pf_state_key_z) 1619 VNET_DECLARE(uma_zone_t, pf_state_scrub_z); 1620 #define V_pf_state_scrub_z VNET(pf_state_scrub_z) 1621 1622 extern void pf_purge_thread(void *); 1623 extern void pf_unload_vnet_purge(void); 1624 extern void pf_intr(void *); 1625 extern void pf_purge_expired_src_nodes(void); 1626 1627 extern int pf_unlink_state(struct pf_state *, u_int); 1628 #define PF_ENTER_LOCKED 0x00000001 1629 #define PF_RETURN_LOCKED 0x00000002 1630 extern int pf_state_insert(struct pfi_kif *, 1631 struct pf_state_key *, 1632 struct pf_state_key *, 1633 struct pf_state *); 1634 extern void pf_free_state(struct pf_state *); 1635 1636 static __inline void 1637 pf_ref_state(struct pf_state *s) 1638 { 1639 1640 refcount_acquire(&s->refs); 1641 } 1642 1643 static __inline int 1644 pf_release_state(struct pf_state *s) 1645 { 1646 1647 if (refcount_release(&s->refs)) { 1648 pf_free_state(s); 1649 return (1); 1650 } else 1651 return (0); 1652 } 1653 1654 extern struct pf_state *pf_find_state_byid(uint64_t, uint32_t); 1655 extern struct pf_state *pf_find_state_all(struct pf_state_key_cmp *, 1656 u_int, int *); 1657 extern struct pf_src_node *pf_find_src_node(struct pf_addr *, 1658 struct pf_rule *, sa_family_t, int); 1659 extern void pf_unlink_src_node(struct pf_src_node *); 1660 extern u_int pf_free_src_nodes(struct pf_src_node_list *); 1661 extern void pf_print_state(struct pf_state *); 1662 extern void pf_print_flags(u_int8_t); 1663 extern u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t, 1664 u_int8_t); 1665 extern u_int16_t pf_proto_cksum_fixup(struct mbuf *, u_int16_t, 1666 u_int16_t, u_int16_t, u_int8_t); 1667 1668 VNET_DECLARE(struct ifnet *, sync_ifp); 1669 #define V_sync_ifp VNET(sync_ifp); 1670 VNET_DECLARE(struct pf_rule, pf_default_rule); 1671 #define V_pf_default_rule VNET(pf_default_rule) 1672 extern void pf_addrcpy(struct pf_addr *, struct pf_addr *, 1673 u_int8_t); 1674 void pf_free_rule(struct pf_rule *); 1675 1676 #ifdef INET 1677 int pf_test(int, int, struct ifnet *, struct mbuf **, struct inpcb *); 1678 int pf_normalize_ip(struct mbuf **, int, struct pfi_kif *, u_short *, 1679 struct pf_pdesc *); 1680 #endif /* INET */ 1681 1682 #ifdef INET6 1683 int pf_test6(int, int, struct ifnet *, struct mbuf **, struct inpcb *); 1684 int pf_normalize_ip6(struct mbuf **, int, struct pfi_kif *, u_short *, 1685 struct pf_pdesc *); 1686 void pf_poolmask(struct pf_addr *, struct pf_addr*, 1687 struct pf_addr *, struct pf_addr *, u_int8_t); 1688 void pf_addr_inc(struct pf_addr *, sa_family_t); 1689 int pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *); 1690 #endif /* INET6 */ 1691 1692 u_int32_t pf_new_isn(struct pf_state *); 1693 void *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *, 1694 sa_family_t); 1695 void pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t); 1696 void pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t, 1697 u_int8_t); 1698 void pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t); 1699 void pf_send_deferred_syn(struct pf_state *); 1700 int pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *, 1701 struct pf_addr *, sa_family_t); 1702 int pf_match_addr_range(struct pf_addr *, struct pf_addr *, 1703 struct pf_addr *, sa_family_t); 1704 int pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t); 1705 1706 void pf_normalize_init(void); 1707 void pf_normalize_cleanup(void); 1708 int pf_normalize_tcp(int, struct pfi_kif *, struct mbuf *, int, int, void *, 1709 struct pf_pdesc *); 1710 void pf_normalize_tcp_cleanup(struct pf_state *); 1711 int pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *, 1712 struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *); 1713 int pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *, 1714 u_short *, struct tcphdr *, struct pf_state *, 1715 struct pf_state_peer *, struct pf_state_peer *, int *); 1716 u_int32_t 1717 pf_state_expires(const struct pf_state *); 1718 void pf_purge_expired_fragments(void); 1719 void pf_purge_fragments(uint32_t); 1720 int pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kif *, 1721 int); 1722 int pf_socket_lookup(int, struct pf_pdesc *, struct mbuf *); 1723 struct pf_state_key *pf_alloc_state_key(int); 1724 void pfr_initialize(void); 1725 void pfr_cleanup(void); 1726 int pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t); 1727 void pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t, 1728 u_int64_t, int, int, int); 1729 int pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t); 1730 void pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *); 1731 struct pfr_ktable * 1732 pfr_attach_table(struct pf_ruleset *, char *); 1733 void pfr_detach_table(struct pfr_ktable *); 1734 int pfr_clr_tables(struct pfr_table *, int *, int); 1735 int pfr_add_tables(struct pfr_table *, int, int *, int); 1736 int pfr_del_tables(struct pfr_table *, int, int *, int); 1737 int pfr_table_count(struct pfr_table *, int); 1738 int pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int); 1739 int pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int); 1740 int pfr_clr_tstats(struct pfr_table *, int, int *, int); 1741 int pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int); 1742 int pfr_clr_addrs(struct pfr_table *, int *, int); 1743 int pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long); 1744 int pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 1745 int); 1746 int pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 1747 int); 1748 int pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 1749 int *, int *, int *, int, u_int32_t); 1750 int pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int); 1751 int pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int); 1752 int pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *, 1753 int); 1754 int pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 1755 int); 1756 int pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int); 1757 int pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int); 1758 int pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int); 1759 int pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *, 1760 int *, u_int32_t, int); 1761 1762 MALLOC_DECLARE(PFI_MTYPE); 1763 VNET_DECLARE(struct pfi_kif *, pfi_all); 1764 #define V_pfi_all VNET(pfi_all) 1765 1766 void pfi_initialize(void); 1767 void pfi_initialize_vnet(void); 1768 void pfi_cleanup(void); 1769 void pfi_cleanup_vnet(void); 1770 void pfi_kif_ref(struct pfi_kif *); 1771 void pfi_kif_unref(struct pfi_kif *); 1772 struct pfi_kif *pfi_kif_find(const char *); 1773 struct pfi_kif *pfi_kif_attach(struct pfi_kif *, const char *); 1774 int pfi_kif_match(struct pfi_kif *, struct pfi_kif *); 1775 void pfi_kif_purge(void); 1776 int pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *, 1777 sa_family_t); 1778 int pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t); 1779 void pfi_dynaddr_remove(struct pfi_dynaddr *); 1780 void pfi_dynaddr_copyout(struct pf_addr_wrap *); 1781 void pfi_update_status(const char *, struct pf_status *); 1782 void pfi_get_ifaces(const char *, struct pfi_kif *, int *); 1783 int pfi_set_flags(const char *, int); 1784 int pfi_clear_flags(const char *, int); 1785 1786 int pf_match_tag(struct mbuf *, struct pf_rule *, int *, int); 1787 int pf_tag_packet(struct mbuf *, struct pf_pdesc *, int); 1788 int pf_addr_cmp(struct pf_addr *, struct pf_addr *, 1789 sa_family_t); 1790 void pf_qid2qname(u_int32_t, char *); 1791 1792 VNET_DECLARE(struct pf_kstatus, pf_status); 1793 #define V_pf_status VNET(pf_status) 1794 1795 struct pf_limit { 1796 uma_zone_t zone; 1797 u_int limit; 1798 }; 1799 VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); 1800 #define V_pf_limits VNET(pf_limits) 1801 1802 #endif /* _KERNEL */ 1803 1804 #ifdef _KERNEL 1805 VNET_DECLARE(struct pf_anchor_global, pf_anchors); 1806 #define V_pf_anchors VNET(pf_anchors) 1807 VNET_DECLARE(struct pf_anchor, pf_main_anchor); 1808 #define V_pf_main_anchor VNET(pf_main_anchor) 1809 #define pf_main_ruleset V_pf_main_anchor.ruleset 1810 #endif 1811 1812 /* these ruleset functions can be linked into userland programs (pfctl) */ 1813 int pf_get_ruleset_number(u_int8_t); 1814 void pf_init_ruleset(struct pf_ruleset *); 1815 int pf_anchor_setup(struct pf_rule *, 1816 const struct pf_ruleset *, const char *); 1817 int pf_anchor_copyout(const struct pf_ruleset *, 1818 const struct pf_rule *, struct pfioc_rule *); 1819 void pf_anchor_remove(struct pf_rule *); 1820 void pf_remove_if_empty_ruleset(struct pf_ruleset *); 1821 struct pf_ruleset *pf_find_ruleset(const char *); 1822 struct pf_ruleset *pf_find_or_create_ruleset(const char *); 1823 void pf_rs_initialize(void); 1824 1825 /* The fingerprint functions can be linked into userland programs (tcpdump) */ 1826 int pf_osfp_add(struct pf_osfp_ioctl *); 1827 #ifdef _KERNEL 1828 struct pf_osfp_enlist * 1829 pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int, 1830 const struct tcphdr *); 1831 #endif /* _KERNEL */ 1832 void pf_osfp_flush(void); 1833 int pf_osfp_get(struct pf_osfp_ioctl *); 1834 int pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t); 1835 1836 #ifdef _KERNEL 1837 void pf_print_host(struct pf_addr *, u_int16_t, u_int8_t); 1838 1839 void pf_step_into_anchor(struct pf_anchor_stackframe *, int *, 1840 struct pf_ruleset **, int, struct pf_rule **, 1841 struct pf_rule **, int *); 1842 int pf_step_out_of_anchor(struct pf_anchor_stackframe *, int *, 1843 struct pf_ruleset **, int, struct pf_rule **, 1844 struct pf_rule **, int *); 1845 1846 int pf_map_addr(u_int8_t, struct pf_rule *, 1847 struct pf_addr *, struct pf_addr *, 1848 struct pf_addr *, struct pf_src_node **); 1849 struct pf_rule *pf_get_translation(struct pf_pdesc *, struct mbuf *, 1850 int, int, struct pfi_kif *, struct pf_src_node **, 1851 struct pf_state_key **, struct pf_state_key **, 1852 struct pf_addr *, struct pf_addr *, 1853 uint16_t, uint16_t, struct pf_anchor_stackframe *); 1854 1855 struct pf_state_key *pf_state_key_setup(struct pf_pdesc *, struct pf_addr *, 1856 struct pf_addr *, u_int16_t, u_int16_t); 1857 struct pf_state_key *pf_state_key_clone(struct pf_state_key *); 1858 #endif /* _KERNEL */ 1859 1860 #endif /* _NET_PFVAR_H_ */ 1861