Hello,

please disregard the earlier diff. I was thinking some more
and come with better change/hack.

the idea is to fix calculation of state statistics for
states created by af-to rule. Those states are kind of
special in sense they always match inbound packets.
they never see outbound packets (because of this PF_TAG_GENERATED)

The idea is to always remember af (address family) in pf_state.
the state remembers the af of packet that created that state.
later we compare the family (mmeber caf) with packet that matched
state. If families match, then we bump ?inbound? stats
if families don't match we bump ?outbound? stats.

the change makes the collected stats more sane. the pflow
should also show some change. it will mostlikely report
it as NAT stats. Not sure if it helps you with interpretation.
let me know, there might be some way around it to turn stats
for af-to state towards regular ones.

just remember to rebuilt pfctl(1) tool too when using/testing the diff.

thanks and
regards
sashan

--------8<---------------8<---------------8<------------------8<--------
diff --git a/sys/net/pf.c b/sys/net/pf.c
index cf31277b231..1b2e3cfaaa3 100644
--- a/sys/net/pf.c
+++ b/sys/net/pf.c
@@ -1679,6 +1679,7 @@ pf_state_export(struct pfsync_state *sp, struct pf_state 
*st)
        sp->set_tos = st->set_tos;
        sp->set_prio[0] = st->set_prio[0];
        sp->set_prio[1] = st->set_prio[1];
+       sp->caf = st->caf;
 }
 
 int
@@ -1856,6 +1857,8 @@ pf_state_import(const struct pfsync_state *sp, int flags)
        st->rule.ptr = r;
        st->anchor.ptr = NULL;
 
+       st->caf = sp->caf;
+
        PF_REF_INIT(st->refcnt);
        mtx_init(&st->mtx, IPL_NET);
 
@@ -5279,6 +5282,7 @@ pf_create_state(struct pf_pdesc *pd, struct pf_rule *r, 
struct pf_rule *a,
                }
        }
        st->direction = pd->dir;
+       st->caf = pd->af;
 
        if (pf_state_key_setup(pd, skw, sks, act->rtableid)) {
                REASON_SET(&reason, PFRES_MEMORY);
@@ -8281,7 +8285,22 @@ pf_st_counters_inc(struct pf_pdesc *pd, struct pf_state 
*st, int dirndx)
                        sni->sn->packets[dirndx]++;
                        sni->sn->bytes[dirndx] += pd->tot_len;
                }
-               dirndx = (pd->dir == st->direction) ? 0 : 1;
+               /*
+                * states which do AF translation are created on
+                * on inbound interface. Packet is then forwarded
+                * to its outbound interface. pf(4) never creates
+                * outbound state for AF translated packets.
+                * pf(4) uses af in current packet and af kept
+                * in state for packet that created state to
+                * derive a packet/flow direction.
+                */
+               if (pd->af == st->caf)
+                       dirndx = (pd->dir == st->direction) ? 0 : 1;
+               else if (pd->af == st->caf)
+                       dirndx = 0;
+               else
+                       dirndx = 1;
+
                st->packets[dirndx]++;
                st->bytes[dirndx] += pd->tot_len;
 
diff --git a/sys/net/pfvar.h b/sys/net/pfvar.h
index 750fe0ef144..3bd18c35f02 100644
--- a/sys/net/pfvar.h
+++ b/sys/net/pfvar.h
@@ -807,6 +807,7 @@ struct pfsync_state {
        u_int8_t         set_tos;
        u_int16_t        state_flags;
        u_int8_t         set_prio[2];
+       sa_family_t      caf;
 } __packed;
 
 #define PFSYNC_FLAG_SRCNODE    0x04
diff --git a/sys/net/pfvar_priv.h b/sys/net/pfvar_priv.h
index d8bc3cb4365..78c819c548c 100644
--- a/sys/net/pfvar_priv.h
+++ b/sys/net/pfvar_priv.h
@@ -143,7 +143,8 @@ struct pf_state {
        u_int64_t                id;            /* [I] */
        u_int32_t                creatorid;     /* [I] */
        u_int8_t                 direction;     /* [I] */
-       u_int8_t                 pad[3];
+       sa_family_t              caf;           /* [I] */
+       u_int8_t                 pad[2];
 
        TAILQ_ENTRY(pf_state)    sync_list;     /* [S] */
        struct pfsync_deferral  *sync_defer;    /* [S] */

Reply via email to