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 B0C375A026E for ; Thu, 17 Nov 2022 03:08:56 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4NCNcx2rx3z4xZv; Thu, 17 Nov 2022 13:08:49 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1668650929; bh=V1cRBaQrO1pkQV2bWqguDaFJPW1RbdYwUAiMBAkwE1o=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=DJm12BowmW1n4qTqlkVbxqR1eNn7t7vjFXUHIq3vpAD9CulE2eVL6bnM86MLsmdEY F4pJszHcessHm7U8ZYvOW5sQIE8tesO7FBBUIhTQMZP6Yrid3s5rQlYanTIy1j/qEC R4nbF/ZvvWOpcqroNhu4VTi68U5hEctFnb+4nMg4= Date: Thu, 17 Nov 2022 13:00:50 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 27/32] tcp: NAT IPv4-mapped IPv6 addresses like IPv4 addresses Message-ID: References: <20221116044212.3876516-1-david@gibson.dropbear.id.au> <20221116044212.3876516-28-david@gibson.dropbear.id.au> <20221117011520.4e42fe79@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="SXuRsDXqWqkj3abN" Content-Disposition: inline In-Reply-To: <20221117011520.4e42fe79@elisabeth> Message-ID-Hash: AEXCIK2CN3NG2475GSGGHYBNTA75NKOZ X-Message-ID-Hash: AEXCIK2CN3NG2475GSGGHYBNTA75NKOZ 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 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: --SXuRsDXqWqkj3abN Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Nov 17, 2022 at 01:15:20AM +0100, Stefano Brivio wrote: > On Wed, 16 Nov 2022 15:42:07 +1100 > David Gibson wrote: >=20 > > passt usually doesn't NAT, but it does do so for the remapping of the > > gateway address to refer to the host. Currently we perform this NAT wi= th > > slightly different rules on both IPv4 addresses and IPv6 addresses, but= not > > on IPv4-mapped IPv6 addresses. This means we won't correctly handle the > > case of an IPv4 connection over an IPv6 socket, which is possible on Li= nux > > (and probably other platforms). >=20 > By the way, I really think it's just Linux, I can't think of other > examples. Hmm... so descriptions I've seen of the IPv4-mapped IPv6 addresses seem to imply this is the behaviour in a number of systems. e.g. https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses > > Refactor tcp_conn_from_sock() to perform the NAT after converting either > > address family into an inany_addr, so IPv4 and and IPv4-mapped addresses > > have the same representation. > >=20 > > With two new helpers this lets us remove the IPv4 and IPv6 specific pat= hs > > from tcp_conn_from_sock(). > >=20 > > Signed-off-by: David Gibson > > --- > > inany.h | 30 ++++++++++++++++++++++++++-- > > tcp.c | 62 ++++++++++++++++++++++++--------------------------------- > > 2 files changed, 54 insertions(+), 38 deletions(-) > >=20 > > diff --git a/inany.h b/inany.h > > index 4e53da9..a677aa7 100644 > > --- a/inany.h > > +++ b/inany.h > > @@ -30,11 +30,11 @@ union inany_addr { > > * > > * Return: IPv4 address if @addr is IPv4, NULL otherwise > > */ > > -static inline const struct in_addr *inany_v4(const union inany_addr *a= ddr) > > +static inline struct in_addr *inany_v4(const union inany_addr *addr) >=20 > There must be a reason, but I can't understand why this change is > needed here. Because in tcp_snat_inbound() we want to modify, not just examine the IPv4 address within the IPv6 address. Ideally the return would be const if and only if the input is, but C can't express that. This appears to be the conventional half-arsed solution (see, e.g. memchr() or strstr()). > > { > > if (!IN6_IS_ADDR_V4MAPPED(&addr->a6)) > > return NULL; > > - return &addr->_v4mapped.a4; > > + return (struct in_addr *)&addr->_v4mapped.a4; > > } > > =20 > > /** inany_equals - Compare two IPv[46] addresses > > @@ -66,3 +66,29 @@ static inline void inany_from_af(union inany_addr *a= a, int af, const void *addr) > > assert(0); > > } > > } > > + > > +/** inany_from_sockaddr - Extract IPv[46] address and port number from= sockaddr > > + * @a: Pointer to store IPv[46] address >=20 > This is aa below, I'm not sure why. Fixed. > > + * @port: Pointer to store port number, host order > > + * @addr: struct sockaddr_in (IPv4) or struct sockaddr_in6 (IPv6) >=20 > This became sa_ (needless to say, addr would make more sense). Good call, changed. > > + */ > > +static inline void inany_from_sockaddr(union inany_addr *aa, in_port_t= *port, > > + const void *sa_) > > +{ > > + const struct sockaddr *sa =3D (const struct sockaddr *)sa_; > > + > > + if (sa->sa_family =3D=3D AF_INET6) { > > + struct sockaddr_in6 *sa6 =3D (struct sockaddr_in6 *)sa; > > + > > + inany_from_af(aa, AF_INET6, &sa6->sin6_addr); > > + *port =3D ntohs(sa6->sin6_port); > > + } else if (sa->sa_family =3D=3D AF_INET) { > > + struct sockaddr_in *sa4 =3D (struct sockaddr_in *)sa; > > + > > + inany_from_af(aa, AF_INET, &sa4->sin_addr); > > + *port =3D ntohs(sa4->sin_port); > > + } else { > > + /* Not valid to call with other address families */ > > + assert(0); > > + } > > +} > > diff --git a/tcp.c b/tcp.c > > index b05ed6c..fca5df4 100644 > > --- a/tcp.c > > +++ b/tcp.c > > @@ -2724,6 +2724,29 @@ static void tcp_connect_finish(struct ctx *c, st= ruct tcp_tap_conn *conn) > > conn_flag(c, conn, ACK_FROM_TAP_DUE); > > } > > =20 > > +static void tcp_snat_inbound(const struct ctx *c, union inany_addr *ad= dr) >=20 > What this does is kind of obvious, still a comment would be nice. Good point, added. Especially since I'm hoping to share this with UDP at some later point. > > +{ > > + struct in_addr *addr4 =3D inany_v4(addr); > > + > > + if (addr4) { > > + if (IN4_IS_ADDR_LOOPBACK(addr4) || > > + IN4_IS_ADDR_UNSPECIFIED(addr4) || > > + IN4_ARE_ADDR_EQUAL(addr4, &c->ip4.addr_seen)) > > + *addr4 =3D c->ip4.gw; > > + } else { > > + struct in6_addr *addr6 =3D &addr->a6; > > + > > + if (IN6_IS_ADDR_LOOPBACK(addr6) || > > + IN6_ARE_ADDR_EQUAL(addr6, &c->ip6.addr_seen) || > > + IN6_ARE_ADDR_EQUAL(addr6, &c->ip6.addr)) { > > + if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw)) > > + *addr6 =3D c->ip6.gw; > > + else > > + *addr6 =3D c->ip6.addr_ll; > > + } > > + } > > +} > > + > > /** > > * tcp_tap_conn_from_sock() - Initialize state for non-spliced connect= ion > > * @c: Execution context > > @@ -2744,43 +2767,10 @@ static void tcp_tap_conn_from_sock(struct ctx *= c, union epoll_ref ref, > > conn->ws_to_tap =3D conn->ws_from_tap =3D 0; > > conn_event(c, conn, SOCK_ACCEPTED); > > =20 > > - if (sa->sa_family =3D=3D AF_INET6) { > > - struct sockaddr_in6 sa6; > > - > > - memcpy(&sa6, sa, sizeof(sa6)); > > - > > - if (IN6_IS_ADDR_LOOPBACK(&sa6.sin6_addr) || > > - IN6_ARE_ADDR_EQUAL(&sa6.sin6_addr, &c->ip6.addr_seen) || > > - IN6_ARE_ADDR_EQUAL(&sa6.sin6_addr, &c->ip6.addr)) { > > - struct in6_addr *src; > > + inany_from_sockaddr(&conn->addr, &conn->sock_port, sa); > > + conn->tap_port =3D ref.r.p.tcp.tcp.index; > > =20 > > - if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw)) > > - src =3D &c->ip6.gw; > > - else > > - src =3D &c->ip6.addr_ll; > > - > > - memcpy(&sa6.sin6_addr, src, sizeof(*src)); > > - } > > - > > - inany_from_af(&conn->addr, AF_INET6, &sa6.sin6_addr); > > - > > - conn->sock_port =3D ntohs(sa6.sin6_port); > > - conn->tap_port =3D ref.r.p.tcp.tcp.index; > > - } else { > > - struct sockaddr_in sa4; > > - > > - memcpy(&sa4, sa, sizeof(sa4)); > > - > > - if (IN4_IS_ADDR_LOOPBACK(&sa4.sin_addr) || > > - IN4_IS_ADDR_UNSPECIFIED(&sa4.sin_addr) || > > - IN4_ARE_ADDR_EQUAL(&sa4.sin_addr, &c->ip4.addr_seen)) > > - sa4.sin_addr =3D c->ip4.gw; > > - > > - inany_from_af(&conn->addr, AF_INET, &sa4.sin_addr); > > - > > - conn->sock_port =3D ntohs(sa4.sin_port); > > - conn->tap_port =3D ref.r.p.tcp.tcp.index; > > - } > > + tcp_snat_inbound(c, &conn->addr); > > =20 > > tcp_seq_init(c, conn, now); > > tcp_hash_insert(c, conn); >=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 --SXuRsDXqWqkj3abN Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEoULxWu4/Ws0dB+XtgypY4gEwYSIFAmN1lcsACgkQgypY4gEw YSLNtg//fJDs/XCXWsqMZjHY9bIECM60M0ZwegXktkcj+GqMMtxoA3qzd8QEvBmD 0YLYo3xAdeLBU3FnsgQ+gS2q0OySyE3mfOuLeBXcz6V1xcrW3vHcPVY9x2YKDv/h QlZfcPWTaS8gi60quQXYl6lF1W9fWN/e06wYbXznXS6qyUvGa2OE0pIO2OEq9RhI fpQhX4Agunpqq7he5yhVxv5l+XoUqFzFmA5B9hn6tdAYa5+IrePzvjEYO94b531A 66J+aaCXHLquf/Ooij0QuttT8ORQsH2mrl9I6Nhmt1Ft7ghWIBByHMXlwcpc8aFc xH4Yzrl1QmWPUv9FXojdEEXv4xV2xSgyr1jdtapfBDra09IfrLiT4iXD9eh6vfC6 fQ/SVKX/Hk9jE2LlxZEChkkiSJw13lxBeuUaH6EJa5kPUC4exDOnZ74PVpx/nX3l y99+lLRE9o/92dbRHLN065MLfnRpo3W6AaKTbjOLVjYbL6APjOVK/cniF+jnZb14 A6oBpb2joCjrWbMrojc8ZnH/xNyFhPlcTVQt38izVeBM5wv7nIlzpyZcbSk8Hx/o sZ36J3u7uN0hhYGsTmjzNwzQCn5kuE3Kvo363EHz0LbfBB1ckk1gduuzB+t7wSN2 Ll3mDS5/lAWsunZ0tDtUiJueureo4tgFcgRdQZgHTEwmGSoWnkE= =4/wV -----END PGP SIGNATURE----- --SXuRsDXqWqkj3abN--