git: 2cb0fce24d64 - main - bpf: Make BPF interop consistent with if_loop

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Fri, 19 Apr 2024 18:48:53 UTC
The branch main has been updated by markj:

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

commit 2cb0fce24d64039090dc9243cdf0715ee80c91b1
Author:     Seth Hoffert <seth.hoffert@gmail.com>
AuthorDate: 2023-10-22 14:12:45 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-04-19 18:48:37 +0000

    bpf: Make BPF interop consistent with if_loop
    
    The pseudo_AF_HDRCMPLT check is already being done in if_loop and
    just needed to be ported over to if_ic, if_wg, if_disc, if_gif,
    if_gre, if_me, if_tuntap and ng_iface.  This is needed in order to
    allow these interfaces to work properly with e.g., tcpreplay.
    
    PR:             256587
    Reviewed by:    markj
    MFC after:      2 weeks
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/876
---
 sys/dev/iicbus/if_ic.c  | 4 ++--
 sys/dev/wg/if_wg.c      | 3 ++-
 sys/net/if_disc.c       | 2 +-
 sys/net/if_gif.c        | 3 ++-
 sys/net/if_gre.c        | 3 ++-
 sys/net/if_me.c         | 3 ++-
 sys/net/if_tuntap.c     | 2 +-
 sys/netgraph/ng_iface.c | 2 +-
 8 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/sys/dev/iicbus/if_ic.c b/sys/dev/iicbus/if_ic.c
index 4ca8f3960298..52ab5afb9c4e 100644
--- a/sys/dev/iicbus/if_ic.c
+++ b/sys/dev/iicbus/if_ic.c
@@ -363,8 +363,8 @@ icoutput(if_t ifp, struct mbuf *m, const struct sockaddr *dst,
 	u_char *cp;
 	u_int32_t hdr;
 
-	/* BPF writes need to be handled specially. */ 
-	if (dst->sa_family == AF_UNSPEC)
+	/* BPF writes need to be handled specially. */
+	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
 		bcopy(dst->sa_data, &hdr, sizeof(hdr));
 	else 
 		hdr = RO_GET_FAMILY(ro, dst);
diff --git a/sys/dev/wg/if_wg.c b/sys/dev/wg/if_wg.c
index bb61917ee4fc..d3a5a29e4c08 100644
--- a/sys/dev/wg/if_wg.c
+++ b/sys/dev/wg/if_wg.c
@@ -2196,7 +2196,8 @@ wg_output(if_t ifp, struct mbuf *m, const struct sockaddr *dst, struct route *ro
 	int ret;
 	struct mbuf *defragged;
 
-	if (dst->sa_family == AF_UNSPEC)
+	/* BPF writes need to be handled specially. */
+	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
 		memcpy(&af, dst->sa_data, sizeof(af));
 	else
 		af = dst->sa_family;
diff --git a/sys/net/if_disc.c b/sys/net/if_disc.c
index f4b51b799d73..9740a25f35c6 100644
--- a/sys/net/if_disc.c
+++ b/sys/net/if_disc.c
@@ -180,7 +180,7 @@ discoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
 	M_ASSERTPKTHDR(m);
 
 	/* BPF writes need to be handled specially. */
-	if (dst->sa_family == AF_UNSPEC)
+	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
 		bcopy(dst->sa_data, &af, sizeof(af));
 	else
 		af = RO_GET_FAMILY(ro, dst);
diff --git a/sys/net/if_gif.c b/sys/net/if_gif.c
index e5065889d732..ef64c15074ed 100644
--- a/sys/net/if_gif.c
+++ b/sys/net/if_gif.c
@@ -408,7 +408,8 @@ gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
 	KASSERT(ifp->if_bridge == NULL,
 	    ("%s: unexpectedly called with bridge attached", __func__));
 
-	if (dst->sa_family == AF_UNSPEC)
+	/* BPF writes need to be handled specially. */
+	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
 		memcpy(&af, dst->sa_data, sizeof(af));
 	else
 		af = RO_GET_FAMILY(ro, dst);
diff --git a/sys/net/if_gre.c b/sys/net/if_gre.c
index 55163416f807..ca9c4835daf6 100644
--- a/sys/net/if_gre.c
+++ b/sys/net/if_gre.c
@@ -609,7 +609,8 @@ gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
 {
 	uint32_t af;
 
-	if (dst->sa_family == AF_UNSPEC)
+	/* BPF writes need to be handled specially. */
+	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
 		bcopy(dst->sa_data, &af, sizeof(af));
 	else
 		af = RO_GET_FAMILY(ro, dst);
diff --git a/sys/net/if_me.c b/sys/net/if_me.c
index 126fe680e65f..b839730d2e37 100644
--- a/sys/net/if_me.c
+++ b/sys/net/if_me.c
@@ -538,7 +538,8 @@ me_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
 {
 	uint32_t af;
 
-	if (dst->sa_family == AF_UNSPEC)
+	/* BPF writes need to be handled specially. */
+	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
 		bcopy(dst->sa_data, &af, sizeof(af));
 	else
 		af = RO_GET_FAMILY(ro, dst);
diff --git a/sys/net/if_tuntap.c b/sys/net/if_tuntap.c
index 1009dc7b3806..a70efe79cbb5 100644
--- a/sys/net/if_tuntap.c
+++ b/sys/net/if_tuntap.c
@@ -1410,7 +1410,7 @@ tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
 	}
 
 	/* BPF writes need to be handled specially. */
-	if (dst->sa_family == AF_UNSPEC)
+	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
 		bcopy(dst->sa_data, &af, sizeof(af));
 	else
 		af = RO_GET_FAMILY(ro, dst);
diff --git a/sys/netgraph/ng_iface.c b/sys/netgraph/ng_iface.c
index 8ae4707b7abd..e9f97ff0fdec 100644
--- a/sys/netgraph/ng_iface.c
+++ b/sys/netgraph/ng_iface.c
@@ -367,7 +367,7 @@ ng_iface_output(struct ifnet *ifp, struct mbuf *m,
 	}
 
 	/* BPF writes need to be handled specially. */
-	if (dst->sa_family == AF_UNSPEC)
+	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
 		bcopy(dst->sa_data, &af, sizeof(af));
 	else
 		af = RO_GET_FAMILY(ro, dst);