From nobody Thu Apr 20 10:24:01 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Q2DKG3rCKz46dBt; Thu, 20 Apr 2023 10:24:02 +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 4Q2DKG1dwVz3xLD; Thu, 20 Apr 2023 10:24:02 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1681986242; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=eRYUj7w+XfU+1GOQnd3MPfGIF9ospCLnEz18keoWfxI=; b=WXS7Uui2Mt6HeVVBXSgumtawwomSjCUSpxaDIv1NonLOnvAL/wO/GnSBcsffk7nddnC/Na 81V7Ut7qTiF9WMvBBn03WS+vcqEp258qe8fWRms1Z+X3vPXeJLvEi6dUuWsApduP8sO/JB Xry4KFRDPFFnA/7TUKyOVrYqlfQ++eA2bjMhStZHtVS11lc81NN4Jjui7aTEqhoaxR16f3 tebLkzfRn5OphgbxPCxFmDh2G9pUDfp5RhNy6SBZAf2V8MHKkTAs0Q44/pL7+rlUvpSn9I PYgAfc/7PiB3edA+gNHF4DTf16WSKij57KldnQFzCalAK6w/XCX2LIadA5Qc5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1681986242; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=eRYUj7w+XfU+1GOQnd3MPfGIF9ospCLnEz18keoWfxI=; b=Pbo8G5y8tJQL80G51y4rkGzM4/L2l3mH2RQ+NO/WDHOq5Piyy3xaSLAtiwMkXxm/YokUq9 uxHKq/M1hri/Fdt9Lw4NabqWhtviZCli095q/k3PXa7IxCKKnvodXZCF0MCcqDmyg+I5Zn udMADDFKmWwVjZmgxwig8Xe6G+Q6cFhL3H+adgo0KijV9mmWBOuHQiwt+CjYzXyqMbO87z Q2GdrETQfhjWBvwBPdPJ0KX2AlTqBl1flkqDkL2q1Ub0n3V+RgYX+YCPbrsqOJsTvOrEIv adeGpZ0uc92r3l9m2inth7wyzaPakiR4MxOQs4EujLSYsyfvYiUY5Ct7GBVd7w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1681986242; a=rsa-sha256; cv=none; b=TusTlINREyZUMvk1LrJfXaW80WfaSh3mWbT9E+IwticNDLaloqpYXKgUmvm3NsI+2gNDjb 4SGCBRt62JHsSFkXfsIv4GxrsIjKMybv1An0h88vXoKlnFB7SYgekmjzliiR1Mwg95vHgF cxRuqOOaafguoGZl7f5ol+Y+jXn9DOmVChHuSN0sMEwIKwM/i8z05I/oBgkNHd7xdo5PQu jVDsUROstUP2viaAYceIF4uD8PU0cp93KNZr25+GhwmOl/5jR7xIZbfJszBl18npX9vYMX 1x/8/+fqc5VrvhTYXmQMsmLC9ptbOywB5nX7SjYvH4MeSLf40oCEx7pFSmWJNA== 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 4Q2DKG0HVCzYk0; Thu, 20 Apr 2023 10:24:02 +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 33KAO10S068825; Thu, 20 Apr 2023 10:24:01 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 33KAO1jg068824; Thu, 20 Apr 2023 10:24:01 GMT (envelope-from git) Date: Thu, 20 Apr 2023 10:24:01 GMT Message-Id: <202304201024.33KAO1jg068824@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: 56d4550c4d71 - main - ifnet: factor out interface renaming into a separate function. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 56d4550c4d71e61057362b3f7d5e391496efec4e Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=56d4550c4d71e61057362b3f7d5e391496efec4e commit 56d4550c4d71e61057362b3f7d5e391496efec4e Author: Alexander V. Chernikov AuthorDate: 2023-04-19 14:26:20 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-04-20 10:23:37 +0000 ifnet: factor out interface renaming into a separate function. This change is required to support interface renaming via Netlink. No functional changes intended. Reviewed by: zlei Differential Revision: https://reviews.freebsd.org/D39692 MFC after: 2 weeks --- sys/net/if.c | 116 ++++++++++++++++++++++++++++++------------------------- sys/net/if_var.h | 1 + 2 files changed, 64 insertions(+), 53 deletions(-) diff --git a/sys/net/if.c b/sys/net/if.c index 90ed39bb2cff..b53a361c2ba3 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -2476,13 +2476,9 @@ ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) struct ifreq *ifr; int error = 0, do_ifup = 0; int new_flags, temp_flags; - size_t namelen, onamelen; size_t descrlen, nvbuflen; char *descrbuf; char new_name[IFNAMSIZ]; - char old_name[IFNAMSIZ], strbuf[IFNAMSIZ + 8]; - struct ifaddr *ifa; - struct sockaddr_dl *sdl; void *buf; nvlist_t *nvcap; struct siocsifcapnv_driver_data drv_ioctl_data; @@ -2749,55 +2745,7 @@ ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) NULL); if (error != 0) return (error); - if (new_name[0] == '\0') - return (EINVAL); - if (strcmp(new_name, ifp->if_xname) == 0) - break; - if (ifunit(new_name) != NULL) - return (EEXIST); - - /* - * XXX: Locking. Nothing else seems to lock if_flags, - * and there are numerous other races with the - * ifunit() checks not being atomic with namespace - * changes (renames, vmoves, if_attach, etc). - */ - ifp->if_flags |= IFF_RENAMING; - - EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); - - if_printf(ifp, "changing name to '%s'\n", new_name); - - IF_ADDR_WLOCK(ifp); - strlcpy(old_name, ifp->if_xname, sizeof(old_name)); - strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); - ifa = ifp->if_addr; - sdl = (struct sockaddr_dl *)ifa->ifa_addr; - namelen = strlen(new_name); - onamelen = sdl->sdl_nlen; - /* - * Move the address if needed. This is safe because we - * allocate space for a name of length IFNAMSIZ when we - * create this in if_attach(). - */ - if (namelen != onamelen) { - bcopy(sdl->sdl_data + onamelen, - sdl->sdl_data + namelen, sdl->sdl_alen); - } - bcopy(new_name, sdl->sdl_data, namelen); - sdl->sdl_nlen = namelen; - sdl = (struct sockaddr_dl *)ifa->ifa_netmask; - bzero(sdl->sdl_data, onamelen); - while (namelen != 0) - sdl->sdl_data[--namelen] = 0xff; - IF_ADDR_WUNLOCK(ifp); - - EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); - - ifp->if_flags &= ~IFF_RENAMING; - - snprintf(strbuf, sizeof(strbuf), "name=%s", new_name); - devctl_notify("IFNET", old_name, "RENAME", strbuf); + error = if_rename(ifp, new_name); break; #ifdef VIMAGE @@ -3207,6 +3155,68 @@ out_noref: return (error); } +int +if_rename(struct ifnet *ifp, char *new_name) +{ + struct ifaddr *ifa; + struct sockaddr_dl *sdl; + size_t namelen, onamelen; + char old_name[IFNAMSIZ]; + char strbuf[IFNAMSIZ + 8]; + + if (new_name[0] == '\0') + return (EINVAL); + if (strcmp(new_name, ifp->if_xname) == 0) + return (0); + if (ifunit(new_name) != NULL) + return (EEXIST); + + /* + * XXX: Locking. Nothing else seems to lock if_flags, + * and there are numerous other races with the + * ifunit() checks not being atomic with namespace + * changes (renames, vmoves, if_attach, etc). + */ + ifp->if_flags |= IFF_RENAMING; + + EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); + + if_printf(ifp, "changing name to '%s'\n", new_name); + + IF_ADDR_WLOCK(ifp); + strlcpy(old_name, ifp->if_xname, sizeof(old_name)); + strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); + ifa = ifp->if_addr; + sdl = (struct sockaddr_dl *)ifa->ifa_addr; + namelen = strlen(new_name); + onamelen = sdl->sdl_nlen; + /* + * Move the address if needed. This is safe because we + * allocate space for a name of length IFNAMSIZ when we + * create this in if_attach(). + */ + if (namelen != onamelen) { + bcopy(sdl->sdl_data + onamelen, + sdl->sdl_data + namelen, sdl->sdl_alen); + } + bcopy(new_name, sdl->sdl_data, namelen); + sdl->sdl_nlen = namelen; + sdl = (struct sockaddr_dl *)ifa->ifa_netmask; + bzero(sdl->sdl_data, onamelen); + while (namelen != 0) + sdl->sdl_data[--namelen] = 0xff; + IF_ADDR_WUNLOCK(ifp); + + EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); + + ifp->if_flags &= ~IFF_RENAMING; + + snprintf(strbuf, sizeof(strbuf), "name=%s", new_name); + devctl_notify("IFNET", old_name, "RENAME", strbuf); + + return (0); +} + /* * The code common to handling reference counted flags, * e.g., in ifpromisc() and if_allmulti(). diff --git a/sys/net/if_var.h b/sys/net/if_var.h index 5f11998ecab8..e21bf44516cc 100644 --- a/sys/net/if_var.h +++ b/sys/net/if_var.h @@ -590,6 +590,7 @@ const char *if_getdname(const if_t ifp); void if_setdname(if_t ifp, const char *name); const char *if_name(if_t ifp); int if_setname(if_t ifp, const char *name); +int if_rename(struct ifnet *ifp, char *new_name); void if_setdescr(if_t ifp, char *descrbuf); char *if_allocdescr(size_t sz, int malloc_flag); void if_freedescr(char *descrbuf);