Skip to content

fix: normalize IPv4 addresses and fix byte alignment in Multipath Via attribute#1196

Merged
aboch merged 1 commit into
vishvananda:mainfrom
barisaydogdu:fix/multipath-via-ipv4-alignment
Jun 29, 2026
Merged

fix: normalize IPv4 addresses and fix byte alignment in Multipath Via attribute#1196
aboch merged 1 commit into
vishvananda:mainfrom
barisaydogdu:fix/multipath-via-ipv4-alignment

Conversation

@barisaydogdu

@barisaydogdu barisaydogdu commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Fixes an issue where passing an IPv4 address generated by net.ParseIP() to a Multipath Via attribute causes the kernel to reject the route with an invalid gateway error.

The problem is that net.ParseIP() returns a 16-byte slice, and Via.Encode() was writing all 16 bytes to the RTA_VIA attribute. The kernel strictly expects 4 bytes for AF_INET.

This PR simply adds a .To4() conversion inside Via.Encode() for FAMILY_V4 to ensure we only serialize the correct 4 bytes. I also included a test (TestRouteMultiPathViaIPv4Mapped) to cover this case.

Fixes #1064

Summary by CodeRabbit

  • Bug Fixes

    • Normalized IPv4 Via address encoding by converting inputs to proper IPv4 form, returning an error when an IPv4 conversion isn’t possible to better match kernel behavior.
  • Tests

    • Added a kernel-version-gated regression test for multipath routes using IPv4-mapped Via addresses (add/verify/delete).
    • Added unit tests for Via encoding with IPv4 AddrFamily, validating the encoded family and IPv4 bytes for 1.1.1.1.

@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 82ef28ba-291d-4513-8f14-31d701ced6f1

📥 Commits

Reviewing files that changed from the base of the PR and between 3cddaf8 and de69024.

📒 Files selected for processing (2)
  • route_linux.go
  • route_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • route_linux.go
  • route_test.go

📝 Walkthrough

Walkthrough

Via.Encode in route_linux.go is updated to normalize the encoded address to 4-byte form via To4() when AddrFamily is FAMILY_V4, returning an error if the conversion fails. Two tests verify the fix: TestViaEncodeIPv4Mapped directly validates Via.Encode behavior, and TestRouteMultiPathViaIPv4Mapped exercises the full multipath route add/list/delete lifecycle with a 16-byte IPv4-mapped address.

Changes

Via.Encode IPv4 normalization and test coverage

Layer / File(s) Summary
Via.Encode IPv4 normalization with unit and regression tests
route_linux.go, route_test.go
Via.Encode now calls To4() on v.Addr when AddrFamily is FAMILY_V4 and returns an error if the result is nil, ensuring 16-byte IPv4-mapped addresses are normalized to 4 bytes. TestViaEncodeIPv4Mapped validates the encode method directly with an IPv4-mapped address. TestRouteMultiPathViaIPv4Mapped exercises the full add/list/delete cycle with a net.ParseIP("1.1.1.1") Via on a kernel ≥ 5.4.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

  • #1064: [Bug] RouteAdd always fail when using Multipath & NexthopInfo{}.Via — The fix directly resolves the reported failure: Via.Encode now handles 16-byte IPv4-mapped addresses (produced by net.ParseIP) by normalizing them to 4-byte form, eliminating the need for callers to manually call To4().
  • #1114 — The retrieved issue also describes IPv4-mapped IPv6 address handling problems in Via.Encode, which this normalization fix addresses.

Poem

🐇 A four-byte IPv4 address is all the kernel needs,
Yet sixteen bytes crept in, causing sad "invalid" reads.
With a quick To4() call, the bytes are set aright,
Multipath routes now hop along without a failed write.
✨ No more .To4() for you, dear caller — the library handles it!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: normalizing IPv4 addresses in the Via attribute for Multipath routes to fix byte alignment issues.
Linked Issues check ✅ Passed The PR implementation directly addresses issue #1064 by normalizing IPv4 addresses via .To4() conversion in Via.Encode() and includes regression tests.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the IPv4 address normalization issue in the Via attribute and the corresponding test coverage.

✏️ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
route_test.go (2)

2513-2513: 🧹 Nitpick | 🔵 Trivial | 💤 Low value

Consider clarifying the error message.

The phrase "Bug still persists" may confuse future readers, as it suggests the test is expected to fail. Consider rephrasing to focus on the expected behavior:

-    t.Fatalf("Failed to add route with 16-byte IPv4 Via address (Bug still persists): %v", err)
+    t.Fatalf("RouteAdd should handle 16-byte IPv4 Via addresses: %v", err)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@route_test.go` at line 2513, The error message in the Fatalf call contains
the phrase "Bug still persists" which is confusing and suggests the test is
expected to fail. Replace this phrase with clearer language that describes the
expected behavior and what failed. Instead of implying a bug persists, focus the
message on what the test was attempting to do (add a route with a 16-byte IPv4
Via address) and that this operation failed unexpectedly.

2512-2522: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Consider verifying the Via attribute after listing.

The test confirms the route was added, but doesn't verify that the Via attribute round-trips correctly. TestRouteViaAddDel (lines 2457-2461) demonstrates this pattern:

got := routes[0].Via
want := route.MultiPath[0].Via
if !want.Equal(got) {
    t.Fatalf("Route Via attribute does not match; got: %s, want: %s", got, want)
}

Adding this check would strengthen confidence in the encode/decode round-trip for 16-byte IPv4 addresses.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@route_test.go` around lines 2512 - 2522, The test verifies that a route with
a 16-byte IPv4 Via address was added to the kernel, but it does not verify that
the Via attribute round-trips correctly when retrieved. After the existing
length check that confirms len(routes) == 1, add a verification step that
compares the Via attribute from the returned route (routes[0].Via) with the Via
attribute from the original route object using the Equal method, following the
same pattern as shown in TestRouteViaAddDel where it checks if want.Equal(got)
for the Via attribute comparison.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@route_test.go`:
- Line 2513: The error message in the Fatalf call contains the phrase "Bug still
persists" which is confusing and suggests the test is expected to fail. Replace
this phrase with clearer language that describes the expected behavior and what
failed. Instead of implying a bug persists, focus the message on what the test
was attempting to do (add a route with a 16-byte IPv4 Via address) and that this
operation failed unexpectedly.
- Around line 2512-2522: The test verifies that a route with a 16-byte IPv4 Via
address was added to the kernel, but it does not verify that the Via attribute
round-trips correctly when retrieved. After the existing length check that
confirms len(routes) == 1, add a verification step that compares the Via
attribute from the returned route (routes[0].Via) with the Via attribute from
the original route object using the Equal method, following the same pattern as
shown in TestRouteViaAddDel where it checks if want.Equal(got) for the Via
attribute comparison.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3bed55b7-1078-460f-bb9c-0511d24dfd57

📥 Commits

Reviewing files that changed from the base of the PR and between c05a276 and 87f49a0.

📒 Files selected for processing (2)
  • route_linux.go
  • route_test.go

@barisaydogdu barisaydogdu force-pushed the fix/multipath-via-ipv4-alignment branch 2 times, most recently from a263e89 to 1101b9f Compare June 21, 2026 20:51

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@route_test.go`:
- Around line 2524-2527: After the RouteDel(route) function call completes
without error, add a post-delete assertion to verify that the route has actually
been removed. Use a filtered list check (similar to how routes were listed
earlier in the test) and verify that the result length is zero, ensuring the
test covers the complete regression lifecycle of add, list, and delete
operations.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4766201d-04da-482a-af2d-eac4a9dbab2b

📥 Commits

Reviewing files that changed from the base of the PR and between a263e89 and 1101b9f.

📒 Files selected for processing (2)
  • route_linux.go
  • route_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • route_linux.go

Comment thread route_test.go
@barisaydogdu barisaydogdu force-pushed the fix/multipath-via-ipv4-alignment branch from 1101b9f to 3cddaf8 Compare June 21, 2026 20:56
@barisaydogdu barisaydogdu force-pushed the fix/multipath-via-ipv4-alignment branch from 3cddaf8 to de69024 Compare June 22, 2026 20:27
@barisaydogdu

barisaydogdu commented Jun 28, 2026

Copy link
Copy Markdown
Contributor Author

Hi @vishvananda, @aboch

The PR is ready for review. All CI checks are passing, and full lifecycle regression tests are added for the Multipath Via attribute. Whenever you have a moment, I'd appreciate your feedback.

cc: @alexandremahdhaoui (This should resolve the invalid argument error you encountered in #1064).

@aboch

aboch commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

LGTM

@aboch aboch merged commit 4e35dc9 into vishvananda:main Jun 29, 2026
3 checks passed
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.

[Bug] RouteAdd always fail when using Multipath & NexthopInfo{}.Via

2 participants