* [PATCH v9 1/5] tap, conf: Replace addr_fixed with CONF_ADDR_USER flag check
2026-11-20 19:24 [PATCH v9 0/5] Introduce multiple addresses Jon Maloy
@ 2026-11-20 19:24 ` Jon Maloy
2026-11-20 19:24 ` [PATCH v9 2/5] fwd: Unify guest accessibility checks with unified address array Jon Maloy
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Jon Maloy @ 2026-11-20 19:24 UTC (permalink / raw)
To: sbrivio, david, jmaloy, passt-dev
The addr_fixed boolean (per address family) was introduced to prevent
observed traffic from overwriting addr_seen when the user explicitly
set an address with -a/--address.
The unified address array already tracks this: fwd_set_addr() marks
user-provided addresses with CONF_ADDR_USER. We now replace all
addr_fixed checks with fwd_get_addr() lookups for CONF_ADDR_USER
and remove the now-redundant field from ip4_ctx and ip6_ctx.
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
v9: -Adapted to changes due to rebase
-Adapted to change in fwd_get_addr()
---
conf.c | 7 ++-----
passt.h | 6 ------
tap.c | 6 +++---
3 files changed, 5 insertions(+), 14 deletions(-)
diff --git a/conf.c b/conf.c
index fd9f0771..27656271 100644
--- a/conf.c
+++ b/conf.c
@@ -1266,13 +1266,10 @@ static bool conf_addr(struct ctx *c, char *arg, uint8_t opt_n)
goto bad;
fwd_set_addr(c, &addr, CONF_ADDR_USER, prefix_len);
- if (a4) {
+ if (a4)
c->ip4.no_copy_addrs = true;
- c->ip4.addr_fixed = true;
- } else {
+ else
c->ip6.no_copy_addrs = true;
- c->ip6.addr_fixed = true;
- }
return is_prefix;
diff --git a/passt.h b/passt.h
index d4661d81..40d22bba 100644
--- a/passt.h
+++ b/passt.h
@@ -101,8 +101,6 @@ struct guest_addr {
* @ifname_out: Optional interface name to bind outbound sockets to
* @no_copy_routes: Don't copy all routes when configuring target namespace
* @no_copy_addrs: Don't copy all addresses when configuring namespace
- * @addr_fixed: Address was given explicitly (-a): don't update
- * addr_seen from traffic observed on tap
*/
struct ip4_ctx {
/* PIF_TAP addresses */
@@ -122,7 +120,6 @@ struct ip4_ctx {
bool no_copy_routes;
bool no_copy_addrs;
- bool addr_fixed;
};
/**
@@ -143,8 +140,6 @@ struct ip4_ctx {
* @ifname_out: Optional interface name to bind outbound sockets to
* @no_copy_routes: Don't copy all routes when configuring target namespace
* @no_copy_addrs: Don't copy all addresses when configuring namespace
- * @addr_fixed: Address was given explicitly (-a): don't update
- * addr_seen from traffic observed on tap
*/
struct ip6_ctx {
/* PIF_TAP addresses */
@@ -166,7 +161,6 @@ struct ip6_ctx {
bool no_copy_routes;
bool no_copy_addrs;
- bool addr_fixed;
};
#include <netinet/if_ether.h>
diff --git a/tap.c b/tap.c
index 100afc83..cc2dfbcc 100644
--- a/tap.c
+++ b/tap.c
@@ -756,7 +756,7 @@ resume:
continue;
}
- if (!c->ip4.addr_fixed &&
+ if (!fwd_get_addr(c, AF_INET, true, CONF_ADDR_USER) &&
iph->saddr && c->ip4.addr_seen.s_addr != iph->saddr)
c->ip4.addr_seen.s_addr = iph->saddr;
@@ -1000,7 +1000,7 @@ resume:
if (IN6_IS_ADDR_LINKLOCAL(saddr)) {
c->ip6.addr_ll_seen = *saddr;
- if (!c->ip6.addr_fixed &&
+ if (!fwd_get_addr(c, AF_INET6, true, CONF_ADDR_USER) &&
IN6_IS_ADDR_UNSPECIFIED(&c->ip6.addr_seen)) {
c->ip6.addr_seen = *saddr;
}
@@ -1011,7 +1011,7 @@ resume:
fwd_set_addr(c, &addr, CONF_ADDR_GEN, 64);
}
- } else if (!c->ip6.addr_fixed &&
+ } else if (!fwd_get_addr(c, AF_INET6, true, CONF_ADDR_USER) &&
!IN6_IS_ADDR_UNSPECIFIED(saddr)) {
c->ip6.addr_seen = *saddr;
}
--
2.56.1
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v9 2/5] fwd: Unify guest accessibility checks with unified address array
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 ` Jon Maloy
2026-11-20 19:24 ` [PATCH v9 3/5] arp: Check all configured addresses in ARP filtering Jon Maloy
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Jon Maloy @ 2026-11-20 19:24 UTC (permalink / raw)
To: sbrivio, david, jmaloy, passt-dev
We replace the fwd_guest_accessible4() and fwd_guest_accessible6()
functions with a unified fwd_guest_accessible() function that handles
both address families. With the unified address array, we can check
all configured addresses in a single pass using for_each_addr() with
family filter AF_UNSPEC.
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
v6: -Some fixes based on feedback from David Gibson
v7: -Added curly brackets to for_each_loop() in fwd_guest_accessible(),
as suggested by Stefano Brivio.
v9: Adapted to changes due to rebase and earlier changes in series
---
fwd.c | 71 +++++++++++++----------------------------------------------
1 file changed, 15 insertions(+), 56 deletions(-)
diff --git a/fwd.c b/fwd.c
index 48764c50..5716c9ad 100644
--- a/fwd.c
+++ b/fwd.c
@@ -847,20 +847,19 @@ static bool is_dns_flow(uint8_t proto, const struct flowside *ini)
}
/**
- * fwd_guest_accessible4() - Is IPv4 address guest-accessible
+ * fwd_guest_accessible() - Is address guest-accessible
* @c: Execution context
- * @addr: Host visible IPv4 address
+ * @addr: Host visible address (IPv4 or IPv6)
*
* Return: true if @addr on the host is accessible to the guest without
* translation, false otherwise
*/
-static bool fwd_guest_accessible4(const struct ctx *c,
- const struct in_addr *addr)
+static bool fwd_guest_accessible(const struct ctx *c,
+ const union inany_addr *addr)
{
const struct guest_addr *a;
- const struct in_addr *a4;
- if (IN4_IS_ADDR_LOOPBACK(addr))
+ if (inany_is_loopback(addr))
return false;
/* In socket interfaces 0.0.0.0 generally means "any" or unspecified,
@@ -868,39 +867,18 @@ static bool fwd_guest_accessible4(const struct ctx *c,
* that has a different meaning for host and guest, we can't let it
* through untranslated.
*/
- if (IN4_IS_ADDR_UNSPECIFIED(addr))
+ if (inany_is_unspecified(addr))
return false;
- /* For IPv4, addr_seen is initialised to addr, so is always a valid
- * address
+ /* Check against all configured guest addresses */
+ for_each_addr(a, c->addrs, c->addr_count, AF_UNSPEC) {
+ if (inany_equals(addr, &a->addr))
+ return false;
+ }
+ /* Also check addr_seen: it tracks the address the guest is actually
+ * using, which may differ from configured addresses.
*/
- a = fwd_get_addr(c, AF_INET, true, CONF_ADDR_ANY);
- a4 = a ? inany_v4(&a->addr) : NULL;
- if ((a4 && IN4_ARE_ADDR_EQUAL(addr, a4)) ||
- IN4_ARE_ADDR_EQUAL(addr, &c->ip4.addr_seen))
- return false;
-
- return true;
-}
-
-/**
- * fwd_guest_accessible6() - Is IPv6 address guest-accessible
- * @c: Execution context
- * @addr: Host visible IPv6 address
- *
- * Return: true if @addr on the host is accessible to the guest without
- * translation, false otherwise
- */
-static bool fwd_guest_accessible6(const struct ctx *c,
- const struct in6_addr *addr)
-{
- const struct guest_addr *a;
-
- if (IN6_IS_ADDR_LOOPBACK(addr))
- return false;
-
- a = fwd_get_addr(c, AF_INET6, true, CONF_ADDR_ANY);
- if (a && IN6_ARE_ADDR_EQUAL(addr, &a->addr.a6))
+ if (inany_equals4(addr, &c->ip4.addr_seen))
return false;
/* For IPv6, addr_seen starts unspecified, because we don't know what LL
@@ -908,31 +886,12 @@ static bool fwd_guest_accessible6(const struct ctx *c,
* if it has been set to a real address.
*/
if (!IN6_IS_ADDR_UNSPECIFIED(&c->ip6.addr_seen) &&
- IN6_ARE_ADDR_EQUAL(addr, &c->ip6.addr_seen))
+ inany_equals6(addr, &c->ip6.addr_seen))
return false;
return true;
}
-/**
- * fwd_guest_accessible() - Is IPv[46] address guest-accessible
- * @c: Execution context
- * @addr: Host visible IPv[46] address
- *
- * Return: true if @addr on the host is accessible to the guest without
- * translation, false otherwise
- */
-static bool fwd_guest_accessible(const struct ctx *c,
- const union inany_addr *addr)
-{
- const struct in_addr *a4 = inany_v4(addr);
-
- if (a4)
- return fwd_guest_accessible4(c, a4);
-
- return fwd_guest_accessible6(c, &addr->a6);
-}
-
/**
* nat_outbound() - Apply address translation for outbound (TAP to HOST)
* @c: Execution context
--
2.56.1
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v9 3/5] arp: Check all configured addresses in ARP filtering
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 ` 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 ` [PATCH v9 5/5] netlink, conf: Read all addresses from template interface at startup Jon Maloy
4 siblings, 0 replies; 6+ messages in thread
From: Jon Maloy @ 2026-11-20 19:24 UTC (permalink / raw)
To: sbrivio, david, jmaloy, passt-dev
As a preparation for handling multiple addresses, we update ignore_arp()
to check against all addresses in the unified addrs[] array using the
for_each_addr() macro.
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
v3: -Adapted to single-array changes earlier in this series
v6: -Made loop in ignore_arp() a little more palatable,
but not entirely as suggested by David.
v7: -Curly brackets in ignore_arp(), as suggested by Stefano.
-I did not modify the for_each_addr(), macro, as Stefano suggested,
since it also would require us to add an extra parameter indicating
the array length.
v9: Adapted to changes due to rebase and earlier changes in series
---
arp.c | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/arp.c b/arp.c
index 08d5a9f9..74167e44 100644
--- a/arp.c
+++ b/arp.c
@@ -42,7 +42,7 @@ static bool ignore_arp(const struct ctx *c,
const struct arphdr *ah, const struct arpmsg *am)
{
const struct guest_addr *a;
- const struct in_addr *a4;
+ union inany_addr addr;
if (ah->ar_hrd != htons(ARPHRD_ETHER) ||
ah->ar_pro != htons(ETH_P_IP) ||
@@ -56,14 +56,12 @@ static bool ignore_arp(const struct ctx *c,
!memcmp(am->sip, am->tip, sizeof(am->sip)))
return true;
- /* Don't resolve the guest's assigned address, either. */
- a = fwd_get_addr(c, AF_INET, true, CONF_ADDR_ANY);
- if (!a)
- return false;
- a4 = inany_v4(&a->addr);
- if (a4 && !memcmp(am->tip, a4, sizeof(am->tip)))
- return true;
-
+ /* Don't resolve any of the guest's addresses */
+ inany_from_af(&addr, AF_INET, am->tip);
+ for_each_addr(a, c->addrs, c->addr_count, AF_INET) {
+ if (inany_equals(&addr, &a->addr))
+ return true;
+ }
return false;
}
--
2.56.1
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v9 4/5] conf: Allow multiple -a/--address options per address family
2026-11-20 19:24 [PATCH v9 0/5] Introduce multiple addresses Jon Maloy
` (2 preceding siblings ...)
2026-11-20 19:24 ` [PATCH v9 3/5] arp: Check all configured addresses in ARP filtering Jon Maloy
@ 2026-11-20 19:24 ` Jon Maloy
2026-11-20 19:24 ` [PATCH v9 5/5] netlink, conf: Read all addresses from template interface at startup Jon Maloy
4 siblings, 0 replies; 6+ messages in thread
From: Jon Maloy @ 2026-11-20 19:24 UTC (permalink / raw)
To: sbrivio, david, jmaloy, passt-dev
Allow specifying multiple addresses per family with -a/--address.
The first address of each family is used for DHCP/DHCPv6 assignment.
Link: https://bugs.passt.top/show_bug.cgi?id=47
Cc: lemmi@nerd2nerd.org
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
v2: - Adapted to previous code changes
v3: - Adapted to single-array strategy
- Changes according to feedback from S. Brivio and G Gibson.
v4: - Stripped down and adapted after feedback from David G.
v6: - Adapted to previous changes in series
- Removed the "one address" limitation for -n option
v7: - Updated man page.
v8: - Adapted to previous changes in this series
- Updated according to comments from David G.
v9: - Adapted to previous changes in this series
---
conf.c | 6 ++++--
fwd.c | 9 +++++----
passt.1 | 18 ++++++++----------
pasta.c | 27 +++++++++++++++------------
4 files changed, 32 insertions(+), 28 deletions(-)
diff --git a/conf.c b/conf.c
index 27656271..f0a87d8f 100644
--- a/conf.c
+++ b/conf.c
@@ -628,9 +628,11 @@ static void usage(const char *name, FILE *f, int status)
" default: 65520: maximum 802.3 MTU minus 802.3 header\n"
" length, rounded to 32 bits (IPv4 words)\n"
" -a, --address ADDR Assign IPv4 or IPv6 address ADDR[/PREFIXLEN]\n"
- " can be specified zero to two times (for IPv4 and IPv6)\n"
+ " can be specified up to a maximum of %d times\n"
" default: use addresses from interface with default route\n"
- " -n, --netmask MASK Assign IPv4 MASK, dot-decimal or bits\n"
+ " -n, --netmask MASK Assign IPv4 MASK, dot-decimal or bits\n",
+ MAX_GUEST_ADDRS);
+ FPRINTF(f,
" default: netmask from matching address on the host\n"
" -M, --mac-addr ADDR Use source MAC address ADDR\n"
" default: 9a:55:9a:55:9a:55 (locally administered)\n"
diff --git a/fwd.c b/fwd.c
index 5716c9ad..8fbabb29 100644
--- a/fwd.c
+++ b/fwd.c
@@ -250,8 +250,7 @@ void fwd_neigh_table_init(const struct ctx *c)
* @flags: CONF_ADDR_* flags for this address
* @prefix_len: Prefix length in IPv6 format (96-128 for IPv4-mapped)
*
- * Find the first existing entry of the same address family and
- * overwrite it, or create a new one if none exists
+ * Update the existing entry for @addr, if any, or append a new one
*/
void fwd_set_addr(struct ctx *c, const union inany_addr *addr,
uint8_t flags, int prefix_len)
@@ -260,8 +259,10 @@ void fwd_set_addr(struct ctx *c, const union inany_addr *addr,
assert(flags);
- for_each_addr(a, c->addrs, c->addr_count, inany_af(addr))
- goto found;
+ for_each_addr(a, c->addrs, c->addr_count, inany_af(addr)) {
+ if (inany_equals(&a->addr, addr))
+ goto found;
+ }
if (c->addr_count >= MAX_GUEST_ADDRS)
return;
diff --git a/passt.1 b/passt.1
index 5d236bdc..cdaaf96a 100644
--- a/passt.1
+++ b/passt.1
@@ -169,16 +169,14 @@ An optional /\fIprefix_len\fR (0-32 for IPv4, 0-128 for IPv6) can be
appended in CIDR notation (e.g. 192.0.2.1/24). This is an alternative to
using the \fB-n\fR, \fB--netmask\fR option. Mixing CIDR notation with
\fB-n\fR results in an error.
-If a prefix length is assigned to an IPv6 address using this method, it will
-in the current code version be overridden by the default value of 64.
-This option can be specified zero (for defaults) to two times (once for IPv4,
-once for IPv6).
-By default, assigned IPv4 and IPv6 addresses are taken from the host interfaces
-with the first default route, if any, for the corresponding IP version. If no
-default routes are available and there is any interface with any route for a
-given IP version, the first of these interfaces will be chosen instead. If no
-such interface exists for a given IP version, the link-local address 169.254.2.1
-is assigned for IPv4, and no additional address will be assigned for IPv6.
+This option can be given multiple times, indicating multiple different
+addresses. By default, assigned IPv4 and IPv6 addresses are taken from
+the host interfaces with the first default route, if any, for the
+corresponding IP version. If no default routes are available and there
+is any interface with any route for a given IP version, the first of
+these interfaces will be chosen instead. If no such interface exists for
+a given IP version, the link-local address 169.254.2.1 is assigned for
+IPv4, and no additional address will be assigned for IPv6.
.TP
.BR \-n ", " \-\-netmask " " \fImask
diff --git a/pasta.c b/pasta.c
index 4cd58fd7..02bd87e5 100644
--- a/pasta.c
+++ b/pasta.c
@@ -303,6 +303,7 @@ static int pasta_conf_addrs(const struct ctx *c, sa_family_t af, int ifi,
bool no_copy)
{
const struct guest_addr *a;
+ int rc = 0;
if (!ifi)
return 0;
@@ -310,21 +311,23 @@ static int pasta_conf_addrs(const struct ctx *c, sa_family_t af, int ifi,
if (!no_copy)
return nl_addr_dup(nl_sock, ifi, nl_sock_ns, c->pasta_ifi, af);
- a = fwd_get_addr(c, af, true, CONF_ADDR_ANY);
- if (!a)
- return 0;
-
- if (af == AF_INET) {
- const struct in_addr *a4;
- int plen = 0;
+ for_each_addr(a, c->addrs, c->addr_count, af) {
+ if (af == AF_INET) {
+ const struct in_addr *a4;
+ int plen = 0;
- a4 = inany_prefix_v4(&a->addr, a->prefix_len, &plen);
- return nl_addr_set(nl_sock_ns, c->pasta_ifi, AF_INET,
- a4, plen);
+ a4 = inany_prefix_v4(&a->addr, a->prefix_len, &plen);
+ rc = nl_addr_set(nl_sock_ns, c->pasta_ifi, AF_INET,
+ a4, plen);
+ } else {
+ rc = nl_addr_set(nl_sock_ns, c->pasta_ifi, AF_INET6,
+ &a->addr.a6, a->prefix_len);
+ }
+ if (rc < 0)
+ break;
}
- return nl_addr_set(nl_sock_ns, c->pasta_ifi, AF_INET6,
- &a->addr.a6, a->prefix_len);
+ return rc;
}
/**
--
2.56.1
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v9 5/5] netlink, conf: Read all addresses from template interface at startup
2026-11-20 19:24 [PATCH v9 0/5] Introduce multiple addresses Jon Maloy
` (3 preceding siblings ...)
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
4 siblings, 0 replies; 6+ messages in thread
From: Jon Maloy @ 2026-11-20 19:24 UTC (permalink / raw)
To: sbrivio, david, jmaloy, passt-dev
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
^ permalink raw reply [flat|nested] 6+ messages in thread