From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 2277045B68;
	Fri, 18 Oct 2024 11:19:51 +0200 (CEST)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 14DFC40E45;
	Fri, 18 Oct 2024 11:18:42 +0200 (CEST)
Received: from us-smtp-delivery-124.mimecast.com
 (us-smtp-delivery-124.mimecast.com [170.10.133.124])
 by mails.dpdk.org (Postfix) with ESMTP id 319DC40E34
 for <dev@dpdk.org>; Fri, 18 Oct 2024 11:18:39 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1729243118;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:mime-version:mime-version:content-type:content-type:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references;
 bh=yGcJcVW256w6BiugMdu56n6ovfX9KOloYMjrfwdQ7+w=;
 b=bNHg7Wmpg1Q599Rq0FGdBeSB6tjlGH91l1EtPCtAFpy+4njI9rOSnq7ag8y/a2WanQt4rs
 uMmPcY0hYk2AfeBftWvgr28HhcNw03431FLAJCuzo7dMRiMBZq+QlO86lPAgzehGFTL76+
 N5oNQUzXX1q4Ki6E+1/HYLC65ty4RUk=
Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com
 (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by
 relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,
 cipher=TLS_AES_256_GCM_SHA384) id us-mta-460-TY5BF98_N3y4ZQirQXEgZQ-1; Fri,
 18 Oct 2024 05:18:35 -0400
X-MC-Unique: TY5BF98_N3y4ZQirQXEgZQ-1
Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com
 (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS
 id B99891955F40
 for <dev@dpdk.org>; Fri, 18 Oct 2024 09:18:34 +0000 (UTC)
Received: from ringo.redhat.com (unknown [10.39.208.23])
 by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP
 id CC8CC19560AD; Fri, 18 Oct 2024 09:18:33 +0000 (UTC)
From: Robin Jarry <rjarry@redhat.com>
To: dev@dpdk.org
Subject: [PATCH dpdk v4 17/17] net: add function to check IPv6 version
Date: Fri, 18 Oct 2024 11:17:34 +0200
Message-ID: <20241018091734.64601-18-rjarry@redhat.com>
In-Reply-To: <20241018091734.64601-1-rjarry@redhat.com>
References: <20240821162516.610624-17-rjarry@redhat.com>
 <20241018091734.64601-1-rjarry@redhat.com>
MIME-Version: 1.0
X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40
X-Mimecast-Spam-Score: 0
X-Mimecast-Originator: redhat.com
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset="US-ASCII"; x-default=true
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

Add a function to check the version in IPv6 headers.

Signed-off-by: Robin Jarry <rjarry@redhat.com>
---
 app/test/test_net_ip6.c | 16 ++++++++++++++++
 lib/net/rte_ip6.h       | 14 ++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/app/test/test_net_ip6.c b/app/test/test_net_ip6.c
index 94033421ad0b..9cc10b1f714f 100644
--- a/app/test/test_net_ip6.c
+++ b/app/test/test_net_ip6.c
@@ -9,6 +9,21 @@
 static const struct rte_ipv6_addr mask_full = RTE_IPV6_MASK_FULL;
 static const struct rte_ipv6_addr zero_addr = RTE_IPV6_ADDR_UNSPEC;
 
+static int
+test_ipv6_check_version(void)
+{
+	struct rte_ipv6_hdr h;
+
+	h.vtc_flow = 0;
+	TEST_ASSERT_EQUAL(rte_ipv6_check_version(&h), -EINVAL, "");
+	h.vtc_flow = RTE_BE32(0x7f00ba44);
+	TEST_ASSERT_EQUAL(rte_ipv6_check_version(&h), -EINVAL, "");
+	h.vtc_flow = RTE_BE32(0x6badcaca);
+	TEST_ASSERT_EQUAL(rte_ipv6_check_version(&h), 0, "");
+
+	return 0;
+}
+
 static int
 test_ipv6_addr_mask(void)
 {
@@ -167,6 +182,7 @@ test_ether_mcast_from_ipv6(void)
 static int
 test_net_ipv6(void)
 {
+	TEST_ASSERT_SUCCESS(test_ipv6_check_version(), "");
 	TEST_ASSERT_SUCCESS(test_ipv6_addr_mask(), "");
 	TEST_ASSERT_SUCCESS(test_ipv6_addr_eq_prefix(), "");
 	TEST_ASSERT_SUCCESS(test_ipv6_addr_kind(), "");
diff --git a/lib/net/rte_ip6.h b/lib/net/rte_ip6.h
index d02b225303e9..94bc5a39b348 100644
--- a/lib/net/rte_ip6.h
+++ b/lib/net/rte_ip6.h
@@ -364,6 +364,20 @@ struct rte_ipv6_hdr {
 	struct rte_ipv6_addr dst_addr;	/**< IP address of destination host(s). */
 } __rte_packed;
 
+/**
+ * Check that the IPv6 header version field is valid according to RFC 8200 section 3.
+ *
+ * @return
+ *   0 if the version field is valid. -EINVAL otherwise.
+ */
+static inline int rte_ipv6_check_version(const struct rte_ipv6_hdr *ip)
+{
+	uint8_t version = ((const uint8_t *)ip)[0];
+	if ((version & 0xf0) != 0x60)
+		return -EINVAL;
+	return 0;
+}
+
 /* IPv6 routing extension type definition. */
 #define RTE_IPV6_SRCRT_TYPE_4 4
 
-- 
2.47.0