Skip to content

Parse & encode the IPv6 fragment header in network byte order#149

Open
deusmatrix wants to merge 1 commit into
JulianSchmid:masterfrom
deusmatrix:wc/encode_decode_fragment_header_network_byte_order
Open

Parse & encode the IPv6 fragment header in network byte order#149
deusmatrix wants to merge 1 commit into
JulianSchmid:masterfrom
deusmatrix:wc/encode_decode_fragment_header_network_byte_order

Conversation

@deusmatrix
Copy link
Copy Markdown

@deusmatrix deusmatrix commented Jun 3, 2026

The 13-bit fragment offset and the "more fragments" (M) flag were encoded/decoded with the wrong bit positions: the offset's low bits were packed into byte 3 bits 4..0 and M was read from byte 3 bit 7. Per RFC 8200 §4.5, bytes 2..3 form a big-endian 16-bit word where the offset is the top 13 bits (word >> 3) and M is the least significant bit.

The encode and decode bugs were symmetric, so to_bytes() round-trips parsed back cleanly and every existing test passed — but the bytes on the wire did not match the RFC or any other stack. A hand-crafted byte-level test (network_byte_order) is added to break that symmetry and lock the layout.

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced IPv6 fragment header parsing with improved safety mechanisms and RFC 8200 compliance.
    • Refined fragment offset and more-fragments bit decoding logic for increased accuracy.
  • Tests

    • Added comprehensive test coverage for IPv6 fragment header network byte order serialization and deserialization.

The 13-bit fragment offset and the "more fragments" (M) flag were
encoded/decoded with the wrong bit positions: the offset's low bits were
packed into byte 3 bits 4..0 and M was read from byte 3 bit 7. Per
RFC 8200 §4.5, bytes 2..3 form a big-endian 16-bit word where the offset
is the top 13 bits (word >> 3) and M is the least significant bit.

The encode and decode bugs were symmetric, so to_bytes() round-trips
parsed back cleanly and every existing test passed — but the bytes on the
wire did not match the RFC or any other stack. A hand-crafted byte-level
test (network_byte_order) is added to break that symmetry and lock the
layout.

The bit layout is now defined in a single place (Ipv6FragmentHeaderSlice);
read/read_limited and is_fragmenting_payload delegate to it so the three
copies can no longer drift apart.

Note: this changes the serialized bytes for any header with a non-zero
offset or the M flag set, so it is a wire-format-breaking fix.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 040b690c-9dbf-4d06-9a7f-68082fc925be

📥 Commits

Reviewing files that changed from the base of the PR and between 7cb4b6f and 6889de9.

📒 Files selected for processing (2)
  • etherparse/src/net/ipv6_fragment_header.rs
  • etherparse/src/net/ipv6_fragment_header_slice.rs

📝 Walkthrough

Walkthrough

This PR refactors IPv6 fragment header bit-parsing to centralize wire-format decoding in slice accessors, updates header serialization to match the new bit layout, consolidates deserialization to use slice-based parsing, and adds comprehensive RFC 8200 compliance testing through round-trip verification.

Changes

IPv6 Fragment Header Bit-Layout Refactoring

Layer / File(s) Summary
Slice bit-decoding refactoring
etherparse/src/net/ipv6_fragment_header_slice.rs
fragment_offset() now derives the 13-bit offset by reading two bytes as a big-endian u16 and right-shifting by 3. more_fragments() tests the M flag using a simplified bitmask. is_fragmenting_payload() is rewritten to use accessor methods instead of unsafe byte-level checks. Documentation example updated for the new M-bit value.
Header serialization bit computation
etherparse/src/net/ipv6_fragment_header.rs (lines 151–161, 379–390)
to_bytes() serialization refactored to compute fragment-offset + M-flag bits via left-shift of fragment_offset.value() combined with conditional M-flag insertion. Test expectations adjusted to match the new computation.
Header deserialization consolidation
etherparse/src/net/ipv6_fragment_header.rs (lines 60–62, 82–84)
read() and read_limited() consolidated to call Ipv6FragmentHeaderSlice::from_slice_unchecked(&buffer).to_header(), eliminating manual byte-to-field parsing and adding safety comments.
RFC 8200 compliance testing and documentation
etherparse/src/net/ipv6_fragment_header.rs (lines 15, 400–435)
Documentation typo corrected ("Identifcation" → "Identification"). New network_byte_order test verifies exact RFC 8200 §4.5 wire layout through round-trip to_bytes() and from_slice() operations for maximum offset, offset with/without M flag, and edge cases.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Suggested reviewers

  • JulianSchmid

Poem

🐰 Bits align in perfect rows,
Fragment headers now pristine and clear,
Where slices parse what RFC shows,
Round-tripping checks without a fear! 🎯

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Parse & encode the IPv6 fragment header in network byte order' directly and clearly summarizes the main change: fixing the IPv6 fragment header to use proper network byte order per RFC 8200. This is the core objective of the PR.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@deusmatrix deusmatrix marked this pull request as draft June 3, 2026 01:12
@JulianSchmid JulianSchmid marked this pull request as ready for review June 3, 2026 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant