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 1FF575A026B for ; Tue, 8 Nov 2022 02:02:14 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4N5qZB1rlWz4xx3; Tue, 8 Nov 2022 12:02:10 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1667869330; bh=hW31vIyyI7uNgQbA5pjyKIa4oOi7wjX+eNZDXQvJtVc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dZHcm6IYsymuZusXxrL07eHVeGiKyg/yOCd+OvlRgVyof49pSfo//utQzmyLGBtt6 6WQI7qaz0z8PwLOQPFktZhRWkvnHsrg1StcaLWGOahQvf7BIJovG44CWroRArlRldr 5RerLUC4hqPLd5FMB7YfMU011n4UQaUtXTnlPBvg= Date: Tue, 8 Nov 2022 11:46:57 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 02/10] tcp: Helper to encode IPv4-mapped IPv6 addresses Message-ID: References: <20221104084333.3761760-1-david@gibson.dropbear.id.au> <20221104084333.3761760-3-david@gibson.dropbear.id.au> <20221107190819.397599a9@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="zIYsYDj0UbJysJSw" Content-Disposition: inline In-Reply-To: <20221107190819.397599a9@elisabeth> Message-ID-Hash: 57M3TVNNVTETIMR4KF2W7I2GKDGFNKL3 X-Message-ID-Hash: 57M3TVNNVTETIMR4KF2W7I2GKDGFNKL3 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: --zIYsYDj0UbJysJSw Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Nov 07, 2022 at 07:08:19PM +0100, Stefano Brivio wrote: > On Fri, 4 Nov 2022 19:43:25 +1100 > David Gibson wrote: >=20 > > In the tcp_conn structure we have space for an IPv6 address, and use > > IPv4-mapped IPv6 addresses to describe IPv4 connections. We open code > > the construction of those IPv4-mapped address in two places. > >=20 > > Avoid the duplication with a helper function. >=20 > Much nicer, indeed. >=20 > > Signed-off-by: David Gibson > > --- > > tcp.c | 9 ++------- > > util.c | 20 ++++++++++++++++++++ > > util.h | 1 + > > 3 files changed, 23 insertions(+), 7 deletions(-) > >=20 > > diff --git a/tcp.c b/tcp.c > > index 3d48d6e..26dd268 100644 > > --- a/tcp.c > > +++ b/tcp.c > > @@ -2217,9 +2217,7 @@ static void tcp_conn_from_tap(struct ctx *c, int = af, const void *addr, > > sa =3D (struct sockaddr *)&addr4; > > sl =3D sizeof(addr4); > > =20 > > - memset(&conn->a.a4.zero, 0, sizeof(conn->a.a4.zero)); > > - memset(&conn->a.a4.one, 0xff, sizeof(conn->a.a4.one)); > > - memcpy(&conn->a.a4.a, addr, sizeof(conn->a.a4.a)); > > + encode_ip4mapped_ip6(&conn->a.a6, addr); > > } else { > > sa =3D (struct sockaddr *)&addr6; > > sl =3D sizeof(addr6); > > @@ -2902,15 +2900,12 @@ static void tcp_conn_from_sock(struct ctx *c, u= nion epoll_ref ref, > > =20 > > memcpy(&sa4, &sa, sizeof(sa4)); > > =20 > > - memset(&conn->a.a4.zero, 0, sizeof(conn->a.a4.zero)); > > - memset(&conn->a.a4.one, 0xff, sizeof(conn->a.a4.one)); > > - > > 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; > > =20 > > - conn->a.a4.a =3D sa4.sin_addr; > > + encode_ip4mapped_ip6(&conn->a.a6, &sa4.sin_addr); > > =20 > > conn->sock_port =3D ntohs(sa4.sin_port); > > conn->tap_port =3D ref.r.p.tcp.tcp.index; > > diff --git a/util.c b/util.c > > index 514bd44..257d0b6 100644 > > --- a/util.c > > +++ b/util.c > > @@ -482,3 +482,23 @@ int write_file(const char *path, const char *buf) > > close(fd); > > return len =3D=3D 0 ? 0 : -1; > > } > > + > > +struct ip4mapped_ip6 { > > + uint8_t zero[10]; > > + uint8_t one[2]; > > + struct in_addr a4; > > +}; >=20 > Document fields even if they're obvious. Can we reuse this part in > struct conn instead of using struct in6_addr there as you do later in > 7/10? I don't have a strong preference though. Yeah, we could. Originally I was trying to make this even more hidden, with the decode function returning a struct in_addr *, that would be NULL if the IPv6 address wasn't v4-mapped. Unfortunately I seemed to hit more weird compiler aliasing issues that caused hard to predict failures. Hrm... your comment gives me some thoughts on maybe a better way to do this, I'll look into it. >=20 > > + > > +/** > > + * encode_ip4mapped_ip6() - Convert an IPv4 address to an IPv4-mapped = IPv6 address > > + * @ip6: Buffer to store the IPv4-mapped IPv6 address > > + * @ip4: IPv4 address, network order > > + */ > > +void encode_ip4mapped_ip6(struct in6_addr *ip6, const void *ip4) > > +{ > > + struct ip4mapped_ip6 *a =3D (struct ip4mapped_ip6 *)ip6; > > + > > + memset(&a->zero, 0, sizeof(a->zero)); > > + memset(&a->one, 0xff, sizeof(a->one)); > > + memcpy(&a->a4, ip4, sizeof(a->a4)); > > +} > > diff --git a/util.h b/util.h > > index 2d4e1ff..f7d6a6f 100644 > > --- a/util.h > > +++ b/util.h > > @@ -209,5 +209,6 @@ void write_pidfile(int fd, pid_t pid); > > int __daemon(int pidfile_fd, int devnull_fd); > > int fls(unsigned long x); > > int write_file(const char *path, const char *buf); > > +void encode_ip4mapped_ip6(struct in6_addr *ip6, const void *ip4); > > =20 > > #endif /* UTIL_H */ >=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 --zIYsYDj0UbJysJSw Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEoULxWu4/Ws0dB+XtgypY4gEwYSIFAmNppvsACgkQgypY4gEw YSKrEBAAtL14QLn9vPEXs4L2KhcZ6H9CIDlziltbXHZCZZY+mBeAb7aBYKNf3ICM SHt6zBvTvQ474u42xba65FMCmpM9ekZbNnvWT61/be2MgFcMq9G6l6b03Vmq2ZNj UpspP4k4VooywstdyMRmGxj6ifRERu/c/HtMwnFSGPm9IpCFjg8JzWhiX4HPY6ai O0WzonWlv++AnGm6dzAWcFawFXFHkQ8J5z1mDFUY90+gL+R3N8InkNZN0ZnI5kia iR190QzyscKlPIUnUWhgI/gVfW53INVuMY2TgXpeVbbA+hwNWdEjZNZ3140INlXq XVncUqS/2+/lOerxN4TVjVf7Xkz9ry174yFbEWjJXHtu+jPriYFl6eEupT9R89i3 rAbUzokofRgWH+f2eS0h1TJC/4qGQDWADjuUfdhRspqmhBBkuK2zq2laQi/0iU14 svtkr/Qxhim2syzHlPjPOZLuBoUzshNVLJgcJ/gEz3NvuRD5IKBDR0+yt/XeOjw/ H869CGCISnml1xrgMunLBu10R5dAUmANUyb5oFmNVjAOsYMK4L+YIOLKsIWT51v7 PwxZhzbfweUHIIA9UgEp/AwIzC5R4ILJZaYFAvzAblIqRCkZTifZ/VposKDG0mki LBSq8tJzq6HJWIlpxQ9HRTbqToU2JPwgyTBPEMTQaRz2gqf54O8= =+OCI -----END PGP SIGNATURE----- --zIYsYDj0UbJysJSw--