Bug 291763 - pfctl: loginterface packet counts for pass/block swapped; byte counts all 0
Summary: pfctl: loginterface packet counts for pass/block swapped; byte counts all 0
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 15.0-RELEASE
Hardware: Any Any
: --- Affects Only Me
Assignee: Kristof Provost
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-12-18 05:56 UTC by eborisch+FreeBSD
Modified: 2026-07-18 19:11 UTC (History)
1 user (show)

See Also:
linimon: mfc-stable14?


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description eborisch+FreeBSD 2025-12-18 05:56:11 UTC
It appears that the "Packets Out" values printed by pfctl for Passed/Blocked
(present when a 'loginterface' is set in pf.conf) are incorrect.

I'm connecting to bugzilla over said "cable" interface, so I don't believe
the "Passed" count of 0, and I just took way too long trying to figure out
what in the world was generating so much outbound traffic getting blocked
before noticing the "0" passed. (I don't believe 0 "passed" value outbound
is actually the "blocked" count, either -- it may be true in this instance,
but in testing on another 15.0-p1 box, it does not increment even when an 
outbound packet is intentionally blocked on the interface.)

Current output on my 15.0-p1 router:

root@router:~ # pfctl -s info
Status: Enabled for 0 days 03:39:23           Debug: Urgent

Interface Stats for cable             IPv4             IPv6
  Bytes In                               0                0
  Bytes Out                              0                0
  Packets In
    Passed                        14816644                0
    Blocked                           2157                0
  Packets Out
    Passed                               0                0  <--- Likely not the "blocked" value, either
    Blocked                       10950797                0  <--- Signs point to this being the "passed" value.
Comment 1 eborisch+FreeBSD 2025-12-18 21:11:29 UTC
Reverting a3f71765 (https://cgit.freebsd.org/src/commit/?h=releng/15.0&id=a3f71765)
seems to fix it; but I'm unaware of the background / ill effects of reverting
that commit, but pf_status (pf.h) still has pcounter[2][2][3] arrays.


Contents of a3f71765 for reference:


libpfctl: fix incorrect pcounters array size
The array is 2 x 2 x 2, not 2 x 2 x 3.

Sponsored by:	Rubicon Communications, LLC ("Netgate")
MFC after:	2 weeks
Diffstat
-rw-r--r--	lib/libpfctl/libpfctl.c	2	
-rw-r--r--	lib/libpfctl/libpfctl.h	2	
2 files changed, 2 insertions, 2 deletions
diff --git a/lib/libpfctl/libpfctl.c b/lib/libpfctl/libpfctl.c
index 076cfd3400e6..b42afc542273 100644
--- a/lib/libpfctl/libpfctl.c
+++ b/lib/libpfctl/libpfctl.c
@@ -325,7 +325,7 @@ pfctl_get_status(int dev)
 	_pfctl_get_status_counters(nvlist_get_nvlist(nvl, "scounters"),
 	    &status->scounters);
 
-	pf_nvuint_64_array(nvl, "pcounters", 2 * 2 * 3,
+	pf_nvuint_64_array(nvl, "pcounters", 2 * 2 * 2,
 	    (uint64_t *)status->pcounters, NULL);
 	pf_nvuint_64_array(nvl, "bcounters", 2 * 2,
 	    (uint64_t *)status->bcounters, NULL);
diff --git a/lib/libpfctl/libpfctl.h b/lib/libpfctl/libpfctl.h
index 4e06981a79c7..bdb4bfae4be1 100644
--- a/lib/libpfctl/libpfctl.h
+++ b/lib/libpfctl/libpfctl.h
@@ -62,7 +62,7 @@ struct pfctl_status {
 	struct pfctl_status_counters	 lcounters;
 	struct pfctl_status_counters	 fcounters;
 	struct pfctl_status_counters	 scounters;
-	uint64_t	pcounters[2][2][3];
+	uint64_t	pcounters[2][2][2];
 	uint64_t	bcounters[2][2];
 };
Comment 2 eborisch+FreeBSD 2025-12-18 23:13:46 UTC
Of note (I don't scrape these values from pfctl, and I didn't notice when
writing this up), the 'Bytes In'/Out display doesn't seem to be functioning
at all. (All zeros, all the time.)
Comment 3 Kristof Provost freebsd_committer freebsd_triage 2025-12-19 16:53:45 UTC
That's ... odd.

The array really is 2 * 2 * 2, and moreover, you've reverted a patch in the nvlist based ioctl call, but in 15 (lib)pfctl gathers that information via netlink.
Comment 4 eborisch+FreeBSD 2026-01-15 23:02:19 UTC
Not sure what to say other than it (reverting that commit) seems to work:

## BEFORE

# /.zfs/snapshot/2026-01-15-16:41:56/sbin/pfctl -s info | head -n 12
Status: Enabled for 28 days 21:05:29          Debug: Urgent

Interface Stats for cable             IPv4             IPv6
  Bytes In                               0                0
  Bytes Out                              0                0
  Packets In
    Passed                       908277654              115
    Blocked                         413880                0
  Packets Out
    Passed                               0                0
    Blocked                      392875690                0


## AFTER

# pfctl -s info | head -n 12
Status: Enabled for 28 days 21:05:31          Debug: Urgent

Interface Stats for cable             IPv4             IPv6
  Bytes In                               0                0
  Bytes Out                              0                0
  Packets In
    Passed                       908277678                0
    Blocked                         413880                0
  Packets Out
    Passed                       392875724                0
    Blocked                            115                0


And those are the correct numbers; if I put in a rule to block outgoing to port 12345, and then try to use it, I see the blocked count go up:


# nc www.freebsd.org 12345
# pfctl -s info | head -n 12
Status: Enabled for 28 days 21:05:34          Debug: Urgent

Interface Stats for cable             IPv4             IPv6
  Bytes In                               0                0
  Bytes Out                              0                0
  Packets In
    Passed                       908277708                0
    Blocked                         413880                0
  Packets Out
    Passed                       392875766                0
    Blocked                            116                0

# freebsd-version
15.0-RELEASE-p1
Comment 5 eborisch+FreeBSD 2026-02-09 21:48:47 UTC
I think I've pinned this down.

pfctl (in pfctl_show_status()) calls pfctl_get_status_h().

[lib/libpfctl/libpfctl.c]
pfctl_get_status_h() {
  hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GET_STATUS);
}

which appears to call pf_handle_get_status()

[sys/netpfil/pf/pf_nl.c]
pf_handle_get_status() {
...
nlattr_add_u64_array(nw, PF_GS_PCOUNTERS, 2 * 2 * 2, (uint64_t *)s.pcounters);
...
}

but the s here is pf_status, and in [sys/netpfil/pf/pf.h]
struct pf_status {
  [...]
  uint64_t        pcounters[2][2][3];
  [...]
}

so copying 8 bytes out of s.pcounters (above) in pf_handle_get_status() will end up copying the wrong values.

It would seem pf_nl.c should be using something similar to the parsing done in 
[sys/netpfil/pf/pf_ioctl.c]

pf_getstatus() {
...
for (int i = 0; i < 2; i++) {
                  for (int j = 0; j < 2; j++) {
                          for (int k = 0; k < 2; k++) {
                                  nvlist_append_number_array(nvl, "pcounters",
                                      s.pcounters[i][j][k]);
                          }
...
}

to peel out the first two elements of the last index.
Comment 6 Kristof Provost freebsd_committer freebsd_triage 2026-02-10 09:38:08 UTC
(In reply to eborisch+FreeBSD from comment #5)
15 still has the bug where the pcounters array is too big, but that's been fixed in main. Other than a little wasted memory this does not matter.

I also don't understand why you think pf_getstatus() demonstrates different, because it clearly treats the array as a 2 * 2 * 2 as well.
Note also that pf_getstatus() implements the nvlist based method for retrieving these counters, which is not what pfctl uses in 15. It uses the netlink interface instead.

It's also interesting to look at pfi_update_status(), where pcounters is populated, which also treats it as a 2 * 2 * 2 array.
Comment 7 eborisch+FreeBSD 2026-02-10 14:59:47 UTC
> It's also interesting to look at pfi_update_status(), where pcounters is 
> populated, which also treats it as a 2 * 2 * 2 array.

In 15, with the definition of pf_status.pcounters[2][2][3], the pfi_update_status() code extracts out the [2][2][2] lower sub-array by indexing [i][j][k] with the loop.

In contrast, 'pfctl -s info' hits pf_handle_get_status(), which _casts pf_status.pcounters[2][2][3] to a pointer_ and copies the first 8 elements into its output. This should work on main with the right-sized pf_status.pcounters, but leads downstream to the incorrect output from ‘pfctl -s info’ (with 'set loginterface ifname') on 15.

If the resize to pf_status.pcounters[2][2][2] lands in 15 / 15.0, we should be set. If the desire is to not resize the kernel structure within stable/15, then pf_handle_get_status() needs an [i][j][k] extraction loop like pfi_update_status(). I can create a patch with those changes and push it up to GitHub, if you like.

15.0-p2:

# pfctl -s info
Status: Enabled for 4 days 12:54:12           Debug: Urgent

Interface Stats for vtnet0            IPv4             IPv6
  Bytes In                               0                0  # Stays zero
  Bytes Out                              0                0  # Stays zero
  Packets In
    Passed                         8979115               24  # Correct
    Blocked                          20600                0  # Correct
  Packets Out
    Passed                               0                0  # Stays zero
    Blocked                        6507902                0  # Actually Passed Out


vs. 14.3 on a sibling system:

# pfctl -s info
Status: Enabled for 6 days 04:51:56           Debug: Urgent

Interface Stats for vmx0              IPv4             IPv6
  Bytes In                     89609864859                0
  Bytes Out                    89429385267                0
  Packets In
    Passed                       116945513                0
    Blocked                        1061014                0
  Packets Out
    Passed                        93152637                0
    Blocked                            629                0
Comment 8 eborisch+FreeBSD 2026-02-10 20:40:03 UTC
Hrm. Even with my suggested array-size-munging loops (to match pfi_update_status()),
the bytes in/bytes remain stuck at zero.

The Packets Out Passed/Blocked are correct, at least:

 # pfctl -s info
Status: Enabled for 0 days 00:06:13           Debug: Urgent

Interface Stats for vtnet0            IPv4             IPv6
  Bytes In                               0                0
  Bytes Out                              0                0
  Packets In
    Passed                           19899                0
    Blocked                              0                0
  Packets Out
    Passed                           10480                0
    Blocked                              1                0

I've verified the bytes counts are set (non-zero) in pf_status.bcounters when
pfi_update_status() is called, but they aren't making it through -- zeros in
pfctl_status.bcounters immediately before return in pfctl_get_status_h().
Comment 9 eborisch+FreeBSD 2026-02-12 00:42:02 UTC
Found the source of zero byte counters; snl attribute parser wasn't sorted:

https://github.com/freebsd/freebsd-src/pull/2017
Comment 10 eborisch+FreeBSD 2026-02-12 06:06:52 UTC
(Apologies for the GitHub noise; the previous fix is actually 2018 now.)

Proposed (for stable/15; [2][2][3]) sub-array buffering:

https://github.com/freebsd/freebsd-src/pull/2019

With both 2018 and 2019 applied, have byte counters and correct packets out counters:

$ pfctl -s info
Status: Enabled for 0 days 00:45:07           Debug: Urgent

Interface Stats for eth0              IPv4             IPv6
  Bytes In                         3722870                0
  Bytes Out                        7111105                0
  Packets In
    Passed                            8685                0
    Blocked                           8337                0
  Packets Out
    Passed                            7043                0
    Blocked                             26                0
Comment 11 Kristof Provost freebsd_committer freebsd_triage 2026-02-12 09:06:54 UTC
(In reply to eborisch+FreeBSD from comment #9)
Ah. Yes. That does make a lot of sense. I'll try to get this merged this week.
Comment 12 commit-hook freebsd_committer freebsd_triage 2026-02-12 21:40:29 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=363b57d579bafa8a52cfb5a1dcb98af821b1ecb6

commit 363b57d579bafa8a52cfb5a1dcb98af821b1ecb6
Author:     Eric A. Borisch <eborisch@gmail.com>
AuthorDate: 2026-02-12 00:26:26 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2026-02-12 16:05:16 +0000

    libpfctl: Sort order of  snl attribute parser

    snl atttribute parsers must be sorted by type, so PF_GS_BCOUNTERS
    (16) must follow PF_GF_PCOUNTERS (15). Fix ordering and add a call
    to SNL_VERIFY_PARSERS.

    Without this fix, byte counters reported by 'pfctl -s info' with
    a loginterface are always zero.

    PR:             291763
    MFC after:      1 week
    Reviewed by:    kp
    Signed-off-by:  eborisch@gmail.com

 lib/libpfctl/libpfctl.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
Comment 13 commit-hook freebsd_committer freebsd_triage 2026-02-12 21:40:30 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=d03b6bb766f8c816547f9c39e05af8238242251e

commit d03b6bb766f8c816547f9c39e05af8238242251e
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2026-02-12 13:06:34 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2026-02-12 16:05:16 +0000

    pf tests: verify blocked count on log interface

    Also check the IPv6 pass/block numbers.

    PR:             291763
    MFC after:      1 week
    Sponsored by:   Rubicon Communications, LLC ("Netgate")

 tests/sys/netpfil/pf/loginterface.sh | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
Comment 14 eborisch+FreeBSD 2026-02-17 19:36:34 UTC
Any further thoughts on https://github.com/freebsd/freebsd-src/pull/2019 ?

Either the structure needs to change to [2][2][2] on stable/15, or the i,j,k extraction loop is needed.
Comment 15 Kristof Provost freebsd_committer freebsd_triage 2026-02-17 19:57:57 UTC
(In reply to eborisch+FreeBSD from comment #14)
The array size fix was already merged back. I intend to merge your fix back there as well soon. That includes a test case, so if we're still missing something (and I don't think we are) we should spot that quickly.
Comment 16 eborisch+FreeBSD 2026-02-17 20:07:54 UTC
(In reply to Kristof Provost from comment #15)

Perfect. Apologies for missing the size fix landing, and thanks for your help!
Comment 17 commit-hook freebsd_committer freebsd_triage 2026-02-19 16:01:23 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=4d0186cd12a79269d5ddb607eff8deebe4667f67

commit 4d0186cd12a79269d5ddb607eff8deebe4667f67
Author:     Eric A. Borisch <eborisch@gmail.com>
AuthorDate: 2026-02-12 00:26:26 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2026-02-19 16:00:19 +0000

    libpfctl: Sort order of  snl attribute parser

    snl atttribute parsers must be sorted by type, so PF_GS_BCOUNTERS
    (16) must follow PF_GF_PCOUNTERS (15). Fix ordering and add a call
    to SNL_VERIFY_PARSERS.

    Without this fix, byte counters reported by 'pfctl -s info' with
    a loginterface are always zero.

    PR:             291763
    MFC after:      1 week
    Reviewed by:    kp
    Signed-off-by:  eborisch@gmail.com

    (cherry picked from commit 363b57d579bafa8a52cfb5a1dcb98af821b1ecb6)

 lib/libpfctl/libpfctl.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
Comment 18 commit-hook freebsd_committer freebsd_triage 2026-02-19 16:01:24 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=dc7eb96ba94580548937632d3a035574fc4feedf

commit dc7eb96ba94580548937632d3a035574fc4feedf
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2026-02-12 13:06:34 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2026-02-19 16:00:19 +0000

    pf tests: verify blocked count on log interface

    Also check the IPv6 pass/block numbers.

    PR:             291763
    MFC after:      1 week
    Sponsored by:   Rubicon Communications, LLC ("Netgate")

    (cherry picked from commit d03b6bb766f8c816547f9c39e05af8238242251e)

 tests/sys/netpfil/pf/loginterface.sh | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)