Index | Thread | Search

From:
David Gwynne <david@gwynne.id.au>
Subject:
consolidate ether bridge/trunk port handling in struct arpcom
To:
tech@openbsd.org
Date:
Sun, 15 Nov 2026 20:10:20 +1000

Download raw body.

Thread
  • David Gwynne:

    consolidate ether bridge/trunk port handling in struct arpcom

this has the side effect of preventing an ethernet interface being part
of both an aggr/trunk and bridge/veb/tpmr at the same time.

i can't think of a situation where that makes sense. both bridges and
trunks basically take over the interface completely, and often rewrite
function pointers in the struct ifnet as part of that take over. if you
do add an interface to both a bridge and trunk, but remove them in the
wrong order, you'll mess up these function pointers and cause damage.

this is largely mechanical in the bridge and trunk drivers. the big
change is having struct ether_port store what type of port it is so
ether_input knows when the right time to call it is. ive pulled the l2
and l3 ethernet processing apart to make accounting for the ether_port
reference easier, but the code should work much the same.

this will help resolve some of the issues tom smyth has reported on
bugs@.

i'm going to commit this in the next day or two unless someone finds a
problem.

Index: net/if.c
===================================================================
RCS file: /cvs/src/sys/net/if.c,v
diff -u -p -r1.768 if.c
--- net/if.c	4 Nov 2026 08:51:51 -0000	1.768
+++ net/if.c	15 Nov 2026 09:34:07 -0000
@@ -3521,7 +3521,10 @@ ifsetlro(struct ifnet *ifp, int on)
 
 	if (on && !ISSET(ifp->if_xflags, IFXF_LRO)) {
 #if NETHER > 0
-		if (ifp->if_type == IFT_ETHER && ether_brport_isset(ifp)) {
+		const struct ether_port *ep;
+		if (ifp->if_type == IFT_ETHER &&
+		    (ep = ether_port_get_locked(ifp)) != NULL &&
+		    ep->ep_type == ETHER_PORT_BRIDGE) {
 			error = EBUSY;
 			goto out;
 		}
Index: net/if_aggr.c
===================================================================
RCS file: /cvs/src/sys/net/if_aggr.c,v
diff -u -p -r1.56 if_aggr.c
--- net/if_aggr.c	15 Nov 2026 03:47:44 -0000	1.56
+++ net/if_aggr.c	15 Nov 2026 09:34:07 -0000
@@ -1207,7 +1207,7 @@ aggr_add_port(struct aggr_softc *sc, con
 		goto put;
 	}
 
-	error = ether_brport_isset(ifp0);
+	error = ether_port_isset(ifp0);
 	if (error != 0)
 		goto put;
 
@@ -1216,12 +1216,6 @@ aggr_add_port(struct aggr_softc *sc, con
 		goto put;
 	}
 
-	ac0 = (struct arpcom *)ifp0;
-	if (SMR_PTR_GET_LOCKED(&ac0->ac_trport) != NULL) {
-		error = EBUSY;
-		goto put;
-	}
-
 	/* let's try */
 
 	p = malloc(sizeof(*p), M_DEVBUF, M_WAITOK|M_ZERO|M_CANFAIL);
@@ -1250,7 +1244,9 @@ aggr_add_port(struct aggr_softc *sc, con
 	p->p_ether_port.ep_port_take = aggr_cpu_take;
 	p->p_ether_port.ep_port_rele = aggr_cpu_rele;
 	p->p_ether_port.ep_port = p;
+	p->p_ether_port.ep_type = ETHER_PORT_TRUNK;
 
+	ac0 = (struct arpcom *)ifp0;
 	CTASSERT(sizeof(p->p_lladdr) == sizeof(ac0->ac_enaddr));
 	memcpy(p->p_lladdr, ac0->ac_enaddr, sizeof(p->p_lladdr));
 	p->p_ioctl = ifp0->if_ioctl;
@@ -1288,6 +1284,11 @@ aggr_add_port(struct aggr_softc *sc, con
 	task_set(&p->p_dhook, aggr_p_detach, p);
 	if_detachhook_add(ifp0, &p->p_dhook);
 
+	/* check again after the sleeps above */
+	error = ether_port_isset(ifp0);
+	if (error != 0)
+		goto unpromisc;
+
 	task_set(&p->p_rxm_task, aggr_rx, p);
 	ml_init(&p->p_rxm_ml);
 
@@ -1316,13 +1317,8 @@ aggr_add_port(struct aggr_softc *sc, con
 
 	aggr_update_capabilities(sc);
 
-	/*
-	 * modification of ac->ac_trport is protected by NET_LOCK.
-	 */
-
-	KASSERT(SMR_PTR_GET_LOCKED(&ac0->ac_trport) == NULL);
 	aggr_port_take(p); /* for the SMR ptr */
-	SMR_PTR_SET_LOCKED(&ac0->ac_trport, &p->p_ether_port);
+	ether_port_set(ifp0, &p->p_ether_port);
 
 	/* make sure p is visible before handlers can run */
 	membar_producer();
@@ -1335,6 +1331,11 @@ aggr_add_port(struct aggr_softc *sc, con
 
 	return (0);
 
+unpromisc:
+	if (sc->sc_promisc && ifpromisc(ifp0, 0) != 0) {
+		log(LOG_WARNING, "%s add %s: unable to reset promisc\n",
+		    ifp->if_xname, ifp0->if_xname);
+	}
 unmtu:
 	if (aggr_p_set_mtu(p, p->p_mtu) != 0) {
 		log(LOG_WARNING, "%s add %s: unable to reset mtu %u\n",
@@ -1460,8 +1461,7 @@ aggr_p_set_mtu(struct aggr_port *p, uint
 static int
 aggr_p_ioctl(struct ifnet *ifp0, u_long cmd, caddr_t data)
 {
-	struct arpcom *ac0 = (struct arpcom *)ifp0;
-	const struct ether_port *ep = SMR_PTR_GET_LOCKED(&ac0->ac_trport);
+	const struct ether_port *ep = ether_port_get_locked(ifp0);
 	struct aggr_port *p;
 	struct ifreq *ifr = (struct ifreq *)data;
 	int error = 0;
@@ -1533,7 +1533,7 @@ aggr_p_output(struct ifnet *ifp0, struct
 	}
 
 	smr_read_enter();
-	ep = SMR_PTR_GET(&ac0->ac_trport);
+	ep = SMR_PTR_GET(&ac0->ac_port);
 	if (ep != NULL && ep->ep_input == aggr_port_input) {
 		struct aggr_port *p = ep->ep_port;
 		p_output = p->p_output; /* code doesn't go away */
@@ -1575,17 +1575,12 @@ aggr_p_dtor(struct aggr_softc *sc, struc
 	timeout_del(&p->p_current_while_timer);
 	timeout_del(&p->p_wait_while_timer);
 
-	/*
-	 * modification of ac->ac_trport is protected by NET_LOCK.
-	 */
-
-	NET_ASSERT_LOCKED();
-	KASSERT(SMR_PTR_GET_LOCKED(&ac0->ac_trport) == &p->p_ether_port);
+	KASSERT(SMR_PTR_GET_LOCKED(&ac0->ac_port) == &p->p_ether_port);
 	smr_init(&smrdtor);
 
 	ifp0->if_ioctl = p->p_ioctl;
 	ifp0->if_output = p->p_output;
-	SMR_PTR_SET_LOCKED(&ac0->ac_trport, NULL);
+	ether_port_clr(ifp0);
 
 	/* fold the per cpu refcnt back into the port refcnt */
 	CPUMEM_FOREACH(r, &cmi, p->p_cpurefs)
Index: net/if_bridge.c
===================================================================
RCS file: /cvs/src/sys/net/if_bridge.c,v
diff -u -p -r1.383 if_bridge.c
--- net/if_bridge.c	12 Nov 2026 13:59:44 -0000	1.383
+++ net/if_bridge.c	15 Nov 2026 09:34:07 -0000
@@ -150,11 +150,11 @@ struct niqueue bridgeintrq = NIQUEUE_INI
 struct if_clone bridge_cloner =
     IF_CLONE_INITIALIZER("bridge", bridge_clone_create, bridge_clone_destroy);
 
-const struct ether_port bridge_brport = {
-	bridge_input,
-	bridge_take,
-	bridge_rele,
-	NULL,
+const struct ether_port bridge_port = {
+	.ep_input	= bridge_input,
+	.ep_port_take	= bridge_take,
+	.ep_port_rele	= bridge_rele,
+	.ep_type	= ETHER_PORT_BRIDGE,
 };
 
 void
@@ -295,7 +295,7 @@ bridge_ioctl(struct ifnet *ifp, u_long c
 			break;
 		}
 
-		error = ether_brport_isset(ifs);
+		error = ether_port_isset(ifs);
 		if (error != 0) {
 			if_put(ifs);
 			break;
@@ -347,7 +347,7 @@ bridge_ioctl(struct ifnet *ifp, u_long c
 		ifs->if_bridgeidx = ifp->if_index;
 		task_set(&bif->bif_dtask, bridge_ifdetach, bif);
 		if_detachhook_add(ifs, &bif->bif_dtask);
-		ether_brport_set(bif->ifp, &bridge_brport);
+		ether_port_set(bif->ifp, &bridge_port);
 		SMR_SLIST_INSERT_HEAD_LOCKED(&sc->sc_iflist, bif, bif_next);
 		break;
 	case SIOCBRDGDEL:
@@ -550,7 +550,7 @@ bridge_ifremove(struct bridge_iflist *bi
 
 	SMR_SLIST_REMOVE_LOCKED(&sc->sc_iflist, bif, bridge_iflist, bif_next);
 	if_detachhook_del(bif->ifp, &bif->bif_dtask);
-	ether_brport_clr(bif->ifp);
+	ether_port_clr(bif->ifp);
 
 	smr_barrier();
 
Index: net/if_ethersubr.c
===================================================================
RCS file: /cvs/src/sys/net/if_ethersubr.c,v
diff -u -p -r1.309 if_ethersubr.c
--- net/if_ethersubr.c	7 Nov 2026 10:00:05 -0000	1.309
+++ net/if_ethersubr.c	15 Nov 2026 09:34:07 -0000
@@ -375,26 +375,6 @@ ether_output(struct ifnet *ifp, struct m
 	return (if_enqueue(ifp, m));
 }
 
-static struct mbuf *
-ether_port_input(struct ifnet *ifp, struct mbuf *m, uint64_t dst,
-    const struct ether_port **epp, struct netstack *ns)
-{
-	const struct ether_port *ep;
-	void *ref;
-
-	smr_read_enter();
-	ep = SMR_PTR_GET(epp);
-	if (ep != NULL)
-		ref = ep->ep_port_take(ep->ep_port);
-	smr_read_leave();
-	if (ep != NULL) {
-		m = (*ep->ep_input)(ifp, m, dst, ep->ep_port, ns);
-		ep->ep_port_rele(ref, ep->ep_port);
-	}
-
-	return (m);
-}
-
 /*
  * Process a received Ethernet packet.
  *
@@ -402,22 +382,16 @@ ether_port_input(struct ifnet *ifp, stru
  * support virtual/pseudo interfaces before actual layer 3 protocol
  * handling.
  */
-void
-ether_input(struct ifnet *ifp, struct mbuf *m, struct netstack *ns)
+
+static struct mbuf *
+ether_l2_input(struct ifnet *ifp, struct mbuf *m, uint64_t dst,
+    const struct ether_port *ep, struct netstack *ns)
 {
-	struct ether_header *eh;
-	void (*input)(struct ifnet *, struct mbuf *, struct netstack *);
-	u_int16_t etype;
 	struct arpcom *ac = (struct arpcom *)ifp;
+	struct ether_header *eh;
+	uint16_t etype;
 	unsigned int sdelim = 0;
-	uint64_t dst, self;
-
-	/* Drop short frames */
-	if (m->m_len < ETHER_HDR_LEN)
-		goto dropanyway;
-
-	eh = mtod(m, struct ether_header *);
-	dst = ether_addr_to_e64((struct ether_addr *)eh->ether_dhost);
+	uint64_t self;
 
 	/*
 	 * First phase:
@@ -426,9 +400,11 @@ ether_input(struct ifnet *ifp, struct mb
 	 * Ethernet ports into a single logical interface, ie, aggr(4)
 	 * and trunk(4).
 	 */
-	m = ether_port_input(ifp, m, dst, &ac->ac_trport, ns);
-	if (m == NULL)
-		return;
+	if (ep != NULL && ep->ep_type == ETHER_PORT_TRUNK) {
+		m = (*ep->ep_input)(ifp, m, dst, ep->ep_port, ns);
+		if (m == NULL)
+			return (NULL);
+	}
 
 	/*
 	 * Second phase: service delimited packet filtering.
@@ -439,6 +415,7 @@ ether_input(struct ifnet *ifp, struct mb
 	 * bridge can have a go at forwarding them.
 	 */
 
+	eh = mtod(m, struct ether_header *);
 	etype = ntohs(eh->ether_type);
 
 	if (ISSET(m->m_flags, M_VLANTAG) ||
@@ -446,10 +423,9 @@ ether_input(struct ifnet *ifp, struct mb
 #if NVLAN > 0
 		m = vlan_input(ifp, m, &sdelim, ns);
 		if (m == NULL)
-			return;
-#else
-		sdelim = 1;
+			return (NULL);
 #endif
+		sdelim = 1;
 	}
 
 	/*
@@ -461,9 +437,11 @@ ether_input(struct ifnet *ifp, struct mb
 	 * may return it here to ether_input() to support local
 	 * delivery to this port.
 	 */
-	m = ether_port_input(ifp, m, dst, &ac->ac_brport, ns);
-	if (m == NULL)
-		return;
+	if (ep != NULL && ep->ep_type == ETHER_PORT_BRIDGE) {
+		m = (*ep->ep_input)(ifp, m, dst, ep->ep_port, ns);
+		if (m == NULL)
+			return (NULL);
+	}
 
 	/*
 	 * Fourth phase: drop service delimited packets.
@@ -481,7 +459,6 @@ ether_input(struct ifnet *ifp, struct mb
 	 * Is the packet specifically addressed to this port?
 	 */
 
-	eh = mtod(m, struct ether_header *);
 	self = ether_addr_to_e64((struct ether_addr *)ac->ac_enaddr);
 	if (dst != self) {
 #if NCARP > 0
@@ -492,9 +469,7 @@ ether_input(struct ifnet *ifp, struct mb
 		    !SMR_LIST_EMPTY_LOCKED(&ifp->if_carp)) {
 			m = carp_input(ifp, m, dst, ns);
 			if (m == NULL)
-				return;
-
-			eh = mtod(m, struct ether_header *);
+				return (NULL);
 		}
 #endif
 
@@ -509,7 +484,10 @@ ether_input(struct ifnet *ifp, struct mb
 		 * if it came from us.
 		 */
 		if ((ifp->if_flags & IFF_SIMPLEX) == 0) {
-			uint64_t src = ether_addr_to_e64(
+			uint64_t src;
+
+			eh = mtod(m, struct ether_header *);
+			src = ether_addr_to_e64(
 			    (struct ether_addr *)eh->ether_shost);
 			if (self == src)
 				goto dropanyway;
@@ -525,7 +503,20 @@ ether_input(struct ifnet *ifp, struct mb
 	 * At this point it is known that the packet is destined
 	 * for layer 3 protocol handling on the local port.
 	 */
-	etype = ntohs(eh->ether_type);
+	return (m);
+
+dropanyway:
+	m_freem(m);
+	return (NULL);
+}
+
+static void
+ether_l3_input(struct ifnet *ifp, struct mbuf *m, uint64_t dst,
+    struct netstack *ns)
+{
+	struct ether_header *eh = mtod(m, struct ether_header *);
+	uint16_t etype = ntohs(eh->ether_type);
+	void (*input)(struct ifnet *, struct mbuf *, struct netstack *);
 
 	switch (etype) {
 	case ETHERTYPE_IP:
@@ -604,56 +595,113 @@ dropanyway:
 	return;
 }
 
-int
-ether_brport_isset(struct ifnet *ifp)
+void
+ether_input(struct ifnet *ifp, struct mbuf *m, struct netstack *ns)
 {
 	struct arpcom *ac = (struct arpcom *)ifp;
+	struct ether_header *eh;
+	uint64_t dst;
+	const struct ether_port *ep;
+	void *ref;
+
+	/* Drop short frames */
+	if (m->m_len < ETHER_HDR_LEN) {
+		m_freem(m);
+		return;
+	}
+
+	eh = mtod(m, struct ether_header *);
+	dst = ether_addr_to_e64((struct ether_addr *)eh->ether_dhost);
+
+	smr_read_enter();
+	ep = SMR_PTR_GET(&ac->ac_port);
+	if (ep != NULL)
+		ref = ep->ep_port_take(ep->ep_port);
+	smr_read_leave();
+
+	m = ether_l2_input(ifp, m, dst, ep, ns);
+
+	if (ep != NULL)
+		ep->ep_port_rele(ref, ep->ep_port);
+
+	if (m == NULL)
+		return;
+
+	ether_l3_input(ifp, m, dst, ns);
+}
+
+int
+ether_port_isset(struct ifnet *ifp)
+{
+	struct arpcom *ac;
+
+	KASSERT(ifp->if_type == IFT_ETHER);
+	ac = (struct arpcom *)ifp;
 
 	KERNEL_ASSERT_LOCKED();
-	if (SMR_PTR_GET_LOCKED(&ac->ac_brport) != NULL)
+	if (SMR_PTR_GET_LOCKED(&ac->ac_port) != NULL)
 		return (EBUSY);
 
 	return (0);
 }
 
 void
-ether_brport_set(struct ifnet *ifp, const struct ether_port *ep)
+ether_port_set(struct ifnet *ifp, const struct ether_port *ep)
 {
-	struct arpcom *ac = (struct arpcom *)ifp;
+	struct arpcom *ac;
+
+	KASSERT(ifp->if_type == IFT_ETHER);
+	ac = (struct arpcom *)ifp;
+
+#ifdef DIAGNOSTIC
+	switch (ep->ep_type) {
+	case ETHER_PORT_TRUNK:
+	case ETHER_PORT_BRIDGE:
+		break;
+
+	case ETHER_PORT_UNSET:
+		panic("ether_port %p ep_type is ETHER_PORT_UNSET", ep);
+	default:
+		panic("ether_port %p ep_type is uknown %u", ep, ep->ep_type);
+	}
+#endif
 
 	KERNEL_ASSERT_LOCKED();
-	KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_brport) == NULL,
-	    "%s setting an already set brport", ifp->if_xname);
+	KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_port) == NULL,
+	    "%s setting an already set port", ifp->if_xname);
 
-	SMR_PTR_SET_LOCKED(&ac->ac_brport, ep);
+	SMR_PTR_SET_LOCKED(&ac->ac_port, ep);
 }
 
 void
-ether_brport_clr(struct ifnet *ifp)
+ether_port_clr(struct ifnet *ifp)
 {
-	struct arpcom *ac = (struct arpcom *)ifp;
+	struct arpcom *ac;
+
+	KASSERT(ifp->if_type == IFT_ETHER);
+	ac = (struct arpcom *)ifp;
 
 	KERNEL_ASSERT_LOCKED();
-	KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_brport) != NULL,
-	    "%s clearing an already clear brport", ifp->if_xname);
+	KASSERTMSG(SMR_PTR_GET_LOCKED(&ac->ac_port) != NULL,
+	    "%s clearing an already clear port", ifp->if_xname);
 
-	SMR_PTR_SET_LOCKED(&ac->ac_brport, NULL);
+	SMR_PTR_SET_LOCKED(&ac->ac_port, NULL);
 }
 
 const struct ether_port *
-ether_brport_get(struct ifnet *ifp)
+ether_port_get(struct ifnet *ifp)
 {
 	struct arpcom *ac = (struct arpcom *)ifp;
 	SMR_ASSERT_CRITICAL();
-	return (SMR_PTR_GET(&ac->ac_brport));
+	return (SMR_PTR_GET(&ac->ac_port));
 }
 
 const struct ether_port *
-ether_brport_get_locked(struct ifnet *ifp)
+ether_port_get_locked(struct ifnet *ifp)
 {
 	struct arpcom *ac = (struct arpcom *)ifp;
 	KERNEL_ASSERT_LOCKED();
-	return (SMR_PTR_GET_LOCKED(&ac->ac_brport));
+	return (SMR_PTR_GET_LOCKED(&ac->ac_port));
 }
 
 /*
Index: net/if_tpmr.c
===================================================================
RCS file: /cvs/src/sys/net/if_tpmr.c,v
diff -u -p -r1.46 if_tpmr.c
--- net/if_tpmr.c	12 Nov 2026 13:59:44 -0000	1.46
+++ net/if_tpmr.c	15 Nov 2026 09:34:07 -0000
@@ -515,7 +515,7 @@ tpmr_add_port(struct tpmr_softc *sc, con
 		goto put;
 	}
 
-	error = ether_brport_isset(ifp0);
+	error = ether_port_isset(ifp0);
 	if (error != 0)
 		goto put;
 
@@ -548,7 +548,7 @@ tpmr_add_port(struct tpmr_softc *sc, con
 		goto free;
 
 	/* this might have changed if we slept for malloc or ifpromisc */
-	error = ether_brport_isset(ifp0);
+	error = ether_port_isset(ifp0);
 	if (error != 0)
 		goto unpromisc;
 
@@ -562,6 +562,7 @@ tpmr_add_port(struct tpmr_softc *sc, con
 	p->p_brport.ep_port_take = tpmr_cpu_take;
 	p->p_brport.ep_port_rele = tpmr_cpu_rele;
 	p->p_brport.ep_port = p;
+	p->p_brport.ep_type = ETHER_PORT_BRIDGE;
 
 	/* commit */
 	DPRINTF(sc, "%s %s trunkport: creating port\n",
@@ -577,7 +578,7 @@ tpmr_add_port(struct tpmr_softc *sc, con
 	p->p_slot = i;
 
 	/* give the ref to the SMR pointers */
-	ether_brport_set(ifp0, &p->p_brport);
+	ether_port_set(ifp0, &p->p_brport);
 	ifp0->if_ioctl = tpmr_p_ioctl;
 	ifp0->if_output = tpmr_p_output;
 
@@ -675,12 +676,12 @@ done:
 static int
 tpmr_p_ioctl(struct ifnet *ifp0, u_long cmd, caddr_t data)
 {
-	const struct ether_port *ep = ether_brport_get_locked(ifp0);
+	const struct ether_port *ep = ether_port_get_locked(ifp0);
 	struct tpmr_port *p;
 	int error = 0;
 
 	KASSERTMSG(ep != NULL,
-	    "%s: %s called without an ether_brport set",
+	    "%s: %s called without an ether_port set",
 	    ifp0->if_xname, __func__);
 	KASSERTMSG(ep->ep_input == tpmr_input,
 	    "%s: %s called, but ep_input seems wrong (%p != tpmr_input())",
@@ -716,7 +717,7 @@ tpmr_p_output(struct ifnet *ifp0, struct
 	}
 
 	smr_read_enter();
-	ep = ether_brport_get(ifp0);
+	ep = ether_port_get(ifp0);
 	if (ep != NULL && ep->ep_input == tpmr_input) {
 		struct tpmr_port *p = ep->ep_port;
 		p_output = p->p_output; /* code doesn't go away */
@@ -783,7 +784,7 @@ tpmr_p_dtor(struct tpmr_softc *sc, struc
 	ifp0->if_ioctl = p->p_ioctl;
 	ifp0->if_output = p->p_output;
 
-	ether_brport_clr(ifp0);
+	ether_port_clr(ifp0);
 
 	sc->sc_nports--;
 	SMR_PTR_SET_LOCKED(&sc->sc_ports[p->p_slot], NULL);
Index: net/if_trunk.c
===================================================================
RCS file: /cvs/src/sys/net/if_trunk.c,v
diff -u -p -r1.166 if_trunk.c
--- net/if_trunk.c	15 Nov 2026 05:49:52 -0000	1.166
+++ net/if_trunk.c	15 Nov 2026 09:34:07 -0000
@@ -262,7 +262,6 @@ trunk_port_create(struct trunk_softc *tr
 {
 	struct trunk_softc *tr_ptr;
 	struct trunk_port *tp;
-	struct arpcom *ac0;
 	int error = 0;
 
 	/* Limit the maximal number of trunk ports */
@@ -280,9 +279,9 @@ trunk_port_create(struct trunk_softc *tr
 	if (ifp->if_type != IFT_ETHER)
 		return (EPROTONOSUPPORT);
 
-	ac0 = (struct arpcom *)ifp;
-	if (SMR_PTR_GET_LOCKED(&ac0->ac_trport) != NULL)
-		return (EBUSY);
+	error = ether_port_isset(ifp);
+	if (error != 0)
+		return (error);
 
 	/* Take MTU from the first member port */
 	if (SLIST_EMPTY(&tr->tr_ports)) {
@@ -297,17 +296,22 @@ trunk_port_create(struct trunk_softc *tr
 		return (EINVAL);
 	}
 
-	if ((error = ifpromisc(ifp, 1)) != 0)
-		return (error);
-
 	if ((tp = malloc(sizeof *tp, M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
 		return (ENOMEM);
 
+	if ((error = ifpromisc(ifp, 1)) != 0)
+		goto free;
+
+	error = ether_port_isset(ifp); /* check again if the the above slept */
+	if (error != 0)
+		goto unpromisc;
+
 	refcnt_init(&tp->tp_refs);
 	tp->tp_ether_port.ep_input = trunk_input;
 	tp->tp_ether_port.ep_port_take = trunk_port_take;
 	tp->tp_ether_port.ep_port_rele = trunk_port_rele;
 	tp->tp_ether_port.ep_port = tp;
+	tp->tp_ether_port.ep_type = ETHER_PORT_TRUNK;
 
 	/* Check if port is a stacked trunk */
 	SLIST_FOREACH(tr_ptr, &trunk_list, tr_entries) {
@@ -360,7 +364,7 @@ trunk_port_create(struct trunk_softc *tr
 
 	/* Assign input handler of the physical interface. */
 	NET_ASSERT_LOCKED();
-	SMR_PTR_SET_LOCKED(&ac0->ac_trport, &tp->tp_ether_port);
+	ether_port_set(ifp, &tp->tp_ether_port);
 
 	tp->tp_ioctl = ifp->if_ioctl;
 	ifp->if_ioctl = trunk_port_ioctl;
@@ -372,6 +376,13 @@ trunk_port_create(struct trunk_softc *tr
 	trunk_link_active(tr, tp);
 
 	return (error);
+
+unpromisc:
+	ifpromisc(ifp, 0);
+free:
+	free(tp, M_DEVBUF, sizeof(*tp));
+
+	return (error);
 }
 
 int
@@ -397,12 +408,10 @@ trunk_port_destroy(struct trunk_port *tp
 	struct trunk_softc *tr = (struct trunk_softc *)tp->tp_trunk;
 	struct trunk_port *tp_ptr;
 	struct ifnet *ifp = tp->tp_if;
-	struct arpcom *ac0 = (struct arpcom *)ifp;
 
 	/* Restore previous input handler. */
 	NET_ASSERT_LOCKED();
-	KASSERT(SMR_PTR_GET_LOCKED(&ac0->ac_trport) == &tp->tp_ether_port);
-	SMR_PTR_SET_LOCKED(&ac0->ac_trport, NULL);
+	ether_port_clr(ifp);
 	smr_barrier();
 	refcnt_finalize(&tp->tp_refs, "trdtor");
 
@@ -458,8 +467,7 @@ trunk_port_destroy(struct trunk_port *tp
 int
 trunk_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 {
-	struct arpcom *ac0 = (struct arpcom *)ifp;
-	const struct ether_port *ep = SMR_PTR_GET_LOCKED(&ac0->ac_trport);
+	const struct ether_port *ep = ether_port_get_locked(ifp);
 	struct trunk_reqport *rp = (struct trunk_reqport *)data;
 	struct trunk_softc *tr;
 	struct trunk_port *tp = NULL;
@@ -646,10 +654,8 @@ trunk_ioctl(struct ifnet *ifp, u_long cm
 		 * granting us an exclusive access to it.
 		 */
 		NET_ASSERT_LOCKED();
-		SLIST_FOREACH(tp, &tr->tr_ports, tp_entries) {
-			struct arpcom *ac = (struct arpcom *)tp->tp_if;
-			SMR_PTR_SET_LOCKED(&ac->ac_trport, NULL);
-		}
+		SLIST_FOREACH(tp, &tr->tr_ports, tp_entries)
+			ether_port_clr(tp->tp_if);
 		SLIST_FOREACH(tp, &tr->tr_ports, tp_entries)
 			refcnt_finalize(&tp->tp_refs, "trunkset");
 		smr_barrier();
@@ -667,10 +673,8 @@ trunk_ioctl(struct ifnet *ifp, u_long cm
 				if (tr->tr_proto != TRUNK_PROTO_NONE)
 					error = trunk_protos[i].ti_attach(tr);
 				SLIST_FOREACH(tp, &tr->tr_ports, tp_entries) {
-					struct arpcom *ac =
-					    (struct arpcom *)tp->tp_if;
 					refcnt_init(&tp->tp_refs);
-					SMR_PTR_SET_LOCKED(&ac->ac_trport,
+					ether_port_set(tp->tp_if,
 					    &tp->tp_ether_port);
 				}
 				/* Update trunk capabilities */
Index: net/if_veb.c
===================================================================
RCS file: /cvs/src/sys/net/if_veb.c,v
diff -u -p -r1.78 if_veb.c
--- net/if_veb.c	15 Nov 2026 09:18:33 -0000	1.78
+++ net/if_veb.c	15 Nov 2026 09:34:07 -0000
@@ -1,4 +1,4 @@
-/*	$OpenBSD: if_veb.c,v 1.78 2026/11/15 09:18:33 dlg Exp $ */
+/*	$OpenBSD: if_veb.c,v 1.77 2026/11/15 06:40:06 dlg Exp $ */
 
 /*
  * Copyright (c) 2021 David Gwynne <dlg@openbsd.org>
@@ -1767,7 +1767,7 @@ veb_add_port(struct veb_softc *sc, const
 
 	isvport = if_is_vport(ifp0);
 
-	error = ether_brport_isset(ifp0);
+	error = ether_port_isset(ifp0);
 	if (error != 0)
 		goto put;
 
@@ -1806,6 +1806,7 @@ veb_add_port(struct veb_softc *sc, const
 	p->p_brport.ep_port = p;
 	p->p_brport.ep_port_take = veb_ep_brport_take;
 	p->p_brport.ep_port_rele = veb_ep_brport_rele;
+	p->p_brport.ep_type = ETHER_PORT_BRIDGE;
 
 	if (span) {
 		ports_ptr = &sc->sc_spans;
@@ -1833,7 +1834,7 @@ veb_add_port(struct veb_softc *sc, const
 	nm = veb_ports_insert(om, p);
 
 	/* this might have changed if we slept for malloc or ifpromisc */
-	error = ether_brport_isset(ifp0);
+	error = ether_port_isset(ifp0);
 	if (error != 0)
 		goto unpromisc;
 
@@ -1846,7 +1847,7 @@ veb_add_port(struct veb_softc *sc, const
 	/* commit */
 	SMR_PTR_SET_LOCKED(ports_ptr, nm);
 
-	ether_brport_set(ifp0, &p->p_brport);
+	ether_port_set(ifp0, &p->p_brport);
 	if (!isvport) { /* vport is special */
 		ifp0->if_ioctl = veb_p_ioctl;
 		ifp0->if_output = veb_p_output;
@@ -3121,12 +3122,12 @@ veb_del_vid_addr(struct veb_softc *sc, c
 static int
 veb_p_ioctl(struct ifnet *ifp0, u_long cmd, caddr_t data)
 {
-	const struct ether_port *ep = ether_brport_get_locked(ifp0);
+	const struct ether_port *ep = ether_port_get_locked(ifp0);
 	struct veb_port *p;
 	int error = 0;
 
 	KASSERTMSG(ep != NULL,
-	    "%s: %s called without an ether_brport set",
+	    "%s: %s called without an ether_port set",
 	    ifp0->if_xname, __func__);
 	KASSERTMSG((ep->ep_input == veb_port_input) ||
 	    (ep->ep_input == veb_span_input),
@@ -3163,7 +3164,7 @@ veb_p_output(struct ifnet *ifp0, struct 
 	}
 
 	smr_read_enter();
-	ep = ether_brport_get(ifp0);
+	ep = ether_port_get(ifp0);
 	if (ep != NULL && ep->ep_input == veb_port_input) {
 		struct veb_port *p = ep->ep_port;
 		p_output = p->p_output; /* code doesn't go away */
@@ -3179,7 +3180,7 @@ veb_p_output(struct ifnet *ifp0, struct 
 }
 
 /*
- * there must be an smr_barrier after ether_brport_clr() and before
+ * there must be an smr_barrier after ether_port_clr() and before
  * veb_port is freed in veb_p_fini()
  */
 
@@ -3192,7 +3193,7 @@ veb_p_unlink(struct veb_softc *sc, struc
 	ifp0->if_ioctl = p->p_ioctl;
 	ifp0->if_output = p->p_output;
 
-	ether_brport_clr(ifp0); /* needs an smr_barrier */
+	ether_port_clr(ifp0); /* needs an smr_barrier */
 
 	if_detachhook_del(ifp0, &p->p_dtask);
 	if_linkstatehook_del(ifp0, &p->p_ltask);
@@ -3707,7 +3708,7 @@ vport_enqueue(struct ifnet *ifp, struct 
 	ac = (struct arpcom *)ifp;
 
 	smr_read_enter();
-	ep = SMR_PTR_GET(&ac->ac_brport);
+	ep = SMR_PTR_GET(&ac->ac_port);
 	if (ep != NULL)
 		ref = ep->ep_port_take(ep->ep_port);
 	smr_read_leave();
Index: net/if_vlan.c
===================================================================
RCS file: /cvs/src/sys/net/if_vlan.c,v
diff -u -p -r1.224 if_vlan.c
--- net/if_vlan.c	18 Dec 2025 02:14:09 -0000	1.224
+++ net/if_vlan.c	15 Nov 2026 09:34:07 -0000
@@ -948,7 +948,7 @@ vlan_set_parent(struct vlan_softc *sc, c
 	if (error != 0)
 		goto put;
 
-	if (ether_brport_isset(ifp))
+	if (ether_port_isset(ifp))
 		ifsetlro(ifp0, 0);
 
 	/* commit */
Index: netinet/if_ether.h
===================================================================
RCS file: /cvs/src/sys/netinet/if_ether.h,v
diff -u -p -r1.99 if_ether.h
--- netinet/if_ether.h	2 Dec 2025 03:24:19 -0000	1.99
+++ netinet/if_ether.h	15 Nov 2026 09:34:07 -0000
@@ -220,12 +220,20 @@ do {									\
 
 #include <net/if_var.h>	/* for "struct ifnet" */
 
+enum ether_port_type {
+	ETHER_PORT_UNSET,
+	ETHER_PORT_TRUNK,
+	ETHER_PORT_BRIDGE,
+};
+
 struct ether_port {
 	struct mbuf	*(*ep_input)(struct ifnet *, struct mbuf *,
 			   uint64_t, void *, struct netstack *);
 	void		*(*ep_port_take)(void *);
 	void		 (*ep_port_rele)(void *, void *);
 	void		  *ep_port;
+	enum ether_port_type
+			   ep_type;
 };
 
 /*
@@ -241,8 +249,7 @@ struct	arpcom {
 	int	 ac_multicnt;			/* length of ac_multiaddrs */
 	int	 ac_multirangecnt;		/* number of mcast ranges */
 
-	const struct ether_port *ac_trport;
-	const struct ether_port *ac_brport;
+	const struct ether_port *ac_port;
 };
 
 extern int arpt_keep;				/* arp resolved cache expire */
@@ -288,13 +295,13 @@ int	ether_output(struct ifnet *, struct 
 void	ether_rtrequest(struct ifnet *, int, struct rtentry *);
 char	*ether_sprintf(u_char *);
 
-int	ether_brport_isset(struct ifnet *);
-void	ether_brport_set(struct ifnet *, const struct ether_port *);
-void	ether_brport_clr(struct ifnet *);
+int	ether_port_isset(struct ifnet *);
+void	ether_port_set(struct ifnet *, const struct ether_port *);
+void	ether_port_clr(struct ifnet *);
 const struct ether_port *
-	ether_brport_get(struct ifnet *);
+	ether_port_get(struct ifnet *);
 const struct ether_port *
-	ether_brport_get_locked(struct ifnet *);
+	ether_port_get_locked(struct ifnet *);
 
 uint64_t	ether_addr_to_e64(const struct ether_addr *);
 void		ether_e64_to_addr(struct ether_addr *, uint64_t);