The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/pfvar.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 6bd5cda4114c3237f7164a118e246379


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.