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 C0E015A0279 for ; Wed, 28 Feb 2024 12:25:27 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709119522; bh=aNv+03KlbhIs1XSRccXSxVEawOuYNO487CZ1nF1N3jQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eWs1/oHBputI/WgdnesyQz97ncwqs/dUugHxevkE2osDuKQvDVofJk+uXryMHJKM0 Ccp7Vj4RYZY6wtrgCLNt1MUBZ2B2H5gDDUYoyXMdCj7x3m92CuSiXUgOQNzFRX512T 5fYxT0DtSDaEMF2cL+RVSIeF76YF6KP6W5lihB98vjSEKMVBchHJcPRRnAQJ1zQV3l f8aPMwp3uL4eLsiNYm3gs7j2EsoKpNQVUbNFzEQOCeCq8NgCZ4ZVM58DlMMizOn+Ua 4JEoWxuEpEZ1HsZouBUI010w2r1yIn/fcNkzax6GhhD8Ug9huxqVH3nEUnvVXDNTy0 XD3yXBIivhQLA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TlBq64QYzz4wcH; Wed, 28 Feb 2024 22:25:22 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v3 01/20] inany: Helper to test for various address types Date: Wed, 28 Feb 2024 22:25:01 +1100 Message-ID: <20240228112520.2078220-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240228112520.2078220-1-david@gibson.dropbear.id.au> References: <20240228112520.2078220-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: DYFP2E2ZC3AB7YLHNPQVEBPAYFU2RXCT X-Message-ID-Hash: DYFP2E2ZC3AB7YLHNPQVEBPAYFU2RXCT 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: Add helpers to determine if an inany is loopback, unspecified or multicast, regardless of whether it's a "true" IPv6 address or an IPv4 address represented as v4-mapped. Use the loopback helper to simplify tcp_splice_conn_from_sock() slightly. Signed-off-by: David Gibson --- inany.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ tcp_splice.c | 15 +++------------ 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/inany.h b/inany.h index fe652ff7..d1103802 100644 --- a/inany.h +++ b/inany.h @@ -55,6 +55,56 @@ 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 = inany_v4(a); + + return IN6_IS_ADDR_LOOPBACK(&a->a6) || (v4 && IN4_IS_ADDR_LOOPBACK(v4)); +} + +/** inany_is_unspecified() - Check if address is unspecified + * @a: IPv[46] address + * + * Return: true if @a is either :: or 0.0.0.0 + */ +static inline bool inany_is_unspecified(const union inany_addr *a) +{ + const struct in_addr *v4 = inany_v4(a); + + return IN6_IS_ADDR_UNSPECIFIED(&a->a6) || + (v4 && IN4_IS_ADDR_UNSPECIFIED(v4)); +} + +/** inany_is_multicast() - Check if address is multicast or broadcast + * @a: IPv[46] address + * + * Return: true if @a is IPv6 multicast or IPv4 multicast or broadcast + */ +static inline bool inany_is_multicast(const union inany_addr *a) +{ + const struct in_addr *v4 = inany_v4(a); + + return IN6_IS_ADDR_MULTICAST(&a->a6) || + (v4 && (IN4_IS_ADDR_MULTICAST(v4) || + IN4_IS_ADDR_BROADCAST(v4))); +} + +/** inany_is_unicast() - Check if address is specified and unicast + * @a: IPv[46] address + * + * Return: true if @a is specified and a unicast address + */ +/* cppcheck-suppress unusedFunction */ +static inline bool inany_is_unicast(const union inany_addr *a) +{ + return !inany_is_unspecified(a) && !inany_is_multicast(a); +} + /** 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 5b38a82d..cdc09a4c 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -449,29 +449,20 @@ 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 (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; - } + if (!inany_is_loopback(&aany)) + return false; if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), sizeof(int))) flow_trace(conn, "failed to set TCP_QUICKACK on %i", s); conn->f.type = FLOW_TCP_SPLICE; + conn->flags = inany_v4(&aany) ? 0 : SPLICE_V6; conn->s[0] = s; if (tcp_splice_new(c, conn, ref.port, ref.pif)) -- 2.43.2