From: Jon Maloy <jmaloy@redhat.com>
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 [thread overview]
Message-ID: <20261120192437.1860855-6-jmaloy@redhat.com> (raw)
In-Reply-To: <20261120192437.1860855-1-jmaloy@redhat.com>
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 <jmaloy@redhat.com>
---
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 <david@gibson.dropbear.id.au>"
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
prev parent reply other threads:[~2026-11-20 19:24 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 19:24 [PATCH v9 0/5] Introduce multiple addresses Jon Maloy
2026-11-20 19:24 ` [PATCH v9 1/5] tap, conf: Replace addr_fixed with CONF_ADDR_USER flag check Jon Maloy
2026-11-20 19:24 ` [PATCH v9 2/5] fwd: Unify guest accessibility checks with unified address array Jon Maloy
2026-11-20 19:24 ` [PATCH v9 3/5] arp: Check all configured addresses in ARP filtering Jon Maloy
2026-11-20 19:24 ` [PATCH v9 4/5] conf: Allow multiple -a/--address options per address family Jon Maloy
2026-11-20 19:24 ` Jon Maloy [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261120192437.1860855-6-jmaloy@redhat.com \
--to=jmaloy@redhat.com \
--cc=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).