From owner-dev-commits-src-main@freebsd.org Wed May 5 20:59:36 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id AD86D62351A; Wed, 5 May 2021 20:59:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Fb8Gc4WzXz4jfW; Wed, 5 May 2021 20:59:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8E67B3AF8; Wed, 5 May 2021 20:59:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 145Kxanf028723; Wed, 5 May 2021 20:59:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 145KxadA028722; Wed, 5 May 2021 20:59:36 GMT (envelope-from git) Date: Wed, 5 May 2021 20:59:36 GMT Message-Id: <202105052059.145KxadA028722@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: d17e0940f79f - main - Rework compat shims in ifioctl(). MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d17e0940f79fd96a2bf27413c1fe682dd8ee5bac Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 May 2021 20:59:36 -0000 The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=d17e0940f79fd96a2bf27413c1fe682dd8ee5bac commit d17e0940f79fd96a2bf27413c1fe682dd8ee5bac Author: John Baldwin AuthorDate: 2021-05-05 20:58:23 +0000 Commit: John Baldwin CommitDate: 2021-05-05 20:58:23 +0000 Rework compat shims in ifioctl(). Centralize logic for handling compat ioctls into two blocks of code at the start and end of the ioctl routine. This avoids the conversion logic being spread out both in multiple blocks in ifioctl as well as various helper functions. Reviewed by: brooks, kib Obtained from: CheriBSD Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D29891 --- sys/net/if.c | 118 +++++++++++++++++++++++++---------------------------------- 1 file changed, 49 insertions(+), 69 deletions(-) diff --git a/sys/net/if.c b/sys/net/if.c index 1e410142747f..a3f5a8dc831a 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -2917,38 +2917,6 @@ struct ifconf32 { }; #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32) #endif - -#ifdef COMPAT_FREEBSD32 -static void -ifmr_init(struct ifmediareq *ifmr, caddr_t data) -{ - struct ifmediareq32 *ifmr32; - - ifmr32 = (struct ifmediareq32 *)data; - memcpy(ifmr->ifm_name, ifmr32->ifm_name, - sizeof(ifmr->ifm_name)); - ifmr->ifm_current = ifmr32->ifm_current; - ifmr->ifm_mask = ifmr32->ifm_mask; - ifmr->ifm_status = ifmr32->ifm_status; - ifmr->ifm_active = ifmr32->ifm_active; - ifmr->ifm_count = ifmr32->ifm_count; - ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist; -} - -static void -ifmr_update(const struct ifmediareq *ifmr, caddr_t data) -{ - struct ifmediareq32 *ifmr32; - - ifmr32 = (struct ifmediareq32 *)data; - ifmr32->ifm_current = ifmr->ifm_current; - ifmr32->ifm_mask = ifmr->ifm_mask; - ifmr32->ifm_status = ifmr->ifm_status; - ifmr32->ifm_active = ifmr->ifm_active; - ifmr32->ifm_count = ifmr->ifm_count; -} -#endif - /* * Interface ioctls. */ @@ -2956,9 +2924,14 @@ int ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) { #ifdef COMPAT_FREEBSD32 - caddr_t saved_data = NULL; - struct ifmediareq ifmr; - struct ifmediareq *ifmrp = NULL; + union { + struct ifconf ifc; + struct ifmediareq ifmr; + } thunk; + caddr_t saved_data; + u_long saved_cmd; + struct ifconf32 *ifc32; + struct ifmediareq32 *ifmr32; #endif struct ifnet *ifp; struct ifreq *ifr; @@ -2978,41 +2951,40 @@ ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) } #endif - switch (cmd) { - case SIOCGIFCONF: - error = ifconf(cmd, data); - goto out_noref; - -#ifdef COMPAT_FREEBSD32 - case SIOCGIFCONF32: - { - struct ifconf32 *ifc32; - struct ifconf ifc; - - ifc32 = (struct ifconf32 *)data; - ifc.ifc_len = ifc32->ifc_len; - ifc.ifc_buf = PTRIN(ifc32->ifc_buf); - - error = ifconf(SIOCGIFCONF, (void *)&ifc); - if (error == 0) - ifc32->ifc_len = ifc.ifc_len; - goto out_noref; - } -#endif - } - #ifdef COMPAT_FREEBSD32 + saved_cmd = cmd; + saved_data = data; switch (cmd) { + case SIOCGIFCONF32: + ifc32 = (struct ifconf32 *)data; + thunk.ifc.ifc_len = ifc32->ifc_len; + thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf); + data = (caddr_t)&thunk.ifc; + cmd = SIOCGIFCONF; + break; case SIOCGIFMEDIA32: case SIOCGIFXMEDIA32: - ifmrp = &ifmr; - ifmr_init(ifmrp, data); + ifmr32 = (struct ifmediareq32 *)data; + memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name, + sizeof(thunk.ifmr.ifm_name)); + thunk.ifmr.ifm_current = ifmr32->ifm_current; + thunk.ifmr.ifm_mask = ifmr32->ifm_mask; + thunk.ifmr.ifm_status = ifmr32->ifm_status; + thunk.ifmr.ifm_active = ifmr32->ifm_active; + thunk.ifmr.ifm_count = ifmr32->ifm_count; + thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist); + data = (caddr_t)&thunk.ifmr; cmd = _IOC_NEWTYPE(cmd, struct ifmediareq); - saved_data = data; - data = (caddr_t)ifmrp; + break; } #endif + switch (cmd) { + case SIOCGIFCONF: + error = ifconf(cmd, data); + goto out_noref; + } + ifr = (struct ifreq *)data; switch (cmd) { #ifdef VIMAGE @@ -3102,16 +3074,24 @@ ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) out_ref: if_rele(ifp); out_noref: + CURVNET_RESTORE(); #ifdef COMPAT_FREEBSD32 - if (ifmrp != NULL) { - KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA), - ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx", - cmd)); - data = saved_data; - ifmr_update(ifmrp, data); + if (error != 0) + return (error); + switch (saved_cmd) { + case SIOCGIFCONF32: + ifc32->ifc_len = thunk.ifc.ifc_len; + break; + case SIOCGIFMEDIA32: + case SIOCGIFXMEDIA32: + ifmr32->ifm_current = thunk.ifmr.ifm_current; + ifmr32->ifm_mask = thunk.ifmr.ifm_mask; + ifmr32->ifm_status = thunk.ifmr.ifm_status; + ifmr32->ifm_active = thunk.ifmr.ifm_active; + ifmr32->ifm_count = thunk.ifmr.ifm_count; + break; } #endif - CURVNET_RESTORE(); return (error); }