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 5906B5A0272 for ; Mon, 29 Jan 2024 05:36:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1706502959; bh=ajR41VZ9uRg+0UPf57ifPpuJfVjPtl4boIZKcjWskgg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hh6X+2V/ClQXHc5yC7IPwwn/jsjXNguvHLXaHyI2rbPe7qyuqPsdYzvU2nSQZ2T4v UZGHVCO8WVgf4o61mbKMS0p6GhaK4FCFiUJQcnKB5IvPrevdV4pWG7UmxfnQqUAQtp vJRF1dylDz+14Vr9nfpdDsZCvS+XV8z3n4b5zbMb43kkv74ptf3r1ofLSshIsz7HQ0 nHHxBn3RnqCdXHY1eDSYimvG1/HgfN4IGgKul1DXvcMCZ+0nKdYyR/lQUWn5QF0fbA OBh401e+ivQAzldDhfhmNH734KcXvdqBawCfizNRquu9imBqmdSHtxpaV8Dzq0u9iF quRe+aHwCNPgg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TNb8b5gKHz4x2T; Mon, 29 Jan 2024 15:35:59 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 05/16] inany: Helper to test for IPv4 or IPv6 loopback address Date: Mon, 29 Jan 2024 15:35:46 +1100 Message-ID: <20240129043557.823451-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240129043557.823451-1-david@gibson.dropbear.id.au> References: <20240129043557.823451-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: ZOLURS4CAQNYJAA4R7FD4LBWC4LHR54L X-Message-ID-Hash: ZOLURS4CAQNYJAA4R7FD4LBWC4LHR54L 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.8 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: tcp_splice_conn_from_sock() needs to check if an inany is either the IPv6 loopback address (::) or an IPv4 loopback address (127.0.0.0/8). We may have other places that also want to check this in future, so simplify it with an inany_is_loopback() helper. Signed-off-by: David Gibson --- inany.h | 13 +++++++++++++ tcp_splice.c | 14 +++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/inany.h b/inany.h index fe652ff7..d758f7ba 100644 --- a/inany.h +++ b/inany.h @@ -55,6 +55,19 @@ static inline bool inany_equals(const union inany_addr *a, return IN6_ARE_ADDR_EQUAL(&a->a6, &b->a6); } +/** inany_is_loopback - Check if address is loopback + * @a: IPv[46] address + * + * Return: true if @a is either ::1 or in 127.0.0.1/8 + */ +static inline bool inany_is_loopback(const union inany_addr *a) +{ + const struct in_addr *v4; + + return IN6_IS_ADDR_LOOPBACK(&a->a6) || + ((v4 = inany_v4(a)) && IN4_IS_ADDR_LOOPBACK(v4)); +} + /** inany_from_af - Set IPv[46] address from IPv4 or IPv6 address * @aa: Pointer to store IPv[46] address * @af: Address family of @addr diff --git a/tcp_splice.c b/tcp_splice.c index 76258e89..fbce4879 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -436,24 +436,16 @@ bool tcp_splice_conn_from_sock(const struct ctx *c, struct tcp_splice_conn *conn, int s, const struct sockaddr *sa) { - const struct in_addr *a4; union inany_addr aany; in_port_t port; ASSERT(c->mode == MODE_PASTA); inany_from_sockaddr(&aany, &port, sa); - a4 = inany_v4(&aany); + if (!inany_is_loopback(&aany)) + return false; - if (a4) { - if (!IN4_IS_ADDR_LOOPBACK(a4)) - return false; - conn->flags = 0; - } else { - if (!IN6_IS_ADDR_LOOPBACK(&aany.a6)) - return false; - conn->flags = SPLICE_V6; - } + conn->flags = inany_v4(&aany) ? 0 : SPLICE_V6; if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), sizeof(int))) flow_trace(conn, "failed to set TCP_QUICKACK on %i", s); -- 2.43.0