From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id EB1585A0270 for ; Fri, 4 Nov 2022 09:43:44 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4N3Z0W2BnSz4xG6; Fri, 4 Nov 2022 19:43:39 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1667551419; bh=GLDLhH/nEVuw51pIVdN0cY1EN5Vd5+SSz8tdqYIaqGI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e8f3BLUWiUzpL6YSbXX9488nH88ocG2lDurPIwt0SHgfbzcD7HW+Snkn+rLzvHm+L e4Gc1mtlF7iIZhQW57m0nMuPfs4qthT2PlGnmY9nGZy0sFjyC7MywBz+EWoC8419pF HG7l/jxXyf0ugR6TrjZ8uabsQ159S5u1y8SPGZeA= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 02/10] tcp: Helper to encode IPv4-mapped IPv6 addresses Date: Fri, 4 Nov 2022 19:43:25 +1100 Message-Id: <20221104084333.3761760-3-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221104084333.3761760-1-david@gibson.dropbear.id.au> References: <20221104084333.3761760-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: LNWT4EAOQEPD7AWBKKFZT4T2YAHZUTUH X-Message-ID-Hash: LNWT4EAOQEPD7AWBKKFZT4T2YAHZUTUH 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: David Gibson 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: 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. 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; +}; + +/** + * 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 */ -- 2.38.1