Skip to content

Fix BSD route parsing: truncated trailing netmask reported as prefix 0 - #52

Merged
xmh0511 merged 3 commits into
mainfrom
copilot/fix-bsd-route-truncation-issue
Oct 12, 2026
Merged

xmh0511 merged 3 commits into
mainfrom
copilot/fix-bsd-route-truncation-issue

Conversation

Copilot AI commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

On macOS, RouteListener::listen() reports ordinary routes such as 10.0.0.0/8 as prefix 0, so they look like default routes. BSD truncates netmask sockaddrs to their significant bytes. When RTA_NETMASK is the last address, the remaining buffer was shorter than size_of::<sockaddr>(), so the walk skipped it and message_to_route mapped the missing entry to prefix = 0. RouteManager::list() was unaffected because its sysctl dumps carry more addresses after the netmask.

I couldn't build or test this on macOS or a BSD, so it is untested. RouteListener::listen() against the issue's RFC1918 routes should now report prefixes 8, 12, 16 and 16.

All changes are in src/unix_bsd/mod.rs:

  • Sockaddr walk (message_to_route)
    • Reads sa_len first and advances by the aligned length instead of requiring a full sockaddr to remain.
    • Keeps the netmask as the raw bytes that are present (&buf[..min(sa_len, buf.len())]) rather than a &sockaddr.
    • Non-netmask addresses are still stored as &sockaddr only when a full sockaddr is available and sa_len fits in the buffer. A short non-netmask address is skipped, but the walk still advances past it so later addresses stay aligned.
    • The walk stops instead of panicking when cur_pos runs past the message.
    • The macOS ROUNDUP now uses usize, so it can't overflow u8 for sa_len ≥ 253.
  • Prefix calculation
    • Copies the available bytes into a zeroed 16-byte buffer, taken from offset 4 for IPv4 and offset 8 for IPv6, then counts leading_ones.
    • This removes the transmute that read 16 bytes from a 5–8 byte region.
    • A zero-length or absent netmask still gives prefix 0, as before.
  • Message type filter (deserialize_res)
    • Only RTM_ADD, RTM_DELETE, RTM_CHANGE and RTM_GET are parsed. Other types, such as RTM_IFINFO, are skipped before rtm_errno is read.
    • Previously a tun coming up produced a bogus Err(Os { code: <mtu> }). That error also discarded any RTM_ADD/RTM_DELETE batched into the same read().
    • list() uses NET_RT_DUMP, which emits RTM_GET, so it still passes the filter.

@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1960106e-8bf8-4010-b86e-5c3707717df5

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Co-authored-by: xmh0511 <17973891+xmh0511@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix BSD route reporting for truncated netmask Fix BSD route parsing: truncated trailing netmask reported as prefix 0 Oct 10, 2026
Copilot AI requested a review from xmh0511 October 10, 2026 09:47
@xmh0511
xmh0511 requested a review from vnt-dev October 10, 2026 09:49
@xmh0511
xmh0511 marked this pull request as ready for review October 12, 2026 08:02
@xmh0511
xmh0511 merged commit 9c0d1cf into main Oct 12, 2026
13 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.

BSD: routes with a truncated trailing netmask are reported as prefix 0

2 participants