Skip to content

Tolerate non-zero bits beyond the prefix in IPv6Prefix (fixes #119)#141

Open
jamiesun wants to merge 1 commit into
layeh:masterfrom
talkincode:fix/ipv6prefix-non-octet-prefix
Open

Tolerate non-zero bits beyond the prefix in IPv6Prefix (fixes #119)#141
jamiesun wants to merge 1 commit into
layeh:masterfrom
talkincode:fix/ipv6prefix-non-octet-prefix

Conversation

@jamiesun

@jamiesun jamiesun commented Jun 8, 2026

Copy link
Copy Markdown

Summary

Fixes #119 (follow-up to #118). IPv6Prefix rejected any Framed-IPv6-Prefix / Delegated-IPv6-Prefix attribute whose bits beyond the Prefix-Length were non-zero, returning invalid prefix data.

The validation loop added in 1006025 (the #118 fix) is too strict. Real-world equipment frequently leaves the padding bits set — for example:

  • a /64 sent with the host identifier still present (2001:db8::1/64), or
  • a non-octet-aligned prefix carrying a trailing non-zero bit (/47 with bit 47 set).

Per RFC 3162 those bits should be zero, but rejecting otherwise-valid prefixes breaks decoding of packets from common gear.

Fix

Clear the bits beyond the prefix length instead of erroring:

  • mask the trailing bits of the partial byte (ip[prefixLength/8] &^= 0xff >> (prefixLength%8)), and
  • zero every whole byte after it.

This restores the lenient behaviour that predated #118 while keeping that fix's tolerance for omitted trailing zero bytes. The approach matches the suggestion in review comment r134862652 on the original commit.

Tests

TestIPv6Prefix_issue119 covers both the host-bits-set /64 case and the non-octet /47 case; both fail on the previous code with invalid prefix data and pass now. Existing TestIPv6Prefix and TestIPv6Prefix_issue118 continue to pass, and go test ./... / go vet ./... are clean.

Thanks to @dav-komarek for the report and reproduction.

IPv6Prefix rejected any Framed-IPv6-Prefix/Delegated-IPv6-Prefix
attribute whose bits past the Prefix-Length were non-zero, returning
"invalid prefix data". Real-world equipment frequently leaves those
padding bits set (for example sending a /64 with the host identifier
still present, or a non-octet-aligned prefix with a trailing bit), so
valid prefixes failed to decode.

Clear the bits beyond the prefix length instead of erroring: mask the
partial byte and zero every whole byte after it. This restores the
lenient behaviour that predated the issue layeh#118 fix while still keeping
that fix's tolerance for trailing zero bytes being omitted.

Closes layeh#119
Refs layeh#118

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

IPv6Prefix incorrectly handles IPv6 with prefix non-divisible by 8

1 participant