From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 28CE75A0269 for ; Thu, 3 Nov 2022 04:42:37 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4N2qMY4lWFz4xwp; Thu, 3 Nov 2022 14:42:33 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1667446953; bh=ZT5qODHl8gJ+VU6r03kIBn2Uk7Oiw9wILbAUvoSWfhA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=OnLCUy5Ez2nxhGt9FARH3/8QQ93//efqNW7rvpkVDxb8Pak5janU9dVypH4hn061L WI+zRRM7oGDwqDTgFjw8T4XjlBt/C0c/HRnlF/1/sxSyYABhfn1bEjGn4DEfFXI4Gg 2jI5s6jJgILduqh4qc/SvuN6D43sdPjapNHqbq+8= Date: Thu, 3 Nov 2022 14:37:18 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 2/3] conf: Split the notions of read DNS addresses and offered ones Message-ID: References: <20221102230443.377446-1-sbrivio@redhat.com> <20221102230443.377446-3-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="3gdEZWTHDhYlDLQR" Content-Disposition: inline In-Reply-To: <20221102230443.377446-3-sbrivio@redhat.com> Message-ID-Hash: 42XQBYC4VVER2QA4YU4RBANTJF3KG2HG X-Message-ID-Hash: 42XQBYC4VVER2QA4YU4RBANTJF3KG2HG X-MailFrom: dgibson@gandalf.ozlabs.org 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: passt-dev@passt.top, Paul Holzinger X-Mailman-Version: 3.3.3 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: --3gdEZWTHDhYlDLQR Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Nov 03, 2022 at 12:04:42AM +0100, Stefano Brivio wrote: > With --dns-forward, if the host has a loopback address configured as > DNS server, we should actually use it to forward queries, but, if > --no-map-gw is passed, we shouldn't offer the same address via DHCP, > NDP and DHCPv6, because it's not going to be reachable. >=20 > Problematic configuration: systemd-resolved configuring the usual > 127.0.0.53 on the host, and --dns-forward specified with an unrelated > address. We still want to forward queries to 127.0.0.53, so we can't > drop it from the addresses in IPv4 and IPv6 context, I'm not entirely sure what you mean by that. > but we shouldn't > offer that address either. >=20 > With this change, I'm only covering the case of automatically > configured DNS servers from /etc/resolv.conf. We could extend this to > addresses configured with command-line options, but I don't really > see a likely use case at this point. >=20 > Signed-off-by: Stefano Brivio > --- > conf.c | 50 ++++++++++++++++++++++++++++++++++---------------- > dhcp.c | 5 +++-- > dhcpv6.c | 5 +++-- > ndp.c | 6 +++--- > passt.h | 8 ++++++-- > 5 files changed, 49 insertions(+), 25 deletions(-) >=20 > diff --git a/conf.c b/conf.c > index 5b88547..c4e1030 100644 > --- a/conf.c > +++ b/conf.c > @@ -355,10 +355,11 @@ overlap: > */ > static void get_dns(struct ctx *c) > { > + uint32_t *dns4 =3D &c->ip4.dns[0], *dns4_send =3D &c->ip4.dns_send[0]; > + struct in6_addr *dns6_send =3D &c->ip6.dns_send[0]; > int dns4_set, dns6_set, dnss_set, dns_set, fd; > struct in6_addr *dns6 =3D &c->ip6.dns[0]; > struct fqdn *s =3D c->dns_search; > - uint32_t *dns4 =3D &c->ip4.dns[0]; > struct lineread resolvconf; > int line_len; > char *line, *p, *end; > @@ -388,30 +389,45 @@ static void get_dns(struct ctx *c) > if (!dns4_set && > dns4 - &c->ip4.dns[0] < ARRAY_SIZE(c->ip4.dns) - 1 && > inet_pton(AF_INET, p + 1, dns4)) { > - /* We can only access local addresses via the gw redirect */ > - if (ntohl(*dns4) >> IN_CLASSA_NSHIFT =3D=3D IN_LOOPBACKNET) { > - if (c->no_map_gw) { > - *dns4 =3D 0; > + /* Guest or container can only access local > + * addresses via local redirect > + */ > + if (IPV4_IS_LOOPBACK(ntohl(*dns4))) { > + if (c->no_map_gw) > continue; In this case shouldn't you still be recording the local address in the dns[] array (but not dns_send[]) since it's a valid nameserver for the host. In which case you'd need to advance the dns4 pointer. If I'm mistaken and you don't want to record it in the dns[] array, then shouldn't you clear it (because otherwise you will record it if this is the last "nameserver" line). > - } > - *dns4 =3D c->ip4.gw; > + > + *dns4_send =3D c->ip4.gw; > + } else { > + *dns4_send =3D *dns4; > } I think it would be clearer to update *dns4 if necessary, then set *dns4_send =3D *dns4 outside the if statement. > + > + dns4_send++; > dns4++; > - *dns4 =3D 0; > + > + *dns4 =3D *dns4_send =3D 0; > } > =20 > if (!dns6_set && > dns6 - &c->ip6.dns[0] < ARRAY_SIZE(c->ip6.dns) - 1 && > inet_pton(AF_INET6, p + 1, dns6)) { > - /* We can only access local addresses via the gw redirect */ > + /* Guest or container can only access local > + * addresses via local redirect > + */ > if (IN6_IS_ADDR_LOOPBACK(dns6)) { > - if (c->no_map_gw) { > - memset(dns6, 0, sizeof(*dns6)); > + if (c->no_map_gw) > continue; > - } > - memcpy(dns6, &c->ip6.gw, sizeof(*dns6)); > + > + memcpy(dns6_send, &c->ip6.gw, > + sizeof(*dns6_send)); > + } else { > + memcpy(dns6_send, &c->ip6.gw, > + sizeof(*dns6_send)); > } > + > + dns6_send++; > dns6++; > + > + memset(dns6_send, 0, sizeof(*dns6_send)); > memset(dns6, 0, sizeof(*dns6)); > } > } else if (!dnss_set && strstr(line, "search ") =3D=3D line && > @@ -832,10 +848,11 @@ static void conf_print(const struct ctx *c) > inet_ntop(AF_INET, &c->ip4.gw, buf4, sizeof(buf4))); > } > =20 > - for (i =3D 0; c->ip4.dns[i]; i++) { > + for (i =3D 0; c->ip4.dns_send[i]; i++) { > if (!i) > info("DNS:"); > - inet_ntop(AF_INET, &c->ip4.dns[i], buf4, sizeof(buf4)); > + inet_ntop(AF_INET, &c->ip4.dns_send[i], buf4, > + sizeof(buf4)); > info(" %s", buf4); > } > =20 > @@ -866,7 +883,8 @@ static void conf_print(const struct ctx *c) > inet_ntop(AF_INET6, &c->ip6.addr_ll, buf6, sizeof(buf6))); > =20 > dns6: > - for (i =3D 0; !IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns[i]); i++) { > + for (i =3D 0; !IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_send[i]); > + i++) { > if (!i) > info("DNS:"); > inet_ntop(AF_INET6, &c->ip6.dns[i], buf6, sizeof(buf6)); > diff --git a/dhcp.c b/dhcp.c > index d22698a..e816eb6 100644 > --- a/dhcp.c > +++ b/dhcp.c > @@ -355,8 +355,9 @@ int dhcp(const struct ctx *c, const struct pool *p) > opts[26].s[1] =3D c->mtu % 256; > } > =20 > - for (i =3D 0, opts[6].slen =3D 0; !c->no_dhcp_dns && c->ip4.dns[i]; i++= ) { > - ((uint32_t *)opts[6].s)[i] =3D c->ip4.dns[i]; > + opts[6].slen =3D 0; > + for (i =3D 0; !c->no_dhcp_dns && c->ip4.dns_send[i]; i++) { > + ((uint32_t *)opts[6].s)[i] =3D c->ip4.dns_send[i]; > opts[6].slen +=3D sizeof(uint32_t); > } > =20 > diff --git a/dhcpv6.c b/dhcpv6.c > index e763aed..67262e6 100644 > --- a/dhcpv6.c > +++ b/dhcpv6.c > @@ -379,7 +379,7 @@ static size_t dhcpv6_dns_fill(const struct ctx *c, ch= ar *buf, int offset) > if (c->no_dhcp_dns) > goto search; > =20 > - for (i =3D 0; !IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns[i]); i++) { > + for (i =3D 0; !IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_send[i]); i++) { > if (!i) { > srv =3D (struct opt_dns_servers *)(buf + offset); > offset +=3D sizeof(struct opt_hdr); > @@ -387,7 +387,8 @@ static size_t dhcpv6_dns_fill(const struct ctx *c, ch= ar *buf, int offset) > srv->hdr.l =3D 0; > } > =20 > - memcpy(&srv->addr[i], &c->ip6.dns[i], sizeof(srv->addr[i])); > + memcpy(&srv->addr[i], &c->ip6.dns_send[i], > + sizeof(srv->addr[i])); > srv->hdr.l +=3D sizeof(srv->addr[i]); > offset +=3D sizeof(srv->addr[i]); > } > diff --git a/ndp.c b/ndp.c > index 80e1f19..6d79477 100644 > --- a/ndp.c > +++ b/ndp.c > @@ -121,7 +121,7 @@ int ndp(struct ctx *c, const struct icmp6hdr *ih, con= st struct in6_addr *saddr) > if (c->no_dhcp_dns) > goto dns_done; > =20 > - for (n =3D 0; !IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns[n]); n++); > + for (n =3D 0; !IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_send[n]); n++); > if (n) { > *p++ =3D 25; /* RDNSS */ > *p++ =3D 1 + 2 * n; /* length */ > @@ -130,8 +130,8 @@ int ndp(struct ctx *c, const struct icmp6hdr *ih, con= st struct in6_addr *saddr) > p +=3D 4; > =20 > for (i =3D 0; i < n; i++) { > - memcpy(p, &c->ip6.dns[i], 16); /* address */ > - p +=3D 16; > + memcpy(p, &c->ip6.dns_send[i], 16); > + p +=3D 16; /* address */ > } > =20 > for (n =3D 0; *c->dns_search[n].n; n++) > diff --git a/passt.h b/passt.h > index 67281db..9f9bf3b 100644 > --- a/passt.h > +++ b/passt.h > @@ -101,7 +101,8 @@ enum passt_modes { > * @addr_seen: Latest IPv4 address seen as source from tap > * @mask: IPv4 netmask, network order > * @gw: Default IPv4 gateway, network order > - * @dns: IPv4 DNS addresses, zero-terminated, network order > + * @dns: Host IPv4 DNS addresses, zero-terminated, network order > + * @dns_send: Offered IPv4 DNS, zero-terminated, network order > * @dns_fwd: Address forwarded (UDP) to first IPv4 DNS, network order > */ > struct ip4_ctx { > @@ -110,6 +111,7 @@ struct ip4_ctx { > uint32_t mask; > uint32_t gw; > uint32_t dns[MAXNS + 1]; > + uint32_t dns_send[MAXNS + 1]; > uint32_t dns_fwd; > }; > =20 > @@ -120,7 +122,8 @@ struct ip4_ctx { > * @addr_seen: Latest IPv6 global/site address seen as source from tap > * @addr_ll_seen: Latest IPv6 link-local address seen as source from tap > * @gw: Default IPv6 gateway > - * @dns: IPv6 DNS addresses, zero-terminated > + * @dns: Host IPv6 DNS addresses, zero-terminated > + * @dns_send: Offered IPv6 DNS addresses, zero-terminated > * @dns_fwd: Address forwarded (UDP) to first IPv6 DNS, network order > */ > struct ip6_ctx { > @@ -130,6 +133,7 @@ struct ip6_ctx { > struct in6_addr addr_ll_seen; > struct in6_addr gw; > struct in6_addr dns[MAXNS + 1]; > + struct in6_addr dns_send[MAXNS + 1]; > struct in6_addr dns_fwd; > }; > =20 --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --3gdEZWTHDhYlDLQR Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEoULxWu4/Ws0dB+XtgypY4gEwYSIFAmNjN2cACgkQgypY4gEw YSJmthAA1MVfj7nVcNbZk/lQ3DV53C/VvBT8FvxEF8ew7CUCrgRHigdgCUksYwh4 i89OeeL5TZSpGD1CSuqDZ94nXF0FNBhyrC6+llZTGenTF+XSNrIlANto3bfS3vJ3 K0Ixi3BfrAyZdG0xg/c/KUoe2z1+ux7R+1UpTWu/s6EWq5mkQ35PdWLJcEpJeeQM 9kisCnuzC0AGm31EZfBi93YNkvMsHboKRC4iU+c6vioMjHypAmkQ55DLJqHYMuv1 m64+lUSDG/Z1LTCwhSv3PElEsH7pSHyhcc4bdGjKtg4a1meqbCkLyRjnY5h9ystc mVLBI6Gxiah2B20HYmPrPcr3d56LyZxROwZiGjAikcK9kL9Xa+Oj+/quwoeHgGLx YUO8BGW7cljjjJy51/Gqmz8a1cKne/Hs40WtCtizrqoj8r8IJl/bFXQnwuzsNhqj pvtmqmhRxY3ElEvSVwV5Al3xcYFWxfw4bz7cJMiT33TQQPU0UrKyLoJMFhFuyfag PqQCSh5OdOpcmy27UPB8KdhlvwAiQn2UkTarsXvLsA3iTUuRqcnmr3JHsmXdwBBD UBCTIb8qli8f0XVdHrX+7OZlABJcr6AcQciFuLf0N/t1+DLreuYw2AoDdclIY3YK FbY9JJDrT+7/0AhHlMvOxzuyqwVATqA0XBF2m9+psggiKGQGiVk= =qbVn -----END PGP SIGNATURE----- --3gdEZWTHDhYlDLQR--