Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 30 Apr 2026 09:53:23 +0000
From:      Zhenlei Huang <zlei@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 477c3ba36731 - stable/15 - ifnet: Move SIOCSIFVNET from ifhwioctl() to ifioctl()
Message-ID:  <69f32693.466f8.39cf56ab@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch stable/15 has been updated by zlei:

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

commit 477c3ba3673152e88ec5c50d8f45db24102bdf07
Author:     Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2026-04-13 04:38:44 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2026-04-30 09:52:00 +0000

    ifnet: Move SIOCSIFVNET from ifhwioctl() to ifioctl()
    
    SIOCSIFVNET is not a hardware ioctl. Move it to where it belongs.
    
    Where here, rewrite the logic of checking whether we are moving the
    interface from and to the same vnet or not, since it is obviously not
    stable to access the interface's vnet, given the current thread may
    race with other threads those running if_vmove().
    
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D55880
    
    (cherry picked from commit 38bd7ef62f318f791e232e217855307a9d75efa0)
---
 sys/net/if.c | 44 +++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 17 deletions(-)

diff --git a/sys/net/if.c b/sys/net/if.c
index 7424da5ccf6d..2da0afbeabc9 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -1300,26 +1300,39 @@ if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
  * Move an ifnet to or from another child prison/vnet, specified by the jail id.
  */
 static int
-if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
+if_vmove_loan(struct thread *td, char *ifname, int jid)
 {
 	struct prison *pr;
-	struct ifnet *difp;
+	struct ifnet *ifp, *difp;
 	bool found;
 
+	MPASS(curthread == td);
+	MPASS(curvnet == TD_TO_VNET(td));
+
+	/*
+	 * We check the existence of the interface, and will later try to
+	 * unlink it from the "active" list, so it is sufficient to only
+	 * hold a weak reference to it.
+	 * Be aware that it is unsafe to access any member of it, until it
+	 * is proven to be safe to ( say it was on the "active" list ).
+	 */
+	ifp = ifunit(ifname);
+	if (ifp == NULL)
+		return (ENXIO);
+
 	/* Try to find the prison within our visibility. */
 	sx_slock(&allprison_lock);
 	pr = prison_find_child(td->td_ucred->cr_prison, jid);
 	sx_sunlock(&allprison_lock);
 	if (pr == NULL)
 		return (ENXIO);
-	prison_hold_locked(pr);
-	mtx_unlock(&pr->pr_mtx);
-
-	/* Do not try to move the iface from and to the same prison. */
-	if (pr->pr_vnet == ifp->if_vnet) {
-		prison_free(pr);
+	/* Do not try to move the iface from and to the same vnet. */
+	if (pr->pr_vnet == TD_TO_VNET(td)) {
+		mtx_unlock(&pr->pr_mtx);
 		return (EEXIST);
 	}
+	prison_hold_locked(pr);
+	mtx_unlock(&pr->pr_mtx);
 
 	/* Make sure the named iface does not exists in the dst. prison/vnet. */
 	/* XXX Lock interfaces to avoid races. */
@@ -2693,15 +2706,6 @@ ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
 		error = if_rename(ifp, new_name);
 		break;
 
-#ifdef VIMAGE
-	case SIOCSIFVNET:
-		error = priv_check(td, PRIV_NET_SETIFVNET);
-		if (error)
-			return (error);
-		error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
-		break;
-#endif
-
 	case SIOCSIFMETRIC:
 		error = priv_check(td, PRIV_NET_SETIFMETRIC);
 		if (error)
@@ -2989,6 +2993,12 @@ ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
 	ifr = (struct ifreq *)data;
 	switch (cmd) {
 #ifdef VIMAGE
+	case SIOCSIFVNET:
+		error = priv_check(td, PRIV_NET_SETIFVNET);
+		if (error == 0)
+			error = if_vmove_loan(td, ifr->ifr_name, ifr->ifr_jid);
+		goto out_noref;
+
 	case SIOCSIFRVNET:
 		error = priv_check(td, PRIV_NET_SETIFVNET);
 		if (error == 0)


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?69f32693.466f8.39cf56ab>