From nobody Tue Sep 27 13:23:42 2022 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 4McL1C0GBPz4d057; Tue, 27 Sep 2022 13:23:43 +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 4McL1B6w3Bz3TwT; Tue, 27 Sep 2022 13:23:42 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664285023; 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=YeHPCmg53WJv9V+ZQKEW5ivkrOpFUNVhB0njX2D0Aaw=; b=T8eIBeoFFnzc1nrPSx42fgXkzu41Pv+Le19AuztA6Waut5LEQxfJk052xQGc0F84CV+fj4 kxbyyRl6EcC3bi2+npPgBShg40D2fnsCslnfyU9VLN2M28ExO196QybB91xpDzazIA24wt IQl0vpgm5EkN7j6wQ/MB/SOjTPsK0V4GURtbRcr5ezWSz1VkyMJ50PwsJ/2aYFgc1mKJkb x/2rCnxIlOHsIMfERWV+d4P9D/xwmy8uWrOBhWyGSeh7AmGg5suJ4CSXBpRcLIfM4AgZ9W QXv3Hn5aDmOrlORze+IY9170gvnYuiIsLsg9azoUPQVS6MtSoP+X58Fwm6DQtQ== 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 4McL1B5yrgz17bl; Tue, 27 Sep 2022 13:23:42 +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 28RDNgD0078509; Tue, 27 Sep 2022 13:23:42 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 28RDNgVJ078508; Tue, 27 Sep 2022 13:23:42 GMT (envelope-from git) Date: Tue, 27 Sep 2022 13:23:42 GMT Message-Id: <202209271323.28RDNgVJ078508@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: e437991fc936 - main - netinet6: factor interface addition code to the dedicated 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: e437991fc9361bce1b7dca999dbe3769321f01da Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1664285023; 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=YeHPCmg53WJv9V+ZQKEW5ivkrOpFUNVhB0njX2D0Aaw=; b=An20ASzSMXehpB3n+29YD7aRGn0w9O8XFzHo512Xu+Wx97PVcGpMHC7e9vTMUIw/NHgepl w+/CIMWR2DnjwD1Z+44ncIkYsuNNUxHncQRS/IgJNykLAUDPeZCcQuu6L0M/KZJpFH5fW9 qkQAq21a2nwDhMm0K4aJAgpkykBBnw28Yhy6JhgkQaAeaFnF6KvnbeBCL1BVZV9gMv1c7T kqVO7EjSbys/LJGgLiqrhpYq7gZgTK/i4lxqwhKTYTivEYwxmy5sLVivJZUa5vC98DxJ/9 /0P2jmsS+fjuRqi3MAeNvOfSGfVF1wiXupRpejPK5D6/9mIfc6msjFl+7Hnzog== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1664285023; a=rsa-sha256; cv=none; b=IZ/SiFKpvPZnXTJh+Hv8IdiEp4MzxhAc1mqCdyABLw3Z3+PV2eXv0Ax0ALNC5pkWXej0A4 PG1jazljaQ7oaUe6RVsMOUfx9DyiUEYHFjb3Oq1MXYqQkkSPVvlR0zv/o/375jRoE/4Ds5 5FnFb9rL4hNvHnYf6fSAElbyeO4NajePcKlOQRb5FKPuo7ZUVfhw+VDvaB8I1mDX8iwBqO ST90h+SlX0743Fcoy3x8jEkIYQLWP0SjtU3GnwUHpJ7vVZqb/NtS2XaUxJuDcutIWuuq49 ejaaiIiv+ih303oFJDQoxYONsesZdrupPHbuqkhhT9eA4MHpc2r3ZGaz+KTQBw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=e437991fc9361bce1b7dca999dbe3769321f01da commit e437991fc9361bce1b7dca999dbe3769321f01da Author: Alexander V. Chernikov AuthorDate: 2022-09-26 13:49:02 +0000 Commit: Alexander V. Chernikov CommitDate: 2022-09-27 13:23:34 +0000 netinet6: factor interface addition code to the dedicated function Summary: Move SIOCAIFADDR_IN6 (current "primary" ioctl to add an IPv6 interface address) handling code to the dedicated in6_addifaddr() function and make it a part of KPI. This allows in-kernel users to add/delete interfaces addresses without relying on ioctl interface. Subscribers: imp, ae, glebius Differential Revision: https://reviews.freebsd.org/D36713 --- sys/netinet6/in6.c | 288 +++++++++++++++++++++++++------------------------ sys/netinet6/in6_var.h | 1 + 2 files changed, 151 insertions(+), 138 deletions(-) diff --git a/sys/netinet6/in6.c b/sys/netinet6/in6.c index ccdf71cfc01c..0a00ea6b8be4 100644 --- a/sys/netinet6/in6.c +++ b/sys/netinet6/in6.c @@ -252,15 +252,15 @@ in6_control(struct socket *so, u_long cmd, void *data, struct in6_ifaddr *ia = NULL; struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; struct sockaddr_in6 *sa6; - int carp_attached = 0; int error; - u_long ocmd = cmd; /* * Compat to make pre-10.x ifconfig(8) operable. */ - if (cmd == OSIOCAIFADDR_IN6) + if (cmd == OSIOCAIFADDR_IN6) { cmd = SIOCAIFADDR_IN6; + ifra->ifra_vhid = 0; + } switch (cmd) { case SIOCGETSGCNT_IN6: @@ -560,142 +560,9 @@ in6_control(struct socket *so, u_long cmd, void *data, break; case SIOCAIFADDR_IN6: - { - struct nd_prefixctl pr0; - struct nd_prefix *pr; - - /* - * first, make or update the interface address structure, - * and link it to the list. - */ - if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0) - goto out; - if (ia != NULL) { - if (ia->ia_ifa.ifa_carp) - (*carp_detach_p)(&ia->ia_ifa, true); - ifa_free(&ia->ia_ifa); - } - if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr)) - == NULL) { - /* - * this can happen when the user specify the 0 valid - * lifetime. - */ - break; - } - - if (cmd == ocmd && ifra->ifra_vhid > 0) { - if (carp_attach_p != NULL) - error = (*carp_attach_p)(&ia->ia_ifa, - ifra->ifra_vhid); - else - error = EPROTONOSUPPORT; - if (error) - goto out; - else - carp_attached = 1; - } - - /* - * then, make the prefix on-link on the interface. - * XXX: we'd rather create the prefix before the address, but - * we need at least one address to install the corresponding - * interface route, so we configure the address first. - */ - - /* - * convert mask to prefix length (prefixmask has already - * been validated in in6_update_ifa(). - */ - bzero(&pr0, sizeof(pr0)); - pr0.ndpr_ifp = ifp; - pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, - NULL); - if (pr0.ndpr_plen == 128) { - /* we don't need to install a host route. */ - goto aifaddr_out; - } - pr0.ndpr_prefix = ifra->ifra_addr; - /* apply the mask for safety. */ - IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, - &ifra->ifra_prefixmask.sin6_addr); - - /* - * XXX: since we don't have an API to set prefix (not address) - * lifetimes, we just use the same lifetimes as addresses. - * The (temporarily) installed lifetimes can be overridden by - * later advertised RAs (when accept_rtadv is non 0), which is - * an intended behavior. - */ - pr0.ndpr_raf_onlink = 1; /* should be configurable? */ - pr0.ndpr_raf_auto = - ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0); - pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; - pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; - - /* add the prefix if not yet. */ - if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { - /* - * nd6_prelist_add will install the corresponding - * interface route. - */ - if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) { - if (carp_attached) - (*carp_detach_p)(&ia->ia_ifa, false); - goto out; - } - } - - /* relate the address to the prefix */ - if (ia->ia6_ndpr == NULL) { - ia->ia6_ndpr = pr; - pr->ndpr_addrcnt++; - - /* - * If this is the first autoconf address from the - * prefix, create a temporary address as well - * (when required). - */ - if ((ia->ia6_flags & IN6_IFF_AUTOCONF) && - V_ip6_use_tempaddr && pr->ndpr_addrcnt == 1) { - int e; - if ((e = in6_tmpifadd(ia, 1, 0)) != 0) { - log(LOG_NOTICE, "in6_control: failed " - "to create a temporary address, " - "errno=%d\n", e); - } - } - } - nd6_prefix_rele(pr); - - /* - * this might affect the status of autoconfigured addresses, - * that is, this address might make other addresses detached. - */ - pfxlist_onlink_check(); - -aifaddr_out: - /* - * Try to clear the flag when a new IPv6 address is added - * onto an IFDISABLED interface and it succeeds. - */ - if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) { - struct in6_ndireq nd; - - memset(&nd, 0, sizeof(nd)); - nd.ndi.flags = ND_IFINFO(ifp)->flags; - nd.ndi.flags &= ~ND6_IFF_IFDISABLED; - if (nd6_ioctl(SIOCSIFINFO_FLAGS, (caddr_t)&nd, ifp) < 0) - log(LOG_NOTICE, "SIOCAIFADDR_IN6: " - "SIOCSIFINFO_FLAGS for -ifdisabled " - "failed."); - /* - * Ignore failure of clearing the flag intentionally. - * The failure means address duplication was detected. - */ - } + error = in6_addifaddr(ifp, ifra, ia); + ia = NULL; break; - } case SIOCDIFADDR_IN6: in6_purgeifaddr(ia); @@ -1324,6 +1191,151 @@ ifa_is_p2p(struct in6_ifaddr *ia) return (false); } +int +in6_addifaddr(struct ifnet *ifp, struct in6_aliasreq *ifra, struct in6_ifaddr *ia) +{ + struct nd_prefixctl pr0; + struct nd_prefix *pr; + int carp_attached = 0; + int error; + + /* + * first, make or update the interface address structure, + * and link it to the list. + */ + if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0) + goto out; + if (ia != NULL) { + if (ia->ia_ifa.ifa_carp) + (*carp_detach_p)(&ia->ia_ifa, true); + ifa_free(&ia->ia_ifa); + } + if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr)) == NULL) { + /* + * this can happen when the user specify the 0 valid + * lifetime. + */ + return (0); + } + + if (ifra->ifra_vhid > 0) { + if (carp_attach_p != NULL) + error = (*carp_attach_p)(&ia->ia_ifa, + ifra->ifra_vhid); + else + error = EPROTONOSUPPORT; + if (error) + goto out; + else + carp_attached = 1; + } + + /* + * then, make the prefix on-link on the interface. + * XXX: we'd rather create the prefix before the address, but + * we need at least one address to install the corresponding + * interface route, so we configure the address first. + */ + + /* + * convert mask to prefix length (prefixmask has already + * been validated in in6_update_ifa(). + */ + bzero(&pr0, sizeof(pr0)); + pr0.ndpr_ifp = ifp; + pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, + NULL); + if (pr0.ndpr_plen == 128) { + /* we don't need to install a host route. */ + goto aifaddr_out; + } + pr0.ndpr_prefix = ifra->ifra_addr; + /* apply the mask for safety. */ + IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, + &ifra->ifra_prefixmask.sin6_addr); + + /* + * XXX: since we don't have an API to set prefix (not address) + * lifetimes, we just use the same lifetimes as addresses. + * The (temporarily) installed lifetimes can be overridden by + * later advertised RAs (when accept_rtadv is non 0), which is + * an intended behavior. + */ + pr0.ndpr_raf_onlink = 1; /* should be configurable? */ + pr0.ndpr_raf_auto = + ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0); + pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; + pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; + + /* add the prefix if not yet. */ + if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { + /* + * nd6_prelist_add will install the corresponding + * interface route. + */ + if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) { + if (carp_attached) + (*carp_detach_p)(&ia->ia_ifa, false); + goto out; + } + } + + /* relate the address to the prefix */ + if (ia->ia6_ndpr == NULL) { + ia->ia6_ndpr = pr; + pr->ndpr_addrcnt++; + + /* + * If this is the first autoconf address from the + * prefix, create a temporary address as well + * (when required). + */ + if ((ia->ia6_flags & IN6_IFF_AUTOCONF) && + V_ip6_use_tempaddr && pr->ndpr_addrcnt == 1) { + int e; + if ((e = in6_tmpifadd(ia, 1, 0)) != 0) { + log(LOG_NOTICE, "in6_control: failed " + "to create a temporary address, " + "errno=%d\n", e); + } + } + } + nd6_prefix_rele(pr); + + /* + * this might affect the status of autoconfigured addresses, + * that is, this address might make other addresses detached. + */ + pfxlist_onlink_check(); + +aifaddr_out: + /* + * Try to clear the flag when a new IPv6 address is added + * onto an IFDISABLED interface and it succeeds. + */ + if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) { + struct in6_ndireq nd; + + memset(&nd, 0, sizeof(nd)); + nd.ndi.flags = ND_IFINFO(ifp)->flags; + nd.ndi.flags &= ~ND6_IFF_IFDISABLED; + if (nd6_ioctl(SIOCSIFINFO_FLAGS, (caddr_t)&nd, ifp) < 0) + log(LOG_NOTICE, "SIOCAIFADDR_IN6: " + "SIOCSIFINFO_FLAGS for -ifdisabled " + "failed."); + /* + * Ignore failure of clearing the flag intentionally. + * The failure means address duplication was detected. + */ + } + error = 0; + +out: + if (ia != NULL) + ifa_free(&ia->ia_ifa); + return (error); +} + void in6_purgeaddr(struct ifaddr *ifa) { diff --git a/sys/netinet6/in6_var.h b/sys/netinet6/in6_var.h index 6af0e54ccb75..b40b426d1332 100644 --- a/sys/netinet6/in6_var.h +++ b/sys/netinet6/in6_var.h @@ -889,6 +889,7 @@ int in6_update_ifa(struct ifnet *, struct in6_aliasreq *, struct in6_ifaddr *, int); void in6_prepare_ifra(struct in6_aliasreq *, const struct in6_addr *, const struct in6_addr *); +int in6_addifaddr(struct ifnet *, struct in6_aliasreq *, struct in6_ifaddr *); void in6_purgeaddr(struct ifaddr *); void in6_purgeifaddr(struct in6_ifaddr *); int in6if_do_dad(struct ifnet *);