Add a function to check the version in IPv6 headers.

Signed-off-by: Robin Jarry <rja...@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

Reply via email to