Index | Thread | Search

From:
David Gwynne <david@gwynne.id.au>
Subject:
add an opaque driver settable identifier to struct ifnet
To:
tech@openbsd.org
Date:
Fri, 13 Nov 2026 14:10:05 +1000

Download raw body.

Thread
there's a few places where specific drivers need to know if another
interface is backed by the same driver. at the moment they abuse a
function pointer to check this, but that's brittle because things like
trunks and bridges fiddle with function pointers on their port
interfaces to mask certain behaviours.

this adds a dedicated driver settable identifier for this purpose
instead, and then uses it in the appropriate drivers. a driver can point
this at anything it wants to act as an identifier. as these are all
clonable interfaces, they're all using the address of their if_cloner
struct as the identifier.

this started out because i was looking at replacing rport_enqueue with a
generic function provided by if.h, which would break using it to
identify instances of rport(4).

i believe this also fixes a bug reported by tom smyth in pair(4)
by checking if the type of interface you're trying to join together
is another pair(4) before assuming it has a pair_softc and fiddling
with it.

i intend to reclaim the space used by this pointer in struct ifnet by
getting rid of if_bridgeidx in the future.

Index: if_var.h
===================================================================
RCS file: /cvs/src/sys/net/if_var.h,v
diff -u -p -r1.150 if_var.h
--- if_var.h	4 Nov 2026 08:51:51 -0000	1.150
+++ if_var.h	13 Nov 2026 02:44:33 -0000
@@ -150,6 +150,7 @@ SMR_LIST_HEAD(carp_iflist, carp_softc);
 
 struct ifnet {				/* and the entries */
 	void	*if_softc;		/* [I] lower-level data for this if */
+	void	*if_drvid;		/* [I] opaque driver identifier */
 	struct	refcnt if_refcnt;
 	TAILQ_ENTRY(ifnet) if_list;	/* [NK] all struct ifnets are chained */
 	TAILQ_ENTRY(ifnet) if_tmplist;	/* [T] temporary list */
Index: if_pair.c
===================================================================
RCS file: /cvs/src/sys/net/if_pair.c,v
diff -u -p -r1.18 if_pair.c
--- if_pair.c	7 Jul 2025 02:28:50 -0000	1.18
+++ if_pair.c	13 Nov 2026 02:44:33 -0000
@@ -51,6 +51,12 @@ struct pair_softc {
 struct if_clone	pair_cloner =
     IF_CLONE_INITIALIZER("pair", pair_clone_create, pair_clone_destroy);
 
+static inline int
+if_is_pair(struct ifnet *ifp)
+{
+	return (ifp->if_drvid == &pair_cloner);
+}
+
 int
 pair_media_change(struct ifnet *ifp)
 {
@@ -113,6 +119,7 @@ pair_clone_create(struct if_clone *ifc, 
 	ether_fakeaddr(ifp);
 
 	ifp->if_softc = sc;
+	ifp->if_drvid = &pair_cloner;
 	ifp->if_ioctl = pairioctl;
 	ifp->if_qstart = pairstart;
 	ifp->if_xflags = IFXF_CLONED | IFXF_MPSAFE;
@@ -191,10 +198,9 @@ pairioctl(struct ifnet *ifp, u_long cmd,
 {
 	struct pair_softc	*sc = (struct pair_softc *)ifp->if_softc;
 	struct ifreq		*ifr = (struct ifreq *)data;
-	struct if_clone		*ifc;
 	struct pair_softc	*pairedsc = ifp->if_softc;
 	struct ifnet		*oldifp = NULL, *newifp = NULL;
-	int			 error = 0, unit;
+	int			 error = 0;
 
 	switch (cmd) {
 	case SIOCSIFADDR:
@@ -231,18 +237,15 @@ pairioctl(struct ifnet *ifp, u_long cmd,
 		newifp = if_get(ifr->ifr_index);
 
 		if (newifp != NULL) {
-			pairedsc = newifp->if_softc;
-
-			if (pairedsc->sc_pairedif != 0) {
-				error = EBUSY;
+			/* Only allow pair(4) interfaces for the pair */
+			if (!if_is_pair(newifp)) {
+				error = ENODEV;
 				break;
 			}
 
-			/* Only allow pair(4) interfaces for the pair */
-			if ((ifc = if_clone_lookup(newifp->if_xname,
-			    &unit)) == NULL || strcmp("pair",
-			    ifc->ifc_name) != 0) {
-				error = ENODEV;
+			pairedsc = newifp->if_softc;
+			if (pairedsc->sc_pairedif != 0) {
+				error = EBUSY;
 				break;
 			}
 
Index: if_rport.c
===================================================================
RCS file: /cvs/src/sys/net/if_rport.c,v
diff -u -p -r1.14 if_rport.c
--- if_rport.c	7 Nov 2026 12:50:08 -0000	1.14
+++ if_rport.c	13 Nov 2026 02:44:33 -0000
@@ -82,6 +82,12 @@ rportattach(int count)
 	if_clone_attach(&rport_cloner);
 }
 
+static inline int
+if_is_rport(struct ifnet *ifp)
+{
+	return (ifp->if_drvid == &rport_cloner);
+}
+
 static int
 rport_clone_create(struct if_clone *ifc, int unit)
 {
@@ -94,6 +100,8 @@ rport_clone_create(struct if_clone *ifc,
 	snprintf(ifp->if_xname, sizeof(ifp->if_xname),
 	    "%s%d", ifc->ifc_name, unit);
 
+	ifp->if_softc = sc;
+	ifp->if_drvid = &rport_cloner;
 	ifp->if_mtu = RPORT_MTU_DEFAULT;
 	ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
 	ifp->if_xflags = IFXF_CLONED | IFXF_MPSAFE;
@@ -105,7 +113,6 @@ rport_clone_create(struct if_clone *ifc,
 	ifp->if_input = p2p_input;
 	ifp->if_rtrequest = p2p_rtrequest;
 	ifp->if_type = IFT_TUNNEL;
-	ifp->if_softc = sc;
 
 	ifp->if_capabilities |= IFCAP_CSUM_IPv4;
 	ifp->if_capabilities |= IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
@@ -359,7 +366,7 @@ rport_set_parent(struct rport_softc *sc,
 		goto put;
 	}
 
-	if (ifp0->if_enqueue != rport_enqueue) {
+	if (!if_is_rport(ifp0)) {
 		error = EPROTONOSUPPORT;
 		goto put;
 	}
Index: if_veb.c
===================================================================
RCS file: /cvs/src/sys/net/if_veb.c,v
diff -u -p -r1.74 if_veb.c
--- if_veb.c	17 Oct 2026 16:12:45 -0000	1.74
+++ if_veb.c	13 Nov 2026 02:44:33 -0000
@@ -413,6 +413,12 @@ static int	vport_iff(struct vport_softc 
 static struct if_clone vport_cloner =
     IF_CLONE_INITIALIZER("vport", vport_clone_create, vport_clone_destroy);
 
+static inline int
+if_is_vport(struct ifnet *ifp)
+{
+	return (ifp->if_drvid == &vport_cloner);
+}
+
 void
 vebattach(int count)
 {
@@ -809,7 +815,7 @@ veb_pf(struct ifnet *ifp0, int dir, stru
 	 * pf direction on vport interfaces is reversed compared to
 	 * other veb ports.
 	 */
-	if (ifp0->if_enqueue == vport_enqueue)
+	if (if_is_vport(ifp0))
 		return (m);
 
 	eh = mtod(m, struct ether_header *);
@@ -1759,7 +1765,7 @@ veb_add_port(struct veb_softc *sc, const
 		goto put;
 	}
 
-	isvport = (ifp0->if_enqueue == vport_enqueue);
+	isvport = if_is_vport(ifp0);
 
 	error = ether_brport_isset(ifp0);
 	if (error != 0)
@@ -1986,7 +1992,7 @@ veb_port_set_pvid(struct veb_softc *sc, 
 		return (ESRCH);
 
 	if ((pvid < EVL_VLID_MIN || pvid > EVL_VLID_MAX) &&
-	    p->p_ifp0->if_enqueue == vport_enqueue) {
+	    if_is_vport(p->p_ifp0)) {
 		error = EOPNOTSUPP;
 		goto put;
 	}
@@ -2030,7 +2036,7 @@ veb_get_vid_map(struct veb_softc *sc, st
 	}
 	smr_read_leave();
 
-	if (p->p_ifp0->if_enqueue == vport_enqueue && !anybits)
+	if (if_is_vport(p->p_ifp0) && !anybits)
 		error = ENOENT;
 
 	veb_port_put(sc, p);
@@ -3522,6 +3528,7 @@ vport_clone_create(struct if_clone *ifc,
 	snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
 
 	ifp->if_softc = sc;
+	ifp->if_drvid = &vport_cloner;
 	ifp->if_type = IFT_ETHER;
 	ifp->if_hardmtu = ETHER_MAX_HARDMTU_LEN;
 	ifp->if_ioctl = vport_ioctl;