git: 0dddcc657676 - stable/13 - pfsync: Correctly check if bpf peers are present

From: Zhenlei Huang <zlei_at_FreeBSD.org>
Date: Mon, 17 Jun 2024 04:28:03 UTC
The branch stable/13 has been updated by zlei:

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

commit 0dddcc6576768806aab1967289a349a65fe7cd3d
Author:     Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2024-06-09 01:05:22 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2024-06-17 04:16:38 +0000

    pfsync: Correctly check if bpf peers are present
    
    On creating the pfsync(4) interface, pfsync_clone_create() does an
    unconditional bpfattach(). Use bpf_peers_present() which was introduced
    in commit 16d878cc99ef [1] to check the presence of bpf peers.
    
    This will save a little CPU cycles and memory usage when the
    synchronisation interface is not configured and there is no bpf peers
    present. There should be no functional change.
    
    1. 16d878cc99ef Fix the following bpf(4) race condition which can result in a panic
    
    Reviewed by:    kp
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D45533
    
    (cherry picked from commit 2671bde99295d9e01d10316d0f3fb8b6d21f0f4d)
    (cherry picked from commit f14b540dc4c17f6b60e23274153985fb7a2f0cb7)
---
 sys/netpfil/pf/if_pfsync.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/sys/netpfil/pf/if_pfsync.c b/sys/netpfil/pf/if_pfsync.c
index d4d3c521a568..1dde4e52e3f8 100644
--- a/sys/netpfil/pf/if_pfsync.c
+++ b/sys/netpfil/pf/if_pfsync.c
@@ -1587,7 +1587,7 @@ pfsync_sendout(int schedswi, int c)
 	    ("%s: sc_len %zu", __func__, b->b_len));
 	PFSYNC_BUCKET_LOCK_ASSERT(b);
 
-	if (ifp->if_bpf == NULL && sc->sc_sync_if == NULL) {
+	if (!bpf_peers_present(ifp->if_bpf) && sc->sc_sync_if == NULL) {
 		pfsync_drop(sc);
 		return;
 	}
@@ -1689,10 +1689,10 @@ pfsync_sendout(int schedswi, int c)
 	V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_EOF]++;
 
 	/* we're done, let's put it on the wire */
-	if (ifp->if_bpf) {
+	if (bpf_peers_present(ifp->if_bpf)) {
 		m->m_data += sizeof(*ip);
 		m->m_len = m->m_pkthdr.len = b->b_len - sizeof(*ip);
-		BPF_MTAP(ifp, m);
+		bpf_mtap(ifp->if_bpf, m);
 		m->m_data -= sizeof(*ip);
 		m->m_len = m->m_pkthdr.len = b->b_len;
 	}