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