1fe267a55SPedro F. Giffuni /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause 3fe267a55SPedro F. Giffuni * 43b3a8eb9SGleb Smirnoff * Copyright (c) 2001 Daniel Hartmeier 53b3a8eb9SGleb Smirnoff * All rights reserved. 63b3a8eb9SGleb Smirnoff * 73b3a8eb9SGleb Smirnoff * Redistribution and use in source and binary forms, with or without 83b3a8eb9SGleb Smirnoff * modification, are permitted provided that the following conditions 93b3a8eb9SGleb Smirnoff * are met: 103b3a8eb9SGleb Smirnoff * 113b3a8eb9SGleb Smirnoff * - Redistributions of source code must retain the above copyright 123b3a8eb9SGleb Smirnoff * notice, this list of conditions and the following disclaimer. 133b3a8eb9SGleb Smirnoff * - Redistributions in binary form must reproduce the above 143b3a8eb9SGleb Smirnoff * copyright notice, this list of conditions and the following 153b3a8eb9SGleb Smirnoff * disclaimer in the documentation and/or other materials provided 163b3a8eb9SGleb Smirnoff * with the distribution. 173b3a8eb9SGleb Smirnoff * 183b3a8eb9SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 193b3a8eb9SGleb Smirnoff * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 203b3a8eb9SGleb Smirnoff * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 213b3a8eb9SGleb Smirnoff * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 223b3a8eb9SGleb Smirnoff * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 233b3a8eb9SGleb Smirnoff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 243b3a8eb9SGleb Smirnoff * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 253b3a8eb9SGleb Smirnoff * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 263b3a8eb9SGleb Smirnoff * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 273b3a8eb9SGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 283b3a8eb9SGleb Smirnoff * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 293b3a8eb9SGleb Smirnoff * POSSIBILITY OF SUCH DAMAGE. 303b3a8eb9SGleb Smirnoff * 319ed8bbbdSGleb Smirnoff * $OpenBSD: pfvar.h,v 1.282 2009/01/29 15:12:28 pyr Exp $ 323b3a8eb9SGleb Smirnoff */ 333b3a8eb9SGleb Smirnoff 343b3a8eb9SGleb Smirnoff #ifndef _NET_PFVAR_H_ 353b3a8eb9SGleb Smirnoff #define _NET_PFVAR_H_ 363b3a8eb9SGleb Smirnoff 373b3a8eb9SGleb Smirnoff #include <sys/param.h> 383b3a8eb9SGleb Smirnoff #include <sys/queue.h> 3948278b88SGleb Smirnoff #include <sys/counter.h> 40455969d3SKristof Provost #include <sys/cpuset.h> 4120c4899aSKristof Provost #include <sys/epoch.h> 428ec07310SGleb Smirnoff #include <sys/malloc.h> 43d710367dSKristof Provost #include <sys/nv.h> 443b3a8eb9SGleb Smirnoff #include <sys/refcount.h> 45ec7b47fcSKristof Provost #include <sys/sdt.h> 468f2ac656SPatrick Kelsey #include <sys/sysctl.h> 47defdcdd5SMateusz Guzik #include <sys/smp.h> 48d25c25dcSKristof Provost #include <sys/lock.h> 49455969d3SKristof Provost #include <sys/rmlock.h> 503b3a8eb9SGleb Smirnoff #include <sys/tree.h> 51defdcdd5SMateusz Guzik #include <sys/seqc.h> 528ec07310SGleb Smirnoff #include <vm/uma.h> 533b3a8eb9SGleb Smirnoff 54c658610bSKristof Provost #include <net/if.h> 55e732e742SKristof Provost #include <net/ethernet.h> 563b3a8eb9SGleb Smirnoff #include <net/radix.h> 573b3a8eb9SGleb Smirnoff #include <netinet/in.h> 58d38630f6SKristof Provost #ifdef _KERNEL 59d38630f6SKristof Provost #include <netinet/ip.h> 60d38630f6SKristof Provost #include <netinet/tcp.h> 61d38630f6SKristof Provost #include <netinet/udp.h> 62010ee43fSKristof Provost #include <netinet/sctp.h> 63d38630f6SKristof Provost #include <netinet/ip_icmp.h> 64d38630f6SKristof Provost #include <netinet/icmp6.h> 65d38630f6SKristof Provost #endif 663b3a8eb9SGleb Smirnoff 6775bf2db3SGleb Smirnoff #include <netpfil/pf/pf.h> 6875bf2db3SGleb Smirnoff #include <netpfil/pf/pf_altq.h> 6975bf2db3SGleb Smirnoff #include <netpfil/pf/pf_mtag.h> 703b3a8eb9SGleb Smirnoff 713b3a8eb9SGleb Smirnoff #ifdef _KERNEL 723b3a8eb9SGleb Smirnoff 7356b7685aSKristof Provost #define PF_PFIL_NOREFRAGMENT 0x80000000 7456b7685aSKristof Provost 75defdcdd5SMateusz Guzik #if defined(__arm__) 76defdcdd5SMateusz Guzik #define PF_WANT_32_TO_64_COUNTER 77defdcdd5SMateusz Guzik #endif 78defdcdd5SMateusz Guzik 79defdcdd5SMateusz Guzik /* 80defdcdd5SMateusz Guzik * A hybrid of 32-bit and 64-bit counters which can be used on platforms where 81defdcdd5SMateusz Guzik * counter(9) is very expensive. 82defdcdd5SMateusz Guzik * 83defdcdd5SMateusz Guzik * As 32-bit counters are expected to overflow, a periodic job sums them up to 84defdcdd5SMateusz Guzik * a saved 64-bit state. Fetching the value still walks all CPUs to get the most 85defdcdd5SMateusz Guzik * current snapshot. 86defdcdd5SMateusz Guzik */ 87defdcdd5SMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER 88defdcdd5SMateusz Guzik struct pf_counter_u64_pcpu { 89defdcdd5SMateusz Guzik u_int32_t current; 90defdcdd5SMateusz Guzik u_int32_t snapshot; 91defdcdd5SMateusz Guzik }; 92defdcdd5SMateusz Guzik 93defdcdd5SMateusz Guzik struct pf_counter_u64 { 94defdcdd5SMateusz Guzik struct pf_counter_u64_pcpu *pfcu64_pcpu; 95defdcdd5SMateusz Guzik u_int64_t pfcu64_value; 96defdcdd5SMateusz Guzik seqc_t pfcu64_seqc; 97defdcdd5SMateusz Guzik }; 98defdcdd5SMateusz Guzik 99defdcdd5SMateusz Guzik static inline int 100defdcdd5SMateusz Guzik pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags) 101defdcdd5SMateusz Guzik { 102defdcdd5SMateusz Guzik 103defdcdd5SMateusz Guzik pfcu64->pfcu64_value = 0; 104defdcdd5SMateusz Guzik pfcu64->pfcu64_seqc = 0; 105defdcdd5SMateusz Guzik pfcu64->pfcu64_pcpu = uma_zalloc_pcpu(pcpu_zone_8, flags | M_ZERO); 106defdcdd5SMateusz Guzik if (__predict_false(pfcu64->pfcu64_pcpu == NULL)) 107defdcdd5SMateusz Guzik return (ENOMEM); 108defdcdd5SMateusz Guzik return (0); 109defdcdd5SMateusz Guzik } 110defdcdd5SMateusz Guzik 111defdcdd5SMateusz Guzik static inline void 112defdcdd5SMateusz Guzik pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64) 113defdcdd5SMateusz Guzik { 114defdcdd5SMateusz Guzik 115defdcdd5SMateusz Guzik uma_zfree_pcpu(pcpu_zone_8, pfcu64->pfcu64_pcpu); 116defdcdd5SMateusz Guzik } 117defdcdd5SMateusz Guzik 118defdcdd5SMateusz Guzik static inline void 119defdcdd5SMateusz Guzik pf_counter_u64_critical_enter(void) 120defdcdd5SMateusz Guzik { 121defdcdd5SMateusz Guzik 122defdcdd5SMateusz Guzik critical_enter(); 123defdcdd5SMateusz Guzik } 124defdcdd5SMateusz Guzik 125defdcdd5SMateusz Guzik static inline void 126defdcdd5SMateusz Guzik pf_counter_u64_critical_exit(void) 127defdcdd5SMateusz Guzik { 128defdcdd5SMateusz Guzik 129defdcdd5SMateusz Guzik critical_exit(); 130defdcdd5SMateusz Guzik } 131defdcdd5SMateusz Guzik 132defdcdd5SMateusz Guzik static inline void 133b6196537SMateusz Guzik pf_counter_u64_rollup_protected(struct pf_counter_u64 *pfcu64, uint64_t n) 134b6196537SMateusz Guzik { 135b6196537SMateusz Guzik 136b6196537SMateusz Guzik MPASS(curthread->td_critnest > 0); 137b6196537SMateusz Guzik pfcu64->pfcu64_value += n; 138b6196537SMateusz Guzik } 139b6196537SMateusz Guzik 140b6196537SMateusz Guzik static inline void 141defdcdd5SMateusz Guzik pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n) 142defdcdd5SMateusz Guzik { 143defdcdd5SMateusz Guzik struct pf_counter_u64_pcpu *pcpu; 144defdcdd5SMateusz Guzik u_int32_t val; 145defdcdd5SMateusz Guzik 146defdcdd5SMateusz Guzik MPASS(curthread->td_critnest > 0); 147defdcdd5SMateusz Guzik pcpu = zpcpu_get(pfcu64->pfcu64_pcpu); 148defdcdd5SMateusz Guzik val = atomic_load_int(&pcpu->current); 149defdcdd5SMateusz Guzik atomic_store_int(&pcpu->current, val + n); 150defdcdd5SMateusz Guzik } 151defdcdd5SMateusz Guzik 152defdcdd5SMateusz Guzik static inline void 153defdcdd5SMateusz Guzik pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n) 154defdcdd5SMateusz Guzik { 155defdcdd5SMateusz Guzik 156defdcdd5SMateusz Guzik critical_enter(); 157defdcdd5SMateusz Guzik pf_counter_u64_add_protected(pfcu64, n); 158defdcdd5SMateusz Guzik critical_exit(); 159defdcdd5SMateusz Guzik } 160defdcdd5SMateusz Guzik 161defdcdd5SMateusz Guzik static inline u_int64_t 162defdcdd5SMateusz Guzik pf_counter_u64_periodic(struct pf_counter_u64 *pfcu64) 163defdcdd5SMateusz Guzik { 164defdcdd5SMateusz Guzik struct pf_counter_u64_pcpu *pcpu; 165defdcdd5SMateusz Guzik u_int64_t sum; 166defdcdd5SMateusz Guzik u_int32_t val; 167defdcdd5SMateusz Guzik int cpu; 168defdcdd5SMateusz Guzik 169defdcdd5SMateusz Guzik MPASS(curthread->td_critnest > 0); 170defdcdd5SMateusz Guzik seqc_write_begin(&pfcu64->pfcu64_seqc); 171defdcdd5SMateusz Guzik sum = pfcu64->pfcu64_value; 172defdcdd5SMateusz Guzik CPU_FOREACH(cpu) { 173defdcdd5SMateusz Guzik pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); 174defdcdd5SMateusz Guzik val = atomic_load_int(&pcpu->current); 175defdcdd5SMateusz Guzik sum += (uint32_t)(val - pcpu->snapshot); 176defdcdd5SMateusz Guzik pcpu->snapshot = val; 177defdcdd5SMateusz Guzik } 178defdcdd5SMateusz Guzik pfcu64->pfcu64_value = sum; 179defdcdd5SMateusz Guzik seqc_write_end(&pfcu64->pfcu64_seqc); 180defdcdd5SMateusz Guzik return (sum); 181defdcdd5SMateusz Guzik } 182defdcdd5SMateusz Guzik 183defdcdd5SMateusz Guzik static inline u_int64_t 184773e3a71SMark Johnston pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64) 185defdcdd5SMateusz Guzik { 186defdcdd5SMateusz Guzik struct pf_counter_u64_pcpu *pcpu; 187defdcdd5SMateusz Guzik u_int64_t sum; 188defdcdd5SMateusz Guzik seqc_t seqc; 189defdcdd5SMateusz Guzik int cpu; 190defdcdd5SMateusz Guzik 191defdcdd5SMateusz Guzik for (;;) { 192defdcdd5SMateusz Guzik seqc = seqc_read(&pfcu64->pfcu64_seqc); 193defdcdd5SMateusz Guzik sum = 0; 194defdcdd5SMateusz Guzik CPU_FOREACH(cpu) { 195defdcdd5SMateusz Guzik pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); 196defdcdd5SMateusz Guzik sum += (uint32_t)(atomic_load_int(&pcpu->current) -pcpu->snapshot); 197defdcdd5SMateusz Guzik } 198defdcdd5SMateusz Guzik sum += pfcu64->pfcu64_value; 199defdcdd5SMateusz Guzik if (seqc_consistent(&pfcu64->pfcu64_seqc, seqc)) 200defdcdd5SMateusz Guzik break; 201defdcdd5SMateusz Guzik } 202defdcdd5SMateusz Guzik return (sum); 203defdcdd5SMateusz Guzik } 204defdcdd5SMateusz Guzik 205defdcdd5SMateusz Guzik static inline void 206defdcdd5SMateusz Guzik pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64) 207defdcdd5SMateusz Guzik { 208defdcdd5SMateusz Guzik struct pf_counter_u64_pcpu *pcpu; 209defdcdd5SMateusz Guzik int cpu; 210defdcdd5SMateusz Guzik 211defdcdd5SMateusz Guzik MPASS(curthread->td_critnest > 0); 212defdcdd5SMateusz Guzik seqc_write_begin(&pfcu64->pfcu64_seqc); 213defdcdd5SMateusz Guzik CPU_FOREACH(cpu) { 214defdcdd5SMateusz Guzik pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu); 215defdcdd5SMateusz Guzik pcpu->snapshot = atomic_load_int(&pcpu->current); 216defdcdd5SMateusz Guzik } 217defdcdd5SMateusz Guzik pfcu64->pfcu64_value = 0; 218defdcdd5SMateusz Guzik seqc_write_end(&pfcu64->pfcu64_seqc); 219defdcdd5SMateusz Guzik } 220defdcdd5SMateusz Guzik 221defdcdd5SMateusz Guzik static inline void 222defdcdd5SMateusz Guzik pf_counter_u64_zero(struct pf_counter_u64 *pfcu64) 223defdcdd5SMateusz Guzik { 224defdcdd5SMateusz Guzik 225defdcdd5SMateusz Guzik critical_enter(); 226defdcdd5SMateusz Guzik pf_counter_u64_zero_protected(pfcu64); 227defdcdd5SMateusz Guzik critical_exit(); 228defdcdd5SMateusz Guzik } 229defdcdd5SMateusz Guzik #else 230defdcdd5SMateusz Guzik struct pf_counter_u64 { 231defdcdd5SMateusz Guzik counter_u64_t counter; 232defdcdd5SMateusz Guzik }; 233defdcdd5SMateusz Guzik 234defdcdd5SMateusz Guzik static inline int 235defdcdd5SMateusz Guzik pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags) 236defdcdd5SMateusz Guzik { 237defdcdd5SMateusz Guzik 238defdcdd5SMateusz Guzik pfcu64->counter = counter_u64_alloc(flags); 239defdcdd5SMateusz Guzik if (__predict_false(pfcu64->counter == NULL)) 240defdcdd5SMateusz Guzik return (ENOMEM); 241defdcdd5SMateusz Guzik return (0); 242defdcdd5SMateusz Guzik } 243defdcdd5SMateusz Guzik 244defdcdd5SMateusz Guzik static inline void 245defdcdd5SMateusz Guzik pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64) 246defdcdd5SMateusz Guzik { 247defdcdd5SMateusz Guzik 248defdcdd5SMateusz Guzik counter_u64_free(pfcu64->counter); 249defdcdd5SMateusz Guzik } 250defdcdd5SMateusz Guzik 251defdcdd5SMateusz Guzik static inline void 252defdcdd5SMateusz Guzik pf_counter_u64_critical_enter(void) 253defdcdd5SMateusz Guzik { 254defdcdd5SMateusz Guzik 255defdcdd5SMateusz Guzik } 256defdcdd5SMateusz Guzik 257defdcdd5SMateusz Guzik static inline void 258defdcdd5SMateusz Guzik pf_counter_u64_critical_exit(void) 259defdcdd5SMateusz Guzik { 260defdcdd5SMateusz Guzik 261defdcdd5SMateusz Guzik } 262defdcdd5SMateusz Guzik 263defdcdd5SMateusz Guzik static inline void 264b6196537SMateusz Guzik pf_counter_u64_rollup_protected(struct pf_counter_u64 *pfcu64, uint64_t n) 265b6196537SMateusz Guzik { 266b6196537SMateusz Guzik 267b6196537SMateusz Guzik counter_u64_add(pfcu64->counter, n); 268b6196537SMateusz Guzik } 269b6196537SMateusz Guzik 270b6196537SMateusz Guzik static inline void 271defdcdd5SMateusz Guzik pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n) 272defdcdd5SMateusz Guzik { 273defdcdd5SMateusz Guzik 274defdcdd5SMateusz Guzik counter_u64_add(pfcu64->counter, n); 275defdcdd5SMateusz Guzik } 276defdcdd5SMateusz Guzik 277defdcdd5SMateusz Guzik static inline void 278defdcdd5SMateusz Guzik pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n) 279defdcdd5SMateusz Guzik { 280defdcdd5SMateusz Guzik 281defdcdd5SMateusz Guzik pf_counter_u64_add_protected(pfcu64, n); 282defdcdd5SMateusz Guzik } 283defdcdd5SMateusz Guzik 284defdcdd5SMateusz Guzik static inline u_int64_t 285773e3a71SMark Johnston pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64) 286defdcdd5SMateusz Guzik { 287defdcdd5SMateusz Guzik 288defdcdd5SMateusz Guzik return (counter_u64_fetch(pfcu64->counter)); 289defdcdd5SMateusz Guzik } 290defdcdd5SMateusz Guzik 291defdcdd5SMateusz Guzik static inline void 292defdcdd5SMateusz Guzik pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64) 293defdcdd5SMateusz Guzik { 294defdcdd5SMateusz Guzik 295defdcdd5SMateusz Guzik counter_u64_zero(pfcu64->counter); 296defdcdd5SMateusz Guzik } 297defdcdd5SMateusz Guzik 298defdcdd5SMateusz Guzik static inline void 299defdcdd5SMateusz Guzik pf_counter_u64_zero(struct pf_counter_u64 *pfcu64) 300defdcdd5SMateusz Guzik { 301defdcdd5SMateusz Guzik 302defdcdd5SMateusz Guzik pf_counter_u64_zero_protected(pfcu64); 303defdcdd5SMateusz Guzik } 304defdcdd5SMateusz Guzik #endif 305defdcdd5SMateusz Guzik 3060abcc1d2SReid Linnemann #define pf_get_timestamp(prule)({ \ 3070abcc1d2SReid Linnemann uint32_t _ts = 0; \ 3080abcc1d2SReid Linnemann uint32_t __ts; \ 3090abcc1d2SReid Linnemann int cpu; \ 3100abcc1d2SReid Linnemann CPU_FOREACH(cpu) { \ 3110abcc1d2SReid Linnemann __ts = *zpcpu_get_cpu(prule->timestamp, cpu); \ 3120abcc1d2SReid Linnemann if (__ts > _ts) \ 3130abcc1d2SReid Linnemann _ts = __ts; \ 3140abcc1d2SReid Linnemann } \ 3150abcc1d2SReid Linnemann _ts; \ 3160abcc1d2SReid Linnemann }) 3170abcc1d2SReid Linnemann 3180abcc1d2SReid Linnemann #define pf_update_timestamp(prule) \ 3190abcc1d2SReid Linnemann do { \ 3200abcc1d2SReid Linnemann critical_enter(); \ 3210abcc1d2SReid Linnemann *zpcpu_get((prule)->timestamp) = time_second; \ 3220abcc1d2SReid Linnemann critical_exit(); \ 3230abcc1d2SReid Linnemann } while (0) 3240abcc1d2SReid Linnemann 3251d090028SKristof Provost #define pf_timestamp_pcpu_zone (sizeof(time_t) == 4 ? pcpu_zone_4 : pcpu_zone_8) 3261d090028SKristof Provost _Static_assert(sizeof(time_t) == 4 || sizeof(time_t) == 8, "unexpected time_t size"); 3270abcc1d2SReid Linnemann 3288f2ac656SPatrick Kelsey SYSCTL_DECL(_net_pf); 3298f2ac656SPatrick Kelsey MALLOC_DECLARE(M_PFHASH); 33039282ef3SKajetan Staszkiewicz MALLOC_DECLARE(M_PF_RULE_ITEM); 3318f2ac656SPatrick Kelsey 332ec7b47fcSKristof Provost SDT_PROVIDER_DECLARE(pf); 333ec7b47fcSKristof Provost 3343b3a8eb9SGleb Smirnoff struct pfi_dynaddr { 3353b3a8eb9SGleb Smirnoff TAILQ_ENTRY(pfi_dynaddr) entry; 3363b3a8eb9SGleb Smirnoff struct pf_addr pfid_addr4; 3373b3a8eb9SGleb Smirnoff struct pf_addr pfid_mask4; 3383b3a8eb9SGleb Smirnoff struct pf_addr pfid_addr6; 3393b3a8eb9SGleb Smirnoff struct pf_addr pfid_mask6; 3403b3a8eb9SGleb Smirnoff struct pfr_ktable *pfid_kt; 341320c1116SKristof Provost struct pfi_kkif *pfid_kif; 3423b3a8eb9SGleb Smirnoff int pfid_net; /* mask or 128 */ 3433b3a8eb9SGleb Smirnoff int pfid_acnt4; /* address count IPv4 */ 3443b3a8eb9SGleb Smirnoff int pfid_acnt6; /* address count IPv6 */ 3453b3a8eb9SGleb Smirnoff sa_family_t pfid_af; /* rule af */ 3463b3a8eb9SGleb Smirnoff u_int8_t pfid_iflags; /* PFI_AFLAG_* */ 3473b3a8eb9SGleb Smirnoff }; 3483b3a8eb9SGleb Smirnoff 3493b3a8eb9SGleb Smirnoff /* 3503b3a8eb9SGleb Smirnoff * Address manipulation macros 3513b3a8eb9SGleb Smirnoff */ 3523b3a8eb9SGleb Smirnoff #define HTONL(x) (x) = htonl((__uint32_t)(x)) 3533b3a8eb9SGleb Smirnoff #define HTONS(x) (x) = htons((__uint16_t)(x)) 3543b3a8eb9SGleb Smirnoff #define NTOHL(x) (x) = ntohl((__uint32_t)(x)) 3553b3a8eb9SGleb Smirnoff #define NTOHS(x) (x) = ntohs((__uint16_t)(x)) 3563b3a8eb9SGleb Smirnoff 3573b3a8eb9SGleb Smirnoff #define PF_NAME "pf" 3583b3a8eb9SGleb Smirnoff 3593b3a8eb9SGleb Smirnoff #define PF_HASHROW_ASSERT(h) mtx_assert(&(h)->lock, MA_OWNED) 3603b3a8eb9SGleb Smirnoff #define PF_HASHROW_LOCK(h) mtx_lock(&(h)->lock) 3613b3a8eb9SGleb Smirnoff #define PF_HASHROW_UNLOCK(h) mtx_unlock(&(h)->lock) 3623b3a8eb9SGleb Smirnoff 363907257d6SMateusz Guzik #ifdef INVARIANTS 3643b3a8eb9SGleb Smirnoff #define PF_STATE_LOCK(s) \ 3653b3a8eb9SGleb Smirnoff do { \ 366907257d6SMateusz Guzik struct pf_kstate *_s = (s); \ 367907257d6SMateusz Guzik struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 368907257d6SMateusz Guzik MPASS(_s->lock == &_ih->lock); \ 369907257d6SMateusz Guzik mtx_lock(_s->lock); \ 3703b3a8eb9SGleb Smirnoff } while (0) 3713b3a8eb9SGleb Smirnoff #define PF_STATE_UNLOCK(s) \ 3723b3a8eb9SGleb Smirnoff do { \ 373907257d6SMateusz Guzik struct pf_kstate *_s = (s); \ 374907257d6SMateusz Guzik struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 375907257d6SMateusz Guzik MPASS(_s->lock == &_ih->lock); \ 376907257d6SMateusz Guzik mtx_unlock(_s->lock); \ 3773b3a8eb9SGleb Smirnoff } while (0) 378907257d6SMateusz Guzik #else 3790578fe49SKristof Provost #define PF_STATE_LOCK(s) mtx_lock((s)->lock) 3800578fe49SKristof Provost #define PF_STATE_UNLOCK(s) mtx_unlock((s)->lock) 381907257d6SMateusz Guzik #endif 3823b3a8eb9SGleb Smirnoff 3833b3a8eb9SGleb Smirnoff #ifdef INVARIANTS 3843b3a8eb9SGleb Smirnoff #define PF_STATE_LOCK_ASSERT(s) \ 3853b3a8eb9SGleb Smirnoff do { \ 386907257d6SMateusz Guzik struct pf_kstate *_s = (s); \ 387907257d6SMateusz Guzik struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)]; \ 388907257d6SMateusz Guzik MPASS(_s->lock == &_ih->lock); \ 3893b3a8eb9SGleb Smirnoff PF_HASHROW_ASSERT(_ih); \ 3903b3a8eb9SGleb Smirnoff } while (0) 3913b3a8eb9SGleb Smirnoff #else /* !INVARIANTS */ 3923b3a8eb9SGleb Smirnoff #define PF_STATE_LOCK_ASSERT(s) do {} while (0) 3933b3a8eb9SGleb Smirnoff #endif /* INVARIANTS */ 3943b3a8eb9SGleb Smirnoff 395db0a2bfdSKajetan Staszkiewicz #ifdef INVARIANTS 396db0a2bfdSKajetan Staszkiewicz #define PF_SRC_NODE_LOCK(sn) \ 397db0a2bfdSKajetan Staszkiewicz do { \ 398db0a2bfdSKajetan Staszkiewicz struct pf_ksrc_node *_sn = (sn); \ 399db0a2bfdSKajetan Staszkiewicz struct pf_srchash *_sh = &V_pf_srchash[ \ 400db0a2bfdSKajetan Staszkiewicz pf_hashsrc(&_sn->addr, _sn->af)]; \ 401db0a2bfdSKajetan Staszkiewicz MPASS(_sn->lock == &_sh->lock); \ 402db0a2bfdSKajetan Staszkiewicz mtx_lock(_sn->lock); \ 403db0a2bfdSKajetan Staszkiewicz } while (0) 404db0a2bfdSKajetan Staszkiewicz #define PF_SRC_NODE_UNLOCK(sn) \ 405db0a2bfdSKajetan Staszkiewicz do { \ 406db0a2bfdSKajetan Staszkiewicz struct pf_ksrc_node *_sn = (sn); \ 407db0a2bfdSKajetan Staszkiewicz struct pf_srchash *_sh = &V_pf_srchash[ \ 408db0a2bfdSKajetan Staszkiewicz pf_hashsrc(&_sn->addr, _sn->af)]; \ 409db0a2bfdSKajetan Staszkiewicz MPASS(_sn->lock == &_sh->lock); \ 410db0a2bfdSKajetan Staszkiewicz mtx_unlock(_sn->lock); \ 411db0a2bfdSKajetan Staszkiewicz } while (0) 412db0a2bfdSKajetan Staszkiewicz #else 413db0a2bfdSKajetan Staszkiewicz #define PF_SRC_NODE_LOCK(sn) mtx_lock((sn)->lock) 414db0a2bfdSKajetan Staszkiewicz #define PF_SRC_NODE_UNLOCK(sn) mtx_unlock((sn)->lock) 415db0a2bfdSKajetan Staszkiewicz #endif 416db0a2bfdSKajetan Staszkiewicz 417db0a2bfdSKajetan Staszkiewicz #ifdef INVARIANTS 418db0a2bfdSKajetan Staszkiewicz #define PF_SRC_NODE_LOCK_ASSERT(sn) \ 419db0a2bfdSKajetan Staszkiewicz do { \ 420db0a2bfdSKajetan Staszkiewicz struct pf_ksrc_node *_sn = (sn); \ 421db0a2bfdSKajetan Staszkiewicz struct pf_srchash *_sh = &V_pf_srchash[ \ 422db0a2bfdSKajetan Staszkiewicz pf_hashsrc(&_sn->addr, _sn->af)]; \ 423db0a2bfdSKajetan Staszkiewicz MPASS(_sn->lock == &_sh->lock); \ 424db0a2bfdSKajetan Staszkiewicz PF_HASHROW_ASSERT(_sh); \ 425db0a2bfdSKajetan Staszkiewicz } while (0) 426db0a2bfdSKajetan Staszkiewicz #else /* !INVARIANTS */ 427db0a2bfdSKajetan Staszkiewicz #define PF_SRC_NODE_LOCK_ASSERT(sn) do {} while (0) 428db0a2bfdSKajetan Staszkiewicz #endif /* INVARIANTS */ 429db0a2bfdSKajetan Staszkiewicz 430f649cff5SMateusz Guzik extern struct mtx_padalign pf_unlnkdrules_mtx; 4313b3a8eb9SGleb Smirnoff #define PF_UNLNKDRULES_LOCK() mtx_lock(&pf_unlnkdrules_mtx) 4323b3a8eb9SGleb Smirnoff #define PF_UNLNKDRULES_UNLOCK() mtx_unlock(&pf_unlnkdrules_mtx) 433f11b6505SMateusz Guzik #define PF_UNLNKDRULES_ASSERT() mtx_assert(&pf_unlnkdrules_mtx, MA_OWNED) 4343b3a8eb9SGleb Smirnoff 43593f8c38cSMateusz Guzik extern struct sx pf_config_lock; 43693f8c38cSMateusz Guzik #define PF_CONFIG_LOCK() sx_xlock(&pf_config_lock) 43793f8c38cSMateusz Guzik #define PF_CONFIG_UNLOCK() sx_xunlock(&pf_config_lock) 43893f8c38cSMateusz Guzik #define PF_CONFIG_ASSERT() sx_assert(&pf_config_lock, SA_XLOCKED) 43993f8c38cSMateusz Guzik 4402e6cdfe2SKristof Provost VNET_DECLARE(struct rmlock, pf_rules_lock); 4412e6cdfe2SKristof Provost #define V_pf_rules_lock VNET(pf_rules_lock) 4422e6cdfe2SKristof Provost 443455969d3SKristof Provost #define PF_RULES_RLOCK_TRACKER struct rm_priotracker _pf_rules_tracker 4442e6cdfe2SKristof Provost #define PF_RULES_RLOCK() rm_rlock(&V_pf_rules_lock, &_pf_rules_tracker) 4452e6cdfe2SKristof Provost #define PF_RULES_RUNLOCK() rm_runlock(&V_pf_rules_lock, &_pf_rules_tracker) 4462e6cdfe2SKristof Provost #define PF_RULES_WLOCK() rm_wlock(&V_pf_rules_lock) 4472e6cdfe2SKristof Provost #define PF_RULES_WUNLOCK() rm_wunlock(&V_pf_rules_lock) 4482e6cdfe2SKristof Provost #define PF_RULES_WOWNED() rm_wowned(&V_pf_rules_lock) 4492e6cdfe2SKristof Provost #define PF_RULES_ASSERT() rm_assert(&V_pf_rules_lock, RA_LOCKED) 4502e6cdfe2SKristof Provost #define PF_RULES_RASSERT() rm_assert(&V_pf_rules_lock, RA_RLOCKED) 4512e6cdfe2SKristof Provost #define PF_RULES_WASSERT() rm_assert(&V_pf_rules_lock, RA_WLOCKED) 4523b3a8eb9SGleb Smirnoff 453f649cff5SMateusz Guzik extern struct mtx_padalign pf_table_stats_lock; 454dc1ab04eSMateusz Guzik #define PF_TABLE_STATS_LOCK() mtx_lock(&pf_table_stats_lock) 455dc1ab04eSMateusz Guzik #define PF_TABLE_STATS_UNLOCK() mtx_unlock(&pf_table_stats_lock) 456dc1ab04eSMateusz Guzik #define PF_TABLE_STATS_OWNED() mtx_owned(&pf_table_stats_lock) 457af94d8ccSKristof Provost #define PF_TABLE_STATS_ASSERT() mtx_assert(&pf_table_stats_lock, MA_OWNED) 458dc1ab04eSMateusz Guzik 4592f8fb3a8SKristof Provost extern struct sx pf_end_lock; 4602f8fb3a8SKristof Provost 4613b3a8eb9SGleb Smirnoff #define PF_MODVER 1 4623b3a8eb9SGleb Smirnoff #define PFLOG_MODVER 1 4633b3a8eb9SGleb Smirnoff #define PFSYNC_MODVER 1 4643b3a8eb9SGleb Smirnoff 4653b3a8eb9SGleb Smirnoff #define PFLOG_MINVER 1 4663b3a8eb9SGleb Smirnoff #define PFLOG_PREFVER PFLOG_MODVER 4673b3a8eb9SGleb Smirnoff #define PFLOG_MAXVER 1 4683b3a8eb9SGleb Smirnoff #define PFSYNC_MINVER 1 4693b3a8eb9SGleb Smirnoff #define PFSYNC_PREFVER PFSYNC_MODVER 4703b3a8eb9SGleb Smirnoff #define PFSYNC_MAXVER 1 4713b3a8eb9SGleb Smirnoff 4723b3a8eb9SGleb Smirnoff #ifdef INET 4733b3a8eb9SGleb Smirnoff #ifndef INET6 4743b3a8eb9SGleb Smirnoff #define PF_INET_ONLY 4753b3a8eb9SGleb Smirnoff #endif /* ! INET6 */ 4763b3a8eb9SGleb Smirnoff #endif /* INET */ 4773b3a8eb9SGleb Smirnoff 4783b3a8eb9SGleb Smirnoff #ifdef INET6 4793b3a8eb9SGleb Smirnoff #ifndef INET 4803b3a8eb9SGleb Smirnoff #define PF_INET6_ONLY 4813b3a8eb9SGleb Smirnoff #endif /* ! INET */ 4823b3a8eb9SGleb Smirnoff #endif /* INET6 */ 4833b3a8eb9SGleb Smirnoff 4843b3a8eb9SGleb Smirnoff #ifdef INET 4853b3a8eb9SGleb Smirnoff #ifdef INET6 4863b3a8eb9SGleb Smirnoff #define PF_INET_INET6 4873b3a8eb9SGleb Smirnoff #endif /* INET6 */ 4883b3a8eb9SGleb Smirnoff #endif /* INET */ 4893b3a8eb9SGleb Smirnoff 4903b3a8eb9SGleb Smirnoff #else 4913b3a8eb9SGleb Smirnoff 4923b3a8eb9SGleb Smirnoff #define PF_INET_INET6 4933b3a8eb9SGleb Smirnoff 4943b3a8eb9SGleb Smirnoff #endif /* _KERNEL */ 4953b3a8eb9SGleb Smirnoff 4963b3a8eb9SGleb Smirnoff /* Both IPv4 and IPv6 */ 4973b3a8eb9SGleb Smirnoff #ifdef PF_INET_INET6 4983b3a8eb9SGleb Smirnoff 4993b3a8eb9SGleb Smirnoff #define PF_AEQ(a, b, c) \ 5003b3a8eb9SGleb Smirnoff ((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \ 5016332e4ccSGeorge V. Neville-Neil (c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \ 5023b3a8eb9SGleb Smirnoff (a)->addr32[2] == (b)->addr32[2] && \ 5033b3a8eb9SGleb Smirnoff (a)->addr32[1] == (b)->addr32[1] && \ 5043b3a8eb9SGleb Smirnoff (a)->addr32[0] == (b)->addr32[0])) \ 5053b3a8eb9SGleb Smirnoff 5063b3a8eb9SGleb Smirnoff #define PF_ANEQ(a, b, c) \ 5077d762423SKristof Provost ((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \ 50826022843SKristof Provost (c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \ 5093b3a8eb9SGleb Smirnoff (a)->addr32[1] != (b)->addr32[1] || \ 5106332e4ccSGeorge V. Neville-Neil (a)->addr32[2] != (b)->addr32[2] || \ 51126022843SKristof Provost (a)->addr32[3] != (b)->addr32[3]))) \ 5123b3a8eb9SGleb Smirnoff 5133b3a8eb9SGleb Smirnoff #define PF_AZERO(a, c) \ 5143b3a8eb9SGleb Smirnoff ((c == AF_INET && !(a)->addr32[0]) || \ 5156332e4ccSGeorge V. Neville-Neil (c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \ 5163b3a8eb9SGleb Smirnoff !(a)->addr32[2] && !(a)->addr32[3] )) \ 5173b3a8eb9SGleb Smirnoff 5183b3a8eb9SGleb Smirnoff #define PF_MATCHA(n, a, m, b, f) \ 5193b3a8eb9SGleb Smirnoff pf_match_addr(n, a, m, b, f) 5203b3a8eb9SGleb Smirnoff 5213b3a8eb9SGleb Smirnoff #define PF_ACPY(a, b, f) \ 5223b3a8eb9SGleb Smirnoff pf_addrcpy(a, b, f) 5233b3a8eb9SGleb Smirnoff 5243b3a8eb9SGleb Smirnoff #define PF_AINC(a, f) \ 5253b3a8eb9SGleb Smirnoff pf_addr_inc(a, f) 5263b3a8eb9SGleb Smirnoff 5273b3a8eb9SGleb Smirnoff #define PF_POOLMASK(a, b, c, d, f) \ 5283b3a8eb9SGleb Smirnoff pf_poolmask(a, b, c, d, f) 5293b3a8eb9SGleb Smirnoff 5303b3a8eb9SGleb Smirnoff #else 5313b3a8eb9SGleb Smirnoff 5323b3a8eb9SGleb Smirnoff /* Just IPv6 */ 5333b3a8eb9SGleb Smirnoff 5343b3a8eb9SGleb Smirnoff #ifdef PF_INET6_ONLY 5353b3a8eb9SGleb Smirnoff 5363b3a8eb9SGleb Smirnoff #define PF_AEQ(a, b, c) \ 5373b3a8eb9SGleb Smirnoff ((a)->addr32[3] == (b)->addr32[3] && \ 5383b3a8eb9SGleb Smirnoff (a)->addr32[2] == (b)->addr32[2] && \ 5393b3a8eb9SGleb Smirnoff (a)->addr32[1] == (b)->addr32[1] && \ 5403b3a8eb9SGleb Smirnoff (a)->addr32[0] == (b)->addr32[0]) \ 5413b3a8eb9SGleb Smirnoff 5423b3a8eb9SGleb Smirnoff #define PF_ANEQ(a, b, c) \ 5433b3a8eb9SGleb Smirnoff ((a)->addr32[3] != (b)->addr32[3] || \ 5443b3a8eb9SGleb Smirnoff (a)->addr32[2] != (b)->addr32[2] || \ 5453b3a8eb9SGleb Smirnoff (a)->addr32[1] != (b)->addr32[1] || \ 5463b3a8eb9SGleb Smirnoff (a)->addr32[0] != (b)->addr32[0]) \ 5473b3a8eb9SGleb Smirnoff 5483b3a8eb9SGleb Smirnoff #define PF_AZERO(a, c) \ 5493b3a8eb9SGleb Smirnoff (!(a)->addr32[0] && \ 5503b3a8eb9SGleb Smirnoff !(a)->addr32[1] && \ 5513b3a8eb9SGleb Smirnoff !(a)->addr32[2] && \ 5523b3a8eb9SGleb Smirnoff !(a)->addr32[3] ) \ 5533b3a8eb9SGleb Smirnoff 5543b3a8eb9SGleb Smirnoff #define PF_MATCHA(n, a, m, b, f) \ 5553b3a8eb9SGleb Smirnoff pf_match_addr(n, a, m, b, f) 5563b3a8eb9SGleb Smirnoff 5573b3a8eb9SGleb Smirnoff #define PF_ACPY(a, b, f) \ 5583b3a8eb9SGleb Smirnoff pf_addrcpy(a, b, f) 5593b3a8eb9SGleb Smirnoff 5603b3a8eb9SGleb Smirnoff #define PF_AINC(a, f) \ 5613b3a8eb9SGleb Smirnoff pf_addr_inc(a, f) 5623b3a8eb9SGleb Smirnoff 5633b3a8eb9SGleb Smirnoff #define PF_POOLMASK(a, b, c, d, f) \ 5643b3a8eb9SGleb Smirnoff pf_poolmask(a, b, c, d, f) 5653b3a8eb9SGleb Smirnoff 5663b3a8eb9SGleb Smirnoff #else 5673b3a8eb9SGleb Smirnoff 5683b3a8eb9SGleb Smirnoff /* Just IPv4 */ 5693b3a8eb9SGleb Smirnoff #ifdef PF_INET_ONLY 5703b3a8eb9SGleb Smirnoff 5713b3a8eb9SGleb Smirnoff #define PF_AEQ(a, b, c) \ 5723b3a8eb9SGleb Smirnoff ((a)->addr32[0] == (b)->addr32[0]) 5733b3a8eb9SGleb Smirnoff 5743b3a8eb9SGleb Smirnoff #define PF_ANEQ(a, b, c) \ 5753b3a8eb9SGleb Smirnoff ((a)->addr32[0] != (b)->addr32[0]) 5763b3a8eb9SGleb Smirnoff 5773b3a8eb9SGleb Smirnoff #define PF_AZERO(a, c) \ 5783b3a8eb9SGleb Smirnoff (!(a)->addr32[0]) 5793b3a8eb9SGleb Smirnoff 5803b3a8eb9SGleb Smirnoff #define PF_MATCHA(n, a, m, b, f) \ 5813b3a8eb9SGleb Smirnoff pf_match_addr(n, a, m, b, f) 5823b3a8eb9SGleb Smirnoff 5833b3a8eb9SGleb Smirnoff #define PF_ACPY(a, b, f) \ 5843b3a8eb9SGleb Smirnoff (a)->v4.s_addr = (b)->v4.s_addr 5853b3a8eb9SGleb Smirnoff 5863b3a8eb9SGleb Smirnoff #define PF_AINC(a, f) \ 5873b3a8eb9SGleb Smirnoff do { \ 5883b3a8eb9SGleb Smirnoff (a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \ 5893b3a8eb9SGleb Smirnoff } while (0) 5903b3a8eb9SGleb Smirnoff 5913b3a8eb9SGleb Smirnoff #define PF_POOLMASK(a, b, c, d, f) \ 5923b3a8eb9SGleb Smirnoff do { \ 5933b3a8eb9SGleb Smirnoff (a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \ 5943b3a8eb9SGleb Smirnoff (((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \ 5953b3a8eb9SGleb Smirnoff } while (0) 5963b3a8eb9SGleb Smirnoff 5973b3a8eb9SGleb Smirnoff #endif /* PF_INET_ONLY */ 5983b3a8eb9SGleb Smirnoff #endif /* PF_INET6_ONLY */ 5993b3a8eb9SGleb Smirnoff #endif /* PF_INET_INET6 */ 6003b3a8eb9SGleb Smirnoff 601*e7995c99SKristof Provost #ifdef _KERNEL 602*e7995c99SKristof Provost #ifdef INET6 603*e7995c99SKristof Provost static void inline 604*e7995c99SKristof Provost pf_addrcpy(struct pf_addr *dst, const struct pf_addr *src, sa_family_t af) 605*e7995c99SKristof Provost { 606*e7995c99SKristof Provost switch (af) { 607*e7995c99SKristof Provost #ifdef INET 608*e7995c99SKristof Provost case AF_INET: 609*e7995c99SKristof Provost memcpy(&dst->v4, &src->v4, sizeof(dst->v4)); 610*e7995c99SKristof Provost break; 611*e7995c99SKristof Provost #endif /* INET */ 612*e7995c99SKristof Provost case AF_INET6: 613*e7995c99SKristof Provost memcpy(&dst->v6, &src->v6, sizeof(dst->v6)); 614*e7995c99SKristof Provost break; 615*e7995c99SKristof Provost } 616*e7995c99SKristof Provost } 617*e7995c99SKristof Provost #endif /* INET6 */ 618*e7995c99SKristof Provost #endif 619*e7995c99SKristof Provost 6203b3a8eb9SGleb Smirnoff /* 6213b3a8eb9SGleb Smirnoff * XXX callers not FIB-aware in our version of pf yet. 6223b3a8eb9SGleb Smirnoff * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio. 6233b3a8eb9SGleb Smirnoff */ 6243b3a8eb9SGleb Smirnoff #define PF_MISMATCHAW(aw, x, af, neg, ifp, rtid) \ 6253b3a8eb9SGleb Smirnoff ( \ 6263b3a8eb9SGleb Smirnoff (((aw)->type == PF_ADDR_NOROUTE && \ 6273b3a8eb9SGleb Smirnoff pf_routable((x), (af), NULL, (rtid))) || \ 6283b3a8eb9SGleb Smirnoff (((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL && \ 6293b3a8eb9SGleb Smirnoff pf_routable((x), (af), (ifp), (rtid))) || \ 6303b3a8eb9SGleb Smirnoff ((aw)->type == PF_ADDR_TABLE && \ 6313b3a8eb9SGleb Smirnoff !pfr_match_addr((aw)->p.tbl, (x), (af))) || \ 6323b3a8eb9SGleb Smirnoff ((aw)->type == PF_ADDR_DYNIFTL && \ 6333b3a8eb9SGleb Smirnoff !pfi_match_addr((aw)->p.dyn, (x), (af))) || \ 6343b3a8eb9SGleb Smirnoff ((aw)->type == PF_ADDR_RANGE && \ 6353b3a8eb9SGleb Smirnoff !pf_match_addr_range(&(aw)->v.a.addr, \ 6363b3a8eb9SGleb Smirnoff &(aw)->v.a.mask, (x), (af))) || \ 6373b3a8eb9SGleb Smirnoff ((aw)->type == PF_ADDR_ADDRMASK && \ 6383b3a8eb9SGleb Smirnoff !PF_AZERO(&(aw)->v.a.mask, (af)) && \ 6393b3a8eb9SGleb Smirnoff !PF_MATCHA(0, &(aw)->v.a.addr, \ 6403b3a8eb9SGleb Smirnoff &(aw)->v.a.mask, (x), (af))))) != \ 6413b3a8eb9SGleb Smirnoff (neg) \ 6423b3a8eb9SGleb Smirnoff ) 6433b3a8eb9SGleb Smirnoff 644c3f69af0SKristof Provost #define PF_ALGNMNT(off) (((off) % 2) == 0) 645c3f69af0SKristof Provost 64607e070efSKajetan Staszkiewicz /* 64707e070efSKajetan Staszkiewicz * At the moment there are no rules which have both NAT and RDR actions, 64807e070efSKajetan Staszkiewicz * apart from af-to rules, but those don't to source tracking for address 64907e070efSKajetan Staszkiewicz * translation. And the r->rdr pool is used for both NAT and RDR. 65007e070efSKajetan Staszkiewicz * So there is no PF_SN_RDR. 65107e070efSKajetan Staszkiewicz */ 65207e070efSKajetan Staszkiewicz enum pf_sn_types { PF_SN_LIMIT, PF_SN_NAT, PF_SN_ROUTE, PF_SN_MAX }; 65307e070efSKajetan Staszkiewicz typedef enum pf_sn_types pf_sn_types_t; 65407e070efSKajetan Staszkiewicz #define PF_SN_TYPE_NAMES { \ 65507e070efSKajetan Staszkiewicz "limit source-track", \ 65607e070efSKajetan Staszkiewicz "NAT/RDR sticky-address", \ 65707e070efSKajetan Staszkiewicz "route sticky-address", \ 65807e070efSKajetan Staszkiewicz NULL \ 65907e070efSKajetan Staszkiewicz } 66007e070efSKajetan Staszkiewicz 66117ad7334SKristof Provost #ifdef _KERNEL 662e86bddeaSKristof Provost 663320c1116SKristof Provost struct pf_kpooladdr { 664320c1116SKristof Provost struct pf_addr_wrap addr; 665320c1116SKristof Provost TAILQ_ENTRY(pf_kpooladdr) entries; 666320c1116SKristof Provost char ifname[IFNAMSIZ]; 667320c1116SKristof Provost struct pfi_kkif *kif; 668320c1116SKristof Provost }; 669320c1116SKristof Provost 670320c1116SKristof Provost TAILQ_HEAD(pf_kpalist, pf_kpooladdr); 671320c1116SKristof Provost 672320c1116SKristof Provost struct pf_kpool { 6735f5e32f1SKristof Provost struct mtx mtx; 674320c1116SKristof Provost struct pf_kpalist list; 675320c1116SKristof Provost struct pf_kpooladdr *cur; 676320c1116SKristof Provost struct pf_poolhashkey key; 677320c1116SKristof Provost struct pf_addr counter; 6782aa21096SKurosawa Takahiro struct pf_mape_portset mape; 679320c1116SKristof Provost int tblidx; 680320c1116SKristof Provost u_int16_t proxy_port[2]; 681320c1116SKristof Provost u_int8_t opts; 682320c1116SKristof Provost }; 683320c1116SKristof Provost 684ef950daaSKristof Provost struct pf_rule_actions { 685c49c9da2SKajetan Staszkiewicz struct pf_addr rt_addr; 686c49c9da2SKajetan Staszkiewicz struct pfi_kkif *rt_kif; 6878c23afdbSKajetan Staszkiewicz int32_t rtableid; 688c49c9da2SKajetan Staszkiewicz uint32_t flags; 689b64f7ce9SKristof Provost uint16_t qid; 690b64f7ce9SKristof Provost uint16_t pqid; 69139282ef3SKajetan Staszkiewicz uint16_t max_mss; 692c49c9da2SKajetan Staszkiewicz uint16_t dnpipe; 693c49c9da2SKajetan Staszkiewicz uint16_t dnrpipe; /* Reverse direction pipe */ 69439282ef3SKajetan Staszkiewicz uint8_t log; 69539282ef3SKajetan Staszkiewicz uint8_t set_tos; 69639282ef3SKajetan Staszkiewicz uint8_t min_ttl; 6974bf98559SKajetan Staszkiewicz uint8_t set_prio[2]; 698c49c9da2SKajetan Staszkiewicz uint8_t rt; 699ef950daaSKristof Provost }; 700ef950daaSKristof Provost 701e732e742SKristof Provost union pf_keth_rule_ptr { 702e732e742SKristof Provost struct pf_keth_rule *ptr; 703e732e742SKristof Provost uint32_t nr; 704e732e742SKristof Provost }; 705e732e742SKristof Provost 706e732e742SKristof Provost struct pf_keth_rule_addr { 707e732e742SKristof Provost uint8_t addr[ETHER_ADDR_LEN]; 708b590f17aSKristof Provost uint8_t mask[ETHER_ADDR_LEN]; 709e732e742SKristof Provost bool neg; 710e732e742SKristof Provost uint8_t isset; 711e732e742SKristof Provost }; 712e732e742SKristof Provost 713c5131afeSKristof Provost struct pf_keth_anchor; 714c5131afeSKristof Provost 715c5131afeSKristof Provost TAILQ_HEAD(pf_keth_ruleq, pf_keth_rule); 716c5131afeSKristof Provost 717c5131afeSKristof Provost struct pf_keth_ruleset { 718c5131afeSKristof Provost struct pf_keth_ruleq rules[2]; 719c5131afeSKristof Provost struct pf_keth_rules { 720c5131afeSKristof Provost struct pf_keth_ruleq *rules; 721c5131afeSKristof Provost int open; 722c5131afeSKristof Provost uint32_t ticket; 723c5131afeSKristof Provost } active, inactive; 724c5131afeSKristof Provost struct vnet *vnet; 725c5131afeSKristof Provost struct pf_keth_anchor *anchor; 726c5131afeSKristof Provost }; 727c5131afeSKristof Provost 728c5131afeSKristof Provost RB_HEAD(pf_keth_anchor_global, pf_keth_anchor); 729c5131afeSKristof Provost RB_HEAD(pf_keth_anchor_node, pf_keth_anchor); 730c5131afeSKristof Provost struct pf_keth_anchor { 731c5131afeSKristof Provost RB_ENTRY(pf_keth_anchor) entry_node; 732c5131afeSKristof Provost RB_ENTRY(pf_keth_anchor) entry_global; 733c5131afeSKristof Provost struct pf_keth_anchor *parent; 734c5131afeSKristof Provost struct pf_keth_anchor_node children; 735c5131afeSKristof Provost char name[PF_ANCHOR_NAME_SIZE]; 736c5131afeSKristof Provost char path[MAXPATHLEN]; 737c5131afeSKristof Provost struct pf_keth_ruleset ruleset; 738c5131afeSKristof Provost int refcnt; /* anchor rules */ 739c5131afeSKristof Provost uint8_t anchor_relative; 740c5131afeSKristof Provost uint8_t anchor_wildcard; 741c5131afeSKristof Provost }; 742c5131afeSKristof Provost RB_PROTOTYPE(pf_keth_anchor_node, pf_keth_anchor, entry_node, 743c5131afeSKristof Provost pf_keth_anchor_compare); 744c5131afeSKristof Provost RB_PROTOTYPE(pf_keth_anchor_global, pf_keth_anchor, entry_global, 745c5131afeSKristof Provost pf_keth_anchor_compare); 746c5131afeSKristof Provost 747e732e742SKristof Provost struct pf_keth_rule { 748e732e742SKristof Provost #define PFE_SKIP_IFP 0 749e732e742SKristof Provost #define PFE_SKIP_DIR 1 750e732e742SKristof Provost #define PFE_SKIP_PROTO 2 751e732e742SKristof Provost #define PFE_SKIP_SRC_ADDR 3 752e732e742SKristof Provost #define PFE_SKIP_DST_ADDR 4 753ba94bf28SKristof Provost #define PFE_SKIP_SRC_IP_ADDR 5 754ba94bf28SKristof Provost #define PFE_SKIP_DST_IP_ADDR 6 755ba94bf28SKristof Provost #define PFE_SKIP_COUNT 7 756e732e742SKristof Provost union pf_keth_rule_ptr skip[PFE_SKIP_COUNT]; 757e732e742SKristof Provost 758e732e742SKristof Provost TAILQ_ENTRY(pf_keth_rule) entries; 759e732e742SKristof Provost 760c5131afeSKristof Provost struct pf_keth_anchor *anchor; 761c5131afeSKristof Provost u_int8_t anchor_relative; 762c5131afeSKristof Provost u_int8_t anchor_wildcard; 763c5131afeSKristof Provost 764e732e742SKristof Provost uint32_t nr; 765e732e742SKristof Provost 766e732e742SKristof Provost bool quick; 767e732e742SKristof Provost 768e732e742SKristof Provost /* Filter */ 769e732e742SKristof Provost char ifname[IFNAMSIZ]; 770e732e742SKristof Provost struct pfi_kkif *kif; 771e732e742SKristof Provost bool ifnot; 772e732e742SKristof Provost uint8_t direction; 773e732e742SKristof Provost uint16_t proto; 774e732e742SKristof Provost struct pf_keth_rule_addr src, dst; 7758a42005dSKristof Provost struct pf_rule_addr ipsrc, ipdst; 7761f61367fSKristof Provost char match_tagname[PF_TAG_NAME_SIZE]; 7771f61367fSKristof Provost uint16_t match_tag; 7781f61367fSKristof Provost bool match_tag_not; 7791f61367fSKristof Provost 780e732e742SKristof Provost 781e732e742SKristof Provost /* Stats */ 782e732e742SKristof Provost counter_u64_t evaluations; 783e732e742SKristof Provost counter_u64_t packets[2]; 784e732e742SKristof Provost counter_u64_t bytes[2]; 7851d090028SKristof Provost time_t *timestamp; 786e732e742SKristof Provost 787e732e742SKristof Provost /* Action */ 788e732e742SKristof Provost char qname[PF_QNAME_SIZE]; 789e732e742SKristof Provost int qid; 790e732e742SKristof Provost char tagname[PF_TAG_NAME_SIZE]; 791e732e742SKristof Provost uint16_t tag; 7928a8af942SKristof Provost char bridge_to_name[IFNAMSIZ]; 7938a8af942SKristof Provost struct pfi_kkif *bridge_to; 794e732e742SKristof Provost uint8_t action; 795fb330f39SKristof Provost uint16_t dnpipe; 796fb330f39SKristof Provost uint32_t dnflags; 797ef661d4aSChristian McDonald 798ef661d4aSChristian McDonald char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; 799ef661d4aSChristian McDonald uint32_t ridentifier; 800e732e742SKristof Provost }; 801e732e742SKristof Provost 802bd7762c8SMateusz Guzik RB_HEAD(pf_krule_global, pf_krule); 803bd7762c8SMateusz Guzik RB_PROTOTYPE(pf_krule_global, pf_krule, entry_global, pf_krule_compare); 804bd7762c8SMateusz Guzik 805e86bddeaSKristof Provost struct pf_krule { 806e86bddeaSKristof Provost struct pf_rule_addr src; 807e86bddeaSKristof Provost struct pf_rule_addr dst; 808e5c64b26SKajetan Staszkiewicz struct pf_krule *skip[PF_SKIP_COUNT]; 8096fcc8e04SKristof Provost char label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE]; 81076c5eeccSKristof Provost uint32_t ridentifier; 811e86bddeaSKristof Provost char ifname[IFNAMSIZ]; 8122339ead6SKristof Provost char rcv_ifname[IFNAMSIZ]; 813e86bddeaSKristof Provost char qname[PF_QNAME_SIZE]; 814e86bddeaSKristof Provost char pqname[PF_QNAME_SIZE]; 815e86bddeaSKristof Provost char tagname[PF_TAG_NAME_SIZE]; 816e86bddeaSKristof Provost char match_tagname[PF_TAG_NAME_SIZE]; 817e86bddeaSKristof Provost 818e86bddeaSKristof Provost char overload_tblname[PF_TABLE_NAME_SIZE]; 819e86bddeaSKristof Provost 820e86bddeaSKristof Provost TAILQ_ENTRY(pf_krule) entries; 821e11dacbfSKristof Provost struct pf_kpool nat; 822e11dacbfSKristof Provost struct pf_kpool rdr; 8230972294eSKristof Provost struct pf_kpool route; 824e86bddeaSKristof Provost 82502cf67ccSMateusz Guzik struct pf_counter_u64 evaluations; 82602cf67ccSMateusz Guzik struct pf_counter_u64 packets[2]; 82702cf67ccSMateusz Guzik struct pf_counter_u64 bytes[2]; 8281d090028SKristof Provost time_t *timestamp; 829e86bddeaSKristof Provost 830320c1116SKristof Provost struct pfi_kkif *kif; 8312339ead6SKristof Provost struct pfi_kkif *rcv_kif; 832e86bddeaSKristof Provost struct pf_kanchor *anchor; 833e86bddeaSKristof Provost struct pfr_ktable *overload_tbl; 834e86bddeaSKristof Provost 835e86bddeaSKristof Provost pf_osfp_t os_fingerprint; 836e86bddeaSKristof Provost 8378c23afdbSKajetan Staszkiewicz int32_t rtableid; 838e86bddeaSKristof Provost u_int32_t timeout[PFTM_MAX]; 839e86bddeaSKristof Provost u_int32_t max_states; 840e86bddeaSKristof Provost u_int32_t max_src_nodes; 841e86bddeaSKristof Provost u_int32_t max_src_states; 842e86bddeaSKristof Provost u_int32_t max_src_conn; 843e86bddeaSKristof Provost struct { 844e86bddeaSKristof Provost u_int32_t limit; 845e86bddeaSKristof Provost u_int32_t seconds; 846e86bddeaSKristof Provost } max_src_conn_rate; 847b64f7ce9SKristof Provost u_int16_t qid; 848b64f7ce9SKristof Provost u_int16_t pqid; 84963b3c1c7SKristof Provost u_int16_t dnpipe; 85063b3c1c7SKristof Provost u_int16_t dnrpipe; 85163b3c1c7SKristof Provost u_int32_t free_flags; 852e86bddeaSKristof Provost u_int32_t nr; 853e86bddeaSKristof Provost u_int32_t prob; 854e86bddeaSKristof Provost uid_t cuid; 855e86bddeaSKristof Provost pid_t cpid; 856e86bddeaSKristof Provost 857e86bddeaSKristof Provost counter_u64_t states_cur; 858e86bddeaSKristof Provost counter_u64_t states_tot; 85907e070efSKajetan Staszkiewicz counter_u64_t src_nodes[PF_SN_MAX]; 860e86bddeaSKristof Provost 861e86bddeaSKristof Provost u_int16_t return_icmp; 862e86bddeaSKristof Provost u_int16_t return_icmp6; 863e86bddeaSKristof Provost u_int16_t max_mss; 864e86bddeaSKristof Provost u_int16_t tag; 865e86bddeaSKristof Provost u_int16_t match_tag; 866e86bddeaSKristof Provost u_int16_t scrub_flags; 867e86bddeaSKristof Provost 868e86bddeaSKristof Provost struct pf_rule_uid uid; 869e86bddeaSKristof Provost struct pf_rule_gid gid; 870e86bddeaSKristof Provost 871e86bddeaSKristof Provost u_int32_t rule_flag; 8724f1f67e8SKristof Provost uint32_t rule_ref; 873e86bddeaSKristof Provost u_int8_t action; 874e86bddeaSKristof Provost u_int8_t direction; 875e86bddeaSKristof Provost u_int8_t log; 876e86bddeaSKristof Provost u_int8_t logif; 877e86bddeaSKristof Provost u_int8_t quick; 878e86bddeaSKristof Provost u_int8_t ifnot; 879e86bddeaSKristof Provost u_int8_t match_tag_not; 880e86bddeaSKristof Provost u_int8_t natpass; 881e86bddeaSKristof Provost 882e86bddeaSKristof Provost u_int8_t keep_state; 883e86bddeaSKristof Provost sa_family_t af; 884e86bddeaSKristof Provost u_int8_t proto; 885e86bddeaSKristof Provost u_int8_t type; 886e86bddeaSKristof Provost u_int8_t code; 887e86bddeaSKristof Provost u_int8_t flags; 888e86bddeaSKristof Provost u_int8_t flagset; 889e86bddeaSKristof Provost u_int8_t min_ttl; 890e86bddeaSKristof Provost u_int8_t allow_opts; 891e86bddeaSKristof Provost u_int8_t rt; 892e86bddeaSKristof Provost u_int8_t return_ttl; 893e86bddeaSKristof Provost u_int8_t tos; 894e86bddeaSKristof Provost u_int8_t set_tos; 895e86bddeaSKristof Provost u_int8_t anchor_relative; 896e86bddeaSKristof Provost u_int8_t anchor_wildcard; 897e86bddeaSKristof Provost 898e86bddeaSKristof Provost u_int8_t flush; 899e86bddeaSKristof Provost u_int8_t prio; 900e86bddeaSKristof Provost u_int8_t set_prio[2]; 901fcdb520cSKristof Provost sa_family_t naf; 90267c19da0SKristof Provost u_int8_t rcvifnot; 903e86bddeaSKristof Provost 904e86bddeaSKristof Provost struct { 905e86bddeaSKristof Provost struct pf_addr addr; 906e86bddeaSKristof Provost u_int16_t port; 907e86bddeaSKristof Provost } divert; 9081a3e98a5SMateusz Guzik u_int8_t md5sum[PF_MD5_DIGEST_LENGTH]; 909bd7762c8SMateusz Guzik RB_ENTRY(pf_krule) entry_global; 91002cf67ccSMateusz Guzik 91102cf67ccSMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER 91202cf67ccSMateusz Guzik LIST_ENTRY(pf_krule) allrulelist; 91302cf67ccSMateusz Guzik bool allrulelinked; 91402cf67ccSMateusz Guzik #endif 915e86bddeaSKristof Provost }; 916e86bddeaSKristof Provost 91739282ef3SKajetan Staszkiewicz struct pf_krule_item { 91839282ef3SKajetan Staszkiewicz SLIST_ENTRY(pf_krule_item) entry; 91939282ef3SKajetan Staszkiewicz struct pf_krule *r; 92039282ef3SKajetan Staszkiewicz }; 92139282ef3SKajetan Staszkiewicz 92239282ef3SKajetan Staszkiewicz SLIST_HEAD(pf_krule_slist, pf_krule_item); 92339282ef3SKajetan Staszkiewicz 92417ad7334SKristof Provost struct pf_ksrc_node { 92517ad7334SKristof Provost LIST_ENTRY(pf_ksrc_node) entry; 9263b3a8eb9SGleb Smirnoff struct pf_addr addr; 9273b3a8eb9SGleb Smirnoff struct pf_addr raddr; 92839282ef3SKajetan Staszkiewicz struct pf_krule_slist match_rules; 929e5c64b26SKajetan Staszkiewicz struct pf_krule *rule; 930d10de21fSKajetan Staszkiewicz struct pfi_kkif *rkif; 931fbbf270eSKristof Provost counter_u64_t bytes[2]; 932fbbf270eSKristof Provost counter_u64_t packets[2]; 9333b3a8eb9SGleb Smirnoff u_int32_t states; 9343b3a8eb9SGleb Smirnoff u_int32_t conn; 9353b3a8eb9SGleb Smirnoff struct pf_threshold conn_rate; 9363b3a8eb9SGleb Smirnoff u_int32_t creation; 9373b3a8eb9SGleb Smirnoff u_int32_t expire; 9383b3a8eb9SGleb Smirnoff sa_family_t af; 939aa69fdf1SKristof Provost sa_family_t naf; 9403b3a8eb9SGleb Smirnoff u_int8_t ruletype; 94107e070efSKajetan Staszkiewicz pf_sn_types_t type; 942db0a2bfdSKajetan Staszkiewicz struct mtx *lock; 9433b3a8eb9SGleb Smirnoff }; 94417ad7334SKristof Provost #endif 9453b3a8eb9SGleb Smirnoff 9463b3a8eb9SGleb Smirnoff struct pf_state_scrub { 9473b3a8eb9SGleb Smirnoff struct timeval pfss_last; /* time received last packet */ 9483b3a8eb9SGleb Smirnoff u_int32_t pfss_tsecr; /* last echoed timestamp */ 9493b3a8eb9SGleb Smirnoff u_int32_t pfss_tsval; /* largest timestamp */ 9503b3a8eb9SGleb Smirnoff u_int32_t pfss_tsval0; /* original timestamp */ 9513b3a8eb9SGleb Smirnoff u_int16_t pfss_flags; 9523b3a8eb9SGleb Smirnoff #define PFSS_TIMESTAMP 0x0001 /* modulate timestamp */ 9533b3a8eb9SGleb Smirnoff #define PFSS_PAWS 0x0010 /* stricter PAWS checks */ 9543b3a8eb9SGleb Smirnoff #define PFSS_PAWS_IDLED 0x0020 /* was idle too long. no PAWS */ 9553b3a8eb9SGleb Smirnoff #define PFSS_DATA_TS 0x0040 /* timestamp on data packets */ 9563b3a8eb9SGleb Smirnoff #define PFSS_DATA_NOTS 0x0080 /* no timestamp on data packets */ 9573b3a8eb9SGleb Smirnoff u_int8_t pfss_ttl; /* stashed TTL */ 9583b3a8eb9SGleb Smirnoff u_int8_t pad; 95951a78dd2SKristof Provost union { 9603b3a8eb9SGleb Smirnoff u_int32_t pfss_ts_mod; /* timestamp modulation */ 96151a78dd2SKristof Provost u_int32_t pfss_v_tag; /* SCTP verification tag */ 96251a78dd2SKristof Provost }; 9633b3a8eb9SGleb Smirnoff }; 9643b3a8eb9SGleb Smirnoff 9653b3a8eb9SGleb Smirnoff struct pf_state_host { 9663b3a8eb9SGleb Smirnoff struct pf_addr addr; 9673b3a8eb9SGleb Smirnoff u_int16_t port; 9683b3a8eb9SGleb Smirnoff u_int16_t pad; 9693b3a8eb9SGleb Smirnoff }; 9703b3a8eb9SGleb Smirnoff 9713b3a8eb9SGleb Smirnoff struct pf_state_peer { 9723b3a8eb9SGleb Smirnoff struct pf_state_scrub *scrub; /* state is scrubbed */ 9733b3a8eb9SGleb Smirnoff u_int32_t seqlo; /* Max sequence number sent */ 9743b3a8eb9SGleb Smirnoff u_int32_t seqhi; /* Max the other end ACKd + win */ 9753b3a8eb9SGleb Smirnoff u_int32_t seqdiff; /* Sequence number modulator */ 9763b3a8eb9SGleb Smirnoff u_int16_t max_win; /* largest window (pre scaling) */ 9773b3a8eb9SGleb Smirnoff u_int16_t mss; /* Maximum segment size option */ 9783b3a8eb9SGleb Smirnoff u_int8_t state; /* active state level */ 9793b3a8eb9SGleb Smirnoff u_int8_t wscale; /* window scaling factor */ 9803b3a8eb9SGleb Smirnoff u_int8_t tcp_est; /* Did we reach TCPS_ESTABLISHED */ 9813b3a8eb9SGleb Smirnoff u_int8_t pad[1]; 9823b3a8eb9SGleb Smirnoff }; 9833b3a8eb9SGleb Smirnoff 984390dc369STom Jones /* Keep synced with struct pf_udp_endpoint. */ 985390dc369STom Jones struct pf_udp_endpoint_cmp { 986390dc369STom Jones struct pf_addr addr; 987390dc369STom Jones uint16_t port; 988390dc369STom Jones sa_family_t af; 989390dc369STom Jones uint8_t pad[1]; 990390dc369STom Jones }; 991390dc369STom Jones 992390dc369STom Jones struct pf_udp_endpoint { 993390dc369STom Jones struct pf_addr addr; 994390dc369STom Jones uint16_t port; 995390dc369STom Jones sa_family_t af; 996390dc369STom Jones uint8_t pad[1]; 997390dc369STom Jones 998390dc369STom Jones struct pf_udp_mapping *mapping; 999390dc369STom Jones LIST_ENTRY(pf_udp_endpoint) entry; 1000390dc369STom Jones }; 1001390dc369STom Jones 1002390dc369STom Jones struct pf_udp_mapping { 1003390dc369STom Jones struct pf_udp_endpoint endpoints[2]; 1004390dc369STom Jones u_int refs; 1005390dc369STom Jones }; 1006390dc369STom Jones 10073b3a8eb9SGleb Smirnoff /* Keep synced with struct pf_state_key. */ 10083b3a8eb9SGleb Smirnoff struct pf_state_key_cmp { 10093b3a8eb9SGleb Smirnoff struct pf_addr addr[2]; 10103b3a8eb9SGleb Smirnoff u_int16_t port[2]; 10113b3a8eb9SGleb Smirnoff sa_family_t af; 10123b3a8eb9SGleb Smirnoff u_int8_t proto; 10133b3a8eb9SGleb Smirnoff u_int8_t pad[2]; 10143b3a8eb9SGleb Smirnoff }; 10153b3a8eb9SGleb Smirnoff 10163b3a8eb9SGleb Smirnoff struct pf_state_key { 10173b3a8eb9SGleb Smirnoff struct pf_addr addr[2]; 10183b3a8eb9SGleb Smirnoff u_int16_t port[2]; 10193b3a8eb9SGleb Smirnoff sa_family_t af; 10203b3a8eb9SGleb Smirnoff u_int8_t proto; 10213b3a8eb9SGleb Smirnoff u_int8_t pad[2]; 10223b3a8eb9SGleb Smirnoff 10233b3a8eb9SGleb Smirnoff LIST_ENTRY(pf_state_key) entry; 1024211cddf9SKristof Provost TAILQ_HEAD(, pf_kstate) states[2]; 10253b3a8eb9SGleb Smirnoff }; 10263b3a8eb9SGleb Smirnoff 1027f6f116cdSKajetan Staszkiewicz #define PF_REVERSED_KEY(state, family) \ 1028f6f116cdSKajetan Staszkiewicz (((state)->key[PF_SK_WIRE]->af != (state)->key[PF_SK_STACK]->af) && \ 1029f6f116cdSKajetan Staszkiewicz ((state)->key[PF_SK_WIRE]->af != (family)) && \ 1030f6f116cdSKajetan Staszkiewicz ((state)->direction == PF_IN)) 1031fcdb520cSKristof Provost 1032211cddf9SKristof Provost /* Keep synced with struct pf_kstate. */ 10333b3a8eb9SGleb Smirnoff struct pf_state_cmp { 10343b3a8eb9SGleb Smirnoff u_int64_t id; 10353b3a8eb9SGleb Smirnoff u_int32_t creatorid; 10363b3a8eb9SGleb Smirnoff u_int8_t direction; 10373b3a8eb9SGleb Smirnoff u_int8_t pad[3]; 10383b3a8eb9SGleb Smirnoff }; 10393b3a8eb9SGleb Smirnoff 1040c6bf20a2SKristof Provost struct pf_state_scrub_export { 1041c6bf20a2SKristof Provost uint16_t pfss_flags; 1042c6bf20a2SKristof Provost uint8_t pfss_ttl; /* stashed TTL */ 1043c6bf20a2SKristof Provost #define PF_SCRUB_FLAG_VALID 0x01 1044c6bf20a2SKristof Provost uint8_t scrub_flag; 1045c6bf20a2SKristof Provost uint32_t pfss_ts_mod; /* timestamp modulation */ 1046c6bf20a2SKristof Provost }; 1047c6bf20a2SKristof Provost 1048c6bf20a2SKristof Provost struct pf_state_key_export { 1049c6bf20a2SKristof Provost struct pf_addr addr[2]; 1050c6bf20a2SKristof Provost uint16_t port[2]; 1051c6bf20a2SKristof Provost }; 1052c6bf20a2SKristof Provost 1053c6bf20a2SKristof Provost struct pf_state_peer_export { 1054c6bf20a2SKristof Provost struct pf_state_scrub_export scrub; /* state is scrubbed */ 1055c6bf20a2SKristof Provost uint32_t seqlo; /* Max sequence number sent */ 1056c6bf20a2SKristof Provost uint32_t seqhi; /* Max the other end ACKd + win */ 1057c6bf20a2SKristof Provost uint32_t seqdiff; /* Sequence number modulator */ 1058c6bf20a2SKristof Provost uint16_t max_win; /* largest window (pre scaling) */ 1059c6bf20a2SKristof Provost uint16_t mss; /* Maximum segment size option */ 1060c6bf20a2SKristof Provost uint8_t state; /* active state level */ 1061c6bf20a2SKristof Provost uint8_t wscale; /* window scaling factor */ 1062c6bf20a2SKristof Provost uint8_t dummy[6]; 1063c6bf20a2SKristof Provost }; 1064c6bf20a2SKristof Provost _Static_assert(sizeof(struct pf_state_peer_export) == 32, "size incorrect"); 1065c6bf20a2SKristof Provost 1066c6bf20a2SKristof Provost struct pf_state_export { 1067c6bf20a2SKristof Provost uint64_t version; 1068c45d6b0eSKajetan Staszkiewicz #define PF_STATE_VERSION 20230404 1069c6bf20a2SKristof Provost uint64_t id; 1070c6bf20a2SKristof Provost char ifname[IFNAMSIZ]; 1071c6bf20a2SKristof Provost char orig_ifname[IFNAMSIZ]; 1072c6bf20a2SKristof Provost struct pf_state_key_export key[2]; 1073c6bf20a2SKristof Provost struct pf_state_peer_export src; 1074c6bf20a2SKristof Provost struct pf_state_peer_export dst; 1075c6bf20a2SKristof Provost struct pf_addr rt_addr; 1076c6bf20a2SKristof Provost uint32_t rule; 1077c6bf20a2SKristof Provost uint32_t anchor; 1078c6bf20a2SKristof Provost uint32_t nat_rule; 1079c6bf20a2SKristof Provost uint32_t creation; 1080c6bf20a2SKristof Provost uint32_t expire; 1081c6bf20a2SKristof Provost uint32_t spare0; 1082c6bf20a2SKristof Provost uint64_t packets[2]; 1083c6bf20a2SKristof Provost uint64_t bytes[2]; 1084c6bf20a2SKristof Provost uint32_t creatorid; 1085c6bf20a2SKristof Provost uint32_t spare1; 1086c6bf20a2SKristof Provost sa_family_t af; 1087c6bf20a2SKristof Provost uint8_t proto; 1088c6bf20a2SKristof Provost uint8_t direction; 1089c6bf20a2SKristof Provost uint8_t log; 109039282ef3SKajetan Staszkiewicz uint8_t state_flags_compat; 1091c6bf20a2SKristof Provost uint8_t timeout; 1092c6bf20a2SKristof Provost uint8_t sync_flags; 1093c6bf20a2SKristof Provost uint8_t updates; 109439282ef3SKajetan Staszkiewicz uint16_t state_flags; 1095c45d6b0eSKajetan Staszkiewicz uint16_t qid; 1096c45d6b0eSKajetan Staszkiewicz uint16_t pqid; 1097c45d6b0eSKajetan Staszkiewicz uint16_t dnpipe; 1098c45d6b0eSKajetan Staszkiewicz uint16_t dnrpipe; 1099c45d6b0eSKajetan Staszkiewicz int32_t rtableid; 1100c45d6b0eSKajetan Staszkiewicz uint8_t min_ttl; 1101c45d6b0eSKajetan Staszkiewicz uint8_t set_tos; 1102c45d6b0eSKajetan Staszkiewicz uint16_t max_mss; 1103c45d6b0eSKajetan Staszkiewicz uint8_t set_prio[2]; 1104c45d6b0eSKajetan Staszkiewicz uint8_t rt; 1105c45d6b0eSKajetan Staszkiewicz char rt_ifname[IFNAMSIZ]; 1106c6bf20a2SKristof Provost 1107c45d6b0eSKajetan Staszkiewicz uint8_t spare[72]; 1108c6bf20a2SKristof Provost }; 1109c6bf20a2SKristof Provost _Static_assert(sizeof(struct pf_state_export) == 384, "size incorrect"); 1110c6bf20a2SKristof Provost 1111e86bddeaSKristof Provost #ifdef _KERNEL 1112211cddf9SKristof Provost struct pf_kstate { 1113f9aa757dSMateusz Guzik /* 1114f9aa757dSMateusz Guzik * Area shared with pf_state_cmp 1115f9aa757dSMateusz Guzik */ 11163b3a8eb9SGleb Smirnoff u_int64_t id; 11173b3a8eb9SGleb Smirnoff u_int32_t creatorid; 11183b3a8eb9SGleb Smirnoff u_int8_t direction; 11193b3a8eb9SGleb Smirnoff u_int8_t pad[3]; 1120f9aa757dSMateusz Guzik /* 1121f9aa757dSMateusz Guzik * end of the area 1122f9aa757dSMateusz Guzik */ 11233b3a8eb9SGleb Smirnoff 112439282ef3SKajetan Staszkiewicz u_int16_t state_flags; 11259009d36aSMateusz Guzik u_int8_t timeout; 11269009d36aSMateusz Guzik u_int8_t sync_state; /* PFSYNC_S_x */ 11270078c109SKristof Provost u_int8_t sync_updates; 11283b3a8eb9SGleb Smirnoff u_int refs; 1129907257d6SMateusz Guzik struct mtx *lock; 1130211cddf9SKristof Provost TAILQ_ENTRY(pf_kstate) sync_list; 1131211cddf9SKristof Provost TAILQ_ENTRY(pf_kstate) key_list[2]; 1132211cddf9SKristof Provost LIST_ENTRY(pf_kstate) entry; 11333b3a8eb9SGleb Smirnoff struct pf_state_peer src; 11343b3a8eb9SGleb Smirnoff struct pf_state_peer dst; 113539282ef3SKajetan Staszkiewicz struct pf_krule_slist match_rules; 1136e5c64b26SKajetan Staszkiewicz struct pf_krule *rule; 1137e5c64b26SKajetan Staszkiewicz struct pf_krule *anchor; 1138e5c64b26SKajetan Staszkiewicz struct pf_krule *nat_rule; 11393b3a8eb9SGleb Smirnoff struct pf_state_key *key[2]; /* addresses stack and wire */ 1140390dc369STom Jones struct pf_udp_mapping *udp_mapping; 1141320c1116SKristof Provost struct pfi_kkif *kif; 1142d0fdf2b2SKristof Provost struct pfi_kkif *orig_kif; /* The real kif, even if we're a floating state (i.e. if == V_pfi_all). */ 114307e070efSKajetan Staszkiewicz struct pf_ksrc_node *sns[PF_SN_MAX];/* source nodes */ 114455cc305dSMateusz Guzik u_int64_t packets[2]; 114555cc305dSMateusz Guzik u_int64_t bytes[2]; 114604932601SKristof Provost u_int64_t creation; 114704932601SKristof Provost u_int64_t expire; 11483b3a8eb9SGleb Smirnoff u_int32_t pfsync_time; 11496b4ed16dSKajetan Staszkiewicz struct pf_rule_actions act; 11503b3a8eb9SGleb Smirnoff u_int16_t tag; 1151f92d9b1aSKristof Provost u_int16_t if_index_in; 1152f92d9b1aSKristof Provost u_int16_t if_index_out; 11533b3a8eb9SGleb Smirnoff }; 1154e6dd0e2eSMateusz Guzik 1155e6dd0e2eSMateusz Guzik /* 115607e070efSKajetan Staszkiewicz * 6 cache lines per struct, 10 structs per page. 115707e070efSKajetan Staszkiewicz * Try to not grow the struct beyond that. 1158e6dd0e2eSMateusz Guzik */ 115907e070efSKajetan Staszkiewicz _Static_assert(sizeof(struct pf_kstate) <= 384, "pf_kstate size crosses 384 bytes"); 1160e86bddeaSKristof Provost #endif 11613b3a8eb9SGleb Smirnoff 11623b3a8eb9SGleb Smirnoff /* 11633b3a8eb9SGleb Smirnoff * Unified state structures for pulling states out of the kernel 11643b3a8eb9SGleb Smirnoff * used by pfsync(4) and the pf(4) ioctl. 11653b3a8eb9SGleb Smirnoff */ 11663b3a8eb9SGleb Smirnoff struct pfsync_state_scrub { 11673b3a8eb9SGleb Smirnoff u_int16_t pfss_flags; 11683b3a8eb9SGleb Smirnoff u_int8_t pfss_ttl; /* stashed TTL */ 11693b3a8eb9SGleb Smirnoff #define PFSYNC_SCRUB_FLAG_VALID 0x01 11703b3a8eb9SGleb Smirnoff u_int8_t scrub_flag; 11713b3a8eb9SGleb Smirnoff u_int32_t pfss_ts_mod; /* timestamp modulation */ 11723b3a8eb9SGleb Smirnoff } __packed; 11733b3a8eb9SGleb Smirnoff 11743b3a8eb9SGleb Smirnoff struct pfsync_state_peer { 11753b3a8eb9SGleb Smirnoff struct pfsync_state_scrub scrub; /* state is scrubbed */ 11763b3a8eb9SGleb Smirnoff u_int32_t seqlo; /* Max sequence number sent */ 11773b3a8eb9SGleb Smirnoff u_int32_t seqhi; /* Max the other end ACKd + win */ 11783b3a8eb9SGleb Smirnoff u_int32_t seqdiff; /* Sequence number modulator */ 11793b3a8eb9SGleb Smirnoff u_int16_t max_win; /* largest window (pre scaling) */ 11803b3a8eb9SGleb Smirnoff u_int16_t mss; /* Maximum segment size option */ 11813b3a8eb9SGleb Smirnoff u_int8_t state; /* active state level */ 11823b3a8eb9SGleb Smirnoff u_int8_t wscale; /* window scaling factor */ 11833b3a8eb9SGleb Smirnoff u_int8_t pad[6]; 11843b3a8eb9SGleb Smirnoff } __packed; 11853b3a8eb9SGleb Smirnoff 11863b3a8eb9SGleb Smirnoff struct pfsync_state_key { 11873b3a8eb9SGleb Smirnoff struct pf_addr addr[2]; 11883b3a8eb9SGleb Smirnoff u_int16_t port[2]; 11893b3a8eb9SGleb Smirnoff }; 11903b3a8eb9SGleb Smirnoff 11914bf98559SKajetan Staszkiewicz struct pfsync_state_1301 { 11924bf98559SKajetan Staszkiewicz u_int64_t id; 11934bf98559SKajetan Staszkiewicz char ifname[IFNAMSIZ]; 11944bf98559SKajetan Staszkiewicz struct pfsync_state_key key[2]; 11954bf98559SKajetan Staszkiewicz struct pfsync_state_peer src; 11964bf98559SKajetan Staszkiewicz struct pfsync_state_peer dst; 11974bf98559SKajetan Staszkiewicz struct pf_addr rt_addr; 11984bf98559SKajetan Staszkiewicz u_int32_t rule; 11994bf98559SKajetan Staszkiewicz u_int32_t anchor; 12004bf98559SKajetan Staszkiewicz u_int32_t nat_rule; 12014bf98559SKajetan Staszkiewicz u_int32_t creation; 12024bf98559SKajetan Staszkiewicz u_int32_t expire; 12034bf98559SKajetan Staszkiewicz u_int32_t packets[2][2]; 12044bf98559SKajetan Staszkiewicz u_int32_t bytes[2][2]; 12054bf98559SKajetan Staszkiewicz u_int32_t creatorid; 12064bf98559SKajetan Staszkiewicz sa_family_t af; 12074bf98559SKajetan Staszkiewicz u_int8_t proto; 12084bf98559SKajetan Staszkiewicz u_int8_t direction; 12094bf98559SKajetan Staszkiewicz u_int8_t __spare[2]; 12104bf98559SKajetan Staszkiewicz u_int8_t log; 12114bf98559SKajetan Staszkiewicz u_int8_t state_flags; 12124bf98559SKajetan Staszkiewicz u_int8_t timeout; 12134bf98559SKajetan Staszkiewicz u_int8_t sync_flags; 12144bf98559SKajetan Staszkiewicz u_int8_t updates; 12154bf98559SKajetan Staszkiewicz } __packed; 12164bf98559SKajetan Staszkiewicz 12174bf98559SKajetan Staszkiewicz struct pfsync_state_1400 { 12184bf98559SKajetan Staszkiewicz /* The beginning of the struct is compatible with previous versions */ 12193b3a8eb9SGleb Smirnoff u_int64_t id; 12203b3a8eb9SGleb Smirnoff char ifname[IFNAMSIZ]; 12213b3a8eb9SGleb Smirnoff struct pfsync_state_key key[2]; 12223b3a8eb9SGleb Smirnoff struct pfsync_state_peer src; 12233b3a8eb9SGleb Smirnoff struct pfsync_state_peer dst; 12243b3a8eb9SGleb Smirnoff struct pf_addr rt_addr; 12253b3a8eb9SGleb Smirnoff u_int32_t rule; 12263b3a8eb9SGleb Smirnoff u_int32_t anchor; 12273b3a8eb9SGleb Smirnoff u_int32_t nat_rule; 12283b3a8eb9SGleb Smirnoff u_int32_t creation; 12293b3a8eb9SGleb Smirnoff u_int32_t expire; 12303b3a8eb9SGleb Smirnoff u_int32_t packets[2][2]; 12313b3a8eb9SGleb Smirnoff u_int32_t bytes[2][2]; 12323b3a8eb9SGleb Smirnoff u_int32_t creatorid; 12333b3a8eb9SGleb Smirnoff sa_family_t af; 12343b3a8eb9SGleb Smirnoff u_int8_t proto; 12353b3a8eb9SGleb Smirnoff u_int8_t direction; 123639282ef3SKajetan Staszkiewicz u_int16_t state_flags; 12373b3a8eb9SGleb Smirnoff u_int8_t log; 12384bf98559SKajetan Staszkiewicz u_int8_t __spare; 12393b3a8eb9SGleb Smirnoff u_int8_t timeout; 12403b3a8eb9SGleb Smirnoff u_int8_t sync_flags; 12413b3a8eb9SGleb Smirnoff u_int8_t updates; 12424bf98559SKajetan Staszkiewicz /* The rest is not */ 12434bf98559SKajetan Staszkiewicz u_int16_t qid; 12444bf98559SKajetan Staszkiewicz u_int16_t pqid; 12454bf98559SKajetan Staszkiewicz u_int16_t dnpipe; 12464bf98559SKajetan Staszkiewicz u_int16_t dnrpipe; 12474bf98559SKajetan Staszkiewicz int32_t rtableid; 12484bf98559SKajetan Staszkiewicz u_int8_t min_ttl; 12494bf98559SKajetan Staszkiewicz u_int8_t set_tos; 12504bf98559SKajetan Staszkiewicz u_int16_t max_mss; 12514bf98559SKajetan Staszkiewicz u_int8_t set_prio[2]; 12524bf98559SKajetan Staszkiewicz u_int8_t rt; 12534bf98559SKajetan Staszkiewicz char rt_ifname[IFNAMSIZ]; 12544bf98559SKajetan Staszkiewicz 12554bf98559SKajetan Staszkiewicz } __packed; 12564bf98559SKajetan Staszkiewicz 12574bf98559SKajetan Staszkiewicz union pfsync_state_union { 12584bf98559SKajetan Staszkiewicz struct pfsync_state_1301 pfs_1301; 12594bf98559SKajetan Staszkiewicz struct pfsync_state_1400 pfs_1400; 12603b3a8eb9SGleb Smirnoff } __packed; 12613b3a8eb9SGleb Smirnoff 12623b3a8eb9SGleb Smirnoff #ifdef _KERNEL 12633b3a8eb9SGleb Smirnoff /* pfsync */ 12644bf98559SKajetan Staszkiewicz typedef int pfsync_state_import_t(union pfsync_state_union *, int, int); 1265211cddf9SKristof Provost typedef void pfsync_insert_state_t(struct pf_kstate *); 1266211cddf9SKristof Provost typedef void pfsync_update_state_t(struct pf_kstate *); 1267211cddf9SKristof Provost typedef void pfsync_delete_state_t(struct pf_kstate *); 12683b3a8eb9SGleb Smirnoff typedef void pfsync_clear_states_t(u_int32_t, const char *); 1269211cddf9SKristof Provost typedef int pfsync_defer_t(struct pf_kstate *, struct mbuf *); 1270fbbf436dSKristof Provost typedef void pfsync_detach_ifnet_t(struct ifnet *); 1271baf9b6d0SKristof Provost typedef void pflow_export_state_t(const struct pf_kstate *); 12727d381d0aSKristof Provost typedef bool pf_addr_filter_func_t(const sa_family_t, const struct pf_addr *); 12733b3a8eb9SGleb Smirnoff 12745f6cf24eSKristof Provost VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr); 12755f6cf24eSKristof Provost #define V_pfsync_state_import_ptr VNET(pfsync_state_import_ptr) 12765f6cf24eSKristof Provost VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr); 12775f6cf24eSKristof Provost #define V_pfsync_insert_state_ptr VNET(pfsync_insert_state_ptr) 12785f6cf24eSKristof Provost VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr); 12795f6cf24eSKristof Provost #define V_pfsync_update_state_ptr VNET(pfsync_update_state_ptr) 12805f6cf24eSKristof Provost VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr); 12815f6cf24eSKristof Provost #define V_pfsync_delete_state_ptr VNET(pfsync_delete_state_ptr) 12825f6cf24eSKristof Provost VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr); 12835f6cf24eSKristof Provost #define V_pfsync_clear_states_ptr VNET(pfsync_clear_states_ptr) 12845f6cf24eSKristof Provost VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr); 12855f6cf24eSKristof Provost #define V_pfsync_defer_ptr VNET(pfsync_defer_ptr) 1286baf9b6d0SKristof Provost VNET_DECLARE(pflow_export_state_t *, pflow_export_state_ptr); 1287baf9b6d0SKristof Provost #define V_pflow_export_state_ptr VNET(pflow_export_state_ptr) 1288fbbf436dSKristof Provost extern pfsync_detach_ifnet_t *pfsync_detach_ifnet_ptr; 12893b3a8eb9SGleb Smirnoff 12904bf98559SKajetan Staszkiewicz void pfsync_state_export(union pfsync_state_union *, 12914bf98559SKajetan Staszkiewicz struct pf_kstate *, int); 1292c6bf20a2SKristof Provost void pf_state_export(struct pf_state_export *, 1293c6bf20a2SKristof Provost struct pf_kstate *); 12943b3a8eb9SGleb Smirnoff 12953b3a8eb9SGleb Smirnoff /* pflog */ 1296e86bddeaSKristof Provost struct pf_kruleset; 12973b3a8eb9SGleb Smirnoff struct pf_pdesc; 12989a405864SKristof Provost typedef int pflog_packet_t(uint8_t, u_int8_t, 1299b4a42589SKristof Provost struct pf_krule *, struct pf_krule *, struct pf_kruleset *, 13001ba1e152SKristof Provost struct pf_pdesc *, int, struct pf_krule *); 13013b3a8eb9SGleb Smirnoff extern pflog_packet_t *pflog_packet_ptr; 13023b3a8eb9SGleb Smirnoff 13033b3a8eb9SGleb Smirnoff #endif /* _KERNEL */ 13043b3a8eb9SGleb Smirnoff 13053b3a8eb9SGleb Smirnoff #define PFSYNC_FLAG_SRCNODE 0x04 13063b3a8eb9SGleb Smirnoff #define PFSYNC_FLAG_NATSRCNODE 0x08 13073b3a8eb9SGleb Smirnoff 13083b3a8eb9SGleb Smirnoff /* for copies to/from network byte order */ 13093b3a8eb9SGleb Smirnoff /* ioctl interface also uses network byte order */ 13103b3a8eb9SGleb Smirnoff #define pf_state_peer_hton(s,d) do { \ 13113b3a8eb9SGleb Smirnoff (d)->seqlo = htonl((s)->seqlo); \ 13123b3a8eb9SGleb Smirnoff (d)->seqhi = htonl((s)->seqhi); \ 13133b3a8eb9SGleb Smirnoff (d)->seqdiff = htonl((s)->seqdiff); \ 13143b3a8eb9SGleb Smirnoff (d)->max_win = htons((s)->max_win); \ 13153b3a8eb9SGleb Smirnoff (d)->mss = htons((s)->mss); \ 13163b3a8eb9SGleb Smirnoff (d)->state = (s)->state; \ 13173b3a8eb9SGleb Smirnoff (d)->wscale = (s)->wscale; \ 13183b3a8eb9SGleb Smirnoff if ((s)->scrub) { \ 13193b3a8eb9SGleb Smirnoff (d)->scrub.pfss_flags = \ 13203b3a8eb9SGleb Smirnoff htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP); \ 13213b3a8eb9SGleb Smirnoff (d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl; \ 13223b3a8eb9SGleb Smirnoff (d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\ 13233b3a8eb9SGleb Smirnoff (d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID; \ 13243b3a8eb9SGleb Smirnoff } \ 13253b3a8eb9SGleb Smirnoff } while (0) 13263b3a8eb9SGleb Smirnoff 13273b3a8eb9SGleb Smirnoff #define pf_state_peer_ntoh(s,d) do { \ 13283b3a8eb9SGleb Smirnoff (d)->seqlo = ntohl((s)->seqlo); \ 13293b3a8eb9SGleb Smirnoff (d)->seqhi = ntohl((s)->seqhi); \ 13303b3a8eb9SGleb Smirnoff (d)->seqdiff = ntohl((s)->seqdiff); \ 13313b3a8eb9SGleb Smirnoff (d)->max_win = ntohs((s)->max_win); \ 13323b3a8eb9SGleb Smirnoff (d)->mss = ntohs((s)->mss); \ 13333b3a8eb9SGleb Smirnoff (d)->state = (s)->state; \ 13343b3a8eb9SGleb Smirnoff (d)->wscale = (s)->wscale; \ 13353b3a8eb9SGleb Smirnoff if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && \ 13363b3a8eb9SGleb Smirnoff (d)->scrub != NULL) { \ 13373b3a8eb9SGleb Smirnoff (d)->scrub->pfss_flags = \ 13383b3a8eb9SGleb Smirnoff ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP; \ 13393b3a8eb9SGleb Smirnoff (d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl; \ 13403b3a8eb9SGleb Smirnoff (d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\ 13413b3a8eb9SGleb Smirnoff } \ 13423b3a8eb9SGleb Smirnoff } while (0) 13433b3a8eb9SGleb Smirnoff 13443b3a8eb9SGleb Smirnoff #define pf_state_counter_hton(s,d) do { \ 13453b3a8eb9SGleb Smirnoff d[0] = htonl((s>>32)&0xffffffff); \ 13463b3a8eb9SGleb Smirnoff d[1] = htonl(s&0xffffffff); \ 13473b3a8eb9SGleb Smirnoff } while (0) 13483b3a8eb9SGleb Smirnoff 13493b3a8eb9SGleb Smirnoff #define pf_state_counter_from_pfsync(s) \ 13503b3a8eb9SGleb Smirnoff (((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1])) 13513b3a8eb9SGleb Smirnoff 13523b3a8eb9SGleb Smirnoff #define pf_state_counter_ntoh(s,d) do { \ 13533b3a8eb9SGleb Smirnoff d = ntohl(s[0]); \ 13543b3a8eb9SGleb Smirnoff d = d<<32; \ 13553b3a8eb9SGleb Smirnoff d += ntohl(s[1]); \ 13563b3a8eb9SGleb Smirnoff } while (0) 13573b3a8eb9SGleb Smirnoff 1358e86bddeaSKristof Provost TAILQ_HEAD(pf_krulequeue, pf_krule); 13593b3a8eb9SGleb Smirnoff 1360e86bddeaSKristof Provost struct pf_kanchor; 13613b3a8eb9SGleb Smirnoff 1362e86bddeaSKristof Provost struct pf_kruleset { 13633b3a8eb9SGleb Smirnoff struct { 1364e86bddeaSKristof Provost struct pf_krulequeue queues[2]; 13653b3a8eb9SGleb Smirnoff struct { 1366e86bddeaSKristof Provost struct pf_krulequeue *ptr; 1367e86bddeaSKristof Provost struct pf_krule **ptr_array; 13683b3a8eb9SGleb Smirnoff u_int32_t rcount; 13693b3a8eb9SGleb Smirnoff u_int32_t ticket; 13703b3a8eb9SGleb Smirnoff int open; 1371bd7762c8SMateusz Guzik struct pf_krule_global *tree; 13723b3a8eb9SGleb Smirnoff } active, inactive; 13733b3a8eb9SGleb Smirnoff } rules[PF_RULESET_MAX]; 1374e86bddeaSKristof Provost struct pf_kanchor *anchor; 13753b3a8eb9SGleb Smirnoff u_int32_t tticket; 13763b3a8eb9SGleb Smirnoff int tables; 13773b3a8eb9SGleb Smirnoff int topen; 13783b3a8eb9SGleb Smirnoff }; 13793b3a8eb9SGleb Smirnoff 1380e86bddeaSKristof Provost RB_HEAD(pf_kanchor_global, pf_kanchor); 1381e86bddeaSKristof Provost RB_HEAD(pf_kanchor_node, pf_kanchor); 1382e86bddeaSKristof Provost struct pf_kanchor { 1383e86bddeaSKristof Provost RB_ENTRY(pf_kanchor) entry_global; 1384e86bddeaSKristof Provost RB_ENTRY(pf_kanchor) entry_node; 1385e86bddeaSKristof Provost struct pf_kanchor *parent; 1386e86bddeaSKristof Provost struct pf_kanchor_node children; 13873b3a8eb9SGleb Smirnoff char name[PF_ANCHOR_NAME_SIZE]; 13883b3a8eb9SGleb Smirnoff char path[MAXPATHLEN]; 1389e86bddeaSKristof Provost struct pf_kruleset ruleset; 13903b3a8eb9SGleb Smirnoff int refcnt; /* anchor rules */ 13913b3a8eb9SGleb Smirnoff }; 1392e86bddeaSKristof Provost RB_PROTOTYPE(pf_kanchor_global, pf_kanchor, entry_global, pf_anchor_compare); 1393e86bddeaSKristof Provost RB_PROTOTYPE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare); 13943b3a8eb9SGleb Smirnoff 13953b3a8eb9SGleb Smirnoff #define PF_RESERVED_ANCHOR "_pf" 13963b3a8eb9SGleb Smirnoff 13973b3a8eb9SGleb Smirnoff #define PFR_TFLAG_PERSIST 0x00000001 13983b3a8eb9SGleb Smirnoff #define PFR_TFLAG_CONST 0x00000002 13993b3a8eb9SGleb Smirnoff #define PFR_TFLAG_ACTIVE 0x00000004 14003b3a8eb9SGleb Smirnoff #define PFR_TFLAG_INACTIVE 0x00000008 14013b3a8eb9SGleb Smirnoff #define PFR_TFLAG_REFERENCED 0x00000010 14023b3a8eb9SGleb Smirnoff #define PFR_TFLAG_REFDANCHOR 0x00000020 14033b3a8eb9SGleb Smirnoff #define PFR_TFLAG_COUNTERS 0x00000040 14043b3a8eb9SGleb Smirnoff /* Adjust masks below when adding flags. */ 14053b3a8eb9SGleb Smirnoff #define PFR_TFLAG_USRMASK (PFR_TFLAG_PERSIST | \ 14063b3a8eb9SGleb Smirnoff PFR_TFLAG_CONST | \ 14073b3a8eb9SGleb Smirnoff PFR_TFLAG_COUNTERS) 14083b3a8eb9SGleb Smirnoff #define PFR_TFLAG_SETMASK (PFR_TFLAG_ACTIVE | \ 14093b3a8eb9SGleb Smirnoff PFR_TFLAG_INACTIVE | \ 14103b3a8eb9SGleb Smirnoff PFR_TFLAG_REFERENCED | \ 14113b3a8eb9SGleb Smirnoff PFR_TFLAG_REFDANCHOR) 14123b3a8eb9SGleb Smirnoff #define PFR_TFLAG_ALLMASK (PFR_TFLAG_PERSIST | \ 14133b3a8eb9SGleb Smirnoff PFR_TFLAG_CONST | \ 14143b3a8eb9SGleb Smirnoff PFR_TFLAG_ACTIVE | \ 14153b3a8eb9SGleb Smirnoff PFR_TFLAG_INACTIVE | \ 14163b3a8eb9SGleb Smirnoff PFR_TFLAG_REFERENCED | \ 14173b3a8eb9SGleb Smirnoff PFR_TFLAG_REFDANCHOR | \ 14183b3a8eb9SGleb Smirnoff PFR_TFLAG_COUNTERS) 14193b3a8eb9SGleb Smirnoff 1420e86bddeaSKristof Provost struct pf_kanchor_stackframe; 1421c5131afeSKristof Provost struct pf_keth_anchor_stackframe; 14221d6139c0SGleb Smirnoff 14233b3a8eb9SGleb Smirnoff struct pfr_table { 14243b3a8eb9SGleb Smirnoff char pfrt_anchor[MAXPATHLEN]; 14253b3a8eb9SGleb Smirnoff char pfrt_name[PF_TABLE_NAME_SIZE]; 14263b3a8eb9SGleb Smirnoff u_int32_t pfrt_flags; 14273b3a8eb9SGleb Smirnoff u_int8_t pfrt_fback; 14283b3a8eb9SGleb Smirnoff }; 14293b3a8eb9SGleb Smirnoff 14303b3a8eb9SGleb Smirnoff enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED, 14313b3a8eb9SGleb Smirnoff PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE, 14323b3a8eb9SGleb Smirnoff PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX }; 14333b3a8eb9SGleb Smirnoff 14343b3a8eb9SGleb Smirnoff struct pfr_addr { 14353b3a8eb9SGleb Smirnoff union { 14363b3a8eb9SGleb Smirnoff struct in_addr _pfra_ip4addr; 14373b3a8eb9SGleb Smirnoff struct in6_addr _pfra_ip6addr; 14383b3a8eb9SGleb Smirnoff } pfra_u; 14393b3a8eb9SGleb Smirnoff u_int8_t pfra_af; 14403b3a8eb9SGleb Smirnoff u_int8_t pfra_net; 14413b3a8eb9SGleb Smirnoff u_int8_t pfra_not; 14423b3a8eb9SGleb Smirnoff u_int8_t pfra_fback; 14433b3a8eb9SGleb Smirnoff }; 14443b3a8eb9SGleb Smirnoff #define pfra_ip4addr pfra_u._pfra_ip4addr 14453b3a8eb9SGleb Smirnoff #define pfra_ip6addr pfra_u._pfra_ip6addr 14463b3a8eb9SGleb Smirnoff 14473b3a8eb9SGleb Smirnoff enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX }; 14483b3a8eb9SGleb Smirnoff enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX }; 1449c1be8399SMark Johnston enum { PFR_TYPE_PACKETS, PFR_TYPE_BYTES, PFR_TYPE_MAX }; 1450c1be8399SMark Johnston #define PFR_NUM_COUNTERS (PFR_DIR_MAX * PFR_OP_ADDR_MAX * PFR_TYPE_MAX) 14513b3a8eb9SGleb Smirnoff #define PFR_OP_XPASS PFR_OP_ADDR_MAX 14523b3a8eb9SGleb Smirnoff 14533b3a8eb9SGleb Smirnoff struct pfr_astats { 14543b3a8eb9SGleb Smirnoff struct pfr_addr pfras_a; 14553b3a8eb9SGleb Smirnoff u_int64_t pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 14563b3a8eb9SGleb Smirnoff u_int64_t pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX]; 14577e7f8800SKristof Provost time_t pfras_tzero; 14583b3a8eb9SGleb Smirnoff }; 14593b3a8eb9SGleb Smirnoff 14603b3a8eb9SGleb Smirnoff enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX }; 14613b3a8eb9SGleb Smirnoff 14623b3a8eb9SGleb Smirnoff struct pfr_tstats { 14633b3a8eb9SGleb Smirnoff struct pfr_table pfrts_t; 14643b3a8eb9SGleb Smirnoff u_int64_t pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 14653b3a8eb9SGleb Smirnoff u_int64_t pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 14663b3a8eb9SGleb Smirnoff u_int64_t pfrts_match; 14673b3a8eb9SGleb Smirnoff u_int64_t pfrts_nomatch; 14687e7f8800SKristof Provost time_t pfrts_tzero; 14693b3a8eb9SGleb Smirnoff int pfrts_cnt; 14703b3a8eb9SGleb Smirnoff int pfrts_refcnt[PFR_REFCNT_MAX]; 14713b3a8eb9SGleb Smirnoff }; 147259048686SKristof Provost 1473f92c21a2SMateusz Guzik #ifdef _KERNEL 1474f92c21a2SMateusz Guzik 1475f92c21a2SMateusz Guzik struct pfr_kstate_counter { 1476f92c21a2SMateusz Guzik counter_u64_t pkc_pcpu; 1477f92c21a2SMateusz Guzik u_int64_t pkc_zero; 1478f92c21a2SMateusz Guzik }; 1479f92c21a2SMateusz Guzik 1480f92c21a2SMateusz Guzik static inline int 1481f92c21a2SMateusz Guzik pfr_kstate_counter_init(struct pfr_kstate_counter *pfrc, int flags) 1482f92c21a2SMateusz Guzik { 1483f92c21a2SMateusz Guzik 1484f92c21a2SMateusz Guzik pfrc->pkc_zero = 0; 1485f92c21a2SMateusz Guzik pfrc->pkc_pcpu = counter_u64_alloc(flags); 1486f92c21a2SMateusz Guzik if (pfrc->pkc_pcpu == NULL) 1487f92c21a2SMateusz Guzik return (ENOMEM); 1488f92c21a2SMateusz Guzik return (0); 1489f92c21a2SMateusz Guzik } 1490f92c21a2SMateusz Guzik 1491f92c21a2SMateusz Guzik static inline void 1492f92c21a2SMateusz Guzik pfr_kstate_counter_deinit(struct pfr_kstate_counter *pfrc) 1493f92c21a2SMateusz Guzik { 1494f92c21a2SMateusz Guzik 1495f92c21a2SMateusz Guzik counter_u64_free(pfrc->pkc_pcpu); 1496f92c21a2SMateusz Guzik } 1497f92c21a2SMateusz Guzik 1498f92c21a2SMateusz Guzik static inline u_int64_t 1499f92c21a2SMateusz Guzik pfr_kstate_counter_fetch(struct pfr_kstate_counter *pfrc) 1500f92c21a2SMateusz Guzik { 1501f92c21a2SMateusz Guzik u_int64_t c; 1502f92c21a2SMateusz Guzik 1503f92c21a2SMateusz Guzik c = counter_u64_fetch(pfrc->pkc_pcpu); 1504f92c21a2SMateusz Guzik c -= pfrc->pkc_zero; 1505f92c21a2SMateusz Guzik return (c); 1506f92c21a2SMateusz Guzik } 1507f92c21a2SMateusz Guzik 1508f92c21a2SMateusz Guzik static inline void 1509f92c21a2SMateusz Guzik pfr_kstate_counter_zero(struct pfr_kstate_counter *pfrc) 1510f92c21a2SMateusz Guzik { 1511f92c21a2SMateusz Guzik u_int64_t c; 1512f92c21a2SMateusz Guzik 1513f92c21a2SMateusz Guzik c = counter_u64_fetch(pfrc->pkc_pcpu); 1514f92c21a2SMateusz Guzik pfrc->pkc_zero = c; 1515f92c21a2SMateusz Guzik } 1516f92c21a2SMateusz Guzik 1517f92c21a2SMateusz Guzik static inline void 1518f92c21a2SMateusz Guzik pfr_kstate_counter_add(struct pfr_kstate_counter *pfrc, int64_t n) 1519f92c21a2SMateusz Guzik { 1520f92c21a2SMateusz Guzik 1521f92c21a2SMateusz Guzik counter_u64_add(pfrc->pkc_pcpu, n); 1522f92c21a2SMateusz Guzik } 1523f92c21a2SMateusz Guzik 152459048686SKristof Provost struct pfr_ktstats { 152559048686SKristof Provost struct pfr_table pfrts_t; 1526f92c21a2SMateusz Guzik struct pfr_kstate_counter pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1527f92c21a2SMateusz Guzik struct pfr_kstate_counter pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX]; 1528f92c21a2SMateusz Guzik struct pfr_kstate_counter pfrkts_match; 1529f92c21a2SMateusz Guzik struct pfr_kstate_counter pfrkts_nomatch; 15307e7f8800SKristof Provost time_t pfrkts_tzero; 153159048686SKristof Provost int pfrkts_cnt; 153259048686SKristof Provost int pfrkts_refcnt[PFR_REFCNT_MAX]; 153359048686SKristof Provost }; 1534f92c21a2SMateusz Guzik 1535f92c21a2SMateusz Guzik #endif /* _KERNEL */ 1536f92c21a2SMateusz Guzik 15373b3a8eb9SGleb Smirnoff #define pfrts_name pfrts_t.pfrt_name 15383b3a8eb9SGleb Smirnoff #define pfrts_flags pfrts_t.pfrt_flags 15393b3a8eb9SGleb Smirnoff 15403b3a8eb9SGleb Smirnoff #ifndef _SOCKADDR_UNION_DEFINED 15413b3a8eb9SGleb Smirnoff #define _SOCKADDR_UNION_DEFINED 15423b3a8eb9SGleb Smirnoff union sockaddr_union { 15433b3a8eb9SGleb Smirnoff struct sockaddr sa; 15443b3a8eb9SGleb Smirnoff struct sockaddr_in sin; 15453b3a8eb9SGleb Smirnoff struct sockaddr_in6 sin6; 15463b3a8eb9SGleb Smirnoff }; 15473b3a8eb9SGleb Smirnoff #endif /* _SOCKADDR_UNION_DEFINED */ 15483b3a8eb9SGleb Smirnoff 15493b3a8eb9SGleb Smirnoff struct pfr_kcounters { 1550c1be8399SMark Johnston counter_u64_t pfrkc_counters; 15517e7f8800SKristof Provost time_t pfrkc_tzero; 15523b3a8eb9SGleb Smirnoff }; 1553c1be8399SMark Johnston #define pfr_kentry_counter(kc, dir, op, t) \ 1554c1be8399SMark Johnston ((kc)->pfrkc_counters + \ 1555c1be8399SMark Johnston (dir) * PFR_OP_ADDR_MAX * PFR_TYPE_MAX + (op) * PFR_TYPE_MAX + (t)) 15563b3a8eb9SGleb Smirnoff 1557e86bddeaSKristof Provost #ifdef _KERNEL 15583b3a8eb9SGleb Smirnoff SLIST_HEAD(pfr_kentryworkq, pfr_kentry); 15593b3a8eb9SGleb Smirnoff struct pfr_kentry { 15603b3a8eb9SGleb Smirnoff struct radix_node pfrke_node[2]; 15613b3a8eb9SGleb Smirnoff union sockaddr_union pfrke_sa; 15623b3a8eb9SGleb Smirnoff SLIST_ENTRY(pfr_kentry) pfrke_workq; 156359048686SKristof Provost struct pfr_kcounters pfrke_counters; 15643b3a8eb9SGleb Smirnoff u_int8_t pfrke_af; 15653b3a8eb9SGleb Smirnoff u_int8_t pfrke_net; 15663b3a8eb9SGleb Smirnoff u_int8_t pfrke_not; 15673b3a8eb9SGleb Smirnoff u_int8_t pfrke_mark; 15683b3a8eb9SGleb Smirnoff }; 15693b3a8eb9SGleb Smirnoff 15703b3a8eb9SGleb Smirnoff SLIST_HEAD(pfr_ktableworkq, pfr_ktable); 15713b3a8eb9SGleb Smirnoff RB_HEAD(pfr_ktablehead, pfr_ktable); 15723b3a8eb9SGleb Smirnoff struct pfr_ktable { 157359048686SKristof Provost struct pfr_ktstats pfrkt_kts; 15743b3a8eb9SGleb Smirnoff RB_ENTRY(pfr_ktable) pfrkt_tree; 15753b3a8eb9SGleb Smirnoff SLIST_ENTRY(pfr_ktable) pfrkt_workq; 15763b3a8eb9SGleb Smirnoff struct radix_node_head *pfrkt_ip4; 15773b3a8eb9SGleb Smirnoff struct radix_node_head *pfrkt_ip6; 15783b3a8eb9SGleb Smirnoff struct pfr_ktable *pfrkt_shadow; 15793b3a8eb9SGleb Smirnoff struct pfr_ktable *pfrkt_root; 1580e86bddeaSKristof Provost struct pf_kruleset *pfrkt_rs; 15813b3a8eb9SGleb Smirnoff long pfrkt_larg; 15823b3a8eb9SGleb Smirnoff int pfrkt_nflags; 15833b3a8eb9SGleb Smirnoff }; 158459048686SKristof Provost #define pfrkt_t pfrkt_kts.pfrts_t 15853b3a8eb9SGleb Smirnoff #define pfrkt_name pfrkt_t.pfrt_name 15863b3a8eb9SGleb Smirnoff #define pfrkt_anchor pfrkt_t.pfrt_anchor 15873b3a8eb9SGleb Smirnoff #define pfrkt_ruleset pfrkt_t.pfrt_ruleset 15883b3a8eb9SGleb Smirnoff #define pfrkt_flags pfrkt_t.pfrt_flags 158959048686SKristof Provost #define pfrkt_cnt pfrkt_kts.pfrkts_cnt 159059048686SKristof Provost #define pfrkt_refcnt pfrkt_kts.pfrkts_refcnt 159159048686SKristof Provost #define pfrkt_packets pfrkt_kts.pfrkts_packets 159259048686SKristof Provost #define pfrkt_bytes pfrkt_kts.pfrkts_bytes 159359048686SKristof Provost #define pfrkt_match pfrkt_kts.pfrkts_match 159459048686SKristof Provost #define pfrkt_nomatch pfrkt_kts.pfrkts_nomatch 159559048686SKristof Provost #define pfrkt_tzero pfrkt_kts.pfrkts_tzero 1596e86bddeaSKristof Provost #endif 15973b3a8eb9SGleb Smirnoff 1598320c1116SKristof Provost #ifdef _KERNEL 1599320c1116SKristof Provost struct pfi_kkif { 16003b3a8eb9SGleb Smirnoff char pfik_name[IFNAMSIZ]; 16013b3a8eb9SGleb Smirnoff union { 1602320c1116SKristof Provost RB_ENTRY(pfi_kkif) _pfik_tree; 1603320c1116SKristof Provost LIST_ENTRY(pfi_kkif) _pfik_list; 16043b3a8eb9SGleb Smirnoff } _pfik_glue; 16053b3a8eb9SGleb Smirnoff #define pfik_tree _pfik_glue._pfik_tree 16063b3a8eb9SGleb Smirnoff #define pfik_list _pfik_glue._pfik_list 1607d40d4b3eSMateusz Guzik struct pf_counter_u64 pfik_packets[2][2][2]; 1608d40d4b3eSMateusz Guzik struct pf_counter_u64 pfik_bytes[2][2][2]; 16097e7f8800SKristof Provost time_t pfik_tzero; 16103b3a8eb9SGleb Smirnoff u_int pfik_flags; 16113b3a8eb9SGleb Smirnoff struct ifnet *pfik_ifp; 16123b3a8eb9SGleb Smirnoff struct ifg_group *pfik_group; 16133b3a8eb9SGleb Smirnoff u_int pfik_rulerefs; 16143b3a8eb9SGleb Smirnoff TAILQ_HEAD(, pfi_dynaddr) pfik_dynaddrs; 1615d40d4b3eSMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER 1616d40d4b3eSMateusz Guzik LIST_ENTRY(pfi_kkif) pfik_allkiflist; 1617d40d4b3eSMateusz Guzik #endif 16183b3a8eb9SGleb Smirnoff }; 1619320c1116SKristof Provost #endif 16203b3a8eb9SGleb Smirnoff 16213b3a8eb9SGleb Smirnoff #define PFI_IFLAG_REFS 0x0001 /* has state references */ 16223b3a8eb9SGleb Smirnoff #define PFI_IFLAG_SKIP 0x0100 /* skip filtering on interface */ 1623d2bb1988SKristof Provost #define PFI_IFLAG_ANY 0x0200 /* match any non-loopback interface */ 16243b3a8eb9SGleb Smirnoff 162529698ed9SKristof Provost #ifdef _KERNEL 162610aa9ddbSKristof Provost struct pf_sctp_multihome_job; 162710aa9ddbSKristof Provost TAILQ_HEAD(pf_sctp_multihome_jobs, pf_sctp_multihome_job); 162810aa9ddbSKristof Provost 16293b3a8eb9SGleb Smirnoff struct pf_pdesc { 16303b3a8eb9SGleb Smirnoff struct { 16313b3a8eb9SGleb Smirnoff int done; 16323b3a8eb9SGleb Smirnoff uid_t uid; 16333b3a8eb9SGleb Smirnoff gid_t gid; 16343b3a8eb9SGleb Smirnoff } lookup; 16353b3a8eb9SGleb Smirnoff u_int64_t tot_len; /* Make Mickey money */ 1636d38630f6SKristof Provost union pf_headers { 1637d38630f6SKristof Provost struct tcphdr tcp; 1638d38630f6SKristof Provost struct udphdr udp; 1639010ee43fSKristof Provost struct sctphdr sctp; 1640d38630f6SKristof Provost struct icmp icmp; 16413b3a8eb9SGleb Smirnoff #ifdef INET6 1642d38630f6SKristof Provost struct icmp6_hdr icmp6; 16433b3a8eb9SGleb Smirnoff #endif /* INET6 */ 1644d38630f6SKristof Provost char any[0]; 16453b3a8eb9SGleb Smirnoff } hdr; 16463b3a8eb9SGleb Smirnoff 16472d7e68d5SKristof Provost struct pf_addr nsaddr; /* src address after NAT */ 16482d7e68d5SKristof Provost struct pf_addr ndaddr; /* dst address after NAT */ 16492d7e68d5SKristof Provost 1650b4a42589SKristof Provost struct pfi_kkif *kif; /* incomming interface */ 16519a405864SKristof Provost struct mbuf *m; 1652b4a42589SKristof Provost 16533b3a8eb9SGleb Smirnoff struct pf_addr *src; /* src address */ 16543b3a8eb9SGleb Smirnoff struct pf_addr *dst; /* dst address */ 1655bdea9cbcSKristof Provost struct pf_addr osrc; 1656bdea9cbcSKristof Provost struct pf_addr odst; 1657c56650fdSKristof Provost u_int16_t *pcksum; /* proto cksum */ 16583b3a8eb9SGleb Smirnoff u_int16_t *sport; 16593b3a8eb9SGleb Smirnoff u_int16_t *dport; 1660e11dacbfSKristof Provost u_int16_t osport; 1661e11dacbfSKristof Provost u_int16_t odport; 16622d7e68d5SKristof Provost u_int16_t nsport; /* src port after NAT */ 16632d7e68d5SKristof Provost u_int16_t ndport; /* dst port after NAT */ 16643b3a8eb9SGleb Smirnoff struct pf_mtag *pf_mtag; 1665ef950daaSKristof Provost struct pf_rule_actions act; 16663b3a8eb9SGleb Smirnoff 1667739731b8SKristof Provost u_int32_t off; /* protocol header offset */ 1668fffedd81SKristof Provost bool df; /* IPv4 Don't fragment flag. */ 1669739731b8SKristof Provost u_int32_t hdrlen; /* protocol header length */ 1670739731b8SKristof Provost u_int32_t p_len; /* total length of protocol payload */ 16711941d370SKristof Provost u_int32_t extoff; /* extentsion header offset */ 16721941d370SKristof Provost u_int32_t fragoff; /* fragment header offset */ 16731941d370SKristof Provost u_int32_t jumbolen; /* length from v6 jumbo header */ 167439f615e9SKristof Provost u_int32_t badopts; /* v4 options or v6 routing headers */ 16753b3a8eb9SGleb Smirnoff 16763b3a8eb9SGleb Smirnoff u_int16_t *ip_sum; 16773b3a8eb9SGleb Smirnoff u_int16_t flags; /* Let SCRUB trigger behavior in 16783b3a8eb9SGleb Smirnoff * state code. Easier than tags */ 16793b3a8eb9SGleb Smirnoff #define PFDESC_TCP_NORM 0x0001 /* TCP shall be statefully scrubbed */ 1680288bec2bSKristof Provost u_int16_t virtual_proto; 1681288bec2bSKristof Provost #define PF_VPROTO_FRAGMENT 256 16823b3a8eb9SGleb Smirnoff sa_family_t af; 1683fcdb520cSKristof Provost sa_family_t naf; 16843b3a8eb9SGleb Smirnoff u_int8_t proto; 16853b3a8eb9SGleb Smirnoff u_int8_t tos; 16865c3d74ecSKristof Provost u_int8_t ttl; 16873b3a8eb9SGleb Smirnoff u_int8_t dir; /* direction */ 16883b3a8eb9SGleb Smirnoff u_int8_t sidx; /* key index for source */ 16893b3a8eb9SGleb Smirnoff u_int8_t didx; /* key index for destination */ 1690010ee43fSKristof Provost #define PFDESC_SCTP_INIT 0x0001 1691010ee43fSKristof Provost #define PFDESC_SCTP_INIT_ACK 0x0002 1692010ee43fSKristof Provost #define PFDESC_SCTP_COOKIE 0x0004 16937093414cSKristof Provost #define PFDESC_SCTP_COOKIE_ACK 0x0008 16947093414cSKristof Provost #define PFDESC_SCTP_ABORT 0x0010 16957093414cSKristof Provost #define PFDESC_SCTP_SHUTDOWN 0x0020 16967093414cSKristof Provost #define PFDESC_SCTP_SHUTDOWN_COMPLETE 0x0040 16977093414cSKristof Provost #define PFDESC_SCTP_DATA 0x0080 16987093414cSKristof Provost #define PFDESC_SCTP_ASCONF 0x0100 16997093414cSKristof Provost #define PFDESC_SCTP_HEARTBEAT 0x0200 17007093414cSKristof Provost #define PFDESC_SCTP_HEARTBEAT_ACK 0x0400 17017093414cSKristof Provost #define PFDESC_SCTP_OTHER 0x0800 17027093414cSKristof Provost #define PFDESC_SCTP_ADD_IP 0x1000 1703010ee43fSKristof Provost u_int16_t sctp_flags; 1704d1bc1e9eSKristof Provost u_int32_t sctp_initiate_tag; 1705f3e8653bSKristof Provost u_int16_t sctp_dummy_sum; 1706e4f2733dSKristof Provost struct pf_krule *related_rule; 170710aa9ddbSKristof Provost 170810aa9ddbSKristof Provost struct pf_sctp_multihome_jobs sctp_multihome_jobs; 17093b3a8eb9SGleb Smirnoff }; 171010aa9ddbSKristof Provost 171110aa9ddbSKristof Provost struct pf_sctp_multihome_job { 171210aa9ddbSKristof Provost TAILQ_ENTRY(pf_sctp_multihome_job) next; 171310aa9ddbSKristof Provost struct pf_pdesc pd; 171410aa9ddbSKristof Provost struct pf_addr src; 171510aa9ddbSKristof Provost struct pf_addr dst; 17164d3af82fSKristof Provost int op; 171710aa9ddbSKristof Provost }; 171810aa9ddbSKristof Provost 171929698ed9SKristof Provost #endif 17203b3a8eb9SGleb Smirnoff 17213b3a8eb9SGleb Smirnoff /* flags for RDR options */ 17223b3a8eb9SGleb Smirnoff #define PF_DPORT_RANGE 0x01 /* Dest port uses range */ 17233b3a8eb9SGleb Smirnoff #define PF_RPORT_RANGE 0x02 /* RDR'ed port uses range */ 17243b3a8eb9SGleb Smirnoff 17253b3a8eb9SGleb Smirnoff /* UDP state enumeration */ 17263b3a8eb9SGleb Smirnoff #define PFUDPS_NO_TRAFFIC 0 17273b3a8eb9SGleb Smirnoff #define PFUDPS_SINGLE 1 17283b3a8eb9SGleb Smirnoff #define PFUDPS_MULTIPLE 2 17293b3a8eb9SGleb Smirnoff 17303b3a8eb9SGleb Smirnoff #define PFUDPS_NSTATES 3 /* number of state levels */ 17313b3a8eb9SGleb Smirnoff 17323b3a8eb9SGleb Smirnoff #define PFUDPS_NAMES { \ 17333b3a8eb9SGleb Smirnoff "NO_TRAFFIC", \ 17343b3a8eb9SGleb Smirnoff "SINGLE", \ 17353b3a8eb9SGleb Smirnoff "MULTIPLE", \ 17363b3a8eb9SGleb Smirnoff NULL \ 17373b3a8eb9SGleb Smirnoff } 17383b3a8eb9SGleb Smirnoff 17393b3a8eb9SGleb Smirnoff /* Other protocol state enumeration */ 17403b3a8eb9SGleb Smirnoff #define PFOTHERS_NO_TRAFFIC 0 17413b3a8eb9SGleb Smirnoff #define PFOTHERS_SINGLE 1 17423b3a8eb9SGleb Smirnoff #define PFOTHERS_MULTIPLE 2 17433b3a8eb9SGleb Smirnoff 17443b3a8eb9SGleb Smirnoff #define PFOTHERS_NSTATES 3 /* number of state levels */ 17453b3a8eb9SGleb Smirnoff 17463b3a8eb9SGleb Smirnoff #define PFOTHERS_NAMES { \ 17473b3a8eb9SGleb Smirnoff "NO_TRAFFIC", \ 17483b3a8eb9SGleb Smirnoff "SINGLE", \ 17493b3a8eb9SGleb Smirnoff "MULTIPLE", \ 17503b3a8eb9SGleb Smirnoff NULL \ 17513b3a8eb9SGleb Smirnoff } 17523b3a8eb9SGleb Smirnoff 17533b3a8eb9SGleb Smirnoff #define ACTION_SET(a, x) \ 17543b3a8eb9SGleb Smirnoff do { \ 17553b3a8eb9SGleb Smirnoff if ((a) != NULL) \ 17563b3a8eb9SGleb Smirnoff *(a) = (x); \ 17573b3a8eb9SGleb Smirnoff } while (0) 17583b3a8eb9SGleb Smirnoff 17593b3a8eb9SGleb Smirnoff #define REASON_SET(a, x) \ 17603b3a8eb9SGleb Smirnoff do { \ 17613b3a8eb9SGleb Smirnoff if ((a) != NULL) \ 17623b3a8eb9SGleb Smirnoff *(a) = (x); \ 17633b3a8eb9SGleb Smirnoff if (x < PFRES_MAX) \ 1764a9572d8fSGleb Smirnoff counter_u64_add(V_pf_status.counters[x], 1); \ 17653b3a8eb9SGleb Smirnoff } while (0) 17663b3a8eb9SGleb Smirnoff 17678e1864edSKristof Provost enum pf_syncookies_mode { 17688e1864edSKristof Provost PF_SYNCOOKIES_NEVER = 0, 17698e1864edSKristof Provost PF_SYNCOOKIES_ALWAYS = 1, 1770bf863718SKristof Provost PF_SYNCOOKIES_ADAPTIVE = 2, 1771bf863718SKristof Provost PF_SYNCOOKIES_MODE_MAX = PF_SYNCOOKIES_ADAPTIVE 17728e1864edSKristof Provost }; 17738e1864edSKristof Provost 17745062afffSKristof Provost #define PF_SYNCOOKIES_HIWATPCT 25 17755062afffSKristof Provost #define PF_SYNCOOKIES_LOWATPCT (PF_SYNCOOKIES_HIWATPCT / 2) 17765062afffSKristof Provost 1777d9cc6ea2SMateusz Guzik #ifdef _KERNEL 1778a9572d8fSGleb Smirnoff struct pf_kstatus { 1779a9572d8fSGleb Smirnoff counter_u64_t counters[PFRES_MAX]; /* reason for passing/dropping */ 17804cab80a8SKristof Provost counter_u64_t lcounters[KLCNT_MAX]; /* limit counters */ 1781fc4c42ceSMateusz Guzik struct pf_counter_u64 fcounters[FCNT_MAX]; /* state operation counters */ 1782a9572d8fSGleb Smirnoff counter_u64_t scounters[SCNT_MAX]; /* src_node operation counters */ 1783a9572d8fSGleb Smirnoff uint32_t states; 1784a9572d8fSGleb Smirnoff uint32_t src_nodes; 1785a9572d8fSGleb Smirnoff uint32_t running; 1786a9572d8fSGleb Smirnoff uint32_t since; 1787a9572d8fSGleb Smirnoff uint32_t debug; 1788a9572d8fSGleb Smirnoff uint32_t hostid; 17893b3a8eb9SGleb Smirnoff char ifname[IFNAMSIZ]; 1790a9572d8fSGleb Smirnoff uint8_t pf_chksum[PF_MD5_DIGEST_LENGTH]; 179142ec75f8SKristof Provost bool keep_counters; 17928e1864edSKristof Provost enum pf_syncookies_mode syncookies_mode; 17938e1864edSKristof Provost bool syncookies_active; 1794bf863718SKristof Provost uint64_t syncookies_inflight[2]; 1795bf863718SKristof Provost uint32_t states_halfopen; 179639282ef3SKajetan Staszkiewicz uint32_t reass; 17973b3a8eb9SGleb Smirnoff }; 1798d9cc6ea2SMateusz Guzik #endif 17993b3a8eb9SGleb Smirnoff 18003b3a8eb9SGleb Smirnoff struct pf_divert { 18013b3a8eb9SGleb Smirnoff union { 18023b3a8eb9SGleb Smirnoff struct in_addr ipv4; 18033b3a8eb9SGleb Smirnoff struct in6_addr ipv6; 18043b3a8eb9SGleb Smirnoff } addr; 18053b3a8eb9SGleb Smirnoff u_int16_t port; 18063b3a8eb9SGleb Smirnoff }; 18073b3a8eb9SGleb Smirnoff 18083b3a8eb9SGleb Smirnoff #define PFFRAG_FRENT_HIWAT 5000 /* Number of fragment entries */ 18093b3a8eb9SGleb Smirnoff #define PFR_KENTRY_HIWAT 200000 /* Number of table entries */ 18103b3a8eb9SGleb Smirnoff 18117cae58a4SKristof Provost struct pf_fragment_tag { 18127cae58a4SKristof Provost uint16_t ft_hdrlen; /* header length of reassembled pkt */ 18137cae58a4SKristof Provost uint16_t ft_extoff; /* last extension header offset or 0 */ 18147cae58a4SKristof Provost uint16_t ft_maxlen; /* maximum fragment payload length */ 18157cae58a4SKristof Provost uint32_t ft_id; /* fragment id */ 18167cae58a4SKristof Provost }; 18177cae58a4SKristof Provost 18183b3a8eb9SGleb Smirnoff /* 1819fd2ea405SKristof Provost * Limit the length of the fragment queue traversal. Remember 1820fd2ea405SKristof Provost * search entry points based on the fragment offset. 1821fd2ea405SKristof Provost */ 1822fd2ea405SKristof Provost #define PF_FRAG_ENTRY_POINTS 16 1823fd2ea405SKristof Provost 1824fd2ea405SKristof Provost /* 1825790194cdSKristof Provost * The number of entries in the fragment queue must be limited 1826ef88adc5SGordon Bergling * to avoid DoS by linear searching. Instead of a global limit, 1827790194cdSKristof Provost * use a limit per entry point. For large packets these sum up. 1828790194cdSKristof Provost */ 1829790194cdSKristof Provost #define PF_FRAG_ENTRY_LIMIT 64 1830790194cdSKristof Provost 1831790194cdSKristof Provost /* 18323b3a8eb9SGleb Smirnoff * ioctl parameter structures 18333b3a8eb9SGleb Smirnoff */ 18343b3a8eb9SGleb Smirnoff 18353b3a8eb9SGleb Smirnoff struct pfioc_pooladdr { 18363b3a8eb9SGleb Smirnoff u_int32_t action; 18373b3a8eb9SGleb Smirnoff u_int32_t ticket; 18383b3a8eb9SGleb Smirnoff u_int32_t nr; 18393b3a8eb9SGleb Smirnoff u_int32_t r_num; 18403b3a8eb9SGleb Smirnoff u_int8_t r_action; 18413b3a8eb9SGleb Smirnoff u_int8_t r_last; 18423b3a8eb9SGleb Smirnoff u_int8_t af; 18433b3a8eb9SGleb Smirnoff char anchor[MAXPATHLEN]; 18443b3a8eb9SGleb Smirnoff struct pf_pooladdr addr; 18453b3a8eb9SGleb Smirnoff }; 18463b3a8eb9SGleb Smirnoff 18473b3a8eb9SGleb Smirnoff struct pfioc_rule { 18483b3a8eb9SGleb Smirnoff u_int32_t action; 18493b3a8eb9SGleb Smirnoff u_int32_t ticket; 18503b3a8eb9SGleb Smirnoff u_int32_t pool_ticket; 18513b3a8eb9SGleb Smirnoff u_int32_t nr; 18523b3a8eb9SGleb Smirnoff char anchor[MAXPATHLEN]; 18533b3a8eb9SGleb Smirnoff char anchor_call[MAXPATHLEN]; 18543b3a8eb9SGleb Smirnoff struct pf_rule rule; 18553b3a8eb9SGleb Smirnoff }; 18563b3a8eb9SGleb Smirnoff 18573b3a8eb9SGleb Smirnoff struct pfioc_natlook { 18583b3a8eb9SGleb Smirnoff struct pf_addr saddr; 18593b3a8eb9SGleb Smirnoff struct pf_addr daddr; 18603b3a8eb9SGleb Smirnoff struct pf_addr rsaddr; 18613b3a8eb9SGleb Smirnoff struct pf_addr rdaddr; 18623b3a8eb9SGleb Smirnoff u_int16_t sport; 18633b3a8eb9SGleb Smirnoff u_int16_t dport; 18643b3a8eb9SGleb Smirnoff u_int16_t rsport; 18653b3a8eb9SGleb Smirnoff u_int16_t rdport; 18663b3a8eb9SGleb Smirnoff sa_family_t af; 18673b3a8eb9SGleb Smirnoff u_int8_t proto; 18683b3a8eb9SGleb Smirnoff u_int8_t direction; 18693b3a8eb9SGleb Smirnoff }; 18703b3a8eb9SGleb Smirnoff 18713b3a8eb9SGleb Smirnoff struct pfioc_state { 18724bf98559SKajetan Staszkiewicz struct pfsync_state_1301 state; 18733b3a8eb9SGleb Smirnoff }; 18743b3a8eb9SGleb Smirnoff 18753b3a8eb9SGleb Smirnoff struct pfioc_src_node_kill { 18763b3a8eb9SGleb Smirnoff sa_family_t psnk_af; 18773b3a8eb9SGleb Smirnoff struct pf_rule_addr psnk_src; 18783b3a8eb9SGleb Smirnoff struct pf_rule_addr psnk_dst; 18793b3a8eb9SGleb Smirnoff u_int psnk_killed; 18803b3a8eb9SGleb Smirnoff }; 18813b3a8eb9SGleb Smirnoff 18827606a45dSKristof Provost #ifdef _KERNEL 18837606a45dSKristof Provost struct pf_kstate_kill { 18847606a45dSKristof Provost struct pf_state_cmp psk_pfcmp; 18857606a45dSKristof Provost sa_family_t psk_af; 18867606a45dSKristof Provost int psk_proto; 18877606a45dSKristof Provost struct pf_rule_addr psk_src; 18887606a45dSKristof Provost struct pf_rule_addr psk_dst; 1889abbcba9cSKristof Provost struct pf_rule_addr psk_rt_addr; 18907606a45dSKristof Provost char psk_ifname[IFNAMSIZ]; 18917606a45dSKristof Provost char psk_label[PF_RULE_LABEL_SIZE]; 18927606a45dSKristof Provost u_int psk_killed; 189393abcf17SKristof Provost bool psk_kill_match; 18944f337550SKristof Provost bool psk_nat; 18957606a45dSKristof Provost }; 18967606a45dSKristof Provost #endif 18977606a45dSKristof Provost 18983b3a8eb9SGleb Smirnoff struct pfioc_state_kill { 18993b3a8eb9SGleb Smirnoff struct pf_state_cmp psk_pfcmp; 19003b3a8eb9SGleb Smirnoff sa_family_t psk_af; 19013b3a8eb9SGleb Smirnoff int psk_proto; 19023b3a8eb9SGleb Smirnoff struct pf_rule_addr psk_src; 19033b3a8eb9SGleb Smirnoff struct pf_rule_addr psk_dst; 19043b3a8eb9SGleb Smirnoff char psk_ifname[IFNAMSIZ]; 19053b3a8eb9SGleb Smirnoff char psk_label[PF_RULE_LABEL_SIZE]; 19063b3a8eb9SGleb Smirnoff u_int psk_killed; 19073b3a8eb9SGleb Smirnoff }; 19083b3a8eb9SGleb Smirnoff 19093b3a8eb9SGleb Smirnoff struct pfioc_states { 19103b3a8eb9SGleb Smirnoff int ps_len; 19113b3a8eb9SGleb Smirnoff union { 1912c4a32455SKristof Provost void *ps_buf; 19134bf98559SKajetan Staszkiewicz struct pfsync_state_1301 *ps_states; 19147b676698SKristof Provost }; 19153b3a8eb9SGleb Smirnoff }; 19163b3a8eb9SGleb Smirnoff 1917c6bf20a2SKristof Provost struct pfioc_states_v2 { 1918c6bf20a2SKristof Provost int ps_len; 1919c6bf20a2SKristof Provost uint64_t ps_req_version; 1920c6bf20a2SKristof Provost union { 1921c4a32455SKristof Provost void *ps_buf; 19227b676698SKristof Provost struct pf_state_export *ps_states; 19237b676698SKristof Provost }; 1924c6bf20a2SKristof Provost }; 1925c6bf20a2SKristof Provost 19263b3a8eb9SGleb Smirnoff struct pfioc_src_nodes { 19273b3a8eb9SGleb Smirnoff int psn_len; 19283b3a8eb9SGleb Smirnoff union { 1929c4a32455SKristof Provost void *psn_buf; 19307b676698SKristof Provost struct pf_src_node *psn_src_nodes; 19317b676698SKristof Provost }; 19323b3a8eb9SGleb Smirnoff }; 19333b3a8eb9SGleb Smirnoff 19343b3a8eb9SGleb Smirnoff struct pfioc_if { 19353b3a8eb9SGleb Smirnoff char ifname[IFNAMSIZ]; 19363b3a8eb9SGleb Smirnoff }; 19373b3a8eb9SGleb Smirnoff 19383b3a8eb9SGleb Smirnoff struct pfioc_tm { 19393b3a8eb9SGleb Smirnoff int timeout; 19403b3a8eb9SGleb Smirnoff int seconds; 19413b3a8eb9SGleb Smirnoff }; 19423b3a8eb9SGleb Smirnoff 19433b3a8eb9SGleb Smirnoff struct pfioc_limit { 19443b3a8eb9SGleb Smirnoff int index; 19453b3a8eb9SGleb Smirnoff unsigned limit; 19463b3a8eb9SGleb Smirnoff }; 19473b3a8eb9SGleb Smirnoff 1948249cc75fSPatrick Kelsey struct pfioc_altq_v0 { 19493b3a8eb9SGleb Smirnoff u_int32_t action; 19503b3a8eb9SGleb Smirnoff u_int32_t ticket; 19513b3a8eb9SGleb Smirnoff u_int32_t nr; 1952249cc75fSPatrick Kelsey struct pf_altq_v0 altq; 19533b3a8eb9SGleb Smirnoff }; 19543b3a8eb9SGleb Smirnoff 1955249cc75fSPatrick Kelsey struct pfioc_altq_v1 { 1956249cc75fSPatrick Kelsey u_int32_t action; 1957249cc75fSPatrick Kelsey u_int32_t ticket; 1958249cc75fSPatrick Kelsey u_int32_t nr; 1959249cc75fSPatrick Kelsey /* 1960249cc75fSPatrick Kelsey * Placed here so code that only uses the above parameters can be 1961249cc75fSPatrick Kelsey * written entirely in terms of the v0 or v1 type. 1962249cc75fSPatrick Kelsey */ 1963249cc75fSPatrick Kelsey u_int32_t version; 1964249cc75fSPatrick Kelsey struct pf_altq_v1 altq; 1965249cc75fSPatrick Kelsey }; 1966249cc75fSPatrick Kelsey 1967249cc75fSPatrick Kelsey /* 1968249cc75fSPatrick Kelsey * Latest version of struct pfioc_altq_vX. This must move in lock-step with 1969249cc75fSPatrick Kelsey * the latest version of struct pf_altq_vX as it has that struct as a 1970249cc75fSPatrick Kelsey * member. 1971249cc75fSPatrick Kelsey */ 1972249cc75fSPatrick Kelsey #define PFIOC_ALTQ_VERSION PF_ALTQ_VERSION 1973249cc75fSPatrick Kelsey 1974249cc75fSPatrick Kelsey struct pfioc_qstats_v0 { 19753b3a8eb9SGleb Smirnoff u_int32_t ticket; 19763b3a8eb9SGleb Smirnoff u_int32_t nr; 19773b3a8eb9SGleb Smirnoff void *buf; 19783b3a8eb9SGleb Smirnoff int nbytes; 19793b3a8eb9SGleb Smirnoff u_int8_t scheduler; 19803b3a8eb9SGleb Smirnoff }; 19813b3a8eb9SGleb Smirnoff 1982249cc75fSPatrick Kelsey struct pfioc_qstats_v1 { 1983249cc75fSPatrick Kelsey u_int32_t ticket; 1984249cc75fSPatrick Kelsey u_int32_t nr; 1985249cc75fSPatrick Kelsey void *buf; 1986249cc75fSPatrick Kelsey int nbytes; 1987249cc75fSPatrick Kelsey u_int8_t scheduler; 1988249cc75fSPatrick Kelsey /* 1989249cc75fSPatrick Kelsey * Placed here so code that only uses the above parameters can be 1990249cc75fSPatrick Kelsey * written entirely in terms of the v0 or v1 type. 1991249cc75fSPatrick Kelsey */ 1992249cc75fSPatrick Kelsey u_int32_t version; /* Requested version of stats struct */ 1993249cc75fSPatrick Kelsey }; 1994249cc75fSPatrick Kelsey 1995249cc75fSPatrick Kelsey /* Latest version of struct pfioc_qstats_vX */ 1996249cc75fSPatrick Kelsey #define PFIOC_QSTATS_VERSION 1 1997249cc75fSPatrick Kelsey 19983b3a8eb9SGleb Smirnoff struct pfioc_ruleset { 19993b3a8eb9SGleb Smirnoff u_int32_t nr; 20003b3a8eb9SGleb Smirnoff char path[MAXPATHLEN]; 20013b3a8eb9SGleb Smirnoff char name[PF_ANCHOR_NAME_SIZE]; 20023b3a8eb9SGleb Smirnoff }; 20033b3a8eb9SGleb Smirnoff 20043b3a8eb9SGleb Smirnoff #define PF_RULESET_ALTQ (PF_RULESET_MAX) 20053b3a8eb9SGleb Smirnoff #define PF_RULESET_TABLE (PF_RULESET_MAX+1) 2006e732e742SKristof Provost #define PF_RULESET_ETH (PF_RULESET_MAX+2) 20073b3a8eb9SGleb Smirnoff struct pfioc_trans { 20083b3a8eb9SGleb Smirnoff int size; /* number of elements */ 20093b3a8eb9SGleb Smirnoff int esize; /* size of each element in bytes */ 20103b3a8eb9SGleb Smirnoff struct pfioc_trans_e { 20113b3a8eb9SGleb Smirnoff int rs_num; 20123b3a8eb9SGleb Smirnoff char anchor[MAXPATHLEN]; 20133b3a8eb9SGleb Smirnoff u_int32_t ticket; 20143b3a8eb9SGleb Smirnoff } *array; 20153b3a8eb9SGleb Smirnoff }; 20163b3a8eb9SGleb Smirnoff 20173b3a8eb9SGleb Smirnoff #define PFR_FLAG_ATOMIC 0x00000001 /* unused */ 20183b3a8eb9SGleb Smirnoff #define PFR_FLAG_DUMMY 0x00000002 20193b3a8eb9SGleb Smirnoff #define PFR_FLAG_FEEDBACK 0x00000004 20203b3a8eb9SGleb Smirnoff #define PFR_FLAG_CLSTATS 0x00000008 20213b3a8eb9SGleb Smirnoff #define PFR_FLAG_ADDRSTOO 0x00000010 20223b3a8eb9SGleb Smirnoff #define PFR_FLAG_REPLACE 0x00000020 20233b3a8eb9SGleb Smirnoff #define PFR_FLAG_ALLRSETS 0x00000040 20243b3a8eb9SGleb Smirnoff #define PFR_FLAG_ALLMASK 0x0000007F 20253b3a8eb9SGleb Smirnoff #ifdef _KERNEL 20263b3a8eb9SGleb Smirnoff #define PFR_FLAG_USERIOCTL 0x10000000 20273b3a8eb9SGleb Smirnoff #endif 20283b3a8eb9SGleb Smirnoff 20293b3a8eb9SGleb Smirnoff struct pfioc_table { 20303b3a8eb9SGleb Smirnoff struct pfr_table pfrio_table; 20313b3a8eb9SGleb Smirnoff void *pfrio_buffer; 20323b3a8eb9SGleb Smirnoff int pfrio_esize; 20333b3a8eb9SGleb Smirnoff int pfrio_size; 20343b3a8eb9SGleb Smirnoff int pfrio_size2; 20353b3a8eb9SGleb Smirnoff int pfrio_nadd; 20363b3a8eb9SGleb Smirnoff int pfrio_ndel; 20373b3a8eb9SGleb Smirnoff int pfrio_nchange; 20383b3a8eb9SGleb Smirnoff int pfrio_flags; 20393b3a8eb9SGleb Smirnoff u_int32_t pfrio_ticket; 20403b3a8eb9SGleb Smirnoff }; 20413b3a8eb9SGleb Smirnoff #define pfrio_exists pfrio_nadd 20423b3a8eb9SGleb Smirnoff #define pfrio_nzero pfrio_nadd 20433b3a8eb9SGleb Smirnoff #define pfrio_nmatch pfrio_nadd 20443b3a8eb9SGleb Smirnoff #define pfrio_naddr pfrio_size2 20453b3a8eb9SGleb Smirnoff #define pfrio_setflag pfrio_size2 20463b3a8eb9SGleb Smirnoff #define pfrio_clrflag pfrio_nadd 20473b3a8eb9SGleb Smirnoff 20483b3a8eb9SGleb Smirnoff struct pfioc_iface { 20493b3a8eb9SGleb Smirnoff char pfiio_name[IFNAMSIZ]; 20503b3a8eb9SGleb Smirnoff void *pfiio_buffer; 20513b3a8eb9SGleb Smirnoff int pfiio_esize; 20523b3a8eb9SGleb Smirnoff int pfiio_size; 20533b3a8eb9SGleb Smirnoff int pfiio_nzero; 20543b3a8eb9SGleb Smirnoff int pfiio_flags; 20553b3a8eb9SGleb Smirnoff }; 20563b3a8eb9SGleb Smirnoff 20573b3a8eb9SGleb Smirnoff /* 20583b3a8eb9SGleb Smirnoff * ioctl operations 20593b3a8eb9SGleb Smirnoff */ 20603b3a8eb9SGleb Smirnoff 20613b3a8eb9SGleb Smirnoff #define DIOCSTART _IO ('D', 1) 20623b3a8eb9SGleb Smirnoff #define DIOCSTOP _IO ('D', 2) 20633b3a8eb9SGleb Smirnoff #define DIOCADDRULE _IOWR('D', 4, struct pfioc_rule) 20645c62ededSKristof Provost #define DIOCADDRULENV _IOWR('D', 4, struct pfioc_nv) 20653b3a8eb9SGleb Smirnoff #define DIOCGETRULES _IOWR('D', 6, struct pfioc_rule) 2066d710367dSKristof Provost #define DIOCGETRULENV _IOWR('D', 7, struct pfioc_nv) 20673b3a8eb9SGleb Smirnoff /* XXX cut 8 - 17 */ 20687606a45dSKristof Provost #define DIOCCLRSTATESNV _IOWR('D', 18, struct pfioc_nv) 2069cc535c95SWill Andrews #define DIOCGETSTATE _IOWR('D', 19, struct pfioc_state) 20701732afaaSKristof Provost #define DIOCGETSTATENV _IOWR('D', 19, struct pfioc_nv) 20713b3a8eb9SGleb Smirnoff #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if) 20722b10cf85SKristof Provost #define DIOCGETSTATUSNV _IOWR('D', 21, struct pfioc_nv) 20733b3a8eb9SGleb Smirnoff #define DIOCCLRSTATUS _IO ('D', 22) 20743b3a8eb9SGleb Smirnoff #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook) 20753b3a8eb9SGleb Smirnoff #define DIOCSETDEBUG _IOWR('D', 24, u_int32_t) 2076586fae3cSKristof Provost #ifdef COMPAT_FREEBSD14 20773b3a8eb9SGleb Smirnoff #define DIOCGETSTATES _IOWR('D', 25, struct pfioc_states) 2078586fae3cSKristof Provost #endif 2079cc535c95SWill Andrews #define DIOCCHANGERULE _IOWR('D', 26, struct pfioc_rule) 20803b3a8eb9SGleb Smirnoff /* XXX cut 26 - 28 */ 20813b3a8eb9SGleb Smirnoff #define DIOCSETTIMEOUT _IOWR('D', 29, struct pfioc_tm) 20823b3a8eb9SGleb Smirnoff #define DIOCGETTIMEOUT _IOWR('D', 30, struct pfioc_tm) 2083cc535c95SWill Andrews #define DIOCADDSTATE _IOWR('D', 37, struct pfioc_state) 2084cc535c95SWill Andrews #define DIOCCLRRULECTRS _IO ('D', 38) 20853b3a8eb9SGleb Smirnoff #define DIOCGETLIMIT _IOWR('D', 39, struct pfioc_limit) 20863b3a8eb9SGleb Smirnoff #define DIOCSETLIMIT _IOWR('D', 40, struct pfioc_limit) 2087e989530aSKristof Provost #define DIOCKILLSTATESNV _IOWR('D', 41, struct pfioc_nv) 20883b3a8eb9SGleb Smirnoff #define DIOCSTARTALTQ _IO ('D', 42) 20893b3a8eb9SGleb Smirnoff #define DIOCSTOPALTQ _IO ('D', 43) 2090249cc75fSPatrick Kelsey #define DIOCADDALTQV0 _IOWR('D', 45, struct pfioc_altq_v0) 2091249cc75fSPatrick Kelsey #define DIOCADDALTQV1 _IOWR('D', 45, struct pfioc_altq_v1) 2092249cc75fSPatrick Kelsey #define DIOCGETALTQSV0 _IOWR('D', 47, struct pfioc_altq_v0) 2093249cc75fSPatrick Kelsey #define DIOCGETALTQSV1 _IOWR('D', 47, struct pfioc_altq_v1) 2094249cc75fSPatrick Kelsey #define DIOCGETALTQV0 _IOWR('D', 48, struct pfioc_altq_v0) 2095249cc75fSPatrick Kelsey #define DIOCGETALTQV1 _IOWR('D', 48, struct pfioc_altq_v1) 2096249cc75fSPatrick Kelsey #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0) 2097249cc75fSPatrick Kelsey #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1) 2098249cc75fSPatrick Kelsey #define DIOCGETQSTATSV0 _IOWR('D', 50, struct pfioc_qstats_v0) 2099249cc75fSPatrick Kelsey #define DIOCGETQSTATSV1 _IOWR('D', 50, struct pfioc_qstats_v1) 21003b3a8eb9SGleb Smirnoff #define DIOCBEGINADDRS _IOWR('D', 51, struct pfioc_pooladdr) 21013b3a8eb9SGleb Smirnoff #define DIOCADDADDR _IOWR('D', 52, struct pfioc_pooladdr) 21023b3a8eb9SGleb Smirnoff #define DIOCGETADDRS _IOWR('D', 53, struct pfioc_pooladdr) 21033b3a8eb9SGleb Smirnoff #define DIOCGETADDR _IOWR('D', 54, struct pfioc_pooladdr) 2104cc535c95SWill Andrews #define DIOCCHANGEADDR _IOWR('D', 55, struct pfioc_pooladdr) 21053b3a8eb9SGleb Smirnoff /* XXX cut 55 - 57 */ 21063b3a8eb9SGleb Smirnoff #define DIOCGETRULESETS _IOWR('D', 58, struct pfioc_ruleset) 21073b3a8eb9SGleb Smirnoff #define DIOCGETRULESET _IOWR('D', 59, struct pfioc_ruleset) 21083b3a8eb9SGleb Smirnoff #define DIOCRCLRTABLES _IOWR('D', 60, struct pfioc_table) 21093b3a8eb9SGleb Smirnoff #define DIOCRADDTABLES _IOWR('D', 61, struct pfioc_table) 21103b3a8eb9SGleb Smirnoff #define DIOCRDELTABLES _IOWR('D', 62, struct pfioc_table) 21113b3a8eb9SGleb Smirnoff #define DIOCRGETTABLES _IOWR('D', 63, struct pfioc_table) 21123b3a8eb9SGleb Smirnoff #define DIOCRGETTSTATS _IOWR('D', 64, struct pfioc_table) 21133b3a8eb9SGleb Smirnoff #define DIOCRCLRTSTATS _IOWR('D', 65, struct pfioc_table) 21143b3a8eb9SGleb Smirnoff #define DIOCRCLRADDRS _IOWR('D', 66, struct pfioc_table) 21153b3a8eb9SGleb Smirnoff #define DIOCRADDADDRS _IOWR('D', 67, struct pfioc_table) 21163b3a8eb9SGleb Smirnoff #define DIOCRDELADDRS _IOWR('D', 68, struct pfioc_table) 21173b3a8eb9SGleb Smirnoff #define DIOCRSETADDRS _IOWR('D', 69, struct pfioc_table) 21183b3a8eb9SGleb Smirnoff #define DIOCRGETADDRS _IOWR('D', 70, struct pfioc_table) 21193b3a8eb9SGleb Smirnoff #define DIOCRGETASTATS _IOWR('D', 71, struct pfioc_table) 21203b3a8eb9SGleb Smirnoff #define DIOCRCLRASTATS _IOWR('D', 72, struct pfioc_table) 21213b3a8eb9SGleb Smirnoff #define DIOCRTSTADDRS _IOWR('D', 73, struct pfioc_table) 21223b3a8eb9SGleb Smirnoff #define DIOCRSETTFLAGS _IOWR('D', 74, struct pfioc_table) 21233b3a8eb9SGleb Smirnoff #define DIOCRINADEFINE _IOWR('D', 77, struct pfioc_table) 21243b3a8eb9SGleb Smirnoff #define DIOCOSFPFLUSH _IO('D', 78) 21253b3a8eb9SGleb Smirnoff #define DIOCOSFPADD _IOWR('D', 79, struct pf_osfp_ioctl) 21263b3a8eb9SGleb Smirnoff #define DIOCOSFPGET _IOWR('D', 80, struct pf_osfp_ioctl) 21273b3a8eb9SGleb Smirnoff #define DIOCXBEGIN _IOWR('D', 81, struct pfioc_trans) 21283b3a8eb9SGleb Smirnoff #define DIOCXCOMMIT _IOWR('D', 82, struct pfioc_trans) 21293b3a8eb9SGleb Smirnoff #define DIOCXROLLBACK _IOWR('D', 83, struct pfioc_trans) 21303b3a8eb9SGleb Smirnoff #define DIOCGETSRCNODES _IOWR('D', 84, struct pfioc_src_nodes) 21313b3a8eb9SGleb Smirnoff #define DIOCCLRSRCNODES _IO('D', 85) 21323b3a8eb9SGleb Smirnoff #define DIOCSETHOSTID _IOWR('D', 86, u_int32_t) 21333b3a8eb9SGleb Smirnoff #define DIOCIGETIFACES _IOWR('D', 87, struct pfioc_iface) 21343b3a8eb9SGleb Smirnoff #define DIOCSETIFFLAG _IOWR('D', 89, struct pfioc_iface) 21353b3a8eb9SGleb Smirnoff #define DIOCCLRIFFLAG _IOWR('D', 90, struct pfioc_iface) 21363b3a8eb9SGleb Smirnoff #define DIOCKILLSRCNODES _IOWR('D', 91, struct pfioc_src_node_kill) 2137047c4e36SKristof Provost #define DIOCGIFSPEEDV0 _IOWR('D', 92, struct pf_ifspeed_v0) 2138047c4e36SKristof Provost #define DIOCGIFSPEEDV1 _IOWR('D', 92, struct pf_ifspeed_v1) 2139586fae3cSKristof Provost #ifdef COMPAT_FREEBSD14 2140c6bf20a2SKristof Provost #define DIOCGETSTATESV2 _IOWR('D', 93, struct pfioc_states_v2) 2141586fae3cSKristof Provost #endif 21428e1864edSKristof Provost #define DIOCGETSYNCOOKIES _IOWR('D', 94, struct pfioc_nv) 21438e1864edSKristof Provost #define DIOCSETSYNCOOKIES _IOWR('D', 95, struct pfioc_nv) 2144047c4e36SKristof Provost #define DIOCKEEPCOUNTERS _IOWR('D', 96, struct pfioc_nv) 21458e492101SKristof Provost #define DIOCKEEPCOUNTERS_FREEBSD13 _IOWR('D', 92, struct pfioc_nv) 2146e732e742SKristof Provost #define DIOCADDETHRULE _IOWR('D', 97, struct pfioc_nv) 2147e732e742SKristof Provost #define DIOCGETETHRULE _IOWR('D', 98, struct pfioc_nv) 2148e732e742SKristof Provost #define DIOCGETETHRULES _IOWR('D', 99, struct pfioc_nv) 21499bb06778SKristof Provost #define DIOCGETETHRULESETS _IOWR('D', 100, struct pfioc_nv) 21509bb06778SKristof Provost #define DIOCGETETHRULESET _IOWR('D', 101, struct pfioc_nv) 215139282ef3SKajetan Staszkiewicz #define DIOCSETREASS _IOWR('D', 102, u_int32_t) 215242ec75f8SKristof Provost 2153249cc75fSPatrick Kelsey struct pf_ifspeed_v0 { 21543b3a8eb9SGleb Smirnoff char ifname[IFNAMSIZ]; 21553b3a8eb9SGleb Smirnoff u_int32_t baudrate; 21563b3a8eb9SGleb Smirnoff }; 2157249cc75fSPatrick Kelsey 2158249cc75fSPatrick Kelsey struct pf_ifspeed_v1 { 2159249cc75fSPatrick Kelsey char ifname[IFNAMSIZ]; 2160249cc75fSPatrick Kelsey u_int32_t baudrate32; 2161249cc75fSPatrick Kelsey /* layout identical to struct pf_ifspeed_v0 up to this point */ 2162249cc75fSPatrick Kelsey u_int64_t baudrate; 2163249cc75fSPatrick Kelsey }; 2164249cc75fSPatrick Kelsey 2165249cc75fSPatrick Kelsey /* Latest version of struct pf_ifspeed_vX */ 2166249cc75fSPatrick Kelsey #define PF_IFSPEED_VERSION 1 2167249cc75fSPatrick Kelsey 2168249cc75fSPatrick Kelsey /* 2169249cc75fSPatrick Kelsey * Compatibility and convenience macros 2170249cc75fSPatrick Kelsey */ 2171249cc75fSPatrick Kelsey #ifndef _KERNEL 2172249cc75fSPatrick Kelsey #ifdef PFIOC_USE_LATEST 2173249cc75fSPatrick Kelsey /* 2174249cc75fSPatrick Kelsey * Maintaining in-tree consumers of the ioctl interface is easier when that 2175249cc75fSPatrick Kelsey * code can be written in terms old names that refer to the latest interface 2176249cc75fSPatrick Kelsey * version as that reduces the required changes in the consumers to those 2177249cc75fSPatrick Kelsey * that are functionally necessary to accommodate a new interface version. 2178249cc75fSPatrick Kelsey */ 2179249cc75fSPatrick Kelsey #define pfioc_altq __CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION) 2180249cc75fSPatrick Kelsey #define pfioc_qstats __CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION) 2181249cc75fSPatrick Kelsey #define pf_ifspeed __CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION) 2182249cc75fSPatrick Kelsey 2183249cc75fSPatrick Kelsey #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION) 2184249cc75fSPatrick Kelsey #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION) 2185249cc75fSPatrick Kelsey #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION) 2186249cc75fSPatrick Kelsey #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION) 2187249cc75fSPatrick Kelsey #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION) 2188249cc75fSPatrick Kelsey #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION) 2189249cc75fSPatrick Kelsey #else 2190249cc75fSPatrick Kelsey /* 2191249cc75fSPatrick Kelsey * When building out-of-tree code that is written for the old interface, 2192249cc75fSPatrick Kelsey * such as may exist in ports for example, resolve the old struct tags and 2193249cc75fSPatrick Kelsey * ioctl command names to the v0 versions. 2194249cc75fSPatrick Kelsey */ 2195249cc75fSPatrick Kelsey #define pfioc_altq __CONCAT(pfioc_altq_v, 0) 2196249cc75fSPatrick Kelsey #define pfioc_qstats __CONCAT(pfioc_qstats_v, 0) 2197249cc75fSPatrick Kelsey #define pf_ifspeed __CONCAT(pf_ifspeed_v, 0) 2198249cc75fSPatrick Kelsey 2199249cc75fSPatrick Kelsey #define DIOCADDALTQ __CONCAT(DIOCADDALTQV, 0) 2200249cc75fSPatrick Kelsey #define DIOCGETALTQS __CONCAT(DIOCGETALTQSV, 0) 2201249cc75fSPatrick Kelsey #define DIOCGETALTQ __CONCAT(DIOCGETALTQV, 0) 2202249cc75fSPatrick Kelsey #define DIOCCHANGEALTQ __CONCAT(DIOCCHANGEALTQV, 0) 2203249cc75fSPatrick Kelsey #define DIOCGETQSTATS __CONCAT(DIOCGETQSTATSV, 0) 2204249cc75fSPatrick Kelsey #define DIOCGIFSPEED __CONCAT(DIOCGIFSPEEDV, 0) 2205249cc75fSPatrick Kelsey #endif /* PFIOC_USE_LATEST */ 2206249cc75fSPatrick Kelsey #endif /* _KERNEL */ 22073b3a8eb9SGleb Smirnoff 22083b3a8eb9SGleb Smirnoff #ifdef _KERNEL 220917ad7334SKristof Provost LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node); 22103b3a8eb9SGleb Smirnoff struct pf_srchash { 221117ad7334SKristof Provost struct pf_ksrc_node_list nodes; 22123b3a8eb9SGleb Smirnoff struct mtx lock; 22133b3a8eb9SGleb Smirnoff }; 22143b3a8eb9SGleb Smirnoff 22153b3a8eb9SGleb Smirnoff struct pf_keyhash { 22163b3a8eb9SGleb Smirnoff LIST_HEAD(, pf_state_key) keys; 22173b3a8eb9SGleb Smirnoff struct mtx lock; 22183b3a8eb9SGleb Smirnoff }; 22193b3a8eb9SGleb Smirnoff 22203b3a8eb9SGleb Smirnoff struct pf_idhash { 2221211cddf9SKristof Provost LIST_HEAD(, pf_kstate) states; 22223b3a8eb9SGleb Smirnoff struct mtx lock; 22233b3a8eb9SGleb Smirnoff }; 22243b3a8eb9SGleb Smirnoff 2225390dc369STom Jones struct pf_udpendpointhash { 2226390dc369STom Jones LIST_HEAD(, pf_udp_endpoint) endpoints; 2227390dc369STom Jones /* refcont is synchronized on the source endpoint's row lock */ 2228390dc369STom Jones struct mtx lock; 2229390dc369STom Jones }; 2230390dc369STom Jones 2231231e83d3SKristof Provost extern u_long pf_ioctl_maxcount; 2232271f1469SKristof Provost VNET_DECLARE(u_long, pf_hashmask); 2233271f1469SKristof Provost #define V_pf_hashmask VNET(pf_hashmask) 2234271f1469SKristof Provost VNET_DECLARE(u_long, pf_srchashmask); 2235271f1469SKristof Provost #define V_pf_srchashmask VNET(pf_srchashmask) 2236390dc369STom Jones VNET_DECLARE(u_long, pf_udpendpointhashmask); 2237390dc369STom Jones #define V_pf_udpendpointhashmask VNET(pf_udpendpointhashmask) 223891e0f2d2SKristof Provost #define PF_HASHSIZ (131072) 2239bf56a3feSKristof Provost #define PF_SRCHASHSIZ (PF_HASHSIZ/4) 2240390dc369STom Jones #define PF_UDPENDHASHSIZ (PF_HASHSIZ/4) 22413b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_keyhash *, pf_keyhash); 22423b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_idhash *, pf_idhash); 2243390dc369STom Jones VNET_DECLARE(struct pf_udpendpointhash *, pf_udpendpointhash); 22443b3a8eb9SGleb Smirnoff #define V_pf_keyhash VNET(pf_keyhash) 22453b3a8eb9SGleb Smirnoff #define V_pf_idhash VNET(pf_idhash) 2246390dc369STom Jones #define V_pf_udpendpointhash VNET(pf_udpendpointhash) 22473b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_srchash *, pf_srchash); 22483b3a8eb9SGleb Smirnoff #define V_pf_srchash VNET(pf_srchash) 22493b3a8eb9SGleb Smirnoff 225027f70efeSMark Johnston #define PF_IDHASHID(id) (be64toh(id) % (V_pf_hashmask + 1)) 225127f70efeSMark Johnston #define PF_IDHASH(s) PF_IDHASHID((s)->id) 22523b3a8eb9SGleb Smirnoff 22533b3a8eb9SGleb Smirnoff VNET_DECLARE(void *, pf_swi_cookie); 22543b3a8eb9SGleb Smirnoff #define V_pf_swi_cookie VNET(pf_swi_cookie) 2255cecfaf9bSKristof Provost VNET_DECLARE(struct intr_event *, pf_swi_ie); 2256cecfaf9bSKristof Provost #define V_pf_swi_ie VNET(pf_swi_ie) 22573b3a8eb9SGleb Smirnoff 2258133935d2SKristof Provost VNET_DECLARE(struct unrhdr64, pf_stateid); 22593b3a8eb9SGleb Smirnoff #define V_pf_stateid VNET(pf_stateid) 22603b3a8eb9SGleb Smirnoff 22613b3a8eb9SGleb Smirnoff TAILQ_HEAD(pf_altqqueue, pf_altq); 22628f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue, pf_altqs[4]); 22633b3a8eb9SGleb Smirnoff #define V_pf_altqs VNET(pf_altqs) 22640972294eSKristof Provost VNET_DECLARE(struct pf_kpalist, pf_pabuf[3]); 22653b3a8eb9SGleb Smirnoff #define V_pf_pabuf VNET(pf_pabuf) 22663b3a8eb9SGleb Smirnoff 22673b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t, ticket_altqs_active); 22683b3a8eb9SGleb Smirnoff #define V_ticket_altqs_active VNET(ticket_altqs_active) 22693b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t, ticket_altqs_inactive); 22703b3a8eb9SGleb Smirnoff #define V_ticket_altqs_inactive VNET(ticket_altqs_inactive) 22713b3a8eb9SGleb Smirnoff VNET_DECLARE(int, altqs_inactive_open); 22723b3a8eb9SGleb Smirnoff #define V_altqs_inactive_open VNET(altqs_inactive_open) 22733b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t, ticket_pabuf); 22743b3a8eb9SGleb Smirnoff #define V_ticket_pabuf VNET(ticket_pabuf) 22753b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_altqqueue *, pf_altqs_active); 22763b3a8eb9SGleb Smirnoff #define V_pf_altqs_active VNET(pf_altqs_active) 22778f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue *, pf_altq_ifs_active); 22788f2ac656SPatrick Kelsey #define V_pf_altq_ifs_active VNET(pf_altq_ifs_active) 22793b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_altqqueue *, pf_altqs_inactive); 22803b3a8eb9SGleb Smirnoff #define V_pf_altqs_inactive VNET(pf_altqs_inactive) 22818f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue *, pf_altq_ifs_inactive); 22828f2ac656SPatrick Kelsey #define V_pf_altq_ifs_inactive VNET(pf_altq_ifs_inactive) 22833b3a8eb9SGleb Smirnoff 2284e86bddeaSKristof Provost VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules); 22853b3a8eb9SGleb Smirnoff #define V_pf_unlinked_rules VNET(pf_unlinked_rules) 22863b3a8eb9SGleb Smirnoff 2287d40d4b3eSMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER 2288d40d4b3eSMateusz Guzik LIST_HEAD(allkiflist_head, pfi_kkif); 2289d40d4b3eSMateusz Guzik VNET_DECLARE(struct allkiflist_head, pf_allkiflist); 2290d40d4b3eSMateusz Guzik #define V_pf_allkiflist VNET(pf_allkiflist) 2291d40d4b3eSMateusz Guzik VNET_DECLARE(size_t, pf_allkifcount); 2292d40d4b3eSMateusz Guzik #define V_pf_allkifcount VNET(pf_allkifcount) 2293d40d4b3eSMateusz Guzik VNET_DECLARE(struct pfi_kkif *, pf_kifmarker); 2294d40d4b3eSMateusz Guzik #define V_pf_kifmarker VNET(pf_kifmarker) 229502cf67ccSMateusz Guzik 229602cf67ccSMateusz Guzik LIST_HEAD(allrulelist_head, pf_krule); 229702cf67ccSMateusz Guzik VNET_DECLARE(struct allrulelist_head, pf_allrulelist); 229802cf67ccSMateusz Guzik #define V_pf_allrulelist VNET(pf_allrulelist) 229902cf67ccSMateusz Guzik VNET_DECLARE(size_t, pf_allrulecount); 230002cf67ccSMateusz Guzik #define V_pf_allrulecount VNET(pf_allrulecount) 230102cf67ccSMateusz Guzik VNET_DECLARE(struct pf_krule *, pf_rulemarker); 230202cf67ccSMateusz Guzik #define V_pf_rulemarker VNET(pf_rulemarker) 2303d40d4b3eSMateusz Guzik #endif 2304d40d4b3eSMateusz Guzik 23057e51bc6cSKristof Provost void unhandled_af(int) __dead2; 230681647eb6SKristof Provost int pf_start(void); 230781647eb6SKristof Provost int pf_stop(void); 2308efc6c51fSGleb Smirnoff void pf_initialize(void); 23097e92ce73SMartin Matuska void pf_mtag_initialize(void); 23107e92ce73SMartin Matuska void pf_mtag_cleanup(void); 23113b3a8eb9SGleb Smirnoff void pf_cleanup(void); 23123b3a8eb9SGleb Smirnoff 23133b3a8eb9SGleb Smirnoff struct pf_mtag *pf_get_mtag(struct mbuf *); 23143b3a8eb9SGleb Smirnoff 2315e86bddeaSKristof Provost extern void pf_calc_skip_steps(struct pf_krulequeue *); 23163b3a8eb9SGleb Smirnoff #ifdef ALTQ 23173b3a8eb9SGleb Smirnoff extern void pf_altq_ifnet_event(struct ifnet *, int); 23183b3a8eb9SGleb Smirnoff #endif 23193b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t, pf_state_z); 23203b3a8eb9SGleb Smirnoff #define V_pf_state_z VNET(pf_state_z) 23213b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t, pf_state_key_z); 23223b3a8eb9SGleb Smirnoff #define V_pf_state_key_z VNET(pf_state_key_z) 2323390dc369STom Jones VNET_DECLARE(uma_zone_t, pf_udp_mapping_z); 2324390dc369STom Jones #define V_pf_udp_mapping_z VNET(pf_udp_mapping_z) 23253b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t, pf_state_scrub_z); 23263b3a8eb9SGleb Smirnoff #define V_pf_state_scrub_z VNET(pf_state_scrub_z) 23273b3a8eb9SGleb Smirnoff 23283b3a8eb9SGleb Smirnoff extern void pf_purge_thread(void *); 2329a0429b54SBjoern A. Zeeb extern void pf_unload_vnet_purge(void); 23303b3a8eb9SGleb Smirnoff extern void pf_intr(void *); 23313b3a8eb9SGleb Smirnoff extern void pf_purge_expired_src_nodes(void); 23323b3a8eb9SGleb Smirnoff 23338f3d786cSMateusz Guzik extern int pf_unlink_state(struct pf_kstate *); 2334320c1116SKristof Provost extern int pf_state_insert(struct pfi_kkif *, 2335d0fdf2b2SKristof Provost struct pfi_kkif *, 23363b3a8eb9SGleb Smirnoff struct pf_state_key *, 23373b3a8eb9SGleb Smirnoff struct pf_state_key *, 2338211cddf9SKristof Provost struct pf_kstate *); 2339211cddf9SKristof Provost extern struct pf_kstate *pf_alloc_state(int); 2340211cddf9SKristof Provost extern void pf_free_state(struct pf_kstate *); 2341706d465dSKristof Provost extern void pf_killstates(struct pf_kstate_kill *, 2342706d465dSKristof Provost unsigned int *); 2343706d465dSKristof Provost extern unsigned int pf_clear_states(const struct pf_kstate_kill *); 23443b3a8eb9SGleb Smirnoff 23453b3a8eb9SGleb Smirnoff static __inline void 2346211cddf9SKristof Provost pf_ref_state(struct pf_kstate *s) 23473b3a8eb9SGleb Smirnoff { 23483b3a8eb9SGleb Smirnoff 23493b3a8eb9SGleb Smirnoff refcount_acquire(&s->refs); 23503b3a8eb9SGleb Smirnoff } 23513b3a8eb9SGleb Smirnoff 23523b3a8eb9SGleb Smirnoff static __inline int 2353211cddf9SKristof Provost pf_release_state(struct pf_kstate *s) 23543b3a8eb9SGleb Smirnoff { 23553b3a8eb9SGleb Smirnoff 23563b3a8eb9SGleb Smirnoff if (refcount_release(&s->refs)) { 23573b3a8eb9SGleb Smirnoff pf_free_state(s); 23583b3a8eb9SGleb Smirnoff return (1); 23593b3a8eb9SGleb Smirnoff } else 23603b3a8eb9SGleb Smirnoff return (0); 23613b3a8eb9SGleb Smirnoff } 23623b3a8eb9SGleb Smirnoff 2363d09388d0SMateusz Guzik static __inline int 2364211cddf9SKristof Provost pf_release_staten(struct pf_kstate *s, u_int n) 2365d09388d0SMateusz Guzik { 2366d09388d0SMateusz Guzik 2367d09388d0SMateusz Guzik if (refcount_releasen(&s->refs, n)) { 2368d09388d0SMateusz Guzik pf_free_state(s); 2369d09388d0SMateusz Guzik return (1); 2370d09388d0SMateusz Guzik } else 2371d09388d0SMateusz Guzik return (0); 2372d09388d0SMateusz Guzik } 2373d09388d0SMateusz Guzik 237404932601SKristof Provost static __inline uint64_t 237504932601SKristof Provost pf_get_uptime(void) 237604932601SKristof Provost { 237704932601SKristof Provost struct timeval t; 237804932601SKristof Provost microuptime(&t); 237904932601SKristof Provost return ((t.tv_sec * 1000) + (t.tv_usec / 1000)); 238004932601SKristof Provost } 238104932601SKristof Provost 238204932601SKristof Provost static __inline uint64_t 238304932601SKristof Provost pf_get_time(void) 238404932601SKristof Provost { 238504932601SKristof Provost struct timeval t; 238604932601SKristof Provost microtime(&t); 238704932601SKristof Provost return ((t.tv_sec * 1000) + (t.tv_usec / 1000)); 238804932601SKristof Provost } 238904932601SKristof Provost 2390211cddf9SKristof Provost extern struct pf_kstate *pf_find_state_byid(uint64_t, uint32_t); 239166b8cac8SMark Johnston extern struct pf_kstate *pf_find_state_all( 239266b8cac8SMark Johnston const struct pf_state_key_cmp *, 23933b3a8eb9SGleb Smirnoff u_int, int *); 239466b8cac8SMark Johnston extern bool pf_find_state_all_exists( 239566b8cac8SMark Johnston const struct pf_state_key_cmp *, 239619d6e29bSMateusz Guzik u_int); 2397390dc369STom Jones extern struct pf_udp_mapping *pf_udp_mapping_find(struct pf_udp_endpoint_cmp 2398390dc369STom Jones *endpoint); 2399390dc369STom Jones extern struct pf_udp_mapping *pf_udp_mapping_create(sa_family_t af, 2400390dc369STom Jones struct pf_addr *src_addr, uint16_t src_port, 2401390dc369STom Jones struct pf_addr *nat_addr, uint16_t nat_port); 2402390dc369STom Jones extern int pf_udp_mapping_insert(struct pf_udp_mapping 2403390dc369STom Jones *mapping); 2404390dc369STom Jones extern void pf_udp_mapping_release(struct pf_udp_mapping 2405390dc369STom Jones *mapping); 2406b9c0321dSKajetan Staszkiewicz uint32_t pf_hashsrc(struct pf_addr *, sa_family_t); 2407b9c0321dSKajetan Staszkiewicz extern bool pf_src_node_exists(struct pf_ksrc_node **, 2408b9c0321dSKajetan Staszkiewicz struct pf_srchash *); 240917ad7334SKristof Provost extern struct pf_ksrc_node *pf_find_src_node(struct pf_addr *, 2410db0a2bfdSKajetan Staszkiewicz struct pf_krule *, sa_family_t, 241107e070efSKajetan Staszkiewicz struct pf_srchash **, pf_sn_types_t, bool); 241217ad7334SKristof Provost extern void pf_unlink_src_node(struct pf_ksrc_node *); 241317ad7334SKristof Provost extern u_int pf_free_src_nodes(struct pf_ksrc_node_list *); 2414211cddf9SKristof Provost extern void pf_print_state(struct pf_kstate *); 2415347dd053SRichard Scheffenegger extern void pf_print_flags(uint16_t); 2416ba94bf28SKristof Provost extern int pf_addr_wrap_neq(struct pf_addr_wrap *, 2417ba94bf28SKristof Provost struct pf_addr_wrap *); 24183b3a8eb9SGleb Smirnoff extern u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t, 24193b3a8eb9SGleb Smirnoff u_int8_t); 2420c110fc49SKristof Provost extern u_int16_t pf_proto_cksum_fixup(struct mbuf *, u_int16_t, 2421c110fc49SKristof Provost u_int16_t, u_int16_t, u_int8_t); 24223b3a8eb9SGleb Smirnoff 24233b3a8eb9SGleb Smirnoff VNET_DECLARE(struct ifnet *, sync_ifp); 24243b3a8eb9SGleb Smirnoff #define V_sync_ifp VNET(sync_ifp); 2425e86bddeaSKristof Provost VNET_DECLARE(struct pf_krule, pf_default_rule); 24263b3a8eb9SGleb Smirnoff #define V_pf_default_rule VNET(pf_default_rule) 242707579e21SKristof Provost extern void pf_addrcpy(struct pf_addr *, const struct pf_addr *, 24288216f1a9SKristof Provost sa_family_t); 2429e86bddeaSKristof Provost void pf_free_rule(struct pf_krule *); 24303b3a8eb9SGleb Smirnoff 2431e732e742SKristof Provost int pf_test_eth(int, int, struct ifnet *, struct mbuf **, struct inpcb *); 24329a405864SKristof Provost int pf_scan_sctp(struct pf_pdesc *); 243327f54be5SKristof Provost #if defined(INET) || defined(INET6) 243427f54be5SKristof Provost int pf_test(sa_family_t, int, int, struct ifnet *, struct mbuf **, struct inpcb *, 24359925aee0SKristof Provost struct pf_rule_actions *); 243627f54be5SKristof Provost #endif 243727f54be5SKristof Provost #ifdef INET 24385d28f4caSKristof Provost int pf_normalize_ip(u_short *, struct pf_pdesc *); 24393b3a8eb9SGleb Smirnoff #endif /* INET */ 24403b3a8eb9SGleb Smirnoff 24413b3a8eb9SGleb Smirnoff #ifdef INET6 24425d28f4caSKristof Provost int pf_normalize_ip6(int, u_short *, struct pf_pdesc *); 24433b3a8eb9SGleb Smirnoff void pf_poolmask(struct pf_addr *, struct pf_addr*, 24448216f1a9SKristof Provost struct pf_addr *, struct pf_addr *, sa_family_t); 24453b3a8eb9SGleb Smirnoff void pf_addr_inc(struct pf_addr *, sa_family_t); 244654c62e3eSKristof Provost int pf_max_frag_size(struct mbuf *); 244756b7685aSKristof Provost int pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *, 244856b7685aSKristof Provost struct ifnet *, bool); 24493b3a8eb9SGleb Smirnoff #endif /* INET6 */ 24503b3a8eb9SGleb Smirnoff 24519a405864SKristof Provost int pf_multihome_scan_init(int, int, struct pf_pdesc *); 24529a405864SKristof Provost int pf_multihome_scan_asconf(int, int, struct pf_pdesc *); 245310aa9ddbSKristof Provost 2454211cddf9SKristof Provost u_int32_t pf_new_isn(struct pf_kstate *); 24552096ad13SKristof Provost void *pf_pull_hdr(const struct mbuf *, int, void *, int, u_short *, u_short *, 24563b3a8eb9SGleb Smirnoff sa_family_t); 24573b3a8eb9SGleb Smirnoff void pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t); 2458c110fc49SKristof Provost void pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t, 2459c110fc49SKristof Provost u_int8_t); 2460c110fc49SKristof Provost void pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t); 2461c3f69af0SKristof Provost void pf_patch_16_unaligned(struct mbuf *, u_int16_t *, void *, u_int16_t, 2462c3f69af0SKristof Provost bool, u_int8_t); 2463c3f69af0SKristof Provost void pf_patch_32_unaligned(struct mbuf *, u_int16_t *, void *, u_int32_t, 2464c3f69af0SKristof Provost bool, u_int8_t); 2465211cddf9SKristof Provost void pf_send_deferred_syn(struct pf_kstate *); 246607579e21SKristof Provost int pf_match_addr(u_int8_t, const struct pf_addr *, 246707579e21SKristof Provost const struct pf_addr *, const struct pf_addr *, sa_family_t); 246807579e21SKristof Provost int pf_match_addr_range(const struct pf_addr *, const struct pf_addr *, 246907579e21SKristof Provost const struct pf_addr *, sa_family_t); 24703b3a8eb9SGleb Smirnoff int pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t); 24713b3a8eb9SGleb Smirnoff 2472efc6c51fSGleb Smirnoff void pf_normalize_init(void); 24733b3a8eb9SGleb Smirnoff void pf_normalize_cleanup(void); 24749a405864SKristof Provost int pf_normalize_tcp(struct pf_pdesc *); 2475211cddf9SKristof Provost void pf_normalize_tcp_cleanup(struct pf_kstate *); 24769a405864SKristof Provost int pf_normalize_tcp_init(struct pf_pdesc *, 24773b3a8eb9SGleb Smirnoff struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *); 24789a405864SKristof Provost int pf_normalize_tcp_stateful(struct pf_pdesc *, 2479211cddf9SKristof Provost u_short *, struct tcphdr *, struct pf_kstate *, 24803b3a8eb9SGleb Smirnoff struct pf_state_peer *, struct pf_state_peer *, int *); 24819a405864SKristof Provost int pf_normalize_sctp_init(struct pf_pdesc *, 248251a78dd2SKristof Provost struct pf_state_peer *, struct pf_state_peer *); 24839a405864SKristof Provost int pf_normalize_sctp(struct pf_pdesc *); 24843b3a8eb9SGleb Smirnoff u_int32_t 2485211cddf9SKristof Provost pf_state_expires(const struct pf_kstate *); 24863b3a8eb9SGleb Smirnoff void pf_purge_expired_fragments(void); 24875d0020d6SKristof Provost void pf_purge_fragments(uint32_t); 2488320c1116SKristof Provost int pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *, 24893b3a8eb9SGleb Smirnoff int); 24909a405864SKristof Provost int pf_socket_lookup(struct pf_pdesc *); 24913b3a8eb9SGleb Smirnoff struct pf_state_key *pf_alloc_state_key(int); 2492fcdb520cSKristof Provost int pf_translate(struct pf_pdesc *, struct pf_addr *, u_int16_t, 2493fcdb520cSKristof Provost struct pf_addr *, u_int16_t, u_int16_t, int); 2494fcdb520cSKristof Provost int pf_translate_af(struct pf_pdesc *); 24953b3a8eb9SGleb Smirnoff void pfr_initialize(void); 24963b3a8eb9SGleb Smirnoff void pfr_cleanup(void); 24973b3a8eb9SGleb Smirnoff int pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t); 24983b3a8eb9SGleb Smirnoff void pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t, 24993b3a8eb9SGleb Smirnoff u_int64_t, int, int, int); 25007d381d0aSKristof Provost int pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t, 25017d381d0aSKristof Provost pf_addr_filter_func_t); 25023b3a8eb9SGleb Smirnoff void pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *); 25033b3a8eb9SGleb Smirnoff struct pfr_ktable * 2504e86bddeaSKristof Provost pfr_attach_table(struct pf_kruleset *, char *); 2505812839e5SKristof Provost struct pfr_ktable * 2506812839e5SKristof Provost pfr_eth_attach_table(struct pf_keth_ruleset *, char *); 25073b3a8eb9SGleb Smirnoff void pfr_detach_table(struct pfr_ktable *); 25083b3a8eb9SGleb Smirnoff int pfr_clr_tables(struct pfr_table *, int *, int); 25093b3a8eb9SGleb Smirnoff int pfr_add_tables(struct pfr_table *, int, int *, int); 25103b3a8eb9SGleb Smirnoff int pfr_del_tables(struct pfr_table *, int, int *, int); 2511adfe2f6aSKristof Provost int pfr_table_count(struct pfr_table *, int); 25123b3a8eb9SGleb Smirnoff int pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int); 25133b3a8eb9SGleb Smirnoff int pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int); 25143b3a8eb9SGleb Smirnoff int pfr_clr_tstats(struct pfr_table *, int, int *, int); 25153b3a8eb9SGleb Smirnoff int pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int); 25163b3a8eb9SGleb Smirnoff int pfr_clr_addrs(struct pfr_table *, int *, int); 25177e7f8800SKristof Provost int pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, time_t); 25183b3a8eb9SGleb Smirnoff int pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 25193b3a8eb9SGleb Smirnoff int); 25203b3a8eb9SGleb Smirnoff int pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 25213b3a8eb9SGleb Smirnoff int); 25223b3a8eb9SGleb Smirnoff int pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 25233b3a8eb9SGleb Smirnoff int *, int *, int *, int, u_int32_t); 25243b3a8eb9SGleb Smirnoff int pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int); 25253b3a8eb9SGleb Smirnoff int pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int); 25263b3a8eb9SGleb Smirnoff int pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *, 25273b3a8eb9SGleb Smirnoff int); 25283b3a8eb9SGleb Smirnoff int pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *, 25293b3a8eb9SGleb Smirnoff int); 25303b3a8eb9SGleb Smirnoff int pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int); 25313b3a8eb9SGleb Smirnoff int pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int); 25323b3a8eb9SGleb Smirnoff int pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int); 25333b3a8eb9SGleb Smirnoff int pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *, 25343b3a8eb9SGleb Smirnoff int *, u_int32_t, int); 25353b3a8eb9SGleb Smirnoff 25363b3a8eb9SGleb Smirnoff MALLOC_DECLARE(PFI_MTYPE); 2537320c1116SKristof Provost VNET_DECLARE(struct pfi_kkif *, pfi_all); 25383b3a8eb9SGleb Smirnoff #define V_pfi_all VNET(pfi_all) 25393b3a8eb9SGleb Smirnoff 2540efc6c51fSGleb Smirnoff void pfi_initialize(void); 2541a0429b54SBjoern A. Zeeb void pfi_initialize_vnet(void); 25423b3a8eb9SGleb Smirnoff void pfi_cleanup(void); 2543a0429b54SBjoern A. Zeeb void pfi_cleanup_vnet(void); 2544320c1116SKristof Provost void pfi_kkif_ref(struct pfi_kkif *); 2545320c1116SKristof Provost void pfi_kkif_unref(struct pfi_kkif *); 2546320c1116SKristof Provost struct pfi_kkif *pfi_kkif_find(const char *); 2547320c1116SKristof Provost struct pfi_kkif *pfi_kkif_attach(struct pfi_kkif *, const char *); 2548320c1116SKristof Provost int pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *); 2549320c1116SKristof Provost void pfi_kkif_purge(void); 25503b3a8eb9SGleb Smirnoff int pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *, 25513b3a8eb9SGleb Smirnoff sa_family_t); 25523b3a8eb9SGleb Smirnoff int pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t); 25533b3a8eb9SGleb Smirnoff void pfi_dynaddr_remove(struct pfi_dynaddr *); 25543b3a8eb9SGleb Smirnoff void pfi_dynaddr_copyout(struct pf_addr_wrap *); 25553b3a8eb9SGleb Smirnoff void pfi_update_status(const char *, struct pf_status *); 25563b3a8eb9SGleb Smirnoff void pfi_get_ifaces(const char *, struct pfi_kif *, int *); 25573b3a8eb9SGleb Smirnoff int pfi_set_flags(const char *, int); 25583b3a8eb9SGleb Smirnoff int pfi_clear_flags(const char *, int); 25593b3a8eb9SGleb Smirnoff 2560e86bddeaSKristof Provost int pf_match_tag(struct mbuf *, struct pf_krule *, int *, int); 25619a405864SKristof Provost int pf_tag_packet(struct pf_pdesc *, int); 256260048052SGleb Smirnoff int pf_addr_cmp(struct pf_addr *, struct pf_addr *, 256360048052SGleb Smirnoff sa_family_t); 25643b3a8eb9SGleb Smirnoff 25659a405864SKristof Provost u_int16_t pf_get_mss(struct pf_pdesc *); 25669a405864SKristof Provost u_int8_t pf_get_wscale(struct pf_pdesc *); 25678e1864edSKristof Provost struct mbuf *pf_build_tcp(const struct pf_krule *, sa_family_t, 25688e1864edSKristof Provost const struct pf_addr *, const struct pf_addr *, 25698e1864edSKristof Provost u_int16_t, u_int16_t, u_int32_t, u_int32_t, 25704a7c6d62SMark Johnston u_int8_t, u_int16_t, u_int16_t, u_int8_t, int, 25717dc3be36SKajetan Staszkiewicz u_int16_t, u_int16_t, int); 25728e1864edSKristof Provost void pf_send_tcp(const struct pf_krule *, sa_family_t, 25738e1864edSKristof Provost const struct pf_addr *, const struct pf_addr *, 25748e1864edSKristof Provost u_int16_t, u_int16_t, u_int32_t, u_int32_t, 25754a7c6d62SMark Johnston u_int8_t, u_int16_t, u_int16_t, u_int8_t, int, 25767dc3be36SKajetan Staszkiewicz u_int16_t, u_int16_t, int); 25778e1864edSKristof Provost 25788e1864edSKristof Provost void pf_syncookies_init(void); 257932271c4dSKristof Provost void pf_syncookies_cleanup(void); 25808e1864edSKristof Provost int pf_get_syncookies(struct pfioc_nv *); 25818e1864edSKristof Provost int pf_set_syncookies(struct pfioc_nv *); 25828e1864edSKristof Provost int pf_synflood_check(struct pf_pdesc *); 25839a405864SKristof Provost void pf_syncookie_send(struct pf_pdesc *); 25849c041b45SKristof Provost bool pf_syncookie_check(struct pf_pdesc *); 25858e1864edSKristof Provost u_int8_t pf_syncookie_validate(struct pf_pdesc *); 258605896f1eSKristof Provost struct mbuf * pf_syncookie_recreate_syn(struct pf_pdesc *); 25878e1864edSKristof Provost 2588a9572d8fSGleb Smirnoff VNET_DECLARE(struct pf_kstatus, pf_status); 25893b3a8eb9SGleb Smirnoff #define V_pf_status VNET(pf_status) 25903b3a8eb9SGleb Smirnoff 25913b3a8eb9SGleb Smirnoff struct pf_limit { 25923b3a8eb9SGleb Smirnoff uma_zone_t zone; 25933b3a8eb9SGleb Smirnoff u_int limit; 25943b3a8eb9SGleb Smirnoff }; 25953b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); 25963b3a8eb9SGleb Smirnoff #define V_pf_limits VNET(pf_limits) 25973b3a8eb9SGleb Smirnoff 25983b3a8eb9SGleb Smirnoff #endif /* _KERNEL */ 25993b3a8eb9SGleb Smirnoff 26003b3a8eb9SGleb Smirnoff #ifdef _KERNEL 2601e11dacbfSKristof Provost struct pf_nl_pooladdr { 2602e11dacbfSKristof Provost u_int32_t action; 2603e11dacbfSKristof Provost u_int32_t ticket; 2604e11dacbfSKristof Provost u_int32_t nr; 2605e11dacbfSKristof Provost u_int32_t r_num; 2606e11dacbfSKristof Provost u_int8_t r_action; 2607e11dacbfSKristof Provost u_int8_t r_last; 2608e11dacbfSKristof Provost u_int8_t af; 2609e11dacbfSKristof Provost char anchor[MAXPATHLEN]; 2610e11dacbfSKristof Provost struct pf_pooladdr addr; 2611e11dacbfSKristof Provost /* Above this is identical to pfioc_pooladdr */ 2612e11dacbfSKristof Provost int which; 2613e11dacbfSKristof Provost }; 2614e11dacbfSKristof Provost 2615e86bddeaSKristof Provost VNET_DECLARE(struct pf_kanchor_global, pf_anchors); 26163b3a8eb9SGleb Smirnoff #define V_pf_anchors VNET(pf_anchors) 2617e86bddeaSKristof Provost VNET_DECLARE(struct pf_kanchor, pf_main_anchor); 26183b3a8eb9SGleb Smirnoff #define V_pf_main_anchor VNET(pf_main_anchor) 2619c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_anchor_global, pf_keth_anchors); 2620c5131afeSKristof Provost #define V_pf_keth_anchors VNET(pf_keth_anchors) 26213b3a8eb9SGleb Smirnoff #define pf_main_ruleset V_pf_main_anchor.ruleset 26223b3a8eb9SGleb Smirnoff 2623c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_anchor, pf_main_keth_anchor); 2624c5131afeSKristof Provost #define V_pf_main_keth_anchor VNET(pf_main_keth_anchor) 2625c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_ruleset*, pf_keth); 2626e732e742SKristof Provost #define V_pf_keth VNET(pf_keth) 2627e732e742SKristof Provost 2628e86bddeaSKristof Provost void pf_init_kruleset(struct pf_kruleset *); 2629c5131afeSKristof Provost void pf_init_keth(struct pf_keth_ruleset *); 2630e86bddeaSKristof Provost int pf_kanchor_setup(struct pf_krule *, 2631e86bddeaSKristof Provost const struct pf_kruleset *, const char *); 2632777a4702SKristof Provost int pf_kanchor_copyout(const struct pf_kruleset *, 26336ee3e376SKristof Provost const struct pf_krule *, char *, size_t); 2634d710367dSKristof Provost int pf_kanchor_nvcopyout(const struct pf_kruleset *, 2635d710367dSKristof Provost const struct pf_krule *, nvlist_t *); 2636e86bddeaSKristof Provost void pf_kanchor_remove(struct pf_krule *); 2637e86bddeaSKristof Provost void pf_remove_if_empty_kruleset(struct pf_kruleset *); 2638e86bddeaSKristof Provost struct pf_kruleset *pf_find_kruleset(const char *); 2639e86bddeaSKristof Provost struct pf_kruleset *pf_find_or_create_kruleset(const char *); 26403b3a8eb9SGleb Smirnoff void pf_rs_initialize(void); 26415e9dae8eSKristof Provost 2642c5131afeSKristof Provost 2643773e3a71SMark Johnston struct pf_krule *pf_krule_alloc(void); 2644c5131afeSKristof Provost 2645c5131afeSKristof Provost void pf_remove_if_empty_keth_ruleset( 2646c5131afeSKristof Provost struct pf_keth_ruleset *); 2647c5131afeSKristof Provost struct pf_keth_ruleset *pf_find_keth_ruleset(const char *); 2648c5131afeSKristof Provost struct pf_keth_anchor *pf_find_keth_anchor(const char *); 2649c5131afeSKristof Provost int pf_keth_anchor_setup(struct pf_keth_rule *, 2650c5131afeSKristof Provost const struct pf_keth_ruleset *, const char *); 2651c5131afeSKristof Provost int pf_keth_anchor_nvcopyout( 2652c5131afeSKristof Provost const struct pf_keth_ruleset *, 2653c5131afeSKristof Provost const struct pf_keth_rule *, nvlist_t *); 2654c5131afeSKristof Provost struct pf_keth_ruleset *pf_find_or_create_keth_ruleset(const char *); 2655c5131afeSKristof Provost void pf_keth_anchor_remove(struct pf_keth_rule *); 2656c5131afeSKristof Provost 265744f323ecSKristof Provost int pf_ioctl_getrules(struct pfioc_rule *); 2658ffbf2595SKristof Provost int pf_ioctl_addrule(struct pf_krule *, uint32_t, 2659ffbf2595SKristof Provost uint32_t, const char *, const char *, uid_t uid, 2660ffbf2595SKristof Provost pid_t); 26619dbbe68bSKristof Provost void pf_ioctl_clear_status(void); 266230bad751SKristof Provost int pf_ioctl_get_timeout(int, int *); 266330bad751SKristof Provost int pf_ioctl_set_timeout(int, int, int *); 2664d9ab8999SKristof Provost int pf_ioctl_get_limit(int, unsigned int *); 2665d9ab8999SKristof Provost int pf_ioctl_set_limit(int, unsigned int, unsigned int *); 2666ba2a9207SKristof Provost int pf_ioctl_begin_addrs(uint32_t *); 2667e11dacbfSKristof Provost int pf_ioctl_add_addr(struct pf_nl_pooladdr *); 2668e11dacbfSKristof Provost int pf_ioctl_get_addrs(struct pf_nl_pooladdr *); 2669e11dacbfSKristof Provost int pf_ioctl_get_addr(struct pf_nl_pooladdr *); 267025e0f8f9SKristof Provost int pf_ioctl_get_rulesets(struct pfioc_ruleset *); 267148f5bf8bSKristof Provost int pf_ioctl_get_ruleset(struct pfioc_ruleset *); 2672ffbf2595SKristof Provost 26735e9dae8eSKristof Provost void pf_krule_free(struct pf_krule *); 2674777a4702SKristof Provost void pf_krule_clear_counters(struct pf_krule *); 26751fc0dac5SKajetan Staszkiewicz void pf_addr_copyout(struct pf_addr_wrap *); 2676e86bddeaSKristof Provost #endif 26773b3a8eb9SGleb Smirnoff 26783b3a8eb9SGleb Smirnoff /* The fingerprint functions can be linked into userland programs (tcpdump) */ 26793b3a8eb9SGleb Smirnoff int pf_osfp_add(struct pf_osfp_ioctl *); 26803b3a8eb9SGleb Smirnoff #ifdef _KERNEL 26813b3a8eb9SGleb Smirnoff struct pf_osfp_enlist * 26829a405864SKristof Provost pf_osfp_fingerprint(struct pf_pdesc *, const struct tcphdr *); 26833b3a8eb9SGleb Smirnoff #endif /* _KERNEL */ 26843b3a8eb9SGleb Smirnoff void pf_osfp_flush(void); 26853b3a8eb9SGleb Smirnoff int pf_osfp_get(struct pf_osfp_ioctl *); 26863b3a8eb9SGleb Smirnoff int pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t); 26873b3a8eb9SGleb Smirnoff 26883b3a8eb9SGleb Smirnoff #ifdef _KERNEL 26898216f1a9SKristof Provost void pf_print_host(struct pf_addr *, u_int16_t, sa_family_t); 269007d9bc07SGleb Smirnoff 2691e86bddeaSKristof Provost void pf_step_into_anchor(struct pf_kanchor_stackframe *, int *, 2692e86bddeaSKristof Provost struct pf_kruleset **, int, struct pf_krule **, 269330ab6e82SKristof Provost struct pf_krule **); 2694e86bddeaSKristof Provost int pf_step_out_of_anchor(struct pf_kanchor_stackframe *, int *, 2695e86bddeaSKristof Provost struct pf_kruleset **, int, struct pf_krule **, 2696e86bddeaSKristof Provost struct pf_krule **, int *); 2697c5131afeSKristof Provost void pf_step_into_keth_anchor(struct pf_keth_anchor_stackframe *, 2698c5131afeSKristof Provost int *, struct pf_keth_ruleset **, 2699c5131afeSKristof Provost struct pf_keth_rule **, struct pf_keth_rule **, 2700c5131afeSKristof Provost int *); 2701c5131afeSKristof Provost int pf_step_out_of_keth_anchor(struct pf_keth_anchor_stackframe *, 2702c5131afeSKristof Provost int *, struct pf_keth_ruleset **, 2703c5131afeSKristof Provost struct pf_keth_rule **, struct pf_keth_rule **, 2704c5131afeSKristof Provost int *); 27053b3a8eb9SGleb Smirnoff 270616303d2bSKajetan Staszkiewicz u_short pf_map_addr(u_int8_t, struct pf_krule *, 27073b3a8eb9SGleb Smirnoff struct pf_addr *, struct pf_addr *, 2708fcdb520cSKristof Provost struct pfi_kkif **nkif, struct pf_addr *, 2709fcdb520cSKristof Provost struct pf_kpool *); 27108e3d2529SKajetan Staszkiewicz u_short pf_map_addr_sn(u_int8_t, struct pf_krule *, 27118e3d2529SKajetan Staszkiewicz struct pf_addr *, struct pf_addr *, 2712d10de21fSKajetan Staszkiewicz struct pfi_kkif **nkif, struct pf_addr *, 2713fcdb520cSKristof Provost struct pf_ksrc_node **, struct pf_srchash **, 271407e070efSKajetan Staszkiewicz struct pf_kpool *, pf_sn_types_t); 2715fcdb520cSKristof Provost int pf_get_transaddr_af(struct pf_krule *, 2716fcdb520cSKristof Provost struct pf_pdesc *); 27179a405864SKristof Provost u_short pf_get_translation(struct pf_pdesc *, 2718b9c0321dSKajetan Staszkiewicz int, struct pf_state_key **, struct pf_state_key **, 27192d7e68d5SKristof Provost struct pf_kanchor_stackframe *, struct pf_krule **, 2720390dc369STom Jones struct pf_udp_mapping **udp_mapping); 27213b3a8eb9SGleb Smirnoff 2722fcdb520cSKristof Provost int pf_state_key_setup(struct pf_pdesc *, 2723fcdb520cSKristof Provost u_int16_t, u_int16_t, 2724fcdb520cSKristof Provost struct pf_state_key **sk, struct pf_state_key **nk); 272566b8cac8SMark Johnston struct pf_state_key *pf_state_key_clone(const struct pf_state_key *); 27266b4ed16dSKajetan Staszkiewicz void pf_rule_to_actions(struct pf_krule *, 27276b4ed16dSKajetan Staszkiewicz struct pf_rule_actions *); 27289a405864SKristof Provost int pf_normalize_mss(struct pf_pdesc *pd); 27294f9e6887SKristof Provost #if defined(INET) || defined(INET6) 27309a405864SKristof Provost void pf_scrub(struct pf_pdesc *); 27314f9e6887SKristof Provost #endif 273239282ef3SKajetan Staszkiewicz 273326c841e2SKristof Provost struct pfi_kkif *pf_kkif_create(int); 273426c841e2SKristof Provost void pf_kkif_free(struct pfi_kkif *); 27355a3b9507SKristof Provost void pf_kkif_zero(struct pfi_kkif *); 2736fcdb520cSKristof Provost 2737fcdb520cSKristof Provost 2738fcdb520cSKristof Provost /* NAT64 functions. */ 2739fcdb520cSKristof Provost int inet_nat64(int, const void *, void *, const void *, u_int8_t); 2740fcdb520cSKristof Provost int inet_nat64_inet(const void *, void *, const void *, u_int8_t); 2741fcdb520cSKristof Provost int inet_nat64_inet6(const void *, void *, const void *, u_int8_t); 2742fcdb520cSKristof Provost 2743fcdb520cSKristof Provost int inet_nat46(int, const void *, void *, const void *, u_int8_t); 2744fcdb520cSKristof Provost int inet_nat46_inet(const void *, void *, const void *, u_int8_t); 2745fcdb520cSKristof Provost int inet_nat46_inet6(const void *, void *, const void *, u_int8_t); 2746fcdb520cSKristof Provost 27473b3a8eb9SGleb Smirnoff #endif /* _KERNEL */ 27483b3a8eb9SGleb Smirnoff 27493b3a8eb9SGleb Smirnoff #endif /* _NET_PFVAR_H_ */ 2750