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_PFHASH); 330 MALLOC_DECLARE(M_PF_RULE_ITEM); 331 332 SDT_PROVIDER_DECLARE(pf); 333 334 struct pfi_dynaddr { 335 TAILQ_ENTRY(pfi_dynaddr) entry; 336 struct pf_addr pfid_addr4; 337 struct pf_addr pfid_mask4; 338 struct pf_addr pfid_addr6; 339 struct pf_addr pfid_mask6; 340 struct pfr_ktable *pfid_kt; 341 struct pfi_kkif *pfid_kif; 342 int pfid_net; /* mask or 128 */ 343 int pfid_acnt4; /* address count IPv4 */ 344 int pfid_acnt6; /* address count IPv6 */ 345 sa_family_t pfid_af; /* rule af */ 346 u_int8_t pfid_iflags; /* PFI_AFLAG_* */ 347 }; 348 349 /* 350 * Address manipulation macros 351 */ 352 #define HTONL(x) (x) = htonl((__uint32_t)(x)) 353 #define HTONS(x) (x) = htons((__uint16_t)(x)) 354 #define NTOHL(x) (x) = ntohl((__uint32_t)(x)) 355 #define NTOHS(x) (x) = ntohs((__uint16_t)(x)) 356 357 #define PF_NAME "pf" 358 359 #define PF_HASHROW_ASSERT(h) mtx_assert(&(h)->lock, MA_OWNED) 360 #define PF_HASHROW_LOCK(h) mtx_lock(&(h)->lock) 361 #define PF_HASHROW_UNLOCK(h) mtx_unlock(&(h)->lock) 362 363 #ifdef INVARIANTS 364 #define PF_STATE_LOCK(s) \ 365 do { \ 366 struct pf_kstate *_s = (s); \ 367 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 368 MPASS(_s->lock == &_ih->lock); \ 369 mtx_lock(_s->lock); \ 370 } while (0) 371 #define PF_STATE_UNLOCK(s) \ 372 do { \ 373 struct pf_kstate *_s = (s); \ 374 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 375 MPASS(_s->lock == &_ih->lock); \ 376 mtx_unlock(_s->lock); \ 377 } while (0) 378 #else 379 #define PF_STATE_LOCK(s) mtx_lock((s)->lock) 380 #define PF_STATE_UNLOCK(s) mtx_unlock((s)->lock) 381 #endif 382 383 #ifdef INVARIANTS 384 #define PF_STATE_LOCK_ASSERT(s) \ 385 do { \ 386 struct pf_kstate *_s = (s); \ 387 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 388 MPASS(_s->lock == &_ih->lock); \ 389 PF_HASHROW_ASSERT(_ih); \ 390 } while (0) 391 #else /* !INVARIANTS */ 392 #define PF_STATE_LOCK_ASSERT(s) do {} while (0) 393 #endif /* INVARIANTS */ 394 395 #ifdef INVARIANTS 396 #define PF_SRC_NODE_LOCK(sn) \ 397 do { \ 398 struct pf_ksrc_node *_sn = (sn); \ 399 struct pf_srchash *_sh = &V_pf_srchash[ \ 400 pf_hashsrc(&_sn->addr, _sn->af)]; \ 401 MPASS(_sn->lock == &_sh->lock); \ 402 mtx_lock(_sn->lock); \ 403 } while (0) 404 #define PF_SRC_NODE_UNLOCK(sn) \ 405 do { \ 406 struct pf_ksrc_node *_sn = (sn); \ 407 struct pf_srchash *_sh = &V_pf_srchash[ \ 408 pf_hashsrc(&_sn->addr, _sn->af)]; \ 409 MPASS(_sn->lock == &_sh->lock); \ 410 mtx_unlock(_sn->lock); \ 411 } while (0) 412 #else 413 #define PF_SRC_NODE_LOCK(sn) mtx_lock((sn)->lock) 414 #define PF_SRC_NODE_UNLOCK(sn) mtx_unlock((sn)->lock) 415 #endif 416 417 #ifdef INVARIANTS 418 #define PF_SRC_NODE_LOCK_ASSERT(sn) \ 419 do { \ 420 struct pf_ksrc_node *_sn = (sn); \ 421 struct pf_srchash *_sh = &V_pf_srchash[ \ 422 pf_hashsrc(&_sn->addr, _sn->af)]; \ 423 MPASS(_sn->lock == &_sh->lock); \ 424 PF_HASHROW_ASSERT(_sh); \ 425 } while (0) 426 #else /* !INVARIANTS */ 427 #define PF_SRC_NODE_LOCK_ASSERT(sn) do {} while (0) 428 #endif /* INVARIANTS */ 429 430 extern struct mtx_padalign pf_unlnkdrules_mtx; 431 #define PF_UNLNKDRULES_LOCK() mtx_lock(&pf_unlnkdrules_mtx) 432 #define PF_UNLNKDRULES_UNLOCK() mtx_unlock(&pf_unlnkdrules_mtx) 433 #define PF_UNLNKDRULES_ASSERT() mtx_assert(&pf_unlnkdrules_mtx, MA_OWNED) 434 435 extern struct sx pf_config_lock; 436 #define PF_CONFIG_LOCK() sx_xlock(&pf_config_lock) 437 #define PF_CONFIG_UNLOCK() sx_xunlock(&pf_config_lock) 438 #define PF_CONFIG_ASSERT() sx_assert(&pf_config_lock, SA_XLOCKED) 439 440 VNET_DECLARE(struct rmlock, pf_rules_lock); 441 #define V_pf_rules_lock VNET(pf_rules_lock) 442 443 #define PF_RULES_RLOCK_TRACKER struct rm_priotracker _pf_rules_tracker 444 #define PF_RULES_RLOCK() rm_rlock(&V_pf_rules_lock, &_pf_rules_tracker) 445 #define PF_RULES_RUNLOCK() rm_runlock(&V_pf_rules_lock, &_pf_rules_tracker) 446 #define PF_RULES_WLOCK() rm_wlock(&V_pf_rules_lock) 447 #define PF_RULES_WUNLOCK() rm_wunlock(&V_pf_rules_lock) 448 #define PF_RULES_WOWNED() rm_wowned(&V_pf_rules_lock) 449 #define PF_RULES_ASSERT() rm_assert(&V_pf_rules_lock, RA_LOCKED) 450 #define PF_RULES_RASSERT() rm_assert(&V_pf_rules_lock, RA_RLOCKED) 451 #define PF_RULES_WASSERT() rm_assert(&V_pf_rules_lock, RA_WLOCKED) 452 453 extern struct mtx_padalign pf_table_stats_lock; 454 #define PF_TABLE_STATS_LOCK() mtx_lock(&pf_table_stats_lock) 455 #define PF_TABLE_STATS_UNLOCK() mtx_unlock(&pf_table_stats_lock) 456 #define PF_TABLE_STATS_OWNED() mtx_owned(&pf_table_stats_lock) 457 #define PF_TABLE_STATS_ASSERT() mtx_assert(&pf_table_stats_lock, MA_OWNED) 458 459 extern struct sx pf_end_lock; 460 461 #define PF_MODVER 1 462 #define PFLOG_MODVER 1 463 #define PFSYNC_MODVER 1 464 465 #define PFLOG_MINVER 1 466 #define PFLOG_PREFVER PFLOG_MODVER 467 #define PFLOG_MAXVER 1 468 #define PFSYNC_MINVER 1 469 #define PFSYNC_PREFVER PFSYNC_MODVER 470 #define PFSYNC_MAXVER 1 471 472 #ifdef INET 473 #ifndef INET6 474 #define PF_INET_ONLY 475 #endif /* ! INET6 */ 476 #endif /* INET */ 477 478 #ifdef INET6 479 #ifndef INET 480 #define PF_INET6_ONLY 481 #endif /* ! INET */ 482 #endif /* INET6 */ 483 484 #ifdef INET 485 #ifdef INET6 486 #define PF_INET_INET6 487 #endif /* INET6 */ 488 #endif /* INET */ 489 490 #else 491 492 #define PF_INET_INET6 493 494 #endif /* _KERNEL */ 495 496 /* Both IPv4 and IPv6 */ 497 #ifdef PF_INET_INET6 498 499 #define PF_AEQ(a, b, c) \ 500 ((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \ 501 (c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \ 502 (a)->addr32[2] == (b)->addr32[2] && \ 503 (a)->addr32[1] == (b)->addr32[1] && \ 504 (a)->addr32[0] == (b)->addr32[0])) \ 505 506 #define PF_ANEQ(a, b, c) \ 507 ((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \ 508 (c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \ 509 (a)->addr32[1] != (b)->addr32[1] || \ 510 (a)->addr32[2] != (b)->addr32[2] || \ 511 (a)->addr32[3] != (b)->addr32[3]))) \ 512 513 #define PF_AZERO(a, c) \ 514 ((c == AF_INET && !(a)->addr32[0]) || \ 515 (c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \ 516 !(a)->addr32[2] && !(a)->addr32[3] )) \ 517 518 #define PF_MATCHA(n, a, m, b, f) \ 519 pf_match_addr(n, a, m, b, f) 520 521 #define PF_ACPY(a, b, f) \ 522 pf_addrcpy(a, b, f) 523 524 #define PF_AINC(a, f) \ 525 pf_addr_inc(a, f) 526 527 #define PF_POOLMASK(a, b, c, d, f) \ 528 pf_poolmask(a, b, c, d, f) 529 530 #else 531 532 /* Just IPv6 */ 533 534 #ifdef PF_INET6_ONLY 535 536 #define PF_AEQ(a, b, c) \ 537 ((a)->addr32[3] == (b)->addr32[3] && \ 538 (a)->addr32[2] == (b)->addr32[2] && \ 539 (a)->addr32[1] == (b)->addr32[1] && \ 540 (a)->addr32[0] == (b)->addr32[0]) \ 541 542 #define PF_ANEQ(a, b, c) \ 543 ((a)->addr32[3] != (b)->addr32[3] || \ 544 (a)->addr32[2] != (b)->addr32[2] || \ 545 (a)->addr32[1] != (b)->addr32[1] || \ 546 (a)->addr32[0] != (b)->addr32[0]) \ 547 548 #define PF_AZERO(a, c) \ 549 (!(a)->addr32[0] && \ 550 !(a)->addr32[1] && \ 551 !(a)->addr32[2] && \ 552 !(a)->addr32[3] ) \ 553 554 #define PF_MATCHA(n, a, m, b, f) \ 555 pf_match_addr(n, a, m, b, f) 556 557 #define PF_ACPY(a, b, f) \ 558 pf_addrcpy(a, b, f) 559 560 #define PF_AINC(a, f) \ 561 pf_addr_inc(a, f) 562 563 #define PF_POOLMASK(a, b, c, d, f) \ 564 pf_poolmask(a, b, c, d, f) 565 566 #else 567 568 /* Just IPv4 */ 569 #ifdef PF_INET_ONLY 570 571 #define PF_AEQ(a, b, c) \ 572 ((a)->addr32[0] == (b)->addr32[0]) 573 574 #define PF_ANEQ(a, b, c) \ 575 ((a)->addr32[0] != (b)->addr32[0]) 576 577 #define PF_AZERO(a, c) \ 578 (!(a)->addr32[0]) 579 580 #define PF_MATCHA(n, a, m, b, f) \ 581 pf_match_addr(n, a, m, b, f) 582 583 #define PF_ACPY(a, b, f) \ 584 (a)->v4.s_addr = (b)->v4.s_addr 585 586 #define PF_AINC(a, f) \ 587 do { \ 588 (a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \ 589 } while (0) 590 591 #define PF_POOLMASK(a, b, c, d, f) \ 592 do { \ 593 (a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \ 594 (((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \ 595 } while (0) 596 597 #endif /* PF_INET_ONLY */ 598 #endif /* PF_INET6_ONLY */ 599 #endif /* PF_INET_INET6 */ 600 601 /* 602 * XXX callers not FIB-aware in our version of pf yet. 603 * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio. 604 */ 605 #define PF_MISMATCHAW(aw, x, af, neg, ifp, rtid) \ 606 ( \ 607 (((aw)->type == PF_ADDR_NOROUTE && \ 608 pf_routable((x), (af), NULL, (rtid))) || \ 609 (((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL && \ 610 pf_routable((x), (af), (ifp), (rtid))) || \ 611 ((aw)->type == PF_ADDR_TABLE && \ 612 !pfr_match_addr((aw)->p.tbl, (x), (af))) || \ 613 ((aw)->type == PF_ADDR_DYNIFTL && \ 614 !pfi_match_addr((aw)->p.dyn, (x), (af))) || \ 615 ((aw)->type == PF_ADDR_RANGE && \ 616 !pf_match_addr_range(&(aw)->v.a.addr, \ 617 &(aw)->v.a.mask, (x), (af))) || \ 618 ((aw)->type == PF_ADDR_ADDRMASK && \ 619 !PF_AZERO(&(aw)->v.a.mask, (af)) && \ 620 !PF_MATCHA(0, &(aw)->v.a.addr, \ 621 &(aw)->v.a.mask, (x), (af))))) != \ 622 (neg) \ 623 ) 624 625 #define PF_ALGNMNT(off) (((off) % 2) == 0) 626 627 #ifdef _KERNEL 628 629 struct pf_kpooladdr { 630 struct pf_addr_wrap addr; 631 TAILQ_ENTRY(pf_kpooladdr) entries; 632 char ifname[IFNAMSIZ]; 633 struct pfi_kkif *kif; 634 }; 635 636 TAILQ_HEAD(pf_kpalist, pf_kpooladdr); 637 638 struct pf_kpool { 639 struct mtx mtx; 640 struct pf_kpalist list; 641 struct pf_kpooladdr *cur; 642 struct pf_poolhashkey key; 643 struct pf_addr counter; 644 struct pf_mape_portset mape; 645 int tblidx; 646 u_int16_t proxy_port[2]; 647 u_int8_t opts; 648 }; 649 650 struct pf_rule_actions { 651 struct pf_addr rt_addr; 652 struct pfi_kkif *rt_kif; 653 int32_t rtableid; 654 uint32_t flags; 655 uint16_t qid; 656 uint16_t pqid; 657 uint16_t max_mss; 658 uint16_t dnpipe; 659 uint16_t dnrpipe; /* Reverse direction pipe */ 660 uint8_t log; 661 uint8_t set_tos; 662 uint8_t min_ttl; 663 uint8_t set_prio[2]; 664 uint8_t rt; 665 }; 666 667 union pf_keth_rule_ptr { 668 struct pf_keth_rule *ptr; 669 uint32_t nr; 670 }; 671 672 struct pf_keth_rule_addr { 673 uint8_t addr[ETHER_ADDR_LEN]; 674 uint8_t mask[ETHER_ADDR_LEN]; 675 bool neg; 676 uint8_t isset; 677 }; 678 679 struct pf_keth_anchor; 680 681 TAILQ_HEAD(pf_keth_ruleq, pf_keth_rule); 682 683 struct pf_keth_ruleset { 684 struct pf_keth_ruleq rules[2]; 685 struct pf_keth_rules { 686 struct pf_keth_ruleq *rules; 687 int open; 688 uint32_t ticket; 689 } active, inactive; 690 struct epoch_context epoch_ctx; 691 struct vnet *vnet; 692 struct pf_keth_anchor *anchor; 693 }; 694 695 RB_HEAD(pf_keth_anchor_global, pf_keth_anchor); 696 RB_HEAD(pf_keth_anchor_node, pf_keth_anchor); 697 struct pf_keth_anchor { 698 RB_ENTRY(pf_keth_anchor) entry_node; 699 RB_ENTRY(pf_keth_anchor) entry_global; 700 struct pf_keth_anchor *parent; 701 struct pf_keth_anchor_node children; 702 char name[PF_ANCHOR_NAME_SIZE]; 703 char path[MAXPATHLEN]; 704 struct pf_keth_ruleset ruleset; 705 int refcnt; /* anchor rules */ 706 uint8_t anchor_relative; 707 uint8_t anchor_wildcard; 708 }; 709 RB_PROTOTYPE(pf_keth_anchor_node, pf_keth_anchor, entry_node, 710 pf_keth_anchor_compare); 711 RB_PROTOTYPE(pf_keth_anchor_global, pf_keth_anchor, entry_global, 712 pf_keth_anchor_compare); 713 714 struct pf_keth_rule { 715 #define PFE_SKIP_IFP 0 716 #define PFE_SKIP_DIR 1 717 #define PFE_SKIP_PROTO 2 718 #define PFE_SKIP_SRC_ADDR 3 719 #define PFE_SKIP_DST_ADDR 4 720 #define PFE_SKIP_SRC_IP_ADDR 5 721 #define PFE_SKIP_DST_IP_ADDR 6 722 #define PFE_SKIP_COUNT 7 723 union pf_keth_rule_ptr skip[PFE_SKIP_COUNT]; 724 725 TAILQ_ENTRY(pf_keth_rule) entries; 726 727 struct pf_keth_anchor *anchor; 728 u_int8_t anchor_relative; 729 u_int8_t anchor_wildcard; 730 731 uint32_t nr; 732 733 bool quick; 734 735 /* Filter */ 736 char ifname[IFNAMSIZ]; 737 struct pfi_kkif *kif; 738 bool ifnot; 739 uint8_t direction; 740 uint16_t proto; 741 struct pf_keth_rule_addr src, dst; 742 struct pf_rule_addr ipsrc, ipdst; 743 char match_tagname[PF_TAG_NAME_SIZE]; 744 uint16_t match_tag; 745 bool match_tag_not; 746 747 748 /* Stats */ 749 counter_u64_t evaluations; 750 counter_u64_t packets[2]; 751 counter_u64_t bytes[2]; 752 time_t *timestamp; 753 754 /* Action */ 755 char qname[PF_QNAME_SIZE]; 756 int qid; 757 char tagname[PF_TAG_NAME_SIZE]; 758 uint16_t tag; 759 char bridge_to_name[IFNAMSIZ]; 760 struct pfi_kkif *bridge_to; 761 uint8_t action; 762 uint16_t dnpipe; 763 uint32_t dnflags; 764 765 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; 766 uint32_t ridentifier; 767 }; 768 769 RB_HEAD(pf_krule_global, pf_krule); 770 RB_PROTOTYPE(pf_krule_global, pf_krule, entry_global, pf_krule_compare); 771 772 struct pf_krule { 773 struct pf_rule_addr src; 774 struct pf_rule_addr dst; 775 struct pf_krule *skip[PF_SKIP_COUNT]; 776 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; 777 uint32_t ridentifier; 778 char ifname[IFNAMSIZ]; 779 char rcv_ifname[IFNAMSIZ]; 780 char qname[PF_QNAME_SIZE]; 781 char pqname[PF_QNAME_SIZE]; 782 char tagname[PF_TAG_NAME_SIZE]; 783 char match_tagname[PF_TAG_NAME_SIZE]; 784 785 char overload_tblname[PF_TABLE_NAME_SIZE]; 786 787 TAILQ_ENTRY(pf_krule) entries; 788 struct pf_kpool nat; 789 struct pf_kpool rdr; 790 791 struct pf_counter_u64 evaluations; 792 struct pf_counter_u64 packets[2]; 793 struct pf_counter_u64 bytes[2]; 794 time_t *timestamp; 795 796 struct pfi_kkif *kif; 797 struct pfi_kkif *rcv_kif; 798 struct pf_kanchor *anchor; 799 struct pfr_ktable *overload_tbl; 800 801 pf_osfp_t os_fingerprint; 802 803 int32_t rtableid; 804 u_int32_t timeout[PFTM_MAX]; 805 u_int32_t max_states; 806 u_int32_t max_src_nodes; 807 u_int32_t max_src_states; 808 u_int32_t max_src_conn; 809 struct { 810 u_int32_t limit; 811 u_int32_t seconds; 812 } max_src_conn_rate; 813 u_int16_t qid; 814 u_int16_t pqid; 815 u_int16_t dnpipe; 816 u_int16_t dnrpipe; 817 u_int32_t free_flags; 818 u_int32_t nr; 819 u_int32_t prob; 820 uid_t cuid; 821 pid_t cpid; 822 823 counter_u64_t states_cur; 824 counter_u64_t states_tot; 825 counter_u64_t src_nodes; 826 827 u_int16_t return_icmp; 828 u_int16_t return_icmp6; 829 u_int16_t max_mss; 830 u_int16_t tag; 831 u_int16_t match_tag; 832 u_int16_t scrub_flags; 833 834 struct pf_rule_uid uid; 835 struct pf_rule_gid gid; 836 837 u_int32_t rule_flag; 838 uint32_t rule_ref; 839 u_int8_t action; 840 u_int8_t direction; 841 u_int8_t log; 842 u_int8_t logif; 843 u_int8_t quick; 844 u_int8_t ifnot; 845 u_int8_t match_tag_not; 846 u_int8_t natpass; 847 848 u_int8_t keep_state; 849 sa_family_t af; 850 u_int8_t proto; 851 u_int8_t type; 852 u_int8_t code; 853 u_int8_t flags; 854 u_int8_t flagset; 855 u_int8_t min_ttl; 856 u_int8_t allow_opts; 857 u_int8_t rt; 858 u_int8_t return_ttl; 859 u_int8_t tos; 860 u_int8_t set_tos; 861 u_int8_t anchor_relative; 862 u_int8_t anchor_wildcard; 863 864 u_int8_t flush; 865 u_int8_t prio; 866 u_int8_t set_prio[2]; 867 sa_family_t naf; 868 869 struct { 870 struct pf_addr addr; 871 u_int16_t port; 872 } divert; 873 u_int8_t md5sum[PF_MD5_DIGEST_LENGTH]; 874 RB_ENTRY(pf_krule) entry_global; 875 876 #ifdef PF_WANT_32_TO_64_COUNTER 877 LIST_ENTRY(pf_krule) allrulelist; 878 bool allrulelinked; 879 #endif 880 }; 881 882 struct pf_krule_item { 883 SLIST_ENTRY(pf_krule_item) entry; 884 struct pf_krule *r; 885 }; 886 887 SLIST_HEAD(pf_krule_slist, pf_krule_item); 888 889 struct pf_ksrc_node { 890 LIST_ENTRY(pf_ksrc_node) entry; 891 struct pf_addr addr; 892 struct pf_addr raddr; 893 struct pf_krule_slist match_rules; 894 struct pf_krule *rule; 895 struct pfi_kkif *rkif; 896 counter_u64_t bytes[2]; 897 counter_u64_t packets[2]; 898 u_int32_t states; 899 u_int32_t conn; 900 struct pf_threshold conn_rate; 901 u_int32_t creation; 902 u_int32_t expire; 903 sa_family_t af; 904 sa_family_t naf; 905 u_int8_t ruletype; 906 struct mtx *lock; 907 }; 908 #endif 909 910 struct pf_state_scrub { 911 struct timeval pfss_last; /* time received last packet */ 912 u_int32_t pfss_tsecr; /* last echoed timestamp */ 913 u_int32_t pfss_tsval; /* largest timestamp */ 914 u_int32_t pfss_tsval0; /* original timestamp */ 915 u_int16_t pfss_flags; 916 #define PFSS_TIMESTAMP 0x0001 /* modulate timestamp */ 917 #define PFSS_PAWS 0x0010 /* stricter PAWS checks */ 918 #define PFSS_PAWS_IDLED 0x0020 /* was idle too long. no PAWS */ 919 #define PFSS_DATA_TS 0x0040 /* timestamp on data packets */ 920 #define PFSS_DATA_NOTS 0x0080 /* no timestamp on data packets */ 921 u_int8_t pfss_ttl; /* stashed TTL */ 922 u_int8_t pad; 923 union { 924 u_int32_t pfss_ts_mod; /* timestamp modulation */ 925 u_int32_t pfss_v_tag; /* SCTP verification tag */ 926 }; 927 }; 928 929 struct pf_state_host { 930 struct pf_addr addr; 931 u_int16_t port; 932 u_int16_t pad; 933 }; 934 935 struct pf_state_peer { 936 struct pf_state_scrub *scrub; /* state is scrubbed */ 937 u_int32_t seqlo; /* Max sequence number sent */ 938 u_int32_t seqhi; /* Max the other end ACKd + win */ 939 u_int32_t seqdiff; /* Sequence number modulator */ 940 u_int16_t max_win; /* largest window (pre scaling) */ 941 u_int16_t mss; /* Maximum segment size option */ 942 u_int8_t state; /* active state level */ 943 u_int8_t wscale; /* window scaling factor */ 944 u_int8_t tcp_est; /* Did we reach TCPS_ESTABLISHED */ 945 u_int8_t pad[1]; 946 }; 947 948 /* Keep synced with struct pf_udp_endpoint. */ 949 struct pf_udp_endpoint_cmp { 950 struct pf_addr addr; 951 uint16_t port; 952 sa_family_t af; 953 uint8_t pad[1]; 954 }; 955 956 struct pf_udp_endpoint { 957 struct pf_addr addr; 958 uint16_t port; 959 sa_family_t af; 960 uint8_t pad[1]; 961 962 struct pf_udp_mapping *mapping; 963 LIST_ENTRY(pf_udp_endpoint) entry; 964 }; 965 966 struct pf_udp_mapping { 967 struct pf_udp_endpoint endpoints[2]; 968 u_int refs; 969 }; 970 971 /* Keep synced with struct pf_state_key. */ 972 struct pf_state_key_cmp { 973 struct pf_addr addr[2]; 974 u_int16_t port[2]; 975 sa_family_t af; 976 u_int8_t proto; 977 u_int8_t pad[2]; 978 }; 979 980 struct pf_state_key { 981 struct pf_addr addr[2]; 982 u_int16_t port[2]; 983 sa_family_t af; 984 u_int8_t proto; 985 u_int8_t pad[2]; 986 987 LIST_ENTRY(pf_state_key) entry; 988 TAILQ_HEAD(, pf_kstate) states[2]; 989 }; 990 991 #define PF_REVERSED_KEY(key, family) \ 992 ((key[PF_SK_WIRE]->af != key[PF_SK_STACK]->af) && \ 993 (key[PF_SK_WIRE]->af != (family))) 994 995 /* Keep synced with struct pf_kstate. */ 996 struct pf_state_cmp { 997 u_int64_t id; 998 u_int32_t creatorid; 999 u_int8_t direction; 1000 u_int8_t pad[3]; 1001 }; 1002 1003 struct pf_state_scrub_export { 1004 uint16_t pfss_flags; 1005 uint8_t pfss_ttl; /* stashed TTL */ 1006 #define PF_SCRUB_FLAG_VALID 0x01 1007 uint8_t scrub_flag; 1008 uint32_t pfss_ts_mod; /* timestamp modulation */ 1009 }; 1010 1011 struct pf_state_key_export { 1012 struct pf_addr addr[2]; 1013 uint16_t port[2]; 1014 }; 1015 1016 struct pf_state_peer_export { 1017 struct pf_state_scrub_export scrub; /* state is scrubbed */ 1018 uint32_t seqlo; /* Max sequence number sent */ 1019 uint32_t seqhi; /* Max the other end ACKd + win */ 1020 uint32_t seqdiff; /* Sequence number modulator */ 1021 uint16_t max_win; /* largest window (pre scaling) */ 1022 uint16_t mss; /* Maximum segment size option */ 1023 uint8_t state; /* active state level */ 1024 uint8_t wscale; /* window scaling factor */ 1025 uint8_t dummy[6]; 1026 }; 1027 _Static_assert(sizeof(struct pf_state_peer_export) == 32, "size incorrect"); 1028 1029 struct pf_state_export { 1030 uint64_t version; 1031 #define PF_STATE_VERSION 20230404 1032 uint64_t id; 1033 char ifname[IFNAMSIZ]; 1034 char orig_ifname[IFNAMSIZ]; 1035 struct pf_state_key_export key[2]; 1036 struct pf_state_peer_export src; 1037 struct pf_state_peer_export dst; 1038 struct pf_addr rt_addr; 1039 uint32_t rule; 1040 uint32_t anchor; 1041 uint32_t nat_rule; 1042 uint32_t creation; 1043 uint32_t expire; 1044 uint32_t spare0; 1045 uint64_t packets[2]; 1046 uint64_t bytes[2]; 1047 uint32_t creatorid; 1048 uint32_t spare1; 1049 sa_family_t af; 1050 uint8_t proto; 1051 uint8_t direction; 1052 uint8_t log; 1053 uint8_t state_flags_compat; 1054 uint8_t timeout; 1055 uint8_t sync_flags; 1056 uint8_t updates; 1057 uint16_t state_flags; 1058 uint16_t qid; 1059 uint16_t pqid; 1060 uint16_t dnpipe; 1061 uint16_t dnrpipe; 1062 int32_t rtableid; 1063 uint8_t min_ttl; 1064 uint8_t set_tos; 1065 uint16_t max_mss; 1066 uint8_t set_prio[2]; 1067 uint8_t rt; 1068 char rt_ifname[IFNAMSIZ]; 1069 1070 uint8_t spare[72]; 1071 }; 1072 _Static_assert(sizeof(struct pf_state_export) == 384, "size incorrect"); 1073 1074 #ifdef _KERNEL 1075 struct pf_kstate { 1076 /* 1077 * Area shared with pf_state_cmp 1078 */ 1079 u_int64_t id; 1080 u_int32_t creatorid; 1081 u_int8_t direction; 1082 u_int8_t pad[3]; 1083 /* 1084 * end of the area 1085 */ 1086 1087 u_int16_t state_flags; 1088 u_int8_t timeout; 1089 u_int8_t sync_state; /* PFSYNC_S_x */ 1090 u_int8_t sync_updates; /* XXX */ 1091 u_int refs; 1092 struct mtx *lock; 1093 TAILQ_ENTRY(pf_kstate) sync_list; 1094 TAILQ_ENTRY(pf_kstate) key_list[2]; 1095 LIST_ENTRY(pf_kstate) entry; 1096 struct pf_state_peer src; 1097 struct pf_state_peer dst; 1098 struct pf_krule_slist match_rules; 1099 struct pf_krule *rule; 1100 struct pf_krule *anchor; 1101 struct pf_krule *nat_rule; 1102 struct pf_state_key *key[2]; /* addresses stack and wire */ 1103 struct pf_udp_mapping *udp_mapping; 1104 struct pfi_kkif *kif; 1105 struct pfi_kkif *orig_kif; /* The real kif, even if we're a floating state (i.e. if == V_pfi_all). */ 1106 struct pf_ksrc_node *src_node; 1107 struct pf_ksrc_node *nat_src_node; 1108 u_int64_t packets[2]; 1109 u_int64_t bytes[2]; 1110 u_int64_t creation; 1111 u_int64_t expire; 1112 u_int32_t pfsync_time; 1113 struct pf_rule_actions act; 1114 u_int16_t tag; 1115 u_int16_t if_index_in; 1116 u_int16_t if_index_out; 1117 }; 1118 1119 /* 1120 * Size <= fits 11 objects per page on LP64. Try to not grow the struct beyond that. 1121 */ 1122 _Static_assert(sizeof(struct pf_kstate) <= 372, "pf_kstate size crosses 372 bytes"); 1123 #endif 1124 1125 /* 1126 * Unified state structures for pulling states out of the kernel 1127 * used by pfsync(4) and the pf(4) ioctl. 1128 */ 1129 struct pfsync_state_scrub { 1130 u_int16_t pfss_flags; 1131 u_int8_t pfss_ttl; /* stashed TTL */ 1132 #define PFSYNC_SCRUB_FLAG_VALID 0x01 1133 u_int8_t scrub_flag; 1134 u_int32_t pfss_ts_mod; /* timestamp modulation */ 1135 } __packed; 1136 1137 struct pfsync_state_peer { 1138 struct pfsync_state_scrub scrub; /* state is scrubbed */ 1139 u_int32_t seqlo; /* Max sequence number sent */ 1140 u_int32_t seqhi; /* Max the other end ACKd + win */ 1141 u_int32_t seqdiff; /* Sequence number modulator */ 1142 u_int16_t max_win; /* largest window (pre scaling) */ 1143 u_int16_t mss; /* Maximum segment size option */ 1144 u_int8_t state; /* active state level */ 1145 u_int8_t wscale; /* window scaling factor */ 1146 u_int8_t pad[6]; 1147 } __packed; 1148 1149 struct pfsync_state_key { 1150 struct pf_addr addr[2]; 1151 u_int16_t port[2]; 1152 }; 1153 1154 struct pfsync_state_1301 { 1155 u_int64_t id; 1156 char ifname[IFNAMSIZ]; 1157 struct pfsync_state_key key[2]; 1158 struct pfsync_state_peer src; 1159 struct pfsync_state_peer dst; 1160 struct pf_addr rt_addr; 1161 u_int32_t rule; 1162 u_int32_t anchor; 1163 u_int32_t nat_rule; 1164 u_int32_t creation; 1165 u_int32_t expire; 1166 u_int32_t packets[2][2]; 1167 u_int32_t bytes[2][2]; 1168 u_int32_t creatorid; 1169 sa_family_t af; 1170 u_int8_t proto; 1171 u_int8_t direction; 1172 u_int8_t __spare[2]; 1173 u_int8_t log; 1174 u_int8_t state_flags; 1175 u_int8_t timeout; 1176 u_int8_t sync_flags; 1177 u_int8_t updates; 1178 } __packed; 1179 1180 struct pfsync_state_1400 { 1181 /* The beginning of the struct is compatible with previous versions */ 1182 u_int64_t id; 1183 char ifname[IFNAMSIZ]; 1184 struct pfsync_state_key key[2]; 1185 struct pfsync_state_peer src; 1186 struct pfsync_state_peer dst; 1187 struct pf_addr rt_addr; 1188 u_int32_t rule; 1189 u_int32_t anchor; 1190 u_int32_t nat_rule; 1191 u_int32_t creation; 1192 u_int32_t expire; 1193 u_int32_t packets[2][2]; 1194 u_int32_t bytes[2][2]; 1195 u_int32_t creatorid; 1196 sa_family_t af; 1197 u_int8_t proto; 1198 u_int8_t direction; 1199 u_int16_t state_flags; 1200 u_int8_t log; 1201 u_int8_t __spare; 1202 u_int8_t timeout; 1203 u_int8_t sync_flags; 1204 u_int8_t updates; 1205 /* The rest is not */ 1206 u_int16_t qid; 1207 u_int16_t pqid; 1208 u_int16_t dnpipe; 1209 u_int16_t dnrpipe; 1210 int32_t rtableid; 1211 u_int8_t min_ttl; 1212 u_int8_t set_tos; 1213 u_int16_t max_mss; 1214 u_int8_t set_prio[2]; 1215 u_int8_t rt; 1216 char rt_ifname[IFNAMSIZ]; 1217 1218 } __packed; 1219 1220 union pfsync_state_union { 1221 struct pfsync_state_1301 pfs_1301; 1222 struct pfsync_state_1400 pfs_1400; 1223 } __packed; 1224 1225 #ifdef _KERNEL 1226 /* pfsync */ 1227 typedef int pfsync_state_import_t(union pfsync_state_union *, int, int); 1228 typedef void pfsync_insert_state_t(struct pf_kstate *); 1229 typedef void pfsync_update_state_t(struct pf_kstate *); 1230 typedef void pfsync_delete_state_t(struct pf_kstate *); 1231 typedef void pfsync_clear_states_t(u_int32_t, const char *); 1232 typedef int pfsync_defer_t(struct pf_kstate *, struct mbuf *); 1233 typedef void pfsync_detach_ifnet_t(struct ifnet *); 1234 typedef void pflow_export_state_t(const struct pf_kstate *); 1235 typedef bool pf_addr_filter_func_t(const sa_family_t, const struct pf_addr *); 1236 1237 VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr); 1238 #define V_pfsync_state_import_ptr VNET(pfsync_state_import_ptr) 1239 VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr); 1240 #define V_pfsync_insert_state_ptr VNET(pfsync_insert_state_ptr) 1241 VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr); 1242 #define V_pfsync_update_state_ptr VNET(pfsync_update_state_ptr) 1243 VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr); 1244 #define V_pfsync_delete_state_ptr VNET(pfsync_delete_state_ptr) 1245 VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr); 1246 #define V_pfsync_clear_states_ptr VNET(pfsync_clear_states_ptr) 1247 VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr); 1248 #define V_pfsync_defer_ptr VNET(pfsync_defer_ptr) 1249 VNET_DECLARE(pflow_export_state_t *, pflow_export_state_ptr); 1250 #define V_pflow_export_state_ptr VNET(pflow_export_state_ptr) 1251 extern pfsync_detach_ifnet_t *pfsync_detach_ifnet_ptr; 1252 1253 void pfsync_state_export(union pfsync_state_union *, 1254 struct pf_kstate *, int); 1255 void pf_state_export(struct pf_state_export *, 1256 struct pf_kstate *); 1257 1258 /* pflog */ 1259 struct pf_kruleset; 1260 struct pf_pdesc; 1261 typedef int pflog_packet_t(uint8_t, u_int8_t, 1262 struct pf_krule *, struct pf_krule *, struct pf_kruleset *, 1263 struct pf_pdesc *, int); 1264 extern pflog_packet_t *pflog_packet_ptr; 1265 1266 #endif /* _KERNEL */ 1267 1268 #define PFSYNC_FLAG_SRCNODE 0x04 1269 #define PFSYNC_FLAG_NATSRCNODE 0x08 1270 1271 /* for copies to/from network byte order */ 1272 /* ioctl interface also uses network byte order */ 1273 #define pf_state_peer_hton(s,d) do { \ 1274 (d)->seqlo = htonl((s)->seqlo); \ 1275 (d)->seqhi = htonl((s)->seqhi); \ 1276 (d)->seqdiff = htonl((s)->seqdiff); \ 1277 (d)->max_win = htons((s)->max_win); \ 1278 (d)->mss = htons((s)->mss); \ 1279 (d)->state = (s)->state; \ 1280 (d)->wscale = (s)->wscale; \ 1281 if ((s)->scrub) { \ 1282 (d)->scrub.pfss_flags = \ 1283 htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP); \ 1284 (d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl; \ 1285 (d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\ 1286 (d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID; \ 1287 } \ 1288 } while (0) 1289 1290 #define pf_state_peer_ntoh(s,d) do { \ 1291 (d)->seqlo = ntohl((s)->seqlo); \ 1292 (d)->seqhi = ntohl((s)->seqhi); \ 1293 (d)->seqdiff = ntohl((s)->seqdiff); \ 1294 (d)->max_win = ntohs((s)->max_win); \ 1295 (d)->mss = ntohs((s)->mss); \ 1296 (d)->state = (s)->state; \ 1297 (d)->wscale = (s)->wscale; \ 1298 if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && \ 1299 (d)->scrub != NULL) { \ 1300 (d)->scrub->pfss_flags = \ 1301 ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP; \ 1302 (d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl; \ 1303 (d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\ 1304 } \ 1305 } while (0) 1306 1307 #define pf_state_counter_hton(s,d) do { \ 1308 d[0] = htonl((s>>32)&0xffffffff); \ 1309 d[1] = htonl(s&0xffffffff); \ 1310 } while (0) 1311 1312 #define pf_state_counter_from_pfsync(s) \ 1313 (((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1])) 1314 1315 #define pf_state_counter_ntoh(s,d) do { \ 1316 d = ntohl(s[0]); \ 1317 d = d<<32; \ 1318 d += ntohl(s[1]); \ 1319 } while (0) 1320 1321 TAILQ_HEAD(pf_krulequeue, pf_krule); 1322 1323 struct pf_kanchor; 1324 1325 struct pf_kruleset { 1326 struct { 1327 struct pf_krulequeue queues[2]; 1328 struct { 1329 struct pf_krulequeue *ptr; 1330 struct pf_krule **ptr_array; 1331 u_int32_t rcount; 1332 u_int32_t ticket; 1333 int open; 1334 struct pf_krule_global *tree; 1335 } active, inactive; 1336 } rules[PF_RULESET_MAX]; 1337 struct pf_kanchor *anchor; 1338 u_int32_t tticket; 1339 int tables; 1340 int topen; 1341 }; 1342 1343 RB_HEAD(pf_kanchor_global, pf_kanchor); 1344 RB_HEAD(pf_kanchor_node, pf_kanchor); 1345 struct pf_kanchor { 1346 RB_ENTRY(pf_kanchor) entry_global; 1347 RB_ENTRY(pf_kanchor) entry_node; 1348 struct pf_kanchor *parent; 1349 struct pf_kanchor_node children; 1350 char name[PF_ANCHOR_NAME_SIZE]; 1351 char path[MAXPATHLEN]; 1352 struct pf_kruleset ruleset; 1353 int refcnt; /* anchor rules */ 1354 }; 1355 RB_PROTOTYPE(pf_kanchor_global, pf_kanchor, entry_global, pf_anchor_compare); 1356 RB_PROTOTYPE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare); 1357 1358 #define PF_RESERVED_ANCHOR "_pf" 1359 1360 #define PFR_TFLAG_PERSIST 0x00000001 1361 #define PFR_TFLAG_CONST 0x00000002 1362 #define PFR_TFLAG_ACTIVE 0x00000004 1363 #define PFR_TFLAG_INACTIVE 0x00000008 1364 #define PFR_TFLAG_REFERENCED 0x00000010 1365 #define PFR_TFLAG_REFDANCHOR 0x00000020 1366 #define PFR_TFLAG_COUNTERS 0x00000040 1367 /* Adjust masks below when adding flags. */ 1368 #define PFR_TFLAG_USRMASK (PFR_TFLAG_PERSIST | \ 1369 PFR_TFLAG_CONST | \ 1370 PFR_TFLAG_COUNTERS) 1371 #define PFR_TFLAG_SETMASK (PFR_TFLAG_ACTIVE | \ 1372 PFR_TFLAG_INACTIVE | \ 1373 PFR_TFLAG_REFERENCED | \ 1374 PFR_TFLAG_REFDANCHOR) 1375 #define PFR_TFLAG_ALLMASK (PFR_TFLAG_PERSIST | \ 1376 PFR_TFLAG_CONST | \ 1377 PFR_TFLAG_ACTIVE | \ 1378 PFR_TFLAG_INACTIVE | \ 1379 PFR_TFLAG_REFERENCED | \ 1380 PFR_TFLAG_REFDANCHOR | \ 1381 PFR_TFLAG_COUNTERS) 1382 1383 struct pf_kanchor_stackframe; 1384 struct pf_keth_anchor_stackframe; 1385 1386 struct pfr_table { 1387 char pfrt_anchor[MAXPATHLEN]; 1388 char pfrt_name[PF_TABLE_NAME_SIZE]; 1389 u_int32_t pfrt_flags; 1390 u_int8_t pfrt_fback; 1391 }; 1392 1393 enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED, 1394 PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE, 1395 PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX }; 1396 1397 struct pfr_addr { 1398 union { 1399 struct in_addr _pfra_ip4addr; 1400 struct in6_addr _pfra_ip6addr; 1401 } pfra_u; 1402 u_int8_t pfra_af; 1403 u_int8_t pfra_net; 1404 u_int8_t pfra_not; 1405 u_int8_t pfra_fback; 1406 }; 1407 #define pfra_ip4addr pfra_u._pfra_ip4addr 1408 #define pfra_ip6addr pfra_u._pfra_ip6addr 1409 1410 enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX }; 1411 enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX }; 1412 enum { PFR_TYPE_PACKETS, PFR_TYPE_BYTES, PFR_TYPE_MAX }; 1413 #define PFR_NUM_COUNTERS (PFR_DIR_MAX * PFR_OP_ADDR_MAX * PFR_TYPE_MAX) 1414 #define PFR_OP_XPASS PFR_OP_ADDR_MAX 1415 1416 struct pfr_astats { 1417 struct pfr_addr pfras_a; 1418 u_int64_t pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 1419 u_int64_t pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 1420 long pfras_tzero; 1421 }; 1422 1423 enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX }; 1424 1425 struct pfr_tstats { 1426 struct pfr_table pfrts_t; 1427 u_int64_t pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1428 u_int64_t pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1429 u_int64_t pfrts_match; 1430 u_int64_t pfrts_nomatch; 1431 long pfrts_tzero; 1432 int pfrts_cnt; 1433 int pfrts_refcnt[PFR_REFCNT_MAX]; 1434 }; 1435 1436 #ifdef _KERNEL 1437 1438 struct pfr_kstate_counter { 1439 counter_u64_t pkc_pcpu; 1440 u_int64_t pkc_zero; 1441 }; 1442 1443 static inline int 1444 pfr_kstate_counter_init(struct pfr_kstate_counter *pfrc, int flags) 1445 { 1446 1447 pfrc->pkc_zero = 0; 1448 pfrc->pkc_pcpu = counter_u64_alloc(flags); 1449 if (pfrc->pkc_pcpu == NULL) 1450 return (ENOMEM); 1451 return (0); 1452 } 1453 1454 static inline void 1455 pfr_kstate_counter_deinit(struct pfr_kstate_counter *pfrc) 1456 { 1457 1458 counter_u64_free(pfrc->pkc_pcpu); 1459 } 1460 1461 static inline u_int64_t 1462 pfr_kstate_counter_fetch(struct pfr_kstate_counter *pfrc) 1463 { 1464 u_int64_t c; 1465 1466 c = counter_u64_fetch(pfrc->pkc_pcpu); 1467 c -= pfrc->pkc_zero; 1468 return (c); 1469 } 1470 1471 static inline void 1472 pfr_kstate_counter_zero(struct pfr_kstate_counter *pfrc) 1473 { 1474 u_int64_t c; 1475 1476 c = counter_u64_fetch(pfrc->pkc_pcpu); 1477 pfrc->pkc_zero = c; 1478 } 1479 1480 static inline void 1481 pfr_kstate_counter_add(struct pfr_kstate_counter *pfrc, int64_t n) 1482 { 1483 1484 counter_u64_add(pfrc->pkc_pcpu, n); 1485 } 1486 1487 struct pfr_ktstats { 1488 struct pfr_table pfrts_t; 1489 struct pfr_kstate_counter pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1490 struct pfr_kstate_counter pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1491 struct pfr_kstate_counter pfrkts_match; 1492 struct pfr_kstate_counter pfrkts_nomatch; 1493 long pfrkts_tzero; 1494 int pfrkts_cnt; 1495 int pfrkts_refcnt[PFR_REFCNT_MAX]; 1496 }; 1497 1498 #endif /* _KERNEL */ 1499 1500 #define pfrts_name pfrts_t.pfrt_name 1501 #define pfrts_flags pfrts_t.pfrt_flags 1502 1503 #ifndef _SOCKADDR_UNION_DEFINED 1504 #define _SOCKADDR_UNION_DEFINED 1505 union sockaddr_union { 1506 struct sockaddr sa; 1507 struct sockaddr_in sin; 1508 struct sockaddr_in6 sin6; 1509 }; 1510 #endif /* _SOCKADDR_UNION_DEFINED */ 1511 1512 struct pfr_kcounters { 1513 counter_u64_t pfrkc_counters; 1514 long pfrkc_tzero; 1515 }; 1516 #define pfr_kentry_counter(kc, dir, op, t) \ 1517 ((kc)->pfrkc_counters + \ 1518 (dir) * PFR_OP_ADDR_MAX * PFR_TYPE_MAX + (op) * PFR_TYPE_MAX + (t)) 1519 1520 #ifdef _KERNEL 1521 SLIST_HEAD(pfr_kentryworkq, pfr_kentry); 1522 struct pfr_kentry { 1523 struct radix_node pfrke_node[2]; 1524 union sockaddr_union pfrke_sa; 1525 SLIST_ENTRY(pfr_kentry) pfrke_workq; 1526 struct pfr_kcounters pfrke_counters; 1527 u_int8_t pfrke_af; 1528 u_int8_t pfrke_net; 1529 u_int8_t pfrke_not; 1530 u_int8_t pfrke_mark; 1531 }; 1532 1533 SLIST_HEAD(pfr_ktableworkq, pfr_ktable); 1534 RB_HEAD(pfr_ktablehead, pfr_ktable); 1535 struct pfr_ktable { 1536 struct pfr_ktstats pfrkt_kts; 1537 RB_ENTRY(pfr_ktable) pfrkt_tree; 1538 SLIST_ENTRY(pfr_ktable) pfrkt_workq; 1539 struct radix_node_head *pfrkt_ip4; 1540 struct radix_node_head *pfrkt_ip6; 1541 struct pfr_ktable *pfrkt_shadow; 1542 struct pfr_ktable *pfrkt_root; 1543 struct pf_kruleset *pfrkt_rs; 1544 long pfrkt_larg; 1545 int pfrkt_nflags; 1546 }; 1547 #define pfrkt_t pfrkt_kts.pfrts_t 1548 #define pfrkt_name pfrkt_t.pfrt_name 1549 #define pfrkt_anchor pfrkt_t.pfrt_anchor 1550 #define pfrkt_ruleset pfrkt_t.pfrt_ruleset 1551 #define pfrkt_flags pfrkt_t.pfrt_flags 1552 #define pfrkt_cnt pfrkt_kts.pfrkts_cnt 1553 #define pfrkt_refcnt pfrkt_kts.pfrkts_refcnt 1554 #define pfrkt_packets pfrkt_kts.pfrkts_packets 1555 #define pfrkt_bytes pfrkt_kts.pfrkts_bytes 1556 #define pfrkt_match pfrkt_kts.pfrkts_match 1557 #define pfrkt_nomatch pfrkt_kts.pfrkts_nomatch 1558 #define pfrkt_tzero pfrkt_kts.pfrkts_tzero 1559 #endif 1560 1561 #ifdef _KERNEL 1562 struct pfi_kkif { 1563 char pfik_name[IFNAMSIZ]; 1564 union { 1565 RB_ENTRY(pfi_kkif) _pfik_tree; 1566 LIST_ENTRY(pfi_kkif) _pfik_list; 1567 } _pfik_glue; 1568 #define pfik_tree _pfik_glue._pfik_tree 1569 #define pfik_list _pfik_glue._pfik_list 1570 struct pf_counter_u64 pfik_packets[2][2][2]; 1571 struct pf_counter_u64 pfik_bytes[2][2][2]; 1572 u_int32_t pfik_tzero; 1573 u_int pfik_flags; 1574 struct ifnet *pfik_ifp; 1575 struct ifg_group *pfik_group; 1576 u_int pfik_rulerefs; 1577 TAILQ_HEAD(, pfi_dynaddr) pfik_dynaddrs; 1578 #ifdef PF_WANT_32_TO_64_COUNTER 1579 LIST_ENTRY(pfi_kkif) pfik_allkiflist; 1580 #endif 1581 }; 1582 #endif 1583 1584 #define PFI_IFLAG_REFS 0x0001 /* has state references */ 1585 #define PFI_IFLAG_SKIP 0x0100 /* skip filtering on interface */ 1586 1587 #ifdef _KERNEL 1588 struct pf_sctp_multihome_job; 1589 TAILQ_HEAD(pf_sctp_multihome_jobs, pf_sctp_multihome_job); 1590 1591 struct pf_pdesc { 1592 struct { 1593 int done; 1594 uid_t uid; 1595 gid_t gid; 1596 } lookup; 1597 u_int64_t tot_len; /* Make Mickey money */ 1598 union pf_headers { 1599 struct tcphdr tcp; 1600 struct udphdr udp; 1601 struct sctphdr sctp; 1602 struct icmp icmp; 1603 #ifdef INET6 1604 struct icmp6_hdr icmp6; 1605 #endif /* INET6 */ 1606 char any[0]; 1607 } hdr; 1608 1609 struct pf_addr nsaddr; /* src address after NAT */ 1610 struct pf_addr ndaddr; /* dst address after NAT */ 1611 1612 struct pfi_kkif *kif; /* incomming interface */ 1613 struct mbuf *m; 1614 1615 struct pf_addr *src; /* src address */ 1616 struct pf_addr *dst; /* dst address */ 1617 u_int16_t *sport; 1618 u_int16_t *dport; 1619 u_int16_t osport; 1620 u_int16_t odport; 1621 u_int16_t nsport; /* src port after NAT */ 1622 u_int16_t ndport; /* dst port after NAT */ 1623 struct pf_mtag *pf_mtag; 1624 struct pf_rule_actions act; 1625 1626 u_int32_t off; /* protocol header offset */ 1627 u_int32_t hdrlen; /* protocol header length */ 1628 u_int32_t p_len; /* total length of protocol payload */ 1629 u_int32_t badopts; /* v4 options or v6 routing headers */ 1630 1631 u_int16_t *ip_sum; 1632 u_int16_t flags; /* Let SCRUB trigger behavior in 1633 * state code. Easier than tags */ 1634 #define PFDESC_TCP_NORM 0x0001 /* TCP shall be statefully scrubbed */ 1635 u_int16_t virtual_proto; 1636 #define PF_VPROTO_FRAGMENT 256 1637 int extoff; 1638 sa_family_t af; 1639 sa_family_t naf; 1640 u_int8_t proto; 1641 u_int8_t tos; 1642 u_int8_t ttl; 1643 u_int8_t dir; /* direction */ 1644 u_int8_t sidx; /* key index for source */ 1645 u_int8_t didx; /* key index for destination */ 1646 #define PFDESC_SCTP_INIT 0x0001 1647 #define PFDESC_SCTP_INIT_ACK 0x0002 1648 #define PFDESC_SCTP_COOKIE 0x0004 1649 #define PFDESC_SCTP_COOKIE_ACK 0x0008 1650 #define PFDESC_SCTP_ABORT 0x0010 1651 #define PFDESC_SCTP_SHUTDOWN 0x0020 1652 #define PFDESC_SCTP_SHUTDOWN_COMPLETE 0x0040 1653 #define PFDESC_SCTP_DATA 0x0080 1654 #define PFDESC_SCTP_ASCONF 0x0100 1655 #define PFDESC_SCTP_HEARTBEAT 0x0200 1656 #define PFDESC_SCTP_HEARTBEAT_ACK 0x0400 1657 #define PFDESC_SCTP_OTHER 0x0800 1658 #define PFDESC_SCTP_ADD_IP 0x1000 1659 u_int16_t sctp_flags; 1660 u_int32_t sctp_initiate_tag; 1661 1662 struct pf_sctp_multihome_jobs sctp_multihome_jobs; 1663 }; 1664 1665 struct pf_sctp_multihome_job { 1666 TAILQ_ENTRY(pf_sctp_multihome_job) next; 1667 struct pf_pdesc pd; 1668 struct pf_addr src; 1669 struct pf_addr dst; 1670 int op; 1671 }; 1672 1673 #endif 1674 1675 /* flags for RDR options */ 1676 #define PF_DPORT_RANGE 0x01 /* Dest port uses range */ 1677 #define PF_RPORT_RANGE 0x02 /* RDR'ed port uses range */ 1678 1679 /* UDP state enumeration */ 1680 #define PFUDPS_NO_TRAFFIC 0 1681 #define PFUDPS_SINGLE 1 1682 #define PFUDPS_MULTIPLE 2 1683 1684 #define PFUDPS_NSTATES 3 /* number of state levels */ 1685 1686 #define PFUDPS_NAMES { \ 1687 "NO_TRAFFIC", \ 1688 "SINGLE", \ 1689 "MULTIPLE", \ 1690 NULL \ 1691 } 1692 1693 /* Other protocol state enumeration */ 1694 #define PFOTHERS_NO_TRAFFIC 0 1695 #define PFOTHERS_SINGLE 1 1696 #define PFOTHERS_MULTIPLE 2 1697 1698 #define PFOTHERS_NSTATES 3 /* number of state levels */ 1699 1700 #define PFOTHERS_NAMES { \ 1701 "NO_TRAFFIC", \ 1702 "SINGLE", \ 1703 "MULTIPLE", \ 1704 NULL \ 1705 } 1706 1707 #define ACTION_SET(a, x) \ 1708 do { \ 1709 if ((a) != NULL) \ 1710 *(a) = (x); \ 1711 } while (0) 1712 1713 #define REASON_SET(a, x) \ 1714 do { \ 1715 if ((a) != NULL) \ 1716 *(a) = (x); \ 1717 if (x < PFRES_MAX) \ 1718 counter_u64_add(V_pf_status.counters[x], 1); \ 1719 } while (0) 1720 1721 enum pf_syncookies_mode { 1722 PF_SYNCOOKIES_NEVER = 0, 1723 PF_SYNCOOKIES_ALWAYS = 1, 1724 PF_SYNCOOKIES_ADAPTIVE = 2, 1725 PF_SYNCOOKIES_MODE_MAX = PF_SYNCOOKIES_ADAPTIVE 1726 }; 1727 1728 #define PF_SYNCOOKIES_HIWATPCT 25 1729 #define PF_SYNCOOKIES_LOWATPCT (PF_SYNCOOKIES_HIWATPCT / 2) 1730 1731 #ifdef _KERNEL 1732 struct pf_kstatus { 1733 counter_u64_t counters[PFRES_MAX]; /* reason for passing/dropping */ 1734 counter_u64_t lcounters[KLCNT_MAX]; /* limit counters */ 1735 struct pf_counter_u64 fcounters[FCNT_MAX]; /* state operation counters */ 1736 counter_u64_t scounters[SCNT_MAX]; /* src_node operation counters */ 1737 uint32_t states; 1738 uint32_t src_nodes; 1739 uint32_t running; 1740 uint32_t since; 1741 uint32_t debug; 1742 uint32_t hostid; 1743 char ifname[IFNAMSIZ]; 1744 uint8_t pf_chksum[PF_MD5_DIGEST_LENGTH]; 1745 bool keep_counters; 1746 enum pf_syncookies_mode syncookies_mode; 1747 bool syncookies_active; 1748 uint64_t syncookies_inflight[2]; 1749 uint32_t states_halfopen; 1750 uint32_t reass; 1751 }; 1752 #endif 1753 1754 struct pf_divert { 1755 union { 1756 struct in_addr ipv4; 1757 struct in6_addr ipv6; 1758 } addr; 1759 u_int16_t port; 1760 }; 1761 1762 #define PFFRAG_FRENT_HIWAT 5000 /* Number of fragment entries */ 1763 #define PFR_KENTRY_HIWAT 200000 /* Number of table entries */ 1764 1765 struct pf_fragment_tag { 1766 uint16_t ft_hdrlen; /* header length of reassembled pkt */ 1767 uint16_t ft_extoff; /* last extension header offset or 0 */ 1768 uint16_t ft_maxlen; /* maximum fragment payload length */ 1769 uint32_t ft_id; /* fragment id */ 1770 }; 1771 1772 /* 1773 * Limit the length of the fragment queue traversal. Remember 1774 * search entry points based on the fragment offset. 1775 */ 1776 #define PF_FRAG_ENTRY_POINTS 16 1777 1778 /* 1779 * The number of entries in the fragment queue must be limited 1780 * to avoid DoS by linear searching. Instead of a global limit, 1781 * use a limit per entry point. For large packets these sum up. 1782 */ 1783 #define PF_FRAG_ENTRY_LIMIT 64 1784 1785 /* 1786 * ioctl parameter structures 1787 */ 1788 1789 struct pfioc_pooladdr { 1790 u_int32_t action; 1791 u_int32_t ticket; 1792 u_int32_t nr; 1793 u_int32_t r_num; 1794 u_int8_t r_action; 1795 u_int8_t r_last; 1796 u_int8_t af; 1797 char anchor[MAXPATHLEN]; 1798 struct pf_pooladdr addr; 1799 }; 1800 1801 struct pfioc_rule { 1802 u_int32_t action; 1803 u_int32_t ticket; 1804 u_int32_t pool_ticket; 1805 u_int32_t nr; 1806 char anchor[MAXPATHLEN]; 1807 char anchor_call[MAXPATHLEN]; 1808 struct pf_rule rule; 1809 }; 1810 1811 struct pfioc_natlook { 1812 struct pf_addr saddr; 1813 struct pf_addr daddr; 1814 struct pf_addr rsaddr; 1815 struct pf_addr rdaddr; 1816 u_int16_t sport; 1817 u_int16_t dport; 1818 u_int16_t rsport; 1819 u_int16_t rdport; 1820 sa_family_t af; 1821 u_int8_t proto; 1822 u_int8_t direction; 1823 }; 1824 1825 struct pfioc_state { 1826 struct pfsync_state_1301 state; 1827 }; 1828 1829 struct pfioc_src_node_kill { 1830 sa_family_t psnk_af; 1831 struct pf_rule_addr psnk_src; 1832 struct pf_rule_addr psnk_dst; 1833 u_int psnk_killed; 1834 }; 1835 1836 #ifdef _KERNEL 1837 struct pf_kstate_kill { 1838 struct pf_state_cmp psk_pfcmp; 1839 sa_family_t psk_af; 1840 int psk_proto; 1841 struct pf_rule_addr psk_src; 1842 struct pf_rule_addr psk_dst; 1843 struct pf_rule_addr psk_rt_addr; 1844 char psk_ifname[IFNAMSIZ]; 1845 char psk_label[PF_RULE_LABEL_SIZE]; 1846 u_int psk_killed; 1847 bool psk_kill_match; 1848 bool psk_nat; 1849 }; 1850 #endif 1851 1852 struct pfioc_state_kill { 1853 struct pf_state_cmp psk_pfcmp; 1854 sa_family_t psk_af; 1855 int psk_proto; 1856 struct pf_rule_addr psk_src; 1857 struct pf_rule_addr psk_dst; 1858 char psk_ifname[IFNAMSIZ]; 1859 char psk_label[PF_RULE_LABEL_SIZE]; 1860 u_int psk_killed; 1861 }; 1862 1863 struct pfioc_states { 1864 int ps_len; 1865 union { 1866 void *ps_buf; 1867 struct pfsync_state_1301 *ps_states; 1868 }; 1869 }; 1870 1871 struct pfioc_states_v2 { 1872 int ps_len; 1873 uint64_t ps_req_version; 1874 union { 1875 void *ps_buf; 1876 struct pf_state_export *ps_states; 1877 }; 1878 }; 1879 1880 struct pfioc_src_nodes { 1881 int psn_len; 1882 union { 1883 void *psn_buf; 1884 struct pf_src_node *psn_src_nodes; 1885 }; 1886 }; 1887 1888 struct pfioc_if { 1889 char ifname[IFNAMSIZ]; 1890 }; 1891 1892 struct pfioc_tm { 1893 int timeout; 1894 int seconds; 1895 }; 1896 1897 struct pfioc_limit { 1898 int index; 1899 unsigned limit; 1900 }; 1901 1902 struct pfioc_altq_v0 { 1903 u_int32_t action; 1904 u_int32_t ticket; 1905 u_int32_t nr; 1906 struct pf_altq_v0 altq; 1907 }; 1908 1909 struct pfioc_altq_v1 { 1910 u_int32_t action; 1911 u_int32_t ticket; 1912 u_int32_t nr; 1913 /* 1914 * Placed here so code that only uses the above parameters can be 1915 * written entirely in terms of the v0 or v1 type. 1916 */ 1917 u_int32_t version; 1918 struct pf_altq_v1 altq; 1919 }; 1920 1921 /* 1922 * Latest version of struct pfioc_altq_vX. This must move in lock-step with 1923 * the latest version of struct pf_altq_vX as it has that struct as a 1924 * member. 1925 */ 1926 #define PFIOC_ALTQ_VERSION PF_ALTQ_VERSION 1927 1928 struct pfioc_qstats_v0 { 1929 u_int32_t ticket; 1930 u_int32_t nr; 1931 void *buf; 1932 int nbytes; 1933 u_int8_t scheduler; 1934 }; 1935 1936 struct pfioc_qstats_v1 { 1937 u_int32_t ticket; 1938 u_int32_t nr; 1939 void *buf; 1940 int nbytes; 1941 u_int8_t scheduler; 1942 /* 1943 * Placed here so code that only uses the above parameters can be 1944 * written entirely in terms of the v0 or v1 type. 1945 */ 1946 u_int32_t version; /* Requested version of stats struct */ 1947 }; 1948 1949 /* Latest version of struct pfioc_qstats_vX */ 1950 #define PFIOC_QSTATS_VERSION 1 1951 1952 struct pfioc_ruleset { 1953 u_int32_t nr; 1954 char path[MAXPATHLEN]; 1955 char name[PF_ANCHOR_NAME_SIZE]; 1956 }; 1957 1958 #define PF_RULESET_ALTQ (PF_RULESET_MAX) 1959 #define PF_RULESET_TABLE (PF_RULESET_MAX+1) 1960 #define PF_RULESET_ETH (PF_RULESET_MAX+2) 1961 struct pfioc_trans { 1962 int size; /* number of elements */ 1963 int esize; /* size of each element in bytes */ 1964 struct pfioc_trans_e { 1965 int rs_num; 1966 char anchor[MAXPATHLEN]; 1967 u_int32_t ticket; 1968 } *array; 1969 }; 1970 1971 #define PFR_FLAG_ATOMIC 0x00000001 /* unused */ 1972 #define PFR_FLAG_DUMMY 0x00000002 1973 #define PFR_FLAG_FEEDBACK 0x00000004 1974 #define PFR_FLAG_CLSTATS 0x00000008 1975 #define PFR_FLAG_ADDRSTOO 0x00000010 1976 #define PFR_FLAG_REPLACE 0x00000020 1977 #define PFR_FLAG_ALLRSETS 0x00000040 1978 #define PFR_FLAG_ALLMASK 0x0000007F 1979 #ifdef _KERNEL 1980 #define PFR_FLAG_USERIOCTL 0x10000000 1981 #endif 1982 1983 struct pfioc_table { 1984 struct pfr_table pfrio_table; 1985 void *pfrio_buffer; 1986 int pfrio_esize; 1987 int pfrio_size; 1988 int pfrio_size2; 1989 int pfrio_nadd; 1990 int pfrio_ndel; 1991 int pfrio_nchange; 1992 int pfrio_flags; 1993 u_int32_t pfrio_ticket; 1994 }; 1995 #define pfrio_exists pfrio_nadd 1996 #define pfrio_nzero pfrio_nadd 1997 #define pfrio_nmatch pfrio_nadd 1998 #define pfrio_naddr pfrio_size2 1999 #define pfrio_setflag pfrio_size2 2000 #define pfrio_clrflag pfrio_nadd 2001 2002 struct pfioc_iface { 2003 char pfiio_name[IFNAMSIZ]; 2004 void *pfiio_buffer; 2005 int pfiio_esize; 2006 int pfiio_size; 2007 int pfiio_nzero; 2008 int pfiio_flags; 2009 }; 2010 2011 /* 2012 * ioctl operations 2013 */ 2014 2015 #define DIOCSTART _IO ('D', 1) 2016 #define DIOCSTOP _IO ('D', 2) 2017 #define DIOCADDRULE _IOWR('D', 4, struct pfioc_rule) 2018 #define DIOCADDRULENV _IOWR('D', 4, struct pfioc_nv) 2019 #define DIOCGETRULES _IOWR('D', 6, struct pfioc_rule) 2020 #define DIOCGETRULENV _IOWR('D', 7, struct pfioc_nv) 2021 /* XXX cut 8 - 17 */ 2022 #define DIOCCLRSTATESNV _IOWR('D', 18, struct pfioc_nv) 2023 #define DIOCGETSTATE _IOWR('D', 19, struct pfioc_state) 2024 #define DIOCGETSTATENV _IOWR('D', 19, struct pfioc_nv) 2025 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if) 2026 #define DIOCGETSTATUSNV _IOWR('D', 21, struct pfioc_nv) 2027 #define DIOCCLRSTATUS _IO ('D', 22) 2028 #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook) 2029 #define DIOCSETDEBUG _IOWR('D', 24, u_int32_t) 2030 #ifdef COMPAT_FREEBSD14 2031 #define DIOCGETSTATES _IOWR('D', 25, struct pfioc_states) 2032 #endif 2033 #define DIOCCHANGERULE _IOWR('D', 26, struct pfioc_rule) 2034 /* XXX cut 26 - 28 */ 2035 #define DIOCSETTIMEOUT _IOWR('D', 29, struct pfioc_tm) 2036 #define DIOCGETTIMEOUT _IOWR('D', 30, struct pfioc_tm) 2037 #define DIOCADDSTATE _IOWR('D', 37, struct pfioc_state) 2038 #define DIOCCLRRULECTRS _IO ('D', 38) 2039 #define DIOCGETLIMIT _IOWR('D', 39, struct pfioc_limit) 2040 #define DIOCSETLIMIT _IOWR('D', 40, struct pfioc_limit) 2041 #define DIOCKILLSTATESNV _IOWR('D', 41, struct pfioc_nv) 2042 #define DIOCSTARTALTQ _IO ('D', 42) 2043 #define DIOCSTOPALTQ _IO ('D', 43) 2044 #define DIOCADDALTQV0 _IOWR('D', 45, struct pfioc_altq_v0) 2045 #define DIOCADDALTQV1 _IOWR('D', 45, struct pfioc_altq_v1) 2046 #define DIOCGETALTQSV0 _IOWR('D', 47, struct pfioc_altq_v0) 2047 #define DIOCGETALTQSV1 _IOWR('D', 47, struct pfioc_altq_v1) 2048 #define DIOCGETALTQV0 _IOWR('D', 48, struct pfioc_altq_v0) 2049 #define DIOCGETALTQV1 _IOWR('D', 48, struct pfioc_altq_v1) 2050 #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0) 2051 #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1) 2052 #define DIOCGETQSTATSV0 _IOWR('D', 50, struct pfioc_qstats_v0) 2053 #define DIOCGETQSTATSV1 _IOWR('D', 50, struct pfioc_qstats_v1) 2054 #define DIOCBEGINADDRS _IOWR('D', 51, struct pfioc_pooladdr) 2055 #define DIOCADDADDR _IOWR('D', 52, struct pfioc_pooladdr) 2056 #define DIOCGETADDRS _IOWR('D', 53, struct pfioc_pooladdr) 2057 #define DIOCGETADDR _IOWR('D', 54, struct pfioc_pooladdr) 2058 #define DIOCCHANGEADDR _IOWR('D', 55, struct pfioc_pooladdr) 2059 /* XXX cut 55 - 57 */ 2060 #define DIOCGETRULESETS _IOWR('D', 58, struct pfioc_ruleset) 2061 #define DIOCGETRULESET _IOWR('D', 59, struct pfioc_ruleset) 2062 #define DIOCRCLRTABLES _IOWR('D', 60, struct pfioc_table) 2063 #define DIOCRADDTABLES _IOWR('D', 61, struct pfioc_table) 2064 #define DIOCRDELTABLES _IOWR('D', 62, struct pfioc_table) 2065 #define DIOCRGETTABLES _IOWR('D', 63, struct pfioc_table) 2066 #define DIOCRGETTSTATS _IOWR('D', 64, struct pfioc_table) 2067 #define DIOCRCLRTSTATS _IOWR('D', 65, struct pfioc_table) 2068 #define DIOCRCLRADDRS _IOWR('D', 66, struct pfioc_table) 2069 #define DIOCRADDADDRS _IOWR('D', 67, struct pfioc_table) 2070 #define DIOCRDELADDRS _IOWR('D', 68, struct pfioc_table) 2071 #define DIOCRSETADDRS _IOWR('D', 69, struct pfioc_table) 2072 #define DIOCRGETADDRS _IOWR('D', 70, struct pfioc_table) 2073 #define DIOCRGETASTATS _IOWR('D', 71, struct pfioc_table) 2074 #define DIOCRCLRASTATS _IOWR('D', 72, struct pfioc_table) 2075 #define DIOCRTSTADDRS _IOWR('D', 73, struct pfioc_table) 2076 #define DIOCRSETTFLAGS _IOWR('D', 74, struct pfioc_table) 2077 #define DIOCRINADEFINE _IOWR('D', 77, struct pfioc_table) 2078 #define DIOCOSFPFLUSH _IO('D', 78) 2079 #define DIOCOSFPADD _IOWR('D', 79, struct pf_osfp_ioctl) 2080 #define DIOCOSFPGET _IOWR('D', 80, struct pf_osfp_ioctl) 2081 #define DIOCXBEGIN _IOWR('D', 81, struct pfioc_trans) 2082 #define DIOCXCOMMIT _IOWR('D', 82, struct pfioc_trans) 2083 #define DIOCXROLLBACK _IOWR('D', 83, struct pfioc_trans) 2084 #define DIOCGETSRCNODES _IOWR('D', 84, struct pfioc_src_nodes) 2085 #define DIOCCLRSRCNODES _IO('D', 85) 2086 #define DIOCSETHOSTID _IOWR('D', 86, u_int32_t) 2087 #define DIOCIGETIFACES _IOWR('D', 87, struct pfioc_iface) 2088 #define DIOCSETIFFLAG _IOWR('D', 89, struct pfioc_iface) 2089 #define DIOCCLRIFFLAG _IOWR('D', 90, struct pfioc_iface) 2090 #define DIOCKILLSRCNODES _IOWR('D', 91, struct pfioc_src_node_kill) 2091 #define DIOCGIFSPEEDV0 _IOWR('D', 92, struct pf_ifspeed_v0) 2092 #define DIOCGIFSPEEDV1 _IOWR('D', 92, struct pf_ifspeed_v1) 2093 #ifdef COMPAT_FREEBSD14 2094 #define DIOCGETSTATESV2 _IOWR('D', 93, struct pfioc_states_v2) 2095 #endif 2096 #define DIOCGETSYNCOOKIES _IOWR('D', 94, struct pfioc_nv) 2097 #define DIOCSETSYNCOOKIES _IOWR('D', 95, struct pfioc_nv) 2098 #define DIOCKEEPCOUNTERS _IOWR('D', 96, struct pfioc_nv) 2099 #define DIOCKEEPCOUNTERS_FREEBSD13 _IOWR('D', 92, struct pfioc_nv) 2100 #define DIOCADDETHRULE _IOWR('D', 97, struct pfioc_nv) 2101 #define DIOCGETETHRULE _IOWR('D', 98, struct pfioc_nv) 2102 #define DIOCGETETHRULES _IOWR('D', 99, struct pfioc_nv) 2103 #define DIOCGETETHRULESETS _IOWR('D', 100, struct pfioc_nv) 2104 #define DIOCGETETHRULESET _IOWR('D', 101, struct pfioc_nv) 2105 #define DIOCSETREASS _IOWR('D', 102, u_int32_t) 2106 2107 struct pf_ifspeed_v0 { 2108 char ifname[IFNAMSIZ]; 2109 u_int32_t baudrate; 2110 }; 2111 2112 struct pf_ifspeed_v1 { 2113 char ifname[IFNAMSIZ]; 2114 u_int32_t baudrate32; 2115 /* layout identical to struct pf_ifspeed_v0 up to this point */ 2116 u_int64_t baudrate; 2117 }; 2118 2119 /* Latest version of struct pf_ifspeed_vX */ 2120 #define PF_IFSPEED_VERSION 1 2121 2122 /* 2123 * Compatibility and convenience macros 2124 */ 2125 #ifndef _KERNEL 2126 #ifdef PFIOC_USE_LATEST 2127 /* 2128 * Maintaining in-tree consumers of the ioctl interface is easier when that 2129 * code can be written in terms old names that refer to the latest interface 2130 * version as that reduces the required changes in the consumers to those 2131 * that are functionally necessary to accommodate a new interface version. 2132 */ 2133 #define pfioc_altq __CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION) 2134 #define pfioc_qstats __CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION) 2135 #define pf_ifspeed __CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION) 2136 2137 #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION) 2138 #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION) 2139 #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION) 2140 #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION) 2141 #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION) 2142 #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION) 2143 #else 2144 /* 2145 * When building out-of-tree code that is written for the old interface, 2146 * such as may exist in ports for example, resolve the old struct tags and 2147 * ioctl command names to the v0 versions. 2148 */ 2149 #define pfioc_altq __CONCAT(pfioc_altq_v, 0) 2150 #define pfioc_qstats __CONCAT(pfioc_qstats_v, 0) 2151 #define pf_ifspeed __CONCAT(pf_ifspeed_v, 0) 2152 2153 #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, 0) 2154 #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, 0) 2155 #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, 0) 2156 #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, 0) 2157 #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, 0) 2158 #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, 0) 2159 #endif /* PFIOC_USE_LATEST */ 2160 #endif /* _KERNEL */ 2161 2162 #ifdef _KERNEL 2163 LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node); 2164 struct pf_srchash { 2165 struct pf_ksrc_node_list nodes; 2166 struct mtx lock; 2167 }; 2168 2169 struct pf_keyhash { 2170 LIST_HEAD(, pf_state_key) keys; 2171 struct mtx lock; 2172 }; 2173 2174 struct pf_idhash { 2175 LIST_HEAD(, pf_kstate) states; 2176 struct mtx lock; 2177 }; 2178 2179 struct pf_udpendpointhash { 2180 LIST_HEAD(, pf_udp_endpoint) endpoints; 2181 /* refcont is synchronized on the source endpoint's row lock */ 2182 struct mtx lock; 2183 }; 2184 2185 extern u_long pf_ioctl_maxcount; 2186 VNET_DECLARE(u_long, pf_hashmask); 2187 #define V_pf_hashmask VNET(pf_hashmask) 2188 VNET_DECLARE(u_long, pf_srchashmask); 2189 #define V_pf_srchashmask VNET(pf_srchashmask) 2190 VNET_DECLARE(u_long, pf_udpendpointhashmask); 2191 #define V_pf_udpendpointhashmask VNET(pf_udpendpointhashmask) 2192 #define PF_HASHSIZ (131072) 2193 #define PF_SRCHASHSIZ (PF_HASHSIZ/4) 2194 #define PF_UDPENDHASHSIZ (PF_HASHSIZ/4) 2195 VNET_DECLARE(struct pf_keyhash *, pf_keyhash); 2196 VNET_DECLARE(struct pf_idhash *, pf_idhash); 2197 VNET_DECLARE(struct pf_udpendpointhash *, pf_udpendpointhash); 2198 #define V_pf_keyhash VNET(pf_keyhash) 2199 #define V_pf_idhash VNET(pf_idhash) 2200 #define V_pf_udpendpointhash VNET(pf_udpendpointhash) 2201 VNET_DECLARE(struct pf_srchash *, pf_srchash); 2202 #define V_pf_srchash VNET(pf_srchash) 2203 2204 #define PF_IDHASH(s) (be64toh((s)->id) % (V_pf_hashmask + 1)) 2205 2206 VNET_DECLARE(void *, pf_swi_cookie); 2207 #define V_pf_swi_cookie VNET(pf_swi_cookie) 2208 VNET_DECLARE(struct intr_event *, pf_swi_ie); 2209 #define V_pf_swi_ie VNET(pf_swi_ie) 2210 2211 VNET_DECLARE(struct unrhdr64, pf_stateid); 2212 #define V_pf_stateid VNET(pf_stateid) 2213 2214 TAILQ_HEAD(pf_altqqueue, pf_altq); 2215 VNET_DECLARE(struct pf_altqqueue, pf_altqs[4]); 2216 #define V_pf_altqs VNET(pf_altqs) 2217 VNET_DECLARE(struct pf_kpalist, pf_pabuf[2]); 2218 #define V_pf_pabuf VNET(pf_pabuf) 2219 2220 VNET_DECLARE(u_int32_t, ticket_altqs_active); 2221 #define V_ticket_altqs_active VNET(ticket_altqs_active) 2222 VNET_DECLARE(u_int32_t, ticket_altqs_inactive); 2223 #define V_ticket_altqs_inactive VNET(ticket_altqs_inactive) 2224 VNET_DECLARE(int, altqs_inactive_open); 2225 #define V_altqs_inactive_open VNET(altqs_inactive_open) 2226 VNET_DECLARE(u_int32_t, ticket_pabuf); 2227 #define V_ticket_pabuf VNET(ticket_pabuf) 2228 VNET_DECLARE(struct pf_altqqueue *, pf_altqs_active); 2229 #define V_pf_altqs_active VNET(pf_altqs_active) 2230 VNET_DECLARE(struct pf_altqqueue *, pf_altq_ifs_active); 2231 #define V_pf_altq_ifs_active VNET(pf_altq_ifs_active) 2232 VNET_DECLARE(struct pf_altqqueue *, pf_altqs_inactive); 2233 #define V_pf_altqs_inactive VNET(pf_altqs_inactive) 2234 VNET_DECLARE(struct pf_altqqueue *, pf_altq_ifs_inactive); 2235 #define V_pf_altq_ifs_inactive VNET(pf_altq_ifs_inactive) 2236 2237 VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules); 2238 #define V_pf_unlinked_rules VNET(pf_unlinked_rules) 2239 2240 #ifdef PF_WANT_32_TO_64_COUNTER 2241 LIST_HEAD(allkiflist_head, pfi_kkif); 2242 VNET_DECLARE(struct allkiflist_head, pf_allkiflist); 2243 #define V_pf_allkiflist VNET(pf_allkiflist) 2244 VNET_DECLARE(size_t, pf_allkifcount); 2245 #define V_pf_allkifcount VNET(pf_allkifcount) 2246 VNET_DECLARE(struct pfi_kkif *, pf_kifmarker); 2247 #define V_pf_kifmarker VNET(pf_kifmarker) 2248 2249 LIST_HEAD(allrulelist_head, pf_krule); 2250 VNET_DECLARE(struct allrulelist_head, pf_allrulelist); 2251 #define V_pf_allrulelist VNET(pf_allrulelist) 2252 VNET_DECLARE(size_t, pf_allrulecount); 2253 #define V_pf_allrulecount VNET(pf_allrulecount) 2254 VNET_DECLARE(struct pf_krule *, pf_rulemarker); 2255 #define V_pf_rulemarker VNET(pf_rulemarker) 2256 #endif 2257 2258 int pf_start(void); 2259 int pf_stop(void); 2260 void pf_initialize(void); 2261 void pf_mtag_initialize(void); 2262 void pf_mtag_cleanup(void); 2263 void pf_cleanup(void); 2264 2265 struct pf_mtag *pf_get_mtag(struct mbuf *); 2266 2267 extern void pf_calc_skip_steps(struct pf_krulequeue *); 2268 #ifdef ALTQ 2269 extern void pf_altq_ifnet_event(struct ifnet *, int); 2270 #endif 2271 VNET_DECLARE(uma_zone_t, pf_state_z); 2272 #define V_pf_state_z VNET(pf_state_z) 2273 VNET_DECLARE(uma_zone_t, pf_state_key_z); 2274 #define V_pf_state_key_z VNET(pf_state_key_z) 2275 VNET_DECLARE(uma_zone_t, pf_udp_mapping_z); 2276 #define V_pf_udp_mapping_z VNET(pf_udp_mapping_z) 2277 VNET_DECLARE(uma_zone_t, pf_state_scrub_z); 2278 #define V_pf_state_scrub_z VNET(pf_state_scrub_z) 2279 2280 extern void pf_purge_thread(void *); 2281 extern void pf_unload_vnet_purge(void); 2282 extern void pf_intr(void *); 2283 extern void pf_purge_expired_src_nodes(void); 2284 2285 extern int pf_unlink_state(struct pf_kstate *); 2286 extern int pf_state_insert(struct pfi_kkif *, 2287 struct pfi_kkif *, 2288 struct pf_state_key *, 2289 struct pf_state_key *, 2290 struct pf_kstate *); 2291 extern struct pf_kstate *pf_alloc_state(int); 2292 extern void pf_free_state(struct pf_kstate *); 2293 extern void pf_killstates(struct pf_kstate_kill *, 2294 unsigned int *); 2295 extern unsigned int pf_clear_states(const struct pf_kstate_kill *); 2296 2297 static __inline void 2298 pf_ref_state(struct pf_kstate *s) 2299 { 2300 2301 refcount_acquire(&s->refs); 2302 } 2303 2304 static __inline int 2305 pf_release_state(struct pf_kstate *s) 2306 { 2307 2308 if (refcount_release(&s->refs)) { 2309 pf_free_state(s); 2310 return (1); 2311 } else 2312 return (0); 2313 } 2314 2315 static __inline int 2316 pf_release_staten(struct pf_kstate *s, u_int n) 2317 { 2318 2319 if (refcount_releasen(&s->refs, n)) { 2320 pf_free_state(s); 2321 return (1); 2322 } else 2323 return (0); 2324 } 2325 2326 static __inline uint64_t 2327 pf_get_uptime(void) 2328 { 2329 struct timeval t; 2330 microuptime(&t); 2331 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)); 2332 } 2333 2334 static __inline uint64_t 2335 pf_get_time(void) 2336 { 2337 struct timeval t; 2338 microtime(&t); 2339 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)); 2340 } 2341 2342 extern struct pf_kstate *pf_find_state_byid(uint64_t, uint32_t); 2343 extern struct pf_kstate *pf_find_state_all( 2344 const struct pf_state_key_cmp *, 2345 u_int, int *); 2346 extern bool pf_find_state_all_exists( 2347 const struct pf_state_key_cmp *, 2348 u_int); 2349 extern struct pf_udp_mapping *pf_udp_mapping_find(struct pf_udp_endpoint_cmp 2350 *endpoint); 2351 extern struct pf_udp_mapping *pf_udp_mapping_create(sa_family_t af, 2352 struct pf_addr *src_addr, uint16_t src_port, 2353 struct pf_addr *nat_addr, uint16_t nat_port); 2354 extern int pf_udp_mapping_insert(struct pf_udp_mapping 2355 *mapping); 2356 extern void pf_udp_mapping_release(struct pf_udp_mapping 2357 *mapping); 2358 uint32_t pf_hashsrc(struct pf_addr *, sa_family_t); 2359 extern bool pf_src_node_exists(struct pf_ksrc_node **, 2360 struct pf_srchash *); 2361 extern struct pf_ksrc_node *pf_find_src_node(struct pf_addr *, 2362 struct pf_krule *, sa_family_t, 2363 struct pf_srchash **, bool); 2364 extern void pf_unlink_src_node(struct pf_ksrc_node *); 2365 extern u_int pf_free_src_nodes(struct pf_ksrc_node_list *); 2366 extern void pf_print_state(struct pf_kstate *); 2367 extern void pf_print_flags(uint16_t); 2368 extern int pf_addr_wrap_neq(struct pf_addr_wrap *, 2369 struct pf_addr_wrap *); 2370 extern u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t, 2371 u_int8_t); 2372 extern u_int16_t pf_proto_cksum_fixup(struct mbuf *, u_int16_t, 2373 u_int16_t, u_int16_t, u_int8_t); 2374 2375 VNET_DECLARE(struct ifnet *, sync_ifp); 2376 #define V_sync_ifp VNET(sync_ifp); 2377 VNET_DECLARE(struct pf_krule, pf_default_rule); 2378 #define V_pf_default_rule VNET(pf_default_rule) 2379 extern void pf_addrcpy(struct pf_addr *, struct pf_addr *, 2380 sa_family_t); 2381 void pf_free_rule(struct pf_krule *); 2382 2383 int pf_test_eth(int, int, struct ifnet *, struct mbuf **, struct inpcb *); 2384 int pf_scan_sctp(struct pf_pdesc *); 2385 #if defined(INET) || defined(INET6) 2386 int pf_test(sa_family_t, int, int, struct ifnet *, struct mbuf **, struct inpcb *, 2387 struct pf_rule_actions *); 2388 #endif 2389 #ifdef INET 2390 int pf_normalize_ip(struct mbuf **, u_short *, struct pf_pdesc *); 2391 #endif /* INET */ 2392 2393 #ifdef INET6 2394 int pf_walk_header6(struct mbuf *, struct ip6_hdr *, int *, int *, int *, 2395 uint8_t *, uint32_t *, u_short *); 2396 int pf_normalize_ip6(struct mbuf **, int, u_short *, struct pf_pdesc *); 2397 void pf_poolmask(struct pf_addr *, struct pf_addr*, 2398 struct pf_addr *, struct pf_addr *, sa_family_t); 2399 void pf_addr_inc(struct pf_addr *, sa_family_t); 2400 int pf_max_frag_size(struct mbuf *); 2401 int pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *, 2402 struct ifnet *, bool); 2403 #endif /* INET6 */ 2404 2405 int pf_multihome_scan_init(int, int, struct pf_pdesc *); 2406 int pf_multihome_scan_asconf(int, int, struct pf_pdesc *); 2407 2408 u_int32_t pf_new_isn(struct pf_kstate *); 2409 void *pf_pull_hdr(const struct mbuf *, int, void *, int, u_short *, u_short *, 2410 sa_family_t); 2411 void pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t); 2412 void pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t, 2413 u_int8_t); 2414 void pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t); 2415 void pf_patch_16_unaligned(struct mbuf *, u_int16_t *, void *, u_int16_t, 2416 bool, u_int8_t); 2417 void pf_patch_32_unaligned(struct mbuf *, u_int16_t *, void *, u_int32_t, 2418 bool, u_int8_t); 2419 void pf_send_deferred_syn(struct pf_kstate *); 2420 int pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *, 2421 struct pf_addr *, sa_family_t); 2422 int pf_match_addr_range(struct pf_addr *, struct pf_addr *, 2423 struct pf_addr *, sa_family_t); 2424 int pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t); 2425 2426 void pf_normalize_init(void); 2427 void pf_normalize_cleanup(void); 2428 int pf_normalize_tcp(struct pf_pdesc *); 2429 void pf_normalize_tcp_cleanup(struct pf_kstate *); 2430 int pf_normalize_tcp_init(struct pf_pdesc *, 2431 struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *); 2432 int pf_normalize_tcp_stateful(struct pf_pdesc *, 2433 u_short *, struct tcphdr *, struct pf_kstate *, 2434 struct pf_state_peer *, struct pf_state_peer *, int *); 2435 int pf_normalize_sctp_init(struct pf_pdesc *, 2436 struct pf_state_peer *, struct pf_state_peer *); 2437 int pf_normalize_sctp(struct pf_pdesc *); 2438 u_int32_t 2439 pf_state_expires(const struct pf_kstate *); 2440 void pf_purge_expired_fragments(void); 2441 void pf_purge_fragments(uint32_t); 2442 int pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *, 2443 int); 2444 int pf_socket_lookup(struct pf_pdesc *); 2445 struct pf_state_key *pf_alloc_state_key(int); 2446 int pf_translate(struct pf_pdesc *, struct pf_addr *, u_int16_t, 2447 struct pf_addr *, u_int16_t, u_int16_t, int); 2448 int pf_translate_af(struct pf_pdesc *); 2449 void pfr_initialize(void); 2450 void pfr_cleanup(void); 2451 int pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t); 2452 void pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t, 2453 u_int64_t, int, int, int); 2454 int pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t, 2455 pf_addr_filter_func_t); 2456 void pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *); 2457 struct pfr_ktable * 2458 pfr_attach_table(struct pf_kruleset *, char *); 2459 struct pfr_ktable * 2460 pfr_eth_attach_table(struct pf_keth_ruleset *, char *); 2461 void pfr_detach_table(struct pfr_ktable *); 2462 int pfr_clr_tables(struct pfr_table *, int *, int); 2463 int pfr_add_tables(struct pfr_table *, int, int *, int); 2464 int pfr_del_tables(struct pfr_table *, int, int *, int); 2465 int pfr_table_count(struct pfr_table *, int); 2466 int pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int); 2467 int pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int); 2468 int pfr_clr_tstats(struct pfr_table *, int, int *, int); 2469 int pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int); 2470 int pfr_clr_addrs(struct pfr_table *, int *, int); 2471 int pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long); 2472 int pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2473 int); 2474 int pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2475 int); 2476 int pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2477 int *, int *, int *, int, u_int32_t); 2478 int pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int); 2479 int pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int); 2480 int pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *, 2481 int); 2482 int pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2483 int); 2484 int pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int); 2485 int pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int); 2486 int pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int); 2487 int pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *, 2488 int *, u_int32_t, int); 2489 2490 MALLOC_DECLARE(PFI_MTYPE); 2491 VNET_DECLARE(struct pfi_kkif *, pfi_all); 2492 #define V_pfi_all VNET(pfi_all) 2493 2494 void pfi_initialize(void); 2495 void pfi_initialize_vnet(void); 2496 void pfi_cleanup(void); 2497 void pfi_cleanup_vnet(void); 2498 void pfi_kkif_ref(struct pfi_kkif *); 2499 void pfi_kkif_unref(struct pfi_kkif *); 2500 struct pfi_kkif *pfi_kkif_find(const char *); 2501 struct pfi_kkif *pfi_kkif_attach(struct pfi_kkif *, const char *); 2502 int pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *); 2503 void pfi_kkif_purge(void); 2504 int pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *, 2505 sa_family_t); 2506 int pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t); 2507 void pfi_dynaddr_remove(struct pfi_dynaddr *); 2508 void pfi_dynaddr_copyout(struct pf_addr_wrap *); 2509 void pfi_update_status(const char *, struct pf_status *); 2510 void pfi_get_ifaces(const char *, struct pfi_kif *, int *); 2511 int pfi_set_flags(const char *, int); 2512 int pfi_clear_flags(const char *, int); 2513 2514 int pf_match_tag(struct mbuf *, struct pf_krule *, int *, int); 2515 int pf_tag_packet(struct pf_pdesc *, int); 2516 int pf_addr_cmp(struct pf_addr *, struct pf_addr *, 2517 sa_family_t); 2518 2519 u_int16_t pf_get_mss(struct pf_pdesc *); 2520 u_int8_t pf_get_wscale(struct pf_pdesc *); 2521 struct mbuf *pf_build_tcp(const struct pf_krule *, sa_family_t, 2522 const struct pf_addr *, const struct pf_addr *, 2523 u_int16_t, u_int16_t, u_int32_t, u_int32_t, 2524 u_int8_t, u_int16_t, u_int16_t, u_int8_t, int, 2525 u_int16_t, u_int16_t, int); 2526 void pf_send_tcp(const struct pf_krule *, sa_family_t, 2527 const struct pf_addr *, const struct pf_addr *, 2528 u_int16_t, u_int16_t, u_int32_t, u_int32_t, 2529 u_int8_t, u_int16_t, u_int16_t, u_int8_t, int, 2530 u_int16_t, u_int16_t, int); 2531 2532 void pf_syncookies_init(void); 2533 void pf_syncookies_cleanup(void); 2534 int pf_get_syncookies(struct pfioc_nv *); 2535 int pf_set_syncookies(struct pfioc_nv *); 2536 int pf_synflood_check(struct pf_pdesc *); 2537 void pf_syncookie_send(struct pf_pdesc *); 2538 bool pf_syncookie_check(struct pf_pdesc *); 2539 u_int8_t pf_syncookie_validate(struct pf_pdesc *); 2540 struct mbuf * pf_syncookie_recreate_syn(struct pf_pdesc *); 2541 2542 VNET_DECLARE(struct pf_kstatus, pf_status); 2543 #define V_pf_status VNET(pf_status) 2544 2545 struct pf_limit { 2546 uma_zone_t zone; 2547 u_int limit; 2548 }; 2549 VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); 2550 #define V_pf_limits VNET(pf_limits) 2551 2552 #endif /* _KERNEL */ 2553 2554 #ifdef _KERNEL 2555 struct pf_nl_pooladdr { 2556 u_int32_t action; 2557 u_int32_t ticket; 2558 u_int32_t nr; 2559 u_int32_t r_num; 2560 u_int8_t r_action; 2561 u_int8_t r_last; 2562 u_int8_t af; 2563 char anchor[MAXPATHLEN]; 2564 struct pf_pooladdr addr; 2565 /* Above this is identical to pfioc_pooladdr */ 2566 int which; 2567 }; 2568 2569 VNET_DECLARE(struct pf_kanchor_global, pf_anchors); 2570 #define V_pf_anchors VNET(pf_anchors) 2571 VNET_DECLARE(struct pf_kanchor, pf_main_anchor); 2572 #define V_pf_main_anchor VNET(pf_main_anchor) 2573 VNET_DECLARE(struct pf_keth_anchor_global, pf_keth_anchors); 2574 #define V_pf_keth_anchors VNET(pf_keth_anchors) 2575 #define pf_main_ruleset V_pf_main_anchor.ruleset 2576 2577 VNET_DECLARE(struct pf_keth_anchor, pf_main_keth_anchor); 2578 #define V_pf_main_keth_anchor VNET(pf_main_keth_anchor) 2579 VNET_DECLARE(struct pf_keth_ruleset*, pf_keth); 2580 #define V_pf_keth VNET(pf_keth) 2581 2582 void pf_init_kruleset(struct pf_kruleset *); 2583 void pf_init_keth(struct pf_keth_ruleset *); 2584 int pf_kanchor_setup(struct pf_krule *, 2585 const struct pf_kruleset *, const char *); 2586 int pf_kanchor_copyout(const struct pf_kruleset *, 2587 const struct pf_krule *, char *, size_t); 2588 int pf_kanchor_nvcopyout(const struct pf_kruleset *, 2589 const struct pf_krule *, nvlist_t *); 2590 void pf_kanchor_remove(struct pf_krule *); 2591 void pf_remove_if_empty_kruleset(struct pf_kruleset *); 2592 struct pf_kruleset *pf_find_kruleset(const char *); 2593 struct pf_kruleset *pf_find_or_create_kruleset(const char *); 2594 void pf_rs_initialize(void); 2595 2596 2597 struct pf_krule *pf_krule_alloc(void); 2598 2599 void pf_remove_if_empty_keth_ruleset( 2600 struct pf_keth_ruleset *); 2601 struct pf_keth_ruleset *pf_find_keth_ruleset(const char *); 2602 struct pf_keth_anchor *pf_find_keth_anchor(const char *); 2603 int pf_keth_anchor_setup(struct pf_keth_rule *, 2604 const struct pf_keth_ruleset *, const char *); 2605 int pf_keth_anchor_nvcopyout( 2606 const struct pf_keth_ruleset *, 2607 const struct pf_keth_rule *, nvlist_t *); 2608 struct pf_keth_ruleset *pf_find_or_create_keth_ruleset(const char *); 2609 void pf_keth_anchor_remove(struct pf_keth_rule *); 2610 2611 int pf_ioctl_getrules(struct pfioc_rule *); 2612 int pf_ioctl_addrule(struct pf_krule *, uint32_t, 2613 uint32_t, const char *, const char *, uid_t uid, 2614 pid_t); 2615 void pf_ioctl_clear_status(void); 2616 int pf_ioctl_get_timeout(int, int *); 2617 int pf_ioctl_set_timeout(int, int, int *); 2618 int pf_ioctl_get_limit(int, unsigned int *); 2619 int pf_ioctl_set_limit(int, unsigned int, unsigned int *); 2620 int pf_ioctl_begin_addrs(uint32_t *); 2621 int pf_ioctl_add_addr(struct pf_nl_pooladdr *); 2622 int pf_ioctl_get_addrs(struct pf_nl_pooladdr *); 2623 int pf_ioctl_get_addr(struct pf_nl_pooladdr *); 2624 int pf_ioctl_get_rulesets(struct pfioc_ruleset *); 2625 int pf_ioctl_get_ruleset(struct pfioc_ruleset *); 2626 2627 void pf_krule_free(struct pf_krule *); 2628 void pf_krule_clear_counters(struct pf_krule *); 2629 void pf_addr_copyout(struct pf_addr_wrap *); 2630 #endif 2631 2632 /* The fingerprint functions can be linked into userland programs (tcpdump) */ 2633 int pf_osfp_add(struct pf_osfp_ioctl *); 2634 #ifdef _KERNEL 2635 struct pf_osfp_enlist * 2636 pf_osfp_fingerprint(struct pf_pdesc *, const struct tcphdr *); 2637 #endif /* _KERNEL */ 2638 void pf_osfp_flush(void); 2639 int pf_osfp_get(struct pf_osfp_ioctl *); 2640 int pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t); 2641 2642 #ifdef _KERNEL 2643 void pf_print_host(struct pf_addr *, u_int16_t, sa_family_t); 2644 2645 void pf_step_into_anchor(struct pf_kanchor_stackframe *, int *, 2646 struct pf_kruleset **, int, struct pf_krule **, 2647 struct pf_krule **, int *); 2648 int pf_step_out_of_anchor(struct pf_kanchor_stackframe *, int *, 2649 struct pf_kruleset **, int, struct pf_krule **, 2650 struct pf_krule **, int *); 2651 void pf_step_into_keth_anchor(struct pf_keth_anchor_stackframe *, 2652 int *, struct pf_keth_ruleset **, 2653 struct pf_keth_rule **, struct pf_keth_rule **, 2654 int *); 2655 int pf_step_out_of_keth_anchor(struct pf_keth_anchor_stackframe *, 2656 int *, struct pf_keth_ruleset **, 2657 struct pf_keth_rule **, struct pf_keth_rule **, 2658 int *); 2659 2660 u_short pf_map_addr(u_int8_t, struct pf_krule *, 2661 struct pf_addr *, struct pf_addr *, 2662 struct pfi_kkif **nkif, struct pf_addr *, 2663 struct pf_kpool *); 2664 u_short pf_map_addr_sn(u_int8_t, struct pf_krule *, 2665 struct pf_addr *, struct pf_addr *, 2666 struct pfi_kkif **nkif, struct pf_addr *, 2667 struct pf_ksrc_node **, struct pf_srchash **, 2668 struct pf_kpool *); 2669 int pf_get_transaddr_af(struct pf_krule *, 2670 struct pf_pdesc *); 2671 u_short pf_get_translation(struct pf_pdesc *, 2672 int, struct pf_state_key **, struct pf_state_key **, 2673 struct pf_kanchor_stackframe *, struct pf_krule **, 2674 struct pf_udp_mapping **udp_mapping); 2675 2676 int pf_state_key_setup(struct pf_pdesc *, 2677 u_int16_t, u_int16_t, 2678 struct pf_state_key **sk, struct pf_state_key **nk); 2679 struct pf_state_key *pf_state_key_clone(const struct pf_state_key *); 2680 void pf_rule_to_actions(struct pf_krule *, 2681 struct pf_rule_actions *); 2682 int pf_normalize_mss(struct pf_pdesc *pd); 2683 #if defined(INET) || defined(INET6) 2684 void pf_scrub(struct pf_pdesc *); 2685 #endif 2686 2687 struct pfi_kkif *pf_kkif_create(int); 2688 void pf_kkif_free(struct pfi_kkif *); 2689 void pf_kkif_zero(struct pfi_kkif *); 2690 2691 2692 /* NAT64 functions. */ 2693 int inet_nat64(int, const void *, void *, const void *, u_int8_t); 2694 int inet_nat64_inet(const void *, void *, const void *, u_int8_t); 2695 int inet_nat64_inet6(const void *, void *, const void *, u_int8_t); 2696 2697 int inet_nat46(int, const void *, void *, const void *, u_int8_t); 2698 int inet_nat46_inet(const void *, void *, const void *, u_int8_t); 2699 int inet_nat46_inet6(const void *, void *, const void *, u_int8_t); 2700 2701 #endif /* _KERNEL */ 2702 2703 #endif /* _NET_PFVAR_H_ */ 2704