From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTP id 8B5675A0265 for ; Mon, 7 Nov 2022 19:08:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667844507; 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=JDkyzoMp6taQ69E9bR8TCIBPbsAPbUKtBl1VnKCcq48=; b=H3E+AfKxHlKABZsf0s+/OXjY8D/pJyQIXaDqMxXIDtc5PcL6DZJ+kLacejR8sVbcbbms0a vXxfdwXwMS0UgKMmtuyddonZJJRrHyEv51j4mwVSnkl0tZB0rADEjRHs9HVBpxK+GBJtNp WJOo75jlcDAUkMoL9gCsKBkdDXKhXPA= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-630-5TiSUE5tOV25rRkLHfYYSw-1; Mon, 07 Nov 2022 13:08:24 -0500 X-MC-Unique: 5TiSUE5tOV25rRkLHfYYSw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B6EE71C08969; Mon, 7 Nov 2022 18:08:23 +0000 (UTC) Received: from maya.cloud.tilaa.com (ovpn-208-9.brq.redhat.com [10.40.208.9]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7D53440C2064; Mon, 7 Nov 2022 18:08:23 +0000 (UTC) Date: Mon, 7 Nov 2022 19:08:19 +0100 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 02/10] tcp: Helper to encode IPv4-mapped IPv6 addresses Message-ID: <20221107190819.397599a9@elisabeth> In-Reply-To: <20221104084333.3761760-3-david@gibson.dropbear.id.au> References: <20221104084333.3761760-1-david@gibson.dropbear.id.au> <20221104084333.3761760-3-david@gibson.dropbear.id.au> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: VCR5YM5EZC5QOEZ7LYR6RETSHPONV52J X-Message-ID-Hash: VCR5YM5EZC5QOEZ7LYR6RETSHPONV52J 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: 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: On Fri, 4 Nov 2022 19:43:25 +1100 David Gibson wrote: > 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. > > Avoid the duplication with a helper function. Much nicer, indeed. > Signed-off-by: David Gibson > --- > tcp.c | 9 ++------- > util.c | 20 ++++++++++++++++++++ > util.h | 1 + > 3 files changed, 23 insertions(+), 7 deletions(-) > > 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 = (struct sockaddr *)&addr4; > sl = sizeof(addr4); > > - 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 = (struct sockaddr *)&addr6; > sl = sizeof(addr6); > @@ -2902,15 +2900,12 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, > > memcpy(&sa4, &sa, sizeof(sa4)); > > - 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 = c->ip4.gw; > > - conn->a.a4.a = sa4.sin_addr; > + encode_ip4mapped_ip6(&conn->a.a6, &sa4.sin_addr); > > conn->sock_port = ntohs(sa4.sin_port); > conn->tap_port = 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 == 0 ? 0 : -1; > } > + > +struct ip4mapped_ip6 { > + uint8_t zero[10]; > + uint8_t one[2]; > + struct in_addr a4; > +}; 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. > + > +/** > + * 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 = (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); > > #endif /* UTIL_H */ -- Stefano