Skip to contentSkip to conversation

ip_frag: fix IPV6 frag id

PATCH New Stephen Hemminger 1 participant 3 messages Nov 21

lore
Share & tools
lore
#0 lore raw

cover ip_frag: fix IPV6 frag id

The DPDK ip_frag library was not doing IPV6 fragmentation headers correctly. Found by OSS Scanner.

Stephen Hemminger (2):
ip_frag: generate a fragment identification for IPv6
test: add test for IPv6 fragmentation id

 app/test/test_ipfrag.c               | 54 +++++++++++++++++++++++++++-
 lib/ip_frag/rte_ipv6_fragmentation.c | 14 ++++++--
 2 files changed, 64 insertions(+), 4 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 ip_frag: generate a fragment identification for IPv6

The current ip_frag library always writes id of zero in the fragment header. This can cause problems at receiver because all fragments between the same addresses are then interchangeable, so a lost fragment splices two datagrams instead of timing out.

The existing code does not follow the relevant RFC's for this. RFC 8200 section 4.5 wants an Identification distinct from other recent packets between those addresses,
RFC 7739 an unpredictable one.

Bugzilla ID: 2153

Fixes 0aa31d7a5929 ("ip_frag: add IPv6 fragmentation support")
Reported-by Anthropic OSS scanner (ANT-2026-70V47B5Y)
---
 lib/ip_frag/rte_ipv6_fragmentation.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
lib/ip_frag/rte_ipv6_fragmentation.c +11 -3
index 791193f181..69ac9012b1 100644
@@ -6,6 +6,7 @@
 #include <errno.h>
 
 #include <eal_export.h>
+#include <rte_random.h>
 
 #include "ip_frag_common.h"
 
@@ -19,7 +20,7 @@
 static inline void
 __fill_ipv6hdr_frag(struct rte_ipv6_hdr *dst,
 		const struct rte_ipv6_hdr *src, uint16_t len, uint16_t fofs,
-		uint32_t mf)
+		uint32_t mf, rte_be32_t id)
 {
 	struct rte_ipv6_fragment_ext *fh;
 
@@ -31,7 +32,7 @@ __fill_ipv6hdr_frag(struct rte_ipv6_hdr *dst,
 	fh->next_header = src->proto;
 	fh->reserved = 0;
 	fh->frag_data = rte_cpu_to_be_16(RTE_IPV6_SET_FRAG_DATA(fofs, mf));
-	fh->id = 0;
+	fh->id = id;
 }
 
 static inline void
@@ -78,6 +79,7 @@ rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
 	uint32_t more_in_segs;
 	uint16_t fragment_offset, frag_size;
 	uint64_t frag_bytes_remaining;
+	rte_be32_t frag_id;
 
 	/*
 	 * Formal parameter checking.
@@ -117,6 +119,12 @@ rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
 	out_pkt_pos = 0;
 	fragment_offset = 0;
 
+	/*
+	 * An unfragmented packet carries no Identification, so generate one.
+	 * RFC 8200 wants it distinct per packet, RFC 7739 unpredictable.
+	 */
+	frag_id = (rte_be32_t)rte_rand();
+
 	more_in_segs = 1;
 	while (likely(more_in_segs)) {
 		struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
@@ -188,7 +196,7 @@ rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
 
 		__fill_ipv6hdr_frag(out_hdr, in_hdr,
 		    (uint16_t) out_pkt->pkt_len - sizeof(struct rte_ipv6_hdr),
-		    fragment_offset, more_in_segs);
+		    fragment_offset, more_in_segs, frag_id);
 
 		fragment_offset = (uint16_t)(fragment_offset +
 		    out_pkt->pkt_len - sizeof(struct rte_ipv6_hdr)
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/2 test: add test for IPv6 fragmentation id

In reply to Stephen Hemminger

The fragment id's must be consistent across all fragments and should be random.

---
 app/test/test_ipfrag.c | 54 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)
app/test/test_ipfrag.c +53 -1
index 247575d6c5..e518b5eb3a 100644
@@ -483,6 +483,57 @@ test_ip_frag(void)
 	return result;
 }
 
+/*
+ * All fragments of one IPv6 packet carry the same Identification, and two
+ * packets get different ones (RFC 8200 section 4.5).
+ */
+static int
+test_ipv6_frag_id(void)
+{
+	rte_be32_t ids[2];
+	size_t i;
+
+	for (i = 0; i < RTE_DIM(ids); i++) {
+		struct rte_mbuf *pkts_out[BURST];
+		struct rte_mbuf *b = rte_pktmbuf_alloc(pkt_pool);
+		int32_t len, j;
+
+		RTE_TEST_ASSERT_NOT_EQUAL(b, NULL, "Failed to allocate pkt.");
+
+		v6_allocate_packet_of(b, 0x41414141, 1400, 64, IPPROTO_ICMP,
+				      rte_rand_max(UINT16_MAX));
+
+		len = rte_ipv6_fragment_packet(b, pkts_out, BURST, 1280,
+					       direct_pool, indirect_pool);
+		rte_pktmbuf_free(b);
+		RTE_TEST_ASSERT_EQUAL(len, 2, "Failed to fragment packet %zd.",
+				      i);
+
+		ids[i] = 0;
+		for (j = 0; j < len; j++) {
+			struct rte_ipv6_fragment_ext *fh =
+				rte_pktmbuf_mtod_offset(pkts_out[j],
+					struct rte_ipv6_fragment_ext *,
+					sizeof(struct rte_ipv6_hdr));
+
+			if (j == 0)
+				ids[i] = fh->id;
+			else if (fh->id != ids[i]) {
+				test_free_fragments(pkts_out, len);
+				RTE_TEST_ASSERT(0,
+					"Fragments of packet %zd differ in id.",
+					i);
+			}
+		}
+		test_free_fragments(pkts_out, len);
+	}
+
+	RTE_TEST_ASSERT_NOT_EQUAL(ids[0], ids[1],
+				  "Two packets share a fragment id.");
+
+	return TEST_SUCCESS;
+}
+
 static struct unit_test_suite ipfrag_testsuite  = {
 	.suite_name = "IP Frag Unit Test Suite",
 	.setup = testsuite_setup,
@@ -490,7 +541,8 @@ static struct unit_test_suite ipfrag_testsuite  = {
 	.unit_test_cases = {
 		TEST_CASE_ST(ut_setup, ut_teardown,
 			     test_ip_frag),
-
+		TEST_CASE_ST(ut_setup, ut_teardown,
+			     test_ipv6_frag_id),
 		TEST_CASES_END() /**< NULL terminate unit test array */
 	}
 };
Signature · 1 line
2.53.0