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 */ 33 34 #ifndef _NET_PFVAR_H_ 35 #define _NET_PFVAR_H_ 36 37 #include <sys/param.h> 38 #include <sys/queue.h> 39 #include <sys/counter.h> 40 #include <sys/cpuset.h> 41 #include <sys/epoch.h> 42 #include <sys/malloc.h> 43 #include <sys/nv.h> 44 #include <sys/refcount.h> 45 #include <sys/sdt.h> 46 #include <sys/sysctl.h> 47 #include <sys/smp.h> 48 #include <sys/lock.h> 49 #include <sys/rmlock.h> 50 #include <sys/tree.h> 51 #include <sys/seqc.h> 52 #include <vm/uma.h> 53 54 #include <net/if.h> 55 #include <net/ethernet.h> 56 #include <net/radix.h> 57 #include <netinet/in.h> 58 #ifdef _KERNEL 59 #include <netinet/ip.h> 60 #include <netinet/tcp.h> 61 #include <netinet/udp.h> 62 #include <netinet/sctp.h> 63 #include <netinet/ip_icmp.h> 64 #include <netinet/icmp6.h> 65 #endif 66 67 #include <netpfil/pf/pf.h> 68 #include <netpfil/pf/pf_altq.h> 69 #include <netpfil/pf/pf_mtag.h> 70 71 #ifdef _KERNEL 72 73 #define PF_PFIL_NOREFRAGMENT 0x80000000 74 75 #if defined(__arm__) 76 #define PF_WANT_32_TO_64_COUNTER 77 #endif 78 79 /* 80 * A hybrid of 32-bit and 64-bit counters which can be used on platforms where 81 * counter(9) is very expensive. 82 * 83 * As 32-bit counters are expected to overflow, a periodic job sums them up to 84 * a saved 64-bit state. Fetching the value still walks all CPUs to get the most 85 * current snapshot. 86 */ 87 #ifdef PF_WANT_32_TO_64_COUNTER 88 struct pf_counter_u64_pcpu { 89 u_int32_t current; 90 u_int32_t snapshot; 91 }; 92 93 struct pf_counter_u64 { 94 struct pf_counter_u64_pcpu *pfcu64_pcpu; 95 u_int64_t pfcu64_value; 96 seqc_t pfcu64_seqc; 97 }; 98 99 static inline int 100 pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags) 101 { 102 103 pfcu64->pfcu64_value = 0; 104 pfcu64->pfcu64_seqc = 0; 105 pfcu64->pfcu64_pcpu = uma_zalloc_pcpu(pcpu_zone_8, flags | M_ZERO); 106 if (__predict_false(pfcu64->pfcu64_pcpu == NULL)) 107 return (ENOMEM); 108 return (0); 109 } 110 111 static inline void 112 pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64) 113 { 114 115 uma_zfree_pcpu(pcpu_zone_8, pfcu64->pfcu64_pcpu); 116 } 117 118 static inline void 119 pf_counter_u64_critical_enter(void) 120 { 121 122 critical_enter(); 123 } 124 125 static inline void 126 pf_counter_u64_critical_exit(void) 127 { 128 129 critical_exit(); 130 } 131 132 static inline void 133 pf_counter_u64_rollup_protected(struct pf_counter_u64 *pfcu64, uint64_t n) 134 { 135 136 MPASS(curthread->td_critnest > 0); 137 pfcu64->pfcu64_value += n; 138 } 139 140 static inline void 141 pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n) 142 { 143 struct pf_counter_u64_pcpu *pcpu; 144 u_int32_t val; 145 146 MPASS(curthread->td_critnest > 0); 147 pcpu = zpcpu_get(pfcu64->pfcu64_pcpu); 148 val = atomic_load_int(&pcpu->current); 149 atomic_store_int(&pcpu->current, val + n); 150 } 151 152 static inline void 153 pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n) 154 { 155 156 critical_enter(); 157 pf_counter_u64_add_protected(pfcu64, n); 158 critical_exit(); 159 } 160 161 static inline u_int64_t 162 pf_counter_u64_periodic(struct pf_counter_u64 *pfcu64) 163 { 164 struct pf_counter_u64_pcpu *pcpu; 165 u_int64_t sum; 166 u_int32_t val; 167 int cpu; 168 169 MPASS(curthread->td_critnest > 0); 170 seqc_write_begin(&pfcu64->pfcu64_seqc); 171 sum = pfcu64->pfcu64_value; 172 CPU_FOREACH(cpu) { 173 pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); 174 val = atomic_load_int(&pcpu->current); 175 sum += (uint32_t)(val - pcpu->snapshot); 176 pcpu->snapshot = val; 177 } 178 pfcu64->pfcu64_value = sum; 179 seqc_write_end(&pfcu64->pfcu64_seqc); 180 return (sum); 181 } 182 183 static inline u_int64_t 184 pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64) 185 { 186 struct pf_counter_u64_pcpu *pcpu; 187 u_int64_t sum; 188 seqc_t seqc; 189 int cpu; 190 191 for (;;) { 192 seqc = seqc_read(&pfcu64->pfcu64_seqc); 193 sum = 0; 194 CPU_FOREACH(cpu) { 195 pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); 196 sum += (uint32_t)(atomic_load_int(&pcpu->current) -pcpu->snapshot); 197 } 198 sum += pfcu64->pfcu64_value; 199 if (seqc_consistent(&pfcu64->pfcu64_seqc, seqc)) 200 break; 201 } 202 return (sum); 203 } 204 205 static inline void 206 pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64) 207 { 208 struct pf_counter_u64_pcpu *pcpu; 209 int cpu; 210 211 MPASS(curthread->td_critnest > 0); 212 seqc_write_begin(&pfcu64->pfcu64_seqc); 213 CPU_FOREACH(cpu) { 214 pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); 215 pcpu->snapshot = atomic_load_int(&pcpu->current); 216 } 217 pfcu64->pfcu64_value = 0; 218 seqc_write_end(&pfcu64->pfcu64_seqc); 219 } 220 221 static inline void 222 pf_counter_u64_zero(struct pf_counter_u64 *pfcu64) 223 { 224 225 critical_enter(); 226 pf_counter_u64_zero_protected(pfcu64); 227 critical_exit(); 228 } 229 #else 230 struct pf_counter_u64 { 231 counter_u64_t counter; 232 }; 233 234 static inline int 235 pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags) 236 { 237 238 pfcu64->counter = counter_u64_alloc(flags); 239 if (__predict_false(pfcu64->counter == NULL)) 240 return (ENOMEM); 241 return (0); 242 } 243 244 static inline void 245 pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64) 246 { 247 248 counter_u64_free(pfcu64->counter); 249 } 250 251 static inline void 252 pf_counter_u64_critical_enter(void) 253 { 254 255 } 256 257 static inline void 258 pf_counter_u64_critical_exit(void) 259 { 260 261 } 262 263 static inline void 264 pf_counter_u64_rollup_protected(struct pf_counter_u64 *pfcu64, uint64_t n) 265 { 266 267 counter_u64_add(pfcu64->counter, n); 268 } 269 270 static inline void 271 pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n) 272 { 273 274 counter_u64_add(pfcu64->counter, n); 275 } 276 277 static inline void 278 pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n) 279 { 280 281 pf_counter_u64_add_protected(pfcu64, n); 282 } 283 284 static inline u_int64_t 285 pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64) 286 { 287 288 return (counter_u64_fetch(pfcu64->counter)); 289 } 290 291 static inline void 292 pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64) 293 { 294 295 counter_u64_zero(pfcu64->counter); 296 } 297 298 static inline void 299 pf_counter_u64_zero(struct pf_counter_u64 *pfcu64) 300 { 301 302 pf_counter_u64_zero_protected(pfcu64); 303 } 304 #endif 305 306 #define pf_get_timestamp(prule)({ \ 307 uint32_t _ts = 0; \ 308 uint32_t __ts; \ 309 int cpu; \ 310 CPU_FOREACH(cpu) { \ 311 __ts = *zpcpu_get_cpu(prule->timestamp, cpu); \ 312 if (__ts > _ts) \ 313 _ts = __ts; \ 314 } \ 315 _ts; \ 316 }) 317 318 #define pf_update_timestamp(prule) \ 319 do { \ 320 critical_enter(); \ 321 *zpcpu_get((prule)->timestamp) = time_second; \ 322 critical_exit(); \ 323 } while (0) 324 325 #define pf_timestamp_pcpu_zone (sizeof(time_t) == 4 ? pcpu_zone_4 : pcpu_zone_8) 326 _Static_assert(sizeof(time_t) == 4 || sizeof(time_t) == 8, "unexpected time_t size"); 327 328 SYSCTL_DECL(_net_pf); 329 MALLOC_DECLARE(M_PF); 330 MALLOC_DECLARE(M_PFHASH); 331 MALLOC_DECLARE(M_PF_RULE_ITEM); 332 MALLOC_DECLARE(M_PF_STATE_LINK); 333 MALLOC_DECLARE(M_PF_SOURCE_LIM); 334 MALLOC_DECLARE(M_PF_STATE_LIM); 335 336 SDT_PROVIDER_DECLARE(pf); 337 SDT_PROBE_DECLARE(pf, , test, reason_set); 338 SDT_PROBE_DECLARE(pf, , log, log); 339 340 #define DPFPRINTF(n, fmt, x...) \ 341 do { \ 342 SDT_PROBE2(pf, , log, log, (n), fmt); \ 343 if (V_pf_status.debug >= (n)) \ 344 printf(fmt "\n", ##x); \ 345 } while (0) 346 347 struct pfi_dynaddr { 348 TAILQ_ENTRY(pfi_dynaddr) entry; 349 struct pf_addr pfid_addr4; 350 struct pf_addr pfid_mask4; 351 struct pf_addr pfid_addr6; 352 struct pf_addr pfid_mask6; 353 struct pfr_ktable *pfid_kt; 354 struct pfi_kkif *pfid_kif; 355 int pfid_net; /* mask or 128 */ 356 int pfid_acnt4; /* address count IPv4 */ 357 int pfid_acnt6; /* address count IPv6 */ 358 sa_family_t pfid_af; /* rule af */ 359 u_int8_t pfid_iflags; /* PFI_AFLAG_* */ 360 }; 361 362 #define PF_NAME "pf" 363 364 #define PF_HASHROW_ASSERT(h) mtx_assert(&(h)->lock, MA_OWNED) 365 #define PF_HASHROW_LOCK(h) mtx_lock(&(h)->lock) 366 #define PF_HASHROW_UNLOCK(h) mtx_unlock(&(h)->lock) 367 368 #ifdef INVARIANTS 369 #define PF_STATE_LOCK(s) \ 370 do { \ 371 struct pf_kstate *_s = (s); \ 372 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 373 MPASS(_s->lock == &_ih->lock); \ 374 mtx_lock(_s->lock); \ 375 } while (0) 376 #define PF_STATE_UNLOCK(s) \ 377 do { \ 378 struct pf_kstate *_s = (s); \ 379 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 380 MPASS(_s->lock == &_ih->lock); \ 381 mtx_unlock(_s->lock); \ 382 } while (0) 383 #else 384 #define PF_STATE_LOCK(s) mtx_lock((s)->lock) 385 #define PF_STATE_UNLOCK(s) mtx_unlock((s)->lock) 386 #endif 387 388 #ifdef INVARIANTS 389 #define PF_STATE_LOCK_ASSERT(s) \ 390 do { \ 391 struct pf_kstate *_s = (s); \ 392 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 393 MPASS(_s->lock == &_ih->lock); \ 394 PF_HASHROW_ASSERT(_ih); \ 395 } while (0) 396 #else /* !INVARIANTS */ 397 #define PF_STATE_LOCK_ASSERT(s) do {} while (0) 398 #endif /* INVARIANTS */ 399 400 #ifdef INVARIANTS 401 #define PF_SRC_NODE_LOCK(sn) \ 402 do { \ 403 struct pf_ksrc_node *_sn = (sn); \ 404 struct pf_srchash *_sh = &V_pf_srchash[ \ 405 pf_hashsrc(&_sn->addr, _sn->af)]; \ 406 MPASS(_sn->lock == &_sh->lock); \ 407 mtx_lock(_sn->lock); \ 408 } while (0) 409 #define PF_SRC_NODE_UNLOCK(sn) \ 410 do { \ 411 struct pf_ksrc_node *_sn = (sn); \ 412 struct pf_srchash *_sh = &V_pf_srchash[ \ 413 pf_hashsrc(&_sn->addr, _sn->af)]; \ 414 MPASS(_sn->lock == &_sh->lock); \ 415 mtx_unlock(_sn->lock); \ 416 } while (0) 417 #else 418 #define PF_SRC_NODE_LOCK(sn) mtx_lock((sn)->lock) 419 #define PF_SRC_NODE_UNLOCK(sn) mtx_unlock((sn)->lock) 420 #endif 421 422 #ifdef INVARIANTS 423 #define PF_SRC_NODE_LOCK_ASSERT(sn) \ 424 do { \ 425 struct pf_ksrc_node *_sn = (sn); \ 426 struct pf_srchash *_sh = &V_pf_srchash[ \ 427 pf_hashsrc(&_sn->addr, _sn->af)]; \ 428 MPASS(_sn->lock == &_sh->lock); \ 429 PF_HASHROW_ASSERT(_sh); \ 430 } while (0) 431 #else /* !INVARIANTS */ 432 #define PF_SRC_NODE_LOCK_ASSERT(sn) do {} while (0) 433 #endif /* INVARIANTS */ 434 435 extern struct mtx_padalign pf_unlnkdrules_mtx; 436 #define PF_UNLNKDRULES_LOCK() mtx_lock(&pf_unlnkdrules_mtx) 437 #define PF_UNLNKDRULES_UNLOCK() mtx_unlock(&pf_unlnkdrules_mtx) 438 #define PF_UNLNKDRULES_ASSERT() mtx_assert(&pf_unlnkdrules_mtx, MA_OWNED) 439 440 extern struct sx pf_config_lock; 441 #define PF_CONFIG_LOCK() sx_xlock(&pf_config_lock) 442 #define PF_CONFIG_UNLOCK() sx_xunlock(&pf_config_lock) 443 #define PF_CONFIG_ASSERT() sx_assert(&pf_config_lock, SA_XLOCKED) 444 445 VNET_DECLARE(struct rmlock, pf_rules_lock); 446 #define V_pf_rules_lock VNET(pf_rules_lock) 447 448 #define PF_RULES_RLOCK_TRACKER struct rm_priotracker _pf_rules_tracker 449 #define PF_RULES_RLOCK() rm_rlock(&V_pf_rules_lock, &_pf_rules_tracker) 450 #define PF_RULES_RUNLOCK() rm_runlock(&V_pf_rules_lock, &_pf_rules_tracker) 451 #define PF_RULES_WLOCK() rm_wlock(&V_pf_rules_lock) 452 #define PF_RULES_WUNLOCK() rm_wunlock(&V_pf_rules_lock) 453 #define PF_RULES_WOWNED() rm_wowned(&V_pf_rules_lock) 454 #define PF_RULES_ASSERT() rm_assert(&V_pf_rules_lock, RA_LOCKED) 455 #define PF_RULES_RASSERT() rm_assert(&V_pf_rules_lock, RA_RLOCKED) 456 #define PF_RULES_WASSERT() rm_assert(&V_pf_rules_lock, RA_WLOCKED) 457 458 VNET_DECLARE(struct rmlock, pf_tags_lock); 459 #define V_pf_tags_lock VNET(pf_tags_lock) 460 461 #define PF_TAGS_RLOCK_TRACKER struct rm_priotracker _pf_tags_tracker 462 #define PF_TAGS_RLOCK() rm_rlock(&V_pf_tags_lock, &_pf_tags_tracker) 463 #define PF_TAGS_RUNLOCK() rm_runlock(&V_pf_tags_lock, &_pf_tags_tracker) 464 #define PF_TAGS_WLOCK() rm_wlock(&V_pf_tags_lock) 465 #define PF_TAGS_WUNLOCK() rm_wunlock(&V_pf_tags_lock) 466 #define PF_TAGS_WASSERT() rm_assert(&V_pf_tags_lock, RA_WLOCKED) 467 468 extern struct mtx_padalign pf_table_stats_lock; 469 #define PF_TABLE_STATS_LOCK() mtx_lock(&pf_table_stats_lock) 470 #define PF_TABLE_STATS_UNLOCK() mtx_unlock(&pf_table_stats_lock) 471 #define PF_TABLE_STATS_OWNED() mtx_owned(&pf_table_stats_lock) 472 #define PF_TABLE_STATS_ASSERT() mtx_assert(&pf_table_stats_lock, MA_OWNED) 473 474 extern struct sx pf_end_lock; 475 476 #define PF_MODVER 1 477 #define PFLOG_MODVER 1 478 #define PFSYNC_MODVER 1 479 480 #define PFLOG_MINVER 1 481 #define PFLOG_PREFVER PFLOG_MODVER 482 #define PFLOG_MAXVER 1 483 #define PFSYNC_MINVER 1 484 #define PFSYNC_PREFVER PFSYNC_MODVER 485 #define PFSYNC_MAXVER 1 486 487 #ifdef INET 488 #ifndef INET6 489 #define PF_INET_ONLY 490 #endif /* ! INET6 */ 491 #endif /* INET */ 492 493 #ifdef INET6 494 #ifndef INET 495 #define PF_INET6_ONLY 496 #endif /* ! INET */ 497 #endif /* INET6 */ 498 499 #ifdef INET 500 #ifdef INET6 501 #define PF_INET_INET6 502 #endif /* INET6 */ 503 #endif /* INET */ 504 505 #else 506 507 #define PF_INET_INET6 508 509 #endif /* _KERNEL */ 510 511 /* Both IPv4 and IPv6 */ 512 #ifdef PF_INET_INET6 513 514 #define PF_AEQ(a, b, c) \ 515 ((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \ 516 (c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \ 517 (a)->addr32[2] == (b)->addr32[2] && \ 518 (a)->addr32[1] == (b)->addr32[1] && \ 519 (a)->addr32[0] == (b)->addr32[0])) \ 520 521 #define PF_ANEQ(a, b, c) \ 522 ((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \ 523 (c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \ 524 (a)->addr32[1] != (b)->addr32[1] || \ 525 (a)->addr32[2] != (b)->addr32[2] || \ 526 (a)->addr32[3] != (b)->addr32[3]))) \ 527 528 #define PF_AZERO(a, c) \ 529 ((c == AF_INET && !(a)->addr32[0]) || \ 530 (c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \ 531 !(a)->addr32[2] && !(a)->addr32[3] )) \ 532 533 #else 534 535 /* Just IPv6 */ 536 537 #ifdef PF_INET6_ONLY 538 539 #define PF_AEQ(a, b, c) \ 540 ((a)->addr32[3] == (b)->addr32[3] && \ 541 (a)->addr32[2] == (b)->addr32[2] && \ 542 (a)->addr32[1] == (b)->addr32[1] && \ 543 (a)->addr32[0] == (b)->addr32[0]) \ 544 545 #define PF_ANEQ(a, b, c) \ 546 ((a)->addr32[3] != (b)->addr32[3] || \ 547 (a)->addr32[2] != (b)->addr32[2] || \ 548 (a)->addr32[1] != (b)->addr32[1] || \ 549 (a)->addr32[0] != (b)->addr32[0]) \ 550 551 #define PF_AZERO(a, c) \ 552 (!(a)->addr32[0] && \ 553 !(a)->addr32[1] && \ 554 !(a)->addr32[2] && \ 555 !(a)->addr32[3] ) \ 556 557 #else 558 559 /* Just IPv4 */ 560 #ifdef PF_INET_ONLY 561 562 #define PF_AEQ(a, b, c) \ 563 ((a)->addr32[0] == (b)->addr32[0]) 564 565 #define PF_ANEQ(a, b, c) \ 566 ((a)->addr32[0] != (b)->addr32[0]) 567 568 #define PF_AZERO(a, c) \ 569 (!(a)->addr32[0]) 570 571 #endif /* PF_INET_ONLY */ 572 #endif /* PF_INET6_ONLY */ 573 #endif /* PF_INET_INET6 */ 574 575 #ifdef _KERNEL 576 577 static inline __dead2 void 578 unhandled_af(int af) 579 { 580 panic("unhandled af %d", af); 581 } 582 583 static void inline 584 pf_addrcpy(struct pf_addr *dst, const struct pf_addr *src, sa_family_t af) 585 { 586 switch (af) { 587 #ifdef INET 588 case AF_INET: 589 memcpy(&dst->v4, &src->v4, sizeof(dst->v4)); 590 break; 591 #endif /* INET */ 592 #ifdef INET6 593 case AF_INET6: 594 memcpy(&dst->v6, &src->v6, sizeof(dst->v6)); 595 break; 596 #endif /* INET6 */ 597 default: 598 unhandled_af(af); 599 } 600 } 601 #endif 602 603 /* 604 * XXX callers not FIB-aware in our version of pf yet. 605 * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio. 606 */ 607 #define PF_MISMATCHAW(aw, x, af, neg, ifp, rtid) \ 608 ( \ 609 (((aw)->type == PF_ADDR_NOROUTE && \ 610 pf_routable((x), (af), NULL, (rtid))) || \ 611 (((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL && \ 612 pf_routable((x), (af), (ifp), (rtid))) || \ 613 ((aw)->type == PF_ADDR_TABLE && \ 614 !pfr_match_addr((aw)->p.tbl, (x), (af))) || \ 615 ((aw)->type == PF_ADDR_DYNIFTL && \ 616 !pfi_match_addr((aw)->p.dyn, (x), (af))) || \ 617 ((aw)->type == PF_ADDR_RANGE && \ 618 !pf_match_addr_range(&(aw)->v.a.addr, \ 619 &(aw)->v.a.mask, (x), (af))) || \ 620 ((aw)->type == PF_ADDR_ADDRMASK && \ 621 !PF_AZERO(&(aw)->v.a.mask, (af)) && \ 622 !pf_match_addr(0, &(aw)->v.a.addr, \ 623 &(aw)->v.a.mask, (x), (af))))) != \ 624 (neg) \ 625 ) 626 627 #define PF_ALGNMNT(off) (((off) % 2) == 0) 628 629 /* 630 * At the moment there are no rules which have both NAT and RDR actions, 631 * apart from af-to rules, but those don't to source tracking for address 632 * translation. And the r->rdr pool is used for both NAT and RDR. 633 * So there is no PF_SN_RDR. 634 */ 635 enum pf_sn_types { PF_SN_LIMIT, PF_SN_NAT, PF_SN_ROUTE, PF_SN_MAX }; 636 typedef enum pf_sn_types pf_sn_types_t; 637 #define PF_SN_TYPE_NAMES { \ 638 "limit source-track", \ 639 "NAT/RDR sticky-address", \ 640 "route sticky-address", \ 641 NULL \ 642 } 643 644 #ifdef _KERNEL 645 646 struct pf_kpooladdr { 647 struct pf_addr_wrap addr; 648 TAILQ_ENTRY(pf_kpooladdr) entries; 649 char ifname[IFNAMSIZ]; 650 sa_family_t af; 651 struct pfi_kkif *kif; 652 }; 653 654 TAILQ_HEAD(pf_kpalist, pf_kpooladdr); 655 656 struct pf_kpool { 657 struct mtx mtx; 658 struct pf_kpalist list; 659 struct pf_kpooladdr *cur; 660 struct pf_poolhashkey key; 661 struct pf_addr counter; 662 struct pf_mape_portset mape; 663 int tblidx; 664 u_int16_t proxy_port[2]; 665 u_int8_t opts; 666 sa_family_t ipv6_nexthop_af; 667 }; 668 669 struct pf_rule_actions { 670 struct pf_addr rt_addr; 671 struct pfi_kkif *rt_kif; 672 int32_t rtableid; 673 uint32_t flags; 674 uint16_t qid; 675 uint16_t pqid; 676 uint16_t max_mss; 677 uint16_t dnpipe; 678 uint16_t dnrpipe; /* Reverse direction pipe */ 679 sa_family_t rt_af; 680 uint8_t log; 681 uint8_t set_tos; 682 uint8_t min_ttl; 683 uint8_t set_prio[2]; 684 uint8_t rt; 685 uint8_t allow_opts; 686 uint16_t max_pkt_size; 687 }; 688 689 union pf_keth_rule_ptr { 690 struct pf_keth_rule *ptr; 691 uint32_t nr; 692 }; 693 694 struct pf_keth_rule_addr { 695 uint8_t addr[ETHER_ADDR_LEN]; 696 uint8_t mask[ETHER_ADDR_LEN]; 697 bool neg; 698 uint8_t isset; 699 }; 700 701 struct pf_keth_anchor; 702 703 TAILQ_HEAD(pf_keth_ruleq, pf_keth_rule); 704 705 struct pf_keth_ruleset { 706 struct pf_keth_ruleq rules[2]; 707 struct pf_keth_rules { 708 struct pf_keth_ruleq *rules; 709 int open; 710 uint32_t ticket; 711 } active, inactive; 712 struct vnet *vnet; 713 struct pf_keth_anchor *anchor; 714 }; 715 716 RB_HEAD(pf_keth_anchor_global, pf_keth_anchor); 717 RB_HEAD(pf_keth_anchor_node, pf_keth_anchor); 718 struct pf_keth_anchor { 719 RB_ENTRY(pf_keth_anchor) entry_node; 720 RB_ENTRY(pf_keth_anchor) entry_global; 721 struct pf_keth_anchor *parent; 722 struct pf_keth_anchor_node children; 723 char name[PF_ANCHOR_NAME_SIZE]; 724 char path[MAXPATHLEN]; 725 struct pf_keth_ruleset ruleset; 726 int refcnt; /* anchor rules */ 727 uint8_t anchor_relative; 728 uint8_t anchor_wildcard; 729 }; 730 RB_PROTOTYPE(pf_keth_anchor_node, pf_keth_anchor, entry_node, 731 pf_keth_anchor_compare); 732 RB_PROTOTYPE(pf_keth_anchor_global, pf_keth_anchor, entry_global, 733 pf_keth_anchor_compare); 734 735 struct pf_keth_rule { 736 #define PFE_SKIP_IFP 0 737 #define PFE_SKIP_DIR 1 738 #define PFE_SKIP_PROTO 2 739 #define PFE_SKIP_SRC_ADDR 3 740 #define PFE_SKIP_DST_ADDR 4 741 #define PFE_SKIP_SRC_IP_ADDR 5 742 #define PFE_SKIP_DST_IP_ADDR 6 743 #define PFE_SKIP_COUNT 7 744 union pf_keth_rule_ptr skip[PFE_SKIP_COUNT]; 745 746 TAILQ_ENTRY(pf_keth_rule) entries; 747 748 struct pf_keth_anchor *anchor; 749 u_int8_t anchor_relative; 750 u_int8_t anchor_wildcard; 751 752 uint32_t nr; 753 754 bool quick; 755 756 /* Filter */ 757 char ifname[IFNAMSIZ]; 758 struct pfi_kkif *kif; 759 bool ifnot; 760 uint8_t direction; 761 uint16_t proto; 762 struct pf_keth_rule_addr src, dst; 763 struct pf_rule_addr ipsrc, ipdst; 764 char match_tagname[PF_TAG_NAME_SIZE]; 765 uint16_t match_tag; 766 bool match_tag_not; 767 768 769 /* Stats */ 770 counter_u64_t evaluations; 771 counter_u64_t packets[2]; 772 counter_u64_t bytes[2]; 773 time_t *timestamp; 774 775 /* Action */ 776 char qname[PF_QNAME_SIZE]; 777 int qid; 778 char tagname[PF_TAG_NAME_SIZE]; 779 uint16_t tag; 780 char bridge_to_name[IFNAMSIZ]; 781 struct pfi_kkif *bridge_to; 782 uint8_t action; 783 uint16_t dnpipe; 784 uint32_t dnflags; 785 786 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; 787 uint32_t ridentifier; 788 }; 789 790 struct pf_kthreshold { 791 uint32_t limit; 792 uint32_t seconds; 793 struct counter_rate *cr; 794 }; 795 796 RB_HEAD(pf_krule_global, pf_krule); 797 RB_PROTOTYPE(pf_krule_global, pf_krule, entry_global, pf_krule_compare); 798 799 struct pf_krule { 800 struct pf_rule_addr src; 801 struct pf_rule_addr dst; 802 struct pf_krule *skip[PF_SKIP_COUNT]; 803 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; 804 uint32_t ridentifier; 805 char ifname[IFNAMSIZ]; 806 char rcv_ifname[IFNAMSIZ]; 807 char qname[PF_QNAME_SIZE]; 808 char pqname[PF_QNAME_SIZE]; 809 char tagname[PF_TAG_NAME_SIZE]; 810 char match_tagname[PF_TAG_NAME_SIZE]; 811 812 char overload_tblname[PF_TABLE_NAME_SIZE]; 813 814 TAILQ_ENTRY(pf_krule) entries; 815 struct pf_kpool nat; 816 struct pf_kpool rdr; 817 struct pf_kpool route; 818 struct pf_kthreshold pktrate; 819 820 struct pf_counter_u64 evaluations; 821 struct pf_counter_u64 packets[2]; 822 struct pf_counter_u64 bytes[2]; 823 time_t *timestamp; 824 825 struct pfi_kkif *kif; 826 struct pfi_kkif *rcv_kif; 827 struct pf_kanchor *anchor; 828 struct pfr_ktable *overload_tbl; 829 830 pf_osfp_t os_fingerprint; 831 832 int32_t rtableid; 833 u_int32_t timeout[PFTM_MAX]; 834 u_int32_t max_states; 835 u_int32_t max_src_nodes; 836 u_int32_t max_src_states; 837 u_int32_t max_src_conn; 838 struct { 839 u_int32_t limit; 840 u_int32_t seconds; 841 } max_src_conn_rate; 842 uint16_t max_pkt_size; 843 u_int16_t qid; 844 u_int16_t pqid; 845 u_int16_t dnpipe; 846 u_int16_t dnrpipe; 847 u_int32_t free_flags; 848 u_int32_t nr; 849 u_int32_t prob; 850 uid_t cuid; 851 pid_t cpid; 852 853 counter_u64_t states_cur; 854 counter_u64_t states_tot; 855 counter_u64_t src_nodes[PF_SN_MAX]; 856 857 u_int16_t return_icmp; 858 u_int16_t return_icmp6; 859 u_int16_t max_mss; 860 u_int16_t tag; 861 u_int16_t match_tag; 862 u_int16_t scrub_flags; 863 864 struct pf_rule_uid uid; 865 struct pf_rule_gid gid; 866 867 u_int32_t rule_flag; 868 uint32_t rule_ref; 869 u_int8_t action; 870 u_int8_t direction; 871 u_int8_t log; 872 u_int8_t logif; 873 u_int8_t quick; 874 u_int8_t ifnot; 875 u_int8_t match_tag_not; 876 u_int8_t natpass; 877 878 u_int8_t keep_state; 879 sa_family_t af; 880 u_int8_t proto; 881 uint16_t type; 882 uint16_t code; 883 u_int8_t flags; 884 u_int8_t flagset; 885 u_int8_t min_ttl; 886 u_int8_t allow_opts; 887 u_int8_t rt; 888 u_int8_t return_ttl; 889 u_int8_t tos; 890 u_int8_t set_tos; 891 u_int8_t anchor_relative; 892 u_int8_t anchor_wildcard; 893 894 u_int8_t flush; 895 u_int8_t prio; 896 u_int8_t set_prio[2]; 897 sa_family_t naf; 898 u_int8_t rcvifnot; 899 struct { 900 uint8_t id; 901 int limiter_action; 902 } statelim; 903 struct { 904 uint8_t id; 905 int limiter_action; 906 } sourcelim; 907 908 struct { 909 struct pf_addr addr; 910 u_int16_t port; 911 } divert; 912 u_int8_t md5sum[PF_MD5_DIGEST_LENGTH]; 913 RB_ENTRY(pf_krule) entry_global; 914 915 #ifdef PF_WANT_32_TO_64_COUNTER 916 LIST_ENTRY(pf_krule) allrulelist; 917 bool allrulelinked; 918 #endif 919 time_t exptime; 920 }; 921 922 struct pf_krule_item { 923 SLIST_ENTRY(pf_krule_item) entry; 924 struct pf_krule *r; 925 }; 926 927 SLIST_HEAD(pf_krule_slist, pf_krule_item); 928 929 struct pf_ksrc_node { 930 LIST_ENTRY(pf_ksrc_node) entry; 931 struct pf_addr addr; 932 struct pf_addr raddr; 933 struct pf_krule_slist match_rules; 934 struct pf_krule *rule; 935 struct pfi_kkif *rkif; 936 counter_u64_t bytes[2]; 937 counter_u64_t packets[2]; 938 u_int32_t states; 939 u_int32_t conn; 940 struct pf_kthreshold conn_rate; 941 u_int32_t creation; 942 u_int32_t expire; 943 sa_family_t af; 944 sa_family_t raf; 945 u_int8_t ruletype; 946 pf_sn_types_t type; 947 struct mtx *lock; 948 }; 949 #endif 950 951 struct pf_state_scrub { 952 struct timeval pfss_last; /* time received last packet */ 953 u_int32_t pfss_tsecr; /* last echoed timestamp */ 954 u_int32_t pfss_tsval; /* largest timestamp */ 955 u_int32_t pfss_tsval0; /* original timestamp */ 956 u_int16_t pfss_flags; 957 #define PFSS_TIMESTAMP 0x0001 /* modulate timestamp */ 958 #define PFSS_PAWS 0x0010 /* stricter PAWS checks */ 959 #define PFSS_PAWS_IDLED 0x0020 /* was idle too long. no PAWS */ 960 #define PFSS_DATA_TS 0x0040 /* timestamp on data packets */ 961 #define PFSS_DATA_NOTS 0x0080 /* no timestamp on data packets */ 962 u_int8_t pfss_ttl; /* stashed TTL */ 963 u_int8_t pad; 964 union { 965 u_int32_t pfss_ts_mod; /* timestamp modulation */ 966 u_int32_t pfss_v_tag; /* SCTP verification tag */ 967 }; 968 }; 969 970 struct pf_state_host { 971 struct pf_addr addr; 972 u_int16_t port; 973 u_int16_t pad; 974 }; 975 976 struct pf_state_peer { 977 struct pf_state_scrub *scrub; /* state is scrubbed */ 978 u_int32_t seqlo; /* Max sequence number sent */ 979 u_int32_t seqhi; /* Max the other end ACKd + win */ 980 u_int32_t seqdiff; /* Sequence number modulator */ 981 u_int16_t max_win; /* largest window (pre scaling) */ 982 u_int16_t mss; /* Maximum segment size option */ 983 u_int8_t state; /* active state level */ 984 u_int8_t wscale; /* window scaling factor */ 985 u_int8_t tcp_est; /* Did we reach TCPS_ESTABLISHED */ 986 u_int8_t pad[1]; 987 }; 988 989 /* Keep synced with struct pf_udp_endpoint. */ 990 struct pf_udp_endpoint_cmp { 991 struct pf_addr addr; 992 uint16_t port; 993 sa_family_t af; 994 uint8_t pad[1]; 995 }; 996 997 struct pf_udp_endpoint { 998 struct pf_addr addr; 999 uint16_t port; 1000 sa_family_t af; 1001 uint8_t pad[1]; 1002 1003 struct pf_udp_mapping *mapping; 1004 LIST_ENTRY(pf_udp_endpoint) entry; 1005 }; 1006 1007 struct pf_udp_mapping { 1008 struct pf_udp_endpoint endpoints[2]; 1009 u_int refs; 1010 }; 1011 1012 /* Keep synced with struct pf_state_key. */ 1013 struct pf_state_key_cmp { 1014 struct pf_addr addr[2]; 1015 u_int16_t port[2]; 1016 sa_family_t af; 1017 u_int8_t proto; 1018 u_int8_t pad[2]; 1019 }; 1020 1021 struct pf_state_key { 1022 struct pf_addr addr[2]; 1023 u_int16_t port[2]; 1024 sa_family_t af; 1025 u_int8_t proto; 1026 u_int8_t pad[2]; 1027 1028 LIST_ENTRY(pf_state_key) entry; 1029 TAILQ_HEAD(, pf_kstate) states[2]; 1030 }; 1031 1032 #define PF_REVERSED_KEY(state, family) \ 1033 (((state)->key[PF_SK_WIRE]->af != (state)->key[PF_SK_STACK]->af) && \ 1034 ((state)->key[PF_SK_WIRE]->af != (family)) && \ 1035 ((state)->direction == PF_IN)) 1036 1037 /* Keep synced with struct pf_kstate. */ 1038 struct pf_state_cmp { 1039 u_int64_t id; 1040 u_int32_t creatorid; 1041 u_int8_t direction; 1042 u_int8_t pad[3]; 1043 }; 1044 1045 struct pf_state_scrub_export { 1046 uint16_t pfss_flags; 1047 uint8_t pfss_ttl; /* stashed TTL */ 1048 #define PF_SCRUB_FLAG_VALID 0x01 1049 uint8_t scrub_flag; 1050 uint32_t pfss_ts_mod; /* timestamp modulation */ 1051 } __packed; 1052 1053 struct pf_state_key_export { 1054 struct pf_addr addr[2]; 1055 uint16_t port[2]; 1056 }; 1057 1058 struct pf_state_peer_export { 1059 struct pf_state_scrub_export scrub; /* state is scrubbed */ 1060 uint32_t seqlo; /* Max sequence number sent */ 1061 uint32_t seqhi; /* Max the other end ACKd + win */ 1062 uint32_t seqdiff; /* Sequence number modulator */ 1063 uint16_t max_win; /* largest window (pre scaling) */ 1064 uint16_t mss; /* Maximum segment size option */ 1065 uint8_t state; /* active state level */ 1066 uint8_t wscale; /* window scaling factor */ 1067 uint8_t dummy[6]; 1068 } __packed; 1069 _Static_assert(sizeof(struct pf_state_peer_export) == 32, "size incorrect"); 1070 1071 struct pf_state_export { 1072 uint64_t version; 1073 #define PF_STATE_VERSION 20230404 1074 uint64_t id; 1075 char ifname[IFNAMSIZ]; 1076 char orig_ifname[IFNAMSIZ]; 1077 struct pf_state_key_export key[2]; 1078 struct pf_state_peer_export src; 1079 struct pf_state_peer_export dst; 1080 struct pf_addr rt_addr; 1081 uint32_t rule; 1082 uint32_t anchor; 1083 uint32_t nat_rule; 1084 uint32_t creation; 1085 uint32_t expire; 1086 uint32_t spare0; 1087 uint64_t packets[2]; 1088 uint64_t bytes[2]; 1089 uint32_t creatorid; 1090 uint32_t spare1; 1091 sa_family_t af; 1092 uint8_t proto; 1093 uint8_t direction; 1094 uint8_t log; 1095 uint8_t state_flags_compat; 1096 uint8_t timeout; 1097 uint8_t sync_flags; 1098 uint8_t updates; 1099 uint16_t state_flags; 1100 uint16_t qid; 1101 uint16_t pqid; 1102 uint16_t dnpipe; 1103 uint16_t dnrpipe; 1104 int32_t rtableid; 1105 uint8_t min_ttl; 1106 uint8_t set_tos; 1107 uint16_t max_mss; 1108 uint8_t set_prio[2]; 1109 uint8_t rt; 1110 char rt_ifname[IFNAMSIZ]; 1111 1112 uint8_t spare[72]; 1113 }; 1114 _Static_assert(sizeof(struct pf_state_export) == 384, "size incorrect"); 1115 1116 #define PF_STATELIM_NAME_LEN 16 /* kstat istr */ 1117 #define PF_STATELIM_DESCR_LEN 64 1118 1119 #define PF_SOURCELIM_NAME_LEN 16 /* kstat istr */ 1120 #define PF_SOURCELIM_DESCR_LEN 64 1121 1122 #ifdef _KERNEL 1123 struct kstat; 1124 1125 /* 1126 * PF state links 1127 * 1128 * This is used to augment a struct pf_state so it can be 1129 * tracked/referenced by the state and source address limiter things. 1130 * Each limiter maintains a list of the states they "own", and these 1131 * state links are what the limiters use to wire a state into their 1132 * lists. 1133 * 1134 * Without PF state links, the pf_state struct would have to grow 1135 * a lot to support a feature that may not be used. 1136 * 1137 * pfl_entry is used by the pools to add states to their list. 1138 * pfl_state allows the pools to get from their list of states to 1139 * the states themselves. 1140 * 1141 * pfl_link allows operations on states (well, delete) to be able 1142 * to quickly locate the pf_state_link struct so they can be unwired 1143 * from the pools. 1144 */ 1145 1146 #define PF_STATE_LINK_TYPE_STATELIM 1 1147 #define PF_STATE_LINK_TYPE_SOURCELIM 2 1148 1149 struct pf_kstate; 1150 struct pf_state_link { 1151 /* used by source/state pools to get to states */ 1152 TAILQ_ENTRY(pf_state_link) pfl_link; 1153 1154 /* used by pf_state to get to source/state pools */ 1155 SLIST_ENTRY(pf_state_link) pfl_linkage; 1156 1157 struct pf_kstate *pfl_state; 1158 unsigned int pfl_type; 1159 }; 1160 1161 TAILQ_HEAD(pf_state_link_list, pf_state_link); 1162 SLIST_HEAD(pf_state_linkage, pf_state_link); 1163 1164 struct pf_kstate { 1165 /* 1166 * Area shared with pf_state_cmp 1167 */ 1168 u_int64_t id; 1169 u_int32_t creatorid; 1170 u_int8_t direction; 1171 u_int8_t pad[3]; 1172 /* 1173 * end of the area 1174 */ 1175 1176 u_int16_t state_flags; 1177 u_int8_t timeout; 1178 u_int8_t sync_state; /* PFSYNC_S_x */ 1179 u_int8_t sync_updates; 1180 u_int refs; 1181 struct mtx *lock; 1182 TAILQ_ENTRY(pf_kstate) sync_list; 1183 TAILQ_ENTRY(pf_kstate) key_list[2]; 1184 LIST_ENTRY(pf_kstate) entry; 1185 struct pf_state_peer src; 1186 struct pf_state_peer dst; 1187 struct pf_krule_slist match_rules; 1188 struct pf_krule *rule; 1189 struct pf_krule *anchor; 1190 struct pf_krule *nat_rule; 1191 struct pf_state_key *key[2]; /* addresses stack and wire */ 1192 struct pf_udp_mapping *udp_mapping; 1193 struct pfi_kkif *kif; 1194 struct pfi_kkif *orig_kif; /* The real kif, even if we're a floating state (i.e. if == V_pfi_all). */ 1195 struct pf_ksrc_node *sns[PF_SN_MAX];/* source nodes */ 1196 u_int64_t packets[2]; 1197 u_int64_t bytes[2]; 1198 u_int64_t creation; 1199 u_int64_t expire; 1200 u_int32_t pfsync_time; 1201 struct pf_rule_actions act; 1202 u_int16_t tag; 1203 u_int16_t if_index_in; 1204 u_int16_t if_index_out; 1205 uint8_t statelim; 1206 uint8_t sourcelim; 1207 struct pf_state_linkage linkage; 1208 }; 1209 1210 /* 1211 * State limiter 1212 */ 1213 1214 struct pf_limiter_rate { 1215 unsigned int limit; 1216 unsigned int seconds; 1217 }; 1218 1219 struct pf_statelim { 1220 RB_ENTRY(pf_statelim) pfstlim_id_tree; 1221 RB_ENTRY(pf_statelim) pfstlim_nm_tree; 1222 TAILQ_ENTRY(pf_statelim) pfstlim_list; 1223 struct kstat *pfstlim_ks; 1224 1225 uint32_t pfstlim_id; 1226 char pfstlim_nm[PF_STATELIM_NAME_LEN]; 1227 1228 /* config */ 1229 1230 unsigned int pfstlim_limit; 1231 struct pf_limiter_rate pfstlim_rate; 1232 1233 /* run state */ 1234 struct mtx pfstlim_lock; 1235 1236 /* rate limiter */ 1237 uint64_t pfstlim_rate_ts; 1238 uint64_t pfstlim_rate_token; 1239 uint64_t pfstlim_rate_bucket; 1240 1241 unsigned int pfstlim_inuse; 1242 struct pf_state_link_list pfstlim_states; 1243 1244 /* counters */ 1245 1246 struct { 1247 uint64_t admitted; 1248 uint64_t hardlimited; 1249 uint64_t ratelimited; 1250 } pfstlim_counters; 1251 1252 struct { 1253 time_t created; 1254 time_t updated; 1255 time_t cleared; 1256 } pfstlim_timestamps; 1257 }; 1258 1259 RB_HEAD(pf_statelim_id_tree, pf_statelim); 1260 RB_PROTOTYPE(pf_statelim_id_tree, pf_statelim, pfstlim_id_tree, cmp); 1261 1262 RB_HEAD(pf_statelim_nm_tree, pf_statelim); 1263 RB_PROTOTYPE(pf_statelim_nm_tree, pf_statelim, pfstlim_nm_tree, cmp); 1264 1265 TAILQ_HEAD(pf_statelim_list, pf_statelim); 1266 1267 VNET_DECLARE(struct pf_statelim_id_tree, pf_statelim_id_tree_active); 1268 #define V_pf_statelim_id_tree_active VNET(pf_statelim_id_tree_active) 1269 VNET_DECLARE(struct pf_statelim_list, pf_statelim_list_active); 1270 #define V_pf_statelim_list_active VNET(pf_statelim_list_active) 1271 1272 VNET_DECLARE(struct pf_statelim_id_tree, pf_statelim_id_tree_inactive); 1273 #define V_pf_statelim_id_tree_inactive VNET(pf_statelim_id_tree_inactive) 1274 VNET_DECLARE(struct pf_statelim_nm_tree, pf_statelim_nm_tree_inactive); 1275 #define V_pf_statelim_nm_tree_inactive VNET(pf_statelim_nm_tree_inactive) 1276 VNET_DECLARE(struct pf_statelim_list, pf_statelim_list_inactive); 1277 #define V_pf_statelim_list_inactive VNET(pf_statelim_list_inactive) 1278 1279 static inline unsigned int 1280 pf_statelim_enter(struct pf_statelim *pfstlim) 1281 { 1282 mtx_lock(&pfstlim->pfstlim_lock); 1283 1284 return (0); 1285 } 1286 1287 static inline void 1288 pf_statelim_leave(struct pf_statelim *pfstlim, unsigned int gen) 1289 { 1290 return (mtx_unlock(&pfstlim->pfstlim_lock)); 1291 } 1292 1293 /* 1294 * Source address pools 1295 */ 1296 1297 struct pf_sourcelim; 1298 1299 struct pf_source { 1300 RB_ENTRY(pf_source) pfsr_tree; 1301 RB_ENTRY(pf_source) pfsr_ioc_tree; 1302 struct pf_sourcelim *pfsr_parent; 1303 1304 sa_family_t pfsr_af; 1305 u_int16_t pfsr_rdomain; 1306 struct pf_addr pfsr_addr; 1307 1308 /* run state */ 1309 1310 unsigned int pfsr_inuse; 1311 unsigned int pfsr_intable; 1312 struct pf_state_link_list pfsr_states; 1313 time_t pfsr_empty_ts; 1314 TAILQ_ENTRY(pf_source) pfsr_empty_gc; 1315 1316 /* rate limiter */ 1317 uint64_t pfsr_rate_ts; 1318 1319 struct { 1320 uint64_t admitted; 1321 uint64_t hardlimited; 1322 uint64_t ratelimited; 1323 } pfsr_counters; 1324 }; 1325 1326 RB_HEAD(pf_source_tree, pf_source); 1327 RB_PROTOTYPE(pf_source_tree, pf_source, pfsr_tree, cmp); 1328 1329 RB_HEAD(pf_source_ioc_tree, pf_source); 1330 RB_PROTOTYPE(pf_source_ioc_tree, pf_source, pfsr_ioc_tree, cmp); 1331 1332 TAILQ_HEAD(pf_source_list, pf_source); 1333 1334 struct pf_sourcelim { 1335 RB_ENTRY(pf_sourcelim) pfsrlim_id_tree; 1336 RB_ENTRY(pf_sourcelim) pfsrlim_nm_tree; 1337 TAILQ_ENTRY(pf_sourcelim) pfsrlim_list; 1338 struct kstat *pfsrlim_ks; 1339 1340 uint32_t pfsrlim_id; 1341 char pfsrlim_nm[PF_SOURCELIM_NAME_LEN]; 1342 unsigned int pfsrlim_disabled; 1343 1344 /* config */ 1345 1346 unsigned int pfsrlim_entries; 1347 unsigned int pfsrlim_limit; 1348 unsigned int pfsrlim_ipv4_prefix; 1349 unsigned int pfsrlim_ipv6_prefix; 1350 1351 struct pf_limiter_rate pfsrlim_rate; 1352 1353 struct { 1354 char name[PF_TABLE_NAME_SIZE]; 1355 unsigned int hwm; 1356 unsigned int lwm; 1357 struct pfr_ktable *table; 1358 } pfsrlim_overload; 1359 1360 /* run state */ 1361 struct mtx pfsrlim_lock; 1362 1363 struct pf_addr pfsrlim_ipv4_mask; 1364 struct pf_addr pfsrlim_ipv6_mask; 1365 1366 uint64_t pfsrlim_rate_token; 1367 uint64_t pfsrlim_rate_bucket; 1368 1369 /* number of pf_sources */ 1370 unsigned int pfsrlim_nsources; 1371 struct pf_source_tree pfsrlim_sources; 1372 struct pf_source_ioc_tree pfsrlim_ioc_sources; 1373 1374 struct { 1375 /* number of times pf_source was allocated */ 1376 uint64_t addrallocs; 1377 /* state was rejected because the address limit was hit */ 1378 uint64_t addrlimited; 1379 /* no memory to create address thing */ 1380 uint64_t addrnomem; 1381 1382 /* sum of pf_source inuse gauges */ 1383 uint64_t inuse; 1384 /* sum of pf_source admitted counters */ 1385 uint64_t admitted; 1386 /* sum of pf_source hardlimited counters */ 1387 uint64_t hardlimited; 1388 /* sum of pf_source ratelimited counters */ 1389 uint64_t ratelimited; 1390 } pfsrlim_counters; 1391 }; 1392 1393 RB_HEAD(pf_sourcelim_id_tree, pf_sourcelim); 1394 RB_PROTOTYPE(pf_sourcelim_id_tree, pf_sourcelim, pfsrlim_id_tree, cmp); 1395 1396 RB_HEAD(pf_sourcelim_nm_tree, pf_sourcelim); 1397 RB_PROTOTYPE(pf_sourcelim_nm_tree, pf_sourcelim, pfsrlim_nm_tree, cmp); 1398 1399 TAILQ_HEAD(pf_sourcelim_list, pf_sourcelim); 1400 1401 VNET_DECLARE(struct pf_sourcelim_id_tree, pf_sourcelim_id_tree_active); 1402 #define V_pf_sourcelim_id_tree_active VNET(pf_sourcelim_id_tree_active) 1403 VNET_DECLARE(struct pf_sourcelim_list, pf_sourcelim_list_active); 1404 #define V_pf_sourcelim_list_active VNET(pf_sourcelim_list_active) 1405 1406 VNET_DECLARE(struct pf_sourcelim_id_tree, pf_sourcelim_id_tree_inactive); 1407 #define V_pf_sourcelim_id_tree_inactive VNET(pf_sourcelim_id_tree_inactive) 1408 VNET_DECLARE(struct pf_sourcelim_nm_tree, pf_sourcelim_nm_tree_inactive); 1409 #define V_pf_sourcelim_nm_tree_inactive VNET(pf_sourcelim_nm_tree_inactive) 1410 VNET_DECLARE(struct pf_sourcelim_list, pf_sourcelim_list_inactive); 1411 #define V_pf_sourcelim_list_inactive VNET(pf_sourcelim_list_inactive) 1412 1413 static inline unsigned int 1414 pf_sourcelim_enter(struct pf_sourcelim *pfsrlim) 1415 { 1416 mtx_lock(&pfsrlim->pfsrlim_lock); 1417 return (0); 1418 } 1419 1420 static inline void 1421 pf_sourcelim_leave(struct pf_sourcelim *pfsrlim, unsigned int gen) 1422 { 1423 mtx_unlock(&pfsrlim->pfsrlim_lock); 1424 } 1425 1426 enum pf_test_status { 1427 PF_TEST_FAIL = -1, 1428 PF_TEST_OK, 1429 PF_TEST_QUICK 1430 }; 1431 1432 struct pf_test_ctx { 1433 enum pf_test_status test_status; 1434 struct pf_pdesc *pd; 1435 struct pf_rule_actions act; 1436 uint8_t icmpcode; 1437 uint8_t icmptype; 1438 int icmp_dir; 1439 int state_icmp; 1440 int tag; 1441 int rewrite; 1442 int limiter_drop; 1443 u_short reason; 1444 struct pf_krule_slist *match_rules; 1445 struct pf_krule_item *last_match_rule; 1446 struct pf_krule *nr; 1447 struct pf_krule *tr; 1448 struct pf_krule **rm; 1449 struct pf_krule *a; 1450 struct pf_krule **am; 1451 struct pf_kruleset **rsm; 1452 struct pf_kruleset *arsm; 1453 struct pf_kruleset *aruleset; 1454 struct pf_state_key *sk; 1455 struct pf_state_key *nk; 1456 struct tcphdr *th; 1457 struct pf_udp_mapping *udp_mapping; 1458 struct pf_kpool *nat_pool; 1459 uint16_t virtual_type; 1460 uint16_t virtual_id; 1461 int depth; 1462 struct pf_statelim *statelim; 1463 struct pf_sourcelim *sourcelim; 1464 struct pf_source *source; 1465 }; 1466 1467 #define PF_ANCHOR_STACK_MAX 32 1468 #endif 1469 1470 /* 1471 * Unified state structures for pulling states out of the kernel 1472 * used by pfsync(4) and the pf(4) ioctl. 1473 */ 1474 struct pfsync_state_key { 1475 struct pf_addr addr[2]; 1476 u_int16_t port[2]; 1477 }; 1478 1479 struct pfsync_state_1301 { 1480 u_int64_t id; 1481 char ifname[IFNAMSIZ]; 1482 struct pfsync_state_key key[2]; 1483 struct pf_state_peer_export src; 1484 struct pf_state_peer_export dst; 1485 struct pf_addr rt_addr; 1486 u_int32_t rule; 1487 u_int32_t anchor; 1488 u_int32_t nat_rule; 1489 u_int32_t creation; 1490 u_int32_t expire; 1491 u_int32_t packets[2][2]; 1492 u_int32_t bytes[2][2]; 1493 u_int32_t creatorid; 1494 sa_family_t af; 1495 u_int8_t proto; 1496 u_int8_t direction; 1497 u_int8_t __spare[2]; 1498 u_int8_t log; 1499 u_int8_t state_flags; 1500 u_int8_t timeout; 1501 u_int8_t sync_flags; 1502 u_int8_t updates; /* unused */ 1503 } __packed; 1504 1505 struct pfsync_state_1400 { 1506 /* The beginning of the struct is compatible with pfsync_state_1301 */ 1507 u_int64_t id; 1508 char ifname[IFNAMSIZ]; 1509 struct pfsync_state_key key[2]; 1510 struct pf_state_peer_export src; 1511 struct pf_state_peer_export dst; 1512 struct pf_addr rt_addr; 1513 u_int32_t rule; 1514 u_int32_t anchor; 1515 u_int32_t nat_rule; 1516 u_int32_t creation; 1517 u_int32_t expire; 1518 u_int32_t packets[2][2]; 1519 u_int32_t bytes[2][2]; 1520 u_int32_t creatorid; 1521 sa_family_t af; 1522 u_int8_t proto; 1523 u_int8_t direction; 1524 u_int16_t state_flags; 1525 u_int8_t log; 1526 u_int8_t __spare; 1527 u_int8_t timeout; 1528 u_int8_t sync_flags; 1529 u_int8_t updates; /* unused */ 1530 /* The rest is not */ 1531 u_int16_t qid; 1532 u_int16_t pqid; 1533 u_int16_t dnpipe; 1534 u_int16_t dnrpipe; 1535 int32_t rtableid; 1536 u_int8_t min_ttl; 1537 u_int8_t set_tos; 1538 u_int16_t max_mss; 1539 u_int8_t set_prio[2]; 1540 u_int8_t rt; 1541 char rt_ifname[IFNAMSIZ]; 1542 } __packed; 1543 1544 struct pfsync_state_1500 { 1545 /* The beginning of the struct is compatible with pfsync_state_1301 */ 1546 u_int64_t id; 1547 char ifname[IFNAMSIZ]; 1548 struct pfsync_state_key key[2]; 1549 struct pf_state_peer_export src; 1550 struct pf_state_peer_export dst; 1551 struct pf_addr rt_addr; 1552 u_int32_t rule; 1553 u_int32_t anchor; 1554 u_int32_t nat_rule; 1555 u_int32_t creation; 1556 u_int32_t expire; 1557 u_int32_t packets[2][2]; 1558 u_int32_t bytes[2][2]; 1559 u_int32_t creatorid; 1560 /* The rest is not, use the opportunity to fix alignment */ 1561 char tagname[PF_TAG_NAME_SIZE]; 1562 char rt_ifname[IFNAMSIZ]; 1563 char orig_ifname[IFNAMSIZ]; 1564 int32_t rtableid; 1565 u_int16_t state_flags; 1566 u_int16_t qid; 1567 u_int16_t pqid; 1568 u_int16_t dnpipe; 1569 u_int16_t dnrpipe; 1570 u_int16_t max_mss; 1571 sa_family_t wire_af; 1572 sa_family_t stack_af; 1573 sa_family_t rt_af; 1574 u_int8_t wire_proto; 1575 u_int8_t stack_proto; 1576 u_int8_t log; 1577 u_int8_t timeout; 1578 u_int8_t direction; 1579 u_int8_t rt; 1580 u_int8_t min_ttl; 1581 u_int8_t set_tos; 1582 u_int8_t set_prio[2]; 1583 u_int8_t spare[3]; /* Improve struct alignment */ 1584 } __packed; 1585 1586 union pfsync_state_union { 1587 struct pfsync_state_1301 pfs_1301; 1588 struct pfsync_state_1400 pfs_1400; 1589 struct pfsync_state_1500 pfs_1500; 1590 } __packed; 1591 1592 #ifdef _KERNEL 1593 /* pfsync */ 1594 typedef int pfsync_state_import_t(union pfsync_state_union *, int, int); 1595 typedef void pfsync_insert_state_t(struct pf_kstate *); 1596 typedef void pfsync_update_state_t(struct pf_kstate *); 1597 typedef void pfsync_delete_state_t(struct pf_kstate *); 1598 typedef void pfsync_clear_states_t(u_int32_t, const char *); 1599 typedef int pfsync_defer_t(struct pf_kstate *, struct mbuf *); 1600 typedef void pfsync_detach_ifnet_t(struct ifnet *); 1601 typedef void pflow_export_state_t(const struct pf_kstate *); 1602 typedef bool pf_addr_filter_func_t(const sa_family_t, const struct pf_addr *); 1603 1604 VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr); 1605 #define V_pfsync_state_import_ptr VNET(pfsync_state_import_ptr) 1606 VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr); 1607 #define V_pfsync_insert_state_ptr VNET(pfsync_insert_state_ptr) 1608 VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr); 1609 #define V_pfsync_update_state_ptr VNET(pfsync_update_state_ptr) 1610 VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr); 1611 #define V_pfsync_delete_state_ptr VNET(pfsync_delete_state_ptr) 1612 VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr); 1613 #define V_pfsync_clear_states_ptr VNET(pfsync_clear_states_ptr) 1614 VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr); 1615 #define V_pfsync_defer_ptr VNET(pfsync_defer_ptr) 1616 VNET_DECLARE(pflow_export_state_t *, pflow_export_state_ptr); 1617 #define V_pflow_export_state_ptr VNET(pflow_export_state_ptr) 1618 extern pfsync_detach_ifnet_t *pfsync_detach_ifnet_ptr; 1619 1620 void pfsync_state_export_1301(struct pfsync_state_1301 *, 1621 struct pf_kstate *); 1622 void pfsync_state_export_1400(struct pfsync_state_1400 *, 1623 struct pf_kstate *); 1624 void pfsync_state_export_1500(struct pfsync_state_1500 *, 1625 struct pf_kstate *); 1626 void pf_state_export(struct pf_state_export *, 1627 struct pf_kstate *); 1628 1629 /* pflog */ 1630 struct pf_kruleset; 1631 struct pf_pdesc; 1632 typedef int pflog_packet_t(uint8_t, u_int8_t, 1633 struct pf_krule *, struct pf_krule *, struct pf_kruleset *, 1634 struct pf_pdesc *, int, struct pf_krule *); 1635 extern pflog_packet_t *pflog_packet_ptr; 1636 1637 #endif /* _KERNEL */ 1638 1639 #define PFSYNC_FLAG_SRCNODE 0x04 1640 #define PFSYNC_FLAG_NATSRCNODE 0x08 1641 1642 /* for copies to/from network byte order */ 1643 /* ioctl interface also uses network byte order */ 1644 void pf_state_peer_hton(const struct pf_state_peer *, 1645 struct pf_state_peer_export *); 1646 void pf_state_peer_ntoh(const struct pf_state_peer_export *, 1647 struct pf_state_peer *); 1648 1649 #define pf_state_counter_hton(s,d) do { \ 1650 d[0] = htonl((s>>32)&0xffffffff); \ 1651 d[1] = htonl(s&0xffffffff); \ 1652 } while (0) 1653 1654 #define pf_state_counter_from_pfsync(s) \ 1655 (((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1])) 1656 1657 #define pf_state_counter_ntoh(s,d) do { \ 1658 d = ntohl(s[0]); \ 1659 d = d<<32; \ 1660 d += ntohl(s[1]); \ 1661 } while (0) 1662 1663 TAILQ_HEAD(pf_krulequeue, pf_krule); 1664 1665 struct pf_kanchor; 1666 1667 struct pf_kruleset { 1668 struct { 1669 struct pf_krulequeue queues[2]; 1670 struct { 1671 struct pf_krulequeue *ptr; 1672 u_int32_t rcount; 1673 u_int32_t ticket; 1674 int open; 1675 struct pf_krule_global *tree; 1676 } active, inactive; 1677 } rules[PF_RULESET_MAX]; 1678 struct pf_kanchor *anchor; 1679 u_int32_t tticket; 1680 int tables; 1681 int topen; 1682 }; 1683 1684 RB_HEAD(pf_kanchor_global, pf_kanchor); 1685 RB_HEAD(pf_kanchor_node, pf_kanchor); 1686 struct pf_kanchor { 1687 RB_ENTRY(pf_kanchor) entry_global; 1688 RB_ENTRY(pf_kanchor) entry_node; 1689 struct pf_kanchor *parent; 1690 struct pf_kanchor_node children; 1691 char name[PF_ANCHOR_NAME_SIZE]; 1692 char path[MAXPATHLEN]; 1693 struct pf_kruleset ruleset; 1694 int refcnt; /* anchor rules */ 1695 }; 1696 RB_PROTOTYPE(pf_kanchor_global, pf_kanchor, entry_global, pf_anchor_compare); 1697 RB_PROTOTYPE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare); 1698 1699 #define PF_RESERVED_ANCHOR "_pf" 1700 1701 #define PFR_TFLAG_PERSIST 0x00000001 1702 #define PFR_TFLAG_CONST 0x00000002 1703 #define PFR_TFLAG_ACTIVE 0x00000004 1704 #define PFR_TFLAG_INACTIVE 0x00000008 1705 #define PFR_TFLAG_REFERENCED 0x00000010 1706 #define PFR_TFLAG_REFDANCHOR 0x00000020 1707 #define PFR_TFLAG_COUNTERS 0x00000040 1708 /* Adjust masks below when adding flags. */ 1709 #define PFR_TFLAG_USRMASK (PFR_TFLAG_PERSIST | \ 1710 PFR_TFLAG_CONST | \ 1711 PFR_TFLAG_COUNTERS) 1712 #define PFR_TFLAG_SETMASK (PFR_TFLAG_ACTIVE | \ 1713 PFR_TFLAG_INACTIVE | \ 1714 PFR_TFLAG_REFERENCED | \ 1715 PFR_TFLAG_REFDANCHOR) 1716 #define PFR_TFLAG_ALLMASK (PFR_TFLAG_PERSIST | \ 1717 PFR_TFLAG_CONST | \ 1718 PFR_TFLAG_ACTIVE | \ 1719 PFR_TFLAG_INACTIVE | \ 1720 PFR_TFLAG_REFERENCED | \ 1721 PFR_TFLAG_REFDANCHOR | \ 1722 PFR_TFLAG_COUNTERS) 1723 1724 struct pf_keth_anchor_stackframe; 1725 1726 struct pfr_table { 1727 char pfrt_anchor[MAXPATHLEN]; 1728 char pfrt_name[PF_TABLE_NAME_SIZE]; 1729 u_int32_t pfrt_flags; 1730 u_int8_t pfrt_fback; 1731 }; 1732 1733 enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED, 1734 PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE, 1735 PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX }; 1736 1737 struct pfr_addr { 1738 union { 1739 struct in_addr _pfra_ip4addr; 1740 struct in6_addr _pfra_ip6addr; 1741 } pfra_u; 1742 u_int8_t pfra_af; 1743 u_int8_t pfra_net; 1744 u_int8_t pfra_not; 1745 u_int8_t pfra_fback; 1746 }; 1747 #define pfra_ip4addr pfra_u._pfra_ip4addr 1748 #define pfra_ip6addr pfra_u._pfra_ip6addr 1749 1750 enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX }; 1751 enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX }; 1752 enum { PFR_TYPE_PACKETS, PFR_TYPE_BYTES, PFR_TYPE_MAX }; 1753 #define PFR_NUM_COUNTERS (PFR_DIR_MAX * PFR_OP_ADDR_MAX * PFR_TYPE_MAX) 1754 #define PFR_OP_XPASS PFR_OP_ADDR_MAX 1755 1756 struct pfr_astats { 1757 struct pfr_addr pfras_a; 1758 u_int64_t pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 1759 u_int64_t pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 1760 time_t pfras_tzero; 1761 }; 1762 1763 enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX }; 1764 1765 struct pfr_tstats { 1766 struct pfr_table pfrts_t; 1767 u_int64_t pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1768 u_int64_t pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1769 u_int64_t pfrts_match; 1770 u_int64_t pfrts_nomatch; 1771 time_t pfrts_tzero; 1772 int pfrts_cnt; 1773 int pfrts_refcnt[PFR_REFCNT_MAX]; 1774 }; 1775 1776 #ifdef _KERNEL 1777 1778 struct pfr_kstate_counter { 1779 counter_u64_t pkc_pcpu; 1780 u_int64_t pkc_zero; 1781 }; 1782 1783 static inline int 1784 pfr_kstate_counter_init(struct pfr_kstate_counter *pfrc, int flags) 1785 { 1786 1787 pfrc->pkc_zero = 0; 1788 pfrc->pkc_pcpu = counter_u64_alloc(flags); 1789 if (pfrc->pkc_pcpu == NULL) 1790 return (ENOMEM); 1791 return (0); 1792 } 1793 1794 static inline void 1795 pfr_kstate_counter_deinit(struct pfr_kstate_counter *pfrc) 1796 { 1797 1798 counter_u64_free(pfrc->pkc_pcpu); 1799 } 1800 1801 static inline u_int64_t 1802 pfr_kstate_counter_fetch(struct pfr_kstate_counter *pfrc) 1803 { 1804 u_int64_t c; 1805 1806 c = counter_u64_fetch(pfrc->pkc_pcpu); 1807 c -= pfrc->pkc_zero; 1808 return (c); 1809 } 1810 1811 static inline void 1812 pfr_kstate_counter_zero(struct pfr_kstate_counter *pfrc) 1813 { 1814 u_int64_t c; 1815 1816 c = counter_u64_fetch(pfrc->pkc_pcpu); 1817 pfrc->pkc_zero = c; 1818 } 1819 1820 static inline void 1821 pfr_kstate_counter_add(struct pfr_kstate_counter *pfrc, int64_t n) 1822 { 1823 1824 counter_u64_add(pfrc->pkc_pcpu, n); 1825 } 1826 1827 struct pfr_ktstats { 1828 struct pfr_table pfrts_t; 1829 struct pfr_kstate_counter pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1830 struct pfr_kstate_counter pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1831 struct pfr_kstate_counter pfrkts_match; 1832 struct pfr_kstate_counter pfrkts_nomatch; 1833 time_t pfrkts_tzero; 1834 int pfrkts_cnt; 1835 int pfrkts_refcnt[PFR_REFCNT_MAX]; 1836 }; 1837 1838 #endif /* _KERNEL */ 1839 1840 #define pfrts_name pfrts_t.pfrt_name 1841 #define pfrts_flags pfrts_t.pfrt_flags 1842 1843 #ifndef _SOCKADDR_UNION_DEFINED 1844 #define _SOCKADDR_UNION_DEFINED 1845 union sockaddr_union { 1846 struct sockaddr sa; 1847 struct sockaddr_in sin; 1848 struct sockaddr_in6 sin6; 1849 }; 1850 #endif /* _SOCKADDR_UNION_DEFINED */ 1851 1852 struct pfr_kcounters { 1853 counter_u64_t pfrkc_counters; 1854 time_t pfrkc_tzero; 1855 }; 1856 #define pfr_kentry_counter(kc, dir, op, t) \ 1857 ((kc)->pfrkc_counters + \ 1858 (dir) * PFR_OP_ADDR_MAX * PFR_TYPE_MAX + (op) * PFR_TYPE_MAX + (t)) 1859 1860 #ifdef _KERNEL 1861 SLIST_HEAD(pfr_kentryworkq, pfr_kentry); 1862 struct pfr_kentry { 1863 struct radix_node pfrke_node[2]; 1864 union sockaddr_union pfrke_sa; 1865 SLIST_ENTRY(pfr_kentry) pfrke_workq; 1866 struct pfr_kcounters pfrke_counters; 1867 u_int8_t pfrke_af; 1868 u_int8_t pfrke_net; 1869 u_int8_t pfrke_not; 1870 u_int8_t pfrke_mark; 1871 }; 1872 1873 SLIST_HEAD(pfr_ktableworkq, pfr_ktable); 1874 RB_HEAD(pfr_ktablehead, pfr_ktable); 1875 struct pfr_ktable { 1876 struct pfr_ktstats pfrkt_kts; 1877 RB_ENTRY(pfr_ktable) pfrkt_tree; 1878 SLIST_ENTRY(pfr_ktable) pfrkt_workq; 1879 struct radix_node_head *pfrkt_ip4; 1880 struct radix_node_head *pfrkt_ip6; 1881 struct pfr_ktable *pfrkt_shadow; 1882 struct pfr_ktable *pfrkt_root; 1883 struct pf_kruleset *pfrkt_rs; 1884 long pfrkt_larg; 1885 int pfrkt_nflags; 1886 }; 1887 #define pfrkt_t pfrkt_kts.pfrts_t 1888 #define pfrkt_name pfrkt_t.pfrt_name 1889 #define pfrkt_anchor pfrkt_t.pfrt_anchor 1890 #define pfrkt_ruleset pfrkt_t.pfrt_ruleset 1891 #define pfrkt_flags pfrkt_t.pfrt_flags 1892 #define pfrkt_cnt pfrkt_kts.pfrkts_cnt 1893 #define pfrkt_refcnt pfrkt_kts.pfrkts_refcnt 1894 #define pfrkt_packets pfrkt_kts.pfrkts_packets 1895 #define pfrkt_bytes pfrkt_kts.pfrkts_bytes 1896 #define pfrkt_match pfrkt_kts.pfrkts_match 1897 #define pfrkt_nomatch pfrkt_kts.pfrkts_nomatch 1898 #define pfrkt_tzero pfrkt_kts.pfrkts_tzero 1899 #endif 1900 1901 #ifdef _KERNEL 1902 struct pfi_kkif { 1903 char pfik_name[IFNAMSIZ]; 1904 union { 1905 RB_ENTRY(pfi_kkif) _pfik_tree; 1906 LIST_ENTRY(pfi_kkif) _pfik_list; 1907 } _pfik_glue; 1908 #define pfik_tree _pfik_glue._pfik_tree 1909 #define pfik_list _pfik_glue._pfik_list 1910 struct pf_counter_u64 pfik_packets[2][2][2]; 1911 struct pf_counter_u64 pfik_bytes[2][2][2]; 1912 time_t pfik_tzero; 1913 u_int pfik_flags; 1914 struct ifnet *pfik_ifp; 1915 struct ifg_group *pfik_group; 1916 u_int pfik_rulerefs; 1917 TAILQ_HEAD(, pfi_dynaddr) pfik_dynaddrs; 1918 #ifdef PF_WANT_32_TO_64_COUNTER 1919 LIST_ENTRY(pfi_kkif) pfik_allkiflist; 1920 #endif 1921 }; 1922 #endif 1923 1924 #define PFI_IFLAG_REFS 0x0001 /* has state references */ 1925 #define PFI_IFLAG_SKIP 0x0100 /* skip filtering on interface */ 1926 #define PFI_IFLAG_ANY 0x0200 /* match any non-loopback interface */ 1927 1928 #ifdef _KERNEL 1929 struct pf_sctp_multihome_job; 1930 TAILQ_HEAD(pf_sctp_multihome_jobs, pf_sctp_multihome_job); 1931 1932 struct pf_pdesc { 1933 struct { 1934 int done; 1935 uid_t uid; 1936 gid_t gid; 1937 } lookup; 1938 u_int64_t tot_len; /* Make Mickey money */ 1939 union pf_headers { 1940 struct tcphdr tcp; 1941 struct udphdr udp; 1942 struct sctphdr sctp; 1943 struct icmp icmp; 1944 #ifdef INET6 1945 struct icmp6_hdr icmp6; 1946 #endif /* INET6 */ 1947 char any[0]; 1948 } hdr; 1949 1950 struct pf_addr nsaddr; /* src address after NAT */ 1951 struct pf_addr ndaddr; /* dst address after NAT */ 1952 1953 struct pfi_kkif *kif; /* incomming interface */ 1954 struct mbuf *m; 1955 1956 struct pf_addr *src; /* src address */ 1957 struct pf_addr *dst; /* dst address */ 1958 struct pf_addr osrc; 1959 struct pf_addr odst; 1960 u_int16_t *pcksum; /* proto cksum */ 1961 u_int16_t *sport; 1962 u_int16_t *dport; 1963 u_int16_t osport; 1964 u_int16_t odport; 1965 u_int16_t nsport; /* src port after NAT */ 1966 u_int16_t ndport; /* dst port after NAT */ 1967 struct pf_mtag *pf_mtag; 1968 struct pf_rule_actions act; 1969 1970 u_int32_t off; /* protocol header offset */ 1971 bool df; /* IPv4 Don't fragment flag. */ 1972 u_int32_t hdrlen; /* protocol header length */ 1973 u_int32_t p_len; /* total length of protocol payload */ 1974 u_int32_t extoff; /* extentsion header offset */ 1975 u_int32_t fragoff; /* fragment header offset */ 1976 u_int32_t jumbolen; /* length from v6 jumbo header */ 1977 u_int32_t badopts; /* v4 options or v6 routing headers */ 1978 #define PF_OPT_OTHER 0x0001 1979 #define PF_OPT_JUMBO 0x0002 1980 #define PF_OPT_ROUTER_ALERT 0x0004 1981 1982 u_int16_t *ip_sum; 1983 u_int16_t flags; /* Let SCRUB trigger behavior in 1984 * state code. Easier than tags */ 1985 #define PFDESC_TCP_NORM 0x0001 /* TCP shall be statefully scrubbed */ 1986 u_int16_t virtual_proto; 1987 #define PF_VPROTO_FRAGMENT 256 1988 sa_family_t af; 1989 sa_family_t naf; 1990 u_int8_t proto; 1991 u_int8_t tos; 1992 u_int8_t ttl; 1993 u_int8_t dir; /* direction */ 1994 u_int8_t sidx; /* key index for source */ 1995 u_int8_t didx; /* key index for destination */ 1996 #define PFDESC_SCTP_INIT 0x0001 1997 #define PFDESC_SCTP_INIT_ACK 0x0002 1998 #define PFDESC_SCTP_COOKIE 0x0004 1999 #define PFDESC_SCTP_COOKIE_ACK 0x0008 2000 #define PFDESC_SCTP_ABORT 0x0010 2001 #define PFDESC_SCTP_SHUTDOWN 0x0020 2002 #define PFDESC_SCTP_SHUTDOWN_COMPLETE 0x0040 2003 #define PFDESC_SCTP_DATA 0x0080 2004 #define PFDESC_SCTP_ASCONF 0x0100 2005 #define PFDESC_SCTP_HEARTBEAT 0x0200 2006 #define PFDESC_SCTP_HEARTBEAT_ACK 0x0400 2007 #define PFDESC_SCTP_OTHER 0x0800 2008 #define PFDESC_SCTP_ADD_IP 0x1000 2009 u_int16_t sctp_flags; 2010 u_int32_t sctp_initiate_tag; 2011 u_int16_t sctp_dummy_sum; 2012 struct pf_krule *related_rule; 2013 2014 struct pf_sctp_multihome_jobs sctp_multihome_jobs; 2015 }; 2016 2017 struct pf_sctp_multihome_job { 2018 TAILQ_ENTRY(pf_sctp_multihome_job) next; 2019 struct pf_pdesc pd; 2020 struct pf_addr src; 2021 struct pf_addr dst; 2022 int op; 2023 }; 2024 2025 #endif 2026 2027 /* flags for RDR options */ 2028 #define PF_DPORT_RANGE 0x01 /* Dest port uses range */ 2029 #define PF_RPORT_RANGE 0x02 /* RDR'ed port uses range */ 2030 2031 /* UDP state enumeration */ 2032 #define PFUDPS_NO_TRAFFIC 0 2033 #define PFUDPS_SINGLE 1 2034 #define PFUDPS_MULTIPLE 2 2035 2036 #define PFUDPS_NSTATES 3 /* number of state levels */ 2037 2038 #define PFUDPS_NAMES { \ 2039 "NO_TRAFFIC", \ 2040 "SINGLE", \ 2041 "MULTIPLE", \ 2042 NULL \ 2043 } 2044 2045 /* Other protocol state enumeration */ 2046 #define PFOTHERS_NO_TRAFFIC 0 2047 #define PFOTHERS_SINGLE 1 2048 #define PFOTHERS_MULTIPLE 2 2049 2050 #define PFOTHERS_NSTATES 3 /* number of state levels */ 2051 2052 #define PFOTHERS_NAMES { \ 2053 "NO_TRAFFIC", \ 2054 "SINGLE", \ 2055 "MULTIPLE", \ 2056 NULL \ 2057 } 2058 2059 #define ACTION_SET(a, x) \ 2060 do { \ 2061 if ((a) != NULL) \ 2062 *(a) = (x); \ 2063 } while (0) 2064 2065 #define REASON_SET(a, x) \ 2066 do { \ 2067 SDT_PROBE2(pf, , test, reason_set, x, __LINE__); \ 2068 if ((a) != NULL) \ 2069 *(a) = (x); \ 2070 if (x < PFRES_MAX) \ 2071 counter_u64_add(V_pf_status.counters[x], 1); \ 2072 } while (0) 2073 2074 enum pf_syncookies_mode { 2075 PF_SYNCOOKIES_NEVER = 0, 2076 PF_SYNCOOKIES_ALWAYS = 1, 2077 PF_SYNCOOKIES_ADAPTIVE = 2, 2078 PF_SYNCOOKIES_MODE_MAX = PF_SYNCOOKIES_ADAPTIVE 2079 }; 2080 2081 #define PF_SYNCOOKIES_HIWATPCT 25 2082 #define PF_SYNCOOKIES_LOWATPCT (PF_SYNCOOKIES_HIWATPCT / 2) 2083 2084 #define PF_STATELIM_ID_NONE 0 2085 #define PF_STATELIM_ID_MIN 1 2086 #define PF_STATELIM_ID_MAX 255 /* fits in pf_state uint8_t */ 2087 #define PF_STATELIM_LIMIT_MIN 1 2088 #define PF_STATELIM_LIMIT_MAX (1 << 24) /* pf is pretty scalable */ 2089 2090 #define PF_SOURCELIM_ID_NONE 0 2091 #define PF_SOURCELIM_ID_MIN 1 2092 #define PF_SOURCELIM_ID_MAX 255 /* fits in pf_state uint8_t */ 2093 2094 #ifdef _KERNEL 2095 2096 struct pfioc_statelim { 2097 uint32_t ticket; 2098 2099 char name[PF_STATELIM_NAME_LEN]; 2100 uint32_t id; 2101 2102 /* limit on the total number of states */ 2103 unsigned int limit; 2104 2105 /* rate limit on the creation of states */ 2106 struct pf_limiter_rate rate; 2107 2108 char description[PF_STATELIM_DESCR_LEN]; 2109 2110 /* kernel state for GET ioctls */ 2111 unsigned int inuse; /* gauge */ 2112 uint64_t admitted; /* counter */ 2113 uint64_t hardlimited; /* counter */ 2114 uint64_t ratelimited; /* counter */ 2115 }; 2116 2117 struct pfioc_sourcelim { 2118 uint32_t ticket; 2119 2120 char name[PF_SOURCELIM_NAME_LEN]; 2121 uint32_t id; 2122 2123 /* limit on the total number of address entries */ 2124 unsigned int entries; 2125 2126 /* limit on the number of states per address entry */ 2127 unsigned int limit; 2128 2129 /* rate limit on the creation of states by an address entry */ 2130 struct pf_limiter_rate rate; 2131 2132 /* 2133 * when the number of states on an entry exceeds hwm, add 2134 * the address to the specified table. when the number of 2135 * states goes below lwm, remove it from the table. 2136 */ 2137 char overload_tblname[PF_TABLE_NAME_SIZE]; 2138 unsigned int overload_hwm; 2139 unsigned int overload_lwm; 2140 2141 /* 2142 * mask addresses before they're used for entries. /64s 2143 * everywhere for inet6 makes it easy to use too much memory. 2144 */ 2145 unsigned int inet_prefix; 2146 unsigned int inet6_prefix; 2147 2148 char description[PF_SOURCELIM_DESCR_LEN]; 2149 2150 /* kernel state for GET ioctls */ 2151 unsigned int nentries; /* gauge */ 2152 unsigned int inuse; /* gauge */ 2153 2154 uint64_t addrallocs; /* counter */ 2155 uint64_t addrnomem; /* counter */ 2156 uint64_t admitted; /* counter */ 2157 uint64_t addrlimited; /* counter */ 2158 uint64_t hardlimited; /* counter */ 2159 uint64_t ratelimited; /* counter */ 2160 }; 2161 2162 struct pfioc_source_kill { 2163 char name[PF_SOURCELIM_NAME_LEN]; 2164 uint32_t id; 2165 unsigned int rdomain; 2166 sa_family_t af; 2167 struct pf_addr addr; 2168 2169 unsigned int rmstates; /* kill the states too? */ 2170 }; 2171 2172 int pf_statelim_add(const struct pfioc_statelim *); 2173 struct pf_statelim *pf_statelim_rb_find(struct pf_statelim_id_tree *, 2174 struct pf_statelim *); 2175 struct pf_statelim *pf_statelim_rb_nfind(struct pf_statelim_id_tree *, 2176 struct pf_statelim *); 2177 int pf_statelim_get(struct pfioc_statelim *, 2178 struct pf_statelim *(*rbt_op)(struct pf_statelim_id_tree *, 2179 struct pf_statelim *)); 2180 int pf_sourcelim_add(const struct pfioc_sourcelim *); 2181 struct pf_sourcelim *pf_sourcelim_rb_find(struct pf_sourcelim_id_tree *, 2182 struct pf_sourcelim *); 2183 struct pf_sourcelim *pf_sourcelim_rb_nfind(struct pf_sourcelim_id_tree *, 2184 struct pf_sourcelim *); 2185 int pf_sourcelim_get(struct pfioc_sourcelim *, 2186 struct pf_sourcelim *(*rbt_op)(struct pf_sourcelim_id_tree *, 2187 struct pf_sourcelim *)); 2188 struct pf_source *pf_source_rb_find(struct pf_source_ioc_tree *, 2189 struct pf_source *); 2190 struct pf_source *pf_source_rb_nfind(struct pf_source_ioc_tree *, 2191 struct pf_source *); 2192 int pf_source_clr(struct pfioc_source_kill *); 2193 2194 struct pf_kstatus { 2195 counter_u64_t counters[PFRES_MAX]; /* reason for passing/dropping */ 2196 counter_u64_t lcounters[KLCNT_MAX]; /* limit counters */ 2197 struct pf_counter_u64 fcounters[FCNT_MAX]; /* state operation counters */ 2198 counter_u64_t scounters[SCNT_MAX]; /* src_node operation counters */ 2199 counter_u64_t ncounters[NCNT_MAX]; 2200 uint32_t states; 2201 uint32_t src_nodes; 2202 uint32_t running; 2203 uint32_t since; 2204 uint32_t debug; 2205 uint32_t hostid; 2206 char ifname[IFNAMSIZ]; 2207 uint8_t pf_chksum[PF_MD5_DIGEST_LENGTH]; 2208 bool keep_counters; 2209 enum pf_syncookies_mode syncookies_mode; 2210 bool syncookies_active; 2211 uint64_t syncookies_inflight[2]; 2212 uint32_t states_halfopen; 2213 uint32_t reass; 2214 }; 2215 #endif 2216 2217 struct pf_divert { 2218 union { 2219 struct in_addr ipv4; 2220 struct in6_addr ipv6; 2221 } addr; 2222 u_int16_t port; 2223 }; 2224 2225 #define PFFRAG_FRENT_HIWAT 5000 /* Number of fragment entries */ 2226 #define PFR_KENTRY_HIWAT 200000 /* Number of table entries */ 2227 2228 struct pf_fragment_tag { 2229 uint16_t ft_hdrlen; /* header length of reassembled pkt */ 2230 uint16_t ft_extoff; /* last extension header offset or 0 */ 2231 uint16_t ft_maxlen; /* maximum fragment payload length */ 2232 uint32_t ft_id; /* fragment id */ 2233 }; 2234 2235 /* 2236 * Limit the length of the fragment queue traversal. Remember 2237 * search entry points based on the fragment offset. 2238 */ 2239 #define PF_FRAG_ENTRY_POINTS 16 2240 2241 /* 2242 * The number of entries in the fragment queue must be limited 2243 * to avoid DoS by linear searching. Instead of a global limit, 2244 * use a limit per entry point. For large packets these sum up. 2245 */ 2246 #define PF_FRAG_ENTRY_LIMIT 64 2247 2248 /* 2249 * ioctl parameter structures 2250 */ 2251 2252 struct pfioc_pooladdr { 2253 u_int32_t action; 2254 u_int32_t ticket; 2255 u_int32_t nr; 2256 u_int32_t r_num; 2257 u_int8_t r_action; 2258 u_int8_t r_last; 2259 u_int8_t af; 2260 char anchor[MAXPATHLEN]; 2261 struct pf_pooladdr addr; 2262 }; 2263 2264 struct pfioc_rule { 2265 u_int32_t action; 2266 u_int32_t ticket; 2267 u_int32_t pool_ticket; 2268 u_int32_t nr; 2269 char anchor[MAXPATHLEN]; 2270 char anchor_call[MAXPATHLEN]; 2271 struct pf_rule rule; 2272 }; 2273 2274 struct pfioc_natlook { 2275 struct pf_addr saddr; 2276 struct pf_addr daddr; 2277 struct pf_addr rsaddr; 2278 struct pf_addr rdaddr; 2279 u_int16_t sport; 2280 u_int16_t dport; 2281 u_int16_t rsport; 2282 u_int16_t rdport; 2283 sa_family_t af; 2284 u_int8_t proto; 2285 u_int8_t direction; 2286 }; 2287 2288 struct pfioc_state { 2289 struct pfsync_state_1301 state; 2290 }; 2291 2292 struct pfioc_src_node_kill { 2293 sa_family_t psnk_af; 2294 struct pf_rule_addr psnk_src; 2295 struct pf_rule_addr psnk_dst; 2296 u_int psnk_killed; 2297 }; 2298 2299 #ifdef _KERNEL 2300 struct pf_kstate_kill { 2301 struct pf_state_cmp psk_pfcmp; 2302 sa_family_t psk_af; 2303 int psk_proto; 2304 struct pf_rule_addr psk_src; 2305 struct pf_rule_addr psk_dst; 2306 struct pf_rule_addr psk_rt_addr; 2307 char psk_ifname[IFNAMSIZ]; 2308 char psk_label[PF_RULE_LABEL_SIZE]; 2309 u_int psk_killed; 2310 bool psk_kill_match; 2311 bool psk_nat; 2312 }; 2313 #endif 2314 2315 struct pfioc_state_kill { 2316 struct pf_state_cmp psk_pfcmp; 2317 sa_family_t psk_af; 2318 int psk_proto; 2319 struct pf_rule_addr psk_src; 2320 struct pf_rule_addr psk_dst; 2321 char psk_ifname[IFNAMSIZ]; 2322 char psk_label[PF_RULE_LABEL_SIZE]; 2323 u_int psk_killed; 2324 }; 2325 2326 struct pfioc_states { 2327 int ps_len; 2328 union { 2329 void *ps_buf; 2330 struct pfsync_state_1301 *ps_states; 2331 }; 2332 }; 2333 2334 struct pfioc_states_v2 { 2335 int ps_len; 2336 uint64_t ps_req_version; 2337 union { 2338 void *ps_buf; 2339 struct pf_state_export *ps_states; 2340 }; 2341 }; 2342 2343 struct pfioc_src_nodes { 2344 int psn_len; 2345 union { 2346 void *psn_buf; 2347 struct pf_src_node *psn_src_nodes; 2348 }; 2349 }; 2350 2351 struct pfioc_if { 2352 char ifname[IFNAMSIZ]; 2353 }; 2354 2355 struct pfioc_tm { 2356 int timeout; 2357 int seconds; 2358 }; 2359 2360 struct pfioc_limit { 2361 int index; 2362 unsigned limit; 2363 }; 2364 2365 struct pfioc_altq_v0 { 2366 u_int32_t action; 2367 u_int32_t ticket; 2368 u_int32_t nr; 2369 struct pf_altq_v0 altq; 2370 }; 2371 2372 struct pfioc_altq_v1 { 2373 u_int32_t action; 2374 u_int32_t ticket; 2375 u_int32_t nr; 2376 /* 2377 * Placed here so code that only uses the above parameters can be 2378 * written entirely in terms of the v0 or v1 type. 2379 */ 2380 u_int32_t version; 2381 struct pf_altq_v1 altq; 2382 }; 2383 2384 /* 2385 * Latest version of struct pfioc_altq_vX. This must move in lock-step with 2386 * the latest version of struct pf_altq_vX as it has that struct as a 2387 * member. 2388 */ 2389 #define PFIOC_ALTQ_VERSION PF_ALTQ_VERSION 2390 2391 struct pfioc_qstats_v0 { 2392 u_int32_t ticket; 2393 u_int32_t nr; 2394 void *buf; 2395 int nbytes; 2396 u_int8_t scheduler; 2397 }; 2398 2399 struct pfioc_qstats_v1 { 2400 u_int32_t ticket; 2401 u_int32_t nr; 2402 void *buf; 2403 int nbytes; 2404 u_int8_t scheduler; 2405 /* 2406 * Placed here so code that only uses the above parameters can be 2407 * written entirely in terms of the v0 or v1 type. 2408 */ 2409 u_int32_t version; /* Requested version of stats struct */ 2410 }; 2411 2412 /* Latest version of struct pfioc_qstats_vX */ 2413 #define PFIOC_QSTATS_VERSION 1 2414 2415 struct pfioc_ruleset { 2416 u_int32_t nr; 2417 char path[MAXPATHLEN]; 2418 char name[PF_ANCHOR_NAME_SIZE]; 2419 }; 2420 2421 #define PF_RULESET_ALTQ (PF_RULESET_MAX) 2422 #define PF_RULESET_TABLE (PF_RULESET_MAX+1) 2423 #define PF_RULESET_ETH (PF_RULESET_MAX+2) 2424 struct pfioc_trans { 2425 int size; /* number of elements */ 2426 int esize; /* size of each element in bytes */ 2427 struct pfioc_trans_e { 2428 int rs_num; 2429 char anchor[MAXPATHLEN]; 2430 u_int32_t ticket; 2431 } *array; 2432 }; 2433 2434 #define PFR_FLAG_START 0x00000001 2435 #define PFR_FLAG_DUMMY 0x00000002 2436 #define PFR_FLAG_FEEDBACK 0x00000004 2437 #define PFR_FLAG_CLSTATS 0x00000008 2438 #define PFR_FLAG_ADDRSTOO 0x00000010 2439 #define PFR_FLAG_REPLACE 0x00000020 2440 #define PFR_FLAG_ALLRSETS 0x00000040 2441 #define PFR_FLAG_DONE 0x00000080 2442 #define PFR_FLAG_ALLMASK 0x000000FF 2443 #ifdef _KERNEL 2444 #define PFR_FLAG_USERIOCTL 0x10000000 2445 #endif 2446 2447 struct pfioc_table { 2448 struct pfr_table pfrio_table; 2449 void *pfrio_buffer; 2450 int pfrio_esize; 2451 int pfrio_size; 2452 int pfrio_size2; 2453 int pfrio_nadd; 2454 int pfrio_ndel; 2455 int pfrio_nchange; 2456 int pfrio_flags; 2457 u_int32_t pfrio_ticket; 2458 }; 2459 #define pfrio_exists pfrio_nadd 2460 #define pfrio_nzero pfrio_nadd 2461 #define pfrio_nmatch pfrio_nadd 2462 #define pfrio_naddr pfrio_size2 2463 #define pfrio_setflag pfrio_size2 2464 #define pfrio_clrflag pfrio_nadd 2465 2466 struct pfioc_iface { 2467 char pfiio_name[IFNAMSIZ]; 2468 void *pfiio_buffer; 2469 int pfiio_esize; 2470 int pfiio_size; 2471 int pfiio_nzero; 2472 int pfiio_flags; 2473 }; 2474 2475 /* 2476 * ioctl operations 2477 */ 2478 2479 #define DIOCSTART _IO ('D', 1) 2480 #define DIOCSTOP _IO ('D', 2) 2481 #define DIOCADDRULE _IOWR('D', 4, struct pfioc_rule) 2482 #define DIOCADDRULENV _IOWR('D', 4, struct pfioc_nv) 2483 #define DIOCGETRULES _IOWR('D', 6, struct pfioc_rule) 2484 #define DIOCGETRULENV _IOWR('D', 7, struct pfioc_nv) 2485 #define DIOCCLRSTATESNV _IOWR('D', 18, struct pfioc_nv) 2486 #define DIOCGETSTATE _IOWR('D', 19, struct pfioc_state) 2487 #define DIOCGETSTATENV _IOWR('D', 19, struct pfioc_nv) 2488 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if) 2489 #define DIOCGETSTATUSNV _IOWR('D', 21, struct pfioc_nv) 2490 #define DIOCCLRSTATUS _IO ('D', 22) 2491 #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook) 2492 #define DIOCSETDEBUG _IOWR('D', 24, u_int32_t) 2493 #ifdef COMPAT_FREEBSD14 2494 #define DIOCGETSTATES _IOWR('D', 25, struct pfioc_states) 2495 #endif 2496 #define DIOCCHANGERULE _IOWR('D', 26, struct pfioc_rule) 2497 #define DIOCSETTIMEOUT _IOWR('D', 29, struct pfioc_tm) 2498 #define DIOCGETTIMEOUT _IOWR('D', 30, struct pfioc_tm) 2499 #define DIOCADDSTATE _IOWR('D', 37, struct pfioc_state) 2500 #define DIOCCLRRULECTRS _IO ('D', 38) 2501 #define DIOCGETLIMIT _IOWR('D', 39, struct pfioc_limit) 2502 #define DIOCSETLIMIT _IOWR('D', 40, struct pfioc_limit) 2503 #define DIOCKILLSTATESNV _IOWR('D', 41, struct pfioc_nv) 2504 #define DIOCSTARTALTQ _IO ('D', 42) 2505 #define DIOCSTOPALTQ _IO ('D', 43) 2506 #define DIOCADDALTQV0 _IOWR('D', 45, struct pfioc_altq_v0) 2507 #define DIOCADDALTQV1 _IOWR('D', 45, struct pfioc_altq_v1) 2508 #define DIOCGETALTQSV0 _IOWR('D', 47, struct pfioc_altq_v0) 2509 #define DIOCGETALTQSV1 _IOWR('D', 47, struct pfioc_altq_v1) 2510 #define DIOCGETALTQV0 _IOWR('D', 48, struct pfioc_altq_v0) 2511 #define DIOCGETALTQV1 _IOWR('D', 48, struct pfioc_altq_v1) 2512 #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0) 2513 #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1) 2514 #define DIOCGETQSTATSV0 _IOWR('D', 50, struct pfioc_qstats_v0) 2515 #define DIOCGETQSTATSV1 _IOWR('D', 50, struct pfioc_qstats_v1) 2516 #define DIOCBEGINADDRS _IOWR('D', 51, struct pfioc_pooladdr) 2517 #define DIOCADDADDR _IOWR('D', 52, struct pfioc_pooladdr) 2518 #define DIOCGETADDRS _IOWR('D', 53, struct pfioc_pooladdr) 2519 #define DIOCGETADDR _IOWR('D', 54, struct pfioc_pooladdr) 2520 #define DIOCCHANGEADDR _IOWR('D', 55, struct pfioc_pooladdr) 2521 #define DIOCGETRULESETS _IOWR('D', 58, struct pfioc_ruleset) 2522 #define DIOCGETRULESET _IOWR('D', 59, struct pfioc_ruleset) 2523 #define DIOCRCLRTABLES _IOWR('D', 60, struct pfioc_table) 2524 #define DIOCRADDTABLES _IOWR('D', 61, struct pfioc_table) 2525 #define DIOCRDELTABLES _IOWR('D', 62, struct pfioc_table) 2526 #define DIOCRGETTABLES _IOWR('D', 63, struct pfioc_table) 2527 #define DIOCRGETTSTATS _IOWR('D', 64, struct pfioc_table) 2528 #define DIOCRCLRTSTATS _IOWR('D', 65, struct pfioc_table) 2529 #define DIOCRCLRADDRS _IOWR('D', 66, struct pfioc_table) 2530 #define DIOCRADDADDRS _IOWR('D', 67, struct pfioc_table) 2531 #define DIOCRDELADDRS _IOWR('D', 68, struct pfioc_table) 2532 #define DIOCRSETADDRS _IOWR('D', 69, struct pfioc_table) 2533 #define DIOCRGETADDRS _IOWR('D', 70, struct pfioc_table) 2534 #define DIOCRGETASTATS _IOWR('D', 71, struct pfioc_table) 2535 #define DIOCRCLRASTATS _IOWR('D', 72, struct pfioc_table) 2536 #define DIOCRTSTADDRS _IOWR('D', 73, struct pfioc_table) 2537 #define DIOCRSETTFLAGS _IOWR('D', 74, struct pfioc_table) 2538 #define DIOCRINADEFINE _IOWR('D', 77, struct pfioc_table) 2539 #define DIOCOSFPFLUSH _IO('D', 78) 2540 #define DIOCOSFPADD _IOWR('D', 79, struct pf_osfp_ioctl) 2541 #define DIOCOSFPGET _IOWR('D', 80, struct pf_osfp_ioctl) 2542 #define DIOCXBEGIN _IOWR('D', 81, struct pfioc_trans) 2543 #define DIOCXCOMMIT _IOWR('D', 82, struct pfioc_trans) 2544 #define DIOCXROLLBACK _IOWR('D', 83, struct pfioc_trans) 2545 #define DIOCGETSRCNODES _IOWR('D', 84, struct pfioc_src_nodes) 2546 #define DIOCCLRSRCNODES _IO('D', 85) 2547 #define DIOCSETHOSTID _IOWR('D', 86, u_int32_t) 2548 #define DIOCIGETIFACES _IOWR('D', 87, struct pfioc_iface) 2549 #define DIOCSETIFFLAG _IOWR('D', 89, struct pfioc_iface) 2550 #define DIOCCLRIFFLAG _IOWR('D', 90, struct pfioc_iface) 2551 #define DIOCKILLSRCNODES _IOWR('D', 91, struct pfioc_src_node_kill) 2552 #define DIOCGIFSPEEDV0 _IOWR('D', 92, struct pf_ifspeed_v0) 2553 #define DIOCGIFSPEEDV1 _IOWR('D', 92, struct pf_ifspeed_v1) 2554 #ifdef COMPAT_FREEBSD14 2555 #define DIOCGETSTATESV2 _IOWR('D', 93, struct pfioc_states_v2) 2556 #endif 2557 #define DIOCGETSYNCOOKIES _IOWR('D', 94, struct pfioc_nv) 2558 #define DIOCSETSYNCOOKIES _IOWR('D', 95, struct pfioc_nv) 2559 #define DIOCKEEPCOUNTERS _IOWR('D', 96, struct pfioc_nv) 2560 #define DIOCKEEPCOUNTERS_FREEBSD13 _IOWR('D', 92, struct pfioc_nv) 2561 #define DIOCADDETHRULE _IOWR('D', 97, struct pfioc_nv) 2562 #define DIOCGETETHRULE _IOWR('D', 98, struct pfioc_nv) 2563 #define DIOCGETETHRULES _IOWR('D', 99, struct pfioc_nv) 2564 #define DIOCGETETHRULESETS _IOWR('D', 100, struct pfioc_nv) 2565 #define DIOCGETETHRULESET _IOWR('D', 101, struct pfioc_nv) 2566 #define DIOCSETREASS _IOWR('D', 102, u_int32_t) 2567 2568 struct pf_ifspeed_v0 { 2569 char ifname[IFNAMSIZ]; 2570 u_int32_t baudrate; 2571 }; 2572 2573 struct pf_ifspeed_v1 { 2574 char ifname[IFNAMSIZ]; 2575 u_int32_t baudrate32; 2576 /* layout identical to struct pf_ifspeed_v0 up to this point */ 2577 u_int64_t baudrate; 2578 }; 2579 2580 /* Latest version of struct pf_ifspeed_vX */ 2581 #define PF_IFSPEED_VERSION 1 2582 2583 /* 2584 * Compatibility and convenience macros 2585 */ 2586 #ifndef _KERNEL 2587 #ifdef PFIOC_USE_LATEST 2588 /* 2589 * Maintaining in-tree consumers of the ioctl interface is easier when that 2590 * code can be written in terms old names that refer to the latest interface 2591 * version as that reduces the required changes in the consumers to those 2592 * that are functionally necessary to accommodate a new interface version. 2593 */ 2594 #define pfioc_altq __CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION) 2595 #define pfioc_qstats __CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION) 2596 #define pf_ifspeed __CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION) 2597 2598 #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION) 2599 #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION) 2600 #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION) 2601 #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION) 2602 #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION) 2603 #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION) 2604 #else 2605 /* 2606 * When building out-of-tree code that is written for the old interface, 2607 * such as may exist in ports for example, resolve the old struct tags and 2608 * ioctl command names to the v0 versions. 2609 */ 2610 #define pfioc_altq __CONCAT(pfioc_altq_v, 0) 2611 #define pfioc_qstats __CONCAT(pfioc_qstats_v, 0) 2612 #define pf_ifspeed __CONCAT(pf_ifspeed_v, 0) 2613 2614 #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, 0) 2615 #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, 0) 2616 #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, 0) 2617 #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, 0) 2618 #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, 0) 2619 #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, 0) 2620 #endif /* PFIOC_USE_LATEST */ 2621 #endif /* _KERNEL */ 2622 2623 #ifdef _KERNEL 2624 LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node); 2625 struct pf_srchash { 2626 struct pf_ksrc_node_list nodes; 2627 struct mtx lock; 2628 }; 2629 2630 struct pf_keyhash { 2631 LIST_HEAD(, pf_state_key) keys; 2632 struct mtx lock; 2633 }; 2634 2635 struct pf_idhash { 2636 LIST_HEAD(, pf_kstate) states; 2637 struct mtx lock; 2638 }; 2639 2640 struct pf_udpendpointhash { 2641 LIST_HEAD(, pf_udp_endpoint) endpoints; 2642 /* refcont is synchronized on the source endpoint's row lock */ 2643 struct mtx lock; 2644 }; 2645 2646 extern u_long pf_ioctl_maxcount; 2647 VNET_DECLARE(u_long, pf_hashmask); 2648 #define V_pf_hashmask VNET(pf_hashmask) 2649 VNET_DECLARE(u_long, pf_srchashmask); 2650 #define V_pf_srchashmask VNET(pf_srchashmask) 2651 VNET_DECLARE(u_long, pf_udpendpointhashmask); 2652 #define V_pf_udpendpointhashmask VNET(pf_udpendpointhashmask) 2653 #define PF_HASHSIZ (131072) 2654 #define PF_SRCHASHSIZ (PF_HASHSIZ/4) 2655 #define PF_UDPENDHASHSIZ (PF_HASHSIZ/4) 2656 VNET_DECLARE(struct pf_keyhash *, pf_keyhash); 2657 VNET_DECLARE(struct pf_idhash *, pf_idhash); 2658 VNET_DECLARE(struct pf_udpendpointhash *, pf_udpendpointhash); 2659 #define V_pf_keyhash VNET(pf_keyhash) 2660 #define V_pf_idhash VNET(pf_idhash) 2661 #define V_pf_udpendpointhash VNET(pf_udpendpointhash) 2662 VNET_DECLARE(struct pf_srchash *, pf_srchash); 2663 #define V_pf_srchash VNET(pf_srchash) 2664 2665 #define PF_IDHASHID(id) (be64toh(id) % (V_pf_hashmask + 1)) 2666 #define PF_IDHASH(s) PF_IDHASHID((s)->id) 2667 2668 VNET_DECLARE(void *, pf_swi_cookie); 2669 #define V_pf_swi_cookie VNET(pf_swi_cookie) 2670 VNET_DECLARE(struct intr_event *, pf_swi_ie); 2671 #define V_pf_swi_ie VNET(pf_swi_ie) 2672 2673 VNET_DECLARE(struct unrhdr64, pf_stateid); 2674 #define V_pf_stateid VNET(pf_stateid) 2675 2676 TAILQ_HEAD(pf_altqqueue, pf_altq); 2677 VNET_DECLARE(struct pf_altqqueue, pf_altqs[4]); 2678 #define V_pf_altqs VNET(pf_altqs) 2679 VNET_DECLARE(struct pf_kpalist, pf_pabuf[3]); 2680 #define V_pf_pabuf VNET(pf_pabuf) 2681 2682 VNET_DECLARE(u_int32_t, ticket_altqs_active); 2683 #define V_ticket_altqs_active VNET(ticket_altqs_active) 2684 VNET_DECLARE(u_int32_t, ticket_altqs_inactive); 2685 #define V_ticket_altqs_inactive VNET(ticket_altqs_inactive) 2686 VNET_DECLARE(int, altqs_inactive_open); 2687 #define V_altqs_inactive_open VNET(altqs_inactive_open) 2688 VNET_DECLARE(u_int32_t, ticket_pabuf); 2689 #define V_ticket_pabuf VNET(ticket_pabuf) 2690 VNET_DECLARE(struct pf_altqqueue *, pf_altqs_active); 2691 #define V_pf_altqs_active VNET(pf_altqs_active) 2692 VNET_DECLARE(struct pf_altqqueue *, pf_altq_ifs_active); 2693 #define V_pf_altq_ifs_active VNET(pf_altq_ifs_active) 2694 VNET_DECLARE(struct pf_altqqueue *, pf_altqs_inactive); 2695 #define V_pf_altqs_inactive VNET(pf_altqs_inactive) 2696 VNET_DECLARE(struct pf_altqqueue *, pf_altq_ifs_inactive); 2697 #define V_pf_altq_ifs_inactive VNET(pf_altq_ifs_inactive) 2698 2699 VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules); 2700 #define V_pf_unlinked_rules VNET(pf_unlinked_rules) 2701 2702 #ifdef PF_WANT_32_TO_64_COUNTER 2703 LIST_HEAD(allkiflist_head, pfi_kkif); 2704 VNET_DECLARE(struct allkiflist_head, pf_allkiflist); 2705 #define V_pf_allkiflist VNET(pf_allkiflist) 2706 VNET_DECLARE(size_t, pf_allkifcount); 2707 #define V_pf_allkifcount VNET(pf_allkifcount) 2708 VNET_DECLARE(struct pfi_kkif *, pf_kifmarker); 2709 #define V_pf_kifmarker VNET(pf_kifmarker) 2710 2711 LIST_HEAD(allrulelist_head, pf_krule); 2712 VNET_DECLARE(struct allrulelist_head, pf_allrulelist); 2713 #define V_pf_allrulelist VNET(pf_allrulelist) 2714 VNET_DECLARE(size_t, pf_allrulecount); 2715 #define V_pf_allrulecount VNET(pf_allrulecount) 2716 VNET_DECLARE(struct pf_krule *, pf_rulemarker); 2717 #define V_pf_rulemarker VNET(pf_rulemarker) 2718 #endif 2719 2720 int pf_start(void); 2721 int pf_stop(void); 2722 void pf_initialize(void); 2723 void pf_mtag_initialize(void); 2724 void pf_mtag_cleanup(void); 2725 void pf_cleanup(void); 2726 2727 struct pf_mtag *pf_get_mtag(struct mbuf *); 2728 2729 extern void pf_calc_skip_steps(struct pf_krulequeue *); 2730 #ifdef ALTQ 2731 extern void pf_altq_ifnet_event(struct ifnet *, int); 2732 #endif 2733 VNET_DECLARE(uma_zone_t, pf_state_z); 2734 #define V_pf_state_z VNET(pf_state_z) 2735 VNET_DECLARE(uma_zone_t, pf_state_key_z); 2736 #define V_pf_state_key_z VNET(pf_state_key_z) 2737 VNET_DECLARE(uma_zone_t, pf_udp_mapping_z); 2738 #define V_pf_udp_mapping_z VNET(pf_udp_mapping_z) 2739 VNET_DECLARE(uma_zone_t, pf_state_scrub_z); 2740 #define V_pf_state_scrub_z VNET(pf_state_scrub_z) 2741 VNET_DECLARE(uma_zone_t, pf_anchor_z); 2742 #define V_pf_anchor_z VNET(pf_anchor_z) 2743 VNET_DECLARE(uma_zone_t, pf_eth_anchor_z); 2744 #define V_pf_eth_anchor_z VNET(pf_eth_anchor_z) 2745 2746 extern void pf_purge_thread(void *); 2747 extern void pf_unload_vnet_purge(void); 2748 extern void pf_intr(void *); 2749 extern void pf_purge_expired_src_nodes(void); 2750 2751 extern int pf_remove_state(struct pf_kstate *); 2752 extern int pf_state_insert(struct pfi_kkif *, 2753 struct pfi_kkif *, 2754 struct pf_state_key *, 2755 struct pf_state_key *, 2756 struct pf_kstate *); 2757 extern struct pf_kstate *pf_alloc_state(int); 2758 extern void pf_free_state(struct pf_kstate *); 2759 extern void pf_killstates(struct pf_kstate_kill *, 2760 unsigned int *); 2761 extern unsigned int pf_clear_states(const struct pf_kstate_kill *); 2762 2763 static __inline void 2764 pf_ref_state(struct pf_kstate *s) 2765 { 2766 2767 refcount_acquire(&s->refs); 2768 } 2769 2770 static __inline int 2771 pf_release_state(struct pf_kstate *s) 2772 { 2773 2774 if (refcount_release(&s->refs)) { 2775 pf_free_state(s); 2776 return (1); 2777 } else 2778 return (0); 2779 } 2780 2781 static __inline int 2782 pf_release_staten(struct pf_kstate *s, u_int n) 2783 { 2784 2785 if (refcount_releasen(&s->refs, n)) { 2786 pf_free_state(s); 2787 return (1); 2788 } else 2789 return (0); 2790 } 2791 2792 static __inline uint64_t 2793 pf_get_uptime(void) 2794 { 2795 struct timeval t; 2796 microuptime(&t); 2797 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)); 2798 } 2799 2800 static __inline uint64_t 2801 pf_get_time(void) 2802 { 2803 struct timeval t; 2804 microtime(&t); 2805 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)); 2806 } 2807 2808 static inline uint64_t 2809 SEC_TO_NSEC(uint64_t seconds) 2810 { 2811 if (seconds > UINT64_MAX / 1000000000ULL) 2812 return (UINT64_MAX); 2813 return (seconds * 1000000000ULL); 2814 } 2815 2816 extern struct pf_kstate *pf_find_state_byid(uint64_t, uint32_t); 2817 extern struct pf_kstate *pf_find_state_all( 2818 const struct pf_state_key_cmp *, 2819 u_int, int *); 2820 extern bool pf_find_state_all_exists( 2821 const struct pf_state_key_cmp *, 2822 u_int); 2823 extern struct pf_udp_mapping *pf_udp_mapping_find(struct pf_udp_endpoint_cmp 2824 *endpoint); 2825 extern struct pf_udp_mapping *pf_udp_mapping_create(sa_family_t af, 2826 struct pf_addr *src_addr, uint16_t src_port, 2827 struct pf_addr *nat_addr, uint16_t nat_port); 2828 extern int pf_udp_mapping_insert(struct pf_udp_mapping 2829 *mapping); 2830 extern void pf_udp_mapping_release(struct pf_udp_mapping 2831 *mapping); 2832 uint32_t pf_hashsrc(struct pf_addr *, sa_family_t); 2833 extern bool pf_src_node_exists(struct pf_ksrc_node **, 2834 struct pf_srchash *); 2835 extern struct pf_ksrc_node *pf_find_src_node(struct pf_addr *, 2836 struct pf_krule *, sa_family_t, 2837 struct pf_srchash **, pf_sn_types_t, bool); 2838 extern void pf_unlink_src_node(struct pf_ksrc_node *); 2839 extern u_int pf_free_src_nodes(struct pf_ksrc_node_list *); 2840 extern void pf_free_src_node(struct pf_ksrc_node *); 2841 extern void pf_print_state(struct pf_kstate *); 2842 extern void pf_print_flags(uint16_t); 2843 extern int pf_addr_wrap_neq(struct pf_addr_wrap *, 2844 struct pf_addr_wrap *); 2845 extern u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t, 2846 u_int8_t); 2847 extern u_int16_t pf_proto_cksum_fixup(struct mbuf *, u_int16_t, 2848 u_int16_t, u_int16_t, u_int8_t); 2849 2850 VNET_DECLARE(struct pf_krule, pf_default_rule); 2851 #define V_pf_default_rule VNET(pf_default_rule) 2852 extern void pf_addrcpy(struct pf_addr *, const struct pf_addr *, 2853 sa_family_t); 2854 void pf_free_rule(struct pf_krule *); 2855 2856 int pf_test_eth(int, int, struct ifnet *, struct mbuf **, struct inpcb *); 2857 int pf_scan_sctp(struct pf_pdesc *); 2858 #if defined(INET) || defined(INET6) 2859 int pf_test(sa_family_t, int, int, struct ifnet *, struct mbuf **, struct inpcb *, 2860 struct pf_rule_actions *); 2861 #endif 2862 #ifdef INET 2863 int pf_normalize_ip(u_short *, struct pf_pdesc *); 2864 #endif /* INET */ 2865 2866 void pf_poolmask(struct pf_addr *, struct pf_addr*, 2867 struct pf_addr *, struct pf_addr *, sa_family_t); 2868 void pf_addr_inc(struct pf_addr *, sa_family_t); 2869 #ifdef INET6 2870 int pf_normalize_ip6(int, u_short *, struct pf_pdesc *); 2871 int pf_max_frag_size(struct mbuf *); 2872 int pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *, 2873 struct ifnet *, bool); 2874 #endif /* INET6 */ 2875 2876 int pf_multihome_scan_init(int, int, struct pf_pdesc *); 2877 int pf_multihome_scan_asconf(int, int, struct pf_pdesc *); 2878 2879 u_int32_t pf_new_isn(struct pf_kstate *); 2880 void *pf_pull_hdr(const struct mbuf *, int, void *, int, u_short *, 2881 sa_family_t); 2882 void pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t); 2883 void pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t, 2884 u_int8_t); 2885 void pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t); 2886 int pf_patch_16(struct pf_pdesc *, void *, u_int16_t, bool); 2887 int pf_patch_32(struct pf_pdesc *, void *, u_int32_t, bool); 2888 void pf_send_deferred_syn(struct pf_kstate *); 2889 int pf_match_addr(u_int8_t, const struct pf_addr *, 2890 const struct pf_addr *, const struct pf_addr *, sa_family_t); 2891 int pf_match_addr_range(const struct pf_addr *, const struct pf_addr *, 2892 const struct pf_addr *, sa_family_t); 2893 int pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t); 2894 2895 void pf_normalize_init(void); 2896 void pf_normalize_cleanup(void); 2897 uint64_t pf_normalize_get_frag_count(void); 2898 int pf_normalize_tcp(struct pf_pdesc *); 2899 void pf_normalize_tcp_cleanup(struct pf_kstate *); 2900 int pf_normalize_tcp_init(struct pf_pdesc *, 2901 struct tcphdr *, struct pf_state_peer *); 2902 int pf_normalize_tcp_stateful(struct pf_pdesc *, 2903 u_short *, struct tcphdr *, struct pf_kstate *, 2904 struct pf_state_peer *, struct pf_state_peer *, int *); 2905 int pf_normalize_sctp_init(struct pf_pdesc *, 2906 struct pf_state_peer *, struct pf_state_peer *); 2907 int pf_normalize_sctp(struct pf_pdesc *); 2908 u_int32_t 2909 pf_state_expires(const struct pf_kstate *); 2910 void pf_purge_expired_fragments(void); 2911 void pf_purge_fragments(uint32_t); 2912 int pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *, 2913 int); 2914 int pf_socket_lookup(struct pf_pdesc *); 2915 struct pf_state_key *pf_alloc_state_key(int); 2916 int pf_translate(struct pf_pdesc *, struct pf_addr *, u_int16_t, 2917 struct pf_addr *, u_int16_t, u_int16_t, int); 2918 bool pf_init_threshold(struct pf_kthreshold *, uint32_t, uint32_t); 2919 uint16_t pf_tagname2tag(const char *); 2920 #ifdef ALTQ 2921 uint16_t pf_qname2qid(const char *, bool); 2922 #endif /* ALTQ */ 2923 2924 void pfr_initialize(void); 2925 void pfr_cleanup(void); 2926 struct pfr_kentry * 2927 pfr_kentry_byaddr(struct pfr_ktable *, struct pf_addr *, sa_family_t, 2928 int); 2929 int pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t); 2930 void pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t, 2931 u_int64_t, int, int, int); 2932 int pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t, 2933 pf_addr_filter_func_t, bool); 2934 void pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *); 2935 struct pfr_ktable * 2936 pfr_attach_table(struct pf_kruleset *, char *); 2937 struct pfr_ktable * 2938 pfr_eth_attach_table(struct pf_keth_ruleset *, char *); 2939 void pfr_detach_table(struct pfr_ktable *); 2940 int pfr_clr_tables(struct pfr_table *, int *, int); 2941 int pfr_add_tables(struct pfr_table *, int, int *, int); 2942 int pfr_del_tables(struct pfr_table *, int, int *, int); 2943 int pfr_table_count(struct pfr_table *, int); 2944 int pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int); 2945 int pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int); 2946 int pfr_clr_tstats(struct pfr_table *, int, int *, int); 2947 int pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int); 2948 int pfr_clr_addrs(struct pfr_table *, int *, int); 2949 int pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, time_t); 2950 int pfr_remove_kentry(struct pfr_ktable *, struct pfr_addr *); 2951 int pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2952 int); 2953 int pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2954 int); 2955 int pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2956 int *, int *, int *, int, u_int32_t); 2957 int pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int); 2958 int pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int); 2959 int pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *, 2960 int); 2961 int pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2962 int); 2963 int pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int); 2964 int pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int); 2965 int pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int); 2966 int pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *, 2967 int *, u_int32_t, int); 2968 struct pfr_ktable 2969 *pfr_ktable_select_active(struct pfr_ktable *); 2970 2971 MALLOC_DECLARE(PFI_MTYPE); 2972 VNET_DECLARE(struct pfi_kkif *, pfi_all); 2973 #define V_pfi_all VNET(pfi_all) 2974 2975 void pfi_initialize(void); 2976 void pfi_initialize_vnet(void); 2977 void pfi_cleanup(void); 2978 void pfi_cleanup_vnet(void); 2979 void pfi_kkif_ref(struct pfi_kkif *); 2980 void pfi_kkif_unref(struct pfi_kkif *); 2981 struct pfi_kkif *pfi_kkif_find(const char *); 2982 struct pfi_kkif *pfi_kkif_attach(struct pfi_kkif *, const char *); 2983 int pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *); 2984 void pfi_kkif_purge(void); 2985 int pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *, 2986 sa_family_t); 2987 int pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t); 2988 void pfi_dynaddr_remove(struct pfi_dynaddr *); 2989 void pfi_dynaddr_copyout(struct pf_addr_wrap *); 2990 void pfi_update_status(const char *, struct pf_status *); 2991 void pfi_get_ifaces(const char *, struct pfi_kif *, int *); 2992 int pfi_set_flags(const char *, int); 2993 int pfi_clear_flags(const char *, int); 2994 2995 int pf_match_tag(struct mbuf *, struct pf_krule *, int *, int); 2996 int pf_tag_packet(struct pf_pdesc *, int); 2997 int pf_addr_cmp(const struct pf_addr *, const struct pf_addr *, 2998 sa_family_t); 2999 3000 uint8_t* pf_find_tcpopt(u_int8_t *, u_int8_t *, size_t, 3001 u_int8_t, u_int8_t); 3002 u_int16_t pf_get_mss(struct pf_pdesc *); 3003 u_int8_t pf_get_wscale(struct pf_pdesc *); 3004 struct mbuf *pf_build_tcp(const struct pf_krule *, sa_family_t, 3005 const struct pf_addr *, const struct pf_addr *, 3006 u_int16_t, u_int16_t, u_int32_t, u_int32_t, 3007 u_int8_t, u_int16_t, u_int16_t, u_int8_t, int, 3008 u_int16_t, u_int16_t, u_int, int, u_short *); 3009 void pf_send_tcp(const struct pf_krule *, sa_family_t, 3010 const struct pf_addr *, const struct pf_addr *, 3011 u_int16_t, u_int16_t, u_int32_t, u_int32_t, 3012 u_int8_t, u_int16_t, u_int16_t, u_int8_t, int, 3013 u_int16_t, u_int16_t, int, u_short *); 3014 3015 void pf_syncookies_init(void); 3016 void pf_syncookies_cleanup(void); 3017 int pf_get_syncookies(struct pfioc_nv *); 3018 int pf_set_syncookies(struct pfioc_nv *); 3019 int pf_synflood_check(struct pf_pdesc *); 3020 void pf_syncookie_send(struct pf_pdesc *, u_short *); 3021 bool pf_syncookie_check(struct pf_pdesc *); 3022 u_int8_t pf_syncookie_validate(struct pf_pdesc *); 3023 struct mbuf * pf_syncookie_recreate_syn(struct pf_pdesc *, 3024 u_short *); 3025 3026 VNET_DECLARE(struct pf_kstatus, pf_status); 3027 #define V_pf_status VNET(pf_status) 3028 3029 struct pf_limit { 3030 uma_zone_t zone; 3031 u_int limit; 3032 }; 3033 VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); 3034 #define V_pf_limits VNET(pf_limits) 3035 3036 #endif /* _KERNEL */ 3037 3038 #ifdef _KERNEL 3039 struct pf_nl_pooladdr { 3040 u_int32_t action; 3041 u_int32_t ticket; 3042 u_int32_t nr; 3043 u_int32_t r_num; 3044 u_int8_t r_action; 3045 u_int8_t r_last; 3046 u_int8_t af; 3047 char anchor[MAXPATHLEN]; 3048 struct pf_pooladdr addr; 3049 /* Above this is identical to pfioc_pooladdr */ 3050 int which; 3051 }; 3052 3053 VNET_DECLARE(struct pf_kanchor_global, pf_anchors); 3054 #define V_pf_anchors VNET(pf_anchors) 3055 VNET_DECLARE(struct pf_kanchor, pf_main_anchor); 3056 #define V_pf_main_anchor VNET(pf_main_anchor) 3057 VNET_DECLARE(struct pf_keth_anchor_global, pf_keth_anchors); 3058 #define V_pf_keth_anchors VNET(pf_keth_anchors) 3059 #define pf_main_ruleset V_pf_main_anchor.ruleset 3060 3061 VNET_DECLARE(struct pf_keth_anchor, pf_main_keth_anchor); 3062 #define V_pf_main_keth_anchor VNET(pf_main_keth_anchor) 3063 VNET_DECLARE(struct pf_keth_ruleset*, pf_keth); 3064 #define V_pf_keth VNET(pf_keth) 3065 3066 void pf_init_kruleset(struct pf_kruleset *); 3067 void pf_init_keth(struct pf_keth_ruleset *); 3068 int pf_kanchor_setup(struct pf_krule *, 3069 const struct pf_kruleset *, const char *); 3070 int pf_kanchor_copyout(const struct pf_kruleset *, 3071 const struct pf_krule *, char *, size_t); 3072 int pf_kanchor_nvcopyout(const struct pf_kruleset *, 3073 const struct pf_krule *, nvlist_t *); 3074 void pf_remove_kanchor(struct pf_krule *); 3075 void pf_remove_if_empty_kruleset(struct pf_kruleset *); 3076 struct pf_kruleset *pf_find_kruleset(const char *); 3077 struct pf_kruleset *pf_get_leaf_kruleset(char *, char **); 3078 struct pf_kruleset *pf_find_or_create_kruleset(const char *); 3079 void pf_rs_initialize(void); 3080 void pf_rule_tree_free(struct pf_krule_global *); 3081 3082 3083 struct pf_krule *pf_krule_alloc(void); 3084 3085 void pf_remove_if_empty_keth_ruleset( 3086 struct pf_keth_ruleset *); 3087 struct pf_keth_ruleset *pf_find_keth_ruleset(const char *); 3088 struct pf_keth_anchor *pf_find_keth_anchor(const char *); 3089 int pf_keth_anchor_setup(struct pf_keth_rule *, 3090 const struct pf_keth_ruleset *, const char *); 3091 int pf_keth_anchor_nvcopyout( 3092 const struct pf_keth_ruleset *, 3093 const struct pf_keth_rule *, nvlist_t *); 3094 struct pf_keth_ruleset *pf_find_or_create_keth_ruleset(const char *); 3095 void pf_keth_anchor_remove(struct pf_keth_rule *); 3096 3097 int pf_ioctl_getrules(struct pfioc_rule *); 3098 int pf_ioctl_addrule(struct pf_krule *, uint32_t, 3099 uint32_t, const char *, const char *, uid_t uid, 3100 pid_t); 3101 void pf_ioctl_clear_status(void); 3102 int pf_ioctl_get_timeout(int, int *); 3103 int pf_ioctl_set_timeout(int, int, int *); 3104 int pf_ioctl_get_limit(int, unsigned int *); 3105 int pf_ioctl_set_limit(int, unsigned int, unsigned int *); 3106 int pf_ioctl_begin_addrs(uint32_t *); 3107 int pf_ioctl_add_addr(struct pf_nl_pooladdr *); 3108 int pf_ioctl_get_addrs(struct pf_nl_pooladdr *); 3109 int pf_ioctl_get_addr(struct pf_nl_pooladdr *); 3110 int pf_ioctl_get_rulesets(struct pfioc_ruleset *); 3111 int pf_ioctl_get_ruleset(struct pfioc_ruleset *); 3112 int pf_ioctl_natlook(struct pfioc_natlook *); 3113 3114 void pf_krule_free(struct pf_krule *); 3115 void pf_krule_clear_counters(struct pf_krule *); 3116 void pf_addr_copyout(struct pf_addr_wrap *); 3117 #endif 3118 3119 /* The fingerprint functions can be linked into userland programs (tcpdump) */ 3120 int pf_osfp_add(struct pf_osfp_ioctl *); 3121 #ifdef _KERNEL 3122 struct pf_osfp_enlist * 3123 pf_osfp_fingerprint(struct pf_pdesc *, const struct tcphdr *); 3124 #endif /* _KERNEL */ 3125 void pf_osfp_flush(void); 3126 int pf_osfp_get(struct pf_osfp_ioctl *); 3127 int pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t); 3128 3129 #ifdef _KERNEL 3130 void pf_print_host(struct pf_addr *, u_int16_t, sa_family_t); 3131 3132 enum pf_test_status pf_step_into_anchor(struct pf_test_ctx *, struct pf_krule *); 3133 void pf_step_into_keth_anchor(struct pf_keth_anchor_stackframe *, 3134 int *, struct pf_keth_ruleset **, 3135 struct pf_keth_rule **, struct pf_keth_rule **, 3136 int *); 3137 int pf_step_out_of_keth_anchor(struct pf_keth_anchor_stackframe *, 3138 int *, struct pf_keth_ruleset **, 3139 struct pf_keth_rule **, struct pf_keth_rule **, 3140 int *); 3141 3142 u_short pf_map_addr(sa_family_t, struct pf_krule *, 3143 struct pf_addr *, struct pf_addr *, 3144 struct pfi_kkif **nkif, sa_family_t *, 3145 struct pf_addr *, struct pf_kpool *); 3146 u_short pf_map_addr_sn(u_int8_t, struct pf_krule *, 3147 struct pf_addr *, struct pf_addr *, 3148 sa_family_t *, struct pfi_kkif **, 3149 struct pf_addr *, struct pf_kpool *, 3150 pf_sn_types_t); 3151 int pf_get_transaddr_af(struct pf_krule *, 3152 struct pf_pdesc *); 3153 u_short pf_get_translation(struct pf_test_ctx *); 3154 u_short pf_get_transaddr(struct pf_test_ctx *, 3155 struct pf_krule *, 3156 u_int8_t, struct pf_kpool *); 3157 int pf_translate_compat(struct pf_test_ctx *); 3158 3159 int pf_state_key_setup(struct pf_pdesc *, 3160 u_int16_t, u_int16_t, 3161 struct pf_state_key **sk, struct pf_state_key **nk); 3162 struct pf_state_key *pf_state_key_clone(const struct pf_state_key *); 3163 void pf_rule_to_actions(struct pf_krule *, 3164 struct pf_rule_actions *); 3165 int pf_normalize_mss(struct pf_pdesc *pd); 3166 #if defined(INET) || defined(INET6) 3167 void pf_scrub(struct pf_pdesc *); 3168 #endif 3169 3170 struct pfi_kkif *pf_kkif_create(int); 3171 void pf_kkif_free(struct pfi_kkif *); 3172 void pf_kkif_zero(struct pfi_kkif *); 3173 3174 3175 /* NAT64 functions. */ 3176 int inet_nat64(int, const void *, void *, const void *, u_int8_t); 3177 int inet_nat64_inet(const void *, void *, const void *, u_int8_t); 3178 int inet_nat64_inet6(const void *, void *, const void *, u_int8_t); 3179 3180 int inet_nat46(int, const void *, void *, const void *, u_int8_t); 3181 int inet_nat46_inet(const void *, void *, const void *, u_int8_t); 3182 int inet_nat46_inet6(const void *, void *, const void *, u_int8_t); 3183 3184 #endif /* _KERNEL */ 3185 3186 #endif /* _NET_PFVAR_H_ */ 3187