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