From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=TY4GhVU7; dkim-atps=neutral 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 DECCA5A065B for ; Wed, 21 Jan 2026 20:42:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1769024545; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BGsol5HBkU+WO0BUKhYL0otYe1PEeH60K9gmac35eb4=; b=TY4GhVU7Il0vR/4ZH/imyJk5eMgGVrdq/7TRDO5WjRmjnvk1RCs0qjSY5S2xv+oY05W1ic mNBh5Xo8dDFzhkYcAkmt9LVBBJ3KR9QUTDKFu9ZjNAwdHbu6X5f5KgWln5yxdVU7Y4El0J 6jLFUl2btdUk58S3Ub7+p1HPw+OZUXE= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-679-lmu5CzOGO1WO1F1KazVyYA-1; Wed, 21 Jan 2026 14:42:24 -0500 X-MC-Unique: lmu5CzOGO1WO1F1KazVyYA-1 X-Mimecast-MFC-AGG-ID: lmu5CzOGO1WO1F1KazVyYA_1769024543 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-430fc83f58dso116696f8f.2 for ; Wed, 21 Jan 2026 11:42:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769024543; x=1769629343; h=content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BGsol5HBkU+WO0BUKhYL0otYe1PEeH60K9gmac35eb4=; b=oTh233HPCm3v1WmPY3yEwSqZIdnxpX0ZIdYjM2vFsT03171EEhrBVToN/JfyZS88tH YSLDtS6fjne37/FOQIo7dPcOi7M88gvqtdc+5f3JfaqhMt9dAfAvvO7LAFZvz4XJr2gY 2BbY9VcBMADWlfGDALIorMQudpzx8UmSJlZCPO5lmRqTtMhuN996qwmt44+0LGeweT+d 1Kjrux95aflIQyIrn7uELumX/kL9VkvTIbo3fGpT3kw9rrF6wTRdSjsAcgRWMr91QMcU nLaAC1+ItFWj0nd6piVPzxcIASmV6W092KWaqU49U11nzy0Uv0rIdFv0WZNGMkKDF5+n 2ymg== X-Forwarded-Encrypted: i=1; AJvYcCWwvq4AF5lKM8Z+Kt0Q/9tzb2AQguuff4Q0Dk1o0d0sho504f7j9b4T90meLSKZhvI+T4JvlP55/WA=@passt.top X-Gm-Message-State: AOJu0Yz4O228LsyXkNnP8ykxKeTwxLh4D5zXoSQDjmg3DbN/Sw+sqd7l hThBGBIEfAvS238+L6aTOIx6atafMGaETf8OnwyRoaEtNd5jAj3oNmdbAJ6Y8rOnEMF3cDzROrf UtCk8D5hqEYDzoRzXBXIYspfX4HR5MEncXZjDVq3PmTWvRxz79S6LdJ9nBZnnRQ== X-Gm-Gg: AZuq6aK73O2XP99UJgNav0oK1sSC8muEcjdFUKaDKSYXT6blETZ10SCJv9aBZoHn2Hy 47Sq8Rd/gbw9OpNk5Kx9l2D5pWeBPkG+waWvh4PuBEwRqmbCE0lolh9hceSDfxvNUkZaAispZn6 cJxxcb+XdxzxWY4s2TUlJwrPnR8sDyq2kk8GcwqDyNZCQZAcLbxWKqeOhbHoiMOzzK0PktUX9XZ /ODULs+xaaIgoCdvjxjVsy1DwLPHes+v9h6Q1ZOOSOkfIG+tF4ptX18fdcbK02MF4REcvXgQc5w MT0wRro5O1VeR3uwfd2YW4jrGTrcrkfZAbu6mo1uLEFzXM+PsRP1s0hjD41WbKQdVrckbWy+L1D TZzaRZ5Mq70CxaHnHJ3rd X-Received: by 2002:a5d:64c3:0:b0:42b:3131:5437 with SMTP id ffacd0b85a97d-43569bc17f6mr24131115f8f.34.1769024542772; Wed, 21 Jan 2026 11:42:22 -0800 (PST) X-Received: by 2002:a5d:64c3:0:b0:42b:3131:5437 with SMTP id ffacd0b85a97d-43569bc17f6mr24131087f8f.34.1769024542293; Wed, 21 Jan 2026 11:42:22 -0800 (PST) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [2a10:fc81:a806:d6a9::1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4356996dad0sm39263010f8f.27.2026.01.21.11.42.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jan 2026 11:42:21 -0800 (PST) Date: Wed, 21 Jan 2026 20:42:20 +0100 From: Stefano Brivio To: Jon Maloy Subject: Re: [PATCH v2 8/9] ip: Track observed guest IPv4 addresses in unified address array Message-ID: <20260121204220.2032cec9@elisabeth> In-Reply-To: <20260118221612.2115386-9-jmaloy@redhat.com> References: <20260118221612.2115386-1-jmaloy@redhat.com> <20260118221612.2115386-9-jmaloy@redhat.com> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.49; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: IOuSP2Nlx6-K6GOwwO8FvaFKzauUxtORs-zWvwlJ2G4_1769024543 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: CNAIKVEQW56O2PLMIBKYTLIR2JIREQBB X-Message-ID-Hash: CNAIKVEQW56O2PLMIBKYTLIR2JIREQBB X-MailFrom: sbrivio@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 CC: dgibson@redhat.com, david@gibson.dropbear.id.au, passt-dev@passt.top 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: On Sun, 18 Jan 2026 17:16:11 -0500 Jon Maloy wrote: > We remove the addr_seen field in struct ip4_ctx and replace it by > setting a new INANY_ADDR_OBSERVED flag in the corresponding entry in > the new address array. If the seen address is not present in the > array we add it first. > > Signed-off-by: Jon Maloy > --- > conf.c | 3 --- > fwd.c | 49 ++++++++++++++++++++++++++++++++++++++----------- > inany.h | 1 + > migrate.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- > passt.h | 3 --- > tap.c | 38 +++++++++++++++++++++++++++++++++----- > 6 files changed, 114 insertions(+), 24 deletions(-) > > diff --git a/conf.c b/conf.c > index 22c2222..8f0091d 100644 > --- a/conf.c > +++ b/conf.c > @@ -787,8 +787,6 @@ static unsigned int conf_ip4(unsigned int ifi, struct ip4_ctx *ip4) > ip4->addrs[i].prefix_len = ip4_default_prefix_len(a4); > } > > - ip4->addr_seen = *inany_v4(&ip4->addrs[0].addr); > - > ip4->our_tap_addr = ip4->guest_gw; > > if (!ip4->addr_count) > @@ -804,7 +802,6 @@ static unsigned int conf_ip4(unsigned int ifi, struct ip4_ctx *ip4) > static void conf_ip4_local(struct ip4_ctx *ip4) > { > ip4->addrs[0].addr = inany_from_v4(IP4_LL_GUEST_ADDR); > - ip4->addr_seen = *inany_v4(&ip4->addrs[0].addr); > ip4->our_tap_addr = ip4->guest_gw = IP4_LL_GUEST_GW; > ip4->addrs[0].prefix_len = IP4_LL_PREFIX_LEN; > ip4->addr_count = 1; > diff --git a/fwd.c b/fwd.c > index f1db34c..08b0b83 100644 > --- a/fwd.c > +++ b/fwd.c > @@ -491,6 +491,29 @@ static bool is_dns_flow(uint8_t proto, const struct flowside *ini) > ((ini->oport == 53) || (ini->oport == 853)); > } > > +/** > + * fwd_guest_addr4() - Get first observed IPv4 guest address > + * @c: Execution context > + * > + * Return: pointer to first observed IPv4 address, or first address if none > + * observed, or NULL if no addresses Well, it's kind of obvious it's a pointer. What about: * Return: first observed IPv4 address if any, first configured if any, or NULL ? > + */ > +static const struct in_addr *fwd_guest_addr4(const struct ctx *c) > +{ > + int i; > + > + /* Find first observed address */ > + for (i = 0; i < c->ip4.addr_count; i++) > + if (c->ip4.addrs[i].flags & INANY_ADDR_OBSERVED) > + return inany_v4(&c->ip4.addrs[i].addr); > + > + /* Fallback to first address */ > + if (c->ip4.addr_count > 0) > + return inany_v4(&c->ip4.addrs[0].addr); > + > + return NULL; > +} > + > /** > * fwd_guest_accessible4() - Is IPv4 address guest-accessible > * @c: Execution context > @@ -515,17 +538,11 @@ static bool fwd_guest_accessible4(const struct ctx *c, > if (IN4_IS_ADDR_UNSPECIFIED(addr)) > return false; > > - /* Check against all configured guest addresses */ > + /* Check against all guest addresses */ > for (i = 0; i < c->ip4.addr_count; i++) > if (IN4_ARE_ADDR_EQUAL(addr, inany_v4(&c->ip4.addrs[i].addr))) > return false; > > - /* Also check addr_seen: it tracks the address the guest is actually > - * using, which may differ from configured addresses. > - */ > - if (IN4_ARE_ADDR_EQUAL(addr, &c->ip4.addr_seen)) > - return false; > - > return true; > } > > @@ -768,10 +785,16 @@ uint8_t fwd_nat_from_host(const struct ctx *c, uint8_t proto, > * match. > */ > if (inany_v4(&ini->eaddr)) { > - if (c->host_lo_to_ns_lo) > + const struct in_addr *guest_addr; cppcheck argues that: fwd.c:1140:26: style: The scope of the variable 'guest_addr' can be reduced. [variableScope] const struct in_addr *guest_addr; ^ > + > + if (c->host_lo_to_ns_lo) { > tgt->eaddr = inany_loopback4; > - else > - tgt->eaddr = inany_from_v4(c->ip4.addr_seen); > + } else { > + guest_addr = fwd_guest_addr4(c); > + if (!guest_addr) > + return PIF_NONE; > + tgt->eaddr = inany_from_v4(*guest_addr); > + } > tgt->oaddr = inany_any4; > } else { > if (c->host_lo_to_ns_lo) > @@ -803,7 +826,11 @@ uint8_t fwd_nat_from_host(const struct ctx *c, uint8_t proto, > tgt->oport = ini->eport; > > if (inany_v4(&tgt->oaddr)) { > - tgt->eaddr = inany_from_v4(c->ip4.addr_seen); > + const struct in_addr *guest_addr = fwd_guest_addr4(c); > + > + if (!guest_addr) > + return PIF_NONE; > + tgt->eaddr = inany_from_v4(*guest_addr); > } else { > if (inany_is_linklocal6(&tgt->oaddr)) > tgt->eaddr.a6 = c->ip6.addr_ll_seen; > diff --git a/inany.h b/inany.h > index 07bfc3d..a334da9 100644 > --- a/inany.h > +++ b/inany.h > @@ -300,6 +300,7 @@ int inany_prefix_pton(const char *src, union inany_addr *dst, int *prefix_len); > /* Flags for struct inany_addr_entry */ > #define INANY_ADDR_CONFIGURED (1 << 0) /* User set via -a */ > #define INANY_ADDR_HOST (1 << 1) /* From host interface */ > +#define INANY_ADDR_OBSERVED (1 << 2) /* Seen in guest traffic */ > > /** > * struct inany_addr_entry - Unified IPv4/IPv6 address entry > diff --git a/migrate.c b/migrate.c > index 48d63a0..01be6f1 100644 > --- a/migrate.c > +++ b/migrate.c > @@ -57,8 +57,16 @@ static int seen_addrs_source_v1(struct ctx *c, > struct migrate_seen_addrs_v1 addrs = { > .addr6 = c->ip6.addr_seen, > .addr6_ll = c->ip6.addr_ll_seen, > - .addr4 = c->ip4.addr_seen, This requires a new version of the migration data, and a specific function to handle migration from v1 to v2. If it's trivial, migration from v2 to v1 (keeping a single address) might also be convenient. > }; > + int i; > + > + /* Find first observed IPv4 address */ > + for (i = 0; i < c->ip4.addr_count; i++) { > + if (c->ip4.addrs[i].flags & INANY_ADDR_OBSERVED) { > + addrs.addr4 = *inany_v4(&c->ip4.addrs[i].addr); > + break; > + } > + } > > (void)stage; > > @@ -70,6 +78,33 @@ static int seen_addrs_source_v1(struct ctx *c, > return 0; > } > > +/** > + * migrate_observed_addr4() - Add observed IPv4 address to ip4_ctx address array > + * @c: Execution context > + * @addr: IPv4 address to add > + */ > +static void migrate_observed_addr4(struct ctx *c, const struct in_addr *addr) > +{ > + int i; > + > + if (IN4_IS_ADDR_UNSPECIFIED(addr)) > + return; > + > + for (i = 0; i < c->ip4.addr_count; i++) { > + if (IN4_ARE_ADDR_EQUAL(addr, inany_v4(&c->ip4.addrs[i].addr))) { > + c->ip4.addrs[i].flags |= INANY_ADDR_OBSERVED; > + return; > + } > + } > + > + if (c->ip4.addr_count < IP4_MAX_ADDRS) { > + c->ip4.addrs[c->ip4.addr_count].addr = inany_from_v4(*addr); > + c->ip4.addrs[c->ip4.addr_count].prefix_len = 0; > + c->ip4.addrs[c->ip4.addr_count].flags = INANY_ADDR_OBSERVED; > + c->ip4.addr_count++; > + } > +} > + > /** > * seen_addrs_target_v1() - Receive and use guest observed addresses on target > * @c: Execution context > @@ -82,6 +117,7 @@ static int seen_addrs_target_v1(struct ctx *c, > const struct migrate_stage *stage, int fd) > { > struct migrate_seen_addrs_v1 addrs; > + struct in_addr addr4; > > (void)stage; > > @@ -90,7 +126,11 @@ static int seen_addrs_target_v1(struct ctx *c, > > c->ip6.addr_seen = addrs.addr6; > c->ip6.addr_ll_seen = addrs.addr6_ll; > - c->ip4.addr_seen = addrs.addr4; > + > + /* Avoid alignment warning */ > + addr4 = addrs.addr4; > + migrate_observed_addr4(c, &addr4); > + > memcpy(c->guest_mac, addrs.mac, sizeof(c->guest_mac)); > > return 0; > diff --git a/passt.h b/passt.h > index 929b474..db09da5 100644 > --- a/passt.h > +++ b/passt.h > @@ -68,7 +68,6 @@ enum passt_modes { > * struct ip4_ctx - IPv4 execution context > * @addrs: IPv4 addresses assigned to guest > * @addr_count: Number of addresses in addrs[] array > - * @addr_seen: Latest IPv4 address seen as source from tap > * @guest_gw: IPv4 gateway as seen by the guest > * @map_host_loopback: Outbound connections to this address are NATted to the > * host's 127.0.0.1 > @@ -87,7 +86,6 @@ struct ip4_ctx { > struct inany_addr_entry addrs[IP4_MAX_ADDRS]; > int addr_count; > > - struct in_addr addr_seen; > struct in_addr guest_gw; > struct in_addr map_host_loopback; > struct in_addr map_guest_addr; > @@ -127,7 +125,6 @@ struct ip6_ctx { > /* PIF_TAP addresses */ > struct inany_addr_entry addrs[IP6_MAX_ADDRS]; > int addr_count; > - Unrelated change. > struct in6_addr addr_seen; > struct in6_addr addr_ll_seen; > struct in6_addr guest_gw; > diff --git a/tap.c b/tap.c > index 7c50013..1a52adb 100644 > --- a/tap.c > +++ b/tap.c > @@ -161,6 +161,37 @@ void tap_send_single(const struct ctx *c, const void *data, size_t l2len) > } > } > > +/** > + * tap_check_src_addr4() - Note an IPv4 address seen in guest traffic > + * @c: Execution context > + * @addr: IPv4 address seen as source from guest > + * > + * Add the address to ip4.addrs[] with OBSERVED flag if not already present. Nit: we don't add . at the end of those descriptions (they're not always full sentences). > + */ > +static void tap_check_src_addr4(struct ctx *c, const struct in_addr *addr) > +{ > + int i; > + > + /* Check if already in array */ > + for (i = 0; i < c->ip4.addr_count; i++) { > + if (IN4_ARE_ADDR_EQUAL(addr, inany_v4(&c->ip4.addrs[i].addr))) { > + c->ip4.addrs[i].flags |= INANY_ADDR_OBSERVED; > + return; > + } > + } > + > + /* Add new entry if space available */ > + if (c->ip4.addr_count < IP4_MAX_ADDRS) { > + c->ip4.addrs[c->ip4.addr_count].addr = inany_from_v4(*addr); > + c->ip4.addrs[c->ip4.addr_count].prefix_len = 0; > + c->ip4.addrs[c->ip4.addr_count].flags = INANY_ADDR_OBSERVED; > + debug("added new IPv4 address at index %d", c->ip6.addr_count); > + c->ip4.addr_count++; > + } else { > + warn("IPv4 address table full, can't add new address"); This is a rather convenient vector for a denial of service: just have passt logging to syslog (default) and then a guest cycling over a set of addresses. It should be debug() instead. And I guess we actually want to reuse the least recently used (_OBSERVED) entry. Given that the array is short, we could keep it ordered, too. > + } > +} > + > /** > * tap_ip6_daddr() - Normal IPv6 destination address for inbound packets > * @c: Execution context > @@ -771,8 +802,8 @@ resume: > continue; > } > > - if (iph->saddr && c->ip4.addr_seen.s_addr != iph->saddr) > - c->ip4.addr_seen.s_addr = iph->saddr; > + if (iph->saddr) > + tap_check_src_addr4(c, (const struct in_addr *)&iph->saddr); > > if (!iov_drop_header(&data, hlen)) > continue; > @@ -950,9 +981,6 @@ resume: > if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.addr_seen)) { > c->ip6.addr_seen = *saddr; > } > - > - if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.addrs[0].addr.a6)) > - c->ip6.addrs[0].addr.a6 = *saddr; > } else if (!IN6_IS_ADDR_UNSPECIFIED(saddr)){ > c->ip6.addr_seen = *saddr; > } -- Stefano