From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=fail (p=quarantine dis=none) header.from=redhat.com Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id 0FEB45A0275 for ; Fri, 20 Nov 2026 20:24:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795202688; 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: in-reply-to:in-reply-to:references:references; bh=5rhKKFtAP01hwEOQql4oVlK4dADrEfcMnddZ42gVZto=; b=bmFULp9o70qqNZt/QrAW+ECv2YYNTEpENSN7vkCxkT/MizD9HwR652/cA1YGYQ98uftdZP DwIChSuDbC7n0lkMnYsCnlUWkXbtYW2a4e4x2HJCEyVkyw+VrD3ksgiUjhD1htnAL4rT1W gPCvT8iH+AzewLLGrvZ389L/uu8L1x8= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-653-8b6H4IV_NXuWGUA41mblYA-1; Fri, 20 Nov 2026 19:24:46 +0000 X-MC-Unique: 8b6H4IV_NXuWGUA41mblYA-1 X-Mimecast-MFC-AGG-ID: 8b6H4IV_NXuWGUA41mblYA_1795202686 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 147111801337; Fri, 20 Nov 2026 19:24:46 +0000 (UTC) Received: from jmaloy-thinkpadp16vgen1.redhat.corp (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5BF1E18004FC; Fri, 20 Nov 2026 19:24:45 +0000 (UTC) From: Jon Maloy To: sbrivio@redhat.com, david@gibson.dropbear.id.au, jmaloy@redhat.com, passt-dev@passt.top Subject: [PATCH v9 5/5] netlink, conf: Read all addresses from template interface at startup Date: Fri, 20 Nov 2026 14:24:37 -0500 Message-ID: <20261120192437.1860855-6-jmaloy@redhat.com> In-Reply-To: <20261120192437.1860855-1-jmaloy@redhat.com> References: <20261120192437.1860855-1-jmaloy@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: bCFRP3cCrhTIrlGjGJXwusuYx7qRlePtAP8fiSMP5Tw_1795202686 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: NGPSLMUCWRVUGVOIZVI7XNNHH7NUA42P X-Message-ID-Hash: NGPSLMUCWRVUGVOIZVI7XNNHH7NUA42P X-MailFrom: jmaloy@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Add nl_addr_get_all() to read all addresses from the template interface into the c->addrs[] array, replacing nl_addr_get(), which only returned a single address per family. With pasta and --config-net, all addresses of the template interface are already copied to the namespace, but so far, we only knew about one of them. Recording all of them lets the address checks, ARP filtering and the guest accessibility checks consider every address the guest might use, and makes multi-address setups work in passt mode, too. The first address of each family in c->addrs[] is the one we assign via DHCP, DHCPv6 and NDP. To keep the behaviour introduced by commit f091893c1ffe ("netlink: Fetch most specific (longest prefix) address in nl_addr_get()"), add the address with the longest prefix first, and the other ones after it, in the order reported by the kernel. Otherwise, in nested setups, an L2 guest would pick the address the L1 guest obtained via SLAAC rather than the one matching the host via DHCPv6. For IPv6, the function also stores the link-local address with the longest prefix into c->ip6.our_tap_ll, as nl_addr_get() did. IPv4 addresses with link scope are now skipped altogether. Update conf_ip4() and conf_ip6() to use nl_addr_get_all() when no user-provided addresses are present. Signed-off-by: Jon Maloy --- v7: -Excluded all link-local addresses from host to be added to array -Minor comment change suggested by Stefano v8: -Adapted to previous changes in series v9: - Adapted to previous changes in code base and series - Add the address with the longest prefix first, to keep the address selection of nl_addr_get(), and pick the IPv6 link-local address the same way - Store IPv4 prefix lengths in IPv6 format (+96) - Removed "Reviewed-by: David Gibson " because of above change. --- conf.c | 50 +++++++++++++------------------ netlink.c | 88 ++++++++++++++++++++++++++++++++++++++++--------------- netlink.h | 3 +- 3 files changed, 86 insertions(+), 55 deletions(-) diff --git a/conf.c b/conf.c index f0a87d8f..36652bc0 100644 --- a/conf.c +++ b/conf.c @@ -385,8 +385,6 @@ static unsigned int conf_ip4(struct ctx *c, unsigned int ifi) const struct guest_addr *a; const struct in_addr *a4; - a = fwd_get_addr(c, AF_INET, true, CONF_ADDR_ANY); - if (!ifi) ifi = nl_get_ext_if(nl_sock, AF_INET); @@ -405,26 +403,20 @@ static unsigned int conf_ip4(struct ctx *c, unsigned int ifi) } } + a = fwd_get_addr(c, AF_INET, true, CONF_ADDR_USER); if (!a) { - struct in_addr addr = { 0 }; - int prefix_len = 0; - int rc = nl_addr_get(nl_sock, ifi, AF_INET, - &addr, &prefix_len, NULL); + int rc = nl_addr_get_all(c, nl_sock, ifi, AF_INET); + if (rc < 0) { - debug("Couldn't discover IPv4 address: %s", + debug("Couldn't discover IPv4 addresses: %s", strerror_(-rc)); return 0; } - if (IN4_IS_ADDR_UNSPECIFIED(&addr)) { - warn("Host returned address 0.0.0.0"); + a = fwd_get_addr(c, AF_INET, true, CONF_ADDR_ANY); + if (!rc || !a) return 0; - } - fwd_set_addr(c, &inany_from_v4(addr), CONF_ADDR_HOST, - prefix_len + 96); } - a = fwd_get_addr(c, AF_INET, true, CONF_ADDR_ANY); - assert(a); a4 = inany_v4(&a->addr); assert(a4); ip4->addr_seen = *a4; @@ -473,8 +465,6 @@ static unsigned int conf_ip6(struct ctx *c, unsigned int ifi) { struct ip6_ctx *ip6 = &c->ip6; const struct guest_addr *a; - union inany_addr addr; - int prefix_len = 0; int rc; if (!ifi) @@ -494,24 +484,26 @@ static unsigned int conf_ip6(struct ctx *c, unsigned int ifi) } } - rc = nl_addr_get(nl_sock, ifi, AF_INET6, &addr.a6, - &prefix_len, &ip6->our_tap_ll); - if (rc < 0) { - debug("Couldn't discover IPv6 address: %s", strerror_(-rc)); - return 0; - } - - a = fwd_get_addr(c, AF_INET6, true, CONF_ADDR_ANY); + a = fwd_get_addr(c, AF_INET6, true, CONF_ADDR_USER); if (!a) { - if (IN6_IS_ADDR_UNSPECIFIED(&addr)) + rc = nl_addr_get_all(c, nl_sock, ifi, AF_INET6); + if (rc < 0) { + debug("Couldn't discover IPv6 addresses: %s", + strerror_(-rc)); return 0; - - fwd_set_addr(c, &addr, CONF_ADDR_HOST, prefix_len); - ip6->addr_seen = addr.a6; + } + a = fwd_get_addr(c, AF_INET6, true, CONF_ADDR_HOST); } else { - ip6->addr_seen = a->addr.a6; + rc = nl_addr_get_ll(nl_sock, ifi, &ip6->our_tap_ll); + if (rc < 0) { + debug("Couldn't get link-local address: %s", + strerror_(-rc)); + } } + if (a) + ip6->addr_seen = a->addr.a6; + if (IN6_IS_ADDR_LINKLOCAL(&ip6->guest_gw)) ip6->our_tap_ll = ip6->guest_gw; else diff --git a/netlink.c b/netlink.c index 8d7f2e7b..7ef67073 100644 --- a/netlink.c +++ b/netlink.c @@ -760,20 +760,21 @@ int nl_addr_set_ll_nodad(int s, unsigned int ifi) } /** - * nl_addr_get() - Get most specific global address, given interface and family + * nl_addr_get_all() - Get all addresses for a given interface into ctx + * @c: Execution context * @s: Netlink socket - * @ifi: Interface index in outer network namespace - * @af: Address family - * @addr: Global address to fill - * @prefix_len: Mask or prefix length, to fill - * @addr_l: Link-scoped address to fill (for IPv6) + * @ifi: Interface index + * @af: Address family (AF_INET or AF_INET6) * - * Return: 0 on success, negative error code on failure + * Populates c->addrs[] with all non-deprecated addresses from the interface. + * For IPv6, also captures link-local address in c->ip6.our_tap_ll. + * Skips IPv6 link-local addresses for the main array. The address with the + * longest prefix is added first, so that it's the one we assign to the guest. + * + * Return: number of addresses added, or negative error code on failure */ -int nl_addr_get(int s, unsigned int ifi, sa_family_t af, - void *addr, int *prefix_len, void *addr_l) +int nl_addr_get_all(struct ctx *c, int s, unsigned int ifi, sa_family_t af) { - uint8_t prefix_max = 0, prefix_max_ll = 0; struct req_t { struct nlmsghdr nlh; struct ifaddrmsg ifa; @@ -781,6 +782,11 @@ int nl_addr_get(int s, unsigned int ifi, sa_family_t af, .ifa.ifa_family = af, .ifa.ifa_index = ifi, }; + struct { + union inany_addr addr; + int prefix_len; + } found[MAX_GUEST_ADDRS]; + int best = 0, n = 0, ll_plen = 0, i; struct nlmsghdr *nh; char buf[NLBUFSIZ]; ssize_t status; @@ -797,27 +803,61 @@ int nl_addr_get(int s, unsigned int ifi, sa_family_t af, for (rta = IFA_RTA(ifa), na = IFA_PAYLOAD(nh); RTA_OK(rta, na); rta = RTA_NEXT(rta, na)) { - if ((af == AF_INET && rta->rta_type != IFA_LOCAL) || - (af == AF_INET6 && rta->rta_type != IFA_ADDRESS)) + if (af == AF_INET && rta->rta_type != IFA_LOCAL) + continue; + if (af == AF_INET6 && rta->rta_type != IFA_ADDRESS) continue; - if (ifa->ifa_prefixlen > prefix_max && addr && - (af == AF_INET || ifa->ifa_scope < RT_SCOPE_LINK)) { - memcpy(addr, RTA_DATA(rta), RTA_PAYLOAD(rta)); - - prefix_max = *prefix_len = ifa->ifa_prefixlen; + /* Skip link-local addresses (kernel auto-configures), + * but keep the IPv6 one with the longest prefix as ours + */ + if (ifa->ifa_scope == RT_SCOPE_LINK) { + if (af == AF_INET || + ifa->ifa_prefixlen <= ll_plen) + continue; + memcpy(&c->ip6.our_tap_ll, RTA_DATA(rta), + sizeof(c->ip6.our_tap_ll)); + ll_plen = ifa->ifa_prefixlen; + continue; } - if (addr_l && - af == AF_INET6 && ifa->ifa_scope == RT_SCOPE_LINK && - ifa->ifa_prefixlen > prefix_max_ll) { - memcpy(addr_l, RTA_DATA(rta), RTA_PAYLOAD(rta)); + if (n >= MAX_GUEST_ADDRS) + continue; - prefix_max_ll = ifa->ifa_prefixlen; - } + inany_from_af(&found[n].addr, af, RTA_DATA(rta)); + found[n].prefix_len = ifa->ifa_prefixlen; + if (af == AF_INET) + found[n].prefix_len += 96; + + if (found[n].prefix_len > found[best].prefix_len) + best = n; + n++; } } - return status; + + if (status < 0) + return status; + + /* The first address of each family in c->addrs[] is the one we assign + * via DHCP, DHCPv6 and NDP (see fwd_get_addr()). If there are several, + * we want the one with the longest prefix, as that's likely the most + * specific assignment, e.g. a /128 from DHCPv6 rather than a /64 from + * SLAAC: add it first, then the others in the order reported by the + * kernel. + */ + if (n) { + fwd_set_addr(c, &found[best].addr, CONF_ADDR_HOST, + found[best].prefix_len); + } + + for (i = 0; i < n; i++) { + if (i != best) { + fwd_set_addr(c, &found[i].addr, CONF_ADDR_HOST, + found[i].prefix_len); + } + } + + return n; } /** diff --git a/netlink.h b/netlink.h index b22f485a..3af6d58b 100644 --- a/netlink.h +++ b/netlink.h @@ -19,8 +19,7 @@ int nl_route_get_def(int s, unsigned int ifi, sa_family_t af, void *gw); int nl_route_set_def(int s, unsigned int ifi, sa_family_t af, const void *gw); int nl_route_dup(int s_src, unsigned int ifi_src, int s_dst, unsigned int ifi_dst, sa_family_t af); -int nl_addr_get(int s, unsigned int ifi, sa_family_t af, - void *addr, int *prefix_len, void *addr_l); +int nl_addr_get_all(struct ctx *c, int s, unsigned int ifi, sa_family_t af); bool nl_neigh_mac_get(int s, const union inany_addr *addr, int ifi, unsigned char *mac); int nl_addr_set(int s, unsigned int ifi, sa_family_t af, -- 2.56.1