Skip to content

feat(netwatch): allow setting SO_MARK on UdpSocket#179

Merged
flub merged 2 commits into
n0-computer:mainfrom
rayfish:feat/socket-mark
Jul 13, 2026
Merged

feat(netwatch): allow setting SO_MARK on UdpSocket#179
flub merged 2 commits into
n0-computer:mainfrom
rayfish:feat/socket-mark

Conversation

@ifdario

@ifdario ifdario commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Adds an optional fwmark to netwatch's UDP socket so a caller can policy-route the socket's own egress, for example around a full-tunnel default route so the tunnel does not swallow the packets carrying it.

What

  • New public UdpSocket::bind_full_with_mark(addr, Option<u32>). Existing bind_full and the other bind_* entrypoints are unchanged (they pass None).
  • The mark threads through bind_raw into SocketState::bind and is stored in both SocketState variants, so it is reapplied on every rebind.
  • Applied via socket2's set_mark under cfg(any(target_os = "linux", target_os = "android")); a no-op on other platforms.

Why

I'm building a full-tunnel client on top of iroh where the endpoint's own underlay UDP packets must skip a 0.0.0.0/0 default route (the standard WireGuard/Tailscale SO_MARK + ip rule approach). netwatch owns the socket and today exposes no way to set a socket option, so this adds a minimal, opt-in hook. None preserves current behaviour exactly.

Happy to adjust the API shape (e.g. a builder-style option) if you'd prefer.

Threads an optional fwmark through UdpSocket::bind_full_with_mark down to
SocketState::bind, stored in the socket state so it is reapplied on rebind.
Applied via socket2 set_mark on Linux and Android, a no-op elsewhere. Lets a
caller policy-route the socket's own egress, for example around a full-tunnel
default route.
@ifdario

ifdario commented Jul 11, 2026

Copy link
Copy Markdown
Contributor Author

Downstream consumer that motivates this: n0-computer/iroh#4406 (draft, exposes it as Endpoint::builder().socket_mark(..)). It's blocked on this landing plus a netwatch release.

@n0bot n0bot Bot added this to iroh Jul 11, 2026
@github-project-automation github-project-automation Bot moved this to 🚑 Needs Triage in iroh Jul 11, 2026
@flub flub changed the title feat(udp): add bind_full_with_mark to set SO_MARK on UDP sockets feat(netwatch): allow setting SO_MARK on UdpSocket Jul 13, 2026

@flub flub left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great feature to add, thanks for working on this! Only some cosmetic API changes needed I think.

Comment thread netwatch/src/udp.rs Outdated
@github-project-automation github-project-automation Bot moved this from 🚑 Needs Triage to 🏗 In progress in iroh Jul 13, 2026
@ifdario
ifdario requested a review from flub July 13, 2026 12:59

@flub flub left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, looks good!

@flub
flub merged commit 420f1fd into n0-computer:main Jul 13, 2026
29 checks passed
@github-project-automation github-project-automation Bot moved this from 🏗 In progress to ✅ Done in iroh Jul 13, 2026
ifdario added a commit to rayfish/net-tools that referenced this pull request Jul 16, 2026
…Options::set_mark

A mark is a one-line configurator, so the dedicated option is redundant.
Reverts the SO_MARK plumbing from n0-computer#179 (unreleased) in favor of the
general hook.
Kiesen pushed a commit to mira-mobility/net-tools that referenced this pull request Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

2 participants