Hello,

I took a look at it to see what's going on here. I have explanation,
but I'm not sure I would start adventure trying to fix it.

I'm using slightly modified rules what match pf test bed
set up:

    block return all
    pass all flags S/SA
    pass in on vio1 inet6 from fdd7:e83e:66bc:210::/64 to \
        fdd7:e83e:66bc:211:5054:ff::/96 flags S/SA af-to inet from (vio2) 
round-robin

as soon as IPv6 packets coming over vio1 interface hits the af-to rule
it is translated such:
    source address is replaced with IPv4 address attached to vio2 interface

    destination address comes from lowest 32-bits of IPb6 destination in
    original IPv6 packet.

once pf_test() function finds rule/matching states it arrives to
code here (comes from end of pf_test()):

    8809 #ifdef INET6
    8810         case PF_AFRT:
    8811                 if (pf_translate_af(&pd)) {
    8812                         action = PF_DROP;
    8813                         goto out;
    8814                 }
    8815                 pd.m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
    8816                 switch (pd.naf) {
    8817                 case AF_INET:
    8818                         if (pd.dir == PF_IN) {
    8819                                 int flags = IP_REDIRECT;
    8820
    8821                                 switch 
(atomic_load_int(&ip_forwarding)) {
    8822                                 case 2:
    8823                                         SET(flags, 
IP_FORWARDING_IPSEC);
    8824                                         /* FALLTHROUGH */
    8825                                 case 1:
    8826                                         SET(flags, IP_FORWARDING);
    8827                                         break;
    8828                                 default:
    8829                                         ipstat_inc(ips_cantforward);
    8830                                         action = PF_DROP;
    8831                                         goto out;
    8832                                 }
    8833                                 if (atomic_load_int(&ip_directedbcast))
    8834                                         SET(flags, IP_ALLOWBROADCAST);
    8835                                 ip_forward(pd.m, ifp, NULL, flags);
    8836                         } else
    8837                                 ip_output(pd.m, NULL, NULL, 0, NULL, 
NULL, 0);
    8838                         break;
    8839                 case AF_INET6:
    8840                         if (pd.dir == PF_IN) {
    8841                                 int flags = IPV6_REDIRECT;
    8842
    8843                                 switch 
(atomic_load_int(&ip6_forwarding)) {
    8844                                 case 2:
    8845                                         SET(flags, 
IPV6_FORWARDING_IPSEC);
    8846                                         /* FALLTHROUGH */
    8847                                 case 1:
    8848                                         SET(flags, IPV6_FORWARDING);
    8849                                         break;


The inbound IPv6 packet got translated to IPv6, hence naf is AF_INET,
therefore pf_test() executes case block 8818 - 8837. However make sure
you note line 8815 where pf_test() sets PF_TAG_GENERATED flag to packet
that is forwarded to stack at line 8835.

ip_forward() calls ip_output() which in turn calls pf_test(). the very
first thing pf_test() does it checks for PF_TAG_GENERATED:

    8400 #ifdef DIAGNOSTIC
    8401         if (((*m0)->m_flags & M_PKTHDR) == 0)
    8402                 panic("non-M_PKTHDR is passed to pf_test");
    8403 #endif /* DIAGNOSTIC */
    8404
    8405         if ((*m0)->m_pkthdr.pf.flags & PF_TAG_GENERATED)
    8406                 return (PF_PASS);
    8407
    8408         if ((*m0)->m_pkthdr.pf.flags & PF_TAG_DIVERTED_PACKET) {
    8409                 (*m0)->m_pkthdr.pf.flags &= ~PF_TAG_DIVERTED_PACKET;
    8410                 return (PF_PASS);
    8411         }

as you can see if PF_TAG_GENERATED is found the pf_test() returns with PF_PASS
verdict. effectively bypassing all tests for outbound rules.

this explains the distortion in stats you are seeing. According to history
the behavior is present since 2016.

are distorted stats the only issue/symptom you are concerned of? or is there
other more important reason we should change that?

thanks and
regards
sashan

Reply via email to