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