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