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