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 #if defined(__arm__) 74 #define PF_WANT_32_TO_64_COUNTER 75 #endif 76 77 /* 78 * A hybrid of 32-bit and 64-bit counters which can be used on platforms where 79 * counter(9) is very expensive. 80 * 81 * As 32-bit counters are expected to overflow, a periodic job sums them up to 82 * a saved 64-bit state. Fetching the value still walks all CPUs to get the most 83 * current snapshot. 84 */ 85 #ifdef PF_WANT_32_TO_64_COUNTER 86 struct pf_counter_u64_pcpu { 87 u_int32_t current; 88 u_int32_t snapshot; 89 }; 90 91 struct pf_counter_u64 { 92 struct pf_counter_u64_pcpu *pfcu64_pcpu; 93 u_int64_t pfcu64_value; 94 seqc_t pfcu64_seqc; 95 }; 96 97 static inline int 98 pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags) 99 { 100 101 pfcu64->pfcu64_value = 0; 102 pfcu64->pfcu64_seqc = 0; 103 pfcu64->pfcu64_pcpu = uma_zalloc_pcpu(pcpu_zone_8, flags | M_ZERO); 104 if (__predict_false(pfcu64->pfcu64_pcpu == NULL)) 105 return (ENOMEM); 106 return (0); 107 } 108 109 static inline void 110 pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64) 111 { 112 113 uma_zfree_pcpu(pcpu_zone_8, pfcu64->pfcu64_pcpu); 114 } 115 116 static inline void 117 pf_counter_u64_critical_enter(void) 118 { 119 120 critical_enter(); 121 } 122 123 static inline void 124 pf_counter_u64_critical_exit(void) 125 { 126 127 critical_exit(); 128 } 129 130 static inline void 131 pf_counter_u64_rollup_protected(struct pf_counter_u64 *pfcu64, uint64_t n) 132 { 133 134 MPASS(curthread->td_critnest > 0); 135 pfcu64->pfcu64_value += n; 136 } 137 138 static inline void 139 pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n) 140 { 141 struct pf_counter_u64_pcpu *pcpu; 142 u_int32_t val; 143 144 MPASS(curthread->td_critnest > 0); 145 pcpu = zpcpu_get(pfcu64->pfcu64_pcpu); 146 val = atomic_load_int(&pcpu->current); 147 atomic_store_int(&pcpu->current, val + n); 148 } 149 150 static inline void 151 pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n) 152 { 153 154 critical_enter(); 155 pf_counter_u64_add_protected(pfcu64, n); 156 critical_exit(); 157 } 158 159 static inline u_int64_t 160 pf_counter_u64_periodic(struct pf_counter_u64 *pfcu64) 161 { 162 struct pf_counter_u64_pcpu *pcpu; 163 u_int64_t sum; 164 u_int32_t val; 165 int cpu; 166 167 MPASS(curthread->td_critnest > 0); 168 seqc_write_begin(&pfcu64->pfcu64_seqc); 169 sum = pfcu64->pfcu64_value; 170 CPU_FOREACH(cpu) { 171 pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); 172 val = atomic_load_int(&pcpu->current); 173 sum += (uint32_t)(val - pcpu->snapshot); 174 pcpu->snapshot = val; 175 } 176 pfcu64->pfcu64_value = sum; 177 seqc_write_end(&pfcu64->pfcu64_seqc); 178 return (sum); 179 } 180 181 static inline u_int64_t 182 pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64) 183 { 184 struct pf_counter_u64_pcpu *pcpu; 185 u_int64_t sum; 186 seqc_t seqc; 187 int cpu; 188 189 for (;;) { 190 seqc = seqc_read(&pfcu64->pfcu64_seqc); 191 sum = 0; 192 CPU_FOREACH(cpu) { 193 pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); 194 sum += (uint32_t)(atomic_load_int(&pcpu->current) -pcpu->snapshot); 195 } 196 sum += pfcu64->pfcu64_value; 197 if (seqc_consistent(&pfcu64->pfcu64_seqc, seqc)) 198 break; 199 } 200 return (sum); 201 } 202 203 static inline void 204 pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64) 205 { 206 struct pf_counter_u64_pcpu *pcpu; 207 int cpu; 208 209 MPASS(curthread->td_critnest > 0); 210 seqc_write_begin(&pfcu64->pfcu64_seqc); 211 CPU_FOREACH(cpu) { 212 pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); 213 pcpu->snapshot = atomic_load_int(&pcpu->current); 214 } 215 pfcu64->pfcu64_value = 0; 216 seqc_write_end(&pfcu64->pfcu64_seqc); 217 } 218 219 static inline void 220 pf_counter_u64_zero(struct pf_counter_u64 *pfcu64) 221 { 222 223 critical_enter(); 224 pf_counter_u64_zero_protected(pfcu64); 225 critical_exit(); 226 } 227 #else 228 struct pf_counter_u64 { 229 counter_u64_t counter; 230 }; 231 232 static inline int 233 pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags) 234 { 235 236 pfcu64->counter = counter_u64_alloc(flags); 237 if (__predict_false(pfcu64->counter == NULL)) 238 return (ENOMEM); 239 return (0); 240 } 241 242 static inline void 243 pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64) 244 { 245 246 counter_u64_free(pfcu64->counter); 247 } 248 249 static inline void 250 pf_counter_u64_critical_enter(void) 251 { 252 253 } 254 255 static inline void 256 pf_counter_u64_critical_exit(void) 257 { 258 259 } 260 261 static inline void 262 pf_counter_u64_rollup_protected(struct pf_counter_u64 *pfcu64, uint64_t n) 263 { 264 265 counter_u64_add(pfcu64->counter, n); 266 } 267 268 static inline void 269 pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n) 270 { 271 272 counter_u64_add(pfcu64->counter, n); 273 } 274 275 static inline void 276 pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n) 277 { 278 279 pf_counter_u64_add_protected(pfcu64, n); 280 } 281 282 static inline u_int64_t 283 pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64) 284 { 285 286 return (counter_u64_fetch(pfcu64->counter)); 287 } 288 289 static inline void 290 pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64) 291 { 292 293 counter_u64_zero(pfcu64->counter); 294 } 295 296 static inline void 297 pf_counter_u64_zero(struct pf_counter_u64 *pfcu64) 298 { 299 300 pf_counter_u64_zero_protected(pfcu64); 301 } 302 #endif 303 304 #define pf_get_timestamp(prule)({ \ 305 uint32_t _ts = 0; \ 306 uint32_t __ts; \ 307 int cpu; \ 308 CPU_FOREACH(cpu) { \ 309 __ts = *zpcpu_get_cpu(prule->timestamp, cpu); \ 310 if (__ts > _ts) \ 311 _ts = __ts; \ 312 } \ 313 _ts; \ 314 }) 315 316 #define pf_update_timestamp(prule) \ 317 do { \ 318 critical_enter(); \ 319 *zpcpu_get((prule)->timestamp) = time_second; \ 320 critical_exit(); \ 321 } while (0) 322 323 #define pf_timestamp_pcpu_zone (sizeof(time_t) == 4 ? pcpu_zone_4 : pcpu_zone_8) 324 _Static_assert(sizeof(time_t) == 4 || sizeof(time_t) == 8, "unexpected time_t size"); 325 326 SYSCTL_DECL(_net_pf); 327 MALLOC_DECLARE(M_PFHASH); 328 MALLOC_DECLARE(M_PF_RULE_ITEM); 329 330 SDT_PROVIDER_DECLARE(pf); 331 332 struct pfi_dynaddr { 333 TAILQ_ENTRY(pfi_dynaddr) entry; 334 struct pf_addr pfid_addr4; 335 struct pf_addr pfid_mask4; 336 struct pf_addr pfid_addr6; 337 struct pf_addr pfid_mask6; 338 struct pfr_ktable *pfid_kt; 339 struct pfi_kkif *pfid_kif; 340 int pfid_net; /* mask or 128 */ 341 int pfid_acnt4; /* address count IPv4 */ 342 int pfid_acnt6; /* address count IPv6 */ 343 sa_family_t pfid_af; /* rule af */ 344 u_int8_t pfid_iflags; /* PFI_AFLAG_* */ 345 }; 346 347 /* 348 * Address manipulation macros 349 */ 350 #define HTONL(x) (x) = htonl((__uint32_t)(x)) 351 #define HTONS(x) (x) = htons((__uint16_t)(x)) 352 #define NTOHL(x) (x) = ntohl((__uint32_t)(x)) 353 #define NTOHS(x) (x) = ntohs((__uint16_t)(x)) 354 355 #define PF_NAME "pf" 356 357 #define PF_HASHROW_ASSERT(h) mtx_assert(&(h)->lock, MA_OWNED) 358 #define PF_HASHROW_LOCK(h) mtx_lock(&(h)->lock) 359 #define PF_HASHROW_UNLOCK(h) mtx_unlock(&(h)->lock) 360 361 #ifdef INVARIANTS 362 #define PF_STATE_LOCK(s) \ 363 do { \ 364 struct pf_kstate *_s = (s); \ 365 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 366 MPASS(_s->lock == &_ih->lock); \ 367 mtx_lock(_s->lock); \ 368 } while (0) 369 #define PF_STATE_UNLOCK(s) \ 370 do { \ 371 struct pf_kstate *_s = (s); \ 372 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 373 MPASS(_s->lock == &_ih->lock); \ 374 mtx_unlock(_s->lock); \ 375 } while (0) 376 #else 377 #define PF_STATE_LOCK(s) mtx_lock((s)->lock) 378 #define PF_STATE_UNLOCK(s) mtx_unlock((s)->lock) 379 #endif 380 381 #ifdef INVARIANTS 382 #define PF_STATE_LOCK_ASSERT(s) \ 383 do { \ 384 struct pf_kstate *_s = (s); \ 385 struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 386 MPASS(_s->lock == &_ih->lock); \ 387 PF_HASHROW_ASSERT(_ih); \ 388 } while (0) 389 #else /* !INVARIANTS */ 390 #define PF_STATE_LOCK_ASSERT(s) do {} while (0) 391 #endif /* INVARIANTS */ 392 393 #ifdef INVARIANTS 394 #define PF_SRC_NODE_LOCK(sn) \ 395 do { \ 396 struct pf_ksrc_node *_sn = (sn); \ 397 struct pf_srchash *_sh = &V_pf_srchash[ \ 398 pf_hashsrc(&_sn->addr, _sn->af)]; \ 399 MPASS(_sn->lock == &_sh->lock); \ 400 mtx_lock(_sn->lock); \ 401 } while (0) 402 #define PF_SRC_NODE_UNLOCK(sn) \ 403 do { \ 404 struct pf_ksrc_node *_sn = (sn); \ 405 struct pf_srchash *_sh = &V_pf_srchash[ \ 406 pf_hashsrc(&_sn->addr, _sn->af)]; \ 407 MPASS(_sn->lock == &_sh->lock); \ 408 mtx_unlock(_sn->lock); \ 409 } while (0) 410 #else 411 #define PF_SRC_NODE_LOCK(sn) mtx_lock((sn)->lock) 412 #define PF_SRC_NODE_UNLOCK(sn) mtx_unlock((sn)->lock) 413 #endif 414 415 #ifdef INVARIANTS 416 #define PF_SRC_NODE_LOCK_ASSERT(sn) \ 417 do { \ 418 struct pf_ksrc_node *_sn = (sn); \ 419 struct pf_srchash *_sh = &V_pf_srchash[ \ 420 pf_hashsrc(&_sn->addr, _sn->af)]; \ 421 MPASS(_sn->lock == &_sh->lock); \ 422 PF_HASHROW_ASSERT(_sh); \ 423 } while (0) 424 #else /* !INVARIANTS */ 425 #define PF_SRC_NODE_LOCK_ASSERT(sn) do {} while (0) 426 #endif /* INVARIANTS */ 427 428 extern struct mtx_padalign pf_unlnkdrules_mtx; 429 #define PF_UNLNKDRULES_LOCK() mtx_lock(&pf_unlnkdrules_mtx) 430 #define PF_UNLNKDRULES_UNLOCK() mtx_unlock(&pf_unlnkdrules_mtx) 431 #define PF_UNLNKDRULES_ASSERT() mtx_assert(&pf_unlnkdrules_mtx, MA_OWNED) 432 433 extern struct sx pf_config_lock; 434 #define PF_CONFIG_LOCK() sx_xlock(&pf_config_lock) 435 #define PF_CONFIG_UNLOCK() sx_xunlock(&pf_config_lock) 436 #define PF_CONFIG_ASSERT() sx_assert(&pf_config_lock, SA_XLOCKED) 437 438 VNET_DECLARE(struct rmlock, pf_rules_lock); 439 #define V_pf_rules_lock VNET(pf_rules_lock) 440 441 #define PF_RULES_RLOCK_TRACKER struct rm_priotracker _pf_rules_tracker 442 #define PF_RULES_RLOCK() rm_rlock(&V_pf_rules_lock, &_pf_rules_tracker) 443 #define PF_RULES_RUNLOCK() rm_runlock(&V_pf_rules_lock, &_pf_rules_tracker) 444 #define PF_RULES_WLOCK() rm_wlock(&V_pf_rules_lock) 445 #define PF_RULES_WUNLOCK() rm_wunlock(&V_pf_rules_lock) 446 #define PF_RULES_WOWNED() rm_wowned(&V_pf_rules_lock) 447 #define PF_RULES_ASSERT() rm_assert(&V_pf_rules_lock, RA_LOCKED) 448 #define PF_RULES_RASSERT() rm_assert(&V_pf_rules_lock, RA_RLOCKED) 449 #define PF_RULES_WASSERT() rm_assert(&V_pf_rules_lock, RA_WLOCKED) 450 451 extern struct mtx_padalign pf_table_stats_lock; 452 #define PF_TABLE_STATS_LOCK() mtx_lock(&pf_table_stats_lock) 453 #define PF_TABLE_STATS_UNLOCK() mtx_unlock(&pf_table_stats_lock) 454 #define PF_TABLE_STATS_OWNED() mtx_owned(&pf_table_stats_lock) 455 #define PF_TABLE_STATS_ASSERT() mtx_assert(&pf_table_stats_lock, MA_OWNED) 456 457 extern struct sx pf_end_lock; 458 459 #define PF_MODVER 1 460 #define PFLOG_MODVER 1 461 #define PFSYNC_MODVER 1 462 463 #define PFLOG_MINVER 1 464 #define PFLOG_PREFVER PFLOG_MODVER 465 #define PFLOG_MAXVER 1 466 #define PFSYNC_MINVER 1 467 #define PFSYNC_PREFVER PFSYNC_MODVER 468 #define PFSYNC_MAXVER 1 469 470 #ifdef INET 471 #ifndef INET6 472 #define PF_INET_ONLY 473 #endif /* ! INET6 */ 474 #endif /* INET */ 475 476 #ifdef INET6 477 #ifndef INET 478 #define PF_INET6_ONLY 479 #endif /* ! INET */ 480 #endif /* INET6 */ 481 482 #ifdef INET 483 #ifdef INET6 484 #define PF_INET_INET6 485 #endif /* INET6 */ 486 #endif /* INET */ 487 488 #else 489 490 #define PF_INET_INET6 491 492 #endif /* _KERNEL */ 493 494 /* Both IPv4 and IPv6 */ 495 #ifdef PF_INET_INET6 496 497 #define PF_AEQ(a, b, c) \ 498 ((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \ 499 (c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \ 500 (a)->addr32[2] == (b)->addr32[2] && \ 501 (a)->addr32[1] == (b)->addr32[1] && \ 502 (a)->addr32[0] == (b)->addr32[0])) \ 503 504 #define PF_ANEQ(a, b, c) \ 505 ((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \ 506 (c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \ 507 (a)->addr32[1] != (b)->addr32[1] || \ 508 (a)->addr32[2] != (b)->addr32[2] || \ 509 (a)->addr32[3] != (b)->addr32[3]))) \ 510 511 #define PF_AZERO(a, c) \ 512 ((c == AF_INET && !(a)->addr32[0]) || \ 513 (c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \ 514 !(a)->addr32[2] && !(a)->addr32[3] )) \ 515 516 #define PF_MATCHA(n, a, m, b, f) \ 517 pf_match_addr(n, a, m, b, f) 518 519 #define PF_ACPY(a, b, f) \ 520 pf_addrcpy(a, b, f) 521 522 #define PF_AINC(a, f) \ 523 pf_addr_inc(a, f) 524 525 #define PF_POOLMASK(a, b, c, d, f) \ 526 pf_poolmask(a, b, c, d, f) 527 528 #else 529 530 /* Just IPv6 */ 531 532 #ifdef PF_INET6_ONLY 533 534 #define PF_AEQ(a, b, c) \ 535 ((a)->addr32[3] == (b)->addr32[3] && \ 536 (a)->addr32[2] == (b)->addr32[2] && \ 537 (a)->addr32[1] == (b)->addr32[1] && \ 538 (a)->addr32[0] == (b)->addr32[0]) \ 539 540 #define PF_ANEQ(a, b, c) \ 541 ((a)->addr32[3] != (b)->addr32[3] || \ 542 (a)->addr32[2] != (b)->addr32[2] || \ 543 (a)->addr32[1] != (b)->addr32[1] || \ 544 (a)->addr32[0] != (b)->addr32[0]) \ 545 546 #define PF_AZERO(a, c) \ 547 (!(a)->addr32[0] && \ 548 !(a)->addr32[1] && \ 549 !(a)->addr32[2] && \ 550 !(a)->addr32[3] ) \ 551 552 #define PF_MATCHA(n, a, m, b, f) \ 553 pf_match_addr(n, a, m, b, f) 554 555 #define PF_ACPY(a, b, f) \ 556 pf_addrcpy(a, b, f) 557 558 #define PF_AINC(a, f) \ 559 pf_addr_inc(a, f) 560 561 #define PF_POOLMASK(a, b, c, d, f) \ 562 pf_poolmask(a, b, c, d, f) 563 564 #else 565 566 /* Just IPv4 */ 567 #ifdef PF_INET_ONLY 568 569 #define PF_AEQ(a, b, c) \ 570 ((a)->addr32[0] == (b)->addr32[0]) 571 572 #define PF_ANEQ(a, b, c) \ 573 ((a)->addr32[0] != (b)->addr32[0]) 574 575 #define PF_AZERO(a, c) \ 576 (!(a)->addr32[0]) 577 578 #define PF_MATCHA(n, a, m, b, f) \ 579 pf_match_addr(n, a, m, b, f) 580 581 #define PF_ACPY(a, b, f) \ 582 (a)->v4.s_addr = (b)->v4.s_addr 583 584 #define PF_AINC(a, f) \ 585 do { \ 586 (a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \ 587 } while (0) 588 589 #define PF_POOLMASK(a, b, c, d, f) \ 590 do { \ 591 (a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \ 592 (((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \ 593 } while (0) 594 595 #endif /* PF_INET_ONLY */ 596 #endif /* PF_INET6_ONLY */ 597 #endif /* PF_INET_INET6 */ 598 599 /* 600 * XXX callers not FIB-aware in our version of pf yet. 601 * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio. 602 */ 603 #define PF_MISMATCHAW(aw, x, af, neg, ifp, rtid) \ 604 ( \ 605 (((aw)->type == PF_ADDR_NOROUTE && \ 606 pf_routable((x), (af), NULL, (rtid))) || \ 607 (((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL && \ 608 pf_routable((x), (af), (ifp), (rtid))) || \ 609 ((aw)->type == PF_ADDR_TABLE && \ 610 !pfr_match_addr((aw)->p.tbl, (x), (af))) || \ 611 ((aw)->type == PF_ADDR_DYNIFTL && \ 612 !pfi_match_addr((aw)->p.dyn, (x), (af))) || \ 613 ((aw)->type == PF_ADDR_RANGE && \ 614 !pf_match_addr_range(&(aw)->v.a.addr, \ 615 &(aw)->v.a.mask, (x), (af))) || \ 616 ((aw)->type == PF_ADDR_ADDRMASK && \ 617 !PF_AZERO(&(aw)->v.a.mask, (af)) && \ 618 !PF_MATCHA(0, &(aw)->v.a.addr, \ 619 &(aw)->v.a.mask, (x), (af))))) != \ 620 (neg) \ 621 ) 622 623 #define PF_ALGNMNT(off) (((off) % 2) == 0) 624 625 #ifdef _KERNEL 626 627 struct pf_kpooladdr { 628 struct pf_addr_wrap addr; 629 TAILQ_ENTRY(pf_kpooladdr) entries; 630 char ifname[IFNAMSIZ]; 631 struct pfi_kkif *kif; 632 }; 633 634 TAILQ_HEAD(pf_kpalist, pf_kpooladdr); 635 636 struct pf_kpool { 637 struct mtx mtx; 638 struct pf_kpalist list; 639 struct pf_kpooladdr *cur; 640 struct pf_poolhashkey key; 641 struct pf_addr counter; 642 struct pf_mape_portset mape; 643 int tblidx; 644 u_int16_t proxy_port[2]; 645 u_int8_t opts; 646 }; 647 648 struct pf_rule_actions { 649 int32_t rtableid; 650 uint16_t qid; 651 uint16_t pqid; 652 uint16_t max_mss; 653 uint8_t log; 654 uint8_t set_tos; 655 uint8_t min_ttl; 656 uint16_t dnpipe; 657 uint16_t dnrpipe; /* Reverse direction pipe */ 658 uint32_t flags; 659 uint8_t set_prio[2]; 660 }; 661 662 union pf_keth_rule_ptr { 663 struct pf_keth_rule *ptr; 664 uint32_t nr; 665 }; 666 667 struct pf_keth_rule_addr { 668 uint8_t addr[ETHER_ADDR_LEN]; 669 uint8_t mask[ETHER_ADDR_LEN]; 670 bool neg; 671 uint8_t isset; 672 }; 673 674 struct pf_keth_anchor; 675 676 TAILQ_HEAD(pf_keth_ruleq, pf_keth_rule); 677 678 struct pf_keth_ruleset { 679 struct pf_keth_ruleq rules[2]; 680 struct pf_keth_rules { 681 struct pf_keth_ruleq *rules; 682 int open; 683 uint32_t ticket; 684 } active, inactive; 685 struct epoch_context epoch_ctx; 686 struct vnet *vnet; 687 struct pf_keth_anchor *anchor; 688 }; 689 690 RB_HEAD(pf_keth_anchor_global, pf_keth_anchor); 691 RB_HEAD(pf_keth_anchor_node, pf_keth_anchor); 692 struct pf_keth_anchor { 693 RB_ENTRY(pf_keth_anchor) entry_node; 694 RB_ENTRY(pf_keth_anchor) entry_global; 695 struct pf_keth_anchor *parent; 696 struct pf_keth_anchor_node children; 697 char name[PF_ANCHOR_NAME_SIZE]; 698 char path[MAXPATHLEN]; 699 struct pf_keth_ruleset ruleset; 700 int refcnt; /* anchor rules */ 701 uint8_t anchor_relative; 702 uint8_t anchor_wildcard; 703 }; 704 RB_PROTOTYPE(pf_keth_anchor_node, pf_keth_anchor, entry_node, 705 pf_keth_anchor_compare); 706 RB_PROTOTYPE(pf_keth_anchor_global, pf_keth_anchor, entry_global, 707 pf_keth_anchor_compare); 708 709 struct pf_keth_rule { 710 #define PFE_SKIP_IFP 0 711 #define PFE_SKIP_DIR 1 712 #define PFE_SKIP_PROTO 2 713 #define PFE_SKIP_SRC_ADDR 3 714 #define PFE_SKIP_DST_ADDR 4 715 #define PFE_SKIP_SRC_IP_ADDR 5 716 #define PFE_SKIP_DST_IP_ADDR 6 717 #define PFE_SKIP_COUNT 7 718 union pf_keth_rule_ptr skip[PFE_SKIP_COUNT]; 719 720 TAILQ_ENTRY(pf_keth_rule) entries; 721 722 struct pf_keth_anchor *anchor; 723 u_int8_t anchor_relative; 724 u_int8_t anchor_wildcard; 725 726 uint32_t nr; 727 728 bool quick; 729 730 /* Filter */ 731 char ifname[IFNAMSIZ]; 732 struct pfi_kkif *kif; 733 bool ifnot; 734 uint8_t direction; 735 uint16_t proto; 736 struct pf_keth_rule_addr src, dst; 737 struct pf_rule_addr ipsrc, ipdst; 738 char match_tagname[PF_TAG_NAME_SIZE]; 739 uint16_t match_tag; 740 bool match_tag_not; 741 742 743 /* Stats */ 744 counter_u64_t evaluations; 745 counter_u64_t packets[2]; 746 counter_u64_t bytes[2]; 747 time_t *timestamp; 748 749 /* Action */ 750 char qname[PF_QNAME_SIZE]; 751 int qid; 752 char tagname[PF_TAG_NAME_SIZE]; 753 uint16_t tag; 754 char bridge_to_name[IFNAMSIZ]; 755 struct pfi_kkif *bridge_to; 756 uint8_t action; 757 uint16_t dnpipe; 758 uint32_t dnflags; 759 760 char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; 761 uint32_t ridentifier; 762 }; 763 764 union pf_krule_ptr { 765 struct pf_krule *ptr; 766 u_int32_t nr; 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 union pf_krule_ptr 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 qname[PF_QNAME_SIZE]; 780 char pqname[PF_QNAME_SIZE]; 781 char tagname[PF_TAG_NAME_SIZE]; 782 char match_tagname[PF_TAG_NAME_SIZE]; 783 784 char overload_tblname[PF_TABLE_NAME_SIZE]; 785 786 TAILQ_ENTRY(pf_krule) entries; 787 struct pf_kpool rpool; 788 789 struct pf_counter_u64 evaluations; 790 struct pf_counter_u64 packets[2]; 791 struct pf_counter_u64 bytes[2]; 792 time_t *timestamp; 793 794 struct pfi_kkif *kif; 795 struct pf_kanchor *anchor; 796 struct pfr_ktable *overload_tbl; 797 798 pf_osfp_t os_fingerprint; 799 800 int32_t rtableid; 801 u_int32_t timeout[PFTM_MAX]; 802 u_int32_t max_states; 803 u_int32_t max_src_nodes; 804 u_int32_t max_src_states; 805 u_int32_t max_src_conn; 806 struct { 807 u_int32_t limit; 808 u_int32_t seconds; 809 } max_src_conn_rate; 810 u_int16_t qid; 811 u_int16_t pqid; 812 u_int16_t dnpipe; 813 u_int16_t dnrpipe; 814 u_int32_t free_flags; 815 u_int32_t nr; 816 u_int32_t prob; 817 uid_t cuid; 818 pid_t cpid; 819 820 counter_u64_t states_cur; 821 counter_u64_t states_tot; 822 counter_u64_t src_nodes; 823 824 u_int16_t return_icmp; 825 u_int16_t return_icmp6; 826 u_int16_t max_mss; 827 u_int16_t tag; 828 u_int16_t match_tag; 829 u_int16_t scrub_flags; 830 831 struct pf_rule_uid uid; 832 struct pf_rule_gid gid; 833 834 u_int32_t rule_flag; 835 uint32_t rule_ref; 836 u_int8_t action; 837 u_int8_t direction; 838 u_int8_t log; 839 u_int8_t logif; 840 u_int8_t quick; 841 u_int8_t ifnot; 842 u_int8_t match_tag_not; 843 u_int8_t natpass; 844 845 u_int8_t keep_state; 846 sa_family_t af; 847 u_int8_t proto; 848 u_int8_t type; 849 u_int8_t code; 850 u_int8_t flags; 851 u_int8_t flagset; 852 u_int8_t min_ttl; 853 u_int8_t allow_opts; 854 u_int8_t rt; 855 u_int8_t return_ttl; 856 u_int8_t tos; 857 u_int8_t set_tos; 858 u_int8_t anchor_relative; 859 u_int8_t anchor_wildcard; 860 861 u_int8_t flush; 862 u_int8_t prio; 863 u_int8_t set_prio[2]; 864 865 struct { 866 struct pf_addr addr; 867 u_int16_t port; 868 } divert; 869 u_int8_t md5sum[PF_MD5_DIGEST_LENGTH]; 870 RB_ENTRY(pf_krule) entry_global; 871 872 #ifdef PF_WANT_32_TO_64_COUNTER 873 LIST_ENTRY(pf_krule) allrulelist; 874 bool allrulelinked; 875 #endif 876 }; 877 878 struct pf_krule_item { 879 SLIST_ENTRY(pf_krule_item) entry; 880 struct pf_krule *r; 881 }; 882 883 SLIST_HEAD(pf_krule_slist, pf_krule_item); 884 885 struct pf_ksrc_node { 886 LIST_ENTRY(pf_ksrc_node) entry; 887 struct pf_addr addr; 888 struct pf_addr raddr; 889 struct pf_krule_slist match_rules; 890 union pf_krule_ptr rule; 891 struct pfi_kkif *rkif; 892 counter_u64_t bytes[2]; 893 counter_u64_t packets[2]; 894 u_int32_t states; 895 u_int32_t conn; 896 struct pf_threshold conn_rate; 897 u_int32_t creation; 898 u_int32_t expire; 899 sa_family_t af; 900 u_int8_t ruletype; 901 struct mtx *lock; 902 }; 903 #endif 904 905 struct pf_state_scrub { 906 struct timeval pfss_last; /* time received last packet */ 907 u_int32_t pfss_tsecr; /* last echoed timestamp */ 908 u_int32_t pfss_tsval; /* largest timestamp */ 909 u_int32_t pfss_tsval0; /* original timestamp */ 910 u_int16_t pfss_flags; 911 #define PFSS_TIMESTAMP 0x0001 /* modulate timestamp */ 912 #define PFSS_PAWS 0x0010 /* stricter PAWS checks */ 913 #define PFSS_PAWS_IDLED 0x0020 /* was idle too long. no PAWS */ 914 #define PFSS_DATA_TS 0x0040 /* timestamp on data packets */ 915 #define PFSS_DATA_NOTS 0x0080 /* no timestamp on data packets */ 916 u_int8_t pfss_ttl; /* stashed TTL */ 917 u_int8_t pad; 918 union { 919 u_int32_t pfss_ts_mod; /* timestamp modulation */ 920 u_int32_t pfss_v_tag; /* SCTP verification tag */ 921 }; 922 }; 923 924 struct pf_state_host { 925 struct pf_addr addr; 926 u_int16_t port; 927 u_int16_t pad; 928 }; 929 930 struct pf_state_peer { 931 struct pf_state_scrub *scrub; /* state is scrubbed */ 932 u_int32_t seqlo; /* Max sequence number sent */ 933 u_int32_t seqhi; /* Max the other end ACKd + win */ 934 u_int32_t seqdiff; /* Sequence number modulator */ 935 u_int16_t max_win; /* largest window (pre scaling) */ 936 u_int16_t mss; /* Maximum segment size option */ 937 u_int8_t state; /* active state level */ 938 u_int8_t wscale; /* window scaling factor */ 939 u_int8_t tcp_est; /* Did we reach TCPS_ESTABLISHED */ 940 u_int8_t pad[1]; 941 }; 942 943 /* Keep synced with struct pf_udp_endpoint. */ 944 struct pf_udp_endpoint_cmp { 945 struct pf_addr addr; 946 uint16_t port; 947 sa_family_t af; 948 uint8_t pad[1]; 949 }; 950 951 struct pf_udp_endpoint { 952 struct pf_addr addr; 953 uint16_t port; 954 sa_family_t af; 955 uint8_t pad[1]; 956 957 struct pf_udp_mapping *mapping; 958 LIST_ENTRY(pf_udp_endpoint) entry; 959 }; 960 961 struct pf_udp_mapping { 962 struct pf_udp_endpoint endpoints[2]; 963 u_int refs; 964 }; 965 966 /* Keep synced with struct pf_state_key. */ 967 struct pf_state_key_cmp { 968 struct pf_addr addr[2]; 969 u_int16_t port[2]; 970 sa_family_t af; 971 u_int8_t proto; 972 u_int8_t pad[2]; 973 }; 974 975 struct pf_state_key { 976 struct pf_addr addr[2]; 977 u_int16_t port[2]; 978 sa_family_t af; 979 u_int8_t proto; 980 u_int8_t pad[2]; 981 982 LIST_ENTRY(pf_state_key) entry; 983 TAILQ_HEAD(, pf_kstate) states[2]; 984 }; 985 986 /* Keep synced with struct pf_kstate. */ 987 struct pf_state_cmp { 988 u_int64_t id; 989 u_int32_t creatorid; 990 u_int8_t direction; 991 u_int8_t pad[3]; 992 }; 993 994 struct pf_state_scrub_export { 995 uint16_t pfss_flags; 996 uint8_t pfss_ttl; /* stashed TTL */ 997 #define PF_SCRUB_FLAG_VALID 0x01 998 uint8_t scrub_flag; 999 uint32_t pfss_ts_mod; /* timestamp modulation */ 1000 }; 1001 1002 struct pf_state_key_export { 1003 struct pf_addr addr[2]; 1004 uint16_t port[2]; 1005 }; 1006 1007 struct pf_state_peer_export { 1008 struct pf_state_scrub_export scrub; /* state is scrubbed */ 1009 uint32_t seqlo; /* Max sequence number sent */ 1010 uint32_t seqhi; /* Max the other end ACKd + win */ 1011 uint32_t seqdiff; /* Sequence number modulator */ 1012 uint16_t max_win; /* largest window (pre scaling) */ 1013 uint16_t mss; /* Maximum segment size option */ 1014 uint8_t state; /* active state level */ 1015 uint8_t wscale; /* window scaling factor */ 1016 uint8_t dummy[6]; 1017 }; 1018 _Static_assert(sizeof(struct pf_state_peer_export) == 32, "size incorrect"); 1019 1020 struct pf_state_export { 1021 uint64_t version; 1022 #define PF_STATE_VERSION 20230404 1023 uint64_t id; 1024 char ifname[IFNAMSIZ]; 1025 char orig_ifname[IFNAMSIZ]; 1026 struct pf_state_key_export key[2]; 1027 struct pf_state_peer_export src; 1028 struct pf_state_peer_export dst; 1029 struct pf_addr rt_addr; 1030 uint32_t rule; 1031 uint32_t anchor; 1032 uint32_t nat_rule; 1033 uint32_t creation; 1034 uint32_t expire; 1035 uint32_t spare0; 1036 uint64_t packets[2]; 1037 uint64_t bytes[2]; 1038 uint32_t creatorid; 1039 uint32_t spare1; 1040 sa_family_t af; 1041 uint8_t proto; 1042 uint8_t direction; 1043 uint8_t log; 1044 uint8_t state_flags_compat; 1045 uint8_t timeout; 1046 uint8_t sync_flags; 1047 uint8_t updates; 1048 uint16_t state_flags; 1049 uint16_t qid; 1050 uint16_t pqid; 1051 uint16_t dnpipe; 1052 uint16_t dnrpipe; 1053 int32_t rtableid; 1054 uint8_t min_ttl; 1055 uint8_t set_tos; 1056 uint16_t max_mss; 1057 uint8_t set_prio[2]; 1058 uint8_t rt; 1059 char rt_ifname[IFNAMSIZ]; 1060 1061 uint8_t spare[72]; 1062 }; 1063 _Static_assert(sizeof(struct pf_state_export) == 384, "size incorrect"); 1064 1065 #ifdef _KERNEL 1066 struct pf_kstate { 1067 /* 1068 * Area shared with pf_state_cmp 1069 */ 1070 u_int64_t id; 1071 u_int32_t creatorid; 1072 u_int8_t direction; 1073 u_int8_t pad[3]; 1074 /* 1075 * end of the area 1076 */ 1077 1078 u_int16_t state_flags; 1079 u_int8_t timeout; 1080 u_int8_t sync_state; /* PFSYNC_S_x */ 1081 u_int8_t sync_updates; /* XXX */ 1082 u_int refs; 1083 struct mtx *lock; 1084 TAILQ_ENTRY(pf_kstate) sync_list; 1085 TAILQ_ENTRY(pf_kstate) key_list[2]; 1086 LIST_ENTRY(pf_kstate) entry; 1087 struct pf_state_peer src; 1088 struct pf_state_peer dst; 1089 struct pf_krule_slist match_rules; 1090 union pf_krule_ptr rule; 1091 union pf_krule_ptr anchor; 1092 union pf_krule_ptr nat_rule; 1093 struct pf_addr rt_addr; 1094 struct pf_state_key *key[2]; /* addresses stack and wire */ 1095 struct pf_udp_mapping *udp_mapping; 1096 struct pfi_kkif *kif; 1097 struct pfi_kkif *orig_kif; /* The real kif, even if we're a floating state (i.e. if == V_pfi_all). */ 1098 struct pfi_kkif *rt_kif; 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_int8_t rt; 1109 u_int16_t if_index_in; 1110 u_int16_t if_index_out; 1111 }; 1112 1113 /* 1114 * Size <= fits 11 objects per page on LP64. Try to not grow the struct beyond that. 1115 */ 1116 _Static_assert(sizeof(struct pf_kstate) <= 372, "pf_kstate size crosses 372 bytes"); 1117 #endif 1118 1119 /* 1120 * Unified state structures for pulling states out of the kernel 1121 * used by pfsync(4) and the pf(4) ioctl. 1122 */ 1123 struct pfsync_state_scrub { 1124 u_int16_t pfss_flags; 1125 u_int8_t pfss_ttl; /* stashed TTL */ 1126 #define PFSYNC_SCRUB_FLAG_VALID 0x01 1127 u_int8_t scrub_flag; 1128 u_int32_t pfss_ts_mod; /* timestamp modulation */ 1129 } __packed; 1130 1131 struct pfsync_state_peer { 1132 struct pfsync_state_scrub scrub; /* state is scrubbed */ 1133 u_int32_t seqlo; /* Max sequence number sent */ 1134 u_int32_t seqhi; /* Max the other end ACKd + win */ 1135 u_int32_t seqdiff; /* Sequence number modulator */ 1136 u_int16_t max_win; /* largest window (pre scaling) */ 1137 u_int16_t mss; /* Maximum segment size option */ 1138 u_int8_t state; /* active state level */ 1139 u_int8_t wscale; /* window scaling factor */ 1140 u_int8_t pad[6]; 1141 } __packed; 1142 1143 struct pfsync_state_key { 1144 struct pf_addr addr[2]; 1145 u_int16_t port[2]; 1146 }; 1147 1148 struct pfsync_state_1301 { 1149 u_int64_t id; 1150 char ifname[IFNAMSIZ]; 1151 struct pfsync_state_key key[2]; 1152 struct pfsync_state_peer src; 1153 struct pfsync_state_peer dst; 1154 struct pf_addr rt_addr; 1155 u_int32_t rule; 1156 u_int32_t anchor; 1157 u_int32_t nat_rule; 1158 u_int32_t creation; 1159 u_int32_t expire; 1160 u_int32_t packets[2][2]; 1161 u_int32_t bytes[2][2]; 1162 u_int32_t creatorid; 1163 sa_family_t af; 1164 u_int8_t proto; 1165 u_int8_t direction; 1166 u_int8_t __spare[2]; 1167 u_int8_t log; 1168 u_int8_t state_flags; 1169 u_int8_t timeout; 1170 u_int8_t sync_flags; 1171 u_int8_t updates; 1172 } __packed; 1173 1174 struct pfsync_state_1400 { 1175 /* The beginning of the struct is compatible with previous versions */ 1176 u_int64_t id; 1177 char ifname[IFNAMSIZ]; 1178 struct pfsync_state_key key[2]; 1179 struct pfsync_state_peer src; 1180 struct pfsync_state_peer dst; 1181 struct pf_addr rt_addr; 1182 u_int32_t rule; 1183 u_int32_t anchor; 1184 u_int32_t nat_rule; 1185 u_int32_t creation; 1186 u_int32_t expire; 1187 u_int32_t packets[2][2]; 1188 u_int32_t bytes[2][2]; 1189 u_int32_t creatorid; 1190 sa_family_t af; 1191 u_int8_t proto; 1192 u_int8_t direction; 1193 u_int16_t state_flags; 1194 u_int8_t log; 1195 u_int8_t __spare; 1196 u_int8_t timeout; 1197 u_int8_t sync_flags; 1198 u_int8_t updates; 1199 /* The rest is not */ 1200 u_int16_t qid; 1201 u_int16_t pqid; 1202 u_int16_t dnpipe; 1203 u_int16_t dnrpipe; 1204 int32_t rtableid; 1205 u_int8_t min_ttl; 1206 u_int8_t set_tos; 1207 u_int16_t max_mss; 1208 u_int8_t set_prio[2]; 1209 u_int8_t rt; 1210 char rt_ifname[IFNAMSIZ]; 1211 1212 } __packed; 1213 1214 union pfsync_state_union { 1215 struct pfsync_state_1301 pfs_1301; 1216 struct pfsync_state_1400 pfs_1400; 1217 } __packed; 1218 1219 #ifdef _KERNEL 1220 /* pfsync */ 1221 typedef int pfsync_state_import_t(union pfsync_state_union *, int, int); 1222 typedef void pfsync_insert_state_t(struct pf_kstate *); 1223 typedef void pfsync_update_state_t(struct pf_kstate *); 1224 typedef void pfsync_delete_state_t(struct pf_kstate *); 1225 typedef void pfsync_clear_states_t(u_int32_t, const char *); 1226 typedef int pfsync_defer_t(struct pf_kstate *, struct mbuf *); 1227 typedef void pfsync_detach_ifnet_t(struct ifnet *); 1228 typedef void pflow_export_state_t(const struct pf_kstate *); 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(struct pfi_kkif *, struct mbuf *, sa_family_t, 1255 uint8_t, u_int8_t, 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 pf_krule *nat_rule; /* nat/rdr rule applied to packet */ 1603 struct pf_addr *src; /* src address */ 1604 struct pf_addr *dst; /* dst address */ 1605 u_int16_t *sport; 1606 u_int16_t *dport; 1607 struct pf_mtag *pf_mtag; 1608 struct pf_rule_actions act; 1609 1610 u_int32_t p_len; /* total length of payload */ 1611 1612 u_int16_t *ip_sum; 1613 u_int16_t *proto_sum; 1614 u_int16_t flags; /* Let SCRUB trigger behavior in 1615 * state code. Easier than tags */ 1616 #define PFDESC_TCP_NORM 0x0001 /* TCP shall be statefully scrubbed */ 1617 #define PFDESC_IP_REAS 0x0002 /* IP frags would've been reassembled */ 1618 sa_family_t af; 1619 u_int8_t proto; 1620 u_int8_t tos; 1621 u_int8_t dir; /* direction */ 1622 u_int8_t sidx; /* key index for source */ 1623 u_int8_t didx; /* key index for destination */ 1624 #define PFDESC_SCTP_INIT 0x0001 1625 #define PFDESC_SCTP_INIT_ACK 0x0002 1626 #define PFDESC_SCTP_COOKIE 0x0004 1627 #define PFDESC_SCTP_COOKIE_ACK 0x0008 1628 #define PFDESC_SCTP_ABORT 0x0010 1629 #define PFDESC_SCTP_SHUTDOWN 0x0020 1630 #define PFDESC_SCTP_SHUTDOWN_COMPLETE 0x0040 1631 #define PFDESC_SCTP_DATA 0x0080 1632 #define PFDESC_SCTP_ASCONF 0x0100 1633 #define PFDESC_SCTP_HEARTBEAT 0x0200 1634 #define PFDESC_SCTP_HEARTBEAT_ACK 0x0400 1635 #define PFDESC_SCTP_OTHER 0x0800 1636 #define PFDESC_SCTP_ADD_IP 0x1000 1637 u_int16_t sctp_flags; 1638 u_int32_t sctp_initiate_tag; 1639 1640 struct pf_sctp_multihome_jobs sctp_multihome_jobs; 1641 }; 1642 1643 struct pf_sctp_multihome_job { 1644 TAILQ_ENTRY(pf_sctp_multihome_job) next; 1645 struct pf_pdesc pd; 1646 struct pf_addr src; 1647 struct pf_addr dst; 1648 struct mbuf *m; 1649 int op; 1650 }; 1651 1652 #endif 1653 1654 /* flags for RDR options */ 1655 #define PF_DPORT_RANGE 0x01 /* Dest port uses range */ 1656 #define PF_RPORT_RANGE 0x02 /* RDR'ed port uses range */ 1657 1658 /* UDP state enumeration */ 1659 #define PFUDPS_NO_TRAFFIC 0 1660 #define PFUDPS_SINGLE 1 1661 #define PFUDPS_MULTIPLE 2 1662 1663 #define PFUDPS_NSTATES 3 /* number of state levels */ 1664 1665 #define PFUDPS_NAMES { \ 1666 "NO_TRAFFIC", \ 1667 "SINGLE", \ 1668 "MULTIPLE", \ 1669 NULL \ 1670 } 1671 1672 /* Other protocol state enumeration */ 1673 #define PFOTHERS_NO_TRAFFIC 0 1674 #define PFOTHERS_SINGLE 1 1675 #define PFOTHERS_MULTIPLE 2 1676 1677 #define PFOTHERS_NSTATES 3 /* number of state levels */ 1678 1679 #define PFOTHERS_NAMES { \ 1680 "NO_TRAFFIC", \ 1681 "SINGLE", \ 1682 "MULTIPLE", \ 1683 NULL \ 1684 } 1685 1686 #define ACTION_SET(a, x) \ 1687 do { \ 1688 if ((a) != NULL) \ 1689 *(a) = (x); \ 1690 } while (0) 1691 1692 #define REASON_SET(a, x) \ 1693 do { \ 1694 if ((a) != NULL) \ 1695 *(a) = (x); \ 1696 if (x < PFRES_MAX) \ 1697 counter_u64_add(V_pf_status.counters[x], 1); \ 1698 } while (0) 1699 1700 enum pf_syncookies_mode { 1701 PF_SYNCOOKIES_NEVER = 0, 1702 PF_SYNCOOKIES_ALWAYS = 1, 1703 PF_SYNCOOKIES_ADAPTIVE = 2, 1704 PF_SYNCOOKIES_MODE_MAX = PF_SYNCOOKIES_ADAPTIVE 1705 }; 1706 1707 #define PF_SYNCOOKIES_HIWATPCT 25 1708 #define PF_SYNCOOKIES_LOWATPCT (PF_SYNCOOKIES_HIWATPCT / 2) 1709 1710 #ifdef _KERNEL 1711 struct pf_kstatus { 1712 counter_u64_t counters[PFRES_MAX]; /* reason for passing/dropping */ 1713 counter_u64_t lcounters[KLCNT_MAX]; /* limit counters */ 1714 struct pf_counter_u64 fcounters[FCNT_MAX]; /* state operation counters */ 1715 counter_u64_t scounters[SCNT_MAX]; /* src_node operation counters */ 1716 uint32_t states; 1717 uint32_t src_nodes; 1718 uint32_t running; 1719 uint32_t since; 1720 uint32_t debug; 1721 uint32_t hostid; 1722 char ifname[IFNAMSIZ]; 1723 uint8_t pf_chksum[PF_MD5_DIGEST_LENGTH]; 1724 bool keep_counters; 1725 enum pf_syncookies_mode syncookies_mode; 1726 bool syncookies_active; 1727 uint64_t syncookies_inflight[2]; 1728 uint32_t states_halfopen; 1729 uint32_t reass; 1730 }; 1731 #endif 1732 1733 struct pf_divert { 1734 union { 1735 struct in_addr ipv4; 1736 struct in6_addr ipv6; 1737 } addr; 1738 u_int16_t port; 1739 }; 1740 1741 #define PFFRAG_FRENT_HIWAT 5000 /* Number of fragment entries */ 1742 #define PFR_KENTRY_HIWAT 200000 /* Number of table entries */ 1743 1744 /* 1745 * Limit the length of the fragment queue traversal. Remember 1746 * search entry points based on the fragment offset. 1747 */ 1748 #define PF_FRAG_ENTRY_POINTS 16 1749 1750 /* 1751 * The number of entries in the fragment queue must be limited 1752 * to avoid DoS by linear searching. Instead of a global limit, 1753 * use a limit per entry point. For large packets these sum up. 1754 */ 1755 #define PF_FRAG_ENTRY_LIMIT 64 1756 1757 /* 1758 * ioctl parameter structures 1759 */ 1760 1761 struct pfioc_pooladdr { 1762 u_int32_t action; 1763 u_int32_t ticket; 1764 u_int32_t nr; 1765 u_int32_t r_num; 1766 u_int8_t r_action; 1767 u_int8_t r_last; 1768 u_int8_t af; 1769 char anchor[MAXPATHLEN]; 1770 struct pf_pooladdr addr; 1771 }; 1772 1773 struct pfioc_rule { 1774 u_int32_t action; 1775 u_int32_t ticket; 1776 u_int32_t pool_ticket; 1777 u_int32_t nr; 1778 char anchor[MAXPATHLEN]; 1779 char anchor_call[MAXPATHLEN]; 1780 struct pf_rule rule; 1781 }; 1782 1783 struct pfioc_natlook { 1784 struct pf_addr saddr; 1785 struct pf_addr daddr; 1786 struct pf_addr rsaddr; 1787 struct pf_addr rdaddr; 1788 u_int16_t sport; 1789 u_int16_t dport; 1790 u_int16_t rsport; 1791 u_int16_t rdport; 1792 sa_family_t af; 1793 u_int8_t proto; 1794 u_int8_t direction; 1795 }; 1796 1797 struct pfioc_state { 1798 struct pfsync_state_1301 state; 1799 }; 1800 1801 struct pfioc_src_node_kill { 1802 sa_family_t psnk_af; 1803 struct pf_rule_addr psnk_src; 1804 struct pf_rule_addr psnk_dst; 1805 u_int psnk_killed; 1806 }; 1807 1808 #ifdef _KERNEL 1809 struct pf_kstate_kill { 1810 struct pf_state_cmp psk_pfcmp; 1811 sa_family_t psk_af; 1812 int psk_proto; 1813 struct pf_rule_addr psk_src; 1814 struct pf_rule_addr psk_dst; 1815 struct pf_rule_addr psk_rt_addr; 1816 char psk_ifname[IFNAMSIZ]; 1817 char psk_label[PF_RULE_LABEL_SIZE]; 1818 u_int psk_killed; 1819 bool psk_kill_match; 1820 bool psk_nat; 1821 }; 1822 #endif 1823 1824 struct pfioc_state_kill { 1825 struct pf_state_cmp psk_pfcmp; 1826 sa_family_t psk_af; 1827 int psk_proto; 1828 struct pf_rule_addr psk_src; 1829 struct pf_rule_addr psk_dst; 1830 char psk_ifname[IFNAMSIZ]; 1831 char psk_label[PF_RULE_LABEL_SIZE]; 1832 u_int psk_killed; 1833 }; 1834 1835 struct pfioc_states { 1836 int ps_len; 1837 union { 1838 void *ps_buf; 1839 struct pfsync_state_1301 *ps_states; 1840 }; 1841 }; 1842 1843 struct pfioc_states_v2 { 1844 int ps_len; 1845 uint64_t ps_req_version; 1846 union { 1847 void *ps_buf; 1848 struct pf_state_export *ps_states; 1849 }; 1850 }; 1851 1852 struct pfioc_src_nodes { 1853 int psn_len; 1854 union { 1855 void *psn_buf; 1856 struct pf_src_node *psn_src_nodes; 1857 }; 1858 }; 1859 1860 struct pfioc_if { 1861 char ifname[IFNAMSIZ]; 1862 }; 1863 1864 struct pfioc_tm { 1865 int timeout; 1866 int seconds; 1867 }; 1868 1869 struct pfioc_limit { 1870 int index; 1871 unsigned limit; 1872 }; 1873 1874 struct pfioc_altq_v0 { 1875 u_int32_t action; 1876 u_int32_t ticket; 1877 u_int32_t nr; 1878 struct pf_altq_v0 altq; 1879 }; 1880 1881 struct pfioc_altq_v1 { 1882 u_int32_t action; 1883 u_int32_t ticket; 1884 u_int32_t nr; 1885 /* 1886 * Placed here so code that only uses the above parameters can be 1887 * written entirely in terms of the v0 or v1 type. 1888 */ 1889 u_int32_t version; 1890 struct pf_altq_v1 altq; 1891 }; 1892 1893 /* 1894 * Latest version of struct pfioc_altq_vX. This must move in lock-step with 1895 * the latest version of struct pf_altq_vX as it has that struct as a 1896 * member. 1897 */ 1898 #define PFIOC_ALTQ_VERSION PF_ALTQ_VERSION 1899 1900 struct pfioc_qstats_v0 { 1901 u_int32_t ticket; 1902 u_int32_t nr; 1903 void *buf; 1904 int nbytes; 1905 u_int8_t scheduler; 1906 }; 1907 1908 struct pfioc_qstats_v1 { 1909 u_int32_t ticket; 1910 u_int32_t nr; 1911 void *buf; 1912 int nbytes; 1913 u_int8_t scheduler; 1914 /* 1915 * Placed here so code that only uses the above parameters can be 1916 * written entirely in terms of the v0 or v1 type. 1917 */ 1918 u_int32_t version; /* Requested version of stats struct */ 1919 }; 1920 1921 /* Latest version of struct pfioc_qstats_vX */ 1922 #define PFIOC_QSTATS_VERSION 1 1923 1924 struct pfioc_ruleset { 1925 u_int32_t nr; 1926 char path[MAXPATHLEN]; 1927 char name[PF_ANCHOR_NAME_SIZE]; 1928 }; 1929 1930 #define PF_RULESET_ALTQ (PF_RULESET_MAX) 1931 #define PF_RULESET_TABLE (PF_RULESET_MAX+1) 1932 #define PF_RULESET_ETH (PF_RULESET_MAX+2) 1933 struct pfioc_trans { 1934 int size; /* number of elements */ 1935 int esize; /* size of each element in bytes */ 1936 struct pfioc_trans_e { 1937 int rs_num; 1938 char anchor[MAXPATHLEN]; 1939 u_int32_t ticket; 1940 } *array; 1941 }; 1942 1943 #define PFR_FLAG_ATOMIC 0x00000001 /* unused */ 1944 #define PFR_FLAG_DUMMY 0x00000002 1945 #define PFR_FLAG_FEEDBACK 0x00000004 1946 #define PFR_FLAG_CLSTATS 0x00000008 1947 #define PFR_FLAG_ADDRSTOO 0x00000010 1948 #define PFR_FLAG_REPLACE 0x00000020 1949 #define PFR_FLAG_ALLRSETS 0x00000040 1950 #define PFR_FLAG_ALLMASK 0x0000007F 1951 #ifdef _KERNEL 1952 #define PFR_FLAG_USERIOCTL 0x10000000 1953 #endif 1954 1955 struct pfioc_table { 1956 struct pfr_table pfrio_table; 1957 void *pfrio_buffer; 1958 int pfrio_esize; 1959 int pfrio_size; 1960 int pfrio_size2; 1961 int pfrio_nadd; 1962 int pfrio_ndel; 1963 int pfrio_nchange; 1964 int pfrio_flags; 1965 u_int32_t pfrio_ticket; 1966 }; 1967 #define pfrio_exists pfrio_nadd 1968 #define pfrio_nzero pfrio_nadd 1969 #define pfrio_nmatch pfrio_nadd 1970 #define pfrio_naddr pfrio_size2 1971 #define pfrio_setflag pfrio_size2 1972 #define pfrio_clrflag pfrio_nadd 1973 1974 struct pfioc_iface { 1975 char pfiio_name[IFNAMSIZ]; 1976 void *pfiio_buffer; 1977 int pfiio_esize; 1978 int pfiio_size; 1979 int pfiio_nzero; 1980 int pfiio_flags; 1981 }; 1982 1983 /* 1984 * ioctl operations 1985 */ 1986 1987 #define DIOCSTART _IO ('D', 1) 1988 #define DIOCSTOP _IO ('D', 2) 1989 #define DIOCADDRULE _IOWR('D', 4, struct pfioc_rule) 1990 #define DIOCADDRULENV _IOWR('D', 4, struct pfioc_nv) 1991 #define DIOCGETRULES _IOWR('D', 6, struct pfioc_rule) 1992 #define DIOCGETRULENV _IOWR('D', 7, struct pfioc_nv) 1993 /* XXX cut 8 - 17 */ 1994 #define DIOCCLRSTATESNV _IOWR('D', 18, struct pfioc_nv) 1995 #define DIOCGETSTATE _IOWR('D', 19, struct pfioc_state) 1996 #define DIOCGETSTATENV _IOWR('D', 19, struct pfioc_nv) 1997 #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if) 1998 #define DIOCGETSTATUSNV _IOWR('D', 21, struct pfioc_nv) 1999 #define DIOCCLRSTATUS _IO ('D', 22) 2000 #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook) 2001 #define DIOCSETDEBUG _IOWR('D', 24, u_int32_t) 2002 #ifdef COMPAT_FREEBSD14 2003 #define DIOCGETSTATES _IOWR('D', 25, struct pfioc_states) 2004 #endif 2005 #define DIOCCHANGERULE _IOWR('D', 26, struct pfioc_rule) 2006 /* XXX cut 26 - 28 */ 2007 #define DIOCSETTIMEOUT _IOWR('D', 29, struct pfioc_tm) 2008 #define DIOCGETTIMEOUT _IOWR('D', 30, struct pfioc_tm) 2009 #define DIOCADDSTATE _IOWR('D', 37, struct pfioc_state) 2010 #define DIOCCLRRULECTRS _IO ('D', 38) 2011 #define DIOCGETLIMIT _IOWR('D', 39, struct pfioc_limit) 2012 #define DIOCSETLIMIT _IOWR('D', 40, struct pfioc_limit) 2013 #define DIOCKILLSTATESNV _IOWR('D', 41, struct pfioc_nv) 2014 #define DIOCSTARTALTQ _IO ('D', 42) 2015 #define DIOCSTOPALTQ _IO ('D', 43) 2016 #define DIOCADDALTQV0 _IOWR('D', 45, struct pfioc_altq_v0) 2017 #define DIOCADDALTQV1 _IOWR('D', 45, struct pfioc_altq_v1) 2018 #define DIOCGETALTQSV0 _IOWR('D', 47, struct pfioc_altq_v0) 2019 #define DIOCGETALTQSV1 _IOWR('D', 47, struct pfioc_altq_v1) 2020 #define DIOCGETALTQV0 _IOWR('D', 48, struct pfioc_altq_v0) 2021 #define DIOCGETALTQV1 _IOWR('D', 48, struct pfioc_altq_v1) 2022 #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0) 2023 #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1) 2024 #define DIOCGETQSTATSV0 _IOWR('D', 50, struct pfioc_qstats_v0) 2025 #define DIOCGETQSTATSV1 _IOWR('D', 50, struct pfioc_qstats_v1) 2026 #define DIOCBEGINADDRS _IOWR('D', 51, struct pfioc_pooladdr) 2027 #define DIOCADDADDR _IOWR('D', 52, struct pfioc_pooladdr) 2028 #define DIOCGETADDRS _IOWR('D', 53, struct pfioc_pooladdr) 2029 #define DIOCGETADDR _IOWR('D', 54, struct pfioc_pooladdr) 2030 #define DIOCCHANGEADDR _IOWR('D', 55, struct pfioc_pooladdr) 2031 /* XXX cut 55 - 57 */ 2032 #define DIOCGETRULESETS _IOWR('D', 58, struct pfioc_ruleset) 2033 #define DIOCGETRULESET _IOWR('D', 59, struct pfioc_ruleset) 2034 #define DIOCRCLRTABLES _IOWR('D', 60, struct pfioc_table) 2035 #define DIOCRADDTABLES _IOWR('D', 61, struct pfioc_table) 2036 #define DIOCRDELTABLES _IOWR('D', 62, struct pfioc_table) 2037 #define DIOCRGETTABLES _IOWR('D', 63, struct pfioc_table) 2038 #define DIOCRGETTSTATS _IOWR('D', 64, struct pfioc_table) 2039 #define DIOCRCLRTSTATS _IOWR('D', 65, struct pfioc_table) 2040 #define DIOCRCLRADDRS _IOWR('D', 66, struct pfioc_table) 2041 #define DIOCRADDADDRS _IOWR('D', 67, struct pfioc_table) 2042 #define DIOCRDELADDRS _IOWR('D', 68, struct pfioc_table) 2043 #define DIOCRSETADDRS _IOWR('D', 69, struct pfioc_table) 2044 #define DIOCRGETADDRS _IOWR('D', 70, struct pfioc_table) 2045 #define DIOCRGETASTATS _IOWR('D', 71, struct pfioc_table) 2046 #define DIOCRCLRASTATS _IOWR('D', 72, struct pfioc_table) 2047 #define DIOCRTSTADDRS _IOWR('D', 73, struct pfioc_table) 2048 #define DIOCRSETTFLAGS _IOWR('D', 74, struct pfioc_table) 2049 #define DIOCRINADEFINE _IOWR('D', 77, struct pfioc_table) 2050 #define DIOCOSFPFLUSH _IO('D', 78) 2051 #define DIOCOSFPADD _IOWR('D', 79, struct pf_osfp_ioctl) 2052 #define DIOCOSFPGET _IOWR('D', 80, struct pf_osfp_ioctl) 2053 #define DIOCXBEGIN _IOWR('D', 81, struct pfioc_trans) 2054 #define DIOCXCOMMIT _IOWR('D', 82, struct pfioc_trans) 2055 #define DIOCXROLLBACK _IOWR('D', 83, struct pfioc_trans) 2056 #define DIOCGETSRCNODES _IOWR('D', 84, struct pfioc_src_nodes) 2057 #define DIOCCLRSRCNODES _IO('D', 85) 2058 #define DIOCSETHOSTID _IOWR('D', 86, u_int32_t) 2059 #define DIOCIGETIFACES _IOWR('D', 87, struct pfioc_iface) 2060 #define DIOCSETIFFLAG _IOWR('D', 89, struct pfioc_iface) 2061 #define DIOCCLRIFFLAG _IOWR('D', 90, struct pfioc_iface) 2062 #define DIOCKILLSRCNODES _IOWR('D', 91, struct pfioc_src_node_kill) 2063 #define DIOCGIFSPEEDV0 _IOWR('D', 92, struct pf_ifspeed_v0) 2064 #define DIOCGIFSPEEDV1 _IOWR('D', 92, struct pf_ifspeed_v1) 2065 #ifdef COMPAT_FREEBSD14 2066 #define DIOCGETSTATESV2 _IOWR('D', 93, struct pfioc_states_v2) 2067 #endif 2068 #define DIOCGETSYNCOOKIES _IOWR('D', 94, struct pfioc_nv) 2069 #define DIOCSETSYNCOOKIES _IOWR('D', 95, struct pfioc_nv) 2070 #define DIOCKEEPCOUNTERS _IOWR('D', 96, struct pfioc_nv) 2071 #define DIOCKEEPCOUNTERS_FREEBSD13 _IOWR('D', 92, struct pfioc_nv) 2072 #define DIOCADDETHRULE _IOWR('D', 97, struct pfioc_nv) 2073 #define DIOCGETETHRULE _IOWR('D', 98, struct pfioc_nv) 2074 #define DIOCGETETHRULES _IOWR('D', 99, struct pfioc_nv) 2075 #define DIOCGETETHRULESETS _IOWR('D', 100, struct pfioc_nv) 2076 #define DIOCGETETHRULESET _IOWR('D', 101, struct pfioc_nv) 2077 #define DIOCSETREASS _IOWR('D', 102, u_int32_t) 2078 2079 struct pf_ifspeed_v0 { 2080 char ifname[IFNAMSIZ]; 2081 u_int32_t baudrate; 2082 }; 2083 2084 struct pf_ifspeed_v1 { 2085 char ifname[IFNAMSIZ]; 2086 u_int32_t baudrate32; 2087 /* layout identical to struct pf_ifspeed_v0 up to this point */ 2088 u_int64_t baudrate; 2089 }; 2090 2091 /* Latest version of struct pf_ifspeed_vX */ 2092 #define PF_IFSPEED_VERSION 1 2093 2094 /* 2095 * Compatibility and convenience macros 2096 */ 2097 #ifndef _KERNEL 2098 #ifdef PFIOC_USE_LATEST 2099 /* 2100 * Maintaining in-tree consumers of the ioctl interface is easier when that 2101 * code can be written in terms old names that refer to the latest interface 2102 * version as that reduces the required changes in the consumers to those 2103 * that are functionally necessary to accommodate a new interface version. 2104 */ 2105 #define pfioc_altq __CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION) 2106 #define pfioc_qstats __CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION) 2107 #define pf_ifspeed __CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION) 2108 2109 #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION) 2110 #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION) 2111 #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION) 2112 #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION) 2113 #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION) 2114 #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION) 2115 #else 2116 /* 2117 * When building out-of-tree code that is written for the old interface, 2118 * such as may exist in ports for example, resolve the old struct tags and 2119 * ioctl command names to the v0 versions. 2120 */ 2121 #define pfioc_altq __CONCAT(pfioc_altq_v, 0) 2122 #define pfioc_qstats __CONCAT(pfioc_qstats_v, 0) 2123 #define pf_ifspeed __CONCAT(pf_ifspeed_v, 0) 2124 2125 #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, 0) 2126 #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, 0) 2127 #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, 0) 2128 #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, 0) 2129 #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, 0) 2130 #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, 0) 2131 #endif /* PFIOC_USE_LATEST */ 2132 #endif /* _KERNEL */ 2133 2134 #ifdef _KERNEL 2135 LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node); 2136 struct pf_srchash { 2137 struct pf_ksrc_node_list nodes; 2138 struct mtx lock; 2139 }; 2140 2141 struct pf_keyhash { 2142 LIST_HEAD(, pf_state_key) keys; 2143 struct mtx lock; 2144 }; 2145 2146 struct pf_idhash { 2147 LIST_HEAD(, pf_kstate) states; 2148 struct mtx lock; 2149 }; 2150 2151 struct pf_udpendpointhash { 2152 LIST_HEAD(, pf_udp_endpoint) endpoints; 2153 /* refcont is synchronized on the source endpoint's row lock */ 2154 struct mtx lock; 2155 }; 2156 2157 extern u_long pf_ioctl_maxcount; 2158 VNET_DECLARE(u_long, pf_hashmask); 2159 #define V_pf_hashmask VNET(pf_hashmask) 2160 VNET_DECLARE(u_long, pf_srchashmask); 2161 #define V_pf_srchashmask VNET(pf_srchashmask) 2162 VNET_DECLARE(u_long, pf_udpendpointhashmask); 2163 #define V_pf_udpendpointhashmask VNET(pf_udpendpointhashmask) 2164 #define PF_HASHSIZ (131072) 2165 #define PF_SRCHASHSIZ (PF_HASHSIZ/4) 2166 #define PF_UDPENDHASHSIZ (PF_HASHSIZ/4) 2167 VNET_DECLARE(struct pf_keyhash *, pf_keyhash); 2168 VNET_DECLARE(struct pf_idhash *, pf_idhash); 2169 VNET_DECLARE(struct pf_udpendpointhash *, pf_udpendpointhash); 2170 #define V_pf_keyhash VNET(pf_keyhash) 2171 #define V_pf_idhash VNET(pf_idhash) 2172 #define V_pf_udpendpointhash VNET(pf_udpendpointhash) 2173 VNET_DECLARE(struct pf_srchash *, pf_srchash); 2174 #define V_pf_srchash VNET(pf_srchash) 2175 2176 #define PF_IDHASH(s) (be64toh((s)->id) % (V_pf_hashmask + 1)) 2177 2178 VNET_DECLARE(void *, pf_swi_cookie); 2179 #define V_pf_swi_cookie VNET(pf_swi_cookie) 2180 VNET_DECLARE(struct intr_event *, pf_swi_ie); 2181 #define V_pf_swi_ie VNET(pf_swi_ie) 2182 2183 VNET_DECLARE(struct unrhdr64, pf_stateid); 2184 #define V_pf_stateid VNET(pf_stateid) 2185 2186 TAILQ_HEAD(pf_altqqueue, pf_altq); 2187 VNET_DECLARE(struct pf_altqqueue, pf_altqs[4]); 2188 #define V_pf_altqs VNET(pf_altqs) 2189 VNET_DECLARE(struct pf_kpalist, pf_pabuf); 2190 #define V_pf_pabuf VNET(pf_pabuf) 2191 2192 VNET_DECLARE(u_int32_t, ticket_altqs_active); 2193 #define V_ticket_altqs_active VNET(ticket_altqs_active) 2194 VNET_DECLARE(u_int32_t, ticket_altqs_inactive); 2195 #define V_ticket_altqs_inactive VNET(ticket_altqs_inactive) 2196 VNET_DECLARE(int, altqs_inactive_open); 2197 #define V_altqs_inactive_open VNET(altqs_inactive_open) 2198 VNET_DECLARE(u_int32_t, ticket_pabuf); 2199 #define V_ticket_pabuf VNET(ticket_pabuf) 2200 VNET_DECLARE(struct pf_altqqueue *, pf_altqs_active); 2201 #define V_pf_altqs_active VNET(pf_altqs_active) 2202 VNET_DECLARE(struct pf_altqqueue *, pf_altq_ifs_active); 2203 #define V_pf_altq_ifs_active VNET(pf_altq_ifs_active) 2204 VNET_DECLARE(struct pf_altqqueue *, pf_altqs_inactive); 2205 #define V_pf_altqs_inactive VNET(pf_altqs_inactive) 2206 VNET_DECLARE(struct pf_altqqueue *, pf_altq_ifs_inactive); 2207 #define V_pf_altq_ifs_inactive VNET(pf_altq_ifs_inactive) 2208 2209 VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules); 2210 #define V_pf_unlinked_rules VNET(pf_unlinked_rules) 2211 2212 #ifdef PF_WANT_32_TO_64_COUNTER 2213 LIST_HEAD(allkiflist_head, pfi_kkif); 2214 VNET_DECLARE(struct allkiflist_head, pf_allkiflist); 2215 #define V_pf_allkiflist VNET(pf_allkiflist) 2216 VNET_DECLARE(size_t, pf_allkifcount); 2217 #define V_pf_allkifcount VNET(pf_allkifcount) 2218 VNET_DECLARE(struct pfi_kkif *, pf_kifmarker); 2219 #define V_pf_kifmarker VNET(pf_kifmarker) 2220 2221 LIST_HEAD(allrulelist_head, pf_krule); 2222 VNET_DECLARE(struct allrulelist_head, pf_allrulelist); 2223 #define V_pf_allrulelist VNET(pf_allrulelist) 2224 VNET_DECLARE(size_t, pf_allrulecount); 2225 #define V_pf_allrulecount VNET(pf_allrulecount) 2226 VNET_DECLARE(struct pf_krule *, pf_rulemarker); 2227 #define V_pf_rulemarker VNET(pf_rulemarker) 2228 #endif 2229 2230 int pf_start(void); 2231 int pf_stop(void); 2232 void pf_initialize(void); 2233 void pf_mtag_initialize(void); 2234 void pf_mtag_cleanup(void); 2235 void pf_cleanup(void); 2236 2237 struct pf_mtag *pf_get_mtag(struct mbuf *); 2238 2239 extern void pf_calc_skip_steps(struct pf_krulequeue *); 2240 #ifdef ALTQ 2241 extern void pf_altq_ifnet_event(struct ifnet *, int); 2242 #endif 2243 VNET_DECLARE(uma_zone_t, pf_state_z); 2244 #define V_pf_state_z VNET(pf_state_z) 2245 VNET_DECLARE(uma_zone_t, pf_state_key_z); 2246 #define V_pf_state_key_z VNET(pf_state_key_z) 2247 VNET_DECLARE(uma_zone_t, pf_udp_mapping_z); 2248 #define V_pf_udp_mapping_z VNET(pf_udp_mapping_z) 2249 VNET_DECLARE(uma_zone_t, pf_state_scrub_z); 2250 #define V_pf_state_scrub_z VNET(pf_state_scrub_z) 2251 2252 extern void pf_purge_thread(void *); 2253 extern void pf_unload_vnet_purge(void); 2254 extern void pf_intr(void *); 2255 extern void pf_purge_expired_src_nodes(void); 2256 2257 extern int pf_unlink_state(struct pf_kstate *); 2258 extern int pf_state_insert(struct pfi_kkif *, 2259 struct pfi_kkif *, 2260 struct pf_state_key *, 2261 struct pf_state_key *, 2262 struct pf_kstate *); 2263 extern struct pf_kstate *pf_alloc_state(int); 2264 extern void pf_free_state(struct pf_kstate *); 2265 extern void pf_killstates(struct pf_kstate_kill *, 2266 unsigned int *); 2267 extern unsigned int pf_clear_states(const struct pf_kstate_kill *); 2268 2269 static __inline void 2270 pf_ref_state(struct pf_kstate *s) 2271 { 2272 2273 refcount_acquire(&s->refs); 2274 } 2275 2276 static __inline int 2277 pf_release_state(struct pf_kstate *s) 2278 { 2279 2280 if (refcount_release(&s->refs)) { 2281 pf_free_state(s); 2282 return (1); 2283 } else 2284 return (0); 2285 } 2286 2287 static __inline int 2288 pf_release_staten(struct pf_kstate *s, u_int n) 2289 { 2290 2291 if (refcount_releasen(&s->refs, n)) { 2292 pf_free_state(s); 2293 return (1); 2294 } else 2295 return (0); 2296 } 2297 2298 static __inline uint64_t 2299 pf_get_uptime(void) 2300 { 2301 struct timeval t; 2302 microuptime(&t); 2303 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)); 2304 } 2305 2306 static __inline uint64_t 2307 pf_get_time(void) 2308 { 2309 struct timeval t; 2310 microtime(&t); 2311 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)); 2312 } 2313 2314 extern struct pf_kstate *pf_find_state_byid(uint64_t, uint32_t); 2315 extern struct pf_kstate *pf_find_state_all( 2316 const struct pf_state_key_cmp *, 2317 u_int, int *); 2318 extern bool pf_find_state_all_exists( 2319 const struct pf_state_key_cmp *, 2320 u_int); 2321 extern struct pf_udp_mapping *pf_udp_mapping_find(struct pf_udp_endpoint_cmp 2322 *endpoint); 2323 extern struct pf_udp_mapping *pf_udp_mapping_create(sa_family_t af, 2324 struct pf_addr *src_addr, uint16_t src_port, 2325 struct pf_addr *nat_addr, uint16_t nat_port); 2326 extern int pf_udp_mapping_insert(struct pf_udp_mapping 2327 *mapping); 2328 extern void pf_udp_mapping_release(struct pf_udp_mapping 2329 *mapping); 2330 extern struct pf_ksrc_node *pf_find_src_node(struct pf_addr *, 2331 struct pf_krule *, sa_family_t, 2332 struct pf_srchash **, bool); 2333 extern void pf_unlink_src_node(struct pf_ksrc_node *); 2334 extern u_int pf_free_src_nodes(struct pf_ksrc_node_list *); 2335 extern void pf_print_state(struct pf_kstate *); 2336 extern void pf_print_flags(u_int8_t); 2337 extern int pf_addr_wrap_neq(struct pf_addr_wrap *, 2338 struct pf_addr_wrap *); 2339 extern u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t, 2340 u_int8_t); 2341 extern u_int16_t pf_proto_cksum_fixup(struct mbuf *, u_int16_t, 2342 u_int16_t, u_int16_t, u_int8_t); 2343 2344 VNET_DECLARE(struct ifnet *, sync_ifp); 2345 #define V_sync_ifp VNET(sync_ifp); 2346 VNET_DECLARE(struct pf_krule, pf_default_rule); 2347 #define V_pf_default_rule VNET(pf_default_rule) 2348 extern void pf_addrcpy(struct pf_addr *, struct pf_addr *, 2349 sa_family_t); 2350 void pf_free_rule(struct pf_krule *); 2351 2352 int pf_test_eth(int, int, struct ifnet *, struct mbuf **, struct inpcb *); 2353 #ifdef INET 2354 int pf_test(int, int, struct ifnet *, struct mbuf **, struct inpcb *, 2355 struct pf_rule_actions *); 2356 int pf_normalize_ip(struct mbuf **, struct pfi_kkif *, u_short *, 2357 struct pf_pdesc *); 2358 #endif /* INET */ 2359 2360 #ifdef INET6 2361 int pf_test6(int, int, struct ifnet *, struct mbuf **, struct inpcb *, 2362 struct pf_rule_actions *); 2363 int pf_normalize_ip6(struct mbuf **, struct pfi_kkif *, u_short *, 2364 struct pf_pdesc *); 2365 void pf_poolmask(struct pf_addr *, struct pf_addr*, 2366 struct pf_addr *, struct pf_addr *, sa_family_t); 2367 void pf_addr_inc(struct pf_addr *, sa_family_t); 2368 int pf_max_frag_size(struct mbuf *); 2369 int pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *, bool); 2370 #endif /* INET6 */ 2371 2372 int pf_multihome_scan_init(struct mbuf *, int, int, struct pf_pdesc *, 2373 struct pfi_kkif *); 2374 int pf_multihome_scan_asconf(struct mbuf *, int, int, struct pf_pdesc *, 2375 struct pfi_kkif *); 2376 2377 u_int32_t pf_new_isn(struct pf_kstate *); 2378 void *pf_pull_hdr(const struct mbuf *, int, void *, int, u_short *, u_short *, 2379 sa_family_t); 2380 void pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t); 2381 void pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t, 2382 u_int8_t); 2383 void pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t); 2384 void pf_patch_16_unaligned(struct mbuf *, u_int16_t *, void *, u_int16_t, 2385 bool, u_int8_t); 2386 void pf_patch_32_unaligned(struct mbuf *, u_int16_t *, void *, u_int32_t, 2387 bool, u_int8_t); 2388 void pf_send_deferred_syn(struct pf_kstate *); 2389 int pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *, 2390 struct pf_addr *, sa_family_t); 2391 int pf_match_addr_range(struct pf_addr *, struct pf_addr *, 2392 struct pf_addr *, sa_family_t); 2393 int pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t); 2394 2395 void pf_normalize_init(void); 2396 void pf_normalize_cleanup(void); 2397 int pf_normalize_tcp(struct pfi_kkif *, struct mbuf *, int, int, void *, 2398 struct pf_pdesc *); 2399 void pf_normalize_tcp_cleanup(struct pf_kstate *); 2400 int pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *, 2401 struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *); 2402 int pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *, 2403 u_short *, struct tcphdr *, struct pf_kstate *, 2404 struct pf_state_peer *, struct pf_state_peer *, int *); 2405 int pf_normalize_sctp_init(struct mbuf *, int, struct pf_pdesc *, 2406 struct pf_state_peer *, struct pf_state_peer *); 2407 int pf_normalize_sctp(int, struct pfi_kkif *, struct mbuf *, int, 2408 int, void *, struct pf_pdesc *); 2409 u_int32_t 2410 pf_state_expires(const struct pf_kstate *); 2411 void pf_purge_expired_fragments(void); 2412 void pf_purge_fragments(uint32_t); 2413 int pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *, 2414 int); 2415 int pf_socket_lookup(struct pf_pdesc *, struct mbuf *); 2416 struct pf_state_key *pf_alloc_state_key(int); 2417 void pfr_initialize(void); 2418 void pfr_cleanup(void); 2419 int pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t); 2420 void pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t, 2421 u_int64_t, int, int, int); 2422 int pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t); 2423 void pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *); 2424 struct pfr_ktable * 2425 pfr_attach_table(struct pf_kruleset *, char *); 2426 struct pfr_ktable * 2427 pfr_eth_attach_table(struct pf_keth_ruleset *, char *); 2428 void pfr_detach_table(struct pfr_ktable *); 2429 int pfr_clr_tables(struct pfr_table *, int *, int); 2430 int pfr_add_tables(struct pfr_table *, int, int *, int); 2431 int pfr_del_tables(struct pfr_table *, int, int *, int); 2432 int pfr_table_count(struct pfr_table *, int); 2433 int pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int); 2434 int pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int); 2435 int pfr_clr_tstats(struct pfr_table *, int, int *, int); 2436 int pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int); 2437 int pfr_clr_addrs(struct pfr_table *, int *, int); 2438 int pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long); 2439 int pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2440 int); 2441 int pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2442 int); 2443 int pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2444 int *, int *, int *, int, u_int32_t); 2445 int pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int); 2446 int pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int); 2447 int pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *, 2448 int); 2449 int pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 2450 int); 2451 int pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int); 2452 int pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int); 2453 int pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int); 2454 int pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *, 2455 int *, u_int32_t, int); 2456 2457 MALLOC_DECLARE(PFI_MTYPE); 2458 VNET_DECLARE(struct pfi_kkif *, pfi_all); 2459 #define V_pfi_all VNET(pfi_all) 2460 2461 void pfi_initialize(void); 2462 void pfi_initialize_vnet(void); 2463 void pfi_cleanup(void); 2464 void pfi_cleanup_vnet(void); 2465 void pfi_kkif_ref(struct pfi_kkif *); 2466 void pfi_kkif_unref(struct pfi_kkif *); 2467 struct pfi_kkif *pfi_kkif_find(const char *); 2468 struct pfi_kkif *pfi_kkif_attach(struct pfi_kkif *, const char *); 2469 int pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *); 2470 void pfi_kkif_purge(void); 2471 int pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *, 2472 sa_family_t); 2473 int pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t); 2474 void pfi_dynaddr_remove(struct pfi_dynaddr *); 2475 void pfi_dynaddr_copyout(struct pf_addr_wrap *); 2476 void pfi_update_status(const char *, struct pf_status *); 2477 void pfi_get_ifaces(const char *, struct pfi_kif *, int *); 2478 int pfi_set_flags(const char *, int); 2479 int pfi_clear_flags(const char *, int); 2480 2481 int pf_match_tag(struct mbuf *, struct pf_krule *, int *, int); 2482 int pf_tag_packet(struct mbuf *, struct pf_pdesc *, int); 2483 int pf_addr_cmp(struct pf_addr *, struct pf_addr *, 2484 sa_family_t); 2485 2486 u_int16_t pf_get_mss(struct mbuf *, int, u_int16_t, sa_family_t); 2487 u_int8_t pf_get_wscale(struct mbuf *, int, u_int16_t, sa_family_t); 2488 struct mbuf *pf_build_tcp(const struct pf_krule *, sa_family_t, 2489 const struct pf_addr *, const struct pf_addr *, 2490 u_int16_t, u_int16_t, u_int32_t, u_int32_t, 2491 u_int8_t, u_int16_t, u_int16_t, u_int8_t, bool, 2492 u_int16_t, u_int16_t, int); 2493 void pf_send_tcp(const struct pf_krule *, sa_family_t, 2494 const struct pf_addr *, const struct pf_addr *, 2495 u_int16_t, u_int16_t, u_int32_t, u_int32_t, 2496 u_int8_t, u_int16_t, u_int16_t, u_int8_t, bool, 2497 u_int16_t, u_int16_t, int); 2498 2499 void pf_syncookies_init(void); 2500 void pf_syncookies_cleanup(void); 2501 int pf_get_syncookies(struct pfioc_nv *); 2502 int pf_set_syncookies(struct pfioc_nv *); 2503 int pf_synflood_check(struct pf_pdesc *); 2504 void pf_syncookie_send(struct mbuf *m, int off, 2505 struct pf_pdesc *); 2506 bool pf_syncookie_check(struct pf_pdesc *); 2507 u_int8_t pf_syncookie_validate(struct pf_pdesc *); 2508 struct mbuf * pf_syncookie_recreate_syn(uint8_t, int, 2509 struct pf_pdesc *); 2510 2511 VNET_DECLARE(struct pf_kstatus, pf_status); 2512 #define V_pf_status VNET(pf_status) 2513 2514 struct pf_limit { 2515 uma_zone_t zone; 2516 u_int limit; 2517 }; 2518 VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); 2519 #define V_pf_limits VNET(pf_limits) 2520 2521 #endif /* _KERNEL */ 2522 2523 #ifdef _KERNEL 2524 VNET_DECLARE(struct pf_kanchor_global, pf_anchors); 2525 #define V_pf_anchors VNET(pf_anchors) 2526 VNET_DECLARE(struct pf_kanchor, pf_main_anchor); 2527 #define V_pf_main_anchor VNET(pf_main_anchor) 2528 VNET_DECLARE(struct pf_keth_anchor_global, pf_keth_anchors); 2529 #define V_pf_keth_anchors VNET(pf_keth_anchors) 2530 #define pf_main_ruleset V_pf_main_anchor.ruleset 2531 2532 VNET_DECLARE(struct pf_keth_anchor, pf_main_keth_anchor); 2533 #define V_pf_main_keth_anchor VNET(pf_main_keth_anchor) 2534 VNET_DECLARE(struct pf_keth_ruleset*, pf_keth); 2535 #define V_pf_keth VNET(pf_keth) 2536 2537 void pf_init_kruleset(struct pf_kruleset *); 2538 void pf_init_keth(struct pf_keth_ruleset *); 2539 int pf_kanchor_setup(struct pf_krule *, 2540 const struct pf_kruleset *, const char *); 2541 int pf_kanchor_copyout(const struct pf_kruleset *, 2542 const struct pf_krule *, char *, size_t); 2543 int pf_kanchor_nvcopyout(const struct pf_kruleset *, 2544 const struct pf_krule *, nvlist_t *); 2545 void pf_kanchor_remove(struct pf_krule *); 2546 void pf_remove_if_empty_kruleset(struct pf_kruleset *); 2547 struct pf_kruleset *pf_find_kruleset(const char *); 2548 struct pf_kruleset *pf_find_or_create_kruleset(const char *); 2549 void pf_rs_initialize(void); 2550 2551 2552 struct pf_krule *pf_krule_alloc(void); 2553 2554 void pf_remove_if_empty_keth_ruleset( 2555 struct pf_keth_ruleset *); 2556 struct pf_keth_ruleset *pf_find_keth_ruleset(const char *); 2557 struct pf_keth_anchor *pf_find_keth_anchor(const char *); 2558 int pf_keth_anchor_setup(struct pf_keth_rule *, 2559 const struct pf_keth_ruleset *, const char *); 2560 int pf_keth_anchor_nvcopyout( 2561 const struct pf_keth_ruleset *, 2562 const struct pf_keth_rule *, nvlist_t *); 2563 struct pf_keth_ruleset *pf_find_or_create_keth_ruleset(const char *); 2564 void pf_keth_anchor_remove(struct pf_keth_rule *); 2565 2566 int pf_ioctl_getrules(struct pfioc_rule *); 2567 int pf_ioctl_addrule(struct pf_krule *, uint32_t, 2568 uint32_t, const char *, const char *, uid_t uid, 2569 pid_t); 2570 void pf_ioctl_clear_status(void); 2571 int pf_ioctl_get_timeout(int, int *); 2572 int pf_ioctl_set_timeout(int, int, int *); 2573 int pf_ioctl_get_limit(int, unsigned int *); 2574 int pf_ioctl_set_limit(int, unsigned int, unsigned int *); 2575 int pf_ioctl_begin_addrs(uint32_t *); 2576 int pf_ioctl_add_addr(struct pfioc_pooladdr *); 2577 int pf_ioctl_get_addrs(struct pfioc_pooladdr *); 2578 int pf_ioctl_get_addr(struct pfioc_pooladdr *); 2579 2580 void pf_krule_free(struct pf_krule *); 2581 void pf_krule_clear_counters(struct pf_krule *); 2582 void pf_addr_copyout(struct pf_addr_wrap *); 2583 #endif 2584 2585 /* The fingerprint functions can be linked into userland programs (tcpdump) */ 2586 int pf_osfp_add(struct pf_osfp_ioctl *); 2587 #ifdef _KERNEL 2588 struct pf_osfp_enlist * 2589 pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int, 2590 const struct tcphdr *); 2591 #endif /* _KERNEL */ 2592 void pf_osfp_flush(void); 2593 int pf_osfp_get(struct pf_osfp_ioctl *); 2594 int pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t); 2595 2596 #ifdef _KERNEL 2597 void pf_print_host(struct pf_addr *, u_int16_t, sa_family_t); 2598 2599 void pf_step_into_anchor(struct pf_kanchor_stackframe *, int *, 2600 struct pf_kruleset **, int, struct pf_krule **, 2601 struct pf_krule **, int *); 2602 int pf_step_out_of_anchor(struct pf_kanchor_stackframe *, int *, 2603 struct pf_kruleset **, int, struct pf_krule **, 2604 struct pf_krule **, int *); 2605 void pf_step_into_keth_anchor(struct pf_keth_anchor_stackframe *, 2606 int *, struct pf_keth_ruleset **, 2607 struct pf_keth_rule **, struct pf_keth_rule **, 2608 int *); 2609 int pf_step_out_of_keth_anchor(struct pf_keth_anchor_stackframe *, 2610 int *, struct pf_keth_ruleset **, 2611 struct pf_keth_rule **, struct pf_keth_rule **, 2612 int *); 2613 2614 u_short pf_map_addr(u_int8_t, struct pf_krule *, 2615 struct pf_addr *, struct pf_addr *, 2616 struct pfi_kkif **nkif, struct pf_addr *, 2617 struct pf_ksrc_node **); 2618 u_short pf_get_translation(struct pf_pdesc *, struct mbuf *, 2619 int, struct pfi_kkif *, struct pf_ksrc_node **, 2620 struct pf_state_key **, struct pf_state_key **, 2621 struct pf_addr *, struct pf_addr *, 2622 uint16_t, uint16_t, struct pf_kanchor_stackframe *, 2623 struct pf_krule **, 2624 struct pf_udp_mapping **udp_mapping); 2625 2626 struct pf_state_key *pf_state_key_setup(struct pf_pdesc *, struct mbuf *, int, 2627 struct pf_addr *, struct pf_addr *, u_int16_t, u_int16_t); 2628 struct pf_state_key *pf_state_key_clone(const struct pf_state_key *); 2629 void pf_rule_to_actions(struct pf_krule *, 2630 struct pf_rule_actions *); 2631 int pf_normalize_mss(struct mbuf *m, int off, 2632 struct pf_pdesc *pd); 2633 #ifdef INET 2634 void pf_scrub_ip(struct mbuf **, struct pf_pdesc *); 2635 #endif /* INET */ 2636 #ifdef INET6 2637 void pf_scrub_ip6(struct mbuf **, struct pf_pdesc *); 2638 #endif /* INET6 */ 2639 2640 struct pfi_kkif *pf_kkif_create(int); 2641 void pf_kkif_free(struct pfi_kkif *); 2642 void pf_kkif_zero(struct pfi_kkif *); 2643 #endif /* _KERNEL */ 2644 2645 #endif /* _NET_PFVAR_H_ */ 2646